Hex Artifact Content
Not logged in

Artifact 860d465dafb346112d53cefee7cfc2448a24cf7c:

File src/sqlite3.c part of check-in [355e34ba9d] - Update to the latest SQLite by drh on 2009-11-04 13:32:50.

0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  ** This file is 
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65  of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20   C source files 
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76  from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 32 30 2e 20 20  ersion 3.6.20.  
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c  By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20   the individual 
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74  C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c  o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68  e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61  e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73  n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69   a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69  on.** unit.  Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f  s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70  mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74  timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a   would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65   possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70   files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d    Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66  provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65   5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77   commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f  hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67  mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f  le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79  is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69  ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75  le SQLite.  To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68  se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20  er.** programs, 
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69  you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69  le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69  te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a  le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69  ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  ng interface to 
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20  ry.  (If you do 
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65  not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61   "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64  der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20  , you will find 
0340: 61 20 63 6f 70 79 20 65 6d 62 65 64 64 65 64 20  a copy embedded 
0350: 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 74 65  within.** the te
0360: 78 74 20 6f 66 20 74 68 69 73 20 66 69 6c 65 2e  xt of this file.
0370: 20 20 53 65 61 72 63 68 20 66 6f 72 20 22 42 65    Search for "Be
0380: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33  gin file sqlite3
0390: 2e 68 22 20 74 6f 20 66 69 6e 64 20 74 68 65 20  .h" to find the 
03a0: 73 74 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20  start.** of the 
03b0: 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74 65 33  embedded sqlite3
03c0: 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 2e 29  .h header file.)
03d0: 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65   Additional code
03e0: 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20 6e 65   files may be ne
03f0: 65 64 65 64 0a 2a 2a 20 69 66 20 79 6f 75 20 77  eded.** if you w
0400: 61 6e 74 20 61 20 77 72 61 70 70 65 72 20 74 6f  ant a wrapper to
0410: 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74   interface SQLit
0420: 65 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69  e with your choi
0430: 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d 69 6e  ce of programmin
0440: 67 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e 20 54  g.** language. T
0450: 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  he code for the 
0460: 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e  "sqlite3" comman
0470: 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69 73 20  d-line shell is 
0480: 61 6c 73 6f 20 69 6e 20 61 0a 2a 2a 20 73 65 70  also in a.** sep
0490: 61 72 61 74 65 20 66 69 6c 65 2e 20 54 68 69 73  arate file. This
04a0: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f   file contains o
04b0: 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65  nly code for the
04c0: 20 63 6f 72 65 20 53 51 4c 69 74 65 20 6c 69 62   core SQLite lib
04d0: 72 61 72 79 2e 0a 2a 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 2a 0a 2a 2a 20 40 28 23 29 20  ess..**.** @(#) 
0ef0: 24 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d 69 74  $Id: sqliteLimit
0f00: 2e 68 2c 76 20 31 2e 31 30 20 32 30 30 39 2f 30  .h,v 1.10 2009/0
0f10: 31 2f 31 30 20 31 36 3a 31 35 3a 30 39 20 64 61  1/10 16:15:09 da
0f20: 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
0f30: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  */../*.** The ma
0f40: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
0f50: 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 69  a TEXT or BLOB i
0f60: 6e 20 62 79 74 65 73 2e 20 20 20 54 68 69 73 20  n bytes.   This 
0f70: 61 6c 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73 20 74  also.** limits t
0f80: 68 65 20 73 69 7a 65 20 6f 66 20 61 20 72 6f 77  he size of a row
0f90: 20 69 6e 20 61 20 74 61 62 6c 65 20 6f 72 20 69   in a table or i
0fa0: 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ndex..**.** The 
0fb0: 68 61 72 64 20 6c 69 6d 69 74 20 69 73 20 74 68  hard limit is th
0fc0: 65 20 61 62 69 6c 69 74 79 20 6f 66 20 61 20 33  e ability of a 3
0fd0: 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  2-bit signed int
0fe0: 65 67 65 72 0a 2a 2a 20 74 6f 20 63 6f 75 6e 74  eger.** to count
0ff0: 20 74 68 65 20 73 69 7a 65 3a 20 32 5e 33 31 2d   the size: 2^31-
1000: 31 20 6f 72 20 32 31 34 37 34 38 33 36 34 37 2e  1 or 2147483647.
1010: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1020: 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 20  TE_MAX_LENGTH.# 
1030: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
1040: 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30  X_LENGTH 1000000
1050: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  000.#endif../*.*
1060: 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 6d 61  * This is the ma
1070: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a  ximum number of.
1080: 2a 2a 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d  **.**    * Colum
1090: 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 0a 2a 2a  ns in a table.**
10a0: 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e      * Columns in
10b0: 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20   an index.**    
10c0: 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 76  * Columns in a v
10d0: 69 65 77 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d  iew.**    * Term
10e0: 73 20 69 6e 20 74 68 65 20 53 45 54 20 63 6c 61  s in the SET cla
10f0: 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 54 45  use of an UPDATE
1100: 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20   statement.**   
1110: 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20   * Terms in the 
1120: 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20  result set of a 
1130: 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1140: 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69  .**    * Terms i
1150: 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 6f  n the GROUP BY o
1160: 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  r ORDER BY claus
1170: 65 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  es of a SELECT s
1180: 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20 20 20  tatement..**    
1190: 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 56  * Terms in the V
11a0: 41 4c 55 45 53 20 63 6c 61 75 73 65 20 6f 66 20  ALUES clause of 
11b0: 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  an INSERT statem
11c0: 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61  ent.**.** The ha
11d0: 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74 20 68  rd upper limit h
11e0: 65 72 65 20 69 73 20 33 32 36 37 36 2e 20 20 4d  ere is 32676.  M
11f0: 6f 73 74 20 64 61 74 61 62 61 73 65 20 70 65 6f  ost database peo
1200: 70 6c 65 20 77 69 6c 6c 0a 2a 2a 20 74 65 6c 6c  ple will.** tell
1210: 20 79 6f 75 20 74 68 61 74 20 69 6e 20 61 20 77   you that in a w
1220: 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 20 64  ell-normalized d
1230: 61 74 61 62 61 73 65 2c 20 79 6f 75 20 75 73 75  atabase, you usu
1240: 61 6c 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e  ally should.** n
1250: 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74 68 61  ot have more tha
1260: 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73 6f 20  n a dozen or so 
1270: 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 20 74  columns in any t
1280: 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0a 2a 2a  able.  And if.**
1290: 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61 73   that is the cas
12a0: 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70  e, there is no p
12b0: 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67 20 6d  oint in having m
12c0: 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77 0a 2a  ore than a few.*
12d0: 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73 20 69  * dozen values i
12e0: 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68  n any of the oth
12f0: 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20 64 65  er situations de
1300: 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a  scribed above..*
1310: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1320: 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20 64 65  _MAX_COLUMN.# de
1330: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
1340: 43 4f 4c 55 4d 4e 20 32 30 30 30 0a 23 65 6e 64  COLUMN 2000.#end
1350: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
1360: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
1370: 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61  a single SQL sta
1380: 74 65 6d 65 6e 74 20 69 6e 20 62 79 74 65 73 2e  tement in bytes.
1390: 0a 2a 2a 0a 2a 2a 20 49 74 20 75 73 65 64 20 74  .**.** It used t
13a0: 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  o be the case th
13b0: 61 74 20 73 65 74 74 69 6e 67 20 74 68 69 73 20  at setting this 
13c0: 76 61 6c 75 65 20 74 6f 20 7a 65 72 6f 20 77 6f  value to zero wo
13d0: 75 6c 64 0a 2a 2a 20 74 75 72 6e 20 74 68 65 20  uld.** turn the 
13e0: 6c 69 6d 69 74 20 6f 66 66 2e 20 20 54 68 61 74  limit off.  That
13f0: 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72   is no longer tr
1400: 75 65 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 70  ue.  It is not p
1410: 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 74 75  ossible.** to tu
1420: 72 6e 20 74 68 69 73 20 6c 69 6d 69 74 20 6f 66  rn this limit of
1430: 66 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  f..*/.#ifndef SQ
1440: 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e  LITE_MAX_SQL_LEN
1450: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  GTH.# define SQL
1460: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
1470: 54 48 20 31 30 30 30 30 30 30 30 30 30 0a 23 65  TH 1000000000.#e
1480: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
1490: 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66  maximum depth of
14a0: 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
14b0: 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c 69 6d  ree. This is lim
14c0: 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f 6d 65  ited to .** some
14d0: 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c 49 54   extent by SQLIT
14e0: 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48  E_MAX_SQL_LENGTH
14f0: 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 20 79  . But sometime y
1500: 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77 61 6e  ou might .** wan
1510: 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72 65 20  t to place more 
1520: 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20 6f 6e  severe limits on
1530: 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74 79 20   the complexity 
1540: 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72 65 73  of an .** expres
1550: 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61  sion..**.** A va
1560: 6c 75 65 20 6f 66 20 30 20 75 73 65 64 20 74 6f  lue of 0 used to
1570: 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c   mean that the l
1580: 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65 6e 66  imit was not enf
1590: 6f 72 63 65 64 2e 0a 2a 2a 20 42 75 74 20 74 68  orced..** But th
15a0: 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  at is no longer 
15b0: 74 72 75 65 2e 20 20 54 68 65 20 6c 69 6d 69 74  true.  The limit
15c0: 20 69 73 20 6e 6f 77 20 73 74 72 69 63 74 6c 79   is now strictly
15d0: 20 65 6e 66 6f 72 63 65 64 0a 2a 2a 20 61 74 20   enforced.** at 
15e0: 61 6c 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69  all times..*/.#i
15f0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
1600: 5f 45 58 50 52 5f 44 45 50 54 48 0a 23 20 64 65  _EXPR_DEPTH.# de
1610: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
1620: 45 58 50 52 5f 44 45 50 54 48 20 31 30 30 30 0a  EXPR_DEPTH 1000.
1630: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
1640: 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1650: 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63   of terms in a c
1660: 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73  ompound SELECT s
1670: 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65  tatement..** The
1680: 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
1690: 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  for compound SEL
16a0: 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 64  ECT statements d
16b0: 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76 65 6c  oes one.** level
16c0: 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f   of recursion fo
16d0: 72 20 65 61 63 68 20 74 65 72 6d 2e 20 20 41 20  r each term.  A 
16e0: 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20 63  stack overflow c
16f0: 61 6e 20 72 65 73 75 6c 74 0a 2a 2a 20 69 66 20  an result.** if 
1700: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65  the number of te
1710: 72 6d 73 20 69 73 20 74 6f 6f 20 6c 61 72 67 65  rms is too large
1720: 2e 20 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20  .  In practice, 
1730: 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20 6e 65 76 65  most SQL.** neve
1740: 72 20 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20  r has more than 
1750: 33 20 6f 72 20 34 20 74 65 72 6d 73 2e 20 20 55  3 or 4 terms.  U
1760: 73 65 20 61 20 76 61 6c 75 65 20 6f 66 20 30 20  se a value of 0 
1770: 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 61 6e  to disable.** an
1780: 79 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e  y limit on the n
1790: 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69  umber of terms i
17a0: 6e 20 61 20 63 6f 6d 70 6f 75 6e 74 20 53 45 4c  n a compount SEL
17b0: 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ECT..*/.#ifndef 
17c0: 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f  SQLITE_MAX_COMPO
17d0: 55 4e 44 5f 53 45 4c 45 43 54 0a 23 20 64 65 66  UND_SELECT.# def
17e0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43  ine SQLITE_MAX_C
17f0: 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 35  OMPOUND_SELECT 5
1800: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1810: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
1820: 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 69  ber of opcodes i
1830: 6e 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d  n a VDBE program
1840: 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74  ..** Not current
1850: 6c 79 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2f 0a  ly enforced..*/.
1860: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1870: 41 58 5f 56 44 42 45 5f 4f 50 0a 23 20 64 65 66  AX_VDBE_OP.# def
1880: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56  ine SQLITE_MAX_V
1890: 44 42 45 5f 4f 50 20 32 35 30 30 30 0a 23 65 6e  DBE_OP 25000.#en
18a0: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  dif../*.** The m
18b0: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
18c0: 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 6e   arguments to an
18d0: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   SQL function..*
18e0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
18f0: 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  _MAX_FUNCTION_AR
1900: 47 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  G.# define SQLIT
1910: 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41  E_MAX_FUNCTION_A
1920: 52 47 20 31 32 37 0a 23 65 6e 64 69 66 0a 0a 2f  RG 127.#endif../
1930: 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  *.** The maximum
1940: 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65   number of in-me
1950: 6d 6f 72 79 20 70 61 67 65 73 20 74 6f 20 75 73  mory pages to us
1960: 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  e for the main d
1970: 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65  atabase.** table
1980: 20 61 6e 64 20 66 6f 72 20 74 65 6d 70 6f 72 61   and for tempora
1990: 72 79 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20  ry tables.  The 
19a0: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43  SQLITE_DEFAULT_C
19b0: 41 43 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23 69 66  ACHE_SIZE.*/.#if
19c0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
19d0: 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23  ULT_CACHE_SIZE.#
19e0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44   define SQLITE_D
19f0: 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
1a00: 45 20 20 32 30 30 30 0a 23 65 6e 64 69 66 0a 23  E  2000.#endif.#
1a10: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
1a20: 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45  FAULT_TEMP_CACHE
1a30: 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53  _SIZE.# define S
1a40: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45  QLITE_DEFAULT_TE
1a50: 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 35  MP_CACHE_SIZE  5
1a60: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1a70: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
1a80: 62 65 72 20 6f 66 20 61 74 74 61 63 68 65 64 20  ber of attached 
1a90: 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 69 73  databases.  This
1aa0: 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e   must be between
1ab0: 20 30 0a 2a 2a 20 61 6e 64 20 33 30 2e 20 20 54   0.** and 30.  T
1ac0: 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f  he upper bound o
1ad0: 6e 20 33 30 20 69 73 20 62 65 63 61 75 73 65 20  n 30 is because 
1ae0: 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  a 32-bit integer
1af0: 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20 75 73   bitmap.** is us
1b00: 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f  ed internally to
1b10: 20 74 72 61 63 6b 20 61 74 74 61 63 68 65 64 20   track attached 
1b20: 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a 23 69  databases..*/.#i
1b30: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
1b40: 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65 66 69  _ATTACHED.# defi
1b50: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54  ne SQLITE_MAX_AT
1b60: 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64 69 66  TACHED 10.#endif
1b70: 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78  .../*.** The max
1b80: 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 61 20  imum value of a 
1b90: 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20 74 68  ?nnn wildcard th
1ba0: 61 74 20 74 68 65 20 70 61 72 73 65 72 20 77 69  at the parser wi
1bb0: 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a 23 69  ll accept..*/.#i
1bc0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
1bd0: 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
1be0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1bf0: 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55  _MAX_VARIABLE_NU
1c00: 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69 66 0a  MBER 999.#endif.
1c10: 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61 67 65  ./* Maximum page
1c20: 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70 70 65   size.  The uppe
1c30: 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 20  r bound on this 
1c40: 76 61 6c 75 65 20 69 73 20 33 32 37 36 38 2e 20  value is 32768. 
1c50: 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a 2a 2a   This a limit.**
1c60: 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68 65 20   imposed by the 
1c70: 6e 65 63 65 73 73 69 74 79 20 6f 66 20 73 74 6f  necessity of sto
1c80: 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 69  ring the value i
1c90: 6e 20 61 20 32 2d 62 79 74 65 20 75 6e 73 69 67  n a 2-byte unsig
1ca0: 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61  ned integer.** a
1cb0: 6e 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74  nd the fact that
1cc0: 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d   the page size m
1cd0: 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f  ust be a power o
1ce0: 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  f 2..**.** If th
1cf0: 69 73 20 6c 69 6d 69 74 20 69 73 20 63 68 61 6e  is limit is chan
1d00: 67 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  ged, then the co
1d10: 6d 70 69 6c 65 64 20 6c 69 62 72 61 72 79 20 69  mpiled library i
1d20: 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a  s technically.**
1d30: 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 69   incompatible wi
1d40: 74 68 20 61 6e 20 53 51 4c 69 74 65 20 6c 69 62  th an SQLite lib
1d50: 72 61 72 79 20 63 6f 6d 70 69 6c 65 64 20 77 69  rary compiled wi
1d60: 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 6c  th a different l
1d70: 69 6d 69 74 2e 20 49 66 0a 2a 2a 20 61 20 70 72  imit. If.** a pr
1d80: 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e 67 20  ocess operating 
1d90: 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 77 69  on a database wi
1da0: 74 68 20 61 20 70 61 67 65 2d 73 69 7a 65 20 6f  th a page-size o
1db0: 66 20 36 35 35 33 36 20 62 79 74 65 73 20 0a 2a  f 65536 bytes .*
1dc0: 2a 20 63 72 61 73 68 65 73 2c 20 74 68 65 6e 20  * crashes, then 
1dd0: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 53  an instance of S
1de0: 51 4c 69 74 65 20 63 6f 6d 70 69 6c 65 64 20 77  QLite compiled w
1df0: 69 74 68 20 74 68 65 20 64 65 66 61 75 6c 74 20  ith the default 
1e00: 70 61 67 65 2d 73 69 7a 65 20 0a 2a 2a 20 6c 69  page-size .** li
1e10: 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  mit will not be 
1e20: 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  able to rollback
1e30: 20 74 68 65 20 61 62 6f 72 74 65 64 20 74 72 61   the aborted tra
1e40: 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 63  nsaction. This c
1e50: 6f 75 6c 64 0a 2a 2a 20 6c 65 61 64 20 74 6f 20  ould.** lead to 
1e60: 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
1e70: 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ion..*/.#ifndef 
1e80: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
1e90: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
1ea0: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
1eb0: 5a 45 20 33 32 37 36 38 0a 23 65 6e 64 69 66 0a  ZE 32768.#endif.
1ec0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  ../*.** The defa
1ed0: 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 64 61  ult size of a da
1ee0: 74 61 62 61 73 65 20 70 61 67 65 2e 0a 2a 2f 0a  tabase page..*/.
1ef0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
1f00: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1f10: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1f20: 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
1f30: 5a 45 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 23  ZE 1024.#endif.#
1f40: 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  if SQLITE_DEFAUL
1f50: 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49  T_PAGE_SIZE>SQLI
1f60: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
1f70: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
1f80: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
1f90: 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
1fa0: 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  E_DEFAULT_PAGE_S
1fb0: 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  IZE SQLITE_MAX_P
1fc0: 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a  AGE_SIZE.#endif.
1fd0: 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72 69 6c  ./*.** Ordinaril
1fe0: 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65 20 69  y, if no value i
1ff0: 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70 72 6f  s explicitly pro
2000: 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20 63 72  vided, SQLite cr
2010: 65 61 74 65 73 20 64 61 74 61 62 61 73 65 73 0a  eates databases.
2020: 2a 2a 20 77 69 74 68 20 70 61 67 65 20 73 69 7a  ** with page siz
2030: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
2040: 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48 6f 77 65  _PAGE_SIZE. Howe
2050: 76 65 72 2c 20 62 61 73 65 64 20 6f 6e 20 63 65  ver, based on ce
2060: 72 74 61 69 6e 0a 2a 2a 20 64 65 76 69 63 65 20  rtain.** device 
2070: 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  characteristics 
2080: 28 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 6e 64  (sector-size and
2090: 20 61 74 6f 6d 69 63 20 77 72 69 74 65 28 29 20   atomic write() 
20a0: 73 75 70 70 6f 72 74 29 2c 0a 2a 2a 20 53 51 4c  support),.** SQL
20b0: 69 74 65 20 6d 61 79 20 63 68 6f 6f 73 65 20 61  ite may choose a
20c0: 20 6c 61 72 67 65 72 20 76 61 6c 75 65 2e 20 54   larger value. T
20d0: 68 69 73 20 63 6f 6e 73 74 61 6e 74 20 69 73 20  his constant is 
20e0: 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75  the maximum valu
20f0: 65 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c  e.** SQLite will
2100: 20 63 68 6f 6f 73 65 20 6f 6e 20 69 74 73 20 6f   choose on its o
2110: 77 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  wn..*/.#ifndef S
2120: 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c  QLITE_MAX_DEFAUL
2130: 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65  T_PAGE_SIZE.# de
2140: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
2150: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
2160: 45 20 38 31 39 32 0a 23 65 6e 64 69 66 0a 23 69  E 8192.#endif.#i
2170: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  f SQLITE_MAX_DEF
2180: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53  AULT_PAGE_SIZE>S
2190: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
21a0: 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49  IZE.# undef SQLI
21b0: 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50  TE_MAX_DEFAULT_P
21c0: 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e  AGE_SIZE.# defin
21d0: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  e SQLITE_MAX_DEF
21e0: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 53  AULT_PAGE_SIZE S
21f0: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
2200: 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  IZE.#endif.../*.
2210: 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  ** Maximum numbe
2220: 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 6f 6e  r of pages in on
2230: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
2240: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 72  .**.** This is r
2250: 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65 20 64  eally just the d
2260: 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
2270: 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f 63 6f   the max_page_co
2280: 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a 20 54  unt pragma..** T
2290: 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20 62 65  his value can be
22a0: 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72 61 69   lowered (or rai
22b0: 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69 6d 65  sed) at run-time
22c0: 20 75 73 69 6e 67 20 74 68 61 74 20 74 68 65 0a   using that the.
22d0: 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e  ** max_page_coun
22e0: 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e  t macro..*/.#ifn
22f0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  def SQLITE_MAX_P
2300: 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65 66 69  AGE_COUNT.# defi
2310: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  ne SQLITE_MAX_PA
2320: 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37 34 31  GE_COUNT 1073741
2330: 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  823.#endif../*.*
2340: 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
2350: 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 20 74   (in bytes) of t
2360: 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 61 20  he pattern in a 
2370: 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20  LIKE or GLOB.** 
2380: 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66  operator..*/.#if
2390: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
23a0: 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
23b0: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  GTH.# define SQL
23c0: 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54  ITE_MAX_LIKE_PAT
23d0: 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30  TERN_LENGTH 5000
23e0: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
23f0: 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66  Maximum depth of
2400: 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 20 74   recursion for t
2410: 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 41  riggers..**.** A
2420: 20 76 61 6c 75 65 20 6f 66 20 31 20 6d 65 61 6e   value of 1 mean
2430: 73 20 74 68 61 74 20 61 20 74 72 69 67 67 65 72  s that a trigger
2440: 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20 6e 6f   program will no
2450: 74 20 62 65 20 61 62 6c 65 20 74 6f 20 69 74 73  t be able to its
2460: 65 6c 66 0a 2a 2a 20 66 69 72 65 20 61 6e 79 20  elf.** fire any 
2470: 74 72 69 67 67 65 72 73 2e 20 41 20 76 61 6c 75  triggers. A valu
2480: 65 20 6f 66 20 30 20 6d 65 61 6e 73 20 74 68 61  e of 0 means tha
2490: 74 20 6e 6f 20 74 72 69 67 67 65 72 20 70 72 6f  t no trigger pro
24a0: 67 72 61 6d 73 20 61 74 20 61 6c 6c 20 0a 2a 2a  grams at all .**
24b0: 20 6d 61 79 20 62 65 20 65 78 65 63 75 74 65 64   may be executed
24c0: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
24d0: 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f  ITE_MAX_TRIGGER_
24e0: 44 45 50 54 48 0a 23 69 66 20 64 65 66 69 6e 65  DEPTH.#if define
24f0: 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53  d(SQLITE_SMALL_S
2500: 54 41 43 4b 29 0a 23 20 64 65 66 69 6e 65 20 53  TACK).# define S
2510: 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45  QLITE_MAX_TRIGGE
2520: 52 5f 44 45 50 54 48 20 31 30 0a 23 65 6c 73 65  R_DEPTH 10.#else
2530: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
2540: 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50  _MAX_TRIGGER_DEP
2550: 54 48 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 23  TH 1000.#endif.#
2560: 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
2570: 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71  ****** End of sq
2580: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a  liteLimit.h ****
2590: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25b0: 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
25c0: 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
25d0: 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
25e0: 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  off in sqliteInt
25f0: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
2600: 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 73 61 62  *****/../* Disab
2610: 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 61 72 6e  le nuisance warn
2620: 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e 64 20  ings on Borland 
2630: 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 23 69 66  compilers */.#if
2640: 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41   defined(__BORLA
2650: 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d 61 20 77  NDC__).#pragma w
2660: 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e 72 65  arn -rch /* unre
2670: 61 63 68 61 62 6c 65 20 63 6f 64 65 20 2a 2f 0a  achable code */.
2680: 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 63  #pragma warn -cc
2690: 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20 69  c /* Condition i
26a0: 73 20 61 6c 77 61 79 73 20 74 72 75 65 20 6f 72  s always true or
26b0: 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72 61 67 6d   false */.#pragm
26c0: 61 20 77 61 72 6e 20 2d 61 75 73 20 2f 2a 20 41  a warn -aus /* A
26d0: 73 73 69 67 6e 65 64 20 76 61 6c 75 65 20 69 73  ssigned value is
26e0: 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23   never used */.#
26f0: 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 73 75  pragma warn -csu
2700: 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 20 73 69   /* Comparing si
2710: 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 6e 65  gned and unsigne
2720: 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72  d */.#pragma war
2730: 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73 70 69 63  n -spa /* Suspic
2740: 69 6f 75 73 20 70 6f 69 6e 74 65 72 20 61 72 69  ious pointer ari
2750: 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65 6e 64 69  thmetic */.#endi
2760: 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20 66 6f 72  f../* Needed for
2770: 20 76 61 72 69 6f 75 73 20 64 65 66 69 6e 69 74   various definit
2780: 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 66 6e 64  ions... */.#ifnd
2790: 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23  ef _GNU_SOURCE.#
27a0: 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f 53 4f 55   define _GNU_SOU
27b0: 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  RCE.#endif../*.*
27c0: 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 6e 64 61  * Include standa
27d0: 72 64 20 68 65 61 64 65 72 20 66 69 6c 65 73 20  rd header files 
27e0: 61 73 20 6e 65 63 65 73 73 61 72 79 0a 2a 2f 0a  as necessary.*/.
27f0: 23 69 66 64 65 66 20 48 41 56 45 5f 53 54 44 49  #ifdef HAVE_STDI
2800: 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 20 3c 73  NT_H.#include <s
2810: 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 69 66 0a  tdint.h>.#endif.
2820: 23 69 66 64 65 66 20 48 41 56 45 5f 49 4e 54 54  #ifdef HAVE_INTT
2830: 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 64 65 20  YPES_H.#include 
2840: 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a 23 65 6e  <inttypes.h>.#en
2850: 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c  dif..#define SQL
2860: 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45  ITE_INDEX_SAMPLE
2870: 53 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  S 10../*.** This
2880: 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74   macro is used t
2890: 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20 75 67  o "hide" some ug
28a0: 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74 69 6e  liness in castin
28b0: 67 20 61 6e 20 69 6e 74 0a 2a 2a 20 76 61 6c 75  g an int.** valu
28c0: 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c 75 65  e to a ptr value
28d0: 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56 43 20   under the MSVC 
28e0: 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 72 2e  64-bit compiler.
28f0: 20 20 20 43 61 73 74 69 6e 67 0a 2a 2a 20 6e 6f     Casting.** no
2900: 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65 73 20  n 64-bit values 
2910: 74 6f 20 70 74 72 20 74 79 70 65 73 20 72 65 73  to ptr types res
2920: 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72 64 22  ults in a "hard"
2930: 20 65 72 72 6f 72 20 77 69 74 68 20 0a 2a 2a 20   error with .** 
2940: 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20  the MSVC 64-bit 
2950: 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68 20 74  compiler which t
2960: 68 69 73 20 61 74 74 65 6d 70 74 73 20 74 6f 20  his attempts to 
2970: 61 76 6f 69 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 41  avoid.  .**.** A
2980: 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c 65 72   simple compiler
2990: 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73 74 69   pragma or casti
29a0: 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 75 6c  ng sequence coul
29b0: 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a 2a  d not be found.*
29c0: 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 69  * to correct thi
29d0: 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61 74 69  s in all situati
29e0: 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d 61 63  ons, so this mac
29f0: 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75 63 65  ro was introduce
2a00: 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 63 6f 75 6c  d..**.** It coul
2a10: 64 20 62 65 20 61 72 67 75 65 64 20 74 68 61 74  d be argued that
2a20: 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20 74 79   the intptr_t ty
2a30: 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73 65 64  pe could be used
2a40: 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65   in this.** case
2a50: 2c 20 62 75 74 20 74 68 61 74 20 74 79 70 65 20  , but that type 
2a60: 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
2a70: 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72   on all compiler
2a80: 73 2c 20 6f 72 20 0a 2a 2a 20 72 65 71 75 69 72  s, or .** requir
2a90: 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64 65 20  es the #include 
2aa0: 6f 66 20 73 70 65 63 69 66 69 63 20 68 65 61 64  of specific head
2ab0: 65 72 73 20 77 68 69 63 68 20 64 69 66 66 65 72  ers which differ
2ac0: 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 70 6c 61  s between.** pla
2ad0: 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 69  tforms..**.** Ti
2ae0: 63 6b 65 74 20 23 33 38 36 30 3a 20 20 54 68 65  cket #3860:  The
2af0: 20 6c 6c 76 6d 2d 67 63 63 2d 34 2e 32 20 63 6f   llvm-gcc-4.2 co
2b00: 6d 70 69 6c 65 72 20 66 72 6f 6d 20 41 70 70 6c  mpiler from Appl
2b10: 65 20 63 68 6f 6b 65 73 20 6f 6e 0a 2a 2a 20 74  e chokes on.** t
2b20: 68 65 20 28 28 76 6f 69 64 2a 29 26 28 28 63 68  he ((void*)&((ch
2b30: 61 72 2a 29 30 29 5b 58 5d 29 20 63 6f 6e 73 74  ar*)0)[X]) const
2b40: 72 75 63 74 2e 20 20 42 75 74 20 4d 53 56 43 20  ruct.  But MSVC 
2b50: 63 68 6f 6b 65 73 20 6f 6e 20 28 28 76 6f 69 64  chokes on ((void
2b60: 2a 29 28 58 29 29 2e 0a 2a 2a 20 53 6f 20 77 65  *)(X))..** So we
2b70: 20 68 61 76 65 20 74 6f 20 64 65 66 69 6e 65 20   have to define 
2b80: 74 68 65 20 6d 61 63 72 6f 73 20 69 6e 20 64 69  the macros in di
2b90: 66 66 65 72 65 6e 74 20 77 61 79 73 20 64 65 70  fferent ways dep
2ba0: 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0a 2a 2a  ending on the.**
2bb0: 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2f 0a 23 69   compiler..*/.#i
2bc0: 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43  f defined(__GNUC
2bd0: 5f 5f 29 0a 23 20 69 66 20 64 65 66 69 6e 65 64  __).# if defined
2be0: 28 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 29 0a  (HAVE_STDINT_H).
2bf0: 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  #   define SQLIT
2c00: 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20  E_INT_TO_PTR(X) 
2c10: 20 28 28 76 6f 69 64 2a 29 28 69 6e 74 70 74 72   ((void*)(intptr
2c20: 5f 74 29 28 58 29 29 0a 23 20 20 20 64 65 66 69  _t)(X)).#   defi
2c30: 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f  ne SQLITE_PTR_TO
2c40: 5f 49 4e 54 28 58 29 20 20 28 28 69 6e 74 29 28  _INT(X)  ((int)(
2c50: 69 6e 74 70 74 72 5f 74 29 28 58 29 29 0a 23 20  intptr_t)(X)).# 
2c60: 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 20  else.#   define 
2c70: 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54  SQLITE_INT_TO_PT
2c80: 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29 28 58  R(X)  ((void*)(X
2c90: 29 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51  )).#   define SQ
2ca0: 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28  LITE_PTR_TO_INT(
2cb0: 58 29 20 20 28 28 69 6e 74 29 28 58 29 29 0a 23  X)  ((int)(X)).#
2cc0: 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 23 20 64   endif.#else.# d
2cd0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54  efine SQLITE_INT
2ce0: 5f 54 4f 5f 50 54 52 28 58 29 20 20 20 28 28 76  _TO_PTR(X)   ((v
2cf0: 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29  oid*)&((char*)0)
2d00: 5b 58 5d 29 0a 23 20 64 65 66 69 6e 65 20 53 51  [X]).# define SQ
2d10: 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28  LITE_PTR_TO_INT(
2d20: 58 29 20 20 20 28 28 69 6e 74 29 28 28 28 63 68  X)   ((int)(((ch
2d30: 61 72 2a 29 58 29 2d 28 63 68 61 72 2a 29 30 29  ar*)X)-(char*)0)
2d40: 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  ).#endif.../*.**
2d50: 20 54 68 65 20 53 51 4c 49 54 45 5f 54 48 52 45   The SQLITE_THRE
2d60: 41 44 53 41 46 45 20 6d 61 63 72 6f 20 6d 75 73  ADSAFE macro mus
2d70: 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 73 20  t be defined as 
2d80: 65 69 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a  either 0 or 1..*
2d90: 2a 20 4f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73  * Older versions
2da0: 20 6f 66 20 53 51 4c 69 74 65 20 75 73 65 64 20   of SQLite used 
2db0: 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54 48 52 45  an optional THRE
2dc0: 41 44 53 41 46 45 20 6d 61 63 72 6f 2e 0a 2a 2a  ADSAFE macro..**
2dd0: 20 57 65 20 73 75 70 70 6f 72 74 20 74 68 61 74   We support that
2de0: 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a 2f 0a 23   for legacy.*/.#
2df0: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
2e00: 54 45 5f 54 48 52 45 41 44 53 41 46 45 29 0a 23  TE_THREADSAFE).#
2e10: 69 66 20 64 65 66 69 6e 65 64 28 54 48 52 45 41  if defined(THREA
2e20: 44 53 41 46 45 29 0a 23 20 64 65 66 69 6e 65 20  DSAFE).# define 
2e30: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
2e40: 45 20 54 48 52 45 41 44 53 41 46 45 0a 23 65 6c  E THREADSAFE.#el
2e50: 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  se.# define SQLI
2e60: 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 31 0a  TE_THREADSAFE 1.
2e70: 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f  #endif.#endif../
2e80: 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
2e90: 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55  DEFAULT_MEMSTATU
2ea0: 53 20 6d 61 63 72 6f 20 6d 75 73 74 20 62 65 20  S macro must be 
2eb0: 64 65 66 69 6e 65 64 20 61 73 20 65 69 74 68 65  defined as eithe
2ec0: 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 49 74 20  r 0 or 1..** It 
2ed0: 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68  determines wheth
2ee0: 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 66 65  er or not the fe
2ef0: 61 74 75 72 65 73 20 72 65 6c 61 74 65 64 20 74  atures related t
2f00: 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e  o .** SQLITE_CON
2f10: 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 61 72  FIG_MEMSTATUS ar
2f20: 65 20 61 76 61 69 6c 61 62 6c 65 20 62 79 20 64  e available by d
2f30: 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74 2e 20 54  efault or not. T
2f40: 68 69 73 20 76 61 6c 75 65 20 63 61 6e 0a 2a 2a  his value can.**
2f50: 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61   be overridden a
2f60: 74 20 72 75 6e 74 69 6d 65 20 75 73 69 6e 67 20  t runtime using 
2f70: 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  the sqlite3_conf
2f80: 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 69 66  ig() API..*/.#if
2f90: 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2fa0: 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54  _DEFAULT_MEMSTAT
2fb0: 55 53 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  US).# define SQL
2fc0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53  ITE_DEFAULT_MEMS
2fd0: 54 41 54 55 53 20 31 0a 23 65 6e 64 69 66 0a 0a  TATUS 1.#endif..
2fe0: 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79 20 6f 6e  /*.** Exactly on
2ff0: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
3000: 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 74 20 62  ng macros must b
3010: 65 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 72 64  e defined in ord
3020: 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63 69 66 79  er to.** specify
3030: 20 77 68 69 63 68 20 6d 65 6d 6f 72 79 20 61 6c   which memory al
3040: 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
3050: 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a 0a 2a 2a  em to use..**.**
3060: 20 20 20 20 20 53 51 4c 49 54 45 5f 53 59 53 54       SQLITE_SYST
3070: 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 20 20  EM_MALLOC       
3080: 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 6d 61 6c     // Use normal
3090: 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29   system malloc()
30a0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d  .**     SQLITE_M
30b0: 45 4d 44 45 42 55 47 20 20 20 20 20 20 20 20 20  EMDEBUG         
30c0: 20 20 20 20 20 20 2f 2f 20 44 65 62 75 67 67 69        // Debuggi
30d0: 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 79  ng version of sy
30e0: 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a 2a 2a  stem malloc().**
30f0: 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 45 4d 4f       SQLITE_MEMO
3100: 52 59 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20  RY_SIZE         
3110: 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 61     // internal a
3120: 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a 2a 20 20  llocator #1.**  
3130: 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48     SQLITE_MMAP_H
3140: 45 41 50 5f 53 49 5a 45 20 20 20 20 20 20 20 20  EAP_SIZE        
3150: 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 6d 6d 61   // internal mma
3160: 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a  p() allocator.**
3170: 20 20 20 20 20 53 51 4c 49 54 45 5f 50 4f 57 32       SQLITE_POW2
3180: 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20  _MEMORY_SIZE    
3190: 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 70     // internal p
31a0: 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c 6f  ower-of-two allo
31b0: 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  cator.**.** If n
31c0: 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76 65  one of the above
31d0: 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68   are defined, th
31e0: 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f 53 59  en set SQLITE_SY
31f0: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73 0a 2a  STEM_MALLOC as.*
3200: 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 2a  * the default..*
3210: 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
3220: 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c  LITE_SYSTEM_MALL
3230: 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49  OC)+defined(SQLI
3240: 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c 0a 20  TE_MEMDEBUG)+\. 
3250: 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54     defined(SQLIT
3260: 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 2b 64  E_MEMORY_SIZE)+d
3270: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 4d  efined(SQLITE_MM
3280: 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b 5c 0a  AP_HEAP_SIZE)+\.
3290: 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49      defined(SQLI
32a0: 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53  TE_POW2_MEMORY_S
32b0: 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f 72 20 22  IZE)>1.# error "
32c0: 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 74  At most one of t
32d0: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d  he following com
32e0: 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e 66 69 67  pile-time config
32f0: 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 5c  uration options\
3300: 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20 53 51 4c  . is allows: SQL
3310: 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f  ITE_SYSTEM_MALLO
3320: 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42  C, SQLITE_MEMDEB
3330: 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52  UG, SQLITE_MEMOR
3340: 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c 49 54 45  Y_SIZE,\. SQLITE
3350: 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 2c  _MMAP_HEAP_SIZE,
3360: 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d   SQLITE_POW2_MEM
3370: 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e 64 69 66  ORY_SIZE".#endif
3380: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
3390: 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f  ITE_SYSTEM_MALLO
33a0: 43 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54  C)+defined(SQLIT
33b0: 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c 0a 20 20  E_MEMDEBUG)+\.  
33c0: 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45    defined(SQLITE
33d0: 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 2b 64 65  _MEMORY_SIZE)+de
33e0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 4d 41  fined(SQLITE_MMA
33f0: 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b 5c 0a 20  P_HEAP_SIZE)+\. 
3400: 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54     defined(SQLIT
3410: 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49  E_POW2_MEMORY_SI
3420: 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69 6e 65 20  ZE)==0.# define 
3430: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
3440: 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  LLOC 1.#endif../
3450: 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4d  *.** If SQLITE_M
3460: 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54  ALLOC_SOFT_LIMIT
3470: 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68   is not zero, th
3480: 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 70 20 74  en try to keep t
3490: 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f 66 20 6d  he.** sizes of m
34a0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
34b0: 73 20 62 65 6c 6f 77 20 74 68 69 73 20 76 61 6c  s below this val
34c0: 75 65 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c  ue where possibl
34d0: 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  e..*/.#if !defin
34e0: 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43  ed(SQLITE_MALLOC
34f0: 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a 23 20 64  _SOFT_LIMIT).# d
3500: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 4c  efine SQLITE_MAL
3510: 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 31  LOC_SOFT_LIMIT 1
3520: 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  024.#endif../*.*
3530: 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 64 65 66  * We need to def
3540: 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43  ine _XOPEN_SOURC
3550: 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20 69 6e 20  E as follows in 
3560: 6f 72 64 65 72 20 74 6f 20 65 6e 61 62 6c 65 0a  order to enable.
3570: 2a 2a 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ** recursive mut
3580: 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20 55 6e 69  exes on most Uni
3590: 78 20 73 79 73 74 65 6d 73 2e 20 20 42 75 74 20  x systems.  But 
35a0: 4d 61 63 20 4f 53 20 58 20 69 73 20 64 69 66 66  Mac OS X is diff
35b0: 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 5f 58  erent..** The _X
35c0: 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64 65 66 69  OPEN_SOURCE defi
35d0: 6e 65 20 63 61 75 73 65 73 20 70 72 6f 62 6c 65  ne causes proble
35e0: 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53 20 58 20  ms for Mac OS X 
35f0: 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a 2a 2a 20  we are told,.** 
3600: 73 6f 20 69 74 20 69 73 20 6f 6d 69 74 74 65 64  so it is omitted
3610: 20 74 68 65 72 65 2e 20 20 53 65 65 20 74 69 63   there.  See tic
3620: 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a 0a 2a 2a  ket #2673..**.**
3630: 20 4c 61 74 65 72 20 77 65 20 6c 65 61 72 6e 20   Later we learn 
3640: 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53 4f 55 52  that _XOPEN_SOUR
3650: 43 45 20 69 73 20 70 6f 6f 72 6c 79 20 6f 72 20  CE is poorly or 
3660: 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a 2a 20 69  incorrectly.** i
3670: 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e 20 73 6f  mplemented on so
3680: 6d 65 20 73 79 73 74 65 6d 73 2e 20 20 53 6f 20  me systems.  So 
3690: 77 65 20 61 76 6f 69 64 20 64 65 66 69 6e 69 6e  we avoid definin
36a0: 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a 2a 20 69  g it at all.** i
36b0: 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20  f it is already 
36c0: 64 65 66 69 6e 65 64 20 6f 72 20 69 66 20 69 74  defined or if it
36d0: 20 69 73 20 75 6e 6e 65 65 64 65 64 20 62 65 63   is unneeded bec
36e0: 61 75 73 65 20 77 65 20 61 72 65 0a 2a 2a 20 6e  ause we are.** n
36f0: 6f 74 20 64 6f 69 6e 67 20 61 20 74 68 72 65 61  ot doing a threa
3700: 64 73 61 66 65 20 62 75 69 6c 64 2e 20 20 54 69  dsafe build.  Ti
3710: 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a  cket #2681..**.*
3720: 2a 20 53 65 65 20 61 6c 73 6f 20 74 69 63 6b 65  * See also ticke
3730: 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23 69 66 20  t #2741..*/.#if 
3740: 21 64 65 66 69 6e 65 64 28 5f 58 4f 50 45 4e 5f  !defined(_XOPEN_
3750: 53 4f 55 52 43 45 29 20 26 26 20 21 64 65 66 69  SOURCE) && !defi
3760: 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f 5f 29 20  ned(__DARWIN__) 
3770: 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 41 50  && !defined(__AP
3780: 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
3790: 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 20 64  _THREADSAFE.#  d
37a0: 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55  efine _XOPEN_SOU
37b0: 52 43 45 20 35 30 30 20 20 2f 2a 20 4e 65 65 64  RCE 500  /* Need
37c0: 65 64 20 74 6f 20 65 6e 61 62 6c 65 20 70 74 68  ed to enable pth
37d0: 72 65 61 64 20 72 65 63 75 72 73 69 76 65 20 6d  read recursive m
37e0: 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e 64 69 66  utexes */.#endif
37f0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 43 4c 20  ../*.** The TCL 
3800: 68 65 61 64 65 72 73 20 61 72 65 20 6f 6e 6c 79  headers are only
3810: 20 6e 65 65 64 65 64 20 77 68 65 6e 20 63 6f 6d   needed when com
3820: 70 69 6c 69 6e 67 20 74 68 65 20 54 43 4c 20 62  piling the TCL b
3830: 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23 69 66 20  indings..*/.#if 
3840: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
3850: 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 54  CL) || defined(T
3860: 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75 64 65 20  CLSH).# include 
3870: 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a  <tcl.h>.#endif..
3880: 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65 6f 70 6c  /*.** Many peopl
3890: 65 20 61 72 65 20 66 61 69 6c 69 6e 67 20 74 6f  e are failing to
38a0: 20 73 65 74 20 2d 44 4e 44 45 42 55 47 3d 31 20   set -DNDEBUG=1 
38b0: 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 53  when compiling S
38c0: 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74 74 69 6e  QLite..** Settin
38d0: 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 73 20 74  g NDEBUG makes t
38e0: 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c 65 72 20  he code smaller 
38f0: 61 6e 64 20 72 75 6e 20 66 61 73 74 65 72 2e 20  and run faster. 
3900: 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   So the followin
3910: 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72 65 20 61  g.** lines are a
3920: 64 64 65 64 20 74 6f 20 61 75 74 6f 6d 61 74 69  dded to automati
3930: 63 61 6c 6c 79 20 73 65 74 20 4e 44 45 42 55 47  cally set NDEBUG
3940: 20 75 6e 6c 65 73 73 20 74 68 65 20 2d 44 53 51   unless the -DSQ
3950: 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a 2a 2a 20  LITE_DEBUG=1.** 
3960: 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2e 20 20  option is set.  
3970: 54 68 75 73 20 4e 44 45 42 55 47 20 62 65 63 6f  Thus NDEBUG beco
3980: 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e 20 72 61  mes an opt-in ra
3990: 74 68 65 72 20 74 68 61 6e 20 61 6e 20 6f 70 74  ther than an opt
39a0: 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75 72 65 2e  -out.** feature.
39b0: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
39c0: 28 4e 44 45 42 55 47 29 20 26 26 20 21 64 65 66  (NDEBUG) && !def
39d0: 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
39e0: 47 29 20 0a 23 20 64 65 66 69 6e 65 20 4e 44 45  G) .# define NDE
39f0: 42 55 47 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  BUG 1.#endif../*
3a00: 0a 2a 2a 20 54 68 65 20 74 65 73 74 63 61 73 65  .** The testcase
3a10: 28 29 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64  () macro is used
3a20: 20 74 6f 20 61 69 64 20 69 6e 20 63 6f 76 65 72   to aid in cover
3a30: 61 67 65 20 74 65 73 74 69 6e 67 2e 20 20 57 68  age testing.  Wh
3a40: 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20 63 6f 76  en .** doing cov
3a50: 65 72 61 67 65 20 74 65 73 74 69 6e 67 2c 20 74  erage testing, t
3a60: 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 69 6e 73  he condition ins
3a70: 69 64 65 20 74 68 65 20 61 72 67 75 6d 65 6e 74  ide the argument
3a80: 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61 73 65 28   to.** testcase(
3a90: 29 20 6d 75 73 74 20 62 65 20 65 76 61 6c 75 61  ) must be evalua
3aa0: 74 65 64 20 62 6f 74 68 20 74 72 75 65 20 61 6e  ted both true an
3ab0: 64 20 66 61 6c 73 65 20 69 6e 20 6f 72 64 65 72  d false in order
3ac0: 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75 6c 6c 20   to.** get full 
3ad0: 62 72 61 6e 63 68 20 63 6f 76 65 72 61 67 65 2e  branch coverage.
3ae0: 20 20 54 68 65 20 74 65 73 74 63 61 73 65 28 29    The testcase()
3af0: 20 6d 61 63 72 6f 20 69 73 20 69 6e 73 65 72 74   macro is insert
3b00: 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70 20 65 6e  ed.** to help en
3b10: 73 75 72 65 20 61 64 65 71 75 61 74 65 20 74 65  sure adequate te
3b20: 73 74 20 63 6f 76 65 72 61 67 65 20 69 6e 20 70  st coverage in p
3b30: 6c 61 63 65 73 20 77 68 65 72 65 20 73 69 6d 70  laces where simp
3b40: 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 2f  le.** condition/
3b50: 64 65 63 69 73 69 6f 6e 20 63 6f 76 65 72 61 67  decision coverag
3b60: 65 20 69 73 20 69 6e 61 64 65 71 75 61 74 65 2e  e is inadequate.
3b70: 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74    For example, t
3b80: 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e  estcase().** can
3b90: 20 62 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65   be used to make
3ba0: 20 73 75 72 65 20 62 6f 75 6e 64 61 72 79 20 76   sure boundary v
3bb0: 61 6c 75 65 73 20 61 72 65 20 74 65 73 74 65 64  alues are tested
3bc0: 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74 6d 61 73  .  For.** bitmas
3bd0: 6b 20 74 65 73 74 73 2c 20 74 65 73 74 63 61 73  k tests, testcas
3be0: 65 28 29 20 63 61 6e 20 62 65 20 75 73 65 64 20  e() can be used 
3bf0: 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 65 61 63  to make sure eac
3c00: 68 20 62 69 74 0a 2a 2a 20 69 73 20 73 69 67 6e  h bit.** is sign
3c10: 69 66 69 63 61 6e 74 20 61 6e 64 20 75 73 65 64  ificant and used
3c20: 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20   at least once. 
3c30: 20 4f 6e 20 73 77 69 74 63 68 20 73 74 61 74 65   On switch state
3c40: 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72 65 20 6d  ments.** where m
3c50: 75 6c 74 69 70 6c 65 20 63 61 73 65 73 20 67 6f  ultiple cases go
3c60: 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62 6c 6f   to the same blo
3c70: 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74 65 73 74  ck of code, test
3c80: 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e 20 69 6e  case().** can in
3c90: 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 63 61  sure that all ca
3ca0: 73 65 73 20 61 72 65 20 65 76 61 6c 75 61 74 65  ses are evaluate
3cb0: 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65 66 20  d..**.*/.#ifdef 
3cc0: 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f  SQLITE_COVERAGE_
3cd0: 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
3ce0: 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
3cf0: 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 29 3b  e3Coverage(int);
3d00: 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74 63 61  .# define testca
3d10: 73 65 28 58 29 20 20 69 66 28 20 58 20 29 7b 20  se(X)  if( X ){ 
3d20: 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28  sqlite3Coverage(
3d30: 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a 23 65 6c  __LINE__); }.#el
3d40: 73 65 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74  se.# define test
3d50: 63 61 73 65 28 58 29 0a 23 65 6e 64 69 66 0a 0a  case(X).#endif..
3d60: 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53 54 4f 4e  /*.** The TESTON
3d70: 4c 59 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64  LY macro is used
3d80: 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76 61 72 69   to enclose vari
3d90: 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f 6e  able declaration
3da0: 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 62 69  s or.** other bi
3db0: 74 73 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20  ts of code that 
3dc0: 61 72 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75  are needed to su
3dd0: 70 70 6f 72 74 20 74 68 65 20 61 72 67 75 6d 65  pport the argume
3de0: 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 65  nts.** within te
3df0: 73 74 63 61 73 65 28 29 20 61 6e 64 20 61 73 73  stcase() and ass
3e00: 65 72 74 28 29 20 6d 61 63 72 6f 73 2e 0a 2a 2f  ert() macros..*/
3e10: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
3e20: 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64  EBUG) || defined
3e30: 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45  (SQLITE_COVERAGE
3e40: 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20  _TEST).# define 
3e50: 54 45 53 54 4f 4e 4c 59 28 58 29 20 20 58 0a 23  TESTONLY(X)  X.#
3e60: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 54 45  else.# define TE
3e70: 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66  STONLY(X).#endif
3e80: 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65  ../*.** Sometime
3e90: 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d 61 6c  s we need a smal
3ea0: 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f 64 65  l amount of code
3eb0: 20 73 75 63 68 20 61 73 20 61 20 76 61 72 69 61   such as a varia
3ec0: 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ble initializati
3ed0: 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 70 20 66  on.** to setup f
3ee0: 6f 72 20 61 20 6c 61 74 65 72 20 61 73 73 65 72  or a later asser
3ef0: 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  t() statement.  
3f00: 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74  We do not want t
3f10: 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 61  his code to.** a
3f20: 70 70 65 61 72 20 77 68 65 6e 20 61 73 73 65 72  ppear when asser
3f30: 74 28 29 20 69 73 20 64 69 73 61 62 6c 65 64 2e  t() is disabled.
3f40: 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
3f50: 6d 61 63 72 6f 20 69 73 20 74 68 65 72 65 66 6f  macro is therefo
3f60: 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 6f  re.** used to co
3f70: 6e 74 61 69 6e 20 74 68 61 74 20 73 65 74 75 70  ntain that setup
3f80: 20 63 6f 64 65 2e 20 20 54 68 65 20 22 56 56 41   code.  The "VVA
3f90: 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 6e 64 73  " acronym stands
3fa0: 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 66 69 63   for.** "Verific
3fb0: 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 74 69 6f  ation, Validatio
3fc0: 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 69 74 61  n, and Accredita
3fd0: 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 68 65 72  tion".  In other
3fe0: 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a 20 63   words, the.** c
3ff0: 6f 64 65 20 77 69 74 68 69 6e 20 56 56 41 5f 4f  ode within VVA_O
4000: 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e 6c 79 20  NLY() will only 
4010: 72 75 6e 20 64 75 72 69 6e 67 20 76 65 72 69 66  run during verif
4020: 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 73 73 65  ication processe
4030: 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44  s..*/.#ifndef ND
4040: 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20 56 56  EBUG.# define VV
4050: 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a 23 65 6c  A_ONLY(X)  X.#el
4060: 73 65 0a 23 20 64 65 66 69 6e 65 20 56 56 41 5f  se.# define VVA_
4070: 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66 0a 0a  ONLY(X).#endif..
4080: 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 41 59 53  /*.** The ALWAYS
4090: 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 63 72 6f   and NEVER macro
40a0: 73 20 73 75 72 72 6f 75 6e 64 20 62 6f 6f 6c 65  s surround boole
40b0: 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77  an expressions w
40c0: 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 69 6e 74  hich .** are int
40d0: 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 79 73 20  ended to always 
40e0: 62 65 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65  be true or false
40f0: 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  , respectively. 
4100: 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 65 73 73   Such.** express
4110: 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 20 6f 6d  ions could be om
4120: 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 63  itted from the c
4130: 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c 79 2e 20  ode completely. 
4140: 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 61 72 65   But they.** are
4150: 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 61 20 66   included in a f
4160: 65 77 20 63 61 73 65 73 20 69 6e 20 6f 72 64 65  ew cases in orde
4170: 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 74 68 65  r to enhance the
4180: 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a 2a 20 6f   resilience.** o
4190: 66 20 53 51 4c 69 74 65 20 74 6f 20 75 6e 65 78  f SQLite to unex
41a0: 70 65 63 74 65 64 20 62 65 68 61 76 69 6f 72 20  pected behavior 
41b0: 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 63 6f  - to make the co
41c0: 64 65 20 22 73 65 6c 66 2d 68 65 61 6c 69 6e 67  de "self-healing
41d0: 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 69 6c 65  ".** or "ductile
41e0: 22 20 72 61 74 68 65 72 20 74 68 61 6e 20 62 65  " rather than be
41f0: 69 6e 67 20 22 62 72 69 74 74 6c 65 22 20 61 6e  ing "brittle" an
4200: 64 20 63 72 61 73 68 69 6e 67 20 61 74 20 74 68  d crashing at th
4210: 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 6e 74 20  e first.** hint 
4220: 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 62 65 68  of unplanned beh
4230: 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  avior..**.** In 
4240: 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 41 4c 57  other words, ALW
4250: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 61 72  AYS and NEVER ar
4260: 65 20 61 64 64 65 64 20 66 6f 72 20 64 65 66 65  e added for defe
4270: 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  nsive code..**.*
4280: 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 63 6f 76  * When doing cov
4290: 65 72 61 67 65 20 74 65 73 74 69 6e 67 20 41 4c  erage testing AL
42a0: 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 61  WAYS and NEVER a
42b0: 72 65 20 68 61 72 64 2d 63 6f 64 65 64 20 74 6f  re hard-coded to
42c0: 0a 2a 2a 20 62 65 20 74 72 75 65 20 61 6e 64 20  .** be true and 
42d0: 66 61 6c 73 65 20 73 6f 20 74 68 61 74 20 74 68  false so that th
42e0: 65 20 75 6e 72 65 61 63 68 61 62 6c 65 20 63 6f  e unreachable co
42f0: 64 65 20 74 68 65 6e 20 73 70 65 63 69 66 79 20  de then specify 
4300: 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 65 20 63  will.** not be c
4310: 6f 75 6e 74 65 64 20 61 73 20 75 6e 74 65 73 74  ounted as untest
4320: 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 20  ed code..*/.#if 
4330: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
4340: 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a 23 20  OVERAGE_TEST).# 
4350: 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29  define ALWAYS(X)
4360: 20 20 20 20 20 20 28 31 29 0a 23 20 64 65 66 69        (1).# defi
4370: 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20  ne NEVER(X)     
4380: 20 20 28 30 29 0a 23 65 6c 69 66 20 21 64 65 66    (0).#elif !def
4390: 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 23 20 64  ined(NDEBUG).# d
43a0: 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29 20  efine ALWAYS(X) 
43b0: 20 20 20 20 20 28 28 58 29 3f 31 3a 28 61 73 73       ((X)?1:(ass
43c0: 65 72 74 28 30 29 2c 30 29 29 0a 23 20 64 65 66  ert(0),0)).# def
43d0: 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20  ine NEVER(X)    
43e0: 20 20 20 28 28 58 29 3f 28 61 73 73 65 72 74 28     ((X)?(assert(
43f0: 30 29 2c 31 29 3a 30 29 0a 23 65 6c 73 65 0a 23  0),1):0).#else.#
4400: 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58   define ALWAYS(X
4410: 29 20 20 20 20 20 20 28 58 29 0a 23 20 64 65 66  )      (X).# def
4420: 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20  ine NEVER(X)    
4430: 20 20 20 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f     (X).#endif../
4440: 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 75  *.** The macro u
4450: 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20 61 20 68  nlikely() is a h
4460: 69 6e 74 20 74 68 61 74 20 73 75 72 72 6f 75 6e  int that surroun
4470: 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20  ds a boolean.** 
4480: 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
4490: 69 73 20 75 73 75 61 6c 6c 79 20 66 61 6c 73 65  is usually false
44a0: 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c 79 28  .  Macro likely(
44b0: 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a 2a 20 61  ) surrounds.** a
44c0: 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73   boolean express
44d0: 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 75 61  ion that is usua
44e0: 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 43 20 69  lly true.  GCC i
44f0: 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 75 73 65  s able to.** use
4500: 20 74 68 65 73 65 20 68 69 6e 74 73 20 74 6f 20   these hints to 
4510: 67 65 6e 65 72 61 74 65 20 62 65 74 74 65 72 20  generate better 
4520: 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65 73 2e  code, sometimes.
4530: 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
4540: 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 30 0a 23  __GNUC__) && 0.#
4550: 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28 58   define likely(X
4560: 29 20 20 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65  )    __builtin_e
4570: 78 70 65 63 74 28 28 58 29 2c 31 29 0a 23 20 64  xpect((X),1).# d
4580: 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58  efine unlikely(X
4590: 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78 70  )  __builtin_exp
45a0: 65 63 74 28 28 58 29 2c 30 29 0a 23 65 6c 73 65  ect((X),0).#else
45b0: 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79  .# define likely
45c0: 28 58 29 20 20 20 20 21 21 28 58 29 0a 23 20 64  (X)    !!(X).# d
45d0: 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58  efine unlikely(X
45e0: 29 20 20 21 21 28 58 29 0a 23 65 6e 64 69 66 0a  )  !!(X).#endif.
45f0: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4600: 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 33   Include sqlite3
4610: 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
4620: 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
4630: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4640: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4650: 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69   Begin file sqli
4660: 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  te3.h **********
4670: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4680: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4690: 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
46a0: 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
46b0: 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
46c0: 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
46d0: 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
46e0: 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
46f0: 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
4700: 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
4710: 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
4720: 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
4730: 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
4740: 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
4750: 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
4760: 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
4770: 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
4780: 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
4790: 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
47a0: 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
47b0: 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
47c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4800: 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
4810: 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 69  header file defi
4820: 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63  nes the interfac
4830: 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 69 74  e that the SQLit
4840: 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 70 72 65  e library.** pre
4850: 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 6e 74 20  sents to client 
4860: 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 20 61 20  programs.  If a 
4870: 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 74 72 75  C-function, stru
4880: 63 74 75 72 65 2c 20 64 61 74 61 74 79 70 65 2c  cture, datatype,
4890: 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20  .** or constant 
48a0: 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f 65 73 20  definition does 
48b0: 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 74 68  not appear in th
48c0: 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74  is file, then it
48d0: 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 75 62   is.** not a pub
48e0: 6c 69 73 68 65 64 20 41 50 49 20 6f 66 20 53 51  lished API of SQ
48f0: 4c 69 74 65 2c 20 69 73 20 73 75 62 6a 65 63 74  Lite, is subject
4900: 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f   to change witho
4910: 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c 20 61 6e  ut.** notice, an
4920: 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  d should not be 
4930: 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 70 72  referenced by pr
4940: 6f 67 72 61 6d 73 20 74 68 61 74 20 75 73 65 20  ograms that use 
4950: 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f  SQLite..**.** So
4960: 6d 65 20 6f 66 20 74 68 65 20 64 65 66 69 6e 69  me of the defini
4970: 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 69  tions that are i
4980: 6e 20 74 68 69 73 20 66 69 6c 65 20 61 72 65 20  n this file are 
4990: 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 22 65 78  marked as.** "ex
49a0: 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20 45 78  perimental".  Ex
49b0: 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72  perimental inter
49c0: 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d 61 6c  faces are normal
49d0: 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 74 75 72  ly new.** featur
49e0: 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65  es recently adde
49f0: 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20 57 65  d to SQLite.  We
4a00: 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69 70 61   do not anticipa
4a10: 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 6f  te changes.** to
4a20: 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e   experimental in
4a30: 74 65 72 66 61 63 65 73 20 62 75 74 20 72 65 73  terfaces but res
4a40: 65 72 76 65 20 74 68 65 20 72 69 67 68 74 20 74  erve the right t
4a50: 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 63 68 61  o make minor cha
4a60: 6e 67 65 73 0a 2a 2a 20 69 66 20 65 78 70 65 72  nges.** if exper
4a70: 69 65 6e 63 65 20 66 72 6f 6d 20 75 73 65 20 22  ience from use "
4a80: 69 6e 20 74 68 65 20 77 69 6c 64 22 20 73 75 67  in the wild" sug
4a90: 67 65 73 74 20 73 75 63 68 20 63 68 61 6e 67 65  gest such change
4aa0: 73 20 61 72 65 20 70 72 75 64 65 6e 74 2e 0a 2a  s are prudent..*
4ab0: 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69 63 69 61  *.** The officia
4ac0: 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20 41 50 49  l C-language API
4ad0: 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
4ae0: 6f 72 20 53 51 4c 69 74 65 20 69 73 20 64 65 72  or SQLite is der
4af0: 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 63 6f 6d  ived.** from com
4b00: 6d 65 6e 74 73 20 69 6e 20 74 68 69 73 20 66 69  ments in this fi
4b10: 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65 20 69  le.  This file i
4b20: 73 20 74 68 65 20 61 75 74 68 6f 72 69 74 61 74  s the authoritat
4b30: 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a 20 6f 6e  ive source.** on
4b40: 20 68 6f 77 20 53 51 4c 69 74 65 20 69 6e 74 65   how SQLite inte
4b50: 72 66 61 63 65 73 20 61 72 65 20 73 75 70 70 6f  rfaces are suppo
4b60: 73 65 20 74 6f 20 6f 70 65 72 61 74 65 2e 0a 2a  se to operate..*
4b70: 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  *.** The name of
4b80: 20 74 68 69 73 20 66 69 6c 65 20 75 6e 64 65 72   this file under
4b90: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d   configuration m
4ba0: 61 6e 61 67 65 6d 65 6e 74 20 69 73 20 22 73 71  anagement is "sq
4bb0: 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a 20 54  lite.h.in"..** T
4bc0: 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d 61 6b 65  he makefile make
4bd0: 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20 63 68 61  s some minor cha
4be0: 6e 67 65 73 20 74 6f 20 74 68 69 73 20 66 69 6c  nges to this fil
4bf0: 65 20 28 73 75 63 68 20 61 73 20 69 6e 73 65 72  e (such as inser
4c00: 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 65 72 73  ting.** the vers
4c10: 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61 6e 64 20  ion number) and 
4c20: 63 68 61 6e 67 65 73 20 69 74 73 20 6e 61 6d 65  changes its name
4c30: 20 74 6f 20 22 73 71 6c 69 74 65 33 2e 68 22 20   to "sqlite3.h" 
4c40: 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 74 68  as.** part of th
4c50: 65 20 62 75 69 6c 64 20 70 72 6f 63 65 73 73 2e  e build process.
4c60: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c  .*/.#ifndef _SQL
4c70: 49 54 45 33 5f 48 5f 0a 23 64 65 66 69 6e 65 20  ITE3_H_.#define 
4c80: 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 69 6e 63  _SQLITE3_H_.#inc
4c90: 6c 75 64 65 20 3c 73 74 64 61 72 67 2e 68 3e 20  lude <stdarg.h> 
4ca0: 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 20 66 6f      /* Needed fo
4cb0: 72 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e  r the definition
4cc0: 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a 2f 0a 0a   of va_list */..
4cd0: 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
4ce0: 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 68 69 73  we can call this
4cf0: 20 73 74 75 66 66 20 66 72 6f 6d 20 43 2b 2b 2e   stuff from C++.
4d00: 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74 65 72 6e  .*/.#if 0.extern
4d10: 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 0a 0a 0a   "C" {.#endif...
4d20: 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 61 62  /*.** Add the ab
4d30: 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72 69 64  ility to overrid
4d40: 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f 0a 23 69  e 'extern'.*/.#i
4d50: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 58 54  fndef SQLITE_EXT
4d60: 45 52 4e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ERN.# define SQL
4d70: 49 54 45 5f 45 58 54 45 52 4e 20 65 78 74 65 72  ITE_EXTERN exter
4d80: 6e 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  n.#endif..#ifnde
4d90: 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 20 64  f SQLITE_API.# d
4da0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 50 49  efine SQLITE_API
4db0: 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
4dc0: 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d 61 63 72  These no-op macr
4dd0: 6f 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 66  os are used in f
4de0: 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72 66 61 63  ront of interfac
4df0: 65 73 20 74 6f 20 6d 61 72 6b 20 74 68 6f 73 65  es to mark those
4e00: 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 61  .** interfaces a
4e10: 73 20 65 69 74 68 65 72 20 64 65 70 72 65 63 61  s either depreca
4e20: 74 65 64 20 6f 72 20 65 78 70 65 72 69 6d 65 6e  ted or experimen
4e30: 74 61 6c 2e 20 20 4e 65 77 20 61 70 70 6c 69 63  tal.  New applic
4e40: 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64  ations.** should
4e50: 20 6e 6f 74 20 75 73 65 20 64 65 70 72 65 63 61   not use depreca
4e60: 74 65 64 20 69 6e 74 65 72 66 61 63 65 73 20 2d  ted interfaces -
4e70: 20 74 68 65 79 20 61 72 65 20 73 75 70 70 6f 72   they are suppor
4e80: 74 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 0a  t for backwards.
4e90: 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ** compatibility
4ea0: 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74   only.  Applicat
4eb0: 69 6f 6e 20 77 72 69 74 65 72 73 20 73 68 6f 75  ion writers shou
4ec0: 6c 64 20 62 65 20 61 77 61 72 65 20 74 68 61 74  ld be aware that
4ed0: 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e 74 61 6c  .** experimental
4ee0: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20   interfaces are 
4ef0: 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
4f00: 65 20 69 6e 20 70 6f 69 6e 74 20 72 65 6c 65 61  e in point relea
4f10: 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ses..**.** These
4f20: 20 6d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20   macros used to 
4f30: 72 65 73 6f 6c 76 65 20 74 6f 20 76 61 72 69 6f  resolve to vario
4f40: 75 73 20 6b 69 6e 64 73 20 6f 66 20 63 6f 6d 70  us kinds of comp
4f50: 69 6c 65 72 20 6d 61 67 69 63 20 74 68 61 74 0a  iler magic that.
4f60: 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65 72 61 74  ** would generat
4f70: 65 20 77 61 72 6e 69 6e 67 20 6d 65 73 73 61 67  e warning messag
4f80: 65 73 20 77 68 65 6e 20 74 68 65 79 20 77 65 72  es when they wer
4f90: 65 20 75 73 65 64 2e 20 20 42 75 74 20 74 68 61  e used.  But tha
4fa0: 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 6d 61  t.** compiler ma
4fb0: 67 69 63 20 65 6e 64 65 64 20 75 70 20 67 65 6e  gic ended up gen
4fc0: 65 72 61 74 69 6e 67 20 73 75 63 68 20 61 20 66  erating such a f
4fd0: 6c 75 72 72 79 20 6f 66 20 62 75 67 20 72 65 70  lurry of bug rep
4fe0: 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20 77 65 20  orts.** that we 
4ff0: 68 61 76 65 20 74 61 6b 65 6e 20 69 74 20 61 6c  have taken it al
5000: 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e 65 20 62  l out and gone b
5010: 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 73 69 6d  ack to using sim
5020: 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d 61 63 72  ple.** noop macr
5030: 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  os..*/.#define S
5040: 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44  QLITE_DEPRECATED
5050: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5060: 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 0a 2f 2a  EXPERIMENTAL../*
5070: 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68 65 73 65  .** Ensure these
5080: 20 73 79 6d 62 6f 6c 73 20 77 65 72 65 20 6e 6f   symbols were no
5090: 74 20 64 65 66 69 6e 65 64 20 62 79 20 73 6f 6d  t defined by som
50a0: 65 20 70 72 65 76 69 6f 75 73 20 68 65 61 64 65  e previous heade
50b0: 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65  r file..*/.#ifde
50c0: 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  f SQLITE_VERSION
50d0: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
50e0: 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69 66 0a 23  VERSION.#endif.#
50f0: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52  ifdef SQLITE_VER
5100: 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 20 75 6e  SION_NUMBER.# un
5110: 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49  def SQLITE_VERSI
5120: 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e 64 69 66  ON_NUMBER.#endif
5130: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
5140: 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 4c  : Compile-Time L
5150: 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e  ibrary Version N
5160: 75 6d 62 65 72 73 20 7b 48 31 30 30 31 30 7d 20  umbers {H10010} 
5170: 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S60100>.**.** T
5180: 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  he SQLITE_VERSIO
5190: 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f 56 45 52  N and SQLITE_VER
51a0: 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 64 65 66  SION_NUMBER #def
51b0: 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73  ines in.** the s
51c0: 71 6c 69 74 65 33 2e 68 20 66 69 6c 65 20 73 70  qlite3.h file sp
51d0: 65 63 69 66 79 20 74 68 65 20 76 65 72 73 69 6f  ecify the versio
51e0: 6e 20 6f 66 20 53 51 4c 69 74 65 20 77 69 74 68  n of SQLite with
51f0: 20 77 68 69 63 68 0a 2a 2a 20 74 68 61 74 20 68   which.** that h
5200: 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 61 73  eader file is as
5210: 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  sociated..**.** 
5220: 54 68 65 20 22 76 65 72 73 69 6f 6e 22 20 6f 66  The "version" of
5230: 20 53 51 4c 69 74 65 20 69 73 20 61 20 73 74 72   SQLite is a str
5240: 69 6e 67 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ing of the form 
5250: 22 57 2e 58 2e 59 22 20 6f 72 20 22 57 2e 58 2e  "W.X.Y" or "W.X.
5260: 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20 57 20 76  Y.Z"..** The W v
5270: 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72 20 76 65  alue is major ve
5280: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64  rsion number and
5290: 20 69 73 20 61 6c 77 61 79 73 20 33 20 69 6e 20   is always 3 in 
52a0: 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54 68 65 20  SQLite3..** The 
52b0: 57 20 76 61 6c 75 65 20 6f 6e 6c 79 20 63 68 61  W value only cha
52c0: 6e 67 65 73 20 77 68 65 6e 20 62 61 63 6b 77 61  nges when backwa
52d0: 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  rds compatibilit
52e0: 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65 6e 20 61  y is.** broken a
52f0: 6e 64 20 77 65 20 69 6e 74 65 6e 64 20 74 6f 20  nd we intend to 
5300: 6e 65 76 65 72 20 62 72 65 61 6b 20 62 61 63 6b  never break back
5310: 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
5320: 69 74 79 2e 0a 2a 2a 20 54 68 65 20 58 20 76 61  ity..** The X va
5330: 6c 75 65 20 69 73 20 74 68 65 20 6d 69 6e 6f 72  lue is the minor
5340: 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20   version number 
5350: 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e 67 65 73  and only changes
5360: 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72 65 20 61   when.** there a
5370: 72 65 20 6d 61 6a 6f 72 20 66 65 61 74 75 72 65  re major feature
5380: 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20 74 68   enhancements th
5390: 61 74 20 61 72 65 20 66 6f 72 77 61 72 64 73 20  at are forwards 
53a0: 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 62 75  compatible.** bu
53b0: 74 20 6e 6f 74 20 62 61 63 6b 77 61 72 64 73 20  t not backwards 
53c0: 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2a 20 54  compatible..** T
53d0: 68 65 20 59 20 76 61 6c 75 65 20 69 73 20 74 68  he Y value is th
53e0: 65 20 72 65 6c 65 61 73 65 20 6e 75 6d 62 65 72  e release number
53f0: 20 61 6e 64 20 69 73 20 69 6e 63 72 65 6d 65 6e   and is incremen
5400: 74 65 64 20 77 69 74 68 0a 2a 2a 20 65 61 63 68  ted with.** each
5410: 20 72 65 6c 65 61 73 65 20 62 75 74 20 72 65 73   release but res
5420: 65 74 73 20 62 61 63 6b 20 74 6f 20 30 20 77 68  ets back to 0 wh
5430: 65 6e 65 76 65 72 20 58 20 69 73 20 69 6e 63 72  enever X is incr
5440: 65 6d 65 6e 74 65 64 2e 0a 2a 2a 20 54 68 65 20  emented..** The 
5450: 5a 20 76 61 6c 75 65 20 6f 6e 6c 79 20 61 70 70  Z value only app
5460: 65 61 72 73 20 6f 6e 20 62 72 61 6e 63 68 20 72  ears on branch r
5470: 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  eleases..**.** T
5480: 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  he SQLITE_VERSIO
5490: 4e 5f 4e 55 4d 42 45 52 20 69 73 20 61 6e 20 69  N_NUMBER is an i
54a0: 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 63  nteger that is c
54b0: 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 20 66 6f  omputed as.** fo
54c0: 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c  llows:.**.** <bl
54d0: 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a  ockquote><pre>.*
54e0: 2a 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  * SQLITE_VERSION
54f0: 5f 4e 55 4d 42 45 52 20 3d 20 57 2a 31 30 30 30  _NUMBER = W*1000
5500: 30 30 30 20 2b 20 58 2a 31 30 30 30 20 2b 20 59  000 + X*1000 + Y
5510: 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
5520: 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 53 69  kquote>.**.** Si
5530: 6e 63 65 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e  nce version 3.6.
5540: 31 38 2c 20 53 51 4c 69 74 65 20 73 6f 75 72 63  18, SQLite sourc
5550: 65 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e 20  e code has been 
5560: 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a 2a  stored in the.**
5570: 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70 3a 2f   <a href="http:/
5580: 2f 77 77 77 2e 66 6f 73 73 69 6c 2d 73 63 6d 2e  /www.fossil-scm.
5590: 6f 72 67 2f 22 3e 66 6f 73 73 69 6c 20 63 6f 6e  org/">fossil con
55a0: 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e 61 67  figuration manag
55b0: 65 6d 65 6e 74 0a 2a 2a 20 73 79 73 74 65 6d 3c  ement.** system<
55c0: 2f 61 3e 2e 20 20 54 68 65 20 53 51 4c 49 54 45  /a>.  The SQLITE
55d0: 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a 20 6d 61  _SOURCE_ID.** ma
55e0: 63 72 6f 20 69 73 20 61 20 73 74 72 69 6e 67 20  cro is a string 
55f0: 77 68 69 63 68 20 69 64 65 6e 74 69 66 69 65 73  which identifies
5600: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 63 68   a particular ch
5610: 65 63 6b 2d 69 6e 20 6f 66 20 53 51 4c 69 74 65  eck-in of SQLite
5620: 0a 2a 2a 20 77 69 74 68 69 6e 20 69 74 73 20 63  .** within its c
5630: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e  onfiguration man
5640: 61 67 65 6d 65 6e 74 20 73 79 73 74 65 6d 2e 20  agement system. 
5650: 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6e 74   The string cont
5660: 61 69 6e 73 20 74 68 65 0a 2a 2a 20 64 61 74 65  ains the.** date
5670: 20 61 6e 64 20 74 69 6d 65 20 6f 66 20 74 68 65   and time of the
5680: 20 63 68 65 63 6b 2d 69 6e 20 28 55 54 43 29 20   check-in (UTC) 
5690: 61 6e 64 20 61 6e 20 53 48 41 31 20 68 61 73 68  and an SHA1 hash
56a0: 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 0a 2a   of the entire.*
56b0: 2a 20 73 6f 75 72 63 65 20 74 72 65 65 2e 0a 2a  * source tree..*
56c0: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
56d0: 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
56e0: 6f 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74  on()],.** [sqlit
56f0: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  e3_libversion_nu
5700: 6d 62 65 72 28 29 5d 2c 20 5b 73 71 6c 69 74 65  mber()], [sqlite
5710: 33 5f 73 6f 75 72 63 65 69 64 28 29 5d 2c 0a 2a  3_sourceid()],.*
5720: 2a 20 5b 73 71 6c 69 74 65 5f 76 65 72 73 69 6f  * [sqlite_versio
5730: 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  n()] and [sqlite
5740: 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d 2e 0a 2a  _source_id()]..*
5750: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
5760: 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b 48 31 30  s: [H10011] [H10
5770: 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  014].*/.#define 
5780: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 20 20  SQLITE_VERSION  
5790: 20 20 20 20 20 20 22 33 2e 36 2e 32 30 22 0a 23        "3.6.20".#
57a0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 45  define SQLITE_VE
57b0: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 33 30 30  RSION_NUMBER 300
57c0: 36 30 32 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  6020.#define SQL
57d0: 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 20 20 20  ITE_SOURCE_ID   
57e0: 20 20 20 22 32 30 30 39 2d 31 31 2d 30 34 20 31     "2009-11-04 1
57f0: 33 3a 33 30 3a 30 32 20 65 62 37 61 35 34 34 66  3:30:02 eb7a544f
5800: 65 34 39 64 31 36 32 36 62 61 63 65 63 66 65 35  e49d1626bacecfe5
5810: 33 64 64 63 30 33 66 65 30 38 32 65 33 32 34 33  3ddc03fe082e3243
5820: 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  "../*.** CAPI3RE
5830: 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 62 72  F: Run-Time Libr
5840: 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62  ary Version Numb
5850: 65 72 73 20 7b 48 31 30 30 32 30 7d 20 3c 53 36  ers {H10020} <S6
5860: 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0100>.** KEYWORD
5870: 53 3a 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69  S: sqlite3_versi
5880: 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69  on.**.** These i
5890: 6e 74 65 72 66 61 63 65 73 20 70 72 6f 76 69 64  nterfaces provid
58a0: 65 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72  e the same infor
58b0: 6d 61 74 69 6f 6e 20 61 73 20 74 68 65 20 5b 53  mation as the [S
58c0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d 2c 0a  QLITE_VERSION],.
58d0: 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49  ** [SQLITE_VERSI
58e0: 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61 6e 64 20  ON_NUMBER], and 
58f0: 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49  [SQLITE_SOURCE_I
5900: 44 5d 20 23 64 65 66 69 6e 65 73 20 69 6e 20 74  D] #defines in t
5910: 68 65 20 68 65 61 64 65 72 2c 0a 2a 2a 20 62 75  he header,.** bu
5920: 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64  t are associated
5930: 20 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 72   with the librar
5940: 79 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  y instead of the
5950: 20 68 65 61 64 65 72 20 66 69 6c 65 2e 20 20 43   header file.  C
5960: 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72 6f 67 72  autious.** progr
5970: 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 69 6e 63  ammers might inc
5980: 6c 75 64 65 20 61 73 73 65 72 74 28 29 20 73 74  lude assert() st
5990: 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 69  atements in thei
59a0: 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f  r application to
59b0: 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 61 74 20  .** verify that 
59c0: 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20  values returned 
59d0: 62 79 20 74 68 65 73 65 20 69 6e 74 65 72 66 61  by these interfa
59e0: 63 65 73 20 6d 61 74 63 68 20 74 68 65 20 6d 61  ces match the ma
59f0: 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68 65 20 68  cros in.** the h
5a00: 65 61 64 65 72 2c 20 61 6e 64 20 74 68 75 73 20  eader, and thus 
5a10: 69 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20  insure that the 
5a20: 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 0a 2a  application is.*
5a30: 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  * compiled with 
5a40: 6d 61 74 63 68 69 6e 67 20 6c 69 62 72 61 72 79  matching library
5a50: 20 61 6e 64 20 68 65 61 64 65 72 20 66 69 6c 65   and header file
5a60: 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  s..**.** <blockq
5a70: 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 61 73  uote><pre>.** as
5a80: 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6c 69  sert( sqlite3_li
5a90: 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28  bversion_number(
5aa0: 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  )==SQLITE_VERSIO
5ab0: 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a 2a 20 61  N_NUMBER );.** a
5ac0: 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 73 71  ssert( strcmp(sq
5ad0: 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29  lite3_sourceid()
5ae0: 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49  ,SQLITE_SOURCE_I
5af0: 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61 73 73 65  D)==0 );.** asse
5b00: 72 74 28 20 73 74 72 63 6d 70 28 73 71 6c 69 74  rt( strcmp(sqlit
5b10: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 2c 53 51  e3_libversion,SQ
5b20: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29 3d 3d 30  LITE_VERSION)==0
5b30: 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62   );.** </pre></b
5b40: 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
5b50: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 62   The sqlite3_lib
5b60: 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69  version() functi
5b70: 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  on returns the s
5b80: 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ame information 
5b90: 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74 68 65 20  as is.** in the 
5ba0: 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b  sqlite3_version[
5bb0: 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e  ] string constan
5bc0: 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e  t.  The function
5bd0: 20 69 73 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20   is provided.** 
5be0: 66 6f 72 20 75 73 65 20 69 6e 20 44 4c 4c 73 20  for use in DLLs 
5bf0: 73 69 6e 63 65 20 44 4c 4c 20 75 73 65 72 73 20  since DLL users 
5c00: 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f 74 20 68  usually do not h
5c10: 61 76 65 20 64 69 72 65 63 74 20 61 63 63 65 73  ave direct acces
5c20: 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a 2a 20 63  s to string.** c
5c30: 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 69 6e 20  onstants within 
5c40: 74 68 65 20 44 4c 4c 2e 20 20 53 69 6d 69 6c 61  the DLL.  Simila
5c50: 72 6c 79 2c 20 74 68 65 20 73 71 6c 69 74 65 33  rly, the sqlite3
5c60: 5f 73 6f 75 72 63 65 69 64 28 29 20 66 75 6e 63  _sourceid() func
5c70: 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e 73 20  tion.** returns 
5c80: 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61  the same informa
5c90: 74 69 6f 6e 20 61 73 20 69 73 20 69 6e 20 74 68  tion as is in th
5ca0: 65 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45  e [SQLITE_SOURCE
5cb0: 5f 49 44 5d 20 23 64 65 66 69 6e 65 20 6f 66 0a  _ID] #define of.
5cc0: 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 66 69  ** the header fi
5cd0: 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  le..**.** See al
5ce0: 73 6f 3a 20 5b 73 71 6c 69 74 65 5f 76 65 72 73  so: [sqlite_vers
5cf0: 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  ion()] and [sqli
5d00: 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d 2e  te_source_id()].
5d10: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
5d20: 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d 20 5b 48  nts: [H10021] [H
5d30: 31 30 30 32 32 5d 20 5b 48 31 30 30 32 33 5d 0a  10022] [H10023].
5d40: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
5d50: 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33  nst char sqlite3
5d60: 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 51 4c  _version[] = SQL
5d70: 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53 51 4c  ITE_VERSION;.SQL
5d80: 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
5d90: 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76  ar *sqlite3_libv
5da0: 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 53 51  ersion(void);.SQ
5db0: 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
5dc0: 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6f 75  har *sqlite3_sou
5dd0: 72 63 65 69 64 28 76 6f 69 64 29 3b 0a 53 51 4c  rceid(void);.SQL
5de0: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
5df0: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  te3_libversion_n
5e00: 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a 0a 2f 2a  umber(void);../*
5e10: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65  .** CAPI3REF: Te
5e20: 73 74 20 54 6f 20 53 65 65 20 49 66 20 54 68 65  st To See If The
5e30: 20 4c 69 62 72 61 72 79 20 49 73 20 54 68 72 65   Library Is Thre
5e40: 61 64 73 61 66 65 20 7b 48 31 30 31 30 30 7d 20  adsafe {H10100} 
5e50: 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 53  <S60100>.**.** S
5e60: 51 4c 69 74 65 20 63 61 6e 20 62 65 20 63 6f 6d  QLite can be com
5e70: 70 69 6c 65 64 20 77 69 74 68 20 6f 72 20 77 69  piled with or wi
5e80: 74 68 6f 75 74 20 6d 75 74 65 78 65 73 2e 20 20  thout mutexes.  
5e90: 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 53 51 4c  When.** the [SQL
5ea0: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20  ITE_THREADSAFE] 
5eb0: 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d  C preprocessor m
5ec0: 61 63 72 6f 20 69 73 20 31 20 6f 72 20 32 2c 20  acro is 1 or 2, 
5ed0: 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65  mutexes.** are e
5ee0: 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c 69 74  nabled and SQLit
5ef0: 65 20 69 73 20 74 68 72 65 61 64 73 61 66 65 2e  e is threadsafe.
5f00: 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 53    When the.** [S
5f10: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
5f20: 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20 0a 2a  ] macro is 0, .*
5f30: 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 72  * the mutexes ar
5f40: 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69 74 68  e omitted.  With
5f50: 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65 73 2c  out the mutexes,
5f60: 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0a   it is not safe.
5f70: 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69 74 65  ** to use SQLite
5f80: 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20 66 72   concurrently fr
5f90: 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  om more than one
5fa0: 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 45   thread..**.** E
5fb0: 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65 73 20  nabling mutexes 
5fc0: 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75 72 61  incurs a measura
5fd0: 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ble performance 
5fe0: 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f 20 69  penalty..** So i
5ff0: 66 20 73 70 65 65 64 20 69 73 20 6f 66 20 75 74  f speed is of ut
6000: 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63 65 2c  most importance,
6010: 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73 65 20   it makes sense 
6020: 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68  to disable.** th
6030: 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75 74 20  e mutexes.  But 
6040: 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61 66 65  for maximum safe
6050: 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68 6f 75  ty, mutexes shou
6060: 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a  ld be enabled..*
6070: 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 65  * The default be
6080: 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20 6d 75  havior is for mu
6090: 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e 61 62  texes to be enab
60a0: 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  led..**.** This 
60b0: 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65  interface can be
60c0: 20 75 73 65 64 20 62 79 20 61 6e 20 61 70 70 6c   used by an appl
60d0: 69 63 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20  ication to make 
60e0: 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a  sure that the.**
60f0: 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69   version of SQLi
6100: 74 65 20 74 68 61 74 20 69 74 20 69 73 20 6c 69  te that it is li
6110: 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74 20 77 61  nking against wa
6120: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a  s compiled with.
6130: 2a 2a 20 74 68 65 20 64 65 73 69 72 65 64 20 73  ** the desired s
6140: 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 5b 53  etting of the [S
6150: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
6160: 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54  ] macro..**.** T
6170: 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 6f 6e  his interface on
6180: 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e 20 74 68  ly reports on th
6190: 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6d  e compile-time m
61a0: 75 74 65 78 20 73 65 74 74 69 6e 67 0a 2a 2a 20  utex setting.** 
61b0: 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54  of the [SQLITE_T
61c0: 48 52 45 41 44 53 41 46 45 5d 20 66 6c 61 67 2e  HREADSAFE] flag.
61d0: 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63    If SQLite is c
61e0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20  ompiled with.** 
61f0: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
6200: 45 3d 31 20 74 68 65 6e 20 6d 75 74 65 78 65 73  E=1 then mutexes
6210: 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62 79 20   are enabled by 
6220: 64 65 66 61 75 6c 74 20 62 75 74 0a 2a 2a 20 63  default but.** c
6230: 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f 72 20 70  an be fully or p
6240: 61 72 74 69 61 6c 6c 79 20 64 69 73 61 62 6c 65  artially disable
6250: 64 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74  d using a call t
6260: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  o [sqlite3_confi
6270: 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20 74 68 65  g()].** with the
6280: 20 76 65 72 62 73 20 5b 53 51 4c 49 54 45 5f 43   verbs [SQLITE_C
6290: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
62a0: 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  AD], [SQLITE_CON
62b0: 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d  FIG_MULTITHREAD]
62c0: 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ,.** or [SQLITE_
62d0: 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 2e 20 20  CONFIG_MUTEX].  
62e0: 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
62f0: 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
6300: 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e 6c 79 20  n shows.** only 
6310: 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6d 70  the default comp
6320: 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 69 6e 67  ile-time setting
6330: 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e 2d 74 69  , not any run-ti
6340: 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 6f  me changes.** to
6350: 20 74 68 61 74 20 73 65 74 74 69 6e 67 2e 0a 2a   that setting..*
6360: 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68  *.** See the [th
6370: 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f  reading mode] do
6380: 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  cumentation for 
6390: 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
63a0: 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65  mation..**.** Re
63b0: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30  quirements: [H10
63c0: 31 30 31 5d 20 5b 48 31 30 31 30 32 5d 0a 2a 2f  101] [H10102].*/
63d0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
63e0: 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61  sqlite3_threadsa
63f0: 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a  fe(void);../*.**
6400: 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62   CAPI3REF: Datab
6410: 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 48  ase Connection H
6420: 61 6e 64 6c 65 20 7b 48 31 32 30 30 30 7d 20 3c  andle {H12000} <
6430: 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S40200>.** KEYWO
6440: 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65 20 63  RDS: {database c
6450: 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61 74 61  onnection} {data
6460: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
6470: 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f 70 65  }.**.** Each ope
6480: 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  n SQLite databas
6490: 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  e is represented
64a0: 20 62 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   by a pointer to
64b0: 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a   an instance of.
64c0: 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 20 73 74  ** the opaque st
64d0: 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 73  ructure named "s
64e0: 71 6c 69 74 65 33 22 2e 20 20 49 74 20 69 73 20  qlite3".  It is 
64f0: 75 73 65 66 75 6c 20 74 6f 20 74 68 69 6e 6b 20  useful to think 
6500: 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 0a 2a 2a  of an sqlite3.**
6510: 20 70 6f 69 6e 74 65 72 20 61 73 20 61 6e 20 6f   pointer as an o
6520: 62 6a 65 63 74 2e 20 20 54 68 65 20 5b 73 71 6c  bject.  The [sql
6530: 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73  ite3_open()], [s
6540: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
6550: 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65  , and.** [sqlite
6560: 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74  3_open_v2()] int
6570: 65 72 66 61 63 65 73 20 61 72 65 20 69 74 73 20  erfaces are its 
6580: 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c 20 61 6e  constructors, an
6590: 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  d [sqlite3_close
65a0: 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73 20 64 65  ()].** is its de
65b0: 73 74 72 75 63 74 6f 72 2e 20 20 54 68 65 72 65  structor.  There
65c0: 20 61 72 65 20 6d 61 6e 79 20 6f 74 68 65 72 20   are many other 
65d0: 69 6e 74 65 72 66 61 63 65 73 20 28 73 75 63 68  interfaces (such
65e0: 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   as.** [sqlite3_
65f0: 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b  prepare_v2()], [
6600: 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
6610: 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 6e 64 0a  unction()], and.
6620: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  ** [sqlite3_busy
6630: 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 6f 20 6e  _timeout()] to n
6640: 61 6d 65 20 62 75 74 20 74 68 72 65 65 29 20 74  ame but three) t
6650: 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64 73 20  hat are methods 
6660: 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33  on an.** sqlite3
6670: 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65   object..*/.type
6680: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
6690: 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 2f 2a 0a  e3 sqlite3;../*.
66a0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36 34 2d  ** CAPI3REF: 64-
66b0: 42 69 74 20 49 6e 74 65 67 65 72 20 54 79 70 65  Bit Integer Type
66c0: 73 20 7b 48 31 30 32 30 30 7d 20 3c 53 31 30 31  s {H10200} <S101
66d0: 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  10>.** KEYWORDS:
66e0: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71   sqlite_int64 sq
66f0: 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a 2a 0a 2a  lite_uint64.**.*
6700: 2a 20 42 65 63 61 75 73 65 20 74 68 65 72 65 20  * Because there 
6710: 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70 6c 61 74  is no cross-plat
6720: 66 6f 72 6d 20 77 61 79 20 74 6f 20 73 70 65 63  form way to spec
6730: 69 66 79 20 36 34 2d 62 69 74 20 69 6e 74 65 67  ify 64-bit integ
6740: 65 72 20 74 79 70 65 73 0a 2a 2a 20 53 51 4c 69  er types.** SQLi
6750: 74 65 20 69 6e 63 6c 75 64 65 73 20 74 79 70 65  te includes type
6760: 64 65 66 73 20 66 6f 72 20 36 34 2d 62 69 74 20  defs for 64-bit 
6770: 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67  signed and unsig
6780: 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a  ned integers..**
6790: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
67a0: 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65  int64 and sqlite
67b0: 33 5f 75 69 6e 74 36 34 20 61 72 65 20 74 68 65  3_uint64 are the
67c0: 20 70 72 65 66 65 72 72 65 64 20 74 79 70 65 20   preferred type 
67d0: 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 20  definitions..** 
67e0: 54 68 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  The sqlite_int64
67f0: 20 61 6e 64 20 73 71 6c 69 74 65 5f 75 69 6e 74   and sqlite_uint
6800: 36 34 20 74 79 70 65 73 20 61 72 65 20 73 75 70  64 types are sup
6810: 70 6f 72 74 65 64 20 66 6f 72 20 62 61 63 6b 77  ported for backw
6820: 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62  ards.** compatib
6830: 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a  ility only..**.*
6840: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20  * Requirements: 
6850: 5b 48 31 30 32 30 31 5d 20 5b 48 31 30 32 30 32  [H10201] [H10202
6860: 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ].*/.#ifdef SQLI
6870: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a 20 20  TE_INT64_TYPE.  
6880: 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f 49  typedef SQLITE_I
6890: 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74 65  NT64_TYPE sqlite
68a0: 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65  _int64;.  typede
68b0: 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c 49 54  f unsigned SQLIT
68c0: 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c  E_INT64_TYPE sql
68d0: 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 69  ite_uint64;.#eli
68e0: 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56  f defined(_MSC_V
68f0: 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  ER) || defined(_
6900: 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 20 74  _BORLANDC__).  t
6910: 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 20 73  ypedef __int64 s
6920: 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74  qlite_int64;.  t
6930: 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20  ypedef unsigned 
6940: 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75  __int64 sqlite_u
6950: 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 20 74  int64;.#else.  t
6960: 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e 67  ypedef long long
6970: 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e 74 36   int sqlite_int6
6980: 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73  4;.  typedef uns
6990: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
69a0: 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  int sqlite_uint6
69b0: 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65  4;.#endif.typede
69c0: 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73  f sqlite_int64 s
69d0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a 74 79  qlite3_int64;.ty
69e0: 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e  pedef sqlite_uin
69f0: 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 6e 74  t64 sqlite3_uint
6a00: 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f  64;../*.** If co
6a10: 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 72  mpiling for a pr
6a20: 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 63  ocessor that lac
6a30: 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  ks floating poin
6a40: 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75  t support,.** su
6a50: 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 72  bstitute integer
6a60: 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f   for floating-po
6a70: 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  int..*/.#ifdef S
6a80: 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
6a90: 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69  ING_POINT.# defi
6aa0: 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  ne double sqlite
6ab0: 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 0a 0a  3_int64.#endif..
6ac0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
6ad0: 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 62 61  Closing A Databa
6ae0: 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48  se Connection {H
6af0: 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30 3e 3c  12010} <S30100><
6b00: 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S40200>.**.** Th
6b10: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
6b20: 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72  e destructor for
6b30: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f   the [sqlite3] o
6b40: 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 70 70  bject..**.** App
6b50: 6c 69 63 61 74 69 6f 6e 73 20 6d 75 73 74 20 5b  lications must [
6b60: 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
6b70: 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 61 6c 6c   | finalize] all
6b80: 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
6b90: 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64 20 5b 73  ments].** and [s
6ba0: 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
6bb0: 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c 6c 20 5b  e | close] all [
6bc0: 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d 20 61 73  BLOB handles] as
6bd0: 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a  sociated with.**
6be0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f   the [sqlite3] o
6bf0: 62 6a 65 63 74 20 70 72 69 6f 72 20 74 6f 20 61  bject prior to a
6c00: 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 63 6c 6f  ttempting to clo
6c10: 73 65 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a  se the object..*
6c20: 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 33  *.** If [sqlite3
6c30: 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20 69 6e 76  _close()] is inv
6c40: 6f 6b 65 64 20 77 68 69 6c 65 20 61 20 74 72 61  oked while a tra
6c50: 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nsaction is open
6c60: 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63  ,.** the transac
6c70: 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74 69  tion is automati
6c80: 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  cally rolled bac
6c90: 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 70  k..**.** The C p
6ca0: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
6cb0: 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 6d  ite3_close(C)] m
6cc0: 75 73 74 20 62 65 20 65 69 74 68 65 72 20 61 20  ust be either a 
6cd0: 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  NULL.** pointer 
6ce0: 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 33 5d 20  or an [sqlite3] 
6cf0: 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65 72 20 6f  object pointer o
6d00: 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  btained.** from 
6d10: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
6d20: 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  , [sqlite3_open1
6d30: 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c  6()], or.** [sql
6d40: 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c  ite3_open_v2()],
6d50: 20 61 6e 64 20 6e 6f 74 20 70 72 65 76 69 6f 75   and not previou
6d60: 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a  sly closed..**.*
6d70: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
6d80: 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b 48 31 32  ** [H12011] [H12
6d90: 30 31 32 5d 20 5b 48 31 32 30 31 33 5d 20 5b 48  012] [H12013] [H
6da0: 31 32 30 31 34 5d 20 5b 48 31 32 30 31 35 5d 20  12014] [H12015] 
6db0: 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53 51 4c 49  [H12019].*/.SQLI
6dc0: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
6dd0: 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33  e3_close(sqlite3
6de0: 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   *);../*.** The 
6df0: 74 79 70 65 20 66 6f 72 20 61 20 63 61 6c 6c 62  type for a callb
6e00: 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  ack function..**
6e10: 20 54 68 69 73 20 69 73 20 6c 65 67 61 63 79 20   This is legacy 
6e20: 61 6e 64 20 64 65 70 72 65 63 61 74 65 64 2e 20  and deprecated. 
6e30: 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20   It is included 
6e40: 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a  for historical.*
6e50: 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  * compatibility 
6e60: 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 6d  and is not docum
6e70: 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65  ented..*/.typede
6e80: 66 20 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 5f  f int (*sqlite3_
6e90: 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c  callback)(void*,
6ea0: 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 72  int,char**, char
6eb0: 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  **);../*.** CAPI
6ec0: 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 51  3REF: One-Step Q
6ed0: 75 65 72 79 20 45 78 65 63 75 74 69 6f 6e 20 49  uery Execution I
6ee0: 6e 74 65 72 66 61 63 65 20 7b 48 31 32 31 30 30  nterface {H12100
6ef0: 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a  } <S10000>.**.**
6f00: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65   The sqlite3_exe
6f10: 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  c() interface is
6f20: 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 77 61   a convenient wa
6f30: 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20 6f 6e 65  y of running one
6f40: 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20   or more.** SQL 
6f50: 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74 68 6f  statements witho
6f60: 75 74 20 68 61 76 69 6e 67 20 74 6f 20 77 72 69  ut having to wri
6f70: 74 65 20 61 20 6c 6f 74 20 6f 66 20 43 20 63 6f  te a lot of C co
6f80: 64 65 2e 20 20 54 68 65 20 55 54 46 2d 38 20 65  de.  The UTF-8 e
6f90: 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 73 74  ncoded.** SQL st
6fa0: 61 74 65 6d 65 6e 74 73 20 61 72 65 20 70 61 73  atements are pas
6fb0: 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 73 65  sed in as the se
6fc0: 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74  cond parameter t
6fd0: 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  o sqlite3_exec()
6fe0: 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65  ..** The stateme
6ff0: 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65  nts are evaluate
7000: 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20 75 6e 74  d one by one unt
7010: 69 6c 20 65 69 74 68 65 72 20 61 6e 20 65 72 72  il either an err
7020: 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e 74 65  or or.** an inte
7030: 72 72 75 70 74 20 69 73 20 65 6e 63 6f 75 6e 74  rrupt is encount
7040: 65 72 65 64 2c 20 6f 72 20 75 6e 74 69 6c 20 74  ered, or until t
7050: 68 65 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e 65  hey are all done
7060: 2e 20 20 54 68 65 20 33 72 64 20 70 61 72 61 6d  .  The 3rd param
7070: 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e 20 6f 70  eter.** is an op
7080: 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20  tional callback 
7090: 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20  that is invoked 
70a0: 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f  once for each ro
70b0: 77 20 6f 66 20 61 6e 79 20 71 75 65 72 79 0a 2a  w of any query.*
70c0: 2a 20 72 65 73 75 6c 74 73 20 70 72 6f 64 75 63  * results produc
70d0: 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 74  ed by the SQL st
70e0: 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 35  atements.  The 5
70f0: 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 65 6c  th parameter tel
7100: 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20 77  ls where.** to w
7110: 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d  rite any error m
7120: 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  essages..**.** T
7130: 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
7140: 20 70 61 73 73 65 64 20 62 61 63 6b 20 74 68 72   passed back thr
7150: 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61 72  ough the 5th par
7160: 61 6d 65 74 65 72 20 69 73 20 68 65 6c 64 0a 2a  ameter is held.*
7170: 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  * in memory obta
7180: 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74  ined from [sqlit
7190: 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54  e3_malloc()].  T
71a0: 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72 79  o avoid a memory
71b0: 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 63 61   leak,.** the ca
71c0: 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61 74 69 6f  lling applicatio
71d0: 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 5b 73  n should call [s
71e0: 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f  qlite3_free()] o
71f0: 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a 2a 20 6d  n any error.** m
7200: 65 73 73 61 67 65 20 72 65 74 75 72 6e 65 64 20  essage returned 
7210: 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20  through the 5th 
7220: 70 61 72 61 6d 65 74 65 72 20 77 68 65 6e 20 69  parameter when i
7230: 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75  t has finished u
7240: 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65 72 72 6f  sing.** the erro
7250: 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a  r message..**.**
7260: 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   If the SQL stat
7270: 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 32 6e 64  ement in the 2nd
7280: 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55   parameter is NU
7290: 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73  LL or an empty s
72a0: 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 73 74  tring.** or a st
72b0: 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
72c0: 6f 6e 6c 79 20 77 68 69 74 65 73 70 61 63 65 20  only whitespace 
72d0: 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 68  and comments, th
72e0: 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 74 61  en no SQL.** sta
72f0: 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c  tements are eval
7300: 75 61 74 65 64 20 61 6e 64 20 74 68 65 20 64 61  uated and the da
7310: 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 63 68  tabase is not ch
7320: 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  anged..**.** The
7330: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
7340: 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6d 70  interface is imp
7350: 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 65 72 6d  lemented in term
7360: 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  s of.** [sqlite3
7370: 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20  _prepare_v2()], 
7380: 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
7390: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66  , and [sqlite3_f
73a0: 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54  inalize()]..** T
73b0: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  he sqlite3_exec(
73c0: 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e  ) routine does n
73d0: 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61  othing to the da
73e0: 74 61 62 61 73 65 20 74 68 61 74 20 63 61 6e 6e  tabase that cann
73f0: 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 62 79  ot be done.** by
7400: 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
7410: 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65  e_v2()], [sqlite
7420: 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e 64 20 5b  3_step()], and [
7430: 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
7440: 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ()]..**.** The f
7450: 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74  irst parameter t
7460: 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  o [sqlite3_exec(
7470: 29 5d 20 6d 75 73 74 20 62 65 20 61 6e 20 76 61  )] must be an va
7480: 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a 2a 2a 20  lid and open.** 
7490: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
74a0: 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tion]..**.** The
74b0: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
74c0: 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65  tion must not be
74d0: 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 0a 2a 2a   closed while.**
74e0: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
74f0: 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a  ] is running..**
7500: 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  .** The calling 
7510: 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
7520: 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 66 72 65  use [sqlite3_fre
7530: 65 28 29 5d 20 74 6f 20 66 72 65 65 0a 2a 2a 20  e()] to free.** 
7540: 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20  the memory that 
7550: 2a 65 72 72 6d 73 67 20 69 73 20 6c 65 66 74 20  *errmsg is left 
7560: 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f 6e 63 65  pointing at once
7570: 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65   the error.** me
7580: 73 73 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67  ssage is no long
7590: 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a  er needed..**.**
75a0: 20 54 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   The SQL stateme
75b0: 6e 74 20 74 65 78 74 20 69 6e 20 74 68 65 20 32  nt text in the 2
75c0: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
75d0: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
75e0: 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61 69 6e 20  .** must remain 
75f0: 75 6e 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20  unchanged while 
7600: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
7610: 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a   is running..**.
7620: 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
7630: 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20 5b 48 31  .** [H12101] [H1
7640: 32 31 30 32 5d 20 5b 48 31 32 31 30 34 5d 20 5b  2102] [H12104] [
7650: 48 31 32 31 30 35 5d 20 5b 48 31 32 31 30 37 5d  H12105] [H12107]
7660: 20 5b 48 31 32 31 31 30 5d 20 5b 48 31 32 31 31   [H12110] [H1211
7670: 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a 2a 20 5b  3] [H12116].** [
7680: 48 31 32 31 31 39 5d 20 5b 48 31 32 31 32 32 5d  H12119] [H12122]
7690: 20 5b 48 31 32 31 32 35 5d 20 5b 48 31 32 31 33   [H12125] [H1213
76a0: 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b 48 31 32  1] [H12134] [H12
76b0: 31 33 37 5d 20 5b 48 31 32 31 33 38 5d 0a 2a 2f  137] [H12138].*/
76c0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
76d0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20  sqlite3_exec(.  
76e0: 73 71 6c 69 74 65 33 2a 2c 20 20 20 20 20 20 20  sqlite3*,       
76f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7700: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e             /* An
7710: 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a   open database *
7720: 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
7730: 73 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  sql,            
7740: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
7750: 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c  * SQL to be eval
7760: 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 28  uated */.  int (
7770: 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  *callback)(void*
7780: 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63 68 61 72  ,int,char**,char
7790: 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c 62 61 63  **),  /* Callbac
77a0: 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  k function */.  
77b0: 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20  void *,         
77c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77d0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73             /* 1s
77e0: 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 61  t argument to ca
77f0: 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 68 61 72  llback */.  char
7800: 20 2a 2a 65 72 72 6d 73 67 20 20 20 20 20 20 20   **errmsg       
7810: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7820: 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20         /* Error 
7830: 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 72 65  msg written here
7840: 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.);../*.** CA
7850: 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20 43  PI3REF: Result C
7860: 6f 64 65 73 20 7b 48 31 30 32 31 30 7d 20 3c 53  odes {H10210} <S
7870: 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  10700>.** KEYWOR
7880: 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b 20 7b 65  DS: SQLITE_OK {e
7890: 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 72 72 6f  rror code} {erro
78a0: 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57  r codes}.** KEYW
78b0: 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74 20 63 6f  ORDS: {result co
78c0: 64 65 7d 20 7b 72 65 73 75 6c 74 20 63 6f 64 65  de} {result code
78d0: 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 53 51  s}.**.** Many SQ
78e0: 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 72  Lite functions r
78f0: 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
7900: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f   result code fro
7910: 6d 20 74 68 65 20 73 65 74 20 73 68 6f 77 6e 0a  m the set shown.
7920: 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72 64 65 72  ** here in order
7930: 20 74 6f 20 69 6e 64 69 63 61 74 65 73 20 73 75   to indicates su
7940: 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65  ccess or failure
7950: 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65 72 72 6f  ..**.** New erro
7960: 72 20 63 6f 64 65 73 20 6d 61 79 20 62 65 20 61  r codes may be a
7970: 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 76  dded in future v
7980: 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
7990: 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  e..**.** See als
79a0: 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  o: [SQLITE_IOERR
79b0: 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64 65 64  _READ | extended
79c0: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 0a 2a   result codes].*
79d0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
79e0: 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 20 30 20  _OK           0 
79f0: 20 20 2f 2a 20 53 75 63 63 65 73 73 66 75 6c 20    /* Successful 
7a00: 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62 65 67  result */./* beg
7a10: 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f 72 2d  inning-of-error-
7a20: 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  codes */.#define
7a30: 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20 20   SQLITE_ERROR   
7a40: 20 20 20 20 20 31 20 20 20 2f 2a 20 53 51 4c 20       1   /* SQL 
7a50: 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e 67  error or missing
7a60: 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65   database */.#de
7a70: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  fine SQLITE_INTE
7a80: 52 4e 41 4c 20 20 20 20 20 32 20 20 20 2f 2a 20  RNAL     2   /* 
7a90: 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69 63 20 65  Internal logic e
7aa0: 72 72 6f 72 20 69 6e 20 53 51 4c 69 74 65 20 2a  rror in SQLite *
7ab0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7ac0: 5f 50 45 52 4d 20 20 20 20 20 20 20 20 20 33 20  _PERM         3 
7ad0: 20 20 2f 2a 20 41 63 63 65 73 73 20 70 65 72 6d    /* Access perm
7ae0: 69 73 73 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f  ission denied */
7af0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7b00: 41 42 4f 52 54 20 20 20 20 20 20 20 20 34 20 20  ABORT        4  
7b10: 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 72 6f 75   /* Callback rou
7b20: 74 69 6e 65 20 72 65 71 75 65 73 74 65 64 20 61  tine requested a
7b30: 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64 65 66 69  n abort */.#defi
7b40: 6e 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 20  ne SQLITE_BUSY  
7b50: 20 20 20 20 20 20 20 35 20 20 20 2f 2a 20 54 68         5   /* Th
7b60: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
7b70: 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65  is locked */.#de
7b80: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  fine SQLITE_LOCK
7b90: 45 44 20 20 20 20 20 20 20 36 20 20 20 2f 2a 20  ED       6   /* 
7ba0: 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64  A table in the d
7bb0: 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65  atabase is locke
7bc0: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
7bd0: 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20  ITE_NOMEM       
7be0: 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63   7   /* A malloc
7bf0: 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a 23 64 65  () failed */.#de
7c00: 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44  fine SQLITE_READ
7c10: 4f 4e 4c 59 20 20 20 20 20 38 20 20 20 2f 2a 20  ONLY     8   /* 
7c20: 41 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  Attempt to write
7c30: 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61   a readonly data
7c40: 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  base */.#define 
7c50: 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
7c60: 20 20 20 20 39 20 20 20 2f 2a 20 4f 70 65 72 61      9   /* Opera
7c70: 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74 65 64 20  tion terminated 
7c80: 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  by sqlite3_inter
7c90: 72 75 70 74 28 29 2a 2f 0a 23 64 65 66 69 6e 65  rupt()*/.#define
7ca0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 20 20   SQLITE_IOERR   
7cb0: 20 20 20 20 31 30 20 20 20 2f 2a 20 53 6f 6d 65      10   /* Some
7cc0: 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b 20 49 2f   kind of disk I/
7cd0: 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  O error occurred
7ce0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
7cf0: 54 45 5f 43 4f 52 52 55 50 54 20 20 20 20 20 31  TE_CORRUPT     1
7d00: 31 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  1   /* The datab
7d10: 61 73 65 20 64 69 73 6b 20 69 6d 61 67 65 20 69  ase disk image i
7d20: 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a 2f 0a 23  s malformed */.#
7d30: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f  define SQLITE_NO
7d40: 54 46 4f 55 4e 44 20 20 20 20 31 32 20 20 20 2f  TFOUND    12   /
7d50: 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54 61 62 6c  * NOT USED. Tabl
7d60: 65 20 6f 72 20 72 65 63 6f 72 64 20 6e 6f 74 20  e or record not 
7d70: 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65  found */.#define
7d80: 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 20 20 20   SQLITE_FULL    
7d90: 20 20 20 20 31 33 20 20 20 2f 2a 20 49 6e 73 65      13   /* Inse
7da0: 72 74 69 6f 6e 20 66 61 69 6c 65 64 20 62 65 63  rtion failed bec
7db0: 61 75 73 65 20 64 61 74 61 62 61 73 65 20 69 73  ause database is
7dc0: 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e 65   full */.#define
7dd0: 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
7de0: 20 20 20 20 31 34 20 20 20 2f 2a 20 55 6e 61 62      14   /* Unab
7df0: 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 64  le to open the d
7e00: 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
7e10: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50  #define SQLITE_P
7e20: 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35 20 20 20  ROTOCOL    15   
7e30: 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 44 61 74  /* NOT USED. Dat
7e40: 61 62 61 73 65 20 6c 6f 63 6b 20 70 72 6f 74 6f  abase lock proto
7e50: 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65  col error */.#de
7e60: 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4d 50 54  fine SQLITE_EMPT
7e70: 59 20 20 20 20 20 20 20 31 36 20 20 20 2f 2a 20  Y       16   /* 
7e80: 44 61 74 61 62 61 73 65 20 69 73 20 65 6d 70 74  Database is empt
7e90: 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  y */.#define SQL
7ea0: 49 54 45 5f 53 43 48 45 4d 41 20 20 20 20 20 20  ITE_SCHEMA      
7eb0: 31 37 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  17   /* The data
7ec0: 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e  base schema chan
7ed0: 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ged */.#define S
7ee0: 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 20 20 20  QLITE_TOOBIG    
7ef0: 20 20 31 38 20 20 20 2f 2a 20 53 74 72 69 6e 67    18   /* String
7f00: 20 6f 72 20 42 4c 4f 42 20 65 78 63 65 65 64 73   or BLOB exceeds
7f10: 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a 2f 0a 23   size limit */.#
7f20: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
7f30: 4e 53 54 52 41 49 4e 54 20 20 31 39 20 20 20 2f  NSTRAINT  19   /
7f40: 2a 20 41 62 6f 72 74 20 64 75 65 20 74 6f 20 63  * Abort due to c
7f50: 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74  onstraint violat
7f60: 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ion */.#define S
7f70: 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 20  QLITE_MISMATCH  
7f80: 20 20 32 30 20 20 20 2f 2a 20 44 61 74 61 20 74    20   /* Data t
7f90: 79 70 65 20 6d 69 73 6d 61 74 63 68 20 2a 2f 0a  ype mismatch */.
7fa0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
7fb0: 49 53 55 53 45 20 20 20 20 20 20 32 31 20 20 20  ISUSE      21   
7fc0: 2f 2a 20 4c 69 62 72 61 72 79 20 75 73 65 64 20  /* Library used 
7fd0: 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a 2f 0a 23  incorrectly */.#
7fe0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f  define SQLITE_NO
7ff0: 4c 46 53 20 20 20 20 20 20 20 32 32 20 20 20 2f  LFS       22   /
8000: 2a 20 55 73 65 73 20 4f 53 20 66 65 61 74 75 72  * Uses OS featur
8010: 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64  es not supported
8020: 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23 64 65 66   on host */.#def
8030: 69 6e 65 20 53 51 4c 49 54 45 5f 41 55 54 48 20  ine SQLITE_AUTH 
8040: 20 20 20 20 20 20 20 32 33 20 20 20 2f 2a 20 41         23   /* A
8050: 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65 6e  uthorization den
8060: 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ied */.#define S
8070: 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20 20 20 20  QLITE_FORMAT    
8080: 20 20 32 34 20 20 20 2f 2a 20 41 75 78 69 6c 69    24   /* Auxili
8090: 61 72 79 20 64 61 74 61 62 61 73 65 20 66 6f 72  ary database for
80a0: 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65  mat error */.#de
80b0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 41 4e 47  fine SQLITE_RANG
80c0: 45 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a 20  E       25   /* 
80d0: 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  2nd parameter to
80e0: 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 20 6f 75   sqlite3_bind ou
80f0: 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a 23 64  t of range */.#d
8100: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54  efine SQLITE_NOT
8110: 41 44 42 20 20 20 20 20 20 32 36 20 20 20 2f 2a  ADB      26   /*
8120: 20 46 69 6c 65 20 6f 70 65 6e 65 64 20 74 68 61   File opened tha
8130: 74 20 69 73 20 6e 6f 74 20 61 20 64 61 74 61 62  t is not a datab
8140: 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66  ase file */.#def
8150: 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f 57 20 20  ine SQLITE_ROW  
8160: 20 20 20 20 20 20 20 31 30 30 20 20 2f 2a 20 73         100  /* s
8170: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 68 61  qlite3_step() ha
8180: 73 20 61 6e 6f 74 68 65 72 20 72 6f 77 20 72 65  s another row re
8190: 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ady */.#define S
81a0: 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 20 20 20  QLITE_DONE      
81b0: 20 20 31 30 31 20 20 2f 2a 20 73 71 6c 69 74 65    101  /* sqlite
81c0: 33 5f 73 74 65 70 28 29 20 68 61 73 20 66 69 6e  3_step() has fin
81d0: 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67 20  ished executing 
81e0: 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d 65 72 72  */./* end-of-err
81f0: 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a 2f 2a 0a  or-codes */../*.
8200: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 78 74  ** CAPI3REF: Ext
8210: 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64  ended Result Cod
8220: 65 73 20 7b 48 31 30 32 32 30 7d 20 3c 53 31 30  es {H10220} <S10
8230: 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  700>.** KEYWORDS
8240: 3a 20 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f  : {extended erro
8250: 72 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65  r code} {extende
8260: 64 20 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a  d error codes}.*
8270: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74  * KEYWORDS: {ext
8280: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
8290: 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 72 65 73  e} {extended res
82a0: 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a  ult codes}.**.**
82b0: 20 49 6e 20 69 74 73 20 64 65 66 61 75 6c 74 20   In its default 
82c0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 53  configuration, S
82d0: 51 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69 6e  QLite API routin
82e0: 65 73 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66  es return one of
82f0: 20 32 36 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b   26 integer.** [
8300: 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72 65 73 75  SQLITE_OK | resu
8310: 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48 6f 77 65  lt codes].  Howe
8320: 76 65 72 2c 20 65 78 70 65 72 69 65 6e 63 65 20  ver, experience 
8330: 68 61 73 20 73 68 6f 77 6e 20 74 68 61 74 20 6d  has shown that m
8340: 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20  any of.** these 
8350: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65  result codes are
8360: 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67 72 61 69   too coarse-grai
8370: 6e 65 64 2e 20 20 54 68 65 79 20 64 6f 20 6e 6f  ned.  They do no
8380: 74 20 70 72 6f 76 69 64 65 20 61 73 0a 2a 2a 20  t provide as.** 
8390: 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  much information
83a0: 20 61 62 6f 75 74 20 70 72 6f 62 6c 65 6d 73 20   about problems 
83b0: 61 73 20 70 72 6f 67 72 61 6d 6d 65 72 73 20 6d  as programmers m
83c0: 69 67 68 74 20 6c 69 6b 65 2e 20 20 49 6e 20 61  ight like.  In a
83d0: 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a 2a 20 61  n effort to.** a
83e0: 64 64 72 65 73 73 20 74 68 69 73 2c 20 6e 65 77  ddress this, new
83f0: 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  er versions of S
8400: 51 4c 69 74 65 20 28 76 65 72 73 69 6f 6e 20 33  QLite (version 3
8410: 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65 72 29 20  .3.8 and later) 
8420: 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75 70 70 6f  include.** suppo
8430: 72 74 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  rt for additiona
8440: 6c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 74  l result codes t
8450: 68 61 74 20 70 72 6f 76 69 64 65 20 6d 6f 72 65  hat provide more
8460: 20 64 65 74 61 69 6c 65 64 20 69 6e 66 6f 72 6d   detailed inform
8470: 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 65  ation.** about e
8480: 72 72 6f 72 73 2e 20 54 68 65 20 65 78 74 65 6e  rrors. The exten
8490: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
84a0: 20 61 72 65 20 65 6e 61 62 6c 65 64 20 6f 72 20   are enabled or 
84b0: 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f 6e 20 61  disabled.** on a
84c0: 20 70 65 72 20 64 61 74 61 62 61 73 65 20 63 6f   per database co
84d0: 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 20 75  nnection basis u
84e0: 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  sing the.** [sql
84f0: 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
8500: 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d 20 41 50  sult_codes()] AP
8510: 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66  I..**.** Some of
8520: 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 65   the available e
8530: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
8540: 6f 64 65 73 20 61 72 65 20 6c 69 73 74 65 64 20  odes are listed 
8550: 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20 6d 61 79  here..** One may
8560: 20 65 78 70 65 63 74 20 74 68 65 20 6e 75 6d 62   expect the numb
8570: 65 72 20 6f 66 20 65 78 74 65 6e 64 65 64 20 72  er of extended r
8580: 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 6c 6c  esult codes will
8590: 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a 20 6f 76   be expand.** ov
85a0: 65 72 20 74 69 6d 65 2e 20 20 53 6f 66 74 77 61  er time.  Softwa
85b0: 72 65 20 74 68 61 74 20 75 73 65 73 20 65 78 74  re that uses ext
85c0: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
85d0: 65 73 20 73 68 6f 75 6c 64 20 65 78 70 65 63 74  es should expect
85e0: 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65 77 20 72  .** to see new r
85f0: 65 73 75 6c 74 20 63 6f 64 65 73 20 69 6e 20 66  esult codes in f
8600: 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f  uture releases o
8610: 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  f SQLite..**.** 
8620: 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65  The SQLITE_OK re
8630: 73 75 6c 74 20 63 6f 64 65 20 77 69 6c 6c 20 6e  sult code will n
8640: 65 76 65 72 20 62 65 20 65 78 74 65 6e 64 65 64  ever be extended
8650: 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c 77 61 79  .  It will alway
8660: 73 0a 2a 2a 20 62 65 20 65 78 61 63 74 6c 79 20  s.** be exactly 
8670: 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  zero..*/.#define
8680: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45   SQLITE_IOERR_RE
8690: 41 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AD              
86a0: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
86b0: 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (1<<8)).#define 
86c0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
86d0: 52 54 5f 52 45 41 44 20 20 20 20 20 20 20 20 28  RT_READ        (
86e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
86f0: 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53  2<<8)).#define S
8700: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54  QLITE_IOERR_WRIT
8710: 45 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53  E             (S
8720: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 33  QLITE_IOERR | (3
8730: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
8740: 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43  LITE_IOERR_FSYNC
8750: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51               (SQ
8760: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 34 3c  LITE_IOERR | (4<
8770: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8780: 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53  ITE_IOERR_DIR_FS
8790: 59 4e 43 20 20 20 20 20 20 20 20 20 28 53 51 4c  YNC         (SQL
87a0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c 3c  ITE_IOERR | (5<<
87b0: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
87c0: 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54  TE_IOERR_TRUNCAT
87d0: 45 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49  E          (SQLI
87e0: 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c 38  TE_IOERR | (6<<8
87f0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8800: 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 20 20  E_IOERR_FSTAT   
8810: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8820: 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29  E_IOERR | (7<<8)
8830: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8840: 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 20  _IOERR_UNLOCK   
8850: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
8860: 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29 29  _IOERR | (8<<8))
8870: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8880: 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 20 20 20  IOERR_RDLOCK    
8890: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
88a0: 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 29 29 0a  IOERR | (9<<8)).
88b0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
88c0: 4f 45 52 52 5f 44 45 4c 45 54 45 20 20 20 20 20  OERR_DELETE     
88d0: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
88e0: 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 29 29 0a  OERR | (10<<8)).
88f0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8900: 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20 20  OERR_BLOCKED    
8910: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
8920: 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38 29 29 0a  OERR | (11<<8)).
8930: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8940: 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20 20  OERR_NOMEM      
8950: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
8960: 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38 29 29 0a  OERR | (12<<8)).
8970: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8980: 4f 45 52 52 5f 41 43 43 45 53 53 20 20 20 20 20  OERR_ACCESS     
8990: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
89a0: 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38 29 29 0a  OERR | (13<<8)).
89b0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
89c0: 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56  OERR_CHECKRESERV
89d0: 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f 49  EDLOCK (SQLITE_I
89e0: 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38 29 29 0a  OERR | (14<<8)).
89f0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8a00: 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20 20 20 20  OERR_LOCK       
8a10: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
8a20: 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38 29 29 0a  OERR | (15<<8)).
8a30: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8a40: 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20 20 20 20  OERR_CLOSE      
8a50: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
8a60: 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38 29 29 0a  OERR | (16<<8)).
8a70: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8a80: 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 20 20  OERR_DIR_CLOSE  
8a90: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
8aa0: 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38 29 29 0a  OERR | (17<<8)).
8ab0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
8ac0: 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48  OCKED_SHAREDCACH
8ad0: 45 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4c  E      (SQLITE_L
8ae0: 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38 29 20 29  OCKED | (1<<8) )
8af0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
8b00: 3a 20 46 6c 61 67 73 20 46 6f 72 20 46 69 6c 65  : Flags For File
8b10: 20 4f 70 65 6e 20 4f 70 65 72 61 74 69 6f 6e 73   Open Operations
8b20: 20 7b 48 31 30 32 33 30 7d 20 3c 48 31 31 31 32   {H10230} <H1112
8b30: 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a 2a 0a 2a  0> <H12700>.**.*
8b40: 2a 20 54 68 65 73 65 20 62 69 74 20 76 61 6c 75  * These bit valu
8b50: 65 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20  es are intended 
8b60: 66 6f 72 20 75 73 65 20 69 6e 20 74 68 65 0a 2a  for use in the.*
8b70: 2a 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  * 3rd parameter 
8b80: 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  to the [sqlite3_
8b90: 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72  open_v2()] inter
8ba0: 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69 6e 20 74  face and.** in t
8bb0: 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72  he 4th parameter
8bc0: 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d 65   to the xOpen me
8bd0: 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 5b  thod of the.** [
8be0: 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a  sqlite3_vfs] obj
8bf0: 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ect..*/.#define 
8c00: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
8c10: 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 30 78 30  ONLY         0x0
8c20: 30 30 30 30 30 30 31 20 20 2f 2a 20 4f 6b 20 66  0000001  /* Ok f
8c30: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  or sqlite3_open_
8c40: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  v2() */.#define 
8c50: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
8c60: 57 52 49 54 45 20 20 20 20 20 20 20 20 30 78 30  WRITE        0x0
8c70: 30 30 30 30 30 30 32 20 20 2f 2a 20 4f 6b 20 66  0000002  /* Ok f
8c80: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  or sqlite3_open_
8c90: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  v2() */.#define 
8ca0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
8cb0: 54 45 20 20 20 20 20 20 20 20 20 20 20 30 78 30  TE           0x0
8cc0: 30 30 30 30 30 30 34 20 20 2f 2a 20 4f 6b 20 66  0000004  /* Ok f
8cd0: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  or sqlite3_open_
8ce0: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  v2() */.#define 
8cf0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
8d00: 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20 30 78 30  TEONCLOSE    0x0
8d10: 30 30 30 30 30 30 38 20 20 2f 2a 20 56 46 53 20  0000008  /* VFS 
8d20: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
8d30: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
8d40: 55 53 49 56 45 20 20 20 20 20 20 20 20 30 78 30  USIVE        0x0
8d50: 30 30 30 30 30 31 30 20 20 2f 2a 20 56 46 53 20  0000010  /* VFS 
8d60: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
8d70: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
8d80: 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 78 30  _DB          0x0
8d90: 30 30 30 30 31 30 30 20 20 2f 2a 20 56 46 53 20  0000100  /* VFS 
8da0: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
8db0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
8dc0: 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 78 30  _DB          0x0
8dd0: 30 30 30 30 32 30 30 20 20 2f 2a 20 56 46 53 20  0000200  /* VFS 
8de0: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
8df0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e  SQLITE_OPEN_TRAN
8e00: 53 49 45 4e 54 5f 44 42 20 20 20 20 20 30 78 30  SIENT_DB     0x0
8e10: 30 30 30 30 34 30 30 20 20 2f 2a 20 56 46 53 20  0000400  /* VFS 
8e20: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
8e30: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
8e40: 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30  _JOURNAL     0x0
8e50: 30 30 30 30 38 30 30 20 20 2f 2a 20 56 46 53 20  0000800  /* VFS 
8e60: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
8e70: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
8e80: 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30  _JOURNAL     0x0
8e90: 30 30 30 31 30 30 30 20 20 2f 2a 20 56 46 53 20  0001000  /* VFS 
8ea0: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
8eb0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a  SQLITE_OPEN_SUBJ
8ec0: 4f 55 52 4e 41 4c 20 20 20 20 20 20 20 30 78 30  OURNAL       0x0
8ed0: 30 30 30 32 30 30 30 20 20 2f 2a 20 56 46 53 20  0002000  /* VFS 
8ee0: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
8ef0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54  SQLITE_OPEN_MAST
8f00: 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20 30 78 30  ER_JOURNAL   0x0
8f10: 30 30 30 34 30 30 30 20 20 2f 2a 20 56 46 53 20  0004000  /* VFS 
8f20: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
8f30: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55  SQLITE_OPEN_NOMU
8f40: 54 45 58 20 20 20 20 20 20 20 20 20 20 30 78 30  TEX          0x0
8f50: 30 30 30 38 30 30 30 20 20 2f 2a 20 4f 6b 20 66  0008000  /* Ok f
8f60: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  or sqlite3_open_
8f70: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  v2() */.#define 
8f80: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c  SQLITE_OPEN_FULL
8f90: 4d 55 54 45 58 20 20 20 20 20 20 20 20 30 78 30  MUTEX        0x0
8fa0: 30 30 31 30 30 30 30 20 20 2f 2a 20 4f 6b 20 66  0010000  /* Ok f
8fb0: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  or sqlite3_open_
8fc0: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  v2() */.#define 
8fd0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52  SQLITE_OPEN_SHAR
8fe0: 45 44 43 41 43 48 45 20 20 20 20 20 20 30 78 30  EDCACHE      0x0
8ff0: 30 30 32 30 30 30 30 20 20 2f 2a 20 4f 6b 20 66  0020000  /* Ok f
9000: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  or sqlite3_open_
9010: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  v2() */.#define 
9020: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56  SQLITE_OPEN_PRIV
9030: 41 54 45 43 41 43 48 45 20 20 20 20 20 30 78 30  ATECACHE     0x0
9040: 30 30 34 30 30 30 30 20 20 2f 2a 20 4f 6b 20 66  0040000  /* Ok f
9050: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  or sqlite3_open_
9060: 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  v2() */../*.** C
9070: 41 50 49 33 52 45 46 3a 20 44 65 76 69 63 65 20  API3REF: Device 
9080: 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  Characteristics 
9090: 7b 48 31 30 32 34 30 7d 20 3c 48 31 31 31 32 30  {H10240} <H11120
90a0: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 76  >.**.** The xDev
90b0: 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 73 20  iceCapabilities 
90c0: 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 5b 73  method of the [s
90d0: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
90e0: 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 74  s].** object ret
90f0: 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20  urns an integer 
9100: 77 68 69 63 68 20 69 73 20 61 20 76 65 63 74 6f  which is a vecto
9110: 72 20 6f 66 20 74 68 65 20 74 68 65 73 65 0a 2a  r of the these.*
9120: 2a 20 62 69 74 20 76 61 6c 75 65 73 20 65 78 70  * bit values exp
9130: 72 65 73 73 69 6e 67 20 49 2f 4f 20 63 68 61 72  ressing I/O char
9140: 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74  acteristics of t
9150: 68 65 20 6d 61 73 73 20 73 74 6f 72 61 67 65 0a  he mass storage.
9160: 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 68  ** device that h
9170: 6f 6c 64 73 20 74 68 65 20 66 69 6c 65 20 74 68  olds the file th
9180: 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  at the [sqlite3_
9190: 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 72  io_methods].** r
91a0: 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20  efers to..**.** 
91b0: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  The SQLITE_IOCAP
91c0: 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 79  _ATOMIC property
91d0: 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c 20   means that all 
91e0: 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79  writes of.** any
91f0: 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 63   size are atomic
9200: 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f  .  The SQLITE_IO
9210: 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61  CAP_ATOMICnnn va
9220: 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61  lues.** mean tha
9230: 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f 63  t writes of bloc
9240: 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e 20  ks that are nnn 
9250: 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e  bytes in size an
9260: 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 64  d.** are aligned
9270: 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20 77   to an address w
9280: 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65 67  hich is an integ
9290: 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a  er multiple of.*
92a0: 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63  * nnn are atomic
92b0: 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f  .  The SQLITE_IO
92c0: 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20  CAP_SAFE_APPEND 
92d0: 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74  value means.** t
92e0: 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 69 73  hat when data is
92f0: 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 66   appended to a f
9300: 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69 73  ile, the data is
9310: 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72   appended.** fir
9320: 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65  st then the size
9330: 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   of the file is 
9340: 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 20  extended, never 
9350: 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79  the other.** way
9360: 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 51   around.  The SQ
9370: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45  LITE_IOCAP_SEQUE
9380: 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20 6d  NTIAL property m
9390: 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66  eans that.** inf
93a0: 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 74  ormation is writ
93b0: 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 74  ten to disk in t
93c0: 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61 73  he same order as
93d0: 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72   calls.** to xWr
93e0: 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ite()..*/.#defin
93f0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
9400: 54 4f 4d 49 43 20 20 20 20 20 20 20 20 20 20 30  TOMIC          0
9410: 78 30 30 30 30 30 30 30 31 0a 23 64 65 66 69 6e  x00000001.#defin
9420: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
9430: 54 4f 4d 49 43 35 31 32 20 20 20 20 20 20 20 30  TOMIC512       0
9440: 78 30 30 30 30 30 30 30 32 0a 23 64 65 66 69 6e  x00000002.#defin
9450: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
9460: 54 4f 4d 49 43 31 4b 20 20 20 20 20 20 20 20 30  TOMIC1K        0
9470: 78 30 30 30 30 30 30 30 34 0a 23 64 65 66 69 6e  x00000004.#defin
9480: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
9490: 54 4f 4d 49 43 32 4b 20 20 20 20 20 20 20 20 30  TOMIC2K        0
94a0: 78 30 30 30 30 30 30 30 38 0a 23 64 65 66 69 6e  x00000008.#defin
94b0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
94c0: 54 4f 4d 49 43 34 4b 20 20 20 20 20 20 20 20 30  TOMIC4K        0
94d0: 78 30 30 30 30 30 30 31 30 0a 23 64 65 66 69 6e  x00000010.#defin
94e0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
94f0: 54 4f 4d 49 43 38 4b 20 20 20 20 20 20 20 20 30  TOMIC8K        0
9500: 78 30 30 30 30 30 30 32 30 0a 23 64 65 66 69 6e  x00000020.#defin
9510: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
9520: 54 4f 4d 49 43 31 36 4b 20 20 20 20 20 20 20 30  TOMIC16K       0
9530: 78 30 30 30 30 30 30 34 30 0a 23 64 65 66 69 6e  x00000040.#defin
9540: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
9550: 54 4f 4d 49 43 33 32 4b 20 20 20 20 20 20 20 30  TOMIC32K       0
9560: 78 30 30 30 30 30 30 38 30 0a 23 64 65 66 69 6e  x00000080.#defin
9570: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
9580: 54 4f 4d 49 43 36 34 4b 20 20 20 20 20 20 20 30  TOMIC64K       0
9590: 78 30 30 30 30 30 31 30 30 0a 23 64 65 66 69 6e  x00000100.#defin
95a0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  e SQLITE_IOCAP_S
95b0: 41 46 45 5f 41 50 50 45 4e 44 20 20 20 20 20 30  AFE_APPEND     0
95c0: 78 30 30 30 30 30 32 30 30 0a 23 64 65 66 69 6e  x00000200.#defin
95d0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  e SQLITE_IOCAP_S
95e0: 45 51 55 45 4e 54 49 41 4c 20 20 20 20 20 20 30  EQUENTIAL      0
95f0: 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a  x00000400../*.**
9600: 20 43 41 50 49 33 52 45 46 3a 20 46 69 6c 65 20   CAPI3REF: File 
9610: 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c 73 20 7b  Locking Levels {
9620: 48 31 30 32 35 30 7d 20 3c 48 31 31 31 32 30 3e  H10250} <H11120>
9630: 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a 2a 2a 20   <H11310>.**.** 
9640: 53 51 4c 69 74 65 20 75 73 65 73 20 6f 6e 65 20  SQLite uses one 
9650: 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72  of these integer
9660: 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 73   values as the s
9670: 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e  econd.** argumen
9680: 74 20 74 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61  t to calls it ma
9690: 6b 65 73 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b  kes to the xLock
96a0: 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29  () and xUnlock()
96b0: 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61   methods.** of a
96c0: 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  n [sqlite3_io_me
96d0: 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a  thods] object..*
96e0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
96f0: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20  _LOCK_NONE      
9700: 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51      0.#define SQ
9710: 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44  LITE_LOCK_SHARED
9720: 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
9730: 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45  e SQLITE_LOCK_RE
9740: 53 45 52 56 45 44 20 20 20 20 20 20 32 0a 23 64  SERVED      2.#d
9750: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43  efine SQLITE_LOC
9760: 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20 20 20 20  K_PENDING       
9770: 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  3.#define SQLITE
9780: 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 20  _LOCK_EXCLUSIVE 
9790: 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50      4../*.** CAP
97a0: 49 33 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69  I3REF: Synchroni
97b0: 7a 61 74 69 6f 6e 20 54 79 70 65 20 46 6c 61 67  zation Type Flag
97c0: 73 20 7b 48 31 30 32 36 30 7d 20 3c 48 31 31 31  s {H10260} <H111
97d0: 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53  20>.**.** When S
97e0: 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68  QLite invokes th
97f0: 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64  e xSync() method
9800: 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74   of an.** [sqlit
9810: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f  e3_io_methods] o
9820: 62 6a 65 63 74 20 69 74 20 75 73 65 73 20 61 20  bject it uses a 
9830: 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a  combination of.*
9840: 2a 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20  * these integer 
9850: 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 65  values as the se
9860: 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  cond argument..*
9870: 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 53 51  *.** When the SQ
9880: 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e  LITE_SYNC_DATAON
9890: 4c 59 20 66 6c 61 67 20 69 73 20 75 73 65 64 2c  LY flag is used,
98a0: 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
98b0: 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70 65 72 61  he.** sync opera
98c0: 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65 64 73 20  tion only needs 
98d0: 74 6f 20 66 6c 75 73 68 20 64 61 74 61 20 74 6f  to flush data to
98e0: 20 6d 61 73 73 20 73 74 6f 72 61 67 65 2e 20 20   mass storage.  
98f0: 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61  Inode.** informa
9900: 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74 20 62 65  tion need not be
9910: 20 66 6c 75 73 68 65 64 2e 20 49 66 20 74 68 65   flushed. If the
9920: 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73   lower four bits
9930: 20 6f 66 20 74 68 65 20 66 6c 61 67 0a 2a 2a 20   of the flag.** 
9940: 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53 59 4e  equal SQLITE_SYN
9950: 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61 74 20 6d  C_NORMAL, that m
9960: 65 61 6e 73 20 74 6f 20 75 73 65 20 6e 6f 72 6d  eans to use norm
9970: 61 6c 20 66 73 79 6e 63 28 29 20 73 65 6d 61 6e  al fsync() seman
9980: 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20  tics..** If the 
9990: 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73 20  lower four bits 
99a0: 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53 59 4e  equal SQLITE_SYN
99b0: 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20 6d 65 61  C_FULL, that mea
99c0: 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20 4d 61 63  ns.** to use Mac
99d0: 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c 6c   OS X style full
99e0: 73 79 6e 63 20 69 6e 73 74 65 61 64 20 6f 66 20  sync instead of 
99f0: 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23 64 65 66  fsync()..*/.#def
9a00: 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  ine SQLITE_SYNC_
9a10: 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20 20 30 78  NORMAL        0x
9a20: 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51  00002.#define SQ
9a30: 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 20  LITE_SYNC_FULL  
9a40: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 33 0a          0x00003.
9a50: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
9a60: 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 20 20 20  YNC_DATAONLY    
9a70: 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0a 2a 2a    0x00010../*.**
9a80: 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e   CAPI3REF: OS In
9a90: 74 65 72 66 61 63 65 20 4f 70 65 6e 20 46 69 6c  terface Open Fil
9aa0: 65 20 48 61 6e 64 6c 65 20 7b 48 31 31 31 31 30  e Handle {H11110
9ab0: 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a  } <S20110>.**.**
9ac0: 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c   An [sqlite3_fil
9ad0: 65 5d 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73  e] object repres
9ae0: 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c  ents an open fil
9af0: 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 5b 73 71  e in the .** [sq
9b00: 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f 53 20 69  lite3_vfs | OS i
9b10: 6e 74 65 72 66 61 63 65 20 6c 61 79 65 72 5d 2e  nterface layer].
9b20: 20 20 49 6e 64 69 76 69 64 75 61 6c 20 4f 53 20    Individual OS 
9b30: 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 69 6d 70  interface.** imp
9b40: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c  lementations wil
9b50: 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 73 75 62  l.** want to sub
9b60: 63 6c 61 73 73 20 74 68 69 73 20 6f 62 6a 65 63  class this objec
9b70: 74 20 62 79 20 61 70 70 65 6e 64 69 6e 67 20 61  t by appending a
9b80: 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
9b90: 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72 20 6f 77  .** for their ow
9ba0: 6e 20 75 73 65 2e 20 20 54 68 65 20 70 4d 65 74  n use.  The pMet
9bb0: 68 6f 64 73 20 65 6e 74 72 79 20 69 73 20 61 20  hods entry is a 
9bc0: 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a  pointer to an.**
9bd0: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
9be0: 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 74 68 61  hods] object tha
9bf0: 74 20 64 65 66 69 6e 65 73 20 6d 65 74 68 6f 64  t defines method
9c00: 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 69 6e 67  s for performing
9c10: 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61 74 69 6f  .** I/O operatio
9c20: 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65 6e 20 66  ns on the open f
9c30: 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ile..*/.typedef 
9c40: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 66  struct sqlite3_f
9c50: 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ile sqlite3_file
9c60: 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ;.struct sqlite3
9c70: 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20  _file {.  const 
9c80: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
9c90: 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68  o_methods *pMeth
9ca0: 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68 6f 64 73  ods;  /* Methods
9cb0: 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20 66 69 6c   for an open fil
9cc0: 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  e */.};../*.** C
9cd0: 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65  API3REF: OS Inte
9ce0: 72 66 61 63 65 20 46 69 6c 65 20 56 69 72 74 75  rface File Virtu
9cf0: 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63  al Methods Objec
9d00: 74 20 7b 48 31 31 31 32 30 7d 20 3c 53 32 30 31  t {H11120} <S201
9d10: 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20  10>.**.** Every 
9d20: 66 69 6c 65 20 6f 70 65 6e 65 64 20 62 79 20 74  file opened by t
9d30: 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  he [sqlite3_vfs]
9d40: 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 70 6f   xOpen method po
9d50: 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a 20 5b 73  pulates an.** [s
9d60: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a  qlite3_file] obj
9d70: 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65 20 63 6f  ect (or, more co
9d80: 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62 63 6c 61  mmonly, a subcla
9d90: 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71  ss of the.** [sq
9da0: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65  lite3_file] obje
9db0: 63 74 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74  ct) with a point
9dc0: 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63  er to an instanc
9dd0: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
9de0: 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74  ..** This object
9df0: 20 64 65 66 69 6e 65 73 20 74 68 65 20 6d 65 74   defines the met
9e00: 68 6f 64 73 20 75 73 65 64 20 74 6f 20 70 65 72  hods used to per
9e10: 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 6f 70 65  form various ope
9e20: 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67 61 69 6e  rations.** again
9e30: 73 74 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65  st the open file
9e40: 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20   represented by 
9e50: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  the [sqlite3_fil
9e60: 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a  e] object..**.**
9e70: 20 49 66 20 74 68 65 20 78 4f 70 65 6e 20 6d 65   If the xOpen me
9e80: 74 68 6f 64 20 73 65 74 73 20 74 68 65 20 73 71  thod sets the sq
9e90: 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68  lite3_file.pMeth
9ea0: 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a 2a 2a 20  ods element .** 
9eb0: 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f  to a non-NULL po
9ec0: 69 6e 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20  inter, then the 
9ed0: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
9ee0: 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  ds.xClose method
9ef0: 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b  .** may be invok
9f00: 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 20 78  ed even if the x
9f10: 4f 70 65 6e 20 72 65 70 6f 72 74 65 64 20 74 68  Open reported th
9f20: 61 74 20 69 74 20 66 61 69 6c 65 64 2e 20 20 54  at it failed.  T
9f30: 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61 79 20 74  he.** only way t
9f40: 6f 20 70 72 65 76 65 6e 74 20 61 20 63 61 6c 6c  o prevent a call
9f50: 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f 6c 6c 6f   to xClose follo
9f60: 77 69 6e 67 20 61 20 66 61 69 6c 65 64 20 78 4f  wing a failed xO
9f70: 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72 20 74 68  pen.** is for th
9f80: 65 20 78 4f 70 65 6e 20 74 6f 20 73 65 74 20 74  e xOpen to set t
9f90: 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e  he sqlite3_file.
9fa0: 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d 65 6e 74  pMethods element
9fb0: 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   to NULL..**.** 
9fc0: 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65  The flags argume
9fd0: 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d 61 79 20  nt to xSync may 
9fe0: 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54  be one of [SQLIT
9ff0: 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 5d 20 6f  E_SYNC_NORMAL] o
a000: 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 59 4e  r.** [SQLITE_SYN
a010: 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65 20 66 69  C_FULL].  The fi
a020: 72 73 74 20 63 68 6f 69 63 65 20 69 73 20 74 68  rst choice is th
a030: 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29  e normal fsync()
a040: 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
a050: 63 68 6f 69 63 65 20 69 73 20 61 20 4d 61 63 20  choice is a Mac 
a060: 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c 6c 73  OS X style fulls
a070: 79 6e 63 2e 20 20 54 68 65 20 5b 53 51 4c 49 54  ync.  The [SQLIT
a080: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 5d  E_SYNC_DATAONLY]
a090: 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20 62 65 20  .** flag may be 
a0a0: 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e 64 69 63  ORed in to indic
a0b0: 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 20 74 68  ate that only th
a0c0: 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 66 69  e data of the fi
a0d0: 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74 20 69 74  le.** and not it
a0e0: 73 20 69 6e 6f 64 65 20 6e 65 65 64 73 20 74 6f  s inode needs to
a0f0: 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a   be synced..**.*
a100: 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 61  * The integer va
a110: 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b 28 29 20  lues to xLock() 
a120: 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 61 72  and xUnlock() ar
a130: 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c 75 6c 3e  e one of.** <ul>
a140: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
a150: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a 2a 2a 20  _LOCK_NONE],.** 
a160: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
a170: 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 3c 6c  K_SHARED],.** <l
a180: 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  i> [SQLITE_LOCK_
a190: 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a 20 3c 6c  RESERVED],.** <l
a1a0: 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  i> [SQLITE_LOCK_
a1b0: 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a 2a 2a 20  PENDING], or.** 
a1c0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
a1d0: 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e 0a 2a 2a  K_EXCLUSIVE]..**
a1e0: 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f 63 6b 28   </ul>.** xLock(
a1f0: 29 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 20  ) increases the 
a200: 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b 28 29 20  lock. xUnlock() 
a210: 64 65 63 72 65 61 73 65 73 20 74 68 65 20 6c 6f  decreases the lo
a220: 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43 68 65 63  ck..** The xChec
a230: 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 20  kReservedLock() 
a240: 6d 65 74 68 6f 64 20 63 68 65 63 6b 73 20 77 68  method checks wh
a250: 65 74 68 65 72 20 61 6e 79 20 64 61 74 61 62 61  ether any databa
a260: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 0a 2a  se connection,.*
a270: 2a 20 65 69 74 68 65 72 20 69 6e 20 74 68 69 73  * either in this
a280: 20 70 72 6f 63 65 73 73 20 6f 72 20 69 6e 20 73   process or in s
a290: 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73  ome other proces
a2a0: 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20  s, is holding a 
a2b0: 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20 50 45 4e  RESERVED,.** PEN
a2c0: 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c 55 53 49  DING, or EXCLUSI
a2d0: 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66  VE lock on the f
a2e0: 69 6c 65 2e 20 20 49 74 20 72 65 74 75 72 6e 73  ile.  It returns
a2f0: 20 74 72 75 65 0a 2a 2a 20 69 66 20 73 75 63 68   true.** if such
a300: 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 61   a lock exists a
a310: 6e 64 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69  nd false otherwi
a320: 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 46  se..**.** The xF
a330: 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 65 74  ileControl() met
a340: 68 6f 64 20 69 73 20 61 20 67 65 6e 65 72 69 63  hod is a generic
a350: 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20   interface that 
a360: 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d 0a 2a 2a  allows custom.**
a370: 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74   VFS implementat
a380: 69 6f 6e 73 20 74 6f 20 64 69 72 65 63 74 6c 79  ions to directly
a390: 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f 70 65 6e   control an open
a3a0: 20 66 69 6c 65 20 75 73 69 6e 67 20 74 68 65 0a   file using the.
a3b0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65  ** [sqlite3_file
a3c0: 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65  _control()] inte
a3d0: 72 66 61 63 65 2e 20 20 54 68 65 20 73 65 63 6f  rface.  The seco
a3e0: 6e 64 20 22 6f 70 22 20 61 72 67 75 6d 65 6e 74  nd "op" argument
a3f0: 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65   is an.** intege
a400: 72 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 74  r opcode.  The t
a410: 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73  hird argument is
a420: 20 61 20 67 65 6e 65 72 69 63 20 70 6f 69 6e 74   a generic point
a430: 65 72 20 69 6e 74 65 6e 64 65 64 20 74 6f 0a 2a  er intended to.*
a440: 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72  * point to a str
a450: 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79 20  ucture that may 
a460: 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d 65 6e 74  contain argument
a470: 73 20 6f 72 20 73 70 61 63 65 20 69 6e 20 77 68  s or space in wh
a480: 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20  ich to.** write 
a490: 72 65 74 75 72 6e 20 76 61 6c 75 65 73 2e 20 20  return values.  
a4a0: 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 73 20 66  Potential uses f
a4b0: 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  or xFileControl(
a4c0: 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 66 75  ) might be.** fu
a4d0: 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e 61 62 6c  nctions to enabl
a4e0: 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f 63 6b 73  e blocking locks
a4f0: 20 77 69 74 68 20 74 69 6d 65 6f 75 74 73 2c 20   with timeouts, 
a500: 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a  to change the.**
a510: 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
a520: 79 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 74  y (for example t
a530: 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c 65 20 6c  o use dot-file l
a540: 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 75 69 72  ocks), to inquir
a550: 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 73  e.** about the s
a560: 74 61 74 75 73 20 6f 66 20 61 20 6c 6f 63 6b 2c  tatus of a lock,
a570: 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 73 74 61   or to break sta
a580: 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 53  le locks.  The S
a590: 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 72 65  QLite.** core re
a5a0: 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 63 6f 64  serves all opcod
a5b0: 65 73 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30  es less than 100
a5c0: 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 75 73 65   for its own use
a5d0: 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 45 5f 46  ..** A [SQLITE_F
a5e0: 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 7c  CNTL_LOCKSTATE |
a5f0: 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f 64 65 73   list of opcodes
a600: 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30 20  ] less than 100 
a610: 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a  is available..**
a620: 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   Applications th
a630: 61 74 20 64 65 66 69 6e 65 20 61 20 63 75 73 74  at define a cust
a640: 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  om xFileControl 
a650: 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 75 73  method should us
a660: 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 67 72 65  e opcodes.** gre
a670: 61 74 65 72 20 74 68 61 6e 20 31 30 30 20 74 6f  ater than 100 to
a680: 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69 63 74 73   avoid conflicts
a690: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 65 63  ..**.** The xSec
a6a0: 74 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f 64  torSize() method
a6b0: 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 65 63   returns the sec
a6c0: 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 0a  tor size of the.
a6d0: 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 75  ** device that u
a6e0: 6e 64 65 72 6c 69 65 73 20 74 68 65 20 66 69 6c  nderlies the fil
a6f0: 65 2e 20 20 54 68 65 20 73 65 63 74 6f 72 20 73  e.  The sector s
a700: 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 6d 69  ize is the.** mi
a710: 6e 69 6d 75 6d 20 77 72 69 74 65 20 74 68 61 74  nimum write that
a720: 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65   can be performe
a730: 64 20 77 69 74 68 6f 75 74 20 64 69 73 74 75 72  d without distur
a740: 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20 62 79  bing.** other by
a750: 74 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e  tes in the file.
a760: 20 20 54 68 65 20 78 44 65 76 69 63 65 43 68 61    The xDeviceCha
a770: 72 61 63 74 65 72 69 73 74 69 63 73 28 29 0a 2a  racteristics().*
a780: 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73  * method returns
a790: 20 61 20 62 69 74 20 76 65 63 74 6f 72 20 64 65   a bit vector de
a7a0: 73 63 72 69 62 69 6e 67 20 62 65 68 61 76 69 6f  scribing behavio
a7b0: 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 75 6e 64  rs of the.** und
a7c0: 65 72 6c 79 69 6e 67 20 64 65 76 69 63 65 3a 0a  erlying device:.
a7d0: 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
a7e0: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
a7f0: 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c 6c 69 3e  _ATOMIC].** <li>
a800: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41   [SQLITE_IOCAP_A
a810: 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 3c 6c 69  TOMIC512].** <li
a820: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
a830: 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 3c 6c 69  ATOMIC1K].** <li
a840: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
a850: 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20 3c 6c 69  ATOMIC2K].** <li
a860: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
a870: 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 3c 6c 69  ATOMIC4K].** <li
a880: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
a890: 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 3c 6c 69  ATOMIC8K].** <li
a8a0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
a8b0: 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a 20 3c 6c  ATOMIC16K].** <l
a8c0: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
a8d0: 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a 2a 20 3c  _ATOMIC32K].** <
a8e0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
a8f0: 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a 2a 2a 20  P_ATOMIC64K].** 
a900: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a910: 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 5d 0a  AP_SAFE_APPEND].
a920: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a930: 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c  IOCAP_SEQUENTIAL
a940: 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a  ].** </ul>.**.**
a950: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41   The SQLITE_IOCA
a960: 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74  P_ATOMIC propert
a970: 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c  y means that all
a980: 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e   writes of.** an
a990: 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69  y size are atomi
a9a0: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49  c.  The SQLITE_I
a9b0: 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76  OCAP_ATOMICnnn v
a9c0: 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68  alues.** mean th
a9d0: 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f  at writes of blo
a9e0: 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e  cks that are nnn
a9f0: 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61   bytes in size a
aa00: 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65  nd.** are aligne
aa10: 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20  d to an address 
aa20: 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65  which is an inte
aa30: 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a  ger multiple of.
aa40: 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69  ** nnn are atomi
aa50: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49  c.  The SQLITE_I
aa60: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
aa70: 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20   value means.** 
aa80: 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 69  that when data i
aa90: 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20  s appended to a 
aaa0: 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69  file, the data i
aab0: 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69  s appended.** fi
aac0: 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a  rst then the siz
aad0: 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73  e of the file is
aae0: 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72   extended, never
aaf0: 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61   the other.** wa
ab00: 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53  y around.  The S
ab10: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55  QLITE_IOCAP_SEQU
ab20: 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20  ENTIAL property 
ab30: 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e  means that.** in
ab40: 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69  formation is wri
ab50: 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20  tten to disk in 
ab60: 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61  the same order a
ab70: 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57  s calls.** to xW
ab80: 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66  rite()..**.** If
ab90: 20 78 52 65 61 64 28 29 20 72 65 74 75 72 6e 73   xRead() returns
aba0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48   SQLITE_IOERR_SH
abb0: 4f 52 54 5f 52 45 41 44 20 69 74 20 6d 75 73 74  ORT_READ it must
abc0: 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a 20 69 6e   also fill.** in
abd0: 20 74 68 65 20 75 6e 72 65 61 64 20 70 6f 72 74   the unread port
abe0: 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 75 66 66  ions of the buff
abf0: 65 72 20 77 69 74 68 20 7a 65 72 6f 73 2e 20 20  er with zeros.  
ac00: 41 20 56 46 53 20 74 68 61 74 0a 2a 2a 20 66 61  A VFS that.** fa
ac10: 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c  ils to zero-fill
ac20: 20 73 68 6f 72 74 20 72 65 61 64 73 20 6d 69 67   short reads mig
ac30: 68 74 20 73 65 65 6d 20 74 6f 20 77 6f 72 6b 2e  ht seem to work.
ac40: 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 61    However,.** fa
ac50: 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f 2d 66 69  ilure to zero-fi
ac60: 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 77  ll short reads w
ac70: 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 6c  ill eventually l
ac80: 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74 61 62 61  ead to.** databa
ac90: 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a  se corruption..*
aca0: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
acb0: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
acc0: 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  ods sqlite3_io_m
acd0: 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73  ethods;.struct s
ace0: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
acf0: 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 73 69  s {.  int iVersi
ad00: 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f  on;.  int (*xClo
ad10: 73 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  se)(sqlite3_file
ad20: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 65 61  *);.  int (*xRea
ad30: 64 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  d)(sqlite3_file*
ad40: 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d  , void*, int iAm
ad50: 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  t, sqlite3_int64
ad60: 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28   iOfst);.  int (
ad70: 2a 78 57 72 69 74 65 29 28 73 71 6c 69 74 65 33  *xWrite)(sqlite3
ad80: 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f  _file*, const vo
ad90: 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73  id*, int iAmt, s
ada0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66  qlite3_int64 iOf
adb0: 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 54 72  st);.  int (*xTr
adc0: 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f  uncate)(sqlite3_
add0: 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f 69  file*, sqlite3_i
ade0: 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 20 69 6e  nt64 size);.  in
adf0: 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74  t (*xSync)(sqlit
ae00: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 66 6c  e3_file*, int fl
ae10: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46  ags);.  int (*xF
ae20: 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 74 65 33  ileSize)(sqlite3
ae30: 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f  _file*, sqlite3_
ae40: 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 3b 0a 20  int64 *pSize);. 
ae50: 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 28 73 71   int (*xLock)(sq
ae60: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
ae70: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 6e 6c 6f  );.  int (*xUnlo
ae80: 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck)(sqlite3_file
ae90: 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  *, int);.  int (
aea0: 2a 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  *xCheckReservedL
aeb0: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock)(sqlite3_fil
aec0: 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  e*, int *pResOut
aed0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c 65  );.  int (*xFile
aee0: 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74 65 33  Control)(sqlite3
aef0: 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f 70 2c 20  _file*, int op, 
af00: 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a 20 20 69  void *pArg);.  i
af10: 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a 65  nt (*xSectorSize
af20: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  )(sqlite3_file*)
af30: 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 76 69 63  ;.  int (*xDevic
af40: 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
af50: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  )(sqlite3_file*)
af60: 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 6f 6e 61  ;.  /* Additiona
af70: 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 20 62 65  l methods may be
af80: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65   added in future
af90: 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a 7d 3b 0a   releases */.};.
afa0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
afb0: 20 53 74 61 6e 64 61 72 64 20 46 69 6c 65 20 43   Standard File C
afc0: 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 73 20 7b  ontrol Opcodes {
afd0: 48 31 31 33 31 30 7d 20 3c 53 33 30 38 30 30 3e  H11310} <S30800>
afe0: 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74  .**.** These int
aff0: 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 61  eger constants a
b000: 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 74  re opcodes for t
b010: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  he xFileControl 
b020: 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65  method.** of the
b030: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
b040: 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 6e 64  hods] object and
b050: 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65   for the [sqlite
b060: 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29  3_file_control()
b070: 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a  ].** interface..
b080: 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54  **.** The [SQLIT
b090: 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
b0a0: 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65  E] opcode is use
b0b0: 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e  d for debugging.
b0c0: 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 6f 64 65    This.** opcode
b0d0: 20 63 61 75 73 65 73 20 74 68 65 20 78 46 69 6c   causes the xFil
b0e0: 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20  eControl method 
b0f0: 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72  to write the cur
b100: 72 65 6e 74 20 73 74 61 74 65 20 6f 66 0a 2a 2a  rent state of.**
b110: 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e 65 20 6f   the lock (one o
b120: 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e  f [SQLITE_LOCK_N
b130: 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f  ONE], [SQLITE_LO
b140: 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 5b  CK_SHARED],.** [
b150: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45  SQLITE_LOCK_RESE
b160: 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c  RVED], [SQLITE_L
b170: 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72  OCK_PENDING], or
b180: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58   [SQLITE_LOCK_EX
b190: 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 69 6e 74  CLUSIVE]).** int
b1a0: 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  o an integer tha
b1b0: 74 20 74 68 65 20 70 41 72 67 20 61 72 67 75 6d  t the pArg argum
b1c0: 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 54  ent points to. T
b1d0: 68 69 73 20 63 61 70 61 62 69 6c 69 74 79 0a 2a  his capability.*
b1e0: 2a 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67  * is used during
b1f0: 20 74 65 73 74 69 6e 67 20 61 6e 64 20 6f 6e 6c   testing and onl
b200: 79 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 75  y needs to be su
b210: 70 70 6f 72 74 65 64 20 77 68 65 6e 20 53 51 4c  pported when SQL
b220: 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 73 20 64  ITE_TEST.** is d
b230: 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64 65 66 69  efined..*/.#defi
b240: 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  ne SQLITE_FCNTL_
b250: 4c 4f 43 4b 53 54 41 54 45 20 20 20 20 20 20 20  LOCKSTATE       
b260: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
b270: 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_GET_LOCKPROXYF
b280: 49 4c 45 20 20 20 20 20 20 32 0a 23 64 65 66 69  ILE      2.#defi
b290: 6e 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f  ne SQLITE_SET_LO
b2a0: 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 20  CKPROXYFILE     
b2b0: 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   3.#define SQLIT
b2c0: 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20 20 20 20  E_LAST_ERRNO    
b2d0: 20 20 20 20 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a           4../*.*
b2e0: 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
b2f0: 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 31 31 30  x Handle {H17110
b300: 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 0a 2a 2a  } <S20130>.**.**
b310: 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64 75 6c   The mutex modul
b320: 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65 20  e within SQLite 
b330: 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74 65 33  defines [sqlite3
b340: 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20 61 6e  _mutex] to be an
b350: 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74 79 70  .** abstract typ
b360: 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20 6f 62  e for a mutex ob
b370: 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c 69 74  ject.  The SQLit
b380: 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c 6f 6f  e core never loo
b390: 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 69 6e 74  ks.** at the int
b3a0: 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
b3b0: 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 71 6c 69  tion of an [sqli
b3c0: 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 49 74 20  te3_mutex].  It 
b3d0: 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 20 77 69  only.** deals wi
b3e0: 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  th pointers to t
b3f0: 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  he [sqlite3_mute
b400: 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a  x] object..**.**
b410: 20 4d 75 74 65 78 65 73 20 61 72 65 20 63 72 65   Mutexes are cre
b420: 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69  ated using [sqli
b430: 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
b440: 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  )]..*/.typedef s
b450: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75  truct sqlite3_mu
b460: 74 65 78 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  tex sqlite3_mute
b470: 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  x;../*.** CAPI3R
b480: 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 65  EF: OS Interface
b490: 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 34 30 7d   Object {H11140}
b4a0: 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20100>.**.** 
b4b0: 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
b4c0: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f  he sqlite3_vfs o
b4d0: 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68  bject defines th
b4e0: 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 74 77  e interface betw
b4f0: 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74  een.** the SQLit
b500: 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65 20 75  e core and the u
b510: 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74  nderlying operat
b520: 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54 68 65  ing system.  The
b530: 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74 68 65   "vfs".** in the
b540: 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 62 6a   name of the obj
b550: 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72 20 22  ect stands for "
b560: 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73  virtual file sys
b570: 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tem"..**.** The 
b580: 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 56 65  value of the iVe
b590: 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 20 69  rsion field is i
b5a0: 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 74 20 6d  nitially 1 but m
b5b0: 61 79 20 62 65 20 6c 61 72 67 65 72 20 69 6e 0a  ay be larger in.
b5c0: 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73 69 6f  ** future versio
b5d0: 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41  ns of SQLite.  A
b5e0: 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
b5f0: 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64   may be appended
b600: 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65   to this.** obje
b610: 63 74 20 77 68 65 6e 20 74 68 65 20 69 56 65 72  ct when the iVer
b620: 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20 69 6e  sion value is in
b630: 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65 20 74  creased.  Note t
b640: 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72  hat the structur
b650: 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69  e.** of the sqli
b660: 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 20 63  te3_vfs object c
b670: 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 74 72  hanges in the tr
b680: 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77 65 65  ansaction betwee
b690: 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 73  n.** SQLite vers
b6a0: 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 20 33 2e  ion 3.5.9 and 3.
b6b0: 36 2e 30 20 61 6e 64 20 79 65 74 20 74 68 65 20  6.0 and yet the 
b6c0: 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 77  iVersion field w
b6d0: 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 66 69  as not.** modifi
b6e0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a  ed..**.** The sz
b6f0: 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 69 73 20  OsFile field is 
b700: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
b710: 73 75 62 63 6c 61 73 73 65 64 20 5b 73 71 6c 69  subclassed [sqli
b720: 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 73 74 72  te3_file].** str
b730: 75 63 74 75 72 65 20 75 73 65 64 20 62 79 20 74  ucture used by t
b740: 68 69 73 20 56 46 53 2e 20 20 6d 78 50 61 74 68  his VFS.  mxPath
b750: 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61 78 69  name is the maxi
b760: 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 0a 2a 2a  mum length of.**
b770: 20 61 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 74   a pathname in t
b780: 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 52  his VFS..**.** R
b790: 65 67 69 73 74 65 72 65 64 20 73 71 6c 69 74 65  egistered sqlite
b7a0: 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 61 72  3_vfs objects ar
b7b0: 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b  e kept on a link
b7c0: 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64 20 62  ed list formed b
b7d0: 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74 20 70  y.** the pNext p
b7e0: 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b 73 71  ointer.  The [sq
b7f0: 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
b800: 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71  er()].** and [sq
b810: 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
b820: 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63  ster()] interfac
b830: 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73 20 6c  es manage this l
b840: 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68 72 65  ist.** in a thre
b850: 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20 54 68  ad-safe way.  Th
b860: 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66  e [sqlite3_vfs_f
b870: 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61 63 65  ind()] interface
b880: 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74 68 65  .** searches the
b890: 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 65 72 20   list.  Neither 
b8a0: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
b8b0: 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 56 46 53  code nor the VFS
b8c0: 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
b8d0: 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68  on should use th
b8e0: 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e  e pNext pointer.
b8f0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65 78 74  .**.** The pNext
b900: 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 6f 6e   field is the on
b910: 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20  ly field in the 
b920: 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 73  sqlite3_vfs.** s
b930: 74 72 75 63 74 75 72 65 20 74 68 61 74 20 53 51  tructure that SQ
b940: 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72 20 6d  Lite will ever m
b950: 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65 20 77  odify.  SQLite w
b960: 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73 73 0a  ill only access.
b970: 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69  ** or modify thi
b980: 73 20 66 69 65 6c 64 20 77 68 69 6c 65 20 68 6f  s field while ho
b990: 6c 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c  lding a particul
b9a0: 61 72 20 73 74 61 74 69 63 20 6d 75 74 65 78 2e  ar static mutex.
b9b0: 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74  .** The applicat
b9c0: 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  ion should never
b9d0: 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 69 6e 67   modify anything
b9e0: 20 77 69 74 68 69 6e 20 74 68 65 20 73 71 6c 69   within the sqli
b9f0: 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 6a 65 63  te3_vfs.** objec
ba00: 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 6a 65 63  t once the objec
ba10: 74 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73  t has been regis
ba20: 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tered..**.** The
ba30: 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 68 6f 6c   zName field hol
ba40: 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ds the name of t
ba50: 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 2e 20 20  he VFS module.  
ba60: 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 0a 2a 2a  The name must.**
ba70: 20 62 65 20 75 6e 69 71 75 65 20 61 63 72 6f 73   be unique acros
ba80: 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 75 6c 65  s all VFS module
ba90: 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  s..**.** SQLite 
baa0: 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 65 20 74  will guarantee t
bab0: 68 61 74 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d  hat the zFilenam
bac0: 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 78  e parameter to x
bad0: 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69 74 68 65  Open.** is eithe
bae0: 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  r a NULL pointer
baf0: 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 74 61 69   or string obtai
bb00: 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78 46 75 6c  ned.** from xFul
bb10: 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20 20 53 51  lPathname().  SQ
bb20: 4c 69 74 65 20 66 75 72 74 68 65 72 20 67 75 61  Lite further gua
bb30: 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a 2a 20  rantees that.** 
bb40: 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c 20  the string will 
bb50: 62 65 20 76 61 6c 69 64 20 61 6e 64 20 75 6e 63  be valid and unc
bb60: 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 78 43 6c  hanged until xCl
bb70: 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 61 6c 6c  ose() is.** call
bb80: 65 64 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74  ed. Because of t
bb90: 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74  he previous sent
bba0: 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20 5b 73 71  ence,.** the [sq
bbb0: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e 20  lite3_file] can 
bbc0: 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20 70  safely store a p
bbd0: 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a  ointer to the.**
bbe0: 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74 20   filename if it 
bbf0: 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62 65  needs to remembe
bc00: 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 66  r the filename f
bc10: 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e 0a  or some reason..
bc20: 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c 65 6e  ** If the zFilen
bc30: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  ame parameter is
bc40: 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 4c 4c   xOpen is a NULL
bc50: 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 78 4f   pointer then xO
bc60: 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 76 65  pen.** must inve
bc70: 6e 74 20 69 74 73 20 6f 77 6e 20 74 65 6d 70 6f  nt its own tempo
bc80: 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 74 68  rary name for th
bc90: 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 65 76 65  e file.  Wheneve
bca0: 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c 65 6e  r the .** xFilen
bcb0: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  ame parameter is
bcc0: 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20 61 6c   NULL it will al
bcd0: 73 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 74  so be the case t
bce0: 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61 67 73  hat the.** flags
bcf0: 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c 20   parameter will 
bd00: 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f  include [SQLITE_
bd10: 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
bd20: 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  SE]..**.** The f
bd30: 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f  lags argument to
bd40: 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c 75 64 65   xOpen() include
bd50: 73 20 61 6c 6c 20 62 69 74 73 20 73 65 74 20 69  s all bits set i
bd60: 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 73 20 61  n.** the flags a
bd70: 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69  rgument to [sqli
bd80: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20  te3_open_v2()]. 
bd90: 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 65 33 5f   Or if [sqlite3_
bda0: 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73  open()].** or [s
bdb0: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
bdc0: 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 66   is used, then f
bdd0: 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 20 61 74  lags includes at
bde0: 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 4c 49 54   least.** [SQLIT
bdf0: 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
be00: 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  ] | [SQLITE_OPEN
be10: 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a 20 49 66  _CREATE]. .** If
be20: 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 20 61   xOpen() opens a
be30: 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c 79 20   file read-only 
be40: 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a 70 4f  then it sets *pO
be50: 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 69 6e  utFlags to.** in
be60: 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50  clude [SQLITE_OP
be70: 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 20 4f  EN_READONLY].  O
be80: 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a 70 4f  ther bits in *pO
be90: 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65 20 73  utFlags may be s
bea0: 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  et..**.** SQLite
beb0: 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 64 20 6f   will also add o
bec0: 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
bed0: 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65  ing flags to the
bee0: 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63 61 6c 6c   xOpen().** call
bef0: 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  , depending on t
bf00: 68 65 20 6f 62 6a 65 63 74 20 62 65 69 6e 67 20  he object being 
bf10: 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75  opened:.**.** <u
bf20: 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c  l>.** <li>  [SQL
bf30: 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
bf40: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49  ].** <li>  [SQLI
bf50: 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
bf60: 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b  RNAL].** <li>  [
bf70: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
bf80: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  _DB].** <li>  [S
bf90: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
bfa0: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e  JOURNAL].** <li>
bfb0: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54    [SQLITE_OPEN_T
bfc0: 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a 2a 2a 20  RANSIENT_DB].** 
bfd0: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
bfe0: 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 5d 0a 2a  EN_SUBJOURNAL].*
bff0: 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f  * <li>  [SQLITE_
c000: 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
c010: 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  NAL].** </ul>.**
c020: 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 49 2f 4f  .** The file I/O
c030: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
c040: 63 61 6e 20 75 73 65 20 74 68 65 20 6f 62 6a 65  can use the obje
c050: 63 74 20 74 79 70 65 20 66 6c 61 67 73 20 74 6f  ct type flags to
c060: 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 20 77  .** change the w
c070: 61 79 20 69 74 20 64 65 61 6c 73 20 77 69 74 68  ay it deals with
c080: 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 65 78 61   files.  For exa
c090: 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61  mple, an applica
c0a0: 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 6f 65  tion.** that doe
c0b0: 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74  s not care about
c0c0: 20 63 72 61 73 68 20 72 65 63 6f 76 65 72 79 20   crash recovery 
c0d0: 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67 68  or rollback migh
c0e0: 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f 70  t make.** the op
c0f0: 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20  en of a journal 
c100: 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 57  file a no-op.  W
c110: 72 69 74 65 73 20 74 6f 20 74 68 69 73 20 6a 6f  rites to this jo
c120: 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a 20 61  urnal would.** a
c130: 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20 61  lso be no-ops, a
c140: 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74  nd any attempt t
c150: 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75 72 6e  o read the journ
c160: 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 0a  al would return.
c170: 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2e  ** SQLITE_IOERR.
c180: 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65    Or the impleme
c190: 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 72 65  ntation might re
c1a0: 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61 20 64  cognize that a d
c1b0: 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20  atabase.** file 
c1c0: 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20 70 61  will be doing pa
c1d0: 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63 74 6f  ge-aligned secto
c1e0: 72 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74  r reads and writ
c1f0: 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d 0a 2a  es in a random.*
c200: 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 65 74 20  * order and set 
c210: 75 70 20 69 74 73 20 49 2f 4f 20 73 75 62 73 79  up its I/O subsy
c220: 73 74 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c 79  stem accordingly
c230: 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d  ..**.** SQLite m
c240: 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20 6f 6e  ight also add on
c250: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
c260: 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65 20  ng flags to the 
c270: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a 0a 2a 2a  xOpen method:.**
c280: 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
c290: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45   [SQLITE_OPEN_DE
c2a0: 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20  LETEONCLOSE].** 
c2b0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45  <li> [SQLITE_OPE
c2c0: 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a 2a 2a 20  N_EXCLUSIVE].** 
c2d0: 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  </ul>.**.** The 
c2e0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c  [SQLITE_OPEN_DEL
c2f0: 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 6c 61 67  ETEONCLOSE] flag
c300: 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65 20   means the file 
c310: 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 65 6c  should be.** del
c320: 65 74 65 64 20 77 68 65 6e 20 69 74 20 69 73 20  eted when it is 
c330: 63 6c 6f 73 65 64 2e 20 20 54 68 65 20 5b 53 51  closed.  The [SQ
c340: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
c350: 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c  ONCLOSE].** will
c360: 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 4d 50   be set for TEMP
c370: 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a 6f 75    databases, jou
c380: 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20 73 75  rnals and for su
c390: 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a  bjournals..**.**
c3a0: 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45   The [SQLITE_OPE
c3b0: 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66 6c 61  N_EXCLUSIVE] fla
c3c0: 67 20 69 73 20 61 6c 77 61 79 73 20 75 73 65 64  g is always used
c3d0: 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 6f 6e 0a   in conjunction.
c3e0: 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c  ** with the [SQL
c3f0: 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d  ITE_OPEN_CREATE]
c400: 20 66 6c 61 67 2c 20 77 68 69 63 68 20 61 72 65   flag, which are
c410: 20 62 6f 74 68 20 64 69 72 65 63 74 6c 79 0a 2a   both directly.*
c420: 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f 20 74  * analogous to t
c430: 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 20 4f 5f  he O_EXCL and O_
c440: 43 52 45 41 54 20 66 6c 61 67 73 20 6f 66 20 74  CREAT flags of t
c450: 68 65 20 50 4f 53 49 58 20 6f 70 65 6e 28 29 0a  he POSIX open().
c460: 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c  ** API.  The SQL
c470: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
c480: 56 45 20 66 6c 61 67 2c 20 77 68 65 6e 20 70 61  VE flag, when pa
c490: 69 72 65 64 20 77 69 74 68 20 74 68 65 20 0a 2a  ired with the .*
c4a0: 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  * SQLITE_OPEN_CR
c4b0: 45 41 54 45 2c 20 69 73 20 75 73 65 64 20 74 6f  EATE, is used to
c4c0: 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 66   indicate that f
c4d0: 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ile should alway
c4e0: 73 0a 2a 2a 20 62 65 20 63 72 65 61 74 65 64 2c  s.** be created,
c4f0: 20 61 6e 64 20 74 68 61 74 20 69 74 20 69 73 20   and that it is 
c500: 61 6e 20 65 72 72 6f 72 20 69 66 20 69 74 20 61  an error if it a
c510: 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a 2a  lready exists..*
c520: 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f 74 3c 2f  * It is <i>not</
c530: 69 3e 20 75 73 65 64 20 74 6f 20 69 6e 64 69 63  i> used to indic
c540: 61 74 65 20 74 68 65 20 66 69 6c 65 20 73 68 6f  ate the file sho
c550: 75 6c 64 20 62 65 20 6f 70 65 6e 65 64 20 0a 2a  uld be opened .*
c560: 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 76 65 20  * for exclusive 
c570: 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 74  access..**.** At
c580: 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65 20   least szOsFile 
c590: 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
c5a0: 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  are allocated by
c5b0: 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 68 6f   SQLite.** to ho
c5c0: 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65 33  ld the  [sqlite3
c5d0: 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72 65  _file] structure
c5e0: 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74   passed as the t
c5f0: 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  hird.** argument
c600: 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 65 20   to xOpen.  The 
c610: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f 65  xOpen method doe
c620: 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a 2a  s not have to.**
c630: 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 74   allocate the st
c640: 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f 75  ructure; it shou
c650: 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74 20  ld just fill it 
c660: 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 2a  in.  Note that.*
c670: 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68  * the xOpen meth
c680: 6f 64 20 6d 75 73 74 20 73 65 74 20 74 68 65 20  od must set the 
c690: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65  sqlite3_file.pMe
c6a0: 74 68 6f 64 73 20 74 6f 20 65 69 74 68 65 72 0a  thods to either.
c6b0: 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73 71 6c 69  ** a valid [sqli
c6c0: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20  te3_io_methods] 
c6d0: 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20 4e 55 4c  object or to NUL
c6e0: 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73 74 20 64  L.  xOpen must d
c6f0: 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65 6e 20 69  o.** this even i
c700: 66 20 74 68 65 20 6f 70 65 6e 20 66 61 69 6c 73  f the open fails
c710: 2e 20 20 53 51 4c 69 74 65 20 65 78 70 65 63 74  .  SQLite expect
c720: 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74  s that the sqlit
c730: 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64 73  e3_file.pMethods
c740: 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 69 6c 6c  .** element will
c750: 20 62 65 20 76 61 6c 69 64 20 61 66 74 65 72 20   be valid after 
c760: 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 20 72 65  xOpen returns re
c770: 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20  gardless of the 
c780: 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 66 61  success.** or fa
c790: 69 6c 75 72 65 20 6f 66 20 74 68 65 20 78 4f 70  ilure of the xOp
c7a0: 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  en call..**.** T
c7b0: 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e  he flags argumen
c7c0: 74 20 74 6f 20 78 41 63 63 65 73 73 28 29 20 6d  t to xAccess() m
c7d0: 61 79 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 43  ay be [SQLITE_AC
c7e0: 43 45 53 53 5f 45 58 49 53 54 53 5d 0a 2a 2a 20  CESS_EXISTS].** 
c7f0: 74 6f 20 74 65 73 74 20 66 6f 72 20 74 68 65 20  to test for the 
c800: 65 78 69 73 74 65 6e 63 65 20 6f 66 20 61 20 66  existence of a f
c810: 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ile, or [SQLITE_
c820: 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45  ACCESS_READWRITE
c830: 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20 77 68 65  ] to.** test whe
c840: 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 72  ther a file is r
c850: 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74  eadable and writ
c860: 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45  able, or [SQLITE
c870: 5f 41 43 43 45 53 53 5f 52 45 41 44 5d 0a 2a 2a  _ACCESS_READ].**
c880: 20 74 6f 20 74 65 73 74 20 77 68 65 74 68 65 72   to test whether
c890: 20 61 20 66 69 6c 65 20 69 73 20 61 74 20 6c 65   a file is at le
c8a0: 61 73 74 20 72 65 61 64 61 62 6c 65 2e 20 20 20  ast readable.   
c8b0: 54 68 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20  The file can be 
c8c0: 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a  a.** directory..
c8d0: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  **.** SQLite wil
c8e0: 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74  l always allocat
c8f0: 65 20 61 74 20 6c 65 61 73 74 20 6d 78 50 61 74  e at least mxPat
c900: 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 20 66 6f  hname+1 bytes fo
c910: 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20  r the.** output 
c920: 62 75 66 66 65 72 20 78 46 75 6c 6c 50 61 74 68  buffer xFullPath
c930: 6e 61 6d 65 2e 20 20 54 68 65 20 65 78 61 63 74  name.  The exact
c940: 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74   size of the out
c950: 70 75 74 20 62 75 66 66 65 72 0a 2a 2a 20 69 73  put buffer.** is
c960: 20 61 6c 73 6f 20 70 61 73 73 65 64 20 61 73 20   also passed as 
c970: 61 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62  a parameter to b
c980: 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e 20 49 66  oth  methods. If
c990: 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
c9a0: 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72  er.** is not lar
c9b0: 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53 51 4c 49  ge enough, [SQLI
c9c0: 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 68 6f  TE_CANTOPEN] sho
c9d0: 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e  uld be returned.
c9e0: 20 53 69 6e 63 65 20 74 68 69 73 20 69 73 0a 2a   Since this is.*
c9f0: 2a 20 68 61 6e 64 6c 65 64 20 61 73 20 61 20 66  * handled as a f
ca00: 61 74 61 6c 20 65 72 72 6f 72 20 62 79 20 53 51  atal error by SQ
ca10: 4c 69 74 65 2c 20 76 66 73 20 69 6d 70 6c 65 6d  Lite, vfs implem
ca20: 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64  entations should
ca30: 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 6f 20   endeavor.** to 
ca40: 70 72 65 76 65 6e 74 20 74 68 69 73 20 62 79 20  prevent this by 
ca50: 73 65 74 74 69 6e 67 20 6d 78 50 61 74 68 6e 61  setting mxPathna
ca60: 6d 65 20 74 6f 20 61 20 73 75 66 66 69 63 69 65  me to a sufficie
ca70: 6e 74 6c 79 20 6c 61 72 67 65 20 76 61 6c 75 65  ntly large value
ca80: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 61 6e  ..**.** The xRan
ca90: 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65 65  domness(), xSlee
caa0: 70 28 29 2c 20 61 6e 64 20 78 43 75 72 72 65 6e  p(), and xCurren
cab0: 74 54 69 6d 65 28 29 20 69 6e 74 65 72 66 61 63  tTime() interfac
cac0: 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 73 74  es.** are not st
cad0: 72 69 63 74 6c 79 20 61 20 70 61 72 74 20 6f 66  rictly a part of
cae0: 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 2c   the filesystem,
caf0: 20 62 75 74 20 74 68 65 79 20 61 72 65 0a 2a 2a   but they are.**
cb00: 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65   included in the
cb10: 20 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66   VFS structure f
cb20: 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e  or completeness.
cb30: 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e  .** The xRandomn
cb40: 65 73 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 61  ess() function a
cb50: 74 74 65 6d 70 74 73 20 74 6f 20 72 65 74 75 72  ttempts to retur
cb60: 6e 20 6e 42 79 74 65 73 20 62 79 74 65 73 0a 2a  n nBytes bytes.*
cb70: 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 6c 69 74  * of good-qualit
cb80: 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74  y randomness int
cb90: 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 72 65 74  o zOut.  The ret
cba0: 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20  urn value is.** 
cbb0: 74 68 65 20 61 63 74 75 61 6c 20 6e 75 6d 62 65  the actual numbe
cbc0: 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 72 61  r of bytes of ra
cbd0: 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65  ndomness obtaine
cbe0: 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c 65 65 70  d..** The xSleep
cbf0: 28 29 20 6d 65 74 68 6f 64 20 63 61 75 73 65 73  () method causes
cc00: 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72   the calling thr
cc10: 65 61 64 20 74 6f 20 73 6c 65 65 70 20 66 6f 72  ead to sleep for
cc20: 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 74 68 65   at.** least the
cc30: 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f   number of micro
cc40: 73 65 63 6f 6e 64 73 20 67 69 76 65 6e 2e 20 20  seconds given.  
cc50: 54 68 65 20 78 43 75 72 72 65 6e 74 54 69 6d 65  The xCurrentTime
cc60: 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74  ().** method ret
cc70: 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e 20 44 61  urns a Julian Da
cc80: 79 20 4e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  y Number for the
cc90: 20 63 75 72 72 65 6e 74 20 64 61 74 65 20 61 6e   current date an
cca0: 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f 0a 74 79  d time..**.*/.ty
ccb0: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
ccc0: 69 74 65 33 5f 76 66 73 20 73 71 6c 69 74 65 33  ite3_vfs sqlite3
ccd0: 5f 76 66 73 3b 0a 73 74 72 75 63 74 20 73 71 6c  _vfs;.struct sql
cce0: 69 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 6e 74  ite3_vfs {.  int
ccf0: 20 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20 20   iVersion;      
cd00: 20 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75        /* Structu
cd10: 72 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65  re version numbe
cd20: 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4f 73 46  r */.  int szOsF
cd30: 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ile;            
cd40: 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75 62 63 6c  /* Size of subcl
cd50: 61 73 73 65 64 20 73 71 6c 69 74 65 33 5f 66 69  assed sqlite3_fi
cd60: 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61  le */.  int mxPa
cd70: 74 68 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  thname;         
cd80: 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69 6c 65   /* Maximum file
cd90: 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67 74 68   pathname length
cda0: 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66   */.  sqlite3_vf
cdb0: 73 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f  s *pNext;      /
cdc0: 2a 20 4e 65 78 74 20 72 65 67 69 73 74 65 72 65  * Next registere
cdd0: 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f 6e 73 74  d VFS */.  const
cde0: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
cdf0: 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
ce00: 68 69 73 20 76 69 72 74 75 61 6c 20 66 69 6c 65  his virtual file
ce10: 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 76 6f 69   system */.  voi
ce20: 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 20  d *pAppData;    
ce30: 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
ce40: 20 74 6f 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   to application-
ce50: 73 70 65 63 69 66 69 63 20 64 61 74 61 20 2a 2f  specific data */
ce60: 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28  .  int (*xOpen)(
ce70: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f  sqlite3_vfs*, co
ce80: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
ce90: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 0a   sqlite3_file*,.
cea0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
ceb0: 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70  nt flags, int *p
cec0: 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 69 6e 74  OutFlags);.  int
ced0: 20 28 2a 78 44 65 6c 65 74 65 29 28 73 71 6c 69   (*xDelete)(sqli
cee0: 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20  te3_vfs*, const 
cef0: 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74  char *zName, int
cf00: 20 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 6e 74   syncDir);.  int
cf10: 20 28 2a 78 41 63 63 65 73 73 29 28 73 71 6c 69   (*xAccess)(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 4e 61 6d 65 2c 20 69 6e 74  char *zName, int
cf40: 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 52 65   flags, int *pRe
cf50: 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78  sOut);.  int (*x
cf60: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 28 73 71  FullPathname)(sq
cf70: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73  lite3_vfs*, cons
cf80: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69  t char *zName, i
cf90: 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72 20 2a 7a  nt nOut, char *z
cfa0: 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a  Out);.  void *(*
cfb0: 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69 74 65 33  xDlOpen)(sqlite3
cfc0: 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61  _vfs*, const cha
cfd0: 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  r *zFilename);. 
cfe0: 20 76 6f 69 64 20 28 2a 78 44 6c 45 72 72 6f 72   void (*xDlError
cff0: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20  )(sqlite3_vfs*, 
d000: 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20  int nByte, char 
d010: 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 6f 69  *zErrMsg);.  voi
d020: 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29 28 73 71  d (*(*xDlSym)(sq
d030: 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f 69 64 2a  lite3_vfs*,void*
d040: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
d050: 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 3b 0a 20  ymbol))(void);. 
d060: 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c 6f 73 65   void (*xDlClose
d070: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20  )(sqlite3_vfs*, 
d080: 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  void*);.  int (*
d090: 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28 73 71 6c  xRandomness)(sql
d0a0: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e  ite3_vfs*, int n
d0b0: 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 4f 75 74  Byte, char *zOut
d0c0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 6c 65 65  );.  int (*xSlee
d0d0: 70 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  p)(sqlite3_vfs*,
d0e0: 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64   int microsecond
d0f0: 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 75 72  s);.  int (*xCur
d100: 72 65 6e 74 54 69 6d 65 29 28 73 71 6c 69 74 65  rentTime)(sqlite
d110: 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c 65 2a 29  3_vfs*, double*)
d120: 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 74 4c 61  ;.  int (*xGetLa
d130: 73 74 45 72 72 6f 72 29 28 73 71 6c 69 74 65 33  stError)(sqlite3
d140: 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63 68 61 72  _vfs*, int, char
d150: 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 20 66 69   *);.  /* New fi
d160: 65 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 65  elds may be appe
d170: 6e 64 65 64 20 69 6e 20 66 69 67 75 72 65 20 76  nded in figure v
d180: 65 72 73 69 6f 6e 73 2e 20 20 54 68 65 20 69 56  ersions.  The iV
d190: 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 61 6c 75  ersion.  ** valu
d1a0: 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e 74  e will increment
d1b0: 20 77 68 65 6e 65 76 65 72 20 74 68 69 73 20 68   whenever this h
d1c0: 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f  appens. */.};../
d1d0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46  *.** CAPI3REF: F
d1e0: 6c 61 67 73 20 66 6f 72 20 74 68 65 20 78 41 63  lags for the xAc
d1f0: 63 65 73 73 20 56 46 53 20 6d 65 74 68 6f 64 20  cess VFS method 
d200: 7b 48 31 31 31 39 30 7d 20 3c 48 31 31 31 34 30  {H11190} <H11140
d210: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e  >.**.** These in
d220: 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20  teger constants 
d230: 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74  can be used as t
d240: 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  he third paramet
d250: 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 78 41 63  er to.** the xAc
d260: 63 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 61  cess method of a
d270: 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  n [sqlite3_vfs] 
d280: 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d 20 20 54  object. {END}  T
d290: 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a  hey determine.**
d2a0: 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 70 65   what kind of pe
d2b0: 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 78 41  rmissions the xA
d2c0: 63 63 65 73 73 20 6d 65 74 68 6f 64 20 69 73 20  ccess method is 
d2d0: 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a 2a 20  looking for..** 
d2e0: 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45  With SQLITE_ACCE
d2f0: 53 53 5f 45 58 49 53 54 53 2c 20 74 68 65 20 78  SS_EXISTS, the x
d300: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a  Access method.**
d310: 20 73 69 6d 70 6c 79 20 63 68 65 63 6b 73 20 77   simply checks w
d320: 68 65 74 68 65 72 20 74 68 65 20 66 69 6c 65 20  hether the file 
d330: 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69 74 68 20  exists..** With 
d340: 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45  SQLITE_ACCESS_RE
d350: 41 44 57 52 49 54 45 2c 20 74 68 65 20 78 41 63  ADWRITE, the xAc
d360: 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 63  cess method.** c
d370: 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 74 68  hecks whether th
d380: 65 20 66 69 6c 65 20 69 73 20 62 6f 74 68 20 72  e file is both r
d390: 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74  eadable and writ
d3a0: 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68 20 53 51  able..** With SQ
d3b0: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
d3c0: 2c 20 74 68 65 20 78 41 63 63 65 73 73 20 6d 65  , the xAccess me
d3d0: 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73 20 77  thod.** checks w
d3e0: 68 65 74 68 65 72 20 74 68 65 20 66 69 6c 65 20  hether the file 
d3f0: 69 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2f 0a  is readable..*/.
d400: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
d410: 43 43 45 53 53 5f 45 58 49 53 54 53 20 20 20 20  CCESS_EXISTS    
d420: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
d430: 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
d440: 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  E 1.#define SQLI
d450: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 20 20  TE_ACCESS_READ  
d460: 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 43 41 50      2../*.** CAP
d470: 49 33 52 45 46 3a 20 49 6e 69 74 69 61 6c 69 7a  I3REF: Initializ
d480: 65 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62  e The SQLite Lib
d490: 72 61 72 79 20 7b 48 31 30 31 33 30 7d 20 3c 53  rary {H10130} <S
d4a0: 32 30 30 30 30 3e 3c 53 33 30 31 30 30 3e 0a 2a  20000><S30100>.*
d4b0: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
d4c0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f  _initialize() ro
d4d0: 75 74 69 6e 65 20 69 6e 69 74 69 61 6c 69 7a 65  utine initialize
d4e0: 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74 65 20  s the.** SQLite 
d4f0: 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 73 71  library.  The sq
d500: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
d510: 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 65 61 6c   routine.** deal
d520: 6c 6f 63 61 74 65 73 20 61 6e 79 20 72 65 73 6f  locates any reso
d530: 75 72 63 65 73 20 74 68 61 74 20 77 65 72 65 20  urces that were 
d540: 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c  allocated by sql
d550: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
d560: 29 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  )..** This routi
d570: 6e 65 73 20 61 72 65 20 64 65 73 69 67 6e 65 64  nes are designed
d580: 20 74 6f 20 61 69 64 20 69 6e 20 70 72 6f 63 65   to aid in proce
d590: 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ss initializatio
d5a0: 6e 20 61 6e 64 0a 2a 2a 20 73 68 75 74 64 6f 77  n and.** shutdow
d5b0: 6e 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 79  n on embedded sy
d5c0: 73 74 65 6d 73 2e 20 20 57 6f 72 6b 73 74 61 74  stems.  Workstat
d5d0: 69 6f 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  ion applications
d5e0: 20 75 73 69 6e 67 0a 2a 2a 20 53 51 4c 69 74 65   using.** SQLite
d5f0: 20 6e 6f 72 6d 61 6c 6c 79 20 64 6f 20 6e 6f 74   normally do not
d600: 20 6e 65 65 64 20 74 6f 20 69 6e 76 6f 6b 65 20   need to invoke 
d610: 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20  either of these 
d620: 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
d630: 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  A call to sqlite
d640: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69  3_initialize() i
d650: 73 20 61 6e 20 22 65 66 66 65 63 74 69 76 65 22  s an "effective"
d660: 20 63 61 6c 6c 20 69 66 20 69 74 20 69 73 0a 2a   call if it is.*
d670: 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  * the first time
d680: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
d690: 69 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64  ize() is invoked
d6a0: 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65   during the life
d6b0: 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 70  time of.** the p
d6c0: 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20 69 74  rocess, or if it
d6d0: 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74 69   is the first ti
d6e0: 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  me sqlite3_initi
d6f0: 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b  alize() is invok
d700: 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  ed.** following 
d710: 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
d720: 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 4f  3_shutdown().  O
d730: 6e 6c 79 20 61 6e 20 65 66 66 65 63 74 69 76 65  nly an effective
d740: 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20 73 71 6c 69   call.** of sqli
d750: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
d760: 20 64 6f 65 73 20 61 6e 79 20 69 6e 69 74 69 61   does any initia
d770: 6c 69 7a 61 74 69 6f 6e 2e 20 20 41 6c 6c 20 6f  lization.  All o
d780: 74 68 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 61 72  ther calls.** ar
d790: 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70  e harmless no-op
d7a0: 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20  s..**.** A call 
d7b0: 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  to sqlite3_shutd
d7c0: 6f 77 6e 28 29 20 69 73 20 61 6e 20 22 65 66 66  own() is an "eff
d7d0: 65 63 74 69 76 65 22 20 63 61 6c 6c 20 69 66 20  ective" call if 
d7e0: 69 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0a  it is the first.
d7f0: 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ** call to sqlit
d800: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 73 69  e3_shutdown() si
d810: 6e 63 65 20 74 68 65 20 6c 61 73 74 20 73 71 6c  nce the last sql
d820: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
d830: 29 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 61 6e 20 65  ).  Only.** an e
d840: 66 66 65 63 74 69 76 65 20 63 61 6c 6c 20 74 6f  ffective call to
d850: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
d860: 6e 28 29 20 64 6f 65 73 20 61 6e 79 20 64 65 69  n() does any dei
d870: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a  nitialization..*
d880: 2a 20 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 69  * All other vali
d890: 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  d calls to sqlit
d8a0: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 61 72  e3_shutdown() ar
d8b0: 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70  e harmless no-op
d8c0: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  s..**.** The sql
d8d0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
d8e0: 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74  ) interface is t
d8f0: 68 72 65 61 64 73 61 66 65 2c 20 62 75 74 20 73  hreadsafe, but s
d900: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
d910: 29 0a 2a 2a 20 69 73 20 6e 6f 74 2e 20 20 54 68  ).** is not.  Th
d920: 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  e sqlite3_shutdo
d930: 77 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 6d  wn() interface m
d940: 75 73 74 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c  ust only be call
d950: 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 69 6e  ed from a.** sin
d960: 67 6c 65 20 74 68 72 65 61 64 2e 20 20 41 6c 6c  gle thread.  All
d970: 20 6f 70 65 6e 20 5b 64 61 74 61 62 61 73 65 20   open [database 
d980: 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 6d 75 73  connections] mus
d990: 74 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20  t be closed and 
d9a0: 61 6c 6c 0a 2a 2a 20 6f 74 68 65 72 20 53 51 4c  all.** other SQL
d9b0: 69 74 65 20 72 65 73 6f 75 72 63 65 73 20 6d 75  ite resources mu
d9c0: 73 74 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65  st be deallocate
d9d0: 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b  d prior to invok
d9e0: 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73  ing.** sqlite3_s
d9f0: 68 75 74 64 6f 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a  hutdown()..**.**
da00: 20 41 6d 6f 6e 67 20 6f 74 68 65 72 20 74 68 69   Among other thi
da10: 6e 67 73 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69  ngs, sqlite3_ini
da20: 74 69 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20 69  tialize() will i
da30: 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33  nvoke.** sqlite3
da40: 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d  _os_init().  Sim
da50: 69 6c 61 72 6c 79 2c 20 73 71 6c 69 74 65 33 5f  ilarly, sqlite3_
da60: 73 68 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 77 69  shutdown().** wi
da70: 6c 6c 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65  ll invoke sqlite
da80: 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a  3_os_end()..**.*
da90: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  * The sqlite3_in
daa0: 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69  itialize() routi
dab0: 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  ne returns [SQLI
dac0: 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73  TE_OK] on succes
dad0: 73 2e 0a 2a 2a 20 49 66 20 66 6f 72 20 73 6f 6d  s..** If for som
dae0: 65 20 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65  e reason, sqlite
daf0: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69  3_initialize() i
db00: 73 20 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74  s unable to init
db10: 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69  ialize.** the li
db20: 62 72 61 72 79 20 28 70 65 72 68 61 70 73 20 69  brary (perhaps i
db30: 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61  t is unable to a
db40: 6c 6c 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64  llocate a needed
db50: 20 72 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a   resource such.*
db60: 2a 20 61 73 20 61 20 6d 75 74 65 78 29 20 69 74  * as a mutex) it
db70: 20 72 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72   returns an [err
db80: 6f 72 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74  or code] other t
db90: 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e  han [SQLITE_OK].
dba0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
dbb0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
dbc0: 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
dbd0: 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20  d internally by 
dbe0: 6d 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 51  many other.** SQ
dbf0: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20  Lite interfaces 
dc00: 73 6f 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69  so that an appli
dc10: 63 61 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64  cation usually d
dc20: 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a  oes not need to.
dc30: 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65  ** invoke sqlite
dc40: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64  3_initialize() d
dc50: 69 72 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78  irectly.  For ex
dc60: 61 6d 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f  ample, [sqlite3_
dc70: 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73  open()].** calls
dc80: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
dc90: 69 7a 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c  ize() so the SQL
dca0: 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c  ite library will
dcb0: 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   be automaticall
dcc0: 79 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64  y.** initialized
dcd0: 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f   when [sqlite3_o
dce0: 70 65 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64  pen()] is called
dcf0: 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62   if it has not b
dd00: 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a  e initialized.**
dd10: 20 61 6c 72 65 61 64 79 2e 20 20 48 6f 77 65 76   already.  Howev
dd20: 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73  er, if SQLite is
dd30: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
dd40: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  he [SQLITE_OMIT_
dd50: 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d  AUTOINIT].** com
dd60: 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  pile-time option
dd70: 2c 20 74 68 65 6e 20 74 68 65 20 61 75 74 6f 6d  , then the autom
dd80: 61 74 69 63 20 63 61 6c 6c 73 20 74 6f 20 73 71  atic calls to sq
dd90: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
dda0: 28 29 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 74 65  ().** are omitte
ddb0: 64 20 61 6e 64 20 74 68 65 20 61 70 70 6c 69 63  d and the applic
ddc0: 61 74 69 6f 6e 20 6d 75 73 74 20 63 61 6c 6c 20  ation must call 
ddd0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
dde0: 7a 65 28 29 20 64 69 72 65 63 74 6c 79 0a 2a 2a  ze() directly.**
ddf0: 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20   prior to using 
de00: 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65  any other SQLite
de10: 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46 6f 72   interface.  For
de20: 20 6d 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69   maximum portabi
de30: 6c 69 74 79 2c 0a 2a 2a 20 69 74 20 69 73 20 72  lity,.** it is r
de40: 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20  ecommended that 
de50: 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 6c 77  applications alw
de60: 61 79 73 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74  ays invoke sqlit
de70: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a  e3_initialize().
de80: 2a 2a 20 64 69 72 65 63 74 6c 79 20 70 72 69 6f  ** directly prio
de90: 72 20 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f  r to using any o
dea0: 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65  ther SQLite inte
deb0: 72 66 61 63 65 2e 20 20 46 75 74 75 72 65 20 72  rface.  Future r
dec0: 65 6c 65 61 73 65 73 0a 2a 2a 20 6f 66 20 53 51  eleases.** of SQ
ded0: 4c 69 74 65 20 6d 61 79 20 72 65 71 75 69 72 65  Lite may require
dee0: 20 74 68 69 73 2e 20 20 49 6e 20 6f 74 68 65 72   this.  In other
def0: 20 77 6f 72 64 73 2c 20 74 68 65 20 62 65 68 61   words, the beha
df00: 76 69 6f 72 20 65 78 68 69 62 69 74 65 64 0a 2a  vior exhibited.*
df10: 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73  * when SQLite is
df20: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b   compiled with [
df30: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
df40: 49 4e 49 54 5d 20 6d 69 67 68 74 20 62 65 63 6f  INIT] might beco
df50: 6d 65 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c  me the.** defaul
df60: 74 20 62 65 68 61 76 69 6f 72 20 69 6e 20 73 6f  t behavior in so
df70: 6d 65 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  me future releas
df80: 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  e of SQLite..**.
df90: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f  ** The sqlite3_o
dfa0: 73 5f 69 6e 69 74 28 29 20 72 6f 75 74 69 6e 65  s_init() routine
dfb0: 20 64 6f 65 73 20 6f 70 65 72 61 74 69 6e 67 2d   does operating-
dfc0: 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 63 0a  system specific.
dfd0: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ** initializatio
dfe0: 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20  n of the SQLite 
dff0: 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 73 71  library.  The sq
e000: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a  lite3_os_end().*
e010: 2a 20 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73  * routine undoes
e020: 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 73   the effect of s
e030: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
e040: 2e 20 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73  .  Typical tasks
e050: 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79  .** performed by
e060: 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
e070: 69 6e 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69  include allocati
e080: 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69  on or deallocati
e090: 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20  on.** of static 
e0a0: 72 65 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69  resources, initi
e0b0: 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f  alization of glo
e0c0: 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a  bal variables,.*
e0d0: 2a 20 73 65 74 74 69 6e 67 20 75 70 20 61 20 64  * setting up a d
e0e0: 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f  efault [sqlite3_
e0f0: 76 66 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72 20  vfs] module, or 
e100: 73 65 74 74 69 6e 67 20 75 70 0a 2a 2a 20 61 20  setting up.** a 
e110: 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72  default configur
e120: 61 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c  ation using [sql
e130: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a  ite3_config()]..
e140: 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63  **.** The applic
e150: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76  ation should nev
e160: 65 72 20 69 6e 76 6f 6b 65 20 65 69 74 68 65 72  er invoke either
e170: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
e180: 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33  ().** or sqlite3
e190: 5f 6f 73 5f 65 6e 64 28 29 20 64 69 72 65 63 74  _os_end() direct
e1a0: 6c 79 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61  ly.  The applica
e1b0: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79  tion should only
e1c0: 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74   invoke.** sqlit
e1d0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
e1e0: 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68 75 74  and sqlite3_shut
e1f0: 64 6f 77 6e 28 29 2e 20 20 54 68 65 20 73 71 6c  down().  The sql
e200: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a  ite3_os_init().*
e210: 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 63  * interface is c
e220: 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61  alled automatica
e230: 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33 5f 69  lly by sqlite3_i
e240: 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 0a  nitialize() and.
e250: 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  ** sqlite3_os_en
e260: 64 28 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79  d() is called by
e270: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
e280: 6e 28 29 2e 20 20 41 70 70 72 6f 70 72 69 61 74  n().  Appropriat
e290: 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  e.** implementat
e2a0: 69 6f 6e 73 20 66 6f 72 20 73 71 6c 69 74 65 33  ions for sqlite3
e2b0: 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73  _os_init() and s
e2c0: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a  qlite3_os_end().
e2d0: 2a 2a 20 61 72 65 20 62 75 69 6c 74 20 69 6e 74  ** are built int
e2e0: 6f 20 53 51 4c 69 74 65 20 77 68 65 6e 20 69 74  o SQLite when it
e2f0: 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72   is compiled for
e300: 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20   Unix, Windows, 
e310: 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20 57 68 65 6e  or OS/2..** When
e320: 20 5b 63 75 73 74 6f 6d 20 62 75 69 6c 64 73 20   [custom builds 
e330: 7c 20 62 75 69 6c 74 20 66 6f 72 20 6f 74 68 65  | built for othe
e340: 72 20 70 6c 61 74 66 6f 72 6d 73 5d 0a 2a 2a 20  r platforms].** 
e350: 28 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49  (using the [SQLI
e360: 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d 20 63  TE_OS_OTHER=1] c
e370: 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6f  ompile-time.** o
e380: 70 74 69 6f 6e 29 20 74 68 65 20 61 70 70 6c 69  ption) the appli
e390: 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75 70 70  cation must supp
e3a0: 6c 79 20 61 20 73 75 69 74 61 62 6c 65 20 69 6d  ly a suitable im
e3b0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
e3c0: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69  .** sqlite3_os_i
e3d0: 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65  nit() and sqlite
e3e0: 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 41 6e 20  3_os_end().  An 
e3f0: 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70 70  application-supp
e400: 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  lied.** implemen
e410: 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  tation of sqlite
e420: 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72 20 73  3_os_init() or s
e430: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a  qlite3_os_end().
e440: 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e 20 5b  ** must return [
e450: 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75  SQLITE_OK] on su
e460: 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65 20 6f  ccess and some o
e470: 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f 64 65  ther [error code
e480: 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c 75 72  ] upon.** failur
e490: 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
e4a0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 69   int sqlite3_ini
e4b0: 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b 0a 53  tialize(void);.S
e4c0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e4d0: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76  lite3_shutdown(v
e4e0: 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  oid);.SQLITE_API
e4f0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f   int sqlite3_os_
e500: 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49  init(void);.SQLI
e510: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
e520: 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 3b  e3_os_end(void);
e530: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
e540: 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 20 54 68  : Configuring Th
e550: 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79  e SQLite Library
e560: 20 7b 48 31 34 31 30 30 7d 20 3c 53 32 30 30 30   {H14100} <S2000
e570: 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 2a 20 45 58  0><S30200>.** EX
e580: 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
e590: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e   The sqlite3_con
e5a0: 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20  fig() interface 
e5b0: 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20  is used to make 
e5c0: 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61  global configura
e5d0: 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20  tion.** changes 
e5e0: 74 6f 20 53 51 4c 69 74 65 20 69 6e 20 6f 72 64  to SQLite in ord
e5f0: 65 72 20 74 6f 20 74 75 6e 65 20 53 51 4c 69 74  er to tune SQLit
e600: 65 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69  e to the specifi
e610: 63 20 6e 65 65 64 73 20 6f 66 0a 2a 2a 20 74 68  c needs of.** th
e620: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20  e application.  
e630: 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6e 66  The default conf
e640: 69 67 75 72 61 74 69 6f 6e 20 69 73 20 72 65 63  iguration is rec
e650: 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6d 6f 73  ommended for mos
e660: 74 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  t.** application
e670: 73 20 61 6e 64 20 73 6f 20 74 68 69 73 20 72 6f  s and so this ro
e680: 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79  utine is usually
e690: 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20   not necessary. 
e6a0: 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64   It is.** provid
e6b0: 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 72 61  ed to support ra
e6c0: 72 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  re applications 
e6d0: 77 69 74 68 20 75 6e 75 73 75 61 6c 20 6e 65 65  with unusual nee
e6e0: 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ds..**.** The sq
e6f0: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69  lite3_config() i
e700: 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20  nterface is not 
e710: 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65  threadsafe.  The
e720: 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20   application.** 
e730: 6d 75 73 74 20 69 6e 73 75 72 65 20 74 68 61 74  must insure that
e740: 20 6e 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 65   no other SQLite
e750: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20   interfaces are 
e760: 69 6e 76 6f 6b 65 64 20 62 79 20 6f 74 68 65 72  invoked by other
e770: 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 68 69 6c  .** threads whil
e780: 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  e sqlite3_config
e790: 28 29 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20  () is running.  
e7a0: 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 73 71 6c  Furthermore, sql
e7b0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 0a 2a 2a  ite3_config().**
e7c0: 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 69 6e 76   may only be inv
e7d0: 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 6c 69  oked prior to li
e7e0: 62 72 61 72 79 20 69 6e 69 74 69 61 6c 69 7a 61  brary initializa
e7f0: 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73  tion using.** [s
e800: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
e810: 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 73 68  e()] or after sh
e820: 75 74 64 6f 77 6e 20 62 79 20 5b 73 71 6c 69 74  utdown by [sqlit
e830: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e 0a  e3_shutdown()]..
e840: 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72  ** Note, however
e850: 2c 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63  , that sqlite3_c
e860: 6f 6e 66 69 67 28 29 20 63 61 6e 20 62 65 20 63  onfig() can be c
e870: 61 6c 6c 65 64 20 61 73 20 70 61 72 74 20 6f 66  alled as part of
e880: 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e   the.** implemen
e890: 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 61 70 70  tation of an app
e8a0: 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
e8b0: 20 5b 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69   [sqlite3_os_ini
e8c0: 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  t()]..**.** The 
e8d0: 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
e8e0: 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  o sqlite3_config
e8f0: 28 29 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  () is an integer
e900: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  .** [SQLITE_CONF
e910: 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20  IG_SINGLETHREAD 
e920: 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  | configuration 
e930: 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 64 65 74  option] that det
e940: 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 61 74 20  ermines.** what 
e950: 70 72 6f 70 65 72 74 79 20 6f 66 20 53 51 4c 69  property of SQLi
e960: 74 65 20 69 73 20 74 6f 20 62 65 20 63 6f 6e 66  te is to be conf
e970: 69 67 75 72 65 64 2e 20 20 53 75 62 73 65 71 75  igured.  Subsequ
e980: 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a  ent arguments.**
e990: 20 76 61 72 79 20 64 65 70 65 6e 64 69 6e 67 20   vary depending 
e9a0: 6f 6e 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43  on the [SQLITE_C
e9b0: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
e9c0: 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69  AD | configurati
e9d0: 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a 2a 20 69 6e  on option].** in
e9e0: 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
e9f0: 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ent..**.** When 
ea00: 61 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  a configuration 
ea10: 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2c 20 73  option is set, s
ea20: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20  qlite3_config() 
ea30: 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
ea40: 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f  OK]..** If the o
ea50: 70 74 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f 77 6e  ption is unknown
ea60: 20 6f 72 20 53 51 4c 69 74 65 20 69 73 20 75 6e   or SQLite is un
ea70: 61 62 6c 65 20 74 6f 20 73 65 74 20 74 68 65 20  able to set the 
ea80: 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 74  option.** then t
ea90: 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
eaa0: 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 5b  rns a non-zero [
eab0: 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a  error code]..**.
eac0: 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
ead0: 0a 2a 2a 20 5b 48 31 34 31 30 33 5d 20 5b 48 31  .** [H14103] [H1
eae0: 34 31 30 36 5d 20 5b 48 31 34 31 32 30 5d 20 5b  4106] [H14120] [
eaf0: 48 31 34 31 32 33 5d 20 5b 48 31 34 31 32 36 5d  H14123] [H14126]
eb00: 20 5b 48 31 34 31 32 39 5d 20 5b 48 31 34 31 33   [H14129] [H1413
eb10: 32 5d 20 5b 48 31 34 31 33 35 5d 0a 2a 2a 20 5b  2] [H14135].** [
eb20: 48 31 34 31 33 38 5d 20 5b 48 31 34 31 34 31 5d  H14138] [H14141]
eb30: 20 5b 48 31 34 31 34 34 5d 20 5b 48 31 34 31 34   [H14144] [H1414
eb40: 37 5d 20 5b 48 31 34 31 35 30 5d 20 5b 48 31 34  7] [H14150] [H14
eb50: 31 35 33 5d 20 5b 48 31 34 31 35 36 5d 20 5b 48  153] [H14156] [H
eb60: 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 31 34 31 36  14159].** [H1416
eb70: 32 5d 20 5b 48 31 34 31 36 35 5d 20 5b 48 31 34  2] [H14165] [H14
eb80: 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  168].*/.SQLITE_A
eb90: 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49  PI SQLITE_EXPERI
eba0: 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74  MENTAL int sqlit
ebb0: 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 2c 20 2e  e3_config(int, .
ebc0: 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ..);../*.** CAPI
ebd0: 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 65 20  3REF: Configure 
ebe0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
ebf0: 69 6f 6e 73 20 20 7b 48 31 34 32 30 30 7d 20 3c  ions  {H14200} <
ec00: 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S20000>.** EXPER
ec10: 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
ec20: 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e  e sqlite3_db_con
ec30: 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20  fig() interface 
ec40: 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20  is used to make 
ec50: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a  configuration.**
ec60: 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 5b 64   changes to a [d
ec70: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
ec80: 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74 65 72 66  on].  The interf
ec90: 61 63 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74  ace is similar t
eca0: 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f  o.** [sqlite3_co
ecb0: 6e 66 69 67 28 29 5d 20 65 78 63 65 70 74 20 74  nfig()] except t
ecc0: 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 73 20  hat the changes 
ecd0: 61 70 70 6c 79 20 74 6f 20 61 20 73 69 6e 67 6c  apply to a singl
ece0: 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  e.** [database c
ecf0: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73 70 65 63  onnection] (spec
ed00: 69 66 69 65 64 20 69 6e 20 74 68 65 20 66 69 72  ified in the fir
ed10: 73 74 20 61 72 67 75 6d 65 6e 74 29 2e 20 20 54  st argument).  T
ed20: 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 64 62  he.** sqlite3_db
ed30: 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66  _config() interf
ed40: 61 63 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20  ace can only be 
ed50: 75 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  used immediately
ed60: 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 64 61   after.** the da
ed70: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
ed80: 6e 20 69 73 20 63 72 65 61 74 65 64 20 75 73 69  n is created usi
ed90: 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ng [sqlite3_open
eda0: 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ()],.** [sqlite3
edb0: 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72 20 5b  _open16()], or [
edc0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
edd0: 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  )].  .**.** The 
ede0: 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
edf0: 74 6f 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f  to sqlite3_db_co
ee00: 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 20 69  nfig(D,V,...)  i
ee10: 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 66 69 67 75  s the.** configu
ee20: 72 61 74 69 6f 6e 20 76 65 72 62 20 2d 20 61 6e  ration verb - an
ee30: 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68   integer code th
ee40: 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 68 61  at indicates wha
ee50: 74 0a 2a 2a 20 61 73 70 65 63 74 20 6f 66 20 74  t.** aspect of t
ee60: 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
ee70: 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e  nection] is bein
ee80: 67 20 63 6f 6e 66 69 67 75 72 65 64 2e 0a 2a 2a  g configured..**
ee90: 20 54 68 65 20 6f 6e 6c 79 20 63 68 6f 69 63 65   The only choice
eea0: 20 66 6f 72 20 74 68 69 73 20 76 61 6c 75 65 20   for this value 
eeb0: 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e  is [SQLITE_DBCON
eec0: 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d 2e 0a  FIG_LOOKASIDE]..
eed0: 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 61 72 65  ** New verbs are
eee0: 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 64   likely to be ad
eef0: 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65  ded in future re
ef00: 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
ef10: 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20  ..** Additional 
ef20: 61 72 67 75 6d 65 6e 74 73 20 64 65 70 65 6e 64  arguments depend
ef30: 20 6f 6e 20 74 68 65 20 76 65 72 62 2e 0a 2a 2a   on the verb..**
ef40: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
ef50: 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 5d 20 5b 48  :.** [H14203] [H
ef60: 31 34 32 30 36 5d 20 5b 48 31 34 32 30 39 5d 20  14206] [H14209] 
ef70: 5b 48 31 34 32 31 32 5d 20 5b 48 31 34 32 31 35  [H14212] [H14215
ef80: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
ef90: 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
efa0: 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  TAL int sqlite3_
efb0: 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65  db_config(sqlite
efc0: 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29  3*, int op, ...)
efd0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
efe0: 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61  F: Memory Alloca
eff0: 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48  tion Routines {H
f000: 31 30 31 35 35 7d 20 3c 53 32 30 31 32 30 3e 0a  10155} <S20120>.
f010: 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
f020: 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  **.** An instanc
f030: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
f040: 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
f050: 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 20 53  erface between S
f060: 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f 77  QLite.** and low
f070: 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
f080: 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  location routine
f090: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62  s..**.** This ob
f0a0: 6a 65 63 74 20 69 73 20 75 73 65 64 20 69 6e 20  ject is used in 
f0b0: 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 69  only one place i
f0c0: 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74  n the SQLite int
f0d0: 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f 69  erface..** A poi
f0e0: 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
f0f0: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
f100: 63 74 20 69 73 20 74 68 65 20 61 72 67 75 6d 65  ct is the argume
f110: 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  nt to.** [sqlite
f120: 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e  3_config()] when
f130: 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69   the configurati
f140: 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20  on option is.** 
f150: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
f160: 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c 49 54  ALLOC] or [SQLIT
f170: 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c  E_CONFIG_GETMALL
f180: 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79 20 63 72 65  OC].  .** By cre
f190: 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63  ating an instanc
f1a0: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
f1b0: 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20  .** and passing 
f1c0: 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  it to [sqlite3_c
f1d0: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43  onfig]([SQLITE_C
f1e0: 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29 0a 2a  ONFIG_MALLOC]).*
f1f0: 2a 20 64 75 72 69 6e 67 20 63 6f 6e 66 69 67 75  * during configu
f200: 72 61 74 69 6f 6e 2c 20 61 6e 20 61 70 70 6c 69  ration, an appli
f210: 63 61 74 69 6f 6e 20 63 61 6e 20 73 70 65 63 69  cation can speci
f220: 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76  fy an alternativ
f230: 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  e.** memory allo
f240: 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
f250: 20 66 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75   for SQLite to u
f260: 73 65 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74  se for all of it
f270: 73 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 65 6d  s.** dynamic mem
f280: 6f 72 79 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a  ory needs..**.**
f290: 20 4e 6f 74 65 20 74 68 61 74 20 53 51 4c 69 74   Note that SQLit
f2a0: 65 20 63 6f 6d 65 73 20 77 69 74 68 20 73 65 76  e comes with sev
f2b0: 65 72 61 6c 20 5b 62 75 69 6c 74 2d 69 6e 20 6d  eral [built-in m
f2c0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73  emory allocators
f2d0: 5d 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 70 65  ].** that are pe
f2e0: 72 66 65 63 74 6c 79 20 61 64 65 71 75 61 74 65  rfectly adequate
f2f0: 20 66 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65   for the overwhe
f300: 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f  lming majority o
f310: 66 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a  f applications.*
f320: 2a 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20  * and that this 
f330: 6f 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75  object is only u
f340: 73 65 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20  seful to a tiny 
f350: 6d 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c  minority of appl
f360: 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68  ications.** with
f370: 20 73 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d   specialized mem
f380: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
f390: 65 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68  equirements.  Th
f3a0: 69 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20  is object is.** 
f3b0: 61 6c 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67  also used during
f3c0: 20 74 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69   testing of SQLi
f3d0: 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73  te in order to s
f3e0: 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e  pecify an altern
f3f0: 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20  ative.** memory 
f400: 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73  allocator that s
f410: 69 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20  imulates memory 
f420: 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f  out-of-memory co
f430: 6e 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f  nditions in.** o
f440: 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74  rder to verify t
f450: 68 61 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76  hat SQLite recov
f460: 65 72 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66  ers gracefully f
f470: 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64  rom such.** cond
f480: 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  itions..**.** Th
f490: 65 20 78 4d 61 6c 6c 6f 63 20 61 6e 64 20 78 46  e xMalloc and xF
f4a0: 72 65 65 20 6d 65 74 68 6f 64 73 20 6d 75 73 74  ree methods must
f4b0: 20 77 6f 72 6b 20 6c 69 6b 65 20 74 68 65 0a 2a   work like the.*
f4c0: 2a 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66  * malloc() and f
f4d0: 72 65 65 28 29 20 66 75 6e 63 74 69 6f 6e 73 20  ree() functions 
f4e0: 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72  from the standar
f4f0: 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20  d C library..** 
f500: 54 68 65 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74  The xRealloc met
f510: 68 6f 64 20 6d 75 73 74 20 77 6f 72 6b 20 6c 69  hod must work li
f520: 6b 65 20 72 65 61 6c 6c 6f 63 28 29 20 66 72 6f  ke realloc() fro
f530: 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43  m the standard C
f540: 20 6c 69 62 72 61 72 79 0a 2a 2a 20 77 69 74 68   library.** with
f550: 20 74 68 65 20 65 78 63 65 70 74 69 6f 6e 20 74   the exception t
f560: 68 61 74 20 69 66 20 74 68 65 20 73 65 63 6f 6e  hat if the secon
f570: 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 52  d argument to xR
f580: 65 61 6c 6c 6f 63 20 69 73 20 7a 65 72 6f 2c 0a  ealloc is zero,.
f590: 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 6d 75 73 74  ** xRealloc must
f5a0: 20 62 65 20 61 20 6e 6f 2d 6f 70 20 2d 20 69 74   be a no-op - it
f5b0: 20 6d 75 73 74 20 6e 6f 74 20 70 65 72 66 6f 72   must not perfor
f5c0: 6d 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  m any allocation
f5d0: 20 6f 72 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74   or.** deallocat
f5e0: 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 67 75 61  ion.  SQLite gua
f5f0: 72 61 6e 74 65 65 64 73 20 74 68 61 74 20 74 68  ranteeds that th
f600: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
f610: 74 20 74 6f 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63  t to.** xRealloc
f620: 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c   is always a val
f630: 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61  ue returned by a
f640: 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 78   prior call to x
f650: 52 6f 75 6e 64 75 70 2e 0a 2a 2a 20 41 6e 64 20  Roundup..** And 
f660: 73 6f 20 69 6e 20 63 61 73 65 73 20 77 68 65 72  so in cases wher
f670: 65 20 78 52 6f 75 6e 64 75 70 20 61 6c 77 61 79  e xRoundup alway
f680: 73 20 72 65 74 75 72 6e 73 20 61 20 70 6f 73 69  s returns a posi
f690: 74 69 76 65 20 6e 75 6d 62 65 72 2c 0a 2a 2a 20  tive number,.** 
f6a0: 78 52 65 61 6c 6c 6f 63 20 63 61 6e 20 70 65 72  xRealloc can per
f6b0: 66 6f 72 6d 20 65 78 61 63 74 6c 79 20 61 73 20  form exactly as 
f6c0: 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62  the standard lib
f6d0: 72 61 72 79 20 72 65 61 6c 6c 6f 63 28 29 20 61  rary realloc() a
f6e0: 6e 64 0a 2a 2a 20 73 74 69 6c 6c 20 62 65 20 69  nd.** still be i
f6f0: 6e 20 63 6f 6d 70 6c 69 61 6e 63 65 20 77 69 74  n compliance wit
f700: 68 20 74 68 69 73 20 73 70 65 63 69 66 69 63 61  h this specifica
f710: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 78 53 69 7a  tion..**.** xSiz
f720: 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20  e should return 
f730: 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69  the allocated si
f740: 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61  ze of a memory a
f750: 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 70 72 65  llocation.** pre
f760: 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64  viously obtained
f770: 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 20 6f 72   from xMalloc or
f780: 20 78 52 65 61 6c 6c 6f 63 2e 20 20 54 68 65 20   xRealloc.  The 
f790: 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 0a 2a  allocated size.*
f7a0: 2a 20 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c  * is always at l
f7b0: 65 61 73 74 20 61 73 20 62 69 67 20 61 73 20 74  east as big as t
f7c0: 68 65 20 72 65 71 75 65 73 74 65 64 20 73 69 7a  he requested siz
f7d0: 65 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72  e but may be lar
f7e0: 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ger..**.** The x
f7f0: 52 6f 75 6e 64 75 70 20 6d 65 74 68 6f 64 20 72  Roundup method r
f800: 65 74 75 72 6e 73 20 77 68 61 74 20 77 6f 75 6c  eturns what woul
f810: 64 20 62 65 20 74 68 65 20 61 6c 6c 6f 63 61 74  d be the allocat
f820: 65 64 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 61 20  ed size of.** a 
f830: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
f840: 6e 20 67 69 76 65 6e 20 61 20 70 61 72 74 69 63  n given a partic
f850: 75 6c 61 72 20 72 65 71 75 65 73 74 65 64 20 73  ular requested s
f860: 69 7a 65 2e 20 20 4d 6f 73 74 20 6d 65 6d 6f 72  ize.  Most memor
f870: 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 73 20  y.** allocators 
f880: 72 6f 75 6e 64 20 75 70 20 6d 65 6d 6f 72 79 20  round up memory 
f890: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 74 20 6c  allocations at l
f8a0: 65 61 73 74 20 74 6f 20 74 68 65 20 6e 65 78 74  east to the next
f8b0: 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 6f 66 20   multiple.** of 
f8c0: 38 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f 63 61 74  8.  Some allocat
f8d0: 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 74 6f 20  ors round up to 
f8e0: 61 20 6c 61 72 67 65 72 20 6d 75 6c 74 69 70 6c  a larger multipl
f8f0: 65 20 6f 72 20 74 6f 20 61 20 70 6f 77 65 72 20  e or to a power 
f900: 6f 66 20 32 2e 0a 2a 2a 20 45 76 65 72 79 20 6d  of 2..** Every m
f910: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
f920: 20 72 65 71 75 65 73 74 20 63 6f 6d 69 6e 67 20   request coming 
f930: 69 6e 20 74 68 72 6f 75 67 68 20 5b 73 71 6c 69  in through [sqli
f940: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0a 2a 2a  te3_malloc()].**
f950: 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61   or [sqlite3_rea
f960: 6c 6c 6f 63 28 29 5d 20 66 69 72 73 74 20 63 61  lloc()] first ca
f970: 6c 6c 73 20 78 52 6f 75 6e 64 75 70 2e 20 20 49  lls xRoundup.  I
f980: 66 20 78 52 6f 75 6e 64 75 70 20 72 65 74 75 72  f xRoundup retur
f990: 6e 73 20 30 2c 20 0a 2a 2a 20 74 68 61 74 20 63  ns 0, .** that c
f9a0: 61 75 73 65 73 20 74 68 65 20 63 6f 72 72 65 73  auses the corres
f9b0: 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61  ponding memory a
f9c0: 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 66 61 69  llocation to fai
f9d0: 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e  l..**.** The xIn
f9e0: 69 74 20 6d 65 74 68 6f 64 20 69 6e 69 74 69 61  it method initia
f9f0: 6c 69 7a 65 73 20 74 68 65 20 6d 65 6d 6f 72 79  lizes the memory
fa00: 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f   allocator.  (Fo
fa10: 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74  r example,.** it
fa20: 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20   might allocate 
fa30: 61 6e 79 20 72 65 71 75 69 72 65 20 6d 75 74 65  any require mute
fa40: 78 65 73 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a  xes or initializ
fa50: 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a  e internal data.
fa60: 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 20  ** structures.  
fa70: 54 68 65 20 78 53 68 75 74 64 6f 77 6e 20 6d 65  The xShutdown me
fa80: 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b 65 64 20  thod is invoked 
fa90: 28 69 6e 64 69 72 65 63 74 6c 79 29 20 62 79 0a  (indirectly) by.
faa0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74  ** [sqlite3_shut
fab0: 64 6f 77 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75  down()] and shou
fac0: 6c 64 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e  ld deallocate an
fad0: 79 20 72 65 73 6f 75 72 63 65 73 20 61 63 71 75  y resources acqu
fae0: 69 72 65 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74  ired.** by xInit
faf0: 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61 20  .  The pAppData 
fb00: 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20  pointer is used 
fb10: 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61  as the only para
fb20: 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69  meter to.** xIni
fb30: 74 20 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e  t and xShutdown.
fb40: 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f  .**.** SQLite ho
fb50: 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f  lds the [SQLITE_
fb60: 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
fb70: 54 45 52 5d 20 6d 75 74 65 78 20 77 68 65 6e 20  TER] mutex when 
fb80: 69 74 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 74 68  it invokes.** th
fb90: 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 2c 20  e xInit method, 
fba0: 73 6f 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74  so the xInit met
fbb0: 68 6f 64 20 6e 65 65 64 20 6e 6f 74 20 62 65 20  hod need not be 
fbc0: 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65  threadsafe.  The
fbd0: 0a 2a 2a 20 78 53 68 75 74 64 6f 77 6e 20 6d 65  .** xShutdown me
fbe0: 74 68 6f 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c  thod is only cal
fbf0: 6c 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  led from [sqlite
fc00: 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f  3_shutdown()] so
fc10: 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20   it does.** not 
fc20: 6e 65 65 64 20 74 6f 20 62 65 20 74 68 72 65 61  need to be threa
fc30: 64 73 61 66 65 20 65 69 74 68 65 72 2e 20 20 46  dsafe either.  F
fc40: 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74  or all other met
fc50: 68 6f 64 73 2c 20 53 51 4c 69 74 65 0a 2a 2a 20  hods, SQLite.** 
fc60: 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54  holds the [SQLIT
fc70: 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
fc80: 45 4d 5d 20 6d 75 74 65 78 20 61 73 20 6c 6f 6e  EM] mutex as lon
fc90: 67 20 61 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c  g as the.** [SQL
fca0: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54  ITE_CONFIG_MEMST
fcb0: 41 54 55 53 5d 20 63 6f 6e 66 69 67 75 72 61 74  ATUS] configurat
fcc0: 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 74 75  ion option is tu
fcd0: 72 6e 65 64 20 6f 6e 20 28 77 68 69 63 68 0a 2a  rned on (which.*
fce0: 2a 20 69 74 20 69 73 20 62 79 20 64 65 66 61 75  * it is by defau
fcf0: 6c 74 29 20 61 6e 64 20 73 6f 20 74 68 65 20 6d  lt) and so the m
fd00: 65 74 68 6f 64 73 20 61 72 65 20 61 75 74 6f 6d  ethods are autom
fd10: 61 74 69 63 61 6c 6c 79 20 73 65 72 69 61 6c 69  atically seriali
fd20: 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  zed..** However,
fd30: 20 69 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46   if [SQLITE_CONF
fd40: 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 20 69 73  IG_MEMSTATUS] is
fd50: 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20   disabled, then 
fd60: 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 74  the other.** met
fd70: 68 6f 64 73 20 6d 75 73 74 20 62 65 20 74 68 72  hods must be thr
fd80: 65 61 64 73 61 66 65 20 6f 72 20 65 6c 73 65 20  eadsafe or else 
fd90: 6d 61 6b 65 20 74 68 65 69 72 20 6f 77 6e 20 61  make their own a
fda0: 72 72 61 6e 67 65 6d 65 6e 74 73 20 66 6f 72 0a  rrangements for.
fdb0: 2a 2a 20 73 65 72 69 61 6c 69 7a 61 74 69 6f 6e  ** serialization
fdc0: 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77  ..**.** SQLite w
fdd0: 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65  ill never invoke
fde0: 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68   xInit() more th
fdf0: 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20  an once without 
fe00: 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a  an intervening.*
fe10: 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64  * call to xShutd
fe20: 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65  own()..*/.typede
fe30: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
fe40: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73 71 6c  _mem_methods sql
fe50: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
fe60: 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ;.struct sqlite3
fe70: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 7b 0a 20  _mem_methods {. 
fe80: 20 76 6f 69 64 20 2a 28 2a 78 4d 61 6c 6c 6f 63   void *(*xMalloc
fe90: 29 28 69 6e 74 29 3b 20 20 20 20 20 20 20 20 20  )(int);         
fea0: 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  /* Memory alloca
feb0: 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  tion function */
fec0: 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 29  .  void (*xFree)
fed0: 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20  (void*);        
fee0: 20 20 2f 2a 20 46 72 65 65 20 61 20 70 72 69 6f    /* Free a prio
fef0: 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a  r allocation */.
ff00: 20 20 76 6f 69 64 20 2a 28 2a 78 52 65 61 6c 6c    void *(*xReall
ff10: 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  oc)(void*,int); 
ff20: 20 2f 2a 20 52 65 73 69 7a 65 20 61 6e 20 61 6c   /* Resize an al
ff30: 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  location */.  in
ff40: 74 20 28 2a 78 53 69 7a 65 29 28 76 6f 69 64 2a  t (*xSize)(void*
ff50: 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  );           /* 
ff60: 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
ff70: 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  of an allocation
ff80: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 52 6f 75   */.  int (*xRou
ff90: 6e 64 75 70 29 28 69 6e 74 29 3b 20 20 20 20 20  ndup)(int);     
ffa0: 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 20 75 70       /* Round up
ffb0: 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f   request size to
ffc0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
ffd0: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69   */.  int (*xIni
ffe0: 74 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20  t)(void*);      
fff0: 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69       /* Initiali
10000 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ze the memory al
10010 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69  locator */.  voi
10020 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76  d (*xShutdown)(v
10030 6f 69 64 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44  oid*);      /* D
10040 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  einitialize the 
10050 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
10060 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 70 70   */.  void *pApp
10070 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
10080 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
10090 20 74 6f 20 78 49 6e 69 74 28 29 20 61 6e 64 20   to xInit() and 
100a0 78 53 68 75 74 64 6f 77 6e 28 29 20 2a 2f 0a 7d  xShutdown() */.}
100b0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
100c0 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e  F: Configuration
100d0 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 36 30   Options {H10160
100e0 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58  } <S20000>.** EX
100f0 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
10100 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73   These constants
10110 20 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62   are the availab
10120 6c 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69  le integer confi
10130 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73  guration options
10140 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20   that.** can be 
10150 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
10160 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
10170 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  the [sqlite3_con
10180 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65  fig()] interface
10190 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66  ..**.** New conf
101a0 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
101b0 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69  s may be added i
101c0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
101d0 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20  s of SQLite..** 
101e0 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75  Existing configu
101f0 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d  ration options m
10200 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69  ight be disconti
10210 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69  nued.  Applicati
10220 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68  ons.** should ch
10230 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63  eck the return c
10240 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  ode from [sqlite
10250 33 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f 20 6d  3_config()] to m
10260 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a  ake sure that.**
10270 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64   the call worked
10280 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  .  The [sqlite3_
10290 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66  config()] interf
102a0 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ace will return 
102b0 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65  a.** non-zero [e
102c0 72 72 6f 72 20 63 6f 64 65 5d 20 69 66 20 61 20  rror code] if a 
102d0 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20  discontinued or 
102e0 75 6e 73 75 70 70 6f 72 74 65 64 20 63 6f 6e 66  unsupported conf
102f0 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
10300 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a  .** is invoked..
10310 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64  **.** <dl>.** <d
10320 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
10330 53 49 4e 47 4c 45 54 48 52 45 41 44 3c 2f 64 74  SINGLETHREAD</dt
10340 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61  >.** <dd>There a
10350 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20  re no arguments 
10360 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20  to this option. 
10370 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 73   This option dis
10380 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74  ables.** all mut
10390 65 78 69 6e 67 20 61 6e 64 20 70 75 74 73 20 53  exing and puts S
103a0 51 4c 69 74 65 20 69 6e 74 6f 20 61 20 6d 6f 64  QLite into a mod
103b0 65 20 77 68 65 72 65 20 69 74 20 63 61 6e 20 6f  e where it can o
103c0 6e 6c 79 20 62 65 20 75 73 65 64 0a 2a 2a 20 62  nly be used.** b
103d0 79 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61  y a single threa
103e0 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  d.</dd>.**.** <d
103f0 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
10400 4d 55 4c 54 49 54 48 52 45 41 44 3c 2f 64 74 3e  MULTITHREAD</dt>
10410 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72  .** <dd>There ar
10420 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74  e no arguments t
10430 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20  o this option.  
10440 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 73 61  This option disa
10450 62 6c 65 73 0a 2a 2a 20 6d 75 74 65 78 69 6e 67  bles.** mutexing
10460 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f   on [database co
10470 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70  nnection] and [p
10480 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
10490 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 54  t] objects..** T
104a0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69  he application i
104b0 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
104c0 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 63  r serializing ac
104d0 63 65 73 73 20 74 6f 0a 2a 2a 20 5b 64 61 74 61  cess to.** [data
104e0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
104f0 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20  ] and [prepared 
10500 73 74 61 74 65 6d 65 6e 74 73 5d 2e 20 20 42 75  statements].  Bu
10510 74 20 6f 74 68 65 72 20 6d 75 74 65 78 65 73 0a  t other mutexes.
10520 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20 73  ** are enabled s
10530 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69  o that SQLite wi
10540 6c 6c 20 62 65 20 73 61 66 65 20 74 6f 20 75 73  ll be safe to us
10550 65 20 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 72  e in a multi-thr
10560 65 61 64 65 64 0a 2a 2a 20 65 6e 76 69 72 6f 6e  eaded.** environ
10570 6d 65 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20  ment as long as 
10580 6e 6f 20 74 77 6f 20 74 68 72 65 61 64 73 20 61  no two threads a
10590 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68  ttempt to use th
105a0 65 20 73 61 6d 65 0a 2a 2a 20 5b 64 61 74 61 62  e same.** [datab
105b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
105c0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
105d0 2e 20 20 53 65 65 20 74 68 65 20 5b 74 68 72 65  .  See the [thre
105e0 61 64 69 6e 67 20 6d 6f 64 65 5d 0a 2a 2a 20 64  ading mode].** d
105f0 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  ocumentation for
10600 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
10610 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a  rmation.</dd>.**
10620 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
10630 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44  ONFIG_SERIALIZED
10640 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
10650 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65  re are no argume
10660 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69  nts to this opti
10670 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e  on.  This option
10680 20 65 6e 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20   enables.** all 
10690 6d 75 74 65 78 65 73 20 69 6e 63 6c 75 64 69 6e  mutexes includin
106a0 67 20 74 68 65 20 72 65 63 75 72 73 69 76 65 0a  g the recursive.
106b0 2a 2a 20 6d 75 74 65 78 65 73 20 6f 6e 20 5b 64  ** mutexes on [d
106c0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
106d0 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65  on] and [prepare
106e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a  d statement] obj
106f0 65 63 74 73 2e 0a 2a 2a 20 49 6e 20 74 68 69 73  ects..** In this
10700 20 6d 6f 64 65 20 28 77 68 69 63 68 20 69 73 20   mode (which is 
10710 74 68 65 20 64 65 66 61 75 6c 74 20 77 68 65 6e  the default when
10720 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
10730 6c 65 64 20 77 69 74 68 0a 2a 2a 20 5b 53 51 4c  led with.** [SQL
10740 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31  ITE_THREADSAFE=1
10750 5d 29 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  ]) the SQLite li
10760 62 72 61 72 79 20 77 69 6c 6c 20 69 74 73 65 6c  brary will itsel
10770 66 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63 65  f serialize acce
10780 73 73 0a 2a 2a 20 74 6f 20 5b 64 61 74 61 62 61  ss.** to [databa
10790 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20  se connections] 
107a0 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 74  and [prepared st
107b0 61 74 65 6d 65 6e 74 73 5d 20 73 6f 20 74 68 61  atements] so tha
107c0 74 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61  t the.** applica
107d0 74 69 6f 6e 20 69 73 20 66 72 65 65 20 74 6f 20  tion is free to 
107e0 75 73 65 20 74 68 65 20 73 61 6d 65 20 5b 64 61  use the same [da
107f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
10800 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d  n] or the.** sam
10810 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
10820 65 6d 65 6e 74 5d 20 69 6e 20 64 69 66 66 65 72  ement] in differ
10830 65 6e 74 20 74 68 72 65 61 64 73 20 61 74 20 74  ent threads at t
10840 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a  he same time..**
10850 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 64   See the [thread
10860 69 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65  ing mode] docume
10870 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69  ntation for addi
10880 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
10890 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  on.</dd>.**.** <
108a0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
108b0 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20  _MALLOC</dt>.** 
108c0 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
108d0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
108e0 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73  rgument which is
108f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
10900 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
10910 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d  the [sqlite3_mem
10920 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74  _methods] struct
10930 75 72 65 2e 20 20 54 68 65 20 61 72 67 75 6d 65  ure.  The argume
10940 6e 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20  nt specifies.** 
10950 61 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d  alternative low-
10960 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
10970 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73  ocation routines
10980 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70   to be used in p
10990 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6d  lace of.** the m
109a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
109b0 20 72 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20   routines built 
109c0 69 6e 74 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64  into SQLite.</dd
109d0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
109e0 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c  TE_CONFIG_GETMAL
109f0 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  LOC</dt>.** <dd>
10a00 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65  This option take
10a10 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d  s a single argum
10a20 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 70  ent which is a p
10a30 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20  ointer to an.** 
10a40 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
10a50 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74  [sqlite3_mem_met
10a60 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e  hods] structure.
10a70 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d    The [sqlite3_m
10a80 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73  em_methods].** s
10a90 74 72 75 63 74 75 72 65 20 69 73 20 66 69 6c 6c  tructure is fill
10aa0 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72 72  ed with the curr
10ab0 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d 65  ently defined me
10ac0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
10ad0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69  routines..** Thi
10ae0 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20  s option can be 
10af0 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64  used to overload
10b00 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d   the default mem
10b10 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  ory allocation.*
10b20 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74 68 20  * routines with 
10b30 61 20 77 72 61 70 70 65 72 20 74 68 61 74 20 73  a wrapper that s
10b40 69 6d 75 6c 61 74 69 6f 6e 73 20 6d 65 6d 6f 72  imulations memor
10b50 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  y allocation fai
10b60 6c 75 72 65 20 6f 72 0a 2a 2a 20 74 72 61 63 6b  lure or.** track
10b70 73 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 2c 20  s memory usage, 
10b80 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64  for example.</dd
10b90 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
10ba0 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41  TE_CONFIG_MEMSTA
10bb0 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TUS</dt>.** <dd>
10bc0 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65  This option take
10bd0 73 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e  s single argumen
10be0 74 20 6f 66 20 74 79 70 65 20 69 6e 74 2c 20 69  t of type int, i
10bf0 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20  nterpreted as a 
10c00 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c 20 77 68 69  .** boolean, whi
10c10 63 68 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69  ch enables or di
10c20 73 61 62 6c 65 73 20 74 68 65 20 63 6f 6c 6c 65  sables the colle
10c30 63 74 69 6f 6e 20 6f 66 20 6d 65 6d 6f 72 79 20  ction of memory 
10c40 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 73  allocation .** s
10c50 74 61 74 69 73 74 69 63 73 2e 20 57 68 65 6e 20  tatistics. When 
10c60 64 69 73 61 62 6c 65 64 2c 20 74 68 65 20 66 6f  disabled, the fo
10c70 6c 6c 6f 77 69 6e 67 20 53 51 4c 69 74 65 20 69  llowing SQLite i
10c80 6e 74 65 72 66 61 63 65 73 20 62 65 63 6f 6d 65  nterfaces become
10c90 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65 72 61 74 69   .** non-operati
10ca0 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c 75 6c 3e 0a  onal:.**   <ul>.
10cb0 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74  **   <li> [sqlit
10cc0 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29  e3_memory_used()
10cd0 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c  ].**   <li> [sql
10ce0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68  ite3_memory_high
10cf0 77 61 74 65 72 28 29 5d 0a 2a 2a 20 20 20 3c 6c  water()].**   <l
10d00 69 3e 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74  i> [sqlite3_soft
10d10 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 5d 0a 2a  _heap_limit()].*
10d20 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65  *   <li> [sqlite
10d30 33 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2a 20 20  3_status()].**  
10d40 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f 64 64 3e 0a   </ul>.** </dd>.
10d50 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
10d60 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3c  _CONFIG_SCRATCH<
10d70 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
10d80 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65   option specifie
10d90 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72  s a static memor
10da0 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51  y buffer that SQ
10db0 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f 72  Lite can use for
10dc0 0a 2a 2a 20 73 63 72 61 74 63 68 20 6d 65 6d 6f  .** scratch memo
10dd0 72 79 2e 20 20 54 68 65 72 65 20 61 72 65 20 74  ry.  There are t
10de0 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20  hree arguments: 
10df0 20 41 20 70 6f 69 6e 74 65 72 20 61 6e 20 38 2d   A pointer an 8-
10e00 62 79 74 65 0a 2a 2a 20 61 6c 69 67 6e 65 64 20  byte.** aligned 
10e10 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 66 72  memory buffer fr
10e20 6f 6d 20 77 68 69 63 68 20 74 68 65 20 73 63 72  om which the scr
10e30 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ach allocations 
10e40 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 72 61 77 6e  will be.** drawn
10e50 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61  , the size of ea
10e60 63 68 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63  ch scratch alloc
10e70 61 74 69 6f 6e 20 28 73 7a 29 2c 0a 2a 2a 20 61  ation (sz),.** a
10e80 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  nd the maximum n
10e90 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74 63 68  umber of scratch
10ea0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 28 4e 29   allocations (N)
10eb0 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 61 72 67  .  The sz.** arg
10ec0 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 61 20  ument must be a 
10ed0 6d 75 6c 74 69 70 6c 65 20 6f 66 20 31 36 2e 20  multiple of 16. 
10ee0 54 68 65 20 73 7a 20 70 61 72 61 6d 65 74 65 72  The sz parameter
10ef0 20 73 68 6f 75 6c 64 20 62 65 20 61 20 66 65 77   should be a few
10f00 20 62 79 74 65 73 0a 2a 2a 20 6c 61 72 67 65 72   bytes.** larger
10f10 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c   than the actual
10f20 20 73 63 72 61 74 63 68 20 73 70 61 63 65 20 72   scratch space r
10f30 65 71 75 69 72 65 64 20 64 75 65 20 74 6f 20 69  equired due to i
10f40 6e 74 65 72 6e 61 6c 20 6f 76 65 72 68 65 61 64  nternal overhead
10f50 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  ..** The first a
10f60 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70  rgument should p
10f70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 38 2d 62  ointer to an 8-b
10f80 79 74 65 20 61 6c 69 67 6e 65 64 20 62 75 66 66  yte aligned buff
10f90 65 72 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 61 73  er.** of at leas
10fa0 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20  t sz*N bytes of 
10fb0 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74  memory..** SQLit
10fc0 65 20 77 69 6c 6c 20 75 73 65 20 6e 6f 20 6d 6f  e will use no mo
10fd0 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61  re than one scra
10fe0 74 63 68 20 62 75 66 66 65 72 20 61 74 20 6f 6e  tch buffer at on
10ff0 63 65 20 70 65 72 20 74 68 72 65 61 64 2c 20 73  ce per thread, s
11000 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c 64 20 62 65  o.** N should be
11010 20 73 65 74 20 74 6f 20 74 68 65 20 65 78 70 65   set to the expe
11020 63 74 65 64 20 6d 61 78 69 6d 75 6d 20 6e 75 6d  cted maximum num
11030 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 2e 20  ber of threads. 
11040 20 54 68 65 20 73 7a 0a 2a 2a 20 70 61 72 61 6d   The sz.** param
11050 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 36  eter should be 6
11060 20 74 69 6d 65 73 20 74 68 65 20 73 69 7a 65 20   times the size 
11070 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 64  of the largest d
11080 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a  atabase page siz
11090 65 2e 0a 2a 2a 20 53 63 72 61 74 63 68 20 62 75  e..** Scratch bu
110a0 66 66 65 72 73 20 61 72 65 20 75 73 65 64 20 61  ffers are used a
110b0 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 62 74  s part of the bt
110c0 72 65 65 20 62 61 6c 61 6e 63 65 20 6f 70 65 72  ree balance oper
110d0 61 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 54 68  ation.  If.** Th
110e0 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 65 72  e btree balancer
110f0 20 6e 65 65 64 73 20 61 64 64 69 74 69 6f 6e 61   needs additiona
11100 6c 20 6d 65 6d 6f 72 79 20 62 65 79 6f 6e 64 20  l memory beyond 
11110 77 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 64  what is provided
11120 20 62 79 0a 2a 2a 20 73 63 72 61 74 63 68 20 62   by.** scratch b
11130 75 66 66 65 72 73 20 6f 72 20 69 66 20 6e 6f 20  uffers or if no 
11140 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 73  scratch buffer s
11150 70 61 63 65 20 69 73 20 73 70 65 63 69 66 69 65  pace is specifie
11160 64 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a 2a  d, then SQLite.*
11170 2a 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74  * goes to [sqlit
11180 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 74 6f 20  e3_malloc()] to 
11190 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72  obtain the memor
111a0 79 20 69 74 20 6e 65 65 64 73 2e 3c 2f 64 64 3e  y it needs.</dd>
111b0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
111c0 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
111d0 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  HE</dt>.** <dd>T
111e0 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69  his option speci
111f0 66 69 65 73 20 61 20 73 74 61 74 69 63 20 6d 65  fies a static me
11200 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 61 74  mory buffer that
11210 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65 20   SQLite can use 
11220 66 6f 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  for.** the datab
11230 61 73 65 20 70 61 67 65 20 63 61 63 68 65 20 77  ase page cache w
11240 69 74 68 20 74 68 65 20 64 65 66 61 75 6c 74 20  ith the default 
11250 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65  page cache imple
11260 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a 2a 2a 20 54  menation.  .** T
11270 68 69 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  his configuratio
11280 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  n should not be 
11290 75 73 65 64 20 69 66 20 61 6e 20 61 70 70 6c 69  used if an appli
112a0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 20 70 61  cation-define pa
112b0 67 65 0a 2a 2a 20 63 61 63 68 65 20 69 6d 70 6c  ge.** cache impl
112c0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6c 6f  ementation is lo
112d0 61 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 53  aded using the S
112e0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41  QLITE_CONFIG_PCA
112f0 43 48 45 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 54  CHE option..** T
11300 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 61  here are three a
11310 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73  rguments to this
11320 20 6f 70 74 69 6f 6e 3a 20 41 20 70 6f 69 6e 74   option: A point
11330 65 72 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69  er to 8-byte ali
11340 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20  gned.** memory, 
11350 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68  the size of each
11360 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73 7a   page buffer (sz
11370 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65  ), and the numbe
11380 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0a  r of pages (N)..
11390 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d 65  ** The sz argume
113a0 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  nt should be the
113b0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72   size of the lar
113c0 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70 61  gest database pa
113d0 67 65 0a 2a 2a 20 28 61 20 70 6f 77 65 72 20 6f  ge.** (a power o
113e0 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31  f two between 51
113f0 32 20 61 6e 64 20 33 32 37 36 38 29 20 70 6c 75  2 and 32768) plu
11400 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61  s a little extra
11410 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 70 61 67   for each.** pag
11420 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 20 70  e header.  The p
11430 61 67 65 20 68 65 61 64 65 72 20 73 69 7a 65 20  age header size 
11440 69 73 20 32 30 20 74 6f 20 34 30 20 62 79 74 65  is 20 to 40 byte
11450 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 0a 2a  s depending on.*
11460 2a 20 74 68 65 20 68 6f 73 74 20 61 72 63 68 69  * the host archi
11470 74 65 63 74 75 72 65 2e 20 20 49 74 20 69 73 20  tecture.  It is 
11480 68 61 72 6d 6c 65 73 73 2c 20 61 70 61 72 74 20  harmless, apart 
11490 66 72 6f 6d 20 74 68 65 20 77 61 73 74 65 64 20  from the wasted 
114a0 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 6f 20 6d 61  memory,.** to ma
114b0 6b 65 20 73 7a 20 61 20 6c 69 74 74 6c 65 20 74  ke sz a little t
114c0 6f 6f 20 6c 61 72 67 65 2e 20 20 54 68 65 20 66  oo large.  The f
114d0 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  irst.** argument
114e0 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f   should point to
114f0 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f   an allocation o
11500 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20  f at least sz*N 
11510 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e  bytes of memory.
11520 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
11530 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70  use the memory p
11540 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 66  rovided by the f
11550 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
11560 20 73 61 74 69 73 66 79 20 69 74 73 0a 2a 2a 20   satisfy its.** 
11570 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 66 6f 72  memory needs for
11580 20 74 68 65 20 66 69 72 73 74 20 4e 20 70 61 67   the first N pag
11590 65 73 20 74 68 61 74 20 69 74 20 61 64 64 73 20  es that it adds 
115a0 74 6f 20 63 61 63 68 65 2e 20 20 49 66 20 61 64  to cache.  If ad
115b0 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61 67 65  ditional.** page
115c0 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 69 73   cache memory is
115d0 20 6e 65 65 64 65 64 20 62 65 79 6f 6e 64 20 77   needed beyond w
115e0 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 20  hat is provided 
115f0 62 79 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20  by this option, 
11600 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 67  then.** SQLite g
11610 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  oes to [sqlite3_
11620 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 74 68  malloc()] for th
11630 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 6f  e additional sto
11640 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2a 20 54  rage space..** T
11650 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
11660 6e 20 6d 69 67 68 74 20 75 73 65 20 6f 6e 65 20  n might use one 
11670 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 4e  or more of the N
11680 20 62 75 66 66 65 72 73 20 74 6f 20 68 6f 6c 64   buffers to hold
11690 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 63 63 6f   .** memory acco
116a0 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  unting informati
116b0 6f 6e 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  on. The pointer 
116c0 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67  in the first arg
116d0 75 6d 65 6e 74 20 6d 75 73 74 0a 2a 2a 20 62 65  ument must.** be
116e0 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38   aligned to an 8
116f0 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 6f  -byte boundary o
11700 72 20 73 75 62 73 65 71 75 65 6e 74 20 62 65 68  r subsequent beh
11710 61 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 0a  avior of SQLite.
11720 2a 2a 20 77 69 6c 6c 20 62 65 20 75 6e 64 65 66  ** will be undef
11730 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ined.</dd>.**.**
11740 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
11750 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a 2a 20  IG_HEAP</dt>.** 
11760 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
11770 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74  specifies a stat
11780 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72  ic memory buffer
11790 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c   that SQLite wil
117a0 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c  l use.** for all
117b0 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 20   of its dynamic 
117c0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
117d0 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e 64 20 74  n needs beyond t
117e0 68 6f 73 65 20 70 72 6f 76 69 64 65 64 0a 2a 2a  hose provided.**
117f0 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 54 45 5f   for by [SQLITE_
11800 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20  CONFIG_SCRATCH] 
11810 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  and [SQLITE_CONF
11820 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 0a 2a  IG_PAGECACHE]..*
11830 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65  * There are thre
11840 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 41 6e 20  e arguments: An 
11850 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 70  8-byte aligned p
11860 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65  ointer to the me
11870 6d 6f 72 79 2c 0a 2a 2a 20 74 68 65 20 6e 75 6d  mory,.** the num
11880 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
11890 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 66 66 65  the memory buffe
118a0 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e 69 6d  r, and the minim
118b0 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69  um allocation si
118c0 7a 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  ze..** If the fi
118d0 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65  rst pointer (the
118e0 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29   memory pointer)
118f0 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53   is NULL, then S
11900 51 4c 69 74 65 20 72 65 76 65 72 74 73 0a 2a 2a  QLite reverts.**
11910 20 74 6f 20 75 73 69 6e 67 20 69 74 73 20 64 65   to using its de
11920 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c  fault memory all
11930 6f 63 61 74 6f 72 20 28 74 68 65 20 73 79 73 74  ocator (the syst
11940 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c  em malloc() impl
11950 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a 2a 20  ementation),.** 
11960 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72 69 6f  undoing any prio
11970 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  r invocation of 
11980 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
11990 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 68 65 0a  ALLOC].  If the.
119a0 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65  ** memory pointe
119b0 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e  r is not NULL an
119c0 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45  d either [SQLITE
119d0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 5d  _ENABLE_MEMSYS3]
119e0 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45   or.** [SQLITE_E
119f0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 5d 20 61  NABLE_MEMSYS5] a
11a00 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e  re defined, then
11a10 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65   the alternative
11a20 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63   memory.** alloc
11a30 61 74 6f 72 20 69 73 20 65 6e 67 61 67 65 64 20  ator is engaged 
11a40 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c 20 6f 66  to handle all of
11a50 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f 72 79 20   SQLites memory 
11a60 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73  allocation needs
11a70 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70  ..** The first p
11a80 6f 69 6e 74 65 72 20 28 74 68 65 20 6d 65 6d 6f  ointer (the memo
11a90 72 79 20 70 6f 69 6e 74 65 72 29 20 6d 75 73 74  ry pointer) must
11aa0 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61   be aligned to a
11ab0 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 62 6f 75 6e  n 8-byte.** boun
11ac0 64 61 72 79 20 6f 72 20 73 75 62 73 65 71 75 65  dary or subseque
11ad0 6e 74 20 62 65 68 61 76 69 6f 72 20 6f 66 20 53  nt behavior of S
11ae0 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 75 6e  QLite will be un
11af0 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a  defined.</dd>.**
11b00 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
11b10 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74 3e  ONFIG_MUTEX</dt>
11b20 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
11b30 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
11b40 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
11b50 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
11b60 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  o an.** instance
11b70 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
11b80 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20  _mutex_methods] 
11b90 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
11ba0 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69  argument specifi
11bb0 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76  es.** alternativ
11bc0 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65  e low-level mute
11bd0 78 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65  x routines to be
11be0 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 0a 2a   used in place.*
11bf0 2a 20 74 68 65 20 6d 75 74 65 78 20 72 6f 75 74  * the mutex rout
11c00 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 6f 20  ines built into 
11c10 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a  SQLite.</dd>.**.
11c20 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
11c30 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64  NFIG_GETMUTEX</d
11c40 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
11c50 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ption takes a si
11c60 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68  ngle argument wh
11c70 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ich is a pointer
11c80 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   to an.** instan
11c90 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ce of the [sqlit
11ca0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
11cb0 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  ] structure.  Th
11cc0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 75  e.** [sqlite3_mu
11cd0 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20  tex_methods].** 
11ce0 73 74 72 75 63 74 75 72 65 20 69 73 20 66 69 6c  structure is fil
11cf0 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72  led with the cur
11d00 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d  rently defined m
11d10 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  utex routines..*
11d20 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61  * This option ca
11d30 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65  n be used to ove
11d40 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c  rload the defaul
11d50 74 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 69  t mutex allocati
11d60 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77  on.** routines w
11d70 69 74 68 20 61 20 77 72 61 70 70 65 72 20 75 73  ith a wrapper us
11d80 65 64 20 74 6f 20 74 72 61 63 6b 20 6d 75 74 65  ed to track mute
11d90 78 20 75 73 61 67 65 20 66 6f 72 20 70 65 72 66  x usage for perf
11da0 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 69  ormance.** profi
11db0 6c 69 6e 67 20 6f 72 20 74 65 73 74 69 6e 67 2c  ling or testing,
11dc0 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64   for example.</d
11dd0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
11de0 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  ITE_CONFIG_LOOKA
11df0 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  SIDE</dt>.** <dd
11e00 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b  >This option tak
11e10 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  es two arguments
11e20 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20   that determine 
11e30 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d  the default.** m
11e40 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
11e50 20 6c 6f 6f 6b 61 73 69 64 65 20 6f 70 74 69 6d   lookaside optim
11e60 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 66 69  ization.  The fi
11e70 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
11e80 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65  the.** size of e
11e90 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  ach lookaside bu
11ea0 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68  ffer slot and th
11eb0 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20  e second is the 
11ec0 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f  number of.** slo
11ed0 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  ts allocated to 
11ee0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f  each database co
11ef0 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 20  nnection.  This 
11f00 6f 70 74 69 6f 6e 20 73 65 74 73 20 74 68 65 0a  option sets the.
11f10 2a 2a 20 3c 69 3e 64 65 66 61 75 6c 74 3c 2f 69  ** <i>default</i
11f20 3e 20 6c 6f 6f 6b 61 73 69 64 65 20 73 69 7a 65  > lookaside size
11f30 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 44  .  The [SQLITE_D
11f40 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44  BCONFIG_LOOKASID
11f50 45 5d 0a 2a 2a 20 76 65 72 62 20 74 6f 20 5b 73  E].** verb to [s
11f60 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
11f70 28 29 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20  ()] can be used 
11f80 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 6c 6f  to change the lo
11f90 6f 6b 61 73 69 64 65 0a 2a 2a 20 63 6f 6e 66 69  okaside.** confi
11fa0 67 75 72 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69  guration on indi
11fb0 76 69 64 75 61 6c 20 63 6f 6e 6e 65 63 74 69 6f  vidual connectio
11fc0 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ns.</dd>.**.** <
11fd0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
11fe0 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20  _PCACHE</dt>.** 
11ff0 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
12000 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
12010 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73  rgument which is
12020 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
12030 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 61   an [sqlite3_pca
12040 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  che_methods] obj
12050 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 63  ect.  This objec
12060 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  t specifies the 
12070 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20  interface.** to 
12080 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61  a custom page ca
12090 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
120a0 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b 65  on.  SQLite make
120b0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a  s a copy of the.
120c0 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73  ** object and us
120d0 65 73 20 69 74 20 66 6f 72 20 70 61 67 65 20 63  es it for page c
120e0 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ache memory allo
120f0 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a  cations.</dd>.**
12100 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
12110 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 3c  ONFIG_GETPCACHE<
12120 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
12130 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
12140 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
12150 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
12160 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c  er to an.** [sql
12170 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
12180 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 51  ods] object.  SQ
12190 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66 20 74  Lite copies of t
121a0 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 61  he current.** pa
121b0 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  ge cache impleme
121c0 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 61  ntation into tha
121d0 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a 2a  t object.</dd>.*
121e0 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64  *.** </dl>.*/.#d
121f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
12200 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
12210 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23    1  /* nil */.#
12220 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
12230 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44  NFIG_MULTITHREAD
12240 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a     2  /* nil */.
12250 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
12260 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44  ONFIG_SERIALIZED
12270 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f      3  /* nil */
12280 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
12290 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20  CONFIG_MALLOC   
122a0 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74       4  /* sqlit
122b0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20  e3_mem_methods* 
122c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
122d0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c  E_CONFIG_GETMALL
122e0 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c  OC     5  /* sql
122f0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
12300 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  * */.#define SQL
12310 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
12320 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76  CH       6  /* v
12330 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e  oid*, int sz, in
12340 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  t N */.#define S
12350 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
12360 45 43 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a  ECACHE     7  /*
12370 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20   void*, int sz, 
12380 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65  int N */.#define
12390 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48   SQLITE_CONFIG_H
123a0 45 41 50 20 20 20 20 20 20 20 20 20 20 38 20 20  EAP          8  
123b0 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42  /* void*, int nB
123c0 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a  yte, int min */.
123d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
123e0 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20  ONFIG_MEMSTATUS 
123f0 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61      9  /* boolea
12400 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  n */.#define SQL
12410 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
12420 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73          10  /* s
12430 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
12440 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65  hods* */.#define
12450 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47   SQLITE_CONFIG_G
12460 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 20 20  ETMUTEX     11  
12470 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  /* sqlite3_mutex
12480 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20  _methods* */./* 
12490 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54  previously SQLIT
124a0 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c  E_CONFIG_CHUNKAL
124b0 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 73 20  LOC 12 which is 
124c0 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a  now unused. */ .
124d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
124e0 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20  ONFIG_LOOKASIDE 
124f0 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e     13  /* int in
12500 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  t */.#define SQL
12510 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48  ITE_CONFIG_PCACH
12520 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a 20 73  E       14  /* s
12530 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
12540 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e  thods* */.#defin
12550 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
12560 47 45 54 50 43 41 43 48 45 20 20 20 20 31 35 20  GETPCACHE    15 
12570 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63   /* sqlite3_pcac
12580 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 0a  he_methods* */..
12590 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
125a0 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70  Configuration Op
125b0 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c  tions {H10170} <
125c0 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S20000>.** EXPER
125d0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
125e0 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  ese constants ar
125f0 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20  e the available 
12600 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72  integer configur
12610 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68  ation options th
12620 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73  at.** can be pas
12630 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
12640 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
12650 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f  e [sqlite3_db_co
12660 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63  nfig()] interfac
12670 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e  e..**.** New con
12680 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
12690 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20  ns may be added 
126a0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
126b0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a  es of SQLite..**
126c0 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67   Existing config
126d0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20  uration options 
126e0 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74  might be discont
126f0 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74  inued.  Applicat
12700 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63  ions.** should c
12710 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20  heck the return 
12720 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74  code from [sqlit
12730 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20  e3_db_config()] 
12740 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
12750 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f  t.** the call wo
12760 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69  rked.  The [sqli
12770 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d  te3_db_config()]
12780 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20   interface will 
12790 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d  return a.** non-
127a0 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65  zero [error code
127b0 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e  ] if a discontin
127c0 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  ued or unsupport
127d0 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ed configuration
127e0 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e   option.** is in
127f0 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c  voked..**.** <dl
12800 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  >.** <dt>SQLITE_
12810 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49  DBCONFIG_LOOKASI
12820 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  DE</dt>.** <dd>T
12830 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73  his option takes
12840 20 74 68 72 65 65 20 61 64 64 69 74 69 6f 6e 61   three additiona
12850 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74  l arguments that
12860 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 0a   determine the .
12870 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  ** [lookaside me
12880 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20  mory allocator] 
12890 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f  configuration fo
128a0 72 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  r the [database 
128b0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20  connection]..** 
128c0 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
128d0 6e 74 20 28 74 68 65 20 74 68 69 72 64 20 70 61  nt (the third pa
128e0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
128f0 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d  te3_db_config()]
12900 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72   is a.** pointer
12910 20 74 6f 20 61 6e 20 6d 65 6d 6f 72 79 20 62 75   to an memory bu
12920 66 66 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20  ffer to use for 
12930 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
12940 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  ..** The first a
12950 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e  rgument may be N
12960 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73  ULL in which cas
12970 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c  e SQLite will al
12980 6c 6f 63 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f  locate the.** lo
12990 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 69  okaside buffer i
129a0 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c  tself using [sql
129b0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20  ite3_malloc()]. 
129c0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
129d0 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73  ment is the.** s
129e0 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b  ize of each look
129f0 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f  aside buffer slo
12a00 74 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20  t and the third 
12a10 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
12a20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f  number of.** slo
12a30 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66  ts.  The size of
12a40 20 74 68 65 20 62 75 66 66 65 72 20 69 6e 20 74   the buffer in t
12a50 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
12a60 74 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65  t must be greate
12a70 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75  r than.** or equ
12a80 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64 75 63  al to the produc
12a90 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20  t of the second 
12aa0 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 6d 65  and third argume
12ab0 6e 74 73 2e 20 20 54 68 65 20 62 75 66 66 65 72  nts.  The buffer
12ac0 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 6c 69 67  .** must be alig
12ad0 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65  ned to an 8-byte
12ae0 20 62 6f 75 6e 64 61 72 79 2e 20 20 49 66 20 74   boundary.  If t
12af0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
12b00 6e 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 6d  nt is not.** a m
12b10 75 6c 74 69 70 6c 65 20 6f 66 20 38 2c 20 69 74  ultiple of 8, it
12b20 20 69 73 20 69 6e 74 65 72 6e 61 6c 6c 79 20 72   is internally r
12b30 6f 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 74  ounded down to t
12b40 68 65 20 6e 65 78 74 20 73 6d 61 6c 6c 65 72 0a  he next smaller.
12b50 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38  ** multiple of 8
12b60 2e 20 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51  .  See also: [SQ
12b70 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b  LITE_CONFIG_LOOK
12b80 41 53 49 44 45 5d 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ASIDE]</dd>.**.*
12b90 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69  * </dl>.*/.#defi
12ba0 6e 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46  ne SQLITE_DBCONF
12bb0 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20  IG_LOOKASIDE    
12bc0 31 30 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69  1001  /* void* i
12bd0 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  nt int */.../*.*
12be0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62  * CAPI3REF: Enab
12bf0 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78  le Or Disable Ex
12c00 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f  tended Result Co
12c10 64 65 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31  des {H12200} <S1
12c20 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0700>.**.** The 
12c30 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
12c40 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20  _result_codes() 
12c50 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20  routine enables 
12c60 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a  or disables the.
12c70 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73  ** [extended res
12c80 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75  ult codes] featu
12c90 72 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68  re of SQLite. Th
12ca0 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  e extended resul
12cb0 74 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64  t.** codes are d
12cc0 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75  isabled by defau
12cd0 6c 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61  lt for historica
12ce0 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  l compatibility 
12cf0 63 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a  considerations..
12d00 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
12d10 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 5d 20  ts:.** [H12201] 
12d20 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 4c 49  [H12202].*/.SQLI
12d30 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
12d40 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75  e3_extended_resu
12d50 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33  lt_codes(sqlite3
12d60 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a  *, int onoff);..
12d70 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
12d80 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69  Last Insert Rowi
12d90 64 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37  d {H12220} <S107
12da0 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65  00>.**.** Each e
12db0 6e 74 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74  ntry in an SQLit
12dc0 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e  e table has a un
12dd0 69 71 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e  ique 64-bit sign
12de0 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65  ed.** integer ke
12df0 79 20 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f  y called the [RO
12e00 57 49 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20  WID | "rowid"]. 
12e10 54 68 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77  The rowid is alw
12e20 61 79 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a  ays available.**
12e30 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65   as an undeclare
12e40 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52  d column named R
12e50 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52  OWID, OID, or _R
12e60 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73  OWID_ as long as
12e70 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20   those.** names 
12e80 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65  are not also use
12e90 64 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20  d by explicitly 
12ea0 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73  declared columns
12eb0 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  . If.** the tabl
12ec0 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f  e has a column o
12ed0 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20  f type [INTEGER 
12ee0 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65  PRIMARY KEY] the
12ef0 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a  n that column.**
12f00 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61   is another alia
12f10 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e  s for the rowid.
12f20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
12f30 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
12f40 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d  [rowid] of the m
12f50 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75  ost recent.** su
12f60 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54  ccessful [INSERT
12f70 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  ] into the datab
12f80 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61  ase from the [da
12f90 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
12fa0 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72  n].** in the fir
12fb0 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66  st argument.  If
12fc0 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b   no successful [
12fd0 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65  INSERT]s.** have
12fe0 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f   ever occurred o
12ff0 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20  n that database 
13000 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f  connection, zero
13010 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
13020 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52  .** If an [INSER
13030 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  T] occurs within
13040 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e   a trigger, then
13050 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20   the [rowid] of 
13060 74 68 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20  the inserted.** 
13070 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20  row is returned 
13080 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  by this routine 
13090 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74  as long as the t
130a0 72 69 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e  rigger is runnin
130b0 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74  g..** But once t
130c0 68 65 20 74 72 69 67 67 65 72 20 74 65 72 6d 69  he trigger termi
130d0 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65  nates, the value
130e0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69   returned by thi
130f0 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76  s routine.** rev
13100 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74  erts to the last
13110 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20   value inserted 
13120 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67  before the trigg
13130 65 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  er fired..**.** 
13140 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74  An [INSERT] that
13150 20 66 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20   fails due to a 
13160 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
13170 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a  tion is not a.**
13180 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53   successful [INS
13190 45 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f  ERT] and does no
131a0 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c  t change the val
131b0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ue returned by t
131c0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20  his.** routine. 
131d0 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20   Thus INSERT OR 
131e0 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20  FAIL, INSERT OR 
131f0 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f  IGNORE, INSERT O
13200 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61  R ROLLBACK,.** a
13210 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f  nd INSERT OR ABO
13220 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67  RT make no chang
13230 65 73 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e  es to the return
13240 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a   value of this.*
13250 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74  * routine when t
13260 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66  heir insertion f
13270 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45  ails.  When INSE
13280 52 54 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a  RT OR REPLACE.**
13290 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f   encounters a co
132a0 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69  nstraint violati
132b0 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20  on, it does not 
132c0 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e  fail.  The.** IN
132d0 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74  SERT continues t
132e0 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74  o completion aft
132f0 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73  er deleting rows
13300 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20   that caused.** 
13310 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70  the constraint p
13320 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54  roblem so INSERT
13330 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c   OR REPLACE will
13340 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a   always change.*
13350 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * the return val
13360 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72  ue of this inter
13370 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  face..**.** For 
13380 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
13390 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e  this routine, an
133a0 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e   [INSERT] is con
133b0 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65  sidered to.** be
133c0 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e   successful even
133d0 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65 71   if it is subseq
133e0 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61  uently rolled ba
133f0 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ck..**.** Requir
13400 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32  ements:.** [H122
13410 32 31 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2a 0a  21] [H12223].**.
13420 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65  ** If a separate
13430 20 74 68 72 65 61 64 20 70 65 72 66 6f 72 6d 73   thread performs
13440 20 61 20 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20   a new [INSERT] 
13450 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64  on the same.** d
13460 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
13470 6f 6e 20 77 68 69 6c 65 20 74 68 65 20 5b 73 71  on while the [sq
13480 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
13490 74 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 66 75  t_rowid()].** fu
134a0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
134b0 67 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67  g and thus chang
134c0 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65  es the last inse
134d0 72 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 74  rt [rowid],.** t
134e0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  hen the value re
134f0 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74  turned by [sqlit
13500 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
13510 6f 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20 75 6e  owid()] is.** un
13520 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20  predictable and 
13530 6d 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20  might not equal 
13540 65 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f  either the old o
13550 72 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 73  r the new.** las
13560 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d  t insert [rowid]
13570 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
13580 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71  sqlite3_int64 sq
13590 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
135a0 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a  t_rowid(sqlite3*
135b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
135c0 45 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75  EF: Count The Nu
135d0 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64  mber Of Rows Mod
135e0 69 66 69 65 64 20 7b 48 31 32 32 34 30 7d 20 3c  ified {H12240} <
135f0 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S10600>.**.** Th
13600 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
13610 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
13620 66 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20  f database rows 
13630 74 68 61 74 20 77 65 72 65 20 63 68 61 6e 67 65  that were change
13640 64 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64  d.** or inserted
13650 20 6f 72 20 64 65 6c 65 74 65 64 20 62 79 20 74   or deleted by t
13660 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
13670 20 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73   completed SQL s
13680 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e 20 74  tatement.** on t
13690 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
136a0 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69  nection] specifi
136b0 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20  ed by the first 
136c0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 6e  parameter..** On
136d0 6c 79 20 63 68 61 6e 67 65 73 20 74 68 61 74 20  ly changes that 
136e0 61 72 65 20 64 69 72 65 63 74 6c 79 20 73 70 65  are directly spe
136f0 63 69 66 69 65 64 20 62 79 20 74 68 65 20 5b 49  cified by the [I
13700 4e 53 45 52 54 5d 2c 20 5b 55 50 44 41 54 45 5d  NSERT], [UPDATE]
13710 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 45 5d  ,.** or [DELETE]
13720 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63   statement are c
13730 6f 75 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61  ounted.  Auxilia
13740 72 79 20 63 68 61 6e 67 65 73 20 63 61 75 73 65  ry changes cause
13750 64 20 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73  d by.** triggers
13760 20 6f 72 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79   or [foreign key
13770 20 61 63 74 69 6f 6e 73 5d 20 61 72 65 20 6e 6f   actions] are no
13780 74 20 63 6f 75 6e 74 65 64 2e 20 55 73 65 20 74  t counted. Use t
13790 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74  he.** [sqlite3_t
137a0 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20  otal_changes()] 
137b0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 6e 64  function to find
137c0 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
137d0 72 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20  r of changes.** 
137e0 69 6e 63 6c 75 64 69 6e 67 20 63 68 61 6e 67 65  including change
137f0 73 20 63 61 75 73 65 64 20 62 79 20 74 72 69 67  s caused by trig
13800 67 65 72 73 20 61 6e 64 20 66 6f 72 65 69 67 6e  gers and foreign
13810 20 6b 65 79 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a   key actions..**
13820 0a 2a 2a 20 43 68 61 6e 67 65 73 20 74 6f 20 61  .** Changes to a
13830 20 76 69 65 77 20 74 68 61 74 20 61 72 65 20 73   view that are s
13840 69 6d 75 6c 61 74 65 64 20 62 79 20 61 6e 20 5b  imulated by an [
13850 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67  INSTEAD OF trigg
13860 65 72 5d 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 63  er].** are not c
13870 6f 75 6e 74 65 64 2e 20 20 4f 6e 6c 79 20 72 65  ounted.  Only re
13880 61 6c 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73  al table changes
13890 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a   are counted..**
138a0 0a 2a 2a 20 41 20 22 72 6f 77 20 63 68 61 6e 67  .** A "row chang
138b0 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 20 74  e" is a change t
138c0 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f  o a single row o
138d0 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  f a single table
138e0 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 61 6e  .** caused by an
138f0 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 2c   INSERT, DELETE,
13900 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65   or UPDATE state
13910 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68 61 74  ment.  Rows that
13920 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65 64 20  .** are changed 
13930 61 73 20 73 69 64 65 20 65 66 66 65 63 74 73 20  as side effects 
13940 6f 66 20 5b 52 45 50 4c 41 43 45 5d 20 63 6f 6e  of [REPLACE] con
13950 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 69  straint resoluti
13960 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c  on,.** rollback,
13970 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e   ABORT processin
13980 67 2c 20 5b 44 52 4f 50 20 54 41 42 4c 45 5d 2c  g, [DROP TABLE],
13990 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68 65 72   or by any other
139a0 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73 20 64  .** mechanisms d
139b0 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 64  o not count as d
139c0 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65  irect row change
139d0 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72 69 67  s..**.** A "trig
139e0 67 65 72 20 63 6f 6e 74 65 78 74 22 20 69 73 20  ger context" is 
139f0 61 20 73 63 6f 70 65 20 6f 66 20 65 78 65 63 75  a scope of execu
13a00 74 69 6f 6e 20 74 68 61 74 20 62 65 67 69 6e 73  tion that begins
13a10 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20 77 69 74   and.** ends wit
13a20 68 20 74 68 65 20 73 63 72 69 70 74 20 6f 66 20  h the script of 
13a30 61 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 45  a [CREATE TRIGGE
13a40 52 20 7c 20 74 72 69 67 67 65 72 5d 2e 20 0a 2a  R | trigger]. .*
13a50 2a 20 4d 6f 73 74 20 53 51 4c 20 73 74 61 74 65  * Most SQL state
13a60 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 65 76 61  ments are.** eva
13a70 6c 75 61 74 65 64 20 6f 75 74 73 69 64 65 20 6f  luated outside o
13a80 66 20 61 6e 79 20 74 72 69 67 67 65 72 2e 20 20  f any trigger.  
13a90 54 68 69 73 20 69 73 20 74 68 65 20 22 74 6f 70  This is the "top
13aa0 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 72 69 67 67   level".** trigg
13ab0 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 49 66 20  er context.  If 
13ac0 61 20 74 72 69 67 67 65 72 20 66 69 72 65 73 20  a trigger fires 
13ad0 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c 65 76  from the top lev
13ae0 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 20 74 72 69  el, a.** new tri
13af0 67 67 65 72 20 63 6f 6e 74 65 78 74 20 69 73 20  gger context is 
13b00 65 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65 20  entered for the 
13b10 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 61 74  duration of that
13b20 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 67 65 72 2e   one.** trigger.
13b30 20 20 53 75 62 74 72 69 67 67 65 72 73 20 63 72    Subtriggers cr
13b40 65 61 74 65 20 73 75 62 63 6f 6e 74 65 78 74 73  eate subcontexts
13b50 20 66 6f 72 20 74 68 65 69 72 20 64 75 72 61 74   for their durat
13b60 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69  ion..**.** Calli
13b70 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  ng [sqlite3_exec
13b80 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
13b90 73 74 65 70 28 29 5d 20 72 65 63 75 72 73 69 76  step()] recursiv
13ba0 65 6c 79 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20  ely does.** not 
13bb0 63 72 65 61 74 65 20 61 20 6e 65 77 20 74 72 69  create a new tri
13bc0 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a  gger context..**
13bd0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
13be0 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  n returns the nu
13bf0 6d 62 65 72 20 6f 66 20 64 69 72 65 63 74 20 72  mber of direct r
13c00 6f 77 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  ow changes in th
13c10 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74  e.** most recent
13c20 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
13c30 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65   or DELETE state
13c40 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68 65 20  ment within the 
13c50 73 61 6d 65 0a 2a 2a 20 74 72 69 67 67 65 72 20  same.** trigger 
13c60 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54  context..**.** T
13c70 68 75 73 2c 20 77 68 65 6e 20 63 61 6c 6c 65 64  hus, when called
13c80 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c 65   from the top le
13c90 76 65 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69  vel, this functi
13ca0 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a  on returns the.*
13cb0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e  * number of chan
13cc0 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20  ges in the most 
13cd0 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55  recent INSERT, U
13ce0 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45  PDATE, or DELETE
13cf0 0a 2a 2a 20 74 68 61 74 20 61 6c 73 6f 20 6f 63  .** that also oc
13d00 63 75 72 72 65 64 20 61 74 20 74 68 65 20 74 6f  curred at the to
13d10 70 20 6c 65 76 65 6c 2e 20 20 57 69 74 68 69 6e  p level.  Within
13d20 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 74   the body of a t
13d30 72 69 67 67 65 72 2c 0a 2a 2a 20 74 68 65 20 73  rigger,.** the s
13d40 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29  qlite3_changes()
13d50 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62   interface can b
13d60 65 20 63 61 6c 6c 65 64 20 74 6f 20 66 69 6e 64  e called to find
13d70 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
13d80 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65  * changes in the
13d90 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63   most recently c
13da0 6f 6d 70 6c 65 74 65 64 20 49 4e 53 45 52 54 2c  ompleted INSERT,
13db0 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45   UPDATE, or DELE
13dc0 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  TE.** statement 
13dd0 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20  within the body 
13de0 6f 66 20 74 68 65 20 73 61 6d 65 20 74 72 69 67  of the same trig
13df0 67 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  ger..** However,
13e00 20 74 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75   the number retu
13e10 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e  rned does not in
13e20 63 6c 75 64 65 20 63 68 61 6e 67 65 73 0a 2a 2a  clude changes.**
13e30 20 63 61 75 73 65 64 20 62 79 20 73 75 62 74 72   caused by subtr
13e40 69 67 67 65 72 73 20 73 69 6e 63 65 20 74 68 6f  iggers since tho
13e50 73 65 20 68 61 76 65 20 74 68 65 69 72 20 6f 77  se have their ow
13e60 6e 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a  n context..**.**
13e70 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73   See also the [s
13e80 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
13e90 6e 67 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63  nges()] interfac
13ea0 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b 63 6f  e and the.** [co
13eb0 75 6e 74 5f 63 68 61 6e 67 65 73 20 70 72 61 67  unt_changes prag
13ec0 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ma]..**.** Requi
13ed0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
13ee0 32 34 31 5d 20 5b 48 31 32 32 34 33 5d 0a 2a 2a  241] [H12243].**
13ef0 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74  .** If a separat
13f00 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63  e thread makes c
13f10 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61  hanges on the sa
13f20 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
13f30 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20  ection.** while 
13f40 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73  [sqlite3_changes
13f50 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74  ()] is running t
13f60 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  hen the value re
13f70 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 75 6e 70  turned.** is unp
13f80 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e  redictable and n
13f90 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a  ot meaningful..*
13fa0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
13fb0 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
13fc0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
13fd0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 6f 74  ** CAPI3REF: Tot
13fe0 61 6c 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77  al Number Of Row
13ff0 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 32  s Modified {H122
14000 36 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a  60} <S10600>.**.
14010 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
14020 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
14030 62 65 72 20 6f 66 20 72 6f 77 20 63 68 61 6e 67  ber of row chang
14040 65 73 20 63 61 75 73 65 64 20 62 79 20 5b 49 4e  es caused by [IN
14050 53 45 52 54 5d 2c 0a 2a 2a 20 5b 55 50 44 41 54  SERT],.** [UPDAT
14060 45 5d 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73  E] or [DELETE] s
14070 74 61 74 65 6d 65 6e 74 73 20 73 69 6e 63 65 20  tatements since 
14080 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
14090 6e 6e 65 63 74 69 6f 6e 5d 20 77 61 73 20 6f 70  nnection] was op
140a0 65 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 63 6f 75  ened..** The cou
140b0 6e 74 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20  nt includes all 
140c0 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 61 6c 6c  changes from all
140d0 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 45 52   [CREATE TRIGGER
140e0 20 7c 20 74 72 69 67 67 65 72 5d 20 0a 2a 2a 20   | trigger] .** 
140f0 63 6f 6e 74 65 78 74 73 20 61 6e 64 20 63 68 61  contexts and cha
14100 6e 67 65 73 20 6d 61 64 65 20 62 79 20 5b 66 6f  nges made by [fo
14110 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e  reign key action
14120 73 5d 2e 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20  s]. However,.** 
14130 74 68 65 20 63 6f 75 6e 74 20 64 6f 65 73 20 6e  the count does n
14140 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61 6e 67  ot include chang
14150 65 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  es used to imple
14160 6d 65 6e 74 20 5b 52 45 50 4c 41 43 45 5d 20 63  ment [REPLACE] c
14170 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a 2a 20 64  onstraints,.** d
14180 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f 72 20 41  o rollbacks or A
14190 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 2c  BORT processing,
141a0 20 6f 72 20 5b 44 52 4f 50 20 54 41 42 4c 45 5d   or [DROP TABLE]
141b0 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 54 68   processing.  Th
141c0 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 6f 65 73 20  e.** count does 
141d0 6e 6f 74 20 69 6e 63 6c 75 64 65 20 72 6f 77 73  not include rows
141e0 20 6f 66 20 76 69 65 77 73 20 74 68 61 74 20 66   of views that f
141f0 69 72 65 20 61 6e 20 5b 49 4e 53 54 45 41 44 20  ire an [INSTEAD 
14200 4f 46 20 74 72 69 67 67 65 72 5d 2c 0a 2a 2a 20  OF trigger],.** 
14210 74 68 6f 75 67 68 20 69 66 20 74 68 65 20 49 4e  though if the IN
14220 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72  STEAD OF trigger
14230 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f   makes changes o
14240 66 20 69 74 73 20 6f 77 6e 2c 20 74 68 6f 73 65  f its own, those
14250 20 63 68 61 6e 67 65 73 20 0a 2a 2a 20 61 72 65   changes .** are
14260 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 20 54 68 65   counted..** The
14270 20 63 68 61 6e 67 65 73 20 61 72 65 20 63 6f 75   changes are cou
14280 6e 74 65 64 20 61 73 20 73 6f 6f 6e 20 61 73 20  nted as soon as 
14290 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68  the statement th
142a0 61 74 20 6d 61 6b 65 73 20 74 68 65 6d 20 69 73  at makes them is
142b0 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20 28 77  .** completed (w
142c0 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e  hen the statemen
142d0 74 20 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73  t handle is pass
142e0 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  ed to [sqlite3_r
142f0 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73  eset()] or.** [s
14300 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
14310 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  )])..**.** See a
14320 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33  lso the [sqlite3
14330 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 6e 74 65  _changes()] inte
14340 72 66 61 63 65 20 61 6e 64 20 74 68 65 0a 2a 2a  rface and the.**
14350 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20   [count_changes 
14360 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52  pragma]..**.** R
14370 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
14380 5b 48 31 32 32 36 31 5d 20 5b 48 31 32 32 36 33  [H12261] [H12263
14390 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70  ].**.** If a sep
143a0 61 72 61 74 65 20 74 68 72 65 61 64 20 6d 61 6b  arate thread mak
143b0 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 68  es changes on th
143c0 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
143d0 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 68  connection.** wh
143e0 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74  ile [sqlite3_tot
143f0 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 73  al_changes()] is
14400 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 68   running then th
14410 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72  e value.** retur
14420 6e 65 64 20 69 73 20 75 6e 70 72 65 64 69 63 74  ned is unpredict
14430 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 61  able and not mea
14440 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49  ningful..*/.SQLI
14450 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
14460 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
14470 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
14480 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e 74  ** CAPI3REF: Int
14490 65 72 72 75 70 74 20 41 20 4c 6f 6e 67 2d 52 75  errupt A Long-Ru
144a0 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b 48 31 32  nning Query {H12
144b0 32 37 30 7d 20 3c 53 33 30 35 30 30 3e 0a 2a 2a  270} <S30500>.**
144c0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
144d0 6e 20 63 61 75 73 65 73 20 61 6e 79 20 70 65 6e  n causes any pen
144e0 64 69 6e 67 20 64 61 74 61 62 61 73 65 20 6f 70  ding database op
144f0 65 72 61 74 69 6f 6e 20 74 6f 20 61 62 6f 72 74  eration to abort
14500 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 61   and.** return a
14510 74 20 69 74 73 20 65 61 72 6c 69 65 73 74 20 6f  t its earliest o
14520 70 70 6f 72 74 75 6e 69 74 79 2e 20 54 68 69 73  pportunity. This
14530 20 72 6f 75 74 69 6e 65 20 69 73 20 74 79 70 69   routine is typi
14540 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64 20  cally.** called 
14550 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61  in response to a
14560 20 75 73 65 72 20 61 63 74 69 6f 6e 20 73 75 63   user action suc
14570 68 20 61 73 20 70 72 65 73 73 69 6e 67 20 22 43  h as pressing "C
14580 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 20 43 74 72  ancel".** or Ctr
14590 6c 2d 43 20 77 68 65 72 65 20 74 68 65 20 75 73  l-C where the us
145a0 65 72 20 77 61 6e 74 73 20 61 20 6c 6f 6e 67 20  er wants a long 
145b0 71 75 65 72 79 20 6f 70 65 72 61 74 69 6f 6e 20  query operation 
145c0 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65 64  to halt.** immed
145d0 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74  iately..**.** It
145e0 20 69 73 20 73 61 66 65 20 74 6f 20 63 61 6c 6c   is safe to call
145f0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72   this routine fr
14600 6f 6d 20 61 20 74 68 72 65 61 64 20 64 69 66 66  om a thread diff
14610 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a  erent from the.*
14620 2a 20 74 68 72 65 61 64 20 74 68 61 74 20 69 73  * thread that is
14630 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69   currently runni
14640 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ng the database 
14650 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20  operation.  But 
14660 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 61 66  it.** is not saf
14670 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72  e to call this r
14680 6f 75 74 69 6e 65 20 77 69 74 68 20 61 20 5b 64  outine with a [d
14690 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
146a0 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 69 73 20 63  on] that.** is c
146b0 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68 74 20 63  losed or might c
146c0 6c 6f 73 65 20 62 65 66 6f 72 65 20 73 71 6c 69  lose before sqli
146d0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20  te3_interrupt() 
146e0 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49  returns..**.** I
146f0 66 20 61 6e 20 53 51 4c 20 6f 70 65 72 61 74 69  f an SQL operati
14700 6f 6e 20 69 73 20 76 65 72 79 20 6e 65 61 72 6c  on is very nearl
14710 79 20 66 69 6e 69 73 68 65 64 20 61 74 20 74 68  y finished at th
14720 65 20 74 69 6d 65 20 77 68 65 6e 0a 2a 2a 20 73  e time when.** s
14730 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
14740 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  () is called, th
14750 65 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20  en it might not 
14760 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e  have an opportun
14770 69 74 79 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74  ity.** to be int
14780 65 72 72 75 70 74 65 64 20 61 6e 64 20 6d 69 67  errupted and mig
14790 68 74 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 63  ht continue to c
147a0 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ompletion..**.**
147b0 20 41 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 6f   An SQL operatio
147c0 6e 20 74 68 61 74 20 69 73 20 69 6e 74 65 72 72  n that is interr
147d0 75 70 74 65 64 20 77 69 6c 6c 20 72 65 74 75 72  upted will retur
147e0 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  n [SQLITE_INTERR
147f0 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20  UPT]..** If the 
14800 69 6e 74 65 72 72 75 70 74 65 64 20 53 51 4c 20  interrupted SQL 
14810 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20  operation is an 
14820 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20  INSERT, UPDATE, 
14830 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61  or DELETE.** tha
14840 74 20 69 73 20 69 6e 73 69 64 65 20 61 6e 20 65  t is inside an e
14850 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74  xplicit transact
14860 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 65 6e  ion, then the en
14870 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  tire transaction
14880 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c  .** will be roll
14890 65 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 69  ed back automati
148a0 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cally..**.** The
148b0 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
148c0 70 74 28 44 29 20 63 61 6c 6c 20 69 73 20 69 6e  pt(D) call is in
148d0 20 65 66 66 65 63 74 20 75 6e 74 69 6c 20 61 6c   effect until al
148e0 6c 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e  l currently runn
148f0 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65  ing.** SQL state
14900 6d 65 6e 74 73 20 6f 6e 20 5b 64 61 74 61 62 61  ments on [databa
14910 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
14920 20 63 6f 6d 70 6c 65 74 65 2e 20 20 41 6e 79 20   complete.  Any 
14930 6e 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  new SQL statemen
14940 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73  ts.** that are s
14950 74 61 72 74 65 64 20 61 66 74 65 72 20 74 68 65  tarted after the
14960 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
14970 70 74 28 29 20 63 61 6c 6c 20 61 6e 64 20 62 65  pt() call and be
14980 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 72 75 6e  fore the .** run
14990 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20  ning statements 
149a0 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 72 65  reaches zero are
149b0 20 69 6e 74 65 72 72 75 70 74 65 64 20 61 73 20   interrupted as 
149c0 69 66 20 74 68 65 79 20 68 61 64 20 62 65 65 6e  if they had been
149d0 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 70 72 69 6f  .** running prio
149e0 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  r to the sqlite3
149f0 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 61 6c  _interrupt() cal
14a00 6c 2e 20 20 4e 65 77 20 53 51 4c 20 73 74 61 74  l.  New SQL stat
14a10 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61  ements.** that a
14a20 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72  re started after
14a30 20 74 68 65 20 72 75 6e 6e 69 6e 67 20 73 74 61   the running sta
14a40 74 65 6d 65 6e 74 20 63 6f 75 6e 74 20 72 65 61  tement count rea
14a50 63 68 65 73 20 7a 65 72 6f 20 61 72 65 0a 2a 2a  ches zero are.**
14a60 20 6e 6f 74 20 65 66 66 65 63 74 65 64 20 62 79   not effected by
14a70 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74   the sqlite3_int
14a80 65 72 72 75 70 74 28 29 2e 0a 2a 2a 20 41 20 63  errupt()..** A c
14a90 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69  all to sqlite3_i
14aa0 6e 74 65 72 72 75 70 74 28 44 29 20 74 68 61 74  nterrupt(D) that
14ab0 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 68 65   occurs when the
14ac0 72 65 20 61 72 65 20 6e 6f 20 72 75 6e 6e 69 6e  re are no runnin
14ad0 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65  g.** SQL stateme
14ae0 6e 74 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61  nts is a no-op a
14af0 6e 64 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74  nd has no effect
14b00 20 6f 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   on SQL statemen
14b10 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73  ts.** that are s
14b20 74 61 72 74 65 64 20 61 66 74 65 72 20 74 68 65  tarted after the
14b30 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
14b40 70 74 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e  pt() call return
14b50 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  s..**.** Require
14b60 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 37  ments:.** [H1227
14b70 31 5d 20 5b 48 31 32 32 37 32 5d 0a 2a 2a 0a 2a  1] [H12272].**.*
14b80 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
14b90 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6c 6f  e connection clo
14ba0 73 65 73 20 77 68 69 6c 65 20 5b 73 71 6c 69 74  ses while [sqlit
14bb0 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 5d 0a  e3_interrupt()].
14bc0 2a 2a 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68  ** is running th
14bd0 65 6e 20 62 61 64 20 74 68 69 6e 67 73 20 77 69  en bad things wi
14be0 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 70 70 65 6e  ll likely happen
14bf0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
14c00 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74  void sqlite3_int
14c10 65 72 72 75 70 74 28 73 71 6c 69 74 65 33 2a 29  errupt(sqlite3*)
14c20 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
14c30 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 49 66 20  F: Determine If 
14c40 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74  An SQL Statement
14c50 20 49 73 20 43 6f 6d 70 6c 65 74 65 20 7b 48 31   Is Complete {H1
14c60 30 35 31 30 7d 20 3c 53 37 30 32 30 30 3e 0a 2a  0510} <S70200>.*
14c70 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
14c80 6e 65 73 20 61 72 65 20 75 73 65 66 75 6c 20 64  nes are useful d
14c90 75 72 69 6e 67 20 63 6f 6d 6d 61 6e 64 2d 6c 69  uring command-li
14ca0 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65 74 65  ne input to dete
14cb0 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a 2a 20  rmine if the.** 
14cc0 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65  currently entere
14cd0 64 20 74 65 78 74 20 73 65 65 6d 73 20 74 6f 20  d text seems to 
14ce0 66 6f 72 6d 20 61 20 63 6f 6d 70 6c 65 74 65 20  form a complete 
14cf0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f 72  SQL statement or
14d00 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f 6e 61  .** if additiona
14d10 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65 64 65  l input is neede
14d20 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69 6e 67  d before sending
14d30 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f 0a 2a   the text into.*
14d40 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 61 72  * SQLite for par
14d50 73 69 6e 67 2e 20 20 54 68 65 73 65 20 72 6f 75  sing.  These rou
14d60 74 69 6e 65 73 20 72 65 74 75 72 6e 20 31 20 69  tines return 1 i
14d70 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69  f the input stri
14d80 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73 20 74 6f  ng.** appears to
14d90 20 62 65 20 61 20 63 6f 6d 70 6c 65 74 65 20 53   be a complete S
14da0 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41  QL statement.  A
14db0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6a 75   statement is ju
14dc0 64 67 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63 6f  dged to be.** co
14dd0 6d 70 6c 65 74 65 20 69 66 20 69 74 20 65 6e 64  mplete if it end
14de0 73 20 77 69 74 68 20 61 20 73 65 6d 69 63 6f 6c  s with a semicol
14df0 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73 20  on token and is 
14e00 6e 6f 74 20 61 20 70 72 65 66 69 78 20 6f 66 20  not a prefix of 
14e10 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  a.** well-formed
14e20 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
14e30 73 74 61 74 65 6d 65 6e 74 2e 20 20 53 65 6d 69  statement.  Semi
14e40 63 6f 6c 6f 6e 73 20 74 68 61 74 20 61 72 65 20  colons that are 
14e50 65 6d 62 65 64 64 65 64 20 77 69 74 68 69 6e 0a  embedded within.
14e60 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61  ** string litera
14e70 6c 73 20 6f 72 20 71 75 6f 74 65 64 20 69 64 65  ls or quoted ide
14e80 6e 74 69 66 69 65 72 20 6e 61 6d 65 73 20 6f 72  ntifier names or
14e90 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20 6e 6f   comments are no
14ea0 74 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74  t.** independent
14eb0 20 74 6f 6b 65 6e 73 20 28 74 68 65 79 20 61 72   tokens (they ar
14ec0 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 6f  e part of the to
14ed0 6b 65 6e 20 69 6e 20 77 68 69 63 68 20 74 68 65  ken in which the
14ee0 79 20 61 72 65 0a 2a 2a 20 65 6d 62 65 64 64 65  y are.** embedde
14ef0 64 29 20 61 6e 64 20 74 68 75 73 20 64 6f 20 6e  d) and thus do n
14f00 6f 74 20 63 6f 75 6e 74 20 61 73 20 61 20 73 74  ot count as a st
14f10 61 74 65 6d 65 6e 74 20 74 65 72 6d 69 6e 61 74  atement terminat
14f20 6f 72 2e 20 20 57 68 69 74 65 73 70 61 63 65 0a  or.  Whitespace.
14f30 2a 2a 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 20  ** and comments 
14f40 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 65 20  that follow the 
14f50 66 69 6e 61 6c 20 73 65 6d 69 63 6f 6c 6f 6e 20  final semicolon 
14f60 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a  are ignored..**.
14f70 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
14f80 73 20 72 65 74 75 72 6e 20 30 20 69 66 20 74 68  s return 0 if th
14f90 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 69  e statement is i
14fa0 6e 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 61  ncomplete.  If a
14fb0 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  .** memory alloc
14fc0 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65  ation fails, the
14fd0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  n SQLITE_NOMEM i
14fe0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
14ff0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
15000 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 20 74 68   do not parse th
15010 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  e SQL statements
15020 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f   thus.** will no
15030 74 20 64 65 74 65 63 74 20 73 79 6e 74 61 63 74  t detect syntact
15040 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 65 63 74  ically incorrect
15050 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53   SQL..**.** If S
15060 51 4c 69 74 65 20 68 61 73 20 6e 6f 74 20 62 65  QLite has not be
15070 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 75  en initialized u
15080 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 69 6e  sing [sqlite3_in
15090 69 74 69 61 6c 69 7a 65 28 29 5d 20 70 72 69 6f  itialize()] prio
150a0 72 20 0a 2a 2a 20 74 6f 20 69 6e 76 6f 6b 69 6e  r .** to invokin
150b0 67 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  g sqlite3_comple
150c0 74 65 31 36 28 29 20 74 68 65 6e 20 73 71 6c 69  te16() then sqli
150d0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
150e0 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61   is invoked.** a
150f0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20  utomatically by 
15100 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
15110 31 36 28 29 2e 20 20 49 66 20 74 68 61 74 20 69  16().  If that i
15120 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 66 61  nitialization fa
15130 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ils,.** then the
15140 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72   return value fr
15150 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  om sqlite3_compl
15160 65 74 65 31 36 28 29 20 77 69 6c 6c 20 62 65 20  ete16() will be 
15170 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 72 65 67 61  non-zero.** rega
15180 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
15190 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 70  r or not the inp
151a0 75 74 20 53 51 4c 20 69 73 20 63 6f 6d 70 6c 65  ut SQL is comple
151b0 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  te..**.** Requir
151c0 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 31 31 5d  ements: [H10511]
151d0 20 5b 48 31 30 35 31 32 5d 0a 2a 2a 0a 2a 2a 20   [H10512].**.** 
151e0 54 68 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 71  The input to [sq
151f0 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29  lite3_complete()
15200 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f  ] must be a zero
15210 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55  -terminated.** U
15220 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  TF-8 string..**.
15230 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20  ** The input to 
15240 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74  [sqlite3_complet
15250 65 31 36 28 29 5d 20 6d 75 73 74 20 62 65 20 61  e16()] must be a
15260 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
15270 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e  .** UTF-16 strin
15280 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65  g in native byte
15290 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54   order..*/.SQLIT
152a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
152b0 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74  3_complete(const
152c0 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a 53 51 4c   char *sql);.SQL
152d0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
152e0 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63  te3_complete16(c
152f0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 29 3b  onst void *sql);
15300 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
15310 3a 20 52 65 67 69 73 74 65 72 20 41 20 43 61 6c  : Register A Cal
15320 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 6c 65 20  lback To Handle 
15330 53 51 4c 49 54 45 5f 42 55 53 59 20 45 72 72 6f  SQLITE_BUSY Erro
15340 72 73 20 7b 48 31 32 33 31 30 7d 20 3c 53 34 30  rs {H12310} <S40
15350 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  400>.**.** This 
15360 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 63  routine sets a c
15370 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
15380 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 69   that might be i
15390 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 0a  nvoked whenever.
153a0 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73  ** an attempt is
153b0 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20   made to open a 
153c0 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 74  database table t
153d0 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65  hat another thre
153e0 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 73 73  ad.** or process
153f0 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a 0a   has locked..**.
15400 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 20 63  ** If the busy c
15410 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2c  allback is NULL,
15420 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 42 55   then [SQLITE_BU
15430 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49  SY] or [SQLITE_I
15440 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a  OERR_BLOCKED].**
15450 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d   is returned imm
15460 65 64 69 61 74 65 6c 79 20 75 70 6f 6e 20 65 6e  ediately upon en
15470 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 65 20 6c  countering the l
15480 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 75 73 79  ock. If the busy
15490 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20   callback.** is 
154a0 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74  not NULL, then t
154b0 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c  he callback will
154c0 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68   be invoked with
154d0 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0a   two arguments..
154e0 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
154f0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
15500 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 6f 70  handler is a cop
15510 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70  y of the void* p
15520 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a 20  ointer which.** 
15530 69 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67  is the third arg
15540 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
15550 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 2e  _busy_handler().
15560 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67    The second arg
15570 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20  ument to.** the 
15580 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b  handler callback
15590 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
155a0 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 65  f times that the
155b0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 68 61   busy handler ha
155c0 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b 65  s.** been invoke
155d0 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b 69  d for this locki
155e0 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 20 74 68  ng event.  If th
155f0 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 61  e.** busy callba
15600 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68  ck returns 0, th
15610 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e 61 6c  en no additional
15620 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 6d 61   attempts are ma
15630 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 73 73 20  de to.** access 
15640 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64  the database and
15650 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f   [SQLITE_BUSY] o
15660 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  r [SQLITE_IOERR_
15670 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 75  BLOCKED] is retu
15680 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20  rned..** If the 
15690 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
156a0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
156b0 61 6e 6f 74 68 65 72 20 61 74 74 65 6d 70 74 0a  another attempt.
156c0 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70  ** is made to op
156d0 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
156e0 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20  for reading and 
156f0 74 68 65 20 63 79 63 6c 65 20 72 65 70 65 61 74  the cycle repeat
15700 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65  s..**.** The pre
15710 73 65 6e 63 65 20 6f 66 20 61 20 62 75 73 79 20  sence of a busy 
15720 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 6e 6f 74  handler does not
15730 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
15740 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b  it will be invok
15750 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 72 65  ed.** when there
15760 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74   is lock content
15770 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 65 20 64  ion. If SQLite d
15780 65 74 65 72 6d 69 6e 65 73 20 74 68 61 74 20 69  etermines that i
15790 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79  nvoking the busy
157a0 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 6f 75 6c  .** handler coul
157b0 64 20 72 65 73 75 6c 74 20 69 6e 20 61 20 64 65  d result in a de
157c0 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 6c 6c 20  adlock, it will 
157d0 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 65 74  go ahead and ret
157e0 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  urn [SQLITE_BUSY
157f0 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ].** or [SQLITE_
15800 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69  IOERR_BLOCKED] i
15810 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69  nstead of invoki
15820 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  ng the busy hand
15830 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72  ler..** Consider
15840 20 61 20 73 63 65 6e 61 72 69 6f 20 77 68 65 72   a scenario wher
15850 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 69 73  e one process is
15860 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64 20   holding a read 
15870 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 69 74 20  lock that.** it 
15880 69 73 20 74 72 79 69 6e 67 20 74 6f 20 70 72 6f  is trying to pro
15890 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 65 72 76  mote to a reserv
158a0 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a 20 61  ed lock and.** a
158b0 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20   second process 
158c0 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 73  is holding a res
158d0 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 61 74 20  erved lock that 
158e0 69 74 20 69 73 20 74 72 79 69 6e 67 0a 2a 2a 20  it is trying.** 
158f0 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 6e  to promote to an
15900 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e   exclusive lock.
15910 20 20 54 68 65 20 66 69 72 73 74 20 70 72 6f 63    The first proc
15920 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63 65  ess cannot proce
15930 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 20 69 74  ed.** because it
15940 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74   is blocked by t
15950 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68  he second and th
15960 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73  e second process
15970 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f 63 65   cannot.** proce
15980 65 64 20 62 65 63 61 75 73 65 20 69 74 20 69 73  ed because it is
15990 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20   blocked by the 
159a0 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74 68 20  first.  If both 
159b0 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 69 6e 76  processes.** inv
159c0 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e  oke the busy han
159d0 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 72 20 77  dlers, neither w
159e0 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 70 72 6f  ill make any pro
159f0 67 72 65 73 73 2e 20 20 54 68 65 72 65 66 6f 72  gress.  Therefor
15a00 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 72 65 74  e,.** SQLite ret
15a10 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 42 55 53  urns [SQLITE_BUS
15a20 59 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74  Y] for the first
15a30 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 69 6e 67   process, hoping
15a40 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 77 69   that this.** wi
15a50 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 20 66 69  ll induce the fi
15a60 72 73 74 20 70 72 6f 63 65 73 73 20 74 6f 20 72  rst process to r
15a70 65 6c 65 61 73 65 20 69 74 73 20 72 65 61 64 20  elease its read 
15a80 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 0a 2a  lock and allow.*
15a90 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f  * the second pro
15aa0 63 65 73 73 20 74 6f 20 70 72 6f 63 65 65 64 2e  cess to proceed.
15ab0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  .**.** The defau
15ac0 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  lt busy callback
15ad0 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   is NULL..**.** 
15ae0 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  The [SQLITE_BUSY
15af0 5d 20 65 72 72 6f 72 20 69 73 20 63 6f 6e 76 65  ] error is conve
15b00 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 54 45 5f  rted to [SQLITE_
15b10 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a  IOERR_BLOCKED].*
15b20 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73  * when SQLite is
15b30 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
15b40 66 20 61 20 6c 61 72 67 65 20 74 72 61 6e 73 61  f a large transa
15b50 63 74 69 6f 6e 20 77 68 65 72 65 20 61 6c 6c 20  ction where all 
15b60 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 73 20 77  the.** changes w
15b70 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74 6f  ill not fit into
15b80 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63   the in-memory c
15b90 61 63 68 65 2e 20 20 53 51 4c 69 74 65 20 77 69  ache.  SQLite wi
15ba0 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 6f  ll.** already ho
15bb0 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  ld a RESERVED lo
15bc0 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
15bd0 73 65 20 66 69 6c 65 2c 20 62 75 74 20 69 74 20  se file, but it 
15be0 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 72 6f 6d  needs.** to prom
15bf0 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20 74 6f  ote this lock to
15c00 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20 74 68   EXCLUSIVE so th
15c10 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c 6c 20  at it can spill 
15c20 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 73 20 69  cache.** pages i
15c30 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
15c40 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 68 61   file without ha
15c50 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e 74  rm to concurrent
15c60 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20 49 66  .** readers.  If
15c70 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f   it is unable to
15c80 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 6c 6f 63   promote the loc
15c90 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 2d 6d  k, then the in-m
15ca0 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 65 20 77  emory.** cache w
15cb0 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e 20 61  ill be left in a
15cc0 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73  n inconsistent s
15cd0 74 61 74 65 20 61 6e 64 20 73 6f 20 74 68 65 20  tate and so the 
15ce0 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73  error.** code is
15cf0 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f 6d 20 74   promoted from t
15d00 68 65 20 72 65 6c 61 74 69 76 65 6c 79 20 62 65  he relatively be
15d10 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53  nign [SQLITE_BUS
15d20 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 6d 6f 72  Y] to.** the mor
15d30 65 20 73 65 76 65 72 65 20 5b 53 51 4c 49 54 45  e severe [SQLITE
15d40 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e  _IOERR_BLOCKED].
15d50 20 20 54 68 69 73 20 65 72 72 6f 72 20 63 6f 64    This error cod
15d60 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20 66  e promotion.** f
15d70 6f 72 63 65 73 20 61 6e 20 61 75 74 6f 6d 61 74  orces an automat
15d80 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74  ic rollback of t
15d90 68 65 20 63 68 61 6e 67 65 73 2e 20 20 53 65 65  he changes.  See
15da0 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d   the.** <a href=
15db0 22 2f 63 76 73 74 72 61 63 2f 77 69 6b 69 3f 70  "/cvstrac/wiki?p
15dc0 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f  =CorruptionFollo
15dd0 77 69 6e 67 42 75 73 79 45 72 72 6f 72 22 3e 0a  wingBusyError">.
15de0 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c  ** CorruptionFol
15df0 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 3c  lowingBusyError<
15e00 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 20 66 6f  /a> wiki page fo
15e10 72 20 61 20 64 69 73 63 75 73 73 69 6f 6e 20 6f  r a discussion o
15e20 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 20 69 73  f why.** this is
15e30 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a 2a   important..**.*
15e40 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  * There can only
15e50 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73   be a single bus
15e60 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65  y handler define
15e70 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b 64  d for each.** [d
15e80 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
15e90 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 20  on].  Setting a 
15ea0 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72  new busy handler
15eb0 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20 70   clears any.** p
15ec0 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68 61  reviously set ha
15ed0 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 74 68 61  ndler.  Note tha
15ee0 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74  t calling [sqlit
15ef0 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28  e3_busy_timeout(
15f00 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 6f 20  )].** will also 
15f10 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68 65  set or clear the
15f20 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a   busy handler..*
15f30 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20 63 61  *.** The busy ca
15f40 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 6e 6f  llback should no
15f50 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74 69 6f  t take any actio
15f60 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66 79 20  ns which modify 
15f70 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
15f80 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20  connection that 
15f90 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79  invoked the busy
15fa0 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79 20 73   handler.  Any s
15fb0 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 72  uch actions.** r
15fc0 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e  esult in undefin
15fd0 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 20  ed behavior..** 
15fe0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
15ff0 3a 0a 2a 2a 20 5b 48 31 32 33 31 31 5d 20 5b 48  :.** [H12311] [H
16000 31 32 33 31 32 5d 20 5b 48 31 32 33 31 34 5d 20  12312] [H12314] 
16010 5b 48 31 32 33 31 36 5d 20 5b 48 31 32 33 31 38  [H12316] [H12318
16020 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75 73 79 20 68  ].**.** A busy h
16030 61 6e 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20  andler must not 
16040 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 61  close the databa
16050 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se connection.**
16060 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20 73 74   or [prepared st
16070 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 6e  atement] that in
16080 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79 20 68  voked the busy h
16090 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  andler..*/.SQLIT
160a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
160b0 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 73  3_busy_handler(s
160c0 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28  qlite3*, int(*)(
160d0 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64  void*,int), void
160e0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
160f0 52 45 46 3a 20 53 65 74 20 41 20 42 75 73 79 20  REF: Set A Busy 
16100 54 69 6d 65 6f 75 74 20 7b 48 31 32 33 34 30 7d  Timeout {H12340}
16110 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20   <S40410>.**.** 
16120 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
16130 73 20 61 20 5b 73 71 6c 69 74 65 33 5f 62 75 73  s a [sqlite3_bus
16140 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 79  y_handler | busy
16150 20 68 61 6e 64 6c 65 72 5d 20 74 68 61 74 20 73   handler] that s
16160 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 20 61 20 73  leeps.** for a s
16170 70 65 63 69 66 69 65 64 20 61 6d 6f 75 6e 74 20  pecified amount 
16180 6f 66 20 74 69 6d 65 20 77 68 65 6e 20 61 20 74  of time when a t
16190 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20  able is locked. 
161a0 20 54 68 65 20 68 61 6e 64 6c 65 72 0a 2a 2a 20   The handler.** 
161b0 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69  will sleep multi
161c0 70 6c 65 20 74 69 6d 65 73 20 75 6e 74 69 6c 20  ple times until 
161d0 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20 6d 69  at least "ms" mi
161e0 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c  lliseconds of sl
161f0 65 65 70 69 6e 67 0a 2a 2a 20 68 61 76 65 20 61  eeping.** have a
16200 63 63 75 6d 75 6c 61 74 65 64 2e 20 7b 48 31 32  ccumulated. {H12
16210 33 34 33 7d 20 41 66 74 65 72 20 22 6d 73 22 20  343} After "ms" 
16220 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20  milliseconds of 
16230 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 65  sleeping,.** the
16240 20 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73   handler returns
16250 20 30 20 77 68 69 63 68 20 63 61 75 73 65 73 20   0 which causes 
16260 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
16270 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53   to return.** [S
16280 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b  QLITE_BUSY] or [
16290 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f  SQLITE_IOERR_BLO
162a0 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c  CKED]..**.** Cal
162b0 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e  ling this routin
162c0 65 20 77 69 74 68 20 61 6e 20 61 72 67 75 6d 65  e with an argume
162d0 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  nt less than or 
162e0 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a 2a  equal to zero.**
162f0 20 74 75 72 6e 73 20 6f 66 66 20 61 6c 6c 20 62   turns off all b
16300 75 73 79 20 68 61 6e 64 6c 65 72 73 2e 0a 2a 2a  usy handlers..**
16310 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e  .** There can on
16320 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62  ly be a single b
16330 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20  usy handler for 
16340 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20  a particular.** 
16350 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
16360 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 67 69  tion] any any gi
16370 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 66 20  ven moment.  If 
16380 61 6e 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e  another busy han
16390 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64 65 66 69  dler.** was defi
163a0 6e 65 64 20 20 28 75 73 69 6e 67 20 5b 73 71 6c  ned  (using [sql
163b0 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65  ite3_busy_handle
163c0 72 28 29 5d 29 20 70 72 69 6f 72 20 74 6f 20 63  r()]) prior to c
163d0 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72  alling.** this r
163e0 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 6f 74 68  outine, that oth
163f0 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  er busy handler 
16400 69 73 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a  is cleared..**.*
16410 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
16420 2a 2a 20 5b 48 31 32 33 34 31 5d 20 5b 48 31 32  ** [H12341] [H12
16430 33 34 33 5d 20 5b 48 31 32 33 34 34 5d 0a 2a 2f  343] [H12344].*/
16440 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
16450 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d  sqlite3_busy_tim
16460 65 6f 75 74 28 73 71 6c 69 74 65 33 2a 2c 20 69  eout(sqlite3*, i
16470 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  nt ms);../*.** C
16480 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 65 6e 69  API3REF: Conveni
16490 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 20 46 6f  ence Routines Fo
164a0 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 72 69 65  r Running Querie
164b0 73 20 7b 48 31 32 33 37 30 7d 20 3c 53 31 30 30  s {H12370} <S100
164c0 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 69  00>.**.** Defini
164d0 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 65 73 75 6c  tion: A <b>resul
164e0 74 20 74 61 62 6c 65 3c 2f 62 3e 20 69 73 20 6d  t table</b> is m
164f0 65 6d 6f 72 79 20 64 61 74 61 20 73 74 72 75 63  emory data struc
16500 74 75 72 65 20 63 72 65 61 74 65 64 20 62 79 20  ture created by 
16510 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  the.** [sqlite3_
16520 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 69 6e 74  get_table()] int
16530 65 72 66 61 63 65 2e 20 20 41 20 72 65 73 75 6c  erface.  A resul
16540 74 20 74 61 62 6c 65 20 72 65 63 6f 72 64 73 20  t table records 
16550 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 20  the.** complete 
16560 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 66 72  query results fr
16570 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 71  om one or more q
16580 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ueries..**.** Th
16590 65 20 74 61 62 6c 65 20 63 6f 6e 63 65 70 74 75  e table conceptu
165a0 61 6c 6c 79 20 68 61 73 20 61 20 6e 75 6d 62 65  ally has a numbe
165b0 72 20 6f 66 20 72 6f 77 73 20 61 6e 64 20 63 6f  r of rows and co
165c0 6c 75 6d 6e 73 2e 20 20 42 75 74 0a 2a 2a 20 74  lumns.  But.** t
165d0 68 65 73 65 20 6e 75 6d 62 65 72 73 20 61 72 65  hese numbers are
165e0 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65   not part of the
165f0 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 69 74   result table it
16600 73 65 6c 66 2e 20 20 54 68 65 73 65 0a 2a 2a 20  self.  These.** 
16610 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61  numbers are obta
16620 69 6e 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  ined separately.
16630 20 20 4c 65 74 20 4e 20 62 65 20 74 68 65 20 6e    Let N be the n
16640 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a 2a 2a  umber of rows.**
16650 20 61 6e 64 20 4d 20 62 65 20 74 68 65 20 6e 75   and M be the nu
16660 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e  mber of columns.
16670 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20  .**.** A result 
16680 74 61 62 6c 65 20 69 73 20 61 6e 20 61 72 72 61  table is an arra
16690 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  y of pointers to
166a0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
166b0 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a   UTF-8 strings..
166c0 2a 2a 20 54 68 65 72 65 20 61 72 65 20 28 4e 2b  ** There are (N+
166d0 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73 20 69 6e  1)*M elements in
166e0 20 74 68 65 20 61 72 72 61 79 2e 20 20 54 68 65   the array.  The
166f0 20 66 69 72 73 74 20 4d 20 70 6f 69 6e 74 65 72   first M pointer
16700 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 7a 65  s point.** to ze
16710 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74  ro-terminated st
16720 72 69 6e 67 73 20 74 68 61 74 20 20 63 6f 6e 74  rings that  cont
16730 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  ain the names of
16740 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a   the columns..**
16750 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65   The remaining e
16760 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f 69 6e 74  ntries all point
16770 20 74 6f 20 71 75 65 72 79 20 72 65 73 75 6c 74   to query result
16780 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20  s.  NULL values 
16790 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 4e 55 4c  result.** in NUL
167a0 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20 41 6c 6c  L pointers.  All
167b0 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 61 72   other values ar
167c0 65 20 69 6e 20 74 68 65 69 72 20 55 54 46 2d 38  e in their UTF-8
167d0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
167e0 0a 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65  .** string repre
167f0 73 65 6e 74 61 74 69 6f 6e 20 61 73 20 72 65 74  sentation as ret
16800 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65  urned by [sqlite
16810 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d  3_column_text()]
16820 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74  ..**.** A result
16830 20 74 61 62 6c 65 20 6d 69 67 68 74 20 63 6f 6e   table might con
16840 73 69 73 74 20 6f 66 20 6f 6e 65 20 6f 72 20 6d  sist of one or m
16850 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ore memory alloc
16860 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20 69 73  ations..** It is
16870 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 70 61 73   not safe to pas
16880 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c 65  s a result table
16890 20 64 69 72 65 63 74 6c 79 20 74 6f 20 5b 73 71   directly to [sq
168a0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a  lite3_free()]..*
168b0 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65  * A result table
168c0 20 73 68 6f 75 6c 64 20 62 65 20 64 65 61 6c 6c   should be deall
168d0 6f 63 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71  ocated using [sq
168e0 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
168f0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 61 6e  ()]..**.** As an
16900 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 68 65 20   example of the 
16910 72 65 73 75 6c 74 20 74 61 62 6c 65 20 66 6f 72  result table for
16920 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 61 20 71  mat, suppose a q
16930 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 69  uery result.** i
16940 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
16950 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
16960 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20  <pre>.**        
16970 4e 61 6d 65 20 20 20 20 20 20 20 20 7c 20 41 67  Name        | Ag
16980 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d 2d 2d  e.**        ----
16990 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
169a0 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20 41 6c  ---.**        Al
169b0 69 63 65 20 20 20 20 20 20 20 7c 20 34 33 0a 2a  ice       | 43.*
169c0 2a 20 20 20 20 20 20 20 20 42 6f 62 20 20 20 20  *        Bob    
169d0 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 20 20 20       | 28.**    
169e0 20 20 20 20 43 69 6e 64 79 20 20 20 20 20 20 20      Cindy       
169f0 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f  | 21.** </pre></
16a00 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a  blockquote>.**.*
16a10 2a 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 20  * There are two 
16a20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20 61 6e  column (M==2) an
16a30 64 20 74 68 72 65 65 20 72 6f 77 73 20 28 4e 3d  d three rows (N=
16a40 3d 33 29 2e 20 20 54 68 75 73 20 74 68 65 0a 2a  =3).  Thus the.*
16a50 2a 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 68  * result table h
16a60 61 73 20 38 20 65 6e 74 72 69 65 73 2e 20 20 53  as 8 entries.  S
16a70 75 70 70 6f 73 65 20 74 68 65 20 72 65 73 75 6c  uppose the resul
16a80 74 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65  t table is store
16a90 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 72 61 79  d.** in an array
16aa0 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 6c 74 2e   names azResult.
16ab0 20 20 54 68 65 6e 20 61 7a 52 65 73 75 6c 74 20    Then azResult 
16ac0 68 6f 6c 64 73 20 74 68 69 73 20 63 6f 6e 74 65  holds this conte
16ad0 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  nt:.**.** <block
16ae0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
16af0 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23        azResult&#
16b00 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 22 3b 0a  91;0] = "Name";.
16b10 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
16b20 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 41 67 65  lt&#91;1] = "Age
16b30 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
16b40 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 3d 20 22  esult&#91;2] = "
16b50 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 20 20 20  Alice";.**      
16b60 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 33    azResult&#91;3
16b70 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 20 20 20  ] = "43";.**    
16b80 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31      azResult&#91
16b90 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a 2a 20  ;4] = "Bob";.** 
16ba0 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26         azResult&
16bb0 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 3b 0a 2a  #91;5] = "28";.*
16bc0 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c  *        azResul
16bd0 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 69 6e 64  t&#91;6] = "Cind
16be0 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a  y";.**        az
16bf0 52 65 73 75 6c 74 26 23 39 31 3b 37 5d 20 3d 20  Result&#91;7] = 
16c00 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  "21";.** </pre><
16c10 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
16c20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67  ** The sqlite3_g
16c30 65 74 5f 74 61 62 6c 65 28 29 20 66 75 6e 63 74  et_table() funct
16c40 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 6f 6e  ion evaluates on
16c50 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73 65 6d  e or more.** sem
16c60 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 74 65 64  icolon-separated
16c70 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
16c80 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d  in the zero-term
16c90 69 6e 61 74 65 64 20 55 54 46 2d 38 0a 2a 2a 20  inated UTF-8.** 
16ca0 73 74 72 69 6e 67 20 6f 66 20 69 74 73 20 32 6e  string of its 2n
16cb0 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 49 74  d parameter.  It
16cc0 20 72 65 74 75 72 6e 73 20 61 20 72 65 73 75 6c   returns a resul
16cd0 74 20 74 61 62 6c 65 20 74 6f 20 74 68 65 0a 2a  t table to the.*
16ce0 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 65 6e 20  * pointer given 
16cf0 69 6e 20 69 74 73 20 33 72 64 20 70 61 72 61 6d  in its 3rd param
16d00 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  eter..**.** Afte
16d10 72 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75  r the calling fu
16d20 6e 63 74 69 6f 6e 20 68 61 73 20 66 69 6e 69 73  nction has finis
16d30 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 65  hed using the re
16d40 73 75 6c 74 2c 20 69 74 20 73 68 6f 75 6c 64 0a  sult, it should.
16d50 2a 2a 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e  ** pass the poin
16d60 74 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c  ter to the resul
16d70 74 20 74 61 62 6c 65 20 74 6f 20 73 71 6c 69 74  t table to sqlit
16d80 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 20  e3_free_table() 
16d90 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 72  in order to.** r
16da0 65 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72  elease the memor
16db0 79 20 74 68 61 74 20 77 61 73 20 6d 61 6c 6c 6f  y that was mallo
16dc0 63 65 64 2e 20 20 42 65 63 61 75 73 65 20 6f 66  ced.  Because of
16dd0 20 74 68 65 20 77 61 79 20 74 68 65 0a 2a 2a 20   the way the.** 
16de0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
16df0 29 5d 20 68 61 70 70 65 6e 73 20 77 69 74 68 69  )] happens withi
16e00 6e 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  n sqlite3_get_ta
16e10 62 6c 65 28 29 2c 20 74 68 65 20 63 61 6c 6c 69  ble(), the calli
16e20 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d  ng.** function m
16e30 75 73 74 20 6e 6f 74 20 74 72 79 20 74 6f 20 63  ust not try to c
16e40 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65  all [sqlite3_fre
16e50 65 28 29 5d 20 64 69 72 65 63 74 6c 79 2e 20 20  e()] directly.  
16e60 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  Only.** [sqlite3
16e70 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 20 69  _free_table()] i
16e80 73 20 61 62 6c 65 20 74 6f 20 72 65 6c 65 61 73  s able to releas
16e90 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 72 6f  e the memory pro
16ea0 70 65 72 6c 79 20 61 6e 64 20 73 61 66 65 6c 79  perly and safely
16eb0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
16ec0 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20  te3_get_table() 
16ed0 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6d 70  interface is imp
16ee0 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 77 72  lemented as a wr
16ef0 61 70 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20  apper around.** 
16f00 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
16f10 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67  .  The sqlite3_g
16f20 65 74 5f 74 61 62 6c 65 28 29 20 72 6f 75 74 69  et_table() routi
16f30 6e 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  ne does not have
16f40 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 61 6e   access.** to an
16f50 79 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 20  y internal data 
16f60 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 53 51  structures of SQ
16f70 4c 69 74 65 2e 20 20 49 74 20 75 73 65 73 20 6f  Lite.  It uses o
16f80 6e 6c 79 20 74 68 65 20 70 75 62 6c 69 63 0a 2a  nly the public.*
16f90 2a 20 69 6e 74 65 72 66 61 63 65 20 64 65 66 69  * interface defi
16fa0 6e 65 64 20 68 65 72 65 2e 20 20 41 73 20 61 20  ned here.  As a 
16fb0 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20 65 72 72  consequence, err
16fc0 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72 20 69  ors that occur i
16fd0 6e 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72  n the.** wrapper
16fe0 20 6c 61 79 65 72 20 6f 75 74 73 69 64 65 20 6f   layer outside o
16ff0 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 5b  f the internal [
17000 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
17010 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0a 2a 2a 20  call are not.** 
17020 72 65 66 6c 65 63 74 65 64 20 69 6e 20 73 75 62  reflected in sub
17030 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f  sequent calls to
17040 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64   [sqlite3_errcod
17050 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  e()] or [sqlite3
17060 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 0a 2a  _errmsg()]..**.*
17070 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
17080 2a 2a 20 5b 48 31 32 33 37 31 5d 20 5b 48 31 32  ** [H12371] [H12
17090 33 37 33 5d 20 5b 48 31 32 33 37 34 5d 20 5b 48  373] [H12374] [H
170a0 31 32 33 37 36 5d 20 5b 48 31 32 33 37 39 5d 20  12376] [H12379] 
170b0 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a 53 51 4c 49  [H12382].*/.SQLI
170c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
170d0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 20  e3_get_table(.  
170e0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
170f0 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e        /* An open
17100 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63   database */.  c
17110 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
17120 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62       /* SQL to b
17130 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20  e evaluated */. 
17140 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75   char ***pazResu
17150 6c 74 2c 20 20 20 20 2f 2a 20 52 65 73 75 6c 74  lt,    /* Result
17160 73 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a  s of the query *
17170 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c 20  /.  int *pnRow, 
17180 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
17190 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f  ber of result ro
171a0 77 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  ws written here 
171b0 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75  */.  int *pnColu
171c0 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  mn,        /* Nu
171d0 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63  mber of result c
171e0 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e 20 68  olumns written h
171f0 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ere */.  char **
17200 70 7a 45 72 72 6d 73 67 20 20 20 20 20 20 20 2f  pzErrmsg       /
17210 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74  * Error msg writ
17220 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 53  ten here */.);.S
17230 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
17240 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
17250 65 28 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 29  e(char **result)
17260 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
17270 46 3a 20 46 6f 72 6d 61 74 74 65 64 20 53 74 72  F: Formatted Str
17280 69 6e 67 20 50 72 69 6e 74 69 6e 67 20 46 75 6e  ing Printing Fun
17290 63 74 69 6f 6e 73 20 7b 48 31 37 34 30 30 7d 20  ctions {H17400} 
172a0 3c 53 37 30 30 30 30 3e 3c 53 32 30 30 30 30 3e  <S70000><S20000>
172b0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
172c0 74 69 6e 65 73 20 61 72 65 20 77 6f 72 6b 2d 61  tines are work-a
172d0 6c 69 6b 65 73 20 6f 66 20 74 68 65 20 22 70 72  likes of the "pr
172e0 69 6e 74 66 28 29 22 20 66 61 6d 69 6c 79 20 6f  intf()" family o
172f0 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 66  f functions.** f
17300 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64  rom the standard
17310 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a   C library..**.*
17320 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 70  * The sqlite3_mp
17330 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c 69  rintf() and sqli
17340 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 29 20 72  te3_vmprintf() r
17350 6f 75 74 69 6e 65 73 20 77 72 69 74 65 20 74 68  outines write th
17360 65 69 72 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69  eir.** results i
17370 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
17380 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  ned from [sqlite
17390 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 20  3_malloc()]..** 
173a0 54 68 65 20 73 74 72 69 6e 67 73 20 72 65 74 75  The strings retu
173b0 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 74 77  rned by these tw
173c0 6f 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c  o routines shoul
173d0 64 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64  d be.** released
173e0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 66 72 65   by [sqlite3_fre
173f0 65 28 29 5d 2e 20 20 42 6f 74 68 20 72 6f 75 74  e()].  Both rout
17400 69 6e 65 73 20 72 65 74 75 72 6e 20 61 0a 2a 2a  ines return a.**
17410 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66   NULL pointer if
17420 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
17430 28 29 5d 20 69 73 20 75 6e 61 62 6c 65 20 74 6f  ()] is unable to
17440 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68   allocate enough
17450 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f  .** memory to ho
17460 6c 64 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  ld the resulting
17470 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49   string..**.** I
17480 6e 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e  n sqlite3_snprin
17490 74 66 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20  tf() routine is 
174a0 73 69 6d 69 6c 61 72 20 74 6f 20 22 73 6e 70 72  similar to "snpr
174b0 69 6e 74 66 28 29 22 20 66 72 6f 6d 0a 2a 2a 20  intf()" from.** 
174c0 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c  the standard C l
174d0 69 62 72 61 72 79 2e 20 20 54 68 65 20 72 65 73  ibrary.  The res
174e0 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e 20 69  ult is written i
174f0 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66 66 65  nto the.** buffe
17500 72 20 73 75 70 70 6c 69 65 64 20 61 73 20 74 68  r supplied as th
17510 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
17520 65 72 20 77 68 6f 73 65 20 73 69 7a 65 20 69 73  er whose size is
17530 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65   given by.** the
17540 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
17550 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  . Note that the 
17560 6f 72 64 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20  order of the.** 
17570 66 69 72 73 74 20 74 77 6f 20 70 61 72 61 6d 65  first two parame
17580 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65 64  ters is reversed
17590 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74 66 28 29   from snprintf()
175a0 2e 20 20 54 68 69 73 20 69 73 20 61 6e 0a 2a 2a  .  This is an.**
175b0 20 68 69 73 74 6f 72 69 63 61 6c 20 61 63 63 69   historical acci
175c0 64 65 6e 74 20 74 68 61 74 20 63 61 6e 6e 6f 74  dent that cannot
175d0 20 62 65 20 66 69 78 65 64 20 77 69 74 68 6f 75   be fixed withou
175e0 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 61  t breaking.** ba
175f0 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
17600 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20 61 6c 73  ility.  Note als
17610 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 73  o that sqlite3_s
17620 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 72 65 74  nprintf().** ret
17630 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
17640 6f 20 69 74 73 20 62 75 66 66 65 72 20 69 6e 73  o its buffer ins
17650 74 65 61 64 20 6f 66 20 74 68 65 20 6e 75 6d 62  tead of the numb
17660 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74  er of.** charact
17670 65 72 73 20 61 63 74 75 61 6c 6c 79 20 77 72 69  ers actually wri
17680 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 62 75  tten into the bu
17690 66 66 65 72 2e 20 20 57 65 20 61 64 6d 69 74 20  ffer.  We admit 
176a0 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  that.** the numb
176b0 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
176c0 20 77 72 69 74 74 65 6e 20 77 6f 75 6c 64 20 62   written would b
176d0 65 20 61 20 6d 6f 72 65 20 75 73 65 66 75 6c 20  e a more useful 
176e0 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20  return.** value 
176f0 62 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 63 68  but we cannot ch
17700 61 6e 67 65 20 74 68 65 20 69 6d 70 6c 65 6d 65  ange the impleme
17710 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74  ntation of sqlit
17720 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a  e3_snprintf().**
17730 20 6e 6f 77 20 77 69 74 68 6f 75 74 20 62 72 65   now without bre
17740 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 62 69 6c  aking compatibil
17750 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 6c 6f  ity..**.** As lo
17760 6e 67 20 61 73 20 74 68 65 20 62 75 66 66 65 72  ng as the buffer
17770 20 73 69 7a 65 20 69 73 20 67 72 65 61 74 65 72   size is greater
17780 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73 71 6c 69   than zero, sqli
17790 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a  te3_snprintf().*
177a0 2a 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  * guarantees tha
177b0 74 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20  t the buffer is 
177c0 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d  always zero-term
177d0 69 6e 61 74 65 64 2e 20 20 54 68 65 20 66 69 72  inated.  The fir
177e0 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  st.** parameter 
177f0 22 6e 22 20 69 73 20 74 68 65 20 74 6f 74 61 6c  "n" is the total
17800 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66   size of the buf
17810 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20 73  fer, including s
17820 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20  pace for.** the 
17830 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e  zero terminator.
17840 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67 65 73 74    So the longest
17850 20 73 74 72 69 6e 67 20 74 68 61 74 20 63 61 6e   string that can
17860 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a   be completely.*
17870 2a 20 77 72 69 74 74 65 6e 20 77 69 6c 6c 20 62  * written will b
17880 65 20 6e 2d 31 20 63 68 61 72 61 63 74 65 72 73  e n-1 characters
17890 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
178a0 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d 70 6c 65  utines all imple
178b0 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64 69 74 69  ment some additi
178c0 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69 6e 67 0a  onal formatting.
178d0 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 20  ** options that 
178e0 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72 20 63  are useful for c
178f0 6f 6e 73 74 72 75 63 74 69 6e 67 20 53 51 4c 20  onstructing SQL 
17900 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 41  statements..** A
17910 6c 6c 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20  ll of the usual 
17920 70 72 69 6e 74 66 28 29 20 66 6f 72 6d 61 74 74  printf() formatt
17930 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61 70 70 6c  ing options appl
17940 79 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c  y.  In addition,
17950 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61 72 65   there.** is are
17960 20 22 25 71 22 2c 20 22 25 51 22 2c 20 61 6e 64   "%q", "%Q", and
17970 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73 2e 0a 2a   "%z" options..*
17980 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f 70 74 69  *.** The %q opti
17990 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 73  on works like %s
179a0 20 69 6e 20 74 68 61 74 20 69 74 20 73 75 62 73   in that it subs
179b0 74 69 74 75 74 65 73 20 61 20 6e 75 6c 6c 2d 74  titutes a null-t
179c0 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72  erminated.** str
179d0 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 61 72 67  ing from the arg
179e0 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20 42 75 74  ument list.  But
179f0 20 25 71 20 61 6c 73 6f 20 64 6f 75 62 6c 65 73   %q also doubles
17a00 20 65 76 65 72 79 20 27 5c 27 27 20 63 68 61 72   every '\'' char
17a10 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20 69 73 20  acter..** %q is 
17a20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65  designed for use
17a30 20 69 6e 73 69 64 65 20 61 20 73 74 72 69 6e 67   inside a string
17a40 20 6c 69 74 65 72 61 6c 2e 20 20 42 79 20 64 6f   literal.  By do
17a50 75 62 6c 69 6e 67 20 65 61 63 68 20 27 5c 27 27  ubling each '\''
17a60 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 69 74  .** character it
17a70 20 65 73 63 61 70 65 73 20 74 68 61 74 20 63 68   escapes that ch
17a80 61 72 61 63 74 65 72 20 61 6e 64 20 61 6c 6c 6f  aracter and allo
17a90 77 73 20 69 74 20 74 6f 20 62 65 20 69 6e 73 65  ws it to be inse
17aa0 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65  rted into.** the
17ab0 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46   string..**.** F
17ac0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 75  or example, assu
17ad0 6d 65 20 74 68 65 20 73 74 72 69 6e 67 20 76 61  me the string va
17ae0 72 69 61 62 6c 65 20 7a 54 65 78 74 20 63 6f 6e  riable zText con
17af0 74 61 69 6e 73 20 74 65 78 74 20 61 73 20 66 6f  tains text as fo
17b00 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c  llows:.**.** <bl
17b10 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a  ockquote><pre>.*
17b20 2a 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d  *  char *zText =
17b30 20 22 49 74 27 73 20 61 20 68 61 70 70 79 20 64   "It's a happy d
17b40 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  ay!";.** </pre><
17b50 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
17b60 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73 65 20 74  ** One can use t
17b70 68 69 73 20 74 65 78 74 20 69 6e 20 61 6e 20 53  his text in an S
17b80 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73 20  QL statement as 
17b90 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c  follows:.**.** <
17ba0 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
17bb0 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 51 4c 20  .**  char *zSQL 
17bc0 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
17bd0 66 28 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74  f("INSERT INTO t
17be0 61 62 6c 65 20 56 41 4c 55 45 53 28 27 25 71 27  able VALUES('%q'
17bf0 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 20  )", zText);.**  
17c00 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
17c10 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29 3b   zSQL, 0, 0, 0);
17c20 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .**  sqlite3_fre
17c30 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72  e(zSQL);.** </pr
17c40 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
17c50 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68  **.** Because th
17c60 65 20 25 71 20 66 6f 72 6d 61 74 20 73 74 72 69  e %q format stri
17c70 6e 67 20 69 73 20 75 73 65 64 2c 20 74 68 65 20  ng is used, the 
17c80 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 20 69  '\'' character i
17c90 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 73 20 65 73  n zText.** is es
17ca0 63 61 70 65 64 20 61 6e 64 20 74 68 65 20 53 51  caped and the SQ
17cb0 4c 20 67 65 6e 65 72 61 74 65 64 20 69 73 20 61  L generated is a
17cc0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
17cd0 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72   <blockquote><pr
17ce0 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e  e>.**  INSERT IN
17cf0 54 4f 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53  TO table1 VALUES
17d00 28 27 49 74 27 27 73 20 61 20 68 61 70 70 79 20  ('It''s a happy 
17d10 64 61 79 21 27 29 0a 2a 2a 20 3c 2f 70 72 65 3e  day!').** </pre>
17d20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
17d30 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 6f 72 72  .** This is corr
17d40 65 63 74 2e 20 20 48 61 64 20 77 65 20 75 73 65  ect.  Had we use
17d50 64 20 25 73 20 69 6e 73 74 65 61 64 20 6f 66 20  d %s instead of 
17d60 25 71 2c 20 74 68 65 20 67 65 6e 65 72 61 74 65  %q, the generate
17d70 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 6c 64 20 68  d SQL.** would h
17d80 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69 6b 65 20  ave looked like 
17d90 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  this:.**.** <blo
17da0 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
17db0 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61    INSERT INTO ta
17dc0 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 74 27  ble1 VALUES('It'
17dd0 73 20 61 20 68 61 70 70 79 20 64 61 79 21 27 29  s a happy day!')
17de0 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  ;.** </pre></blo
17df0 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54  ckquote>.**.** T
17e00 68 69 73 20 73 65 63 6f 6e 64 20 65 78 61 6d 70  his second examp
17e10 6c 65 20 69 73 20 61 6e 20 53 51 4c 20 73 79 6e  le is an SQL syn
17e20 74 61 78 20 65 72 72 6f 72 2e 20 20 41 73 20 61  tax error.  As a
17e30 20 67 65 6e 65 72 61 6c 20 72 75 6c 65 20 79 6f   general rule yo
17e40 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c 77 61  u should.** alwa
17e50 79 73 20 75 73 65 20 25 71 20 69 6e 73 74 65 61  ys use %q instea
17e60 64 20 6f 66 20 25 73 20 77 68 65 6e 20 69 6e 73  d of %s when ins
17e70 65 72 74 69 6e 67 20 74 65 78 74 20 69 6e 74 6f  erting text into
17e80 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61   a string litera
17e90 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 51 20  l..**.** The %Q 
17ea0 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b  option works lik
17eb0 65 20 25 71 20 65 78 63 65 70 74 20 69 74 20 61  e %q except it a
17ec0 6c 73 6f 20 61 64 64 73 20 73 69 6e 67 6c 65 20  lso adds single 
17ed0 71 75 6f 74 65 73 20 61 72 6f 75 6e 64 0a 2a 2a  quotes around.**
17ee0 20 74 68 65 20 6f 75 74 73 69 64 65 20 6f 66 20   the outside of 
17ef0 74 68 65 20 74 6f 74 61 6c 20 73 74 72 69 6e 67  the total string
17f00 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c  .  Additionally,
17f10 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   if the paramete
17f20 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75  r in the.** argu
17f30 6d 65 6e 74 20 6c 69 73 74 20 69 73 20 61 20 4e  ment list is a N
17f40 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 25 51 20  ULL pointer, %Q 
17f50 73 75 62 73 74 69 74 75 74 65 73 20 74 68 65 20  substitutes the 
17f60 74 65 78 74 20 22 4e 55 4c 4c 22 20 28 77 69 74  text "NULL" (wit
17f70 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 6c 65 20 71  hout.** single q
17f80 75 6f 74 65 73 29 20 69 6e 20 70 6c 61 63 65 20  uotes) in place 
17f90 6f 66 20 74 68 65 20 25 51 20 6f 70 74 69 6f 6e  of the %Q option
17fa0 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70  .  So, for examp
17fb0 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64 20 73 61  le, one could sa
17fc0 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  y:.**.** <blockq
17fd0 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63  uote><pre>.**  c
17fe0 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69  har *zSQL = sqli
17ff0 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53  te3_mprintf("INS
18000 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56  ERT INTO table V
18010 41 4c 55 45 53 28 25 51 29 22 2c 20 7a 54 65 78  ALUES(%Q)", zTex
18020 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f  t);.**  sqlite3_
18030 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30  exec(db, zSQL, 0
18040 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c  , 0, 0);.**  sql
18050 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b  ite3_free(zSQL);
18060 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
18070 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68  kquote>.**.** Th
18080 65 20 63 6f 64 65 20 61 62 6f 76 65 20 77 69 6c  e code above wil
18090 6c 20 72 65 6e 64 65 72 20 61 20 63 6f 72 72 65  l render a corre
180a0 63 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ct SQL statement
180b0 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a 2a 2a 20   in the zSQL.** 
180c0 76 61 72 69 61 62 6c 65 20 65 76 65 6e 20 69 66  variable even if
180d0 20 74 68 65 20 7a 54 65 78 74 20 76 61 72 69 61   the zText varia
180e0 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f  ble is a NULL po
180f0 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  inter..**.** The
18100 20 22 25 7a 22 20 66 6f 72 6d 61 74 74 69 6e 67   "%z" formatting
18110 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 65 78   option works ex
18120 61 63 74 6c 79 20 6c 69 6b 65 20 22 25 73 22 20  actly like "%s" 
18130 77 69 74 68 20 74 68 65 0a 2a 2a 20 61 64 64 69  with the.** addi
18140 74 69 6f 6e 20 74 68 61 74 20 61 66 74 65 72 20  tion that after 
18150 74 68 65 20 73 74 72 69 6e 67 20 68 61 73 20 62  the string has b
18160 65 65 6e 20 72 65 61 64 20 61 6e 64 20 63 6f 70  een read and cop
18170 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20  ied into.** the 
18180 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69 74 65 33  result, [sqlite3
18190 5f 66 72 65 65 28 29 5d 20 69 73 20 63 61 6c 6c  _free()] is call
181a0 65 64 20 6f 6e 20 74 68 65 20 69 6e 70 75 74 20  ed on the input 
181b0 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d 0a 2a 2a  string. {END}.**
181c0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
181d0 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 5d 20 5b 48  :.** [H17403] [H
181e0 31 37 34 30 36 5d 20 5b 48 31 37 34 30 37 5d 0a  17406] [H17407].
181f0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  */.SQLITE_API ch
18200 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69  ar *sqlite3_mpri
18210 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ntf(const char*,
18220 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ...);.SQLITE_API
18230 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76   char *sqlite3_v
18240 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68  mprintf(const ch
18250 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53  ar*, va_list);.S
18260 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
18270 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
18280 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74  (int,char*,const
18290 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 0a 2f   char*, ...);../
182a0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d  *.** CAPI3REF: M
182b0 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e  emory Allocation
182c0 20 53 75 62 73 79 73 74 65 6d 20 7b 48 31 37 33   Subsystem {H173
182d0 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a  00} <S20000>.**.
182e0 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  ** The SQLite co
182f0 72 65 20 20 75 73 65 73 20 74 68 65 73 65 20 74  re  uses these t
18300 68 72 65 65 20 72 6f 75 74 69 6e 65 73 20 66 6f  hree routines fo
18310 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 6f 77 6e  r all of its own
18320 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 6d 65 6d  .** internal mem
18330 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e  ory allocation n
18340 65 65 64 73 2e 20 22 43 6f 72 65 22 20 69 6e 20  eeds. "Core" in 
18350 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e  the previous sen
18360 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f  tence.** does no
18370 74 20 69 6e 63 6c 75 64 65 20 6f 70 65 72 61 74  t include operat
18380 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69  ing-system speci
18390 66 69 63 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e  fic VFS implemen
183a0 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20  tation.  The.** 
183b0 57 69 6e 64 6f 77 73 20 56 46 53 20 75 73 65 73  Windows VFS uses
183c0 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 28 29   native malloc()
183d0 20 61 6e 64 20 66 72 65 65 28 29 20 66 6f 72 20   and free() for 
183e0 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f 6e 73 2e  some operations.
183f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
18400 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 6f 75 74  e3_malloc() rout
18410 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ine returns a po
18420 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b  inter to a block
18430 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 61 74  .** of memory at
18440 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69   least N bytes i
18450 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65 72 65 20  n length, where 
18460 4e 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74  N is the paramet
18470 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65  er..** If sqlite
18480 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 75 6e  3_malloc() is un
18490 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 73  able to obtain s
184a0 75 66 66 69 63 69 65 6e 74 20 66 72 65 65 0a 2a  ufficient free.*
184b0 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 72 65 74  * memory, it ret
184c0 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  urns a NULL poin
184d0 74 65 72 2e 20 20 49 66 20 74 68 65 20 70 61 72  ter.  If the par
184e0 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a 2a 20 73  ameter N to.** s
184f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
18500 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74  is zero or negat
18510 69 76 65 20 74 68 65 6e 20 73 71 6c 69 74 65 33  ive then sqlite3
18520 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e  _malloc() return
18530 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  s.** a NULL poin
18540 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69  ter..**.** Calli
18550 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ng sqlite3_free(
18560 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72  ) with a pointer
18570 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 74 75   previously retu
18580 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74  rned.** by sqlit
18590 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73  e3_malloc() or s
185a0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
185b0 20 72 65 6c 65 61 73 65 73 20 74 68 61 74 20 6d   releases that m
185c0 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 68 61 74  emory so.** that
185d0 20 69 74 20 6d 69 67 68 74 20 62 65 20 72 65 75   it might be reu
185e0 73 65 64 2e 20 20 54 68 65 20 73 71 6c 69 74 65  sed.  The sqlite
185f0 33 5f 66 72 65 65 28 29 20 72 6f 75 74 69 6e 65  3_free() routine
18600 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 20 69   is.** a no-op i
18610 66 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68  f is called with
18620 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e   a NULL pointer.
18630 20 20 50 61 73 73 69 6e 67 20 61 20 4e 55 4c 4c    Passing a NULL
18640 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 73   pointer.** to s
18650 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 73  qlite3_free() is
18660 20 68 61 72 6d 6c 65 73 73 2e 20 20 41 66 74 65   harmless.  Afte
18670 72 20 62 65 69 6e 67 20 66 72 65 65 64 2c 20 6d  r being freed, m
18680 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75 6c 64 20  emory.** should 
18690 6e 65 69 74 68 65 72 20 62 65 20 72 65 61 64 20  neither be read 
186a0 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20 20 45 76  nor written.  Ev
186b0 65 6e 20 72 65 61 64 69 6e 67 20 70 72 65 76 69  en reading previ
186c0 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a 2a 20 6d  ously freed.** m
186d0 65 6d 6f 72 79 20 6d 69 67 68 74 20 72 65 73 75  emory might resu
186e0 6c 74 20 69 6e 20 61 20 73 65 67 6d 65 6e 74 61  lt in a segmenta
186f0 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72 20 6f 74  tion fault or ot
18700 68 65 72 20 73 65 76 65 72 65 20 65 72 72 6f 72  her severe error
18710 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 6f 72 72  ..** Memory corr
18720 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 6d 65 6e  uption, a segmen
18730 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c 20 6f 72  tation fault, or
18740 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72   other severe er
18750 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20 72 65 73  ror.** might res
18760 75 6c 74 20 69 66 20 73 71 6c 69 74 65 33 5f 66  ult if sqlite3_f
18770 72 65 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ree() is called 
18780 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20  with a non-NULL 
18790 70 6f 69 6e 74 65 72 20 74 68 61 74 0a 2a 2a 20  pointer that.** 
187a0 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64  was not obtained
187b0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
187c0 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65  lloc() or sqlite
187d0 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a  3_realloc()..**.
187e0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
187f0 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65 72 66 61  ealloc() interfa
18800 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72  ce attempts to r
18810 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72 69 6f 72  esize a.** prior
18820 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
18830 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c 65 61 73  on to be at leas
18840 74 20 4e 20 62 79 74 65 73 2c 20 77 68 65 72 65  t N bytes, where
18850 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 73 65 63   N is the.** sec
18860 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20  ond parameter.  
18870 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  The memory alloc
18880 61 74 69 6f 6e 20 74 6f 20 62 65 20 72 65 73 69  ation to be resi
18890 7a 65 64 20 69 73 20 74 68 65 20 66 69 72 73 74  zed is the first
188a0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 20  .** parameter.  
188b0 49 66 20 74 68 65 20 66 69 72 73 74 20 70 61 72  If the first par
188c0 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
188d0 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69  3_realloc().** i
188e0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
188f0 20 74 68 65 6e 20 69 74 73 20 62 65 68 61 76 69   then its behavi
18900 6f 72 20 69 73 20 69 64 65 6e 74 69 63 61 6c 20  or is identical 
18910 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71  to calling.** sq
18920 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 4e 29 20  lite3_malloc(N) 
18930 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73  where N is the s
18940 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
18950 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  to sqlite3_reall
18960 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74 68 65 20  oc()..** If the 
18970 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
18980 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c   to sqlite3_real
18990 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f 20 6f 72  loc() is zero or
189a0 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 74 68 65  .** negative the
189b0 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  n the behavior i
189c0 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61  s exactly the sa
189d0 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67 0a 2a 2a  me as calling.**
189e0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 50 29   sqlite3_free(P)
189f0 20 77 68 65 72 65 20 50 20 69 73 20 74 68 65 20   where P is the 
18a00 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
18a10 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  to sqlite3_reall
18a20 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69 74 65 33  oc()..** sqlite3
18a30 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 72  _realloc() retur
18a40 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
18a50 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
18a60 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 61  ion.** of at lea
18a70 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69  st N bytes in si
18a80 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 73 75  ze or NULL if su
18a90 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 79 20  fficient memory 
18aa0 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 0a  is unavailable..
18ab0 2a 2a 20 49 66 20 4d 20 69 73 20 74 68 65 20 73  ** If M is the s
18ac0 69 7a 65 20 6f 66 20 74 68 65 20 70 72 69 6f 72  ize of the prior
18ad0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 74 68 65   allocation, the
18ae0 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79 74 65 73  n min(N,M) bytes
18af0 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72 69 6f 72  .** of the prior
18b00 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 72 65 20   allocation are 
18b10 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20  copied into the 
18b20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 62 75 66  beginning of buf
18b30 66 65 72 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  fer returned.** 
18b40 62 79 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  by sqlite3_reall
18b50 6f 63 28 29 20 61 6e 64 20 74 68 65 20 70 72 69  oc() and the pri
18b60 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  or allocation is
18b70 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 73 71   freed..** If sq
18b80 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20  lite3_realloc() 
18b90 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c 20 74 68  returns NULL, th
18ba0 65 6e 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c  en the prior all
18bb0 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f  ocation.** is no
18bc0 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  t freed..**.** T
18bd0 68 65 20 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e  he memory return
18be0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61  ed by sqlite3_ma
18bf0 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71 6c 69 74  lloc() and sqlit
18c00 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20  e3_realloc().** 
18c10 69 73 20 61 6c 77 61 79 73 20 61 6c 69 67 6e 65  is always aligne
18c20 64 20 74 6f 20 61 74 20 6c 65 61 73 74 20 61 6e  d to at least an
18c30 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61 72 79   8 byte boundary
18c40 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68  . {END}.**.** Th
18c50 65 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d  e default implem
18c60 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
18c70 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
18c80 6e 20 73 75 62 73 79 73 74 65 6d 20 75 73 65 73  n subsystem uses
18c90 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 28 29  .** the malloc()
18ca0 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 6e 64 20  , realloc() and 
18cb0 66 72 65 65 28 29 20 70 72 6f 76 69 64 65 64 20  free() provided 
18cc0 62 79 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  by the standard 
18cd0 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 7b 48  C library..** {H
18ce0 31 37 33 38 32 7d 20 48 6f 77 65 76 65 72 2c 20  17382} However, 
18cf0 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  if SQLite is com
18d00 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a  piled with the.*
18d10 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f  * SQLITE_MEMORY_
18d20 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20  SIZE=<i>NNN</i> 
18d30 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d  C preprocessor m
18d40 61 63 72 6f 20 28 77 68 65 72 65 20 3c 69 3e 4e  acro (where <i>N
18d50 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20 61 6e 20  NN</i>.** is an 
18d60 69 6e 74 65 67 65 72 29 2c 20 74 68 65 6e 20 53  integer), then S
18d70 51 4c 69 74 65 20 63 72 65 61 74 65 20 61 20 73  QLite create a s
18d80 74 61 74 69 63 20 61 72 72 61 79 20 6f 66 20 61  tatic array of a
18d90 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69 3e 4e 4e  t least.** <i>NN
18da0 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69 6e 20 73  N</i> bytes in s
18db0 69 7a 65 20 61 6e 64 20 75 73 65 73 20 74 68 61  ize and uses tha
18dc0 74 20 61 72 72 61 79 20 66 6f 72 20 61 6c 6c 20  t array for all 
18dd0 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 0a 2a  of its dynamic.*
18de0 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  * memory allocat
18df0 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45 4e 44 7d  ion needs. {END}
18e00 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d    Additional mem
18e10 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 6f 70  ory allocator op
18e20 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20 62 65 20  tions.** may be 
18e30 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20  added in future 
18e40 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20  releases..**.** 
18e50 49 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f  In SQLite versio
18e60 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33 2e 35 2e  n 3.5.0 and 3.5.
18e70 31 2c 20 69 74 20 77 61 73 20 70 6f 73 73 69 62  1, it was possib
18e80 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a 2a 2a 20  le to define.** 
18e90 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  the SQLITE_OMIT_
18ea0 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54 49 4f  MEMORY_ALLOCATIO
18eb0 4e 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61  N which would ca
18ec0 75 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e  use the built-in
18ed0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
18ee0 6f 6e 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74  on of these rout
18ef0 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d 69 74 74  ines to be omitt
18f00 65 64 2e 20 20 54 68 61 74 20 63 61 70 61 62 69  ed.  That capabi
18f10 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f  lity.** is no lo
18f20 6e 67 65 72 20 70 72 6f 76 69 64 65 64 2e 20 20  nger provided.  
18f30 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e 20 6d 65  Only built-in me
18f40 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 20  mory allocators 
18f50 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a  can be used..**.
18f60 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77 73 20 4f  ** The Windows O
18f70 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65  S interface laye
18f80 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68 65 20 73  r calls.** the s
18f90 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 61  ystem malloc() a
18fa0 6e 64 20 66 72 65 65 28 29 20 64 69 72 65 63 74  nd free() direct
18fb0 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65 72 74 69  ly when converti
18fc0 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 73 20  ng.** filenames 
18fd0 62 65 74 77 65 65 6e 20 74 68 65 20 55 54 46 2d  between the UTF-
18fe0 38 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20  8 encoding used 
18ff0 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64  by SQLite.** and
19000 20 77 68 61 74 65 76 65 72 20 66 69 6c 65 6e 61   whatever filena
19010 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75  me encoding is u
19020 73 65 64 20 62 79 20 74 68 65 20 70 61 72 74 69  sed by the parti
19030 63 75 6c 61 72 20 57 69 6e 64 6f 77 73 0a 2a 2a  cular Windows.**
19040 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e 2e 20 20   installation.  
19050 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  Memory allocatio
19060 6e 20 65 72 72 6f 72 73 20 61 72 65 20 64 65 74  n errors are det
19070 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68  ected, but.** th
19080 65 79 20 61 72 65 20 72 65 70 6f 72 74 65 64 20  ey are reported 
19090 62 61 63 6b 20 61 73 20 5b 53 51 4c 49 54 45 5f  back as [SQLITE_
190a0 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a 2a 2a 20  CANTOPEN] or.** 
190b0 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5d 20 72  [SQLITE_IOERR] r
190c0 61 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49  ather than [SQLI
190d0 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a  TE_NOMEM]..**.**
190e0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
190f0 2a 20 5b 48 31 37 33 30 33 5d 20 5b 48 31 37 33  * [H17303] [H173
19100 30 34 5d 20 5b 48 31 37 33 30 35 5d 20 5b 48 31  04] [H17305] [H1
19110 37 33 30 36 5d 20 5b 48 31 37 33 31 30 5d 20 5b  7306] [H17310] [
19120 48 31 37 33 31 32 5d 20 5b 48 31 37 33 31 35 5d  H17312] [H17315]
19130 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 20 5b 48 31   [H17318].** [H1
19140 37 33 32 31 5d 20 5b 48 31 37 33 32 32 5d 20 5b  7321] [H17322] [
19150 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2a 20 54 68  H17323].**.** Th
19160 65 20 70 6f 69 6e 74 65 72 20 61 72 67 75 6d 65  e pointer argume
19170 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  nts to [sqlite3_
19180 66 72 65 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c  free()] and [sql
19190 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a  ite3_realloc()].
191a0 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
191b0 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73 65 20 70  r NULL or else p
191c0 6f 69 6e 74 65 72 73 20 6f 62 74 61 69 6e 65 64  ointers obtained
191d0 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a   from a prior.**
191e0 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b   invocation of [
191f0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
19200 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65  ] or [sqlite3_re
19210 61 6c 6c 6f 63 28 29 5d 20 74 68 61 74 20 68 61  alloc()] that ha
19220 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65  ve.** not yet be
19230 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a  en released..**.
19240 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69  ** The applicati
19250 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72 65 61 64  on must not read
19260 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 70 61   or write any pa
19270 72 74 20 6f 66 0a 2a 2a 20 61 20 62 6c 6f 63 6b  rt of.** a block
19280 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66 74 65 72   of memory after
19290 20 69 74 20 68 61 73 20 62 65 65 6e 20 72 65 6c   it has been rel
192a0 65 61 73 65 64 20 75 73 69 6e 67 0a 2a 2a 20 5b  eased using.** [
192b0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20  sqlite3_free()] 
192c0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c  or [sqlite3_real
192d0 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54  loc()]..*/.SQLIT
192e0 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
192f0 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 29 3b  te3_malloc(int);
19300 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
19310 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   *sqlite3_reallo
19320 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53  c(void*, int);.S
19330 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
19340 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64  qlite3_free(void
19350 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
19360 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f  REF: Memory Allo
19370 63 61 74 6f 72 20 53 74 61 74 69 73 74 69 63 73  cator Statistics
19380 20 7b 48 31 37 33 37 30 7d 20 3c 53 33 30 32 31   {H17370} <S3021
19390 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  0>.**.** SQLite 
193a0 70 72 6f 76 69 64 65 73 20 74 68 65 73 65 20 74  provides these t
193b0 77 6f 20 69 6e 74 65 72 66 61 63 65 73 20 66 6f  wo interfaces fo
193c0 72 20 72 65 70 6f 72 74 69 6e 67 20 6f 6e 20 74  r reporting on t
193d0 68 65 20 73 74 61 74 75 73 0a 2a 2a 20 6f 66 20  he status.** of 
193e0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  the [sqlite3_mal
193f0 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  loc()], [sqlite3
19400 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 20 5b 73  _free()], and [s
19410 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
19420 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2c 20 77  ].** routines, w
19430 68 69 63 68 20 66 6f 72 6d 20 74 68 65 20 62 75  hich form the bu
19440 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c  ilt-in memory al
19450 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
19460 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  em..**.** Requir
19470 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33  ements:.** [H173
19480 37 31 5d 20 5b 48 31 37 33 37 33 5d 20 5b 48 31  71] [H17373] [H1
19490 37 33 37 34 5d 20 5b 48 31 37 33 37 35 5d 0a 2a  7374] [H17375].*
194a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
194b0 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74  ite3_int64 sqlit
194c0 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76  e3_memory_used(v
194d0 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  oid);.SQLITE_API
194e0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
194f0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69  qlite3_memory_hi
19500 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65  ghwater(int rese
19510 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  tFlag);../*.** C
19520 41 50 49 33 52 45 46 3a 20 50 73 65 75 64 6f 2d  API3REF: Pseudo-
19530 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72 20 47 65  Random Number Ge
19540 6e 65 72 61 74 6f 72 20 7b 48 31 37 33 39 30 7d  nerator {H17390}
19550 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20000>.**.** 
19560 53 51 4c 69 74 65 20 63 6f 6e 74 61 69 6e 73 20  SQLite contains 
19570 61 20 68 69 67 68 2d 71 75 61 6c 69 74 79 20 70  a high-quality p
19580 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d  seudo-random num
19590 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 28 50  ber generator (P
195a0 52 4e 47 29 20 75 73 65 64 20 74 6f 0a 2a 2a 20  RNG) used to.** 
195b0 73 65 6c 65 63 74 20 72 61 6e 64 6f 6d 20 5b 52  select random [R
195c0 4f 57 49 44 20 7c 20 52 4f 57 49 44 73 5d 20 77  OWID | ROWIDs] w
195d0 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 6e 65  hen inserting ne
195e0 77 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61  w records into a
195f0 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 61   table that.** a
19600 6c 72 65 61 64 79 20 75 73 65 73 20 74 68 65 20  lready uses the 
19610 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65  largest possible
19620 20 5b 52 4f 57 49 44 5d 2e 20 20 54 68 65 20 50   [ROWID].  The P
19630 52 4e 47 20 69 73 20 61 6c 73 6f 20 75 73 65 64  RNG is also used
19640 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75 69 6c   for.** the buil
19650 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20 61 6e  d-in random() an
19660 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 53  d randomblob() S
19670 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54  QL functions.  T
19680 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c  his interface al
19690 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 63 61 74  lows.** applicat
196a0 69 6f 6e 73 20 74 6f 20 61 63 63 65 73 73 20 74  ions to access t
196b0 68 65 20 73 61 6d 65 20 50 52 4e 47 20 66 6f 72  he same PRNG for
196c0 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e   other purposes.
196d0 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f  .**.** A call to
196e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74   this routine st
196f0 6f 72 65 73 20 4e 20 62 79 74 65 73 20 6f 66 20  ores N bytes of 
19700 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20  randomness into 
19710 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20  buffer P..**.** 
19720 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  The first time t
19730 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69  his routine is i
19740 6e 76 6f 6b 65 64 20 28 65 69 74 68 65 72 20 69  nvoked (either i
19750 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0a  nternally or by.
19760 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ** the applicati
19770 6f 6e 29 20 74 68 65 20 50 52 4e 47 20 69 73 20  on) the PRNG is 
19780 73 65 65 64 65 64 20 75 73 69 6e 67 20 72 61 6e  seeded using ran
19790 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64  domness obtained
197a0 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 52 61  .** from the xRa
197b0 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20  ndomness method 
197c0 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b  of the default [
197d0 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a  sqlite3_vfs] obj
197e0 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 73  ect..** On all s
197f0 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61  ubsequent invoca
19800 74 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 75 64  tions, the pseud
19810 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73 20  o-randomness is 
19820 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e 74  generated.** int
19830 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74 68  ernally and with
19840 6f 75 74 20 72 65 63 6f 75 72 73 65 20 74 6f 20  out recourse to 
19850 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  the [sqlite3_vfs
19860 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a 2a  ] xRandomness.**
19870 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52   method..**.** R
19880 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
19890 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a 53 51 4c 49  [H17392].*/.SQLI
198a0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
198b0 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 69  te3_randomness(i
198c0 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50 29 3b 0a  nt N, void *P);.
198d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
198e0 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 41 75   Compile-Time Au
198f0 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43 61 6c 6c  thorization Call
19900 62 61 63 6b 73 20 7b 48 31 32 35 30 30 7d 20 3c  backs {H12500} <
19910 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S70100>.**.** Th
19920 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69 73  is routine regis
19930 74 65 72 73 20 61 20 61 75 74 68 6f 72 69 7a 65  ters a authorize
19940 72 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20  r callback with 
19950 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20  a particular.** 
19960 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
19970 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69 65 64 20  tion], supplied 
19980 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67  in the first arg
19990 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 61 75  ument..** The au
199a0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
199b0 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 20  k is invoked as 
199c0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61  SQL statements a
199d0 72 65 20 62 65 69 6e 67 20 63 6f 6d 70 69 6c 65  re being compile
199e0 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 74 65 33  d.** by [sqlite3
199f0 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 20 69  _prepare()] or i
19a00 74 73 20 76 61 72 69 61 6e 74 73 20 5b 73 71 6c  ts variants [sql
19a10 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
19a20 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  )],.** [sqlite3_
19a30 70 72 65 70 61 72 65 31 36 28 29 5d 20 61 6e 64  prepare16()] and
19a40 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
19a50 65 31 36 5f 76 32 28 29 5d 2e 20 20 41 74 20 76  e16_v2()].  At v
19a60 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69 6e 74 73  arious.** points
19a70 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 70   during the comp
19a80 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65 73 73 2c  ilation process,
19a90 20 61 73 20 6c 6f 67 69 63 20 69 73 20 62 65 69   as logic is bei
19aa0 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 6f  ng created.** to
19ab0 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73   perform various
19ac0 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20 61 75   actions, the au
19ad0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
19ae0 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a  k is invoked to.
19af0 2a 2a 20 73 65 65 20 69 66 20 74 68 6f 73 65 20  ** see if those 
19b00 61 63 74 69 6f 6e 73 20 61 72 65 20 61 6c 6c 6f  actions are allo
19b10 77 65 64 2e 20 20 54 68 65 20 61 75 74 68 6f 72  wed.  The author
19b20 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 73 68  izer callback sh
19b30 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 6e 20 5b  ould.** return [
19b40 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f 20 61 6c  SQLITE_OK] to al
19b50 6c 6f 77 20 74 68 65 20 61 63 74 69 6f 6e 2c 20  low the action, 
19b60 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20  [SQLITE_IGNORE] 
19b70 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 0a  to disallow the.
19b80 2a 2a 20 73 70 65 63 69 66 69 63 20 61 63 74 69  ** specific acti
19b90 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20 74 68 65  on but allow the
19ba0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74   SQL statement t
19bb0 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65  o continue to be
19bc0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c 20 6f 72  .** compiled, or
19bd0 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 20 74   [SQLITE_DENY] t
19be0 6f 20 63 61 75 73 65 20 74 68 65 20 65 6e 74 69  o cause the enti
19bf0 72 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  re SQL statement
19c00 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a 65 63 74   to be.** reject
19c10 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ed with an error
19c20 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 6f 72  .  If the author
19c30 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65  izer callback re
19c40 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 76 61 6c  turns.** any val
19c50 75 65 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53  ue other than [S
19c60 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 5b  QLITE_IGNORE], [
19c70 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f 72 20 5b  SQLITE_OK], or [
19c80 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a 2a 2a 20  SQLITE_DENY].** 
19c90 74 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 74 65  then the [sqlite
19ca0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
19cb0 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63 61  or equivalent ca
19cc0 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65  ll that triggere
19cd0 64 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69  d.** the authori
19ce0 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77 69  zer will fail wi
19cf0 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  th an error mess
19d00 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  age..**.** When 
19d10 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  the callback ret
19d20 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  urns [SQLITE_OK]
19d30 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65  , that means the
19d40 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 65   operation.** re
19d50 71 75 65 73 74 65 64 20 69 73 20 6f 6b 2e 20 20  quested is ok.  
19d60 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63  When the callbac
19d70 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  k returns [SQLIT
19d80 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20  E_DENY], the.** 
19d90 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
19da0 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69 76 61  _v2()] or equiva
19db0 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74 20 74  lent call that t
19dc0 72 69 67 67 65 72 65 64 20 74 68 65 0a 2a 2a 20  riggered the.** 
19dd0 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20  authorizer will 
19de0 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65 72 72  fail with an err
19df0 6f 72 20 6d 65 73 73 61 67 65 20 65 78 70 6c 61  or message expla
19e00 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 61 63  ining that.** ac
19e10 63 65 73 73 20 69 73 20 64 65 6e 69 65 64 2e 20  cess is denied. 
19e20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
19e30 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
19e40 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
19e50 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 79 20  lback is a copy 
19e60 6f 66 20 74 68 65 20 74 68 69 72 64 0a 2a 2a 20  of the third.** 
19e70 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
19e80 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74   sqlite3_set_aut
19e90 68 6f 72 69 7a 65 72 28 29 20 69 6e 74 65 72 66  horizer() interf
19ea0 61 63 65 2e 20 54 68 65 20 73 65 63 6f 6e 64 20  ace. The second 
19eb0 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20  parameter.** to 
19ec0 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  the callback is 
19ed0 61 6e 20 69 6e 74 65 67 65 72 20 5b 53 51 4c 49  an integer [SQLI
19ee0 54 45 5f 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e  TE_COPY | action
19ef0 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 63   code] that spec
19f00 69 66 69 65 73 0a 2a 2a 20 74 68 65 20 70 61 72  ifies.** the par
19f10 74 69 63 75 6c 61 72 20 61 63 74 69 6f 6e 20 74  ticular action t
19f20 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64 2e  o be authorized.
19f30 20 54 68 65 20 74 68 69 72 64 20 74 68 72 6f 75   The third throu
19f40 67 68 20 73 69 78 74 68 20 70 61 72 61 6d 65 74  gh sixth paramet
19f50 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61  ers.** to the ca
19f60 6c 6c 62 61 63 6b 20 61 72 65 20 7a 65 72 6f 2d  llback are zero-
19f70 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
19f80 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20  gs that contain 
19f90 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65  additional.** de
19fa0 74 61 69 6c 73 20 61 62 6f 75 74 20 74 68 65 20  tails about the 
19fb0 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 75 74  action to be aut
19fc0 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  horized..**.** I
19fd0 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 6f 64  f the action cod
19fe0 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 52 45 41  e is [SQLITE_REA
19ff0 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 61  D].** and the ca
1a000 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b  llback returns [
1a010 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74  SQLITE_IGNORE] t
1a020 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70  hen the.** [prep
1a030 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
1a040 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6e  statement is con
1a050 73 74 72 75 63 74 65 64 20 74 6f 20 73 75 62 73  structed to subs
1a060 74 69 74 75 74 65 0a 2a 2a 20 61 20 4e 55 4c 4c  titute.** a NULL
1a070 20 76 61 6c 75 65 20 69 6e 20 70 6c 61 63 65 20   value in place 
1a080 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c  of the table col
1a090 75 6d 6e 20 74 68 61 74 20 77 6f 75 6c 64 20 68  umn that would h
1a0a0 61 76 65 0a 2a 2a 20 62 65 65 6e 20 72 65 61 64  ave.** been read
1a0b0 20 69 66 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20   if [SQLITE_OK] 
1a0c0 68 61 64 20 62 65 65 6e 20 72 65 74 75 72 6e 65  had been returne
1a0d0 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  d.  The [SQLITE_
1a0e0 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 65 74 75 72  IGNORE].** retur
1a0f0 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  n can be used to
1a100 20 64 65 6e 79 20 61 6e 20 75 6e 74 72 75 73 74   deny an untrust
1a110 65 64 20 75 73 65 72 20 61 63 63 65 73 73 20 74  ed user access t
1a120 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20  o individual.** 
1a130 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 20 74 61 62  columns of a tab
1a140 6c 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 61 63  le..** If the ac
1a150 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 5b 53 51  tion code is [SQ
1a160 4c 49 54 45 5f 44 45 4c 45 54 45 5d 20 61 6e 64  LITE_DELETE] and
1a170 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65   the callback re
1a180 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 4c 49 54 45  turns.** [SQLITE
1a190 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68  _IGNORE] then th
1a1a0 65 20 5b 44 45 4c 45 54 45 5d 20 6f 70 65 72 61  e [DELETE] opera
1a1b0 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 20 62 75  tion proceeds bu
1a1c0 74 20 74 68 65 0a 2a 2a 20 5b 74 72 75 6e 63 61  t the.** [trunca
1a1d0 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 5d  te optimization]
1a1e0 20 69 73 20 64 69 73 61 62 6c 65 64 20 61 6e 64   is disabled and
1a1f0 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 20 64 65   all rows are de
1a200 6c 65 74 65 64 20 69 6e 64 69 76 69 64 75 61 6c  leted individual
1a210 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75 74  ly..**.** An aut
1a220 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 64 20  horizer is used 
1a230 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72  when [sqlite3_pr
1a240 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 69 6e  epare | preparin
1a250 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d  g].** SQL statem
1a260 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 6e 74  ents from an unt
1a270 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c 20 74  rusted source, t
1a280 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  o ensure that th
1a290 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  e SQL statements
1a2a0 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 20 74  .** do not try t
1a2b0 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 74 68  o access data th
1a2c0 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77  ey are not allow
1a2d0 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 74 68  ed to see, or th
1a2e0 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 0a 2a  at they do not.*
1a2f0 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 74 65  * try to execute
1a300 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 74 65   malicious state
1a310 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d 61 67  ments that damag
1a320 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  e the database. 
1a330 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c   For.** example,
1a340 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20   an application 
1a350 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 65 72  may allow a user
1a360 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 74 72   to enter arbitr
1a370 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 72 69  ary.** SQL queri
1a380 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 69 6f  es for evaluatio
1a390 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 65 2e  n by a database.
1a3a0 20 20 42 75 74 20 74 68 65 20 61 70 70 6c 69 63    But the applic
1a3b0 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 6e 6f  ation does.** no
1a3c0 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 72 20  t want the user 
1a3d0 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 6d 61  to be able to ma
1a3e0 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 68 61  ke arbitrary cha
1a3f0 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64  nges to the.** d
1a400 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 75 74  atabase.  An aut
1a410 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 74 68  horizer could th
1a420 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 6c 61  en be put in pla
1a430 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a 2a 20  ce while the.** 
1a440 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 51 4c  user-entered SQL
1a450 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c 69 74   is being [sqlit
1a460 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72 65  e3_prepare | pre
1a470 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a 20 64  pared] that.** d
1a480 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 74 68  isallows everyth
1a490 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 4c 45  ing except [SELE
1a4a0 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  CT] statements..
1a4b0 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f  **.** Applicatio
1a4c0 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20  ns that need to 
1a4d0 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 6f 6d  process SQL from
1a4e0 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63   untrusted sourc
1a4f0 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c 73 6f  es.** might also
1a500 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 72 69   consider loweri
1a510 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 6d 69  ng resource limi
1a520 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ts using [sqlite
1a530 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 61 6e  3_limit()].** an
1a540 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 61 62  d limiting datab
1a550 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 20 74  ase size using t
1a560 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75  he [max_page_cou
1a570 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a 2a 20  nt] [PRAGMA].** 
1a580 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20 75  in addition to u
1a590 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69 7a  sing an authoriz
1a5a0 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 61  er..**.** Only a
1a5b0 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 69 7a   single authoriz
1a5c0 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 6c 61  er can be in pla
1a5d0 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  ce on a database
1a5e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61   connection.** a
1a5f0 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 68 20  t a time.  Each 
1a600 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
1a610 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 6f  set_authorizer o
1a620 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a 2a 20  verrides the.** 
1a630 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e 20 20  previous call.  
1a640 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 74 68  Disable the auth
1a650 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 61 6c  orizer by instal
1a660 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 6c 6c  ling a NULL call
1a670 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 75 74  back..** The aut
1a680 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 61 62  horizer is disab
1a690 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 0a  led by default..
1a6a0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1a6b0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 75  izer callback mu
1a6c0 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69  st not do anythi
1a6d0 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64  ng that will mod
1a6e0 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  ify.** the datab
1a6f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
1a700 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20  hat invoked the 
1a710 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
1a720 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ack..** Note tha
1a730 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  t [sqlite3_prepa
1a740 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71  re_v2()] and [sq
1a750 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f  lite3_step()] bo
1a760 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a  th modify their.
1a770 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
1a780 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20  ections for the 
1a790 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69  meaning of "modi
1a7a0 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 61  fy" in this para
1a7b0 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  graph..**.** Whe
1a7c0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  n [sqlite3_prepa
1a7d0 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 65 64  re_v2()] is used
1a7e0 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 73 74   to prepare a st
1a7f0 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a 2a 20  atement, the.** 
1a800 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 74 20  statement might 
1a810 62 65 20 72 65 2d 70 72 65 70 61 72 65 64 20 64  be re-prepared d
1a820 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73  uring [sqlite3_s
1a830 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61 20  tep()] due to a 
1a840 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67  .** schema chang
1a850 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 61  e.  Hence, the a
1a860 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c  pplication shoul
1a870 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  d ensure that th
1a880 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75 74  e.** correct aut
1a890 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1a8a0 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61 63   remains in plac
1a8b0 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73 71  e during the [sq
1a8c0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a  lite3_step()]..*
1a8d0 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
1a8e0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
1a8f0 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65  llback is invoke
1a900 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a 2a  d only during.**
1a910 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
1a920 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69  e()] or its vari
1a930 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a 61  ants.  Authoriza
1a940 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 70  tion is not.** p
1a950 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67 20  erformed during 
1a960 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75 61  statement evalua
1a970 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65 33  tion in [sqlite3
1a980 5f 73 74 65 70 28 29 5d 2c 20 75 6e 6c 65 73 73  _step()], unless
1a990 0a 2a 2a 20 61 73 20 73 74 61 74 65 64 20 69 6e  .** as stated in
1a9a0 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61   the previous pa
1a9b0 72 61 67 72 61 70 68 2c 20 73 71 6c 69 74 65 33  ragraph, sqlite3
1a9c0 5f 73 74 65 70 28 29 20 69 6e 76 6f 6b 65 73 0a  _step() invokes.
1a9d0 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ** sqlite3_prepa
1a9e0 72 65 5f 76 32 28 29 20 74 6f 20 72 65 70 72 65  re_v2() to repre
1a9f0 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 6e 74  pare a statement
1aa00 20 61 66 74 65 72 20 61 20 73 63 68 65 6d 61 20   after a schema 
1aa10 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 52 65  change..**.** Re
1aa20 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
1aa30 48 31 32 35 30 31 5d 20 5b 48 31 32 35 30 32 5d  H12501] [H12502]
1aa40 20 5b 48 31 32 35 30 33 5d 20 5b 48 31 32 35 30   [H12503] [H1250
1aa50 34 5d 20 5b 48 31 32 35 30 35 5d 20 5b 48 31 32  4] [H12505] [H12
1aa60 35 30 36 5d 20 5b 48 31 32 35 30 37 5d 20 5b 48  506] [H12507] [H
1aa70 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 31 32 35 31  12510].** [H1251
1aa80 31 5d 20 5b 48 31 32 35 31 32 5d 20 5b 48 31 32  1] [H12512] [H12
1aa90 35 32 30 5d 20 5b 48 31 32 35 32 31 5d 20 5b 48  520] [H12521] [H
1aaa0 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12522].*/.SQLITE
1aab0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1aac0 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
1aad0 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20 20 69  .  sqlite3*,.  i
1aae0 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64  nt (*xAuth)(void
1aaf0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
1ab00 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
1ab10 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
1ab20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64 20 2a  char*),.  void *
1ab30 70 55 73 65 72 44 61 74 61 0a 29 3b 0a 0a 2f 2a  pUserData.);../*
1ab40 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75  .** CAPI3REF: Au
1ab50 74 68 6f 72 69 7a 65 72 20 52 65 74 75 72 6e 20  thorizer Return 
1ab60 43 6f 64 65 73 20 7b 48 31 32 35 39 30 7d 20 3c  Codes {H12590} <
1ab70 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  H12500>.**.** Th
1ab80 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  e [sqlite3_set_a
1ab90 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 68  uthorizer | auth
1aba0 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
1abb0 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 74 0a 2a  function] must.*
1abc0 2a 20 72 65 74 75 72 6e 20 65 69 74 68 65 72 20  * return either 
1abd0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72 20 6f  [SQLITE_OK] or o
1abe0 6e 65 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20  ne of these two 
1abf0 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 6f 72 64  constants in ord
1ac00 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e 61 6c 20  er.** to signal 
1ac10 53 51 4c 69 74 65 20 77 68 65 74 68 65 72 20 6f  SQLite whether o
1ac20 72 20 6e 6f 74 20 74 68 65 20 61 63 74 69 6f 6e  r not the action
1ac30 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e 20 20   is permitted.  
1ac40 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69  See the.** [sqli
1ac50 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
1ac60 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20  er | authorizer 
1ac70 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 5d 20 66  documentation] f
1ac80 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  or additional.**
1ac90 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
1aca0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1acb0 44 45 4e 59 20 20 20 31 20 20 20 2f 2a 20 41 62  DENY   1   /* Ab
1acc0 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74 61 74  ort the SQL stat
1acd0 65 6d 65 6e 74 20 77 69 74 68 20 61 6e 20 65 72  ement with an er
1ace0 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ror */.#define S
1acf0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32 20 20  QLITE_IGNORE 2  
1ad00 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f 77 20   /* Don't allow 
1ad10 61 63 63 65 73 73 2c 20 62 75 74 20 64 6f 6e 27  access, but don'
1ad20 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72  t generate an er
1ad30 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41  ror */../*.** CA
1ad40 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a  PI3REF: Authoriz
1ad50 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 65 73 20  er Action Codes 
1ad60 7b 48 31 32 35 35 30 7d 20 3c 48 31 32 35 30 30  {H12550} <H12500
1ad70 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  >.**.** The [sql
1ad80 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
1ad90 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63 65  zer()] interface
1ada0 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c   registers a cal
1adb0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a  lback function.*
1adc0 2a 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65  * that is invoke
1add0 64 20 74 6f 20 61 75 74 68 6f 72 69 7a 65 20 63  d to authorize c
1ade0 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61 74 65  ertain SQL state
1adf0 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20 20 54  ment actions.  T
1ae00 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72  he.** second par
1ae10 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 63 61  ameter to the ca
1ae20 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e 74  llback is an int
1ae30 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 20 73  eger code that s
1ae40 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 61 74  pecifies.** what
1ae50 20 61 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67   action is being
1ae60 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 54 68   authorized.  Th
1ae70 65 73 65 20 61 72 65 20 74 68 65 20 69 6e 74 65  ese are the inte
1ae80 67 65 72 20 61 63 74 69 6f 6e 20 63 6f 64 65 73  ger action codes
1ae90 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 61 75 74   that.** the aut
1aea0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1aeb0 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 2e 0a   may be passed..
1aec0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 63 74 69  **.** These acti
1aed0 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 73 20 73  on code values s
1aee0 69 67 6e 69 66 79 20 77 68 61 74 20 6b 69 6e 64  ignify what kind
1aef0 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 69 73   of operation is
1af00 20 74 6f 20 62 65 0a 2a 2a 20 61 75 74 68 6f 72   to be.** author
1af10 69 7a 65 64 2e 20 20 54 68 65 20 33 72 64 20 61  ized.  The 3rd a
1af20 6e 64 20 34 74 68 20 70 61 72 61 6d 65 74 65 72  nd 4th parameter
1af30 73 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69  s to the authori
1af40 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61  zation.** callba
1af50 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c  ck function will
1af60 20 62 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f   be parameters o
1af70 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69 6e 67  r NULL depending
1af80 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 74 68 65   on which of the
1af90 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 73 20 75  se.** codes is u
1afa0 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
1afb0 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68  d parameter.  Th
1afc0 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 5th parameter 
1afd0 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f 72  to the.** author
1afe0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73  izer callback is
1aff0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1b000 20 64 61 74 61 62 61 73 65 20 28 22 6d 61 69 6e   database ("main
1b010 22 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a 20 65 74  ", "temp",.** et
1b020 63 2e 29 20 69 66 20 61 70 70 6c 69 63 61 62 6c  c.) if applicabl
1b030 65 2e 20 20 54 68 65 20 36 74 68 20 70 61 72 61  e.  The 6th para
1b040 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 74  meter to the aut
1b050 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1b060 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d 65 20  .** is the name 
1b070 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73  of the inner-mos
1b080 74 20 74 72 69 67 67 65 72 20 6f 72 20 76 69 65  t trigger or vie
1b090 77 20 74 68 61 74 20 69 73 20 72 65 73 70 6f 6e  w that is respon
1b0a0 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74 68 65  sible for.** the
1b0b0 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74 20   access attempt 
1b0c0 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 20  or NULL if this 
1b0d0 61 63 63 65 73 73 20 61 74 74 65 6d 70 74 20 69  access attempt i
1b0e0 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 0a  s directly from.
1b0f0 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 53 51 4c  ** top-level SQL
1b100 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71   code..**.** Req
1b110 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
1b120 31 32 35 35 31 5d 20 5b 48 31 32 35 35 32 5d 20  12551] [H12552] 
1b130 5b 48 31 32 35 35 33 5d 20 5b 48 31 32 35 35 34  [H12553] [H12554
1b140 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ].*/./**********
1b150 2a 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 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  * 3rd **********
1b180 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ** 4th *********
1b190 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  **/.#define SQLI
1b1a0 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 20  TE_CREATE_INDEX 
1b1b0 20 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20           1   /* 
1b1c0 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20  Index Name      
1b1d0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b1e0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b1f0 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 20 20  E_CREATE_TABLE  
1b200 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a 20 54          2   /* T
1b210 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
1b220 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1b230 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b240 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44  _CREATE_TEMP_IND
1b250 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20 49 6e  EX     3   /* In
1b260 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61  dex Name      Ta
1b270 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
1b280 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b290 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c  CREATE_TEMP_TABL
1b2a0 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54 61 62  E     4   /* Tab
1b2b0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c  le Name      NUL
1b2c0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1b2d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
1b2e0 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47 47  REATE_TEMP_TRIGG
1b2f0 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72 69 67  ER   5   /* Trig
1b300 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
1b310 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23  e Name      */.#
1b320 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
1b330 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 20 20  EATE_TEMP_VIEW  
1b340 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65 77 20      6   /* View 
1b350 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20  Name       NULL 
1b360 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1b370 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45  efine SQLITE_CRE
1b380 41 54 45 5f 54 52 49 47 47 45 52 20 20 20 20 20  ATE_TRIGGER     
1b390 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67 67 65     7   /* Trigge
1b3a0 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20  r Name    Table 
1b3b0 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
1b3c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41  fine SQLITE_CREA
1b3d0 54 45 5f 56 49 45 57 20 20 20 20 20 20 20 20 20  TE_VIEW         
1b3e0 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20 4e 61    8   /* View Na
1b3f0 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  me       NULL   
1b400 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1b410 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c 45 54  ine SQLITE_DELET
1b420 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
1b430 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61   9   /* Table Na
1b440 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1b450 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1b460 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49  ne SQLITE_DROP_I
1b470 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 31  NDEX           1
1b480 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d  0   /* Index Nam
1b490 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  e      Table Nam
1b4a0 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
1b4b0 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41  e SQLITE_DROP_TA
1b4c0 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 31 31  BLE           11
1b4d0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1b4e0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1b4f0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b500 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
1b510 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31 32 20  P_INDEX      12 
1b520 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20    /* Index Name 
1b530 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20       Table Name 
1b540 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b550 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
1b560 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33 20 20  _TABLE      13  
1b570 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20   /* Table Name  
1b580 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
1b590 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b5a0 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
1b5b0 54 52 49 47 47 45 52 20 20 20 20 31 34 20 20 20  TRIGGER    14   
1b5c0 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20  /* Trigger Name 
1b5d0 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
1b5e0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b5f0 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56  LITE_DROP_TEMP_V
1b600 49 45 57 20 20 20 20 20 20 20 31 35 20 20 20 2f  IEW       15   /
1b610 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20  * View Name     
1b620 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1b630 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b640 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52  ITE_DROP_TRIGGER
1b650 20 20 20 20 20 20 20 20 20 31 36 20 20 20 2f 2a           16   /*
1b660 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20   Trigger Name   
1b670 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1b680 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b690 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20 20 20  TE_DROP_VIEW    
1b6a0 20 20 20 20 20 20 20 20 31 37 20 20 20 2f 2a 20          17   /* 
1b6b0 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20  View Name       
1b6c0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
1b6d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b6e0 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20  E_INSERT        
1b6f0 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a 20 54         18   /* T
1b700 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
1b710 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1b720 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b730 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 20 20  _PRAGMA         
1b740 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 50 72        19   /* Pr
1b750 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20 31 73  agma Name     1s
1b760 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20 2a 2f  t arg or NULL */
1b770 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b780 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20  READ            
1b790 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54 61 62       20   /* Tab
1b7a0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c  le Name      Col
1b7b0 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a  umn Name     */.
1b7c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
1b7d0 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 20  ELECT           
1b7e0 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55 4c 4c      21   /* NULL
1b7f0 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c              NULL
1b800 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1b810 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 52  define SQLITE_TR
1b820 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20 20  ANSACTION       
1b830 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 65 72 61     22   /* Opera
1b840 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 4c 4c 20  tion       NULL 
1b850 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1b860 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 50 44  efine SQLITE_UPD
1b870 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ATE             
1b880 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c 65 20    23   /* Table 
1b890 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d 6e  Name      Column
1b8a0 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65   Name     */.#de
1b8b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54 54 41  fine SQLITE_ATTA
1b8c0 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CH              
1b8d0 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d   24   /* Filenam
1b8e0 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  e        NULL   
1b8f0 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1b900 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54 41 43  ine SQLITE_DETAC
1b910 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
1b920 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  25   /* Database
1b930 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20 20 20   Name   NULL    
1b940 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1b950 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f  ne SQLITE_ALTER_
1b960 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 32  TABLE          2
1b970 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20  6   /* Database 
1b980 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e 61 6d  Name   Table Nam
1b990 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
1b9a0 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 45 58  e SQLITE_REINDEX
1b9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 37                27
1b9c0 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65     /* Index Name
1b9d0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1b9e0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b9f0 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a 45 20   SQLITE_ANALYZE 
1ba00 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38 20               28 
1ba10 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
1ba20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
1ba30 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1ba40 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 54  SQLITE_CREATE_VT
1ba50 41 42 4c 45 20 20 20 20 20 20 20 20 32 39 20 20  ABLE        29  
1ba60 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20   /* Table Name  
1ba70 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 20      Module Name 
1ba80 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1ba90 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c  QLITE_DROP_VTABL
1baa0 45 20 20 20 20 20 20 20 20 20 20 33 30 20 20 20  E          30   
1bab0 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20  /* Table Name   
1bac0 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 20 20     Module Name  
1bad0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1bae0 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20 20 20  LITE_FUNCTION   
1baf0 20 20 20 20 20 20 20 20 20 20 33 31 20 20 20 2f            31   /
1bb00 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20  * NULL          
1bb10 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d 65 20    Function Name 
1bb20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1bb30 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 20 20 20  ITE_SAVEPOINT   
1bb40 20 20 20 20 20 20 20 20 20 33 32 20 20 20 2f 2a           32   /*
1bb50 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20 20 20   Operation      
1bb60 20 53 61 76 65 70 6f 69 6e 74 20 4e 61 6d 65 20   Savepoint Name 
1bb70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1bb80 54 45 5f 43 4f 50 59 20 20 20 20 20 20 20 20 20  TE_COPY         
1bb90 20 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a 20           0   /* 
1bba0 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 64 20 2a  No longer used *
1bbb0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  /../*.** CAPI3RE
1bbc0 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 64 20 50  F: Tracing And P
1bbd0 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 74 69 6f  rofiling Functio
1bbe0 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c 53 36 30  ns {H12280} <S60
1bbf0 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  400>.** EXPERIME
1bc00 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  NTAL.**.** These
1bc10 20 72 6f 75 74 69 6e 65 73 20 72 65 67 69 73 74   routines regist
1bc20 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  er callback func
1bc30 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 62  tions that can b
1bc40 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 72  e used for.** tr
1bc50 61 63 69 6e 67 20 61 6e 64 20 70 72 6f 66 69 6c  acing and profil
1bc60 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f  ing the executio
1bc70 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65  n of SQL stateme
1bc80 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  nts..**.** The c
1bc90 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
1bca0 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20 73   registered by s
1bcb0 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 20 69  qlite3_trace() i
1bcc0 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a 2a 2a 20  s invoked at.** 
1bcd0 76 61 72 69 6f 75 73 20 74 69 6d 65 73 20 77 68  various times wh
1bce0 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  en an SQL statem
1bcf0 65 6e 74 20 69 73 20 62 65 69 6e 67 20 72 75 6e  ent is being run
1bd00 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   by [sqlite3_ste
1bd10 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 63 61 6c  p()]..** The cal
1bd20 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 61 20  lback returns a 
1bd30 55 54 46 2d 38 20 72 65 6e 64 65 72 69 6e 67 20  UTF-8 rendering 
1bd40 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  of the SQL state
1bd50 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 61 73 20  ment text.** as 
1bd60 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 69  the statement fi
1bd70 72 73 74 20 62 65 67 69 6e 73 20 65 78 65 63 75  rst begins execu
1bd80 74 69 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e 61  ting.  Additiona
1bd90 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f 63 63 75  l callbacks occu
1bda0 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 74 72 69  r.** as each tri
1bdb0 67 67 65 72 65 64 20 73 75 62 70 72 6f 67 72 61  ggered subprogra
1bdc0 6d 20 69 73 20 65 6e 74 65 72 65 64 2e 20 20 54  m is entered.  T
1bdd0 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 66 6f 72  he callbacks for
1bde0 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 63 6f 6e   triggers.** con
1bdf0 74 61 69 6e 20 61 20 55 54 46 2d 38 20 53 51 4c  tain a UTF-8 SQL
1be00 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 69 64   comment that id
1be10 65 6e 74 69 66 69 65 73 20 74 68 65 20 74 72 69  entifies the tri
1be20 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  gger..**.** The 
1be30 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
1be40 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20  n registered by 
1be50 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28  sqlite3_profile(
1be60 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20  ) is invoked.** 
1be70 61 73 20 65 61 63 68 20 53 51 4c 20 73 74 61 74  as each SQL stat
1be80 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 73 2e 20  ement finishes. 
1be90 20 54 68 65 20 70 72 6f 66 69 6c 65 20 63 61 6c   The profile cal
1bea0 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73 0a 2a  lback contains.*
1beb0 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73  * the original s
1bec0 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20 61 6e  tatement text an
1bed0 64 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66  d an estimate of
1bee0 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69 6d 65   wall-clock time
1bef0 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f 6e 67 20  .** of how long 
1bf00 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 74  that statement t
1bf10 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a 2a 0a 2a  ook to run..**.*
1bf20 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
1bf30 2a 2a 20 5b 48 31 32 32 38 31 5d 20 5b 48 31 32  ** [H12281] [H12
1bf40 32 38 32 5d 20 5b 48 31 32 32 38 33 5d 20 5b 48  282] [H12283] [H
1bf50 31 32 32 38 34 5d 20 5b 48 31 32 32 38 35 5d 20  12284] [H12285] 
1bf60 5b 48 31 32 32 38 37 5d 20 5b 48 31 32 32 38 38  [H12287] [H12288
1bf70 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2a 20 5b 48  ] [H12289].** [H
1bf80 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12290].*/.SQLITE
1bf90 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
1bfa0 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a 73  RIMENTAL void *s
1bfb0 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c  qlite3_trace(sql
1bfc0 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 72  ite3*, void(*xTr
1bfd0 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ace)(void*,const
1bfe0 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 3b   char*), void*);
1bff0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
1c000 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
1c010 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72  void *sqlite3_pr
1c020 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c 0a  ofile(sqlite3*,.
1c030 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 6c     void(*xProfil
1c040 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
1c050 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 6e  har*,sqlite3_uin
1c060 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f  t64), void*);../
1c070 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 51  *.** CAPI3REF: Q
1c080 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 43 61  uery Progress Ca
1c090 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 30 7d  llbacks {H12910}
1c0a0 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S60400>.**.** 
1c0b0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e  This routine con
1c0c0 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c 62 61  figures a callba
1c0d0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 74 68  ck function - th
1c0e0 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 63 61  e.** progress ca
1c0f0 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 69 73  llback - that is
1c100 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f 64 69   invoked periodi
1c110 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c 6f 6e  cally during lon
1c120 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 61 6c  g.** running cal
1c130 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65  ls to [sqlite3_e
1c140 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  xec()], [sqlite3
1c150 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a 2a 20  _step()] and.** 
1c160 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62  [sqlite3_get_tab
1c170 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d 70  le()].  An examp
1c180 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 73 0a  le use for this.
1c190 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  ** interface is 
1c1a0 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75 70  to keep a GUI up
1c1b0 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20 6c  dated during a l
1c1c0 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a  arge query..**.*
1c1d0 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 73  * If the progres
1c1e0 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  s callback retur
1c1f0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  ns non-zero, the
1c200 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a   operation is.**
1c210 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20 54   interrupted.  T
1c220 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e 20  his feature can 
1c230 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  be used to imple
1c240 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63 65  ment a.** "Cance
1c250 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20 47  l" button on a G
1c260 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61 6c  UI progress dial
1c270 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 68  og box..**.** Th
1c280 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c  e progress handl
1c290 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61  er must not do a
1c2a0 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c  nything that wil
1c2b0 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20  l modify.** the 
1c2c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1c2d0 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64  ion that invoked
1c2e0 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 68 61   the progress ha
1c2f0 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 20 74  ndler..** Note t
1c300 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  hat [sqlite3_pre
1c310 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b  pare_v2()] and [
1c320 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
1c330 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69  both modify thei
1c340 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  r.** database co
1c350 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68  nnections for th
1c360 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f  e meaning of "mo
1c370 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61  dify" in this pa
1c380 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52  ragraph..**.** R
1c390 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
1c3a0 5b 48 31 32 39 31 31 5d 20 5b 48 31 32 39 31 32  [H12911] [H12912
1c3b0 5d 20 5b 48 31 32 39 31 33 5d 20 5b 48 31 32 39  ] [H12913] [H129
1c3c0 31 34 5d 20 5b 48 31 32 39 31 35 5d 20 5b 48 31  14] [H12915] [H1
1c3d0 32 39 31 36 5d 20 5b 48 31 32 39 31 37 5d 20 5b  2916] [H12917] [
1c3e0 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 2f 0a 53 51  H12918].**.*/.SQ
1c3f0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1c400 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68  lite3_progress_h
1c410 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 2a 2c  andler(sqlite3*,
1c420 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76 6f 69   int, int(*)(voi
1c430 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a  d*), void*);../*
1c440 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 70  .** CAPI3REF: Op
1c450 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61 74 61  ening A New Data
1c460 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20  base Connection 
1c470 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32 30 30  {H12700} <S40200
1c480 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  >.**.** These ro
1c490 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e 20 53  utines open an S
1c4a0 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 66  QLite database f
1c4b0 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65 20 69  ile whose name i
1c4c0 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 0a 2a  s given by the.*
1c4d0 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d  * filename argum
1c4e0 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e 61 6d  ent. The filenam
1c4f0 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69 6e  e argument is in
1c500 74 65 72 70 72 65 74 65 64 20 61 73 20 55 54 46  terpreted as UTF
1c510 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  -8 for.** sqlite
1c520 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c  3_open() and sql
1c530 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 61  ite3_open_v2() a
1c540 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20  nd as UTF-16 in 
1c550 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 0a  the native byte.
1c560 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73 71 6c  ** order for sql
1c570 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 20 41  ite3_open16(). A
1c580 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
1c590 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 69 73  ction] handle is
1c5a0 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65 74 75   usually.** retu
1c5b0 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c 20 65  rned in *ppDb, e
1c5c0 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ven if an error 
1c5d0 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f 6e 6c  occurs.  The onl
1c5e0 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 74  y exception is t
1c5f0 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69 74 65  hat.** if SQLite
1c600 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c   is unable to al
1c610 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f  locate memory to
1c620 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c 69 74   hold the [sqlit
1c630 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a 20 61  e3] object,.** a
1c640 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20 77 72   NULL will be wr
1c650 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70 44 62  itten into *ppDb
1c660 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 70 6f   instead of a po
1c670 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71  inter to the [sq
1c680 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65 63 74  lite3].** object
1c690 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  . If the databas
1c6a0 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61 6e 64  e is opened (and
1c6b0 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73 75 63  /or created) suc
1c6c0 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65 6e 0a  cessfully, then.
1c6d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69  ** [SQLITE_OK] i
1c6e0 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 74 68  s returned.  Oth
1c6f0 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f 72  erwise an [error
1c700 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
1c710 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73 71 6c  ed.  The.** [sql
1c720 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 6f  ite3_errmsg()] o
1c730 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  r [sqlite3_errms
1c740 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 73 20  g16()] routines 
1c750 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f  can be used to o
1c760 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e 67 6c  btain.** an Engl
1c770 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64 65 73  ish language des
1c780 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20  cription of the 
1c790 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  error..**.** The
1c7a0 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64 69 6e   default encodin
1c7b0 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61  g for the databa
1c7c0 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46 2d 38  se will be UTF-8
1c7d0 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f   if.** sqlite3_o
1c7e0 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33  pen() or sqlite3
1c7f0 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 63 61  _open_v2() is ca
1c800 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54 46 2d  lled and.** UTF-
1c810 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65  16 in the native
1c820 20 62 79 74 65 20 6f 72 64 65 72 20 69 66 20 73   byte order if s
1c830 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 20  qlite3_open16() 
1c840 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 57  is used..**.** W
1c850 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e  hether or not an
1c860 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68   error occurs wh
1c870 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64 2c  en it is opened,
1c880 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20 61 73   resources.** as
1c890 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
1c8a0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
1c8b0 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 73  ection] handle s
1c8c0 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61 73 65  hould be release
1c8d0 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e 67 20  d by.** passing 
1c8e0 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  it to [sqlite3_c
1c8f0 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69 74 20  lose()] when it 
1c900 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71  is no longer req
1c910 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  uired..**.** The
1c920 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
1c930 28 29 20 69 6e 74 65 72 66 61 63 65 20 77 6f 72  () interface wor
1c940 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f  ks like sqlite3_
1c950 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65 70 74  open().** except
1c960 20 74 68 61 74 20 69 74 20 61 63 63 65 70 74 73   that it accepts
1c970 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20   two additional 
1c980 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 61  parameters for a
1c990 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74 72 6f  dditional contro
1c9a0 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20 6e 65  l.** over the ne
1c9b0 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  w database conne
1c9c0 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c 61 67  ction.  The flag
1c9d0 73 20 70 61 72 61 6d 65 74 65 72 20 63 61 6e 20  s parameter can 
1c9e0 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74  take one of.** t
1c9f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72  he following thr
1ca00 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74 69 6f  ee values, optio
1ca10 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64 20 77  nally combined w
1ca20 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53 51 4c  ith the .** [SQL
1ca30 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58  ITE_OPEN_NOMUTEX
1ca40 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ], [SQLITE_OPEN_
1ca50 46 55 4c 4c 4d 55 54 45 58 5d 2c 20 5b 53 51 4c  FULLMUTEX], [SQL
1ca60 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43  ITE_OPEN_SHAREDC
1ca70 41 43 48 45 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72  ACHE],.** and/or
1ca80 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52   [SQLITE_OPEN_PR
1ca90 49 56 41 54 45 43 41 43 48 45 5d 20 66 6c 61 67  IVATECACHE] flag
1caa0 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a  s:.**.** <dl>.**
1cab0 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45   <dt>[SQLITE_OPE
1cac0 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f 64 74 3e  N_READONLY]</dt>
1cad0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61  .** <dd>The data
1cae0 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 69  base is opened i
1caf0 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65  n read-only mode
1cb00 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61  .  If the databa
1cb10 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61  se does not.** a
1cb20 6c 72 65 61 64 79 20 65 78 69 73 74 2c 20 61 6e  lready exist, an
1cb30 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
1cb40 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ed.</dd>.**.** <
1cb50 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  dt>[SQLITE_OPEN_
1cb60 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 74 3e 0a  READWRITE]</dt>.
1cb70 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62  ** <dd>The datab
1cb80 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f  ase is opened fo
1cb90 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72  r reading and wr
1cba0 69 74 69 6e 67 20 69 66 20 70 6f 73 73 69 62 6c  iting if possibl
1cbb0 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 0a 2a 2a  e, or reading.**
1cbc0 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 66 69 6c   only if the fil
1cbd0 65 20 69 73 20 77 72 69 74 65 20 70 72 6f 74 65  e is write prote
1cbe0 63 74 65 64 20 62 79 20 74 68 65 20 6f 70 65 72  cted by the oper
1cbf0 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 49  ating system.  I
1cc00 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 61 73 65  n either.** case
1cc10 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75   the database mu
1cc20 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  st already exist
1cc30 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65  , otherwise an e
1cc40 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
1cc50 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
1cc60 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  >[SQLITE_OPEN_RE
1cc70 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49  ADWRITE] | [SQLI
1cc80 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 3c  TE_OPEN_CREATE]<
1cc90 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20  /dt>.** <dd>The 
1cca0 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  database is open
1ccb0 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61  ed for reading a
1ccc0 6e 64 20 77 72 69 74 69 6e 67 2c 20 61 6e 64 20  nd writing, and 
1ccd0 69 73 20 63 72 65 61 74 65 73 20 69 74 20 69 66  is creates it if
1cce0 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e 6f 74 20  .** it does not 
1ccf0 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 20 54  already exist. T
1cd00 68 69 73 20 69 73 20 74 68 65 20 62 65 68 61 76  his is the behav
1cd10 69 6f 72 20 74 68 61 74 20 69 73 20 61 6c 77 61  ior that is alwa
1cd20 79 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 73  ys used for.** s
1cd30 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e  qlite3_open() an
1cd40 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  d sqlite3_open16
1cd50 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c  ().</dd>.** </dl
1cd60 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 33  >.**.** If the 3
1cd70 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  rd parameter to 
1cd80 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
1cd90 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 6f 66 20  ) is not one of 
1cda0 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e 61 74 69  the.** combinati
1cdb0 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76 65 20  ons shown above 
1cdc0 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f  or one of the co
1cdd0 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f 77 6e  mbinations shown
1cde0 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e 65 64 0a   above combined.
1cdf0 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c  ** with the [SQL
1ce00 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58  ITE_OPEN_NOMUTEX
1ce10 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ], [SQLITE_OPEN_
1ce20 46 55 4c 4c 4d 55 54 45 58 5d 2c 0a 2a 2a 20 5b  FULLMUTEX],.** [
1ce30 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52  SQLITE_OPEN_SHAR
1ce40 45 44 43 41 43 48 45 5d 20 61 6e 64 2f 6f 72 20  EDCACHE] and/or 
1ce50 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41  [SQLITE_OPEN_SHA
1ce60 52 45 44 43 41 43 48 45 5d 20 66 6c 61 67 73 2c  REDCACHE] flags,
1ce70 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68  .** then the beh
1ce80 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
1ce90 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
1cea0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f   [SQLITE_OPEN_NO
1ceb0 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 73  MUTEX] flag is s
1cec0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74  et, then the dat
1ced0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1cee0 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 74 68 65  .** opens in the
1cef0 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 20 5b 74   multi-thread [t
1cf00 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 61  hreading mode] a
1cf10 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 73 69  s long as the si
1cf20 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a 2a 20 6d  ngle-thread.** m
1cf30 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ode has not been
1cf40 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 2d   set at compile-
1cf50 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d 74 69  time or start-ti
1cf60 6d 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 5b  me.  If the.** [
1cf70 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c  SQLITE_OPEN_FULL
1cf80 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 73  MUTEX] flag is s
1cf90 65 74 20 74 68 65 6e 20 74 68 65 20 64 61 74 61  et then the data
1cfa0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1cfb0 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20  opens.** in the 
1cfc0 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65  serialized [thre
1cfd0 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e 6c 65  ading mode] unle
1cfe0 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64  ss single-thread
1cff0 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73   was.** previous
1d000 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74 20 63  ly selected at c
1d010 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73  ompile-time or s
1d020 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 20 54 68  tart-time..** Th
1d030 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  e [SQLITE_OPEN_S
1d040 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c 61 67  HAREDCACHE] flag
1d050 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61   causes the data
1d060 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1d070 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 67 69 62 6c  to be.** eligibl
1d080 65 20 74 6f 20 75 73 65 20 5b 73 68 61 72 65 64  e to use [shared
1d090 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 72 65   cache mode], re
1d0a0 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
1d0b0 68 65 72 20 6f 72 20 6e 6f 74 20 73 68 61 72 65  her or not share
1d0c0 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 65 6e  d.** cache is en
1d0d0 61 62 6c 65 64 20 75 73 69 6e 67 20 5b 73 71 6c  abled using [sql
1d0e0 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
1d0f0 65 64 5f 63 61 63 68 65 28 29 5d 2e 20 20 54 68  ed_cache()].  Th
1d100 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45  e.** [SQLITE_OPE
1d110 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 5d 20  N_PRIVATECACHE] 
1d120 66 6c 61 67 20 63 61 75 73 65 73 20 74 68 65 20  flag causes the 
1d130 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1d140 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a 2a 20 70 61  ion to not.** pa
1d150 72 74 69 63 69 70 61 74 65 20 69 6e 20 5b 73 68  rticipate in [sh
1d160 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d  ared cache mode]
1d170 20 65 76 65 6e 20 69 66 20 69 74 20 69 73 20 65   even if it is e
1d180 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  nabled..**.** If
1d190 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73   the filename is
1d1a0 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65   ":memory:", the
1d1b0 6e 20 61 20 70 72 69 76 61 74 65 2c 20 74 65 6d  n a private, tem
1d1c0 70 6f 72 61 72 79 20 69 6e 2d 6d 65 6d 6f 72 79  porary in-memory
1d1d0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20   database.** is 
1d1e0 63 72 65 61 74 65 64 20 66 6f 72 20 74 68 65 20  created for the 
1d1f0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69  connection.  Thi
1d200 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  s in-memory data
1d210 62 61 73 65 20 77 69 6c 6c 20 76 61 6e 69 73 68  base will vanish
1d220 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 74   when.** the dat
1d230 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1d240 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 46 75 74   is closed.  Fut
1d250 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ure versions of 
1d260 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20  SQLite might.** 
1d270 6d 61 6b 65 20 75 73 65 20 6f 66 20 61 64 64 69  make use of addi
1d280 74 69 6f 6e 61 6c 20 73 70 65 63 69 61 6c 20 66  tional special f
1d290 69 6c 65 6e 61 6d 65 73 20 74 68 61 74 20 62 65  ilenames that be
1d2a0 67 69 6e 20 77 69 74 68 20 74 68 65 20 22 3a 22  gin with the ":"
1d2b0 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49   character..** I
1d2c0 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  t is recommended
1d2d0 20 74 68 61 74 20 77 68 65 6e 20 61 20 64 61 74   that when a dat
1d2e0 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 61  abase filename a
1d2f0 63 74 75 61 6c 6c 79 20 64 6f 65 73 20 62 65 67  ctually does beg
1d300 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 22 3a 22  in with.** a ":"
1d310 20 63 68 61 72 61 63 74 65 72 20 79 6f 75 20 73   character you s
1d320 68 6f 75 6c 64 20 70 72 65 66 69 78 20 74 68 65  hould prefix the
1d330 20 66 69 6c 65 6e 61 6d 65 20 77 69 74 68 20 61   filename with a
1d340 20 70 61 74 68 6e 61 6d 65 20 73 75 63 68 20 61   pathname such a
1d350 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20 61 76 6f  s.** "./" to avo
1d360 69 64 20 61 6d 62 69 67 75 69 74 79 2e 0a 2a 2a  id ambiguity..**
1d370 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 6e  .** If the filen
1d380 61 6d 65 20 69 73 20 61 6e 20 65 6d 70 74 79 20  ame is an empty 
1d390 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 20 70  string, then a p
1d3a0 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72  rivate, temporar
1d3b0 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 64 61 74  y.** on-disk dat
1d3c0 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 72  abase will be cr
1d3d0 65 61 74 65 64 2e 20 20 54 68 69 73 20 70 72 69  eated.  This pri
1d3e0 76 61 74 65 20 64 61 74 61 62 61 73 65 20 77 69  vate database wi
1d3f0 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f 6d 61 74  ll be.** automat
1d400 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 61  ically deleted a
1d410 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 64 61  s soon as the da
1d420 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1d430 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a  n is closed..**.
1d440 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61  ** The fourth pa
1d450 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
1d460 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20  e3_open_v2() is 
1d470 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a  the name of the.
1d480 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  ** [sqlite3_vfs]
1d490 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 66   object that def
1d4a0 69 6e 65 73 20 74 68 65 20 6f 70 65 72 61 74 69  ines the operati
1d4b0 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
1d4c0 61 63 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ace that.** the 
1d4d0 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e  new database con
1d4e0 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75  nection should u
1d4f0 73 65 2e 20 20 49 66 20 74 68 65 20 66 6f 75 72  se.  If the four
1d500 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a  th parameter is.
1d510 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ** a NULL pointe
1d520 72 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75  r then the defau
1d530 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  lt [sqlite3_vfs]
1d540 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 2e   object is used.
1d550 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 20 74  .**.** <b>Note t
1d560 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 72 73 3a  o Windows users:
1d570 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f 64 69  </b>  The encodi
1d580 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  ng used for the 
1d590 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e  filename argumen
1d5a0 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 5f  t.** of sqlite3_
1d5b0 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74  open() and sqlit
1d5c0 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d 75 73  e3_open_v2() mus
1d5d0 74 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f 74 20  t be UTF-8, not 
1d5e0 77 68 61 74 65 76 65 72 0a 2a 2a 20 63 6f 64 65  whatever.** code
1d5f0 70 61 67 65 20 69 73 20 63 75 72 72 65 6e 74 6c  page is currentl
1d600 79 20 64 65 66 69 6e 65 64 2e 20 20 46 69 6c 65  y defined.  File
1d610 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69 6e 67  names containing
1d620 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61 6c 0a 2a   international.*
1d630 2a 20 63 68 61 72 61 63 74 65 72 73 20 6d 75 73  * characters mus
1d640 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74  t be converted t
1d650 6f 20 55 54 46 2d 38 20 70 72 69 6f 72 20 74 6f  o UTF-8 prior to
1d660 20 70 61 73 73 69 6e 67 20 74 68 65 6d 20 69 6e   passing them in
1d670 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70  to.** sqlite3_op
1d680 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  en() or sqlite3_
1d690 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 2a 2a  open_v2()..**.**
1d6a0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
1d6b0 2a 20 5b 48 31 32 37 30 31 5d 20 5b 48 31 32 37  * [H12701] [H127
1d6c0 30 32 5d 20 5b 48 31 32 37 30 33 5d 20 5b 48 31  02] [H12703] [H1
1d6d0 32 37 30 34 5d 20 5b 48 31 32 37 30 36 5d 20 5b  2704] [H12706] [
1d6e0 48 31 32 37 30 37 5d 20 5b 48 31 32 37 30 39 5d  H12707] [H12709]
1d6f0 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 20 5b 48 31   [H12711].** [H1
1d700 32 37 31 32 5d 20 5b 48 31 32 37 31 33 5d 20 5b  2712] [H12713] [
1d710 48 31 32 37 31 34 5d 20 5b 48 31 32 37 31 37 5d  H12714] [H12717]
1d720 20 5b 48 31 32 37 31 39 5d 20 5b 48 31 32 37 32   [H12719] [H1272
1d730 31 5d 20 5b 48 31 32 37 32 33 5d 0a 2a 2f 0a 53  1] [H12723].*/.S
1d740 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1d750 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63 6f  lite3_open(.  co
1d760 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61  nst char *filena
1d770 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73  me,   /* Databas
1d780 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d  e filename (UTF-
1d790 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  8) */.  sqlite3 
1d7a0 2a 2a 70 70 44 62 20 20 20 20 20 20 20 20 20 20  **ppDb          
1d7b0 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64  /* OUT: SQLite d
1d7c0 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53  b handle */.);.S
1d7d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1d7e0 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a 20 20  lite3_open16(.  
1d7f0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 66 69 6c 65  const void *file
1d800 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62  name,   /* Datab
1d810 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54  ase filename (UT
1d820 46 2d 31 36 29 20 2a 2f 0a 20 20 73 71 6c 69 74  F-16) */.  sqlit
1d830 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20  e3 **ppDb       
1d840 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74     /* OUT: SQLit
1d850 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29  e db handle */.)
1d860 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
1d870 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
1d880 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
1d890 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44  filename,   /* D
1d8a0 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
1d8b0 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71   (UTF-8) */.  sq
1d8c0 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20  lite3 **ppDb,   
1d8d0 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51        /* OUT: SQ
1d8e0 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a  Lite db handle *
1d8f0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
1d900 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
1d910 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  lags */.  const 
1d920 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20  char *zVfs      
1d930 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46 53    /* Name of VFS
1d940 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 2a   module to use *
1d950 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.);../*.** CAPI
1d960 33 52 45 46 3a 20 45 72 72 6f 72 20 43 6f 64 65  3REF: Error Code
1d970 73 20 41 6e 64 20 4d 65 73 73 61 67 65 73 20 7b  s And Messages {
1d980 48 31 32 38 30 30 7d 20 3c 53 36 30 32 30 30 3e  H12800} <S60200>
1d990 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
1d9a0 65 33 5f 65 72 72 63 6f 64 65 28 29 20 69 6e 74  e3_errcode() int
1d9b0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74  erface returns t
1d9c0 68 65 20 6e 75 6d 65 72 69 63 20 5b 72 65 73 75  he numeric [resu
1d9d0 6c 74 20 63 6f 64 65 5d 20 6f 72 0a 2a 2a 20 5b  lt code] or.** [
1d9e0 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
1d9f0 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f  code] for the mo
1da00 73 74 20 72 65 63 65 6e 74 20 66 61 69 6c 65 64  st recent failed
1da10 20 73 71 6c 69 74 65 33 5f 2a 20 41 50 49 20 63   sqlite3_* API c
1da20 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  all.** associate
1da30 64 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61  d with a [databa
1da40 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20  se connection]. 
1da50 49 66 20 61 20 70 72 69 6f 72 20 41 50 49 20 63  If a prior API c
1da60 61 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a 20 62 75  all failed.** bu
1da70 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  t the most recen
1da80 74 20 41 50 49 20 63 61 6c 6c 20 73 75 63 63 65  t API call succe
1da90 65 64 65 64 2c 20 74 68 65 20 72 65 74 75 72 6e  eded, the return
1daa0 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 73   value from.** s
1dab0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
1dac0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20   is undefined.  
1dad0 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65  The sqlite3_exte
1dae0 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 0a 2a  nded_errcode().*
1daf0 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74  * interface is t
1db00 68 65 20 73 61 6d 65 20 65 78 63 65 70 74 20 74  he same except t
1db10 68 61 74 20 69 74 20 61 6c 77 61 79 73 20 72 65  hat it always re
1db20 74 75 72 6e 73 20 74 68 65 20 0a 2a 2a 20 5b 65  turns the .** [e
1db30 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
1db40 6f 64 65 5d 20 65 76 65 6e 20 77 68 65 6e 20 65  ode] even when e
1db50 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
1db60 6f 64 65 73 20 61 72 65 0a 2a 2a 20 64 69 73 61  odes are.** disa
1db70 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  bled..**.** The 
1db80 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
1db90 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72   and sqlite3_err
1dba0 6d 73 67 31 36 28 29 20 72 65 74 75 72 6e 20 45  msg16() return E
1dbb0 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 61 67 65 0a  nglish-language.
1dbc0 2a 2a 20 74 65 78 74 20 74 68 61 74 20 64 65 73  ** text that des
1dbd0 63 72 69 62 65 73 20 74 68 65 20 65 72 72 6f 72  cribes the error
1dbe0 2c 20 61 73 20 65 69 74 68 65 72 20 55 54 46 2d  , as either UTF-
1dbf0 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65 73 70  8 or UTF-16 resp
1dc00 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 4d 65 6d  ectively..** Mem
1dc10 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
1dc20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74  error message st
1dc30 72 69 6e 67 20 69 73 20 6d 61 6e 61 67 65 64 20  ring is managed 
1dc40 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2a 20 54  internally..** T
1dc50 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64  he application d
1dc60 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  oes not need to 
1dc70 77 6f 72 72 79 20 61 62 6f 75 74 20 66 72 65 65  worry about free
1dc80 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2e 0a  ing the result..
1dc90 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20  ** However, the 
1dca0 65 72 72 6f 72 20 73 74 72 69 6e 67 20 6d 69 67  error string mig
1dcb0 68 74 20 62 65 20 6f 76 65 72 77 72 69 74 74 65  ht be overwritte
1dcc0 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64  n or deallocated
1dcd0 20 62 79 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e   by.** subsequen
1dce0 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74 68 65 72  t calls to other
1dcf0 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
1dd00 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a  e functions..**.
1dd10 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 65 72 69  ** When the seri
1dd20 61 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69 6e  alized [threadin
1dd30 67 20 6d 6f 64 65 5d 20 69 73 20 69 6e 20 75 73  g mode] is in us
1dd40 65 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74  e, it might be t
1dd50 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20  he.** case that 
1dd60 61 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 6f  a second error o
1dd70 63 63 75 72 73 20 6f 6e 20 61 20 73 65 70 61 72  ccurs on a separ
1dd80 61 74 65 20 74 68 72 65 61 64 20 69 6e 20 62 65  ate thread in be
1dd90 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 74 69 6d  tween.** the tim
1dda0 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 65  e of the first e
1ddb0 72 72 6f 72 20 61 6e 64 20 74 68 65 20 63 61 6c  rror and the cal
1ddc0 6c 20 74 6f 20 74 68 65 73 65 20 69 6e 74 65 72  l to these inter
1ddd0 66 61 63 65 73 2e 0a 2a 2a 20 57 68 65 6e 20 74  faces..** When t
1dde0 68 61 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65  hat happens, the
1ddf0 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 77 69   second error wi
1de00 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20 73  ll be reported s
1de10 69 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 69 6e  ince these.** in
1de20 74 65 72 66 61 63 65 73 20 61 6c 77 61 79 73 20  terfaces always 
1de30 72 65 70 6f 72 74 20 74 68 65 20 6d 6f 73 74 20  report the most 
1de40 72 65 63 65 6e 74 20 72 65 73 75 6c 74 2e 20 20  recent result.  
1de50 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 74 68 69 73  To avoid.** this
1de60 2c 20 65 61 63 68 20 74 68 72 65 61 64 20 63 61  , each thread ca
1de70 6e 20 6f 62 74 61 69 6e 20 65 78 63 6c 75 73 69  n obtain exclusi
1de80 76 65 20 75 73 65 20 6f 66 20 74 68 65 20 5b 64  ve use of the [d
1de90 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1dea0 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 69 6e 76 6f  on] D.** by invo
1deb0 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75  king [sqlite3_mu
1dec0 74 65 78 5f 65 6e 74 65 72 5d 28 5b 73 71 6c 69  tex_enter]([sqli
1ded0 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 29  te3_db_mutex](D)
1dee0 29 20 62 65 66 6f 72 65 20 62 65 67 69 6e 6e 69  ) before beginni
1def0 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 20 44 20 61  ng.** to use D a
1df00 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c  nd invoking [sql
1df10 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1df20 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75  ]([sqlite3_db_mu
1df30 74 65 78 5d 28 44 29 29 20 61 66 74 65 72 0a 2a  tex](D)) after.*
1df40 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 74 6f 20 74  * all calls to t
1df50 68 65 20 69 6e 74 65 72 66 61 63 65 73 20 6c 69  he interfaces li
1df60 73 74 65 64 20 68 65 72 65 20 61 72 65 20 63 6f  sted here are co
1df70 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  mpleted..**.** I
1df80 66 20 61 6e 20 69 6e 74 65 72 66 61 63 65 20 66  f an interface f
1df90 61 69 6c 73 20 77 69 74 68 20 53 51 4c 49 54 45  ails with SQLITE
1dfa0 5f 4d 49 53 55 53 45 2c 20 74 68 61 74 20 6d 65  _MISUSE, that me
1dfb0 61 6e 73 20 74 68 65 20 69 6e 74 65 72 66 61 63  ans the interfac
1dfc0 65 0a 2a 2a 20 77 61 73 20 69 6e 76 6f 6b 65 64  e.** was invoked
1dfd0 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 62 79 20   incorrectly by 
1dfe0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e  the application.
1dff0 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20    In that case, 
1e000 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64  the.** error cod
1e010 65 20 61 6e 64 20 6d 65 73 73 61 67 65 20 6d 61  e and message ma
1e020 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20  y or may not be 
1e030 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  set..**.** Requi
1e040 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
1e050 38 30 31 5d 20 5b 48 31 32 38 30 32 5d 20 5b 48  801] [H12802] [H
1e060 31 32 38 30 33 5d 20 5b 48 31 32 38 30 37 5d 20  12803] [H12807] 
1e070 5b 48 31 32 38 30 38 5d 20 5b 48 31 32 38 30 39  [H12808] [H12809
1e080 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
1e090 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63  int sqlite3_errc
1e0a0 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ode(sqlite3 *db)
1e0b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
1e0c0 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
1e0d0 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65  d_errcode(sqlite
1e0e0 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41  3 *db);.SQLITE_A
1e0f0 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
1e100 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71  qlite3_errmsg(sq
1e110 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
1e120 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1e130 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
1e140 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
1e150 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c  ** CAPI3REF: SQL
1e160 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63   Statement Objec
1e170 74 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 30  t {H13000} <H130
1e180 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  10>.** KEYWORDS:
1e190 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   {prepared state
1e1a0 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 20  ment} {prepared 
1e1b0 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a  statements}.**.*
1e1c0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
1e1d0 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70   this object rep
1e1e0 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65  resents a single
1e1f0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a   SQL statement..
1e200 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69  ** This object i
1e210 73 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77  s variously know
1e220 6e 20 61 73 20 61 20 22 70 72 65 70 61 72 65 64  n as a "prepared
1e230 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61   statement" or a
1e240 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51  .** "compiled SQ
1e250 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20  L statement" or 
1e260 73 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61  simply as a "sta
1e270 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 54  tement"..**.** T
1e280 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 61  he life of a sta
1e290 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 6f  tement object go
1e2a0 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b  es something lik
1e2b0 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f  e this:.**.** <o
1e2c0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 74  l>.** <li> Creat
1e2d0 65 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69  e the object usi
1e2e0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ng [sqlite3_prep
1e2f0 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 72  are_v2()] or a r
1e300 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 66  elated.**      f
1e310 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e  unction..** <li>
1e320 20 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20   Bind values to 
1e330 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73  [host parameters
1e340 5d 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69  ] using the sqli
1e350 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20  te3_bind_*().** 
1e360 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 2e       interfaces.
1e370 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 65  .** <li> Run the
1e380 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 20   SQL by calling 
1e390 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1e3a0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d   one or more tim
1e3b0 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 65  es..** <li> Rese
1e3c0 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  t the statement 
1e3d0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72  using [sqlite3_r
1e3e0 65 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f 20  eset()] then go 
1e3f0 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f 20  back.**      to 
1e400 73 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 73  step 2.  Do this
1e410 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 69   zero or more ti
1e420 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 73  mes..** <li> Des
1e430 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 20  troy the object 
1e440 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66  using [sqlite3_f
1e450 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 3c  inalize()]..** <
1e460 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72  /ol>.**.** Refer
1e470 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f   to documentatio
1e480 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20  n on individual 
1e490 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f  methods above fo
1e4a0 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  r additional.** 
1e4b0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
1e4c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
1e4d0 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c 69  qlite3_stmt sqli
1e4e0 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a  te3_stmt;../*.**
1e4f0 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 74   CAPI3REF: Run-t
1e500 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 37  ime Limits {H127
1e510 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 0a  60} <S20600>.**.
1e520 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63  ** This interfac
1e530 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a  e allows the siz
1e540 65 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f 6e  e of various con
1e550 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c 69  structs to be li
1e560 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f  mited.** on a co
1e570 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e  nnection by conn
1e580 65 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 54  ection basis.  T
1e590 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
1e5a0 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61  er is the.** [da
1e5b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1e5c0 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69  n] whose limit i
1e5d0 73 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 71  s to be set or q
1e5e0 75 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20  ueried.  The.** 
1e5f0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
1e600 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b   is one of the [
1e610 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73  limit categories
1e620 5d 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 0a  ] that define a.
1e630 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73  ** class of cons
1e640 74 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 7a  tructs to be siz
1e650 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 20  e limited.  The 
1e660 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
1e670 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69  is the.** new li
1e680 6d 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f 6e  mit for that con
1e690 73 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 6e  struct.  The fun
1e6a0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
1e6b0 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a  e old limit..**.
1e6c0 2a 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c 69  ** If the new li
1e6d0 6d 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76  mit is a negativ
1e6e0 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69  e number, the li
1e6f0 6d 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64  mit is unchanged
1e700 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d  ..** For the lim
1e710 69 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 53  it category of S
1e720 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20  QLITE_LIMIT_XYZ 
1e730 74 68 65 72 65 20 69 73 20 61 20 0a 2a 2a 20 5b  there is a .** [
1e740 6c 69 6d 69 74 73 20 7c 20 68 61 72 64 20 75 70  limits | hard up
1e750 70 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a 20 73 65  per bound].** se
1e760 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d 74  t by a compile-t
1e770 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73 73  ime C preprocess
1e780 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20 0a  or macro named .
1e790 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 53 51 4c  ** [limits | SQL
1e7a0 49 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e 0a 2a 2a  ITE_MAX_XYZ]..**
1e7b0 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22 20   (The "_LIMIT_" 
1e7c0 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20 63  in the name is c
1e7d0 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 58 5f  hanged to "_MAX_
1e7e0 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 73 20  ".).** Attempts 
1e7f0 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20 6c 69  to increase a li
1e800 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20 68 61  mit above its ha
1e810 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20 61  rd upper bound a
1e820 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20 74  re.** silently t
1e830 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65 20  runcated to the 
1e840 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74  hard upper limit
1e850 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d 65  ..**.** Run time
1e860 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e 74 65   limits are inte
1e870 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 20  nded for use in 
1e880 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  applications tha
1e890 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f 74 68  t manage.** both
1e8a0 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 65 72   their own inter
1e8b0 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61 6e 64  nal database and
1e8c0 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65 73 20   also databases 
1e8d0 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72 6f 6c  that are control
1e8e0 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 75 73  led.** by untrus
1e8f0 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f 75  ted external sou
1e900 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70 6c  rces.  An exampl
1e910 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 69  e application mi
1e920 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 62 20  ght be a.** web 
1e930 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 73  browser that has
1e940 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 73   its own databas
1e950 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 68  es for storing h
1e960 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73 65  istory and.** se
1e970 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65 73  parate databases
1e980 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 4a   controlled by J
1e990 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69 63  avaScript applic
1e9a0 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64 65  ations downloade
1e9b0 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e 74  d.** off the Int
1e9c0 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74 65  ernet.  The inte
1e9d0 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20 63  rnal databases c
1e9e0 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65 0a  an be given the.
1e9f0 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75 6c  ** large, defaul
1ea00 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61 62  t limits.  Datab
1ea10 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79 20  ases managed by 
1ea20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73  external sources
1ea30 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65 6e   can.** be given
1ea40 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c 69   much smaller li
1ea50 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 74 6f  mits designed to
1ea60 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69 61   prevent a denia
1ea70 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a 20  l of service.** 
1ea80 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f 70  attack.  Develop
1ea90 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 77  ers might also w
1eaa0 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 5b  ant to use the [
1eab0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
1eac0 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e 74  orizer()].** int
1ead0 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 68 65  erface to furthe
1eae0 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75 73  r control untrus
1eaf0 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73 69  ted SQL.  The si
1eb00 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
1eb10 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79  se.** created by
1eb20 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73 63   an untrusted sc
1eb30 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e 74  ript can be cont
1eb40 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 0a  ained using the.
1eb50 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75  ** [max_page_cou
1eb60 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a 2a  nt] [PRAGMA]..**
1eb70 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d 65  .** New run-time
1eb80 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65   limit categorie
1eb90 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69  s may be added i
1eba0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
1ebb0 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  s..**.** Require
1ebc0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 37 36  ments:.** [H1276
1ebd0 32 5d 20 5b 48 31 32 37 36 36 5d 20 5b 48 31 32  2] [H12766] [H12
1ebe0 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  769].*/.SQLITE_A
1ebf0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c  PI int sqlite3_l
1ec00 69 6d 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 69  imit(sqlite3*, i
1ec10 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65 77 56 61  nt id, int newVa
1ec20 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  l);../*.** CAPI3
1ec30 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69  REF: Run-Time Li
1ec40 6d 69 74 20 43 61 74 65 67 6f 72 69 65 73 20 7b  mit Categories {
1ec50 48 31 32 37 39 30 7d 20 3c 48 31 32 37 36 30 3e  H12790} <H12760>
1ec60 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 6c  .** KEYWORDS: {l
1ec70 69 6d 69 74 20 63 61 74 65 67 6f 72 79 7d 20 7b  imit category} {
1ec80 2a 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65  *limit categorie
1ec90 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  s}.**.** These c
1eca0 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e 65 20  onstants define 
1ecb0 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72 6d 61  various performa
1ecc0 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 74 68  nce limits.** th
1ecd0 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 72 65  at can be lowere
1ece0 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73  d at run-time us
1ecf0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d  ing [sqlite3_lim
1ed00 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 79  it()]..** The sy
1ed10 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 6d 65  nopsis of the me
1ed20 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61  anings of the va
1ed30 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 73 20  rious limits is 
1ed40 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a 2a 20  shown below..** 
1ed50 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  Additional infor
1ed60 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61  mation is availa
1ed70 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 20 7c  ble at [limits |
1ed80 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c 69 74   Limits in SQLit
1ed90 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a  e]..**.** <dl>.*
1eda0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d  * <dt>SQLITE_LIM
1edb0 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a  IT_LENGTH</dt>.*
1edc0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1edd0 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 73 74  m size of any st
1ede0 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f 72 20  ring or BLOB or 
1edf0 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e 0a 2a  table row.<dd>.*
1ee00 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
1ee10 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48  LIMIT_SQL_LENGTH
1ee20 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
1ee30 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   maximum length 
1ee40 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  of an SQL statem
1ee50 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ent.</dd>.**.** 
1ee60 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1ee70 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a 2a 20  _COLUMN</dt>.** 
1ee80 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
1ee90 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
1eea0 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64 65 66  s in a table def
1eeb0 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20 74 68  inition or in th
1eec0 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20  e.** result set 
1eed0 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 6f 72  of a [SELECT] or
1eee0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
1eef0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
1ef00 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 72  n an index.** or
1ef10 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20   in an ORDER BY 
1ef20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  or GROUP BY clau
1ef30 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  se.</dd>.**.** <
1ef40 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
1ef50 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a  EXPR_DEPTH</dt>.
1ef60 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
1ef70 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  um depth of the 
1ef80 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61 6e  parse tree on an
1ef90 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f 64  y expression.</d
1efa0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
1efb0 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55  ITE_LIMIT_COMPOU
1efc0 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a  ND_SELECT</dt>.*
1efd0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1efe0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  m number of term
1eff0 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20  s in a compound 
1f000 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1f010 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
1f020 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44  >SQLITE_LIMIT_VD
1f030 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  BE_OP</dt>.** <d
1f040 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  d>The maximum nu
1f050 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74  mber of instruct
1f060 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75 61  ions in a virtua
1f070 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 61  l machine progra
1f080 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d 70  m.** used to imp
1f090 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 74  lement an SQL st
1f0a0 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a  atement.</dd>.**
1f0b0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
1f0c0 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  IMIT_FUNCTION_AR
1f0d0 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  G</dt>.** <dd>Th
1f0e0 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1f0f0 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e   of arguments on
1f100 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64   a function.</dd
1f110 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
1f120 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45  TE_LIMIT_ATTACHE
1f130 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
1f140 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1f150 20 6f 66 20 5b 41 54 54 41 43 48 20 7c 20 61 74   of [ATTACH | at
1f160 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
1f170 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  ].</dd>.**.** <d
1f180 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t>SQLITE_LIMIT_L
1f190 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47  IKE_PATTERN_LENG
1f1a0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  TH</dt>.** <dd>T
1f1b0 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
1f1c0 68 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e  h of the pattern
1f1d0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
1f1e0 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 5b 47   [LIKE] or.** [G
1f1f0 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73 2e 3c  LOB] operators.<
1f200 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
1f210 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
1f220 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74 3e  ABLE_NUMBER</dt>
1f230 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1f240 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76 61  mum number of va
1f250 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 53 51  riables in an SQ
1f260 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  L statement that
1f270 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e 64   can.** be bound
1f280 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
1f290 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52  >SQLITE_LIMIT_TR
1f2a0 49 47 47 45 52 5f 44 45 50 54 48 3c 2f 64 74 3e  IGGER_DEPTH</dt>
1f2b0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1f2c0 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 72 65 63  mum depth of rec
1f2d0 75 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 67 67  ursion for trigg
1f2e0 65 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64  ers.</dd>.** </d
1f2f0 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  l>.*/.#define SQ
1f300 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1f310 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
1f320 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53       0.#define S
1f330 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f  QLITE_LIMIT_SQL_
1f340 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20  LENGTH          
1f350 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
1f360 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c  SQLITE_LIMIT_COL
1f370 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  UMN             
1f380 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65         2.#define
1f390 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58   SQLITE_LIMIT_EX
1f3a0 50 52 5f 44 45 50 54 48 20 20 20 20 20 20 20 20  PR_DEPTH        
1f3b0 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e          3.#defin
1f3c0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  e SQLITE_LIMIT_C
1f3d0 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20  OMPOUND_SELECT  
1f3e0 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69           4.#defi
1f3f0 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
1f400 56 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20  VDBE_OP         
1f410 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66            5.#def
1f420 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
1f430 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20  _FUNCTION_ARG   
1f440 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65             6.#de
1f450 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
1f460 54 5f 41 54 54 41 43 48 45 44 20 20 20 20 20 20  T_ATTACHED      
1f470 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64              7.#d
1f480 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d  efine SQLITE_LIM
1f490 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  IT_LIKE_PATTERN_
1f4a0 4c 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23  LENGTH       8.#
1f4b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49  define SQLITE_LI
1f4c0 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
1f4d0 42 45 52 20 20 20 20 20 20 20 20 20 20 20 39 0a  BER           9.
1f4e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
1f4f0 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50  IMIT_TRIGGER_DEP
1f500 54 48 20 20 20 20 20 20 20 20 20 20 20 20 31 30  TH            10
1f510 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
1f520 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53  : Compiling An S
1f530 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31  QL Statement {H1
1f540 33 30 31 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a  3010} <S10000>.*
1f550 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c  * KEYWORDS: {SQL
1f560 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69   statement compi
1f570 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78  ler}.**.** To ex
1f580 65 63 75 74 65 20 61 6e 20 53 51 4c 20 71 75 65  ecute an SQL que
1f590 72 79 2c 20 69 74 20 6d 75 73 74 20 66 69 72 73  ry, it must firs
1f5a0 74 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e  t be compiled in
1f5b0 74 6f 20 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a  to a byte-code.*
1f5c0 2a 20 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20  * program using 
1f5d0 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75  one of these rou
1f5e0 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tines..**.** The
1f5f0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c   first argument,
1f600 20 22 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74   "db", is a [dat
1f610 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1f620 5d 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  ] obtained from 
1f630 61 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63 63 65  a.** prior succe
1f640 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73  ssful call to [s
1f650 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
1f660 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
1f670 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  ()] or.** [sqlit
1f680 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 20 54  e3_open16()].  T
1f690 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
1f6a0 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20  ection must not 
1f6b0 68 61 76 65 20 62 65 65 6e 20 63 6c 6f 73 65 64  have been closed
1f6c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ..**.** The seco
1f6d0 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 7a 53  nd argument, "zS
1f6e0 71 6c 22 2c 20 69 73 20 74 68 65 20 73 74 61 74  ql", is the stat
1f6f0 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70  ement to be comp
1f700 69 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a  iled, encoded.**
1f710 20 61 73 20 65 69 74 68 65 72 20 55 54 46 2d 38   as either UTF-8
1f720 20 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 68 65   or UTF-16.  The
1f730 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1f740 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70  () and sqlite3_p
1f750 72 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20 69  repare_v2().** i
1f760 6e 74 65 72 66 61 63 65 73 20 75 73 65 20 55 54  nterfaces use UT
1f770 46 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74 65 33  F-8, and sqlite3
1f780 5f 70 72 65 70 61 72 65 31 36 28 29 20 61 6e 64  _prepare16() and
1f790 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1f7a0 31 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20 55  16_v2().** use U
1f7b0 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  TF-16..**.** If 
1f7c0 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65  the nByte argume
1f7d0 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  nt is less than 
1f7e0 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c 20  zero, then zSql 
1f7f0 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 74 68  is read up to th
1f800 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72 6f 20  e.** first zero 
1f810 74 65 72 6d 69 6e 61 74 6f 72 2e 20 49 66 20 6e  terminator. If n
1f820 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67 61  Byte is non-nega
1f830 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 69 73  tive, then it is
1f840 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20   the maximum.** 
1f850 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74 65 73  number of  bytes
1f860 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e   read from zSql.
1f870 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69 73 20    When nByte is 
1f880 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68  non-negative, th
1f890 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 6e 67  e.** zSql string
1f8a0 20 65 6e 64 73 20 61 74 20 65 69 74 68 65 72 20   ends at either 
1f8b0 74 68 65 20 66 69 72 73 74 20 27 5c 30 30 30 27  the first '\000'
1f8c0 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63 68 61   or '\u0000' cha
1f8d0 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74 68 65  racter or.** the
1f8e0 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65 2c 20   nByte-th byte, 
1f8f0 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20  whichever comes 
1f900 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 63 61  first. If the ca
1f910 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 74 68  ller knows.** th
1f920 61 74 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  at the supplied 
1f930 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d 74 65  string is nul-te
1f940 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e 20 74  rminated, then t
1f950 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c 6c 0a  here is a small.
1f960 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61  ** performance a
1f970 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65 20 67  dvantage to be g
1f980 61 69 6e 65 64 20 62 79 20 70 61 73 73 69 6e 67  ained by passing
1f990 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61 6d 65   an nByte parame
1f9a0 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 65  ter that.** is e
1f9b0 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d 62  qual to the numb
1f9c0 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
1f9d0 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20  he input string 
1f9e0 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f 69 3e  <i>including</i>
1f9f0 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d  .** the nul-term
1fa00 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 0a 2a 2a  inator bytes..**
1fa10 0a 2a 2a 20 49 66 20 70 7a 54 61 69 6c 20 69 73  .** If pzTail is
1fa20 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a   not NULL then *
1fa30 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74  pzTail is made t
1fa40 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  o point to the f
1fa50 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 70 61 73  irst byte.** pas
1fa60 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
1fa70 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 65   first SQL state
1fa80 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54  ment in zSql.  T
1fa90 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e  hese routines on
1faa0 6c 79 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74 68  ly.** compile th
1fab0 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e  e first statemen
1fac0 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a 70  t in zSql, so *p
1fad0 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70 6f  zTail is left po
1fae0 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 77 68 61  inting to.** wha
1faf0 74 20 72 65 6d 61 69 6e 73 20 75 6e 63 6f 6d 70  t remains uncomp
1fb00 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53  iled..**.** *ppS
1fb10 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  tmt is left poin
1fb20 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 6c  ting to a compil
1fb30 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  ed [prepared sta
1fb40 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 6e  tement] that can
1fb50 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20   be.** executed 
1fb60 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73  using [sqlite3_s
1fb70 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65 72  tep()].  If ther
1fb80 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a  e is an error, *
1fb90 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a 2a  ppStmt is set.**
1fba0 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68   to NULL.  If th
1fbb0 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e  e input text con
1fbc0 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66  tains no SQL (if
1fbd0 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e   the input is an
1fbe0 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67   empty.** string
1fbf0 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74   or a comment) t
1fc00 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73  hen *ppStmt is s
1fc10 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 54  et to NULL..** T
1fc20 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65  he calling proce
1fc30 64 75 72 65 20 69 73 20 72 65 73 70 6f 6e 73 69  dure is responsi
1fc40 62 6c 65 20 66 6f 72 20 64 65 6c 65 74 69 6e 67  ble for deleting
1fc50 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a   the compiled.**
1fc60 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 75   SQL statement u
1fc70 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69  sing [sqlite3_fi
1fc80 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74 65 72 20  nalize()] after 
1fc90 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  it has finished 
1fca0 77 69 74 68 20 69 74 2e 0a 2a 2a 20 70 70 53 74  with it..** ppSt
1fcb0 6d 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55  mt may not be NU
1fcc0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63  LL..**.** On suc
1fcd0 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b  cess, [SQLITE_OK
1fce0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f  ] is returned, o
1fcf0 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72  therwise an [err
1fd00 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
1fd10 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rned..**.** The 
1fd20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
1fd30 76 32 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  v2() and sqlite3
1fd40 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 20  _prepare16_v2() 
1fd50 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a  interfaces are.*
1fd60 2a 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f  * recommended fo
1fd70 72 20 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61  r all new progra
1fd80 6d 73 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65  ms. The two olde
1fd90 72 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65  r interfaces are
1fda0 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72   retained.** for
1fdb0 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
1fdc0 74 69 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68  tibility, but th
1fdd0 65 69 72 20 75 73 65 20 69 73 20 64 69 73 63 6f  eir use is disco
1fde0 75 72 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68  uraged..** In th
1fdf0 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65  e "v2" interface
1fe00 73 2c 20 74 68 65 20 70 72 65 70 61 72 65 64 20  s, the prepared 
1fe10 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61  statement.** tha
1fe20 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 28 74  t is returned (t
1fe30 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74  he [sqlite3_stmt
1fe40 5d 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69  ] object) contai
1fe50 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ns a copy of the
1fe60 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  .** original SQL
1fe70 20 74 65 78 74 2e 20 54 68 69 73 20 63 61 75 73   text. This caus
1fe80 65 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  es the [sqlite3_
1fe90 73 74 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63  step()] interfac
1fea0 65 20 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61  e to.** behave a
1feb0 20 64 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20   differently in 
1fec0 74 68 72 65 65 20 77 61 79 73 3a 0a 2a 2a 0a 2a  three ways:.**.*
1fed0 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a  * <ol>.** <li>.*
1fee0 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
1fef0 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  e schema changes
1ff00 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 74  , instead of ret
1ff10 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 45 5f 53  urning [SQLITE_S
1ff20 43 48 45 4d 41 5d 20 61 73 20 69 74 0a 2a 2a 20  CHEMA] as it.** 
1ff30 61 6c 77 61 79 73 20 75 73 65 64 20 74 6f 20 64  always used to d
1ff40 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  o, [sqlite3_step
1ff50 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74  ()] will automat
1ff60 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65  ically recompile
1ff70 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73 74 61 74   the SQL.** stat
1ff80 65 6d 65 6e 74 20 61 6e 64 20 74 72 79 20 74 6f  ement and try to
1ff90 20 72 75 6e 20 69 74 20 61 67 61 69 6e 2e 20 20   run it again.  
1ffa0 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 68 61  If the schema ha
1ffb0 73 20 63 68 61 6e 67 65 64 20 69 6e 0a 2a 2a 20  s changed in.** 
1ffc0 61 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65 73  a way that makes
1ffd0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6e   the statement n
1ffe0 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69 64 2c 20  o longer valid, 
1fff0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
20000 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72   will still.** r
20010 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 53 43  eturn [SQLITE_SC
20020 48 45 4d 41 5d 2e 20 20 42 75 74 20 75 6e 6c 69  HEMA].  But unli
20030 6b 65 20 74 68 65 20 6c 65 67 61 63 79 20 62 65  ke the legacy be
20040 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49 54 45 5f  havior, [SQLITE_
20050 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f  SCHEMA] is.** no
20060 77 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e  w a fatal error.
20070 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74    Calling [sqlit
20080 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
20090 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e 6f 74 20   again will not 
200a0 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65 72 72 6f  make the.** erro
200b0 72 20 67 6f 20 61 77 61 79 2e 20 20 4e 6f 74 65  r go away.  Note
200c0 3a 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 65  : use [sqlite3_e
200d0 72 72 6d 73 67 28 29 5d 20 74 6f 20 66 69 6e 64  rrmsg()] to find
200e0 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 6f 66 20   the text.** of 
200f0 74 68 65 20 70 61 72 73 69 6e 67 20 65 72 72 6f  the parsing erro
20100 72 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 69  r that results i
20110 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48  n an [SQLITE_SCH
20120 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a 2a 2a 20  EMA] return..** 
20130 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e  </li>.**.** <li>
20140 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65 72 72 6f  .** When an erro
20150 72 20 6f 63 63 75 72 73 2c 20 5b 73 71 6c 69 74  r occurs, [sqlit
20160 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20  e3_step()] will 
20170 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68  return one of th
20180 65 20 64 65 74 61 69 6c 65 64 0a 2a 2a 20 5b 65  e detailed.** [e
20190 72 72 6f 72 20 63 6f 64 65 73 5d 20 6f 72 20 5b  rror codes] or [
201a0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
201b0 6f 64 65 73 5d 2e 20 20 54 68 65 20 6c 65 67 61  odes].  The lega
201c0 63 79 20 62 65 68 61 76 69 6f 72 20 77 61 73 20  cy behavior was 
201d0 74 68 61 74 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  that.** [sqlite3
201e0 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c 64 20 6f  _step()] would o
201f0 6e 6c 79 20 72 65 74 75 72 6e 20 61 20 67 65 6e  nly return a gen
20200 65 72 69 63 20 5b 53 51 4c 49 54 45 5f 45 52 52  eric [SQLITE_ERR
20210 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f 64 65 0a  OR] result code.
20220 2a 2a 20 61 6e 64 20 79 6f 75 20 77 6f 75 6c 64  ** and you would
20230 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 20   have to make a 
20240 73 65 63 6f 6e 64 20 63 61 6c 6c 20 74 6f 20 5b  second call to [
20250 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
20260 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20   in order.** to 
20270 66 69 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79  find the underly
20280 69 6e 67 20 63 61 75 73 65 20 6f 66 20 74 68 65  ing cause of the
20290 20 70 72 6f 62 6c 65 6d 2e 20 57 69 74 68 20 74   problem. With t
202a0 68 65 20 22 76 32 22 20 70 72 65 70 61 72 65 0a  he "v2" prepare.
202b0 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74  ** interfaces, t
202c0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 72 65  he underlying re
202d0 61 73 6f 6e 20 66 6f 72 20 74 68 65 20 65 72 72  ason for the err
202e0 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  or is returned i
202f0 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c  mmediately..** <
20300 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a  /li>.**.** <li>.
20310 2a 2a 20 5e 49 66 20 74 68 65 20 76 61 6c 75 65  ** ^If the value
20320 20 6f 66 20 61 20 5b 70 61 72 61 6d 65 74 65 72   of a [parameter
20330 20 7c 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65   | host paramete
20340 72 5d 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  r] in the WHERE 
20350 63 6c 61 75 73 65 20 6d 69 67 68 74 0a 2a 2a 20  clause might.** 
20360 63 68 61 6e 67 65 20 74 68 65 20 71 75 65 72 79  change the query
20370 20 70 6c 61 6e 20 66 6f 72 20 61 20 73 74 61 74   plan for a stat
20380 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 65 20  ement, then the 
20390 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 62 65  statement may be
203a0 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  .** automaticall
203b0 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 28 61 73  y recompiled (as
203c0 20 69 66 20 74 68 65 72 65 20 68 61 64 20 62 65   if there had be
203d0 65 6e 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e  en a schema chan
203e0 67 65 29 20 6f 6e 20 74 68 65 20 66 69 72 73 74  ge) on the first
203f0 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74   .** [sqlite3_st
20400 65 70 28 29 5d 20 63 61 6c 6c 20 66 6f 6c 6c 6f  ep()] call follo
20410 77 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 65 20  wing any change 
20420 74 6f 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69  to the .** [sqli
20430 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 7c 20  te3_bind_text | 
20440 62 69 6e 64 69 6e 67 73 5d 20 6f 66 20 74 68 65  bindings] of the
20450 20 5b 70 61 72 61 6d 65 74 65 72 5d 2e 20 0a 2a   [parameter]. .*
20460 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f 6c 3e  * </li>.** </ol>
20470 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
20480 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 30 31 31 5d  nts:.** [H13011]
20490 20 5b 48 31 33 30 31 32 5d 20 5b 48 31 33 30 31   [H13012] [H1301
204a0 33 5d 20 5b 48 31 33 30 31 34 5d 20 5b 48 31 33  3] [H13014] [H13
204b0 30 31 35 5d 20 5b 48 31 33 30 31 36 5d 20 5b 48  015] [H13016] [H
204c0 31 33 30 31 39 5d 20 5b 48 31 33 30 32 31 5d 0a  13019] [H13021].
204d0 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  **.*/.SQLITE_API
204e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
204f0 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20  pare(.  sqlite3 
20500 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
20510 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
20520 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
20530 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  ar *zSql,       
20540 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  /* SQL statement
20550 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  , UTF-8 encoded 
20560 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20  */.  int nByte, 
20570 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20580 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f  Maximum length o
20590 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
205a0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
205b0 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a  mt **ppStmt,  /*
205c0 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20   OUT: Statement 
205d0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
205e0 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20  t char **pzTail 
205f0 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e      /* OUT: Poin
20600 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f  ter to unused po
20610 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f  rtion of zSql */
20620 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
20630 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nt sqlite3_prepa
20640 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  re_v2(.  sqlite3
20650 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
20660 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
20670 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
20680 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  har *zSql,      
20690 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   /* SQL statemen
206a0 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  t, UTF-8 encoded
206b0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c   */.  int nByte,
206c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
206d0 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   Maximum length 
206e0 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
206f0 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  . */.  sqlite3_s
20700 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f  tmt **ppStmt,  /
20710 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74  * OUT: Statement
20720 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
20730 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  st char **pzTail
20740 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69       /* OUT: Poi
20750 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70  nter to unused p
20760 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a  ortion of zSql *
20770 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  /.);.SQLITE_API 
20780 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70  int sqlite3_prep
20790 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33  are16(.  sqlite3
207a0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
207b0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
207c0 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  dle */.  const v
207d0 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  oid *zSql,      
207e0 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   /* SQL statemen
207f0 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  t, UTF-16 encode
20800 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  d */.  int nByte
20810 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
20820 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
20830 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
20840 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
20850 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
20860 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e  /* OUT: Statemen
20870 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  t handle */.  co
20880 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
20890 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f  l     /* OUT: Po
208a0 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20  inter to unused 
208b0 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20  portion of zSql 
208c0 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  */.);.SQLITE_API
208d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
208e0 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73 71 6c  pare16_v2(.  sql
208f0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
20900 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
20910 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
20920 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20  st void *zSql,  
20930 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74       /* SQL stat
20940 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e  ement, UTF-16 en
20950 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  coded */.  int n
20960 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  Byte,           
20970 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65     /* Maximum le
20980 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
20990 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  bytes. */.  sqli
209a0 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
209b0 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74  t,  /* OUT: Stat
209c0 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a  ement handle */.
209d0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70    const void **p
209e0 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54  zTail     /* OUT
209f0 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75  : Pointer to unu
20a00 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a  sed portion of z
20a10 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  Sql */.);../*.**
20a20 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69   CAPI3REF: Retri
20a30 65 76 69 6e 67 20 53 74 61 74 65 6d 65 6e 74 20  eving Statement 
20a40 53 51 4c 20 7b 48 31 33 31 30 30 7d 20 3c 48 31  SQL {H13100} <H1
20a50 33 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  3000>.**.** This
20a60 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62   interface can b
20a70 65 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65  e used to retrie
20a80 76 65 20 61 20 73 61 76 65 64 20 63 6f 70 79 20  ve a saved copy 
20a90 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a  of the original.
20aa0 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 73 65 64  ** SQL text used
20ab0 20 74 6f 20 63 72 65 61 74 65 20 61 20 5b 70 72   to create a [pr
20ac0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
20ad0 5d 20 69 66 20 74 68 61 74 20 73 74 61 74 65 6d  ] if that statem
20ae0 65 6e 74 20 77 61 73 0a 2a 2a 20 63 6f 6d 70 69  ent was.** compi
20af0 6c 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 72  led using either
20b00 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
20b10 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69  e_v2()] or [sqli
20b20 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
20b30 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ()]..**.** Requi
20b40 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33  rements:.** [H13
20b50 31 30 31 5d 20 5b 48 31 33 31 30 32 5d 20 5b 48  101] [H13102] [H
20b60 31 33 31 30 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45  13103].*/.SQLITE
20b70 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
20b80 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c  *sqlite3_sql(sql
20b90 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
20ba0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
20bb0 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20  EF: Dynamically 
20bc0 54 79 70 65 64 20 56 61 6c 75 65 20 4f 62 6a 65  Typed Value Obje
20bd0 63 74 20 7b 48 31 35 30 30 30 7d 20 3c 53 32 30  ct {H15000} <S20
20be0 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  200>.** KEYWORDS
20bf0 3a 20 7b 70 72 6f 74 65 63 74 65 64 20 73 71 6c  : {protected sql
20c00 69 74 65 33 5f 76 61 6c 75 65 7d 20 7b 75 6e 70  ite3_value} {unp
20c10 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
20c20 5f 76 61 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20 53 51  _value}.**.** SQ
20c30 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73 71  Lite uses the sq
20c40 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
20c50 63 74 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20  ct to represent 
20c60 61 6c 6c 20 76 61 6c 75 65 73 0a 2a 2a 20 74 68  all values.** th
20c70 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64  at can be stored
20c80 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 74   in a database t
20c90 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 75 73 65  able. SQLite use
20ca0 73 20 64 79 6e 61 6d 69 63 20 74 79 70 69 6e 67  s dynamic typing
20cb0 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76 61 6c 75  .** for the valu
20cc0 65 73 20 69 74 20 73 74 6f 72 65 73 2e 20 56 61  es it stores. Va
20cd0 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 73  lues stored in s
20ce0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
20cf0 65 63 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 69  ects.** can be i
20d00 6e 74 65 67 65 72 73 2c 20 66 6c 6f 61 74 69 6e  ntegers, floatin
20d10 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 2c 20  g point values, 
20d20 73 74 72 69 6e 67 73 2c 20 42 4c 4f 42 73 2c 20  strings, BLOBs, 
20d30 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41  or NULL..**.** A
20d40 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  n sqlite3_value 
20d50 6f 62 6a 65 63 74 20 6d 61 79 20 62 65 20 65 69  object may be ei
20d60 74 68 65 72 20 22 70 72 6f 74 65 63 74 65 64 22  ther "protected"
20d70 20 6f 72 20 22 75 6e 70 72 6f 74 65 63 74 65 64   or "unprotected
20d80 22 2e 0a 2a 2a 20 53 6f 6d 65 20 69 6e 74 65 72  "..** Some inter
20d90 66 61 63 65 73 20 72 65 71 75 69 72 65 20 61 20  faces require a 
20da0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
20db0 33 5f 76 61 6c 75 65 2e 20 20 4f 74 68 65 72 20  3_value.  Other 
20dc0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 77 69  interfaces.** wi
20dd0 6c 6c 20 61 63 63 65 70 74 20 65 69 74 68 65 72  ll accept either
20de0 20 61 20 70 72 6f 74 65 63 74 65 64 20 6f 72 20   a protected or 
20df0 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 73  an unprotected s
20e00 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a  qlite3_value..**
20e10 20 45 76 65 72 79 20 69 6e 74 65 72 66 61 63 65   Every interface
20e20 20 74 68 61 74 20 61 63 63 65 70 74 73 20 73 71   that accepts sq
20e30 6c 69 74 65 33 5f 76 61 6c 75 65 20 61 72 67 75  lite3_value argu
20e40 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65 73 0a  ments specifies.
20e50 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ** whether or no
20e60 74 20 69 74 20 72 65 71 75 69 72 65 73 20 61 20  t it requires a 
20e70 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
20e80 33 5f 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  3_value..**.** T
20e90 68 65 20 74 65 72 6d 73 20 22 70 72 6f 74 65 63  he terms "protec
20ea0 74 65 64 22 20 61 6e 64 20 22 75 6e 70 72 6f 74  ted" and "unprot
20eb0 65 63 74 65 64 22 20 72 65 66 65 72 20 74 6f 20  ected" refer to 
20ec0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a  whether or not.*
20ed0 2a 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c  * a mutex is hel
20ee0 64 2e 20 20 41 20 69 6e 74 65 72 6e 61 6c 20 6d  d.  A internal m
20ef0 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72  utex is held for
20f00 20 61 20 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20   a protected.** 
20f10 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
20f20 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d 75 74 65  ject but no mute
20f30 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61 6e  x is held for an
20f40 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20   unprotected.** 
20f50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
20f60 6a 65 63 74 2e 20 20 49 66 20 53 51 4c 69 74 65  ject.  If SQLite
20f70 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20   is compiled to 
20f80 62 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64  be single-thread
20f90 65 64 0a 2a 2a 20 28 77 69 74 68 20 5b 53 51 4c  ed.** (with [SQL
20fa0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 30  ITE_THREADSAFE=0
20fb0 5d 20 61 6e 64 20 77 69 74 68 20 5b 73 71 6c 69  ] and with [sqli
20fc0 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 29  te3_threadsafe()
20fd0 5d 20 72 65 74 75 72 6e 69 6e 67 20 30 29 0a 2a  ] returning 0).*
20fe0 2a 20 6f 72 20 69 66 20 53 51 4c 69 74 65 20 69  * or if SQLite i
20ff0 73 20 72 75 6e 20 69 6e 20 6f 6e 65 20 6f 66 20  s run in one of 
21000 72 65 64 75 63 65 64 20 6d 75 74 65 78 20 6d 6f  reduced mutex mo
21010 64 65 73 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  des .** [SQLITE_
21020 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52  CONFIG_SINGLETHR
21030 45 41 44 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  EAD] or [SQLITE_
21040 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45  CONFIG_MULTITHRE
21050 41 44 5d 0a 2a 2a 20 74 68 65 6e 20 74 68 65 72  AD].** then ther
21060 65 20 69 73 20 6e 6f 20 64 69 73 74 69 6e 63 74  e is no distinct
21070 69 6f 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74  ion between prot
21080 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74  ected and unprot
21090 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ected.** sqlite3
210a0 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 61  _value objects a
210b0 6e 64 20 74 68 65 79 20 63 61 6e 20 62 65 20 75  nd they can be u
210c0 73 65 64 20 69 6e 74 65 72 63 68 61 6e 67 65 61  sed interchangea
210d0 62 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a  bly.  However,.*
210e0 2a 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 63 6f  * for maximum co
210f0 64 65 20 70 6f 72 74 61 62 69 6c 69 74 79 20 69  de portability i
21100 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  t is recommended
21110 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 69 6f   that applicatio
21120 6e 73 0a 2a 2a 20 73 74 69 6c 6c 20 6d 61 6b 65  ns.** still make
21130 20 74 68 65 20 64 69 73 74 69 6e 63 74 69 6f 6e   the distinction
21140 20 62 65 74 77 65 65 6e 20 62 65 74 77 65 65 6e   between between
21150 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75   protected and u
21160 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71  nprotected.** sq
21170 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
21180 63 74 73 20 65 76 65 6e 20 77 68 65 6e 20 6e 6f  cts even when no
21190 74 20 73 74 72 69 63 74 6c 79 20 72 65 71 75 69  t strictly requi
211a0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  red..**.** The s
211b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
211c0 65 63 74 73 20 74 68 61 74 20 61 72 65 20 70 61  ects that are pa
211d0 73 73 65 64 20 61 73 20 70 61 72 61 6d 65 74 65  ssed as paramete
211e0 72 73 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 69  rs into the.** i
211f0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
21200 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65   [application-de
21210 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69  fined SQL functi
21220 6f 6e 73 5d 20 61 72 65 20 70 72 6f 74 65 63 74  ons] are protect
21230 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ed..** The sqlit
21240 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20  e3_value object 
21250 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b  returned by.** [
21260 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76  sqlite3_column_v
21270 61 6c 75 65 28 29 5d 20 69 73 20 75 6e 70 72 6f  alue()] is unpro
21280 74 65 63 74 65 64 2e 0a 2a 2a 20 55 6e 70 72 6f  tected..** Unpro
21290 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
212a0 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 6d 61 79  alue objects may
212b0 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69   only be used wi
212c0 74 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72  th.** [sqlite3_r
212d0 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 20 61  esult_value()] a
212e0 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  nd [sqlite3_bind
212f0 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 54 68  _value()]..** Th
21300 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
21310 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f  _blob | sqlite3_
21320 76 61 6c 75 65 5f 74 79 70 65 28 29 5d 20 66 61  value_type()] fa
21330 6d 69 6c 79 20 6f 66 0a 2a 2a 20 69 6e 74 65 72  mily of.** inter
21340 66 61 63 65 73 20 72 65 71 75 69 72 65 20 70 72  faces require pr
21350 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
21360 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a  value objects..*
21370 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
21380 20 4d 65 6d 20 73 71 6c 69 74 65 33 5f 76 61 6c   Mem sqlite3_val
21390 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  ue;../*.** CAPI3
213a0 52 45 46 3a 20 53 51 4c 20 46 75 6e 63 74 69 6f  REF: SQL Functio
213b0 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a 65 63 74  n Context Object
213c0 20 7b 48 31 36 30 30 31 7d 20 3c 53 32 30 32 30   {H16001} <S2020
213d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e  0>.**.** The con
213e0 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 61 6e  text in which an
213f0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 65 78   SQL function ex
21400 65 63 75 74 65 73 20 69 73 20 73 74 6f 72 65 64  ecutes is stored
21410 20 69 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65   in an.** sqlite
21420 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74  3_context object
21430 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  .  A pointer to 
21440 61 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  an sqlite3_conte
21450 78 74 20 6f 62 6a 65 63 74 0a 2a 2a 20 69 73 20  xt object.** is 
21460 61 6c 77 61 79 73 20 66 69 72 73 74 20 70 61 72  always first par
21470 61 6d 65 74 65 72 20 74 6f 20 5b 61 70 70 6c 69  ameter to [appli
21480 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53  cation-defined S
21490 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a  QL functions]..*
214a0 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * The applicatio
214b0 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75  n-defined SQL fu
214c0 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
214d0 61 74 69 6f 6e 20 77 69 6c 6c 20 70 61 73 73 20  ation will pass 
214e0 74 68 69 73 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  this.** pointer 
214f0 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 63 61 6c  through into cal
21500 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  ls to [sqlite3_r
21510 65 73 75 6c 74 5f 69 6e 74 20 7c 20 73 71 6c 69  esult_int | sqli
21520 74 65 33 5f 72 65 73 75 6c 74 28 29 5d 2c 0a 2a  te3_result()],.*
21530 2a 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 65  * [sqlite3_aggre
21540 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 5d 2c  gate_context()],
21550 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64   [sqlite3_user_d
21560 61 74 61 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  ata()],.** [sqli
21570 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
21580 61 6e 64 6c 65 28 29 5d 2c 20 5b 73 71 6c 69 74  andle()], [sqlit
21590 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29  e3_get_auxdata()
215a0 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b 73 71  ],.** and/or [sq
215b0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
215c0 61 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  a()]..*/.typedef
215d0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
215e0 63 6f 6e 74 65 78 74 20 73 71 6c 69 74 65 33 5f  context sqlite3_
215f0 63 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 2a 2a 20  context;../*.** 
21600 43 41 50 49 33 52 45 46 3a 20 42 69 6e 64 69 6e  CAPI3REF: Bindin
21610 67 20 56 61 6c 75 65 73 20 54 6f 20 50 72 65 70  g Values To Prep
21620 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 73 20  ared Statements 
21630 7b 48 31 33 35 30 30 7d 20 3c 53 37 30 33 30 30  {H13500} <S70300
21640 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
21650 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 7d 20  host parameter} 
21660 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73  {host parameters
21670 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65  } {host paramete
21680 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b 45 59 57 4f  r name}.** KEYWO
21690 52 44 53 3a 20 7b 53 51 4c 20 70 61 72 61 6d 65  RDS: {SQL parame
216a0 74 65 72 7d 20 7b 53 51 4c 20 70 61 72 61 6d 65  ter} {SQL parame
216b0 74 65 72 73 7d 20 7b 70 61 72 61 6d 65 74 65 72  ters} {parameter
216c0 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a 0a 2a 2a 20   binding}.**.** 
216d0 49 6e 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e  In the SQL strin
216e0 67 73 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c  gs input to [sql
216f0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
21700 29 5d 20 61 6e 64 20 69 74 73 20 76 61 72 69 61  )] and its varia
21710 6e 74 73 2c 0a 2a 2a 20 6c 69 74 65 72 61 6c 73  nts,.** literals
21720 20 6d 61 79 20 62 65 20 72 65 70 6c 61 63 65 64   may be replaced
21730 20 62 79 20 61 20 5b 70 61 72 61 6d 65 74 65 72   by a [parameter
21740 5d 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6f  ] that matches o
21750 6e 65 20 6f 66 20 66 6f 6c 6c 6f 77 69 6e 67 0a  ne of following.
21760 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a 0a 2a 2a  ** templates:.**
21770 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
21780 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e    ?.** <li>  ?NN
21790 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a  N.** <li>  :VVV.
217a0 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a  ** <li>  @VVV.**
217b0 20 3c 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c   <li>  $VVV.** <
217c0 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  /ul>.**.** In th
217d0 65 20 74 65 6d 70 6c 61 74 65 73 20 61 62 6f 76  e templates abov
217e0 65 2c 20 4e 4e 4e 20 72 65 70 72 65 73 65 6e 74  e, NNN represent
217f0 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c 69 74  s an integer lit
21800 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 56  eral,.** and VVV
21810 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 61   represents an a
21820 6c 70 68 61 6e 75 6d 65 72 69 63 20 69 64 65 6e  lphanumeric iden
21830 74 69 66 65 72 2e 20 20 54 68 65 20 76 61 6c 75  tifer.  The valu
21840 65 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70  es of these.** p
21850 61 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20  arameters (also 
21860 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72  called "host par
21870 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 6f 72  ameter names" or
21880 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73   "SQL parameters
21890 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 74  ").** can be set
218a0 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
218b0 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74  e3_bind_*() rout
218c0 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68 65 72  ines defined her
218d0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  e..**.** The fir
218e0 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
218f0 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  he sqlite3_bind_
21900 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20  *() routines is 
21910 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e  always.** a poin
21920 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69  ter to the [sqli
21930 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74  te3_stmt] object
21940 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a   returned from.*
21950 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
21960 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20  re_v2()] or its 
21970 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  variants..**.** 
21980 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
21990 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 78  ent is the index
219a0 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61 72 61   of the SQL para
219b0 6d 65 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e  meter to be set.
219c0 0a 2a 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 74  .** The leftmost
219d0 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 68   SQL parameter h
219e0 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31  as an index of 1
219f0 2e 20 20 57 68 65 6e 20 74 68 65 20 73 61 6d 65  .  When the same
21a00 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61   named.** SQL pa
21a10 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 20  rameter is used 
21a20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20  more than once, 
21a30 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65  second and subse
21a40 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65  quent.** occurre
21a50 6e 63 65 73 20 68 61 76 65 20 74 68 65 20 73 61  nces have the sa
21a60 6d 65 20 69 6e 64 65 78 20 61 73 20 74 68 65 20  me index as the 
21a70 66 69 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65  first occurrence
21a80 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 66  ..** The index f
21a90 6f 72 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74  or named paramet
21aa0 65 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65  ers can be looke
21ab0 64 20 75 70 20 75 73 69 6e 67 20 74 68 65 0a 2a  d up using the.*
21ac0 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  * [sqlite3_bind_
21ad0 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28  parameter_index(
21ae0 29 5d 20 41 50 49 20 69 66 20 64 65 73 69 72 65  )] API if desire
21af0 64 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a  d.  The index.**
21b00 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61   for "?NNN" para
21b10 6d 65 74 65 72 73 20 69 73 20 74 68 65 20 76 61  meters is the va
21b20 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54  lue of NNN..** T
21b30 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73  he NNN value mus
21b40 74 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61  t be between 1 a
21b50 6e 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  nd the [sqlite3_
21b60 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61  limit()].** para
21b70 6d 65 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49  meter [SQLITE_LI
21b80 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
21b90 42 45 52 5d 20 28 64 65 66 61 75 6c 74 20 76 61  BER] (default va
21ba0 6c 75 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a  lue: 999)..**.**
21bb0 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   The third argum
21bc0 65 6e 74 20 69 73 20 74 68 65 20 76 61 6c 75 65  ent is the value
21bd0 20 74 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 20   to bind to the 
21be0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a  parameter..**.**
21bf0 20 49 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 6e   In those routin
21c00 65 73 20 74 68 61 74 20 68 61 76 65 20 61 20 66  es that have a f
21c10 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20  ourth argument, 
21c20 69 74 73 20 76 61 6c 75 65 20 69 73 20 74 68 65  its value is the
21c30 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  .** number of by
21c40 74 65 73 20 69 6e 20 74 68 65 20 70 61 72 61 6d  tes in the param
21c50 65 74 65 72 2e 20 20 54 6f 20 62 65 20 63 6c 65  eter.  To be cle
21c60 61 72 3a 20 74 68 65 20 76 61 6c 75 65 20 69 73  ar: the value is
21c70 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
21c80 66 20 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69  f <u>bytes</u> i
21c90 6e 20 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74  n the value, not
21ca0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
21cb0 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66  haracters..** If
21cc0 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61   the fourth para
21cd0 6d 65 74 65 72 20 69 73 20 6e 65 67 61 74 69 76  meter is negativ
21ce0 65 2c 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  e, the length of
21cf0 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 0a 2a   the string is.*
21d00 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
21d10 62 79 74 65 73 20 75 70 20 74 6f 20 74 68 65 20  bytes up to the 
21d20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69  first zero termi
21d30 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  nator..**.** The
21d40 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20   fifth argument 
21d50 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  to sqlite3_bind_
21d60 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f  blob(), sqlite3_
21d70 62 69 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64  bind_text(), and
21d80 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  .** sqlite3_bind
21d90 5f 74 65 78 74 31 36 28 29 20 69 73 20 61 20 64  _text16() is a d
21da0 65 73 74 72 75 63 74 6f 72 20 75 73 65 64 20 74  estructor used t
21db0 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65  o dispose of the
21dc0 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69   BLOB or.** stri
21dd0 6e 67 20 61 66 74 65 72 20 53 51 4c 69 74 65 20  ng after SQLite 
21de0 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74  has finished wit
21df0 68 20 69 74 2e 20 49 66 20 74 68 65 20 66 69 66  h it. If the fif
21e00 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a  th argument is.*
21e10 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 76 61  * the special va
21e20 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54  lue [SQLITE_STAT
21e30 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  IC], then SQLite
21e40 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
21e50 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  e.** information
21e60 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c 20 75   is in static, u
21e70 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 61  nmanaged space a
21e80 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  nd does not need
21e90 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a   to be freed..**
21ea0 20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 72   If the fifth ar
21eb0 67 75 6d 65 6e 74 20 68 61 73 20 74 68 65 20 76  gument has the v
21ec0 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 41  alue [SQLITE_TRA
21ed0 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a  NSIENT], then.**
21ee0 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74   SQLite makes it
21ef0 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f  s own private co
21f00 70 79 20 6f 66 20 74 68 65 20 64 61 74 61 20 69  py of the data i
21f10 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f  mmediately, befo
21f20 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65  re.** the sqlite
21f30 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69  3_bind_*() routi
21f40 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a  ne returns..**.*
21f50 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69  * The sqlite3_bi
21f60 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f  nd_zeroblob() ro
21f70 75 74 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c  utine binds a BL
21f80 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74  OB of length N t
21f90 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64  hat.** is filled
21fa0 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 41   with zeroes.  A
21fb0 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 61   zeroblob uses a
21fc0 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66   fixed amount of
21fd0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74   memory.** (just
21fe0 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 68   an integer to h
21ff0 6f 6c 64 20 69 74 73 20 73 69 7a 65 29 20 77 68  old its size) wh
22000 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67 20  ile it is being 
22010 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65  processed..** Ze
22020 72 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 65  roblobs are inte
22030 6e 64 65 64 20 74 6f 20 73 65 72 76 65 20 61 73  nded to serve as
22040 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f   placeholders fo
22050 72 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a  r BLOBs whose.**
22060 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 65   content is late
22070 72 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67 0a  r written using.
22080 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ** [sqlite3_blob
22090 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e  _open | incremen
220a0 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f  tal BLOB I/O] ro
220b0 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67  utines..** A neg
220c0 61 74 69 76 65 20 76 61 6c 75 65 20 66 6f 72 20  ative value for 
220d0 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73  the zeroblob res
220e0 75 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c  ults in a zero-l
220f0 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a  ength BLOB..**.*
22100 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69  * The sqlite3_bi
22110 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20  nd_*() routines 
22120 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61  must be called a
22130 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  fter.** [sqlite3
22140 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28  _prepare_v2()] (
22150 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73  and its variants
22160 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65  ) or [sqlite3_re
22170 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65  set()] and.** be
22180 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74  fore [sqlite3_st
22190 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e  ep()]..** Bindin
221a0 67 73 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72  gs are not clear
221b0 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74  ed by the [sqlit
221c0 65 33 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74  e3_reset()] rout
221d0 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20  ine..** Unbound 
221e0 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 69  parameters are i
221f0 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 4e 55  nterpreted as NU
22200 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  LL..**.** These 
22210 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
22220 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73  [SQLITE_OK] on s
22230 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72  uccess or an err
22240 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e  or code if.** an
22250 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ything goes wron
22260 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47  g.  [SQLITE_RANG
22270 45 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  E] is returned i
22280 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a  f the parameter.
22290 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20  ** index is out 
222a0 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49  of range.  [SQLI
222b0 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74  TE_NOMEM] is ret
222c0 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28  urned if malloc(
222d0 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c  ) fails..** [SQL
222e0 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68  ITE_MISUSE] migh
222f0 74 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66  t be returned if
22300 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
22310 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a  are called on a.
22320 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  ** virtual machi
22330 6e 65 20 74 68 61 74 20 69 73 20 74 68 65 20 77  ne that is the w
22340 72 6f 6e 67 20 73 74 61 74 65 20 6f 72 20 77 68  rong state or wh
22350 69 63 68 20 68 61 73 20 61 6c 72 65 61 64 79 20  ich has already 
22360 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a  been finalized..
22370 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 20  ** Detection of 
22380 6d 69 73 75 73 65 20 69 73 20 75 6e 72 65 6c 69  misuse is unreli
22390 61 62 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 69  able.  Applicati
223a0 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64  ons should not d
223b0 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49  epend.** on SQLI
223c0 54 45 5f 4d 49 53 55 53 45 20 72 65 74 75 72 6e  TE_MISUSE return
223d0 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  s.  SQLITE_MISUS
223e0 45 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  E is intended to
223f0 20 69 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 61   indicate a.** a
22400 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20   logic error in 
22410 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e  the application.
22420 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e    Future version
22430 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68  s of SQLite migh
22440 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 65  t.** panic rathe
22450 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20 53 51  r than return SQ
22460 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a  LITE_MISUSE..**.
22470 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
22480 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
22490 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a  eter_count()],.*
224a0 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  * [sqlite3_bind_
224b0 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29  parameter_name()
224c0 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ], and [sqlite3_
224d0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
224e0 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52  ndex()]..**.** R
224f0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
22500 5b 48 31 33 35 30 36 5d 20 5b 48 31 33 35 30 39  [H13506] [H13509
22510 5d 20 5b 48 31 33 35 31 32 5d 20 5b 48 31 33 35  ] [H13512] [H135
22520 31 35 5d 20 5b 48 31 33 35 31 38 5d 20 5b 48 31  15] [H13518] [H1
22530 33 35 32 31 5d 20 5b 48 31 33 35 32 34 5d 20 5b  3521] [H13524] [
22540 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 48 31 33 35  H13527].** [H135
22550 33 30 5d 20 5b 48 31 33 35 33 33 5d 20 5b 48 31  30] [H13533] [H1
22560 33 35 33 36 5d 20 5b 48 31 33 35 33 39 5d 20 5b  3536] [H13539] [
22570 48 31 33 35 34 32 5d 20 5b 48 31 33 35 34 35 5d  H13542] [H13545]
22580 20 5b 48 31 33 35 34 38 5d 20 5b 48 31 33 35 35   [H13548] [H1355
22590 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  1].**.*/.SQLITE_
225a0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
225b0 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65  bind_blob(sqlite
225c0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
225d0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e  nst void*, int n
225e0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
225f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
22600 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64  t sqlite3_bind_d
22610 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74  ouble(sqlite3_st
22620 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65  mt*, int, double
22630 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
22640 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  t sqlite3_bind_i
22650 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt(sqlite3_stmt*
22660 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
22670 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
22680 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
22690 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
226a0 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  t, sqlite3_int64
226b0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
226c0 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e  t sqlite3_bind_n
226d0 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ull(sqlite3_stmt
226e0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
226f0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
22700 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 65  bind_text(sqlite
22710 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
22720 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e  nst char*, int n
22730 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
22740 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
22750 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74  t sqlite3_bind_t
22760 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ext16(sqlite3_st
22770 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  mt*, int, const 
22780 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64  void*, int, void
22790 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
227a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
227b0 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73  te3_bind_value(s
227c0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
227d0 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  t, const sqlite3
227e0 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
227f0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
22800 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73  _bind_zeroblob(s
22810 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
22820 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a  t, int n);../*.*
22830 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62  * CAPI3REF: Numb
22840 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d 65  er Of SQL Parame
22850 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c 53  ters {H13600} <S
22860 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  70300>.**.** Thi
22870 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65  s routine can be
22880 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68   used to find th
22890 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c  e number of [SQL
228a0 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a 20   parameters].** 
228b0 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  in a [prepared s
228c0 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c 20  tatement].  SQL 
228d0 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 74  parameters are t
228e0 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a 20  okens of the.** 
228f0 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22  form "?", "?NNN"
22900 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41 22  , ":AAA", "$AAA"
22910 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61 74  , or "@AAA" that
22920 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c 61   serve as.** pla
22930 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76 61  ceholders for va
22940 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b 73  lues that are [s
22950 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
22960 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20   | bound].** to 
22970 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61  the parameters a
22980 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a  t a later time..
22990 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
229a0 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74 75  ne actually retu
229b0 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66  rns the index of
229c0 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72 69   the largest (ri
229d0 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61  ghtmost).** para
229e0 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66  meter. For all f
229f0 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e  orms except ?NNN
22a00 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 72  , this will corr
22a10 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a  espond to the.**
22a20 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 75   number of uniqu
22a30 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 49  e parameters.  I
22a40 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20  f parameters of 
22a50 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73 65  the ?NNN are use
22a60 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 20  d,.** there may 
22a70 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 6c  be gaps in the l
22a80 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ist..**.** See a
22a90 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69  lso: [sqlite3_bi
22aa0 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f  nd_blob|sqlite3_
22ab0 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  bind()],.** [sql
22ac0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
22ad0 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64  ter_name()], and
22ae0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
22af0 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
22b00 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  x()]..**.** Requ
22b10 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
22b20 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  3601].*/.SQLITE_
22b30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
22b40 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
22b50 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  ount(sqlite3_stm
22b60 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  t*);../*.** CAPI
22b70 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20  3REF: Name Of A 
22b80 48 6f 73 74 20 50 61 72 61 6d 65 74 65 72 20 7b  Host Parameter {
22b90 48 31 33 36 32 30 7d 20 3c 53 37 30 33 30 30 3e  H13620} <S70300>
22ba0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
22bb0 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ine returns a po
22bc0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d  inter to the nam
22bd0 65 20 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a  e of the n-th.**
22be0 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d   [SQL parameter]
22bf0 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20   in a [prepared 
22c00 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53  statement]..** S
22c10 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66  QL parameters of
22c20 20 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22   the form "?NNN"
22c30 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40   or ":AAA" or "@
22c40 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a  AAA" or "$AAA".*
22c50 2a 20 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68  * have a name wh
22c60 69 63 68 20 69 73 20 74 68 65 20 73 74 72 69 6e  ich is the strin
22c70 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41  g "?NNN" or ":AA
22c80 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20  A" or "@AAA" or 
22c90 22 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63  "$AAA".** respec
22ca0 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74  tively..** In ot
22cb0 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69  her words, the i
22cc0 6e 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24  nitial ":" or "$
22cd0 22 20 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a  " or "@" or "?".
22ce0 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61  ** is included a
22cf0 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61  s part of the na
22d00 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  me..** Parameter
22d10 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f  s of the form "?
22d20 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c  " without a foll
22d30 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20 68 61  owing integer ha
22d40 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e  ve no name.** an
22d50 64 20 61 72 65 20 61 6c 73 6f 20 72 65 66 65 72  d are also refer
22d60 72 65 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79  red to as "anony
22d70 6d 6f 75 73 20 70 61 72 61 6d 65 74 65 72 73 22  mous parameters"
22d80 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
22d90 74 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72  t host parameter
22da0 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66   has an index of
22db0 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a   1, not 0..**.**
22dc0 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20   If the value n 
22dd0 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  is out of range 
22de0 6f 72 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70  or if the n-th p
22df0 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e  arameter is.** n
22e00 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55  ameless, then NU
22e10 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  LL is returned. 
22e20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   The returned st
22e30 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79  ring is.** alway
22e40 73 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64  s in UTF-8 encod
22e50 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68 65 20  ing even if the 
22e60 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20  named parameter 
22e70 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c  was.** originall
22e80 79 20 73 70 65 63 69 66 69 65 64 20 61 73 20 55  y specified as U
22e90 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65  TF-16 in [sqlite
22ea0 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f  3_prepare16()] o
22eb0 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72  r.** [sqlite3_pr
22ec0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a  epare16_v2()]..*
22ed0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
22ee0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
22ef0 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29  b|sqlite3_bind()
22f00 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  ],.** [sqlite3_b
22f10 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
22f20 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b  unt()], and.** [
22f30 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
22f40 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e  ameter_index()].
22f50 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
22f60 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 32 31 5d  nts:.** [H13621]
22f70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
22f80 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
22f90 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
22fa0 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  r_name(sqlite3_s
22fb0 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a  tmt*, int);../*.
22fc0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e 64  ** CAPI3REF: Ind
22fd0 65 78 20 4f 66 20 41 20 50 61 72 61 6d 65 74 65  ex Of A Paramete
22fe0 72 20 57 69 74 68 20 41 20 47 69 76 65 6e 20 4e  r With A Given N
22ff0 61 6d 65 20 7b 48 31 33 36 34 30 7d 20 3c 53 37  ame {H13640} <S7
23000 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  0300>.**.** Retu
23010 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
23020 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  an SQL parameter
23030 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e   given its name.
23040 20 20 54 68 65 0a 2a 2a 20 69 6e 64 65 78 20 76    The.** index v
23050 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
23060 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73   suitable for us
23070 65 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 0a  e as the second.
23080 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  ** parameter to 
23090 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
230a0 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28  ob|sqlite3_bind(
230b0 29 5d 2e 20 20 41 20 7a 65 72 6f 0a 2a 2a 20 69  )].  A zero.** i
230c0 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 6e 6f  s returned if no
230d0 20 6d 61 74 63 68 69 6e 67 20 70 61 72 61 6d 65   matching parame
230e0 74 65 72 20 69 73 20 66 6f 75 6e 64 2e 20 20 54  ter is found.  T
230f0 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  he parameter.** 
23100 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 67 69 76  name must be giv
23110 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 76 65 6e  en in UTF-8 even
23120 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   if the original
23130 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 77 61   statement.** wa
23140 73 20 70 72 65 70 61 72 65 64 20 66 72 6f 6d 20  s prepared from 
23150 55 54 46 2d 31 36 20 74 65 78 74 20 75 73 69 6e  UTF-16 text usin
23160 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  g [sqlite3_prepa
23170 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a  re16_v2()]..**.*
23180 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
23190 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73  ite3_bind_blob|s
231a0 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a  qlite3_bind()],.
231b0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  ** [sqlite3_bind
231c0 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
231d0 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c  ()], and.** [sql
231e0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
231f0 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a  ter_index()]..**
23200 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
23210 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 5d 0a 2a 2f  :.** [H13641].*/
23220 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
23230 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
23240 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c  ameter_index(sql
23250 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73  ite3_stmt*, cons
23260 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 3b 0a  t char *zName);.
23270 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
23280 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e 64 69   Reset All Bindi
23290 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61 72 65  ngs On A Prepare
232a0 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33  d Statement {H13
232b0 36 36 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a  660} <S70300>.**
232c0 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20 74 6f 20  .** Contrary to 
232d0 74 68 65 20 69 6e 74 75 69 74 69 6f 6e 20 6f 66  the intuition of
232e0 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65 33 5f   many, [sqlite3_
232f0 72 65 73 65 74 28 29 5d 20 64 6f 65 73 20 6e 6f  reset()] does no
23300 74 20 72 65 73 65 74 0a 2a 2a 20 74 68 65 20 5b  t reset.** the [
23310 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
23320 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f 6e  b | bindings] on
23330 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
23340 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 55 73 65 20  tement]..** Use 
23350 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20  this routine to 
23360 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 74 20 70  reset all host p
23370 61 72 61 6d 65 74 65 72 73 20 74 6f 20 4e 55 4c  arameters to NUL
23380 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  L..**.** Require
23390 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 36  ments:.** [H1366
233a0 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  1].*/.SQLITE_API
233b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65   int sqlite3_cle
233c0 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69  ar_bindings(sqli
233d0 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a  te3_stmt*);../*.
233e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d  ** CAPI3REF: Num
233f0 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 49  ber Of Columns I
23400 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 20 7b  n A Result Set {
23410 48 31 33 37 31 30 7d 20 3c 53 31 30 37 30 30 3e  H13710} <S10700>
23420 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
23430 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
23440 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  mns in the resul
23450 74 20 73 65 74 20 72 65 74 75 72 6e 65 64 20 62  t set returned b
23460 79 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72  y the.** [prepar
23470 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 54  ed statement]. T
23480 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
23490 72 6e 73 20 30 20 69 66 20 70 53 74 6d 74 20 69  rns 0 if pStmt i
234a0 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 74 61 74  s an SQL.** stat
234b0 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73 20  ement that does 
234c0 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 61 20  not return data 
234d0 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e 20  (for example an 
234e0 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a  [UPDATE])..**.**
234f0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
23500 2a 20 5b 48 31 33 37 31 31 5d 0a 2a 2f 0a 53 51  * [H13711].*/.SQ
23510 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
23520 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
23530 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
23540 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  pStmt);../*.** C
23550 41 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20  API3REF: Column 
23560 4e 61 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c  Names In A Resul
23570 74 20 53 65 74 20 7b 48 31 33 37 32 30 7d 20 3c  t Set {H13720} <
23580 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S10700>.**.** Th
23590 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74  ese routines ret
235a0 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 73  urn the name ass
235b0 69 67 6e 65 64 20 74 6f 20 61 20 70 61 72 74 69  igned to a parti
235c0 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  cular column.** 
235d0 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
235e0 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20  t of a [SELECT] 
235f0 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20  statement.  The 
23600 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
23610 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61  ame().** interfa
23620 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
23630 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74  nter to a zero-t
23640 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20  erminated UTF-8 
23650 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71  string.** and sq
23660 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
23670 65 31 36 28 29 20 72 65 74 75 72 6e 73 20 61 20  e16() returns a 
23680 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72  pointer to a zer
23690 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20  o-terminated.** 
236a0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20  UTF-16 string.  
236b0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
236c0 74 65 72 20 69 73 20 74 68 65 20 5b 70 72 65 70  ter is the [prep
236d0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a  ared statement].
236e0 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ** that implemen
236f0 74 73 20 74 68 65 20 5b 53 45 4c 45 43 54 5d 20  ts the [SELECT] 
23700 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73  statement. The s
23710 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
23720 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e  is the.** column
23730 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65   number.  The le
23740 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73  ftmost column is
23750 20 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a   number 0..**.**
23760 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   The returned st
23770 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20  ring pointer is 
23780 76 61 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68  valid until eith
23790 65 72 20 74 68 65 20 5b 70 72 65 70 61 72 65 64  er the [prepared
237a0 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69   statement].** i
237b0 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b  s destroyed by [
237c0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
237d0 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65  ()] or until the
237e0 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a   next call to.**
237f0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
23800 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65  name() or sqlite
23810 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28  3_column_name16(
23820 29 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f  ) on the same co
23830 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  lumn..**.** If s
23840 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
23850 66 61 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65  fails during the
23860 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65   processing of e
23870 69 74 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a  ither routine.**
23880 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75   (for example du
23890 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f  ring a conversio
238a0 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20  n from UTF-8 to 
238b0 55 54 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a  UTF-16) then a.*
238c0 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69  * NULL pointer i
238d0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
238e0 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  * The name of a 
238f0 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73  result column is
23900 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
23910 65 20 22 41 53 22 20 63 6c 61 75 73 65 20 66 6f  e "AS" clause fo
23920 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e  r.** that column
23930 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  , if there is an
23940 20 41 53 20 63 6c 61 75 73 65 2e 20 20 49 66 20   AS clause.  If 
23950 74 68 65 72 65 20 69 73 20 6e 6f 20 41 53 20 63  there is no AS c
23960 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68  lause.** then th
23970 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f  e name of the co
23980 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 66  lumn is unspecif
23990 69 65 64 20 61 6e 64 20 6d 61 79 20 63 68 61 6e  ied and may chan
239a0 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72  ge from.** one r
239b0 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65  elease of SQLite
239c0 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a   to the next..**
239d0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
239e0 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 5d 20 5b 48  :.** [H13721] [H
239f0 31 33 37 32 33 5d 20 5b 48 31 33 37 32 34 5d 20  13723] [H13724] 
23a00 5b 48 31 33 37 32 35 5d 20 5b 48 31 33 37 32 36  [H13725] [H13726
23a10 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 2f 0a 53 51  ] [H13727].*/.SQ
23a20 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
23a30 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
23a40 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33  umn_name(sqlite3
23a50 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a  _stmt*, int N);.
23a60 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
23a70 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
23a80 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c  olumn_name16(sql
23a90 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
23aa0 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  N);../*.** CAPI3
23ab0 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 20 44  REF: Source Of D
23ac0 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 20 52  ata In A Query R
23ad0 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d 20 3c  esult {H13740} <
23ae0 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S10700>.**.** Th
23af0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72 6f  ese routines pro
23b00 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f 20  vide a means to 
23b10 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 63  determine what c
23b20 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a 2a  olumn of what.**
23b30 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 20   table in which 
23b40 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75 6c  database a resul
23b50 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20  t of a [SELECT] 
23b60 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20  statement comes 
23b70 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 6d  from..** The nam
23b80 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
23b90 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 63 6f  e or table or co
23ba0 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 74 75  lumn can be retu
23bb0 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 68 65  rned as.** eithe
23bc0 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 54 46  r a UTF-8 or UTF
23bd0 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 65  -16 string.  The
23be0 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f 75 74   _database_ rout
23bf0 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a 20 74  ines return.** t
23c00 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
23c10 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 72 6f  , the _table_ ro
23c20 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68  utines return th
23c30 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 61 6e  e table name, an
23c40 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 5f  d.** the origin_
23c50 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
23c60 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
23c70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  ..** The returne
23c80 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 6c 69  d string is vali
23c90 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 72 65  d until the [pre
23ca0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
23cb0 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a   is destroyed.**
23cc0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
23cd0 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 75  finalize()] or u
23ce0 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69 6e  ntil the same in
23cf0 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 71  formation is req
23d00 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e 20  uested.** again 
23d10 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 65  in a different e
23d20 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  ncoding..**.** T
23d30 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 6e 65  he names returne
23d40 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 69 6e  d are the origin
23d50 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e 61  al un-aliased na
23d60 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  mes of the.** da
23d70 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c 20 61  tabase, table, a
23d80 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a  nd column..**.**
23d90 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
23da0 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  ent to the follo
23db0 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 61 20  wing calls is a 
23dc0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
23dd0 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 66  ent]..** These f
23de0 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20  unctions return 
23df0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
23e00 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  t the Nth column
23e10 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20   returned by.** 
23e20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 77  the statement, w
23e30 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65  here N is the se
23e40 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 72  cond function ar
23e50 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  gument..**.** If
23e60 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
23e70 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
23e80 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e 20  statement is an 
23e90 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a 2a 2a  expression or.**
23ea0 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69 73   subquery and is
23eb0 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76 61   not a column va
23ec0 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f 66  lue, then all of
23ed0 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   these functions
23ee0 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 2e   return.** NULL.
23ef0 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20    These routine 
23f00 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75 72  might also retur
23f10 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f  n NULL if a memo
23f20 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
23f30 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20  ror.** occurs.  
23f40 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79 20  Otherwise, they 
23f50 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  return the name 
23f60 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64 20  of the attached 
23f70 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 0a  database, table.
23f80 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 68  ** and column th
23f90 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74 20  at query result 
23fa0 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 72 61  column was extra
23fb0 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a  cted from..**.**
23fc0 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f 74 68   As with all oth
23fd0 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 2c 20  er SQLite APIs, 
23fe0 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 64 20  those postfixed 
23ff0 77 69 74 68 20 22 31 36 22 20 72 65 74 75 72 6e  with "16" return
24000 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  .** UTF-16 encod
24010 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 65 20  ed strings, the 
24020 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  other functions 
24030 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 7b 45  return UTF-8. {E
24040 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ND}.**.** These 
24050 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61 76  APIs are only av
24060 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c  ailable if the l
24070 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69  ibrary was compi
24080 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20  led with the.** 
24090 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43  [SQLITE_ENABLE_C
240a0 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d 20  OLUMN_METADATA] 
240b0 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73  C-preprocessor s
240c0 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a  ymbol defined..*
240d0 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a 2a 2a  *.** {A13751}.**
240e0 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   If two or more 
240f0 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65  threads call one
24100 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 73   or more of thes
24110 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69 6e  e routines again
24120 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70  st the same.** p
24130 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
24140 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 20  t and column at 
24150 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 68  the same time th
24160 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  en the results a
24170 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e  re.** undefined.
24180 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
24190 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 34 31 5d  nts:.** [H13741]
241a0 20 5b 48 31 33 37 34 32 5d 20 5b 48 31 33 37 34   [H13742] [H1374
241b0 33 5d 20 5b 48 31 33 37 34 34 5d 20 5b 48 31 33  3] [H13744] [H13
241c0 37 34 35 5d 20 5b 48 31 33 37 34 36 5d 20 5b 48  745] [H13746] [H
241d0 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a 20 49 66 20  13748].**.** If 
241e0 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65  two or more thre
241f0 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20  ads call one or 
24200 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  more.** [sqlite3
24210 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
24220 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e 20 6d  _name | column m
24230 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 61 63  etadata interfac
24240 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73  es].** for the s
24250 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  ame [prepared st
24260 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 72 65 73  atement] and res
24270 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 74  ult column.** at
24280 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74   the same time t
24290 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20  hen the results 
242a0 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a  are undefined..*
242b0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
242c0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
242d0 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
242e0 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
242f0 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  mt*,int);.SQLITE
24300 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
24310 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
24320 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28  database_name16(
24330 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
24340 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  t);.SQLITE_API c
24350 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
24360 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
24370 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
24380 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
24390 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
243a0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
243b0 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69  able_name16(sqli
243c0 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
243d0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
243e0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
243f0 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
24400 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
24410 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
24420 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
24430 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
24440 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65  in_name16(sqlite
24450 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f  3_stmt*,int);../
24460 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
24470 65 63 6c 61 72 65 64 20 44 61 74 61 74 79 70 65  eclared Datatype
24480 20 4f 66 20 41 20 51 75 65 72 79 20 52 65 73 75   Of A Query Resu
24490 6c 74 20 7b 48 31 33 37 36 30 7d 20 3c 53 31 30  lt {H13760} <S10
244a0 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  700>.**.** The f
244b0 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69  irst parameter i
244c0 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  s a [prepared st
244d0 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20  atement]..** If 
244e0 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69  this statement i
244f0 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61  s a [SELECT] sta
24500 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 4e  tement and the N
24510 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
24520 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72 65 73  .** returned res
24530 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74 20  ult set of that 
24540 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 20 74 61  [SELECT] is a ta
24550 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74 20  ble column (not 
24560 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  an.** expression
24570 20 6f 72 20 73 75 62 71 75 65 72 79 29 20 74 68   or subquery) th
24580 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65 64 20  en the declared 
24590 74 79 70 65 20 6f 66 20 74 68 65 20 74 61 62 6c  type of the tabl
245a0 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 72  e.** column is r
245b0 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65  eturned.  If the
245c0 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74   Nth column of t
245d0 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 73  he result set is
245e0 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f   an.** expressio
245f0 6e 20 6f 72 20 73 75 62 71 75 65 72 79 2c 20 74  n or subquery, t
24600 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  hen a NULL point
24610 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  er is returned..
24620 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
24630 73 74 72 69 6e 67 20 69 73 20 61 6c 77 61 79 73  string is always
24640 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e 20   UTF-8 encoded. 
24650 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  {END}.**.** For 
24660 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74  example, given t
24670 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
24680 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45  ma:.**.** CREATE
24690 20 54 41 42 4c 45 20 74 31 28 63 31 20 56 41 52   TABLE t1(c1 VAR
246a0 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64  IANT);.**.** and
246b0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
246c0 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63  tatement to be c
246d0 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 53  ompiled:.**.** S
246e0 45 4c 45 43 54 20 63 31 20 2b 20 31 2c 20 63 31  ELECT c1 + 1, c1
246f0 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 20   FROM t1;.**.** 
24700 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 75  this routine wou
24710 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 74  ld return the st
24720 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22 20 66  ring "VARIANT" f
24730 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 72 65  or the second re
24740 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 28  sult.** column (
24750 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e 55 4c  i==1), and a NUL
24760 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68  L pointer for th
24770 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20 63  e first result c
24780 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e 0a 2a 2a  olumn (i==0)..**
24790 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20  .** SQLite uses 
247a0 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 69 6d 65  dynamic run-time
247b0 20 74 79 70 69 6e 67 2e 20 20 53 6f 20 6a 75 73   typing.  So jus
247c0 74 20 62 65 63 61 75 73 65 20 61 20 63 6f 6c 75  t because a colu
247d0 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c 61 72 65  mn.** is declare
247e0 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 70  d to contain a p
247f0 61 72 74 69 63 75 6c 61 72 20 74 79 70 65 20 64  articular type d
24800 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61  oes not mean tha
24810 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 20 73 74  t the.** data st
24820 6f 72 65 64 20 69 6e 20 74 68 61 74 20 63 6f 6c  ored in that col
24830 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 20 64 65  umn is of the de
24840 63 6c 61 72 65 64 20 74 79 70 65 2e 20 20 53 51  clared type.  SQ
24850 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74 72 6f 6e  Lite is.** stron
24860 67 6c 79 20 74 79 70 65 64 2c 20 62 75 74 20 74  gly typed, but t
24870 68 65 20 74 79 70 69 6e 67 20 69 73 20 64 79 6e  he typing is dyn
24880 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 69 63 2e  amic not static.
24890 20 20 54 79 70 65 0a 2a 2a 20 69 73 20 61 73 73    Type.** is ass
248a0 6f 63 69 61 74 65 64 20 77 69 74 68 20 69 6e 64  ociated with ind
248b0 69 76 69 64 75 61 6c 20 76 61 6c 75 65 73 2c 20  ividual values, 
248c0 6e 6f 74 20 77 69 74 68 20 74 68 65 20 63 6f 6e  not with the con
248d0 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73 65 64 20  tainers.** used 
248e0 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65 20 76 61  to hold those va
248f0 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  lues..**.** Requ
24900 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
24910 33 37 36 31 5d 20 5b 48 31 33 37 36 32 5d 20 5b  3761] [H13762] [
24920 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54  H13763].*/.SQLIT
24930 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
24940 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
24950 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65  _decltype(sqlite
24960 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51  3_stmt*,int);.SQ
24970 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
24980 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
24990 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73  umn_decltype16(s
249a0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
249b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
249c0 45 46 3a 20 45 76 61 6c 75 61 74 65 20 41 6e 20  EF: Evaluate An 
249d0 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b 48  SQL Statement {H
249e0 31 33 32 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a  13200} <S10000>.
249f0 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 5b 70  **.** After a [p
24a00 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
24a10 74 5d 20 68 61 73 20 62 65 65 6e 20 70 72 65 70  t] has been prep
24a20 61 72 65 64 20 75 73 69 6e 67 20 65 69 74 68 65  ared using eithe
24a30 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72  r.** [sqlite3_pr
24a40 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b  epare_v2()] or [
24a50 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
24a60 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f  6_v2()] or one o
24a70 66 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20  f the legacy.** 
24a80 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69  interfaces [sqli
24a90 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f  te3_prepare()] o
24aa0 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  r [sqlite3_prepa
24ab0 72 65 31 36 28 29 5d 2c 20 74 68 69 73 20 66 75  re16()], this fu
24ac0 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62  nction.** must b
24ad0 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20  e called one or 
24ae0 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 65 76  more times to ev
24af0 61 6c 75 61 74 65 20 74 68 65 20 73 74 61 74 65  aluate the state
24b00 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
24b10 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 62  details of the b
24b20 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20 73  ehavior of the s
24b30 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 6e  qlite3_step() in
24b40 74 65 72 66 61 63 65 20 64 65 70 65 6e 64 0a 2a  terface depend.*
24b50 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68 65  * on whether the
24b60 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70   statement was p
24b70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 74 68  repared using th
24b80 65 20 6e 65 77 65 72 20 22 76 32 22 20 69 6e 74  e newer "v2" int
24b90 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c 69 74  erface.** [sqlit
24ba0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
24bb0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72   and [sqlite3_pr
24bc0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f 72  epare16_v2()] or
24bd0 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 61 63   the older legac
24be0 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 5b  y.** interface [
24bf0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
24c00 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
24c10 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 20 54  prepare16()].  T
24c20 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a  he use of the.**
24c30 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65 72 66   new "v2" interf
24c40 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64  ace is recommend
24c50 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70 6c 69  ed for new appli
24c60 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68 65 20  cations but the 
24c70 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66  legacy.** interf
24c80 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75  ace will continu
24c90 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65  e to be supporte
24ca0 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  d..**.** In the 
24cb0 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65  legacy interface
24cc0 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
24cd0 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74 68 65  ue will be eithe
24ce0 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c  r [SQLITE_BUSY],
24cf0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45  .** [SQLITE_DONE
24d00 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c  ], [SQLITE_ROW],
24d10 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c   [SQLITE_ERROR],
24d20 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55   or [SQLITE_MISU
24d30 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65  SE]..** With the
24d40 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 2c   "v2" interface,
24d50 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65   any of the othe
24d60 72 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 73 5d  r [result codes]
24d70 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64   or.** [extended
24d80 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 6d   result codes] m
24d90 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 64  ight be returned
24da0 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20   as well..**.** 
24db0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6d 65  [SQLITE_BUSY] me
24dc0 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 61 74  ans that the dat
24dd0 61 62 61 73 65 20 65 6e 67 69 6e 65 20 77 61 73  abase engine was
24de0 20 75 6e 61 62 6c 65 20 74 6f 20 61 63 71 75 69   unable to acqui
24df0 72 65 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  re the.** databa
24e00 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e 65 65 64  se locks it need
24e10 73 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f 62 2e  s to do its job.
24e20 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65    If the stateme
24e30 6e 74 20 69 73 20 61 20 5b 43 4f 4d 4d 49 54 5d  nt is a [COMMIT]
24e40 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20 6f 75  .** or occurs ou
24e50 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78 70 6c  tside of an expl
24e60 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  icit transaction
24e70 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e 20 72  , then you can r
24e80 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 74 61 74  etry the.** stat
24e90 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 73  ement.  If the s
24ea0 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20  tatement is not 
24eb0 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e 64 20 6f  a [COMMIT] and o
24ec0 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 0a 2a  ccurs within a.*
24ed0 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73  * explicit trans
24ee0 61 63 74 69 6f 6e 20 74 68 65 6e 20 79 6f 75 20  action then you 
24ef0 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61 63 6b 20  should rollback 
24f00 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
24f10 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e  before.** contin
24f20 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c  uing..**.** [SQL
24f30 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61 6e 73 20  ITE_DONE] means 
24f40 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d 65  that the stateme
24f50 6e 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  nt has finished 
24f60 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 73 75 63  executing.** suc
24f70 63 65 73 73 66 75 6c 6c 79 2e 20 20 73 71 6c 69  cessfully.  sqli
24f80 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c  te3_step() shoul
24f90 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  d not be called 
24fa0 61 67 61 69 6e 20 6f 6e 20 74 68 69 73 20 76 69  again on this vi
24fb0 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65  rtual.** machine
24fc0 20 77 69 74 68 6f 75 74 20 66 69 72 73 74 20 63   without first c
24fd0 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  alling [sqlite3_
24fe0 72 65 73 65 74 28 29 5d 20 74 6f 20 72 65 73 65  reset()] to rese
24ff0 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a  t the virtual.**
25000 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 20 74 6f   machine back to
25010 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61   its initial sta
25020 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  te..**.** If the
25030 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62   SQL statement b
25040 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 72 65  eing executed re
25050 74 75 72 6e 73 20 61 6e 79 20 64 61 74 61 2c 20  turns any data, 
25060 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57  then [SQLITE_ROW
25070 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  ].** is returned
25080 20 65 61 63 68 20 74 69 6d 65 20 61 20 6e 65 77   each time a new
25090 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20   row of data is 
250a0 72 65 61 64 79 20 66 6f 72 20 70 72 6f 63 65 73  ready for proces
250b0 73 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20 63  sing by the.** c
250c0 61 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c 75 65  aller. The value
250d0 73 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65  s may be accesse
250e0 64 20 75 73 69 6e 67 20 74 68 65 20 5b 63 6f 6c  d using the [col
250f0 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74  umn access funct
25100 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c 69 74 65  ions]..** sqlite
25110 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c 6c  3_step() is call
25120 65 64 20 61 67 61 69 6e 20 74 6f 20 72 65 74 72  ed again to retr
25130 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 72 6f  ieve the next ro
25140 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a  w of data..**.**
25150 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
25160 6d 65 61 6e 73 20 74 68 61 74 20 61 20 72 75 6e  means that a run
25170 2d 74 69 6d 65 20 65 72 72 6f 72 20 28 73 75 63  -time error (suc
25180 68 20 61 73 20 61 20 63 6f 6e 73 74 72 61 69 6e  h as a constrain
25190 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 29 20  t.** violation) 
251a0 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 73  has occurred.  s
251b0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73 68  qlite3_step() sh
251c0 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c  ould not be call
251d0 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a 2a 20 74  ed again on.** t
251e0 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69 6e 66 6f  he VM. More info
251f0 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 66  rmation may be f
25200 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69 6e 67 20  ound by calling 
25210 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  [sqlite3_errmsg(
25220 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 20  )]..** With the 
25230 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65  legacy interface
25240 2c 20 61 20 6d 6f 72 65 20 73 70 65 63 69 66 69  , a more specifi
25250 63 20 65 72 72 6f 72 20 63 6f 64 65 20 28 66 6f  c error code (fo
25260 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 5b 53  r example,.** [S
25270 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d  QLITE_INTERRUPT]
25280 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  , [SQLITE_SCHEMA
25290 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 52 52 55  ], [SQLITE_CORRU
252a0 50 54 5d 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74  PT], and so fort
252b0 68 29 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 62 74  h).** can be obt
252c0 61 69 6e 65 64 20 62 79 20 63 61 6c 6c 69 6e 67  ained by calling
252d0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
252e0 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a 20 5b 70 72  )] on the.** [pr
252f0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
25300 5d 2e 20 20 49 6e 20 74 68 65 20 22 76 32 22 20  ].  In the "v2" 
25310 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 74 68  interface,.** th
25320 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 20  e more specific 
25330 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
25340 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 20  turned directly 
25350 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  by sqlite3_step(
25360 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45  )..**.** [SQLITE
25370 5f 4d 49 53 55 53 45 5d 20 6d 65 61 6e 73 20 74  _MISUSE] means t
25380 68 61 74 20 74 68 65 20 74 68 69 73 20 72 6f 75  hat the this rou
25390 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 20  tine was called 
253a0 69 6e 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  inappropriately.
253b0 0a 2a 2a 20 50 65 72 68 61 70 73 20 69 74 20 77  .** Perhaps it w
253c0 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 5b  as called on a [
253d0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
253e0 6e 74 5d 20 74 68 61 74 20 68 61 73 0a 2a 2a 20  nt] that has.** 
253f0 61 6c 72 65 61 64 79 20 62 65 65 6e 20 5b 73 71  already been [sq
25400 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c  lite3_finalize |
25410 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f 72 20 6f   finalized] or o
25420 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 64 0a 2a  n one that had.*
25430 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 74  * previously ret
25440 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 45 52  urned [SQLITE_ER
25450 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ROR] or [SQLITE_
25460 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74 20 63 6f  DONE].  Or it co
25470 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65 20 63 61  uld.** be the ca
25480 73 65 20 74 68 61 74 20 74 68 65 20 73 61 6d 65  se that the same
25490 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
254a0 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 75 73  tion is being us
254b0 65 64 20 62 79 20 74 77 6f 20 6f 72 0a 2a 2a 20  ed by two or.** 
254c0 6d 6f 72 65 20 74 68 72 65 61 64 73 20 61 74 20  more threads at 
254d0 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65 6e 74 20  the same moment 
254e0 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c  in time..**.** <
254f0 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 66 61 63  b>Goofy Interfac
25500 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 49 6e 20  e Alert:</b> In 
25510 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72  the legacy inter
25520 66 61 63 65 2c 20 74 68 65 20 73 71 6c 69 74 65  face, the sqlite
25530 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 20  3_step().** API 
25540 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 61  always returns a
25550 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72 20 63   generic error c
25560 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52  ode, [SQLITE_ERR
25570 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  OR], following a
25580 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f 74 68 65  ny.** error othe
25590 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 42  r than [SQLITE_B
255a0 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45  USY] and [SQLITE
255b0 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f 75 20 6d  _MISUSE].  You m
255c0 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b 73 71 6c  ust call.** [sql
255d0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72  ite3_reset()] or
255e0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
255f0 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 72 20 74  ze()] in order t
25600 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 20 74 68  o find one of th
25610 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 5b 65  e.** specific [e
25620 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 68 61 74  rror codes] that
25630 20 62 65 74 74 65 72 20 64 65 73 63 72 69 62 65   better describe
25640 73 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 20  s the error..** 
25650 57 65 20 61 64 6d 69 74 20 74 68 61 74 20 74 68  We admit that th
25660 69 73 20 69 73 20 61 20 67 6f 6f 66 79 20 64 65  is is a goofy de
25670 73 69 67 6e 2e 20 20 54 68 65 20 70 72 6f 62 6c  sign.  The probl
25680 65 6d 20 68 61 73 20 62 65 65 6e 20 66 69 78 65  em has been fixe
25690 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 22 76  d.** with the "v
256a0 32 22 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49  2" interface.  I
256b0 66 20 79 6f 75 20 70 72 65 70 61 72 65 20 61 6c  f you prepare al
256c0 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c 20 73 74  l of your SQL st
256d0 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 73 69 6e  atements.** usin
256e0 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74 65  g either [sqlite
256f0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
25700 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  or [sqlite3_prep
25710 61 72 65 31 36 5f 76 32 28 29 5d 20 69 6e 73 74  are16_v2()] inst
25720 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 65  ead.** of the le
25730 67 61 63 79 20 5b 73 71 6c 69 74 65 33 5f 70 72  gacy [sqlite3_pr
25740 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 71  epare()] and [sq
25750 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28  lite3_prepare16(
25760 29 5d 20 69 6e 74 65 72 66 61 63 65 73 2c 0a 2a  )] interfaces,.*
25770 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72 65 20  * then the more 
25780 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20  specific [error 
25790 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 75 72  codes] are retur
257a0 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20  ned directly.** 
257b0 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  by sqlite3_step(
257c0 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 74  ).  The use of t
257d0 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63  he "v2" interfac
257e0 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  e is recommended
257f0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
25800 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 32 30 32  ents:.** [H13202
25810 5d 20 5b 48 31 35 33 30 34 5d 20 5b 48 31 35 33  ] [H15304] [H153
25820 30 36 5d 20 5b 48 31 35 33 30 38 5d 20 5b 48 31  06] [H15308] [H1
25830 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  5310].*/.SQLITE_
25840 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
25850 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d  step(sqlite3_stm
25860 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  t*);../*.** CAPI
25870 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20  3REF: Number of 
25880 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65 73  columns in a res
25890 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30 7d  ult set {H13770}
258a0 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
258b0 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  Returns the numb
258c0 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e 20  er of values in 
258d0 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
258e0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
258f0 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  t..**.** Require
25900 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 37  ments:.** [H1377
25910 31 5d 20 5b 48 31 33 37 37 32 5d 0a 2a 2f 0a 53  1] [H13772].*/.S
25920 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
25930 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74  lite3_data_count
25940 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
25950 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Stmt);../*.** CA
25960 50 49 33 52 45 46 3a 20 46 75 6e 64 61 6d 65 6e  PI3REF: Fundamen
25970 74 61 6c 20 44 61 74 61 74 79 70 65 73 20 7b 48  tal Datatypes {H
25980 31 30 32 36 35 7d 20 3c 53 31 30 31 31 30 3e 3c  10265} <S10110><
25990 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S10120>.** KEYWO
259a0 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 45 58 54  RDS: SQLITE_TEXT
259b0 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 36 36 7d 20  .**.** {H10266} 
259c0 45 76 65 72 79 20 76 61 6c 75 65 20 69 6e 20 53  Every value in S
259d0 51 4c 69 74 65 20 68 61 73 20 6f 6e 65 20 6f 66  QLite has one of
259e0 20 66 69 76 65 20 66 75 6e 64 61 6d 65 6e 74 61   five fundamenta
259f0 6c 20 64 61 74 61 74 79 70 65 73 3a 0a 2a 2a 0a  l datatypes:.**.
25a00 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
25a10 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
25a20 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 3e 20 36 34  teger.** <li> 64
25a30 2d 62 69 74 20 49 45 45 45 20 66 6c 6f 61 74 69  -bit IEEE floati
25a40 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 0a  ng point number.
25a50 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 6e 67 0a 2a  ** <li> string.*
25a60 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a 2a 2a 20 3c  * <li> BLOB.** <
25a70 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c  li> NULL.** </ul
25a80 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68  > {END}.**.** Th
25a90 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  ese constants ar
25aa0 65 20 63 6f 64 65 73 20 66 6f 72 20 65 61 63 68  e codes for each
25ab0 20 6f 66 20 74 68 6f 73 65 20 74 79 70 65 73 2e   of those types.
25ac0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
25ad0 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 58 54   the SQLITE_TEXT
25ae0 20 63 6f 6e 73 74 61 6e 74 20 77 61 73 20 61 6c   constant was al
25af0 73 6f 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  so used in SQLit
25b00 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a 2a 20 66  e version 2.** f
25b10 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 6c 79 20  or a completely 
25b20 64 69 66 66 65 72 65 6e 74 20 6d 65 61 6e 69 6e  different meanin
25b30 67 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 61  g.  Software tha
25b40 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20  t links against 
25b50 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74 65 20 76  both.** SQLite v
25b60 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 53 51 4c  ersion 2 and SQL
25b70 69 74 65 20 76 65 72 73 69 6f 6e 20 33 20 73 68  ite version 3 sh
25b80 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 54 45 33  ould use SQLITE3
25b90 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a 2a 20 53 51  _TEXT, not.** SQ
25ba0 4c 49 54 45 5f 54 45 58 54 2e 0a 2a 2f 0a 23 64  LITE_TEXT..*/.#d
25bb0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54  efine SQLITE_INT
25bc0 45 47 45 52 20 20 31 0a 23 64 65 66 69 6e 65 20  EGER  1.#define 
25bd0 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 20 20 20  SQLITE_FLOAT    
25be0 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
25bf0 5f 42 4c 4f 42 20 20 20 20 20 34 0a 23 64 65 66  _BLOB     4.#def
25c00 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ine SQLITE_NULL 
25c10 20 20 20 20 35 0a 23 69 66 64 65 66 20 53 51 4c      5.#ifdef SQL
25c20 49 54 45 5f 54 45 58 54 0a 23 20 75 6e 64 65 66  ITE_TEXT.# undef
25c30 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 65 6c   SQLITE_TEXT.#el
25c40 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  se.# define SQLI
25c50 54 45 5f 54 45 58 54 20 20 20 20 20 33 0a 23 65  TE_TEXT     3.#e
25c60 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 53 51 4c  ndif.#define SQL
25c70 49 54 45 33 5f 54 45 58 54 20 20 20 20 20 33 0a  ITE3_TEXT     3.
25c80 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
25c90 20 52 65 73 75 6c 74 20 56 61 6c 75 65 73 20 46   Result Values F
25ca0 72 6f 6d 20 41 20 51 75 65 72 79 20 7b 48 31 33  rom A Query {H13
25cb0 38 30 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a  800} <S10700>.**
25cc0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63 6f 6c 75   KEYWORDS: {colu
25cd0 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 69  mn access functi
25ce0 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ons}.**.** These
25cf0 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 6d 20 74   routines form t
25d00 68 65 20 22 72 65 73 75 6c 74 20 73 65 74 20 71  he "result set q
25d10 75 65 72 79 22 20 69 6e 74 65 72 66 61 63 65 2e  uery" interface.
25d20 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
25d30 74 69 6e 65 73 20 72 65 74 75 72 6e 20 69 6e 66  tines return inf
25d40 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
25d50 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f   single column o
25d60 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a  f the current.**
25d70 20 72 65 73 75 6c 74 20 72 6f 77 20 6f 66 20 61   result row of a
25d80 20 71 75 65 72 79 2e 20 20 49 6e 20 65 76 65 72   query.  In ever
25d90 79 20 63 61 73 65 20 74 68 65 20 66 69 72 73 74  y case the first
25da0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
25db0 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ointer.** to the
25dc0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
25dd0 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20 62 65  ment] that is be
25de0 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 28 74  ing evaluated (t
25df0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74  he [sqlite3_stmt
25e00 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 72  *].** that was r
25e10 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73 71  eturned from [sq
25e20 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
25e30 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74  ()] or one of it
25e40 73 20 76 61 72 69 61 6e 74 73 29 0a 2a 2a 20 61  s variants).** a
25e50 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  nd the second ar
25e60 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e  gument is the in
25e70 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  dex of the colum
25e80 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66 6f  n for which info
25e90 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c  rmation.** shoul
25ea0 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20  d be returned.  
25eb0 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c  The leftmost col
25ec0 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c  umn of the resul
25ed0 74 20 73 65 74 20 68 61 73 20 74 68 65 20 69 6e  t set has the in
25ee0 64 65 78 20 30 2e 0a 2a 2a 20 54 68 65 20 6e 75  dex 0..** The nu
25ef0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
25f00 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 63 61  in the result ca
25f10 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65 64 20  n be determined 
25f20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65  using.** [sqlite
25f30 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29  3_column_count()
25f40 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  ]..**.** If the 
25f50 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64 6f  SQL statement do
25f60 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  es not currently
25f70 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69   point to a vali
25f80 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68 65  d row, or if the
25f90 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  .** column index
25fa0 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
25fb0 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
25fc0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 68  undefined..** Th
25fd0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61 79  ese routines may
25fe0 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20   only be called 
25ff0 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72 65  when the most re
26000 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  cent call to.** 
26010 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
26020 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53   has returned [S
26030 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20 6e  QLITE_ROW] and n
26040 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74  either.** [sqlit
26050 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72 20  e3_reset()] nor 
26060 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
26070 65 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20 63  e()] have been c
26080 61 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e 74  alled subsequent
26090 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66  ly..** If any of
260a0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
260b0 61 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72  are called after
260c0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
260d0 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65  )] or.** [sqlite
260e0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72  3_finalize()] or
260f0 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33 5f   after [sqlite3_
26100 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 75  step()] has retu
26110 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e  rned.** somethin
26120 67 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51  g other than [SQ
26130 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 72  LITE_ROW], the r
26140 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66  esults are undef
26150 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c  ined..** If [sql
26160 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20  ite3_step()] or 
26170 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
26180 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69  ] or [sqlite3_fi
26190 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 65  nalize()].** are
261a0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20 64   called from a d
261b0 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 20  ifferent thread 
261c0 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68 65  while any of the
261d0 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61  se routines.** a
261e0 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65 6e  re pending, then
261f0 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65   the results are
26200 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a   undefined..**.*
26210 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  * The sqlite3_co
26220 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75 74  lumn_type() rout
26230 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a  ine returns the.
26240 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47  ** [SQLITE_INTEG
26250 45 52 20 7c 20 64 61 74 61 74 79 70 65 20 63 6f  ER | datatype co
26260 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e 69 74  de] for the init
26270 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a 2a 2a  ial data type.**
26280 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 63   of the result c
26290 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 74 75  olumn.  The retu
262a0 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6f 6e  rned value is on
262b0 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 54  e of [SQLITE_INT
262c0 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54  EGER],.** [SQLIT
262d0 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 54  E_FLOAT], [SQLIT
262e0 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 54 45  E_TEXT], [SQLITE
262f0 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c 49  _BLOB], or [SQLI
26300 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 76  TE_NULL].  The v
26310 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64  alue.** returned
26320 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   by sqlite3_colu
26330 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 6f 6e 6c  mn_type() is onl
26340 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20  y meaningful if 
26350 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 65  no type.** conve
26360 72 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 63 75  rsions have occu
26370 72 72 65 64 20 61 73 20 64 65 73 63 72 69 62 65  rred as describe
26380 64 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 72 20  d below.  After 
26390 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f  a type conversio
263a0 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  n,.** the value 
263b0 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
263c0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
263d0 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20  ) is undefined. 
263e0 20 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73 69   Future.** versi
263f0 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 61  ons of SQLite ma
26400 79 20 63 68 61 6e 67 65 20 74 68 65 20 62 65 68  y change the beh
26410 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33  avior of sqlite3
26420 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 2a  _column_type().*
26430 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 79  * following a ty
26440 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a  pe conversion..*
26450 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75  *.** If the resu
26460 6c 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72 20  lt is a BLOB or 
26470 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68 65  UTF-8 string the
26480 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  n the sqlite3_co
26490 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20  lumn_bytes().** 
264a0 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
264b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
264c0 74 65 73 20 69 6e 20 74 68 61 74 20 42 4c 4f 42  tes in that BLOB
264d0 20 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 49   or string..** I
264e0 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  f the result is 
264f0 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2c  a UTF-16 string,
26500 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f   then sqlite3_co
26510 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f 6e  lumn_bytes() con
26520 76 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 74 72  verts.** the str
26530 69 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 6e 64  ing to UTF-8 and
26540 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68   then returns th
26550 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
26560 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73  s..** If the res
26570 75 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69 63  ult is a numeric
26580 20 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c 69   value then sqli
26590 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
265a0 28 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c 69  () uses.** [sqli
265b0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20  te3_snprintf()] 
265c0 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20  to convert that 
265d0 76 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d 38  value to a UTF-8
265e0 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75   string and retu
265f0 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  rns.** the numbe
26600 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
26610 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 68  at string..** Th
26620 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
26630 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
26640 65 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69  e the zero termi
26650 6e 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e 64  nator at the end
26660 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69 6e  .** of the strin
26670 67 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 79 3a  g.  For clarity:
26680 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
26690 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ned is the numbe
266a0 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e  r of.** bytes in
266b0 20 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74   the string, not
266c0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
266d0 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a  haracters..**.**
266e0 20 53 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65   Strings returne
266f0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c  d by sqlite3_col
26700 75 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 73  umn_text() and s
26710 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
26720 78 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20  xt16(),.** even 
26730 65 6d 70 74 79 20 73 74 72 69 6e 67 73 2c 20 61  empty strings, a
26740 72 65 20 61 6c 77 61 79 73 20 7a 65 72 6f 20 74  re always zero t
26750 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20  erminated.  The 
26760 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20  return.** value 
26770 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  from sqlite3_col
26780 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20 61  umn_blob() for a
26790 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f   zero-length BLO
267a0 42 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72  B is an arbitrar
267b0 79 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 70 6f  y.** pointer, po
267c0 73 73 69 62 6c 79 20 65 76 65 6e 20 61 20 4e 55  ssibly even a NU
267d0 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
267e0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  * The sqlite3_co
267f0 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 72  lumn_bytes16() r
26800 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61  outine is simila
26810 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  r to sqlite3_col
26820 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 62  umn_bytes().** b
26830 75 74 20 6c 65 61 76 65 73 20 74 68 65 20 72 65  ut leaves the re
26840 73 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 20 69  sult in UTF-16 i
26850 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  n native byte or
26860 64 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 55  der instead of U
26870 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a 65 72  TF-8..** The zer
26880 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73 20  o terminator is 
26890 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  not included in 
268a0 74 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a  this count..**.*
268b0 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 72 65 74  * The object ret
268c0 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65  urned by [sqlite
268d0 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29  3_column_value()
268e0 5d 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72  ] is an.** [unpr
268f0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
26900 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 20 20  value] object.  
26910 41 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 73  An unprotected s
26920 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
26930 65 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20  ect.** may only 
26940 62 65 20 75 73 65 64 20 77 69 74 68 20 5b 73 71  be used with [sq
26950 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65  lite3_bind_value
26960 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
26970 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d  _result_value()]
26980 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 6e 70  ..** If the [unp
26990 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
269a0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 72  _value] object r
269b0 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73  eturned by.** [s
269c0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61  qlite3_column_va
269d0 6c 75 65 28 29 5d 20 69 73 20 75 73 65 64 20 69  lue()] is used i
269e0 6e 20 61 6e 79 20 6f 74 68 65 72 20 77 61 79 2c  n any other way,
269f0 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73   including calls
26a00 0a 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 73 20  .** to routines 
26a10 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 76 61  like [sqlite3_va
26a20 6c 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 6c  lue_int()], [sql
26a30 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
26a40 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74  )],.** or [sqlit
26a50 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29  e3_value_bytes()
26a60 5d 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 61  ], then the beha
26a70 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
26a80 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  d..**.** These r
26a90 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 20  outines attempt 
26aa0 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76  to convert the v
26ab0 61 6c 75 65 20 77 68 65 72 65 20 61 70 70 72 6f  alue where appro
26ac0 70 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a 20  priate.  For.** 
26ad0 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20  example, if the 
26ae0 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65  internal represe
26af0 6e 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41 54  ntation is FLOAT
26b00 20 61 6e 64 20 61 20 74 65 78 74 20 72 65 73 75   and a text resu
26b10 6c 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 73 74  lt.** is request
26b20 65 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70  ed, [sqlite3_snp
26b30 72 69 6e 74 66 28 29 5d 20 69 73 20 75 73 65 64  rintf()] is used
26b40 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 70   internally to p
26b50 65 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 63 6f  erform the.** co
26b60 6e 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61 74  nversion automat
26b70 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 6f 6c  ically.  The fol
26b80 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 74  lowing table det
26b90 61 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72 73  ails the convers
26ba0 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72 65  ions.** that are
26bb0 20 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20   applied:.**.** 
26bc0 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20  <blockquote>.** 
26bd0 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 31  <table border="1
26be0 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 49  ">.** <tr><th> I
26bf0 6e 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 65 20  nternal<br>Type 
26c00 3c 74 68 3e 20 52 65 71 75 65 73 74 65 64 3c 62  <th> Requested<b
26c10 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 6f 6e  r>Type <th>  Con
26c20 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74  version.**.** <t
26c30 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20  r><td>  NULL    
26c40 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c  <td> INTEGER   <
26c50 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 0a  td> Result is 0.
26c60 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c  ** <tr><td>  NUL
26c70 4c 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54  L    <td>  FLOAT
26c80 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20      <td> Result 
26c90 69 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74  is 0.0.** <tr><t
26ca0 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e  d>  NULL    <td>
26cb0 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20     TEXT    <td> 
26cc0 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70  Result is NULL p
26cd0 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74  ointer.** <tr><t
26ce0 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e  d>  NULL    <td>
26cf0 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20     BLOB    <td> 
26d00 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70  Result is NULL p
26d10 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74  ointer.** <tr><t
26d20 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e  d> INTEGER  <td>
26d30 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20    FLOAT    <td> 
26d40 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 6e 74  Convert from int
26d50 65 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a  eger to float.**
26d60 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45   <tr><td> INTEGE
26d70 52 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20  R  <td>   TEXT  
26d80 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e    <td> ASCII ren
26d90 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 69 6e  dering of the in
26da0 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64  teger.** <tr><td
26db0 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20  > INTEGER  <td> 
26dc0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53    BLOB    <td> S
26dd0 61 6d 65 20 61 73 20 49 4e 54 45 47 45 52 2d 3e  ame as INTEGER->
26de0 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  TEXT.** <tr><td>
26df0 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49    FLOAT   <td> I
26e00 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f  NTEGER   <td> Co
26e10 6e 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74  nvert from float
26e20 20 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c   to integer.** <
26e30 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20  tr><td>  FLOAT  
26e40 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20   <td>   TEXT    
26e50 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65  <td> ASCII rende
26e60 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f 61  ring of the floa
26e70 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46  t.** <tr><td>  F
26e80 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42 4c  LOAT   <td>   BL
26e90 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20  OB    <td> Same 
26ea0 61 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a  as FLOAT->TEXT.*
26eb0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54  * <tr><td>  TEXT
26ec0 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52      <td> INTEGER
26ed0 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 69     <td> Use atoi
26ee0 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ().** <tr><td>  
26ef0 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46 4c  TEXT    <td>  FL
26f00 4f 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65 20  OAT    <td> Use 
26f10 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74  atof().** <tr><t
26f20 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  d>  TEXT    <td>
26f30 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20     BLOB    <td> 
26f40 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 72  No change.** <tr
26f50 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c  ><td>  BLOB    <
26f60 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74  td> INTEGER   <t
26f70 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 45  d> Convert to TE
26f80 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f 69  XT then use atoi
26f90 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ().** <tr><td>  
26fa0 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46 4c  BLOB    <td>  FL
26fb0 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76  OAT    <td> Conv
26fc0 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e  ert to TEXT then
26fd0 20 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c   use atof().** <
26fe0 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20  tr><td>  BLOB   
26ff0 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20   <td>   TEXT    
27000 3c 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f 20  <td> Add a zero 
27010 74 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e 65  terminator if ne
27020 65 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e  eded.** </table>
27030 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  .** </blockquote
27040 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c  >.**.** The tabl
27050 65 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 65  e above makes re
27060 66 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e 64  ference to stand
27070 61 72 64 20 43 20 6c 69 62 72 61 72 79 20 66 75  ard C library fu
27080 6e 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a  nctions atoi().*
27090 2a 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 53  * and atof().  S
270a0 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 72  QLite does not r
270b0 65 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65 20  eally use these 
270c0 66 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 68  functions.  It h
270d0 61 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 71  as its.** own eq
270e0 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61  uivalent interna
270f0 6c 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65  l routines.  The
27100 20 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f 66   atoi() and atof
27110 28 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20  () names are.** 
27120 75 73 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c  used in the tabl
27130 65 20 66 6f 72 20 62 72 65 76 69 74 79 20 61 6e  e for brevity an
27140 64 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61  d because they a
27150 72 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d  re familiar to m
27160 6f 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d  ost.** C program
27170 6d 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  mers..**.** Note
27180 20 74 68 61 74 20 77 68 65 6e 20 74 79 70 65 20   that when type 
27190 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 75  conversions occu
271a0 72 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75  r, pointers retu
271b0 72 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a  rned by prior.**
271c0 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
271d0 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c  3_column_blob(),
271e0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
271f0 74 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a  text(), and/or.*
27200 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
27210 5f 74 65 78 74 31 36 28 29 20 6d 61 79 20 62 65  _text16() may be
27220 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a   invalidated..**
27230 20 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e   Type conversion
27240 73 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e  s and pointer in
27250 76 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 68  validations migh
27260 74 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68  t occur.** in th
27270 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65  e following case
27280 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  s:.**.** <ul>.**
27290 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61   <li> The initia
272a0 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 42  l content is a B
272b0 4c 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  LOB and sqlite3_
272c0 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72  column_text() or
272d0 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
272e0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29  _column_text16()
272f0 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 20 7a   is called.  A z
27300 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 6d  ero-terminator m
27310 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65 65  ight.**      nee
27320 64 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f  d to be added to
27330 20 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c 69   the string.</li
27340 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e  >.** <li> The in
27350 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73  itial content is
27360 20 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64 20   UTF-8 text and 
27370 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
27380 79 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20 20  ytes16() or.**  
27390 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
273a0 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 63  mn_text16() is c
273b0 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e 74  alled.  The cont
273c0 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e 76  ent must be conv
273d0 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f  erted.**      to
273e0 20 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a   UTF-16.</li>.**
273f0 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61   <li> The initia
27400 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46  l content is UTF
27410 2d 31 36 20 74 65 78 74 20 61 6e 64 20 73 71 6c  -16 text and sql
27420 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
27430 73 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73  s() or.**      s
27440 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
27450 78 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20  xt() is called. 
27460 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73   The content mus
27470 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a  t be converted.*
27480 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38 2e  *      to UTF-8.
27490 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  </li>.** </ul>.*
274a0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73  *.** Conversions
274b0 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 62   between UTF-16b
274c0 65 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 61  e and UTF-16le a
274d0 72 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 69  re always done i
274e0 6e 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a  n place and do.*
274f0 2a 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65  * not invalidate
27500 20 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72   a prior pointer
27510 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72  , though of cour
27520 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  se the content o
27530 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20  f the buffer.** 
27540 74 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 70  that the prior p
27550 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 6f  ointer points to
27560 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20   will have been 
27570 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 72  modified.  Other
27580 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e   kinds.** of con
27590 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65  version are done
275a0 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 69   in place when i
275b0 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 62  t is possible, b
275c0 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68 65  ut sometimes the
275d0 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f 73  y.** are not pos
275e0 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f  sible and in tho
275f0 73 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 70  se cases prior p
27600 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 61  ointers are inva
27610 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  lidated..**.** T
27620 68 65 20 73 61 66 65 73 74 20 61 6e 64 20 65 61  he safest and ea
27630 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 65  siest to remembe
27640 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 69  r policy is to i
27650 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 74  nvoke these rout
27660 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f  ines.** in one o
27670 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
27680 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  ways:.**.** <ul>
27690 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33  .**  <li>sqlite3
276a0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66  _column_text() f
276b0 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74  ollowed by sqlit
276c0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
276d0 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73  )</li>.**  <li>s
276e0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
276f0 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79  ob() followed by
27700 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27710 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20  bytes()</li>.** 
27720 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c   <li>sqlite3_col
27730 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f 6c  umn_text16() fol
27740 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33  lowed by sqlite3
27750 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
27760 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a  )</li>.** </ul>.
27770 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  **.** In other w
27780 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64  ords, you should
27790 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f   call sqlite3_co
277a0 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a 20  lumn_text(),.** 
277b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
277c0 6c 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74 65  lob(), or sqlite
277d0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
277e0 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 65  ) first to force
277f0 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69   the result.** i
27800 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 20  nto the desired 
27810 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76  format, then inv
27820 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  oke sqlite3_colu
27830 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a  mn_bytes() or.**
27840 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27850 62 79 74 65 73 31 36 28 29 20 74 6f 20 66 69 6e  bytes16() to fin
27860 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  d the size of th
27870 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f  e result.  Do no
27880 74 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 74  t mix calls.** t
27890 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  o sqlite3_column
278a0 5f 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69 74  _text() or sqlit
278b0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29  e3_column_blob()
278c0 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a 2a   with calls to.*
278d0 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
278e0 5f 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64 20  _bytes16(), and 
278f0 64 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73  do not mix calls
27900 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
27910 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 77  mn_text16().** w
27920 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  ith calls to sql
27930 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
27940 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  s()..**.** The p
27950 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64  ointers returned
27960 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69 6c   are valid until
27970 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69   a type conversi
27980 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a 20  on occurs as.** 
27990 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c  described above,
279a0 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74   or until [sqlit
279b0 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73  e3_step()] or [s
279c0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
279d0 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66  or.** [sqlite3_f
279e0 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63 61  inalize()] is ca
279f0 6c 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72  lled.  The memor
27a00 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20  y space used to 
27a10 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20  hold strings.** 
27a20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 72 65  and BLOBs is fre
27a30 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ed automatically
27a40 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e  .  Do <b>not</b>
27a50 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74 65   pass the pointe
27a60 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 5b  rs returned.** [
27a70 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
27a80 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  lob()], [sqlite3
27a90 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c  _column_text()],
27aa0 20 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73   etc. into.** [s
27ab0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a  qlite3_free()]..
27ac0 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72  **.** If a memor
27ad0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
27ae0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
27af0 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20   the evaluation 
27b00 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65  of any.** of the
27b10 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 20 64  se routines, a d
27b20 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 20  efault value is 
27b30 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 64  returned.  The d
27b40 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20  efault value.** 
27b50 69 73 20 65 69 74 68 65 72 20 74 68 65 20 69 6e  is either the in
27b60 74 65 67 65 72 20 30 2c 20 74 68 65 20 66 6c 6f  teger 0, the flo
27b70 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62  ating point numb
27b80 65 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55 4c  er 0.0, or a NUL
27b90 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 53  L.** pointer.  S
27ba0 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20  ubsequent calls 
27bb0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63  to [sqlite3_errc
27bc0 6f 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 74 75  ode()] will retu
27bd0 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f  rn.** [SQLITE_NO
27be0 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  MEM]..**.** Requ
27bf0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
27c00 33 38 30 33 5d 20 5b 48 31 33 38 30 36 5d 20 5b  3803] [H13806] [
27c10 48 31 33 38 30 39 5d 20 5b 48 31 33 38 31 32 5d  H13809] [H13812]
27c20 20 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 38 31   [H13815] [H1381
27c30 38 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 31 33  8] [H13821] [H13
27c40 38 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 37 5d  824].** [H13827]
27c50 20 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 51 4c   [H13830].*/.SQL
27c60 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
27c70 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
27c80 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  mn_blob(sqlite3_
27c90 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
27ca0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
27cb0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27cc0 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74  bytes(sqlite3_st
27cd0 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a  mt*, int iCol);.
27ce0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
27cf0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
27d00 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74  tes16(sqlite3_st
27d10 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a  mt*, int iCol);.
27d20 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c  SQLITE_API doubl
27d30 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
27d40 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f  _double(sqlite3_
27d50 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
27d60 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
27d70 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27d80 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  int(sqlite3_stmt
27d90 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51  *, int iCol);.SQ
27da0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
27db0 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63  _int64 sqlite3_c
27dc0 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69  olumn_int64(sqli
27dd0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
27de0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  Col);.SQLITE_API
27df0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
27e00 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
27e10 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 65  lumn_text(sqlite
27e20 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
27e30 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  l);.SQLITE_API c
27e40 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
27e50 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
27e60 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
27e70 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54  int iCol);.SQLIT
27e80 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
27e90 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71  3_column_type(sq
27ea0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
27eb0 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   iCol);.SQLITE_A
27ec0 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  PI sqlite3_value
27ed0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
27ee0 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73  _value(sqlite3_s
27ef0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
27f00 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
27f10 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 70  : Destroy A Prep
27f20 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f  ared Statement O
27f30 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20 3c  bject {H13300} <
27f40 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e 0a  S70300><S30100>.
27f50 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
27f60 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 6e  3_finalize() fun
27f70 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
27f80 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72 65  to delete a [pre
27f90 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
27fa0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 74  ..** If the stat
27fb0 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75 74  ement was execut
27fc0 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  ed successfully 
27fd0 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64 20  or not executed 
27fe0 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a 20  at all, then.** 
27ff0 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
28000 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75 74  urned. If execut
28010 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65  ion of the state
28020 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65 6e  ment failed then
28030 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f   an.** [error co
28040 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64  de] or [extended
28050 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20   error code] is 
28060 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
28070 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e  This routine can
28080 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e   be called at an
28090 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 74  y point during t
280a0 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  he execution of 
280b0 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64  the.** [prepared
280c0 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 66   statement].  If
280d0 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63   the virtual mac
280e0 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a 20  hine has not.** 
280f0 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74  completed execut
28100 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f  ion when this ro
28110 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
28120 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a 2a   that is like.**
28130 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61 6e   encountering an
28140 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73 71   error or an [sq
28150 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20  lite3_interrupt 
28160 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a 2a  | interrupt]..**
28170 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64 61   Incomplete upda
28180 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65  tes may be rolle
28190 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e 73  d back and trans
281a0 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65 64  actions canceled
281b0 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f  ,.** depending o
281c0 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 6e  n the circumstan
281d0 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20  ces, and the.** 
281e0 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 74  [error code] ret
281f0 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b 53  urned will be [S
28200 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a  QLITE_ABORT]..**
28210 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
28220 3a 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 5b 48  :.** [H11302] [H
28230 31 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45  11304].*/.SQLITE
28240 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
28250 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65  _finalize(sqlite
28260 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a  3_stmt *pStmt);.
28270 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
28280 20 52 65 73 65 74 20 41 20 50 72 65 70 61 72 65   Reset A Prepare
28290 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65  d Statement Obje
282a0 63 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 37 30  ct {H13330} <S70
282b0 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  300>.**.** The s
282c0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 66  qlite3_reset() f
282d0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
282e0 64 20 74 6f 20 72 65 73 65 74 20 61 20 5b 70 72  d to reset a [pr
282f0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
28300 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 63 6b  ].** object back
28310 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20   to its initial 
28320 73 74 61 74 65 2c 20 72 65 61 64 79 20 74 6f 20  state, ready to 
28330 62 65 20 72 65 2d 65 78 65 63 75 74 65 64 2e 0a  be re-executed..
28340 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 74 65  ** Any SQL state
28350 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 73 20 74  ment variables t
28360 68 61 74 20 68 61 64 20 76 61 6c 75 65 73 20 62  hat had values b
28370 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 73 69  ound to them usi
28380 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74  ng.** the [sqlit
28390 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 73  e3_bind_blob | s
283a0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20  qlite3_bind_*() 
283b0 41 50 49 5d 20 72 65 74 61 69 6e 20 74 68 65 69  API] retain thei
283c0 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 73 65  r values..** Use
283d0 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f   [sqlite3_clear_
283e0 62 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20 72  bindings()] to r
283f0 65 73 65 74 20 74 68 65 20 62 69 6e 64 69 6e 67  eset the binding
28400 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 32  s..**.** {H11332
28410 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72  } The [sqlite3_r
28420 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61  eset(S)] interfa
28430 63 65 20 72 65 73 65 74 73 20 74 68 65 20 5b 70  ce resets the [p
28440 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
28450 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 20 20  t] S.**         
28460 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65 67   back to the beg
28470 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70 72  inning of its pr
28480 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ogram..**.** {H1
28490 31 33 33 34 7d 20 49 66 20 74 68 65 20 6d 6f 73  1334} If the mos
284a0 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f  t recent call to
284b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
284c0 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20  )] for the.**   
284d0 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64         [prepared
284e0 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 72 65   statement] S re
284f0 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52  turned [SQLITE_R
28500 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44  OW] or [SQLITE_D
28510 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  ONE],.**        
28520 20 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 65 33    or if [sqlite3
28530 5f 73 74 65 70 28 53 29 5d 20 68 61 73 20 6e 65  _step(S)] has ne
28540 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20  ver before been 
28550 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a 2a 20  called on S,.** 
28560 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73           then [s
28570 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d  qlite3_reset(S)]
28580 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
28590 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  _OK]..**.** {H11
285a0 33 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 73 74  336} If the most
285b0 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20   recent call to 
285c0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29  [sqlite3_step(S)
285d0 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20  ] for the.**    
285e0 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20        [prepared 
285f0 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 64  statement] S ind
28600 69 63 61 74 65 64 20 61 6e 20 65 72 72 6f 72 2c  icated an error,
28610 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20   then.**        
28620 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74    [sqlite3_reset
28630 28 53 29 5d 20 72 65 74 75 72 6e 73 20 61 6e 20  (S)] returns an 
28640 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72 72  appropriate [err
28650 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20  or code]..**.** 
28660 7b 48 31 31 33 33 38 7d 20 54 68 65 20 5b 73 71  {H11338} The [sq
28670 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20  lite3_reset(S)] 
28680 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20 6e  interface does n
28690 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61  ot change the va
286a0 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  lues.**         
286b0 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 65 33   of any [sqlite3
286c0 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64 69  _bind_blob|bindi
286d0 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72 65  ngs] on the [pre
286e0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
286f0 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50   S..*/.SQLITE_AP
28700 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65  I int sqlite3_re
28710 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  set(sqlite3_stmt
28720 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a   *pStmt);../*.**
28730 20 43 41 50 49 33 52 45 46 3a 20 43 72 65 61 74   CAPI3REF: Creat
28740 65 20 4f 72 20 52 65 64 65 66 69 6e 65 20 53 51  e Or Redefine SQ
28750 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36  L Functions {H16
28760 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a  100} <S20200>.**
28770 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 6e 63   KEYWORDS: {func
28780 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f  tion creation ro
28790 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f  utines}.** KEYWO
287a0 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f  RDS: {applicatio
287b0 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75  n-defined SQL fu
287c0 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 57 4f  nction}.** KEYWO
287d0 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f  RDS: {applicatio
287e0 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75  n-defined SQL fu
287f0 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54  nctions}.**.** T
28800 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f  hese two functio
28810 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 6c 79  ns (collectively
28820 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e 63 74   known as "funct
28830 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f 75  ion creation rou
28840 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 20 75  tines").** are u
28850 73 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20 66  sed to add SQL f
28860 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72  unctions or aggr
28870 65 67 61 74 65 73 20 6f 72 20 74 6f 20 72 65 64  egates or to red
28880 65 66 69 6e 65 20 74 68 65 20 62 65 68 61 76 69  efine the behavi
28890 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 69 6e  or.** of existin
288a0 67 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20  g SQL functions 
288b0 6f 72 20 61 67 67 72 65 67 61 74 65 73 2e 20 20  or aggregates.  
288c0 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65  The only differe
288d0 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 0a  nce between the.
288e0 2a 2a 20 74 77 6f 20 69 73 20 74 68 61 74 20 74  ** two is that t
288f0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
28900 74 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 6f 66  ter, the name of
28910 20 74 68 65 20 28 73 63 61 6c 61 72 29 20 66 75   the (scalar) fu
28920 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67  nction or.** agg
28930 72 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f 64  regate, is encod
28940 65 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72 20  ed in UTF-8 for 
28950 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
28960 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 55 54  unction() and UT
28970 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 6c 69  F-16.** for sqli
28980 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
28990 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  ion16()..**.** T
289a0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
289b0 65 72 20 69 73 20 74 68 65 20 5b 64 61 74 61 62  er is the [datab
289c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
289d0 74 6f 20 77 68 69 63 68 20 74 68 65 20 53 51 4c  to which the SQL
289e0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  .** function is 
289f0 74 6f 20 62 65 20 61 64 64 65 64 2e 20 20 49 66  to be added.  If
28a00 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 72 61   a single progra
28a10 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 68 61 6e  m uses more than
28a20 20 6f 6e 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   one database.**
28a30 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 74 65   connection inte
28a40 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 51 4c  rnally, then SQL
28a50 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74 20   functions must 
28a60 62 65 20 61 64 64 65 64 20 69 6e 64 69 76 69 64  be added individ
28a70 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 63 68  ually to.** each
28a80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
28a90 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
28aa0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
28ab0 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
28ac0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
28ad0 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 6f   to be created o
28ae0 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20  r.** redefined. 
28af0 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   The length of t
28b00 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69 74  he name is limit
28b10 65 64 20 74 6f 20 32 35 35 20 62 79 74 65 73 2c  ed to 255 bytes,
28b20 20 65 78 63 6c 75 73 69 76 65 20 6f 66 0a 2a 2a   exclusive of.**
28b30 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e   the zero-termin
28b40 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61 74  ator.  Note that
28b50 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74 68   the name length
28b60 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79 74   limit is in byt
28b70 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72 61  es, not.** chara
28b80 63 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74 65  cters.  Any atte
28b90 6d 70 74 20 74 6f 20 63 72 65 61 74 65 20 61 20  mpt to create a 
28ba0 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20  function with a 
28bb0 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20 77  longer name.** w
28bc0 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 5b 53  ill result in [S
28bd0 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 65 69  QLITE_ERROR] bei
28be0 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  ng returned..**.
28bf0 2a 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 72  ** The third par
28c00 61 6d 65 74 65 72 20 28 6e 41 72 67 29 0a 2a 2a  ameter (nArg).**
28c10 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
28c20 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74  f arguments that
28c30 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
28c40 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74  n or.** aggregat
28c50 65 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69 73  e takes. If this
28c60 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 2d 31   parameter is -1
28c70 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 20 66  , then the SQL f
28c80 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67  unction or.** ag
28c90 67 72 65 67 61 74 65 20 6d 61 79 20 74 61 6b 65  gregate may take
28ca0 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61   any number of a
28cb0 72 67 75 6d 65 6e 74 73 20 62 65 74 77 65 65 6e  rguments between
28cc0 20 30 20 61 6e 64 20 74 68 65 20 6c 69 6d 69 74   0 and the limit
28cd0 0a 2a 2a 20 73 65 74 20 62 79 20 5b 73 71 6c 69  .** set by [sqli
28ce0 74 65 33 5f 6c 69 6d 69 74 5d 28 5b 53 51 4c 49  te3_limit]([SQLI
28cf0 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f  TE_LIMIT_FUNCTIO
28d00 4e 5f 41 52 47 5d 29 2e 20 20 49 66 20 74 68 65  N_ARG]).  If the
28d10 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d 65   third.** parame
28d20 74 65 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e  ter is less than
28d30 20 2d 31 20 6f 72 20 67 72 65 61 74 65 72 20 74   -1 or greater t
28d40 68 61 6e 20 31 32 37 20 74 68 65 6e 20 74 68 65  han 127 then the
28d50 20 62 65 68 61 76 69 6f 72 20 69 73 0a 2a 2a 20   behavior is.** 
28d60 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
28d70 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 61   The fourth para
28d80 6d 65 74 65 72 2c 20 65 54 65 78 74 52 65 70 2c  meter, eTextRep,
28d90 20 73 70 65 63 69 66 69 65 73 20 77 68 61 74 0a   specifies what.
28da0 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 20  ** [SQLITE_UTF8 
28db0 7c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 5d  | text encoding]
28dc0 20 74 68 69 73 20 53 51 4c 20 66 75 6e 63 74 69   this SQL functi
28dd0 6f 6e 20 70 72 65 66 65 72 73 20 66 6f 72 0a 2a  on prefers for.*
28de0 2a 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 73  * its parameters
28df0 2e 20 20 41 6e 79 20 53 51 4c 20 66 75 6e 63 74  .  Any SQL funct
28e00 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ion implementati
28e10 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c  on should be abl
28e20 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 77 6f 72  e to work.** wor
28e30 6b 20 77 69 74 68 20 55 54 46 2d 38 2c 20 55 54  k with UTF-8, UT
28e40 46 2d 31 36 6c 65 2c 20 6f 72 20 55 54 46 2d 31  F-16le, or UTF-1
28e50 36 62 65 2e 20 20 42 75 74 20 73 6f 6d 65 20 69  6be.  But some i
28e60 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d  mplementations m
28e70 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 65 66  ay be.** more ef
28e80 66 69 63 69 65 6e 74 20 77 69 74 68 20 6f 6e 65  ficient with one
28e90 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 6e 20 61   encoding than a
28ea0 6e 6f 74 68 65 72 2e 20 20 41 6e 20 61 70 70 6c  nother.  An appl
28eb0 69 63 61 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 69  ication may.** i
28ec0 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 72  nvoke sqlite3_cr
28ed0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20  eate_function() 
28ee0 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  or sqlite3_creat
28ef0 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 20 6d  e_function16() m
28f00 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d 65 73  ultiple.** times
28f10 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 66   with the same f
28f20 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69 74 68  unction but with
28f30 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65   different value
28f40 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e 0a 2a  s of eTextRep..*
28f50 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20  * When multiple 
28f60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
28f70 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63  of the same func
28f80 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c 61 62  tion are availab
28f90 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69  le, SQLite.** wi
28fa0 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e 65 20  ll pick the one 
28fb0 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 74 68  that involves th
28fc0 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20 6f  e least amount o
28fd0 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73 69 6f  f data conversio
28fe0 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  n..** If there i
28ff0 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20  s only a single 
29000 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
29010 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 63 61  hich does not ca
29020 72 65 20 77 68 61 74 20 74 65 78 74 0a 2a 2a 20  re what text.** 
29030 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65 64  encoding is used
29040 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75 72 74  , then the fourt
29050 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c  h argument shoul
29060 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 4e 59  d be [SQLITE_ANY
29070 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66  ]..**.** The fif
29080 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  th parameter is 
29090 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 6f 69  an arbitrary poi
290a0 6e 74 65 72 2e 20 20 54 68 65 20 69 6d 70 6c 65  nter.  The imple
290b0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
290c0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e  .** function can
290d0 20 67 61 69 6e 20 61 63 63 65 73 73 20 74 6f 20   gain access to 
290e0 74 68 69 73 20 70 6f 69 6e 74 65 72 20 75 73 69  this pointer usi
290f0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72  ng [sqlite3_user
29100 5f 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  _data()]..**.** 
29110 54 68 65 20 73 65 76 65 6e 74 68 2c 20 65 69 67  The seventh, eig
29120 68 74 68 20 61 6e 64 20 6e 69 6e 74 68 20 70 61  hth and ninth pa
29130 72 61 6d 65 74 65 72 73 2c 20 78 46 75 6e 63 2c  rameters, xFunc,
29140 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61   xStep and xFina
29150 6c 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74 65  l, are.** pointe
29160 72 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67 65  rs to C-language
29170 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
29180 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51  implement the SQ
29190 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a  L function or.**
291a0 20 61 67 67 72 65 67 61 74 65 2e 20 41 20 73 63   aggregate. A sc
291b0 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f  alar SQL functio
291c0 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d  n requires an im
291d0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
291e0 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 61 6c  the xFunc.** cal
291f0 6c 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 4c 4c  lback only, NULL
29200 20 70 6f 69 6e 74 65 72 73 20 73 68 6f 75 6c 64   pointers should
29210 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68   be passed as th
29220 65 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e  e xStep and xFin
29230 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73  al.** parameters
29240 2e 20 41 6e 20 61 67 67 72 65 67 61 74 65 20 53  . An aggregate S
29250 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75  QL function requ
29260 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e  ires an implemen
29270 74 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 70 0a  tation of xStep.
29280 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61 6e  ** and xFinal an
29290 64 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 62 65  d NULL should be
292a0 20 70 61 73 73 65 64 20 66 6f 72 20 78 46 75 6e   passed for xFun
292b0 63 2e 20 54 6f 20 64 65 6c 65 74 65 20 61 6e 20  c. To delete an 
292c0 65 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51 4c 20  existing.** SQL 
292d0 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72  function or aggr
292e0 65 67 61 74 65 2c 20 70 61 73 73 20 4e 55 4c 4c  egate, pass NULL
292f0 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20 66   for all three f
29300 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  unction callback
29310 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 70  s..**.** It is p
29320 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67 69  ermitted to regi
29330 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69 6d  ster multiple im
29340 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
29350 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75 6e   the same.** fun
29360 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65 20  ctions with the 
29370 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69  same name but wi
29380 74 68 20 65 69 74 68 65 72 20 64 69 66 66 65 72  th either differ
29390 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a 2a  ing numbers of.*
293a0 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 64  * arguments or d
293b0 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 72 72  iffering preferr
293c0 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  ed text encoding
293d0 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  s.  SQLite will 
293e0 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65  use.** the imple
293f0 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6d  mentation that m
29400 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74 63  ost closely matc
29410 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20 77  hes the way in w
29420 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c 20  hich the.** SQL 
29430 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
29440 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69 6d  .  A function im
29450 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74  plementation wit
29460 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65  h a non-negative
29470 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65 74  .** nArg paramet
29480 65 72 20 69 73 20 61 20 62 65 74 74 65 72 20 6d  er is a better m
29490 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63  atch than a func
294a0 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tion implementat
294b0 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e 65  ion with.** a ne
294c0 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41 20  gative nArg.  A 
294d0 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74  function where t
294e0 68 65 20 70 72 65 66 65 72 72 65 64 20 74 65 78  he preferred tex
294f0 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d 61  t encoding.** ma
29500 74 63 68 65 73 20 74 68 65 20 64 61 74 61 62 61  tches the databa
29510 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61  se encoding is a
29520 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63 68   better.** match
29530 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e   than a function
29540 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64   where the encod
29550 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e 74  ing is different
29560 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f  .  .** A functio
29570 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f  n where the enco
29580 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 20  ding difference 
29590 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 31 36  is between UTF16
295a0 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a 2a  le and UTF16be.*
295b0 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d 61  * is a closer ma
295c0 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74  tch than a funct
295d0 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e  ion where the en
295e0 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63  coding differenc
295f0 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20  e is.** between 
29600 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e 0a  UTF8 and UTF16..
29610 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20 66  **.** Built-in f
29620 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 20  unctions may be 
29630 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e 65  overloaded by ne
29640 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  w application-de
29650 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 2e  fined functions.
29660 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 70  .** The first ap
29670 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
29680 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  d function with 
29690 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76 65  a given name ove
296a0 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62 75  rrides all.** bu
296b0 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73  ilt-in functions
296c0 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61   in the same [da
296d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
296e0 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  n] with the same
296f0 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65 71   name..** Subseq
29700 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f 6e  uent application
29710 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
29720 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e  ns of the same n
29730 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69 64  ame only overrid
29740 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70 6c  e .** prior appl
29750 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
29760 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 61  functions that a
29770 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63  re an exact matc
29780 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75 6d  h for the.** num
29790 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72  ber of parameter
297a0 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 64 20  s and preferred 
297b0 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  encoding..**.** 
297c0 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  An application-d
297d0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
297e0 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20  is permitted to 
297f0 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 51  call other.** SQ
29800 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 2e  Lite interfaces.
29810 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68 20    However, such 
29820 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a 2a  calls must not.*
29830 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61  * close the data
29840 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
29850 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72 20  nor finalize or 
29860 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61 72  reset the prepar
29870 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  ed.** statement 
29880 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 75 6e  in which the fun
29890 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67  ction is running
298a0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
298b0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31 30 33  ents:.** [H16103
298c0 5d 20 5b 48 31 36 31 30 36 5d 20 5b 48 31 36 31  ] [H16106] [H161
298d0 30 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b 48 31  09] [H16112] [H1
298e0 36 31 31 38 5d 20 5b 48 31 36 31 32 31 5d 20 5b  6118] [H16121] [
298f0 48 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 36 31  H16127].** [H161
29900 33 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 48 31  30] [H16133] [H1
29910 36 31 33 36 5d 20 5b 48 31 36 31 33 39 5d 20 5b  6136] [H16139] [
29920 48 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54  H16142].*/.SQLIT
29930 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
29940 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
29950 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n(.  sqlite3 *db
29960 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
29970 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20  zFunctionName,. 
29980 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74   int nArg,.  int
29990 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69   eTextRep,.  voi
299a0 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20  d *pApp,.  void 
299b0 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33  (*xFunc)(sqlite3
299c0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
299d0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a  lite3_value**),.
299e0 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28    void (*xStep)(
299f0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
29a00 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
29a10 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  ue**),.  void (*
29a20 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f  xFinal)(sqlite3_
29a30 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 51 4c  context*).);.SQL
29a40 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
29a50 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
29a60 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33  ion16(.  sqlite3
29a70 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76 6f   *db,.  const vo
29a80 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  id *zFunctionNam
29a90 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20  e,.  int nArg,. 
29aa0 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a 20   int eTextRep,. 
29ab0 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20 76   void *pApp,.  v
29ac0 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c  oid (*xFunc)(sql
29ad0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
29ae0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
29af0 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74  *),.  void (*xSt
29b00 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
29b10 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
29b20 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
29b30 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69  d (*xFinal)(sqli
29b40 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b  te3_context*).);
29b50 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
29b60 3a 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67 73  : Text Encodings
29b70 20 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 32 30   {H10267} <S5020
29b80 30 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 0a 2a  0> <H16100>.**.*
29b90 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74  * These constant
29ba0 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20   define integer 
29bb0 63 6f 64 65 73 20 74 68 61 74 20 72 65 70 72 65  codes that repre
29bc0 73 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 73  sent the various
29bd0 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  .** text encodin
29be0 67 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20  gs supported by 
29bf0 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66 69  SQLite..*/.#defi
29c00 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 38 20 20  ne SQLITE_UTF8  
29c10 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69           1.#defi
29c20 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  ne SQLITE_UTF16L
29c30 45 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69  E        2.#defi
29c40 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  ne SQLITE_UTF16B
29c50 45 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69  E        3.#defi
29c60 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20  ne SQLITE_UTF16 
29c70 20 20 20 20 20 20 20 20 20 34 20 20 20 20 2f 2a           4    /*
29c80 20 55 73 65 20 6e 61 74 69 76 65 20 62 79 74 65   Use native byte
29c90 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e   order */.#defin
29ca0 65 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 20 20  e SQLITE_ANY    
29cb0 20 20 20 20 20 20 20 20 35 20 20 20 20 2f 2a 20          5    /* 
29cc0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
29cd0 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a  unction only */.
29ce0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55  #define SQLITE_U
29cf0 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 38 20  TF16_ALIGNED  8 
29d00 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 72     /* sqlite3_cr
29d10 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 6f  eate_collation o
29d20 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41  nly */../*.** CA
29d30 50 49 33 52 45 46 3a 20 44 65 70 72 65 63 61 74  PI3REF: Deprecat
29d40 65 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20  ed Functions.** 
29d50 44 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a 2a 2a  DEPRECATED.**.**
29d60 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   These functions
29d70 20 61 72 65 20 5b 64 65 70 72 65 63 61 74 65 64   are [deprecated
29d80 5d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20  ].  In order to 
29d90 6d 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 63 6b  maintain.** back
29da0 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
29db0 69 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 63  ity with older c
29dc0 6f 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63 74  ode, these funct
29dd0 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0a 2a  ions continue .*
29de0 2a 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65  * to be supporte
29df0 64 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65 77  d.  However, new
29e00 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68   applications sh
29e10 6f 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74 68  ould avoid.** th
29e20 65 20 75 73 65 20 6f 66 20 74 68 65 73 65 20 66  e use of these f
29e30 75 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68 65  unctions.  To he
29e40 6c 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65 6f  lp encourage peo
29e50 70 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20  ple to avoid.** 
29e60 75 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e 63  using these func
29e70 74 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e 6f  tions, we are no
29e80 74 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c 20  t going to tell 
29e90 79 6f 75 20 77 68 61 74 20 74 68 65 79 20 64 6f  you what they do
29ea0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
29eb0 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41  ITE_OMIT_DEPRECA
29ec0 54 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  TED.SQLITE_API S
29ed0 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44  QLITE_DEPRECATED
29ee0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67   int sqlite3_agg
29ef0 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c  regate_count(sql
29f00 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
29f10 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
29f20 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74  E_DEPRECATED int
29f30 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64   sqlite3_expired
29f40 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
29f50 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
29f60 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e  TE_DEPRECATED in
29f70 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66  t sqlite3_transf
29f80 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69  er_bindings(sqli
29f90 74 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c 69 74  te3_stmt*, sqlit
29fa0 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54  e3_stmt*);.SQLIT
29fb0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
29fc0 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69  RECATED int sqli
29fd0 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76  te3_global_recov
29fe0 65 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  er(void);.SQLITE
29ff0 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52  _API SQLITE_DEPR
2a000 45 43 41 54 45 44 20 76 6f 69 64 20 73 71 6c 69  ECATED void sqli
2a010 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e  te3_thread_clean
2a020 75 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  up(void);.SQLITE
2a030 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52  _API SQLITE_DEPR
2a040 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74  ECATED int sqlit
2a050 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28  e3_memory_alarm(
2a060 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71  void(*)(void*,sq
2a070 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29  lite3_int64,int)
2a080 2c 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69  ,void*,sqlite3_i
2a090 6e 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f  nt64);.#endif../
2a0a0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
2a0b0 62 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 75 6e  btaining SQL Fun
2a0c0 63 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 72 20  ction Parameter 
2a0d0 56 61 6c 75 65 73 20 7b 48 31 35 31 30 30 7d 20  Values {H15100} 
2a0e0 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20200>.**.** T
2a0f0 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69 6d  he C-language im
2a100 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
2a110 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e  SQL functions an
2a120 64 20 61 67 67 72 65 67 61 74 65 73 20 75 73 65  d aggregates use
2a130 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f 66  s.** this set of
2a140 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   interface routi
2a150 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68  nes to access th
2a160 65 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c 75  e parameter valu
2a170 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75 6e  es on.** the fun
2a180 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61  ction or aggrega
2a190 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 46  te..**.** The xF
2a1a0 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72 20  unc (for scalar 
2a1b0 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78 53  functions) or xS
2a1c0 74 65 70 20 28 66 6f 72 20 61 67 67 72 65 67 61  tep (for aggrega
2a1d0 74 65 73 29 20 70 61 72 61 6d 65 74 65 72 73 0a  tes) parameters.
2a1e0 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ** to [sqlite3_c
2a1f0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29  reate_function()
2a200 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63  ] and [sqlite3_c
2a210 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
2a220 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63 61  ()].** define ca
2a230 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d 70  llbacks that imp
2a240 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66  lement the SQL f
2a250 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67  unctions and agg
2a260 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65 20  regates..** The 
2a270 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  4th parameter to
2a280 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b 73   these callbacks
2a290 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20   is an array of 
2a2a0 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 5b  pointers to.** [
2a2b0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
2a2c0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73  3_value] objects
2a2d0 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20  .  There is one 
2a2e0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20  [sqlite3_value] 
2a2f0 6f 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65 61  object for.** ea
2a300 63 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  ch parameter to 
2a310 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
2a320 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
2a330 73 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a  s are used to.**
2a340 20 65 78 74 72 61 63 74 20 76 61 6c 75 65 73 20   extract values 
2a350 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65  from the [sqlite
2a360 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73  3_value] objects
2a370 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
2a380 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c 79  utines work only
2a390 20 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65 64   with [protected
2a3a0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
2a3b0 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79 20  objects..** Any 
2a3c0 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74  attempt to use t
2a3d0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e  hese routines on
2a3e0 20 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65 64   an [unprotected
2a3f0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a   sqlite3_value].
2a400 2a 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c 74  ** object result
2a410 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62  s in undefined b
2a420 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  ehavior..**.** T
2a430 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f  hese routines wo
2a440 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65  rk just like the
2a450 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 5b   corresponding [
2a460 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75  column access fu
2a470 6e 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 63 65  nctions].** exce
2a480 70 74 20 74 68 61 74 20 20 74 68 65 73 65 20 72  pt that  these r
2a490 6f 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20 73  outines take a s
2a4a0 69 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65 64  ingle [protected
2a4b0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
2a4c0 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 65  object.** pointe
2a4d0 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 5b  r instead of a [
2a4e0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 70  sqlite3_stmt*] p
2a4f0 6f 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69 6e  ointer and an in
2a500 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d  teger column num
2a510 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ber..**.** The s
2a520 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2a530 74 31 36 28 29 20 69 6e 74 65 72 66 61 63 65 20  t16() interface 
2a540 65 78 74 72 61 63 74 73 20 61 20 55 54 46 2d 31  extracts a UTF-1
2a550 36 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 74  6 string.** in t
2a560 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 2d 6f  he native byte-o
2a570 72 64 65 72 20 6f 66 20 74 68 65 20 68 6f 73 74  rder of the host
2a580 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 0a 2a   machine.  The.*
2a590 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  * sqlite3_value_
2a5a0 74 65 78 74 31 36 62 65 28 29 20 61 6e 64 20 73  text16be() and s
2a5b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2a5c0 74 31 36 6c 65 28 29 20 69 6e 74 65 72 66 61 63  t16le() interfac
2a5d0 65 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 55 54  es.** extract UT
2a5e0 46 2d 31 36 20 73 74 72 69 6e 67 73 20 61 73 20  F-16 strings as 
2a5f0 62 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 20 6c  big-endian and l
2a600 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 65 73  ittle-endian res
2a610 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a  pectively..**.**
2a620 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c   The sqlite3_val
2a630 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
2a640 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65  ) interface atte
2a650 6d 70 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a 2a  mpts to apply.**
2a660 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74   numeric affinit
2a670 79 20 74 6f 20 74 68 65 20 76 61 6c 75 65 2e 20  y to the value. 
2a680 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74   This means that
2a690 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 0a 2a   an attempt is.*
2a6a0 2a 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 72  * made to conver
2a6b0 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 61  t the value to a
2a6c0 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f  n integer or flo
2a6d0 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 66  ating point.  If
2a6e0 0a 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 65  .** such a conve
2a6f0 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c  rsion is possibl
2a700 65 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f  e without loss o
2a710 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 69  f information (i
2a720 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73  n other.** words
2a730 2c 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69  , if the value i
2a740 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  s a string that 
2a750 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d  looks like a num
2a760 62 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ber).** then the
2a770 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70   conversion is p
2a780 65 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65 72  erformed.  Other
2a790 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73 69  wise no conversi
2a7a0 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54 68  on occurs..** Th
2a7b0 65 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  e [SQLITE_INTEGE
2a7c0 52 20 7c 20 64 61 74 61 74 79 70 65 5d 20 61 66  R | datatype] af
2a7d0 74 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  ter conversion i
2a7e0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
2a7f0 2a 20 50 6c 65 61 73 65 20 70 61 79 20 70 61 72  * Please pay par
2a800 74 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69 6f  ticular attentio
2a810 6e 20 74 6f 20 74 68 65 20 66 61 63 74 20 74 68  n to the fact th
2a820 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72  at the pointer r
2a830 65 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  eturned.** from 
2a840 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  [sqlite3_value_b
2a850 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  lob()], [sqlite3
2a860 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 20  _value_text()], 
2a870 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76  or.** [sqlite3_v
2a880 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 63  alue_text16()] c
2a890 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65  an be invalidate
2a8a0 64 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e  d by a subsequen
2a8b0 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71  t call to.** [sq
2a8c0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
2a8d0 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76  s()], [sqlite3_v
2a8e0 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d 2c  alue_bytes16()],
2a8f0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
2a900 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b  text()],.** or [
2a910 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2a920 78 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54  xt16()]..**.** T
2a930 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75  hese routines mu
2a940 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f  st be called fro
2a950 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  m the same threa
2a960 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c 20  d as.** the SQL 
2a970 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 75  function that su
2a980 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c 69  pplied the [sqli
2a990 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72 61  te3_value*] para
2a9a0 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65  meters..**.** Re
2a9b0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
2a9c0 48 31 35 31 30 33 5d 20 5b 48 31 35 31 30 36 5d  H15103] [H15106]
2a9d0 20 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 31 31   [H15109] [H1511
2a9e0 32 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 31 35  2] [H15115] [H15
2a9f0 31 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 5b 48  118] [H15121] [H
2aa00 31 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 31 32  15124].** [H1512
2aa10 37 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 31 35  7] [H15130] [H15
2aa20 31 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 2a 2f  133] [H15136].*/
2aa30 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
2aa40 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
2aa50 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74  value_blob(sqlit
2aa60 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49  e3_value*);.SQLI
2aa70 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2aa80 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73  e3_value_bytes(s
2aa90 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2aaa0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2aab0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
2aac0 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c  es16(sqlite3_val
2aad0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
2aae0 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f   double sqlite3_
2aaf0 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c  value_double(sql
2ab00 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
2ab10 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2ab20 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73  ite3_value_int(s
2ab30 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2ab40 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
2ab50 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e3_int64 sqlite3
2ab60 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c  _value_int64(sql
2ab70 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
2ab80 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75  LITE_API const u
2ab90 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71  nsigned char *sq
2aba0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2abb0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2abc0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
2abd0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
2abe0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71  _value_text16(sq
2abf0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2ac00 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2ac10 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61  void *sqlite3_va
2ac20 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c  lue_text16le(sql
2ac30 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
2ac40 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
2ac50 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  oid *sqlite3_val
2ac60 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69  ue_text16be(sqli
2ac70 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
2ac80 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2ac90 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73  te3_value_type(s
2aca0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2acb0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2acc0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d  qlite3_value_num
2acd0 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65  eric_type(sqlite
2ace0 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a 0a 2a  3_value*);../*.*
2acf0 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61  * CAPI3REF: Obta
2ad00 69 6e 20 41 67 67 72 65 67 61 74 65 20 46 75 6e  in Aggregate Fun
2ad10 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 7b 48  ction Context {H
2ad20 31 36 32 31 30 7d 20 3c 53 32 30 32 30 30 3e 0a  16210} <S20200>.
2ad30 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d  **.** The implem
2ad40 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67 72  entation of aggr
2ad50 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69  egate SQL functi
2ad60 6f 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f 75  ons use this rou
2ad70 74 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65  tine to allocate
2ad80 0a 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65 20  .** a structure 
2ad90 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 69  for storing thei
2ada0 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54  r state..**.** T
2adb0 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  he first time th
2adc0 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  e sqlite3_aggreg
2add0 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 72 6f  ate_context() ro
2ade0 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
2adf0 66 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 63 75  for a.** particu
2ae00 6c 61 72 20 61 67 67 72 65 67 61 74 65 2c 20 53  lar aggregate, S
2ae10 51 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 73 20  QLite allocates 
2ae20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79  nBytes of memory
2ae30 2c 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68 61  , zeroes out tha
2ae40 74 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 6e 64  t.** memory, and
2ae50 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
2ae60 65 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 65 63  er to it. On sec
2ae70 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65  ond and subseque
2ae80 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73  nt calls to.** s
2ae90 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
2aea0 5f 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20 74  _context() for t
2aeb0 68 65 20 73 61 6d 65 20 61 67 67 72 65 67 61 74  he same aggregat
2aec0 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 65 78  e function index
2aed0 2c 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 62 75  ,.** the same bu
2aee0 66 66 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  ffer is returned
2aef0 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  . The implementa
2af00 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67 72  tion of the aggr
2af10 65 67 61 74 65 20 63 61 6e 20 75 73 65 0a 2a 2a  egate can use.**
2af20 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 62 75   the returned bu
2af30 66 66 65 72 20 74 6f 20 61 63 63 75 6d 75 6c 61  ffer to accumula
2af40 74 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53  te data..**.** S
2af50 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61  QLite automatica
2af60 6c 6c 79 20 66 72 65 65 73 20 74 68 65 20 61 6c  lly frees the al
2af70 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 77  located buffer w
2af80 68 65 6e 20 74 68 65 20 61 67 67 72 65 67 61 74  hen the aggregat
2af90 65 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e 63 6c  e.** query concl
2afa0 75 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  udes..**.** The 
2afb0 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
2afc0 73 68 6f 75 6c 64 20 62 65 20 61 20 63 6f 70 79  should be a copy
2afd0 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69   of the.** [sqli
2afe0 74 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 53 51  te3_context | SQ
2aff0 4c 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65  L function conte
2b000 78 74 5d 20 74 68 61 74 20 69 73 20 74 68 65 20  xt] that is the 
2b010 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 0a  first parameter.
2b020 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  ** to the callba
2b030 63 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20  ck routine that 
2b040 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 61  implements the a
2b050 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2b060 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  n..**.** This ro
2b070 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61  utine must be ca
2b080 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61  lled from the sa
2b090 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69  me thread in whi
2b0a0 63 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 65 67  ch.** the aggreg
2b0b0 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ate SQL function
2b0c0 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a   is running..**.
2b0d0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2b0e0 0a 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b 48 31  .** [H16211] [H1
2b0f0 36 32 31 33 5d 20 5b 48 31 36 32 31 35 5d 20 5b  6213] [H16215] [
2b100 48 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c 49 54  H16217].*/.SQLIT
2b110 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
2b120 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
2b130 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f  ntext(sqlite3_co
2b140 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79 74  ntext*, int nByt
2b150 65 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  es);../*.** CAPI
2b160 33 52 45 46 3a 20 55 73 65 72 20 44 61 74 61 20  3REF: User Data 
2b170 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48  For Functions {H
2b180 31 36 32 34 30 7d 20 3c 53 32 30 32 30 30 3e 0a  16240} <S20200>.
2b190 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2b1a0 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69 6e  3_user_data() in
2b1b0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
2b1c0 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65  a copy of.** the
2b1d0 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77 61   pointer that wa
2b1e0 73 20 74 68 65 20 70 55 73 65 72 44 61 74 61 20  s the pUserData 
2b1f0 70 61 72 61 6d 65 74 65 72 20 28 74 68 65 20 35  parameter (the 5
2b200 74 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a  th parameter).**
2b210 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
2b220 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
2b230 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69  ()].** and [sqli
2b240 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
2b250 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e 65  ion16()] routine
2b260 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c  s that originall
2b270 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20  y.** registered 
2b280 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
2b290 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2b2a0 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68  . {END}.**.** Th
2b2b0 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20  is routine must 
2b2c0 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74  be called from t
2b2d0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69  he same thread i
2b2e0 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 61  n which.** the a
2b2f0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2b300 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72  ed function is r
2b310 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65  unning..**.** Re
2b320 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
2b330 48 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54  H16243].*/.SQLIT
2b340 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
2b350 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71  te3_user_data(sq
2b360 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
2b370 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
2b380 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65  : Database Conne
2b390 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69  ction For Functi
2b3a0 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 36  ons {H16250} <S6
2b3b0 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 2a  0600><S20200>.**
2b3c0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2b3d0 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
2b3e0 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  e() interface re
2b3f0 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a  turns a copy of.
2b400 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  ** the pointer t
2b410 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  o the [database 
2b420 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65  connection] (the
2b430 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a   1st parameter).
2b440 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ** of the [sqlit
2b450 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
2b460 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71  on()].** and [sq
2b470 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
2b480 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69  ction16()] routi
2b490 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61  nes that origina
2b4a0 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65  lly.** registere
2b4b0 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  d the applicatio
2b4c0 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n defined functi
2b4d0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  on..**.** Requir
2b4e0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32  ements:.** [H162
2b4f0 35 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  53].*/.SQLITE_AP
2b500 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74  I sqlite3 *sqlit
2b510 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
2b520 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ndle(sqlite3_con
2b530 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  text*);../*.** C
2b540 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 6f  API3REF: Functio
2b550 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 61  n Auxiliary Data
2b560 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32 30   {H16270} <S2020
2b570 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  0>.**.** The fol
2b580 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 74  lowing two funct
2b590 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 64  ions may be used
2b5a0 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 66   by scalar SQL f
2b5b0 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 61  unctions to.** a
2b5c0 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 74  ssociate metadat
2b5d0 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20  a with argument 
2b5e0 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 73  values. If the s
2b5f0 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 73  ame value is pas
2b600 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 70  sed to.** multip
2b610 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f  le invocations o
2b620 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 66  f the same SQL f
2b630 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 71  unction during q
2b640 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c 20  uery execution, 
2b650 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 69  under.** some ci
2b660 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 20  rcumstances the 
2b670 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 64  associated metad
2b680 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 65  ata may be prese
2b690 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a 2a  rved. This may.*
2b6a0 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 65  * be used, for e
2b6b0 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 61  xample, to add a
2b6c0 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 73   regular-express
2b6d0 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 61  ion matching sca
2b6e0 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e  lar.** function.
2b6f0 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 65   The compiled ve
2b700 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 67  rsion of the reg
2b710 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20  ular expression 
2b720 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20  is stored as.** 
2b730 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 61  metadata associa
2b740 74 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c  ted with the SQL
2b750 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73   value passed as
2b760 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70   the regular exp
2b770 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 65  ression.** patte
2b780 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c 65  rn.  The compile
2b790 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73  d regular expres
2b7a0 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 73  sion can be reus
2b7b0 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a  ed on multiple.*
2b7c0 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66  * invocations of
2b7d0 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69   the same functi
2b7e0 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f  on so that the o
2b7f0 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e 20  riginal pattern 
2b800 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 6e  string.** does n
2b810 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65  ot need to be re
2b820 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 68  compiled on each
2b830 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a   invocation..**.
2b840 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67  ** The sqlite3_g
2b850 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74  et_auxdata() int
2b860 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
2b870 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2b880 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 6f  metadata.** asso
2b890 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73 71  ciated by the sq
2b8a0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
2b8b0 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  a() function wit
2b8c0 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65  h the Nth argume
2b8d0 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 74  nt.** value to t
2b8e0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
2b8f0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e  efined function.
2b900 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 20   If no metadata 
2b910 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a 2a  has been ever.**
2b920 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 68   been set for th
2b930 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 6f  e Nth argument o
2b940 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c 20  f the function, 
2b950 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 73  or if the corres
2b960 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 74  ponding.** funct
2b970 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 61  ion parameter ha
2b980 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20  s changed since 
2b990 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 61  the meta-data wa
2b9a0 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 73  s set,.** then s
2b9b0 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61  qlite3_get_auxda
2b9c0 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 4e  ta() returns a N
2b9d0 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ULL pointer..**.
2b9e0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73  ** The sqlite3_s
2b9f0 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74  et_auxdata() int
2ba00 65 72 66 61 63 65 20 73 61 76 65 73 20 74 68 65  erface saves the
2ba10 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f 69   metadata.** poi
2ba20 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 33  nted to by its 3
2ba30 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20  rd parameter as 
2ba40 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f 72  the metadata for
2ba50 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72 67   the N-th.** arg
2ba60 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70 70  ument of the app
2ba70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
2ba80 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62 73   function.  Subs
2ba90 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20  equent.** calls 
2baa0 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61  to sqlite3_get_a
2bab0 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20 72  uxdata() might r
2bac0 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61 2c  eturn this data,
2bad0 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e 6f   if it has.** no
2bae0 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65 64  t been destroyed
2baf0 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f  ..** If it is no
2bb00 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20 77  t NULL, SQLite w
2bb10 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64  ill invoke the d
2bb20 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75 6e  estructor.** fun
2bb30 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20 74  ction given by t
2bb40 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72  he 4th parameter
2bb50 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 5f   to sqlite3_set_
2bb60 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a 20  auxdata() on.** 
2bb70 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 68 65  the metadata whe
2bb80 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
2bb90 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 72  ing function par
2bba0 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a 2a  ameter changes.*
2bbb0 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53 51  * or when the SQ
2bbc0 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70  L statement comp
2bbd0 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 65 72  letes, whichever
2bbe0 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a 2a   comes first..**
2bbf0 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 72  .** SQLite is fr
2bc00 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 64  ee to call the d
2bc10 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 72  estructor and dr
2bc20 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 61  op metadata on a
2bc30 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  ny.** parameter 
2bc40 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e 20  of any function 
2bc50 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54 68  at any time.  Th
2bc60 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65  e only guarantee
2bc70 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20   is that.** the 
2bc80 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c 20  destructor will 
2bc90 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65  be called before
2bca0 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 69 73   the metadata is
2bcb0 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a 20   dropped..**.** 
2bcc0 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65 74  In practice, met
2bcd0 61 64 61 74 61 20 69 73 20 70 72 65 73 65 72 76  adata is preserv
2bce0 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63 74  ed between funct
2bcf0 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a 2a  ion calls for.**
2bd00 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61   expressions tha
2bd10 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 61  t are constant a
2bd20 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 20  t compile time. 
2bd30 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c 69  This includes li
2bd40 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 20  teral.** values 
2bd50 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c 65  and SQL variable
2bd60 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  s..**.** These r
2bd70 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20  outines must be 
2bd80 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20  called from the 
2bd90 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77  same thread in w
2bda0 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c 20  hich.** the SQL 
2bdb0 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e  function is runn
2bdc0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ing..**.** Requi
2bdd0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36  rements:.** [H16
2bde0 32 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 5b 48  272] [H16274] [H
2bdf0 31 36 32 37 36 5d 20 5b 48 31 36 32 37 37 5d 20  16276] [H16277] 
2be00 5b 48 31 36 32 37 38 5d 20 5b 48 31 36 32 37 39  [H16278] [H16279
2be10 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
2be20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67 65  void *sqlite3_ge
2be30 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65  t_auxdata(sqlite
2be40 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20  3_context*, int 
2be50 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  N);.SQLITE_API v
2be60 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  oid sqlite3_set_
2be70 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f  auxdata(sqlite3_
2be80 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e 2c  context*, int N,
2be90 20 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a 29   void*, void (*)
2bea0 28 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a 2a  (void*));.../*.*
2beb0 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 73  * CAPI3REF: Cons
2bec0 74 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 20 53  tants Defining S
2bed0 70 65 63 69 61 6c 20 44 65 73 74 72 75 63 74 6f  pecial Destructo
2bee0 72 20 42 65 68 61 76 69 6f 72 20 7b 48 31 30 32  r Behavior {H102
2bef0 38 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a  80} <S30100>.**.
2bf00 2a 2a 20 54 68 65 73 65 20 61 72 65 20 73 70 65  ** These are spe
2bf10 63 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20  cial values for 
2bf20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 74  the destructor t
2bf30 68 61 74 20 69 73 20 70 61 73 73 65 64 20 69 6e  hat is passed in
2bf40 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61 6c   as the.** final
2bf50 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f 75   argument to rou
2bf60 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c 69  tines like [sqli
2bf70 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28  te3_result_blob(
2bf80 29 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73 74  )].  If the dest
2bf90 72 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d 65  ructor.** argume
2bfa0 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54 41  nt is SQLITE_STA
2bfb0 54 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  TIC, it means th
2bfc0 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70  at the content p
2bfd0 6f 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74 61  ointer is consta
2bfe0 6e 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20 6e  nt.** and will n
2bff0 65 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49 74  ever change.  It
2c000 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
2c010 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e 20  o be destroyed. 
2c020 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 54   The.** SQLITE_T
2c030 52 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20 6d  RANSIENT value m
2c040 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f  eans that the co
2c050 6e 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65 6c  ntent will likel
2c060 79 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 74  y change in.** t
2c070 68 65 20 6e 65 61 72 20 66 75 74 75 72 65 20 61  he near future a
2c080 6e 64 20 74 68 61 74 20 53 51 4c 69 74 65 20 73  nd that SQLite s
2c090 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f  hould make its o
2c0a0 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20  wn private copy 
2c0b0 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  of.** the conten
2c0c0 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  t before returni
2c0d0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 79  ng..**.** The ty
2c0e0 70 65 64 65 66 20 69 73 20 6e 65 63 65 73 73 61  pedef is necessa
2c0f0 72 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  ry to work aroun
2c100 64 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63 65  d problems in ce
2c110 72 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f 6d  rtain.** C++ com
2c120 70 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69 63  pilers.  See tic
2c130 6b 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74 79  ket #2191..*/.ty
2c140 70 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71 6c  pedef void (*sql
2c150 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f  ite3_destructor_
2c160 74 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23 64  type)(void*);.#d
2c170 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41  efine SQLITE_STA
2c180 54 49 43 20 20 20 20 20 20 28 28 73 71 6c 69 74  TIC      ((sqlit
2c190 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79  e3_destructor_ty
2c1a0 70 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53 51  pe)0).#define SQ
2c1b0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 20  LITE_TRANSIENT  
2c1c0 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72   ((sqlite3_destr
2c1d0 75 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a 0a  uctor_type)-1)..
2c1e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2c1f0 53 65 74 74 69 6e 67 20 54 68 65 20 52 65 73 75  Setting The Resu
2c200 6c 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75 6e  lt Of An SQL Fun
2c210 63 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d 20 3c  ction {H16400} <
2c220 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20200>.**.** Th
2c230 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
2c240 20 75 73 65 64 20 62 79 20 74 68 65 20 78 46 75   used by the xFu
2c250 6e 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 61 6c  nc or xFinal cal
2c260 6c 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a 20 69  lbacks that.** i
2c270 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 75 6e  mplement SQL fun
2c280 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65  ctions and aggre
2c290 67 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a 20 5b  gates.  See.** [
2c2a0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2c2b0 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b  unction()] and [
2c2c0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2c2d0 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20  unction16()].** 
2c2e0 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
2c2f0 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nformation..**.*
2c300 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  * These function
2c310 73 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 63 68  s work very much
2c320 20 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 61 6d   like the [param
2c330 65 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 66 61  eter binding] fa
2c340 6d 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e 63 74  mily of.** funct
2c350 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69 6e  ions used to bin
2c360 64 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73 74  d values to host
2c370 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 70   parameters in p
2c380 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2c390 74 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20  ts..** Refer to 
2c3a0 74 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65 74  the [SQL paramet
2c3b0 65 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  er] documentatio
2c3c0 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  n for additional
2c3d0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
2c3e0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2c3f0 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 69 6e  result_blob() in
2c400 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65  terface sets the
2c410 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20   result from.** 
2c420 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  an application-d
2c430 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
2c440 74 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 20 77  to be the BLOB w
2c450 68 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 73 20  hose content is 
2c460 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79  pointed.** to by
2c470 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
2c480 6d 65 74 65 72 20 61 6e 64 20 77 68 69 63 68 20  meter and which 
2c490 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 20  is N bytes long 
2c4a0 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a  where N is the.*
2c4b0 2a 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  * third paramete
2c4c0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  r..**.** The sql
2c4d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f  ite3_result_zero
2c4e0 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65  blob() interface
2c4f0 73 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74  s set the result
2c500 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69   of.** the appli
2c510 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
2c520 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20  unction to be a 
2c530 42 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e 67 20  BLOB containing 
2c540 61 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 74 65  all zero.** byte
2c550 73 20 61 6e 64 20 4e 20 62 79 74 65 73 20 69 6e  s and N bytes in
2c560 20 73 69 7a 65 2c 20 77 68 65 72 65 20 4e 20 69   size, where N i
2c570 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  s the value of t
2c580 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  he 2nd parameter
2c590 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2c5a0 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
2c5b0 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65  e() interface se
2c5c0 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72  ts the result fr
2c5d0 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61  om.** an applica
2c5e0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
2c5f0 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66 6c  ction to be a fl
2c600 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
2c610 75 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  ue specified.** 
2c620 62 79 20 69 74 73 20 32 6e 64 20 61 72 67 75 6d  by its 2nd argum
2c630 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ent..**.** The s
2c640 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2c650 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ror() and sqlite
2c660 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
2c670 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20  () functions.** 
2c680 63 61 75 73 65 20 74 68 65 20 69 6d 70 6c 65 6d  cause the implem
2c690 65 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 74 69  ented SQL functi
2c6a0 6f 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65  on to throw an e
2c6b0 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 51 4c  xception..** SQL
2c6c0 69 74 65 20 75 73 65 73 20 74 68 65 20 73 74 72  ite uses the str
2c6d0 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ing pointed to b
2c6e0 79 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 61 72  y the.** 2nd par
2c6f0 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 65  ameter of sqlite
2c700 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
2c710 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75   or sqlite3_resu
2c720 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20  lt_error16().** 
2c730 61 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61  as the text of a
2c740 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e  n error message.
2c750 20 20 53 51 4c 69 74 65 20 69 6e 74 65 72 70 72    SQLite interpr
2c760 65 74 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a  ets the error.**
2c770 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20   message string 
2c780 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73  from sqlite3_res
2c790 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 20 55  ult_error() as U
2c7a0 54 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a 2a 20  TF-8. SQLite.** 
2c7b0 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 73  interprets the s
2c7c0 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74  tring from sqlit
2c7d0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
2c7e0 36 28 29 20 61 73 20 55 54 46 2d 31 36 20 69 6e  6() as UTF-16 in
2c7f0 20 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65 20   native.** byte 
2c800 6f 72 64 65 72 2e 20 20 49 66 20 74 68 65 20 74  order.  If the t
2c810 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74  hird parameter t
2c820 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  o sqlite3_result
2c830 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73  _error().** or s
2c840 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2c850 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 74  ror16() is negat
2c860 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20  ive then SQLite 
2c870 74 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 72  takes as the err
2c880 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 6c  or.** message al
2c890 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 67  l text up throug
2c8a0 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f  h the first zero
2c8b0 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49   character..** I
2c8c0 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61  f the third para
2c8d0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
2c8e0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20  _result_error() 
2c8f0 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65  or.** sqlite3_re
2c900 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69  sult_error16() i
2c910 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 74  s non-negative t
2c920 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73  hen SQLite takes
2c930 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62 79   that many.** by
2c940 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74  tes (not charact
2c950 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32 6e  ers) from the 2n
2c960 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 74  d parameter as t
2c970 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
2c980 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  ..** The sqlite3
2c990 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20  _result_error() 
2c9a0 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  and sqlite3_resu
2c9b0 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20  lt_error16().** 
2c9c0 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 61 20  routines make a 
2c9d0 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20  private copy of 
2c9e0 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
2c9f0 65 20 74 65 78 74 20 62 65 66 6f 72 65 0a 2a 2a  e text before.**
2ca00 20 74 68 65 79 20 72 65 74 75 72 6e 2e 20 20 48   they return.  H
2ca10 65 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c 69 6e  ence, the callin
2ca20 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64  g function can d
2ca30 65 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a 2a 20  eallocate or.** 
2ca40 6d 6f 64 69 66 79 20 74 68 65 20 74 65 78 74 20  modify the text 
2ca50 61 66 74 65 72 20 74 68 65 79 20 72 65 74 75 72  after they retur
2ca60 6e 20 77 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a  n without harm..
2ca70 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2ca80 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
2ca90 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e  () function chan
2caa0 67 65 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f  ges the error co
2cab0 64 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62  de.** returned b
2cac0 79 20 53 51 4c 69 74 65 20 61 73 20 61 20 72 65  y SQLite as a re
2cad0 73 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 6f 72  sult of an error
2cae0 20 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20   in a function. 
2caf0 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a 20   By default,.** 
2cb00 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  the error code i
2cb10 73 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20  s SQLITE_ERROR. 
2cb20 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61   A subsequent ca
2cb30 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65  ll to sqlite3_re
2cb40 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20  sult_error().** 
2cb50 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  or sqlite3_resul
2cb60 74 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73 65  t_error16() rese
2cb70 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ts the error cod
2cb80 65 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f  e to SQLITE_ERRO
2cb90 52 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  R..**.** The sql
2cba0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f 62  ite3_result_toob
2cbb0 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 63  ig() interface c
2cbc0 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20  auses SQLite to 
2cbd0 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a  throw an error.*
2cbe0 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61  * indicating tha
2cbf0 74 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42 4c  t a string or BL
2cc00 4f 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 74 6f  OB is to long to
2cc10 20 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a 2a   represent..**.*
2cc20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2cc30 73 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 6e 74  sult_nomem() int
2cc40 65 72 66 61 63 65 20 63 61 75 73 65 73 20 53 51  erface causes SQ
2cc50 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e  Lite to throw an
2cc60 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61   error.** indica
2cc70 74 69 6e 67 20 74 68 61 74 20 61 20 6d 65 6d 6f  ting that a memo
2cc80 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
2cc90 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  iled..**.** The 
2cca0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
2ccb0 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 73  nt() interface s
2ccc0 65 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76  ets the return v
2ccd0 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61  alue.** of the a
2cce0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2ccf0 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
2cd00 65 20 74 68 65 20 33 32 2d 62 69 74 20 73 69 67  e the 32-bit sig
2cd10 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76  ned integer.** v
2cd20 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68  alue given in th
2cd30 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a  e 2nd argument..
2cd40 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2cd50 65 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69 6e  esult_int64() in
2cd60 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65  terface sets the
2cd70 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
2cd80 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   of the applicat
2cd90 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2cda0 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 36  tion to be the 6
2cdb0 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  4-bit signed int
2cdc0 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69  eger.** value gi
2cdd0 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61  ven in the 2nd a
2cde0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
2cdf0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2ce00 74 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66 61  t_null() interfa
2ce10 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74 75  ce sets the retu
2ce20 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74  rn value.** of t
2ce30 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
2ce40 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
2ce50 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  to be NULL..**.*
2ce60 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2ce70 73 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71 6c  sult_text(), sql
2ce80 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2ce90 31 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  16(),.** sqlite3
2cea0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
2ceb0 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  (), and sqlite3_
2cec0 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28  result_text16be(
2ced0 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  ) interfaces.** 
2cee0 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 76  set the return v
2cef0 61 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70 6c  alue of the appl
2cf00 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2cf10 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a 2a  function to be.*
2cf20 2a 20 61 20 74 65 78 74 20 73 74 72 69 6e 67 20  * a text string 
2cf30 77 68 69 63 68 20 69 73 20 72 65 70 72 65 73 65  which is represe
2cf40 6e 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20 55  nted as UTF-8, U
2cf50 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74  TF-16 native byt
2cf60 65 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46 2d  e order,.** UTF-
2cf70 31 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e  16 little endian
2cf80 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67 20  , or UTF-16 big 
2cf90 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74 69  endian, respecti
2cfa0 76 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20  vely..** SQLite 
2cfb0 74 61 6b 65 73 20 74 68 65 20 74 65 78 74 20 72  takes the text r
2cfc0 65 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20 61  esult from the a
2cfd0 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d 0a  pplication from.
2cfe0 2a 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d  ** the 2nd param
2cff0 65 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69  eter of the sqli
2d000 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a  te3_result_text*
2d010 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20   interfaces..** 
2d020 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d  If the 3rd param
2d030 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69  eter to the sqli
2d040 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a  te3_result_text*
2d050 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69   interfaces.** i
2d060 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e  s negative, then
2d070 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72 65   SQLite takes re
2d080 73 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20 74  sult text from t
2d090 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  he 2nd parameter
2d0a0 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20  .** through the 
2d0b0 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 61  first zero chara
2d0c0 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20  cter..** If the 
2d0d0 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  3rd parameter to
2d0e0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   the sqlite3_res
2d0f0 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66  ult_text* interf
2d100 61 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e 2d 6e  aces.** is non-n
2d110 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 61 73  egative, then as
2d120 20 6d 61 6e 79 20 62 79 74 65 73 20 28 6e 6f 74   many bytes (not
2d130 20 63 68 61 72 61 63 74 65 72 73 29 20 6f 66 20   characters) of 
2d140 74 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f 69 6e  the text.** poin
2d150 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 32 6e  ted to by the 2n
2d160 64 20 70 61 72 61 6d 65 74 65 72 20 61 72 65 20  d parameter are 
2d170 74 61 6b 65 6e 20 61 73 20 74 68 65 20 61 70 70  taken as the app
2d180 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
2d190 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 73  .** function res
2d1a0 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34  ult..** If the 4
2d1b0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
2d1c0 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  the sqlite3_resu
2d1d0 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61  lt_text* interfa
2d1e0 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65  ces.** or sqlite
2d1f0 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73  3_result_blob is
2d200 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e   a non-NULL poin
2d210 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  ter, then SQLite
2d220 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a 20 66   calls that.** f
2d230 75 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20 64  unction as the d
2d240 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 65  estructor on the
2d250 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 65   text or BLOB re
2d260 73 75 6c 74 20 77 68 65 6e 20 69 74 20 68 61 73  sult when it has
2d270 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 73 69  .** finished usi
2d280 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a  ng that result..
2d290 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61  ** If the 4th pa
2d2a0 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73  rameter to the s
2d2b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2d2c0 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 20 6f  xt* interfaces o
2d2d0 72 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  r to.** sqlite3_
2d2e0 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74  result_blob is t
2d2f0 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74  he special const
2d300 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 54 49  ant SQLITE_STATI
2d310 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a 2a  C, then SQLite.*
2d320 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74  * assumes that t
2d330 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20  he text or BLOB 
2d340 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63 6f 6e  result is in con
2d350 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e 64 20  stant space and 
2d360 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f 70 79  does not.** copy
2d370 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
2d380 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 6e 6f  the parameter no
2d390 72 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75 63  r call a destruc
2d3a0 74 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65  tor on the conte
2d3b0 6e 74 0a 2a 2a 20 77 68 65 6e 20 69 74 20 68 61  nt.** when it ha
2d3c0 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67  s finished using
2d3d0 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a 2a   that result..**
2d3e0 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72 61   If the 4th para
2d3f0 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c  meter to the sql
2d400 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2d410 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  * interfaces.** 
2d420 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  or sqlite3_resul
2d430 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73 70  t_blob is the sp
2d440 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53  ecial constant S
2d450 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 0a  QLITE_TRANSIENT.
2d460 2a 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d  ** then SQLite m
2d470 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74  akes a copy of t
2d480 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 73  he result into s
2d490 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72  pace obtained fr
2d4a0 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69  om.** from [sqli
2d4b0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 65  te3_malloc()] be
2d4c0 66 6f 72 65 20 69 74 20 72 65 74 75 72 6e 73 2e  fore it returns.
2d4d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2d4e0 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
2d4f0 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73  ) interface sets
2d500 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a   the result of.*
2d510 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * the applicatio
2d520 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
2d530 6f 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 79 20  on to be a copy 
2d540 74 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63  the.** [unprotec
2d550 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
2d560 65 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 69 66  e] object specif
2d570 69 65 64 20 62 79 20 74 68 65 20 32 6e 64 20 70  ied by the 2nd p
2d580 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 0a 2a  arameter.  The.*
2d590 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  * sqlite3_result
2d5a0 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 61  _value() interfa
2d5b0 63 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20  ce makes a copy 
2d5c0 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
2d5d0 76 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 68 61  value].** so tha
2d5e0 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  t the [sqlite3_v
2d5f0 61 6c 75 65 5d 20 73 70 65 63 69 66 69 65 64 20  alue] specified 
2d600 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  in the parameter
2d610 20 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 0a 2a   may change or.*
2d620 2a 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64  * be deallocated
2d630 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 72   after sqlite3_r
2d640 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 72 65  esult_value() re
2d650 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 68 61  turns without ha
2d660 72 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 65 63  rm..** A [protec
2d670 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
2d680 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61 6c  e] object may al
2d690 77 61 79 73 20 62 65 20 75 73 65 64 20 77 68 65  ways be used whe
2d6a0 72 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74  re an.** [unprot
2d6b0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
2d6c0 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20 72  lue] object is r
2d6d0 65 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74 68  equired, so eith
2d6e0 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 5b 73  er.** kind of [s
2d6f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
2d700 6a 65 63 74 20 63 61 6e 20 62 65 20 75 73 65 64  ject can be used
2d710 20 77 69 74 68 20 74 68 69 73 20 69 6e 74 65 72   with this inter
2d720 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  face..**.** If t
2d730 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
2d740 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  e called from wi
2d750 74 68 69 6e 20 74 68 65 20 64 69 66 66 65 72 65  thin the differe
2d760 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 68 61  nt thread.** tha
2d770 6e 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 61 69  n the one contai
2d780 6e 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63 61  ning the applica
2d790 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
2d7a0 63 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65 69  ction that recei
2d7b0 76 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69  ved.** the [sqli
2d7c0 74 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 6f 69  te3_context] poi
2d7d0 6e 74 65 72 2c 20 74 68 65 20 72 65 73 75 6c 74  nter, the result
2d7e0 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e  s are undefined.
2d7f0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
2d800 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 33 5d  nts:.** [H16403]
2d810 20 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 34 30   [H16406] [H1640
2d820 39 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 31 36  9] [H16412] [H16
2d830 34 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 5b 48  415] [H16418] [H
2d840 31 36 34 32 31 5d 20 5b 48 31 36 34 32 34 5d 0a  16421] [H16424].
2d850 2a 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 31 36  ** [H16427] [H16
2d860 34 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 5b 48  430] [H16433] [H
2d870 31 36 34 33 36 5d 20 5b 48 31 36 34 33 39 5d 20  16436] [H16439] 
2d880 5b 48 31 36 34 34 32 5d 20 5b 48 31 36 34 34 35  [H16442] [H16445
2d890 5d 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 5b 48  ] [H16448].** [H
2d8a0 31 36 34 35 31 5d 20 5b 48 31 36 34 35 34 5d 20  16451] [H16454] 
2d8b0 5b 48 31 36 34 35 37 5d 20 5b 48 31 36 34 36 30  [H16457] [H16460
2d8c0 5d 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 53 51  ] [H16463].*/.SQ
2d8d0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2d8e0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
2d8f0 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  b(sqlite3_contex
2d900 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  t*, const void*,
2d910 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f   int, void(*)(vo
2d920 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50  id*));.SQLITE_AP
2d930 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2d940 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c  esult_double(sql
2d950 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64  ite3_context*, d
2d960 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41  ouble);.SQLITE_A
2d970 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2d980 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c  result_error(sql
2d990 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63  ite3_context*, c
2d9a0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29  onst char*, int)
2d9b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
2d9c0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
2d9d0 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33  _error16(sqlite3
2d9e0 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74  _context*, const
2d9f0 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51   void*, int);.SQ
2da00 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2da10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2da20 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65  or_toobig(sqlite
2da30 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c  3_context*);.SQL
2da40 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2da50 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2da60 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f  r_nomem(sqlite3_
2da70 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54  context*);.SQLIT
2da80 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2da90 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
2daa0 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  code(sqlite3_con
2dab0 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  text*, int);.SQL
2dac0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2dad0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
2dae0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2daf0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  , int);.SQLITE_A
2db00 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2db10 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c  result_int64(sql
2db20 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73  ite3_context*, s
2db30 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53  qlite3_int64);.S
2db40 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2db50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
2db60 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ll(sqlite3_conte
2db70 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  xt*);.SQLITE_API
2db80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
2db90 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65  sult_text(sqlite
2dba0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73  3_context*, cons
2dbb0 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f  t char*, int, vo
2dbc0 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53  id(*)(void*));.S
2dbd0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2dbe0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2dbf0 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e  xt16(sqlite3_con
2dc00 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  text*, const voi
2dc10 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29  d*, int, void(*)
2dc20 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45  (void*));.SQLITE
2dc30 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
2dc40 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
2dc50 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
2dc60 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  t*, const void*,
2dc70 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69   int,void(*)(voi
2dc80 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
2dc90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
2dca0 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71  sult_text16be(sq
2dcb0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2dcc0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
2dcd0 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
2dce0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
2dcf0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
2dd00 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63  _value(sqlite3_c
2dd10 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33  ontext*, sqlite3
2dd20 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
2dd30 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
2dd40 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f  3_result_zeroblo
2dd50 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  b(sqlite3_contex
2dd60 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a  t*, int n);../*.
2dd70 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66  ** CAPI3REF: Def
2dd80 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e  ine New Collatin
2dd90 67 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36  g Sequences {H16
2dda0 36 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a  600} <S20300>.**
2ddb0 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69  .** These functi
2ddc0 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  ons are used to 
2ddd0 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f  add new collatio
2dde0 6e 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74  n sequences to t
2ddf0 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20  he.** [database 
2de00 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63  connection] spec
2de10 69 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72  ified as the fir
2de20 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  st argument..**.
2de30 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  ** The name of t
2de40 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e  he new collation
2de50 20 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65   sequence is spe
2de60 63 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d  cified as a UTF-
2de70 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20  8 string.** for 
2de80 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2de90 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73  ollation() and s
2dea0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2deb0 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20  llation_v2().** 
2dec0 61 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72  and a UTF-16 str
2ded0 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  ing for sqlite3_
2dee0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
2def0 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73  16(). In all cas
2df00 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69  es.** the name i
2df10 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
2df20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20  second function 
2df30 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
2df40 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  The third argume
2df50 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66  nt may be one of
2df60 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b   the constants [
2df70 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a  SQLITE_UTF8],.**
2df80 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45   [SQLITE_UTF16LE
2df90 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54  ], or [SQLITE_UT
2dfa0 46 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69  F16BE], indicati
2dfb0 6e 67 20 74 68 61 74 20 74 68 65 20 75 73 65 72  ng that the user
2dfc0 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75  -supplied.** rou
2dfd0 74 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20  tine expects to 
2dfe0 62 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65  be passed pointe
2dff0 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e  rs to strings en
2e000 63 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d  coded using UTF-
2e010 38 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74  8,.** UTF-16 lit
2e020 74 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55  tle-endian, or U
2e030 54 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e  TF-16 big-endian
2e040 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  , respectively. 
2e050 54 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67  The.** third arg
2e060 75 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f  ument might also
2e070 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31   be [SQLITE_UTF1
2e080 36 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  6] to indicate t
2e090 68 61 74 20 74 68 65 20 72 6f 75 74 69 6e 65 0a  hat the routine.
2e0a0 2a 2a 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74  ** expects point
2e0b0 65 72 73 20 74 6f 20 62 65 20 55 54 46 2d 31 36  ers to be UTF-16
2e0c0 20 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20   strings in the 
2e0d0 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
2e0e0 72 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 72 67  r, or the.** arg
2e0f0 75 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b 53 51  ument can be [SQ
2e100 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
2e110 45 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 74 68  ED] if the.** th
2e120 65 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74  e routine expect
2e130 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36  s pointers to 16
2e140 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65  -bit word aligne
2e150 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20  d strings.** of 
2e160 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61  UTF-16 in the na
2e170 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e  tive byte order.
2e180 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  .**.** A pointer
2e190 20 74 6f 20 74 68 65 20 75 73 65 72 20 73 75 70   to the user sup
2e1a0 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d 75  plied routine mu
2e1b0 73 74 20 62 65 20 70 61 73 73 65 64 20 61 73 20  st be passed as 
2e1c0 74 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72 67  the fifth.** arg
2e1d0 75 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69 73  ument.  If it is
2e1e0 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20 74   NULL, this is t
2e1f0 68 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65 74  he same as delet
2e200 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  ing the collatio
2e210 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28 73  n.** sequence (s
2e220 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61  o that SQLite ca
2e230 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e 79  nnot call it any
2e240 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20 74  more)..** Each t
2e250 69 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61 74  ime the applicat
2e260 69 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75 6e  ion supplied fun
2e270 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64  ction is invoked
2e280 2c 20 69 74 20 69 73 20 70 61 73 73 65 64 0a 2a  , it is passed.*
2e290 2a 20 61 73 20 69 74 73 20 66 69 72 73 74 20 70  * as its first p
2e2a0 61 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79 20  arameter a copy 
2e2b0 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61 73  of the void* pas
2e2c0 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74  sed as the fourt
2e2d0 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f  h argument.** to
2e2e0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2e2f0 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20 73  collation() or s
2e300 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2e310 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a  llation16()..**.
2e320 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67  ** The remaining
2e330 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68   arguments to th
2e340 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75  e application-su
2e350 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 61  pplied routine a
2e360 72 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c 0a  re two strings,.
2e370 2a 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 6e  ** each represen
2e380 74 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 68  ted by a (length
2e390 2c 20 64 61 74 61 29 20 70 61 69 72 20 61 6e 64  , data) pair and
2e3a0 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20   encoded in the 
2e3b0 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 74  encoding.** that
2e3c0 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74   was passed as t
2e3d0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
2e3e0 74 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61  t when the colla
2e3f0 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 61  tion sequence wa
2e400 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 2e  s.** registered.
2e410 20 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70 6c   {END}  The appl
2e420 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  ication defined 
2e430 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 6e  collation routin
2e440 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75  e should.** retu
2e450 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72  rn negative, zer
2e460 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66  o or positive if
2e470 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e   the first strin
2e480 67 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 0a  g is less than,.
2e490 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20  ** equal to, or 
2e4a0 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65  greater than the
2e4b0 20 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e 20   second string. 
2e4c0 69 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d 20  i.e. (STRING1 - 
2e4d0 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a 20  STRING2)..**.** 
2e4e0 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  The sqlite3_crea
2e4f0 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28  te_collation_v2(
2e500 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c  ) works like sql
2e510 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
2e520 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65 70  ation().** excep
2e530 74 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20  t that it takes 
2e540 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e  an extra argumen
2e550 74 20 77 68 69 63 68 20 69 73 20 61 20 64 65 73  t which is a des
2e560 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20 74  tructor for.** t
2e570 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20 54  he collation.  T
2e580 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 73  he destructor is
2e590 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   called when the
2e5a0 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a 2a   collation is.**
2e5b0 20 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 69   destroyed and i
2e5c0 73 20 70 61 73 73 65 64 20 61 20 63 6f 70 79 20  s passed a copy 
2e5d0 6f 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61  of the fourth pa
2e5e0 72 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70 6f  rameter void* po
2e5f0 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20  inter.** of the 
2e600 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2e610 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a 2a  ollation_v2()..*
2e620 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72 65  * Collations are
2e630 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20   destroyed when 
2e640 74 68 65 79 20 61 72 65 20 6f 76 65 72 72 69 64  they are overrid
2e650 64 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61 6c  den by later cal
2e660 6c 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6c  ls to the.** col
2e670 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20  lation creation 
2e680 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68 65  functions or whe
2e690 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  n the [database 
2e6a0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 63  connection] is c
2e6b0 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20 5b  losed.** using [
2e6c0 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d  sqlite3_close()]
2e6d0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
2e6e0 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  :  [sqlite3_coll
2e6f0 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d 20  ation_needed()] 
2e700 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  and [sqlite3_col
2e710 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28  lation_needed16(
2e720 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  )]..**.** Requir
2e730 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 36  ements:.** [H166
2e740 30 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 48 31  03] [H16604] [H1
2e750 36 36 30 36 5d 20 5b 48 31 36 36 30 39 5d 20 5b  6606] [H16609] [
2e760 48 31 36 36 31 32 5d 20 5b 48 31 36 36 31 35 5d  H16612] [H16615]
2e770 20 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 36 32   [H16618] [H1662
2e780 31 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 20 5b  1].** [H16624] [
2e790 48 31 36 36 32 37 5d 20 5b 48 31 36 36 33 30 5d  H16627] [H16630]
2e7a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2e7b0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
2e7c0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73  e_collation(.  s
2e7d0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73  qlite3*, .  cons
2e7e0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a  t char *zName, .
2e7f0 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20    int eTextRep, 
2e800 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28  .  void*,.  int(
2e810 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a  *xCompare)(void*
2e820 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
2e830 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
2e840 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ).);.SQLITE_API 
2e850 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
2e860 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28  te_collation_v2(
2e870 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20  .  sqlite3*, .  
2e880 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
2e890 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52  e, .  int eTextR
2e8a0 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20  ep, .  void*,.  
2e8b0 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76  int(*xCompare)(v
2e8c0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
2e8d0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
2e8e0 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78  oid*),.  void(*x
2e8f0 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a  Destroy)(void*).
2e900 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
2e910 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
2e920 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20  _collation16(.  
2e930 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e  sqlite3*, .  con
2e940 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a  st void *zName,.
2e950 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20    int eTextRep, 
2e960 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28  .  void*,.  int(
2e970 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a  *xCompare)(void*
2e980 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
2e990 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
2e9a0 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ).);../*.** CAPI
2e9b0 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20  3REF: Collation 
2e9c0 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73  Needed Callbacks
2e9d0 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30   {H16700} <S2030
2e9e0 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69  0>.**.** To avoi
2e9f0 64 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69  d having to regi
2ea00 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69  ster all collati
2ea10 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66  on sequences bef
2ea20 6f 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a  ore a database.*
2ea30 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61  * can be used, a
2ea40 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b   single callback
2ea50 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65   function may be
2ea60 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68   registered with
2ea70 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73   the.** [databas
2ea80 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f  e connection] to
2ea90 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65   be called whene
2eaa0 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64  ver an undefined
2eab0 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65   collation.** se
2eac0 71 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72  quence is requir
2ead0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
2eae0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67   function is reg
2eaf0 69 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68  istered using th
2eb00 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  e sqlite3_collat
2eb10 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49  ion_needed() API
2eb20 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20  ,.** then it is 
2eb30 70 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73  passed the names
2eb40 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f   of undefined co
2eb50 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2eb60 73 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20  s as strings.** 
2eb70 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38  encoded in UTF-8
2eb80 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71  . {H16703} If sq
2eb90 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
2eba0 6e 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73  needed16() is us
2ebb0 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73  ed,.** the names
2ebc0 20 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55   are passed as U
2ebd0 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65  TF-16 in machine
2ebe0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
2ebf0 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f  er..** A call to
2ec00 20 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e   either function
2ec10 20 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78   replaces any ex
2ec20 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e  isting callback.
2ec30 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
2ec40 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
2ec50 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61  ked, the first a
2ec60 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69  rgument passed i
2ec70 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74  s a copy.** of t
2ec80 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
2ec90 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  nt to sqlite3_co
2eca0 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29  llation_needed()
2ecb0 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63   or.** sqlite3_c
2ecc0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
2ecd0 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  6().  The second
2ece0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
2ecf0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e   database.** con
2ed00 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68  nection.  The th
2ed10 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ird argument is 
2ed20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55  one of [SQLITE_U
2ed30 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54  TF8], [SQLITE_UT
2ed40 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53  F16BE],.** or [S
2ed50 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20  QLITE_UTF16LE], 
2ed60 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d  indicating the m
2ed70 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f  ost desirable fo
2ed80 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74  rm of the collat
2ed90 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ion.** sequence 
2eda0 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65  function require
2edb0 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70  d.  The fourth p
2edc0 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
2edd0 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72  name of the.** r
2ede0 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f  equired collatio
2edf0 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a  n sequence..**.*
2ee00 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  * The callback f
2ee10 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72  unction should r
2ee20 65 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69  egister the desi
2ee30 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73  red collation us
2ee40 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ing.** [sqlite3_
2ee50 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
2ee60 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72  ()], [sqlite3_cr
2ee70 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36  eate_collation16
2ee80 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69  ()], or.** [sqli
2ee90 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
2eea0 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a  tion_v2()]..**.*
2eeb0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
2eec0 2a 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 31 36  ** [H16702] [H16
2eed0 37 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 2a 2f  704] [H16706].*/
2eee0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2eef0 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
2ef00 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c 69  n_needed(.  sqli
2ef10 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 20  te3*, .  void*, 
2ef20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  .  void(*)(void*
2ef30 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
2ef40 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61  extRep,const cha
2ef50 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  r*).);.SQLITE_AP
2ef60 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
2ef70 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
2ef80 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20  (.  sqlite3*, . 
2ef90 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28 2a   void*,.  void(*
2efa0 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
2efb0 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
2efc0 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f  nst void*).);../
2efd0 2a 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68 65  *.** Specify the
2efe0 20 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e 63 72   key for an encr
2eff0 79 70 74 65 64 20 64 61 74 61 62 61 73 65 2e 20  ypted database. 
2f000 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   This routine sh
2f010 6f 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c 65  ould be.** calle
2f020 64 20 72 69 67 68 74 20 61 66 74 65 72 20 73 71  d right after sq
2f030 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2a  lite3_open()..**
2f040 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20  .** The code to 
2f050 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41  implement this A
2f060 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  PI is not availa
2f070 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69  ble in the publi
2f080 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20  c release.** of 
2f090 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54  SQLite..*/.SQLIT
2f0a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2f0b0 33 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33  3_key(.  sqlite3
2f0c0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
2f0d0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2f0e0 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65  ase to be rekeye
2f0f0 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  d */.  const voi
2f100 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  d *pKey, int nKe
2f110 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  y     /* The key
2f120 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68   */.);../*.** Ch
2f130 61 6e 67 65 20 74 68 65 20 6b 65 79 20 6f 6e 20  ange the key on 
2f140 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  an open database
2f150 2e 20 20 49 66 20 74 68 65 20 63 75 72 72 65 6e  .  If the curren
2f160 74 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f  t database is no
2f170 74 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c 20  t.** encrypted, 
2f180 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
2f190 6c 20 65 6e 63 72 79 70 74 20 69 74 2e 20 20 49  l encrypt it.  I
2f1a0 66 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e 65  f pNew==0 or nNe
2f1b0 77 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61 74  w==0, the.** dat
2f1c0 61 62 61 73 65 20 69 73 20 64 65 63 72 79 70 74  abase is decrypt
2f1d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ed..**.** The co
2f1e0 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
2f1f0 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74 20  this API is not 
2f200 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65  available in the
2f210 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65 0a   public release.
2f220 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f  ** of SQLite..*/
2f230 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2f240 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a 20  sqlite3_rekey(. 
2f250 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
2f260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f270 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 62  /* Database to b
2f280 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63  e rekeyed */.  c
2f290 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
2f2a0 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a   int nKey     /*
2f2b0 20 54 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f 0a   The new key */.
2f2c0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
2f2d0 45 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65 63  EF: Suspend Exec
2f2e0 75 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72  ution For A Shor
2f2f0 74 20 54 69 6d 65 20 7b 48 31 30 35 33 30 7d 20  t Time {H10530} 
2f300 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S40410>.**.** T
2f310 68 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70  he sqlite3_sleep
2f320 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73  () function caus
2f330 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  es the current t
2f340 68 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e 64  hread to suspend
2f350 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66 6f   execution.** fo
2f360 72 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75 6d  r at least a num
2f370 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f  ber of milliseco
2f380 6e 64 73 20 73 70 65 63 69 66 69 65 64 20 69 6e  nds specified in
2f390 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e 0a   its parameter..
2f3a0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 65  **.** If the ope
2f3b0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 6f  rating system do
2f3c0 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73  es not support s
2f3d0 6c 65 65 70 20 72 65 71 75 65 73 74 73 20 77 69  leep requests wi
2f3e0 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f 6e  th.** millisecon
2f3f0 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69 6f  d time resolutio
2f400 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d 65  n, then the time
2f410 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65 64   will be rounded
2f420 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 65   up to.** the ne
2f430 61 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54 68  arest second. Th
2f440 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c  e number of mill
2f450 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65  iseconds of slee
2f460 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65  p actually.** re
2f470 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65  quested from the
2f480 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
2f490 6d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  m is returned..*
2f4a0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c  *.** SQLite impl
2f4b0 65 6d 65 6e 74 73 20 74 68 69 73 20 69 6e 74 65  ements this inte
2f4c0 72 66 61 63 65 20 62 79 20 63 61 6c 6c 69 6e 67  rface by calling
2f4d0 20 74 68 65 20 78 53 6c 65 65 70 28 29 0a 2a 2a   the xSleep().**
2f4e0 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 64   method of the d
2f4f0 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f  efault [sqlite3_
2f500 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a  vfs] object..**.
2f510 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2f520 20 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 35 33   [H10533] [H1053
2f530 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  6].*/.SQLITE_API
2f540 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c 65   int sqlite3_sle
2f550 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ep(int);../*.** 
2f560 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f  CAPI3REF: Name O
2f570 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c  f The Folder Hol
2f580 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20 46  ding Temporary F
2f590 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c 53  iles {H10310} <S
2f5a0 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 20  20000>.**.** If 
2f5b0 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69  this global vari
2f5c0 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f 20  able is made to 
2f5d0 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e  point to a strin
2f5e0 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68  g which is.** th
2f5f0 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64  e name of a fold
2f600 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63  er (a.k.a. direc
2f610 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20  tory), then all 
2f620 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 0a  temporary files.
2f630 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 53 51  ** created by SQ
2f640 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c 61  Lite will be pla
2f650 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 72 65  ced in that dire
2f660 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73 20  ctory.  If this 
2f670 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 61  variable.** is a
2f680 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74   NULL pointer, t
2f690 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 6f  hen SQLite perfo
2f6a0 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f 72  rms a search for
2f6b0 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 0a   an appropriate.
2f6c0 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  ** temporary fil
2f6d0 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a  e directory..**.
2f6e0 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66  ** It is not saf
2f6f0 65 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64  e to read or mod
2f700 69 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c  ify this variabl
2f710 65 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f  e in more than o
2f720 6e 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 74 20  ne.** thread at 
2f730 61 20 74 69 6d 65 2e 20 20 49 74 20 69 73 20 6e  a time.  It is n
2f740 6f 74 20 73 61 66 65 20 74 6f 20 72 65 61 64 20  ot safe to read 
2f750 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76  or modify this v
2f760 61 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 61 20  ariable.** if a 
2f770 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
2f780 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 75  tion] is being u
2f790 73 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20  sed at the same 
2f7a0 74 69 6d 65 20 69 6e 20 61 20 73 65 70 61 72 61  time in a separa
2f7b0 74 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a  te.** thread..**
2f7c0 20 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20   It is intended 
2f7d0 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62  that this variab
2f7e0 6c 65 20 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a  le be set once.*
2f7f0 2a 20 61 73 20 70 61 72 74 20 6f 66 20 70 72 6f  * as part of pro
2f800 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74  cess initializat
2f810 69 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 20 61  ion and before a
2f820 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  ny SQLite interf
2f830 61 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  ace.** routines 
2f840 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64  have been called
2f850 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 76   and that this v
2f860 61 72 69 61 62 6c 65 20 72 65 6d 61 69 6e 20 75  ariable remain u
2f870 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 65 72  nchanged.** ther
2f880 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  eafter..**.** Th
2f890 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  e [temp_store_di
2f8a0 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20  rectory pragma] 
2f8b0 6d 61 79 20 6d 6f 64 69 66 79 20 74 68 69 73 20  may modify this 
2f8c0 76 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61 75  variable and cau
2f8d0 73 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 6e  se.** it to poin
2f8e0 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  t to memory obta
2f8f0 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74  ined from [sqlit
2f900 65 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 75 72  e3_malloc].  Fur
2f910 74 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68 65  thermore,.** the
2f920 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72   [temp_store_dir
2f930 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 61  ectory pragma] a
2f940 6c 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 68  lways assumes th
2f950 61 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a 2a  at any string.**
2f960 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61   that this varia
2f970 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73  ble points to is
2f980 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20   held in memory 
2f990 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a 2a  obtained from .*
2f9a0 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  * [sqlite3_mallo
2f9b0 63 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67 6d  c] and the pragm
2f9c0 61 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f  a may attempt to
2f9d0 20 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f 72   free that memor
2f9e0 79 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69  y.** using [sqli
2f9f0 74 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48 65  te3_free]..** He
2fa00 6e 63 65 2c 20 69 66 20 74 68 69 73 20 76 61 72  nce, if this var
2fa10 69 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69 65  iable is modifie
2fa20 64 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74 68  d directly, eith
2fa30 65 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a  er it should be.
2fa40 2a 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72 20  ** made NULL or 
2fa50 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f  made to point to
2fa60 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
2fa70 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
2fa80 61 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c 73  alloc].** or els
2fa90 65 20 74 68 65 20 75 73 65 20 6f 66 20 74 68 65  e the use of the
2faa0 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72   [temp_store_dir
2fab0 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 73  ectory pragma] s
2fac0 68 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65 64  hould be avoided
2fad0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2fae0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65  char *sqlite3_te
2faf0 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a 2f  mp_directory;../
2fb00 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54  *.** CAPI3REF: T
2fb10 65 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 6f 6d  est For Auto-Com
2fb20 6d 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 33 30  mit Mode {H12930
2fb30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 4b 45  } <S60200>.** KE
2fb40 59 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 6f 6d  YWORDS: {autocom
2fb50 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20  mit mode}.**.** 
2fb60 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  The sqlite3_get_
2fb70 61 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 6e 74  autocommit() int
2fb80 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 6e  erface returns n
2fb90 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 7a 65  on-zero or.** ze
2fba0 72 6f 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  ro if the given 
2fbb0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2fbc0 69 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f 74  ion is or is not
2fbd0 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d   in autocommit m
2fbe0 6f 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 74 69  ode,.** respecti
2fbf0 76 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d 6d 69  vely.  Autocommi
2fc00 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20  t mode is on by 
2fc10 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 74 6f  default..** Auto
2fc20 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 64  commit mode is d
2fc30 69 73 61 62 6c 65 64 20 62 79 20 61 20 5b 42 45  isabled by a [BE
2fc40 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 2e 0a  GIN] statement..
2fc50 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  ** Autocommit mo
2fc60 64 65 20 69 73 20 72 65 2d 65 6e 61 62 6c 65 64  de is re-enabled
2fc70 20 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 6f   by a [COMMIT] o
2fc80 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a  r [ROLLBACK]..**
2fc90 0a 2a 2a 20 49 66 20 63 65 72 74 61 69 6e 20 6b  .** If certain k
2fca0 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 6f  inds of errors o
2fcb0 63 63 75 72 20 6f 6e 20 61 20 73 74 61 74 65 6d  ccur on a statem
2fcc0 65 6e 74 20 77 69 74 68 69 6e 20 61 20 6d 75 6c  ent within a mul
2fcd0 74 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  ti-statement.** 
2fce0 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 72 72  transaction (err
2fcf0 6f 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 5b 53  ors including [S
2fd00 51 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b 53 51  QLITE_FULL], [SQ
2fd10 4c 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a 2a 20  LITE_IOERR],.** 
2fd20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20  [SQLITE_NOMEM], 
2fd30 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20 61  [SQLITE_BUSY], a
2fd40 6e 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52  nd [SQLITE_INTER
2fd50 52 55 50 54 5d 29 20 74 68 65 6e 20 74 68 65 0a  RUPT]) then the.
2fd60 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d  ** transaction m
2fd70 69 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 20 62  ight be rolled b
2fd80 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ack automaticall
2fd90 79 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 79  y.  The only way
2fda0 20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 74 20   to.** find out 
2fdb0 77 68 65 74 68 65 72 20 53 51 4c 69 74 65 20 61  whether SQLite a
2fdc0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f 6c  utomatically rol
2fdd0 6c 65 64 20 62 61 63 6b 20 74 68 65 20 74 72 61  led back the tra
2fde0 6e 73 61 63 74 69 6f 6e 20 61 66 74 65 72 0a 2a  nsaction after.*
2fdf0 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 6f  * an error is to
2fe00 20 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69   use this functi
2fe10 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f  on..**.** If ano
2fe20 74 68 65 72 20 74 68 72 65 61 64 20 63 68 61 6e  ther thread chan
2fe30 67 65 73 20 74 68 65 20 61 75 74 6f 63 6f 6d 6d  ges the autocomm
2fe40 69 74 20 73 74 61 74 75 73 20 6f 66 20 74 68 65  it status of the
2fe50 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e   database.** con
2fe60 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68  nection while th
2fe70 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75  is routine is ru
2fe80 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20  nning, then the 
2fe90 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
2fea0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a  is undefined..**
2feb0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
2fec0 3a 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 32 39  : [H12931] [H129
2fed0 33 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 48 31  32] [H12933] [H1
2fee0 32 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  2934].*/.SQLITE_
2fef0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2ff00 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73  get_autocommit(s
2ff10 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  qlite3*);../*.**
2ff20 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 20   CAPI3REF: Find 
2ff30 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61 6e  The Database Han
2ff40 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 65  dle Of A Prepare
2ff50 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33  d Statement {H13
2ff60 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 2a  120} <S60600>.**
2ff70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2ff80 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 66  db_handle interf
2ff90 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ace returns the 
2ffa0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
2ffb0 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20  tion] handle.** 
2ffc0 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 70  to which a [prep
2ffd0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
2ffe0 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 5b 64  belongs.  The [d
2fff0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
30000 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  on].** returned 
30010 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61  by sqlite3_db_ha
30020 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65  ndle is the same
30030 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
30040 63 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 73 20  ction] that was 
30050 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
30060 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73 71  nt.** to the [sq
30070 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
30080 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 74 73  ()] call (or its
30090 20 76 61 72 69 61 6e 74 73 29 20 74 68 61 74 20   variants) that 
300a0 77 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63  was used to.** c
300b0 72 65 61 74 65 20 74 68 65 20 73 74 61 74 65 6d  reate the statem
300c0 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74  ent in the first
300d0 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 65   place..**.** Re
300e0 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 33  quirements: [H13
300f0 31 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  123].*/.SQLITE_A
30100 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69  PI sqlite3 *sqli
30110 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71  te3_db_handle(sq
30120 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f  lite3_stmt*);../
30130 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46  *.** CAPI3REF: F
30140 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72 65  ind the next pre
30150 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
30160 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30 30  {H13140} <S60600
30170 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  >.**.** This int
30180 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
30190 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
301a0 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 20 73  next [prepared s
301b0 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72 0a  tatement] after.
301c0 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69 61  ** pStmt associa
301d0 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61  ted with the [da
301e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
301f0 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d  n] pDb.  If pStm
30200 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 65  t is NULL.** the
30210 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65  n this interface
30220 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
30230 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  er to the first 
30240 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
30250 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  nt.** associated
30260 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
30270 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44  se connection pD
30280 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61 72  b.  If no prepar
30290 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  ed statement.** 
302a0 73 61 74 69 73 66 69 65 73 20 74 68 65 20 63 6f  satisfies the co
302b0 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69 73  nditions of this
302c0 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65 74   routine, it ret
302d0 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  urns NULL..**.**
302e0 20 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   The [database c
302f0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 6e 74  onnection] point
30300 65 72 20 44 20 69 6e 20 61 20 63 61 6c 6c 20 74  er D in a call t
30310 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6e 65  o.** [sqlite3_ne
30320 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 6d 75  xt_stmt(D,S)] mu
30330 73 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 6f  st refer to an o
30340 70 65 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  pen database.** 
30350 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 20 69  connection and i
30360 6e 20 70 61 72 74 69 63 75 6c 61 72 20 6d 75 73  n particular mus
30370 74 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c 4c 20  t not be a NULL 
30380 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 52  pointer..**.** R
30390 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31  equirements: [H1
303a0 33 31 34 33 5d 20 5b 48 31 33 31 34 36 5d 20 5b  3143] [H13146] [
303b0 48 31 33 31 34 39 5d 20 5b 48 31 33 31 35 32 5d  H13149] [H13152]
303c0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
303d0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71 6c  qlite3_stmt *sql
303e0 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 73  ite3_next_stmt(s
303f0 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71 6c  qlite3 *pDb, sql
30400 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
30410 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
30420 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20 52  EF: Commit And R
30430 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63 61  ollback Notifica
30440 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b  tion Callbacks {
30450 48 31 32 39 35 30 7d 20 3c 53 36 30 34 30 30 3e  H12950} <S60400>
30460 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
30470 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29  e3_commit_hook()
30480 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73   interface regis
30490 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a  ters a callback.
304a0 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ** function to b
304b0 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76  e invoked whenev
304c0 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  er a transaction
304d0 20 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 63 6f   is [COMMIT | co
304e0 6d 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 41 6e 79  mmitted]..** Any
304f0 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79   callback set by
30500 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c   a previous call
30510 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d   to sqlite3_comm
30520 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72  it_hook().** for
30530 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
30540 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
30550 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20   overridden..** 
30560 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c  The sqlite3_roll
30570 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e 74 65  back_hook() inte
30580 72 66 61 63 65 20 72 65 67 69 73 74 65 72 73 20  rface registers 
30590 61 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75  a callback.** fu
305a0 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76  nction to be inv
305b0 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 20  oked whenever a 
305c0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 5b  transaction is [
305d0 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c 6c 65  ROLLBACK | rolle
305e0 64 20 62 61 63 6b 5d 2e 0a 2a 2a 20 41 6e 79 20  d back]..** Any 
305f0 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20  callback set by 
30600 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20  a previous call 
30610 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69  to sqlite3_commi
30620 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72 20  t_hook().** for 
30630 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
30640 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
30650 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20 54  overridden..** T
30660 68 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e 74  he pArg argument
30670 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75   is passed throu
30680 67 68 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  gh to the callba
30690 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 61  ck..** If the ca
306a0 6c 6c 62 61 63 6b 20 6f 6e 20 61 20 63 6f 6d 6d  llback on a comm
306b0 69 74 20 68 6f 6f 6b 20 66 75 6e 63 74 69 6f 6e  it hook function
306c0 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
306d0 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63  o,.** then the c
306e0 6f 6d 6d 69 74 20 69 73 20 63 6f 6e 76 65 72 74  ommit is convert
306f0 65 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c 62 61  ed into a rollba
30700 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f  ck..**.** If ano
30710 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61  ther function wa
30720 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67  s previously reg
30730 69 73 74 65 72 65 64 2c 20 69 74 73 0a 2a 2a 20  istered, its.** 
30740 70 41 72 67 20 76 61 6c 75 65 20 69 73 20 72 65  pArg value is re
30750 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69  turned.  Otherwi
30760 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  se NULL is retur
30770 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ned..**.** The c
30780 61 6c 6c 62 61 63 6b 20 69 6d 70 6c 65 6d 65 6e  allback implemen
30790 74 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20  tation must not 
307a0 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74  do anything that
307b0 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20   will modify.** 
307c0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
307d0 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76  nection that inv
307e0 6f 6b 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63  oked the callbac
307f0 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a  k.  Any actions.
30800 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65  ** to modify the
30810 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
30820 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66  tion must be def
30830 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65  erred until afte
30840 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74  r the.** complet
30850 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  ion of the [sqli
30860 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c  te3_step()] call
30870 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20   that triggered 
30880 74 68 65 20 63 6f 6d 6d 69 74 0a 2a 2a 20 6f 72  the commit.** or
30890 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 69   rollback hook i
308a0 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63  n the first plac
308b0 65 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  e..** Note that 
308c0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
308d0 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  _v2()] and [sqli
308e0 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68  te3_step()] both
308f0 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a   modify their.**
30900 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
30910 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65  tions for the me
30920 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79  aning of "modify
30930 22 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72  " in this paragr
30940 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73  aph..**.** Regis
30950 74 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20 66 75  tering a NULL fu
30960 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20  nction disables 
30970 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a  the callback..**
30980 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f 6d  .** When the com
30990 6d 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63  mit hook callbac
309a0 6b 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  k routine return
309b0 73 20 7a 65 72 6f 2c 20 74 68 65 20 5b 43 4f 4d  s zero, the [COM
309c0 4d 49 54 5d 0a 2a 2a 20 6f 70 65 72 61 74 69 6f  MIT].** operatio
309d0 6e 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20  n is allowed to 
309e0 63 6f 6e 74 69 6e 75 65 20 6e 6f 72 6d 61 6c 6c  continue normall
309f0 79 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d 69  y.  If the commi
30a00 74 20 68 6f 6f 6b 0a 2a 2a 20 72 65 74 75 72 6e  t hook.** return
30a10 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
30a20 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 20 69 73   the [COMMIT] is
30a30 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
30a40 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a  a [ROLLBACK]..**
30a50 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f   The rollback ho
30a60 6f 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  ok is invoked on
30a70 20 61 20 72 6f 6c 6c 62 61 63 6b 20 74 68 61 74   a rollback that
30a80 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 61 20   results from a 
30a90 63 6f 6d 6d 69 74 0a 2a 2a 20 68 6f 6f 6b 20 72  commit.** hook r
30aa0 65 74 75 72 6e 69 6e 67 20 6e 6f 6e 2d 7a 65 72  eturning non-zer
30ab0 6f 2c 20 6a 75 73 74 20 61 73 20 69 74 20 77 6f  o, just as it wo
30ac0 75 6c 64 20 62 65 20 77 69 74 68 20 61 6e 79 20  uld be with any 
30ad0 6f 74 68 65 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a  other rollback..
30ae0 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75  **.** For the pu
30af0 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 41  rposes of this A
30b00 50 49 2c 20 61 20 74 72 61 6e 73 61 63 74 69 6f  PI, a transactio
30b10 6e 20 69 73 20 73 61 69 64 20 74 6f 20 68 61 76  n is said to hav
30b20 65 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c 65 64  e been.** rolled
30b30 20 62 61 63 6b 20 69 66 20 61 6e 20 65 78 70 6c   back if an expl
30b40 69 63 69 74 20 22 52 4f 4c 4c 42 41 43 4b 22 20  icit "ROLLBACK" 
30b50 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78 65  statement is exe
30b60 63 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 6e 20  cuted, or.** an 
30b70 65 72 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72 61  error or constra
30b80 69 6e 74 20 63 61 75 73 65 73 20 61 6e 20 69 6d  int causes an im
30b90 70 6c 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b 20  plicit rollback 
30ba0 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 54 68 65  to occur..** The
30bb0 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61   rollback callba
30bc0 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65  ck is not invoke
30bd0 64 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69  d if a transacti
30be0 6f 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d 61 74  on is.** automat
30bf0 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61  ically rolled ba
30c00 63 6b 20 62 65 63 61 75 73 65 20 74 68 65 20 64  ck because the d
30c10 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
30c20 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a  on is closed..**
30c30 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61   The rollback ca
30c40 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e  llback is not in
30c50 76 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e 73  voked if a trans
30c60 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 72 6f 6c  action is.** rol
30c70 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 73 65  led back because
30c80 20 61 20 63 6f 6d 6d 69 74 20 63 61 6c 6c 62 61   a commit callba
30c90 63 6b 20 72 65 74 75 72 6e 65 64 20 6e 6f 6e 2d  ck returned non-
30ca0 7a 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 6f 3e 20  zero..** <todo> 
30cb0 43 68 65 63 6b 20 6f 6e 20 74 68 69 73 20 3c 2f  Check on this </
30cc0 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  todo>.**.** See 
30cd0 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65  also the [sqlite
30ce0 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 5d  3_update_hook()]
30cf0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a   interface..**.*
30d00 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
30d10 2a 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 31 32  ** [H12951] [H12
30d20 39 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 5b 48  952] [H12953] [H
30d30 31 32 39 35 34 5d 20 5b 48 31 32 39 35 35 5d 0a  12954] [H12955].
30d40 2a 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 31 32  ** [H12961] [H12
30d50 39 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 5b 48  962] [H12963] [H
30d60 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12964].*/.SQLITE
30d70 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
30d80 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 73  e3_commit_hook(s
30d90 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28  qlite3*, int(*)(
30da0 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a  void*), void*);.
30db0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
30dc0 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63  *sqlite3_rollbac
30dd0 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c  k_hook(sqlite3*,
30de0 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29   void(*)(void *)
30df0 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  , void*);../*.**
30e00 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 20   CAPI3REF: Data 
30e10 43 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 61 74  Change Notificat
30e20 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48  ion Callbacks {H
30e30 31 32 39 37 30 7d 20 3c 53 36 30 34 30 30 3e 0a  12970} <S60400>.
30e40 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
30e50 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 20  3_update_hook() 
30e60 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74  interface regist
30e70 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66  ers a callback f
30e80 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 68 20  unction.** with 
30e90 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
30ea0 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e 74 69  nnection] identi
30eb0 66 69 65 64 20 62 79 20 74 68 65 20 66 69 72 73  fied by the firs
30ec0 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f  t argument.** to
30ed0 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e   be invoked when
30ee0 65 76 65 72 20 61 20 72 6f 77 20 69 73 20 75 70  ever a row is up
30ef0 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20  dated, inserted 
30f00 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 41  or deleted..** A
30f10 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20  ny callback set 
30f20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61  by a previous ca
30f30 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  ll to this funct
30f40 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73  ion.** for the s
30f50 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
30f60 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72  nection is overr
30f70 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  idden..**.** The
30f80 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
30f90 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
30fa0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f   the function to
30fb0 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0a 2a   invoke when a.*
30fc0 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64  * row is updated
30fd0 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65  , inserted or de
30fe0 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 66 69  leted..** The fi
30ff0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
31000 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  the callback is 
31010 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74 68  a copy of the th
31020 69 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ird argument.** 
31030 74 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74  to sqlite3_updat
31040 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68 65  e_hook()..** The
31050 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63 6b   second callback
31060 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65   argument is one
31070 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45   of [SQLITE_INSE
31080 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c  RT], [SQLITE_DEL
31090 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c  ETE],.** or [SQL
310a0 49 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65 70  ITE_UPDATE], dep
310b0 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70  ending on the op
310c0 65 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61 75  eration that cau
310d0 73 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  sed the callback
310e0 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65  .** to be invoke
310f0 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20  d..** The third 
31100 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d  and fourth argum
31110 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c 6c  ents to the call
31120 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69  back contain poi
31130 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a 20  nters to the.** 
31140 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 61 62  database and tab
31150 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e 69  le name containi
31160 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 64 20  ng the affected 
31170 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 6e 61  row..** The fina
31180 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61 6d  l callback param
31190 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f 77  eter is the [row
311a0 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e 0a  id] of the row..
311b0 2a 2a 20 49 6e 20 74 68 65 20 63 61 73 65 20 6f  ** In the case o
311c0 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68 69  f an update, thi
311d0 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64 5d  s is the [rowid]
311e0 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74   after the updat
311f0 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a 2a  e takes place..*
31200 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20  *.** The update 
31210 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f  hook is not invo
31220 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e 61  ked when interna
31230 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73 20  l system tables 
31240 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20  are.** modified 
31250 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73  (i.e. sqlite_mas
31260 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f 73  ter and sqlite_s
31270 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a 20  equence)..**.** 
31280 49 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 69  In the current i
31290 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74  mplementation, t
312a0 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a 2a  he update hook.*
312b0 2a 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64  * is not invoked
312c0 20 77 68 65 6e 20 64 75 70 6c 69 63 61 74 69 6f   when duplicatio
312d0 6e 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74  n rows are delet
312e0 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 61 6e  ed because of an
312f0 0a 2a 2a 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54  .** [ON CONFLICT
31300 20 7c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 52   | ON CONFLICT R
31310 45 50 4c 41 43 45 5d 20 63 6c 61 75 73 65 2e 20  EPLACE] clause. 
31320 20 4e 6f 72 20 69 73 20 74 68 65 20 75 70 64 61   Nor is the upda
31330 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 6e 76 6f 6b  te hook.** invok
31340 65 64 20 77 68 65 6e 20 72 6f 77 73 20 61 72 65  ed when rows are
31350 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 74   deleted using t
31360 68 65 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 74  he [truncate opt
31370 69 6d 69 7a 61 74 69 6f 6e 5d 2e 0a 2a 2a 20 54  imization]..** T
31380 68 65 20 65 78 63 65 70 74 69 6f 6e 73 20 64 65  he exceptions de
31390 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20 70 61  fined in this pa
313a0 72 61 67 72 61 70 68 20 6d 69 67 68 74 20 63 68  ragraph might ch
313b0 61 6e 67 65 20 69 6e 20 61 20 66 75 74 75 72 65  ange in a future
313c0 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 66 20 53  .** release of S
313d0 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  QLite..**.** The
313e0 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d 70   update hook imp
313f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74  lementation must
31400 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67   not do anything
31410 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66   that will modif
31420 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  y.** the databas
31430 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61  e connection tha
31440 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75 70  t invoked the up
31450 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79 20  date hook.  Any 
31460 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f  actions.** to mo
31470 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73  dify the databas
31480 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73  e connection mus
31490 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75 6e  t be deferred un
314a0 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a 2a  til after the.**
314b0 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74   completion of t
314c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  he [sqlite3_step
314d0 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72  ()] call that tr
314e0 69 67 67 65 72 65 64 20 74 68 65 20 75 70 64 61  iggered the upda
314f0 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f 74 65  te hook..** Note
31500 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70   that [sqlite3_p
31510 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64  repare_v2()] and
31520 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
31530 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68  ] both modify th
31540 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  eir.** database 
31550 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20  connections for 
31560 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22  the meaning of "
31570 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20  modify" in this 
31580 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a  paragraph..**.**
31590 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63   If another func
315a0 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75  tion was previou
315b0 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c 20  sly registered, 
315c0 69 74 73 20 70 41 72 67 20 76 61 6c 75 65 0a 2a  its pArg value.*
315d0 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  * is returned.  
315e0 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69  Otherwise NULL i
315f0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
31600 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b  * See also the [
31610 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68  sqlite3_commit_h
31620 6f 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  ook()] and [sqli
31630 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f  te3_rollback_hoo
31640 6b 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63  k()].** interfac
31650 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  es..**.** Requir
31660 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39  ements:.** [H129
31670 37 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 48 31  71] [H12973] [H1
31680 32 39 37 35 5d 20 5b 48 31 32 39 37 37 5d 20 5b  2975] [H12977] [
31690 48 31 32 39 37 39 5d 20 5b 48 31 32 39 38 31 5d  H12979] [H12981]
316a0 20 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 39 38   [H12983] [H1298
316b0 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  6].*/.SQLITE_API
316c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75   void *sqlite3_u
316d0 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 71  pdate_hook(.  sq
316e0 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 28  lite3*, .  void(
316f0 2a 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c 63  *)(void *,int ,c
31700 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72  har const *,char
31710 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 33   const *,sqlite3
31720 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 2a  _int64),.  void*
31730 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
31740 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44  REF: Enable Or D
31750 69 73 61 62 6c 65 20 53 68 61 72 65 64 20 50 61  isable Shared Pa
31760 67 65 72 20 43 61 63 68 65 20 7b 48 31 30 33 33  ger Cache {H1033
31770 30 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 20 4b  0} <S30900>.** K
31780 45 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65 64  EYWORDS: {shared
31790 20 63 61 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 54 68   cache}.**.** Th
317a0 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c  is routine enabl
317b0 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74  es or disables t
317c0 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74 68  he sharing of th
317d0 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65  e database cache
317e0 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20 64  .** and schema d
317f0 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 62  ata structures b
31800 65 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73 65  etween [database
31810 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63 6f   connection | co
31820 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74 6f  nnections].** to
31830 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
31840 73 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20 65  se. Sharing is e
31850 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72  nabled if the ar
31860 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a 2a  gument is true.*
31870 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 69  * and disabled i
31880 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69  f the argument i
31890 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 43  s false..**.** C
318a0 61 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20  ache sharing is 
318b0 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73 61  enabled and disa
318c0 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74 69  bled for an enti
318d0 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 20 54  re process..** T
318e0 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20  his is a change 
318f0 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72  as of SQLite ver
31900 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70  sion 3.5.0. In p
31910 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66  rior versions of
31920 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 72   SQLite,.** shar
31930 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20  ing was enabled 
31940 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20  or disabled for 
31950 65 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61  each thread sepa
31960 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rately..**.** Th
31970 65 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 20  e cache sharing 
31980 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 73  mode set by this
31990 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 63   interface effec
319a0 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e  ts all subsequen
319b0 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73  t.** calls to [s
319c0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
319d0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
319e0 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
319f0 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20  3_open16()]..** 
31a00 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73  Existing databas
31a10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f  e connections co
31a20 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 73  ntinue use the s
31a30 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74  haring mode.** t
31a40 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 63  hat was in effec
31a50 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 68  t at the time th
31a60 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e 0a  ey were opened..
31a70 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61  **.** Virtual ta
31a80 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 75  bles cannot be u
31a90 73 65 64 20 77 69 74 68 20 61 20 73 68 61 72 65  sed with a share
31aa0 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 73  d cache.  When s
31ab0 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69  hared.** cache i
31ac0 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b  s enabled, the [
31ad0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
31ae0 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 65  odule()] API use
31af0 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a  d to register.**
31b00 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
31b10 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75  will always retu
31b20 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a  rn an error..**.
31b30 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
31b40 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
31b50 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61  OK] if shared ca
31b60 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20  che was enabled 
31b70 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 73  or disabled.** s
31b80 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e  uccessfully.  An
31b90 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73   [error code] is
31ba0 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77   returned otherw
31bb0 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65  ise..**.** Share
31bc0 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61 62  d cache is disab
31bd0 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20  led by default. 
31be0 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 63  But this might c
31bf0 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75  hange in.** futu
31c00 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53  re releases of S
31c10 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 74  QLite.  Applicat
31c20 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 61  ions that care a
31c30 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 63  bout shared.** c
31c40 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 6f  ache setting sho
31c50 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c 69  uld set it expli
31c60 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  citly..**.** See
31c70 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 65 20   Also:  [SQLite 
31c80 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f 64  Shared-Cache Mod
31c90 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  e].**.** Require
31ca0 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 5d 20  ments: [H10331] 
31cb0 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 33 33 37  [H10336] [H10337
31cc0 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 53 51  ] [H10339].*/.SQ
31cd0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
31ce0 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
31cf0 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a  ed_cache(int);..
31d00 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
31d10 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65 20  Attempt To Free 
31d20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 31 37  Heap Memory {H17
31d30 33 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a  340} <S30220>.**
31d40 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
31d50 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29  release_memory()
31d60 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d   interface attem
31d70 70 74 73 20 74 6f 20 66 72 65 65 20 4e 20 62 79  pts to free N by
31d80 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 20 6d  tes.** of heap m
31d90 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f 63  emory by dealloc
31da0 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e 74  ating non-essent
31db0 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ial memory alloc
31dc0 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20 62  ations.** held b
31dd0 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  y the database l
31de0 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 20 4d  ibrary. {END}  M
31df0 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63 61  emory used to ca
31e00 63 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  che database.** 
31e10 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76 65  pages to improve
31e20 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 20   performance is 
31e30 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e 6f  an example of no
31e40 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f  n-essential memo
31e50 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72  ry..** sqlite3_r
31e60 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
31e70 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
31e80 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75  er of bytes actu
31e90 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a 20 77  ally freed,.** w
31ea0 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d 6f  hich might be mo
31eb0 72 65 20 6f 72 20 6c 65 73 73 20 74 68 61 6e 20  re or less than 
31ec0 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75 65  the amount reque
31ed0 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  sted..**.** Requ
31ee0 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 33 34  irements: [H1734
31ef0 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 0a 53  1] [H17342].*/.S
31f00 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
31f10 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
31f20 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  mory(int);../*.*
31f30 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70 6f  * CAPI3REF: Impo
31f40 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48 65  se A Limit On He
31f50 61 70 20 53 69 7a 65 20 7b 48 31 37 33 35 30 7d  ap Size {H17350}
31f60 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20   <S30220>.**.** 
31f70 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 66 74  The sqlite3_soft
31f80 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 69 6e  _heap_limit() in
31f90 74 65 72 66 61 63 65 20 70 6c 61 63 65 73 20 61  terface places a
31fa0 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a 2a 2a   "soft" limit.**
31fb0 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f   on the amount o
31fc0 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74 68  f heap memory th
31fd0 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61  at may be alloca
31fe0 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a  ted by SQLite..*
31ff0 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c  * If an internal
32000 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 72   allocation is r
32010 65 71 75 65 73 74 65 64 20 74 68 61 74 20 77 6f  equested that wo
32020 75 6c 64 20 65 78 63 65 65 64 20 74 68 65 0a 2a  uld exceed the.*
32030 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69  * soft heap limi
32040 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65  t, [sqlite3_rele
32050 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 73  ase_memory()] is
32060 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f 72 0a   invoked one or.
32070 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f  ** more times to
32080 20 66 72 65 65 20 75 70 20 73 6f 6d 65 20 73 70   free up some sp
32090 61 63 65 20 62 65 66 6f 72 65 20 74 68 65 20 61  ace before the a
320a0 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 65 72  llocation is per
320b0 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  formed..**.** Th
320c0 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c 65  e limit is calle
320d0 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75 73  d "soft", becaus
320e0 65 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 72 65  e if [sqlite3_re
320f0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 0a  lease_memory()].
32100 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 20 73  ** cannot free s
32110 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 79  ufficient memory
32120 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 20   to prevent the 
32130 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 6e 67  limit from being
32140 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 74 68   exceeded,.** th
32150 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f  e memory is allo
32160 63 61 74 65 64 20 61 6e 79 77 61 79 20 61 6e 64  cated anyway and
32170 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65   the current ope
32180 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 2e  ration proceeds.
32190 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76  .**.** A negativ
321a0 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 65 20  e or zero value 
321b0 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 61 74  for N means that
321c0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f 66   there is no sof
321d0 74 20 68 65 61 70 20 6c 69 6d 69 74 20 61 6e 64  t heap limit and
321e0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c  .** [sqlite3_rel
321f0 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 77  ease_memory()] w
32200 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c  ill only be call
32210 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 69  ed when memory i
32220 73 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a 20  s exhausted..** 
32230 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  The default valu
32240 65 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68  e for the soft h
32250 65 61 70 20 6c 69 6d 69 74 20 69 73 20 7a 65 72  eap limit is zer
32260 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  o..**.** SQLite 
32270 6d 61 6b 65 73 20 61 20 62 65 73 74 20 65 66 66  makes a best eff
32280 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 68 65  ort to honor the
32290 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
322a0 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20  ..** But if the 
322b0 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20  soft heap limit 
322c0 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f 72 65  cannot be honore
322d0 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69 6c  d, execution wil
322e0 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77 69  l.** continue wi
322f0 74 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20 6e  thout error or n
32300 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54 68  otification.  Th
32310 69 73 20 69 73 20 77 68 79 20 74 68 65 20 6c 69  is is why the li
32320 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64  mit is.** called
32330 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 2e   a "soft" limit.
32340 20 20 49 74 20 69 73 20 61 64 76 69 73 6f 72 79    It is advisory
32350 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 72 69   only..**.** Pri
32360 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65 72  or to SQLite ver
32370 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69 73  sion 3.5.0, this
32380 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f   routine only co
32390 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d 65  nstrained the me
323a0 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  mory.** allocate
323b0 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 68  d by a single th
323c0 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65 20  read - the same 
323d0 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 20  thread in which 
323e0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
323f0 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e 67  runs.  Beginning
32400 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 65 72   with SQLite ver
32410 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65 20  sion 3.5.0, the 
32420 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20  soft heap limit 
32430 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74 6f  is.** applied to
32440 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 54 68   all threads. Th
32450 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65  e value specifie
32460 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68  d for the soft h
32470 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 73 20  eap limit.** is 
32480 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f  an upper bound o
32490 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 6d 6f  n the total memo
324a0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f  ry allocation fo
324b0 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 49  r all threads. I
324c0 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 35  n.** version 3.5
324d0 2e 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  .0 there is no m
324e0 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c 69 6d  echanism for lim
324f0 69 74 69 6e 67 20 74 68 65 20 68 65 61 70 20 75  iting the heap u
32500 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e 64 69  sage for.** indi
32510 76 69 64 75 61 6c 20 74 68 72 65 61 64 73 2e 0a  vidual threads..
32520 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
32530 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 5d 20  ts:.** [H16351] 
32540 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 33 35 33  [H16352] [H16353
32550 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 36 33  ] [H16354] [H163
32560 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 2f 0a  55] [H16358].*/.
32570 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
32580 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61  sqlite3_soft_hea
32590 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f  p_limit(int);../
325a0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45  *.** CAPI3REF: E
325b0 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20  xtract Metadata 
325c0 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f  About A Column O
325d0 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 35  f A Table {H1285
325e0 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a  0} <S60300>.**.*
325f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
32600 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 20  eturns metadata 
32610 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63  about a specific
32620 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 65   column of a spe
32630 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 73  cific.** databas
32640 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 62  e table accessib
32650 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 61  le using the [da
32660 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
32670 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 73  n] handle.** pas
32680 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
32690 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
326a0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  nt..**.** The co
326b0 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69  lumn is identifi
326c0 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  ed by the second
326d0 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72  , third and four
326e0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  th parameters to
326f0 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
32700 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61  n. The second pa
32710 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65  rameter is eithe
32720 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  r the name of th
32730 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69  e database.** (i
32740 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d  .e. "main", "tem
32750 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65  p" or an attache
32760 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74  d database) cont
32770 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69  aining the speci
32780 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72  fied.** table or
32790 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20   NULL. If it is 
327a0 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61  NULL, then all a
327b0 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
327c0 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a  s are searched.*
327d0 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  * for the table 
327e0 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61  using the same a
327f0 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79  lgorithm used by
32800 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e   the database en
32810 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c  gine to.** resol
32820 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74  ve unqualified t
32830 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e  able references.
32840 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  .**.** The third
32850 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61   and fourth para
32860 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66  meters to this f
32870 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20  unction are the 
32880 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
32890 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  .** name of the 
328a0 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20  desired column, 
328b0 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65  respectively. Ne
328c0 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70  ither of these p
328d0 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79  arameters.** may
328e0 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   be NULL..**.** 
328f0 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75  Metadata is retu
32900 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20  rned by writing 
32910 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f  to the memory lo
32920 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61  cations passed a
32930 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64  s the 5th.** and
32940 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61   subsequent para
32950 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66  meters to this f
32960 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20  unction. Any of 
32970 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20  these arguments 
32980 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20  may be.** NULL, 
32990 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68  in which case th
329a0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
329b0 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 64  element of metad
329c0 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a  ata is omitted..
329d0 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
329e0 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72  e>.** <table bor
329f0 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e  der="1">.** <tr>
32a00 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c  <th> Parameter <
32a10 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79  th> Output<br>Ty
32a20 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70  pe <th>  Descrip
32a30 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c  tion.**.** <tr><
32a40 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f 6e  td> 5th <td> con
32a50 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 61  st char* <td> Da
32a60 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c  ta type.** <tr><
32a70 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f 6e  td> 6th <td> con
32a80 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e 61  st char* <td> Na
32a90 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 6f  me of default co
32aa0 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
32ab0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 68  .** <tr><td> 7th
32ac0 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20   <td> int       
32ad0 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63    <td> True if c
32ae0 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 20  olumn has a NOT 
32af0 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 0a  NULL constraint.
32b00 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 20  ** <tr><td> 8th 
32b10 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20  <td> int        
32b20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f   <td> True if co
32b30 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20  lumn is part of 
32b40 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 0a  the PRIMARY KEY.
32b50 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 20  ** <tr><td> 9th 
32b60 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20  <td> int        
32b70 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f   <td> True if co
32b80 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 4e 43  lumn is [AUTOINC
32b90 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 61 62  REMENT].** </tab
32ba0 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75  le>.** </blockqu
32bb0 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  ote>.**.** The m
32bc0 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f  emory pointed to
32bd0 20 62 79 20 74 68 65 20 63 68 61 72 61 63 74 65   by the characte
32be0 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72  r pointers retur
32bf0 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64  ned for the.** d
32c00 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20  eclaration type 
32c10 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  and collation se
32c20 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64 20  quence is valid 
32c30 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 6e  only until the n
32c40 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61  ext.** call to a
32c50 6e 79 20 53 51 4c 69 74 65 20 41 50 49 20 66 75  ny SQLite API fu
32c60 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  nction..**.** If
32c70 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 74   the specified t
32c80 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79  able is actually
32c90 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 72 72   a view, an [err
32ca0 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
32cb0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rned..**.** If t
32cc0 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c  he specified col
32cd0 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c 20  umn is "rowid", 
32ce0 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64  "oid" or "_rowid
32cf0 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b 49 4e  _" and an.** [IN
32d00 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
32d10 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62 65  Y] column has be
32d20 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65  en explicitly de
32d30 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65  clared, then the
32d40 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d   output.** param
32d50 65 74 65 72 73 20 61 72 65 20 73 65 74 20 66 6f  eters are set fo
32d60 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c 79  r the explicitly
32d70 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e   declared column
32d80 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  . If there is no
32d90 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 64  .** explicitly d
32da0 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 45 52  eclared [INTEGER
32db0 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f   PRIMARY KEY] co
32dc0 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 6f  lumn, then the o
32dd0 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74  utput.** paramet
32de0 65 72 73 20 61 72 65 20 73 65 74 20 61 73 20 66  ers are set as f
32df0 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 70  ollows:.**.** <p
32e00 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61 20  re>.**     data 
32e10 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22 0a  type: "INTEGER".
32e20 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f 6e  **     collation
32e30 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e 41   sequence: "BINA
32e40 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6e  RY".**     not n
32e50 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70 72  ull: 0.**     pr
32e60 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a 20  imary key: 1.** 
32e70 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d 65      auto increme
32e80 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e 0a  nt: 0.** </pre>.
32e90 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
32ea0 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e 65  ion may load one
32eb0 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 73   or more schemas
32ec0 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 66   from database f
32ed0 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 65  iles. If an.** e
32ee0 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
32ef0 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c  ng this process,
32f00 20 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 65   or if the reque
32f10 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63 6f  sted table or co
32f20 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62  lumn.** cannot b
32f30 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72 72  e found, an [err
32f40 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
32f50 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f  rned and an erro
32f60 72 20 6d 65 73 73 61 67 65 20 6c 65 66 74 0a 2a  r message left.*
32f70 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 61  * in the [databa
32f80 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28  se connection] (
32f90 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20  to be retrieved 
32fa0 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 72  using sqlite3_er
32fb0 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 54  rmsg())..**.** T
32fc0 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20  his API is only 
32fd0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65  available if the
32fe0 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d   library was com
32ff0 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a  piled with the.*
33000 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  * [SQLITE_ENABLE
33010 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
33020 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72  ] C-preprocessor
33030 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e   symbol defined.
33040 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
33050 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65  nt sqlite3_table
33060 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61  _column_metadata
33070 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
33080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33090 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  /* Connection ha
330a0 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
330b0 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 20  char *zDbName,  
330c0 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
330d0 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a  e name or NULL *
330e0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
330f0 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20  zTableName,     
33100 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f  /* Table name */
33110 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
33120 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f  ColumnName,    /
33130 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f  * Column name */
33140 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a  .  char const **
33150 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 2f  pzDataType,    /
33160 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 72  * OUTPUT: Declar
33170 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f 0a  ed data type */.
33180 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70    char const **p
33190 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f 2a  zCollSeq,     /*
331a0 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 69   OUTPUT: Collati
331b0 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65  on sequence name
331c0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 4e   */.  int *pNotN
331d0 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  ull,            
331e0 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75    /* OUTPUT: Tru
331f0 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  e if NOT NULL co
33200 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 20  nstraint exists 
33210 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d 61  */.  int *pPrima
33220 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20  ryKey,          
33230 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65   /* OUTPUT: True
33240 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 20   if column part 
33250 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a  of PK */.  int *
33260 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 20  pAutoinc        
33270 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54         /* OUTPUT
33280 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e  : True if column
33290 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65   is auto-increme
332a0 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.);../*.** 
332b0 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20 41  CAPI3REF: Load A
332c0 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 31 32  n Extension {H12
332d0 36 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a  600} <S20500>.**
332e0 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
332f0 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c 69  ce loads an SQLi
33300 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62  te extension lib
33310 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e 61  rary from the na
33320 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  med file..**.** 
33330 7b 48 31 32 36 30 31 7d 20 54 68 65 20 73 71 6c  {H12601} The sql
33340 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  ite3_load_extens
33350 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20  ion() interface 
33360 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 61 64  attempts to load
33370 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   an.**          
33380 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e  SQLite extension
33390 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e   library contain
333a0 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 7a  ed in the file z
333b0 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  File..**.** {H12
333c0 36 30 32 7d 20 54 68 65 20 65 6e 74 72 79 20 70  602} The entry p
333d0 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0a 2a  oint is zProc..*
333e0 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 7a 50  *.** {H12603} zP
333f0 72 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 69 6e  roc may be 0, in
33400 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20   which case the 
33410 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74 72  name of the entr
33420 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20  y point.**      
33430 20 20 20 20 64 65 66 61 75 6c 74 73 20 74 6f 20      defaults to 
33440 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69  "sqlite3_extensi
33450 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a 20  on_init"..**.** 
33460 7b 48 31 32 36 30 34 7d 20 54 68 65 20 73 71 6c  {H12604} The sql
33470 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  ite3_load_extens
33480 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20  ion() interface 
33490 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20  shall return.** 
334a0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45           [SQLITE
334b0 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20  _OK] on success 
334c0 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  and [SQLITE_ERRO
334d0 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20  R] if something 
334e0 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a  goes wrong..**.*
334f0 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 20 61 6e  * {H12605} If an
33500 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e   error occurs an
33510 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e 6f  d pzErrMsg is no
33520 74 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  t 0, then the.**
33530 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
33540 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  e3_load_extensio
33550 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73  n()] interface s
33560 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 0a  hall attempt to.
33570 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c 6c  **          fill
33580 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 20   *pzErrMsg with 
33590 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65  error message te
335a0 78 74 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d  xt stored in mem
335b0 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ory.**          
335c0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73  obtained from [s
335d0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
335e0 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 61 6c  . {END}  The cal
335f0 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  ling function.**
33600 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c 64            should
33610 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72   free this memor
33620 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71  y by calling [sq
33630 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a  lite3_free()]..*
33640 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 45 78  *.** {H12606} Ex
33650 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
33660 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64 20  must be enabled 
33670 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20  using.**        
33680 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c    [sqlite3_enabl
33690 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  e_load_extension
336a0 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 61 6c  ()] prior to cal
336b0 6c 69 6e 67 20 74 68 69 73 20 41 50 49 2c 0a 2a  ling this API,.*
336c0 2a 20 20 20 20 20 20 20 20 20 20 6f 74 68 65 72  *          other
336d0 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 77 69  wise an error wi
336e0 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a  ll be returned..
336f0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
33700 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65  t sqlite3_load_e
33710 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69  xtension(.  sqli
33720 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
33730 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78    /* Load the ex
33740 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69  tension into thi
33750 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  s database conne
33760 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
33770 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20   char *zFile,   
33780 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
33790 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63  shared library c
337a0 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73  ontaining extens
337b0 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
337c0 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f  har *zProc,    /
337d0 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20  * Entry point.  
337e0 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 69  Derived from zFi
337f0 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61  le if 0 */.  cha
33800 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20  r **pzErrMsg    
33810 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20     /* Put error 
33820 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20  message here if 
33830 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  not 0 */.);../*.
33840 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61  ** CAPI3REF: Ena
33850 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45  ble Or Disable E
33860 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67  xtension Loading
33870 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 35 30   {H12620} <S2050
33880 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e  0>.**.** So as n
33890 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72  ot to open secur
338a0 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64  ity holes in old
338b0 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  er applications 
338c0 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72  that are.** unpr
338d0 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 77  epared to deal w
338e0 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  ith extension lo
338f0 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 20  ading, and as a 
33900 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c 69  means of disabli
33910 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20  ng.** extension 
33920 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65 76  loading while ev
33930 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65 6e  aluating user-en
33940 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20 66  tered SQL, the f
33950 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20  ollowing API.** 
33960 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20 74  is provided to t
33970 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33  urn the [sqlite3
33980 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
33990 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20  )] mechanism on 
339a0 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45  and off..**.** E
339b0 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
339c0 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75   is off by defau
339d0 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 74 20 23  lt. See ticket #
339e0 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  1863..**.** {H12
339f0 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 73 71  621} Call the sq
33a00 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61  lite3_enable_loa
33a10 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 72 6f  d_extension() ro
33a20 75 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f 66 66  utine with onoff
33a30 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ==1.**          
33a40 74 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 69 6f  to turn extensio
33a50 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 6e 64  n loading on and
33a60 20 63 61 6c 6c 20 69 74 20 77 69 74 68 20 6f 6e   call it with on
33a70 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e 0a 2a  off==0 to turn.*
33a80 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 62 61  *          it ba
33a90 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a 2a  ck off again..**
33aa0 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 78 74  .** {H12622} Ext
33ab0 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69  ension loading i
33ac0 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74  s off by default
33ad0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
33ae0 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62  int sqlite3_enab
33af0 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  le_load_extensio
33b00 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  n(sqlite3 *db, i
33b10 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a  nt onoff);../*.*
33b20 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 6f  * CAPI3REF: Auto
33b30 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 20 41  matically Load A
33b40 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b 48 31  n Extensions {H1
33b50 32 36 34 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a  2640} <S20500>.*
33b60 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 63 61  *.** This API ca
33b70 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 74 20  n be invoked at 
33b80 70 72 6f 67 72 61 6d 20 73 74 61 72 74 75 70 20  program startup 
33b90 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 67 69  in order to regi
33ba0 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d  ster.** one or m
33bb0 6f 72 65 20 73 74 61 74 69 63 61 6c 6c 79 20 6c  ore statically l
33bc0 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e 73  inked extensions
33bd0 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61 76   that will be av
33be0 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c  ailable.** to al
33bf0 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20  l new [database 
33c00 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b 45  connections]. {E
33c10 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ND}.**.** This r
33c20 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 20  outine stores a 
33c30 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65  pointer to the e
33c40 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61  xtension in an a
33c50 72 72 61 79 20 74 68 61 74 20 69 73 0a 2a 2a 20  rray that is.** 
33c60 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73  obtained from [s
33c70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
33c80 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20 61 20  .  If you run a 
33c90 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 65 63  memory leak chec
33ca0 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 70  ker.** on your p
33cb0 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 72 65  rogram and it re
33cc0 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 65 63  ports a leak bec
33cd0 61 75 73 65 20 6f 66 20 74 68 69 73 20 61 72 72  ause of this arr
33ce0 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73  ay, invoke.** [s
33cf0 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74  qlite3_reset_aut
33d00 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70  o_extension()] p
33d10 72 69 6f 72 20 74 6f 20 73 68 75 74 64 6f 77 6e  rior to shutdown
33d20 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65 6d   to free the mem
33d30 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  ory..**.** {H126
33d40 34 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 6f  41} This functio
33d50 6e 20 72 65 67 69 73 74 65 72 73 20 61 6e 20 65  n registers an e
33d60 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 20 70  xtension entry p
33d70 6f 69 6e 74 20 74 68 61 74 20 69 73 0a 2a 2a 20  oint that is.** 
33d80 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74           automat
33d90 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 77  ically invoked w
33da0 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 5b 64  henever a new [d
33db0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
33dc0 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  on].**          
33dd0 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20  is opened using 
33de0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
33df0 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  , [sqlite3_open1
33e00 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  6()],.**        
33e10 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70    or [sqlite3_op
33e20 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  en_v2()]..**.** 
33e30 7b 48 31 32 36 34 32 7d 20 44 75 70 6c 69 63 61  {H12642} Duplica
33e40 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 72  te extensions ar
33e50 65 20 64 65 74 65 63 74 65 64 20 73 6f 20 63 61  e detected so ca
33e60 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  lling this routi
33e70 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d  ne.**          m
33e80 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69  ultiple times wi
33e90 74 68 20 74 68 65 20 73 61 6d 65 20 65 78 74 65  th the same exte
33ea0 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73  nsion is harmles
33eb0 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 33  s..**.** {H12643
33ec0 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  } This routine s
33ed0 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20  tores a pointer 
33ee0 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e  to the extension
33ef0 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20   in an array.** 
33f00 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69 73           that is
33f10 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b   obtained from [
33f20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
33f30 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 34  ]..**.** {H12644
33f40 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 78 74 65  } Automatic exte
33f50 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 63 72  nsions apply acr
33f60 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 64 73 2e  oss all threads.
33f70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
33f80 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f  nt sqlite3_auto_
33f90 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28  extension(void (
33fa0 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f  *xEntryPoint)(vo
33fb0 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  id));../*.** CAP
33fc0 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 74  I3REF: Reset Aut
33fd0 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f 6e  omatic Extension
33fe0 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36 30   Loading {H12660
33ff0 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20500>.**.**
34000 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64   This function d
34010 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 76  isables all prev
34020 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65  iously registere
34030 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 65  d automatic.** e
34040 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d  xtensions. {END}
34050 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65 20    It undoes the 
34060 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 72  effect of all pr
34070 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ior.** [sqlite3_
34080 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29  auto_extension()
34090 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b  ] calls..**.** {
340a0 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 75 6e  H12661} This fun
340b0 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61  ction disables a
340c0 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  ll previously re
340d0 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20 20  gistered.**     
340e0 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20 65       automatic e
340f0 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  xtensions..**.**
34100 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20 66   {H12662} This f
34110 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73  unction disables
34120 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e   automatic exten
34130 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68 72  sions in all thr
34140 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eads..*/.SQLITE_
34150 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
34160 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65  _reset_auto_exte
34170 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a  nsion(void);../*
34180 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45  .****** EXPERIME
34190 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74  NTAL - subject t
341a0 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74  o change without
341b0 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a   notice ********
341c0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  ******.**.** The
341d0 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   interface to th
341e0 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  e virtual-table 
341f0 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75 72  mechanism is cur
34200 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65  rently considere
34210 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72  d.** to be exper
34220 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e  imental.  The in
34230 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 68  terface might ch
34240 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74  ange in incompat
34250 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66  ible ways..** If
34260 20 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c   this is a probl
34270 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e  em for you, do n
34280 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72  ot use the inter
34290 66 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d  face at this tim
342a0 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  e..**.** When th
342b0 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  e virtual-table 
342c0 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c  mechanism stabil
342d0 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65  izes, we will de
342e0 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74  clare the.** int
342f0 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 75  erface fixed, su
34300 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e  pport it indefin
34310 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76  itely, and remov
34320 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a  e this comment..
34330 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74  */../*.** Struct
34340 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65  ures used by the
34350 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
34360 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65  nterface.*/.type
34370 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
34380 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33 5f  e3_vtab sqlite3_
34390 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73 74  vtab;.typedef st
343a0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
343b0 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f  ex_info sqlite3_
343c0 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65  index_info;.type
343d0 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
343e0 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 73  e3_vtab_cursor s
343f0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
34400 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  or;.typedef stru
34410 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  ct sqlite3_modul
34420 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  e sqlite3_module
34430 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
34440 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65  F: Virtual Table
34450 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30 7d   Object {H18000}
34460 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59   <S20400>.** KEY
34470 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 6d  WORDS: sqlite3_m
34480 6f 64 75 6c 65 20 7b 76 69 72 74 75 61 6c 20 74  odule {virtual t
34490 61 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20  able module}.** 
344a0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
344b0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
344c0 65 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 61 6c  e, sometimes cal
344d0 6c 65 64 20 61 20 61 20 22 76 69 72 74 75 61 6c  led a a "virtual
344e0 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 2c 20   table module", 
344f0 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20  .** defines the 
34500 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
34510 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62  f a [virtual tab
34520 6c 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 20  les].  .** This 
34530 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 73  structure consis
34540 74 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74  ts mostly of met
34550 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64  hods for the mod
34560 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72  ule..**.** A vir
34570 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
34580 65 20 69 73 20 63 72 65 61 74 65 64 20 62 79 20  e is created by 
34590 66 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65 72  filling in a per
345a0 73 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 61  sistent.** insta
345b0 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
345c0 63 74 75 72 65 20 61 6e 64 20 70 61 73 73 69 6e  cture and passin
345d0 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  g a pointer to t
345e0 68 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20  hat instance.** 
345f0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  to [sqlite3_crea
34600 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 20  te_module()] or 
34610 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
34620 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a  module_v2()]..**
34630 20 54 68 65 20 72 65 67 69 73 74 72 61 74 69 6f   The registratio
34640 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20  n remains valid 
34650 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65 70 6c  until it is repl
34660 61 63 65 64 20 62 79 20 61 20 64 69 66 66 65 72  aced by a differ
34670 65 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72  ent.** module or
34680 20 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 74 61   until the [data
34690 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
346a0 20 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 63 6f   closes.  The co
346b0 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 73  ntent.** of this
346c0 20 73 74 72 75 63 74 75 72 65 20 6d 75 73 74 20   structure must 
346d0 6e 6f 74 20 63 68 61 6e 67 65 20 77 68 69 6c 65  not change while
346e0 20 69 74 20 69 73 20 72 65 67 69 73 74 65 72 65   it is registere
346f0 64 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 64 61  d with.** any da
34700 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
34710 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c  n..*/.struct sql
34720 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20  ite3_module {.  
34730 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20  int iVersion;.  
34740 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73  int (*xCreate)(s
34750 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70  qlite3*, void *p
34760 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Aux,.           
34770 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f      int argc, co
34780 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a  nst char *const*
34790 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20  argv,.          
347a0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
347b0 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72  b **ppVTab, char
347c0 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f  **);.  int (*xCo
347d0 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c  nnect)(sqlite3*,
347e0 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20   void *pAux,.   
347f0 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
34800 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
34810 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20   *const*argv,.  
34820 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
34830 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54  ite3_vtab **ppVT
34840 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69  ab, char**);.  i
34850 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29  nt (*xBestIndex)
34860 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
34870 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e  VTab, sqlite3_in
34880 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e  dex_info*);.  in
34890 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29  t (*xDisconnect)
348a0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
348b0 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78  VTab);.  int (*x
348c0 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33  Destroy)(sqlite3
348d0 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
348e0 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71   int (*xOpen)(sq
348f0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
34900 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  b, sqlite3_vtab_
34910 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f  cursor **ppCurso
34920 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f  r);.  int (*xClo
34930 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  se)(sqlite3_vtab
34940 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74  _cursor*);.  int
34950 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69   (*xFilter)(sqli
34960 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a  te3_vtab_cursor*
34970 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f  , int idxNum, co
34980 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72  nst char *idxStr
34990 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
349a0 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69    int argc, sqli
349b0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
349c0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74  );.  int (*xNext
349d0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  )(sqlite3_vtab_c
349e0 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28  ursor*);.  int (
349f0 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76  *xEof)(sqlite3_v
34a00 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20  tab_cursor*);.  
34a10 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73  int (*xColumn)(s
34a20 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
34a30 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e  or*, sqlite3_con
34a40 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69  text*, int);.  i
34a50 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c  nt (*xRowid)(sql
34a60 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
34a70 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
34a80 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74   *pRowid);.  int
34a90 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69   (*xUpdate)(sqli
34aa0 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c  te3_vtab *, int,
34ab0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
34ac0 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
34ad0 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65   *);.  int (*xBe
34ae0 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61  gin)(sqlite3_vta
34af0 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74  b *pVTab);.  int
34b00 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65   (*xSync)(sqlite
34b10 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a  3_vtab *pVTab);.
34b20 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29    int (*xCommit)
34b30 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
34b40 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78  VTab);.  int (*x
34b50 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65  Rollback)(sqlite
34b60 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a  3_vtab *pVTab);.
34b70 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e    int (*xFindFun
34b80 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76  ction)(sqlite3_v
34b90 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20  tab *pVtab, int 
34ba0 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72  nArg, const char
34bb0 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20   *zName,.       
34bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34bd0 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28  void (**pxFunc)(
34be0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
34bf0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
34c00 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20  ue**),.         
34c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f                vo
34c20 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69  id **ppArg);.  i
34c30 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71  nt (*xRename)(sq
34c40 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
34c50 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
34c60 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  New);.};../*.** 
34c70 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61  CAPI3REF: Virtua
34c80 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67  l Table Indexing
34c90 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31   Information {H1
34ca0 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a  8100} <S20400>.*
34cb0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69  * KEYWORDS: sqli
34cc0 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a  te3_index_info.*
34cd0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
34ce0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
34cf0 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
34d00 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75  cture and its su
34d10 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75  bstructures is u
34d20 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69  sed to.** pass i
34d30 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20  nformation into 
34d40 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20  and receive the 
34d50 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 5b  reply from the [
34d60 78 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d  xBestIndex].** m
34d70 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72 74  ethod of a [virt
34d80 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
34d90 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20 75  ].  The fields u
34da0 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20  nder **Inputs** 
34db0 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74  are the.** input
34dc0 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20  s to xBestIndex 
34dd0 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c  and are read-onl
34de0 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20 69  y.  xBestIndex i
34df0 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 72 65  nserts its.** re
34e00 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 20 2a  sults into the *
34e10 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 6c 64  *Outputs** field
34e20 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 6f  s..**.** The aCo
34e30 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79  nstraint[] array
34e40 20 72 65 63 6f 72 64 73 20 57 48 45 52 45 20 63   records WHERE c
34e50 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74  lause constraint
34e60 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
34e70 2a 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e  *.** <pre>column
34e80 20 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e 0a 2a   OP expr</pre>.*
34e90 2a 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 69 73  *.** where OP is
34ea0 20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c   =, &lt;, &lt;=,
34eb0 20 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e   &gt;, or &gt;=.
34ec0 20 20 54 68 65 20 70 61 72 74 69 63 75 6c 61 72    The particular
34ed0 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20   operator is.** 
34ee0 73 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73 74  stored in aConst
34ef0 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 65  raint[].op.  The
34f00 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f   index of the co
34f10 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 20 69  lumn is stored i
34f20 6e 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74  n.** aConstraint
34f30 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f  [].iColumn.  aCo
34f40 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c  nstraint[].usabl
34f50 65 20 69 73 20 54 52 55 45 20 69 66 20 74 68 65  e is TRUE if the
34f60 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 65 20  .** expr on the 
34f70 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
34f80 63 61 6e 20 62 65 20 65 76 61 6c 75 61 74 65 64  can be evaluated
34f90 20 28 61 6e 64 20 74 68 75 73 20 74 68 65 20 63   (and thus the c
34fa0 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 73 20  onstraint.** is 
34fb0 75 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c 73  usable) and fals
34fc0 65 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e 0a  e if it cannot..
34fd0 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69  **.** The optimi
34fe0 7a 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  zer automaticall
34ff0 79 20 69 6e 76 65 72 74 73 20 74 65 72 6d 73 20  y inverts terms 
35000 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 65 78 70  of the form "exp
35010 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20  r OP column".** 
35020 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 72 20  and makes other 
35030 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73 20  simplifications 
35040 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  to the WHERE cla
35050 75 73 65 20 69 6e 20 61 6e 20 61 74 74 65 6d 70  use in an attemp
35060 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d  t to.** get as m
35070 61 6e 79 20 57 48 45 52 45 20 63 6c 61 75 73 65  any WHERE clause
35080 20 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 65 20   terms into the 
35090 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65  form shown above
350a0 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a   as possible..**
350b0 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74   The aConstraint
350c0 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72 65  [] array only re
350d0 70 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61 75  ports WHERE clau
350e0 73 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20  se terms in the 
350f0 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 6d 20  correct.** form 
35100 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68  that refer to th
35110 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 69 72  e particular vir
35120 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69 6e 67  tual table being
35130 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20   queried..**.** 
35140 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
35150 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  t the ORDER BY c
35160 6c 61 75 73 65 20 69 73 20 73 74 6f 72 65 64 20  lause is stored 
35170 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a  in aOrderBy[]..*
35180 2a 20 45 61 63 68 20 74 65 72 6d 20 6f 66 20 61  * Each term of a
35190 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64 73 20  OrderBy records 
351a0 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  a column of the 
351b0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e  ORDER BY clause.
351c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 65 73  .**.** The [xBes
351d0 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 20 6d  tIndex] method m
351e0 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74 72  ust fill aConstr
351f0 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 68  aintUsage[] with
35200 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
35210 61 62 6f 75 74 20 77 68 61 74 20 70 61 72 61 6d  about what param
35220 65 74 65 72 73 20 74 6f 20 70 61 73 73 20 74 6f  eters to pass to
35230 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61 72   xFilter.  If ar
35240 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a 2a  gvIndex>0 then.*
35250 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  * the right-hand
35260 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f 72   side of the cor
35270 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e 73  responding aCons
35280 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61 6c  traint[] is eval
35290 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65 63  uated.** and bec
352a0 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e 64  omes the argvInd
352b0 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20 61  ex-th entry in a
352c0 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74 72  rgv.  If aConstr
352d0 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 74  aintUsage[].omit
352e0 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68 65  .** is true, the
352f0 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  n the constraint
35300 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62   is assumed to b
35310 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64 20  e fully handled 
35320 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61  by the.** virtua
35330 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20 6e  l table and is n
35340 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61 69 6e  ot checked again
35350 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a   by SQLite..**.*
35360 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e 64  * The idxNum and
35370 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 20 61   idxPtr values a
35380 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64 20  re recorded and 
35390 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 0a  passed into the.
353a0 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65 74  ** [xFilter] met
353b0 68 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  hod..** [sqlite3
353c0 5f 66 72 65 65 28 29 5d 20 69 73 20 75 73 65 64  _free()] is used
353d0 20 74 6f 20 66 72 65 65 20 69 64 78 50 74 72 20   to free idxPtr 
353e0 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 66 0a  if and only iff.
353f0 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78  ** needToFreeIdx
35400 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a  Ptr is true..**.
35410 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43 6f  ** The orderByCo
35420 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68 61  nsumed means tha
35430 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 5b 78  t output from [x
35440 46 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 5d 20  Filter]/[xNext] 
35450 77 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a 2a 2a  will occur in.**
35460 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64   the correct ord
35470 65 72 20 74 6f 20 73 61 74 69 73 66 79 20 74 68  er to satisfy th
35480 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
35490 65 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 65 70  e so that no sep
354a0 61 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 6e 67  arate.** sorting
354b0 20 73 74 65 70 20 69 73 20 72 65 71 75 69 72 65   step is require
354c0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74  d..**.** The est
354d0 69 6d 61 74 65 64 43 6f 73 74 20 76 61 6c 75 65  imatedCost value
354e0 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20   is an estimate 
354f0 6f 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20 64  of the cost of d
35500 6f 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 74  oing the.** part
35510 69 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 20  icular lookup.  
35520 41 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61  A full scan of a
35530 20 74 61 62 6c 65 20 77 69 74 68 20 4e 20 65 6e   table with N en
35540 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76  tries should hav
35550 65 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e  e.** a cost of N
35560 2e 20 20 41 20 62 69 6e 61 72 79 20 73 65 61 72  .  A binary sear
35570 63 68 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 66  ch of a table of
35580 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75 6c   N entries shoul
35590 64 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 73 74  d have a.** cost
355a0 20 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 65 6c   of approximatel
355b0 79 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72  y log(N)..*/.str
355c0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
355d0 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e  x_info {.  /* In
355e0 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  puts */.  int nC
355f0 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20  onstraint;      
35600 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
35610 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f  f entries in aCo
35620 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 73 74  nstraint */.  st
35630 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
35640 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b 0a  ex_constraint {.
35650 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e       int iColumn
35660 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
35670 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74  * Column on left
35680 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63 6f  -hand side of co
35690 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20  nstraint */.    
356a0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f   unsigned char o
356b0 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  p;         /* Co
356c0 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74 6f  nstraint operato
356d0 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e  r */.     unsign
356e0 65 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b 20  ed char usable; 
356f0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
35700 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  his constraint i
35710 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20  s usable */.    
35720 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65 74   int iTermOffset
35730 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73  ;          /* Us
35740 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d 20  ed internally - 
35750 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75 6c  xBestIndex shoul
35760 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20  d ignore */.  } 
35770 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20  *aConstraint;   
35780 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
35790 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75 73  e of WHERE claus
357a0 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f  e constraints */
357b0 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b  .  int nOrderBy;
357c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
357d0 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   Number of terms
357e0 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59   in the ORDER BY
357f0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72   clause */.  str
35800 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
35810 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 20 20  x_orderby {.    
35820 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   int iColumn;   
35830 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
35840 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  lumn number */. 
35850 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
35860 72 20 64 65 73 63 3b 20 20 20 20 20 20 20 2f 2a  r desc;       /*
35870 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 2e 20   True for DESC. 
35880 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43 2e 20   False for ASC. 
35890 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 42 79  */.  } *aOrderBy
358a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
358b0 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
358c0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f  clause */.  /* O
358d0 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 72 75  utputs */.  stru
358e0 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
358f0 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67  _constraint_usag
35900 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 67 76  e {.    int argv
35910 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 20  Index;          
35920 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73 74   /* if >0, const
35930 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f 66  raint is part of
35940 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65 72   argv to xFilter
35950 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64   */.    unsigned
35960 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20 20   char omit;     
35970 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20   /* Do not code 
35980 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 73 20  a test for this 
35990 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20  constraint */.  
359a0 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  } *aConstraintUs
359b0 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e 75  age;.  int idxNu
359c0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
359d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65 64    /* Number used
359e0 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65   to identify the
359f0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61 72   index */.  char
35a00 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20 20   *idxStr;       
35a10 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67         /* String
35a20 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61 69  , possibly obtai
35a30 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
35a40 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74  _malloc */.  int
35a50 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74   needToFreeIdxSt
35a60 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20  r;      /* Free 
35a70 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 71 6c  idxStr using sql
35a80 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 20 74  ite3_free() if t
35a90 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64  rue */.  int ord
35aa0 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20 20  erByConsumed;   
35ab0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6f      /* True if o
35ac0 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 64 79  utput is already
35ad0 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64 6f   ordered */.  do
35ae0 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43 6f  uble estimatedCo
35af0 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74 69  st;      /* Esti
35b00 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75 73  mated cost of us
35b10 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a  ing this index *
35b20 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c  /.};.#define SQL
35b30 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
35b40 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64 65  AINT_EQ    2.#de
35b50 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  fine SQLITE_INDE
35b60 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20  X_CONSTRAINT_GT 
35b70 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c     4.#define SQL
35b80 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
35b90 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64 65  AINT_LE    8.#de
35ba0 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  fine SQLITE_INDE
35bb0 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20  X_CONSTRAINT_LT 
35bc0 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51     16.#define SQ
35bd0 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
35be0 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a 23  RAINT_GE    32.#
35bf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
35c00 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d  DEX_CONSTRAINT_M
35c10 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43  ATCH 64../*.** C
35c20 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65  API3REF: Registe
35c30 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c  r A Virtual Tabl
35c40 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e Implementation
35c50 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 34 30   {H18200} <S2040
35c60 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
35c70 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  AL.**.** This ro
35c80 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f  utine is used to
35c90 20 72 65 67 69 73 74 65 72 20 61 20 6e 65 77 20   register a new 
35ca0 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d  [virtual table m
35cb0 6f 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20  odule] name..** 
35cc0 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73  Module names mus
35cd0 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20  t be registered 
35ce0 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69  before.** creati
35cf0 6e 67 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61  ng a new [virtua
35d00 6c 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 20 74  l table] using t
35d10 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65  he module, or be
35d20 66 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a 2a 20  fore using a.** 
35d30 70 72 65 65 78 69 73 74 69 6e 67 20 5b 76 69 72  preexisting [vir
35d40 74 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f 72 20  tual table] for 
35d50 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a  the module..**.*
35d60 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d  * The module nam
35d70 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20  e is registered 
35d80 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  on the [database
35d90 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65   connection] spe
35da0 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65  cified.** by the
35db0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
35dc0 2e 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  .  The name of t
35dd0 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69 76  he module is giv
35de0 65 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 73 65  en by the .** se
35df0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20  cond parameter. 
35e00 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   The third param
35e10 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65  eter is a pointe
35e20 72 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 6c  r to.** the impl
35e30 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
35e40 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e [virtual table
35e50 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 65 20   module].   The 
35e60 66 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 6d 65  fourth.** parame
35e70 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72  ter is an arbitr
35e80 61 72 79 20 63 6c 69 65 6e 74 20 64 61 74 61 20  ary client data 
35e90 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20  pointer that is 
35ea0 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a  passed through.*
35eb0 2a 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 72 65  * into the [xCre
35ec0 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65  ate] and [xConne
35ed0 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 74  ct] methods of t
35ee0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
35ef0 20 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 6e 20   module.** when 
35f00 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 61  a new virtual ta
35f10 62 6c 65 20 69 73 20 62 65 20 62 65 69 6e 67 20  ble is be being 
35f20 63 72 65 61 74 65 64 20 6f 72 20 72 65 69 6e 69  created or reini
35f30 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20  tialized..**.** 
35f40 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 68  This interface h
35f50 61 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73  as exactly the s
35f60 61 6d 65 20 65 66 66 65 63 74 20 61 73 20 63 61  ame effect as ca
35f70 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65  lling.** [sqlite
35f80 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
35f90 76 32 28 29 5d 20 77 69 74 68 20 61 20 4e 55 4c  v2()] with a NUL
35fa0 4c 20 63 6c 69 65 6e 74 20 64 61 74 61 20 64 65  L client data de
35fb0 73 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c  structor..*/.SQL
35fc0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45  ITE_API SQLITE_E
35fd0 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20  XPERIMENTAL int 
35fe0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
35ff0 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  odule(.  sqlite3
36000 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
36010 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f      /* SQLite co
36020 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69  nnection to regi
36030 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68  ster module with
36040 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
36050 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
36060 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
36070 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  module */.  cons
36080 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
36090 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64   *p,   /* Method
360a0 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65  s for the module
360b0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69   */.  void *pCli
360c0 65 6e 74 44 61 74 61 20 20 20 20 20 20 20 20 20  entData         
360d0 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20   /* Client data 
360e0 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e  for xCreate/xCon
360f0 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a  nect */.);../*.*
36100 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69  * CAPI3REF: Regi
36110 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54  ster A Virtual T
36120 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74  able Implementat
36130 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c 53 32  ion {H18210} <S2
36140 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0400>.** EXPERIM
36150 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ENTAL.**.** This
36160 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65 6e   routine is iden
36170 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b 73 71  tical to the [sq
36180 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
36190 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a  ule()] method,.*
361a0 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  * except that it
361b0 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 70 61   has an extra pa
361c0 72 61 6d 65 74 65 72 20 74 6f 20 73 70 65 63 69  rameter to speci
361d0 66 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 75 63  fy .** a destruc
361e0 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  tor function for
361f0 20 74 68 65 20 63 6c 69 65 6e 74 20 64 61 74 61   the client data
36200 20 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c 69 74   pointer.  SQLit
36210 65 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65  e will.** invoke
36220 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
36230 66 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 74 20  function (if it 
36240 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65  is not NULL) whe
36250 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c  n SQLite.** no l
36260 6f 6e 67 65 72 20 6e 65 65 64 73 20 74 68 65 20  onger needs the 
36270 70 43 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 6e  pClientData poin
36280 74 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45  ter.  .*/.SQLITE
36290 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
362a0 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c  RIMENTAL int sql
362b0 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
362c0 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  le_v2(.  sqlite3
362d0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
362e0 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f      /* SQLite co
362f0 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69  nnection to regi
36300 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68  ster module with
36310 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
36320 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
36330 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
36340 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  module */.  cons
36350 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
36360 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64   *p,   /* Method
36370 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65  s for the module
36380 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69   */.  void *pCli
36390 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 20  entData,        
363a0 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20   /* Client data 
363b0 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e  for xCreate/xCon
363c0 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a  nect */.  void(*
363d0 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29  xDestroy)(void*)
363e0 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64       /* Module d
363f0 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
36400 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  on */.);../*.** 
36410 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61  CAPI3REF: Virtua
36420 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63 65  l Table Instance
36430 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 30 7d   Object {H18010}
36440 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59   <S20400>.** KEY
36450 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76  WORDS: sqlite3_v
36460 74 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  tab.** EXPERIMEN
36470 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20  TAL.**.** Every 
36480 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d  [virtual table m
36490 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74  odule] implement
364a0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62  ation uses a sub
364b0 63 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 65 20  class.** of the 
364c0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
364d0 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20  ure to describe 
364e0 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 73  a particular ins
364f0 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20  tance.** of the 
36500 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e  [virtual table].
36510 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 20    Each subclass 
36520 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f  will.** be tailo
36530 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 69  red to the speci
36540 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65  fic needs of the
36550 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
36560 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70  tation..** The p
36570 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73  urpose of this s
36580 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20  uperclass is to 
36590 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66  define certain f
365a0 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a 2a  ields that are.*
365b0 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20  * common to all 
365c0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  module implement
365d0 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69  ations..**.** Vi
365e0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 74  rtual tables met
365f0 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e 20  hods can set an 
36600 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 79  error message by
36610 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20   assigning a.** 
36620 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20  string obtained 
36630 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70  from [sqlite3_mp
36640 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 72  rintf()] to zErr
36650 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64  Msg.  The method
36660 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20   should.** take 
36670 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 72  care that any pr
36680 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 72  ior string is fr
36690 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  eed by a call to
366a0 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
366b0 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73  ].** prior to as
366c0 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74  signing a new st
366d0 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e  ring to zErrMsg.
366e0 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72 6f    After the erro
366f0 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20  r message.** is 
36700 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20  delivered up to 
36710 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69  the client appli
36720 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69  cation, the stri
36730 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d  ng will be autom
36740 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65  atically.** free
36750 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65  d by sqlite3_fre
36760 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72  e() and the zErr
36770 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62  Msg field will b
36780 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72  e zeroed..*/.str
36790 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62  uct sqlite3_vtab
367a0 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74   {.  const sqlit
367b0 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
367c0 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75  le;  /* The modu
367d0 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69 72 74  le for this virt
367e0 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  ual table */.  i
367f0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
36800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36810 2a 20 4e 4f 20 4c 4f 4e 47 45 52 20 55 53 45 44  * NO LONGER USED
36820 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72   */.  char *zErr
36830 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Msg;            
36840 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d        /* Error m
36850 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c 69  essage from sqli
36860 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f  te3_mprintf() */
36870 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61  .  /* Virtual ta
36880 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
36890 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c  ons will typical
368a0 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61  ly add additiona
368b0 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a  l fields */.};..
368c0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
368d0 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43 75  Virtual Table Cu
368e0 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b 48 31  rsor Object  {H1
368f0 38 30 32 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a  8020} <S20400>.*
36900 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69  * KEYWORDS: sqli
36910 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
36920 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63  {virtual table c
36930 75 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 49  ursor}.** EXPERI
36940 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65  MENTAL.**.** Eve
36950 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c  ry [virtual tabl
36960 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d  e module] implem
36970 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20  entation uses a 
36980 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 0a  subclass of the.
36990 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  ** following str
369a0 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 69  ucture to descri
369b0 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  be cursors that 
369c0 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a 2a  point into the.*
369d0 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  * [virtual table
369e0 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a 2a  ] and are used.*
369f0 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67  * to loop throug
36a00 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  h the virtual ta
36a10 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61 72  ble.  Cursors ar
36a20 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  e created using 
36a30 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  the.** [sqlite3_
36a40 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 78  module.xOpen | x
36a50 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20  Open] method of 
36a60 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20 61  the module and a
36a70 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20  re destroyed.** 
36a80 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  by the [sqlite3_
36a90 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c 20  module.xClose | 
36aa0 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e 20  xClose] method. 
36ab0 20 43 75 73 73 6f 72 73 20 61 72 65 20 75 73 65   Cussors are use
36ac0 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46 69  d.** by the [xFi
36ad0 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c 20  lter], [xNext], 
36ae0 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d 6e  [xEof], [xColumn
36af0 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d 20  ], and [xRowid] 
36b00 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68  methods.** of th
36b10 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 20  e module.  Each 
36b20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  module implement
36b30 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e  ation will defin
36b40 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  e.** the content
36b50 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 72   of a cursor str
36b60 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 69  ucture to suit i
36b70 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a  ts own needs..**
36b80 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 6c  .** This supercl
36b90 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f 72  ass exists in or
36ba0 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 69  der to define fi
36bb0 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 73  elds of the curs
36bc0 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63  or that.** are c
36bd0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70  ommon to all imp
36be0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f  lementations..*/
36bf0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
36c00 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20  vtab_cursor {.  
36c10 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
36c20 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 72  tab;      /* Vir
36c30 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 68  tual table of th
36c40 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f  is cursor */.  /
36c50 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20  * Virtual table 
36c60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
36c70 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61  will typically a
36c80 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69  dd additional fi
36c90 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  elds */.};../*.*
36ca0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c  * CAPI3REF: Decl
36cb0 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 20 4f  are The Schema O
36cc0 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c  f A Virtual Tabl
36cd0 65 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 30 34  e {H18280} <S204
36ce0 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
36cf0 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78  TAL.**.** The [x
36d00 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f  Create] and [xCo
36d10 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f  nnect] methods o
36d20 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20  f a.** [virtual 
36d30 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61  table module] ca
36d40 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  ll this interfac
36d50 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65 20  e.** to declare 
36d60 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65 20  the format (the 
36d70 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74 79  names and dataty
36d80 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  pes of the colum
36d90 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69  ns) of.** the vi
36da0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68 65  rtual tables the
36db0 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a  y implement..*/.
36dc0 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
36dd0 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69  E_EXPERIMENTAL i
36de0 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61  nt sqlite3_decla
36df0 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 2a  re_vtab(sqlite3*
36e00 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
36e10 51 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  QL);../*.** CAPI
36e20 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 20 41  3REF: Overload A
36e30 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 41 20   Function For A 
36e40 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b 48  Virtual Table {H
36e50 31 38 33 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a  18300} <S20400>.
36e60 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
36e70 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61  **.** Virtual ta
36e80 62 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 64 65  bles can provide
36e90 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 6d 70   alternative imp
36ea0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
36eb0 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 69  functions.** usi
36ec0 6e 67 20 74 68 65 20 5b 78 46 69 6e 64 46 75 6e  ng the [xFindFun
36ed0 63 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f 66  ction] method of
36ee0 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61   the [virtual ta
36ef0 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a  ble module].  .*
36f00 2a 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65 72  * But global ver
36f10 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20 66  sions of those f
36f20 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74  unctions.** must
36f30 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 20   exist in order 
36f40 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 64  to be overloaded
36f50 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49  ..**.** This API
36f60 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67 6c   makes sure a gl
36f70 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20  obal version of 
36f80 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  a function with 
36f90 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20  a particular.** 
36fa0 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72 20  name and number 
36fb0 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 65 78  of parameters ex
36fc0 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75 63  ists.  If no suc
36fd0 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74  h function exist
36fe0 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73  s.** before this
36ff0 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c 20   API is called, 
37000 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69  a new function i
37010 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20  s created.  The 
37020 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
37030 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75 6e  * of the new fun
37040 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61 75  ction always cau
37050 73 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f 6e  ses an exception
37060 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20 20   to be thrown.  
37070 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66 75  So.** the new fu
37080 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 6f  nction is not go
37090 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67 20  od for anything 
370a0 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73 20  by itself.  Its 
370b0 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65 20  only.** purpose 
370c0 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63 65  is to be a place
370d0 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e 20  holder function 
370e0 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65 72  that can be over
370f0 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 20 5b  loaded.** by a [
37100 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a  virtual table]..
37110 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
37120 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
37130 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76  L int sqlite3_ov
37140 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
37150 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
37160 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c  char *zFuncName,
37170 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a   int nArg);../*.
37180 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65  ** The interface
37190 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d   to the virtual-
371a0 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20  table mechanism 
371b0 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28 62  defined above (b
371c0 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63  ack up.** to a c
371d0 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c  omment remarkabl
371e0 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69  y similar to thi
371f0 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65 6e  s one) is curren
37200 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a  tly considered.*
37210 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65  * to be experime
37220 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72  ntal.  The inter
37230 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67  face might chang
37240 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c  e in incompatibl
37250 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68  e ways..** If th
37260 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20  is is a problem 
37270 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20  for you, do not 
37280 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63  use the interfac
37290 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a  e at this time..
372a0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76  **.** When the v
372b0 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63  irtual-table mec
372c0 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65  hanism stabilize
372d0 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61  s, we will decla
372e0 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66  re the.** interf
372f0 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f  ace fixed, suppo
37300 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65  rt it indefinite
37310 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74  ly, and remove t
37320 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a  his comment..**.
37330 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  ****** EXPERIMEN
37340 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 6f  TAL - subject to
37350 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20   change without 
37360 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  notice *********
37370 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *****.*/../*.** 
37380 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e 64  CAPI3REF: A Hand
37390 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c  le To An Open BL
373a0 4f 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 33 30  OB {H17800} <S30
373b0 32 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  230>.** KEYWORDS
373c0 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20  : {BLOB handle} 
373d0 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a  {BLOB handles}.*
373e0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
373f0 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20   of this object 
37400 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f 70  represents an op
37410 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68  en BLOB on which
37420 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  .** [sqlite3_blo
37430 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65  b_open | increme
37440 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63  ntal BLOB I/O] c
37450 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64 2e  an be performed.
37460 0a 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 20 74  .** Objects of t
37470 68 69 73 20 74 79 70 65 20 61 72 65 20 63 72 65  his type are cre
37480 61 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  ated by [sqlite3
37490 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a  _blob_open()].**
374a0 20 61 6e 64 20 64 65 73 74 72 6f 79 65 64 20 62   and destroyed b
374b0 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  y [sqlite3_blob_
374c0 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65  close()]..** The
374d0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72   [sqlite3_blob_r
374e0 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  ead()] and [sqli
374f0 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29  te3_blob_write()
37500 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  ] interfaces.** 
37510 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72  can be used to r
37520 65 61 64 20 6f 72 20 77 72 69 74 65 20 73 6d 61  ead or write sma
37530 6c 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 20 6f  ll subsections o
37540 66 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54  f the BLOB..** T
37550 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
37560 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66  _bytes()] interf
37570 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ace returns the 
37580 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f 42  size of the BLOB
37590 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79   in bytes..*/.ty
375a0 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
375b0 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65  ite3_blob sqlite
375c0 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43  3_blob;../*.** C
375d0 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41 20  API3REF: Open A 
375e0 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d 65  BLOB For Increme
375f0 6e 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30  ntal I/O {H17810
37600 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a  } <S30230>.**.**
37610 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 73   This interfaces
37620 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68   opens a [BLOB h
37630 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20  andle | handle] 
37640 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61  to the BLOB loca
37650 74 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52  ted.** in row iR
37660 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75  ow, column zColu
37670 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 6c 65  mn, table zTable
37680 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44 62   in database zDb
37690 3b 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f  ;.** in other wo
376a0 72 64 73 2c 20 74 68 65 20 73 61 6d 65 20 42 4c  rds, the same BL
376b0 4f 42 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65  OB that would be
376c0 20 73 65 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a   selected by:.**
376d0 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20  .** <pre>.**    
376e0 20 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20   SELECT zColumn 
376f0 46 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20  FROM zDb.zTable 
37700 57 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20  WHERE [rowid] = 
37710 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20  iRow;.** </pre> 
37720 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74  {END}.**.** If t
37730 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74  he flags paramet
37740 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  er is non-zero, 
37750 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69 73  then the BLOB is
37760 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64   opened for read
37770 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 61 63  .** and write ac
37780 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 20 7a  cess. If it is z
37790 65 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 69 73  ero, the BLOB is
377a0 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64   opened for read
377b0 20 61 63 63 65 73 73 2e 0a 2a 2a 20 49 74 20 69   access..** It i
377c0 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74  s not possible t
377d0 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20  o open a column 
377e0 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20  that is part of 
377f0 61 6e 20 69 6e 64 65 78 20 6f 72 20 70 72 69 6d  an index or prim
37800 61 72 79 20 0a 2a 2a 20 6b 65 79 20 66 6f 72 20  ary .** key for 
37810 77 72 69 74 69 6e 67 2e 20 5e 49 66 20 5b 66 6f  writing. ^If [fo
37820 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
37830 61 69 6e 74 73 5d 20 61 72 65 20 65 6e 61 62 6c  aints] are enabl
37840 65 64 2c 20 69 74 20 69 73 20 0a 2a 2a 20 6e 6f  ed, it is .** no
37850 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70  t possible to op
37860 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74  en a column that
37870 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 5b 63   is part of a [c
37880 68 69 6c 64 20 6b 65 79 5d 20 66 6f 72 20 77 72  hild key] for wr
37890 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  iting..**.** Not
378a0 65 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62  e that the datab
378b0 61 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20  ase name is not 
378c0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 68 61  the filename tha
378d0 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68  t contains.** th
378e0 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 72  e database but r
378f0 61 74 68 65 72 20 74 68 65 20 73 79 6d 62 6f 6c  ather the symbol
37900 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  ic name of the d
37910 61 74 61 62 61 73 65 20 74 68 61 74 0a 2a 2a 20  atabase that.** 
37920 69 73 20 61 73 73 69 67 6e 65 64 20 77 68 65 6e  is assigned when
37930 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
37940 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69 6e 67   connected using
37950 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f   [ATTACH]..** Fo
37960 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
37970 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 64 61  ase file, the da
37980 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22  tabase name is "
37990 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 45  main"..** For TE
379a0 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 65 20 64  MP tables, the d
379b0 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20  atabase name is 
379c0 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  "temp"..**.** On
379d0 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 54   success, [SQLIT
379e0 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65  E_OK] is returne
379f0 64 20 61 6e 64 20 74 68 65 20 6e 65 77 20 5b 42  d and the new [B
37a00 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 73 20 77  LOB handle] is w
37a10 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70  ritten.** to *pp
37a20 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 73 65 20  Blob. Otherwise 
37a30 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  an [error code] 
37a40 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
37a50 2a 70 70 42 6c 6f 62 20 69 73 20 73 65 74 0a 2a  *ppBlob is set.*
37a60 2a 20 74 6f 20 62 65 20 61 20 6e 75 6c 6c 20 70  * to be a null p
37a70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 54 68 69 73 20  ointer..** This 
37a80 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68  function sets th
37a90 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
37aa0 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 63 6f  ection] error co
37ab0 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 0a 2a  de and message.*
37ac0 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61  * accessible via
37ad0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64   [sqlite3_errcod
37ae0 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  e()] and [sqlite
37af0 33 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e 64 20  3_errmsg()] and 
37b00 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74  related.** funct
37b10 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61 74  ions.  Note that
37b20 20 74 68 65 20 2a 70 70 42 6c 6f 62 20 76 61 72   the *ppBlob var
37b30 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79 73 20  iable is always 
37b40 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e 20 61  initialized in a
37b50 0a 2a 2a 20 77 61 79 20 74 68 61 74 20 6d 61 6b  .** way that mak
37b60 65 73 20 69 74 20 73 61 66 65 20 74 6f 20 69 6e  es it safe to in
37b70 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c  voke [sqlite3_bl
37b80 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f 6e 20 2a  ob_close()] on *
37b90 70 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 61 72 64  ppBlob.** regard
37ba0 6c 65 73 73 20 6f 66 20 74 68 65 20 73 75 63 63  less of the succ
37bb0 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 20 6f  ess or failure o
37bc0 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  f this routine..
37bd0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 77  **.** If the row
37be0 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61 6e   that a BLOB han
37bf0 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73  dle points to is
37c00 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 0a   modified by an.
37c10 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44 45  ** [UPDATE], [DE
37c20 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f 4e  LETE], or by [ON
37c30 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 2d   CONFLICT] side-
37c40 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e 20  effects.** then 
37c50 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20  the BLOB handle 
37c60 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78  is marked as "ex
37c70 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73 20  pired"..** This 
37c80 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 63  is true if any c
37c90 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f 77  olumn of the row
37ca0 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76 65   is changed, eve
37cb0 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 74  n a column.** ot
37cc0 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65  her than the one
37cd0 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65   the BLOB handle
37ce0 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a 20   is open on..** 
37cf0 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  Calls to [sqlite
37d00 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61  3_blob_read()] a
37d10 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  nd [sqlite3_blob
37d20 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0a 2a 2a  _write()] for.**
37d30 20 61 20 65 78 70 69 72 65 64 20 42 4c 4f 42 20   a expired BLOB 
37d40 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 74 68  handle fail with
37d50 20 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 65 20   an return code 
37d60 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54  of [SQLITE_ABORT
37d70 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 77 72  ]..** Changes wr
37d80 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 4c 4f  itten into a BLO
37d90 42 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 42  B prior to the B
37da0 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 61 72 65  LOB expiring are
37db0 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b   not.** rollback
37dc0 20 62 79 20 74 68 65 20 65 78 70 69 72 61 74 69   by the expirati
37dd0 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e 20  on of the BLOB. 
37de0 20 53 75 63 68 20 63 68 61 6e 67 65 73 20 77 69   Such changes wi
37df0 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a  ll eventually.**
37e00 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 20 74   commit if the t
37e10 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74 69  ransaction conti
37e20 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69  nues to completi
37e30 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68  on..**.** Use th
37e40 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
37e50 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61  bytes()] interfa
37e60 63 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ce to determine 
37e70 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74  the size of.** t
37e80 68 65 20 6f 70 65 6e 65 64 20 62 6c 6f 62 2e 20  he opened blob. 
37e90 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 62   The size of a b
37ea0 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62 65 20 63  lob may not be c
37eb0 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 0a 2a  hanged by this.*
37ec0 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 55 73  * interface.  Us
37ed0 65 20 74 68 65 20 5b 55 50 44 41 54 45 5d 20 53  e the [UPDATE] S
37ee0 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 68  QL command to ch
37ef0 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ange the size of
37f00 20 61 0a 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a   a.** blob..**.*
37f10 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  * The [sqlite3_b
37f20 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20  ind_zeroblob()] 
37f30 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  and [sqlite3_res
37f40 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20  ult_zeroblob()] 
37f50 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 61 6e  interfaces.** an
37f60 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 5b  d the built-in [
37f70 7a 65 72 6f 62 6c 6f 62 5d 20 53 51 4c 20 66 75  zeroblob] SQL fu
37f80 6e 63 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73  nction can be us
37f90 65 64 2c 20 69 66 20 64 65 73 69 72 65 64 2c 0a  ed, if desired,.
37fa0 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20  ** to create an 
37fb0 65 6d 70 74 79 2c 20 7a 65 72 6f 2d 66 69 6c 6c  empty, zero-fill
37fc0 65 64 20 62 6c 6f 62 20 69 6e 20 77 68 69 63 68  ed blob in which
37fd0 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74   to read or writ
37fe0 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 69 73 20  e using.** this 
37ff0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a  interface..**.**
38000 20 54 6f 20 61 76 6f 69 64 20 61 20 72 65 73 6f   To avoid a reso
38010 75 72 63 65 20 6c 65 61 6b 2c 20 65 76 65 72 79  urce leak, every
38020 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64   open [BLOB hand
38030 6c 65 5d 20 73 68 6f 75 6c 64 20 65 76 65 6e 74  le] should event
38040 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 72 65 6c 65  ually.** be rele
38050 61 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74  ased by a call t
38060 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  o [sqlite3_blob_
38070 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  close()]..**.** 
38080 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
38090 20 5b 48 31 37 38 31 33 5d 20 5b 48 31 37 38 31   [H17813] [H1781
380a0 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b 48 31 37  4] [H17816] [H17
380b0 38 31 39 5d 20 5b 48 31 37 38 32 31 5d 20 5b 48  819] [H17821] [H
380c0 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45  17824].*/.SQLITE
380d0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
380e0 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71  _blob_open(.  sq
380f0 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20  lite3*,.  const 
38100 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e  char *zDb,.  con
38110 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c  st char *zTable,
38120 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
38130 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65  Column,.  sqlite
38140 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20  3_int64 iRow,.  
38150 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c  int flags,.  sql
38160 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c  ite3_blob **ppBl
38170 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ob.);../*.** CAP
38180 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 20 42  I3REF: Close A B
38190 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 37 38  LOB Handle {H178
381a0 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a  30} <S30230>.**.
381b0 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65  ** Closes an ope
381c0 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e  n [BLOB handle].
381d0 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61  .**.** Closing a
381e0 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 75 73   BLOB shall caus
381f0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  e the current tr
38200 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d  ansaction to com
38210 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 65 20  mit.** if there 
38220 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f  are no other BLO
38230 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70  Bs, no pending p
38240 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
38250 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64  ts, and the.** d
38260 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
38270 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f  on is in [autoco
38280 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49  mmit mode]..** I
38290 66 20 61 6e 79 20 77 72 69 74 65 73 20 77 65 72  f any writes wer
382a0 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42 4c  e made to the BL
382b0 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20 62  OB, they might b
382c0 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 0a  e held in cache.
382d0 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f  ** until the clo
382e0 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66 20  se operation if 
382f0 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 0a 2a  they will fit..*
38300 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 68 65  *.** Closing the
38310 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f 72 63   BLOB often forc
38320 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 0a 2a  es the changes.*
38330 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 6e  * out to disk an
38340 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f 4f 20  d so if any I/O 
38350 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 68  errors occur, th
38360 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 6f  ey will likely o
38370 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 74  ccur.** at the t
38380 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 4c 4f  ime when the BLO
38390 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 41 6e  B is closed.  An
383a0 79 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63  y errors that oc
383b0 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20 63 6c  cur during.** cl
383c0 6f 73 69 6e 67 20 61 72 65 20 72 65 70 6f 72 74  osing are report
383d0 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f  ed as a non-zero
383e0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a 2a   return value..*
383f0 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20 69 73  *.** The BLOB is
38400 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 69 74   closed uncondit
38410 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e 20 69  ionally.  Even i
38420 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  f this routine r
38430 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 72  eturns.** an err
38440 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 42 4c 4f  or code, the BLO
38450 42 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f 73 65  B is still close
38460 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67  d..**.** Calling
38470 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
38480 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65  th a null pointe
38490 72 20 28 77 68 69 63 68 20 61 73 20 77 6f 75 6c  r (which as woul
384a0 64 20 62 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a  d be returned.**
384b0 20 62 79 20 66 61 69 6c 65 64 20 63 61 6c 6c 20   by failed call 
384c0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  to [sqlite3_blob
384d0 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20 61 20 68  _open()]) is a h
384e0 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a  armless no-op..*
384f0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
38500 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d 20 5b  s:.** [H17833] [
38510 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33 39 5d  H17836] [H17839]
38520 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
38530 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
38540 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 6c  close(sqlite3_bl
38550 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ob *);../*.** CA
38560 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e 20 54  PI3REF: Return T
38570 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70  he Size Of An Op
38580 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d  en BLOB {H17840}
38590 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20   <S30230>.**.** 
385a0 52 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65  Returns the size
385b0 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65   in bytes of the
385c0 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62 6c 65   BLOB accessible
385d0 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 73 75 63   via the .** suc
385e0 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64  cessfully opened
385f0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69   [BLOB handle] i
38600 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 75 6d  n its only argum
38610 65 6e 74 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 63  ent.  The.** inc
38620 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49 2f  remental blob I/
38630 4f 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 6f  O routines can o
38640 6e 6c 79 20 72 65 61 64 20 6f 72 20 6f 76 65 72  nly read or over
38650 77 72 69 74 69 6e 67 20 65 78 69 73 74 69 6e 67  writing existing
38660 0a 2a 2a 20 62 6c 6f 62 20 63 6f 6e 74 65 6e 74  .** blob content
38670 3b 20 74 68 65 79 20 63 61 6e 6e 6f 74 20 63 68  ; they cannot ch
38680 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ange the size of
38690 20 61 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 54   a blob..**.** T
386a0 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  his routine only
386b0 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f   works on a [BLO
386c0 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20  B handle] which 
386d0 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64  has been created
386e0 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73  .** by a prior s
386f0 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
38700 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  o [sqlite3_blob_
38710 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63  open()] and whic
38720 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65  h has not.** bee
38730 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c  n closed by [sql
38740 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28  ite3_blob_close(
38750 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79  )].  Passing any
38760 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69   other pointer i
38770 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75  n.** to this rou
38780 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20  tine results in 
38790 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72  undefined and pr
387a0 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62  obably undesirab
387b0 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a  le behavior..**.
387c0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
387d0 0a 2a 2a 20 5b 48 31 37 38 34 33 5d 0a 2a 2f 0a  .** [H17843].*/.
387e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
387f0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65  qlite3_blob_byte
38800 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  s(sqlite3_blob *
38810 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
38820 45 46 3a 20 52 65 61 64 20 44 61 74 61 20 46 72  EF: Read Data Fr
38830 6f 6d 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d  om A BLOB Increm
38840 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 35 30 7d  entally {H17850}
38850 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20   <S30230>.**.** 
38860 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
38870 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 61   used to read da
38880 74 61 20 66 72 6f 6d 20 61 6e 20 6f 70 65 6e 20  ta from an open 
38890 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e  [BLOB handle] in
388a0 74 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73  to a.** caller-s
388b0 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e 20  upplied buffer. 
388c0 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  N bytes of data 
388d0 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  are copied into 
388e0 62 75 66 66 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d  buffer Z.** from
388f0 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20   the open BLOB, 
38900 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73  starting at offs
38910 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a  et iOffset..**.*
38920 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66  * If offset iOff
38930 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e  set is less than
38940 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68   N bytes from th
38950 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f  e end of the BLO
38960 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52  B,.** [SQLITE_ER
38970 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ROR] is returned
38980 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20   and no data is 
38990 72 65 61 64 2e 20 20 49 66 20 4e 20 6f 72 20 69  read.  If N or i
389a0 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73  Offset is.** les
389b0 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51  s than zero, [SQ
389c0 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72  LITE_ERROR] is r
389d0 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64  eturned and no d
389e0 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a 20  ata is read..** 
389f0 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  The size of the 
38a00 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e 63 65 20  blob (and hence 
38a10 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75  the maximum valu
38a20 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29 0a  e of N+iOffset).
38a30 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72 6d  ** can be determ
38a40 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b  ined using the [
38a50 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
38a60 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  es()] interface.
38a70 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70  .**.** An attemp
38a80 74 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 61  t to read from a
38a90 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f 42 20  n expired [BLOB 
38aa0 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 77 69  handle] fails wi
38ab0 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63  th an.** error c
38ac0 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41  ode of [SQLITE_A
38ad0 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20  BORT]..**.** On 
38ae0 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f  success, SQLITE_
38af0 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  OK is returned..
38b00 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e  ** Otherwise, an
38b10 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72   [error code] or
38b20 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65 72   an [extended er
38b30 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74  ror code] is ret
38b40 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  urned..**.** Thi
38b50 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77  s routine only w
38b60 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20  orks on a [BLOB 
38b70 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61  handle] which ha
38b80 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a 2a  s been created.*
38b90 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 63  * by a prior suc
38ba0 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20  cessful call to 
38bb0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70  [sqlite3_blob_op
38bc0 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 20  en()] and which 
38bd0 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20  has not.** been 
38be0 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 74  closed by [sqlit
38bf0 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d  e3_blob_close()]
38c00 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20 6f  .  Passing any o
38c10 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a  ther pointer in.
38c20 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ** to this routi
38c30 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e  ne results in un
38c40 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62  defined and prob
38c50 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65  ably undesirable
38c60 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a   behavior..**.**
38c70 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69   See also: [sqli
38c80 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29  te3_blob_write()
38c90 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
38ca0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 35  ments:.** [H1785
38cb0 33 5d 20 5b 48 31 37 38 35 36 5d 20 5b 48 31 37  3] [H17856] [H17
38cc0 38 35 39 5d 20 5b 48 31 37 38 36 32 5d 20 5b 48  859] [H17862] [H
38cd0 31 37 38 36 33 5d 20 5b 48 31 37 38 36 35 5d 20  17863] [H17865] 
38ce0 5b 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c 49  [H17868].*/.SQLI
38cf0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
38d00 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c  e3_blob_read(sql
38d10 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69  ite3_blob *, voi
38d20 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e 74  d *Z, int N, int
38d30 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a   iOffset);../*.*
38d40 2a 20 43 41 50 49 33 52 45 46 3a 20 57 72 69 74  * CAPI3REF: Writ
38d50 65 20 44 61 74 61 20 49 6e 74 6f 20 41 20 42 4c  e Data Into A BL
38d60 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79  OB Incrementally
38d70 20 7b 48 31 37 38 37 30 7d 20 3c 53 33 30 32 33   {H17870} <S3023
38d80 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  0>.**.** This fu
38d90 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
38da0 6f 20 77 72 69 74 65 20 64 61 74 61 20 69 6e 74  o write data int
38db0 6f 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20  o an open [BLOB 
38dc0 68 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a  handle] from a.*
38dd0 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65  * caller-supplie
38de0 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74 65  d buffer. N byte
38df0 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f  s of data are co
38e00 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75  pied from the bu
38e10 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74  ffer Z.** into t
38e20 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74  he open BLOB, st
38e30 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74  arting at offset
38e40 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20   iOffset..**.** 
38e50 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e  If the [BLOB han
38e60 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20 74  dle] passed as t
38e70 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
38e80 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64  t was not opened
38e90 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20   for.** writing 
38ea0 28 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d  (the flags param
38eb0 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33  eter to [sqlite3
38ec0 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 61  _blob_open()] wa
38ed0 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 69 73  s zero),.** this
38ee0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
38ef0 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e  s [SQLITE_READON
38f00 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  LY]..**.** This 
38f10 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c  function may onl
38f20 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f 6e  y modify the con
38f30 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42 4c 4f  tents of the BLO
38f40 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20  B; it is.** not 
38f50 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 63 72  possible to incr
38f60 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ease the size of
38f70 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20 74 68   a BLOB using th
38f80 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20 6f 66  is API..** If of
38f90 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 20  fset iOffset is 
38fa0 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65  less than N byte
38fb0 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f  s from the end o
38fc0 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b  f the BLOB,.** [
38fd0 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73  SQLITE_ERROR] is
38fe0 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f   returned and no
38ff0 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e   data is written
39000 2e 20 20 49 66 20 4e 20 69 73 0a 2a 2a 20 6c 65  .  If N is.** le
39010 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b 53 51  ss than zero [SQ
39020 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72  LITE_ERROR] is r
39030 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64  eturned and no d
39040 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e 0a  ata is written..
39050 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74  ** The size of t
39060 68 65 20 42 4c 4f 42 20 28 61 6e 64 20 68 65 6e  he BLOB (and hen
39070 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76  ce the maximum v
39080 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65  alue of N+iOffse
39090 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74  t).** can be det
390a0 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68  ermined using th
390b0 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
390c0 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61  bytes()] interfa
390d0 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74  ce..**.** An att
390e0 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 74 6f  empt to write to
390f0 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f   an expired [BLO
39100 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20  B handle] fails 
39110 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72  with an.** error
39120 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45   code of [SQLITE
39130 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 74 65 73  _ABORT].  Writes
39140 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 74 68 61   to the BLOB tha
39150 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 62 65  t occurred.** be
39160 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42 20 68  fore the [BLOB h
39170 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 64 20 61  andle] expired a
39180 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61  re not rolled ba
39190 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 65 78 70  ck by the.** exp
391a0 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 68  iration of the h
391b0 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20 6f 66  andle, though of
391c0 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20 63 68   course those ch
391d0 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68  anges might.** h
391e0 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77 72 69  ave been overwri
391f0 74 74 65 6e 20 62 79 20 74 68 65 20 73 74 61 74  tten by the stat
39200 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70 69 72  ement that expir
39210 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64  ed the BLOB hand
39220 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f 74 68 65  le.** or by othe
39230 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 74  r independent st
39240 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
39250 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49  On success, SQLI
39260 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
39270 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  d..** Otherwise,
39280 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 6f 64 65   an  [error code
39290 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65  ] or an [extende
392a0 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73  d error code] is
392b0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
392c0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   This routine on
392d0 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42  ly works on a [B
392e0 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63  LOB handle] whic
392f0 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74  h has been creat
39300 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72  ed.** by a prior
39310 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
39320 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f   to [sqlite3_blo
39330 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68  b_open()] and wh
39340 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62  ich has not.** b
39350 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73  een closed by [s
39360 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
39370 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61  e()].  Passing a
39380 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72  ny other pointer
39390 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72   in.** to this r
393a0 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69  outine results i
393b0 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20  n undefined and 
393c0 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72  probably undesir
393d0 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a  able behavior..*
393e0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
393f0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61  sqlite3_blob_rea
39400 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  d()]..**.** Requ
39410 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
39420 37 38 37 33 5d 20 5b 48 31 37 38 37 34 5d 20 5b  7873] [H17874] [
39430 48 31 37 38 37 35 5d 20 5b 48 31 37 38 37 36 5d  H17875] [H17876]
39440 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 37 38 37   [H17877] [H1787
39450 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 48 31 37  9] [H17882] [H17
39460 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 38 38 5d  885].** [H17888]
39470 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
39480 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
39490 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c  write(sqlite3_bl
394a0 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ob *, const void
394b0 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20   *z, int n, int 
394c0 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a  iOffset);../*.**
394d0 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75   CAPI3REF: Virtu
394e0 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d 20 4f  al File System O
394f0 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30 7d 20  bjects {H11200} 
39500 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41  <S20100>.**.** A
39510 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73 79 73   virtual filesys
39520 74 65 6d 20 28 56 46 53 29 20 69 73 20 61 6e 20  tem (VFS) is an 
39530 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
39540 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c  ject.** that SQL
39550 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e 74 65  ite uses to inte
39560 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74 68 65  ract.** with the
39570 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72   underlying oper
39580 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d  ating system.  M
39590 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69 6c 64  ost SQLite build
395a0 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a  s come with a.**
395b0 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c 74 20   single default 
395c0 56 46 53 20 74 68 61 74 20 69 73 20 61 70 70 72  VFS that is appr
395d0 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20  opriate for the 
395e0 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a  host computer..*
395f0 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61 6e 20  * New VFSes can 
39600 62 65 20 72 65 67 69 73 74 65 72 65 64 20 61 6e  be registered an
39610 64 20 65 78 69 73 74 69 6e 67 20 56 46 53 65 73  d existing VFSes
39620 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69 73 74   can be unregist
39630 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c  ered..** The fol
39640 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 65  lowing interface
39650 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 2e 0a  s are provided..
39660 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
39670 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e 74  3_vfs_find() int
39680 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
39690 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56 46   pointer to a VF
396a0 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65  S given its name
396b0 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 63  ..** Names are c
396c0 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a  ase sensitive..*
396d0 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65 72 6f  * Names are zero
396e0 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
396f0 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 66  8 strings..** If
39700 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74   there is no mat
39710 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ch, a NULL point
39720 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  er is returned..
39730 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65 20 69  ** If zVfsName i
39740 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20  s NULL then the 
39750 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 72  default VFS is r
39760 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e  eturned..**.** N
39770 65 77 20 56 46 53 65 73 20 61 72 65 20 72 65 67  ew VFSes are reg
39780 69 73 74 65 72 65 64 20 77 69 74 68 20 73 71 6c  istered with sql
39790 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65  ite3_vfs_registe
397a0 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 77  r()..** Each new
397b0 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 65   VFS becomes the
397c0 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 66 20   default VFS if 
397d0 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61  the makeDflt fla
397e0 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54 68 65  g is set..** The
397f0 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20 62 65   same VFS can be
39800 20 72 65 67 69 73 74 65 72 65 64 20 6d 75 6c 74   registered mult
39810 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 6f  iple times witho
39820 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f  ut injury..** To
39830 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74 69 6e   make an existin
39840 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65 20 64  g VFS into the d
39850 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65 67 69  efault VFS, regi
39860 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a 2a 2a  ster it again.**
39870 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65 44 66   with the makeDf
39880 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20 49 66  lt flag set.  If
39890 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 20 56   two different V
398a0 46 53 65 73 20 77 69 74 68 20 74 68 65 0a 2a 2a  FSes with the.**
398b0 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65 20 72   same name are r
398c0 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 62  egistered, the b
398d0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
398e0 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a 20 56  ined.  If a.** V
398f0 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64  FS is registered
39900 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 68 61   with a name tha
39910 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20  t is NULL or an 
39920 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a 2a 2a  empty string,.**
39930 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69   then the behavi
39940 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  or is undefined.
39950 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65  .**.** Unregiste
39960 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65  r a VFS with the
39970 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72   sqlite3_vfs_unr
39980 65 67 69 73 74 65 72 28 29 20 69 6e 74 65 72 66  egister() interf
39990 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64  ace..** If the d
399a0 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 75 6e  efault VFS is un
399b0 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e 6f 74  registered, anot
399c0 68 65 72 20 56 46 53 20 69 73 20 63 68 6f 73 65  her VFS is chose
399d0 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66 61  n as.** the defa
399e0 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 63 65  ult.  The choice
399f0 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56 46 53   for the new VFS
39a00 20 69 73 20 61 72 62 69 74 72 61 72 79 2e 0a 2a   is arbitrary..*
39a10 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
39a20 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 5d 20 5b  s:.** [H11203] [
39a30 48 31 31 32 30 36 5d 20 5b 48 31 31 32 30 39 5d  H11206] [H11209]
39a40 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 31 32 31   [H11212] [H1121
39a50 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a 53  5] [H11218].*/.S
39a60 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
39a70 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76  3_vfs *sqlite3_v
39a80 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68  fs_find(const ch
39a90 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53  ar *zVfsName);.S
39aa0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
39ab0 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
39ac0 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  er(sqlite3_vfs*,
39ad0 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a   int makeDflt);.
39ae0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
39af0 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67  qlite3_vfs_unreg
39b00 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66  ister(sqlite3_vf
39b10 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  s*);../*.** CAPI
39b20 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b 48  3REF: Mutexes {H
39b30 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a  17000} <S20000>.
39b40 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65  **.** The SQLite
39b50 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73 65   core uses these
39b60 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74 68   routines for th
39b70 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e  read.** synchron
39b80 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20  ization. Though 
39b90 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64 65  they are intende
39ba0 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a  d for internal.*
39bb0 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2c  * use by SQLite,
39bc0 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b 73   code that links
39bd0 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65 20   against SQLite 
39be0 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 20  is.** permitted 
39bf0 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68  to use any of th
39c00 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  ese routines..**
39c10 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 73  .** The SQLite s
39c20 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 61  ource code conta
39c30 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70  ins multiple imp
39c40 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20  lementations.** 
39c50 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20 72  of these mutex r
39c60 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70 70  outines.  An app
39c70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d 65  ropriate impleme
39c80 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65  ntation.** is se
39c90 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69 63  lected automatic
39ca0 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d  ally at compile-
39cb0 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  time.  The follo
39cc0 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  wing.** implemen
39cd0 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 69  tations are avai
39ce0 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 4c  lable in the SQL
39cf0 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20  ite core:.**.** 
39d00 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53  <ul>.** <li>   S
39d10 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a  QLITE_MUTEX_OS2.
39d20 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45  ** <li>   SQLITE
39d30 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a 2a  _MUTEX_PTHREAD.*
39d40 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f  * <li>   SQLITE_
39d50 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69  MUTEX_W32.** <li
39d60 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  >   SQLITE_MUTEX
39d70 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  _NOOP.** </ul>.*
39d80 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
39d90 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65  MUTEX_NOOP imple
39da0 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20 73  mentation is a s
39db0 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a  et of routines.*
39dc0 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 72  * that does no r
39dd0 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20  eal locking and 
39de0 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  is appropriate f
39df0 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73  or use in.** a s
39e00 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61  ingle-threaded a
39e10 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 65  pplication.  The
39e20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53   SQLITE_MUTEX_OS
39e30 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54  2,.** SQLITE_MUT
39e40 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 20  EX_PTHREAD, and 
39e50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32  SQLITE_MUTEX_W32
39e60 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
39e70 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 69  .** are appropri
39e80 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f  ate for use on O
39e90 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57  S/2, Unix, and W
39ea0 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  indows..**.** If
39eb0 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
39ec0 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c  led with the SQL
39ed0 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46  ITE_MUTEX_APPDEF
39ee0 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a   preprocessor.**
39ef0 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 28   macro defined (
39f00 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d  with "-DSQLITE_M
39f10 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 2c  UTEX_APPDEF=1"),
39f20 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a   then no mutex.*
39f30 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
39f40 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 74   is included wit
39f50 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 49  h the library. I
39f60 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 0a  n this case the.
39f70 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  ** application m
39f80 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 73  ust supply a cus
39f90 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  tom mutex implem
39fa0 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 74  entation using t
39fb0 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f  he.** [SQLITE_CO
39fc0 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69  NFIG_MUTEX] opti
39fd0 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
39fe0 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 74  3_config() funct
39ff0 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61  ion.** before ca
3a000 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e  lling sqlite3_in
3a010 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e  itialize() or an
3a020 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20 73  y other public s
3a030 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74  qlite3_.** funct
3a040 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20 73  ion that calls s
3a050 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
3a060 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  e()..**.** {H170
3a070 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f  11} The sqlite3_
3a080 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f  mutex_alloc() ro
3a090 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20  utine allocates 
3a0a0 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61  a new.** mutex a
3a0b0 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  nd returns a poi
3a0c0 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31 37  nter to it. {H17
3a0d0 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 75 72  012} If it retur
3a0e0 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20  ns NULL.** that 
3a0f0 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74  means that a mut
3a100 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ex could not be 
3a110 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37 30  allocated. {H170
3a120 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69  13} SQLite.** wi
3a130 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74  ll unwind its st
3a140 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ack and return a
3a150 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31 34  n error. {H17014
3a160 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a  } The argument.*
3a170 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  * to sqlite3_mut
3a180 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e  ex_alloc() is on
3a190 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
3a1a0 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a  er constants:.**
3a1b0 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
3a1c0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46    SQLITE_MUTEX_F
3a1d0 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  AST.** <li>  SQL
3a1e0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
3a1f0 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  IVE.** <li>  SQL
3a200 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
3a210 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20  _MASTER.** <li> 
3a220 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
3a230 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e  ATIC_MEM.** <li>
3a240 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
3a250 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c  TATIC_MEM2.** <l
3a260 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
3a270 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20  _STATIC_PRNG.** 
3a280 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
3a290 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a  EX_STATIC_LRU.**
3a2a0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
3a2b0 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a  TEX_STATIC_LRU2.
3a2c0 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b  ** </ul>.**.** {
3a2d0 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 72 73  H17015} The firs
3a2e0 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20  t two constants 
3a2f0 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75  cause sqlite3_mu
3a300 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63  tex_alloc() to c
3a310 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d  reate.** a new m
3a320 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d  utex.  The new m
3a330 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76  utex is recursiv
3a340 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  e when SQLITE_MU
3a350 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
3a360 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74   is used but not
3a370 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20   necessarily so 
3a380 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  when SQLITE_MUTE
3a390 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 20  X_FAST is used. 
3a3a0 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74  {END}.** The mut
3a3b0 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
3a3c0 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
3a3d0 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e  to make a distin
3a3e0 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e  ction.** between
3a3f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
3a400 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49  CURSIVE and SQLI
3a410 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66  TE_MUTEX_FAST if
3a420 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20   it does.** not 
3a430 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30 31  want to.  {H1701
3a440 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77 69  6} But SQLite wi
3a450 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20  ll only request 
3a460 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  a recursive mute
3a470 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68  x in.** cases wh
3a480 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65  ere it really ne
3a490 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20  eds one.  {END} 
3a4a0 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d  If a faster non-
3a4b0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a  recursive mutex.
3a4c0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
3a4d0 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  n is available o
3a4e0 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66  n the host platf
3a4f0 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73  orm, the mutex s
3a500 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68  ubsystem.** migh
3a510 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20  t return such a 
3a520 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  mutex in respons
3a530 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45  e to SQLITE_MUTE
3a540 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48  X_FAST..**.** {H
3a550 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65 72  17017} The other
3a560 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74   allowed paramet
3a570 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  ers to sqlite3_m
3a580 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63  utex_alloc() eac
3a590 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f  h return.** a po
3a5a0 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69  inter to a stati
3a5b0 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75  c preexisting mu
3a5c0 74 65 78 2e 20 7b 45 4e 44 7d 20 20 53 69 78 20  tex. {END}  Six 
3a5d0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61  static mutexes a
3a5e0 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68  re.** used by th
3a5f0 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f  e current versio
3a600 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75  n of SQLite.  Fu
3a610 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  ture versions of
3a620 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61   SQLite.** may a
3a630 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74  dd additional st
3a640 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53  atic mutexes.  S
3a650 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72  tatic mutexes ar
3a660 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a  e for internal.*
3a670 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20  * use by SQLite 
3a680 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69  only.  Applicati
3a690 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 4c  ons that use SQL
3a6a0 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75  ite mutexes shou
3a6b0 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74  ld.** use only t
3a6c0 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78  he dynamic mutex
3a6d0 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53  es returned by S
3a6e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
3a6f0 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55   or.** SQLITE_MU
3a700 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a  TEX_RECURSIVE..*
3a710 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e 6f  *.** {H17018} No
3a720 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f  te that if one o
3a730 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  f the dynamic mu
3a740 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28  tex parameters (
3a750 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
3a760 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d  T.** or SQLITE_M
3a770 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20  UTEX_RECURSIVE) 
3a780 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c  is used then sql
3a790 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
3a7a0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  ().** returns a 
3a7b0 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20  different mutex 
3a7c0 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20  on every call.  
3a7d0 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f 72  {H17034} But for
3a7e0 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d   the static.** m
3a7f0 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20  utex types, the 
3a800 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65  same mutex is re
3a810 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20  turned on every 
3a820 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a  call that has.**
3a830 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e   the same type n
3a840 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  umber..**.** {H1
3a850 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74 65  7019} The sqlite
3a860 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20 72  3_mutex_free() r
3a870 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74  outine deallocat
3a880 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a  es a previously.
3a890 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e  ** allocated dyn
3a8a0 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31 37  amic mutex. {H17
3a8b0 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20 63  020} SQLite is c
3a8c0 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f  areful to deallo
3a8d0 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64 79  cate every.** dy
3a8e0 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61 74  namic mutex that
3a8f0 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20 7b   it allocates. {
3a900 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e 61  A17021} The dyna
3a910 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 74  mic mutexes must
3a920 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73   not be in.** us
3a930 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20  e when they are 
3a940 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 31  deallocated. {A1
3a950 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e 67  7022} Attempting
3a960 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 61   to deallocate a
3a970 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78   static.** mutex
3a980 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65   results in unde
3a990 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 20  fined behavior. 
3a9a0 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65 20  {H17023} SQLite 
3a9b0 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74 65  never deallocate
3a9c0 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d 75  s.** a static mu
3a9d0 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  tex. {END}.**.**
3a9e0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
3a9f0 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73  ex_enter() and s
3aa00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
3aa10 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65  () routines atte
3aa20 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20  mpt.** to enter 
3aa30 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 34  a mutex. {H17024
3aa40 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72  } If another thr
3aa50 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77  ead is already w
3aa60 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c  ithin the mutex,
3aa70 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
3aa80 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
3aa90 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
3aaa0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
3aab0 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  l return.** SQLI
3aac0 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32 35  TE_BUSY. {H17025
3aad0 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  }  The sqlite3_m
3aae0 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72  utex_try() inter
3aaf0 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53 51  face returns [SQ
3ab00 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e  LITE_OK].** upon
3ab10 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72   successful entr
3ab20 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75 74  y.  {H17026} Mut
3ab30 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69  exes created usi
3ab40 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54  ng.** SQLITE_MUT
3ab50 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e  EX_RECURSIVE can
3ab60 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74   be entered mult
3ab70 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68  iple times by th
3ab80 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a 2a  e same thread..*
3ab90 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73 75  * {H17027} In su
3aba0 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a  ch cases the,.**
3abb0 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65   mutex must be e
3abc0 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e  xited an equal n
3abd0 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62  umber of times b
3abe0 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68  efore another th
3abf0 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65  read.** can ente
3ac00 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66 20  r.  {A17028} If 
3ac10 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
3ac20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61  tries to enter a
3ac30 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e 64  ny other.** kind
3ac40 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20 74   of mutex more t
3ac50 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65  han once, the be
3ac60 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
3ac70 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 7d  ned..** {H17029}
3ac80 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
3ac90 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 75  er exhibit.** su
3aca0 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20 69  ch behavior in i
3acb0 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75  ts own use of mu
3acc0 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d  texes..**.** Som
3acd0 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20 65  e systems (for e
3ace0 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73 20  xample, Windows 
3acf0 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f  95) do not suppo
3ad00 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  rt the operation
3ad10 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  .** implemented 
3ad20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  by sqlite3_mutex
3ad30 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f 73  _try().  On thos
3ad40 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69 74  e systems, sqlit
3ad50 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a 2a  e3_mutex_try().*
3ad60 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65  * will always re
3ad70 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
3ad80 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65 20  .  {H17030} The 
3ad90 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c 79  SQLite core only
3ada0 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73 71   ever uses.** sq
3adb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
3adc0 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61  ) as an optimiza
3add0 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73 20  tion so this is 
3ade0 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61 76  acceptable behav
3adf0 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  ior..**.** {H170
3ae00 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f  31} The sqlite3_
3ae10 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f  mutex_leave() ro
3ae20 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75  utine exits a mu
3ae30 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20  tex that was.** 
3ae40 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72  previously enter
3ae50 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  ed by the same t
3ae60 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32 7d  hread.  {A17032}
3ae70 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a   The behavior.**
3ae80 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66   is undefined if
3ae90 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f   the mutex is no
3aea0 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65  t currently ente
3aeb0 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 61  red by the.** ca
3aec0 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72 20  lling thread or 
3aed0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
3aee0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48 31   allocated.  {H1
3aef0 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69 6c  7033} SQLite wil
3af00 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65 69  l.** never do ei
3af10 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  ther. {END}.**.*
3af20 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e  * If the argumen
3af30 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  t to sqlite3_mut
3af40 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c 69  ex_enter(), sqli
3af50 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 2c  te3_mutex_try(),
3af60 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d   or.** sqlite3_m
3af70 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73 20  utex_leave() is 
3af80 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  a NULL pointer, 
3af90 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20 72  then all three r
3afa0 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61 76  outines.** behav
3afb0 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a  e as no-ops..**.
3afc0 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
3afd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
3afe0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
3aff0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
3b000 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ]..*/.SQLITE_API
3b010 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
3b020 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
3b030 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45  loc(int);.SQLITE
3b040 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
3b050 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c  3_mutex_free(sql
3b060 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51  ite3_mutex*);.SQ
3b070 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
3b080 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
3b090 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a  r(sqlite3_mutex*
3b0a0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
3b0b0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  t sqlite3_mutex_
3b0c0 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  try(sqlite3_mute
3b0d0 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  x*);.SQLITE_API 
3b0e0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74  void sqlite3_mut
3b0f0 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33  ex_leave(sqlite3
3b100 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _mutex*);../*.**
3b110 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78   CAPI3REF: Mutex
3b120 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 20   Methods Object 
3b130 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33 30  {H17120} <S20130
3b140 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
3b150 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  L.**.** An insta
3b160 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
3b170 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68  cture defines th
3b180 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74  e low-level rout
3b190 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20  ines.** used to 
3b1a0 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73 65  allocate and use
3b1b0 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20   mutexes..**.** 
3b1c0 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65 66  Usually, the def
3b1d0 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c 65  ault mutex imple
3b1e0 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76 69  mentations provi
3b1f0 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 72  ded by SQLite ar
3b200 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 2c  e.** sufficient,
3b210 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73 65   however the use
3b220 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f 6e  r has the option
3b230 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e 67   of substituting
3b240 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70   a custom.** imp
3b250 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  lementation for 
3b260 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 6c  specialized depl
3b270 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 65  oyments or syste
3b280 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 4c  ms for which SQL
3b290 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20  ite.** does not 
3b2a0 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 62  provide a suitab
3b2b0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
3b2c0 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  n. In this case,
3b2d0 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72 65   the user.** cre
3b2e0 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61 74  ates and populat
3b2f0 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  es an instance o
3b300 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
3b310 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73   to pass.** to s
3b320 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20  qlite3_config() 
3b330 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 5b  along with the [
3b340 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
3b350 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20  TEX] option..** 
3b360 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e  Additionally, an
3b370 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
3b380 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20  s structure can 
3b390 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a  be used as an.**
3b3a0 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
3b3b0 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20 74   when querying t
3b3c0 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74 68  he system for th
3b3d0 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 0a  e current mutex.
3b3e0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
3b3f0 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53 51  n, using the [SQ
3b400 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d  LITE_CONFIG_GETM
3b410 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a  UTEX] option..**
3b420 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49 6e  .** The xMutexIn
3b430 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65  it method define
3b440 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74  d by this struct
3b450 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61  ure is invoked a
3b460 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73  s.** part of sys
3b470 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  tem initializati
3b480 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  on by the sqlite
3b490 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66  3_initialize() f
3b4a0 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 37  unction..** {H17
3b4b0 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78 49  001} The xMutexI
3b4c0 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61 6c  nit routine shal
3b4d0 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 53  l be called by S
3b4e0 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20 65  QLite once for e
3b4f0 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76 65  ach.** effective
3b500 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
3b510 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 2e  3_initialize()].
3b520 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65  .**.** The xMute
3b530 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66 69  xEnd method defi
3b540 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75  ned by this stru
3b550 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64  cture is invoked
3b560 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73   as.** part of s
3b570 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20 62  ystem shutdown b
3b580 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 68  y the sqlite3_sh
3b590 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69 6f  utdown() functio
3b5a0 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d  n. The.** implem
3b5b0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  entation of this
3b5c0 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65 63   method is expec
3b5d0 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 61  ted to release a
3b5e0 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a  ll outstanding.*
3b5f0 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74 61  * resources obta
3b600 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74 65  ined by the mute
3b610 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d  x methods implem
3b620 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63 69  entation, especi
3b630 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f 62  ally.** those ob
3b640 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78 4d  tained by the xM
3b650 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e  utexInit method.
3b660 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78 4d   {H17003} The xM
3b670 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e 74  utexEnd().** int
3b680 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 20  erface shall be 
3b690 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72  invoked once for
3b6a0 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73   each call to [s
3b6b0 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
3b6c0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  )]..**.** The re
3b6d0 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d 65  maining seven me
3b6e0 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62 79  thods defined by
3b6f0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
3b700 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a 2a  (xMutexAlloc,.**
3b710 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d 75   xMutexFree, xMu
3b720 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65 78  texEnter, xMutex
3b730 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76 65  Try, xMutexLeave
3b740 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e 64  , xMutexHeld and
3b750 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65 6c  .** xMutexNothel
3b760 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  d) implement the
3b770 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72   following inter
3b780 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69 76  faces (respectiv
3b790 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  ely):.**.** <ul>
3b7a0 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c  .**   <li>  [sql
3b7b0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
3b7c0 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c  ()] </li>.**   <
3b7d0 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75  li>  [sqlite3_mu
3b7e0 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c 69  tex_free()] </li
3b7f0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
3b800 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
3b810 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20  r()] </li>.**   
3b820 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d  <li>  [sqlite3_m
3b830 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c 69  utex_try()] </li
3b840 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
3b850 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3b860 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20  e()] </li>.**   
3b870 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d  <li>  [sqlite3_m
3b880 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f 6c  utex_held()] </l
3b890 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
3b8a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
3b8b0 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a  held()] </li>.**
3b8c0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
3b8d0 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65   only difference
3b8e0 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75 62   is that the pub
3b8f0 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58 20  lic sqlite3_XXX 
3b900 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65 72  functions enumer
3b910 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73 69  ated.** above si
3b920 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61 6e  lently ignore an
3b930 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74 68  y invocations th
3b940 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20 70  at pass a NULL p
3b950 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a 2a  ointer instead.*
3b960 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 74  * of a valid mut
3b970 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 69  ex handle. The i
3b980 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
3b990 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64 65  f the methods de
3b9a0 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69 73  fined.** by this
3b9b0 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 6e   structure are n
3b9c0 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 68  ot required to h
3b9d0 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65 2c  andle this case,
3b9e0 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20   the results.** 
3b9f0 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55 4c  of passing a NUL
3ba00 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61  L pointer instea
3ba10 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 74  d of a valid mut
3ba20 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75 6e  ex handle are un
3ba30 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 2e  defined.** (i.e.
3ba40 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62 6c   it is acceptabl
3ba50 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20  e to provide an 
3ba60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74  implementation t
3ba70 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69 66  hat segfaults if
3ba80 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65 64  .** it is passed
3ba90 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 29   a NULL pointer)
3baa0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74  ..**.** The xMut
3bab0 65 78 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 20  exInit() method 
3bac0 6d 75 73 74 20 62 65 20 74 68 72 65 61 64 73 61  must be threadsa
3bad0 66 65 2e 20 20 49 74 20 6d 75 73 74 20 62 65 20  fe.  It must be 
3bae0 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 69  harmless to.** i
3baf0 6e 76 6f 6b 65 20 78 4d 75 74 65 78 49 6e 69 74  nvoke xMutexInit
3bb00 28 29 20 6d 75 74 69 70 6c 65 20 74 69 6d 65 73  () mutiple times
3bb10 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65   within the same
3bb20 20 70 72 6f 63 65 73 73 20 61 6e 64 20 77 69 74   process and wit
3bb30 68 6f 75 74 0a 2a 2a 20 69 6e 74 65 72 76 65 6e  hout.** interven
3bb40 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 78 4d 75  ing calls to xMu
3bb50 74 65 78 45 6e 64 28 29 2e 20 20 53 65 63 6f 6e  texEnd().  Secon
3bb60 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74  d and subsequent
3bb70 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 78 4d 75   calls to.** xMu
3bb80 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20 62  texInit() must b
3bb90 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20  e no-ops..**.** 
3bba0 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75 73  xMutexInit() mus
3bbb0 74 20 6e 6f 74 20 75 73 65 20 53 51 4c 69 74 65  t not use SQLite
3bbc0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
3bbd0 6f 6e 20 28 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  on ([sqlite3_mal
3bbe0 6c 6f 63 28 29 5d 0a 2a 2a 20 61 6e 64 20 69 74  loc()].** and it
3bbf0 73 20 61 73 73 6f 63 69 61 74 65 73 29 2e 20 20  s associates).  
3bc00 53 69 6d 69 6c 61 72 6c 79 2c 20 78 4d 75 74 65  Similarly, xMute
3bc10 78 41 6c 6c 6f 63 28 29 20 6d 75 73 74 20 6e 6f  xAlloc() must no
3bc20 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65 6d  t use SQLite mem
3bc30 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  ory.** allocatio
3bc40 6e 20 66 6f 72 20 61 20 73 74 61 74 69 63 20 6d  n for a static m
3bc50 75 74 65 78 2e 20 20 48 6f 77 65 76 65 72 20 78  utex.  However x
3bc60 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 61 79  MutexAlloc() may
3bc70 20 75 73 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6d   use SQLite.** m
3bc80 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
3bc90 20 66 6f 72 20 61 20 66 61 73 74 20 6f 72 20 72   for a fast or r
3bca0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 2e 0a  ecursive mutex..
3bcb0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  **.** SQLite wil
3bcc0 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 4d 75  l invoke the xMu
3bcd0 74 65 78 45 6e 64 28 29 20 6d 65 74 68 6f 64 20  texEnd() method 
3bce0 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68  when [sqlite3_sh
3bcf0 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a 20  utdown()] is.** 
3bd00 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f 6e 6c 79  called, but only
3bd10 20 69 66 20 74 68 65 20 70 72 69 6f 72 20 63 61   if the prior ca
3bd20 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 6e 69 74  ll to xMutexInit
3bd30 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54 45   returned SQLITE
3bd40 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d 75 74 65  _OK..** If xMute
3bd50 78 49 6e 69 74 20 66 61 69 6c 73 20 69 6e 20 61  xInit fails in a
3bd60 6e 79 20 77 61 79 2c 20 69 74 20 69 73 20 65 78  ny way, it is ex
3bd70 70 65 63 74 65 64 20 74 6f 20 63 6c 65 61 6e 20  pected to clean 
3bd80 75 70 20 61 66 74 65 72 20 69 74 73 65 6c 66 0a  up after itself.
3bd90 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74 75  ** prior to retu
3bda0 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64 65  rning..*/.typede
3bdb0 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
3bdc0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73  _mutex_methods s
3bdd0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
3bde0 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c  hods;.struct sql
3bdf0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
3be00 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 4d 75  ds {.  int (*xMu
3be10 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 3b 0a  texInit)(void);.
3be20 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 45 6e    int (*xMutexEn
3be30 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c 69  d)(void);.  sqli
3be40 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 4d 75  te3_mutex *(*xMu
3be50 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a  texAlloc)(int);.
3be60 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 46    void (*xMutexF
3be70 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74  ree)(sqlite3_mut
3be80 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a  ex *);.  void (*
3be90 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71 6c  xMutexEnter)(sql
3bea0 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20  ite3_mutex *);. 
3beb0 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72 79   int (*xMutexTry
3bec0 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  )(sqlite3_mutex 
3bed0 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75  *);.  void (*xMu
3bee0 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74 65  texLeave)(sqlite
3bef0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e  3_mutex *);.  in
3bf00 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 29 28  t (*xMutexHeld)(
3bf10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29  sqlite3_mutex *)
3bf20 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78  ;.  int (*xMutex
3bf30 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 65 33  Notheld)(sqlite3
3bf40 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f  _mutex *);.};../
3bf50 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d  *.** CAPI3REF: M
3bf60 75 74 65 78 20 56 65 72 69 66 69 63 61 74 69 6f  utex Verificatio
3bf70 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 37 30  n Routines {H170
3bf80 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 3c 53 33  80} <S20130> <S3
3bf90 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0800>.**.** The 
3bfa0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
3bfb0 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  ld() and sqlite3
3bfc0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
3bfd0 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65   routines.** are
3bfe0 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73   intended for us
3bff0 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28  e inside assert(
3c000 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 7b 48  ) statements. {H
3c010 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c 69 74  17081} The SQLit
3c020 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72 20  e core.** never 
3c030 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74 69  uses these routi
3c040 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73 69 64  nes except insid
3c050 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 61 6e  e an assert() an
3c060 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a  d applications.*
3c070 2a 20 61 72 65 20 61 64 76 69 73 65 64 20 74 6f  * are advised to
3c080 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61 64   follow the lead
3c090 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20 20 7b   of the core.  {
3c0a0 48 31 37 30 38 32 7d 20 54 68 65 20 63 6f 72 65  H17082} The core
3c0b0 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 65   only.** provide
3c0c0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
3c0d0 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f 75 74  s for these rout
3c0e0 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69 73 20  ines when it is 
3c0f0 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68  compiled.** with
3c100 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 42 55   the SQLITE_DEBU
3c110 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 30 38 37  G flag.  {A17087
3c120 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74 65 78  } External mutex
3c130 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
3c140 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72 65 71  .** are only req
3c150 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64 65  uired to provide
3c160 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
3c170 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  if SQLITE_DEBUG 
3c180 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e  is.** defined an
3c190 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 6e  d if NDEBUG is n
3c1a0 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a  ot defined..**.*
3c1b0 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 65 73 65  * {H17083} These
3c1c0 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64   routines should
3c1d0 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   return true if 
3c1e0 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74 68 65  the mutex in the
3c1f0 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69  ir argument.** i
3c200 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 68 65  s held or not he
3c210 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  ld, respectively
3c220 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  , by the calling
3c230 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b   thread..**.** {
3c240 58 31 37 30 38 34 7d 20 54 68 65 20 69 6d 70 6c  X17084} The impl
3c250 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f  ementation is no
3c260 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 72  t required to pr
3c270 6f 76 69 64 65 64 20 76 65 72 73 69 6f 6e 73 20  ovided versions 
3c280 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74  of these.** rout
3c290 69 6e 65 73 20 74 68 61 74 20 61 63 74 75 61 6c  ines that actual
3c2a0 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 68 65 20  ly work. If the 
3c2b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64  implementation d
3c2c0 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 20  oes not provide 
3c2d0 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 72 73 69  working.** versi
3c2e0 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 72 6f 75  ons of these rou
3c2f0 74 69 6e 65 73 2c 20 69 74 20 73 68 6f 75 6c 64  tines, it should
3c300 20 61 74 20 6c 65 61 73 74 20 70 72 6f 76 69 64   at least provid
3c310 65 20 73 74 75 62 73 20 74 68 61 74 20 61 6c 77  e stubs that alw
3c320 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72  ays.** return tr
3c330 75 65 20 73 6f 20 74 68 61 74 20 6f 6e 65 20 64  ue so that one d
3c340 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 70 75 72  oes not get spur
3c350 69 6f 75 73 20 61 73 73 65 72 74 69 6f 6e 20 66  ious assertion f
3c360 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b  ailures..**.** {
3c370 48 31 37 30 38 35 7d 20 49 66 20 74 68 65 20 61  H17085} If the a
3c380 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
3c390 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20  e3_mutex_held() 
3c3a0 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  is a NULL pointe
3c3b0 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 6f  r then.** the ro
3c3c0 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74  utine should ret
3c3d0 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d 20 54 68  urn 1.  {END} Th
3c3e0 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74 65 72  is seems counter
3c3f0 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e 63 65  -intuitive since
3c400 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 68 65 20  .** clearly the 
3c410 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 62 65 20  mutex cannot be 
3c420 68 65 6c 64 20 69 66 20 69 74 20 64 6f 65 73 20  held if it does 
3c430 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 75 74 20  not exist.  But 
3c440 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 61 73 6f  the.** the reaso
3c450 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f 65 73  n the mutex does
3c460 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20 62 65   not exist is be
3c470 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 64 20  cause the build 
3c480 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67 20  is not.** using 
3c490 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20 77 65  mutexes.  And we
3c4a0 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65   do not want the
3c4b0 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74 61 69   assert() contai
3c4c0 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 61 6c 6c  ning the.** call
3c4d0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
3c4e0 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61 69 6c  x_held() to fail
3c4f0 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  , so a non-zero 
3c500 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 74 68 65  return is.** the
3c510 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 68 69   appropriate thi
3c520 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 31 37 30  ng to do.  {H170
3c530 38 36 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f  86} The sqlite3_
3c540 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 0a  mutex_notheld().
3c550 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68 6f  ** interface sho
3c560 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72 6e 20  uld also return 
3c570 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61 20 4e  1 when given a N
3c580 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  ULL pointer..*/.
3c590 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3c5a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
3c5b0 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a  d(sqlite3_mutex*
3c5c0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
3c5d0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  t sqlite3_mutex_
3c5e0 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f  notheld(sqlite3_
3c5f0 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  mutex*);../*.** 
3c600 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20  CAPI3REF: Mutex 
3c610 54 79 70 65 73 20 7b 48 31 37 30 30 31 7d 20 3c  Types {H17001} <
3c620 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  H17000>.**.** Th
3c630 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  e [sqlite3_mutex
3c640 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72 66  _alloc()] interf
3c650 61 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ace takes a sing
3c660 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77  le argument.** w
3c670 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20 74  hich is one of t
3c680 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e  hese integer con
3c690 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  stants..**.** Th
3c6a0 65 20 73 65 74 20 6f 66 20 73 74 61 74 69 63 20  e set of static 
3c6b0 6d 75 74 65 78 65 73 20 6d 61 79 20 63 68 61 6e  mutexes may chan
3c6c0 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 4c 69  ge from one SQLi
3c6d0 74 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 68  te release to th
3c6e0 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70 6c  e.** next.  Appl
3c6f0 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76  ications that ov
3c700 65 72 72 69 64 65 20 74 68 65 20 62 75 69 6c 74  erride the built
3c710 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63 20  -in mutex logic 
3c720 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 70 61  must be.** prepa
3c730 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 61  red to accommoda
3c740 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74  te additional st
3c750 61 74 69 63 20 6d 75 74 65 78 65 73 2e 0a 2a 2f  atic mutexes..*/
3c760 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3c770 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 20  MUTEX_FAST      
3c780 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65         0.#define
3c790 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
3c7a0 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 31  CURSIVE        1
3c7b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3c7c0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
3c7d0 54 45 52 20 20 20 20 32 0a 23 64 65 66 69 6e 65  TER    2.#define
3c7e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
3c7f0 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 33  ATIC_MEM       3
3c800 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c    /* sqlite3_mal
3c810 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  loc() */.#define
3c820 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
3c830 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 20 34  ATIC_MEM2      4
3c840 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f    /* NOT USED */
3c850 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3c860 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 45  MUTEX_STATIC_OPE
3c870 4e 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c  N      4  /* sql
3c880 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 29 20  ite3BtreeOpen() 
3c890 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3c8a0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50  E_MUTEX_STATIC_P
3c8b0 52 4e 47 20 20 20 20 20 20 35 20 20 2f 2a 20 73  RNG      5  /* s
3c8c0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 29 20  qlite3_random() 
3c8d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3c8e0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
3c8f0 52 55 20 20 20 20 20 20 20 36 20 20 2f 2a 20 6c  RU       6  /* l
3c900 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a  ru page list */.
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 32  UTEX_STATIC_LRU2
3c930 20 20 20 20 20 20 37 20 20 2f 2a 20 6c 72 75 20        7  /* lru 
3c940 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f 2a  page list */../*
3c950 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
3c960 74 72 69 65 76 65 20 74 68 65 20 6d 75 74 65 78  trieve the mutex
3c970 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20   for a database 
3c980 63 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 37 30  connection {H170
3c990 30 32 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a  02} <H17000>.**.
3c9a0 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63  ** This interfac
3c9b0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
3c9c0 74 65 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ter the [sqlite3
3c9d0 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 20 74  _mutex] object t
3c9e0 68 61 74 20 0a 2a 2a 20 73 65 72 69 61 6c 69 7a  hat .** serializ
3c9f0 65 73 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  es access to the
3ca00 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3ca10 63 74 69 6f 6e 5d 20 67 69 76 65 6e 20 69 6e 20  ction] given in 
3ca20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  the argument.** 
3ca30 77 68 65 6e 20 74 68 65 20 5b 74 68 72 65 61 64  when the [thread
3ca40 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 65 72  ing mode] is Ser
3ca50 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 74  ialized..** If t
3ca60 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f  he [threading mo
3ca70 64 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d 74 68  de] is Single-th
3ca80 72 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d 74 68  read or Multi-th
3ca90 72 65 61 64 20 74 68 65 6e 20 74 68 69 73 0a 2a  read then this.*
3caa0 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  * routine return
3cab0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
3cac0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3cad0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73  sqlite3_mutex *s
3cae0 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 28  qlite3_db_mutex(
3caf0 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
3cb00 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 77 2d  * CAPI3REF: Low-
3cb10 4c 65 76 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66  Level Control Of
3cb20 20 44 61 74 61 62 61 73 65 20 46 69 6c 65 73 20   Database Files 
3cb30 7b 48 31 31 33 30 30 7d 20 3c 53 33 30 38 30 30  {H11300} <S30800
3cb40 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 31 7d  >.**.** {H11301}
3cb50 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69   The [sqlite3_fi
3cb60 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e  le_control()] in
3cb70 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20 61 20  terface makes a 
3cb80 64 69 72 65 63 74 20 63 61 6c 6c 20 74 6f 20 74  direct call to t
3cb90 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72  he.** xFileContr
3cba0 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68  ol method for th
3cbb0 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  e [sqlite3_io_me
3cbc0 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 73  thods] object as
3cbd0 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68  sociated.** with
3cbe0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61   a particular da
3cbf0 74 61 62 61 73 65 20 69 64 65 6e 74 69 66 69 65  tabase identifie
3cc00 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20  d by the second 
3cc10 61 72 67 75 6d 65 6e 74 2e 20 7b 48 31 31 33 30  argument. {H1130
3cc20 32 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f  2} The.** name o
3cc30 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
3cc40 73 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67  s the name assig
3cc50 6e 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62  ned to the datab
3cc60 61 73 65 20 62 79 20 74 68 65 0a 2a 2a 20 3c 61  ase by the.** <a
3cc70 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74 74 61   href="lang_atta
3cc80 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43 48 3c  ch.html">ATTACH<
3cc90 2f 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20  /a> SQL command 
3cca0 74 68 61 74 20 6f 70 65 6e 65 64 20 74 68 65 0a  that opened the.
3ccb0 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 7b 48 31  ** database. {H1
3ccc0 31 33 30 33 7d 20 54 6f 20 63 6f 6e 74 72 6f 6c  1303} To control
3ccd0 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
3cce0 73 65 20 66 69 6c 65 2c 20 75 73 65 20 74 68 65  se file, use the
3ccf0 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a 20   name "main".** 
3cd00 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  or a NULL pointe
3cd10 72 2e 20 7b 48 31 31 33 30 34 7d 20 54 68 65 20  r. {H11304} The 
3cd20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68  third and fourth
3cd30 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74   parameters to t
3cd40 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61  his routine.** a
3cd50 72 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74  re passed direct
3cd60 6c 79 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ly through to th
3cd70 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 69  e second and thi
3cd80 72 64 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66  rd parameters of
3cd90 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65 43 6f 6e  .** the xFileCon
3cda0 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20 7b 48  trol method.  {H
3cdb0 31 31 33 30 35 7d 20 54 68 65 20 72 65 74 75 72  11305} The retur
3cdc0 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 78  n value of the x
3cdd0 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 6d  FileControl.** m
3cde0 65 74 68 6f 64 20 62 65 63 6f 6d 65 73 20 74 68  ethod becomes th
3cdf0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f  e return value o
3ce00 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  f this routine..
3ce10 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 7d 20 49  **.** {H11306} I
3ce20 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  f the second par
3ce30 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d 65 29  ameter (zDbName)
3ce40 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
3ce50 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 79 0a  the name of any.
3ce60 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  ** open database
3ce70 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49   file, then SQLI
3ce80 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75  TE_ERROR is retu
3ce90 72 6e 65 64 2e 20 7b 48 31 31 33 30 37 7d 20 54  rned. {H11307} T
3cea0 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64  his error.** cod
3ceb0 65 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d 62 65  e is not remembe
3cec0 72 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74  red and will not
3ced0 20 62 65 20 72 65 63 61 6c 6c 65 64 20 62 79 20   be recalled by 
3cee0 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65  [sqlite3_errcode
3cef0 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74  ()].** or [sqlit
3cf00 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20 7b 41  e3_errmsg()]. {A
3cf10 31 31 33 30 38 7d 20 54 68 65 20 75 6e 64 65 72  11308} The under
3cf20 6c 79 69 6e 67 20 78 46 69 6c 65 43 6f 6e 74 72  lying xFileContr
3cf30 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 67 68 74 0a  ol method might.
3cf40 2a 2a 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 53  ** also return S
3cf50 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 7b 41  QLITE_ERROR.  {A
3cf60 31 31 33 30 39 7d 20 54 68 65 72 65 20 69 73 20  11309} There is 
3cf70 6e 6f 20 77 61 79 20 74 6f 20 64 69 73 74 69 6e  no way to distin
3cf80 67 75 69 73 68 20 62 65 74 77 65 65 6e 0a 2a 2a  guish between.**
3cf90 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 7a 44   an incorrect zD
3cfa0 62 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53 51 4c  bName and an SQL
3cfb0 49 54 45 5f 45 52 52 4f 52 20 72 65 74 75 72 6e  ITE_ERROR return
3cfc0 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c   from the underl
3cfd0 79 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e  ying.** xFileCon
3cfe0 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 7b 45 4e  trol method. {EN
3cff0 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  D}.**.** See als
3d000 6f 3a 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c  o: [SQLITE_FCNTL
3d010 5f 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a 53  _LOCKSTATE].*/.S
3d020 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
3d030 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
3d040 6f 6c 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e  ol(sqlite3*, con
3d050 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65  st char *zDbName
3d060 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 2a 29  , int op, void*)
3d070 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3d080 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65 72  F: Testing Inter
3d090 66 61 63 65 20 7b 48 31 31 34 30 30 7d 20 3c 53  face {H11400} <S
3d0a0 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  30800>.**.** The
3d0b0 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
3d0c0 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63  ntrol() interfac
3d0d0 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61  e is used to rea
3d0e0 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c 0a 2a  d out internal.*
3d0f0 2a 20 73 74 61 74 65 20 6f 66 20 53 51 4c 69 74  * state of SQLit
3d100 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63 74 20  e and to inject 
3d110 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 51 4c 69  faults into SQLi
3d120 74 65 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a  te for testing.*
3d130 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68 65  * purposes.  The
3d140 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
3d150 20 69 73 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e   is an operation
3d160 20 63 6f 64 65 20 74 68 61 74 20 64 65 74 65 72   code that deter
3d170 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6e 75 6d  mines.** the num
3d180 62 65 72 2c 20 6d 65 61 6e 69 6e 67 2c 20 61 6e  ber, meaning, an
3d190 64 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 61  d operation of a
3d1a0 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 70 61  ll subsequent pa
3d1b0 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  rameters..**.** 
3d1c0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69  This interface i
3d1d0 73 20 6e 6f 74 20 66 6f 72 20 75 73 65 20 62 79  s not for use by
3d1e0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20   applications.  
3d1f0 49 74 20 65 78 69 73 74 73 20 73 6f 6c 65 6c 79  It exists solely
3d200 0a 2a 2a 20 66 6f 72 20 76 65 72 69 66 79 69 6e  .** for verifyin
3d210 67 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70  g the correct op
3d220 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53  eration of the S
3d230 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20  QLite library.  
3d240 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20  Depending.** on 
3d250 68 6f 77 20 74 68 65 20 53 51 4c 69 74 65 20 6c  how the SQLite l
3d260 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c  ibrary is compil
3d270 65 64 2c 20 74 68 69 73 20 69 6e 74 65 72 66 61  ed, this interfa
3d280 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 65 78 69  ce might not exi
3d290 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65  st..**.** The de
3d2a0 74 61 69 6c 73 20 6f 66 20 74 68 65 20 6f 70 65  tails of the ope
3d2b0 72 61 74 69 6f 6e 20 63 6f 64 65 73 2c 20 74 68  ration codes, th
3d2c0 65 69 72 20 6d 65 61 6e 69 6e 67 73 2c 20 74 68  eir meanings, th
3d2d0 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  e parameters.** 
3d2e0 74 68 65 79 20 74 61 6b 65 2c 20 61 6e 64 20 77  they take, and w
3d2f0 68 61 74 20 74 68 65 79 20 64 6f 20 61 72 65 20  hat they do are 
3d300 61 6c 6c 20 73 75 62 6a 65 63 74 20 74 6f 20 63  all subject to c
3d310 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f  hange without no
3d320 74 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 20  tice..** Unlike 
3d330 6d 6f 73 74 20 6f 66 20 74 68 65 20 53 51 4c 69  most of the SQLi
3d340 74 65 20 41 50 49 2c 20 74 68 69 73 20 66 75 6e  te API, this fun
3d350 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 75 61  ction is not gua
3d360 72 61 6e 74 65 65 64 20 74 6f 0a 2a 2a 20 6f 70  ranteed to.** op
3d370 65 72 61 74 65 20 63 6f 6e 73 69 73 74 65 6e 74  erate consistent
3d380 6c 79 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c 65  ly from one rele
3d390 61 73 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e  ase to the next.
3d3a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
3d3b0 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  nt sqlite3_test_
3d3c0 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20  control(int op, 
3d3d0 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ...);../*.** CAP
3d3e0 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20 49  I3REF: Testing I
3d3f0 6e 74 65 72 66 61 63 65 20 4f 70 65 72 61 74 69  nterface Operati
3d400 6f 6e 20 43 6f 64 65 73 20 7b 48 31 31 34 31 30  on Codes {H11410
3d410 7d 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a 2a  } <H11400>.**.**
3d420 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73   These constants
3d430 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 6f   are the valid o
3d440 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 70 61  peration code pa
3d450 72 61 6d 65 74 65 72 73 20 75 73 65 64 0a 2a 2a  rameters used.**
3d460 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
3d470 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74  gument to [sqlit
3d480 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
3d490 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  )]..**.** These 
3d4a0 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20 74  parameters and t
3d4b0 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 20 61 72  heir meanings ar
3d4c0 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  e subject to cha
3d4d0 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20 6e  nge.** without n
3d4e0 6f 74 69 63 65 2e 20 20 54 68 65 73 65 20 76 61  otice.  These va
3d4f0 6c 75 65 73 20 61 72 65 20 66 6f 72 20 74 65 73  lues are for tes
3d500 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e  ting purposes on
3d510 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69  ly..** Applicati
3d520 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75  ons should not u
3d530 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20  se any of these 
3d540 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 74 68  parameters or th
3d550 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74 65  e.** [sqlite3_te
3d560 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e  st_control()] in
3d570 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66  terface..*/.#def
3d580 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ine SQLITE_TESTC
3d590 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 20 20  TRL_PRNG_SAVE   
3d5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23               5.#
3d5b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
3d5c0 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54  STCTRL_PRNG_REST
3d5d0 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ORE             
3d5e0 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  6.#define SQLITE
3d5f0 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52  _TESTCTRL_PRNG_R
3d600 45 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20  ESET            
3d610 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c     7.#define SQL
3d620 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49 54  ITE_TESTCTRL_BIT
3d630 56 45 43 5f 54 45 53 54 20 20 20 20 20 20 20 20  VEC_TEST        
3d640 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20        8.#define 
3d650 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3d660 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c 20 20 20  FAULT_INSTALL   
3d670 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69           9.#defi
3d680 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
3d690 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43  RL_BENIGN_MALLOC
3d6a0 5f 48 4f 4f 4b 53 20 20 20 20 20 31 30 0a 23 64  _HOOKS     10.#d
3d6b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
3d6c0 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59  TCTRL_PENDING_BY
3d6d0 54 45 20 20 20 20 20 20 20 20 20 20 20 20 31 31  TE            11
3d6e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3d6f0 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54 20  TESTCTRL_ASSERT 
3d700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d710 20 31 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   12.#define SQLI
3d720 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 41  TE_TESTCTRL_ALWA
3d730 59 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  YS              
3d740 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 53      13.#define S
3d750 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 52  QLITE_TESTCTRL_R
3d760 45 53 45 52 56 45 20 20 20 20 20 20 20 20 20 20  ESERVE          
3d770 20 20 20 20 20 20 20 31 34 0a 0a 2f 2a 0a 2a 2a         14../*.**
3d780 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 69 74   CAPI3REF: SQLit
3d790 65 20 52 75 6e 74 69 6d 65 20 53 74 61 74 75 73  e Runtime Status
3d7a0 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 30 32 30   {H17200} <S6020
3d7b0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
3d7c0 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  AL.**.** This in
3d7d0 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20  terface is used 
3d7e0 74 6f 20 72 65 74 72 69 65 76 65 20 72 75 6e 74  to retrieve runt
3d7f0 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72  ime status infor
3d800 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20  mation.** about 
3d810 74 68 65 20 70 72 65 66 6f 72 6d 61 6e 63 65 20  the preformance 
3d820 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f  of SQLite, and o
3d830 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73  ptionally to res
3d840 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69  et various.** hi
3d850 67 68 77 61 74 65 72 20 6d 61 72 6b 73 2e 20 20  ghwater marks.  
3d860 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
3d870 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  nt is an integer
3d880 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65   code for.** the
3d890 20 73 70 65 63 69 66 69 63 20 70 61 72 61 6d 65   specific parame
3d8a0 74 65 72 20 74 6f 20 6d 65 61 73 75 72 65 2e 20  ter to measure. 
3d8b0 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65   Recognized inte
3d8c0 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65  ger codes.** are
3d8d0 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5b 53 51   of the form [SQ
3d8e0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f  LITE_STATUS_MEMO
3d8f0 52 59 5f 55 53 45 44 20 7c 20 53 51 4c 49 54 45  RY_USED | SQLITE
3d900 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a  _STATUS_...]..**
3d910 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   The current val
3d920 75 65 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65  ue of the parame
3d930 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 20  ter is returned 
3d940 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a  into *pCurrent..
3d950 2a 2a 20 54 68 65 20 68 69 67 68 65 73 74 20 72  ** The highest r
3d960 65 63 6f 72 64 65 64 20 76 61 6c 75 65 20 69 73  ecorded value is
3d970 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 48   returned in *pH
3d980 69 67 68 77 61 74 65 72 2e 20 20 49 66 20 74 68  ighwater.  If th
3d990 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 67 20 69  e.** resetFlag i
3d9a0 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65  s true, then the
3d9b0 20 68 69 67 68 65 73 74 20 72 65 63 6f 72 64 20   highest record 
3d9c0 76 61 6c 75 65 20 69 73 20 72 65 73 65 74 20 61  value is reset a
3d9d0 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61  fter.** *pHighwa
3d9e0 74 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e 20  ter is written. 
3d9f0 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20  Some parameters 
3da00 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20 74 68  do not record th
3da10 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c  e highest.** val
3da20 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 65 20 70  ue.  For those p
3da30 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74  arameters.** not
3da40 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20  hing is written 
3da50 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 74 65 72  into *pHighwater
3da60 20 61 6e 64 20 74 68 65 20 72 65 73 65 74 46 6c   and the resetFl
3da70 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a  ag is ignored..*
3da80 2a 20 4f 74 68 65 72 20 70 61 72 61 6d 65 74 65  * Other paramete
3da90 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74  rs record only t
3daa0 68 65 20 68 69 67 68 77 61 74 65 72 20 6d 61 72  he highwater mar
3dab0 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 63 75  k and not the cu
3dac0 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20  rrent.** value. 
3dad0 20 46 6f 72 20 74 68 65 73 65 20 6c 61 74 74 65   For these latte
3dae0 72 20 70 61 72 61 6d 65 74 65 72 73 20 6e 6f 74  r parameters not
3daf0 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20  hing is written 
3db00 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a  into *pCurrent..
3db10 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
3db20 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ne returns SQLIT
3db30 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20  E_OK on success 
3db40 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a  and a non-zero.*
3db50 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f  * [error code] o
3db60 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a  n failure..**.**
3db70 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
3db80 20 74 68 72 65 61 64 73 61 66 65 20 62 75 74 20   threadsafe but 
3db90 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20  is not atomic.  
3dba0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e  This routine can
3dbb0 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 68   be.** called wh
3dbc0 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61 64  ile other thread
3dbd0 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74 68  s are running th
3dbe0 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65 72  e same or differ
3dbf0 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e  ent SQLite.** in
3dc00 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76  terfaces.  Howev
3dc10 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72 65  er the values re
3dc20 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72 72  turned in *pCurr
3dc30 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 67  ent and.** *pHig
3dc40 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20 74  hwater reflect t
3dc50 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51 4c  he status of SQL
3dc60 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e 74  ite at different
3dc70 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65 0a   points in time.
3dc80 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f 73  ** and it is pos
3dc90 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68  sible that anoth
3dca0 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74 20  er thread might 
3dcb0 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61 6d  change the param
3dcc0 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77 65  eter.** in betwe
3dcd0 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68 65  en the times whe
3dce0 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 20  n *pCurrent and 
3dcf0 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65 20  *pHighwater are 
3dd00 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53  written..**.** S
3dd10 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65  ee also: [sqlite
3dd20 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a 2a  3_db_status()].*
3dd30 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
3dd40 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
3dd50 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61   int sqlite3_sta
3dd60 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20  tus(int op, int 
3dd70 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a  *pCurrent, int *
3dd80 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20  pHighwater, int 
3dd90 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a  resetFlag);.../*
3dda0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74  .** CAPI3REF: St
3ddb0 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 20  atus Parameters 
3ddc0 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 30 30  {H17250} <H17200
3ddd0 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
3dde0 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e  L.**.** These in
3ddf0 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20  teger constants 
3de00 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 6f 75  designate variou
3de10 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 74 75  s run-time statu
3de20 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  s parameters.** 
3de30 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 74 75  that can be retu
3de40 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  rned by [sqlite3
3de50 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a  _status()]..**.*
3de60 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51  * <dl>.** <dt>SQ
3de70 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f  LITE_STATUS_MEMO
3de80 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20  RY_USED</dt>.** 
3de90 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
3dea0 65 72 20 69 73 20 74 68 65 20 63 75 72 72 65 6e  er is the curren
3deb0 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  t amount of memo
3dec0 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0a 2a  ry checked out.*
3ded0 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  * using [sqlite3
3dee0 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 68  _malloc()], eith
3def0 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69  er directly or i
3df00 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 0a  ndirectly.  The.
3df10 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75 64  ** figure includ
3df20 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74 6f  es calls made to
3df30 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
3df40 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c 69  ()] by the appli
3df50 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e  cation.** and in
3df60 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 73  ternal memory us
3df70 61 67 65 20 62 79 20 74 68 65 20 53 51 4c 69 74  age by the SQLit
3df80 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 72 61  e library.  Scra
3df90 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f  tch memory.** co
3dfa0 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c  ntrolled by [SQL
3dfb0 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
3dfc0 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 61 72  CH] and auxiliar
3dfd0 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a 2a 20  y page-cache.** 
3dfe0 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c 65  memory controlle
3dff0 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  d by [SQLITE_CON
3e000 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 20 69  FIG_PAGECACHE] i
3e010 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69  s not included i
3e020 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 6d 65  n.** this parame
3e030 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 6e 74  ter.  The amount
3e040 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
3e050 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c 6c 6f   sum of the allo
3e060 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20  cation.** sizes 
3e070 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 20 74  as reported by t
3e080 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f 64 20  he xSize method 
3e090 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  in [sqlite3_mem_
3e0a0 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a  methods].</dd>.*
3e0b0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3e0c0 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49  STATUS_MALLOC_SI
3e0d0 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ZE</dt>.** <dd>T
3e0e0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
3e0f0 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73  cords the larges
3e100 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  t memory allocat
3e110 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68  ion request.** h
3e120 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 74 65  anded to [sqlite
3e130 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b  3_malloc()] or [
3e140 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
3e150 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20  )] (or their.** 
3e160 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 61 6c  internal equival
3e170 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65  ents).  Only the
3e180 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
3e190 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68  in the.** *pHigh
3e1a0 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20  water parameter 
3e1b0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74  to [sqlite3_stat
3e1c0 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65  us()] is of inte
3e1d0 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76  rest.  .** The v
3e1e0 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74  alue written int
3e1f0 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20  o the *pCurrent 
3e200 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64  parameter is und
3e210 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a  efined.</dd>.**.
3e220 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
3e230 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
3e240 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  SED</dt>.** <dd>
3e250 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
3e260 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
3e270 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 64 20  r of pages used 
3e280 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70  out of the.** [p
3e290 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20  agecache memory 
3e2a0 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74 20  allocator] that 
3e2b0 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 75  was configured u
3e2c0 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 45  sing .** [SQLITE
3e2d0 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
3e2e0 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c 75  E].  The.** valu
3e2f0 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e  e returned is in
3e300 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e 20 62   pages, not in b
3e310 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ytes.</dd>.**.**
3e320 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54   <dt>SQLITE_STAT
3e330 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45  US_PAGECACHE_OVE
3e340 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  RFLOW</dt>.** <d
3e350 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
3e360 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
3e370 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
3e380 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 61 6c  page cache.** al
3e390 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63  location which c
3e3a0 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74  ould not be stat
3e3b0 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b 53  isfied by the [S
3e3c0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
3e3d0 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 66 65  ECACHE].** buffe
3e3e0 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63  r and where forc
3e3f0 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74  ed to overflow t
3e400 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  o [sqlite3_mallo
3e410 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 65  c()].  The.** re
3e420 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e 63  turned value inc
3e430 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 6f 6e  ludes allocation
3e440 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 65  s that overflowe
3e450 64 20 62 65 63 61 75 73 65 20 74 68 65 79 0a 2a  d because they.*
3e460 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 72 67  * where too larg
3e470 65 20 28 74 68 65 79 20 77 65 72 65 20 6c 61 72  e (they were lar
3e480 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 73 7a  ger than the "sz
3e490 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a  " parameter to.*
3e4a0 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  * [SQLITE_CONFIG
3e4b0 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 6e 64  _PAGECACHE]) and
3e4c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61   allocations tha
3e4d0 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63  t overflowed bec
3e4e0 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 65  ause.** no space
3e4f0 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68 65   was left in the
3e500 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f 64 64   page cache.</dd
3e510 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
3e520 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41  TE_STATUS_PAGECA
3e530 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a  CHE_SIZE</dt>.**
3e540 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
3e550 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20  ter records the 
3e560 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61  largest memory a
3e570 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73  llocation reques
3e580 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b  t.** handed to [
3e590 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79  pagecache memory
3e5a0 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e   allocator].  On
3e5b0 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  ly the value ret
3e5c0 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20  urned in the.** 
3e5d0 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 61  *pHighwater para
3e5e0 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
3e5f0 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f  3_status()] is o
3e600 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a  f interest.  .**
3e610 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 74   The value writt
3e620 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75  en into the *pCu
3e630 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20  rrent parameter 
3e640 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64  is undefined.</d
3e650 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
3e660 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
3e670 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20  CH_USED</dt>.** 
3e680 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
3e690 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  er returns the n
3e6a0 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74  umber of allocat
3e6b0 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 6f 66  ions used out of
3e6c0 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 63 68   the.** [scratch
3e6d0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
3e6e0 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73  r] configured us
3e6f0 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43  ing.** [SQLITE_C
3e700 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2e 20  ONFIG_SCRATCH]. 
3e710 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
3e720 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f 63 61  ned is in alloca
3e730 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e  tions, not.** in
3e740 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 20 61   bytes.  Since a
3e750 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 6d   single thread m
3e760 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f 6e 65  ay only have one
3e770 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74   scratch allocat
3e780 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69  ion.** outstandi
3e790 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 69 73  ng at time, this
3e7a0 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 6f 20   parameter also 
3e7b0 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 6d 62  reports the numb
3e7c0 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a 2a 2a  er of threads.**
3e7d0 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 20 6d   using scratch m
3e7e0 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 61 6d  emory at the sam
3e7f0 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a  e time.</dd>.**.
3e800 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
3e810 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45  ATUS_SCRATCH_OVE
3e820 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  RFLOW</dt>.** <d
3e830 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
3e840 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
3e850 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
3e860 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a  scratch memory.*
3e870 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69  * allocation whi
3e880 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ch could not be 
3e890 73 74 61 74 69 73 66 69 65 64 20 62 79 20 74 68  statisfied by th
3e8a0 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  e [SQLITE_CONFIG
3e8b0 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75 66  _SCRATCH].** buf
3e8c0 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f  fer and where fo
3e8d0 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77  rced to overflow
3e8e0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c   to [sqlite3_mal
3e8f0 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61 6c  loc()].  The val
3e900 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  ues.** returned 
3e910 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f 77  include overflow
3e920 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72 65  s because the re
3e930 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69  quested allocati
3e940 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61  on was too.** la
3e950 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20 62  rger (that is, 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 6c 61 72 67 65 72 20 74 68 61 6e 20  was larger than 
3e990 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72 61  the.** "sz" para
3e9a0 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54 45  meter to [SQLITE
3e9b0 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d  _CONFIG_SCRATCH]
3e9c0 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e 6f  ) and because no
3e9d0 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 0a   scratch buffer.
3e9e0 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61 76  ** slots were av
3e9f0 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64  ailable..** </dd
3ea00 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
3ea10 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
3ea20 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c  H_SIZE</dt>.** <
3ea30 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
3ea40 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61  r records the la
3ea50 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c  rgest memory all
3ea60 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a  ocation request.
3ea70 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 63  ** handed to [sc
3ea80 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c  ratch memory all
3ea90 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74  ocator].  Only t
3eaa0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
3eab0 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69  d in the.** *pHi
3eac0 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65  ghwater paramete
3ead0 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  r to [sqlite3_st
3eae0 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e  atus()] is of in
3eaf0 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65  terest.  .** The
3eb00 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69   value written i
3eb10 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e  nto the *pCurren
3eb20 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75  t parameter is u
3eb30 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a  ndefined.</dd>.*
3eb40 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3eb50 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54  STATUS_PARSER_ST
3eb60 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  ACK</dt>.** <dd>
3eb70 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
3eb80 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 70 65  ecords the deepe
3eb90 73 74 20 70 61 72 73 65 72 20 73 74 61 63 6b 2e  st parser stack.
3eba0 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20    It is only.** 
3ebb0 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 53 51  meaningful if SQ
3ebc0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
3ebd0 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b 4d 41   with [YYTRACKMA
3ebe0 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f 64  XSTACKDEPTH].</d
3ebf0 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a  d>.** </dl>.**.*
3ec00 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 61 72  * New status par
3ec10 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 20 61  ameters may be a
3ec20 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 20 74  dded from time t
3ec30 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69  o time..*/.#defi
3ec40 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53  ne SQLITE_STATUS
3ec50 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 20 20  _MEMORY_USED    
3ec60 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
3ec70 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
3ec80 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 20 20  GECACHE_USED    
3ec90 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c     1.#define SQL
3eca0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
3ecb0 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 20 20  ACHE_OVERFLOW   
3ecc0 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
3ecd0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
3ece0 55 53 45 44 20 20 20 20 20 20 20 20 20 33 0a 23  USED         3.#
3ecf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
3ed00 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45  ATUS_SCRATCH_OVE
3ed10 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 65 66  RFLOW     4.#def
3ed20 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
3ed30 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 20  S_MALLOC_SIZE   
3ed40 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65         5.#define
3ed50 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50   SQLITE_STATUS_P
3ed60 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 20 20  ARSER_STACK     
3ed70 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51      6.#define SQ
3ed80 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
3ed90 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 20 20  CACHE_SIZE      
3eda0 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   7.#define SQLIT
3edb0 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
3edc0 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 38 0a  _SIZE         8.
3edd0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3ede0 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63   Database Connec
3edf0 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 31 37  tion Status {H17
3ee00 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a  500} <S60200>.**
3ee10 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
3ee20 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
3ee30 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ce is used to re
3ee40 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20 73  trieve runtime s
3ee50 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tatus informatio
3ee60 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73 69  n .** about a si
3ee70 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20 63  ngle [database c
3ee80 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65  onnection].  The
3ee90 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
3eea0 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  is the.** databa
3eeb0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62  se connection ob
3eec0 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 65 72  ject to be inter
3eed0 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 73 65  rogated.  The se
3eee0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  cond argument.**
3eef0 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 65   is the paramete
3ef00 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 74 65  r to interrogate
3ef10 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68  .  Currently, th
3ef20 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 76  e only allowed v
3ef30 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20  alue.** for the 
3ef40 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
3ef50 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 53 54   is [SQLITE_DBST
3ef60 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55  ATUS_LOOKASIDE_U
3ef70 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 6f  SED]..** Additio
3ef80 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 6c 6c  nal options will
3ef90 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 20 69   likely appear i
3efa0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
3efb0 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s of SQLite..**.
3efc0 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76  ** The current v
3efd0 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71 75  alue of the requ
3efe0 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 72 20  ested parameter 
3eff0 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  is written into 
3f000 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 68 65  *pCur.** and the
3f010 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 6e 74   highest instant
3f020 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 73 20  aneous value is 
3f030 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48  written into *pH
3f040 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 68 65  iwtr.  If.** the
3f050 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72 75   resetFlg is tru
3f060 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67 68  e, then the high
3f070 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75  est instantaneou
3f080 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 72 65  s value is.** re
3f090 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 74 6f  set back down to
3f0a0 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   the current val
3f0b0 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ue..**.** See al
3f0c0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61  so: [sqlite3_sta
3f0d0 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  tus()] and [sqli
3f0e0 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28  te3_stmt_status(
3f0f0 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
3f100 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  I SQLITE_EXPERIM
3f110 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65  ENTAL int sqlite
3f120 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c 69  3_db_status(sqli
3f130 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  te3*, int op, in
3f140 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48  t *pCur, int *pH
3f150 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74 46  iwtr, int resetF
3f160 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lg);../*.** CAPI
3f170 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72  3REF: Status Par
3f180 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 74 61  ameters for data
3f190 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
3f1a0 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 35 30   {H17520} <H1750
3f1b0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
3f1c0 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  AL.**.** These c
3f1d0 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65  onstants are the
3f1e0 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67   available integ
3f1f0 65 72 20 22 76 65 72 62 73 22 20 74 68 61 74 20  er "verbs" that 
3f200 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 73  can be passed as
3f210 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 61  .** the second a
3f220 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b  rgument to the [
3f230 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75  sqlite3_db_statu
3f240 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a  s()] interface..
3f250 2a 2a 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 20  **.** New verbs 
3f260 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20  may be added in 
3f270 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20  future releases 
3f280 6f 66 20 53 51 4c 69 74 65 2e 20 45 78 69 73 74  of SQLite. Exist
3f290 69 6e 67 20 76 65 72 62 73 0a 2a 2a 20 6d 69 67  ing verbs.** mig
3f2a0 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e 75  ht be discontinu
3f2b0 65 64 2e 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  ed. Applications
3f2c0 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68   should check th
3f2d0 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72  e return code fr
3f2e0 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 64  om.** [sqlite3_d
3f2f0 62 5f 73 74 61 74 75 73 28 29 5d 20 74 6f 20 6d  b_status()] to m
3f300 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
3f310 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 0a 2a  e call worked..*
3f320 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 64  * The [sqlite3_d
3f330 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65  b_status()] inte
3f340 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72  rface will retur
3f350 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 65 72 72  n a non-zero err
3f360 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 66 20 61 20  or code.** if a 
3f370 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20  discontinued or 
3f380 75 6e 73 75 70 70 6f 72 74 65 64 20 76 65 72 62  unsupported verb
3f390 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a   is invoked..**.
3f3a0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53  ** <dl>.** <dt>S
3f3b0 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c  QLITE_DBSTATUS_L
3f3c0 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f 64  OOKASIDE_USED</d
3f3d0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70  t>.** <dd>This p
3f3e0 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73  arameter returns
3f3f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c   the number of l
3f400 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20  ookaside memory 
3f410 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 0a  slots currently.
3f420 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c  ** checked out.<
3f430 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f  /dd>.** </dl>.*/
3f440 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3f450 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49  DBSTATUS_LOOKASI
3f460 44 45 5f 55 53 45 44 20 20 20 20 20 30 0a 0a 0a  DE_USED     0...
3f470 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3f480 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65  Prepared Stateme
3f490 6e 74 20 53 74 61 74 75 73 20 7b 48 31 37 35 35  nt Status {H1755
3f4a0 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45  0} <S60200>.** E
3f4b0 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
3f4c0 2a 20 45 61 63 68 20 70 72 65 70 61 72 65 64 20  * Each prepared 
3f4d0 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61  statement mainta
3f4e0 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b  ins various.** [
3f4f0 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55  SQLITE_STMTSTATU
3f500 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72  S_SORT | counter
3f510 73 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65 20  s] that measure 
3f520 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66  the number.** of
3f530 20 74 69 6d 65 73 20 69 74 20 68 61 73 20 70 65   times it has pe
3f540 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63  rformed specific
3f550 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 68   operations.  Th
3f560 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e  ese counters can
3f570 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d  .** be used to m
3f580 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f  onitor the perfo
3f590 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65 72  rmance character
3f5a0 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 70 72  istics of the pr
3f5b0 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d  epared.** statem
3f5c0 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70  ents.  For examp
3f5d0 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65  le, if the numbe
3f5e0 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73  r of table steps
3f5f0 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 64 73   greatly exceeds
3f600 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
3f610 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 65 73  f table searches
3f620 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c   or result rows,
3f630 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64   that would tend
3f640 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20   to indicate.** 
3f650 74 68 61 74 20 74 68 65 20 70 72 65 70 61 72 65  that the prepare
3f660 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75  d statement is u
3f670 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c  sing a full tabl
3f680 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 74 68  e scan rather th
3f690 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20  an.** an index. 
3f6a0 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74   .**.** This int
3f6b0 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74  erface is used t
3f6c0 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64 20 72  o retrieve and r
3f6d0 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76 61 6c  eset counter val
3f6e0 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70  ues from.** a [p
3f6f0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3f700 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 61  t].  The first a
3f710 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70  rgument is the p
3f720 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3f730 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62  t.** object to b
3f740 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20  e interrogated. 
3f750 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
3f760 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e  ment.** is an in
3f770 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61  teger code for a
3f780 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54   specific [SQLIT
3f790 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52  E_STMTSTATUS_SOR
3f7a0 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20  T | counter].** 
3f7b0 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74  to be interrogat
3f7c0 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 72  ed. .** The curr
3f7d0 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ent value of the
3f7e0 20 72 65 71 75 65 73 74 65 64 20 63 6f 75 6e 74   requested count
3f7f0 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  er is returned..
3f800 2a 2a 20 49 66 20 74 68 65 20 72 65 73 65 74 46  ** If the resetF
3f810 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  lg is true, then
3f820 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20   the counter is 
3f830 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66  reset to zero af
3f840 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65  ter this.** inte
3f850 72 66 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72  rface call retur
3f860 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ns..**.** See al
3f870 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61  so: [sqlite3_sta
3f880 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  tus()] and [sqli
3f890 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d  te3_db_status()]
3f8a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3f8b0 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
3f8c0 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  TAL int sqlite3_
3f8d0 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69  stmt_status(sqli
3f8e0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f  te3_stmt*, int o
3f8f0 70 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b  p,int resetFlg);
3f900 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3f910 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74  : Status Paramet
3f920 65 72 73 20 66 6f 72 20 70 72 65 70 61 72 65 64  ers for prepared
3f930 20 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 37   statements {H17
3f940 35 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a  570} <H17550>.**
3f950 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
3f960 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f  .** These prepro
3f970 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65  cessor macros de
3f980 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64  fine integer cod
3f990 65 73 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75  es that name cou
3f9a0 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61  nter.** values a
3f9b0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
3f9c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74  he [sqlite3_stmt
3f9d0 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72  _status()] inter
3f9e0 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61  face..** The mea
3f9f0 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72  nings of the var
3fa00 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72  ious counters ar
3fa10 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  e as follows:.**
3fa20 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e  .** <dl>.** <dt>
3fa30 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55  SQLITE_STMTSTATU
3fa40 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c  S_FULLSCAN_STEP<
3fa50 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
3fa60 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
3fa70 66 20 74 69 6d 65 73 20 74 68 61 74 20 53 51 4c  f times that SQL
3fa80 69 74 65 20 68 61 73 20 73 74 65 70 70 65 64 20  ite has stepped 
3fa90 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20  forward in.** a 
3faa0 74 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f 66  table as part of
3fab0 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63   a full table sc
3fac0 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 65  an.  Large numbe
3fad0 72 73 20 66 6f 72 20 74 68 69 73 20 63 6f 75 6e  rs for this coun
3fae0 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63  ter.** may indic
3faf0 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 65  ate opportunitie
3fb00 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63  s for performanc
3fb10 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68  e improvement th
3fb20 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 75  rough .** carefu
3fb30 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73  l use of indices
3fb40 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
3fb50 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54  >SQLITE_STMTSTAT
3fb60 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20  US_SORT</dt>.** 
3fb70 3c 64 64 3e 54 68 69 73 20 69 73 20 74 68 65 20  <dd>This is the 
3fb80 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f  number of sort o
3fb90 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68  perations that h
3fba0 61 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a  ave occurred..**
3fbb0 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75   A non-zero valu
3fbc0 65 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65  e in this counte
3fbd0 72 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61  r may indicate a
3fbe0 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f  n opportunity to
3fbf0 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20  .** improvement 
3fc00 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f  performance thro
3fc10 75 67 68 20 63 61 72 65 66 75 6c 20 75 73 65 20  ugh careful use 
3fc20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e  of indices.</dd>
3fc30 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a  .**.** </dl>.*/.
3fc40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
3fc50 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43  TMTSTATUS_FULLSC
3fc60 41 4e 5f 53 54 45 50 20 20 20 20 20 31 0a 23 64  AN_STEP     1.#d
3fc70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d  efine SQLITE_STM
3fc80 54 53 54 41 54 55 53 5f 53 4f 52 54 20 20 20 20  TSTATUS_SORT    
3fc90 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a            2../*.
3fca0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 75 73  ** CAPI3REF: Cus
3fcb0 74 6f 6d 20 50 61 67 65 20 43 61 63 68 65 20 4f  tom Page Cache O
3fcc0 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d  bject.** EXPERIM
3fcd0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ENTAL.**.** The 
3fce0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 74  sqlite3_pcache t
3fcf0 79 70 65 20 69 73 20 6f 70 61 71 75 65 2e 20 20  ype is opaque.  
3fd00 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  It is implemente
3fd10 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67  d by.** the plug
3fd20 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54  gable module.  T
3fd30 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 68  he SQLite core h
3fd40 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20  as no knowledge 
3fd50 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f  of.** its size o
3fd60 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63  r internal struc
3fd70 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20 64  ture and never d
3fd80 65 61 6c 73 20 77 69 74 68 20 74 68 65 0a 2a 2a  eals with the.**
3fd90 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20   sqlite3_pcache 
3fda0 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20 62 79  object except by
3fdb0 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61 73   holding and pas
3fdc0 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a  sing pointers.**
3fdd0 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a   to the object..
3fde0 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74  **.** See [sqlit
3fdf0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
3fe00 73 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  s] for additiona
3fe10 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
3fe20 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
3fe30 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20   sqlite3_pcache 
3fe40 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 3b 0a  sqlite3_pcache;.
3fe50 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3fe60 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 66   Application Def
3fe70 69 6e 65 64 20 50 61 67 65 20 43 61 63 68 65 2e  ined Page Cache.
3fe80 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70  .** KEYWORDS: {p
3fe90 61 67 65 20 63 61 63 68 65 7d 0a 2a 2a 20 45 58  age cache}.** EX
3fea0 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
3feb0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f   The [sqlite3_co
3fec0 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f  nfig]([SQLITE_CO
3fed0 4e 46 49 47 5f 50 43 41 43 48 45 5d 2c 20 2e 2e  NFIG_PCACHE], ..
3fee0 2e 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e  .) interface can
3fef0 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61 6e 20  .** register an 
3ff00 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67 65  alternative page
3ff10 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
3ff20 61 74 69 6f 6e 20 62 79 20 70 61 73 73 69 6e 67  ation by passing
3ff30 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 6e 73 74 61   in an .** insta
3ff40 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74  nce of the sqlit
3ff50 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
3ff60 73 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65  s structure. The
3ff70 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 74 68 65   majority of the
3ff80 20 0a 2a 2a 20 68 65 61 70 20 6d 65 6d 6f 72 79   .** heap memory
3ff90 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
3ffa0 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70  is used by the p
3ffb0 61 67 65 20 63 61 63 68 65 20 74 6f 20 63 61 63  age cache to cac
3ffc0 68 65 20 64 61 74 61 20 72 65 61 64 20 0a 2a 2a  he data read .**
3ffd0 20 66 72 6f 6d 2c 20 6f 72 20 72 65 61 64 79 20   from, or ready 
3ffe0 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f  to be written to
3fff0 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  , the database f
40000 69 6c 65 2e 20 42 79 20 69 6d 70 6c 65 6d 65 6e  ile. By implemen
40010 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 73 74 6f  ting a .** custo
40020 6d 20 70 61 67 65 20 63 61 63 68 65 20 75 73 69  m page cache usi
40030 6e 67 20 74 68 69 73 20 41 50 49 2c 20 61 6e 20  ng this API, an 
40040 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20  application can 
40050 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 20 0a 2a 2a  control more .**
40060 20 70 72 65 63 69 73 65 6c 79 20 74 68 65 20 61   precisely the a
40070 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
40080 63 6f 6e 73 75 6d 65 64 20 62 79 20 53 51 4c 69  consumed by SQLi
40090 74 65 2c 20 74 68 65 20 77 61 79 20 69 6e 20 77  te, the way in w
400a0 68 69 63 68 20 0a 2a 2a 20 74 68 61 74 20 6d 65  hich .** that me
400b0 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65  mory is allocate
400c0 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 2c 20  d and released, 
400d0 61 6e 64 20 74 68 65 20 70 6f 6c 69 63 69 65 73  and the policies
400e0 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20 64 65 74   used to .** det
400f0 65 72 6d 69 6e 65 20 65 78 61 63 74 6c 79 20 77  ermine exactly w
40100 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 61 20  hich parts of a 
40110 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 72  database file ar
40120 65 20 63 61 63 68 65 64 20 61 6e 64 20 66 6f 72  e cached and for
40130 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e 67 2e 0a 2a   .** how long..*
40140 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74  *.** The content
40150 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  s of the sqlite3
40160 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20  _pcache_methods 
40170 73 74 72 75 63 74 75 72 65 20 61 72 65 20 63 6f  structure are co
40180 70 69 65 64 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e  pied to an.** in
40190 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62 79  ternal buffer by
401a0 20 53 51 4c 69 74 65 20 77 69 74 68 69 6e 20 74   SQLite within t
401b0 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  he call to [sqli
401c0 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 20 48 65  te3_config].  He
401d0 6e 63 65 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69  nce.** the appli
401e0 63 61 74 69 6f 6e 20 6d 61 79 20 64 69 73 63 61  cation may disca
401f0 72 64 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  rd the parameter
40200 20 61 66 74 65 72 20 74 68 65 20 63 61 6c 6c 20   after the call 
40210 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  to.** [sqlite3_c
40220 6f 6e 66 69 67 28 29 5d 20 72 65 74 75 72 6e 73  onfig()] returns
40230 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69  ..**.** The xIni
40240 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61  t() method is ca
40250 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  lled once for ea
40260 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ch call to [sqli
40270 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
40280 5d 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20 6f 6e  ].** (usually on
40290 6c 79 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 74  ly once during t
402a0 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74  he lifetime of t
402b0 68 65 20 70 72 6f 63 65 73 73 29 2e 20 49 74 20  he process). It 
402c0 69 73 20 70 61 73 73 65 64 0a 2a 2a 20 61 20 63  is passed.** a c
402d0 6f 70 79 20 6f 66 20 74 68 65 20 73 71 6c 69 74  opy of the sqlit
402e0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
402f0 73 2e 70 41 72 67 20 76 61 6c 75 65 2e 20 49 74  s.pArg value. It
40300 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
40310 73 65 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 6c  set.** up global
40320 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64 20   structures and 
40330 6d 75 74 65 78 65 73 20 72 65 71 75 69 72 65 64  mutexes required
40340 20 62 79 20 74 68 65 20 63 75 73 74 6f 6d 20 70   by the custom p
40350 61 67 65 20 63 61 63 68 65 20 0a 2a 2a 20 69 6d  age cache .** im
40360 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a  plementation. .*
40370 2a 0a 2a 2a 20 54 68 65 20 78 53 68 75 74 64 6f  *.** The xShutdo
40380 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63  wn() method is c
40390 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69  alled from withi
403a0 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64  n [sqlite3_shutd
403b0 6f 77 6e 28 29 5d 2c 20 0a 2a 2a 20 69 66 20 74  own()], .** if t
403c0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69  he application i
403d0 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50 49 2e  nvokes this API.
403e0 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 20   It can be used 
403f0 74 6f 20 63 6c 65 61 6e 20 75 70 20 0a 2a 2a 20  to clean up .** 
40400 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  any outstanding 
40410 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72 65  resources before
40420 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f 77   process shutdow
40430 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a  n, if required..
40440 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f 6c  **.** SQLite hol
40450 64 73 20 61 20 5b 53 51 4c 49 54 45 5f 4d 55 54  ds a [SQLITE_MUT
40460 45 58 5f 52 45 43 55 52 53 49 56 45 5d 20 6d 75  EX_RECURSIVE] mu
40470 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e 76 6f  tex when it invo
40480 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 6e 69 74  kes.** the xInit
40490 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68 65 20   method, so the 
404a0 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e 65 65  xInit method nee
404b0 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61 64 73  d not be threads
404c0 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 78 53 68  afe.  The.** xSh
404d0 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69 73  utdown method is
404e0 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f   only called fro
404f0 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64  m [sqlite3_shutd
40500 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 64 6f 65  own()] so it doe
40510 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f  s.** not need to
40520 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20 65   be threadsafe e
40530 69 74 68 65 72 2e 20 20 41 6c 6c 20 6f 74 68 65  ither.  All othe
40540 72 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 62  r methods must b
40550 65 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a 20  e threadsafe.** 
40560 69 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64  in multithreaded
40570 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 0a 2a   applications..*
40580 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c  *.** SQLite will
40590 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78 49   never invoke xI
405a0 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 61 6e 20  nit() more than 
405b0 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 61 6e 20  once without an 
405c0 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a 2a 20 63  intervening.** c
405d0 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 6f 77 6e  all to xShutdown
405e0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43  ()..**.** The xC
405f0 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64 20 69  reate() method i
40600 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 74 72  s used to constr
40610 75 63 74 20 61 20 6e 65 77 20 63 61 63 68 65 20  uct a new cache 
40620 69 6e 73 74 61 6e 63 65 2e 20 20 53 51 4c 69 74  instance.  SQLit
40630 65 0a 2a 2a 20 77 69 6c 6c 20 74 79 70 69 63 61  e.** will typica
40640 6c 6c 79 20 63 72 65 61 74 65 20 6f 6e 65 20 63  lly create one c
40650 61 63 68 65 20 69 6e 73 74 61 6e 63 65 20 66 6f  ache instance fo
40660 72 20 65 61 63 68 20 6f 70 65 6e 20 64 61 74 61  r each open data
40670 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 74 68  base file,.** th
40680 6f 75 67 68 20 74 68 69 73 20 69 73 20 6e 6f 74  ough this is not
40690 20 67 75 61 72 61 6e 74 65 65 64 2e 20 54 68 65   guaranteed. The
406a0 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d 65  .** first parame
406b0 74 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73 20  ter, szPage, is 
406c0 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65  the size in byte
406d0 73 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 74  s of the pages t
406e0 68 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61  hat must.** be a
406f0 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20  llocated by the 
40700 63 61 63 68 65 2e 20 20 73 7a 50 61 67 65 20 77  cache.  szPage w
40710 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 70 6f 77  ill not be a pow
40720 65 72 20 6f 66 20 74 77 6f 2e 20 20 73 7a 50 61  er of two.  szPa
40730 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 65 20 70  ge.** will the p
40740 61 67 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  age size of the 
40750 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 68  database file th
40760 61 74 20 69 73 20 74 6f 20 62 65 20 63 61 63 68  at is to be cach
40770 65 64 20 70 6c 75 73 20 61 6e 0a 2a 2a 20 69 6e  ed plus an.** in
40780 63 72 65 6d 65 6e 74 20 28 68 65 72 65 20 63 61  crement (here ca
40790 6c 6c 65 64 20 22 52 22 29 20 6f 66 20 61 62 6f  lled "R") of abo
407a0 75 74 20 31 30 30 20 6f 72 20 32 30 30 2e 20 20  ut 100 or 200.  
407b0 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20  SQLite will use 
407c0 74 68 65 0a 2a 2a 20 65 78 74 72 61 20 52 20 62  the.** extra R b
407d0 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67  ytes on each pag
407e0 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 74 61 64  e to store metad
407f0 61 74 61 20 61 62 6f 75 74 20 74 68 65 20 75 6e  ata about the un
40800 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64 61 74 61  derlying.** data
40810 62 61 73 65 20 70 61 67 65 20 6f 6e 20 64 69 73  base page on dis
40820 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66  k.  The value of
40830 20 52 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f 6e   R depends.** on
40840 20 74 68 65 20 53 51 4c 69 74 65 20 76 65 72 73   the SQLite vers
40850 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 65 74 20  ion, the target 
40860 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 20 68 6f  platform, and ho
40870 77 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f 6d  w SQLite was com
40880 70 69 6c 65 64 2e 0a 2a 2a 20 52 20 69 73 20 63  piled..** R is c
40890 6f 6e 73 74 61 6e 74 20 66 6f 72 20 61 20 70 61  onstant for a pa
408a0 72 74 69 63 75 6c 61 72 20 62 75 69 6c 64 20 6f  rticular build o
408b0 66 20 53 51 4c 69 74 65 2e 20 20 54 68 65 20 73  f SQLite.  The s
408c0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
408d0 6f 0a 2a 2a 20 78 43 72 65 61 74 65 28 29 2c 20  o.** xCreate(), 
408e0 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20 74  bPurgeable, is t
408f0 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68 65  rue if the cache
40900 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 77   being created w
40910 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 20 74  ill.** be used t
40920 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73 65  o cache database
40930 20 70 61 67 65 73 20 6f 66 20 61 20 66 69 6c 65   pages of a file
40940 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 2c   stored on disk,
40950 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20   or.** false if 
40960 69 74 20 69 73 20 75 73 65 64 20 66 6f 72 20 61  it is used for a
40970 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
40980 62 61 73 65 2e 20 54 68 65 20 63 61 63 68 65 20  base. The cache 
40990 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
409a0 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  * does not have 
409b0 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 73  to do anything s
409c0 70 65 63 69 61 6c 20 62 61 73 65 64 20 77 69 74  pecial based wit
409d0 68 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 62  h the value of b
409e0 50 75 72 67 65 61 62 6c 65 3b 0a 2a 2a 20 69 74  Purgeable;.** it
409f0 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 69 73   is purely advis
40a00 6f 72 79 2e 20 20 4f 6e 20 61 20 63 61 63 68 65  ory.  On a cache
40a10 20 77 68 65 72 65 20 62 50 75 72 67 65 61 62 6c   where bPurgeabl
40a20 65 20 69 73 20 66 61 6c 73 65 2c 20 53 51 4c 69  e is false, SQLi
40a30 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72  te will.** never
40a40 20 69 6e 76 6f 6b 65 20 78 55 6e 70 69 6e 28 29   invoke xUnpin()
40a50 20 65 78 63 65 70 74 20 74 6f 20 64 65 6c 69 62   except to delib
40a60 65 72 61 74 65 6c 79 20 64 65 6c 65 74 65 20 61  erately delete a
40a70 20 70 61 67 65 2e 0a 2a 2a 20 49 6e 20 6f 74 68   page..** In oth
40a80 65 72 20 77 6f 72 64 73 2c 20 61 20 63 61 63 68  er words, a cach
40a90 65 20 63 72 65 61 74 65 64 20 77 69 74 68 20 62  e created with b
40aa0 50 75 72 67 65 61 62 6c 65 20 73 65 74 20 74 6f  Purgeable set to
40ab0 20 66 61 6c 73 65 20 77 69 6c 6c 0a 2a 2a 20 6e   false will.** n
40ac0 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 61 6e 79  ever contain any
40ad0 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 2e   unpinned pages.
40ae0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 61 63 68  .**.** The xCach
40af0 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 6d  esize() method m
40b00 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20  ay be called at 
40b10 61 6e 79 20 74 69 6d 65 20 62 79 20 53 51 4c 69  any time by SQLi
40b20 74 65 20 74 6f 20 73 65 74 20 74 68 65 0a 2a 2a  te to set the.**
40b30 20 73 75 67 67 65 73 74 65 64 20 6d 61 78 69 6d   suggested maxim
40b40 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 20 28 6e  um cache-size (n
40b50 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73  umber of pages s
40b60 74 6f 72 65 64 20 62 79 29 20 74 68 65 20 63 61  tored by) the ca
40b70 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20  che.** instance 
40b80 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
40b90 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68  rst argument. Th
40ba0 69 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  is is the value 
40bb0 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67  configured using
40bc0 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 22  .** the SQLite "
40bd0 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 69  [PRAGMA cache_si
40be0 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 20 41 73  ze]" command. As
40bf0 20 77 69 74 68 20 74 68 65 20 62 50 75 72 67 65   with the bPurge
40c00 61 62 6c 65 20 70 61 72 61 6d 65 74 65 72 2c 0a  able parameter,.
40c10 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ** the implement
40c20 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65 71  ation is not req
40c30 75 69 72 65 64 20 74 6f 20 64 6f 20 61 6e 79 74  uired to do anyt
40c40 68 69 6e 67 20 77 69 74 68 20 74 68 69 73 0a 2a  hing with this.*
40c50 2a 20 76 61 6c 75 65 3b 20 69 74 20 69 73 20 61  * value; it is a
40c60 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a  dvisory only..**
40c70 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 63 6f 75  .** The xPagecou
40c80 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 68 6f 75  nt() method shou
40c90 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  ld return the nu
40ca0 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 75  mber of pages cu
40cb0 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65  rrently.** store
40cc0 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a  d in the cache..
40cd0 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46 65 74 63  ** .** The xFetc
40ce0 68 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 73  h() method is us
40cf0 65 64 20 74 6f 20 66 65 74 63 68 20 61 20 70 61  ed to fetch a pa
40d00 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  ge and return a 
40d10 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 0a  pointer to it. .
40d20 2a 2a 20 41 20 27 70 61 67 65 27 2c 20 69 6e 20  ** A 'page', in 
40d30 74 68 69 73 20 63 6f 6e 74 65 78 74 2c 20 69 73  this context, is
40d40 20 61 20 62 75 66 66 65 72 20 6f 66 20 73 7a 50   a buffer of szP
40d50 61 67 65 20 62 79 74 65 73 20 61 6c 69 67 6e 65  age bytes aligne
40d60 64 20 61 74 20 61 6e 0a 2a 2a 20 38 2d 62 79 74  d at an.** 8-byt
40d70 65 20 62 6f 75 6e 64 61 72 79 2e 20 54 68 65 20  e boundary. The 
40d80 70 61 67 65 20 74 6f 20 62 65 20 66 65 74 63 68  page to be fetch
40d90 65 64 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  ed is determined
40da0 20 62 79 20 74 68 65 20 6b 65 79 2e 20 54 68 65   by the key. The
40db0 0a 2a 2a 20 6d 69 6d 69 6d 75 6d 20 6b 65 79 20  .** mimimum key 
40dc0 76 61 6c 75 65 20 69 73 20 31 2e 20 41 66 74 65  value is 1. Afte
40dd0 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 72 65  r it has been re
40de0 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 78 46  trieved using xF
40df0 65 74 63 68 2c 20 74 68 65 20 70 61 67 65 20 0a  etch, the page .
40e00 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ** is considered
40e10 20 74 6f 20 62 65 20 22 70 69 6e 6e 65 64 22 2e   to be "pinned".
40e20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  .**.** If the re
40e30 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20  quested page is 
40e40 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 70  already in the p
40e50 61 67 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20  age cache, then 
40e60 74 68 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a  the page cache.*
40e70 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
40e80 20 6d 75 73 74 20 72 65 74 75 72 6e 20 61 20 70   must return a p
40e90 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61  ointer to the pa
40ea0 67 65 20 62 75 66 66 65 72 20 77 69 74 68 20 69  ge buffer with i
40eb0 74 73 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 6e  ts content.** in
40ec0 74 61 63 74 2e 20 20 49 66 20 74 68 65 20 72 65  tact.  If the re
40ed0 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20  quested page is 
40ee0 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74  not already in t
40ef0 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74  he cache, then t
40f00 68 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 20 6f  he.** behavior o
40f10 66 20 74 68 65 20 63 61 63 68 65 20 69 6d 70 6c  f the cache impl
40f20 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 64 65  ementation is de
40f30 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20  termined by the 
40f40 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20  value of the.** 
40f50 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 61 6d  createFlag param
40f60 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 78  eter passed to x
40f70 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e 67  Fetch, according
40f80 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   to the followin
40f90 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c  g table:.**.** <
40fa0 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20 77  table border=1 w
40fb0 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d 63  idth=85% align=c
40fc0 65 6e 74 65 72 3e 0a 2a 2a 20 3c 74 72 3e 3c 74  enter>.** <tr><t
40fd0 68 3e 20 63 72 65 61 74 65 46 6c 61 67 20 3c 74  h> createFlag <t
40fe0 68 3e 20 42 65 68 61 76 69 6f 75 72 20 77 68 65  h> Behaviour whe
40ff0 6e 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c  n page is not al
41000 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 0a 2a  ready in cache.*
41010 2a 20 3c 74 72 3e 3c 74 64 3e 20 30 20 3c 74 64  * <tr><td> 0 <td
41020 3e 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74  > Do not allocat
41030 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20 52  e a new page.  R
41040 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 3c  eturn NULL..** <
41050 74 72 3e 3c 74 64 3e 20 31 20 3c 74 64 3e 20 41  tr><td> 1 <td> A
41060 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61  llocate a new pa
41070 67 65 20 69 66 20 69 74 20 65 61 73 79 20 61 6e  ge if it easy an
41080 64 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20  d convenient to 
41090 64 6f 20 73 6f 2e 0a 2a 2a 20 20 20 20 20 20 20  do so..**       
410a0 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77            Otherw
410b0 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  ise return NULL.
410c0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 32 20 3c  .** <tr><td> 2 <
410d0 74 64 3e 20 4d 61 6b 65 20 65 76 65 72 79 20 65  td> Make every e
410e0 66 66 6f 72 74 20 74 6f 20 61 6c 6c 6f 63 61 74  ffort to allocat
410f0 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20 4f  e a new page.  O
41100 6e 6c 79 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20  nly return.**   
41110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55                NU
41120 4c 4c 20 69 66 20 61 6c 6c 6f 63 61 74 69 6e 67  LL if allocating
41130 20 61 20 6e 65 77 20 70 61 67 65 20 69 73 20 65   a new page is e
41140 66 66 65 63 74 69 76 65 6c 79 20 69 6d 70 6f 73  ffectively impos
41150 73 69 62 6c 65 2e 0a 2a 2a 20 3c 2f 74 61 62 6c  sible..** </tabl
41160 65 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  e>.**.** SQLite 
41170 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20 69 6e  will normally in
41180 76 6f 6b 65 20 78 46 65 74 63 68 28 29 20 77 69  voke xFetch() wi
41190 74 68 20 61 20 63 72 65 61 74 65 46 6c 61 67 20  th a createFlag 
411a0 6f 66 20 30 20 6f 72 20 31 2e 20 20 49 66 0a 2a  of 0 or 1.  If.*
411b0 2a 20 61 20 63 61 6c 6c 20 74 6f 20 78 46 65 74  * a call to xFet
411c0 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 65  ch() with create
411d0 46 6c 61 67 3d 3d 31 20 72 65 74 75 72 6e 73 20  Flag==1 returns 
411e0 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c 69 74  NULL, then SQLit
411f0 65 20 77 69 6c 6c 0a 2a 2a 20 61 74 74 65 6d 70  e will.** attemp
41200 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65 20 6f  t to unpin one o
41210 72 20 6d 6f 72 65 20 63 61 63 68 65 20 70 61 67  r more cache pag
41220 65 73 20 62 79 20 73 70 69 6c 6c 69 6e 67 20 74  es by spilling t
41230 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a  he content of.**
41240 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 6f   pinned pages to
41250 20 64 69 73 6b 20 61 6e 64 20 73 79 6e 63 68 69   disk and synchi
41260 6e 67 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  ng the operating
41270 20 73 79 73 74 65 6d 20 64 69 73 6b 20 63 61 63   system disk cac
41280 68 65 2e 20 41 66 74 65 72 0a 2a 2a 20 61 74 74  he. After.** att
41290 65 6d 70 74 69 6e 67 20 74 6f 20 75 6e 70 69 6e  empting to unpin
412a0 20 70 61 67 65 73 2c 20 74 68 65 20 78 46 65 74   pages, the xFet
412b0 63 68 28 29 20 6d 65 74 68 6f 64 20 77 69 6c 6c  ch() method will
412c0 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 67 61 69   be invoked agai
412d0 6e 20 77 69 74 68 0a 2a 2a 20 61 20 63 72 65 61  n with.** a crea
412e0 74 65 46 6c 61 67 20 6f 66 20 32 2e 0a 2a 2a 0a  teFlag of 2..**.
412f0 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 20 63  ** xUnpin() is c
41300 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20  alled by SQLite 
41310 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74  with a pointer t
41320 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 70 69  o a currently pi
41330 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 73 20  nned page.** as 
41340 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  its second argum
41350 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 69 72  ent. If the thir
41360 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 69 73  d parameter, dis
41370 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72  card, is non-zer
41380 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70  o,.** then the p
41390 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 65 76  age should be ev
413a0 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 63  icted from the c
413b0 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63 61  ache. In this ca
413c0 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 61 73  se SQLite .** as
413d0 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 6e  sumes that the n
413e0 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 61 67  ext time the pag
413f0 65 20 69 73 20 72 65 74 72 69 65 76 65 64 20 66  e is retrieved f
41400 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75 73  rom the cache us
41410 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 74 63  ing.** the xFetc
41420 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 20 77  h() method, it w
41430 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 20 49  ill be zeroed. I
41440 66 20 74 68 65 20 64 69 73 63 61 72 64 20 70 61  f the discard pa
41450 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 7a 65  rameter is.** ze
41460 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67  ro, then the pag
41470 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  e is considered 
41480 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20  to be unpinned. 
41490 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d  The cache implem
414a0 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 20  entation.** may 
414b0 63 68 6f 6f 73 65 20 74 6f 20 65 76 69 63 74 20  choose to evict 
414c0 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 61  unpinned pages a
414d0 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a 2a  t any time..**.*
414e0 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 20 6e  * The cache is n
414f0 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 70  ot required to p
41500 65 72 66 6f 72 6d 20 61 6e 79 20 72 65 66 65 72  erform any refer
41510 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 2e 20 41  ence counting. A
41520 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63 61 6c 6c   single .** call
41530 20 74 6f 20 78 55 6e 70 69 6e 28 29 20 75 6e 70   to xUnpin() unp
41540 69 6e 73 20 74 68 65 20 70 61 67 65 20 72 65 67  ins the page reg
41550 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6e  ardless of the n
41560 75 6d 62 65 72 20 6f 66 20 70 72 69 6f 72 20 63  umber of prior c
41570 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78 46 65 74  alls .** to xFet
41580 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ch()..**.** The 
41590 78 52 65 6b 65 79 28 29 20 6d 65 74 68 6f 64 20  xRekey() method 
415a0 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e 67  is used to chang
415b0 65 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20  e the key value 
415c0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
415d0 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 61 73 73  the.** page pass
415e0 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
415f0 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20 6f   argument from o
41600 6c 64 4b 65 79 20 74 6f 20 6e 65 77 4b 65 79 2e  ldKey to newKey.
41610 20 49 66 20 74 68 65 20 63 61 63 68 65 0a 2a 2a   If the cache.**
41620 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74   previously cont
41630 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 20 61 73  ains an entry as
41640 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65  sociated with ne
41650 77 4b 65 79 2c 20 69 74 20 73 68 6f 75 6c 64 20  wKey, it should 
41660 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 65 64 2e  be.** discarded.
41670 20 41 6e 79 20 70 72 69 6f 72 20 63 61 63 68 65   Any prior cache
41680 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61 74 65   entry associate
41690 64 20 77 69 74 68 20 6e 65 77 4b 65 79 20 69 73  d with newKey is
416a0 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 0a   guaranteed not.
416b0 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e 65 64 2e  ** to be pinned.
416c0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69  .**.** When SQLi
416d0 74 65 20 63 61 6c 6c 73 20 74 68 65 20 78 54 72  te calls the xTr
416e0 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64 2c  uncate() method,
416f0 20 74 68 65 20 63 61 63 68 65 20 6d 75 73 74 20   the cache must 
41700 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a 2a 20 65  discard all.** e
41710 78 69 73 74 69 6e 67 20 63 61 63 68 65 20 65 6e  xisting cache en
41720 74 72 69 65 73 20 77 69 74 68 20 70 61 67 65 20  tries with page 
41730 6e 75 6d 62 65 72 73 20 28 6b 65 79 73 29 20 67  numbers (keys) g
41740 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
41750 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68 65 20 76  qual.** to the v
41760 61 6c 75 65 20 6f 66 20 74 68 65 20 69 4c 69 6d  alue of the iLim
41770 69 74 20 70 61 72 61 6d 65 74 65 72 20 70 61 73  it parameter pas
41780 73 65 64 20 74 6f 20 78 54 72 75 6e 63 61 74 65  sed to xTruncate
41790 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a 20 6f 66  (). If any.** of
417a0 20 74 68 65 73 65 20 70 61 67 65 73 20 61 72 65   these pages are
417b0 20 70 69 6e 6e 65 64 2c 20 74 68 65 79 20 61 72   pinned, they ar
417c0 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e 70  e implicitly unp
417d0 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74  inned, meaning t
417e0 68 61 74 0a 2a 2a 20 74 68 65 79 20 63 61 6e 20  hat.** they can 
417f0 62 65 20 73 61 66 65 6c 79 20 64 69 73 63 61 72  be safely discar
41800 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ded..**.** The x
41810 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f 64  Destroy() method
41820 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 6c 65   is used to dele
41830 74 65 20 61 20 63 61 63 68 65 20 61 6c 6c 6f 63  te a cache alloc
41840 61 74 65 64 20 62 79 20 78 43 72 65 61 74 65 28  ated by xCreate(
41850 29 2e 0a 2a 2a 20 41 6c 6c 20 72 65 73 6f 75 72  )..** All resour
41860 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
41870 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 65  ith the specifie
41880 64 20 63 61 63 68 65 20 73 68 6f 75 6c 64 20 62  d cache should b
41890 65 20 66 72 65 65 64 2e 20 41 66 74 65 72 0a 2a  e freed. After.*
418a0 2a 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 44  * calling the xD
418b0 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f 64 2c  estroy() method,
418c0 20 53 51 4c 69 74 65 20 63 6f 6e 73 69 64 65 72   SQLite consider
418d0 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70  s the [sqlite3_p
418e0 63 61 63 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64 6c  cache*].** handl
418f0 65 20 69 6e 76 61 6c 69 64 2c 20 61 6e 64 20 77  e invalid, and w
41900 69 6c 6c 20 6e 6f 74 20 75 73 65 20 69 74 20 77  ill not use it w
41910 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 73 71  ith any other sq
41920 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
41930 68 6f 64 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  hods.** function
41940 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
41950 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61  ruct sqlite3_pca
41960 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69  che_methods sqli
41970 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
41980 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74  ds;.struct sqlit
41990 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
419a0 73 20 7b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  s {.  void *pArg
419b0 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29  ;.  int (*xInit)
419c0 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20  (void*);.  void 
419d0 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 6f 69  (*xShutdown)(voi
419e0 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 70  d*);.  sqlite3_p
419f0 63 61 63 68 65 20 2a 28 2a 78 43 72 65 61 74 65  cache *(*xCreate
41a00 29 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e  )(int szPage, in
41a10 74 20 62 50 75 72 67 65 61 62 6c 65 29 3b 0a 20  t bPurgeable);. 
41a20 20 76 6f 69 64 20 28 2a 78 43 61 63 68 65 73 69   void (*xCachesi
41a30 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63  ze)(sqlite3_pcac
41a40 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 68 65 73  he*, int nCaches
41a50 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 50  ize);.  int (*xP
41a60 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69 74 65  agecount)(sqlite
41a70 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 20 76 6f  3_pcache*);.  vo
41a80 69 64 20 2a 28 2a 78 46 65 74 63 68 29 28 73 71  id *(*xFetch)(sq
41a90 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 75  lite3_pcache*, u
41aa0 6e 73 69 67 6e 65 64 20 6b 65 79 2c 20 69 6e 74  nsigned key, int
41ab0 20 63 72 65 61 74 65 46 6c 61 67 29 3b 0a 20 20   createFlag);.  
41ac0 76 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29 28 73  void (*xUnpin)(s
41ad0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20  qlite3_pcache*, 
41ae0 76 6f 69 64 2a 2c 20 69 6e 74 20 64 69 73 63 61  void*, int disca
41af0 72 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 52  rd);.  void (*xR
41b00 65 6b 65 79 29 28 73 71 6c 69 74 65 33 5f 70 63  ekey)(sqlite3_pc
41b10 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e  ache*, void*, un
41b20 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 2c 20 75  signed oldKey, u
41b30 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 79 29 3b  nsigned newKey);
41b40 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 75 6e 63  .  void (*xTrunc
41b50 61 74 65 29 28 73 71 6c 69 74 65 33 5f 70 63 61  ate)(sqlite3_pca
41b60 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69  che*, unsigned i
41b70 4c 69 6d 69 74 29 3b 0a 20 20 76 6f 69 64 20 28  Limit);.  void (
41b80 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74  *xDestroy)(sqlit
41b90 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 7d 3b 0a  e3_pcache*);.};.
41ba0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
41bb0 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 4f   Online Backup O
41bc0 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d  bject.** EXPERIM
41bd0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ENTAL.**.** The 
41be0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f  sqlite3_backup o
41bf0 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20 73 74  bject records st
41c00 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ate information 
41c10 61 62 6f 75 74 20 61 6e 20 6f 6e 67 6f 69 6e 67  about an ongoing
41c20 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 62 61 63 6b 75  .** online backu
41c30 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54 68  p operation.  Th
41c40 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  e sqlite3_backup
41c50 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74   object is creat
41c60 65 64 20 62 79 0a 2a 2a 20 61 20 63 61 6c 6c 20  ed by.** a call 
41c70 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b  to [sqlite3_back
41c80 75 70 5f 69 6e 69 74 28 29 5d 20 61 6e 64 20 69  up_init()] and i
41c90 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 61  s destroyed by a
41ca0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c   call to.** [sql
41cb0 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
41cc0 73 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  sh()]..**.** See
41cd0 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68   Also: [Using th
41ce0 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20  e SQLite Online 
41cf0 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a 74  Backup API].*/.t
41d00 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
41d10 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73 71 6c  lite3_backup sql
41d20 69 74 65 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f 2a  ite3_backup;../*
41d30 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e  .** CAPI3REF: On
41d40 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 2e  line Backup API.
41d50 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
41d60 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20  .**.** This API 
41d70 69 73 20 75 73 65 64 20 74 6f 20 6f 76 65 72 77  is used to overw
41d80 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  rite the content
41d90 73 20 6f 66 20 6f 6e 65 20 64 61 74 61 62 61 73  s of one databas
41da0 65 20 77 69 74 68 20 74 68 61 74 0a 2a 2a 20 6f  e with that.** o
41db0 66 20 61 6e 6f 74 68 65 72 2e 20 49 74 20 69 73  f another. It is
41dc0 20 75 73 65 66 75 6c 20 65 69 74 68 65 72 20 66   useful either f
41dd0 6f 72 20 63 72 65 61 74 69 6e 67 20 62 61 63 6b  or creating back
41de0 75 70 73 20 6f 66 20 64 61 74 61 62 61 73 65 73  ups of databases
41df0 20 6f 72 0a 2a 2a 20 66 6f 72 20 63 6f 70 79 69   or.** for copyi
41e00 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  ng in-memory dat
41e10 61 62 61 73 65 73 20 74 6f 20 6f 72 20 66 72 6f  abases to or fro
41e20 6d 20 70 65 72 73 69 73 74 65 6e 74 20 66 69 6c  m persistent fil
41e30 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41  es. .**.** See A
41e40 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20  lso: [Using the 
41e50 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61  SQLite Online Ba
41e60 63 6b 75 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a 20  ckup API].**.** 
41e70 45 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73  Exclusive access
41e80 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20   is required to 
41e90 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
41ea0 64 61 74 61 62 61 73 65 20 66 6f 72 20 74 68 65  database for the
41eb0 20 0a 2a 2a 20 64 75 72 61 74 69 6f 6e 20 6f 66   .** duration of
41ec0 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20   the operation. 
41ed0 48 6f 77 65 76 65 72 20 74 68 65 20 73 6f 75 72  However the sour
41ee0 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f  ce database is o
41ef0 6e 6c 79 0a 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b  nly.** read-lock
41f00 65 64 20 77 68 69 6c 65 20 69 74 20 69 73 20 61  ed while it is a
41f10 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 20 72 65  ctually being re
41f20 61 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f  ad, it is not lo
41f30 63 6b 65 64 0a 2a 2a 20 63 6f 6e 74 69 6e 75 6f  cked.** continuo
41f40 75 73 6c 79 20 66 6f 72 20 74 68 65 20 65 6e 74  usly for the ent
41f50 69 72 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54  ire operation. T
41f60 68 75 73 2c 20 74 68 65 20 62 61 63 6b 75 70 20  hus, the backup 
41f70 6d 61 79 20 62 65 0a 2a 2a 20 70 65 72 66 6f 72  may be.** perfor
41f80 6d 65 64 20 6f 6e 20 61 20 6c 69 76 65 20 64 61  med on a live da
41f90 74 61 62 61 73 65 20 77 69 74 68 6f 75 74 20 70  tabase without p
41fa0 72 65 76 65 6e 74 69 6e 67 20 6f 74 68 65 72 20  reventing other 
41fb0 75 73 65 72 73 20 66 72 6f 6d 0a 2a 2a 20 77 72  users from.** wr
41fc0 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74  iting to the dat
41fd0 61 62 61 73 65 20 66 6f 72 20 61 6e 20 65 78 74  abase for an ext
41fe0 65 6e 64 65 64 20 70 65 72 69 6f 64 20 6f 66 20  ended period of 
41ff0 74 69 6d 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 6f 20  time..** .** To 
42000 70 65 72 66 6f 72 6d 20 61 20 62 61 63 6b 75 70  perform a backup
42010 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 2a 2a 20   operation: .** 
42020 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 20 20 20 3c 6c    <ol>.**     <l
42030 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63  i><b>sqlite3_bac
42040 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 20 69  kup_init()</b> i
42050 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 74 6f  s called once to
42060 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 0a   initialize the.
42070 2a 2a 20 20 20 20 20 20 20 20 20 62 61 63 6b 75  **         backu
42080 70 2c 20 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c  p, .**     <li><
42090 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  b>sqlite3_backup
420a0 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69 73 20 63  _step()</b> is c
420b0 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72  alled one or mor
420c0 65 20 74 69 6d 65 73 20 74 6f 20 74 72 61 6e 73  e times to trans
420d0 66 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  fer .**         
420e0 74 68 65 20 64 61 74 61 20 62 65 74 77 65 65 6e  the data between
420f0 20 74 68 65 20 74 77 6f 20 64 61 74 61 62 61 73   the two databas
42100 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 0a  es, and finally.
42110 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71  **     <li><b>sq
42120 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
42130 69 73 68 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c  ish()</b> is cal
42140 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 61  led to release a
42150 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 0a 2a 2a  ll resources .**
42160 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 61           associa
42170 74 65 64 20 77 69 74 68 20 74 68 65 20 62 61 63  ted with the bac
42180 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 0a  kup operation. .
42190 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20 54 68  **   </ol>.** Th
421a0 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 65 78  ere should be ex
421b0 61 63 74 6c 79 20 6f 6e 65 20 63 61 6c 6c 20 74  actly one call t
421c0 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  o sqlite3_backup
421d0 5f 66 69 6e 69 73 68 28 29 20 66 6f 72 20 65 61  _finish() for ea
421e0 63 68 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c  ch.** successful
421f0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
42200 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a  _backup_init()..
42210 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33  **.** <b>sqlite3
42220 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f  _backup_init()</
42230 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  b>.**.** The fir
42240 73 74 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  st two arguments
42250 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69   passed to [sqli
42260 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
42270 29 5d 20 61 72 65 20 74 68 65 20 64 61 74 61 62  )] are the datab
42280 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73  ase.** handle as
42290 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
422a0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
422b0 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 20 64  tabase and the d
422c0 61 74 61 62 61 73 65 20 6e 61 6d 65 20 0a 2a 2a  atabase name .**
422d0 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 68 20   used to attach 
422e0 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
422f0 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 20  database to the 
42300 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 61 74 61  handle. The data
42310 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20  base name.** is 
42320 22 6d 61 69 6e 22 20 66 6f 72 20 74 68 65 20 6d  "main" for the m
42330 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 22 74  ain database, "t
42340 65 6d 70 22 20 66 6f 72 20 74 68 65 20 74 65 6d  emp" for the tem
42350 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2c  porary database,
42360 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20   or.** the name 
42370 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72  specified as par
42380 74 20 6f 66 20 74 68 65 20 5b 41 54 54 41 43 48  t of the [ATTACH
42390 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 74  ] statement if t
423a0 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69  he destination i
423b0 73 0a 2a 2a 20 61 6e 20 61 74 74 61 63 68 65 64  s.** an attached
423c0 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 74   database. The t
423d0 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20  hird and fourth 
423e0 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64  arguments passed
423f0 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   to .** sqlite3_
42400 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 69 64  backup_init() id
42410 65 6e 74 69 66 79 20 74 68 65 20 5b 64 61 74 61  entify the [data
42420 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
42430 0a 2a 2a 20 61 6e 64 20 64 61 74 61 62 61 73 65  .** and database
42440 20 6e 61 6d 65 20 75 73 65 64 0a 2a 2a 20 74 6f   name used.** to
42450 20 61 63 63 65 73 73 20 74 68 65 20 73 6f 75 72   access the sour
42460 63 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  ce database. The
42470 20 76 61 6c 75 65 73 20 70 61 73 73 65 64 20 66   values passed f
42480 6f 72 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e  or the source an
42490 64 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f  d .** destinatio
424a0 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  n [database conn
424b0 65 63 74 69 6f 6e 5d 20 70 61 72 61 6d 65 74 65  ection] paramete
424c0 72 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 74  rs must not be t
424d0 68 65 20 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49  he same..**.** I
424e0 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
424f0 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33  s within sqlite3
42500 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20  _backup_init(), 
42510 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72 65 74  then NULL is ret
42520 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20  urned.** and an 
42530 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65  error code and e
42540 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 69  rror message wri
42550 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 5b 64  tten into the [d
42560 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
42570 6f 6e 5d 20 0a 2a 2a 20 70 61 73 73 65 64 20 61  on] .** passed a
42580 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
42590 6d 65 6e 74 2e 20 54 68 65 79 20 6d 61 79 20 62  ment. They may b
425a0 65 20 72 65 74 72 69 65 76 65 64 20 75 73 69 6e  e retrieved usin
425b0 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65  g the.** [sqlite
425c0 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73  3_errcode()], [s
425d0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d  qlite3_errmsg()]
425e0 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65  , and [sqlite3_e
425f0 72 72 6d 73 67 31 36 28 29 5d 20 66 75 6e 63 74  rrmsg16()] funct
42600 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 68 65 72 77 69  ions..** Otherwi
42610 73 65 2c 20 69 66 20 73 75 63 63 65 73 73 66 75  se, if successfu
42620 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  l, a pointer to 
42630 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b  an [sqlite3_back
42640 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a  up] object is.**
42650 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20   returned. This 
42660 70 6f 69 6e 74 65 72 20 6d 61 79 20 62 65 20 75  pointer may be u
42670 73 65 64 20 77 69 74 68 20 74 68 65 20 73 71 6c  sed with the sql
42680 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
42690 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65  () and.** sqlite
426a0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
426b0 29 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 70  ) functions to p
426c0 65 72 66 6f 72 6d 20 74 68 65 20 73 70 65 63 69  erform the speci
426d0 66 69 65 64 20 62 61 63 6b 75 70 20 0a 2a 2a 20  fied backup .** 
426e0 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  operation..**.**
426f0 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b   <b>sqlite3_back
42700 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 0a 2a 2a  up_step()</b>.**
42710 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 5b 73 71  .** Function [sq
42720 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
42730 70 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20  p()] is used to 
42740 63 6f 70 79 20 75 70 20 74 6f 20 6e 50 61 67 65  copy up to nPage
42750 20 70 61 67 65 73 20 62 65 74 77 65 65 6e 20 0a   pages between .
42760 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e  ** the source an
42770 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  d destination da
42780 74 61 62 61 73 65 73 2c 20 77 68 65 72 65 20 6e  tabases, where n
42790 50 61 67 65 20 69 73 20 74 68 65 20 76 61 6c 75  Page is the valu
427a0 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 73 65 63  e of the .** sec
427b0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 70 61  ond parameter pa
427c0 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f  ssed to sqlite3_
427d0 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 49  backup_step(). I
427e0 66 20 6e 50 61 67 65 20 69 73 20 61 20 6e 65 67  f nPage is a neg
427f0 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75 65 2c 20  ative.** value, 
42800 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 73 6f  all remaining so
42810 75 72 63 65 20 70 61 67 65 73 20 61 72 65 20 63  urce pages are c
42820 6f 70 69 65 64 2e 20 49 66 20 74 68 65 20 72 65  opied. If the re
42830 71 75 69 72 65 64 20 70 61 67 65 73 20 61 72 65  quired pages are
42840 20 0a 2a 2a 20 73 75 63 63 65 73 66 75 6c 6c 79   .** succesfully
42850 20 63 6f 70 69 65 64 2c 20 62 75 74 20 74 68 65   copied, but the
42860 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 6f 72  re are still mor
42870 65 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20  e pages to copy 
42880 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 62  before the .** b
42890 61 63 6b 75 70 20 69 73 20 63 6f 6d 70 6c 65 74  ackup is complet
428a0 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 5b 53  e, it returns [S
428b0 51 4c 49 54 45 5f 4f 4b 5d 2e 20 49 66 20 6e 6f  QLITE_OK]. If no
428c0 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 61   error occured a
428d0 6e 64 20 74 68 65 72 65 20 0a 2a 2a 20 61 72 65  nd there .** are
428e0 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74   no more pages t
428f0 6f 20 63 6f 70 79 2c 20 74 68 65 6e 20 5b 53 51  o copy, then [SQ
42900 4c 49 54 45 5f 44 4f 4e 45 5d 20 69 73 20 72 65  LITE_DONE] is re
42910 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72  turned. If an er
42920 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20  ror .** occurs, 
42930 74 68 65 6e 20 61 6e 20 53 51 4c 69 74 65 20 65  then an SQLite e
42940 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
42950 75 72 6e 65 64 2e 20 41 73 20 77 65 6c 6c 20 61  urned. As well a
42960 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 61 6e  s [SQLITE_OK] an
42970 64 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e  d.** [SQLITE_DON
42980 45 5d 2c 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  E], a call to sq
42990 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
429a0 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 5b  p() may return [
429b0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d  SQLITE_READONLY]
429c0 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d  ,.** [SQLITE_NOM
429d0 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53  EM], [SQLITE_BUS
429e0 59 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b  Y], [SQLITE_LOCK
429f0 45 44 5d 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53  ED], or an.** [S
42a00 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45  QLITE_IOERR_ACCE
42a10 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52  SS | SQLITE_IOER
42a20 52 5f 58 58 58 5d 20 65 78 74 65 6e 64 65 64 20  R_XXX] extended 
42a30 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  error code..**.*
42a40 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 74 68 65  * As well as the
42a50 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20   case where the 
42a60 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
42a70 62 61 73 65 20 66 69 6c 65 20 77 61 73 20 6f 70  base file was op
42a80 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 72 65 61 64  ened for.** read
42a90 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2c 20 73 71  -only access, sq
42aa0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
42ab0 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 5b  p() may return [
42ac0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d  SQLITE_READONLY]
42ad0 20 69 66 0a 2a 2a 20 74 68 65 20 64 65 73 74 69   if.** the desti
42ae0 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 2d  nation is an in-
42af0 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20  memory database 
42b00 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74  with a different
42b10 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 66 72   page size.** fr
42b20 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  om the source da
42b30 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  tabase..**.** If
42b40 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
42b50 73 74 65 70 28 29 20 63 61 6e 6e 6f 74 20 6f 62  step() cannot ob
42b60 74 61 69 6e 20 61 20 72 65 71 75 69 72 65 64 20  tain a required 
42b70 66 69 6c 65 2d 73 79 73 74 65 6d 20 6c 6f 63 6b  file-system lock
42b80 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 73  , then.** the [s
42b90 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
42ba0 6c 65 72 20 7c 20 62 75 73 79 2d 68 61 6e 64 6c  ler | busy-handl
42bb0 65 72 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a 20  er function].** 
42bc0 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 66 20 6f  is invoked (if o
42bd0 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64 29  ne is specified)
42be0 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 62 75 73  . If the .** bus
42bf0 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e  y-handler return
42c00 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f 72  s non-zero befor
42c10 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 76  e the lock is av
42c20 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 0a 2a  ailable, then .*
42c30 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  * [SQLITE_BUSY] 
42c40 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
42c50 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 20 74 68  he caller. In th
42c60 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c  is case the call
42c70 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62   to.** sqlite3_b
42c80 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e  ackup_step() can
42c90 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 65   be retried late
42ca0 72 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65  r. If the source
42cb0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f  .** [database co
42cc0 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 20  nnection].** is 
42cd0 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 77 72  being used to wr
42ce0 69 74 65 20 74 6f 20 74 68 65 20 73 6f 75 72 63  ite to the sourc
42cf0 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 20  e database when 
42d00 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
42d10 74 65 70 28 29 0a 2a 2a 20 69 73 20 63 61 6c 6c  tep().** is call
42d20 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45  ed, then [SQLITE
42d30 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 75  _LOCKED] is retu
42d40 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  rned immediately
42d50 2e 20 41 67 61 69 6e 2c 20 69 6e 20 74 68 69 73  . Again, in this
42d60 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 63 61 6c  .** case the cal
42d70 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  l to sqlite3_bac
42d80 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e 20 62  kup_step() can b
42d90 65 20 72 65 74 72 69 65 64 20 6c 61 74 65 72 20  e retried later 
42da0 6f 6e 2e 20 49 66 0a 2a 2a 20 5b 53 51 4c 49 54  on. If.** [SQLIT
42db0 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 7c  E_IOERR_ACCESS |
42dc0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58   SQLITE_IOERR_XX
42dd0 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45  X], [SQLITE_NOME
42de0 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54  M], or.** [SQLIT
42df0 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 73 20 72  E_READONLY] is r
42e00 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 0a 2a  eturned, then .*
42e10 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f  * there is no po
42e20 69 6e 74 20 69 6e 20 72 65 74 72 79 69 6e 67 20  int in retrying 
42e30 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  the call to sqli
42e40 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
42e50 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 65 72 72  ). These .** err
42e60 6f 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72  ors are consider
42e70 65 64 20 66 61 74 61 6c 2e 20 41 74 20 74 68 69  ed fatal. At thi
42e80 73 20 70 6f 69 6e 74 20 74 68 65 20 61 70 70 6c  s point the appl
42e90 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 61 63 63  ication must acc
42ea0 65 70 74 20 0a 2a 2a 20 74 68 61 74 20 74 68 65  ept .** that the
42eb0 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
42ec0 6e 20 68 61 73 20 66 61 69 6c 65 64 20 61 6e 64  n has failed and
42ed0 20 70 61 73 73 20 74 68 65 20 62 61 63 6b 75 70   pass the backup
42ee0 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 6e 64 6c   operation handl
42ef0 65 20 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71 6c  e .** to the sql
42f00 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
42f10 73 68 28 29 20 74 6f 20 72 65 6c 65 61 73 65 20  sh() to release 
42f20 61 73 73 6f 63 69 61 74 65 64 20 72 65 73 6f 75  associated resou
42f30 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 6c 6c  rces..**.** Foll
42f40 6f 77 69 6e 67 20 74 68 65 20 66 69 72 73 74 20  owing the first 
42f50 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
42f60 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 61  backup_step(), a
42f70 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
42f80 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20   is.** obtained 
42f90 6f 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  on the destinati
42fa0 6f 6e 20 66 69 6c 65 2e 20 49 74 20 69 73 20 6e  on file. It is n
42fb0 6f 74 20 72 65 6c 65 61 73 65 64 20 75 6e 74 69  ot released unti
42fc0 6c 20 65 69 74 68 65 72 20 0a 2a 2a 20 73 71 6c  l either .** sql
42fd0 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
42fe0 73 68 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f  sh() is called o
42ff0 72 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65  r the backup ope
43000 72 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65  ration is comple
43010 74 65 20 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74  te .** and sqlit
43020 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
43030 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
43040 5f 44 4f 4e 45 5d 2e 20 41 64 64 69 74 69 6f 6e  _DONE]. Addition
43050 61 6c 6c 79 2c 20 65 61 63 68 20 74 69 6d 65 20  ally, each time 
43060 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  .** a call to sq
43070 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
43080 70 28 29 20 69 73 20 6d 61 64 65 20 61 20 5b 73  p() is made a [s
43090 68 61 72 65 64 20 6c 6f 63 6b 5d 20 69 73 20 6f  hared lock] is o
430a0 62 74 61 69 6e 65 64 20 6f 6e 0a 2a 2a 20 74 68  btained on.** th
430b0 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
430c0 65 20 66 69 6c 65 2e 20 54 68 69 73 20 6c 6f 63  e file. This loc
430d0 6b 20 69 73 20 72 65 6c 65 61 73 65 64 20 62 65  k is released be
430e0 66 6f 72 65 20 74 68 65 0a 2a 2a 20 73 71 6c 69  fore the.** sqli
430f0 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
43100 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 20  ) call returns. 
43110 42 65 63 61 75 73 65 20 74 68 65 20 73 6f 75 72  Because the sour
43120 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6e  ce database is n
43130 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 64 20 62 65 74  ot.** locked bet
43140 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f 20 73 71  ween calls to sq
43150 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
43160 70 28 29 2c 20 69 74 20 6d 61 79 20 62 65 20 6d  p(), it may be m
43170 6f 64 69 66 69 65 64 20 6d 69 64 2d 77 61 79 0a  odified mid-way.
43180 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 62  ** through the b
43190 61 63 6b 75 70 20 70 72 6f 63 65 64 75 72 65 2e  ackup procedure.
431a0 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 64   If the source d
431b0 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66  atabase is modif
431c0 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20 65 78 74  ied by an.** ext
431d0 65 72 6e 61 6c 20 70 72 6f 63 65 73 73 20 6f 72  ernal process or
431e0 20 76 69 61 20 61 20 64 61 74 61 62 61 73 65 20   via a database 
431f0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74 68 65 72  connection other
43200 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 62 65   than the one be
43210 69 6e 67 0a 2a 2a 20 75 73 65 64 20 62 79 20 74  ing.** used by t
43220 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74  he backup operat
43230 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 62 61  ion, then the ba
43240 63 6b 75 70 20 77 69 6c 6c 20 62 65 20 74 72 61  ckup will be tra
43250 6e 73 70 61 72 65 6e 74 6c 79 0a 2a 2a 20 72 65  nsparently.** re
43260 73 74 61 72 74 65 64 20 62 79 20 74 68 65 20 6e  started by the n
43270 65 78 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ext call to sqli
43280 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
43290 29 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65  ). If the source
432a0 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73   .** database is
432b0 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74 68 65   modified by the
432c0 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20   using the same 
432d0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
432e0 69 6f 6e 20 61 73 20 69 73 20 75 73 65 64 0a 2a  ion as is used.*
432f0 2a 20 62 79 20 74 68 65 20 62 61 63 6b 75 70 20  * by the backup 
43300 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20  operation, then 
43310 74 68 65 20 62 61 63 6b 75 70 20 64 61 74 61 62  the backup datab
43320 61 73 65 20 69 73 20 74 72 61 6e 73 70 61 72 65  ase is transpare
43330 6e 74 6c 79 20 0a 2a 2a 20 75 70 64 61 74 65 64  ntly .** updated
43340 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
43350 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69  e..**.** <b>sqli
43360 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73  te3_backup_finis
43370 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 4f 6e  h()</b>.**.** On
43380 63 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ce sqlite3_backu
43390 70 5f 73 74 65 70 28 29 20 68 61 73 20 72 65 74  p_step() has ret
433a0 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 44 4f  urned [SQLITE_DO
433b0 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 74 68 65  NE], or when the
433c0 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e   .** application
433d0 20 77 69 73 68 65 73 20 74 6f 20 61 62 61 6e 64   wishes to aband
433e0 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70  on the backup op
433f0 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 5b 73 71  eration, the [sq
43400 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 0a 2a 2a  lite3_backup].**
43410 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64 20 62   object should b
43420 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69  e passed to sqli
43430 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73  te3_backup_finis
43440 68 28 29 2e 20 54 68 69 73 20 72 65 6c 65 61 73  h(). This releas
43450 65 73 20 61 6c 6c 0a 2a 2a 20 72 65 73 6f 75 72  es all.** resour
43460 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
43470 69 74 68 20 74 68 65 20 62 61 63 6b 75 70 20 6f  ith the backup o
43480 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 73 71 6c  peration. If sql
43490 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
434a0 28 29 0a 2a 2a 20 68 61 73 20 6e 6f 74 20 79 65  ().** has not ye
434b0 74 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49  t returned [SQLI
434c0 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65 6e 20 61  TE_DONE], then a
434d0 6e 79 20 61 63 74 69 76 65 20 77 72 69 74 65 2d  ny active write-
434e0 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74  transaction on t
434f0 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f  he.** destinatio
43500 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 72 6f  n database is ro
43510 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 65 20 5b  lled back. The [
43520 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20  sqlite3_backup] 
43530 6f 62 6a 65 63 74 20 69 73 20 69 6e 76 61 6c 69  object is invali
43540 64 0a 2a 2a 20 61 6e 64 20 6d 61 79 20 6e 6f 74  d.** and may not
43550 20 62 65 20 75 73 65 64 20 66 6f 6c 6c 6f 77 69   be used followi
43560 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ng a call to sql
43570 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
43580 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sh()..**.** The 
43590 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
435a0 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  y sqlite3_backup
435b0 5f 66 69 6e 69 73 68 20 69 73 20 5b 53 51 4c 49  _finish is [SQLI
435c0 54 45 5f 4f 4b 5d 20 69 66 20 6e 6f 20 65 72 72  TE_OK] if no err
435d0 6f 72 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2c 20  or.** occurred, 
435e0 72 65 67 61 72 64 6c 65 73 73 20 6f 72 20 77 68  regardless or wh
435f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 71 6c  ether or not sql
43600 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
43610 28 29 20 77 61 73 20 63 61 6c 6c 65 64 0a 2a 2a  () was called.**
43620 20 61 20 73 75 66 66 69 63 69 65 6e 74 20 6e 75   a sufficient nu
43630 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 6f  mber of times to
43640 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 62 61   complete the ba
43650 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20  ckup operation. 
43660 4f 72 2c 20 69 66 0a 2a 2a 20 61 6e 20 6f 75 74  Or, if.** an out
43670 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69  -of-memory condi
43680 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72 72 6f 72  tion or IO error
43690 20 6f 63 63 75 72 65 64 20 64 75 72 69 6e 67 20   occured during 
436a0 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c  a call to.** sql
436b0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
436c0 28 29 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f  () then [SQLITE_
436d0 4e 4f 4d 45 4d 5d 20 6f 72 20 61 6e 0a 2a 2a 20  NOMEM] or an.** 
436e0 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43  [SQLITE_IOERR_AC
436f0 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f  CESS | SQLITE_IO
43700 45 52 52 5f 58 58 58 5d 20 65 72 72 6f 72 20 63  ERR_XXX] error c
43710 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  ode.** is return
43720 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
43730 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
43740 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  and an error mes
43750 73 61 67 65 20 61 72 65 0a 2a 2a 20 77 72 69 74  sage are.** writ
43760 74 65 6e 20 74 6f 20 74 68 65 20 64 65 73 74 69  ten to the desti
43770 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65  nation [database
43780 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a   connection]..**
43790 0a 2a 2a 20 41 20 72 65 74 75 72 6e 20 6f 66 20  .** A return of 
437a0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72  [SQLITE_BUSY] or
437b0 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d   [SQLITE_LOCKED]
437c0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 62 61   from sqlite3_ba
437d0 63 6b 75 70 5f 73 74 65 70 28 29 20 69 73 0a 2a  ckup_step() is.*
437e0 2a 20 6e 6f 74 20 61 20 70 65 72 6d 61 6e 65 6e  * not a permanen
437f0 74 20 65 72 72 6f 72 20 61 6e 64 20 64 6f 65 73  t error and does
43800 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65 20   not affect the 
43810 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 0a  return value of.
43820 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
43830 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a  p_finish()..**.*
43840 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63  * <b>sqlite3_bac
43850 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c  kup_remaining(),
43860 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
43870 70 61 67 65 63 6f 75 6e 74 28 29 3c 2f 62 3e 0a  pagecount()</b>.
43880 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c 20  **.** Each call 
43890 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
438a0 70 5f 73 74 65 70 28 29 20 73 65 74 73 20 74 77  p_step() sets tw
438b0 6f 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20  o values stored 
438c0 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79  internally.** by
438d0 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63   an [sqlite3_bac
438e0 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 20 54 68 65  kup] object. The
438f0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
43900 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 63   still to be bac
43910 6b 65 64 0a 2a 2a 20 75 70 2c 20 77 68 69 63 68  ked.** up, which
43920 20 6d 61 79 20 62 65 20 71 75 65 72 69 65 64 20   may be queried 
43930 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  by sqlite3_backu
43940 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c 20 61  p_remaining(), a
43950 6e 64 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20  nd the total.** 
43960 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
43970 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  in the source da
43980 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69  tabase file, whi
43990 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 65  ch may be querie
439a0 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  d by.** sqlite3_
439b0 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74  backup_pagecount
439c0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61  ()..**.** The va
439d0 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62 79  lues returned by
439e0 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   these functions
439f0 20 61 72 65 20 6f 6e 6c 79 20 75 70 64 61 74 65   are only update
43a00 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  d by.** sqlite3_
43a10 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 49  backup_step(). I
43a20 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74  f the source dat
43a30 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65  abase is modifie
43a40 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b 75  d during a backu
43a50 70 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2c 20  p.** operation, 
43a60 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 73 20  then the values 
43a70 61 72 65 20 6e 6f 74 20 75 70 64 61 74 65 64 20  are not updated 
43a80 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 61  to account for a
43a90 6e 79 20 65 78 74 72 61 0a 2a 2a 20 70 61 67 65  ny extra.** page
43aa0 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62  s that need to b
43ab0 65 20 75 70 64 61 74 65 64 20 6f 72 20 74 68 65  e updated or the
43ac0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 6f 75   size of the sou
43ad0 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  rce database fil
43ae0 65 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 2e 0a 2a  e.** changing..*
43af0 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e 63 75 72 72 65  *.** <b>Concurre
43b00 6e 74 20 55 73 61 67 65 20 6f 66 20 44 61 74 61  nt Usage of Data
43b10 62 61 73 65 20 48 61 6e 64 6c 65 73 3c 2f 62 3e  base Handles</b>
43b20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 75 72 63  .**.** The sourc
43b30 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
43b40 65 63 74 69 6f 6e 5d 20 6d 61 79 20 62 65 20 75  ection] may be u
43b50 73 65 64 20 62 79 20 74 68 65 20 61 70 70 6c 69  sed by the appli
43b60 63 61 74 69 6f 6e 20 66 6f 72 20 6f 74 68 65 72  cation for other
43b70 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 77 68 69  .** purposes whi
43b80 6c 65 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72  le a backup oper
43b90 61 74 69 6f 6e 20 69 73 20 75 6e 64 65 72 77 61  ation is underwa
43ba0 79 20 6f 72 20 62 65 69 6e 67 20 69 6e 69 74 69  y or being initi
43bb0 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 53 51  alized..** If SQ
43bc0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
43bd0 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 65 64 20   and configured 
43be0 74 6f 20 73 75 70 70 6f 72 74 20 74 68 72 65 61  to support threa
43bf0 64 73 61 66 65 20 64 61 74 61 62 61 73 65 0a 2a  dsafe database.*
43c00 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 74  * connections, t
43c10 68 65 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64  hen the source d
43c20 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
43c30 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 20 63  on may be used c
43c40 6f 6e 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 66  oncurrently.** f
43c50 72 6f 6d 20 77 69 74 68 69 6e 20 6f 74 68 65 72  rom within other
43c60 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20   threads..**.** 
43c70 48 6f 77 65 76 65 72 2c 20 74 68 65 20 61 70 70  However, the app
43c80 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67 75  lication must gu
43c90 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65  arantee that the
43ca0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
43cb0 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
43cc0 69 6f 6e 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f  ion handle is no
43cd0 74 20 70 61 73 73 65 64 20 74 6f 20 61 6e 79 20  t passed to any 
43ce0 6f 74 68 65 72 20 41 50 49 20 28 62 79 20 61 6e  other API (by an
43cf0 79 20 74 68 72 65 61 64 29 20 61 66 74 65 72 20  y thread) after 
43d00 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
43d10 75 70 5f 69 6e 69 74 28 29 20 69 73 20 63 61 6c  up_init() is cal
43d20 6c 65 64 20 61 6e 64 20 62 65 66 6f 72 65 20 74  led and before t
43d30 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
43d40 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69   call to.** sqli
43d50 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73  te3_backup_finis
43d60 68 28 29 2e 20 55 6e 66 6f 72 74 75 6e 61 74 65  h(). Unfortunate
43d70 6c 79 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e  ly SQLite does n
43d80 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65  ot currently che
43d90 63 6b 0a 2a 2a 20 66 6f 72 20 74 68 69 73 2c 20  ck.** for this, 
43da0 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  if the applicati
43db0 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68 65 20  on does use the 
43dc0 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74  destination [dat
43dd0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
43de0 5d 0a 2a 2a 20 66 6f 72 20 73 6f 6d 65 20 6f 74  ].** for some ot
43df0 68 65 72 20 70 75 72 70 6f 73 65 20 64 75 72 69  her purpose duri
43e00 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72  ng a backup oper
43e10 61 74 69 6f 6e 2c 20 74 68 69 6e 67 73 20 6d 61  ation, things ma
43e20 79 20 61 70 70 65 61 72 20 74 6f 0a 2a 2a 20 77  y appear to.** w
43e30 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 62 75  ork correctly bu
43e40 74 20 69 6e 20 66 61 63 74 20 62 65 20 73 75 62  t in fact be sub
43e50 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 69  tly malfunctioni
43e60 6e 67 2e 20 20 55 73 65 20 6f 66 20 74 68 65 0a  ng.  Use of the.
43e70 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  ** destination d
43e80 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
43e90 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 6b 75  on while a backu
43ea0 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 73 73  p is in progress
43eb0 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 63   might.** also c
43ec0 61 75 73 65 20 61 20 6d 75 74 65 78 20 64 65 61  ause a mutex dea
43ed0 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 75 72  dlock..**.** Fur
43ee0 74 68 65 72 6d 6f 72 65 2c 20 69 66 20 72 75 6e  thermore, if run
43ef0 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 65 64 20  ning in [shared 
43f00 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 74 68 65  cache mode], the
43f10 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73   application mus
43f20 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74  t.** guarantee t
43f30 68 61 74 20 74 68 65 20 73 68 61 72 65 64 20 63  hat the shared c
43f40 61 63 68 65 20 75 73 65 64 20 62 79 20 74 68 65  ache used by the
43f50 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
43f60 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 20  abase.** is not 
43f70 61 63 63 65 73 73 65 64 20 77 68 69 6c 65 20 74  accessed while t
43f80 68 65 20 62 61 63 6b 75 70 20 69 73 20 72 75 6e  he backup is run
43f90 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 74 69 63  ning. In practic
43fa0 65 20 74 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20  e this means.** 
43fb0 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63 61  that the applica
43fc0 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 6e  tion must guaran
43fd0 74 65 65 20 74 68 61 74 20 74 68 65 20 66 69 6c  tee that the fil
43fe0 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 20 62 65  e-system file be
43ff0 69 6e 67 20 0a 2a 2a 20 62 61 63 6b 65 64 20 75  ing .** backed u
44000 70 20 74 6f 20 69 73 20 6e 6f 74 20 61 63 63 65  p to is not acce
44010 73 73 65 64 20 62 79 20 61 6e 79 20 63 6f 6e 6e  ssed by any conn
44020 65 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 74 68  ection within th
44030 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 6e 6f  e process,.** no
44040 74 20 6a 75 73 74 20 74 68 65 20 73 70 65 63 69  t just the speci
44050 66 69 63 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  fic connection t
44060 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20 74  hat was passed t
44070 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  o sqlite3_backup
44080 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  _init()..**.** T
44090 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b  he [sqlite3_back
440a0 75 70 5d 20 6f 62 6a 65 63 74 20 69 74 73 65 6c  up] object itsel
440b0 66 20 69 73 20 70 61 72 74 69 61 6c 6c 79 20 74  f is partially t
440c0 68 72 65 61 64 73 61 66 65 2e 20 4d 75 6c 74 69  hreadsafe. Multi
440d0 70 6c 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 20  ple .** threads 
440e0 6d 61 79 20 73 61 66 65 6c 79 20 6d 61 6b 65 20  may safely make 
440f0 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75 72 72  multiple concurr
44100 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  ent calls to sql
44110 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
44120 28 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20  ()..** However, 
44130 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
44140 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61  up_remaining() a
44150 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  nd sqlite3_backu
44160 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 0a 2a 2a  p_pagecount().**
44170 20 41 50 49 73 20 61 72 65 20 6e 6f 74 20 73 74   APIs are not st
44180 72 69 63 74 6c 79 20 73 70 65 61 6b 69 6e 67 20  rictly speaking 
44190 74 68 72 65 61 64 73 61 66 65 2e 20 49 66 20 74  threadsafe. If t
441a0 68 65 79 20 61 72 65 20 69 6e 76 6f 6b 65 64 20  hey are invoked 
441b0 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74  at the.** same t
441c0 69 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 20 74  ime as another t
441d0 68 72 65 61 64 20 69 73 20 69 6e 76 6f 6b 69 6e  hread is invokin
441e0 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  g sqlite3_backup
441f0 5f 73 74 65 70 28 29 20 69 74 20 69 73 0a 2a 2a  _step() it is.**
44200 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74   possible that t
44210 68 65 79 20 72 65 74 75 72 6e 20 69 6e 76 61 6c  hey return inval
44220 69 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51  id values..*/.SQ
44230 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
44240 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 65 33  _backup *sqlite3
44250 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20  _backup_init(.  
44260 73 71 6c 69 74 65 33 20 2a 70 44 65 73 74 2c 20  sqlite3 *pDest, 
44270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44280 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e         /* Destin
44290 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68  ation database h
442a0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
442b0 20 63 68 61 72 20 2a 7a 44 65 73 74 4e 61 6d 65   char *zDestName
442c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
442d0 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e    /* Destination
442e0 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a   database name *
442f0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 53 6f  /.  sqlite3 *pSo
44300 75 72 63 65 2c 20 20 20 20 20 20 20 20 20 20 20  urce,           
44310 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
44320 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61  urce database ha
44330 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
44340 63 68 61 72 20 2a 7a 53 6f 75 72 63 65 4e 61 6d  char *zSourceNam
44350 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
44360 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61 62   /* Source datab
44370 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 53  ase name */.);.S
44380 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
44390 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
443a0 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  p(sqlite3_backup
443b0 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 29 3b   *p, int nPage);
443c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
443d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
443e0 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f 62 61  inish(sqlite3_ba
443f0 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45  ckup *p);.SQLITE
44400 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
44410 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e  _backup_remainin
44420 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  g(sqlite3_backup
44430 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49   *p);.SQLITE_API
44440 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63   int sqlite3_bac
44450 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 73 71  kup_pagecount(sq
44460 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29  lite3_backup *p)
44470 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
44480 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69  F: Unlock Notifi
44490 63 61 74 69 6f 6e 0a 2a 2a 20 45 58 50 45 52 49  cation.** EXPERI
444a0 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 57 68 65  MENTAL.**.** Whe
444b0 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 68 61  n running in sha
444c0 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20  red-cache mode, 
444d0 61 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61  a database opera
444e0 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c 20 77 69  tion may fail wi
444f0 74 68 0a 2a 2a 20 61 6e 20 5b 53 51 4c 49 54 45  th.** an [SQLITE
44500 5f 4c 4f 43 4b 45 44 5d 20 65 72 72 6f 72 20 69  _LOCKED] error i
44510 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c  f the required l
44520 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 68 61 72  ocks on the shar
44530 65 64 2d 63 61 63 68 65 20 6f 72 0a 2a 2a 20 69  ed-cache or.** i
44540 6e 64 69 76 69 64 75 61 6c 20 74 61 62 6c 65 73  ndividual tables
44550 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72   within the shar
44560 65 64 2d 63 61 63 68 65 20 63 61 6e 6e 6f 74 20  ed-cache cannot 
44570 62 65 20 6f 62 74 61 69 6e 65 64 2e 20 53 65 65  be obtained. See
44580 0a 2a 2a 20 5b 53 51 4c 69 74 65 20 53 68 61 72  .** [SQLite Shar
44590 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 5d 20 66  ed-Cache Mode] f
445a0 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e  or a description
445b0 20 6f 66 20 73 68 61 72 65 64 2d 63 61 63 68 65   of shared-cache
445c0 20 6c 6f 63 6b 69 6e 67 2e 20 0a 2a 2a 20 54 68   locking. .** Th
445d0 69 73 20 41 50 49 20 6d 61 79 20 62 65 20 75 73  is API may be us
445e0 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 61  ed to register a
445f0 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 53   callback that S
44600 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b  QLite will invok
44610 65 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 63  e .** when the c
44620 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 6e  onnection curren
44630 74 6c 79 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  tly holding the 
44640 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 72 65  required lock re
44650 6c 69 6e 71 75 69 73 68 65 73 20 69 74 2e 0a 2a  linquishes it..*
44660 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e  * This API is on
44670 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20  ly available if 
44680 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20  the library was 
44690 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
446a0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41  e.** [SQLITE_ENA
446b0 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46  BLE_UNLOCK_NOTIF
446c0 59 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f  Y] C-preprocesso
446d0 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64  r symbol defined
446e0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f  ..**.** See Also
446f0 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51 4c  : [Using the SQL
44700 69 74 65 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66  ite Unlock Notif
44710 69 63 61 74 69 6f 6e 20 46 65 61 74 75 72 65 5d  ication Feature]
44720 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 2d 63  ..**.** Shared-c
44730 61 63 68 65 20 6c 6f 63 6b 73 20 61 72 65 20 72  ache locks are r
44740 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 20 64  eleased when a d
44750 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
44760 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a 20  on concludes.** 
44770 69 74 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e  its current tran
44780 73 61 63 74 69 6f 6e 2c 20 65 69 74 68 65 72 20  saction, either 
44790 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67 20 69 74  by committing it
447a0 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69 74 20 62   or rolling it b
447b0 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  ack. .**.** When
447c0 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 6b   a connection (k
447d0 6e 6f 77 6e 20 61 73 20 74 68 65 20 62 6c 6f 63  nown as the bloc
447e0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20  ked connection) 
447f0 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20  fails to obtain 
44800 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68  a.** shared-cach
44810 65 20 6c 6f 63 6b 20 61 6e 64 20 53 51 4c 49 54  e lock and SQLIT
44820 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74 75  E_LOCKED is retu
44830 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c  rned to the call
44840 65 72 2c 20 74 68 65 0a 2a 2a 20 69 64 65 6e 74  er, the.** ident
44850 69 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 62  ity of the datab
44860 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28  ase connection (
44870 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e  the blocking con
44880 6e 65 63 74 69 6f 6e 29 20 74 68 61 74 0a 2a 2a  nection) that.**
44890 20 68 61 73 20 6c 6f 63 6b 65 64 20 74 68 65 20   has locked the 
448a0 72 65 71 75 69 72 65 64 20 72 65 73 6f 75 72 63  required resourc
448b0 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 74 65  e is stored inte
448c0 72 6e 61 6c 6c 79 2e 20 41 66 74 65 72 20 61 6e  rnally. After an
448d0 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e   .** application
448e0 20 72 65 63 65 69 76 65 73 20 61 6e 20 53 51 4c   receives an SQL
448f0 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72  ITE_LOCKED error
44900 2c 20 69 74 20 6d 61 79 20 63 61 6c 6c 20 74 68  , it may call th
44910 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c  e.** sqlite3_unl
44920 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 6d 65 74  ock_notify() met
44930 68 6f 64 20 77 69 74 68 20 74 68 65 20 62 6c 6f  hod with the blo
44940 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  cked connection 
44950 68 61 6e 64 6c 65 20 61 73 20 0a 2a 2a 20 74 68  handle as .** th
44960 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
44970 20 74 6f 20 72 65 67 69 73 74 65 72 20 66 6f 72   to register for
44980 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74   a callback that
44990 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64   will be invoked
449a0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 62 6c 6f  .** when the blo
449b0 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  cking connection
449c0 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  s current transa
449d0 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64  ction is conclud
449e0 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 62  ed. The.** callb
449f0 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 66  ack is invoked f
44a00 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 5b  rom within the [
44a10 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 20 6f 72  sqlite3_step] or
44a20 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5d   [sqlite3_close]
44a30 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 63 6f  .** call that co
44a40 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c 6f 63  ncludes the bloc
44a50 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  king connections
44a60 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a   transaction..**
44a70 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 75  .** If sqlite3_u
44a80 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69  nlock_notify() i
44a90 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 6d 75  s called in a mu
44aa0 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70  lti-threaded app
44ab0 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 68 65  lication,.** the
44ac0 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74  re is a chance t
44ad0 68 61 74 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67  hat the blocking
44ae0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c   connection will
44af0 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a   have already.**
44b00 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 73 20 74   concluded its t
44b10 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 20 74 68  ransaction by th
44b20 65 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 75  e time sqlite3_u
44b30 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69  nlock_notify() i
44b40 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 49 66  s invoked..** If
44b50 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 74   this happens, t
44b60 68 65 6e 20 74 68 65 20 73 70 65 63 69 66 69 65  hen the specifie
44b70 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  d callback is in
44b80 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  voked immediatel
44b90 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69  y,.** from withi
44ba0 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71  n the call to sq
44bb0 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
44bc0 69 66 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ify()..**.** If 
44bd0 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  the blocked conn
44be0 65 63 74 69 6f 6e 20 69 73 20 61 74 74 65 6d 70  ection is attemp
44bf0 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 6e 20 61  ting to obtain a
44c00 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61   write-lock on a
44c10 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65  .** shared-cache
44c20 20 74 61 62 6c 65 2c 20 61 6e 64 20 6d 6f 72 65   table, and more
44c30 20 74 68 61 6e 20 6f 6e 65 20 6f 74 68 65 72 20   than one other 
44c40 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65  connection curre
44c50 6e 74 6c 79 20 68 6f 6c 64 73 0a 2a 2a 20 61 20  ntly holds.** a 
44c60 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  read-lock on the
44c70 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 74 68 65   same table, the
44c80 6e 20 53 51 4c 69 74 65 20 61 72 62 69 74 72 61  n SQLite arbitra
44c90 72 69 6c 79 20 73 65 6c 65 63 74 73 20 6f 6e 65  rily selects one
44ca0 20 6f 66 20 0a 2a 2a 20 74 68 65 20 6f 74 68 65   of .** the othe
44cb0 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f  r connections to
44cc0 20 75 73 65 20 61 73 20 74 68 65 20 62 6c 6f 63   use as the bloc
44cd0 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  king connection.
44ce0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79  .**.** There may
44cf0 20 62 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20   be at most one 
44d00 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
44d10 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65 72 65  llback registere
44d20 64 20 62 79 20 61 20 0a 2a 2a 20 62 6c 6f 63 6b  d by a .** block
44d30 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49  ed connection. I
44d40 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b  f sqlite3_unlock
44d50 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63 61 6c  _notify() is cal
44d60 6c 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20  led when the.** 
44d70 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
44d80 6f 6e 20 61 6c 72 65 61 64 79 20 68 61 73 20 61  on already has a
44d90 20 72 65 67 69 73 74 65 72 65 64 20 75 6e 6c 6f   registered unlo
44da0 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
44db0 63 6b 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  ck,.** then the 
44dc0 6e 65 77 20 63 61 6c 6c 62 61 63 6b 20 72 65 70  new callback rep
44dd0 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 2e 20 49  laces the old. I
44de0 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b  f sqlite3_unlock
44df0 5f 6e 6f 74 69 66 79 28 29 20 69 73 0a 2a 2a 20  _notify() is.** 
44e00 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55  called with a NU
44e10 4c 4c 20 70 6f 69 6e 74 65 72 20 61 73 20 69 74  LL pointer as it
44e20 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  s second argumen
44e30 74 2c 20 74 68 65 6e 20 61 6e 79 20 65 78 69 73  t, then any exis
44e40 74 69 6e 67 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e  ting.** unlock-n
44e50 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69  otify callback i
44e60 73 20 63 61 6e 63 65 6c 6c 65 64 2e 20 54 68 65  s cancelled. The
44e70 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
44e80 69 6f 6e 73 20 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d  ions .** unlock-
44e90 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20  notify callback 
44ea0 6d 61 79 20 61 6c 73 6f 20 62 65 20 63 61 6e 63  may also be canc
44eb0 65 6c 65 64 20 62 79 20 63 6c 6f 73 69 6e 67 20  eled by closing 
44ec0 74 68 65 20 62 6c 6f 63 6b 65 64 0a 2a 2a 20 63  the blocked.** c
44ed0 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 69 6e 67 20  onnection using 
44ee0 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29  [sqlite3_close()
44ef0 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 6e 6c  ]..**.** The unl
44f00 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
44f10 61 63 6b 20 69 73 20 6e 6f 74 20 72 65 65 6e 74  ack is not reent
44f20 72 61 6e 74 2e 20 49 66 20 61 6e 20 61 70 70 6c  rant. If an appl
44f30 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 0a  ication invokes.
44f40 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65 33 5f 78  ** any sqlite3_x
44f50 78 78 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73  xx API functions
44f60 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20   from within an 
44f70 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
44f80 6c 6c 62 61 63 6b 2c 20 61 0a 2a 2a 20 63 72 61  llback, a.** cra
44f90 73 68 20 6f 72 20 64 65 61 64 6c 6f 63 6b 20 6d  sh or deadlock m
44fa0 61 79 20 62 65 20 74 68 65 20 72 65 73 75 6c 74  ay be the result
44fb0 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 64  ..**.** Unless d
44fc0 65 61 64 6c 6f 63 6b 20 69 73 20 64 65 74 65 63  eadlock is detec
44fd0 74 65 64 20 28 73 65 65 20 62 65 6c 6f 77 29 2c  ted (see below),
44fe0 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f   sqlite3_unlock_
44ff0 6e 6f 74 69 66 79 28 29 20 61 6c 77 61 79 73 0a  notify() always.
45000 2a 2a 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ** returns SQLIT
45010 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43  E_OK..**.** <b>C
45020 61 6c 6c 62 61 63 6b 20 49 6e 76 6f 63 61 74 69  allback Invocati
45030 6f 6e 20 44 65 74 61 69 6c 73 3c 2f 62 3e 0a 2a  on Details</b>.*
45040 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 75 6e 6c  *.** When an unl
45050 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
45060 61 63 6b 20 69 73 20 72 65 67 69 73 74 65 72 65  ack is registere
45070 64 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  d, the applicati
45080 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 20 0a 2a  on provides a .*
45090 2a 20 73 69 6e 67 6c 65 20 76 6f 69 64 2a 20 70  * single void* p
450a0 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 70  ointer that is p
450b0 61 73 73 65 64 20 74 6f 20 74 68 65 20 63 61 6c  assed to the cal
450c0 6c 62 61 63 6b 20 77 68 65 6e 20 69 74 20 69 73  lback when it is
450d0 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 48 6f 77   invoked..** How
450e0 65 76 65 72 2c 20 74 68 65 20 73 69 67 6e 61 74  ever, the signat
450f0 75 72 65 20 6f 66 20 74 68 65 20 63 61 6c 6c 62  ure of the callb
45100 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c  ack function all
45110 6f 77 73 20 53 51 4c 69 74 65 20 74 6f 20 70 61  ows SQLite to pa
45120 73 73 0a 2a 2a 20 69 74 20 61 6e 20 61 72 72 61  ss.** it an arra
45130 79 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e 74 65  y of void* conte
45140 78 74 20 70 6f 69 6e 74 65 72 73 2e 20 54 68 65  xt pointers. The
45150 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
45160 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 61 6e 20  passed to.** an 
45170 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
45180 6c 6c 62 61 63 6b 20 69 73 20 61 20 70 6f 69 6e  llback is a poin
45190 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20  ter to an array 
451a0 6f 66 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72  of void* pointer
451b0 73 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 65  s,.** and the se
451c0 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d 62  cond is the numb
451d0 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
451e0 20 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a   the array..**.*
451f0 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63 6b 69 6e  * When a blockin
45200 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72  g connections tr
45210 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e  ansaction is con
45220 63 6c 75 64 65 64 2c 20 74 68 65 72 65 20 6d 61  cluded, there ma
45230 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 74 68 61  y be.** more tha
45240 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64 20 63 6f  n one blocked co
45250 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 68 61  nnection that ha
45260 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72  s registered for
45270 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
45280 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 49  y.** callback. I
45290 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75  f two or more su
452a0 63 68 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  ch blocked conne
452b0 63 74 69 6f 6e 73 20 68 61 76 65 20 73 70 65 63  ctions have spec
452c0 69 66 69 65 64 20 74 68 65 0a 2a 2a 20 73 61 6d  ified the.** sam
452d0 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
452e0 69 6f 6e 2c 20 74 68 65 6e 20 69 6e 73 74 65 61  ion, then instea
452f0 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68  d of invoking th
45300 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
45310 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20  ion.** multiple 
45320 74 69 6d 65 73 2c 20 69 74 20 69 73 20 69 6e 76  times, it is inv
45330 6f 6b 65 64 20 6f 6e 63 65 20 77 69 74 68 20 74  oked once with t
45340 68 65 20 73 65 74 20 6f 66 20 76 6f 69 64 2a 20  he set of void* 
45350 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73  context pointers
45360 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79  .** specified by
45370 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e   the blocked con
45380 6e 65 63 74 69 6f 6e 73 20 62 75 6e 64 6c 65 64  nections bundled
45390 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 61   together into a
453a0 6e 20 61 72 72 61 79 2e 0a 2a 2a 20 54 68 69 73  n array..** This
453b0 20 67 69 76 65 73 20 74 68 65 20 61 70 70 6c 69   gives the appli
453c0 63 61 74 69 6f 6e 20 61 6e 20 6f 70 70 6f 72 74  cation an opport
453d0 75 6e 69 74 79 20 74 6f 20 70 72 69 6f 72 69 74  unity to priorit
453e0 69 7a 65 20 61 6e 79 20 61 63 74 69 6f 6e 73 20  ize any actions 
453f0 0a 2a 2a 20 72 65 6c 61 74 65 64 20 74 6f 20 74  .** related to t
45400 68 65 20 73 65 74 20 6f 66 20 75 6e 62 6c 6f 63  he set of unbloc
45410 6b 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ked database con
45420 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  nections..**.** 
45430 3c 62 3e 44 65 61 64 6c 6f 63 6b 20 44 65 74 65  <b>Deadlock Dete
45440 63 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20  ction</b>.**.** 
45450 41 73 73 75 6d 69 6e 67 20 74 68 61 74 20 61 66  Assuming that af
45460 74 65 72 20 72 65 67 69 73 74 65 72 69 6e 67 20  ter registering 
45470 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f  for an unlock-no
45480 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 61 20  tify callback a 
45490 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 61 69  .** database wai
454a0 74 73 20 66 6f 72 20 74 68 65 20 63 61 6c 6c 62  ts for the callb
454b0 61 63 6b 20 74 6f 20 62 65 20 69 73 73 75 65 64  ack to be issued
454c0 20 62 65 66 6f 72 65 20 74 61 6b 69 6e 67 20 61   before taking a
454d0 6e 79 20 66 75 72 74 68 65 72 0a 2a 2a 20 61 63  ny further.** ac
454e0 74 69 6f 6e 20 28 61 20 72 65 61 73 6f 6e 61 62  tion (a reasonab
454f0 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e 29 2c 20  le assumption), 
45500 74 68 65 6e 20 75 73 69 6e 67 20 74 68 69 73 20  then using this 
45510 41 50 49 20 6d 61 79 20 63 61 75 73 65 20 74 68  API may cause th
45520 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  e.** application
45530 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e 20 46 6f   to deadlock. Fo
45540 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 63 6f  r example, if co
45550 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73 20 77 61  nnection X is wa
45560 69 74 69 6e 67 20 66 6f 72 0a 2a 2a 20 63 6f 6e  iting for.** con
45570 6e 65 63 74 69 6f 6e 20 59 27 73 20 74 72 61 6e  nection Y's tran
45580 73 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f  saction to be co
45590 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20 73 69 6d  ncluded, and sim
455a0 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63 74 69 6f  ilarly connectio
455b0 6e 0a 2a 2a 20 59 20 69 73 20 77 61 69 74 69 6e  n.** Y is waitin
455c0 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  g on connection 
455d0 58 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  X's transaction,
455e0 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 63 6f   then neither co
455f0 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c  nnection.** will
45600 20 70 72 6f 63 65 65 64 20 61 6e 64 20 74 68 65   proceed and the
45610 20 73 79 73 74 65 6d 20 6d 61 79 20 72 65 6d 61   system may rema
45620 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64 20 69 6e  in deadlocked in
45630 64 65 66 69 6e 69 74 65 6c 79 2e 0a 2a 2a 0a 2a  definitely..**.*
45640 2a 20 54 6f 20 61 76 6f 69 64 20 74 68 69 73 20  * To avoid this 
45650 73 63 65 6e 61 72 69 6f 2c 20 74 68 65 20 73 71  scenario, the sq
45660 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
45670 69 66 79 28 29 20 70 65 72 66 6f 72 6d 73 20 64  ify() performs d
45680 65 61 64 6c 6f 63 6b 0a 2a 2a 20 64 65 74 65 63  eadlock.** detec
45690 74 69 6f 6e 2e 20 49 66 20 61 20 67 69 76 65 6e  tion. If a given
456a0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
456b0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
456c0 20 77 6f 75 6c 64 20 70 75 74 20 74 68 65 0a 2a   would put the.*
456d0 2a 20 73 79 73 74 65 6d 20 69 6e 20 61 20 64 65  * system in a de
456e0 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 2c 20  adlocked state, 
456f0 74 68 65 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  then SQLITE_LOCK
45700 45 44 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  ED is returned a
45710 6e 64 20 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d  nd no.** unlock-
45720 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20  notify callback 
45730 69 73 20 72 65 67 69 73 74 65 72 65 64 2e 20 54  is registered. T
45740 68 65 20 73 79 73 74 65 6d 20 69 73 20 73 61 69  he system is sai
45750 64 20 74 6f 20 62 65 20 69 6e 0a 2a 2a 20 61 20  d to be in.** a 
45760 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 65  deadlocked state
45770 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 41   if connection A
45780 20 68 61 73 20 72 65 67 69 73 74 65 72 65 64 20   has registered 
45790 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f  for an unlock-no
457a0 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  tify.** callback
457b0 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73 69   on the conclusi
457c0 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e  on of connection
457d0 20 42 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e   B's transaction
457e0 2c 20 61 6e 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  , and connection
457f0 0a 2a 2a 20 42 20 68 61 73 20 69 74 73 65 6c 66  .** B has itself
45800 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20   registered for 
45810 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  an unlock-notify
45820 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20 63   callback when c
45830 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 41 27 73  onnection.** A's
45840 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
45850 63 6f 6e 63 6c 75 64 65 64 2e 20 49 6e 64 69 72  concluded. Indir
45860 65 63 74 20 64 65 61 64 6c 6f 63 6b 20 69 73 20  ect deadlock is 
45870 61 6c 73 6f 20 64 65 74 65 63 74 65 64 2c 20 73  also detected, s
45880 6f 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20  o.** the system 
45890 69 73 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72  is also consider
458a0 65 64 20 74 6f 20 62 65 20 64 65 61 64 6c 6f 63  ed to be deadloc
458b0 6b 65 64 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f  ked if connectio
458c0 6e 20 42 20 68 61 73 0a 2a 2a 20 72 65 67 69 73  n B has.** regis
458d0 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c  tered for an unl
458e0 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
458f0 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c  ack on the concl
45900 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74  usion of connect
45910 69 6f 6e 0a 2a 2a 20 43 27 73 20 74 72 61 6e 73  ion.** C's trans
45920 61 63 74 69 6f 6e 2c 20 77 68 65 72 65 20 63 6f  action, where co
45930 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73 20 77 61  nnection C is wa
45940 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74  iting on connect
45950 69 6f 6e 20 41 2e 20 41 6e 79 0a 2a 2a 20 6e 75  ion A. Any.** nu
45960 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f  mber of levels o
45970 66 20 69 6e 64 69 72 65 63 74 69 6f 6e 20 61 72  f indirection ar
45980 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a 2a  e allowed..**.**
45990 20 3c 62 3e 54 68 65 20 22 44 52 4f 50 20 54 41   <b>The "DROP TA
459a0 42 4c 45 22 20 45 78 63 65 70 74 69 6f 6e 3c 2f  BLE" Exception</
459b0 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20  b>.**.** When a 
459c0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
459d0 5f 73 74 65 70 28 29 5d 20 72 65 74 75 72 6e 73  _step()] returns
459e0 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20   SQLITE_LOCKED, 
459f0 69 74 20 69 73 20 61 6c 6d 6f 73 74 20 0a 2a 2a  it is almost .**
45a00 20 61 6c 77 61 79 73 20 61 70 70 72 6f 70 72 69   always appropri
45a10 61 74 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69  ate to call sqli
45a20 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
45a30 79 28 29 2e 20 54 68 65 72 65 20 69 73 20 68 6f  y(). There is ho
45a40 77 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65 78  wever,.** one ex
45a50 63 65 70 74 69 6f 6e 2e 20 57 68 65 6e 20 65 78  ception. When ex
45a60 65 63 75 74 69 6e 67 20 61 20 22 44 52 4f 50 20  ecuting a "DROP 
45a70 54 41 42 4c 45 22 20 6f 72 20 22 44 52 4f 50 20  TABLE" or "DROP 
45a80 49 4e 44 45 58 22 20 73 74 61 74 65 6d 65 6e 74  INDEX" statement
45a90 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 63 68 65 63  ,.** SQLite chec
45aa0 6b 73 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ks if there are 
45ab0 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 65 78  any currently ex
45ac0 65 63 75 74 69 6e 67 20 53 45 4c 45 43 54 20 73  ecuting SELECT s
45ad0 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61  tatements.** tha
45ae0 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 65 20  t belong to the 
45af0 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  same connection.
45b00 20 49 66 20 74 68 65 72 65 20 61 72 65 2c 20 53   If there are, S
45b10 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 0a  QLITE_LOCKED is.
45b20 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20  ** returned. In 
45b30 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20  this case there 
45b40 69 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67 20  is no "blocking 
45b50 63 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 73 6f 20  connection", so 
45b60 69 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69  invoking.** sqli
45b70 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
45b80 79 28 29 20 72 65 73 75 6c 74 73 20 69 6e 20 74  y() results in t
45b90 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  he unlock-notify
45ba0 20 63 61 6c 6c 62 61 63 6b 20 62 65 69 6e 67 0a   callback being.
45bb0 2a 2a 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64  ** invoked immed
45bc0 69 61 74 65 6c 79 2e 20 49 66 20 74 68 65 20 61  iately. If the a
45bd0 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 65 6e 20  pplication then 
45be0 72 65 2d 61 74 74 65 6d 70 74 73 20 74 68 65 20  re-attempts the 
45bf0 22 44 52 4f 50 20 54 41 42 4c 45 22 0a 2a 2a 20  "DROP TABLE".** 
45c00 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58 22 20  or "DROP INDEX" 
45c10 71 75 65 72 79 2c 20 61 6e 20 69 6e 66 69 6e 69  query, an infini
45c20 74 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20 62 65  te loop might be
45c30 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a   the result..**.
45c40 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f 75 6e  ** One way aroun
45c50 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 69  d this problem i
45c60 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 65  s to check the e
45c70 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f  xtended error co
45c80 64 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62  de returned.** b
45c90 79 20 61 6e 20 73 71 6c 69 74 65 33 5f 73 74 65  y an sqlite3_ste
45ca0 70 28 29 20 63 61 6c 6c 2e 20 49 66 20 74 68 65  p() call. If the
45cb0 72 65 20 69 73 20 61 20 62 6c 6f 63 6b 69 6e 67  re is a blocking
45cc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65   connection, the
45cd0 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 6e 64 65  n the.** extende
45ce0 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  d error code is 
45cf0 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c 4f  set to SQLITE_LO
45d00 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45  CKED_SHAREDCACHE
45d10 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e 0a  . Otherwise, in.
45d20 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 22  ** the special "
45d30 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e 44 45 58  DROP TABLE/INDEX
45d40 22 20 63 61 73 65 2c 20 74 68 65 20 65 78 74 65  " case, the exte
45d50 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20  nded error code 
45d60 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 51 4c 49  is just .** SQLI
45d70 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 53 51  TE_LOCKED..*/.SQ
45d80 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
45d90 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69  ite3_unlock_noti
45da0 66 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 70  fy(.  sqlite3 *p
45db0 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20 20 20 20  Blocked,        
45dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45dd0 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 63 6f 6e    /* Waiting con
45de0 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  nection */.  voi
45df0 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f 69  d (*xNotify)(voi
45e00 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e 74 20 6e  d **apArg, int n
45e10 41 72 67 29 2c 20 20 20 20 2f 2a 20 43 61 6c 6c  Arg),    /* Call
45e20 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74 6f  back function to
45e30 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 76 6f 69   invoke */.  voi
45e40 64 20 2a 70 4e 6f 74 69 66 79 41 72 67 20 20 20  d *pNotifyArg   
45e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45e60 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
45e70 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20  ment to pass to 
45e80 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b 0a 0a 0a  xNotify */.);...
45e90 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
45ea0 53 74 72 69 6e 67 20 43 6f 6d 70 61 72 69 73 6f  String Compariso
45eb0 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  n.** EXPERIMENTA
45ec0 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  L.**.** The [sql
45ed0 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 29 5d  ite3_strnicmp()]
45ee0 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70 70 6c   API allows appl
45ef0 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 65 78 74  ications and ext
45f00 65 6e 73 69 6f 6e 73 20 74 6f 0a 2a 2a 20 63 6f  ensions to.** co
45f10 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65 6e  mpare the conten
45f20 74 73 20 6f 66 20 74 77 6f 20 62 75 66 66 65 72  ts of two buffer
45f30 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 55 54 46  s containing UTF
45f40 2d 38 20 73 74 72 69 6e 67 73 20 69 6e 20 61 0a  -8 strings in a.
45f50 2a 2a 20 63 61 73 65 2d 69 6e 64 65 6e 64 65 6e  ** case-indenden
45f60 74 20 66 61 73 68 69 6f 6e 2c 20 75 73 69 6e 67  t fashion, using
45f70 20 74 68 65 20 73 61 6d 65 20 64 65 66 69 6e 69   the same defini
45f80 74 69 6f 6e 20 6f 66 20 63 61 73 65 20 69 6e 64  tion of case ind
45f90 65 70 65 6e 64 65 6e 63 65 20 0a 2a 2a 20 74 68  ependence .** th
45fa0 61 74 20 53 51 4c 69 74 65 20 75 73 65 73 20 69  at SQLite uses i
45fb0 6e 74 65 72 6e 61 6c 6c 79 20 77 68 65 6e 20 63  nternally when c
45fc0 6f 6d 70 61 72 69 6e 67 20 69 64 65 6e 74 69 66  omparing identif
45fd0 69 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  iers..*/.SQLITE_
45fe0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
45ff0 73 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63  strnicmp(const c
46000 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  har *, const cha
46010 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  r *, int);../*.*
46020 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20  * Undo the hack 
46030 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c  that converts fl
46040 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70  oating point typ
46050 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f  es to integer fo
46060 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70  r.** builds on p
46070 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75  rocessors withou
46080 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  t floating point
46090 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66   support..*/.#if
460a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
460b0 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23  FLOATING_POINT.#
460c0 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 65   undef double.#e
460d0 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f  ndif..#if 0.}  /
460e0 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65 78  * End of the 'ex
460f0 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20  tern "C"' block 
46100 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  */.#endif.#endif
46110 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
46120 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65  ** End of sqlite
46130 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  3.h ************
46140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46160 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
46170 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
46180 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
46190 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
461a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
461b0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
461c0 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 68 2e  ** Include hash.
461d0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
461e0 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  of sqliteInt.h *
461f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46200 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
46210 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61  ** Begin file ha
46220 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
46230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46250 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
46260 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a  ptember 22.**.**
46270 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
46280 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
46290 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
462a0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
462b0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
462c0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
462d0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
462e0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
462f0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
46300 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
46310 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
46320 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
46330 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
46340 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
46350 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
46360 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
46370 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
46380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
463d0 73 20 69 73 20 74 68 65 20 68 65 61 64 65 72 20  s is the header 
463e0 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67 65 6e  file for the gen
463f0 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65 20  eric hash-table 
46400 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a  implemenation.**
46410 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e   used in SQLite.
46420 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68  .**.** $Id: hash
46430 2e 68 2c 76 20 31 2e 31 35 20 32 30 30 39 2f 30  .h,v 1.15 2009/0
46440 35 2f 30 32 20 31 33 3a 32 39 3a 33 38 20 64 72  5/02 13:29:38 dr
46450 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  h Exp $.*/.#ifnd
46460 65 66 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f  ef _SQLITE_HASH_
46470 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49  H_.#define _SQLI
46480 54 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46  TE_HASH_H_../* F
46490 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
464a0 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 65  ons of structure
464b0 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s. */.typedef st
464c0 72 75 63 74 20 48 61 73 68 20 48 61 73 68 3b 0a  ruct Hash Hash;.
464d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 48  typedef struct H
464e0 61 73 68 45 6c 65 6d 20 48 61 73 68 45 6c 65 6d  ashElem HashElem
464f0 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65  ;../* A complete
46500 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61   hash table is a
46510 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
46520 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
46530 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e  cture..** The in
46540 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20  ternals of this 
46550 73 74 72 75 63 74 75 72 65 20 61 72 65 20 69 6e  structure are in
46560 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61  tended to be opa
46570 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a  que -- client.**
46580 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74   code should not
46590 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63 65   attempt to acce
465a0 73 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 65  ss or modify the
465b0 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20   fields of this 
465c0 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 72  structure.** dir
465d0 65 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 74  ectly.  Change t
465e0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e  his structure on
465f0 6c 79 20 62 79 20 75 73 69 6e 67 20 74 68 65 20  ly by using the 
46600 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a  routines below..
46610 2a 2a 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65  ** However, some
46620 20 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64 75   of the "procedu
46630 72 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74 69  res" and "functi
46640 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 69  ons" for modifyi
46650 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73  ng and.** access
46660 69 6e 67 20 74 68 69 73 20 73 74 72 75 63 74 75  ing this structu
46670 72 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d 61  re are really ma
46680 63 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e 27  cros, so we can'
46690 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a  t really make.**
466a0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
466b0 6f 70 61 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c  opaque..**.** Al
466c0 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  l elements of th
466d0 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 72 65  e hash table are
466e0 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75   on a single dou
466f0 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e  bly-linked list.
46700 0a 2a 2a 20 48 61 73 68 2e 66 69 72 73 74 20 70  .** Hash.first p
46710 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 68 65 61  oints to the hea
46720 64 20 6f 66 20 74 68 69 73 20 6c 69 73 74 2e 0a  d of this list..
46730 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  **.** There are 
46740 48 61 73 68 2e 68 74 73 69 7a 65 20 62 75 63 6b  Hash.htsize buck
46750 65 74 73 2e 20 20 45 61 63 68 20 62 75 63 6b 65  ets.  Each bucke
46760 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 70  t points to a sp
46770 6f 74 20 69 6e 0a 2a 2a 20 74 68 65 20 67 6c 6f  ot in.** the glo
46780 62 61 6c 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65  bal doubly-linke
46790 64 20 6c 69 73 74 2e 20 20 54 68 65 20 63 6f 6e  d list.  The con
467a0 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 75 63  tents of the buc
467b0 6b 65 74 20 61 72 65 20 74 68 65 0a 2a 2a 20 65  ket are the.** e
467c0 6c 65 6d 65 6e 74 20 70 6f 69 6e 74 65 64 20 74  lement pointed t
467d0 6f 20 70 6c 75 73 20 74 68 65 20 6e 65 78 74 20  o plus the next 
467e0 5f 68 74 2e 63 6f 75 6e 74 2d 31 20 65 6c 65 6d  _ht.count-1 elem
467f0 65 6e 74 73 20 69 6e 20 74 68 65 20 6c 69 73 74  ents in the list
46800 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 2e 68 74 73  ..**.** Hash.hts
46810 69 7a 65 20 61 6e 64 20 48 61 73 68 2e 68 74 20  ize and Hash.ht 
46820 6d 61 79 20 62 65 20 7a 65 72 6f 2e 20 20 49 6e  may be zero.  In
46830 20 74 68 61 74 20 63 61 73 65 20 6c 6f 6f 6b 75   that case looku
46840 70 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 62 79 20  p is done.** by 
46850 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 20  a linear search 
46860 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c 69  of the global li
46870 73 74 2e 20 20 46 6f 72 20 73 6d 61 6c 6c 20 74  st.  For small t
46880 61 62 6c 65 73 2c 20 74 68 65 20 0a 2a 2a 20 48  ables, the .** H
46890 61 73 68 2e 68 74 20 74 61 62 6c 65 20 69 73 20  ash.ht table is 
468a0 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 64 20  never allocated 
468b0 62 65 63 61 75 73 65 20 69 66 20 74 68 65 72 65  because if there
468c0 20 61 72 65 20 66 65 77 20 65 6c 65 6d 65 6e 74   are few element
468d0 73 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 6c  s.** in the tabl
468e0 65 2c 20 69 74 20 69 73 20 66 61 73 74 65 72 20  e, it is faster 
468f0 74 6f 20 64 6f 20 61 20 6c 69 6e 65 61 72 20 73  to do a linear s
46900 65 61 72 63 68 20 74 68 61 6e 20 74 6f 20 6d 61  earch than to ma
46910 6e 61 67 65 0a 2a 2a 20 74 68 65 20 68 61 73 68  nage.** the hash
46920 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63   table..*/.struc
46930 74 20 48 61 73 68 20 7b 0a 20 20 75 6e 73 69 67  t Hash {.  unsig
46940 6e 65 64 20 69 6e 74 20 68 74 73 69 7a 65 3b 20  ned int htsize; 
46950 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
46960 66 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68 65  f buckets in the
46970 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
46980 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 6f   unsigned int co
46990 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  unt;       /* Nu
469a0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
469b0 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  in this table */
469c0 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 72  .  HashElem *fir
469d0 73 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  st;          /* 
469e0 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e  The first elemen
469f0 74 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 2a  t of the array *
46a00 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 7b  /.  struct _ht {
46a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
46a20 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
46a30 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e 74  */.    int count
46a40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
46a50 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
46a60 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 69 73  ntries with this
46a70 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 48 61 73   hash */.    Has
46a80 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20  hElem *chain;   
46a90 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
46aa0 65 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72  er to first entr
46ab0 79 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68  y with this hash
46ac0 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a   */.  } *ht;.};.
46ad0 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e 74  ./* Each element
46ae0 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62   in the hash tab
46af0 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  le is an instanc
46b00 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
46b10 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ng .** structure
46b20 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20  .  All elements 
46b30 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20  are stored on a 
46b40 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69  single doubly-li
46b50 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a  nked list..**.**
46b60 20 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74 72   Again, this str
46b70 75 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e 64  ucture is intend
46b80 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 2c  ed to be opaque,
46b90 20 62 75 74 20 69 74 20 63 61 6e 27 74 20 72 65   but it can't re
46ba0 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75  ally.** be opaqu
46bb0 65 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20  e because it is 
46bc0 75 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a  used by macros..
46bd0 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68 45 6c  */.struct HashEl
46be0 65 6d 20 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  em {.  HashElem 
46bf0 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 20 20  *next, *prev;   
46c00 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64 20      /* Next and 
46c10 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74  previous element
46c20 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 2a  s in the table *
46c30 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 20  /.  void *data; 
46c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46c50 20 2f 2a 20 44 61 74 61 20 61 73 73 6f 63 69 61   /* Data associa
46c60 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65 6c  ted with this el
46c70 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ement */.  const
46c80 20 63 68 61 72 20 2a 70 4b 65 79 3b 20 69 6e 74   char *pKey; int
46c90 20 6e 4b 65 79 3b 20 20 2f 2a 20 4b 65 79 20 61   nKey;  /* Key a
46ca0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
46cb0 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d  his element */.}
46cc0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20  ;../*.** Access 
46cd0 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64 65  routines.  To de
46ce0 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20 4e  lete, insert a N
46cf0 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  ULL pointer..*/.
46d00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
46d10 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49  oid sqlite3HashI
46d20 6e 69 74 28 48 61 73 68 2a 29 3b 0a 53 51 4c 49  nit(Hash*);.SQLI
46d30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
46d40 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65  *sqlite3HashInse
46d50 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20  rt(Hash*, const 
46d60 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20  char *pKey, int 
46d70 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61 74  nKey, void *pDat
46d80 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  a);.SQLITE_PRIVA
46d90 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
46da0 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 48  HashFind(const H
46db0 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  ash*, const char
46dc0 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
46dd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
46de0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61  E void sqlite3Ha
46df0 73 68 43 6c 65 61 72 28 48 61 73 68 2a 29 3b 0a  shClear(Hash*);.
46e00 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  ./*.** Macros fo
46e10 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61  r looping over a
46e20 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61  ll elements of a
46e30 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68   hash table.  Th
46e40 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69  e idiom is.** li
46e50 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
46e60 20 48 61 73 68 20 68 3b 0a 2a 2a 20 20 20 48 61   Hash h;.**   Ha
46e70 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20  shElem *p;.**   
46e80 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28 70 3d 73  ....**   for(p=s
46e90 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
46ea0 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48  h); p; p=sqliteH
46eb0 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a 20  ashNext(p)){.** 
46ec0 20 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75 72      SomeStructur
46ed0 65 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69 74  e *pData = sqlit
46ee0 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 2a 2a  eHashData(p);.**
46ef0 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 74       // do somet
46f00 68 69 6e 67 20 77 69 74 68 20 70 44 61 74 61 0a  hing with pData.
46f10 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e  **   }.*/.#defin
46f20 65 20 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  e sqliteHashFirs
46f30 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72 73  t(H)  ((H)->firs
46f40 74 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  t).#define sqlit
46f50 65 48 61 73 68 4e 65 78 74 28 45 29 20 20 20 28  eHashNext(E)   (
46f60 28 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69  (E)->next).#defi
46f70 6e 65 20 73 71 6c 69 74 65 48 61 73 68 44 61 74  ne sqliteHashDat
46f80 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64 61 74  a(E)   ((E)->dat
46f90 61 29 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 71  a)./* #define sq
46fa0 6c 69 74 65 48 61 73 68 4b 65 79 28 45 29 20 20  liteHashKey(E)  
46fb0 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 20 2f 2f    ((E)->pKey) //
46fc0 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a 20   NOT USED */./* 
46fd0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61  #define sqliteHa
46fe0 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28 28 45  shKeysize(E) ((E
46ff0 29 2d 3e 6e 4b 65 79 29 20 20 2f 2f 20 4e 4f 54  )->nKey)  // NOT
47000 20 55 53 45 44 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20   USED */../*.** 
47010 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
47020 73 20 69 6e 20 61 20 68 61 73 68 20 74 61 62 6c  s in a hash tabl
47030 65 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20  e.*/./* #define 
47040 73 71 6c 69 74 65 48 61 73 68 43 6f 75 6e 74 28  sqliteHashCount(
47050 48 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29  H)  ((H)->count)
47060 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a   // NOT USED */.
47070 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49  .#endif /* _SQLI
47080 54 45 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f  TE_HASH_H_ */../
47090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
470a0 6e 64 20 6f 66 20 68 61 73 68 2e 68 20 2a 2a 2a  nd of hash.h ***
470b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2f 0a 2f  *************/./
470e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
470f0 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
47100 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73  we left off in s
47110 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
47120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
47130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
47140 6e 63 6c 75 64 65 20 70 61 72 73 65 2e 68 20 69  nclude parse.h i
47150 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
47160 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
47170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
47180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
47190 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e  egin file parse.
471a0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  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 2f 0a 23  *************/.#
471d0 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d 49 20 20  define TK_SEMI  
471e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471f0 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66            1.#def
47200 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49 4e 20 20  ine TK_EXPLAIN  
47210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47220 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65         2.#define
47230 20 54 4b 5f 51 55 45 52 59 20 20 20 20 20 20 20   TK_QUERY       
47240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47250 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 54 4b      3.#define TK
47260 5f 50 4c 41 4e 20 20 20 20 20 20 20 20 20 20 20  _PLAN           
47270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47280 20 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45   4.#define TK_BE
47290 47 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  GIN             
472a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a                5.
472b0 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 41 4e 53  #define TK_TRANS
472c0 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20  ACTION          
472d0 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65             6.#de
472e0 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 45 44  fine TK_DEFERRED
472f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47300 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e          7.#defin
47310 65 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20 20  e TK_IMMEDIATE  
47320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47330 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 54       8.#define T
47340 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  K_EXCLUSIVE     
47350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47360 20 20 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43    9.#define TK_C
47370 4f 4d 4d 49 54 20 20 20 20 20 20 20 20 20 20 20  OMMIT           
47380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
47390 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 20  .#define TK_END 
473a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473b0 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 64             11.#d
473c0 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41 43  efine TK_ROLLBAC
473d0 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K               
473e0 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69          12.#defi
473f0 6e 65 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54 20  ne TK_SAVEPOINT 
47400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47410 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20       13.#define 
47420 54 4b 5f 52 45 4c 45 41 53 45 20 20 20 20 20 20  TK_RELEASE      
47430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47440 20 20 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f    14.#define TK_
47450 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TO              
47460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
47470 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 41 42  5.#define TK_TAB
47480 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LE              
47490 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 23              16.#
474a0 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54 45  define TK_CREATE
474b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474c0 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65 66           17.#def
474d0 69 6e 65 20 54 4b 5f 49 46 20 20 20 20 20 20 20  ine 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 31 38 0a 23 64 65 66 69 6e 65        18.#define
47500 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 20 20 20   TK_NOT         
47510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47520 20 20 20 31 39 0a 23 64 65 66 69 6e 65 20 54 4b     19.#define TK
47530 5f 45 58 49 53 54 53 20 20 20 20 20 20 20 20 20  _EXISTS         
47540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47550 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 45  20.#define TK_TE
47560 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MP              
47570 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31 0a               21.
47580 23 64 65 66 69 6e 65 20 54 4b 5f 4c 50 20 20 20  #define TK_LP   
47590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475a0 20 20 20 20 20 20 20 20 20 20 32 32 0a 23 64 65            22.#de
475b0 66 69 6e 65 20 54 4b 5f 52 50 20 20 20 20 20 20  fine 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 32 33 0a 23 64 65 66 69 6e         23.#defin
475e0 65 20 54 4b 5f 41 53 20 20 20 20 20 20 20 20 20  e TK_AS         
475f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47600 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 54      24.#define T
47610 4b 5f 43 4f 4d 4d 41 20 20 20 20 20 20 20 20 20  K_COMMA         
47620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47630 20 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49   25.#define TK_I
47640 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
47650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 36                26
47660 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45  .#define TK_INDE
47670 58 45 44 20 20 20 20 20 20 20 20 20 20 20 20 20  XED             
47680 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64             27.#d
47690 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 54 20 20  efine TK_ABORT  
476a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
476b0 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69          28.#defi
476c0 6e 65 20 54 4b 5f 41 43 54 49 4f 4e 20 20 20 20  ne TK_ACTION    
476d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
476e0 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 20       29.#define 
476f0 54 4b 5f 41 46 54 45 52 20 20 20 20 20 20 20 20  TK_AFTER        
47700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47710 20 20 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f    30.#define TK_
47720 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20  ANALYZE         
47730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
47740 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 43  1.#define TK_ASC
47750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47760 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 23              32.#
47770 64 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43 48  define TK_ATTACH
47780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47790 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65 66           33.#def
477a0 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20 20 20  ine TK_BEFORE   
477b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477c0 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65        34.#define
477d0 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20 20   TK_BY          
477e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477f0 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 54 4b     35.#define TK
47800 5f 43 41 53 43 41 44 45 20 20 20 20 20 20 20 20  _CASCADE        
47810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47820 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41  36.#define TK_CA
47830 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ST              
47840 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 0a               37.
47850 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d  #define TK_COLUM
47860 4e 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20  NKW             
47870 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 65            38.#de
47880 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 4c 49 43 54  fine TK_CONFLICT
47890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478a0 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69 6e         39.#defin
478b0 65 20 54 4b 5f 44 41 54 41 42 41 53 45 20 20 20  e TK_DATABASE   
478c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478d0 20 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 54      40.#define T
478e0 4b 5f 44 45 53 43 20 20 20 20 20 20 20 20 20 20  K_DESC          
478f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47900 20 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44   41.#define TK_D
47910 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20  ETACH           
47920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 32                42
47930 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 41 43 48  .#define TK_EACH
47940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47950 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 23 64             43.#d
47960 65 66 69 6e 65 20 54 4b 5f 46 41 49 4c 20 20 20  efine TK_FAIL   
47970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47980 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 69          44.#defi
47990 6e 65 20 54 4b 5f 46 4f 52 20 20 20 20 20 20 20  ne 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 34 35 0a 23 64 65 66 69 6e 65 20       45.#define 
479c0 54 4b 5f 49 47 4e 4f 52 45 20 20 20 20 20 20 20  TK_IGNORE       
479d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479e0 20 20 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f    46.#define TK_
479f0 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20  INITIALLY       
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
47a10 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53  7.#define TK_INS
47a20 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20  TEAD            
47a30 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 23              48.#
47a40 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f 4b  define TK_LIKE_K
47a50 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  W               
47a60 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 66           49.#def
47a70 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20 20 20  ine TK_MATCH    
47a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a90 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 6e 65        50.#define
47aa0 20 54 4b 5f 4e 4f 20 20 20 20 20 20 20 20 20 20   TK_NO          
47ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ac0 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 54 4b     51.#define TK
47ad0 5f 4b 45 59 20 20 20 20 20 20 20 20 20 20 20 20  _KEY            
47ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47af0 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46  52.#define TK_OF
47b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b10 20 20 20 20 20 20 20 20 20 20 20 20 20 35 33 0a               53.
47b20 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 46 53 45  #define TK_OFFSE
47b30 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
47b40 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64 65            54.#de
47b50 66 69 6e 65 20 54 4b 5f 50 52 41 47 4d 41 20 20  fine TK_PRAGMA  
47b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b70 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e         55.#defin
47b80 65 20 54 4b 5f 52 41 49 53 45 20 20 20 20 20 20  e TK_RAISE      
47b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ba0 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65 20 54      56.#define T
47bb0 4b 5f 52 45 50 4c 41 43 45 20 20 20 20 20 20 20  K_REPLACE       
47bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bd0 20 35 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   57.#define TK_R
47be0 45 53 54 52 49 43 54 20 20 20 20 20 20 20 20 20  ESTRICT         
47bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 38                58
47c00 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 57 20  .#define TK_ROW 
47c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 64             59.#d
47c30 65 66 69 6e 65 20 54 4b 5f 54 52 49 47 47 45 52  efine TK_TRIGGER
47c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c50 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 69          60.#defi
47c60 6e 65 20 54 4b 5f 56 41 43 55 55 4d 20 20 20 20  ne TK_VACUUM    
47c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c80 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 20       61.#define 
47c90 54 4b 5f 56 49 45 57 20 20 20 20 20 20 20 20 20  TK_VIEW         
47ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47cb0 20 20 36 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f    62.#define TK_
47cc0 56 49 52 54 55 41 4c 20 20 20 20 20 20 20 20 20  VIRTUAL         
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
47ce0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 49  3.#define TK_REI
47cf0 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20  NDEX            
47d00 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23              64.#
47d10 64 65 66 69 6e 65 20 54 4b 5f 52 45 4e 41 4d 45  define TK_RENAME
47d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d30 20 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 66           65.#def
47d40 69 6e 65 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 20  ine TK_CTIME_KW 
47d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d60 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 6e 65        66.#define
47d70 20 54 4b 5f 41 4e 59 20 20 20 20 20 20 20 20 20   TK_ANY         
47d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d90 20 20 20 36 37 0a 23 64 65 66 69 6e 65 20 54 4b     67.#define TK
47da0 5f 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20  _OR             
47db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47dc0 36 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e  68.#define TK_AN
47dd0 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
47de0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 39 0a               69.
47df0 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 20 20 20  #define TK_IS   
47e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e10 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 64 65            70.#de
47e20 66 69 6e 65 20 54 4b 5f 42 45 54 57 45 45 4e 20  fine TK_BETWEEN 
47e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e40 20 20 20 20 20 20 20 37 31 0a 23 64 65 66 69 6e         71.#defin
47e50 65 20 54 4b 5f 49 4e 20 20 20 20 20 20 20 20 20  e TK_IN         
47e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e70 20 20 20 20 37 32 0a 23 64 65 66 69 6e 65 20 54      72.#define T
47e80 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20  K_ISNULL        
47e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ea0 20 37 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e   73.#define TK_N
47eb0 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20  OTNULL          
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 34                74
47ed0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 45 20 20  .#define TK_NE  
47ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ef0 20 20 20 20 20 20 20 20 20 20 20 37 35 0a 23 64             75.#d
47f00 65 66 69 6e 65 20 54 4b 5f 45 51 20 20 20 20 20  efine TK_EQ     
47f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f20 20 20 20 20 20 20 20 20 37 36 0a 23 64 65 66 69          76.#defi
47f30 6e 65 20 54 4b 5f 47 54 20 20 20 20 20 20 20 20  ne 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 37 37 0a 23 64 65 66 69 6e 65 20       77.#define 
47f60 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20  TK_LE           
47f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f80 20 20 37 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f    78.#define TK_
47f90 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LT              
47fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
47fb0 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 45 20  9.#define TK_GE 
47fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 38 30 0a 23              80.#
47fe0 64 65 66 69 6e 65 20 54 4b 5f 45 53 43 41 50 45  define TK_ESCAPE
47ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48000 20 20 20 20 20 20 20 20 20 38 31 0a 23 64 65 66           81.#def
48010 69 6e 65 20 54 4b 5f 42 49 54 41 4e 44 20 20 20  ine TK_BITAND   
48020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48030 20 20 20 20 20 20 38 32 0a 23 64 65 66 69 6e 65        82.#define
48040 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 20 20 20   TK_BITOR       
48050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48060 20 20 20 38 33 0a 23 64 65 66 69 6e 65 20 54 4b     83.#define TK
48070 5f 4c 53 48 49 46 54 20 20 20 20 20 20 20 20 20  _LSHIFT         
48080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48090 38 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 53  84.#define TK_RS
480a0 48 49 46 54 20 20 20 20 20 20 20 20 20 20 20 20  HIFT            
480b0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35 0a               85.
480c0 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 55 53 20  #define TK_PLUS 
480d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480e0 20 20 20 20 20 20 20 20 20 20 38 36 0a 23 64 65            86.#de
480f0 66 69 6e 65 20 54 4b 5f 4d 49 4e 55 53 20 20 20  fine TK_MINUS   
48100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48110 20 20 20 20 20 20 20 38 37 0a 23 64 65 66 69 6e         87.#defin
48120 65 20 54 4b 5f 53 54 41 52 20 20 20 20 20 20 20  e TK_STAR       
48130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48140 20 20 20 20 38 38 0a 23 64 65 66 69 6e 65 20 54      88.#define T
48150 4b 5f 53 4c 41 53 48 20 20 20 20 20 20 20 20 20  K_SLASH         
48160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48170 20 38 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   89.#define TK_R
48180 45 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EM              
48190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 30                90
481a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 43  .#define TK_CONC
481b0 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AT              
481c0 20 20 20 20 20 20 20 20 20 20 20 39 31 0a 23 64             91.#d
481d0 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c 41 54 45  efine TK_COLLATE
481e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481f0 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 66 69          92.#defi
48200 6e 65 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 20  ne TK_BITNOT    
48210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48220 20 20 20 20 20 39 33 0a 23 64 65 66 69 6e 65 20       93.#define 
48230 54 4b 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20  TK_STRING       
48240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48250 20 20 39 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f    94.#define TK_
48260 4a 4f 49 4e 5f 4b 57 20 20 20 20 20 20 20 20 20  JOIN_KW         
48270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
48280 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e  5.#define TK_CON
48290 53 54 52 41 49 4e 54 20 20 20 20 20 20 20 20 20  STRAINT         
482a0 20 20 20 20 20 20 20 20 20 20 20 20 39 36 0a 23              96.#
482b0 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 41 55 4c  define TK_DEFAUL
482c0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
482d0 20 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 66           97.#def
482e0 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20 20 20 20  ine TK_NULL     
482f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48300 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 6e 65        98.#define
48310 20 54 4b 5f 50 52 49 4d 41 52 59 20 20 20 20 20   TK_PRIMARY     
48320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48330 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 54 4b     99.#define TK
48340 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20 20 20  _UNIQUE         
48350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48360 31 30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43  100.#define TK_C
48370 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20 20  HECK            
48380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
48390 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 46  1.#define TK_REF
483a0 45 52 45 4e 43 45 53 20 20 20 20 20 20 20 20 20  ERENCES         
483b0 20 20 20 20 20 20 20 20 20 20 20 20 31 30 32 0a              102.
483c0 23 64 65 66 69 6e 65 20 54 4b 5f 41 55 54 4f 49  #define TK_AUTOI
483d0 4e 43 52 20 20 20 20 20 20 20 20 20 20 20 20 20  NCR             
483e0 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64            103.#d
483f0 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20 20 20 20  efine TK_ON     
48400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48410 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 66          104.#def
48420 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 20 20 20  ine TK_DELETE   
48430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48440 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e        105.#defin
48450 65 20 54 4b 5f 55 50 44 41 54 45 20 20 20 20 20  e TK_UPDATE     
48460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48470 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 65 20      106.#define 
48480 54 4b 5f 53 45 54 20 20 20 20 20 20 20 20 20 20  TK_SET          
48490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484a0 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 54 4b    107.#define TK
484b0 5f 44 45 46 45 52 52 41 42 4c 45 20 20 20 20 20  _DEFERRABLE     
484c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484d0 31 30 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46  108.#define TK_F
484e0 4f 52 45 49 47 4e 20 20 20 20 20 20 20 20 20 20  OREIGN          
484f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
48500 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 52 4f  9.#define TK_DRO
48510 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  P               
48520 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30 0a              110.
48530 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f 4e  #define TK_UNION
48540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48550 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 23 64            111.#d
48560 65 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 20  efine TK_ALL    
48570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48580 20 20 20 20 20 20 20 20 31 31 32 0a 23 64 65 66          112.#def
48590 69 6e 65 20 54 4b 5f 45 58 43 45 50 54 20 20 20  ine TK_EXCEPT   
485a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485b0 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e        113.#defin
485c0 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 20  e TK_INTERSECT  
485d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485e0 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 20      114.#define 
485f0 54 4b 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20  TK_SELECT       
48600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48610 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 54 4b    115.#define TK
48620 5f 44 49 53 54 49 4e 43 54 20 20 20 20 20 20 20  _DISTINCT       
48630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48640 31 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44  116.#define TK_D
48650 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OT              
48660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
48670 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 52 4f  7.#define TK_FRO
48680 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  M               
48690 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38 0a              118.
486a0 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 20  #define TK_JOIN 
486b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486c0 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 23 64            119.#d
486d0 65 66 69 6e 65 20 54 4b 5f 55 53 49 4e 47 20 20  efine TK_USING  
486e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486f0 20 20 20 20 20 20 20 20 31 32 30 0a 23 64 65 66          120.#def
48700 69 6e 65 20 54 4b 5f 4f 52 44 45 52 20 20 20 20  ine TK_ORDER    
48710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48720 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 6e        121.#defin
48730 65 20 54 4b 5f 47 52 4f 55 50 20 20 20 20 20 20  e TK_GROUP      
48740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48750 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20      122.#define 
48760 54 4b 5f 48 41 56 49 4e 47 20 20 20 20 20 20 20  TK_HAVING       
48770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48780 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 54 4b    123.#define TK
48790 5f 4c 49 4d 49 54 20 20 20 20 20 20 20 20 20 20  _LIMIT          
487a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487b0 31 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57  124.#define TK_W
487c0 48 45 52 45 20 20 20 20 20 20 20 20 20 20 20 20  HERE            
487d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
487e0 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54  5.#define TK_INT
487f0 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  O               
48800 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36 0a              126.
48810 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 4c 55 45  #define TK_VALUE
48820 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  S               
48830 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 64            127.#d
48840 65 66 69 6e 65 20 54 4b 5f 49 4e 53 45 52 54 20  efine TK_INSERT 
48850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48860 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66          128.#def
48870 69 6e 65 20 54 4b 5f 49 4e 54 45 47 45 52 20 20  ine TK_INTEGER  
48880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48890 20 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e        129.#defin
488a0 65 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 20 20  e TK_FLOAT      
488b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488c0 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e 65 20      130.#define 
488d0 54 4b 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 20  TK_BLOB         
488e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488f0 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 54 4b    131.#define TK
48900 5f 52 45 47 49 53 54 45 52 20 20 20 20 20 20 20  _REGISTER       
48910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48920 31 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56  132.#define TK_V
48930 41 52 49 41 42 4c 45 20 20 20 20 20 20 20 20 20  ARIABLE         
48940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
48950 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53  3.#define TK_CAS
48960 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
48970 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 0a              134.
48980 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 4e 20  #define TK_WHEN 
48990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
489a0 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 64            135.#d
489b0 65 66 69 6e 65 20 54 4b 5f 54 48 45 4e 20 20 20  efine TK_THEN   
489c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
489d0 20 20 20 20 20 20 20 20 31 33 36 0a 23 64 65 66          136.#def
489e0 69 6e 65 20 54 4b 5f 45 4c 53 45 20 20 20 20 20  ine TK_ELSE     
489f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a00 20 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e        137.#defin
48a10 65 20 54 4b 5f 49 4e 44 45 58 20 20 20 20 20 20  e TK_INDEX      
48a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a30 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65 20      138.#define 
48a40 54 4b 5f 41 4c 54 45 52 20 20 20 20 20 20 20 20  TK_ALTER        
48a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a60 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 54 4b    139.#define TK
48a70 5f 41 44 44 20 20 20 20 20 20 20 20 20 20 20 20  _ADD            
48a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a90 31 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54  140.#define TK_T
48aa0 4f 5f 54 45 58 54 20 20 20 20 20 20 20 20 20 20  O_TEXT          
48ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
48ac0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f  1.#define TK_TO_
48ad0 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20 20  BLOB            
48ae0 20 20 20 20 20 20 20 20 20 20 20 20 31 34 32 0a              142.
48af0 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e 55  #define TK_TO_NU
48b00 4d 45 52 49 43 20 20 20 20 20 20 20 20 20 20 20  MERIC           
48b10 20 20 20 20 20 20 20 20 20 20 31 34 33 0a 23 64            143.#d
48b20 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54 20  efine TK_TO_INT 
48b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48b40 20 20 20 20 20 20 20 20 31 34 34 0a 23 64 65 66          144.#def
48b50 69 6e 65 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20  ine TK_TO_REAL  
48b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48b70 20 20 20 20 20 20 31 34 35 0a 23 64 65 66 69 6e        145.#defin
48b80 65 20 54 4b 5f 49 53 4e 4f 54 20 20 20 20 20 20  e TK_ISNOT      
48b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48ba0 20 20 20 20 31 34 36 0a 23 64 65 66 69 6e 65 20      146.#define 
48bb0 54 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c 45 20 20  TK_END_OF_FILE  
48bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48bd0 20 20 31 34 37 0a 23 64 65 66 69 6e 65 20 54 4b    147.#define TK
48be0 5f 49 4c 4c 45 47 41 4c 20 20 20 20 20 20 20 20  _ILLEGAL        
48bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48c00 31 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53  148.#define TK_S
48c10 50 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20  PACE            
48c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
48c30 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 43  9.#define TK_UNC
48c40 4c 4f 53 45 44 5f 53 54 52 49 4e 47 20 20 20 20  LOSED_STRING    
48c50 20 20 20 20 20 20 20 20 20 20 20 20 31 35 30 0a              150.
48c60 23 64 65 66 69 6e 65 20 54 4b 5f 46 55 4e 43 54  #define TK_FUNCT
48c70 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  ION             
48c80 20 20 20 20 20 20 20 20 20 20 31 35 31 0a 23 64            151.#d
48c90 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20  efine TK_COLUMN 
48ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48cb0 20 20 20 20 20 20 20 20 31 35 32 0a 23 64 65 66          152.#def
48cc0 69 6e 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  ine TK_AGG_FUNCT
48cd0 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  ION             
48ce0 20 20 20 20 20 20 31 35 33 0a 23 64 65 66 69 6e        153.#defin
48cf0 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20  e TK_AGG_COLUMN 
48d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48d10 20 20 20 20 31 35 34 0a 23 64 65 66 69 6e 65 20      154.#define 
48d20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 20 20  TK_CONST_FUNC   
48d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48d40 20 20 31 35 35 0a 23 64 65 66 69 6e 65 20 54 4b    155.#define TK
48d50 5f 55 4d 49 4e 55 53 20 20 20 20 20 20 20 20 20  _UMINUS         
48d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48d70 31 35 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55  156.#define TK_U
48d80 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20 20  PLUS            
48d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35                15
48da0 37 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  7../************
48db0 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e  ** End of parse.
48dc0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  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 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
48e00 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
48e10 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
48e20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
48e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48e40 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64  */.#include <std
48e50 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  io.h>.#include <
48e60 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75  stdlib.h>.#inclu
48e70 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69  de <string.h>.#i
48e80 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68  nclude <assert.h
48e90 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 64  >.#include <stdd
48ea0 65 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  ef.h>../*.** If 
48eb0 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20  compiling for a 
48ec0 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c  processor that l
48ed0 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f  acks floating po
48ee0 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20  int support,.** 
48ef0 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67  substitute integ
48f00 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d  er for floating-
48f10 70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64 65 66 20  point.*/.#ifdef 
48f20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
48f30 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66  TING_POINT.# def
48f40 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  ine double sqlit
48f50 65 5f 69 6e 74 36 34 0a 23 20 64 65 66 69 6e 65  e_int64.# define
48f60 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45   LONGDOUBLE_TYPE
48f70 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20   sqlite_int64.# 
48f80 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49  ifndef SQLITE_BI
48f90 47 5f 44 42 4c 0a 23 20 20 20 64 65 66 69 6e 65  G_DBL.#   define
48fa0 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20   SQLITE_BIG_DBL 
48fb0 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  (((sqlite3_int64
48fc0 29 31 29 3c 3c 35 30 29 0a 23 20 65 6e 64 69 66  )1)<<50).# endif
48fd0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
48fe0 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46  _OMIT_DATETIME_F
48ff0 55 4e 43 53 20 31 0a 23 20 64 65 66 69 6e 65 20  UNCS 1.# define 
49000 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
49010 45 20 31 0a 23 20 75 6e 64 65 66 20 53 51 4c 49  E 1.# undef SQLI
49020 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f  TE_MIXED_ENDIAN_
49030 36 34 42 49 54 5f 46 4c 4f 41 54 0a 23 20 75 6e  64BIT_FLOAT.# un
49040 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56 45 5f  def SQLITE_HAVE_
49050 49 53 4e 41 4e 0a 23 65 6e 64 69 66 0a 23 69 66  ISNAN.#endif.#if
49060 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47 5f  ndef SQLITE_BIG_
49070 44 42 4c 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  DBL.# define SQL
49080 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 31 65 39  ITE_BIG_DBL (1e9
49090 39 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  9).#endif../*.**
490a0 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 73 20   OMIT_TEMPDB is 
490b0 73 65 74 20 74 6f 20 31 20 69 66 20 53 51 4c 49  set to 1 if SQLI
490c0 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69  TE_OMIT_TEMPDB i
490d0 73 20 64 65 66 69 6e 65 64 2c 20 6f 72 20 30 0a  s defined, or 0.
490e0 2a 2a 20 61 66 74 65 72 77 61 72 64 2e 20 48 61  ** afterward. Ha
490f0 76 69 6e 67 20 74 68 69 73 20 6d 61 63 72 6f 20  ving this macro 
49100 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 63 61 75  allows us to cau
49110 73 65 20 74 68 65 20 43 20 63 6f 6d 70 69 6c 65  se the C compile
49120 72 20 0a 2a 2a 20 74 6f 20 6f 6d 69 74 20 63 6f  r .** to omit co
49130 64 65 20 75 73 65 64 20 62 79 20 54 45 4d 50 20  de used by TEMP 
49140 74 61 62 6c 65 73 20 77 69 74 68 6f 75 74 20 6d  tables without m
49150 65 73 73 79 20 23 69 66 6e 64 65 66 20 73 74 61  essy #ifndef sta
49160 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 69 66 64  tements..*/.#ifd
49170 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
49180 45 4d 50 44 42 0a 23 64 65 66 69 6e 65 20 4f 4d  EMPDB.#define OM
49190 49 54 5f 54 45 4d 50 44 42 20 31 0a 23 65 6c 73  IT_TEMPDB 1.#els
491a0 65 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54  e.#define OMIT_T
491b0 45 4d 50 44 42 20 30 0a 23 65 6e 64 69 66 0a 0a  EMPDB 0.#endif..
491c0 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c  /*.** If the fol
491d0 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20  lowing macro is 
491e0 73 65 74 20 74 6f 20 31 2c 20 74 68 65 6e 20 4e  set to 1, then N
491f0 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63  ULL values are c
49200 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 64 69 73  onsidered.** dis
49210 74 69 6e 63 74 20 77 68 65 6e 20 64 65 74 65 72  tinct when deter
49220 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 6f  mining whether o
49230 72 20 6e 6f 74 20 74 77 6f 20 65 6e 74 72 69 65  r not two entrie
49240 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 0a 2a  s are the same.*
49250 2a 20 69 6e 20 61 20 55 4e 49 51 55 45 20 69 6e  * in a UNIQUE in
49260 64 65 78 2e 20 20 54 68 69 73 20 69 73 20 74 68  dex.  This is th
49270 65 20 77 61 79 20 50 6f 73 74 67 72 65 53 51 4c  e way PostgreSQL
49280 2c 20 4f 72 61 63 6c 65 2c 20 44 42 32 2c 20 4d  , Oracle, DB2, M
49290 79 53 51 4c 2c 0a 2a 2a 20 4f 43 45 4c 4f 54 2c  ySQL,.** OCELOT,
492a0 20 61 6e 64 20 46 69 72 65 62 69 72 64 20 61 6c   and Firebird al
492b0 6c 20 77 6f 72 6b 2e 20 20 54 68 65 20 53 51 4c  l work.  The SQL
492c0 39 32 20 73 70 65 63 20 65 78 70 6c 69 63 69 74  92 spec explicit
492d0 6c 79 20 73 61 79 73 20 74 68 69 73 0a 2a 2a 20  ly says this.** 
492e0 69 73 20 74 68 65 20 77 61 79 20 74 68 69 6e 67  is the way thing
492f0 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f  s are suppose to
49300 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   work..**.** If 
49310 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
49320 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 30 2c  cro is set to 0,
49330 20 74 68 65 20 4e 55 4c 4c 73 20 61 72 65 20 69   the NULLs are i
49340 6e 64 69 73 74 69 6e 63 74 20 66 6f 72 0a 2a 2a  ndistinct for.**
49350 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e   a UNIQUE index.
49360 20 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c 20    In this mode, 
49370 79 6f 75 20 63 61 6e 20 6f 6e 6c 79 20 68 61 76  you can only hav
49380 65 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 20  e a single NULL 
49390 65 6e 74 72 79 0a 2a 2a 20 66 6f 72 20 61 20 63  entry.** for a c
493a0 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64 20 55  olumn declared U
493b0 4e 49 51 55 45 2e 20 20 54 68 69 73 20 69 73 20  NIQUE.  This is 
493c0 74 68 65 20 77 61 79 20 49 6e 66 6f 72 6d 69 78  the way Informix
493d0 20 61 6e 64 20 53 51 4c 20 53 65 72 76 65 72 0a   and SQL Server.
493e0 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 64 65 66  ** work..*/.#def
493f0 69 6e 65 20 4e 55 4c 4c 5f 44 49 53 54 49 4e 43  ine NULL_DISTINC
49400 54 5f 46 4f 52 5f 55 4e 49 51 55 45 20 31 0a 0a  T_FOR_UNIQUE 1..
49410 2f 2a 0a 2a 2a 20 54 68 65 20 22 66 69 6c 65 20  /*.** The "file 
49420 66 6f 72 6d 61 74 22 20 6e 75 6d 62 65 72 20 69  format" number i
49430 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  s an integer tha
49440 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  t is incremented
49450 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 74 68 65   whenever.** the
49460 20 56 44 42 45 2d 6c 65 76 65 6c 20 66 69 6c 65   VDBE-level file
49470 20 66 6f 72 6d 61 74 20 63 68 61 6e 67 65 73 2e   format changes.
49480 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
49490 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 68  macros define th
494a0 65 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  e.** the default
494b0 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72   file format for
494c0 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 61   new databases a
494d0 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 66  nd the maximum f
494e0 69 6c 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 74 68  ile format.** th
494f0 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 63  at the library c
49500 61 6e 20 72 65 61 64 2e 0a 2a 2f 0a 23 64 65 66  an read..*/.#def
49510 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46  ine SQLITE_MAX_F
49520 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0a 23 69 66  ILE_FORMAT 4.#if
49530 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
49540 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 0a  ULT_FILE_FORMAT.
49550 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
49560 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52  DEFAULT_FILE_FOR
49570 4d 41 54 20 31 0a 23 65 6e 64 69 66 0a 0a 23 69  MAT 1.#endif..#i
49580 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  fndef SQLITE_DEF
49590 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 5f 54  AULT_RECURSIVE_T
495a0 52 49 47 47 45 52 53 0a 23 20 64 65 66 69 6e 65  RIGGERS.# define
495b0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
495c0 52 45 43 55 52 53 49 56 45 5f 54 52 49 47 47 45  RECURSIVE_TRIGGE
495d0 52 53 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  RS 0.#endif../*.
495e0 2a 2a 20 50 72 6f 76 69 64 65 20 61 20 64 65 66  ** Provide a def
495f0 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 53  ault value for S
49600 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45  QLITE_TEMP_STORE
49610 20 69 6e 20 63 61 73 65 20 69 74 20 69 73 20 6e   in case it is n
49620 6f 74 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  ot specified.** 
49630 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d 6c  on the command-l
49640 69 6e 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ine.*/.#ifndef S
49650 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45  QLITE_TEMP_STORE
49660 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
49670 5f 54 45 4d 50 5f 53 54 4f 52 45 20 31 0a 23 65  _TEMP_STORE 1.#e
49680 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43 20  ndif../*.** GCC 
49690 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20  does not define 
496a0 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29 20 6d  the offsetof() m
496b0 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20 68 61  acro so we'll ha
496c0 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a 2a 20 6f  ve to do it.** o
496d0 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69 66  urselves..*/.#if
496e0 6e 64 65 66 20 6f 66 66 73 65 74 6f 66 0a 23 64  ndef offsetof.#d
496f0 65 66 69 6e 65 20 6f 66 66 73 65 74 6f 66 28 53  efine offsetof(S
49700 54 52 55 43 54 55 52 45 2c 46 49 45 4c 44 29 20  TRUCTURE,FIELD) 
49710 28 28 69 6e 74 29 28 28 63 68 61 72 2a 29 26 28  ((int)((char*)&(
49720 28 53 54 52 55 43 54 55 52 45 2a 29 30 29 2d 3e  (STRUCTURE*)0)->
49730 46 49 45 4c 44 29 29 0a 23 65 6e 64 69 66 0a 0a  FIELD)).#endif..
49740 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
49750 65 65 20 69 66 20 74 68 69 73 20 6d 61 63 68 69  ee if this machi
49760 6e 65 20 75 73 65 73 20 45 42 43 44 49 43 2e 20  ne uses EBCDIC. 
49770 20 28 59 65 73 2c 20 62 65 6c 69 65 76 65 20 69   (Yes, believe i
49780 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68 65  t or.** not, the
49790 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 63  re are still mac
497a0 68 69 6e 65 73 20 6f 75 74 20 74 68 65 72 65 20  hines out there 
497b0 74 68 61 74 20 75 73 65 20 45 42 43 44 49 43 2e  that use EBCDIC.
497c0 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20 3d 3d 20  ).*/.#if 'A' == 
497d0 27 5c 33 30 31 27 0a 23 20 64 65 66 69 6e 65 20  '\301'.# define 
497e0 53 51 4c 49 54 45 5f 45 42 43 44 49 43 20 31 0a  SQLITE_EBCDIC 1.
497f0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
49800 51 4c 49 54 45 5f 41 53 43 49 49 20 31 0a 23 65  QLITE_ASCII 1.#e
49810 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  ndif../*.** Inte
49820 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20 73 69  gers of known si
49830 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79 70 65  zes.  These type
49840 64 65 66 73 20 6d 69 67 68 74 20 63 68 61 6e 67  defs might chang
49850 65 20 66 6f 72 20 61 72 63 68 69 74 65 63 74 75  e for architectu
49860 72 65 73 0a 2a 2a 20 77 68 65 72 65 20 74 68 65  res.** where the
49870 20 73 69 7a 65 73 20 76 65 72 79 2e 20 20 50 72   sizes very.  Pr
49880 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f  eprocessor macro
49890 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20  s are available 
498a0 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74  so that the.** t
498b0 79 70 65 73 20 63 61 6e 20 62 65 20 63 6f 6e 76  ypes can be conv
498c0 65 6e 69 65 6e 74 6c 79 20 72 65 64 65 66 69 6e  eniently redefin
498d0 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 79  ed at compile-ty
498e0 70 65 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a  pe.  Like this:.
498f0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 63  **.**         cc
49900 20 27 2d 44 55 49 4e 54 50 54 52 5f 54 59 50 45   '-DUINTPTR_TYPE
49910 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 27 20  =long long int' 
49920 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 55  ....*/.#ifndef U
49930 49 4e 54 33 32 5f 54 59 50 45 0a 23 20 69 66 64  INT32_TYPE.# ifd
49940 65 66 20 48 41 56 45 5f 55 49 4e 54 33 32 5f 54  ef HAVE_UINT32_T
49950 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 33  .#  define UINT3
49960 32 5f 54 59 50 45 20 75 69 6e 74 33 32 5f 74 0a  2_TYPE uint32_t.
49970 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
49980 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 6e 73   UINT32_TYPE uns
49990 69 67 6e 65 64 20 69 6e 74 0a 23 20 65 6e 64 69  igned int.# endi
499a0 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  f.#endif.#ifndef
499b0 20 55 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69   UINT16_TYPE.# i
499c0 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 31 36  fdef HAVE_UINT16
499d0 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e  _T.#  define UIN
499e0 54 31 36 5f 54 59 50 45 20 75 69 6e 74 31 36 5f  T16_TYPE uint16_
499f0 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69  t.# else.#  defi
49a00 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ne UINT16_TYPE u
49a10 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e  nsigned short in
49a20 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  t.# endif.#endif
49a30 0a 23 69 66 6e 64 65 66 20 49 4e 54 31 36 5f 54  .#ifndef INT16_T
49a40 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
49a50 5f 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69  _INT16_T.#  defi
49a60 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 69 6e  ne INT16_TYPE in
49a70 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20  t16_t.# else.#  
49a80 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 50  define INT16_TYP
49a90 45 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e  E short int.# en
49aa0 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  dif.#endif.#ifnd
49ab0 65 66 20 55 49 4e 54 38 5f 54 59 50 45 0a 23 20  ef UINT8_TYPE.# 
49ac0 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 38  ifdef HAVE_UINT8
49ad0 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e  _T.#  define UIN
49ae0 54 38 5f 54 59 50 45 20 75 69 6e 74 38 5f 74 0a  T8_TYPE uint8_t.
49af0 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
49b00 20 55 49 4e 54 38 5f 54 59 50 45 20 75 6e 73 69   UINT8_TYPE unsi
49b10 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69  gned char.# endi
49b20 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  f.#endif.#ifndef
49b30 20 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 64   INT8_TYPE.# ifd
49b40 65 66 20 48 41 56 45 5f 49 4e 54 38 5f 54 0a 23  ef HAVE_INT8_T.#
49b50 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59    define INT8_TY
49b60 50 45 20 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65  PE int8_t.# else
49b70 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f  .#  define INT8_
49b80 54 59 50 45 20 73 69 67 6e 65 64 20 63 68 61 72  TYPE signed char
49b90 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
49ba0 23 69 66 6e 64 65 66 20 4c 4f 4e 47 44 4f 55 42  #ifndef LONGDOUB
49bb0 4c 45 5f 54 59 50 45 0a 23 20 64 65 66 69 6e 65  LE_TYPE.# define
49bc0 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45   LONGDOUBLE_TYPE
49bd0 20 6c 6f 6e 67 20 64 6f 75 62 6c 65 0a 23 65 6e   long double.#en
49be0 64 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c 69  dif.typedef sqli
49bf0 74 65 5f 69 6e 74 36 34 20 69 36 34 3b 20 20 20  te_int64 i64;   
49c00 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65         /* 8-byte
49c10 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
49c20 2a 2f 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74  */.typedef sqlit
49c30 65 5f 75 69 6e 74 36 34 20 75 36 34 3b 20 20 20  e_uint64 u64;   
49c40 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20        /* 8-byte 
49c50 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
49c60 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54   */.typedef UINT
49c70 33 32 5f 54 59 50 45 20 75 33 32 3b 20 20 20 20  32_TYPE u32;    
49c80 20 20 20 20 20 20 20 2f 2a 20 34 2d 62 79 74 65         /* 4-byte
49c90 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
49ca0 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e  r */.typedef UIN
49cb0 54 31 36 5f 54 59 50 45 20 75 31 36 3b 20 20 20  T16_TYPE u16;   
49cc0 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 74          /* 2-byt
49cd0 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  e unsigned integ
49ce0 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e  er */.typedef IN
49cf0 54 31 36 5f 54 59 50 45 20 69 31 36 3b 20 20 20  T16_TYPE i16;   
49d00 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79           /* 2-by
49d10 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
49d20 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e  r */.typedef UIN
49d30 54 38 5f 54 59 50 45 20 75 38 3b 20 20 20 20 20  T8_TYPE u8;     
49d40 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 74          /* 1-byt
49d50 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  e unsigned integ
49d60 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e  er */.typedef IN
49d70 54 38 5f 54 59 50 45 20 69 38 3b 20 20 20 20 20  T8_TYPE i8;     
49d80 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79           /* 1-by
49d90 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
49da0 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 49  r */../*.** SQLI
49db0 54 45 5f 4d 41 58 5f 55 33 32 20 69 73 20 61 20  TE_MAX_U32 is a 
49dc0 75 36 34 20 63 6f 6e 73 74 61 6e 74 20 74 68 61  u64 constant tha
49dd0 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  t is the maximum
49de0 20 75 36 34 20 76 61 6c 75 65 0a 2a 2a 20 74 68   u64 value.** th
49df0 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64  at can be stored
49e00 20 69 6e 20 61 20 75 33 32 20 77 69 74 68 6f 75   in a u32 withou
49e10 74 20 6c 6f 73 73 20 6f 66 20 64 61 74 61 2e 20  t loss of data. 
49e20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73   The value.** is
49e30 20 30 78 30 30 30 30 30 30 30 30 66 66 66 66 66   0x00000000fffff
49e40 66 66 66 2e 20 20 42 75 74 20 62 65 63 61 75 73  fff.  But becaus
49e50 65 20 6f 66 20 71 75 69 72 6b 73 20 6f 66 20 73  e of quirks of s
49e60 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 77  ome compilers, w
49e70 65 0a 2a 2a 20 68 61 76 65 20 74 6f 20 73 70 65  e.** have to spe
49e80 63 69 66 79 20 74 68 65 20 76 61 6c 75 65 20 69  cify the value i
49e90 6e 20 74 68 65 20 6c 65 73 73 20 69 6e 74 75 69  n the less intui
49ea0 74 69 76 65 20 6d 61 6e 6e 65 72 20 73 68 6f 77  tive manner show
49eb0 6e 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  n:.*/.#define SQ
49ec0 4c 49 54 45 5f 4d 41 58 5f 55 33 32 20 20 28 28  LITE_MAX_U32  ((
49ed0 28 28 75 36 34 29 31 29 3c 3c 33 32 29 2d 31 29  ((u64)1)<<32)-1)
49ee0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74  ../*.** Macros t
49ef0 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74  o determine whet
49f00 68 65 72 20 74 68 65 20 6d 61 63 68 69 6e 65 20  her the machine 
49f10 69 73 20 62 69 67 20 6f 72 20 6c 69 74 74 6c 65  is big or little
49f20 20 65 6e 64 69 61 6e 2c 0a 2a 2a 20 65 76 61 6c   endian,.** eval
49f30 75 61 74 65 64 20 61 74 20 72 75 6e 74 69 6d 65  uated at runtime
49f40 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
49f50 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a  TE_AMALGAMATION.
49f60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
49f70 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33  onst int sqlite3
49f80 6f 6e 65 20 3d 20 31 3b 0a 23 65 6c 73 65 0a 53  one = 1;.#else.S
49f90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
49fa0 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33 6f  nst int sqlite3o
49fb0 6e 65 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64  ne;.#endif.#if d
49fc0 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20  efined(i386) || 
49fd0 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f  defined(__i386__
49fe0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f  ) || defined(_M_
49ff0 49 58 38 36 29 5c 0a 20 20 20 20 20 20 20 20 20  IX86)\.         
4a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a010 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f      || defined(_
4a020 5f 78 38 36 5f 36 34 29 20 7c 7c 20 64 65 66 69  _x86_64) || defi
4a030 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 0a  ned(__x86_64__).
4a040 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
4a050 42 49 47 45 4e 44 49 41 4e 20 20 20 20 30 0a 23  BIGENDIAN    0.#
4a060 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c   define SQLITE_L
4a070 49 54 54 4c 45 45 4e 44 49 41 4e 20 31 0a 23 20  ITTLEENDIAN 1.# 
4a080 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54  define SQLITE_UT
4a090 46 31 36 4e 41 54 49 56 45 20 20 53 51 4c 49 54  F16NATIVE  SQLIT
4a0a0 45 5f 55 54 46 31 36 4c 45 0a 23 65 6c 73 65 0a  E_UTF16LE.#else.
4a0b0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
4a0c0 42 49 47 45 4e 44 49 41 4e 20 20 20 20 28 2a 28  BIGENDIAN    (*(
4a0d0 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33  char *)(&sqlite3
4a0e0 6f 6e 65 29 3d 3d 30 29 0a 23 20 64 65 66 69 6e  one)==0).# defin
4a0f0 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45  e SQLITE_LITTLEE
4a100 4e 44 49 41 4e 20 28 2a 28 63 68 61 72 20 2a 29  NDIAN (*(char *)
4a110 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 31  (&sqlite3one)==1
4a120 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ).# define SQLIT
4a130 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 28 53  E_UTF16NATIVE (S
4a140 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 3f  QLITE_BIGENDIAN?
4a150 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3a 53  SQLITE_UTF16BE:S
4a160 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 0a 23  QLITE_UTF16LE).#
4a170 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  endif../*.** Con
4a180 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 20 6c  stants for the l
4a190 61 72 67 65 73 74 20 61 6e 64 20 73 6d 61 6c 6c  argest and small
4a1a0 65 73 74 20 70 6f 73 73 69 62 6c 65 20 36 34 2d  est possible 64-
4a1b0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
4a1c0 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 6d 61  ers..** These ma
4a1d0 63 72 6f 73 20 61 72 65 20 64 65 73 69 67 6e 65  cros are designe
4a1e0 64 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63  d to work correc
4a1f0 74 6c 79 20 6f 6e 20 62 6f 74 68 20 33 32 2d 62  tly on both 32-b
4a200 69 74 20 61 6e 64 20 36 34 2d 62 69 74 0a 2a 2a  it and 64-bit.**
4a210 20 63 6f 6d 70 69 6c 65 72 73 2e 0a 2a 2f 0a 23   compilers..*/.#
4a220 64 65 66 69 6e 65 20 4c 41 52 47 45 53 54 5f 49  define LARGEST_I
4a230 4e 54 36 34 20 20 28 30 78 66 66 66 66 66 66 66  NT64  (0xfffffff
4a240 66 7c 28 28 28 69 36 34 29 30 78 37 66 66 66 66  f|(((i64)0x7ffff
4a250 66 66 66 29 3c 3c 33 32 29 29 0a 23 64 65 66 69  fff)<<32)).#defi
4a260 6e 65 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36  ne SMALLEST_INT6
4a270 34 20 28 28 28 69 36 34 29 2d 31 29 20 2d 20 4c  4 (((i64)-1) - L
4a280 41 52 47 45 53 54 5f 49 4e 54 36 34 29 0a 0a 2f  ARGEST_INT64)../
4a290 2a 20 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61  * .** Round up a
4a2a0 20 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20 6e   number to the n
4a2b0 65 78 74 20 6c 61 72 67 65 72 20 6d 75 6c 74 69  ext larger multi
4a2c0 70 6c 65 20 6f 66 20 38 2e 20 20 54 68 69 73 20  ple of 8.  This 
4a2d0 69 73 20 75 73 65 64 0a 2a 2a 20 74 6f 20 66 6f  is used.** to fo
4a2e0 72 63 65 20 38 2d 62 79 74 65 20 61 6c 69 67 6e  rce 8-byte align
4a2f0 6d 65 6e 74 20 6f 6e 20 36 34 2d 62 69 74 20 61  ment on 64-bit a
4a300 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a 2f  rchitectures..*/
4a310 0a 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 38 28  .#define ROUND8(
4a320 78 29 20 20 20 20 20 28 28 28 78 29 2b 37 29 26  x)     (((x)+7)&
4a330 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64  ~7)../*.** Round
4a340 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 61   down to the nea
4a350 72 65 73 74 20 6d 75 6c 74 69 70 6c 65 20 6f 66  rest multiple of
4a360 20 38 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f   8.*/.#define RO
4a370 55 4e 44 44 4f 57 4e 38 28 78 29 20 28 28 78 29  UNDDOWN8(x) ((x)
4a380 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 65  &~7)../*.** Asse
4a390 72 74 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e  rt that the poin
4a3a0 74 65 72 20 58 20 69 73 20 61 6c 69 67 6e 65 64  ter X is aligned
4a3b0 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 62 6f   to an 8-byte bo
4a3c0 75 6e 64 61 72 79 2e 0a 2a 2f 0a 23 64 65 66 69  undary..*/.#defi
4a3d0 6e 65 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c  ne EIGHT_BYTE_AL
4a3e0 49 47 4e 4d 45 4e 54 28 58 29 20 20 20 28 28 28  IGNMENT(X)   (((
4a3f0 28 63 68 61 72 2a 29 28 58 29 20 2d 20 28 63 68  (char*)(X) - (ch
4a400 61 72 2a 29 30 29 26 37 29 3d 3d 30 29 0a 0a 0a  ar*)0)&7)==0)...
4a410 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
4a420 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
4a430 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ng structure is 
4a440 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  used to store th
4a450 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a  e busy-handler.*
4a460 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  * callback for a
4a470 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68 61   given sqlite ha
4a480 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ndle. .**.** The
4a490 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64   sqlite.busyHand
4a4a0 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 68  ler member of th
4a4b0 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74 20  e sqlite struct 
4a4c0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75 73  contains the bus
4a4d0 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f  y.** callback fo
4a4e0 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  r the database h
4a4f0 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67 65  andle. Each page
4a500 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 65  r opened via the
4a510 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c   sqlite.** handl
4a520 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70 6f  e is passed a po
4a530 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 2e  inter to sqlite.
4a540 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 65  busyHandler. The
4a550 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a   busy-handler.**
4a560 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 72   callback is cur
4a570 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 6f  rently invoked o
4a580 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  nly from within 
4a590 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65  pager.c..*/.type
4a5a0 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79 48  def struct BusyH
4a5b0 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64 6c  andler BusyHandl
4a5c0 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79 48  er;.struct BusyH
4a5d0 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 28  andler {.  int (
4a5e0 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c 69  *xFunc)(void *,i
4a5f0 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 73  nt);  /* The bus
4a600 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  y callback */.  
4a610 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20 20  void *pArg;     
4a620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
4a630 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79 20  rst arg to busy 
4a640 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e  callback */.  in
4a650 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 20  t nBusy;        
4a660 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72           /* Incr
4a670 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63  emented with eac
4a680 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d  h busy call */.}
4a690 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66  ;../*.** Name of
4a6a0 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61   the master data
4a6b0 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68 65  base table.  The
4a6c0 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65   master database
4a6d0 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 73   table.** is a s
4a6e0 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68 61  pecial table tha
4a6f0 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65  t holds the name
4a700 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65 73  s and attributes
4a710 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72 20   of all.** user 
4a720 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63  tables and indic
4a730 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  es..*/.#define M
4a740 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20 20  ASTER_NAME      
4a750 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22   "sqlite_master"
4a760 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d 41  .#define TEMP_MA
4a770 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c 69  STER_NAME  "sqli
4a780 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 0a  te_temp_master".
4a790 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 2d  ./*.** The root-
4a7a0 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73 74  page of the mast
4a7b0 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  er database tabl
4a7c0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41  e..*/.#define MA
4a7d0 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20 20  STER_ROOT       
4a7e0 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d  1../*.** The nam
4a7f0 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20  e of the schema 
4a800 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  table..*/.#defin
4a810 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 78  e SCHEMA_TABLE(x
4a820 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50 44  )  ((!OMIT_TEMPD
4a830 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50 5f  B)&&(x==1)?TEMP_
4a840 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53 54  MASTER_NAME:MAST
4a850 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20  ER_NAME)../*.** 
4a860 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d 61  A convenience ma
4a870 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e 73  cro that returns
4a880 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
4a890 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e  lements in.** an
4a8a0 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66 69   array..*/.#defi
4a8b0 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29 20  ne ArraySize(X) 
4a8c0 20 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66     ((int)(sizeof
4a8d0 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d 29  (X)/sizeof(X[0])
4a8e0 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  ))../*.** The fo
4a8f0 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61 73  llowing value as
4a900 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d 65   a destructor me
4a910 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69 74  ans to use sqlit
4a920 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2a 20 54  e3DbFree()..** T
4a930 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  his is an intern
4a940 61 6c 20 65 78 74 65 6e 73 69 6f 6e 20 74 6f 20  al extension to 
4a950 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 61 6e  SQLITE_STATIC an
4a960 64 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  d SQLITE_TRANSIE
4a970 4e 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  NT..*/.#define S
4a980 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 20 20  QLITE_DYNAMIC   
4a990 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75  ((sqlite3_destru
4a9a0 63 74 6f 72 5f 74 79 70 65 29 73 71 6c 69 74 65  ctor_type)sqlite
4a9b0 33 44 62 46 72 65 65 29 0a 0a 2f 2a 0a 2a 2a 20  3DbFree)../*.** 
4a9c0 57 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4d 49 54  When SQLITE_OMIT
4a9d0 5f 57 53 44 20 69 73 20 64 65 66 69 6e 65 64 2c  _WSD is defined,
4a9e0 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
4a9f0 68 65 20 74 61 72 67 65 74 20 70 6c 61 74 66 6f  he target platfo
4aa00 72 6d 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 73  rm does.** not s
4aa10 75 70 70 6f 72 74 20 57 72 69 74 61 62 6c 65 20  upport Writable 
4aa20 53 74 61 74 69 63 20 44 61 74 61 20 28 57 53 44  Static Data (WSD
4aa30 29 20 73 75 63 68 20 61 73 20 67 6c 6f 62 61 6c  ) such as global
4aa40 20 61 6e 64 20 73 74 61 74 69 63 20 76 61 72 69   and static vari
4aa50 61 62 6c 65 73 2e 0a 2a 2a 20 41 6c 6c 20 76 61  ables..** All va
4aa60 72 69 61 62 6c 65 73 20 6d 75 73 74 20 65 69 74  riables must eit
4aa70 68 65 72 20 62 65 20 6f 6e 20 74 68 65 20 73 74  her be on the st
4aa80 61 63 6b 20 6f 72 20 64 79 6e 61 6d 69 63 61 6c  ack or dynamical
4aa90 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 6f  ly allocated fro
4aaa0 6d 0a 2a 2a 20 74 68 65 20 68 65 61 70 2e 20 20  m.** the heap.  
4aab0 57 68 65 6e 20 57 53 44 20 69 73 20 75 6e 73 75  When WSD is unsu
4aac0 70 70 6f 72 74 65 64 2c 20 74 68 65 20 76 61 72  pported, the var
4aad0 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f  iable declaratio
4aae0 6e 73 20 73 63 61 74 74 65 72 65 64 0a 2a 2a 20  ns scattered.** 
4aaf0 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20 53  throughout the S
4ab00 51 4c 69 74 65 20 63 6f 64 65 20 6d 75 73 74 20  QLite code must 
4ab10 62 65 63 6f 6d 65 20 63 6f 6e 73 74 61 6e 74 73  become constants
4ab20 20 69 6e 73 74 65 61 64 2e 20 20 54 68 65 20 53   instead.  The S
4ab30 51 4c 49 54 45 5f 57 53 44 0a 2a 2a 20 6d 61 63  QLITE_WSD.** mac
4ab40 72 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  ro is used for t
4ab50 68 69 73 20 70 75 72 70 6f 73 65 2e 20 20 41 6e  his purpose.  An
4ab60 64 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 66  d instead of ref
4ab70 65 72 65 6e 63 69 6e 67 20 74 68 65 20 76 61 72  erencing the var
4ab80 69 61 62 6c 65 0a 2a 2a 20 64 69 72 65 63 74 6c  iable.** directl
4ab90 79 2c 20 77 65 20 75 73 65 20 69 74 73 20 63 6f  y, we use its co
4aba0 6e 73 74 61 6e 74 20 61 73 20 61 20 6b 65 79 20  nstant as a key 
4abb0 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 72 75  to lookup the ru
4abc0 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64  n-time allocated
4abd0 0a 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74 20  .** buffer that 
4abe0 68 6f 6c 64 73 20 72 65 61 6c 20 76 61 72 69 61  holds real varia
4abf0 62 6c 65 2e 20 20 54 68 65 20 63 6f 6e 73 74 61  ble.  The consta
4ac00 6e 74 20 69 73 20 61 6c 73 6f 20 74 68 65 20 69  nt is also the i
4ac10 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a 20 66 6f  nitializer.** fo
4ac20 72 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61  r the run-time a
4ac30 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 2e  llocated buffer.
4ac40 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 75 73  .**.** In the us
4ac50 75 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 57  ual case where W
4ac60 53 44 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c  SD is supported,
4ac70 20 74 68 65 20 53 51 4c 49 54 45 5f 57 53 44 20   the SQLITE_WSD 
4ac80 61 6e 64 20 47 4c 4f 42 41 4c 0a 2a 2a 20 6d 61  and GLOBAL.** ma
4ac90 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f  cros become no-o
4aca0 70 73 20 61 6e 64 20 68 61 76 65 20 7a 65 72 6f  ps and have zero
4acb0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70   performance imp
4acc0 61 63 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  act..*/.#ifdef S
4acd0 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20  QLITE_OMIT_WSD. 
4ace0 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f   #define SQLITE_
4acf0 57 53 44 20 63 6f 6e 73 74 0a 20 20 23 64 65 66  WSD const.  #def
4ad00 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20  ine GLOBAL(t,v) 
4ad10 28 2a 28 74 2a 29 73 71 6c 69 74 65 33 5f 77 73  (*(t*)sqlite3_ws
4ad20 64 5f 66 69 6e 64 28 28 76 6f 69 64 2a 29 26 28  d_find((void*)&(
4ad30 76 29 2c 20 73 69 7a 65 6f 66 28 76 29 29 29 0a  v), sizeof(v))).
4ad40 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
4ad50 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 47 4c  3GlobalConfig GL
4ad60 4f 42 41 4c 28 73 74 72 75 63 74 20 53 71 6c 69  OBAL(struct Sqli
4ad70 74 65 33 43 6f 6e 66 69 67 2c 20 73 71 6c 69 74  te3Config, sqlit
4ad80 65 33 43 6f 6e 66 69 67 29 0a 53 51 4c 49 54 45  e3Config).SQLITE
4ad90 5f 41 50 49 20 20 20 69 6e 74 20 73 71 6c 69 74  _API   int sqlit
4ada0 65 33 5f 77 73 64 5f 69 6e 69 74 28 69 6e 74 20  e3_wsd_init(int 
4adb0 4e 2c 20 69 6e 74 20 4a 29 3b 0a 53 51 4c 49 54  N, int J);.SQLIT
4adc0 45 5f 41 50 49 20 20 20 76 6f 69 64 20 2a 73 71  E_API   void *sq
4add0 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 76  lite3_wsd_find(v
4ade0 6f 69 64 20 2a 4b 2c 20 69 6e 74 20 4c 29 3b 0a  oid *K, int L);.
4adf0 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
4ae00 53 51 4c 49 54 45 5f 57 53 44 20 0a 20 20 23 64  SQLITE_WSD .  #d
4ae10 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76  efine GLOBAL(t,v
4ae20 29 20 76 0a 20 20 23 64 65 66 69 6e 65 20 73 71  ) v.  #define sq
4ae30 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
4ae40 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 0a  g sqlite3Config.
4ae50 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
4ae60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
4ae70 6f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 73  os are used to s
4ae80 75 70 70 72 65 73 73 20 63 6f 6d 70 69 6c 65 72  uppress compiler
4ae90 20 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 74 6f   warnings and to
4aea0 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63 6c 65 61  .** make it clea
4aeb0 72 20 74 6f 20 68 75 6d 61 6e 20 72 65 61 64 65  r to human reade
4aec0 72 73 20 77 68 65 6e 20 61 20 66 75 6e 63 74 69  rs when a functi
4aed0 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  on parameter is 
4aee0 64 65 6c 69 62 65 72 61 74 65 6c 79 20 0a 2a 2a  deliberately .**
4aef0 20 6c 65 66 74 20 75 6e 75 73 65 64 20 77 69 74   left unused wit
4af00 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20  hin the body of 
4af10 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73  a function. This
4af20 20 75 73 75 61 6c 6c 79 20 68 61 70 70 65 6e 73   usually happens
4af30 20 77 68 65 6e 0a 2a 2a 20 61 20 66 75 6e 63 74   when.** a funct
4af40 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 76 69  ion is called vi
4af50 61 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  a a function poi
4af60 6e 74 65 72 2e 20 46 6f 72 20 65 78 61 6d 70 6c  nter. For exampl
4af70 65 20 74 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d  e the .** implem
4af80 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53  entation of an S
4af90 51 4c 20 61 67 67 72 65 67 61 74 65 20 73 74 65  QL aggregate ste
4afa0 70 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 6e  p callback may n
4afb0 6f 74 20 75 73 65 20 74 68 65 0a 2a 2a 20 70 61  ot use the.** pa
4afc0 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 69  rameter indicati
4afd0 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ng the number of
4afe0 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65   arguments passe
4aff0 64 20 74 6f 20 74 68 65 20 61 67 67 72 65 67 61  d to the aggrega
4b000 74 65 2c 0a 2a 2a 20 69 66 20 69 74 20 6b 6e 6f  te,.** if it kno
4b010 77 73 20 74 68 61 74 20 74 68 69 73 20 69 73 20  ws that this is 
4b020 65 6e 66 6f 72 63 65 64 20 65 6c 73 65 77 68 65  enforced elsewhe
4b030 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  re..**.** When a
4b040 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65   function parame
4b050 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 20  ter is not used 
4b060 61 74 20 61 6c 6c 20 77 69 74 68 69 6e 20 74 68  at all within th
4b070 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e 63  e body of a func
4b080 74 69 6f 6e 2c 0a 2a 2a 20 69 74 20 69 73 20 67  tion,.** it is g
4b090 65 6e 65 72 61 6c 6c 79 20 6e 61 6d 65 64 20 22  enerally named "
4b0a0 4e 6f 74 55 73 65 64 22 20 6f 72 20 22 4e 6f 74  NotUsed" or "Not
4b0b0 55 73 65 64 32 22 20 74 6f 20 6d 61 6b 65 20 74  Used2" to make t
4b0c0 68 69 6e 67 73 20 65 76 65 6e 20 63 6c 65 61 72  hings even clear
4b0d0 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20  er..** However, 
4b0e0 74 68 65 73 65 20 6d 61 63 72 6f 73 20 6d 61 79  these macros may
4b0f0 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74 6f   also be used to
4b100 20 73 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e   suppress warnin
4b110 67 73 20 72 65 6c 61 74 65 64 20 74 6f 0a 2a 2a  gs related to.**
4b120 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74   parameters that
4b130 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20   may or may not 
4b140 62 65 20 75 73 65 64 20 64 65 70 65 6e 64 69 6e  be used dependin
4b150 67 20 6f 6e 20 63 6f 6d 70 69 6c 61 74 69 6f 6e  g on compilation
4b160 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 20 46 6f 72   options..** For
4b170 20 65 78 61 6d 70 6c 65 20 74 68 6f 73 65 20 70   example those p
4b180 61 72 61 6d 65 74 65 72 73 20 6f 6e 6c 79 20 75  arameters only u
4b190 73 65 64 20 69 6e 20 61 73 73 65 72 74 28 29 20  sed in assert() 
4b1a0 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 6e 20 74  statements. In t
4b1b0 68 65 73 65 0a 2a 2a 20 63 61 73 65 73 20 74 68  hese.** cases th
4b1c0 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  e parameters are
4b1d0 20 6e 61 6d 65 64 20 61 73 20 70 65 72 20 74 68   named as per th
4b1e0 65 20 75 73 75 61 6c 20 63 6f 6e 76 65 6e 74 69  e usual conventi
4b1f0 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ons..*/.#define 
4b200 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
4b210 28 78 29 20 28 76 6f 69 64 29 28 78 29 0a 23 64  (x) (void)(x).#d
4b220 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 52  efine UNUSED_PAR
4b230 41 4d 45 54 45 52 32 28 78 2c 79 29 20 55 4e 55  AMETER2(x,y) UNU
4b240 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29  SED_PARAMETER(x)
4b250 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45  ,UNUSED_PARAMETE
4b260 52 28 79 29 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77  R(y)../*.** Forw
4b270 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 74  ard references t
4b280 6f 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2f 0a  o structures.*/.
4b290 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41  typedef struct A
4b2a0 67 67 49 6e 66 6f 20 41 67 67 49 6e 66 6f 3b 0a  ggInfo AggInfo;.
4b2b0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41  typedef struct A
4b2c0 75 74 68 43 6f 6e 74 65 78 74 20 41 75 74 68 43  uthContext AuthC
4b2d0 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 20  ontext;.typedef 
4b2e0 73 74 72 75 63 74 20 41 75 74 6f 69 6e 63 49 6e  struct AutoincIn
4b2f0 66 6f 20 41 75 74 6f 69 6e 63 49 6e 66 6f 3b 0a  fo AutoincInfo;.
4b300 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42  typedef struct B
4b310 69 74 76 65 63 20 42 69 74 76 65 63 3b 0a 74 79  itvec Bitvec;.ty
4b320 70 65 64 65 66 20 73 74 72 75 63 74 20 52 6f 77  pedef struct Row
4b330 53 65 74 20 52 6f 77 53 65 74 3b 0a 74 79 70 65  Set RowSet;.type
4b340 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 6c 53  def struct CollS
4b350 65 71 20 43 6f 6c 6c 53 65 71 3b 0a 74 79 70 65  eq CollSeq;.type
4b360 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 75 6d  def struct Colum
4b370 6e 20 43 6f 6c 75 6d 6e 3b 0a 74 79 70 65 64 65  n Column;.typede
4b380 66 20 73 74 72 75 63 74 20 44 62 20 44 62 3b 0a  f struct Db Db;.
4b390 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
4b3a0 63 68 65 6d 61 20 53 63 68 65 6d 61 3b 0a 74 79  chema Schema;.ty
4b3b0 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70  pedef struct Exp
4b3c0 72 20 45 78 70 72 3b 0a 74 79 70 65 64 65 66 20  r Expr;.typedef 
4b3d0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 20  struct ExprList 
4b3e0 45 78 70 72 4c 69 73 74 3b 0a 74 79 70 65 64 65  ExprList;.typede
4b3f0 66 20 73 74 72 75 63 74 20 45 78 70 72 53 70 61  f struct ExprSpa
4b400 6e 20 45 78 70 72 53 70 61 6e 3b 0a 74 79 70 65  n ExprSpan;.type
4b410 64 65 66 20 73 74 72 75 63 74 20 46 4b 65 79 20  def struct FKey 
4b420 46 4b 65 79 3b 0a 74 79 70 65 64 65 66 20 73 74  FKey;.typedef st
4b430 72 75 63 74 20 46 75 6e 63 44 65 66 20 46 75 6e  ruct FuncDef Fun
4b440 63 44 65 66 3b 0a 74 79 70 65 64 65 66 20 73 74  cDef;.typedef st
4b450 72 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 68  ruct FuncDefHash
4b460 20 46 75 6e 63 44 65 66 48 61 73 68 3b 0a 74 79   FuncDefHash;.ty
4b470 70 65 64 65 66 20 73 74 72 75 63 74 20 49 64 4c  pedef struct IdL
4b480 69 73 74 20 49 64 4c 69 73 74 3b 0a 74 79 70 65  ist IdList;.type
4b490 64 65 66 20 73 74 72 75 63 74 20 49 6e 64 65 78  def struct Index
4b4a0 20 49 6e 64 65 78 3b 0a 74 79 70 65 64 65 66 20   Index;.typedef 
4b4b0 73 74 72 75 63 74 20 49 6e 64 65 78 53 61 6d 70  struct IndexSamp
4b4c0 6c 65 20 49 6e 64 65 78 53 61 6d 70 6c 65 3b 0a  le IndexSample;.
4b4d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4b  typedef struct K
4b4e0 65 79 43 6c 61 73 73 20 4b 65 79 43 6c 61 73 73  eyClass KeyClass
4b4f0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b500 20 4b 65 79 49 6e 66 6f 20 4b 65 79 49 6e 66 6f   KeyInfo KeyInfo
4b510 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b520 20 4c 6f 6f 6b 61 73 69 64 65 20 4c 6f 6f 6b 61   Lookaside Looka
4b530 73 69 64 65 3b 0a 74 79 70 65 64 65 66 20 73 74  side;.typedef st
4b540 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c  ruct LookasideSl
4b550 6f 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74  ot LookasideSlot
4b560 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b570 20 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a   Module Module;.
4b580 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4e  typedef struct N
4b590 61 6d 65 43 6f 6e 74 65 78 74 20 4e 61 6d 65 43  ameContext NameC
4b5a0 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 20  ontext;.typedef 
4b5b0 73 74 72 75 63 74 20 50 61 72 73 65 20 50 61 72  struct Parse Par
4b5c0 73 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  se;.typedef stru
4b5d0 63 74 20 53 61 76 65 70 6f 69 6e 74 20 53 61 76  ct Savepoint Sav
4b5e0 65 70 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 20  epoint;.typedef 
4b5f0 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 53 65  struct Select Se
4b600 6c 65 63 74 3b 0a 74 79 70 65 64 65 66 20 73 74  lect;.typedef st
4b610 72 75 63 74 20 53 72 63 4c 69 73 74 20 53 72 63  ruct SrcList Src
4b620 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74  List;.typedef st
4b630 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 53 74  ruct StrAccum St
4b640 72 41 63 63 75 6d 3b 0a 74 79 70 65 64 65 66 20  rAccum;.typedef 
4b650 73 74 72 75 63 74 20 54 61 62 6c 65 20 54 61 62  struct Table Tab
4b660 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  le;.typedef stru
4b670 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 54 61 62  ct TableLock Tab
4b680 6c 65 4c 6f 63 6b 3b 0a 74 79 70 65 64 65 66 20  leLock;.typedef 
4b690 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 54 6f 6b  struct Token Tok
4b6a0 65 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  en;.typedef stru
4b6b0 63 74 20 54 72 69 67 67 65 72 50 72 67 20 54 72  ct TriggerPrg Tr
4b6c0 69 67 67 65 72 50 72 67 3b 0a 74 79 70 65 64 65  iggerPrg;.typede
4b6d0 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72  f struct Trigger
4b6e0 53 74 65 70 20 54 72 69 67 67 65 72 53 74 65 70  Step TriggerStep
4b6f0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b700 20 54 72 69 67 67 65 72 20 54 72 69 67 67 65 72   Trigger Trigger
4b710 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b720 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
4b730 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 3b 0a  UnpackedRecord;.
4b740 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56  typedef struct V
4b750 54 61 62 6c 65 20 56 54 61 62 6c 65 3b 0a 74 79  Table VTable;.ty
4b760 70 65 64 65 66 20 73 74 72 75 63 74 20 57 61 6c  pedef struct Wal
4b770 6b 65 72 20 57 61 6c 6b 65 72 3b 0a 74 79 70 65  ker Walker;.type
4b780 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65  def struct Where
4b790 50 6c 61 6e 20 57 68 65 72 65 50 6c 61 6e 3b 0a  Plan WherePlan;.
4b7a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
4b7b0 68 65 72 65 49 6e 66 6f 20 57 68 65 72 65 49 6e  hereInfo WhereIn
4b7c0 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  fo;.typedef stru
4b7d0 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 57 68  ct WhereLevel Wh
4b7e0 65 72 65 4c 65 76 65 6c 3b 0a 0a 2f 2a 0a 2a 2a  ereLevel;../*.**
4b7f0 20 44 65 66 65 72 20 73 6f 75 72 63 69 6e 67 20   Defer sourcing 
4b800 76 64 62 65 2e 68 20 61 6e 64 20 62 74 72 65 65  vdbe.h and btree
4b810 2e 68 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74  .h until after t
4b820 68 65 20 22 75 38 22 20 61 6e 64 20 0a 2a 2a 20  he "u8" and .** 
4b830 22 42 75 73 79 48 61 6e 64 6c 65 72 22 20 74 79  "BusyHandler" ty
4b840 70 65 64 65 66 73 2e 20 76 64 62 65 2e 68 20 61  pedefs. vdbe.h a
4b850 6c 73 6f 20 72 65 71 75 69 72 65 73 20 61 20 66  lso requires a f
4b860 65 77 20 6f 66 20 74 68 65 20 6f 70 61 71 75 65  ew of the opaque
4b870 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 79 70 65  .** pointer type
4b880 73 20 28 69 2e 65 2e 20 46 75 6e 63 44 65 66 29  s (i.e. FuncDef)
4b890 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 2e 0a   defined above..
4b8a0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4b8b0 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65 65  ** Include btree
4b8c0 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
4b8d0 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
4b8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b8f0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4b900 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74  ** Begin file bt
4b910 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ree.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 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
4b950 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
4b960 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
4b970 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
4b980 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
4b990 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
4b9a0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
4b9b0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
4b9c0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
4b9d0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
4b9e0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
4b9f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
4ba00 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
4ba10 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
4ba20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
4ba30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
4ba40 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
4ba50 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
4ba60 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
4ba70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 0a 2a 2a 20 54 68 69  *********.** Thi
4bac0 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65  s header file de
4bad0 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66  fines the interf
4bae0 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c  ace that the sql
4baf0 69 74 65 20 42 2d 54 72 65 65 20 66 69 6c 65 0a  ite B-Tree file.
4bb00 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 20 53  ** subsystem.  S
4bb10 65 65 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74  ee comments in t
4bb20 68 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 66  he source code f
4bb30 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64 65  or a detailed de
4bb40 73 63 72 69 70 74 69 6f 6e 0a 2a 2a 20 6f 66 20  scription.** of 
4bb50 77 68 61 74 20 65 61 63 68 20 69 6e 74 65 72 66  what each interf
4bb60 61 63 65 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  ace routine does
4bb70 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
4bb80 3a 20 62 74 72 65 65 2e 68 2c 76 20 31 2e 31 32  : btree.h,v 1.12
4bb90 30 20 32 30 30 39 2f 30 37 2f 32 32 20 30 30 3a  0 2009/07/22 00:
4bba0 33 35 3a 32 34 20 64 72 68 20 45 78 70 20 24 0a  35:24 drh Exp $.
4bbb0 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42 54 52 45  */.#ifndef _BTRE
4bbc0 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 42 54  E_H_.#define _BT
4bbd0 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a  REE_H_../* TODO:
4bbe0 20 54 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e   This definition
4bbf0 20 69 73 20 6a 75 73 74 20 69 6e 63 6c 75 64 65   is just include
4bc00 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f 64 75 6c  d so other modul
4bc10 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a  es compile. It.*
4bc20 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  * needs to be re
4bc30 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 66  visited..*/.#def
4bc40 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 42 54 52  ine SQLITE_N_BTR
4bc50 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a  EE_META 10../*.*
4bc60 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 73 20  * If defined as 
4bc70 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76  non-zero, auto-v
4bc80 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64  acuum is enabled
4bc90 20 62 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 68   by default. Oth
4bca0 65 72 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73  erwise.** it mus
4bcb0 74 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 66  t be turned on f
4bcc0 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65  or each database
4bcd0 20 75 73 69 6e 67 20 22 50 52 41 47 4d 41 20 61   using "PRAGMA a
4bce0 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e  uto_vacuum = 1".
4bcf0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
4bd00 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56  TE_DEFAULT_AUTOV
4bd10 41 43 55 55 4d 0a 20 20 23 64 65 66 69 6e 65 20  ACUUM.  #define 
4bd20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41  SQLITE_DEFAULT_A
4bd30 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e 64  UTOVACUUM 0.#end
4bd40 69 66 0a 0a 23 64 65 66 69 6e 65 20 42 54 52 45  if..#define BTRE
4bd50 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e  E_AUTOVACUUM_NON
4bd60 45 20 30 20 20 20 20 20 20 20 20 2f 2a 20 44 6f  E 0        /* Do
4bd70 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d 76 61 63   not do auto-vac
4bd80 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  uum */.#define B
4bd90 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
4bda0 46 55 4c 4c 20 31 20 20 20 20 20 20 20 20 2f 2a  FULL 1        /*
4bdb0 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f 2d 76 61   Do full auto-va
4bdc0 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cuum */.#define 
4bdd0 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
4bde0 5f 49 4e 43 52 20 32 20 20 20 20 20 20 20 20 2f  _INCR 2        /
4bdf0 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61  * Incremental va
4be00 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46  cuum */../*.** F
4be10 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
4be20 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 65  ons of structure
4be30 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
4be40 63 74 20 42 74 72 65 65 20 42 74 72 65 65 3b 0a  ct Btree Btree;.
4be50 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42  typedef struct B
4be60 74 43 75 72 73 6f 72 20 42 74 43 75 72 73 6f 72  tCursor BtCursor
4be70 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4be80 20 42 74 53 68 61 72 65 64 20 42 74 53 68 61 72   BtShared BtShar
4be90 65 64 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ed;.typedef stru
4bea0 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 72  ct BtreeMutexArr
4beb0 61 79 20 42 74 72 65 65 4d 75 74 65 78 41 72 72  ay BtreeMutexArr
4bec0 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ay;../*.** This 
4bed0 73 74 72 75 63 74 75 72 65 20 72 65 63 6f 72 64  structure record
4bee0 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 42 74 72  s all of the Btr
4bef0 65 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  ees that need to
4bf00 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 78   hold.** a mutex
4bf10 20 62 65 66 6f 72 65 20 77 65 20 65 6e 74 65 72   before we enter
4bf20 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
4bf30 28 29 2e 20 20 54 68 65 20 42 74 72 65 65 73 20  ().  The Btrees 
4bf40 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c 61 63 65  are.** are place
4bf50 64 20 69 6e 20 61 42 74 72 65 65 5b 5d 20 69 6e  d in aBtree[] in
4bf60 20 6f 72 64 65 72 20 6f 66 20 61 42 74 72 65 65   order of aBtree
4bf70 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61 74 20 77  []->pBt.  That w
4bf80 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e 20 61 6c  ay,.** we can al
4bf90 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64 20 75 6e  ways lock and un
4bfa0 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c 20 71 75  lock them all qu
4bfb0 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74  ickly..*/.struct
4bfc0 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79   BtreeMutexArray
4bfd0 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74 65 78 3b   {.  int nMutex;
4bfe0 0a 20 20 42 74 72 65 65 20 2a 61 42 74 72 65 65  .  Btree *aBtree
4bff0 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41  [SQLITE_MAX_ATTA
4c000 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51  CHED+1];.};...SQ
4c010 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4c020 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65   sqlite3BtreeOpe
4c030 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  n(.  const char 
4c040 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a  *zFilename,   /*
4c050 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
4c060 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a  e file to open *
4c070 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  /.  sqlite3 *db,
4c080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4c090 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62  Associated datab
4c0a0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
4c0b0 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74  /.  Btree **ppBt
4c0c0 72 65 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ree,         /* 
4c0d0 52 65 74 75 72 6e 20 6f 70 65 6e 20 42 74 72 65  Return open Btre
4c0e0 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  e* here */.  int
4c0f0 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
4c100 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a        /* Flags *
4c110 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73  /.  int vfsFlags
4c120 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4c130 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72  Flags passed thr
4c140 6f 75 67 68 20 74 6f 20 56 46 53 20 6f 70 65 6e  ough to VFS open
4c150 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66   */.);../* The f
4c160 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 74  lags parameter t
4c170 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70  o sqlite3BtreeOp
4c180 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20 62 69  en can be the bi
4c190 74 77 69 73 65 20 6f 72 20 6f 66 20 74 68 65 0a  twise or of the.
4c1a0 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c  ** following val
4c1b0 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a  ues..**.** NOTE:
4c1c0 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d    These values m
4c1d0 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f  ust match the co
4c1e0 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 41 47 45  rresponding PAGE
4c1f0 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20  R_ values in.** 
4c200 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66  pager.h..*/.#def
4c210 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a  ine BTREE_OMIT_J
4c220 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f  OURNAL  1  /* Do
4c230 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72 6e 61 6c   not use journal
4c240 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a  .  No argument *
4c250 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
4c260 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 32 20  NO_READLOCK   2 
4c270 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63   /* Omit readloc
4c280 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66  ks on readonly f
4c290 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  iles */.#define 
4c2a0 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20  BTREE_MEMORY    
4c2b0 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d      4  /* In-mem
4c2c0 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61 72 67 75  ory DB.  No argu
4c2d0 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ment */.#define 
4c2e0 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c 59 20 20  BTREE_READONLY  
4c2f0 20 20 20 20 38 20 20 2f 2a 20 4f 70 65 6e 20 74      8  /* Open t
4c300 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 72  he database in r
4c310 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f  ead-only mode */
4c320 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52  .#define BTREE_R
4c330 45 41 44 57 52 49 54 45 20 20 20 20 31 36 20 20  EADWRITE    16  
4c340 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62 6f 74 68  /* Open for both
4c350 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69   reading and wri
4c360 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ting */.#define 
4c370 42 54 52 45 45 5f 43 52 45 41 54 45 20 20 20 20  BTREE_CREATE    
4c380 20 20 20 33 32 20 20 2f 2a 20 43 72 65 61 74 65     32  /* Create
4c390 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 66   the database if
4c3a0 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69   it does not exi
4c3b0 73 74 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52  st */..SQLITE_PR
4c3c0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c3d0 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 72 65  3BtreeClose(Btre
4c3e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
4c3f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4c400 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65  treeSetCacheSize
4c410 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51  (Btree*,int);.SQ
4c420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4c430 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
4c440 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65  SafetyLevel(Btre
4c450 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  e*,int,int);.SQL
4c460 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c470 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63  sqlite3BtreeSync
4c480 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 2a 29  Disabled(Btree*)
4c490 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c4a0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c4b0 65 53 65 74 50 61 67 65 53 69 7a 65 28 42 74 72  eSetPageSize(Btr
4c4c0 65 65 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65  ee *p, int nPage
4c4d0 73 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 72  size, int nReser
4c4e0 76 65 2c 20 69 6e 74 20 65 46 69 78 29 3b 0a 53  ve, int eFix);.S
4c4f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c500 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  t sqlite3BtreeGe
4c510 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 2a  tPageSize(Btree*
4c520 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c530 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c540 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 42  eeMaxPageCount(B
4c550 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  tree*,int);.SQLI
4c560 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c570 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 65  qlite3BtreeGetRe
4c580 73 65 72 76 65 28 42 74 72 65 65 2a 29 3b 0a 53  serve(Btree*);.S
4c590 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c5a0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
4c5b0 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65  tAutoVacuum(Btre
4c5c0 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  e *, int);.SQLIT
4c5d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4c5e0 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75 74  lite3BtreeGetAut
4c5f0 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29  oVacuum(Btree *)
4c600 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c610 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c620 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65  eBeginTrans(Btre
4c630 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  e*,int);.SQLITE_
4c640 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c650 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
4c660 61 73 65 4f 6e 65 28 42 74 72 65 65 2a 2c 20 63  aseOne(Btree*, c
4c670 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74  onst char *zMast
4c680 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  er);.SQLITE_PRIV
4c690 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4c6a0 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54  treeCommitPhaseT
4c6b0 77 6f 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  wo(Btree*);.SQLI
4c6c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c6d0 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
4c6e0 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  t(Btree*);.SQLIT
4c6f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4c700 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
4c710 63 6b 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  ck(Btree*);.SQLI
4c720 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c730 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e  qlite3BtreeBegin
4c740 53 74 6d 74 28 42 74 72 65 65 2a 2c 69 6e 74 29  Stmt(Btree*,int)
4c750 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c760 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c770 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72  eCreateTable(Btr
4c780 65 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66  ee*, int*, int f
4c790 6c 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52  lags);.SQLITE_PR
4c7a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c7b0 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
4c7c0 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
4c7d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c7e0 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64  te3BtreeIsInRead
4c7f0 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53  Trans(Btree*);.S
4c800 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c810 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  t sqlite3BtreeIs
4c820 49 6e 42 61 63 6b 75 70 28 42 74 72 65 65 2a 29  InBackup(Btree*)
4c830 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c840 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74   void *sqlite3Bt
4c850 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 20  reeSchema(Btree 
4c860 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28  *, int, void(*)(
4c870 76 6f 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45  void *));.SQLITE
4c880 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c890 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c  ite3BtreeSchemaL
4c8a0 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 42 74  ocked(Btree *pBt
4c8b0 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ree);.SQLITE_PRI
4c8c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c8d0 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42  BtreeLockTable(B
4c8e0 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e  tree *pBtree, in
4c8f0 74 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69  t iTab, u8 isWri
4c900 74 65 4c 6f 63 6b 29 3b 0a 53 51 4c 49 54 45 5f  teLock);.SQLITE_
4c910 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c920 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e  te3BtreeSavepoin
4c930 74 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20  t(Btree *, int, 
4c940 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
4c950 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
4c960 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65   *sqlite3BtreeGe
4c970 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20  tFilename(Btree 
4c980 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4c990 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
4c9a0 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f  qlite3BtreeGetJo
4c9b0 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20  urnalname(Btree 
4c9c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4c9d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4c9e0 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65  reeCopyFile(Btre
4c9f0 65 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a  e *, Btree *);..
4ca00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4ca10 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
4ca20 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 20  ncrVacuum(Btree 
4ca30 2a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67  *);../* The flag
4ca40 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  s parameter to s
4ca50 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
4ca60 65 54 61 62 6c 65 20 63 61 6e 20 62 65 20 74 68  eTable can be th
4ca70 65 20 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20  e bitwise OR.** 
4ca80 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
4ca90 20 66 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69   flags:.*/.#defi
4caa0 6e 65 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20  ne BTREE_INTKEY 
4cab0 20 20 20 20 31 20 20 20 20 2f 2a 20 54 61 62 6c      1    /* Tabl
4cac0 65 20 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69  e has only 64-bi
4cad0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
4cae0 20 6b 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65   keys */.#define
4caf0 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20   BTREE_ZERODATA 
4cb00 20 20 32 20 20 20 20 2f 2a 20 54 61 62 6c 65 20    2    /* Table 
4cb10 68 61 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20  has keys only - 
4cb20 6e 6f 20 64 61 74 61 20 2a 2f 0a 23 64 65 66 69  no data */.#defi
4cb30 6e 65 20 42 54 52 45 45 5f 4c 45 41 46 44 41 54  ne BTREE_LEAFDAT
4cb40 41 20 20 20 34 20 20 20 20 2f 2a 20 44 61 74 61  A   4    /* Data
4cb50 20 73 74 6f 72 65 64 20 69 6e 20 6c 65 61 76 65   stored in leave
4cb60 73 20 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73  s only.  Implies
4cb70 20 49 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49   INTKEY */..SQLI
4cb80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4cb90 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54  qlite3BtreeDropT
4cba0 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74  able(Btree*, int
4cbb0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  , int*);.SQLITE_
4cbc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4cbd0 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62  te3BtreeClearTab
4cbe0 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20  le(Btree*, int, 
4cbf0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
4cc00 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4cc10 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75  e3BtreeTripAllCu
4cc20 72 73 6f 72 73 28 42 74 72 65 65 2a 2c 20 69 6e  rsors(Btree*, in
4cc30 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
4cc40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4cc50 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 72  BtreeGetMeta(Btr
4cc60 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20  ee *pBtree, int 
4cc70 69 64 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 65  idx, u32 *pValue
4cc80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4cc90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4cca0 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 72  eeUpdateMeta(Btr
4ccb0 65 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33  ee*, int idx, u3
4ccc0 32 20 76 61 6c 75 65 29 3b 0a 0a 2f 2a 0a 2a 2a  2 value);../*.**
4ccd0 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   The second para
4cce0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
4ccf0 42 74 72 65 65 47 65 74 4d 65 74 61 20 6f 72 20  BtreeGetMeta or 
4cd00 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61  sqlite3BtreeUpda
4cd10 74 65 4d 65 74 61 0a 2a 2a 20 73 68 6f 75 6c 64  teMeta.** should
4cd20 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66   be one of the f
4cd30 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e  ollowing values.
4cd40 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   The integer val
4cd50 75 65 73 20 61 72 65 20 61 73 73 69 67 6e 65 64  ues are assigned
4cd60 20 0a 2a 2a 20 74 6f 20 63 6f 6e 73 74 61 6e 74   .** to constant
4cd70 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 66  s so that the of
4cd80 66 73 65 74 20 6f 66 20 74 68 65 20 63 6f 72 72  fset of the corr
4cd90 65 73 70 6f 6e 64 69 6e 67 20 66 69 65 6c 64 20  esponding field 
4cda0 69 6e 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20  in an.** SQLite 
4cdb0 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 20  database header 
4cdc0 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 75 73 69  may be found usi
4cdd0 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ng the following
4cde0 20 66 6f 72 6d 75 6c 61 3a 0a 2a 2a 0a 2a 2a 20   formula:.**.** 
4cdf0 20 20 6f 66 66 73 65 74 20 3d 20 33 36 20 2b 20    offset = 36 + 
4ce00 28 69 64 78 20 2a 20 34 29 0a 2a 2a 0a 2a 2a 20  (idx * 4).**.** 
4ce10 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  For example, the
4ce20 20 66 72 65 65 2d 70 61 67 65 2d 63 6f 75 6e 74   free-page-count
4ce30 20 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65   field is locate
4ce40 64 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 74  d at byte offset
4ce50 20 33 36 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61   36 of.** the da
4ce60 74 61 62 61 73 65 20 66 69 6c 65 20 68 65 61 64  tabase file head
4ce70 65 72 2e 20 54 68 65 20 69 6e 63 72 2d 76 61 63  er. The incr-vac
4ce80 75 75 6d 2d 66 6c 61 67 20 66 69 65 6c 64 20 69  uum-flag field i
4ce90 73 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20  s located at.** 
4cea0 62 79 74 65 20 6f 66 66 73 65 74 20 36 34 20 28  byte offset 64 (
4ceb0 3d 3d 20 33 36 2b 34 2a 37 29 2e 0a 2a 2f 0a 23  == 36+4*7)..*/.#
4cec0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 46 52 45  define BTREE_FRE
4ced0 45 5f 50 41 47 45 5f 43 4f 55 4e 54 20 20 20 20  E_PAGE_COUNT    
4cee0 20 30 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45   0.#define BTREE
4cef0 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20  _SCHEMA_VERSION 
4cf00 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 42       1.#define B
4cf10 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  TREE_FILE_FORMAT
4cf20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69           2.#defi
4cf30 6e 65 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54  ne BTREE_DEFAULT
4cf40 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 33 0a 23  _CACHE_SIZE  3.#
4cf50 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4c 41 52  define BTREE_LAR
4cf60 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 20  GEST_ROOT_PAGE  
4cf70 20 34 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45   4.#define BTREE
4cf80 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 20 20  _TEXT_ENCODING  
4cf90 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 42       5.#define B
4cfa0 54 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49 4f  TREE_USER_VERSIO
4cfb0 4e 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69  N        6.#defi
4cfc0 6e 65 20 42 54 52 45 45 5f 49 4e 43 52 5f 56 41  ne BTREE_INCR_VA
4cfd0 43 55 55 4d 20 20 20 20 20 20 20 20 20 37 0a 0a  CUUM         7..
4cfe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4cff0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
4d000 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c  ursor(.  Btree*,
4d010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4d030 20 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e   BTree containin
4d040 67 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  g table to open 
4d050 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c  */.  int iTable,
4d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
4d080 65 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 20  ex of root page 
4d090 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c  */.  int wrFlag,
4d0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d0b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66            /* 1 f
4d0c0 6f 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20 66  or writing.  0 f
4d0d0 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a  or read-only */.
4d0e0 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f    struct KeyInfo
4d0f0 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *,              
4d100 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
4d110 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70  argument to comp
4d120 61 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  are function */.
4d130 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
4d140 73 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 20  sor             
4d150 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
4d160 74 6f 20 77 72 69 74 65 20 63 75 72 73 6f 72 20  to write cursor 
4d170 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a  structure */.);.
4d180 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4d190 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
4d1a0 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b  ursorSize(void);
4d1b0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
4d1c0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4d1d0 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43  eCloseCursor(BtC
4d1e0 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ursor*);.SQLITE_
4d1f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d200 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e  te3BtreeMovetoUn
4d210 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73  packed(.  BtCurs
4d220 6f 72 2a 2c 0a 20 20 55 6e 70 61 63 6b 65 64 52  or*,.  UnpackedR
4d230 65 63 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a 20  ecord *pUnKey,. 
4d240 20 69 36 34 20 69 6e 74 4b 65 79 2c 0a 20 20 69   i64 intKey,.  i
4d250 6e 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a  nt bias,.  int *
4d260 70 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50  pRes.);.SQLITE_P
4d270 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d280 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73  e3BtreeCursorHas
4d290 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72 2a 2c  Moved(BtCursor*,
4d2a0 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   int*);.SQLITE_P
4d2b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d2c0 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 74  e3BtreeDelete(Bt
4d2d0 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45  Cursor*);.SQLITE
4d2e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4d2f0 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28  ite3BtreeInsert(
4d300 42 74 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73 74  BtCursor*, const
4d310 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34   void *pKey, i64
4d320 20 6e 4b 65 79 2c 0a 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 20 63 6f 6e 73 74 20 76           const v
4d350 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  oid *pData, int 
4d360 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
4d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d380 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 5a 65           int nZe
4d390 72 6f 2c 20 69 6e 74 20 62 69 61 73 2c 20 69 6e  ro, int bias, in
4d3a0 74 20 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 53  t seekResult);.S
4d3b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d3c0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69  t sqlite3BtreeFi
4d3d0 72 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69  rst(BtCursor*, i
4d3e0 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54  nt *pRes);.SQLIT
4d3f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4d400 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42  lite3BtreeLast(B
4d410 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70  tCursor*, int *p
4d420 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Res);.SQLITE_PRI
4d430 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4d440 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73  BtreeNext(BtCurs
4d450 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b  or*, int *pRes);
4d460 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d470 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d480 45 6f 66 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a  Eof(BtCursor*);.
4d490 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4d4a0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  nt sqlite3BtreeP
4d4b0 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72  revious(BtCursor
4d4c0 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53  *, int *pRes);.S
4d4d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d4e0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  t sqlite3BtreeKe
4d4f0 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c  ySize(BtCursor*,
4d500 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51   i64 *pSize);.SQ
4d510 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d520 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
4d530 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20  (BtCursor*, u32 
4d540 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c  offset, u32 amt,
4d550 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
4d560 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f  PRIVATE const vo
4d570 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  id *sqlite3Btree
4d580 4b 65 79 46 65 74 63 68 28 42 74 43 75 72 73 6f  KeyFetch(BtCurso
4d590 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a  r*, int *pAmt);.
4d5a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
4d5b0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
4d5c0 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
4d5d0 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20  (BtCursor*, int 
4d5e0 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *pAmt);.SQLITE_P
4d5f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d600 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28  e3BtreeDataSize(
4d610 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 2a  BtCursor*, u32 *
4d620 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50  pSize);.SQLITE_P
4d630 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d640 65 33 42 74 72 65 65 44 61 74 61 28 42 74 43 75  e3BtreeData(BtCu
4d650 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65  rsor*, u32 offse
4d660 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64  t, u32 amt, void
4d670 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4d680 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
4d690 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77  treeSetCachedRow
4d6a0 69 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 73 71  id(BtCursor*, sq
4d6b0 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51  lite3_int64);.SQ
4d6c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
4d6d0 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74  ite3_int64 sqlit
4d6e0 65 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64  e3BtreeGetCached
4d6f0 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 2a 29  Rowid(BtCursor*)
4d700 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
4d710 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42  E char *sqlite3B
4d720 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65  treeIntegrityChe
4d730 63 6b 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 2a  ck(Btree*, int *
4d740 61 52 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f 74  aRoot, int nRoot
4d750 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51  , int, int*);.SQ
4d760 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 72  LITE_PRIVATE str
4d770 75 63 74 20 50 61 67 65 72 20 2a 73 71 6c 69 74  uct Pager *sqlit
4d780 65 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72  e3BtreePager(Btr
4d790 65 65 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  ee*);..SQLITE_PR
4d7a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d7b0 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74  3BtreePutData(Bt
4d7c0 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66  Cursor*, u32 off
4d7d0 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f  set, u32 amt, vo
4d7e0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  id*);.SQLITE_PRI
4d7f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4d800 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66  3BtreeCacheOverf
4d810 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29 3b  low(BtCursor *);
4d820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d830 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
4d840 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43  eClearCursor(BtC
4d850 75 72 73 6f 72 20 2a 29 3b 0a 0a 23 69 66 6e 64  ursor *);..#ifnd
4d860 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45  ef NDEBUG.SQLITE
4d870 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4d880 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
4d890 73 56 61 6c 69 64 28 42 74 43 75 72 73 6f 72 2a  sValid(BtCursor*
4d8a0 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  );.#endif..#ifnd
4d8b0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
4d8c0 54 52 45 45 43 4f 55 4e 54 0a 53 51 4c 49 54 45  TREECOUNT.SQLITE
4d8d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4d8e0 69 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42  ite3BtreeCount(B
4d8f0 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a  tCursor *, i64 *
4d900 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  );.#endif..#ifde
4d910 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
4d920 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d930 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
4d940 73 6f 72 49 6e 66 6f 28 42 74 43 75 72 73 6f 72  sorInfo(BtCursor
4d950 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53  *, int*, int);.S
4d960 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4d970 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  id sqlite3BtreeC
4d980 75 72 73 6f 72 4c 69 73 74 28 42 74 72 65 65 2a  ursorList(Btree*
4d990 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  );.#endif../*.**
4d9a0 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 75   If we are not u
4d9b0 73 69 6e 67 20 73 68 61 72 65 64 20 63 61 63 68  sing shared cach
4d9c0 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73  e, then there is
4d9d0 20 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 75   no need to.** u
4d9e0 73 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61 63  se mutexes to ac
4d9f0 63 65 73 73 20 74 68 65 20 42 74 53 68 61 72 65  cess the BtShare
4da00 64 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 53  d structures.  S
4da10 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45 6e  o make the.** En
4da20 74 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70 72  ter and Leave pr
4da30 6f 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73 2e  ocedures no-ops.
4da40 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
4da50 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
4da60 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56  ACHE.SQLITE_PRIV
4da70 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
4da80 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 72  e3BtreeEnter(Btr
4da90 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
4daa0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
4dab0 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
4dac0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73  (sqlite3*);.#els
4dad0 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  e.# define sqlit
4dae0 65 33 42 74 72 65 65 45 6e 74 65 72 28 58 29 20  e3BtreeEnter(X) 
4daf0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
4db00 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 58  3BtreeEnterAll(X
4db10 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64  ).#endif..#if !d
4db20 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
4db30 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29  IT_SHARED_CACHE)
4db40 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41   && SQLITE_THREA
4db50 44 53 41 46 45 0a 53 51 4c 49 54 45 5f 50 52 49  DSAFE.SQLITE_PRI
4db60 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
4db70 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 42 74  te3BtreeLeave(Bt
4db80 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
4db90 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
4dba0 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 43 75  ite3BtreeEnterCu
4dbb0 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b  rsor(BtCursor*);
4dbc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4dbd0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74    void sqlite3Bt
4dbe0 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 42  reeLeaveCursor(B
4dbf0 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54  tCursor*);.SQLIT
4dc00 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
4dc10 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
4dc20 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b  veAll(sqlite3*);
4dc30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4dc40 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74    void sqlite3Bt
4dc50 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74  reeMutexArrayEnt
4dc60 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72  er(BtreeMutexArr
4dc70 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ay*);.SQLITE_PRI
4dc80 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
4dc90 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72  te3BtreeMutexArr
4dca0 61 79 4c 65 61 76 65 28 42 74 72 65 65 4d 75 74  ayLeave(BtreeMut
4dcb0 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54  exArray*);.SQLIT
4dcc0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
4dcd0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
4dce0 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 42 74  exArrayInsert(Bt
4dcf0 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 2c 20  reeMutexArray*, 
4dd00 42 74 72 65 65 2a 29 3b 0a 23 69 66 6e 64 65 66  Btree*);.#ifndef
4dd10 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65   NDEBUG.  /* The
4dd20 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
4dd30 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65  used inside asse
4dd40 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
4dd50 6f 6e 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f  only. */.SQLITE_
4dd60 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
4dd70 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
4dd80 75 74 65 78 28 42 74 72 65 65 2a 29 3b 0a 53 51  utex(Btree*);.SQ
4dd90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
4dda0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  nt sqlite3BtreeH
4ddb0 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 73  oldsAllMutexes(s
4ddc0 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6e 64 69 66  qlite3*);.#endif
4ddd0 0a 23 65 6c 73 65 0a 0a 23 20 64 65 66 69 6e 65  .#else..# define
4dde0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
4ddf0 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  ve(X).# define s
4de00 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
4de10 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66 69  Cursor(X).# defi
4de20 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  ne sqlite3BtreeL
4de30 65 61 76 65 43 75 72 73 6f 72 28 58 29 0a 23 20  eaveCursor(X).# 
4de40 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
4de50 72 65 65 4c 65 61 76 65 41 6c 6c 28 58 29 0a 23  reeLeaveAll(X).#
4de60 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
4de70 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e  treeMutexArrayEn
4de80 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20  ter(X).# define 
4de90 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
4dea0 78 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a 23  xArrayLeave(X).#
4deb0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
4dec0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e  treeMutexArrayIn
4ded0 73 65 72 74 28 58 2c 59 29 0a 0a 23 20 64 65 66  sert(X,Y)..# def
4dee0 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
4def0 48 6f 6c 64 73 4d 75 74 65 78 28 58 29 20 31 0a  HoldsMutex(X) 1.
4df00 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
4df10 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
4df20 65 78 65 73 28 58 29 20 31 0a 23 65 6e 64 69 66  exes(X) 1.#endif
4df30 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54  ...#endif /* _BT
4df40 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  REE_H_ */../****
4df50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
4df60 66 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a  f 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 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
4dfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
4dfb0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
4dfc0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74  eft off in sqlit
4dfd0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
4dfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
4dff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
4e000 64 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65  de vdbe.h in the
4e010 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
4e020 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
4e030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
4e040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
4e050 20 66 69 6c 65 20 76 64 62 65 2e 68 20 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 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
4e090 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
4e0a0 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
4e0b0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
4e0c0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
4e0d0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
4e0e0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
4e0f0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
4e100 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
4e110 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
4e120 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
4e130 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
4e140 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
4e150 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
4e160 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
4e170 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
4e180 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
4e190 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
4e1a0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
4e1b0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
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 2a 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65  *.** Header file
4e210 20 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c   for the Virtual
4e220 20 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65   DataBase Engine
4e230 20 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68   (VDBE).**.** Th
4e240 69 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65  is header define
4e250 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  s the interface 
4e260 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64  to the virtual d
4e270 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a  atabase engine.*
4e280 2a 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20  * or VDBE.  The 
4e290 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20  VDBE implements 
4e2a0 61 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68  an abstract mach
4e2b0 69 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a  ine that runs a.
4e2c0 2a 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61  ** simple progra
4e2d0 6d 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20  m to access and 
4e2e0 6d 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72  modify the under
4e2f0 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a  lying database..
4e300 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 2e  **.** $Id: vdbe.
4e310 68 2c 76 20 31 2e 31 34 32 20 32 30 30 39 2f 30  h,v 1.142 2009/0
4e320 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20 64 61  7/24 17:58:53 da
4e330 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
4e340 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49  */.#ifndef _SQLI
4e350 54 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69  TE_VDBE_H_.#defi
4e360 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f  ne _SQLITE_VDBE_
4e370 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67  H_../*.** A sing
4e380 6c 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70  le VDBE is an op
4e390 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e  aque structure n
4e3a0 61 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e  amed "Vdbe".  On
4e3b0 6c 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69  ly routines.** i
4e3c0 6e 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c  n the source fil
4e3d0 65 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61  e sqliteVdbe.c a
4e3e0 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65  re allowed to se
4e3f0 65 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a  e the insides.**
4e400 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
4e410 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  re..*/.typedef s
4e420 74 72 75 63 74 20 56 64 62 65 20 56 64 62 65 3b  truct Vdbe Vdbe;
4e430 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65  ../*.** The name
4e440 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s of the followi
4e450 6e 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 65  ng types declare
4e460 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 61  d in vdbeInt.h a
4e470 72 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 66  re required.** f
4e480 6f 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 65  or the VdbeOp de
4e490 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70  finition..*/.typ
4e4a0 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65  edef struct Vdbe
4e4b0 46 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a 74  Func VdbeFunc;.t
4e4c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65  ypedef struct Me
4e4d0 6d 20 4d 65 6d 3b 0a 74 79 70 65 64 65 66 20 73  m Mem;.typedef s
4e4e0 74 72 75 63 74 20 53 75 62 50 72 6f 67 72 61 6d  truct SubProgram
4e4f0 20 53 75 62 50 72 6f 67 72 61 6d 3b 0a 0a 2f 2a   SubProgram;../*
4e500 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 73  .** A single ins
4e510 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20  truction of the 
4e520 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
4e530 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a 2a  has an opcode.**
4e540 20 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 20   and as many as 
4e550 74 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e 20  three operands. 
4e560 20 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e   The instruction
4e570 20 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a 20   is recorded.** 
4e580 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  as an instance o
4e590 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
4e5a0 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73 74  structure:.*/.st
4e5b0 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20 20  ruct VdbeOp {.  
4e5c0 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20  u8 opcode;      
4e5d0 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72      /* What oper
4e5e0 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d  ation to perform
4e5f0 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61   */.  signed cha
4e600 72 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e 65  r p4type; /* One
4e610 20 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20 63   of the P4_xxx c
4e620 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34 20  onstants for p4 
4e630 2a 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73 3b  */.  u8 opflags;
4e640 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20           /* Not 
4e650 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a  currently used *
4e660 2f 0a 20 20 75 38 20 70 35 3b 20 20 20 20 20 20  /.  u8 p5;      
4e670 20 20 20 20 20 20 20 20 2f 2a 20 46 69 66 74 68          /* Fifth
4e680 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e   parameter is an
4e690 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63   unsigned charac
4e6a0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b  ter */.  int p1;
4e6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4e6c0 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f  First operand */
4e6d0 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20  .  int p2;      
4e6e0 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64         /* Second
4e6f0 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65   parameter (ofte
4e700 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69  n the jump desti
4e710 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e 74  nation) */.  int
4e720 20 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 20   p3;            
4e730 20 2f 2a 20 54 68 65 20 74 68 69 72 64 20 70 61   /* The third pa
4e740 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e 69  rameter */.  uni
4e750 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  on {            
4e760 20 2f 2a 20 66 6f 75 72 74 68 20 70 61 72 61 6d   /* fourth param
4e770 65 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  eter */.    int 
4e780 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
4e790 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61     /* Integer va
4e7a0 6c 75 65 20 69 66 20 70 34 74 79 70 65 3d 3d 50  lue if p4type==P
4e7b0 34 5f 49 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76  4_INT32 */.    v
4e7c0 6f 69 64 20 2a 70 3b 20 20 20 20 20 20 20 20 20  oid *p;         
4e7d0 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 69 63        /* Generic
4e7e0 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20   pointer */.    
4e7f0 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
4e800 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
4e810 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 73 74  r to data for st
4e820 72 69 6e 67 20 28 63 68 61 72 20 61 72 72 61 79  ring (char array
4e830 29 20 74 79 70 65 73 20 2a 2f 0a 20 20 20 20 69  ) types */.    i
4e840 36 34 20 2a 70 49 36 34 3b 20 20 20 20 20 20 20  64 *pI64;       
4e850 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68        /* Used wh
4e860 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f  en p4type is P4_
4e870 49 4e 54 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75  INT64 */.    dou
4e880 62 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20  ble *pReal;     
4e890 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
4e8a0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 52 45   p4type is P4_RE
4e8b0 41 4c 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65  AL */.    FuncDe
4e8c0 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
4e8d0 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
4e8e0 74 79 70 65 20 69 73 20 50 34 5f 46 55 4e 43 44  type is P4_FUNCD
4e8f0 45 46 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 75  EF */.    VdbeFu
4e900 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20  nc *pVdbeFunc;  
4e910 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
4e920 74 79 70 65 20 69 73 20 50 34 5f 56 44 42 45 46  type is P4_VDBEF
4e930 55 4e 43 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53  UNC */.    CollS
4e940 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20  eq *pColl;      
4e950 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
4e960 34 74 79 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c  4type is P4_COLL
4e970 53 45 51 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a  SEQ */.    Mem *
4e980 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  pMem;           
4e990 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
4e9a0 34 74 79 70 65 20 69 73 20 50 34 5f 4d 45 4d 20  4type is P4_MEM 
4e9b0 2a 2f 0a 20 20 20 20 56 54 61 62 6c 65 20 2a 70  */.    VTable *p
4e9c0 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 2f 2a  Vtab;         /*
4e9d0 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
4e9e0 65 20 69 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a  e is P4_VTAB */.
4e9f0 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
4ea00 79 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73  yInfo;     /* Us
4ea10 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4ea20 73 20 50 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a  s P4_KEYINFO */.
4ea30 20 20 20 20 69 6e 74 20 2a 61 69 3b 20 20 20 20      int *ai;    
4ea40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
4ea50 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4ea60 73 20 50 34 5f 49 4e 54 41 52 52 41 59 20 2a 2f  s P4_INTARRAY */
4ea70 0a 20 20 20 20 53 75 62 50 72 6f 67 72 61 6d 20  .    SubProgram 
4ea80 2a 70 50 72 6f 67 72 61 6d 3b 20 20 2f 2a 20 55  *pProgram;  /* U
4ea90 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20  sed when p4type 
4eaa0 69 73 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d  is P4_SUBPROGRAM
4eab0 20 2a 2f 0a 20 20 7d 20 70 34 3b 0a 23 69 66 64   */.  } p4;.#ifd
4eac0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
4ead0 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 65 6e 74    char *zComment
4eae0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ;          /* Co
4eaf0 6d 6d 65 6e 74 20 74 6f 20 69 6d 70 72 6f 76 65  mment to improve
4eb00 20 72 65 61 64 61 62 69 6c 69 74 79 20 2a 2f 0a   readability */.
4eb10 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56 44  #endif.#ifdef VD
4eb20 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 69 6e 74  BE_PROFILE.  int
4eb30 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20   cnt;           
4eb40 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
4eb50 6f 66 20 74 69 6d 65 73 20 74 68 69 73 20 69 6e  of times this in
4eb60 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 65 78  struction was ex
4eb70 65 63 75 74 65 64 20 2a 2f 0a 20 20 75 36 34 20  ecuted */.  u64 
4eb80 63 79 63 6c 65 73 3b 20 20 20 20 20 20 20 20 20  cycles;         
4eb90 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 74 69       /* Total ti
4eba0 6d 65 20 73 70 65 6e 74 20 65 78 65 63 75 74 69  me spent executi
4ebb0 6e 67 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  ng this instruct
4ebc0 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  ion */.#endif.};
4ebd0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4ebe0 56 64 62 65 4f 70 20 56 64 62 65 4f 70 3b 0a 0a  VdbeOp VdbeOp;..
4ebf0 0a 2f 2a 0a 2a 2a 20 41 20 73 75 62 2d 72 6f 75  ./*.** A sub-rou
4ec00 74 69 6e 65 20 75 73 65 64 20 74 6f 20 69 6d 70  tine used to imp
4ec10 6c 65 6d 65 6e 74 20 61 20 74 72 69 67 67 65 72  lement a trigger
4ec20 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 73 74 72   program..*/.str
4ec30 75 63 74 20 53 75 62 50 72 6f 67 72 61 6d 20 7b  uct SubProgram {
4ec40 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f 70 3b 20  .  VdbeOp *aOp; 
4ec50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ec60 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 6f 70 63   /* Array of opc
4ec70 6f 64 65 73 20 66 6f 72 20 73 75 62 2d 70 72 6f  odes for sub-pro
4ec80 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  gram */.  int nO
4ec90 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
4eca0 20 20 20 20 20 20 20 20 2f 2a 20 45 6c 65 6d 65          /* Eleme
4ecb0 6e 74 73 20 69 6e 20 61 4f 70 5b 5d 20 2a 2f 0a  nts in aOp[] */.
4ecc0 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20    int nMem;     
4ecd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ece0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d  /* Number of mem
4ecf0 6f 72 79 20 63 65 6c 6c 73 20 72 65 71 75 69 72  ory cells requir
4ed00 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 73 72  ed */.  int nCsr
4ed10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4ed20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
4ed30 6f 66 20 63 75 72 73 6f 72 73 20 72 65 71 75 69  of cursors requi
4ed40 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  red */.  int nRe
4ed50 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
4ed60 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
4ed70 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
4ed80 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a  this structure *
4ed90 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e 3b  /.  void *token;
4eda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4edb0 20 20 2f 2a 20 69 64 20 74 68 61 74 20 6d 61 79    /* id that may
4edc0 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 63 75   be used to recu
4edd0 72 73 69 76 65 20 74 72 69 67 67 65 72 73 20 2a  rsive triggers *
4ede0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6d  /.};../*.** A sm
4edf0 61 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f 66  aller version of
4ee00 20 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f 72   VdbeOp used for
4ee10 20 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c 69   the VdbeAddOpLi
4ee20 73 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65  st() function be
4ee30 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b 65  cause.** it take
4ee40 73 20 75 70 20 6c 65 73 73 20 73 70 61 63 65 2e  s up less space.
4ee50 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f  .*/.struct VdbeO
4ee60 70 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70 63  pList {.  u8 opc
4ee70 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ode;          /*
4ee80 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20   What operation 
4ee90 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20  to perform */.  
4eea0 73 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b 20  signed char p1; 
4eeb0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65      /* First ope
4eec0 72 61 6e 64 20 2a 2f 0a 20 20 73 69 67 6e 65 64  rand */.  signed
4eed0 20 63 68 61 72 20 70 32 3b 20 20 20 20 20 2f 2a   char p2;     /*
4eee0 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   Second paramete
4eef0 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d  r (often the jum
4ef00 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a  p destination) *
4ef10 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20  /.  signed char 
4ef20 70 33 3b 20 20 20 20 20 2f 2a 20 54 68 69 72 64  p3;     /* Third
4ef30 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d 3b   parameter */.};
4ef40 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4ef50 56 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65 4f  VdbeOpList VdbeO
4ef60 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c  pList;../*.** Al
4ef70 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20  lowed values of 
4ef80 56 64 62 65 4f 70 2e 70 34 74 79 70 65 0a 2a 2f  VdbeOp.p4type.*/
4ef90 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f 54 55  .#define P4_NOTU
4efa0 53 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54 68  SED    0   /* Th
4efb0 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69  e P4 parameter i
4efc0 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23 64  s not used */.#d
4efd0 65 66 69 6e 65 20 50 34 5f 44 59 4e 41 4d 49 43  efine P4_DYNAMIC
4efe0 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e 74    (-1)  /* Point
4eff0 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f  er to a string o
4f000 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
4f010 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23  iteMalloc() */.#
4f020 64 65 66 69 6e 65 20 50 34 5f 53 54 41 54 49 43  define P4_STATIC
4f030 20 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69 6e     (-2)  /* Poin
4f040 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20  ter to a static 
4f050 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e  string */.#defin
4f060 65 20 50 34 5f 43 4f 4c 4c 53 45 51 20 20 28 2d  e P4_COLLSEQ  (-
4f070 34 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70  4)  /* P4 is a p
4f080 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c  ointer to a Coll
4f090 53 65 71 20 73 74 72 75 63 74 75 72 65 20 2a 2f  Seq structure */
4f0a0 0a 23 64 65 66 69 6e 65 20 50 34 5f 46 55 4e 43  .#define P4_FUNC
4f0b0 44 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50 34  DEF  (-5)  /* P4
4f0c0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
4f0d0 20 61 20 46 75 6e 63 44 65 66 20 73 74 72 75 63   a FuncDef struc
4f0e0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
4f0f0 50 34 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36 29  P4_KEYINFO  (-6)
4f100 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
4f110 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66  nter to a KeyInf
4f120 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23  o structure */.#
4f130 64 65 66 69 6e 65 20 50 34 5f 56 44 42 45 46 55  define P4_VDBEFU
4f140 4e 43 20 28 2d 37 29 20 20 2f 2a 20 50 34 20 69  NC (-7)  /* P4 i
4f150 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
4f160 20 56 64 62 65 46 75 6e 63 20 73 74 72 75 63 74   VdbeFunc struct
4f170 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
4f180 34 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29 20  4_MEM      (-8) 
4f190 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
4f1a0 74 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20 20  ter to a Mem*   
4f1b0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
4f1c0 65 66 69 6e 65 20 50 34 5f 54 52 41 4e 53 49 45  efine P4_TRANSIE
4f1d0 4e 54 20 28 2d 39 29 20 2f 2a 20 50 34 20 69 73  NT (-9) /* P4 is
4f1e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
4f1f0 74 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e 67  transient string
4f200 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56   */.#define P4_V
4f210 54 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f 2a  TAB     (-10) /*
4f220 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
4f230 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76   to an sqlite3_v
4f240 74 61 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f  tab structure */
4f250 0a 23 64 65 66 69 6e 65 20 50 34 5f 4d 50 52 49  .#define P4_MPRI
4f260 4e 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50 34  NTF  (-11) /* P4
4f270 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62 74   is a string obt
4f280 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
4f290 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a  e3_mprintf() */.
4f2a0 23 64 65 66 69 6e 65 20 50 34 5f 52 45 41 4c 20  #define P4_REAL 
4f2b0 20 20 20 20 28 2d 31 32 29 20 2f 2a 20 50 34 20      (-12) /* P4 
4f2c0 69 73 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61  is a 64-bit floa
4f2d0 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
4f2e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49   */.#define P4_I
4f2f0 4e 54 36 34 20 20 20 20 28 2d 31 33 29 20 2f 2a  NT64    (-13) /*
4f300 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20   P4 is a 64-bit 
4f310 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
4f320 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54  /.#define P4_INT
4f330 33 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20 50  32    (-14) /* P
4f340 34 20 69 73 20 61 20 33 32 2d 62 69 74 20 73 69  4 is a 32-bit si
4f350 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
4f360 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41 52  #define P4_INTAR
4f370 52 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34 20  RAY (-15) /* P4 
4f380 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 33  is a vector of 3
4f390 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 2a  2-bit integers *
4f3a0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 53 55 42  /.#define P4_SUB
4f3b0 50 52 4f 47 52 41 4d 20 20 28 2d 31 38 29 20 2f  PROGRAM  (-18) /
4f3c0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
4f3d0 72 20 74 6f 20 61 20 53 75 62 50 72 6f 67 72 61  r to a SubProgra
4f3e0 6d 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 0a  m structure */..
4f3f0 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20 61  /* When adding a
4f400 20 50 34 20 61 72 67 75 6d 65 6e 74 20 75 73 69   P4 argument usi
4f410 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f 2c 20 61  ng P4_KEYINFO, a
4f420 20 63 6f 70 79 20 6f 66 20 74 68 65 20 4b 65 79   copy of the Key
4f430 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a  Info structure.*
4f440 2a 20 69 73 20 6d 61 64 65 2e 20 20 54 68 61 74  * is made.  That
4f450 20 63 6f 70 79 20 69 73 20 66 72 65 65 64 20 77   copy is freed w
4f460 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 20  hen the Vdbe is 
4f470 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42 75 74 20  finalized.  But 
4f480 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65  if the.** argume
4f490 6e 74 20 69 73 20 50 34 5f 4b 45 59 49 4e 46 4f  nt is P4_KEYINFO
4f4a0 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65 20 70 61  _HANDOFF, the pa
4f4b0 73 73 65 64 20 69 6e 20 70 6f 69 6e 74 65 72 20  ssed in pointer 
4f4c0 69 73 20 75 73 65 64 2e 20 20 49 74 20 73 74 69  is used.  It sti
4f4d0 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72 65 65 64  ll.** gets freed
4f4e0 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69   when the Vdbe i
4f4f0 73 20 66 69 6e 61 6c 69 7a 65 64 20 73 6f 20 69  s finalized so i
4f500 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c 64 20 62  t still should b
4f510 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  e obtained.** fr
4f520 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73 71 6c 69  om a single sqli
4f530 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 42 75 74  teMalloc().  But
4f540 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d 61 64 65   no copy is made
4f550 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e 67   and the calling
4f560 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  .** function sho
4f570 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79 20 74 6f  uld *not* try to
4f580 20 66 72 65 65 20 74 68 65 20 4b 65 79 49 6e 66   free the KeyInf
4f590 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  o..*/.#define P4
4f5a0 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
4f5b0 20 28 2d 31 36 29 0a 23 64 65 66 69 6e 65 20 50   (-16).#define P
4f5c0 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 43  4_KEYINFO_STATIC
4f5d0 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a 2a 20 54    (-17)../*.** T
4f5e0 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e 61 6d 65  he Vdbe.aColName
4f5f0 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20   array contains 
4f600 35 6e 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65  5n Mem structure
4f610 73 2c 20 77 68 65 72 65 20 6e 20 69 73 20 74 68  s, where n is th
4f620 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  e .** number of 
4f630 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20  columns of data 
4f640 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
4f650 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64  statement..*/.#d
4f660 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 41  efine COLNAME_NA
4f670 4d 45 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65  ME     0.#define
4f680 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50   COLNAME_DECLTYP
4f690 45 20 31 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e  E 1.#define COLN
4f6a0 41 4d 45 5f 44 41 54 41 42 41 53 45 20 32 0a 23  AME_DATABASE 2.#
4f6b0 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 54  define COLNAME_T
4f6c0 41 42 4c 45 20 20 20 20 33 0a 23 64 65 66 69 6e  ABLE    3.#defin
4f6d0 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e  e COLNAME_COLUMN
4f6e0 20 20 20 34 0a 23 69 66 64 65 66 20 53 51 4c 49     4.#ifdef SQLI
4f6f0 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
4f700 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65 66 69  _METADATA.# defi
4f710 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20  ne COLNAME_N    
4f720 20 20 20 20 35 20 20 20 20 20 20 2f 2a 20 4e 75      5      /* Nu
4f730 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41 4d 45 5f  mber of COLNAME_
4f740 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a 23  xxx symbols */.#
4f750 65 6c 73 65 0a 23 20 69 66 64 65 66 20 53 51 4c  else.# ifdef SQL
4f760 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50  ITE_OMIT_DECLTYP
4f770 45 0a 23 20 20 20 64 65 66 69 6e 65 20 43 4f 4c  E.#   define COL
4f780 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31 20 20 20  NAME_N      1   
4f790 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f 6e 6c 79     /* Store only
4f7a0 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 23 20 65   the name */.# e
4f7b0 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 20 43  lse.#   define C
4f7c0 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 32 20  OLNAME_N      2 
4f7d0 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68       /* Store th
4f7e0 65 20 6e 61 6d 65 20 61 6e 64 20 64 65 63 6c 74  e name and declt
4f7f0 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69 66 0a 23  ype */.# endif.#
4f800 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
4f810 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
4f820 20 63 6f 6e 76 65 72 74 73 20 61 20 72 65 6c 61   converts a rela
4f830 74 69 76 65 20 61 64 64 72 65 73 73 20 69 6e 20  tive address in 
4f840 74 68 65 20 70 32 20 66 69 65 6c 64 0a 2a 2a 20  the p2 field.** 
4f850 6f 66 20 61 20 56 64 62 65 4f 70 20 73 74 72 75  of a VdbeOp stru
4f860 63 74 75 72 65 20 69 6e 74 6f 20 61 20 6e 65 67  cture into a neg
4f870 61 74 69 76 65 20 6e 75 6d 62 65 72 20 73 6f 20  ative number so 
4f880 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69 74 65 33  that .** sqlite3
4f890 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 29 20  VdbeAddOpList() 
4f8a0 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 65 20 61  knows that the a
4f8b0 64 64 72 65 73 73 20 69 73 20 72 65 6c 61 74 69  ddress is relati
4f8c0 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a 2a 2a 20  ve.  Calling.** 
4f8d0 74 68 65 20 6d 61 63 72 6f 20 61 67 61 69 6e 20  the macro again 
4f8e0 72 65 73 74 6f 72 65 73 20 74 68 65 20 61 64 64  restores the add
4f8f0 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ress..*/.#define
4f900 20 41 44 44 52 28 58 29 20 20 28 2d 31 2d 28 58   ADDR(X)  (-1-(X
4f910 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  ))../*.** The ma
4f920 6b 65 66 69 6c 65 20 73 63 61 6e 73 20 74 68 65  kefile scans the
4f930 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65 20 66   vdbe.c source f
4f940 69 6c 65 20 61 6e 64 20 63 72 65 61 74 65 73 20  ile and creates 
4f950 74 68 65 20 22 6f 70 63 6f 64 65 73 2e 68 22 0a  the "opcodes.h".
4f960 2a 2a 20 68 65 61 64 65 72 20 66 69 6c 65 20 74  ** header file t
4f970 68 61 74 20 64 65 66 69 6e 65 73 20 61 20 6e 75  hat defines a nu
4f980 6d 62 65 72 20 66 6f 72 20 65 61 63 68 20 6f 70  mber for each op
4f990 63 6f 64 65 20 75 73 65 64 20 62 79 20 74 68 65  code used by the
4f9a0 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a   VDBE..*/./*****
4f9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
4f9c0 65 20 6f 70 63 6f 64 65 73 2e 68 20 69 6e 20 74  e opcodes.h in t
4f9d0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76 64 62  he middle of vdb
4f9e0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
4f9f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
4fa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
4fa10 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e 68 20 2a  file opcodes.h *
4fa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4fa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4fa40 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75 74  ********/./* Aut
4fa50 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72  omatically gener
4fa60 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 65 64  ated.  Do not ed
4fa70 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 74 68 65  it */./* See the
4fa80 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73   mkopcodeh.awk s
4fa90 63 72 69 70 74 20 66 6f 72 20 64 65 74 61 69 6c  cript for detail
4faa0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f  s */.#define OP_
4fab0 47 6f 74 6f 20 20 20 20 20 20 20 20 20 20 20 20  Goto            
4fac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fad0 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 4f       1.#define O
4fae0 50 5f 47 6f 73 75 62 20 20 20 20 20 20 20 20 20  P_Gosub         
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 32 0a 23 64 65 66 69 6e 65         2.#define
4fb10 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 20 20 20   OP_Return      
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 33 0a 23 64 65 66 69           3.#defi
4fb40 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20 20 20  ne OP_Yield     
4fb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb60 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65             4.#de
4fb70 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75  fine OP_HaltIfNu
4fb80 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
4fb90 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23               5.#
4fba0 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 20 20  define OP_Halt  
4fbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
4fbd0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65  .#define OP_Inte
4fbe0 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20  ger             
4fbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc00 20 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e   7.#define OP_In
4fc10 74 36 34 20 20 20 20 20 20 20 20 20 20 20 20 20  t64             
4fc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc30 20 20 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f     8.#define OP_
4fc40 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 20  Real            
4fc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc60 20 20 20 31 33 30 20 20 20 2f 2a 20 73 61 6d 65     130   /* same
4fc70 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20   as TK_FLOAT    
4fc80 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74  */.#define OP_St
4fc90 72 69 6e 67 38 20 20 20 20 20 20 20 20 20 20 20  ring8           
4fca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcb0 20 20 39 34 20 20 20 2f 2a 20 73 61 6d 65 20 61    94   /* same a
4fcc0 73 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 2a 2f  s TK_STRING   */
4fcd0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74 72 69  .#define OP_Stri
4fce0 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ng              
4fcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd00 20 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75   9.#define OP_Nu
4fd10 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
4fd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd30 20 20 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f    10.#define OP_
4fd40 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20  Blob            
4fd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd60 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 4f      11.#define O
4fd70 50 5f 56 61 72 69 61 62 6c 65 20 20 20 20 20 20  P_Variable      
4fd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd90 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65        12.#define
4fda0 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20 20 20 20   OP_Move        
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 31 33 0a 23 64 65 66 69          13.#defi
4fdd0 6e 65 20 4f 50 5f 43 6f 70 79 20 20 20 20 20 20  ne OP_Copy      
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 31 34 0a 23 64 65            14.#de
4fe00 66 69 6e 65 20 4f 50 5f 53 43 6f 70 79 20 20 20  fine OP_SCopy   
4fe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23              15.#
4fe30 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75 6c 74  define OP_Result
4fe40 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 20 20  Row             
4fe50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36                16
4fe60 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 63  .#define OP_Conc
4fe70 61 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  at              
4fe80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe90 39 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  91   /* same as 
4fea0 54 4b 5f 43 4f 4e 43 41 54 20 20 20 2a 2f 0a 23  TK_CONCAT   */.#
4feb0 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 20 20 20  define OP_Add   
4fec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36                86
4fee0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
4fef0 5f 50 4c 55 53 20 20 20 20 20 2a 2f 0a 23 64 65  _PLUS     */.#de
4ff00 66 69 6e 65 20 4f 50 5f 53 75 62 74 72 61 63 74  fine OP_Subtract
4ff10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff20 20 20 20 20 20 20 20 20 20 20 20 20 38 37 20 20              87  
4ff30 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d   /* same as TK_M
4ff40 49 4e 55 53 20 20 20 20 2a 2f 0a 23 64 65 66 69  INUS    */.#defi
4ff50 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 20 20  ne OP_Multiply  
4ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff70 20 20 20 20 20 20 20 20 20 20 38 38 20 20 20 2f            88   /
4ff80 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 41  * same as TK_STA
4ff90 52 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  R     */.#define
4ffa0 20 4f 50 5f 44 69 76 69 64 65 20 20 20 20 20 20   OP_Divide      
4ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffc0 20 20 20 20 20 20 20 20 38 39 20 20 20 2f 2a 20          89   /* 
4ffd0 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48  same as TK_SLASH
4ffe0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
4fff0 50 5f 52 65 6d 61 69 6e 64 65 72 20 20 20 20 20  P_Remainder     
50000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50010 20 20 20 20 20 20 39 30 20 20 20 2f 2a 20 73 61        90   /* sa
50020 6d 65 20 61 73 20 54 4b 5f 52 45 4d 20 20 20 20  me as TK_REM    
50030 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
50040 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 20 20 20  CollSeq         
50050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50060 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 20 4f      17.#define O
50070 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 20 20  P_Function      
50080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50090 20 20 20 20 20 20 31 38 0a 23 64 65 66 69 6e 65        18.#define
500a0 20 4f 50 5f 42 69 74 41 6e 64 20 20 20 20 20 20   OP_BitAnd      
500b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500c0 20 20 20 20 20 20 20 20 38 32 20 20 20 2f 2a 20          82   /* 
500d0 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 41 4e  same as TK_BITAN
500e0 44 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  D   */.#define O
500f0 50 5f 42 69 74 4f 72 20 20 20 20 20 20 20 20 20  P_BitOr         
50100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50110 20 20 20 20 20 20 38 33 20 20 20 2f 2a 20 73 61        83   /* sa
50120 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 20 20  me as TK_BITOR  
50130 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
50140 53 68 69 66 74 4c 65 66 74 20 20 20 20 20 20 20  ShiftLeft       
50150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50160 20 20 20 20 38 34 20 20 20 2f 2a 20 73 61 6d 65      84   /* same
50170 20 61 73 20 54 4b 5f 4c 53 48 49 46 54 20 20 20   as TK_LSHIFT   
50180 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 68  */.#define OP_Sh
50190 69 66 74 52 69 67 68 74 20 20 20 20 20 20 20 20  iftRight        
501a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501b0 20 20 38 35 20 20 20 2f 2a 20 73 61 6d 65 20 61    85   /* same a
501c0 73 20 54 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f  s TK_RSHIFT   */
501d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 49  .#define OP_AddI
501e0 6d 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  mm              
501f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50200 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75  20.#define OP_Mu
50210 73 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20  stBeInt         
50220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50230 20 20 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f    21.#define OP_
50240 52 65 61 6c 41 66 66 69 6e 69 74 79 20 20 20 20  RealAffinity    
50250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50260 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 4f      22.#define O
50270 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20 20 20  P_ToText        
50280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50290 20 20 20 20 20 31 34 31 20 20 20 2f 2a 20 73 61       141   /* sa
502a0 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 54  me as TK_TO_TEXT
502b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
502c0 54 6f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20  ToBlob          
502d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
502e0 20 20 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65     142   /* same
502f0 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20   as TK_TO_BLOB  
50300 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f  */.#define OP_To
50310 4e 75 6d 65 72 69 63 20 20 20 20 20 20 20 20 20  Numeric         
50320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50330 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 20 61   143   /* same a
50340 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a  s TK_TO_NUMERIC*
50350 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 49  /.#define OP_ToI
50360 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
50370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50380 31 34 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73  144   /* same as
50390 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 2a 2f 0a   TK_TO_INT   */.
503a0 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 52 65 61  #define OP_ToRea
503b0 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
503c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
503d0 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  5   /* same as T
503e0 4b 5f 54 4f 5f 52 45 41 4c 20 20 2a 2f 0a 23 64  K_TO_REAL  */.#d
503f0 65 66 69 6e 65 20 4f 50 5f 45 71 20 20 20 20 20  efine OP_Eq     
50400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50410 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 20               76 
50420 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
50430 45 51 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  EQ       */.#def
50440 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20 20  ine OP_Ne       
50450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50460 20 20 20 20 20 20 20 20 20 20 20 37 35 20 20 20             75   
50470 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45  /* same as TK_NE
50480 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
50490 65 20 4f 50 5f 4c 74 20 20 20 20 20 20 20 20 20  e OP_Lt         
504a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504b0 20 20 20 20 20 20 20 20 20 37 39 20 20 20 2f 2a           79   /*
504c0 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 20 20   same as TK_LT  
504d0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
504e0 4f 50 5f 4c 65 20 20 20 20 20 20 20 20 20 20 20  OP_Le           
504f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50500 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a 20 73         78   /* s
50510 61 6d 65 20 61 73 20 54 4b 5f 4c 45 20 20 20 20  ame as TK_LE    
50520 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
50530 5f 47 74 20 20 20 20 20 20 20 20 20 20 20 20 20  _Gt             
50540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50550 20 20 20 20 20 37 37 20 20 20 2f 2a 20 73 61 6d       77   /* sam
50560 65 20 61 73 20 54 4b 5f 47 54 20 20 20 20 20 20  e as TK_GT      
50570 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47   */.#define OP_G
50580 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
50590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505a0 20 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 65 20     80   /* same 
505b0 61 73 20 54 4b 5f 47 45 20 20 20 20 20 20 20 2a  as TK_GE       *
505c0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 65 72  /.#define OP_Per
505d0 6d 75 74 61 74 69 6f 6e 20 20 20 20 20 20 20 20  mutation        
505e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505f0 20 32 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43   23.#define OP_C
50600 6f 6d 70 61 72 65 20 20 20 20 20 20 20 20 20 20  ompare          
50610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50620 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 4f 50     24.#define OP
50630 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 20 20 20  _Jump           
50640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50650 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20       25.#define 
50660 4f 50 5f 41 6e 64 20 20 20 20 20 20 20 20 20 20  OP_And          
50670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50680 20 20 20 20 20 20 20 36 39 20 20 20 2f 2a 20 73         69   /* s
50690 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 20 20 20  ame as TK_AND   
506a0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
506b0 5f 4f 72 20 20 20 20 20 20 20 20 20 20 20 20 20  _Or             
506c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506d0 20 20 20 20 20 36 38 20 20 20 2f 2a 20 73 61 6d       68   /* sam
506e0 65 20 61 73 20 54 4b 5f 4f 52 20 20 20 20 20 20  e as TK_OR      
506f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e   */.#define OP_N
50700 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ot              
50710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50720 20 20 20 31 39 20 20 20 2f 2a 20 73 61 6d 65 20     19   /* same 
50730 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 2a  as TK_NOT      *
50740 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74  /.#define OP_Bit
50750 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  Not             
50760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50770 20 39 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   93   /* same as
50780 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a   TK_BITNOT   */.
50790 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 20 20 20  #define OP_If   
507a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
507c0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4e  6.#define OP_IfN
507d0 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ot              
507e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507f0 20 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   27.#define OP_I
50800 73 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20  sNull           
50810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50820 20 20 20 37 33 20 20 20 2f 2a 20 73 61 6d 65 20     73   /* same 
50830 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 2a  as TK_ISNULL   *
50840 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74  /.#define OP_Not
50850 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  Null            
50860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50870 20 37 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   74   /* same as
50880 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 2a 2f 0a   TK_NOTNULL  */.
50890 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d  #define OP_Colum
508a0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
508b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
508c0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 66 66  8.#define OP_Aff
508d0 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 20 20  inity           
508e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508f0 20 32 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d   29.#define OP_M
50900 61 6b 65 52 65 63 6f 72 64 20 20 20 20 20 20 20  akeRecord       
50910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50920 20 20 20 33 30 0a 23 64 65 66 69 6e 65 20 4f 50     30.#define OP
50930 5f 43 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20  _Count          
50940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50950 20 20 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20       31.#define 
50960 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20  OP_Savepoint    
50970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50980 20 20 20 20 20 20 20 33 32 0a 23 64 65 66 69 6e         32.#defin
50990 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20  e OP_AutoCommit 
509a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509b0 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65 66           33.#def
509c0 69 6e 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69  ine OP_Transacti
509d0 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
509e0 20 20 20 20 20 20 20 20 20 20 20 33 34 0a 23 64             34.#d
509f0 65 66 69 6e 65 20 4f 50 5f 52 65 61 64 43 6f 6f  efine OP_ReadCoo
50a00 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20  kie             
50a10 20 20 20 20 20 20 20 20 20 20 20 20 20 33 35 0a               35.
50a20 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74 43 6f  #define OP_SetCo
50a30 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20  okie            
50a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
50a50 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 65 72  6.#define OP_Ver
50a60 69 66 79 43 6f 6f 6b 69 65 20 20 20 20 20 20 20  ifyCookie       
50a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a80 20 33 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f   37.#define OP_O
50a90 70 65 6e 52 65 61 64 20 20 20 20 20 20 20 20 20  penRead         
50aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ab0 20 20 20 33 38 0a 23 64 65 66 69 6e 65 20 4f 50     38.#define OP
50ac0 5f 4f 70 65 6e 57 72 69 74 65 20 20 20 20 20 20  _OpenWrite      
50ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ae0 20 20 20 20 20 33 39 0a 23 64 65 66 69 6e 65 20       39.#define 
50af0 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
50b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b10 20 20 20 20 20 20 20 34 30 0a 23 64 65 66 69 6e         40.#defin
50b20 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 20  e OP_OpenPseudo 
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 34 31 0a 23 64 65 66           41.#def
50b50 69 6e 65 20 4f 50 5f 43 6c 6f 73 65 20 20 20 20  ine OP_Close    
50b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b70 20 20 20 20 20 20 20 20 20 20 20 34 32 0a 23 64             42.#d
50b80 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 20  efine OP_SeekLt 
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 33 0a               43.
50bb0 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c  #define OP_SeekL
50bc0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
50bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
50be0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65  4.#define OP_See
50bf0 6b 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20  kGe             
50c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c10 20 34 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   45.#define OP_S
50c20 65 65 6b 47 74 20 20 20 20 20 20 20 20 20 20 20  eekGt           
50c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c40 20 20 20 34 36 0a 23 64 65 66 69 6e 65 20 4f 50     46.#define OP
50c50 5f 53 65 65 6b 20 20 20 20 20 20 20 20 20 20 20  _Seek           
50c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c70 20 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20       47.#define 
50c80 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20  OP_NotFound     
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 34 38 0a 23 64 65 66 69 6e         48.#defin
50cb0 65 20 4f 50 5f 46 6f 75 6e 64 20 20 20 20 20 20  e OP_Found      
50cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cd0 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 66           49.#def
50ce0 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20  ine OP_IsUnique 
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d00 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 64             50.#d
50d10 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73  efine OP_NotExis
50d20 74 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ts              
50d30 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 0a               51.
50d40 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 71 75 65  #define OP_Seque
50d50 6e 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20  nce             
50d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
50d70 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 77  2.#define OP_New
50d80 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20  Rowid           
50d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50da0 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   53.#define OP_I
50db0 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20  nsert           
50dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50dd0 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 4f 50     54.#define OP
50de0 5f 49 6e 73 65 72 74 49 6e 74 20 20 20 20 20 20  _InsertInt      
50df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e00 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65 20       55.#define 
50e10 4f 50 5f 44 65 6c 65 74 65 20 20 20 20 20 20 20  OP_Delete       
50e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e30 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e         56.#defin
50e40 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 20  e OP_ResetCount 
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 35 37 0a 23 64 65 66           57.#def
50e70 69 6e 65 20 4f 50 5f 52 6f 77 4b 65 79 20 20 20  ine OP_RowKey   
50e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e90 20 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64             58.#d
50ea0 65 66 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61  efine OP_RowData
50eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a               59.
50ed0 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64  #define OP_Rowid
50ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
50f00 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 6c  0.#define OP_Nul
50f10 6c 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 20  lRow            
50f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f30 20 36 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c   61.#define OP_L
50f40 61 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ast             
50f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f60 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20 4f 50     62.#define OP
50f70 5f 53 6f 72 74 20 20 20 20 20 20 20 20 20 20 20  _Sort           
50f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f90 20 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20       63.#define 
50fa0 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20 20  OP_Rewind       
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 36 34 0a 23 64 65 66 69 6e         64.#defin
50fd0 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20 20  e OP_Prev       
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 36 35 0a 23 64 65 66           65.#def
51000 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 20 20  ine OP_Next     
51010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51020 20 20 20 20 20 20 20 20 20 20 20 36 36 0a 23 64             66.#d
51030 65 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73 65  efine OP_IdxInse
51040 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rt              
51050 20 20 20 20 20 20 20 20 20 20 20 20 20 36 37 0a               67.
51060 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 44 65  #define OP_IdxDe
51070 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20  lete            
51080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
51090 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78  0.#define OP_Idx
510a0 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20  Rowid           
510b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510c0 20 37 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   71.#define OP_I
510d0 64 78 4c 54 20 20 20 20 20 20 20 20 20 20 20 20  dxLT            
510e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510f0 20 20 20 37 32 0a 23 64 65 66 69 6e 65 20 4f 50     72.#define OP
51100 5f 49 64 78 47 45 20 20 20 20 20 20 20 20 20 20  _IdxGE          
51110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51120 20 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65 20       81.#define 
51130 4f 50 5f 44 65 73 74 72 6f 79 20 20 20 20 20 20  OP_Destroy      
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 39 32 0a 23 64 65 66 69 6e         92.#defin
51160 65 20 4f 50 5f 43 6c 65 61 72 20 20 20 20 20 20  e OP_Clear      
51170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51180 20 20 20 20 20 20 20 20 20 39 35 0a 23 64 65 66           95.#def
51190 69 6e 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64  ine OP_CreateInd
511a0 65 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ex              
511b0 20 20 20 20 20 20 20 20 20 20 20 39 36 0a 23 64             96.#d
511c0 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 65 54  efine OP_CreateT
511d0 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20  able            
511e0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 37 0a               97.
511f0 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 72 73 65  #define OP_Parse
51200 53 63 68 65 6d 61 20 20 20 20 20 20 20 20 20 20  Schema          
51210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
51220 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 6f 61  8.#define OP_Loa
51230 64 41 6e 61 6c 79 73 69 73 20 20 20 20 20 20 20  dAnalysis       
51240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51250 20 39 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44   99.#define OP_D
51260 72 6f 70 54 61 62 6c 65 20 20 20 20 20 20 20 20  ropTable        
51270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51280 20 20 31 30 30 0a 23 64 65 66 69 6e 65 20 4f 50    100.#define OP
51290 5f 44 72 6f 70 49 6e 64 65 78 20 20 20 20 20 20  _DropIndex      
512a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
512b0 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20      101.#define 
512c0 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 20 20  OP_DropTrigger  
512d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
512e0 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69 6e        102.#defin
512f0 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b  e OP_IntegrityCk
51300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51310 20 20 20 20 20 20 20 20 31 30 33 0a 23 64 65 66          103.#def
51320 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64  ine OP_RowSetAdd
51330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51340 20 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64            104.#d
51350 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 52  efine OP_RowSetR
51360 65 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20  ead             
51370 20 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a              105.
51380 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65  #define OP_RowSe
51390 74 54 65 73 74 20 20 20 20 20 20 20 20 20 20 20  tTest           
513a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
513b0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72 6f  6.#define OP_Pro
513c0 67 72 61 6d 20 20 20 20 20 20 20 20 20 20 20 20  gram            
513d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
513e0 31 30 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50  107.#define OP_P
513f0 61 72 61 6d 20 20 20 20 20 20 20 20 20 20 20 20  aram            
51400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51410 20 20 31 30 38 0a 23 64 65 66 69 6e 65 20 4f 50    108.#define OP
51420 5f 46 6b 43 6f 75 6e 74 65 72 20 20 20 20 20 20  _FkCounter      
51430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51440 20 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20      109.#define 
51450 4f 50 5f 46 6b 49 66 5a 65 72 6f 20 20 20 20 20  OP_FkIfZero     
51460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51470 20 20 20 20 20 20 31 31 30 0a 23 64 65 66 69 6e        110.#defin
51480 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20  e OP_MemMax     
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 31 31 31 0a 23 64 65 66          111.#def
514b0 69 6e 65 20 4f 50 5f 49 66 50 6f 73 20 20 20 20  ine OP_IfPos    
514c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514d0 20 20 20 20 20 20 20 20 20 20 31 31 32 0a 23 64            112.#d
514e0 65 66 69 6e 65 20 4f 50 5f 49 66 4e 65 67 20 20  efine OP_IfNeg  
514f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51500 20 20 20 20 20 20 20 20 20 20 20 20 31 31 33 0a              113.
51510 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72  #define OP_IfZer
51520 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o               
51530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
51540 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 67 67  4.#define OP_Agg
51550 53 74 65 70 20 20 20 20 20 20 20 20 20 20 20 20  Step            
51560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51570 31 31 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41  115.#define OP_A
51580 67 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20 20  ggFinal         
51590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
515a0 20 20 31 31 36 0a 23 64 65 66 69 6e 65 20 4f 50    116.#define OP
515b0 5f 56 61 63 75 75 6d 20 20 20 20 20 20 20 20 20  _Vacuum         
515c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
515d0 20 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20      117.#define 
515e0 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 20 20 20  OP_IncrVacuum   
515f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51600 20 20 20 20 20 20 31 31 38 0a 23 64 65 66 69 6e        118.#defin
51610 65 20 4f 50 5f 45 78 70 69 72 65 20 20 20 20 20  e OP_Expire     
51620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51630 20 20 20 20 20 20 20 20 31 31 39 0a 23 64 65 66          119.#def
51640 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b  ine OP_TableLock
51650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51660 20 20 20 20 20 20 20 20 20 20 31 32 30 0a 23 64            120.#d
51670 65 66 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20  efine OP_VBegin 
51680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51690 20 20 20 20 20 20 20 20 20 20 20 20 31 32 31 0a              121.
516a0 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65 61  #define OP_VCrea
516b0 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  te              
516c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
516d0 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 44 65  2.#define OP_VDe
516e0 73 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20  stroy           
516f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51700 31 32 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56  123.#define OP_V
51710 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20  Open            
51720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51730 20 20 31 32 34 0a 23 64 65 66 69 6e 65 20 4f 50    124.#define OP
51740 5f 56 46 69 6c 74 65 72 20 20 20 20 20 20 20 20  _VFilter        
51750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51760 20 20 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20      125.#define 
51770 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20 20 20 20 20  OP_VColumn      
51780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51790 20 20 20 20 20 20 31 32 36 0a 23 64 65 66 69 6e        126.#defin
517a0 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 20 20 20  e OP_VNext      
517b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
517c0 20 20 20 20 20 20 20 20 31 32 37 0a 23 64 65 66          127.#def
517d0 69 6e 65 20 4f 50 5f 56 52 65 6e 61 6d 65 20 20  ine OP_VRename  
517e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
517f0 20 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64            128.#d
51800 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 61 74 65  efine OP_VUpdate
51810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51820 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39 0a              129.
51830 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 67 65 63  #define OP_Pagec
51840 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  ount            
51850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
51860 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 72 61  1.#define OP_Tra
51870 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ce              
51880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51890 31 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e  132.#define OP_N
518a0 6f 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 20  oop             
518b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
518c0 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 4f 50    133.#define OP
518d0 5f 45 78 70 6c 61 69 6e 20 20 20 20 20 20 20 20  _Explain        
518e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
518f0 20 20 20 20 31 33 34 0a 0a 2f 2a 20 54 68 65 20      134../* The 
51900 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64 65  following opcode
51910 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 76 65   values are neve
51920 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e  r used */.#defin
51930 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 35  e OP_NotUsed_135
51940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51950 20 20 20 20 20 20 20 20 31 33 35 0a 23 64 65 66          135.#def
51960 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31  ine OP_NotUsed_1
51970 33 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20  36              
51980 20 20 20 20 20 20 20 20 20 20 31 33 36 0a 23 64            136.#d
51990 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64  efine OP_NotUsed
519a0 5f 31 33 37 20 20 20 20 20 20 20 20 20 20 20 20  _137            
519b0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 37 0a              137.
519c0 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73  #define OP_NotUs
519d0 65 64 5f 31 33 38 20 20 20 20 20 20 20 20 20 20  ed_138          
519e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
519f0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74  8.#define OP_Not
51a00 55 73 65 64 5f 31 33 39 20 20 20 20 20 20 20 20  Used_139        
51a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51a20 31 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e  139.#define OP_N
51a30 6f 74 55 73 65 64 5f 31 34 30 20 20 20 20 20 20  otUsed_140      
51a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51a50 20 20 31 34 30 0a 0a 0a 2f 2a 20 50 72 6f 70 65    140.../* Prope
51a60 72 74 69 65 73 20 73 75 63 68 20 61 73 20 22 6f  rties such as "o
51a70 75 74 32 22 20 6f 72 20 22 6a 75 6d 70 22 20 74  ut2" or "jump" t
51a80 68 61 74 20 61 72 65 20 73 70 65 63 69 66 69 65  hat are specifie
51a90 64 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73  d in.** comments
51aa0 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22   following the "
51ab0 63 61 73 65 22 20 66 6f 72 20 65 61 63 68 20 6f  case" for each o
51ac0 70 63 6f 64 65 20 69 6e 20 74 68 65 20 76 64 62  pcode in the vdb
51ad0 65 2e 63 0a 2a 2a 20 61 72 65 20 65 6e 63 6f 64  e.c.** are encod
51ae0 65 64 20 69 6e 74 6f 20 62 69 74 76 65 63 74 6f  ed into bitvecto
51af0 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  rs as follows:.*
51b00 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f  /.#define OPFLG_
51b10 4a 55 4d 50 20 20 20 20 20 20 20 20 20 20 20 20  JUMP            
51b20 30 78 30 30 30 31 20 20 2f 2a 20 6a 75 6d 70 3a  0x0001  /* jump:
51b30 20 20 50 32 20 68 6f 6c 64 73 20 6a 6d 70 20 74    P2 holds jmp t
51b40 61 72 67 65 74 20 2a 2f 0a 23 64 65 66 69 6e 65  arget */.#define
51b50 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52   OPFLG_OUT2_PRER
51b60 45 4c 45 41 53 45 20 30 78 30 30 30 32 20 20 2f  ELEASE 0x0002  /
51b70 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
51b80 65 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  e: */.#define OP
51b90 46 4c 47 5f 49 4e 31 20 20 20 20 20 20 20 20 20  FLG_IN1         
51ba0 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 69      0x0004  /* i
51bb0 6e 31 3a 20 20 20 50 31 20 69 73 20 61 6e 20 69  n1:   P1 is an i
51bc0 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nput */.#define 
51bd0 4f 50 46 4c 47 5f 49 4e 32 20 20 20 20 20 20 20  OPFLG_IN2       
51be0 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a        0x0008  /*
51bf0 20 69 6e 32 3a 20 20 20 50 32 20 69 73 20 61 6e   in2:   P2 is an
51c00 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e   input */.#defin
51c10 65 20 4f 50 46 4c 47 5f 49 4e 33 20 20 20 20 20  e OPFLG_IN3     
51c20 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20 20          0x0010  
51c30 2f 2a 20 69 6e 33 3a 20 20 20 50 33 20 69 73 20  /* in3:   P3 is 
51c40 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66  an input */.#def
51c50 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20 20  ine OPFLG_OUT3  
51c60 20 20 20 20 20 20 20 20 20 20 30 78 30 30 32 30            0x0020
51c70 20 20 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20 69    /* out3:  P3 i
51c80 73 20 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a 23  s an output */.#
51c90 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 49  define OPFLG_INI
51ca0 54 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20  TIALIZER {\./*  
51cb0 20 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31   0 */ 0x00, 0x01
51cc0 2c 20 30 78 30 31 2c 20 30 78 30 34 2c 20 30 78  , 0x01, 0x04, 0x
51cd0 30 34 2c 20 30 78 31 30 2c 20 30 78 30 30 2c 20  04, 0x10, 0x00, 
51ce0 30 78 30 32 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f  0x02,\./*   8 */
51cf0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
51d00 32 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  2, 0x02, 0x00, 0
51d10 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 34 2c  x00, 0x04, 0x04,
51d20 5c 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 30  \./*  16 */ 0x00
51d30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
51d40 30 34 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20  04, 0x04, 0x05, 
51d50 30 78 30 34 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20  0x04, 0x00,\./* 
51d60 20 32 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30   24 */ 0x00, 0x0
51d70 31 2c 20 30 78 30 35 2c 20 30 78 30 35 2c 20 30  1, 0x05, 0x05, 0
51d80 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
51d90 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 33 32 20 2a   0x02,\./*  32 *
51da0 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  / 0x00, 0x00, 0x
51db0 30 30 2c 20 30 78 30 32 2c 20 30 78 31 30 2c 20  00, 0x02, 0x10, 
51dc0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
51dd0 2c 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78 30  ,\./*  40 */ 0x0
51de0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
51df0 78 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31 2c  x11, 0x11, 0x11,
51e00 20 30 78 31 31 2c 20 30 78 30 38 2c 5c 0a 2f 2a   0x11, 0x08,\./*
51e10 20 20 34 38 20 2a 2f 20 30 78 31 31 2c 20 30 78    48 */ 0x11, 0x
51e20 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20  11, 0x11, 0x11, 
51e30 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 30  0x02, 0x02, 0x00
51e40 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 35 36 20  , 0x00,\./*  56 
51e50 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  */ 0x00, 0x00, 0
51e60 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32 2c  x00, 0x00, 0x02,
51e70 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
51e80 31 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30 78  1,\./*  64 */ 0x
51e90 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20  01, 0x01, 0x01, 
51ea0 30 78 30 38 2c 20 30 78 32 63 2c 20 30 78 32 63  0x08, 0x2c, 0x2c
51eb0 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 2f  , 0x00, 0x02,\./
51ec0 2a 20 20 37 32 20 2a 2f 20 30 78 31 31 2c 20 30  *  72 */ 0x11, 0
51ed0 78 30 35 2c 20 30 78 30 35 2c 20 30 78 31 35 2c  x05, 0x05, 0x15,
51ee0 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31   0x15, 0x15, 0x1
51ef0 35 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20 20 38 30  5, 0x15,\./*  80
51f00 20 2a 2f 20 30 78 31 35 2c 20 30 78 31 31 2c 20   */ 0x15, 0x11, 
51f10 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63  0x2c, 0x2c, 0x2c
51f20 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78  , 0x2c, 0x2c, 0x
51f30 32 63 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20 30  2c,\./*  88 */ 0
51f40 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c  x2c, 0x2c, 0x2c,
51f50 20 30 78 32 63 2c 20 30 78 30 32 2c 20 30 78 30   0x2c, 0x02, 0x0
51f60 34 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a  4, 0x02, 0x00,\.
51f70 2f 2a 20 20 39 36 20 2a 2f 20 30 78 30 32 2c 20  /*  96 */ 0x02, 
51f80 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30  0x02, 0x00, 0x00
51f90 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
51fa0 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 30  00, 0x00,\./* 10
51fb0 34 20 2a 2f 20 30 78 30 38 2c 20 30 78 32 31 2c  4 */ 0x08, 0x21,
51fc0 20 30 78 31 35 2c 20 30 78 30 31 2c 20 30 78 30   0x15, 0x01, 0x0
51fd0 32 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30  2, 0x00, 0x01, 0
51fe0 78 30 38 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20  x08,\./* 112 */ 
51ff0 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 30 35  0x05, 0x05, 0x05
52000 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
52010 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 5c  00, 0x01, 0x00,\
52020 0a 2f 2a 20 31 32 30 20 2a 2f 20 30 78 30 30 2c  ./* 120 */ 0x00,
52030 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
52040 30 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30  0, 0x00, 0x01, 0
52050 78 30 30 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 31  x00, 0x01,\./* 1
52060 32 38 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30  28 */ 0x00, 0x00
52070 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
52080 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
52090 30 78 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f  0x00,\./* 136 */
520a0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
520b0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
520c0 78 30 34 2c 20 30 78 30 34 2c 20 30 78 30 34 2c  x04, 0x04, 0x04,
520d0 5c 0a 2f 2a 20 31 34 34 20 2a 2f 20 30 78 30 34  \./* 144 */ 0x04
520e0 2c 20 30 78 30 34 2c 7d 0a 0a 2f 2a 2a 2a 2a 2a  , 0x04,}../*****
520f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
52100 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a   opcodes.h *****
52110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52130 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
52140 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
52150 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
52160 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 68  ft off in vdbe.h
52170 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
52180 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
52190 20 50 72 6f 74 6f 74 79 70 65 73 20 66 6f 72 20   Prototypes for 
521a0 74 68 65 20 56 44 42 45 20 69 6e 74 65 72 66 61  the VDBE interfa
521b0 63 65 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74  ce.  See comment
521c0 73 20 6f 6e 20 74 68 65 20 69 6d 70 6c 65 6d 65  s on the impleme
521d0 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61  ntation.** for a
521e0 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20   description of 
521f0 77 68 61 74 20 65 61 63 68 20 6f 66 20 74 68 65  what each of the
52200 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 65 73  se routines does
52210 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
52220 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65  ATE Vdbe *sqlite
52230 33 56 64 62 65 43 72 65 61 74 65 28 73 71 6c 69  3VdbeCreate(sqli
52240 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
52250 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
52260 33 56 64 62 65 41 64 64 4f 70 30 28 56 64 62 65  3VdbeAddOp0(Vdbe
52270 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
52280 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
52290 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64 62  e3VdbeAddOp1(Vdb
522a0 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  e*,int,int);.SQL
522b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
522c0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
522d0 32 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c  2(Vdbe*,int,int,
522e0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
522f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
52300 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 65 2a  VdbeAddOp3(Vdbe*
52310 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
52320 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
52330 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
52340 65 41 64 64 4f 70 34 28 56 64 62 65 2a 2c 69 6e  eAddOp4(Vdbe*,in
52350 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 63 6f  t,int,int,int,co
52360 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 69 6e  nst char *zP4,in
52370 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
52380 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
52390 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 65  beAddOpList(Vdbe
523a0 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65  *, int nOp, Vdbe
523b0 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f  OpList const *aO
523c0 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  p);.SQLITE_PRIVA
523d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
523e0 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65  dbeChangeP1(Vdbe
523f0 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74  *, int addr, int
52400 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   P1);.SQLITE_PRI
52410 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
52420 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 56 64  3VdbeChangeP2(Vd
52430 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69  be*, int addr, i
52440 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54 45 5f 50  nt P2);.SQLITE_P
52450 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
52460 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
52470 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c  Vdbe*, int addr,
52480 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c 49 54 45   int P3);.SQLITE
52490 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
524a0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
524b0 35 28 56 64 62 65 2a 2c 20 75 38 20 50 35 29 3b  5(Vdbe*, u8 P5);
524c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
524d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
524e0 4a 75 6d 70 48 65 72 65 28 56 64 62 65 2a 2c 20  JumpHere(Vdbe*, 
524f0 69 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49 54  int addr);.SQLIT
52500 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52510 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
52520 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c 20 69 6e  ToNoop(Vdbe*, in
52530 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 3b 0a  t addr, int N);.
52540 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
52550 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
52560 68 61 6e 67 65 50 34 28 56 64 62 65 2a 2c 20 69  hangeP4(Vdbe*, i
52570 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63  nt addr, const c
52580 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 4e 29  har *zP4, int N)
52590 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
525a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
525b0 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65 2a  eUsesBtree(Vdbe*
525c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
525d0 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73  RIVATE VdbeOp *s
525e0 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28  qlite3VdbeGetOp(
525f0 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  Vdbe*, int);.SQL
52600 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
52610 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
52620 61 62 65 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c  abel(Vdbe*);.SQL
52630 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
52640 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
52650 74 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  te(Vdbe*);.SQLIT
52660 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52670 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65  qlite3VdbeMakeRe
52680 61 64 79 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  ady(Vdbe*,int,in
52690 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e  t,int,int,int,in
526a0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
526b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
526c0 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 2a  beFinalize(Vdbe*
526d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
526e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
526f0 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56  beResolveLabel(V
52700 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  dbe*, int);.SQLI
52710 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
52720 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
52730 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 69  tAddr(Vdbe*);.#i
52740 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
52750 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  G.SQLITE_PRIVATE
52760 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64     int sqlite3Vd
52770 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74  beAssertMayAbort
52780 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53  (Vdbe *, int);.S
52790 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
527a0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
527b0 54 72 61 63 65 28 56 64 62 65 2a 2c 46 49 4c 45  Trace(Vdbe*,FILE
527c0 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  *);.#endif.SQLIT
527d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
527e0 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 53  qlite3VdbeResetS
527f0 74 65 70 52 65 73 75 6c 74 28 56 64 62 65 2a 29  tepResult(Vdbe*)
52800 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52810 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
52820 52 65 73 65 74 28 56 64 62 65 2a 29 3b 0a 53 51  Reset(Vdbe*);.SQ
52830 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
52840 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  d sqlite3VdbeSet
52850 4e 75 6d 43 6f 6c 73 28 56 64 62 65 2a 2c 69 6e  NumCols(Vdbe*,in
52860 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
52870 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
52880 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64 62  beSetColName(Vdb
52890 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63 6f  e*, int, int, co
528a0 6e 73 74 20 63 68 61 72 20 2a 2c 20 76 6f 69 64  nst char *, void
528b0 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
528c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
528d0 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e   sqlite3VdbeCoun
528e0 74 43 68 61 6e 67 65 73 28 56 64 62 65 2a 29 3b  tChanges(Vdbe*);
528f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52900 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33  sqlite3 *sqlite3
52910 56 64 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 53  VdbeDb(Vdbe*);.S
52920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
52930 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  id sqlite3VdbeSe
52940 74 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73  tSql(Vdbe*, cons
52950 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  t char *z, int n
52960 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
52970 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
52980 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65  te3VdbeSwap(Vdbe
52990 2a 2c 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  *,Vdbe*);.SQLITE
529a0 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20  _PRIVATE VdbeOp 
529b0 2a 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b 65  *sqlite3VdbeTake
529c0 4f 70 41 72 72 61 79 28 56 64 62 65 2a 2c 20 69  OpArray(Vdbe*, i
529d0 6e 74 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  nt*, int*);.SQLI
529e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
529f0 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f 67 72  sqlite3VdbeProgr
52a00 61 6d 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  amDelete(sqlite3
52a10 20 2a 2c 20 53 75 62 50 72 6f 67 72 61 6d 20 2a   *, SubProgram *
52a20 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
52a30 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76  RIVATE sqlite3_v
52a40 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64 62  alue *sqlite3Vdb
52a50 65 47 65 74 56 61 6c 75 65 28 56 64 62 65 2a 2c  eGetValue(Vdbe*,
52a60 20 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49 54   int, u8);.SQLIT
52a70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52a80 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 72  qlite3VdbeSetVar
52a90 6d 61 73 6b 28 56 64 62 65 2a 2c 20 69 6e 74 29  mask(Vdbe*, int)
52aa0 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
52ab0 45 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  E UnpackedRecord
52ac0 20 2a 73 71 6c 69 74 65 33 56 64 62 65 52 65 63   *sqlite3VdbeRec
52ad0 6f 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 6e 66  ordUnpack(KeyInf
52ae0 6f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  o*,int,const voi
52af0 64 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53  d*,char*,int);.S
52b00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
52b10 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65  id sqlite3VdbeDe
52b20 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f  leteUnpackedReco
52b30 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  rd(UnpackedRecor
52b40 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d*);.SQLITE_PRIV
52b50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
52b60 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65  dbeRecordCompare
52b70 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  (int,const void*
52b80 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a  ,UnpackedRecord*
52b90 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  );...#ifndef NDE
52ba0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
52bb0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
52bc0 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62  3VdbeComment(Vdb
52bd0 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e*, const char*,
52be0 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20   ...);.# define 
52bf0 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20 20  VdbeComment(X)  
52c00 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65  sqlite3VdbeComme
52c10 6e 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt X.SQLITE_PRIV
52c20 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
52c30 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e  e3VdbeNoopCommen
52c40 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63  t(Vdbe*, const c
52c50 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65  har*, ...);.# de
52c60 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d  fine VdbeNoopCom
52c70 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 33  ment(X)  sqlite3
52c80 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20  VdbeNoopComment 
52c90 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  X.#else.# define
52ca0 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a   VdbeComment(X).
52cb0 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f  # define VdbeNoo
52cc0 70 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e 64  pComment(X).#end
52cd0 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  if..#endif../***
52ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
52cf0 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a  of vdbe.h ******
52d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
52d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
52d40 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
52d50 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
52d60 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
52d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
52d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
52d90 75 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20 74  ude pager.h in t
52da0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
52db0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
52dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
52dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
52de0 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20 2a  n file pager.h *
52df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
52e20 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
52e30 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
52e40 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
52e50 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
52e60 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
52e70 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
52e80 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
52e90 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
52ea0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
52eb0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
52ec0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
52ed0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
52ee0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
52ef0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
52f00 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
52f10 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
52f20 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
52f30 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
52f40 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
52f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52f90 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
52fa0 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74  r file defines t
52fb0 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61  he interface tha
52fc0 74 20 74 68 65 20 73 71 6c 69 74 65 20 70 61 67  t the sqlite pag
52fd0 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73 79  e cache.** subsy
52fe0 73 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65 20  stem.  The page 
52ff0 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20  cache subsystem 
53000 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 73  reads and writes
53010 20 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a 2a   a file a page.*
53020 2a 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 20  * at a time and 
53030 70 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72 6e  provides a journ
53040 61 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e  al for rollback.
53050 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a  .**.** @(#) $Id:
53060 20 70 61 67 65 72 2e 68 2c 76 20 31 2e 31 30 34   pager.h,v 1.104
53070 20 32 30 30 39 2f 30 37 2f 32 34 20 31 39 3a 30   2009/07/24 19:0
53080 31 3a 31 39 20 64 72 68 20 45 78 70 20 24 0a 2a  1:19 drh Exp $.*
53090 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 45  /..#ifndef _PAGE
530a0 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41  R_H_.#define _PA
530b0 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65  GER_H_../*.** De
530c0 66 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69  fault maximum si
530d0 7a 65 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e  ze for persisten
530e0 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e  t journal files.
530f0 20 41 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20   A negative .** 
53100 76 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c  value means no l
53110 69 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65  imit. This value
53120 20 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64 64   may be overridd
53130 65 6e 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a  en using the .**
53140 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
53150 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20  rnalSizeLimit() 
53160 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50  API. See also "P
53170 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69  RAGMA journal_si
53180 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69  ze_limit"..*/.#i
53190 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  fndef SQLITE_DEF
531a0 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a  AULT_JOURNAL_SIZ
531b0 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 6e  E_LIMIT.  #defin
531c0 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
531d0 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49  _JOURNAL_SIZE_LI
531e0 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f  MIT -1.#endif../
531f0 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73  *.** The type us
53200 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20  ed to represent 
53210 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20  a page number.  
53220 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69  The first page i
53230 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63  n a file.** is c
53240 61 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30  alled page 1.  0
53250 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 70 72   is used to repr
53260 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67  esent "not a pag
53270 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75  e"..*/.typedef u
53280 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20  32 Pgno;../*.** 
53290 45 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 69  Each open file i
532a0 73 20 6d 61 6e 61 67 65 64 20 62 79 20 61 20 73  s managed by a s
532b0 65 70 61 72 61 74 65 20 69 6e 73 74 61 6e 63 65  eparate instance
532c0 20 6f 66 20 74 68 65 20 22 50 61 67 65 72 22 20   of the "Pager" 
532d0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79  structure..*/.ty
532e0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 67  pedef struct Pag
532f0 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a  er Pager;../*.**
53300 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66 6f 72   Handle type for
53310 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64   pages..*/.typed
53320 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 20  ef struct PgHdr 
53330 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50  DbPage;../*.** P
53340 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52  age number PAGER
53350 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65  _MJ_PGNO is neve
53360 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c  r used in an SQL
53370 69 74 65 20 64 61 74 61 62 61 73 65 20 28 69 74  ite database (it
53380 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20   is.** reserved 
53390 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75  for working arou
533a0 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73  nd a windows/pos
533b0 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69  ix incompatibili
533c0 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73  ty). It is.** us
533d0 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ed in the journa
533e0 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61  l to signify tha
533f0 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20  t the remainder 
53400 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
53410 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74  ile .** is devot
53420 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20  ed to storing a 
53430 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
53440 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65 20  ame - there are 
53450 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f  no more pages to
53460 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53  .** roll back. S
53470 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20  ee comments for 
53480 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61  function writeMa
53490 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e  sterJournal() in
534a0 20 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72   pager.c .** for
534b0 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65   details..*/.#de
534c0 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47  fine PAGER_MJ_PG
534d0 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50  NO(x) ((Pgno)((P
534e0 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29  ENDING_BYTE/((x)
534f0 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29  ->pageSize))+1))
53500 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ../*.** Allowed 
53510 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66  values for the f
53520 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 74  lags parameter t
53530 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70  o sqlite3PagerOp
53540 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45  en()..**.** NOTE
53550 3a 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d  : These values m
53560 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f  ust match the co
53570 72 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 45  rresponding BTRE
53580 45 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 74 72  E_ values in btr
53590 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ee.h..*/.#define
535a0 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52   PAGER_OMIT_JOUR
535b0 4e 41 4c 20 20 30 78 30 30 30 31 20 20 20 20 2f  NAL  0x0001    /
535c0 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 72  * Do not use a r
535d0 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
535e0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
535f0 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30  _NO_READLOCK   0
53600 78 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74  x0002    /* Omit
53610 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65   readlocks on re
53620 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a  adonly files */.
53630 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c  ./*.** Valid val
53640 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f  ues for the seco
53650 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73  nd argument to s
53660 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69  qlite3PagerLocki
53670 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65  ngMode()..*/.#de
53680 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49  fine PAGER_LOCKI
53690 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20  NGMODE_QUERY    
536a0 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47    -1.#define PAG
536b0 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
536c0 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 65  ORMAL      0.#de
536d0 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49  fine PAGER_LOCKI
536e0 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
536f0 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69     1../*.** Vali
53700 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65  d values for the
53710 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
53720 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72   to sqlite3Pager
53730 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a 2a  JournalMode()..*
53740 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
53750 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52  JOURNALMODE_QUER
53760 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e  Y      -1.#defin
53770 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  e PAGER_JOURNALM
53780 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20  ODE_DELETE      
53790 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79  0   /* Commit by
537a0 20 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e 61   deleting journa
537b0 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e  l file */.#defin
537c0 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  e PAGER_JOURNALM
537d0 4f 44 45 5f 50 45 52 53 49 53 54 20 20 20 20 20  ODE_PERSIST     
537e0 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79  1   /* Commit by
537f0 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 6c   zeroing journal
53800 20 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 69   header */.#defi
53810 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
53820 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 20 20  MODE_OFF        
53830 20 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20   2   /* Journal 
53840 6f 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 65  omitted.  */.#de
53850 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e  fine PAGER_JOURN
53860 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20  ALMODE_TRUNCATE 
53870 20 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 74     3   /* Commit
53880 20 62 79 20 74 72 75 6e 63 61 74 69 6e 67 20 6a   by truncating j
53890 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e  ournal */.#defin
538a0 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  e PAGER_JOURNALM
538b0 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20  ODE_MEMORY      
538c0 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79  4   /* In-memory
538d0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
538e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61  ../*.** The rema
538f0 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66 69  inder of this fi
53900 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
53910 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20  declarations of 
53920 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a  the functions.**
53930 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68   that make up th
53940 65 20 50 61 67 65 72 20 73 75 62 2d 73 79 73 74  e Pager sub-syst
53950 65 6d 20 41 50 49 2e 20 53 65 65 20 73 6f 75 72  em API. See sour
53960 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73  ce code comments
53970 20 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 69   for .** a detai
53980 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e 20  led description 
53990 6f 66 20 65 61 63 68 20 72 6f 75 74 69 6e 65 2e  of each routine.
539a0 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64  .*/../* Open and
539b0 20 63 6c 6f 73 65 20 61 20 50 61 67 65 72 20 63   close a Pager c
539c0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53  onnection. */ .S
539d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
539e0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70  t sqlite3PagerOp
539f0 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  en(.  sqlite3_vf
53a00 73 2a 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70 70  s*,.  Pager **pp
53a10 50 61 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20 63  Pager,.  const c
53a20 68 61 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 69  har*,.  int,.  i
53a30 6e 74 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f 69  nt,.  int,.  voi
53a40 64 28 2a 29 28 44 62 50 61 67 65 2a 29 0a 29 3b  d(*)(DbPage*).);
53a50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53a60 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
53a70 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61  Close(Pager *pPa
53a80 67 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ger);.SQLITE_PRI
53a90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53aa0 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61  PagerReadFilehea
53ab0 64 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c  der(Pager*, int,
53ac0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29   unsigned char*)
53ad0 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20  ;../* Functions 
53ae0 75 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 72  used to configur
53af0 65 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74  e a Pager object
53b00 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  . */.SQLITE_PRIV
53b10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
53b20 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64  PagerSetBusyhand
53b30 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 28  ler(Pager*, int(
53b40 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64  *)(void *), void
53b50 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
53b60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
53b70 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28  agerSetPagesize(
53b80 50 61 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69 6e  Pager*, u16*, in
53b90 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
53ba0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
53bb0 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  gerMaxPageCount(
53bc0 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51  Pager*, int);.SQ
53bd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
53be0 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  d sqlite3PagerSe
53bf0 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72  tCachesize(Pager
53c00 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
53c10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
53c20 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65  ite3PagerSetSafe
53c30 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c 69  tyLevel(Pager*,i
53c40 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  nt,int);.SQLITE_
53c50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
53c60 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d  te3PagerLockingM
53c70 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74  ode(Pager *, int
53c80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
53c90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
53ca0 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61  erJournalMode(Pa
53cb0 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ger *, int);.SQL
53cc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20  ITE_PRIVATE i64 
53cd0 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
53ce0 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 67  nalSizeLimit(Pag
53cf0 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49  er *, i64);.SQLI
53d00 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
53d10 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c 69  e3_backup **sqli
53d20 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74  te3PagerBackupPt
53d30 72 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46  r(Pager*);../* F
53d40 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f  unctions used to
53d50 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 65   obtain and rele
53d60 61 73 65 20 70 61 67 65 20 72 65 66 65 72 65 6e  ase page referen
53d70 63 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f  ces. */ .SQLITE_
53d80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
53d90 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28  te3PagerAcquire(
53da0 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
53db0 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67 65  gno pgno, DbPage
53dc0 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20 63   **ppPage, int c
53dd0 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e 65  lrFlag);.#define
53de0 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
53df0 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33 50  (A,B,C) sqlite3P
53e00 61 67 65 72 41 63 71 75 69 72 65 28 41 2c 42 2c  agerAcquire(A,B,
53e10 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  C,0).SQLITE_PRIV
53e20 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69  ATE DbPage *sqli
53e30 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50  te3PagerLookup(P
53e40 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67  ager *pPager, Pg
53e50 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 45  no pgno);.SQLITE
53e60 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
53e70 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 44 62  lite3PagerRef(Db
53e80 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Page*);.SQLITE_P
53e90 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
53ea0 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 62  te3PagerUnref(Db
53eb0 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 72  Page*);../* Oper
53ec0 61 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20 72  ations on page r
53ed0 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 51  eferences. */.SQ
53ee0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
53ef0 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
53f00 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c  te(DbPage*);.SQL
53f10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
53f20 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
53f30 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b  tWrite(DbPage*);
53f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53f50 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
53f60 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a 2c  Movepage(Pager*,
53f70 44 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74  DbPage*,Pgno,int
53f80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
53f90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
53fa0 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44  erPageRefcount(D
53fb0 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  bPage*);.SQLITE_
53fc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
53fd0 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
53fe0 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51  a(DbPage *); .SQ
53ff0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
54000 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47  d *sqlite3PagerG
54010 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20 2a  etExtra(DbPage *
54020 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e  ); ../* Function
54030 73 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67 65  s used to manage
54040 20 70 61 67 65 72 20 74 72 61 6e 73 61 63 74 69   pager transacti
54050 6f 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69 6e  ons and savepoin
54060 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ts. */.SQLITE_PR
54070 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
54080 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
54090 50 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53  Pager*, int*);.S
540a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
540b0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 65  t sqlite3PagerBe
540c0 67 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74 20  gin(Pager*, int 
540d0 65 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 51  exFlag, int);.SQ
540e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
540f0 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
54100 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67 65  mitPhaseOne(Page
54110 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r*,const char *z
54120 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 51  Master, int);.SQ
54130 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
54140 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e   sqlite3PagerSyn
54150 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  c(Pager *pPager)
54160 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
54170 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
54180 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  rCommitPhaseTwo(
54190 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
541a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
541b0 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
541c0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
541d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
541e0 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76  ite3PagerOpenSav
541f0 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50  epoint(Pager *pP
54200 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51  ager, int n);.SQ
54210 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
54220 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76   sqlite3PagerSav
54230 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50  epoint(Pager *pP
54240 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  ager, int op, in
54250 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 53  t iSavepoint);.S
54260 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54270 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68  t sqlite3PagerSh
54280 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a  aredLock(Pager *
54290 70 50 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 6e  pPager);../* Fun
542a0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 71  ctions used to q
542b0 75 65 72 79 20 70 61 67 65 72 20 73 74 61 74 65  uery pager state
542c0 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74 69   and configurati
542d0 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on. */.SQLITE_PR
542e0 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33  IVATE u8 sqlite3
542f0 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28  PagerIsreadonly(
54300 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
54310 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
54320 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
54330 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
54340 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
54350 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65  har *sqlite3Page
54360 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a  rFilename(Pager*
54370 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
54380 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  E const sqlite3_
54390 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65  vfs *sqlite3Page
543a0 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 51  rVfs(Pager*);.SQ
543b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
543c0 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74  ite3_file *sqlit
543d0 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 65  e3PagerFile(Page
543e0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
543f0 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
54400 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
54410 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b  nalname(Pager*);
54420 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54430 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
54440 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a  Nosync(Pager*);.
54450 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
54460 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65  oid *sqlite3Page
54470 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72  rTempSpace(Pager
54480 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
54490 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
544a0 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72  gerIsMemdb(Pager
544b0 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e  *);../* Function
544c0 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 61  s used to trunca
544d0 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  te the database 
544e0 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f  file. */.SQLITE_
544f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
54500 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
54510 65 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 67  eImage(Pager*,Pg
54520 6e 6f 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f  no);../* Functio
54530 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74 65  ns to support te
54540 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67  sting and debugg
54550 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65 66  ing. */.#if !def
54560 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20  ined(NDEBUG) || 
54570 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
54580 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  EST).SQLITE_PRIV
54590 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 74  ATE   Pgno sqlit
545a0 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65  e3PagerPagenumbe
545b0 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49  r(DbPage*);.SQLI
545c0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
545d0 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
545e0 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 2a  riteable(DbPage*
545f0 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  );.#endif.#ifdef
54600 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
54610 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
54620 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 53  t *sqlite3PagerS
54630 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 51  tats(Pager*);.SQ
54640 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
54650 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
54660 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29 3b  Refdump(Pager*);
54670 0a 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65 5f  .  void disable_
54680 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
54690 6f 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69  ors(void);.  voi
546a0 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74  d enable_simulat
546b0 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69  ed_io_errors(voi
546c0 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  d);.#else.# defi
546d0 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c  ne disable_simul
546e0 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29  ated_io_errors()
546f0 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65  .# define enable
54700 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
54710 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 23  rors().#endif..#
54720 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f  endif /* _PAGER_
54730 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  H_ */../********
54740 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61  ****** End of pa
54750 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ger.h **********
54760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54780 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
54790 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
547a0 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
547b0 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  off in sqliteInt
547c0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
547d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
547e0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70  ****** Include p
547f0 63 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20 6d  cache.h in the m
54800 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
54810 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
54820 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
54830 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
54840 65 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a  e pcache.h *****
54850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54870 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
54880 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a  8 August 05.**.*
54890 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
548a0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
548b0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
548c0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
548d0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
548e0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
548f0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
54900 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
54910 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
54920 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
54930 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
54940 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
54950 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
54960 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
54970 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
54980 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
54990 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
549a0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
549b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
549c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
549d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
549e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
549f0 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64  is header file d
54a00 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
54a10 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71  face that the sq
54a20 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65 0a  lite page cache.
54a30 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a 2a  ** subsystem. .*
54a40 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70  *.** @(#) $Id: p
54a50 63 61 63 68 65 2e 68 2c 76 20 31 2e 32 30 20 32  cache.h,v 1.20 2
54a60 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36 3a  009/07/25 11:46:
54a70 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  49 danielk1977 E
54a80 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66  xp $.*/..#ifndef
54a90 20 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70   _PCACHE_H_..typ
54aa0 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64  edef struct PgHd
54ab0 72 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 66  r PgHdr;.typedef
54ac0 20 73 74 72 75 63 74 20 50 43 61 63 68 65 20 50   struct PCache P
54ad0 43 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76  Cache;../*.** Ev
54ae0 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20  ery page in the 
54af0 63 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c  cache is control
54b00 6c 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e  led by an instan
54b10 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
54b20 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ing.** structure
54b30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64  ..*/.struct PgHd
54b40 72 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74  r {.  void *pDat
54b50 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
54b60 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20       /* Content 
54b70 6f 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a  of this page */.
54b80 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b 20    void *pExtra; 
54b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54ba0 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 6e   /* Extra conten
54bb0 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44  t */.  PgHdr *pD
54bc0 69 72 74 79 3b 20 20 20 20 20 20 20 20 20 20 20  irty;           
54bd0 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 65        /* Transie
54be0 6e 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79  nt list of dirty
54bf0 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e 6f   pages */.  Pgno
54c00 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20   pgno;          
54c10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
54c20 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  ge number for th
54c30 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 67  is page */.  Pag
54c40 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20  er *pPager;     
54c50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
54c60 68 65 20 70 61 67 65 72 20 74 68 69 73 20 70 61  he pager this pa
54c70 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f  ge is part of */
54c80 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
54c90 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 32  HECK_PAGES.  u32
54ca0 20 70 61 67 65 48 61 73 68 3b 20 20 20 20 20 20   pageHash;      
54cb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
54cc0 61 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e 74  ash of page cont
54cd0 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ent */.#endif.  
54ce0 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20  u16 flags;      
54cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
54d00 2a 20 50 47 48 44 52 20 66 6c 61 67 73 20 64 65  * PGHDR flags de
54d10 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 0a  fined below */..
54d20 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    /*************
54d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 45  *********.  ** E
54d70 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 72  lements above ar
54d80 65 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 74  e public.  All t
54d90 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 70  hat follows is p
54da0 72 69 76 61 74 65 20 74 6f 20 70 63 61 63 68 65  rivate to pcache
54db0 2e 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f 75  .c.  ** and shou
54dc0 6c 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73 73  ld not be access
54dd0 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75  ed by other modu
54de0 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 20  les..  */.  i16 
54df0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
54e00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
54e10 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66  mber of users of
54e20 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20   this page */.  
54e30 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20  PCache *pCache; 
54e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
54e50 2a 20 43 61 63 68 65 20 74 68 61 74 20 6f 77 6e  * Cache that own
54e60 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 0a  s this page */..
54e70 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 4e    PgHdr *pDirtyN
54e80 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
54e90 20 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74   /* Next element
54ea0 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74   in list of dirt
54eb0 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48  y pages */.  PgH
54ec0 64 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b 20  dr *pDirtyPrev; 
54ed0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
54ee0 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 20  revious element 
54ef0 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79  in list of dirty
54f00 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   pages */.};../*
54f10 20 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72 20   Bit values for 
54f20 50 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a 23  PgHdr.flags */.#
54f30 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 52  define PGHDR_DIR
54f40 54 59 20 20 20 20 20 20 20 20 20 20 20 20 20 30  TY             0
54f50 78 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 61  x002  /* Page ha
54f60 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65  s changed */.#de
54f70 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f  fine PGHDR_NEED_
54f80 53 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 30  SYNC         0x0
54f90 30 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 65  04  /* Fsync the
54fa0 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
54fb0 6c 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 20  l before.       
54fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54fe0 2a 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 20  ** writing this 
54ff0 70 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61  page to the data
55000 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  base */.#define 
55010 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 20  PGHDR_NEED_READ 
55020 20 20 20 20 20 20 20 20 30 78 30 30 38 20 20 2f          0x008  /
55030 2a 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72  * Content is unr
55040 65 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ead */.#define P
55050 47 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b  GHDR_REUSE_UNLIK
55060 45 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f 2a  ELY    0x010  /*
55070 20 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 75   A hint that reu
55080 73 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a  se is unlikely *
55090 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f  /.#define PGHDR_
550a0 44 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 20  DONT_WRITE      
550b0 20 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e    0x020  /* Do n
550c0 6f 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e 74  ot write content
550d0 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20   to disk */../* 
550e0 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73  Initialize and s
550f0 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65  hutdown the page
55100 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d   cache subsystem
55110 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
55120 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
55130 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76  acheInitialize(v
55140 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
55150 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
55160 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28  3PcacheShutdown(
55170 76 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20  void);../* Page 
55180 63 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e  cache buffer man
55190 61 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73  agement:.** Thes
551a0 65 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65  e routines imple
551b0 6d 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46  ment SQLITE_CONF
551c0 49 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f  IG_PAGECACHE..*/
551d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
551e0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63  void sqlite3PCac
551f0 68 65 42 75 66 66 65 72 53 65 74 75 70 28 76 6f  heBufferSetup(vo
55200 69 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e  id *, int sz, in
55210 74 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65  t n);../* Create
55220 20 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 63   a new pager cac
55230 68 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d  he..** Under mem
55240 6f 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 6f  ory stress, invo
55250 6b 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 72  ke xStress to tr
55260 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20  y to make pages 
55270 63 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63  clean..** Only c
55280 6c 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65  lean and unpinne
55290 64 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 72  d pages can be r
552a0 65 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c  eclaimed..*/.SQL
552b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
552c0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70   sqlite3PcacheOp
552d0 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65  en(.  int szPage
552e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
552f0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
55300 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20  every page */.  
55310 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20  int szExtra,    
55320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
55330 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61 73  * Extra space as
55340 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61  sociated with ea
55350 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ch page */.  int
55360 20 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20   bPurgeable,    
55370 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
55380 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65  rue if pages are
55390 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72   on backing stor
553a0 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74  e */.  int (*xSt
553b0 72 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 48  ress)(void*, PgH
553c0 64 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f  dr*), /* Call to
553d0 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67   try to make pag
553e0 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f  es clean */.  vo
553f0 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20  id *pStress,    
55400 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
55410 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72  Argument to xStr
55420 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20  ess */.  PCache 
55430 2a 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20  *pToInit        
55440 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c          /* Preal
55450 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f  located space fo
55460 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a  r the PCache */.
55470 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68  );../* Modify th
55480 65 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 65  e page-size afte
55490 72 20 74 68 65 20 63 61 63 68 65 20 68 61 73 20  r the cache has 
554a0 62 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f  been created. */
554b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
554c0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
554d0 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43  heSetPageSize(PC
554e0 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f  ache *, int);../
554f0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a  * Return the siz
55500 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20  e in bytes of a 
55510 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20  PCache object.  
55520 55 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63  Used to prealloc
55530 61 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73  ate.** storage s
55540 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  pace..*/.SQLITE_
55550 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
55560 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f  te3PcacheSize(vo
55570 69 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c  id);../* One rel
55580 65 61 73 65 20 70 65 72 20 73 75 63 63 65 73 73  ease per success
55590 66 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 65  ful fetch.  Page
555a0 20 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c   is pinned until
555b0 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65   released..** Re
555c0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e  ference counted.
555d0 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
555e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
555f0 63 61 63 68 65 46 65 74 63 68 28 50 43 61 63 68  cacheFetch(PCach
55600 65 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72  e*, Pgno, int cr
55610 65 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 2a  eateFlag, PgHdr*
55620 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
55630 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
55640 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48  cacheRelease(PgH
55650 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  dr*);..SQLITE_PR
55660 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
55670 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48  e3PcacheDrop(PgH
55680 64 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a  dr*);         /*
55690 20 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f   Remove page fro
556a0 6d 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54  m cache */.SQLIT
556b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
556c0 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
556d0 44 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 20  Dirty(PgHdr*);  
556e0 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 70    /* Make sure p
556f0 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 69  age is marked di
55700 72 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rty */.SQLITE_PR
55710 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
55720 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61  e3PcacheMakeClea
55730 6e 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a  n(PgHdr*);    /*
55740 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 70   Mark a single p
55750 61 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a  age as clean */.
55760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
55770 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
55780 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65  eCleanAll(PCache
55790 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61  *);    /* Mark a
557a0 6c 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 61  ll dirty list pa
557b0 67 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a  ges as clean */.
557c0 0a 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 67  ./* Change a pag
557d0 65 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 20  e number.  Used 
557e0 62 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20  by incr-vacuum. 
557f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
55800 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
55810 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c  acheMove(PgHdr*,
55820 20 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f   Pgno);../* Remo
55830 76 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 74  ve all pages wit
55840 68 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74  h pgno>x.  Reset
55850 20 74 68 65 20 63 61 63 68 65 20 69 66 20 78 3d   the cache if x=
55860 3d 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  =0 */.SQLITE_PRI
55870 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
55880 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28  3PcacheTruncate(
55890 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29  PCache*, Pgno x)
558a0 3b 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 74  ;../* Get a list
558b0 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61   of all dirty pa
558c0 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65  ges in the cache
558d0 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65  , sorted by page
558e0 20 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54   number */.SQLIT
558f0 45 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 20  E_PRIVATE PgHdr 
55900 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69  *sqlite3PcacheDi
55910 72 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a 29  rtyList(PCache*)
55920 3b 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20  ;../* Reset and 
55930 63 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 20  close the cache 
55940 6f 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45  object */.SQLITE
55950 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
55960 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65  lite3PcacheClose
55970 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43  (PCache*);../* C
55980 6c 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d 20  lear flags from 
55990 70 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 67  pages of the pag
559a0 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54  e cache */.SQLIT
559b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
559c0 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61  qlite3PcacheClea
559d0 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68  rSyncFlags(PCach
559e0 65 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 72  e *);../* Discar
559f0 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  d the contents o
55a00 66 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 53  f the cache */.S
55a10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
55a20 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
55a30 43 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b 0a  Clear(PCache*);.
55a40 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74  ./* Return the t
55a50 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f  otal number of o
55a60 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20  utstanding page 
55a70 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 51  references */.SQ
55a80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
55a90 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
55aa0 66 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b  fCount(PCache*);
55ab0 0a 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74  ../* Increment t
55ac0 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
55ad0 6e 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e  nt of an existin
55ae0 67 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 45  g page */.SQLITE
55af0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
55b00 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28 50  lite3PcacheRef(P
55b10 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f  gHdr*);..SQLITE_
55b20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
55b30 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66  te3PcachePageRef
55b40 63 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a 0a  count(PgHdr*);..
55b50 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f  /* Return the to
55b60 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
55b70 67 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68  ges stored in th
55b80 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54  e cache */.SQLIT
55b90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
55ba0 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63  lite3PcachePagec
55bb0 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a  ount(PCache*);..
55bc0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
55bd0 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20  TE_CHECK_PAGES) 
55be0 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
55bf0 45 5f 44 45 42 55 47 29 0a 2f 2a 20 49 74 65 72  E_DEBUG)./* Iter
55c00 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20  ate through all 
55c10 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72  dirty pages curr
55c20 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20  ently stored in 
55c30 74 68 65 20 63 61 63 68 65 2e 20 54 68 69 73 0a  the cache. This.
55c40 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  ** interface is 
55c50 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
55c60 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
55c70 41 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20  AGES is defined 
55c80 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 62  when the .** lib
55c90 72 61 72 79 20 69 73 20 62 75 69 6c 74 2e 0a 2a  rary is built..*
55ca0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
55cb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
55cc0 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28  cheIterateDirty(
55cd0 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20  PCache *pCache, 
55ce0 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67  void (*xIter)(Pg
55cf0 48 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66 0a  Hdr *));.#endif.
55d00 0a 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 74 20  ./* Set and get 
55d10 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61  the suggested ca
55d20 63 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 68 65  che-size for the
55d30 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 72   specified pager
55d40 2d 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  -cache..**.** If
55d50 20 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69 6d   no global maxim
55d60 75 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64  um is configured
55d70 2c 20 74 68 65 6e 20 74 68 65 20 73 79 73 74 65  , then the syste
55d80 6d 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 69  m attempts to li
55d90 6d 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c  mit.** the total
55da0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
55db0 20 63 61 63 68 65 64 20 62 79 20 70 75 72 67 65   cached by purge
55dc0 61 62 6c 65 20 70 61 67 65 72 2d 63 61 63 68 65  able pager-cache
55dd0 73 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a 20  s to the sum.** 
55de0 6f 66 20 74 68 65 20 73 75 67 67 65 73 74 65 64  of the suggested
55df0 20 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a 2f   cache-sizes..*/
55e00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
55e10 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
55e20 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50  heSetCachesize(P
55e30 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 23  Cache *, int);.#
55e40 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
55e50 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
55e60 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
55e70 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 50  heGetCachesize(P
55e80 43 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69 66  Cache *);.#endif
55e90 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
55ea0 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
55eb0 4e 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20  NAGEMENT./* Try 
55ec0 74 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79  to return memory
55ed0 20 75 73 65 64 20 62 79 20 74 68 65 20 70 63 61   used by the pca
55ee0 63 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68  che module to th
55ef0 65 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65  e main memory he
55f00 61 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ap */.SQLITE_PRI
55f10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
55f20 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d  PcacheReleaseMem
55f30 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66  ory(int);.#endif
55f40 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
55f50 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
55f60 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
55f70 50 63 61 63 68 65 53 74 61 74 73 28 69 6e 74 2a  PcacheStats(int*
55f80 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29  ,int*,int*,int*)
55f90 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  ;.#endif..SQLITE
55fa0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
55fb0 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65  lite3PCacheSetDe
55fc0 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 65  fault(void);..#e
55fd0 6e 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 45 5f  ndif /* _PCACHE_
55fe0 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  H_ */../********
55ff0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63  ****** End of pc
56000 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ache.h *********
56010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56030 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
56040 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
56050 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
56060 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  off in sqliteInt
56070 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
56080 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  *****/../*******
56090 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
560a0 6f 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  os.h in the midd
560b0 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  le of sqliteInt.
560c0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
560d0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
560e0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
560f0 6c 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  le os.h ********
56100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56120 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
56130 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 36 0a  01 September 16.
56140 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
56150 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
56160 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
56170 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
56180 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
56190 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
561a0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
561b0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
561c0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
561d0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
561e0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
561f0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
56200 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
56210 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
56220 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
56230 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
56240 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
56250 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
56260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562a0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68  ***.**.** This h
562b0 65 61 64 65 72 20 66 69 6c 65 20 28 74 6f 67 65  eader file (toge
562c0 74 68 65 72 20 77 69 74 68 20 69 73 20 63 6f 6d  ther with is com
562d0 70 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65 2d  panion C source-
562e0 63 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f 73  code file.** "os
562f0 2e 63 22 29 20 61 74 74 65 6d 70 74 20 74 6f 20  .c") attempt to 
56300 61 62 73 74 72 61 63 74 20 74 68 65 20 75 6e 64  abstract the und
56310 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e  erlying operatin
56320 67 20 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74  g system so that
56330 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c  .** the SQLite l
56340 69 62 72 61 72 79 20 77 69 6c 6c 20 77 6f 72 6b  ibrary will work
56350 20 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 20 61   on both POSIX a
56360 6e 64 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65  nd windows syste
56370 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68  ms..**.** This h
56380 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 23 69  eader file is #i
56390 6e 63 6c 75 64 65 2d 65 64 20 62 79 20 73 71 6c  nclude-ed by sql
563a0 69 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 68 75  iteInt.h and thu
563b0 73 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65 69  s ends up.** bei
563c0 6e 67 20 69 6e 63 6c 75 64 65 64 20 62 79 20 65  ng included by e
563d0 76 65 72 79 20 73 6f 75 72 63 65 20 66 69 6c 65  very source file
563e0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e  ..**.** $Id: os.
563f0 68 2c 76 20 31 2e 31 30 38 20 32 30 30 39 2f 30  h,v 1.108 2009/0
56400 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 20 64 72  2/05 16:31:46 dr
56410 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  h Exp $.*/.#ifnd
56420 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f  ef _SQLITE_OS_H_
56430 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45  .#define _SQLITE
56440 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 69  _OS_H_../*.** Fi
56450 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 61  gure out if we a
56460 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
56470 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f  Unix, Windows, o
56480 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20  r some other.** 
56490 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
564a0 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 6f 6c  .  After the fol
564b0 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20  lowing block of 
564c0 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72 6f  preprocess macro
564d0 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 4c  s,.** all of SQL
564e0 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 4c  ITE_OS_UNIX, SQL
564f0 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c 49  ITE_OS_WIN, SQLI
56500 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 53  TE_OS_OS2, and S
56510 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 0a  QLITE_OS_OTHER .
56520 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 20  ** will defined 
56530 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20 30  to either 1 or 0
56540 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f  .  One of the fo
56550 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20 54  ur will be 1.  T
56560 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68 72  he other .** thr
56570 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a 2f  ee will be 0..*/
56580 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
56590 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20  ITE_OS_OTHER).# 
565a0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48  if SQLITE_OS_OTH
565b0 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66 20  ER==1.#   undef 
565c0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23  SQLITE_OS_UNIX.#
565d0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
565e0 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 75  _OS_UNIX 0.#   u
565f0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  ndef SQLITE_OS_W
56600 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51  IN.#   define SQ
56610 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20  LITE_OS_WIN 0.# 
56620 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f    undef SQLITE_O
56630 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e 65  S_OS2.#   define
56640 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30   SQLITE_OS_OS2 0
56650 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 64 65  .# else.#   unde
56660 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45  f SQLITE_OS_OTHE
56670 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  R.# endif.#endif
56680 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
56690 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 26 26  LITE_OS_UNIX) &&
566a0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
566b0 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 65 66  _OS_OTHER).# def
566c0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54  ine SQLITE_OS_OT
566d0 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66 20 53  HER 0.# ifndef S
566e0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20  QLITE_OS_WIN.#  
566f0 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e   if defined(_WIN
56700 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 57  32) || defined(W
56710 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64  IN32) || defined
56720 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c 20  (__CYGWIN__) || 
56730 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33  defined(__MINGW3
56740 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  2__) || defined(
56750 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 20  __BORLANDC__).# 
56760 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
56770 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20 20  E_OS_WIN 1.#    
56780 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
56790 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64  S_UNIX 0.#     d
567a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
567b0 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20 64  OS2 0.#   elif d
567c0 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29 20  efined(__EMX__) 
567d0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 29  || defined(_OS2)
567e0 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32 29   || defined(OS2)
567f0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32   || defined(_OS2
56800 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  _) || defined(__
56810 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65 66  OS2__).#     def
56820 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ine SQLITE_OS_WI
56830 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65  N 0.#     define
56840 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20   SQLITE_OS_UNIX 
56850 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53  0.#     define S
56860 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 0a 23  QLITE_OS_OS2 1.#
56870 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20 64 65     else.#     de
56880 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
56890 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e  IN 0.#     defin
568a0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  e SQLITE_OS_UNIX
568b0 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20   1.#     define 
568c0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a  SQLITE_OS_OS2 0.
568d0 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65 0a  #  endif.# else.
568e0 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  #  define SQLITE
568f0 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64 65  _OS_UNIX 0.#  de
56900 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  fine SQLITE_OS_O
56910 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6c  S2 0.# endif.#el
56920 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49  se.# ifndef SQLI
56930 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65 66  TE_OS_WIN.#  def
56940 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ine SQLITE_OS_WI
56950 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64  N 0.# endif.#end
56960 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d  if../*.** Determ
56970 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64 65  ine if we are de
56980 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64 6f  aling with Windo
56990 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 61 73  wsCE - which has
569a0 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75 63   a much.** reduc
569b0 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 64  ed API..*/.#if d
569c0 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f 57 43  efined(_WIN32_WC
569d0 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  E).# define SQLI
569e0 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a 23 65  TE_OS_WINCE 1.#e
569f0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  lse.# define SQL
56a00 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 0a 23  ITE_OS_WINCE 0.#
56a10 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65  endif.../*.** De
56a20 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 6d  fine the maximum
56a30 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d 70 6f   size of a tempo
56a40 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a 2a 2f  rary filename.*/
56a50 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
56a60 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77 69  IN.# include <wi
56a70 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69 6e  ndows.h>.# defin
56a80 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d  e SQLITE_TEMPNAM
56a90 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54 48  E_SIZE (MAX_PATH
56aa0 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c 49 54  +50).#elif SQLIT
56ab0 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 28 5f  E_OS_OS2.# if (_
56ac0 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c 20 5f  _GNUC__ > 3 || _
56ad0 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 26 20  _GNUC__ == 3 && 
56ae0 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20 3e  __GNUC_MINOR__ >
56af0 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65 64 28  = 3) && defined(
56b00 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 59 29  OS2_HIGH_MEMORY)
56b10 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32  .#  include <os2
56b20 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 20 74  safe.h> /* has t
56b30 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 62 65  o be included be
56b40 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 20 6c  fore os2.h for l
56b50 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b 20 2a  inking to work *
56b60 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69  /.# endif.# defi
56b70 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 45 54  ne INCL_DOSDATET
56b80 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 4e 43  IME.# define INC
56b90 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 20 64  L_DOSFILEMGR.# d
56ba0 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45 52  efine INCL_DOSER
56bb0 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 49 4e  RORS.# define IN
56bc0 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 65 66  CL_DOSMISC.# def
56bd0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f 43  ine INCL_DOSPROC
56be0 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 43  ESS.# define INC
56bf0 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a 23  L_DOSMODULEMGR.#
56c00 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53   define INCL_DOS
56c10 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 6e 63  SEMAPHORES.# inc
56c20 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 20 69  lude <os2.h>.# i
56c30 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e 68 3e  nclude <uconv.h>
56c40 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
56c50 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28  _TEMPNAME_SIZE (
56c60 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 29 0a  CCHMAXPATHCOMP).
56c70 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
56c80 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53  QLITE_TEMPNAME_S
56c90 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 0a 0a  IZE 200.#endif..
56ca0 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f 46 55  /* If the SET_FU
56cb0 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 73 20  LLSYNC macro is 
56cc0 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62 6f 76  not defined abov
56cd0 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 74 0a  e, then make it.
56ce0 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23 69  ** a no-op.*/.#i
56cf0 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c 53 59  fndef SET_FULLSY
56d00 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f  NC.# define SET_
56d10 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a 23 65  FULLSYNC(x,y).#e
56d20 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
56d30 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 20  default size of 
56d40 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a 2a 2f  a disk sector.*/
56d50 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
56d60 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53  DEFAULT_SECTOR_S
56d70 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  IZE.# define SQL
56d80 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54  ITE_DEFAULT_SECT
56d90 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 6e 64  OR_SIZE 512.#end
56da0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f 72  if../*.** Tempor
56db0 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 6e 61  ary files are na
56dc0 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 74  med starting wit
56dd0 68 20 74 68 69 73 20 70 72 65 66 69 78 20 66 6f  h this prefix fo
56de0 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72 61 6e  llowed by 16 ran
56df0 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d 65  dom.** alphanume
56e00 72 69 63 20 63 68 61 72 61 63 74 65 72 73 2c 20  ric characters, 
56e10 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 74 65  and no file exte
56e20 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 65 20  nsion. They are 
56e30 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a 2a  stored in the.**
56e40 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64 20 74   OS's standard t
56e50 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64 69  emporary file di
56e60 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 72 65  rectory, and are
56e70 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72 20 74   deleted prior t
56e80 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 73 71  o exit..** If sq
56e90 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20 65 6d  lite is being em
56ea0 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 68 65  bedded in anothe
56eb0 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 20 6d  r program, you m
56ec0 61 79 20 77 69 73 68 20 74 6f 20 63 68 61 6e 67  ay wish to chang
56ed0 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78 20  e the.** prefix 
56ee0 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 72 20  to reflect your 
56ef0 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 2c 20  program's name, 
56f00 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75 72 20  so that if your 
56f10 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a  program exits.**
56f20 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 6f 6c   prematurely, ol
56f30 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  d temporary file
56f40 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c 79 20  s can be easily 
56f50 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68 69 73  identified. This
56f60 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a 20   can be done.** 
56f70 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f 54  using -DSQLITE_T
56f80 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 3d  EMP_FILE_PREFIX=
56f90 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 68 65  myprefix_ on the
56fa0 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e   compiler comman
56fb0 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 30  d line..**.** 20
56fc0 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 20 64  06-10-31:  The d
56fd0 65 66 61 75 6c 74 20 70 72 65 66 69 78 20 75 73  efault prefix us
56fe0 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74 65  ed to be "sqlite
56ff0 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a 2a 2a  _".  But then.**
57000 20 4d 63 61 66 65 65 20 73 74 61 72 74 65 64 20   Mcafee started 
57010 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 20  using SQLite in 
57020 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72 75 73  their anti-virus
57030 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69 74 0a   product and it.
57040 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74 74 69  ** started putti
57050 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20 74 68  ng files with th
57060 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65 20  e "sqlite" name 
57070 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20 66  in the c:/temp f
57080 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20 61  older..** This a
57090 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e 64  nnoyed many wind
570a0 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f 73  ows users.  Thos
570b0 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74 68  e users would th
570c0 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f 67  en do a .** Goog
570d0 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 22 73  le search for "s
570e0 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68 65  qlite", find the
570f0 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62 65   telephone numbe
57100 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76  rs of the.** dev
57110 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 6c 6c  elopers and call
57120 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 75 70   to wake them up
57130 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20 63 6f   at night and co
57140 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20 74  mplain..** For t
57150 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20  his reason, the 
57160 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 72 65  default name pre
57170 66 69 78 20 69 73 20 63 68 61 6e 67 65 64 20 74  fix is changed t
57180 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20 0a 2a  o be "sqlite" .*
57190 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b 77 61  * spelled backwa
571a0 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74 65 6d  rds.  So the tem
571b0 70 20 66 69 6c 65 73 20 61 72 65 20 73 74 69 6c  p files are stil
571c0 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20 62 75  l identified, bu
571d0 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 6d 61  t.** anybody sma
571e0 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 69 67  rt enough to fig
571f0 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f 64 65  ure out the code
57200 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c 79 20   is also likely 
57210 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 68 20  smart.** enough 
57220 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 61 6c  to know that cal
57230 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c 6f 70  ling the develop
57240 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 6c 70  er will not help
57250 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 20 74   get rid.** of t
57260 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e  he file..*/.#ifn
57270 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f  def SQLITE_TEMP_
57280 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 64 65  FILE_PREFIX.# de
57290 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50  fine SQLITE_TEMP
572a0 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65 74  _FILE_PREFIX "et
572b0 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a 2f  ilqs_".#endif../
572c0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
572d0 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65  ng values may be
572e0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
572f0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
57300 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f  o.** sqlite3OsLo
57310 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f 75  ck(). The variou
57320 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74 20  s locks exhibit 
57330 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65  the following se
57340 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 53  mantics:.**.** S
57350 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e 75  HARED:    Any nu
57360 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73 65  mber of processe
57370 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48 41  s may hold a SHA
57380 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74 61  RED lock simulta
57390 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 53 45  neously..** RESE
573a0 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c 65 20  RVED:  A single 
573b0 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c 64  process may hold
573c0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
573d0 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a   on a file at.**
573e0 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20              any 
573f0 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 6f 63  time. Other proc
57400 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 61  esses may hold a
57410 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 53 48  nd obtain new SH
57420 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 50  ARED locks..** P
57430 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 6e 67  ENDING:   A sing
57440 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68  le process may h
57450 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  old a PENDING lo
57460 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a  ck on a file at.
57470 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e  **            an
57480 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 69 73  y one time. Exis
57490 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b  ting SHARED lock
574a0 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c 20 62  s may persist, b
574b0 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20 20  ut no new.**    
574c0 20 20 20 20 20 20 20 20 53 48 41 52 45 44 20 6c          SHARED l
574d0 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 74 61  ocks may be obta
574e0 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20 70 72  ined by other pr
574f0 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 43 4c  ocesses..** EXCL
57500 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c 55 53  USIVE: An EXCLUS
57510 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c 75 64  IVE lock preclud
57520 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63  es all other loc
57530 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49 4e  ks..**.** PENDIN
57540 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 20 62  G_LOCK may not b
57550 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74 6c  e passed directl
57560 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c 6f  y to sqlite3OsLo
57570 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 61  ck(). Instead, a
57580 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 61 74  .** process that
57590 20 72 65 71 75 65 73 74 73 20 61 6e 20 45 58 43   requests an EXC
575a0 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 79 20  LUSIVE lock may 
575b0 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 6e 20  actually obtain 
575c0 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f 63  a PENDING.** loc
575d0 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  k. This can be u
575e0 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58  pgraded to an EX
575f0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 79 20  CLUSIVE lock by 
57600 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  a subsequent cal
57610 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f  l to.** sqlite3O
57620 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65 66  sLock()..*/.#def
57630 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20  ine NO_LOCK     
57640 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 48      0.#define SH
57650 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a  ARED_LOCK     1.
57660 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44  #define RESERVED
57670 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 69 6e  _LOCK   2.#defin
57680 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 20  e PENDING_LOCK  
57690 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58 43 4c    3.#define EXCL
576a0 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a 2f  USIVE_LOCK  4../
576b0 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e  *.** File Lockin
576c0 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 74 6c  g Notes:  (Mostl
576d0 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 73 20  y about windows 
576e0 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 69 6e  but also some in
576f0 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a 2a 0a  fo for Unix).**.
57700 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 73 65  ** We cannot use
57710 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f 72   LockFileEx() or
57720 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20   UnlockFileEx() 
57730 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 62  on Win95/98/ME b
57740 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 65 20  ecause.** those 
57750 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f  functions are no
57760 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 53 6f  t available.  So
57770 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c 6f 63   we use only Loc
57780 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 55  kFile() and.** U
57790 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a 0a  nlockFile()..**.
577a0 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 70 72  ** LockFile() pr
577b0 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 74 20  events not just 
577c0 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c 73 6f  writing but also
577d0 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74 68 65   reading by othe
577e0 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20  r processes..** 
577f0 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 69 73  A SHARED_LOCK is
57800 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63   obtained by loc
57810 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72 61  king a single ra
57820 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a 2a  ndomly-chosen .*
57830 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20 61 20  * byte out of a 
57840 73 70 65 63 69 66 69 63 20 72 61 6e 67 65 20 6f  specific range o
57850 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c 6f 63  f bytes. The loc
57860 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61 69 6e  k byte is obtain
57870 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 6f 6d  ed at .** random
57880 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61 74 65   so two separate
57890 20 72 65 61 64 65 72 73 20 63 61 6e 20 70 72 6f   readers can pro
578a0 62 61 62 6c 79 20 61 63 63 65 73 73 20 74 68 65  bably access the
578b0 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a 2a 2a   file at the .**
578c0 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c 65   same time, unle
578d0 73 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c 75  ss they are unlu
578e0 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20 74  cky and choose t
578f0 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79 74  he same lock byt
57900 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49  e..** An EXCLUSI
57910 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69  VE_LOCK is obtai
57920 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61  ned by locking a
57930 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  ll bytes in the 
57940 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 65 20  range..** There 
57950 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65 20  can only be one 
57960 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 45 52  writer.  A RESER
57970 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61  VED_LOCK is obta
57980 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 0a  ined by locking.
57990 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65  ** a single byte
579a0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 68 61   of the file tha
579b0 74 20 69 73 20 64 65 73 69 67 6e 61 74 65 64 20  t is designated 
579c0 61 73 20 74 68 65 20 72 65 73 65 72 76 65 64 20  as the reserved 
579d0 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 20  lock byte..** A 
579e0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 73 20  PENDING_LOCK is 
579f0 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b  obtained by lock
57a00 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74 65 64  ing a designated
57a10 20 62 79 74 65 20 64 69 66 66 65 72 65 6e 74 20   byte different 
57a20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 53 45  from.** the RESE
57a30 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a  RVED_LOCK byte..
57a40 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f 32  **.** On WinNT/2
57a50 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 4c 6f  K/XP systems, Lo
57a60 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 20 55  ckFileEx() and U
57a70 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 72  nlockFileEx() ar
57a80 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a 20  e available,.** 
57a90 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65 20 63  which means we c
57aa0 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f 77 72  an use reader/wr
57ab0 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 68 65  iter locks.  Whe
57ac0 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 20  n reader/writer 
57ad0 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 73 65  locks.** are use
57ae0 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 70  d, the lock is p
57af0 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73 61 6d  laced on the sam
57b00 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73  e range of bytes
57b10 20 74 68 61 74 20 69 73 20 75 73 65 64 0a 2a 2a   that is used.**
57b20 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 73 74   for probabilist
57b30 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 57 69  ic locking in Wi
57b40 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 6e 63  n95/98/ME.  Henc
57b50 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73  e, the locking s
57b60 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73 75  cheme.** will su
57b70 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d 6f 72  pport two or mor
57b80 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 20  e Win95 readers 
57b90 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57  or two or more W
57ba0 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a 2a 2a  inNT readers..**
57bb0 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20 57 69   But a single Wi
57bc0 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c 6c 20  n95 reader will 
57bd0 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 69 6e  lock out all Win
57be0 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64 20 61  NT readers and a
57bf0 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e 54   single.** WinNT
57c00 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63   reader will loc
57c10 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 72 20  k out all other 
57c20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e 0a 2a  Win95 readers..*
57c30 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
57c40 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70 65 63  ng #defines spec
57c50 69 66 79 20 74 68 65 20 72 61 6e 67 65 20 6f 66  ify the range of
57c60 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 20   bytes used for 
57c70 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41 52  locking..** SHAR
57c80 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20 6e  ED_SIZE is the n
57c90 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61  umber of bytes a
57ca0 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20  vailable in the 
57cb0 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68 0a  pool from which.
57cc0 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65  ** a random byte
57cd0 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f 72   is selected for
57ce0 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20   a shared lock. 
57cf0 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79 74   The pool of byt
57d00 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 65 64  es for.** shared
57d10 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61 74   locks begins at
57d20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20 0a   SHARED_FIRST. .
57d30 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20 6c  **.** The same l
57d40 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20  ocking strategy 
57d50 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 6e 67  and.** byte rang
57d60 65 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20  es are used for 
57d70 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 61 76  Unix.  This leav
57d80 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f 73 73  es open the poss
57d90 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69 6e 67  iblity of having
57da0 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e 20 77  .** clients on w
57db0 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 6e 64  in95, winNT, and
57dc0 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b 69 6e   unix all talkin
57dd0 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73 68  g to the same sh
57de0 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 6e 64  ared file.** and
57df0 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 72   all locking cor
57e00 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f 20 73  rectly.  To do s
57e10 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72 65 20  o would require 
57e20 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72 20 77  that samba (or w
57e30 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c 20  hatever.** tool 
57e40 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f  is being used fo
57e50 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67 29 20  r file sharing) 
57e60 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b 73  implements locks
57e70 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74 77 65   correctly betwe
57e80 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 61 6e  en.** windows an
57e90 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 75 65  d unix.  I'm gue
57ea0 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e 27 74  ssing that isn't
57eb0 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 70 65   likely to happe
57ec0 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 73 69  n, but by.** usi
57ed0 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b  ng the same lock
57ee0 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61 72 65  ing range we are
57ef0 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e 20 74   at least open t
57f00 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74  o the possibilit
57f10 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e 67  y..**.** Locking
57f20 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 20 6d   in windows is m
57f30 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 20 74  anditory.  For t
57f40 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 20 63  his reason, we c
57f50 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a 20 61  annot store.** a
57f60 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20 74 68  ctual data in th
57f70 65 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72  e bytes used for
57f80 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 20 70   locking.  The p
57f90 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c 6f 63  ager never alloc
57fa0 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 67 65  ates.** the page
57fb0 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 6c 6f  s involved in lo
57fc0 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 65 2e  cking therefore.
57fd0 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73    SHARED_SIZE is
57fe0 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a 2a 20   selected so.** 
57ff0 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 77  that all locks w
58000 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 69 6e  ill fit on a sin
58010 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20 61 74  gle page even at
58020 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61 67   the minimum pag
58030 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44 49  e size..** PENDI
58040 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73 20  NG_BYTE defines 
58050 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
58060 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79 20   the locks.  By 
58070 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47 5f  default PENDING_
58080 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 20 68  BYTE.** is set h
58090 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 20 64  igh so that we d
580a0 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c 6c  on't have to all
580b0 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64 20  ocate an unused 
580c0 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a 20 66  page except.** f
580d0 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 64 61  or very large da
580e0 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f 6e  tabases.  But on
580f0 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74 68  e should test th
58100 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67 20  e page skipping 
58110 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 65 74  logic .** by set
58120 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54  ting PENDING_BYT
58130 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e 69 6e  E low and runnin
58140 67 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 67  g the entire reg
58150 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e 0a 2a  ression suite..*
58160 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68  *.** Changing th
58170 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e 44 49  e value of PENDI
58180 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74 73 20  NG_BYTE results 
58190 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e 63 6f  in a subtly inco
581a0 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 6c 65  mpatible.** file
581b0 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 6e 64   format.  Depend
581c0 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 69 73  ing on how it is
581d0 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 6d 69   changed, you mi
581e0 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 0a 2a  ght not notice.*
581f0 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 69 62  * the incompatib
58200 69 6c 69 74 79 20 72 69 67 68 74 20 61 77 61 79  ility right away
58210 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 20 61  , even running a
58220 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69 6f 6e   full regression
58230 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 64 65   test..** The de
58240 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e 20 6f  fault location o
58250 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 69  f PENDING_BYTE i
58260 73 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  s the first byte
58270 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 47 42   past the.** 1GB
58280 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a 2f   boundary..**.*/
58290 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47  .#define PENDING
582a0 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c 69 74  _BYTE      sqlit
582b0 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a 23 64  e3PendingByte.#d
582c0 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f 42  efine RESERVED_B
582d0 59 54 45 20 20 20 20 20 28 50 45 4e 44 49 4e 47  YTE     (PENDING
582e0 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69 6e 65  _BYTE+1).#define
582f0 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 20 20   SHARED_FIRST   
58300 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 45     (PENDING_BYTE
58310 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48 41 52  +2).#define SHAR
58320 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20 35 31  ED_SIZE       51
58330 30 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72  0../*.** Wrapper
58340 20 61 72 6f 75 6e 64 20 4f 53 20 73 70 65 63 69   around OS speci
58350 66 69 63 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69  fic sqlite3_os_i
58360 6e 69 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a  nit() function..
58370 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
58380 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49  E int sqlite3OsI
58390 6e 69 74 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 0a  nit(void);../* .
583a0 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72  ** Functions for
583b0 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74   accessing sqlit
583c0 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20  e3_file methods 
583d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
583e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
583f0 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69  Close(sqlite3_fi
58400 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  le*);.SQLITE_PRI
58410 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
58420 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f 66  OsRead(sqlite3_f
58430 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74  ile*, void*, int
58440 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74   amt, i64 offset
58450 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
58460 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57  E int sqlite3OsW
58470 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  rite(sqlite3_fil
58480 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  e*, const void*,
58490 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66   int amt, i64 of
584a0 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  fset);.SQLITE_PR
584b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
584c0 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69  3OsTruncate(sqli
584d0 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73  te3_file*, i64 s
584e0 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ize);.SQLITE_PRI
584f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
58500 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66  OsSync(sqlite3_f
58510 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ile*, int);.SQLI
58520 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58530 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65  qlite3OsFileSize
58540 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
58550 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c  i64 *pSize);.SQL
58560 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
58570 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71  sqlite3OsLock(sq
58580 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
58590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
585a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55  E int sqlite3OsU
585b0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
585c0 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  le*, int);.SQLIT
585d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
585e0 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65  lite3OsCheckRese
585f0 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33  rvedLock(sqlite3
58600 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a  _file *id, int *
58610 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45  pResOut);.SQLITE
58620 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
58630 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f  ite3OsFileContro
58640 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  l(sqlite3_file*,
58650 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66  int,void*);.#def
58660 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ine SQLITE_FCNTL
58670 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30 78  _DB_UNCHANGED 0x
58680 63 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45 5f  ca093fa0.SQLITE_
58690 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
586a0 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28  te3OsSectorSize(
586b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
586c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
586d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44  E int sqlite3OsD
586e0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
586f0 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c  tics(sqlite3_fil
58700 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20  e *id);../* .** 
58710 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63  Functions for ac
58720 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  cessing sqlite3_
58730 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a  vfs methods .*/.
58740 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
58750 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e  nt sqlite3OsOpen
58760 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
58770 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71  const char *, sq
58780 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
58790 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45  , int *);.SQLITE
587a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
587b0 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c  ite3OsDelete(sql
587c0 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73  ite3_vfs *, cons
587d0 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a  t char *, int);.
587e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
587f0 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65  nt sqlite3OsAcce
58800 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ss(sqlite3_vfs *
58810 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
58820 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  int, int *pResOu
58830 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
58840 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
58850 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c  FullPathname(sql
58860 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73  ite3_vfs *, cons
58870 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63  t char *, int, c
58880 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20  har *);.#ifndef 
58890 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
588a0 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54  _EXTENSION.SQLIT
588b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
588c0 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28  sqlite3OsDlOpen(
588d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63  sqlite3_vfs *, c
588e0 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51  onst char *);.SQ
588f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
58900 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72  d sqlite3OsDlErr
58910 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  or(sqlite3_vfs *
58920 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a  , int, char *);.
58930 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
58940 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 44  oid (*sqlite3OsD
58950 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73  lSym(sqlite3_vfs
58960 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73   *, void *, cons
58970 74 20 63 68 61 72 20 2a 29 29 28 76 6f 69 64 29  t char *))(void)
58980 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
58990 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44   void sqlite3OsD
589a0 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76  lClose(sqlite3_v
589b0 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23  fs *, void *);.#
589c0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
589d0 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
589e0 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ION */.SQLITE_PR
589f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
58a00 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71  3OsRandomness(sq
58a10 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74  lite3_vfs *, int
58a20 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54  , char *);.SQLIT
58a30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
58a40 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c  lite3OsSleep(sql
58a50 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29  ite3_vfs *, int)
58a60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
58a70 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75   int sqlite3OsCu
58a80 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65  rrentTime(sqlite
58a90 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a  3_vfs *, double*
58aa0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e  );../*.** Conven
58ab0 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 73 20  ience functions 
58ac0 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20  for opening and 
58ad0 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 75 73  closing files us
58ae0 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ing .** sqlite3_
58af0 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61  malloc() to obta
58b00 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  in space for the
58b10 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72   file-handle str
58b20 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ucture..*/.SQLIT
58b30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
58b40 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f  lite3OsOpenMallo
58b50 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  c(sqlite3_vfs *,
58b60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73   const char *, s
58b70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20  qlite3_file **, 
58b80 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54  int,int*);.SQLIT
58b90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
58ba0 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65  lite3OsCloseFree
58bb0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
58bc0 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51  ;..#endif /* _SQ
58bd0 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f  LITE_OS_H_ */../
58be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
58bf0 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a 2a  nd 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 2f 0a 2f  *************/./
58c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
58c40 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
58c50 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73  we left off in s
58c60 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
58c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
58c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
58c90 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 20 69  nclude mutex.h i
58ca0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
58cb0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
58cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
58cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
58ce0 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e  egin file mutex.
58cf0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  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 2f 0a 2f  *************/./
58d20 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74  *.** 2007 August
58d30 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   28.**.** The au
58d40 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
58d50 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
58d60 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
58d70 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
58d80 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
58d90 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
58da0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
58db0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
58dc0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
58dd0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
58de0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
58df0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
58e00 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
58e10 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
58e20 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
58e30 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
58e40 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  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 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
58ea0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
58eb0 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 66 6f  common header fo
58ec0 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c  r all mutex impl
58ed0 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20  ementations..** 
58ee0 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 20  The sqliteInt.h 
58ef0 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64 65 73  header #includes
58f00 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20 74 68   this file so th
58f10 61 74 20 69 74 20 69 73 20 61 76 61 69 6c 61 62  at it is availab
58f20 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f 75  le.** to all sou
58f30 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65 20 62  rce files.  We b
58f40 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e 20 61  reak it out in a
58f50 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70  n effort to keep
58f60 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 74   the code.** bet
58f70 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e 0a 2a  ter organized..*
58f80 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72  *.** NOTE:  sour
58f90 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20  ce files should 
58fa0 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 20 74  *not* #include t
58fb0 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
58fc0 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f 75  directly..** Sou
58fd0 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64  rce files should
58fe0 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20 73 71   #include the sq
58ff0 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 20 61  liteInt.h file a
59000 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69 6c 65  nd let that file
59010 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 69 73  .** include this
59020 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c 79 2e   one indirectly.
59030 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65  .**.** $Id: mute
59040 78 2e 68 2c 76 20 31 2e 39 20 32 30 30 38 2f 31  x.h,v 1.9 2008/1
59050 30 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 64 72  0/07 15:25:48 dr
59060 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a  h Exp $.*/.../*.
59070 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 68  ** Figure out wh
59080 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  at version of th
59090 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 20  e code to use.  
590a0 54 68 65 20 63 68 6f 69 63 65 73 20 61 72 65 0a  The choices are.
590b0 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  **.**   SQLITE_M
590c0 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20 20  UTEX_OMIT       
590d0 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63    No mutex logic
590e0 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75 62  .  Not even stub
590f0 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20  s.  The.**      
59100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59110 20 20 20 20 20 20 20 6d 75 74 65 78 65 73 20 69         mutexes i
59120 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e  mplemention cann
59130 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e  ot be overridden
59140 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
59150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59160 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a  at start-time..*
59170 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55  *.**   SQLITE_MU
59180 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20  TEX_NOOP        
59190 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72 65   For single-thre
591a0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  aded application
591b0 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20  s.  No.**       
591c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
591d0 20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 78 63        mutual exc
591e0 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 69 64  lusion is provid
591f0 65 64 2e 20 20 42 75 74 20 74 68 69 73 0a 2a 2a  ed.  But this.**
59200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59210 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6d 70               imp
59220 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20  lementation can 
59230 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61 74  be overridden at
59240 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
59250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59260 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a  start-time..**.*
59270 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  *   SQLITE_MUTEX
59280 5f 50 54 48 52 45 41 44 53 20 20 20 20 20 46 6f  _PTHREADS     Fo
59290 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64  r multi-threaded
592a0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e   applications on
592b0 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53   Unix..**.**   S
592c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20  QLITE_MUTEX_W32 
592d0 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c           For mul
592e0 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c  ti-threaded appl
592f0 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33  ications on Win3
59300 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54  2..**.**   SQLIT
59310 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20 20  E_MUTEX_OS2     
59320 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74       For multi-t
59330 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74  hreaded applicat
59340 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f  ions on OS/2..*/
59350 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48 52  .#if !SQLITE_THR
59360 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65  EADSAFE.# define
59370 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d   SQLITE_MUTEX_OM
59380 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  IT.#endif.#if SQ
59390 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
593a0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
593b0 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23  TE_MUTEX_NOOP).#
593c0 20 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55    if SQLITE_OS_U
593d0 4e 49 58 0a 23 20 20 20 20 64 65 66 69 6e 65 20  NIX.#    define 
593e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48  SQLITE_MUTEX_PTH
593f0 52 45 41 44 53 0a 23 20 20 65 6c 69 66 20 53 51  READS.#  elif SQ
59400 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20  LITE_OS_WIN.#   
59410 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
59420 55 54 45 58 5f 57 33 32 0a 23 20 20 65 6c 69 66  UTEX_W32.#  elif
59430 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23   SQLITE_OS_OS2.#
59440 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
59450 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20 65  E_MUTEX_OS2.#  e
59460 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20  lse.#    define 
59470 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
59480 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69  P.#  endif.#endi
59490 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
594a0 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a  _MUTEX_OMIT./*.*
594b0 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6e  * If this is a n
594c0 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74  o-op implementat
594d0 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65  ion, implement e
594e0 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61 63  verything as mac
594f0 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ros..*/.#define 
59500 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
59510 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c 69  loc(X)    ((sqli
59520 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64  te3_mutex*)8).#d
59530 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75  efine sqlite3_mu
59540 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65 66  tex_free(X).#def
59550 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
59560 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66 69  x_enter(X).#defi
59570 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
59580 5f 74 72 79 28 58 29 20 20 20 20 20 20 53 51 4c  _try(X)      SQL
59590 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73  ITE_OK.#define s
595a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
595b0 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71  ve(X).#define sq
595c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
595d0 28 58 29 20 20 20 20 20 31 0a 23 64 65 66 69 6e  (X)     1.#defin
595e0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
595f0 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23 64  notheld(X)  1.#d
59600 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74  efine sqlite3Mut
59610 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 20 20  exAlloc(X)      
59620 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a  ((sqlite3_mutex*
59630 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  )8).#define sqli
59640 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 20 20  te3MutexInit()  
59650 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a        SQLITE_OK.
59660 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d  #define sqlite3M
59670 75 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 69 66  utexEnd().#endif
59680 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49   /* defined(SQLI
59690 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 20 2a  TE_MUTEX_OMIT) *
596a0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
596b0 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e  ** End of mutex.
596c0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
596d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
596e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
596f0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
59700 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
59710 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
59720 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
59730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59740 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  */.../*.** Each 
59750 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f  database file to
59760 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20   be accessed by 
59770 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 61 6e  the system is an
59780 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20   instance.** of 
59790 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
597a0 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 20  ructure.  There 
597b0 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f  are normally two
597c0 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74   of these struct
597d0 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73  ures.** in the s
597e0 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 72 61  qlite.aDb[] arra
597f0 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 74 68  y.  aDb[0] is th
59800 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
59810 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b  file and.** aDb[
59820 31 5d 20 69 73 20 74 68 65 20 64 61 74 61 62 61  1] is the databa
59830 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20  se file used to 
59840 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 74  hold temporary t
59850 61 62 6c 65 73 2e 20 20 41 64 64 69 74 69 6f 6e  ables.  Addition
59860 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20  al.** databases 
59870 6d 61 79 20 62 65 20 61 74 74 61 63 68 65 64 2e  may be attached.
59880 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 7b 0a  .*/.struct Db {.
59890 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
598a0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
598b0 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  f this database 
598c0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b  */.  Btree *pBt;
598d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
598e0 20 42 2a 54 72 65 65 20 73 74 72 75 63 74 75 72   B*Tree structur
598f0 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62  e for this datab
59900 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38  ase file */.  u8
59910 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20   inTrans;       
59920 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69     /* 0: not wri
59930 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73  table.  1: Trans
59940 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63  action.  2: Chec
59950 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73  kpoint */.  u8 s
59960 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20  afety_level;    
59970 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73 69   /* How aggressi
59980 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64 61  ve at syncing da
59990 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20  ta to disk */.  
599a0 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b  Schema *pSchema;
599b0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
599c0 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 65  to database sche
599d0 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 61  ma (possibly sha
599e0 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  red) */.};../*.*
599f0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
59a00 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
59a10 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 20  tructure stores 
59a20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  a database schem
59a30 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  a..**.** If ther
59a40 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 6c  e are no virtual
59a50 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 72   tables configur
59a60 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 6d  ed in this schem
59a70 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61  a, the.** Schema
59a80 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 20  .db variable is 
59a90 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74  set to NULL. Aft
59aa0 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69 72  er the first vir
59ab0 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61  tual table.** ha
59ac0 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 74  s been added, it
59ad0 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74   is set to point
59ae0 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
59af0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20   connection .** 
59b00 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74  used to create t
59b10 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f  he connection. O
59b20 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 61  nce a virtual ta
59b30 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20  ble has been.** 
59b40 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 68  added to the Sch
59b50 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 6e  ema structure an
59b60 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 20  d the Schema.db 
59b70 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 74  variable populat
59b80 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61  ed, .** only tha
59b90 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  t database conne
59ba0 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 68  ction may use th
59bb0 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 70  e Schema to prep
59bc0 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  are .** statemen
59bd0 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63  ts..*/.struct Sc
59be0 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 68  hema {.  int sch
59bf0 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a  ema_cookie;   /*
59c00 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   Database schema
59c10 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20   version number 
59c20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f  for this file */
59c30 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 3b  .  Hash tblHash;
59c40 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74          /* All t
59c50 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 79  ables indexed by
59c60 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20   name */.  Hash 
59c70 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 20  idxHash;        
59c80 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69  /* All (named) i
59c90 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 62  ndices indexed b
59ca0 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68  y name */.  Hash
59cb0 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 20   trigHash;      
59cc0 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 73   /* All triggers
59cd0 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65   indexed by name
59ce0 20 2a 2f 0a 20 20 48 61 73 68 20 66 6b 65 79 48   */.  Hash fkeyH
59cf0 61 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c  ash;       /* Al
59d00 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 62  l foreign keys b
59d10 79 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 62  y referenced tab
59d20 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 61 62  le name */.  Tab
59d30 6c 65 20 2a 70 53 65 71 54 61 62 3b 20 20 20 20  le *pSeqTab;    
59d40 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f    /* The sqlite_
59d50 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75  sequence table u
59d60 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 52 45  sed by AUTOINCRE
59d70 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c  MENT */.  u8 fil
59d80 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f  e_format;      /
59d90 2a 20 53 63 68 65 6d 61 20 66 6f 72 6d 61 74 20  * Schema format 
59da0 76 65 72 73 69 6f 6e 20 66 6f 72 20 74 68 69 73  version for this
59db0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e   file */.  u8 en
59dc0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
59dd0 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67  /* Text encoding
59de0 20 75 73 65 64 20 62 79 20 74 68 69 73 20 64 61   used by this da
59df0 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 31 36 20  tabase */.  u16 
59e00 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
59e10 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 69   /* Flags associ
59e20 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 73  ated with this s
59e30 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63  chema */.  int c
59e40 61 63 68 65 5f 73 69 7a 65 3b 20 20 20 20 20 20  ache_size;      
59e50 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
59e60 65 73 20 74 6f 20 75 73 65 20 69 6e 20 74 68 65  es to use in the
59e70 20 63 61 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65   cache */.#ifnde
59e80 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
59e90 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c  RTUALTABLE.  sql
59ea0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
59eb0 20 20 2f 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e    /* "Owner" con
59ec0 6e 65 63 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d  nection. See com
59ed0 6d 65 6e 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65  ment above */.#e
59ee0 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  ndif.};../*.** T
59ef0 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20  hese macros can 
59f00 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c  be used to test,
59f10 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62   set, or clear b
59f20 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44  its in the .** D
59f30 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a  b.flags field..*
59f40 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73 50  /.#define DbHasP
59f50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20  roperty(D,I,P)  
59f60 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d     (((D)->aDb[I]
59f70 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26  .pSchema->flags&
59f80 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69  (P))==(P)).#defi
59f90 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f 70 65  ne DbHasAnyPrope
59fa0 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28 28 44  rty(D,I,P)  (((D
59fb0 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d  )->aDb[I].pSchem
59fc0 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30  a->flags&(P))!=0
59fd0 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65 74 50  ).#define DbSetP
59fe0 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20  roperty(D,I,P)  
59ff0 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70     (D)->aDb[I].p
5a000 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28  Schema->flags|=(
5a010 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43 6c 65  P).#define DbCle
5a020 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50  arProperty(D,I,P
5a030 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e  )   (D)->aDb[I].
5a040 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d  pSchema->flags&=
5a050 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  ~(P)../*.** Allo
5a060 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74  wed values for t
5a070 68 65 20 44 42 2e 66 6c 61 67 73 20 66 69 65 6c  he DB.flags fiel
5a080 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f  d..**.** The DB_
5a090 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61  SchemaLoaded fla
5a0a0 67 20 69 73 20 73 65 74 20 61 66 74 65 72 20 74  g is set after t
5a0b0 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
5a0c0 6d 61 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72  ma has been.** r
5a0d0 65 61 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61  ead into interna
5a0e0 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a  l hash tables..*
5a0f0 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65 73 65 74 56  *.** DB_UnresetV
5a100 69 65 77 73 20 6d 65 61 6e 73 20 74 68 61 74 20  iews means that 
5a110 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 69 65 77  one or more view
5a120 73 20 68 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61  s have column na
5a130 6d 65 73 20 74 68 61 74 0a 2a 2a 20 68 61 76 65  mes that.** have
5a140 20 62 65 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74   been filled out
5a150 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 6d 61  .  If the schema
5a160 20 63 68 61 6e 67 65 73 2c 20 74 68 65 73 65 20   changes, these 
5a170 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67  column names mig
5a180 68 74 0a 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e  ht.** changes an
5a190 64 20 73 6f 20 74 68 65 20 76 69 65 77 20 77 69  d so the view wi
5a1a0 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65  ll need to be re
5a1b0 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  set..*/.#define 
5a1c0 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20  DB_SchemaLoaded 
5a1d0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 54 68     0x0001  /* Th
5a1e0 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65 65  e schema has bee
5a1f0 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66  n loaded */.#def
5a200 69 6e 65 20 44 42 5f 55 6e 72 65 73 65 74 56 69  ine DB_UnresetVi
5a210 65 77 73 20 20 20 20 30 78 30 30 30 32 20 20 2f  ews    0x0002  /
5a220 2a 20 53 6f 6d 65 20 76 69 65 77 73 20 68 61 76  * Some views hav
5a230 65 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e  e defined column
5a240 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e   names */.#defin
5a250 65 20 44 42 5f 45 6d 70 74 79 20 20 20 20 20 20  e DB_Empty      
5a260 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20       0x0004  /* 
5a270 54 68 65 20 66 69 6c 65 20 69 73 20 65 6d 70 74  The file is empt
5a280 79 20 28 6c 65 6e 67 74 68 20 30 20 62 79 74 65  y (length 0 byte
5a290 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  s) */../*.** The
5a2a0 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65   number of diffe
5a2b0 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74 68  rent kinds of th
5a2c0 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20 62 65  ings that can be
5a2d0 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 75 73 69 6e   limited.** usin
5a2e0 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 69  g the sqlite3_li
5a2f0 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e  mit() interface.
5a300 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
5a310 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 4c 49  TE_N_LIMIT (SQLI
5a320 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52  TE_LIMIT_TRIGGER
5a330 5f 44 45 50 54 48 2b 31 29 0a 0a 2f 2a 0a 2a 2a  _DEPTH+1)../*.**
5a340 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f   Lookaside mallo
5a350 63 20 69 73 20 61 20 73 65 74 20 6f 66 20 66 69  c is a set of fi
5a360 78 65 64 2d 73 69 7a 65 20 62 75 66 66 65 72 73  xed-size buffers
5a370 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
5a380 64 0a 2a 2a 20 74 6f 20 73 61 74 69 73 66 79 20  d.** to satisfy 
5a390 73 6d 61 6c 6c 20 74 72 61 6e 73 69 65 6e 74 20  small transient 
5a3a0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
5a3b0 6e 20 72 65 71 75 65 73 74 73 20 66 6f 72 20 6f  n requests for o
5a3c0 62 6a 65 63 74 73 0a 2a 2a 20 61 73 73 6f 63 69  bjects.** associ
5a3d0 61 74 65 64 20 77 69 74 68 20 61 20 70 61 72 74  ated with a part
5a3e0 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20  icular database 
5a3f0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65  connection.  The
5a400 20 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61   use of.** looka
5a410 73 69 64 65 20 6d 61 6c 6c 6f 63 20 70 72 6f 76  side malloc prov
5a420 69 64 65 73 20 61 20 73 69 67 6e 69 66 69 63 61  ides a significa
5a430 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 65  nt performance e
5a440 6e 68 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 61  nhancement.** (a
5a450 70 70 72 6f 78 20 31 30 25 29 20 62 79 20 61 76  pprox 10%) by av
5a460 6f 69 64 69 6e 67 20 6e 75 6d 65 72 6f 75 73 20  oiding numerous 
5a470 6d 61 6c 6c 6f 63 2f 66 72 65 65 20 72 65 71 75  malloc/free requ
5a480 65 73 74 73 20 77 68 69 6c 65 20 70 61 72 73 69  ests while parsi
5a490 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d  ng.** SQL statem
5a4a0 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ents..**.** The 
5a4b0 4c 6f 6f 6b 61 73 69 64 65 20 73 74 72 75 63 74  Lookaside struct
5a4c0 75 72 65 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67  ure holds config
5a4d0 75 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74  uration informat
5a4e0 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a  ion about the.**
5a4f0 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f   lookaside mallo
5a500 63 20 73 75 62 73 79 73 74 65 6d 2e 20 20 45 61  c subsystem.  Ea
5a510 63 68 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d  ch available mem
5a520 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  ory allocation i
5a530 6e 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69  n.** the lookasi
5a540 64 65 20 73 75 62 73 79 73 74 65 6d 20 69 73 20  de subsystem is 
5a550 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b  stored on a link
5a560 65 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61  ed list of Looka
5a570 73 69 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a 65  sideSlot.** obje
5a580 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61  cts..**.** Looka
5a590 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  side allocations
5a5a0 20 61 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65   are only allowe
5a5b0 64 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 74 68  d for objects th
5a5c0 61 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 65  at are associate
5a5d0 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74  d.** with a part
5a5e0 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20  icular database 
5a5f0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 48 65 6e  connection.  Hen
5a600 63 65 2c 20 73 63 68 65 6d 61 20 69 6e 66 6f 72  ce, schema infor
5a610 6d 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a  mation cannot.**
5a620 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f   be stored in lo
5a630 6f 6b 61 73 69 64 65 20 62 65 63 61 75 73 65 20  okaside because 
5a640 69 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 20  in shared cache 
5a650 6d 6f 64 65 20 74 68 65 20 73 63 68 65 6d 61 20  mode the schema 
5a660 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69  information.** i
5a670 73 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c 74  s shared by mult
5a680 69 70 6c 65 20 64 61 74 61 62 61 73 65 20 63 6f  iple database co
5a690 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68 65 72  nnections.  Ther
5a6a0 65 66 6f 72 65 2c 20 77 68 69 6c 65 20 70 61 72  efore, while par
5a6b0 73 69 6e 67 0a 2a 2a 20 73 63 68 65 6d 61 20 69  sing.** schema i
5a6c0 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 20  nformation, the 
5a6d0 4c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  Lookaside.bEnabl
5a6e0 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72  ed flag is clear
5a6f0 65 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 6c 6f  ed so that.** lo
5a700 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69  okaside allocati
5a710 6f 6e 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64  ons are not used
5a720 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 68   to construct th
5a730 65 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73  e schema objects
5a740 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b  ..*/.struct Look
5a750 61 73 69 64 65 20 7b 0a 20 20 75 31 36 20 73 7a  aside {.  u16 sz
5a760 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5a770 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63    /* Size of eac
5a780 68 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65  h buffer in byte
5a790 73 20 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c  s */.  u8 bEnabl
5a7a0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ed;            /
5a7b0 2a 20 46 61 6c 73 65 20 74 6f 20 64 69 73 61 62  * False to disab
5a7c0 6c 65 20 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 65  le new lookaside
5a7d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a   allocations */.
5a7e0 20 20 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20    u8 bMalloced; 
5a7f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
5a800 65 20 69 66 20 70 53 74 61 72 74 20 6f 62 74 61  e if pStart obta
5a810 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
5a820 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20  3_malloc() */.  
5a830 69 6e 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 20  int nOut;       
5a840 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
5a850 72 20 6f 66 20 62 75 66 66 65 72 73 20 63 75 72  r of buffers cur
5a860 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f  rently checked o
5a870 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75  ut */.  int mxOu
5a880 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
5a890 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 72  /* Highwater mar
5a8a0 6b 20 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20  k for nOut */.  
5a8b0 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70  LookasideSlot *p
5a8c0 46 72 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20  Free;   /* List 
5a8d0 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 75 66  of available buf
5a8e0 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  fers */.  void *
5a8f0 70 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 20  pStart;         
5a900 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
5a910 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d  of available mem
5a920 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 76  ory space */.  v
5a930 6f 69 64 20 2a 70 45 6e 64 3b 20 20 20 20 20 20  oid *pEnd;      
5a940 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
5a950 62 79 74 65 20 70 61 73 74 20 65 6e 64 20 6f 66  byte past end of
5a960 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65   available space
5a970 20 2a 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f   */.};.struct Lo
5a980 6f 6b 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20  okasideSlot {.  
5a990 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70  LookasideSlot *p
5a9a0 4e 65 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74  Next;    /* Next
5a9b0 20 62 75 66 66 65 72 20 69 6e 20 74 68 65 20 6c   buffer in the l
5a9c0 69 73 74 20 6f 66 20 66 72 65 65 20 62 75 66 66  ist of free buff
5a9d0 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ers */.};../*.**
5a9e0 20 41 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f   A hash table fo
5a9f0 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  r function defin
5aa00 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61  itions..**.** Ha
5aa10 73 68 20 65 61 63 68 20 46 75 6e 63 44 65 66 20  sh each FuncDef 
5aa20 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6f  structure into o
5aa30 6e 65 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65  ne of the FuncDe
5aa40 66 48 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e  fHash.a[] slots.
5aa50 0a 2a 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61  .** Collisions a
5aa60 72 65 20 6f 6e 20 74 68 65 20 46 75 6e 63 44 65  re on the FuncDe
5aa70 66 2e 70 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a  f.pHash chain..*
5aa80 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 66  /.struct FuncDef
5aa90 48 61 73 68 20 7b 0a 20 20 46 75 6e 63 44 65 66  Hash {.  FuncDef
5aaa0 20 2a 61 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f   *a[23];       /
5aab0 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72  * Hash table for
5aac0 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b   functions */.};
5aad0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74  ../*.** Each dat
5aae0 61 62 61 73 65 20 69 73 20 61 6e 20 69 6e 73 74  abase is an inst
5aaf0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
5ab00 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
5ab10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
5ab20 65 2e 6c 61 73 74 52 6f 77 69 64 20 72 65 63 6f  e.lastRowid reco
5ab30 72 64 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73  rds the last ins
5ab40 65 72 74 20 72 6f 77 69 64 20 67 65 6e 65 72 61  ert rowid genera
5ab50 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73  ted by an.** ins
5ab60 65 72 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ert statement.  
5ab70 49 6e 73 65 72 74 73 20 6f 6e 20 76 69 65 77 73  Inserts on views
5ab80 20 64 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 69   do not affect i
5ab90 74 73 20 76 61 6c 75 65 2e 20 20 45 61 63 68 0a  ts value.  Each.
5aba0 2a 2a 20 74 72 69 67 67 65 72 20 68 61 73 20 69  ** trigger has i
5abb0 74 73 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20  ts own context, 
5abc0 73 6f 20 74 68 61 74 20 6c 61 73 74 52 6f 77 69  so that lastRowi
5abd0 64 20 63 61 6e 20 62 65 20 75 70 64 61 74 65 64  d can be updated
5abe0 20 69 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 67   inside.** trigg
5abf0 65 72 73 20 61 73 20 75 73 75 61 6c 2e 20 20 54  ers as usual.  T
5ac00 68 65 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75  he previous valu
5ac10 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72  e will be restor
5ac20 65 64 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67  ed once the trig
5ac30 67 65 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55  ger.** exits.  U
5ac40 70 6f 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 62  pon entering a b
5ac50 65 66 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 64  efore or instead
5ac60 20 6f 66 20 74 72 69 67 67 65 72 2c 20 6c 61 73   of trigger, las
5ac70 74 52 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20  tRowid is no.** 
5ac80 6c 6f 6e 67 65 72 20 28 73 69 6e 63 65 20 61 66  longer (since af
5ac90 74 65 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e  ter version 2.8.
5aca0 31 32 29 20 72 65 73 65 74 20 74 6f 20 2d 31 2e  12) reset to -1.
5acb0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
5acc0 65 2e 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 6e  e.nChange does n
5acd0 6f 74 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73  ot count changes
5ace0 20 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 73   within triggers
5acf0 20 61 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a   and keeps no.**
5ad00 20 63 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 73   context.  It is
5ad10 20 72 65 73 65 74 20 61 74 20 73 74 61 72 74 20   reset at start 
5ad20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2e  of sqlite3_exec.
5ad30 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c  .** The sqlite.l
5ad40 73 43 68 61 6e 67 65 20 72 65 70 72 65 73 65 6e  sChange represen
5ad50 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ts the number of
5ad60 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79   changes made by
5ad70 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73   the last.** ins
5ad80 65 72 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 20  ert, update, or 
5ad90 64 65 6c 65 74 65 20 73 74 61 74 65 6d 65 6e 74  delete statement
5ada0 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 63 6f  .  It remains co
5adb0 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75  nstant throughou
5adc0 74 20 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20  t the.** length 
5add0 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61  of a statement a
5ade0 6e 64 20 69 73 20 74 68 65 6e 20 75 70 64 61 74  nd is then updat
5adf0 65 64 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e  ed by OP_SetCoun
5ae00 74 73 2e 20 20 49 74 20 6b 65 65 70 73 20 61 0a  ts.  It keeps a.
5ae10 2a 2a 20 63 6f 6e 74 65 78 74 20 73 74 61 63 6b  ** context stack
5ae20 20 6a 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 52   just like lastR
5ae30 6f 77 69 64 20 73 6f 20 74 68 61 74 20 74 68 65  owid so that the
5ae40 20 63 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 65   count of change
5ae50 73 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 72  s.** within a tr
5ae60 69 67 67 65 72 20 69 73 20 6e 6f 74 20 73 65 65  igger is not see
5ae70 6e 20 6f 75 74 73 69 64 65 20 74 68 65 20 74 72  n outside the tr
5ae80 69 67 67 65 72 2e 20 20 43 68 61 6e 67 65 73 20  igger.  Changes 
5ae90 74 6f 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a  to views do not.
5aea0 2a 2a 20 61 66 66 65 63 74 20 74 68 65 20 76 61  ** affect the va
5aeb0 6c 75 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e  lue of lsChange.
5aec0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 63  .** The sqlite.c
5aed0 73 43 68 61 6e 67 65 20 6b 65 65 70 73 20 74 72  sChange keeps tr
5aee0 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ack of the numbe
5aef0 72 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 61  r of current cha
5af00 6e 67 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74  nges (since.** t
5af10 68 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e  he last statemen
5af20 74 29 20 61 6e 64 20 69 73 20 75 73 65 64 20 74  t) and is used t
5af30 6f 20 75 70 64 61 74 65 20 73 71 6c 69 74 65 5f  o update sqlite_
5af40 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20  lsChange..**.** 
5af50 54 68 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61  The member varia
5af60 62 6c 65 73 20 73 71 6c 69 74 65 2e 65 72 72 43  bles sqlite.errC
5af70 6f 64 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72  ode, sqlite.zErr
5af80 4d 73 67 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a  Msg and sqlite.z
5af90 45 72 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72  ErrMsg16.** stor
5afa0 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  e the most recen
5afb0 74 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  t error code and
5afc0 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2c  , if applicable,
5afd0 20 73 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20   string. The.** 
5afe0 69 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f  internal functio
5aff0 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29  n sqlite3Error()
5b000 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20   is used to set 
5b010 74 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 0a  these variables.
5b020 2a 2a 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e  ** consistently.
5b030 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
5b040 65 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  e3 {.  sqlite3_v
5b050 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20  fs *pVfs;       
5b060 20 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72       /* OS Inter
5b070 66 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44  face */.  int nD
5b080 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
5b090 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
5b0a0 72 20 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75  r of backends cu
5b0b0 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a  rrently in use *
5b0c0 2f 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20 20  /.  Db *aDb;    
5b0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b0e0 20 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64    /* All backend
5b0f0 73 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  s */.  int flags
5b100 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5b110 20 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61       /* Miscella
5b120 6e 65 6f 75 73 20 66 6c 61 67 73 2e 20 53 65 65  neous flags. See
5b130 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20   below */.  int 
5b140 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20  openFlags;      
5b150 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
5b160 67 73 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  gs passed to sql
5b170 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29  ite3_vfs.xOpen()
5b180 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64   */.  int errCod
5b190 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
5b1a0 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65      /* Most rece
5b1b0 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 28 53  nt error code (S
5b1c0 51 4c 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e  QLITE_*) */.  in
5b1d0 74 20 65 72 72 4d 61 73 6b 3b 20 20 20 20 20 20  t errMask;      
5b1e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 26              /* &
5b1f0 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69   result codes wi
5b200 74 68 20 74 68 69 73 20 62 65 66 6f 72 65 20 72  th this before r
5b210 65 74 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38  eturning */.  u8
5b220 20 61 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20   autoCommit;    
5b230 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
5b240 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66  he auto-commit f
5b250 6c 61 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d  lag. */.  u8 tem
5b260 70 5f 73 74 6f 72 65 3b 20 20 20 20 20 20 20 20  p_store;        
5b270 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 66 69          /* 1: fi
5b280 6c 65 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20  le 2: memory 0: 
5b290 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20  default */.  u8 
5b2a0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20  mallocFailed;   
5b2b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
5b2c0 75 65 20 69 66 20 77 65 20 68 61 76 65 20 73 65  ue if we have se
5b2d0 65 6e 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  en a malloc fail
5b2e0 75 72 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74  ure */.  u8 dflt
5b2f0 4c 6f 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20  LockMode;       
5b300 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c         /* Defaul
5b310 74 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66  t locking-mode f
5b320 6f 72 20 61 74 74 61 63 68 65 64 20 64 62 73 20  or attached dbs 
5b330 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75 72  */.  u8 dfltJour
5b340 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20  nalMode;        
5b350 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f     /* Default jo
5b360 75 72 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61  urnal mode for a
5b370 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20  ttached dbs */. 
5b380 20 73 69 67 6e 65 64 20 63 68 61 72 20 6e 65 78   signed char nex
5b390 74 41 75 74 6f 76 61 63 3b 20 20 20 20 20 20 2f  tAutovac;      /
5b3a0 2a 20 41 75 74 6f 76 61 63 20 73 65 74 74 69 6e  * Autovac settin
5b3b0 67 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 69  g after VACUUM i
5b3c0 66 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e  f >=0 */.  int n
5b3d0 65 78 74 50 61 67 65 73 69 7a 65 3b 20 20 20 20  extPagesize;    
5b3e0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
5b3f0 73 69 7a 65 20 61 66 74 65 72 20 56 41 43 55 55  size after VACUU
5b400 4d 20 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74  M if >0 */.  int
5b410 20 6e 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20   nTable;        
5b420 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
5b430 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69  mber of tables i
5b440 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  n the database *
5b450 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66  /.  CollSeq *pDf
5b460 6c 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  ltColl;         
5b470 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74    /* The default
5b480 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
5b490 6e 63 65 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a  nce (BINARY) */.
5b4a0 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b    i64 lastRowid;
5b4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b4c0 2f 2a 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74  /* ROWID of most
5b4d0 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74 20 28   recent insert (
5b4e0 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20  see above) */.  
5b4f0 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20  u32 magic;      
5b500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5b510 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f   Magic number fo
5b520 72 20 64 65 74 65 63 74 20 6c 69 62 72 61 72 79  r detect library
5b530 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e 74   misuse */.  int
5b540 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20   nChange;       
5b550 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
5b560 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
5b570 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
5b580 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 61  ) */.  int nTota
5b590 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20  lChange;        
5b5a0 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65       /* Value re
5b5b0 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
5b5c0 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  3_total_changes(
5b5d0 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ) */.  sqlite3_m
5b5e0 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20  utex *mutex;    
5b5f0 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
5b600 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e  on mutex */.  in
5b610 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  t aLimit[SQLITE_
5b620 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 4c  N_LIMIT];   /* L
5b630 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75 63  imits */.  struc
5b640 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e 66  t sqlite3InitInf
5b650 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f  o {      /* Info
5b660 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75 72  rmation used dur
5b670 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ing initializati
5b680 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44  on */.    int iD
5b690 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
5b6a0 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 61        /* When ba
5b6b0 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69 74  ck is being init
5b6c0 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 69  ialized */.    i
5b6d0 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 20  nt newTnum;     
5b6e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
5b6f0 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20  otpage of table 
5b700 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65  being initialize
5b710 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 79  d */.    u8 busy
5b720 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5b730 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 20       /* TRUE if 
5b740 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 61  currently initia
5b750 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 20 20 75 38  lizing */.    u8
5b760 20 6f 72 70 68 61 6e 54 72 69 67 67 65 72 3b 20   orphanTrigger; 
5b770 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73            /* Las
5b780 74 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f  t statement is o
5b790 72 70 68 61 6e 65 64 20 54 45 4d 50 20 74 72 69  rphaned TEMP tri
5b7a0 67 67 65 72 20 2a 2f 0a 20 20 7d 20 69 6e 69 74  gger */.  } init
5b7b0 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 6e 73 69  ;.  int nExtensi
5b7c0 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  on;             
5b7d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c    /* Number of l
5b7e0 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f 6e 73  oaded extensions
5b7f0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78   */.  void **aEx
5b800 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20  tension;        
5b810 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
5b820 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 68  shared library h
5b830 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72 75  andles */.  stru
5b840 63 74 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20  ct Vdbe *pVdbe; 
5b850 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73            /* Lis
5b860 74 20 6f 66 20 61 63 74 69 76 65 20 76 69 72 74  t of active virt
5b870 75 61 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0a  ual machines */.
5b880 20 20 69 6e 74 20 61 63 74 69 76 65 56 64 62 65    int activeVdbe
5b890 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Cnt;            
5b8a0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56 44 42  /* Number of VDB
5b8b0 45 73 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65  Es currently exe
5b8c0 63 75 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  cuting */.  int 
5b8d0 77 72 69 74 65 56 64 62 65 43 6e 74 3b 20 20 20  writeVdbeCnt;   
5b8e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
5b8f0 62 65 72 20 6f 66 20 61 63 74 69 76 65 20 56 44  ber of active VD
5b900 42 45 73 20 74 68 61 74 20 61 72 65 20 77 72 69  BEs that are wri
5b910 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 28  ting */.  void (
5b920 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63  *xTrace)(void*,c
5b930 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 20 20 20  onst char*);    
5b940 20 20 20 20 2f 2a 20 54 72 61 63 65 20 66 75 6e      /* Trace fun
5b950 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
5b960 2a 70 54 72 61 63 65 41 72 67 3b 20 20 20 20 20  *pTraceArg;     
5b970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b980 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
5b990 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 66 75   to the trace fu
5b9a0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
5b9b0 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69   (*xProfile)(voi
5b9c0 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75  d*,const char*,u
5b9d0 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69  64);  /* Profili
5b9e0 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  ng function */. 
5b9f0 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 41   void *pProfileA
5ba00 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
5ba10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
5ba20 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 6c  gument to profil
5ba30 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  e function */.  
5ba40 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 67  void *pCommitArg
5ba50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5ba60 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
5ba70 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b   xCommitCallback
5ba80 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 28  () */   .  int (
5ba90 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b  *xCommitCallback
5baa0 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a 20  )(void*);    /* 
5bab0 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79  Invoked at every
5bac0 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f   commit. */.  vo
5bad0 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 67  id *pRollbackArg
5bae0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5baf0 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78  /* Argument to x
5bb00 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b  RollbackCallback
5bb10 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 20  () */   .  void 
5bb20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62  (*xRollbackCallb
5bb30 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20  ack)(void*); /* 
5bb40 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79  Invoked at every
5bb50 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f   commit. */.  vo
5bb60 69 64 20 2a 70 55 70 64 61 74 65 41 72 67 3b 0a  id *pUpdateArg;.
5bb70 20 20 76 6f 69 64 20 28 2a 78 55 70 64 61 74 65    void (*xUpdate
5bb80 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c  Callback)(void*,
5bb90 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  int, const char*
5bba0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c  ,const char*,sql
5bbb0 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f  ite_int64);.  vo
5bbc0 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29  id(*xCollNeeded)
5bbd0 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c  (void*,sqlite3*,
5bbe0 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e  int eTextRep,con
5bbf0 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f 69  st char*);.  voi
5bc00 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36  d(*xCollNeeded16
5bc10 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
5bc20 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
5bc30 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f  nst void*);.  vo
5bc40 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41  id *pCollNeededA
5bc50 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  rg;.  sqlite3_va
5bc60 6c 75 65 20 2a 70 45 72 72 3b 20 20 20 20 20 20  lue *pErr;      
5bc70 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65      /* Most rece
5bc80 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  nt error message
5bc90 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72   */.  char *zErr
5bca0 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Msg;            
5bcb0 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65      /* Most rece
5bcc0 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  nt error message
5bcd0 20 28 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 29   (UTF-8 encoded)
5bce0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72   */.  char *zErr
5bcf0 4d 73 67 31 36 3b 20 20 20 20 20 20 20 20 20 20  Msg16;          
5bd00 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65      /* Most rece
5bd10 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  nt error message
5bd20 20 28 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64   (UTF-16 encoded
5bd30 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20  ) */.  union {. 
5bd40 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20     volatile int 
5bd50 69 73 49 6e 74 65 72 72 75 70 74 65 64 3b 20 2f  isInterrupted; /
5bd60 2a 20 54 72 75 65 20 69 66 20 73 71 6c 69 74 65  * True if sqlite
5bd70 33 5f 69 6e 74 65 72 72 75 70 74 20 68 61 73 20  3_interrupt has 
5bd80 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 20  been called */. 
5bd90 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73 65     double notUse
5bda0 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  d1;            /
5bdb0 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d 20  * Spacer */.  } 
5bdc0 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 20  u1;.  Lookaside 
5bdd0 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20  lookaside;      
5bde0 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 65      /* Lookaside
5bdf0 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 72   malloc configur
5be00 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66  ation */.#ifndef
5be10 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
5be20 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74  HORIZATION.  int
5be30 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c   (*xAuth)(void*,
5be40 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  int,const char*,
5be50 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
5be60 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
5be70 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ar*);.          
5be80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5be90 20 20 20 20 20 20 2f 2a 20 41 63 63 65 73 73 20        /* Access 
5bea0 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75  authorization fu
5beb0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
5bec0 20 2a 70 41 75 74 68 41 72 67 3b 20 20 20 20 20   *pAuthArg;     
5bed0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74            /* 1st
5bee0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
5bef0 20 61 63 63 65 73 73 20 61 75 74 68 20 66 75 6e   access auth fun
5bf00 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a  ction */.#endif.
5bf10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
5bf20 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c  MIT_PROGRESS_CAL
5bf30 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50  LBACK.  int (*xP
5bf40 72 6f 67 72 65 73 73 29 28 76 6f 69 64 20 2a 29  rogress)(void *)
5bf50 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f  ;     /* The pro
5bf60 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a  gress callback *
5bf70 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 67 72  /.  void *pProgr
5bf80 65 73 73 41 72 67 3b 20 20 20 20 20 20 20 20 20  essArg;         
5bf90 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
5bfa0 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61   the progress ca
5bfb0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20  llback */.  int 
5bfc0 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b 20 20 20  nProgressOps;   
5bfd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
5bfe0 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 66  ber of opcodes f
5bff0 6f 72 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c  or progress call
5c000 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  back */.#endif.#
5c010 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
5c020 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
5c030 20 20 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20    Hash aModule; 
5c040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c050 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20  /* populated by 
5c060 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
5c070 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61 62  odule() */.  Tab
5c080 6c 65 20 2a 70 56 54 61 62 3b 20 20 20 20 20 20  le *pVTab;      
5c090 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 74             /* vt
5c0a0 61 62 20 77 69 74 68 20 61 63 74 69 76 65 20 43  ab with active C
5c0b0 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65 20 6d 65  onnect/Create me
5c0c0 74 68 6f 64 20 2a 2f 0a 20 20 56 54 61 62 6c 65  thod */.  VTable
5c0d0 20 2a 2a 61 56 54 72 61 6e 73 3b 20 20 20 20 20   **aVTrans;     
5c0e0 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75          /* Virtu
5c0f0 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 20 6f  al tables with o
5c100 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  pen transactions
5c110 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61 6e   */.  int nVTran
5c120 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
5c130 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64      /* Allocated
5c140 20 73 69 7a 65 20 6f 66 20 61 56 54 72 61 6e 73   size of aVTrans
5c150 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 44   */.  VTable *pD
5c160 69 73 63 6f 6e 6e 65 63 74 3b 20 20 20 20 2f 2a  isconnect;    /*
5c170 20 44 69 73 63 6f 6e 6e 65 63 74 20 74 68 65 73   Disconnect thes
5c180 65 20 69 6e 20 6e 65 78 74 20 73 71 6c 69 74 65  e in next sqlite
5c190 33 5f 70 72 65 70 61 72 65 28 29 20 2a 2f 0a 23  3_prepare() */.#
5c1a0 65 6e 64 69 66 0a 20 20 46 75 6e 63 44 65 66 48  endif.  FuncDefH
5c1b0 61 73 68 20 61 46 75 6e 63 3b 20 20 20 20 20 20  ash aFunc;      
5c1c0 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61        /* Hash ta
5c1d0 62 6c 65 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f  ble of connectio
5c1e0 6e 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20  n functions */. 
5c1f0 20 48 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20   Hash aCollSeq; 
5c200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5c210 2a 20 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20  * All collating 
5c220 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42  sequences */.  B
5c230 75 73 79 48 61 6e 64 6c 65 72 20 62 75 73 79 48  usyHandler busyH
5c240 61 6e 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20  andler;      /* 
5c250 42 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  Busy callback */
5c260 0a 20 20 69 6e 74 20 62 75 73 79 54 69 6d 65 6f  .  int busyTimeo
5c270 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
5c280 20 2f 2a 20 42 75 73 79 20 68 61 6e 64 6c 65 72   /* Busy handler
5c290 20 74 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 65   timeout, in mse
5c2a0 63 20 2a 2f 0a 20 20 44 62 20 61 44 62 53 74 61  c */.  Db aDbSta
5c2b0 74 69 63 5b 32 5d 3b 20 20 20 20 20 20 20 20 20  tic[2];         
5c2c0 20 20 20 20 20 2f 2a 20 53 74 61 74 69 63 20 73       /* Static s
5c2d0 70 61 63 65 20 66 6f 72 20 74 68 65 20 32 20 64  pace for the 2 d
5c2e0 65 66 61 75 6c 74 20 62 61 63 6b 65 6e 64 73 20  efault backends 
5c2f0 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a  */.  Savepoint *
5c300 70 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20  pSavepoint;     
5c310 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63     /* List of ac
5c320 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20  tive savepoints 
5c330 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f  */.  int nSavepo
5c340 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  int;            
5c350 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5c360 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  non-transaction 
5c370 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20  savepoints */.  
5c380 69 6e 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20  int nStatement; 
5c390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5c3a0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65   Number of neste
5c3b0 64 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e  d statement-tran
5c3c0 73 61 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 75  sactions  */.  u
5c3d0 38 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53  8 isTransactionS
5c3e0 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20  avepoint;    /* 
5c3f0 54 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 65  True if the oute
5c400 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 20  rmost savepoint 
5c410 69 73 20 61 20 54 53 20 2a 2f 0a 20 20 69 36 34  is a TS */.  i64
5c420 20 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20   nDeferredCons; 
5c430 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
5c440 74 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74  t deferred const
5c450 72 61 69 6e 74 73 20 74 68 69 73 20 74 72 61 6e  raints this tran
5c460 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 0a 23 69 66  saction. */..#if
5c470 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
5c480 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a  E_UNLOCK_NOTIFY.
5c490 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
5c4a0 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  ng variables are
5c4b0 20 61 6c 6c 20 70 72 6f 74 65 63 74 65 64 20 62   all protected b
5c4c0 79 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53  y the STATIC_MAS
5c4d0 54 45 52 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c  TER .  ** mutex,
5c4e0 20 6e 6f 74 20 62 79 20 73 71 6c 69 74 65 33 2e   not by sqlite3.
5c4f0 6d 75 74 65 78 2e 20 54 68 65 79 20 61 72 65 20  mutex. They are 
5c500 75 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20  used by code in 
5c510 6e 6f 74 69 66 79 2e 63 2e 20 0a 20 20 2a 2a 0a  notify.c. .  **.
5c520 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 55 6e 6c    ** When X.pUnl
5c530 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59  ockConnection==Y
5c540 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  , that means tha
5c550 74 20 58 20 69 73 20 77 61 69 74 69 6e 67 20 66  t X is waiting f
5c560 6f 72 20 59 20 74 6f 0a 20 20 2a 2a 20 75 6e 6c  or Y to.  ** unl
5c570 6f 63 6b 20 73 6f 20 74 68 61 74 20 69 74 20 63  ock so that it c
5c580 61 6e 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2a  an proceed..  **
5c590 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 42 6c  .  ** When X.pBl
5c5a0 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
5c5b0 3d 3d 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  ==Y, that means 
5c5c0 74 68 61 74 20 73 6f 6d 65 74 68 69 6e 67 20 74  that something t
5c5d0 68 61 74 20 58 20 74 72 69 65 64 0a 20 20 2a 2a  hat X tried.  **
5c5e0 20 74 72 69 65 64 20 74 6f 20 64 6f 20 72 65 63   tried to do rec
5c5f0 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 77 69 74  ently failed wit
5c600 68 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  h an SQLITE_LOCK
5c610 45 44 20 65 72 72 6f 72 20 64 75 65 20 74 6f 20  ED error due to 
5c620 6c 6f 63 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 20  locks.  ** held 
5c630 62 79 20 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  by Y..  */.  sql
5c640 69 74 65 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 43  ite3 *pBlockingC
5c650 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 6f  onnection; /* Co
5c660 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 63 61  nnection that ca
5c670 75 73 65 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  used SQLITE_LOCK
5c680 45 44 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ED */.  sqlite3 
5c690 2a 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69  *pUnlockConnecti
5c6a0 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  on;           /*
5c6b0 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 77   Connection to w
5c6c0 61 74 63 68 20 66 6f 72 20 75 6e 6c 6f 63 6b 20  atch for unlock 
5c6d0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 6e 6c 6f  */.  void *pUnlo
5c6e0 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  ckArg;          
5c6f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
5c700 67 75 6d 65 6e 74 20 74 6f 20 78 55 6e 6c 6f 63  gument to xUnloc
5c710 6b 4e 6f 74 69 66 79 20 2a 2f 0a 20 20 76 6f 69  kNotify */.  voi
5c720 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  d (*xUnlockNotif
5c730 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29  y)(void **, int)
5c740 3b 20 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74  ;  /* Unlock not
5c750 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a  ify callback */.
5c760 20 20 73 71 6c 69 74 65 33 20 2a 70 4e 65 78 74    sqlite3 *pNext
5c770 42 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20  Blocked;        
5c780 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73 74 20  /* Next in list 
5c790 6f 66 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63  of all blocked c
5c7a0 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 65  onnections */.#e
5c7b0 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  ndif.};../*.** A
5c7c0 20 6d 61 63 72 6f 20 74 6f 20 64 69 73 63 6f 76   macro to discov
5c7d0 65 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  er the encoding 
5c7e0 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a  of a database..*
5c7f0 2f 0a 23 64 65 66 69 6e 65 20 45 4e 43 28 64 62  /.#define ENC(db
5c800 29 20 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e  ) ((db)->aDb[0].
5c810 70 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f  pSchema->enc)../
5c820 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61  *.** Possible va
5c830 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c  lues for the sql
5c840 69 74 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72  ite.flags and or
5c850 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 73   Db.flags fields
5c860 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74  ..**.** On sqlit
5c870 65 2e 66 6c 61 67 73 2c 20 74 68 65 20 53 51 4c  e.flags, the SQL
5c880 49 54 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75  ITE_InTrans valu
5c890 65 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20  e means that we 
5c8a0 68 61 76 65 0a 2a 2a 20 65 78 65 63 75 74 65 64  have.** executed
5c8b0 20 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62   a BEGIN.  On Db
5c8c0 2e 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49  .flags, SQLITE_I
5c8d0 6e 54 72 61 6e 73 20 6d 65 61 6e 73 20 61 20 73  nTrans means a s
5c8e0 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e  tatement.** tran
5c8f0 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76  saction is activ
5c900 65 20 6f 6e 20 74 68 61 74 20 70 61 72 74 69 63  e on that partic
5c910 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 69  ular database fi
5c920 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  le..*/.#define S
5c930 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 20  QLITE_VdbeTrace 
5c940 20 20 20 20 20 30 78 30 30 30 30 30 30 30 31 20       0x00000001 
5c950 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63   /* True to trac
5c960 65 20 56 44 42 45 20 65 78 65 63 75 74 69 6f 6e  e VDBE execution
5c970 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5c980 54 45 5f 49 6e 54 72 61 6e 73 20 20 20 20 20 20  TE_InTrans      
5c990 20 20 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a    0x00000008  /*
5c9a0 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 74 72   True if in a tr
5c9b0 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65  ansaction */.#de
5c9c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74 65  fine SQLITE_Inte
5c9d0 72 6e 43 68 61 6e 67 65 73 20 20 30 78 30 30 30  rnChanges  0x000
5c9e0 30 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d  00010  /* Uncomm
5c9f0 69 74 74 65 64 20 48 61 73 68 20 74 61 62 6c 65  itted Hash table
5ca00 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66   changes */.#def
5ca10 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43  ine SQLITE_FullC
5ca20 6f 6c 4e 61 6d 65 73 20 20 20 30 78 30 30 30 30  olNames   0x0000
5ca30 30 30 32 30 20 20 2f 2a 20 53 68 6f 77 20 66 75  0020  /* Show fu
5ca40 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ll column names 
5ca50 6f 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65  on SELECT */.#de
5ca60 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f 72  fine SQLITE_Shor
5ca70 74 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30 30  tColNames  0x000
5ca80 30 30 30 34 30 20 20 2f 2a 20 53 68 6f 77 20 73  00040  /* Show s
5ca90 68 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d  hort columns nam
5caa0 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  es */.#define SQ
5cab0 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20  LITE_CountRows  
5cac0 20 20 20 20 30 78 30 30 30 30 30 30 38 30 20 20      0x00000080  
5cad0 2f 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20 63 68  /* Count rows ch
5cae0 61 6e 67 65 64 20 62 79 20 49 4e 53 45 52 54 2c  anged by INSERT,
5caf0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
5cb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5cb20 20 20 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50     DELETE, or UP
5cb30 44 41 54 45 20 61 6e 64 20 72 65 74 75 72 6e 20  DATE and return 
5cb40 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
5cb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cb60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5cb70 20 20 74 68 65 20 63 6f 75 6e 74 20 75 73 69 6e    the count usin
5cb80 67 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f  g a callback. */
5cb90 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5cba0 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30  NullCallback   0
5cbb0 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 49 6e  x00000100  /* In
5cbc0 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63  voke the callbac
5cbd0 6b 20 6f 6e 63 65 20 69 66 20 74 68 65 20 2a 2f  k once if the */
5cbe0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
5cbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cc00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20             /*   
5cc10 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 6d  result set is em
5cc20 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  pty */.#define S
5cc30 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20 20  QLITE_SqlTrace  
5cc40 20 20 20 20 20 30 78 30 30 30 30 30 32 30 30 20       0x00000200 
5cc50 20 2f 2a 20 44 65 62 75 67 20 70 72 69 6e 74 20   /* Debug print 
5cc60 53 51 4c 20 61 73 20 69 74 20 65 78 65 63 75 74  SQL as it execut
5cc70 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  es */.#define SQ
5cc80 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67  LITE_VdbeListing
5cc90 20 20 20 20 30 78 30 30 30 30 30 34 30 30 20 20      0x00000400  
5cca0 2f 2a 20 44 65 62 75 67 20 6c 69 73 74 69 6e 67  /* Debug listing
5ccb0 73 20 6f 66 20 56 44 42 45 20 70 72 6f 67 72 61  s of VDBE progra
5ccc0 6d 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ms */.#define SQ
5ccd0 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61  LITE_WriteSchema
5cce0 20 20 20 20 30 78 30 30 30 30 30 38 30 30 20 20      0x00000800  
5ccf0 2f 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74 65 20  /* OK to update 
5cd00 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f  SQLITE_MASTER */
5cd10 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5cd20 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 20 30  NoReadlock     0
5cd30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 52 65  x00001000  /* Re
5cd40 61 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74  adlocks are omit
5cd50 74 65 64 20 77 68 65 6e 20 0a 20 20 20 20 20 20  ted when .      
5cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cd80 20 20 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67      ** accessing
5cd90 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62   read-only datab
5cda0 61 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ases */.#define 
5cdb0 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65  SQLITE_IgnoreChe
5cdc0 63 6b 73 20 20 20 30 78 30 30 30 30 32 30 30 30  cks   0x00002000
5cdd0 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f    /* Do not enfo
5cde0 72 63 65 20 63 68 65 63 6b 20 63 6f 6e 73 74 72  rce check constr
5cdf0 61 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65  aints */.#define
5ce00 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f   SQLITE_ReadUnco
5ce10 6d 6d 69 74 74 65 64 20 30 78 30 30 30 30 34 30  mmitted 0x000040
5ce20 30 30 20 2f 2a 20 46 6f 72 20 73 68 61 72 65 64  00 /* For shared
5ce30 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23  -cache mode */.#
5ce40 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65  define SQLITE_Le
5ce50 67 61 63 79 46 69 6c 65 46 6d 74 20 20 30 78 30  gacyFileFmt  0x0
5ce60 30 30 30 38 30 30 30 20 20 2f 2a 20 43 72 65 61  0008000  /* Crea
5ce70 74 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 73  te new databases
5ce80 20 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a   in format 1 */.
5ce90 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
5cea0 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 20 30 78  ullFSync      0x
5ceb0 30 30 30 31 30 30 30 30 20 20 2f 2a 20 55 73 65  00010000  /* Use
5cec0 20 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74   full fsync on t
5ced0 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64  he backend */.#d
5cee0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61  efine SQLITE_Loa
5cef0 64 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 30 30  dExtension  0x00
5cf00 30 32 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c  020000  /* Enabl
5cf10 65 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  e load_extension
5cf20 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c   */..#define SQL
5cf30 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65  ITE_RecoveryMode
5cf40 20 20 20 30 78 30 30 30 34 30 30 30 30 20 20 2f     0x00040000  /
5cf50 2a 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 20  * Ignore schema 
5cf60 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e  errors */.#defin
5cf70 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65  e SQLITE_Reverse
5cf80 4f 72 64 65 72 20 20 20 30 78 30 30 31 30 30 30  Order   0x001000
5cf90 30 30 20 20 2f 2a 20 52 65 76 65 72 73 65 20 75  00  /* Reverse u
5cfa0 6e 6f 72 64 65 72 65 64 20 53 45 4c 45 43 54 73  nordered SELECTs
5cfb0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5cfc0 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 20 20  TE_RecTriggers  
5cfd0 20 20 30 78 30 30 32 30 30 30 30 30 20 20 2f 2a    0x00200000  /*
5cfe0 20 45 6e 61 62 6c 65 20 72 65 63 75 72 73 69 76   Enable recursiv
5cff0 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 64  e triggers */.#d
5d000 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 6f 72  efine SQLITE_For
5d010 65 69 67 6e 4b 65 79 73 20 20 20 20 30 78 30 30  eignKeys    0x00
5d020 34 30 30 30 30 30 20 20 2f 2a 20 45 6e 66 6f 72  400000  /* Enfor
5d030 63 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  ce foreign key c
5d040 6f 6e 73 74 72 61 69 6e 74 73 20 20 2a 2f 0a 0a  onstraints  */..
5d050 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76  /*.** Possible v
5d060 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 71  alues for the sq
5d070 6c 69 74 65 2e 6d 61 67 69 63 20 66 69 65 6c 64  lite.magic field
5d080 2e 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 73  ..** The numbers
5d090 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 61 74   are obtained at
5d0a0 20 72 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76 65   random and have
5d0b0 20 6e 6f 20 73 70 65 63 69 61 6c 20 6d 65 61 6e   no special mean
5d0c0 69 6e 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 68  ing, other.** th
5d0d0 61 6e 20 62 65 69 6e 67 20 64 69 73 74 69 6e 63  an being distinc
5d0e0 74 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68  t from one anoth
5d0f0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  er..*/.#define S
5d100 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
5d110 20 20 20 20 20 30 78 61 30 32 39 61 36 39 37 20       0xa029a697 
5d120 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20   /* Database is 
5d130 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  open */.#define 
5d140 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f  SQLITE_MAGIC_CLO
5d150 53 45 44 20 20 20 30 78 39 66 33 63 32 64 33 33  SED   0x9f3c2d33
5d160 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73    /* Database is
5d170 20 63 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66 69   closed */.#defi
5d180 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  ne SQLITE_MAGIC_
5d190 53 49 43 4b 20 20 20 20 20 30 78 34 62 37 37 31  SICK     0x4b771
5d1a0 32 39 30 20 20 2f 2a 20 45 72 72 6f 72 20 61 6e  290  /* Error an
5d1b0 64 20 61 77 61 69 74 69 6e 67 20 63 6c 6f 73 65  d awaiting close
5d1c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5d1d0 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 20  TE_MAGIC_BUSY   
5d1e0 20 20 30 78 66 30 33 62 37 39 30 36 20 20 2f 2a    0xf03b7906  /*
5d1f0 20 44 61 74 61 62 61 73 65 20 63 75 72 72 65 6e   Database curren
5d200 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64  tly in use */.#d
5d210 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47  efine SQLITE_MAG
5d220 49 43 5f 45 52 52 4f 52 20 20 20 20 30 78 62 35  IC_ERROR    0xb5
5d230 33 35 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 51  357930  /* An SQ
5d240 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f  LITE_MISUSE erro
5d250 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f  r occurred */../
5d260 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 75  *.** Each SQL fu
5d270 6e 63 74 69 6f 6e 20 69 73 20 64 65 66 69 6e 65  nction is define
5d280 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  d by an instance
5d290 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
5d2a0 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20  g.** structure. 
5d2b0 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
5d2c0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
5d2d0 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71  stored in the sq
5d2e0 6c 69 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61  lite.aFunc.** ha
5d2f0 73 68 20 74 61 62 6c 65 2e 20 20 57 68 65 6e 20  sh table.  When 
5d300 6d 75 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 6f  multiple functio
5d310 6e 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65  ns have the same
5d320 20 6e 61 6d 65 2c 20 74 68 65 20 68 61 73 68 20   name, the hash 
5d330 74 61 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20  table.** points 
5d340 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  to a linked list
5d350 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74   of these struct
5d360 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ures..*/.struct 
5d370 46 75 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 20  FuncDef {.  i16 
5d380 6e 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  nArg;           
5d390 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
5d3a0 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61  guments.  -1 mea
5d3b0 6e 73 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a  ns unlimited */.
5d3c0 20 20 75 38 20 69 50 72 65 66 45 6e 63 3b 20 20    u8 iPrefEnc;  
5d3d0 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72         /* Prefer
5d3e0 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  red text encodin
5d3f0 67 20 28 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g (SQLITE_UTF8, 
5d400 31 36 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20  16LE, 16BE) */. 
5d410 20 75 38 20 66 6c 61 67 73 3b 20 20 20 20 20 20   u8 flags;      
5d420 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f        /* Some co
5d430 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 53 51 4c  mbination of SQL
5d440 49 54 45 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20  ITE_FUNC_* */.  
5d450 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74 61 3b  void *pUserData;
5d460 20 20 20 20 20 2f 2a 20 55 73 65 72 20 64 61 74       /* User dat
5d470 61 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20  a parameter */. 
5d480 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b   FuncDef *pNext;
5d490 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 75        /* Next fu
5d4a0 6e 63 74 69 6f 6e 20 77 69 74 68 20 73 61 6d 65  nction with same
5d4b0 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20   name */.  void 
5d4c0 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33  (*xFunc)(sqlite3
5d4d0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
5d4e0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20  lite3_value**); 
5d4f0 2f 2a 20 52 65 67 75 6c 61 72 20 66 75 6e 63 74  /* Regular funct
5d500 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ion */.  void (*
5d510 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63  xStep)(sqlite3_c
5d520 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
5d530 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a  te3_value**); /*
5d540 20 41 67 67 72 65 67 61 74 65 20 73 74 65 70 20   Aggregate step 
5d550 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e  */.  void (*xFin
5d560 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 63  alize)(sqlite3_c
5d570 6f 6e 74 65 78 74 2a 29 3b 20 20 20 20 20 20 20  ontext*);       
5d580 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72           /* Aggr
5d590 65 67 61 74 65 20 66 69 6e 61 6c 69 7a 65 72 20  egate finalizer 
5d5a0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  */.  char *zName
5d5b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ;         /* SQL
5d5c0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e   name of the fun
5d5d0 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e 63  ction. */.  Func
5d5e0 44 65 66 20 2a 70 48 61 73 68 3b 20 20 20 20 20  Def *pHash;     
5d5f0 20 2f 2a 20 4e 65 78 74 20 77 69 74 68 20 61 20   /* Next with a 
5d600 64 69 66 66 65 72 65 6e 74 20 6e 61 6d 65 20 62  different name b
5d610 75 74 20 74 68 65 20 73 61 6d 65 20 68 61 73 68  ut the same hash
5d620 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f   */.};../*.** Po
5d630 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f  ssible values fo
5d640 72 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 0a  r FuncDef.flags.
5d650 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5d660 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20 20  E_FUNC_LIKE     
5d670 30 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61 74  0x01 /* Candidat
5d680 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f  e for the LIKE o
5d690 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23  ptimization */.#
5d6a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
5d6b0 4e 43 5f 43 41 53 45 20 20 20 20 20 30 78 30 32  NC_CASE     0x02
5d6c0 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69   /* Case-sensiti
5d6d0 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e  ve LIKE-type fun
5d6e0 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ction */.#define
5d6f0 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48   SQLITE_FUNC_EPH
5d700 45 4d 20 20 20 20 30 78 30 34 20 2f 2a 20 45 70  EM    0x04 /* Ep
5d710 68 65 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65  hemeral.  Delete
5d720 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 23 64   with VDBE */.#d
5d730 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
5d740 43 5f 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38 20  C_NEEDCOLL 0x08 
5d750 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e  /* sqlite3GetFun
5d760 63 43 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 74  cCollSeq() might
5d770 20 62 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 23 64   be called */.#d
5d780 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
5d790 43 5f 50 52 49 56 41 54 45 20 20 30 78 31 30 20  C_PRIVATE  0x10 
5d7a0 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6f 72 20 69  /* Allowed for i
5d7b0 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79  nternal use only
5d7c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5d7d0 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 20 20 20  TE_FUNC_COUNT   
5d7e0 20 30 78 32 30 20 2f 2a 20 42 75 69 6c 74 2d 69   0x20 /* Built-i
5d7f0 6e 20 63 6f 75 6e 74 28 2a 29 20 61 67 67 72 65  n count(*) aggre
5d800 67 61 74 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  gate */../*.** T
5d810 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72  he following thr
5d820 65 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e 43 54  ee macros, FUNCT
5d830 49 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e 43 28  ION(), LIKEFUNC(
5d840 29 20 61 6e 64 20 41 47 47 52 45 47 41 54 45 28  ) and AGGREGATE(
5d850 29 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f  ) are.** used to
5d860 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 69 74   create the init
5d870 69 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 68 65  ializers for the
5d880 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
5d890 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55 4e  res..**.**   FUN
5d8a0 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72  CTION(zName, nAr
5d8b0 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46  g, iArg, bNC, xF
5d8c0 75 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 65 64  unc).**     Used
5d8d0 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 61   to create a sca
5d8e0 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  lar function def
5d8f0 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e  inition of a fun
5d900 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20  ction zName .** 
5d910 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20      implemented 
5d920 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78 46  by C function xF
5d930 75 6e 63 20 74 68 61 74 20 61 63 63 65 70 74 73  unc that accepts
5d940 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 2e   nArg arguments.
5d950 20 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 6c 75   The.**     valu
5d960 65 20 70 61 73 73 65 64 20 61 73 20 69 41 72 67  e passed as iArg
5d970 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76   is cast to a (v
5d980 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20 61  oid*) and made a
5d990 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 20 20  vailable.**     
5d9a0 61 73 20 74 68 65 20 75 73 65 72 2d 64 61 74 61  as the user-data
5d9b0 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64   (sqlite3_user_d
5d9c0 61 74 61 28 29 29 20 66 6f 72 20 74 68 65 20 66  ata()) for the f
5d9d0 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a 20  unction. If .** 
5d9e0 20 20 20 20 61 72 67 75 6d 65 6e 74 20 62 4e 43      argument bNC
5d9f0 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
5da00 68 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e  he SQLITE_FUNC_N
5da10 45 45 44 43 4f 4c 4c 20 66 6c 61 67 20 69 73 20  EEDCOLL flag is 
5da20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47 47  set..**.**   AGG
5da30 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41  REGATE(zName, nA
5da40 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78  rg, iArg, bNC, x
5da50 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a 2a 2a  Step, xFinal).**
5da60 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65       Used to cre
5da70 61 74 65 20 61 6e 20 61 67 67 72 65 67 61 74 65  ate an aggregate
5da80 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
5da90 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64  tion implemented
5daa0 20 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 20 43   by.**     the C
5dab0 20 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 65 70   functions xStep
5dac0 20 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 68 65   and xFinal. The
5dad0 20 66 69 72 73 74 20 66 6f 75 72 20 70 61 72 61   first four para
5dae0 6d 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 72  meters.**     ar
5daf0 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 69 6e  e interpreted in
5db00 20 74 68 65 20 73 61 6d 65 20 77 61 79 20 61 73   the same way as
5db10 20 74 68 65 20 66 69 72 73 74 20 34 20 70 61 72   the first 4 par
5db20 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 20 20  ameters to.**   
5db30 20 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a 2a    FUNCTION()..**
5db40 0a 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 28 7a  .**   LIKEFUNC(z
5db50 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67  Name, nArg, pArg
5db60 2c 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 20 20  , flags).**     
5db70 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  Used to create a
5db80 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
5db90 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61   definition of a
5dba0 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20   function zName 
5dbb0 0a 2a 2a 20 20 20 20 20 74 68 61 74 20 61 63 63  .**     that acc
5dbc0 65 70 74 73 20 6e 41 72 67 20 61 72 67 75 6d 65  epts nArg argume
5dbd0 6e 74 73 20 61 6e 64 20 69 73 20 69 6d 70 6c 65  nts and is imple
5dbe0 6d 65 6e 74 65 64 20 62 79 20 61 20 63 61 6c 6c  mented by a call
5dbf0 20 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 66 75   to C .**     fu
5dc00 6e 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e  nction likeFunc.
5dc10 20 41 72 67 75 6d 65 6e 74 20 70 41 72 67 20 69   Argument pArg i
5dc20 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69  s cast to a (voi
5dc30 64 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a 2a 2a  d *) and made.**
5dc40 20 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20 61       available a
5dc50 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75  s the function u
5dc60 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65  ser-data (sqlite
5dc70 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e 20  3_user_data()). 
5dc80 54 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e 63 44  The.**     FuncD
5dc90 65 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62 6c  ef.flags variabl
5dca0 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  e is set to the 
5dcb0 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20  value passed as 
5dcc0 74 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 20 20  the flags.**    
5dcd0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 23   parameter..*/.#
5dce0 64 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e 28  define FUNCTION(
5dcf0 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72  zName, nArg, iAr
5dd00 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c  g, bNC, xFunc) \
5dd10 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45  .  {nArg, SQLITE
5dd20 5f 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54  _UTF8, bNC*SQLIT
5dd30 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c  E_FUNC_NEEDCOLL,
5dd40 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e 54   \.   SQLITE_INT
5dd50 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 2c 20 30  _TO_PTR(iArg), 0
5dd60 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23  , xFunc, 0, 0, #
5dd70 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e  zName, 0}.#defin
5dd80 65 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a  e STR_FUNCTION(z
5dd90 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67  Name, nArg, pArg
5dda0 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a  , bNC, xFunc) \.
5ddb0 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f    {nArg, SQLITE_
5ddc0 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45  UTF8, bNC*SQLITE
5ddd0 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20  _FUNC_NEEDCOLL, 
5dde0 5c 0a 20 20 20 70 41 72 67 2c 20 30 2c 20 78 46  \.   pArg, 0, xF
5ddf0 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d  unc, 0, 0, #zNam
5de00 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 4c 49  e, 0}.#define LI
5de10 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41  KEFUNC(zName, nA
5de20 72 67 2c 20 61 72 67 2c 20 66 6c 61 67 73 29 20  rg, arg, flags) 
5de30 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54  \.  {nArg, SQLIT
5de40 45 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c 20 28  E_UTF8, flags, (
5de50 76 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c 20 6c  void *)arg, 0, l
5de60 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23  ikeFunc, 0, 0, #
5de70 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e  zName, 0}.#defin
5de80 65 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 6d  e AGGREGATE(zNam
5de90 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 6e 63  e, nArg, arg, nc
5dea0 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29  , xStep, xFinal)
5deb0 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49   \.  {nArg, SQLI
5dec0 54 45 5f 55 54 46 38 2c 20 6e 63 2a 53 51 4c 49  TE_UTF8, nc*SQLI
5ded0 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c  TE_FUNC_NEEDCOLL
5dee0 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e  , \.   SQLITE_IN
5def0 54 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 30  T_TO_PTR(arg), 0
5df00 2c 20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 61  , 0, xStep,xFina
5df10 6c 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a  l,#zName,0}../*.
5df20 2a 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 20 73  ** All current s
5df30 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 74  avepoints are st
5df40 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64  ored in a linked
5df50 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61   list starting a
5df60 74 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53 61  t.** sqlite3.pSa
5df70 76 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 69 72  vepoint. The fir
5df80 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68  st element in th
5df90 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 6d 6f  e list is the mo
5dfa0 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f  st recently.** o
5dfb0 70 65 6e 65 64 20 73 61 76 65 70 6f 69 6e 74 2e  pened savepoint.
5dfc0 20 53 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20   Savepoints are 
5dfd0 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c 69 73  added to the lis
5dfe0 74 20 62 79 20 74 68 65 20 76 64 62 65 0a 2a 2a  t by the vdbe.**
5dff0 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e   OP_Savepoint in
5e000 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  struction..*/.st
5e010 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 7b  ruct Savepoint {
5e020 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
5e030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e040 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70 6f         /* Savepo
5e050 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 65  int name (nul-te
5e060 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20 69  rminated) */.  i
5e070 36 34 20 6e 44 65 66 65 72 72 65 64 43 6f 6e 73  64 nDeferredCons
5e080 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5e090 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5e0a0 64 65 66 65 72 72 65 64 20 66 6b 20 76 69 6f 6c  deferred fk viol
5e0b0 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 53 61 76 65  ations */.  Save
5e0c0 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 20 20  point *pNext;   
5e0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e0e0 2f 2a 20 50 61 72 65 6e 74 20 73 61 76 65 70 6f  /* Parent savepo
5e0f0 69 6e 74 20 28 69 66 20 61 6e 79 29 20 2a 2f 0a  int (if any) */.
5e100 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  };../*.** The fo
5e110 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 65 64  llowing are used
5e120 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70   as the second p
5e130 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
5e140 74 65 33 53 61 76 65 70 6f 69 6e 74 28 29 2c 0a  te3Savepoint(),.
5e150 2a 2a 20 61 6e 64 20 61 73 20 74 68 65 20 50 31  ** and as the P1
5e160 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
5e170 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e   OP_Savepoint in
5e180 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 64  struction..*/.#d
5e190 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f  efine SAVEPOINT_
5e1a0 42 45 47 49 4e 20 20 20 20 20 20 30 0a 23 64 65  BEGIN      0.#de
5e1b0 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52  fine SAVEPOINT_R
5e1c0 45 4c 45 41 53 45 20 20 20 20 31 0a 23 64 65 66  ELEASE    1.#def
5e1d0 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f  ine SAVEPOINT_RO
5e1e0 4c 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a 0a  LLBACK   2.../*.
5e1f0 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d  ** Each SQLite m
5e200 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74  odule (virtual t
5e210 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29  able definition)
5e220 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61   is defined by a
5e230 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66  n.** instance of
5e240 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
5e250 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65 64  tructure, stored
5e260 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e   in the sqlite3.
5e270 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20  aModule.** hash 
5e280 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  table..*/.struct
5e290 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73   Module {.  cons
5e2a0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
5e2b0 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20   *pModule;      
5e2c0 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69   /* Callback poi
5e2d0 6e 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  nters */.  const
5e2e0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
5e2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e300 2f 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 20 74  /* Name passed t
5e310 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28  o create_module(
5e320 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75  ) */.  void *pAu
5e330 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
5e340 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
5e350 41 75 78 20 70 61 73 73 65 64 20 74 6f 20 63 72  Aux passed to cr
5e360 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f  eate_module() */
5e370 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72  .  void (*xDestr
5e380 6f 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20  oy)(void *);    
5e390 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c          /* Modul
5e3a0 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
5e3b0 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ction */.};../*.
5e3c0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ** information a
5e3d0 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e  bout each column
5e3e0 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c 65   of an SQL table
5e3f0 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 69   is held in an i
5e400 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68  nstance.** of th
5e410 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  is structure..*/
5e420 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b  .struct Column {
5e430 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
5e440 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
5e450 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  his column */.  
5e460 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20  Expr *pDflt;    
5e470 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75   /* Default valu
5e480 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e  e of this column
5e490 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 66 6c   */.  char *zDfl
5e4a0 74 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e  t;     /* Origin
5e4b0 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 64  al text of the d
5e4c0 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f 0a  efault value */.
5e4d0 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20    char *zType;  
5e4e0 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20     /* Data type 
5e4f0 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20  for this column 
5e500 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  */.  char *zColl
5e510 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69  ;     /* Collati
5e520 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20 49 66  ng sequence.  If
5e530 20 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65 20 64   NULL, use the d
5e540 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6e  efault */.  u8 n
5e550 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f 2a 20  otNull;      /* 
5e560 54 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73  True if there is
5e570 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73   a NOT NULL cons
5e580 74 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38 20 69  traint */.  u8 i
5e590 73 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f 2a 20  sPrimKey;    /* 
5e5a0 54 72 75 65 20 69 66 20 74 68 69 73 20 63 6f 6c  True if this col
5e5b0 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74  umn is part of t
5e5c0 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a  he PRIMARY KEY *
5e5d0 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74  /.  char affinit
5e5e0 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74  y;   /* One of t
5e5f0 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 2e 2e  he SQLITE_AFF_..
5e600 2e 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69 66 6e  . values */.#ifn
5e610 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
5e620 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 75  VIRTUALTABLE.  u
5e630 38 20 69 73 48 69 64 64 65 6e 3b 20 20 20 20 20  8 isHidden;     
5e640 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
5e650 63 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64 64 65  column is 'hidde
5e660 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  n' */.#endif.};.
5e670 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61 74  ./*.** A "Collat
5e680 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20 69 73  ing Sequence" is
5e690 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69   defined by an i
5e6a0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
5e6b0 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75  ollowing.** stru
5e6c0 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 75 61  cture. Conceptua
5e6d0 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 6e 67  lly, a collating
5e6e0 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 69 73   sequence consis
5e6f0 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 6e 64  ts of a name and
5e700 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e  .** a comparison
5e710 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 65   routine that de
5e720 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65 72 20  fines the order 
5e730 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e 63 65  of that sequence
5e740 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61  ..**.** There ma
5e750 79 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 69  y two separate i
5e760 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
5e770 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  f the collation 
5e780 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a  function, one.**
5e790 20 74 68 61 74 20 70 72 6f 63 65 73 73 65 73 20   that processes 
5e7a0 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20 65 6e  text in UTF-8 en
5e7b0 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 71 2e  coding (CollSeq.
5e7c0 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 68 65  xCmp) and anothe
5e7d0 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 65 73  r that.** proces
5e7e0 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64 65 64  ses text encoded
5e7f0 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f 6c 6c   in UTF-16 (Coll
5e800 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 73 69  Seq.xCmp16), usi
5e810 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65 0a 2a  ng the machine.*
5e820 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  * native byte or
5e830 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f 6c 6c  der. When a coll
5e840 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69  ation sequence i
5e850 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c 69 74  s invoked, SQLit
5e860 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 68 65  e selects.** the
5e870 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 77 69   version that wi
5e880 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65 20 6c  ll require the l
5e890 65 61 73 74 20 65 78 70 65 6e 73 69 76 65 20 65  east expensive e
5e8a0 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 6e 73  ncoding.** trans
5e8b0 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e 79 2e  lations, if any.
5e8c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c 6c 53  .**.** The CollS
5e8d0 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 72 20  eq.pUser member 
5e8e0 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e 20 65  variable is an e
5e8f0 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 74  xtra parameter t
5e900 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a 2a 2a  hat passed in.**
5e910 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
5e920 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 55 54  gument to the UT
5e930 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66  F-8 comparison f
5e940 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e 0a 2a  unction, xCmp..*
5e950 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 31  * CollSeq.pUser1
5e960 36 20 69 73 20 74 68 65 20 65 71 75 69 76 61 6c  6 is the equival
5e970 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54 46 2d  ent for the UTF-
5e980 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75  16 comparison fu
5e990 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 31  nction,.** xCmp1
5e9a0 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74 68  6..**.** If both
5e9b0 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 61 6e   CollSeq.xCmp an
5e9c0 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36  d CollSeq.xCmp16
5e9d0 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 6d 65   are NULL, it me
5e9e0 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ans that the.** 
5e9f0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
5ea00 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  ce is undefined.
5ea10 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c 74 20    Indices built 
5ea20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a  on an undefined.
5ea30 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  ** collating seq
5ea40 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 62 65  uence may not be
5ea50 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e   read or written
5ea60 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 6c  ..*/.struct Coll
5ea70 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  Seq {.  char *zN
5ea80 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ame;          /*
5ea90 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c   Name of the col
5eaa0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2c  lating sequence,
5eab0 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a   UTF-8 encoded *
5eac0 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20  /.  u8 enc;     
5ead0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
5eae0 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e 64 6c  t encoding handl
5eaf0 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a 2f 0a  ed by xCmp() */.
5eb00 20 20 75 38 20 74 79 70 65 3b 20 20 20 20 20 20    u8 type;      
5eb10 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
5eb20 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4c  f the SQLITE_COL
5eb30 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62 65 6c  L_... values bel
5eb40 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55  ow */.  void *pU
5eb50 73 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ser;          /*
5eb60 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   First argument 
5eb70 74 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 69  to xCmp() */.  i
5eb80 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69 64 2a  nt (*xCmp)(void*
5eb90 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ,int, const void
5eba0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f  *, int, const vo
5ebb0 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  id*);.  void (*x
5ebc0 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a  Del)(void*);  /*
5ebd0 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20   Destructor for 
5ebe0 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  pUser */.};../*.
5ebf0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
5ec00 73 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74 79 70  s of CollSeq.typ
5ec10 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e:.*/.#define SQ
5ec20 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59  LITE_COLL_BINARY
5ec30 20 20 31 20 20 2f 2a 20 54 68 65 20 64 65 66 61    1  /* The defa
5ec40 75 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63 6f 6c  ult memcmp() col
5ec50 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
5ec60 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5ec70 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 20 32  E_COLL_NOCASE  2
5ec80 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69    /* The built-i
5ec90 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 74 69  n NOCASE collati
5eca0 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23  ng sequence */.#
5ecb0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
5ecc0 4c 4c 5f 52 45 56 45 52 53 45 20 33 20 20 2f 2a  LL_REVERSE 3  /*
5ecd0 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 52 45   The built-in RE
5ece0 56 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20  VERSE collating 
5ecf0 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66  sequence */.#def
5ed00 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  ine SQLITE_COLL_
5ed10 55 53 45 52 20 20 20 20 30 20 20 2f 2a 20 41 6e  USER    0  /* An
5ed20 79 20 6f 74 68 65 72 20 75 73 65 72 2d 64 65 66  y other user-def
5ed30 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73  ined collating s
5ed40 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a  equence */../*.*
5ed50 2a 20 41 20 73 6f 72 74 20 6f 72 64 65 72 20 63  * A sort order c
5ed60 61 6e 20 62 65 20 65 69 74 68 65 72 20 41 53 43  an be either ASC
5ed70 20 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23 64 65   or DESC..*/.#de
5ed80 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 41  fine SQLITE_SO_A
5ed90 53 43 20 20 20 20 20 20 20 30 20 20 2f 2a 20 53  SC       0  /* S
5eda0 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67  ort in ascending
5edb0 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e   order */.#defin
5edc0 65 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43  e SQLITE_SO_DESC
5edd0 20 20 20 20 20 20 31 20 20 2f 2a 20 53 6f 72 74        1  /* Sort
5ede0 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72   in ascending or
5edf0 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f  der */../*.** Co
5ee00 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 79  lumn affinity ty
5ee10 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  pes..**.** These
5ee20 20 75 73 65 64 20 74 6f 20 68 61 76 65 20 6d 6e   used to have mn
5ee30 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69 6b 65  emonic name like
5ee40 20 27 69 27 20 66 6f 72 20 53 51 4c 49 54 45 5f   'i' for SQLITE_
5ee50 41 46 46 5f 49 4e 54 45 47 45 52 20 61 6e 64 0a  AFF_INTEGER and.
5ee60 2a 2a 20 27 74 27 20 66 6f 72 20 53 51 4c 49 54  ** 't' for SQLIT
5ee70 45 5f 41 46 46 5f 54 45 58 54 2e 20 20 42 75 74  E_AFF_TEXT.  But
5ee80 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 20 6c   we can save a l
5ee90 69 74 74 6c 65 20 73 70 61 63 65 20 61 6e 64 20  ittle space and 
5eea0 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65 20 73  improve.** the s
5eeb0 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20 62 79  peed a little by
5eec0 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65 20 76   numbering the v
5eed0 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74 69 76  alues consecutiv
5eee0 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 75 74  ely.  .**.** But
5eef0 20 72 61 74 68 65 72 20 74 68 61 6e 20 73 74 61   rather than sta
5ef00 72 74 20 77 69 74 68 20 30 20 6f 72 20 31 2c 20  rt with 0 or 1, 
5ef10 77 65 20 62 65 67 69 6e 20 77 69 74 68 20 27 61  we begin with 'a
5ef20 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a  '.  That way,.**
5ef30 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 61   when multiple a
5ef40 66 66 69 6e 69 74 79 20 74 79 70 65 73 20 61 72  ffinity types ar
5ef50 65 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 69  e concatenated i
5ef60 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 61 6e 64  nto a string and
5ef70 0a 2a 2a 20 75 73 65 64 20 61 73 20 74 68 65 20  .** used as the 
5ef80 50 34 20 6f 70 65 72 61 6e 64 2c 20 74 68 65 79  P4 operand, they
5ef90 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 72 65   will be more re
5efa0 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  adable..**.** No
5efb0 74 65 20 61 6c 73 6f 20 74 68 61 74 20 74 68 65  te also that the
5efc0 20 6e 75 6d 65 72 69 63 20 74 79 70 65 73 20 61   numeric types a
5efd0 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 65 74  re grouped toget
5efe0 68 65 72 20 73 6f 20 74 68 61 74 20 74 65 73 74  her so that test
5eff0 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e 75 6d  ing.** for a num
5f000 65 72 69 63 20 74 79 70 65 20 69 73 20 61 20 73  eric type is a s
5f010 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e  ingle comparison
5f020 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
5f030 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20  ITE_AFF_TEXT    
5f040 20 27 61 27 0a 23 64 65 66 69 6e 65 20 53 51 4c   'a'.#define SQL
5f050 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20  ITE_AFF_NONE    
5f060 20 27 62 27 0a 23 64 65 66 69 6e 65 20 53 51 4c   'b'.#define SQL
5f070 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20  ITE_AFF_NUMERIC 
5f080 20 27 63 27 0a 23 64 65 66 69 6e 65 20 53 51 4c   'c'.#define SQL
5f090 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20  ITE_AFF_INTEGER 
5f0a0 20 27 64 27 0a 23 64 65 66 69 6e 65 20 53 51 4c   'd'.#define SQL
5f0b0 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 20  ITE_AFF_REAL    
5f0c0 20 27 65 27 0a 0a 23 64 65 66 69 6e 65 20 73 71   'e'..#define sq
5f0d0 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66  lite3IsNumericAf
5f0e0 66 69 6e 69 74 79 28 58 29 20 20 28 28 58 29 3e  finity(X)  ((X)>
5f0f0 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
5f100 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  RIC)../*.** The 
5f110 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20  SQLITE_AFF_MASK 
5f120 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f 66 66  values masks off
5f130 20 74 68 65 20 73 69 67 6e 69 66 69 63 61 6e 74   the significant
5f140 20 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a 20 61   bits of an.** a
5f150 66 66 69 6e 69 74 79 20 76 61 6c 75 65 2e 20 0a  ffinity value. .
5f160 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5f170 45 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20 20 30  E_AFF_MASK     0
5f180 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 69 74  x67../*.** Addit
5f190 69 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75 65 73  ional bit values
5f1a0 20 74 68 61 74 20 63 61 6e 20 62 65 20 4f 52 65   that can be ORe
5f1b0 64 20 77 69 74 68 20 61 6e 20 61 66 66 69 6e 69  d with an affini
5f1c0 74 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20 63 68  ty without.** ch
5f1d0 61 6e 67 69 6e 67 20 74 68 65 20 61 66 66 69 6e  anging the affin
5f1e0 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ity..*/.#define 
5f1f0 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
5f200 4c 20 20 20 30 78 30 38 20 20 2f 2a 20 6a 75 6d  L   0x08  /* jum
5f210 70 73 20 69 66 20 65 69 74 68 65 72 20 6f 70 65  ps if either ope
5f220 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a  rand is NULL */.
5f230 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
5f240 54 4f 52 45 50 32 20 20 20 20 20 20 30 78 31 30  TOREP2      0x10
5f250 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c    /* Store resul
5f260 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72 61 74  t in reg[P2] rat
5f270 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20 2a 2f  her than jump */
5f280 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5f290 4e 55 4c 4c 45 51 20 20 20 20 20 20 20 30 78 38  NULLEQ       0x8
5f2a0 30 20 20 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c 20  0  /* NULL=NULL 
5f2b0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a  */../*.** An obj
5f2c0 65 63 74 20 6f 66 20 74 68 69 73 20 74 79 70 65  ect of this type
5f2d0 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20   is created for 
5f2e0 65 61 63 68 20 76 69 72 74 75 61 6c 20 74 61 62  each virtual tab
5f2f0 6c 65 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a  le present in.**
5f300 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
5f310 68 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  hema. .**.** If 
5f320 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
5f330 65 6d 61 20 69 73 20 73 68 61 72 65 64 2c 20 74  ema is shared, t
5f340 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 65  hen there is one
5f350 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
5f360 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 66  s.** structure f
5f370 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65  or each database
5f380 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 73 71 6c   connection (sql
5f390 69 74 65 33 2a 29 20 74 68 61 74 20 75 73 65 73  ite3*) that uses
5f3a0 20 74 68 65 20 73 68 61 72 65 64 0a 2a 2a 20 73   the shared.** s
5f3b0 63 68 65 6d 61 2e 20 54 68 69 73 20 69 73 20 62  chema. This is b
5f3c0 65 63 61 75 73 65 20 65 61 63 68 20 64 61 74 61  ecause each data
5f3d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
5f3e0 72 65 71 75 69 72 65 73 20 69 74 73 20 6f 77 6e  requires its own
5f3f0 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 73 74 61   unique.** insta
5f400 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74  nce of the sqlit
5f410 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 20  e3_vtab* handle 
5f420 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74  used to access t
5f430 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
5f440 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74   .** implementat
5f450 69 6f 6e 2e 20 73 71 6c 69 74 65 33 5f 76 74 61  ion. sqlite3_vta
5f460 62 2a 20 68 61 6e 64 6c 65 73 20 63 61 6e 20 6e  b* handles can n
5f470 6f 74 20 62 65 20 73 68 61 72 65 64 20 62 65 74  ot be shared bet
5f480 77 65 65 6e 20 0a 2a 2a 20 64 61 74 61 62 61 73  ween .** databas
5f490 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 65  e connections, e
5f4a0 76 65 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73  ven when the res
5f4b0 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  t of the in-memo
5f4c0 72 79 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20  ry database .** 
5f4d0 73 63 68 65 6d 61 20 69 73 20 73 68 61 72 65 64  schema is shared
5f4e0 2c 20 61 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  , as the impleme
5f4f0 6e 74 61 74 69 6f 6e 20 6f 66 74 65 6e 20 73 74  ntation often st
5f500 6f 72 65 73 20 74 68 65 20 64 61 74 61 62 61 73  ores the databas
5f510 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  e.** connection 
5f520 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74 6f  handle passed to
5f530 20 69 74 20 76 69 61 20 74 68 65 20 78 43 6f 6e   it via the xCon
5f540 6e 65 63 74 28 29 20 6f 72 20 78 43 72 65 61 74  nect() or xCreat
5f550 65 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 64 75  e() method.** du
5f560 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
5f570 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20  ion internally. 
5f580 54 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f  This database co
5f590 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20  nnection handle 
5f5a0 6d 61 79 0a 2a 2a 20 74 68 65 6e 20 75 73 65 64  may.** then used
5f5b0 20 62 79 20 74 68 65 20 76 69 72 74 75 61 6c 20   by the virtual 
5f5c0 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
5f5d0 74 69 6f 6e 20 74 6f 20 61 63 63 65 73 73 20 72  tion to access r
5f5e0 65 61 6c 20 74 61 62 6c 65 73 20 0a 2a 2a 20 77  eal tables .** w
5f5f0 69 74 68 69 6e 20 74 68 65 20 64 61 74 61 62 61  ithin the databa
5f600 73 65 2e 20 53 6f 20 74 68 61 74 20 74 68 65 79  se. So that they
5f610 20 61 70 70 65 61 72 20 61 73 20 70 61 72 74 20   appear as part 
5f620 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 73 20 0a  of the callers .
5f630 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  ** transaction, 
5f640 74 68 65 73 65 20 61 63 63 65 73 73 65 73 20 6e  these accesses n
5f650 65 65 64 20 74 6f 20 62 65 20 6d 61 64 65 20 76  eed to be made v
5f660 69 61 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  ia the same data
5f670 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74  base .** connect
5f680 69 6f 6e 20 61 73 20 74 68 61 74 20 75 73 65 64  ion as that used
5f690 20 74 6f 20 65 78 65 63 75 74 65 20 53 51 4c 20   to execute SQL 
5f6a0 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68  operations on th
5f6b0 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
5f6c0 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 56 54 61 62 6c  .**.** All VTabl
5f6d0 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 63  e objects that c
5f6e0 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 61 20 73  orrespond to a s
5f6f0 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 61  ingle table in a
5f700 20 73 68 61 72 65 64 0a 2a 2a 20 64 61 74 61 62   shared.** datab
5f710 61 73 65 20 73 63 68 65 6d 61 20 61 72 65 20 69  ase schema are i
5f720 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 64 20  nitially stored 
5f730 69 6e 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74  in a linked-list
5f740 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a   pointed to by.*
5f750 2a 20 74 68 65 20 54 61 62 6c 65 2e 70 56 54 61  * the Table.pVTa
5f760 62 6c 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61  ble member varia
5f770 62 6c 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65  ble of the corre
5f780 73 70 6f 6e 64 69 6e 67 20 54 61 62 6c 65 20 6f  sponding Table o
5f790 62 6a 65 63 74 2e 0a 2a 2a 20 57 68 65 6e 20 61  bject..** When a
5f7a0 6e 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  n sqlite3_prepar
5f7b0 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  e() operation is
5f7c0 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 63 63   required to acc
5f7d0 65 73 73 20 74 68 65 20 76 69 72 74 75 61 6c 0a  ess the virtual.
5f7e0 2a 2a 20 74 61 62 6c 65 2c 20 69 74 20 73 65 61  ** table, it sea
5f7f0 72 63 68 65 73 20 74 68 65 20 6c 69 73 74 20 66  rches the list f
5f800 6f 72 20 74 68 65 20 56 54 61 62 6c 65 20 74 68  or the VTable th
5f810 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  at corresponds t
5f820 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  o the.** databas
5f830 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 6f 69  e connection doi
5f840 6e 67 20 74 68 65 20 70 72 65 70 61 72 69 6e 67  ng the preparing
5f850 20 73 6f 20 61 73 20 74 6f 20 75 73 65 20 74 68   so as to use th
5f860 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 73 71 6c  e correct.** sql
5f870 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c  ite3_vtab* handl
5f880 65 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  e in the compile
5f890 64 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 57  d query..**.** W
5f8a0 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  hen an in-memory
5f8b0 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 69 73   Table object is
5f8c0 20 64 65 6c 65 74 65 64 20 28 66 6f 72 20 65 78   deleted (for ex
5f8d0 61 6d 70 6c 65 20 77 68 65 6e 20 74 68 65 0a 2a  ample when the.*
5f8e0 2a 20 73 63 68 65 6d 61 20 69 73 20 62 65 69 6e  * schema is bein
5f8f0 67 20 72 65 6c 6f 61 64 65 64 20 66 6f 72 20 73  g reloaded for s
5f900 6f 6d 65 20 72 65 61 73 6f 6e 29 2c 20 74 68 65  ome reason), the
5f910 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 20   VTable objects 
5f920 61 72 65 20 6e 6f 74 20 0a 2a 2a 20 64 65 6c 65  are not .** dele
5f930 74 65 64 20 61 6e 64 20 74 68 65 20 73 71 6c 69  ted and the sqli
5f940 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65  te3_vtab* handle
5f950 73 20 61 72 65 20 6e 6f 74 20 78 44 69 73 63 6f  s are not xDisco
5f960 6e 6e 65 63 74 28 29 65 64 20 0a 2a 2a 20 69 6d  nnect()ed .** im
5f970 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 73 74 65  mediately. Inste
5f980 61 64 2c 20 74 68 65 79 20 61 72 65 20 6d 6f 76  ad, they are mov
5f990 65 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c  ed from the Tabl
5f9a0 65 2e 70 56 54 61 62 6c 65 20 6c 69 73 74 20 74  e.pVTable list t
5f9b0 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 6c 69 6e  o.** another lin
5f9c0 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20  ked list headed 
5f9d0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70  by the sqlite3.p
5f9e0 44 69 73 63 6f 6e 6e 65 63 74 20 6d 65 6d 62 65  Disconnect membe
5f9f0 72 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 72 72  r of the.** corr
5fa00 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65  esponding sqlite
5fa10 33 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65  3 structure. The
5fa20 79 20 61 72 65 20 74 68 65 6e 20 64 65 6c 65 74  y are then delet
5fa30 65 64 2f 78 44 69 73 63 6f 6e 6e 65 63 74 65 64  ed/xDisconnected
5fa40 20 0a 2a 2a 20 6e 65 78 74 20 74 69 6d 65 20 61   .** next time a
5fa50 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 72   statement is pr
5fa60 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 61 69  epared using sai
5fa70 64 20 73 71 6c 69 74 65 33 2a 2e 20 54 68 69 73  d sqlite3*. This
5fa80 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 61   is done.** to a
5fa90 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 20 69 73  void deadlock is
5faa0 73 75 65 73 20 69 6e 76 6f 6c 76 69 6e 67 20 6d  sues involving m
5fab0 75 6c 74 69 70 6c 65 20 73 71 6c 69 74 65 33 2e  ultiple sqlite3.
5fac0 6d 75 74 65 78 20 6d 75 74 65 78 65 73 2e 0a 2a  mutex mutexes..*
5fad0 2a 20 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65  * Refer to comme
5fae0 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69  nts above functi
5faf0 6f 6e 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e  on sqlite3VtabUn
5fb00 6c 6f 63 6b 4c 69 73 74 28 29 20 66 6f 72 20 61  lockList() for a
5fb10 6e 0a 2a 2a 20 65 78 70 6c 61 6e 61 74 69 6f 6e  n.** explanation
5fb20 20 61 73 20 74 6f 20 77 68 79 20 69 74 20 69 73   as to why it is
5fb30 20 73 61 66 65 20 74 6f 20 61 64 64 20 61 6e 20   safe to add an 
5fb40 65 6e 74 72 79 20 74 6f 20 61 6e 20 73 71 6c 69  entry to an sqli
5fb50 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 0a  te3.pDisconnect.
5fb60 2a 2a 20 6c 69 73 74 20 77 69 74 68 6f 75 74 20  ** list without 
5fb70 68 6f 6c 64 69 6e 67 20 74 68 65 20 63 6f 72 72  holding the corr
5fb80 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65  esponding sqlite
5fb90 33 2e 6d 75 74 65 78 20 6d 75 74 65 78 2e 0a 2a  3.mutex mutex..*
5fba0 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20  *.** The memory 
5fbb0 66 6f 72 20 6f 62 6a 65 63 74 73 20 6f 66 20 74  for objects of t
5fbc0 68 69 73 20 74 79 70 65 20 69 73 20 61 6c 77 61  his type is alwa
5fbd0 79 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  ys allocated by 
5fbe0 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  .** sqlite3DbMal
5fbf0 6c 6f 63 28 29 2c 20 75 73 69 6e 67 20 74 68 65  loc(), using the
5fc00 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64   connection hand
5fc10 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 56 54 61  le stored in VTa
5fc20 62 6c 65 2e 64 62 20 61 73 20 0a 2a 2a 20 74 68  ble.db as .** th
5fc30 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
5fc40 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 54 61 62  ..*/.struct VTab
5fc50 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  le {.  sqlite3 *
5fc60 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
5fc70 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
5fc80 6e 65 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74  nection associat
5fc90 65 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62  ed with this tab
5fca0 6c 65 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a  le */.  Module *
5fcb0 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 20 20 20  pMod;           
5fcc0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
5fcd0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  module implement
5fce0 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  ation */.  sqlit
5fcf0 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20  e3_vtab *pVtab; 
5fd00 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
5fd10 74 6f 20 76 74 61 62 20 69 6e 73 74 61 6e 63 65  to vtab instance
5fd20 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
5fd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5fd40 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69  /* Number of poi
5fd50 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74  nters to this st
5fd60 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 56 54 61  ructure */.  VTa
5fd70 62 6c 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  ble *pNext;     
5fd80 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69         /* Next i
5fd90 6e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 73  n linked list (s
5fda0 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 7d 3b 0a  ee above) */.};.
5fdb0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20  ./*.** Each SQL 
5fdc0 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 73 65  table is represe
5fdd0 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62  nted in memory b
5fde0 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
5fdf0 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e   the.** followin
5fe00 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  g structure..**.
5fe10 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65 20 69  ** Table.zName i
5fe20 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
5fe30 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 63 61  e table.  The ca
5fe40 73 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e  se of the origin
5fe50 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42  al.** CREATE TAB
5fe60 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  LE statement is 
5fe70 73 74 6f 72 65 64 2c 20 62 75 74 20 63 61 73 65  stored, but case
5fe80 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 66 69 63   is not signific
5fe90 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 61  ant for.** compa
5fea0 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 61  risons..**.** Ta
5feb0 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 65 20  ble.nCol is the 
5fec0 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
5fed0 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 2e  s in this table.
5fee0 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 73 20    Table.aCol is 
5fef0 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20  a.** pointer to 
5ff00 61 6e 20 61 72 72 61 79 20 6f 66 20 43 6f 6c 75  an array of Colu
5ff10 6d 6e 20 73 74 72 75 63 74 75 72 65 73 2c 20 6f  mn structures, o
5ff20 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  ne for each colu
5ff30 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  mn..**.** If the
5ff40 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e   table has an IN
5ff50 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
5ff60 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e 69 50  Y, then Table.iP
5ff70 4b 65 79 20 69 73 20 74 68 65 20 69 6e 64 65 78  Key is the index
5ff80 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d   of.** the colum
5ff90 6e 20 74 68 61 74 20 69 73 20 74 68 61 74 20 6b  n that is that k
5ffa0 65 79 2e 20 20 20 4f 74 68 65 72 77 69 73 65 20  ey.   Otherwise 
5ffb0 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 6e  Table.iPKey is n
5ffc0 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 0a 2a  egative.  Note.*
5ffd0 2a 20 74 68 61 74 20 74 68 65 20 64 61 74 61 74  * that the datat
5ffe0 79 70 65 20 6f 66 20 74 68 65 20 50 52 49 4d 41  ype of the PRIMA
5fff0 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 49  RY KEY must be I
60000 4e 54 45 47 45 52 20 66 6f 72 20 74 68 69 73 20  NTEGER for this 
60010 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65 20 73  field to.** be s
60020 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 45 52 20  et.  An INTEGER 
60030 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20 75  PRIMARY KEY is u
60040 73 65 64 20 61 73 20 74 68 65 20 72 6f 77 69 64  sed as the rowid
60050 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66   for each row of
60060 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20 20  .** the table.  
60070 49 66 20 61 20 74 61 62 6c 65 20 68 61 73 20 6e  If a table has n
60080 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  o INTEGER PRIMAR
60090 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 20 72 61  Y KEY, then a ra
600a0 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20 69 73  ndom rowid.** is
600b0 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 65   generated for e
600c0 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20 74  ach row of the t
600d0 61 62 6c 65 2e 20 20 54 46 5f 48 61 73 50 72 69  able.  TF_HasPri
600e0 6d 61 72 79 4b 65 79 20 69 73 20 73 65 74 20 69  maryKey is set i
600f0 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 68  f.** the table h
60100 61 73 20 61 6e 79 20 50 52 49 4d 41 52 59 20 4b  as any PRIMARY K
60110 45 59 2c 20 49 4e 54 45 47 45 52 20 6f 72 20 6f  EY, INTEGER or o
60120 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20  therwise..**.** 
60130 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20 74 68  Table.tnum is th
60140 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  e page number fo
60150 72 20 74 68 65 20 72 6f 6f 74 20 42 54 72 65 65  r the root BTree
60160 20 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62   page of the tab
60170 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64 61 74  le in the.** dat
60180 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20  abase file.  If 
60190 54 61 62 6c 65 2e 69 44 62 20 69 73 20 74 68 65  Table.iDb is the
601a0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   index of the da
601b0 74 61 62 61 73 65 20 74 61 62 6c 65 20 62 61 63  tabase table bac
601c0 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c 69 74  kend.** in sqlit
601d0 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73 20 66  e.aDb[].  0 is f
601e0 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  or the main data
601f0 62 61 73 65 20 61 6e 64 20 31 20 69 73 20 66 6f  base and 1 is fo
60200 72 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 0a  r the file that.
60210 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f 72 61  ** holds tempora
60220 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e  ry tables and in
60230 64 69 63 65 73 2e 20 20 49 66 20 54 46 5f 45 70  dices.  If TF_Ep
60240 68 65 6d 65 72 61 6c 20 69 73 20 73 65 74 0a 2a  hemeral is set.*
60250 2a 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65  * then the table
60260 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20   is stored in a 
60270 66 69 6c 65 20 74 68 61 74 20 69 73 20 61 75 74  file that is aut
60280 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74  omatically delet
60290 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 56  ed.** when the V
602a0 44 42 45 20 63 75 72 73 6f 72 20 74 6f 20 74 68  DBE cursor to th
602b0 65 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73 65  e table is close
602c0 64 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73 65  d.  In this case
602d0 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a 20   Table.tnum .** 
602e0 72 65 66 65 72 73 20 56 44 42 45 20 63 75 72 73  refers VDBE curs
602f0 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 74 20 68  or number that h
60300 6f 6c 64 73 20 74 68 65 20 74 61 62 6c 65 20 6f  olds the table o
60310 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68 65 20  pen, not to the 
60320 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 6e 75 6d  root.** page num
60330 62 65 72 2e 20 20 54 72 61 6e 73 69 65 6e 74 20  ber.  Transient 
60340 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 20  tables are used 
60350 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75  to hold the resu
60360 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73 75 62 2d  lts of a.** sub-
60370 71 75 65 72 79 20 74 68 61 74 20 61 70 70 65 61  query that appea
60380 72 73 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  rs instead of a 
60390 72 65 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 20  real table name 
603a0 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
603b0 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45  se .** of a SELE
603c0 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  CT statement..*/
603d0 0a 73 74 72 75 63 74 20 54 61 62 6c 65 20 7b 0a  .struct Table {.
603e0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4d 65 6d    sqlite3 *dbMem
603f0 3b 20 20 20 20 20 20 2f 2a 20 44 42 20 63 6f 6e  ;      /* DB con
60400 6e 65 63 74 69 6f 6e 20 75 73 65 64 20 66 6f 72  nection used for
60410 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63   lookaside alloc
60420 61 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 63 68 61  ations. */.  cha
60430 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
60440 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
60450 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a   table or view *
60460 2f 0a 20 20 69 6e 74 20 69 50 4b 65 79 3b 20 20  /.  int iPKey;  
60470 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e           /* If n
60480 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 75 73 65  ot negative, use
60490 20 61 43 6f 6c 5b 69 50 4b 65 79 5d 20 61 73 20   aCol[iPKey] as 
604a0 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20  the primary key 
604b0 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20  */.  int nCol;  
604c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
604d0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
604e0 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a  n this table */.
604f0 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b 20    Column *aCol; 
60500 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d         /* Inform
60510 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68  ation about each
60520 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64   column */.  Ind
60530 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20  ex *pIndex;     
60540 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c    /* List of SQL
60550 20 69 6e 64 65 78 65 73 20 6f 6e 20 74 68 69 73   indexes on this
60560 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 69 6e 74   table. */.  int
60570 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20   tnum;          
60580 20 20 2f 2a 20 52 6f 6f 74 20 42 54 72 65 65 20    /* Root BTree 
60590 6e 6f 64 65 20 66 6f 72 20 74 68 69 73 20 74 61  node for this ta
605a0 62 6c 65 20 28 73 65 65 20 6e 6f 74 65 20 61 62  ble (see note ab
605b0 6f 76 65 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ove) */.  Select
605c0 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20 2f   *pSelect;     /
605d0 2a 20 4e 55 4c 4c 20 66 6f 72 20 74 61 62 6c 65  * NULL for table
605e0 73 2e 20 20 50 6f 69 6e 74 73 20 74 6f 20 64 65  s.  Points to de
605f0 66 69 6e 69 74 69 6f 6e 20 69 66 20 61 20 76 69  finition if a vi
60600 65 77 2e 20 2a 2f 0a 20 20 75 31 36 20 6e 52 65  ew. */.  u16 nRe
60610 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  f;            /*
60620 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74   Number of point
60630 65 72 73 20 74 6f 20 74 68 69 73 20 54 61 62 6c  ers to this Tabl
60640 65 20 2a 2f 0a 20 20 75 38 20 74 61 62 46 6c 61  e */.  u8 tabFla
60650 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  gs;         /* M
60660 61 73 6b 20 6f 66 20 54 46 5f 2a 20 76 61 6c 75  ask of TF_* valu
60670 65 73 20 2a 2f 0a 20 20 75 38 20 6b 65 79 43 6f  es */.  u8 keyCo
60680 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nf;          /* 
60690 57 68 61 74 20 74 6f 20 64 6f 20 69 6e 20 63 61  What to do in ca
606a0 73 65 20 6f 66 20 75 6e 69 71 75 65 6e 65 73 73  se of uniqueness
606b0 20 63 6f 6e 66 6c 69 63 74 20 6f 6e 20 69 50 4b   conflict on iPK
606c0 65 79 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46  ey */.  FKey *pF
606d0 4b 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Key;         /* 
606e0 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61  Linked list of a
606f0 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20  ll foreign keys 
60700 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  in this table */
60710 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66  .  char *zColAff
60720 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e  ;       /* Strin
60730 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 61  g defining the a
60740 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 20  ffinity of each 
60750 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65  column */.#ifnde
60760 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  f SQLITE_OMIT_CH
60770 45 43 4b 0a 20 20 45 78 70 72 20 2a 70 43 68 65  ECK.  Expr *pChe
60780 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ck;        /* Th
60790 65 20 41 4e 44 20 6f 66 20 61 6c 6c 20 43 48 45  e AND of all CHE
607a0 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  CK constraints *
607b0 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  /.#endif.#ifndef
607c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54   SQLITE_OMIT_ALT
607d0 45 52 54 41 42 4c 45 0a 20 20 69 6e 74 20 61 64  ERTABLE.  int ad
607e0 64 43 6f 6c 4f 66 66 73 65 74 3b 20 20 20 20 2f  dColOffset;    /
607f0 2a 20 4f 66 66 73 65 74 20 69 6e 20 43 52 45 41  * Offset in CREA
60800 54 45 20 54 41 42 4c 45 20 73 74 6d 74 20 74 6f  TE TABLE stmt to
60810 20 61 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d   add a new colum
60820 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e  n */.#endif.#ifn
60830 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
60840 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 56  VIRTUALTABLE.  V
60850 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 3b 20  Table *pVTable; 
60860 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 56      /* List of V
60870 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e 20 2a  Table objects. *
60880 2f 0a 20 20 69 6e 74 20 6e 4d 6f 64 75 6c 65 41  /.  int nModuleA
60890 72 67 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  rg;      /* Numb
608a0 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
608b0 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  to the module */
608c0 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75  .  char **azModu
608d0 6c 65 41 72 67 3b 20 20 2f 2a 20 54 65 78 74 20  leArg;  /* Text 
608e0 6f 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 61 72  of all module ar
608f0 67 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64 75 6c  gs. [0] is modul
60900 65 20 6e 61 6d 65 20 2a 2f 0a 23 65 6e 64 69 66  e name */.#endif
60910 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
60920 67 67 65 72 3b 20 20 20 2f 2a 20 4c 69 73 74 20  gger;   /* List 
60930 6f 66 20 74 72 69 67 67 65 72 73 20 73 74 6f 72  of triggers stor
60940 65 64 20 69 6e 20 70 53 63 68 65 6d 61 20 2a 2f  ed in pSchema */
60950 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65  .  Schema *pSche
60960 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d  ma;     /* Schem
60970 61 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  a that contains 
60980 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
60990 54 61 62 6c 65 20 2a 70 4e 65 78 74 5a 6f 6d 62  Table *pNextZomb
609a0 69 65 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20  ie;  /* Next on 
609b0 74 68 65 20 50 61 72 73 65 2e 70 5a 6f 6d 62 69  the Parse.pZombi
609c0 65 54 61 62 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a  eTab list */.};.
609d0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
609e0 61 6c 75 65 73 20 66 6f 72 20 54 61 62 65 2e 74  alues for Tabe.t
609f0 61 62 46 6c 61 67 73 2e 0a 2a 2f 0a 23 64 65 66  abFlags..*/.#def
60a00 69 6e 65 20 54 46 5f 52 65 61 64 6f 6e 6c 79 20  ine TF_Readonly 
60a10 20 20 20 20 20 20 20 30 78 30 31 20 20 20 20 2f         0x01    /
60a20 2a 20 52 65 61 64 2d 6f 6e 6c 79 20 73 79 73 74  * Read-only syst
60a30 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66  em table */.#def
60a40 69 6e 65 20 54 46 5f 45 70 68 65 6d 65 72 61 6c  ine TF_Ephemeral
60a50 20 20 20 20 20 20 20 30 78 30 32 20 20 20 20 2f         0x02    /
60a60 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 74  * An ephemeral t
60a70 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  able */.#define 
60a80 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79  TF_HasPrimaryKey
60a90 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 61     0x04    /* Ta
60aa0 62 6c 65 20 68 61 73 20 61 20 70 72 69 6d 61 72  ble has a primar
60ab0 79 20 6b 65 79 20 2a 2f 0a 23 64 65 66 69 6e 65  y key */.#define
60ac0 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e   TF_Autoincremen
60ad0 74 20 20 20 30 78 30 38 20 20 20 20 2f 2a 20 49  t   0x08    /* I
60ae0 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b  nteger primary k
60af0 65 79 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d  ey is autoincrem
60b00 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ent */.#define T
60b10 46 5f 56 69 72 74 75 61 6c 20 20 20 20 20 20 20  F_Virtual       
60b20 20 20 30 78 31 30 20 20 20 20 2f 2a 20 49 73 20    0x10    /* Is 
60b30 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
60b40 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 4e 65  */.#define TF_Ne
60b50 65 64 4d 65 74 61 64 61 74 61 20 20 20 20 30 78  edMetadata    0x
60b60 32 30 20 20 20 20 2f 2a 20 61 43 6f 6c 5b 5d 2e  20    /* aCol[].
60b70 7a 54 79 70 65 20 61 6e 64 20 61 43 6f 6c 5b 5d  zType and aCol[]
60b80 2e 70 43 6f 6c 6c 20 6d 69 73 73 69 6e 67 20 2a  .pColl missing *
60b90 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20  /..../*.** Test 
60ba0 74 6f 20 73 65 65 20 77 68 65 74 68 65 72 20 6f  to see whether o
60bb0 72 20 6e 6f 74 20 61 20 74 61 62 6c 65 20 69 73  r not a table is
60bc0 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
60bd0 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 64 6f  .  This is.** do
60be0 6e 65 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f  ne as a macro so
60bf0 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65   that it will be
60c00 20 6f 70 74 69 6d 69 7a 65 64 20 6f 75 74 20 77   optimized out w
60c10 68 65 6e 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74  hen virtual.** t
60c20 61 62 6c 65 20 73 75 70 70 6f 72 74 20 69 73 20  able support is 
60c30 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65  omitted from the
60c40 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64   build..*/.#ifnd
60c50 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
60c60 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 64  IRTUALTABLE.#  d
60c70 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28  efine IsVirtual(
60c80 58 29 20 20 20 20 20 20 28 28 28 58 29 2d 3e 74  X)      (((X)->t
60c90 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72  abFlags & TF_Vir
60ca0 74 75 61 6c 29 21 3d 30 29 0a 23 20 20 64 65 66  tual)!=0).#  def
60cb0 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75  ine IsHiddenColu
60cc0 6d 6e 28 58 29 20 28 28 58 29 2d 3e 69 73 48 69  mn(X) ((X)->isHi
60cd0 64 64 65 6e 29 0a 23 65 6c 73 65 0a 23 20 20 64  dden).#else.#  d
60ce0 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28  efine IsVirtual(
60cf0 58 29 20 20 20 20 20 20 30 0a 23 20 20 64 65 66  X)      0.#  def
60d00 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75  ine IsHiddenColu
60d10 6d 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a  mn(X) 0.#endif..
60d20 2f 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72 65 69  /*.** Each forei
60d30 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
60d40 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  t is an instance
60d50 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
60d60 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  g structure..**.
60d70 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b 65 79  ** A foreign key
60d80 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77   is associated w
60d90 69 74 68 20 74 77 6f 20 74 61 62 6c 65 73 2e 20  ith two tables. 
60da0 20 54 68 65 20 22 66 72 6f 6d 22 20 74 61 62 6c   The "from" tabl
60db0 65 20 69 73 0a 2a 2a 20 74 68 65 20 74 61 62 6c  e is.** the tabl
60dc0 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
60dd0 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 63  the REFERENCES c
60de0 6c 61 75 73 65 20 74 68 61 74 20 63 72 65 61 74  lause that creat
60df0 65 73 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a  es the foreign.*
60e00 2a 20 6b 65 79 2e 20 20 54 68 65 20 22 74 6f 22  * key.  The "to"
60e10 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 74 61   table is the ta
60e20 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d 65  ble that is name
60e30 64 20 69 6e 20 74 68 65 20 52 45 46 45 52 45 4e  d in the REFEREN
60e40 43 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 43  CES clause..** C
60e50 6f 6e 73 69 64 65 72 20 74 68 69 73 20 65 78 61  onsider this exa
60e60 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mple:.**.**     
60e70 43 52 45 41 54 45 20 54 41 42 4c 45 20 65 78 31  CREATE TABLE ex1
60e80 28 0a 2a 2a 20 20 20 20 20 20 20 61 20 49 4e 54  (.**       a INT
60e90 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
60ea0 2c 0a 2a 2a 20 20 20 20 20 20 20 62 20 49 4e 54  ,.**       b INT
60eb0 45 47 45 52 20 43 4f 4e 53 54 52 41 49 4e 54 20  EGER CONSTRAINT 
60ec0 66 6b 31 20 52 45 46 45 52 45 4e 43 45 53 20 65  fk1 REFERENCES e
60ed0 78 32 28 78 29 0a 2a 2a 20 20 20 20 20 29 3b 0a  x2(x).**     );.
60ee0 2a 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 69 67  **.** For foreig
60ef0 6e 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 68 65  n key "fk1", the
60f00 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 22   from-table is "
60f10 65 78 31 22 20 61 6e 64 20 74 68 65 20 74 6f 2d  ex1" and the to-
60f20 74 61 62 6c 65 20 69 73 20 22 65 78 32 22 2e 0a  table is "ex2"..
60f30 2a 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46 45 52  **.** Each REFER
60f40 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67 65 6e  ENCES clause gen
60f50 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74 61 6e  erates an instan
60f60 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
60f70 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  ing structure.**
60f80 20 77 68 69 63 68 20 69 73 20 61 74 74 61 63 68   which is attach
60f90 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d 2d 74  ed to the from-t
60fa0 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d 74 61  able.  The to-ta
60fb0 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65 78 69  ble need not exi
60fc0 73 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 66  st when.** the f
60fd0 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63 72 65  rom-table is cre
60fe0 61 74 65 64 2e 20 20 54 68 65 20 65 78 69 73 74  ated.  The exist
60ff0 65 6e 63 65 20 6f 66 20 74 68 65 20 74 6f 2d 74  ence of the to-t
61000 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 68 65 63  able is not chec
61010 6b 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46  ked..*/.struct F
61020 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70  Key {.  Table *p
61030 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 61 62  From;     /* Tab
61040 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
61050 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61  e REFERENCES cla
61060 75 73 65 20 28 61 6b 61 3a 20 43 68 69 6c 64 29  use (aka: Child)
61070 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78   */.  FKey *pNex
61080 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20  tFrom;  /* Next 
61090 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70  foreign key in p
610a0 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a  From */.  char *
610b0 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  zTo;        /* N
610c0 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61  ame of table tha
610d0 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73  t the key points
610e0 20 74 6f 20 28 61 6b 61 3a 20 50 61 72 65 6e 74   to (aka: Parent
610f0 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65  ) */.  FKey *pNe
61100 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74  xtTo;    /* Next
61110 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20   foreign key on 
61120 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20  table named zTo 
61130 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 50 72 65 76  */.  FKey *pPrev
61140 54 6f 3b 20 20 20 20 2f 2a 20 50 72 65 76 69 6f  To;    /* Previo
61150 75 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f  us foreign key o
61160 6e 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54  n table named zT
61170 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  o */.  int nCol;
61180 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
61190 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
611a0 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 2f   this key */.  /
611b0 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32 31  * EV: R-30323-21
611c0 39 31 37 20 2a 2f 0a 20 20 75 38 20 69 73 44 65  917 */.  u8 isDe
611d0 66 65 72 72 65 64 3b 20 20 20 20 2f 2a 20 54 72  ferred;    /* Tr
611e0 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74  ue if constraint
611f0 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 65 66   checking is def
61200 65 72 72 65 64 20 74 69 6c 6c 20 43 4f 4d 4d 49  erred till COMMI
61210 54 20 2a 2f 0a 20 20 75 38 20 61 41 63 74 69 6f  T */.  u8 aActio
61220 6e 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 2f  n[2];          /
61230 2a 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 20  * ON DELETE and 
61240 4f 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e  ON UPDATE action
61250 73 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 20  s, respectively 
61260 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 61 70  */.  Trigger *ap
61270 54 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f 2a 20  Trigger[2];  /* 
61280 54 72 69 67 67 65 72 73 20 66 6f 72 20 61 41 63  Triggers for aAc
61290 74 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73 20 2a  tion[] actions *
612a0 2f 0a 20 20 73 74 72 75 63 74 20 73 43 6f 6c 4d  /.  struct sColM
612b0 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 6e 67  ap {  /* Mapping
612c0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70   of columns in p
612d0 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73 20  From to columns 
612e0 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20 69 6e  in zTo */.    in
612f0 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20  t iFrom;        
61300 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
61310 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a  umn in pFrom */.
61320 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20      char *zCol; 
61330 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
61340 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e  f column in zTo.
61350 20 20 49 66 20 30 20 75 73 65 20 50 52 49 4d 41    If 0 use PRIMA
61360 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20 61 43  RY KEY */.  } aC
61370 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a  ol[1];        /*
61380 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65   One entry for e
61390 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75  ach of nCol colu
613a0 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  mn s */.};../*.*
613b0 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74  * SQLite support
613c0 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74  s many different
613d0 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65   ways to resolve
613e0 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a   a constraint.**
613f0 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43   error.  ROLLBAC
61400 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61  K processing mea
61410 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72  ns that a constr
61420 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a  aint violation.*
61430 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f 70 65  * causes the ope
61440 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73  ration in proces
61450 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f  s to fail and fo
61460 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  r the current tr
61470 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20  ansaction.** to 
61480 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  be rolled back. 
61490 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e   ABORT processin
614a0 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72  g means the oper
614b0 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73  ation in process
614c0 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e  .** fails and an
614d0 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20  y prior changes 
614e0 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70  from that one op
614f0 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b  eration are back
61500 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74  ed out,.** but t
61510 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
61520 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63  s not rolled bac
61530 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73  k.  FAIL process
61540 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a  ing means that.*
61550 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  * the operation 
61560 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70  in progress stop
61570 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e  s and returns an
61580 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75   error code.  Bu
61590 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67  t prior.** chang
615a0 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 73 61  es due to the sa
615b0 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65  me operation are
615c0 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20   not backed out 
615d0 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a  and no rollback.
615e0 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f  ** occurs.  IGNO
615f0 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  RE means that th
61600 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77  e particular row
61610 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65   that caused the
61620 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65   constraint.** e
61630 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65  rror is not inse
61640 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e  rted or updated.
61650 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e    Processing con
61660 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72  tinues and no er
61670 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  ror.** is return
61680 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61  ed.  REPLACE mea
61690 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73 74  ns that preexist
616a0 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f 77  ing database row
616b0 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a  s that caused.**
616c0 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72   a UNIQUE constr
616d0 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61  aint violation a
616e0 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68  re removed so th
616f0 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72  at the new inser
61700 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63  t or.** update c
61710 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f  an proceed.  Pro
61720 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65  cessing continue
61730 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69  s and no error i
61740 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a  s reported..**.*
61750 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e  * RESTRICT, SETN
61760 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45  ULL, and CASCADE
61770 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f   actions apply o
61780 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b  nly to foreign k
61790 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54  eys..** RESTRICT
617a0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
617b0 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49  ABORT for IMMEDI
617c0 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73  ATE foreign keys
617d0 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65   and the.** same
617e0 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72   as ROLLBACK for
617f0 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20   DEFERRED keys. 
61800 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74   SETNULL means t
61810 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a  hat the foreign.
61820 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f  ** key is set to
61830 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20   NULL.  CASCADE 
61840 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c  means that a DEL
61850 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66  ETE or UPDATE of
61860 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63   the.** referenc
61870 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20  ed table row is 
61880 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20  propagated into 
61890 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c  the row that hol
618a0 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67  ds the.** foreig
618b0 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  n key..** .** Th
618c0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62  e following symb
618d0 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65 20  olic values are 
618e0 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77  used to record w
618f0 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20  hich type.** of 
61900 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a  action to take..
61910 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f  */.#define OE_No
61920 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68  ne     0   /* Th
61930 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72  ere is no constr
61940 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f  aint to check */
61950 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c  .#define OE_Roll
61960 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c  back 1   /* Fail
61970 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61   the operation a
61980 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  nd rollback the 
61990 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23  transaction */.#
619a0 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20  define OE_Abort 
619b0 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f     2   /* Back o
619c0 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20 64  ut changes but d
619d0 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72  o no rollback tr
619e0 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65  ansaction */.#de
619f0 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20  fine OE_Fail    
61a00 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65   3   /* Stop the
61a10 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c   operation but l
61a20 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63  eave all prior c
61a30 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e  hanges */.#defin
61a40 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20  e OE_Ignore   4 
61a50 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20    /* Ignore the 
61a60 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f  error. Do not do
61a70 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55   the INSERT or U
61a80 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65  PDATE */.#define
61a90 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20   OE_Replace  5  
61aa0 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74   /* Delete exist
61ab0 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e  ing record, then
61ac0 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50   do INSERT or UP
61ad0 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65  DATE */..#define
61ae0 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20 20   OE_Restrict 6  
61af0 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72   /* OE_Abort for
61b00 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52   IMMEDIATE, OE_R
61b10 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45  ollback for DEFE
61b20 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20  RRED */.#define 
61b30 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20  OE_SetNull  7   
61b40 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69  /* Set the forei
61b50 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20  gn key value to 
61b60 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NULL */.#define 
61b70 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20  OE_SetDflt  8   
61b80 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69  /* Set the forei
61b90 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20  gn key value to 
61ba0 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23  its default */.#
61bb0 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64  define OE_Cascad
61bc0 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64  e  9   /* Cascad
61bd0 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f  e the changes */
61be0 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66  ..#define OE_Def
61bf0 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20  ault  99  /* Do 
61c00 77 68 61 74 65 76 65 72 20 74 68 65 20 64 65 66  whatever the def
61c10 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a  ault action is *
61c20 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  /.../*.** An ins
61c30 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
61c40 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
61c50 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68   is passed as th
61c60 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d  e first.** argum
61c70 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64  ent to sqlite3Vd
61c80 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64  beKeyCompare and
61c90 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74   is used to cont
61ca0 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70  rol the .** comp
61cb0 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77  arison of the tw
61cc0 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f  o index keys..*/
61cd0 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20  .struct KeyInfo 
61ce0 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
61cf0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
61d00 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
61d10 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20  on */.  u8 enc; 
61d20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
61d30 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f  ext encoding - o
61d40 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55  ne of the TEXT_U
61d50 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  tf* values */.  
61d60 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20  u16 nField;     
61d70 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
61d80 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c   entries in aCol
61d90 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f  l[] */.  u8 *aSo
61da0 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a 20  rtOrder;     /* 
61db0 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20 61 53  If defined an aS
61dc0 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 74  ortOrder[i] is t
61dd0 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43 20 2a  rue, sort DESC *
61de0 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f  /.  CollSeq *aCo
61df0 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61  ll[1];  /* Colla
61e00 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f  ting sequence fo
61e10 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74  r each term of t
61e20 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  he key */.};../*
61e30 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
61e40 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
61e50 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
61e60 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
61e70 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69  ut a.** single i
61e80 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68 61 74  ndex record that
61e90 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
61ea0 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69 6e 74  n parsed out int
61eb0 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20  o individual.** 
61ec0 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20  values..**.** A 
61ed0 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a  record is an obj
61ee0 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ect that contain
61ef0 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69  s one or more fi
61f00 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a  elds of data..**
61f10 20 52 65 63 6f 72 64 73 20 61 72 65 20 75 73 65   Records are use
61f20 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63  d to store the c
61f30 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 62 6c  ontent of a tabl
61f40 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f  e row and to sto
61f50 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66  re.** the key of
61f60 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c   an index.  A bl
61f70 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61  ob encoding of a
61f80 20 72 65 63 6f 72 64 20 69 73 20 63 72 65 61 74   record is creat
61f90 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f  ed by.** the OP_
61fa0 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64  MakeRecord opcod
61fb0 65 20 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e  e of the VDBE an
61fc0 64 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c 65  d is disassemble
61fd0 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43  d by the.** OP_C
61fe0 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a  olumn opcode..**
61ff0 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75  .** This structu
62000 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f 72  re holds a recor
62010 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61  d that has alrea
62020 64 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 6d  dy been disassem
62030 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73  bled.** into its
62040 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 69 65   constituent fie
62050 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55  lds..*/.struct U
62060 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a  npackedRecord {.
62070 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
62080 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69  nfo;  /* Collati
62090 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65  on and sort-orde
620a0 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f  r information */
620b0 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20  .  u16 nField;  
620c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
620d0 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
620e0 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20  pMem[] */.  u16 
620f0 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
62100 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69  /* Boolean setti
62110 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e  ngs.  UNPACKED_.
62120 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 36  .. below */.  i6
62130 34 20 72 6f 77 69 64 3b 20 20 20 20 20 20 20 20  4 rowid;        
62140 20 20 2f 2a 20 55 73 65 64 20 62 79 20 55 4e 50    /* Used by UNP
62150 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
62160 52 43 48 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d  RCH */.  Mem *aM
62170 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  em;          /* 
62180 56 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  Values */.};../*
62190 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
621a0 65 73 20 6f 66 20 55 6e 70 61 63 6b 65 64 52 65  es of UnpackedRe
621b0 63 6f 72 64 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64  cord.flags.*/.#d
621c0 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e  efine UNPACKED_N
621d0 45 45 44 5f 46 52 45 45 20 20 20 20 20 30 78 30  EED_FREE     0x0
621e0 30 30 31 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 69  001  /* Memory i
621f0 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61  s from sqlite3Ma
62200 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e  lloc() */.#defin
62210 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f  e UNPACKED_NEED_
62220 44 45 53 54 52 4f 59 20 20 30 78 30 30 30 32 20  DESTROY  0x0002 
62230 20 2f 2a 20 61 70 4d 65 6d 5b 5d 73 20 73 68 6f   /* apMem[]s sho
62240 75 6c 64 20 61 6c 6c 20 62 65 20 64 65 73 74 72  uld all be destr
62250 6f 79 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  oyed */.#define 
62260 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f  UNPACKED_IGNORE_
62270 52 4f 57 49 44 20 20 30 78 30 30 30 34 20 20 2f  ROWID  0x0004  /
62280 2a 20 49 67 6e 6f 72 65 20 74 72 61 69 6c 69 6e  * Ignore trailin
62290 67 20 72 6f 77 69 64 20 6f 6e 20 6b 65 79 31 20  g rowid on key1 
622a0 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43  */.#define UNPAC
622b0 4b 45 44 5f 49 4e 43 52 4b 45 59 20 20 20 20 20  KED_INCRKEY     
622c0 20 20 30 78 30 30 30 38 20 20 2f 2a 20 4d 61 6b    0x0008  /* Mak
622d0 65 20 74 68 69 73 20 6b 65 79 20 61 6e 20 65 70  e this key an ep
622e0 73 69 6c 6f 6e 20 6c 61 72 67 65 72 20 2a 2f 0a  silon larger */.
622f0 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44  #define UNPACKED
62300 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 20 30  _PREFIX_MATCH  0
62310 78 30 30 31 30 20 20 2f 2a 20 41 20 70 72 65 66  x0010  /* A pref
62320 69 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e 73  ix match is cons
62330 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 23 64 65  idered OK */.#de
62340 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 50 52  fine UNPACKED_PR
62350 45 46 49 58 5f 53 45 41 52 43 48 20 30 78 30 30  EFIX_SEARCH 0x00
62360 32 30 20 20 2f 2a 20 41 20 70 72 65 66 69 78 20  20  /* A prefix 
62370 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69 64 65  match is conside
62380 72 65 64 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  red OK */../*.**
62390 20 45 61 63 68 20 53 51 4c 20 69 6e 64 65 78 20   Each SQL index 
623a0 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69  is represented i
623b0 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 0a 2a  n memory by an.*
623c0 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
623d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
623e0 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cture..**.** The
623f0 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
62400 74 61 62 6c 65 20 74 68 61 74 20 61 72 65 20 74  table that are t
62410 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61 72 65  o be indexed are
62420 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62 79   described.** by
62430 20 74 68 65 20 61 69 43 6f 6c 75 6d 6e 5b 5d 20   the aiColumn[] 
62440 66 69 65 6c 64 20 6f 66 20 74 68 69 73 20 73 74  field of this st
62450 72 75 63 74 75 72 65 2e 20 20 46 6f 72 20 65 78  ructure.  For ex
62460 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 0a 2a  ample, suppose.*
62470 2a 20 77 65 20 68 61 76 65 20 74 68 65 20 66 6f  * we have the fo
62480 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 61 6e  llowing table an
62490 64 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20  d index:.**.**  
624a0 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
624b0 45 78 31 28 63 31 20 69 6e 74 2c 20 63 32 20 69  Ex1(c1 int, c2 i
624c0 6e 74 2c 20 63 33 20 74 65 78 74 29 3b 0a 2a 2a  nt, c3 text);.**
624d0 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45       CREATE INDE
624e0 58 20 45 78 32 20 4f 4e 20 45 78 31 28 63 33 2c  X Ex2 ON Ex1(c3,
624f0 63 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  c1);.**.** In th
62500 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
62510 65 20 64 65 73 63 72 69 62 69 6e 67 20 45 78 31  e describing Ex1
62520 2c 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61 75 73  , nCol==3 becaus
62530 65 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 74  e there are.** t
62540 68 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  hree columns in 
62550 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 20 74  the table.  In t
62560 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  he Index structu
62570 72 65 20 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a  re describing.**
62580 20 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d 32   Ex2, nColumn==2
62590 20 73 69 6e 63 65 20 32 20 6f 66 20 74 68 65 20   since 2 of the 
625a0 33 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45 78 31  3 columns of Ex1
625b0 20 61 72 65 20 69 6e 64 65 78 65 64 2e 0a 2a 2a   are indexed..**
625c0 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 61 69   The value of ai
625d0 43 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20 30 7d  Column is {2, 0}
625e0 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d  .  aiColumn[0]==
625f0 32 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 2a  2 because the .*
62600 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 74  * first column t
62610 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 33  o be indexed (c3
62620 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f  ) has an index o
62630 66 20 32 20 69 6e 20 45 78 31 2e 61 43 6f 6c 5b  f 2 in Ex1.aCol[
62640 5d 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  ]..** The second
62650 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e   column to be in
62660 64 65 78 65 64 20 28 63 31 29 20 68 61 73 20 61  dexed (c1) has a
62670 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 69 6e 0a  n index of 0 in.
62680 2a 2a 20 45 78 31 2e 61 43 6f 6c 5b 5d 2c 20 68  ** Ex1.aCol[], h
62690 65 6e 63 65 20 45 78 32 2e 61 69 43 6f 6c 75 6d  ence Ex2.aiColum
626a0 6e 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54  n[1]==0..**.** T
626b0 68 65 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72  he Index.onError
626c0 20 66 69 65 6c 64 20 64 65 74 65 72 6d 69 6e 65   field determine
626d0 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  s whether or not
626e0 20 74 68 65 20 69 6e 64 65 78 65 64 20 63 6f 6c   the indexed col
626f0 75 6d 6e 73 0a 2a 2a 20 6d 75 73 74 20 62 65 20  umns.** must be 
62700 75 6e 69 71 75 65 20 61 6e 64 20 77 68 61 74 20  unique and what 
62710 74 6f 20 64 6f 20 69 66 20 74 68 65 79 20 61 72  to do if they ar
62720 65 20 6e 6f 74 2e 20 20 57 68 65 6e 20 49 6e 64  e not.  When Ind
62730 65 78 2e 6f 6e 45 72 72 6f 72 3d 4f 45 5f 4e 6f  ex.onError=OE_No
62740 6e 65 2c 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20  ne,.** it means 
62750 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 75 6e  this is not a un
62760 69 71 75 65 20 69 6e 64 65 78 2e 20 20 4f 74 68  ique index.  Oth
62770 65 72 77 69 73 65 20 69 74 20 69 73 20 61 20 75  erwise it is a u
62780 6e 69 71 75 65 20 69 6e 64 65 78 0a 2a 2a 20 61  nique index.** a
62790 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  nd the value of 
627a0 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 69 6e  Index.onError in
627b0 64 69 63 61 74 65 20 74 68 65 20 77 68 69 63 68  dicate the which
627c0 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   conflict resolu
627d0 74 69 6f 6e 20 0a 2a 2a 20 61 6c 67 6f 72 69 74  tion .** algorit
627e0 68 6d 20 74 6f 20 65 6d 70 6c 6f 79 20 77 68 65  hm to employ whe
627f0 6e 65 76 65 72 20 61 6e 20 61 74 74 65 6d 70 74  never an attempt
62800 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73 65   is made to inse
62810 72 74 20 61 20 6e 6f 6e 2d 75 6e 69 71 75 65 0a  rt a non-unique.
62820 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 73  ** element..*/.s
62830 74 72 75 63 74 20 49 6e 64 65 78 20 7b 0a 20 20  truct Index {.  
62840 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
62850 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73   /* Name of this
62860 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
62870 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20  nColumn;     /* 
62880 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
62890 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 75  s in the table u
628a0 73 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65  sed by this inde
628b0 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f  x */.  int *aiCo
628c0 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 63 68  lumn;   /* Which
628d0 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73 65   columns are use
628e0 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 2e  d by this index.
628f0 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0a 20 20    1st is 0 */.  
62900 75 6e 73 69 67 6e 65 64 20 2a 61 69 52 6f 77 45  unsigned *aiRowE
62910 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66  st; /* Result of
62920 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e 20 72   ANALYZE: Est. r
62930 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62 79 20  ows selected by 
62940 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  each column */. 
62950 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20   Table *pTable; 
62960 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 61 62    /* The SQL tab
62970 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78 65 64  le being indexed
62980 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20   */.  int tnum; 
62990 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63         /* Page c
629a0 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6f  ontaining root o
629b0 66 20 74 68 69 73 20 69 6e 64 65 78 20 69 6e 20  f this index in 
629c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
629d0 0a 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b 20 20  .  u8 onError;  
629e0 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c      /* OE_Abort,
629f0 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52   OE_Ignore, OE_R
62a00 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f  eplace, or OE_No
62a10 6e 65 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 49  ne */.  u8 autoI
62a20 6e 64 65 78 3b 20 20 20 20 2f 2a 20 54 72 75 65  ndex;    /* True
62a30 20 69 66 20 69 73 20 61 75 74 6f 6d 61 74 69 63   if is automatic
62a40 61 6c 6c 79 20 63 72 65 61 74 65 64 20 28 65 78  ally created (ex
62a50 3a 20 62 79 20 55 4e 49 51 55 45 29 20 2a 2f 0a  : by UNIQUE) */.
62a60 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b    char *zColAff;
62a70 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 66     /* String def
62a80 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69  ining the affini
62a90 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d  ty of each colum
62aa0 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 4e  n */.  Index *pN
62ab0 65 78 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 6e  ext;    /* The n
62ac0 65 78 74 20 69 6e 64 65 78 20 61 73 73 6f 63 69  ext index associ
62ad0 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 61  ated with the sa
62ae0 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 53 63  me table */.  Sc
62af0 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 2f  hema *pSchema; /
62b00 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e  * Schema contain
62b10 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a  ing this index *
62b20 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64  /.  u8 *aSortOrd
62b30 65 72 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66  er;  /* Array of
62b40 20 73 69 7a 65 20 49 6e 64 65 78 2e 6e 43 6f 6c   size Index.nCol
62b50 75 6d 6e 2e 20 54 72 75 65 3d 3d 44 45 53 43 2c  umn. True==DESC,
62b60 20 46 61 6c 73 65 3d 3d 41 53 43 20 2a 2f 0a 20   False==ASC */. 
62b70 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 6c 3b 20   char **azColl; 
62b80 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 6f    /* Array of co
62b90 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
62ba0 20 6e 61 6d 65 73 20 66 6f 72 20 69 6e 64 65 78   names for index
62bb0 20 2a 2f 0a 20 20 49 6e 64 65 78 53 61 6d 70 6c   */.  IndexSampl
62bc0 65 20 2a 61 53 61 6d 70 6c 65 3b 20 20 20 20 2f  e *aSample;    /
62bd0 2a 20 41 72 72 61 79 20 6f 66 20 53 51 4c 49 54  * Array of SQLIT
62be0 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20  E_INDEX_SAMPLES 
62bf0 73 61 6d 70 6c 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f  samples */.};../
62c00 2a 0a 2a 2a 20 45 61 63 68 20 73 61 6d 70 6c 65  *.** Each sample
62c10 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73   stored in the s
62c20 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c  qlite_stat2 tabl
62c30 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  e is represented
62c40 20 69 6e 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 75   in memory .** u
62c50 73 69 6e 67 20 61 20 73 74 72 75 63 74 75 72 65  sing a structure
62c60 20 6f 66 20 74 68 69 73 20 74 79 70 65 2e 0a 2a   of this type..*
62c70 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 78 53 61  /.struct IndexSa
62c80 6d 70 6c 65 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b  mple {.  union {
62c90 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20  .    char *z;   
62ca0 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 66       /* Value if
62cb0 20 65 54 79 70 65 20 69 73 20 53 51 4c 49 54 45   eType is SQLITE
62cc0 5f 54 45 58 54 20 6f 72 20 53 51 4c 49 54 45 5f  _TEXT or SQLITE_
62cd0 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 64 6f 75 62  BLOB */.    doub
62ce0 6c 65 20 72 3b 20 20 20 20 20 20 20 2f 2a 20 56  le r;       /* V
62cf0 61 6c 75 65 20 69 66 20 65 54 79 70 65 20 69 73  alue if eType is
62d00 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 6f 72   SQLITE_FLOAT or
62d10 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20   SQLITE_INTEGER 
62d20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 38 20 65  */.  } u;.  u8 e
62d30 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Type;         /*
62d40 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51   SQLITE_NULL, SQ
62d50 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 2e 2e 2e  LITE_INTEGER ...
62d60 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 6e 42   etc. */.  u8 nB
62d70 79 74 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  yte;         /* 
62d80 53 69 7a 65 20 69 6e 20 62 79 74 65 20 6f 66 20  Size in byte of 
62d90 74 65 78 74 20 6f 72 20 62 6c 6f 62 2e 20 2a 2f  text or blob. */
62da0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  .};../*.** Each 
62db0 74 6f 6b 65 6e 20 63 6f 6d 69 6e 67 20 6f 75 74  token coming out
62dc0 20 6f 66 20 74 68 65 20 6c 65 78 65 72 20 69 73   of the lexer is
62dd0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a   an instance of.
62de0 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ** this structur
62df0 65 2e 20 20 54 6f 6b 65 6e 73 20 61 72 65 20 61  e.  Tokens are a
62e00 6c 73 6f 20 75 73 65 64 20 61 73 20 70 61 72 74  lso used as part
62e10 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f   of an expressio
62e20 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66  n..**.** Note if
62e30 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e   Token.z==0 then
62e40 20 54 6f 6b 65 6e 2e 64 79 6e 20 61 6e 64 20 54   Token.dyn and T
62e50 6f 6b 65 6e 2e 6e 20 61 72 65 20 75 6e 64 65 66  oken.n are undef
62e60 69 6e 65 64 20 61 6e 64 0a 2a 2a 20 6d 61 79 20  ined and.** may 
62e70 63 6f 6e 74 61 69 6e 20 72 61 6e 64 6f 6d 20 76  contain random v
62e80 61 6c 75 65 73 2e 20 20 44 6f 20 6e 6f 74 20 6d  alues.  Do not m
62e90 61 6b 65 20 61 6e 79 20 61 73 73 75 6d 70 74 69  ake any assumpti
62ea0 6f 6e 73 20 61 62 6f 75 74 20 54 6f 6b 65 6e 2e  ons about Token.
62eb0 64 79 6e 0a 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e  dyn.** and Token
62ec0 2e 6e 20 77 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d  .n when Token.z=
62ed0 3d 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 6f  =0..*/.struct To
62ee0 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68  ken {.  const ch
62ef0 61 72 20 2a 7a 3b 20 20 20 20 20 2f 2a 20 54 65  ar *z;     /* Te
62f00 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e  xt of the token.
62f10 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69    Not NULL-termi
62f20 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69  nated! */.  unsi
62f30 67 6e 65 64 20 69 6e 74 20 6e 3b 20 20 20 20 2f  gned int n;    /
62f40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  * Number of char
62f50 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74  acters in this t
62f60 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  oken */.};../*.*
62f70 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
62f80 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
62f90 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
62fa0 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67  tion needed to g
62fb0 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20  enerate.** code 
62fc0 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61  for a SELECT tha
62fd0 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65  t contains aggre
62fe0 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  gate functions..
62ff0 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70  **.** If Expr.op
63000 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20  ==TK_AGG_COLUMN 
63010 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  or TK_AGG_FUNCTI
63020 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67  ON then Expr.pAg
63030 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f  gInfo is a.** po
63040 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74  inter to this st
63050 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78  ructure.  The Ex
63060 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64  pr.iColumn field
63070 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e   is the index in
63080 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c  .** AggInfo.aCol
63090 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46  [] or AggInfo.aF
630a0 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61  unc[] of informa
630b0 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67  tion needed to g
630c0 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20  enerate.** code 
630d0 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a  for that node..*
630e0 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72  *.** AggInfo.pGr
630f0 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66  oupBy and AggInf
63100 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f  o.aFunc.pExpr po
63110 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69  int to fields wi
63120 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67  thin the.** orig
63130 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75  inal Select stru
63140 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72  cture that descr
63150 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20  ibes the SELECT 
63160 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73  statement.  Thes
63170 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e  e.** fields do n
63180 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72  ot need to be fr
63190 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63  eed when dealloc
631a0 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66  ating the AggInf
631b0 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  o structure..*/.
631c0 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b  struct AggInfo {
631d0 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65  .  u8 directMode
631e0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69  ;          /* Di
631f0 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d  rect rendering m
63200 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64  ode means take d
63210 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20  ata directly.   
63220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63230 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73         ** from s
63240 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74  ource tables rat
63250 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63  her than from ac
63260 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20  cumulators */.  
63270 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78  u8 useSortingIdx
63280 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69  ;       /* In di
63290 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72  rect mode, refer
632a0 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67  ence the sorting
632b0 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20   index rather.  
632c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
632d0 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20          ** than 
632e0 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65  the source table
632f0 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e   */.  int sortin
63300 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a  gIdx;         /*
63310 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   Cursor number o
63320 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e  f the sorting in
63330 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  dex */.  ExprLis
63340 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20  t *pGroupBy;    
63350 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79   /* The group by
63360 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
63370 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b   nSortingColumn;
63380 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
63390 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
633a0 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a   sorting index *
633b0 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e  /.  struct AggIn
633c0 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46  fo_col {    /* F
633d0 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75  or each column u
633e0 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61  sed in source ta
633f0 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c  bles */.    Tabl
63400 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
63410 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74       /* Source t
63420 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  able */.    int 
63430 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  iTable;         
63440 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
63450 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75  umber of the sou
63460 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  rce table */.   
63470 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   int iColumn;   
63480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
63490 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69  umn number withi
634a0 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62  n the source tab
634b0 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53  le */.    int iS
634c0 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20  orterColumn;    
634d0 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d     /* Column num
634e0 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69  ber in the sorti
634f0 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20  ng index */.    
63500 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20  int iMem;       
63510 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f           /* Memo
63520 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74  ry location that
63530 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c   acts as accumul
63540 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72  ator */.    Expr
63550 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20   *pExpr;        
63560 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67       /* The orig
63570 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20  inal expression 
63580 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20  */.  } *aCol;.  
63590 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
635a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
635b0 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65  r of used entrie
635c0 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20  s in aCol[] */. 
635d0 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f   int nColumnAllo
635e0 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  c;       /* Numb
635f0 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f  er of slots allo
63600 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d  cated for aCol[]
63610 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d   */.  int nAccum
63620 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a  ulator;       /*
63630 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
63640 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72  ns that show thr
63650 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70  ough to the outp
63660 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  ut..            
63670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
63680 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75   Additional colu
63690 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c  mns are used onl
636a0 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20  y as parameters 
636b0 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  to.             
636c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
636d0 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
636e0 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ons */.  struct 
636f0 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20  AggInfo_func {  
63700 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67   /* For each agg
63710 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
63720 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78  */.    Expr *pEx
63730 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pr;             
63740 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e  /* Expression en
63750 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74  coding the funct
63760 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44  ion */.    FuncD
63770 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20  ef *pFunc;      
63780 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65      /* The aggre
63790 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d  gate function im
637a0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  plementation */.
637b0 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20      int iMem;   
637c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
637d0 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  Memory location 
637e0 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63  that acts as acc
637f0 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20  umulator */.    
63800 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20  int iDistinct;  
63810 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65           /* Ephe
63820 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64  meral table used
63830 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54   to enforce DIST
63840 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75  INCT */.  } *aFu
63850 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b  nc;.  int nFunc;
63860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
63870 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
63880 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f  es in aFunc[] */
63890 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f  .  int nFuncAllo
638a0 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  c;         /* Nu
638b0 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c  mber of slots al
638c0 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e  located for aFun
638d0 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  c[] */.};../*.**
638e0 20 54 68 65 20 64 61 74 61 74 79 70 65 20 79 6e   The datatype yn
638f0 56 61 72 20 69 73 20 61 20 73 69 67 6e 65 64 20  Var is a signed 
63900 69 6e 74 65 67 65 72 2c 20 65 69 74 68 65 72 20  integer, either 
63910 31 36 2d 62 69 74 20 6f 72 20 33 32 2d 62 69 74  16-bit or 32-bit
63920 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 69 74 20  ..** Usually it 
63930 69 73 20 31 36 2d 62 69 74 73 2e 20 20 42 75 74  is 16-bits.  But
63940 20 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56   if SQLITE_MAX_V
63950 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 69  ARIABLE_NUMBER i
63960 73 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61  s greater.** tha
63970 6e 20 33 32 37 36 37 20 77 65 20 68 61 76 65 20  n 32767 we have 
63980 74 6f 20 6d 61 6b 65 20 69 74 20 33 32 2d 62 69  to make it 32-bi
63990 74 2e 20 20 31 36 2d 62 69 74 20 69 73 20 70 72  t.  16-bit is pr
639a0 65 66 65 72 72 65 64 20 62 65 63 61 75 73 65 0a  eferred because.
639b0 2a 2a 20 69 74 20 75 73 65 73 20 6c 65 73 73 20  ** it uses less 
639c0 6d 65 6d 6f 72 79 20 69 6e 20 74 68 65 20 45 78  memory in the Ex
639d0 70 72 20 6f 62 6a 65 63 74 2c 20 77 68 69 63 68  pr object, which
639e0 20 69 73 20 61 20 62 69 67 20 6d 65 6d 6f 72 79   is a big memory
639f0 20 75 73 65 72 0a 2a 2a 20 69 6e 20 73 79 73 74   user.** in syst
63a00 65 6d 73 20 77 69 74 68 20 6c 6f 74 73 20 6f 66  ems with lots of
63a10 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
63a20 65 6e 74 73 2e 20 20 41 6e 64 20 66 65 77 20 61  ents.  And few a
63a30 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 6e  pplications.** n
63a40 65 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 61 62  eed more than ab
63a50 6f 75 74 20 31 30 20 6f 72 20 32 30 20 76 61 72  out 10 or 20 var
63a60 69 61 62 6c 65 73 2e 20 20 42 75 74 20 73 6f 6d  iables.  But som
63a70 65 20 65 78 74 72 65 6d 65 20 75 73 65 72 73 20  e extreme users 
63a80 77 61 6e 74 0a 2a 2a 20 74 6f 20 68 61 76 65 20  want.** to have 
63a90 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
63aa0 6e 74 73 20 77 69 74 68 20 6f 76 65 72 20 33 32  nts with over 32
63ab0 37 36 37 20 76 61 72 69 61 62 6c 65 73 2c 20 61  767 variables, a
63ac0 6e 64 20 66 6f 72 20 74 68 65 6d 0a 2a 2a 20 74  nd for them.** t
63ad0 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 61 76 61  he option is ava
63ae0 69 6c 61 62 6c 65 20 28 61 74 20 63 6f 6d 70 69  ilable (at compi
63af0 6c 65 2d 74 69 6d 65 29 2e 0a 2a 2f 0a 23 69 66  le-time)..*/.#if
63b00 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49   SQLITE_MAX_VARI
63b10 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 3d 33 32 37  ABLE_NUMBER<=327
63b20 36 37 0a 74 79 70 65 64 65 66 20 69 36 34 20 79  67.typedef i64 y
63b30 6e 56 61 72 3b 0a 23 65 6c 73 65 0a 74 79 70 65  nVar;.#else.type
63b40 64 65 66 20 69 6e 74 20 79 6e 56 61 72 3b 0a 23  def int ynVar;.#
63b50 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  endif../*.** Eac
63b60 68 20 6e 6f 64 65 20 6f 66 20 61 6e 20 65 78 70  h node of an exp
63b70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 70  ression in the p
63b80 61 72 73 65 20 74 72 65 65 20 69 73 20 61 6e 20  arse tree is an 
63b90 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74  instance.** of t
63ba0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  his structure..*
63bb0 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 20 69 73 20  *.** Expr.op is 
63bc0 74 68 65 20 6f 70 63 6f 64 65 2e 20 54 68 65 20  the opcode. The 
63bd0 69 6e 74 65 67 65 72 20 70 61 72 73 65 72 20 74  integer parser t
63be0 6f 6b 65 6e 20 63 6f 64 65 73 20 61 72 65 20 72  oken codes are r
63bf0 65 75 73 65 64 0a 2a 2a 20 61 73 20 6f 70 63 6f  eused.** as opco
63c00 64 65 73 20 68 65 72 65 2e 20 46 6f 72 20 65 78  des here. For ex
63c10 61 6d 70 6c 65 2c 20 74 68 65 20 70 61 72 73 65  ample, the parse
63c20 72 20 64 65 66 69 6e 65 73 20 54 4b 5f 47 45 20  r defines TK_GE 
63c30 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72  to be an integer
63c40 0a 2a 2a 20 63 6f 64 65 20 72 65 70 72 65 73 65  .** code represe
63c50 6e 74 69 6e 67 20 74 68 65 20 22 3e 3d 22 20 6f  nting the ">=" o
63c60 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73 61  perator. This sa
63c70 6d 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20  me integer code 
63c80 69 73 20 72 65 75 73 65 64 0a 2a 2a 20 74 6f 20  is reused.** to 
63c90 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 67 72  represent the gr
63ca0 65 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d 65 71  eater-than-or-eq
63cb0 75 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f 72 20  ual-to operator 
63cc0 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
63cd0 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a  n.** tree..**.**
63ce0 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
63cf0 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 6c 69 74  on is an SQL lit
63d00 65 72 61 6c 20 28 54 4b 5f 49 4e 54 45 47 45 52  eral (TK_INTEGER
63d10 2c 20 54 4b 5f 46 4c 4f 41 54 2c 20 54 4b 5f 42  , TK_FLOAT, TK_B
63d20 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 20 54 4b 5f 53  LOB, .** or TK_S
63d30 54 52 49 4e 47 29 2c 20 74 68 65 6e 20 45 78 70  TRING), then Exp
63d40 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73  r.token contains
63d50 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65   the text of the
63d60 20 53 51 4c 20 6c 69 74 65 72 61 6c 2e 20 49 66   SQL literal. If
63d70 0a 2a 2a 20 74 68 65 20 65 78 70 72 65 73 73 69  .** the expressi
63d80 6f 6e 20 69 73 20 61 20 76 61 72 69 61 62 6c 65  on is a variable
63d90 20 28 54 4b 5f 56 41 52 49 41 42 4c 45 29 2c 20   (TK_VARIABLE), 
63da0 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20  then Expr.token 
63db0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 2a 2a  contains the .**
63dc0 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 2e 20   variable name. 
63dd0 46 69 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 20  Finally, if the 
63de0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e  expression is an
63df0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 28 54   SQL function (T
63e00 4b 5f 46 55 4e 43 54 49 4f 4e 29 2c 0a 2a 2a 20  K_FUNCTION),.** 
63e10 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20  then Expr.token 
63e20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d  contains the nam
63e30 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  e of the functio
63e40 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52  n..**.** Expr.pR
63e50 69 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c  ight and Expr.pL
63e60 65 66 74 20 61 72 65 20 74 68 65 20 6c 65 66 74  eft are the left
63e70 20 61 6e 64 20 72 69 67 68 74 20 73 75 62 65 78   and right subex
63e80 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 0a 2a  pressions of a.*
63e90 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74 6f  * binary operato
63ea0 72 2e 20 45 69 74 68 65 72 20 6f 72 20 62 6f 74  r. Either or bot
63eb0 68 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a  h may be NULL..*
63ec0 2a 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 4c 69 73  *.** Expr.x.pLis
63ed0 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 61  t is a list of a
63ee0 72 67 75 6d 65 6e 74 73 20 69 66 20 74 68 65 20  rguments if the 
63ef0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e  expression is an
63f00 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a   SQL function,.*
63f10 2a 20 61 20 43 41 53 45 20 65 78 70 72 65 73 73  * a CASE express
63f20 69 6f 6e 20 6f 72 20 61 6e 20 49 4e 20 65 78 70  ion or an IN exp
63f30 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66  ression of the f
63f40 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 3c  orm "<lhs> IN (<
63f50 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0a 2a 2a  y>, <z>...)"..**
63f60 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20   Expr.x.pSelect 
63f70 69 73 20 75 73 65 64 20 69 66 20 74 68 65 20 65  is used if the e
63f80 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73  xpression is a s
63f90 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 6e 20  ub-select or an 
63fa0 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 0a 2a 2a  expression of.**
63fb0 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e   the form "<lhs>
63fc0 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29   IN (SELECT ...)
63fd0 22 2e 20 49 66 20 74 68 65 20 45 50 5f 78 49 73  ". If the EP_xIs
63fe0 53 65 6c 65 63 74 20 62 69 74 20 69 73 20 73 65  Select bit is se
63ff0 74 20 69 6e 20 74 68 65 0a 2a 2a 20 45 78 70 72  t in the.** Expr
64000 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65  .flags mask, the
64010 6e 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74  n Expr.x.pSelect
64020 20 69 73 20 76 61 6c 69 64 2e 20 4f 74 68 65 72   is valid. Other
64030 77 69 73 65 2c 20 45 78 70 72 2e 78 2e 70 4c 69  wise, Expr.x.pLi
64040 73 74 20 69 73 20 0a 2a 2a 20 76 61 6c 69 64 2e  st is .** valid.
64050 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73  .**.** An expres
64060 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d  sion of the form
64070 20 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66   ID or ID.ID ref
64080 65 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20  ers to a column 
64090 69 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46  in a table..** F
640a0 6f 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69  or such expressi
640b0 6f 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20  ons, Expr.op is 
640c0 73 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e  set to TK_COLUMN
640d0 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65   and Expr.iTable
640e0 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67   is.** the integ
640f0 65 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  er cursor number
64100 20 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f   of a VDBE curso
64110 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  r pointing to th
64120 61 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20  at table and.** 
64130 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20  Expr.iColumn is 
64140 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  the column numbe
64150 72 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66  r for the specif
64160 69 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74  ic column.  If t
64170 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  he.** expression
64180 20 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65   is used as a re
64190 73 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65  sult in an aggre
641a0 67 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65  gate SELECT, the
641b0 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69  n the.** value i
641c0 73 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e  s also stored in
641d0 20 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63   the Expr.iAgg c
641e0 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67  olumn in the agg
641f0 72 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a  regate so that.*
64200 2a 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65  * it can be acce
64210 73 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61  ssed after all a
64220 67 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f  ggregates are co
64230 6d 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  mputed..**.** If
64240 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
64250 69 73 20 61 6e 20 75 6e 62 6f 75 6e 64 20 76 61  is an unbound va
64260 72 69 61 62 6c 65 20 6d 61 72 6b 65 72 20 28 61  riable marker (a
64270 20 71 75 65 73 74 69 6f 6e 20 6d 61 72 6b 20 0a   question mark .
64280 2a 2a 20 63 68 61 72 61 63 74 65 72 20 27 3f 27  ** character '?'
64290 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   in the original
642a0 20 53 51 4c 29 20 74 68 65 6e 20 74 68 65 20 45   SQL) then the E
642b0 78 70 72 2e 69 54 61 62 6c 65 20 68 6f 6c 64 73  xpr.iTable holds
642c0 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6e   the index .** n
642d0 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20 76  umber for that v
642e0 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  ariable..**.** I
642f0 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
64300 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20 74   is a subquery t
64310 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e  hen Expr.iColumn
64320 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65   holds an intege
64330 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 6e 75  r.** register nu
64340 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  mber containing 
64350 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
64360 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 66 20  e subquery.  If 
64370 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79 20  the.** subquery 
64380 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61 6e 74  gives a constant
64390 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 69 54   result, then iT
643a0 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 66 20  able is -1.  If 
643b0 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a 2a 20  the subquery.** 
643c0 67 69 76 65 73 20 61 20 64 69 66 66 65 72 65 6e  gives a differen
643d0 74 20 61 6e 73 77 65 72 20 61 74 20 64 69 66 66  t answer at diff
643e0 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75 72 69  erent times duri
643f0 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 72 6f  ng statement pro
64400 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 6e 20  cessing.** then 
64410 69 54 61 62 6c 65 20 69 73 20 74 68 65 20 61 64  iTable is the ad
64420 64 72 65 73 73 20 6f 66 20 61 20 73 75 62 72 6f  dress of a subro
64430 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d 70 75  utine that compu
64440 74 65 73 20 74 68 65 20 73 75 62 71 75 65 72 79  tes the subquery
64450 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45  ..**.** If the E
64460 78 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f  xpr is of type O
64470 50 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68  P_Column, and th
64480 65 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65  e table it is se
64490 6c 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20  lecting from.** 
644a0 69 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20  is a disk table 
644b0 6f 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70  or the "old.*" p
644c0 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65  seudo-table, the
644d0 6e 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f  n pTab points to
644e0 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f   the.** correspo
644f0 6e 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69  nding table defi
64500 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 4c  nition..**.** AL
64510 4c 4f 43 41 54 49 4f 4e 20 4e 4f 54 45 53 3a 0a  LOCATION NOTES:.
64520 2a 2a 0a 2a 2a 20 45 78 70 72 20 6f 62 6a 65 63  **.** Expr objec
64530 74 73 20 63 61 6e 20 75 73 65 20 61 20 6c 6f 74  ts can use a lot
64540 20 6f 66 20 6d 65 6d 6f 72 79 20 73 70 61 63 65   of memory space
64550 20 69 6e 20 64 61 74 61 62 61 73 65 20 73 63 68   in database sch
64560 65 6d 61 2e 20 20 54 6f 0a 2a 2a 20 68 65 6c 70  ema.  To.** help
64570 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 20 72   reduce memory r
64580 65 71 75 69 72 65 6d 65 6e 74 73 2c 20 73 6f 6d  equirements, som
64590 65 74 69 6d 65 73 20 61 6e 20 45 78 70 72 20 6f  etimes an Expr o
645a0 62 6a 65 63 74 20 77 69 6c 6c 20 62 65 0a 2a 2a  bject will be.**
645b0 20 74 72 75 6e 63 61 74 65 64 2e 20 20 41 6e 64   truncated.  And
645c0 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 6e   to reduce the n
645d0 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20  umber of memory 
645e0 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 73 6f 6d  allocations, som
645f0 65 74 69 6d 65 73 0a 2a 2a 20 74 77 6f 20 6f 72  etimes.** two or
64600 20 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a 65 63   more Expr objec
64610 74 73 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65  ts will be store
64620 64 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6d 65  d in a single me
64630 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c  mory allocation,
64640 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 69 74  .** together wit
64650 68 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 73 74  h Expr.zToken st
64660 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rings..**.** If 
64670 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 61  the EP_Reduced a
64680 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20  nd EP_TokenOnly 
64690 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 77 68  flags are set wh
646a0 65 6e 0a 2a 2a 20 61 6e 20 45 78 70 72 20 6f 62  en.** an Expr ob
646b0 6a 65 63 74 20 69 73 20 74 72 75 6e 63 61 74 65  ject is truncate
646c0 64 2e 20 20 57 68 65 6e 20 45 50 5f 52 65 64 75  d.  When EP_Redu
646d0 63 65 64 20 69 73 20 73 65 74 2c 20 74 68 65 6e  ced is set, then
646e0 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 63 68 69 6c   all.** the chil
646f0 64 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 69  d Expr objects i
64700 6e 20 74 68 65 20 45 78 70 72 2e 70 4c 65 66 74  n the Expr.pLeft
64710 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74   and Expr.pRight
64720 20 73 75 62 74 72 65 65 73 0a 2a 2a 20 61 72 65   subtrees.** are
64730 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
64740 6e 20 74 68 65 20 73 61 6d 65 20 6d 65 6d 6f 72  n the same memor
64750 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e  y allocation.  N
64760 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 68  ote, however, th
64770 61 74 0a 2a 2a 20 74 68 65 20 73 75 62 74 72 65  at.** the subtre
64780 65 73 20 69 6e 20 45 78 70 72 2e 78 2e 70 4c 69  es in Expr.x.pLi
64790 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 53 65  st or Expr.x.pSe
647a0 6c 65 63 74 20 61 72 65 20 61 6c 77 61 79 73 20  lect are always 
647b0 73 65 70 61 72 61 74 65 6c 79 0a 2a 2a 20 61 6c  separately.** al
647c0 6c 6f 63 61 74 65 64 2c 20 72 65 67 61 72 64 6c  located, regardl
647d0 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f  ess of whether o
647e0 72 20 6e 6f 74 20 45 50 5f 52 65 64 75 63 65 64  r not EP_Reduced
647f0 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 72 75   is set..*/.stru
64800 63 74 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f  ct Expr {.  u8 o
64810 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
64820 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20     /* Operation 
64830 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69  performed by thi
64840 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72  s node */.  char
64850 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20   affinity;      
64860 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69     /* The affini
64870 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ty of the column
64880 20 6f 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63   or 0 if not a c
64890 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66  olumn */.  u16 f
648a0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
648b0 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61    /* Various fla
648c0 67 73 2e 20 20 45 50 5f 2a 20 53 65 65 20 62 65  gs.  EP_* See be
648d0 6c 6f 77 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b  low */.  union {
648e0 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65  .    char *zToke
648f0 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  n;          /* T
64900 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 5a 65 72 6f  oken value. Zero
64910 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20   terminated and 
64920 64 65 71 75 6f 74 65 64 20 2a 2f 0a 20 20 20 20  dequoted */.    
64930 69 6e 74 20 69 56 61 6c 75 65 3b 20 20 20 20 20  int iValue;     
64940 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65         /* Intege
64950 72 20 76 61 6c 75 65 20 69 66 20 45 50 5f 49 6e  r value if EP_In
64960 74 56 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75 3b  tValue */.  } u;
64970 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50  ..  /* If the EP
64980 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 20  _TokenOnly flag 
64990 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78  is set in the Ex
649a0 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74  pr.flags mask, t
649b0 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63  hen no.  ** spac
649c0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  e is allocated f
649d0 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65  or the fields be
649e0 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20  low this point. 
649f0 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20  An attempt to.  
64a00 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77  ** access them w
64a10 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20  ill result in a 
64a20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66  segfault or malf
64a30 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a  unction. .  ****
64a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2f 0a 0a 20 20 45 78 70 72 20 2a 70 4c 65 66  */..  Expr *pLef
64a90 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
64aa0 4c 65 66 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a  Left subnode */.
64ab0 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20    Expr *pRight; 
64ac0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68           /* Righ
64ad0 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 75  t subnode */.  u
64ae0 6e 69 6f 6e 20 7b 0a 20 20 20 20 45 78 70 72 4c  nion {.    ExprL
64af0 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 20  ist *pList;     
64b00 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 61 72 67 75  /* Function argu
64b10 6d 65 6e 74 73 20 6f 72 20 69 6e 20 22 3c 65 78  ments or in "<ex
64b20 70 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69  pr> IN (<expr-li
64b30 73 74 29 22 20 2a 2f 0a 20 20 20 20 53 65 6c 65  st)" */.    Sele
64b40 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20  ct *pSelect;    
64b50 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 75 62   /* Used for sub
64b60 2d 73 65 6c 65 63 74 73 20 61 6e 64 20 22 3c 65  -selects and "<e
64b70 78 70 72 3e 20 49 4e 20 28 3c 73 65 6c 65 63 74  xpr> IN (<select
64b80 3e 29 22 20 2a 2f 0a 20 20 7d 20 78 3b 0a 20 20  >)" */.  } x;.  
64b90 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20  CollSeq *pColl; 
64ba0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
64bb0 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f 66 20  llation type of 
64bc0 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20  the column or 0 
64bd0 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  */..  /* If the 
64be0 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20  EP_Reduced flag 
64bf0 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78  is set in the Ex
64c00 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74  pr.flags mask, t
64c10 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63  hen no.  ** spac
64c20 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  e is allocated f
64c30 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65  or the fields be
64c40 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20  low this point. 
64c50 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20  An attempt to.  
64c60 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77  ** access them w
64c70 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20  ill result in a 
64c80 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66  segfault or malf
64c90 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a 2a  unction..  *****
64ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2f 0a 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 3b  /..  int iTable;
64cf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
64d00 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73 6f 72  K_COLUMN: cursor
64d10 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65   number of table
64d20 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e 0a   holding column.
64d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64d40 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 52           ** TK_R
64d50 45 47 49 53 54 45 52 3a 20 72 65 67 69 73 74 65  EGISTER: registe
64d60 72 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 20  r number.       
64d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64d80 20 20 2a 2a 20 54 4b 5f 54 52 49 47 47 45 52 3a    ** TK_TRIGGER:
64d90 20 31 20 2d 3e 20 6e 65 77 2c 20 30 20 2d 3e 20   1 -> new, 0 -> 
64da0 6f 6c 64 20 2a 2f 0a 20 20 79 6e 56 61 72 20 69  old */.  ynVar i
64db0 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
64dc0 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 6f  /* TK_COLUMN: co
64dd0 6c 75 6d 6e 20 69 6e 64 65 78 2e 20 20 2d 31 20  lumn index.  -1 
64de0 66 6f 72 20 72 6f 77 69 64 2e 0a 20 20 20 20 20  for rowid..     
64df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64e00 20 20 20 20 2a 2a 20 54 4b 5f 56 41 52 49 41 42      ** TK_VARIAB
64e10 4c 45 3a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d  LE: variable num
64e20 62 65 72 20 28 61 6c 77 61 79 73 20 3e 3d 20 31  ber (always >= 1
64e30 29 2e 20 2a 2f 0a 20 20 69 31 36 20 69 41 67 67  ). */.  i16 iAgg
64e40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
64e50 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e  * Which entry in
64e60 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b   pAggInfo->aCol[
64e70 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a  ] or ->aFunc[] *
64e80 2f 0a 20 20 69 31 36 20 69 52 69 67 68 74 4a 6f  /.  i16 iRightJo
64e90 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66  inTable;   /* If
64ea0 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68   EP_FromJoin, th
64eb0 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66  e right table of
64ec0 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 75   the join */.  u
64ed0 38 20 66 6c 61 67 73 32 3b 20 20 20 20 20 20 20  8 flags2;       
64ee0 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20        /* Second 
64ef0 73 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 20 45  set of flags.  E
64f00 50 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 20 6f  P2_... */.  u8 o
64f10 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p2;             
64f20 20 20 20 2f 2a 20 49 66 20 61 20 54 4b 5f 52 45     /* If a TK_RE
64f30 47 49 53 54 45 52 2c 20 74 68 65 20 6f 72 69 67  GISTER, the orig
64f40 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 45 78  inal value of Ex
64f50 70 72 2e 6f 70 20 2a 2f 0a 20 20 41 67 67 49 6e  pr.op */.  AggIn
64f60 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20  fo *pAggInfo;   
64f70 20 20 2f 2a 20 55 73 65 64 20 62 79 20 54 4b 5f    /* Used by TK_
64f80 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 54  AGG_COLUMN and T
64f90 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 2a  K_AGG_FUNCTION *
64fa0 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
64fb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
64fc0 62 6c 65 20 66 6f 72 20 54 4b 5f 43 4f 4c 55 4d  ble for TK_COLUM
64fd0 4e 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 2a  N expressions. *
64fe0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  /.#if SQLITE_MAX
64ff0 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20  _EXPR_DEPTH>0.  
65000 69 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20 20  int nHeight;    
65010 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74         /* Height
65020 20 6f 66 20 74 68 65 20 74 72 65 65 20 68 65 61   of the tree hea
65030 64 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65  ded by this node
65040 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
65050 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
65060 6e 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69  ng are the meani
65070 6e 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74  ngs of bits in t
65080 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69  he Expr.flags fi
65090 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  eld..*/.#define 
650a0 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 30 78  EP_FromJoin   0x
650b0 30 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 6e 61  0001  /* Origina
650c0 74 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 53 49  ted in ON or USI
650d0 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a  NG clause of a j
650e0 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  oin */.#define E
650f0 50 5f 41 67 67 20 20 20 20 20 20 20 20 30 78 30  P_Agg        0x0
65100 30 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73  002  /* Contains
65110 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 67 67   one or more agg
65120 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
65130 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 52   */.#define EP_R
65140 65 73 6f 6c 76 65 64 20 20 20 30 78 30 30 30 34  esolved   0x0004
65150 20 20 2f 2a 20 49 44 73 20 68 61 76 65 20 62 65    /* IDs have be
65160 65 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 43  en resolved to C
65170 4f 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 69 6e  OLUMNs */.#defin
65180 65 20 45 50 5f 45 72 72 6f 72 20 20 20 20 20 20  e EP_Error      
65190 30 78 30 30 30 38 20 20 2f 2a 20 45 78 70 72 65  0x0008  /* Expre
651a0 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 6f  ssion contains o
651b0 6e 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 6f 72  ne or more error
651c0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  s */.#define EP_
651d0 44 69 73 74 69 6e 63 74 20 20 20 30 78 30 30 31  Distinct   0x001
651e0 30 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20  0  /* Aggregate 
651f0 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 44 49  function with DI
65200 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 2a  STINCT keyword *
65210 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 61 72  /.#define EP_Var
65220 53 65 6c 65 63 74 20 20 30 78 30 30 32 30 20 20  Select  0x0020  
65230 2f 2a 20 70 53 65 6c 65 63 74 20 69 73 20 63 6f  /* pSelect is co
65240 72 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 63 6f  rrelated, not co
65250 6e 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  nstant */.#defin
65260 65 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 20  e EP_DblQuoted  
65270 30 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 65 6e  0x0040  /* token
65280 2e 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c  .z was originall
65290 79 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 23 64  y in "..." */.#d
652a0 65 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75  efine EP_InfixFu
652b0 6e 63 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54  nc  0x0080  /* T
652c0 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78  rue for an infix
652d0 20 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c   function: LIKE,
652e0 20 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64   GLOB, etc */.#d
652f0 65 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c  efine EP_ExpColl
65300 61 74 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43  ate 0x0100  /* C
65310 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
65320 65 20 73 70 65 63 69 66 69 65 64 20 65 78 70 6c  e specified expl
65330 69 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  icitly */.#defin
65340 65 20 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20  e EP_AnyAff     
65350 30 78 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74  0x0200  /* Can t
65360 61 6b 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c  ake a cached col
65370 75 6d 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e  umn of any affin
65380 69 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ity */.#define E
65390 50 5f 46 69 78 65 64 44 65 73 74 20 20 30 78 30  P_FixedDest  0x0
653a0 34 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e  400  /* Result n
653b0 65 65 64 65 64 20 69 6e 20 61 20 73 70 65 63 69  eeded in a speci
653c0 66 69 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  fic register */.
653d0 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61  #define EP_IntVa
653e0 6c 75 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a  lue   0x0800  /*
653f0 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63   Integer value c
65400 6f 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e 69 56  ontained in u.iV
65410 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  alue */.#define 
65420 45 50 5f 78 49 73 53 65 6c 65 63 74 20 20 30 78  EP_xIsSelect  0x
65430 31 30 30 30 20 20 2f 2a 20 78 2e 70 53 65 6c 65  1000  /* x.pSele
65440 63 74 20 69 73 20 76 61 6c 69 64 20 28 6f 74 68  ct is valid (oth
65450 65 72 77 69 73 65 20 78 2e 70 4c 69 73 74 20 69  erwise x.pList i
65460 73 29 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 45  s) */..#define E
65470 50 5f 52 65 64 75 63 65 64 20 20 20 20 30 78 32  P_Reduced    0x2
65480 30 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72  000  /* Expr str
65490 75 63 74 20 69 73 20 45 58 50 52 5f 52 45 44 55  uct is EXPR_REDU
654a0 43 45 44 53 49 5a 45 20 62 79 74 65 73 20 6f 6e  CEDSIZE bytes on
654b0 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  ly */.#define EP
654c0 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 34 30  _TokenOnly  0x40
654d0 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75  00  /* Expr stru
654e0 63 74 20 69 73 20 45 58 50 52 5f 54 4f 4b 45 4e  ct is EXPR_TOKEN
654f0 4f 4e 4c 59 53 49 5a 45 20 62 79 74 65 73 20 6f  ONLYSIZE bytes o
65500 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  nly */.#define E
65510 50 5f 53 74 61 74 69 63 20 20 20 20 20 30 78 38  P_Static     0x8
65520 30 30 30 20 20 2f 2a 20 48 65 6c 64 20 69 6e 20  000  /* Held in 
65530 6d 65 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 61 69  memory not obtai
65540 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28  ned from malloc(
65550 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ) */../*.** The 
65560 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68  following are th
65570 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69  e meanings of bi
65580 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66  ts in the Expr.f
65590 6c 61 67 73 32 20 66 69 65 6c 64 2e 0a 2a 2f 0a  lags2 field..*/.
655a0 23 64 65 66 69 6e 65 20 45 50 32 5f 4d 61 6c 6c  #define EP2_Mall
655b0 6f 63 65 64 54 6f 6b 65 6e 20 20 30 78 30 30 30  ocedToken  0x000
655c0 31 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 73 71  1  /* Need to sq
655d0 6c 69 74 65 33 44 62 46 72 65 65 28 29 20 45 78  lite3DbFree() Ex
655e0 70 72 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 64 65  pr.zToken */.#de
655f0 66 69 6e 65 20 45 50 32 5f 49 72 72 65 64 75 63  fine EP2_Irreduc
65600 69 62 6c 65 20 20 20 20 30 78 30 30 30 32 20 20  ible    0x0002  
65610 2f 2a 20 43 61 6e 6e 6f 74 20 45 58 50 52 44 55  /* Cannot EXPRDU
65620 50 5f 52 45 44 55 43 45 20 74 68 69 73 20 45 78  P_REDUCE this Ex
65630 70 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  pr */../*.** The
65640 20 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65 20   pseudo-routine 
65650 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 49 72  sqlite3ExprSetIr
65660 72 65 64 75 63 69 62 6c 65 20 73 65 74 73 20 74  reducible sets t
65670 68 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 62  he EP2_Irreducib
65680 6c 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61 6e  le.** flag on an
65690 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75   expression stru
656a0 63 74 75 72 65 2e 20 20 54 68 69 73 20 66 6c 61  cture.  This fla
656b0 67 20 69 73 20 75 73 65 64 20 66 6f 72 20 56 56  g is used for VV
656c0 26 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0a 2a 2a  &A only.  The.**
656d0 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6d 70 6c   routine is impl
656e0 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61 63  emented as a mac
656f0 72 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f 72  ro that only wor
65700 6b 73 20 77 68 65 6e 20 69 6e 20 64 65 62 75 67  ks when in debug
65710 67 69 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 73 6f  ging mode,.** so
65720 20 61 73 20 6e 6f 74 20 74 6f 20 62 75 72 64 65   as not to burde
65730 6e 20 70 72 6f 64 75 63 74 69 6f 6e 20 63 6f 64  n production cod
65740 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
65750 49 54 45 5f 44 45 42 55 47 0a 23 20 64 65 66 69  ITE_DEBUG.# defi
65760 6e 65 20 45 78 70 72 53 65 74 49 72 72 65 64 75  ne ExprSetIrredu
65770 63 69 62 6c 65 28 58 29 20 20 28 58 29 2d 3e 66  cible(X)  (X)->f
65780 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 49 72 72  lags2 |= EP2_Irr
65790 65 64 75 63 69 62 6c 65 0a 23 65 6c 73 65 0a 23  educible.#else.#
657a0 20 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 49   define ExprSetI
657b0 72 72 65 64 75 63 69 62 6c 65 28 58 29 0a 23 65  rreducible(X).#e
657c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73  ndif../*.** Thes
657d0 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20  e macros can be 
657e0 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65  used to test, se
657f0 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73  t, or clear bits
65800 20 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72   in the .** Expr
65810 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f  .flags field..*/
65820 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48 61 73  .#define ExprHas
65830 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20  Property(E,P)   
65840 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28    (((E)->flags&(
65850 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e  P))==(P)).#defin
65860 65 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70  e ExprHasAnyProp
65870 65 72 74 79 28 45 2c 50 29 20 20 28 28 28 45 29  erty(E,P)  (((E)
65880 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29  ->flags&(P))!=0)
65890 0a 23 64 65 66 69 6e 65 20 45 78 70 72 53 65 74  .#define ExprSet
658a0 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20  Property(E,P)   
658b0 20 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50    (E)->flags|=(P
658c0 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c  ).#define ExprCl
658d0 65 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29  earProperty(E,P)
658e0 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e     (E)->flags&=~
658f0 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  (P)../*.** Macro
65900 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  s to determine t
65910 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
65920 65 73 20 72 65 71 75 69 72 65 64 20 62 79 20 61  es required by a
65930 20 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a 2a   normal Expr .**
65940 20 73 74 72 75 63 74 2c 20 61 6e 20 45 78 70 72   struct, an Expr
65950 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68 65   struct with the
65960 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67   EP_Reduced flag
65970 20 73 65 74 20 69 6e 20 45 78 70 72 2e 66 6c 61   set in Expr.fla
65980 67 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 78  gs .** and an Ex
65990 70 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74  pr struct with t
659a0 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20  he EP_TokenOnly 
659b0 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 65  flag set..*/.#de
659c0 66 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53 49  fine EXPR_FULLSI
659d0 5a 45 20 20 20 20 20 20 20 20 20 20 20 73 69 7a  ZE           siz
659e0 65 6f 66 28 45 78 70 72 29 20 20 20 20 20 20 20  eof(Expr)       
659f0 20 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a 65      /* Full size
65a00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52   */.#define EXPR
65a10 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 20  _REDUCEDSIZE    
65a20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70      offsetof(Exp
65a30 72 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 43 6f  r,iTable)  /* Co
65a40 6d 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 2a 2f  mmon features */
65a50 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 54 4f  .#define EXPR_TO
65a60 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 20 20  KENONLYSIZE     
65a70 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c 70   offsetof(Expr,p
65a80 4c 65 66 74 29 20 20 20 2f 2a 20 46 65 77 65 72  Left)   /* Fewer
65a90 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a 2f 2a   features */../*
65aa0 0a 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64  .** Flags passed
65ab0 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 45   to the sqlite3E
65ac0 78 70 72 44 75 70 28 29 20 66 75 6e 63 74 69 6f  xprDup() functio
65ad0 6e 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65  n. See the heade
65ae0 72 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62  r comment .** ab
65af0 6f 76 65 20 73 71 6c 69 74 65 33 45 78 70 72 44  ove sqlite3ExprD
65b00 75 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73  up() for details
65b10 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50  ..*/.#define EXP
65b20 52 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 20  RDUP_REDUCE     
65b30 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55      0x0001  /* U
65b40 73 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65  sed reduced-size
65b50 20 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a 0a   Expr nodes */..
65b60 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20  /*.** A list of 
65b70 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 45 61  expressions.  Ea
65b80 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61  ch expression ma
65b90 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61 76  y optionally hav
65ba0 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e  e a.** name.  An
65bb0 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62 69   expr/name combi
65bc0 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73  nation can be us
65bd0 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61  ed in several wa
65be0 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20 74  ys, such.** as t
65bf0 68 65 20 6c 69 73 74 20 6f 66 20 22 65 78 70 72  he list of "expr
65c00 20 41 53 20 49 44 22 20 66 69 65 6c 64 73 20 66   AS ID" fields f
65c10 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c 45  ollowing a "SELE
65c20 43 54 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a  CT" or in the.**
65c30 20 6c 69 73 74 20 6f 66 20 22 49 44 20 3d 20 65   list of "ID = e
65c40 78 70 72 22 20 69 74 65 6d 73 20 69 6e 20 61 6e  xpr" items in an
65c50 20 55 50 44 41 54 45 2e 20 20 41 20 6c 69 73 74   UPDATE.  A list
65c60 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20   of expressions 
65c70 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75  can.** also be u
65c80 73 65 64 20 61 73 20 74 68 65 20 61 72 67 75 6d  sed as the argum
65c90 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ent to a functio
65ca0 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  n, in which case
65cb0 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20   the a.zName.** 
65cc0 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 75 73 65  field is not use
65cd0 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70  d..*/.struct Exp
65ce0 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45  rList {.  int nE
65cf0 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
65d00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78   /* Number of ex
65d10 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68 65  pressions on the
65d20 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e   list */.  int n
65d30 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20  Alloc;          
65d40 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
65d50 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64  ntries allocated
65d60 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20   below */.  int 
65d70 69 45 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20  iECursor;       
65d80 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f     /* VDBE Curso
65d90 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  r associated wit
65da0 68 20 74 68 69 73 20 45 78 70 72 4c 69 73 74 20  h this ExprList 
65db0 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  */.  struct Expr
65dc0 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20  List_item {.    
65dd0 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20  Expr *pExpr;    
65de0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69         /* The li
65df0 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  st of expression
65e00 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  s */.    char *z
65e10 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
65e20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 69 61  /* Token associa
65e30 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65 78  ted with this ex
65e40 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20  pression */.    
65e50 63 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20 20 20  char *zSpan;    
65e60 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e         /* Origin
65e70 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 65  al text of the e
65e80 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20  xpression */.   
65e90 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b 20 20   u8 sortOrder;  
65ea0 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72          /* 1 for
65eb0 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72 20 41   DESC or 0 for A
65ec0 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f 6e  SC */.    u8 don
65ed0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
65ee0 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69 6e   /* A flag to in
65ef0 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f 63  dicate when proc
65f00 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68  essing is finish
65f10 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 43  ed */.    u16 iC
65f20 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
65f30 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 42 59   /* For ORDER BY
65f40 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  , column number 
65f50 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  in result set */
65f60 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61 73 3b  .    u16 iAlias;
65f70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
65f80 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73 65 2e  ndex into Parse.
65f90 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a 4e 61  aAlias[] for zNa
65fa0 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20 20  me */.  } *a;   
65fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
65fc0 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20  * One entry for 
65fd0 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20  each expression 
65fe0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
65ff0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
66000 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
66010 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
66020 20 74 6f 20 72 65 63 6f 72 64 20 62 6f 74 68 0a   to record both.
66030 2a 2a 20 74 68 65 20 70 61 72 73 65 20 74 72 65  ** the parse tre
66040 65 20 66 6f 72 20 61 6e 20 65 78 70 72 65 73 73  e for an express
66050 69 6f 6e 20 61 6e 64 20 74 68 65 20 73 70 61 6e  ion and the span
66060 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 66   of input text f
66070 6f 72 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73  or an.** express
66080 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45  ion..*/.struct E
66090 78 70 72 53 70 61 6e 20 7b 0a 20 20 45 78 70 72  xprSpan {.  Expr
660a0 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20   *pExpr;        
660b0 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
660c0 69 6f 6e 20 70 61 72 73 65 20 74 72 65 65 20 2a  ion parse tree *
660d0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
660e0 7a 53 74 61 72 74 3b 20 20 20 2f 2a 20 46 69 72  zStart;   /* Fir
660f0 73 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20  st character of 
66100 69 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 20 20  input text */.  
66110 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64  const char *zEnd
66120 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 68 61  ;     /* One cha
66130 72 61 63 74 65 72 20 70 61 73 74 20 74 68 65 20  racter past the 
66140 65 6e 64 20 6f 66 20 69 6e 70 75 74 20 74 65 78  end of input tex
66150 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  t */.};../*.** A
66160 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
66170 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  is structure can
66180 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c   hold a simple l
66190 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65  ist of identifie
661a0 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74  rs,.** such as t
661b0 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20  he list "a,b,c" 
661c0 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
661d0 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a   statements:.**.
661e0 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  **      INSERT I
661f0 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c  NTO t(a,b,c) VAL
66200 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20  UES ...;.**     
66210 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64   CREATE INDEX id
66220 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a  x ON t(a,b,c);.*
66230 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52  *      CREATE TR
66240 49 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52  IGGER trig BEFOR
66250 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c  E UPDATE ON t(a,
66260 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20  b,c) ...;.**.** 
66270 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78  The IdList.a.idx
66280 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77   field is used w
66290 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72  hen the IdList r
662a0 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69  epresents the li
662b0 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  st of.** column 
662c0 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61  names after a ta
662d0 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49  ble name in an I
662e0 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e  NSERT statement.
662f0 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65    In the stateme
66300 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53  nt.**.**     INS
66310 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63  ERT INTO t(a,b,c
66320 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22  ) ....**.** If "
66330 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63  a" is the k-th c
66340 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22  olumn of table "
66350 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e  t", then IdList.
66360 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a  a[0].idx==k..*/.
66370 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a  struct IdList {.
66380 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f    struct IdList_
66390 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20  item {.    char 
663a0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20  *zName;      /* 
663b0 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e  Name of the iden
663c0 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e  tifier */.    in
663d0 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  t idx;          
663e0 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65  /* Index in some
663f0 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66   Table.aCol[] of
66400 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20   a column named 
66410 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b  zName */.  } *a;
66420 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20  .  int nId;     
66430 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
66440 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20   identifiers on 
66450 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  the list */.  in
66460 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f  t nAlloc;      /
66470 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
66480 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ies allocated fo
66490 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d  r a[] below */.}
664a0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74  ;../*.** The bit
664b0 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65  mask datatype de
664c0 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75  fined below is u
664d0 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20  sed for various 
664e0 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a  optimizations..*
664f0 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68  *.** Changing th
66500 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74  is from a 64-bit
66510 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70   to a 32-bit typ
66520 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d  e limits the num
66530 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73  ber of.** tables
66540 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32   in a join to 32
66550 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20   instead of 64. 
66560 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64   But it also red
66570 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a  uces the size.**
66580 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   of the library 
66590 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20  by 738 bytes on 
665a0 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ix86..*/.typedef
665b0 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f   u64 Bitmask;../
665c0 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20  *.** The number 
665d0 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74  of bits in a Bit
665e0 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61  mask.  "BMS" mea
665f0 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65  ns "BitMask Size
66600 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d  "..*/.#define BM
66610 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66  S  ((int)(sizeof
66620 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f  (Bitmask)*8))../
66630 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
66640 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73  ng structure des
66650 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20  cribes the FROM 
66660 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
66670 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CT statement..**
66680 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73   Each table or s
66690 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46  ubquery in the F
666a0 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20  ROM clause is a 
666b0 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74  separate element
666c0 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69   of.** the SrcLi
666d0 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a  st.a[] array..**
666e0 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64  .** With the add
666f0 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c  ition of multipl
66700 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
66710 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  rt, the followin
66720 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63  g structure.** c
66730 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20  an also be used 
66740 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61  to describe a pa
66750 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73  rticular table s
66760 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65  uch as the table
66770 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69   that.** is modi
66780 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52  fied by an INSER
66790 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50  T, DELETE, or UP
667a0 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  DATE statement. 
667b0 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c   In standard SQL
667c0 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c  ,.** such a tabl
667d0 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70  e must be a simp
667e0 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75  le name: ID.  Bu
667f0 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65  t in SQLite, the
66800 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f   table can.** no
66810 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20  w be identified 
66820 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61  by a database na
66830 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20  me, a dot, then 
66840 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20  the table name: 
66850 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ID.ID..**.** The
66860 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73   jointype starts
66870 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65   out showing the
66880 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65   join type betwe
66890 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  en the current t
668a0 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20  able.** and the 
668b0 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68  next table on th
668c0 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72  e list.  The par
668d0 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c  ser builds the l
668e0 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a  ist this way..**
668f0 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c   But sqlite3SrcL
66900 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65  istShiftJoinType
66910 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20  () later shifts 
66920 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f  the jointypes so
66930 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f   that each.** jo
66940 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73  intype expresses
66950 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65   the join betwee
66960 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  n the table and 
66970 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62  the previous tab
66980 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72  le..*/.struct Sr
66990 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53  cList {.  i16 nS
669a0 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  rc;        /* Nu
669b0 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f  mber of tables o
669c0 72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20  r subqueries in 
669d0 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
669e0 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b  */.  i16 nAlloc;
669f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
66a00 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63  of entries alloc
66a10 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f  ated in a[] belo
66a20 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  w */.  struct Sr
66a30 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20  cList_item {.   
66a40 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65   char *zDatabase
66a50 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61  ;  /* Name of da
66a60 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74  tabase holding t
66a70 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  his table */.   
66a80 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
66a90 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
66aa0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63  e table */.    c
66ab0 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20  har *zAlias;    
66ac0 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74   /* The "B" part
66ad0 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70   of a "A AS B" p
66ae0 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73  hrase.  zName is
66af0 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20   the "A" */.    
66b00 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
66b10 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c    /* An SQL tabl
66b20 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
66b30 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  to zName */.    
66b40 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b  Select *pSelect;
66b50 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74    /* A SELECT st
66b60 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20  atement used in 
66b70 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65  place of a table
66b80 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20   name */.    u8 
66b90 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f  isPopulated;   /
66ba0 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  * Temporary tabl
66bb0 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
66bc0 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75  h SELECT is popu
66bd0 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20  lated */.    u8 
66be0 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f  jointype;      /
66bf0 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62  * Type of join b
66c00 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65  etween this able
66c10 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75   and the previou
66c20 73 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 49  s */.    u8 notI
66c30 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 72  ndexed;    /* Tr
66c40 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ue if there is a
66c50 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61   NOT INDEXED cla
66c60 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  use */.    int i
66c70 43 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20  Cursor;      /* 
66c80 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
66c90 6e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61  number used to a
66ca0 63 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65  ccess this table
66cb0 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f   */.    Expr *pO
66cc0 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  n;        /* The
66cd0 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20   ON clause of a 
66ce0 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69  join */.    IdLi
66cf0 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a  st *pUsing;   /*
66d00 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73   The USING claus
66d10 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20  e of a join */. 
66d20 20 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73     Bitmask colUs
66d30 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31  ed;  /* Bit N (1
66d40 3c 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75  <<N) set if colu
66d50 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 20  mn N of pTab is 
66d60 75 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 72  used */.    char
66d70 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a   *zIndex;     /*
66d80 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d   Identifier from
66d90 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49   "INDEXED BY <zI
66da0 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f  ndex>" clause */
66db0 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  .    Index *pInd
66dc0 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20  ex;    /* Index 
66dd0 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73  structure corres
66de0 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65  ponding to zInde
66df0 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d  x, if any */.  }
66e00 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20   a[1];          
66e10 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20     /* One entry 
66e20 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66  for each identif
66e30 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  ier on the list 
66e40 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72  */.};../*.** Per
66e50 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66  mitted values of
66e60 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a   the SrcList.a.j
66e70 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f  ointype field.*/
66e80 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45  .#define JT_INNE
66e90 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20  R     0x0001    
66ea0 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69  /* Any kind of i
66eb0 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f  nner or cross jo
66ec0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  in */.#define JT
66ed0 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30  _CROSS     0x000
66ee0 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74  2    /* Explicit
66ef0 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53   use of the CROS
66f00 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65  S keyword */.#de
66f10 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20  fine JT_NATURAL 
66f20 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54    0x0004    /* T
66f30 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72  rue for a "natur
66f40 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66  al" join */.#def
66f50 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20  ine JT_LEFT     
66f60 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65   0x0008    /* Le
66f70 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f  ft outer join */
66f80 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48  .#define JT_RIGH
66f90 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20  T     0x0010    
66fa0 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a  /* Right outer j
66fb0 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a  oin */.#define J
66fc0 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30  T_OUTER     0x00
66fd0 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55  20    /* The "OU
66fe0 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20  TER" keyword is 
66ff0 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69  present */.#defi
67000 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20  ne JT_ERROR     
67010 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b  0x0040    /* unk
67020 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72  nown or unsuppor
67030 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f  ted join type */
67040 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65  .../*.** A Where
67050 50 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c 64  Plan object hold
67060 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  s information th
67070 61 74 20 64 65 73 63 72 69 62 65 73 20 61 20 6c  at describes a l
67080 6f 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 67  ookup.** strateg
67090 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62  y..**.** This ob
670a0 6a 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 64  ject is intended
670b0 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f 75   to be opaque ou
670c0 74 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 65  tside of the whe
670d0 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20  re.c module..** 
670e0 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 68  It is included h
670f0 65 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 74  ere only so that
67100 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 77   that compiler w
67110 69 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67  ill know how big
67120 20 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65   it.** is.  None
67130 20 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 69   of the fields i
67140 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 68  n this object sh
67150 6f 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 74  ould be used out
67160 73 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 77  side of.** the w
67170 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a  here.c module..*
67180 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20  *.** Within the 
67190 75 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f  union, pIdx is o
671a0 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73  nly used when ws
671b0 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45  Flags&WHERE_INDE
671c0 58 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20  XED is true..** 
671d0 70 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73  pTerm is only us
671e0 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26  ed when wsFlags&
671f0 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69  WHERE_MULTI_OR i
67200 73 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74  s true.  And pVt
67210 61 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79  abIdx.** is only
67220 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61   used when wsFla
67230 67 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c  gs&WHERE_VIRTUAL
67240 54 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20  TABLE is true.  
67250 49 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0a  It is never the.
67260 2a 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f 72  ** case that mor
67270 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68  e than one of th
67280 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69  ese conditions i
67290 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63  s true..*/.struc
672a0 74 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 20  t WherePlan {.  
672b0 75 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 20  u32 wsFlags;    
672c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
672d0 2a 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20  * WHERE_* flags 
672e0 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68  that describe th
672f0 65 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20  e strategy */.  
67300 75 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 20  u32 nEq;        
67310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
67320 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63  * Number of == c
67330 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
67340 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 65  union {.    Inde
67350 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
67360 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
67370 64 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f 49  dex when WHERE_I
67380 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 20 2a  NDEXED is true *
67390 2f 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 65  /.    struct Whe
673a0 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20  reTerm *pTerm;  
673b0 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c       /* WHERE cl
673c0 61 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f 52  ause term for OR
673d0 2d 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 73  -search */.    s
673e0 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
673f0 6f 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f 2a  o *pVtabIdx;  /*
67400 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   Virtual table i
67410 6e 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20  ndex to use */. 
67420 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   } u;.};../*.** 
67430 46 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20  For each nested 
67440 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20  loop in a WHERE 
67450 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74  clause implement
67460 61 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65  ation, the Where
67470 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72  Info.** structur
67480 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e  e contains a sin
67490 67 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  gle instance of 
674a0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20  this structure. 
674b0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   This structure.
674c0 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  ** is intended t
674d0 6f 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65  o be private the
674e0 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64   the where.c mod
674f0 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e  ule and should n
67500 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20  ot be.** access 
67510 6f 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f  or modified by o
67520 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a  ther modules..**
67530 0a 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f  .** The pIdxInfo
67540 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74   field is used t
67550 6f 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20  o help pick the 
67560 62 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0a  best index on a.
67570 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
67580 2e 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20  .  The pIdxInfo 
67590 70 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73  pointer contains
675a0 20 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66   indexing.** inf
675b0 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  ormation for the
675c0 20 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74   i-th table in t
675d0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62  he FROM clause b
675e0 65 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67  efore reordering
675f0 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64  ..** All the pId
67600 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61  xInfo pointers a
67610 72 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72  re freed by wher
67620 65 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77  eInfoFree() in w
67630 68 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f  here.c..** All o
67640 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ther information
67650 20 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 65   in the i-th Whe
67660 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 66  reLevel object f
67670 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c  or the i-th tabl
67680 65 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d 20  e.** after FROM 
67690 63 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e  clause ordering.
676a0 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65  .*/.struct Where
676b0 4c 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 50  Level {.  WhereP
676c0 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 20  lan plan;       
676d0 2f 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f  /* query plan fo
676e0 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 6f  r this element o
676f0 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
67700 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74  e */.  int iLeft
67710 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20  Join;        /* 
67720 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64  Memory cell used
67730 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c 45   to implement LE
67740 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a 2f  FT OUTER JOIN */
67750 0a 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20  .  int iTabCur; 
67760 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
67770 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64  VDBE cursor used
67780 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 74   to access the t
67790 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49  able */.  int iI
677a0 64 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20  dxCur;          
677b0 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73  /* The VDBE curs
677c0 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  or used to acces
677d0 73 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20  s pIdx */.  int 
677e0 61 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20  addrBrk;        
677f0 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
67800 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74  o break out of t
67810 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  he loop */.  int
67820 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20   addrNxt;       
67830 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
67840 74 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 78  to start the nex
67850 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  t IN combination
67860 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f   */.  int addrCo
67870 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4a  nt;         /* J
67880 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74  ump here to cont
67890 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65  inue with the ne
678a0 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f  xt loop cycle */
678b0 0a 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74  .  int addrFirst
678c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  ;        /* Firs
678d0 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66  t instruction of
678e0 20 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65   interior of the
678f0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 46   loop */.  u8 iF
67900 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  rom;            
67910 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20   /* Which entry 
67920 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
67930 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 70  se */.  u8 op, p
67940 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  5;            /*
67950 20 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f   Opcode and P5 o
67960 66 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61  f the opcode tha
67970 74 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20  t ends the loop 
67980 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b  */.  int p1, p2;
67990 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
679a0 65 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70  erands of the op
679b0 63 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64  code used to end
679c0 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  s the loop */.  
679d0 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20  union {         
679e0 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61        /* Informa
679f0 74 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e 64  tion that depend
67a00 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67  s on plan.wsFlag
67a10 73 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20  s */.    struct 
67a20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b  {.      int nIn;
67a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
67a40 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
67a50 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20  es in aInLoop[] 
67a60 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  */.      struct 
67a70 49 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20  InLoop {.       
67a80 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20   int iCur;      
67a90 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
67aa0 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20  DBE cursor used 
67ab0 62 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61  by this IN opera
67ac0 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  tor */.        i
67ad0 6e 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20  nt addrInTop;   
67ae0 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20        /* Top of 
67af0 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20  the IN loop */. 
67b00 20 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b       } *aInLoop;
67b10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
67b20 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
67b30 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f  each nested IN o
67b40 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 7d  perator */.    }
67b50 20 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20   in;            
67b60 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
67b70 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57  n plan.wsFlags&W
67b80 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a  HERE_IN_ABLE */.
67b90 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 65    } u;..  /* The
67ba0 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64   following field
67bb0 20 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70   is really not p
67bc0 61 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 65  art of the curre
67bd0 6e 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20  nt level.  But. 
67be0 20 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c   ** we need a pl
67bf0 61 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 72  ace to cache vir
67c00 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78  tual table index
67c10 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
67c20 20 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 75   each.  ** virtu
67c30 61 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  al table in the 
67c40 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20  FROM clause and 
67c50 74 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73  the WhereLevel s
67c60 74 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a 2a  tructure is.  **
67c70 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c   a convenient pl
67c80 61 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 20  ace since there 
67c90 69 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65  is one WhereLeve
67ca0 6c 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20  l for each FROM 
67cb0 63 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d  clause.  ** elem
67cc0 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ent..  */.  sqli
67cd0 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
67ce0 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e  pIdxInfo;  /* In
67cf0 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74  dex info for n-t
67d00 68 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a  h source table *
67d10 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67  /.};../*.** Flag
67d20 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  s appropriate fo
67d30 72 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 73  r the wctrlFlags
67d40 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71   parameter of sq
67d50 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
67d60 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65  ).** and the Whe
67d70 72 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 67  reInfo.wctrlFlag
67d80 73 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65  s member..*/.#de
67d90 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52  fine WHERE_ORDER
67da0 42 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30  BY_NORMAL   0x00
67db0 30 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23  00 /* No-op */.#
67dc0 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44  define WHERE_ORD
67dd0 45 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78  ERBY_MIN      0x
67de0 30 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59  0001 /* ORDER BY
67df0 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20   processing for 
67e00 6d 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64  min() func */.#d
67e10 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45  efine WHERE_ORDE
67e20 52 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30  RBY_MAX      0x0
67e30 30 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20  002 /* ORDER BY 
67e40 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d  processing for m
67e50 61 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65  ax() func */.#de
67e60 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41  fine WHERE_ONEPA
67e70 53 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30  SS_DESIRED  0x00
67e80 30 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f  04 /* Want to do
67e90 20 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45   one-pass UPDATE
67ea0 2f 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69  /DELETE */.#defi
67eb0 6e 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41  ne WHERE_DUPLICA
67ec0 54 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 38  TES_OK    0x0008
67ed0 20 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 6e   /* Ok to return
67ee0 20 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 6e   a row more than
67ef0 20 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65   once */.#define
67f00 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e   WHERE_OMIT_OPEN
67f10 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20 2f          0x0010 /
67f20 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 61  * Table cursor a
67f30 72 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20  re already open 
67f40 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
67f50 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 20  _OMIT_CLOSE     
67f60 20 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 69 74    0x0020 /* Omit
67f70 20 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 20   close of table 
67f80 26 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20  & index cursors 
67f90 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
67fa0 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20  _FORCE_TABLE    
67fb0 20 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 20 6e    0x0040 /* Do n
67fc0 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d  ot use an index-
67fd0 6f 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a 0a  only search */..
67fe0 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20  /*.** The WHERE 
67ff0 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
68000 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77  g routine has tw
68010 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a  o halves.  The.*
68020 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65  * first part doe
68030 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  s the start of t
68040 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e  he WHERE loop an
68050 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  d the second.** 
68060 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61  half does the ta
68070 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  il of the WHERE 
68080 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e  loop.  An instan
68090 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74  ce of.** this st
680a0 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72  ructure is retur
680b0 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74  ned by the first
680c0 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64   half and passed
680d0 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63  .** into the sec
680e0 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65  ond half to give
680f0 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79   some continuity
68100 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
68110 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20  eInfo {.  Parse 
68120 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f  *pParse;       /
68130 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
68140 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
68150 6e 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 20 77  ntext */.  u16 w
68160 63 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20 20  ctrlFlags;      
68170 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e 61  /* Flags origina
68180 6c 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73 71  lly passed to sq
68190 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
681a0 29 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65 50  ) */.  u8 okOneP
681b0 61 73 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f  ass;        /* O
681c0 6b 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 61 73  k to use one-pas
681d0 73 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20  s algorithm for 
681e0 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
681f0 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
68200 54 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20 20  TabList;        
68210 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
68220 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f  tables in the jo
68230 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70  in */.  int iTop
68240 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
68250 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65         /* The ve
68260 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  ry beginning of 
68270 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a  the WHERE loop *
68280 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75  /.  int iContinu
68290 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
682a0 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
682b0 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68  to continue with
682c0 20 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a   next record */.
682d0 20 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20    int iBreak;   
682e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
682f0 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
68300 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68   break out of th
68310 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  e loop */.  int 
68320 6e 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20  nLevel;         
68330 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
68340 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c  mber of nested l
68350 6f 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  oop */.  struct 
68360 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
68370 3b 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d  ;       /* Decom
68380 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20  position of the 
68390 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
683a0 20 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31    WhereLevel a[1
683b0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
683c0 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
683d0 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20  about each nest 
683e0 6c 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 2a 2f  loop in WHERE */
683f0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d  .};../*.** A Nam
68400 65 43 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 73  eContext defines
68410 20 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68   a context in wh
68420 69 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 74  ich to resolve t
68430 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a  able and column.
68440 2a 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 63  ** names.  The c
68450 6f 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 73 20  ontext consists 
68460 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62  of a list of tab
68470 6c 65 73 20 28 74 68 65 20 70 53 72 63 4c 69 73  les (the pSrcLis
68480 74 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a 20  t) field and.** 
68490 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20  a list of named 
684a0 65 78 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69  expression (pELi
684b0 73 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20  st).  The named 
684c0 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20  expression list 
684d0 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20  may.** be NULL. 
684e0 20 54 68 65 20 70 53 72 63 20 63 6f 72 72 65 73   The pSrc corres
684f0 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f  ponds to the FRO
68500 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45  M clause of a SE
68510 4c 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 68  LECT or.** to th
68520 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70  e table being op
68530 65 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53  erated on by INS
68540 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20  ERT, UPDATE, or 
68550 44 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a 20  DELETE.  The.** 
68560 70 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e  pEList correspon
68570 64 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74  ds to the result
68580 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54   set of a SELECT
68590 20 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f 72   and is NULL for
685a0 0a 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 65 6d  .** other statem
685b0 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65  ents..**.** Name
685c0 43 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 65 20  Contexts can be 
685d0 6e 65 73 74 65 64 2e 20 20 57 68 65 6e 20 72 65  nested.  When re
685e0 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 74  solving names, t
685f0 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a  he inner-most .*
68600 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 73 65 61  * context is sea
68610 72 63 68 65 64 20 66 69 72 73 74 2e 20 20 49 66  rched first.  If
68620 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75   no match is fou
68630 6e 64 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 74  nd, the next out
68640 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73  er.** context is
68650 20 63 68 65 63 6b 65 64 2e 20 20 49 66 20 74 68   checked.  If th
68660 65 72 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f 20  ere is still no 
68670 6d 61 74 63 68 2c 20 74 68 65 20 6e 65 78 74 20  match, the next 
68680 63 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 68  context.** is ch
68690 65 63 6b 65 64 2e 20 20 54 68 69 73 20 70 72 6f  ecked.  This pro
686a0 63 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 75  cess continues u
686b0 6e 74 69 6c 20 65 69 74 68 65 72 20 61 20 6d 61  ntil either a ma
686c0 74 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a 20  tch is found.** 
686d0 6f 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 20  or all contexts 
686e0 61 72 65 20 63 68 65 63 6b 2e 20 20 57 68 65 6e  are check.  When
686f0 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e   a match is foun
68700 64 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 6d 62  d, the nRef memb
68710 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e  er of.** the con
68720 74 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20  text containing 
68730 74 68 65 20 6d 61 74 63 68 20 69 73 20 69 6e 63  the match is inc
68740 72 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a  remented. .**.**
68750 20 45 61 63 68 20 73 75 62 71 75 65 72 79 20 67   Each subquery g
68760 65 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 6f  ets a new NameCo
68770 6e 74 65 78 74 2e 20 20 54 68 65 20 70 4e 65 78  ntext.  The pNex
68780 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74  t field points t
68790 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e  o the.** NameCon
687a0 74 65 78 74 20 69 6e 20 74 68 65 20 70 61 72 65  text in the pare
687b0 6e 74 20 71 75 65 72 79 2e 20 20 54 68 75 73 20  nt query.  Thus 
687c0 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 73  the process of s
687d0 63 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 4e  canning the.** N
687e0 61 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 20  ameContext list 
687f0 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73  corresponds to s
68800 65 61 72 63 68 69 6e 67 20 74 68 72 6f 75 67 68  earching through
68810 20 73 75 63 63 65 73 73 69 76 65 6c 79 20 6f 75   successively ou
68820 74 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 65  ter.** subquerie
68830 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20  s looking for a 
68840 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 74  match..*/.struct
68850 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a 20   NameContext {. 
68860 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
68870 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
68880 73 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ser */.  SrcList
68890 20 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 2f 2a   *pSrcList;   /*
688a0 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 62   One or more tab
688b0 6c 65 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f  les used to reso
688c0 6c 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 45  lve names */.  E
688d0 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
688e0 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20      /* Optional 
688f0 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78  list of named ex
68900 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69  pressions */.  i
68910 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
68920 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
68930 20 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 20   names resolved 
68940 62 79 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20  by this context 
68950 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20  */.  int nErr;  
68960 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
68970 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e  ber of errors en
68980 63 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20  countered while 
68990 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20  resolving names 
689a0 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 67  */.  u8 allowAgg
689b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67  ;         /* Agg
689c0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
689d0 20 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a 2f   allowed here */
689e0 0a 20 20 75 38 20 68 61 73 41 67 67 3b 20 20 20  .  u8 hasAgg;   
689f0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
68a00 69 66 20 61 67 67 72 65 67 61 74 65 73 20 61 72  if aggregates ar
68a10 65 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 69  e seen */.  u8 i
68a20 73 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 20  sCheck;         
68a30 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 73 6f   /* True if reso
68a40 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 61  lving names in a
68a50 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e   CHECK constrain
68a60 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 74  t */.  int nDept
68a70 68 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  h;          /* D
68a80 65 70 74 68 20 6f 66 20 73 75 62 71 75 65 72 79  epth of subquery
68a90 20 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 6f   recursion. 1 fo
68aa0 72 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 2a  r no recursion *
68ab0 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  /.  AggInfo *pAg
68ac0 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f  gInfo;   /* Info
68ad0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 67  rmation about ag
68ae0 67 72 65 67 61 74 65 73 20 61 74 20 74 68 69 73  gregates at this
68af0 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65   level */.  Name
68b00 43 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b 20  Context *pNext; 
68b10 20 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 6e   /* Next outer n
68b20 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e 55  ame context.  NU
68b30 4c 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 74  LL for outermost
68b40 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
68b50 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
68b60 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
68b70 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c  ture contains al
68b80 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  l information.**
68b90 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72   needed to gener
68ba0 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73  ate code for a s
68bb0 69 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61  ingle SELECT sta
68bc0 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c  tement..**.** nL
68bd0 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 2d  imit is set to -
68be0 31 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  1 if there is no
68bf0 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20   LIMIT clause.  
68c00 6e 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74  nOffset is set t
68c10 6f 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65  o 0..** If there
68c20 20 69 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75   is a LIMIT clau
68c30 73 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 73  se, the parser s
68c40 65 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 68  ets nLimit to th
68c50 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a  e value of the.*
68c60 2a 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66  * limit and nOff
68c70 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65  set to the value
68c80 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 28   of the offset (
68c90 6f 72 20 30 20 69 66 20 74 68 65 72 65 20 69 73  or 0 if there is
68ca0 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 2e   not.** offset).
68cb0 20 20 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c 20    But later on, 
68cc0 6e 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73  nLimit and nOffs
68cd0 65 74 20 62 65 63 6f 6d 65 20 74 68 65 20 6d 65  et become the me
68ce0 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a  mory locations.*
68cf0 2a 20 69 6e 20 74 68 65 20 56 44 42 45 20 74 68  * in the VDBE th
68d00 61 74 20 72 65 63 6f 72 64 20 74 68 65 20 6c 69  at record the li
68d10 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20 63  mit and offset c
68d20 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61  ounters..**.** a
68d30 64 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 6e  ddrOpenEphm[] en
68d40 74 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 68  tries contain th
68d50 65 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50 5f  e address of OP_
68d60 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70  OpenEphemeral op
68d70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 20  codes..** These 
68d80 61 64 64 72 65 73 73 65 73 20 6d 75 73 74 20 62  addresses must b
68d90 65 20 73 74 6f 72 65 64 20 73 6f 20 74 68 61 74  e stored so that
68da0 20 77 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20   we can go back 
68db0 61 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74  and fill in.** t
68dc0 68 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 6e  he P4_KEYINFO an
68dd0 64 20 50 32 20 70 61 72 61 6d 65 74 65 72 73 20  d P2 parameters 
68de0 6c 61 74 65 72 2e 20 20 4e 65 69 74 68 65 72 20  later.  Neither 
68df0 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a  the KeyInfo nor.
68e00 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
68e10 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 63   columns in P2 c
68e20 61 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61  an be computed a
68e30 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 0a  t the same time.
68e40 2a 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 65  ** as the OP_Ope
68e50 6e 45 70 68 6d 20 69 6e 73 74 72 75 63 74 69 6f  nEphm instructio
68e60 6e 20 69 73 20 63 6f 64 65 64 20 62 65 63 61 75  n is coded becau
68e70 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68  se not.** enough
68e80 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
68e90 75 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  ut the compound 
68ea0 71 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61  query is known a
68eb0 74 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a  t that point..**
68ec0 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72   The KeyInfo for
68ed0 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d   addrOpenTran[0]
68ee0 20 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e   and [1] contain
68ef0 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  s collating sequ
68f00 65 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 65  ences.** for the
68f10 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68   result set.  Th
68f20 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64  e KeyInfo for ad
68f30 64 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f  drOpenTran[2] co
68f40 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67  ntains collating
68f50 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f  .** sequences fo
68f60 72 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  r the ORDER BY c
68f70 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  lause..*/.struct
68f80 20 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 72   Select {.  Expr
68f90 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20  List *pEList;   
68fa0 20 20 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 73     /* The fields
68fb0 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 2a   of the result *
68fc0 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20  /.  u8 op;      
68fd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
68fe0 65 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54  e of: TK_UNION T
68ff0 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 45  K_ALL TK_INTERSE
69000 43 54 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f 0a  CT TK_EXCEPT */.
69010 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b    char affinity;
69020 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65           /* Make
69030 52 65 63 6f 72 64 20 77 69 74 68 20 74 68 69 73  Record with this
69040 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52   affinity for SR
69050 54 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 73  T_Set */.  u16 s
69060 65 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  elFlags;        
69070 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f    /* Various SF_
69080 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 72  * values */.  Sr
69090 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20  cList *pSrc;    
690a0 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d       /* The FROM
690b0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70   clause */.  Exp
690c0 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20  r *pWhere;      
690d0 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
690e0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70   clause */.  Exp
690f0 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b  rList *pGroupBy;
69100 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50      /* The GROUP
69110 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
69120 45 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20  Expr *pHaving;  
69130 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41         /* The HA
69140 56 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20  VING clause */. 
69150 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
69160 72 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f  rBy;    /* The O
69170 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
69180 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69  /.  Select *pPri
69190 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72  or;        /* Pr
691a0 69 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 61 20  ior select in a 
691b0 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20  compound select 
691c0 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53  statement */.  S
691d0 65 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 20  elect *pNext;   
691e0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 65        /* Next se
691f0 6c 65 63 74 20 74 6f 20 74 68 65 20 6c 65 66 74  lect to the left
69200 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a   in a compound *
69210 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67  /.  Select *pRig
69220 68 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69  htmost;    /* Ri
69230 67 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20  ght-most select 
69240 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65  in a compound se
69250 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a  lect statement *
69260 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74  /.  Expr *pLimit
69270 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49  ;          /* LI
69280 4d 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  MIT expression. 
69290 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75  NULL means not u
692a0 73 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  sed. */.  Expr *
692b0 70 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  pOffset;        
692c0 20 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65   /* OFFSET expre
692d0 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e  ssion. NULL mean
692e0 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20  s not used. */. 
692f0 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f 66   int iLimit, iOf
69300 66 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72  fset;   /* Memor
69310 79 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64  y registers hold
69320 69 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 53  ing LIMIT & OFFS
69330 45 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  ET counters */. 
69340 20 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 68   int addrOpenEph
69350 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70  m[3];   /* OP_Op
69360 65 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 20  enEphem opcodes 
69370 72 65 6c 61 74 65 64 20 74 6f 20 74 68 69 73 20  related to this 
69380 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  select */.};../*
69390 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
693a0 65 73 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 65  es for Select.se
693b0 6c 46 6c 61 67 73 2e 20 20 54 68 65 20 22 53 46  lFlags.  The "SF
693c0 22 20 70 72 65 66 69 78 20 73 74 61 6e 64 73 20  " prefix stands 
693d0 66 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46  for.** "Select F
693e0 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  lag"..*/.#define
693f0 20 53 46 5f 44 69 73 74 69 6e 63 74 20 20 20 20   SF_Distinct    
69400 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f      0x0001  /* O
69410 75 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20  utput should be 
69420 44 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66  DISTINCT */.#def
69430 69 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 20  ine SF_Resolved 
69440 20 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f         0x0002  /
69450 2a 20 49 64 65 6e 74 69 66 69 65 72 73 20 68 61  * Identifiers ha
69460 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64  ve been resolved
69470 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 41   */.#define SF_A
69480 67 67 72 65 67 61 74 65 20 20 20 20 20 20 20 30  ggregate       0
69490 78 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69  x0004  /* Contai
694a0 6e 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  ns aggregate fun
694b0 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e  ctions */.#defin
694c0 65 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72  e SF_UsesEphemer
694d0 61 6c 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20  al   0x0008  /* 
694e0 55 73 65 73 20 74 68 65 20 4f 70 65 6e 45 70 68  Uses the OpenEph
694f0 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f  emeral opcode */
69500 0a 23 64 65 66 69 6e 65 20 53 46 5f 45 78 70 61  .#define SF_Expa
69510 6e 64 65 64 20 20 20 20 20 20 20 20 30 78 30 30  nded        0x00
69520 31 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65  10  /* sqlite3Se
69530 6c 65 63 74 45 78 70 61 6e 64 28 29 20 63 61 6c  lectExpand() cal
69540 6c 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23  led on this */.#
69550 64 65 66 69 6e 65 20 53 46 5f 48 61 73 54 79 70  define SF_HasTyp
69560 65 49 6e 66 6f 20 20 20 20 20 30 78 30 30 32 30  eInfo     0x0020
69570 20 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 65    /* FROM subque
69580 72 69 65 73 20 68 61 76 65 20 54 61 62 6c 65 20  ries have Table 
69590 6d 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a  metadata */.../*
695a0 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20  .** The results 
695b0 6f 66 20 61 20 73 65 6c 65 63 74 20 63 61 6e 20  of a select can 
695c0 62 65 20 64 69 73 74 72 69 62 75 74 65 64 20 69  be distributed i
695d0 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73 2e 20  n several ways. 
695e0 20 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 72   The.** "SRT" pr
695f0 65 66 69 78 20 6d 65 61 6e 73 20 22 53 45 4c 45  efix means "SELE
69600 43 54 20 52 65 73 75 6c 74 20 54 79 70 65 22 2e  CT Result Type".
69610 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f  .*/.#define SRT_
69620 55 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 20 20  Union        1  
69630 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20  /* Store result 
69640 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e  as keys in an in
69650 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  dex */.#define S
69660 52 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 20  RT_Except       
69670 32 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 73  2  /* Remove res
69680 75 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f 4e  ult from a UNION
69690 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e   index */.#defin
696a0 65 20 53 52 54 5f 45 78 69 73 74 73 20 20 20 20  e SRT_Exists    
696b0 20 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 31     3  /* Store 1
696c0 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   if the result i
696d0 73 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 23  s not empty */.#
696e0 64 65 66 69 6e 65 20 53 52 54 5f 44 69 73 63 61  define SRT_Disca
696f0 72 64 20 20 20 20 20 20 34 20 20 2f 2a 20 44 6f  rd      4  /* Do
69700 20 6e 6f 74 20 73 61 76 65 20 74 68 65 20 72 65   not save the re
69710 73 75 6c 74 73 20 61 6e 79 77 68 65 72 65 20 2a  sults anywhere *
69720 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20  /../* The ORDER 
69730 42 59 20 63 6c 61 75 73 65 20 69 73 20 69 67 6e  BY clause is ign
69740 6f 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20  ored for all of 
69750 74 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65  the above */.#de
69760 66 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72  fine IgnorableOr
69770 64 65 72 62 79 28 58 29 20 28 28 58 2d 3e 65 44  derby(X) ((X->eD
69780 65 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 72  est)<=SRT_Discar
69790 64 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 5f  d)..#define SRT_
697a0 4f 75 74 70 75 74 20 20 20 20 20 20 20 35 20 20  Output       5  
697b0 2f 2a 20 4f 75 74 70 75 74 20 65 61 63 68 20 72  /* Output each r
697c0 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a  ow of result */.
697d0 23 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20  #define SRT_Mem 
697e0 20 20 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53           6  /* S
697f0 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61  tore result in a
69800 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a   memory cell */.
69810 23 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20  #define SRT_Set 
69820 20 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53           7  /* S
69830 74 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20  tore results as 
69840 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78  keys in an index
69850 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
69860 54 61 62 6c 65 20 20 20 20 20 20 20 20 38 20 20  Table        8  
69870 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20  /* Store result 
69880 61 73 20 64 61 74 61 20 77 69 74 68 20 61 6e 20  as data with an 
69890 61 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20  automatic rowid 
698a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45  */.#define SRT_E
698b0 70 68 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f  phemTab     9  /
698c0 2a 20 43 72 65 61 74 65 20 74 72 61 6e 73 69 65  * Create transie
698d0 6e 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65  nt tab and store
698e0 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20   like SRT_Table 
698f0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 43  */.#define SRT_C
69900 6f 72 6f 75 74 69 6e 65 20 20 20 31 30 20 20 2f  oroutine   10  /
69910 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e  * Generate a sin
69920 67 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c  gle row of resul
69930 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74  t */../*.** A st
69940 72 75 63 74 75 72 65 20 75 73 65 64 20 74 6f 20  ructure used to 
69950 63 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 65  customize the be
69960 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65  havior of sqlite
69970 33 53 65 6c 65 63 74 28 29 2e 20 53 65 65 0a 2a  3Select(). See.*
69980 2a 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65  * comments above
69990 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29   sqlite3Select()
699a0 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f   for details..*/
699b0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
699c0 53 65 6c 65 63 74 44 65 73 74 20 53 65 6c 65 63  SelectDest Selec
699d0 74 44 65 73 74 3b 0a 73 74 72 75 63 74 20 53 65  tDest;.struct Se
699e0 6c 65 63 74 44 65 73 74 20 7b 0a 20 20 75 38 20  lectDest {.  u8 
699f0 65 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 2f  eDest;         /
69a00 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65  * How to dispose
69a10 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20   of the results 
69a20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79  */.  u8 affinity
69a30 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69  ;      /* Affini
69a40 74 79 20 75 73 65 64 20 77 68 65 6e 20 65 44 65  ty used when eDe
69a50 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a 20  st==SRT_Set */. 
69a60 20 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 20   int iParm;     
69a70 20 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 65     /* A paramete
69a80 72 20 75 73 65 64 20 62 79 20 74 68 65 20 65 44  r used by the eD
69a90 65 73 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 74  est disposal met
69aa0 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65  hod */.  int iMe
69ab0 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61  m;         /* Ba
69ac0 73 65 20 72 65 67 69 73 74 65 72 20 77 68 65 72  se register wher
69ad0 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 77 72  e results are wr
69ae0 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  itten */.  int n
69af0 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Mem;         /* 
69b00 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74  Number of regist
69b10 65 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  ers allocated */
69b20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e  .};../*.** Durin
69b30 67 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6f  g code generatio
69b40 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 20  n of statements 
69b50 74 68 61 74 20 64 6f 20 69 6e 73 65 72 74 73 20  that do inserts 
69b60 69 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45 4d 45  into AUTOINCREME
69b70 4e 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c 20 74  NT .** tables, t
69b80 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66  he following inf
69b90 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 74 74 61  ormation is atta
69ba0 63 68 65 64 20 74 6f 20 74 68 65 20 54 61 62 6c  ched to the Tabl
69bb0 65 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a 2a 2a  e.u.autoInc.p.**
69bc0 20 70 6f 69 6e 74 65 72 20 6f 66 20 65 61 63 68   pointer of each
69bd0 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74   autoincrement t
69be0 61 62 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 73  able to record s
69bf0 6f 6d 65 20 73 69 64 65 20 69 6e 66 6f 72 6d 61  ome side informa
69c00 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 68 65  tion that.** the
69c10 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
69c20 6e 65 65 64 73 2e 20 20 57 65 20 68 61 76 65 20  needs.  We have 
69c30 74 6f 20 6b 65 65 70 20 70 65 72 2d 74 61 62 6c  to keep per-tabl
69c40 65 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0a  e autoincrement.
69c50 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ** information i
69c60 6e 20 63 61 73 65 20 69 6e 73 65 72 74 73 20 61  n case inserts a
69c70 72 65 20 64 6f 77 6e 20 77 69 74 68 69 6e 20 74  re down within t
69c80 72 69 67 67 65 72 73 2e 20 20 54 72 69 67 67 65  riggers.  Trigge
69c90 72 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 6f 72  rs do not.** nor
69ca0 6d 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61 74 65  mally coordinate
69cb0 20 74 68 65 69 72 20 61 63 74 69 76 69 74 69 65   their activitie
69cc0 73 2c 20 62 75 74 20 77 65 20 64 6f 20 6e 65 65  s, but we do nee
69cd0 64 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20  d to coordinate 
69ce0 74 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 61  the.** loading a
69cf0 6e 64 20 73 61 76 69 6e 67 20 6f 66 20 61 75 74  nd saving of aut
69d00 6f 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66 6f 72  oincrement infor
69d10 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63  mation..*/.struc
69d20 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 7b 0a  t AutoincInfo {.
69d30 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70    AutoincInfo *p
69d40 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20  Next;   /* Next 
69d50 69 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20 61 20  info block in a 
69d60 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c  list of them all
69d70 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
69d80 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  b;          /* T
69d90 61 62 6c 65 20 74 68 69 73 20 69 6e 66 6f 20 62  able this info b
69da0 6c 6f 63 6b 20 72 65 66 65 72 73 20 74 6f 20 2a  lock refers to *
69db0 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
69dc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
69dd0 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44  ex in sqlite3.aD
69de0 62 5b 5d 20 6f 66 20 64 61 74 61 62 61 73 65 20  b[] of database 
69df0 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a  holding pTab */.
69e00 20 20 69 6e 74 20 72 65 67 43 74 72 3b 20 20 20    int regCtr;   
69e10 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
69e20 79 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69  y register holdi
69e30 6e 67 20 74 68 65 20 72 6f 77 69 64 20 63 6f 75  ng the rowid cou
69e40 6e 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  nter */.};../*.*
69e50 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 6f  * Size of the co
69e60 6c 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a 23 69  lumn cache.*/.#i
69e70 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 5f 43  fndef SQLITE_N_C
69e80 4f 4c 43 41 43 48 45 0a 23 20 64 65 66 69 6e 65  OLCACHE.# define
69e90 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43   SQLITE_N_COLCAC
69ea0 48 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  HE 10.#endif../*
69eb0 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f 6e 65  .** At least one
69ec0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
69ed0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
69ee0 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20  ture is created 
69ef0 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74 72 69  for each .** tri
69f00 67 67 65 72 20 74 68 61 74 20 6d 61 79 20 62 65  gger that may be
69f10 20 66 69 72 65 64 20 77 68 69 6c 65 20 70 61 72   fired while par
69f20 73 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 2c 20  sing an INSERT, 
69f30 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
69f40 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 41  .** statement. A
69f50 6c 6c 20 73 75 63 68 20 6f 62 6a 65 63 74 73 20  ll such objects 
69f60 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68  are stored in th
69f70 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65  e linked list he
69f80 61 64 65 64 20 61 74 0a 2a 2a 20 50 61 72 73 65  aded at.** Parse
69f90 2e 70 54 72 69 67 67 65 72 50 72 67 20 61 6e 64  .pTriggerPrg and
69fa0 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 73 74   deleted once st
69fb0 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 61 74  atement compilat
69fc0 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20  ion has been.** 
69fd0 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a  completed..**.**
69fe0 20 41 20 56 64 62 65 20 73 75 62 2d 70 72 6f 67   A Vdbe sub-prog
69ff0 72 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ram that impleme
6a000 6e 74 73 20 74 68 65 20 62 6f 64 79 20 61 6e 64  nts the body and
6a010 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20   WHEN clause of 
6a020 74 72 69 67 67 65 72 0a 2a 2a 20 54 72 69 67 67  trigger.** Trigg
6a030 65 72 50 72 67 2e 70 54 72 69 67 67 65 72 2c 20  erPrg.pTrigger, 
6a040 61 73 73 75 6d 69 6e 67 20 61 20 64 65 66 61 75  assuming a defau
6a050 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63  lt ON CONFLICT c
6a060 6c 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72 69 67  lause of.** Trig
6a070 67 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c 20 69  gerPrg.orconf, i
6a080 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
6a090 54 72 69 67 67 65 72 50 72 67 2e 70 50 72 6f 67  TriggerPrg.pProg
6a0a0 72 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a  ram variable..**
6a0b0 20 54 68 65 20 50 61 72 73 65 2e 70 54 72 69 67   The Parse.pTrig
6a0c0 67 65 72 50 72 67 20 6c 69 73 74 20 6e 65 76 65  gerPrg list neve
6a0d0 72 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 65  r contains two e
6a0e0 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 65 20  ntries with the 
6a0f0 73 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73 20 66  same.** values f
6a100 6f 72 20 62 6f 74 68 20 70 54 72 69 67 67 65 72  or both pTrigger
6a110 20 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a 2a 0a   and orconf..**.
6a120 2a 2a 20 54 68 65 20 54 72 69 67 67 65 72 50 72  ** The TriggerPr
6a130 67 2e 6f 6c 64 6d 61 73 6b 20 76 61 72 69 61 62  g.oldmask variab
6a140 6c 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 6d  le is set to a m
6a150 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c  ask of old.* col
6a160 75 6d 6e 73 0a 2a 2a 20 61 63 63 65 73 73 65 64  umns.** accessed
6a170 20 28 6f 72 20 73 65 74 20 74 6f 20 30 20 66 6f   (or set to 0 fo
6a180 72 20 74 72 69 67 67 65 72 73 20 66 69 72 65 64  r triggers fired
6a190 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20   as a result of 
6a1a0 49 4e 53 45 52 54 20 0a 2a 2a 20 73 74 61 74 65  INSERT .** state
6a1b0 6d 65 6e 74 73 29 2e 0a 2a 2f 0a 73 74 72 75 63  ments)..*/.struc
6a1c0 74 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a 20  t TriggerPrg {. 
6a1d0 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
6a1e0 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 67  er;      /* Trig
6a1f0 67 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 6d  ger this program
6a200 20 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d 20   was coded from 
6a210 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b  */.  int orconf;
6a220 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6a230 44 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c  Default ON CONFL
6a240 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20  ICT policy */.  
6a250 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f  SubProgram *pPro
6a260 67 72 61 6d 3b 20 20 20 2f 2a 20 50 72 6f 67 72  gram;   /* Progr
6a270 61 6d 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  am implementing 
6a280 70 54 72 69 67 67 65 72 2f 6f 72 63 6f 6e 66 20  pTrigger/orconf 
6a290 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 6b  */.  u32 oldmask
6a2a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
6a2b0 4d 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f  Mask of old.* co
6a2c0 6c 75 6d 6e 73 20 61 63 63 65 73 73 65 64 20 2a  lumns accessed *
6a2d0 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a  /.  TriggerPrg *
6a2e0 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e  pNext;      /* N
6a2f0 65 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61 72  ext entry in Par
6a300 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c  se.pTriggerPrg l
6a310 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ist */.};../*.**
6a320 20 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63   An SQL parser c
6a330 6f 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20  ontext.  A copy 
6a340 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
6a350 65 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f  e is passed thro
6a360 75 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65  ugh.** the parse
6a370 72 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20  r and down into 
6a380 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61  all the parser a
6a390 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e  ction routine in
6a3a0 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72   order to.** car
6a3b0 72 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d  ry around inform
6a3c0 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c  ation that is gl
6a3d0 6f 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69  obal to the enti
6a3e0 72 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20  re parse..**.** 
6a3f0 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73  The structure is
6a400 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77   divided into tw
6a410 6f 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74  o parts.  When t
6a420 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f  he parser and co
6a430 64 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63  de.** generate c
6a440 61 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72  all themselves r
6a450 65 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20  ecursively, the 
6a460 66 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68  first part of th
6a470 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69  e structure.** i
6a480 73 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74  s constant but t
6a490 68 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69  he second part i
6a4a0 73 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62  s reset at the b
6a4b0 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64  eginning and end
6a4c0 20 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75   of.** each recu
6a4d0 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rsion..**.** The
6a4e0 20 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20   nTableLock and 
6a4f0 61 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61  aTableLock varia
6a500 62 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73  bles are only us
6a510 65 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64  ed if the shared
6a520 2d 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75  -cache .** featu
6a530 72 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69  re is enabled (i
6a540 66 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e  f sqlite3Tsd()->
6a550 75 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73  useSharedData is
6a560 20 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65   true). They are
6a570 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72  .** used to stor
6a580 65 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62  e the set of tab
6a590 6c 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65  le-locks require
6a5a0 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65  d by the stateme
6a5b0 6e 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70  nt being.** comp
6a5c0 69 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73  iled. Function s
6a5d0 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28  qlite3TableLock(
6a5e0 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64  ) is used to add
6a5f0 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a   entries to the.
6a600 2a 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75  ** list..*/.stru
6a610 63 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c  ct Parse {.  sql
6a620 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
6a630 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61    /* The main da
6a640 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
6a650 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
6a660 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
6a670 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65  turn code from e
6a680 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68  xecution */.  ch
6a690 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20  ar *zErrMsg;    
6a6a0 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d     /* An error m
6a6b0 65 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65  essage */.  Vdbe
6a6c0 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20   *pVdbe;        
6a6d0 20 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f   /* An engine fo
6a6e0 72 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61  r executing data
6a6f0 62 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f  base bytecode */
6a700 0a 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65  .  u8 colNamesSe
6a710 74 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20  t;      /* TRUE 
6a720 61 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e  after OP_ColumnN
6a730 61 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73  ame has been iss
6a740 75 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a  ued to pVdbe */.
6a750 20 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20    u8 nameClash; 
6a760 20 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d         /* A perm
6a770 61 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65  anent table name
6a780 20 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65   clashes with te
6a790 6d 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f  mp table name */
6a7a0 0a 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d  .  u8 checkSchem
6a7b0 61 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65  a;      /* Cause
6a7c0 73 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  s schema cookie 
6a7d0 63 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65  check after an e
6a7e0 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73  rror */.  u8 nes
6a7f0 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ted;           /
6a800 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74  * Number of nest
6a810 65 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20  ed calls to the 
6a820 70 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65  parser/code gene
6a830 72 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61  rator */.  u8 pa
6a840 72 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20  rseError;       
6a850 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20  /* True after a 
6a860 70 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20  parsing error.  
6a870 54 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a  Ticket #1794 */.
6a880 20 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20    u8 nTempReg;  
6a890 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6a8a0 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65   of temporary re
6a8b0 67 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70  gisters in aTemp
6a8c0 52 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54  Reg[] */.  u8 nT
6a8d0 65 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20  empInUse;       
6a8e0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65  /* Number of aTe
6a8f0 6d 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c  mpReg[] currentl
6a900 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f  y checked out */
6a910 0a 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b  .  int aTempReg[
6a920 38 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69  8];     /* Holdi
6a930 6e 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70  ng area for temp
6a940 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20  orary registers 
6a950 2a 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52  */.  int nRangeR
6a960 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a  eg;       /* Siz
6a970 65 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61  e of the tempora
6a980 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63  ry register bloc
6a990 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67  k */.  int iRang
6a9a0 65 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46  eReg;       /* F
6a9b0 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e  irst register in
6a9c0 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73   temporary regis
6a9d0 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69  ter block */.  i
6a9e0 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20  nt nErr;        
6a9f0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6aa00 20 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a   errors seen */.
6aa10 20 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20    int nTab;     
6aa20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6aa30 20 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61   of previously a
6aa40 6c 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75  llocated VDBE cu
6aa50 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  rsors */.  int n
6aa60 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
6aa70 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d  /* Number of mem
6aa80 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73  ory cells used s
6aa90 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  o far */.  int n
6aaa0 53 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Set;            
6aab0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74  /* Number of set
6aac0 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f  s used so far */
6aad0 0a 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20  .  int ckBase;  
6aae0 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20          /* Base 
6aaf0 72 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61  register of data
6ab00 20 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f   during check co
6ab10 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69  nstraints */.  i
6ab20 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b 20  nt iCacheLevel; 
6ab30 20 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 20      /* ColCache 
6ab40 76 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c 43  valid when aColC
6ab50 61 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d 69  ache[].iLevel<=i
6ab60 43 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 20  CacheLevel */.  
6ab70 69 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 20  int iCacheCnt;  
6ab80 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20       /* Counter 
6ab90 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65  used to generate
6aba0 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 75   aColCache[].lru
6abb0 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20   values */.  u8 
6abc0 6e 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20  nColCache;      
6abd0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
6abe0 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f  ntries in the co
6abf0 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20  lumn cache */.  
6ac00 75 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20  u8 iColCache;   
6ac10 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74       /* Next ent
6ac20 72 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 20  ry of the cache 
6ac30 74 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20  to replace */.  
6ac40 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
6ac50 20 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c   {.    int iTabl
6ac60 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
6ac70 54 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d  Table cursor num
6ac80 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ber */.    int i
6ac90 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
6aca0 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e   /* Table column
6acb0 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 75   number */.    u
6acc0 38 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 20  8 affChange;    
6acd0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
6ace0 74 68 69 73 20 72 65 67 69 73 74 65 72 20 68 61  this register ha
6acf0 73 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69 74  s had an affinit
6ad00 79 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20  y change */.    
6ad10 75 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20 20  u8 tempReg;     
6ad20 20 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69 73        /* iReg is
6ad30 20 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 72   a temp register
6ad40 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62   that needs to b
6ad50 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 69  e freed */.    i
6ad60 6e 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20 20  nt iLevel;      
6ad70 20 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20       /* Nesting 
6ad80 6c 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e 74  level */.    int
6ad90 20 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20   iReg;          
6ada0 20 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20 76     /* Reg with v
6adb0 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c  alue of this col
6adc0 75 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f 6e  umn. 0 means non
6add0 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72  e. */.    int lr
6ade0 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  u;              
6adf0 2f 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74 6c  /* Least recentl
6ae00 79 20 75 73 65 64 20 65 6e 74 72 79 20 68 61 73  y used entry has
6ae10 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61   the smallest va
6ae20 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43  lue */.  } aColC
6ae30 61 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 4f  ache[SQLITE_N_CO
6ae40 4c 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e 65  LCACHE];  /* One
6ae50 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   for each column
6ae60 20 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a   cache entry */.
6ae70 20 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b 3b    u32 writeMask;
6ae80 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20         /* Start 
6ae90 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  a write transact
6aea0 69 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61 74  ion on these dat
6aeb0 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32 20  abases */.  u32 
6aec0 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20 20  cookieMask;     
6aed0 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 73   /* Bitmask of s
6aee0 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 64  chema verified d
6aef0 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 38  atabases */.  u8
6af00 20 69 73 4d 75 6c 74 69 57 72 69 74 65 3b 20 20   isMultiWrite;  
6af10 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74     /* True if st
6af20 61 74 65 6d 65 6e 74 20 6d 61 79 20 61 66 66 65  atement may affe
6af30 63 74 2f 69 6e 73 65 72 74 20 6d 75 6c 74 69 70  ct/insert multip
6af40 6c 65 20 72 6f 77 73 20 2a 2f 0a 20 20 75 38 20  le rows */.  u8 
6af50 6d 61 79 41 62 6f 72 74 3b 20 20 20 20 20 20 20  mayAbort;       
6af60 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 61    /* True if sta
6af70 74 65 6d 65 6e 74 20 6d 61 79 20 74 68 72 6f 77  tement may throw
6af80 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74   an ABORT except
6af90 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f  ion */.  int coo
6afa0 6b 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a  kieGoto;      /*
6afb0 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47   Address of OP_G
6afc0 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65  oto to cookie ve
6afd0 72 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e  rifier subroutin
6afe0 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69  e */.  int cooki
6aff0 65 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41  eValue[SQLITE_MA
6b000 58 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20  X_ATTACHED+2];  
6b010 2f 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f  /* Values of coo
6b020 6b 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a  kies to verify *
6b030 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
6b040 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
6b050 48 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c  HE.  int nTableL
6b060 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  ock;        /* N
6b070 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69  umber of locks i
6b080 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a  n aTableLock */.
6b090 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61    TableLock *aTa
6b0a0 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75  bleLock; /* Requ
6b0b0 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73  ired table locks
6b0c0 20 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68   for shared-cach
6b0d0 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66  e mode */.#endif
6b0e0 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b  .  int regRowid;
6b0f0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
6b100 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69  ter holding rowi
6b110 64 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c  d of CREATE TABL
6b120 45 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74  E entry */.  int
6b130 20 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20   regRoot;       
6b140 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f    /* Register ho
6b150 6c 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20  lding root page 
6b160 6e 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f  number for new o
6b170 62 6a 65 63 74 73 20 2a 2f 0a 20 20 41 75 74 6f  bjects */.  Auto
6b180 69 6e 63 49 6e 66 6f 20 2a 70 41 69 6e 63 3b 20  incInfo *pAinc; 
6b190 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
6b1a0 61 62 6f 75 74 20 41 55 54 4f 49 4e 43 52 45 4d  about AUTOINCREM
6b1b0 45 4e 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  ENT counters */.
6b1c0 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 3b 20 20    int nMaxArg;  
6b1d0 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 61 72         /* Max ar
6b1e0 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 73 65  gs passed to use
6b1f0 72 20 66 75 6e 63 74 69 6f 6e 20 62 79 20 73 75  r function by su
6b200 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 0a 20 20  b-program */..  
6b210 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75  /* Information u
6b220 73 65 64 20 77 68 69 6c 65 20 63 6f 64 69 6e 67  sed while coding
6b230 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
6b240 73 2e 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70  s. */.  Parse *p
6b250 54 6f 70 6c 65 76 65 6c 3b 20 20 20 20 2f 2a 20  Toplevel;    /* 
6b260 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20  Parse structure 
6b270 66 6f 72 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d  for main program
6b280 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20   (or NULL) */.  
6b290 54 61 62 6c 65 20 2a 70 54 72 69 67 67 65 72 54  Table *pTriggerT
6b2a0 61 62 3b 20 20 2f 2a 20 54 61 62 6c 65 20 74 72  ab;  /* Table tr
6b2b0 69 67 67 65 72 73 20 61 72 65 20 62 65 69 6e 67  iggers are being
6b2c0 20 63 6f 64 65 64 20 66 6f 72 20 2a 2f 0a 20 20   coded for */.  
6b2d0 75 33 32 20 6f 6c 64 6d 61 73 6b 3b 20 20 20 20  u32 oldmask;    
6b2e0 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
6b2f0 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72 65  old.* columns re
6b300 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75 38  ferenced */.  u8
6b310 20 65 54 72 69 67 67 65 72 4f 70 3b 20 20 20 20   eTriggerOp;    
6b320 20 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45 2c     /* TK_UPDATE,
6b330 20 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 54 4b   TK_INSERT or TK
6b340 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20  _DELETE */.  u8 
6b350 65 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20  eOrconf;        
6b360 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20    /* Default ON 
6b370 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20  CONFLICT policy 
6b380 66 6f 72 20 74 72 69 67 67 65 72 20 73 74 65 70  for trigger step
6b390 73 20 2a 2f 0a 20 20 75 38 20 64 69 73 61 62 6c  s */.  u8 disabl
6b3a0 65 54 72 69 67 67 65 72 73 3b 20 20 2f 2a 20 54  eTriggers;  /* T
6b3b0 72 75 65 20 74 6f 20 64 69 73 61 62 6c 65 20 74  rue to disable t
6b3c0 72 69 67 67 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a  riggers */..  /*
6b3d0 20 41 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61   Above is consta
6b3e0 6e 74 20 62 65 74 77 65 65 6e 20 72 65 63 75 72  nt between recur
6b3f0 73 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73  sions.  Below is
6b400 20 72 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e   reset before an
6b410 64 20 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63  d after.  ** eac
6b420 68 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a  h recursion */..
6b430 20 20 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20    int nVar;     
6b440 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6b450 20 6f 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65   of '?' variable
6b460 73 20 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51  s seen in the SQ
6b470 4c 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e  L so far */.  in
6b480 74 20 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20  t nVarExpr;     
6b490 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6b4a0 75 73 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70  used slots in ap
6b4b0 56 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69  VarExpr[] */.  i
6b4c0 6e 74 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63  nt nVarExprAlloc
6b4d0 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  ;   /* Number of
6b4e0 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73   allocated slots
6b4f0 20 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20   in apVarExpr[] 
6b500 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61  */.  Expr **apVa
6b510 72 45 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69  rExpr;    /* Poi
6b520 6e 74 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e  nters to :aaa an
6b530 64 20 24 61 61 61 61 20 77 69 6c 64 63 61 72 64  d $aaaa wildcard
6b540 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a   expressions */.
6b550 20 20 56 64 62 65 20 2a 70 52 65 70 72 65 70 61    Vdbe *pReprepa
6b560 72 65 3b 20 20 20 20 2f 2a 20 56 4d 20 62 65 69  re;    /* VM bei
6b570 6e 67 20 72 65 70 72 65 70 61 72 65 64 20 28 73  ng reprepared (s
6b580 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28  qlite3Reprepare(
6b590 29 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 69  )) */.  int nAli
6b5a0 61 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  as;          /* 
6b5b0 4e 75 6d 62 65 72 20 6f 66 20 61 6c 69 61 73 65  Number of aliase
6b5c0 64 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c  d result set col
6b5d0 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  umns */.  int nA
6b5e0 6c 69 61 73 41 6c 6c 6f 63 3b 20 20 20 20 20 2f  liasAlloc;     /
6b5f0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f  * Number of allo
6b600 63 61 74 65 64 20 73 6c 6f 74 73 20 66 6f 72 20  cated slots for 
6b610 61 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 20 69 6e  aAlias[] */.  in
6b620 74 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 20 20  t *aAlias;      
6b630 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75     /* Register u
6b640 73 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 69 61  sed to hold alia
6b650 73 65 64 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20  sed result */.  
6b660 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20  u8 explain;     
6b670 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
6b680 74 68 65 20 45 58 50 4c 41 49 4e 20 66 6c 61 67  the EXPLAIN flag
6b690 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65   is found on the
6b6a0 20 71 75 65 72 79 20 2a 2f 0a 20 20 54 6f 6b 65   query */.  Toke
6b6b0 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20  n sNameToken;   
6b6c0 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20 75   /* Token with u
6b6d0 6e 71 75 61 6c 69 66 69 65 64 20 73 63 68 65 6d  nqualified schem
6b6e0 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f  a object name */
6b6f0 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54 6f  .  Token sLastTo
6b700 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c  ken;    /* The l
6b710 61 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 65 64  ast token parsed
6b720 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
6b730 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 6c   *zTail;   /* Al
6b740 6c 20 53 51 4c 20 74 65 78 74 20 70 61 73 74 20  l SQL text past 
6b750 74 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f 6c  the last semicol
6b760 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 54  on parsed */.  T
6b770 61 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 3b  able *pNewTable;
6b780 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 62      /* A table b
6b790 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64  eing constructed
6b7a0 20 62 79 20 43 52 45 41 54 45 20 54 41 42 4c 45   by CREATE TABLE
6b7b0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
6b7c0 4e 65 77 54 72 69 67 67 65 72 3b 20 20 20 20 20  NewTrigger;     
6b7d0 2f 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65 72  /* Trigger under
6b7e0 20 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61 20   construct by a 
6b7f0 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2a  CREATE TRIGGER *
6b800 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
6b810 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a  zAuthContext; /*
6b820 20 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74   The 6th paramet
6b830 65 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20  er to db->xAuth 
6b840 63 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66  callbacks */.#if
6b850 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
6b860 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
6b870 54 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20  Token sArg;     
6b880 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
6b890 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61  mplete text of a
6b8a0 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74   module argument
6b8b0 20 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65   */.  u8 declare
6b8c0 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  Vtab;           
6b8d0 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69   /* True if insi
6b8e0 64 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61  de sqlite3_decla
6b8f0 72 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 69  re_vtab() */.  i
6b900 6e 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 20 20  nt nVtabLock;   
6b910 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
6b920 62 65 72 20 6f 66 20 76 69 72 74 75 61 6c 20 74  ber of virtual t
6b930 61 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f  ables to lock */
6b940 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61  .  Table **apVta
6b950 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a  bLock;        /*
6b960 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74   Pointer to virt
6b970 75 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69  ual tables needi
6b980 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65  ng locking */.#e
6b990 6e 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 69 67  ndif.  int nHeig
6b9a0 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ht;            /
6b9b0 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65  * Expression tre
6b9c0 65 20 68 65 69 67 68 74 20 6f 66 20 63 75 72 72  e height of curr
6b9d0 65 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 20 2a  ent sub-select *
6b9e0 2f 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d 62  /.  Table *pZomb
6b9f0 69 65 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 4c  ieTab;      /* L
6ba00 69 73 74 20 6f 66 20 54 61 62 6c 65 20 6f 62 6a  ist of Table obj
6ba10 65 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 61  ects to delete a
6ba20 66 74 65 72 20 63 6f 64 65 20 67 65 6e 20 2a 2f  fter code gen */
6ba30 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70  .  TriggerPrg *p
6ba40 54 72 69 67 67 65 72 50 72 67 3b 20 20 20 20 2f  TriggerPrg;    /
6ba50 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  * Linked list of
6ba60 20 63 6f 64 65 64 20 74 72 69 67 67 65 72 73 20   coded triggers 
6ba70 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51  */.};..#ifdef SQ
6ba80 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
6ba90 4c 54 41 42 4c 45 0a 20 20 23 64 65 66 69 6e 65  LTABLE.  #define
6baa0 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42   IN_DECLARE_VTAB
6bab0 20 30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69   0.#else.  #defi
6bac0 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  ne IN_DECLARE_VT
6bad0 41 42 20 28 70 50 61 72 73 65 2d 3e 64 65 63 6c  AB (pParse->decl
6bae0 61 72 65 56 74 61 62 29 0a 23 65 6e 64 69 66 0a  areVtab).#endif.
6baf0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
6bb00 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
6bb10 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61  ing structure ca
6bb20 6e 20 62 65 20 64 65 63 6c 61 72 65 64 20 6f 6e  n be declared on
6bb30 20 61 20 73 74 61 63 6b 20 61 6e 64 20 75 73 65   a stack and use
6bb40 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 68 65  d.** to save the
6bb50 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74   Parse.zAuthCont
6bb60 65 78 74 20 76 61 6c 75 65 20 73 6f 20 74 68 61  ext value so tha
6bb70 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 73 74  t it can be rest
6bb80 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73  ored later..*/.s
6bb90 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78  truct AuthContex
6bba0 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t {.  const char
6bbb0 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20   *zAuthContext; 
6bbc0 20 20 2f 2a 20 50 75 74 20 73 61 76 65 64 20 50    /* Put saved P
6bbd0 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78  arse.zAuthContex
6bbe0 74 20 68 65 72 65 20 2a 2f 0a 20 20 50 61 72 73  t here */.  Pars
6bbf0 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
6bc00 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50          /* The P
6bc10 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 2a  arse structure *
6bc20 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66  /.};../*.** Bitf
6bc30 69 65 6c 64 20 66 6c 61 67 73 20 66 6f 72 20 50  ield flags for P
6bc40 35 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e  5 value in OP_In
6bc50 73 65 72 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65  sert and OP_Dele
6bc60 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  te.*/.#define OP
6bc70 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20 20 20  FLAG_NCHANGE    
6bc80 20 20 20 30 78 30 31 20 20 20 20 2f 2a 20 53 65     0x01    /* Se
6bc90 74 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d 3e  t to update db->
6bca0 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69  nChange */.#defi
6bcb0 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f  ne OPFLAG_LASTRO
6bcc0 57 49 44 20 20 20 20 20 30 78 30 32 20 20 20 20  WID     0x02    
6bcd0 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65  /* Set to update
6bce0 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a   db->lastRowid *
6bcf0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47  /.#define OPFLAG
6bd00 5f 49 53 55 50 44 41 54 45 20 20 20 20 20 20 30  _ISUPDATE      0
6bd10 78 30 34 20 20 20 20 2f 2a 20 54 68 69 73 20 4f  x04    /* This O
6bd20 50 5f 49 6e 73 65 72 74 20 69 73 20 61 6e 20 73  P_Insert is an s
6bd30 71 6c 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65  ql UPDATE */.#de
6bd40 66 69 6e 65 20 4f 50 46 4c 41 47 5f 41 50 50 45  fine OPFLAG_APPE
6bd50 4e 44 20 20 20 20 20 20 20 20 30 78 30 38 20 20  ND        0x08  
6bd60 20 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b    /* This is lik
6bd70 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70  ely to be an app
6bd80 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  end */.#define O
6bd90 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53  PFLAG_USESEEKRES
6bda0 55 4c 54 20 30 78 31 30 20 20 20 20 2f 2a 20 54  ULT 0x10    /* T
6bdb0 72 79 20 74 6f 20 61 76 6f 69 64 20 61 20 73 65  ry to avoid a se
6bdc0 65 6b 20 69 6e 20 42 74 72 65 65 49 6e 73 65 72  ek in BtreeInser
6bdd0 74 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  t() */.#define O
6bde0 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45  PFLAG_CLEARCACHE
6bdf0 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 43      0x20    /* C
6be00 6c 65 61 72 20 70 73 65 75 64 6f 2d 74 61 62 6c  lear pseudo-tabl
6be10 65 20 63 61 63 68 65 20 69 6e 20 4f 50 5f 43 6f  e cache in OP_Co
6be20 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45  lumn */../*. * E
6be30 61 63 68 20 74 72 69 67 67 65 72 20 70 72 65 73  ach trigger pres
6be40 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62  ent in the datab
6be50 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 74  ase schema is st
6be60 6f 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61  ored as an insta
6be70 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74  nce of. * struct
6be80 20 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a   Trigger. . *. *
6be90 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73   Pointers to ins
6bea0 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74  tances of struct
6beb0 20 54 72 69 67 67 65 72 20 61 72 65 20 73 74 6f   Trigger are sto
6bec0 72 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e  red in two ways.
6bed0 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74  . * 1. In the "t
6bee0 72 69 67 48 61 73 68 22 20 68 61 73 68 20 74 61  rigHash" hash ta
6bef0 62 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68 65  ble (part of the
6bf00 20 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72   sqlite3* that r
6bf10 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20  epresents the . 
6bf20 2a 20 20 20 20 64 61 74 61 62 61 73 65 29 2e 20  *    database). 
6bf30 54 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67  This allows Trig
6bf40 67 65 72 20 73 74 72 75 63 74 75 72 65 73 20 74  ger structures t
6bf50 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 62  o be retrieved b
6bf60 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c  y name.. * 2. Al
6bf70 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63  l triggers assoc
6bf80 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e  iated with a sin
6bf90 67 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61  gle table form a
6bfa0 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73   linked list, us
6bfb0 69 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e  ing the. *    pN
6bfc0 65 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74  ext member of st
6bfd0 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 41 20  ruct Trigger. A 
6bfe0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66  pointer to the f
6bff0 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
6c000 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64  the. *    linked
6c010 20 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20   list is stored 
6c020 61 73 20 74 68 65 20 22 70 54 72 69 67 67 65 72  as the "pTrigger
6c030 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20  " member of the 
6c040 61 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20  associated. *   
6c050 20 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20   struct Table.. 
6c060 2a 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c  *. * The "step_l
6c070 69 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e  ist" member poin
6c080 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  ts to the first 
6c090 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e  element of a lin
6c0a0 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74  ked list. * cont
6c0b0 61 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73  aining the SQL s
6c0c0 74 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66  tatements specif
6c0d0 69 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67  ied as the trigg
6c0e0 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a  er program.. */.
6c0f0 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 7b  struct Trigger {
6c100 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
6c110 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
6c120 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72  e name of the tr
6c130 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20  igger           
6c140 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a               */.
6c150 20 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20    char *table;  
6c160 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
6c170 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74   table or view t
6c180 6f 20 77 68 69 63 68 20 74 68 65 20 74 72 69 67  o which the trig
6c190 67 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20  ger applies */. 
6c1a0 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20   u8 op;         
6c1b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
6c1c0 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b  of TK_DELETE, TK
6c1d0 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45  _UPDATE, TK_INSE
6c1e0 52 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  RT         */.  
6c1f0 75 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20  u8 tr_tm;       
6c200 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
6c210 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  f TRIGGER_BEFORE
6c220 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20  , TRIGGER_AFTER 
6c230 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e  */.  Expr *pWhen
6c240 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
6c250 54 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20  The WHEN clause 
6c260 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
6c270 6e 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20  n (may be NULL) 
6c280 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f  */.  IdList *pCo
6c290 6c 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20  lumns;       /* 
6c2a0 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50  If this is an UP
6c2b0 44 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d  DATE OF <column-
6c2c0 6c 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20  list> trigger,. 
6c2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c2e0 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
6c2f0 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73  <column-list> is
6c300 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a   stored here */.
6c310 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
6c320 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68  a;        /* Sch
6c330 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ema containing t
6c340 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20  he trigger */.  
6c350 53 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65  Schema *pTabSche
6c360 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d  ma;     /* Schem
6c370 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  a containing the
6c380 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67   table */.  Trig
6c390 67 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69  gerStep *step_li
6c3a0 73 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74  st; /* Link list
6c3b0 20 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67   of trigger prog
6c3c0 72 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20  ram steps       
6c3d0 20 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67        */.  Trigg
6c3e0 65 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  er *pNext;      
6c3f0 20 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67     /* Next trigg
6c400 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  er associated wi
6c410 74 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  th the table */.
6c420 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67  };../*.** A trig
6c430 67 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20  ger is either a 
6c440 42 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54  BEFORE or an AFT
6c450 45 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65  ER trigger.  The
6c460 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74   following const
6c470 61 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e  ants.** determin
6c480 65 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20  e which. .**.** 
6c490 49 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c  If there are mul
6c4a0 74 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20  tiple triggers, 
6c4b0 79 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d  you might of som
6c4c0 65 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d  e BEFORE and som
6c4d0 65 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74  e AFTER..** In t
6c4e0 68 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63  hat cases, the c
6c4f0 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63  onstants below c
6c500 61 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74  an be ORed toget
6c510 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  her..*/.#define 
6c520 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20  TRIGGER_BEFORE  
6c530 31 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45  1.#define TRIGGE
6c540 52 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a  R_AFTER   2../*.
6c550 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f   * An instance o
6c560 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72  f struct Trigger
6c570 53 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20  Step is used to 
6c580 73 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53  store a single S
6c590 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20  QL statement. * 
6c5a0 74 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f  that is a part o
6c5b0 66 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67  f a trigger-prog
6c5c0 72 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74  ram. . *. * Inst
6c5d0 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20  ances of struct 
6c5e0 54 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20  TriggerStep are 
6c5f0 73 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67  stored in a sing
6c600 6c 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28  ly linked list (
6c610 6c 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20  linked. * using 
6c620 74 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62  the "pNext" memb
6c630 65 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62  er) referenced b
6c640 79 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74  y the "step_list
6c650 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20  " member of the 
6c660 0a 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73  . * associated s
6c670 74 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e  truct Trigger in
6c680 73 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73  stance. The firs
6c690 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  t element of the
6c6a0 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a   linked list is.
6c6b0 20 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65   * the first ste
6c6c0 70 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  p of the trigger
6c6d0 2d 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a  -program.. * . *
6c6e0 20 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72   The "op" member
6c6f0 20 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68   indicates wheth
6c700 65 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45  er this is a "DE
6c710 4c 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c  LETE", "INSERT",
6c720 20 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20   "UPDATE" or. * 
6c730 22 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65  "SELECT" stateme
6c740 6e 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73  nt. The meanings
6c750 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65   of the other me
6c760 6d 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69  mbers is determi
6c770 6e 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76  ned by the . * v
6c780 61 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20  alue of "op" as 
6c790 66 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28  follows:. *. * (
6c7a0 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29  op == TK_INSERT)
6c7b0 0a 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e  . * orconf    ->
6c7c0 20 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43   stores the ON C
6c7d0 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68  ONFLICT algorith
6c7e0 6d 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d  m. * pSelect   -
6c7f0 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  > If this is an 
6c800 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20  INSERT INTO ... 
6c810 53 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65  SELECT ... state
6c820 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20  ment, then. *   
6c830 20 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20             this 
6c840 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72  stores a pointer
6c850 20 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73   to the SELECT s
6c860 74 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77  tatement. Otherw
6c870 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72  ise NULL.. * tar
6c880 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65  get    -> A toke
6c890 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75  n holding the qu
6c8a0 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65  oted name of the
6c8b0 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74   table to insert
6c8c0 20 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c   into.. * pExprL
6c8d0 69 73 74 20 2d 3e 20 49 66 20 74 68 69 73 20 69  ist -> If this i
6c8e0 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f  s an INSERT INTO
6c8f0 20 2e 2e 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20   ... VALUES ... 
6c900 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a  statement, then.
6c910 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
6c920 74 68 69 73 20 73 74 6f 72 65 73 20 76 61 6c 75  this stores valu
6c930 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  es to be inserte
6c940 64 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c  d. Otherwise NUL
6c950 4c 2e 0a 20 2a 20 70 49 64 4c 69 73 74 20 20 20  L.. * pIdList   
6c960 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  -> If this is an
6c970 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e   INSERT INTO ...
6c980 20 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e   (<column-names>
6c990 29 20 56 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a  ) VALUES ... . *
6c9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
6c9b0 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68  atement, then th
6c9c0 69 73 20 73 74 6f 72 65 73 20 74 68 65 20 63 6f  is stores the co
6c9d0 6c 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65  lumn-names to be
6c9e0 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  . *             
6c9f0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a   inserted into..
6ca00 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f   *. * (op == TK_
6ca10 44 45 4c 45 54 45 29 0a 20 2a 20 74 61 72 67 65  DELETE). * targe
6ca20 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20  t    -> A token 
6ca30 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 6f 74  holding the quot
6ca40 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  ed name of the t
6ca50 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 66  able to delete f
6ca60 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 20  rom.. * pWhere  
6ca70 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63    -> The WHERE c
6ca80 6c 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 4c  lause of the DEL
6ca90 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66  ETE statement if
6caa0 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65   one is specifie
6cab0 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  d.. *           
6cac0 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c     Otherwise NUL
6cad0 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d  L.. * . * (op ==
6cae0 20 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 74   TK_UPDATE). * t
6caf0 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f  arget    -> A to
6cb00 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ken holding the 
6cb10 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74  quoted name of t
6cb20 68 65 20 74 61 62 6c 65 20 74 6f 20 75 70 64 61  he table to upda
6cb30 74 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a 20 70  te rows of.. * p
6cb40 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20  Where    -> The 
6cb50 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20  WHERE clause of 
6cb60 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65  the UPDATE state
6cb70 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73  ment if one is s
6cb80 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20  pecified.. *    
6cb90 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77            Otherw
6cba0 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 45 78  ise NULL.. * pEx
6cbb0 70 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 73 74  prList -> A list
6cbc0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20   of the columns 
6cbd0 74 6f 20 75 70 64 61 74 65 20 61 6e 64 20 74 68  to update and th
6cbe0 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f  e expressions to
6cbf0 20 75 70 64 61 74 65 0a 20 2a 20 20 20 20 20 20   update. *      
6cc00 20 20 20 20 20 20 20 20 74 68 65 6d 20 74 6f 2e          them to.
6cc10 20 53 65 65 20 73 71 6c 69 74 65 33 55 70 64 61   See sqlite3Upda
6cc20 74 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 74 69  te() documentati
6cc30 6f 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 73 22  on of "pChanges"
6cc40 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  . *             
6cc50 20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 0a 20   argument.. * . 
6cc60 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65  */.struct Trigge
6cc70 72 53 74 65 70 20 7b 0a 20 20 75 38 20 6f 70 3b  rStep {.  u8 op;
6cc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6cc90 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45  * One of TK_DELE
6cca0 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54  TE, TK_UPDATE, T
6ccb0 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 45 4c  K_INSERT, TK_SEL
6ccc0 45 43 54 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f  ECT */.  u8 orco
6ccd0 6e 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  nf;           /*
6cce0 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 74 63   OE_Rollback etc
6ccf0 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  . */.  Trigger *
6cd00 70 54 72 69 67 3b 20 20 20 20 20 20 2f 2a 20 54  pTrig;      /* T
6cd10 68 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20  he trigger that 
6cd20 74 68 69 73 20 73 74 65 70 20 69 73 20 61 20 70  this step is a p
6cd30 61 72 74 20 6f 66 20 2a 2f 0a 20 20 53 65 6c 65  art of */.  Sele
6cd40 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20  ct *pSelect;    
6cd50 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61 74 6d   /* SELECT statm
6cd60 65 6e 74 20 6f 72 20 52 48 53 20 6f 66 20 49 4e  ent or RHS of IN
6cd70 53 45 52 54 20 49 4e 54 4f 20 2e 2e 20 53 45 4c  SERT INTO .. SEL
6cd80 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 54 6f 6b  ECT ... */.  Tok
6cd90 65 6e 20 74 61 72 67 65 74 3b 20 20 20 20 20 20  en target;      
6cda0 20 20 2f 2a 20 54 61 72 67 65 74 20 74 61 62 6c    /* Target tabl
6cdb0 65 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 50  e for DELETE, UP
6cdc0 44 41 54 45 2c 20 49 4e 53 45 52 54 20 2a 2f 0a  DATE, INSERT */.
6cdd0 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20    Expr *pWhere; 
6cde0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
6cdf0 45 52 45 20 63 6c 61 75 73 65 20 66 6f 72 20 44  ERE clause for D
6ce00 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20  ELETE or UPDATE 
6ce10 73 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 4c  steps */.  ExprL
6ce20 69 73 74 20 2a 70 45 78 70 72 4c 69 73 74 3b 20  ist *pExprList; 
6ce30 2f 2a 20 53 45 54 20 63 6c 61 75 73 65 20 66 6f  /* SET clause fo
6ce40 72 20 55 50 44 41 54 45 2e 20 20 56 41 4c 55 45  r UPDATE.  VALUE
6ce50 53 20 63 6c 61 75 73 65 20 66 6f 72 20 49 4e 53  S clause for INS
6ce60 45 52 54 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20  ERT */.  IdList 
6ce70 2a 70 49 64 4c 69 73 74 3b 20 20 20 20 20 2f 2a  *pIdList;     /*
6ce80 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f   Column names fo
6ce90 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 54 72  r INSERT */.  Tr
6cea0 69 67 67 65 72 53 74 65 70 20 2a 70 4e 65 78 74  iggerStep *pNext
6ceb0 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68  ;  /* Next in th
6cec0 65 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20  e link-list */. 
6ced0 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4c   TriggerStep *pL
6cee0 61 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c  ast;  /* Last el
6cef0 65 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69  ement in link-li
6cf00 73 74 2e 20 56 61 6c 69 64 20 66 6f 72 20 31 73  st. Valid for 1s
6cf10 74 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d  t elem only */.}
6cf20 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  ;../*.** The fol
6cf30 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
6cf40 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d   contains inform
6cf50 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68  ation used by th
6cf60 65 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a  e sqliteFix....*
6cf70 2a 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68  * routines as th
6cf80 65 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73  ey walk the pars
6cf90 65 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64  e tree to make d
6cfa0 61 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63  atabase referenc
6cfb0 65 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20  es.** explicit. 
6cfc0 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72   .*/.typedef str
6cfd0 75 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69  uct DbFixer DbFi
6cfe0 78 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69  xer;.struct DbFi
6cff0 78 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70  xer {.  Parse *p
6d000 50 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54  Parse;      /* T
6d010 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
6d020 78 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61  xt.  Error messa
6d030 67 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65  ges written here
6d040 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
6d050 20 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b   *zDb;    /* Mak
6d060 65 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63  e sure all objec
6d070 74 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64  ts are contained
6d080 20 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73   in this databas
6d090 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
6d0a0 72 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79  r *zType;  /* Ty
6d0b0 70 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69  pe of the contai
6d0c0 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65  ner - used for e
6d0d0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f  rror messages */
6d0e0 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a  .  const Token *
6d0f0 70 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f  pName; /* Name o
6d100 66 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20  f the container 
6d110 2d 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72  - used for error
6d120 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a   messages */.};.
6d130 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74  ./*.** An object
6d140 65 64 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d  ed used to accum
6d150 75 6c 61 74 65 20 74 68 65 20 74 65 78 74 20 6f  ulate the text o
6d160 66 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65  f a string where
6d170 20 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65   we.** do not ne
6d180 63 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68  cessarily know h
6d190 6f 77 20 62 69 67 20 74 68 65 20 73 74 72 69 6e  ow big the strin
6d1a0 67 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65  g will be in the
6d1b0 20 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20   end..*/.struct 
6d1c0 53 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c  StrAccum {.  sql
6d1d0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
6d1e0 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61    /* Optional da
6d1f0 74 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61  tabase for looka
6d200 73 69 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55  side.  Can be NU
6d210 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42  LL */.  char *zB
6d220 61 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ase;         /* 
6d230 41 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f  A base allocatio
6d240 6e 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c  n.  Not from mal
6d250 6c 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a  loc. */.  char *
6d260 7a 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f  zText;         /
6d270 2a 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c  * The string col
6d280 6c 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f  lected so far */
6d290 0a 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20  .  int  nChar;  
6d2a0 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
6d2b0 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20  h of the string 
6d2c0 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20  so far */.  int 
6d2d0 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20   nAlloc;        
6d2e0 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70   /* Amount of sp
6d2f0 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  ace allocated in
6d300 20 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20   zText */.  int 
6d310 20 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20   mxAlloc;       
6d320 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f   /* Maximum allo
6d330 77 65 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74  wed string lengt
6d340 68 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c  h */.  u8   mall
6d350 6f 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42  ocFailed;   /* B
6d360 65 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61  ecomes true if a
6d370 6e 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ny memory alloca
6d380 74 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20  tion fails */.  
6d390 75 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20  u8   useMalloc; 
6d3a0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
6d3b0 7a 54 65 78 74 20 69 73 20 65 6e 6c 61 72 67 65  zText is enlarge
6d3c0 61 62 6c 65 20 75 73 69 6e 67 20 72 65 61 6c 6c  able using reall
6d3d0 6f 63 20 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f  oc */.  u8   too
6d3e0 42 69 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Big;         /* 
6d3f0 42 65 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20  Becomes true if 
6d400 73 74 72 69 6e 67 20 73 69 7a 65 20 65 78 63 65  string size exce
6d410 65 64 73 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b  eds limits */.};
6d420 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65  ../*.** A pointe
6d430 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74  r to this struct
6d440 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 63  ure is used to c
6d450 6f 6d 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72  ommunicate infor
6d460 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73  mation.** from s
6d470 71 6c 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f  qlite3Init and O
6d480 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e  P_ParseSchema in
6d490 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e  to the sqlite3In
6d4a0 69 74 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74  itCallback..*/.t
6d4b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a  ypedef struct {.
6d4c0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
6d4d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
6d4e0 61 62 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74  abase being init
6d4f0 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74  ialized */.  int
6d500 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
6d510 20 2f 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64   /* 0 for main d
6d520 61 74 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20  atabase.  1 for 
6d530 54 45 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54  TEMP, 2.. for AT
6d540 54 41 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72  TACHed */.  char
6d550 20 2a 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20   **pzErrMsg;    
6d560 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65  /* Error message
6d570 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a   stored here */.
6d580 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
6d590 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
6d5a0 63 6f 64 65 20 73 74 6f 72 65 64 20 68 65 72 65  code stored here
6d5b0 20 2a 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a   */.} InitData;.
6d5c0 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65  ./*.** Structure
6d5d0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62   containing glob
6d5e0 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  al configuration
6d5f0 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 53 51   data for the SQ
6d600 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a  Lite library..**
6d610 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75  .** This structu
6d620 72 65 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73  re also contains
6d630 20 73 6f 6d 65 20 73 74 61 74 65 20 69 6e 66 6f   some state info
6d640 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75  rmation..*/.stru
6d650 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67  ct Sqlite3Config
6d660 20 7b 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61   {.  int bMemsta
6d670 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
6d680 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
6d690 6f 20 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20  o enable memory 
6d6a0 73 74 61 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20  status */.  int 
6d6b0 62 43 6f 72 65 4d 75 74 65 78 3b 20 20 20 20 20  bCoreMutex;     
6d6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6d6d0 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20   True to enable 
6d6e0 63 6f 72 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f  core mutexing */
6d6f0 0a 20 20 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65  .  int bFullMute
6d700 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
6d710 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
6d720 65 6e 61 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65  enable full mute
6d730 78 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78  xing */.  int mx
6d740 53 74 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20  Strlen;         
6d750 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
6d760 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65  aximum string le
6d770 6e 67 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  ngth */.  int sz
6d780 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20  Lookaside;      
6d790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
6d7a0 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65  efault lookaside
6d7b0 20 62 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a   buffer size */.
6d7c0 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65    int nLookaside
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 44 65 66 61 75 6c 74 20 6c      /* Default l
6d7f0 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20  ookaside buffer 
6d800 63 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74  count */.  sqlit
6d810 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d  e3_mem_methods m
6d820 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
6d830 4c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  Low-level memory
6d840 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65   allocation inte
6d850 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74  rface */.  sqlit
6d860 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
6d870 20 6d 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20   mutex;      /* 
6d880 4c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20  Low-level mutex 
6d890 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73  interface */.  s
6d8a0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
6d8b0 74 68 6f 64 73 20 70 63 61 63 68 65 3b 20 20 20  thods pcache;   
6d8c0 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61   /* Low-level pa
6d8d0 67 65 2d 63 61 63 68 65 20 69 6e 74 65 72 66 61  ge-cache interfa
6d8e0 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48  ce */.  void *pH
6d8f0 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  eap;            
6d900 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61            /* Hea
6d910 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20  p storage space 
6d920 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 61 70 3b 20  */.  int nHeap; 
6d930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d940 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
6d950 66 20 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69  f pHeap[] */.  i
6d960 6e 74 20 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b  nt mnReq, mxReq;
6d970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d980 20 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20   /* Min and max 
6d990 68 65 61 70 20 72 65 71 75 65 73 74 73 20 73 69  heap requests si
6d9a0 7a 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  zes */.  void *p
6d9b0 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20  Scratch;        
6d9c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63             /* Sc
6d9d0 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a  ratch memory */.
6d9e0 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b    int szScratch;
6d9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da00 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65      /* Size of e
6da10 61 63 68 20 73 63 72 61 74 63 68 20 62 75 66 66  ach scratch buff
6da20 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72  er */.  int nScr
6da30 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20  atch;           
6da40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
6da50 62 65 72 20 6f 66 20 73 63 72 61 74 63 68 20 62  ber of scratch b
6da60 75 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64  uffers */.  void
6da70 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20   *pPage;        
6da80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6da90 20 50 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f   Page cache memo
6daa0 72 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61  ry */.  int szPa
6dab0 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
6dac0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
6dad0 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 69  e of each page i
6dae0 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69  n pPage[] */.  i
6daf0 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20  nt nPage;       
6db00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6db10 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
6db20 67 65 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a  ges in pPage[] *
6db30 2f 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65 72  /.  int mxParser
6db40 53 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20  Stack;          
6db50 20 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d        /* maximum
6db60 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61   depth of the pa
6db70 72 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20  rser stack */.  
6db80 69 6e 74 20 73 68 61 72 65 64 43 61 63 68 65 45  int sharedCacheE
6db90 6e 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 20  nabled;         
6dba0 20 20 2f 2a 20 74 72 75 65 20 69 66 20 73 68 61    /* true if sha
6dbb0 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 65  red-cache mode e
6dbc0 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 2f 2a 20 54  nabled */.  /* T
6dbd0 68 65 20 61 62 6f 76 65 20 6d 69 67 68 74 20 62  he above might b
6dbe0 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  e initialized to
6dbf0 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 68 65 20   non-zero.  The 
6dc00 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 64 20 74  following need t
6dc10 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 69 6e  o always.  ** in
6dc20 69 74 69 61 6c 6c 79 20 62 65 20 7a 65 72 6f 2c  itially be zero,
6dc30 20 68 6f 77 65 76 65 72 2e 20 2a 2f 0a 20 20 69   however. */.  i
6dc40 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20  nt isInit;      
6dc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dc60 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 69   /* True after i
6dc70 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61  nitialization ha
6dc80 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20  s finished */.  
6dc90 69 6e 74 20 69 6e 50 72 6f 67 72 65 73 73 3b 20  int inProgress; 
6dca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dcb0 20 20 2f 2a 20 54 72 75 65 20 77 68 69 6c 65 20    /* True while 
6dcc0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69  initialization i
6dcd0 6e 20 70 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20  n progress */.  
6dce0 69 6e 74 20 69 73 4d 75 74 65 78 49 6e 69 74 3b  int isMutexInit;
6dcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dd00 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20    /* True after 
6dd10 6d 75 74 65 78 65 73 20 61 72 65 20 69 6e 69 74  mutexes are init
6dd20 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74  ialized */.  int
6dd30 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20   isMallocInit;  
6dd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6dd50 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c  * True after mal
6dd60 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  loc is initializ
6dd70 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 50 43  ed */.  int isPC
6dd80 61 63 68 65 49 6e 69 74 3b 20 20 20 20 20 20 20  acheInit;       
6dd90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
6dda0 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69  e after malloc i
6ddb0 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f  s initialized */
6ddc0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
6ddd0 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 20 20   *pInitMutex;   
6dde0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73       /* Mutex us
6ddf0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e  ed by sqlite3_in
6de00 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a 20 20  itialize() */.  
6de10 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 74 65  int nRefInitMute
6de20 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
6de30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75    /* Number of u
6de40 73 65 72 73 20 6f 66 20 70 49 6e 69 74 4d 75 74  sers of pInitMut
6de50 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ex */.};../*.** 
6de60 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20  Context pointer 
6de70 70 61 73 73 65 64 20 64 6f 77 6e 20 74 68 72 6f  passed down thro
6de80 75 67 68 20 74 68 65 20 74 72 65 65 2d 77 61 6c  ugh the tree-wal
6de90 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 61 6c  k..*/.struct Wal
6dea0 6b 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 45  ker {.  int (*xE
6deb0 78 70 72 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c  xprCallback)(Wal
6dec0 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b 20 20 20  ker*, Expr*);   
6ded0 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f    /* Callback fo
6dee0 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f  r expressions */
6def0 0a 20 20 69 6e 74 20 28 2a 78 53 65 6c 65 63 74  .  int (*xSelect
6df00 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72  Callback)(Walker
6df10 2a 2c 53 65 6c 65 63 74 2a 29 3b 20 20 2f 2a 20  *,Select*);  /* 
6df20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 53 45 4c  Callback for SEL
6df30 45 43 54 73 20 2a 2f 0a 20 20 50 61 72 73 65 20  ECTs */.  Parse 
6df40 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20  *pParse;        
6df50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6df60 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
6df70 6e 74 65 78 74 2e 20 20 2a 2f 0a 20 20 75 6e 69  ntext.  */.  uni
6df80 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  on {            
6df90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dfa0 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20         /* Extra 
6dfb0 64 61 74 61 20 66 6f 72 20 63 61 6c 6c 62 61 63  data for callbac
6dfc0 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e  k */.    NameCon
6dfd0 74 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 20 20  text *pNC;      
6dfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dff0 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20 63 6f      /* Naming co
6e000 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 69 6e 74  ntext */.    int
6e010 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
6e020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e030 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67          /* Integ
6e040 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20  er value */.  } 
6e050 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 77 61 72  u;.};../* Forwar
6e060 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a  d declarations *
6e070 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
6e080 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b   int sqlite3Walk
6e090 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 45 78  Expr(Walker*, Ex
6e0a0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
6e0b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6e0c0 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c  WalkExprList(Wal
6e0d0 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29  ker*, ExprList*)
6e0e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6e0f0 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b   int sqlite3Walk
6e100 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a 2c 20  Select(Walker*, 
6e110 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45  Select*);.SQLITE
6e120 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6e130 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78  ite3WalkSelectEx
6e140 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65  pr(Walker*, Sele
6e150 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ct*);.SQLITE_PRI
6e160 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6e170 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57  WalkSelectFrom(W
6e180 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29  alker*, Select*)
6e190 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  ;../*.** Return 
6e1a0 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 70 61  code from the pa
6e1b0 72 73 65 2d 74 72 65 65 20 77 61 6c 6b 69 6e 67  rse-tree walking
6e1c0 20 70 72 69 6d 69 74 69 76 65 73 20 61 6e 64 20   primitives and 
6e1d0 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c 62 61 63  their.** callbac
6e1e0 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57  ks..*/.#define W
6e1f0 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20 30  RC_Continue    0
6e200 20 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 64     /* Continue d
6e210 6f 77 6e 20 69 6e 74 6f 20 63 68 69 6c 64 72 65  own into childre
6e220 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43  n */.#define WRC
6e230 5f 50 72 75 6e 65 20 20 20 20 20 20 20 31 20 20  _Prune       1  
6e240 20 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 64 72 65   /* Omit childre
6e250 6e 20 62 75 74 20 63 6f 6e 74 69 6e 75 65 20 77  n but continue w
6e260 61 6c 6b 69 6e 67 20 73 69 62 6c 69 6e 67 73 20  alking siblings 
6e270 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 41  */.#define WRC_A
6e280 62 6f 72 74 20 20 20 20 20 20 20 32 20 20 20 2f  bort       2   /
6e290 2a 20 41 62 61 6e 64 6f 6e 20 74 68 65 20 74 72  * Abandon the tr
6e2a0 65 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f 2a 0a 2a  ee walk */../*.*
6e2b0 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70  * Assuming zIn p
6e2c0 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72  oints to the fir
6e2d0 73 74 20 62 79 74 65 20 6f 66 20 61 20 55 54 46  st byte of a UTF
6e2e0 2d 38 20 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a  -8 character,.**
6e2f0 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20   advance zIn to 
6e300 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72  point to the fir
6e310 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e  st byte of the n
6e320 65 78 74 20 55 54 46 2d 38 20 63 68 61 72 61 63  ext UTF-8 charac
6e330 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ter..*/.#define 
6e340 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
6e350 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 20 20  (zIn) {         
6e360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
6e370 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29  .  if( (*(zIn++)
6e380 29 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20  )>=0xc0 ){      
6e390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e3a0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68          \.    wh
6e3b0 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63  ile( (*zIn & 0xc
6e3c0 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b  0)==0x80 ){ zIn+
6e3d0 2b 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20  +; }            
6e3e0 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
6e3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e410 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f            \.}../
6e420 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
6e430 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d 61 63  CORRUPT_BKPT mac
6e440 72 6f 20 63 61 6e 20 62 65 20 65 69 74 68 65 72  ro can be either
6e450 20 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72   a constant (for
6e460 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62   production.** b
6e470 75 69 6c 64 73 29 20 6f 72 20 61 20 66 75 6e 63  uilds) or a func
6e480 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64  tion call (for d
6e490 65 62 75 67 67 69 6e 67 29 2e 20 20 49 66 20 69  ebugging).  If i
6e4a0 74 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20  t is a function 
6e4b0 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f  call,.** it allo
6e4c0 77 73 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20  ws the operator 
6e4d0 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f  to set a breakpo
6e4e0 69 6e 74 20 61 74 20 74 68 65 20 73 70 6f 74 20  int at the spot 
6e4f0 77 68 65 72 65 20 64 61 74 61 62 61 73 65 0a 2a  where database.*
6e500 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20  * corruption is 
6e510 66 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a  first detected..
6e520 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
6e530 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52  _DEBUG.SQLITE_PR
6e540 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
6e550 74 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29  te3Corrupt(void)
6e560 3b 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ;.# define SQLIT
6e570 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 73  E_CORRUPT_BKPT s
6e580 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 29 0a  qlite3Corrupt().
6e590 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
6e5a0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
6e5b0 50 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  PT SQLITE_CORRUP
6e5c0 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  T.#endif../*.** 
6e5d0 54 68 65 20 63 74 79 70 65 2e 68 20 68 65 61 64  The ctype.h head
6e5e0 65 72 20 69 73 20 6e 65 65 64 65 64 20 66 6f 72  er is needed for
6e5f0 20 6e 6f 6e 2d 41 53 43 49 49 20 73 79 73 74 65   non-ASCII syste
6e600 6d 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 0a  ms.  It is also.
6e610 2a 2a 20 6e 65 65 64 65 64 20 62 79 20 46 54 53  ** needed by FTS
6e620 33 20 77 68 65 6e 20 46 54 53 33 20 69 73 20 69  3 when FTS3 is i
6e630 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 61  ncluded in the a
6e640 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  malgamation..*/.
6e650 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
6e660 49 54 45 5f 41 53 43 49 49 29 20 7c 7c 20 5c 0a  ITE_ASCII) || \.
6e670 20 20 20 20 28 64 65 66 69 6e 65 64 28 53 51 4c      (defined(SQL
6e680 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
6e690 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49   && defined(SQLI
6e6a0 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 29  TE_AMALGAMATION)
6e6b0 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c 63 74 79  ).# include <cty
6e6c0 70 65 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a  pe.h>.#endif../*
6e6d0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
6e6e0 67 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 20 74  g macros mimic t
6e6f0 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72  he standard libr
6e700 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f  ary functions to
6e710 75 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 73 70  upper(),.** issp
6e720 61 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28 29  ace(), isalnum()
6e730 2c 20 69 73 64 69 67 69 74 28 29 20 61 6e 64 20  , isdigit() and 
6e740 69 73 78 64 69 67 69 74 28 29 2c 20 72 65 73 70  isxdigit(), resp
6e750 65 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a  ectively. The.**
6e760 20 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e 73   sqlite versions
6e770 20 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20 41   only work for A
6e780 53 43 49 49 20 63 68 61 72 61 63 74 65 72 73 2c  SCII characters,
6e790 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c   regardless of l
6e7a0 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  ocale..*/.#ifdef
6e7b0 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20   SQLITE_ASCII.# 
6e7c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f  define sqlite3To
6e7d0 75 70 70 65 72 28 78 29 20 20 28 28 78 29 26 7e  upper(x)  ((x)&~
6e7e0 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70  (sqlite3CtypeMap
6e7f0 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  [(unsigned char)
6e800 28 78 29 5d 26 30 78 32 30 29 29 0a 23 20 64 65  (x)]&0x20)).# de
6e810 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70  fine sqlite3Issp
6e820 61 63 65 28 78 29 20 20 20 28 73 71 6c 69 74 65  ace(x)   (sqlite
6e830 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67  3CtypeMap[(unsig
6e840 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78  ned char)(x)]&0x
6e850 30 31 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  01).# define sql
6e860 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20  ite3Isalnum(x)  
6e870 20 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61   (sqlite3CtypeMa
6e880 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  p[(unsigned char
6e890 29 28 78 29 5d 26 30 78 30 36 29 0a 23 20 64 65  )(x)]&0x06).# de
6e8a0 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c  fine sqlite3Isal
6e8b0 70 68 61 28 78 29 20 20 20 28 73 71 6c 69 74 65  pha(x)   (sqlite
6e8c0 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67  3CtypeMap[(unsig
6e8d0 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78  ned char)(x)]&0x
6e8e0 30 32 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  02).# define sql
6e8f0 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20  ite3Isdigit(x)  
6e900 20 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61   (sqlite3CtypeMa
6e910 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  p[(unsigned char
6e920 29 28 78 29 5d 26 30 78 30 34 29 0a 23 20 64 65  )(x)]&0x04).# de
6e930 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 64  fine sqlite3Isxd
6e940 69 67 69 74 28 78 29 20 20 28 73 71 6c 69 74 65  igit(x)  (sqlite
6e950 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67  3CtypeMap[(unsig
6e960 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78  ned char)(x)]&0x
6e970 30 38 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  08).# define sql
6e980 69 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20 20  ite3Tolower(x)  
6e990 20 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f   (sqlite3UpperTo
6e9a0 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20  Lower[(unsigned 
6e9b0 63 68 61 72 29 28 78 29 5d 29 0a 23 65 6c 73 65  char)(x)]).#else
6e9c0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6e9d0 33 54 6f 75 70 70 65 72 28 78 29 20 20 20 74 6f  3Toupper(x)   to
6e9e0 75 70 70 65 72 28 28 75 6e 73 69 67 6e 65 64 20  upper((unsigned 
6e9f0 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69  char)(x)).# defi
6ea00 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  ne sqlite3Isspac
6ea10 65 28 78 29 20 20 20 69 73 73 70 61 63 65 28 28  e(x)   isspace((
6ea20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6ea30 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  )).# define sqli
6ea40 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20  te3Isalnum(x)   
6ea50 69 73 61 6c 6e 75 6d 28 28 75 6e 73 69 67 6e 65  isalnum((unsigne
6ea60 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65  d char)(x)).# de
6ea70 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c  fine sqlite3Isal
6ea80 70 68 61 28 78 29 20 20 20 69 73 61 6c 70 68 61  pha(x)   isalpha
6ea90 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  ((unsigned char)
6eaa0 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71  (x)).# define sq
6eab0 6c 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20  lite3Isdigit(x) 
6eac0 20 20 69 73 64 69 67 69 74 28 28 75 6e 73 69 67    isdigit((unsig
6ead0 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20  ned char)(x)).# 
6eae0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73  define sqlite3Is
6eaf0 78 64 69 67 69 74 28 78 29 20 20 69 73 78 64 69  xdigit(x)  isxdi
6eb00 67 69 74 28 28 75 6e 73 69 67 6e 65 64 20 63 68  git((unsigned ch
6eb10 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65  ar)(x)).# define
6eb20 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28   sqlite3Tolower(
6eb30 78 29 20 20 20 74 6f 6c 6f 77 65 72 28 28 75 6e  x)   tolower((un
6eb40 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29  signed char)(x))
6eb50 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
6eb60 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e  nternal function
6eb70 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53   prototypes.*/.S
6eb80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6eb90 74 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  t sqlite3StrICmp
6eba0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63  (const char *, c
6ebb0 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51  onst char *);.SQ
6ebc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6ebd0 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72   sqlite3IsNumber
6ebe0 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e  (const char*, in
6ebf0 74 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f  t*, u8);.SQLITE_
6ec00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6ec10 74 65 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73  te3Strlen30(cons
6ec20 74 20 63 68 61 72 2a 29 3b 0a 23 64 65 66 69 6e  t char*);.#defin
6ec30 65 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  e sqlite3StrNICm
6ec40 70 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63  p sqlite3_strnic
6ec50 6d 70 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  mp..SQLITE_PRIVA
6ec60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61  TE int sqlite3Ma
6ec70 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 3b 0a  llocInit(void);.
6ec80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6ec90 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  oid sqlite3Mallo
6eca0 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49  cEnd(void);.SQLI
6ecb0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6ecc0 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69  *sqlite3Malloc(i
6ecd0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6ece0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
6ecf0 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 29  3MallocZero(int)
6ed00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6ed10 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62   void *sqlite3Db
6ed20 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74  MallocZero(sqlit
6ed30 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  e3*, int);.SQLIT
6ed40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
6ed50 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
6ed60 61 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  aw(sqlite3*, int
6ed70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6ed80 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44  E char *sqlite3D
6ed90 62 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 2a  bStrDup(sqlite3*
6eda0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53  ,const char*);.S
6edb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
6edc0 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72  ar *sqlite3DbStr
6edd0 4e 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f  NDup(sqlite3*,co
6ede0 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b  nst char*, int);
6edf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ee00 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61  void *sqlite3Rea
6ee10 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29  lloc(void*, int)
6ee20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6ee30 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62   void *sqlite3Db
6ee40 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71  ReallocOrFree(sq
6ee50 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c  lite3 *, void *,
6ee60 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6ee70 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
6ee80 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c  te3DbRealloc(sql
6ee90 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20  ite3 *, void *, 
6eea0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6eeb0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6eec0 33 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 2a  3DbFree(sqlite3*
6eed0 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  , void*);.SQLITE
6eee0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6eef0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76  ite3MallocSize(v
6ef00 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  oid*);.SQLITE_PR
6ef10 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6ef20 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71  3DbMallocSize(sq
6ef30 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a  lite3*, void*);.
6ef40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6ef50 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61  oid *sqlite3Scra
6ef60 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a  tchMalloc(int);.
6ef70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6ef80 6f 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74  oid sqlite3Scrat
6ef90 63 68 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53  chFree(void*);.S
6efa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6efb0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 4d  id *sqlite3PageM
6efc0 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49  alloc(int);.SQLI
6efd0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6efe0 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
6eff0 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  void*);.SQLITE_P
6f000 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6f010 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74  te3MemSetDefault
6f020 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50  (void);.SQLITE_P
6f030 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6f040 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48  te3BenignMallocH
6f050 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 76 6f  ooks(void (*)(vo
6f060 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f  id), void (*)(vo
6f070 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  id));.SQLITE_PRI
6f080 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6f090 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f 69 64  MemoryAlarm(void
6f0a0 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 6c 69   (*)(void*, sqli
6f0b0 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 29 2c  te3_int64, int),
6f0c0 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f   void*, sqlite3_
6f0d0 69 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4f  int64);../*.** O
6f0e0 6e 20 73 79 73 74 65 6d 73 20 77 69 74 68 20 61  n systems with a
6f0f0 6d 70 6c 65 20 73 74 61 63 6b 20 73 70 61 63 65  mple stack space
6f100 20 61 6e 64 20 74 68 61 74 20 73 75 70 70 6f 72   and that suppor
6f110 74 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61 6b 65  t alloca(), make
6f120 0a 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c 6f 63  .** use of alloc
6f130 61 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70  a() to obtain sp
6f140 61 63 65 20 66 6f 72 20 6c 61 72 67 65 20 61 75  ace for large au
6f150 74 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74 73 2e  tomatic objects.
6f160 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a    By default,.**
6f170 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 72   obtain space fr
6f180 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a  om malloc()..**.
6f190 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 28 29 20  ** The alloca() 
6f1a0 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 72 65  routine never re
6f1b0 74 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 54 68 69  turns NULL.  Thi
6f1c0 73 20 77 69 6c 6c 20 63 61 75 73 65 20 63 6f 64  s will cause cod
6f1d0 65 20 70 61 74 68 73 0a 2a 2a 20 74 68 61 74 20  e paths.** that 
6f1e0 64 65 61 6c 20 77 69 74 68 20 73 71 6c 69 74 65  deal with sqlite
6f1f0 33 53 74 61 63 6b 41 6c 6c 6f 63 28 29 20 66 61  3StackAlloc() fa
6f200 69 6c 75 72 65 73 20 74 6f 20 62 65 20 75 6e 72  ilures to be unr
6f210 65 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66  eachable..*/.#if
6f220 64 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f 41  def SQLITE_USE_A
6f230 4c 4c 4f 43 41 0a 23 20 64 65 66 69 6e 65 20 73  LLOCA.# define s
6f240 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63  qlite3StackAlloc
6f250 52 61 77 28 44 2c 4e 29 20 20 20 61 6c 6c 6f 63  Raw(D,N)   alloc
6f260 61 28 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71  a(N).# define sq
6f270 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a  lite3StackAllocZ
6f280 65 72 6f 28 44 2c 4e 29 20 20 6d 65 6d 73 65 74  ero(D,N)  memset
6f290 28 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 2c 20 4e  (alloca(N), 0, N
6f2a0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
6f2b0 65 33 53 74 61 63 6b 46 72 65 65 28 44 2c 50 29  e3StackFree(D,P)
6f2c0 20 20 20 20 20 20 20 0a 23 65 6c 73 65 0a 23 20         .#else.# 
6f2d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74  define sqlite3St
6f2e0 61 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29  ackAllocRaw(D,N)
6f2f0 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c     sqlite3DbMall
6f300 6f 63 52 61 77 28 44 2c 4e 29 0a 23 20 64 65 66  ocRaw(D,N).# def
6f310 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b  ine sqlite3Stack
6f320 41 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20  AllocZero(D,N)  
6f330 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
6f340 65 72 6f 28 44 2c 4e 29 0a 23 20 64 65 66 69 6e  ero(D,N).# defin
6f350 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72  e sqlite3StackFr
6f360 65 65 28 44 2c 50 29 20 20 20 20 20 20 20 73 71  ee(D,P)       sq
6f370 6c 69 74 65 33 44 62 46 72 65 65 28 44 2c 50 29  lite3DbFree(D,P)
6f380 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
6f390 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
6f3a0 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 52 49  MSYS3.SQLITE_PRI
6f3b0 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74  VATE const sqlit
6f3c0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a  e3_mem_methods *
6f3d0 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d  sqlite3MemGetMem
6f3e0 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 6e 64  sys3(void);.#end
6f3f0 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
6f400 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a  _ENABLE_MEMSYS5.
6f410 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
6f420 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d  onst sqlite3_mem
6f430 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65  _methods *sqlite
6f440 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76  3MemGetMemsys5(v
6f450 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 23  oid);.#endif...#
6f460 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55  ifndef SQLITE_MU
6f470 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 5f  TEX_OMIT.SQLITE_
6f480 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65  PRIVATE   sqlite
6f490 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
6f4a0 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d  *sqlite3DefaultM
6f4b0 75 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49  utex(void);.SQLI
6f4c0 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c  TE_PRIVATE   sql
6f4d0 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69  ite3_mutex *sqli
6f4e0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e  te3MutexAlloc(in
6f4f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6f500 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
6f510 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 3b  MutexInit(void);
6f520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f530 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74    int sqlite3Mut
6f540 65 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e  exEnd(void);.#en
6f550 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  dif..SQLITE_PRIV
6f560 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
6f570 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74 29 3b  tatusValue(int);
6f580 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f590 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74  void sqlite3Stat
6f5a0 75 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b  usAdd(int, int);
6f5b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f5c0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74  void sqlite3Stat
6f5d0 75 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b  usSet(int, int);
6f5e0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
6f5f0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61   int sqlite3IsNa
6f600 4e 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 4c 49  N(double);..SQLI
6f610 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f620 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28  sqlite3VXPrintf(
6f630 53 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 2c 20  StrAccum*, int, 
6f640 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f  const char*, va_
6f650 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  list);.SQLITE_PR
6f660 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
6f670 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 74  te3MPrintf(sqlit
6f680 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
6f690 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52   ...);.SQLITE_PR
6f6a0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
6f6b0 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69  te3VMPrintf(sqli
6f6c0 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
6f6d0 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49  , va_list);.SQLI
6f6e0 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
6f6f0 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66  *sqlite3MAppendf
6f700 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2c  (sqlite3*,char*,
6f710 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29  const char*,...)
6f720 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ;.#if defined(SQ
6f730 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65  LITE_TEST) || de
6f740 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
6f750 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  UG).SQLITE_PRIVA
6f760 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
6f770 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e  3DebugPrintf(con
6f780 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a  st char*, ...);.
6f790 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e  #endif.#if defin
6f7a0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a  ed(SQLITE_TEST).
6f7b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6f7c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 54 65   void *sqlite3Te
6f7d0 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f 6e 73  stTextToPtr(cons
6f7e0 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66  t char*);.#endif
6f7f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f800 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53  void sqlite3SetS
6f810 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c 20 73  tring(char **, s
6f820 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63  qlite3*, const c
6f830 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49  har*, ...);.SQLI
6f840 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f850 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
6f860 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68  Parse*, const ch
6f870 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54  ar*, ...);.SQLIT
6f880 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f890 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72  qlite3ErrorClear
6f8a0 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
6f8b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6f8c0 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 61 72  ite3Dequote(char
6f8d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f8e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b 65  TE int sqlite3Ke
6f8f0 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20  ywordCode(const 
6f900 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20  unsigned char*, 
6f910 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6f920 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6f930 52 75 6e 50 61 72 73 65 72 28 50 61 72 73 65 2a  RunParser(Parse*
6f940 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63  , const char*, c
6f950 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  har **);.SQLITE_
6f960 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6f970 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67  ite3FinishCoding
6f980 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
6f990 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6f9a0 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50  ite3GetTempReg(P
6f9b0 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
6f9c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6f9d0 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
6f9e0 67 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53  g(Parse*,int);.S
6f9f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6fa00 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  t sqlite3GetTemp
6fa10 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74  Range(Parse*,int
6fa20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6fa30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
6fa40 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 50  leaseTempRange(P
6fa50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  arse*,int,int);.
6fa60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
6fa70 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72  xpr *sqlite3Expr
6fa80 41 6c 6c 6f 63 28 73 71 6c 69 74 65 33 2a 2c 69  Alloc(sqlite3*,i
6fa90 6e 74 2c 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 2c  nt,const Token*,
6faa0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6fab0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
6fac0 65 33 45 78 70 72 28 73 71 6c 69 74 65 33 2a 2c  e3Expr(sqlite3*,
6fad0 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  int,const char*)
6fae0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6faf0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
6fb00 72 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28  rAttachSubtrees(
6fb10 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 45  sqlite3*,Expr*,E
6fb20 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c  xpr*,Expr*);.SQL
6fb30 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
6fb40 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 50   *sqlite3PExpr(P
6fb50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72  arse*, int, Expr
6fb60 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20  *, Expr*, const 
6fb70 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
6fb80 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
6fb90 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c  lite3ExprAnd(sql
6fba0 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20 45 78 70  ite3*,Expr*, Exp
6fbb0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6fbc0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
6fbd0 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61  3ExprFunction(Pa
6fbe0 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20  rse*,ExprList*, 
6fbf0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
6fc00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6fc10 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61  ite3ExprAssignVa
6fc20 72 4e 75 6d 62 65 72 28 50 61 72 73 65 2a 2c 20  rNumber(Parse*, 
6fc30 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
6fc40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6fc50 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c  te3ExprClear(sql
6fc60 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  ite3*, Expr*);.S
6fc70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6fc80 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  id sqlite3ExprDe
6fc90 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45  lete(sqlite3*, E
6fca0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
6fcb0 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a  IVATE ExprList *
6fcc0 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
6fcd0 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70  ppend(Parse*,Exp
6fce0 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b 0a 53  rList*,Expr*);.S
6fcf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6fd00 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  id sqlite3ExprLi
6fd10 73 74 53 65 74 4e 61 6d 65 28 50 61 72 73 65 2a  stSetName(Parse*
6fd20 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f 6b 65 6e  ,ExprList*,Token
6fd30 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
6fd40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6fd50 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70  te3ExprListSetSp
6fd60 61 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69  an(Parse*,ExprLi
6fd70 73 74 2a 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0a  st*,ExprSpan*);.
6fd80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6fd90 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c  oid sqlite3ExprL
6fda0 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65  istDelete(sqlite
6fdb0 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a  3*, ExprList*);.
6fdc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6fdd0 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73  nt sqlite3Init(s
6fde0 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a 2a 29  qlite3*, char**)
6fdf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6fe00 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74   int sqlite3Init
6fe10 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a 2c 20  Callback(void*, 
6fe20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63 68 61  int, char**, cha
6fe30 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r**);.SQLITE_PRI
6fe40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6fe50 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a 2c 54  3Pragma(Parse*,T
6fe60 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b  oken*,Token*,Tok
6fe70 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  en*,int);.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 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
6fea0 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33  alSchema(sqlite3
6feb0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
6fec0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6fed0 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 50  ite3BeginParse(P
6fee0 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  arse*,int);.SQLI
6fef0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6ff00 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74  sqlite3CommitInt
6ff10 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c  ernalChanges(sql
6ff20 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
6ff30 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
6ff40 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66  lite3ResultSetOf
6ff50 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 53 65  Select(Parse*,Se
6ff60 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  lect*);.SQLITE_P
6ff70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6ff80 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62  te3OpenMasterTab
6ff90 6c 65 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 29  le(Parse *, int)
6ffa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6ffb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61   void sqlite3Sta
6ffc0 72 74 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54  rtTable(Parse*,T
6ffd0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74  oken*,Token*,int
6ffe0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ,int,int,int);.S
6fff0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
70000 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  id sqlite3AddCol
70010 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e  umn(Parse*,Token
70020 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70030 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
70040 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 2a  ddNotNull(Parse*
70050 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70060 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70070 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79  te3AddPrimaryKey
70080 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73  (Parse*, ExprLis
70090 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  t*, int, int, in
700a0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
700b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
700c0 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e  ddCheckConstrain
700d0 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29  t(Parse*, Expr*)
700e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
700f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
70100 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65  ColumnType(Parse
70110 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  *,Token*);.SQLIT
70120 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70130 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74  qlite3AddDefault
70140 56 61 6c 75 65 28 50 61 72 73 65 2a 2c 45 78 70  Value(Parse*,Exp
70150 72 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  rSpan*);.SQLITE_
70160 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70170 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79  ite3AddCollateTy
70180 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e  pe(Parse*, Token
70190 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
701a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
701b0 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54  ndTable(Parse*,T
701c0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c  oken*,Token*,Sel
701d0 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  ect*);..SQLITE_P
701e0 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73  RIVATE Bitvec *s
701f0 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
70200 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f  te(u32);.SQLITE_
70210 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
70220 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69  te3BitvecTest(Bi
70230 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c  tvec*, u32);.SQL
70240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
70250 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74  sqlite3BitvecSet
70260 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a  (Bitvec*, u32);.
70270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70280 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65  oid sqlite3Bitve
70290 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c 20  cClear(Bitvec*, 
702a0 75 33 32 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c  u32, void*);.SQL
702b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
702c0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
702d0 73 74 72 6f 79 28 42 69 74 76 65 63 2a 29 3b 0a  stroy(Bitvec*);.
702e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
702f0 33 32 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  32 sqlite3Bitvec
70300 53 69 7a 65 28 42 69 74 76 65 63 2a 29 3b 0a 53  Size(Bitvec*);.S
70310 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
70320 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42  t sqlite3BitvecB
70330 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 2c 69  uiltinTest(int,i
70340 6e 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  nt*);..SQLITE_PR
70350 49 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71  IVATE RowSet *sq
70360 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28  lite3RowSetInit(
70370 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 2c  sqlite3*, void*,
70380 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a   unsigned int);.
70390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
703a0 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65  oid sqlite3RowSe
703b0 74 43 6c 65 61 72 28 52 6f 77 53 65 74 2a 29 3b  tClear(RowSet*);
703c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
703d0 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53  void sqlite3RowS
703e0 65 74 49 6e 73 65 72 74 28 52 6f 77 53 65 74 2a  etInsert(RowSet*
703f0 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50  , i64);.SQLITE_P
70400 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
70410 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77  e3RowSetTest(Row
70420 53 65 74 2a 2c 20 75 38 20 69 42 61 74 63 68 2c  Set*, u8 iBatch,
70430 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52   i64);.SQLITE_PR
70440 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
70450 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53  3RowSetNext(RowS
70460 65 74 2a 2c 20 69 36 34 2a 29 3b 0a 0a 53 51 4c  et*, i64*);..SQL
70470 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
70480 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 56 69   sqlite3CreateVi
70490 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a  ew(Parse*,Token*
704a0 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53  ,Token*,Token*,S
704b0 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b  elect*,int,int);
704c0 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
704d0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
704e0 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
704f0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
70500 54 41 42 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52  TABLE).SQLITE_PR
70510 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
70520 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
70530 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c 54 61 62  Names(Parse*,Tab
70540 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  le*);.#else.# de
70550 66 69 6e 65 20 73 71 6c 69 74 65 33 56 69 65 77  fine sqlite3View
70560 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41  GetColumnNames(A
70570 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51  ,B) 0.#endif..SQ
70580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70590 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62  d sqlite3DropTab
705a0 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  le(Parse*, SrcLi
705b0 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  st*, int, int);.
705c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
705d0 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
705e0 65 54 61 62 6c 65 28 54 61 62 6c 65 2a 29 3b 0a  eTable(Table*);.
705f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
70600 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
70610 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
70620 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41     void sqlite3A
70630 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69  utoincrementBegi
70640 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  n(Parse *pParse)
70650 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70660 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41     void sqlite3A
70670 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28  utoincrementEnd(
70680 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0a  Parse *pParse);.
70690 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
706a0 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d  qlite3Autoincrem
706b0 65 6e 74 42 65 67 69 6e 28 58 29 0a 23 20 64 65  entBegin(X).# de
706c0 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f  fine sqlite3Auto
706d0 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 58 29 0a  incrementEnd(X).
706e0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52  #endif.SQLITE_PR
706f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
70700 65 33 49 6e 73 65 72 74 28 50 61 72 73 65 2a 2c  e3Insert(Parse*,
70710 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c   SrcList*, ExprL
70720 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 49  ist*, Select*, I
70730 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51  dList*, int);.SQ
70740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70750 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41  d *sqlite3ArrayA
70760 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65 33 2a  llocate(sqlite3*
70770 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 69  ,void*,int,int,i
70780 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a  nt*,int*,int*);.
70790 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
707a0 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64  dList *sqlite3Id
707b0 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74  ListAppend(sqlit
707c0 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 54 6f  e3*, IdList*, To
707d0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
707e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
707f0 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c  3IdListIndex(IdL
70800 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ist*,const char*
70810 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70820 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
70830 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65  e3SrcListEnlarge
70840 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69  (sqlite3*, SrcLi
70850 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  st*, int, int);.
70860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
70870 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
70880 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c  rcListAppend(sql
70890 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 2c  ite3*, SrcList*,
708a0 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29   Token*, Token*)
708b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
708c0 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
708d0 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72  3SrcListAppendFr
708e0 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c 20 53  omTerm(Parse*, S
708f0 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c  rcList*, Token*,
70900 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20   Token*,.       
70910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
70930 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a 2c 20  oken*, Select*, 
70940 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a 29 3b  Expr*, IdList*);
70950 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70960 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
70970 69 73 74 49 6e 64 65 78 65 64 42 79 28 50 61 72  istIndexedBy(Par
70980 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c  se *, SrcList *,
70990 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54   Token *);.SQLIT
709a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
709b0 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f  lite3IndexedByLo
709c0 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20 73 74  okup(Parse *, st
709d0 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
709e0 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  m *);.SQLITE_PRI
709f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70a00 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69  3SrcListShiftJoi
70a10 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a 29 3b  nType(SrcList*);
70a20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70a30 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
70a40 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73  istAssignCursors
70a50 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
70a60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70a70 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49  TE void sqlite3I
70a80 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69  dListDelete(sqli
70a90 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a  te3*, IdList*);.
70aa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70ab0 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
70ac0 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  stDelete(sqlite3
70ad0 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51  *, SrcList*);.SQ
70ae0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64  LITE_PRIVATE Ind
70af0 65 78 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74  ex *sqlite3Creat
70b00 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 6f  eIndex(Parse*,To
70b10 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c  ken*,Token*,SrcL
70b20 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69  ist*,ExprList*,i
70b30 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20  nt,Token*,.     
70b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70b50 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20     Token*, int, 
70b60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
70b70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70b80 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65  3DropIndex(Parse
70b90 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74  *, SrcList*, int
70ba0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70bb0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c  E int sqlite3Sel
70bc0 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65  ect(Parse*, Sele
70bd0 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a  ct*, SelectDest*
70be0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70bf0 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65  E Select *sqlite
70c00 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 65  3SelectNew(Parse
70c10 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 4c  *,ExprList*,SrcL
70c20 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 4c  ist*,Expr*,ExprL
70c30 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20  ist*,.          
70c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
70c50 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69  xpr*,ExprList*,i
70c60 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b  nt,Expr*,Expr*);
70c70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70c80 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65  void sqlite3Sele
70c90 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  ctDelete(sqlite3
70ca0 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
70cb0 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
70cc0 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  e *sqlite3SrcLis
70cd0 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20  tLookup(Parse*, 
70ce0 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  SrcList*);.SQLIT
70cf0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
70d00 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28  lite3IsReadOnly(
70d10 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
70d20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
70d30 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70d40 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65  3OpenTable(Parse
70d50 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74  *, int iCur, int
70d60 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e   iDb, Table*, in
70d70 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  t);.#if defined(
70d80 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50  SQLITE_ENABLE_UP
70d90 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49  DATE_DELETE_LIMI
70da0 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  T) && !defined(S
70db0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
70dc0 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  ERY).SQLITE_PRIV
70dd0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
70de0 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 73  3LimitWhere(Pars
70df0 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20  e *, SrcList *, 
70e00 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 74  Expr *, ExprList
70e10 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72   *, Expr *, Expr
70e20 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e   *, char *);.#en
70e30 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
70e40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
70e50 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a  eleteFrom(Parse*
70e60 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72  , SrcList*, Expr
70e70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70e80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
70e90 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72  pdate(Parse*, Sr
70ea0 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74  cList*, ExprList
70eb0 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a  *, Expr*, int);.
70ec0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57  SQLITE_PRIVATE W
70ed0 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65  hereInfo *sqlite
70ee0 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73  3WhereBegin(Pars
70ef0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78  e*, SrcList*, Ex
70f00 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c  pr*, ExprList**,
70f10 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f 50 52   u16);.SQLITE_PR
70f20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
70f30 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 72 65  e3WhereEnd(Where
70f40 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Info*);.SQLITE_P
70f50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
70f60 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c  e3ExprCodeGetCol
70f70 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  umn(Parse*, Tabl
70f80 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  e*, int, int, in
70f90 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
70fa0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70fb0 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
70fc0 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Parse*, int, in
70fd0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
70fe0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70ff0 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79  ite3ExprCodeCopy
71000 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Parse*, int, in
71010 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
71020 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71030 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f  ite3ExprCacheSto
71040 72 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  re(Parse*, int, 
71050 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
71060 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
71070 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
71080 75 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ush(Parse*);.SQL
71090 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
710a0 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
710b0 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e 74  ePop(Parse*, int
710c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
710d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
710e0 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61  prCacheRemove(Pa
710f0 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  rse*, int);.SQLI
71100 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
71110 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
71120 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53  Clear(Parse*);.S
71130 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71140 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
71150 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
71160 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69  e(Parse*, int, i
71170 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
71180 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71190 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72  ExprHardCopy(Par
711a0 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  se*,int,int);.SQ
711b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
711c0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
711d0 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20  (Parse*, Expr*, 
711e0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
711f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
71200 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72  ExprCodeTemp(Par
71210 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2a  se*, Expr*, int*
71220 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71230 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
71240 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73  rCodeTarget(Pars
71250 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b  e*, Expr*, int);
71260 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71270 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
71280 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73  odeAndCache(Pars
71290 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b  e*, Expr*, int);
712a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
712b0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
712c0 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61  CodeConstants(Pa
712d0 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  rse*, Expr*);.SQ
712e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
712f0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
71300 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a 2c  ExprList(Parse*,
71310 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c   ExprList*, int,
71320 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
71330 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
71340 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61 72  e3ExprIfTrue(Par
71350 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c  se*, Expr*, int,
71360 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
71370 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
71380 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50 61  e3ExprIfFalse(Pa
71390 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
713a0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
713b0 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
713c0 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73  lite3FindTable(s
713d0 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
713e0 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  ar*, const char*
713f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71400 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
71410 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 73  LocateTable(Pars
71420 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63 6f  e*,int isView,co
71430 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74  nst char*, const
71440 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
71450 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73  PRIVATE Index *s
71460 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
71470 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
71480 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  har*, const char
71490 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
714a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
714b0 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61  nlinkAndDeleteTa
714c0 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  ble(sqlite3*,int
714d0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53  ,const char*);.S
714e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
714f0 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  id sqlite3Unlink
71500 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73  AndDeleteIndex(s
71510 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73  qlite3*,int,cons
71520 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
71530 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
71540 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73  lite3Vacuum(Pars
71550 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
71560 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
71570 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c  unVacuum(char**,
71580 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49   sqlite3*);.SQLI
71590 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
715a0 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d  *sqlite3NameFrom
715b0 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20  Token(sqlite3*, 
715c0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
715d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
715e0 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45  te3ExprCompare(E
715f0 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  xpr*, Expr*);.SQ
71600 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71610 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61  d sqlite3ExprAna
71620 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e  lyzeAggregates(N
71630 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70  ameContext*, Exp
71640 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
71650 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71660 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69  ExprAnalyzeAggLi
71670 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c  st(NameContext*,
71680 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49  ExprList*);.SQLI
71690 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20  TE_PRIVATE Vdbe 
716a0 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28  *sqlite3GetVdbe(
716b0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
716c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
716d0 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74  ite3PrngSaveStat
716e0 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  e(void);.SQLITE_
716f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71700 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53  ite3PrngRestoreS
71710 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49  tate(void);.SQLI
71720 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
71730 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74  sqlite3PrngReset
71740 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c  State(void);.SQL
71750 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
71760 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
71770 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  All(sqlite3*);.S
71780 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71790 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65  id sqlite3CodeVe
717a0 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 65  rifySchema(Parse
717b0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
717c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
717d0 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63  ite3BeginTransac
717e0 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74  tion(Parse*, int
717f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71800 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
71810 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28  mmitTransaction(
71820 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
71830 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71840 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e  ite3RollbackTran
71850 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b  saction(Parse*);
71860 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71870 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65  void sqlite3Save
71880 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e  point(Parse*, in
71890 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  t, Token*);.SQLI
718a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
718b0 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65  sqlite3CloseSave
718c0 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a  points(sqlite3 *
718d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
718e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
718f0 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72  rIsConstant(Expr
71900 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71910 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
71920 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a  prIsConstantNotJ
71930 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49  oin(Expr*);.SQLI
71940 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
71950 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
71960 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45  tantOrFunction(E
71970 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
71980 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
71990 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45  3ExprIsInteger(E
719a0 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c  xpr*, int*);.SQL
719b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
719c0 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 63  sqlite3IsRowid(c
719d0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
719e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
719f0 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
71a00 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73 65 2a  RowDelete(Parse*
71a10 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69  , Table*, int, i
71a20 6e 74 2c 20 69 6e 74 2c 20 54 72 69 67 67 65 72  nt, int, Trigger
71a30 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
71a40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
71a50 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
71a60 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72 73  IndexDelete(Pars
71a70 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c  e*, Table*, int,
71a80 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   int*);.SQLITE_P
71a90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
71aa0 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b  e3GenerateIndexK
71ab0 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65 78  ey(Parse*, Index
71ac0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
71ad0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71ae0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65  E void sqlite3Ge
71af0 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74  nerateConstraint
71b00 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54 61  Checks(Parse*,Ta
71b10 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a 20 20  ble*,int,int,.  
71b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71b40 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c     int*,int,int,
71b50 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53  int,int,int*);.S
71b60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71b70 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65  id sqlite3Comple
71b80 74 65 49 6e 73 65 72 74 69 6f 6e 28 50 61 72 73  teInsertion(Pars
71b90 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c  e*, Table*, int,
71ba0 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 6e 74 2c   int, int*, int,
71bb0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
71bc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
71bd0 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41  qlite3OpenTableA
71be0 6e 64 49 6e 64 69 63 65 73 28 50 61 72 73 65 2a  ndIndices(Parse*
71bf0 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69  , Table*, int, i
71c00 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
71c10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71c20 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
71c30 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c  ion(Parse*, int,
71c40 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
71c50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
71c60 65 33 4d 75 6c 74 69 57 72 69 74 65 28 50 61 72  e3MultiWrite(Par
71c70 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  se*);.SQLITE_PRI
71c80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71c90 33 4d 61 79 41 62 6f 72 74 28 50 61 72 73 65 2a  3MayAbort(Parse*
71ca0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71cb0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61  E void sqlite3Ha
71cc0 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 72  ltConstraint(Par
71cd0 73 65 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2c  se*, int, char*,
71ce0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
71cf0 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
71d00 74 65 33 45 78 70 72 44 75 70 28 73 71 6c 69 74  te3ExprDup(sqlit
71d10 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 29 3b 0a  e3*,Expr*,int);.
71d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
71d30 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33  xprList *sqlite3
71d40 45 78 70 72 4c 69 73 74 44 75 70 28 73 71 6c 69  ExprListDup(sqli
71d50 74 65 33 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69  te3*,ExprList*,i
71d60 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
71d70 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
71d80 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 73  ite3SrcListDup(s
71d90 71 6c 69 74 65 33 2a 2c 53 72 63 4c 69 73 74 2a  qlite3*,SrcList*
71da0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
71db0 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71  IVATE IdList *sq
71dc0 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 73  lite3IdListDup(s
71dd0 71 6c 69 74 65 33 2a 2c 49 64 4c 69 73 74 2a 29  qlite3*,IdList*)
71de0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71df0 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33   Select *sqlite3
71e00 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65  SelectDup(sqlite
71e10 33 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 29 3b  3*,Select*,int);
71e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71e30 76 6f 69 64 20 73 71 6c 69 74 65 33 46 75 6e 63  void sqlite3Func
71e40 44 65 66 49 6e 73 65 72 74 28 46 75 6e 63 44 65  DefInsert(FuncDe
71e50 66 48 61 73 68 2a 2c 20 46 75 6e 63 44 65 66 2a  fHash*, FuncDef*
71e60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71e70 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74  E FuncDef *sqlit
71e80 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 73  e3FindFunction(s
71e90 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
71ea0 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 75 38 2c 69  ar*,int,int,u8,i
71eb0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
71ec0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71ed0 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46  RegisterBuiltinF
71ee0 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33  unctions(sqlite3
71ef0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71f00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
71f10 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46  egisterDateTimeF
71f20 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a  unctions(void);.
71f30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
71f40 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
71f50 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f  terGlobalFunctio
71f60 6e 73 28 76 6f 69 64 29 3b 0a 23 69 66 64 65 66  ns(void);.#ifdef
71f70 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51   SQLITE_DEBUG.SQ
71f80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
71f90 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  nt sqlite3Safety
71fa0 4f 6e 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  On(sqlite3*);.SQ
71fb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
71fc0 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  nt sqlite3Safety
71fd0 4f 66 66 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23  Off(sqlite3*);.#
71fe0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
71ff0 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 41 29  lite3SafetyOn(A)
72000 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
72010 74 65 33 53 61 66 65 74 79 4f 66 66 28 41 29 20  te3SafetyOff(A) 
72020 30 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  0.#endif.SQLITE_
72030 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
72040 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b  te3SafetyCheckOk
72050 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49  (sqlite3*);.SQLI
72060 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72070 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
72080 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 74 65  kSickOrOk(sqlite
72090 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  3*);.SQLITE_PRIV
720a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
720b0 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 50 61 72  ChangeCookie(Par
720c0 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 20  se*, int);..#if 
720d0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
720e0 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64  OMIT_VIEW) && !d
720f0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
72100 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49  IT_TRIGGER).SQLI
72110 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
72120 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c 69  sqlite3Materiali
72130 7a 65 56 69 65 77 28 50 61 72 73 65 2a 2c 20 54  zeView(Parse*, T
72140 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  able*, Expr*, in
72150 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  t);.#endif..#ifn
72160 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
72170 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45 5f 50  TRIGGER.SQLITE_P
72180 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
72190 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67 67 65  lite3BeginTrigge
721a0 72 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a  r(Parse*, Token*
721b0 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c  ,Token*,int,int,
721c0 49 64 4c 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a  IdList*,SrcList*
721d0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
721e0 20 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70               Exp
721f0 72 2a 2c 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  r*,int, int);.SQ
72200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
72210 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73  oid sqlite3Finis
72220 68 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c  hTrigger(Parse*,
72230 20 54 72 69 67 67 65 72 53 74 65 70 2a 2c 20 54   TriggerStep*, T
72240 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
72250 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
72260 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72  lite3DropTrigger
72270 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
72280 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
72290 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
722a0 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65  qlite3DropTrigge
722b0 72 50 74 72 28 50 61 72 73 65 2a 2c 20 54 72 69  rPtr(Parse*, Tri
722c0 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  gger*);.SQLITE_P
722d0 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72  RIVATE   Trigger
722e0 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72   *sqlite3Trigger
722f0 73 45 78 69 73 74 28 50 61 72 73 65 20 2a 2c 20  sExist(Parse *, 
72300 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 45 78 70  Table*, int, Exp
72310 72 4c 69 73 74 2a 2c 20 69 6e 74 20 2a 70 4d 61  rList*, int *pMa
72320 73 6b 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  sk);.SQLITE_PRIV
72330 41 54 45 20 20 20 54 72 69 67 67 65 72 20 2a 73  ATE   Trigger *s
72340 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73  qlite3TriggerLis
72350 74 28 50 61 72 73 65 20 2a 2c 20 54 61 62 6c 65  t(Parse *, Table
72360 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
72370 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
72380 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
72390 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72  (Parse*, Trigger
723a0 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73   *, int, ExprLis
723b0 74 2a 2c 20 69 6e 74 2c 20 54 61 62 6c 65 20 2a  t*, int, Table *
723c0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
723d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
723e0 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
723f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
72400 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 52  oid sqlite3CodeR
72410 6f 77 54 72 69 67 67 65 72 44 69 72 65 63 74 28  owTriggerDirect(
72420 50 61 72 73 65 20 2a 2c 20 54 72 69 67 67 65 72  Parse *, Trigger
72430 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74   *, Table *, int
72440 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 20 20 76  , int, int);.  v
72450 6f 69 64 20 73 71 6c 69 74 65 56 69 65 77 54 72  oid sqliteViewTr
72460 69 67 67 65 72 73 28 50 61 72 73 65 2a 2c 20 54  iggers(Parse*, T
72470 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  able*, Expr*, in
72480 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53  t, ExprList*);.S
72490 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
724a0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
724b0 74 65 54 72 69 67 67 65 72 53 74 65 70 28 73 71  teTriggerStep(sq
724c0 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65 72 53  lite3*, TriggerS
724d0 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  tep*);.SQLITE_PR
724e0 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53  IVATE   TriggerS
724f0 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67  tep *sqlite3Trig
72500 67 65 72 53 65 6c 65 63 74 53 74 65 70 28 73 71  gerSelectStep(sq
72510 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 29 3b  lite3*,Select*);
72520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72530 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73    TriggerStep *s
72540 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e 73  qlite3TriggerIns
72550 65 72 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a  ertStep(sqlite3*
72560 2c 54 6f 6b 65 6e 2a 2c 20 49 64 4c 69 73 74 2a  ,Token*, IdList*
72570 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
72580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72590 20 20 20 20 20 20 20 20 20 20 45 78 70 72 4c 69            ExprLi
725a0 73 74 2a 2c 53 65 6c 65 63 74 2a 2c 75 38 29 3b  st*,Select*,u8);
725b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
725c0 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73    TriggerStep *s
725d0 71 6c 69 74 65 33 54 72 69 67 67 65 72 55 70 64  qlite3TriggerUpd
725e0 61 74 65 53 74 65 70 28 73 71 6c 69 74 65 33 2a  ateStep(sqlite3*
725f0 2c 54 6f 6b 65 6e 2a 2c 45 78 70 72 4c 69 73 74  ,Token*,ExprList
72600 2a 2c 20 45 78 70 72 2a 2c 20 75 38 29 3b 0a 53  *, Expr*, u8);.S
72610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
72620 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c  TriggerStep *sql
72630 69 74 65 33 54 72 69 67 67 65 72 44 65 6c 65 74  ite3TriggerDelet
72640 65 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54  eStep(sqlite3*,T
72650 6f 6b 65 6e 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  oken*, Expr*);.S
72660 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
72670 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
72680 74 65 54 72 69 67 67 65 72 28 73 71 6c 69 74 65  teTrigger(sqlite
72690 33 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53  3*, Trigger*);.S
726a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
726b0 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69  void sqlite3Unli
726c0 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67  nkAndDeleteTrigg
726d0 65 72 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c  er(sqlite3*,int,
726e0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
726f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 75  LITE_PRIVATE   u
72700 33 32 20 73 71 6c 69 74 65 33 54 72 69 67 67 65  32 sqlite3Trigge
72710 72 4f 6c 64 6d 61 73 6b 28 50 61 72 73 65 2a 2c  rOldmask(Parse*,
72720 54 72 69 67 67 65 72 2a 2c 45 78 70 72 4c 69 73  Trigger*,ExprLis
72730 74 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 29 3b 0a  t*,Table*,int);.
72740 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
72750 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 29  ParseToplevel(p)
72760 20 28 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c   ((p)->pToplevel
72770 20 3f 20 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65   ? (p)->pTopleve
72780 6c 20 3a 20 28 70 29 29 0a 23 65 6c 73 65 0a 23  l : (p)).#else.#
72790 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54   define sqlite3T
727a0 72 69 67 67 65 72 73 45 78 69 73 74 28 42 2c 43  riggersExist(B,C
727b0 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69  ,D,E,F) 0.# defi
727c0 6e 65 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  ne sqlite3Delete
727d0 54 72 69 67 67 65 72 28 41 2c 42 29 0a 23 20 64  Trigger(A,B).# d
727e0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 72 6f  efine sqlite3Dro
727f0 70 54 72 69 67 67 65 72 50 74 72 28 41 2c 42 29  pTriggerPtr(A,B)
72800 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
72810 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
72820 54 72 69 67 67 65 72 28 41 2c 42 2c 43 29 0a 23  Trigger(A,B,C).#
72830 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43   define sqlite3C
72840 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c  odeRowTrigger(A,
72850 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 29  B,C,D,E,F,G,H,I)
72860 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
72870 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44  3CodeRowTriggerD
72880 69 72 65 63 74 28 41 2c 42 2c 43 2c 44 2c 45 2c  irect(A,B,C,D,E,
72890 46 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  F).# define sqli
728a0 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 58  te3TriggerList(X
728b0 2c 20 59 29 20 30 0a 23 20 64 65 66 69 6e 65 20  , Y) 0.# define 
728c0 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c  sqlite3ParseTopl
728d0 65 76 65 6c 28 70 29 20 70 0a 23 20 64 65 66 69  evel(p) p.# defi
728e0 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67 67 65  ne sqlite3Trigge
728f0 72 4f 6c 64 6d 61 73 6b 28 41 2c 42 2c 43 2c 44  rOldmask(A,B,C,D
72900 2c 45 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51  ,E) 0.#endif..SQ
72910 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
72920 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65   sqlite3JoinType
72930 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c  (Parse*, Token*,
72940 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29   Token*, Token*)
72950 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72960 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65   void sqlite3Cre
72970 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50 61  ateForeignKey(Pa
72980 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c  rse*, ExprList*,
72990 20 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69 73   Token*, ExprLis
729a0 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  t*, int);.SQLITE
729b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
729c0 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67  lite3DeferForeig
729d0 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e 74  nKey(Parse*, int
729e0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
729f0 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
72a00 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56  TION.SQLITE_PRIV
72a10 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
72a20 65 33 41 75 74 68 52 65 61 64 28 50 61 72 73 65  e3AuthRead(Parse
72a30 2a 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a 2c  *,Expr*,Schema*,
72a40 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  SrcList*);.SQLIT
72a50 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
72a60 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
72a70 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f 6e  (Parse*,int, con
72a80 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20  st char*, const 
72a90 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61  char*, const cha
72aa0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
72ab0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
72ac0 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73  e3AuthContextPus
72ad0 68 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43 6f  h(Parse*, AuthCo
72ae0 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68  ntext*, const ch
72af0 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
72b00 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
72b10 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f  te3AuthContextPo
72b20 70 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29 3b  p(AuthContext*);
72b30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72b40 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74    int sqlite3Aut
72b50 68 52 65 61 64 43 6f 6c 28 50 61 72 73 65 2a 2c  hReadCol(Parse*,
72b60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63   const char *, c
72b70 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
72b80 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
72b90 65 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61  e sqlite3AuthRea
72ba0 64 28 61 2c 62 2c 63 2c 64 29 0a 23 20 64 65 66  d(a,b,c,d).# def
72bb0 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43  ine sqlite3AuthC
72bc0 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 20  heck(a,b,c,d,e) 
72bd0 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64     SQLITE_OK.# d
72be0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74  efine sqlite3Aut
72bf0 68 43 6f 6e 74 65 78 74 50 75 73 68 28 61 2c 62  hContextPush(a,b
72c00 2c 63 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ,c).# define sql
72c10 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
72c20 6f 70 28 61 29 20 20 28 28 76 6f 69 64 29 28 61  op(a)  ((void)(a
72c30 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45  )).#endif.SQLITE
72c40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
72c50 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73  lite3Attach(Pars
72c60 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a  e*, Expr*, Expr*
72c70 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
72c80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
72c90 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72 73  lite3Detach(Pars
72ca0 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  e*, Expr*);.SQLI
72cb0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72cc0 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 6f  qlite3BtreeFacto
72cd0 72 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ry(sqlite3 *db, 
72ce0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
72cf0 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  ename,.         
72d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
72d10 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 69  t omitJournal, i
72d20 6e 74 20 6e 43 61 63 68 65 2c 20 69 6e 74 20 66  nt nCache, int f
72d30 6c 61 67 73 2c 20 42 74 72 65 65 20 2a 2a 70 70  lags, Btree **pp
72d40 42 74 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50  Btree);.SQLITE_P
72d50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
72d60 65 33 46 69 78 49 6e 69 74 28 44 62 46 69 78 65  e3FixInit(DbFixe
72d70 72 2a 2c 20 50 61 72 73 65 2a 2c 20 69 6e 74 2c  r*, Parse*, int,
72d80 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f   const char*, co
72d90 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  nst Token*);.SQL
72da0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
72db0 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73  sqlite3FixSrcLis
72dc0 74 28 44 62 46 69 78 65 72 2a 2c 20 53 72 63 4c  t(DbFixer*, SrcL
72dd0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
72de0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
72df0 33 46 69 78 53 65 6c 65 63 74 28 44 62 46 69 78  3FixSelect(DbFix
72e00 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53  er*, Select*);.S
72e10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
72e20 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  t sqlite3FixExpr
72e30 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72 2a  (DbFixer*, Expr*
72e40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72e50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
72e60 45 78 70 72 4c 69 73 74 28 44 62 46 69 78 65 72  ExprList(DbFixer
72e70 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53  *, ExprList*);.S
72e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
72e90 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 69 67  t sqlite3FixTrig
72ea0 67 65 72 53 74 65 70 28 44 62 46 69 78 65 72 2a  gerStep(DbFixer*
72eb0 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b  , TriggerStep*);
72ec0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72ed0 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28  int sqlite3AtoF(
72ee0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64  const char *z, d
72ef0 6f 75 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  ouble*);.SQLITE_
72f00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
72f10 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73  te3GetInt32(cons
72f20 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2a 29 3b  t char *, int*);
72f30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72f40 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74 73 49  int sqlite3FitsI
72f50 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20 63 68  n64Bits(const ch
72f60 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ar *, int);.SQLI
72f70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72f80 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65 4c  qlite3Utf16ByteL
72f90 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  en(const void *p
72fa0 44 61 74 61 2c 20 69 6e 74 20 6e 43 68 61 72 29  Data, int nChar)
72fb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72fc0 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38   int sqlite3Utf8
72fd0 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68  CharLen(const ch
72fe0 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
72ff0 42 79 74 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Byte);.SQLITE_PR
73000 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
73010 33 55 74 66 38 52 65 61 64 28 63 6f 6e 73 74 20  3Utf8Read(const 
73020 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38 2a 2a 29  u8*, const u8**)
73030 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  ;../*.** Routine
73040 73 20 74 6f 20 72 65 61 64 20 61 6e 64 20 77 72  s to read and wr
73050 69 74 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  ite variable-len
73060 67 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20 54  gth integers.  T
73070 68 65 73 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20  hese used to.** 
73080 62 65 20 64 65 66 69 6e 65 64 20 6c 6f 63 61 6c  be defined local
73090 6c 79 2c 20 62 75 74 20 6e 6f 77 20 77 65 20 75  ly, but now we u
730a0 73 65 20 74 68 65 20 76 61 72 69 6e 74 20 72 6f  se the varint ro
730b0 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20 75 74  utines in the ut
730c0 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65 2e 20 20 43  il.c.** file.  C
730d0 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 20 74  ode should use t
730e0 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d 73 20 62  he MACRO forms b
730f0 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 56 61 72  elow, as the Var
73100 69 6e 74 33 32 20 76 65 72 73 69 6f 6e 73 0a 2a  int32 versions.*
73110 2a 20 61 72 65 20 63 6f 64 65 64 20 74 6f 20 61  * are coded to a
73120 73 73 75 6d 65 20 74 68 65 20 73 69 6e 67 6c 65  ssume the single
73130 20 62 79 74 65 20 63 61 73 65 20 69 73 20 61 6c   byte case is al
73140 72 65 61 64 79 20 68 61 6e 64 6c 65 64 20 28 77  ready handled (w
73150 68 69 63 68 20 0a 2a 2a 20 74 68 65 20 4d 41 43  hich .** the MAC
73160 52 4f 20 66 6f 72 6d 20 64 6f 65 73 29 2e 0a 2a  RO form does)..*
73170 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
73180 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56   int sqlite3PutV
73190 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63  arint(unsigned c
731a0 68 61 72 2a 2c 20 75 36 34 29 3b 0a 53 51 4c 49  har*, u64);.SQLI
731b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
731c0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33  qlite3PutVarint3
731d0 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a  2(unsigned char*
731e0 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50  , u32);.SQLITE_P
731f0 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65  RIVATE u8 sqlite
73200 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74  3GetVarint(const
73210 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
73220 2c 20 75 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45  , u64 *);.SQLITE
73230 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69  _PRIVATE u8 sqli
73240 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 63  te3GetVarint32(c
73250 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
73260 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b 0a 53 51  ar *, u32 *);.SQ
73270 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73280 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65   sqlite3VarintLe
73290 6e 28 75 36 34 20 76 29 3b 0a 0a 2f 2a 0a 2a 2a  n(u64 v);../*.**
732a0 20 54 68 65 20 68 65 61 64 65 72 20 6f 66 20 61   The header of a
732b0 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 73 74 73   record consists
732c0 20 6f 66 20 61 20 73 65 71 75 65 6e 63 65 20 76   of a sequence v
732d0 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
732e0 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73  ntegers..** Thes
732f0 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 61  e integers are a
73300 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 73 6d 61  lmost always sma
73310 6c 6c 20 61 6e 64 20 61 72 65 20 65 6e 63 6f 64  ll and are encod
73320 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65 20 62  ed as a single b
73330 79 74 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  yte..** The foll
73340 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 74 61 6b  owing macros tak
73350 65 20 61 64 76 61 6e 74 61 67 65 20 74 68 69 73  e advantage this
73360 20 66 61 63 74 20 74 6f 20 70 72 6f 76 69 64 65   fact to provide
73370 20 61 20 66 61 73 74 20 65 6e 63 6f 64 65 0a 2a   a fast encode.*
73380 2a 20 61 6e 64 20 64 65 63 6f 64 65 20 6f 66 20  * and decode of 
73390 74 68 65 20 69 6e 74 65 67 65 72 73 20 69 6e 20  the integers in 
733a0 61 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 2e  a record header.
733b0 20 20 49 74 20 69 73 20 66 61 73 74 65 72 20 66    It is faster f
733c0 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a  or the common.**
733d0 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20   case where the 
733e0 69 6e 74 65 67 65 72 20 69 73 20 61 20 73 69 6e  integer is a sin
733f0 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20 69 73  gle byte.  It is
73400 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72   a little slower
73410 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69 6e 74   when the.** int
73420 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72 20 6d  eger is two or m
73430 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75 74 20  ore bytes.  But 
73440 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20 66 61  overall it is fa
73450 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ster..**.** The 
73460 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73  following expres
73470 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69 76 61  sions are equiva
73480 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lent:.**.**     
73490 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61  x = sqlite3GetVa
734a0 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20 29 3b  rint32( A, &B );
734b0 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69  .**     x = sqli
734c0 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 20  te3PutVarint32( 
734d0 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 20 20 20  A, B );.**.**   
734e0 20 20 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33    x = getVarint3
734f0 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 20 20 20  2( A, B );.**   
73500 20 20 78 20 3d 20 70 75 74 56 61 72 69 6e 74 33    x = putVarint3
73510 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2f  2( A, B );.**.*/
73520 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 72 69  .#define getVari
73530 6e 74 33 32 28 41 2c 42 29 20 20 28 75 38 29 28  nt32(A,B)  (u8)(
73540 28 2a 28 41 29 3c 28 75 38 29 30 78 38 30 29 20  (*(A)<(u8)0x80) 
73550 3f 20 28 28 42 29 20 3d 20 28 75 33 32 29 2a 28  ? ((B) = (u32)*(
73560 41 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 47  A)),1 : sqlite3G
73570 65 74 56 61 72 69 6e 74 33 32 28 28 41 29 2c 20  etVarint32((A), 
73580 28 75 33 32 20 2a 29 26 28 42 29 29 29 0a 23 64  (u32 *)&(B))).#d
73590 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e 74 33  efine putVarint3
735a0 32 28 41 2c 42 29 20 20 28 75 38 29 28 28 28 75  2(A,B)  (u8)(((u
735b0 33 32 29 28 42 29 3c 28 75 33 32 29 30 78 38 30  32)(B)<(u32)0x80
735c0 29 20 3f 20 28 2a 28 41 29 20 3d 20 28 75 6e 73  ) ? (*(A) = (uns
735d0 69 67 6e 65 64 20 63 68 61 72 29 28 42 29 29 2c  igned char)(B)),
735e0 31 20 3a 20 73 71 6c 69 74 65 33 50 75 74 56 61  1 : sqlite3PutVa
735f0 72 69 6e 74 33 32 28 28 41 29 2c 20 28 42 29 29  rint32((A), (B))
73600 29 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 72  ).#define getVar
73610 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 47 65  int    sqlite3Ge
73620 74 56 61 72 69 6e 74 0a 23 64 65 66 69 6e 65 20  tVarint.#define 
73630 70 75 74 56 61 72 69 6e 74 20 20 20 20 73 71 6c  putVarint    sql
73640 69 74 65 33 50 75 74 56 61 72 69 6e 74 0a 0a 0a  ite3PutVarint...
73650 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
73660 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
73670 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53  e3IndexAffinityS
73680 74 72 28 56 64 62 65 20 2a 2c 20 49 6e 64 65 78  tr(Vdbe *, Index
73690 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
736a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
736b0 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72  TableAffinityStr
736c0 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 20 2a  (Vdbe *, Table *
736d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
736e0 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 6f  E char sqlite3Co
736f0 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 45 78  mpareAffinity(Ex
73700 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20  pr *pExpr, char 
73710 61 66 66 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52  aff2);.SQLITE_PR
73720 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
73730 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b  3IndexAffinityOk
73740 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68  (Expr *pExpr, ch
73750 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29  ar idx_affinity)
73760 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73770 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 78 70   char sqlite3Exp
73780 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a  rAffinity(Expr *
73790 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50  pExpr);.SQLITE_P
737a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
737b0 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20 63  e3Atoi64(const c
737c0 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a 53 51 4c  har*, i64*);.SQL
737d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
737e0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73 71   sqlite3Error(sq
737f0 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e  lite3*, int, con
73800 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53  st char*,...);.S
73810 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73820 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54 6f  id *sqlite3HexTo
73830 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a 2c 20 63  Blob(sqlite3*, c
73840 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e  onst char *z, in
73850 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  t n);.SQLITE_PRI
73860 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
73870 54 77 6f 50 61 72 74 4e 61 6d 65 28 50 61 72 73  TwoPartName(Pars
73880 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f  e *, Token *, To
73890 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2a 29  ken *, Token **)
738a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
738b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
738c0 69 74 65 33 45 72 72 53 74 72 28 69 6e 74 29 3b  ite3ErrStr(int);
738d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
738e0 69 6e 74 20 73 71 6c 69 74 65 33 52 65 61 64 53  int sqlite3ReadS
738f0 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61  chema(Parse *pPa
73900 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  rse);.SQLITE_PRI
73910 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
73920 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
73930 28 73 71 6c 69 74 65 33 2a 2c 75 38 20 65 6e 63  (sqlite3*,u8 enc
73940 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  , const char*,in
73950 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
73960 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
73970 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
73980 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
73990 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e 61 6d 65  const char*zName
739a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
739b0 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
739c0 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 61  e3ExprCollSeq(Pa
739d0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
739e0 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54  r *pExpr);.SQLIT
739f0 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
73a00 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f  sqlite3ExprSetCo
73a10 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ll(Parse *pParse
73a20 2c 20 45 78 70 72 20 2a 2c 20 54 6f 6b 65 6e 20  , Expr *, Token 
73a30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
73a40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68  TE int sqlite3Ch
73a50 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  eckCollSeq(Parse
73a60 20 2a 2c 20 43 6f 6c 6c 53 65 71 20 2a 29 3b 0a   *, CollSeq *);.
73a70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
73a80 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 4f  nt sqlite3CheckO
73a90 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65 20  bjectName(Parse 
73aa0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  *, const char *)
73ab0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73ac0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
73ad0 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c 69  eSetChanges(sqli
73ae0 74 65 33 20 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51  te3 *, int);..SQ
73af0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
73b00 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
73b10 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74 65  ValueText(sqlite
73b20 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53  3_value*, u8);.S
73b30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
73b40 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79  t sqlite3ValueBy
73b50 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  tes(sqlite3_valu
73b60 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f  e*, u8);.SQLITE_
73b70 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
73b80 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
73b90 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20  sqlite3_value*, 
73ba0 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  int, const void 
73bb0 2a 2c 75 38 2c 20 0a 20 20 20 20 20 20 20 20 20  *,u8, .         
73bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
73bd0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
73be0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73bf0 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65  oid sqlite3Value
73c00 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  Free(sqlite3_val
73c10 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ue*);.SQLITE_PRI
73c20 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c  VATE sqlite3_val
73c30 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65  ue *sqlite3Value
73c40 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a  New(sqlite3 *);.
73c50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
73c60 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31  har *sqlite3Utf1
73c70 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 2c 20  6to8(sqlite3 *, 
73c80 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
73c90 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
73ca0 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51  _ENABLE_STAT2.SQ
73cb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
73cc0 72 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f  r *sqlite3Utf8to
73cd0 31 36 28 73 71 6c 69 74 65 33 20 2a 2c 20 75 38  16(sqlite3 *, u8
73ce0 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69  , char *, int, i
73cf0 6e 74 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51  nt *);.#endif.SQ
73d00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73d10 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f   sqlite3ValueFro
73d20 6d 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a 2c  mExpr(sqlite3 *,
73d30 20 45 78 70 72 20 2a 2c 20 75 38 2c 20 75 38 2c   Expr *, u8, u8,
73d40 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
73d50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
73d60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
73d70 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74  alueApplyAffinit
73d80 79 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  y(sqlite3_value 
73d90 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23 69 66 6e  *, u8, u8);.#ifn
73da0 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47  def SQLITE_AMALG
73db0 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50  AMATION.SQLITE_P
73dc0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73  RIVATE const uns
73dd0 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74  igned char sqlit
73de0 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d  e3UpperToLower[]
73df0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73e00 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
73e10 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 79 70  char sqlite3Ctyp
73e20 65 4d 61 70 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50  eMap[];.SQLITE_P
73e30 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53  RIVATE SQLITE_WS
73e40 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33  D struct Sqlite3
73e50 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f  Config sqlite3Co
73e60 6e 66 69 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nfig;.SQLITE_PRI
73e70 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20  VATE SQLITE_WSD 
73e80 46 75 6e 63 44 65 66 48 61 73 68 20 73 71 6c 69  FuncDefHash sqli
73e90 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f  te3GlobalFunctio
73ea0 6e 73 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ns;.SQLITE_PRIVA
73eb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 65  TE int sqlite3Pe
73ec0 6e 64 69 6e 67 42 79 74 65 3b 0a 23 65 6e 64 69  ndingByte;.#endi
73ed0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
73ee0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f   void sqlite3Roo
73ef0 74 50 61 67 65 4d 6f 76 65 64 28 44 62 2a 2c 20  tPageMoved(Db*, 
73f00 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
73f10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
73f20 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 61  qlite3Reindex(Pa
73f30 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  rse*, Token*, To
73f40 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
73f50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
73f60 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73  e3AlterFunctions
73f70 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49  (sqlite3*);.SQLI
73f80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
73f90 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61  sqlite3AlterRena
73fa0 6d 65 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20  meTable(Parse*, 
73fb0 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a  SrcList*, Token*
73fc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73fd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74  E int sqlite3Get
73fe0 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69  Token(const unsi
73ff0 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 69 6e 74  gned char *, int
74000 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
74010 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
74020 4e 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73  NestedParse(Pars
74030 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e*, const char*,
74040 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52   ...);.SQLITE_PR
74050 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
74060 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
74070 53 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 74  Statements(sqlit
74080 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e3*);.SQLITE_PRI
74090 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
740a0 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 50 61  CodeSubselect(Pa
740b0 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69  rse *, Expr *, i
740c0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
740d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
740e0 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28  lite3SelectPrep(
740f0 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c  Parse*, Select*,
74100 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a   NameContext*);.
74110 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
74120 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  nt sqlite3Resolv
74130 65 45 78 70 72 4e 61 6d 65 73 28 4e 61 6d 65 43  eExprNames(NameC
74140 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b  ontext*, Expr*);
74150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74160 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f  void sqlite3Reso
74170 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 50  lveSelectNames(P
74180 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20  arse*, Select*, 
74190 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53  NameContext*);.S
741a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
741b0 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  t sqlite3Resolve
741c0 4f 72 64 65 72 47 72 6f 75 70 42 79 28 50 61 72  OrderGroupBy(Par
741d0 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78  se*, Select*, Ex
741e0 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63  prList*, const c
741f0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
74200 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
74210 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28  e3ColumnDefault(
74220 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 20 2a 2c  Vdbe *, Table *,
74230 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
74240 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
74250 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69  sqlite3AlterFini
74260 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73  shAddColumn(Pars
74270 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53  e *, Token *);.S
74280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74290 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 42  id sqlite3AlterB
742a0 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61  eginAddColumn(Pa
742b0 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a  rse *, SrcList *
742c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
742d0 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
742e0 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 73 71 6c  e3GetCollSeq(sql
742f0 69 74 65 33 2a 2c 20 75 38 2c 20 43 6f 6c 6c 53  ite3*, u8, CollS
74300 65 71 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  eq *, const char
74310 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74320 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 41  TE char sqlite3A
74330 66 66 69 6e 69 74 79 54 79 70 65 28 63 6f 6e 73  ffinityType(cons
74340 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
74350 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74360 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 50 61 72  lite3Analyze(Par
74370 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b  se*, Token*, Tok
74380 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
74390 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
743a0 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65  InvokeBusyHandle
743b0 72 28 42 75 73 79 48 61 6e 64 6c 65 72 2a 29 3b  r(BusyHandler*);
743c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
743d0 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44  int sqlite3FindD
743e0 62 28 73 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65  b(sqlite3*, Toke
743f0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
74400 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
74410 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c 69 74 65  indDbName(sqlite
74420 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  3 *, const char 
74430 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74440 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 6e  TE int sqlite3An
74450 61 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c 69 74  alysisLoad(sqlit
74460 65 33 2a 2c 69 6e 74 20 69 44 42 29 3b 0a 53 51  e3*,int iDB);.SQ
74470 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
74480 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49  d sqlite3DeleteI
74490 6e 64 65 78 53 61 6d 70 6c 65 73 28 49 6e 64 65  ndexSamples(Inde
744a0 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  x*);.SQLITE_PRIV
744b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
744c0 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e  DefaultRowEst(In
744d0 64 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dex*);.SQLITE_PR
744e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
744f0 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75  e3RegisterLikeFu
74500 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a  nctions(sqlite3*
74510 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
74520 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
74530 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e  e3IsLikeFunction
74540 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c  (sqlite3*,Expr*,
74550 69 6e 74 2a 2c 63 68 61 72 2a 29 3b 0a 53 51 4c  int*,char*);.SQL
74560 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
74570 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46   sqlite3MinimumF
74580 69 6c 65 46 6f 72 6d 61 74 28 50 61 72 73 65 2a  ileFormat(Parse*
74590 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
745a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
745b0 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72   sqlite3SchemaFr
745c0 65 65 28 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49  ee(void *);.SQLI
745d0 54 45 5f 50 52 49 56 41 54 45 20 53 63 68 65 6d  TE_PRIVATE Schem
745e0 61 20 2a 73 71 6c 69 74 65 33 53 63 68 65 6d 61  a *sqlite3Schema
745f0 47 65 74 28 73 71 6c 69 74 65 33 20 2a 2c 20 42  Get(sqlite3 *, B
74600 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  tree *);.SQLITE_
74610 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
74620 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
74630 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 63  (sqlite3 *db, Sc
74640 68 65 6d 61 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  hema *);.SQLITE_
74650 50 52 49 56 41 54 45 20 4b 65 79 49 6e 66 6f 20  PRIVATE KeyInfo 
74660 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79  *sqlite3IndexKey
74670 69 6e 66 6f 28 50 61 72 73 65 20 2a 2c 20 49 6e  info(Parse *, In
74680 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  dex *);.SQLITE_P
74690 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
746a0 65 33 43 72 65 61 74 65 46 75 6e 63 28 73 71 6c  e3CreateFunc(sql
746b0 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68  ite3 *, const ch
746c0 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  ar *, int, int, 
746d0 76 6f 69 64 20 2a 2c 20 0a 20 20 76 6f 69 64 20  void *, .  void 
746e0 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (*)(sqlite3_cont
746f0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
74700 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f  _value **),.  vo
74710 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63  id (*)(sqlite3_c
74720 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
74730 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 20 76  te3_value **), v
74740 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f  oid (*)(sqlite3_
74750 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 53 51 4c 49  context*));.SQLI
74760 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
74770 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 73 71  qlite3ApiExit(sq
74780 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 29 3b  lite3 *db, int);
74790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
747a0 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  int sqlite3OpenT
747b0 65 6d 70 44 61 74 61 62 61 73 65 28 50 61 72 73  empDatabase(Pars
747c0 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  e *);..SQLITE_PR
747d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
747e0 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 53  e3StrAccumInit(S
747f0 74 72 41 63 63 75 6d 2a 2c 20 63 68 61 72 2a 2c  trAccum*, char*,
74800 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
74810 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
74820 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
74830 70 70 65 6e 64 28 53 74 72 41 63 63 75 6d 2a 2c  ppend(StrAccum*,
74840 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29  const char*,int)
74850 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74860 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 53 74   char *sqlite3St
74870 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53 74 72  rAccumFinish(Str
74880 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  Accum*);.SQLITE_
74890 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
748a0 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65  ite3StrAccumRese
748b0 74 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51  t(StrAccum*);.SQ
748c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
748d0 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  d sqlite3SelectD
748e0 65 73 74 49 6e 69 74 28 53 65 6c 65 63 74 44 65  estInit(SelectDe
748f0 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  st*,int,int);.SQ
74900 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
74910 72 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65  r *sqlite3Create
74920 43 6f 6c 75 6d 6e 45 78 70 72 28 73 71 6c 69 74  ColumnExpr(sqlit
74930 65 33 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c  e3 *, SrcList *,
74940 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c   int, int);..SQL
74950 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
74960 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65   sqlite3BackupRe
74970 73 74 61 72 74 28 73 71 6c 69 74 65 33 5f 62 61  start(sqlite3_ba
74980 63 6b 75 70 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  ckup *);.SQLITE_
74990 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
749a0 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65  ite3BackupUpdate
749b0 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
749c0 2a 2c 20 50 67 6e 6f 2c 20 63 6f 6e 73 74 20 75  *, Pgno, const u
749d0 38 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  8 *);../*.** The
749e0 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   interface to th
749f0 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72 61 74 65  e LEMON-generate
74a00 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53 51 4c 49  d parser.*/.SQLI
74a10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
74a20 2a 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c  *sqlite3ParserAl
74a30 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a  loc(void*(*)(siz
74a40 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  e_t));.SQLITE_PR
74a50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
74a60 65 33 50 61 72 73 65 72 46 72 65 65 28 76 6f 69  e3ParserFree(voi
74a70 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64  d*, void(*)(void
74a80 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *));.SQLITE_PRIV
74a90 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
74aa0 50 61 72 73 65 72 28 76 6f 69 64 2a 2c 20 69 6e  Parser(void*, in
74ab0 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72 73 65 2a  t, Token, Parse*
74ac0 29 3b 0a 23 69 66 64 65 66 20 59 59 54 52 41 43  );.#ifdef YYTRAC
74ad0 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a 53  KMAXSTACKDEPTH.S
74ae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
74af0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73 65  int sqlite3Parse
74b00 72 53 74 61 63 6b 50 65 61 6b 28 76 6f 69 64 2a  rStackPeak(void*
74b10 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54  );.#endif..SQLIT
74b20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
74b30 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78  qlite3AutoLoadEx
74b40 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33  tensions(sqlite3
74b50 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  *);.#ifndef SQLI
74b60 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
74b70 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52  ENSION.SQLITE_PR
74b80 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
74b90 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69  ite3CloseExtensi
74ba0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23  ons(sqlite3*);.#
74bb0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
74bc0 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73  lite3CloseExtens
74bd0 69 6f 6e 73 28 58 29 0a 23 65 6e 64 69 66 0a 0a  ions(X).#endif..
74be0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
74bf0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
74c00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74c10 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61    void sqlite3Ta
74c20 62 6c 65 4c 6f 63 6b 28 50 61 72 73 65 20 2a 2c  bleLock(Parse *,
74c30 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38 2c 20 63   int, int, u8, c
74c40 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 23 65  onst char *);.#e
74c50 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
74c60 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 76  lite3TableLock(v
74c70 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66  ,w,x,y,z).#endif
74c80 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
74c90 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
74ca0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
74cb0 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65  3Utf8To8(unsigne
74cc0 64 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66  d char*);.#endif
74cd0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
74ce0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
74cf0 45 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69  E.#  define sqli
74d00 74 65 33 56 74 61 62 43 6c 65 61 72 28 59 29 0a  te3VtabClear(Y).
74d10 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  #  define sqlite
74d20 33 56 74 61 62 53 79 6e 63 28 58 2c 59 29 20 53  3VtabSync(X,Y) S
74d30 51 4c 49 54 45 5f 4f 4b 0a 23 20 20 64 65 66 69  QLITE_OK.#  defi
74d40 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f  ne sqlite3VtabRo
74d50 6c 6c 62 61 63 6b 28 58 29 0a 23 20 20 64 65 66  llback(X).#  def
74d60 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 43  ine sqlite3VtabC
74d70 6f 6d 6d 69 74 28 58 29 0a 23 20 20 64 65 66 69  ommit(X).#  defi
74d80 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e  ne sqlite3VtabIn
74d90 53 79 6e 63 28 64 62 29 20 30 0a 23 20 20 64 65  Sync(db) 0.#  de
74da0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
74db0 4c 6f 63 6b 28 58 29 20 0a 23 20 20 64 65 66 69  Lock(X) .#  defi
74dc0 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e  ne sqlite3VtabUn
74dd0 6c 6f 63 6b 28 58 29 0a 23 20 20 64 65 66 69 6e  lock(X).#  defin
74de0 65 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c  e sqlite3VtabUnl
74df0 6f 63 6b 4c 69 73 74 28 58 29 0a 23 65 6c 73 65  ockList(X).#else
74e00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74e10 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56     void sqlite3V
74e20 74 61 62 43 6c 65 61 72 28 54 61 62 6c 65 2a 29  tabClear(Table*)
74e30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74e40 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56      int sqlite3V
74e50 74 61 62 53 79 6e 63 28 73 71 6c 69 74 65 33 20  tabSync(sqlite3 
74e60 2a 64 62 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53  *db, char **);.S
74e70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
74e80 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
74e90 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33  Rollback(sqlite3
74ea0 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52   *db);.SQLITE_PR
74eb0 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c  IVATE    int sql
74ec0 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 73  ite3VtabCommit(s
74ed0 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c  qlite3 *db);.SQL
74ee0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 76  ITE_PRIVATE    v
74ef0 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4c  oid sqlite3VtabL
74f00 6f 63 6b 28 56 54 61 62 6c 65 20 2a 29 3b 0a 53  ock(VTable *);.S
74f10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
74f20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
74f30 62 55 6e 6c 6f 63 6b 28 56 54 61 62 6c 65 20 2a  bUnlock(VTable *
74f40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74f50 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  E    void sqlite
74f60 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28  3VtabUnlockList(
74f70 73 71 6c 69 74 65 33 2a 29 3b 0a 23 20 20 64 65  sqlite3*);.#  de
74f80 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
74f90 49 6e 53 79 6e 63 28 64 62 29 20 28 28 64 62 29  InSync(db) ((db)
74fa0 2d 3e 6e 56 54 72 61 6e 73 3e 30 20 26 26 20 28  ->nVTrans>0 && (
74fb0 64 62 29 2d 3e 61 56 54 72 61 6e 73 3d 3d 30 29  db)->aVTrans==0)
74fc0 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50  .#endif.SQLITE_P
74fd0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74fe0 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61  te3VtabMakeWrita
74ff0 62 6c 65 28 50 61 72 73 65 2a 2c 54 61 62 6c 65  ble(Parse*,Table
75000 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
75010 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
75020 74 61 62 42 65 67 69 6e 50 61 72 73 65 28 50 61  tabBeginParse(Pa
75030 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  rse*, Token*, To
75040 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53  ken*, Token*);.S
75050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
75060 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 46 69  id sqlite3VtabFi
75070 6e 69 73 68 50 61 72 73 65 28 50 61 72 73 65 2a  nishParse(Parse*
75080 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
75090 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
750a0 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69  qlite3VtabArgIni
750b0 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  t(Parse*);.SQLIT
750c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
750d0 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78 74  qlite3VtabArgExt
750e0 65 6e 64 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  end(Parse*, Toke
750f0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
75100 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
75110 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 73 71  tabCallCreate(sq
75120 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e  lite3*, int, con
75130 73 74 20 63 68 61 72 20 2a 2c 20 63 68 61 72 20  st char *, char 
75140 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
75150 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
75160 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50  tabCallConnect(P
75170 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a  arse*, Table*);.
75180 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
75190 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
751a0 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65  llDestroy(sqlite
751b0 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  3*, int, const c
751c0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har *);.SQLITE_P
751d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
751e0 65 33 56 74 61 62 42 65 67 69 6e 28 73 71 6c 69  e3VtabBegin(sqli
751f0 74 65 33 20 2a 2c 20 56 54 61 62 6c 65 20 2a 29  te3 *, VTable *)
75200 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75210 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65   FuncDef *sqlite
75220 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e  3VtabOverloadFun
75230 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 2c  ction(sqlite3 *,
75240 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74 20 6e 41  FuncDef*, int nA
75250 72 67 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  rg, Expr*);.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 49 6e 76 61 6c 69 64 46 75  sqlite3InvalidFu
75280 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 5f 63  nction(sqlite3_c
75290 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
752a0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 53 51  te3_value**);.SQ
752b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
752c0 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72   sqlite3Transfer
752d0 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33  Bindings(sqlite3
752e0 5f 73 74 6d 74 20 2a 2c 20 73 71 6c 69 74 65 33  _stmt *, sqlite3
752f0 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 4c 49 54 45  _stmt *);.SQLITE
75300 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
75310 69 74 65 33 52 65 70 72 65 70 61 72 65 28 56 64  ite3Reprepare(Vd
75320 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
75330 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
75340 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65  3ExprListCheckLe
75350 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45 78 70  ngth(Parse*, Exp
75360 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68  rList*, const ch
75370 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
75380 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
75390 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61  lite3BinaryCompa
753a0 72 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  reCollSeq(Parse 
753b0 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 20  *, Expr *, Expr 
753c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
753d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 65  TE int sqlite3Te
753e0 6d 70 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74  mpInMemory(const
753f0 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49   sqlite3*);.SQLI
75400 54 45 5f 50 52 49 56 41 54 45 20 56 54 61 62 6c  TE_PRIVATE VTabl
75410 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 54 61  e *sqlite3GetVTa
75420 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 20 54 61  ble(sqlite3*, Ta
75430 62 6c 65 2a 29 3b 0a 0a 2f 2a 20 44 65 63 6c 61  ble*);../* Decla
75440 72 61 74 69 6f 6e 73 20 66 6f 72 20 66 75 6e 63  rations for func
75450 74 69 6f 6e 73 20 69 6e 20 66 6b 65 79 2e 63 2e  tions in fkey.c.
75460 20 41 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72   All of these ar
75470 65 20 72 65 70 6c 61 63 65 64 20 62 79 0a 2a 2a  e replaced by.**
75480 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 69 66   no-op macros if
75490 20 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45   OMIT_FOREIGN_KE
754a0 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20 49 6e  Y is defined. In
754b0 20 74 68 69 73 20 63 61 73 65 20 6e 6f 20 66 6f   this case no fo
754c0 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 20 66 75 6e  reign.** key fun
754d0 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 61 76  ctionality is av
754e0 61 69 6c 61 62 6c 65 2e 20 49 66 20 4f 4d 49 54  ailable. If OMIT
754f0 5f 54 52 49 47 47 45 52 20 69 73 20 64 65 66 69  _TRIGGER is defi
75500 6e 65 64 20 62 75 74 0a 2a 2a 20 4f 4d 49 54 5f  ned but.** OMIT_
75510 46 4f 52 45 49 47 4e 5f 4b 45 59 20 69 73 20 6e  FOREIGN_KEY is n
75520 6f 74 2c 20 6f 6e 6c 79 20 73 6f 6d 65 20 6f 66  ot, only some of
75530 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 61   the functions a
75540 72 65 20 6e 6f 2d 6f 70 65 64 2e 20 49 6e 0a 2a  re no-oped. In.*
75550 2a 20 74 68 69 73 20 63 61 73 65 20 66 6f 72 65  * this case fore
75560 69 67 6e 20 6b 65 79 73 20 61 72 65 20 70 61 72  ign keys are par
75570 73 65 64 2c 20 62 75 74 20 6e 6f 20 6f 74 68 65  sed, but no othe
75580 72 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20  r functionality 
75590 69 73 20 0a 2a 2a 20 70 72 6f 76 69 64 65 64 20  is .** provided 
755a0 28 65 6e 66 6f 72 63 65 6d 65 6e 74 20 6f 66 20  (enforcement of 
755b0 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 72  FK constraints r
755c0 65 71 75 69 72 65 73 20 74 68 65 20 74 72 69 67  equires the trig
755d0 67 65 72 73 20 73 75 62 2d 73 79 73 74 65 6d 29  gers sub-system)
755e0 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
755f0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  d(SQLITE_OMIT_FO
75600 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21 64  REIGN_KEY) && !d
75610 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
75620 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49  IT_TRIGGER).SQLI
75630 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
75640 64 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b  d sqlite3FkCheck
75650 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
75660 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
75670 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
75680 64 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54  d sqlite3FkDropT
75690 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63  able(Parse*, Src
756a0 4c 69 73 74 20 2a 2c 20 54 61 62 6c 65 2a 29 3b  List *, Table*);
756b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
756c0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b    void sqlite3Fk
756d0 41 63 74 69 6f 6e 73 28 50 61 72 73 65 2a 2c 20  Actions(Parse*, 
756e0 54 61 62 6c 65 2a 2c 20 45 78 70 72 4c 69 73 74  Table*, ExprList
756f0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
75700 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
75710 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28  lite3FkRequired(
75720 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
75730 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  int*, int);.SQLI
75740 54 45 5f 50 52 49 56 41 54 45 20 20 20 75 33 32  TE_PRIVATE   u32
75750 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73   sqlite3FkOldmas
75760 6b 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  k(Parse*, Table*
75770 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
75780 45 20 20 20 46 4b 65 79 20 2a 73 71 6c 69 74 65  E   FKey *sqlite
75790 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 54 61  3FkReferences(Ta
757a0 62 6c 65 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20  ble *);.#else.  
757b0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46  #define sqlite3F
757c0 6b 41 63 74 69 6f 6e 73 28 61 2c 62 2c 63 2c 64  kActions(a,b,c,d
757d0 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  ).  #define sqli
757e0 74 65 33 46 6b 43 68 65 63 6b 28 61 2c 62 2c 63  te3FkCheck(a,b,c
757f0 2c 64 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71  ,d).  #define sq
75800 6c 69 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65  lite3FkDropTable
75810 28 61 2c 62 2c 63 29 0a 20 20 23 64 65 66 69 6e  (a,b,c).  #defin
75820 65 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61  e sqlite3FkOldma
75830 73 6b 28 61 2c 62 29 20 20 20 20 20 20 30 0a 20  sk(a,b)      0. 
75840 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
75850 46 6b 52 65 71 75 69 72 65 64 28 61 2c 62 2c 63  FkRequired(a,b,c
75860 2c 64 29 20 30 0a 23 65 6e 64 69 66 0a 23 69 66  ,d) 0.#endif.#if
75870 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
75880 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 53 51 4c  _FOREIGN_KEY.SQL
75890 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
758a0 69 64 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65  id sqlite3FkDele
758b0 74 65 28 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73  te(Table*);.#els
758c0 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  e.  #define sqli
758d0 74 65 33 46 6b 44 65 6c 65 74 65 28 61 29 0a 23  te3FkDelete(a).#
758e0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76  endif.../*.** Av
758f0 61 69 6c 61 62 6c 65 20 66 61 75 6c 74 20 69 6e  ailable fault in
75900 6a 65 63 74 6f 72 73 2e 20 20 53 68 6f 75 6c 64  jectors.  Should
75910 20 62 65 20 6e 75 6d 62 65 72 65 64 20 62 65 67   be numbered beg
75920 69 6e 6e 69 6e 67 20 77 69 74 68 20 30 2e 0a 2a  inning with 0..*
75930 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
75940 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d  _FAULTINJECTOR_M
75950 41 4c 4c 4f 43 20 20 20 20 20 30 0a 23 64 65 66  ALLOC     0.#def
75960 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54  ine SQLITE_FAULT
75970 49 4e 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20 20  INJECTOR_COUNT  
75980 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65      1../*.** The
75990 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   interface to th
759a0 65 20 63 6f 64 65 20 69 6e 20 66 61 75 6c 74 2e  e code in fault.
759b0 63 20 75 73 65 64 20 66 6f 72 20 69 64 65 6e 74  c used for ident
759c0 69 66 79 69 6e 67 20 22 62 65 6e 69 67 6e 22 0a  ifying "benign".
759d0 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  ** malloc failur
759e0 65 73 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79  es. This is only
759f0 20 70 72 65 73 65 6e 74 20 69 66 20 53 51 4c 49   present if SQLI
75a00 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
75a10 54 45 53 54 0a 2a 2a 20 69 73 20 6e 6f 74 20 64  TEST.** is not d
75a20 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64  efined..*/.#ifnd
75a30 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
75a40 55 49 4c 54 49 4e 5f 54 45 53 54 0a 53 51 4c 49  UILTIN_TEST.SQLI
75a50 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
75a60 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65  d sqlite3BeginBe
75a70 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29  nignMalloc(void)
75a80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75a90 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45     void sqlite3E
75aa0 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76  ndBenignMalloc(v
75ab0 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64  oid);.#else.  #d
75ac0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 65 67  efine sqlite3Beg
75ad0 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
75ae0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
75af0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
75b00 63 28 29 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66  c().#endif..#def
75b10 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57  ine IN_INDEX_ROW
75b20 49 44 20 20 20 20 20 20 20 20 20 20 20 31 0a 23  ID           1.#
75b30 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f  define IN_INDEX_
75b40 45 50 48 20 20 20 20 20 20 20 20 20 20 20 20 20  EPH             
75b50 32 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44  2.#define IN_IND
75b60 45 58 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20  EX_INDEX        
75b70 20 20 20 33 0a 53 51 4c 49 54 45 5f 50 52 49 56     3.SQLITE_PRIV
75b80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
75b90 69 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65  indInIndex(Parse
75ba0 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2a   *, Expr *, int*
75bb0 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
75bc0 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
75bd0 57 52 49 54 45 0a 53 51 4c 49 54 45 5f 50 52 49  WRITE.SQLITE_PRI
75be0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
75bf0 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71  e3JournalOpen(sq
75c00 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
75c10 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74  st char *, sqlit
75c20 65 33 5f 66 69 6c 65 20 2a 2c 20 69 6e 74 2c 20  e3_file *, int, 
75c30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
75c40 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
75c50 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71  e3JournalSize(sq
75c60 6c 69 74 65 33 5f 76 66 73 20 2a 29 3b 0a 53 51  lite3_vfs *);.SQ
75c70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
75c80 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  nt sqlite3Journa
75c90 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 5f  lCreate(sqlite3_
75ca0 66 69 6c 65 20 2a 29 3b 0a 23 65 6c 73 65 0a 20  file *);.#else. 
75cb0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
75cc0 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73  JournalSize(pVfs
75cd0 29 20 28 28 70 56 66 73 29 2d 3e 73 7a 4f 73 46  ) ((pVfs)->szOsF
75ce0 69 6c 65 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c  ile).#endif..SQL
75cf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
75d00 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e   sqlite3MemJourn
75d10 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 66  alOpen(sqlite3_f
75d20 69 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ile *);.SQLITE_P
75d30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
75d40 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65  e3MemJournalSize
75d50 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50  (void);.SQLITE_P
75d60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
75d70 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73  e3IsMemJournal(s
75d80 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a  qlite3_file *);.
75d90 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
75da0 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 53 51 4c  EXPR_DEPTH>0.SQL
75db0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
75dc0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 53 65  id sqlite3ExprSe
75dd0 74 48 65 69 67 68 74 28 50 61 72 73 65 20 2a 70  tHeight(Parse *p
75de0 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 29 3b  Parse, Expr *p);
75df0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75e00 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c    int sqlite3Sel
75e10 65 63 74 45 78 70 72 48 65 69 67 68 74 28 53 65  ectExprHeight(Se
75e20 6c 65 63 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  lect *);.SQLITE_
75e30 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
75e40 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65  lite3ExprCheckHe
75e50 69 67 68 74 28 50 61 72 73 65 2a 2c 20 69 6e 74  ight(Parse*, int
75e60 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  );.#else.  #defi
75e70 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 53 65  ne sqlite3ExprSe
75e80 74 48 65 69 67 68 74 28 78 2c 79 29 0a 20 20 23  tHeight(x,y).  #
75e90 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 65  define sqlite3Se
75ea0 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28 78  lectExprHeight(x
75eb0 29 20 30 0a 20 20 23 64 65 66 69 6e 65 20 73 71  ) 0.  #define sq
75ec0 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65  lite3ExprCheckHe
75ed0 69 67 68 74 28 78 2c 79 29 0a 23 65 6e 64 69 66  ight(x,y).#endif
75ee0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
75ef0 20 75 33 32 20 73 71 6c 69 74 65 33 47 65 74 34   u32 sqlite3Get4
75f00 62 79 74 65 28 63 6f 6e 73 74 20 75 38 2a 29 3b  byte(const u8*);
75f10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75f20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 75 74 34  void sqlite3Put4
75f30 62 79 74 65 28 75 38 2a 2c 20 75 33 32 29 3b 0a  byte(u8*, u32);.
75f40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
75f50 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54  NABLE_UNLOCK_NOT
75f60 49 46 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  IFY.SQLITE_PRIVA
75f70 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
75f80 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b  3ConnectionBlock
75f90 65 64 28 73 71 6c 69 74 65 33 20 2a 2c 20 73 71  ed(sqlite3 *, sq
75fa0 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45  lite3 *);.SQLITE
75fb0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
75fc0 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
75fd0 6e 55 6e 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65  nUnlocked(sqlite
75fe0 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50  3 *db);.SQLITE_P
75ff0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
76000 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43  lite3ConnectionC
76010 6c 6f 73 65 64 28 73 71 6c 69 74 65 33 20 2a 64  losed(sqlite3 *d
76020 62 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66  b);.#else.  #def
76030 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  ine sqlite3Conne
76040 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 78 2c 79  ctionBlocked(x,y
76050 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  ).  #define sqli
76060 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c  te3ConnectionUnl
76070 6f 63 6b 65 64 28 78 29 0a 20 20 23 64 65 66 69  ocked(x).  #defi
76080 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63  ne sqlite3Connec
76090 74 69 6f 6e 43 6c 6f 73 65 64 28 78 29 0a 23 65  tionClosed(x).#e
760a0 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
760b0 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45  ITE_DEBUG.SQLITE
760c0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
760d0 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72 61  sqlite3ParserTra
760e0 63 65 28 46 49 4c 45 2a 2c 20 63 68 61 72 20 2a  ce(FILE*, char *
760f0 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  );.#endif../*.**
76100 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 45   If the SQLITE_E
76110 4e 41 42 4c 45 20 49 4f 54 52 41 43 45 20 65 78  NABLE IOTRACE ex
76120 69 73 74 73 20 74 68 65 6e 20 74 68 65 20 67 6c  ists then the gl
76130 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 0a 2a 2a  obal variable.**
76140 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20   sqlite3IoTrace 
76150 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
76160 61 20 70 72 69 6e 74 66 2d 6c 69 6b 65 20 72 6f  a printf-like ro
76170 75 74 69 6e 65 20 75 73 65 64 20 74 6f 0a 2a 2a  utine used to.**
76180 20 70 72 69 6e 74 20 49 2f 4f 20 74 72 61 63 69   print I/O traci
76190 6e 67 20 6d 65 73 73 61 67 65 73 2e 20 0a 2a 2f  ng messages. .*/
761a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
761b0 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 0a 23 20  NABLE_IOTRACE.# 
761c0 64 65 66 69 6e 65 20 49 4f 54 52 41 43 45 28 41  define IOTRACE(A
761d0 29 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f  )  if( sqlite3Io
761e0 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33  Trace ){ sqlite3
761f0 49 6f 54 72 61 63 65 20 41 3b 20 7d 0a 53 51 4c  IoTrace A; }.SQL
76200 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
76210 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f  id sqlite3VdbeIO
76220 54 72 61 63 65 53 71 6c 28 56 64 62 65 2a 29 3b  TraceSql(Vdbe*);
76230 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
76240 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 49 6f  void (*sqlite3Io
76250 54 72 61 63 65 29 28 63 6f 6e 73 74 20 63 68 61  Trace)(const cha
76260 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c 73 65 0a 23  r*,...);.#else.#
76270 20 64 65 66 69 6e 65 20 49 4f 54 52 41 43 45 28   define IOTRACE(
76280 41 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  A).# define sqli
76290 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71  te3VdbeIOTraceSq
762a0 6c 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  l(X).#endif..#en
762b0 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
762c0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69  **** End of sqli
762d0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
762e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
762f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76300 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
76310 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
76320 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  global.c *******
76330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76350 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
76360 4a 75 6e 65 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68  June 13.**.** Th
76370 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
76380 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
76390 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
763a0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
763b0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
763c0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
763d0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
763e0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
763f0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
76400 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
76410 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
76420 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
76430 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
76440 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
76450 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
76460 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
76470 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
76480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
764a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
764b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
764c0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
764d0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
764e0 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 67  definitions of g
764f0 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20  lobal variables 
76500 61 6e 64 20 63 6f 6e 74 61 6e 74 73 2e 0a 2a 2f  and contants..*/
76510 0a 0a 0a 2f 2a 20 41 6e 20 61 72 72 61 79 20 74  .../* An array t
76520 6f 20 6d 61 70 20 61 6c 6c 20 75 70 70 65 72 2d  o map all upper-
76530 63 61 73 65 20 63 68 61 72 61 63 74 65 72 73 20  case characters 
76540 69 6e 74 6f 20 74 68 65 69 72 20 63 6f 72 72 65  into their corre
76550 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6c 6f 77 65  sponding.** lowe
76560 72 2d 63 61 73 65 20 63 68 61 72 61 63 74 65 72  r-case character
76570 2e 20 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  . .**.** SQLite 
76580 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72 73 20 55  only considers U
76590 53 2d 41 53 43 49 49 20 28 6f 72 20 45 42 43 44  S-ASCII (or EBCD
765a0 49 43 29 20 63 68 61 72 61 63 74 65 72 73 2e 20  IC) characters. 
765b0 20 57 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 68 61   We do not.** ha
765c0 6e 64 6c 65 20 63 61 73 65 20 63 6f 6e 76 65 72  ndle case conver
765d0 73 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 55 54  sions for the UT
765e0 46 20 63 68 61 72 61 63 74 65 72 20 73 65 74 20  F character set 
765f0 73 69 6e 63 65 20 74 68 65 20 74 61 62 6c 65 73  since the tables
76600 0a 2a 2a 20 69 6e 76 6f 6c 76 65 64 20 61 72 65  .** involved are
76610 20 6e 65 61 72 6c 79 20 61 73 20 62 69 67 20 6f   nearly as big o
76620 72 20 62 69 67 67 65 72 20 74 68 61 6e 20 53 51  r bigger than SQ
76630 4c 69 74 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a  Lite itself..*/.
76640 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
76650 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
76660 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 54  ar sqlite3UpperT
76670 6f 4c 6f 77 65 72 5b 5d 20 3d 20 7b 0a 23 69 66  oLower[] = {.#if
76680 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49  def SQLITE_ASCII
76690 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32  .      0,  1,  2
766a0 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36  ,  3,  4,  5,  6
766b0 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30  ,  7,  8,  9, 10
766c0 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34  , 11, 12, 13, 14
766d0 2c 20 31 35 2c 20 31 36 2c 20 31 37 2c 0a 20 20  , 15, 16, 17,.  
766e0 20 20 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32     18, 19, 20, 2
766f0 31 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 32  1, 22, 23, 24, 2
76700 35 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 32  5, 26, 27, 28, 2
76710 39 2c 20 33 30 2c 20 33 31 2c 20 33 32 2c 20 33  9, 30, 31, 32, 3
76720 33 2c 20 33 34 2c 20 33 35 2c 0a 20 20 20 20 20  3, 34, 35,.     
76730 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20  36, 37, 38, 39, 
76740 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20  40, 41, 42, 43, 
76750 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20  44, 45, 46, 47, 
76760 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20  48, 49, 50, 51, 
76770 35 32 2c 20 35 33 2c 0a 20 20 20 20 20 35 34 2c  52, 53,.     54,
76780 20 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c   55, 56, 57, 58,
76790 20 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c   59, 60, 61, 62,
767a0 20 36 33 2c 20 36 34 2c 20 39 37 2c 20 39 38 2c   63, 64, 97, 98,
767b0 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c   99,100,101,102,
767c0 31 30 33 2c 0a 20 20 20 20 31 30 34 2c 31 30 35  103,.    104,105
767d0 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39  ,106,107,108,109
767e0 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33  ,110,111,112,113
767f0 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37  ,114,115,116,117
76800 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31  ,118,119,120,121
76810 2c 0a 20 20 20 20 31 32 32 2c 20 39 31 2c 20 39  ,.    122, 91, 9
76820 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 39  2, 93, 94, 95, 9
76830 36 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30  6, 97, 98, 99,10
76840 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30  0,101,102,103,10
76850 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 0a 20  4,105,106,107,. 
76860 20 20 20 31 30 38 2c 31 30 39 2c 31 31 30 2c 31     108,109,110,1
76870 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31  11,112,113,114,1
76880 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31  15,116,117,118,1
76890 31 39 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 31  19,120,121,122,1
768a0 32 33 2c 31 32 34 2c 31 32 35 2c 0a 20 20 20 20  23,124,125,.    
768b0 31 32 36 2c 31 32 37 2c 31 32 38 2c 31 32 39 2c  126,127,128,129,
768c0 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c  130,131,132,133,
768d0 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c  134,135,136,137,
768e0 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c  138,139,140,141,
768f0 31 34 32 2c 31 34 33 2c 0a 20 20 20 20 31 34 34  142,143,.    144
76900 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38  ,145,146,147,148
76910 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32  ,149,150,151,152
76920 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36  ,153,154,155,156
76930 2c 31 35 37 2c 31 35 38 2c 31 35 39 2c 31 36 30  ,157,158,159,160
76940 2c 31 36 31 2c 0a 20 20 20 20 31 36 32 2c 31 36  ,161,.    162,16
76950 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36  3,164,165,166,16
76960 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37  7,168,169,170,17
76970 31 2c 31 37 32 2c 31 37 33 2c 31 37 34 2c 31 37  1,172,173,174,17
76980 35 2c 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37  5,176,177,178,17
76990 39 2c 0a 20 20 20 20 31 38 30 2c 31 38 31 2c 31  9,.    180,181,1
769a0 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 31  82,183,184,185,1
769b0 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 31  86,187,188,189,1
769c0 39 30 2c 31 39 31 2c 31 39 32 2c 31 39 33 2c 31  90,191,192,193,1
769d0 39 34 2c 31 39 35 2c 31 39 36 2c 31 39 37 2c 0a  94,195,196,197,.
769e0 20 20 20 20 31 39 38 2c 31 39 39 2c 32 30 30 2c      198,199,200,
769f0 32 30 31 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c  201,202,203,204,
76a00 32 30 35 2c 32 30 36 2c 32 30 37 2c 32 30 38 2c  205,206,207,208,
76a10 32 30 39 2c 32 31 30 2c 32 31 31 2c 32 31 32 2c  209,210,211,212,
76a20 32 31 33 2c 32 31 34 2c 32 31 35 2c 0a 20 20 20  213,214,215,.   
76a30 20 32 31 36 2c 32 31 37 2c 32 31 38 2c 32 31 39   216,217,218,219
76a40 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 33  ,220,221,222,223
76a50 2c 32 32 34 2c 32 32 35 2c 32 32 36 2c 32 32 37  ,224,225,226,227
76a60 2c 32 32 38 2c 32 32 39 2c 32 33 30 2c 32 33 31  ,228,229,230,231
76a70 2c 32 33 32 2c 32 33 33 2c 0a 20 20 20 20 32 33  ,232,233,.    23
76a80 34 2c 32 33 35 2c 32 33 36 2c 32 33 37 2c 32 33  4,235,236,237,23
76a90 38 2c 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34  8,239,240,241,24
76aa0 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34  2,243,244,245,24
76ab0 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 35  6,247,248,249,25
76ac0 30 2c 32 35 31 2c 0a 20 20 20 20 32 35 32 2c 32  0,251,.    252,2
76ad0 35 33 2c 32 35 34 2c 32 35 35 0a 23 65 6e 64 69  53,254,255.#endi
76ae0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
76af0 45 42 43 44 49 43 0a 20 20 20 20 20 20 30 2c 20  EBCDIC.      0, 
76b00 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20   1,  2,  3,  4, 
76b10 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20   5,  6,  7,  8, 
76b20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20   9, 10, 11, 12, 
76b30 31 33 2c 20 31 34 2c 20 31 35 2c 20 2f 2a 20 30  13, 14, 15, /* 0
76b40 78 20 2a 2f 0a 20 20 20 20 20 31 36 2c 20 31 37  x */.     16, 17
76b50 2c 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 31  , 18, 19, 20, 21
76b60 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 35  , 22, 23, 24, 25
76b70 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 39  , 26, 27, 28, 29
76b80 2c 20 33 30 2c 20 33 31 2c 20 2f 2a 20 31 78 20  , 30, 31, /* 1x 
76b90 2a 2f 0a 20 20 20 20 20 33 32 2c 20 33 33 2c 20  */.     32, 33, 
76ba0 33 34 2c 20 33 35 2c 20 33 36 2c 20 33 37 2c 20  34, 35, 36, 37, 
76bb0 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20  38, 39, 40, 41, 
76bc0 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20  42, 43, 44, 45, 
76bd0 34 36 2c 20 34 37 2c 20 2f 2a 20 32 78 20 2a 2f  46, 47, /* 2x */
76be0 0a 20 20 20 20 20 34 38 2c 20 34 39 2c 20 35 30  .     48, 49, 50
76bf0 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 20 35 34  , 51, 52, 53, 54
76c00 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 38  , 55, 56, 57, 58
76c10 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 32  , 59, 60, 61, 62
76c20 2c 20 36 33 2c 20 2f 2a 20 33 78 20 2a 2f 0a 20  , 63, /* 3x */. 
76c30 20 20 20 20 36 34 2c 20 36 35 2c 20 36 36 2c 20      64, 65, 66, 
76c40 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20  67, 68, 69, 70, 
76c50 37 31 2c 20 37 32 2c 20 37 33 2c 20 37 34 2c 20  71, 72, 73, 74, 
76c60 37 35 2c 20 37 36 2c 20 37 37 2c 20 37 38 2c 20  75, 76, 77, 78, 
76c70 37 39 2c 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20  79, /* 4x */.   
76c80 20 20 38 30 2c 20 38 31 2c 20 38 32 2c 20 38 33    80, 81, 82, 83
76c90 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37  , 84, 85, 86, 87
76ca0 2c 20 38 38 2c 20 38 39 2c 20 39 30 2c 20 39 31  , 88, 89, 90, 91
76cb0 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 35  , 92, 93, 94, 95
76cc0 2c 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 20  , /* 5x */.     
76cd0 39 36 2c 20 39 37 2c 20 36 36 2c 20 36 37 2c 20  96, 97, 66, 67, 
76ce0 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20  68, 69, 70, 71, 
76cf0 37 32 2c 20 37 33 2c 31 30 36 2c 31 30 37 2c 31  72, 73,106,107,1
76d00 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 20  08,109,110,111, 
76d10 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 31 32  /* 6x */.    112
76d20 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34  , 81, 82, 83, 84
76d30 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38  , 85, 86, 87, 88
76d40 2c 20 38 39 2c 31 32 32 2c 31 32 33 2c 31 32 34  , 89,122,123,124
76d50 2c 31 32 35 2c 31 32 36 2c 31 32 37 2c 20 2f 2a  ,125,126,127, /*
76d60 20 37 78 20 2a 2f 0a 20 20 20 20 31 32 38 2c 31   7x */.    128,1
76d70 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31  29,130,131,132,1
76d80 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31  33,134,135,136,1
76d90 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 31  37,138,139,140,1
76da0 34 31 2c 31 34 32 2c 31 34 33 2c 20 2f 2a 20 38  41,142,143, /* 8
76db0 78 20 2a 2f 0a 20 20 20 20 31 34 34 2c 31 34 35  x */.    144,145
76dc0 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39  ,146,147,148,149
76dd0 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33  ,150,151,152,153
76de0 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37  ,154,155,156,157
76df0 2c 31 35 36 2c 31 35 39 2c 20 2f 2a 20 39 78 20  ,156,159, /* 9x 
76e00 2a 2f 0a 20 20 20 20 31 36 30 2c 31 36 31 2c 31  */.    160,161,1
76e10 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31  62,163,164,165,1
76e20 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31  66,167,168,169,1
76e30 37 30 2c 31 37 31 2c 31 34 30 2c 31 34 31 2c 31  70,171,140,141,1
76e40 34 32 2c 31 37 35 2c 20 2f 2a 20 41 78 20 2a 2f  42,175, /* Ax */
76e50 0a 20 20 20 20 31 37 36 2c 31 37 37 2c 31 37 38  .    176,177,178
76e60 2c 31 37 39 2c 31 38 30 2c 31 38 31 2c 31 38 32  ,179,180,181,182
76e70 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36  ,183,184,185,186
76e80 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30  ,187,188,189,190
76e90 2c 31 39 31 2c 20 2f 2a 20 42 78 20 2a 2f 0a 20  ,191, /* Bx */. 
76ea0 20 20 20 31 39 32 2c 31 32 39 2c 31 33 30 2c 31     192,129,130,1
76eb0 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31  31,132,133,134,1
76ec0 33 35 2c 31 33 36 2c 31 33 37 2c 32 30 32 2c 32  35,136,137,202,2
76ed0 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32  03,204,205,206,2
76ee0 30 37 2c 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20  07, /* Cx */.   
76ef0 20 32 30 38 2c 31 34 35 2c 31 34 36 2c 31 34 37   208,145,146,147
76f00 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31  ,148,149,150,151
76f10 2c 31 35 32 2c 31 35 33 2c 32 31 38 2c 32 31 39  ,152,153,218,219
76f20 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 33  ,220,221,222,223
76f30 2c 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 32  , /* Dx */.    2
76f40 32 34 2c 32 32 35 2c 31 36 32 2c 31 36 33 2c 31  24,225,162,163,1
76f50 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31  64,165,166,167,1
76f60 36 38 2c 31 36 39 2c 32 33 32 2c 32 30 33 2c 32  68,169,232,203,2
76f70 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20  04,205,206,207, 
76f80 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20 32 33 39  /* Ex */.    239
76f90 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33  ,240,241,242,243
76fa0 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37  ,244,245,246,247
76fb0 2c 32 34 38 2c 32 34 39 2c 32 31 39 2c 32 32 30  ,248,249,219,220
76fc0 2c 32 32 31 2c 32 32 32 2c 32 35 35 2c 20 2f 2a  ,221,222,255, /*
76fd0 20 46 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b   Fx */.#endif.};
76fe0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
76ff0 6f 77 69 6e 67 20 32 35 36 20 62 79 74 65 20 6c  owing 256 byte l
77000 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75  ookup table is u
77010 73 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 53  sed to support S
77020 51 4c 69 74 65 73 20 62 75 69 6c 74 2d 69 6e 0a  QLites built-in.
77030 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 73 20 74  ** equivalents t
77040 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
77050 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79  standard library
77060 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a   functions:.**.*
77070 2a 20 20 20 69 73 73 70 61 63 65 28 29 20 20 20  *   isspace()   
77080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77090 20 20 20 20 20 30 78 30 31 0a 2a 2a 20 20 20 69       0x01.**   i
770a0 73 61 6c 70 68 61 28 29 20 20 20 20 20 20 20 20  salpha()        
770b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
770c0 30 78 30 32 0a 2a 2a 20 20 20 69 73 64 69 67 69  0x02.**   isdigi
770d0 74 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20  t()             
770e0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 34 0a             0x04.
770f0 2a 2a 20 20 20 69 73 61 6c 6e 75 6d 28 29 20 20  **   isalnum()  
77100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77110 20 20 20 20 20 20 30 78 30 36 0a 2a 2a 20 20 20        0x06.**   
77120 69 73 78 64 69 67 69 74 28 29 20 20 20 20 20 20  isxdigit()      
77130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77140 20 30 78 30 38 0a 2a 2a 20 20 20 74 6f 75 70 70   0x08.**   toupp
77150 65 72 28 29 20 20 20 20 20 20 20 20 20 20 20 20  er()            
77160 20 20 20 20 20 20 20 20 20 20 20 20 30 78 32 30              0x20
77170 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 78 32 30 20  .**.** Bit 0x20 
77180 69 73 20 73 65 74 20 69 66 20 74 68 65 20 6d 61  is set if the ma
77190 70 70 65 64 20 63 68 61 72 61 63 74 65 72 20 72  pped character r
771a0 65 71 75 69 72 65 73 20 74 72 61 6e 73 6c 61 74  equires translat
771b0 69 6f 6e 20 74 6f 20 75 70 70 65 72 0a 2a 2a 20  ion to upper.** 
771c0 63 61 73 65 2e 20 69 2e 65 2e 20 69 66 20 74 68  case. i.e. if th
771d0 65 20 63 68 61 72 61 63 74 65 72 20 69 73 20 61  e character is a
771e0 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 43 49   lower-case ASCI
771f0 49 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20  I character..** 
77200 49 66 20 78 20 69 73 20 61 20 6c 6f 77 65 72 2d  If x is a lower-
77210 63 61 73 65 20 41 53 43 49 49 20 63 68 61 72 61  case ASCII chara
77220 63 74 65 72 2c 20 74 68 65 6e 20 69 74 73 20 75  cter, then its u
77230 70 70 65 72 2d 63 61 73 65 20 65 71 75 69 76 61  pper-case equiva
77240 6c 65 6e 74 0a 2a 2a 20 69 73 20 28 78 20 2d 20  lent.** is (x - 
77250 30 78 32 30 29 2e 20 54 68 65 72 65 66 6f 72 65  0x20). Therefore
77260 20 74 6f 75 70 70 65 72 28 29 20 63 61 6e 20 62   toupper() can b
77270 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73  e implemented as
77280 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 78 20 26 20 7e  :.**.**   (x & ~
77290 28 6d 61 70 5b 78 5d 26 30 78 32 30 29 29 0a 2a  (map[x]&0x20)).*
772a0 2a 0a 2a 2a 20 53 74 61 6e 64 61 72 64 20 66 75  *.** Standard fu
772b0 6e 63 74 69 6f 6e 20 74 6f 6c 6f 77 65 72 28 29  nction tolower()
772c0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
772d0 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  using the sqlite
772e0 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 0a  3UpperToLower[].
772f0 2a 2a 20 61 72 72 61 79 2e 20 74 6f 6c 6f 77 65  ** array. tolowe
77300 72 28 29 20 69 73 20 75 73 65 64 20 6d 6f 72 65  r() is used more
77310 20 6f 66 74 65 6e 20 74 68 61 6e 20 74 6f 75 70   often than toup
77320 70 65 72 28 29 20 62 79 20 53 51 4c 69 74 65 2e  per() by SQLite.
77330 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 27 73 20  .**.** SQLite's 
77340 76 65 72 73 69 6f 6e 73 20 61 72 65 20 69 64 65  versions are ide
77350 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 73 74  ntical to the st
77360 61 6e 64 61 72 64 20 76 65 72 73 69 6f 6e 73 20  andard versions 
77370 61 73 73 75 6d 69 6e 67 20 61 0a 2a 2a 20 6c 6f  assuming a.** lo
77380 63 61 6c 65 20 6f 66 20 22 43 22 2e 20 54 68 65  cale of "C". The
77390 79 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65  y are implemente
773a0 64 20 61 73 20 6d 61 63 72 6f 73 20 69 6e 20 73  d as macros in s
773b0 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 23  qliteInt.h..*/.#
773c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
773d0 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  II.SQLITE_PRIVAT
773e0 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  E const unsigned
773f0 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 79   char sqlite3Cty
77400 70 65 4d 61 70 5b 32 35 36 5d 20 3d 20 7b 0a 20  peMap[256] = {. 
77410 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77420 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77430 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77440 20 20 2f 2a 20 30 30 2e 2e 30 37 20 20 20 20 2e    /* 00..07    .
77450 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30  ....... */.  0x0
77460 30 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30  0, 0x01, 0x01, 0
77470 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c  x01, 0x01, 0x01,
77480 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
77490 20 30 38 2e 2e 30 66 20 20 20 20 2e 2e 2e 2e 2e   08..0f    .....
774a0 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30  ... */.  0x00, 0
774b0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
774c0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
774d0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 31 30 2e  0, 0x00,  /* 10.
774e0 2e 31 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .17    ........ 
774f0 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c  */.  0x00, 0x00,
77500 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77510 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77520 78 30 30 2c 20 20 2f 2a 20 31 38 2e 2e 31 66 20  x00,  /* 18..1f 
77530 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
77540 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30   0x01, 0x00, 0x0
77550 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77560 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77570 20 20 2f 2a 20 32 30 2e 2e 32 37 20 20 20 20 20    /* 20..27     
77580 21 22 23 24 25 26 27 20 2a 2f 0a 20 20 30 78 30  !"#$%&' */.  0x0
77590 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
775a0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
775b0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
775c0 20 32 38 2e 2e 32 66 20 20 20 20 28 29 2a 2b 2c   28..2f    ()*+,
775d0 2d 2e 2f 20 2a 2f 0a 20 20 30 78 30 63 2c 20 30  -./ */.  0x0c, 0
775e0 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c  x0c, 0x0c, 0x0c,
775f0 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30   0x0c, 0x0c, 0x0
77600 63 2c 20 30 78 30 63 2c 20 20 2f 2a 20 33 30 2e  c, 0x0c,  /* 30.
77610 2e 33 37 20 20 20 20 30 31 32 33 34 35 36 37 20  .37    01234567 
77620 2a 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c  */.  0x0c, 0x0c,
77630 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77640 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77650 78 30 30 2c 20 20 2f 2a 20 33 38 2e 2e 33 66 20  x00,  /* 38..3f 
77660 20 20 20 38 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a     89:;<=>? */..
77670 20 20 30 78 30 30 2c 20 30 78 30 61 2c 20 30 78    0x00, 0x0a, 0x
77680 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20  0a, 0x0a, 0x0a, 
77690 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 32  0x0a, 0x0a, 0x02
776a0 2c 20 20 2f 2a 20 34 30 2e 2e 34 37 20 20 20 20  ,  /* 40..47    
776b0 40 41 42 43 44 45 46 47 20 2a 2f 0a 20 20 30 78  @ABCDEFG */.  0x
776c0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
776d0 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
776e0 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 20 2f  , 0x02, 0x02,  /
776f0 2a 20 34 38 2e 2e 34 66 20 20 20 20 48 49 4a 4b  * 48..4f    HIJK
77700 4c 4d 4e 4f 20 2a 2f 0a 20 20 30 78 30 32 2c 20  LMNO */.  0x02, 
77710 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
77720 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
77730 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 35 30  02, 0x02,  /* 50
77740 2e 2e 35 37 20 20 20 20 50 51 52 53 54 55 56 57  ..57    PQRSTUVW
77750 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32   */.  0x02, 0x02
77760 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78  , 0x02, 0x00, 0x
77770 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77780 30 78 30 30 2c 20 20 2f 2a 20 35 38 2e 2e 35 66  0x00,  /* 58..5f
77790 20 20 20 20 58 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a      XYZ[\]^_ */.
777a0 20 20 30 78 30 30 2c 20 30 78 32 61 2c 20 30 78    0x00, 0x2a, 0x
777b0 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20  2a, 0x2a, 0x2a, 
777c0 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 32  0x2a, 0x2a, 0x22
777d0 2c 20 20 2f 2a 20 36 30 2e 2e 36 37 20 20 20 20  ,  /* 60..67    
777e0 60 61 62 63 64 65 66 67 20 2a 2f 0a 20 20 30 78  `abcdefg */.  0x
777f0 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
77800 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
77810 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 20 2f  , 0x22, 0x22,  /
77820 2a 20 36 38 2e 2e 36 66 20 20 20 20 68 69 6a 6b  * 68..6f    hijk
77830 6c 6d 6e 6f 20 2a 2f 0a 20 20 30 78 32 32 2c 20  lmno */.  0x22, 
77840 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
77850 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78  , 0x22, 0x22, 0x
77860 32 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 37 30  22, 0x22,  /* 70
77870 2e 2e 37 37 20 20 20 20 70 71 72 73 74 75 76 77  ..77    pqrstuvw
77880 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32   */.  0x22, 0x22
77890 2c 20 30 78 32 32 2c 20 30 78 30 30 2c 20 30 78  , 0x22, 0x00, 0x
778a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
778b0 30 78 30 30 2c 20 20 2f 2a 20 37 38 2e 2e 37 66  0x00,  /* 78..7f
778c0 20 20 20 20 78 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a      xyz{|}~. */.
778d0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
778e0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
778f0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77900 30 2c 20 20 2f 2a 20 38 30 2e 2e 38 37 20 20 20  0,  /* 80..87   
77910 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
77920 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77930 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77940 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
77950 2f 2a 20 38 38 2e 2e 38 66 20 20 20 20 2e 2e 2e  /* 88..8f    ...
77960 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c  ..... */.  0x00,
77970 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77980 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77990 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 39  x00, 0x00,  /* 9
779a0 30 2e 2e 39 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  0..97    .......
779b0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
779c0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
779d0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
779e0 20 30 78 30 30 2c 20 20 2f 2a 20 39 38 2e 2e 39   0x00,  /* 98..9
779f0 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  f    ........ */
77a00 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
77a10 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77a20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77a30 30 2c 20 20 2f 2a 20 61 30 2e 2e 61 37 20 20 20  0,  /* a0..a7   
77a40 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
77a50 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77a60 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77a70 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
77a80 2f 2a 20 61 38 2e 2e 61 66 20 20 20 20 2e 2e 2e  /* a8..af    ...
77a90 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c  ..... */.  0x00,
77aa0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77ab0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77ac0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 62  x00, 0x00,  /* b
77ad0 30 2e 2e 62 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  0..b7    .......
77ae0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
77af0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77b00 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77b10 20 30 78 30 30 2c 20 20 2f 2a 20 62 38 2e 2e 62   0x00,  /* b8..b
77b20 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  f    ........ */
77b30 0a 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  ..  0x00, 0x00, 
77b40 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77b50 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77b60 30 30 2c 20 20 2f 2a 20 63 30 2e 2e 63 37 20 20  00,  /* c0..c7  
77b70 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
77b80 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77b90 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77ba0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77bb0 20 2f 2a 20 63 38 2e 2e 63 66 20 20 20 20 2e 2e   /* c8..cf    ..
77bc0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30  ...... */.  0x00
77bd0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77be0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77bf0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
77c00 64 30 2e 2e 64 37 20 20 20 20 2e 2e 2e 2e 2e 2e  d0..d7    ......
77c10 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
77c20 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77c30 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77c40 2c 20 30 78 30 30 2c 20 20 2f 2a 20 64 38 2e 2e  , 0x00,  /* d8..
77c50 64 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  df    ........ *
77c60 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
77c70 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77c80 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77c90 30 30 2c 20 20 2f 2a 20 65 30 2e 2e 65 37 20 20  00,  /* e0..e7  
77ca0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
77cb0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77cc0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77cd0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77ce0 20 2f 2a 20 65 38 2e 2e 65 66 20 20 20 20 2e 2e   /* e8..ef    ..
77cf0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30  ...... */.  0x00
77d00 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77d10 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77d20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
77d30 66 30 2e 2e 66 37 20 20 20 20 2e 2e 2e 2e 2e 2e  f0..f7    ......
77d40 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
77d50 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77d60 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77d70 2c 20 30 78 30 30 20 20 20 2f 2a 20 66 38 2e 2e  , 0x00   /* f8..
77d80 66 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  ff    ........ *
77d90 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f  /.};.#endif..../
77da0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
77db0 6e 67 20 73 69 6e 67 6c 65 74 6f 6e 20 63 6f 6e  ng singleton con
77dc0 74 61 69 6e 73 20 74 68 65 20 67 6c 6f 62 61 6c  tains the global
77dd0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66   configuration f
77de0 6f 72 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65  or.** the SQLite
77df0 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 53 51 4c   library..*/.SQL
77e00 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49  ITE_PRIVATE SQLI
77e10 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 53 71  TE_WSD struct Sq
77e20 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 71 6c 69  lite3Config sqli
77e30 74 65 33 43 6f 6e 66 69 67 20 3d 20 7b 0a 20 20  te3Config = {.  
77e40 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
77e50 4d 45 4d 53 54 41 54 55 53 2c 20 20 2f 2a 20 62  MEMSTATUS,  /* b
77e60 4d 65 6d 73 74 61 74 20 2a 2f 0a 20 20 20 31 2c  Memstat */.   1,
77e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77e80 20 20 20 20 20 20 20 20 20 2f 2a 20 62 43 6f 72           /* bCor
77e90 65 4d 75 74 65 78 20 2a 2f 0a 20 20 20 53 51 4c  eMutex */.   SQL
77ea0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d  ITE_THREADSAFE==
77eb0 31 2c 20 20 20 20 20 20 2f 2a 20 62 46 75 6c 6c  1,      /* bFull
77ec0 4d 75 74 65 78 20 2a 2f 0a 20 20 20 30 78 37 66  Mutex */.   0x7f
77ed0 66 66 66 66 66 65 2c 20 20 20 20 20 20 20 20 20  fffffe,         
77ee0 20 20 20 20 20 20 20 2f 2a 20 6d 78 53 74 72 6c         /* mxStrl
77ef0 65 6e 20 2a 2f 0a 20 20 20 31 30 30 2c 20 20 20  en */.   100,   
77f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77f10 20 20 20 20 2f 2a 20 73 7a 4c 6f 6f 6b 61 73 69      /* szLookasi
77f20 64 65 20 2a 2f 0a 20 20 20 35 30 30 2c 20 20 20  de */.   500,   
77f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77f40 20 20 20 20 2f 2a 20 6e 4c 6f 6f 6b 61 73 69 64      /* nLookasid
77f50 65 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30  e */.   {0,0,0,0
77f60 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20 20  ,0,0,0,0},      
77f70 20 20 20 2f 2a 20 6d 20 2a 2f 0a 20 20 20 7b 30     /* m */.   {0
77f80 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30  ,0,0,0,0,0,0,0,0
77f90 7d 2c 20 20 20 20 20 20 20 2f 2a 20 6d 75 74 65  },       /* mute
77fa0 78 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30  x */.   {0,0,0,0
77fb0 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c  ,0,0,0,0,0,0,0},
77fc0 20 20 20 2f 2a 20 70 63 61 63 68 65 20 2a 2f 0a     /* pcache */.
77fd0 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20     (void*)0,    
77fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
77ff0 20 70 48 65 61 70 20 2a 2f 0a 20 20 20 30 2c 20   pHeap */.   0, 
78000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78010 20 20 20 20 20 20 20 20 2f 2a 20 6e 48 65 61 70          /* nHeap
78020 20 2a 2f 0a 20 20 20 30 2c 20 30 2c 20 20 20 20   */.   0, 0,    
78030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78040 20 20 2f 2a 20 6d 6e 48 65 61 70 2c 20 6d 78 48    /* mnHeap, mxH
78050 65 61 70 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a  eap */.   (void*
78060 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  )0,             
78070 20 20 20 20 20 2f 2a 20 70 53 63 72 61 74 63 68       /* pScratch
78080 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
78090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
780a0 20 20 2f 2a 20 73 7a 53 63 72 61 74 63 68 20 2a    /* szScratch *
780b0 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  /.   0,         
780c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
780d0 2f 2a 20 6e 53 63 72 61 74 63 68 20 2a 2f 0a 20  /* nScratch */. 
780e0 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 20    (void*)0,     
780f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
78100 70 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20  pPage */.   0,  
78110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78120 20 20 20 20 20 20 20 2f 2a 20 73 7a 50 61 67 65         /* szPage
78130 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
78140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78150 20 20 2f 2a 20 6e 50 61 67 65 20 2a 2f 0a 20 20    /* nPage */.  
78160 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
78170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d              /* m
78180 78 50 61 72 73 65 72 53 74 61 63 6b 20 2a 2f 0a  xParserStack */.
78190 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
781a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
781b0 20 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62   sharedCacheEnab
781c0 6c 65 64 20 2a 2f 0a 20 20 20 2f 2a 20 41 6c 6c  led */.   /* All
781d0 20 74 68 65 20 72 65 73 74 20 73 68 6f 75 6c 64   the rest should
781e0 20 61 6c 77 61 79 73 20 62 65 20 69 6e 69 74 69   always be initi
781f0 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 20 2a  alized to zero *
78200 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  /.   0,         
78210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78220 2f 2a 20 69 73 49 6e 69 74 20 2a 2f 0a 20 20 20  /* isInit */.   
78230 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
78240 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
78250 50 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 20 30  Progress */.   0
78260 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
78270 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 4d            /* isM
78280 75 74 65 78 49 6e 69 74 20 2a 2f 0a 20 20 20 30  utexInit */.   0
78290 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
782a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 4d            /* isM
782b0 61 6c 6c 6f 63 49 6e 69 74 20 2a 2f 0a 20 20 20  allocInit */.   
782c0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
782d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73             /* is
782e0 50 43 61 63 68 65 49 6e 69 74 20 2a 2f 0a 20 20  PCacheInit */.  
782f0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
78300 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
78310 49 6e 69 74 4d 75 74 65 78 20 2a 2f 0a 20 20 20  InitMutex */.   
78320 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
78330 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 52             /* nR
78340 65 66 49 6e 69 74 4d 75 74 65 78 20 2a 2f 0a 7d  efInitMutex */.}
78350 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 74  ;.../*.** Hash t
78360 61 62 6c 65 20 66 6f 72 20 67 6c 6f 62 61 6c 20  able for global 
78370 66 75 6e 63 74 69 6f 6e 73 20 2d 20 66 75 6e 63  functions - func
78380 74 69 6f 6e 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20  tions common to 
78390 61 6c 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  all.** database 
783a0 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 41 66  connections.  Af
783b0 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ter initializati
783c0 6f 6e 2c 20 74 68 69 73 20 74 61 62 6c 65 20 69  on, this table i
783d0 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a  s.** read-only..
783e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
783f0 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e  E SQLITE_WSD Fun
78400 63 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33  cDefHash sqlite3
78410 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b  GlobalFunctions;
78420 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ../*.** The valu
78430 65 20 6f 66 20 74 68 65 20 22 70 65 6e 64 69 6e  e of the "pendin
78440 67 22 20 62 79 74 65 20 6d 75 73 74 20 62 65 20  g" byte must be 
78450 30 78 34 30 30 30 30 30 30 30 20 28 31 20 62 79  0x40000000 (1 by
78460 74 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31  te past the.** 1
78470 2d 67 69 62 61 62 79 74 65 20 62 6f 75 6e 64 61  -gibabyte bounda
78480 72 79 29 20 69 6e 20 61 20 63 6f 6d 70 61 74 69  ry) in a compati
78490 62 6c 65 20 64 61 74 61 62 61 73 65 2e 20 20 53  ble database.  S
784a0 51 4c 69 74 65 20 6e 65 76 65 72 20 75 73 65 73  QLite never uses
784b0 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
784c0 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61   page that conta
784d0 69 6e 73 20 74 68 65 20 70 65 6e 64 69 6e 67 20  ins the pending 
784e0 62 79 74 65 2e 20 20 49 74 20 6e 65 76 65 72 20  byte.  It never 
784f0 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 72  attempts.** to r
78500 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 68 61  ead or write tha
78510 74 20 70 61 67 65 2e 20 20 54 68 65 20 70 65 6e  t page.  The pen
78520 64 69 6e 67 20 62 79 74 65 20 70 61 67 65 20 69  ding byte page i
78530 73 20 73 65 74 20 61 73 73 69 67 6e 0a 2a 2a 20  s set assign.** 
78540 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 56  for use by the V
78550 46 53 20 6c 61 79 65 72 73 20 61 73 20 73 70 61  FS layers as spa
78560 63 65 20 66 6f 72 20 6d 61 6e 61 67 69 6e 67 20  ce for managing 
78570 66 69 6c 65 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a  file locks..**.*
78580 2a 20 44 75 72 69 6e 67 20 74 65 73 74 69 6e 67  * During testing
78590 2c 20 69 74 20 69 73 20 6f 66 74 65 6e 20 64 65  , it is often de
785a0 73 69 72 61 62 6c 65 20 74 6f 20 6d 6f 76 65 20  sirable to move 
785b0 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65  the pending byte
785c0 20 74 6f 0a 2a 2a 20 61 20 64 69 66 66 65 72 65   to.** a differe
785d0 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74  nt position in t
785e0 68 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 61  he file.  This a
785f0 6c 6c 6f 77 73 20 63 6f 64 65 20 74 68 61 74 20  llows code that 
78600 68 61 73 20 74 6f 0a 2a 2a 20 64 65 61 6c 20 77  has to.** deal w
78610 69 74 68 20 74 68 65 20 70 65 6e 64 69 6e 67 20  ith the pending 
78620 62 79 74 65 20 74 6f 20 72 75 6e 20 6f 6e 20 66  byte to run on f
78630 69 6c 65 73 20 74 68 61 74 20 61 72 65 20 6d 75  iles that are mu
78640 63 68 20 73 6d 61 6c 6c 65 72 0a 2a 2a 20 74 68  ch smaller.** th
78650 61 6e 20 31 20 47 69 42 2e 20 20 54 68 65 20 73  an 1 GiB.  The s
78660 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
78670 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20  rol() interface 
78680 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 0a 2a  can be used to.*
78690 2a 20 6d 6f 76 65 20 74 68 65 20 70 65 6e 64 69  * move the pendi
786a0 6e 67 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 49  ng byte..**.** I
786b0 4d 50 4f 52 54 41 4e 54 3a 20 20 43 68 61 6e 67  MPORTANT:  Chang
786c0 69 6e 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20  ing the pending 
786d0 62 79 74 65 20 74 6f 20 61 6e 79 20 76 61 6c 75  byte to any valu
786e0 65 20 6f 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20  e other than.** 
786f0 30 78 34 30 30 30 30 30 30 30 20 72 65 73 75 6c  0x40000000 resul
78700 74 73 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61  ts in an incompa
78710 74 69 62 6c 65 20 64 61 74 61 62 61 73 65 20 66  tible database f
78720 69 6c 65 20 66 6f 72 6d 61 74 21 0a 2a 2a 20 43  ile format!.** C
78730 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65 6e 64  hanging the pend
78740 69 6e 67 20 62 79 74 65 20 64 75 72 69 6e 67 20  ing byte during 
78750 6f 70 65 72 61 74 69 6e 67 20 72 65 73 75 6c 74  operating result
78760 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a  s in undefined.*
78770 2a 20 61 6e 64 20 64 69 6c 65 74 65 72 69 6f 75  * and dileteriou
78780 73 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53  s behavior..*/.S
78790 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
787a0 74 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67  t sqlite3Pending
787b0 42 79 74 65 20 3d 20 30 78 34 30 30 30 30 30 30  Byte = 0x4000000
787c0 30 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  0;../***********
787d0 2a 2a 2a 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61  *** End of globa
787e0 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  l.c ************
787f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78810 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
78820 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73  *** Begin file s
78830 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  tatus.c ********
78840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78860 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a  **/./*.** 2008 J
78870 75 6e 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65  une 18.**.** The
78880 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
78890 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
788a0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
788b0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
788c0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
788d0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
788e0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
788f0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
78900 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
78910 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
78920 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
78930 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
78940 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
78950 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
78960 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
78970 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
78980 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
78990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
789a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
789b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
789c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
789d0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
789e0 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
789f0 74 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  ts the sqlite3_s
78a00 74 61 74 75 73 28 29 20 69 6e 74 65 72 66 61 63  tatus() interfac
78a10 65 20 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a  e and related.**
78a20 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a   functionality..
78a30 2a 2a 0a 2a 2a 20 24 49 64 3a 20 73 74 61 74 75  **.** $Id: statu
78a40 73 2e 63 2c 76 20 31 2e 39 20 32 30 30 38 2f 30  s.c,v 1.9 2008/0
78a50 39 2f 30 32 20 30 30 3a 35 32 3a 35 32 20 64 72  9/02 00:52:52 dr
78a60 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
78a70 2a 20 56 61 72 69 61 62 6c 65 73 20 69 6e 20 77  * Variables in w
78a80 68 69 63 68 20 74 6f 20 72 65 63 6f 72 64 20 73  hich to record s
78a90 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tatus informatio
78aa0 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
78ab0 72 75 63 74 20 73 71 6c 69 74 65 33 53 74 61 74  ruct sqlite3Stat
78ac0 54 79 70 65 20 73 71 6c 69 74 65 33 53 74 61 74  Type sqlite3Stat
78ad0 54 79 70 65 3b 0a 73 74 61 74 69 63 20 53 51 4c  Type;.static SQL
78ae0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 73  ITE_WSD struct s
78af0 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 20 7b  qlite3StatType {
78b00 0a 20 20 69 6e 74 20 6e 6f 77 56 61 6c 75 65 5b  .  int nowValue[
78b10 39 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  9];         /* C
78b20 75 72 72 65 6e 74 20 76 61 6c 75 65 20 2a 2f 0a  urrent value */.
78b30 20 20 69 6e 74 20 6d 78 56 61 6c 75 65 5b 39 5d    int mxValue[9]
78b40 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  ;          /* Ma
78b50 78 69 6d 75 6d 20 76 61 6c 75 65 20 2a 2f 0a 7d  ximum value */.}
78b60 20 73 71 6c 69 74 65 33 53 74 61 74 20 3d 20 7b   sqlite3Stat = {
78b70 20 7b 30 2c 7d 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a   {0,}, {0,} };..
78b80 0a 2f 2a 20 54 68 65 20 22 77 73 64 53 74 61 74  ./* The "wsdStat
78b90 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73  " macro will res
78ba0 6f 6c 76 65 20 74 6f 20 74 68 65 20 73 74 61 74  olve to the stat
78bb0 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  us information.*
78bc0 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20  * state vector. 
78bd0 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61   If writable sta
78be0 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75  tic data is unsu
78bf0 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74  pported on the t
78c00 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76  arget,.** we hav
78c10 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  e to locate the 
78c20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 20  state vector at 
78c30 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68  run-time.  In th
78c40 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a  e more common.**
78c50 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69 74   case where writ
78c60 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61  able static data
78c70 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77   is supported, w
78c80 73 64 53 74 61 74 20 63 61 6e 20 72 65 66 65 72  sdStat can refer
78c90 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20   directly.** to 
78ca0 74 68 65 20 22 73 71 6c 69 74 65 33 53 74 61 74  the "sqlite3Stat
78cb0 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64  " state vector d
78cc0 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a  eclared above..*
78cd0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
78ce0 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e  OMIT_WSD.# defin
78cf0 65 20 77 73 64 53 74 61 74 49 6e 69 74 20 20 73  e wsdStatInit  s
78d00 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 20 2a  qlite3StatType *
78d10 78 20 3d 20 26 47 4c 4f 42 41 4c 28 73 71 6c 69  x = &GLOBAL(sqli
78d20 74 65 33 53 74 61 74 54 79 70 65 2c 73 71 6c 69  te3StatType,sqli
78d30 74 65 33 53 74 61 74 29 0a 23 20 64 65 66 69 6e  te3Stat).# defin
78d40 65 20 77 73 64 53 74 61 74 20 78 5b 30 5d 0a 23  e wsdStat x[0].#
78d50 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73  else.# define ws
78d60 64 53 74 61 74 49 6e 69 74 0a 23 20 64 65 66 69  dStatInit.# defi
78d70 6e 65 20 77 73 64 53 74 61 74 20 73 71 6c 69 74  ne wsdStat sqlit
78d80 65 33 53 74 61 74 0a 23 65 6e 64 69 66 0a 0a 2f  e3Stat.#endif../
78d90 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
78da0 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66  current value of
78db0 20 61 20 73 74 61 74 75 73 20 70 61 72 61 6d 65   a status parame
78dc0 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ter..*/.SQLITE_P
78dd0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
78de0 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e  e3StatusValue(in
78df0 74 20 6f 70 29 7b 0a 20 20 77 73 64 53 74 61 74  t op){.  wsdStat
78e00 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20  Init;.  assert( 
78e10 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61  op>=0 && op<Arra
78e20 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f  ySize(wsdStat.no
78e30 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 72 65 74  wValue) );.  ret
78e40 75 72 6e 20 77 73 64 53 74 61 74 2e 6e 6f 77 56  urn wsdStat.nowV
78e50 61 6c 75 65 5b 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a  alue[op];.}../*.
78e60 2a 2a 20 41 64 64 20 4e 20 74 6f 20 74 68 65 20  ** Add N to the 
78e70 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75  value of a statu
78e80 73 20 72 65 63 6f 72 64 2e 20 20 49 74 20 69 73  s record.  It is
78e90 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
78ea0 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 68 6f 6c 64  e.** caller hold
78eb0 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f  s appropriate lo
78ec0 63 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  cks..*/.SQLITE_P
78ed0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
78ee0 74 65 33 53 74 61 74 75 73 41 64 64 28 69 6e 74  te3StatusAdd(int
78ef0 20 6f 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 77   op, int N){.  w
78f00 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73  sdStatInit;.  as
78f10 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f  sert( op>=0 && o
78f20 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53  p<ArraySize(wsdS
78f30 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b  tat.nowValue) );
78f40 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61  .  wsdStat.nowVa
78f50 6c 75 65 5b 6f 70 5d 20 2b 3d 20 4e 3b 0a 20 20  lue[op] += N;.  
78f60 69 66 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56  if( wsdStat.nowV
78f70 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74  alue[op]>wsdStat
78f80 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a  .mxValue[op] ){.
78f90 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61      wsdStat.mxVa
78fa0 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61  lue[op] = wsdSta
78fb0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a  t.nowValue[op];.
78fc0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74    }.}../*.** Set
78fd0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
78fe0 73 74 61 74 75 73 20 74 6f 20 58 2e 0a 2a 2f 0a  status to X..*/.
78ff0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
79000 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75  oid sqlite3Statu
79010 73 53 65 74 28 69 6e 74 20 6f 70 2c 20 69 6e 74  sSet(int op, int
79020 20 58 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e   X){.  wsdStatIn
79030 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70  it;.  assert( op
79040 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53  >=0 && op<ArrayS
79050 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56  ize(wsdStat.nowV
79060 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74  alue) );.  wsdSt
79070 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20  at.nowValue[op] 
79080 3d 20 58 3b 0a 20 20 69 66 28 20 77 73 64 53 74  = X;.  if( wsdSt
79090 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e  at.nowValue[op]>
790a0 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b  wsdStat.mxValue[
790b0 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74  op] ){.    wsdSt
790c0 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d  at.mxValue[op] =
790d0 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
790e0 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  e[op];.  }.}../*
790f0 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 75 73  .** Query status
79100 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
79110 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  .** This impleme
79120 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20  ntation assumes 
79130 74 68 61 74 20 72 65 61 64 69 6e 67 20 6f 72 20  that reading or 
79140 77 72 69 74 69 6e 67 20 61 6e 20 61 6c 69 67 6e  writing an align
79150 65 64 0a 2a 2a 20 33 32 2d 62 69 74 20 69 6e 74  ed.** 32-bit int
79160 65 67 65 72 20 69 73 20 61 6e 20 61 74 6f 6d 69  eger is an atomi
79170 63 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66  c operation.  If
79180 20 74 68 61 74 20 61 73 73 75 6d 70 74 69 6f 6e   that assumption
79190 20 69 73 20 6e 6f 74 20 74 72 75 65 2c 0a 2a 2a   is not true,.**
791a0 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
791b0 6e 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64  ne is not thread
791c0 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  safe..*/.SQLITE_
791d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
791e0 73 74 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69  status(int op, i
791f0 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e  nt *pCurrent, in
79200 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69  t *pHighwater, i
79210 6e 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20  nt resetFlag){. 
79220 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20   wsdStatInit;.  
79230 69 66 28 20 6f 70 3c 30 20 7c 7c 20 6f 70 3e 3d  if( op<0 || op>=
79240 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61  ArraySize(wsdSta
79250 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 7b 0a 20  t.nowValue) ){. 
79260 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
79270 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 2a  _MISUSE;.  }.  *
79280 70 43 75 72 72 65 6e 74 20 3d 20 77 73 64 53 74  pCurrent = wsdSt
79290 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b  at.nowValue[op];
792a0 0a 20 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d  .  *pHighwater =
792b0 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65   wsdStat.mxValue
792c0 5b 6f 70 5d 3b 0a 20 20 69 66 28 20 72 65 73 65  [op];.  if( rese
792d0 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 77 73 64  tFlag ){.    wsd
792e0 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d  Stat.mxValue[op]
792f0 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61   = wsdStat.nowVa
79300 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 20 20 72  lue[op];.  }.  r
79310 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
79320 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20  .}../*.** Query 
79330 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69  status informati
79340 6f 6e 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  on for a single 
79350 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
79360 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ion.*/.SQLITE_AP
79370 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62  I int sqlite3_db
79380 5f 73 74 61 74 75 73 28 0a 20 20 73 71 6c 69 74  _status(.  sqlit
79390 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
793a0 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
793b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 6f 73   connection whos
793c0 65 20 73 74 61 74 75 73 20 69 73 20 64 65 73 69  e status is desi
793d0 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c  red */.  int op,
793e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
793f0 2a 20 53 74 61 74 75 73 20 76 65 72 62 20 2a 2f  * Status verb */
79400 0a 20 20 69 6e 74 20 2a 70 43 75 72 72 65 6e 74  .  int *pCurrent
79410 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74  ,        /* Writ
79420 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
79430 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  here */.  int *p
79440 48 69 67 68 77 61 74 65 72 2c 20 20 20 20 20 20  Highwater,      
79450 2f 2a 20 57 72 69 74 65 20 68 69 67 68 2d 77 61  /* Write high-wa
79460 74 65 72 20 6d 61 72 6b 20 68 65 72 65 20 2a 2f  ter mark here */
79470 0a 20 20 69 6e 74 20 72 65 73 65 74 46 6c 61 67  .  int resetFlag
79480 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 65           /* Rese
79490 74 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72  t high-water mar
794a0 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  k if true */.){.
794b0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
794c0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
794d0 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49  DBSTATUS_LOOKASI
794e0 44 45 5f 55 53 45 44 3a 20 7b 0a 20 20 20 20 20  DE_USED: {.     
794f0 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 64 62 2d   *pCurrent = db-
79500 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b  >lookaside.nOut;
79510 0a 20 20 20 20 20 20 2a 70 48 69 67 68 77 61 74  .      *pHighwat
79520 65 72 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  er = db->lookasi
79530 64 65 2e 6d 78 4f 75 74 3b 0a 20 20 20 20 20 20  de.mxOut;.      
79540 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b  if( resetFlag ){
79550 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f  .        db->loo
79560 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20 64  kaside.mxOut = d
79570 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75  b->lookaside.nOu
79580 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
79590 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
795a0 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
795b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
795c0 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20  _ERROR;.    }.  
795d0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
795e0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  E_OK;.}../******
795f0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
79600 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a  status.c *******
79610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79630 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
79640 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
79650 69 6c 65 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a  ile date.c *****
79660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79680 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
79690 30 30 33 20 4f 63 74 6f 62 65 72 20 33 31 0a 2a  003 October 31.*
796a0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
796b0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
796c0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
796d0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
796e0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
796f0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
79700 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
79710 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
79720 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
79730 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
79740 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
79750 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
79760 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
79770 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
79780 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
79790 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
797a0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
797b0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
797c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
797d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
797e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
797f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
79800 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
79810 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
79820 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
79830 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  nt date and time
79840 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f  .** functions fo
79850 72 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a  r SQLite.  .**.*
79860 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c 79 20  * There is only 
79870 6f 6e 65 20 65 78 70 6f 72 74 65 64 20 73 79 6d  one exported sym
79880 62 6f 6c 20 69 6e 20 74 68 69 73 20 66 69 6c 65  bol in this file
79890 20 2d 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a   - the function.
798a0 2a 2a 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  ** sqlite3Regist
798b0 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69  erDateTimeFuncti
798c0 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 74 20 74  ons() found at t
798d0 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65  he bottom of the
798e0 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74   file..** All ot
798f0 68 65 72 20 63 6f 64 65 20 68 61 73 20 66 69 6c  her code has fil
79900 65 20 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 24  e scope..**.** $
79910 49 64 3a 20 64 61 74 65 2e 63 2c 76 20 31 2e 31  Id: date.c,v 1.1
79920 30 37 20 32 30 30 39 2f 30 35 2f 30 33 20 32 30  07 2009/05/03 20
79930 3a 32 33 3a 35 33 20 64 72 68 20 45 78 70 20 24  :23:53 drh Exp $
79940 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72  .**.** SQLite pr
79950 6f 63 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65  ocesses all time
79960 73 20 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a  s and dates as J
79970 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72  ulian Day number
79980 73 2e 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73  s.  The.** dates
79990 20 61 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73   and times are s
799a0 74 6f 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d  tored as the num
799b0 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63  ber of days sinc
799c0 65 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65  e noon.** in Gre
799d0 65 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62  enwich on Novemb
799e0 65 72 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e  er 24, 4714 B.C.
799f0 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   according to th
79a00 65 20 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63  e Gregorian.** c
79a10 61 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20  alendar system. 
79a20 0a 2a 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30  .**.** 1970-01-0
79a30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44  1 00:00:00 is JD
79a40 20 32 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30   2440587.5.** 20
79a50 30 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30  00-01-01 00:00:0
79a60 30 20 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e  0 is JD 2451544.
79a70 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70  5.**.** This imp
79a80 6c 65 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72  lemention requir
79a90 65 73 20 79 65 61 72 73 20 74 6f 20 62 65 20 65  es years to be e
79aa0 78 70 72 65 73 73 65 64 20 61 73 20 61 20 34 2d  xpressed as a 4-
79ab0 64 69 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20  digit number.** 
79ac0 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74  which means that
79ad0 20 6f 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77   only dates betw
79ae0 65 65 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61  een 0000-01-01 a
79af0 6e 64 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61  nd 9999-12-31 ca
79b00 6e 0a 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e  n.** be represen
79b10 74 65 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68  ted, even though
79b20 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62   julian day numb
79b30 65 72 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68  ers allow a much
79b40 20 77 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20   wider.** range 
79b50 6f 66 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  of dates..**.** 
79b60 54 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61  The Gregorian ca
79b70 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73  lendar system is
79b80 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61   used for all da
79b90 74 65 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a  tes and times,.*
79ba0 2a 20 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61  * even those tha
79bb0 74 20 70 72 65 64 61 74 65 20 74 68 65 20 47 72  t predate the Gr
79bc0 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72  egorian calendar
79bd0 2e 20 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73  .  Historians us
79be0 75 61 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65  ually.** use the
79bf0 20 4a 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72   Julian calendar
79c00 20 66 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72   for dates prior
79c10 20 74 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61   to 1582-10-15 a
79c20 6e 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64  nd for some.** d
79c30 61 74 65 73 20 61 66 74 65 72 77 61 72 64 73 2c  ates afterwards,
79c40 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f   depending on lo
79c50 63 61 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66  cale.  Beware of
79c60 20 74 68 69 73 20 64 69 66 66 65 72 65 6e 63 65   this difference
79c70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76  ..**.** The conv
79c80 65 72 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d  ersion algorithm
79c90 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65  s are implemente
79ca0 64 20 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72  d based on descr
79cb0 69 70 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68  iptions.** in th
79cc0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74  e following text
79cd0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61  :.**.**      Jea
79ce0 6e 20 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20  n Meeus.**      
79cf0 41 73 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67  Astronomical Alg
79d00 6f 72 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69  orithms, 2nd Edi
79d10 74 69 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20  tion, 1998.**   
79d20 20 20 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36     ISBM 0-943396
79d30 2d 36 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69  -61-1.**      Wi
79d40 6c 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63  llmann-Bell, Inc
79d50 0a 2a 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e  .**      Richmon
79d60 64 2c 20 56 69 72 67 69 6e 69 61 20 28 55 53 41  d, Virginia (USA
79d70 29 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74  ).*/.#include <t
79d80 69 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20  ime.h>..#ifndef 
79d90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45  SQLITE_OMIT_DATE
79da0 54 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a  TIME_FUNCS../*.*
79db0 2a 20 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64  * On recent Wind
79dc0 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74  ows platforms, t
79dd0 68 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29  he localtime_s()
79de0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61   function is ava
79df0 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72  ilable.** as par
79e00 74 20 6f 66 20 74 68 65 20 22 53 65 63 75 72 65  t of the "Secure
79e10 20 43 52 54 22 2e 20 49 74 20 69 73 20 65 73 73   CRT". It is ess
79e20 65 6e 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c  entially equival
79e30 65 6e 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c  ent to .** local
79e40 74 69 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62  time_r() availab
79e50 6c 65 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f  le under most PO
79e60 53 49 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65  SIX platforms, e
79e70 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 0a  xcept that the .
79e80 2a 2a 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20  ** order of the 
79e90 70 61 72 61 6d 65 74 65 72 73 20 69 73 20 72 65  parameters is re
79ea0 76 65 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65  versed..**.** Se
79eb0 65 20 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69  e http://msdn.mi
79ec0 63 72 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75  crosoft.com/en-u
79ed0 73 2f 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33  s/library/a442x3
79ee0 79 65 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a  ye(VS.80).aspx..
79ef0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65  **.** If the use
79f00 72 20 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61  r has not indica
79f10 74 65 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c  ted to use local
79f20 74 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61  time_r() or loca
79f30 6c 74 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72  ltime_s().** alr
79f40 65 61 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20  eady, check for 
79f50 61 6e 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e  an MSVC build en
79f60 76 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70  vironment that p
79f70 72 6f 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61  rovides .** loca
79f80 6c 74 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69  ltime_s()..*/.#i
79f90 66 20 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f  f !defined(HAVE_
79fa0 4c 4f 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20  LOCALTIME_R) && 
79fb0 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f  !defined(HAVE_LO
79fc0 43 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a  CALTIME_S) && \.
79fd0 20 20 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53       defined(_MS
79fe0 43 5f 56 45 52 29 20 26 26 20 64 65 66 69 6e 65  C_VER) && define
79ff0 64 28 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f  d(_CRT_INSECURE_
7a000 44 45 50 52 45 43 41 54 45 29 0a 23 64 65 66 69  DEPRECATE).#defi
7a010 6e 65 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ne HAVE_LOCALTIM
7a020 45 5f 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  E_S 1.#endif../*
7a030 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20  .** A structure 
7a040 66 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69  for holding a si
7a050 6e 67 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69  ngle date and ti
7a060 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  me..*/.typedef s
7a070 74 72 75 63 74 20 44 61 74 65 54 69 6d 65 20 44  truct DateTime D
7a080 61 74 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20  ateTime;.struct 
7a090 44 61 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c  DateTime {.  sql
7a0a0 69 74 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20  ite3_int64 iJD; 
7a0b0 2f 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61  /* The julian da
7a0c0 79 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38  y number times 8
7a0d0 36 34 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74  6400000 */.  int
7a0e0 20 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20   Y, M, D;       
7a0f0 2f 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20  /* Year, month, 
7a100 61 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74  and day */.  int
7a110 20 68 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20   h, m;          
7a120 2f 2a 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75  /* Hour and minu
7a130 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b  tes */.  int tz;
7a140 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
7a150 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69  imezone offset i
7a160 6e 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64  n minutes */.  d
7a170 6f 75 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20  ouble s;        
7a180 20 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a    /* Seconds */.
7a190 20 20 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b    char validYMD;
7a1a0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29       /* True (1)
7a1b0 20 69 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61   if Y,M,D are va
7a1c0 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61  lid */.  char va
7a1d0 6c 69 64 48 4d 53 3b 20 20 20 20 20 2f 2a 20 54  lidHMS;     /* T
7a1e0 72 75 65 20 28 31 29 20 69 66 20 68 2c 6d 2c 73  rue (1) if h,m,s
7a1f0 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20   are valid */.  
7a200 63 68 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 20  char validJD;   
7a210 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69     /* True (1) i
7a220 66 20 69 4a 44 20 69 73 20 76 61 6c 69 64 20 2a  f iJD is valid *
7a230 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a  /.  char validTZ
7a240 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28  ;      /* True (
7a250 31 29 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69  1) if tz is vali
7a260 64 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  d */.};.../*.** 
7a270 43 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e  Convert zDate in
7a280 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69  to one or more i
7a290 6e 74 65 67 65 72 73 2e 20 20 41 64 64 69 74 69  ntegers.  Additi
7a2a0 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a  onal arguments.*
7a2b0 2a 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73  * come in groups
7a2c0 20 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73   of 5 as follows
7a2d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20  :.**.**       N 
7a2e0 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20        number of 
7a2f0 64 69 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e  digits in the in
7a300 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d  teger.**       m
7a310 69 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61  in     minimum a
7a320 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20  llowed value of 
7a330 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  the integer.**  
7a340 20 20 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78       max     max
7a350 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c  imum allowed val
7a360 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ue of the intege
7a370 72 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43  r.**       nextC
7a380 20 20 20 66 69 72 73 74 20 63 68 61 72 61 63 74     first charact
7a390 65 72 20 61 66 74 65 72 20 74 68 65 20 69 6e 74  er after the int
7a3a0 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56  eger.**       pV
7a3b0 61 6c 20 20 20 20 77 68 65 72 65 20 74 6f 20 77  al    where to w
7a3c0 72 69 74 65 20 74 68 65 20 69 6e 74 65 67 65 72  rite the integer
7a3d0 73 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43  s value..**.** C
7a3e0 6f 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69  onversions conti
7a3f0 6e 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69  nue until one wi
7a400 74 68 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65  th nextC==0 is e
7a410 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54  ncountered..** T
7a420 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
7a430 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
7a440 66 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e  f successful con
7a450 76 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61  versions..*/.sta
7a460 74 69 63 20 69 6e 74 20 67 65 74 44 69 67 69 74  tic int getDigit
7a470 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  s(const char *zD
7a480 61 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  ate, ...){.  va_
7a490 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76  list ap;.  int v
7a4a0 61 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69  al;.  int N;.  i
7a4b0 6e 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61  nt min;.  int ma
7a4c0 78 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a  x;.  int nextC;.
7a4d0 20 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69    int *pVal;.  i
7a4e0 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61  nt cnt = 0;.  va
7a4f0 5f 73 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65  _start(ap, zDate
7a500 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d  );.  do{.    N =
7a510 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
7a520 3b 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61  ;.    min = va_a
7a530 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
7a540 20 6d 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70   max = va_arg(ap
7a550 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74  , int);.    next
7a560 43 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  C = va_arg(ap, i
7a570 6e 74 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20  nt);.    pVal = 
7a580 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29  va_arg(ap, int*)
7a590 3b 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20  ;.    val = 0;. 
7a5a0 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b     while( N-- ){
7a5b0 0a 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69  .      if( !sqli
7a5c0 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74  te3Isdigit(*zDat
7a5d0 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  e) ){.        go
7a5e0 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73  to end_getDigits
7a5f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
7a600 76 61 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a  val = val*10 + *
7a610 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20  zDate - '0';.   
7a620 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
7a630 7d 0a 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69  }.    if( val<mi
7a640 6e 20 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20  n || val>max || 
7a650 28 6e 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78  (nextC!=0 && nex
7a660 74 43 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20  tC!=*zDate) ){. 
7a670 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65       goto end_ge
7a680 74 44 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20  tDigits;.    }. 
7a690 20 20 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a     *pVal = val;.
7a6a0 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20      zDate++;.   
7a6b0 20 63 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65   cnt++;.  }while
7a6c0 28 20 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67  ( nextC );.end_g
7a6d0 65 74 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65  etDigits:.  va_e
7a6e0 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e  nd(ap);.  return
7a6f0 20 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   cnt;.}../*.** R
7a700 65 61 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b  ead text from z[
7a710 5d 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e  ] and convert in
7a720 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f  to a floating po
7a730 69 6e 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74  int number.  Ret
7a740 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  urn.** the numbe
7a750 72 20 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76  r of digits conv
7a760 65 72 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  erted..*/.#defin
7a770 65 20 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74  e getValue sqlit
7a780 65 33 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61  e3AtoF../*.** Pa
7a790 72 73 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65  rse a timezone e
7a7a0 78 74 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20  xtension on the 
7a7b0 65 6e 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69  end of a date-ti
7a7c0 6d 65 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e  me..** The exten
7a7d0 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66  sion is of the f
7a7e0 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  orm:.**.**      
7a7f0 20 20 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a    (+/-)HH:MM.**.
7a800 2a 2a 20 4f 72 20 74 68 65 20 22 7a 75 6c 75 22  ** Or the "zulu"
7a810 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a   notation:.**.**
7a820 20 20 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20          Z.**.** 
7a830 49 66 20 74 68 65 20 70 61 72 73 65 20 69 73 20  If the parse is 
7a840 73 75 63 63 65 73 73 66 75 6c 2c 20 77 72 69 74  successful, writ
7a850 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
7a860 6d 69 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68  minutes.** of ch
7a870 61 6e 67 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e  ange in p->tz an
7a880 64 20 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20  d return 0.  If 
7a890 61 20 70 61 72 73 65 72 20 65 72 72 6f 72 20 6f  a parser error o
7a8a0 63 63 75 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e  ccurs,.** return
7a8b0 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a   non-zero..**.**
7a8c0 20 41 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69   A missing speci
7a8d0 66 69 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73  fier is not cons
7a8e0 69 64 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e  idered an error.
7a8f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
7a900 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e  arseTimezone(con
7a910 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20  st char *zDate, 
7a920 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
7a930 69 6e 74 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69  int sgn = 0;.  i
7a940 6e 74 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69  nt nHr, nMn;.  i
7a950 6e 74 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 73  nt c;.  while( s
7a960 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a  qlite3Isspace(*z
7a970 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b  Date) ){ zDate++
7a980 3b 20 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b  ; }.  p->tz = 0;
7a990 0a 20 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20  .  c = *zDate;. 
7a9a0 20 69 66 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20   if( c=='-' ){. 
7a9b0 20 20 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d     sgn = -1;.  }
7a9c0 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 2b 27 20  else if( c=='+' 
7a9d0 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b  ){.    sgn = +1;
7a9e0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d  .  }else if( c==
7a9f0 27 5a 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b  'Z' || c=='z' ){
7aa00 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20  .    zDate++;.  
7aa10 20 20 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65    goto zulu_time
7aa20 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
7aa30 65 74 75 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a  eturn c!=0;.  }.
7aa40 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28    zDate++;.  if(
7aa50 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65   getDigits(zDate
7aa60 2c 20 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c  , 2, 0, 14, ':',
7aa70 20 26 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c   &nHr, 2, 0, 59,
7aa80 20 30 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a   0, &nMn)!=2 ){.
7aa90 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
7aaa0 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a  }.  zDate += 5;.
7aab0 20 20 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e    p->tz = sgn*(n
7aac0 4d 6e 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75  Mn + nHr*60);.zu
7aad0 6c 75 5f 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65  lu_time:.  while
7aae0 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ( sqlite3Isspace
7aaf0 28 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74  (*zDate) ){ zDat
7ab00 65 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20  e++; }.  return 
7ab10 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a  *zDate!=0;.}../*
7ab20 0a 2a 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20  .** Parse times 
7ab30 6f 66 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d  of the form HH:M
7ab40 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72  M or HH:MM:SS or
7ab50 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a   HH:MM:SS.FFFF..
7ab60 2a 2a 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61  ** The HH, MM, a
7ab70 6e 64 20 53 53 20 6d 75 73 74 20 65 61 63 68 20  nd SS must each 
7ab80 62 65 20 65 78 61 63 74 6c 79 20 32 20 64 69 67  be exactly 2 dig
7ab90 69 74 73 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61  its.  The.** fra
7aba0 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20  ctional seconds 
7abb0 46 46 46 46 20 63 61 6e 20 62 65 20 6f 6e 65 20  FFFF can be one 
7abc0 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a  or more digits..
7abd0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69  **.** Return 1 i
7abe0 66 20 74 68 65 72 65 20 69 73 20 61 20 70 61 72  f there is a par
7abf0 73 69 6e 67 20 65 72 72 6f 72 20 61 6e 64 20 30  sing error and 0
7ac00 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a   on success..*/.
7ac10 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65  static int parse
7ac20 48 68 4d 6d 53 73 28 63 6f 6e 73 74 20 63 68 61  HhMmSs(const cha
7ac30 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69  r *zDate, DateTi
7ac40 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c  me *p){.  int h,
7ac50 20 6d 2c 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20   m, s;.  double 
7ac60 6d 73 20 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20  ms = 0.0;.  if( 
7ac70 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c  getDigits(zDate,
7ac80 20 32 2c 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20   2, 0, 24, ':', 
7ac90 26 68 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c  &h, 2, 0, 59, 0,
7aca0 20 26 6d 29 21 3d 32 20 29 7b 0a 20 20 20 20 72   &m)!=2 ){.    r
7acb0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a  eturn 1;.  }.  z
7acc0 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28  Date += 5;.  if(
7acd0 20 2a 7a 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a   *zDate==':' ){.
7ace0 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20      zDate++;.   
7acf0 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a   if( getDigits(z
7ad00 44 61 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20  Date, 2, 0, 59, 
7ad10 30 2c 20 26 73 29 21 3d 31 20 29 7b 0a 20 20 20  0, &s)!=1 ){.   
7ad20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
7ad30 20 7d 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d 20   }.    zDate += 
7ad40 32 3b 0a 20 20 20 20 69 66 28 20 2a 7a 44 61 74  2;.    if( *zDat
7ad50 65 3d 3d 27 2e 27 20 26 26 20 73 71 6c 69 74 65  e=='.' && sqlite
7ad60 33 49 73 64 69 67 69 74 28 7a 44 61 74 65 5b 31  3Isdigit(zDate[1
7ad70 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62  ]) ){.      doub
7ad80 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b  le rScale = 1.0;
7ad90 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a  .      zDate++;.
7ada0 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c        while( sql
7adb0 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61  ite3Isdigit(*zDa
7adc0 74 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  te) ){.        m
7add0 73 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a  s = ms*10.0 + *z
7ade0 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20  Date - '0';.    
7adf0 20 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30      rScale *= 10
7ae00 2e 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74  .0;.        zDat
7ae10 65 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  e++;.      }.   
7ae20 20 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b     ms /= rScale;
7ae30 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
7ae40 20 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20      s = 0;.  }. 
7ae50 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b   p->validJD = 0;
7ae60 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d  .  p->validHMS =
7ae70 20 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a   1;.  p->h = h;.
7ae80 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d    p->m = m;.  p-
7ae90 3e 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69  >s = s + ms;.  i
7aea0 66 28 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65  f( parseTimezone
7aeb0 28 7a 44 61 74 65 2c 20 70 29 20 29 20 72 65 74  (zDate, p) ) ret
7aec0 75 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69  urn 1;.  p->vali
7aed0 64 54 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29  dTZ = (p->tz!=0)
7aee0 3f 31 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30  ?1:0;.  return 0
7aef0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
7af00 72 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d  rt from YYYY-MM-
7af10 44 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a  DD HH:MM:SS to j
7af20 75 6c 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61  ulian day.  We a
7af30 6c 77 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20  lways assume.** 
7af40 74 68 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d  that the YYYY-MM
7af50 2d 44 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67  -DD is according
7af60 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61   to the Gregoria
7af70 6e 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a  n calendar..**.*
7af80 2a 20 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65  * Reference:  Me
7af90 65 75 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73  eus page 61.*/.s
7afa0 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75  tatic void compu
7afb0 74 65 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70  teJD(DateTime *p
7afc0 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44  ){.  int Y, M, D
7afd0 2c 20 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a  , A, B, X1, X2;.
7afe0 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a  .  if( p->validJ
7aff0 44 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  D ) return;.  if
7b000 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b  ( p->validYMD ){
7b010 0a 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20  .    Y = p->Y;. 
7b020 20 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20     M = p->M;.   
7b030 20 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c   D = p->D;.  }el
7b040 73 65 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30  se{.    Y = 2000
7b050 3b 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20  ;  /* If no YMD 
7b060 73 70 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d  specified, assum
7b070 65 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f  e 2000-Jan-01 */
7b080 0a 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20  .    M = 1;.    
7b090 44 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  D = 1;.  }.  if(
7b0a0 20 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d   M<=2 ){.    Y--
7b0b0 3b 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20  ;.    M += 12;. 
7b0c0 20 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a   }.  A = Y/100;.
7b0d0 20 20 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41    B = 2 - A + (A
7b0e0 2f 34 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32  /4);.  X1 = 3652
7b0f0 35 2a 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a  5*(Y+4716)/100;.
7b100 20 20 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d    X2 = 306001*(M
7b110 2b 31 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e  +1)/10000;.  p->
7b120 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69  iJD = (sqlite3_i
7b130 6e 74 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b  nt64)((X1 + X2 +
7b140 20 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20   D + B - 1524.5 
7b150 29 20 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20  ) * 86400000);. 
7b160 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b   p->validJD = 1;
7b170 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48  .  if( p->validH
7b180 4d 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44  MS ){.    p->iJD
7b190 20 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30   += p->h*3600000
7b1a0 20 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20   + p->m*60000 + 
7b1b0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
7b1c0 70 2d 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20  p->s*1000);.    
7b1d0 69 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29  if( p->validTZ )
7b1e0 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d  {.      p->iJD -
7b1f0 3d 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20  = p->tz*60000;. 
7b200 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44       p->validYMD
7b210 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76   = 0;.      p->v
7b220 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20  alidHMS = 0;.   
7b230 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20     p->validTZ = 
7b240 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
7b250 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65  /*.** Parse date
7b260 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a  s of the form.**
7b270 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d  .**     YYYY-MM-
7b280 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a  DD HH:MM:SS.FFF.
7b290 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44  **     YYYY-MM-D
7b2a0 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20  D HH:MM:SS.**   
7b2b0 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a    YYYY-MM-DD HH:
7b2c0 4d 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d  MM.**     YYYY-M
7b2d0 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65  M-DD.**.** Write
7b2e0 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f   the result into
7b2f0 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74   the DateTime st
7b300 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75  ructure and retu
7b310 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65  rn 0.** on succe
7b320 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 20  ss and 1 if the 
7b330 69 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73 20  input string is 
7b340 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65  not a well-forme
7b350 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74  d.** date..*/.st
7b360 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 59 79  atic int parseYy
7b370 79 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61  yyMmDd(const cha
7b380 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69  r *zDate, DateTi
7b390 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c  me *p){.  int Y,
7b3a0 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69   M, D, neg;..  i
7b3b0 66 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27  f( zDate[0]=='-'
7b3c0 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b   ){.    zDate++;
7b3d0 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20  .    neg = 1;.  
7b3e0 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d  }else{.    neg =
7b3f0 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65   0;.  }.  if( ge
7b400 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c  tDigits(zDate,4,
7b410 30 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c  0,9999,'-',&Y,2,
7b420 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c  1,12,'-',&M,2,1,
7b430 33 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20  31,0,&D)!=3 ){. 
7b440 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
7b450 0a 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a  .  zDate += 10;.
7b460 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
7b470 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20  Isspace(*zDate) 
7b480 7c 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44  || 'T'==*(u8*)zD
7b490 61 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20  ate ){ zDate++; 
7b4a0 7d 0a 20 20 69 66 28 20 70 61 72 73 65 48 68 4d  }.  if( parseHhM
7b4b0 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30  mSs(zDate, p)==0
7b4c0 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f   ){.    /* We go
7b4d0 74 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20  t the time */.  
7b4e0 7d 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65  }else if( *zDate
7b4f0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61  ==0 ){.    p->va
7b500 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65  lidHMS = 0;.  }e
7b510 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
7b520 31 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69  1;.  }.  p->vali
7b530 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61  dJD = 0;.  p->va
7b540 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d  lidYMD = 1;.  p-
7b550 3e 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20  >Y = neg ? -Y : 
7b560 59 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20  Y;.  p->M = M;. 
7b570 20 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28   p->D = D;.  if(
7b580 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20   p->validTZ ){. 
7b590 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
7b5a0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
7b5b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
7b5c0 65 20 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75  e time to the cu
7b5d0 72 72 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72  rrent time repor
7b5e0 74 65 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a  ted by the VFS.*
7b5f0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65  /.static void se
7b600 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65  tDateTimeToCurre
7b610 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nt(sqlite3_conte
7b620 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74  xt *context, Dat
7b630 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75  eTime *p){.  dou
7b640 62 6c 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33  ble r;.  sqlite3
7b650 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63   *db = sqlite3_c
7b660 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
7b670 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c  (context);.  sql
7b680 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d  ite3OsCurrentTim
7b690 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b  e(db->pVfs, &r);
7b6a0 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c  .  p->iJD = (sql
7b6b0 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36  ite3_int64)(r*86
7b6c0 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b  400000.0 + 0.5);
7b6d0 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20  .  p->validJD = 
7b6e0 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  1;.}../*.** Atte
7b6f0 6d 70 74 20 74 6f 20 70 61 72 73 65 20 74 68 65  mpt to parse the
7b700 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e   given string in
7b710 74 6f 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20  to a Julian Day 
7b720 4e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a  Number.  Return.
7b730 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
7b740 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54   errors..**.** T
7b750 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
7b760 20 61 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d   acceptable form
7b770 73 20 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20  s for the input 
7b780 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  string:.**.**   
7b790 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48     YYYY-MM-DD HH
7b7a0 3a 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48  :MM:SS.FFF  +/-H
7b7b0 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44  H:MM.**      DDD
7b7c0 44 2e 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f  D.DD .**      no
7b7d0 77 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66  w.**.** In the f
7b7e0 69 72 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b  irst form, the +
7b7f0 2f 2d 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79  /-HH:MM is alway
7b800 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65  s optional.  The
7b810 20 66 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73   fractional.** s
7b820 65 63 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e  econds extension
7b830 20 28 74 68 65 20 22 2e 46 46 46 22 29 20 69 73   (the ".FFF") is
7b840 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20   optional.  The 
7b850 73 65 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a  seconds portion.
7b860 2a 2a 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69  ** (":SS.FFF") i
7b870 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79  s option.  The y
7b880 65 61 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e  ear and date can
7b890 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c   be omitted as l
7b8a0 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20  ong.** as there 
7b8b0 69 73 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67  is a time string
7b8c0 2e 20 20 54 68 65 20 74 69 6d 65 20 73 74 72 69  .  The time stri
7b8d0 6e 67 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65  ng can be omitte
7b8e0 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20  d as long.** as 
7b8f0 74 68 65 72 65 20 69 73 20 61 20 79 65 61 72 20  there is a year 
7b900 61 6e 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61  and date..*/.sta
7b910 74 69 63 20 69 6e 74 20 70 61 72 73 65 44 61 74  tic int parseDat
7b920 65 4f 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74  eOrTime(.  sqlit
7b930 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
7b940 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  ext, .  const ch
7b950 61 72 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61  ar *zDate, .  Da
7b960 74 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69  teTime *p.){.  i
7b970 6e 74 20 69 73 52 65 61 6c 4e 75 6d 3b 20 20 20  nt isRealNum;   
7b980 20 2f 2a 20 52 65 74 75 72 6e 20 66 72 6f 6d 20   /* Return from 
7b990 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28  sqlite3IsNumber(
7b9a0 29 2e 20 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a  ).  Not used */.
7b9b0 20 20 69 66 28 20 70 61 72 73 65 59 79 79 79 4d    if( parseYyyyM
7b9c0 6d 44 64 28 7a 44 61 74 65 2c 70 29 3d 3d 30 20  mDd(zDate,p)==0 
7b9d0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
7b9e0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 72  .  }else if( par
7b9f0 73 65 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20  seHhMmSs(zDate, 
7ba00 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  p)==0 ){.    ret
7ba10 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  urn 0;.  }else i
7ba20 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
7ba30 70 28 7a 44 61 74 65 2c 22 6e 6f 77 22 29 3d 3d  p(zDate,"now")==
7ba40 30 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54  0){.    setDateT
7ba50 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e  imeToCurrent(con
7ba60 74 65 78 74 2c 20 70 29 3b 0a 20 20 20 20 72 65  text, p);.    re
7ba70 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20  turn 0;.  }else 
7ba80 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d  if( sqlite3IsNum
7ba90 62 65 72 28 7a 44 61 74 65 2c 20 26 69 73 52 65  ber(zDate, &isRe
7baa0 61 6c 4e 75 6d 2c 20 53 51 4c 49 54 45 5f 55 54  alNum, SQLITE_UT
7bab0 46 38 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c  F8) ){.    doubl
7bac0 65 20 72 3b 0a 20 20 20 20 67 65 74 56 61 6c 75  e r;.    getValu
7bad0 65 28 7a 44 61 74 65 2c 20 26 72 29 3b 0a 20 20  e(zDate, &r);.  
7bae0 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69    p->iJD = (sqli
7baf0 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34  te3_int64)(r*864
7bb00 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a  00000.0 + 0.5);.
7bb10 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d      p->validJD =
7bb20 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   1;.    return 0
7bb30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  ;.  }.  return 1
7bb40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  ;.}../*.** Compu
7bb50 74 65 20 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e  te the Year, Mon
7bb60 74 68 2c 20 61 6e 64 20 44 61 79 20 66 72 6f 6d  th, and Day from
7bb70 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20   the julian day 
7bb80 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
7bb90 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d  c void computeYM
7bba0 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  D(DateTime *p){.
7bbb0 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43    int Z, A, B, C
7bbc0 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66  , D, E, X1;.  if
7bbd0 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20  ( p->validYMD ) 
7bbe0 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70  return;.  if( !p
7bbf0 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20  ->validJD ){.   
7bc00 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20   p->Y = 2000;.  
7bc10 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20    p->M = 1;.    
7bc20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  p->D = 1;.  }els
7bc30 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 29  e{.    Z = (int)
7bc40 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30  ((p->iJD + 43200
7bc50 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0a  000)/86400000);.
7bc60 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28 28 5a      A = (int)((Z
7bc70 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33   - 1867216.25)/3
7bc80 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 20 41 20  6524.25);.    A 
7bc90 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 41  = Z + 1 + A - (A
7bca0 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 2b  /4);.    B = A +
7bcb0 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 28   1524;.    C = (
7bcc0 69 6e 74 29 28 28 42 20 2d 20 31 32 32 2e 31 29  int)((B - 122.1)
7bcd0 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 20 44 20  /365.25);.    D 
7bce0 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 30 30 3b  = (36525*C)/100;
7bcf0 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 29 28 28  .    E = (int)((
7bd00 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a 20  B-D)/30.6001);. 
7bd10 20 20 20 58 31 20 3d 20 28 69 6e 74 29 28 33 30     X1 = (int)(30
7bd20 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 20 70 2d  .6001*E);.    p-
7bd30 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 3b  >D = B - D - X1;
7bd40 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 34  .    p->M = E<14
7bd50 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 20   ? E-1 : E-13;. 
7bd60 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e 32     p->Y = p->M>2
7bd70 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20 43 20   ? C - 4716 : C 
7bd80 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 2d  - 4715;.  }.  p-
7bd90 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 7d  >validYMD = 1;.}
7bda0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ../*.** Compute 
7bdb0 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 65  the Hour, Minute
7bdc0 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 72  , and Seconds fr
7bdd0 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61  om the julian da
7bde0 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  y number..*/.sta
7bdf0 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65  tic void compute
7be00 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29  HMS(DateTime *p)
7be10 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 66 28  {.  int s;.  if(
7be20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20 72   p->validHMS ) r
7be30 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74 65  eturn;.  compute
7be40 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 28 69 6e  JD(p);.  s = (in
7be50 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32  t)((p->iJD + 432
7be60 30 30 30 30 30 29 20 25 20 38 36 34 30 30 30 30  00000) % 8640000
7be70 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 31  0);.  p->s = s/1
7be80 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69 6e  000.0;.  s = (in
7be90 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d  t)p->s;.  p->s -
7bea0 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f  = s;.  p->h = s/
7beb0 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e  3600;.  s -= p->
7bec0 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d  h*3600;.  p->m =
7bed0 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d   s/60;.  p->s +=
7bee0 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20   s - p->m*60;.  
7bef0 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b  p->validHMS = 1;
7bf00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74  .}../*.** Comput
7bf10 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48  e both YMD and H
7bf20 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  MS.*/.static voi
7bf30 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53  d computeYMD_HMS
7bf40 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20  (DateTime *p){. 
7bf50 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a   computeYMD(p);.
7bf60 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b    computeHMS(p);
7bf70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
7bf80 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20  the YMD and HMS 
7bf90 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74  and the TZ.*/.st
7bfa0 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59  atic void clearY
7bfb0 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69  MD_HMS_TZ(DateTi
7bfc0 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c  me *p){.  p->val
7bfd0 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e  idYMD = 0;.  p->
7bfe0 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20  validHMS = 0;.  
7bff0 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a  p->validTZ = 0;.
7c000 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
7c010 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45  E_OMIT_LOCALTIME
7c020 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
7c030 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 28 69  he difference (i
7c040 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 0a  n milliseconds).
7c050 2a 2a 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c  ** between local
7c060 74 69 6d 65 20 61 6e 64 20 55 54 43 20 28 61 2e  time and UTC (a.
7c070 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72  k.a. GMT).** for
7c080 20 74 68 65 20 74 69 6d 65 20 76 61 6c 75 65 20   the time value 
7c090 70 20 77 68 65 72 65 20 70 20 69 73 20 69 6e 20  p where p is in 
7c0a0 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  UTC..*/.static s
7c0b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f 63  qlite3_int64 loc
7c0c0 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74  altimeOffset(Dat
7c0d0 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74  eTime *p){.  Dat
7c0e0 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69  eTime x, y;.  ti
7c0f0 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70  me_t t;.  x = *p
7c100 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48  ;.  computeYMD_H
7c110 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e  MS(&x);.  if( x.
7c120 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32  Y<1971 || x.Y>=2
7c130 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d  038 ){.    x.Y =
7c140 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d   2000;.    x.M =
7c150 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b   1;.    x.D = 1;
7c160 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20  .    x.h = 0;.  
7c170 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78    x.m = 0;.    x
7c180 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c  .s = 0.0;.  } el
7c190 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d  se {.    int s =
7c1a0 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 30 2e 35   (int)(x.s + 0.5
7c1b0 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a  );.    x.s = s;.
7c1c0 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a    }.  x.tz = 0;.
7c1d0 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b    x.validJD = 0;
7c1e0 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29  .  computeJD(&x)
7c1f0 3b 0a 20 20 74 20 3d 20 28 74 69 6d 65 5f 74 29  ;.  t = (time_t)
7c200 28 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32 31  (x.iJD/1000 - 21
7c210 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 30  086676*(i64)1000
7c220 30 29 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f  0);.#ifdef HAVE_
7c230 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a  LOCALTIME_R.  {.
7c240 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c      struct tm sL
7c250 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74  ocal;.    localt
7c260 69 6d 65 5f 72 28 26 74 2c 20 26 73 4c 6f 63 61  ime_r(&t, &sLoca
7c270 6c 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c  l);.    y.Y = sL
7c280 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31  ocal.tm_year + 1
7c290 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73  900;.    y.M = s
7c2a0 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31  Local.tm_mon + 1
7c2b0 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63  ;.    y.D = sLoc
7c2c0 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20  al.tm_mday;.    
7c2d0 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.h = sLocal.tm_
7c2e0 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20  hour;.    y.m = 
7c2f0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20  sLocal.tm_min;. 
7c300 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e     y.s = sLocal.
7c310 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 69  tm_sec;.  }.#eli
7c320 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c  f defined(HAVE_L
7c330 4f 43 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 48  OCALTIME_S) && H
7c340 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 0a  AVE_LOCALTIME_S.
7c350 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74    {.    struct t
7c360 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f  m sLocal;.    lo
7c370 63 61 6c 74 69 6d 65 5f 73 28 26 73 4c 6f 63 61  caltime_s(&sLoca
7c380 6c 2c 20 26 74 29 3b 0a 20 20 20 20 79 2e 59 20  l, &t);.    y.Y 
7c390 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72  = sLocal.tm_year
7c3a0 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d   + 1900;.    y.M
7c3b0 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e   = sLocal.tm_mon
7c3c0 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20   + 1;.    y.D = 
7c3d0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a  sLocal.tm_mday;.
7c3e0 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c      y.h = sLocal
7c3f0 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e  .tm_hour;.    y.
7c400 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69  m = sLocal.tm_mi
7c410 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f  n;.    y.s = sLo
7c420 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a  cal.tm_sec;.  }.
7c430 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74  #else.  {.    st
7c440 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20  ruct tm *pTm;.  
7c450 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
7c460 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74  enter(sqlite3Mut
7c470 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
7c480 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
7c490 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20  ER));.    pTm = 
7c4a0 6c 6f 63 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20  localtime(&t);. 
7c4b0 20 20 20 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d     y.Y = pTm->tm
7c4c0 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20  _year + 1900;.  
7c4d0 20 20 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f    y.M = pTm->tm_
7c4e0 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44  mon + 1;.    y.D
7c4f0 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b   = pTm->tm_mday;
7c500 0a 20 20 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e  .    y.h = pTm->
7c510 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d  tm_hour;.    y.m
7c520 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a   = pTm->tm_min;.
7c530 20 20 20 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74      y.s = pTm->t
7c540 6d 5f 73 65 63 3b 0a 20 20 20 20 73 71 6c 69 74  m_sec;.    sqlit
7c550 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
7c560 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
7c570 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
7c580 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20  ATIC_MASTER));. 
7c590 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61   }.#endif.  y.va
7c5a0 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e  lidYMD = 1;.  y.
7c5b0 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20  validHMS = 1;.  
7c5c0 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  y.validJD = 0;. 
7c5d0 20 79 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a   y.validTZ = 0;.
7c5e0 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b    computeJD(&y);
7c5f0 0a 20 20 72 65 74 75 72 6e 20 79 2e 69 4a 44 20  .  return y.iJD 
7c600 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69  - x.iJD;.}.#endi
7c610 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
7c620 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f  _LOCALTIME */../
7c630 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 6d  *.** Process a m
7c640 6f 64 69 66 69 65 72 20 74 6f 20 61 20 64 61 74  odifier to a dat
7c650 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 54  e-time stamp.  T
7c660 68 65 20 6d 6f 64 69 66 69 65 72 73 20 61 72 65  he modifiers are
7c670 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  .** as follows:.
7c680 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61  **.**     NNN da
7c690 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f  ys.**     NNN ho
7c6a0 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d  urs.**     NNN m
7c6b0 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e  inutes.**     NN
7c6c0 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a  N.NNNN seconds.*
7c6d0 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73  *     NNN months
7c6e0 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61 72  .**     NNN year
7c6f0 73 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f  s.**     start o
7c700 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73  f month.**     s
7c710 74 61 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20  tart of year.** 
7c720 20 20 20 20 73 74 61 72 74 20 6f 66 20 77 65 65      start of wee
7c730 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f  k.**     start o
7c740 66 20 64 61 79 0a 2a 2a 20 20 20 20 20 77 65 65  f day.**     wee
7c750 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e  kday N.**     un
7c760 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c  ixepoch.**     l
7c770 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20  ocaltime.**     
7c780 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  utc.**.** Return
7c790 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e   0 on success an
7c7a0 64 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20  d 1 if there is 
7c7b0 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f  any kind of erro
7c7c0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
7c7d0 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28 63   parseModifier(c
7c7e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c  onst char *zMod,
7c7f0 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20   DateTime *p){. 
7c800 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 69   int rc = 1;.  i
7c810 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72  nt n;.  double r
7c820 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75  ;.  char *z, zBu
7c830 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75  f[30];.  z = zBu
7c840 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c  f;.  for(n=0; n<
7c850 41 72 72 61 79 53 69 7a 65 28 7a 42 75 66 29 2d  ArraySize(zBuf)-
7c860 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b  1 && zMod[n]; n+
7c870 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 28  +){.    z[n] = (
7c880 63 68 61 72 29 73 71 6c 69 74 65 33 55 70 70 65  char)sqlite3Uppe
7c890 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4d 6f  rToLower[(u8)zMo
7c8a0 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6e  d[n]];.  }.  z[n
7c8b0 5d 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28  ] = 0;.  switch(
7c8c0 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66   z[0] ){.#ifndef
7c8d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43   SQLITE_OMIT_LOC
7c8e0 41 4c 54 49 4d 45 0a 20 20 20 20 63 61 73 65 20  ALTIME.    case 
7c8f0 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  'l': {.      /* 
7c900 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 20     localtime.   
7c910 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41     **.      ** A
7c920 73 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72 72  ssuming the curr
7c930 65 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20 69  ent time value i
7c940 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d  s UTC (a.k.a. GM
7c950 54 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f 0a  T), shift it to.
7c960 20 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c 6f        ** show lo
7c970 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 20  cal time..      
7c980 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72  */.      if( str
7c990 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d  cmp(z, "localtim
7c9a0 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  e")==0 ){.      
7c9b0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
7c9c0 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b          p->iJD +
7c9d0 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65  = localtimeOffse
7c9e0 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c  t(p);.        cl
7c9f0 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29  earYMD_HMS_TZ(p)
7ca00 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  ;.        rc = 0
7ca10 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
7ca20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
7ca30 64 69 66 0a 20 20 20 20 63 61 73 65 20 27 75 27  dif.    case 'u'
7ca40 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20  : {.      /*.   
7ca50 20 20 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f     **    unixepo
7ca60 63 68 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  ch.      **.    
7ca70 20 20 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63    ** Treat the c
7ca80 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
7ca90 70 2d 3e 69 4a 44 20 61 73 20 74 68 65 20 6e 75  p->iJD as the nu
7caa0 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a  mber of.      **
7cab0 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31   seconds since 1
7cac0 39 37 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f  970.  Convert to
7cad0 20 61 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64   a real julian d
7cae0 61 79 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20  ay number..     
7caf0 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74   */.      if( st
7cb00 72 63 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f  rcmp(z, "unixepo
7cb10 63 68 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61  ch")==0 && p->va
7cb20 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20  lidJD ){.       
7cb30 20 70 2d 3e 69 4a 44 20 3d 20 28 70 2d 3e 69 4a   p->iJD = (p->iJ
7cb40 44 20 2b 20 34 33 32 30 30 29 2f 38 36 34 30 30  D + 43200)/86400
7cb50 20 2b 20 32 31 30 38 36 36 37 36 2a 28 69 36 34   + 21086676*(i64
7cb60 29 31 30 30 30 30 30 30 30 3b 0a 20 20 20 20 20  )10000000;.     
7cb70 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f     clearYMD_HMS_
7cb80 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72  TZ(p);.        r
7cb90 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23  c = 0;.      }.#
7cba0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
7cbb0 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20  IT_LOCALTIME.   
7cbc0 20 20 20 65 6c 73 65 20 69 66 28 20 73 74 72 63     else if( strc
7cbd0 6d 70 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20  mp(z, "utc")==0 
7cbe0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
7cbf0 65 33 5f 69 6e 74 36 34 20 63 31 3b 0a 20 20 20  e3_int64 c1;.   
7cc00 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70       computeJD(p
7cc10 29 3b 0a 20 20 20 20 20 20 20 20 63 31 20 3d 20  );.        c1 = 
7cc20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28  localtimeOffset(
7cc30 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69  p);.        p->i
7cc40 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 20 20 20 20  JD -= c1;.      
7cc50 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54    clearYMD_HMS_T
7cc60 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  Z(p);.        p-
7cc70 3e 69 4a 44 20 2b 3d 20 63 31 20 2d 20 6c 6f 63  >iJD += c1 - loc
7cc80 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b  altimeOffset(p);
7cc90 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b  .        rc = 0;
7cca0 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
7ccb0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7ccc0 20 7d 0a 20 20 20 20 63 61 73 65 20 27 77 27 3a   }.    case 'w':
7ccd0 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20   {.      /*.    
7cce0 20 20 2a 2a 20 20 20 20 77 65 65 6b 64 61 79 20    **    weekday 
7ccf0 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  N.      **.     
7cd00 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74   ** Move the dat
7cd10 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69  e to the same ti
7cd20 6d 65 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f  me on the next o
7cd30 63 63 75 72 72 65 6e 63 65 20 6f 66 0a 20 20 20  ccurrence of.   
7cd40 20 20 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20     ** weekday N 
7cd50 77 68 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c  where 0==Sunday,
7cd60 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20   1==Monday, and 
7cd70 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68  so forth.  If th
7cd80 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 65 20  e.      ** date 
7cd90 69 73 20 61 6c 72 65 61 64 79 20 6f 6e 20 74 68  is already on th
7cda0 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 77 65  e appropriate we
7cdb0 65 6b 64 61 79 2c 20 74 68 69 73 20 69 73 20 61  ekday, this is a
7cdc0 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 20 20 2a 2f   no-op..      */
7cdd0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63  .      if( strnc
7cde0 6d 70 28 7a 2c 20 22 77 65 65 6b 64 61 79 20 22  mp(z, "weekday "
7cdf0 2c 20 38 29 3d 3d 30 20 26 26 20 67 65 74 56 61  , 8)==0 && getVa
7ce00 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 29 3e 30 0a  lue(&z[8],&r)>0.
7ce10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7ce20 20 26 26 20 28 6e 3d 28 69 6e 74 29 72 29 3d 3d   && (n=(int)r)==
7ce30 72 20 26 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37  r && n>=0 && r<7
7ce40 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
7ce50 74 65 33 5f 69 6e 74 36 34 20 5a 3b 0a 20 20 20  te3_int64 Z;.   
7ce60 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f       computeYMD_
7ce70 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20  HMS(p);.        
7ce80 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a  p->validTZ = 0;.
7ce90 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64          p->valid
7cea0 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  JD = 0;.        
7ceb0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
7cec0 20 20 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69        Z = ((p->i
7ced0 4a 44 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f  JD + 129600000)/
7cee0 38 36 34 30 30 30 30 30 29 20 25 20 37 3b 0a 20  86400000) % 7;. 
7cef0 20 20 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29         if( Z>n )
7cf00 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20 20   Z -= 7;.       
7cf10 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d 20   p->iJD += (n - 
7cf20 5a 29 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20  Z)*86400000;.   
7cf30 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d       clearYMD_HM
7cf40 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20  S_TZ(p);.       
7cf50 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d   rc = 0;.      }
7cf60 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
7cf70 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 73 27    }.    case 's'
7cf80 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20  : {.      /*.   
7cf90 20 20 20 2a 2a 20 20 20 20 73 74 61 72 74 20 6f     **    start o
7cfa0 66 20 54 54 54 54 54 0a 20 20 20 20 20 20 2a 2a  f TTTTT.      **
7cfb0 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74  .      ** Move t
7cfc0 68 65 20 64 61 74 65 20 62 61 63 6b 77 61 72 64  he date backward
7cfd0 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69  s to the beginni
7cfe0 6e 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ng of the curren
7cff0 74 20 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20  t day,.      ** 
7d000 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61 72  or month or year
7d010 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
7d020 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20   if( strncmp(z, 
7d030 22 73 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21  "start of ", 9)!
7d040 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
7d050 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20 20    z += 9;.      
7d060 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20  computeYMD(p);. 
7d070 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53       p->validHMS
7d080 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 68   = 1;.      p->h
7d090 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20   = p->m = 0;.   
7d0a0 20 20 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20     p->s = 0.0;. 
7d0b0 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20       p->validTZ 
7d0c0 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61  = 0;.      p->va
7d0d0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20  lidJD = 0;.     
7d0e0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 6d   if( strcmp(z,"m
7d0f0 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20  onth")==0 ){.   
7d100 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20       p->D = 1;. 
7d110 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
7d120 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73       }else if( s
7d130 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d  trcmp(z,"year")=
7d140 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  =0 ){.        co
7d150 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20  mputeYMD(p);.   
7d160 20 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20       p->M = 1;. 
7d170 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b         p->D = 1;
7d180 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b  .        rc = 0;
7d190 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
7d1a0 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29   strcmp(z,"day")
7d1b0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
7d1c0 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 0;.      }. 
7d1d0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
7d1e0 7d 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 0a  }.    case '+':.
7d1f0 20 20 20 20 63 61 73 65 20 27 2d 27 3a 0a 20 20      case '-':.  
7d200 20 20 63 61 73 65 20 27 30 27 3a 0a 20 20 20 20    case '0':.    
7d210 63 61 73 65 20 27 31 27 3a 0a 20 20 20 20 63 61  case '1':.    ca
7d220 73 65 20 27 32 27 3a 0a 20 20 20 20 63 61 73 65  se '2':.    case
7d230 20 27 33 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '3':.    case '
7d240 34 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27  4':.    case '5'
7d250 3a 0a 20 20 20 20 63 61 73 65 20 27 36 27 3a 0a  :.    case '6':.
7d260 20 20 20 20 63 61 73 65 20 27 37 27 3a 0a 20 20      case '7':.  
7d270 20 20 63 61 73 65 20 27 38 27 3a 0a 20 20 20 20    case '8':.    
7d280 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20  case '9': {.    
7d290 20 20 64 6f 75 62 6c 65 20 72 52 6f 75 6e 64 65    double rRounde
7d2a0 72 3b 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74  r;.      n = get
7d2b0 56 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20  Value(z, &r);.  
7d2c0 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31      assert( n>=1
7d2d0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b   );.      if( z[
7d2e0 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20  n]==':' ){.     
7d2f0 20 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72     /* A modifier
7d300 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c   of the form (+|
7d310 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61  -)HH:MM:SS.FFF a
7d320 64 64 73 20 28 6f 72 20 73 75 62 74 72 61 63 74  dds (or subtract
7d330 73 29 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  s) the.        *
7d340 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62  * specified numb
7d350 65 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e  er of hours, min
7d360 75 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61  utes, seconds, a
7d370 6e 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65  nd fractional se
7d380 63 6f 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a  conds.        **
7d390 20 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54   to the time.  T
7d3a0 68 65 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65  he ".FFF" may be
7d3b0 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22   omitted.  The "
7d3c0 3a 53 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a  :SS.FFF" may be.
7d3d0 20 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74          ** omitt
7d3e0 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ed..        */. 
7d3f0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
7d400 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20  r *z2 = z;.     
7d410 20 20 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a     DateTime tx;.
7d420 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
7d430 69 6e 74 36 34 20 64 61 79 3b 0a 20 20 20 20 20  int64 day;.     
7d440 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49     if( !sqlite3I
7d450 73 64 69 67 69 74 28 2a 7a 32 29 20 29 20 7a 32  sdigit(*z2) ) z2
7d460 2b 2b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  ++;.        mems
7d470 65 74 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f  et(&tx, 0, sizeo
7d480 66 28 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20  f(tx));.        
7d490 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28  if( parseHhMmSs(
7d4a0 7a 32 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b  z2, &tx) ) break
7d4b0 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
7d4c0 65 4a 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20  eJD(&tx);.      
7d4d0 20 20 74 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30    tx.iJD -= 4320
7d4e0 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 64 61  0000;.        da
7d4f0 79 20 3d 20 74 78 2e 69 4a 44 2f 38 36 34 30 30  y = tx.iJD/86400
7d500 30 30 30 3b 0a 20 20 20 20 20 20 20 20 74 78 2e  000;.        tx.
7d510 69 4a 44 20 2d 3d 20 64 61 79 2a 38 36 34 30 30  iJD -= day*86400
7d520 30 30 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  000;.        if(
7d530 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e   z[0]=='-' ) tx.
7d540 69 4a 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20  iJD = -tx.iJD;. 
7d550 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44         computeJD
7d560 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65  (p);.        cle
7d570 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
7d580 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
7d590 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20  += tx.iJD;.     
7d5a0 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
7d5b0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
7d5c0 7d 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a  }.      z += n;.
7d5d0 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c        while( sql
7d5e0 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20  ite3Isspace(*z) 
7d5f0 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d  ) z++;.      n =
7d600 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
7d610 28 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  (z);.      if( n
7d620 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72 65  >10 || n<3 ) bre
7d630 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  ak;.      if( z[
7d640 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e  n-1]=='s' ){ z[n
7d650 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a  -1] = 0; n--; }.
7d660 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
7d670 70 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 30  p);.      rc = 0
7d680 3b 0a 20 20 20 20 20 20 72 52 6f 75 6e 64 65 72  ;.      rRounder
7d690 20 3d 20 72 3c 30 20 3f 20 2d 30 2e 35 20 3a 20   = r<0 ? -0.5 : 
7d6a0 2b 30 2e 35 3b 0a 20 20 20 20 20 20 69 66 28 20  +0.5;.      if( 
7d6b0 6e 3d 3d 33 20 26 26 20 73 74 72 63 6d 70 28 7a  n==3 && strcmp(z
7d6c0 2c 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20  ,"day")==0 ){.  
7d6d0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20        p->iJD += 
7d6e0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
7d6f0 72 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72  r*86400000.0 + r
7d700 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20  Rounder);.      
7d710 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26  }else if( n==4 &
7d720 26 20 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 72  & strcmp(z,"hour
7d730 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
7d740 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
7d750 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36  te3_int64)(r*(86
7d760 34 30 30 30 30 30 2e 30 2f 32 34 2e 30 29 20 2b  400000.0/24.0) +
7d770 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20   rRounder);.    
7d780 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36    }else if( n==6
7d790 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69   && strcmp(z,"mi
7d7a0 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  nute")==0 ){.   
7d7b0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28       p->iJD += (
7d7c0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72  sqlite3_int64)(r
7d7d0 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34  *(86400000.0/(24
7d7e0 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75  .0*60.0)) + rRou
7d7f0 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  nder);.      }el
7d800 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73  se if( n==6 && s
7d810 74 72 63 6d 70 28 7a 2c 22 73 65 63 6f 6e 64 22  trcmp(z,"second"
7d820 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
7d830 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74  p->iJD += (sqlit
7d840 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34  e3_int64)(r*(864
7d850 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30  00000.0/(24.0*60
7d860 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75  .0*60.0)) + rRou
7d870 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  nder);.      }el
7d880 73 65 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73  se if( n==5 && s
7d890 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29  trcmp(z,"month")
7d8a0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
7d8b0 6e 74 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20  nt x, y;.       
7d8c0 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
7d8d0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d  p);.        p->M
7d8e0 20 2b 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20   += (int)r;.    
7d8f0 20 20 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f      x = p->M>0 ?
7d900 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20 28   (p->M-1)/12 : (
7d910 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20 20  p->M-12)/12;.   
7d920 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a       p->Y += x;.
7d930 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d 20          p->M -= 
7d940 78 2a 31 32 3b 0a 20 20 20 20 20 20 20 20 70 2d  x*12;.        p-
7d950 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20  >validJD = 0;.  
7d960 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
7d970 70 29 3b 0a 20 20 20 20 20 20 20 20 79 20 3d 20  p);.        y = 
7d980 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20  (int)r;.        
7d990 69 66 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20  if( y!=r ){.    
7d9a0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20        p->iJD += 
7d9b0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
7d9c0 28 72 20 2d 20 79 29 2a 33 30 2e 30 2a 38 36 34  (r - y)*30.0*864
7d9d0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64  00000.0 + rRound
7d9e0 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  er);.        }. 
7d9f0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e       }else if( n
7da00 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c  ==4 && strcmp(z,
7da10 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20  "year")==0 ){.  
7da20 20 20 20 20 20 20 69 6e 74 20 79 20 3d 20 28 69        int y = (i
7da30 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 63 6f  nt)r;.        co
7da40 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b  mputeYMD_HMS(p);
7da50 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d  .        p->Y +=
7da60 20 79 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76   y;.        p->v
7da70 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20  alidJD = 0;.    
7da80 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29      computeJD(p)
7da90 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 21  ;.        if( y!
7daa0 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =r ){.          
7dab0 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74  p->iJD += (sqlit
7dac0 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 79  e3_int64)((r - y
7dad0 29 2a 33 36 35 2e 30 2a 38 36 34 30 30 30 30 30  )*365.0*86400000
7dae0 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a  .0 + rRounder);.
7daf0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
7db00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
7db10 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 1;.      }. 
7db20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d       clearYMD_HM
7db30 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62  S_TZ(p);.      b
7db40 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
7db50 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
7db60 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
7db70 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
7db80 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20  ../*.** Process 
7db90 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72  time function ar
7dba0 67 75 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30  guments.  argv[0
7dbb0 5d 20 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65  ] is a date-time
7dbc0 20 73 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b   stamp..** argv[
7dbd0 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67  1] and following
7dbe0 20 61 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20   are modifiers. 
7dbf0 20 50 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20   Parse them all 
7dc00 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65  and write.** the
7dc10 20 72 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20   resulting time 
7dc20 69 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d  into the DateTim
7dc30 65 20 73 74 72 75 63 74 75 72 65 20 70 2e 20 20  e structure p.  
7dc40 52 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73  Return 0.** on s
7dc50 75 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20  uccess and 1 if 
7dc60 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 65 72  there are any er
7dc70 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rors..**.** If t
7dc80 68 65 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61  here are zero pa
7dc90 72 61 6d 65 74 65 72 73 20 28 69 66 20 65 76 65  rameters (if eve
7dca0 6e 20 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64  n argv[0] is und
7dcb0 65 66 69 6e 65 64 29 0a 2a 2a 20 74 68 65 6e 20  efined).** then 
7dcc0 61 73 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74  assume a default
7dcd0 20 76 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20   value of "now" 
7dce0 66 6f 72 20 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a  for argv[0]..*/.
7dcf0 73 74 61 74 69 63 20 69 6e 74 20 69 73 44 61 74  static int isDat
7dd00 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  e(.  sqlite3_con
7dd10 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a  text *context, .
7dd20 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73    int argc, .  s
7dd30 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
7dd40 72 67 76 2c 20 0a 20 20 44 61 74 65 54 69 6d 65  rgv, .  DateTime
7dd50 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   *p.){.  int i;.
7dd60 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
7dd70 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20   char *z;.  int 
7dd80 65 54 79 70 65 3b 0a 20 20 6d 65 6d 73 65 74 28  eType;.  memset(
7dd90 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29  p, 0, sizeof(*p)
7dda0 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30  );.  if( argc==0
7ddb0 20 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54   ){.    setDateT
7ddc0 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e  imeToCurrent(con
7ddd0 74 65 78 74 2c 20 70 29 3b 0a 20 20 7d 65 6c 73  text, p);.  }els
7dde0 65 20 69 66 28 20 28 65 54 79 70 65 20 3d 20 73  e if( (eType = s
7ddf0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
7de00 65 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c  e(argv[0]))==SQL
7de10 49 54 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 20  ITE_FLOAT.      
7de20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
7de30 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e  eType==SQLITE_IN
7de40 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 2d 3e  TEGER ){.    p->
7de50 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69  iJD = (sqlite3_i
7de60 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61  nt64)(sqlite3_va
7de70 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b  lue_double(argv[
7de80 30 5d 29 2a 38 36 34 30 30 30 30 30 2e 30 20 2b  0])*86400000.0 +
7de90 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76 61   0.5);.    p->va
7dea0 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 6c  lidJD = 1;.  }el
7deb0 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  se{.    z = sqli
7dec0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
7ded0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[0]);.    if(
7dee0 20 21 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 65   !z || parseDate
7def0 4f 72 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c 20  OrTime(context, 
7df00 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a  (char*)z, p) ){.
7df10 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
7df20 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28      }.  }.  for(
7df30 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b  i=1; i<argc; i++
7df40 29 7b 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20  ){.    if( (z = 
7df50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
7df60 78 74 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20  xt(argv[i]))==0 
7df70 7c 7c 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72  || parseModifier
7df80 28 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b  ((char*)z, p) ){
7df90 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
7dfa0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
7dfb0 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn 0;.}.../*.**
7dfc0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
7dfd0 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e  outines implemen
7dfe0 74 20 74 68 65 20 76 61 72 69 6f 75 73 20 64 61  t the various da
7dff0 74 65 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63  te and time func
7e000 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69  tions.** of SQLi
7e010 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20  te..*/../*.**   
7e020 20 6a 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45   julianday( TIME
7e030 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
7e040 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74  , ...).**.** Ret
7e050 75 72 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64  urn the julian d
7e060 61 79 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ay number of the
7e070 20 64 61 74 65 20 73 70 65 63 69 66 69 65 64 20   date specified 
7e080 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73  in the arguments
7e090 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
7e0a0 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20  juliandayFunc(. 
7e0b0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
7e0c0 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
7e0d0 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
7e0e0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
7e0f0 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20  .  DateTime x;. 
7e100 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74   if( isDate(cont
7e110 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  ext, argc, argv,
7e120 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63   &x)==0 ){.    c
7e130 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20  omputeJD(&x);.  
7e140 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
7e150 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c  _double(context,
7e160 20 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 2e   x.iJD/86400000.
7e170 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  0);.  }.}../*.**
7e180 20 20 20 20 64 61 74 65 74 69 6d 65 28 20 54 49      datetime( TI
7e190 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d  MESTRING, MOD, M
7e1a0 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52  OD, ...).**.** R
7e1b0 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44  eturn YYYY-MM-DD
7e1c0 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61   HH:MM:SS.*/.sta
7e1d0 74 69 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d  tic void datetim
7e1e0 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
7e1f0 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
7e200 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
7e210 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7e220 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54  *argv.){.  DateT
7e230 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44  ime x;.  if( isD
7e240 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  ate(context, arg
7e250 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20  c, argv, &x)==0 
7e260 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66  ){.    char zBuf
7e270 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75  [100];.    compu
7e280 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20  teYMD_HMS(&x);. 
7e290 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
7e2a0 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29  ntf(sizeof(zBuf)
7e2b0 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30  , zBuf, "%04d-%0
7e2c0 32 64 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32  2d-%02d %02d:%02
7e2d0 64 3a 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20  d:%02d",.       
7e2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 78 2e                x.
7e2f0 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68  Y, x.M, x.D, x.h
7e300 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73  , x.m, (int)(x.s
7e310 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ));.    sqlite3_
7e320 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
7e330 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53  ext, zBuf, -1, S
7e340 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
7e350 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20  ;.  }.}../*.**  
7e360 20 20 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49    time( TIMESTRI
7e370 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e  NG, MOD, MOD, ..
7e380 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  .).**.** Return 
7e390 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74  HH:MM:SS.*/.stat
7e3a0 69 63 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63  ic void timeFunc
7e3b0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
7e3c0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
7e3d0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
7e3e0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
7e3f0 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78  .){.  DateTime x
7e400 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63  ;.  if( isDate(c
7e410 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
7e420 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20  gv, &x)==0 ){.  
7e430 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d    char zBuf[100]
7e440 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 48 4d 53  ;.    computeHMS
7e450 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (&x);.    sqlite
7e460 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
7e470 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22  f(zBuf), zBuf, "
7e480 25 30 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c  %02d:%02d:%02d",
7e490 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29   x.h, x.m, (int)
7e4a0 78 2e 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  x.s);.    sqlite
7e4b0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
7e4c0 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c  ntext, zBuf, -1,
7e4d0 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
7e4e0 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  T);.  }.}../*.**
7e4f0 20 20 20 20 64 61 74 65 28 20 54 49 4d 45 53 54      date( TIMEST
7e500 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20  RING, MOD, MOD, 
7e510 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ...).**.** Retur
7e520 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a  n YYYY-MM-DD.*/.
7e530 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 65  static void date
7e540 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
7e550 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
7e560 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
7e570 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
7e580 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69  argv.){.  DateTi
7e590 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61  me x;.  if( isDa
7e5a0 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  te(context, argc
7e5b0 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29  , argv, &x)==0 )
7e5c0 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b  {.    char zBuf[
7e5d0 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74  100];.    comput
7e5e0 65 59 4d 44 28 26 78 29 3b 0a 20 20 20 20 73 71  eYMD(&x);.    sq
7e5f0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
7e600 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75  izeof(zBuf), zBu
7e610 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30  f, "%04d-%02d-%0
7e620 32 64 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78  2d", x.Y, x.M, x
7e630 2e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  .D);.    sqlite3
7e640 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
7e650 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20  text, zBuf, -1, 
7e660 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
7e670 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
7e680 20 20 20 73 74 72 66 74 69 6d 65 28 20 46 4f 52     strftime( FOR
7e690 4d 41 54 2c 20 54 49 4d 45 53 54 52 49 4e 47 2c  MAT, TIMESTRING,
7e6a0 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a   MOD, MOD, ...).
7e6b0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73  **.** Return a s
7e6c0 74 72 69 6e 67 20 64 65 73 63 72 69 62 65 64 20  tring described 
7e6d0 62 79 20 46 4f 52 4d 41 54 2e 20 20 43 6f 6e 76  by FORMAT.  Conv
7e6e0 65 72 73 69 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f  ersions as follo
7e6f0 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 20 20  ws:.**.**   %d  
7e700 64 61 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20  day of month.** 
7e710 20 20 25 66 20 20 2a 2a 20 66 72 61 63 74 69 6f    %f  ** fractio
7e720 6e 61 6c 20 73 65 63 6f 6e 64 73 20 20 53 53 2e  nal seconds  SS.
7e730 53 53 53 0a 2a 2a 20 20 20 25 48 20 20 68 6f 75  SSS.**   %H  hou
7e740 72 20 30 30 2d 32 34 0a 2a 2a 20 20 20 25 6a 20  r 00-24.**   %j 
7e750 20 64 61 79 20 6f 66 20 79 65 61 72 20 30 30 30   day of year 000
7e760 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a  -366.**   %J  **
7e770 20 4a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62   Julian day numb
7e780 65 72 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74  er.**   %m  mont
7e790 68 20 30 31 2d 31 32 0a 2a 2a 20 20 20 25 4d 20  h 01-12.**   %M 
7e7a0 20 6d 69 6e 75 74 65 20 30 30 2d 35 39 0a 2a 2a   minute 00-59.**
7e7b0 20 20 20 25 73 20 20 73 65 63 6f 6e 64 73 20 73     %s  seconds s
7e7c0 69 6e 63 65 20 31 39 37 30 2d 30 31 2d 30 31 0a  ince 1970-01-01.
7e7d0 2a 2a 20 20 20 25 53 20 20 73 65 63 6f 6e 64 73  **   %S  seconds
7e7e0 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 77 20 20   00-59.**   %w  
7e7f0 64 61 79 20 6f 66 20 77 65 65 6b 20 30 2d 36 20  day of week 0-6 
7e800 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 20 20   sunday==0.**   
7e810 25 57 20 20 77 65 65 6b 20 6f 66 20 79 65 61 72  %W  week of year
7e820 20 30 30 2d 35 33 0a 2a 2a 20 20 20 25 59 20 20   00-53.**   %Y  
7e830 79 65 61 72 20 30 30 30 30 2d 39 39 39 39 0a 2a  year 0000-9999.*
7e840 2a 20 20 20 25 25 20 20 25 0a 2a 2f 0a 73 74 61  *   %%  %.*/.sta
7e850 74 69 63 20 76 6f 69 64 20 73 74 72 66 74 69 6d  tic void strftim
7e860 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
7e870 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
7e880 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
7e890 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7e8a0 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54  *argv.){.  DateT
7e8b0 69 6d 65 20 78 3b 0a 20 20 75 36 34 20 6e 3b 0a  ime x;.  u64 n;.
7e8c0 20 20 73 69 7a 65 5f 74 20 69 2c 6a 3b 0a 20 20    size_t i,j;.  
7e8d0 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74  char *z;.  sqlit
7e8e0 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20  e3 *db;.  const 
7e8f0 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20 28 63 6f  char *zFmt = (co
7e900 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65  nst char*)sqlite
7e910 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
7e920 76 5b 30 5d 29 3b 0a 20 20 63 68 61 72 20 7a 42  v[0]);.  char zB
7e930 75 66 5b 31 30 30 5d 3b 0a 20 20 69 66 28 20 7a  uf[100];.  if( z
7e940 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 44 61 74 65  Fmt==0 || isDate
7e950 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2d 31  (context, argc-1
7e960 2c 20 61 72 67 76 2b 31 2c 20 26 78 29 20 29 20  , argv+1, &x) ) 
7e970 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 73  return;.  db = s
7e980 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
7e990 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
7e9a0 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d  );.  for(i=0, n=
7e9b0 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c  1; zFmt[i]; i++,
7e9c0 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a   n++){.    if( z
7e9d0 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 29 7b 0a 20  Fmt[i]=='%' ){. 
7e9e0 20 20 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d       switch( zFm
7e9f0 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20  t[i+1] ){.      
7ea00 20 20 63 61 73 65 20 27 64 27 3a 0a 20 20 20 20    case 'd':.    
7ea10 20 20 20 20 63 61 73 65 20 27 48 27 3a 0a 20 20      case 'H':.  
7ea20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a 0a        case 'm':.
7ea30 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4d 27          case 'M'
7ea40 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  :.        case '
7ea50 53 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65  S':.        case
7ea60 20 27 57 27 3a 0a 20 20 20 20 20 20 20 20 20 20   'W':.          
7ea70 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 2f  n++;.          /
7ea80 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20  * fall thru */. 
7ea90 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a         case 'w':
7eaa0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 25  .        case '%
7eab0 27 3a 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  ':.          bre
7eac0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
7ead0 20 27 66 27 3a 0a 20 20 20 20 20 20 20 20 20 20   'f':.          
7eae0 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20  n += 8;.        
7eaf0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
7eb00 20 63 61 73 65 20 27 6a 27 3a 0a 20 20 20 20 20   case 'j':.     
7eb10 20 20 20 20 20 6e 20 2b 3d 20 33 3b 0a 20 20 20       n += 3;.   
7eb20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
7eb30 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 0a        case 'Y':.
7eb40 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 38            n += 8
7eb50 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
7eb60 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
7eb70 27 73 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73  's':.        cas
7eb80 65 20 27 4a 27 3a 0a 20 20 20 20 20 20 20 20 20  e 'J':.         
7eb90 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 20 20 20 20   n += 50;.      
7eba0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
7ebb0 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
7ebc0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f        return;  /
7ebd0 2a 20 45 52 52 4f 52 2e 20 20 72 65 74 75 72 6e  * ERROR.  return
7ebe0 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20   a NULL */.     
7ebf0 20 7d 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20   }.      i++;.  
7ec00 20 20 7d 0a 20 20 7d 0a 20 20 74 65 73 74 63 61    }.  }.  testca
7ec10 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42  se( n==sizeof(zB
7ec20 75 66 29 2d 31 20 29 3b 0a 20 20 74 65 73 74 63  uf)-1 );.  testc
7ec30 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a  ase( n==sizeof(z
7ec40 42 75 66 29 20 29 3b 0a 20 20 74 65 73 74 63 61  Buf) );.  testca
7ec50 73 65 28 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e  se( n==(u64)db->
7ec60 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
7ec70 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b  MIT_LENGTH]+1 );
7ec80 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d  .  testcase( n==
7ec90 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b  (u64)db->aLimit[
7eca0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
7ecb0 47 54 48 5d 20 29 3b 0a 20 20 69 66 28 20 6e 3c  GTH] );.  if( n<
7ecc0 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29 7b 0a  sizeof(zBuf) ){.
7ecd0 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20      z = zBuf;.  
7ece0 7d 65 6c 73 65 20 69 66 28 20 6e 3e 28 75 36 34  }else if( n>(u64
7ecf0 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  )db->aLimit[SQLI
7ed00 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
7ed10 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
7ed20 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
7ed30 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  big(context);.  
7ed40 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73    return;.  }els
7ed50 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  e{.    z = sqlit
7ed60 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
7ed70 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 20 20 69  , (int)n);.    i
7ed80 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( z==0 ){.     
7ed90 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
7eda0 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74  error_nomem(cont
7edb0 65 78 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ext);.      retu
7edc0 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
7edd0 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20  computeJD(&x);. 
7ede0 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
7edf0 26 78 29 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d 30  &x);.  for(i=j=0
7ee00 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b  ; zFmt[i]; i++){
7ee10 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d  .    if( zFmt[i]
7ee20 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 7a  !='%' ){.      z
7ee30 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b  [j++] = zFmt[i];
7ee40 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
7ee50 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 73 77 69    i++;.      swi
7ee60 74 63 68 28 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a  tch( zFmt[i] ){.
7ee70 20 20 20 20 20 20 20 20 63 61 73 65 20 27 64 27          case 'd'
7ee80 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  :  sqlite3_snpri
7ee90 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30  ntf(3, &z[j],"%0
7eea0 32 64 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20  2d",x.D); j+=2; 
7eeb0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
7eec0 61 73 65 20 27 66 27 3a 20 7b 0a 20 20 20 20 20  ase 'f': {.     
7eed0 20 20 20 20 20 64 6f 75 62 6c 65 20 73 20 3d 20       double s = 
7eee0 78 2e 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69  x.s;.          i
7eef0 66 28 20 73 3e 35 39 2e 39 39 39 20 29 20 73 20  f( s>59.999 ) s 
7ef00 3d 20 35 39 2e 39 39 39 3b 0a 20 20 20 20 20 20  = 59.999;.      
7ef10 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
7ef20 69 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25  intf(7, &z[j],"%
7ef30 30 36 2e 33 66 22 2c 20 73 29 3b 0a 20 20 20 20  06.3f", s);.    
7ef40 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74        j += sqlit
7ef50 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d  e3Strlen30(&z[j]
7ef60 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
7ef70 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
7ef80 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a 20        case 'H': 
7ef90 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
7efa0 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64  f(3, &z[j],"%02d
7efb0 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72  ",x.h); j+=2; br
7efc0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
7efd0 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74  e 'W': /* Fall t
7efe0 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63  hru */.        c
7eff0 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20  ase 'j': {.     
7f000 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20       int nDay;  
7f010 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
7f020 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e  mber of days sin
7f030 63 65 20 31 73 74 20 64 61 79 20 6f 66 20 79 65  ce 1st day of ye
7f040 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ar */.          
7f050 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a  DateTime y = x;.
7f060 20 20 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69            y.vali
7f070 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dJD = 0;.       
7f080 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20     y.M = 1;.    
7f090 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20        y.D = 1;. 
7f0a0 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65           compute
7f0b0 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20  JD(&y);.        
7f0c0 20 20 6e 44 61 79 20 3d 20 28 69 6e 74 29 28 28    nDay = (int)((
7f0d0 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b 34 33 32 30  x.iJD-y.iJD+4320
7f0e0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b  0000)/86400000);
7f0f0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
7f100 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 29 7b 0a 20  Fmt[i]=='W' ){. 
7f110 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 77             int w
7f120 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79  d;   /* 0=Monday
7f130 2c 20 31 3d 54 75 65 73 64 61 79 2c 20 2e 2e 2e  , 1=Tuesday, ...
7f140 20 36 3d 53 75 6e 64 61 79 20 2a 2f 0a 20 20 20   6=Sunday */.   
7f150 20 20 20 20 20 20 20 20 20 77 64 20 3d 20 28 69           wd = (i
7f160 6e 74 29 28 28 28 78 2e 69 4a 44 2b 34 33 32 30  nt)(((x.iJD+4320
7f170 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 25  0000)/86400000)%
7f180 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  7);.            
7f190 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
7f1a0 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22  (3, &z[j],"%02d"
7f1b0 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 37 29 3b  ,(nDay+7-wd)/7);
7f1c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b  .            j +
7f1d0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 2;.          }
7f1e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
7f1f0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
7f200 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33  tf(4, &z[j],"%03
7f210 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20 20 20 20  d",nDay+1);.    
7f220 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 33 3b 0a          j += 3;.
7f230 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
7f240 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7f250 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
7f260 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20 20 20 20  ase 'J': {.     
7f270 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
7f280 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c  rintf(20, &z[j],
7f290 22 25 2e 31 36 67 22 2c 78 2e 69 4a 44 2f 38 36  "%.16g",x.iJD/86
7f2a0 34 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 20  400000.0);.     
7f2b0 20 20 20 20 20 6a 2b 3d 73 71 6c 69 74 65 33 53       j+=sqlite3S
7f2c0 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a  trlen30(&z[j]);.
7f2d0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7f2e0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
7f2f0 20 20 20 63 61 73 65 20 27 6d 27 3a 20 20 73 71     case 'm':  sq
7f300 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33  lite3_snprintf(3
7f310 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78  , &z[j],"%02d",x
7f320 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b  .M); j+=2; break
7f330 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
7f340 4d 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  M':  sqlite3_snp
7f350 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22  rintf(3, &z[j],"
7f360 25 30 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32  %02d",x.m); j+=2
7f370 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
7f380 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 20 20   case 's': {.   
7f390 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
7f3a0 6e 70 72 69 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d  nprintf(30,&z[j]
7f3b0 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 20 20 20 20  ,"%lld",.       
7f3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f3d0 20 20 20 20 28 69 36 34 29 28 78 2e 69 4a 44 2f      (i64)(x.iJD/
7f3e0 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a  1000 - 21086676*
7f3f0 28 69 36 34 29 31 30 30 30 30 29 29 3b 0a 20 20  (i64)10000));.  
7f400 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c          j += sql
7f410 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b  ite3Strlen30(&z[
7f420 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62  j]);.          b
7f430 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
7f440 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53 27          case 'S'
7f450 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  :  sqlite3_snpri
7f460 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32  ntf(3,&z[j],"%02
7f470 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b  d",(int)x.s); j+
7f480 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  =2; break;.     
7f490 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20     case 'w': {. 
7f4a0 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20           z[j++] 
7f4b0 3d 20 28 63 68 61 72 29 28 28 28 78 2e 69 4a 44  = (char)(((x.iJD
7f4c0 2b 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30  +129600000)/8640
7f4d0 30 30 30 30 29 20 25 20 37 29 20 2b 20 27 30 27  0000) % 7) + '0'
7f4e0 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
7f4f0 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
7f500 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 20 7b       case 'Y': {
7f510 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
7f520 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a  e3_snprintf(5,&z
7f530 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b  [j],"%04d",x.Y);
7f540 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65   j+=sqlite3Strle
7f550 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20  n30(&z[j]);.    
7f560 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7f570 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
7f580 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d  efault:   z[j++]
7f590 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20   = '%'; break;. 
7f5a0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
7f5b0 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73  .  z[j] = 0;.  s
7f5c0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
7f5d0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d  xt(context, z, -
7f5e0 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
7f5f0 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66           z==zBuf
7f600 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49   ? SQLITE_TRANSI
7f610 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e  ENT : SQLITE_DYN
7f620 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  AMIC);.}../*.** 
7f630 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a  current_time().*
7f640 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
7f650 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  on returns the s
7f660 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d  ame value as tim
7f670 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61  e('now')..*/.sta
7f680 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75  tic void ctimeFu
7f690 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
7f6a0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
7f6b0 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
7f6c0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7f6d0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55  *NotUsed2.){.  U
7f6e0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
7f6f0 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
7f700 64 32 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 28  d2);.  timeFunc(
7f710 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a  context, 0, 0);.
7f720 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74  }../*.** current
7f730 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68  _date().**.** Th
7f740 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
7f750 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c  rns the same val
7f760 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27  ue as date('now'
7f770 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
7f780 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73  d cdateFunc(.  s
7f790 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7f7a0 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
7f7b0 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
7f7c0 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65  3_value **NotUse
7f7d0 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  d2.){.  UNUSED_P
7f7e0 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
7f7f0 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
7f800 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 74  dateFunc(context
7f810 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , 0, 0);.}../*.*
7f820 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74  * current_timest
7f830 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73  amp().**.** This
7f840 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
7f850 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  s the same value
7f860 20 61 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f   as datetime('no
7f870 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  w')..*/.static v
7f880 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75  oid ctimestampFu
7f890 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
7f8a0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
7f8b0 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
7f8c0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7f8d0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55  *NotUsed2.){.  U
7f8e0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
7f8f0 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
7f900 64 32 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 46  d2);.  datetimeF
7f910 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20  unc(context, 0, 
7f920 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  0);.}.#endif /* 
7f930 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
7f940 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55  OMIT_DATETIME_FU
7f950 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 20  NCS) */..#ifdef 
7f960 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45  SQLITE_OMIT_DATE
7f970 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a  TIME_FUNCS./*.**
7f980 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   If the library 
7f990 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 6f  is compiled to o
7f9a0 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 61  mit the full-sca
7f9b0 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  le date and time
7f9c0 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 6f  .** handling (to
7f9d0 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 62   get a smaller b
7f9e0 69 6e 61 72 79 29 2c 20 74 68 65 20 66 6f 6c 6c  inary), the foll
7f9f0 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 65  owing minimal ve
7fa00 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  rsion.** of the 
7fa10 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 6e  functions curren
7fa20 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 72 65 6e  t_time(), curren
7fa30 74 5f 64 61 74 65 28 29 20 61 6e 64 20 63 75 72  t_date() and cur
7fa40 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29  rent_timestamp()
7fa50 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64  .** are included
7fa60 20 69 6e 73 74 65 61 64 2e 20 54 68 69 73 20 69   instead. This i
7fa70 73 20 74 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c  s to support col
7fa80 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  umn declarations
7fa90 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 65   that.** include
7faa0 20 22 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e   "DEFAULT CURREN
7fab0 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a  T_TIME" etc..**.
7fac0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
7fad0 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72   uses the C-libr
7fae0 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69  ary functions ti
7faf0 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a 2a  me(), gmtime().*
7fb00 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65 28 29  * and strftime()
7fb10 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73 74 72  . The format str
7fb20 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f 20 73  ing to pass to s
7fb30 74 72 66 74 69 6d 65 28 29 20 69 73 20 73 75 70  trftime() is sup
7fb40 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 20  plied.** as the 
7fb50 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20 74 68  user-data for th
7fb60 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  e function..*/.s
7fb70 74 61 74 69 63 20 76 6f 69 64 20 63 75 72 72 65  tatic void curre
7fb80 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 71  ntTimeFunc(.  sq
7fb90 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
7fba0 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
7fbb0 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
7fbc0 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
7fbd0 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 72  time_t t;.  char
7fbe0 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 61   *zFormat = (cha
7fbf0 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72  r *)sqlite3_user
7fc00 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a  _data(context);.
7fc10 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
7fc20 20 64 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 68   double rT;.  ch
7fc30 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20  ar zBuf[20];..  
7fc40 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
7fc50 28 61 72 67 63 29 3b 0a 20 20 55 4e 55 53 45 44  (argc);.  UNUSED
7fc60 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 76 29  _PARAMETER(argv)
7fc70 3b 0a 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65  ;..  db = sqlite
7fc80 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
7fc90 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
7fca0 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
7fcb0 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26  Time(db->pVfs, &
7fcc0 72 54 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  rT);.#ifndef SQL
7fcd0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
7fce0 47 5f 50 4f 49 4e 54 0a 20 20 74 20 3d 20 38 36  G_POINT.  t = 86
7fcf0 34 30 30 2e 30 2a 28 72 54 20 2d 20 32 34 34 30  400.0*(rT - 2440
7fd00 35 38 37 2e 35 29 20 2b 20 30 2e 35 3b 0a 23 65  587.5) + 0.5;.#e
7fd10 6c 73 65 0a 20 20 2f 2a 20 77 69 74 68 6f 75 74  lse.  /* without
7fd20 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
7fd30 73 75 70 70 6f 72 74 2c 20 72 54 20 77 69 6c 6c  support, rT will
7fd40 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61   have.  ** alrea
7fd50 64 79 20 6c 6f 73 74 20 66 72 61 63 74 69 6f 6e  dy lost fraction
7fd60 61 6c 20 64 61 79 20 70 72 65 63 69 73 69 6f 6e  al day precision
7fd70 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d 20 38 36 34  ..  */.  t = 864
7fd80 30 30 20 2a 20 28 72 54 20 2d 20 32 34 34 30 35  00 * (rT - 24405
7fd90 38 37 29 20 2d 20 34 33 32 30 30 3b 0a 23 65 6e  87) - 43200;.#en
7fda0 64 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f  dif.#ifdef HAVE_
7fdb0 47 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20  GMTIME_R.  {.   
7fdc0 20 73 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b   struct tm sNow;
7fdd0 0a 20 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74  .    gmtime_r(&t
7fde0 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74  , &sNow);.    st
7fdf0 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c  rftime(zBuf, 20,
7fe00 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29   zFormat, &sNow)
7fe10 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a  ;.  }.#else.  {.
7fe20 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70      struct tm *p
7fe30 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  Tm;.    sqlite3_
7fe40 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69  mutex_enter(sqli
7fe50 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
7fe60 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
7fe70 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20  C_MASTER));.    
7fe80 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 26 74 29  pTm = gmtime(&t)
7fe90 3b 0a 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a  ;.    strftime(z
7fea0 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74  Buf, 20, zFormat
7feb0 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69  , pTm);.    sqli
7fec0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
7fed0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
7fee0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
7fef0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a  TATIC_MASTER));.
7ff00 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71    }.#endif..  sq
7ff10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
7ff20 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
7ff30 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
7ff40 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66  SIENT);.}.#endif
7ff50 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
7ff60 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
7ff70 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76   all of the abov
7ff80 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73  e C functions as
7ff90 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e   SQL.** function
7ffa0 73 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20  s.  This should 
7ffb0 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74  be the only rout
7ffc0 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ine in this file
7ffd0 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61   with.** externa
7ffe0 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51  l linkage..*/.SQ
7fff0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
80000 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  d sqlite3Registe
80010 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f  rDateTimeFunctio
80020 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ns(void){.  stat
80030 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75  ic SQLITE_WSD Fu
80040 6e 63 44 65 66 20 61 44 61 74 65 54 69 6d 65 46  ncDef aDateTimeF
80050 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64  uncs[] = {.#ifnd
80060 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
80070 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20  ATETIME_FUNCS.  
80080 20 20 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61    FUNCTION(julia
80090 6e 64 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c  nday,        -1,
800a0 20 30 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79   0, 0, julianday
800b0 46 75 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43  Func ),.    FUNC
800c0 54 49 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20  TION(date,      
800d0 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c         -1, 0, 0,
800e0 20 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 29   dateFunc      )
800f0 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74  ,.    FUNCTION(t
80100 69 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ime,            
80110 20 2d 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46   -1, 0, 0, timeF
80120 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20  unc      ),.    
80130 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d  FUNCTION(datetim
80140 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30  e,         -1, 0
80150 2c 20 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e  , 0, datetimeFun
80160 63 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49  c  ),.    FUNCTI
80170 4f 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20  ON(strftime,    
80180 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73       -1, 0, 0, s
80190 74 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a  trftimeFunc  ),.
801a0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72      FUNCTION(cur
801b0 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20  rent_time,      
801c0 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75  0, 0, 0, ctimeFu
801d0 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55  nc     ),.    FU
801e0 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74  NCTION(current_t
801f0 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20  imestamp, 0, 0, 
80200 30 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e  0, ctimestampFun
80210 63 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  c),.    FUNCTION
80220 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20  (current_date,  
80230 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61      0, 0, 0, cda
80240 74 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65  teFunc     ),.#e
80250 6c 73 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43  lse.    STR_FUNC
80260 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d  TION(current_tim
80270 65 2c 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25  e,      0, "%H:%
80280 4d 3a 25 53 22 2c 20 20 20 20 20 20 20 20 20 20  M:%S",          
80290 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75  0, currentTimeFu
802a0 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e  nc),.    STR_FUN
802b0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69  CTION(current_ti
802c0 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d  mestamp, 0, "%Y-
802d0 25 6d 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20  %m-%d",         
802e0 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46   0, currentTimeF
802f0 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55  unc),.    STR_FU
80300 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64  NCTION(current_d
80310 61 74 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59  ate,      0, "%Y
80320 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22  -%m-%d %H:%M:%S"
80330 2c 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65  , 0, currentTime
80340 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20  Func),.#endif.  
80350 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75  };.  int i;.  Fu
80360 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68  ncDefHash *pHash
80370 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44   = &GLOBAL(FuncD
80380 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47  efHash, sqlite3G
80390 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b  lobalFunctions);
803a0 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e  .  FuncDef *aFun
803b0 63 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47  c = (FuncDef*)&G
803c0 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61  LOBAL(FuncDef, a
803d0 44 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a  DateTimeFuncs);.
803e0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72  .  for(i=0; i<Ar
803f0 72 61 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d  raySize(aDateTim
80400 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20  eFuncs); i++){. 
80410 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65     sqlite3FuncDe
80420 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26  fInsert(pHash, &
80430 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d  aFunc[i]);.  }.}
80440 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
80450 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20  * End of date.c 
80460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80490 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
804a0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e  * Begin file os.
804b0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
804c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
804d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
804e0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76  /./*.** 2005 Nov
804f0 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54  ember 29.**.** T
80500 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
80510 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
80520 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
80530 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
80540 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
80550 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
80560 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
80570 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
80580 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
80590 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
805a0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
805b0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
805c0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
805d0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
805e0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
805f0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
80600 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
80610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
80660 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
80670 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61  tains OS interfa
80680 63 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ce code that is 
80690 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a  common to all.**
806a0 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a   architectures..
806b0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 63 2c  **.** $Id: os.c,
806c0 76 20 31 2e 31 32 37 20 32 30 30 39 2f 30 37 2f  v 1.127 2009/07/
806d0 32 37 20 31 31 3a 34 31 3a 32 31 20 64 61 6e 69  27 11:41:21 dani
806e0 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
806f0 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45  .#define _SQLITE
80700 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e 64 65 66 20  _OS_C_ 1.#undef 
80710 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f  _SQLITE_OS_C_../
80720 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  *.** The default
80730 20 53 51 4c 69 74 65 20 73 71 6c 69 74 65 33 5f   SQLite sqlite3_
80740 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  vfs implementati
80750 6f 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63  ons do not alloc
80760 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28 61  ate.** memory (a
80770 63 74 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78  ctually, os_unix
80780 2e 63 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73  .c allocates a s
80790 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d  mall amount of m
807a0 65 6d 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 77 69  emory.** from wi
807b0 74 68 69 6e 20 4f 73 4f 70 65 6e 28 29 29 2c 20  thin OsOpen()), 
807c0 62 75 74 20 73 6f 6d 65 20 74 68 69 72 64 2d 70  but some third-p
807d0 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74  arty implementat
807e0 69 6f 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20  ions may..** So 
807f0 77 65 20 74 65 73 74 20 74 68 65 20 65 66 66 65  we test the effe
80800 63 74 73 20 6f 66 20 61 20 6d 61 6c 6c 6f 63 28  cts of a malloc(
80810 29 20 66 61 69 6c 69 6e 67 20 61 6e 64 20 74 68  ) failing and th
80820 65 20 73 71 6c 69 74 65 33 4f 73 58 58 58 28 29  e sqlite3OsXXX()
80830 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  .** function ret
80840 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 49 4f  urning SQLITE_IO
80850 45 52 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 20  ERR_NOMEM using 
80860 74 68 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  the DO_OS_MALLOC
80870 5f 54 45 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a  _TEST macro..**.
80880 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
80890 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69   functions are i
808a0 6e 73 74 72 75 6d 65 6e 74 65 64 20 66 6f 72 20  nstrumented for 
808b0 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
808c0 20 0a 2a 2a 20 74 65 73 74 69 6e 67 3a 0a 2a 2a   .** testing:.**
808d0 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f  .**     sqlite3O
808e0 73 4f 70 65 6e 28 29 0a 2a 2a 20 20 20 20 20 73  sOpen().**     s
808f0 71 6c 69 74 65 33 4f 73 52 65 61 64 28 29 0a 2a  qlite3OsRead().*
80900 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 57  *     sqlite3OsW
80910 72 69 74 65 28 29 0a 2a 2a 20 20 20 20 20 73 71  rite().**     sq
80920 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 0a 2a 2a  lite3OsSync().**
80930 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4c 6f       sqlite3OsLo
80940 63 6b 28 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64  ck().**.*/.#if d
80950 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
80960 53 54 29 20 26 26 20 28 53 51 4c 49 54 45 5f 4f  ST) && (SQLITE_O
80970 53 5f 57 49 4e 3d 3d 30 29 0a 20 20 23 64 65 66  S_WIN==0).  #def
80980 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  ine DO_OS_MALLOC
80990 5f 54 45 53 54 28 78 29 20 69 66 20 28 21 78 20  _TEST(x) if (!x 
809a0 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 4d 65 6d  || !sqlite3IsMem
809b0 4a 6f 75 72 6e 61 6c 28 78 29 29 20 7b 20 20 20  Journal(x)) {   
809c0 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54    \.    void *pT
809d0 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65  stAlloc = sqlite
809e0 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20  3Malloc(10);    
809f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80a00 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69           \.    i
80a10 66 20 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72  f (!pTstAlloc) r
80a20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
80a30 52 52 5f 4e 4f 4d 45 4d 3b 20 20 20 20 20 20 20  RR_NOMEM;       
80a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80a50 5c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  \.    sqlite3_fr
80a60 65 65 28 70 54 73 74 41 6c 6c 6f 63 29 3b 20 20  ee(pTstAlloc);  
80a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80a90 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c         \.  }.#el
80aa0 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f  se.  #define DO_
80ab0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 78  OS_MALLOC_TEST(x
80ac0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
80ad0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
80ae0 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65  utines are conve
80af0 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20  nience wrappers 
80b00 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a  around methods.*
80b10 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  * of the sqlite3
80b20 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54  _file object.  T
80b30 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75  his is mostly ju
80b40 73 74 20 73 79 6e 74 61 63 74 69 63 20 73 75 67  st syntactic sug
80b50 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68  ar. All.** of th
80b60 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70  is would be comp
80b70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74 69 63  letely automatic
80b80 20 69 66 20 53 51 4c 69 74 65 20 77 65 72 65 20   if SQLite were 
80b90 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43  coded using.** C
80ba0 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20 70 6c  ++ instead of pl
80bb0 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51  ain old C..*/.SQ
80bc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
80bd0 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
80be0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49  sqlite3_file *pI
80bf0 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  d){.  int rc = S
80c00 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
80c10 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b  pId->pMethods ){
80c20 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d 3e 70  .    rc = pId->p
80c30 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28  Methods->xClose(
80c40 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70  pId);.    pId->p
80c50 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d  Methods = 0;.  }
80c60 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
80c70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
80c80 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64  nt sqlite3OsRead
80c90 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
80ca0 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69  d, void *pBuf, i
80cb0 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73  nt amt, i64 offs
80cc0 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  et){.  DO_OS_MAL
80cd0 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20  LOC_TEST(id);.  
80ce0 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
80cf0 6f 64 73 2d 3e 78 52 65 61 64 28 69 64 2c 20 70  ods->xRead(id, p
80d00 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74  Buf, amt, offset
80d10 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
80d20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
80d30 73 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66  sWrite(sqlite3_f
80d40 69 6c 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76  ile *id, const v
80d50 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61  oid *pBuf, int a
80d60 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b  mt, i64 offset){
80d70 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  .  DO_OS_MALLOC_
80d80 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75  TEST(id);.  retu
80d90 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
80da0 3e 78 57 72 69 74 65 28 69 64 2c 20 70 42 75 66  >xWrite(id, pBuf
80db0 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a  , amt, offset);.
80dc0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
80dd0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72   int sqlite3OsTr
80de0 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66  uncate(sqlite3_f
80df0 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 73 69 7a  ile *id, i64 siz
80e00 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  e){.  return id-
80e10 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 54 72 75 6e  >pMethods->xTrun
80e20 63 61 74 65 28 69 64 2c 20 73 69 7a 65 29 3b 0a  cate(id, size);.
80e30 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
80e40 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 79   int sqlite3OsSy
80e50 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  nc(sqlite3_file 
80e60 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  *id, int flags){
80e70 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  .  DO_OS_MALLOC_
80e80 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75  TEST(id);.  retu
80e90 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
80ea0 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61 67 73  >xSync(id, flags
80eb0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
80ec0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
80ed0 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65  sFileSize(sqlite
80ee0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20  3_file *id, i64 
80ef0 2a 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f 53  *pSize){.  DO_OS
80f00 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29  _MALLOC_TEST(id)
80f10 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  ;.  return id->p
80f20 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69  Methods->xFileSi
80f30 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d  ze(id, pSize);.}
80f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
80f50 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  int sqlite3OsLoc
80f60 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
80f70 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65  id, int lockType
80f80 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f  ){.  DO_OS_MALLO
80f90 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65  C_TEST(id);.  re
80fa0 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64  turn id->pMethod
80fb0 73 2d 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63  s->xLock(id, loc
80fc0 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45  kType);.}.SQLITE
80fd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
80fe0 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c  ite3OsUnlock(sql
80ff0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
81000 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20  nt lockType){.  
81010 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
81020 6f 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c  ods->xUnlock(id,
81030 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51   lockType);.}.SQ
81040 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
81050 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52   sqlite3OsCheckR
81060 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
81070 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
81080 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44  t *pResOut){.  D
81090 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
810a0 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  (id);.  return i
810b0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 68  d->pMethods->xCh
810c0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
810d0 69 64 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a  id, pResOut);.}.
810e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
810f0 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65  nt sqlite3OsFile
81100 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f  Control(sqlite3_
81110 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70  file *id, int op
81120 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20  , void *pArg){. 
81130 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
81140 68 6f 64 73 2d 3e 78 46 69 6c 65 43 6f 6e 74 72  hods->xFileContr
81150 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 41 72 67 29  ol(id, op, pArg)
81160 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
81170 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
81180 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74  SectorSize(sqlit
81190 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20  e3_file *id){.  
811a0 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a  int (*xSectorSiz
811b0 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  e)(sqlite3_file*
811c0 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  ) = id->pMethods
811d0 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20  ->xSectorSize;. 
811e0 20 72 65 74 75 72 6e 20 28 78 53 65 63 74 6f 72   return (xSector
811f0 53 69 7a 65 20 3f 20 78 53 65 63 74 6f 72 53 69  Size ? xSectorSi
81200 7a 65 28 69 64 29 20 3a 20 53 51 4c 49 54 45 5f  ze(id) : SQLITE_
81210 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53  DEFAULT_SECTOR_S
81220 49 5a 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  IZE);.}.SQLITE_P
81230 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
81240 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63  e3OsDeviceCharac
81250 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65  teristics(sqlite
81260 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72  3_file *id){.  r
81270 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f  eturn id->pMetho
81280 64 73 2d 3e 78 44 65 76 69 63 65 43 68 61 72 61  ds->xDeviceChara
81290 63 74 65 72 69 73 74 69 63 73 28 69 64 29 3b 0a  cteristics(id);.
812a0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78  }../*.** The nex
812b0 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69  t group of routi
812c0 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65  nes are convenie
812d0 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f  nce wrappers aro
812e0 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46 53 20 6d  und the.** VFS m
812f0 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ethods..*/.SQLIT
81300 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
81310 6c 69 74 65 33 4f 73 4f 70 65 6e 28 0a 20 20 73  lite3OsOpen(.  s
81320 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
81330 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
81340 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c 69 74  *zPath, .  sqlit
81350 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20  e3_file *pFile, 
81360 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20  .  int flags, . 
81370 20 69 6e 74 20 2a 70 46 6c 61 67 73 4f 75 74 0a   int *pFlagsOut.
81380 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44  ){.  int rc;.  D
81390 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
813a0 28 30 29 3b 0a 20 20 2f 2a 20 30 78 37 66 31 66  (0);.  /* 0x7f1f
813b0 20 69 73 20 61 20 6d 61 73 6b 20 6f 66 20 53 51   is a mask of SQ
813c0 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73  LITE_OPEN_ flags
813d0 20 74 68 61 74 20 61 72 65 20 76 61 6c 69 64 20   that are valid 
813e0 74 6f 20 62 65 20 70 61 73 73 65 64 0a 20 20 2a  to be passed.  *
813f0 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 74 68 65 20  * down into the 
81400 56 46 53 20 6c 61 79 65 72 2e 20 20 53 6f 6d 65  VFS layer.  Some
81410 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c   SQLITE_OPEN_ fl
81420 61 67 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  ags (for example
81430 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 50  ,.  ** SQLITE_OP
81440 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 6f 72 20  EN_FULLMUTEX or 
81450 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52  SQLITE_OPEN_SHAR
81460 45 44 43 41 43 48 45 29 20 61 72 65 20 62 6c 6f  EDCACHE) are blo
81470 63 6b 65 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a  cked before.  **
81480 20 72 65 61 63 68 69 6e 67 20 74 68 65 20 56 46   reaching the VF
81490 53 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 70 56 66  S. */.  rc = pVf
814a0 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73 2c 20 7a  s->xOpen(pVfs, z
814b0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c 61  Path, pFile, fla
814c0 67 73 20 26 20 30 78 37 66 31 66 2c 20 70 46 6c  gs & 0x7f1f, pFl
814d0 61 67 73 4f 75 74 29 3b 0a 20 20 61 73 73 65 72  agsOut);.  asser
814e0 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
814f0 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68   || pFile->pMeth
81500 6f 64 73 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75  ods==0 );.  retu
81510 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
81520 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
81530 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69  te3OsDelete(sqli
81540 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63  te3_vfs *pVfs, c
81550 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
81560 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a  , int dirSync){.
81570 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78    return pVfs->x
81580 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 50 61  Delete(pVfs, zPa
81590 74 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a 7d 0a  th, dirSync);.}.
815a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
815b0 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65  nt sqlite3OsAcce
815c0 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  ss(.  sqlite3_vf
815d0 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73  s *pVfs, .  cons
815e0 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a  t char *zPath, .
815f0 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20    int flags, .  
81600 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29 7b 0a  int *pResOut.){.
81610 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54    DO_OS_MALLOC_T
81620 45 53 54 28 30 29 3b 0a 20 20 72 65 74 75 72 6e  EST(0);.  return
81630 20 70 56 66 73 2d 3e 78 41 63 63 65 73 73 28 70   pVfs->xAccess(p
81640 56 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c 61 67  Vfs, zPath, flag
81650 73 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 53  s, pResOut);.}.S
81660 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
81670 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50  t sqlite3OsFullP
81680 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74  athname(.  sqlit
81690 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20  e3_vfs *pVfs, . 
816a0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
816b0 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 74 68  th, .  int nPath
816c0 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a 7a 50  Out, .  char *zP
816d0 61 74 68 4f 75 74 0a 29 7b 0a 20 20 72 65 74 75  athOut.){.  retu
816e0 72 6e 20 70 56 66 73 2d 3e 78 46 75 6c 6c 50 61  rn pVfs->xFullPa
816f0 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 50 61  thname(pVfs, zPa
81700 74 68 2c 20 6e 50 61 74 68 4f 75 74 2c 20 7a 50  th, nPathOut, zP
81710 61 74 68 4f 75 74 29 3b 0a 7d 0a 23 69 66 6e 64  athOut);.}.#ifnd
81720 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
81730 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51  OAD_EXTENSION.SQ
81740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
81750 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70  d *sqlite3OsDlOp
81760 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  en(sqlite3_vfs *
81770 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72  pVfs, const char
81780 20 2a 7a 50 61 74 68 29 7b 0a 20 20 72 65 74 75   *zPath){.  retu
81790 72 6e 20 70 56 66 73 2d 3e 78 44 6c 4f 70 65 6e  rn pVfs->xDlOpen
817a0 28 70 56 66 73 2c 20 7a 50 61 74 68 29 3b 0a 7d  (pVfs, zPath);.}
817b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
817c0 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c  void sqlite3OsDl
817d0 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
817e0 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79  s *pVfs, int nBy
817f0 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75  te, char *zBufOu
81800 74 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 45  t){.  pVfs->xDlE
81810 72 72 6f 72 28 70 56 66 73 2c 20 6e 42 79 74 65  rror(pVfs, nByte
81820 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51  , zBufOut);.}.SQ
81830 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
81840 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53  d (*sqlite3OsDlS
81850 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ym(sqlite3_vfs *
81860 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 64 6c  pVfs, void *pHdl
81870 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
81880 53 79 6d 29 29 28 76 6f 69 64 29 7b 0a 20 20 72  Sym))(void){.  r
81890 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c 53  eturn pVfs->xDlS
818a0 79 6d 28 70 56 66 73 2c 20 70 48 64 6c 65 2c 20  ym(pVfs, pHdle, 
818b0 7a 53 79 6d 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  zSym);.}.SQLITE_
818c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
818d0 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73 71  ite3OsDlClose(sq
818e0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
818f0 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b   void *pHandle){
81900 0a 20 20 70 56 66 73 2d 3e 78 44 6c 43 6c 6f 73  .  pVfs->xDlClos
81910 65 28 70 56 66 73 2c 20 70 48 61 6e 64 6c 65 29  e(pVfs, pHandle)
81920 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
81930 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
81940 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49  XTENSION */.SQLI
81950 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
81960 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65  qlite3OsRandomne
81970 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ss(sqlite3_vfs *
81980 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c  pVfs, int nByte,
81990 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b   char *zBufOut){
819a0 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e  .  return pVfs->
819b0 78 52 61 6e 64 6f 6d 6e 65 73 73 28 70 56 66 73  xRandomness(pVfs
819c0 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66 4f 75 74  , nByte, zBufOut
819d0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
819e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
819f0 73 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76  sSleep(sqlite3_v
81a00 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 4d  fs *pVfs, int nM
81a10 69 63 72 6f 29 7b 0a 20 20 72 65 74 75 72 6e 20  icro){.  return 
81a20 70 56 66 73 2d 3e 78 53 6c 65 65 70 28 70 56 66  pVfs->xSleep(pVf
81a30 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d 0a 53 51  s, nMicro);.}.SQ
81a40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
81a50 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
81a60 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  tTime(sqlite3_vf
81a70 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20  s *pVfs, double 
81a80 2a 70 54 69 6d 65 4f 75 74 29 7b 0a 20 20 72 65  *pTimeOut){.  re
81a90 74 75 72 6e 20 70 56 66 73 2d 3e 78 43 75 72 72  turn pVfs->xCurr
81aa0 65 6e 74 54 69 6d 65 28 70 56 66 73 2c 20 70 54  entTime(pVfs, pT
81ab0 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a 53 51 4c 49  imeOut);.}..SQLI
81ac0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
81ad0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c  qlite3OsOpenMall
81ae0 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  oc(.  sqlite3_vf
81af0 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73  s *pVfs, .  cons
81b00 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 0a  t char *zFile, .
81b10 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
81b20 2a 70 70 46 69 6c 65 2c 20 0a 20 20 69 6e 74 20  *ppFile, .  int 
81b30 66 6c 61 67 73 2c 0a 20 20 69 6e 74 20 2a 70 4f  flags,.  int *pO
81b40 75 74 46 6c 61 67 73 0a 29 7b 0a 20 20 69 6e 74  utFlags.){.  int
81b50 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
81b60 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  EM;.  sqlite3_fi
81b70 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 70 46 69  le *pFile;.  pFi
81b80 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69  le = (sqlite3_fi
81b90 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  le *)sqlite3Mall
81ba0 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c  oc(pVfs->szOsFil
81bb0 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20  e);.  if( pFile 
81bc0 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
81bd0 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
81be0 7a 46 69 6c 65 2c 20 70 46 69 6c 65 2c 20 66 6c  zFile, pFile, fl
81bf0 61 67 73 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b  ags, pOutFlags);
81c00 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
81c10 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
81c20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69  sqlite3_free(pFi
81c30 6c 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  le);.    }else{.
81c40 20 20 20 20 20 20 2a 70 70 46 69 6c 65 20 3d 20        *ppFile = 
81c50 70 46 69 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d  pFile;.    }.  }
81c60 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
81c70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
81c80 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73  nt sqlite3OsClos
81c90 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 66 69  eFree(sqlite3_fi
81ca0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e  le *pFile){.  in
81cb0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
81cc0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ;.  assert( pFil
81cd0 65 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  e );.  rc = sqli
81ce0 74 65 33 4f 73 43 6c 6f 73 65 28 70 46 69 6c 65  te3OsClose(pFile
81cf0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
81d00 65 28 70 46 69 6c 65 29 3b 0a 20 20 72 65 74 75  e(pFile);.  retu
81d10 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
81d20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
81d30 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e   a wrapper aroun
81d40 64 20 74 68 65 20 4f 53 20 73 70 65 63 69 66 69  d the OS specifi
81d50 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  c implementation
81d60 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f   of.** sqlite3_o
81d70 73 5f 69 6e 69 74 28 29 2e 20 54 68 65 20 70 75  s_init(). The pu
81d80 72 70 6f 73 65 20 6f 66 20 74 68 65 20 77 72 61  rpose of the wra
81d90 70 70 65 72 20 69 73 20 74 6f 20 70 72 6f 76 69  pper is to provi
81da0 64 65 20 74 68 65 0a 2a 2a 20 61 62 69 6c 69 74  de the.** abilit
81db0 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20  y to simulate a 
81dc0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 2c 20  malloc failure, 
81dd0 73 6f 20 74 68 61 74 20 74 68 65 20 68 61 6e 64  so that the hand
81de0 6c 69 6e 67 20 6f 66 20 61 6e 0a 2a 2a 20 65 72  ling of an.** er
81df0 72 6f 72 20 69 6e 20 73 71 6c 69 74 65 33 5f 6f  ror in sqlite3_o
81e00 73 5f 69 6e 69 74 28 29 20 62 79 20 74 68 65 20  s_init() by the 
81e10 75 70 70 65 72 20 6c 61 79 65 72 73 20 63 61 6e  upper layers can
81e20 20 62 65 20 74 65 73 74 65 64 2e 0a 2a 2f 0a 53   be tested..*/.S
81e30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
81e40 74 20 73 71 6c 69 74 65 33 4f 73 49 6e 69 74 28  t sqlite3OsInit(
81e50 76 6f 69 64 29 7b 0a 20 20 76 6f 69 64 20 2a 70  void){.  void *p
81e60 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
81e70 63 28 31 30 29 3b 0a 20 20 69 66 28 20 70 3d 3d  c(10);.  if( p==
81e80 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
81e90 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74  E_NOMEM;.  sqlit
81ea0 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65  e3_free(p);.  re
81eb0 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f  turn sqlite3_os_
81ec0 69 6e 69 74 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  init();.}../*.**
81ed0 20 54 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c   The list of all
81ee0 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53 20   registered VFS 
81ef0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e  implementations.
81f00 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .*/.static sqlit
81f10 65 33 5f 76 66 73 20 2a 20 53 51 4c 49 54 45 5f  e3_vfs * SQLITE_
81f20 57 53 44 20 76 66 73 4c 69 73 74 20 3d 20 30 3b  WSD vfsList = 0;
81f30 0a 23 64 65 66 69 6e 65 20 76 66 73 4c 69 73 74  .#define vfsList
81f40 20 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 5f   GLOBAL(sqlite3_
81f50 76 66 73 20 2a 2c 20 76 66 73 4c 69 73 74 29 0a  vfs *, vfsList).
81f60 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20  ./*.** Locate a 
81f70 56 46 53 20 62 79 20 6e 61 6d 65 2e 20 20 49 66  VFS by name.  If
81f80 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67 69 76 65   no name is give
81f90 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e  n, simply return
81fa0 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 56 46   the.** first VF
81fb0 53 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a  S on the list..*
81fc0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
81fd0 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65  ite3_vfs *sqlite
81fe0 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74  3_vfs_find(const
81ff0 20 63 68 61 72 20 2a 7a 56 66 73 29 7b 0a 20 20   char *zVfs){.  
82000 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
82010 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54  s = 0;.#if SQLIT
82020 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73  E_THREADSAFE.  s
82030 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
82040 74 65 78 3b 0a 23 65 6e 64 69 66 0a 23 69 66 6e  tex;.#endif.#ifn
82050 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
82060 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72  AUTOINIT.  int r
82070 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  c = sqlite3_init
82080 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20  ialize();.  if( 
82090 72 63 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23  rc ) return 0;.#
820a0 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
820b0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 6d 75  _THREADSAFE.  mu
820c0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
820d0 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
820e0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
820f0 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  ER);.#endif.  sq
82100 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
82110 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28  r(mutex);.  for(
82120 70 56 66 73 20 3d 20 76 66 73 4c 69 73 74 3b 20  pVfs = vfsList; 
82130 70 56 66 73 3b 20 70 56 66 73 3d 70 56 66 73 2d  pVfs; pVfs=pVfs-
82140 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
82150 20 7a 56 66 73 3d 3d 30 20 29 20 62 72 65 61 6b   zVfs==0 ) break
82160 3b 0a 20 20 20 20 69 66 28 20 73 74 72 63 6d 70  ;.    if( strcmp
82170 28 7a 56 66 73 2c 20 70 56 66 73 2d 3e 7a 4e 61  (zVfs, pVfs->zNa
82180 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  me)==0 ) break;.
82190 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
821a0 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
821b0 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 3b  ;.  return pVfs;
821c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b  .}../*.** Unlink
821d0 20 61 20 56 46 53 20 66 72 6f 6d 20 74 68 65 20   a VFS from the 
821e0 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 2a 2f 0a 73  linked list.*/.s
821f0 74 61 74 69 63 20 76 6f 69 64 20 76 66 73 55 6e  tatic void vfsUn
82200 6c 69 6e 6b 28 73 71 6c 69 74 65 33 5f 76 66 73  link(sqlite3_vfs
82210 20 2a 70 56 66 73 29 7b 0a 20 20 61 73 73 65 72   *pVfs){.  asser
82220 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
82230 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74  _held(sqlite3Mut
82240 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
82250 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
82260 45 52 29 29 20 29 3b 0a 20 20 69 66 28 20 70 56  ER)) );.  if( pV
82270 66 73 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  fs==0 ){.    /* 
82280 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d 65 6c 73 65  No-op */.  }else
82290 20 69 66 28 20 76 66 73 4c 69 73 74 3d 3d 70 56   if( vfsList==pV
822a0 66 73 20 29 7b 0a 20 20 20 20 76 66 73 4c 69 73  fs ){.    vfsLis
822b0 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 74 3b  t = pVfs->pNext;
822c0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66 73  .  }else if( vfs
822d0 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  List ){.    sqli
822e0 74 65 33 5f 76 66 73 20 2a 70 20 3d 20 76 66 73  te3_vfs *p = vfs
822f0 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28  List;.    while(
82300 20 70 2d 3e 70 4e 65 78 74 20 26 26 20 70 2d 3e   p->pNext && p->
82310 70 4e 65 78 74 21 3d 70 56 66 73 20 29 7b 0a 20  pNext!=pVfs ){. 
82320 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78       p = p->pNex
82330 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
82340 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 56 66 73 20   p->pNext==pVfs 
82350 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78  ){.      p->pNex
82360 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 74 3b  t = pVfs->pNext;
82370 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
82380 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 56  .** Register a V
82390 46 53 20 77 69 74 68 20 74 68 65 20 73 79 73 74  FS with the syst
823a0 65 6d 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c  em.  It is harml
823b0 65 73 73 20 74 6f 20 72 65 67 69 73 74 65 72 20  ess to register 
823c0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 56 46 53 20  the same.** VFS 
823d0 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 20  multiple times. 
823e0 20 54 68 65 20 6e 65 77 20 56 46 53 20 62 65 63   The new VFS bec
823f0 6f 6d 65 73 20 74 68 65 20 64 65 66 61 75 6c 74  omes the default
82400 20 69 66 20 6d 61 6b 65 44 66 6c 74 20 69 73 0a   if makeDflt is.
82410 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49  ** true..*/.SQLI
82420 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
82430 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28  e3_vfs_register(
82440 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
82450 73 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29  s, int makeDflt)
82460 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
82470 78 20 2a 6d 75 74 65 78 20 3d 20 30 3b 0a 23 69  x *mutex = 0;.#i
82480 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
82490 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74  T_AUTOINIT.  int
824a0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e   rc = sqlite3_in
824b0 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66  itialize();.  if
824c0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
824d0 3b 0a 23 65 6e 64 69 66 0a 20 20 6d 75 74 65 78  ;.#endif.  mutex
824e0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
824f0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
82500 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
82510 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
82520 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a  x_enter(mutex);.
82530 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 73    vfsUnlink(pVfs
82540 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65 44 66 6c  );.  if( makeDfl
82550 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d 3d 30 20  t || vfsList==0 
82560 29 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e 65  ){.    pVfs->pNe
82570 78 74 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20  xt = vfsList;.  
82580 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 66 73    vfsList = pVfs
82590 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
825a0 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73  Vfs->pNext = vfs
825b0 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  List->pNext;.   
825c0 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 20   vfsList->pNext 
825d0 3d 20 70 56 66 73 3b 0a 20 20 7d 0a 20 20 61 73  = pVfs;.  }.  as
825e0 73 65 72 74 28 76 66 73 4c 69 73 74 29 3b 0a 20  sert(vfsList);. 
825f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
82600 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72  eave(mutex);.  r
82610 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
82620 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72 65 67 69  .}../*.** Unregi
82630 73 74 65 72 20 61 20 56 46 53 20 73 6f 20 74 68  ster a VFS so th
82640 61 74 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67  at it is no long
82650 65 72 20 61 63 63 65 73 73 69 62 6c 65 2e 0a 2a  er accessible..*
82660 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
82670 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72   sqlite3_vfs_unr
82680 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f  egister(sqlite3_
82690 76 66 73 20 2a 70 56 66 73 29 7b 0a 23 69 66 20  vfs *pVfs){.#if 
826a0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
826b0 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  E.  sqlite3_mute
826c0 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  x *mutex = sqlit
826d0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
826e0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
826f0 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66  _MASTER);.#endif
82700 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
82710 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20  _enter(mutex);. 
82720 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 73 29   vfsUnlink(pVfs)
82730 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
82740 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
82750 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
82760 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  OK;.}../********
82770 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
82780 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
82790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
827a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
827b0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
827c0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
827d0 65 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a  e fault.c ******
827e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
827f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82800 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
82810 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a 2a 20 54  8 Jan 22.**.** T
82820 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
82830 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
82840 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
82850 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
82860 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
82870 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
82880 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
82890 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
828a0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
828b0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
828c0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
828d0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
828e0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
828f0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
82900 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
82910 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
82920 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
82930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82970 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49  *******.**.** $I
82980 64 3a 20 66 61 75 6c 74 2e 63 2c 76 20 31 2e 31  d: fault.c,v 1.1
82990 31 20 32 30 30 38 2f 30 39 2f 30 32 20 30 30 3a  1 2008/09/02 00:
829a0 35 32 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a  52:52 drh Exp $.
829b0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  */../*.** This f
829c0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
829d0 65 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 65  e to support the
829e0 20 63 6f 6e 63 65 70 74 20 6f 66 20 22 62 65 6e   concept of "ben
829f0 69 67 6e 22 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20  ign" .** malloc 
82a00 66 61 69 6c 75 72 65 73 20 28 77 68 65 6e 20 74  failures (when t
82a10 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 6f 72 20  he xMalloc() or 
82a20 78 52 65 61 6c 6c 6f 63 28 29 20 6d 65 74 68 6f  xRealloc() metho
82a30 64 20 6f 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69  d of the.** sqli
82a40 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
82a50 73 74 72 75 63 74 75 72 65 20 66 61 69 6c 73 20  structure fails 
82a60 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c  to allocate a bl
82a70 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a  ock of memory.**
82a80 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30 29 2e   and returns 0).
82a90 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6d 61 6c   .**.** Most mal
82aa0 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65  loc failures are
82ab0 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 20 41 66 74   non-benign. Aft
82ac0 65 72 20 74 68 65 79 20 6f 63 63 75 72 2c 20 53  er they occur, S
82ad0 51 4c 69 74 65 0a 2a 2a 20 61 62 61 6e 64 6f 6e  QLite.** abandon
82ae0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70  s the current op
82af0 65 72 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75  eration and retu
82b00 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  rns an error cod
82b10 65 20 28 75 73 75 61 6c 6c 79 0a 2a 2a 20 53 51  e (usually.** SQ
82b20 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20 74 6f 20 74  LITE_NOMEM) to t
82b30 68 65 20 75 73 65 72 2e 20 48 6f 77 65 76 65 72  he user. However
82b40 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 20 66 61  , sometimes a fa
82b50 75 6c 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  ult is not neces
82b60 73 61 72 69 6c 79 0a 2a 2a 20 66 61 74 61 6c 2e  sarily.** fatal.
82b70 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66   For example, if
82b80 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20   a malloc fails 
82b90 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20 61  while resizing a
82ba0 20 68 61 73 68 20 74 61 62 6c 65 2c 20 74 68 69   hash table, thi
82bb0 73 20 0a 2a 2a 20 69 73 20 63 6f 6d 70 6c 65 74  s .** is complet
82bc0 65 6c 79 20 72 65 63 6f 76 65 72 61 62 6c 65 20  ely recoverable 
82bd0 73 69 6d 70 6c 79 20 62 79 20 6e 6f 74 20 63 61  simply by not ca
82be0 72 72 79 69 6e 67 20 6f 75 74 20 74 68 65 20 72  rrying out the r
82bf0 65 73 69 7a 65 2e 20 54 68 65 20 0a 2a 2a 20 68  esize. The .** h
82c00 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c 20 63  ash table will c
82c10 6f 6e 74 69 6e 75 65 20 74 6f 20 66 75 6e 63 74  ontinue to funct
82c20 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 53  ion normally.  S
82c30 6f 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  o a malloc failu
82c40 72 65 20 0a 2a 2a 20 64 75 72 69 6e 67 20 61 20  re .** during a 
82c50 68 61 73 68 20 74 61 62 6c 65 20 72 65 73 69 7a  hash table resiz
82c60 65 20 69 73 20 61 20 62 65 6e 69 67 6e 20 66 61  e is a benign fa
82c70 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65  ult..*/...#ifnde
82c80 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
82c90 49 4c 54 49 4e 5f 54 45 53 54 0a 0a 2f 2a 0a 2a  ILTIN_TEST../*.*
82ca0 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  * Global variabl
82cb0 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  es..*/.typedef s
82cc0 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c  truct BenignMall
82cd0 6f 63 48 6f 6f 6b 73 20 42 65 6e 69 67 6e 4d 61  ocHooks BenignMa
82ce0 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a 73 74 61 74 69  llocHooks;.stati
82cf0 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  c SQLITE_WSD str
82d00 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  uct BenignMalloc
82d10 48 6f 6f 6b 73 20 7b 0a 20 20 76 6f 69 64 20 28  Hooks {.  void (
82d20 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76  *xBenignBegin)(v
82d30 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  oid);.  void (*x
82d40 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29  BenignEnd)(void)
82d50 3b 0a 7d 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73  ;.} sqlite3Hooks
82d60 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a   = { 0, 0 };../*
82d70 20 54 68 65 20 22 77 73 64 48 6f 6f 6b 73 22 20   The "wsdHooks" 
82d80 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c  macro will resol
82d90 76 65 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70  ve to the approp
82da0 72 69 61 74 65 20 42 65 6e 69 67 6e 4d 61 6c 6c  riate BenignMall
82db0 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 73 74 72 75 63  ocHooks.** struc
82dc0 74 75 72 65 2e 20 20 49 66 20 77 72 69 74 61 62  ture.  If writab
82dd0 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69  le static data i
82de0 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e  s unsupported on
82df0 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20   the target,.** 
82e00 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74  we have to locat
82e10 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74  e the state vect
82e20 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20  or at run-time. 
82e30 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d   In the more com
82e40 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72  mon.** case wher
82e50 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  e writable stati
82e60 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72  c data is suppor
82e70 74 65 64 2c 20 77 73 64 48 6f 6f 6b 73 20 63 61  ted, wsdHooks ca
82e80 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79  n refer directly
82e90 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69  .** to the "sqli
82ea0 74 65 33 48 6f 6f 6b 73 22 20 73 74 61 74 65 20  te3Hooks" state 
82eb0 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20  vector declared 
82ec0 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  above..*/.#ifdef
82ed0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
82ee0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f  .# define wsdHoo
82ef0 6b 73 49 6e 69 74 20 5c 0a 20 20 42 65 6e 69 67  ksInit \.  Benig
82f00 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 2a 78 20  nMallocHooks *x 
82f10 3d 20 26 47 4c 4f 42 41 4c 28 42 65 6e 69 67 6e  = &GLOBAL(Benign
82f20 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 2c 73 71 6c 69  MallocHooks,sqli
82f30 74 65 33 48 6f 6f 6b 73 29 0a 23 20 64 65 66 69  te3Hooks).# defi
82f40 6e 65 20 77 73 64 48 6f 6f 6b 73 20 78 5b 30 5d  ne wsdHooks x[0]
82f50 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
82f60 77 73 64 48 6f 6f 6b 73 49 6e 69 74 0a 23 20 64  wsdHooksInit.# d
82f70 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 20 73  efine wsdHooks s
82f80 71 6c 69 74 65 33 48 6f 6f 6b 73 0a 23 65 6e 64  qlite3Hooks.#end
82f90 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  if.../*.** Regis
82fa0 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c  ter hooks to cal
82fb0 6c 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 65  l when sqlite3Be
82fc0 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
82fd0 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ) and.** sqlite3
82fe0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
82ff0 29 20 61 72 65 20 63 61 6c 6c 65 64 2c 20 72 65  ) are called, re
83000 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 53  spectively..*/.S
83010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
83020 69 64 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e  id sqlite3Benign
83030 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 0a 20 20 76  MallocHooks(.  v
83040 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67  oid (*xBenignBeg
83050 69 6e 29 28 76 6f 69 64 29 2c 0a 20 20 76 6f 69  in)(void),.  voi
83060 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28  d (*xBenignEnd)(
83070 76 6f 69 64 29 0a 29 7b 0a 20 20 77 73 64 48 6f  void).){.  wsdHo
83080 6f 6b 73 49 6e 69 74 3b 0a 20 20 77 73 64 48 6f  oksInit;.  wsdHo
83090 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e  oks.xBenignBegin
830a0 20 3d 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b   = xBenignBegin;
830b0 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e  .  wsdHooks.xBen
830c0 69 67 6e 45 6e 64 20 3d 20 78 42 65 6e 69 67 6e  ignEnd = xBenign
830d0 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  End;.}../*.** Th
830e0 69 73 20 28 73 71 6c 69 74 65 33 45 6e 64 42 65  is (sqlite3EndBe
830f0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 29 20 69 73  nignMalloc()) is
83100 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74   called by SQLit
83110 65 20 63 6f 64 65 20 74 6f 20 69 6e 64 69 63 61  e code to indica
83120 74 65 20 74 68 61 74 0a 2a 2a 20 73 75 62 73 65  te that.** subse
83130 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69  quent malloc fai
83140 6c 75 72 65 73 20 61 72 65 20 62 65 6e 69 67 6e  lures are benign
83150 2e 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  . A call to sqli
83160 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
83170 6f 63 28 29 0a 2a 2a 20 69 6e 64 69 63 61 74 65  oc().** indicate
83180 73 20 74 68 61 74 20 73 75 62 73 65 71 75 65 6e  s that subsequen
83190 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  t malloc failure
831a0 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e  s are non-benign
831b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
831c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
831d0 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
831e0 63 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f  c(void){.  wsdHo
831f0 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77  oksInit;.  if( w
83200 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42  sdHooks.xBenignB
83210 65 67 69 6e 20 29 7b 0a 20 20 20 20 77 73 64 48  egin ){.    wsdH
83220 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69  ooks.xBenignBegi
83230 6e 28 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54  n();.  }.}.SQLIT
83240 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
83250 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
83260 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77  alloc(void){.  w
83270 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69  sdHooksInit;.  i
83280 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e  f( wsdHooks.xBen
83290 69 67 6e 45 6e 64 20 29 7b 0a 20 20 20 20 77 73  ignEnd ){.    ws
832a0 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e  dHooks.xBenignEn
832b0 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64  d();.  }.}..#end
832c0 69 66 20 20 20 2f 2a 20 23 69 66 6e 64 65 66 20  if   /* #ifndef 
832d0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
832e0 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a  TIN_TEST */../**
832f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
83300 20 6f 66 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a   of fault.c ****
83310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
83340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
83350 69 6e 20 66 69 6c 65 20 6d 65 6d 30 2e 63 20 2a  in file mem0.c *
83360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
83390 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20  ** 2008 October 
833a0 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  28.**.** The aut
833b0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
833c0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
833d0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
833e0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
833f0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
83400 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
83410 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
83420 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
83430 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
83440 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
83450 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
83460 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
83470 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
83480 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
83490 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
834a0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
834b0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
834c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
834d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
834e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
834f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83500 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
83510 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 6e 6f 2d  e contains a no-
83520 6f 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  op memory alloca
83530 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f 72  tion drivers for
83540 20 75 73 65 20 77 68 65 6e 0a 2a 2a 20 53 51 4c   use when.** SQL
83550 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20  ITE_ZERO_MALLOC 
83560 69 73 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65  is defined.  The
83570 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76   allocation driv
83580 65 72 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a  ers implemented.
83590 2a 2a 20 68 65 72 65 20 61 6c 77 61 79 73 20 66  ** here always f
835a0 61 69 6c 2e 20 20 53 51 4c 69 74 65 20 77 69 6c  ail.  SQLite wil
835b0 6c 20 6e 6f 74 20 6f 70 65 72 61 74 65 20 77 69  l not operate wi
835c0 74 68 20 74 68 65 73 65 20 64 72 69 76 65 72 73  th these drivers
835d0 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 20  .  These.** are 
835e0 6d 65 72 65 6c 79 20 70 6c 61 63 65 68 6f 6c 64  merely placehold
835f0 65 72 73 2e 20 20 52 65 61 6c 20 64 72 69 76 65  ers.  Real drive
83600 72 73 20 6d 75 73 74 20 62 65 20 73 75 62 73 74  rs must be subst
83610 69 74 75 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20  ituted using.** 
83620 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
83630 20 62 65 66 6f 72 65 20 53 51 4c 69 74 65 20 77   before SQLite w
83640 69 6c 6c 20 6f 70 65 72 61 74 65 2e 0a 2a 2a 0a  ill operate..**.
83650 2a 2a 20 24 49 64 3a 20 6d 65 6d 30 2e 63 2c 76  ** $Id: mem0.c,v
83660 20 31 2e 31 20 32 30 30 38 2f 31 30 2f 32 38 20   1.1 2008/10/28 
83670 31 38 3a 35 38 3a 32 30 20 64 72 68 20 45 78 70  18:58:20 drh Exp
83680 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   $.*/../*.** Thi
83690 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  s version of the
836a0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
836b0 72 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74  r is the default
836c0 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64  .  It is.** used
836d0 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d   when no other m
836e0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
836f0 69 73 20 73 70 65 63 69 66 69 65 64 20 75 73 69  is specified usi
83700 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a  ng compile-time.
83710 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69  ** macros..*/.#i
83720 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45 52 4f  fdef SQLITE_ZERO
83730 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e  _MALLOC../*.** N
83740 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f 66  o-op versions of
83750 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   all memory allo
83760 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 0a  cation routines.
83770 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
83780 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
83790 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 74  (int nByte){ ret
837a0 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20  urn 0; }.static 
837b0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46  void sqlite3MemF
837c0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ree(void *pPrior
837d0 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61  ){ return; }.sta
837e0 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  tic void *sqlite
837f0 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64  3MemRealloc(void
83800 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42   *pPrior, int nB
83810 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b 20  yte){ return 0; 
83820 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  }.static int sql
83830 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64  ite3MemSize(void
83840 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 72   *pPrior){ retur
83850 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  n 0; }.static in
83860 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e  t sqlite3MemRoun
83870 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 65 74 75  dup(int n){ retu
83880 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69  rn n; }.static i
83890 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69  nt sqlite3MemIni
838a0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
838b0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
838c0 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69  OK; }.static voi
838d0 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74  d sqlite3MemShut
838e0 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73  down(void *NotUs
838f0 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a  ed){ return; }..
83900 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
83910 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72  ne is the only r
83920 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66  outine in this f
83930 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61  ile with externa
83940 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a  l linkage..**.**
83950 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f   Populate the lo
83960 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
83970 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
83980 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a  on pointers in.*
83990 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  * sqlite3GlobalC
839a0 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69  onfig.m with poi
839b0 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75  nters to the rou
839c0 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
839d0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
839e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
839f0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
83a00 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
83a10 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
83a20 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c  m_methods defaul
83a30 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20  tMethods = {.   
83a40 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c    sqlite3MemMall
83a50 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  oc,.     sqlite3
83a60 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71  MemFree,.     sq
83a70 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c  lite3MemRealloc,
83a80 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
83a90 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74  Size,.     sqlit
83aa0 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20  e3MemRoundup,.  
83ab0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69     sqlite3MemIni
83ac0 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  t,.     sqlite3M
83ad0 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20  emShutdown,.    
83ae0 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65   0.  };.  sqlite
83af0 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
83b00 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26  CONFIG_MALLOC, &
83b10 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b  defaultMethods);
83b20 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
83b30 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43  LITE_ZERO_MALLOC
83b40 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
83b50 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 30  **** End of mem0
83b60 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
83b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b90 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
83ba0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
83bb0 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem1.c *********
83bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83be0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
83bf0 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20  August 14.**.** 
83c00 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
83c10 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
83c20 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
83c30 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
83c40 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
83c50 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
83c60 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
83c70 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
83c80 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
83c90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
83ca0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
83cb0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
83cc0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
83cd0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
83ce0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
83cf0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
83d00 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
83d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83d50 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
83d60 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
83d70 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  s low-level memo
83d80 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72  ry allocation dr
83d90 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a 2a  ivers for when.*
83da0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73  * SQLite will us
83db0 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43  e the standard C
83dc0 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f  -library malloc/
83dd0 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74  realloc/free int
83de0 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 74  erface.** to obt
83df0 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69  ain the memory i
83e00 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54  t needs..**.** T
83e10 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
83e20 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
83e30 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76  s of the low-lev
83e40 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  el memory alloca
83e50 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  tion.** routines
83e60 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
83e70 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  e sqlite3_mem_me
83e80 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a  thods object..**
83e90 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 31 2e 63 2c  .** $Id: mem1.c,
83ea0 76 20 31 2e 33 30 20 32 30 30 39 2f 30 33 2f 32  v 1.30 2009/03/2
83eb0 33 20 30 34 3a 33 33 3a 33 33 20 64 61 6e 69 65  3 04:33:33 danie
83ec0 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
83ed0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  ./*.** This vers
83ee0 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
83ef0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74  y allocator is t
83f00 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74 20  he default.  It 
83f10 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e 20  is.** used when 
83f20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20  no other memory 
83f30 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 65  allocator is spe
83f40 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f 6d  cified using com
83f50 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63  pile-time.** mac
83f60 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ros..*/.#ifdef S
83f70 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
83f80 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20  LOC../*.** Like 
83f90 6d 61 6c 6c 6f 63 28 29 2c 20 62 75 74 20 72 65  malloc(), but re
83fa0 6d 65 6d 62 65 72 20 74 68 65 20 73 69 7a 65 20  member the size 
83fb0 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  of the allocatio
83fc0 6e 0a 2a 2a 20 73 6f 20 74 68 61 74 20 77 65 20  n.** so that we 
83fd0 63 61 6e 20 66 69 6e 64 20 69 74 20 6c 61 74 65  can find it late
83fe0 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d  r using sqlite3M
83ff0 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  emSize()..**.** 
84000 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76  For this low-lev
84010 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61  el routine, we a
84020 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 68  re guaranteed th
84030 61 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75  at nByte>0 becau
84040 73 65 0a 2a 2a 20 63 61 73 65 73 20 6f 66 20 6e  se.** cases of n
84050 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20 62 65 20  Byte<=0 will be 
84060 69 6e 74 65 72 63 65 70 74 65 64 20 61 6e 64 20  intercepted and 
84070 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 68 69  dealt with by hi
84080 67 68 65 72 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f  gher level.** ro
84090 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  utines..*/.stati
840a0 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d  c void *sqlite3M
840b0 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79  emMalloc(int nBy
840c0 74 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  te){.  sqlite3_i
840d0 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 72  nt64 *p;.  asser
840e0 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20  t( nByte>0 );.  
840f0 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e  nByte = ROUND8(n
84100 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 6d 61 6c  Byte);.  p = mal
84110 6c 6f 63 28 20 6e 42 79 74 65 2b 38 20 29 3b 0a  loc( nByte+8 );.
84120 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70    if( p ){.    p
84130 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20  [0] = nByte;.   
84140 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75   p++;.  }.  retu
84150 72 6e 20 28 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a  rn (void *)p;.}.
84160 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 66 72 65 65  ./*.** Like free
84170 28 29 20 62 75 74 20 77 6f 72 6b 73 20 66 6f 72  () but works for
84180 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 62 74   allocations obt
84190 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
841a0 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 0a 2a 2a  e3MemMalloc().**
841b0 20 6f 72 20 73 71 6c 69 74 65 33 4d 65 6d 52 65   or sqlite3MemRe
841c0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46  alloc()..**.** F
841d0 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65  or this low-leve
841e0 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 6c  l routine, we al
841f0 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20  ready know that 
84200 70 50 72 69 6f 72 21 3d 30 20 73 69 6e 63 65 0a  pPrior!=0 since.
84210 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 70  ** cases where p
84220 50 72 69 6f 72 3d 3d 30 20 77 69 6c 6c 20 68 61  Prior==0 will ha
84230 76 65 20 62 65 65 6e 20 69 6e 74 65 63 65 70 74  ve been intecept
84240 65 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69 74  ed and dealt wit
84250 68 0a 2a 2a 20 62 79 20 68 69 67 68 65 72 2d 6c  h.** by higher-l
84260 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  evel routines..*
84270 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
84280 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69  lite3MemFree(voi
84290 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71  d *pPrior){.  sq
842a0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d  lite3_int64 *p =
842b0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a   (sqlite3_int64*
842c0 29 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72  )pPrior;.  asser
842d0 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a  t( pPrior!=0 );.
842e0 20 20 70 2d 2d 3b 0a 20 20 66 72 65 65 28 70 29    p--;.  free(p)
842f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20  ;.}../*.** Like 
84300 72 65 61 6c 6c 6f 63 28 29 2e 20 20 52 65 73 69  realloc().  Resi
84310 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  ze an allocation
84320 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61   previously obta
84330 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c  ined from.** sql
84340 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 2e  ite3MemMalloc().
84350 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20  .**.** For this 
84360 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e 74 65 72 66  low-level interf
84370 61 63 65 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61  ace, we know tha
84380 74 20 70 50 72 69 6f 72 21 3d 30 2e 20 20 43 61  t pPrior!=0.  Ca
84390 73 65 73 20 77 68 65 72 65 0a 2a 2a 20 70 50 72  ses where.** pPr
843a0 69 6f 72 3d 3d 30 20 77 68 69 6c 65 20 68 61 76  ior==0 while hav
843b0 65 20 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74  e been intercept
843c0 65 64 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76  ed by higher-lev
843d0 65 6c 20 72 6f 75 74 69 6e 65 20 61 6e 64 0a 2a  el routine and.*
843e0 2a 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20  * redirected to 
843f0 78 4d 61 6c 6c 6f 63 2e 20 20 53 69 6d 69 6c 61  xMalloc.  Simila
84400 72 6c 79 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61  rly, we know tha
84410 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75 73  t nByte>0 becaus
84420 65 73 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72  es.** cases wher
84430 65 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20  e nByte<=0 will 
84440 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72 63  have been interc
84450 65 70 74 65 64 20 62 79 20 68 69 67 68 65 72 2d  epted by higher-
84460 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65  level.** routine
84470 73 20 61 6e 64 20 72 65 64 69 72 65 63 74 65 64  s and redirected
84480 20 74 6f 20 78 46 72 65 65 2e 0a 2a 2f 0a 73 74   to xFree..*/.st
84490 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74  atic void *sqlit
844a0 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69  e3MemRealloc(voi
844b0 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e  d *pPrior, int n
844c0 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  Byte){.  sqlite3
844d0 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c  _int64 *p = (sql
844e0 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69  ite3_int64*)pPri
844f0 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  or;.  assert( pP
84500 72 69 6f 72 21 3d 30 20 26 26 20 6e 42 79 74 65  rior!=0 && nByte
84510 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20  >0 );.  nByte = 
84520 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20  ROUND8(nByte);. 
84530 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e   p = (sqlite3_in
84540 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 70  t64*)pPrior;.  p
84550 2d 2d 3b 0a 20 20 70 20 3d 20 72 65 61 6c 6c 6f  --;.  p = reallo
84560 63 28 70 2c 20 6e 42 79 74 65 2b 38 20 29 3b 0a  c(p, nByte+8 );.
84570 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70    if( p ){.    p
84580 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20  [0] = nByte;.   
84590 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75   p++;.  }.  retu
845a0 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a  rn (void*)p;.}..
845b0 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74 20 74 68 65  /*.** Report the
845c0 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20   allocated size 
845d0 6f 66 20 61 20 70 72 69 6f 72 20 72 65 74 75 72  of a prior retur
845e0 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 28 29  n from xMalloc()
845f0 0a 2a 2a 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28  .** or xRealloc(
84600 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
84610 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28   sqlite3MemSize(
84620 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20  void *pPrior){. 
84630 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
84640 70 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d  p;.  if( pPrior=
84650 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
84660 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e   p = (sqlite3_in
84670 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 70  t64*)pPrior;.  p
84680 2d 2d 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e  --;.  return (in
84690 74 29 70 5b 30 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  t)p[0];.}../*.**
846a0 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75   Round up a requ
846b0 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20  est size to the 
846c0 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63  next valid alloc
846d0 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73  ation size..*/.s
846e0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
846f0 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20  3MemRoundup(int 
84700 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f 55  n){.  return ROU
84710 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ND8(n);.}../*.**
84720 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73   Initialize this
84730 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74   module..*/.stat
84740 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
84750 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55  mInit(void *NotU
84760 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
84770 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
84780 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
84790 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
847a0 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69  Deinitialize thi
847b0 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61  s module..*/.sta
847c0 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
847d0 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64  MemShutdown(void
847e0 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
847f0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
84800 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
84810 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  n;.}../*.** This
84820 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20   routine is the 
84830 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20  only routine in 
84840 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65  this file with e
84850 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e  xternal linkage.
84860 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20  .**.** Populate 
84870 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  the low-level me
84880 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
84890 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
848a0 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47  s in.** sqlite3G
848b0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69  lobalConfig.m wi
848c0 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  th pointers to t
848d0 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  he routines in t
848e0 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c  his file..*/.SQL
848f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
84900 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65   sqlite3MemSetDe
84910 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73  fault(void){.  s
84920 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
84930 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
84940 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d  defaultMethods =
84950 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d   {.     sqlite3M
84960 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73  emMalloc,.     s
84970 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20  qlite3MemFree,. 
84980 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65      sqlite3MemRe
84990 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69  alloc,.     sqli
849a0 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20  te3MemSize,.    
849b0 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64   sqlite3MemRound
849c0 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  up,.     sqlite3
849d0 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71  MemInit,.     sq
849e0 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e  lite3MemShutdown
849f0 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20  ,.     0.  };.  
84a00 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53  sqlite3_config(S
84a10 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c  QLITE_CONFIG_MAL
84a20 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74  LOC, &defaultMet
84a30 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66  hods);.}..#endif
84a40 20 2f 2a 20 53 51 4c 49 54 45 5f 53 59 53 54 45   /* SQLITE_SYSTE
84a50 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a  M_MALLOC */../**
84a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
84a70 20 6f 66 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a   of mem1.c *****
84a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
84ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
84ac0 69 6e 20 66 69 6c 65 20 6d 65 6d 32 2e 63 20 2a  in file mem2.c *
84ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
84b00 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31  ** 2007 August 1
84b10 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
84b20 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
84b30 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
84b40 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
84b50 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
84b60 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
84b70 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
84b80 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
84b90 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
84ba0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
84bb0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
84bc0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
84bd0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
84be0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
84bf0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
84c00 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
84c10 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
84c20 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
84c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84c70 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
84c80 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65   contains low-le
84c90 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  vel memory alloc
84ca0 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f  ation drivers fo
84cb0 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65  r when.** SQLite
84cc0 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 73 74   will use the st
84cd0 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61 72 79  andard C-library
84ce0 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f   malloc/realloc/
84cf0 66 72 65 65 20 69 6e 74 65 72 66 61 63 65 0a 2a  free interface.*
84d00 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20  * to obtain the 
84d10 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 20  memory it needs 
84d20 77 68 69 6c 65 20 61 64 64 69 6e 67 20 6c 6f 74  while adding lot
84d30 73 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20  s of additional 
84d40 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 69 6e 66  debugging.** inf
84d50 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 65 61 63 68  ormation to each
84d60 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 6f   allocation in o
84d70 72 64 65 72 20 74 6f 20 68 65 6c 70 20 64 65 74  rder to help det
84d80 65 63 74 20 61 6e 64 20 66 69 78 20 6d 65 6d 6f  ect and fix memo
84d90 72 79 0a 2a 2a 20 6c 65 61 6b 73 20 61 6e 64 20  ry.** leaks and 
84da0 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 65 72 72  memory usage err
84db0 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ors..**.** This 
84dc0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d  file contains im
84dd0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
84de0 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d   the low-level m
84df0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
84e00 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65  .** routines spe
84e10 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71  cified in the sq
84e20 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
84e30 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  s object..**.** 
84e40 24 49 64 3a 20 6d 65 6d 32 2e 63 2c 76 20 31 2e  $Id: mem2.c,v 1.
84e50 34 35 20 32 30 30 39 2f 30 33 2f 32 33 20 30 34  45 2009/03/23 04
84e60 3a 33 33 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39  :33:33 danielk19
84e70 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  77 Exp $.*/../*.
84e80 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
84e90 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  of the memory al
84ea0 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 20  locator is used 
84eb0 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 53  only if the.** S
84ec0 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 6d  QLITE_MEMDEBUG m
84ed0 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 0a  acro is defined.
84ee0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
84ef0 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a 0a 2a 2a  _MEMDEBUG../*.**
84f00 20 54 68 65 20 62 61 63 6b 74 72 61 63 65 20 66   The backtrace f
84f10 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20  unctionality is 
84f20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 77  only available w
84f30 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a 23 69 66  ith GLIBC.*/.#if
84f40 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f 0a 20 20  def __GLIBC__.  
84f50 65 78 74 65 72 6e 20 69 6e 74 20 62 61 63 6b 74  extern int backt
84f60 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69 6e 74 29  race(void**,int)
84f70 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20  ;.  extern void 
84f80 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c  backtrace_symbol
84f90 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e 73 74 2a  s_fd(void*const*
84fa0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65 6c 73 65  ,int,int);.#else
84fb0 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b 74 72  .# define backtr
84fc0 61 63 65 28 41 2c 42 29 20 31 0a 23 20 64 65 66  ace(A,B) 1.# def
84fd0 69 6e 65 20 62 61 63 6b 74 72 61 63 65 5f 73 79  ine backtrace_sy
84fe0 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c 43 29 0a  mbols_fd(A,B,C).
84ff0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61  #endif../*.** Ea
85000 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ch memory alloca
85010 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  tion looks like 
85020 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 2d 2d 2d  this:.**.**  ---
85030 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85040 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85050 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85060 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85070 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 54 69 74 6c  -----.**  | Titl
85080 65 20 7c 20 20 62 61 63 6b 74 72 61 63 65 20 70  e |  backtrace p
85090 6f 69 6e 74 65 72 73 20 7c 20 20 4d 65 6d 42 6c  ointers |  MemBl
850a0 6f 63 6b 48 64 72 20 7c 20 20 61 6c 6c 6f 63 61  ockHdr |  alloca
850b0 74 69 6f 6e 20 7c 20 20 45 6e 64 47 75 61 72 64  tion |  EndGuard
850c0 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d   |.**  ---------
850d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
850e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
850f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85100 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
85110 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63  **.** The applic
85120 61 74 69 6f 6e 20 63 6f 64 65 20 73 65 65 73 20  ation code sees 
85130 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74  only a pointer t
85140 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  o the allocation
85150 2e 20 20 57 65 20 68 61 76 65 0a 2a 2a 20 74 6f  .  We have.** to
85160 20 62 61 63 6b 20 75 70 20 66 72 6f 6d 20 74 68   back up from th
85170 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 6f 69  e allocation poi
85180 6e 74 65 72 20 74 6f 20 66 69 6e 64 20 74 68 65  nter to find the
85190 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 20 20 54   MemBlockHdr.  T
851a0 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64  he.** MemBlockHd
851b0 72 20 74 65 6c 6c 73 20 75 73 20 74 68 65 20 73  r tells us the s
851c0 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
851d0 61 74 69 6f 6e 20 61 6e 64 20 74 68 65 20 6e 75  ation and the nu
851e0 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 61 63 6b 74  mber of.** backt
851f0 72 61 63 65 20 70 6f 69 6e 74 65 72 73 2e 20 20  race pointers.  
85200 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61 20  There is also a 
85210 67 75 61 72 64 20 77 6f 72 64 20 61 74 20 74 68  guard word at th
85220 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20  e end of the.** 
85230 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 0a 2a 2f 0a  MemBlockHdr..*/.
85240 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
85250 64 72 20 7b 0a 20 20 69 36 34 20 69 53 69 7a 65  dr {.  i64 iSize
85260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
85270 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
85280 7a 65 20 6f 66 20 74 68 69 73 20 61 6c 6c 6f 63  ze of this alloc
85290 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63  ation */.  struc
852a0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
852b0 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 20 2f  Next, *pPrev;  /
852c0 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  * Linked list of
852d0 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d   all unfreed mem
852e0 6f 72 79 20 2a 2f 0a 20 20 63 68 61 72 20 6e 42  ory */.  char nB
852f0 61 63 6b 74 72 61 63 65 3b 20 20 20 20 20 20 20  acktrace;       
85300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
85310 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 74 72  Number of backtr
85320 61 63 65 73 20 6f 6e 20 74 68 69 73 20 61 6c 6c  aces on this all
85330 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 6e 42 61  oc */.  char nBa
85340 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 20 20 20  cktraceSlots;   
85350 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
85360 76 61 69 6c 61 62 6c 65 20 62 61 63 6b 74 72 61  vailable backtra
85370 63 65 20 73 6c 6f 74 73 20 2a 2f 0a 20 20 73 68  ce slots */.  sh
85380 6f 72 74 20 6e 54 69 74 6c 65 3b 20 20 20 20 20  ort nTitle;     
85390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
853a0 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 74 69    /* Bytes of ti
853b0 74 6c 65 3b 20 69 6e 63 6c 75 64 65 73 20 27 5c  tle; includes '\
853c0 30 27 20 2a 2f 0a 20 20 69 6e 74 20 69 46 6f 72  0' */.  int iFor
853d0 65 47 75 61 72 64 3b 20 20 20 20 20 20 20 20 20  eGuard;         
853e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47              /* G
853f0 75 61 72 64 20 77 6f 72 64 20 66 6f 72 20 73 61  uard word for sa
85400 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  nity */.};../*.*
85410 2a 20 47 75 61 72 64 20 77 6f 72 64 73 0a 2a 2f  * Guard words.*/
85420 0a 23 64 65 66 69 6e 65 20 46 4f 52 45 47 55 41  .#define FOREGUA
85430 52 44 20 30 78 38 30 46 35 45 31 35 33 0a 23 64  RD 0x80F5E153.#d
85440 65 66 69 6e 65 20 52 45 41 52 47 55 41 52 44 20  efine REARGUARD 
85450 30 78 45 34 36 37 36 42 35 33 0a 0a 2f 2a 0a 2a  0xE4676B53../*.*
85460 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c  * Number of mall
85470 6f 63 20 73 69 7a 65 20 69 6e 63 72 65 6d 65 6e  oc size incremen
85480 74 73 20 74 6f 20 74 72 61 63 6b 2e 0a 2a 2f 0a  ts to track..*/.
85490 23 64 65 66 69 6e 65 20 4e 43 53 49 5a 45 20 20  #define NCSIZE  
854a0 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20  1000../*.** All 
854b0 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 76 61  of the static va
854c0 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 20  riables used by 
854d0 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 20  this module are 
854e0 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74  collected.** int
854f0 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 63  o a single struc
85500 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d 22  ture named "mem"
85510 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65  .  This is to ke
85520 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63  ep the.** static
85530 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e   variables organ
85540 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75  ized and to redu
85550 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c  ce namespace pol
85560 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74  lution.** when t
85570 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f  his module is co
85580 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65  mbined with othe
85590 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d  r in the amalgam
855a0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ation..*/.static
855b0 20 73 74 72 75 63 74 20 7b 0a 20 20 0a 20 20 2f   struct {.  .  /
855c0 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20  *.  ** Mutex to 
855d0 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74  control access t
855e0 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  o the memory all
855f0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
85600 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  m..  */.  sqlite
85610 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a  3_mutex *mutex;.
85620 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48 65 61 64 20  .  /*.  ** Head 
85630 61 6e 64 20 74 61 69 6c 20 6f 66 20 61 20 6c 69  and tail of a li
85640 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c  nked list of all
85650 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c   outstanding all
85660 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2f 0a 20 20  ocations.  */.  
85670 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
85680 64 72 20 2a 70 46 69 72 73 74 3b 0a 20 20 73 74  dr *pFirst;.  st
85690 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  ruct MemBlockHdr
856a0 20 2a 70 4c 61 73 74 3b 0a 20 20 0a 20 20 2f 2a   *pLast;.  .  /*
856b0 0a 20 20 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72  .  ** The number
856c0 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20 62 61   of levels of ba
856d0 63 6b 74 72 61 63 65 20 74 6f 20 73 61 76 65 20  cktrace to save 
856e0 69 6e 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f  in new allocatio
856f0 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e  ns..  */.  int n
85700 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 76 6f 69  Backtrace;.  voi
85710 64 20 28 2a 78 42 61 63 6b 74 72 61 63 65 29 28  d (*xBacktrace)(
85720 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 2a  int, int, void *
85730 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54  *);..  /*.  ** T
85740 69 74 6c 65 20 74 65 78 74 20 74 6f 20 69 6e 73  itle text to ins
85750 65 72 74 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20  ert in front of 
85760 65 61 63 68 20 62 6c 6f 63 6b 0a 20 20 2a 2f 0a  each block.  */.
85770 20 20 69 6e 74 20 6e 54 69 74 6c 65 3b 20 20 20    int nTitle;   
85780 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66       /* Bytes of
85790 20 7a 54 69 74 6c 65 20 74 6f 20 73 61 76 65 2e   zTitle to save.
857a0 20 20 49 6e 63 6c 75 64 65 73 20 27 5c 30 27 20    Includes '\0' 
857b0 61 6e 64 20 70 61 64 64 69 6e 67 20 2a 2f 0a 20  and padding */. 
857c0 20 63 68 61 72 20 7a 54 69 74 6c 65 5b 31 30 30   char zTitle[100
857d0 5d 3b 20 20 2f 2a 20 54 68 65 20 74 69 74 6c 65  ];  /* The title
857e0 20 74 65 78 74 20 2a 2f 0a 0a 20 20 2f 2a 20 0a   text */..  /* .
857f0 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c    ** sqlite3Mall
85800 6f 63 44 69 73 61 6c 6c 6f 77 28 29 20 69 6e 63  ocDisallow() inc
85810 72 65 6d 65 6e 74 73 20 74 68 65 20 66 6f 6c 6c  rements the foll
85820 6f 77 69 6e 67 20 63 6f 75 6e 74 65 72 2e 0a 20  owing counter.. 
85830 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f   ** sqlite3Mallo
85840 63 41 6c 6c 6f 77 28 29 20 64 65 63 72 65 6d 65  cAllow() decreme
85850 6e 74 73 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69  nts it..  */.  i
85860 6e 74 20 64 69 73 61 6c 6c 6f 77 3b 20 2f 2a 20  nt disallow; /* 
85870 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 6d 65 6d  Do not allow mem
85880 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a  ory allocation *
85890 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 47 61 74  /..  /*.  ** Gat
858a0 68 65 72 20 73 74 61 74 69 73 74 69 63 73 20 6f  her statistics o
858b0 6e 20 74 68 65 20 73 69 7a 65 73 20 6f 66 20 6d  n the sizes of m
858c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
858d0 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c 6f 63 5b 69  s..  ** nAlloc[i
858e0 5d 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  ] is the number 
858f0 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74  of allocation at
85900 74 65 6d 70 74 73 20 6f 66 20 69 2a 38 0a 20 20  tempts of i*8.  
85910 2a 2a 20 62 79 74 65 73 2e 20 20 69 3d 3d 4e 43  ** bytes.  i==NC
85920 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62  SIZE is the numb
85930 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  er of allocation
85940 20 61 74 74 65 6d 70 74 73 20 66 6f 72 0a 20 20   attempts for.  
85950 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 65 20 74 68  ** sizes more th
85960 61 6e 20 4e 43 53 49 5a 45 2a 38 20 62 79 74 65  an NCSIZE*8 byte
85970 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  s..  */.  int nA
85980 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d 3b 20 20 20  lloc[NCSIZE];   
85990 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62     /* Total numb
859a0 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  er of allocation
859b0 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72  s */.  int nCurr
859c0 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20 20 20  ent[NCSIZE];    
859d0 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65  /* Current numbe
859e0 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  r of allocations
859f0 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 75 72 72   */.  int mxCurr
85a00 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20 20 2f  ent[NCSIZE];   /
85a10 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b  * Highwater mark
85a20 20 66 6f 72 20 6e 43 75 72 72 65 6e 74 20 2a 2f   for nCurrent */
85a30 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a  ..} mem;.../*.**
85a40 20 41 64 6a 75 73 74 20 6d 65 6d 6f 72 79 20 75   Adjust memory u
85a50 73 61 67 65 20 73 74 61 74 69 73 74 69 63 73 0a  sage statistics.
85a60 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
85a70 64 6a 75 73 74 53 74 61 74 73 28 69 6e 74 20 69  djustStats(int i
85a80 53 69 7a 65 2c 20 69 6e 74 20 69 6e 63 72 65 6d  Size, int increm
85a90 65 6e 74 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20  ent){.  int i = 
85aa0 52 4f 55 4e 44 38 28 69 53 69 7a 65 29 2f 38 3b  ROUND8(iSize)/8;
85ab0 0a 20 20 69 66 28 20 69 3e 4e 43 53 49 5a 45 2d  .  if( i>NCSIZE-
85ac0 31 20 29 7b 0a 20 20 20 20 69 20 3d 20 4e 43 53  1 ){.    i = NCS
85ad0 49 5a 45 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 69  IZE - 1;.  }.  i
85ae0 66 28 20 69 6e 63 72 65 6d 65 6e 74 3e 30 20 29  f( increment>0 )
85af0 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41 6c 6c 6f 63  {.    mem.nAlloc
85b00 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d 65 6d 2e 6e  [i]++;.    mem.n
85b10 43 75 72 72 65 6e 74 5b 69 5d 2b 2b 3b 0a 20 20  Current[i]++;.  
85b20 20 20 69 66 28 20 6d 65 6d 2e 6e 43 75 72 72 65    if( mem.nCurre
85b30 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78 43 75 72 72  nt[i]>mem.mxCurr
85b40 65 6e 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20  ent[i] ){.      
85b50 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d  mem.mxCurrent[i]
85b60 20 3d 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b   = mem.nCurrent[
85b70 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  i];.    }.  }els
85b80 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72  e{.    mem.nCurr
85b90 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20 20 20 61 73  ent[i]--;.    as
85ba0 73 65 72 74 28 20 6d 65 6d 2e 6e 43 75 72 72 65  sert( mem.nCurre
85bb0 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a 20 20 7d 0a  nt[i]>=0 );.  }.
85bc0 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61  }../*.** Given a
85bd0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 66 69  n allocation, fi
85be0 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48  nd the MemBlockH
85bf0 64 72 20 66 6f 72 20 74 68 61 74 20 61 6c 6c 6f  dr for that allo
85c00 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  cation..**.** Th
85c10 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
85c20 73 20 74 68 65 20 67 75 61 72 64 73 20 61 74 20  s the guards at 
85c30 65 69 74 68 65 72 20 65 6e 64 20 6f 66 20 74 68  either end of th
85c40 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64  e allocation and
85c50 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65 20  .** if they are 
85c60 69 6e 63 6f 72 72 65 63 74 20 69 74 20 61 73 73  incorrect it ass
85c70 65 72 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  erts..*/.static 
85c80 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
85c90 64 72 20 2a 73 71 6c 69 74 65 33 4d 65 6d 73 79  dr *sqlite3Memsy
85ca0 73 47 65 74 48 65 61 64 65 72 28 76 6f 69 64 20  sGetHeader(void 
85cb0 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e 29 7b 0a 20  *pAllocation){. 
85cc0 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
85cd0 48 64 72 20 2a 70 3b 0a 20 20 69 6e 74 20 2a 70  Hdr *p;.  int *p
85ce0 49 6e 74 3b 0a 20 20 75 38 20 2a 70 55 38 3b 0a  Int;.  u8 *pU8;.
85cf0 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65 3b 0a    int nReserve;.
85d00 0a 20 20 70 20 3d 20 28 73 74 72 75 63 74 20 4d  .  p = (struct M
85d10 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 70 41 6c 6c  emBlockHdr*)pAll
85d20 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a  ocation;.  p--;.
85d30 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 46 6f    assert( p->iFo
85d40 72 65 47 75 61 72 64 3d 3d 28 69 6e 74 29 46 4f  reGuard==(int)FO
85d50 52 45 47 55 41 52 44 20 29 3b 0a 20 20 6e 52 65  REGUARD );.  nRe
85d60 73 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28 70  serve = ROUND8(p
85d70 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 70 49 6e 74  ->iSize);.  pInt
85d80 20 3d 20 28 69 6e 74 2a 29 70 41 6c 6c 6f 63 61   = (int*)pAlloca
85d90 74 69 6f 6e 3b 0a 20 20 70 55 38 20 3d 20 28 75  tion;.  pU8 = (u
85da0 38 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a  8*)pAllocation;.
85db0 20 20 61 73 73 65 72 74 28 20 70 49 6e 74 5b 6e    assert( pInt[n
85dc0 52 65 73 65 72 76 65 2f 73 69 7a 65 6f 66 28 69  Reserve/sizeof(i
85dd0 6e 74 29 5d 3d 3d 28 69 6e 74 29 52 45 41 52 47  nt)]==(int)REARG
85de0 55 41 52 44 20 29 3b 0a 20 20 2f 2a 20 54 68 69  UARD );.  /* Thi
85df0 73 20 63 68 65 63 6b 73 20 61 6e 79 20 6f 66 20  s checks any of 
85e00 74 68 65 20 22 65 78 74 72 61 22 20 62 79 74 65  the "extra" byte
85e10 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 75 65 0a  s allocated due.
85e20 20 20 2a 2a 20 74 6f 20 72 6f 75 6e 64 69 6e 67    ** to rounding
85e30 20 75 70 20 74 6f 20 61 6e 20 38 20 62 79 74 65   up to an 8 byte
85e40 20 62 6f 75 6e 64 61 72 79 20 74 6f 20 65 6e 73   boundary to ens
85e50 75 72 65 20 0a 20 20 2a 2a 20 74 68 65 79 20 68  ure .  ** they h
85e60 61 76 65 6e 27 74 20 62 65 65 6e 20 6f 76 65 72  aven't been over
85e70 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20  written..  */.  
85e80 77 68 69 6c 65 28 20 6e 52 65 73 65 72 76 65 2d  while( nReserve-
85e90 2d 20 3e 20 70 2d 3e 69 53 69 7a 65 20 29 20 61  - > p->iSize ) a
85ea0 73 73 65 72 74 28 20 70 55 38 5b 6e 52 65 73 65  ssert( pU8[nRese
85eb0 72 76 65 5d 3d 3d 30 78 36 35 20 29 3b 0a 20 20  rve]==0x65 );.  
85ec0 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
85ed0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
85ee0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 75  mber of bytes cu
85ef0 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65  rrently allocate
85f00 64 20 61 74 20 61 64 64 72 65 73 73 20 70 2e 0a  d at address p..
85f10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
85f20 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69  lite3MemSize(voi
85f30 64 20 2a 70 29 7b 0a 20 20 73 74 72 75 63 74 20  d *p){.  struct 
85f40 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64  MemBlockHdr *pHd
85f50 72 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20  r;.  if( !p ){. 
85f60 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
85f70 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69 74 65  .  pHdr = sqlite
85f80 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72  3MemsysGetHeader
85f90 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 48  (p);.  return pH
85fa0 64 72 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a  dr->iSize;.}../*
85fb0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  .** Initialize t
85fc0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
85fd0 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a  tion subsystem..
85fe0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
85ff0 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69  lite3MemInit(voi
86000 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
86010 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
86020 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65  NotUsed);.  asse
86030 72 74 28 20 28 73 69 7a 65 6f 66 28 73 74 72 75  rt( (sizeof(stru
86040 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 29 26  ct MemBlockHdr)&
86050 37 29 20 3d 3d 20 30 20 29 3b 0a 20 20 69 66 28  7) == 0 );.  if(
86060 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   !sqlite3GlobalC
86070 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29  onfig.bMemstat )
86080 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6d 65 6d 6f  {.    /* If memo
86090 72 79 20 73 74 61 74 75 73 20 69 73 20 65 6e 61  ry status is ena
860a0 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6d  bled, then the m
860b0 61 6c 6c 6f 63 2e 63 20 77 72 61 70 70 65 72 20  alloc.c wrapper 
860c0 77 69 6c 6c 20 61 6c 72 65 61 64 79 0a 20 20 20  will already.   
860d0 20 2a 2a 20 68 6f 6c 64 20 74 68 65 20 53 54 41   ** hold the STA
860e0 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 77 68  TIC_MEM mutex wh
860f0 65 6e 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  en the routines 
86100 68 65 72 65 20 61 72 65 20 69 6e 76 6f 6b 65 64  here are invoked
86110 2e 20 2a 2f 0a 20 20 20 20 6d 65 6d 2e 6d 75 74  . */.    mem.mut
86120 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
86130 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
86140 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b  TEX_STATIC_MEM);
86150 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
86160 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
86170 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74  * Deinitialize t
86180 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
86190 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a  tion subsystem..
861a0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
861b0 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77  qlite3MemShutdow
861c0 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  n(void *NotUsed)
861d0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
861e0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
861f0 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 30 3b 0a   mem.mutex = 0;.
86200 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75  }../*.** Round u
86210 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65  p a request size
86220 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c   to the next val
86230 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69  id allocation si
86240 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ze..*/.static in
86250 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e  t sqlite3MemRoun
86260 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65  dup(int n){.  re
86270 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 29 3b 0a  turn ROUND8(n);.
86280 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
86290 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66  e nByte bytes of
862a0 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74   memory..*/.stat
862b0 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  ic void *sqlite3
862c0 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42  MemMalloc(int nB
862d0 79 74 65 29 7b 0a 20 20 73 74 72 75 63 74 20 4d  yte){.  struct M
862e0 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72  emBlockHdr *pHdr
862f0 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a  ;.  void **pBt;.
86300 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74    char *z;.  int
86310 20 2a 70 49 6e 74 3b 0a 20 20 76 6f 69 64 20 2a   *pInt;.  void *
86320 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74 6f 74  p = 0;.  int tot
86330 61 6c 53 69 7a 65 3b 0a 20 20 69 6e 74 20 6e 52  alSize;.  int nR
86340 65 73 65 72 76 65 3b 0a 20 20 73 71 6c 69 74 65  eserve;.  sqlite
86350 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
86360 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65  m.mutex);.  asse
86370 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77  rt( mem.disallow
86380 3d 3d 30 20 29 3b 0a 20 20 6e 52 65 73 65 72 76  ==0 );.  nReserv
86390 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65  e = ROUND8(nByte
863a0 29 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 3d  );.  totalSize =
863b0 20 6e 52 65 73 65 72 76 65 20 2b 20 73 69 7a 65   nReserve + size
863c0 6f 66 28 2a 70 48 64 72 29 20 2b 20 73 69 7a 65  of(*pHdr) + size
863d0 6f 66 28 69 6e 74 29 20 2b 0a 20 20 20 20 20 20  of(int) +.      
863e0 20 20 20 20 20 20 20 20 20 6d 65 6d 2e 6e 42 61           mem.nBa
863f0 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76  cktrace*sizeof(v
86400 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e 6e 54 69 74  oid*) + mem.nTit
86410 6c 65 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63  le;.  p = malloc
86420 28 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 69  (totalSize);.  i
86430 66 28 20 70 20 29 7b 0a 20 20 20 20 7a 20 3d 20  f( p ){.    z = 
86440 70 3b 0a 20 20 20 20 70 42 74 20 3d 20 28 76 6f  p;.    pBt = (vo
86450 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e 54 69 74  id**)&z[mem.nTit
86460 6c 65 5d 3b 0a 20 20 20 20 70 48 64 72 20 3d 20  le];.    pHdr = 
86470 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b  (struct MemBlock
86480 48 64 72 2a 29 26 70 42 74 5b 6d 65 6d 2e 6e 42  Hdr*)&pBt[mem.nB
86490 61 63 6b 74 72 61 63 65 5d 3b 0a 20 20 20 20 70  acktrace];.    p
864a0 48 64 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a  Hdr->pNext = 0;.
864b0 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 20      pHdr->pPrev 
864c0 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b 0a 20 20 20  = mem.pLast;.   
864d0 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73 74 20 29   if( mem.pLast )
864e0 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70 4c 61 73  {.      mem.pLas
864f0 74 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 3b  t->pNext = pHdr;
86500 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
86510 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20 70    mem.pFirst = p
86520 48 64 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  Hdr;.    }.    m
86530 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72 3b  em.pLast = pHdr;
86540 0a 20 20 20 20 70 48 64 72 2d 3e 69 46 6f 72 65  .    pHdr->iFore
86550 47 75 61 72 64 20 3d 20 46 4f 52 45 47 55 41 52  Guard = FOREGUAR
86560 44 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 42 61  D;.    pHdr->nBa
86570 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 3d 20 6d  cktraceSlots = m
86580 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20  em.nBacktrace;. 
86590 20 20 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 20     pHdr->nTitle 
865a0 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20  = mem.nTitle;.  
865b0 20 20 69 66 28 20 6d 65 6d 2e 6e 42 61 63 6b 74    if( mem.nBackt
865c0 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 76 6f  race ){.      vo
865d0 69 64 20 2a 61 41 64 64 72 5b 34 30 5d 3b 0a 20  id *aAddr[40];. 
865e0 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b       pHdr->nBack
865f0 74 72 61 63 65 20 3d 20 62 61 63 6b 74 72 61 63  trace = backtrac
86600 65 28 61 41 64 64 72 2c 20 6d 65 6d 2e 6e 42 61  e(aAddr, mem.nBa
86610 63 6b 74 72 61 63 65 2b 31 29 2d 31 3b 0a 20 20  cktrace+1)-1;.  
86620 20 20 20 20 6d 65 6d 63 70 79 28 70 42 74 2c 20      memcpy(pBt, 
86630 26 61 41 64 64 72 5b 31 5d 2c 20 70 48 64 72 2d  &aAddr[1], pHdr-
86640 3e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65  >nBacktrace*size
86650 6f 66 28 76 6f 69 64 2a 29 29 3b 0a 20 20 20 20  of(void*));.    
86660 20 20 61 73 73 65 72 74 28 70 42 74 5b 30 5d 29    assert(pBt[0])
86670 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 2e  ;.      if( mem.
86680 78 42 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20  xBacktrace ){.  
86690 20 20 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74        mem.xBackt
866a0 72 61 63 65 28 6e 42 79 74 65 2c 20 70 48 64 72  race(nByte, pHdr
866b0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 2c 20  ->nBacktrace-1, 
866c0 26 61 41 64 64 72 5b 31 5d 29 3b 0a 20 20 20 20  &aAddr[1]);.    
866d0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
866e0 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b       pHdr->nBack
866f0 74 72 61 63 65 20 3d 20 30 3b 0a 20 20 20 20 7d  trace = 0;.    }
86700 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 54 69  .    if( mem.nTi
86710 74 6c 65 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  tle ){.      mem
86720 63 70 79 28 7a 2c 20 6d 65 6d 2e 7a 54 69 74 6c  cpy(z, mem.zTitl
86730 65 2c 20 6d 65 6d 2e 6e 54 69 74 6c 65 29 3b 0a  e, mem.nTitle);.
86740 20 20 20 20 7d 0a 20 20 20 20 70 48 64 72 2d 3e      }.    pHdr->
86750 69 53 69 7a 65 20 3d 20 6e 42 79 74 65 3b 0a 20  iSize = nByte;. 
86760 20 20 20 61 64 6a 75 73 74 53 74 61 74 73 28 6e     adjustStats(n
86770 42 79 74 65 2c 20 2b 31 29 3b 0a 20 20 20 20 70  Byte, +1);.    p
86780 49 6e 74 20 3d 20 28 69 6e 74 2a 29 26 70 48 64  Int = (int*)&pHd
86790 72 5b 31 5d 3b 0a 20 20 20 20 70 49 6e 74 5b 6e  r[1];.    pInt[n
867a0 52 65 73 65 72 76 65 2f 73 69 7a 65 6f 66 28 69  Reserve/sizeof(i
867b0 6e 74 29 5d 20 3d 20 52 45 41 52 47 55 41 52 44  nt)] = REARGUARD
867c0 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49 6e  ;.    memset(pIn
867d0 74 2c 20 30 78 36 35 2c 20 6e 52 65 73 65 72 76  t, 0x65, nReserv
867e0 65 29 3b 0a 20 20 20 20 70 20 3d 20 28 76 6f 69  e);.    p = (voi
867f0 64 2a 29 70 49 6e 74 3b 0a 20 20 7d 0a 20 20 73  d*)pInt;.  }.  s
86800 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
86810 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20  ve(mem.mutex);. 
86820 20 72 65 74 75 72 6e 20 70 3b 20 0a 7d 0a 0a 2f   return p; .}../
86830 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79  *.** Free memory
86840 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
86850 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28   sqlite3MemFree(
86860 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20  void *pPrior){. 
86870 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
86880 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69  Hdr *pHdr;.  voi
86890 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72 20  d **pBt;.  char 
868a0 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  *z;.  assert( sq
868b0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
868c0 67 2e 62 4d 65 6d 73 74 61 74 20 7c 7c 20 6d 65  g.bMemstat || me
868d0 6d 2e 6d 75 74 65 78 21 3d 30 20 29 3b 0a 20 20  m.mutex!=0 );.  
868e0 70 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65  pHdr = sqlite3Me
868f0 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50  msysGetHeader(pP
86900 72 69 6f 72 29 3b 0a 20 20 70 42 74 20 3d 20 28  rior);.  pBt = (
86910 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 70  void**)pHdr;.  p
86920 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63  Bt -= pHdr->nBac
86930 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 73  ktraceSlots;.  s
86940 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
86950 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20  er(mem.mutex);. 
86960 20 69 66 28 20 70 48 64 72 2d 3e 70 50 72 65 76   if( pHdr->pPrev
86970 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
86980 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65  pHdr->pPrev->pNe
86990 78 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20  xt==pHdr );.    
869a0 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65  pHdr->pPrev->pNe
869b0 78 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 74  xt = pHdr->pNext
869c0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
869d0 73 73 65 72 74 28 20 6d 65 6d 2e 70 46 69 72 73  ssert( mem.pFirs
869e0 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 6d  t==pHdr );.    m
869f0 65 6d 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72  em.pFirst = pHdr
86a00 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  ->pNext;.  }.  i
86a10 66 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 29  f( pHdr->pNext )
86a20 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48  {.    assert( pH
86a30 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76  dr->pNext->pPrev
86a40 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 70 48  ==pHdr );.    pH
86a50 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76  dr->pNext->pPrev
86a60 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a   = pHdr->pPrev;.
86a70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
86a80 65 72 74 28 20 6d 65 6d 2e 70 4c 61 73 74 3d 3d  ert( mem.pLast==
86a90 70 48 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e  pHdr );.    mem.
86aa0 70 4c 61 73 74 20 3d 20 70 48 64 72 2d 3e 70 50  pLast = pHdr->pP
86ab0 72 65 76 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28  rev;.  }.  z = (
86ac0 63 68 61 72 2a 29 70 42 74 3b 0a 20 20 7a 20 2d  char*)pBt;.  z -
86ad0 3d 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a  = pHdr->nTitle;.
86ae0 20 20 61 64 6a 75 73 74 53 74 61 74 73 28 70 48    adjustStats(pH
86af0 64 72 2d 3e 69 53 69 7a 65 2c 20 2d 31 29 3b 0a  dr->iSize, -1);.
86b00 20 20 6d 65 6d 73 65 74 28 7a 2c 20 30 78 32 62    memset(z, 0x2b
86b10 2c 20 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 2a  , sizeof(void*)*
86b20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
86b30 53 6c 6f 74 73 20 2b 20 73 69 7a 65 6f 66 28 2a  Slots + sizeof(*
86b40 70 48 64 72 29 20 2b 0a 20 20 20 20 20 20 20 20  pHdr) +.        
86b50 20 20 20 20 20 20 20 20 20 20 70 48 64 72 2d 3e            pHdr->
86b60 69 53 69 7a 65 20 2b 20 73 69 7a 65 6f 66 28 69  iSize + sizeof(i
86b70 6e 74 29 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74  nt) + pHdr->nTit
86b80 6c 65 29 3b 0a 20 20 66 72 65 65 28 7a 29 3b 0a  le);.  free(z);.
86b90 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
86ba0 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29  leave(mem.mutex)
86bb0 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  ;  .}../*.** Cha
86bc0 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20  nge the size of 
86bd0 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f  an existing memo
86be0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a  ry allocation..*
86bf0 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 64 65  *.** For this de
86c00 62 75 67 67 69 6e 67 20 69 6d 70 6c 65 6d 65 6e  bugging implemen
86c10 74 61 74 69 6f 6e 2c 20 77 65 20 2a 61 6c 77 61  tation, we *alwa
86c20 79 73 2a 20 6d 61 6b 65 20 61 20 63 6f 70 79 20  ys* make a copy 
86c30 6f 66 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61  of the.** alloca
86c40 74 69 6f 6e 20 69 6e 74 6f 20 61 20 6e 65 77 20  tion into a new 
86c50 70 6c 61 63 65 20 69 6e 20 6d 65 6d 6f 72 79 2e  place in memory.
86c60 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69    In this way, i
86c70 66 20 74 68 65 20 0a 2a 2a 20 68 69 67 68 65 72  f the .** higher
86c80 20 6c 65 76 65 6c 20 63 6f 64 65 20 69 73 20 75   level code is u
86c90 73 69 6e 67 20 70 6f 69 6e 74 65 72 20 74 6f 20  sing pointer to 
86ca0 74 68 65 20 6f 6c 64 20 61 6c 6c 6f 63 61 74 69  the old allocati
86cb0 6f 6e 2c 20 69 74 20 69 73 20 0a 2a 2a 20 6d 75  on, it is .** mu
86cc0 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74  ch more likely t
86cd0 6f 20 62 72 65 61 6b 20 61 6e 64 20 77 65 20 61  o break and we a
86ce0 72 65 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b  re much more lik
86cf0 69 6e 67 20 74 6f 20 66 69 6e 64 0a 2a 2a 20 74  ing to find.** t
86d00 68 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61  he error..*/.sta
86d10 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  tic void *sqlite
86d20 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64  3MemRealloc(void
86d30 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42   *pPrior, int nB
86d40 79 74 65 29 7b 0a 20 20 73 74 72 75 63 74 20 4d  yte){.  struct M
86d50 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4f 6c 64  emBlockHdr *pOld
86d60 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65  Hdr;.  void *pNe
86d70 77 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  w;.  assert( mem
86d80 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a  .disallow==0 );.
86d90 20 20 70 4f 6c 64 48 64 72 20 3d 20 73 71 6c 69    pOldHdr = sqli
86da0 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64  te3MemsysGetHead
86db0 65 72 28 70 50 72 69 6f 72 29 3b 0a 20 20 70 4e  er(pPrior);.  pN
86dc0 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 4d  ew = sqlite3MemM
86dd0 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20  alloc(nByte);.  
86de0 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20  if( pNew ){.    
86df0 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 50 72  memcpy(pNew, pPr
86e00 69 6f 72 2c 20 6e 42 79 74 65 3c 70 4f 6c 64 48  ior, nByte<pOldH
86e10 64 72 2d 3e 69 53 69 7a 65 20 3f 20 6e 42 79 74  dr->iSize ? nByt
86e20 65 20 3a 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69  e : pOldHdr->iSi
86e30 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 6e 42 79  ze);.    if( nBy
86e40 74 65 3e 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a  te>pOldHdr->iSiz
86e50 65 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65  e ){.      memse
86e60 74 28 26 28 28 63 68 61 72 2a 29 70 4e 65 77 29  t(&((char*)pNew)
86e70 5b 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 5d  [pOldHdr->iSize]
86e80 2c 20 30 78 32 62 2c 20 6e 42 79 74 65 20 2d 20  , 0x2b, nByte - 
86e90 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b  pOldHdr->iSize);
86ea0 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
86eb0 65 33 4d 65 6d 46 72 65 65 28 70 50 72 69 6f 72  e3MemFree(pPrior
86ec0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
86ed0 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  pNew;.}../*.** P
86ee0 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d  opulate the low-
86ef0 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
86f00 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ocation function
86f10 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20   pointers in.** 
86f20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
86f30 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74  fig.m with point
86f40 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69  ers to the routi
86f50 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  nes in this file
86f60 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
86f70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
86f80 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f  MemSetDefault(vo
86f90 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  id){.  static co
86fa0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
86fb0 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d  methods defaultM
86fc0 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20  ethods = {.     
86fd0 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
86fe0 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
86ff0 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 6c 69  mFree,.     sqli
87000 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20  te3MemRealloc,. 
87010 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69      sqlite3MemSi
87020 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  ze,.     sqlite3
87030 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20  MemRoundup,.    
87040 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c   sqlite3MemInit,
87050 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
87060 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30  Shutdown,.     0
87070 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f  .  };.  sqlite3_
87080 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f  config(SQLITE_CO
87090 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65  NFIG_MALLOC, &de
870a0 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d  faultMethods);.}
870b0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
870c0 6e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 74 72  number of backtr
870d0 61 63 65 20 6c 65 76 65 6c 73 20 6b 65 70 74 20  ace levels kept 
870e0 66 6f 72 20 65 61 63 68 20 61 6c 6c 6f 63 61 74  for each allocat
870f0 69 6f 6e 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20  ion..** A value 
87100 6f 66 20 7a 65 72 6f 20 74 75 72 6e 73 20 6f 66  of zero turns of
87110 66 20 62 61 63 6b 74 72 61 63 69 6e 67 2e 20 20  f backtracing.  
87120 54 68 65 20 6e 75 6d 62 65 72 20 69 73 20 61 6c  The number is al
87130 77 61 79 73 20 72 6f 75 6e 64 65 64 0a 2a 2a 20  ways rounded.** 
87140 75 70 20 74 6f 20 61 20 6d 75 6c 74 69 70 6c 65  up to a multiple
87150 20 6f 66 20 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45   of 2..*/.SQLITE
87160 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
87170 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 42 61 63  lite3MemdebugBac
87180 6b 74 72 61 63 65 28 69 6e 74 20 64 65 70 74 68  ktrace(int depth
87190 29 7b 0a 20 20 69 66 28 20 64 65 70 74 68 3c 30  ){.  if( depth<0
871a0 20 29 7b 20 64 65 70 74 68 20 3d 20 30 3b 20 7d   ){ depth = 0; }
871b0 0a 20 20 69 66 28 20 64 65 70 74 68 3e 32 30 20  .  if( depth>20 
871c0 29 7b 20 64 65 70 74 68 20 3d 20 32 30 3b 20 7d  ){ depth = 20; }
871d0 0a 20 20 64 65 70 74 68 20 3d 20 28 64 65 70 74  .  depth = (dept
871e0 68 2b 31 29 26 30 78 66 65 3b 0a 20 20 6d 65 6d  h+1)&0xfe;.  mem
871f0 2e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 64 65  .nBacktrace = de
87200 70 74 68 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50  pth;.}..SQLITE_P
87210 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
87220 74 65 33 4d 65 6d 64 65 62 75 67 42 61 63 6b 74  te3MemdebugBackt
87230 72 61 63 65 43 61 6c 6c 62 61 63 6b 28 76 6f 69  raceCallback(voi
87240 64 20 28 2a 78 42 61 63 6b 74 72 61 63 65 29 28  d (*xBacktrace)(
87250 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 2a  int, int, void *
87260 2a 29 29 7b 0a 20 20 6d 65 6d 2e 78 42 61 63 6b  *)){.  mem.xBack
87270 74 72 61 63 65 20 3d 20 78 42 61 63 6b 74 72 61  trace = xBacktra
87280 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  ce;.}../*.** Set
87290 20 74 68 65 20 74 69 74 6c 65 20 73 74 72 69 6e   the title strin
872a0 67 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74  g for subsequent
872b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f   allocations..*/
872c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
872d0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64  void sqlite3Memd
872e0 65 62 75 67 53 65 74 74 69 74 6c 65 28 63 6f 6e  ebugSettitle(con
872f0 73 74 20 63 68 61 72 20 2a 7a 54 69 74 6c 65 29  st char *zTitle)
87300 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  {.  unsigned int
87310 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
87320 65 6e 33 30 28 7a 54 69 74 6c 65 29 20 2b 20 31  en30(zTitle) + 1
87330 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
87340 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65  x_enter(mem.mute
87350 78 29 3b 0a 20 20 69 66 28 20 6e 3e 3d 73 69 7a  x);.  if( n>=siz
87360 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 20  eof(mem.zTitle) 
87370 29 20 6e 20 3d 20 73 69 7a 65 6f 66 28 6d 65 6d  ) n = sizeof(mem
87380 2e 7a 54 69 74 6c 65 29 2d 31 3b 0a 20 20 6d 65  .zTitle)-1;.  me
87390 6d 63 70 79 28 6d 65 6d 2e 7a 54 69 74 6c 65 2c  mcpy(mem.zTitle,
873a0 20 7a 54 69 74 6c 65 2c 20 6e 29 3b 0a 20 20 6d   zTitle, n);.  m
873b0 65 6d 2e 7a 54 69 74 6c 65 5b 6e 5d 20 3d 20 30  em.zTitle[n] = 0
873c0 3b 0a 20 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 3d  ;.  mem.nTitle =
873d0 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 20 20 73 71   ROUND8(n);.  sq
873e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
873f0 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 7d 0a  e(mem.mutex);.}.
87400 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
87410 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64  void sqlite3Memd
87420 65 62 75 67 53 79 6e 63 28 29 7b 0a 20 20 73 74  ebugSync(){.  st
87430 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  ruct MemBlockHdr
87440 20 2a 70 48 64 72 3b 0a 20 20 66 6f 72 28 70 48   *pHdr;.  for(pH
87450 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70  dr=mem.pFirst; p
87460 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e  Hdr; pHdr=pHdr->
87470 70 4e 65 78 74 29 7b 0a 20 20 20 20 76 6f 69 64  pNext){.    void
87480 20 2a 2a 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a   **pBt = (void**
87490 29 70 48 64 72 3b 0a 20 20 20 20 70 42 74 20 2d  )pHdr;.    pBt -
874a0 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61  = pHdr->nBacktra
874b0 63 65 53 6c 6f 74 73 3b 0a 20 20 20 20 6d 65 6d  ceSlots;.    mem
874c0 2e 78 42 61 63 6b 74 72 61 63 65 28 70 48 64 72  .xBacktrace(pHdr
874d0 2d 3e 69 53 69 7a 65 2c 20 70 48 64 72 2d 3e 6e  ->iSize, pHdr->n
874e0 42 61 63 6b 74 72 61 63 65 2d 31 2c 20 26 70 42  Backtrace-1, &pB
874f0 74 5b 31 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  t[1]);.  }.}../*
87500 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c  .** Open the fil
87510 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20  e indicated and 
87520 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61  write a log of a
87530 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72  ll unfreed memor
87540 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  y .** allocation
87550 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e  s into that log.
87560 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
87570 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
87580 65 6d 64 65 62 75 67 44 75 6d 70 28 63 6f 6e 73  emdebugDump(cons
87590 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
875a0 65 29 7b 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b  e){.  FILE *out;
875b0 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
875c0 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76  ckHdr *pHdr;.  v
875d0 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 69 6e 74  oid **pBt;.  int
875e0 20 69 3b 0a 20 20 6f 75 74 20 3d 20 66 6f 70 65   i;.  out = fope
875f0 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22  n(zFilename, "w"
87600 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 30 20  );.  if( out==0 
87610 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73  ){.    fprintf(s
87620 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c  tderr, "** Unabl
87630 65 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f  e to output memo
87640 72 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20  ry debug output 
87650 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20  log: %s **\n",. 
87660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
87670 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20     zFilename);. 
87680 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
87690 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46   for(pHdr=mem.pF
876a0 69 72 73 74 3b 20 70 48 64 72 3b 20 70 48 64 72  irst; pHdr; pHdr
876b0 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pHdr->pNext){. 
876c0 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 68     char *z = (ch
876d0 61 72 2a 29 70 48 64 72 3b 0a 20 20 20 20 7a 20  ar*)pHdr;.    z 
876e0 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72  -= pHdr->nBacktr
876f0 61 63 65 53 6c 6f 74 73 2a 73 69 7a 65 6f 66 28  aceSlots*sizeof(
87700 76 6f 69 64 2a 29 20 2b 20 70 48 64 72 2d 3e 6e  void*) + pHdr->n
87710 54 69 74 6c 65 3b 0a 20 20 20 20 66 70 72 69 6e  Title;.    fprin
87720 74 66 28 6f 75 74 2c 20 22 2a 2a 2a 2a 20 25 6c  tf(out, "**** %l
87730 6c 64 20 62 79 74 65 73 20 61 74 20 25 70 20 66  ld bytes at %p f
87740 72 6f 6d 20 25 73 20 2a 2a 2a 2a 5c 6e 22 2c 20  rom %s ****\n", 
87750 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 48 64  .            pHd
87760 72 2d 3e 69 53 69 7a 65 2c 20 26 70 48 64 72 5b  r->iSize, &pHdr[
87770 31 5d 2c 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65  1], pHdr->nTitle
87780 20 3f 20 7a 20 3a 20 22 3f 3f 3f 22 29 3b 0a 20   ? z : "???");. 
87790 20 20 20 69 66 28 20 70 48 64 72 2d 3e 6e 42 61     if( pHdr->nBa
877a0 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20  cktrace ){.     
877b0 20 66 66 6c 75 73 68 28 6f 75 74 29 3b 0a 20 20   fflush(out);.  
877c0 20 20 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a      pBt = (void*
877d0 2a 29 70 48 64 72 3b 0a 20 20 20 20 20 20 70 42  *)pHdr;.      pB
877e0 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b  t -= pHdr->nBack
877f0 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 20 20  traceSlots;.    
87800 20 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62    backtrace_symb
87810 6f 6c 73 5f 66 64 28 70 42 74 2c 20 70 48 64 72  ols_fd(pBt, pHdr
87820 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2c 20 66 69  ->nBacktrace, fi
87830 6c 65 6e 6f 28 6f 75 74 29 29 3b 0a 20 20 20 20  leno(out));.    
87840 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
87850 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  \n");.    }.  }.
87860 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
87870 43 4f 55 4e 54 53 3a 5c 6e 22 29 3b 0a 20 20 66  COUNTS:\n");.  f
87880 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45  or(i=0; i<NCSIZE
87890 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  -1; i++){.    if
878a0 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 20  ( mem.nAlloc[i] 
878b0 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66  ){.      fprintf
878c0 28 6f 75 74 2c 20 22 20 20 20 25 35 64 3a 20 25  (out, "   %5d: %
878d0 31 30 64 20 25 31 30 64 20 25 31 30 64 5c 6e 22  10d %10d %10d\n"
878e0 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  , .            i
878f0 2a 38 2c 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69  *8, mem.nAlloc[i
87900 5d 2c 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b  ], mem.nCurrent[
87910 69 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e  i], mem.mxCurren
87920 74 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  t[i]);.    }.  }
87930 0a 20 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f  .  if( mem.nAllo
87940 63 5b 4e 43 53 49 5a 45 2d 31 5d 20 29 7b 0a 20  c[NCSIZE-1] ){. 
87950 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
87960 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 31  "   %5d: %10d %1
87970 30 64 20 25 31 30 64 5c 6e 22 2c 0a 20 20 20 20  0d %10d\n",.    
87980 20 20 20 20 20 20 20 20 20 4e 43 53 49 5a 45 2a           NCSIZE*
87990 38 2d 38 2c 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b  8-8, mem.nAlloc[
879a0 4e 43 53 49 5a 45 2d 31 5d 2c 0a 20 20 20 20 20  NCSIZE-1],.     
879b0 20 20 20 20 20 20 20 20 6d 65 6d 2e 6e 43 75 72          mem.nCur
879c0 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d 2c 20  rent[NCSIZE-1], 
879d0 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 4e 43  mem.mxCurrent[NC
879e0 53 49 5a 45 2d 31 5d 29 3b 0a 20 20 7d 0a 20 20  SIZE-1]);.  }.  
879f0 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 7d 0a 0a  fclose(out);.}..
87a00 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
87a10 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
87a20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f   sqlite3MemMallo
87a30 63 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c  c() has been cal
87a40 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  led..*/.SQLITE_P
87a50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
87a60 65 33 4d 65 6d 64 65 62 75 67 4d 61 6c 6c 6f 63  e3MemdebugMalloc
87a70 43 6f 75 6e 74 28 29 7b 0a 20 20 69 6e 74 20 69  Count(){.  int i
87a80 3b 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c 20 3d  ;.  int nTotal =
87a90 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
87aa0 3c 4e 43 53 49 5a 45 3b 20 69 2b 2b 29 7b 0a 20  <NCSIZE; i++){. 
87ab0 20 20 20 6e 54 6f 74 61 6c 20 2b 3d 20 6d 65 6d     nTotal += mem
87ac0 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b 0a 20 20 7d 0a  .nAlloc[i];.  }.
87ad0 20 20 72 65 74 75 72 6e 20 6e 54 6f 74 61 6c 3b    return nTotal;
87ae0 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .}...#endif /* S
87af0 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 2a  QLITE_MEMDEBUG *
87b00 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
87b10 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 32 2e 63  ** End of mem2.c
87b20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
87b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87b50 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
87b60 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65  ** Begin file me
87b70 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m3.c ***********
87b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87ba0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63  */./*.** 2007 Oc
87bb0 74 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54  tober 14.**.** T
87bc0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
87bd0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
87be0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
87bf0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
87c00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
87c10 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
87c20 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
87c30 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
87c40 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
87c50 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
87c60 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
87c70 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
87c80 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
87c90 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
87ca0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
87cb0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
87cc0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
87cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87d10 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
87d20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
87d30 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  e C functions th
87d40 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d  at implement a m
87d50 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
87d60 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f  ion subsystem fo
87d70 72 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e  r use by SQLite.
87d80 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72   .**.** This ver
87d90 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
87da0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
87db0 62 73 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c  bsystem omits al
87dc0 6c 0a 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c  l.** use of mall
87dd0 6f 63 28 29 2e 20 54 68 65 20 53 51 4c 69 74 65  oc(). The SQLite
87de0 20 75 73 65 72 20 73 75 70 70 6c 69 65 73 20 61   user supplies a
87df0 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79   block of memory
87e00 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  .** before calli
87e10 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ng sqlite3_initi
87e20 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20 77 68 69  alize() from whi
87e30 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a  ch allocations.*
87e40 2a 20 61 72 65 20 6d 61 64 65 20 61 6e 64 20 72  * are made and r
87e50 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78  eturned by the x
87e60 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 78 52 65  Malloc() and xRe
87e70 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69 6d 70 6c  alloc() .** impl
87e80 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 4f 6e 63  ementations. Onc
87e90 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  e sqlite3_initia
87ea0 6c 69 7a 65 28 29 20 68 61 73 20 62 65 65 6e 20  lize() has been 
87eb0 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 20 61  called,.** the a
87ec0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
87ed0 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 53 51 4c  available to SQL
87ee0 69 74 65 20 69 73 20 66 69 78 65 64 20 61 6e 64  ite is fixed and
87ef0 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 63 68   cannot.** be ch
87f00 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  anged..**.** Thi
87f10 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  s version of the
87f20 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
87f30 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 69 73 20  on subsystem is 
87f40 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e 20 74  included.** in t
87f50 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79 20 69 66  he build only if
87f60 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
87f70 45 4d 53 59 53 33 20 69 73 20 64 65 66 69 6e 65  EMSYS3 is define
87f80 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65  d..**.** $Id: me
87f90 6d 33 2e 63 2c 76 20 31 2e 32 35 20 32 30 30 38  m3.c,v 1.25 2008
87fa0 2f 31 31 2f 31 39 20 31 36 3a 35 32 3a 34 34 20  /11/19 16:52:44 
87fb0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
87fc0 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  $.*/../*.** This
87fd0 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
87fe0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
87ff0 20 69 73 20 6f 6e 6c 79 20 62 75 69 6c 74 20 69   is only built i
88000 6e 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 0a  nto the library.
88010 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ** SQLITE_ENABLE
88020 5f 4d 45 4d 53 59 53 33 20 69 73 20 64 65 66 69  _MEMSYS3 is defi
88030 6e 65 64 2e 20 44 65 66 69 6e 69 6e 67 20 74 68  ned. Defining th
88040 69 73 20 73 79 6d 62 6f 6c 20 64 6f 65 73 20 6e  is symbol does n
88050 6f 74 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20  ot.** mean that 
88060 74 68 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c  the library will
88070 20 75 73 65 20 61 20 6d 65 6d 6f 72 79 2d 70 6f   use a memory-po
88080 6f 6c 20 62 79 20 64 65 66 61 75 6c 74 2c 20 6a  ol by default, j
88090 75 73 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69  ust that.** it i
880a0 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 54 68 65  s available. The
880b0 20 6d 65 6d 70 6f 6f 6c 20 61 6c 6c 6f 63 61 74   mempool allocat
880c0 6f 72 20 69 73 20 61 63 74 69 76 61 74 65 64 20  or is activated 
880d0 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71  by calling.** sq
880e0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 0a  lite3_config()..
880f0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
88100 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 0a  _ENABLE_MEMSYS3.
88110 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73  ./*.** Maximum s
88120 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63  ize (in Mem3Bloc
88130 6b 73 29 20 6f 66 20 61 20 22 73 6d 61 6c 6c 22  ks) of a "small"
88140 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23 64 65 66 69   chunk..*/.#defi
88150 6e 65 20 4d 58 5f 53 4d 41 4c 4c 20 31 30 0a 0a  ne MX_SMALL 10..
88160 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66  ./*.** Number of
88170 20 66 72 65 65 6c 69 73 74 20 68 61 73 68 20 73   freelist hash s
88180 6c 6f 74 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  lots.*/.#define 
88190 4e 5f 48 41 53 48 20 20 36 31 0a 0a 2f 2a 0a 2a  N_HASH  61../*.*
881a0 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  * A memory alloc
881b0 61 74 69 6f 6e 20 28 61 6c 73 6f 20 63 61 6c 6c  ation (also call
881c0 65 64 20 61 20 22 63 68 75 6e 6b 22 29 20 63 6f  ed a "chunk") co
881d0 6e 73 69 73 74 73 20 6f 66 20 74 77 6f 20 6f 72  nsists of two or
881e0 20 0a 2a 2a 20 6d 6f 72 65 20 62 6c 6f 63 6b 73   .** more blocks
881f0 20 77 68 65 72 65 20 65 61 63 68 20 62 6c 6f 63   where each bloc
88200 6b 20 69 73 20 38 20 62 79 74 65 73 2e 20 20 54  k is 8 bytes.  T
88210 68 65 20 66 69 72 73 74 20 38 20 62 79 74 65 73  he first 8 bytes
88220 20 61 72 65 20 0a 2a 2a 20 61 20 68 65 61 64 65   are .** a heade
88230 72 20 74 68 61 74 20 69 73 20 6e 6f 74 20 72 65  r that is not re
88240 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73  turned to the us
88250 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 68 75 6e  er..**.** A chun
88260 6b 20 69 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65  k is two or more
88270 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 69 73 20   blocks that is 
88280 65 69 74 68 65 72 20 63 68 65 63 6b 65 64 20 6f  either checked o
88290 75 74 20 6f 72 0a 2a 2a 20 66 72 65 65 2e 20 20  ut or.** free.  
882a0 54 68 65 20 66 69 72 73 74 20 62 6c 6f 63 6b 20  The first block 
882b0 68 61 73 20 66 6f 72 6d 61 74 20 75 2e 68 64 72  has format u.hdr
882c0 2e 20 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  .  u.hdr.size4x 
882d0 69 73 20 34 20 74 69 6d 65 73 20 74 68 65 0a 2a  is 4 times the.*
882e0 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c  * size of the al
882f0 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62 6c 6f 63  location in bloc
88300 6b 73 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 61  ks if the alloca
88310 74 69 6f 6e 20 69 73 20 66 72 65 65 2e 0a 2a 2a  tion is free..**
88320 20 54 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34   The u.hdr.size4
88330 78 26 31 20 62 69 74 20 69 73 20 74 72 75 65 20  x&1 bit is true 
88340 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20  if the chunk is 
88350 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 0a  checked out and.
88360 2a 2a 20 66 61 6c 73 65 20 69 66 20 74 68 65 20  ** false if the 
88370 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65 20  chunk is on the 
88380 66 72 65 65 6c 69 73 74 2e 20 20 54 68 65 20 75  freelist.  The u
88390 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 20 62 69  .hdr.size4x&2 bi
883a0 74 0a 2a 2a 20 69 73 20 74 72 75 65 20 69 66 20  t.** is true if 
883b0 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75  the previous chu
883c0 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75  nk is checked ou
883d0 74 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 74  t and false if t
883e0 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 20 63  he.** previous c
883f0 68 75 6e 6b 20 69 73 20 66 72 65 65 2e 20 20 54  hunk is free.  T
88400 68 65 20 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  he u.hdr.prevSiz
88410 65 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 73  e field is the s
88420 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72  ize of.** the pr
88430 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 6e 20  evious chunk in 
88440 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 70 72  blocks if the pr
88450 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20  evious chunk is 
88460 6f 6e 20 74 68 65 0a 2a 2a 20 66 72 65 65 6c 69  on the.** freeli
88470 73 74 2e 20 49 66 20 74 68 65 20 70 72 65 76 69  st. If the previ
88480 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 63 68 65  ous chunk is che
88490 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 6e 0a 2a  cked out, then.*
884a0 2a 20 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  * u.hdr.prevSize
884b0 20 63 61 6e 20 62 65 20 70 61 72 74 20 6f 66 20   can be part of 
884c0 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 61  the data for tha
884d0 74 20 63 68 75 6e 6b 20 61 6e 64 20 73 68 6f 75  t chunk and shou
884e0 6c 64 0a 2a 2a 20 6e 6f 74 20 62 65 20 72 65 61  ld.** not be rea
884f0 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 0a 2a 2a  d or written..**
88500 0a 2a 2a 20 57 65 20 6f 66 74 65 6e 20 69 64 65  .** We often ide
88510 6e 74 69 66 79 20 61 20 63 68 75 6e 6b 20 62 79  ntify a chunk by
88520 20 69 74 73 20 69 6e 64 65 78 20 69 6e 20 6d 65   its index in me
88530 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e 20 20 57 68 65  m3.aPool[].  Whe
88540 6e 0a 2a 2a 20 74 68 69 73 20 69 73 20 64 6f 6e  n.** this is don
88550 65 2c 20 74 68 65 20 63 68 75 6e 6b 20 69 6e 64  e, the chunk ind
88560 65 78 20 72 65 66 65 72 73 20 74 6f 20 74 68 65  ex refers to the
88570 20 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66   second block of
88580 0a 2a 2a 20 74 68 65 20 63 68 75 6e 6b 2e 20 20  .** the chunk.  
88590 49 6e 20 74 68 69 73 20 77 61 79 2c 20 74 68 65  In this way, the
885a0 20 66 69 72 73 74 20 63 68 75 6e 6b 20 68 61 73   first chunk has
885b0 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 0a   an index of 1..
885c0 2a 2a 20 41 20 63 68 75 6e 6b 20 69 6e 64 65 78  ** A chunk index
885d0 20 6f 66 20 30 20 6d 65 61 6e 73 20 22 6e 6f 20   of 0 means "no 
885e0 73 75 63 68 20 63 68 75 6e 6b 22 20 61 6e 64 20  such chunk" and 
885f0 69 73 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e  is the equivalen
88600 74 0a 2a 2a 20 6f 66 20 61 20 4e 55 4c 4c 20 70  t.** of a NULL p
88610 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ointer..**.** Th
88620 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f  e second block o
88630 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20 69 73  f free chunks is
88640 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 75 2e 6c   of the form u.l
88650 69 73 74 2e 20 20 54 68 65 0a 2a 2a 20 74 77 6f  ist.  The.** two
88660 20 66 69 65 6c 64 73 20 66 6f 72 6d 20 61 20 64   fields form a d
88670 6f 75 62 6c 65 2d 6c 69 6e 6b 65 64 20 6c 69 73  ouble-linked lis
88680 74 20 6f 66 20 63 68 75 6e 6b 73 20 6f 66 20 72  t of chunks of r
88690 65 6c 61 74 65 64 20 73 69 7a 65 73 2e 0a 2a 2a  elated sizes..**
886a0 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   Pointers to the
886b0 20 68 65 61 64 20 6f 66 20 74 68 65 20 6c 69 73   head of the lis
886c0 74 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  t are stored in 
886d0 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 0a  mem3.aiSmall[] .
886e0 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c 65 72 20 63  ** for smaller c
886f0 68 75 6e 6b 73 20 61 6e 64 20 6d 65 6d 33 2e 61  hunks and mem3.a
88700 69 48 61 73 68 5b 5d 20 66 6f 72 20 6c 61 72 67  iHash[] for larg
88710 65 72 20 63 68 75 6e 6b 73 2e 0a 2a 2a 0a 2a 2a  er chunks..**.**
88720 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63   The second bloc
88730 6b 20 6f 66 20 61 20 63 68 75 6e 6b 20 69 73 20  k of a chunk is 
88740 75 73 65 72 20 64 61 74 61 20 69 66 20 74 68 65  user data if the
88750 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
88760 64 20 0a 2a 2a 20 6f 75 74 2e 20 20 49 66 20 61  d .** out.  If a
88770 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
88780 64 20 6f 75 74 2c 20 74 68 65 20 75 73 65 72 20  d out, the user 
88790 64 61 74 61 20 6d 61 79 20 65 78 74 65 6e 64 20  data may extend 
887a0 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 75 2e 68 64  into.** the u.hd
887b0 72 2e 70 72 65 76 53 69 7a 65 20 76 61 6c 75 65  r.prevSize value
887c0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
887d0 67 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 74 79 70 65  g chunk..*/.type
887e0 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 33 42  def struct Mem3B
887f0 6c 6f 63 6b 20 4d 65 6d 33 42 6c 6f 63 6b 3b 0a  lock Mem3Block;.
88800 73 74 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b  struct Mem3Block
88810 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20   {.  union {.   
88820 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20   struct {.      
88830 75 33 32 20 70 72 65 76 53 69 7a 65 3b 20 20 20  u32 prevSize;   
88840 2f 2a 20 53 69 7a 65 20 6f 66 20 70 72 65 76 69  /* Size of previ
88850 6f 75 73 20 63 68 75 6e 6b 20 69 6e 20 4d 65 6d  ous chunk in Mem
88860 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e 74 73 20  3Block elements 
88870 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 73 69 7a  */.      u32 siz
88880 65 34 78 3b 20 20 20 20 20 2f 2a 20 34 78 20 74  e4x;     /* 4x t
88890 68 65 20 73 69 7a 65 20 6f 66 20 63 75 72 72 65  he size of curre
888a0 6e 74 20 63 68 75 6e 6b 20 69 6e 20 4d 65 6d 33  nt chunk in Mem3
888b0 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e 74 73 20 2a  Block elements *
888c0 2f 0a 20 20 20 20 7d 20 68 64 72 3b 0a 20 20 20  /.    } hdr;.   
888d0 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20   struct {.      
888e0 75 33 32 20 6e 65 78 74 3b 20 20 20 20 20 20 20  u32 next;       
888f0 2f 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33  /* Index in mem3
88900 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 6e 65 78 74  .aPool[] of next
88910 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20   free chunk */. 
88920 20 20 20 20 20 75 33 32 20 70 72 65 76 3b 20 20       u32 prev;  
88930 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
88940 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66   mem3.aPool[] of
88950 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 63   previous free c
88960 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 7d 20 6c 69  hunk */.    } li
88970 73 74 3b 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f  st;.  } u;.};../
88980 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20  *.** All of the 
88990 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73  static variables
889a0 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f   used by this mo
889b0 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74  dule are collect
889c0 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e  ed.** into a sin
889d0 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61  gle structure na
889e0 6d 65 64 20 22 6d 65 6d 33 22 2e 20 20 54 68 69  med "mem3".  Thi
889f0 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65  s is to keep the
88a00 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61  .** static varia
88a10 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61  bles organized a
88a20 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d  nd to reduce nam
88a30 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e  espace pollution
88a40 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f  .** when this mo
88a50 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64  dule is combined
88a60 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74   with other in t
88a70 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e  he amalgamation.
88a80 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54  .*/.static SQLIT
88a90 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d  E_WSD struct Mem
88aa0 33 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20  3Global {.  /*. 
88ab0 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c   ** Memory avail
88ac0 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74  able for allocat
88ad0 69 6f 6e 2e 20 6e 50 6f 6f 6c 20 69 73 20 74 68  ion. nPool is th
88ae0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72  e size of the ar
88af0 72 61 79 0a 20 20 2a 2a 20 28 69 6e 20 4d 65 6d  ray.  ** (in Mem
88b00 33 42 6c 6f 63 6b 73 29 20 70 6f 69 6e 74 65 64  3Blocks) pointed
88b10 20 74 6f 20 62 79 20 61 50 6f 6f 6c 20 6c 65 73   to by aPool les
88b20 73 20 32 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20  s 2..  */.  u32 
88b30 6e 50 6f 6f 6c 3b 0a 20 20 4d 65 6d 33 42 6c 6f  nPool;.  Mem3Blo
88b40 63 6b 20 2a 61 50 6f 6f 6c 3b 0a 0a 20 20 2f 2a  ck *aPool;..  /*
88b50 0a 20 20 2a 2a 20 54 72 75 65 20 69 66 20 77 65  .  ** True if we
88b60 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20   are evaluating 
88b70 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79  an out-of-memory
88b80 20 63 61 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2f 0a   callback..  */.
88b90 20 20 69 6e 74 20 61 6c 61 72 6d 42 75 73 79 3b    int alarmBusy;
88ba0 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75  .  .  /*.  ** Mu
88bb0 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61  tex to control a
88bc0 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d  ccess to the mem
88bd0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
88be0 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20  ubsystem..  */. 
88bf0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
88c00 6d 75 74 65 78 3b 0a 20 20 0a 20 20 2f 2a 0a 20  mutex;.  .  /*. 
88c10 20 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20   ** The minimum 
88c20 61 6d 6f 75 6e 74 20 6f 66 20 66 72 65 65 20 73  amount of free s
88c30 70 61 63 65 20 74 68 61 74 20 77 65 20 68 61 76  pace that we hav
88c40 65 20 73 65 65 6e 2e 0a 20 20 2a 2f 0a 20 20 75  e seen..  */.  u
88c50 33 32 20 6d 6e 4d 61 73 74 65 72 3b 0a 0a 20 20  32 mnMaster;..  
88c60 2f 2a 0a 20 20 2a 2a 20 69 4d 61 73 74 65 72 20  /*.  ** iMaster 
88c70 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
88c80 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b  the master chunk
88c90 2e 20 20 4d 6f 73 74 20 6e 65 77 20 61 6c 6c 6f  .  Most new allo
88ca0 63 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 6f 63 63  cations.  ** occ
88cb0 75 72 20 6f 66 66 20 6f 66 20 74 68 69 73 20 63  ur off of this c
88cc0 68 75 6e 6b 2e 20 20 73 7a 4d 61 73 74 65 72 20  hunk.  szMaster 
88cd0 69 73 20 74 68 65 20 73 69 7a 65 20 28 69 6e 20  is the size (in 
88ce0 4d 65 6d 33 42 6c 6f 63 6b 73 29 0a 20 20 2a 2a  Mem3Blocks).  **
88cf0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
88d00 6d 61 73 74 65 72 2e 20 20 69 4d 61 73 74 65 72  master.  iMaster
88d10 20 69 73 20 30 20 69 66 20 74 68 65 72 65 20 69   is 0 if there i
88d20 73 20 6e 6f 74 20 6d 61 73 74 65 72 20 63 68 75  s not master chu
88d30 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 20 6d 61 73  nk..  ** The mas
88d40 74 65 72 20 63 68 75 6e 6b 20 69 73 20 6e 6f 74  ter chunk is not
88d50 20 69 6e 20 65 69 74 68 65 72 20 74 68 65 20 61   in either the a
88d60 69 48 61 73 68 5b 5d 20 6f 72 20 61 69 53 6d 61  iHash[] or aiSma
88d70 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a 20 20 75 33 32  ll[]..  */.  u32
88d80 20 69 4d 61 73 74 65 72 3b 0a 20 20 75 33 32 20   iMaster;.  u32 
88d90 73 7a 4d 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a  szMaster;..  /*.
88da0 20 20 2a 2a 20 41 72 72 61 79 20 6f 66 20 6c 69    ** Array of li
88db0 73 74 73 20 6f 66 20 66 72 65 65 20 62 6c 6f 63  sts of free bloc
88dc0 6b 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  ks according to 
88dd0 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 0a  the block size .
88de0 20 20 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c 65 72    ** for smaller
88df0 20 63 68 75 6e 6b 73 2c 20 6f 72 20 61 20 68 61   chunks, or a ha
88e00 73 68 20 6f 6e 20 74 68 65 20 62 6c 6f 63 6b 20  sh on the block 
88e10 73 69 7a 65 20 66 6f 72 20 6c 61 72 67 65 72 0a  size for larger.
88e20 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 0a 20 20 2a    ** chunks..  *
88e30 2f 0a 20 20 75 33 32 20 61 69 53 6d 61 6c 6c 5b  /.  u32 aiSmall[
88e40 4d 58 5f 53 4d 41 4c 4c 2d 31 5d 3b 20 20 20 2f  MX_SMALL-1];   /
88e50 2a 20 46 6f 72 20 73 69 7a 65 73 20 32 20 74 68  * For sizes 2 th
88e60 72 6f 75 67 68 20 4d 58 5f 53 4d 41 4c 4c 2c 20  rough MX_SMALL, 
88e70 69 6e 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 75  inclusive */.  u
88e80 33 32 20 61 69 48 61 73 68 5b 4e 5f 48 41 53 48  32 aiHash[N_HASH
88e90 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72  ];        /* For
88ea0 20 73 69 7a 65 73 20 4d 58 5f 53 4d 41 4c 4c 2b   sizes MX_SMALL+
88eb0 31 20 61 6e 64 20 6c 61 72 67 65 72 20 2a 2f 0a  1 and larger */.
88ec0 7d 20 6d 65 6d 33 20 3d 20 7b 20 39 37 35 33 35  } mem3 = { 97535
88ed0 35 37 35 20 7d 3b 0a 0a 23 64 65 66 69 6e 65 20  575 };..#define 
88ee0 6d 65 6d 33 20 47 4c 4f 42 41 4c 28 73 74 72 75  mem3 GLOBAL(stru
88ef0 63 74 20 4d 65 6d 33 47 6c 6f 62 61 6c 2c 20 6d  ct Mem3Global, m
88f00 65 6d 33 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69  em3)../*.** Unli
88f10 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20  nk the chunk at 
88f20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72  mem3.aPool[i] fr
88f30 6f 6d 20 6c 69 73 74 20 69 74 20 69 73 20 63 75  om list it is cu
88f40 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20  rrently.** on.  
88f50 2a 70 52 6f 6f 74 20 69 73 20 74 68 65 20 6c 69  *pRoot is the li
88f60 73 74 20 74 68 61 74 20 69 20 69 73 20 61 20 6d  st that i is a m
88f70 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61  ember of..*/.sta
88f80 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
88f90 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 75  UnlinkFromList(u
88fa0 33 32 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74  32 i, u32 *pRoot
88fb0 29 7b 0a 20 20 75 33 32 20 6e 65 78 74 20 3d 20  ){.  u32 next = 
88fc0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
88fd0 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 20 75 33 32  list.next;.  u32
88fe0 20 70 72 65 76 20 3d 20 6d 65 6d 33 2e 61 50 6f   prev = mem3.aPo
88ff0 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65  ol[i].u.list.pre
89000 76 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  v;.  assert( sql
89010 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
89020 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20  mem3.mutex) );. 
89030 20 69 66 28 20 70 72 65 76 3d 3d 30 20 29 7b 0a   if( prev==0 ){.
89040 20 20 20 20 2a 70 52 6f 6f 74 20 3d 20 6e 65 78      *pRoot = nex
89050 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
89060 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 5d  mem3.aPool[prev]
89070 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 6e  .u.list.next = n
89080 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  ext;.  }.  if( n
89090 65 78 74 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e  ext ){.    mem3.
890a0 61 50 6f 6f 6c 5b 6e 65 78 74 5d 2e 75 2e 6c 69  aPool[next].u.li
890b0 73 74 2e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a  st.prev = prev;.
890c0 20 20 7d 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c    }.  mem3.aPool
890d0 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20  [i].u.list.next 
890e0 3d 20 30 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  = 0;.  mem3.aPoo
890f0 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76  l[i].u.list.prev
89100 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55   = 0;.}../*.** U
89110 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20  nlink the chunk 
89120 61 74 20 69 6e 64 65 78 20 69 20 66 72 6f 6d 20  at index i from 
89130 0a 2a 2a 20 77 68 61 74 65 76 65 72 20 6c 69 73  .** whatever lis
89140 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  t is currently a
89150 20 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f 0a 73   member of..*/.s
89160 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
89170 73 33 55 6e 6c 69 6e 6b 28 75 33 32 20 69 29 7b  s3Unlink(u32 i){
89180 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73  .  u32 size, has
89190 68 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  h;.  assert( sql
891a0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
891b0 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20  mem3.mutex) );. 
891c0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61   assert( (mem3.a
891d0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
891e0 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29  size4x & 1)==0 )
891f0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 31  ;.  assert( i>=1
89200 20 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d   );.  size = mem
89210 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
89220 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61  dr.size4x/4;.  a
89230 73 73 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d  ssert( size==mem
89240 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31  3.aPool[i+size-1
89250 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
89260 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69   );.  assert( si
89270 7a 65 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73  ze>=2 );.  if( s
89280 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20  ize <= MX_SMALL 
89290 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 55 6e  ){.    memsys3Un
892a0 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20  linkFromList(i, 
892b0 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 73 69  &mem3.aiSmall[si
892c0 7a 65 2d 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b  ze-2]);.  }else{
892d0 0a 20 20 20 20 68 61 73 68 20 3d 20 73 69 7a 65  .    hash = size
892e0 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20 20 6d   % N_HASH;.    m
892f0 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d  emsys3UnlinkFrom
89300 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69  List(i, &mem3.ai
89310 48 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 7d  Hash[hash]);.  }
89320 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74  .}../*.** Link t
89330 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33  he chunk at mem3
89340 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74 68 61  .aPool[i] so tha
89350 74 20 69 73 20 6f 6e 20 74 68 65 20 6c 69 73 74  t is on the list
89360 20 72 6f 6f 74 65 64 0a 2a 2a 20 61 74 20 2a 70   rooted.** at *p
89370 52 6f 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Root..*/.static 
89380 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  void memsys3Link
89390 49 6e 74 6f 4c 69 73 74 28 75 33 32 20 69 2c 20  IntoList(u32 i, 
893a0 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 61  u32 *pRoot){.  a
893b0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
893c0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
893d0 75 74 65 78 29 20 29 3b 0a 20 20 6d 65 6d 33 2e  utex) );.  mem3.
893e0 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
893f0 6e 65 78 74 20 3d 20 2a 70 52 6f 6f 74 3b 0a 20  next = *pRoot;. 
89400 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75   mem3.aPool[i].u
89410 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a  .list.prev = 0;.
89420 20 20 69 66 28 20 2a 70 52 6f 6f 74 20 29 7b 0a    if( *pRoot ){.
89430 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 2a      mem3.aPool[*
89440 70 52 6f 6f 74 5d 2e 75 2e 6c 69 73 74 2e 70 72  pRoot].u.list.pr
89450 65 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 2a 70  ev = i;.  }.  *p
89460 52 6f 6f 74 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a  Root = i;.}../*.
89470 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e  ** Link the chun
89480 6b 20 61 74 20 69 6e 64 65 78 20 69 20 69 6e 74  k at index i int
89490 6f 20 65 69 74 68 65 72 20 74 68 65 20 61 70 70  o either the app
894a0 72 6f 70 72 69 61 74 65 0a 2a 2a 20 73 6d 61 6c  ropriate.** smal
894b0 6c 20 63 68 75 6e 6b 20 6c 69 73 74 2c 20 6f 72  l chunk list, or
894c0 20 69 6e 74 6f 20 74 68 65 20 6c 61 72 67 65 20   into the large 
894d0 63 68 75 6e 6b 20 68 61 73 68 20 74 61 62 6c 65  chunk hash table
894e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
894f0 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 75 33 32   memsys3Link(u32
89500 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c   i){.  u32 size,
89510 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74 28   hash;.  assert(
89520 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
89530 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
89540 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  );.  assert( i>=
89550 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  1 );.  assert( (
89560 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
89570 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31  u.hdr.size4x & 1
89580 29 3d 3d 30 20 29 3b 0a 20 20 73 69 7a 65 20 3d  )==0 );.  size =
89590 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
895a0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b  .u.hdr.size4x/4;
895b0 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3d  .  assert( size=
895c0 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69  =mem3.aPool[i+si
895d0 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  ze-1].u.hdr.prev
895e0 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74  Size );.  assert
895f0 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 20 69  ( size>=2 );.  i
89600 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d  f( size <= MX_SM
89610 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79  ALL ){.    memsy
89620 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69  s3LinkIntoList(i
89630 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b  , &mem3.aiSmall[
89640 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d 65 6c 73  size-2]);.  }els
89650 65 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 73 69  e{.    hash = si
89660 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20  ze % N_HASH;.   
89670 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f   memsys3LinkInto
89680 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69  List(i, &mem3.ai
89690 48 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 7d  Hash[hash]);.  }
896a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
896b0 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65   STATIC_MEM mute
896c0 78 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  x is not already
896d0 20 68 65 6c 64 2c 20 6f 62 74 61 69 6e 20 69 74   held, obtain it
896e0 20 6e 6f 77 2e 20 54 68 65 20 6d 75 74 65 78 0a   now. The mutex.
896f0 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 20  ** will already 
89700 62 65 20 68 65 6c 64 20 28 6f 62 74 61 69 6e 65  be held (obtaine
89710 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6d 61 6c  d by code in mal
89720 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a 20 73 71 6c  loc.c) if.** sql
89730 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
89740 2e 62 4d 65 6d 53 74 61 74 20 69 73 20 74 72 75  .bMemStat is tru
89750 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
89760 64 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 76  d memsys3Enter(v
89770 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69  oid){.  if( sqli
89780 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
89790 62 4d 65 6d 73 74 61 74 3d 3d 30 20 26 26 20 6d  bMemstat==0 && m
897a0 65 6d 33 2e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a  em3.mutex==0 ){.
897b0 20 20 20 20 6d 65 6d 33 2e 6d 75 74 65 78 20 3d      mem3.mutex =
897c0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
897d0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
897e0 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d  STATIC_MEM);.  }
897f0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
89800 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65  _enter(mem3.mute
89810 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  x);.}.static voi
89820 64 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 76  d memsys3Leave(v
89830 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  oid){.  sqlite3_
89840 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33  mutex_leave(mem3
89850 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  .mutex);.}../*.*
89860 2a 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 77 65  * Called when we
89870 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 73   are unable to s
89880 61 74 69 73 66 79 20 61 6e 20 61 6c 6c 6f 63 61  atisfy an alloca
89890 74 69 6f 6e 20 6f 66 20 6e 42 79 74 65 73 2e 0a  tion of nBytes..
898a0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
898b0 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f 72  emsys3OutOfMemor
898c0 79 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  y(int nByte){.  
898d0 69 66 28 20 21 6d 65 6d 33 2e 61 6c 61 72 6d 42  if( !mem3.alarmB
898e0 75 73 79 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e  usy ){.    mem3.
898f0 61 6c 61 72 6d 42 75 73 79 20 3d 20 31 3b 0a 20  alarmBusy = 1;. 
89900 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
89910 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
89920 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20  m3.mutex) );.   
89930 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
89940 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29  eave(mem3.mutex)
89950 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
89960 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6e 42 79  lease_memory(nBy
89970 74 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  te);.    sqlite3
89980 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
89990 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 6d 65  3.mutex);.    me
899a0 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 30  m3.alarmBusy = 0
899b0 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  ;.  }.}.../*.** 
899c0 43 68 75 6e 6b 20 69 20 69 73 20 61 20 66 72 65  Chunk i is a fre
899d0 65 20 63 68 75 6e 6b 20 74 68 61 74 20 68 61 73  e chunk that has
899e0 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 20   been unlinked. 
899f0 20 41 64 6a 75 73 74 20 69 74 73 20 0a 2a 2a 20   Adjust its .** 
89a00 73 69 7a 65 20 70 61 72 61 6d 65 74 65 72 73 20  size parameters 
89a10 66 6f 72 20 63 68 65 63 6b 2d 6f 75 74 20 61 6e  for check-out an
89a20 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  d return a point
89a30 65 72 20 74 6f 20 74 68 65 20 0a 2a 2a 20 75 73  er to the .** us
89a40 65 72 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68  er portion of th
89a50 65 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 73 74 61 74  e chunk..*/.stat
89a60 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33  ic void *memsys3
89a70 43 68 65 63 6b 6f 75 74 28 75 33 32 20 69 2c 20  Checkout(u32 i, 
89a80 75 33 32 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 75  u32 nBlock){.  u
89a90 33 32 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20  32 x;.  assert( 
89aa0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
89ab0 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29  ld(mem3.mutex) )
89ac0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 31  ;.  assert( i>=1
89ad0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65   );.  assert( me
89ae0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
89af0 68 64 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42  hdr.size4x/4==nB
89b00 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74  lock );.  assert
89b10 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e  ( mem3.aPool[i+n
89b20 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 2e 70  Block-1].u.hdr.p
89b30 72 65 76 53 69 7a 65 3d 3d 6e 42 6c 6f 63 6b 20  revSize==nBlock 
89b40 29 3b 0a 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50  );.  x = mem3.aP
89b50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
89b60 69 7a 65 34 78 3b 0a 20 20 6d 65 6d 33 2e 61 50  ize4x;.  mem3.aP
89b70 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
89b80 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34  ize4x = nBlock*4
89b90 20 7c 20 31 20 7c 20 28 78 26 32 29 3b 0a 20 20   | 1 | (x&2);.  
89ba0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c  mem3.aPool[i+nBl
89bb0 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ock-1].u.hdr.pre
89bc0 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a  vSize = nBlock;.
89bd0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e    mem3.aPool[i+n
89be0 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 2e 73  Block-1].u.hdr.s
89bf0 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20 72 65  ize4x |= 2;.  re
89c00 74 75 72 6e 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c  turn &mem3.aPool
89c10 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61  [i];.}../*.** Ca
89c20 72 76 65 20 61 20 70 69 65 63 65 20 6f 66 66 20  rve a piece off 
89c30 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  of the end of th
89c40 65 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 66  e mem3.iMaster f
89c50 72 65 65 20 63 68 75 6e 6b 2e 0a 2a 2a 20 52 65  ree chunk..** Re
89c60 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
89c70 6f 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 61  o the new alloca
89c80 74 69 6f 6e 2e 20 20 4f 72 2c 20 69 66 20 74 68  tion.  Or, if th
89c90 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 0a 2a  e master chunk.*
89ca0 2a 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 20 65  * is not large e
89cb0 6e 6f 75 67 68 2c 20 72 65 74 75 72 6e 20 30 2e  nough, return 0.
89cc0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
89cd0 2a 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74  *memsys3FromMast
89ce0 65 72 28 75 33 32 20 6e 42 6c 6f 63 6b 29 7b 0a  er(u32 nBlock){.
89cf0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
89d00 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
89d10 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  3.mutex) );.  as
89d20 73 65 72 74 28 20 6d 65 6d 33 2e 73 7a 4d 61 73  sert( mem3.szMas
89d30 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20  ter>=nBlock );. 
89d40 20 69 66 28 20 6e 42 6c 6f 63 6b 3e 3d 6d 65 6d   if( nBlock>=mem
89d50 33 2e 73 7a 4d 61 73 74 65 72 2d 31 20 29 7b 0a  3.szMaster-1 ){.
89d60 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 65      /* Use the e
89d70 6e 74 69 72 65 20 6d 61 73 74 65 72 20 2a 2f 0a  ntire master */.
89d80 20 20 20 20 76 6f 69 64 20 2a 70 20 3d 20 6d 65      void *p = me
89d90 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 28 6d 65  msys3Checkout(me
89da0 6d 33 2e 69 4d 61 73 74 65 72 2c 20 6d 65 6d 33  m3.iMaster, mem3
89db0 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  .szMaster);.    
89dc0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30  mem3.iMaster = 0
89dd0 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73  ;.    mem3.szMas
89de0 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d  ter = 0;.    mem
89df0 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 30 3b 0a  3.mnMaster = 0;.
89e00 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20      return p;.  
89e10 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53 70  }else{.    /* Sp
89e20 6c 69 74 20 74 68 65 20 6d 61 73 74 65 72 20 62  lit the master b
89e30 6c 6f 63 6b 2e 20 20 52 65 74 75 72 6e 20 74 68  lock.  Return th
89e40 65 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 75  e tail. */.    u
89e50 33 32 20 6e 65 77 69 2c 20 78 3b 0a 20 20 20 20  32 newi, x;.    
89e60 6e 65 77 69 20 3d 20 6d 65 6d 33 2e 69 4d 61 73  newi = mem3.iMas
89e70 74 65 72 20 2b 20 6d 65 6d 33 2e 73 7a 4d 61 73  ter + mem3.szMas
89e80 74 65 72 20 2d 20 6e 42 6c 6f 63 6b 3b 0a 20 20  ter - nBlock;.  
89e90 20 20 61 73 73 65 72 74 28 20 6e 65 77 69 20 3e    assert( newi >
89ea0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 31 20   mem3.iMaster+1 
89eb0 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  );.    mem3.aPoo
89ec0 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d  l[mem3.iMaster+m
89ed0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e  em3.szMaster-1].
89ee0 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d  u.hdr.prevSize =
89ef0 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 6d 65 6d   nBlock;.    mem
89f00 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
89f10 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74  ster+mem3.szMast
89f20 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  er-1].u.hdr.size
89f30 34 78 20 7c 3d 20 32 3b 0a 20 20 20 20 6d 65 6d  4x |= 2;.    mem
89f40 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e  3.aPool[newi-1].
89f50 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e  u.hdr.size4x = n
89f60 42 6c 6f 63 6b 2a 34 20 2b 20 31 3b 0a 20 20 20  Block*4 + 1;.   
89f70 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d   mem3.szMaster -
89f80 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 6d 65  = nBlock;.    me
89f90 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69 2d 31 5d  m3.aPool[newi-1]
89fa0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
89fb0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b  = mem3.szMaster;
89fc0 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50  .    x = mem3.aP
89fd0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
89fe0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
89ff0 20 26 20 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61   & 2;.    mem3.a
8a000 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
8a010 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
8a020 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  x = mem3.szMaste
8a030 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 69 66 28  r*4 | x;.    if(
8a040 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3c   mem3.szMaster <
8a050 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 29   mem3.mnMaster )
8a060 7b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d  {.      mem3.mnM
8a070 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d  aster = mem3.szM
8a080 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20  aster;.    }.   
8a090 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 26   return (void*)&
8a0a0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69 5d  mem3.aPool[newi]
8a0b0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 2a  ;.  }.}../*.** *
8a0c0 70 52 6f 6f 74 20 69 73 20 74 68 65 20 68 65 61  pRoot is the hea
8a0d0 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 66  d of a list of f
8a0e0 72 65 65 20 63 68 75 6e 6b 73 20 6f 66 20 74 68  ree chunks of th
8a0f0 65 20 73 61 6d 65 20 73 69 7a 65 0a 2a 2a 20 6f  e same size.** o
8a100 72 20 73 61 6d 65 20 73 69 7a 65 20 68 61 73 68  r same size hash
8a110 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
8a120 73 2c 20 2a 70 52 6f 6f 74 20 69 73 20 61 6e 20  s, *pRoot is an 
8a130 65 6e 74 72 79 20 69 6e 20 65 69 74 68 65 72 0a  entry in either.
8a140 2a 2a 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b  ** mem3.aiSmall[
8a150 5d 20 6f 72 20 6d 65 6d 33 2e 61 69 48 61 73 68  ] or mem3.aiHash
8a160 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  [].  .**.** This
8a170 20 72 6f 75 74 69 6e 65 20 65 78 61 6d 69 6e 65   routine examine
8a180 73 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 6f 6e  s all entries on
8a190 20 74 68 65 20 67 69 76 65 6e 20 6c 69 73 74 20   the given list 
8a1a0 61 6e 64 20 74 72 69 65 73 0a 2a 2a 20 74 6f 20  and tries.** to 
8a1b0 63 6f 61 6c 65 73 63 65 20 65 61 63 68 20 65 6e  coalesce each en
8a1c0 74 72 69 65 73 20 77 69 74 68 20 61 64 6a 61 63  tries with adjac
8a1d0 65 6e 74 20 66 72 65 65 20 63 68 75 6e 6b 73 2e  ent free chunks.
8a1e0 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 73    .**.** If it s
8a1f0 65 65 73 20 61 20 63 68 75 6e 6b 20 74 68 61 74  ees a chunk that
8a200 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
8a210 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c 20 69 74  mem3.iMaster, it
8a220 20 72 65 70 6c 61 63 65 73 20 0a 2a 2a 20 74 68   replaces .** th
8a230 65 20 63 75 72 72 65 6e 74 20 6d 65 6d 33 2e 69  e current mem3.i
8a240 4d 61 73 74 65 72 20 77 69 74 68 20 74 68 65 20  Master with the 
8a250 6e 65 77 20 6c 61 72 67 65 72 20 63 68 75 6e 6b  new larger chunk
8a260 2e 20 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 0a  .  In order for.
8a270 2a 2a 20 74 68 69 73 20 6d 65 6d 33 2e 69 4d 61  ** this mem3.iMa
8a280 73 74 65 72 20 72 65 70 6c 61 63 65 6d 65 6e 74  ster replacement
8a290 20 74 6f 20 77 6f 72 6b 2c 20 74 68 65 20 6d 61   to work, the ma
8a2a0 73 74 65 72 20 63 68 75 6e 6b 20 6d 75 73 74 20  ster chunk must 
8a2b0 62 65 0a 2a 2a 20 6c 69 6e 6b 65 64 20 69 6e 74  be.** linked int
8a2c0 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
8a2d0 73 2e 20 20 54 68 61 74 20 69 73 20 6e 6f 74 20  s.  That is not 
8a2e0 74 68 65 20 6e 6f 72 6d 61 6c 20 73 74 61 74 65  the normal state
8a2f0 20 6f 66 0a 2a 2a 20 61 66 66 61 69 72 73 2c 20   of.** affairs, 
8a300 6f 66 20 63 6f 75 72 73 65 2e 20 20 54 68 65 20  of course.  The 
8a310 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20  calling routine 
8a320 6d 75 73 74 20 6c 69 6e 6b 20 74 68 65 20 6d 61  must link the ma
8a330 73 74 65 72 0a 2a 2a 20 63 68 75 6e 6b 20 62 65  ster.** chunk be
8a340 66 6f 72 65 20 69 6e 76 6f 6b 69 6e 67 20 74 68  fore invoking th
8a350 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e  is routine, then
8a360 20 6d 75 73 74 20 75 6e 6c 69 6e 6b 20 74 68 65   must unlink the
8a370 20 28 70 6f 73 73 69 62 6c 79 0a 2a 2a 20 63 68   (possibly.** ch
8a380 61 6e 67 65 64 29 20 6d 61 73 74 65 72 20 63 68  anged) master ch
8a390 75 6e 6b 20 6f 6e 63 65 20 74 68 69 73 20 72 6f  unk once this ro
8a3a0 75 74 69 6e 65 20 68 61 73 20 66 69 6e 69 73 68  utine has finish
8a3b0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
8a3c0 69 64 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28  id memsys3Merge(
8a3d0 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75  u32 *pRoot){.  u
8a3e0 33 32 20 69 4e 65 78 74 2c 20 70 72 65 76 2c 20  32 iNext, prev, 
8a3f0 73 69 7a 65 2c 20 69 2c 20 78 3b 0a 0a 20 20 61  size, i, x;..  a
8a400 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
8a410 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
8a420 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69  utex) );.  for(i
8a430 3d 2a 70 52 6f 6f 74 3b 20 69 3e 30 3b 20 69 3d  =*pRoot; i>0; i=
8a440 69 4e 65 78 74 29 7b 0a 20 20 20 20 69 4e 65 78  iNext){.    iNex
8a450 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t = mem3.aPool[i
8a460 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20  ].u.list.next;. 
8a470 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61     size = mem3.a
8a480 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
8a490 73 69 7a 65 34 78 3b 0a 20 20 20 20 61 73 73 65  size4x;.    asse
8a4a0 72 74 28 20 28 73 69 7a 65 26 31 29 3d 3d 30 20  rt( (size&1)==0 
8a4b0 29 3b 0a 20 20 20 20 69 66 28 20 28 73 69 7a 65  );.    if( (size
8a4c0 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  &2)==0 ){.      
8a4d0 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f  memsys3UnlinkFro
8a4e0 6d 4c 69 73 74 28 69 2c 20 70 52 6f 6f 74 29 3b  mList(i, pRoot);
8a4f0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
8a500 20 3e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   > mem3.aPool[i-
8a510 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
8a520 65 20 29 3b 0a 20 20 20 20 20 20 70 72 65 76 20  e );.      prev 
8a530 3d 20 69 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c  = i - mem3.aPool
8a540 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  [i-1].u.hdr.prev
8a550 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20  Size;.      if( 
8a560 70 72 65 76 3d 3d 69 4e 65 78 74 20 29 7b 0a 20  prev==iNext ){. 
8a570 20 20 20 20 20 20 20 69 4e 65 78 74 20 3d 20 6d         iNext = m
8a580 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e  em3.aPool[prev].
8a590 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 20 20  u.list.next;.   
8a5a0 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 79     }.      memsy
8a5b0 73 33 55 6e 6c 69 6e 6b 28 70 72 65 76 29 3b 0a  s3Unlink(prev);.
8a5c0 20 20 20 20 20 20 73 69 7a 65 20 3d 20 69 20 2b        size = i +
8a5d0 20 73 69 7a 65 2f 34 20 2d 20 70 72 65 76 3b 0a   size/4 - prev;.
8a5e0 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61        x = mem3.a
8a5f0 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e 68  Pool[prev-1].u.h
8a600 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20  dr.size4x & 2;. 
8a610 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b       mem3.aPool[
8a620 70 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  prev-1].u.hdr.si
8a630 7a 65 34 78 20 3d 20 73 69 7a 65 2a 34 20 7c 20  ze4x = size*4 | 
8a640 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50  x;.      mem3.aP
8a650 6f 6f 6c 5b 70 72 65 76 2b 73 69 7a 65 2d 31 5d  ool[prev+size-1]
8a660 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
8a670 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65  = size;.      me
8a680 6d 73 79 73 33 4c 69 6e 6b 28 70 72 65 76 29 3b  msys3Link(prev);
8a690 0a 20 20 20 20 20 20 69 20 3d 20 70 72 65 76 3b  .      i = prev;
8a6a0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
8a6b0 20 20 73 69 7a 65 20 2f 3d 20 34 3b 0a 20 20 20    size /= 4;.   
8a6c0 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3e   }.    if( size>
8a6d0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b  mem3.szMaster ){
8a6e0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73  .      mem3.iMas
8a6f0 74 65 72 20 3d 20 69 3b 0a 20 20 20 20 20 20 6d  ter = i;.      m
8a700 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 73  em3.szMaster = s
8a710 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ize;.    }.  }.}
8a720 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
8a730 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79   block of memory
8a740 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 79   of at least nBy
8a750 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20  tes in size..** 
8a760 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 75  Return NULL if u
8a770 6e 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  nable..**.** Thi
8a780 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d  s function assum
8a790 65 73 20 74 68 61 74 20 74 68 65 20 6e 65 63 65  es that the nece
8a7a0 73 73 61 72 79 20 6d 75 74 65 78 65 73 2c 20 69  ssary mutexes, i
8a7b0 66 20 61 6e 79 2c 20 61 72 65 0a 2a 2a 20 61 6c  f any, are.** al
8a7c0 72 65 61 64 79 20 68 65 6c 64 20 62 79 20 74 68  ready held by th
8a7d0 65 20 63 61 6c 6c 65 72 2e 20 48 65 6e 63 65 20  e caller. Hence 
8a7e0 22 55 6e 73 61 66 65 22 2e 0a 2a 2f 0a 73 74 61  "Unsafe"..*/.sta
8a7f0 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73  tic void *memsys
8a800 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69 6e  3MallocUnsafe(in
8a810 74 20 6e 42 79 74 65 29 7b 0a 20 20 75 33 32 20  t nByte){.  u32 
8a820 69 3b 0a 20 20 75 33 32 20 6e 42 6c 6f 63 6b 3b  i;.  u32 nBlock;
8a830 0a 20 20 75 33 32 20 74 6f 46 72 65 65 3b 0a 0a  .  u32 toFree;..
8a840 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
8a850 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
8a860 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  3.mutex) );.  as
8a870 73 65 72 74 28 20 73 69 7a 65 6f 66 28 4d 65 6d  sert( sizeof(Mem
8a880 33 42 6c 6f 63 6b 29 3d 3d 38 20 29 3b 0a 20 20  3Block)==8 );.  
8a890 69 66 28 20 6e 42 79 74 65 3c 3d 31 32 20 29 7b  if( nByte<=12 ){
8a8a0 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d 20 32 3b  .    nBlock = 2;
8a8b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 42  .  }else{.    nB
8a8c0 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 65 20 2b 20  lock = (nByte + 
8a8d0 31 31 29 2f 38 3b 0a 20 20 7d 0a 20 20 61 73 73  11)/8;.  }.  ass
8a8e0 65 72 74 28 20 6e 42 6c 6f 63 6b 3e 3d 32 20 29  ert( nBlock>=2 )
8a8f0 3b 0a 0a 20 20 2f 2a 20 53 54 45 50 20 31 3a 0a  ;..  /* STEP 1:.
8a900 20 20 2a 2a 20 4c 6f 6f 6b 20 66 6f 72 20 61 6e    ** Look for an
8a910 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 6f   entry of the co
8a920 72 72 65 63 74 20 73 69 7a 65 20 69 6e 20 65 69  rrect size in ei
8a930 74 68 65 72 20 74 68 65 20 73 6d 61 6c 6c 0a 20  ther the small. 
8a940 20 2a 2a 20 63 68 75 6e 6b 20 74 61 62 6c 65 20   ** chunk table 
8a950 6f 72 20 69 6e 20 74 68 65 20 6c 61 72 67 65 20  or in the large 
8a960 63 68 75 6e 6b 20 68 61 73 68 20 74 61 62 6c 65  chunk hash table
8a970 2e 20 20 54 68 69 73 20 69 73 0a 20 20 2a 2a 20  .  This is.  ** 
8a980 73 75 63 63 65 73 73 66 75 6c 20 6d 6f 73 74 20  successful most 
8a990 6f 66 20 74 68 65 20 74 69 6d 65 20 28 61 62 6f  of the time (abo
8a9a0 75 74 20 39 20 74 69 6d 65 73 20 6f 75 74 20 6f  ut 9 times out o
8a9b0 66 20 31 30 29 2e 0a 20 20 2a 2f 0a 20 20 69 66  f 10)..  */.  if
8a9c0 28 20 6e 42 6c 6f 63 6b 20 3c 3d 20 4d 58 5f 53  ( nBlock <= MX_S
8a9d0 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 69 20 3d 20  MALL ){.    i = 
8a9e0 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c  mem3.aiSmall[nBl
8a9f0 6f 63 6b 2d 32 5d 3b 0a 20 20 20 20 69 66 28 20  ock-2];.    if( 
8aa00 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  i>0 ){.      mem
8aa10 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69  sys3UnlinkFromLi
8aa20 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d  st(i, &mem3.aiSm
8aa30 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 29 3b 0a  all[nBlock-2]);.
8aa40 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d        return mem
8aa50 73 79 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20  sys3Checkout(i, 
8aa60 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 7d 0a 20  nBlock);.    }. 
8aa70 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
8aa80 68 61 73 68 20 3d 20 6e 42 6c 6f 63 6b 20 25 20  hash = nBlock % 
8aa90 4e 5f 48 41 53 48 3b 0a 20 20 20 20 66 6f 72 28  N_HASH;.    for(
8aaa0 69 3d 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61  i=mem3.aiHash[ha
8aab0 73 68 5d 3b 20 69 3e 30 3b 20 69 3d 6d 65 6d 33  sh]; i>0; i=mem3
8aac0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
8aad0 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66  .next){.      if
8aae0 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31  ( mem3.aPool[i-1
8aaf0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34  ].u.hdr.size4x/4
8ab00 3d 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20  ==nBlock ){.    
8ab10 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
8ab20 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65  kFromList(i, &me
8ab30 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29  m3.aiHash[hash])
8ab40 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
8ab50 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74   memsys3Checkout
8ab60 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20  (i, nBlock);.   
8ab70 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
8ab80 20 20 2f 2a 20 53 54 45 50 20 32 3a 0a 20 20 2a    /* STEP 2:.  *
8ab90 2a 20 54 72 79 20 74 6f 20 73 61 74 69 73 66 79  * Try to satisfy
8aba0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
8abb0 62 79 20 63 61 72 76 69 6e 67 20 61 20 70 69 65  by carving a pie
8abc0 63 65 20 6f 66 66 20 6f 66 20 74 68 65 20 65 6e  ce off of the en
8abd0 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 61  d.  ** of the ma
8abe0 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 54 68 69  ster chunk.  Thi
8abf0 73 20 73 74 65 70 20 75 73 75 61 6c 6c 79 20 77  s step usually w
8ac00 6f 72 6b 73 20 69 66 20 73 74 65 70 20 31 20 66  orks if step 1 f
8ac10 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ails..  */.  if(
8ac20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d   mem3.szMaster>=
8ac30 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 72 65  nBlock ){.    re
8ac40 74 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d  turn memsys3From
8ac50 4d 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a  Master(nBlock);.
8ac60 20 20 7d 0a 0a 0a 20 20 2f 2a 20 53 54 45 50 20    }...  /* STEP 
8ac70 33 3a 20 20 0a 20 20 2a 2a 20 4c 6f 6f 70 20 74  3:  .  ** Loop t
8ac80 68 72 6f 75 67 68 20 74 68 65 20 65 6e 74 69 72  hrough the entir
8ac90 65 20 6d 65 6d 6f 72 79 20 70 6f 6f 6c 2e 20 20  e memory pool.  
8aca0 43 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e  Coalesce adjacen
8acb0 74 20 66 72 65 65 0a 20 20 2a 2a 20 63 68 75 6e  t free.  ** chun
8acc0 6b 73 2e 20 20 52 65 63 6f 6d 70 75 74 65 20 74  ks.  Recompute t
8acd0 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20  he master chunk 
8ace0 61 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 66  as the largest f
8acf0 72 65 65 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20  ree chunk..  ** 
8ad00 54 68 65 6e 20 74 72 79 20 61 67 61 69 6e 20 74  Then try again t
8ad10 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61 6c  o satisfy the al
8ad20 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 76  location by carv
8ad30 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 0a  ing a piece off.
8ad40 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 6e 64 20    ** of the end 
8ad50 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68  of the master ch
8ad60 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 20  unk.  This step 
8ad70 68 61 70 70 65 6e 73 20 76 65 72 79 0a 20 20 2a  happens very.  *
8ad80 2a 20 72 61 72 65 6c 79 20 28 77 65 20 68 6f 70  * rarely (we hop
8ad90 65 21 29 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 74  e!).  */.  for(t
8ada0 6f 46 72 65 65 3d 6e 42 6c 6f 63 6b 2a 31 36 3b  oFree=nBlock*16;
8adb0 20 74 6f 46 72 65 65 3c 28 6d 65 6d 33 2e 6e 50   toFree<(mem3.nP
8adc0 6f 6f 6c 2a 31 36 29 3b 20 74 6f 46 72 65 65 20  ool*16); toFree 
8add0 2a 3d 20 32 29 7b 0a 20 20 20 20 6d 65 6d 73 79  *= 2){.    memsy
8ade0 73 33 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 74 6f  s3OutOfMemory(to
8adf0 46 72 65 65 29 3b 0a 20 20 20 20 69 66 28 20 6d  Free);.    if( m
8ae00 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a 20  em3.iMaster ){. 
8ae10 20 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b       memsys3Link
8ae20 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a  (mem3.iMaster);.
8ae30 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74        mem3.iMast
8ae40 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 6d 65  er = 0;.      me
8ae50 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 30 3b  m3.szMaster = 0;
8ae60 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
8ae70 3d 30 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b  =0; i<N_HASH; i+
8ae80 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73  +){.      memsys
8ae90 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 69 48  3Merge(&mem3.aiH
8aea0 61 73 68 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  ash[i]);.    }. 
8aeb0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58     for(i=0; i<MX
8aec0 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a  _SMALL-1; i++){.
8aed0 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72        memsys3Mer
8aee0 67 65 28 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c  ge(&mem3.aiSmall
8aef0 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  [i]);.    }.    
8af00 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  if( mem3.szMaste
8af10 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79  r ){.      memsy
8af20 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d  s3Unlink(mem3.iM
8af30 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 69 66  aster);.      if
8af40 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e  ( mem3.szMaster>
8af50 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  =nBlock ){.     
8af60 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73     return memsys
8af70 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c 6f  3FromMaster(nBlo
8af80 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ck);.      }.   
8af90 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
8afa0 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76  none of the abov
8afb0 65 20 77 6f 72 6b 65 64 2c 20 74 68 65 6e 20 77  e worked, then w
8afc0 65 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 72 65 74  e fail. */.  ret
8afd0 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
8afe0 46 72 65 65 20 61 6e 20 6f 75 74 73 74 61 6e 64  Free an outstand
8aff0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ing memory alloc
8b000 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
8b010 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d  s function assum
8b020 65 73 20 74 68 61 74 20 74 68 65 20 6e 65 63 65  es that the nece
8b030 73 73 61 72 79 20 6d 75 74 65 78 65 73 2c 20 69  ssary mutexes, i
8b040 66 20 61 6e 79 2c 20 61 72 65 0a 2a 2a 20 61 6c  f any, are.** al
8b050 72 65 61 64 79 20 68 65 6c 64 20 62 79 20 74 68  ready held by th
8b060 65 20 63 61 6c 6c 65 72 2e 20 48 65 6e 63 65 20  e caller. Hence 
8b070 22 55 6e 73 61 66 65 22 2e 0a 2a 2f 0a 76 6f 69  "Unsafe"..*/.voi
8b080 64 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e 73  d memsys3FreeUns
8b090 61 66 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29 7b  afe(void *pOld){
8b0a0 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70 20  .  Mem3Block *p 
8b0b0 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 4f  = (Mem3Block*)pO
8b0c0 6c 64 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 75  ld;.  int i;.  u
8b0d0 33 32 20 73 69 7a 65 2c 20 78 3b 0a 20 20 61 73  32 size, x;.  as
8b0e0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
8b0f0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75  tex_held(mem3.mu
8b100 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
8b110 28 20 70 3e 6d 65 6d 33 2e 61 50 6f 6f 6c 20 26  ( p>mem3.aPool &
8b120 26 20 70 3c 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  & p<&mem3.aPool[
8b130 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 20 29 3b 0a 20  mem3.nPool] );. 
8b140 20 69 20 3d 20 70 20 2d 20 6d 65 6d 33 2e 61 50   i = p - mem3.aP
8b150 6f 6f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ool;.  assert( (
8b160 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
8b170 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29 3d  u.hdr.size4x&1)=
8b180 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d  =1 );.  size = m
8b190 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  em3.aPool[i-1].u
8b1a0 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20  .hdr.size4x/4;. 
8b1b0 20 61 73 73 65 72 74 28 20 69 2b 73 69 7a 65 3c   assert( i+size<
8b1c0 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b 31 20 29 3b  =mem3.nPool+1 );
8b1d0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d  .  mem3.aPool[i-
8b1e0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8b1f0 26 3d 20 7e 31 3b 0a 20 20 6d 65 6d 33 2e 61 50  &= ~1;.  mem3.aP
8b200 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e  ool[i+size-1].u.
8b210 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 73  hdr.prevSize = s
8b220 69 7a 65 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  ize;.  mem3.aPoo
8b230 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64  l[i+size-1].u.hd
8b240 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 32 3b 0a  r.size4x &= ~2;.
8b250 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 69 29    memsys3Link(i)
8b260 3b 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 65  ;..  /* Try to e
8b270 78 70 61 6e 64 20 74 68 65 20 6d 61 73 74 65 72  xpand the master
8b280 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 6c 79   using the newly
8b290 20 66 72 65 65 64 20 63 68 75 6e 6b 20 2a 2f 0a   freed chunk */.
8b2a0 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74    if( mem3.iMast
8b2b0 65 72 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28  er ){.    while(
8b2c0 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d   (mem3.aPool[mem
8b2d0 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  3.iMaster-1].u.h
8b2e0 64 72 2e 73 69 7a 65 34 78 26 32 29 3d 3d 30 20  dr.size4x&2)==0 
8b2f0 29 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20  ){.      size = 
8b300 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
8b310 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72  iMaster-1].u.hdr
8b320 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20  .prevSize;.     
8b330 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2d 3d   mem3.iMaster -=
8b340 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d   size;.      mem
8b350 33 2e 73 7a 4d 61 73 74 65 72 20 2b 3d 20 73 69  3.szMaster += si
8b360 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73  ze;.      memsys
8b370 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61  3Unlink(mem3.iMa
8b380 73 74 65 72 29 3b 0a 20 20 20 20 20 20 78 20 3d  ster);.      x =
8b390 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
8b3a0 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  .iMaster-1].u.hd
8b3b0 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20  r.size4x & 2;.  
8b3c0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d      mem3.aPool[m
8b3d0 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
8b3e0 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65  .hdr.size4x = me
8b3f0 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20  m3.szMaster*4 | 
8b400 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50  x;.      mem3.aP
8b410 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
8b420 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31  +mem3.szMaster-1
8b430 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
8b440 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72   = mem3.szMaster
8b450 3b 0a 20 20 20 20 7d 0a 20 20 20 20 78 20 3d 20  ;.    }.    x = 
8b460 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
8b470 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72  iMaster-1].u.hdr
8b480 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 20  .size4x & 2;.   
8b490 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50   while( (mem3.aP
8b4a0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
8b4b0 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31  +mem3.szMaster-1
8b4c0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31  ].u.hdr.size4x&1
8b4d0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65  )==0 ){.      me
8b4e0 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33  msys3Unlink(mem3
8b4f0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
8b500 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 6d  Master);.      m
8b510 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b 3d 20  em3.szMaster += 
8b520 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
8b530 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d  iMaster+mem3.szM
8b540 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73  aster-1].u.hdr.s
8b550 69 7a 65 34 78 2f 34 3b 0a 20 20 20 20 20 20 6d  ize4x/4;.      m
8b560 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
8b570 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
8b580 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a  size4x = mem3.sz
8b590 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20  Master*4 | x;.  
8b5a0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d      mem3.aPool[m
8b5b0 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33  em3.iMaster+mem3
8b5c0 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  .szMaster-1].u.h
8b5d0 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65  dr.prevSize = me
8b5e0 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20  m3.szMaster;.   
8b5f0 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
8b600 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
8b610 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e  of an outstandin
8b620 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e  g allocation, in
8b630 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20   bytes.  The.** 
8b640 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d  size returned om
8b650 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 68  its the 8-byte h
8b660 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20  eader overhead. 
8b670 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f   This only.** wo
8b680 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 74  rks for chunks t
8b690 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c  hat are currentl
8b6a0 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a  y checked out..*
8b6b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d  /.static int mem
8b6c0 73 79 73 33 53 69 7a 65 28 76 6f 69 64 20 2a 70  sys3Size(void *p
8b6d0 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a  ){.  Mem3Block *
8b6e0 70 42 6c 6f 63 6b 3b 0a 20 20 69 66 28 20 70 3d  pBlock;.  if( p=
8b6f0 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
8b700 20 70 42 6c 6f 63 6b 20 3d 20 28 4d 65 6d 33 42   pBlock = (Mem3B
8b710 6c 6f 63 6b 2a 29 70 3b 0a 20 20 61 73 73 65 72  lock*)p;.  asser
8b720 74 28 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75  t( (pBlock[-1].u
8b730 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29 21 3d  .hdr.size4x&1)!=
8b740 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  0 );.  return (p
8b750 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e  Block[-1].u.hdr.
8b760 73 69 7a 65 34 78 26 7e 33 29 2a 32 20 2d 20 34  size4x&~3)*2 - 4
8b770 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64  ;.}../*.** Round
8b780 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73 69   up a request si
8b790 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76  ze to the next v
8b7a0 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  alid allocation 
8b7b0 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  size..*/.static 
8b7c0 69 6e 74 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64  int memsys3Round
8b7d0 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 69 66 28  up(int n){.  if(
8b7e0 20 6e 3c 3d 31 32 20 29 7b 0a 20 20 20 20 72 65   n<=12 ){.    re
8b7f0 74 75 72 6e 20 31 32 3b 0a 20 20 7d 65 6c 73 65  turn 12;.  }else
8b800 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28 6e  {.    return ((n
8b810 2b 31 31 29 26 7e 37 29 20 2d 20 34 3b 0a 20 20  +11)&~7) - 4;.  
8b820 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  }.}../*.** Alloc
8b830 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20 6d 65  ate nBytes of me
8b840 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mory..*/.static 
8b850 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c  void *memsys3Mal
8b860 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29 7b  loc(int nBytes){
8b870 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
8b880 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e   *p;.  assert( n
8b890 42 79 74 65 73 3e 30 20 29 3b 20 20 20 20 20 20  Bytes>0 );      
8b8a0 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f 63 2e 63 20      /* malloc.c 
8b8b0 66 69 6c 74 65 72 73 20 6f 75 74 20 30 20 62 79  filters out 0 by
8b8c0 74 65 20 72 65 71 75 65 73 74 73 20 2a 2f 0a 20  te requests */. 
8b8d0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b   memsys3Enter();
8b8e0 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61  .  p = memsys3Ma
8b8f0 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65  llocUnsafe(nByte
8b900 73 29 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61  s);.  memsys3Lea
8b910 76 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  ve();.  return (
8b920 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a  void*)p; .}../*.
8b930 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a  ** Free memory..
8b940 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46  */.void memsys3F
8b950 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ree(void *pPrior
8b960 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72  ){.  assert( pPr
8b970 69 6f 72 20 29 3b 0a 20 20 6d 65 6d 73 79 73 33  ior );.  memsys3
8b980 45 6e 74 65 72 28 29 3b 0a 20 20 6d 65 6d 73 79  Enter();.  memsy
8b990 73 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72  s3FreeUnsafe(pPr
8b9a0 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 79 73 33 4c  ior);.  memsys3L
8b9b0 65 61 76 65 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  eave();.}../*.**
8b9c0 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65   Change the size
8b9d0 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   of an existing 
8b9e0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
8b9f0 6e 0a 2a 2f 0a 76 6f 69 64 20 2a 6d 65 6d 73 79  n.*/.void *memsy
8ba00 73 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a  s3Realloc(void *
8ba10 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74  pPrior, int nByt
8ba20 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b  es){.  int nOld;
8ba30 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66  .  void *p;.  if
8ba40 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20  ( pPrior==0 ){. 
8ba50 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
8ba60 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29  3_malloc(nBytes)
8ba70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74  ;.  }.  if( nByt
8ba80 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  es<=0 ){.    sql
8ba90 69 74 65 33 5f 66 72 65 65 28 70 50 72 69 6f 72  ite3_free(pPrior
8baa0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
8bab0 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65  .  }.  nOld = me
8bac0 6d 73 79 73 33 53 69 7a 65 28 70 50 72 69 6f 72  msys3Size(pPrior
8bad0 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c  );.  if( nBytes<
8bae0 3d 6e 4f 6c 64 20 26 26 20 6e 42 79 74 65 73 3e  =nOld && nBytes>
8baf0 3d 6e 4f 6c 64 2d 31 32 38 20 29 7b 0a 20 20 20  =nOld-128 ){.   
8bb00 20 72 65 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a   return pPrior;.
8bb10 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74    }.  memsys3Ent
8bb20 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73  er();.  p = mems
8bb30 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28  ys3MallocUnsafe(
8bb40 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70  nBytes);.  if( p
8bb50 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 4f 6c 64   ){.    if( nOld
8bb60 3c 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 20  <nBytes ){.     
8bb70 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f   memcpy(p, pPrio
8bb80 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 65  r, nOld);.    }e
8bb90 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  lse{.      memcp
8bba0 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79  y(p, pPrior, nBy
8bbb0 74 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  tes);.    }.    
8bbc0 6d 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66  memsys3FreeUnsaf
8bbd0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20  e(pPrior);.  }. 
8bbe0 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b   memsys3Leave();
8bbf0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
8bc00 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
8bc10 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f   this module..*/
8bc20 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73  .static int mems
8bc30 79 73 33 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f  ys3Init(void *No
8bc40 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44  tUsed){.  UNUSED
8bc50 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
8bc60 65 64 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  ed);.  if( !sqli
8bc70 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
8bc80 70 48 65 61 70 20 29 7b 0a 20 20 20 20 72 65 74  pHeap ){.    ret
8bc90 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
8bca0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 6f 72  ;.  }..  /* Stor
8bcb0 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  e a pointer to t
8bcc0 68 65 20 6d 65 6d 6f 72 79 20 62 6c 6f 63 6b 20  he memory block 
8bcd0 69 6e 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74  in global struct
8bce0 75 72 65 20 6d 65 6d 33 2e 20 2a 2f 0a 20 20 61  ure mem3. */.  a
8bcf0 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 4d 65  ssert( sizeof(Me
8bd00 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 29 3b 0a 20  m3Block)==8 );. 
8bd10 20 6d 65 6d 33 2e 61 50 6f 6f 6c 20 3d 20 28 4d   mem3.aPool = (M
8bd20 65 6d 33 42 6c 6f 63 6b 20 2a 29 73 71 6c 69 74  em3Block *)sqlit
8bd30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
8bd40 48 65 61 70 3b 0a 20 20 6d 65 6d 33 2e 6e 50 6f  Heap;.  mem3.nPo
8bd50 6f 6c 20 3d 20 28 73 71 6c 69 74 65 33 47 6c 6f  ol = (sqlite3Glo
8bd60 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 20  balConfig.nHeap 
8bd70 2f 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f  / sizeof(Mem3Blo
8bd80 63 6b 29 29 20 2d 20 32 3b 0a 0a 20 20 2f 2a 20  ck)) - 2;..  /* 
8bd90 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  Initialize the m
8bda0 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 2a 2f 0a  aster block. */.
8bdb0 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20    mem3.szMaster 
8bdc0 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20  = mem3.nPool;.  
8bdd0 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20  mem3.mnMaster = 
8bde0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20  mem3.szMaster;. 
8bdf0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20   mem3.iMaster = 
8be00 31 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  1;.  mem3.aPool[
8be10 30 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  0].u.hdr.size4x 
8be20 3d 20 28 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  = (mem3.szMaster
8be30 3c 3c 32 29 20 2b 20 32 3b 0a 20 20 6d 65 6d 33  <<2) + 2;.  mem3
8be40 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f  .aPool[mem3.nPoo
8be50 6c 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  l].u.hdr.prevSiz
8be60 65 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a  e = mem3.nPool;.
8be70 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d    mem3.aPool[mem
8be80 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 73  3.nPool].u.hdr.s
8be90 69 7a 65 34 78 20 3d 20 31 3b 0a 0a 20 20 72 65  ize4x = 1;..  re
8bea0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
8beb0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69  }../*.** Deiniti
8bec0 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c  alize this modul
8bed0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
8bee0 64 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f 77  d memsys3Shutdow
8bef0 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  n(void *NotUsed)
8bf00 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
8bf10 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
8bf20 20 6d 65 6d 33 2e 6d 75 74 65 78 20 3d 20 30 3b   mem3.mutex = 0;
8bf30 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 0a 0a  .  return;.}....
8bf40 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66  /*.** Open the f
8bf50 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e  ile indicated an
8bf60 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66  d write a log of
8bf70 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d   all unfreed mem
8bf80 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  ory .** allocati
8bf90 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f  ons into that lo
8bfa0 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
8bfb0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
8bfc0 33 4d 65 6d 73 79 73 33 44 75 6d 70 28 63 6f 6e  3Memsys3Dump(con
8bfd0 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
8bfe0 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  me){.#ifdef SQLI
8bff0 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20  TE_DEBUG.  FILE 
8c000 2a 6f 75 74 3b 0a 20 20 75 33 32 20 69 2c 20 6a  *out;.  u32 i, j
8c010 3b 0a 20 20 75 33 32 20 73 69 7a 65 3b 0a 20 20  ;.  u32 size;.  
8c020 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30  if( zFilename==0
8c030 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d   || zFilename[0]
8c040 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 20 3d  ==0 ){.    out =
8c050 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c 73 65   stdout;.  }else
8c060 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70 65  {.    out = fope
8c070 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22  n(zFilename, "w"
8c080 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 3d 3d  );.    if( out==
8c090 30 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e  0 ){.      fprin
8c0a0 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55  tf(stderr, "** U
8c0b0 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20  nable to output 
8c0c0 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74  memory debug out
8c0d0 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e  put log: %s **\n
8c0e0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
8c0f0 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61           zFilena
8c100 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  me);.      retur
8c110 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d  n;.    }.  }.  m
8c120 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20  emsys3Enter();. 
8c130 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 43   fprintf(out, "C
8c140 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f  HUNKS:\n");.  fo
8c150 72 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d 33 2e 6e  r(i=1; i<=mem3.n
8c160 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65 2f 34 29  Pool; i+=size/4)
8c170 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d  {.    size = mem
8c180 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
8c190 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 20 69  dr.size4x;.    i
8c1a0 66 28 20 73 69 7a 65 2f 34 3c 3d 31 20 29 7b 0a  f( size/4<=1 ){.
8c1b0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
8c1c0 74 2c 20 22 25 70 20 73 69 7a 65 20 65 72 72 6f  t, "%p size erro
8c1d0 72 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f  r\n", &mem3.aPoo
8c1e0 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73  l[i]);.      ass
8c1f0 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20  ert( 0 );.      
8c200 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
8c210 20 69 66 28 20 28 73 69 7a 65 26 31 29 3d 3d 30   if( (size&1)==0
8c220 20 26 26 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69   && mem3.aPool[i
8c230 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72  +size/4-1].u.hdr
8c240 2e 70 72 65 76 53 69 7a 65 21 3d 73 69 7a 65 2f  .prevSize!=size/
8c250 34 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e  4 ){.      fprin
8c260 74 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c  tf(out, "%p tail
8c270 20 73 69 7a 65 20 64 6f 65 73 20 6e 6f 74 20 6d   size does not m
8c280 61 74 63 68 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61  atch\n", &mem3.a
8c290 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20  Pool[i]);.      
8c2a0 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20  assert( 0 );.   
8c2b0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
8c2c0 20 20 20 20 69 66 28 20 28 28 6d 65 6d 33 2e 61      if( ((mem3.a
8c2d0 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d  Pool[i+size/4-1]
8c2e0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29  .u.hdr.size4x&2)
8c2f0 3e 3e 31 29 21 3d 28 73 69 7a 65 26 31 29 20 29  >>1)!=(size&1) )
8c300 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
8c310 6f 75 74 2c 20 22 25 70 20 74 61 69 6c 20 63 68  out, "%p tail ch
8c320 65 63 6b 6f 75 74 20 62 69 74 20 69 73 20 69 6e  eckout bit is in
8c330 63 6f 72 72 65 63 74 5c 6e 22 2c 20 26 6d 65 6d  correct\n", &mem
8c340 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20  3.aPool[i]);.   
8c350 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a     assert( 0 );.
8c360 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
8c370 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 26   }.    if( size&
8c380 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e  1 ){.      fprin
8c390 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64 20  tf(out, "%p %6d 
8c3a0 62 79 74 65 73 20 63 68 65 63 6b 65 64 20 6f 75  bytes checked ou
8c3b0 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f  t\n", &mem3.aPoo
8c3c0 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38  l[i], (size/4)*8
8c3d0 2d 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  -8);.    }else{.
8c3e0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
8c3f0 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73  t, "%p %6d bytes
8c400 20 66 72 65 65 25 73 5c 6e 22 2c 20 26 6d 65 6d   free%s\n", &mem
8c410 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a  3.aPool[i], (siz
8c420 65 2f 34 29 2a 38 2d 38 2c 0a 20 20 20 20 20 20  e/4)*8-8,.      
8c430 20 20 20 20 20 20 20 20 20 20 20 20 69 3d 3d 6d              i==m
8c440 65 6d 33 2e 69 4d 61 73 74 65 72 20 3f 20 22 20  em3.iMaster ? " 
8c450 2a 2a 6d 61 73 74 65 72 2a 2a 22 20 3a 20 22 22  **master**" : ""
8c460 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  );.    }.  }.  f
8c470 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41  or(i=0; i<MX_SMA
8c480 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  LL-1; i++){.    
8c490 69 66 28 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c  if( mem3.aiSmall
8c4a0 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  [i]==0 ) continu
8c4b0 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f  e;.    fprintf(o
8c4c0 75 74 2c 20 22 73 6d 61 6c 6c 28 25 32 64 29 3a  ut, "small(%2d):
8c4d0 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a  ", i);.    for(j
8c4e0 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b   = mem3.aiSmall[
8c4f0 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e  i]; j>0; j=mem3.
8c500 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e  aPool[j].u.list.
8c510 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 72  next){.      fpr
8c520 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25  intf(out, " %p(%
8c530 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c  d)", &mem3.aPool
8c540 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  [j],.           
8c550 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a     (mem3.aPool[j
8c560 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8c570 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a  /4)*8-8);.    }.
8c580 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
8c590 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66   "\n"); .  }.  f
8c5a0 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 48  or(i=0; i<N_HASH
8c5b0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
8c5c0 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3d 3d  mem3.aiHash[i]==
8c5d0 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
8c5e0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8c5f0 68 61 73 68 28 25 32 64 29 3a 22 2c 20 69 29 3b  hash(%2d):", i);
8c600 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d  .    for(j = mem
8c610 33 2e 61 69 48 61 73 68 5b 69 5d 3b 20 6a 3e 30  3.aiHash[i]; j>0
8c620 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a  ; j=mem3.aPool[j
8c630 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a  ].u.list.next){.
8c640 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
8c650 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26 6d  t, " %p(%d)", &m
8c660 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20  em3.aPool[j],.  
8c670 20 20 20 20 20 20 20 20 20 20 20 20 28 6d 65 6d              (mem
8c680 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68  3.aPool[j-1].u.h
8c690 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38  dr.size4x/4)*8-8
8c6a0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72  );.    }.    fpr
8c6b0 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b  intf(out, "\n");
8c6c0 20 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28   .  }.  fprintf(
8c6d0 6f 75 74 2c 20 22 6d 61 73 74 65 72 3d 25 64 5c  out, "master=%d\
8c6e0 6e 22 2c 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72  n", mem3.iMaster
8c6f0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  );.  fprintf(out
8c700 2c 20 22 6e 6f 77 55 73 65 64 3d 25 64 5c 6e 22  , "nowUsed=%d\n"
8c710 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d  , mem3.nPool*8 -
8c720 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 38   mem3.szMaster*8
8c730 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  );.  fprintf(out
8c740 2c 20 22 6d 78 55 73 65 64 3d 25 64 5c 6e 22 2c  , "mxUsed=%d\n",
8c750 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20   mem3.nPool*8 - 
8c760 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 2a 38 29  mem3.mnMaster*8)
8c770 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
8c780 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74  x_leave(mem3.mut
8c790 65 78 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d  ex);.  if( out==
8c7a0 73 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66  stdout ){.    ff
8c7b0 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20  lush(stdout);.  
8c7c0 7d 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73  }else{.    fclos
8c7d0 65 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65 6c 73  e(out);.  }.#els
8c7e0 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  e.  UNUSED_PARAM
8c7f0 45 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  ETER(zFilename);
8c800 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
8c810 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
8c820 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e   the only routin
8c830 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77  e in this file w
8c840 69 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a  ith external .**
8c850 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20   linkage..**.** 
8c860 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77  Populate the low
8c870 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
8c880 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  location functio
8c890 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a  n pointers in.**
8c8a0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
8c8b0 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e  nfig.m with poin
8c8c0 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74  ters to the rout
8c8d0 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c  ines in this fil
8c8e0 65 2e 20 54 68 65 0a 2a 2a 20 61 72 67 75 6d 65  e. The.** argume
8c8f0 6e 74 73 20 73 70 65 63 69 66 79 20 74 68 65 20  nts specify the 
8c900 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20  block of memory 
8c910 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a 0a 2a 2a  to manage..**.**
8c920 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
8c930 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62 79 20   only called by 
8c940 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
8c950 2c 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65 0a  , and therefore.
8c960 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  ** is not requir
8c970 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73  ed to be threads
8c980 61 66 65 20 28 69 74 20 69 73 20 6e 6f 74 29 2e  afe (it is not).
8c990 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
8c9a0 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  TE const sqlite3
8c9b0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71  _mem_methods *sq
8c9c0 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79  lite3MemGetMemsy
8c9d0 73 33 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  s3(void){.  stat
8c9e0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
8c9f0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 65 6d  _mem_methods mem
8ca00 70 6f 6f 6c 4d 65 74 68 6f 64 73 20 3d 20 7b 0a  poolMethods = {.
8ca10 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 61 6c 6c       memsys3Mall
8ca20 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33  oc,.     memsys3
8ca30 46 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79  Free,.     memsy
8ca40 73 33 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20  s3Realloc,.     
8ca50 6d 65 6d 73 79 73 33 53 69 7a 65 2c 0a 20 20 20  memsys3Size,.   
8ca60 20 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64 75 70    memsys3Roundup
8ca70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 49 6e  ,.     memsys3In
8ca80 69 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33  it,.     memsys3
8ca90 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30  Shutdown,.     0
8caa0 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 26  .  };.  return &
8cab0 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 3b 0a  mempoolMethods;.
8cac0 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
8cad0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
8cae0 53 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  S3 */../********
8caf0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
8cb00 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m3.c ***********
8cb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cb30 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
8cb40 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
8cb50 65 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e mem5.c *******
8cb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cb80 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
8cb90 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a  7 October 14.**.
8cba0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
8cbb0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
8cbc0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
8cbd0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
8cbe0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
8cbf0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
8cc00 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
8cc10 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
8cc20 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
8cc30 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
8cc40 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
8cc50 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
8cc60 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
8cc70 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
8cc80 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
8cc90 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
8cca0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
8ccb0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
8ccc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ccd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ccf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
8cd00 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
8cd10 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
8cd20 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
8cd30 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c   a memory.** all
8cd40 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
8cd50 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c  m for use by SQL
8cd60 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ite. .**.** This
8cd70 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
8cd80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
8cd90 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74  n subsystem omit
8cda0 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20  s all.** use of 
8cdb0 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 61 70  malloc(). The ap
8cdc0 70 6c 69 63 61 74 69 6f 6e 20 67 69 76 65 73 20  plication gives 
8cdd0 53 51 4c 69 74 65 20 61 20 62 6c 6f 63 6b 20 6f  SQLite a block o
8cde0 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f  f memory.** befo
8cdf0 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  re calling sqlit
8ce00 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
8ce10 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63  from which alloc
8ce20 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61  ations.** are ma
8ce30 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20  de and returned 
8ce40 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29  by the xMalloc()
8ce50 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20   and xRealloc() 
8ce60 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
8ce70 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65  ons. Once sqlite
8ce80 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68  3_initialize() h
8ce90 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a  as been called,.
8cea0 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  ** the amount of
8ceb0 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c   memory availabl
8cec0 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66  e to SQLite is f
8ced0 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a  ixed and cannot.
8cee0 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a  ** be changed..*
8cef0 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f  *.** This versio
8cf00 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  n of the memory 
8cf10 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
8cf20 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64  stem is included
8cf30 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64  .** in the build
8cf40 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f   only if SQLITE_
8cf50 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69  ENABLE_MEMSYS5 i
8cf60 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  s defined..**.**
8cf70 20 54 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c   This memory all
8cf80 6f 63 61 74 6f 72 20 75 73 65 73 20 74 68 65 20  ocator uses the 
8cf90 66 6f 6c 6c 6f 77 69 6e 67 20 61 6c 67 6f 72 69  following algori
8cfa0 74 68 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20  thm:.**.**   1. 
8cfb0 20 41 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   All memory allo
8cfc0 63 61 74 69 6f 6e 73 20 73 69 7a 65 73 20 61 72  cations sizes ar
8cfd0 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20  e rounded up to 
8cfe0 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2a  a power of 2..**
8cff0 0a 2a 2a 20 20 20 32 2e 20 20 49 66 20 74 77 6f  .**   2.  If two
8d000 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 62   adjacent free b
8d010 6c 6f 63 6b 73 20 61 72 65 20 74 68 65 20 68 61  locks are the ha
8d020 6c 76 65 73 20 6f 66 20 61 20 6c 61 72 67 65 72  lves of a larger
8d030 20 62 6c 6f 63 6b 2c 0a 2a 2a 20 20 20 20 20 20   block,.**      
8d040 20 74 68 65 6e 20 74 68 65 20 74 77 6f 20 62 6c   then the two bl
8d050 6f 63 6b 73 20 61 72 65 20 63 6f 61 6c 65 73 65  ocks are coalese
8d060 64 20 69 6e 74 6f 20 74 68 65 20 73 69 6e 67 6c  d into the singl
8d070 65 20 6c 61 72 67 65 72 20 62 6c 6f 63 6b 2e 0a  e larger block..
8d080 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 4e 65 77 20  **.**   3.  New 
8d090 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61  memory is alloca
8d0a0 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69 72  ted from the fir
8d0b0 73 74 20 61 76 61 69 6c 61 62 6c 65 20 66 72 65  st available fre
8d0c0 65 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54  e block..**.** T
8d0d0 68 69 73 20 61 6c 67 6f 72 69 74 68 6d 20 69 73  his algorithm is
8d0e0 20 64 65 73 63 72 69 62 65 64 20 69 6e 3a 20 4a   described in: J
8d0f0 2e 20 4d 2e 20 52 6f 62 73 6f 6e 2e 20 22 42 6f  . M. Robson. "Bo
8d100 75 6e 64 73 20 66 6f 72 20 53 6f 6d 65 20 46 75  unds for Some Fu
8d110 6e 63 74 69 6f 6e 73 0a 2a 2a 20 43 6f 6e 63 65  nctions.** Conce
8d120 72 6e 69 6e 67 20 44 79 6e 61 6d 69 63 20 53 74  rning Dynamic St
8d130 6f 72 61 67 65 20 41 6c 6c 6f 63 61 74 69 6f 6e  orage Allocation
8d140 22 2e 20 4a 6f 75 72 6e 61 6c 20 6f 66 20 74 68  ". Journal of th
8d150 65 20 41 73 73 6f 63 69 61 74 69 6f 6e 20 66 6f  e Association fo
8d160 72 0a 2a 2a 20 43 6f 6d 70 75 74 69 6e 67 20 4d  r.** Computing M
8d170 61 63 68 69 6e 65 72 79 2c 20 56 6f 6c 75 6d 65  achinery, Volume
8d180 20 32 31 2c 20 4e 75 6d 62 65 72 20 38 2c 20 4a   21, Number 8, J
8d190 75 6c 79 20 31 39 37 34 2c 20 70 61 67 65 73 20  uly 1974, pages 
8d1a0 34 39 31 2d 34 39 39 2e 0a 2a 2a 20 0a 2a 2a 20  491-499..** .** 
8d1b0 4c 65 74 20 6e 20 62 65 20 74 68 65 20 73 69 7a  Let n be the siz
8d1c0 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74  e of the largest
8d1d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 69 76 69   allocation divi
8d1e0 64 65 64 20 62 79 20 74 68 65 20 6d 69 6e 69 6d  ded by the minim
8d1f0 75 6d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  um.** allocation
8d200 20 73 69 7a 65 20 28 61 66 74 65 72 20 72 6f 75   size (after rou
8d210 6e 64 69 6e 67 20 61 6c 6c 20 73 69 7a 65 73 20  nding all sizes 
8d220 75 70 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66  up to a power of
8d230 20 32 2e 29 20 20 4c 65 74 20 4d 0a 2a 2a 20 62   2.)  Let M.** b
8d240 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d  e the maximum am
8d250 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65  ount of memory e
8d260 76 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  ver outstanding 
8d270 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 4c 65  at one time.  Le
8d280 74 0a 2a 2a 20 4e 20 62 65 20 74 68 65 20 74 6f  t.** N be the to
8d290 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  tal amount of me
8d2a0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66  mory available f
8d2b0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20  or allocation.  
8d2c0 52 6f 62 73 6f 6e 0a 2a 2a 20 70 72 6f 76 65 64  Robson.** proved
8d2d0 20 74 68 61 74 20 74 68 69 73 20 6d 65 6d 6f 72   that this memor
8d2e0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 77 69 6c 6c  y allocator will
8d2f0 20 6e 65 76 65 72 20 62 72 65 61 6b 64 6f 77 6e   never breakdown
8d300 20 64 75 65 20 74 6f 20 0a 2a 2a 20 66 72 61 67   due to .** frag
8d310 6d 65 6e 74 61 74 69 6f 6e 20 61 73 20 6c 6f 6e  mentation as lon
8d320 67 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69  g as the followi
8d330 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 6f  ng constraint ho
8d340 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  lds:.**.**      
8d350 4e 20 3e 3d 20 20 4d 2a 28 31 20 2b 20 6c 6f 67  N >=  M*(1 + log
8d360 32 28 6e 29 2f 32 29 20 2d 20 6e 20 2b 20 31 0a  2(n)/2) - n + 1.
8d370 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
8d380 33 5f 73 74 61 74 75 73 28 29 20 6c 6f 67 69 63  3_status() logic
8d390 20 74 72 61 63 6b 73 20 74 68 65 20 6d 61 78 69   tracks the maxi
8d3a0 6d 75 6d 20 76 61 6c 75 65 73 20 6f 66 20 6e 20  mum values of n 
8d3b0 61 6e 64 20 4d 20 73 6f 0a 2a 2a 20 74 68 61 74  and M so.** that
8d3c0 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20   an application 
8d3d0 63 61 6e 2c 20 61 74 20 61 6e 79 20 74 69 6d 65  can, at any time
8d3e0 2c 20 76 65 72 69 66 79 20 74 68 69 73 20 63 6f  , verify this co
8d3f0 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a 0a 2f 2a  nstraint..*/../*
8d400 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
8d410 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
8d420 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64  llocator is used
8d430 20 6f 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a 20 53   only when .** S
8d440 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
8d450 53 59 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e  SYS5 is defined.
8d460 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
8d470 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35  E_ENABLE_MEMSYS5
8d480 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69 6d 75  ../*.** A minimu
8d490 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  m allocation is 
8d4a0 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
8d4b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
8d4c0 75 63 74 75 72 65 2e 0a 2a 2a 20 4c 61 72 67 65  ucture..** Large
8d4d0 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72  r allocations ar
8d4e0 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 74 68  e an array of th
8d4f0 65 73 65 20 73 74 72 75 63 74 75 72 65 73 20 77  ese structures w
8d500 68 65 72 65 20 74 68 65 0a 2a 2a 20 73 69 7a 65  here the.** size
8d510 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 69 73   of the array is
8d520 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a   a power of 2..*
8d530 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66  *.** The size of
8d540 20 74 68 69 73 20 6f 62 6a 65 63 74 20 6d 75 73   this object mus
8d550 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20  t be a power of 
8d560 74 77 6f 2e 20 20 54 68 61 74 20 66 61 63 74 20  two.  That fact 
8d570 69 73 0a 2a 2a 20 76 65 72 69 66 69 65 64 20 69  is.** verified i
8d580 6e 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 29 2e  n memsys5Init().
8d590 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
8d5a0 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65 6d 35  ct Mem5Link Mem5
8d5b0 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d  Link;.struct Mem
8d5c0 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20 6e 65  5Link {.  int ne
8d5d0 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  xt;       /* Ind
8d5e0 65 78 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20  ex of next free 
8d5f0 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74 20 70  chunk */.  int p
8d600 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e  rev;       /* In
8d610 64 65 78 20 6f 66 20 70 72 65 76 69 6f 75 73 20  dex of previous 
8d620 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b  free chunk */.};
8d630 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20  ../*.** Maximum 
8d640 73 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c 6c 6f  size of any allo
8d650 63 61 74 69 6f 6e 20 69 73 20 28 28 31 3c 3c 4c  cation is ((1<<L
8d660 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 73 7a 41 74  OGMAX)*mem5.szAt
8d670 6f 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 6d 65  om). Since.** me
8d680 6d 35 2e 73 7a 41 74 6f 6d 20 69 73 20 61 6c 77  m5.szAtom is alw
8d690 61 79 73 20 61 74 20 6c 65 61 73 74 20 38 20 61  ays at least 8 a
8d6a0 6e 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65  nd 32-bit intege
8d6b0 72 73 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a 20  rs are used,.** 
8d6c0 69 74 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c  it is not actual
8d6d0 6c 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72  ly possible to r
8d6e0 65 61 63 68 20 74 68 69 73 20 6c 69 6d 69 74 2e  each this limit.
8d6f0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 4f 47 4d  .*/.#define LOGM
8d700 41 58 20 33 30 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73  AX 30../*.** Mas
8d710 6b 73 20 75 73 65 64 20 66 6f 72 20 6d 65 6d 35  ks used for mem5
8d720 2e 61 43 74 72 6c 5b 5d 20 65 6c 65 6d 65 6e 74  .aCtrl[] element
8d730 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54  s..*/.#define CT
8d740 52 4c 5f 4c 4f 47 53 49 5a 45 20 20 30 78 31 66  RL_LOGSIZE  0x1f
8d750 20 20 20 20 2f 2a 20 4c 6f 67 32 20 53 69 7a 65      /* Log2 Size
8d760 20 6f 66 20 74 68 69 73 20 62 6c 6f 63 6b 20 2a   of this block *
8d770 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 46  /.#define CTRL_F
8d780 52 45 45 20 20 20 20 20 30 78 32 30 20 20 20 20  REE     0x20    
8d790 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 74 20 63  /* True if not c
8d7a0 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 0a 2f  hecked out */../
8d7b0 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20  *.** All of the 
8d7c0 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73  static variables
8d7d0 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f   used by this mo
8d7e0 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74  dule are collect
8d7f0 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e  ed.** into a sin
8d800 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61  gle structure na
8d810 6d 65 64 20 22 6d 65 6d 35 22 2e 20 20 54 68 69  med "mem5".  Thi
8d820 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65  s is to keep the
8d830 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61  .** static varia
8d840 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61  bles organized a
8d850 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d  nd to reduce nam
8d860 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e  espace pollution
8d870 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f  .** when this mo
8d880 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64  dule is combined
8d890 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74   with other in t
8d8a0 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e  he amalgamation.
8d8b0 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54  .*/.static SQLIT
8d8c0 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d  E_WSD struct Mem
8d8d0 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20  5Global {.  /*. 
8d8e0 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c   ** Memory avail
8d8f0 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74  able for allocat
8d900 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 73  ion.  */.  int s
8d910 7a 41 74 6f 6d 3b 20 20 20 20 20 20 2f 2a 20 53  zAtom;      /* S
8d920 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 6c 65  mallest possible
8d930 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62   allocation in b
8d940 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ytes */.  int nB
8d950 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 4e 75  lock;      /* Nu
8d960 6d 62 65 72 20 6f 66 20 73 7a 41 74 6f 6d 20 73  mber of szAtom s
8d970 69 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e 20 7a  ized blocks in z
8d980 50 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a 7a 50  Pool */.  u8 *zP
8d990 6f 6f 6c 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65  ool;       /* Me
8d9a0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74  mory available t
8d9b0 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 2a  o be allocated *
8d9c0 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d  /.  .  /*.  ** M
8d9d0 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20  utex to control 
8d9e0 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65  access to the me
8d9f0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
8da00 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a  subsystem..  */.
8da10 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
8da20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20  *mutex;..  /*.  
8da30 2a 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 73  ** Performance s
8da40 74 61 74 69 73 74 69 63 73 0a 20 20 2a 2f 0a 20  tatistics.  */. 
8da50 20 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 20 20   u64 nAlloc;    
8da60 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
8da70 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f  mber of calls to
8da80 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 36 34   malloc */.  u64
8da90 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20 20 20   totalAlloc;    
8daa0 20 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61 6c 6c   /* Total of all
8dab0 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 2d 20   malloc calls - 
8dac0 69 6e 63 6c 75 64 65 73 20 69 6e 74 65 72 6e 61  includes interna
8dad0 6c 20 66 72 61 67 20 2a 2f 0a 20 20 75 36 34 20  l frag */.  u64 
8dae0 74 6f 74 61 6c 45 78 63 65 73 73 3b 20 20 20 20  totalExcess;    
8daf0 2f 2a 20 54 6f 74 61 6c 20 69 6e 74 65 72 6e 61  /* Total interna
8db00 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20  l fragmentation 
8db10 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74  */.  u32 current
8db20 4f 75 74 3b 20 20 20 20 20 2f 2a 20 43 75 72 72  Out;     /* Curr
8db30 65 6e 74 20 63 68 65 63 6b 6f 75 74 2c 20 69 6e  ent checkout, in
8db40 63 6c 75 64 69 6e 67 20 69 6e 74 65 72 6e 61 6c  cluding internal
8db50 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a   fragmentation *
8db60 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 43  /.  u32 currentC
8db70 6f 75 6e 74 3b 20 20 20 2f 2a 20 43 75 72 72 65  ount;   /* Curre
8db80 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 73  nt number of dis
8db90 74 69 6e 63 74 20 63 68 65 63 6b 6f 75 74 73 20  tinct checkouts 
8dba0 2a 2f 0a 20 20 75 33 32 20 6d 61 78 4f 75 74 3b  */.  u32 maxOut;
8dbb0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
8dbc0 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75  mum instantaneou
8dbd0 73 20 63 75 72 72 65 6e 74 4f 75 74 20 2a 2f 0a  s currentOut */.
8dbe0 20 20 75 33 32 20 6d 61 78 43 6f 75 6e 74 3b 20    u32 maxCount; 
8dbf0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
8dc00 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63   instantaneous c
8dc10 75 72 72 65 6e 74 43 6f 75 6e 74 20 2a 2f 0a 20  urrentCount */. 
8dc20 20 75 33 32 20 6d 61 78 52 65 71 75 65 73 74 3b   u32 maxRequest;
8dc30 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20       /* Largest 
8dc40 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 65 78 63 6c  allocation (excl
8dc50 75 73 69 76 65 20 6f 66 20 69 6e 74 65 72 6e 61  usive of interna
8dc60 6c 20 66 72 61 67 29 20 2a 2f 0a 20 20 0a 20 20  l frag) */.  .  
8dc70 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74 73 20 6f 66  /*.  ** Lists of
8dc80 20 66 72 65 65 20 62 6c 6f 63 6b 73 2e 20 20 61   free blocks.  a
8dc90 69 46 72 65 65 6c 69 73 74 5b 30 5d 20 69 73 20  iFreelist[0] is 
8dca0 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 62  a list of free b
8dcb0 6c 6f 63 6b 73 20 6f 66 0a 20 20 2a 2a 20 73 69  locks of.  ** si
8dcc0 7a 65 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2e 20  ze mem5.szAtom. 
8dcd0 20 61 69 46 72 65 65 6c 69 73 74 5b 31 5d 20 68   aiFreelist[1] h
8dce0 6f 6c 64 73 20 62 6c 6f 63 6b 73 20 6f 66 20 73  olds blocks of s
8dcf0 69 7a 65 20 73 7a 41 74 6f 6d 2a 32 2e 0a 20 20  ize szAtom*2..  
8dd00 2a 2a 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e  ** and so forth.
8dd10 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69 46 72  .  */.  int aiFr
8dd20 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d  eelist[LOGMAX+1]
8dd30 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 70 61  ;..  /*.  ** Spa
8dd40 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e 67 20  ce for tracking 
8dd50 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61 72 65  which blocks are
8dd60 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64   checked out and
8dd70 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f   the size.  ** o
8dd80 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20 20 4f  f each block.  O
8dd90 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c 6f 63  ne byte per bloc
8dda0 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a 61 43  k..  */.  u8 *aC
8ddb0 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 20 3d 20 7b  trl;..} mem5 = {
8ddc0 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63   0 };../*.** Acc
8ddd0 65 73 73 20 74 68 65 20 73 74 61 74 69 63 20 76  ess the static v
8dde0 61 72 69 61 62 6c 65 20 74 68 72 6f 75 67 68 20  ariable through 
8ddf0 61 20 6d 61 63 72 6f 20 66 6f 72 20 53 51 4c 49  a macro for SQLI
8de00 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 2a 2f 0a 23  TE_OMIT_WSD.*/.#
8de10 64 65 66 69 6e 65 20 6d 65 6d 35 20 47 4c 4f 42  define mem5 GLOB
8de20 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 35 47 6c  AL(struct Mem5Gl
8de30 6f 62 61 6c 2c 20 6d 65 6d 35 29 0a 0a 2f 2a 0a  obal, mem5)../*.
8de40 2a 2a 20 41 73 73 75 6d 69 6e 67 20 6d 65 6d 35  ** Assuming mem5
8de50 2e 7a 50 6f 6f 6c 20 69 73 20 64 69 76 69 64 65  .zPool is divide
8de60 64 20 75 70 20 69 6e 74 6f 20 61 6e 20 61 72 72  d up into an arr
8de70 61 79 20 6f 66 20 4d 65 6d 35 4c 69 6e 6b 0a 2a  ay of Mem5Link.*
8de80 2a 20 73 74 72 75 63 74 75 72 65 73 2c 20 72 65  * structures, re
8de90 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
8dea0 6f 20 74 68 65 20 69 64 78 2d 74 68 20 73 75 63  o the idx-th suc
8deb0 68 20 6c 69 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e  h lik..*/.#defin
8dec0 65 20 4d 45 4d 35 4c 49 4e 4b 28 69 64 78 29 20  e MEM5LINK(idx) 
8ded0 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a 29 28 26 6d  ((Mem5Link *)(&m
8dee0 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 64 78 29 2a  em5.zPool[(idx)*
8def0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d 29 29 0a 0a  mem5.szAtom]))..
8df00 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65  /*.** Unlink the
8df10 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61   chunk at mem5.a
8df20 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73  Pool[i] from lis
8df30 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c  t it is currentl
8df40 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20 73 68 6f  y.** on.  It sho
8df50 75 6c 64 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20  uld be found on 
8df60 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
8df70 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f 0a 73 74  iLogsize]..*/.st
8df80 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
8df90 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69 2c 20 69  5Unlink(int i, i
8dfa0 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20  nt iLogsize){.  
8dfb0 69 6e 74 20 6e 65 78 74 2c 20 70 72 65 76 3b 0a  int next, prev;.
8dfc0 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
8dfd0 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20  & i<mem5.nBlock 
8dfe0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f  );.  assert( iLo
8dff0 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67  gsize>=0 && iLog
8e000 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a  size<=LOGMAX );.
8e010 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e    assert( (mem5.
8e020 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c 5f  aCtrl[i] & CTRL_
8e030 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 69  LOGSIZE)==iLogsi
8e040 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 74 20 3d 20  ze );..  next = 
8e050 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78  MEM5LINK(i)->nex
8e060 74 3b 0a 20 20 70 72 65 76 20 3d 20 4d 45 4d 35  t;.  prev = MEM5
8e070 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 3b 0a 20  LINK(i)->prev;. 
8e080 20 69 66 28 20 70 72 65 76 3c 30 20 29 7b 0a 20   if( prev<0 ){. 
8e090 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69     mem5.aiFreeli
8e0a0 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 6e  st[iLogsize] = n
8e0b0 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ext;.  }else{.  
8e0c0 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 72 65 76 29    MEM5LINK(prev)
8e0d0 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20  ->next = next;. 
8e0e0 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 3e 3d 30   }.  if( next>=0
8e0f0 20 29 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b   ){.    MEM5LINK
8e100 28 6e 65 78 74 29 2d 3e 70 72 65 76 20 3d 20 70  (next)->prev = p
8e110 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  rev;.  }.}../*.*
8e120 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b  * Link the chunk
8e130 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69   at mem5.aPool[i
8e140 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20  ] so that is on 
8e150 74 68 65 20 69 4c 6f 67 73 69 7a 65 0a 2a 2a 20  the iLogsize.** 
8e160 66 72 65 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74  free list..*/.st
8e170 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
8e180 35 4c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74  5Link(int i, int
8e190 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e   iLogsize){.  in
8e1a0 74 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 73  t x;.  assert( s
8e1b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
8e1c0 64 28 6d 65 6d 35 2e 6d 75 74 65 78 29 20 29 3b  d(mem5.mutex) );
8e1d0 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
8e1e0 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  && i<mem5.nBlock
8e1f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c   );.  assert( iL
8e200 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f  ogsize>=0 && iLo
8e210 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b  gsize<=LOGMAX );
8e220 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 35  .  assert( (mem5
8e230 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c  .aCtrl[i] & CTRL
8e240 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73  _LOGSIZE)==iLogs
8e250 69 7a 65 20 29 3b 0a 0a 20 20 78 20 3d 20 4d 45  ize );..  x = ME
8e260 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 20  M5LINK(i)->next 
8e270 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73  = mem5.aiFreelis
8e280 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 4d  t[iLogsize];.  M
8e290 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76  EM5LINK(i)->prev
8e2a0 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 78 3e 3d   = -1;.  if( x>=
8e2b0 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
8e2c0 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29   x<mem5.nBlock )
8e2d0 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 78  ;.    MEM5LINK(x
8e2e0 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d  )->prev = i;.  }
8e2f0 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69  .  mem5.aiFreeli
8e300 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 69  st[iLogsize] = i
8e310 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
8e320 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74  e STATIC_MEM mut
8e330 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  ex is not alread
8e340 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 6e 20 69  y held, obtain i
8e350 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 74 65 78  t now. The mutex
8e360 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 61 64 79  .** will already
8e370 20 62 65 20 68 65 6c 64 20 28 6f 62 74 61 69 6e   be held (obtain
8e380 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6d 61  ed by code in ma
8e390 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a 20 73 71  lloc.c) if.** sq
8e3a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
8e3b0 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 20 74 72  g.bMemStat is tr
8e3c0 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ue..*/.static vo
8e3d0 69 64 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  id memsys5Enter(
8e3e0 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33  void){.  sqlite3
8e3f0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
8e400 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74  5.mutex);.}.stat
8e410 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c  ic void memsys5L
8e420 65 61 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71  eave(void){.  sq
8e430 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
8e440 65 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d  e(mem5.mutex);.}
8e450 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
8e460 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75  he size of an ou
8e470 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61  tstanding alloca
8e480 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20  tion, in bytes. 
8e490 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74   The.** size ret
8e4a0 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20  urned omits the 
8e4b0 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 6f 76  8-byte header ov
8e4c0 65 72 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e  erhead.  This on
8e4d0 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20  ly.** works for 
8e4e0 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20  chunks that are 
8e4f0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65  currently checke
8e500 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  d out..*/.static
8e510 20 69 6e 74 20 6d 65 6d 73 79 73 35 53 69 7a 65   int memsys5Size
8e520 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 6e 74  (void *p){.  int
8e530 20 69 53 69 7a 65 20 3d 20 30 3b 0a 20 20 69 66   iSize = 0;.  if
8e540 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ( p ){.    int i
8e550 20 3d 20 28 28 75 38 20 2a 29 70 2d 6d 65 6d 35   = ((u8 *)p-mem5
8e560 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 73 7a 41  .zPool)/mem5.szA
8e570 74 6f 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  tom;.    assert(
8e580 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e   i>=0 && i<mem5.
8e590 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 69 53  nBlock );.    iS
8e5a0 69 7a 65 20 3d 20 6d 65 6d 35 2e 73 7a 41 74 6f  ize = mem5.szAto
8e5b0 6d 20 2a 20 28 31 20 3c 3c 20 28 6d 65 6d 35 2e  m * (1 << (mem5.
8e5c0 61 43 74 72 6c 5b 69 5d 26 43 54 52 4c 5f 4c 4f  aCtrl[i]&CTRL_LO
8e5d0 47 53 49 5a 45 29 29 3b 0a 20 20 7d 0a 20 20 72  GSIZE));.  }.  r
8e5e0 65 74 75 72 6e 20 69 53 69 7a 65 3b 0a 7d 0a 0a  eturn iSize;.}..
8e5f0 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 66  /*.** Find the f
8e600 69 72 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68  irst entry on th
8e610 65 20 66 72 65 65 6c 69 73 74 20 69 4c 6f 67 73  e freelist iLogs
8e620 69 7a 65 2e 20 20 55 6e 6c 69 6e 6b 20 74 68 61  ize.  Unlink tha
8e630 74 0a 2a 2a 20 65 6e 74 72 79 20 61 6e 64 20 72  t.** entry and r
8e640 65 74 75 72 6e 20 69 74 73 20 69 6e 64 65 78 2e  eturn its index.
8e650 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20   .*/.static int 
8e660 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72  memsys5UnlinkFir
8e670 73 74 28 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29  st(int iLogsize)
8e680 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
8e690 20 69 46 69 72 73 74 3b 0a 0a 20 20 61 73 73 65   iFirst;..  asse
8e6a0 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20  rt( iLogsize>=0 
8e6b0 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47  && iLogsize<=LOG
8e6c0 4d 41 58 20 29 3b 0a 20 20 69 20 3d 20 69 46 69  MAX );.  i = iFi
8e6d0 72 73 74 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65  rst = mem5.aiFre
8e6e0 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b  elist[iLogsize];
8e6f0 0a 20 20 61 73 73 65 72 74 28 20 69 46 69 72 73  .  assert( iFirs
8e700 74 3e 3d 30 20 29 3b 0a 20 20 77 68 69 6c 65 28  t>=0 );.  while(
8e710 20 69 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20   i>0 ){.    if( 
8e720 69 3c 69 46 69 72 73 74 20 29 20 69 46 69 72 73  i<iFirst ) iFirs
8e730 74 20 3d 20 69 3b 0a 20 20 20 20 69 20 3d 20 4d  t = i;.    i = M
8e740 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74  EM5LINK(i)->next
8e750 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 55  ;.  }.  memsys5U
8e760 6e 6c 69 6e 6b 28 69 46 69 72 73 74 2c 20 69 4c  nlink(iFirst, iL
8e770 6f 67 73 69 7a 65 29 3b 0a 20 20 72 65 74 75 72  ogsize);.  retur
8e780 6e 20 69 46 69 72 73 74 3b 0a 7d 0a 0a 2f 2a 0a  n iFirst;.}../*.
8e790 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63  ** Return a bloc
8e7a0 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61  k of memory of a
8e7b0 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69  t least nBytes i
8e7c0 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72  n size..** Retur
8e7d0 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65  n NULL if unable
8e7e0 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69  .  Return NULL i
8e7f0 66 20 6e 42 79 74 65 73 3d 3d 30 2e 0a 2a 2a 0a  f nBytes==0..**.
8e800 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75  ** The caller gu
8e810 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 6e 42  arantees that nB
8e820 79 74 65 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a  yte positive..**
8e830 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 68  .** The caller h
8e840 61 73 20 6f 62 74 61 69 6e 65 64 20 61 20 6d 75  as obtained a mu
8e850 74 65 78 20 70 72 69 6f 72 20 74 6f 20 69 6e 76  tex prior to inv
8e860 6f 6b 69 6e 67 20 74 68 69 73 0a 2a 2a 20 72 6f  oking this.** ro
8e870 75 74 69 6e 65 20 73 6f 20 74 68 65 72 65 20 69  utine so there i
8e880 73 20 6e 65 76 65 72 20 61 6e 79 20 63 68 61 6e  s never any chan
8e890 63 65 20 74 68 61 74 20 74 77 6f 20 6f 72 20 6d  ce that two or m
8e8a0 6f 72 65 0a 2a 2a 20 74 68 72 65 61 64 73 20 63  ore.** threads c
8e8b0 61 6e 20 62 65 20 69 6e 20 74 68 69 73 20 72 6f  an be in this ro
8e8c0 75 74 69 6e 65 20 61 74 20 74 68 65 20 73 61 6d  utine at the sam
8e8d0 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69  e time..*/.stati
8e8e0 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d  c void *memsys5M
8e8f0 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69 6e 74 20  allocUnsafe(int 
8e900 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 69 3b  nByte){.  int i;
8e910 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
8e920 64 65 78 20 6f 66 20 61 20 6d 65 6d 35 2e 61 50  dex of a mem5.aP
8e930 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 2f 0a 20 20  ool[] slot */.  
8e940 69 6e 74 20 69 42 69 6e 3b 20 20 20 20 20 20 20  int iBin;       
8e950 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 6d   /* Index into m
8e960 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 5d  em5.aiFreelist[]
8e970 20 2a 2f 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53   */.  int iFullS
8e980 7a 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f  z;     /* Size o
8e990 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75  f allocation rou
8e9a0 6e 64 65 64 20 75 70 20 74 6f 20 70 6f 77 65 72  nded up to power
8e9b0 20 6f 66 20 32 20 2a 2f 0a 20 20 69 6e 74 20 69   of 2 */.  int i
8e9c0 4c 6f 67 73 69 7a 65 3b 20 20 20 20 2f 2a 20 4c  Logsize;    /* L
8e9d0 6f 67 32 20 6f 66 20 69 46 75 6c 6c 53 7a 2f 50  og2 of iFullSz/P
8e9e0 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a 20 20 2f 2a  OW2_MIN */..  /*
8e9f0 20 6e 42 79 74 65 20 6d 75 73 74 20 62 65 20 61   nByte must be a
8ea00 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20 61   positive */.  a
8ea10 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29  ssert( nByte>0 )
8ea20 3b 0a 0a 20 20 2f 2a 20 4b 65 65 70 20 74 72 61  ;..  /* Keep tra
8ea30 63 6b 20 6f 66 20 74 68 65 20 6d 61 78 69 6d 75  ck of the maximu
8ea40 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  m allocation req
8ea50 75 65 73 74 2e 20 20 45 76 65 6e 20 75 6e 66 75  uest.  Even unfu
8ea60 6c 66 69 6c 6c 65 64 0a 20 20 2a 2a 20 72 65 71  lfilled.  ** req
8ea70 75 65 73 74 73 20 61 72 65 20 63 6f 75 6e 74 65  uests are counte
8ea80 64 20 2a 2f 0a 20 20 69 66 28 20 28 75 33 32 29  d */.  if( (u32)
8ea90 6e 42 79 74 65 3e 6d 65 6d 35 2e 6d 61 78 52 65  nByte>mem5.maxRe
8eaa0 71 75 65 73 74 20 29 7b 0a 20 20 20 20 6d 65 6d  quest ){.    mem
8eab0 35 2e 6d 61 78 52 65 71 75 65 73 74 20 3d 20 6e  5.maxRequest = n
8eac0 42 79 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Byte;.  }..  /* 
8ead0 41 62 6f 72 74 20 69 66 20 74 68 65 20 72 65 71  Abort if the req
8eae0 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f  uested allocatio
8eaf0 6e 20 73 69 7a 65 20 69 73 20 6c 61 72 67 65 72  n size is larger
8eb00 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65 73   than the larges
8eb10 74 0a 20 20 2a 2a 20 70 6f 77 65 72 20 6f 66 20  t.  ** power of 
8eb20 74 77 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20  two that we can 
8eb30 72 65 70 72 65 73 65 6e 74 20 75 73 69 6e 67 20  represent using 
8eb40 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  32-bit signed in
8eb50 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69  tegers..  */.  i
8eb60 66 28 20 6e 42 79 74 65 20 3e 20 30 78 34 30 30  f( nByte > 0x400
8eb70 30 30 30 30 30 20 29 7b 0a 20 20 20 20 72 65 74  00000 ){.    ret
8eb80 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 0;.  }..  /*
8eb90 20 52 6f 75 6e 64 20 6e 42 79 74 65 20 75 70 20   Round nByte up 
8eba0 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69  to the next vali
8ebb0 64 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 2a  d power of two *
8ebc0 2f 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d  /.  for(iFullSz=
8ebd0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2c 20 69 4c 6f  mem5.szAtom, iLo
8ebe0 67 73 69 7a 65 3d 30 3b 20 69 46 75 6c 6c 53 7a  gsize=0; iFullSz
8ebf0 3c 6e 42 79 74 65 3b 20 69 46 75 6c 6c 53 7a 20  <nByte; iFullSz 
8ec00 2a 3d 20 32 2c 20 69 4c 6f 67 73 69 7a 65 2b 2b  *= 2, iLogsize++
8ec10 29 7b 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  ){}..  /* Make s
8ec20 75 72 65 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c  ure mem5.aiFreel
8ec30 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 63 6f  ist[iLogsize] co
8ec40 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20  ntains at least 
8ec50 6f 6e 65 20 66 72 65 65 0a 20 20 2a 2a 20 62 6c  one free.  ** bl
8ec60 6f 63 6b 2e 20 20 49 66 20 6e 6f 74 2c 20 74 68  ock.  If not, th
8ec70 65 6e 20 73 70 6c 69 74 20 61 20 62 6c 6f 63 6b  en split a block
8ec80 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6c 61 72   of the next lar
8ec90 67 65 72 20 70 6f 77 65 72 20 6f 66 0a 20 20 2a  ger power of.  *
8eca0 2a 20 74 77 6f 20 69 6e 20 6f 72 64 65 72 20 74  * two in order t
8ecb0 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 66  o create a new f
8ecc0 72 65 65 20 62 6c 6f 63 6b 20 6f 66 20 73 69 7a  ree block of siz
8ecd0 65 20 69 4c 6f 67 73 69 7a 65 2e 0a 20 20 2a 2f  e iLogsize..  */
8ece0 0a 20 20 66 6f 72 28 69 42 69 6e 3d 69 4c 6f 67  .  for(iBin=iLog
8ecf0 73 69 7a 65 3b 20 6d 65 6d 35 2e 61 69 46 72 65  size; mem5.aiFre
8ed00 65 6c 69 73 74 5b 69 42 69 6e 5d 3c 30 20 26 26  elist[iBin]<0 &&
8ed10 20 69 42 69 6e 3c 3d 4c 4f 47 4d 41 58 3b 20 69   iBin<=LOGMAX; i
8ed20 42 69 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69  Bin++){}.  if( i
8ed30 42 69 6e 3e 4c 4f 47 4d 41 58 20 29 20 72 65 74  Bin>LOGMAX ) ret
8ed40 75 72 6e 20 30 3b 0a 20 20 69 20 3d 20 6d 65 6d  urn 0;.  i = mem
8ed50 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28  sys5UnlinkFirst(
8ed60 69 42 69 6e 29 3b 0a 20 20 77 68 69 6c 65 28 20  iBin);.  while( 
8ed70 69 42 69 6e 3e 69 4c 6f 67 73 69 7a 65 20 29 7b  iBin>iLogsize ){
8ed80 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65  .    int newSize
8ed90 3b 0a 0a 20 20 20 20 69 42 69 6e 2d 2d 3b 0a 20  ;..    iBin--;. 
8eda0 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 31 20 3c     newSize = 1 <
8edb0 3c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 35  < iBin;.    mem5
8edc0 2e 61 43 74 72 6c 5b 69 2b 6e 65 77 53 69 7a 65  .aCtrl[i+newSize
8edd0 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20  ] = CTRL_FREE | 
8ede0 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 73 79 73  iBin;.    memsys
8edf0 35 4c 69 6e 6b 28 69 2b 6e 65 77 53 69 7a 65 2c  5Link(i+newSize,
8ee00 20 69 42 69 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65   iBin);.  }.  me
8ee10 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 3d 20 69 4c  m5.aCtrl[i] = iL
8ee20 6f 67 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 70  ogsize;..  /* Up
8ee30 64 61 74 65 20 61 6c 6c 6f 63 61 74 6f 72 20 70  date allocator p
8ee40 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69  erformance stati
8ee50 73 74 69 63 73 2e 20 2a 2f 0a 20 20 6d 65 6d 35  stics. */.  mem5
8ee60 2e 6e 41 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d 65 6d  .nAlloc++;.  mem
8ee70 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 2b 3d 20  5.totalAlloc += 
8ee80 69 46 75 6c 6c 53 7a 3b 0a 20 20 6d 65 6d 35 2e  iFullSz;.  mem5.
8ee90 74 6f 74 61 6c 45 78 63 65 73 73 20 2b 3d 20 69  totalExcess += i
8eea0 46 75 6c 6c 53 7a 20 2d 20 6e 42 79 74 65 3b 0a  FullSz - nByte;.
8eeb0 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f    mem5.currentCo
8eec0 75 6e 74 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 63 75  unt++;.  mem5.cu
8eed0 72 72 65 6e 74 4f 75 74 20 2b 3d 20 69 46 75 6c  rrentOut += iFul
8eee0 6c 53 7a 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e  lSz;.  if( mem5.
8eef0 6d 61 78 43 6f 75 6e 74 3c 6d 65 6d 35 2e 63 75  maxCount<mem5.cu
8ef00 72 72 65 6e 74 43 6f 75 6e 74 20 29 20 6d 65 6d  rrentCount ) mem
8ef10 35 2e 6d 61 78 43 6f 75 6e 74 20 3d 20 6d 65 6d  5.maxCount = mem
8ef20 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 0a  5.currentCount;.
8ef30 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 4f 75    if( mem5.maxOu
8ef40 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75  t<mem5.currentOu
8ef50 74 20 29 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20  t ) mem5.maxOut 
8ef60 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75  = mem5.currentOu
8ef70 74 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20  t;..  /* Return 
8ef80 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
8ef90 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
8efa0 79 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28  y. */.  return (
8efb0 76 6f 69 64 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f  void*)&mem5.zPoo
8efc0 6c 5b 69 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d  l[i*mem5.szAtom]
8efd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
8efe0 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d  an outstanding m
8eff0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
8f000 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
8f010 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61   memsys5FreeUnsa
8f020 66 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a  fe(void *pOld){.
8f030 20 20 75 33 32 20 73 69 7a 65 2c 20 69 4c 6f 67    u32 size, iLog
8f040 73 69 7a 65 3b 0a 20 20 69 6e 74 20 69 42 6c 6f  size;.  int iBlo
8f050 63 6b 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 69 42  ck;..  /* Set iB
8f060 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 6e 64 65  lock to the inde
8f070 78 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 70  x of the block p
8f080 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 4f 6c  ointed to by pOl
8f090 64 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 20 61  d in .  ** the a
8f0a0 72 72 61 79 20 6f 66 20 6d 65 6d 35 2e 73 7a 41  rray of mem5.szA
8f0b0 74 6f 6d 20 62 79 74 65 20 62 6c 6f 63 6b 73 20  tom byte blocks 
8f0c0 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 6d 65  pointed to by me
8f0d0 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 2a 2f 0a 20  m5.zPool..  */. 
8f0e0 20 69 42 6c 6f 63 6b 20 3d 20 28 28 75 38 20 2a   iBlock = ((u8 *
8f0f0 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c  )pOld-mem5.zPool
8f100 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 0a  )/mem5.szAtom;..
8f110 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
8f120 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f 6c 64  the pointer pOld
8f130 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 6c   points to a val
8f140 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62 6c 6f  id, non-free blo
8f150 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ck. */.  assert(
8f160 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20 69 42   iBlock>=0 && iB
8f170 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  lock<mem5.nBlock
8f180 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 28   );.  assert( ((
8f190 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a  u8 *)pOld-mem5.z
8f1a0 50 6f 6f 6c 29 25 6d 65 6d 35 2e 73 7a 41 74 6f  Pool)%mem5.szAto
8f1b0 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  m==0 );.  assert
8f1c0 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42  ( (mem5.aCtrl[iB
8f1d0 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 46 52 45  lock] & CTRL_FRE
8f1e0 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 4c 6f 67  E)==0 );..  iLog
8f1f0 73 69 7a 65 20 3d 20 6d 65 6d 35 2e 61 43 74 72  size = mem5.aCtr
8f200 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c  l[iBlock] & CTRL
8f210 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69 7a 65  _LOGSIZE;.  size
8f220 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65 3b 0a   = 1<<iLogsize;.
8f230 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b    assert( iBlock
8f240 2b 73 69 7a 65 2d 31 3c 28 75 33 32 29 6d 65 6d  +size-1<(u32)mem
8f250 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20 6d  5.nBlock );..  m
8f260 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b  em5.aCtrl[iBlock
8f270 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a  ] |= CTRL_FREE;.
8f280 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c    mem5.aCtrl[iBl
8f290 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 7c 3d 20 43  ock+size-1] |= C
8f2a0 54 52 4c 5f 46 52 45 45 3b 0a 20 20 61 73 73 65  TRL_FREE;.  asse
8f2b0 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  rt( mem5.current
8f2c0 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 20 61 73 73  Count>0 );.  ass
8f2d0 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e  ert( mem5.curren
8f2e0 74 4f 75 74 3e 3d 28 73 69 7a 65 2a 6d 65 6d 35  tOut>=(size*mem5
8f2f0 2e 73 7a 41 74 6f 6d 29 20 29 3b 0a 20 20 6d 65  .szAtom) );.  me
8f300 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2d  m5.currentCount-
8f310 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e  -;.  mem5.curren
8f320 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 6d 65 6d  tOut -= size*mem
8f330 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 61 73 73 65  5.szAtom;.  asse
8f340 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  rt( mem5.current
8f350 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75  Out>0 || mem5.cu
8f360 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 29 3b  rrentCount==0 );
8f370 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e  .  assert( mem5.
8f380 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 7c  currentCount>0 |
8f390 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75  | mem5.currentOu
8f3a0 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e  t==0 );..  mem5.
8f3b0 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20  aCtrl[iBlock] = 
8f3c0 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67  CTRL_FREE | iLog
8f3d0 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28 20 41  size;.  while( A
8f3e0 4c 57 41 59 53 28 69 4c 6f 67 73 69 7a 65 3c 4c  LWAYS(iLogsize<L
8f3f0 4f 47 4d 41 58 29 20 29 7b 0a 20 20 20 20 69 6e  OGMAX) ){.    in
8f400 74 20 69 42 75 64 64 79 3b 0a 20 20 20 20 69 66  t iBuddy;.    if
8f410 28 20 28 69 42 6c 6f 63 6b 3e 3e 69 4c 6f 67 73  ( (iBlock>>iLogs
8f420 69 7a 65 29 20 26 20 31 20 29 7b 0a 20 20 20 20  ize) & 1 ){.    
8f430 20 20 69 42 75 64 64 79 20 3d 20 69 42 6c 6f 63    iBuddy = iBloc
8f440 6b 20 2d 20 73 69 7a 65 3b 0a 20 20 20 20 7d 65  k - size;.    }e
8f450 6c 73 65 7b 0a 20 20 20 20 20 20 69 42 75 64 64  lse{.      iBudd
8f460 79 20 3d 20 69 42 6c 6f 63 6b 20 2b 20 73 69 7a  y = iBlock + siz
8f470 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  e;.    }.    ass
8f480 65 72 74 28 20 69 42 75 64 64 79 3e 3d 30 20 29  ert( iBuddy>=0 )
8f490 3b 0a 20 20 20 20 69 66 28 20 28 69 42 75 64 64  ;.    if( (iBudd
8f4a0 79 2b 28 31 3c 3c 69 4c 6f 67 73 69 7a 65 29 29  y+(1<<iLogsize))
8f4b0 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 20 62  >mem5.nBlock ) b
8f4c0 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 6d 65  reak;.    if( me
8f4d0 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d  m5.aCtrl[iBuddy]
8f4e0 21 3d 28 43 54 52 4c 5f 46 52 45 45 20 7c 20 69  !=(CTRL_FREE | i
8f4f0 4c 6f 67 73 69 7a 65 29 20 29 20 62 72 65 61 6b  Logsize) ) break
8f500 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 55 6e 6c  ;.    memsys5Unl
8f510 69 6e 6b 28 69 42 75 64 64 79 2c 20 69 4c 6f 67  ink(iBuddy, iLog
8f520 73 69 7a 65 29 3b 0a 20 20 20 20 69 4c 6f 67 73  size);.    iLogs
8f530 69 7a 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 69  ize++;.    if( i
8f540 42 75 64 64 79 3c 69 42 6c 6f 63 6b 20 29 7b 0a  Buddy<iBlock ){.
8f550 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c        mem5.aCtrl
8f560 5b 69 42 75 64 64 79 5d 20 3d 20 43 54 52 4c 5f  [iBuddy] = CTRL_
8f570 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b  FREE | iLogsize;
8f580 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72  .      mem5.aCtr
8f590 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 30 3b 0a 20  l[iBlock] = 0;. 
8f5a0 20 20 20 20 20 69 42 6c 6f 63 6b 20 3d 20 69 42       iBlock = iB
8f5b0 75 64 64 79 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  uddy;.    }else{
8f5c0 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72  .      mem5.aCtr
8f5d0 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c  l[iBlock] = CTRL
8f5e0 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65  _FREE | iLogsize
8f5f0 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74  ;.      mem5.aCt
8f600 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 30 3b 0a  rl[iBuddy] = 0;.
8f610 20 20 20 20 7d 0a 20 20 20 20 73 69 7a 65 20 2a      }.    size *
8f620 3d 20 32 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79  = 2;.  }.  memsy
8f630 73 35 4c 69 6e 6b 28 69 42 6c 6f 63 6b 2c 20 69  s5Link(iBlock, i
8f640 4c 6f 67 73 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a  Logsize);.}../*.
8f650 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74  ** Allocate nByt
8f660 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2f 0a  es of memory.*/.
8f670 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d  static void *mem
8f680 73 79 73 35 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e  sys5Malloc(int n
8f690 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69 74 65  Bytes){.  sqlite
8f6a0 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 30 3b 0a  3_int64 *p = 0;.
8f6b0 20 20 69 66 28 20 6e 42 79 74 65 73 3e 30 20 29    if( nBytes>0 )
8f6c0 7b 0a 20 20 20 20 6d 65 6d 73 79 73 35 45 6e 74  {.    memsys5Ent
8f6d0 65 72 28 29 3b 0a 20 20 20 20 70 20 3d 20 6d 65  er();.    p = me
8f6e0 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66  msys5MallocUnsaf
8f6f0 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 6d  e(nBytes);.    m
8f700 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20  emsys5Leave();. 
8f710 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69   }.  return (voi
8f720 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  d*)p; .}../*.** 
8f730 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a  Free memory..**.
8f740 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 6c 61 79  ** The outer lay
8f750 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  er memory alloca
8f760 74 6f 72 20 70 72 65 76 65 6e 74 73 20 74 68 69  tor prevents thi
8f770 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 0a 2a  s routine from.*
8f780 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 77  * being called w
8f790 69 74 68 20 70 50 72 69 6f 72 3d 3d 30 2e 0a 2a  ith pPrior==0..*
8f7a0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
8f7b0 6d 73 79 73 35 46 72 65 65 28 76 6f 69 64 20 2a  msys5Free(void *
8f7c0 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72  pPrior){.  asser
8f7d0 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a  t( pPrior!=0 );.
8f7e0 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29    memsys5Enter()
8f7f0 3b 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55  ;.  memsys5FreeU
8f800 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20  nsafe(pPrior);. 
8f810 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b   memsys5Leave();
8f820 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e    .}../*.** Chan
8f830 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  ge the size of a
8f840 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72  n existing memor
8f850 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a  y allocation..**
8f860 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 6c 61  .** The outer la
8f870 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  yer memory alloc
8f880 61 74 6f 72 20 70 72 65 76 65 6e 74 73 20 74 68  ator prevents th
8f890 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 0a  is routine from.
8f8a0 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20  ** being called 
8f8b0 77 69 74 68 20 70 50 72 69 6f 72 3d 3d 30 2e 20  with pPrior==0. 
8f8c0 20 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 73 20 69   .**.** nBytes i
8f8d0 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c 75 65  s always a value
8f8e0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61   obtained from a
8f8f0 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 0a 2a   prior call to.*
8f900 2a 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 28 29  * memsys5Round()
8f910 2e 20 20 48 65 6e 63 65 20 6e 42 79 74 65 73 20  .  Hence nBytes 
8f920 69 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 6e 2d  is always a non-
8f930 6e 65 67 61 74 69 76 65 20 70 6f 77 65 72 0a 2a  negative power.*
8f940 2a 20 6f 66 20 74 77 6f 2e 20 20 49 66 20 6e 42  * of two.  If nB
8f950 79 74 65 73 3d 3d 30 20 74 68 61 74 20 6d 65 61  ytes==0 that mea
8f960 6e 73 20 74 68 61 74 20 61 6e 20 6f 76 65 72 73  ns that an overs
8f970 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  ize allocation.*
8f980 2a 20 28 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  * (an allocation
8f990 20 6c 61 72 67 65 72 20 74 68 61 6e 20 30 78 34   larger than 0x4
8f9a0 30 30 30 30 30 30 30 29 20 77 61 73 20 72 65 71  0000000) was req
8f9b0 75 65 73 74 65 64 20 61 6e 64 20 74 68 69 73 0a  uested and this.
8f9c0 2a 2a 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  ** routine shoul
8f9d0 64 20 72 65 74 75 72 6e 20 30 20 77 69 74 68 6f  d return 0 witho
8f9e0 75 74 20 66 72 65 65 69 6e 67 20 70 50 72 69 6f  ut freeing pPrio
8f9f0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
8fa00 64 20 2a 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f  d *memsys5Reallo
8fa10 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20  c(void *pPrior, 
8fa20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69  int nBytes){.  i
8fa30 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20  nt nOld;.  void 
8fa40 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  *p;.  assert( pP
8fa50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 61 73 73  rior!=0 );.  ass
8fa60 65 72 74 28 20 28 6e 42 79 74 65 73 26 28 6e 42  ert( (nBytes&(nB
8fa70 79 74 65 73 2d 31 29 29 3d 3d 30 20 29 3b 0a 20  ytes-1))==0 );. 
8fa80 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e   assert( nBytes>
8fa90 3d 30 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  =0 );.  if( nByt
8faa0 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  es==0 ){.    ret
8fab0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c  urn 0;.  }.  nOl
8fac0 64 20 3d 20 6d 65 6d 73 79 73 35 53 69 7a 65 28  d = memsys5Size(
8fad0 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e  pPrior);.  if( n
8fae0 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 29 7b 0a 20  Bytes<=nOld ){. 
8faf0 20 20 20 72 65 74 75 72 6e 20 70 50 72 69 6f 72     return pPrior
8fb00 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45  ;.  }.  memsys5E
8fb10 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65  nter();.  p = me
8fb20 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66  msys5MallocUnsaf
8fb30 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28  e(nBytes);.  if(
8fb40 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79   p ){.    memcpy
8fb50 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c 64  (p, pPrior, nOld
8fb60 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 46 72  );.    memsys5Fr
8fb70 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29  eeUnsafe(pPrior)
8fb80 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c  ;.  }.  memsys5L
8fb90 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75 72 6e  eave();.  return
8fba0 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75   p;.}../*.** Rou
8fbb0 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20  nd up a request 
8fbc0 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74  size to the next
8fbd0 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f   valid allocatio
8fbe0 6e 20 73 69 7a 65 2e 20 20 49 66 0a 2a 2a 20 74  n size.  If.** t
8fbf0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  he allocation is
8fc00 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 62 65   too large to be
8fc10 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 69 73   handled by this
8fc20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 79 73 74   allocation syst
8fc30 65 6d 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e  em,.** return 0.
8fc40 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c 6f 63  .**.** All alloc
8fc50 61 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 20 61  ations must be a
8fc60 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 61 6e   power of two an
8fc70 64 20 6d 75 73 74 20 62 65 20 65 78 70 72 65 73  d must be expres
8fc80 73 65 64 20 62 79 20 61 0a 2a 2a 20 33 32 2d 62  sed by a.** 32-b
8fc90 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
8fca0 72 2e 20 20 48 65 6e 63 65 20 74 68 65 20 6c 61  r.  Hence the la
8fcb0 72 67 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e  rgest allocation
8fcc0 20 69 73 20 30 78 34 30 30 30 30 30 30 30 0a 2a   is 0x40000000.*
8fcd0 2a 20 6f 72 20 31 30 37 33 37 34 31 38 32 34 20  * or 1073741824 
8fce0 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  bytes..*/.static
8fcf0 20 69 6e 74 20 6d 65 6d 73 79 73 35 52 6f 75 6e   int memsys5Roun
8fd00 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 69 6e  dup(int n){.  in
8fd10 74 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 69 66 28  t iFullSz;.  if(
8fd20 20 6e 20 3e 20 30 78 34 30 30 30 30 30 30 30 20   n > 0x40000000 
8fd30 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f  ) return 0;.  fo
8fd40 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73  r(iFullSz=mem5.s
8fd50 7a 41 74 6f 6d 3b 20 69 46 75 6c 6c 53 7a 3c 6e  zAtom; iFullSz<n
8fd60 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 29 3b  ; iFullSz *= 2);
8fd70 0a 20 20 72 65 74 75 72 6e 20 69 46 75 6c 6c 53  .  return iFullS
8fd80 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  z;.}../*.** Retu
8fd90 72 6e 20 74 68 65 20 63 65 69 6c 69 6e 67 20 6f  rn the ceiling o
8fda0 66 20 74 68 65 20 6c 6f 67 61 72 69 74 68 6d 20  f the logarithm 
8fdb0 62 61 73 65 20 32 20 6f 66 20 69 56 61 6c 75 65  base 2 of iValue
8fdc0 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 73  ..**.** Examples
8fdd0 3a 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 31  :   memsys5Log(1
8fde0 29 20 2d 3e 20 30 0a 2a 2a 20 20 20 20 20 20 20  ) -> 0.**       
8fdf0 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67        memsys5Log
8fe00 28 32 29 20 2d 3e 20 31 0a 2a 2a 20 20 20 20 20  (2) -> 1.**     
8fe10 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c          memsys5L
8fe20 6f 67 28 34 29 20 2d 3e 20 32 0a 2a 2a 20 20 20  og(4) -> 2.**   
8fe30 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73            memsys
8fe40 35 4c 6f 67 28 35 29 20 2d 3e 20 33 0a 2a 2a 20  5Log(5) -> 3.** 
8fe50 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73              mems
8fe60 79 73 35 4c 6f 67 28 38 29 20 2d 3e 20 33 0a 2a  ys5Log(8) -> 3.*
8fe70 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65  *             me
8fe80 6d 73 79 73 35 4c 6f 67 28 39 29 20 2d 3e 20 34  msys5Log(9) -> 4
8fe90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
8fea0 65 6d 73 79 73 35 4c 6f 67 28 69 6e 74 20 69 56  emsys5Log(int iV
8feb0 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 69 4c 6f  alue){.  int iLo
8fec0 67 3b 0a 20 20 66 6f 72 28 69 4c 6f 67 3d 30 3b  g;.  for(iLog=0;
8fed0 20 28 31 3c 3c 69 4c 6f 67 29 3c 69 56 61 6c 75   (1<<iLog)<iValu
8fee0 65 3b 20 69 4c 6f 67 2b 2b 29 3b 0a 20 20 72 65  e; iLog++);.  re
8fef0 74 75 72 6e 20 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a  turn iLog;.}../*
8ff00 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  .** Initialize t
8ff10 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
8ff20 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  tor..**.** This 
8ff30 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 74  routine is not t
8ff40 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 20  hreadsafe.  The 
8ff50 63 61 6c 6c 65 72 20 6d 75 73 74 20 62 65 20 68  caller must be h
8ff60 6f 6c 64 69 6e 67 20 61 20 6d 75 74 65 78 0a 2a  olding a mutex.*
8ff70 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 6d 75 6c  * to prevent mul
8ff80 74 69 70 6c 65 20 74 68 72 65 61 64 73 20 66 72  tiple threads fr
8ff90 6f 6d 20 65 6e 74 65 72 69 6e 67 20 61 74 20 74  om entering at t
8ffa0 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2f  he same time..*/
8ffb0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73  .static int mems
8ffc0 79 73 35 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f  ys5Init(void *No
8ffd0 74 55 73 65 64 29 7b 0a 20 20 69 6e 74 20 69 69  tUsed){.  int ii
8ffe0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
8fff0 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
90000 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20    int nByte;    
90010 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
90020 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  f bytes of memor
90030 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74  y available to t
90040 68 69 73 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f  his allocator */
90050 0a 20 20 75 38 20 2a 7a 42 79 74 65 3b 20 20 20  .  u8 *zByte;   
90060 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
90070 75 73 61 62 6c 65 20 62 79 20 74 68 69 73 20 61  usable by this a
90080 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 69 6e  llocator */.  in
90090 74 20 6e 4d 69 6e 4c 6f 67 3b 20 20 20 20 20 20  t nMinLog;      
900a0 20 2f 2a 20 4c 6f 67 20 62 61 73 65 20 32 20 6f   /* Log base 2 o
900b0 66 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61  f minimum alloca
900c0 74 69 6f 6e 20 73 69 7a 65 20 69 6e 20 62 79 74  tion size in byt
900d0 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66  es */.  int iOff
900e0 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e  set;       /* An
900f0 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 6d 65 6d   offset into mem
90100 35 2e 61 43 74 72 6c 5b 5d 20 2a 2f 0a 0a 20 20  5.aCtrl[] */..  
90110 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
90120 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a  (NotUsed);..  /*
90130 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65   For the purpose
90140 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  s of this routin
90150 65 2c 20 64 69 73 61 62 6c 65 20 74 68 65 20 6d  e, disable the m
90160 75 74 65 78 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6d  utex */.  mem5.m
90170 75 74 65 78 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  utex = 0;..  /* 
90180 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 4d 65  The size of a Me
90190 6d 35 4c 69 6e 6b 20 6f 62 6a 65 63 74 20 6d 75  m5Link object mu
901a0 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66  st be a power of
901b0 20 74 77 6f 2e 20 20 56 65 72 69 66 79 20 74 68   two.  Verify th
901c0 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20  at.  ** this is 
901d0 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  case..  */.  ass
901e0 65 72 74 28 20 28 73 69 7a 65 6f 66 28 4d 65 6d  ert( (sizeof(Mem
901f0 35 4c 69 6e 6b 29 26 28 73 69 7a 65 6f 66 28 4d  5Link)&(sizeof(M
90200 65 6d 35 4c 69 6e 6b 29 2d 31 29 29 3d 3d 30 20  em5Link)-1))==0 
90210 29 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 73 71  );..  nByte = sq
90220 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
90230 67 2e 6e 48 65 61 70 3b 0a 20 20 7a 42 79 74 65  g.nHeap;.  zByte
90240 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 47   = (u8*)sqlite3G
90250 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61  lobalConfig.pHea
90260 70 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 42 79  p;.  assert( zBy
90270 74 65 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c  te!=0 );  /* sql
90280 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 64 6f  ite3_config() do
90290 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 6f 74 68  es not allow oth
902a0 65 72 77 69 73 65 20 2a 2f 0a 0a 20 20 6e 4d 69  erwise */..  nMi
902b0 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f  nLog = memsys5Lo
902c0 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  g(sqlite3GlobalC
902d0 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20 20  onfig.mnReq);.  
902e0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20 28 31  mem5.szAtom = (1
902f0 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68  <<nMinLog);.  wh
90300 69 6c 65 28 20 28 69 6e 74 29 73 69 7a 65 6f 66  ile( (int)sizeof
90310 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d 35 2e  (Mem5Link)>mem5.
90320 73 7a 41 74 6f 6d 20 29 7b 0a 20 20 20 20 6d 65  szAtom ){.    me
90330 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20 6d 65 6d 35  m5.szAtom = mem5
90340 2e 73 7a 41 74 6f 6d 20 3c 3c 20 31 3b 0a 20 20  .szAtom << 1;.  
90350 7d 0a 0a 20 20 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  }..  mem5.nBlock
90360 20 3d 20 28 6e 42 79 74 65 20 2f 20 28 6d 65 6d   = (nByte / (mem
90370 35 2e 73 7a 41 74 6f 6d 2b 73 69 7a 65 6f 66 28  5.szAtom+sizeof(
90380 75 38 29 29 29 3b 0a 20 20 6d 65 6d 35 2e 7a 50  u8)));.  mem5.zP
90390 6f 6f 6c 20 3d 20 7a 42 79 74 65 3b 0a 20 20 6d  ool = zByte;.  m
903a0 65 6d 35 2e 61 43 74 72 6c 20 3d 20 28 75 38 20  em5.aCtrl = (u8 
903b0 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65  *)&mem5.zPool[me
903c0 6d 35 2e 6e 42 6c 6f 63 6b 2a 6d 65 6d 35 2e 73  m5.nBlock*mem5.s
903d0 7a 41 74 6f 6d 5d 3b 0a 0a 20 20 66 6f 72 28 69  zAtom];..  for(i
903e0 69 3d 30 3b 20 69 69 3c 3d 4c 4f 47 4d 41 58 3b  i=0; ii<=LOGMAX;
903f0 20 69 69 2b 2b 29 7b 0a 20 20 20 20 6d 65 6d 35   ii++){.    mem5
90400 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 69 5d 20  .aiFreelist[ii] 
90410 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 69 4f 66  = -1;.  }..  iOf
90420 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28  fset = 0;.  for(
90430 69 69 3d 4c 4f 47 4d 41 58 3b 20 69 69 3e 3d 30  ii=LOGMAX; ii>=0
90440 3b 20 69 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74  ; ii--){.    int
90450 20 6e 41 6c 6c 6f 63 20 3d 20 28 31 3c 3c 69 69   nAlloc = (1<<ii
90460 29 3b 0a 20 20 20 20 69 66 28 20 28 69 4f 66 66  );.    if( (iOff
90470 73 65 74 2b 6e 41 6c 6c 6f 63 29 3c 3d 6d 65 6d  set+nAlloc)<=mem
90480 35 2e 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20  5.nBlock ){.    
90490 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 4f 66    mem5.aCtrl[iOf
904a0 66 73 65 74 5d 20 3d 20 69 69 20 7c 20 43 54 52  fset] = ii | CTR
904b0 4c 5f 46 52 45 45 3b 0a 20 20 20 20 20 20 6d 65  L_FREE;.      me
904c0 6d 73 79 73 35 4c 69 6e 6b 28 69 4f 66 66 73 65  msys5Link(iOffse
904d0 74 2c 20 69 69 29 3b 0a 20 20 20 20 20 20 69 4f  t, ii);.      iO
904e0 66 66 73 65 74 20 2b 3d 20 6e 41 6c 6c 6f 63 3b  ffset += nAlloc;
904f0 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
90500 74 28 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f  t((iOffset+nAllo
90510 63 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 29 3b  c)>mem5.nBlock);
90520 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20  .  }..  /* If a 
90530 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72 65  mutex is require
90540 64 20 66 6f 72 20 6e 6f 72 6d 61 6c 20 6f 70 65  d for normal ope
90550 72 61 74 69 6f 6e 2c 20 61 6c 6c 6f 63 61 74 65  ration, allocate
90560 20 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20 73 71   one */.  if( sq
90570 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
90580 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 29 7b  g.bMemstat==0 ){
90590 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20  .    mem5.mutex 
905a0 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
905b0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
905c0 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20  _STATIC_MEM);.  
905d0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
905e0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
905f0 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69  Deinitialize thi
90600 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61  s module..*/.sta
90610 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35  tic void memsys5
90620 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e  Shutdown(void *N
90630 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45  otUsed){.  UNUSE
90640 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
90650 73 65 64 29 3b 0a 20 20 6d 65 6d 35 2e 6d 75 74  sed);.  mem5.mut
90660 65 78 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  ex = 0;.  return
90670 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
90680 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4f 70  TE_TEST./*.** Op
90690 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69  en the file indi
906a0 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20  cated and write 
906b0 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66  a log of all unf
906c0 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20  reed memory .** 
906d0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f  allocations into
906e0 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51   that log..*/.SQ
906f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
90700 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 35  d sqlite3Memsys5
90710 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20  Dump(const char 
90720 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46  *zFilename){.  F
90730 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 69 6e 74 20  ILE *out;.  int 
90740 69 2c 20 6a 2c 20 6e 3b 0a 20 20 69 6e 74 20 6e  i, j, n;.  int n
90750 4d 69 6e 4c 6f 67 3b 0a 0a 20 20 69 66 28 20 7a  MinLog;..  if( z
90760 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a  Filename==0 || z
90770 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29  Filename[0]==0 )
90780 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f  {.    out = stdo
90790 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ut;.  }else{.   
907a0 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69   out = fopen(zFi
907b0 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20  lename, "w");.  
907c0 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a    if( out==0 ){.
907d0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
907e0 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65  derr, "** Unable
907f0 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72   to output memor
90800 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c  y debug output l
90810 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20  og: %s **\n",.  
90820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
90830 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a      zFilename);.
90840 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
90850 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73    }.  }.  memsys
90860 35 45 6e 74 65 72 28 29 3b 0a 20 20 6e 4d 69 6e  5Enter();.  nMin
90870 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67  Log = memsys5Log
90880 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 3b 0a 20  (mem5.szAtom);. 
90890 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 4c 4f 47   for(i=0; i<=LOG
908a0 4d 41 58 20 26 26 20 69 2b 6e 4d 69 6e 4c 6f 67  MAX && i+nMinLog
908b0 3c 33 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66  <32; i++){.    f
908c0 6f 72 28 6e 3d 30 2c 20 6a 3d 6d 65 6d 35 2e 61  or(n=0, j=mem5.a
908d0 69 46 72 65 65 6c 69 73 74 5b 69 5d 3b 20 6a 3e  iFreelist[i]; j>
908e0 3d 30 3b 20 6a 20 3d 20 4d 45 4d 35 4c 49 4e 4b  =0; j = MEM5LINK
908f0 28 6a 29 2d 3e 6e 65 78 74 2c 20 6e 2b 2b 29 7b  (j)->next, n++){
90900 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  }.    fprintf(ou
90910 74 2c 20 22 66 72 65 65 6c 69 73 74 20 69 74 65  t, "freelist ite
90920 6d 73 20 6f 66 20 73 69 7a 65 20 25 64 3a 20 25  ms of size %d: %
90930 64 5c 6e 22 2c 20 6d 65 6d 35 2e 73 7a 41 74 6f  d\n", mem5.szAto
90940 6d 20 3c 3c 20 69 2c 20 6e 29 3b 0a 20 20 7d 0a  m << i, n);.  }.
90950 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
90960 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 20 20 20 20 20  mem5.nAlloc     
90970 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d    = %llu\n", mem
90980 35 2e 6e 41 6c 6c 6f 63 29 3b 0a 20 20 66 70 72  5.nAlloc);.  fpr
90990 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e  intf(out, "mem5.
909a0 74 6f 74 61 6c 41 6c 6c 6f 63 20 20 20 3d 20 25  totalAlloc   = %
909b0 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 6f 74  llu\n", mem5.tot
909c0 61 6c 41 6c 6c 6f 63 29 3b 0a 20 20 66 70 72 69  alAlloc);.  fpri
909d0 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 74  ntf(out, "mem5.t
909e0 6f 74 61 6c 45 78 63 65 73 73 20 20 3d 20 25 6c  otalExcess  = %l
909f0 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 6f 74 61  lu\n", mem5.tota
90a00 6c 45 78 63 65 73 73 29 3b 0a 20 20 66 70 72 69  lExcess);.  fpri
90a10 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 63  ntf(out, "mem5.c
90a20 75 72 72 65 6e 74 4f 75 74 20 20 20 3d 20 25 75  urrentOut   = %u
90a30 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72 65 6e  \n", mem5.curren
90a40 74 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e 74 66  tOut);.  fprintf
90a50 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72  (out, "mem5.curr
90a60 65 6e 74 43 6f 75 6e 74 20 3d 20 25 75 5c 6e 22  entCount = %u\n"
90a70 2c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f  , mem5.currentCo
90a80 75 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  unt);.  fprintf(
90a90 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 4f 75  out, "mem5.maxOu
90aa0 74 20 20 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c  t       = %u\n",
90ab0 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 29 3b 0a 20   mem5.maxOut);. 
90ac0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d   fprintf(out, "m
90ad0 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 20 20 20  em5.maxCount    
90ae0 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d   = %u\n", mem5.m
90af0 61 78 43 6f 75 6e 74 29 3b 0a 20 20 66 70 72 69  axCount);.  fpri
90b00 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d  ntf(out, "mem5.m
90b10 61 78 52 65 71 75 65 73 74 20 20 20 3d 20 25 75  axRequest   = %u
90b20 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 52 65 71  \n", mem5.maxReq
90b30 75 65 73 74 29 3b 0a 20 20 6d 65 6d 73 79 73 35  uest);.  memsys5
90b40 4c 65 61 76 65 28 29 3b 0a 20 20 69 66 28 20 6f  Leave();.  if( o
90b50 75 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20  ut==stdout ){.  
90b60 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29    fflush(stdout)
90b70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66  ;.  }else{.    f
90b80 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a  close(out);.  }.
90b90 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
90ba0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
90bb0 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65  the only routine
90bc0 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69   in this file wi
90bd0 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20  th external .** 
90be0 6c 69 6e 6b 61 67 65 2e 20 49 74 20 72 65 74 75  linkage. It retu
90bf0 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
90c00 20 61 20 73 74 61 74 69 63 20 73 71 6c 69 74 65   a static sqlite
90c10 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a 2a 2a  3_mem_methods.**
90c20 20 73 74 72 75 63 74 20 70 6f 70 75 6c 61 74 65   struct populate
90c30 64 20 77 69 74 68 20 74 68 65 20 6d 65 6d 73 79  d with the memsy
90c40 73 35 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53  s5 methods..*/.S
90c50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
90c60 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
90c70 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
90c80 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f  MemGetMemsys5(vo
90c90 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  id){.  static co
90ca0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
90cb0 6d 65 74 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d  methods memsys5M
90cc0 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20  ethods = {.     
90cd0 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0a 20  memsys5Malloc,. 
90ce0 20 20 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c      memsys5Free,
90cf0 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61  .     memsys5Rea
90d00 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79  lloc,.     memsy
90d10 73 35 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d  s5Size,.     mem
90d20 73 79 73 35 52 6f 75 6e 64 75 70 2c 0a 20 20 20  sys5Roundup,.   
90d30 20 20 6d 65 6d 73 79 73 35 49 6e 69 74 2c 0a 20    memsys5Init,. 
90d40 20 20 20 20 6d 65 6d 73 79 73 35 53 68 75 74 64      memsys5Shutd
90d50 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b  own,.     0.  };
90d60 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 73 79  .  return &memsy
90d70 73 35 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65  s5Methods;.}..#e
90d80 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45  ndif /* SQLITE_E
90d90 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 2a 2f  NABLE_MEMSYS5 */
90da0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
90db0 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 35 2e 63 20  * End of mem5.c 
90dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90df0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
90e00 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74  * Begin file mut
90e10 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ex.c ***********
90e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90e40 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67  /./*.** 2007 Aug
90e50 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 14.**.** The
90e60 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
90e70 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
90e80 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
90e90 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
90ea0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
90eb0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
90ec0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
90ed0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
90ee0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
90ef0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
90f00 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
90f10 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
90f20 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
90f30 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
90f40 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
90f50 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
90f60 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
90f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90fb0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
90fc0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
90fd0 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
90fe0 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
90ff0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  es..**.** This f
91000 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
91010 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e  e that is common
91020 20 61 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65   across all mute
91030 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
91040 73 2e 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  s...**.** $Id: m
91050 75 74 65 78 2e 63 2c 76 20 31 2e 33 31 20 32 30  utex.c,v 1.31 20
91060 30 39 2f 30 37 2f 31 36 20 31 38 3a 32 31 3a 31  09/07/16 18:21:1
91070 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  8 drh Exp $.*/..
91080 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
91090 54 45 5f 44 45 42 55 47 29 20 26 26 20 21 64 65  TE_DEBUG) && !de
910a0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54  fined(SQLITE_MUT
910b0 45 58 5f 4f 4d 49 54 29 0a 2f 2a 0a 2a 2a 20 46  EX_OMIT)./*.** F
910c0 6f 72 20 64 65 62 75 67 67 69 6e 67 20 70 75 72  or debugging pur
910d0 70 6f 73 65 73 2c 20 72 65 63 6f 72 64 20 77 68  poses, record wh
910e0 65 6e 20 74 68 65 20 6d 75 74 65 78 20 73 75 62  en the mutex sub
910f0 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61  system is initia
91100 6c 69 7a 65 64 0a 2a 2a 20 61 6e 64 20 75 6e 69  lized.** and uni
91110 6e 69 74 69 61 6c 69 7a 65 64 20 73 6f 20 74 68  nitialized so th
91120 61 74 20 77 65 20 63 61 6e 20 61 73 73 65 72 74  at we can assert
91130 28 29 20 69 66 20 74 68 65 72 65 20 69 73 20 61  () if there is a
91140 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20  n attempt to.** 
91150 61 6c 6c 6f 63 61 74 65 20 61 20 6d 75 74 65 78  allocate a mutex
91160 20 77 68 69 6c 65 20 74 68 65 20 73 79 73 74 65   while the syste
91170 6d 20 69 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a  m is uninitializ
91180 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51  ed..*/.static SQ
91190 4c 49 54 45 5f 57 53 44 20 69 6e 74 20 6d 75 74  LITE_WSD int mut
911a0 65 78 49 73 49 6e 69 74 20 3d 20 30 3b 0a 23 65  exIsInit = 0;.#e
911b0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44  ndif /* SQLITE_D
911c0 45 42 55 47 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65  EBUG */...#ifnde
911d0 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  f SQLITE_MUTEX_O
911e0 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  MIT./*.** Initia
911f0 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73  lize the mutex s
91200 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ystem..*/.SQLITE
91210 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
91220 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f  ite3MutexInit(vo
91230 69 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 20 3d  id){ .  int rc =
91240 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
91250 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
91260 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78  onfig.bCoreMutex
91270 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c   ){.    if( !sql
91280 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
91290 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c  .mutex.xMutexAll
912a0 6f 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  oc ){.      /* I
912b0 66 20 74 68 65 20 78 4d 75 74 65 78 41 6c 6c 6f  f the xMutexAllo
912c0 63 20 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f 74  c method has not
912d0 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20   been set, then 
912e0 74 68 65 20 75 73 65 72 20 64 69 64 20 6e 6f 74  the user did not
912f0 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 61 6c  .      ** instal
91300 6c 20 61 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  l a mutex implem
91310 65 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 71 6c  entation via sql
91320 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 70 72  ite3_config() pr
91330 69 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a  ior to .      **
91340 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
91350 69 7a 65 28 29 20 62 65 69 6e 67 20 63 61 6c 6c  ize() being call
91360 65 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63  ed. This block c
91370 6f 70 69 65 73 20 70 6f 69 6e 74 65 72 73 20 74  opies pointers t
91380 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64  o.      ** the d
91390 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74  efault implement
913a0 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 73  ation into the s
913b0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
913c0 69 67 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20  ig structure..  
913d0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
913e0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
913f0 64 73 20 2a 70 46 72 6f 6d 20 3d 20 73 71 6c 69  ds *pFrom = sqli
91400 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28  te3DefaultMutex(
91410 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
91420 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a  _mutex_methods *
91430 70 54 6f 20 3d 20 26 73 71 6c 69 74 65 33 47 6c  pTo = &sqlite3Gl
91440 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
91450 3b 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ;..      memcpy(
91460 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 6f 66 66 73  pTo, pFrom, offs
91470 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74  etof(sqlite3_mut
91480 65 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74  ex_methods, xMut
91490 65 78 41 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 20  exAlloc));.     
914a0 20 6d 65 6d 63 70 79 28 26 70 54 6f 2d 3e 78 4d   memcpy(&pTo->xM
914b0 75 74 65 78 46 72 65 65 2c 20 26 70 46 72 6f 6d  utexFree, &pFrom
914c0 2d 3e 78 4d 75 74 65 78 46 72 65 65 2c 0a 20 20  ->xMutexFree,.  
914d0 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
914e0 66 28 2a 70 54 6f 29 20 2d 20 6f 66 66 73 65 74  f(*pTo) - offset
914f0 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  of(sqlite3_mutex
91500 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 65 78  _methods, xMutex
91510 46 72 65 65 29 29 3b 0a 20 20 20 20 20 20 70 54  Free));.      pT
91520 6f 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 3d  o->xMutexAlloc =
91530 20 70 46 72 6f 6d 2d 3e 78 4d 75 74 65 78 41 6c   pFrom->xMutexAl
91540 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  loc;.    }.    r
91550 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  c = sqlite3Globa
91560 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d  lConfig.mutex.xM
91570 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 7d 0a  utexInit();.  }.
91580 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
91590 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e  EBUG.  GLOBAL(in
915a0 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74 29 20  t, mutexIsInit) 
915b0 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72  = 1;.#endif..  r
915c0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
915d0 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20  ** Shutdown the 
915e0 6d 75 74 65 78 20 73 79 73 74 65 6d 2e 20 54 68  mutex system. Th
915f0 69 73 20 63 61 6c 6c 20 66 72 65 65 73 20 72 65  is call frees re
91600 73 6f 75 72 63 65 73 20 61 6c 6c 6f 63 61 74 65  sources allocate
91610 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 4d  d by.** sqlite3M
91620 75 74 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a 53  utexInit()..*/.S
91630 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
91640 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e  t sqlite3MutexEn
91650 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72  d(void){.  int r
91660 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
91670 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
91680 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
91690 4d 75 74 65 78 45 6e 64 20 29 7b 0a 20 20 20 20  MutexEnd ){.    
916a0 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  rc = sqlite3Glob
916b0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
916c0 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 7d 0a  MutexEnd();.  }.
916d0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
916e0 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e  EBUG.  GLOBAL(in
916f0 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74 29 20  t, mutexIsInit) 
91700 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72  = 0;.#endif..  r
91710 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
91720 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 6f  ** Retrieve a po
91730 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69  inter to a stati
91740 63 20 6d 75 74 65 78 20 6f 72 20 61 6c 6c 6f 63  c mutex or alloc
91750 61 74 65 20 61 20 6e 65 77 20 64 79 6e 61 6d 69  ate a new dynami
91760 63 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  c one..*/.SQLITE
91770 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74  _API sqlite3_mut
91780 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65  ex *sqlite3_mute
91790 78 5f 61 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b  x_alloc(int id){
917a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
917b0 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
917c0 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  if( sqlite3_init
917d0 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72  ialize() ) retur
917e0 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65  n 0;.#endif.  re
917f0 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62  turn sqlite3Glob
91800 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
91810 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a  MutexAlloc(id);.
91820 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
91830 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  E sqlite3_mutex 
91840 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  *sqlite3MutexAll
91850 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20 20 69 66  oc(int id){.  if
91860 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ( !sqlite3Global
91870 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65  Config.bCoreMute
91880 78 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  x ){.    return 
91890 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  0;.  }.  assert(
918a0 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 74   GLOBAL(int, mut
918b0 65 78 49 73 49 6e 69 74 29 20 29 3b 0a 20 20 72  exIsInit) );.  r
918c0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f  eturn sqlite3Glo
918d0 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
918e0 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b  xMutexAlloc(id);
918f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
91900 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e 0a   dynamic mutex..
91910 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
91920 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  id sqlite3_mutex
91930 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75  _free(sqlite3_mu
91940 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  tex *p){.  if( p
91950 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   ){.    sqlite3G
91960 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
91970 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29 3b  x.xMutexFree(p);
91980 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62  .  }.}../*.** Ob
91990 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70  tain the mutex p
919a0 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  . If some other 
919b0 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 20 68  thread already h
919c0 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 62 6c  as the mutex, bl
919d0 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 20  ock.** until it 
919e0 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 2e  can be obtained.
919f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
91a00 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  oid sqlite3_mute
91a10 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f  x_enter(sqlite3_
91a20 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28  mutex *p){.  if(
91a30 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   p ){.    sqlite
91a40 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
91a50 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 72 28  tex.xMutexEnter(
91a60 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
91a70 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65   Obtain the mute
91a80 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73 66  x p. If successf
91a90 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ul, return SQLIT
91aa0 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c  E_OK. Otherwise,
91ab0 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 74   if another.** t
91ac0 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65 20  hread holds the 
91ad0 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61 6e  mutex and it can
91ae0 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c  not be obtained,
91af0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
91b00 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  USY..*/.SQLITE_A
91b10 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d  PI int sqlite3_m
91b20 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65 33  utex_try(sqlite3
91b30 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e  _mutex *p){.  in
91b40 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
91b50 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
91b60 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47   return sqlite3G
91b70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
91b80 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b 0a  x.xMutexTry(p);.
91b90 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
91ba0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
91bb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
91bc0 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74  e() routine exit
91bd0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77  s a mutex that w
91be0 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a  as previously.**
91bf0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20   entered by the 
91c00 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68  same thread.  Th
91c10 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e  e behavior is un
91c20 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d  defined if the m
91c30 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 20  utex .** is not 
91c40 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65  currently entere
91c50 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69  d. If a NULL poi
91c60 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 20 61  nter is passed a
91c70 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  s an argument.**
91c80 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
91c90 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51  s a no-op..*/.SQ
91ca0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
91cb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
91cc0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
91cd0 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  *p){.  if( p ){.
91ce0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
91cf0 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d  lConfig.mutex.xM
91d00 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a 20 20  utexLeave(p);.  
91d10 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  }.}..#ifndef NDE
91d20 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  BUG./*.** The sq
91d30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
91d40 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
91d50 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72  utex_notheld() r
91d60 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e  outine are.** in
91d70 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69  tended for use i
91d80 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73  nside assert() s
91d90 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51  tatements..*/.SQ
91da0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
91db0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
91dc0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
91dd0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30  ){.  return p==0
91de0 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   || sqlite3Globa
91df0 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d  lConfig.mutex.xM
91e00 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a 53  utexHeld(p);.}.S
91e10 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
91e20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
91e30 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  eld(sqlite3_mute
91e40 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  x *p){.  return 
91e50 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47  p==0 || sqlite3G
91e60 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
91e70 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  x.xMutexNotheld(
91e80 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65  p);.}.#endif..#e
91e90 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  ndif /* SQLITE_M
91ea0 55 54 45 58 5f 4f 4d 49 54 20 2a 2f 0a 0a 2f 2a  UTEX_OMIT */../*
91eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
91ec0 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a 2a  d of mutex.c ***
91ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
91f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
91f10 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 6e  gin file mutex_n
91f20 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  oop.c **********
91f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
91f50 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72  .** 2008 October
91f60 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   07.**.** The au
91f70 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
91f80 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
91f90 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
91fa0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
91fb0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
91fc0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
91fd0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
91fe0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
91ff0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
92000 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
92010 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
92020 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
92030 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
92040 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
92050 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
92060 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
92070 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
92080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
920a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
920b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
920c0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
920d0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
920e0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
920f0 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e  plement mutexes.
92100 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c  .**.** This impl
92110 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68  ementation in th
92120 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74  is file does not
92130 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75 74   provide any mut
92140 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 6f 6e  ual.** exclusion
92150 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75 69   and is thus sui
92160 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f 6e  table for use on
92170 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69 6f  ly in applicatio
92180 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 20 53  ns.** that use S
92190 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67 6c  QLite in a singl
921a0 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 72  e thread.  The r
921b0 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 0a  outines defined.
921c0 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c 61 63  ** here are plac
921d0 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 70 6c  e-holders.  Appl
921e0 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 75 62  ications can sub
921f0 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e 67 0a  stitute working.
92200 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65  ** mutex routine
92210 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 20  s at start-time 
92220 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a 20  using the.**.** 
92230 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66      sqlite3_conf
92240 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
92250 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a  _MUTEX,...).**.*
92260 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  * interface..**.
92270 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20 77  ** If compiled w
92280 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 47  ith SQLITE_DEBUG
92290 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f 6e 61  , then additiona
922a0 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 65 72  l logic is inser
922b0 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73  ted.** that does
922c0 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20   error checking 
922d0 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 6d 61  on mutexes to ma
922e0 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72 65  ke sure they are
922f0 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65 64   being.** called
92300 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a   correctly..**.*
92310 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6e 6f 6f  * $Id: mutex_noo
92320 70 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 2f 31  p.c,v 1.3 2008/1
92330 32 2f 30 35 20 31 37 3a 31 37 3a 30 38 20 64 72  2/05 17:17:08 dr
92340 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66  h Exp $.*/...#if
92350 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
92360 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21  MUTEX_NOOP) && !
92370 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
92380 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75 62  EBUG)./*.** Stub
92390 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 6c   routines for al
923a0 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 2e  l mutex methods.
923b0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
923c0 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f 20  ines provide no 
923d0 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e  mutual exclusion
923e0 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b 69   or error checki
923f0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
92400 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 28  t noopMutexHeld(
92410 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
92420 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73  ){ return 1; }.s
92430 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75  tatic int noopMu
92440 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74  texNotheld(sqlit
92450 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65  e3_mutex *p){ re
92460 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63  turn 1; }.static
92470 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49 6e   int noopMutexIn
92480 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  it(void){ return
92490 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74   SQLITE_OK; }.st
924a0 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74  atic int noopMut
924b0 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74  exEnd(void){ ret
924c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
924d0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
924e0 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 78  mutex *noopMutex
924f0 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 72  Alloc(int id){ r
92500 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 6d  eturn (sqlite3_m
92510 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74 69  utex*)8; }.stati
92520 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78  c void noopMutex
92530 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  Free(sqlite3_mut
92540 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20  ex *p){ return; 
92550 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f  }.static void no
92560 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c  opMutexEnter(sql
92570 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20  ite3_mutex *p){ 
92580 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63  return; }.static
92590 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54 72   int noopMutexTr
925a0 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  y(sqlite3_mutex 
925b0 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  *p){ return SQLI
925c0 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20  TE_OK; }.static 
925d0 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c 65  void noopMutexLe
925e0 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ave(sqlite3_mute
925f0 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d  x *p){ return; }
92600 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
92610 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
92620 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44  ethods *sqlite3D
92630 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64  efaultMutex(void
92640 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
92650 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
92660 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20  s sMutex = {.   
92670 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c 0a   noopMutexInit,.
92680 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64      noopMutexEnd
92690 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 41  ,.    noopMutexA
926a0 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75  lloc,.    noopMu
926b0 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f 6f  texFree,.    noo
926c0 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20  pMutexEnter,.   
926d0 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a 20   noopMutexTry,. 
926e0 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76     noopMutexLeav
926f0 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65  e,..    noopMute
92700 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d  xHeld,.    noopM
92710 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b  utexNotheld.  };
92720 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74  ..  return &sMut
92730 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ex;.}.#endif /* 
92740 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
92750 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 64  UTEX_NOOP) && !d
92760 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
92770 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65 66  BUG) */..#if def
92780 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45  ined(SQLITE_MUTE
92790 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 6e  X_NOOP) && defin
927a0 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
927b0 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69  ./*.** In this i
927c0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 65  mplementation, e
927d0 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 73  rror checking is
927e0 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74 65   provided for te
927f0 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62  sting.** and deb
92800 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 2e  ugging purposes.
92810 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 73 74    The mutexes st
92820 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69  ill do not provi
92830 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61 6c  de any.** mutual
92840 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a 0a   exclusion..*/..
92850 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20  /*.** The mutex 
92860 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63 74  object.*/.struct
92870 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b   sqlite3_mutex {
92880 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 2f  .  int id;     /
92890 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 70 65  * The mutex type
928a0 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20   */.  int cnt;  
928b0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
928c0 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 20 61  ntries without a
928d0 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65 20   matching leave 
928e0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
928f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
92900 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65  eld() and sqlite
92910 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
92920 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a  ) routine are.**
92930 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73   intended for us
92940 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28  e inside assert(
92950 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f  ) statements..*/
92960 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75  .static int debu
92970 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74  gMutexHeld(sqlit
92980 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
92990 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70  return p==0 || p
929a0 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 69  ->cnt>0;.}.stati
929b0 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78  c int debugMutex
929c0 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f  Notheld(sqlite3_
929d0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74  mutex *p){.  ret
929e0 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63  urn p==0 || p->c
929f0 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  nt==0;.}../*.** 
92a00 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64  Initialize and d
92a10 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  einitialize the 
92a20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e  mutex subsystem.
92a30 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
92a40 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76 6f  ebugMutexInit(vo
92a50 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  id){ return SQLI
92a60 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20  TE_OK; }.static 
92a70 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45 6e  int debugMutexEn
92a80 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  d(void){ return 
92a90 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a  SQLITE_OK; }../*
92aa0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
92ab0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f  mutex_alloc() ro
92ac0 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20  utine allocates 
92ad0 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61  a new.** mutex a
92ae0 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  nd returns a poi
92af0 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20  nter to it.  If 
92b00 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a  it returns NULL.
92b10 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  ** that means th
92b20 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64  at a mutex could
92b30 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65   not be allocate
92b40 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71  d. .*/.static sq
92b50 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65 62  lite3_mutex *deb
92b60 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74  ugMutexAlloc(int
92b70 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73   id){.  static s
92b80 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53 74  qlite3_mutex aSt
92b90 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69 74  atic[6];.  sqlit
92ba0 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20 3d  e3_mutex *pNew =
92bb0 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69 64   0;.  switch( id
92bc0 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
92bd0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a  ITE_MUTEX_FAST:.
92be0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
92bf0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a  MUTEX_RECURSIVE:
92c00 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20   {.      pNew = 
92c10 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 69  sqlite3Malloc(si
92c20 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20  zeof(*pNew));.  
92c30 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a      if( pNew ){.
92c40 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 64          pNew->id
92c50 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20 70   = id;.        p
92c60 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20  New->cnt = 0;.  
92c70 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
92c80 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
92c90 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73  ault: {.      as
92ca0 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30 20  sert( id-2 >= 0 
92cb0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
92cc0 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73 69   id-2 < (int)(si
92cd0 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73 69  zeof(aStatic)/si
92ce0 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d 29  zeof(aStatic[0])
92cf0 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20  ) );.      pNew 
92d00 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32 5d  = &aStatic[id-2]
92d10 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 64  ;.      pNew->id
92d20 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72 65   = id;.      bre
92d30 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
92d40 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a  return pNew;.}..
92d50 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
92d60 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61  ne deallocates a
92d70 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
92d80 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a  cated mutex..*/.
92d90 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75  static void debu
92da0 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74  gMutexFree(sqlit
92db0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
92dc0 61 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 3d 3d  assert( p->cnt==
92dd0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
92de0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
92df0 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64  EX_FAST || p->id
92e00 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
92e10 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 73 71  ECURSIVE );.  sq
92e20 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d  lite3_free(p);.}
92e30 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
92e40 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
92e50 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  ) and sqlite3_mu
92e60 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e  tex_try() routin
92e70 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f  es attempt.** to
92e80 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20   enter a mutex. 
92e90 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65   If another thre
92ea0 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69  ad is already wi
92eb0 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a  thin the mutex,.
92ec0 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ** sqlite3_mutex
92ed0 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c  _enter() will bl
92ee0 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  ock and sqlite3_
92ef0 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c  mutex_try() will
92f00 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54   return.** SQLIT
92f10 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c  E_BUSY.  The sql
92f20 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
92f30 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
92f40 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20  ns SQLITE_OK.** 
92f50 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20  upon successful 
92f60 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20  entry.  Mutexes 
92f70 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51  created using SQ
92f80 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
92f90 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65  SIVE can.** be e
92fa0 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20  ntered multiple 
92fb0 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d  times by the sam
92fc0 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75  e thread.  In su
92fd0 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a  ch cases the,.**
92fe0 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65   mutex must be e
92ff0 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e  xited an equal n
93000 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62  umber of times b
93010 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68  efore another th
93020 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65  read.** can ente
93030 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20  r.  If the same 
93040 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20  thread tries to 
93050 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20  enter any other 
93060 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a  kind of mutex.**
93070 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c   more than once,
93080 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73   the behavior is
93090 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73   undefined..*/.s
930a0 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 67  tatic void debug
930b0 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74  MutexEnter(sqlit
930c0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
930d0 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53  assert( p->id==S
930e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
930f0 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75  RSIVE || debugMu
93100 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
93110 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73  .  p->cnt++;.}.s
93120 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d  tatic int debugM
93130 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f  utexTry(sqlite3_
93140 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
93150 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
93160 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
93170 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78  VE || debugMutex
93180 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  Notheld(p) );.  
93190 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 75  p->cnt++;.  retu
931a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
931b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
931c0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
931d0 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
931e0 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a   mutex that was.
931f0 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e  ** previously en
93200 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
93210 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
93220 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e  ehavior.** is un
93230 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d  defined if the m
93240 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72  utex is not curr
93250 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72  ently entered or
93260 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
93270 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20  ntly allocated. 
93280 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
93290 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f  er do either..*/
932a0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62  .static void deb
932b0 75 67 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c  ugMutexLeave(sql
932c0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
932d0 20 20 61 73 73 65 72 74 28 20 64 65 62 75 67 4d    assert( debugM
932e0 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a 20  utexHeld(p) );. 
932f0 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73 73   p->cnt--;.  ass
93300 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
93310 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
93320 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78  VE || debugMutex
93330 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d 0a  Notheld(p) );.}.
93340 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
93350 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
93360 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65  thods *sqlite3De
93370 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29  faultMutex(void)
93380 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74  {.  static sqlit
93390 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
933a0 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20   sMutex = {.    
933b0 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 2c 0a  debugMutexInit,.
933c0 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 45 6e      debugMutexEn
933d0 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65  d,.    debugMute
933e0 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 62 75  xAlloc,.    debu
933f0 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20  gMutexFree,.    
93400 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72 2c  debugMutexEnter,
93410 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 54  .    debugMutexT
93420 72 79 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74  ry,.    debugMut
93430 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 64 65  exLeave,..    de
93440 62 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20  bugMutexHeld,.  
93450 20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68    debugMutexNoth
93460 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75  eld.  };..  retu
93470 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65  rn &sMutex;.}.#e
93480 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28  ndif /* defined(
93490 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
934a0 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  P) && defined(SQ
934b0 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a  LITE_DEBUG) */..
934c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
934d0 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e 6f 6f  End of mutex_noo
934e0 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  p.c ************
934f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
93510 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
93520 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78  Begin file mutex
93530 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _os2.c *********
93540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
93560 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
93570 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 28.**.** The a
93580 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
93590 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
935a0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
935b0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
935c0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
935d0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
935e0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
935f0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
93600 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
93610 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
93620 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
93630 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
93640 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
93650 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
93660 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
93670 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
93680 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
93690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
936a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
936b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
936c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
936d0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
936e0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
936f0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
93700 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73  mplement mutexes
93710 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a 2a 2a 20   for OS/2.**.** 
93720 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73 32 2e 63  $Id: mutex_os2.c
93730 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 31 31 2f  ,v 1.11 2008/11/
93740 32 32 20 31 39 3a 35 30 3a 35 34 20 70 77 65 69  22 19:50:54 pwei
93750 6c 62 61 63 68 65 72 20 45 78 70 20 24 0a 2a 2f  lbacher Exp $.*/
93760 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  ../*.** The code
93770 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73   in this file is
93780 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 53 51   only used if SQ
93790 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 69  LITE_MUTEX_OS2 i
937a0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 53 65  s defined..** Se
937b0 65 20 74 68 65 20 6d 75 74 65 78 2e 68 20 66 69  e the mutex.h fi
937c0 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a  le for details..
937d0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
937e0 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a  _MUTEX_OS2../***
937f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93800 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 78 20 49  *** OS/2 Mutex I
93810 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a  mplementation **
93820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93830 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
93840 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
93850 66 20 6d 75 74 65 78 65 73 20 69 73 20 62 75 69  f mutexes is bui
93860 6c 74 20 75 73 69 6e 67 20 74 68 65 20 4f 53 2f  lt using the OS/
93870 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  2 API..*/../*.**
93880 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65 63   The mutex objec
93890 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73  t.** Each recurs
938a0 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20  ive mutex is an 
938b0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
938c0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
938d0 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73  ure..*/.struct s
938e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20  qlite3_mutex {. 
938f0 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20 20 20   HMTX mutex;    
93900 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74     /* Mutex cont
93910 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b  rolling the lock
93920 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 3b 20 20   */.  int  id;  
93930 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78          /* Mutex
93940 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 20   type */.  int  
93950 6e 52 65 66 3b 20 20 20 20 20 20 20 20 2f 2a 20  nRef;        /* 
93960 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65  Number of refere
93970 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 20 20 6f  nces */.  TID  o
93980 77 6e 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 54  wner;       /* T
93990 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68  hread holding th
939a0 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a  is mutex */.};..
939b0 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d 55 54 45  #define OS2_MUTE
939c0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 20 20  X_INITIALIZER   
939d0 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49  0,0,0,0../*.** I
939e0 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65  nitialize and de
939f0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  initialize the m
93a00 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a  utex subsystem..
93a10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
93a20 32 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29  2MutexInit(void)
93a30 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
93a40 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74  OK; }.static int
93a50 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 76 6f 69   os2MutexEnd(voi
93a60 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
93a70 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK; }../*.** T
93a80 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
93a90 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65  _alloc() routine
93aa0 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77   allocates a new
93ab0 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65  .** mutex and re
93ac0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
93ad0 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65  to it.  If it re
93ae0 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68  turns NULL.** th
93af0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  at means that a 
93b00 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20  mutex could not 
93b10 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a  be allocated. .*
93b20 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 6e  * SQLite will un
93b30 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61  wind its stack a
93b40 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  nd return an err
93b50 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e  or.  The argumen
93b60 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f  t.** to sqlite3_
93b70 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73  mutex_alloc() is
93b80 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e   one of these in
93b90 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a  teger constants:
93ba0 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
93bb0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
93bc0 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 20  X_FAST          
93bd0 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20       0.** <li>  
93be0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
93bf0 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20 20  URSIVE          
93c00 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  1.** <li>  SQLIT
93c10 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
93c20 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a 20  ASTER      2.** 
93c30 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
93c40 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 20  EX_STATIC_MEM   
93c50 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20        3.** <li> 
93c60 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
93c70 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20 20  ATIC_PRNG       
93c80 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a   4.** </ul>.**.*
93c90 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20  * The first two 
93ca0 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20  constants cause 
93cb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
93cc0 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a  loc() to create.
93cd0 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20  ** a new mutex. 
93ce0 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69   The new mutex i
93cf0 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 6e  s recursive when
93d00 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
93d10 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73  CURSIVE.** is us
93d20 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73  ed but not neces
93d30 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53  sarily so when S
93d40 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
93d50 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65   is used..** The
93d60 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74   mutex implement
93d70 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e  ation does not n
93d80 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69  eed to make a di
93d90 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74  stinction.** bet
93da0 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  ween SQLITE_MUTE
93db0 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 20  X_RECURSIVE and 
93dc0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
93dd0 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20  T if it does.** 
93de0 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 75  not want to.  Bu
93df0 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e  t SQLite will on
93e00 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 63  ly request a rec
93e10 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a  ursive mutex in.
93e20 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 69  ** cases where i
93e30 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f  t really needs o
93e40 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 72  ne.  If a faster
93e50 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d   non-recursive m
93e60 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  utex.** implemen
93e70 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61  tation is availa
93e80 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20  ble on the host 
93e90 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75  platform, the mu
93ea0 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a  tex subsystem.**
93eb0 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 75   might return su
93ec0 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65  ch a mutex in re
93ed0 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45  sponse to SQLITE
93ee0 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a  _MUTEX_FAST..**.
93ef0 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c  ** The other all
93f00 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 20  owed parameters 
93f10 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  to sqlite3_mutex
93f20 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65  _alloc() each re
93f30 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
93f40 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72  r to a static pr
93f50 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e  eexisting mutex.
93f60 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20 6d    Three static m
93f70 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73  utexes are.** us
93f80 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e  ed by the curren
93f90 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c  t version of SQL
93fa0 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72  ite.  Future ver
93fb0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a  sions of SQLite.
93fc0 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74  ** may add addit
93fd0 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74  ional static mut
93fe0 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75  exes.  Static mu
93ff0 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e  texes are for in
94000 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79  ternal.** use by
94010 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41   SQLite only.  A
94020 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
94030 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65   use SQLite mute
94040 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73  xes should.** us
94050 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d  e only the dynam
94060 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 72  ic mutexes retur
94070 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55  ned by SQLITE_MU
94080 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53  TEX_FAST or.** S
94090 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
940a0 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  RSIVE..**.** Not
940b0 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66  e that if one of
940c0 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74   the dynamic mut
940d0 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53  ex parameters (S
940e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
940f0 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55  .** or SQLITE_MU
94100 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69  TEX_RECURSIVE) i
94110 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69  s used then sqli
94120 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
94130 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64  ).** returns a d
94140 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f  ifferent mutex o
94150 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42  n every call.  B
94160 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69  ut for the stati
94170 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73  c.** mutex types
94180 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78  , the same mutex
94190 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
941a0 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20  every call that 
941b0 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20  has.** the same 
941c0 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a  type number..*/.
941d0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
941e0 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 78 41 6c  utex *os2MutexAl
941f0 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a  loc(int iType){.
94200 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
94210 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77 69  *p = NULL;.  swi
94220 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20  tch( iType ){.  
94230 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55    case SQLITE_MU
94240 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61  TEX_FAST:.    ca
94250 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  se SQLITE_MUTEX_
94260 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20  RECURSIVE: {.   
94270 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61     p = sqlite3Ma
94280 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66  llocZero( sizeof
94290 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66  (*p) );.      if
942a0 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ( p ){.        p
942b0 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20  ->id = iType;.  
942c0 20 20 20 20 20 20 69 66 28 20 44 6f 73 43 72 65        if( DosCre
942d0 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c 20  ateMutexSem( 0, 
942e0 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 46 41  &p->mutex, 0, FA
942f0 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f  LSE ) != NO_ERRO
94300 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  R ){.          s
94310 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 29  qlite3_free( p )
94320 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20  ;.          p = 
94330 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 7d 0a  NULL;.        }.
94340 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
94350 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
94360 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
94370 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 20  static volatile 
94380 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a  int isInit = 0;.
94390 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71 6c        static sql
943a0 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74 69  ite3_mutex stati
943b0 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a 20  cMutexes[] = {. 
943c0 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54         { OS2_MUT
943d0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20  EX_INITIALIZER, 
943e0 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32  },.        { OS2
943f0 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
94400 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b  ER, },.        {
94410 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49   OS2_MUTEX_INITI
94420 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20  ALIZER, },.     
94430 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49     { OS2_MUTEX_I
94440 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20  NITIALIZER, },. 
94450 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54         { OS2_MUT
94460 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20  EX_INITIALIZER, 
94470 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32  },.        { OS2
94480 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
94490 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d 3b 0a  ER, },.      };.
944a0 20 20 20 20 20 20 69 66 20 28 20 21 69 73 49 6e        if ( !isIn
944b0 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 41 50  it ){.        AP
944c0 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20 20 20  IRET rc;.       
944d0 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20 20   PTIB ptib;.    
944e0 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a 20      PPIB ppib;. 
944f0 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75 74 65         HMTX mute
94500 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  x;.        char 
94510 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20 20 20  name[32];.      
94520 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63    DosGetInfoBloc
94530 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 69 62  ks( &ptib, &ppib
94540 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
94550 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 73 69  te3_snprintf( si
94560 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 6d 65  zeof(name), name
94570 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c 49  , "\\SEM32\\SQLI
94580 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20 20 20  TE%04x",.       
94590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
945a0 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75 6c 70     ppib->pib_ulp
945b0 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68  id );.        wh
945c0 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b 0a  ile( !isInit ){.
945d0 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78 20            mutex 
945e0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72  = 0;.          r
945f0 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d 75 74  c = DosCreateMut
94600 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26 6d 75  exSem( name, &mu
94610 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29 3b 0a  tex, 0, FALSE);.
94620 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
94630 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
94640 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 73 69              unsi
94650 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 20  gned int i;.    
94660 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 49          if( !isI
94670 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  nit ){.         
94680 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30 3b       for( i = 0;
94690 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 74   i < sizeof(stat
946a0 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 6f  icMutexes)/sizeo
946b0 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b  f(staticMutexes[
946c0 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20  0]); i++ ){.    
946d0 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43              DosC
946e0 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30  reateMutexSem( 0
946f0 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73  , &staticMutexes
94700 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 46 41  [i].mutex, 0, FA
94710 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 20  LSE );.         
94720 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
94730 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31 3b       isInit = 1;
94740 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
94750 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43 6c             DosCl
94760 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d 75 74  oseMutexSem( mut
94770 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ex );.          
94780 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d 20  }else if( rc == 
94790 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45 5f  ERROR_DUPLICATE_
947a0 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 20 20  NAME ){.        
947b0 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31 20      DosSleep( 1 
947c0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
947d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
947e0 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 20  return p;.      
947f0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
94800 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
94810 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d  sert( iType-2 >=
94820 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65   0 );.      asse
94830 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73 69  rt( iType-2 < si
94840 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78  zeof(staticMutex
94850 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 69  es)/sizeof(stati
94860 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a  cMutexes[0]) );.
94870 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69        p = &stati
94880 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32  cMutexes[iType-2
94890 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d  ];.      p->id =
948a0 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72   iType;.      br
948b0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
948c0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f   return p;.}.../
948d0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
948e0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
948f0 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63  previously alloc
94900 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a 20 53  ated mutex..** S
94910 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 6c  QLite is careful
94920 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65   to deallocate e
94930 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 74 20  very mutex that 
94940 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f  it allocates..*/
94950 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32  .static void os2
94960 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 65  MutexFree(sqlite
94970 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
94980 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
94990 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
949a0 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Ref==0 );.  asse
949b0 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
949c0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20  E_MUTEX_FAST || 
949d0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
949e0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b  TEX_RECURSIVE );
949f0 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65 78  .  DosCloseMutex
94a00 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29 3b  Sem( p->mutex );
94a10 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
94a20 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   p );.}../*.** T
94a30 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
94a40 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c  _enter() and sql
94a50 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
94a60 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70   routines attemp
94a70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20  t.** to enter a 
94a80 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68  mutex.  If anoth
94a90 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72  er thread is alr
94aa0 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20  eady within the 
94ab0 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65  mutex,.** sqlite
94ac0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20  3_mutex_enter() 
94ad0 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73  will block and s
94ae0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
94af0 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a  () will return.*
94b00 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20  * SQLITE_BUSY.  
94b10 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
94b20 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63  x_try() interfac
94b30 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
94b40 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63  _OK.** upon succ
94b50 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d  essful entry.  M
94b60 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75  utexes created u
94b70 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45  sing SQLITE_MUTE
94b80 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a  X_RECURSIVE can.
94b90 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75  ** be entered mu
94ba0 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20  ltiple times by 
94bb0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e  the same thread.
94bc0 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20    In such cases 
94bd0 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75  the,.** mutex mu
94be0 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20  st be exited an 
94bf0 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  equal number of 
94c00 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f  times before ano
94c10 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63  ther thread.** c
94c20 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68  an enter.  If th
94c30 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72  e same thread tr
94c40 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79  ies to enter any
94c50 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d   other kind of m
94c60 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61  utex.** more tha
94c70 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61  n once, the beha
94c80 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
94c90 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
94ca0 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 72 28  d os2MutexEnter(
94cb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
94cc0 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20  ){.  TID tid;.  
94cd0 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55  PID holder1;.  U
94ce0 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20  LONG holder2;.  
94cf0 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
94d00 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  n;.  assert( p->
94d10 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
94d20 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73  _RECURSIVE || os
94d30 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29  2MutexNotheld(p)
94d40 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 73 74   );.  DosRequest
94d50 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65  MutexSem(p->mute
94d60 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e 49 54  x, SEM_INDEFINIT
94d70 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 51 75  E_WAIT);.  DosQu
94d80 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d  eryMutexSem(p->m
94d90 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20  utex, &holder1, 
94da0 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b  &tid, &holder2);
94db0 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69  .  p->owner = ti
94dc0 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a  d;.  p->nRef++;.
94dd0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  }.static int os2
94de0 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33  MutexTry(sqlite3
94df0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e  _mutex *p){.  in
94e00 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64 3b  t rc;.  TID tid;
94e10 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a  .  PID holder1;.
94e20 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b    ULONG holder2;
94e30 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
94e40 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
94e50 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
94e60 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
94e70 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75  CURSIVE || os2Mu
94e80 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
94e90 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65 73  .  if( DosReques
94ea0 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74  tMutexSem(p->mut
94eb0 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41 54  ex, SEM_IMMEDIAT
94ec0 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f 5f  E_RETURN) == NO_
94ed0 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 6f 73  ERROR) {.    Dos
94ee0 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d  QueryMutexSem(p-
94ef0 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31  >mutex, &holder1
94f00 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32  , &tid, &holder2
94f10 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20  );.    p->owner 
94f20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e 6e 52  = tid;.    p->nR
94f30 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53  ef++;.    rc = S
94f40 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65 6c  QLITE_OK;.  } el
94f50 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  se {.    rc = SQ
94f60 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a  LITE_BUSY;.  }..
94f70 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
94f80 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
94f90 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20  3_mutex_leave() 
94fa0 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20  routine exits a 
94fb0 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a  mutex that was.*
94fc0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74  * previously ent
94fd0 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65  ered by the same
94fe0 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65   thread.  The be
94ff0 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64  havior.** is und
95000 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75  efined if the mu
95010 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65  tex is not curre
95020 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a  ntly entered or.
95030 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  ** is not curren
95040 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20  tly allocated.  
95050 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65  SQLite will neve
95060 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a  r do either..*/.
95070 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 4d  static void os2M
95080 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65  utexLeave(sqlite
95090 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54  3_mutex *p){.  T
950a0 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 6f  ID tid;.  PID ho
950b0 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68  lder1;.  ULONG h
950c0 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 3d  older2;.  if( p=
950d0 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  =0 ) return;.  a
950e0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30  ssert( p->nRef>0
950f0 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d 75   );.  DosQueryMu
95100 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c  texSem(p->mutex,
95110 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c   &holder1, &tid,
95120 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 61 73   &holder2);.  as
95130 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d 3d  sert( p->owner==
95140 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66  tid );.  p->nRef
95150 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  --;.  assert( p-
95160 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69  >nRef==0 || p->i
95170 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
95180 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44  RECURSIVE );.  D
95190 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 53 65  osReleaseMutexSe
951a0 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a  m(p->mutex);.}..
951b0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
951c0 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  BUG./*.** The sq
951d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
951e0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
951f0 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72  utex_notheld() r
95200 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e  outine are.** in
95210 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69  tended for use i
95220 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73  nside assert() s
95230 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74  tatements..*/.st
95240 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65  atic int os2Mute
95250 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  xHeld(sqlite3_mu
95260 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74  tex *p){.  TID t
95270 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a 20  id;.  PID pid;. 
95280 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a   ULONG ulCount;.
95290 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 69    PTIB ptib;.  i
952a0 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20 20 20  f( p!=0 ) {.    
952b0 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d  DosQueryMutexSem
952c0 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c  (p->mutex, &pid,
952d0 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29   &tid, &ulCount)
952e0 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
952f0 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b   DosGetInfoBlock
95300 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a  s(&ptib, NULL);.
95310 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e      tid = ptib->
95320 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f  tib_ptib2->tib2_
95330 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74  ultid;.  }.  ret
95340 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 2d 3e  urn p==0 || (p->
95350 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f 77  nRef!=0 && p->ow
95360 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74 61  ner==tid);.}.sta
95370 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78  tic int os2Mutex
95380 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f  Notheld(sqlite3_
95390 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44  mutex *p){.  TID
953a0 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b   tid;.  PID pid;
953b0 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74  .  ULONG ulCount
953c0 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20  ;.  PTIB ptib;. 
953d0 20 69 66 28 20 70 21 3d 20 30 20 29 20 7b 0a 20   if( p!= 0 ) {. 
953e0 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78     DosQueryMutex
953f0 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70  Sem(p->mutex, &p
95400 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75  id, &tid, &ulCou
95410 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  nt);.  } else {.
95420 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c      DosGetInfoBl
95430 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c  ocks(&ptib, NULL
95440 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69  );.    tid = pti
95450 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69  b->tib_ptib2->ti
95460 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20  b2_ultid;.  }.  
95470 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70  return p==0 || p
95480 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
95490 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d 0a 23 65  owner!=tid;.}.#e
954a0 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49  ndif..SQLITE_PRI
954b0 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74  VATE sqlite3_mut
954c0 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69  ex_methods *sqli
954d0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28  te3DefaultMutex(
954e0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
954f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
95500 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b  thods sMutex = {
95510 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 6e 69  .    os2MutexIni
95520 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 45  t,.    os2MutexE
95530 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  nd,.    os2Mutex
95540 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 4d 75  Alloc,.    os2Mu
95550 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f 73 32  texFree,.    os2
95560 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20  MutexEnter,.    
95570 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 20 20  os2MutexTry,.   
95580 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 2c 0a   os2MutexLeave,.
95590 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
955a0 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  BUG.    os2Mutex
955b0 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74  Held,.    os2Mut
955c0 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69 66  exNotheld.#endif
955d0 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20  .  };..  return 
955e0 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69  &sMutex;.}.#endi
955f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45  f /* SQLITE_MUTE
95600 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  X_OS2 */../*****
95610 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
95620 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a   mutex_os2.c ***
95630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95650 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
95660 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
95670 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78 2e  file mutex_unix.
95680 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
95690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
956a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
956b0 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a  2007 August 28.*
956c0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
956d0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
956e0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
956f0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
95700 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
95710 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
95720 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
95730 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
95740 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
95750 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
95760 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
95770 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
95780 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
95790 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
957a0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
957b0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
957c0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
957d0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
957e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
957f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
95820 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
95830 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
95840 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
95850 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 70  nt mutexes for p
95860 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24 49  threads.**.** $I
95870 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 2c  d: mutex_unix.c,
95880 76 20 31 2e 31 36 20 32 30 30 38 2f 31 32 2f 30  v 1.16 2008/12/0
95890 38 20 31 38 3a 31 39 3a 31 38 20 64 72 68 20 45  8 18:19:18 drh E
958a0 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
958b0 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
958c0 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65  file is only use
958d0 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70  d if we are comp
958e0 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 66 65  iling threadsafe
958f0 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 20 77  .** under unix w
95900 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a 2a 2a  ith pthreads..**
95910 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68  .** Note that th
95920 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
95930 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 65 72  n requires a ver
95940 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 64 73  sion of pthreads
95950 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72 74   that.** support
95960 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  s recursive mute
95970 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  xes..*/.#ifdef S
95980 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52  QLITE_MUTEX_PTHR
95990 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20 3c  EADS..#include <
959a0 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a  pthread.h>.../*.
959b0 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 76  ** Each recursiv
959c0 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e  e mutex is an in
959d0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
959e0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
959f0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c  e..*/.struct sql
95a00 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 70  ite3_mutex {.  p
95a10 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d  thread_mutex_t m
95a20 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75 74  utex;     /* Mut
95a30 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74  ex controlling t
95a40 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  he lock */.  int
95a50 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20   id;            
95a60 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78          /* Mutex
95a70 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   type */.  int n
95a80 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
95a90 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
95aa0 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a  of entrances */.
95ab0 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 6e 65    pthread_t owne
95ac0 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
95ad0 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20 77  Thread that is w
95ae0 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65 78  ithin this mutex
95af0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
95b00 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 74 72  E_DEBUG.  int tr
95b10 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ace;            
95b20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
95b30 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a 2f  trace changes */
95b40 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 65  .#endif.};.#ifde
95b50 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23  f SQLITE_DEBUG.#
95b60 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d  define SQLITE3_M
95b70 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
95b80 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58   { PTHREAD_MUTEX
95b90 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c  _INITIALIZER, 0,
95ba0 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30   0, (pthread_t)0
95bb0 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66  , 0 }.#else.#def
95bc0 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45  ine SQLITE3_MUTE
95bd0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20  X_INITIALIZER { 
95be0 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e  PTHREAD_MUTEX_IN
95bf0 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c  ITIALIZER, 0, 0,
95c00 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 7d 0a   (pthread_t)0 }.
95c10 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
95c20 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
95c30 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74  held() and sqlit
95c40 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
95c50 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a  () routine are.*
95c60 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  * intended for u
95c70 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61  se only inside a
95c80 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
95c90 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c 61  ts.  On some pla
95ca0 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 65  tforms,.** there
95cb0 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 20 63   might be race c
95cc0 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 63  onditions that c
95cd0 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 20 72  an cause these r
95ce0 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 65  outines to.** de
95cf0 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 74 20  liver incorrect 
95d00 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 61 72  results.  In par
95d10 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 68 72  ticular, if pthr
95d20 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 0a 2a  ead_equal() is.*
95d30 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63 20  * not an atomic 
95d40 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20  operation, then 
95d50 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d  these routines m
95d60 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a 2a 2a  ight delivery.**
95d70 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c   incorrect resul
95d80 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c 61  ts.  On most pla
95d90 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 64 5f  tforms, pthread_
95da0 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a 2a 2a  equal() is a .**
95db0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74   comparison of t
95dc0 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20  wo integers and 
95dd0 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 74 6f  is therefore ato
95de0 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 72 65  mic.  But we are
95df0 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 48 50  .** told that HP
95e00 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 20 61  UX is not such a
95e10 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20 73   platform.  If s
95e20 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f  o, then these ro
95e30 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 6e  utines.** will n
95e40 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b 20 63  ot always work c
95e50 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 55 58  orrectly on HPUX
95e60 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 65  ..**.** On those
95e70 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 72 65   platforms where
95e80 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29   pthread_equal()
95e90 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c 20   is not atomic, 
95ea0 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c 64  SQLite.** should
95eb0 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   be compiled wit
95ec0 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f 44 45  hout -DSQLITE_DE
95ed0 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d 44 4e  BUG and with -DN
95ee0 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b 65  DEBUG to.** make
95ef0 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74 28   sure no assert(
95f00 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  ) statements are
95f10 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 68   evaluated and h
95f20 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 72 6f  ence these.** ro
95f30 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65 72  utines are never
95f40 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 20   called..*/.#if 
95f50 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
95f60 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
95f70 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63  TE_DEBUG).static
95f80 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65   int pthreadMute
95f90 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  xHeld(sqlite3_mu
95fa0 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  tex *p){.  retur
95fb0 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26  n (p->nRef!=0 &&
95fc0 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70   pthread_equal(p
95fd0 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64  ->owner, pthread
95fe0 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 74 61  _self()));.}.sta
95ff0 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d  tic int pthreadM
96000 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69  utexNotheld(sqli
96010 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
96020 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d   return p->nRef=
96030 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f 65 71  =0 || pthread_eq
96040 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74  ual(p->owner, pt
96050 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d 30  hread_self())==0
96060 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
96070 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64  * Initialize and
96080 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68   deinitialize th
96090 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
960a0 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m..*/.static int
960b0 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69   pthreadMutexIni
960c0 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  t(void){ return 
960d0 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61  SQLITE_OK; }.sta
960e0 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d  tic int pthreadM
960f0 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72  utexEnd(void){ r
96100 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
96110 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71   }../*.** The sq
96120 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
96130 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f  c() routine allo
96140 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d  cates a new.** m
96150 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73  utex and returns
96160 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
96170 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73  .  If it returns
96180 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65   NULL.** that me
96190 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78  ans that a mutex
961a0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c   could not be al
961b0 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65  located.  SQLite
961c0 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20  .** will unwind 
961d0 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65  its stack and re
961e0 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20  turn an error.  
961f0 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  The argument.** 
96200 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  to sqlite3_mutex
96210 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20  _alloc() is one 
96220 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72  of these integer
96230 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a   constants:.**.*
96240 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
96250 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
96260 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  T.** <li>  SQLIT
96270 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
96280 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  E.** <li>  SQLIT
96290 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
962a0 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53  ASTER.** <li>  S
962b0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
962c0 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20  IC_MEM.** <li>  
962d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
962e0 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e  TIC_MEM2.** <li>
962f0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
96300 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c  TATIC_PRNG.** <l
96310 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
96320 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c  _STATIC_LRU.** <
96330 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
96340 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a  X_STATIC_LRU2.**
96350 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
96360 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74   first two const
96370 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74  ants cause sqlit
96380 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
96390 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20   to create.** a 
963a0 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20  new mutex.  The 
963b0 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63  new mutex is rec
963c0 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49  ursive when SQLI
963d0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
963e0 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75  VE.** is used bu
963f0 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  t not necessaril
96400 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45  y so when SQLITE
96410 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75  _MUTEX_FAST is u
96420 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65  sed..** The mute
96430 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
96440 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
96450 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63  o make a distinc
96460 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20  tion.** between 
96470 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
96480 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54  URSIVE and SQLIT
96490 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20  E_MUTEX_FAST if 
964a0 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77  it does.** not w
964b0 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c  ant to.  But SQL
964c0 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65  ite will only re
964d0 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76  quest a recursiv
964e0 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61  e mutex in.** ca
964f0 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61  ses where it rea
96500 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20  lly needs one.  
96510 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d  If a faster non-
96520 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a  recursive mutex.
96530 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
96540 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  n is available o
96550 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66  n the host platf
96560 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73  orm, the mutex s
96570 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68  ubsystem.** migh
96580 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20  t return such a 
96590 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  mutex in respons
965a0 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45  e to SQLITE_MUTE
965b0 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68  X_FAST..**.** Th
965c0 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20  e other allowed 
965d0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71  parameters to sq
965e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
965f0 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a  c() each return.
96600 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
96610 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73  a static preexis
96620 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 69 78  ting mutex.  Six
96630 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   static mutexes 
96640 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74  are.** used by t
96650 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69  he current versi
96660 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46  on of SQLite.  F
96670 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f  uture versions o
96680 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20  f SQLite.** may 
96690 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  add additional s
966a0 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20  tatic mutexes.  
966b0 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61  Static mutexes a
966c0 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a  re for internal.
966d0 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  ** use by SQLite
966e0 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74   only.  Applicat
966f0 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51  ions that use SQ
96700 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f  Lite mutexes sho
96710 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20  uld.** use only 
96720 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  the dynamic mute
96730 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  xes returned by 
96740 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
96750 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  T or.** SQLITE_M
96760 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a  UTEX_RECURSIVE..
96770 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
96780 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79  if one of the dy
96790 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61  namic mutex para
967a0 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d  meters (SQLITE_M
967b0 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20  UTEX_FAST.** or 
967c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
967d0 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64 20  URSIVE) is used 
967e0 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74  then sqlite3_mut
967f0 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65  ex_alloc().** re
96800 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e  turns a differen
96810 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79  t mutex on every
96820 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20   call.  But for 
96830 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d  the static .** m
96840 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20  utex types, the 
96850 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65  same mutex is re
96860 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20  turned on every 
96870 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a  call that has.**
96880 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e   the same type n
96890 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  umber..*/.static
968a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
968b0 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f  pthreadMutexAllo
968c0 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20  c(int iType){.  
968d0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
968e0 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 78  utex staticMutex
968f0 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 51 4c  es[] = {.    SQL
96900 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
96910 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49  ALIZER,.    SQLI
96920 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  TE3_MUTEX_INITIA
96930 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54  LIZER,.    SQLIT
96940 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  E3_MUTEX_INITIAL
96950 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45  IZER,.    SQLITE
96960 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
96970 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33  ZER,.    SQLITE3
96980 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
96990 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f  ER,.    SQLITE3_
969a0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
969b0 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33  R.  };.  sqlite3
969c0 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73 77 69  _mutex *p;.  swi
969d0 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20  tch( iType ){.  
969e0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55    case SQLITE_MU
969f0 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b  TEX_RECURSIVE: {
96a00 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74  .      p = sqlit
96a10 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69  e3MallocZero( si
96a20 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20  zeof(*p) );.    
96a30 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 64 65    if( p ){.#ifde
96a40 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f  f SQLITE_HOMEGRO
96a50 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54  WN_RECURSIVE_MUT
96a60 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  EX.        /* If
96a70 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
96a80 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c  es are not avail
96a90 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20 68 61  able, we will ha
96aa0 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a  ve to.        **
96ab0 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e 2e 20   build our own. 
96ac0 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20   See below. */. 
96ad0 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
96ae0 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75  utex_init(&p->mu
96af0 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65 0a 20  tex, 0);.#else. 
96b00 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20         /* Use a 
96b10 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20  recursive mutex 
96b20 69 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62  if it is availab
96b30 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 74  le */.        pt
96b40 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f  hread_mutexattr_
96b50 74 20 72 65 63 75 72 73 69 76 65 41 74 74 72 3b  t recursiveAttr;
96b60 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
96b70 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 74 28  _mutexattr_init(
96b80 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 3b  &recursiveAttr);
96b90 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
96ba0 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 74 79  _mutexattr_setty
96bb0 70 65 28 26 72 65 63 75 72 73 69 76 65 41 74 74  pe(&recursiveAtt
96bc0 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58  r, PTHREAD_MUTEX
96bd0 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20 20  _RECURSIVE);.   
96be0 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74       pthread_mut
96bf0 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65  ex_init(&p->mute
96c00 78 2c 20 26 72 65 63 75 72 73 69 76 65 41 74 74  x, &recursiveAtt
96c10 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72  r);.        pthr
96c20 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 64 65  ead_mutexattr_de
96c30 73 74 72 6f 79 28 26 72 65 63 75 72 73 69 76 65  stroy(&recursive
96c40 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20  Attr);.#endif.  
96c50 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54        p->id = iT
96c60 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ype;.      }.   
96c70 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
96c80 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
96c90 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a 20 20  MUTEX_FAST: {.  
96ca0 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
96cb0 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
96cc0 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69  f(*p) );.      i
96cd0 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  f( p ){.        
96ce0 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20  p->id = iType;. 
96cf0 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
96d00 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75  utex_init(&p->mu
96d10 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  tex, 0);.      }
96d20 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
96d30 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
96d40 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
96d50 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b   iType-2 >= 0 );
96d60 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
96d70 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53 69  Type-2 < ArraySi
96d80 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78 65 73  ze(staticMutexes
96d90 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26  ) );.      p = &
96da0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54  staticMutexes[iT
96db0 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d  ype-2];.      p-
96dc0 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20  >id = iType;.   
96dd0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
96de0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
96df0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  }.../*.** This r
96e00 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74  outine deallocat
96e10 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a  es a previously.
96e20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74  ** allocated mut
96e30 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63  ex.  SQLite is c
96e40 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f  areful to deallo
96e50 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75  cate every.** mu
96e60 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f  tex that it allo
96e70 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  cates..*/.static
96e80 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74   void pthreadMut
96e90 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d  exFree(sqlite3_m
96ea0 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65  utex *p){.  asse
96eb0 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  rt( p->nRef==0 )
96ec0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
96ed0 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
96ee0 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  FAST || p->id==S
96ef0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
96f00 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 72 65  RSIVE );.  pthre
96f10 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f 79  ad_mutex_destroy
96f20 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73  (&p->mutex);.  s
96f30 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
96f40 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
96f50 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
96f60 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
96f70 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69  utex_try() routi
96f80 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74  nes attempt.** t
96f90 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e  o enter a mutex.
96fa0 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72    If another thr
96fb0 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77  ead is already w
96fc0 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c  ithin the mutex,
96fd0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
96fe0 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
96ff0 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
97000 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
97010 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  l return.** SQLI
97020 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71  TE_BUSY.  The sq
97030 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
97040 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
97050 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  rns SQLITE_OK.**
97060 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c   upon successful
97070 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73   entry.  Mutexes
97080 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53   created using S
97090 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
970a0 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20  RSIVE can.** be 
970b0 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  entered multiple
970c0 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61   times by the sa
970d0 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73  me thread.  In s
970e0 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
970f0 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
97100 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
97110 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
97120 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
97130 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
97140 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65  er.  If the same
97150 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f   thread tries to
97160 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72   enter any other
97170 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a   kind of mutex.*
97180 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
97190 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  , the behavior i
971a0 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
971b0 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68 72  static void pthr
971c0 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73 71  eadMutexEnter(sq
971d0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
971e0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
971f0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
97200 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 72  ECURSIVE || pthr
97210 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  eadMutexNotheld(
97220 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  p) );..#ifdef SQ
97230 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52  LITE_HOMEGROWN_R
97240 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20  ECURSIVE_MUTEX. 
97250 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 65   /* If recursive
97260 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74   mutexes are not
97270 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e   available, then
97280 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f 77   we have to grow
97290 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20  .  ** our own.  
972a0 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  This implementat
972b0 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74  ion assumes that
972c0 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29   pthread_equal()
972d0 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20  .  ** is atomic 
972e0 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74  - that it cannot
972f0 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e 74   be deceived int
97300 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a  o thinking self.
97310 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65    ** and p->owne
97320 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 70  r are equal if p
97330 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20  ->owner changes 
97340 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75  between two valu
97350 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65  es.  ** that are
97360 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65   not equal to se
97370 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d  lf while the com
97380 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e  parison is takin
97390 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68  g place..  ** Th
973a0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
973b0 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61  n also assumes a
973c0 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20   coherent cache 
973d0 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 70  - that .  ** sep
973e0 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 20  arate processes 
973f0 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 66  cannot read diff
97400 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 6f  erent values fro
97410 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20  m the same.  ** 
97420 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 73  address at the s
97430 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 69  ame time.  If ei
97440 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77  ther of these tw
97450 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a  o conditions.  *
97460 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74  * are not met, t
97470 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20  hen the mutexes 
97480 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 72  will fail and pr
97490 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 75  oblems will resu
974a0 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20  lt..  */.  {.   
974b0 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20   pthread_t self 
974c0 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
974d0 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65  ;.    if( p->nRe
974e0 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f 65  f>0 && pthread_e
974f0 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73  qual(p->owner, s
97500 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 2d  elf) ){.      p-
97510 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 6c  >nRef++;.    }el
97520 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 65 61  se{.      pthrea
97530 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d  d_mutex_lock(&p-
97540 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 61  >mutex);.      a
97550 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
97560 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77  0 );.      p->ow
97570 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20  ner = self;.    
97580 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20    p->nRef = 1;. 
97590 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20     }.  }.#else. 
975a0 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c   /* Use the buil
975b0 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d  t-in recursive m
975c0 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 61  utexes if they a
975d0 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20  re available..  
975e0 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74  */.  pthread_mut
975f0 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65  ex_lock(&p->mute
97600 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d  x);.  p->owner =
97610 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b   pthread_self();
97620 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 65  .  p->nRef++;.#e
97630 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
97640 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20  ITE_DEBUG.  if( 
97650 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20  p->trace ){.    
97660 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75  printf("enter mu
97670 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68  tex %p (%d) with
97680 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20   nRef=%d\n", p, 
97690 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65  p->trace, p->nRe
976a0 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  f);.  }.#endif.}
976b0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72  .static int pthr
976c0 65 61 64 4d 75 74 65 78 54 72 79 28 73 71 6c 69  eadMutexTry(sqli
976d0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
976e0 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
976f0 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
97700 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
97710 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78   || pthreadMutex
97720 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23  Notheld(p) );..#
97730 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d  ifdef SQLITE_HOM
97740 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45  EGROWN_RECURSIVE
97750 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72  _MUTEX.  /* If r
97760 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73  ecursive mutexes
97770 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62   are not availab
97780 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65  le, then we have
97790 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75   to grow.  ** ou
977a0 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70  r own.  This imp
977b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75  lementation assu
977c0 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 64  mes that pthread
977d0 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73  _equal().  ** is
977e0 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69   atomic - that i
977f0 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65  t cannot be dece
97800 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69  ived into thinki
97810 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64  ng self.  ** and
97820 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71   p->owner are eq
97830 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20  ual if p->owner 
97840 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20  changes between 
97850 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20  two values.  ** 
97860 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75  that are not equ
97870 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65  al to self while
97880 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
97890 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e  is taking place.
978a0 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  .  ** This imple
978b0 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61  mentation also a
978c0 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e  ssumes a coheren
978d0 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a  t cache - that .
978e0 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72    ** separate pr
978f0 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72  ocesses cannot r
97900 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 61  ead different va
97910 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61  lues from the sa
97920 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20  me.  ** address 
97930 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
97940 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 20  .  If either of 
97950 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74  these two condit
97960 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f  ions.  ** are no
97970 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20  t met, then the 
97980 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69  mutexes will fai
97990 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77  l and problems w
979a0 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f  ill result..  */
979b0 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64  .  {.    pthread
979c0 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61  _t self = pthrea
979d0 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66  d_self();.    if
979e0 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70  ( p->nRef>0 && p
979f0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e  thread_equal(p->
97a00 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a  owner, self) ){.
97a10 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b        p->nRef++;
97a20 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
97a30 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  TE_OK;.    }else
97a40 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74   if( pthread_mut
97a50 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d  ex_trylock(&p->m
97a60 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20  utex)==0 ){.    
97a70 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
97a80 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d  f==0 );.      p-
97a90 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20  >owner = self;. 
97aa0 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31       p->nRef = 1
97ab0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
97ac0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73  ITE_OK;.    }els
97ad0 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  e{.      rc = SQ
97ae0 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
97af0 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20  .  }.#else.  /* 
97b00 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e  Use the built-in
97b10 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
97b20 65 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61  es if they are a
97b30 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20  vailable..  */. 
97b40 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74   if( pthread_mut
97b50 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d  ex_trylock(&p->m
97b60 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20  utex)==0 ){.    
97b70 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65  p->owner = pthre
97b80 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 70  ad_self();.    p
97b90 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63  ->nRef++;.    rc
97ba0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
97bb0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
97bc0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
97bd0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
97be0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69  SQLITE_DEBUG.  i
97bf0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
97c00 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a   && p->trace ){.
97c10 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65      printf("ente
97c20 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20  r mutex %p (%d) 
97c30 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c  with nRef=%d\n",
97c40 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d   p, p->trace, p-
97c50 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64  >nRef);.  }.#end
97c60 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  if.  return rc;.
97c70 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
97c80 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
97c90 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73  () routine exits
97ca0 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61   a mutex that wa
97cb0 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
97cc0 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73  entered by the s
97cd0 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  ame thread.  The
97ce0 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20   behavior.** is 
97cf0 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65  undefined if the
97d00 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75   mutex is not cu
97d10 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20  rrently entered 
97d20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72  or.** is not cur
97d30 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64  rently allocated
97d40 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  .  SQLite will n
97d50 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a  ever do either..
97d60 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
97d70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65  threadMutexLeave
97d80 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
97d90 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 74  p){.  assert( pt
97da0 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28 70  hreadMutexHeld(p
97db0 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d  ) );.  p->nRef--
97dc0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
97dd0 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d  Ref==0 || p->id=
97de0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
97df0 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 66 64  CURSIVE );..#ifd
97e00 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52  ef SQLITE_HOMEGR
97e10 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55  OWN_RECURSIVE_MU
97e20 54 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65  TEX.  if( p->nRe
97e30 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 72  f==0 ){.    pthr
97e40 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b  ead_mutex_unlock
97e50 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  (&p->mutex);.  }
97e60 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 65 61 64  .#else.  pthread
97e70 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70  _mutex_unlock(&p
97e80 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69 66  ->mutex);.#endif
97e90 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
97ea0 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74  DEBUG.  if( p->t
97eb0 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e  race ){.    prin
97ec0 74 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78 20  tf("leave mutex 
97ed0 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65  %p (%d) with nRe
97ee0 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74  f=%d\n", p, p->t
97ef0 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a  race, p->nRef);.
97f00 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 51    }.#endif.}..SQ
97f10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
97f20 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
97f30 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75  ds *sqlite3Defau
97f40 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20  ltMutex(void){. 
97f50 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
97f60 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d  mutex_methods sM
97f70 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 70 74 68  utex = {.    pth
97f80 72 65 61 64 4d 75 74 65 78 49 6e 69 74 2c 0a 20  readMutexInit,. 
97f90 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 45     pthreadMutexE
97fa0 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d  nd,.    pthreadM
97fb0 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 70  utexAlloc,.    p
97fc0 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 65 2c  threadMutexFree,
97fd0 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65  .    pthreadMute
97fe0 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 74 68 72  xEnter,.    pthr
97ff0 65 61 64 4d 75 74 65 78 54 72 79 2c 0a 20 20 20  eadMutexTry,.   
98000 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61   pthreadMutexLea
98010 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ve,.#ifdef SQLIT
98020 45 5f 44 45 42 55 47 0a 20 20 20 20 70 74 68 72  E_DEBUG.    pthr
98030 65 61 64 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20  eadMutexHeld,.  
98040 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f    pthreadMutexNo
98050 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20  theld.#else.    
98060 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 66 0a  0,.    0.#endif.
98070 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26    };..  return &
98080 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64 69  sMutex;.}..#endi
98090 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45  f /* SQLITE_MUTE
980a0 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f 2a  X_PTHREAD */../*
980b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
980c0 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78 2e  d of mutex_unix.
980d0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
980e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
980f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
98100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
98110 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 77  gin file mutex_w
98120 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  32.c ***********
98130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
98150 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20  .** 2007 August 
98160 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  14.**.** The aut
98170 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
98180 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
98190 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
981a0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
981b0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
981c0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
981d0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
981e0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
981f0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
98200 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
98210 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
98220 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
98230 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
98240 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
98250 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
98260 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
98270 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
98280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
982a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
982b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
982c0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
982d0 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75  ontains the C fu
982e0 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70  nctions that imp
982f0 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66  lement mutexes f
98300 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a 2a 20 24  or win32.**.** $
98310 49 64 3a 20 6d 75 74 65 78 5f 77 33 32 2e 63 2c  Id: mutex_w32.c,
98320 76 20 31 2e 31 38 20 32 30 30 39 2f 30 38 2f 31  v 1.18 2009/08/1
98330 30 20 30 33 3a 32 33 3a 32 31 20 73 68 61 6e 65  0 03:23:21 shane
98340 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
98350 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
98360 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75  s file is only u
98370 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f  sed if we are co
98380 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68 72  mpiling multithr
98390 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 69  eaded.** on a wi
983a0 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23  n32 system..*/.#
983b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54  ifdef SQLITE_MUT
983c0 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 61  EX_W32../*.** Ea
983d0 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ch recursive mut
983e0 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  ex is an instanc
983f0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
98400 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ng structure..*/
98410 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
98420 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54 49 43  mutex {.  CRITIC
98430 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65 78  AL_SECTION mutex
98440 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f  ;    /* Mutex co
98450 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f  ntrolling the lo
98460 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20  ck */.  int id; 
98470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98480 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65     /* Mutex type
98490 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
984a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
984b0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
984c0 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 44 57  terances */.  DW
984d0 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20 20 20  ORD owner;      
984e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65           /* Thre
984f0 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20  ad holding this 
98500 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  mutex */.};../*.
98510 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 28  ** Return true (
98520 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 20  non-zero) if we 
98530 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65  are running unde
98540 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20  r WinNT, Win2K, 
98550 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e  WinXP,.** or Win
98560 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73  CE.  Return fals
98570 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e  e (zero) for Win
98580 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 69  95, Win98, or Wi
98590 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20  nME..**.** Here 
985a0 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e  is an interestin
985b0 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 20  g observation:  
985c0 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e  Win95, Win98, an
985d0 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20  d WinME lack.** 
985e0 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29  the LockFileEx()
985f0 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 63 61   API.  But we ca
98600 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 61 6c  n still statical
98610 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20  ly link against 
98620 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c  that.** API as l
98630 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 74 20  ong as we don't 
98640 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 6e 6e  call it win runn
98650 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e  ing Win95/98/ME.
98660 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74    A call to.** t
98670 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
98680 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
98690 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 20   if the host is 
986a0 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a  Win95/98/ME or.*
986b0 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f  * WinNT/2K/XP so
986c0 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e   that we will kn
986d0 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ow whether or no
986e0 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20  t we can safely 
986f0 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b  call.** the Lock
98700 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2a  FileEx() API..**
98710 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 29 20  .** mutexIsNT() 
98720 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72  is only used for
98730 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 72 69   the TryEnterCri
98740 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 41  ticalSection() A
98750 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68 69 63  PI call,.** whic
98760 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  h is only availa
98770 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 70 6c  ble if your appl
98780 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f 6d 70  ication was comp
98790 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20 5f 57  iled with .** _W
987a0 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66 69 6e  IN32_WINNT defin
987b0 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20 3e 3d  ed to a value >=
987c0 20 30 78 30 34 30 30 2e 20 20 43 75 72 72 65 6e   0x0400.  Curren
987d0 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a  tly, the only.**
987e0 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e 74 65   call to TryEnte
987f0 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  rCriticalSection
98800 28 29 20 69 73 20 23 69 66 64 65 66 27 65 64 20  () is #ifdef'ed 
98810 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66 20 0a  out, so #ifdef .
98820 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73 20 77  ** this out as w
98830 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a 23 69  ell..*/.#if 0.#i
98840 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
98850 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74 65 78  E.# define mutex
98860 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c 73  IsNT()  (1).#els
98870 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 6d  e.  static int m
98880 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29 7b 0a  utexIsNT(void){.
98890 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f      static int o
988a0 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 69  sType = 0;.    i
988b0 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b 0a  f( osType==0 ){.
988c0 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49        OSVERSIONI
988d0 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20  NFO sInfo;.     
988e0 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69   sInfo.dwOSVersi
988f0 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a  onInfoSize = siz
98900 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20  eof(sInfo);.    
98910 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26    GetVersionEx(&
98920 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f 73  sInfo);.      os
98930 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50  Type = sInfo.dwP
98940 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50  latformId==VER_P
98950 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54  LATFORM_WIN32_NT
98960 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a   ? 2 : 1;.    }.
98970 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54 79 70      return osTyp
98980 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  e==2;.  }.#endif
98990 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   /* SQLITE_OS_WI
989a0 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23  NCE */.#endif..#
989b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
989c0 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  UG./*.** The sql
989d0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
989e0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  ) and sqlite3_mu
989f0 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f  tex_notheld() ro
98a00 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74  utine are.** int
98a10 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e  ended for use on
98a20 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  ly inside assert
98a30 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  () statements..*
98a40 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
98a50 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65  MutexHeld(sqlite
98a60 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72  3_mutex *p){.  r
98a70 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21 3d 30  eturn p->nRef!=0
98a80 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65   && p->owner==Ge
98a90 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64  tCurrentThreadId
98aa0 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  ();.}.static int
98ab0 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64   winMutexNotheld
98ac0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
98ad0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  p){.  return p->
98ae0 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77  nRef==0 || p->ow
98af0 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e 74 54  ner!=GetCurrentT
98b00 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23 65 6e  hreadId();.}.#en
98b10 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  dif.../*.** Init
98b20 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69  ialize and deini
98b30 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65  tialize the mute
98b40 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a  x subsystem..*/.
98b50 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
98b60 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f 73 74  utex winMutex_st
98b70 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 3b 0a  aticMutexes[6];.
98b80 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75  static int winMu
98b90 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a  tex_isInit = 0;.
98ba0 2f 2a 20 41 73 20 77 69 6e 4d 75 74 65 78 49 6e  /* As winMutexIn
98bb0 69 74 28 29 20 61 6e 64 20 77 69 6e 4d 75 74 65  it() and winMute
98bc0 78 45 6e 64 28 29 20 61 72 65 20 63 61 6c 6c 65  xEnd() are calle
98bd0 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20  d as part.** of 
98be0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  the sqlite3_init
98bf0 69 61 6c 69 7a 65 20 61 6e 64 20 73 71 6c 69 74  ialize and sqlit
98c00 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a 2a  e3_shutdown().**
98c10 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 74 68 65   processing, the
98c20 20 22 69 6e 74 65 72 6c 6f 63 6b 65 64 22 20 6d   "interlocked" m
98c30 61 67 69 63 20 69 73 20 70 72 6f 62 61 62 6c 79  agic is probably
98c40 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 63 74 6c 79   not.** strictly
98c50 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73   necessary..*/.s
98c60 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e 4d 75  tatic long winMu
98c70 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0a 0a 73  tex_lock = 0;..s
98c80 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74  tatic int winMut
98c90 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20  exInit(void){ . 
98ca0 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 6f   /* The first to
98cb0 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 31 20   increment to 1 
98cc0 64 6f 65 73 20 61 63 74 75 61 6c 20 69 6e 69 74  does actual init
98cd0 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20  ialization */.  
98ce0 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43  if( InterlockedC
98cf0 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28 26  ompareExchange(&
98d00 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 31  winMutex_lock, 1
98d10 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  , 0)==0 ){.    i
98d20 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
98d30 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 77  0; i<ArraySize(w
98d40 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75  inMutex_staticMu
98d50 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a 20 20  texes); i++){.  
98d60 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72      InitializeCr
98d70 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 77  iticalSection(&w
98d80 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75  inMutex_staticMu
98d90 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b  texes[i].mutex);
98da0 0a 20 20 20 20 7d 0a 20 20 20 20 77 69 6e 4d 75  .    }.    winMu
98db0 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 31 3b 0a  tex_isInit = 1;.
98dc0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
98dd0 53 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 69 73 20  Someone else is 
98de0 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f  in the process o
98df0 66 20 69 6e 69 74 69 6e 67 20 74 68 65 20 73 74  f initing the st
98e00 61 74 69 63 20 6d 75 74 65 78 65 73 20 2a 2f 0a  atic mutexes */.
98e10 20 20 20 20 77 68 69 6c 65 28 20 21 77 69 6e 4d      while( !winM
98e20 75 74 65 78 5f 69 73 49 6e 69 74 20 29 7b 0a 20  utex_isInit ){. 
98e30 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20       Sleep(1);. 
98e40 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
98e50 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
98e60 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d  .static int winM
98e70 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 0a  utexEnd(void){ .
98e80 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74    /* The first t
98e90 6f 20 64 65 63 72 65 6d 65 6e 74 20 74 6f 20 30  o decrement to 0
98ea0 20 64 6f 65 73 20 61 63 74 75 61 6c 20 73 68 75   does actual shu
98eb0 74 64 6f 77 6e 20 0a 20 20 2a 2a 20 28 77 68 69  tdown .  ** (whi
98ec0 63 68 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  ch should be the
98ed0 20 6c 61 73 74 20 74 6f 20 73 68 75 74 64 6f 77   last to shutdow
98ee0 6e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 49 6e 74  n.) */.  if( Int
98ef0 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72 65 45  erlockedCompareE
98f00 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75 74 65  xchange(&winMute
98f10 78 5f 6c 6f 63 6b 2c 20 30 2c 20 31 29 3d 3d 31  x_lock, 0, 1)==1
98f20 20 29 7b 0a 20 20 20 20 69 66 28 20 77 69 6e 4d   ){.    if( winM
98f30 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 20 29  utex_isInit==1 )
98f40 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  {.      int i;. 
98f50 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
98f60 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 74  ArraySize(winMut
98f70 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex_staticMutexes
98f80 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ); i++){.       
98f90 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53   DeleteCriticalS
98fa0 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74 65 78  ection(&winMutex
98fb0 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69  _staticMutexes[i
98fc0 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ].mutex);.      
98fd0 7d 0a 20 20 20 20 20 20 77 69 6e 4d 75 74 65 78  }.      winMutex
98fe0 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20  _isInit = 0;.   
98ff0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
99000 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f  SQLITE_OK; .}../
99010 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
99020 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
99030 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
99040 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
99050 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
99060 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66  inter to it.  If
99070 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
99080 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
99090 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c  hat a mutex coul
990a0 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74  d not be allocat
990b0 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77  ed.  SQLite.** w
990c0 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73  ill unwind its s
990d0 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20  tack and return 
990e0 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61  an error.  The a
990f0 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71  rgument.** to sq
99100 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
99110 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  c() is one of th
99120 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73  ese integer cons
99130 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  tants:.**.** <ul
99140 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  >.** <li>  SQLIT
99150 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20  E_MUTEX_FAST.** 
99160 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
99170 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20  EX_RECURSIVE.** 
99180 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
99190 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
991a0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
991b0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
991c0 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  M.** <li>  SQLIT
991d0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
991e0 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  EM2.** <li>  SQL
991f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
99200 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _PRNG.** <li>  S
99210 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
99220 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20  IC_LRU.** <li>  
99230 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
99240 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c  TIC_LRU2.** </ul
99250 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  >.**.** The firs
99260 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20  t two constants 
99270 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75  cause sqlite3_mu
99280 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63  tex_alloc() to c
99290 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d  reate.** a new m
992a0 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d  utex.  The new m
992b0 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76  utex is recursiv
992c0 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  e when SQLITE_MU
992d0 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
992e0 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74   is used but not
992f0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20   necessarily so 
99300 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  when SQLITE_MUTE
99310 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a  X_FAST is used..
99320 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70  ** The mutex imp
99330 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73  lementation does
99340 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b   not need to mak
99350 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a  e a distinction.
99360 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54  ** between SQLIT
99370 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
99380 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54  E and SQLITE_MUT
99390 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f  EX_FAST if it do
993a0 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74  es.** not want t
993b0 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77  o.  But SQLite w
993c0 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74  ill only request
993d0 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74   a recursive mut
993e0 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77  ex in.** cases w
993f0 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e  here it really n
99400 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20  eeds one.  If a 
99410 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72  faster non-recur
99420 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d  sive mutex.** im
99430 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
99440 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65  available on the
99450 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20   host platform, 
99460 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
99470 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74  tem.** might ret
99480 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78  urn such a mutex
99490 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20   in response to 
994a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
994b0 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68  T..**.** The oth
994c0 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d  er allowed param
994d0 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33  eters to sqlite3
994e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65  _mutex_alloc() e
994f0 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  ach return.** a 
99500 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  pointer to a sta
99510 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20  tic preexisting 
99520 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74  mutex.  Six stat
99530 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a  ic mutexes are.*
99540 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75  * used by the cu
99550 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66  rrent version of
99560 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65   SQLite.  Future
99570 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
99580 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61  ite.** may add a
99590 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63  dditional static
995a0 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69   mutexes.  Stati
995b0 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f  c mutexes are fo
995c0 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73  r internal.** us
995d0 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79  e by SQLite only
995e0 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20  .  Applications 
995f0 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20  that use SQLite 
99600 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a  mutexes should.*
99610 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64  * use only the d
99620 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72  ynamic mutexes r
99630 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54  eturned by SQLIT
99640 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a  E_MUTEX_FAST or.
99650 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ** SQLITE_MUTEX_
99660 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a  RECURSIVE..**.**
99670 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e   Note that if on
99680 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63  e of the dynamic
99690 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72   mutex parameter
996a0 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  s (SQLITE_MUTEX_
996b0 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54  FAST.** or SQLIT
996c0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
996d0 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20  E) is used then 
996e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
996f0 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73  loc().** returns
99700 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74   a different mut
99710 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c  ex on every call
99720 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73  .  But for the s
99730 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20  tatic .** mutex 
99740 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20  types, the same 
99750 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65  mutex is returne
99760 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20  d on every call 
99770 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20  that has.** the 
99780 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72  same type number
99790 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69  ..*/.static sqli
997a0 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d 75  te3_mutex *winMu
997b0 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79  texAlloc(int iTy
997c0 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  pe){.  sqlite3_m
997d0 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69 74  utex *p;..  swit
997e0 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20  ch( iType ){.   
997f0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54   case SQLITE_MUT
99800 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73  EX_FAST:.    cas
99810 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  e SQLITE_MUTEX_R
99820 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20  ECURSIVE: {.    
99830 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
99840 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28  locZero( sizeof(
99850 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  *p) );.      if(
99860 20 70 20 29 7b 20 20 0a 20 20 20 20 20 20 20 20   p ){  .        
99870 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20  p->id = iType;. 
99880 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a         Initializ
99890 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  eCriticalSection
998a0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  (&p->mutex);.   
998b0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
998c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
998d0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ult: {.      ass
998e0 65 72 74 28 20 77 69 6e 4d 75 74 65 78 5f 69 73  ert( winMutex_is
998f0 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 20 20 20  Init==1 );.     
99900 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32   assert( iType-2
99910 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61   >= 0 );.      a
99920 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c  ssert( iType-2 <
99930 20 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75   ArraySize(winMu
99940 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65  tex_staticMutexe
99950 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20  s) );.      p = 
99960 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63  &winMutex_static
99970 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d  Mutexes[iType-2]
99980 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20  ;.      p->id = 
99990 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65  iType;.      bre
999a0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
999b0 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a  return p;.}.../*
999c0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
999d0 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70   deallocates a p
999e0 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c  reviously.** all
999f0 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53  ocated mutex.  S
99a00 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 6c  QLite is careful
99a10 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65   to deallocate e
99a20 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68  very.** mutex th
99a30 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e  at it allocates.
99a40 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
99a50 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 71 6c  winMutexFree(sql
99a60 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
99a70 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20    assert( p );. 
99a80 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
99a90 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
99aa0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
99ab0 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e  UTEX_FAST || p->
99ac0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
99ad0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
99ae0 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65  DeleteCriticalSe
99af0 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29  ction(&p->mutex)
99b00 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
99b10 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  (p);.}../*.** Th
99b20 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
99b30 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69  enter() and sqli
99b40 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
99b50 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74  routines attempt
99b60 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d  .** to enter a m
99b70 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65  utex.  If anothe
99b80 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65  r thread is alre
99b90 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d  ady within the m
99ba0 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  utex,.** sqlite3
99bb0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77  _mutex_enter() w
99bc0 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71  ill block and sq
99bd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
99be0 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a  ) will return.**
99bf0 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54   SQLITE_BUSY.  T
99c00 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
99c10 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65  _try() interface
99c20 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
99c30 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65  OK.** upon succe
99c40 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75  ssful entry.  Mu
99c50 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73  texes created us
99c60 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ing SQLITE_MUTEX
99c70 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a  _RECURSIVE can.*
99c80 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c  * be entered mul
99c90 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74  tiple times by t
99ca0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
99cb0 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74   In such cases t
99cc0 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73  he,.** mutex mus
99cd0 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65  t be exited an e
99ce0 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74  qual number of t
99cf0 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74  imes before anot
99d00 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61  her thread.** ca
99d10 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65  n enter.  If the
99d20 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69   same thread tri
99d30 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20  es to enter any 
99d40 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75  other kind of mu
99d50 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e  tex.** more than
99d60 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76   once, the behav
99d70 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
99d80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
99d90 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 73   winMutexEnter(s
99da0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
99db0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  {.  assert( p->i
99dc0 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
99dd0 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e  RECURSIVE || win
99de0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20  MutexNotheld(p) 
99df0 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63  );.  EnterCritic
99e00 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75  alSection(&p->mu
99e10 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72  tex);.  p->owner
99e20 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72   = GetCurrentThr
99e30 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e  eadId(); .  p->n
99e40 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20  Ref++;.}.static 
99e50 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 79 28  int winMutexTry(
99e60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
99e70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
99e80 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 73  LITE_BUSY;.  ass
99e90 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
99ea0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
99eb0 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f  VE || winMutexNo
99ec0 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a  theld(p) );.  /*
99ed0 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  .  ** The sqlite
99ee0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f  3_mutex_try() ro
99ef0 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 72 61  utine is very ra
99f00 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77  rely used, and w
99f10 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75  hen it.  ** is u
99f20 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79  sed it is merely
99f30 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   an optimization
99f40 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66  .  So it is OK f
99f50 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a  or it to always.
99f60 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a    ** fail.  .  *
99f70 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e  *.  ** The TryEn
99f80 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
99f90 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 69  on() interface i
99fa0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
99fb0 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20   on WinNT..  ** 
99fc0 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73  And some windows
99fd0 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c   compilers compl
99fe0 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74  ain if you try t
99ff0 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f 75 74  o use it without
9a000 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e  .  ** first doin
9a010 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20  g some #defines 
9a020 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c  that prevent SQL
9a030 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e  ite from buildin
9a040 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a  g on Win98..  **
9a050 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e   For that reason
9a060 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74  , we will omit t
9a070 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
9a080 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20   for now.  See. 
9a090 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35   ** ticket #2685
9a0a0 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69  ..  */.#if 0.  i
9a0b0 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26  f( mutexIsNT() &
9a0c0 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63  & TryEnterCritic
9a0d0 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75  alSection(&p->mu
9a0e0 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f  tex) ){.    p->o
9a0f0 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e  wner = GetCurren
9a100 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20  tThreadId();.   
9a110 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20   p->nRef++;.    
9a120 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
9a130 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53    }.#else.  UNUS
9a140 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 3b  ED_PARAMETER(p);
9a150 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
9a160 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
9a170 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
9a180 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20  leave() routine 
9a190 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68  exits a mutex th
9a1a0 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f  at was.** previo
9a1b0 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20  usly entered by 
9a1c0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e  the same thread.
9a1d0 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a    The behavior.*
9a1e0 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69  * is undefined i
9a1f0 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e  f the mutex is n
9a200 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74  ot currently ent
9a210 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f  ered or.** is no
9a220 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f  t currently allo
9a230 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77  cated.  SQLite w
9a240 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74  ill never do eit
9a250 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  her..*/.static v
9a260 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 61 76  oid winMutexLeav
9a270 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
9a280 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
9a290 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61 73  ->nRef>0 );.  as
9a2a0 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d 3d  sert( p->owner==
9a2b0 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64  GetCurrentThread
9a2c0 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65  Id() );.  p->nRe
9a2d0 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  f--;.  assert( p
9a2e0 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
9a2f0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
9a300 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
9a310 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53 65 63  LeaveCriticalSec
9a320 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b  tion(&p->mutex);
9a330 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
9a340 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  TE sqlite3_mutex
9a350 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65  _methods *sqlite
9a360 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f  3DefaultMutex(vo
9a370 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71  id){.  static sq
9a380 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
9a390 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20  ods sMutex = {. 
9a3a0 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 2c     winMutexInit,
9a3b0 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e 64  .    winMutexEnd
9a3c0 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 41 6c  ,.    winMutexAl
9a3d0 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65  loc,.    winMute
9a3e0 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e 4d 75  xFree,.    winMu
9a3f0 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 77 69  texEnter,.    wi
9a400 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 77  nMutexTry,.    w
9a410 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69  inMutexLeave,.#i
9a420 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
9a430 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 48 65  G.    winMutexHe
9a440 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  ld,.    winMutex
9a450 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20  Notheld.#else.  
9a460 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69    0,.    0.#endi
9a470 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e  f.  };..  return
9a480 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64   &sMutex;.}.#end
9a490 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
9a4a0 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  EX_W32 */../****
9a4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
9a4c0 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a  f mutex_w32.c **
9a4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
9a500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
9a510 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a   file malloc.c *
9a520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
9a550 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
9a560 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
9a570 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
9a580 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
9a590 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
9a5a0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
9a5b0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
9a5c0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
9a5d0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
9a5e0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
9a5f0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
9a600 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
9a610 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
9a620 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
9a630 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
9a640 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
9a650 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
9a660 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
9a670 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
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 2a 2a 2a 2a 2a 2a 2a  ****************
9a6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a6c0 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61  *.**.** Memory a
9a6d0 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
9a6e0 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68  ons used through
9a6f0 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a  out sqlite..**.*
9a700 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63 2c  * $Id: malloc.c,
9a710 76 20 31 2e 36 36 20 32 30 30 39 2f 30 37 2f 31  v 1.66 2009/07/1
9a720 37 20 31 31 3a 34 34 3a 30 37 20 64 72 68 20 45  7 11:44:07 drh E
9a730 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
9a740 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73  his routine runs
9a750 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79   when the memory
9a760 20 61 6c 6c 6f 63 61 74 6f 72 20 73 65 65 73 20   allocator sees 
9a770 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 6f 74 61  that the.** tota
9a780 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
9a790 69 6f 6e 20 69 73 20 61 62 6f 75 74 20 74 6f 20  ion is about to 
9a7a0 65 78 63 65 65 64 20 74 68 65 20 73 6f 66 74 20  exceed the soft 
9a7b0 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 74 2e 0a 2a  heap.** limit..*
9a7c0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6f  /.static void so
9a7d0 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72  ftHeapLimitEnfor
9a7e0 63 65 72 28 0a 20 20 76 6f 69 64 20 2a 4e 6f 74  cer(.  void *Not
9a7f0 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33  Used, .  sqlite3
9a800 5f 69 6e 74 36 34 20 4e 6f 74 55 73 65 64 32 2c  _int64 NotUsed2,
9a810 0a 20 20 69 6e 74 20 61 6c 6c 6f 63 53 69 7a 65  .  int allocSize
9a820 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  .){.  UNUSED_PAR
9a830 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
9a840 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71   NotUsed2);.  sq
9a850 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
9a860 6d 6f 72 79 28 61 6c 6c 6f 63 53 69 7a 65 29 3b  mory(allocSize);
9a870 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
9a880 65 20 73 6f 66 74 20 68 65 61 70 2d 73 69 7a 65  e soft heap-size
9a890 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 65 20 6c   limit for the l
9a8a0 69 62 72 61 72 79 2e 20 50 61 73 73 69 6e 67 20  ibrary. Passing 
9a8b0 61 20 7a 65 72 6f 20 6f 72 20 0a 2a 2a 20 6e 65  a zero or .** ne
9a8c0 67 61 74 69 76 65 20 76 61 6c 75 65 20 69 6e 64  gative value ind
9a8d0 69 63 61 74 65 73 20 6e 6f 20 6c 69 6d 69 74 2e  icates no limit.
9a8e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
9a8f0 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66 74  oid sqlite3_soft
9a900 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74 20  _heap_limit(int 
9a910 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 75 69  n){.  sqlite3_ui
9a920 6e 74 36 34 20 69 4c 69 6d 69 74 3b 0a 20 20 69  nt64 iLimit;.  i
9a930 6e 74 20 6f 76 65 72 61 67 65 3b 0a 20 20 69 66  nt overage;.  if
9a940 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 69 4c 69  ( n<0 ){.    iLi
9a950 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  mit = 0;.  }else
9a960 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 6e  {.    iLimit = n
9a970 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  ;.  }.#ifndef SQ
9a980 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
9a990 49 54 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 69  IT.  sqlite3_ini
9a9a0 74 69 61 6c 69 7a 65 28 29 3b 0a 23 65 6e 64 69  tialize();.#endi
9a9b0 66 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 30  f.  if( iLimit>0
9a9c0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d   ){.    sqlite3M
9a9d0 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f 66 74 48  emoryAlarm(softH
9a9e0 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72  eapLimitEnforcer
9a9f0 2c 20 30 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20  , 0, iLimit);.  
9aa00 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
9aa10 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30 2c  e3MemoryAlarm(0,
9aa20 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 6f 76   0, 0);.  }.  ov
9aa30 65 72 61 67 65 20 3d 20 28 69 6e 74 29 28 73 71  erage = (int)(sq
9aa40 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65  lite3_memory_use
9aa50 64 28 29 20 2d 20 28 69 36 34 29 6e 29 3b 0a 20  d() - (i64)n);. 
9aa60 20 69 66 28 20 6f 76 65 72 61 67 65 3e 30 20 29   if( overage>0 )
9aa70 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
9aa80 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f 76 65  lease_memory(ove
9aa90 72 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rage);.  }.}../*
9aaa0 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72  .** Attempt to r
9aab0 65 6c 65 61 73 65 20 75 70 20 74 6f 20 6e 20 62  elease up to n b
9aac0 79 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73 73 65  ytes of non-esse
9aad0 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63 75 72  ntial memory cur
9aae0 72 65 6e 74 6c 79 0a 2a 2a 20 68 65 6c 64 20 62  rently.** held b
9aaf0 79 20 53 51 4c 69 74 65 2e 20 41 6e 20 65 78 61  y SQLite. An exa
9ab00 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65  mple of non-esse
9ab10 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69 73 20  ntial memory is 
9ab20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 0a 2a  memory used to.*
9ab30 2a 20 63 61 63 68 65 20 64 61 74 61 62 61 73 65  * cache database
9ab40 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20   pages that are 
9ab50 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  not currently in
9ab60 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   use..*/.SQLITE_
9ab70 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
9ab80 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69  release_memory(i
9ab90 6e 74 20 6e 29 7b 0a 23 69 66 64 65 66 20 53 51  nt n){.#ifdef SQ
9aba0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
9abb0 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20  RY_MANAGEMENT.  
9abc0 69 6e 74 20 6e 52 65 74 20 3d 20 30 3b 0a 20 20  int nRet = 0;.  
9abd0 6e 52 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 50  nRet += sqlite3P
9abe0 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f  cacheReleaseMemo
9abf0 72 79 28 6e 2d 6e 52 65 74 29 3b 0a 20 20 72 65  ry(n-nRet);.  re
9ac00 74 75 72 6e 20 6e 52 65 74 3b 0a 23 65 6c 73 65  turn nRet;.#else
9ac10 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
9ac20 54 45 52 28 6e 29 3b 0a 20 20 72 65 74 75 72 6e  TER(n);.  return
9ac30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64   SQLITE_OK;.#end
9ac40 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74  if.}../*.** Stat
9ac50 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6c 6f  e information lo
9ac60 63 61 6c 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72  cal to the memor
9ac70 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
9ac80 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69  system..*/.stati
9ac90 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  c SQLITE_WSD str
9aca0 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b  uct Mem0Global {
9acb0 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  .  /* Number of 
9acc0 66 72 65 65 20 70 61 67 65 73 20 66 6f 72 20 73  free pages for s
9acd0 63 72 61 74 63 68 20 61 6e 64 20 70 61 67 65 2d  cratch and page-
9ace0 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a  cache memory */.
9acf0 20 20 75 33 32 20 6e 53 63 72 61 74 63 68 46 72    u32 nScratchFr
9ad00 65 65 3b 0a 20 20 75 33 32 20 6e 50 61 67 65 46  ee;.  u32 nPageF
9ad10 72 65 65 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  ree;..  sqlite3_
9ad20 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20  mutex *mutex;   
9ad30 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74        /* Mutex t
9ad40 6f 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63 65  o serialize acce
9ad50 73 73 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ss */..  /*.  **
9ad60 20 54 68 65 20 61 6c 61 72 6d 20 63 61 6c 6c 62   The alarm callb
9ad70 61 63 6b 20 61 6e 64 20 69 74 73 20 61 72 67 75  ack and its argu
9ad80 6d 65 6e 74 73 2e 20 20 54 68 65 20 6d 65 6d 30  ments.  The mem0
9ad90 2e 6d 75 74 65 78 20 6c 6f 63 6b 20 77 69 6c 6c  .mutex lock will
9ada0 0a 20 20 2a 2a 20 62 65 20 68 65 6c 64 20 77 68  .  ** be held wh
9adb0 69 6c 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  ile the callback
9adc0 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 52 65   is running.  Re
9add0 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 69 6e  cursive calls in
9ade0 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f  to.  ** the memo
9adf0 72 79 20 73 75 62 73 79 73 74 65 6d 20 61 72 65  ry subsystem are
9ae00 20 61 6c 6c 6f 77 65 64 2c 20 62 75 74 20 6e 6f   allowed, but no
9ae10 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b 73 20 77   new callbacks w
9ae20 69 6c 6c 20 62 65 0a 20 20 2a 2a 20 69 73 73 75  ill be.  ** issu
9ae30 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ed..  */.  sqlit
9ae40 65 33 5f 69 6e 74 36 34 20 61 6c 61 72 6d 54 68  e3_int64 alarmTh
9ae50 72 65 73 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20  reshold;.  void 
9ae60 28 2a 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29  (*alarmCallback)
9ae70 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f  (void*, sqlite3_
9ae80 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f  int64,int);.  vo
9ae90 69 64 20 2a 61 6c 61 72 6d 41 72 67 3b 0a 0a 20  id *alarmArg;.. 
9aea0 20 2f 2a 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 72   /*.  ** Pointer
9aeb0 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  s to the end of 
9aec0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9aed0 66 69 67 2e 70 53 63 72 61 74 63 68 20 61 6e 64  fig.pScratch and
9aee0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f  .  ** sqlite3Glo
9aef0 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20  balConfig.pPage 
9af00 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65  to a block of me
9af10 6d 6f 72 79 20 74 68 61 74 20 72 65 63 6f 72 64  mory that record
9af20 73 0a 20 20 2a 2a 20 77 68 69 63 68 20 70 61 67  s.  ** which pag
9af30 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  es are available
9af40 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53  ..  */.  u32 *aS
9af50 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33  cratchFree;.  u3
9af60 32 20 2a 61 50 61 67 65 46 72 65 65 3b 0a 7d 20  2 *aPageFree;.} 
9af70 6d 65 6d 30 20 3d 20 7b 20 30 2c 20 30 2c 20 30  mem0 = { 0, 0, 0
9af80 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20  , 0, 0, 0, 0, 0 
9af90 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 30  };..#define mem0
9afa0 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d   GLOBAL(struct M
9afb0 65 6d 30 47 6c 6f 62 61 6c 2c 20 6d 65 6d 30 29  em0Global, mem0)
9afc0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
9afd0 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ze the memory al
9afe0 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
9aff0 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  em..*/.SQLITE_PR
9b000 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
9b010 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64  3MallocInit(void
9b020 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ){.  if( sqlite3
9b030 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9b040 4d 61 6c 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20 20  Malloc==0 ){.   
9b050 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65   sqlite3MemSetDe
9b060 66 61 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 6d  fault();.  }.  m
9b070 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20  emset(&mem0, 0, 
9b080 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 20  sizeof(mem0));. 
9b090 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
9b0a0 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75  alConfig.bCoreMu
9b0b0 74 65 78 20 29 7b 0a 20 20 20 20 6d 65 6d 30 2e  tex ){.    mem0.
9b0c0 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
9b0d0 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
9b0e0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
9b0f0 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  M);.  }.  if( sq
9b100 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b110 67 2e 70 53 63 72 61 74 63 68 20 26 26 20 73 71  g.pScratch && sq
9b120 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b130 67 2e 73 7a 53 63 72 61 74 63 68 3e 3d 31 30 30  g.szScratch>=100
9b140 0a 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65  .      && sqlite
9b150 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53  3GlobalConfig.nS
9b160 63 72 61 74 63 68 3e 3d 30 20 29 7b 0a 20 20 20  cratch>=0 ){.   
9b170 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69   int i;.    sqli
9b180 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b190 73 7a 53 63 72 61 74 63 68 20 3d 20 52 4f 55 4e  szScratch = ROUN
9b1a0 44 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 6c  DDOWN8(sqlite3Gl
9b1b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
9b1c0 61 74 63 68 2d 34 29 3b 0a 20 20 20 20 6d 65 6d  atch-4);.    mem
9b1d0 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 20 3d  0.aScratchFree =
9b1e0 20 28 75 33 32 2a 29 26 28 28 63 68 61 72 2a 29   (u32*)&((char*)
9b1f0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b200 66 69 67 2e 70 53 63 72 61 74 63 68 29 0a 20 20  fig.pScratch).  
9b210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b220 5b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  [sqlite3GlobalCo
9b230 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2a 73  nfig.szScratch*s
9b240 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9b250 69 67 2e 6e 53 63 72 61 74 63 68 5d 3b 0a 20 20  ig.nScratch];.  
9b260 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c    for(i=0; i<sql
9b270 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b280 2e 6e 53 63 72 61 74 63 68 3b 20 69 2b 2b 29 7b  .nScratch; i++){
9b290 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72   mem0.aScratchFr
9b2a0 65 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20  ee[i] = i; }.   
9b2b0 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72   mem0.nScratchFr
9b2c0 65 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  ee = sqlite3Glob
9b2d0 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63  alConfig.nScratc
9b2e0 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  h;.  }else{.    
9b2f0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b300 66 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20 30  fig.pScratch = 0
9b310 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  ;.    sqlite3Glo
9b320 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61  balConfig.szScra
9b330 74 63 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  tch = 0;.  }.  i
9b340 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
9b350 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 26 26 20  Config.pPage && 
9b360 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b370 66 69 67 2e 73 7a 50 61 67 65 3e 3d 35 31 32 0a  fig.szPage>=512.
9b380 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33        && sqlite3
9b390 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61  GlobalConfig.nPa
9b3a0 67 65 3e 3d 31 20 29 7b 0a 20 20 20 20 69 6e 74  ge>=1 ){.    int
9b3b0 20 69 3b 0a 20 20 20 20 69 6e 74 20 6f 76 65 72   i;.    int over
9b3c0 68 65 61 64 3b 0a 20 20 20 20 69 6e 74 20 73 7a  head;.    int sz
9b3d0 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71   = ROUNDDOWN8(sq
9b3e0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b3f0 67 2e 73 7a 50 61 67 65 29 3b 0a 20 20 20 20 69  g.szPage);.    i
9b400 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 6c  nt n = sqlite3Gl
9b410 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65  obalConfig.nPage
9b420 3b 0a 20 20 20 20 6f 76 65 72 68 65 61 64 20 3d  ;.    overhead =
9b430 20 28 34 2a 6e 20 2b 20 73 7a 20 2d 20 31 29 2f   (4*n + sz - 1)/
9b440 73 7a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  sz;.    sqlite3G
9b450 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67  lobalConfig.nPag
9b460 65 20 2d 3d 20 6f 76 65 72 68 65 61 64 3b 0a 20  e -= overhead;. 
9b470 20 20 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65     mem0.aPageFre
9b480 65 20 3d 20 28 75 33 32 2a 29 26 28 28 63 68 61  e = (u32*)&((cha
9b490 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  r*)sqlite3Global
9b4a0 43 6f 6e 66 69 67 2e 70 50 61 67 65 29 0a 20 20  Config.pPage).  
9b4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b4c0 5b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  [sqlite3GlobalCo
9b4d0 6e 66 69 67 2e 73 7a 50 61 67 65 2a 73 71 6c 69  nfig.szPage*sqli
9b4e0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b4f0 6e 50 61 67 65 5d 3b 0a 20 20 20 20 66 6f 72 28  nPage];.    for(
9b500 69 3d 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c  i=0; i<sqlite3Gl
9b510 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65  obalConfig.nPage
9b520 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 50 61  ; i++){ mem0.aPa
9b530 67 65 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d  geFree[i] = i; }
9b540 0a 20 20 20 20 6d 65 6d 30 2e 6e 50 61 67 65 46  .    mem0.nPageF
9b550 72 65 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f  ree = sqlite3Glo
9b560 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b  balConfig.nPage;
9b570 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
9b580 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b590 67 2e 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20  g.pPage = 0;.   
9b5a0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9b5b0 6e 66 69 67 2e 73 7a 50 61 67 65 20 3d 20 30 3b  nfig.szPage = 0;
9b5c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
9b5d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b5e0 67 2e 6d 2e 78 49 6e 69 74 28 73 71 6c 69 74 65  g.m.xInit(sqlite
9b5f0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9b600 70 41 70 70 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a  pAppData);.}../*
9b610 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65  .** Deinitialize
9b620 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
9b630 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
9b640 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
9b650 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
9b660 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 7b  MallocEnd(void){
9b670 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
9b680 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68  obalConfig.m.xSh
9b690 75 74 64 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71  utdown ){.    sq
9b6a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b6b0 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 28 73 71  g.m.xShutdown(sq
9b6c0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b6d0 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b 0a 20  g.m.pAppData);. 
9b6e0 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d   }.  memset(&mem
9b6f0 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d  0, 0, sizeof(mem
9b700 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  0));.}../*.** Re
9b710 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  turn the amount 
9b720 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e  of memory curren
9b730 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e  tly checked out.
9b740 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
9b750 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c  qlite3_int64 sql
9b760 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64  ite3_memory_used
9b770 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 6e 2c  (void){.  int n,
9b780 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69   mx;.  sqlite3_i
9b790 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69  nt64 res;.  sqli
9b7a0 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54  te3_status(SQLIT
9b7b0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
9b7c0 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 30  USED, &n, &mx, 0
9b7d0 29 3b 0a 20 20 72 65 73 20 3d 20 28 73 71 6c 69  );.  res = (sqli
9b7e0 74 65 33 5f 69 6e 74 36 34 29 6e 3b 20 20 2f 2a  te3_int64)n;  /*
9b7f0 20 57 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67   Work around bug
9b800 20 69 6e 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54   in Borland C. T
9b810 69 63 6b 65 74 20 23 33 32 31 36 20 2a 2f 0a 20  icket #3216 */. 
9b820 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a   return res;.}..
9b830 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
9b840 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20   maximum amount 
9b850 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 68  of memory that h
9b860 61 73 20 65 76 65 72 20 62 65 65 6e 0a 2a 2a 20  as ever been.** 
9b870 63 68 65 63 6b 65 64 20 6f 75 74 20 73 69 6e 63  checked out sinc
9b880 65 20 65 69 74 68 65 72 20 74 68 65 20 62 65 67  e either the beg
9b890 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 70  inning of this p
9b8a0 72 6f 63 65 73 73 0a 2a 2a 20 6f 72 20 73 69 6e  rocess.** or sin
9b8b0 63 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  ce the most rece
9b8c0 6e 74 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c  nt reset..*/.SQL
9b8d0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
9b8e0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65  int64 sqlite3_me
9b8f0 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 69  mory_highwater(i
9b900 6e 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20  nt resetFlag){. 
9b910 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71   int n, mx;.  sq
9b920 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b  lite3_int64 res;
9b930 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75  .  sqlite3_statu
9b940 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  s(SQLITE_STATUS_
9b950 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c  MEMORY_USED, &n,
9b960 20 26 6d 78 2c 20 72 65 73 65 74 46 6c 61 67 29   &mx, resetFlag)
9b970 3b 0a 20 20 72 65 73 20 3d 20 28 73 71 6c 69 74  ;.  res = (sqlit
9b980 65 33 5f 69 6e 74 36 34 29 6d 78 3b 20 20 2f 2a  e3_int64)mx;  /*
9b990 20 57 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67   Work around bug
9b9a0 20 69 6e 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54   in Borland C. T
9b9b0 69 63 6b 65 74 20 23 33 32 31 36 20 2a 2f 0a 20  icket #3216 */. 
9b9c0 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a   return res;.}..
9b9d0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
9b9e0 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 0a   alarm callback.
9b9f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
9ba00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  E int sqlite3Mem
9ba10 6f 72 79 41 6c 61 72 6d 28 0a 20 20 76 6f 69 64  oryAlarm(.  void
9ba20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69  (*xCallback)(voi
9ba30 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33  d *pArg, sqlite3
9ba40 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 20  _int64 used,int 
9ba50 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  N),.  void *pArg
9ba60 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ,.  sqlite3_int6
9ba70 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a  4 iThreshold.){.
9ba80 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
9ba90 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78  enter(mem0.mutex
9baa0 29 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43  );.  mem0.alarmC
9bab0 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62  allback = xCallb
9bac0 61 63 6b 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72  ack;.  mem0.alar
9bad0 6d 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 6d  mArg = pArg;.  m
9bae0 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f  em0.alarmThresho
9baf0 6c 64 20 3d 20 69 54 68 72 65 73 68 6f 6c 64 3b  ld = iThreshold;
9bb00 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
9bb10 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65  _leave(mem0.mute
9bb20 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
9bb30 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64  ITE_OK;.}..#ifnd
9bb40 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
9bb50 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20  EPRECATED./*.** 
9bb60 44 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72  Deprecated exter
9bb70 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20  nal interface.  
9bb80 49 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51  Internal/core SQ
9bb90 4c 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f  Lite code.** sho
9bba0 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  uld call sqlite3
9bbb0 4d 65 6d 6f 72 79 41 6c 61 72 6d 2e 0a 2a 2f 0a  MemoryAlarm..*/.
9bbc0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
9bbd0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c  qlite3_memory_al
9bbe0 61 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61  arm(.  void(*xCa
9bbf0 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41  llback)(void *pA
9bc00 72 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  rg, sqlite3_int6
9bc10 34 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20  4 used,int N),. 
9bc20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73   void *pArg,.  s
9bc30 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68  qlite3_int64 iTh
9bc40 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 72 65 74  reshold.){.  ret
9bc50 75 72 6e 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72  urn sqlite3Memor
9bc60 79 41 6c 61 72 6d 28 78 43 61 6c 6c 62 61 63 6b  yAlarm(xCallback
9bc70 2c 20 70 41 72 67 2c 20 69 54 68 72 65 73 68 6f  , pArg, iThresho
9bc80 6c 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ld);.}.#endif../
9bc90 2a 0a 2a 2a 20 54 72 69 67 67 65 72 20 74 68 65  *.** Trigger the
9bca0 20 61 6c 61 72 6d 20 0a 2a 2f 0a 73 74 61 74 69   alarm .*/.stati
9bcb0 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61  c void sqlite3Ma
9bcc0 6c 6c 6f 63 41 6c 61 72 6d 28 69 6e 74 20 6e 42  llocAlarm(int nB
9bcd0 79 74 65 29 7b 0a 20 20 76 6f 69 64 20 28 2a 78  yte){.  void (*x
9bce0 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c  Callback)(void*,
9bcf0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e  sqlite3_int64,in
9bd00 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  t);.  sqlite3_in
9bd10 74 36 34 20 6e 6f 77 55 73 65 64 3b 0a 20 20 76  t64 nowUsed;.  v
9bd20 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 69 66 28  oid *pArg;.  if(
9bd30 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62   mem0.alarmCallb
9bd40 61 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ack==0 ) return;
9bd50 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d  .  xCallback = m
9bd60 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  em0.alarmCallbac
9bd70 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20 73  k;.  nowUsed = s
9bd80 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75  qlite3StatusValu
9bd90 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  e(SQLITE_STATUS_
9bda0 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20  MEMORY_USED);.  
9bdb0 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72  pArg = mem0.alar
9bdc0 6d 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61  mArg;.  mem0.ala
9bdd0 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a  rmCallback = 0;.
9bde0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
9bdf0 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78  leave(mem0.mutex
9be00 29 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 28 70  );.  xCallback(p
9be10 41 72 67 2c 20 6e 6f 77 55 73 65 64 2c 20 6e 42  Arg, nowUsed, nB
9be20 79 74 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  yte);.  sqlite3_
9be30 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
9be40 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e  .mutex);.  mem0.
9be50 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20  alarmCallback = 
9be60 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d  xCallback;.  mem
9be70 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72  0.alarmArg = pAr
9be80 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61  g;.}../*.** Do a
9be90 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
9bea0 6f 6e 20 77 69 74 68 20 73 74 61 74 69 73 74 69  on with statisti
9beb0 63 73 20 61 6e 64 20 61 6c 61 72 6d 73 2e 20 20  cs and alarms.  
9bec0 41 73 73 75 6d 65 20 74 68 65 0a 2a 2a 20 6c 6f  Assume the.** lo
9bed0 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65  ck is already he
9bee0 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ld..*/.static in
9bef0 74 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72  t mallocWithAlar
9bf00 6d 28 69 6e 74 20 6e 2c 20 76 6f 69 64 20 2a 2a  m(int n, void **
9bf10 70 70 29 7b 0a 20 20 69 6e 74 20 6e 46 75 6c 6c  pp){.  int nFull
9bf20 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61  ;.  void *p;.  a
9bf30 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
9bf40 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30 2e 6d  utex_held(mem0.m
9bf50 75 74 65 78 29 20 29 3b 0a 20 20 6e 46 75 6c 6c  utex) );.  nFull
9bf60 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
9bf70 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75  Config.m.xRoundu
9bf80 70 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  p(n);.  sqlite3S
9bf90 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f  tatusSet(SQLITE_
9bfa0 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49  STATUS_MALLOC_SI
9bfb0 5a 45 2c 20 6e 29 3b 0a 20 20 69 66 28 20 6d 65  ZE, n);.  if( me
9bfc0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b  m0.alarmCallback
9bfd0 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  !=0 ){.    int n
9bfe0 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53 74  Used = sqlite3St
9bff0 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45  atusValue(SQLITE
9c000 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
9c010 53 45 44 29 3b 0a 20 20 20 20 69 66 28 20 6e 55  SED);.    if( nU
9c020 73 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 6d 65 6d  sed+nFull >= mem
9c030 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64  0.alarmThreshold
9c040 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
9c050 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75  3MallocAlarm(nFu
9c060 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ll);.    }.  }. 
9c070 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62   p = sqlite3Glob
9c080 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c  alConfig.m.xMall
9c090 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 69 66 28  oc(nFull);.  if(
9c0a0 20 70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c   p==0 && mem0.al
9c0b0 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20  armCallback ){. 
9c0c0 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63     sqlite3Malloc
9c0d0 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20  Alarm(nFull);.  
9c0e0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f    p = sqlite3Glo
9c0f0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c  balConfig.m.xMal
9c100 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a  loc(nFull);.  }.
9c110 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e    if( p ){.    n
9c120 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61  Full = sqlite3Ma
9c130 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20  llocSize(p);.   
9c140 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
9c150 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
9c160 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e 46 75  MEMORY_USED, nFu
9c170 6c 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d  ll);.  }.  *pp =
9c180 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 6e 46 75   p;.  return nFu
9c190 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  ll;.}../*.** All
9c1a0 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20 20 54  ocate memory.  T
9c1b0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6c  his routine is l
9c1c0 69 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ike sqlite3_mall
9c1d0 6f 63 28 29 20 65 78 63 65 70 74 20 74 68 61 74  oc() except that
9c1e0 20 69 74 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74   it.** assumes t
9c1f0 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73  he memory subsys
9c200 74 65 6d 20 68 61 73 20 61 6c 72 65 61 64 79 20  tem has already 
9c210 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
9c220 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
9c230 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
9c240 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a  3Malloc(int n){.
9c250 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28    void *p;.  if(
9c260 20 6e 3c 3d 30 20 7c 7c 20 6e 3e 3d 30 78 37 66   n<=0 || n>=0x7f
9c270 66 66 66 66 30 30 20 29 7b 0a 20 20 20 20 2f 2a  ffff00 ){.    /*
9c280 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   A memory alloca
9c290 74 69 6f 6e 20 6f 66 20 61 20 6e 75 6d 62 65 72  tion of a number
9c2a0 20 6f 66 20 62 79 74 65 73 20 77 68 69 63 68 20   of bytes which 
9c2b0 69 73 20 6e 65 61 72 20 74 68 65 20 6d 61 78 69  is near the maxi
9c2c0 6d 75 6d 0a 20 20 20 20 2a 2a 20 73 69 67 6e 65  mum.    ** signe
9c2d0 64 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  d integer value 
9c2e0 6d 69 67 68 74 20 63 61 75 73 65 20 61 6e 20 69  might cause an i
9c2f0 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20  nteger overflow 
9c300 69 6e 73 69 64 65 20 6f 66 20 74 68 65 0a 20 20  inside of the.  
9c310 20 20 2a 2a 20 78 4d 61 6c 6c 6f 63 28 29 2e 20    ** xMalloc(). 
9c320 20 48 65 6e 63 65 20 77 65 20 6c 69 6d 69 74 20   Hence we limit 
9c330 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65  the maximum size
9c340 20 74 6f 20 30 78 37 66 66 66 66 66 30 30 2c 20   to 0x7fffff00, 
9c350 67 69 76 69 6e 67 0a 20 20 20 20 2a 2a 20 32 35  giving.    ** 25
9c360 35 20 62 79 74 65 73 20 6f 66 20 6f 76 65 72 68  5 bytes of overh
9c370 65 61 64 2e 20 20 53 51 4c 69 74 65 20 69 74 73  ead.  SQLite its
9c380 65 6c 66 20 77 69 6c 6c 20 6e 65 76 65 72 20 75  elf will never u
9c390 73 65 20 61 6e 79 74 68 69 6e 67 20 6e 65 61 72  se anything near
9c3a0 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 61 6d 6f  .    ** this amo
9c3b0 75 6e 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77  unt.  The only w
9c3c0 61 79 20 74 6f 20 72 65 61 63 68 20 74 68 65 20  ay to reach the 
9c3d0 6c 69 6d 69 74 20 69 73 20 77 69 74 68 20 73 71  limit is with sq
9c3e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a  lite3_malloc() *
9c3f0 2f 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d  /.    p = 0;.  }
9c400 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
9c410 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
9c420 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c  mstat ){.    sql
9c430 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
9c440 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9c450 20 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72    mallocWithAlar
9c460 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 73 71  m(n, &p);.    sq
9c470 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
9c480 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  e(mem0.mutex);. 
9c490 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20   }else{.    p = 
9c4a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9c4b0 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29  fig.m.xMalloc(n)
9c4c0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
9c4d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
9c4e0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d  version of the m
9c4f0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
9c500 20 69 73 20 66 6f 72 20 75 73 65 20 62 79 20 74   is for use by t
9c510 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 0a  he application..
9c520 2a 2a 20 46 69 72 73 74 20 6d 61 6b 65 20 73 75  ** First make su
9c530 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75  re the memory su
9c540 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69  bsystem is initi
9c550 61 6c 69 7a 65 64 2c 20 74 68 65 6e 20 64 6f 20  alized, then do 
9c560 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  the.** allocatio
9c570 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
9c580 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d   void *sqlite3_m
9c590 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 23 69  alloc(int n){.#i
9c5a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
9c5b0 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28  T_AUTOINIT.  if(
9c5c0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
9c5d0 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30  ize() ) return 0
9c5e0 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  ;.#endif.  retur
9c5f0 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  n sqlite3Malloc(
9c600 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  n);.}../*.** Eac
9c610 68 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c  h thread may onl
9c620 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20  y have a single 
9c630 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f  outstanding allo
9c640 63 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 78  cation from.** x
9c650 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 29 2e  ScratchMalloc().
9c660 20 20 57 65 20 76 65 72 69 66 79 20 74 68 69 73    We verify this
9c670 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 74   constraint in t
9c680 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64  he single-thread
9c690 65 64 0a 2a 2a 20 63 61 73 65 20 62 79 20 73 65  ed.** case by se
9c6a0 74 74 69 6e 67 20 73 63 72 61 74 63 68 41 6c 6c  tting scratchAll
9c6b0 6f 63 4f 75 74 20 74 6f 20 31 20 77 68 65 6e 20  ocOut to 1 when 
9c6c0 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  an allocation.**
9c6d0 20 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   is outstanding 
9c6e0 63 6c 65 61 72 69 6e 67 20 69 74 20 77 68 65 6e  clearing it when
9c6f0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
9c700 69 73 20 66 72 65 65 64 2e 0a 2a 2f 0a 23 69 66  is freed..*/.#if
9c710 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
9c720 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65  FE==0 && !define
9c730 64 28 4e 44 45 42 55 47 29 0a 73 74 61 74 69 63  d(NDEBUG).static
9c740 20 69 6e 74 20 73 63 72 61 74 63 68 41 6c 6c 6f   int scratchAllo
9c750 63 4f 75 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  cOut = 0;.#endif
9c760 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  .../*.** Allocat
9c770 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 69 73  e memory that is
9c780 20 74 6f 20 62 65 20 75 73 65 64 20 61 6e 64 20   to be used and 
9c790 72 65 6c 65 61 73 65 64 20 72 69 67 68 74 20 61  released right a
9c7a0 77 61 79 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75  way..** This rou
9c7b0 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20  tine is similar 
9c7c0 74 6f 20 61 6c 6c 6f 63 61 28 29 20 69 6e 20 74  to alloca() in t
9c7d0 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 69 6e  hat it is not in
9c7e0 74 65 6e 64 65 64 0a 2a 2a 20 66 6f 72 20 73 69  tended.** for si
9c7f0 74 75 61 74 69 6f 6e 73 20 77 68 65 72 65 20 74  tuations where t
9c800 68 65 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20  he memory might 
9c810 62 65 20 68 65 6c 64 20 6c 6f 6e 67 2d 74 65 72  be held long-ter
9c820 6d 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74  m.  This.** rout
9c830 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ine is intended 
9c840 74 6f 20 67 65 74 20 6d 65 6d 6f 72 79 20 74 6f  to get memory to
9c850 20 6f 6c 64 20 6c 61 72 67 65 20 74 72 61 6e 73   old large trans
9c860 69 65 6e 74 20 64 61 74 61 0a 2a 2a 20 73 74 72  ient data.** str
9c870 75 63 74 75 72 65 73 20 74 68 61 74 20 77 6f 75  uctures that wou
9c880 6c 64 20 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20  ld not normally 
9c890 66 69 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b  fit on the stack
9c8a0 20 6f 66 20 61 6e 0a 2a 2a 20 65 6d 62 65 64 64   of an.** embedd
9c8b0 65 64 20 70 72 6f 63 65 73 73 6f 72 2e 0a 2a 2f  ed processor..*/
9c8c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9c8d0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72  void *sqlite3Scr
9c8e0 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e  atchMalloc(int n
9c8f0 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20  ){.  void *p;.  
9c900 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 0a  assert( n>0 );..
9c910 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
9c920 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66  DSAFE==0 && !def
9c930 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 2f  ined(NDEBUG).  /
9c940 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 6e 6f  * Verify that no
9c950 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73   more than one s
9c960 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f  cratch allocatio
9c970 6e 20 70 65 72 20 74 68 72 65 61 64 0a 20 20 2a  n per thread.  *
9c980 2a 20 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67  * is outstanding
9c990 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28   at one time.  (
9c9a0 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 63 68 65  This is only che
9c9b0 63 6b 65 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a  cked in the.  **
9c9c0 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64   single-threaded
9c9d0 20 63 61 73 65 20 73 69 6e 63 65 20 63 68 65 63   case since chec
9c9e0 6b 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74  king in the mult
9c9f0 69 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 0a  i-threaded case.
9ca00 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75    ** would be mu
9ca10 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61  ch more complica
9ca20 74 65 64 2e 29 20 2a 2f 0a 20 20 61 73 73 65 72  ted.) */.  asser
9ca30 74 28 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f  t( scratchAllocO
9ca40 75 74 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a  ut==0 );.#endif.
9ca50 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
9ca60 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
9ca70 61 74 63 68 3c 6e 20 29 7b 0a 20 20 20 20 67 6f  atch<n ){.    go
9ca80 74 6f 20 73 63 72 61 74 63 68 5f 6f 76 65 72 66  to scratch_overf
9ca90 6c 6f 77 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a  low;.  }else{  .
9caa0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9cab0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74  x_enter(mem0.mut
9cac0 65 78 29 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d  ex);.    if( mem
9cad0 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 3d 3d  0.nScratchFree==
9cae0 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
9caf0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
9cb00 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
9cb10 20 20 67 6f 74 6f 20 73 63 72 61 74 63 68 5f 6f    goto scratch_o
9cb20 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 7d 65 6c  verflow;.    }el
9cb30 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  se{.      int i;
9cb40 0a 20 20 20 20 20 20 69 20 3d 20 6d 65 6d 30 2e  .      i = mem0.
9cb50 61 53 63 72 61 74 63 68 46 72 65 65 5b 2d 2d 6d  aScratchFree[--m
9cb60 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65  em0.nScratchFree
9cb70 5d 3b 0a 20 20 20 20 20 20 69 20 2a 3d 20 73 71  ];.      i *= sq
9cb80 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9cb90 67 2e 73 7a 53 63 72 61 74 63 68 3b 0a 20 20 20  g.szScratch;.   
9cba0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9cbb0 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
9cbc0 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20  S_SCRATCH_USED, 
9cbd0 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
9cbe0 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54  3StatusSet(SQLIT
9cbf0 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
9cc00 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 20  _SIZE, n);.     
9cc10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
9cc20 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29  eave(mem0.mutex)
9cc30 3b 0a 20 20 20 20 20 20 70 20 3d 20 28 76 6f 69  ;.      p = (voi
9cc40 64 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69  d*)&((char*)sqli
9cc50 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9cc60 70 53 63 72 61 74 63 68 29 5b 69 5d 3b 0a 20 20  pScratch)[i];.  
9cc70 20 20 20 20 61 73 73 65 72 74 28 20 20 28 28 28      assert(  (((
9cc80 75 38 2a 29 70 20 2d 20 28 75 38 2a 29 30 29 20  u8*)p - (u8*)0) 
9cc90 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  & 7)==0 );.    }
9cca0 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f  .  }.#if SQLITE_
9ccb0 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26  THREADSAFE==0 &&
9ccc0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
9ccd0 29 0a 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63  ).  scratchAlloc
9cce0 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65 6e 64  Out = p!=0;.#end
9ccf0 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a  if..  return p;.
9cd00 0a 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f  .scratch_overflo
9cd10 77 3a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  w:.  if( sqlite3
9cd20 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
9cd30 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c  mstat ){.    sql
9cd40 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
9cd50 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9cd60 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53    sqlite3StatusS
9cd70 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  et(SQLITE_STATUS
9cd80 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e  _SCRATCH_SIZE, n
9cd90 29 3b 0a 20 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f  );.    n = mallo
9cda0 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70  cWithAlarm(n, &p
9cdb0 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 73  );.    if( p ) s
9cdc0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
9cdd0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
9cde0 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20  RATCH_OVERFLOW, 
9cdf0 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  n);.    sqlite3_
9ce00 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9ce10 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65  .mutex);.  }else
9ce20 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  {.    p = sqlite
9ce30 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9ce40 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a  xMalloc(n);.  }.
9ce50 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
9ce60 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66  DSAFE==0 && !def
9ce70 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73  ined(NDEBUG).  s
9ce80 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d  cratchAllocOut =
9ce90 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 20 20   p!=0;.#endif.  
9cea0 72 65 74 75 72 6e 20 70 3b 20 20 20 20 0a 7d 0a  return p;    .}.
9ceb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
9cec0 6f 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74  oid sqlite3Scrat
9ced0 63 68 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b  chFree(void *p){
9cee0 0a 20 20 69 66 28 20 70 20 29 7b 0a 0a 23 69 66  .  if( p ){..#if
9cef0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
9cf00 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65  FE==0 && !define
9cf10 64 28 4e 44 45 42 55 47 29 0a 20 20 20 20 2f 2a  d(NDEBUG).    /*
9cf20 20 56 65 72 69 66 79 20 74 68 61 74 20 6e 6f 20   Verify that no 
9cf30 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63  more than one sc
9cf40 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e  ratch allocation
9cf50 20 70 65 72 20 74 68 72 65 61 64 0a 20 20 20 20   per thread.    
9cf60 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e 64 69 6e  ** is outstandin
9cf70 67 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20  g at one time.  
9cf80 28 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 63 68  (This is only ch
9cf90 65 63 6b 65 64 20 69 6e 20 74 68 65 0a 20 20 20  ecked in the.   
9cfa0 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 68 72 65 61   ** single-threa
9cfb0 64 65 64 20 63 61 73 65 20 73 69 6e 63 65 20 63  ded case since c
9cfc0 68 65 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 6d  hecking in the m
9cfd0 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 63 61  ulti-threaded ca
9cfe0 73 65 0a 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20  se.    ** would 
9cff0 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d  be much more com
9d000 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a 20 20  plicated.) */.  
9d010 20 20 61 73 73 65 72 74 28 20 73 63 72 61 74 63    assert( scratc
9d020 68 41 6c 6c 6f 63 4f 75 74 3d 3d 31 20 29 3b 0a  hAllocOut==1 );.
9d030 20 20 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63      scratchAlloc
9d040 4f 75 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  Out = 0;.#endif.
9d050 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
9d060 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9d070 72 61 74 63 68 3d 3d 30 0a 20 20 20 20 20 20 20  ratch==0.       
9d080 20 20 20 20 7c 7c 20 70 3c 73 71 6c 69 74 65 33      || p<sqlite3
9d090 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9d0a0 72 61 74 63 68 0a 20 20 20 20 20 20 20 20 20 20  ratch.          
9d0b0 20 7c 7c 20 70 3e 3d 28 76 6f 69 64 2a 29 6d 65   || p>=(void*)me
9d0c0 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 20  m0.aScratchFree 
9d0d0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
9d0e0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9d0f0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20  .bMemstat ){.   
9d100 20 20 20 20 20 69 6e 74 20 69 53 69 7a 65 20 3d       int iSize =
9d110 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69   sqlite3MallocSi
9d120 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 73  ze(p);.        s
9d130 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
9d140 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  er(mem0.mutex);.
9d150 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
9d160 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
9d170 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f  STATUS_SCRATCH_O
9d180 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29  VERFLOW, -iSize)
9d190 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
9d1a0 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
9d1b0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
9d1c0 55 53 45 44 2c 20 2d 69 53 69 7a 65 29 3b 0a 20  USED, -iSize);. 
9d1d0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c         sqlite3Gl
9d1e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72  obalConfig.m.xFr
9d1f0 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 73  ee(p);.        s
9d200 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
9d210 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  ve(mem0.mutex);.
9d220 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
9d230 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
9d240 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65  alConfig.m.xFree
9d250 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  (p);.      }.   
9d260 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e   }else{.      in
9d270 74 20 69 3b 0a 20 20 20 20 20 20 69 20 3d 20 28  t i;.      i = (
9d280 69 6e 74 29 28 28 75 38 2a 29 70 20 2d 20 28 75  int)((u8*)p - (u
9d290 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  8*)sqlite3Global
9d2a0 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29  Config.pScratch)
9d2b0 3b 0a 20 20 20 20 20 20 69 20 2f 3d 20 73 71 6c  ;.      i /= sql
9d2c0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9d2d0 2e 73 7a 53 63 72 61 74 63 68 3b 0a 20 20 20 20  .szScratch;.    
9d2e0 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
9d2f0 26 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61  & i<sqlite3Globa
9d300 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68  lConfig.nScratch
9d310 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
9d320 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
9d330 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
9d340 20 61 73 73 65 72 74 28 20 6d 65 6d 30 2e 6e 53   assert( mem0.nS
9d350 63 72 61 74 63 68 46 72 65 65 3c 28 75 33 32 29  cratchFree<(u32)
9d360 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9d370 66 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a  fig.nScratch );.
9d380 20 20 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 61        mem0.aScra
9d390 74 63 68 46 72 65 65 5b 6d 65 6d 30 2e 6e 53 63  tchFree[mem0.nSc
9d3a0 72 61 74 63 68 46 72 65 65 2b 2b 5d 20 3d 20 69  ratchFree++] = i
9d3b0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ;.      sqlite3S
9d3c0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
9d3d0 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55  STATUS_SCRATCH_U
9d3e0 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  SED, -1);.      
9d3f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
9d400 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ave(mem0.mutex);
9d410 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
9d420 0a 2a 2a 20 54 52 55 45 20 69 66 20 70 20 69 73  .** TRUE if p is
9d430 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d   a lookaside mem
9d440 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
9d450 72 6f 6d 20 64 62 0a 2a 2f 0a 23 69 66 6e 64 65  rom db.*/.#ifnde
9d460 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
9d470 4f 4b 41 53 49 44 45 0a 73 74 61 74 69 63 20 69  OKASIDE.static i
9d480 6e 74 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 73  nt isLookaside(s
9d490 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
9d4a0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 64   *p){.  return d
9d4b0 62 20 26 26 20 70 20 26 26 20 70 3e 3d 64 62 2d  b && p && p>=db-
9d4c0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72  >lookaside.pStar
9d4d0 74 20 26 26 20 70 3c 64 62 2d 3e 6c 6f 6f 6b 61  t && p<db->looka
9d4e0 73 69 64 65 2e 70 45 6e 64 3b 0a 7d 0a 23 65 6c  side.pEnd;.}.#el
9d4f0 73 65 0a 23 64 65 66 69 6e 65 20 69 73 4c 6f 6f  se.#define isLoo
9d500 6b 61 73 69 64 65 28 41 2c 42 29 20 30 0a 23 65  kaside(A,B) 0.#e
9d510 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
9d520 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  rn the size of a
9d530 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
9d540 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62  on previously ob
9d550 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73  tained from.** s
9d560 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 6f  qlite3Malloc() o
9d570 72 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  r sqlite3_malloc
9d580 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
9d590 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
9d5a0 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64  3MallocSize(void
9d5b0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73   *p){.  return s
9d5c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9d5d0 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 7d  ig.m.xSize(p);.}
9d5e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9d5f0 69 6e 74 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  int sqlite3DbMal
9d600 6c 6f 63 53 69 7a 65 28 73 71 6c 69 74 65 33 20  locSize(sqlite3 
9d610 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20  *db, void *p){. 
9d620 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c   assert( db==0 |
9d630 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
9d640 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
9d650 29 3b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61  );.  if( isLooka
9d660 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20  side(db, p) ){. 
9d670 20 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6c 6f     return db->lo
9d680 6f 6b 61 73 69 64 65 2e 73 7a 3b 0a 20 20 7d 65  okaside.sz;.  }e
9d690 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
9d6a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9d6b0 66 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a  fig.m.xSize(p);.
9d6c0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65    }.}../*.** Fre
9d6d0 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 6f 75  e memory previou
9d6e0 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  sly obtained fro
9d6f0 6d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  m sqlite3Malloc(
9d700 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
9d710 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72   void sqlite3_fr
9d720 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69  ee(void *p){.  i
9d730 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
9d740 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  ;.  if( sqlite3G
9d750 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
9d760 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  stat ){.    sqli
9d770 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
9d780 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem0.mutex);.   
9d790 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
9d7a0 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
9d7b0 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 73 71  MEMORY_USED, -sq
9d7c0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
9d7d0 70 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  p));.    sqlite3
9d7e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9d7f0 46 72 65 65 28 70 29 3b 0a 20 20 20 20 73 71 6c  Free(p);.    sql
9d800 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
9d810 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9d820 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
9d830 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9d840 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d  .xFree(p);.  }.}
9d850 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d  ../*.** Free mem
9d860 6f 72 79 20 74 68 61 74 20 6d 69 67 68 74 20 62  ory that might b
9d870 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
9d880 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  h a particular d
9d890 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65  atabase.** conne
9d8a0 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
9d8b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
9d8c0 6c 69 74 65 33 44 62 46 72 65 65 28 73 71 6c 69  lite3DbFree(sqli
9d8d0 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70  te3 *db, void *p
9d8e0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d  ){.  assert( db=
9d8f0 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
9d900 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
9d910 65 78 29 20 29 3b 0a 20 20 69 66 28 20 69 73 4c  ex) );.  if( isL
9d920 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20  ookaside(db, p) 
9d930 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65  ){.    Lookaside
9d940 53 6c 6f 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f  Slot *pBuf = (Lo
9d950 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0a  okasideSlot*)p;.
9d960 20 20 20 20 70 42 75 66 2d 3e 70 4e 65 78 74 20      pBuf->pNext 
9d970 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  = db->lookaside.
9d980 70 46 72 65 65 3b 0a 20 20 20 20 64 62 2d 3e 6c  pFree;.    db->l
9d990 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d  ookaside.pFree =
9d9a0 20 70 42 75 66 3b 0a 20 20 20 20 64 62 2d 3e 6c   pBuf;.    db->l
9d9b0 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2d 2d 3b  ookaside.nOut--;
9d9c0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
9d9d0 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20  lite3_free(p);. 
9d9e0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
9d9f0 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  ge the size of a
9da00 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72  n existing memor
9da10 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a  y allocation.*/.
9da20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
9da30 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c  oid *sqlite3Real
9da40 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20  loc(void *pOld, 
9da50 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69  int nBytes){.  i
9da60 6e 74 20 6e 4f 6c 64 2c 20 6e 4e 65 77 3b 0a 20  nt nOld, nNew;. 
9da70 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 69   void *pNew;.  i
9da80 66 28 20 70 4f 6c 64 3d 3d 30 20 29 7b 0a 20 20  f( pOld==0 ){.  
9da90 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
9daa0 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a  Malloc(nBytes);.
9dab0 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73    }.  if( nBytes
9dac0 3c 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  <=0 ){.    sqlit
9dad0 65 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b 0a 20  e3_free(pOld);. 
9dae0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
9daf0 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3e 3d 30  .  if( nBytes>=0
9db00 78 37 66 66 66 66 66 30 30 20 29 7b 0a 20 20 20  x7fffff00 ){.   
9db10 20 2f 2a 20 54 68 65 20 30 78 37 66 66 66 66 30   /* The 0x7ffff0
9db20 30 20 6c 69 6d 69 74 20 74 65 72 6d 20 69 73 20  0 limit term is 
9db30 65 78 70 6c 61 69 6e 65 64 20 69 6e 20 63 6f 6d  explained in com
9db40 6d 65 6e 74 73 20 6f 6e 20 73 71 6c 69 74 65 33  ments on sqlite3
9db50 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20  Malloc() */.    
9db60 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
9db70 6e 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 4d 61  nOld = sqlite3Ma
9db80 6c 6c 6f 63 53 69 7a 65 28 70 4f 6c 64 29 3b 0a  llocSize(pOld);.
9db90 20 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    nNew = sqlite3
9dba0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9dbb0 52 6f 75 6e 64 75 70 28 6e 42 79 74 65 73 29 3b  Roundup(nBytes);
9dbc0 0a 20 20 69 66 28 20 6e 4f 6c 64 3d 3d 6e 4e 65  .  if( nOld==nNe
9dbd0 77 20 29 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20  w ){.    pNew = 
9dbe0 70 4f 6c 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66  pOld;.  }else if
9dbf0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9dc00 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29  onfig.bMemstat )
9dc10 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
9dc20 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
9dc30 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74  utex);.    sqlit
9dc40 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49  e3StatusSet(SQLI
9dc50 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43  TE_STATUS_MALLOC
9dc60 5f 53 49 5a 45 2c 20 6e 42 79 74 65 73 29 3b 0a  _SIZE, nBytes);.
9dc70 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
9dc80 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54  tatusValue(SQLIT
9dc90 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
9dca0 55 53 45 44 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20  USED)+nNew-nOld 
9dcb0 3e 3d 20 0a 20 20 20 20 20 20 20 20 20 20 6d 65  >= .          me
9dcc0 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c  m0.alarmThreshol
9dcd0 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  d ){.      sqlit
9dce0 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 4e  e3MallocAlarm(nN
9dcf0 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a  ew-nOld);.    }.
9dd00 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
9dd10 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9dd20 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20  .xRealloc(pOld, 
9dd30 6e 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20 70  nNew);.    if( p
9dd40 4e 65 77 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61  New==0 && mem0.a
9dd50 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a  larmCallback ){.
9dd60 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c        sqlite3Mal
9dd70 6c 6f 63 41 6c 61 72 6d 28 6e 42 79 74 65 73 29  locAlarm(nBytes)
9dd80 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73  ;.      pNew = s
9dd90 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9dda0 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f  ig.m.xRealloc(pO
9ddb0 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 7d  ld, nNew);.    }
9ddc0 0a 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b  .    if( pNew ){
9ddd0 0a 20 20 20 20 20 20 6e 4e 65 77 20 3d 20 73 71  .      nNew = sq
9dde0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
9ddf0 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 73 71 6c  pNew);.      sql
9de00 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51  ite3StatusAdd(SQ
9de10 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f  LITE_STATUS_MEMO
9de20 52 59 5f 55 53 45 44 2c 20 6e 4e 65 77 2d 6e 4f  RY_USED, nNew-nO
9de30 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ld);.    }.    s
9de40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
9de50 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  ve(mem0.mutex);.
9de60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65    }else{.    pNe
9de70 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  w = sqlite3Globa
9de80 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c  lConfig.m.xReall
9de90 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a  oc(pOld, nNew);.
9dea0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
9deb0 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  w;.}../*.** The 
9dec0 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65  public interface
9ded0 20 74 6f 20 73 71 6c 69 74 65 33 52 65 61 6c 6c   to sqlite3Reall
9dee0 6f 63 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74  oc.  Make sure t
9def0 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a  hat the memory.*
9df00 2a 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69  * subsystem is i
9df10 6e 69 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72  nitialized prior
9df20 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c   to invoking sql
9df30 69 74 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53  iteRealloc..*/.S
9df40 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
9df50 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
9df60 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20  void *pOld, int 
9df70 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  n){.#ifndef SQLI
9df80 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
9df90 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
9dfa0 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65  nitialize() ) re
9dfb0 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20  turn 0;.#endif. 
9dfc0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52   return sqlite3R
9dfd0 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b  ealloc(pOld, n);
9dfe0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  .}.../*.** Alloc
9dff0 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d  ate and zero mem
9e000 6f 72 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f  ory..*/ .SQLITE_
9e010 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
9e020 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28  lite3MallocZero(
9e030 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a  int n){.  void *
9e040 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  p = sqlite3Mallo
9e050 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  c(n);.  if( p ){
9e060 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  .    memset(p, 0
9e070 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , n);.  }.  retu
9e080 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn p;.}../*.** A
9e090 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f  llocate and zero
9e0a0 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65   memory.  If the
9e0b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
9e0c0 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d  s, make.** the m
9e0d0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
9e0e0 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69   in the connecti
9e0f0 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53  on pointer..*/.S
9e100 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
9e110 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c  id *sqlite3DbMal
9e120 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20  locZero(sqlite3 
9e130 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76  *db, int n){.  v
9e140 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  oid *p = sqlite3
9e150 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
9e160 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  n);.  if( p ){. 
9e170 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20     memset(p, 0, 
9e180 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  n);.  }.  return
9e190 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c   p;.}../*.** All
9e1a0 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d  ocate and zero m
9e1b0 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61  emory.  If the a
9e1c0 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
9e1d0 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c   make.** the mal
9e1e0 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69  locFailed flag i
9e1f0 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  n the connection
9e200 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
9e210 49 66 20 64 62 21 3d 30 20 61 6e 64 20 64 62 2d  If db!=0 and db-
9e220 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73  >mallocFailed is
9e230 20 74 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e   true (indicatin
9e240 67 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63  g a prior malloc
9e250 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 6e 20 74  .** failure on t
9e260 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
9e270 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65   connection) the
9e280 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20  n always return 
9e290 30 2e 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20  0..** Hence for 
9e2a0 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74  a particular dat
9e2b0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
9e2c0 2c 20 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74  , once malloc st
9e2d0 61 72 74 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c  arts.** failing,
9e2e0 20 69 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73   it fails consis
9e2f0 74 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c  tently until mal
9e300 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 72 65 73  locFailed is res
9e310 65 74 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  et..** This is a
9e320 6e 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75  n important assu
9e330 6d 70 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61  mption.  There a
9e340 72 65 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69  re many places i
9e350 6e 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68  n the.** code th
9e360 61 74 20 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b  at do things lik
9e370 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
9e380 20 20 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28        int *a = (
9e390 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  int*)sqlite3DbMa
9e3a0 6c 6c 6f 63 52 61 77 28 64 62 2c 20 31 30 30 29  llocRaw(db, 100)
9e3b0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74  ;.**         int
9e3c0 20 2a 62 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69   *b = (int*)sqli
9e3d0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
9e3e0 62 2c 20 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20  b, 200);.**     
9e3f0 20 20 20 20 69 66 28 20 62 20 29 20 61 5b 31 30      if( b ) a[10
9e400 5d 20 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20  ] = 9;.**.** In 
9e410 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20  other words, if 
9e420 61 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c  a subsequent mal
9e430 6c 6f 63 20 28 65 78 3a 20 22 62 22 29 20 77 6f  loc (ex: "b") wo
9e440 72 6b 65 64 2c 20 69 74 20 69 73 20 61 73 73 75  rked, it is assu
9e450 6d 65 64 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20  med.** that all 
9e460 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65  prior mallocs (e
9e470 78 3a 20 22 61 22 29 20 77 6f 72 6b 65 64 20 74  x: "a") worked t
9e480 6f 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  oo..*/.SQLITE_PR
9e490 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
9e4a0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73  te3DbMallocRaw(s
9e4b0 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
9e4c0 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20  n){.  void *p;. 
9e4d0 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c   assert( db==0 |
9e4e0 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
9e4f0 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
9e500 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
9e510 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45  E_OMIT_LOOKASIDE
9e520 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20 20 20  .  if( db ){.   
9e530 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a   LookasideSlot *
9e540 70 42 75 66 3b 0a 20 20 20 20 69 66 28 20 64 62  pBuf;.    if( db
9e550 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
9e560 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
9e570 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
9e580 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
9e590 6e 61 62 6c 65 64 20 26 26 20 6e 3c 3d 64 62 2d  nabled && n<=db-
9e5a0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 0a 20 20  >lookaside.sz.  
9e5b0 20 20 20 20 20 20 20 26 26 20 28 70 42 75 66 20         && (pBuf 
9e5c0 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  = db->lookaside.
9e5d0 70 46 72 65 65 29 21 3d 30 20 29 7b 0a 20 20 20  pFree)!=0 ){.   
9e5e0 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
9e5f0 2e 70 46 72 65 65 20 3d 20 70 42 75 66 2d 3e 70  .pFree = pBuf->p
9e600 4e 65 78 74 3b 0a 20 20 20 20 20 20 64 62 2d 3e  Next;.      db->
9e610 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2b 2b  lookaside.nOut++
9e620 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e  ;.      if( db->
9e630 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3e 64  lookaside.nOut>d
9e640 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f  b->lookaside.mxO
9e650 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62  ut ){.        db
9e660 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75  ->lookaside.mxOu
9e670 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  t = db->lookasid
9e680 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  e.nOut;.      }.
9e690 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f        return (vo
9e6a0 69 64 2a 29 70 42 75 66 3b 0a 20 20 20 20 7d 0a  id*)pBuf;.    }.
9e6b0 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20    }.#else.  if( 
9e6c0 64 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63  db && db->malloc
9e6d0 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
9e6e0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 6e 64  turn 0;.  }.#end
9e6f0 69 66 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  if.  p = sqlite3
9e700 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28  Malloc(n);.  if(
9e710 20 21 70 20 26 26 20 64 62 20 29 7b 0a 20 20 20   !p && db ){.   
9e720 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
9e730 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  d = 1;.  }.  ret
9e740 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
9e750 52 65 73 69 7a 65 20 74 68 65 20 62 6c 6f 63 6b  Resize the block
9e760 20 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74   of memory point
9e770 65 64 20 74 6f 20 62 79 20 70 20 74 6f 20 6e 20  ed to by p to n 
9e780 62 79 74 65 73 2e 20 49 66 20 74 68 65 0a 2a 2a  bytes. If the.**
9e790 20 72 65 73 69 7a 65 20 66 61 69 6c 73 2c 20 73   resize fails, s
9e7a0 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69  et the mallocFai
9e7b0 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20  led flag in the 
9e7c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63  connection objec
9e7d0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
9e7e0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
9e7f0 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69  e3DbRealloc(sqli
9e800 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70  te3 *db, void *p
9e810 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64  , int n){.  void
9e820 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 61 73   *pNew = 0;.  as
9e830 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20  sert( db!=0 );. 
9e840 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
9e850 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
9e860 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
9e870 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
9e880 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
9e890 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
9e8a0 75 72 6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  urn sqlite3DbMal
9e8b0 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20  locRaw(db, n);. 
9e8c0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4c     }.    if( isL
9e8d0 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20  ookaside(db, p) 
9e8e0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 3c 3d  ){.      if( n<=
9e8f0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a  db->lookaside.sz
9e900 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
9e910 72 6e 20 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn p;.      }.  
9e920 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
9e930 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
9e940 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , n);.      if( 
9e950 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  pNew ){.        
9e960 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20  memcpy(pNew, p, 
9e970 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a  db->lookaside.sz
9e980 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
9e990 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b  e3DbFree(db, p);
9e9a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
9e9b0 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d  se{.      pNew =
9e9c0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
9e9d0 28 70 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66  (p, n);.      if
9e9e0 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  ( !pNew ){.     
9e9f0 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
9ea00 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  led = 1;.      }
9ea10 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
9ea20 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
9ea30 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65  ** Attempt to re
9ea40 61 6c 6c 6f 63 61 74 65 20 70 2e 20 20 49 66 20  allocate p.  If 
9ea50 74 68 65 20 72 65 61 6c 6c 6f 63 61 74 69 6f 6e  the reallocation
9ea60 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 66 72 65   fails, then fre
9ea70 65 20 70 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74  e p.** and set t
9ea80 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
9ea90 66 6c 61 67 20 69 6e 20 74 68 65 20 64 61 74 61  flag in the data
9eaa0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
9eab0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9eac0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
9ead0 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
9eae0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69  sqlite3 *db, voi
9eaf0 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  d *p, int n){.  
9eb00 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 70 4e  void *pNew;.  pN
9eb10 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65  ew = sqlite3DbRe
9eb20 61 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e 29 3b  alloc(db, p, n);
9eb30 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a  .  if( !pNew ){.
9eb40 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
9eb50 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 20 20  e(db, p);.  }.  
9eb60 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a  return pNew;.}..
9eb70 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70  /*.** Make a cop
9eb80 79 20 6f 66 20 61 20 73 74 72 69 6e 67 20 69 6e  y of a string in
9eb90 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
9eba0 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c   from sqliteMall
9ebb0 6f 63 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20  oc(). These .** 
9ebc0 66 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73  functions call s
9ebd0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28  qlite3MallocRaw(
9ebe0 29 20 64 69 72 65 63 74 6c 79 20 69 6e 73 74 65  ) directly inste
9ebf0 61 64 20 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c  ad of sqliteMall
9ec00 6f 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73  oc(). This.** is
9ec10 20 62 65 63 61 75 73 65 20 77 68 65 6e 20 6d 65   because when me
9ec20 6d 6f 72 79 20 64 65 62 75 67 67 69 6e 67 20 69  mory debugging i
9ec30 73 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65  s turned on, the
9ec40 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73  se two functions
9ec50 20 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20   are .** called 
9ec60 76 69 61 20 6d 61 63 72 6f 73 20 74 68 61 74 20  via macros that 
9ec70 72 65 63 6f 72 64 20 74 68 65 20 63 75 72 72 65  record the curre
9ec80 6e 74 20 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65  nt file and line
9ec90 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a   number in the.*
9eca0 2a 20 54 68 72 65 61 64 44 61 74 61 20 73 74 72  * ThreadData str
9ecb0 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ucture..*/.SQLIT
9ecc0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
9ecd0 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
9ece0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
9ecf0 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63  st char *z){.  c
9ed00 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a  har *zNew;.  siz
9ed10 65 5f 74 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d  e_t n;.  if( z==
9ed20 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
9ed30 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c  0;.  }.  n = sql
9ed40 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20  ite3Strlen30(z) 
9ed50 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28  + 1;.  assert( (
9ed60 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e  n&0x7fffffff)==n
9ed70 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c   );.  zNew = sql
9ed80 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
9ed90 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69  db, (int)n);.  i
9eda0 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d  f( zNew ){.    m
9edb0 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e  emcpy(zNew, z, n
9edc0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
9edd0 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  zNew;.}.SQLITE_P
9ede0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
9edf0 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 73 71  ite3DbStrNDup(sq
9ee00 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
9ee10 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
9ee20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a  {.  char *zNew;.
9ee30 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20    if( z==0 ){.  
9ee40 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
9ee50 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 37    assert( (n&0x7
9ee60 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20  fffffff)==n );. 
9ee70 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   zNew = sqlite3D
9ee80 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e  bMallocRaw(db, n
9ee90 2b 31 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20  +1);.  if( zNew 
9eea0 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e  ){.    memcpy(zN
9eeb0 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a  ew, z, n);.    z
9eec0 4e 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a  New[n] = 0;.  }.
9eed0 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d    return zNew;.}
9eee0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
9eef0 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65   string from the
9ef00 20 7a 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e   zFromat argumen
9ef10 74 20 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73  t and the va_lis
9ef20 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a  t that follows..
9ef30 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 73 74 72  ** Store the str
9ef40 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62  ing in memory ob
9ef50 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
9ef60 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d  teMalloc() and m
9ef70 61 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74  ake *pz.** point
9ef80 20 74 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e   to that string.
9ef90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9efa0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
9efb0 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a  etString(char **
9efc0 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  pz, sqlite3 *db,
9efd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
9efe0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
9eff0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
9f000 20 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74   *z;..  va_start
9f010 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
9f020 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72   z = sqlite3VMPr
9f030 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74  intf(db, zFormat
9f040 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
9f050 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  ap);.  sqlite3Db
9f060 46 72 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20  Free(db, *pz);. 
9f070 20 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a   *pz = z;.}.../*
9f080 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
9f090 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  n must be called
9f0a0 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20   before exiting 
9f0b0 61 6e 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  any API function
9f0c0 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72   (i.e. .** retur
9f0d0 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20  ning control to 
9f0e0 74 68 65 20 75 73 65 72 29 20 74 68 61 74 20 68  the user) that h
9f0f0 61 73 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65  as called sqlite
9f100 33 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73  3_malloc or.** s
9f110 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a  qlite3_realloc..
9f120 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  **.** The return
9f130 65 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d  ed value is norm
9f140 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74  ally a copy of t
9f150 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
9f160 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75  nt to this.** fu
9f170 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c  nction. However,
9f180 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66   if a malloc() f
9f190 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72  ailure has occur
9f1a0 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 70 72  red since the pr
9f1b0 65 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61  evious.** invoca
9f1c0 74 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  tion SQLITE_NOME
9f1d0 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  M is returned in
9f1e0 73 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  stead. .**.** If
9f1f0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
9f200 65 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20  ent, db, is not 
9f210 4e 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f  NULL and a mallo
9f220 63 28 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63  c() error has oc
9f230 63 75 72 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20  curred,.** then 
9f240 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65  the connection e
9f250 72 72 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76  rror-code (the v
9f260 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
9f270 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
9f280 28 29 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  ()).** is set to
9f290 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a   SQLITE_NOMEM..*
9f2a0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9f2b0 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45   int sqlite3ApiE
9f2c0 78 69 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c  xit(sqlite3* db,
9f2d0 20 69 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49   int rc){.  /* I
9f2e0 66 20 74 68 65 20 64 62 20 68 61 6e 64 6c 65 20  f the db handle 
9f2f0 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
9f300 6e 20 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 74  n we must hold t
9f310 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  he connection ha
9f320 6e 64 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20  ndle.  ** mutex 
9f330 68 65 72 65 2e 20 4f 74 68 65 72 77 69 73 65 20  here. Otherwise 
9f340 74 68 65 20 72 65 61 64 20 28 61 6e 64 20 70 6f  the read (and po
9f350 73 73 69 62 6c 65 20 77 72 69 74 65 29 20 6f 66  ssible write) of
9f360 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
9f370 64 20 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 66  d .  ** is unsaf
9f380 65 2c 20 61 73 20 69 73 20 74 68 65 20 63 61 6c  e, as is the cal
9f390 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 72 72 6f  l to sqlite3Erro
9f3a0 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  r()..  */.  asse
9f3b0 72 74 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74  rt( !db || sqlit
9f3c0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
9f3d0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
9f3e0 28 20 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c  ( db && (db->mal
9f3f0 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d  locFailed || rc=
9f400 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f  =SQLITE_IOERR_NO
9f410 4d 45 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  MEM) ){.    sqli
9f420 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
9f430 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20  ITE_NOMEM, 0);. 
9f440 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
9f450 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20  led = 0;.    rc 
9f460 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
9f470 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20    }.  return rc 
9f480 26 20 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d  & (db ? db->errM
9f490 61 73 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a  ask : 0xff);.}..
9f4a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
9f4b0 45 6e 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20  End of malloc.c 
9f4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
9f4f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
9f500 42 65 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74  Begin file print
9f510 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f.c ************
9f520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
9f540 2f 2a 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74  /*.** The "print
9f550 66 22 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c  f" code that fol
9f560 6c 6f 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20  lows dates from 
9f570 74 68 65 20 31 39 38 30 27 73 2e 20 20 49 74 20  the 1980's.  It 
9f580 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62  is in.** the pub
9f590 6c 69 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65  lic domain.  The
9f5a0 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e   original commen
9f5b0 74 73 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20  ts are included 
9f5c0 68 65 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70  here for.** comp
9f5d0 6c 65 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20  leteness.  They 
9f5e0 61 72 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d  are very out-of-
9f5f0 64 61 74 65 20 62 75 74 20 6d 69 67 68 74 20 62  date but might b
9f600 65 20 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61  e useful as.** a
9f610 6e 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66  n historical ref
9f620 65 72 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66  erence.  Most of
9f630 20 74 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e   the "enhancemen
9f640 74 73 22 20 68 61 76 65 20 62 65 65 6e 20 62 61  ts" have been ba
9f650 63 6b 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74  cked.** out so t
9f660 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  hat the function
9f670 61 6c 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65  ality is now the
9f680 20 73 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72   same as standar
9f690 64 20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a  d printf()..**.*
9f6a0 2a 20 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 2c  * $Id: printf.c,
9f6b0 76 20 31 2e 31 30 34 20 32 30 30 39 2f 30 36 2f  v 1.104 2009/06/
9f6c0 30 33 20 30 31 3a 32 34 3a 35 34 20 64 72 68 20  03 01:24:54 drh 
9f6d0 45 78 70 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  Exp $.**.*******
9f6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f720 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ***.**.** The fo
9f730 6c 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20  llowing modules 
9f740 69 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72  is an enhanced r
9f750 65 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74  eplacement for t
9f760 68 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72  he "printf" subr
9f770 6f 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64  outines.** found
9f780 20 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64   in the standard
9f790 20 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65   C library.  The
9f7a0 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e   following enhan
9f7b0 63 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73  cements are.** s
9f7c0 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20  upported:.**.** 
9f7d0 20 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e       +  Addition
9f7e0 61 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54  al functions.  T
9f7f0 68 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20  he standard set 
9f800 6f 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63  of "printf" func
9f810 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20  tions.**        
9f820 20 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66   includes printf
9f830 2c 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e  , fprintf, sprin
9f840 74 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70  tf, vprintf, vfp
9f850 72 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20  rintf, and.**   
9f860 20 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20        vsprintf. 
9f870 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64   This module add
9f880 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  s the following:
9f890 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
9f8a0 20 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20   *  snprintf -- 
9f8b0 57 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e  Works like sprin
9f8c0 74 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65  tf, but has an e
9f8d0 78 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  xtra argument.**
9f8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f8f0 20 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20            which 
9f900 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  is the size of t
9f910 68 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65  he buffer writte
9f920 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  n to..**.**     
9f930 20 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66        *  mprintf
9f940 20 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20   --  Similar to 
9f950 73 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73  sprintf.  Writes
9f960 20 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72   output to memor
9f970 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  y.**            
9f980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62                ob
9f990 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
9f9a0 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  oc..**.**       
9f9b0 20 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d      *  xprintf -
9f9c0 2d 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74  -  Calls a funct
9f9d0 69 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f  ion to dispose o
9f9e0 66 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20  f output..**.** 
9f9f0 20 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72            *  npr
9fa00 69 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70  intf --  No outp
9fa10 75 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20  ut, but returns 
9fa20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
9fa30 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20  aracters.**     
9fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fa50 20 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20       that would 
9fa60 68 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74  have been output
9fa70 20 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a   by printf..**.*
9fa80 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41  *           *  A
9fa90 20 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a   v- version (ex:
9faa0 20 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65   vsnprintf) of e
9fab0 76 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73  very function is
9fac0 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20   also.**        
9fad0 20 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a        supplied..
9fae0 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20  **.**      +  A 
9faf0 66 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74  few extensions t
9fb00 6f 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67  o the formatting
9fb10 20 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75   notation are su
9fb20 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  pported:.**.**  
9fb30 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20           *  The 
9fb40 22 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61  "=" flag (simila
9fb50 72 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73  r to "-") causes
9fb60 20 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62   the output to b
9fb70 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
9fb80 20 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e    be centered in
9fb90 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
9fba0 6c 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a  ly sized field..
9fbb0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
9fbc0 2a 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20  *  The %b field 
9fbd0 6f 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67  outputs an integ
9fbe0 65 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74  er in binary not
9fbf0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ation..**.**    
9fc00 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63         *  The %c
9fc10 20 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70   field now accep
9fc20 74 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20  ts a precision. 
9fc30 20 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f   The character o
9fc40 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20  utput.**        
9fc50 20 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65        is repeate
9fc60 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20  d by the number 
9fc70 6f 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65  of times the pre
9fc80 63 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73  cision specifies
9fc90 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..**.**         
9fca0 20 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c    *  The %' fiel
9fcb0 64 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c  d works like %c,
9fcc0 20 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74   but takes as it
9fcd0 73 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a  s character the.
9fce0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
9fcf0 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f  next character o
9fd00 66 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72  f the format str
9fd10 69 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20  ing, instead of 
9fd20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20  the next.**     
9fd30 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e           argumen
9fd40 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  t.  For example,
9fd50 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d    printf("%.78'-
9fd60 22 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69  ")  prints 78 mi
9fd70 6e 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nus.**          
9fd80 20 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73      signs, the s
9fd90 61 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22  ame as  printf("
9fda0 25 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a  %.78c",'-')..**.
9fdb0 2a 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20  **      +  When 
9fdc0 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47  compiled using G
9fdd0 43 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74  CC on a SPARC, t
9fde0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70  his version of p
9fdf0 72 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20  rintf is.**     
9fe00 20 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20      faster than 
9fe10 74 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e  the library prin
9fe20 74 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e  tf for SUN OS 4.
9fe30 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20  1..**.**      + 
9fe40 20 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61   All functions a
9fe50 72 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61  re fully reentra
9fe60 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  nt..**.*/../*.**
9fe70 20 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65   Conversion type
9fe80 73 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69  s fall into vari
9fe90 6f 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61  ous categories a
9fea0 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65  s defined by the
9feb0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e  .** following en
9fec0 75 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64  umeration..*/.#d
9fed0 65 66 69 6e 65 20 65 74 52 41 44 49 58 20 20 20  efine etRADIX   
9fee0 20 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65 72      1 /* Integer
9fef0 20 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78 2c   types.  %d, %x,
9ff00 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74   %o, and so fort
9ff10 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 46  h */.#define etF
9ff20 4c 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a 20  LOAT       2 /* 
9ff30 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20  Floating point. 
9ff40 20 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %f */.#define e
9ff50 74 45 58 50 20 20 20 20 20 20 20 20 20 33 20 2f  tEXP         3 /
9ff60 2a 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20  * Exponentional 
9ff70 6e 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64  notation. %e and
9ff80 20 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %E */.#define e
9ff90 74 47 45 4e 45 52 49 43 20 20 20 20 20 34 20 2f  tGENERIC     4 /
9ffa0 2a 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78  * Floating or ex
9ffb0 70 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e  ponential, depen
9ffc0 64 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74  ding on exponent
9ffd0 2e 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  . %g */.#define 
9ffe0 65 74 53 49 5a 45 20 20 20 20 20 20 20 20 35 20  etSIZE        5 
9fff0 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72  /* Return number
a0000 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 70   of characters p
a0010 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 2e  rocessed so far.
a0020 20 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %n */.#define e
a0030 74 53 54 52 49 4e 47 20 20 20 20 20 20 36 20 2f  tSTRING      6 /
a0040 2a 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f  * Strings. %s */
a0050 0a 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53 54  .#define etDYNST
a0060 52 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e 61  RING   7 /* Dyna
a0070 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
a0080 64 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f  d strings. %z */
a0090 0a 23 64 65 66 69 6e 65 20 65 74 50 45 52 43 45  .#define etPERCE
a00a0 4e 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72 63  NT     8 /* Perc
a00b0 65 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a  ent symbol. %% *
a00c0 2f 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41 52  /.#define etCHAR
a00d0 58 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68 61  X       9 /* Cha
a00e0 72 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f  racters. %c */./
a00f0 2a 20 54 68 65 20 72 65 73 74 20 61 72 65 20 65  * The rest are e
a0100 78 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e  xtensions, not n
a0110 6f 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e  ormally found in
a0120 20 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65   printf() */.#de
a0130 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45  fine etSQLESCAPE
a0140 20 20 31 30 20 2f 2a 20 53 74 72 69 6e 67 73 20    10 /* Strings 
a0150 77 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65  with '\'' double
a0160 64 2e 20 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e  d.  %q */.#defin
a0170 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 20 31  e etSQLESCAPE2 1
a0180 31 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74  1 /* Strings wit
a0190 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 20 61  h '\'' doubled a
a01a0 6e 64 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27  nd enclosed in '
a01b0 27 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ',.             
a01c0 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c               NUL
a01d0 4c 20 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61  L pointers repla
a01e0 63 65 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e  ced by SQL NULL.
a01f0 20 20 25 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20    %Q */.#define 
a0200 65 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20  etTOKEN      12 
a0210 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  /* a pointer to 
a0220 61 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72  a Token structur
a0230 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  e */.#define etS
a0240 52 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20  RCLIST    13 /* 
a0250 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53  a pointer to a S
a0260 72 63 4c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e  rcList */.#defin
a0270 65 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20 31  e etPOINTER    1
a0280 34 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e 76  4 /* The %p conv
a0290 65 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  ersion */.#defin
a02a0 65 20 65 74 53 51 4c 45 53 43 41 50 45 33 20 31  e etSQLESCAPE3 1
a02b0 35 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e  5 /* %w -> Strin
a02c0 67 73 20 77 69 74 68 20 27 5c 22 27 20 64 6f 75  gs with '\"' dou
a02d0 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  bled */.#define 
a02e0 65 74 4f 52 44 49 4e 41 4c 20 20 20 20 31 36 20  etORDINAL    16 
a02f0 2f 2a 20 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e  /* %r -> 1st, 2n
a0300 64 2c 20 33 72 64 2c 20 34 74 68 2c 20 65 74 63  d, 3rd, 4th, etc
a0310 2e 20 20 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20  .  English only 
a0320 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 65 74 49 4e  */..#define etIN
a0330 56 41 4c 49 44 20 20 20 20 20 30 20 2f 2a 20 41  VALID     0 /* A
a0340 6e 79 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20  ny unrecognized 
a0350 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20  conversion type 
a0360 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65  */.../*.** An "e
a0370 74 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d 62  tByte" is an 8-b
a0380 69 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c 75  it unsigned valu
a0390 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e  e..*/.typedef un
a03a0 73 69 67 6e 65 64 20 63 68 61 72 20 65 74 42 79  signed char etBy
a03b0 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  te;../*.** Each 
a03c0 62 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69  builtin conversi
a03d0 6f 6e 20 63 68 61 72 61 63 74 65 72 20 28 65 78  on character (ex
a03e0 3a 20 74 68 65 20 27 64 27 20 69 6e 20 22 25 64  : the 'd' in "%d
a03f0 22 29 20 69 73 20 64 65 73 63 72 69 62 65 64 0a  ") is described.
a0400 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63  ** by an instanc
a0410 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
a0420 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a  ng structure.*/.
a0430 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 65  typedef struct e
a0440 74 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e  t_info {   /* In
a0450 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
a0460 65 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65 6c  each format fiel
a0470 64 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74 74  d */.  char fmtt
a0480 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ype;            
a0490 2f 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 69  /* The format fi
a04a0 65 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72 20  eld code letter 
a04b0 2a 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73 65  */.  etByte base
a04c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
a04d0 20 54 68 65 20 62 61 73 65 20 66 6f 72 20 72 61   The base for ra
a04e0 64 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a  dix conversion *
a04f0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 73  /.  etByte flags
a0500 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
a0510 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46  One or more of F
a0520 4c 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62  LAG_ constants b
a0530 65 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74 65  elow */.  etByte
a0540 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20   type;          
a0550 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e     /* Conversion
a0560 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65   paradigm */.  e
a0570 74 42 79 74 65 20 63 68 61 72 73 65 74 3b 20 20  tByte charset;  
a0580 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
a0590 74 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d  t into aDigits[]
a05a0 20 6f 66 20 74 68 65 20 64 69 67 69 74 73 20 73   of the digits s
a05b0 74 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74  tring */.  etByt
a05c0 65 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20  e prefix;       
a05d0 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
a05e0 74 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66 20  to aPrefix[] of 
a05f0 74 68 65 20 70 72 65 66 69 78 20 73 74 72 69 6e  the prefix strin
a0600 67 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a  g */.} et_info;.
a0610 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
a0620 61 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e 66  alues for et_inf
a0630 6f 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69  o.flags.*/.#defi
a0640 6e 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 20  ne FLAG_SIGNED  
a0650 31 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  1     /* True if
a0660 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63 6f   the value to co
a0670 6e 76 65 72 74 20 69 73 20 73 69 67 6e 65 64 20  nvert is signed 
a0680 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f  */.#define FLAG_
a0690 49 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a  INTERN  2     /*
a06a0 20 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74   True if for int
a06b0 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a  ernal use only *
a06c0 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53  /.#define FLAG_S
a06d0 54 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20  TRING  4     /* 
a06e0 41 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70  Allow infinity p
a06f0 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a  recision */.../*
a0700 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
a0710 67 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63  g table is searc
a0720 68 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f  hed linearly, so
a0730 20 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 70   it is good to p
a0740 75 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66  ut the.** most f
a0750 72 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20 63  requently used c
a0760 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20  onversion types 
a0770 66 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  first..*/.static
a0780 20 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69 67   const char aDig
a0790 69 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35 36  its[] = "0123456
a07a0 37 38 39 41 42 43 44 45 46 30 31 32 33 34 35 36  789ABCDEF0123456
a07b0 37 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61 74  789abcdef";.stat
a07c0 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 50  ic const char aP
a07d0 72 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30  refix[] = "-x0\0
a07e0 30 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63 6f  00X0";.static co
a07f0 6e 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69  nst et_info fmti
a0800 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27  nfo[] = {.  {  '
a0810 64 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44  d', 10, 1, etRAD
a0820 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  IX,      0,  0 }
a0830 2c 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c 20  ,.  {  's',  0, 
a0840 34 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20 20  4, etSTRING,    
a0850 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a0860 67 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e  g',  0, 1, etGEN
a0870 45 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20 7d  ERIC,    30, 0 }
a0880 2c 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20  ,.  {  'z',  0, 
a0890 34 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c 20  4, etDYNSTRING, 
a08a0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a08b0 71 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c  q',  0, 4, etSQL
a08c0 45 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20 7d  ESCAPE,  0,  0 }
a08d0 2c 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20  ,.  {  'Q',  0, 
a08e0 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c  4, etSQLESCAPE2,
a08f0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a0900 77 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c  w',  0, 4, etSQL
a0910 45 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d  ESCAPE3, 0,  0 }
a0920 2c 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20  ,.  {  'c',  0, 
a0930 30 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20  0, etCHARX,     
a0940 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a0950 6f 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41 44  o',  8, 0, etRAD
a0960 49 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20 7d  IX,      0,  2 }
a0970 2c 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c 20  ,.  {  'u', 10, 
a0980 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20  0, etRADIX,     
a0990 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a09a0 78 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44  x', 16, 0, etRAD
a09b0 49 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20 7d  IX,      16, 1 }
a09c0 2c 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c 20  ,.  {  'X', 16, 
a09d0 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20  0, etRADIX,     
a09e0 20 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65   0,  4 },.#ifnde
a09f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
a0a00 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b  OATING_POINT.  {
a0a10 20 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65 74    'f',  0, 1, et
a0a20 46 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20 20  FLOAT,      0,  
a0a30 30 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20  0 },.  {  'e',  
a0a40 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20  0, 1, etEXP,    
a0a50 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b      30, 0 },.  {
a0a60 20 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65 74    'E',  0, 1, et
a0a70 45 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c 20  EXP,        14, 
a0a80 30 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20  0 },.  {  'G',  
a0a90 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c  0, 1, etGENERIC,
a0aa0 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e      14, 0 },.#en
a0ab0 64 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31 30  dif.  {  'i', 10
a0ac0 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20  , 1, etRADIX,   
a0ad0 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20     0,  0 },.  { 
a0ae0 20 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74 53   'n',  0, 0, etS
a0af0 49 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20 30  IZE,       0,  0
a0b00 20 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20 30   },.  {  '%',  0
a0b10 2c 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c 20  , 0, etPERCENT, 
a0b20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20     0,  0 },.  { 
a0b30 20 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50   'p', 16, 0, etP
a0b40 4f 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31  OINTER,    0,  1
a0b50 20 7d 2c 0a 0a 2f 2a 20 41 6c 6c 20 74 68 65 20   },../* All the 
a0b60 72 65 73 74 20 68 61 76 65 20 74 68 65 20 46 4c  rest have the FL
a0b70 41 47 5f 49 4e 54 45 52 4e 20 62 69 74 20 73 65  AG_INTERN bit se
a0b80 74 20 61 6e 64 20 61 72 65 20 74 68 75 73 20 66  t and are thus f
a0b90 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
a0ba0 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20  se only */.  {  
a0bb0 27 54 27 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f  'T',  0, 2, etTO
a0bc0 4b 45 4e 2c 20 20 20 20 20 20 30 2c 20 20 30 20  KEN,      0,  0 
a0bd0 7d 2c 0a 20 20 7b 20 20 27 53 27 2c 20 20 30 2c  },.  {  'S',  0,
a0be0 20 32 2c 20 65 74 53 52 43 4c 49 53 54 2c 20 20   2, etSRCLIST,  
a0bf0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
a0c00 27 72 27 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52  'r', 10, 3, etOR
a0c10 44 49 4e 41 4c 2c 20 20 20 20 30 2c 20 20 30 20  DINAL,    0,  0 
a0c20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  },.};../*.** If 
a0c30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
a0c40 54 49 4e 47 5f 50 4f 49 4e 54 20 69 73 20 64 65  TING_POINT is de
a0c50 66 69 6e 65 64 2c 20 74 68 65 6e 20 6e 6f 6e 65  fined, then none
a0c60 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67   of the floating
a0c70 20 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72   point.** conver
a0c80 73 69 6f 6e 73 20 77 69 6c 6c 20 77 6f 72 6b 2e  sions will work.
a0c90 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
a0ca0 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
a0cb0 5f 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76  _POINT./*.** "*v
a0cc0 61 6c 22 20 69 73 20 61 20 64 6f 75 62 6c 65 20  al" is a double 
a0cd0 73 75 63 68 20 74 68 61 74 20 30 2e 31 20 3c 3d  such that 0.1 <=
a0ce0 20 2a 76 61 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20   *val < 10.0.** 
a0cf0 52 65 74 75 72 6e 20 74 68 65 20 61 73 63 69 69  Return the ascii
a0d00 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65   code for the le
a0d10 61 64 69 6e 67 20 64 69 67 69 74 20 6f 66 20 2a  ading digit of *
a0d20 76 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c  val, then.** mul
a0d30 74 69 70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20  tiply "*val" by 
a0d40 31 30 2e 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c  10.0 to renormal
a0d50 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70  ize..**.** Examp
a0d60 6c 65 3a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74  le:.**     input
a0d70 3a 20 20 20 20 20 2a 76 61 6c 20 3d 20 33 2e 31  :     *val = 3.1
a0d80 34 31 35 39 0a 2a 2a 20 20 20 20 20 6f 75 74 70  4159.**     outp
a0d90 75 74 3a 20 20 20 20 2a 76 61 6c 20 3d 20 31 2e  ut:    *val = 1.
a0da0 34 31 35 39 20 20 20 20 66 75 6e 63 74 69 6f 6e  4159    function
a0db0 20 72 65 74 75 72 6e 20 3d 20 27 33 27 0a 2a 2a   return = '3'.**
a0dc0 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 65 72 20  .** The counter 
a0dd0 2a 63 6e 74 20 69 73 20 69 6e 63 72 65 6d 65 6e  *cnt is incremen
a0de0 74 65 64 20 65 61 63 68 20 74 69 6d 65 2e 20 20  ted each time.  
a0df0 41 66 74 65 72 20 63 6f 75 6e 74 65 72 20 65 78  After counter ex
a0e00 63 65 65 64 73 0a 2a 2a 20 31 36 20 28 74 68 65  ceeds.** 16 (the
a0e10 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 67 6e 69   number of signi
a0e20 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 69 6e  ficant digits in
a0e30 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 29   a 64-bit float)
a0e40 20 27 30 27 20 69 73 0a 2a 2a 20 61 6c 77 61 79   '0' is.** alway
a0e50 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  s returned..*/.s
a0e60 74 61 74 69 63 20 63 68 61 72 20 65 74 5f 67 65  tatic char et_ge
a0e70 74 64 69 67 69 74 28 4c 4f 4e 47 44 4f 55 42 4c  tdigit(LONGDOUBL
a0e80 45 5f 54 59 50 45 20 2a 76 61 6c 2c 20 69 6e 74  E_TYPE *val, int
a0e90 20 2a 63 6e 74 29 7b 0a 20 20 69 6e 74 20 64 69   *cnt){.  int di
a0ea0 67 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c  git;.  LONGDOUBL
a0eb0 45 5f 54 59 50 45 20 64 3b 0a 20 20 69 66 28 20  E_TYPE d;.  if( 
a0ec0 28 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31 36 20 29  (*cnt)++ >= 16 )
a0ed0 20 72 65 74 75 72 6e 20 27 30 27 3b 0a 20 20 64   return '0';.  d
a0ee0 69 67 69 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c  igit = (int)*val
a0ef0 3b 0a 20 20 64 20 3d 20 64 69 67 69 74 3b 0a 20  ;.  d = digit;. 
a0f00 20 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20   digit += '0';. 
a0f10 20 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20   *val = (*val - 
a0f20 64 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74 75 72  d)*10.0;.  retur
a0f30 6e 20 28 63 68 61 72 29 64 69 67 69 74 3b 0a 7d  n (char)digit;.}
a0f40 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
a0f50 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
a0f60 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  POINT */../*.** 
a0f70 41 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63  Append N space c
a0f80 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65  haracters to the
a0f90 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75   given string bu
a0fa0 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ffer..*/.static 
a0fb0 76 6f 69 64 20 61 70 70 65 6e 64 53 70 61 63 65  void appendSpace
a0fc0 28 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75  (StrAccum *pAccu
a0fd0 6d 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61  m, int N){.  sta
a0fe0 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  tic const char z
a0ff0 53 70 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20  Spaces[] = "    
a1000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1010 20 20 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68           ";.  wh
a1020 69 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a  ile( N>=(int)siz
a1030 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29  eof(zSpaces)-1 )
a1040 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72  {.    sqlite3Str
a1050 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63  AccumAppend(pAcc
a1060 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a  um, zSpaces, siz
a1070 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b  eof(zSpaces)-1);
a1080 0a 20 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66  .    N -= sizeof
a1090 28 7a 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d  (zSpaces)-1;.  }
a10a0 0a 20 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20  .  if( N>0 ){.  
a10b0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
a10c0 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20  mAppend(pAccum, 
a10d0 7a 53 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d  zSpaces, N);.  }
a10e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63  .}../*.** On mac
a10f0 68 69 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61  hines with a sma
a1100 6c 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79  ll stack size, y
a1110 6f 75 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20  ou can redefine 
a1120 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52  the.** SQLITE_PR
a1130 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20  INT_BUF_SIZE to 
a1140 62 65 20 6c 65 73 73 20 74 68 61 6e 20 33 35 30  be less than 350
a1150 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
a1160 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49  ITE_PRINT_BUF_SI
a1170 5a 45 0a 23 20 69 66 20 64 65 66 69 6e 65 64 28  ZE.# if defined(
a1180 53 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41  SQLITE_SMALL_STA
a1190 43 4b 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53  CK).#   define S
a11a0 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f  QLITE_PRINT_BUF_
a11b0 53 49 5a 45 20 35 30 0a 23 20 65 6c 73 65 0a 23  SIZE 50.# else.#
a11c0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
a11d0 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20  _PRINT_BUF_SIZE 
a11e0 33 35 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64  350.# endif.#end
a11f0 69 66 0a 23 64 65 66 69 6e 65 20 65 74 42 55 46  if.#define etBUF
a1200 53 49 5a 45 20 53 51 4c 49 54 45 5f 50 52 49 4e  SIZE SQLITE_PRIN
a1210 54 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53  T_BUF_SIZE  /* S
a1220 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75  ize of the outpu
a1230 74 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a  t buffer */../*.
a1240 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 72 6f 67  ** The root prog
a1250 72 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69 61 74  ram.  All variat
a1260 69 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73 20 63  ions call this c
a1270 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54  ore..**.** INPUT
a1280 53 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20 20 54  S:.**   func   T
a1290 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72  his is a pointer
a12a0 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74   to a function t
a12b0 61 6b 69 6e 67 20 74 68 72 65 65 20 61 72 67 75  aking three argu
a12c0 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 20  ments.**        
a12d0 20 20 20 20 31 2e 20 41 20 70 6f 69 6e 74 65 72      1. A pointer
a12e0 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20 20 53   to anything.  S
a12f0 61 6d 65 20 61 73 20 74 68 65 20 22 61 72 67 22  ame as the "arg"
a1300 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20   parameter..**  
a1310 20 20 20 20 20 20 20 20 20 20 32 2e 20 41 20 70            2. A p
a1320 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 69  ointer to the li
a1330 73 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  st of characters
a1340 20 74 6f 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a   to be output.**
a1350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
a1360 4e 6f 74 65 2c 20 74 68 69 73 20 6c 69 73 74 20  Note, this list 
a1370 69 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d  is NOT null term
a1380 69 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20  inated.).**     
a1390 20 20 20 20 20 20 20 33 2e 20 41 6e 20 69 6e 74         3. An int
a13a0 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63  eger number of c
a13b0 68 61 72 61 63 74 65 72 73 20 74 6f 20 62 65 20  haracters to be 
a13c0 6f 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20  output..**      
a13d0 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65 3a 20           (Note: 
a13e0 54 68 69 73 20 6e 75 6d 62 65 72 20 6d 69 67 68  This number migh
a13f0 74 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a  t be zero.).**.*
a1400 2a 20 20 20 61 72 67 20 20 20 20 54 68 69 73 20  *   arg    This 
a1410 69 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  is the pointer t
a1420 6f 20 61 6e 79 74 68 69 6e 67 20 77 68 69 63 68  o anything which
a1430 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20   will be passed 
a1440 61 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  as the.**       
a1450 20 20 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e     first argumen
a1460 74 20 74 6f 20 22 66 75 6e 63 22 2e 20 20 55 73  t to "func".  Us
a1470 65 20 69 74 20 66 6f 72 20 77 68 61 74 65 76 65  e it for whateve
a1480 72 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a  r you like..**.*
a1490 2a 20 20 20 66 6d 74 20 20 20 20 54 68 69 73 20  *   fmt    This 
a14a0 69 73 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74  is the format st
a14b0 72 69 6e 67 2c 20 61 73 20 69 6e 20 74 68 65 20  ring, as in the 
a14c0 75 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a  usual print..**.
a14d0 2a 2a 20 20 20 61 70 20 20 20 20 20 54 68 69 73  **   ap     This
a14e0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
a14f0 20 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d   a list of argum
a1500 65 6e 74 73 2e 20 20 53 61 6d 65 20 61 73 20 69  ents.  Same as i
a1510 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 66  n.**          vf
a1520 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54  print..**.** OUT
a1530 50 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20  PUTS:.**        
a1540 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c    The return val
a1550 75 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20  ue is the total 
a1560 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
a1570 74 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20  ters sent to.** 
a1580 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75 6e           the fun
a1590 63 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52  ction "func".  R
a15a0 65 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65  eturns -1 on a e
a15b0 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  rror..**.** Note
a15c0 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20   that the order 
a15d0 69 6e 20 77 68 69 63 68 20 61 75 74 6f 6d 61 74  in which automat
a15e0 69 63 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  ic variables are
a15f0 20 64 65 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a   declared below.
a1600 2a 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65  ** seems to make
a1610 20 61 20 62 69 67 20 64 69 66 66 65 72 65 6e 63   a big differenc
a1620 65 20 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67  e in determining
a1630 20 68 6f 77 20 66 61 73 74 20 74 68 69 73 20 62   how fast this b
a1640 65 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e  east.** will run
a1650 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
a1660 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
a1670 56 58 50 72 69 6e 74 66 28 0a 20 20 53 74 72 41  VXPrintf(.  StrA
a1680 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20  ccum *pAccum,   
a1690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a16a0 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65 73  * Accumulate res
a16b0 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69  ults here */.  i
a16c0 6e 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20  nt useExtended, 
a16d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a16e0 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e    /* Allow exten
a16f0 64 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e  ded %-conversion
a1700 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
a1710 72 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20  r *fmt,         
a1720 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
a1730 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  mat string */.  
a1740 76 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20  va_list ap      
a1750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1760 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20     /* arguments 
a1770 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20  */.){.  int c;  
a1780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1790 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61     /* Next chara
a17a0 63 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d  cter in the form
a17b0 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63  at string */.  c
a17c0 68 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20  har *bufpt;     
a17d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
a17e0 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76  nter to the conv
a17f0 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f  ersion buffer */
a1800 0a 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e  .  int precision
a1810 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
a1820 20 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68   Precision of th
a1830 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20  e current field 
a1840 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b  */.  int length;
a1850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1860 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65  /* Length of the
a1870 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20   field */.  int 
a1880 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  idx;            
a1890 20 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65         /* A gene
a18a0 72 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70  ral purpose loop
a18b0 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
a18c0 74 20 77 69 64 74 68 3b 20 20 20 20 20 20 20 20  t width;        
a18d0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74           /* Widt
a18e0 68 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  h of the current
a18f0 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79   field */.  etBy
a1900 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74  te flag_leftjust
a1910 69 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69  ify;   /* True i
a1920 66 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72  f "-" flag is pr
a1930 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74  esent */.  etByt
a1940 65 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b  e flag_plussign;
a1950 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a1960 20 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65   "+" flag is pre
a1970 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
a1980 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b   flag_blanksign;
a1990 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a19a0 22 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73  " " flag is pres
a19b0 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ent */.  etByte 
a19c0 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
a19d0 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22  rm; /* True if "
a19e0 23 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  #" flag is prese
a19f0 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  nt */.  etByte f
a1a00 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20  lag_altform2;   
a1a10 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21     /* True if "!
a1a20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
a1a30 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
a1a40 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20  ag_zeropad;     
a1a50 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65    /* True if fie
a1a60 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e  ld width constan
a1a70 74 20 73 74 61 72 74 73 20 77 69 74 68 20 7a 65  t starts with ze
a1a80 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  ro */.  etByte f
a1a90 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20  lag_long;       
a1aa0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c     /* True if "l
a1ab0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
a1ac0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
a1ad0 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20  ag_longlong;    
a1ae0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
a1af0 20 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72   "ll" flag is pr
a1b00 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74  esent */.  etByt
a1b10 65 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20  e done;         
a1b20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65        /* Loop te
a1b30 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a  rmination flag *
a1b40 2f 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  /.  sqlite_uint6
a1b50 34 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f  4 longvalue;   /
a1b60 2a 20 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65  * Value for inte
a1b70 67 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c  ger types */.  L
a1b80 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72  ONGDOUBLE_TYPE r
a1b90 65 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c  ealvalue; /* Val
a1ba0 75 65 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65  ue for real type
a1bb0 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f  s */.  const et_
a1bc0 69 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20  info *infop;    
a1bd0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
a1be0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
a1bf0 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a  info structure *
a1c00 2f 0a 20 20 63 68 61 72 20 62 75 66 5b 65 74 42  /.  char buf[etB
a1c10 55 46 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f  UFSIZE];       /
a1c20 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66  * Conversion buf
a1c30 66 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72  fer */.  char pr
a1c40 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  efix;           
a1c50 20 20 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68      /* Prefix ch
a1c60 61 72 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72  aracter.  "+" or
a1c70 20 22 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27   "-" or " " or '
a1c80 5c 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65  \0'. */.  etByte
a1c90 20 78 74 79 70 65 20 3d 20 30 3b 20 20 20 20 20   xtype = 0;     
a1ca0 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69       /* Conversi
a1cb0 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20  on paradigm */. 
a1cc0 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20   char *zExtra;  
a1cd0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
a1ce0 78 74 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64  xtra memory used
a1cf0 20 66 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45   for etTCLESCAPE
a1d00 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a   conversions */.
a1d10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
a1d20 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
a1d30 4e 54 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65  NT.  int  exp, e
a1d40 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2;              
a1d50 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72  /* exponent of r
a1d60 65 61 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20  eal numbers */. 
a1d70 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b   double rounder;
a1d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
a1d90 73 65 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67  sed for rounding
a1da0 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
a1db0 76 61 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79  values */.  etBy
a1dc0 74 65 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20  te flag_dp;     
a1dd0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
a1de0 66 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  f decimal point 
a1df0 73 68 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20  should be shown 
a1e00 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
a1e10 5f 72 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20  _rtz;           
a1e20 2f 2a 20 54 72 75 65 20 69 66 20 74 72 61 69 6c  /* True if trail
a1e30 69 6e 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64  ing zeros should
a1e40 20 62 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20   be removed */. 
a1e50 20 65 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70   etByte flag_exp
a1e60 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
a1e70 72 75 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73  rue to force dis
a1e80 70 6c 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f  play of the expo
a1e90 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73  nent */.  int ns
a1ea0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
a1eb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
a1ec0 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69  f significant di
a1ed0 67 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f  gits returned */
a1ee0 0a 23 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74  .#endif..  lengt
a1ef0 68 20 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d  h = 0;.  bufpt =
a1f00 20 30 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28   0;.  for(; (c=(
a1f10 2a 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74  *fmt))!=0; ++fmt
a1f20 29 7b 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25  ){.    if( c!='%
a1f30 27 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61  ' ){.      int a
a1f40 6d 74 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20  mt;.      bufpt 
a1f50 3d 20 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20  = (char *)fmt;. 
a1f60 20 20 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20       amt = 1;.  
a1f70 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a      while( (c=(*
a1f80 2b 2b 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20  ++fmt))!='%' && 
a1f90 63 21 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20  c!=0 ) amt++;.  
a1fa0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
a1fb0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
a1fc0 2c 20 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20  , bufpt, amt);. 
a1fd0 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20       if( c==0 ) 
a1fe0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
a1ff0 20 69 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29   if( (c=(*++fmt)
a2000 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )==0 ){.      sq
a2010 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
a2020 65 6e 64 28 70 41 63 63 75 6d 2c 20 22 25 22 2c  end(pAccum, "%",
a2030 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   1);.      break
a2040 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46  ;.    }.    /* F
a2050 69 6e 64 20 6f 75 74 20 77 68 61 74 20 66 6c 61  ind out what fla
a2060 67 73 20 61 72 65 20 70 72 65 73 65 6e 74 20 2a  gs are present *
a2070 2f 0a 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a  /.    flag_leftj
a2080 75 73 74 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c  ustify = flag_pl
a2090 75 73 73 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c  ussign = flag_bl
a20a0 61 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 20 20 20  anksign = .     
a20b0 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
a20c0 72 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72  rm = flag_altfor
a20d0 6d 32 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61  m2 = flag_zeropa
a20e0 64 20 3d 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20  d = 0;.    done 
a20f0 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20  = 0;.    do{.   
a2100 20 20 20 73 77 69 74 63 68 28 20 63 20 29 7b 0a     switch( c ){.
a2110 20 20 20 20 20 20 20 20 63 61 73 65 20 27 2d 27          case '-'
a2120 3a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73  :   flag_leftjus
a2130 74 69 66 79 20 3d 20 31 3b 20 20 20 20 20 62 72  tify = 1;     br
a2140 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
a2150 65 20 27 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c  e '+':   flag_pl
a2160 75 73 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20  ussign = 1;     
a2170 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a2180 20 20 63 61 73 65 20 27 20 27 3a 20 20 20 66 6c    case ' ':   fl
a2190 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31  ag_blanksign = 1
a21a0 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;       break;. 
a21b0 20 20 20 20 20 20 20 63 61 73 65 20 27 23 27 3a         case '#':
a21c0 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74     flag_alternat
a21d0 65 66 6f 72 6d 20 3d 20 31 3b 20 20 20 62 72 65  eform = 1;   bre
a21e0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
a21f0 20 27 21 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74   '!':   flag_alt
a2200 66 6f 72 6d 32 20 3d 20 31 3b 20 20 20 20 20 20  form2 = 1;      
a2210 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
a2220 20 63 61 73 65 20 27 30 27 3a 20 20 20 66 6c 61   case '0':   fla
a2230 67 5f 7a 65 72 6f 70 61 64 20 3d 20 31 3b 20 20  g_zeropad = 1;  
a2240 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a2250 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20        default:  
a2260 20 20 64 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20    done = 1;     
a2270 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
a2280 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
a2290 77 68 69 6c 65 28 20 21 64 6f 6e 65 20 26 26 20  while( !done && 
a22a0 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20  (c=(*++fmt))!=0 
a22b0 29 3b 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68  );.    /* Get th
a22c0 65 20 66 69 65 6c 64 20 77 69 64 74 68 20 2a 2f  e field width */
a22d0 0a 20 20 20 20 77 69 64 74 68 20 3d 20 30 3b 0a  .    width = 0;.
a22e0 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29      if( c=='*' )
a22f0 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20  {.      width = 
a2300 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a  va_arg(ap,int);.
a2310 20 20 20 20 20 20 69 66 28 20 77 69 64 74 68 3c        if( width<
a2320 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61  0 ){.        fla
a2330 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20  g_leftjustify = 
a2340 31 3b 0a 20 20 20 20 20 20 20 20 77 69 64 74 68  1;.        width
a2350 20 3d 20 2d 77 69 64 74 68 3b 0a 20 20 20 20 20   = -width;.     
a2360 20 7d 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b   }.      c = *++
a2370 66 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  fmt;.    }else{.
a2380 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d        while( c>=
a2390 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b  '0' && c<='9' ){
a23a0 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d  .        width =
a23b0 20 77 69 64 74 68 2a 31 30 20 2b 20 63 20 2d 20   width*10 + c - 
a23c0 27 30 27 3b 0a 20 20 20 20 20 20 20 20 63 20 3d  '0';.        c =
a23d0 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d   *++fmt;.      }
a23e0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77  .    }.    if( w
a23f0 69 64 74 68 20 3e 20 65 74 42 55 46 53 49 5a 45  idth > etBUFSIZE
a2400 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 77 69 64  -10 ){.      wid
a2410 74 68 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 31  th = etBUFSIZE-1
a2420 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20  0;.    }.    /* 
a2430 47 65 74 20 74 68 65 20 70 72 65 63 69 73 69 6f  Get the precisio
a2440 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d  n */.    if( c==
a2450 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 70 72 65  '.' ){.      pre
a2460 63 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20  cision = 0;.    
a2470 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20    c = *++fmt;.  
a2480 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29      if( c=='*' )
a2490 7b 0a 20 20 20 20 20 20 20 20 70 72 65 63 69 73  {.        precis
a24a0 69 6f 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  ion = va_arg(ap,
a24b0 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  int);.        if
a24c0 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20  ( precision<0 ) 
a24d0 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65  precision = -pre
a24e0 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20  cision;.        
a24f0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20  c = *++fmt;.    
a2500 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
a2510 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26   while( c>='0' &
a2520 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20  & c<='9' ){.    
a2530 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20        precision 
a2540 3d 20 70 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b  = precision*10 +
a2550 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20   c - '0';.      
a2560 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a      c = *++fmt;.
a2570 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a2580 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
a2590 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d     precision = -
a25a0 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20  1;.    }.    /* 
a25b0 47 65 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69  Get the conversi
a25c0 6f 6e 20 74 79 70 65 20 6d 6f 64 69 66 69 65 72  on type modifier
a25d0 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27   */.    if( c=='
a25e0 6c 27 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67  l' ){.      flag
a25f0 5f 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20  _long = 1;.     
a2600 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20   c = *++fmt;.   
a2610 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b     if( c=='l' ){
a2620 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f  .        flag_lo
a2630 6e 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20  nglong = 1;.    
a2640 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a      c = *++fmt;.
a2650 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a2660 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f       flag_longlo
a2670 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  ng = 0;.      }.
a2680 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a2690 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61   flag_long = fla
a26a0 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a  g_longlong = 0;.
a26b0 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74      }.    /* Fet
a26c0 63 68 20 74 68 65 20 69 6e 66 6f 20 65 6e 74 72  ch the info entr
a26d0 79 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 20  y for the field 
a26e0 2a 2f 0a 20 20 20 20 69 6e 66 6f 70 20 3d 20 26  */.    infop = &
a26f0 66 6d 74 69 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20  fmtinfo[0];.    
a2700 78 74 79 70 65 20 3d 20 65 74 49 4e 56 41 4c 49  xtype = etINVALI
a2710 44 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 30  D;.    for(idx=0
a2720 3b 20 69 64 78 3c 41 72 72 61 79 53 69 7a 65 28  ; idx<ArraySize(
a2730 66 6d 74 69 6e 66 6f 29 3b 20 69 64 78 2b 2b 29  fmtinfo); idx++)
a2740 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 66  {.      if( c==f
a2750 6d 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74  mtinfo[idx].fmtt
a2760 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ype ){.        i
a2770 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b  nfop = &fmtinfo[
a2780 69 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 69 66  idx];.        if
a2790 28 20 75 73 65 45 78 74 65 6e 64 65 64 20 7c 7c  ( useExtended ||
a27a0 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26   (infop->flags &
a27b0 20 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30   FLAG_INTERN)==0
a27c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78 74   ){.          xt
a27d0 79 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70  ype = infop->typ
a27e0 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  e;.        }else
a27f0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
a2800 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rn;.        }.  
a2810 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a2820 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a     }.    }.    z
a2830 45 78 74 72 61 20 3d 20 30 3b 0a 0a 0a 20 20 20  Extra = 0;...   
a2840 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 70 72   /* Limit the pr
a2850 65 63 69 73 69 6f 6e 20 74 6f 20 70 72 65 76 65  ecision to preve
a2860 6e 74 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62  nt overflowing b
a2870 75 66 5b 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76  uf[] during conv
a2880 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66  ersion */.    if
a2890 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55  ( precision>etBU
a28a0 46 53 49 5a 45 2d 34 30 20 26 26 20 28 69 6e 66  FSIZE-40 && (inf
a28b0 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47  op->flags & FLAG
a28c0 5f 53 54 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20  _STRING)==0 ){. 
a28d0 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d       precision =
a28e0 20 65 74 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20   etBUFSIZE-40;. 
a28f0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
a2900 20 2a 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   ** At this poin
a2910 74 2c 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  t, variables are
a2920 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 73 20   initialized as 
a2930 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a  follows:.    **.
a2940 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c      **   flag_al
a2950 74 65 72 6e 61 74 65 66 6f 72 6d 20 20 20 20 20  ternateform     
a2960 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27       TRUE if a '
a2970 23 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20  #' is present.. 
a2980 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74     **   flag_alt
a2990 66 6f 72 6d 32 20 20 20 20 20 20 20 20 20 20 20  form2           
a29a0 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 21      TRUE if a '!
a29b0 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20  ' is present..  
a29c0 20 20 2a 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73    **   flag_plus
a29d0 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20  sign            
a29e0 20 20 20 54 52 55 45 20 69 66 20 61 20 27 2b 27     TRUE if a '+'
a29f0 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20   is present..   
a2a00 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a   **   flag_leftj
a2a10 75 73 74 69 66 79 20 20 20 20 20 20 20 20 20 20  ustify          
a2a20 20 20 54 52 55 45 20 69 66 20 61 20 27 2d 27 20    TRUE if a '-' 
a2a30 69 73 20 70 72 65 73 65 6e 74 20 6f 72 20 69 66  is present or if
a2a40 20 74 68 65 0a 20 20 20 20 2a 2a 20 20 20 20 20   the.    **     
a2a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2a60 20 20 20 20 20 20 20 20 20 20 66 69 65 6c 64 20            field 
a2a70 77 69 64 74 68 20 77 61 73 20 6e 65 67 61 74 69  width was negati
a2a80 76 65 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61  ve..    **   fla
a2a90 67 5f 7a 65 72 6f 70 61 64 20 20 20 20 20 20 20  g_zeropad       
a2aa0 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66           TRUE if
a2ab0 20 74 68 65 20 77 69 64 74 68 20 62 65 67 61 6e   the width began
a2ac0 20 77 69 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20   with 0..    ** 
a2ad0 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20    flag_long     
a2ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52                TR
a2af0 55 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72  UE if the letter
a2b00 20 27 6c 27 20 28 65 6c 6c 29 20 70 72 65 66 69   'l' (ell) prefi
a2b10 78 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  xed.    **      
a2b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2b30 20 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e           the con
a2b40 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65  version characte
a2b50 72 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67  r..    **   flag
a2b60 5f 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20  _longlong       
a2b70 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20          TRUE if 
a2b80 74 68 65 20 6c 65 74 74 65 72 20 27 6c 6c 27 20  the letter 'll' 
a2b90 28 65 6c 6c 20 65 6c 6c 29 20 70 72 65 66 69 78  (ell ell) prefix
a2ba0 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  ed.    **       
a2bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2bc0 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76          the conv
a2bd0 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72  ersion character
a2be0 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f  ..    **   flag_
a2bf0 62 6c 61 6e 6b 73 69 67 6e 20 20 20 20 20 20 20  blanksign       
a2c00 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61         TRUE if a
a2c10 20 27 20 27 20 69 73 20 70 72 65 73 65 6e 74 2e   ' ' is present.
a2c20 0a 20 20 20 20 2a 2a 20 20 20 77 69 64 74 68 20  .    **   width 
a2c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2c40 20 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66        The specif
a2c50 69 65 64 20 66 69 65 6c 64 20 77 69 64 74 68 2e  ied field width.
a2c60 20 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a 2a    This is.    **
a2c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
a2c90 6c 77 61 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69  lways non-negati
a2ca0 76 65 2e 20 20 5a 65 72 6f 20 69 73 20 74 68 65  ve.  Zero is the
a2cb0 20 64 65 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a   default..    **
a2cc0 20 20 20 70 72 65 63 69 73 69 6f 6e 20 20 20 20     precision    
a2cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
a2ce0 68 65 20 73 70 65 63 69 66 69 65 64 20 70 72 65  he specified pre
a2cf0 63 69 73 69 6f 6e 2e 20 20 54 68 65 20 64 65 66  cision.  The def
a2d00 61 75 6c 74 0a 20 20 20 20 2a 2a 20 20 20 20 20  ault.    **     
a2d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2d20 20 20 20 20 20 20 20 20 20 20 69 73 20 2d 31 2e            is -1.
a2d30 0a 20 20 20 20 2a 2a 20 20 20 78 74 79 70 65 20  .    **   xtype 
a2d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2d50 20 20 20 20 20 20 54 68 65 20 63 6c 61 73 73 20        The class 
a2d60 6f 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  of the conversio
a2d70 6e 2e 0a 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f  n..    **   info
a2d80 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
a2d90 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20          Pointer 
a2da0 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  to the appropria
a2db0 74 65 20 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a  te info struct..
a2dc0 20 20 20 20 2a 2f 0a 20 20 20 20 73 77 69 74 63      */.    switc
a2dd0 68 28 20 78 74 79 70 65 20 29 7b 0a 20 20 20 20  h( xtype ){.    
a2de0 20 20 63 61 73 65 20 65 74 50 4f 49 4e 54 45 52    case etPOINTER
a2df0 3a 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c  :.        flag_l
a2e00 6f 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66  onglong = sizeof
a2e10 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28  (char*)==sizeof(
a2e20 69 36 34 29 3b 0a 20 20 20 20 20 20 20 20 66 6c  i64);.        fl
a2e30 61 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66  ag_long = sizeof
a2e40 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28  (char*)==sizeof(
a2e50 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20  long int);.     
a2e60 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75     /* Fall throu
a2e70 67 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74  gh into the next
a2e80 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 63   case */.      c
a2e90 61 73 65 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20  ase etORDINAL:. 
a2ea0 20 20 20 20 20 63 61 73 65 20 65 74 52 41 44 49       case etRADI
a2eb0 58 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  X:.        if( i
a2ec0 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c  nfop->flags & FL
a2ed0 41 47 5f 53 49 47 4e 45 44 20 29 7b 0a 20 20 20  AG_SIGNED ){.   
a2ee0 20 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20         i64 v;.  
a2ef0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
a2f00 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20  _longlong ){.   
a2f10 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f           v = va_
a2f20 61 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20  arg(ap,i64);.   
a2f30 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
a2f40 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20   flag_long ){.  
a2f50 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61            v = va
a2f60 5f 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74  _arg(ap,long int
a2f70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
a2f80 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
a2f90 76 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e  v = va_arg(ap,in
a2fa0 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  t);.          }.
a2fb0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 76 3c            if( v<
a2fc0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
a2fd0 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b   longvalue = -v;
a2fe0 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65  .            pre
a2ff0 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20  fix = '-';.     
a3000 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a3010 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75          longvalu
a3020 65 20 3d 20 76 3b 0a 20 20 20 20 20 20 20 20 20  e = v;.         
a3030 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73     if( flag_plus
a3040 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 70 72  sign )        pr
a3050 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20  efix = '+';.    
a3060 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28          else if(
a3070 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20   flag_blanksign 
a3080 29 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b  )  prefix = ' ';
a3090 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73  .            els
a30a0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
a30b0 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
a30c0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   0;.          }.
a30d0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
a30e0 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61           if( fla
a30f0 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20  g_longlong ){.  
a3100 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61            longva
a3110 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  lue = va_arg(ap,
a3120 75 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  u64);.          
a3130 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c  }else if( flag_l
a3140 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ong ){.         
a3150 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76     longvalue = v
a3160 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65  a_arg(ap,unsigne
a3170 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20  d long int);.   
a3180 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a3190 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61            longva
a31a0 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  lue = va_arg(ap,
a31b0 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20  unsigned int);. 
a31c0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
a31d0 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b       prefix = 0;
a31e0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a31f0 20 20 20 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65     if( longvalue
a3200 3d 3d 30 20 29 20 66 6c 61 67 5f 61 6c 74 65 72  ==0 ) flag_alter
a3210 6e 61 74 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20  nateform = 0;.  
a3220 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a        if( flag_z
a3230 65 72 6f 70 61 64 20 26 26 20 70 72 65 63 69 73  eropad && precis
a3240 69 6f 6e 3c 77 69 64 74 68 2d 28 70 72 65 66 69  ion<width-(prefi
a3250 78 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20  x!=0) ){.       
a3260 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 77     precision = w
a3270 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29  idth-(prefix!=0)
a3280 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a3290 20 20 20 20 62 75 66 70 74 20 3d 20 26 62 75 66      bufpt = &buf
a32a0 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20  [etBUFSIZE-1];. 
a32b0 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65         if( xtype
a32c0 3d 3d 65 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20  ==etORDINAL ){. 
a32d0 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20           static 
a32e0 63 6f 6e 73 74 20 63 68 61 72 20 7a 4f 72 64 5b  const char zOrd[
a32f0 5d 20 3d 20 22 74 68 73 74 6e 64 72 64 22 3b 0a  ] = "thstndrd";.
a3300 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 78 20            int x 
a3310 3d 20 28 69 6e 74 29 28 6c 6f 6e 67 76 61 6c 75  = (int)(longvalu
a3320 65 20 25 20 31 30 29 3b 0a 20 20 20 20 20 20 20  e % 10);.       
a3330 20 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28     if( x>=4 || (
a3340 6c 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 30  longvalue/10)%10
a3350 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==1 ){.         
a3360 20 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20     x = 0;.      
a3370 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
a3380 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d  buf[etBUFSIZE-3]
a3390 20 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20   = zOrd[x*2];.  
a33a0 20 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 55          buf[etBU
a33b0 46 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b  FSIZE-2] = zOrd[
a33c0 78 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20  x*2+1];.        
a33d0 20 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20    bufpt -= 2;.  
a33e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a33f0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69  {.          regi
a3400 73 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20  ster const char 
a3410 2a 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55  *cset;      /* U
a3420 73 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72  se registers for
a3430 20 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 20   speed */.      
a3440 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74      register int
a3450 20 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20   base;.         
a3460 20 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73   cset = &aDigits
a3470 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d  [infop->charset]
a3480 3b 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 65  ;.          base
a3490 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a   = infop->base;.
a34a0 20 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 20            do{   
a34b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a34c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a34d0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65          /* Conve
a34e0 72 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20  rt to ascii */. 
a34f0 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62             *(--b
a3500 75 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e  ufpt) = cset[lon
a3510 67 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20  gvalue%base];.  
a3520 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61            longva
a3530 6c 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f  lue = longvalue/
a3540 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  base;.          
a3550 7d 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75  }while( longvalu
a3560 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d  e>0 );.        }
a3570 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
a3580 3d 20 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42  = (int)(&buf[etB
a3590 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29  UFSIZE-1]-bufpt)
a35a0 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64  ;.        for(id
a35b0 78 3d 70 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67  x=precision-leng
a35c0 74 68 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d  th; idx>0; idx--
a35d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d  ){.          *(-
a35e0 2d 62 75 66 70 74 29 20 3d 20 27 30 27 3b 20 20  -bufpt) = '0';  
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 2f 2a 20 5a 65             /* Ze
a3610 72 6f 20 70 61 64 20 2a 2f 0a 20 20 20 20 20 20  ro pad */.      
a3620 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
a3630 70 72 65 66 69 78 20 29 20 2a 28 2d 2d 62 75 66  prefix ) *(--buf
a3640 70 74 29 20 3d 20 70 72 65 66 69 78 3b 20 20 20  pt) = prefix;   
a3650 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
a3660 64 64 20 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20  dd sign */.     
a3670 20 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 65     if( flag_alte
a3680 72 6e 61 74 65 66 6f 72 6d 20 26 26 20 69 6e 66  rnateform && inf
a3690 6f 70 2d 3e 70 72 65 66 69 78 20 29 7b 20 20 20  op->prefix ){   
a36a0 20 20 20 2f 2a 20 41 64 64 20 22 30 22 20 6f 72     /* Add "0" or
a36b0 20 22 30 78 22 20 2a 2f 0a 20 20 20 20 20 20 20   "0x" */.       
a36c0 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
a36d0 72 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68  re;.          ch
a36e0 61 72 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20  ar x;.          
a36f0 70 72 65 20 3d 20 26 61 50 72 65 66 69 78 5b 69  pre = &aPrefix[i
a3700 6e 66 6f 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20  nfop->prefix];. 
a3710 20 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 28           for(; (
a3720 78 3d 28 2a 70 72 65 29 29 21 3d 30 3b 20 70 72  x=(*pre))!=0; pr
a3730 65 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 74 29 20  e++) *(--bufpt) 
a3740 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = x;.        }. 
a3750 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a3760 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46  (int)(&buf[etBUF
a3770 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a  SIZE-1]-bufpt);.
a3780 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
a3790 20 20 20 20 20 63 61 73 65 20 65 74 46 4c 4f 41       case etFLOA
a37a0 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  T:.      case et
a37b0 45 58 50 3a 0a 20 20 20 20 20 20 63 61 73 65 20  EXP:.      case 
a37c0 65 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20  etGENERIC:.     
a37d0 20 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76     realvalue = v
a37e0 61 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29  a_arg(ap,double)
a37f0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
a3800 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
a3810 4f 49 4e 54 0a 20 20 20 20 20 20 20 20 69 66 28  OINT.        if(
a3820 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70   precision<0 ) p
a3830 72 65 63 69 73 69 6f 6e 20 3d 20 36 3b 20 20 20  recision = 6;   
a3840 20 20 20 20 20 20 2f 2a 20 53 65 74 20 64 65 66        /* Set def
a3850 61 75 6c 74 20 70 72 65 63 69 73 69 6f 6e 20 2a  ault precision *
a3860 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72  /.        if( pr
a3870 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a  ecision>etBUFSIZ
a3880 45 2f 32 2d 31 30 20 29 20 70 72 65 63 69 73 69  E/2-10 ) precisi
a3890 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2f 32  on = etBUFSIZE/2
a38a0 2d 31 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  -10;.        if(
a38b0 20 72 65 61 6c 76 61 6c 75 65 3c 30 2e 30 20 29   realvalue<0.0 )
a38c0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c  {.          real
a38d0 76 61 6c 75 65 20 3d 20 2d 72 65 61 6c 76 61 6c  value = -realval
a38e0 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72  ue;.          pr
a38f0 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20  efix = '-';.    
a3900 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a3910 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c       if( flag_pl
a3920 75 73 73 69 67 6e 20 29 20 20 20 20 20 20 20 20  ussign )        
a3930 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a    prefix = '+';.
a3940 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69            else i
a3950 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67  f( flag_blanksig
a3960 6e 20 29 20 20 20 20 70 72 65 66 69 78 20 3d 20  n )    prefix = 
a3970 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65  ' ';.          e
a3980 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  lse             
a3990 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66              pref
a39a0 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ix = 0;.        
a39b0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74  }.        if( xt
a39c0 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 26  ype==etGENERIC &
a39d0 26 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 20  & precision>0 ) 
a39e0 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66  precision--;.#if
a39f0 20 30 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f   0.        /* Ro
a3a00 75 6e 64 69 6e 67 20 77 6f 72 6b 73 20 6c 69 6b  unding works lik
a3a10 65 20 42 53 44 20 77 68 65 6e 20 74 68 65 20 63  e BSD when the c
a3a20 6f 6e 73 74 61 6e 74 20 30 2e 34 39 39 39 20 69  onstant 0.4999 i
a3a30 73 20 75 73 65 64 2e 20 20 57 69 65 72 64 21 20  s used.  Wierd! 
a3a40 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  */.        for(i
a3a50 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f  dx=precision, ro
a3a60 75 6e 64 65 72 3d 30 2e 34 39 39 39 3b 20 69 64  under=0.4999; id
a3a70 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e  x>0; idx--, roun
a3a80 64 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 6c 73 65  der*=0.1);.#else
a3a90 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 74 20 6d  .        /* It m
a3aa0 61 6b 65 73 20 6d 6f 72 65 20 73 65 6e 73 65 20  akes more sense 
a3ab0 74 6f 20 75 73 65 20 30 2e 35 20 2a 2f 0a 20 20  to use 0.5 */.  
a3ac0 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72        for(idx=pr
a3ad0 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72  ecision, rounder
a3ae0 3d 30 2e 35 3b 20 69 64 78 3e 30 3b 20 69 64 78  =0.5; idx>0; idx
a3af0 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31  --, rounder*=0.1
a3b00 29 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ){}.#endif.     
a3b10 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74     if( xtype==et
a3b20 46 4c 4f 41 54 20 29 20 72 65 61 6c 76 61 6c 75  FLOAT ) realvalu
a3b30 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20  e += rounder;.  
a3b40 20 20 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69        /* Normali
a3b50 7a 65 20 72 65 61 6c 76 61 6c 75 65 20 74 6f 20  ze realvalue to 
a3b60 77 69 74 68 69 6e 20 31 30 2e 30 20 3e 20 72 65  within 10.0 > re
a3b70 61 6c 76 61 6c 75 65 20 3e 3d 20 31 2e 30 20 2a  alvalue >= 1.0 *
a3b80 2f 0a 20 20 20 20 20 20 20 20 65 78 70 20 3d 20  /.        exp = 
a3b90 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  0;.        if( s
a3ba0 71 6c 69 74 65 33 49 73 4e 61 4e 28 28 64 6f 75  qlite3IsNaN((dou
a3bb0 62 6c 65 29 72 65 61 6c 76 61 6c 75 65 29 20 29  ble)realvalue) )
a3bc0 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70  {.          bufp
a3bd0 74 20 3d 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20  t = "NaN";.     
a3be0 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b       length = 3;
a3bf0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
a3c00 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a3c10 20 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75      if( realvalu
a3c20 65 3e 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20  e>0.0 ){.       
a3c30 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61     while( realva
a3c40 6c 75 65 3e 3d 31 65 33 32 20 26 26 20 65 78 70  lue>=1e32 && exp
a3c50 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c  <=350 ){ realval
a3c60 75 65 20 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70  ue *= 1e-32; exp
a3c70 2b 3d 33 32 3b 20 7d 0a 20 20 20 20 20 20 20 20  +=32; }.        
a3c80 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c    while( realval
a3c90 75 65 3e 3d 31 65 38 20 26 26 20 65 78 70 3c 3d  ue>=1e8 && exp<=
a3ca0 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65  350 ){ realvalue
a3cb0 20 2a 3d 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38   *= 1e-8; exp+=8
a3cc0 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68  ; }.          wh
a3cd0 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d  ile( realvalue>=
a3ce0 31 30 2e 30 20 26 26 20 65 78 70 3c 3d 33 35 30  10.0 && exp<=350
a3cf0 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d   ){ realvalue *=
a3d00 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20   0.1; exp++; }. 
a3d10 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
a3d20 72 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 29  realvalue<1e-8 )
a3d30 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31  { realvalue *= 1
a3d40 65 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 20  e8; exp-=8; }.  
a3d50 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72          while( r
a3d60 65 61 6c 76 61 6c 75 65 3c 31 2e 30 20 29 7b 20  ealvalue<1.0 ){ 
a3d70 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e  realvalue *= 10.
a3d80 30 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20  0; exp--; }.    
a3d90 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 33 35        if( exp>35
a3da0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
a3db0 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2d 27   if( prefix=='-'
a3dc0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a3dd0 20 20 62 75 66 70 74 20 3d 20 22 2d 49 6e 66 22    bufpt = "-Inf"
a3de0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65  ;.            }e
a3df0 6c 73 65 20 69 66 28 20 70 72 65 66 69 78 3d 3d  lse if( prefix==
a3e00 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  '+' ){.         
a3e10 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 2b 49       bufpt = "+I
a3e20 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nf";.           
a3e30 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a3e40 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 49        bufpt = "I
a3e50 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nf";.           
a3e60 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c   }.            l
a3e70 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53  ength = sqlite3S
a3e80 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a  trlen30(bufpt);.
a3e90 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
a3ea0 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
a3eb0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a3ec0 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20   bufpt = buf;.  
a3ed0 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20        /*.       
a3ee0 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c 64   ** If the field
a3ef0 20 74 79 70 65 20 69 73 20 65 74 47 45 4e 45 52   type is etGENER
a3f00 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72 74  IC, then convert
a3f10 20 74 6f 20 65 69 74 68 65 72 20 65 74 45 58 50   to either etEXP
a3f20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 65  .        ** or e
a3f30 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72 6f  tFLOAT, as appro
a3f40 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20 20  priate..        
a3f50 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  */.        flag_
a3f60 65 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74 45  exp = xtype==etE
a3f70 58 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  XP;.        if( 
a3f80 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 29  xtype!=etFLOAT )
a3f90 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c  {.          real
a3fa0 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72  value += rounder
a3fb0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
a3fc0 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20  realvalue>=10.0 
a3fd0 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
a3fe0 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20  0.1; exp++; }.  
a3ff0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a4000 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e  if( xtype==etGEN
a4010 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20  ERIC ){.        
a4020 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c    flag_rtz = !fl
a4030 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d  ag_alternateform
a4040 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
a4050 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72  exp<-4 || exp>pr
a4060 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20  ecision ){.     
a4070 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65         xtype = e
a4080 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 20  tEXP;.          
a4090 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a40a0 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70     precision = p
a40b0 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a  recision - exp;.
a40c0 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70              xtyp
a40d0 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20  e = etFLOAT;.   
a40e0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a40f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a4100 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b 0a    flag_rtz = 0;.
a4110 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a4120 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 45    if( xtype==etE
a4130 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  XP ){.          
a4140 65 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  e2 = 0;.        
a4150 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a4160 20 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20 20   e2 = exp;.     
a4170 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73 64     }.        nsd
a4180 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6c   = 0;.        fl
a4190 61 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73 69  ag_dp = (precisi
a41a0 6f 6e 3e 30 20 3f 31 3a 30 29 20 7c 20 66 6c 61  on>0 ?1:0) | fla
a41b0 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20  g_alternateform 
a41c0 7c 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b  | flag_altform2;
a41d0 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
a41e0 73 69 67 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66  sign in front of
a41f0 20 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20   the number */. 
a4200 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69         if( prefi
a4210 78 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  x ){.          *
a4220 28 62 75 66 70 74 2b 2b 29 20 3d 20 70 72 65 66  (bufpt++) = pref
a4230 69 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ix;.        }.  
a4240 20 20 20 20 20 20 2f 2a 20 44 69 67 69 74 73 20        /* Digits 
a4250 70 72 69 6f 72 20 74 6f 20 74 68 65 20 64 65 63  prior to the dec
a4260 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  imal point */.  
a4270 20 20 20 20 20 20 69 66 28 20 65 32 3c 30 20 29        if( e2<0 )
a4280 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75  {.          *(bu
a4290 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20  fpt++) = '0';.  
a42a0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a42b0 20 20 20 20 20 20 20 66 6f 72 28 3b 20 65 32 3e         for(; e2>
a42c0 3d 30 3b 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20  =0; e2--){.     
a42d0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
a42e0 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69 74 28  ) = et_getdigit(
a42f0 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29  &realvalue,&nsd)
a4300 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
a4310 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a4320 2f 2a 20 54 68 65 20 64 65 63 69 6d 61 6c 20 70  /* The decimal p
a4330 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20  oint */.        
a4340 69 66 28 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20  if( flag_dp ){. 
a4350 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
a4360 2b 2b 29 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20  ++) = '.';.     
a4370 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
a4380 22 30 22 20 64 69 67 69 74 73 20 61 66 74 65 72  "0" digits after
a4390 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69   the decimal poi
a43a0 6e 74 20 62 75 74 20 62 65 66 6f 72 65 20 74 68  nt but before th
a43b0 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 20 20  e first.        
a43c0 2a 2a 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64  ** significant d
a43d0 69 67 69 74 20 6f 66 20 74 68 65 20 6e 75 6d 62  igit of the numb
a43e0 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f  er */.        fo
a43f0 72 28 65 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72  r(e2++; e2<0; pr
a4400 65 63 69 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29  ecision--, e2++)
a4410 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
a4420 72 74 28 20 70 72 65 63 69 73 69 6f 6e 3e 30 20  rt( precision>0 
a4430 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62  );.          *(b
a4440 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20  ufpt++) = '0';. 
a4450 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a4460 20 2f 2a 20 53 69 67 6e 69 66 69 63 61 6e 74 20   /* Significant 
a4470 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65  digits after the
a4480 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a   decimal point *
a4490 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  /.        while(
a44a0 20 28 70 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30   (precision--)>0
a44b0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28   ){.          *(
a44c0 62 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65  bufpt++) = et_ge
a44d0 74 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75  tdigit(&realvalu
a44e0 65 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20  e,&nsd);.       
a44f0 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65   }.        /* Re
a4500 6d 6f 76 65 20 74 72 61 69 6c 69 6e 67 20 7a 65  move trailing ze
a4510 72 6f 73 20 61 6e 64 20 74 68 65 20 22 2e 22 20  ros and the "." 
a4520 69 66 20 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c  if no digits fol
a4530 6c 6f 77 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20  low the "." */. 
a4540 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
a4550 72 74 7a 20 26 26 20 66 6c 61 67 5f 64 70 20 29  rtz && flag_dp )
a4560 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  {.          whil
a4570 65 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30  e( bufpt[-1]=='0
a4580 27 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d  ' ) *(--bufpt) =
a4590 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   0;.          as
a45a0 73 65 72 74 28 20 62 75 66 70 74 3e 62 75 66 20  sert( bufpt>buf 
a45b0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
a45c0 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20   bufpt[-1]=='.' 
a45d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
a45e0 66 28 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32  f( flag_altform2
a45f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a4600 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27    *(bufpt++) = '
a4610 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0';.            
a4620 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a4630 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20       *(--bufpt) 
a4640 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
a4650 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
a4660 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a4670 20 2f 2a 20 41 64 64 20 74 68 65 20 22 65 4e 4e   /* Add the "eNN
a4680 4e 22 20 73 75 66 66 69 78 20 2a 2f 0a 20 20 20  N" suffix */.   
a4690 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 65 78       if( flag_ex
a46a0 70 20 7c 7c 20 78 74 79 70 65 3d 3d 65 74 45 58  p || xtype==etEX
a46b0 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  P ){.          *
a46c0 28 62 75 66 70 74 2b 2b 29 20 3d 20 61 44 69 67  (bufpt++) = aDig
a46d0 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73  its[infop->chars
a46e0 65 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69  et];.          i
a46f0 66 28 20 65 78 70 3c 30 20 29 7b 0a 20 20 20 20  f( exp<0 ){.    
a4700 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
a4710 2b 29 20 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20  +) = '-'; exp = 
a4720 2d 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20  -exp;.          
a4730 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a4740 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
a4750 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  '+';.          }
a4760 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65  .          if( e
a4770 78 70 3e 3d 31 30 30 20 29 7b 0a 20 20 20 20 20  xp>=100 ){.     
a4780 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
a4790 29 20 3d 20 28 63 68 61 72 29 28 28 65 78 70 2f  ) = (char)((exp/
a47a0 31 30 30 29 2b 27 30 27 29 3b 20 20 20 20 20 20  100)+'0');      
a47b0 20 20 2f 2a 20 31 30 30 27 73 20 64 69 67 69 74    /* 100's digit
a47c0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
a47d0 65 78 70 20 25 3d 20 31 30 30 3b 0a 20 20 20 20  exp %= 100;.    
a47e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a47f0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28    *(bufpt++) = (
a4800 63 68 61 72 29 28 65 78 70 2f 31 30 2b 27 30 27  char)(exp/10+'0'
a4810 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  );             /
a4820 2a 20 31 30 27 73 20 64 69 67 69 74 20 2a 2f 0a  * 10's digit */.
a4830 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
a4840 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 65 78  t++) = (char)(ex
a4850 70 25 31 30 2b 27 30 27 29 3b 20 20 20 20 20 20  p%10+'0');      
a4860 20 20 20 20 20 20 20 2f 2a 20 31 27 73 20 64 69         /* 1's di
a4870 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d  git */.        }
a4880 0a 20 20 20 20 20 20 20 20 2a 62 75 66 70 74 20  .        *bufpt 
a4890 3d 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  = 0;..        /*
a48a0 20 54 68 65 20 63 6f 6e 76 65 72 74 65 64 20 6e   The converted n
a48b0 75 6d 62 65 72 20 69 73 20 69 6e 20 62 75 66 5b  umber is in buf[
a48c0 5d 20 61 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69  ] and zero termi
a48d0 6e 61 74 65 64 2e 20 4f 75 74 70 75 74 20 69 74  nated. Output it
a48e0 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74  ..        ** Not
a48f0 65 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65  e that the numbe
a4900 72 20 69 73 20 69 6e 20 74 68 65 20 75 73 75 61  r is in the usua
a4910 6c 20 6f 72 64 65 72 2c 20 6e 6f 74 20 72 65 76  l order, not rev
a4920 65 72 73 65 64 20 61 73 20 77 69 74 68 0a 20 20  ersed as with.  
a4930 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72        ** integer
a4940 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f   conversions. */
a4950 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
a4960 3d 20 28 69 6e 74 29 28 62 75 66 70 74 2d 62 75  = (int)(bufpt-bu
a4970 66 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70  f);.        bufp
a4980 74 20 3d 20 62 75 66 3b 0a 0a 20 20 20 20 20 20  t = buf;..      
a4990 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
a49a0 65 3a 20 20 41 64 64 20 6c 65 61 64 69 6e 67 20  e:  Add leading 
a49b0 7a 65 72 6f 73 20 69 66 20 74 68 65 20 66 6c 61  zeros if the fla
a49c0 67 5f 7a 65 72 6f 70 61 64 20 66 6c 61 67 20 69  g_zeropad flag i
a49d0 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74  s.        ** set
a49e0 20 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20   and we are not 
a49f0 6c 65 66 74 20 6a 75 73 74 69 66 69 65 64 20 2a  left justified *
a4a00 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c  /.        if( fl
a4a10 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 21 66  ag_zeropad && !f
a4a20 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20  lag_leftjustify 
a4a30 26 26 20 6c 65 6e 67 74 68 20 3c 20 77 69 64 74  && length < widt
a4a40 68 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  h){.          in
a4a50 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69  t i;.          i
a4a60 6e 74 20 6e 50 61 64 20 3d 20 77 69 64 74 68 20  nt nPad = width 
a4a70 2d 20 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20  - length;.      
a4a80 20 20 20 20 66 6f 72 28 69 3d 77 69 64 74 68 3b      for(i=width;
a4a90 20 69 3e 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a   i>=nPad; i--){.
a4aa0 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70              bufp
a4ab0 74 5b 69 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e  t[i] = bufpt[i-n
a4ac0 50 61 64 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  Pad];.          
a4ad0 7d 0a 20 20 20 20 20 20 20 20 20 20 69 20 3d 20  }.          i = 
a4ae0 70 72 65 66 69 78 21 3d 30 3b 0a 20 20 20 20 20  prefix!=0;.     
a4af0 20 20 20 20 20 77 68 69 6c 65 28 20 6e 50 61 64       while( nPad
a4b00 2d 2d 20 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20  -- ) bufpt[i++] 
a4b10 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20  = '0';.         
a4b20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 3b   length = width;
a4b30 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  .        }.#endi
a4b40 66 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  f.        break;
a4b50 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 49  .      case etSI
a4b60 5a 45 3a 0a 20 20 20 20 20 20 20 20 2a 28 76 61  ZE:.        *(va
a4b70 5f 61 72 67 28 61 70 2c 69 6e 74 2a 29 29 20 3d  _arg(ap,int*)) =
a4b80 20 70 41 63 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a   pAccum->nChar;.
a4b90 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a4ba0 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20   width = 0;.    
a4bb0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
a4bc0 20 63 61 73 65 20 65 74 50 45 52 43 45 4e 54 3a   case etPERCENT:
a4bd0 0a 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20  .        buf[0] 
a4be0 3d 20 27 25 27 3b 0a 20 20 20 20 20 20 20 20 62  = '%';.        b
a4bf0 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20  ufpt = buf;.    
a4c00 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a      length = 1;.
a4c10 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
a4c20 20 20 20 20 20 63 61 73 65 20 65 74 43 48 41 52       case etCHAR
a4c30 58 3a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 76  X:.        c = v
a4c40 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20  a_arg(ap,int);. 
a4c50 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20         buf[0] = 
a4c60 28 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20  (char)c;.       
a4c70 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d   if( precision>=
a4c80 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  0 ){.          f
a4c90 6f 72 28 69 64 78 3d 31 3b 20 69 64 78 3c 70 72  or(idx=1; idx<pr
a4ca0 65 63 69 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20  ecision; idx++) 
a4cb0 62 75 66 5b 69 64 78 5d 20 3d 20 28 63 68 61 72  buf[idx] = (char
a4cc0 29 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65  )c;.          le
a4cd0 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e  ngth = precision
a4ce0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
a4cf0 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74  .          lengt
a4d00 68 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a  h =1;.        }.
a4d10 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
a4d20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65  buf;.        bre
a4d30 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65  ak;.      case e
a4d40 74 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63  tSTRING:.      c
a4d50 61 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a  ase etDYNSTRING:
a4d60 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d  .        bufpt =
a4d70 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a   va_arg(ap,char*
a4d80 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62  );.        if( b
a4d90 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ufpt==0 ){.     
a4da0 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b       bufpt = "";
a4db0 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
a4dc0 66 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53  f( xtype==etDYNS
a4dd0 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20  TRING ){.       
a4de0 20 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70     zExtra = bufp
a4df0 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
a4e00 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69       if( precisi
a4e10 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  on>=0 ){.       
a4e20 20 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b     for(length=0;
a4e30 20 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f   length<precisio
a4e40 6e 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74  n && bufpt[lengt
a4e50 68 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a  h]; length++){}.
a4e60 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
a4e70 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20           length 
a4e80 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
a4e90 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 20  0(bufpt);.      
a4ea0 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
a4eb0 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  k;.      case et
a4ec0 53 51 4c 45 53 43 41 50 45 3a 0a 20 20 20 20 20  SQLESCAPE:.     
a4ed0 20 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50   case etSQLESCAP
a4ee0 45 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65  E2:.      case e
a4ef0 74 53 51 4c 45 53 43 41 50 45 33 3a 20 7b 0a 20  tSQLESCAPE3: {. 
a4f00 20 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c         int i, j,
a4f10 20 6e 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20   n, isnull;.    
a4f20 20 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74      int needQuot
a4f30 65 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  e;.        char 
a4f40 63 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  ch;.        char
a4f50 20 71 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74   q = ((xtype==et
a4f60 53 51 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a  SQLESCAPE3)?'"':
a4f70 27 5c 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74  '\'');   /* Quot
a4f80 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20  e character */. 
a4f90 20 20 20 20 20 20 20 63 68 61 72 20 2a 65 73 63         char *esc
a4fa0 61 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  arg = va_arg(ap,
a4fb0 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20  char*);.        
a4fc0 69 73 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d  isnull = escarg=
a4fd0 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  =0;.        if( 
a4fe0 69 73 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20  isnull ) escarg 
a4ff0 3d 20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45  = (xtype==etSQLE
a5000 53 43 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20  SCAPE2 ? "NULL" 
a5010 3a 20 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20  : "(NULL)");.   
a5020 20 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20       for(i=n=0; 
a5030 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d  (ch=escarg[i])!=
a5040 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  0; i++){.       
a5050 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20     if( ch==q )  
a5060 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n++;.        }. 
a5070 20 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65         needQuote
a5080 20 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74   = !isnull && xt
a5090 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45  ype==etSQLESCAPE
a50a0 32 3b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  2;.        n += 
a50b0 69 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74  i + 1 + needQuot
a50c0 65 2a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28  e*2;.        if(
a50d0 20 6e 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a   n>etBUFSIZE ){.
a50e0 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20            bufpt 
a50f0 3d 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74  = zExtra = sqlit
a5100 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20  e3Malloc( n );. 
a5110 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66           if( buf
a5120 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  pt==0 ){.       
a5130 20 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 6c       pAccum->mal
a5140 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
a5150 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
a5160 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  n;.          }. 
a5170 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a5180 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
a5190 62 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  buf;.        }. 
a51a0 20 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20         j = 0;.  
a51b0 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75        if( needQu
a51c0 6f 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d  ote ) bufpt[j++]
a51d0 20 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 66 6f   = q;.        fo
a51e0 72 28 69 3d 30 3b 20 28 63 68 3d 65 73 63 61 72  r(i=0; (ch=escar
a51f0 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a  g[i])!=0; i++){.
a5200 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b            bufpt[
a5210 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20  j++] = ch;.     
a5220 20 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29       if( ch==q )
a5230 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68   bufpt[j++] = ch
a5240 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a5250 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74      if( needQuot
a5260 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d  e ) bufpt[j++] =
a5270 20 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70   q;.        bufp
a5280 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  t[j] = 0;.      
a5290 20 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 20    length = j;.  
a52a0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65        /* The pre
a52b0 63 69 73 69 6f 6e 20 69 73 20 69 67 6e 6f 72 65  cision is ignore
a52c0 64 20 6f 6e 20 25 71 20 61 6e 64 20 25 51 20 2a  d on %q and %Q *
a52d0 2f 0a 20 20 20 20 20 20 20 20 2f 2a 20 69 66 28  /.        /* if(
a52e0 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26   precision>=0 &&
a52f0 20 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74   precision<lengt
a5300 68 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65  h ) length = pre
a5310 63 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20  cision; */.     
a5320 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a5330 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54  }.      case etT
a5340 4f 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20  OKEN: {.        
a5350 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20  Token *pToken = 
a5360 76 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e  va_arg(ap, Token
a5370 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  *);.        if( 
a5380 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20  pToken ){.      
a5390 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
a53a0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
a53b0 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  , (const char*)p
a53c0 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e  Token->z, pToken
a53d0 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ->n);.        }.
a53e0 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a53f0 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20   width = 0;.    
a5400 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
a5410 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74   }.      case et
a5420 53 52 43 4c 49 53 54 3a 20 7b 0a 20 20 20 20 20  SRCLIST: {.     
a5430 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
a5440 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 53 72   = va_arg(ap, Sr
a5450 63 4c 69 73 74 2a 29 3b 0a 20 20 20 20 20 20 20  cList*);.       
a5460 20 69 6e 74 20 6b 20 3d 20 76 61 5f 61 72 67 28   int k = va_arg(
a5470 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
a5480 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
a5490 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26  _item *pItem = &
a54a0 70 53 72 63 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20  pSrc->a[k];.    
a54b0 20 20 20 20 61 73 73 65 72 74 28 20 6b 3e 3d 30      assert( k>=0
a54c0 20 26 26 20 6b 3c 70 53 72 63 2d 3e 6e 53 72 63   && k<pSrc->nSrc
a54d0 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
a54e0 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  pItem->zDatabase
a54f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
a5500 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
a5510 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65  end(pAccum, pIte
a5520 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31  m->zDatabase, -1
a5530 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
a5540 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
a5550 6e 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20  nd(pAccum, ".", 
a5560 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  1);.        }.  
a5570 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
a5580 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63  AccumAppend(pAcc
a5590 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  um, pItem->zName
a55a0 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c  , -1);.        l
a55b0 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20  ength = width = 
a55c0 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  0;.        break
a55d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
a55e0 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
a55f0 20 20 20 61 73 73 65 72 74 28 20 78 74 79 70 65     assert( xtype
a5600 3d 3d 65 74 49 4e 56 41 4c 49 44 20 29 3b 0a 20  ==etINVALID );. 
a5610 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
a5620 20 20 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45       }.    }/* E
a5630 6e 64 20 73 77 69 74 63 68 20 6f 76 65 72 20 74  nd switch over t
a5640 68 65 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a  he format type *
a5650 2f 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  /.    /*.    ** 
a5660 54 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  The text of the 
a5670 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f  conversion is po
a5680 69 6e 74 65 64 20 74 6f 20 62 79 20 22 62 75 66  inted to by "buf
a5690 70 74 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a  pt" and is.    *
a56a0 2a 20 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61  * "length" chara
a56b0 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65  cters long.  The
a56c0 20 66 69 65 6c 64 20 77 69 64 74 68 20 69 73 20   field width is 
a56d0 22 77 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20  "width".  Do.   
a56e0 20 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a   ** the output..
a56f0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
a5700 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
a5710 20 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74   ){.      regist
a5720 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20  er int nspace;. 
a5730 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69       nspace = wi
a5740 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20  dth-length;.    
a5750 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29    if( nspace>0 )
a5760 7b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64  {.        append
a5770 53 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73  Space(pAccum, ns
a5780 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  pace);.      }. 
a5790 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e     }.    if( len
a57a0 67 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73  gth>0 ){.      s
a57b0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
a57c0 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66  pend(pAccum, buf
a57d0 70 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20  pt, length);.   
a57e0 20 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f   }.    if( flag_
a57f0 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20  leftjustify ){. 
a5800 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e       register in
a5810 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20  t nspace;.      
a5820 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c  nspace = width-l
a5830 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28  ength;.      if(
a5840 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20   nspace>0 ){.   
a5850 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65       appendSpace
a5860 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29  (pAccum, nspace)
a5870 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
a5880 20 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29      if( zExtra )
a5890 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
a58a0 66 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20  free(zExtra);.  
a58b0 20 20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f    }.  }/* End fo
a58c0 72 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20  r loop over the 
a58d0 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f  format string */
a58e0 0a 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e  .} /* End of fun
a58f0 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ction */../*.** 
a5900 41 70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f  Append N bytes o
a5910 66 20 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f  f text from z to
a5920 20 74 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62   the StrAccum ob
a5930 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ject..*/.SQLITE_
a5940 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a5950 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
a5960 6e 64 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20  nd(StrAccum *p, 
a5970 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
a5980 6e 74 20 4e 29 7b 0a 20 20 61 73 73 65 72 74 28  nt N){.  assert(
a5990 20 7a 21 3d 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b   z!=0 || N==0 );
a59a0 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67  .  if( p->tooBig
a59b0 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   | p->mallocFail
a59c0 65 64 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61  ed ){.    testca
a59d0 73 65 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20  se(p->tooBig);. 
a59e0 20 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d     testcase(p->m
a59f0 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20  allocFailed);.  
a5a00 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
a5a10 69 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 4e  if( N<0 ){.    N
a5a20 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
a5a30 33 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28  30(z);.  }.  if(
a5a40 20 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a   N==0 || NEVER(z
a5a50 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ==0) ){.    retu
a5a60 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  rn;.  }.  if( p-
a5a70 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e  >nChar+N >= p->n
a5a80 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61  Alloc ){.    cha
a5a90 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 28  r *zNew;.    if(
a5aa0 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 29   !p->useMalloc )
a5ab0 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69  {.      p->tooBi
a5ac0 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 3d  g = 1;.      N =
a5ad0 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e   p->nAlloc - p->
a5ae0 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 20  nChar - 1;.     
a5af0 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 20   if( N<=0 ){.   
a5b00 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
a5b10 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
a5b20 20 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 20        i64 szNew 
a5b30 3d 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20  = p->nChar;.    
a5b40 20 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 31    szNew += N + 1
a5b50 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 65  ;.      if( szNe
a5b60 77 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29  w > p->mxAlloc )
a5b70 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
a5b80 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70  3StrAccumReset(p
a5b90 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 6f  );.        p->to
a5ba0 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  oBig = 1;.      
a5bb0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
a5bc0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
a5bd0 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 6e 74 29  ->nAlloc = (int)
a5be0 73 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20  szNew;.      }. 
a5bf0 20 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69       zNew = sqli
a5c00 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
a5c10 2d 3e 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20  ->db, p->nAlloc 
a5c20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65  );.      if( zNe
a5c30 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  w ){.        mem
a5c40 63 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65  cpy(zNew, p->zTe
a5c50 78 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20  xt, p->nChar);. 
a5c60 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
a5c70 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a  rAccumReset(p);.
a5c80 20 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74          p->zText
a5c90 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d   = zNew;.      }
a5ca0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d  else{.        p-
a5cb0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
a5cc0 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  1;.        sqlit
a5cd0 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28  e3StrAccumReset(
a5ce0 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  p);.        retu
a5cf0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
a5d00 7d 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26  }.  }.  memcpy(&
a5d10 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61  p->zText[p->nCha
a5d20 72 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e  r], z, N);.  p->
a5d30 6e 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f  nChar += N;.}../
a5d40 2a 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20  *.** Finish off 
a5d50 61 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69  a string by maki
a5d60 6e 67 20 73 75 72 65 20 69 74 20 69 73 20 7a 65  ng sure it is ze
a5d70 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a  ro-terminated..*
a5d80 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
a5d90 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74  er to the result
a5da0 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74  ing string.  Ret
a5db0 75 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f  urn a NULL.** po
a5dc0 69 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e  inter if any kin
a5dd0 64 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65  d of error was e
a5de0 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53  ncountered..*/.S
a5df0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
a5e00 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63  ar *sqlite3StrAc
a5e10 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63  cumFinish(StrAcc
a5e20 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  um *p){.  if( p-
a5e30 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d  >zText ){.    p-
a5e40 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d  >zText[p->nChar]
a5e50 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d   = 0;.    if( p-
a5e60 3e 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d  >useMalloc && p-
a5e70 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65  >zText==p->zBase
a5e80 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65   ){.      p->zTe
a5e90 78 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  xt = sqlite3DbMa
a5ea0 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70  llocRaw(p->db, p
a5eb0 2d 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20  ->nChar+1 );.   
a5ec0 20 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20     if( p->zText 
a5ed0 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ){.        memcp
a5ee0 79 28 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a  y(p->zText, p->z
a5ef0 42 61 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31  Base, p->nChar+1
a5f00 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
a5f10 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f          p->mallo
a5f20 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
a5f30 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
a5f40 20 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74   return p->zText
a5f50 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74  ;.}../*.** Reset
a5f60 20 61 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72   an StrAccum str
a5f70 69 6e 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c  ing.  Reclaim al
a5f80 6c 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72  l malloced memor
a5f90 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
a5fa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a5fb0 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53  3StrAccumReset(S
a5fc0 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69  trAccum *p){.  i
a5fd0 66 28 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e  f( p->zText!=p->
a5fe0 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c  zBase ){.    sql
a5ff0 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62  ite3DbFree(p->db
a6000 2c 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d  , p->zText);.  }
a6010 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b  .  p->zText = 0;
a6020 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
a6030 6c 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63  lize a string ac
a6040 63 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c  cumulator.*/.SQL
a6050 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a6060 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a6070 49 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70  Init(StrAccum *p
a6080 2c 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69  , char *zBase, i
a6090 6e 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20  nt n, int mx){. 
a60a0 20 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a   p->zText = p->z
a60b0 42 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20  Base = zBase;.  
a60c0 70 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e  p->db = 0;.  p->
a60d0 6e 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e  nChar = 0;.  p->
a60e0 6e 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d  nAlloc = n;.  p-
a60f0 3e 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20  >mxAlloc = mx;. 
a6100 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20   p->useMalloc = 
a6110 31 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d  1;.  p->tooBig =
a6120 20 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46   0;.  p->mallocF
a6130 61 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  ailed = 0;.}../*
a6140 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d  .** Print into m
a6150 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
a6160 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
a6170 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74  ().  Use the int
a6180 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65  ernal.** %-conve
a6190 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73  rsion extensions
a61a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
a61b0 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
a61c0 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65  3VMPrintf(sqlite
a61d0 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
a61e0 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c  r *zFormat, va_l
a61f0 69 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20  ist ap){.  char 
a6200 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65  *z;.  char zBase
a6210 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55  [SQLITE_PRINT_BU
a6220 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63  F_SIZE];.  StrAc
a6230 63 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 72  cum acc;.  asser
a6240 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 71  t( db!=0 );.  sq
a6250 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
a6260 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73  t(&acc, zBase, s
a6270 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 20  izeof(zBase),.  
a6280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6290 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53      db->aLimit[S
a62a0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
a62b0 54 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 3d  TH]);.  acc.db =
a62c0 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 58   db;.  sqlite3VX
a62d0 50 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c 20  Printf(&acc, 1, 
a62e0 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
a62f0 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63  z = sqlite3StrAc
a6300 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b  cumFinish(&acc);
a6310 0a 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c 6f  .  if( acc.mallo
a6320 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 64  cFailed ){.    d
a6330 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
a6340 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 1;.  }.  retur
a6350 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  n z;.}../*.** Pr
a6360 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20  int into memory 
a6370 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
a6380 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55  liteMalloc().  U
a6390 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a  se the internal.
a63a0 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20  ** %-conversion 
a63b0 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53  extensions..*/.S
a63c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
a63d0 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e  ar *sqlite3MPrin
a63e0 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  tf(sqlite3 *db, 
a63f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
a6400 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
a6410 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20  list ap;.  char 
a6420 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61  *z;.  va_start(a
a6430 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a  p, zFormat);.  z
a6440 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e   = sqlite3VMPrin
a6450 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20  tf(db, zFormat, 
a6460 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
a6470 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
a6480 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c  ../*.** Like sql
a6490 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62  ite3MPrintf(), b
a64a0 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44  ut call sqlite3D
a64b0 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20  bFree() on zStr 
a64c0 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67  after formatting
a64d0 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61  .** the string a
a64e0 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  nd before return
a64f0 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74  ning.  This rout
a6500 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ine is intended 
a6510 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f  to be used.** to
a6520 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74   modify an exist
a6530 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72  ing string.  For
a6540 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20   example:.**.** 
a6550 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65        x = sqlite
a6560 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c 20  3MPrintf(db, x, 
a6570 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66 69  "prefix %s suffi
a6580 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51  x", x);.**.*/.SQ
a6590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
a65a0 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e  r *sqlite3MAppen
a65b0 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  df(sqlite3 *db, 
a65c0 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e 73  char *zStr, cons
a65d0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
a65e0 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
a65f0 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a   ap;.  char *z;.
a6600 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
a6610 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73  Format);.  z = s
a6620 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64  qlite3VMPrintf(d
a6630 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  b, zFormat, ap);
a6640 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
a6650 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
a6660 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 75  b, zStr);.  retu
a6670 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn z;.}../*.** P
a6680 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
a6690 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
a66a0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
a66b0 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72    Omit the inter
a66c0 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73  nal.** %-convers
a66d0 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a  ion extensions..
a66e0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  */.SQLITE_API ch
a66f0 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72  ar *sqlite3_vmpr
a6700 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20  intf(const char 
a6710 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73  *zFormat, va_lis
a6720 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a  t ap){.  char *z
a6730 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53  ;.  char zBase[S
a6740 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f  QLITE_PRINT_BUF_
a6750 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75  SIZE];.  StrAccu
a6760 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 53  m acc;.#ifndef S
a6770 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
a6780 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  NIT.  if( sqlite
a6790 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29  3_initialize() )
a67a0 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
a67b0 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63  f.  sqlite3StrAc
a67c0 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42  cumInit(&acc, zB
a67d0 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73  ase, sizeof(zBas
a67e0 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  e), SQLITE_MAX_L
a67f0 45 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 65  ENGTH);.  sqlite
a6800 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20  3VXPrintf(&acc, 
a6810 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  0, zFormat, ap);
a6820 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74  .  z = sqlite3St
a6830 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63  rAccumFinish(&ac
a6840 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  c);.  return z;.
a6850 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69  }../*.** Print i
a6860 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
a6870 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
a6880 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 6d  _malloc()().  Om
a6890 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a  it the internal.
a68a0 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20  ** %-conversion 
a68b0 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53  extensions..*/.S
a68c0 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
a68d0 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
a68e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
a68f0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
a6900 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20  list ap;.  char 
a6910 2a 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  *z;.#ifndef SQLI
a6920 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
a6930 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
a6940 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65  nitialize() ) re
a6950 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20  turn 0;.#endif. 
a6960 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
a6970 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71  ormat);.  z = sq
a6980 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a  lite3_vmprintf(z
a6990 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76  Format, ap);.  v
a69a0 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74  a_end(ap);.  ret
a69b0 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
a69c0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
a69d0 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e  () works like sn
a69e0 70 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 20  printf() except 
a69f0 74 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20  that it ignores 
a6a00 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c  the.** current l
a6a10 6f 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20  ocale settings. 
a6a20 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61   This is importa
a6a30 6e 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65  nt for SQLite be
a6a40 63 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20  cause we.** are 
a6a50 6e 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20  not able to use 
a6a60 61 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63  a "," as the dec
a6a70 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c  imal point in pl
a6a80 61 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a  ace of "." as.**
a6a90 20 73 70 65 63 69 66 69 65 64 20 62 79 20 73 6f   specified by so
a6aa0 6d 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53  me locales..*/.S
a6ab0 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
a6ac0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
a6ad0 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42  (int n, char *zB
a6ae0 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  uf, const char *
a6af0 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
a6b00 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c   char *z;.  va_l
a6b10 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63  ist ap;.  StrAcc
a6b20 75 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e  um acc;..  if( n
a6b30 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  <=0 ){.    retur
a6b40 6e 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71  n zBuf;.  }.  sq
a6b50 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
a6b60 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c  t(&acc, zBuf, n,
a6b70 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61   0);.  acc.useMa
a6b80 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73  lloc = 0;.  va_s
a6b90 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29  tart(ap,zFormat)
a6ba0 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69  ;.  sqlite3VXPri
a6bb0 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f  ntf(&acc, 0, zFo
a6bc0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
a6bd0 65 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 73  end(ap);.  z = s
a6be0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
a6bf0 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65  nish(&acc);.  re
a6c00 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64  turn z;.}..#if d
a6c10 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
a6c20 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72  BUG)./*.** A ver
a6c30 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29  sion of printf()
a6c40 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64   that understand
a6c50 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f  s %lld.  Used fo
a6c60 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20  r debugging..** 
a6c70 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69  The printf() bui
a6c80 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72  lt into some ver
a6c90 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73  sions of windows
a6ca0 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73   does not unders
a6cb0 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64  tand %lld.** and
a6cc0 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f   segfaults if yo
a6cd0 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67  u give it a long
a6ce0 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51   long int..*/.SQ
a6cf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a6d00 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  d sqlite3DebugPr
a6d10 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20  intf(const char 
a6d20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
a6d30 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
a6d40 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20  StrAccum acc;.  
a6d50 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 0a  char zBuf[500];.
a6d60 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
a6d70 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 66  mInit(&acc, zBuf
a6d80 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20  , sizeof(zBuf), 
a6d90 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c  0);.  acc.useMal
a6da0 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74  loc = 0;.  va_st
a6db0 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b  art(ap,zFormat);
a6dc0 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e  .  sqlite3VXPrin
a6dd0 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72  tf(&acc, 0, zFor
a6de0 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
a6df0 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65  nd(ap);.  sqlite
a6e00 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28  3StrAccumFinish(
a6e10 26 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 66  &acc);.  fprintf
a6e20 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a 42  (stdout,"%s", zB
a6e30 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74  uf);.  fflush(st
a6e40 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  dout);.}.#endif.
a6e50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a6e60 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e 63   End of printf.c
a6e70 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
a6e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a6ea0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a6eb0 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e 64   Begin file rand
a6ec0 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  om.c ***********
a6ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a6ef0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
a6f00 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
a6f10 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
a6f20 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
a6f30 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
a6f40 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
a6f50 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
a6f60 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
a6f70 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
a6f80 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
a6f90 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
a6fa0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
a6fb0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
a6fc0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
a6fd0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
a6fe0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
a6ff0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
a7000 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
a7010 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
a7020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7060 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
a7070 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
a7080 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
a7090 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20  a pseudo-random 
a70a0 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61  number.** genera
a70b0 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53  tor (PRNG) for S
a70c0 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e  QLite..**.** Ran
a70d0 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20  dom numbers are 
a70e0 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20  used by some of 
a70f0 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63  the database bac
a7100 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a  kends in order.*
a7110 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61  * to generate ra
a7120 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79  ndom integer key
a7130 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20  s for tables or 
a7140 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73  random filenames
a7150 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61 6e  ..**.** $Id: ran
a7160 64 6f 6d 2e 63 2c 76 20 31 2e 32 39 20 32 30 30  dom.c,v 1.29 200
a7170 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34  8/12/10 19:26:24
a7180 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
a7190 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 73  /* All threads s
a71a0 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 61  hare a single ra
a71b0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65  ndom number gene
a71c0 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 73  rator..** This s
a71d0 74 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20  tructure is the 
a71e0 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
a71f0 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a   the generator..
a7200 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45  */.static SQLITE
a7210 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69  _WSD struct sqli
a7220 74 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20 20  te3PrngType {.  
a7230 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 73  unsigned char is
a7240 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f  Init;          /
a7250 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 61  * True if initia
a7260 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67  lized */.  unsig
a7270 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 20  ned char i, j;  
a7280 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
a7290 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a  te variables */.
a72a0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
a72b0 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20 20  s[256];         
a72c0 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62   /* State variab
a72d0 6c 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33  les */.} sqlite3
a72e0 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  Prng;../*.** Get
a72f0 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 20   a single 8-bit 
a7300 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f  random value fro
a7310 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e 20  m the RC4 PRNG. 
a7320 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75   The Mutex.** mu
a7330 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65  st be held while
a7340 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20   executing this 
a7350 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57  routine..**.** W
a7360 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 20  hy not just use 
a7370 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f 6d  a library random
a7380 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20   generator like 
a7390 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 68  lrand48() for th
a73a0 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 74  is?.** Because t
a73b0 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f  he OP_NewRowid o
a73c0 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 42  pcode in the VDB
a73d0 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 76  E depends on hav
a73e0 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f  ing a very.** go
a73f0 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e  od source of ran
a7400 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 68  dom numbers.  Th
a7410 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 72  e lrand48() libr
a7420 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  ary function may
a7430 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64  .** well be good
a7440 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d 61   enough.  But ma
a7450 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79  ybe not.  Or may
a7460 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 73  be lrand48() has
a7470 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20   some.** subtle 
a7480 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65  problems on some
a7490 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 6f   systems that co
a74a0 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65  uld cause proble
a74b0 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 0a  ms.  It is hard.
a74c0 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20  ** to know.  To 
a74d0 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 73  minimize the ris
a74e0 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 75  k of problems du
a74f0 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 38  e to bad lrand48
a7500 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ().** implementa
a7510 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 73  tions, SQLite us
a7520 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e  es this random n
a7530 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20  umber generator 
a7540 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c  based.** on RC4,
a7550 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 77   which we know w
a7560 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a  orks very well..
a7570 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20  **.** (Later):  
a7580 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77  Actually, OP_New
a7590 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 64  Rowid does not d
a75a0 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20  epend on a good 
a75b0 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e  source of.** ran
a75c0 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65  domness any more
a75d0 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c  .  But we will l
a75e0 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 69  eave this code i
a75f0 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a  n all the same..
a7600 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61 6e  */.static u8 ran
a7610 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a 20  domByte(void){. 
a7620 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74   unsigned char t
a7630 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77 73  ;...  /* The "ws
a7640 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69 6c  dPrng" macro wil
a7650 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65  l resolve to the
a7660 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e   pseudo-random n
a7670 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 0a  umber generator.
a7680 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f    ** state vecto
a7690 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20  r.  If writable 
a76a0 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75  static data is u
a76b0 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68  nsupported on th
a76c0 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20 77  e target,.  ** w
a76d0 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65  e have to locate
a76e0 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 6f   the state vecto
a76f0 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20  r at run-time.  
a7700 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d  In the more comm
a7710 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 65  on.  ** case whe
a7720 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74  re writable stat
a7730 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f  ic data is suppo
a7740 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63 61  rted, wsdPrng ca
a7750 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79  n refer directly
a7760 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73 71  .  ** to the "sq
a7770 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 74 65  lite3Prng" state
a7780 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64   vector declared
a7790 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 66   above..  */.#if
a77a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
a77b0 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 71 6c  WSD.  struct sql
a77c0 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70 20  ite3PrngType *p 
a77d0 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74  = &GLOBAL(struct
a77e0 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
a77f0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b 0a  , sqlite3Prng);.
a7800 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67  # define wsdPrng
a7810 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65   p[0].#else.# de
a7820 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71 6c  fine wsdPrng sql
a7830 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66 0a  ite3Prng.#endif.
a7840 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
a7850 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
a7860 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  he random number
a7870 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c   generator once,
a7880 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20  .  ** the first 
a7890 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e  time this routin
a78a0 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68  e is called.  Th
a78b0 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f 65  e seed value doe
a78c0 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 20  s.  ** not need 
a78d0 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f 74  to contain a lot
a78e0 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 73   of randomness s
a78f0 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74 0a  ince we are not.
a7900 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 64    ** trying to d
a7910 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70 74  o secure encrypt
a7920 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67 20  ion or anything 
a7930 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20 2a  like that....  *
a7940 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 69  *.  ** Nothing i
a7950 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20 61  n this file or a
a7960 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e 20  nywhere else in 
a7970 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79 20  SQLite does any 
a7980 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e 63  kind of.  ** enc
a7990 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52 43  ryption.  The RC
a79a0 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 62  4 algorithm is b
a79b0 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20 50  eing used as a P
a79c0 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e 64  RNG (pseudo-rand
a79d0 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67  om.  ** number g
a79e0 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61 73  enerator) not as
a79f0 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20 64   an encryption d
a7a00 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  evice..  */.  if
a7a10 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e 69  ( !wsdPrng.isIni
a7a20 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  t ){.    int i;.
a7a30 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b      char k[256];
a7a40 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 3d  .    wsdPrng.j =
a7a50 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e   0;.    wsdPrng.
a7a60 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  i = 0;.    sqlit
a7a70 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73  e3OsRandomness(s
a7a80 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28  qlite3_vfs_find(
a7a90 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20  0), 256, k);.   
a7aa0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b   for(i=0; i<256;
a7ab0 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 64   i++){.      wsd
a7ac0 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29  Prng.s[i] = (u8)
a7ad0 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  i;.    }.    for
a7ae0 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b  (i=0; i<256; i++
a7af0 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67  ){.      wsdPrng
a7b00 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b  .j += wsdPrng.s[
a7b10 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20  i] + k[i];.     
a7b20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77   t = wsdPrng.s[w
a7b30 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20  sdPrng.j];.     
a7b40 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
a7b50 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e  ng.j] = wsdPrng.
a7b60 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50  s[i];.      wsdP
a7b70 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20  rng.s[i] = t;.  
a7b80 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e    }.    wsdPrng.
a7b90 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a  isInit = 1;.  }.
a7ba0 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
a7bb0 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65  nd return single
a7bc0 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a   random byte.  *
a7bd0 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b  /.  wsdPrng.i++;
a7be0 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73  .  t = wsdPrng.s
a7bf0 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77  [wsdPrng.i];.  w
a7c00 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20  sdPrng.j += t;. 
a7c10 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
a7c20 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e  ng.i] = wsdPrng.
a7c30 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20  s[wsdPrng.j];.  
a7c40 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e  wsdPrng.s[wsdPrn
a7c50 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d  g.j] = t;.  t +=
a7c60 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
a7c70 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20  ng.i];.  return 
a7c80 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a  wsdPrng.s[t];.}.
a7c90 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20  ./*.** Return N 
a7ca0 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f  random bytes..*/
a7cb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
a7cc0 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
a7cd0 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20  ess(int N, void 
a7ce0 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e  *pBuf){.  unsign
a7cf0 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20  ed char *zBuf = 
a7d00 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45  pBuf;.#if SQLITE
a7d10 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71  _THREADSAFE.  sq
a7d20 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
a7d30 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
a7d40 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
a7d50 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29  TEX_STATIC_PRNG)
a7d60 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ;.#endif.  sqlit
a7d70 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
a7d80 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20  utex);.  while( 
a7d90 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75  N-- ){.    *(zBu
a7da0 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74  f++) = randomByt
a7db0 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  e();.  }.  sqlit
a7dc0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
a7dd0 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  utex);.}..#ifnde
a7de0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
a7df0 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  ILTIN_TEST./*.**
a7e00 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72   For testing pur
a7e10 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69  poses, we someti
a7e20 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73  mes want to pres
a7e30 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20 6f  erve the state o
a7e40 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65  f.** PRNG and re
a7e50 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74  store the PRNG t
a7e60 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61 74  o its saved stat
a7e70 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d  e at a later tim
a7e80 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65  e, or.** to rese
a7e90 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74  t the PRNG to it
a7ea0 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e  s initial state.
a7eb0 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
a7ec0 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74   accomplish.** t
a7ed0 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a  hose tasks..**.*
a7ee0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65  * The sqlite3_te
a7ef0 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74  st_control() int
a7f00 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65  erface calls the
a7f10 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a  se routines to.*
a7f20 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52  * control the PR
a7f30 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51  NG..*/.static SQ
a7f40 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
a7f50 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20  sqlite3PrngType 
a7f60 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67  sqlite3SavedPrng
a7f70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
a7f80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e   void sqlite3Prn
a7f90 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29  gSaveState(void)
a7fa0 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20  {.  memcpy(.    
a7fb0 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73  &GLOBAL(struct s
a7fc0 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20  qlite3PrngType, 
a7fd0 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67  sqlite3SavedPrng
a7fe0 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73  ),.    &GLOBAL(s
a7ff0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
a8000 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72  gType, sqlite3Pr
a8010 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28  ng),.    sizeof(
a8020 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29  sqlite3Prng).  )
a8030 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
a8040 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
a8050 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28  rngRestoreState(
a8060 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28  void){.  memcpy(
a8070 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72  .    &GLOBAL(str
a8080 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54  uct sqlite3PrngT
a8090 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67  ype, sqlite3Prng
a80a0 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73  ),.    &GLOBAL(s
a80b0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
a80c0 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61  gType, sqlite3Sa
a80d0 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73 69  vedPrng),.    si
a80e0 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67  zeof(sqlite3Prng
a80f0 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  ).  );.}.SQLITE_
a8100 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a8110 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61  ite3PrngResetSta
a8120 74 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f 42  te(void){.  GLOB
a8130 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65  AL(struct sqlite
a8140 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74  3PrngType, sqlit
a8150 65 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d  e3Prng).isInit =
a8160 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   0;.}.#endif /* 
a8170 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
a8180 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a  TIN_TEST */../**
a8190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
a81a0 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a   of random.c ***
a81b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a81c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a81d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
a81e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
a81f0 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a  in file utf.c **
a8200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
a8230 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 33  ** 2004 April 13
a8240 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
a8250 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
a8260 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
a8270 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
a8280 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
a8290 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
a82a0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
a82b0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
a82c0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
a82d0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
a82e0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
a82f0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
a8300 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
a8310 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
a8320 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
a8330 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
a8340 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
a8350 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
a8360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
a83a0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
a83b0 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75  tains routines u
a83c0 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65  sed to translate
a83d0 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c 20   between UTF-8, 
a83e0 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 2d  .** UTF-16, UTF-
a83f0 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 36  16BE, and UTF-16
a8400 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75  LE..**.** $Id: u
a8410 74 66 2e 63 2c 76 20 31 2e 37 33 20 32 30 30 39  tf.c,v 1.73 2009
a8420 2f 30 34 2f 30 31 20 31 38 3a 34 30 3a 33 32 20  /04/01 18:40:32 
a8430 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20  drh Exp $.**.** 
a8440 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38 3a 0a  Notes on UTF-8:.
a8450 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20 20  **.**   Byte-0  
a8460 20 20 42 79 74 65 2d 31 20 20 20 20 42 79 74 65    Byte-1    Byte
a8470 2d 32 20 20 20 20 42 79 74 65 2d 33 20 20 20 20  -2    Byte-3    
a8480 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78 78 78  Value.**  0xxxxx
a8490 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  xx              
a84a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a84b0 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30     00000000 0000
a84c0 30 30 30 30 20 30 78 78 78 78 78 78 78 0a 2a 2a  0000 0xxxxxxx.**
a84d0 20 20 31 31 30 79 79 79 79 79 20 20 31 30 78 78    110yyyyy  10xx
a84e0 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20  xxxx            
a84f0 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30             00000
a8500 30 30 30 20 30 30 30 30 30 79 79 79 20 79 79 78  000 00000yyy yyx
a8510 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 30 7a 7a  xxxxx.**  1110zz
a8520 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31 30  zz  10yyyyyy  10
a8530 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20  xxxxxx          
a8540 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a     00000000 zzzz
a8550 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a  yyyy yyxxxxxx.**
a8560 20 20 31 31 31 31 30 75 75 75 20 20 31 30 75 75    11110uuu  10uu
a8570 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20  zzzz  10yyyyyy  
a8580 31 30 78 78 78 78 78 78 20 20 20 30 30 30 75 75  10xxxxxx   000uu
a8590 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78  uuu zzzzyyyy yyx
a85a0 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e  xxxxx.**.**.** N
a85b0 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 36 3a 20  otes on UTF-16: 
a85c0 20 28 77 69 74 68 20 77 77 77 77 2b 31 3d 3d 75   (with wwww+1==u
a85d0 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20 20 20  uuuu).**.**     
a85e0 20 57 6f 72 64 2d 30 20 20 20 20 20 20 20 20 20   Word-0         
a85f0 20 20 20 20 20 20 57 6f 72 64 2d 31 20 20 20 20        Word-1    
a8600 20 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20        Value.**  
a8610 31 31 30 31 31 30 77 77 20 77 77 7a 7a 7a 7a 79  110110ww wwzzzzy
a8620 79 20 20 20 31 31 30 31 31 31 79 79 20 79 79 78  y   110111yy yyx
a8630 78 78 78 78 78 20 20 20 20 30 30 30 75 75 75 75  xxxxx    000uuuu
a8640 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78  u zzzzyyyy yyxxx
a8650 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79 79 79  xxx.**  zzzzyyyy
a8660 20 79 79 78 78 78 78 78 78 20 20 20 20 20 20 20   yyxxxxxx       
a8670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8680 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79   00000000 zzzzyy
a8690 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a  yy yyxxxxxx.**.*
a86a0 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79 74 65  *.** BOM or Byte
a86b0 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a 2a 20   Order Mark:.** 
a86c0 20 20 20 20 30 78 66 66 20 30 78 66 65 20 20 20      0xff 0xfe   
a86d0 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 75 74  little-endian ut
a86e0 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 20  f-16 follows.** 
a86f0 20 20 20 20 30 78 66 65 20 30 78 66 66 20 20 20      0xfe 0xff   
a8700 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31  big-endian utf-1
a8710 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a  6 follows.**.*/.
a8720 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a8730 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e 74 2e  Include vdbeInt.
a8740 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
a8750 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a  of utf.c *******
a8760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a8770 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a8780 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 49  Begin file vdbeI
a8790 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
a87a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a87b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a87c0 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65  /*.** 2003 Septe
a87d0 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  mber 6.**.** The
a87e0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
a87f0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
a8800 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
a8810 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
a8820 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
a8830 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
a8840 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
a8850 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
a8860 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
a8870 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
a8880 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
a8890 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
a88a0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
a88b0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
a88c0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
a88d0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
a88e0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
a88f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8930 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  *****.** This is
a8940 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65   the header file
a8950 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   for information
a8960 20 74 68 61 74 20 69 73 20 70 72 69 76 61 74 65   that is private
a8970 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e   to the.** VDBE.
a8980 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69    This informati
a8990 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 20 62  on used to all b
a89a0 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20  e at the top of 
a89b0 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 73 6f  the single.** so
a89c0 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65 20 22  urce code file "
a89d0 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e 20 74  vdbe.c".  When t
a89e0 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d 65 20  hat file became 
a89f0 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a 2a 2a  too big (over.**
a8a00 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f 6e 67   6000 lines long
a8a10 29 20 69 74 20 77 61 73 20 73 70 6c 69 74 20 75  ) it was split u
a8a20 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c 20 73  p into several s
a8a30 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 6e 64  maller files and
a8a40 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65 72 20  .** this header 
a8a50 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20  information was 
a8a60 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a 2a 2a  factored out..**
a8a70 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 49 6e 74  .** $Id: vdbeInt
a8a80 2e 68 2c 76 20 31 2e 31 37 34 20 32 30 30 39 2f  .h,v 1.174 2009/
a8a90 30 36 2f 32 33 20 31 34 3a 31 35 3a 30 34 20 64  06/23 14:15:04 d
a8aa0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  rh Exp $.*/.#ifn
a8ab0 64 65 66 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a  def _VDBEINT_H_.
a8ac0 23 64 65 66 69 6e 65 20 5f 56 44 42 45 49 4e 54  #define _VDBEINT
a8ad0 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69  _H_../*.** SQL i
a8ae0 73 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74  s translated int
a8af0 6f 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20  o a sequence of 
a8b00 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 6f 20  instructions to 
a8b10 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 62  be.** executed b
a8b20 79 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68  y a virtual mach
a8b30 69 6e 65 2e 20 20 45 61 63 68 20 69 6e 73 74 72  ine.  Each instr
a8b40 75 63 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73  uction is an ins
a8b50 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20  tance.** of the 
a8b60 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
a8b70 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ure..*/.typedef 
a8b80 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 4f 70  struct VdbeOp Op
a8b90 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e  ;../*.** Boolean
a8ba0 20 76 61 6c 75 65 73 0a 2a 2f 0a 74 79 70 65 64   values.*/.typed
a8bb0 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  ef unsigned char
a8bc0 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   Bool;../*.** A 
a8bd0 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e  cursor is a poin
a8be0 74 65 72 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c  ter into a singl
a8bf0 65 20 42 54 72 65 65 20 77 69 74 68 69 6e 20 61  e BTree within a
a8c00 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
a8c10 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 61  ** The cursor ca
a8c20 6e 20 73 65 65 6b 20 74 6f 20 61 20 42 54 72 65  n seek to a BTre
a8c30 65 20 65 6e 74 72 79 20 77 69 74 68 20 61 20 70  e entry with a p
a8c40 61 72 74 69 63 75 6c 61 72 20 6b 65 79 2c 20 6f  articular key, o
a8c50 72 0a 2a 2a 20 6c 6f 6f 70 20 6f 76 65 72 20 61  r.** loop over a
a8c60 6c 6c 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68  ll entries of th
a8c70 65 20 42 74 72 65 65 2e 20 20 59 6f 75 20 63 61  e Btree.  You ca
a8c80 6e 20 61 6c 73 6f 20 69 6e 73 65 72 74 20 6e 65  n also insert ne
a8c90 77 20 42 54 72 65 65 0a 2a 2a 20 65 6e 74 72 69  w BTree.** entri
a8ca0 65 73 20 6f 72 20 72 65 74 72 69 65 76 65 20 74  es or retrieve t
a8cb0 68 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66  he key or data f
a8cc0 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68  rom the entry th
a8cd0 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  at the cursor.**
a8ce0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
a8cf0 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a  inting to..** .*
a8d00 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 74  * Every cursor t
a8d10 68 61 74 20 74 68 65 20 76 69 72 74 75 61 6c 20  hat the virtual 
a8d20 6d 61 63 68 69 6e 65 20 68 61 73 20 6f 70 65 6e  machine has open
a8d30 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20   is represented 
a8d40 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63  by an.** instanc
a8d50 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
a8d60 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
a8d70 0a 2a 2a 20 49 66 20 74 68 65 20 56 64 62 65 43  .** If the VdbeC
a8d80 75 72 73 6f 72 2e 69 73 54 72 69 67 67 65 72 52  ursor.isTriggerR
a8d90 6f 77 20 66 6c 61 67 20 69 73 20 73 65 74 20 69  ow flag is set i
a8da0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69  t means that thi
a8db0 73 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 72  s cursor is.** r
a8dc0 65 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 72  eally a single r
a8dd0 6f 77 20 74 68 61 74 20 72 65 70 72 65 73 65 6e  ow that represen
a8de0 74 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c  ts the NEW or OL
a8df0 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f  D pseudo-table o
a8e00 66 0a 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 67  f.** a row trigg
a8e10 65 72 2e 20 20 54 68 65 20 64 61 74 61 20 66 6f  er.  The data fo
a8e20 72 20 74 68 65 20 72 6f 77 20 69 73 20 73 74 6f  r the row is sto
a8e30 72 65 64 20 69 6e 20 56 64 62 65 43 75 72 73 6f  red in VdbeCurso
a8e40 72 2e 70 44 61 74 61 20 61 6e 64 0a 2a 2a 20 74  r.pData and.** t
a8e50 68 65 20 72 6f 77 69 64 20 69 73 20 69 6e 20 56  he rowid is in V
a8e60 64 62 65 43 75 72 73 6f 72 2e 69 4b 65 79 2e 0a  dbeCursor.iKey..
a8e70 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 43 75  */.struct VdbeCu
a8e80 72 73 6f 72 20 7b 0a 20 20 42 74 43 75 72 73 6f  rsor {.  BtCurso
a8e90 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f  r *pCursor;    /
a8ea0 2a 20 54 68 65 20 63 75 72 73 6f 72 20 73 74 72  * The cursor str
a8eb0 75 63 74 75 72 65 20 6f 66 20 74 68 65 20 62 61  ucture of the ba
a8ec0 63 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69  ckend */.  int i
a8ed0 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
a8ee0 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72   /* Index of cur
a8ef0 73 6f 72 20 64 61 74 61 62 61 73 65 20 69 6e 20  sor database in 
a8f00 64 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d 31  db->aDb[] (or -1
a8f10 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52  ) */.  i64 lastR
a8f20 6f 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  owid;        /* 
a8f30 4c 61 73 74 20 72 6f 77 69 64 20 66 72 6f 6d 20  Last rowid from 
a8f40 61 20 4e 65 78 74 20 6f 72 20 4e 65 78 74 49 64  a Next or NextId
a8f50 78 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20  x operation */. 
a8f60 20 42 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20 20   Bool zeroed;   
a8f70 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
a8f80 66 20 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e 64  f zeroed out and
a8f90 20 72 65 61 64 79 20 66 6f 72 20 72 65 75 73 65   ready for reuse
a8fa0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69 64   */.  Bool rowid
a8fb0 49 73 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20 54  IsValid;    /* T
a8fc0 72 75 65 20 69 66 20 6c 61 73 74 52 6f 77 69 64  rue if lastRowid
a8fd0 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42   is valid */.  B
a8fe0 6f 6f 6c 20 61 74 46 69 72 73 74 3b 20 20 20 20  ool atFirst;    
a8ff0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a9000 70 6f 69 6e 74 69 6e 67 20 74 6f 20 66 69 72 73  pointing to firs
a9010 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f 6f  t entry */.  Boo
a9020 6c 20 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64  l useRandomRowid
a9030 3b 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6e  ;  /* Generate n
a9040 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  ew record number
a9050 73 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20  s semi-randomly 
a9060 2a 2f 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f  */.  Bool nullRo
a9070 77 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  w;         /* Tr
a9080 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74  ue if pointing t
a9090 6f 20 61 20 72 6f 77 20 77 69 74 68 20 6e 6f 20  o a row with no 
a90a0 64 61 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20 64  data */.  Bool d
a90b0 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3b 20 20  eferredMoveto;  
a90c0 2f 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c  /* A call to sql
a90d0 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28  ite3BtreeMoveto(
a90e0 29 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20  ) is needed */. 
a90f0 20 42 6f 6f 6c 20 69 73 54 61 62 6c 65 3b 20 20   Bool isTable;  
a9100 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
a9110 66 20 61 20 74 61 62 6c 65 20 72 65 71 75 69 72  f a table requir
a9120 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 73  ing integer keys
a9130 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 49 6e 64   */.  Bool isInd
a9140 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ex;         /* T
a9150 72 75 65 20 69 66 20 61 6e 20 69 6e 64 65 78 20  rue if an index 
a9160 63 6f 6e 74 61 69 6e 69 6e 67 20 6b 65 79 73 20  containing keys 
a9170 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a  only - no data *
a9180 2f 0a 20 20 69 36 34 20 6d 6f 76 65 74 6f 54 61  /.  i64 movetoTa
a9190 72 67 65 74 3b 20 20 20 20 20 2f 2a 20 41 72 67  rget;     /* Arg
a91a0 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 64 65 66  ument to the def
a91b0 65 72 72 65 64 20 73 71 6c 69 74 65 33 42 74 72  erred sqlite3Btr
a91c0 65 65 4d 6f 76 65 74 6f 28 29 20 2a 2f 0a 20 20  eeMoveto() */.  
a91d0 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20  Btree *pBt;     
a91e0 20 20 20 20 20 20 2f 2a 20 53 65 70 61 72 61 74        /* Separat
a91f0 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74  e file holding t
a9200 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 2a  emporary table *
a9210 2f 0a 20 20 69 6e 74 20 70 73 65 75 64 6f 54 61  /.  int pseudoTa
a9220 62 6c 65 52 65 67 3b 20 20 20 2f 2a 20 52 65 67  bleReg;   /* Reg
a9230 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 70 73  ister holding ps
a9240 65 75 64 6f 74 61 62 6c 65 20 63 6f 6e 74 65 6e  eudotable conten
a9250 74 2e 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20  t. */.  KeyInfo 
a9260 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 2f 2a  *pKeyInfo;    /*
a9270 20 49 6e 66 6f 20 61 62 6f 75 74 20 69 6e 64 65   Info about inde
a9280 78 20 6b 65 79 73 20 6e 65 65 64 65 64 20 62 79  x keys needed by
a9290 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a   index cursors *
a92a0 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20  /.  int nField; 
a92b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
a92c0 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e  ber of fields in
a92d0 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20   the header */. 
a92e0 20 69 36 34 20 73 65 71 43 6f 75 6e 74 3b 20 20   i64 seqCount;  
a92f0 20 20 20 20 20 20 20 2f 2a 20 53 65 71 75 65 6e         /* Sequen
a9300 63 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  ce counter */.  
a9310 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
a9320 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72  sor *pVtabCursor
a9330 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72  ;  /* The cursor
a9340 20 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74   for a virtual t
a9350 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  able */.  const 
a9360 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
a9370 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20  pModule;     /* 
a9380 4d 6f 64 75 6c 65 20 66 6f 72 20 63 75 72 73 6f  Module for curso
a9390 72 20 70 56 74 61 62 43 75 72 73 6f 72 20 2a 2f  r pVtabCursor */
a93a0 0a 0a 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66  ..  /* Result of
a93b0 20 6c 61 73 74 20 73 71 6c 69 74 65 33 42 74 72   last sqlite3Btr
a93c0 65 65 4d 6f 76 65 74 6f 28 29 20 64 6f 6e 65 20  eeMoveto() done 
a93d0 62 79 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73  by an OP_NotExis
a93e0 74 73 20 6f 72 20 0a 20 20 2a 2a 20 4f 50 5f 49  ts or .  ** OP_I
a93f0 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 20 6f  sUnique opcode o
a9400 6e 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 2a  n this cursor. *
a9410 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75  /.  int seekResu
a9420 6c 74 3b 0a 0a 20 20 2f 2a 20 43 61 63 68 65 64  lt;..  /* Cached
a9430 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
a9440 75 74 20 74 68 65 20 68 65 61 64 65 72 20 66 6f  ut the header fo
a9450 72 20 74 68 65 20 64 61 74 61 20 72 65 63 6f 72  r the data recor
a9460 64 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20  d that the.  ** 
a9470 63 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e  cursor is curren
a9480 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e  tly pointing to.
a9490 20 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20    Only valid if 
a94a0 63 61 63 68 65 53 74 61 74 75 73 20 6d 61 74 63  cacheStatus matc
a94b0 68 65 73 0a 20 20 2a 2a 20 56 64 62 65 2e 63 61  hes.  ** Vdbe.ca
a94c0 63 68 65 43 74 72 2e 20 20 56 64 62 65 2e 63 61  cheCtr.  Vdbe.ca
a94d0 63 68 65 43 74 72 20 77 69 6c 6c 20 6e 65 76 65  cheCtr will neve
a94e0 72 20 74 61 6b 65 20 6f 6e 20 74 68 65 20 76 61  r take on the va
a94f0 6c 75 65 20 6f 66 0a 20 20 2a 2a 20 43 41 43 48  lue of.  ** CACH
a9500 45 5f 53 54 41 4c 45 20 61 6e 64 20 73 6f 20 73  E_STALE and so s
a9510 65 74 74 69 6e 67 20 63 61 63 68 65 53 74 61 74  etting cacheStat
a9520 75 73 3d 43 41 43 48 45 5f 53 54 41 4c 45 20 67  us=CACHE_STALE g
a9530 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 20  uarantees that. 
a9540 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20 69 73   ** the cache is
a9550 20 6f 75 74 20 6f 66 20 64 61 74 65 2e 0a 20 20   out of date..  
a9560 2a 2a 0a 20 20 2a 2a 20 61 52 6f 77 20 6d 69 67  **.  ** aRow mig
a9570 68 74 20 70 6f 69 6e 74 20 74 6f 20 28 65 70 68  ht point to (eph
a9580 65 6d 65 72 61 6c 29 20 64 61 74 61 20 66 6f 72  emeral) data for
a9590 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
a95a0 2c 20 6f 72 20 69 74 20 6d 69 67 68 74 0a 20 20  , or it might.  
a95b0 2a 2a 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f  ** be NULL..  */
a95c0 0a 20 20 75 33 32 20 63 61 63 68 65 53 74 61 74  .  u32 cacheStat
a95d0 75 73 3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68  us;      /* Cach
a95e0 65 20 69 73 20 76 61 6c 69 64 20 69 66 20 74 68  e is valid if th
a95f0 69 73 20 6d 61 74 63 68 65 73 20 56 64 62 65 2e  is matches Vdbe.
a9600 63 61 63 68 65 43 74 72 20 2a 2f 0a 20 20 69 6e  cacheCtr */.  in
a9610 74 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20  t payloadSize;  
a9620 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
a9630 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
a9640 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
a9650 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20  u32 *aType;     
a9660 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 76 61        /* Type va
a9670 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 65 6e 74  lues for all ent
a9680 72 69 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  ries in the reco
a9690 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66  rd */.  u32 *aOf
a96a0 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  fset;         /*
a96b0 20 43 61 63 68 65 64 20 6f 66 66 73 65 74 73 20   Cached offsets 
a96c0 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
a96d0 65 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64 61 74  each columns dat
a96e0 61 20 2a 2f 0a 20 20 75 38 20 2a 61 52 6f 77 3b  a */.  u8 *aRow;
a96f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a9700 44 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72  Data for the cur
a9710 72 65 6e 74 20 72 6f 77 2c 20 69 66 20 61 6c 6c  rent row, if all
a9720 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a   on one page */.
a9730 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  };.typedef struc
a9740 74 20 56 64 62 65 43 75 72 73 6f 72 20 56 64 62  t VdbeCursor Vdb
a9750 65 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20  eCursor;../*.** 
a9760 57 68 65 6e 20 61 20 73 75 62 2d 70 72 6f 67 72  When a sub-progr
a9770 61 6d 20 69 73 20 65 78 65 63 75 74 65 64 20 28  am is executed (
a9780 4f 50 5f 50 72 6f 67 72 61 6d 29 2c 20 61 20 73  OP_Program), a s
a9790 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 69 73  tructure of this
a97a0 20 74 79 70 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f   type.** is allo
a97b0 63 61 74 65 64 20 74 6f 20 73 74 6f 72 65 20 74  cated to store t
a97c0 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  he current value
a97d0 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20   of the program 
a97e0 63 6f 75 6e 74 65 72 2c 20 61 73 0a 2a 2a 20 77  counter, as.** w
a97f0 65 6c 6c 20 61 73 20 74 68 65 20 63 75 72 72 65  ell as the curre
a9800 6e 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61  nt memory cell a
a9810 72 72 61 79 20 61 6e 64 20 76 61 72 69 6f 75 73  rray and various
a9820 20 6f 74 68 65 72 20 66 72 61 6d 65 20 73 70 65   other frame spe
a9830 63 69 66 69 63 0a 2a 2a 20 76 61 6c 75 65 73 20  cific.** values 
a9840 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64  stored in the Vd
a9850 62 65 20 73 74 72 75 63 74 2e 20 57 68 65 6e 20  be struct. When 
a9860 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20  the sub-program 
a9870 69 73 20 66 69 6e 69 73 68 65 64 2c 20 0a 2a 2a  is finished, .**
a9880 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 61 72   these values ar
a9890 65 20 63 6f 70 69 65 64 20 62 61 63 6b 20 74 6f  e copied back to
a98a0 20 74 68 65 20 56 64 62 65 20 66 72 6f 6d 20 74   the Vdbe from t
a98b0 68 65 20 56 64 62 65 46 72 61 6d 65 20 73 74 72  he VdbeFrame str
a98c0 75 63 74 75 72 65 2c 0a 2a 2a 20 72 65 73 74 6f  ucture,.** resto
a98d0 72 69 6e 67 20 74 68 65 20 73 74 61 74 65 20 6f  ring the state o
a98e0 66 20 74 68 65 20 56 4d 20 74 6f 20 61 73 20 69  f the VM to as i
a98f0 74 20 77 61 73 20 62 65 66 6f 72 65 20 74 68 65  t was before the
a9900 20 73 75 62 2d 70 72 6f 67 72 61 6d 0a 2a 2a 20   sub-program.** 
a9910 62 65 67 61 6e 20 65 78 65 63 75 74 69 6e 67 2e  began executing.
a9920 0a 2a 2a 0a 2a 2a 20 46 72 61 6d 65 73 20 61 72  .**.** Frames ar
a9930 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 6c 69  e stored in a li
a9940 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64  nked list headed
a9950 20 61 74 20 56 64 62 65 2e 70 50 61 72 65 6e 74   at Vdbe.pParent
a9960 2e 20 56 64 62 65 2e 70 50 61 72 65 6e 74 0a 2a  . Vdbe.pParent.*
a9970 2a 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20  * is the parent 
a9980 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 66  of the current f
a9990 72 61 6d 65 2c 20 6f 72 20 7a 65 72 6f 20 69 66  rame, or zero if
a99a0 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 61   the current fra
a99b0 6d 65 0a 2a 2a 20 69 73 20 74 68 65 20 6d 61 69  me.** is the mai
a99c0 6e 20 56 64 62 65 20 70 72 6f 67 72 61 6d 2e 0a  n Vdbe program..
a99d0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
a99e0 74 20 56 64 62 65 46 72 61 6d 65 20 56 64 62 65  t VdbeFrame Vdbe
a99f0 46 72 61 6d 65 3b 0a 73 74 72 75 63 74 20 56 64  Frame;.struct Vd
a9a00 62 65 46 72 61 6d 65 20 7b 0a 20 20 56 64 62 65  beFrame {.  Vdbe
a9a10 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
a9a20 20 20 20 20 2f 2a 20 56 4d 20 74 68 69 73 20 66      /* VM this f
a9a30 72 61 6d 65 20 62 65 6c 6f 6e 67 73 20 74 6f 20  rame belongs to 
a9a40 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20  */.  int pc;    
a9a50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a9a60 50 72 6f 67 72 61 6d 20 43 6f 75 6e 74 65 72 20  Program Counter 
a9a70 2a 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20  */.  Op *aOp;   
a9a80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a9a90 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74  Program instruct
a9aa0 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  ions */.  int nO
a9ab0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
a9ac0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 4f 70    /* Size of aOp
a9ad0 20 61 72 72 61 79 20 2a 2f 0a 20 20 4d 65 6d 20   array */.  Mem 
a9ae0 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *aMem;          
a9af0 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
a9b00 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a  memory cells */.
a9b10 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20    int nMem;     
a9b20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
a9b30 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
a9b40 6e 20 61 4d 65 6d 20 2a 2f 0a 20 20 56 64 62 65  n aMem */.  Vdbe
a9b50 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20  Cursor **apCsr; 
a9b60 20 20 20 20 2f 2a 20 45 6c 65 6d 65 6e 74 20 6f      /* Element o
a9b70 66 20 56 64 62 65 20 63 75 72 73 6f 72 73 20 2a  f Vdbe cursors *
a9b80 2f 0a 20 20 75 31 36 20 6e 43 75 72 73 6f 72 3b  /.  u16 nCursor;
a9b90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
a9ba0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
a9bb0 20 69 6e 20 61 70 43 73 72 20 2a 2f 0a 20 20 76   in apCsr */.  v
a9bc0 6f 69 64 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20  oid *token;     
a9bd0 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
a9be0 66 20 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b  f SubProgram.tok
a9bf0 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 69  en */.  int nChi
a9c00 6c 64 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  ldMem;          
a9c10 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d  /* Number of mem
a9c20 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 68  ory cells for ch
a9c30 69 6c 64 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69  ild frame */.  i
a9c40 6e 74 20 6e 43 68 69 6c 64 43 73 72 3b 20 20 20  nt nChildCsr;   
a9c50 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
a9c60 20 6f 66 20 63 75 72 73 6f 72 73 20 66 6f 72 20   of cursors for 
a9c70 63 68 69 6c 64 20 66 72 61 6d 65 20 2a 2f 0a 20  child frame */. 
a9c80 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20   i64 lastRowid; 
a9c90 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74           /* Last
a9ca0 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20 28 73   insert rowid (s
a9cb0 71 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77 69 64  qlite3.lastRowid
a9cc0 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e  ) */.  int nChan
a9cd0 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ge;            /
a9ce0 2a 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e  * Statement chan
a9cf0 67 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67  ges (Vdbe.nChang
a9d00 65 73 29 20 20 20 20 20 2a 2f 0a 20 20 56 64 62  es)     */.  Vdb
a9d10 65 46 72 61 6d 65 20 2a 70 50 61 72 65 6e 74 3b  eFrame *pParent;
a9d20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 6f       /* Parent o
a9d30 66 20 74 68 69 73 20 66 72 61 6d 65 20 2a 2f 0a  f this frame */.
a9d40 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 56 64 62 65  };..#define Vdbe
a9d50 46 72 61 6d 65 4d 65 6d 28 70 29 20 28 28 4d 65  FrameMem(p) ((Me
a9d60 6d 20 2a 29 26 28 28 75 38 20 2a 29 70 29 5b 52  m *)&((u8 *)p)[R
a9d70 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 64 62  OUND8(sizeof(Vdb
a9d80 65 46 72 61 6d 65 29 29 5d 29 0a 0a 2f 2a 0a 2a  eFrame))])../*.*
a9d90 2a 20 41 20 76 61 6c 75 65 20 66 6f 72 20 56 64  * A value for Vd
a9da0 62 65 43 75 72 73 6f 72 2e 63 61 63 68 65 56 61  beCursor.cacheVa
a9db0 6c 69 64 20 74 68 61 74 20 6d 65 61 6e 73 20 74  lid that means t
a9dc0 68 65 20 63 61 63 68 65 20 69 73 20 61 6c 77 61  he cache is alwa
a9dd0 79 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23  ys invalid..*/.#
a9de0 64 65 66 69 6e 65 20 43 41 43 48 45 5f 53 54 41  define CACHE_STA
a9df0 4c 45 20 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  LE 0../*.** Inte
a9e00 72 6e 61 6c 6c 79 2c 20 74 68 65 20 76 64 62 65  rnally, the vdbe
a9e10 20 6d 61 6e 69 70 75 6c 61 74 65 73 20 6e 65 61   manipulates nea
a9e20 72 6c 79 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75  rly all SQL valu
a9e30 65 73 20 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72  es as Mem.** str
a9e40 75 63 74 75 72 65 73 2e 20 45 61 63 68 20 4d 65  uctures. Each Me
a9e50 6d 20 73 74 72 75 63 74 20 6d 61 79 20 63 61 63  m struct may cac
a9e60 68 65 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 72  he multiple repr
a9e70 65 73 65 6e 74 61 74 69 6f 6e 73 20 28 73 74 72  esentations (str
a9e80 69 6e 67 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20  ing,.** integer 
a9e90 65 74 63 2e 29 20 6f 66 20 74 68 65 20 73 61 6d  etc.) of the sam
a9ea0 65 20 76 61 6c 75 65 2e 20 20 41 20 76 61 6c 75  e value.  A valu
a9eb0 65 20 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65  e (and therefore
a9ec0 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 29 0a   Mem structure).
a9ed0 2a 2a 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f  ** has the follo
a9ee0 77 69 6e 67 20 70 72 6f 70 65 72 74 69 65 73 3a  wing properties:
a9ef0 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75  .**.** Each valu
a9f00 65 20 68 61 73 20 61 20 6d 61 6e 69 66 65 73 74  e has a manifest
a9f10 20 74 79 70 65 2e 20 54 68 65 20 6d 61 6e 69 66   type. The manif
a9f20 65 73 74 20 74 79 70 65 20 6f 66 20 74 68 65 20  est type of the 
a9f30 76 61 6c 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20  value stored.** 
a9f40 69 6e 20 61 20 4d 65 6d 20 73 74 72 75 63 74 20  in a Mem struct 
a9f50 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  is returned by t
a9f60 68 65 20 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29  he MemType(Mem*)
a9f70 20 6d 61 63 72 6f 2e 20 54 68 65 20 74 79 70 65   macro. The type
a9f80 20 69 73 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51   is.** one of SQ
a9f90 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54  LITE_NULL, SQLIT
a9fa0 45 5f 49 4e 54 45 47 45 52 2c 20 53 51 4c 49 54  E_INTEGER, SQLIT
a9fb0 45 5f 52 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54  E_REAL, SQLITE_T
a9fc0 45 58 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  EXT or.** SQLITE
a9fd0 5f 42 4c 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74  _BLOB..*/.struct
a9fe0 20 4d 65 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b   Mem {.  union {
a9ff0 0a 20 20 20 20 69 36 34 20 69 3b 20 20 20 20 20  .    i64 i;     
aa000 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65           /* Inte
aa010 67 65 72 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20  ger value. */.  
aa020 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20    int nZero;    
aa030 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68        /* Used wh
aa040 65 6e 20 62 69 74 20 4d 45 4d 5f 5a 65 72 6f 20  en bit MEM_Zero 
aa050 69 73 20 73 65 74 20 69 6e 20 66 6c 61 67 73 20  is set in flags 
aa060 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a  */.    FuncDef *
aa070 70 44 65 66 3b 20 20 20 20 20 20 2f 2a 20 55 73  pDef;      /* Us
aa080 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61  ed only when fla
aa090 67 73 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20  gs==MEM_Agg */. 
aa0a0 20 20 20 52 6f 77 53 65 74 20 2a 70 52 6f 77 53     RowSet *pRowS
aa0b0 65 74 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 6f  et;    /* Used o
aa0c0 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d  nly when flags==
aa0d0 4d 45 4d 5f 52 6f 77 53 65 74 20 2a 2f 0a 20 20  MEM_RowSet */.  
aa0e0 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72    VdbeFrame *pFr
aa0f0 61 6d 65 3b 20 20 2f 2a 20 55 73 65 64 20 77 68  ame;  /* Used wh
aa100 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 46 72  en flags==MEM_Fr
aa110 61 6d 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20  ame */.  } u;.  
aa120 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20  double r;       
aa130 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75      /* Real valu
aa140 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
aa150 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  db;        /* Th
aa160 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74  e associated dat
aa170 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
aa180 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20   */.  char *z;  
aa190 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72            /* Str
aa1a0 69 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75  ing or BLOB valu
aa1b0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20  e */.  int n;   
aa1c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
aa1d0 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
aa1e0 72 73 20 69 6e 20 73 74 72 69 6e 67 20 76 61 6c  rs in string val
aa1f0 75 65 2c 20 65 78 63 6c 75 64 69 6e 67 20 27 5c  ue, excluding '\
aa200 30 27 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67  0' */.  u16 flag
aa210 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  s;          /* S
aa220 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  ome combination 
aa230 6f 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d  of MEM_Null, MEM
aa240 5f 53 74 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65  _Str, MEM_Dyn, e
aa250 74 63 2e 20 2a 2f 0a 20 20 75 38 20 20 74 79 70  tc. */.  u8  typ
aa260 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
aa270 4f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55  One of SQLITE_NU
aa280 4c 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 2c  LL, SQLITE_TEXT,
aa290 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c   SQLITE_INTEGER,
aa2a0 20 65 74 63 20 2a 2f 0a 20 20 75 38 20 20 65 6e   etc */.  u8  en
aa2b0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  c;            /*
aa2c0 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
aa2d0 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51  LITE_UTF16BE, SQ
aa2e0 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 2a 2f 0a  LITE_UTF16LE */.
aa2f0 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
aa300 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20 6e  oid *);  /* If n
aa310 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 68  ot null, call th
aa320 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64  is function to d
aa330 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20  elete Mem.z */. 
aa340 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20   char *zMalloc; 
aa350 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63 20       /* Dynamic 
aa360 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64  buffer allocated
aa370 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   by sqlite3_mall
aa380 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f  oc() */.};../* O
aa390 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
aa3a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67  e following flag
aa3b0 73 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e 64  s are set to ind
aa3c0 69 63 61 74 65 20 74 68 65 20 76 61 6c 69 64 4f  icate the validO
aa3d0 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74  K.** representat
aa3e0 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c 75  ions of the valu
aa3f0 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
aa400 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a  Mem struct..**.*
aa410 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75 6c  * If the MEM_Nul
aa420 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  l flag is set, t
aa430 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73  hen the value is
aa440 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c   an SQL NULL val
aa450 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 20  ue..** No other 
aa460 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 74  flags may be set
aa470 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a   in this case..*
aa480 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f  *.** If the MEM_
aa490 53 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 20  Str flag is set 
aa4a0 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74  then Mem.z point
aa4b0 73 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 65  s at a string re
aa4c0 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a  presentation..**
aa4d0 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73   Usually this is
aa4e0 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20   encoded in the 
aa4f0 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e 63  same unicode enc
aa500 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69  oding as the mai
aa510 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28 73  n.** database (s
aa520 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78 63  ee below for exc
aa530 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68 65  eptions). If the
aa540 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 69   MEM_Term flag i
aa550 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20 74  s also.** set, t
aa560 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20 69  hen the string i
aa570 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  s nul terminated
aa580 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61 6e  . The MEM_Int an
aa590 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20 66  d MEM_Real .** f
aa5a0 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73 74  lags may coexist
aa5b0 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53 74   with the MEM_St
aa5c0 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d 75  r flag..**.** Mu
aa5d0 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 73 65 20  ltiple of these 
aa5e0 76 61 6c 75 65 73 20 63 61 6e 20 61 70 70 65 61  values can appea
aa5f0 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73 2e 20  r in Mem.flags. 
aa600 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a   But only one.**
aa610 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 61   at a time can a
aa620 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74 79 70  ppear in Mem.typ
aa630 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  e..*/.#define ME
aa640 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78 30 30  M_Null      0x00
aa650 30 31 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73  01   /* Value is
aa660 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65   NULL */.#define
aa670 20 4d 45 4d 5f 53 74 72 20 20 20 20 20 20 20 30   MEM_Str       0
aa680 78 30 30 30 32 20 20 20 2f 2a 20 56 61 6c 75 65  x0002   /* Value
aa690 20 69 73 20 61 20 73 74 72 69 6e 67 20 2a 2f 0a   is a string */.
aa6a0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 74 20  #define MEM_Int 
aa6b0 20 20 20 20 20 20 30 78 30 30 30 34 20 20 20 2f        0x0004   /
aa6c0 2a 20 56 61 6c 75 65 20 69 73 20 61 6e 20 69 6e  * Value is an in
aa6d0 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  teger */.#define
aa6e0 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20 20 30   MEM_Real      0
aa6f0 78 30 30 30 38 20 20 20 2f 2a 20 56 61 6c 75 65  x0008   /* Value
aa700 20 69 73 20 61 20 72 65 61 6c 20 6e 75 6d 62 65   is a real numbe
aa710 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  r */.#define MEM
aa720 5f 42 6c 6f 62 20 20 20 20 20 20 30 78 30 30 31  _Blob      0x001
aa730 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  0   /* Value is 
aa740 61 20 42 4c 4f 42 20 2a 2f 0a 23 64 65 66 69 6e  a BLOB */.#defin
aa750 65 20 4d 45 4d 5f 52 6f 77 53 65 74 20 20 20 20  e MEM_RowSet    
aa760 30 78 30 30 32 30 20 20 20 2f 2a 20 56 61 6c 75  0x0020   /* Valu
aa770 65 20 69 73 20 61 20 52 6f 77 53 65 74 20 6f 62  e is a RowSet ob
aa780 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ject */.#define 
aa790 4d 45 4d 5f 46 72 61 6d 65 20 20 20 20 20 30 78  MEM_Frame     0x
aa7a0 30 30 34 30 20 20 20 2f 2a 20 56 61 6c 75 65 20  0040   /* Value 
aa7b0 69 73 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f  is a VdbeFrame o
aa7c0 62 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65  bject */.#define
aa7d0 20 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 20 20 30   MEM_TypeMask  0
aa7e0 78 30 30 66 66 20 20 20 2f 2a 20 4d 61 73 6b 20  x00ff   /* Mask 
aa7f0 6f 66 20 74 79 70 65 20 62 69 74 73 20 2a 2f 0a  of type bits */.
aa800 0a 2f 2a 20 57 68 65 6e 65 76 65 72 20 4d 65 6d  ./* Whenever Mem
aa810 20 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c 69   contains a vali
aa820 64 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62  d string or blob
aa830 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c   representation,
aa840 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66   one of.** the f
aa850 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 6d  ollowing flags m
aa860 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 64 65  ust be set to de
aa870 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 65 6d 6f  termine the memo
aa880 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a  ry management.**
aa890 20 70 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d 2e   policy for Mem.
aa8a0 7a 2e 20 20 54 68 65 20 4d 45 4d 5f 54 65 72 6d  z.  The MEM_Term
aa8b0 20 66 6c 61 67 20 74 65 6c 6c 73 20 75 73 20 77   flag tells us w
aa8c0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
aa8d0 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 5c  e.** string is \
aa8e0 30 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74 65  000 or \u0000 te
aa8f0 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a 23 64 65 66  rminated.*/.#def
aa900 69 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20 20  ine MEM_Term    
aa910 20 20 30 78 30 32 30 30 20 20 20 2f 2a 20 53 74    0x0200   /* St
aa920 72 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c 20  ring rep is nul 
aa930 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 23 64  terminated */.#d
aa940 65 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20 20 20  efine MEM_Dyn   
aa950 20 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a 20      0x0400   /* 
aa960 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c  Need to call sql
aa970 69 74 65 46 72 65 65 28 29 20 6f 6e 20 4d 65 6d  iteFree() on Mem
aa980 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  .z */.#define ME
aa990 4d 5f 53 74 61 74 69 63 20 20 20 20 30 78 30 38  M_Static    0x08
aa9a0 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f  00   /* Mem.z po
aa9b0 69 6e 74 73 20 74 6f 20 61 20 73 74 61 74 69 63  ints to a static
aa9c0 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69   string */.#defi
aa9d0 6e 65 20 4d 45 4d 5f 45 70 68 65 6d 20 20 20 20  ne MEM_Ephem    
aa9e0 20 30 78 31 30 30 30 20 20 20 2f 2a 20 4d 65 6d   0x1000   /* Mem
aa9f0 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20  .z points to an 
aaa00 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67  ephemeral string
aaa10 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
aaa20 41 67 67 20 20 20 20 20 20 20 30 78 32 30 30 30  Agg       0x2000
aaa30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e     /* Mem.z poin
aaa40 74 73 20 74 6f 20 61 6e 20 61 67 67 20 66 75 6e  ts to an agg fun
aaa50 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f  ction context */
aaa60 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72  .#define MEM_Zer
aaa70 6f 20 20 20 20 20 20 30 78 34 30 30 30 20 20 20  o      0x4000   
aaa80 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e 74 61 69 6e  /* Mem.i contain
aaa90 73 20 63 6f 75 6e 74 20 6f 66 20 30 73 20 61 70  s count of 0s ap
aaaa0 70 65 6e 64 65 64 20 74 6f 20 62 6c 6f 62 20 2a  pended to blob *
aaab0 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
aaac0 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20  _OMIT_INCRBLOB. 
aaad0 20 23 75 6e 64 65 66 20 4d 45 4d 5f 5a 65 72 6f   #undef MEM_Zero
aaae0 0a 20 20 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a  .  #define MEM_Z
aaaf0 65 72 6f 20 30 78 30 30 30 30 0a 23 65 6e 64 69  ero 0x0000.#endi
aab00 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  f.../*.** Clear 
aab10 61 6e 79 20 65 78 69 73 74 69 6e 67 20 74 79 70  any existing typ
aab20 65 20 66 6c 61 67 73 20 66 72 6f 6d 20 61 20 4d  e flags from a M
aab30 65 6d 20 61 6e 64 20 72 65 70 6c 61 63 65 20 74  em and replace t
aab40 68 65 6d 20 77 69 74 68 20 66 0a 2a 2f 0a 23 64  hem with f.*/.#d
aab50 65 66 69 6e 65 20 4d 65 6d 53 65 74 54 79 70 65  efine MemSetType
aab60 46 6c 61 67 28 70 2c 20 66 29 20 5c 0a 20 20 20  Flag(p, f) \.   
aab70 28 28 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28  ((p)->flags = ((
aab80 70 29 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f  p)->flags&~(MEM_
aab90 54 79 70 65 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72  TypeMask|MEM_Zer
aaba0 6f 29 29 7c 66 29 0a 0a 0a 2f 2a 20 41 20 56 64  o))|f).../* A Vd
aabb0 62 65 46 75 6e 63 20 69 73 20 6a 75 73 74 20 61  beFunc is just a
aabc0 20 46 75 6e 63 44 65 66 20 28 64 65 66 69 6e 65   FuncDef (define
aabd0 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  d in sqliteInt.h
aabe0 29 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a  ) that contains.
aabf0 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  ** additional in
aac00 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
aac10 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d  auxiliary inform
aac20 61 74 69 6f 6e 20 62 6f 75 6e 64 20 74 6f 20 61  ation bound to a
aac30 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 74  rguments.** of t
aac40 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  he function.  Th
aac50 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d  is is used to im
aac60 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69  plement the sqli
aac70 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
aac80 29 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33  ).** and sqlite3
aac90 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41  _set_auxdata() A
aaca0 50 49 73 2e 20 20 54 68 65 20 22 61 75 78 64 61  PIs.  The "auxda
aacb0 74 61 22 20 69 73 20 73 6f 6d 65 20 61 75 78 69  ta" is some auxi
aacc0 6c 69 61 72 79 20 64 61 74 61 0a 2a 2a 20 74 68  liary data.** th
aacd0 61 74 20 63 61 6e 20 62 65 20 61 73 73 6f 63 69  at can be associ
aace0 61 74 65 64 20 77 69 74 68 20 61 20 63 6f 6e 73  ated with a cons
aacf0 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  tant argument to
aad00 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68   a function.  Th
aad10 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 66 75 6e  is.** allows fun
aad20 63 74 69 6f 6e 73 20 73 75 63 68 20 61 73 20 22  ctions such as "
aad30 72 65 67 65 78 70 22 20 74 6f 20 63 6f 6d 70 69  regexp" to compi
aad40 6c 65 20 74 68 65 69 72 20 63 6f 6e 73 74 61 6e  le their constan
aad50 74 20 72 65 67 75 6c 61 72 0a 2a 2a 20 65 78 70  t regular.** exp
aad60 72 65 73 73 69 6f 6e 20 61 72 67 75 6d 65 6e 74  ression argument
aad70 20 6f 6e 63 65 20 61 6e 64 20 72 65 75 73 65 64   once and reused
aad80 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 63 6f   the compiled co
aad90 64 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 0a  de for multiple.
aada0 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a  ** invocations..
aadb0 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 46 75  */.struct VdbeFu
aadc0 6e 63 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  nc {.  FuncDef *
aadd0 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20  pFunc;          
aade0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69       /* The defi
aadf0 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75  nition of the fu
aae00 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nction */.  int 
aae10 6e 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20  nAux;           
aae20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
aae30 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61  ber of entries a
aae40 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41  llocated for apA
aae50 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74  ux[] */.  struct
aae60 20 41 75 78 44 61 74 61 20 7b 0a 20 20 20 20 76   AuxData {.    v
aae70 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20  oid *pAux;      
aae80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
aae90 41 75 78 20 64 61 74 61 20 66 6f 72 20 74 68 65  Aux data for the
aaea0 20 69 2d 74 68 20 61 72 67 75 6d 65 6e 74 20 2a   i-th argument *
aaeb0 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65  /.    void (*xDe
aaec0 6c 65 74 65 29 28 76 6f 69 64 20 2a 29 3b 20 20  lete)(void *);  
aaed0 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f      /* Destructo
aaee0 72 20 66 6f 72 20 74 68 65 20 61 75 78 20 64 61  r for the aux da
aaef0 74 61 20 2a 2f 0a 20 20 7d 20 61 70 41 75 78 5b  ta */.  } apAux[
aaf00 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  1];             
aaf10 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f        /* One slo
aaf20 74 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74  t for each funct
aaf30 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a  ion argument */.
aaf40 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 63  };../*.** The "c
aaf50 6f 6e 74 65 78 74 22 20 61 72 67 75 6d 65 6e 74  ontext" argument
aaf60 20 66 6f 72 20 61 20 69 6e 73 74 61 6c 6c 61 62   for a installab
aaf70 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20  le function.  A 
aaf80 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a  pointer to an.**
aaf90 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
aafa0 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74  s structure is t
aafb0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
aafc0 74 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65  t to the routine
aafd0 73 20 75 73 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d  s used.** implem
aafe0 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63  ent the SQL func
aaff0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tions..**.** The
ab000 72 65 20 69 73 20 61 20 74 79 70 65 64 65 66 20  re is a typedef 
ab010 66 6f 72 20 74 68 69 73 20 73 74 72 75 63 74 75  for this structu
ab020 72 65 20 69 6e 20 73 71 6c 69 74 65 2e 68 2e 20  re in sqlite.h. 
ab030 20 53 6f 20 61 6c 6c 20 72 6f 75 74 69 6e 65 73   So all routines
ab040 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 65 20 70 75  ,.** even the pu
ab050 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74  blic interface t
ab060 6f 20 53 51 4c 69 74 65 2c 20 63 61 6e 20 75 73  o SQLite, can us
ab070 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  e a pointer to t
ab080 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  his structure..*
ab090 2a 20 42 75 74 20 74 68 69 73 20 66 69 6c 65 20  * But this file 
ab0a0 69 73 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63  is the only plac
ab0b0 65 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65  e where the inte
ab0c0 72 6e 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20  rnal details of 
ab0d0 74 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72  this.** structur
ab0e0 65 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a  e are known..**.
ab0f0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
ab100 65 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73  e is defined ins
ab110 69 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e 68  ide of vdbeInt.h
ab120 20 62 65 63 61 75 73 65 20 69 74 20 75 73 65 73   because it uses
ab130 20 73 75 62 73 74 72 75 63 74 75 72 65 73 0a 2a   substructures.*
ab140 2a 20 28 4d 65 6d 29 20 77 68 69 63 68 20 61 72  * (Mem) which ar
ab150 65 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64 20 74  e only defined t
ab160 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  here..*/.struct 
ab170 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
ab180 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75  {.  FuncDef *pFu
ab190 6e 63 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  nc;       /* Poi
ab1a0 6e 74 65 72 20 74 6f 20 66 75 6e 63 74 69 6f 6e  nter to function
ab1b0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d   information.  M
ab1c0 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a  UST BE FIRST */.
ab1d0 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62    VdbeFunc *pVdb
ab1e0 65 46 75 6e 63 3b 20 20 2f 2a 20 41 75 78 69 6c  eFunc;  /* Auxil
ab1f0 61 72 79 20 64 61 74 61 2c 20 69 66 20 63 72 65  ary data, if cre
ab200 61 74 65 64 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73  ated. */.  Mem s
ab210 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ab220 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76   /* The return v
ab230 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 68  alue is stored h
ab240 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  ere */.  Mem *pM
ab250 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  em;            /
ab260 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73  * Memory cell us
ab270 65 64 20 74 6f 20 73 74 6f 72 65 20 61 67 67 72  ed to store aggr
ab280 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 2a 2f  egate context */
ab290 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 3b 20  .  int isError; 
ab2a0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
ab2b0 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20  r code returned 
ab2c0 62 79 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  by the function.
ab2d0 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70   */.  CollSeq *p
ab2e0 43 6f 6c 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43  Coll;       /* C
ab2f0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
ab300 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  e */.};../*.** A
ab310 20 53 65 74 20 73 74 72 75 63 74 75 72 65 20 69   Set structure i
ab320 73 20 75 73 65 64 20 66 6f 72 20 71 75 69 63 6b  s used for quick
ab330 20 74 65 73 74 69 6e 67 20 74 6f 20 73 65 65 20   testing to see 
ab340 69 66 20 61 20 76 61 6c 75 65 0a 2a 2a 20 69 73  if a value.** is
ab350 20 70 61 72 74 20 6f 66 20 61 20 73 6d 61 6c 6c   part of a small
ab360 20 73 65 74 2e 20 20 53 65 74 73 20 61 72 65 20   set.  Sets are 
ab370 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
ab380 74 20 63 6f 64 65 20 6c 69 6b 65 0a 2a 2a 20 74  t code like.** t
ab390 68 69 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20  his:.**         
ab3a0 20 20 20 78 2e 79 20 49 4e 20 28 27 68 69 27 2c     x.y IN ('hi',
ab3b0 27 68 6f 6f 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a  'hoo','hum').*/.
ab3c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
ab3d0 65 74 20 53 65 74 3b 0a 73 74 72 75 63 74 20 53  et Set;.struct S
ab3e0 65 74 20 7b 0a 20 20 48 61 73 68 20 68 61 73 68  et {.  Hash hash
ab3f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
ab400 20 41 20 73 65 74 20 69 73 20 6a 75 73 74 20 61   A set is just a
ab410 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
ab420 20 48 61 73 68 45 6c 65 6d 20 2a 70 72 65 76 3b   HashElem *prev;
ab430 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69          /* Previ
ab440 6f 75 73 6c 79 20 61 63 63 65 73 73 65 64 20 68  ously accessed h
ab450 61 73 68 20 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b  ash elemen */.};
ab460 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
ab470 6e 63 65 20 6f 66 20 74 68 65 20 76 69 72 74 75  nce of the virtu
ab480 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 69  al machine.  Thi
ab490 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  s structure cont
ab4a0 61 69 6e 73 20 74 68 65 20 63 6f 6d 70 6c 65 74  ains the complet
ab4b0 65 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 74 68  e.** state of th
ab4c0 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
ab4d0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 73 71  e..**.** The "sq
ab4e0 6c 69 74 65 33 5f 73 74 6d 74 22 20 73 74 72 75  lite3_stmt" stru
ab4f0 63 74 75 72 65 20 70 6f 69 6e 74 65 72 20 74 68  cture pointer th
ab500 61 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 62  at is returned b
ab510 79 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c  y sqlite3_compil
ab520 65 28 29 0a 2a 2a 20 69 73 20 72 65 61 6c 6c 79  e().** is really
ab530 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
ab540 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
ab550 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  s structure..**.
ab560 2a 2a 20 54 68 65 20 56 64 62 65 2e 69 6e 56 74  ** The Vdbe.inVt
ab570 61 62 4d 65 74 68 6f 64 20 76 61 72 69 61 62 6c  abMethod variabl
ab580 65 20 69 73 20 73 65 74 20 74 6f 20 6e 6f 6e 2d  e is set to non-
ab590 7a 65 72 6f 20 66 6f 72 20 74 68 65 20 64 75 72  zero for the dur
ab5a0 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20  ation of.** any 
ab5b0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 65  virtual table me
ab5c0 74 68 6f 64 20 69 6e 76 6f 63 61 74 69 6f 6e 73  thod invocations
ab5d0 20 6d 61 64 65 20 62 79 20 74 68 65 20 76 64 62   made by the vdb
ab5e0 65 20 70 72 6f 67 72 61 6d 2e 20 49 74 20 69 73  e program. It is
ab5f0 0a 2a 2a 20 73 65 74 20 74 6f 20 32 20 66 6f 72  .** set to 2 for
ab600 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64   xDestroy method
ab610 20 63 61 6c 6c 73 20 61 6e 64 20 31 20 66 6f 72   calls and 1 for
ab620 20 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f   all other metho
ab630 64 73 2e 20 54 68 69 73 0a 2a 2a 20 76 61 72 69  ds. This.** vari
ab640 61 62 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72  able is used for
ab650 20 74 77 6f 20 70 75 72 70 6f 73 65 73 3a 20 74   two purposes: t
ab660 6f 20 61 6c 6c 6f 77 20 78 44 65 73 74 72 6f 79  o allow xDestroy
ab670 20 6d 65 74 68 6f 64 73 20 74 6f 20 65 78 65 63   methods to exec
ab680 75 74 65 0a 2a 2a 20 22 44 52 4f 50 20 54 41 42  ute.** "DROP TAB
ab690 4c 45 22 20 73 74 61 74 65 6d 65 6e 74 73 20 61  LE" statements a
ab6a0 6e 64 20 74 6f 20 70 72 65 76 65 6e 74 20 73 6f  nd to prevent so
ab6b0 6d 65 20 6e 61 73 74 79 20 73 69 64 65 20 65 66  me nasty side ef
ab6c0 66 65 63 74 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c  fects of.** mall
ab6d0 6f 63 20 66 61 69 6c 75 72 65 20 77 68 65 6e 20  oc failure when 
ab6e0 53 51 4c 69 74 65 20 69 73 20 69 6e 76 6f 6b 65  SQLite is invoke
ab6f0 64 20 72 65 63 75 72 73 69 76 65 6c 79 20 62 79  d recursively by
ab700 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
ab710 20 0a 2a 2a 20 6d 65 74 68 6f 64 20 66 75 6e 63   .** method func
ab720 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  tion..*/.struct 
ab730 56 64 62 65 20 7b 0a 20 20 73 71 6c 69 74 65 33  Vdbe {.  sqlite3
ab740 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
ab750 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
ab760 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
ab770 20 6f 77 6e 73 20 74 68 69 73 20 73 74 61 74 65   owns this state
ab780 6d 65 6e 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a  ment */.  Vdbe *
ab790 70 50 72 65 76 2c 2a 70 4e 65 78 74 3b 20 20 20  pPrev,*pNext;   
ab7a0 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74    /* Linked list
ab7b0 20 6f 66 20 56 44 42 45 73 20 77 69 74 68 20 74   of VDBEs with t
ab7c0 68 65 20 73 61 6d 65 20 56 64 62 65 2e 64 62 20  he same Vdbe.db 
ab7d0 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20  */.  int nOp;   
ab7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ab7f0 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75  Number of instru
ab800 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72  ctions in the pr
ab810 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e  ogram */.  int n
ab820 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20  OpAlloc;        
ab830 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
ab840 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20  slots allocated 
ab850 66 6f 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f  for aOp[] */.  O
ab860 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20  p *aOp;         
ab870 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
ab880 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 69 72 74  to hold the virt
ab890 75 61 6c 20 6d 61 63 68 69 6e 65 27 73 20 70 72  ual machine's pr
ab8a0 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e  ogram */.  int n
ab8b0 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20  Label;          
ab8c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
ab8d0 6c 61 62 65 6c 73 20 75 73 65 64 20 2a 2f 0a 20  labels used */. 
ab8e0 20 69 6e 74 20 6e 4c 61 62 65 6c 41 6c 6c 6f 63   int nLabelAlloc
ab8f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
ab900 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f  er of slots allo
ab910 63 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b  cated in aLabel[
ab920 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62  ] */.  int *aLab
ab930 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  el;            /
ab940 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
ab950 74 68 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20  the labels */.  
ab960 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 20 20 20 20  Mem **apArg;    
ab970 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
ab980 65 6e 74 73 20 74 6f 20 63 75 72 72 65 6e 74 6c  ents to currentl
ab990 79 20 65 78 65 63 75 74 69 6e 67 20 75 73 65 72  y executing user
ab9a0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 4d   function */.  M
ab9b0 65 6d 20 2a 61 43 6f 6c 4e 61 6d 65 3b 20 20 20  em *aColName;   
ab9c0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
ab9d0 20 6e 61 6d 65 73 20 74 6f 20 72 65 74 75 72 6e   names to return
ab9e0 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 73 75   */.  Mem *pResu
ab9f0 6c 74 53 65 74 3b 20 20 20 20 20 20 20 20 2f 2a  ltSet;        /*
aba00 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61   Pointer to an a
aba10 72 72 61 79 20 6f 66 20 72 65 73 75 6c 74 73 20  rray of results 
aba20 2a 2f 0a 20 20 75 31 36 20 6e 52 65 73 43 6f 6c  */.  u16 nResCol
aba30 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  umn;         /* 
aba40 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
aba50 73 20 69 6e 20 6f 6e 65 20 72 6f 77 20 6f 66 20  s in one row of 
aba60 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
aba70 2f 0a 20 20 75 31 36 20 6e 43 75 72 73 6f 72 3b  /.  u16 nCursor;
aba80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
aba90 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 69  umber of slots i
abaa0 6e 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20 20 56  n apCsr[] */.  V
abab0 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 73  dbeCursor **apCs
abac0 72 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6c  r;     /* One el
abad0 65 6d 65 6e 74 20 6f 66 20 74 68 69 73 20 61 72  ement of this ar
abae0 72 61 79 20 66 6f 72 20 65 61 63 68 20 6f 70 65  ray for each ope
abaf0 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 38  n cursor */.  u8
abb00 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20 20   errorAction;   
abb10 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72        /* Recover
abb20 79 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69  y action to do i
abb30 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72  n case of an err
abb40 6f 72 20 2a 2f 0a 20 20 75 38 20 6f 6b 56 61 72  or */.  u8 okVar
abb50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
abb60 2f 2a 20 54 72 75 65 20 69 66 20 61 7a 56 61 72  /* True if azVar
abb70 5b 5d 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74  [] has been init
abb80 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 79 6e 56  ialized */.  ynV
abb90 61 72 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20  ar nVar;        
abba0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
abbb0 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 56 61  f entries in aVa
abbc0 72 5b 5d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56  r[] */.  Mem *aV
abbd0 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ar;             
abbe0 20 2f 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 74   /* Values for t
abbf0 68 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 6f  he OP_Variable o
abc00 70 63 6f 64 65 2e 20 2a 2f 0a 20 20 63 68 61 72  pcode. */.  char
abc10 20 2a 2a 61 7a 56 61 72 3b 20 20 20 20 20 20 20   **azVar;       
abc20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 76      /* Name of v
abc30 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 33  ariables */.  u3
abc40 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20  2 magic;        
abc50 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 6e        /* Magic n
abc60 75 6d 62 65 72 20 66 6f 72 20 73 61 6e 69 74 79  umber for sanity
abc70 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 69   checking */.  i
abc80 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20  nt nMem;        
abc90 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
abca0 20 6f 66 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74   of memory locat
abcb0 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 61  ions currently a
abcc0 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 4d 65  llocated */.  Me
abcd0 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *aMem;        
abce0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 65 6d        /* The mem
abcf0 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f  ory locations */
abd00 0a 20 20 75 33 32 20 63 61 63 68 65 43 74 72 3b  .  u32 cacheCtr;
abd10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64             /* Vd
abd20 62 65 43 75 72 73 6f 72 20 72 6f 77 20 63 61 63  beCursor row cac
abd30 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 63 6f  he generation co
abd40 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70  unter */.  int p
abd50 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
abd60 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61     /* The progra
abd70 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  m counter */.  i
abd80 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
abd90 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
abda0 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 63  to return */.  c
abdb0 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20  har *zErrMsg;   
abdc0 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20         /* Error 
abdd0 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20  message written 
abde0 68 65 72 65 20 2a 2f 0a 20 20 75 38 20 65 78 70  here */.  u8 exp
abdf0 6c 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20  lain;           
abe00 20 20 2f 2a 20 54 72 75 65 20 69 66 20 45 58 50    /* True if EXP
abe10 4c 41 49 4e 20 70 72 65 73 65 6e 74 20 6f 6e 20  LAIN present on 
abe20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 20  SQL command */. 
abe30 20 75 38 20 63 68 61 6e 67 65 43 6e 74 4f 6e 3b   u8 changeCntOn;
abe40 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
abe50 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63   to update the c
abe60 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f  hange-counter */
abe70 0a 20 20 75 38 20 65 78 70 69 72 65 64 3b 20 20  .  u8 expired;  
abe80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
abe90 75 65 20 69 66 20 74 68 65 20 56 4d 20 6e 65 65  ue if the VM nee
abea0 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69  ds to be recompi
abeb0 6c 65 64 20 2a 2f 0a 20 20 75 38 20 6d 69 6e 57  led */.  u8 minW
abec0 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 20  riteFileFormat; 
abed0 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 66 69 6c 65   /* Minimum file
abee0 20 66 6f 72 6d 61 74 20 66 6f 72 20 77 72 69 74   format for writ
abef0 61 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 69  able database fi
abf00 6c 65 73 20 2a 2f 0a 20 20 75 38 20 69 6e 56 74  les */.  u8 inVt
abf10 61 62 4d 65 74 68 6f 64 3b 20 20 20 20 20 20 20  abMethod;       
abf20 20 2f 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73   /* See comments
abf30 20 61 62 6f 76 65 20 2a 2f 0a 20 20 75 38 20 75   above */.  u8 u
abf40 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 20  sesStmtJournal; 
abf50 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75      /* True if u
abf60 73 65 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20  ses a statement 
abf70 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20  journal */.  u8 
abf80 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20  readOnly;       
abf90 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72       /* True for
abfa0 20 72 65 61 64 2d 6f 6e 6c 79 20 73 74 61 74 65   read-only state
abfb0 6d 65 6e 74 73 20 2a 2f 0a 20 20 75 38 20 69 73  ments */.  u8 is
abfc0 50 72 65 70 61 72 65 56 32 3b 20 20 20 20 20 20  PrepareV2;      
abfd0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72     /* True if pr
abfe0 65 70 61 72 65 64 20 77 69 74 68 20 70 72 65 70  epared with prep
abff0 61 72 65 5f 76 32 28 29 20 2a 2f 0a 20 20 69 6e  are_v2() */.  in
ac000 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20  t nChange;      
ac010 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
ac020 6f 66 20 64 62 20 63 68 61 6e 67 65 73 20 6d 61  of db changes ma
ac030 64 65 20 73 69 6e 63 65 20 6c 61 73 74 20 72 65  de since last re
ac040 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 62 74 72  set */.  int btr
ac050 65 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  eeMask;         
ac060 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 64   /* Bitmask of d
ac070 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69 65 73  b->aDb[] entries
ac080 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20   referenced */. 
ac090 20 69 36 34 20 73 74 61 72 74 54 69 6d 65 3b 20   i64 startTime; 
ac0a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65           /* Time
ac0b0 20 77 68 65 6e 20 71 75 65 72 79 20 73 74 61 72   when query star
ac0c0 74 65 64 20 2d 20 75 73 65 64 20 66 6f 72 20 70  ted - used for p
ac0d0 72 6f 66 69 6c 69 6e 67 20 2a 2f 0a 20 20 42 74  rofiling */.  Bt
ac0e0 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 61 4d  reeMutexArray aM
ac0f0 75 74 65 78 3b 20 2f 2a 20 41 6e 20 61 72 72 61  utex; /* An arra
ac100 79 20 6f 66 20 42 74 72 65 65 20 75 73 65 64 20  y of Btree used 
ac110 68 65 72 65 20 61 6e 64 20 6e 65 65 64 69 6e 67  here and needing
ac120 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69 6e 74 20   locks */.  int 
ac130 61 43 6f 75 6e 74 65 72 5b 32 5d 3b 20 20 20 20  aCounter[2];    
ac140 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 73 20      /* Counters 
ac150 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  used by sqlite3_
ac160 73 74 6d 74 5f 73 74 61 74 75 73 28 29 20 2a 2f  stmt_status() */
ac170 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20  .  char *zSql;  
ac180 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
ac190 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74  xt of the SQL st
ac1a0 61 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e  atement that gen
ac1b0 65 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 20  erated this */. 
ac1c0 20 76 6f 69 64 20 2a 70 46 72 65 65 3b 20 20 20   void *pFree;   
ac1d0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65           /* Free
ac1e0 20 74 68 69 73 20 77 68 65 6e 20 64 65 6c 65 74   this when delet
ac1f0 69 6e 67 20 74 68 65 20 76 64 62 65 20 2a 2f 0a  ing the vdbe */.
ac200 20 20 69 36 34 20 6e 46 6b 43 6f 6e 73 74 72 61    i64 nFkConstra
ac210 69 6e 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d  int;      /* Num
ac220 62 65 72 20 6f 66 20 69 6d 6d 2e 20 46 4b 20 63  ber of imm. FK c
ac230 6f 6e 73 74 72 61 69 6e 74 73 20 74 68 69 73 20  onstraints this 
ac240 56 4d 20 2a 2f 0a 20 20 69 36 34 20 6e 53 74 6d  VM */.  i64 nStm
ac250 74 44 65 66 43 6f 6e 73 3b 20 20 20 20 20 20 20  tDefCons;       
ac260 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 66  /* Number of def
ac270 2e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68  . constraints wh
ac280 65 6e 20 73 74 6d 74 20 73 74 61 72 74 65 64 20  en stmt started 
ac290 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 65 6d  */.  int iStatem
ac2a0 65 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ent;         /* 
ac2b0 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d 62 65 72  Statement number
ac2c0 20 28 6f 72 20 30 20 69 66 20 68 61 73 20 6e 6f   (or 0 if has no
ac2d0 74 20 6f 70 65 6e 65 64 20 73 74 6d 74 29 20 2a  t opened stmt) *
ac2e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
ac2f0 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 74 72  DEBUG.  FILE *tr
ac300 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ace;            
ac310 2f 2a 20 57 72 69 74 65 20 61 6e 20 65 78 65 63  /* Write an exec
ac320 75 74 69 6f 6e 20 74 72 61 63 65 20 68 65 72 65  ution trace here
ac330 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f  , if not NULL */
ac340 0a 23 65 6e 64 69 66 0a 20 20 56 64 62 65 46 72  .#endif.  VdbeFr
ac350 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20  ame *pFrame;    
ac360 20 20 2f 2a 20 50 61 72 65 6e 74 20 66 72 61 6d    /* Parent fram
ac370 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 6d  e */.  int nFram
ac380 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
ac390 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 6d  * Number of fram
ac3a0 65 73 20 69 6e 20 70 46 72 61 6d 65 20 6c 69 73  es in pFrame lis
ac3b0 74 20 2a 2f 0a 20 20 75 33 32 20 65 78 70 6d 61  t */.  u32 expma
ac3c0 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  sk;            /
ac3d0 2a 20 42 69 6e 64 69 6e 67 20 74 6f 20 74 68 65  * Binding to the
ac3e0 73 65 20 76 61 72 73 20 69 6e 76 61 6c 69 64 61  se vars invalida
ac3f0 74 65 73 20 56 4d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  tes VM */.};../*
ac400 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
ac410 67 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 76 61  g are allowed va
ac420 6c 75 65 73 20 66 6f 72 20 56 64 62 65 2e 6d 61  lues for Vdbe.ma
ac430 67 69 63 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56  gic.*/.#define V
ac440 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 20  DBE_MAGIC_INIT  
ac450 20 20 20 30 78 32 36 62 63 65 61 61 35 20 20 20     0x26bceaa5   
ac460 20 2f 2a 20 42 75 69 6c 64 69 6e 67 20 61 20 56   /* Building a V
ac470 44 42 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 23  DBE program */.#
ac480 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49  define VDBE_MAGI
ac490 43 5f 52 55 4e 20 20 20 20 20 20 30 78 62 64 66  C_RUN      0xbdf
ac4a0 32 30 64 61 33 20 20 20 20 2f 2a 20 56 44 42 45  20da3    /* VDBE
ac4b0 20 69 73 20 72 65 61 64 79 20 74 6f 20 65 78 65   is ready to exe
ac4c0 63 75 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cute */.#define 
ac4d0 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20  VDBE_MAGIC_HALT 
ac4e0 20 20 20 20 30 78 35 31 39 63 32 39 37 33 20 20      0x519c2973  
ac4f0 20 20 2f 2a 20 56 44 42 45 20 68 61 73 20 63 6f    /* VDBE has co
ac500 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74 69 6f  mpleted executio
ac510 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42  n */.#define VDB
ac520 45 5f 4d 41 47 49 43 5f 44 45 41 44 20 20 20 20  E_MAGIC_DEAD    
ac530 20 30 78 62 36 30 36 63 33 63 38 20 20 20 20 2f   0xb606c3c8    /
ac540 2a 20 54 68 65 20 56 44 42 45 20 68 61 73 20 62  * The VDBE has b
ac550 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 20  een deallocated 
ac560 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  */../*.** Functi
ac570 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f  on prototypes.*/
ac580 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac590 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ac5a0 46 72 65 65 43 75 72 73 6f 72 28 56 64 62 65 20  FreeCursor(Vdbe 
ac5b0 2a 2c 20 56 64 62 65 43 75 72 73 6f 72 2a 29 3b  *, VdbeCursor*);
ac5c0 0a 76 6f 69 64 20 73 71 6c 69 74 65 56 64 62 65  .void sqliteVdbe
ac5d0 50 6f 70 53 74 61 63 6b 28 56 64 62 65 2a 2c 69  PopStack(Vdbe*,i
ac5e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
ac5f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ac600 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
ac610 56 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a 23 69  VdbeCursor*);.#i
ac620 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
ac630 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e  _DEBUG) || defin
ac640 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29  ed(VDBE_PROFILE)
ac650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac660 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ac670 50 72 69 6e 74 4f 70 28 46 49 4c 45 2a 2c 20 69  PrintOp(FILE*, i
ac680 6e 74 2c 20 4f 70 2a 29 3b 0a 23 65 6e 64 69 66  nt, Op*);.#endif
ac690 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac6a0 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53  u32 sqlite3VdbeS
ac6b0 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 33 32  erialTypeLen(u32
ac6c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ac6d0 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62  E u32 sqlite3Vdb
ac6e0 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 2a  eSerialType(Mem*
ac6f0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
ac700 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
ac710 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28  e3VdbeSerialPut(
ac720 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20  unsigned char*, 
ac730 69 6e 74 2c 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b  int, Mem*, int);
ac740 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac750 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53  u32 sqlite3VdbeS
ac760 65 72 69 61 6c 47 65 74 28 63 6f 6e 73 74 20 75  erialGet(const u
ac770 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75  nsigned char*, u
ac780 33 32 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  32, Mem*);.SQLIT
ac790 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
ac7a0 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
ac7b0 41 75 78 44 61 74 61 28 56 64 62 65 46 75 6e 63  AuxData(VdbeFunc
ac7c0 2a 2c 20 69 6e 74 29 3b 0a 0a 69 6e 74 20 73 71  *, int);..int sq
ac7d0 6c 69 74 65 32 42 74 72 65 65 4b 65 79 43 6f 6d  lite2BtreeKeyCom
ac7e0 70 61 72 65 28 42 74 43 75 72 73 6f 72 20 2a 2c  pare(BtCursor *,
ac7f0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 20 69   const void *, i
ac800 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b  nt, int, int *);
ac810 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac820 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49  int sqlite3VdbeI
ac830 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 56 64 62  dxKeyCompare(Vdb
ac840 65 43 75 72 73 6f 72 2a 2c 55 6e 70 61 63 6b 65  eCursor*,Unpacke
ac850 64 52 65 63 6f 72 64 2a 2c 69 6e 74 2a 29 3b 0a  dRecord*,int*);.
ac860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ac870 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64  nt sqlite3VdbeId
ac880 78 52 6f 77 69 64 28 73 71 6c 69 74 65 33 2a 2c  xRowid(sqlite3*,
ac890 20 42 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 34   BtCursor *, i64
ac8a0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
ac8b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
ac8c0 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20  emCompare(const 
ac8d0 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a  Mem*, const Mem*
ac8e0 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2a  , const CollSeq*
ac8f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ac900 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
ac910 65 45 78 65 63 28 56 64 62 65 2a 29 3b 0a 53 51  eExec(Vdbe*);.SQ
ac920 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ac930 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74   sqlite3VdbeList
ac940 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
ac950 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ac960 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 65  te3VdbeHalt(Vdbe
ac970 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
ac980 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
ac990 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
ac9a0 28 4d 65 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  (Mem *, int);.SQ
ac9b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ac9c0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54   sqlite3VdbeMemT
ac9d0 6f 6f 42 69 67 28 4d 65 6d 2a 29 3b 0a 53 51 4c  ooBig(Mem*);.SQL
ac9e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
ac9f0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f  sqlite3VdbeMemCo
aca00 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d  py(Mem*, const M
aca10 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
aca20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
aca30 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43  3VdbeMemShallowC
aca40 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20  opy(Mem*, const 
aca50 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  Mem*, int);.SQLI
aca60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
aca70 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f  sqlite3VdbeMemMo
aca80 76 65 28 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a  ve(Mem*, Mem*);.
aca90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
acaa0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
acab0 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65  mNulTerminate(Me
acac0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
acad0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
acae0 64 62 65 4d 65 6d 53 65 74 53 74 72 28 4d 65 6d  dbeMemSetStr(Mem
acaf0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
acb00 69 6e 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a 29  int, u8, void(*)
acb10 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45  (void*));.SQLITE
acb20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
acb30 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
acb40 6e 74 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29 3b  nt64(Mem*, i64);
acb50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
acb60 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
acb70 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d  MemSetDouble(Mem
acb80 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49  *, double);.SQLI
acb90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
acba0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
acbb0 74 4e 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c  tNull(Mem*);.SQL
acbc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
acbd0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
acbe0 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c  etZeroBlob(Mem*,
acbf0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
acc00 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
acc10 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65  3VdbeMemSetRowSe
acc20 74 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  t(Mem*);.SQLITE_
acc30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
acc40 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72  te3VdbeMemMakeWr
acc50 69 74 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53  iteable(Mem*);.S
acc60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
acc70 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
acc80 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c 20  Stringify(Mem*, 
acc90 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
acca0 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33  VATE i64 sqlite3
accb0 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d  VdbeIntValue(Mem
accc0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
accd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
acce0 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
accf0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
acd00 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c  IVATE double sql
acd10 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75  ite3VdbeRealValu
acd20 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  e(Mem*);.SQLITE_
acd30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
acd40 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41  ite3VdbeIntegerA
acd50 66 66 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53  ffinity(Mem*);.S
acd60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
acd70 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
acd80 52 65 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a 53  Realify(Mem*);.S
acd90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
acda0 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
acdb0 4e 75 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a  Numerify(Mem*);.
acdc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
acdd0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
acde0 6d 46 72 6f 6d 42 74 72 65 65 28 42 74 43 75 72  mFromBtree(BtCur
acdf0 73 6f 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  sor*,int,int,int
ace00 2c 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ,Mem*);.SQLITE_P
ace10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
ace20 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
ace30 65 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54  e(Mem *p);.SQLIT
ace40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
ace50 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
ace60 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d  easeExternal(Mem
ace70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   *p);.SQLITE_PRI
ace80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
ace90 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28  VdbeMemFinalize(
acea0 4d 65 6d 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b  Mem*, FuncDef*);
aceb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
acec0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
aced0 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e  te3OpcodeName(in
acee0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
acef0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
acf00 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65  beOpcodeHasPrope
acf10 72 74 79 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  rty(int, int);.S
acf20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
acf30 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
acf40 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  Grow(Mem *pMem, 
acf50 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65  int n, int prese
acf60 72 76 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  rve);.SQLITE_PRI
acf70 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
acf80 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65  VdbeCloseStateme
acf90 6e 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b  nt(Vdbe *, int);
acfa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
acfb0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
acfc0 46 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65  FrameDelete(Vdbe
acfd0 46 72 61 6d 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Frame*);.SQLITE_
acfe0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
acff0 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 73 74  te3VdbeFrameRest
ad000 6f 72 65 28 56 64 62 65 46 72 61 6d 65 20 2a 29  ore(VdbeFrame *)
ad010 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ad020 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
ad030 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 4d 65  eMemStoreType(Me
ad040 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64  m *pMem);..#ifnd
ad050 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
ad060 4f 52 45 49 47 4e 5f 4b 45 59 0a 53 51 4c 49 54  OREIGN_KEY.SQLIT
ad070 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ad080 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b  lite3VdbeCheckFk
ad090 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 23  (Vdbe *, int);.#
ad0a0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
ad0b0 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b  lite3VdbeCheckFk
ad0c0 28 70 2c 69 29 20 30 0a 23 65 6e 64 69 66 0a 0a  (p,i) 0.#endif..
ad0d0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
ad0e0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
ad0f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ad100 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ad110 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28  MutexArrayEnter(
ad120 56 64 62 65 20 2a 70 29 3b 0a 23 65 6c 73 65 0a  Vdbe *p);.#else.
ad130 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
ad140 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e  VdbeMutexArrayEn
ad150 74 65 72 28 70 29 0a 23 65 6e 64 69 66 0a 0a 53  ter(p).#endif..S
ad160 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ad170 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
ad180 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 2a 2c 20  Translate(Mem*, 
ad190 75 38 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  u8);.#ifdef SQLI
ad1a0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
ad1b0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
ad1c0 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53  qlite3VdbePrintS
ad1d0 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  ql(Vdbe*);.SQLIT
ad1e0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
ad1f0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50   sqlite3VdbeMemP
ad200 72 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a  rettyPrint(Mem *
ad210 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66  pMem, char *zBuf
ad220 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45  );.#endif.SQLITE
ad230 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ad240 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c  ite3VdbeMemHandl
ad250 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 3b  eBom(Mem *pMem);
ad260 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
ad270 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53  _OMIT_INCRBLOB.S
ad280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
ad290 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
ad2a0 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d  emExpandBlob(Mem
ad2b0 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65   *);.#else.  #de
ad2c0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65  fine sqlite3Vdbe
ad2d0 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 78 29  MemExpandBlob(x)
ad2e0 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69   SQLITE_OK.#endi
ad2f0 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  f..#endif /* !de
ad300 66 69 6e 65 64 28 5f 56 44 42 45 49 4e 54 5f 48  fined(_VDBEINT_H
ad310 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  _) */../********
ad320 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64  ****** End of vd
ad330 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  beInt.h ********
ad340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad360 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
ad370 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
ad380 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
ad390 6f 66 66 20 69 6e 20 75 74 66 2e 63 20 2a 2a 2a  off in utf.c ***
ad3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad3b0 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  *****/..#ifndef 
ad3c0 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
ad3d0 49 4f 4e 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  ION./*.** The fo
ad3e0 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74  llowing constant
ad3f0 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 62   value is used b
ad400 79 20 74 68 65 20 53 51 4c 49 54 45 5f 42 49 47  y the SQLITE_BIG
ad410 45 4e 44 49 41 4e 20 61 6e 64 0a 2a 2a 20 53 51  ENDIAN and.** SQ
ad420 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41  LITE_LITTLEENDIA
ad430 4e 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 53 51 4c  N macros..*/.SQL
ad440 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
ad450 74 20 69 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65  t int sqlite3one
ad460 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   = 1;.#endif /* 
ad470 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
ad480 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ION */../*.** Th
ad490 69 73 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20  is lookup table 
ad4a0 69 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20  is used to help 
ad4b0 64 65 63 6f 64 65 20 74 68 65 20 66 69 72 73 74  decode the first
ad4c0 20 62 79 74 65 20 6f 66 0a 2a 2a 20 61 20 6d 75   byte of.** a mu
ad4d0 6c 74 69 2d 62 79 74 65 20 55 54 46 38 20 63 68  lti-byte UTF8 ch
ad4e0 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 73 74 61 74  aracter..*/.stat
ad4f0 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
ad500 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 74  d char sqlite3Ut
ad510 66 38 54 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a 20  f8Trans1[] = {. 
ad520 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
ad530 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30  2, 0x03, 0x04, 0
ad540 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c  x05, 0x06, 0x07,
ad550 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 30  .  0x08, 0x09, 0
ad560 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 2c  x0a, 0x0b, 0x0c,
ad570 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 30   0x0d, 0x0e, 0x0
ad580 66 2c 0a 20 20 30 78 31 30 2c 20 30 78 31 31 2c  f,.  0x10, 0x11,
ad590 20 30 78 31 32 2c 20 30 78 31 33 2c 20 30 78 31   0x12, 0x13, 0x1
ad5a0 34 2c 20 30 78 31 35 2c 20 30 78 31 36 2c 20 30  4, 0x15, 0x16, 0
ad5b0 78 31 37 2c 0a 20 20 30 78 31 38 2c 20 30 78 31  x17,.  0x18, 0x1
ad5c0 39 2c 20 30 78 31 61 2c 20 30 78 31 62 2c 20 30  9, 0x1a, 0x1b, 0
ad5d0 78 31 63 2c 20 30 78 31 64 2c 20 30 78 31 65 2c  x1c, 0x1d, 0x1e,
ad5e0 20 30 78 31 66 2c 0a 20 20 30 78 30 30 2c 20 30   0x1f,.  0x00, 0
ad5f0 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c  x01, 0x02, 0x03,
ad600 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30   0x04, 0x05, 0x0
ad610 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38 2c  6, 0x07,.  0x08,
ad620 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78 30   0x09, 0x0a, 0x0
ad630 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20 30  b, 0x0c, 0x0d, 0
ad640 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 30  x0e, 0x0f,.  0x0
ad650 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30  0, 0x01, 0x02, 0
ad660 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c  x03, 0x04, 0x05,
ad670 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30   0x06, 0x07,.  0
ad680 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c  x00, 0x01, 0x02,
ad690 20 30 78 30 33 2c 20 30 78 30 30 2c 20 30 78 30   0x03, 0x00, 0x0
ad6a0 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 0a 7d  1, 0x00, 0x00,.}
ad6b0 3b 0a 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54  ;...#define WRIT
ad6c0 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 20  E_UTF8(zOut, c) 
ad6d0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
ad6e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69             \.  i
ad6f0 66 28 20 63 3c 30 78 30 30 30 38 30 20 29 7b 20  f( c<0x00080 ){ 
ad700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad720 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
ad730 2b 20 3d 20 28 75 38 29 28 63 26 30 78 46 46 29  + = (u8)(c&0xFF)
ad740 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ad750 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ad760 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20   }              
ad770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad790 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69        \.  else i
ad7a0 66 28 20 63 3c 30 78 30 30 38 30 30 20 29 7b 20  f( c<0x00800 ){ 
ad7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ad7d0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30  .    *zOut++ = 0
ad7e0 78 43 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36  xC0 + (u8)((c>>6
ad7f0 29 26 30 78 31 46 29 3b 20 20 20 20 20 20 20 20  )&0x1F);        
ad800 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
ad810 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28  Out++ = 0x80 + (
ad820 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20 20  u8)(c & 0x3F);  
ad830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad840 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
ad850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad870 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65 6c            \.  el
ad880 73 65 20 69 66 28 20 63 3c 30 78 31 30 30 30 30  se if( c<0x10000
ad890 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
ad8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8b0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
ad8c0 20 3d 20 30 78 45 30 20 2b 20 28 75 38 29 28 28   = 0xE0 + (u8)((
ad8d0 63 3e 3e 31 32 29 26 30 78 30 46 29 3b 20 20 20  c>>12)&0x0F);   
ad8e0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ad8f0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30    *zOut++ = 0x80
ad900 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 20 26   + (u8)((c>>6) &
ad910 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20   0x3F);         
ad920 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
ad930 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29  ++ = 0x80 + (u8)
ad940 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20  (c & 0x3F);     
ad950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad960 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20    }else{        
ad970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad990 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
ad9a0 75 74 2b 2b 20 3d 20 30 78 46 30 20 2b 20 28 75  ut++ = 0xF0 + (u
ad9b0 38 29 28 28 63 3e 3e 31 38 29 20 26 20 30 78 30  8)((c>>18) & 0x0
ad9c0 37 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  7);             
ad9d0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
ad9e0 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e  0x80 + (u8)((c>>
ad9f0 31 32 29 20 26 20 30 78 33 46 29 3b 20 20 20 20  12) & 0x3F);    
ada00 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
ada10 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
ada20 28 75 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78  (u8)((c>>6) & 0x
ada30 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3F);            
ada40 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
ada50 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20  = 0x80 + (u8)(c 
ada60 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
ada70 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
ada80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ada90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adab0 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65      \.}..#define
adac0 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a   WRITE_UTF16LE(z
adad0 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20  Out, c) {       
adae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
adb00 20 69 66 28 20 63 3c 3d 30 78 46 46 46 46 20 29   if( c<=0xFFFF )
adb10 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
adb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb40 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
adb50 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46   = (u8)(c&0x00FF
adb60 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
adb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb80 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
adb90 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 63  zOut++ = (u8)((c
adba0 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 20  >>8)&0x00FF);   
adbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
adbd0 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20  .  }else{       
adbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc10 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
adc20 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e 3e 31  ++ = (u8)(((c>>1
adc30 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 28 28  0)&0x003F) + (((
adc40 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26  c-0x10000)>>10)&
adc50 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20  0x00C0));  \.   
adc60 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
adc70 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78  0x00D8 + (((c-0x
adc80 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33  10000)>>18)&0x03
adc90 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ));             
adca0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
adcb0 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b   (u8)(c&0x00FF);
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 20 20 20 20 20 20 20 20                  
adce0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
adcf0 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30  ut++ = (u8)(0x00
add00 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 30  DC + ((c>>8)&0x0
add10 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3));            
add20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
add30 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20   }              
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 20 20 20 20 20 20 20 20 20 20 20 20                  
add70 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20     \.}..#define 
add80 57 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 4f  WRITE_UTF16BE(zO
add90 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20  ut, c) {        
adda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addb0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
addc0 69 66 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b  if( c<=0xFFFF ){
addd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade00 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
ade10 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30 78  = (u8)((c>>8)&0x
ade20 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20  00FF);          
ade30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade40 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
ade50 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30  Out++ = (u8)(c&0
ade60 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20  x00FF);         
ade70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ade90 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20    }else{        
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 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aded0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
adee0 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 38 20  + = (u8)(0x00D8 
adef0 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e  + (((c-0x10000)>
adf00 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 20  >18)&0x03));    
adf10 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
adf20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28  *zOut++ = (u8)((
adf30 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29 20  (c>>10)&0x003F) 
adf40 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e  + (((c-0x10000)>
adf50 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b 20 20  >10)&0x00C0));  
adf60 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
adf70 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 28 28  (u8)(0x00DC + ((
adf80 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20  c>>8)&0x03));   
adf90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adfa0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
adfb0 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30  t++ = (u8)(c&0x0
adfc0 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  0FF);           
adfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adfe0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
adff0 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }               
ae000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae030 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52    \.}..#define R
ae040 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c  EAD_UTF16LE(zIn,
ae050 20 54 45 52 4d 2c 20 63 29 7b 20 20 20 20 20 20   TERM, c){      
ae060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae070 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ae080 20 63 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20   c = (*zIn++);  
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 20                  
ae0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0c0 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 28       \.  c += ((
ae0d0 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20  *zIn++)<<8);    
ae0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae100 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ae110 20 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20 26   if( c>=0xD800 &
ae120 26 20 63 3c 30 78 45 30 30 30 20 26 26 20 54 45  & c<0xE000 && TE
ae130 52 4d 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  RM ){           
ae140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae150 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63       \.    int c
ae160 32 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20  2 = (*zIn++);   
ae170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae190 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ae1a0 20 20 20 63 32 20 2b 3d 20 28 28 2a 7a 49 6e 2b     c2 += ((*zIn+
ae1b0 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20  +)<<8);         
ae1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1e0 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28       \.    c = (
ae1f0 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28 63  c2&0x03FF) + ((c
ae200 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20  &0x003F)<<10) + 
ae210 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78 30  (((c&0x03C0)+0x0
ae220 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20  040)<<10);   \. 
ae230 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20   }              
ae240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae270 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e       \.}..#defin
ae280 65 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a  e READ_UTF16BE(z
ae290 49 6e 2c 20 54 45 52 4d 2c 20 63 29 7b 20 20 20  In, TERM, c){   
ae2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae2c0 5c 0a 20 20 63 20 3d 20 28 28 2a 7a 49 6e 2b 2b  \.  c = ((*zIn++
ae2d0 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20  )<<8);          
ae2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae300 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d          \.  c +=
ae310 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20   (*zIn++);      
ae320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae350 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44 38 30  \.  if( c>=0xD80
ae360 30 20 26 26 20 63 3c 30 78 45 30 30 30 20 26 26  0 && c<0xE000 &&
ae370 20 54 45 52 4d 20 29 7b 20 20 20 20 20 20 20 20   TERM ){        
ae380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae390 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e          \.    in
ae3a0 74 20 63 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29  t c2 = ((*zIn++)
ae3b0 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20  <<8);           
ae3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae3e0 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 2a 7a 49  \.    c2 += (*zI
ae3f0 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20  n++);           
ae400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
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 5c 0a 20 20 20 20 63 20          \.    c 
ae430 3d 20 28 63 32 26 30 78 30 33 46 46 29 20 2b 20  = (c2&0x03FF) + 
ae440 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30 29  ((c&0x003F)<<10)
ae450 20 2b 20 28 28 28 63 26 30 78 30 33 43 30 29 2b   + (((c&0x03C0)+
ae460 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20 20  0x0040)<<10);   
ae470 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20  \.  }           
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 20 20 20 20 20                  
ae4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae4b0 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a          \.}../*.
ae4c0 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73  ** Translate a s
ae4d0 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61 72  ingle UTF-8 char
ae4e0 61 63 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74  acter.  Return t
ae4f0 68 65 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65  he unicode value
ae500 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74  ..**.** During t
ae510 72 61 6e 73 6c 61 74 69 6f 6e 2c 20 61 73 73 75  ranslation, assu
ae520 6d 65 20 74 68 61 74 20 74 68 65 20 62 79 74 65  me that the byte
ae530 20 74 68 61 74 20 7a 54 65 72 6d 20 70 6f 69 6e   that zTerm poin
ae540 74 73 0a 2a 2a 20 69 73 20 61 20 30 78 30 30 2e  ts.** is a 0x00.
ae550 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 70  .**.** Write a p
ae560 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
ae570 78 74 20 75 6e 72 65 61 64 20 62 79 74 65 20 62  xt unread byte b
ae580 61 63 6b 20 69 6e 74 6f 20 2a 70 7a 4e 65 78 74  ack into *pzNext
ae590 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 4f 6e  ..**.** Notes On
ae5a0 20 49 6e 76 61 6c 69 64 20 55 54 46 2d 38 3a 0a   Invalid UTF-8:.
ae5b0 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72  **.**  *  This r
ae5c0 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c  outine never all
ae5d0 6f 77 73 20 61 20 37 2d 62 69 74 20 63 68 61 72  ows a 7-bit char
ae5e0 61 63 74 65 72 20 28 30 78 30 30 20 74 68 72 6f  acter (0x00 thro
ae5f0 75 67 68 20 30 78 37 66 29 20 74 6f 0a 2a 2a 20  ugh 0x7f) to.** 
ae600 20 20 20 20 62 65 20 65 6e 63 6f 64 65 64 20 61      be encoded a
ae610 73 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63  s a multi-byte c
ae620 68 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 6d  haracter.  Any m
ae630 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63  ulti-byte charac
ae640 74 65 72 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  ter that.**     
ae650 61 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f  attempts to enco
ae660 64 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65  de a value betwe
ae670 65 6e 20 30 78 30 30 20 61 6e 64 20 30 78 37 66  en 0x00 and 0x7f
ae680 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 20   is rendered as 
ae690 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a  0xfffd..**.**  *
ae6a0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e    This routine n
ae6b0 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 55 54  ever allows a UT
ae6c0 46 31 36 20 73 75 72 72 6f 67 61 74 65 20 76 61  F16 surrogate va
ae6d0 6c 75 65 20 74 6f 20 62 65 20 65 6e 63 6f 64 65  lue to be encode
ae6e0 64 2e 0a 2a 2a 20 20 20 20 20 49 66 20 61 20 6d  d..**     If a m
ae6f0 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63  ulti-byte charac
ae700 74 65 72 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ter attempts to 
ae710 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20 62  encode a value b
ae720 65 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 30 78  etween.**     0x
ae730 64 38 30 30 20 61 6e 64 20 30 78 65 30 30 30 20  d800 and 0xe000 
ae740 74 68 65 6e 20 69 74 20 69 73 20 72 65 6e 64 65  then it is rende
ae750 72 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a  red as 0xfffd..*
ae760 2a 0a 2a 2a 20 20 2a 20 20 42 79 74 65 73 20 69  *.**  *  Bytes i
ae770 6e 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 30  n the range of 0
ae780 78 38 30 20 74 68 72 6f 75 67 68 20 30 78 62 66  x80 through 0xbf
ae790 20 77 68 69 63 68 20 6f 63 63 75 72 20 61 73 20   which occur as 
ae7a0 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 20 20 20  the first.**    
ae7b0 20 62 79 74 65 20 6f 66 20 61 20 63 68 61 72 61   byte of a chara
ae7c0 63 74 65 72 20 61 72 65 20 69 6e 74 65 72 70 72  cter are interpr
ae7d0 65 74 65 64 20 61 73 20 73 69 6e 67 6c 65 2d 62  eted as single-b
ae7e0 79 74 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a  yte characters.*
ae7f0 2a 20 20 20 20 20 61 6e 64 20 72 65 6e 64 65 72  *     and render
ae800 65 64 20 61 73 20 74 68 65 6d 73 65 6c 76 65 73  ed as themselves
ae810 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65   even though the
ae820 79 20 61 72 65 20 74 65 63 68 6e 69 63 61 6c 6c  y are technicall
ae830 79 0a 2a 2a 20 20 20 20 20 69 6e 76 61 6c 69 64  y.**     invalid
ae840 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a   characters..**.
ae850 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74  **  *  This rout
ae860 69 6e 65 20 61 63 63 65 70 74 73 20 61 6e 20 69  ine accepts an i
ae870 6e 66 69 6e 69 74 65 20 6e 75 6d 62 65 72 20 6f  nfinite number o
ae880 66 20 64 69 66 66 65 72 65 6e 74 20 55 54 46 38  f different UTF8
ae890 20 65 6e 63 6f 64 69 6e 67 73 0a 2a 2a 20 20 20   encodings.**   
ae8a0 20 20 66 6f 72 20 75 6e 69 63 6f 64 65 20 76 61    for unicode va
ae8b0 6c 75 65 73 20 30 78 38 30 20 61 6e 64 20 67 72  lues 0x80 and gr
ae8c0 65 61 74 65 72 2e 20 20 49 74 20 64 6f 20 6e 6f  eater.  It do no
ae8d0 74 20 63 68 61 6e 67 65 20 6f 76 65 72 2d 6c 65  t change over-le
ae8e0 6e 67 74 68 0a 2a 2a 20 20 20 20 20 65 6e 63 6f  ngth.**     enco
ae8f0 64 69 6e 67 73 20 74 6f 20 30 78 66 66 66 64 20  dings to 0xfffd 
ae900 61 73 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20  as some systems 
ae910 72 65 63 6f 6d 6d 65 6e 64 2e 0a 2a 2f 0a 23 64  recommend..*/.#d
ae920 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 38 28  efine READ_UTF8(
ae930 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 20 20  zIn, zTerm, c)  
ae940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae950 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d           \.  c =
ae960 20 2a 28 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20   *(zIn++);      
ae970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae990 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e        \.  if( c>
ae9a0 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 20  =0xc0 ){        
ae9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae9d0 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 73 71 6c     \.    c = sql
ae9e0 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b 63  ite3Utf8Trans1[c
ae9f0 2d 30 78 63 30 5d 3b 20 20 20 20 20 20 20 20 20  -0xc0];         
aea00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aea10 5c 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e  \.    while( zIn
aea20 21 3d 7a 54 65 72 6d 20 26 26 20 28 2a 7a 49 6e  !=zTerm && (*zIn
aea30 20 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29   & 0xc0)==0x80 )
aea40 7b 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20  {            \. 
aea50 20 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 29 20       c = (c<<6) 
aea60 2b 20 28 30 78 33 66 20 26 20 2a 28 7a 49 6e 2b  + (0x3f & *(zIn+
aea70 2b 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  +));            
aea80 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
aea90 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }               
aeaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeac0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 28         \.    if(
aead0 20 63 3c 30 78 38 30 20 20 20 20 20 20 20 20 20   c<0x80         
aeae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeb00 20 20 20 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c      \.        ||
aeb10 20 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d   (c&0xFFFFF800)=
aeb20 3d 30 78 44 38 30 30 20 20 20 20 20 20 20 20 20  =0xD800         
aeb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeb40 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63   \.        || (c
aeb50 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78  &0xFFFFFFFE)==0x
aeb60 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46  FFFE ){  c = 0xF
aeb70 46 46 44 3b 20 7d 20 20 20 20 20 20 20 20 5c 0a  FFD; }        \.
aeb80 20 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41    }.SQLITE_PRIVA
aeb90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74  TE int sqlite3Ut
aeba0 66 38 52 65 61 64 28 0a 20 20 63 6f 6e 73 74 20  f8Read(.  const 
aebb0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
aebc0 49 6e 2c 20 20 20 20 20 20 20 2f 2a 20 46 69 72  In,       /* Fir
aebd0 73 74 20 62 79 74 65 20 6f 66 20 55 54 46 2d 38  st byte of UTF-8
aebe0 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20   character */.  
aebf0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
aec00 68 61 72 20 2a 2a 70 7a 4e 65 78 74 20 20 20 20  har **pzNext    
aec10 2f 2a 20 57 72 69 74 65 20 66 69 72 73 74 20 62  /* Write first b
aec20 79 74 65 20 70 61 73 74 20 55 54 46 2d 38 20 63  yte past UTF-8 c
aec30 68 61 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  har here */.){. 
aec40 20 69 6e 74 20 63 3b 0a 0a 20 20 2f 2a 20 53 61   int c;..  /* Sa
aec50 6d 65 20 61 73 20 52 45 41 44 5f 55 54 46 38 28  me as READ_UTF8(
aec60 29 20 61 62 6f 76 65 20 62 75 74 20 77 69 74 68  ) above but with
aec70 6f 75 74 20 74 68 65 20 7a 54 65 72 6d 20 70 61  out the zTerm pa
aec80 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 46 6f  rameter..  ** Fo
aec90 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  r this routine, 
aeca0 77 65 20 61 73 73 75 6d 65 20 74 68 65 20 55 54  we assume the UT
aecb0 46 38 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77  F8 string is alw
aecc0 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  ays zero-termina
aecd0 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 63 20 3d 20  ted..  */.  c = 
aece0 2a 28 7a 49 6e 2b 2b 29 3b 0a 20 20 69 66 28 20  *(zIn++);.  if( 
aecf0 63 3e 3d 30 78 63 30 20 29 7b 0a 20 20 20 20 63  c>=0xc0 ){.    c
aed00 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54 72   = sqlite3Utf8Tr
aed10 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 0a 20 20  ans1[c-0xc0];.  
aed20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26    while( (*zIn &
aed30 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 0a   0xc0)==0x80 ){.
aed40 20 20 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 29        c = (c<<6)
aed50 20 2b 20 28 30 78 33 66 20 26 20 2a 28 7a 49 6e   + (0x3f & *(zIn
aed60 2b 2b 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ++));.    }.    
aed70 69 66 28 20 63 3c 30 78 38 30 0a 20 20 20 20 20  if( c<0x80.     
aed80 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46     || (c&0xFFFFF
aed90 38 30 30 29 3d 3d 30 78 44 38 30 30 0a 20 20 20  800)==0xD800.   
aeda0 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46       || (c&0xFFF
aedb0 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29  FFFFE)==0xFFFE )
aedc0 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 7d  {  c = 0xFFFD; }
aedd0 0a 20 20 7d 0a 20 20 2a 70 7a 4e 65 78 74 20 3d  .  }.  *pzNext =
aede0 20 7a 49 6e 3b 0a 20 20 72 65 74 75 72 6e 20 63   zIn;.  return c
aedf0 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66  ;.}...../*.** If
aee00 20 74 68 65 20 54 52 41 4e 53 4c 41 54 45 5f 54   the TRANSLATE_T
aee10 52 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64 65  RACE macro is de
aee20 66 69 6e 65 64 2c 20 74 68 65 20 76 61 6c 75 65  fined, the value
aee30 20 6f 66 20 65 61 63 68 20 4d 65 6d 20 69 73 0a   of each Mem is.
aee40 2a 2a 20 70 72 69 6e 74 65 64 20 6f 6e 20 73 74  ** printed on st
aee50 64 65 72 72 20 6f 6e 20 74 68 65 20 77 61 79 20  derr on the way 
aee60 69 6e 74 6f 20 61 6e 64 20 6f 75 74 20 6f 66 20  into and out of 
aee70 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72  sqlite3VdbeMemTr
aee80 61 6e 73 6c 61 74 65 28 29 2e 0a 2a 2f 20 0a 2f  anslate()..*/ ./
aee90 2a 20 23 64 65 66 69 6e 65 20 54 52 41 4e 53 4c  * #define TRANSL
aeea0 41 54 45 5f 54 52 41 43 45 20 31 20 2a 2f 0a 0a  ATE_TRACE 1 */..
aeeb0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
aeec0 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20  MIT_UTF16./*.** 
aeed0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61  This routine tra
aeee0 6e 73 66 6f 72 6d 73 20 74 68 65 20 69 6e 74 65  nsforms the inte
aeef0 72 6e 61 6c 20 74 65 78 74 20 65 6e 63 6f 64 69  rnal text encodi
aef00 6e 67 20 75 73 65 64 20 62 79 20 70 4d 65 6d 20  ng used by pMem 
aef10 74 6f 0a 2a 2a 20 64 65 73 69 72 65 64 45 6e 63  to.** desiredEnc
aef20 2e 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72  . It is an error
aef30 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 69   if the string i
aef40 73 20 61 6c 72 65 61 64 79 20 6f 66 20 74 68 65  s already of the
aef50 20 64 65 73 69 72 65 64 0a 2a 2a 20 65 6e 63 6f   desired.** enco
aef60 64 69 6e 67 2c 20 6f 72 20 69 66 20 2a 70 4d 65  ding, or if *pMe
aef70 6d 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61  m does not conta
aef80 69 6e 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75  in a string valu
aef90 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
aefa0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
aefb0 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65  VdbeMemTranslate
aefc0 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 75 38 20 64  (Mem *pMem, u8 d
aefd0 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e  esiredEnc){.  in
aefe0 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20  t len;          
aeff0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
af000 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 6f  imum length of o
af010 75 74 70 75 74 20 73 74 72 69 6e 67 20 69 6e 20  utput string in 
af020 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67  bytes */.  unsig
af030 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 3b 20  ned char *zOut; 
af040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
af050 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65   /* Output buffe
af060 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  r */.  unsigned 
af070 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20 20  char *zIn;      
af080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
af090 49 6e 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a  Input iterator *
af0a0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
af0b0 72 20 2a 7a 54 65 72 6d 3b 20 20 20 20 20 20 20  r *zTerm;       
af0c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64            /* End
af0d0 20 6f 66 20 69 6e 70 75 74 20 2a 2f 0a 20 20 75   of input */.  u
af0e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b  nsigned char *z;
af0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
af100 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 69       /* Output i
af110 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73  terator */.  uns
af120 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20  igned int c;..  
af130 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
af140 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
af150 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
af160 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
af170 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c  assert( pMem->fl
af180 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 3b 0a 20  ags&MEM_Str );. 
af190 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65   assert( pMem->e
af1a0 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63 20 29  nc!=desiredEnc )
af1b0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ;.  assert( pMem
af1c0 2d 3e 65 6e 63 21 3d 30 20 29 3b 0a 20 20 61 73  ->enc!=0 );.  as
af1d0 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30  sert( pMem->n>=0
af1e0 20 29 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64   );..#if defined
af1f0 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45  (TRANSLATE_TRACE
af200 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
af210 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20  ITE_DEBUG).  {. 
af220 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30     char zBuf[100
af230 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
af240 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
af250 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20  (pMem, zBuf);.  
af260 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
af270 2c 20 22 49 4e 50 55 54 3a 20 20 25 73 5c 6e 22  , "INPUT:  %s\n"
af280 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e  , zBuf);.  }.#en
af290 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  dif..  /* If the
af2a0 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20   translation is 
af2b0 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 20 6c  between UTF-16 l
af2c0 69 74 74 6c 65 20 61 6e 64 20 62 69 67 20 65 6e  ittle and big en
af2d0 64 69 61 6e 2c 20 74 68 65 6e 20 0a 20 20 2a 2a  dian, then .  **
af2e0 20 61 6c 6c 20 74 68 61 74 20 69 73 20 72 65 71   all that is req
af2f0 75 69 72 65 64 20 69 73 20 74 6f 20 73 77 61 70  uired is to swap
af300 20 74 68 65 20 62 79 74 65 20 6f 72 64 65 72 2e   the byte order.
af310 20 54 68 69 73 20 63 61 73 65 20 69 73 20 68 61   This case is ha
af320 6e 64 6c 65 64 0a 20 20 2a 2a 20 64 69 66 66 65  ndled.  ** diffe
af330 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65 20  rently from the 
af340 6f 74 68 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69  others..  */.  i
af350 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 53 51  f( pMem->enc!=SQ
af360 4c 49 54 45 5f 55 54 46 38 20 26 26 20 64 65 73  LITE_UTF8 && des
af370 69 72 65 64 45 6e 63 21 3d 53 51 4c 49 54 45 5f  iredEnc!=SQLITE_
af380 55 54 46 38 20 29 7b 0a 20 20 20 20 75 38 20 74  UTF8 ){.    u8 t
af390 65 6d 70 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b  emp;.    int rc;
af3a0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
af3b0 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
af3c0 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 20  eable(pMem);.   
af3d0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
af3e0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  OK ){.      asse
af3f0 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  rt( rc==SQLITE_N
af400 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 72 65  OMEM );.      re
af410 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
af420 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 49 6e  M;.    }.    zIn
af430 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a 3b   = (u8*)pMem->z;
af440 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 49  .    zTerm = &zI
af450 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e 31 5d 3b 0a 20  n[pMem->n&~1];. 
af460 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54     while( zIn<zT
af470 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 74 65 6d  erm ){.      tem
af480 70 20 3d 20 2a 7a 49 6e 3b 0a 20 20 20 20 20 20  p = *zIn;.      
af490 2a 7a 49 6e 20 3d 20 2a 28 7a 49 6e 2b 31 29 3b  *zIn = *(zIn+1);
af4a0 0a 20 20 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20  .      zIn++;.  
af4b0 20 20 20 20 2a 7a 49 6e 2b 2b 20 3d 20 74 65 6d      *zIn++ = tem
af4c0 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65  p;.    }.    pMe
af4d0 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 64  m->enc = desired
af4e0 45 6e 63 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72  Enc;.    goto tr
af4f0 61 6e 73 6c 61 74 65 5f 6f 75 74 3b 0a 20 20 7d  anslate_out;.  }
af500 0a 0a 20 20 2f 2a 20 53 65 74 20 6c 65 6e 20 74  ..  /* Set len t
af510 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  o the maximum nu
af520 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
af530 71 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f 75  quired in the ou
af540 74 70 75 74 20 62 75 66 66 65 72 2e 20 2a 2f 0a  tput buffer. */.
af550 20 20 69 66 28 20 64 65 73 69 72 65 64 45 6e 63    if( desiredEnc
af560 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b  ==SQLITE_UTF8 ){
af570 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e  .    /* When con
af580 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 55 54 46  verting from UTF
af590 2d 31 36 2c 20 74 68 65 20 6d 61 78 69 6d 75 6d  -16, the maximum
af5a0 20 67 72 6f 77 74 68 20 72 65 73 75 6c 74 73 20   growth results 
af5b0 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74 72 61 6e  from.    ** tran
af5c0 73 6c 61 74 69 6e 67 20 61 20 32 2d 62 79 74 65  slating a 2-byte
af5d0 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 61 20   character to a 
af5e0 34 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61  4-byte UTF-8 cha
af5f0 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 41  racter..    ** A
af600 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 73 20   single byte is 
af610 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65  required for the
af620 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67 0a 20   output string. 
af630 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e     ** nul-termin
af640 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ator..    */.   
af650 20 70 4d 65 6d 2d 3e 6e 20 26 3d 20 7e 31 3b 0a   pMem->n &= ~1;.
af660 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e      len = pMem->
af670 6e 20 2a 20 32 20 2b 20 31 3b 0a 20 20 7d 65 6c  n * 2 + 1;.  }el
af680 73 65 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20  se{.    /* When 
af690 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d 20  converting from 
af6a0 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31 36 20  UTF-8 to UTF-16 
af6b0 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77  the maximum grow
af6c0 74 68 20 69 73 20 63 61 75 73 65 64 0a 20 20 20  th is caused.   
af6d0 20 2a 2a 20 77 68 65 6e 20 61 20 31 2d 62 79 74   ** when a 1-byt
af6e0 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  e UTF-8 characte
af6f0 72 20 69 73 20 74 72 61 6e 73 6c 61 74 65 64 20  r is translated 
af700 69 6e 74 6f 20 61 20 32 2d 62 79 74 65 20 55 54  into a 2-byte UT
af710 46 2d 31 36 0a 20 20 20 20 2a 2a 20 63 68 61 72  F-16.    ** char
af720 61 63 74 65 72 2e 20 54 77 6f 20 62 79 74 65 73  acter. Two bytes
af730 20 61 72 65 20 72 65 71 75 69 72 65 64 20 69 6e   are required in
af740 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
af750 65 72 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a  er for the.    *
af760 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72  * nul-terminator
af770 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e  ..    */.    len
af780 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b   = pMem->n * 2 +
af790 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65   2;.  }..  /* Se
af7a0 74 20 7a 49 6e 20 74 6f 20 70 6f 69 6e 74 20 61  t zIn to point a
af7b0 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
af7c0 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 20  he input buffer 
af7d0 61 6e 64 20 7a 54 65 72 6d 20 74 6f 20 70 6f 69  and zTerm to poi
af7e0 6e 74 20 31 0a 20 20 2a 2a 20 62 79 74 65 20 70  nt 1.  ** byte p
af7f0 61 73 74 20 74 68 65 20 65 6e 64 2e 0a 20 20 2a  ast the end..  *
af800 2a 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20  *.  ** Variable 
af810 7a 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 70  zOut is set to p
af820 6f 69 6e 74 20 61 74 20 74 68 65 20 6f 75 74 70  oint at the outp
af830 75 74 20 62 75 66 66 65 72 2c 20 73 70 61 63 65  ut buffer, space
af840 20 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 66   obtained.  ** f
af850 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
af860 6f 63 28 29 2e 0a 20 20 2a 2f 0a 20 20 7a 49 6e  oc()..  */.  zIn
af870 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a 3b   = (u8*)pMem->z;
af880 0a 20 20 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b  .  zTerm = &zIn[
af890 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20 7a 4f 75 74  pMem->n];.  zOut
af8a0 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
af8b0 6f 63 52 61 77 28 70 4d 65 6d 2d 3e 64 62 2c 20  ocRaw(pMem->db, 
af8c0 6c 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a 4f 75  len);.  if( !zOu
af8d0 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
af8e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
af8f0 7d 0a 20 20 7a 20 3d 20 7a 4f 75 74 3b 0a 0a 20  }.  z = zOut;.. 
af900 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d   if( pMem->enc==
af910 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20  SQLITE_UTF8 ){. 
af920 20 20 20 69 66 28 20 64 65 73 69 72 65 64 45 6e     if( desiredEn
af930 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  c==SQLITE_UTF16L
af940 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54  E ){.      /* UT
af950 46 2d 38 20 2d 3e 20 55 54 46 2d 31 36 20 4c 69  F-8 -> UTF-16 Li
af960 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2a 2f 0a 20  ttle-endian */. 
af970 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c       while( zIn<
af980 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20  zTerm ){.       
af990 20 2f 2a 20 63 20 3d 20 73 71 6c 69 74 65 33 55   /* c = sqlite3U
af9a0 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54 65  tf8Read(zIn, zTe
af9b0 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29  rm, (const u8**)
af9c0 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 20 20 20 20  &zIn); */.      
af9d0 20 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c    READ_UTF8(zIn,
af9e0 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 20 20   zTerm, c);.    
af9f0 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 4c      WRITE_UTF16L
afa00 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d  E(z, c);.      }
afa10 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
afa20 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65    assert( desire
afa30 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc==SQLITE_UTF
afa40 31 36 42 45 20 29 3b 0a 20 20 20 20 20 20 2f 2a  16BE );.      /*
afa50 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31 36   UTF-8 -> UTF-16
afa60 20 42 69 67 2d 65 6e 64 69 61 6e 20 2a 2f 0a 20   Big-endian */. 
afa70 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c       while( zIn<
afa80 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20  zTerm ){.       
afa90 20 2f 2a 20 63 20 3d 20 73 71 6c 69 74 65 33 55   /* c = sqlite3U
afaa0 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54 65  tf8Read(zIn, zTe
afab0 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29  rm, (const u8**)
afac0 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 20 20 20 20  &zIn); */.      
afad0 20 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c    READ_UTF8(zIn,
afae0 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 20 20   zTerm, c);.    
afaf0 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 42      WRITE_UTF16B
afb00 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d  E(z, c);.      }
afb10 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
afb20 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a  >n = (int)(z - z
afb30 4f 75 74 29 3b 0a 20 20 20 20 2a 7a 2b 2b 20 3d  Out);.    *z++ =
afb40 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
afb50 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64   assert( desired
afb60 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  Enc==SQLITE_UTF8
afb70 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d   );.    if( pMem
afb80 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
afb90 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f  F16LE ){.      /
afba0 2a 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d  * UTF-16 Little-
afbb0 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d 38 20  endian -> UTF-8 
afbc0 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
afbd0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  zIn<zTerm ){.   
afbe0 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c       READ_UTF16L
afbf0 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54 65 72 6d  E(zIn, zIn<zTerm
afc00 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 20 20 57  , c); .        W
afc10 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 63 29 3b  RITE_UTF8(z, c);
afc20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
afc30 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46  se{.      /* UTF
afc40 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e 20 2d  -16 Big-endian -
afc50 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20  > UTF-8 */.     
afc60 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72   while( zIn<zTer
afc70 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41  m ){.        REA
afc80 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 7a  D_UTF16BE(zIn, z
afc90 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b 20 0a 20  In<zTerm, c); . 
afca0 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46         WRITE_UTF
afcb0 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d  8(z, c);.      }
afcc0 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
afcd0 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a  >n = (int)(z - z
afce0 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d  Out);.  }.  *z =
afcf0 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   0;.  assert( (p
afd00 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45  Mem->n+(desiredE
afd10 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f  nc==SQLITE_UTF8?
afd20 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20  1:2))<=len );.. 
afd30 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
afd40 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
afd50 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pMem->flags &= ~
afd60 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
afd70 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a  Dyn|MEM_Ephem);.
afd80 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65    pMem->enc = de
afd90 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 6d  siredEnc;.  pMem
afda0 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d 5f  ->flags |= (MEM_
afdb0 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a 20  Term|MEM_Dyn);. 
afdc0 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72   pMem->z = (char
afdd0 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d 3e  *)zOut;.  pMem->
afde0 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e  zMalloc = pMem->
afdf0 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75  z;..translate_ou
afe00 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54  t:.#if defined(T
afe10 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20  RANSLATE_TRACE) 
afe20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
afe30 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20  E_DEBUG).  {.   
afe40 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b   char zBuf[100];
afe50 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
afe60 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70  MemPrettyPrint(p
afe70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20  Mem, zBuf);.    
afe80 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
afe90 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20  "OUTPUT: %s\n", 
afea0 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  zBuf);.  }.#endi
afeb0 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  f.  return SQLIT
afec0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
afed0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
afee0 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72  ks for a byte-or
afef0 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20  der mark at the 
aff00 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
aff10 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69   .** UTF-16 stri
aff20 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d  ng stored in *pM
aff30 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72  em. If one is pr
aff40 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d  esent, it is rem
aff50 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20  oved and.** the 
aff60 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20  encoding of the 
aff70 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68  Mem adjusted. Th
aff80 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  is routine does 
aff90 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79  not do any.** by
affa0 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20  te-swapping, it 
affb0 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e  just sets Mem.en
affc0 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  c appropriately.
affd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63  .**.** The alloc
affe0 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64  ation (static, d
afff0 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64  ynamic etc.) and
b0000 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
b0010 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63   Mem may be.** c
b0020 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 66  hanged by this f
b0030 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  unction..*/.SQLI
b0040 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b0050 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e  qlite3VdbeMemHan
b0060 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d  dleBom(Mem *pMem
b0070 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
b0080 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f  LITE_OK;.  u8 bo
b0090 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  m = 0;..  assert
b00a0 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a  ( pMem->n>=0 );.
b00b0 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31 20    if( pMem->n>1 
b00c0 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20 2a  ){.    u8 b1 = *
b00d0 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20  (u8 *)pMem->z;. 
b00e0 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28 75     u8 b2 = *(((u
b00f0 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 31  8 *)pMem->z) + 1
b0100 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30  );.    if( b1==0
b0110 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46 20  xFE && b2==0xFF 
b0120 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53  ){.      bom = S
b0130 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20  QLITE_UTF16BE;. 
b0140 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31 3d     }.    if( b1=
b0150 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78 46  =0xFF && b2==0xF
b0160 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d  E ){.      bom =
b0170 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b   SQLITE_UTF16LE;
b0180 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20  .    }.  }.  .  
b0190 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20 72  if( bom ){.    r
b01a0 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
b01b0 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
b01c0 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72  pMem);.    if( r
b01d0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
b01e0 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d 3d        pMem->n -=
b01f0 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76   2;.      memmov
b0200 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d  e(pMem->z, &pMem
b0210 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 29  ->z[2], pMem->n)
b0220 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b  ;.      pMem->z[
b0230 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 3b  pMem->n] = '\0';
b0240 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70  .      pMem->z[p
b0250 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 27  Mem->n+1] = '\0'
b0260 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ;.      pMem->fl
b0270 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
b0280 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63  .      pMem->enc
b0290 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20   = bom;.    }.  
b02a0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
b02b0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
b02c0 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
b02d0 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55  ./*.** pZ is a U
b02e0 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e 69  TF-8 encoded uni
b02f0 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20  code string. If 
b0300 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20 74 68  nByte is less th
b0310 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75  an zero,.** retu
b0320 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
b0330 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74   unicode charact
b0340 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74 6f 20  ers in pZ up to 
b0350 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69  (but not includi
b0360 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  ng).** the first
b0370 20 30 78 30 30 20 62 79 74 65 2e 20 49 66 20 6e   0x00 byte. If n
b0380 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 73 73  Byte is not less
b0390 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74 75   than zero, retu
b03a0 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  rn the.** number
b03b0 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72   of unicode char
b03c0 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 66 69  acters in the fi
b03d0 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70 5a 20  rst nByte of pZ 
b03e0 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68  (or up to .** th
b03f0 65 20 66 69 72 73 74 20 30 78 30 30 2c 20 77 68  e first 0x00, wh
b0400 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69  ichever comes fi
b0410 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rst)..*/.SQLITE_
b0420 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b0430 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63  te3Utf8CharLen(c
b0440 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20  onst char *zIn, 
b0450 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e  int nByte){.  in
b0460 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74  t r = 0;.  const
b0470 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20   u8 *z = (const 
b0480 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 74  u8*)zIn;.  const
b0490 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 66   u8 *zTerm;.  if
b04a0 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20 20  ( nByte>=0 ){.  
b04b0 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79    zTerm = &z[nBy
b04c0 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  te];.  }else{.  
b04d0 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74    zTerm = (const
b04e0 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a 20   u8*)(-1);.  }. 
b04f0 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65 72   assert( z<=zTer
b0500 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a  m );.  while( *z
b0510 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20 29  !=0 && z<zTerm )
b0520 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49  {.    SQLITE_SKI
b0530 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 72  P_UTF8(z);.    r
b0540 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ++;.  }.  return
b0550 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 74   r;.}../* This t
b0560 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  est function is 
b0570 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 75 73  not currently us
b0580 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 6d 61  ed by the automa
b0590 74 65 64 20 74 65 73 74 2d 73 75 69 74 65 2e 20  ted test-suite. 
b05a0 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 73 20  .** Hence it is 
b05b0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
b05c0 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73 2e 0a  n debug builds..
b05d0 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
b05e0 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64  QLITE_TEST) && d
b05f0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
b0600 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  BUG)./*.** Trans
b0610 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55 54  late UTF-8 to UT
b0620 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  F-8..**.** This 
b0630 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f  has the effect o
b0640 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68  f making sure th
b0650 61 74 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  at the string is
b0660 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20   well-formed.** 
b0670 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65 64  UTF-8.  Miscoded
b0680 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20   characters are 
b0690 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  removed..**.** T
b06a0 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69  he translation i
b06b0 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 20  s done in-place 
b06c0 28 73 69 6e 63 65 20 69 74 20 69 73 20 69 6d 70  (since it is imp
b06d0 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 0a  ossible for the.
b06e0 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46 2d 38  ** correct UTF-8
b06f0 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20   encoding to be 
b0700 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 6d 61  longer than a ma
b0710 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e 67  lformed encoding
b0720 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
b0730 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b0740 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64  Utf8To8(unsigned
b0750 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75   char *zIn){.  u
b0760 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f  nsigned char *zO
b0770 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 69  ut = zIn;.  unsi
b0780 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61 72  gned char *zStar
b0790 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 63  t = zIn;.  u32 c
b07a0 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b  ;..  while( zIn[
b07b0 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71  0] ){.    c = sq
b07c0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49  lite3Utf8Read(zI
b07d0 6e 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26  n, (const u8**)&
b07e0 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63 21  zIn);.    if( c!
b07f0 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20 20  =0xfffd ){.     
b0800 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74   WRITE_UTF8(zOut
b0810 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , c);.    }.  }.
b0820 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20 72    *zOut = 0;.  r
b0830 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 74  eturn (int)(zOut
b0840 20 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23 65   - zStart);.}.#e
b0850 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
b0860 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
b0870 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
b0880 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e  UTF-16 string in
b0890 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63 6f   the native enco
b08a0 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 2d  ding into a UTF-
b08b0 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 6d  8 string..** Mem
b08c0 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
b08d0 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73 20  UTF-8 string is 
b08e0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
b08f0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e 64  lite3_malloc and
b0900 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 65   must.** be free
b0910 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  d by the calling
b0920 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
b0930 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
b0940 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  d if there is an
b0950 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
b0960 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
b0970 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
b0980 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74  e3Utf16to8(sqlit
b0990 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 6f  e3 *db, const vo
b09a0 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 65  id *z, int nByte
b09b0 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65  ){.  Mem m;.  me
b09c0 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65  mset(&m, 0, size
b09d0 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d  of(m));.  m.db =
b09e0 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   db;.  sqlite3Vd
b09f0 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20  beMemSetStr(&m, 
b0a00 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 45  z, nByte, SQLITE
b0a10 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51  _UTF16NATIVE, SQ
b0a20 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
b0a30 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
b0a40 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 51  eEncoding(&m, SQ
b0a50 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66  LITE_UTF8);.  if
b0a60 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
b0a70 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ed ){.    sqlite
b0a80 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
b0a90 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 30  &m);.    m.z = 0
b0aa0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
b0ab0 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54  (m.flags & MEM_T
b0ac0 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d  erm)!=0 || db->m
b0ad0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
b0ae0 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67   assert( (m.flag
b0af0 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20  s & MEM_Str)!=0 
b0b00 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
b0b10 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  led );.  return 
b0b20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44  (m.flags & MEM_D
b0b30 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 73  yn)!=0 ? m.z : s
b0b40 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
b0b50 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  b, m.z);.}../*.*
b0b60 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d  * Convert a UTF-
b0b70 38 20 73 74 72 69 6e 67 20 74 6f 20 74 68 65 20  8 string to the 
b0b80 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20  UTF-16 encoding 
b0b90 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72  specified by par
b0ba0 61 6d 65 74 65 72 0a 2a 2a 20 65 6e 63 2e 20 41  ameter.** enc. A
b0bb0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
b0bc0 6e 65 77 20 73 74 72 69 6e 67 20 69 73 20 72 65  new string is re
b0bd0 74 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65 20  turned, and the 
b0be0 76 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74 0a  value of *pnOut.
b0bf0 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ** is set to the
b0c00 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 72   length of the r
b0c10 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
b0c20 6e 20 62 79 74 65 73 2e 20 54 68 65 20 63 61 6c  n bytes. The cal
b0c30 6c 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 72 72 61  l should.** arra
b0c40 6e 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69  nge to call sqli
b0c50 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20 74  te3DbFree() on t
b0c60 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 6e  he returned poin
b0c70 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a 2a  ter when it is.*
b0c80 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75  * no longer requ
b0c90 69 72 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20  ired..** .** If 
b0ca0 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  a malloc failure
b0cb0 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73   occurs, NULL is
b0cc0 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
b0cd0 65 20 64 62 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65  e db.mallocFaile
b0ce0 64 0a 2a 2a 20 66 6c 61 67 20 73 65 74 2e 0a 2a  d.** flag set..*
b0cf0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
b0d00 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51 4c  ENABLE_STAT2.SQL
b0d10 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
b0d20 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f 31   *sqlite3Utf8to1
b0d30 36 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 75  6(sqlite3 *db, u
b0d40 38 20 65 6e 63 2c 20 63 68 61 72 20 2a 7a 2c 20  8 enc, char *z, 
b0d50 69 6e 74 20 6e 2c 20 69 6e 74 20 2a 70 6e 4f 75  int n, int *pnOu
b0d60 74 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d  t){.  Mem m;.  m
b0d70 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a  emset(&m, 0, siz
b0d80 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20  eof(m));.  m.db 
b0d90 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56  = db;.  sqlite3V
b0da0 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c  dbeMemSetStr(&m,
b0db0 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
b0dc0 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  F8, SQLITE_STATI
b0dd0 43 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  C);.  if( sqlite
b0de0 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74  3VdbeMemTranslat
b0df0 65 28 26 6d 2c 20 65 6e 63 29 20 29 7b 0a 20 20  e(&m, enc) ){.  
b0e00 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61    assert( db->ma
b0e10 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
b0e20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
b0e30 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d 6d    assert( m.z==m
b0e40 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 2a 70  .zMalloc );.  *p
b0e50 6e 4f 75 74 20 3d 20 6d 2e 6e 3b 0a 20 20 72 65  nOut = m.n;.  re
b0e60 74 75 72 6e 20 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64  turn m.z;.}.#end
b0e70 69 66 0a 0a 2f 2a 0a 2a 2a 20 7a 49 6e 20 69 73  if../*.** zIn is
b0e80 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65   a UTF-16 encode
b0e90 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67  d unicode string
b0ea0 20 61 74 20 6c 65 61 73 74 20 6e 43 68 61 72 20   at least nChar 
b0eb0 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e  characters long.
b0ec0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
b0ed0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
b0ee0 6e 20 74 68 65 20 66 69 72 73 74 20 6e 43 68 61  n the first nCha
b0ef0 72 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63  r unicode charac
b0f00 74 65 72 73 0a 2a 2a 20 69 6e 20 70 5a 2e 20 20  ters.** in pZ.  
b0f10 6e 43 68 61 72 20 6d 75 73 74 20 62 65 20 6e 6f  nChar must be no
b0f20 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2f 0a 53  n-negative..*/.S
b0f30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b0f40 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79  t sqlite3Utf16By
b0f50 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64  teLen(const void
b0f60 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72   *zIn, int nChar
b0f70 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 75 6e  ){.  int c;.  un
b0f80 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
b0f90 74 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e  t *z = zIn;.  in
b0fa0 74 20 6e 20 3d 20 30 3b 0a 20 20 0a 20 20 69 66  t n = 0;.  .  if
b0fb0 28 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  ( SQLITE_UTF16NA
b0fc0 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46  TIVE==SQLITE_UTF
b0fd0 31 36 42 45 20 29 7b 0a 20 20 20 20 77 68 69 6c  16BE ){.    whil
b0fe0 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0a 20 20  e( n<nChar ){.  
b0ff0 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42 45      READ_UTF16BE
b1000 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 20  (z, 1, c);.     
b1010 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65   n++;.    }.  }e
b1020 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20  lse{.    while( 
b1030 6e 3c 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20  n<nChar ){.     
b1040 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c   READ_UTF16LE(z,
b1050 20 31 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b   1, c);.      n+
b1060 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  +;.    }.  }.  r
b1070 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 2d 28 75  eturn (int)(z-(u
b1080 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e  nsigned char con
b1090 73 74 20 2a 29 7a 49 6e 29 3b 0a 7d 0a 0a 23 69  st *)zIn);.}..#i
b10a0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
b10b0 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69  _TEST)./*.** Thi
b10c0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
b10d0 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 54 43 4c  led from the TCL
b10e0 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 22   test function "
b10f0 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c 66 74 65  translate_selfte
b1100 73 74 22 2e 0a 2a 2a 20 49 74 20 63 68 65 63 6b  st"..** It check
b1110 73 20 74 68 61 74 20 74 68 65 20 70 72 69 6d 69  s that the primi
b1120 74 69 76 65 73 20 66 6f 72 20 73 65 72 69 61 6c  tives for serial
b1130 69 7a 69 6e 67 20 61 6e 64 20 64 65 73 65 72 69  izing and deseri
b1140 61 6c 69 7a 69 6e 67 0a 2a 2a 20 63 68 61 72 61  alizing.** chara
b1150 63 74 65 72 73 20 69 6e 20 65 61 63 68 20 65 6e  cters in each en
b1160 63 6f 64 69 6e 67 20 61 72 65 20 69 6e 76 65 72  coding are inver
b1170 73 65 73 20 6f 66 20 65 61 63 68 20 6f 74 68 65  ses of each othe
b1180 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
b1190 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
b11a0 33 55 74 66 53 65 6c 66 54 65 73 74 28 76 6f 69  3UtfSelfTest(voi
b11b0 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  d){.  unsigned i
b11c0 6e 74 20 69 2c 20 74 3b 0a 20 20 75 6e 73 69 67  nt i, t;.  unsig
b11d0 6e 65 64 20 63 68 61 72 20 7a 42 75 66 5b 32 30  ned char zBuf[20
b11e0 5d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ];.  unsigned ch
b11f0 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 6e 3b 0a  ar *z;.  int n;.
b1200 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63    unsigned int c
b1210 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
b1220 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29  0x00110000; i++)
b1230 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a  {.    z = zBuf;.
b1240 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a      WRITE_UTF8(z
b1250 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69  , i);.    n = (i
b1260 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20  nt)(z-zBuf);.   
b1270 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20   assert( n>0 && 
b1280 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d  n<=4 );.    z[0]
b1290 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42   = 0;.    z = zB
b12a0 75 66 3b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69  uf;.    c = sqli
b12b0 74 65 33 55 74 66 38 52 65 61 64 28 7a 2c 20 28  te3Utf8Read(z, (
b12c0 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 29 3b 0a  const u8**)&z);.
b12d0 20 20 20 20 74 20 3d 20 69 3b 0a 20 20 20 20 69      t = i;.    i
b12e0 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20  f( i>=0xD800 && 
b12f0 69 3c 3d 30 78 44 46 46 46 20 29 20 74 20 3d 20  i<=0xDFFF ) t = 
b1300 30 78 46 46 46 44 3b 0a 20 20 20 20 69 66 28 20  0xFFFD;.    if( 
b1310 28 69 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d  (i&0xFFFFFFFE)==
b1320 30 78 46 46 46 45 20 29 20 74 20 3d 20 30 78 46  0xFFFE ) t = 0xF
b1330 46 46 44 3b 0a 20 20 20 20 61 73 73 65 72 74 28  FFD;.    assert(
b1340 20 63 3d 3d 74 20 29 3b 0a 20 20 20 20 61 73 73   c==t );.    ass
b1350 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e  ert( (z-zBuf)==n
b1360 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d   );.  }.  for(i=
b1370 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b  0; i<0x00110000;
b1380 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69   i++){.    if( i
b1390 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30 78  >=0xD800 && i<0x
b13a0 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  E000 ) continue;
b13b0 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20  .    z = zBuf;. 
b13c0 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45     WRITE_UTF16LE
b13d0 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20  (z, i);.    n = 
b13e0 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20  (int)(z-zBuf);. 
b13f0 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26     assert( n>0 &
b1400 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b  & n<=4 );.    z[
b1410 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20  0] = 0;.    z = 
b1420 7a 42 75 66 3b 0a 20 20 20 20 52 45 41 44 5f 55  zBuf;.    READ_U
b1430 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63 29 3b  TF16LE(z, 1, c);
b1440 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d  .    assert( c==
b1450 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  i );.    assert(
b1460 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a   (z-zBuf)==n );.
b1470 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
b1480 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b  <0x00110000; i++
b1490 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78  ){.    if( i>=0x
b14a0 44 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30  D800 && i<0xE000
b14b0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
b14c0 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57   z = zBuf;.    W
b14d0 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c 20  RITE_UTF16BE(z, 
b14e0 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74  i);.    n = (int
b14f0 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61  )(z-zBuf);.    a
b1500 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c  ssert( n>0 && n<
b1510 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d  =4 );.    z[0] =
b1520 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66   0;.    z = zBuf
b1530 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36  ;.    READ_UTF16
b1540 42 45 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20  BE(z, 1, c);.   
b1550 20 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b   assert( c==i );
b1560 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d  .    assert( (z-
b1570 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a  zBuf)==n );.  }.
b1580 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
b1590 54 45 5f 54 45 53 54 20 2a 2f 0a 23 65 6e 64 69  TE_TEST */.#endi
b15a0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
b15b0 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _UTF16 */../****
b15c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
b15d0 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  f utf.c ********
b15e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b15f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
b1610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
b1620 20 66 69 6c 65 20 75 74 69 6c 2e 63 20 2a 2a 2a   file util.c ***
b1630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
b1660 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
b1670 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
b1680 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
b1690 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
b16a0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
b16b0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
b16c0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
b16d0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
b16e0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
b16f0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
b1700 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
b1710 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
b1720 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
b1730 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
b1740 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
b1750 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
b1760 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
b1770 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
b1780 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
b1790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b17a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b17b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b17c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b17d0 2a 0a 2a 2a 20 55 74 69 6c 69 74 79 20 66 75 6e  *.** Utility fun
b17e0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f  ctions used thro
b17f0 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a  ughout sqlite..*
b1800 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
b1810 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e  ontains function
b1820 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6e 67  s for allocating
b1830 20 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 61 72 69   memory, compari
b1840 6e 67 0a 2a 2a 20 73 74 72 69 6e 67 73 2c 20 61  ng.** strings, a
b1850 6e 64 20 73 74 75 66 66 20 6c 69 6b 65 20 74 68  nd stuff like th
b1860 61 74 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65 66  at..**.*/.#ifdef
b1870 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e   SQLITE_HAVE_ISN
b1880 41 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6d 61  AN.# include <ma
b1890 74 68 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a  th.h>.#endif../*
b18a0 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65 65 64  .** Routine need
b18b0 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68  ed to support th
b18c0 65 20 74 65 73 74 63 61 73 65 28 29 20 6d 61 63  e testcase() mac
b18d0 72 6f 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ro..*/.#ifdef SQ
b18e0 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45  LITE_COVERAGE_TE
b18f0 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
b1900 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
b1910 76 65 72 61 67 65 28 69 6e 74 20 78 29 7b 0a 20  verage(int x){. 
b1920 20 73 74 61 74 69 63 20 69 6e 74 20 64 75 6d 6d   static int dumm
b1930 79 20 3d 20 30 3b 0a 20 20 64 75 6d 6d 79 20 2b  y = 0;.  dummy +
b1940 3d 20 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  = x;.}.#endif../
b1950 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
b1960 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67   if the floating
b1970 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20   point value is 
b1980 4e 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e 61  Not a Number (Na
b1990 4e 29 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68  N)..**.** Use th
b19a0 65 20 6d 61 74 68 20 6c 69 62 72 61 72 79 20 69  e math library i
b19b0 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20  snan() function 
b19c0 69 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  if compiled with
b19d0 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e   SQLITE_HAVE_ISN
b19e0 41 4e 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  AN..** Otherwise
b19f0 2c 20 77 65 20 68 61 76 65 20 6f 75 72 20 6f 77  , we have our ow
b1a00 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
b1a10 20 74 68 61 74 20 77 6f 72 6b 73 20 6f 6e 20 6d   that works on m
b1a20 6f 73 74 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a  ost systems..*/.
b1a30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b1a40 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28  nt sqlite3IsNaN(
b1a50 64 6f 75 62 6c 65 20 78 29 7b 0a 20 20 69 6e 74  double x){.  int
b1a60 20 72 63 3b 20 20 20 2f 2a 20 54 68 65 20 76 61   rc;   /* The va
b1a70 6c 75 65 20 72 65 74 75 72 6e 20 2a 2f 0a 23 69  lue return */.#i
b1a80 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
b1a90 45 5f 48 41 56 45 5f 49 53 4e 41 4e 29 0a 20 20  E_HAVE_ISNAN).  
b1aa0 2f 2a 0a 20 20 2a 2a 20 53 79 73 74 65 6d 73 20  /*.  ** Systems 
b1ab0 74 68 61 74 20 73 75 70 70 6f 72 74 20 74 68 65  that support the
b1ac0 20 69 73 6e 61 6e 28 29 20 6c 69 62 72 61 72 79   isnan() library
b1ad0 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
b1ae0 20 70 72 6f 62 61 62 6c 79 0a 20 20 2a 2a 20 6d   probably.  ** m
b1af0 61 6b 65 20 75 73 65 20 6f 66 20 69 74 20 62 79  ake use of it by
b1b00 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20   compiling with 
b1b10 2d 44 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53  -DSQLITE_HAVE_IS
b1b20 4e 41 4e 2e 20 20 42 75 74 20 77 65 20 68 61 76  NAN.  But we hav
b1b30 65 0a 20 20 2a 2a 20 66 6f 75 6e 64 20 74 68 61  e.  ** found tha
b1b40 74 20 6d 61 6e 79 20 73 79 73 74 65 6d 73 20 64  t many systems d
b1b50 6f 20 6e 6f 74 20 68 61 76 65 20 61 20 77 6f 72  o not have a wor
b1b60 6b 69 6e 67 20 69 73 6e 61 6e 28 29 20 66 75 6e  king isnan() fun
b1b70 63 74 69 6f 6e 20 73 6f 0a 20 20 2a 2a 20 74 68  ction so.  ** th
b1b80 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
b1b90 6e 20 69 73 20 70 72 6f 76 69 64 65 64 20 61 73  n is provided as
b1ba0 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e   an alternative.
b1bb0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
b1bc0 4e 61 4e 20 74 65 73 74 20 73 6f 6d 65 74 69 6d  NaN test sometim
b1bd0 65 73 20 66 61 69 6c 73 20 69 66 20 63 6f 6d 70  es fails if comp
b1be0 69 6c 65 64 20 6f 6e 20 47 43 43 20 77 69 74 68  iled on GCC with
b1bf0 20 2d 66 66 61 73 74 2d 6d 61 74 68 2e 0a 20 20   -ffast-math..  
b1c00 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20  ** On the other 
b1c10 68 61 6e 64 2c 20 74 68 65 20 75 73 65 20 6f 66  hand, the use of
b1c20 20 2d 66 66 61 73 74 2d 6d 61 74 68 20 63 6f 6d   -ffast-math com
b1c30 65 73 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c  es with the foll
b1c40 6f 77 69 6e 67 0a 20 20 2a 2a 20 77 61 72 6e 69  owing.  ** warni
b1c50 6e 67 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  ng:.  **.  **   
b1c60 20 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 5b     This option [
b1c70 2d 66 66 61 73 74 2d 6d 61 74 68 5d 20 73 68 6f  -ffast-math] sho
b1c80 75 6c 64 20 6e 65 76 65 72 20 62 65 20 74 75 72  uld never be tur
b1c90 6e 65 64 20 6f 6e 20 62 79 20 61 6e 79 0a 20 20  ned on by any.  
b1ca0 2a 2a 20 20 20 20 20 20 2d 4f 20 6f 70 74 69 6f  **      -O optio
b1cb0 6e 20 73 69 6e 63 65 20 69 74 20 63 61 6e 20 72  n since it can r
b1cc0 65 73 75 6c 74 20 69 6e 20 69 6e 63 6f 72 72 65  esult in incorre
b1cd0 63 74 20 6f 75 74 70 75 74 20 66 6f 72 20 70 72  ct output for pr
b1ce0 6f 67 72 61 6d 73 0a 20 20 2a 2a 20 20 20 20 20  ograms.  **     
b1cf0 20 77 68 69 63 68 20 64 65 70 65 6e 64 20 6f 6e   which depend on
b1d00 20 61 6e 20 65 78 61 63 74 20 69 6d 70 6c 65 6d   an exact implem
b1d10 65 6e 74 61 74 69 6f 6e 20 6f 66 20 49 45 45 45  entation of IEEE
b1d20 20 6f 72 20 49 53 4f 20 0a 20 20 2a 2a 20 20 20   or ISO .  **   
b1d30 20 20 20 72 75 6c 65 73 2f 73 70 65 63 69 66 69     rules/specifi
b1d40 63 61 74 69 6f 6e 73 20 66 6f 72 20 6d 61 74 68  cations for math
b1d50 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a   functions..  **
b1d60 0a 20 20 2a 2a 20 55 6e 64 65 72 20 4d 53 56 43  .  ** Under MSVC
b1d70 2c 20 74 68 69 73 20 4e 61 4e 20 74 65 73 74 20  , this NaN test 
b1d80 6d 61 79 20 66 61 69 6c 20 69 66 20 63 6f 6d 70  may fail if comp
b1d90 69 6c 65 64 20 77 69 74 68 20 61 20 66 6c 6f 61  iled with a floa
b1da0 74 69 6e 67 2d 0a 20 20 2a 2a 20 70 6f 69 6e 74  ting-.  ** point
b1db0 20 70 72 65 63 69 73 69 6f 6e 20 6d 6f 64 65 20   precision mode 
b1dc0 6f 74 68 65 72 20 74 68 61 6e 20 2f 66 70 3a 70  other than /fp:p
b1dd0 72 65 63 69 73 65 2e 20 20 46 72 6f 6d 20 74 68  recise.  From th
b1de0 65 20 4d 53 44 4e 20 0a 20 20 2a 2a 20 64 6f 63  e MSDN .  ** doc
b1df0 75 6d 65 6e 74 61 74 69 6f 6e 3a 0a 20 20 2a 2a  umentation:.  **
b1e00 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 65 20 63  .  **      The c
b1e10 6f 6d 70 69 6c 65 72 20 5b 77 69 74 68 20 2f 66  ompiler [with /f
b1e20 70 3a 70 72 65 63 69 73 65 5d 20 77 69 6c 6c 20  p:precise] will 
b1e30 70 72 6f 70 65 72 6c 79 20 68 61 6e 64 6c 65 20  properly handle 
b1e40 63 6f 6d 70 61 72 69 73 6f 6e 73 20 0a 20 20 2a  comparisons .  *
b1e50 2a 20 20 20 20 20 20 69 6e 76 6f 6c 76 69 6e 67  *      involving
b1e60 20 4e 61 4e 2e 20 46 6f 72 20 65 78 61 6d 70 6c   NaN. For exampl
b1e70 65 2c 20 78 20 21 3d 20 78 20 65 76 61 6c 75 61  e, x != x evalua
b1e80 74 65 73 20 74 6f 20 74 72 75 65 20 69 66 20 78  tes to true if x
b1e90 20 69 73 20 4e 61 4e 20 0a 20 20 2a 2a 20 20 20   is NaN .  **   
b1ea0 20 20 20 2e 2e 2e 0a 20 20 2a 2f 0a 23 69 66 64     ....  */.#ifd
b1eb0 65 66 20 5f 5f 46 41 53 54 5f 4d 41 54 48 5f 5f  ef __FAST_MATH__
b1ec0 0a 23 20 65 72 72 6f 72 20 53 51 4c 69 74 65 20  .# error SQLite 
b1ed0 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f  will not work co
b1ee0 72 72 65 63 74 6c 79 20 77 69 74 68 20 74 68 65  rrectly with the
b1ef0 20 2d 66 66 61 73 74 2d 6d 61 74 68 20 6f 70 74   -ffast-math opt
b1f00 69 6f 6e 20 6f 66 20 47 43 43 2e 0a 23 65 6e 64  ion of GCC..#end
b1f10 69 66 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f  if.  volatile do
b1f20 75 62 6c 65 20 79 20 3d 20 78 3b 0a 20 20 76 6f  uble y = x;.  vo
b1f30 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 7a 20  latile double z 
b1f40 3d 20 79 3b 0a 20 20 72 63 20 3d 20 28 79 21 3d  = y;.  rc = (y!=
b1f50 7a 29 3b 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66  z);.#else  /* if
b1f60 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
b1f70 48 41 56 45 5f 49 53 4e 41 4e 29 20 2a 2f 0a 20  HAVE_ISNAN) */. 
b1f80 20 72 63 20 3d 20 69 73 6e 61 6e 28 78 29 3b 0a   rc = isnan(x);.
b1f90 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
b1fa0 5f 48 41 56 45 5f 49 53 4e 41 4e 20 2a 2f 0a 20  _HAVE_ISNAN */. 
b1fb0 20 74 65 73 74 63 61 73 65 28 20 72 63 20 29 3b   testcase( rc );
b1fc0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
b1fd0 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61  ./*.** Compute a
b1fe0 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 74   string length t
b1ff0 68 61 74 20 69 73 20 6c 69 6d 69 74 65 64 20 74  hat is limited t
b2000 6f 20 77 68 61 74 20 63 61 6e 20 62 65 20 73 74  o what can be st
b2010 6f 72 65 64 20 69 6e 0a 2a 2a 20 6c 6f 77 65 72  ored in.** lower
b2020 20 33 30 20 62 69 74 73 20 6f 66 20 61 20 33 32   30 bits of a 32
b2030 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
b2040 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76  ger..**.** The v
b2050 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 77 69  alue returned wi
b2060 6c 6c 20 6e 65 76 65 72 20 62 65 20 6e 65 67 61  ll never be nega
b2070 74 69 76 65 2e 20 20 4e 6f 72 20 77 69 6c 6c 20  tive.  Nor will 
b2080 69 74 20 65 76 65 72 20 62 65 20 67 72 65 61 74  it ever be great
b2090 65 72 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61  er.** than the a
b20a0 63 74 75 61 6c 20 6c 65 6e 67 74 68 20 6f 66 20  ctual length of 
b20b0 74 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72  the string.  For
b20c0 20 76 65 72 79 20 6c 6f 6e 67 20 73 74 72 69 6e   very long strin
b20d0 67 73 20 28 67 72 65 61 74 65 72 0a 2a 2a 20 74  gs (greater.** t
b20e0 68 61 6e 20 31 47 69 42 29 20 74 68 65 20 76 61  han 1GiB) the va
b20f0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 6d 69 67  lue returned mig
b2100 68 74 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20  ht be less than 
b2110 74 68 65 20 74 72 75 65 20 73 74 72 69 6e 67 20  the true string 
b2120 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 53 51 4c 49 54  length..*/.SQLIT
b2130 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b2140 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 6f  lite3Strlen30(co
b2150 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20  nst char *z){.  
b2160 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d  const char *z2 =
b2170 20 7a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29   z;.  if( z==0 )
b2180 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69   return 0;.  whi
b2190 6c 65 28 20 2a 7a 32 20 29 7b 20 7a 32 2b 2b 3b  le( *z2 ){ z2++;
b21a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 78 33 66   }.  return 0x3f
b21b0 66 66 66 66 66 66 20 26 20 28 69 6e 74 29 28 7a  ffffff & (int)(z
b21c0 32 20 2d 20 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  2 - z);.}../*.**
b21d0 20 53 65 74 20 74 68 65 20 6d 6f 73 74 20 72 65   Set the most re
b21e0 63 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20  cent error code 
b21f0 61 6e 64 20 65 72 72 6f 72 20 73 74 72 69 6e 67  and error string
b2200 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 0a   for the sqlite.
b2210 2a 2a 20 68 61 6e 64 6c 65 20 22 64 62 22 2e 20  ** handle "db". 
b2220 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  The error code i
b2230 73 20 73 65 74 20 74 6f 20 22 65 72 72 5f 63 6f  s set to "err_co
b2240 64 65 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74  de"..**.** If it
b2250 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 74   is not NULL, st
b2260 72 69 6e 67 20 7a 46 6f 72 6d 61 74 20 73 70 65  ring zFormat spe
b2270 63 69 66 69 65 73 20 74 68 65 20 66 6f 72 6d 61  cifies the forma
b2280 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 72 72 6f  t of the.** erro
b2290 72 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20  r string in the 
b22a0 73 74 79 6c 65 20 6f 66 20 74 68 65 20 70 72 69  style of the pri
b22b0 6e 74 66 20 66 75 6e 63 74 69 6f 6e 73 3a 20 54  ntf functions: T
b22c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
b22d0 66 6f 72 6d 61 74 20 63 68 61 72 61 63 74 65 72  format character
b22e0 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a  s are allowed:.*
b22f0 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 20 20 20  *.**      %s    
b2300 20 20 49 6e 73 65 72 74 20 61 20 73 74 72 69 6e    Insert a strin
b2310 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20 20 20  g.**      %z    
b2320 20 20 41 20 73 74 72 69 6e 67 20 74 68 61 74 20    A string that 
b2330 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 20  should be freed 
b2340 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 20 20 20  after use.**    
b2350 20 20 25 64 20 20 20 20 20 20 49 6e 73 65 72 74    %d      Insert
b2360 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20   an integer.**  
b2370 20 20 20 20 25 54 20 20 20 20 20 20 49 6e 73 65      %T      Inse
b2380 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20  rt a token.**   
b2390 20 20 20 25 53 20 20 20 20 20 20 49 6e 73 65 72     %S      Inser
b23a0 74 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d  t the first elem
b23b0 65 6e 74 20 6f 66 20 61 20 53 72 63 4c 69 73 74  ent of a SrcList
b23c0 0a 2a 2a 0a 2a 2a 20 7a 46 6f 72 6d 61 74 20 61  .**.** zFormat a
b23d0 6e 64 20 61 6e 79 20 73 74 72 69 6e 67 20 74 6f  nd any string to
b23e0 6b 65 6e 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77  kens that follow
b23f0 20 69 74 20 61 72 65 20 61 73 73 75 6d 65 64 20   it are assumed 
b2400 74 6f 20 62 65 0a 2a 2a 20 65 6e 63 6f 64 65 64  to be.** encoded
b2410 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a   in UTF-8..**.**
b2420 20 54 6f 20 63 6c 65 61 72 20 74 68 65 20 6d 6f   To clear the mo
b2430 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20  st recent error 
b2440 66 6f 72 20 73 71 6c 69 74 65 20 68 61 6e 64 6c  for sqlite handl
b2450 65 20 22 64 62 22 2c 20 73 71 6c 69 74 65 33 45  e "db", sqlite3E
b2460 72 72 6f 72 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  rror.** should b
b2470 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 65 72  e called with er
b2480 72 5f 63 6f 64 65 20 73 65 74 20 74 6f 20 53 51  r_code set to SQ
b2490 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 7a 46 6f 72  LITE_OK and zFor
b24a0 6d 61 74 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55  mat set.** to NU
b24b0 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  LL..*/.SQLITE_PR
b24c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
b24d0 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33 20  e3Error(sqlite3 
b24e0 2a 64 62 2c 20 69 6e 74 20 65 72 72 5f 63 6f 64  *db, int err_cod
b24f0 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
b2500 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20  Format, ...){.  
b2510 69 66 28 20 64 62 20 26 26 20 28 64 62 2d 3e 70  if( db && (db->p
b2520 45 72 72 20 7c 7c 20 28 64 62 2d 3e 70 45 72 72  Err || (db->pErr
b2530 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e   = sqlite3ValueN
b2540 65 77 28 64 62 29 29 21 3d 30 29 20 29 7b 0a 20  ew(db))!=0) ){. 
b2550 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d     db->errCode =
b2560 20 65 72 72 5f 63 6f 64 65 3b 0a 20 20 20 20 69   err_code;.    i
b2570 66 28 20 7a 46 6f 72 6d 61 74 20 29 7b 0a 20 20  f( zFormat ){.  
b2580 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20      char *z;.   
b2590 20 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20     va_list ap;. 
b25a0 20 20 20 20 20 76 61 5f 73 74 61 72 74 28 61 70       va_start(ap
b25b0 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20  , zFormat);.    
b25c0 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50    z = sqlite3VMP
b25d0 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61  rintf(db, zForma
b25e0 74 2c 20 61 70 29 3b 0a 20 20 20 20 20 20 76 61  t, ap);.      va
b25f0 5f 65 6e 64 28 61 70 29 3b 0a 20 20 20 20 20 20  _end(ap);.      
b2600 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
b2610 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c  tr(db->pErr, -1,
b2620 20 7a 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   z, SQLITE_UTF8,
b2630 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29   SQLITE_DYNAMIC)
b2640 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
b2650 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53     sqlite3ValueS
b2660 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20  etStr(db->pErr, 
b2670 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46  0, 0, SQLITE_UTF
b2680 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  8, SQLITE_STATIC
b2690 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
b26a0 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 65 72 72  /*.** Add an err
b26b0 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50  or message to pP
b26c0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e  arse->zErrMsg an
b26d0 64 20 69 6e 63 72 65 6d 65 6e 74 20 70 50 61 72  d increment pPar
b26e0 73 65 2d 3e 6e 45 72 72 2e 0a 2a 2a 20 54 68 65  se->nErr..** The
b26f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 61   following forma
b2700 74 74 69 6e 67 20 63 68 61 72 61 63 74 65 72 73  tting characters
b2710 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a   are allowed:.**
b2720 0a 2a 2a 20 20 20 20 20 20 25 73 20 20 20 20 20  .**      %s     
b2730 20 49 6e 73 65 72 74 20 61 20 73 74 72 69 6e 67   Insert a string
b2740 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20 20 20 20  .**      %z     
b2750 20 41 20 73 74 72 69 6e 67 20 74 68 61 74 20 73   A string that s
b2760 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 20 61  hould be freed a
b2770 66 74 65 72 20 75 73 65 0a 2a 2a 20 20 20 20 20  fter use.**     
b2780 20 25 64 20 20 20 20 20 20 49 6e 73 65 72 74 20   %d      Insert 
b2790 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  an integer.**   
b27a0 20 20 20 25 54 20 20 20 20 20 20 49 6e 73 65 72     %T      Inser
b27b0 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20  t a token.**    
b27c0 20 20 25 53 20 20 20 20 20 20 49 6e 73 65 72 74    %S      Insert
b27d0 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   the first eleme
b27e0 6e 74 20 6f 66 20 61 20 53 72 63 4c 69 73 74 0a  nt of a SrcList.
b27f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
b2800 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 75 73  ion should be us
b2810 65 64 20 74 6f 20 72 65 70 6f 72 74 20 61 6e 79  ed to report any
b2820 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75   error that occu
b2830 72 73 20 77 68 69 6c 73 74 0a 2a 2a 20 63 6f 6d  rs whilst.** com
b2840 70 69 6c 69 6e 67 20 61 6e 20 53 51 4c 20 73 74  piling an SQL st
b2850 61 74 65 6d 65 6e 74 20 28 69 2e 65 2e 20 77 69  atement (i.e. wi
b2860 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 70 72 65  thin sqlite3_pre
b2870 70 61 72 65 28 29 29 2e 20 54 68 65 0a 2a 2a 20  pare()). The.** 
b2880 6c 61 73 74 20 74 68 69 6e 67 20 74 68 65 20 73  last thing the s
b2890 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
b28a0 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 69   function does i
b28b0 73 20 63 6f 70 79 20 74 68 65 20 65 72 72 6f 72  s copy the error
b28c0 0a 2a 2a 20 73 74 6f 72 65 64 20 62 79 20 74 68  .** stored by th
b28d0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f  is function into
b28e0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
b28f0 6e 64 6c 65 20 75 73 69 6e 67 20 73 71 6c 69 74  ndle using sqlit
b2900 65 33 45 72 72 6f 72 28 29 2e 0a 2a 2a 20 46 75  e3Error()..** Fu
b2910 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 45 72  nction sqlite3Er
b2920 72 6f 72 28 29 20 73 68 6f 75 6c 64 20 62 65 20  ror() should be 
b2930 75 73 65 64 20 64 75 72 69 6e 67 20 73 74 61 74  used during stat
b2940 65 6d 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 0a  ement execution.
b2950 2a 2a 20 28 73 71 6c 69 74 65 33 5f 73 74 65 70  ** (sqlite3_step
b2960 28 29 20 65 74 63 2e 29 2e 0a 2a 2f 0a 53 51 4c  () etc.)..*/.SQL
b2970 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b2980 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
b2990 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
b29a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
b29b0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
b29c0 6c 69 73 74 20 61 70 3b 0a 20 20 73 71 6c 69 74  list ap;.  sqlit
b29d0 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
b29e0 3e 64 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  >db;.  pParse->n
b29f0 45 72 72 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  Err++;.  sqlite3
b2a00 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73  DbFree(db, pPars
b2a10 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76  e->zErrMsg);.  v
b2a20 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
b2a30 6d 61 74 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  mat);.  pParse->
b2a40 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
b2a50 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46  3VMPrintf(db, zF
b2a60 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61  ormat, ap);.  va
b2a70 5f 65 6e 64 28 61 70 29 3b 0a 20 20 70 50 61 72  _end(ap);.  pPar
b2a80 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
b2a90 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ERROR;.}../*.** 
b2aa0 43 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20  Clear the error 
b2ab0 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73  message in pPars
b2ac0 65 2c 20 69 66 20 61 6e 79 0a 2a 2f 0a 53 51 4c  e, if any.*/.SQL
b2ad0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b2ae0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65   sqlite3ErrorCle
b2af0 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ar(Parse *pParse
b2b00 29 7b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  ){.  sqlite3DbFr
b2b10 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ee(pParse->db, p
b2b20 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b  Parse->zErrMsg);
b2b30 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  .  pParse->zErrM
b2b40 73 67 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65  sg = 0;.  pParse
b2b50 2d 3e 6e 45 72 72 20 3d 20 30 3b 0a 7d 0a 0a 2f  ->nErr = 0;.}../
b2b60 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20  *.** Convert an 
b2b70 53 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 64  SQL-style quoted
b2b80 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e   string into a n
b2b90 6f 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 20  ormal string by 
b2ba0 72 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20  removing.** the 
b2bb0 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73  quote characters
b2bc0 2e 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f  .  The conversio
b2bd0 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61  n is done in-pla
b2be0 63 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 69  ce.  If the.** i
b2bf0 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62 65  nput does not be
b2c00 67 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74 65  gin with a quote
b2c10 20 63 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e   character, then
b2c20 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
b2c30 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
b2c40 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 73 74 72  ** The input str
b2c50 69 6e 67 20 6d 75 73 74 20 62 65 20 7a 65 72 6f  ing must be zero
b2c60 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 41 20  -terminated.  A 
b2c70 6e 65 77 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  new zero-termina
b2c80 74 6f 72 0a 2a 2a 20 69 73 20 61 64 64 65 64 20  tor.** is added 
b2c90 74 6f 20 74 68 65 20 64 65 71 75 6f 74 65 64 20  to the dequoted 
b2ca0 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  string..**.** Th
b2cb0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
b2cc0 73 20 2d 31 20 69 66 20 6e 6f 20 64 65 71 75 6f  s -1 if no dequo
b2cd0 74 69 6e 67 20 6f 63 63 75 72 73 20 6f 72 20 74  ting occurs or t
b2ce0 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
b2cf0 0a 2a 2a 20 64 65 71 75 6f 74 65 64 20 73 74 72  .** dequoted str
b2d00 69 6e 67 2c 20 65 78 63 6c 75 73 69 76 65 20 6f  ing, exclusive o
b2d10 66 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69  f the zero termi
b2d20 6e 61 74 6f 72 2c 20 69 66 20 64 65 71 75 6f 74  nator, if dequot
b2d30 69 6e 67 20 64 6f 65 73 0a 2a 2a 20 6f 63 63 75  ing does.** occu
b2d40 72 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46 65  r..**.** 2002-Fe
b2d50 62 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 74 69  b-14: This routi
b2d60 6e 65 20 69 73 20 65 78 74 65 6e 64 65 64 20 74  ne is extended t
b2d70 6f 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 63 65  o remove MS-Acce
b2d80 73 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72 61 63  ss style.** brac
b2d90 6b 65 74 73 20 66 72 6f 6d 20 61 72 6f 75 6e 64  kets from around
b2da0 20 69 64 65 6e 74 69 66 65 72 73 2e 20 20 46 6f   identifers.  Fo
b2db0 72 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b 61 2d  r example:  "[a-
b2dc0 62 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a 2a 2a  b-c]" becomes.**
b2dd0 20 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51 4c   "a-b-c"..*/.SQL
b2de0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b2df0 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63  sqlite3Dequote(c
b2e00 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20  har *z){.  char 
b2e10 71 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20  quote;.  int i, 
b2e20 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20  j;.  if( z==0 ) 
b2e30 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 71 75 6f  return -1;.  quo
b2e40 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69  te = z[0];.  swi
b2e50 74 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20  tch( quote ){.  
b2e60 20 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72    case '\'':  br
b2e70 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22  eak;.    case '"
b2e80 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ':   break;.    
b2e90 63 61 73 65 20 27 60 27 3a 20 20 20 62 72 65 61  case '`':   brea
b2ea0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
b2eb0 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63    /* For MySQL c
b2ec0 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a  ompatibility */.
b2ed0 20 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20      case '[':   
b2ee0 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72  quote = ']';  br
b2ef0 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20  eak;  /* For MS 
b2f00 53 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74  SqlServer compat
b2f10 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64  ibility */.    d
b2f20 65 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72  efault:    retur
b2f30 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  n -1;.  }.  for(
b2f40 69 3d 31 2c 20 6a 3d 30 3b 20 41 4c 57 41 59 53  i=1, j=0; ALWAYS
b2f50 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20  (z[i]); i++){.  
b2f60 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74    if( z[i]==quot
b2f70 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a  e ){.      if( z
b2f80 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a  [i+1]==quote ){.
b2f90 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
b2fa0 20 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20   quote;.        
b2fb0 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  i++;.      }else
b2fc0 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
b2fd0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
b2fe0 73 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d  se{.      z[j++]
b2ff0 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20   = z[i];.    }. 
b3000 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20   }.  z[j] = 0;. 
b3010 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a   return j;.}../*
b3020 20 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f 72   Convenient shor
b3030 74 2d 68 61 6e 64 20 2a 2f 0a 23 64 65 66 69 6e  t-hand */.#defin
b3040 65 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 20 73  e UpperToLower s
b3050 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
b3060 65 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 73  er../*.** Some s
b3070 79 73 74 65 6d 73 20 68 61 76 65 20 73 74 72 69  ystems have stri
b3080 63 6d 70 28 29 2e 20 20 4f 74 68 65 72 73 20 68  cmp().  Others h
b3090 61 76 65 20 73 74 72 63 61 73 65 63 6d 70 28 29  ave strcasecmp()
b30a0 2e 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 68  .  Because.** th
b30b0 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 69 73  ere is no consis
b30c0 74 65 6e 63 79 2c 20 77 65 20 77 69 6c 6c 20 64  tency, we will d
b30d0 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 2e 0a 2a  efine our own..*
b30e0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b30f0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49   int sqlite3StrI
b3100 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  Cmp(const char *
b3110 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61  zLeft, const cha
b3120 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 72 65  r *zRight){.  re
b3130 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20  gister unsigned 
b3140 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61  char *a, *b;.  a
b3150 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
b3160 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d  r *)zLeft;.  b =
b3170 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
b3180 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c  *)zRight;.  whil
b3190 65 28 20 2a 61 21 3d 30 20 26 26 20 55 70 70 65  e( *a!=0 && Uppe
b31a0 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70  rToLower[*a]==Up
b31b0 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b  perToLower[*b]){
b31c0 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72   a++; b++; }.  r
b31d0 65 74 75 72 6e 20 55 70 70 65 72 54 6f 4c 6f 77  eturn UpperToLow
b31e0 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f  er[*a] - UpperTo
b31f0 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c  Lower[*b];.}.SQL
b3200 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
b3210 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 63 6f 6e  te3_strnicmp(con
b3220 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20  st char *zLeft, 
b3230 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67  const char *zRig
b3240 68 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  ht, int N){.  re
b3250 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20  gister unsigned 
b3260 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61  char *a, *b;.  a
b3270 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
b3280 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d  r *)zLeft;.  b =
b3290 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
b32a0 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c  *)zRight;.  whil
b32b0 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26 20 2a 61  e( N-- > 0 && *a
b32c0 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f  !=0 && UpperToLo
b32d0 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f  wer[*a]==UpperTo
b32e0 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b  Lower[*b]){ a++;
b32f0 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e   b++; }.  return
b3300 20 4e 3c 30 20 3f 20 30 20 3a 20 55 70 70 65 72   N<0 ? 0 : Upper
b3310 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70  ToLower[*a] - Up
b3320 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a  perToLower[*b];.
b3330 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
b3340 54 52 55 45 20 69 66 20 7a 20 69 73 20 61 20 70  TRUE if z is a p
b3350 75 72 65 20 6e 75 6d 65 72 69 63 20 73 74 72 69  ure numeric stri
b3360 6e 67 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53  ng.  Return FALS
b3370 45 20 61 6e 64 20 6c 65 61 76 65 0a 2a 2a 20 2a  E and leave.** *
b3380 72 65 61 6c 6e 75 6d 20 75 6e 63 68 61 6e 67 65  realnum unchange
b3390 64 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20  d if the string 
b33a0 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 63 68 61  contains any cha
b33b0 72 61 63 74 65 72 20 77 68 69 63 68 20 69 73 20  racter which is 
b33c0 6e 6f 74 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61  not.** part of a
b33d0 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 49   number..**.** I
b33e0 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20  f the string is 
b33f0 70 75 72 65 20 6e 75 6d 65 72 69 63 2c 20 73 65  pure numeric, se
b3400 74 20 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 54 52  t *realnum to TR
b3410 55 45 20 69 66 20 74 68 65 20 73 74 72 69 6e 67  UE if the string
b3420 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  .** contains the
b3430 20 27 2e 27 20 63 68 61 72 61 63 74 65 72 20 6f   '.' character o
b3440 72 20 61 6e 20 22 45 2b 30 30 30 22 20 73 74 79  r an "E+000" sty
b3450 6c 65 20 65 78 70 6f 6e 65 6e 74 69 61 74 69 6f  le exponentiatio
b3460 6e 20 73 75 66 66 69 78 2e 0a 2a 2a 20 4f 74 68  n suffix..** Oth
b3470 65 72 77 69 73 65 20 73 65 74 20 2a 72 65 61 6c  erwise set *real
b3480 6e 75 6d 20 74 6f 20 46 41 4c 53 45 2e 20 20 4e  num to FALSE.  N
b3490 6f 74 65 20 74 68 61 74 20 6a 75 73 74 20 62 65  ote that just be
b34a0 63 61 75 65 20 2a 72 65 61 6c 6e 75 6d 20 69 73  caue *realnum is
b34b0 0a 2a 2a 20 66 61 6c 73 65 20 64 6f 65 73 20 6e  .** false does n
b34c0 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65  ot mean that the
b34d0 20 6e 75 6d 62 65 72 20 63 61 6e 20 62 65 20 73   number can be s
b34e0 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e 76  uccessfully conv
b34f0 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 6e  erted into.** an
b3500 20 69 6e 74 65 67 65 72 20 2d 20 69 74 20 6d 69   integer - it mi
b3510 67 68 74 20 62 65 20 74 6f 6f 20 62 69 67 2e 0a  ght be too big..
b3520 2a 2a 0a 2a 2a 20 41 6e 20 65 6d 70 74 79 20 73  **.** An empty s
b3530 74 72 69 6e 67 20 69 73 20 63 6f 6e 73 69 64 65  tring is conside
b3540 72 65 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 2e  red non-numeric.
b3550 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b3560 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
b3570 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61  Number(const cha
b3580 72 20 2a 7a 2c 20 69 6e 74 20 2a 72 65 61 6c 6e  r *z, int *realn
b3590 75 6d 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69  um, u8 enc){.  i
b35a0 6e 74 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d 3d  nt incr = (enc==
b35b0 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29  SQLITE_UTF8?1:2)
b35c0 3b 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c  ;.  if( enc==SQL
b35d0 49 54 45 5f 55 54 46 31 36 42 45 20 29 20 7a 2b  ITE_UTF16BE ) z+
b35e0 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27  +;.  if( *z=='-'
b35f0 20 7c 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20 7a 20   || *z=='+' ) z 
b3600 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66 28 20 21  += incr;.  if( !
b3610 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
b3620 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  z) ){.    return
b3630 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 2b 3d 20 69   0;.  }.  z += i
b3640 6e 63 72 3b 0a 20 20 2a 72 65 61 6c 6e 75 6d 20  ncr;.  *realnum 
b3650 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 71  = 0;.  while( sq
b3660 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29  lite3Isdigit(*z)
b3670 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d   ){ z += incr; }
b3680 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29  .  if( *z=='.' )
b3690 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b  {.    z += incr;
b36a0 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
b36b0 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 72  3Isdigit(*z) ) r
b36c0 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69  eturn 0;.    whi
b36d0 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67  le( sqlite3Isdig
b36e0 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69  it(*z) ){ z += i
b36f0 6e 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61 6c  ncr; }.    *real
b3700 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69  num = 1;.  }.  i
b3710 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a  f( *z=='e' || *z
b3720 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a 20 2b  =='E' ){.    z +
b3730 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20  = incr;.    if( 
b3740 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d 3d 27  *z=='+' || *z=='
b3750 2d 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a  -' ) z += incr;.
b3760 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
b3770 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 72 65  Isdigit(*z) ) re
b3780 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c  turn 0;.    whil
b3790 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  e( sqlite3Isdigi
b37a0 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e  t(*z) ){ z += in
b37b0 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61 6c 6e  cr; }.    *realn
b37c0 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65  um = 1;.  }.  re
b37d0 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f  turn *z==0;.}../
b37e0 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20  *.** The string 
b37f0 7a 5b 5d 20 69 73 20 61 6e 20 41 53 43 49 49 20  z[] is an ASCII 
b3800 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
b3810 66 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2e  f a real number.
b3820 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 73  .** Convert this
b3830 20 73 74 72 69 6e 67 20 74 6f 20 61 20 64 6f 75   string to a dou
b3840 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ble..**.** This 
b3850 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65 73 20  routine assumes 
b3860 74 68 61 74 20 7a 5b 5d 20 72 65 61 6c 6c 79 20  that z[] really 
b3870 69 73 20 61 20 76 61 6c 69 64 20 6e 75 6d 62 65  is a valid numbe
b3880 72 2e 20 20 49 66 20 69 74 0a 2a 2a 20 69 73 20  r.  If it.** is 
b3890 6e 6f 74 2c 20 74 68 65 20 72 65 73 75 6c 74 20  not, the result 
b38a0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a  is undefined..**
b38b0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
b38c0 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 64   is used instead
b38d0 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   of the library 
b38e0 61 74 6f 66 28 29 20 66 75 6e 63 74 69 6f 6e 20  atof() function 
b38f0 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 6c  because.** the l
b3900 69 62 72 61 72 79 20 61 74 6f 66 28 29 20 6d 69  ibrary atof() mi
b3910 67 68 74 20 77 61 6e 74 20 74 6f 20 75 73 65 20  ght want to use 
b3920 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69 6d  "," as the decim
b3930 61 6c 20 70 6f 69 6e 74 20 69 6e 73 74 65 61 64  al point instead
b3940 0a 2a 2a 20 6f 66 20 22 2e 22 20 64 65 70 65 6e  .** of "." depen
b3950 64 69 6e 67 20 6f 6e 20 68 6f 77 20 6c 6f 63 61  ding on how loca
b3960 6c 65 20 69 73 20 73 65 74 2e 20 20 42 75 74 20  le is set.  But 
b3970 74 68 61 74 20 77 6f 75 6c 64 20 63 61 75 73 65  that would cause
b3980 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a 20 66 6f 72   problems.** for
b3990 20 53 51 4c 2e 20 20 53 6f 20 74 68 69 73 20 72   SQL.  So this r
b39a0 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20 75 73  outine always us
b39b0 65 73 20 22 2e 22 20 72 65 67 61 72 64 6c 65 73  es "." regardles
b39c0 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a  s of locale..*/.
b39d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b39e0 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 63  nt sqlite3AtoF(c
b39f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 6f  onst char *z, do
b3a00 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 29 7b 0a  uble *pResult){.
b3a10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
b3a20 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
b3a30 4e 54 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  NT.  const char 
b3a40 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b 0a 20 20 2f  *zBegin = z;.  /
b3a50 2a 20 73 69 67 6e 20 2a 20 73 69 67 6e 69 66 69  * sign * signifi
b3a60 63 61 6e 64 20 2a 20 28 31 30 20 5e 20 28 65 73  cand * (10 ^ (es
b3a70 69 67 6e 20 2a 20 65 78 70 6f 6e 65 6e 74 29 29  ign * exponent))
b3a80 20 2a 2f 0a 20 20 69 6e 74 20 73 69 67 6e 20 3d   */.  int sign =
b3a90 20 31 3b 20 20 20 2f 2a 20 73 69 67 6e 20 6f 66   1;   /* sign of
b3aa0 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a   significand */.
b3ab0 20 20 69 36 34 20 73 20 3d 20 30 3b 20 20 20 20    i64 s = 0;    
b3ac0 20 20 2f 2a 20 73 69 67 6e 69 66 69 63 61 6e 64    /* significand
b3ad0 20 2a 2f 0a 20 20 69 6e 74 20 64 20 3d 20 30 3b   */.  int d = 0;
b3ae0 20 20 20 20 20 20 2f 2a 20 61 64 6a 75 73 74 20        /* adjust 
b3af0 65 78 70 6f 6e 65 6e 74 20 66 6f 72 20 73 68 69  exponent for shi
b3b00 66 74 69 6e 67 20 64 65 63 69 6d 61 6c 20 70 6f  fting decimal po
b3b10 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 65 73 69  int */.  int esi
b3b20 67 6e 20 3d 20 31 3b 20 20 2f 2a 20 73 69 67 6e  gn = 1;  /* sign
b3b30 20 6f 66 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a   of exponent */.
b3b40 20 20 69 6e 74 20 65 20 3d 20 30 3b 20 20 20 20    int e = 0;    
b3b50 20 20 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 2a 2f    /* exponent */
b3b60 0a 20 20 64 6f 75 62 6c 65 20 72 65 73 75 6c 74  .  double result
b3b70 3b 0a 20 20 69 6e 74 20 6e 44 69 67 69 74 73 20  ;.  int nDigits 
b3b80 3d 20 30 3b 0a 0a 20 20 2f 2a 20 73 6b 69 70 20  = 0;..  /* skip 
b3b90 6c 65 61 64 69 6e 67 20 73 70 61 63 65 73 20 2a  leading spaces *
b3ba0 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74  /.  while( sqlit
b3bb0 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20  e3Isspace(*z) ) 
b3bc0 7a 2b 2b 3b 0a 20 20 2f 2a 20 67 65 74 20 73 69  z++;.  /* get si
b3bd0 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e  gn of significan
b3be0 64 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d 27  d */.  if( *z=='
b3bf0 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e 20 3d  -' ){.    sign =
b3c00 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20   -1;.    z++;.  
b3c10 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b  }else if( *z=='+
b3c20 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20  ' ){.    z++;.  
b3c30 7d 0a 20 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64  }.  /* skip lead
b3c40 69 6e 67 20 7a 65 72 6f 65 73 20 2a 2f 0a 20 20  ing zeroes */.  
b3c50 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30 27  while( z[0]=='0'
b3c60 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b   ) z++, nDigits+
b3c70 2b 3b 0a 0a 20 20 2f 2a 20 63 6f 70 79 20 6d 61  +;..  /* copy ma
b3c80 78 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69  x significant di
b3c90 67 69 74 73 20 74 6f 20 73 69 67 6e 69 66 69 63  gits to signific
b3ca0 61 6e 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  and */.  while( 
b3cb0 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
b3cc0 7a 29 20 26 26 20 73 3c 28 28 4c 41 52 47 45 53  z) && s<((LARGES
b3cd0 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30 29 20 29  T_INT64-9)/10) )
b3ce0 7b 0a 20 20 20 20 73 20 3d 20 73 2a 31 30 20 2b  {.    s = s*10 +
b3cf0 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20   (*z - '0');.   
b3d00 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 3b   z++, nDigits++;
b3d10 0a 20 20 7d 0a 20 20 2f 2a 20 73 6b 69 70 20 6e  .  }.  /* skip n
b3d20 6f 6e 2d 73 69 67 6e 69 66 69 63 61 6e 74 20 73  on-significant s
b3d30 69 67 6e 69 66 69 63 61 6e 64 20 64 69 67 69 74  ignificand digit
b3d40 73 0a 20 20 2a 2a 20 28 69 6e 63 72 65 61 73 65  s.  ** (increase
b3d50 20 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 20 74   exponent by d t
b3d60 6f 20 73 68 69 66 74 20 64 65 63 69 6d 61 6c 20  o shift decimal 
b3d70 6c 65 66 74 29 20 2a 2f 0a 20 20 77 68 69 6c 65  left) */.  while
b3d80 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
b3d90 28 2a 7a 29 20 29 20 7a 2b 2b 2c 20 6e 44 69 67  (*z) ) z++, nDig
b3da0 69 74 73 2b 2b 2c 20 64 2b 2b 3b 0a 0a 20 20 2f  its++, d++;..  /
b3db0 2a 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f 69  * if decimal poi
b3dc0 6e 74 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  nt is present */
b3dd0 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29  .  if( *z=='.' )
b3de0 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 2f  {.    z++;.    /
b3df0 2a 20 63 6f 70 79 20 64 69 67 69 74 73 20 66 72  * copy digits fr
b3e00 6f 6d 20 61 66 74 65 72 20 64 65 63 69 6d 61 6c  om after decimal
b3e10 20 74 6f 20 73 69 67 6e 69 66 69 63 61 6e 64 0a   to significand.
b3e20 20 20 20 20 2a 2a 20 28 64 65 63 72 65 61 73 65      ** (decrease
b3e30 20 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 20 74   exponent by d t
b3e40 6f 20 73 68 69 66 74 20 64 65 63 69 6d 61 6c 20  o shift decimal 
b3e50 72 69 67 68 74 29 20 2a 2f 0a 20 20 20 20 77 68  right) */.    wh
b3e60 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69  ile( sqlite3Isdi
b3e70 67 69 74 28 2a 7a 29 20 26 26 20 73 3c 28 28 4c  git(*z) && s<((L
b3e80 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 39 29 2f  ARGEST_INT64-9)/
b3e90 31 30 29 20 29 7b 0a 20 20 20 20 20 20 73 20 3d  10) ){.      s =
b3ea0 20 73 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30   s*10 + (*z - '0
b3eb0 27 29 3b 0a 20 20 20 20 20 20 7a 2b 2b 2c 20 6e  ');.      z++, n
b3ec0 44 69 67 69 74 73 2b 2b 2c 20 64 2d 2d 3b 0a 20  Digits++, d--;. 
b3ed0 20 20 20 7d 0a 20 20 20 20 2f 2a 20 73 6b 69 70     }.    /* skip
b3ee0 20 6e 6f 6e 2d 73 69 67 6e 69 66 69 63 61 6e 74   non-significant
b3ef0 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 77   digits */.    w
b3f00 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64  hile( sqlite3Isd
b3f10 69 67 69 74 28 2a 7a 29 20 29 20 7a 2b 2b 2c 20  igit(*z) ) z++, 
b3f20 6e 44 69 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a 0a  nDigits++;.  }..
b3f30 20 20 2f 2a 20 69 66 20 65 78 70 6f 6e 65 6e 74    /* if exponent
b3f40 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
b3f50 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20   if( *z=='e' || 
b3f60 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a  *z=='E' ){.    z
b3f70 2b 2b 3b 0a 20 20 20 20 2f 2a 20 67 65 74 20 73  ++;.    /* get s
b3f80 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65 6e 74 20  ign of exponent 
b3f90 2a 2f 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27  */.    if( *z=='
b3fa0 2d 27 20 29 7b 0a 20 20 20 20 20 20 65 73 69 67  -' ){.      esig
b3fb0 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7a 2b  n = -1;.      z+
b3fc0 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  +;.    }else if(
b3fd0 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20   *z=='+' ){.    
b3fe0 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20    z++;.    }.   
b3ff0 20 2f 2a 20 63 6f 70 79 20 64 69 67 69 74 73 20   /* copy digits 
b4000 74 6f 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20  to exponent */. 
b4010 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65     while( sqlite
b4020 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a  3Isdigit(*z) ){.
b4030 20 20 20 20 20 20 65 20 3d 20 65 2a 31 30 20 2b        e = e*10 +
b4040 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20   (*z - '0');.   
b4050 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     z++;.    }.  
b4060 7d 0a 0a 20 20 2f 2a 20 61 64 6a 75 73 74 20 65  }..  /* adjust e
b4070 78 70 6f 6e 65 6e 74 20 62 79 20 64 2c 20 61 6e  xponent by d, an
b4080 64 20 75 70 64 61 74 65 20 73 69 67 6e 20 2a 2f  d update sign */
b4090 0a 20 20 65 20 3d 20 28 65 2a 65 73 69 67 6e 29  .  e = (e*esign)
b40a0 20 2b 20 64 3b 0a 20 20 69 66 28 20 65 3c 30 20   + d;.  if( e<0 
b40b0 29 20 7b 0a 20 20 20 20 65 73 69 67 6e 20 3d 20  ) {.    esign = 
b40c0 2d 31 3b 0a 20 20 20 20 65 20 2a 3d 20 2d 31 3b  -1;.    e *= -1;
b40d0 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
b40e0 65 73 69 67 6e 20 3d 20 31 3b 0a 20 20 7d 0a 0a  esign = 1;.  }..
b40f0 20 20 2f 2a 20 69 66 20 30 20 73 69 67 6e 69 66    /* if 0 signif
b4100 69 63 61 6e 64 20 2a 2f 0a 20 20 69 66 28 20 21  icand */.  if( !
b4110 73 20 29 20 7b 0a 20 20 20 20 2f 2a 20 49 6e 20  s ) {.    /* In 
b4120 74 68 65 20 49 45 45 45 20 37 35 34 20 73 74 61  the IEEE 754 sta
b4130 6e 64 61 72 64 2c 20 7a 65 72 6f 20 69 73 20 73  ndard, zero is s
b4140 69 67 6e 65 64 2e 0a 20 20 20 20 2a 2a 20 41 64  igned..    ** Ad
b4150 64 20 74 68 65 20 73 69 67 6e 20 69 66 20 77 65  d the sign if we
b4160 27 76 65 20 73 65 65 6e 20 61 74 20 6c 65 61 73  've seen at leas
b4170 74 20 6f 6e 65 20 64 69 67 69 74 20 2a 2f 0a 20  t one digit */. 
b4180 20 20 20 72 65 73 75 6c 74 20 3d 20 28 73 69 67     result = (sig
b4190 6e 3c 30 20 26 26 20 6e 44 69 67 69 74 73 29 20  n<0 && nDigits) 
b41a0 3f 20 2d 28 64 6f 75 62 6c 65 29 30 20 3a 20 28  ? -(double)0 : (
b41b0 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 20 65 6c  double)0;.  } el
b41c0 73 65 20 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65  se {.    /* atte
b41d0 6d 70 74 20 74 6f 20 72 65 64 75 63 65 20 65 78  mpt to reduce ex
b41e0 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66  ponent */.    if
b41f0 28 20 65 73 69 67 6e 3e 30 20 29 7b 0a 20 20 20  ( esign>0 ){.   
b4200 20 20 20 77 68 69 6c 65 28 20 73 3c 28 4c 41 52     while( s<(LAR
b4210 47 45 53 54 5f 49 4e 54 36 34 2f 31 30 29 20 26  GEST_INT64/10) &
b4220 26 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2a 3d 31  & e>0 ) e--,s*=1
b4230 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
b4240 20 20 20 20 77 68 69 6c 65 28 20 21 28 73 25 31      while( !(s%1
b4250 30 29 20 26 26 20 65 3e 30 20 29 20 65 2d 2d 2c  0) && e>0 ) e--,
b4260 73 2f 3d 31 30 3b 0a 20 20 20 20 7d 0a 0a 20 20  s/=10;.    }..  
b4270 20 20 2f 2a 20 61 64 6a 75 73 74 20 74 68 65 20    /* adjust the 
b4280 73 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63  sign of signific
b4290 61 6e 64 20 2a 2f 0a 20 20 20 20 73 20 3d 20 73  and */.    s = s
b42a0 69 67 6e 3c 30 20 3f 20 2d 73 20 3a 20 73 3b 0a  ign<0 ? -s : s;.
b42b0 0a 20 20 20 20 2f 2a 20 69 66 20 65 78 70 6f 6e  .    /* if expon
b42c0 65 6e 74 2c 20 73 63 61 6c 65 20 73 69 67 6e 69  ent, scale signi
b42d0 66 69 63 61 6e 64 20 61 73 20 61 70 70 72 6f 70  ficand as approp
b42e0 72 69 61 74 65 0a 20 20 20 20 2a 2a 20 61 6e 64  riate.    ** and
b42f0 20 73 74 6f 72 65 20 69 6e 20 72 65 73 75 6c 74   store in result
b4300 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 20 29  . */.    if( e )
b4310 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 73  {.      double s
b4320 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20  cale = 1.0;.    
b4330 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20    /* attempt to 
b4340 68 61 6e 64 6c 65 20 65 78 74 72 65 6d 65 6c 79  handle extremely
b4350 20 73 6d 61 6c 6c 2f 6c 61 72 67 65 20 6e 75 6d   small/large num
b4360 62 65 72 73 20 62 65 74 74 65 72 20 2a 2f 0a 20  bers better */. 
b4370 20 20 20 20 20 69 66 28 20 65 3e 33 30 37 20 26       if( e>307 &
b4380 26 20 65 3c 33 34 32 20 29 7b 0a 20 20 20 20 20  & e<342 ){.     
b4390 20 20 20 77 68 69 6c 65 28 20 65 25 33 30 38 20     while( e%308 
b43a0 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30  ) { scale *= 1.0
b43b0 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a 20  e+1; e -= 1; }. 
b43c0 20 20 20 20 20 20 20 69 66 28 20 65 73 69 67 6e         if( esign
b43d0 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <0 ){.          
b43e0 72 65 73 75 6c 74 20 3d 20 73 20 2f 20 73 63 61  result = s / sca
b43f0 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  le;.          re
b4400 73 75 6c 74 20 2f 3d 20 31 2e 30 65 2b 33 30 38  sult /= 1.0e+308
b4410 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
b4420 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c  .          resul
b4430 74 20 3d 20 73 20 2a 20 73 63 61 6c 65 3b 0a 20  t = s * scale;. 
b4440 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20           result 
b4450 2a 3d 20 31 2e 30 65 2b 33 30 38 3b 0a 20 20 20  *= 1.0e+308;.   
b4460 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
b4470 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 31  se{.        /* 1
b4480 2e 30 65 2b 32 32 20 69 73 20 74 68 65 20 6c 61  .0e+22 is the la
b4490 72 67 65 73 74 20 70 6f 77 65 72 20 6f 66 20 31  rgest power of 1
b44a0 30 20 74 68 61 6e 20 63 61 6e 20 62 65 20 0a 20  0 than can be . 
b44b0 20 20 20 20 20 20 20 2a 2a 20 72 65 70 72 65 73         ** repres
b44c0 65 6e 74 65 64 20 65 78 61 63 74 6c 79 2e 20 2a  ented exactly. *
b44d0 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  /.        while(
b44e0 20 65 25 32 32 20 29 20 7b 20 73 63 61 6c 65 20   e%22 ) { scale 
b44f0 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d 20  *= 1.0e+1; e -= 
b4500 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69  1; }.        whi
b4510 6c 65 28 20 65 3e 30 20 29 20 7b 20 73 63 61 6c  le( e>0 ) { scal
b4520 65 20 2a 3d 20 31 2e 30 65 2b 32 32 3b 20 65 20  e *= 1.0e+22; e 
b4530 2d 3d 20 32 32 3b 20 7d 0a 20 20 20 20 20 20 20  -= 22; }.       
b4540 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a   if( esign<0 ){.
b4550 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74            result
b4560 20 3d 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 20   = s / scale;.  
b4570 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
b4580 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20         result = 
b4590 73 20 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 20  s * scale;.     
b45a0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
b45b0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
b45c0 72 65 73 75 6c 74 20 3d 20 28 64 6f 75 62 6c 65  result = (double
b45d0 29 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  )s;.    }.  }.. 
b45e0 20 2f 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65   /* store the re
b45f0 73 75 6c 74 20 2a 2f 0a 20 20 2a 70 52 65 73 75  sult */.  *pResu
b4600 6c 74 20 3d 20 72 65 73 75 6c 74 3b 0a 0a 20 20  lt = result;..  
b4610 2f 2a 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 72  /* return number
b4620 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 75   of characters u
b4630 73 65 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  sed */.  return 
b4640 28 69 6e 74 29 28 7a 20 2d 20 7a 42 65 67 69 6e  (int)(z - zBegin
b4650 29 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72  );.#else.  retur
b4660 6e 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28  n sqlite3Atoi64(
b4670 7a 2c 20 70 52 65 73 75 6c 74 29 3b 0a 23 65 6e  z, pResult);.#en
b4680 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
b4690 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
b46a0 54 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  T */.}../*.** Co
b46b0 6d 70 61 72 65 20 74 68 65 20 31 39 2d 63 68 61  mpare the 19-cha
b46c0 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 7a 4e  racter string zN
b46d0 75 6d 20 61 67 61 69 6e 73 74 20 74 68 65 20 74  um against the t
b46e0 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  ext representati
b46f0 6f 6e 0a 2a 2a 20 76 61 6c 75 65 20 32 5e 36 33  on.** value 2^63
b4700 3a 20 20 39 32 32 33 33 37 32 30 33 36 38 35 34  :  9223372036854
b4710 37 37 35 38 30 38 2e 20 20 52 65 74 75 72 6e 20  775808.  Return 
b4720 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20  negative, zero, 
b4730 6f 72 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 69  or positive.** i
b4740 66 20 7a 4e 75 6d 20 69 73 20 6c 65 73 73 20 74  f zNum is less t
b4750 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f  han, equal to, o
b4760 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  r greater than t
b4770 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  he string..**.**
b4780 20 55 6e 6c 69 6b 65 20 6d 65 6d 63 6d 70 28 29   Unlike memcmp()
b4790 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
b47a0 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 72   guaranteed to r
b47b0 65 74 75 72 6e 20 74 68 65 20 64 69 66 66 65 72  eturn the differ
b47c0 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65 20 76  ence.** in the v
b47d0 61 6c 75 65 73 20 6f 66 20 74 68 65 20 6c 61 73  alues of the las
b47e0 74 20 64 69 67 69 74 20 69 66 20 74 68 65 20 6f  t digit if the o
b47f0 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 69  nly difference i
b4800 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 74  s in the.** last
b4810 20 64 69 67 69 74 2e 20 20 53 6f 2c 20 66 6f 72   digit.  So, for
b4820 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20   example,.**.** 
b4830 20 20 20 20 20 63 6f 6d 70 61 72 65 32 70 6f 77       compare2pow
b4840 36 33 28 22 39 32 32 33 33 37 32 30 33 36 38 35  63("922337203685
b4850 34 37 37 35 38 30 30 22 29 0a 2a 2a 0a 2a 2a 20  4775800").**.** 
b4860 77 69 6c 6c 20 72 65 74 75 72 6e 20 2d 38 2e 0a  will return -8..
b4870 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
b4880 6d 70 61 72 65 32 70 6f 77 36 33 28 63 6f 6e 73  mpare2pow63(cons
b4890 74 20 63 68 61 72 20 2a 7a 4e 75 6d 29 7b 0a 20  t char *zNum){. 
b48a0 20 69 6e 74 20 63 3b 0a 20 20 63 20 3d 20 6d 65   int c;.  c = me
b48b0 6d 63 6d 70 28 7a 4e 75 6d 2c 22 39 32 32 33 33  mcmp(zNum,"92233
b48c0 37 32 30 33 36 38 35 34 37 37 35 38 30 22 2c 31  7203685477580",1
b48d0 38 29 2a 31 30 3b 0a 20 20 69 66 28 20 63 3d 3d  8)*10;.  if( c==
b48e0 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 7a 4e 75  0 ){.    c = zNu
b48f0 6d 5b 31 38 5d 20 2d 20 27 38 27 3b 0a 20 20 7d  m[18] - '8';.  }
b4900 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a  .  return c;.}..
b4910 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
b4920 55 45 20 69 66 20 7a 4e 75 6d 20 69 73 20 61 20  UE if zNum is a 
b4930 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
b4940 74 65 67 65 72 20 61 6e 64 20 77 72 69 74 65 0a  teger and write.
b4950 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
b4960 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 74 6f  the integer into
b4970 20 2a 70 4e 75 6d 2e 20 20 49 66 20 7a 4e 75 6d   *pNum.  If zNum
b4980 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67   is not an integ
b4990 65 72 0a 2a 2a 20 6f 72 20 69 73 20 61 6e 20 69  er.** or is an i
b49a0 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 74  nteger that is t
b49b0 6f 6f 20 6c 61 72 67 65 20 74 6f 20 62 65 20 65  oo large to be e
b49c0 78 70 72 65 73 73 65 64 20 77 69 74 68 20 36 34  xpressed with 64
b49d0 20 62 69 74 73 2c 0a 2a 2a 20 74 68 65 6e 20 72   bits,.** then r
b49e0 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a  eturn false..**.
b49f0 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f 75  ** When this rou
b4a00 74 69 6e 65 20 77 61 73 20 6f 72 69 67 69 6e 61  tine was origina
b4a10 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 74 20 64  lly written it d
b4a20 65 61 6c 74 20 77 69 74 68 20 6f 6e 6c 79 0a 2a  ealt with only.*
b4a30 2a 20 33 32 2d 62 69 74 20 6e 75 6d 62 65 72 73  * 32-bit numbers
b4a40 2e 20 20 41 74 20 74 68 61 74 20 74 69 6d 65 2c  .  At that time,
b4a50 20 69 74 20 77 61 73 20 6d 75 63 68 20 66 61 73   it was much fas
b4a60 74 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a 20  ter than the.** 
b4a70 61 74 6f 69 28 29 20 6c 69 62 72 61 72 79 20 72  atoi() library r
b4a80 6f 75 74 69 6e 65 20 69 6e 20 52 65 64 48 61 74  outine in RedHat
b4a90 20 37 2e 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   7.2..*/.SQLITE_
b4aa0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b4ab0 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20  te3Atoi64(const 
b4ac0 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 36 34 20  char *zNum, i64 
b4ad0 2a 70 4e 75 6d 29 7b 0a 20 20 69 36 34 20 76 20  *pNum){.  i64 v 
b4ae0 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 65 67 3b 0a  = 0;.  int neg;.
b4af0 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 63 6f    int i, c;.  co
b4b00 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74  nst char *zStart
b4b10 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74  ;.  while( sqlit
b4b20 65 33 49 73 73 70 61 63 65 28 2a 7a 4e 75 6d 29  e3Isspace(*zNum)
b4b30 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 69 66 28   ) zNum++;.  if(
b4b40 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a 20   *zNum=='-' ){. 
b4b50 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20     neg = 1;.    
b4b60 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20  zNum++;.  }else 
b4b70 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20 29  if( *zNum=='+' )
b4b80 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20  {.    neg = 0;. 
b4b90 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c     zNum++;.  }el
b4ba0 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b  se{.    neg = 0;
b4bb0 0a 20 20 7d 0a 20 20 7a 53 74 61 72 74 20 3d 20  .  }.  zStart = 
b4bc0 7a 4e 75 6d 3b 0a 20 20 77 68 69 6c 65 28 20 7a  zNum;.  while( z
b4bd0 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 7b 20 7a  Num[0]=='0' ){ z
b4be0 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a 20 53 6b 69 70  Num++; } /* Skip
b4bf0 20 6f 76 65 72 20 6c 65 61 64 69 6e 67 20 7a 65   over leading ze
b4c00 72 6f 73 2e 20 54 69 63 6b 65 74 20 23 32 34 35  ros. Ticket #245
b4c10 34 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  4 */.  for(i=0; 
b4c20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27  (c=zNum[i])>='0'
b4c30 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29   && c<='9'; i++)
b4c40 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b  {.    v = v*10 +
b4c50 20 63 20 2d 20 27 30 27 3b 0a 20 20 7d 0a 20 20   c - '0';.  }.  
b4c60 2a 70 4e 75 6d 20 3d 20 6e 65 67 20 3f 20 2d 76  *pNum = neg ? -v
b4c70 20 3a 20 76 3b 0a 20 20 69 66 28 20 63 21 3d 30   : v;.  if( c!=0
b4c80 20 7c 7c 20 28 69 3d 3d 30 20 26 26 20 7a 53 74   || (i==0 && zSt
b4c90 61 72 74 3d 3d 7a 4e 75 6d 29 20 7c 7c 20 69 3e  art==zNum) || i>
b4ca0 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 7a 4e 75  19 ){.    /* zNu
b4cb0 6d 20 69 73 20 65 6d 70 74 79 20 6f 72 20 63 6f  m is empty or co
b4cc0 6e 74 61 69 6e 73 20 6e 6f 6e 2d 6e 75 6d 65 72  ntains non-numer
b4cd0 69 63 20 74 65 78 74 20 6f 72 20 69 73 20 6c 6f  ic text or is lo
b4ce0 6e 67 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e  nger.    ** than
b4cf0 20 31 39 20 64 69 67 69 74 73 20 28 74 68 75 73   19 digits (thus
b4d00 20 67 75 61 72 61 6e 74 69 6e 67 20 74 68 61 74   guaranting that
b4d10 20 69 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 65   it is too large
b4d20 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  ) */.    return 
b4d30 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69  0;.  }else if( i
b4d40 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65  <19 ){.    /* Le
b4d50 73 73 20 74 68 61 6e 20 31 39 20 64 69 67 69 74  ss than 19 digit
b4d60 73 2c 20 73 6f 20 77 65 20 6b 6e 6f 77 20 74 68  s, so we know th
b4d70 61 74 20 69 74 20 66 69 74 73 20 69 6e 20 36 34  at it fits in 64
b4d80 20 62 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74   bits */.    ret
b4d90 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  urn 1;.  }else{.
b4da0 20 20 20 20 2f 2a 20 31 39 2d 64 69 67 69 74 20      /* 19-digit 
b4db0 6e 75 6d 62 65 72 73 20 6d 75 73 74 20 62 65 20  numbers must be 
b4dc0 6e 6f 20 6c 61 72 67 65 72 20 74 68 61 6e 20 39  no larger than 9
b4dd0 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
b4de0 30 37 20 69 66 20 70 6f 73 69 74 69 76 65 0a 20  07 if positive. 
b4df0 20 20 20 2a 2a 20 6f 72 20 39 32 32 33 33 37 32     ** or 9223372
b4e00 30 33 36 38 35 34 37 37 35 38 30 38 20 69 66 20  036854775808 if 
b4e10 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 20  negative.  Note 
b4e20 74 68 61 74 20 39 32 32 33 33 37 32 30 33 36 38  that 92233720368
b4e30 35 34 36 36 35 38 30 38 0a 20 20 20 20 2a 2a 20  54665808.    ** 
b4e40 69 73 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 20  is 2^63. */.    
b4e50 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65 32 70  return compare2p
b4e60 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a  ow63(zNum)<neg;.
b4e70 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
b4e80 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 72 65 70   string zNum rep
b4e90 72 65 73 65 6e 74 73 20 61 6e 20 75 6e 73 69 67  resents an unsig
b4ea0 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 54 68  ned integer.  Th
b4eb0 65 20 7a 4e 75 6d 20 73 74 72 69 6e 67 0a 2a 2a  e zNum string.**
b4ec0 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 6f 6e 65   consists of one
b4ed0 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 20 63   or more digit c
b4ee0 68 61 72 61 63 74 65 72 73 20 61 6e 64 20 69 73  haracters and is
b4ef0 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 0a 2a   terminated by.*
b4f00 2a 20 61 20 7a 65 72 6f 20 63 68 61 72 61 63 74  * a zero charact
b4f10 65 72 2e 20 20 41 6e 79 20 73 74 72 61 79 20 63  er.  Any stray c
b4f20 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 4e 75  haracters in zNu
b4f30 6d 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65  m result in unde
b4f40 66 69 6e 65 64 0a 2a 2a 20 62 65 68 61 76 69 6f  fined.** behavio
b4f50 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  r..**.** If the 
b4f60 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
b4f70 20 74 68 61 74 20 7a 4e 75 6d 20 72 65 70 72 65   that zNum repre
b4f80 73 65 6e 74 73 20 77 69 6c 6c 20 66 69 74 20 69  sents will fit i
b4f90 6e 20 61 0a 2a 2a 20 36 34 2d 62 69 74 20 73 69  n a.** 64-bit si
b4fa0 67 6e 65 64 20 69 6e 74 65 67 65 72 2c 20 72 65  gned integer, re
b4fb0 74 75 72 6e 20 54 52 55 45 2e 20 20 4f 74 68 65  turn TRUE.  Othe
b4fc0 72 77 69 73 65 20 72 65 74 75 72 6e 20 46 41 4c  rwise return FAL
b4fd0 53 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  SE..**.** If the
b4fe0 20 6e 65 67 46 6c 61 67 20 70 61 72 61 6d 65 74   negFlag paramet
b4ff0 65 72 20 69 73 20 74 72 75 65 2c 20 74 68 61 74  er is true, that
b5000 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 4e 75 6d   means that zNum
b5010 20 72 65 61 6c 6c 79 20 72 65 70 72 65 73 65 6e   really represen
b5020 74 73 0a 2a 2a 20 61 20 6e 65 67 61 74 69 76 65  ts.** a negative
b5030 20 6e 75 6d 62 65 72 2e 20 20 28 54 68 65 20 6c   number.  (The l
b5040 65 61 64 69 6e 67 20 22 2d 22 20 69 73 20 6f 6d  eading "-" is om
b5050 69 74 74 65 64 20 66 72 6f 6d 20 7a 4e 75 6d 2e  itted from zNum.
b5060 29 20 20 54 68 69 73 0a 2a 2a 20 70 61 72 61 6d  )  This.** param
b5070 65 74 65 72 20 69 73 20 6e 65 65 64 65 64 20 74  eter is needed t
b5080 6f 20 64 65 74 65 72 6d 69 6e 65 20 61 20 62 6f  o determine a bo
b5090 75 6e 64 61 72 79 20 63 61 73 65 2e 20 20 41 20  undary case.  A 
b50a0 73 74 72 69 6e 67 0a 2a 2a 20 6f 66 20 22 39 32  string.** of "92
b50b0 32 33 33 37 33 30 33 36 38 35 34 37 37 35 38 30  2337303685477580
b50c0 38 22 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65  8" returns false
b50d0 20 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 66   if negFlag is f
b50e0 61 6c 73 65 20 6f 72 20 74 72 75 65 0a 2a 2a 20  alse or true.** 
b50f0 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 74 72  if negFlag is tr
b5100 75 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 64 69 6e  ue..**.** Leadin
b5110 67 20 7a 65 72 6f 73 20 61 72 65 20 69 67 6e 6f  g zeros are igno
b5120 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  red..*/.SQLITE_P
b5130 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b5140 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 63  e3FitsIn64Bits(c
b5150 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c  onst char *zNum,
b5160 20 69 6e 74 20 6e 65 67 46 6c 61 67 29 7b 0a 20   int negFlag){. 
b5170 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 65   int i;.  int ne
b5180 67 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  g = 0;..  assert
b5190 28 20 7a 4e 75 6d 5b 30 5d 3e 3d 27 30 27 20 26  ( zNum[0]>='0' &
b51a0 26 20 7a 4e 75 6d 5b 30 5d 3c 3d 27 39 27 20 29  & zNum[0]<='9' )
b51b0 3b 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 61 6e 20  ; /* zNum is an 
b51c0 75 6e 73 69 67 6e 65 64 20 6e 75 6d 62 65 72 20  unsigned number 
b51d0 2a 2f 0a 0a 20 20 69 66 28 20 6e 65 67 46 6c 61  */..  if( negFla
b51e0 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e 65 67 3b  g ) neg = 1-neg;
b51f0 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e 75 6d 3d  .  while( *zNum=
b5200 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d  ='0' ){.    zNum
b5210 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70 20 6c 65  ++;   /* Skip le
b5220 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 20 54 69  ading zeros.  Ti
b5230 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20  cket #2454 */.  
b5240 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 4e 75  }.  for(i=0; zNu
b5250 6d 5b 69 5d 3b 20 69 2b 2b 29 7b 20 61 73 73 65  m[i]; i++){ asse
b5260 72 74 28 20 7a 4e 75 6d 5b 69 5d 3e 3d 27 30 27  rt( zNum[i]>='0'
b5270 20 26 26 20 7a 4e 75 6d 5b 69 5d 3c 3d 27 39 27   && zNum[i]<='9'
b5280 20 29 3b 20 7d 0a 20 20 69 66 28 20 69 3c 31 39   ); }.  if( i<19
b5290 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61   ){.    /* Guara
b52a0 6e 74 65 65 64 20 74 6f 20 66 69 74 20 69 66 20  nteed to fit if 
b52b0 6c 65 73 73 20 74 68 61 6e 20 31 39 20 64 69 67  less than 19 dig
b52c0 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  its */.    retur
b52d0 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 1;.  }else if(
b52e0 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20   i>19 ){.    /* 
b52f0 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65  Guaranteed to be
b5300 20 74 6f 6f 20 62 69 67 20 69 66 20 67 72 65 61   too big if grea
b5310 74 65 72 20 74 68 61 6e 20 31 39 20 64 69 67 69  ter than 19 digi
b5320 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  ts */.    return
b5330 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
b5340 20 2f 2a 20 43 6f 6d 70 61 72 65 20 61 67 61 69   /* Compare agai
b5350 6e 73 74 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20  nst 2^63. */.   
b5360 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65 32   return compare2
b5370 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b  pow63(zNum)<neg;
b5380 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  .  }.}../*.** If
b5390 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73   zNum represents
b53a0 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74   an integer that
b53b0 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 33 32 2d   will fit in 32-
b53c0 62 69 74 73 2c 20 74 68 65 6e 20 73 65 74 0a 2a  bits, then set.*
b53d0 2a 20 2a 70 56 61 6c 75 65 20 74 6f 20 74 68 61  * *pValue to tha
b53e0 74 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72 65  t integer and re
b53f0 74 75 72 6e 20 74 72 75 65 2e 20 20 4f 74 68 65  turn true.  Othe
b5400 72 77 69 73 65 20 72 65 74 75 72 6e 20 66 61 6c  rwise return fal
b5410 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e 6f  se..**.** Any no
b5420 6e 2d 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63  n-numeric charac
b5430 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ters that follow
b5440 69 6e 67 20 7a 4e 75 6d 20 61 72 65 20 69 67 6e  ing zNum are ign
b5450 6f 72 65 64 2e 0a 2a 2a 20 54 68 69 73 20 69 73  ored..** This is
b5460 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
b5470 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 29 20  sqlite3Atoi64() 
b5480 77 68 69 63 68 20 72 65 71 75 69 72 65 73 20 74  which requires t
b5490 68 65 0a 2a 2a 20 69 6e 70 75 74 20 6e 75 6d 62  he.** input numb
b54a0 65 72 20 74 6f 20 62 65 20 7a 65 72 6f 2d 74 65  er to be zero-te
b54b0 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c  rminated..*/.SQL
b54c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b54d0 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28  sqlite3GetInt32(
b54e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d  const char *zNum
b54f0 2c 20 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a  , int *pValue){.
b5500 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76    sqlite_int64 v
b5510 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c 20 63   = 0;.  int i, c
b5520 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b  ;.  int neg = 0;
b5530 0a 20 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d  .  if( zNum[0]==
b5540 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d  '-' ){.    neg =
b5550 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a   1;.    zNum++;.
b5560 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 4e 75 6d    }else if( zNum
b5570 5b 30 5d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20  [0]=='+' ){.    
b5580 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68  zNum++;.  }.  wh
b5590 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30  ile( zNum[0]=='0
b55a0 27 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 6f  ' ) zNum++;.  fo
b55b0 72 28 69 3d 30 3b 20 69 3c 31 31 20 26 26 20 28  r(i=0; i<11 && (
b55c0 63 20 3d 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 30  c = zNum[i] - '0
b55d0 27 29 3e 3d 30 20 26 26 20 63 3c 3d 39 3b 20 69  ')>=0 && c<=9; i
b55e0 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31  ++){.    v = v*1
b55f0 30 20 2b 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  0 + c;.  }..  /*
b5600 20 54 68 65 20 6c 6f 6e 67 65 73 74 20 64 65 63   The longest dec
b5610 69 6d 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74  imal representat
b5620 69 6f 6e 20 6f 66 20 61 20 33 32 20 62 69 74 20  ion of a 32 bit 
b5630 69 6e 74 65 67 65 72 20 69 73 20 31 30 20 64 69  integer is 10 di
b5640 67 69 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  gits:.  **.  ** 
b5650 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33 34              1234
b5660 35 36 37 38 39 30 0a 20 20 2a 2a 20 20 20 20 20  567890.  **     
b5670 32 5e 33 31 20 2d 3e 20 32 31 34 37 34 38 33 36  2^31 -> 21474836
b5680 34 38 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 3e  48.  */.  if( i>
b5690 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  10 ){.    return
b56a0 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76 2d   0;.  }.  if( v-
b56b0 6e 65 67 3e 32 31 34 37 34 38 33 36 34 37 20 29  neg>2147483647 )
b56c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
b56d0 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 20 29 7b    }.  if( neg ){
b56e0 0a 20 20 20 20 76 20 3d 20 2d 76 3b 0a 20 20 7d  .    v = -v;.  }
b56f0 0a 20 20 2a 70 56 61 6c 75 65 20 3d 20 28 69 6e  .  *pValue = (in
b5700 74 29 76 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  t)v;.  return 1;
b5710 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61  .}../*.** The va
b5720 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
b5730 74 65 67 65 72 20 65 6e 63 6f 64 69 6e 67 20 69  teger encoding i
b5740 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
b5750 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20  .** KEY:.**     
b5760 20 20 20 20 41 20 3d 20 30 78 78 78 78 78 78 78      A = 0xxxxxxx
b5770 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61      7 bits of da
b5780 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20  ta and one flag 
b5790 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 42  bit.**         B
b57a0 20 3d 20 31 78 78 78 78 78 78 78 20 20 20 20 37   = 1xxxxxxx    7
b57b0 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e   bits of data an
b57c0 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a  d one flag bit.*
b57d0 2a 20 20 20 20 20 20 20 20 20 43 20 3d 20 78 78  *         C = xx
b57e0 78 78 78 78 78 78 20 20 20 20 38 20 62 69 74 73  xxxxxx    8 bits
b57f0 20 6f 66 20 64 61 74 61 0a 2a 2a 0a 2a 2a 20 20   of data.**.**  
b5800 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 31 34  7 bits - A.** 14
b5810 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 32 31   bits - BA.** 21
b5820 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 20 32   bits - BBA.** 2
b5830 38 20 62 69 74 73 20 2d 20 42 42 42 41 0a 2a 2a  8 bits - BBBA.**
b5840 20 33 35 20 62 69 74 73 20 2d 20 42 42 42 42 41   35 bits - BBBBA
b5850 0a 2a 2a 20 34 32 20 62 69 74 73 20 2d 20 42 42  .** 42 bits - BB
b5860 42 42 42 41 0a 2a 2a 20 34 39 20 62 69 74 73 20  BBBA.** 49 bits 
b5870 2d 20 42 42 42 42 42 42 41 0a 2a 2a 20 35 36 20  - BBBBBBA.** 56 
b5880 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 41 0a  bits - BBBBBBBA.
b5890 2a 2a 20 36 34 20 62 69 74 73 20 2d 20 42 42 42  ** 64 bits - BBB
b58a0 42 42 42 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  BBBBBC.*/../*.**
b58b0 20 57 72 69 74 65 20 61 20 36 34 2d 62 69 74 20   Write a 64-bit 
b58c0 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
b58d0 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d 6f 72  integer to memor
b58e0 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b  y starting at p[
b58f0 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e 67 74  0]..** The lengt
b5900 68 20 6f 66 20 64 61 74 61 20 77 72 69 74 65 20  h of data write 
b5910 77 69 6c 6c 20 62 65 20 62 65 74 77 65 65 6e 20  will be between 
b5920 31 20 61 6e 64 20 39 20 62 79 74 65 73 2e 20 20  1 and 9 bytes.  
b5930 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66  The number.** of
b5940 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20 69   bytes written i
b5950 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
b5960 2a 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  * A variable-len
b5970 67 74 68 20 69 6e 74 65 67 65 72 20 63 6f 6e 73  gth integer cons
b5980 69 73 74 73 20 6f 66 20 74 68 65 20 6c 6f 77 65  ists of the lowe
b5990 72 20 37 20 62 69 74 73 20 6f 66 20 65 61 63 68  r 7 bits of each
b59a0 20 62 79 74 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c   byte.** for all
b59b0 20 62 79 74 65 73 20 74 68 61 74 20 68 61 76 65   bytes that have
b59c0 20 74 68 65 20 38 74 68 20 62 69 74 20 73 65 74   the 8th bit set
b59d0 20 61 6e 64 20 6f 6e 65 20 62 79 74 65 20 77 69   and one byte wi
b59e0 74 68 20 74 68 65 20 38 74 68 0a 2a 2a 20 62 69  th the 8th.** bi
b59f0 74 20 63 6c 65 61 72 2e 20 20 45 78 63 65 70 74  t clear.  Except
b5a00 2c 20 69 66 20 77 65 20 67 65 74 20 74 6f 20 74  , if we get to t
b5a10 68 65 20 39 74 68 20 62 79 74 65 2c 20 69 74 20  he 9th byte, it 
b5a20 73 74 6f 72 65 73 20 74 68 65 20 66 75 6c 6c 0a  stores the full.
b5a30 2a 2a 20 38 20 62 69 74 73 20 61 6e 64 20 69 73  ** 8 bits and is
b5a40 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 2e 0a   the last byte..
b5a50 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b5a60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74  E int sqlite3Put
b5a70 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64 20  Varint(unsigned 
b5a80 63 68 61 72 20 2a 70 2c 20 75 36 34 20 76 29 7b  char *p, u64 v){
b5a90 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a  .  int i, j, n;.
b5aa0 20 20 75 38 20 62 75 66 5b 31 30 5d 3b 0a 20 20    u8 buf[10];.  
b5ab0 69 66 28 20 76 20 26 20 28 28 28 75 36 34 29 30  if( v & (((u64)0
b5ac0 78 66 66 30 30 30 30 30 30 29 3c 3c 33 32 29 20  xff000000)<<32) 
b5ad0 29 7b 0a 20 20 20 20 70 5b 38 5d 20 3d 20 28 75  ){.    p[8] = (u
b5ae0 38 29 76 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 38  8)v;.    v >>= 8
b5af0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 37 3b 20 69  ;.    for(i=7; i
b5b00 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20  >=0; i--){.     
b5b10 20 70 5b 69 5d 20 3d 20 28 75 38 29 28 28 76 20   p[i] = (u8)((v 
b5b20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 29 3b  & 0x7f) | 0x80);
b5b30 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a  .      v >>= 7;.
b5b40 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
b5b50 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 20 6e 20   9;.  }    .  n 
b5b60 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 62  = 0;.  do{.    b
b5b70 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75 38 29 28 28  uf[n++] = (u8)((
b5b80 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30  v & 0x7f) | 0x80
b5b90 29 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a  );.    v >>= 7;.
b5ba0 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 29    }while( v!=0 )
b5bb0 3b 0a 20 20 62 75 66 5b 30 5d 20 26 3d 20 30 78  ;.  buf[0] &= 0x
b5bc0 37 66 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c  7f;.  assert( n<
b5bd0 3d 39 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  =9 );.  for(i=0,
b5be0 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d   j=n-1; j>=0; j-
b5bf0 2d 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 70 5b 69  -, i++){.    p[i
b5c00 5d 20 3d 20 62 75 66 5b 6a 5d 3b 0a 20 20 7d 0a  ] = buf[j];.  }.
b5c10 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f    return n;.}../
b5c20 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
b5c30 65 20 69 73 20 61 20 66 61 73 74 65 72 20 76 65  e is a faster ve
b5c40 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  rsion of sqlite3
b5c50 50 75 74 56 61 72 69 6e 74 28 29 20 74 68 61 74  PutVarint() that
b5c60 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66   only.** works f
b5c70 6f 72 20 33 32 2d 62 69 74 20 70 6f 73 69 74 69  or 32-bit positi
b5c80 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20  ve integers and 
b5c90 77 68 69 63 68 20 69 73 20 6f 70 74 69 6d 69 7a  which is optimiz
b5ca0 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f  ed for.** the co
b5cb0 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 20 73 6d 61  mmon case of sma
b5cc0 6c 6c 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20  ll integers.  A 
b5cd0 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 70  MACRO version, p
b5ce0 75 74 56 61 72 69 6e 74 33 32 2c 0a 2a 2a 20 69  utVarint32,.** i
b5cf0 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63 68  s provided which
b5d00 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 73 69 6e   inlines the sin
b5d10 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20 20  gle-byte case.  
b5d20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  All code should 
b5d30 75 73 65 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f  use.** the MACRO
b5d40 20 76 65 72 73 69 6f 6e 20 61 73 20 74 68 69 73   version as this
b5d50 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65   function assume
b5d60 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 74  s the single-byt
b5d70 65 20 63 61 73 65 20 68 61 73 0a 2a 2a 20 61 6c  e case has.** al
b5d80 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c  ready been handl
b5d90 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
b5da0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b5db0 33 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73  3PutVarint32(uns
b5dc0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75  igned char *p, u
b5dd0 33 32 20 76 29 7b 0a 23 69 66 6e 64 65 66 20 70  32 v){.#ifndef p
b5de0 75 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66 28  utVarint32.  if(
b5df0 20 28 76 20 26 20 7e 30 78 37 66 29 3d 3d 30 20   (v & ~0x7f)==0 
b5e00 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 76 3b  ){.    p[0] = v;
b5e10 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
b5e20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20   }.#endif.  if( 
b5e30 28 76 20 26 20 7e 30 78 33 66 66 66 29 3d 3d 30  (v & ~0x3fff)==0
b5e40 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 28   ){.    p[0] = (
b5e50 75 38 29 28 28 76 3e 3e 37 29 20 7c 20 30 78 38  u8)((v>>7) | 0x8
b5e60 30 29 3b 0a 20 20 20 20 70 5b 31 5d 20 3d 20 28  0);.    p[1] = (
b5e70 75 38 29 28 76 20 26 20 30 78 37 66 29 3b 0a 20  u8)(v & 0x7f);. 
b5e80 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d     return 2;.  }
b5e90 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
b5ea0 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 76 29  3PutVarint(p, v)
b5eb0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
b5ec0 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c  a 64-bit variabl
b5ed0 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
b5ee0 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61   from memory sta
b5ef0 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a  rting at p[0]..*
b5f00 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
b5f10 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61  ber of bytes rea
b5f20 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73  d.  The value is
b5f30 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a   stored in *v..*
b5f40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b5f50 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61   u8 sqlite3GetVa
b5f60 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67  rint(const unsig
b5f70 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36 34  ned char *p, u64
b5f80 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 62 2c   *v){.  u32 a,b,
b5f90 73 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20  s;..  a = *p;.  
b5fa0 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b  /* a: p0 (unmask
b5fb0 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61  ed) */.  if (!(a
b5fc0 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20  &0x80)).  {.    
b5fd0 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75  *v = a;.    retu
b5fe0 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b  rn 1;.  }..  p++
b5ff0 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a  ;.  b = *p;.  /*
b6000 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65 64   b: p1 (unmasked
b6010 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30  ) */.  if (!(b&0
b6020 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20  x80)).  {.    a 
b6030 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 3d  &= 0x7f;.    a =
b6040 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20   a<<7;.    a |= 
b6050 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20  b;.    *v = a;. 
b6060 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d     return 2;.  }
b6070 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61  ..  p++;.  a = a
b6080 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b  <<14;.  a |= *p;
b6090 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20  .  /* a: p0<<14 
b60a0 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 20  | p2 (unmasked) 
b60b0 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  */.  if (!(a&0x8
b60c0 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d  0)).  {.    a &=
b60d0 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
b60e0 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 78 37  f);.    b &= 0x7
b60f0 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b  f;.    b = b<<7;
b6100 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20  .    a |= b;.   
b6110 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74   *v = a;.    ret
b6120 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 3;.  }..  /*
b6130 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c 6f 77   CSE1 from below
b6140 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 37 66   */.  a &= (0x7f
b6150 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
b6160 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34  p++;.  b = b<<14
b6170 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  b |= *p;.  /
b6180 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33  * b: p1<<14 | p3
b6190 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
b61a0 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a   if (!(b&0x80)).
b61b0 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78    {.    b &= (0x
b61c0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b61d0 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45      /* moved CSE
b61e0 31 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61  1 up */.    /* a
b61f0 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
b6200 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20  0x7f); */.    a 
b6210 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d  = a<<7;.    a |=
b6220 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a   b;.    *v = a;.
b6230 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 20      return 4;.  
b6240 7d 0a 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31  }..  /* a: p0<<1
b6250 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 20  4 | p2 (masked) 
b6260 2a 2f 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31  */.  /* b: p1<<1
b6270 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64  4 | p3 (unmasked
b6280 29 20 2a 2f 0a 20 20 2f 2a 20 31 3a 73 61 76 65  ) */.  /* 1:save
b6290 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 70 31   off p0<<21 | p1
b62a0 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70  <<14 | p2<<7 | p
b62b0 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  3 (masked) */.  
b62c0 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75 70  /* moved CSE1 up
b62d0 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30   */.  /* a &= (0
b62e0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b62f0 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 37 66   */.  b &= (0x7f
b6300 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
b6310 73 20 3d 20 61 3b 0a 20 20 2f 2a 20 73 3a 20 70  s = a;.  /* s: p
b6320 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 73 6b  0<<14 | p2 (mask
b6330 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20  ed) */..  p++;. 
b6340 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20   a = a<<14;.  a 
b6350 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70  |= *p;.  /* a: p
b6360 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c  0<<28 | p2<<14 |
b6370 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a   p4 (unmasked) *
b6380 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30  /.  if (!(a&0x80
b6390 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65  )).  {.    /* we
b63a0 20 63 61 6e 20 73 6b 69 70 20 74 68 65 73 65 20   can skip these 
b63b0 63 61 75 73 65 20 74 68 65 79 20 77 65 72 65 20  cause they were 
b63c0 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 6f  (effectively) do
b63d0 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c 63  ne above in calc
b63e0 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a  'ing s */.    /*
b63f0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29   a &= (0x7f<<28)
b6400 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37  |(0x7f<<14)|(0x7
b6410 66 29 3b 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20  f); */.    /* b 
b6420 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  &= (0x7f<<14)|(0
b6430 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 62 20 3d  x7f); */.    b =
b6440 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20   b<<7;.    a |= 
b6450 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 38  b;.    s = s>>18
b6460 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34  ;.    *v = ((u64
b6470 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20  )s)<<32 | a;.   
b6480 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a   return 5;.  }..
b6490 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66 66 20    /* 2:save off 
b64a0 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20  p0<<21 | p1<<14 
b64b0 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61  | p2<<7 | p3 (ma
b64c0 73 6b 65 64 29 20 2a 2f 0a 20 20 73 20 3d 20 73  sked) */.  s = s
b64d0 3c 3c 37 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 20  <<7;.  s |= b;. 
b64e0 20 2f 2a 20 73 3a 20 70 30 3c 3c 32 31 20 7c 20   /* s: p0<<21 | 
b64f0 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c  p1<<14 | p2<<7 |
b6500 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a   p3 (masked) */.
b6510 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c  .  p++;.  b = b<
b6520 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a  <14;.  b |= *p;.
b6530 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 32 38 20 7c    /* b: p1<<28 |
b6540 20 70 33 3c 3c 31 34 20 7c 20 70 35 20 28 75 6e   p3<<14 | p5 (un
b6550 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20  masked) */.  if 
b6560 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a  (!(b&0x80)).  {.
b6570 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b      /* we can sk
b6580 69 70 20 74 68 69 73 20 63 61 75 73 65 20 69 74  ip this cause it
b6590 20 77 61 73 20 28 65 66 66 65 63 74 69 76 65 6c   was (effectivel
b65a0 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e  y) done above in
b65b0 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20   calc'ing s */. 
b65c0 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 37 66     /* b &= (0x7f
b65d0 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29  <<28)|(0x7f<<14)
b65e0 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20  |(0x7f); */.    
b65f0 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
b6600 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d 20  (0x7f);.    a = 
b6610 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62  a<<7;.    a |= b
b6620 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b  ;.    s = s>>18;
b6630 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29  .    *v = ((u64)
b6640 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20  s)<<32 | a;.    
b6650 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a 20  return 6;.  }.. 
b6660 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31   p++;.  a = a<<1
b6670 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  a |= *p;.  
b6680 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c 20 70  /* a: p2<<28 | p
b6690 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e 6d 61  4<<14 | p6 (unma
b66a0 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b66b0 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (a&0x80)).  {.  
b66c0 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c 32 38    a &= (0x1f<<28
b66d0 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  )|(0x7f<<14)|(0x
b66e0 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 30  7f);.    b &= (0
b66f0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b6700 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20  .    b = b<<7;. 
b6710 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73     a |= b;.    s
b6720 20 3d 20 73 3e 3e 31 31 3b 0a 20 20 20 20 2a 76   = s>>11;.    *v
b6730 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20   = ((u64)s)<<32 
b6740 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  | a;.    return 
b6750 37 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45  7;.  }..  /* CSE
b6760 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a  2 from below */.
b6770 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    a &= (0x7f<<14
b6780 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b  )|(0x7f);.  p++;
b6790 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20  .  b = b<<14;.  
b67a0 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a  b |= *p;.  /* b:
b67b0 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 34   p3<<28 | p5<<14
b67c0 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65 64 29   | p7 (unmasked)
b67d0 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78   */.  if (!(b&0x
b67e0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20 26  80)).  {.    b &
b67f0 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78  = (0x1f<<28)|(0x
b6800 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b6810 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45      /* moved CSE
b6820 32 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61  2 up */.    /* a
b6830 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
b6840 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20  0x7f); */.    a 
b6850 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d  = a<<7;.    a |=
b6860 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 34   b;.    s = s>>4
b6870 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34  ;.    *v = ((u64
b6880 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20  )s)<<32 | a;.   
b6890 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 7d 0a 0a   return 8;.  }..
b68a0 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c    p++;.  a = a<<
b68b0 31 35 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20  15;.  a |= *p;. 
b68c0 20 2f 2a 20 61 3a 20 70 34 3c 3c 32 39 20 7c 20   /* a: p4<<29 | 
b68d0 70 36 3c 3c 31 35 20 7c 20 70 38 20 28 75 6e 6d  p6<<15 | p8 (unm
b68e0 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a 20  asked) */..  /* 
b68f0 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 2a 2f  moved CSE2 up */
b6900 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66  .  /* a &= (0x7f
b6910 3c 3c 32 39 29 7c 28 30 78 37 66 3c 3c 31 35 29  <<29)|(0x7f<<15)
b6920 7c 28 30 78 66 66 29 3b 20 2a 2f 0a 20 20 62 20  |(0xff); */.  b 
b6930 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  &= (0x7f<<14)|(0
b6940 78 37 66 29 3b 0a 20 20 62 20 3d 20 62 3c 3c 38  x7f);.  b = b<<8
b6950 3b 0a 20 20 61 20 7c 3d 20 62 3b 0a 0a 20 20 73  ;.  a |= b;..  s
b6960 20 3d 20 73 3c 3c 34 3b 0a 20 20 62 20 3d 20 70   = s<<4;.  b = p
b6970 5b 2d 34 5d 3b 0a 20 20 62 20 26 3d 20 30 78 37  [-4];.  b &= 0x7
b6980 66 3b 0a 20 20 62 20 3d 20 62 3e 3e 33 3b 0a 20  f;.  b = b>>3;. 
b6990 20 73 20 7c 3d 20 62 3b 0a 0a 20 20 2a 76 20 3d   s |= b;..  *v =
b69a0 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20   ((u64)s)<<32 | 
b69b0 61 3b 0a 0a 20 20 72 65 74 75 72 6e 20 39 3b 0a  a;..  return 9;.
b69c0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20  }../*.** Read a 
b69d0 33 32 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d  32-bit variable-
b69e0 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 66  length integer f
b69f0 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 74  rom memory start
b6a00 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20  ing at p[0]..** 
b6a10 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
b6a20 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e  r of bytes read.
b6a30 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73    The value is s
b6a40 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2a 0a  tored in *v..**.
b6a50 2a 2a 20 49 66 20 74 68 65 20 76 61 72 69 6e 74  ** If the varint
b6a60 20 73 74 6f 72 65 64 20 69 6e 20 70 5b 30 5d 20   stored in p[0] 
b6a70 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 63  is larger than c
b6a80 61 6e 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62  an fit in a 32-b
b6a90 69 74 20 75 6e 73 69 67 6e 65 64 0a 2a 2a 20 69  it unsigned.** i
b6aa0 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 73 65 74  nteger, then set
b6ab0 20 2a 76 20 74 6f 20 30 78 66 66 66 66 66 66 66   *v to 0xfffffff
b6ac0 66 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 41 43 52 4f  f..**.** A MACRO
b6ad0 20 76 65 72 73 69 6f 6e 2c 20 67 65 74 56 61 72   version, getVar
b6ae0 69 6e 74 33 32 2c 20 69 73 20 70 72 6f 76 69 64  int32, is provid
b6af0 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e 65 73  ed which inlines
b6b00 20 74 68 65 20 0a 2a 2a 20 73 69 6e 67 6c 65 2d   the .** single-
b6b10 62 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20  byte case.  All 
b6b20 63 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 20  code should use 
b6b30 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69 6f  the MACRO versio
b6b40 6e 20 61 73 20 0a 2a 2a 20 74 68 69 73 20 66 75  n as .** this fu
b6b50 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  nction assumes t
b6b60 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63  he single-byte c
b6b70 61 73 65 20 68 61 73 20 61 6c 72 65 61 64 79 20  ase has already 
b6b80 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f  been handled..*/
b6b90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b6ba0 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 72  u8 sqlite3GetVar
b6bb0 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 69  int32(const unsi
b6bc0 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33  gned char *p, u3
b6bd0 32 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 62  2 *v){.  u32 a,b
b6be0 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 31 2d 62 79  ;..  /* The 1-by
b6bf0 74 65 20 63 61 73 65 2e 20 20 4f 76 65 72 77 68  te case.  Overwh
b6c00 65 6c 6d 69 6e 67 6c 79 20 74 68 65 20 6d 6f 73  elmingly the mos
b6c10 74 20 63 6f 6d 6d 6f 6e 2e 20 20 48 61 6e 64 6c  t common.  Handl
b6c20 65 64 20 69 6e 6c 69 6e 65 0a 20 20 2a 2a 20 62  ed inline.  ** b
b6c30 79 20 74 68 65 20 67 65 74 56 61 72 69 6e 33 32  y the getVarin32
b6c40 28 29 20 6d 61 63 72 6f 20 2a 2f 0a 20 20 61 20  () macro */.  a 
b6c50 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30  = *p;.  /* a: p0
b6c60 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 23   (unmasked) */.#
b6c70 69 66 6e 64 65 66 20 67 65 74 56 61 72 69 6e 74  ifndef getVarint
b6c80 33 32 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  32.  if (!(a&0x8
b6c90 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56  0)).  {.    /* V
b6ca0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 30 20  alues between 0 
b6cb0 61 6e 64 20 31 32 37 20 2a 2f 0a 20 20 20 20 2a  and 127 */.    *
b6cc0 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72  v = a;.    retur
b6cd0 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  n 1;.  }.#endif.
b6ce0 0a 20 20 2f 2a 20 54 68 65 20 32 2d 62 79 74 65  .  /* The 2-byte
b6cf0 20 63 61 73 65 20 2a 2f 0a 20 20 70 2b 2b 3b 0a   case */.  p++;.
b6d00 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62    b = *p;.  /* b
b6d10 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 29 20  : p1 (unmasked) 
b6d20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 38  */.  if (!(b&0x8
b6d30 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56  0)).  {.    /* V
b6d40 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 32  alues between 12
b6d50 38 20 61 6e 64 20 31 36 33 38 33 20 2a 2f 0a 20  8 and 16383 */. 
b6d60 20 20 20 61 20 26 3d 20 30 78 37 66 3b 0a 20 20     a &= 0x7f;.  
b6d70 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20    a = a<<7;.    
b6d80 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20  *v = a | b;.    
b6d90 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20  return 2;.  }.. 
b6da0 20 2f 2a 20 54 68 65 20 33 2d 62 79 74 65 20 63   /* The 3-byte c
b6db0 61 73 65 20 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20  ase */.  p++;.  
b6dc0 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c  a = a<<14;.  a |
b6dd0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30  = *p;.  /* a: p0
b6de0 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d 61 73  <<14 | p2 (unmas
b6df0 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
b6e00 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  a&0x80)).  {.   
b6e10 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65   /* Values betwe
b6e20 65 6e 20 31 36 33 38 34 20 61 6e 64 20 32 30 39  en 16384 and 209
b6e30 37 31 35 31 20 2a 2f 0a 20 20 20 20 61 20 26 3d  7151 */.    a &=
b6e40 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
b6e50 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 78 37  f);.    b &= 0x7
b6e60 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b  f;.    b = b<<7;
b6e70 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b  .    *v = a | b;
b6e80 0a 20 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20  .    return 3;. 
b6e90 20 7d 0a 0a 20 20 2f 2a 20 41 20 33 32 2d 62 69   }..  /* A 32-bi
b6ea0 74 20 76 61 72 69 6e 74 20 69 73 20 75 73 65 64  t varint is used
b6eb0 20 74 6f 20 73 74 6f 72 65 20 73 69 7a 65 20 69   to store size i
b6ec0 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 62 74  nformation in bt
b6ed0 72 65 65 73 2e 0a 20 20 2a 2a 20 4f 62 6a 65 63  rees..  ** Objec
b6ee0 74 73 20 61 72 65 20 72 61 72 65 6c 79 20 6c 61  ts are rarely la
b6ef0 72 67 65 72 20 74 68 61 6e 20 32 4d 69 42 20 6c  rger than 2MiB l
b6f00 69 6d 69 74 20 6f 66 20 61 20 33 2d 62 79 74 65  imit of a 3-byte
b6f10 20 76 61 72 69 6e 74 2e 0a 20 20 2a 2a 20 41 20   varint..  ** A 
b6f20 33 2d 62 79 74 65 20 76 61 72 69 6e 74 20 69 73  3-byte varint is
b6f30 20 73 75 66 66 69 63 69 65 6e 74 2c 20 66 6f 72   sufficient, for
b6f40 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 72 65 63   example, to rec
b6f50 6f 72 64 20 74 68 65 20 73 69 7a 65 0a 20 20 2a  ord the size.  *
b6f60 2a 20 6f 66 20 61 20 31 30 34 38 35 36 39 2d 62  * of a 1048569-b
b6f70 79 74 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 69  yte BLOB or stri
b6f80 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65  ng..  **.  ** We
b6f90 20 6f 6e 6c 79 20 75 6e 72 6f 6c 6c 20 74 68 65   only unroll the
b6fa0 20 66 69 72 73 74 20 31 2d 2c 20 32 2d 2c 20 61   first 1-, 2-, a
b6fb0 6e 64 20 33 2d 20 62 79 74 65 20 63 61 73 65 73  nd 3- byte cases
b6fc0 2e 20 20 54 68 65 20 76 65 72 79 0a 20 20 2a 2a  .  The very.  **
b6fd0 20 72 61 72 65 20 6c 61 72 67 65 72 20 63 61 73   rare larger cas
b6fe0 65 73 20 63 61 6e 20 62 65 20 68 61 6e 64 6c 65  es can be handle
b6ff0 64 20 62 79 20 74 68 65 20 73 6c 6f 77 65 72 20  d by the slower 
b7000 36 34 2d 62 69 74 20 76 61 72 69 6e 74 0a 20 20  64-bit varint.  
b7010 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a 20 20 2a 2f  ** routine..  */
b7020 0a 23 69 66 20 31 0a 20 20 7b 0a 20 20 20 20 75  .#if 1.  {.    u
b7030 36 34 20 76 36 34 3b 0a 20 20 20 20 75 38 20 6e  64 v64;.    u8 n
b7040 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 32 3b 0a 20  ;..    p -= 2;. 
b7050 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65     n = sqlite3Ge
b7060 74 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34 29  tVarint(p, &v64)
b7070 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e  ;.    assert( n>
b7080 33 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 20  3 && n<=9 );.   
b7090 20 69 66 28 20 28 76 36 34 20 26 20 53 51 4c 49   if( (v64 & SQLI
b70a0 54 45 5f 4d 41 58 5f 55 33 32 29 21 3d 76 36 34  TE_MAX_U32)!=v64
b70b0 20 29 7b 0a 20 20 20 20 20 20 2a 76 20 3d 20 30   ){.      *v = 0
b70c0 78 66 66 66 66 66 66 66 66 3b 0a 20 20 20 20 7d  xffffffff;.    }
b70d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 76 20 3d  else{.      *v =
b70e0 20 28 75 33 32 29 76 36 34 3b 0a 20 20 20 20 7d   (u32)v64;.    }
b70f0 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20  .    return n;. 
b7100 20 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 46   }..#else.  /* F
b7110 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64  or following cod
b7120 65 20 28 6b 65 70 74 20 66 6f 72 20 68 69 73 74  e (kept for hist
b7130 6f 72 69 63 61 6c 20 72 65 63 6f 72 64 20 6f 6e  orical record on
b7140 6c 79 29 20 73 68 6f 77 73 20 61 6e 0a 20 20 2a  ly) shows an.  *
b7150 2a 20 75 6e 72 6f 6c 6c 69 6e 67 20 66 6f 72 20  * unrolling for 
b7160 74 68 65 20 33 2d 20 61 6e 64 20 34 2d 62 79 74  the 3- and 4-byt
b7170 65 20 76 61 72 69 6e 74 20 63 61 73 65 73 2e 20  e varint cases. 
b7180 20 54 68 69 73 20 63 6f 64 65 20 69 73 0a 20 20   This code is.  
b7190 2a 2a 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74  ** slightly fast
b71a0 65 72 2c 20 62 75 74 20 69 74 20 69 73 20 61 6c  er, but it is al
b71b0 73 6f 20 6c 61 72 67 65 72 20 61 6e 64 20 6d 75  so larger and mu
b71c0 63 68 20 68 61 72 64 65 72 20 74 6f 20 74 65 73  ch harder to tes
b71d0 74 2e 0a 20 20 2a 2f 0a 20 20 70 2b 2b 3b 0a 20  t..  */.  p++;. 
b71e0 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20   b = b<<14;.  b 
b71f0 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70  |= *p;.  /* b: p
b7200 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61  1<<14 | p3 (unma
b7210 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b7220 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (b&0x80)).  {.  
b7230 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77    /* Values betw
b7240 65 65 6e 20 32 30 39 37 31 35 32 20 61 6e 64 20  een 2097152 and 
b7250 32 36 38 34 33 35 34 35 35 20 2a 2f 0a 20 20 20  268435455 */.   
b7260 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   b &= (0x7f<<14)
b7270 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 26  |(0x7f);.    a &
b7280 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
b7290 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c  7f);.    a = a<<
b72a0 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20  7;.    *v = a | 
b72b0 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b  b;.    return 4;
b72c0 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61  .  }..  p++;.  a
b72d0 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d   = a<<14;.  a |=
b72e0 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c   *p;.  /* a: p0<
b72f0 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70  <28 | p2<<14 | p
b7300 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  4 (unmasked) */.
b7310 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29    if (!(a&0x80))
b7320 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 57 61 6c 75  .  {.    /* Walu
b7330 65 73 20 20 62 65 74 77 65 65 6e 20 32 36 38 34  es  between 2684
b7340 33 35 34 35 36 20 61 6e 64 20 33 34 33 35 39 37  35456 and 343597
b7350 33 38 33 36 37 20 2a 2f 0a 20 20 20 20 61 20 26  38367 */.    a &
b7360 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78  = (0x1f<<28)|(0x
b7370 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b7380 20 20 20 20 62 20 26 3d 20 28 30 78 31 66 3c 3c      b &= (0x1f<<
b7390 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28  28)|(0x7f<<14)|(
b73a0 30 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 62  0x7f);.    b = b
b73b0 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20  <<7;.    *v = a 
b73c0 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  | b;.    return 
b73d0 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20  5;.  }..  /* We 
b73e0 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63 68 20 74  can only reach t
b73f0 68 69 73 20 70 6f 69 6e 74 20 77 68 65 6e 20 72  his point when r
b7400 65 61 64 69 6e 67 20 61 20 63 6f 72 72 75 70 74  eading a corrupt
b7410 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66   database.  ** f
b7420 69 6c 65 2e 20 20 49 6e 20 74 68 61 74 20 63 61  ile.  In that ca
b7430 73 65 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e  se we are not in
b7440 20 61 6e 79 20 68 75 72 72 79 2e 20 20 55 73 65   any hurry.  Use
b7450 20 74 68 65 20 28 72 65 6c 61 74 69 76 65 6c 79   the (relatively
b7460 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 67 65 6e 65  .  ** slow) gene
b7470 72 61 6c 2d 70 75 72 70 6f 73 65 20 73 71 6c 69  ral-purpose sqli
b7480 74 65 33 47 65 74 56 61 72 69 6e 74 28 29 20 72  te3GetVarint() r
b7490 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61 63  outine to extrac
b74a0 74 20 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75 65  t the.  ** value
b74b0 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 36 34  . */.  {.    u64
b74c0 20 76 36 34 3b 0a 20 20 20 20 75 38 20 6e 3b 0a   v64;.    u8 n;.
b74d0 0a 20 20 20 20 70 20 2d 3d 20 34 3b 0a 20 20 20  .    p -= 4;.   
b74e0 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   n = sqlite3GetV
b74f0 61 72 69 6e 74 28 70 2c 20 26 76 36 34 29 3b 0a  arint(p, &v64);.
b7500 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 35 20      assert( n>5 
b7510 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 20 20 2a  && n<=9 );.    *
b7520 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a 20 20  v = (u32)v64;.  
b7530 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d 0a    return n;.  }.
b7540 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
b7550 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
b7560 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20  r of bytes that 
b7570 77 69 6c 6c 20 62 65 20 6e 65 65 64 65 64 20 74  will be needed t
b7580 6f 20 73 74 6f 72 65 20 74 68 65 20 67 69 76 65  o store the give
b7590 6e 0a 2a 2a 20 36 34 2d 62 69 74 20 69 6e 74 65  n.** 64-bit inte
b75a0 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ger..*/.SQLITE_P
b75b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b75c0 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20  e3VarintLen(u64 
b75d0 76 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b  v){.  int i = 0;
b75e0 0a 20 20 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b 0a  .  do{.    i++;.
b75f0 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d      v >>= 7;.  }
b7600 77 68 69 6c 65 28 20 76 21 3d 30 20 26 26 20 41  while( v!=0 && A
b7610 4c 57 41 59 53 28 69 3c 39 29 20 29 3b 0a 20 20  LWAYS(i<9) );.  
b7620 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 0a 2f 2a  return i;.}.../*
b7630 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 69 74  .** Read or writ
b7640 65 20 61 20 66 6f 75 72 2d 62 79 74 65 20 62 69  e a four-byte bi
b7650 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72  g-endian integer
b7660 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54   value..*/.SQLIT
b7670 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71  E_PRIVATE u32 sq
b7680 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63 6f  lite3Get4byte(co
b7690 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20 20 72 65  nst u8 *p){.  re
b76a0 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32 34 29 20  turn (p[0]<<24) 
b76b0 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20 7c 20 28  | (p[1]<<16) | (
b76c0 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b 33 5d 3b  p[2]<<8) | p[3];
b76d0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
b76e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 75  E void sqlite3Pu
b76f0 74 34 62 79 74 65 28 75 6e 73 69 67 6e 65 64 20  t4byte(unsigned 
b7700 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 7b  char *p, u32 v){
b7710 0a 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 76  .  p[0] = (u8)(v
b7720 3e 3e 32 34 29 3b 0a 20 20 70 5b 31 5d 20 3d 20  >>24);.  p[1] = 
b7730 28 75 38 29 28 76 3e 3e 31 36 29 3b 0a 20 20 70  (u8)(v>>16);.  p
b7740 5b 32 5d 20 3d 20 28 75 38 29 28 76 3e 3e 38 29  [2] = (u8)(v>>8)
b7750 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 75 38 29 76  ;.  p[3] = (u8)v
b7760 3b 0a 7d 0a 0a 0a 0a 23 69 66 20 21 64 65 66 69  ;.}....#if !defi
b7770 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
b7780 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c  BLOB_LITERAL) ||
b7790 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
b77a0 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a  HAS_CODEC)./*.**
b77b0 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69 6e   Translate a sin
b77c0 67 6c 65 20 62 79 74 65 20 6f 66 20 48 65 78 20  gle byte of Hex 
b77d0 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e  into an integer.
b77e0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
b77f0 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20 68   only works if h
b7800 20 72 65 61 6c 6c 79 20 69 73 20 61 20 76 61 6c   really is a val
b7810 69 64 20 68 65 78 61 64 65 63 69 6d 61 6c 0a 2a  id hexadecimal.*
b7820 2a 20 63 68 61 72 61 63 74 65 72 3a 20 20 30 2e  * character:  0.
b7830 2e 39 61 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 73 74  .9a..fA..F.*/.st
b7840 61 74 69 63 20 75 38 20 68 65 78 54 6f 49 6e 74  atic u8 hexToInt
b7850 28 69 6e 74 20 68 29 7b 0a 20 20 61 73 73 65 72  (int h){.  asser
b7860 74 28 20 28 68 3e 3d 27 30 27 20 26 26 20 68 3c  t( (h>='0' && h<
b7870 3d 27 39 27 29 20 7c 7c 20 20 28 68 3e 3d 27 61  ='9') ||  (h>='a
b7880 27 20 26 26 20 68 3c 3d 27 66 27 29 20 7c 7c 20  ' && h<='f') || 
b7890 20 28 68 3e 3d 27 41 27 20 26 26 20 68 3c 3d 27   (h>='A' && h<='
b78a0 46 27 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51  F') );.#ifdef SQ
b78b0 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 68 20 2b  LITE_ASCII.  h +
b78c0 3d 20 39 2a 28 31 26 28 68 3e 3e 36 29 29 3b 0a  = 9*(1&(h>>6));.
b78d0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
b78e0 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 68 20  LITE_EBCDIC.  h 
b78f0 2b 3d 20 39 2a 28 31 26 7e 28 68 3e 3e 34 29 29  += 9*(1&~(h>>4))
b7900 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  ;.#endif.  retur
b7910 6e 20 28 75 38 29 28 68 20 26 20 30 78 66 29 3b  n (u8)(h & 0xf);
b7920 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51  .}.#endif /* !SQ
b7930 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c  LITE_OMIT_BLOB_L
b7940 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45  ITERAL || SQLITE
b7950 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 23  _HAS_CODEC */..#
b7960 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
b7970 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
b7980 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64  ERAL) || defined
b7990 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45  (SQLITE_HAS_CODE
b79a0 43 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  C)./*.** Convert
b79b0 20 61 20 42 4c 4f 42 20 6c 69 74 65 72 61 6c 20   a BLOB literal 
b79c0 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 27 68  of the form "x'h
b79d0 68 68 68 68 68 27 22 20 69 6e 74 6f 20 69 74 73  hhhhh'" into its
b79e0 20 62 69 6e 61 72 79 0a 2a 2a 20 76 61 6c 75 65   binary.** value
b79f0 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
b7a00 74 65 72 20 74 6f 20 69 74 73 20 62 69 6e 61 72  ter to its binar
b7a10 79 20 76 61 6c 75 65 2e 20 20 53 70 61 63 65 20  y value.  Space 
b7a20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 62  to hold the.** b
b7a30 69 6e 61 72 79 20 76 61 6c 75 65 20 68 61 73 20  inary value has 
b7a40 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72  been obtained fr
b7a50 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75  om malloc and mu
b7a60 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a  st be freed by.*
b7a70 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f  * the calling ro
b7a80 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  utine..*/.SQLITE
b7a90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
b7aa0 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28  qlite3HexToBlob(
b7ab0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
b7ac0 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
b7ad0 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 42 6c 6f  n){.  char *zBlo
b7ae0 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 7a  b;.  int i;..  z
b7af0 42 6c 6f 62 20 3d 20 28 63 68 61 72 20 2a 29 73  Blob = (char *)s
b7b00 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
b7b10 77 28 64 62 2c 20 6e 2f 32 20 2b 20 31 29 3b 0a  w(db, n/2 + 1);.
b7b20 20 20 6e 2d 2d 3b 0a 20 20 69 66 28 20 7a 42 6c    n--;.  if( zBl
b7b30 6f 62 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  ob ){.    for(i=
b7b40 30 3b 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a 20  0; i<n; i+=2){. 
b7b50 20 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20       zBlob[i/2] 
b7b60 3d 20 28 68 65 78 54 6f 49 6e 74 28 7a 5b 69 5d  = (hexToInt(z[i]
b7b70 29 3c 3c 34 29 20 7c 20 68 65 78 54 6f 49 6e 74  )<<4) | hexToInt
b7b80 28 7a 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a  (z[i+1]);.    }.
b7b90 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d      zBlob[i/2] =
b7ba0 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
b7bb0 20 7a 42 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69 66   zBlob;.}.#endif
b7bc0 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   /* !SQLITE_OMIT
b7bd0 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c  _BLOB_LITERAL ||
b7be0 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
b7bf0 43 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  C */.../*.** Cha
b7c00 6e 67 65 20 74 68 65 20 73 71 6c 69 74 65 2e 6d  nge the sqlite.m
b7c10 61 67 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 45  agic from SQLITE
b7c20 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 74 6f 20 53  _MAGIC_OPEN to S
b7c30 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
b7c40 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65  ..** Return an e
b7c50 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20  rror (non-zero) 
b7c60 69 66 20 74 68 65 20 6d 61 67 69 63 20 77 61 73  if the magic was
b7c70 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 49   not SQLITE_MAGI
b7c80 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74  C_OPEN.** when t
b7c90 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
b7ca0 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  alled..**.** Thi
b7cb0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
b7cc0 6c 65 64 20 77 68 65 6e 20 65 6e 74 65 72 69 6e  led when enterin
b7cd0 67 20 61 6e 20 53 51 4c 69 74 65 20 41 50 49 2e  g an SQLite API.
b7ce0 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 41 47    The SQLITE_MAG
b7cf0 49 43 5f 4f 50 45 4e 0a 2a 2a 20 76 61 6c 75 65  IC_OPEN.** value
b7d00 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
b7d10 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
b7d20 6e 65 63 74 69 6f 6e 20 70 61 73 73 65 64 20 69  nection passed i
b7d30 6e 74 6f 20 74 68 65 20 41 50 49 20 69 73 0a 2a  nto the API is.*
b7d40 2a 20 6f 70 65 6e 20 61 6e 64 20 69 73 20 6e 6f  * open and is no
b7d50 74 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 20  t being used by 
b7d60 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 2e 20  another thread. 
b7d70 20 42 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65   By changing the
b7d80 20 76 61 6c 75 65 0a 2a 2a 20 74 6f 20 53 51 4c   value.** to SQL
b7d90 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 77  ITE_MAGIC_BUSY w
b7da0 65 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  e indicate that 
b7db0 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  the connection i
b7dc0 73 20 69 6e 20 75 73 65 2e 0a 2a 2a 20 73 71 6c  s in use..** sql
b7dd0 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 29 20  ite3SafetyOff() 
b7de0 62 65 6c 6f 77 20 77 69 6c 6c 20 63 68 61 6e 67  below will chang
b7df0 65 20 74 68 65 20 76 61 6c 75 65 20 62 61 63 6b  e the value back
b7e00 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43   to SQLITE_MAGIC
b7e10 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68  _OPEN.** when th
b7e20 65 20 41 50 49 20 65 78 69 74 73 2e 20 0a 2a 2a  e API exits. .**
b7e30 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
b7e40 20 69 73 20 61 20 61 74 74 65 6d 70 74 20 74 6f   is a attempt to
b7e50 20 64 65 74 65 63 74 20 69 66 20 74 77 6f 20 74   detect if two t
b7e60 68 72 65 61 64 73 20 75 73 65 20 74 68 65 0a 2a  hreads use the.*
b7e70 2a 20 73 61 6d 65 20 73 71 6c 69 74 65 2a 20 70  * same sqlite* p
b7e80 6f 69 6e 74 65 72 20 61 74 20 74 68 65 20 73 61  ointer at the sa
b7e90 6d 65 20 74 69 6d 65 2e 20 20 54 68 65 72 65 20  me time.  There 
b7ea0 69 73 20 61 20 72 61 63 65 20 0a 2a 2a 20 63 6f  is a race .** co
b7eb0 6e 64 69 74 69 6f 6e 20 73 6f 20 69 74 20 69 73  ndition so it is
b7ec0 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74   possible that t
b7ed0 68 65 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20  he error is not 
b7ee0 64 65 74 65 63 74 65 64 2e 0a 2a 2a 20 42 75 74  detected..** But
b7ef0 20 75 73 75 61 6c 6c 79 20 74 68 65 20 70 72 6f   usually the pro
b7f00 62 6c 65 6d 20 77 69 6c 6c 20 62 65 20 73 65 65  blem will be see
b7f10 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 77  n.  The result w
b7f20 69 6c 6c 20 62 65 20 61 6e 0a 2a 2a 20 65 72 72  ill be an.** err
b7f30 6f 72 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  or which can be 
b7f40 75 73 65 64 20 74 6f 20 64 65 62 75 67 20 74 68  used to debug th
b7f50 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68  e application th
b7f60 61 74 20 69 73 0a 2a 2a 20 75 73 69 6e 67 20 53  at is.** using S
b7f70 51 4c 69 74 65 20 69 6e 63 6f 72 72 65 63 74 6c  QLite incorrectl
b7f80 79 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20  y..**.** Ticket 
b7f90 23 32 30 32 3a 20 20 49 66 20 64 62 2d 3e 6d 61  #202:  If db->ma
b7fa0 67 69 63 20 69 73 20 6e 6f 74 20 61 20 76 61 6c  gic is not a val
b7fb0 69 64 20 6f 70 65 6e 20 76 61 6c 75 65 2c 20 74  id open value, t
b7fc0 61 6b 65 20 63 61 72 65 20 6e 6f 74 0a 2a 2a 20  ake care not.** 
b7fd0 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 62  to modify the db
b7fe0 20 73 74 72 75 63 74 75 72 65 20 61 74 20 61 6c   structure at al
b7ff0 6c 2e 20 20 49 74 20 63 6f 75 6c 64 20 62 65 20  l.  It could be 
b8000 74 68 61 74 20 64 62 20 69 73 20 61 20 73 74 61  that db is a sta
b8010 6c 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20  le.** pointer.  
b8020 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
b8030 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74  it could be that
b8040 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e 20   there has been 
b8050 61 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20  a prior.** call 
b8060 74 6f 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  to sqlite3_close
b8070 28 64 62 29 20 61 6e 64 20 64 62 20 68 61 73 20  (db) and db has 
b8080 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64  been deallocated
b8090 2e 20 20 41 6e 64 20 77 65 20 64 6f 0a 2a 2a 20  .  And we do.** 
b80a0 6e 6f 74 20 77 61 6e 74 20 74 6f 20 77 72 69 74  not want to writ
b80b0 65 20 69 6e 74 6f 20 64 65 61 6c 6c 6f 63 61 74  e into deallocat
b80c0 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 23 69  ed memory..*/.#i
b80d0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
b80e0 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  G.SQLITE_PRIVATE
b80f0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65   int sqlite3Safe
b8100 74 79 4f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62  tyOn(sqlite3 *db
b8110 29 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67  ){.  if( db->mag
b8120 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic==SQLITE_MAGIC
b8130 5f 4f 50 45 4e 20 29 7b 0a 20 20 20 20 64 62 2d  _OPEN ){.    db-
b8140 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
b8150 4d 41 47 49 43 5f 42 55 53 59 3b 0a 20 20 20 20  MAGIC_BUSY;.    
b8160 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
b8170 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
b8180 75 74 65 78 29 20 29 3b 0a 20 20 20 20 72 65 74  utex) );.    ret
b8190 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  urn 0;.  }else i
b81a0 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  f( db->magic==SQ
b81b0 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
b81c0 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63  ){.    db->magic
b81d0 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f   = SQLITE_MAGIC_
b81e0 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75  ERROR;.    db->u
b81f0 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
b8200 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 1;.  }.  retur
b8210 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  n 1;.}.#endif../
b8220 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
b8230 6d 61 67 69 63 20 66 72 6f 6d 20 53 51 4c 49 54  magic from SQLIT
b8240 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 74 6f 20  E_MAGIC_BUSY to 
b8250 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
b8260 4e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20  N..** Return an 
b8270 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29  error (non-zero)
b8280 20 69 66 20 74 68 65 20 6d 61 67 69 63 20 77 61   if the magic wa
b8290 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47  s not SQLITE_MAG
b82a0 49 43 5f 42 55 53 59 0a 2a 2a 20 77 68 65 6e 20  IC_BUSY.** when 
b82b0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
b82c0 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 64 65  called..*/.#ifde
b82d0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53  f SQLITE_DEBUG.S
b82e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b82f0 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  t sqlite3SafetyO
b8300 66 66 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ff(sqlite3 *db){
b8310 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63  .  if( db->magic
b8320 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  ==SQLITE_MAGIC_B
b8330 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  USY ){.    db->m
b8340 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
b8350 47 49 43 5f 4f 50 45 4e 3b 0a 20 20 20 20 61 73  GIC_OPEN;.    as
b8360 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
b8370 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
b8380 65 78 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72  ex) );.    retur
b8390 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  n 0;.  }else{.  
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 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
b83f0 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  }.}.#endif../*.*
b8400 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  * Check to make 
b8410 73 75 72 65 20 77 65 20 68 61 76 65 20 61 20 76  sure we have a v
b8420 61 6c 69 64 20 64 62 20 70 6f 69 6e 74 65 72 2e  alid db pointer.
b8430 20 20 54 68 69 73 20 74 65 73 74 20 69 73 20 6e    This test is n
b8440 6f 74 0a 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66 20  ot.** foolproof 
b8450 62 75 74 20 69 74 20 64 6f 65 73 20 70 72 6f 76  but it does prov
b8460 69 64 65 20 73 6f 6d 65 20 6d 65 61 73 75 72 65  ide some measure
b8470 20 6f 66 20 70 72 6f 74 65 63 74 69 6f 6e 20 61   of protection a
b8480 67 61 69 6e 73 74 0a 2a 2a 20 6d 69 73 75 73 65  gainst.** misuse
b8490 20 6f 66 20 74 68 65 20 69 6e 74 65 72 66 61 63   of the interfac
b84a0 65 20 73 75 63 68 20 61 73 20 70 61 73 73 69 6e  e such as passin
b84b0 67 20 69 6e 20 64 62 20 70 6f 69 6e 74 65 72 73  g in db pointers
b84c0 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 4e 55 4c   that are.** NUL
b84d0 4c 20 6f 72 20 77 68 69 63 68 20 68 61 76 65 20  L or which have 
b84e0 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20  been previously 
b84f0 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73  closed.  If this
b8500 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
b8510 0a 2a 2a 20 31 20 69 74 20 6d 65 61 6e 73 20 74  .** 1 it means t
b8520 68 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74  hat the db point
b8530 65 72 20 69 73 20 76 61 6c 69 64 20 61 6e 64 20  er is valid and 
b8540 30 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6e  0 if it should n
b8550 6f 74 20 62 65 0a 2a 2a 20 64 65 72 65 66 65 72  ot be.** derefer
b8560 65 6e 63 65 64 20 66 6f 72 20 61 6e 79 20 72 65  enced for any re
b8570 61 73 6f 6e 2e 20 20 54 68 65 20 63 61 6c 6c 69  ason.  The calli
b8580 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  ng function shou
b8590 6c 64 20 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51 4c  ld invoke.** SQL
b85a0 49 54 45 5f 4d 49 53 55 53 45 20 69 6d 6d 65 64  ITE_MISUSE immed
b85b0 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73 71  iately..**.** sq
b85c0 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
b85d0 4f 6b 28 29 20 72 65 71 75 69 72 65 73 20 74 68  Ok() requires th
b85e0 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65  at the db pointe
b85f0 72 20 62 65 20 76 61 6c 69 64 20 66 6f 72 0a 2a  r be valid for.*
b8600 2a 20 75 73 65 2e 20 20 73 71 6c 69 74 65 33 53  * use.  sqlite3S
b8610 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72  afetyCheckSickOr
b8620 4f 6b 28 29 20 61 6c 6c 6f 77 73 20 61 20 64 62  Ok() allows a db
b8630 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 66 61   pointer that fa
b8640 69 6c 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20  iled to.** open 
b8650 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 69 73 20  properly and is 
b8660 6e 6f 74 20 66 69 74 20 66 6f 72 20 67 65 6e 65  not fit for gene
b8670 72 61 6c 20 75 73 65 20 62 75 74 20 77 68 69 63  ral use but whic
b8680 68 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64  h can be.** used
b8690 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
b86a0 74 6f 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  to sqlite3_errms
b86b0 67 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63  g() or sqlite3_c
b86c0 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  lose()..*/.SQLIT
b86d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b86e0 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
b86f0 4f 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  Ok(sqlite3 *db){
b8700 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 0a 20 20  .  u32 magic;.  
b8710 69 66 28 20 64 62 3d 3d 30 20 29 20 72 65 74 75  if( db==0 ) retu
b8720 72 6e 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d 20  rn 0;.  magic = 
b8730 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28  db->magic;.  if(
b8740 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d   magic!=SQLITE_M
b8750 41 47 49 43 5f 4f 50 45 4e 20 0a 23 69 66 64 65  AGIC_OPEN .#ifde
b8760 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
b8770 20 20 20 20 26 26 20 6d 61 67 69 63 21 3d 53 51      && magic!=SQ
b8780 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a  LITE_MAGIC_BUSY.
b8790 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 20  #endif.  ){.    
b87a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73  return 0;.  }els
b87b0 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  e{.    return 1;
b87c0 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  .  }.}.SQLITE_PR
b87d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b87e0 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
b87f0 4f 72 4f 6b 28 73 71 6c 69 74 65 33 20 2a 64 62  OrOk(sqlite3 *db
b8800 29 7b 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 0a  ){.  u32 magic;.
b8810 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61    magic = db->ma
b8820 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63  gic;.  if( magic
b8830 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53  !=SQLITE_MAGIC_S
b8840 49 43 4b 20 26 26 0a 20 20 20 20 20 20 6d 61 67  ICK &&.      mag
b8850 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic!=SQLITE_MAGIC
b8860 5f 4f 50 45 4e 20 26 26 0a 20 20 20 20 20 20 6d  _OPEN &&.      m
b8870 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47  agic!=SQLITE_MAG
b8880 49 43 5f 42 55 53 59 20 29 20 72 65 74 75 72 6e  IC_BUSY ) return
b8890 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a   0;.  return 1;.
b88a0 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
b88b0 2a 2a 20 45 6e 64 20 6f 66 20 75 74 69 6c 2e 63  ** End of util.c
b88c0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
b88d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b88e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b88f0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
b8900 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61  ** Begin file ha
b8910 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.c ***********
b8920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8940 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
b8950 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a  ptember 22.**.**
b8960 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
b8970 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
b8980 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
b8990 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
b89a0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
b89b0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
b89c0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
b89d0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
b89e0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
b89f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
b8a00 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
b8a10 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
b8a20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
b8a30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
b8a40 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
b8a50 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
b8a60 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
b8a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 0a 2a 2a 20 54 68 69  *********.** Thi
b8ac0 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  s is the impleme
b8ad0 6e 74 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72  ntation of gener
b8ae0 69 63 20 68 61 73 68 2d 74 61 62 6c 65 73 0a 2a  ic hash-tables.*
b8af0 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65  * used in SQLite
b8b00 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73  ..**.** $Id: has
b8b10 68 2e 63 2c 76 20 31 2e 33 38 20 32 30 30 39 2f  h.c,v 1.38 2009/
b8b20 30 35 2f 30 39 20 32 33 3a 32 39 3a 31 32 20 64  05/09 23:29:12 d
b8b30 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20  rh Exp $.*/../* 
b8b40 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79  Turn bulk memory
b8b50 20 69 6e 74 6f 20 61 20 68 61 73 68 20 74 61 62   into a hash tab
b8b60 6c 65 20 6f 62 6a 65 63 74 20 62 79 20 69 6e 69  le object by ini
b8b70 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a  tializing the.**
b8b80 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 48   fields of the H
b8b90 61 73 68 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ash structure..*
b8ba0 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 69 73 20 61  *.** "pNew" is a
b8bb0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
b8bc0 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20  hash table that 
b8bd0 69 73 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c  is to be initial
b8be0 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ized..*/.SQLITE_
b8bf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
b8c00 69 74 65 33 48 61 73 68 49 6e 69 74 28 48 61 73  ite3HashInit(Has
b8c10 68 20 2a 70 4e 65 77 29 7b 0a 20 20 61 73 73 65  h *pNew){.  asse
b8c20 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20  rt( pNew!=0 );. 
b8c30 20 70 4e 65 77 2d 3e 66 69 72 73 74 20 3d 20 30   pNew->first = 0
b8c40 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 20  ;.  pNew->count 
b8c50 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 73  = 0;.  pNew->hts
b8c60 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  ize = 0;.  pNew-
b8c70 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 52  >ht = 0;.}../* R
b8c80 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 72 69 65  emove all entrie
b8c90 73 20 66 72 6f 6d 20 61 20 68 61 73 68 20 74 61  s from a hash ta
b8ca0 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c  ble.  Reclaim al
b8cb0 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61 6c  l memory..** Cal
b8cc0 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74  l this routine t
b8cd0 6f 20 64 65 6c 65 74 65 20 61 20 68 61 73 68 20  o delete a hash 
b8ce0 74 61 62 6c 65 20 6f 72 20 74 6f 20 72 65 73 65  table or to rese
b8cf0 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a  t a hash table.*
b8d00 2a 20 74 6f 20 74 68 65 20 65 6d 70 74 79 20 73  * to the empty s
b8d10 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tate..*/.SQLITE_
b8d20 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
b8d30 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48 61  ite3HashClear(Ha
b8d40 73 68 20 2a 70 48 29 7b 0a 20 20 48 61 73 68 45  sh *pH){.  HashE
b8d50 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20  lem *elem;      
b8d60 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
b8d70 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65  g over all eleme
b8d80 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  nts of the table
b8d90 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
b8da0 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d  H!=0 );.  elem =
b8db0 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70 48   pH->first;.  pH
b8dc0 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 73  ->first = 0;.  s
b8dd0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d 3e  qlite3_free(pH->
b8de0 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20  ht);.  pH->ht = 
b8df0 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20  0;.  pH->htsize 
b8e00 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65 6c  = 0;.  while( el
b8e10 65 6d 20 29 7b 0a 20 20 20 20 48 61 73 68 45 6c  em ){.    HashEl
b8e20 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d 20  em *next_elem = 
b8e30 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20  elem->next;.    
b8e40 73 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65  sqlite3_free(ele
b8e50 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e  m);.    elem = n
b8e60 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20  ext_elem;.  }.  
b8e70 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d  pH->count = 0;.}
b8e80 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 61 73 68  ../*.** The hash
b8e90 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  ing function..*/
b8ea0 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64  .static unsigned
b8eb0 20 69 6e 74 20 73 74 72 48 61 73 68 28 63 6f 6e   int strHash(con
b8ec0 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
b8ed0 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 20 3d  nKey){.  int h =
b8ee0 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4b   0;.  assert( nK
b8ef0 65 79 3e 3d 30 20 29 3b 0a 20 20 77 68 69 6c 65  ey>=0 );.  while
b8f00 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a 20  ( nKey > 0  ){. 
b8f10 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20     h = (h<<3) ^ 
b8f20 68 20 5e 20 73 71 6c 69 74 65 33 55 70 70 65 72  h ^ sqlite3Upper
b8f30 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65  ToLower[(unsigne
b8f40 64 20 63 68 61 72 29 2a 7a 2b 2b 5d 3b 0a 20 20  d char)*z++];.  
b8f50 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20    nKey--;.  }.  
b8f60 72 65 74 75 72 6e 20 68 3b 0a 7d 0a 0a 0a 2f 2a  return h;.}.../*
b8f70 20 4c 69 6e 6b 20 70 4e 65 77 20 65 6c 65 6d 65   Link pNew eleme
b8f80 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68  nt into the hash
b8f90 20 74 61 62 6c 65 20 70 48 2e 20 20 49 66 20 70   table pH.  If p
b8fa0 45 6e 74 72 79 21 3d 30 20 74 68 65 6e 20 61 6c  Entry!=0 then al
b8fb0 73 6f 0a 2a 2a 20 69 6e 73 65 72 74 20 70 4e 65  so.** insert pNe
b8fc0 77 20 69 6e 74 6f 20 74 68 65 20 70 45 6e 74 72  w into the pEntr
b8fd0 79 20 68 61 73 68 20 62 75 63 6b 65 74 2e 0a 2a  y hash bucket..*
b8fe0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
b8ff0 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20 20 48  sertElement(.  H
b9000 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20  ash *pH,        
b9010 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d        /* The com
b9020 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c 65  plete hash table
b9030 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 74   */.  struct _ht
b9040 20 2a 70 45 6e 74 72 79 2c 20 20 20 20 2f 2a 20   *pEntry,    /* 
b9050 54 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 77  The entry into w
b9060 68 69 63 68 20 70 4e 65 77 20 69 73 20 69 6e 73  hich pNew is ins
b9070 65 72 74 65 64 20 2a 2f 0a 20 20 48 61 73 68 45  erted */.  HashE
b9080 6c 65 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 20  lem *pNew       
b9090 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74    /* The element
b90a0 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20   to be inserted 
b90b0 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  */.){.  HashElem
b90c0 20 2a 70 48 65 61 64 3b 20 20 20 20 20 20 20 2f   *pHead;       /
b90d0 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20  * First element 
b90e0 61 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74 72  already in pEntr
b90f0 79 20 2a 2f 0a 20 20 69 66 28 20 70 45 6e 74 72  y */.  if( pEntr
b9100 79 20 29 7b 0a 20 20 20 20 70 48 65 61 64 20 3d  y ){.    pHead =
b9110 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 20 3f   pEntry->count ?
b9120 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3a   pEntry->chain :
b9130 20 30 3b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e   0;.    pEntry->
b9140 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20 20 70 45 6e  count++;.    pEn
b9150 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70 4e 65  try->chain = pNe
b9160 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  w;.  }else{.    
b9170 70 48 65 61 64 20 3d 20 30 3b 0a 20 20 7d 0a 20  pHead = 0;.  }. 
b9180 20 69 66 28 20 70 48 65 61 64 20 29 7b 0a 20 20   if( pHead ){.  
b9190 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70    pNew->next = p
b91a0 48 65 61 64 3b 0a 20 20 20 20 70 4e 65 77 2d 3e  Head;.    pNew->
b91b0 70 72 65 76 20 3d 20 70 48 65 61 64 2d 3e 70 72  prev = pHead->pr
b91c0 65 76 3b 0a 20 20 20 20 69 66 28 20 70 48 65 61  ev;.    if( pHea
b91d0 64 2d 3e 70 72 65 76 20 29 7b 20 70 48 65 61 64  d->prev ){ pHead
b91e0 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 70  ->prev->next = p
b91f0 4e 65 77 3b 20 7d 0a 20 20 20 20 65 6c 73 65 20  New; }.    else 
b9200 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 70 48              { pH
b9210 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 20  ->first = pNew; 
b9220 7d 0a 20 20 20 20 70 48 65 61 64 2d 3e 70 72 65  }.    pHead->pre
b9230 76 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73  v = pNew;.  }els
b9240 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78  e{.    pNew->nex
b9250 74 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20  t = pH->first;. 
b9260 20 20 20 69 66 28 20 70 48 2d 3e 66 69 72 73 74     if( pH->first
b9270 20 29 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e 70   ){ pH->first->p
b9280 72 65 76 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20  rev = pNew; }.  
b9290 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 30    pNew->prev = 0
b92a0 3b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20  ;.    pH->first 
b92b0 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a 0a  = pNew;.  }.}...
b92c0 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68 61  /* Resize the ha
b92d0 73 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74  sh table so that
b92e0 20 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e 65   it cantains "ne
b92f0 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73 2e  w_size" buckets.
b9300 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 73 68 20  .**.** The hash 
b9310 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61 69 6c  table might fail
b9320 20 74 6f 20 72 65 73 69 7a 65 20 69 66 20 73 71   to resize if sq
b9330 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 66  lite3_malloc() f
b9340 61 69 6c 73 20 6f 72 0a 2a 2a 20 69 66 20 74 68  ails or.** if th
b9350 65 20 6e 65 77 20 73 69 7a 65 20 69 73 20 74 68  e new size is th
b9360 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 70 72  e same as the pr
b9370 69 6f 72 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74  ior size..** Ret
b9380 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
b9390 72 65 73 69 7a 65 20 6f 63 63 75 72 73 20 61 6e  resize occurs an
b93a0 64 20 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a  d false if not..
b93b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
b93c0 68 61 73 68 28 48 61 73 68 20 2a 70 48 2c 20 75  hash(Hash *pH, u
b93d0 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 65 77 5f  nsigned int new_
b93e0 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 74 20  size){.  struct 
b93f0 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 20  _ht *new_ht;    
b9400 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
b9410 65 77 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f  ew hash table */
b9420 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  .  HashElem *ele
b9430 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20 20  m, *next_elem;  
b9440 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67    /* For looping
b9450 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 65   over existing e
b9460 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a 23 69 66 20  lements */..#if 
b9470 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f  SQLITE_MALLOC_SO
b9480 46 54 5f 4c 49 4d 49 54 3e 30 0a 20 20 69 66 28  FT_LIMIT>0.  if(
b9490 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66   new_size*sizeof
b94a0 28 73 74 72 75 63 74 20 5f 68 74 29 3e 53 51 4c  (struct _ht)>SQL
b94b0 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f  ITE_MALLOC_SOFT_
b94c0 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 6e 65 77  LIMIT ){.    new
b94d0 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f 4d  _size = SQLITE_M
b94e0 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54  ALLOC_SOFT_LIMIT
b94f0 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f  /sizeof(struct _
b9500 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  ht);.  }.  if( n
b9510 65 77 5f 73 69 7a 65 3d 3d 70 48 2d 3e 68 74 73  ew_size==pH->hts
b9520 69 7a 65 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ize ) return 0;.
b9530 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65  #endif..  /* The
b9540 20 69 6e 61 62 69 6c 69 74 79 20 74 6f 20 61 6c   inability to al
b9550 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 6f  locates space fo
b9560 72 20 61 20 6c 61 72 67 65 72 20 68 61 73 68 20  r a larger hash 
b9570 74 61 62 6c 65 20 69 73 0a 20 20 2a 2a 20 61 20  table is.  ** a 
b9580 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 20  performance hit 
b9590 62 75 74 20 69 74 20 69 73 20 6e 6f 74 20 61 20  but it is not a 
b95a0 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 53 6f  fatal error.  So
b95b0 20 6d 61 72 6b 20 74 68 65 0a 20 20 2a 2a 20 61   mark the.  ** a
b95c0 6c 6c 6f 63 61 74 69 6f 6e 20 61 73 20 61 20 62  llocation as a b
b95d0 65 6e 69 67 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71  enign..  */.  sq
b95e0 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
b95f0 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 6e 65 77 5f  Malloc();.  new_
b9600 68 74 20 3d 20 28 73 74 72 75 63 74 20 5f 68 74  ht = (struct _ht
b9610 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   *)sqlite3Malloc
b9620 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f  ( new_size*sizeo
b9630 66 28 73 74 72 75 63 74 20 5f 68 74 29 20 29 3b  f(struct _ht) );
b9640 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e  .  sqlite3EndBen
b9650 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20  ignMalloc();..  
b9660 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20  if( new_ht==0 ) 
b9670 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 71 6c 69  return 0;.  sqli
b9680 74 65 33 5f 66 72 65 65 28 70 48 2d 3e 68 74 29  te3_free(pH->ht)
b9690 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77  ;.  pH->ht = new
b96a0 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a  _ht;.  pH->htsiz
b96b0 65 20 3d 20 6e 65 77 5f 73 69 7a 65 20 3d 20 73  e = new_size = s
b96c0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65  qlite3MallocSize
b96d0 28 6e 65 77 5f 68 74 29 2f 73 69 7a 65 6f 66 28  (new_ht)/sizeof(
b96e0 73 74 72 75 63 74 20 5f 68 74 29 3b 0a 20 20 6d  struct _ht);.  m
b96f0 65 6d 73 65 74 28 6e 65 77 5f 68 74 2c 20 30 2c  emset(new_ht, 0,
b9700 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66   new_size*sizeof
b9710 28 73 74 72 75 63 74 20 5f 68 74 29 29 3b 0a 20  (struct _ht));. 
b9720 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 69   for(elem=pH->fi
b9730 72 73 74 2c 20 70 48 2d 3e 66 69 72 73 74 3d 30  rst, pH->first=0
b9740 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e  ; elem; elem = n
b9750 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20 20 20 75  ext_elem){.    u
b9760 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20  nsigned int h = 
b9770 73 74 72 48 61 73 68 28 65 6c 65 6d 2d 3e 70 4b  strHash(elem->pK
b9780 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20  ey, elem->nKey) 
b9790 25 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20 20 20  % new_size;.    
b97a0 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d  next_elem = elem
b97b0 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69 6e 73 65  ->next;.    inse
b97c0 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 6e  rtElement(pH, &n
b97d0 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b  ew_ht[h], elem);
b97e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
b97f0 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63  .}../* This func
b9800 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e  tion (for intern
b9810 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63  al use only) loc
b9820 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20  ates an element 
b9830 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61  in an.** hash ta
b9840 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 65 73  ble that matches
b9850 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20   the given key. 
b9860 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68   The hash for th
b9870 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c  is key has.** al
b9880 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75  ready been compu
b9890 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65  ted and is passe
b98a0 64 20 61 73 20 74 68 65 20 34 74 68 20 70 61 72  d as the 4th par
b98b0 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  ameter..*/.stati
b98c0 63 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 6e 64  c HashElem *find
b98d0 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68  ElementGivenHash
b98e0 28 0a 20 20 63 6f 6e 73 74 20 48 61 73 68 20 2a  (.  const Hash *
b98f0 70 48 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70  pH,     /* The p
b9900 48 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64  H to be searched
b9910 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
b9920 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54 68 65   *pKey,   /* The
b9930 20 6b 65 79 20 77 65 20 61 72 65 20 73 65 61 72   key we are sear
b9940 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 69  ching for */.  i
b9950 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20  nt nKey,        
b9960 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 6b     /* Bytes in k
b9970 65 79 20 28 6e 6f 74 20 63 6f 75 6e 74 69 6e 67  ey (not counting
b9980 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
b9990 29 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  ) */.  unsigned 
b99a0 69 6e 74 20 68 20 20 20 20 20 20 2f 2a 20 54 68  int h      /* Th
b99b0 65 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20  e hash for this 
b99c0 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 48 61 73  key. */.){.  Has
b99d0 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20  hElem *elem;    
b99e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
b99f0 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75  sed to loop thru
b9a00 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73   the element lis
b9a10 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74  t */.  int count
b9a20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
b9a30 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
b9a40 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74  of elements left
b9a50 20 74 6f 20 74 65 73 74 20 2a 2f 0a 0a 20 20 69   to test */..  i
b9a60 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20  f( pH->ht ){.   
b9a70 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e   struct _ht *pEn
b9a80 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d  try = &pH->ht[h]
b9a90 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 45 6e  ;.    elem = pEn
b9aa0 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 20 20  try->chain;.    
b9ab0 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79 2d 3e  count = pEntry->
b9ac0 63 6f 75 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  count;.  }else{.
b9ad0 20 20 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66      elem = pH->f
b9ae0 69 72 73 74 3b 0a 20 20 20 20 63 6f 75 6e 74 20  irst;.    count 
b9af0 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d  = pH->count;.  }
b9b00 0a 20 20 77 68 69 6c 65 28 20 63 6f 75 6e 74 2d  .  while( count-
b9b10 2d 20 26 26 20 41 4c 57 41 59 53 28 65 6c 65 6d  - && ALWAYS(elem
b9b20 29 20 29 7b 0a 20 20 20 20 69 66 28 20 65 6c 65  ) ){.    if( ele
b9b30 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 26 26  m->nKey==nKey &&
b9b40 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
b9b50 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 70 4b 65 79  (elem->pKey,pKey
b9b60 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0a 20 20  ,nKey)==0 ){ .  
b9b70 20 20 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b      return elem;
b9b80 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 65 6d 20  .    }.    elem 
b9b90 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20  = elem->next;.  
b9ba0 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
b9bb0 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e  ./* Remove a sin
b9bc0 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74  gle entry from t
b9bd0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69  he hash table gi
b9be0 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ven a pointer to
b9bf0 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74   that.** element
b9c00 20 61 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74   and a hash on t
b9c10 68 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79  he element's key
b9c20 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
b9c30 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69   removeElementGi
b9c40 76 65 6e 48 61 73 68 28 0a 20 20 48 61 73 68 20  venHash(.  Hash 
b9c50 2a 70 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  *pH,         /* 
b9c60 54 68 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e  The pH containin
b9c70 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 48 61  g "elem" */.  Ha
b9c80 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 20 20  shElem* elem,   
b9c90 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74  /* The element t
b9ca0 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66 72 6f  o be removed fro
b9cb0 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 75 6e  m the pH */.  un
b9cc0 73 69 67 6e 65 64 20 69 6e 74 20 68 20 20 20 20  signed int h    
b9cd0 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20 66 6f  /* Hash value fo
b9ce0 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 2a 2f  r the element */
b9cf0 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f 68 74  .){.  struct _ht
b9d00 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20   *pEntry;.  if( 
b9d10 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20  elem->prev ){.  
b9d20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65    elem->prev->ne
b9d30 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  xt = elem->next;
b9d40 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70   .  }else{.    p
b9d50 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d  H->first = elem-
b9d60 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28  >next;.  }.  if(
b9d70 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20   elem->next ){. 
b9d80 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70     elem->next->p
b9d90 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76  rev = elem->prev
b9da0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e  ;.  }.  if( pH->
b9db0 68 74 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79  ht ){.    pEntry
b9dc0 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20   = &pH->ht[h];. 
b9dd0 20 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63     if( pEntry->c
b9de0 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20  hain==elem ){.  
b9df0 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69      pEntry->chai
b9e00 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  n = elem->next;.
b9e10 20 20 20 20 7d 0a 20 20 20 20 70 45 6e 74 72 79      }.    pEntry
b9e20 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20 61  ->count--;.    a
b9e30 73 73 65 72 74 28 20 70 45 6e 74 72 79 2d 3e 63  ssert( pEntry->c
b9e40 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20  ount>=0 );.  }. 
b9e50 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 65   sqlite3_free( e
b9e60 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75  lem );.  pH->cou
b9e70 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e  nt--;.  if( pH->
b9e80 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20  count<=0 ){.    
b9e90 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69 72 73  assert( pH->firs
b9ea0 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  t==0 );.    asse
b9eb0 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30  rt( pH->count==0
b9ec0 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48   );.    sqlite3H
b9ed0 61 73 68 43 6c 65 61 72 28 70 48 29 3b 0a 20 20  ashClear(pH);.  
b9ee0 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20  }.}../* Attempt 
b9ef0 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65  to locate an ele
b9f00 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 61 73 68  ment of the hash
b9f10 20 74 61 62 6c 65 20 70 48 20 77 69 74 68 20 61   table pH with a
b9f20 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74   key.** that mat
b9f30 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20  ches pKey,nKey. 
b9f40 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61   Return the data
b9f50 20 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e   for this elemen
b9f60 74 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 66 6f  t if it is.** fo
b9f70 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20  und, or NULL if 
b9f80 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63  there is no matc
b9f90 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  h..*/.SQLITE_PRI
b9fa0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
b9fb0 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74  e3HashFind(const
b9fc0 20 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74   Hash *pH, const
b9fd0 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74   char *pKey, int
b9fe0 20 6e 4b 65 79 29 7b 0a 20 20 48 61 73 68 45 6c   nKey){.  HashEl
b9ff0 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a 20  em *elem;    /* 
ba000 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61 74  The element that
ba010 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f 0a   matches key */.
ba020 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
ba030 3b 20 20 20 20 2f 2a 20 41 20 68 61 73 68 20 6f  ;    /* A hash o
ba040 6e 20 6b 65 79 20 2a 2f 0a 0a 20 20 61 73 73 65  n key */..  asse
ba050 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 61  rt( pH!=0 );.  a
ba060 73 73 65 72 74 28 20 70 4b 65 79 21 3d 30 20 29  ssert( pKey!=0 )
ba070 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4b 65 79  ;.  assert( nKey
ba080 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 48 2d  >=0 );.  if( pH-
ba090 3e 68 74 20 29 7b 0a 20 20 20 20 68 20 3d 20 73  >ht ){.    h = s
ba0a0 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65  trHash(pKey, nKe
ba0b0 79 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b  y) % pH->htsize;
ba0c0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20  .  }else{.    h 
ba0d0 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 65 6d 20  = 0;.  }.  elem 
ba0e0 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76  = findElementGiv
ba0f0 65 6e 48 61 73 68 28 70 48 2c 20 70 4b 65 79 2c  enHash(pH, pKey,
ba100 20 6e 4b 65 79 2c 20 68 29 3b 0a 20 20 72 65 74   nKey, h);.  ret
ba110 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d  urn elem ? elem-
ba120 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a  >data : 0;.}../*
ba130 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65   Insert an eleme
ba140 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68  nt into the hash
ba150 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20   table pH.  The 
ba160 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79  key is pKey,nKey
ba170 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61  .** and the data
ba180 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a   is "data"..**.*
ba190 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20  * If no element 
ba1a0 65 78 69 73 74 73 20 77 69 74 68 20 61 20 6d 61  exists with a ma
ba1b0 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e  tching key, then
ba1c0 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e   a new.** elemen
ba1d0 74 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64  t is created and
ba1e0 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
ba1f0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74  d..**.** If anot
ba200 68 65 72 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65  her element alre
ba210 61 64 79 20 65 78 69 73 74 73 20 77 69 74 68 20  ady exists with 
ba220 74 68 65 20 73 61 6d 65 20 6b 65 79 2c 20 74 68  the same key, th
ba230 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 77 20 64 61  en the.** new da
ba240 74 61 20 72 65 70 6c 61 63 65 73 20 74 68 65 20  ta replaces the 
ba250 6f 6c 64 20 64 61 74 61 20 61 6e 64 20 74 68 65  old data and the
ba260 20 6f 6c 64 20 64 61 74 61 20 69 73 20 72 65 74   old data is ret
ba270 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 6b 65  urned..** The ke
ba280 79 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 64 20  y is not copied 
ba290 69 6e 20 74 68 69 73 20 69 6e 73 74 61 6e 63 65  in this instance
ba2a0 2e 20 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66  .  If a malloc f
ba2b0 61 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  ails, then.** th
ba2c0 65 20 6e 65 77 20 64 61 74 61 20 69 73 20 72 65  e new data is re
ba2d0 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 68  turned and the h
ba2e0 61 73 68 20 74 61 62 6c 65 20 69 73 20 75 6e 63  ash table is unc
ba2f0 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  hanged..**.** If
ba300 20 74 68 65 20 22 64 61 74 61 22 20 70 61 72 61   the "data" para
ba310 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20 66 75  meter to this fu
ba320 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20  nction is NULL, 
ba330 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 6c 65 6d  then the.** elem
ba340 65 6e 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  ent correspondin
ba350 67 20 74 6f 20 22 6b 65 79 22 20 69 73 20 72 65  g to "key" is re
ba360 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 68  moved from the h
ba370 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  ash table..*/.SQ
ba380 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
ba390 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e  d *sqlite3HashIn
ba3a0 73 65 72 74 28 48 61 73 68 20 2a 70 48 2c 20 63  sert(Hash *pH, c
ba3b0 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c  onst char *pKey,
ba3c0 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20   int nKey, void 
ba3d0 2a 64 61 74 61 29 7b 0a 20 20 75 6e 73 69 67 6e  *data){.  unsign
ba3e0 65 64 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20  ed int h;       
ba3f0 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66 20 74  /* the hash of t
ba400 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61  he key modulo ha
ba410 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f  sh table size */
ba420 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  .  HashElem *ele
ba430 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64  m;       /* Used
ba440 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68   to loop thru th
ba450 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a  e element list *
ba460 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65  /.  HashElem *ne
ba470 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65 77  w_elem;   /* New
ba480 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64 20 74   element added t
ba490 6f 20 74 68 65 20 70 48 20 2a 2f 0a 0a 20 20 61  o the pH */..  a
ba4a0 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a  ssert( pH!=0 );.
ba4b0 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 21 3d    assert( pKey!=
ba4c0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
ba4d0 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20  Key>=0 );.  if( 
ba4e0 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20  pH->htsize ){.  
ba4f0 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b    h = strHash(pK
ba500 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e  ey, nKey) % pH->
ba510 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b  htsize;.  }else{
ba520 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a  .    h = 0;.  }.
ba530 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65    elem = findEle
ba540 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48  mentGivenHash(pH
ba550 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20  ,pKey,nKey,h);. 
ba560 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20   if( elem ){.   
ba570 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61 20   void *old_data 
ba580 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a 20 20  = elem->data;.  
ba590 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 7b    if( data==0 ){
ba5a0 0a 20 20 20 20 20 20 72 65 6d 6f 76 65 45 6c 65  .      removeEle
ba5b0 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48  mentGivenHash(pH
ba5c0 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 20 20 7d 65  ,elem,h);.    }e
ba5d0 6c 73 65 7b 0a 20 20 20 20 20 20 65 6c 65 6d 2d  lse{.      elem-
ba5e0 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20  >data = data;.  
ba5f0 20 20 20 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d      elem->pKey =
ba600 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 61 73 73   pKey;.      ass
ba610 65 72 74 28 6e 4b 65 79 3d 3d 65 6c 65 6d 2d 3e  ert(nKey==elem->
ba620 6e 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20  nKey);.    }.   
ba630 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61   return old_data
ba640 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 61 74 61  ;.  }.  if( data
ba650 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
ba660 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 28 48 61    new_elem = (Ha
ba670 73 68 45 6c 65 6d 2a 29 73 71 6c 69 74 65 33 4d  shElem*)sqlite3M
ba680 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 48 61  alloc( sizeof(Ha
ba690 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69 66 28  shElem) );.  if(
ba6a0 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 20 72   new_elem==0 ) r
ba6b0 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 6e 65  eturn data;.  ne
ba6c0 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 70  w_elem->pKey = p
ba6d0 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d  Key;.  new_elem-
ba6e0 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b 0a 20 20  >nKey = nKey;.  
ba6f0 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61 20 3d  new_elem->data =
ba700 20 64 61 74 61 3b 0a 20 20 70 48 2d 3e 63 6f 75   data;.  pH->cou
ba710 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 48 2d 3e  nt++;.  if( pH->
ba720 63 6f 75 6e 74 3e 3d 31 30 20 26 26 20 70 48 2d  count>=10 && pH-
ba730 3e 63 6f 75 6e 74 20 3e 20 32 2a 70 48 2d 3e 68  >count > 2*pH->h
ba740 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 69 66 28  tsize ){.    if(
ba750 20 72 65 68 61 73 68 28 70 48 2c 20 70 48 2d 3e   rehash(pH, pH->
ba760 63 6f 75 6e 74 2a 32 29 20 29 7b 0a 20 20 20 20  count*2) ){.    
ba770 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 68 74    assert( pH->ht
ba780 73 69 7a 65 3e 30 20 29 3b 0a 20 20 20 20 20 20  size>0 );.      
ba790 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65 79  h = strHash(pKey
ba7a0 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 74  , nKey) % pH->ht
ba7b0 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  size;.    }.  }.
ba7c0 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a    if( pH->ht ){.
ba7d0 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e      insertElemen
ba7e0 74 28 70 48 2c 20 26 70 48 2d 3e 68 74 5b 68 5d  t(pH, &pH->ht[h]
ba7f0 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 7d  , new_elem);.  }
ba800 65 6c 73 65 7b 0a 20 20 20 20 69 6e 73 65 72 74  else{.    insert
ba810 45 6c 65 6d 65 6e 74 28 70 48 2c 20 30 2c 20 6e  Element(pH, 0, n
ba820 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20  ew_elem);.  }.  
ba830 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a  return 0;.}../**
ba840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
ba850 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a   of hash.c *****
ba860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
ba890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
ba8a0 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e  in file opcodes.
ba8b0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
ba8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
ba8e0 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  Automatically ge
ba8f0 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74  nerated.  Do not
ba900 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20   edit */./* See 
ba910 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63 2e 61 77  the mkopcodec.aw
ba920 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74  k script for det
ba930 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20 21 64 65  ails. */.#if !de
ba940 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
ba950 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21 64  T_EXPLAIN) || !d
ba960 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c  efined(NDEBUG) |
ba970 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50  | defined(VDBE_P
ba980 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e  ROFILE) || defin
ba990 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
ba9a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ba9b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
ba9c0 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e  te3OpcodeName(in
ba9d0 74 20 69 29 7b 0a 20 73 74 61 74 69 63 20 63 6f  t i){. static co
ba9e0 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20  nst char *const 
ba9f0 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20 22 3f 22  azName[] = { "?"
baa00 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31 20 2a 2f  ,.     /*   1 */
baa10 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 20 2f 2a   "Goto",.     /*
baa20 20 20 20 32 20 2a 2f 20 22 47 6f 73 75 62 22 2c     2 */ "Gosub",
baa30 0a 20 20 20 20 20 2f 2a 20 20 20 33 20 2a 2f 20  .     /*   3 */ 
baa40 22 52 65 74 75 72 6e 22 2c 0a 20 20 20 20 20 2f  "Return",.     /
baa50 2a 20 20 20 34 20 2a 2f 20 22 59 69 65 6c 64 22  *   4 */ "Yield"
baa60 2c 0a 20 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f  ,.     /*   5 */
baa70 20 22 48 61 6c 74 49 66 4e 75 6c 6c 22 2c 0a 20   "HaltIfNull",. 
baa80 20 20 20 20 2f 2a 20 20 20 36 20 2a 2f 20 22 48      /*   6 */ "H
baa90 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20  alt",.     /*   
baaa0 37 20 2a 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a  7 */ "Integer",.
baab0 20 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22       /*   8 */ "
baac0 49 6e 74 36 34 22 2c 0a 20 20 20 20 20 2f 2a 20  Int64",.     /* 
baad0 20 20 39 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c    9 */ "String",
baae0 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20  .     /*  10 */ 
baaf0 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20  "Null",.     /* 
bab00 20 31 31 20 2a 2f 20 22 42 6c 6f 62 22 2c 0a 20   11 */ "Blob",. 
bab10 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 56      /*  12 */ "V
bab20 61 72 69 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f  ariable",.     /
bab30 2a 20 20 31 33 20 2a 2f 20 22 4d 6f 76 65 22 2c  *  13 */ "Move",
bab40 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 2a 2f 20  .     /*  14 */ 
bab50 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20  "Copy",.     /* 
bab60 20 31 35 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a   15 */ "SCopy",.
bab70 20 20 20 20 20 2f 2a 20 20 31 36 20 2a 2f 20 22       /*  16 */ "
bab80 52 65 73 75 6c 74 52 6f 77 22 2c 0a 20 20 20 20  ResultRow",.    
bab90 20 2f 2a 20 20 31 37 20 2a 2f 20 22 43 6f 6c 6c   /*  17 */ "Coll
baba0 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31  Seq",.     /*  1
babb0 38 20 2a 2f 20 22 46 75 6e 63 74 69 6f 6e 22 2c  8 */ "Function",
babc0 0a 20 20 20 20 20 2f 2a 20 20 31 39 20 2a 2f 20  .     /*  19 */ 
babd0 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  "Not",.     /*  
babe0 32 30 20 2a 2f 20 22 41 64 64 49 6d 6d 22 2c 0a  20 */ "AddImm",.
babf0 20 20 20 20 20 2f 2a 20 20 32 31 20 2a 2f 20 22       /*  21 */ "
bac00 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 20 20  MustBeInt",.    
bac10 20 2f 2a 20 20 32 32 20 2a 2f 20 22 52 65 61 6c   /*  22 */ "Real
bac20 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20  Affinity",.     
bac30 2f 2a 20 20 32 33 20 2a 2f 20 22 50 65 72 6d 75  /*  23 */ "Permu
bac40 74 61 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a  tation",.     /*
bac50 20 20 32 34 20 2a 2f 20 22 43 6f 6d 70 61 72 65    24 */ "Compare
bac60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 35 20 2a  ",.     /*  25 *
bac70 2f 20 22 4a 75 6d 70 22 2c 0a 20 20 20 20 20 2f  / "Jump",.     /
bac80 2a 20 20 32 36 20 2a 2f 20 22 49 66 22 2c 0a 20  *  26 */ "If",. 
bac90 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 49      /*  27 */ "I
baca0 66 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  fNot",.     /*  
bacb0 32 38 20 2a 2f 20 22 43 6f 6c 75 6d 6e 22 2c 0a  28 */ "Column",.
bacc0 20 20 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20 22       /*  29 */ "
bacd0 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20  Affinity",.     
bace0 2f 2a 20 20 33 30 20 2a 2f 20 22 4d 61 6b 65 52  /*  30 */ "MakeR
bacf0 65 63 6f 72 64 22 2c 0a 20 20 20 20 20 2f 2a 20  ecord",.     /* 
bad00 20 33 31 20 2a 2f 20 22 43 6f 75 6e 74 22 2c 0a   31 */ "Count",.
bad10 20 20 20 20 20 2f 2a 20 20 33 32 20 2a 2f 20 22       /*  32 */ "
bad20 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20 20 20 20  Savepoint",.    
bad30 20 2f 2a 20 20 33 33 20 2a 2f 20 22 41 75 74 6f   /*  33 */ "Auto
bad40 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a  Commit",.     /*
bad50 20 20 33 34 20 2a 2f 20 22 54 72 61 6e 73 61 63    34 */ "Transac
bad60 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20  tion",.     /*  
bad70 33 35 20 2a 2f 20 22 52 65 61 64 43 6f 6f 6b 69  35 */ "ReadCooki
bad80 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36 20  e",.     /*  36 
bad90 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a  */ "SetCookie",.
bada0 20 20 20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22       /*  37 */ "
badb0 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c 0a 20  VerifyCookie",. 
badc0 20 20 20 20 2f 2a 20 20 33 38 20 2a 2f 20 22 4f      /*  38 */ "O
badd0 70 65 6e 52 65 61 64 22 2c 0a 20 20 20 20 20 2f  penRead",.     /
bade0 2a 20 20 33 39 20 2a 2f 20 22 4f 70 65 6e 57 72  *  39 */ "OpenWr
badf0 69 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  ite",.     /*  4
bae00 30 20 2a 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65  0 */ "OpenEpheme
bae10 72 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  ral",.     /*  4
bae20 31 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64 6f  1 */ "OpenPseudo
bae30 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 32 20 2a  ",.     /*  42 *
bae40 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 20 20 20 20  / "Close",.     
bae50 2f 2a 20 20 34 33 20 2a 2f 20 22 53 65 65 6b 4c  /*  43 */ "SeekL
bae60 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 34 20  t",.     /*  44 
bae70 2a 2f 20 22 53 65 65 6b 4c 65 22 2c 0a 20 20 20  */ "SeekLe",.   
bae80 20 20 2f 2a 20 20 34 35 20 2a 2f 20 22 53 65 65    /*  45 */ "See
bae90 6b 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  kGe",.     /*  4
baea0 36 20 2a 2f 20 22 53 65 65 6b 47 74 22 2c 0a 20  6 */ "SeekGt",. 
baeb0 20 20 20 20 2f 2a 20 20 34 37 20 2a 2f 20 22 53      /*  47 */ "S
baec0 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  eek",.     /*  4
baed0 38 20 2a 2f 20 22 4e 6f 74 46 6f 75 6e 64 22 2c  8 */ "NotFound",
baee0 0a 20 20 20 20 20 2f 2a 20 20 34 39 20 2a 2f 20  .     /*  49 */ 
baef0 22 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a  "Found",.     /*
baf00 20 20 35 30 20 2a 2f 20 22 49 73 55 6e 69 71 75    50 */ "IsUniqu
baf10 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 31 20  e",.     /*  51 
baf20 2a 2f 20 22 4e 6f 74 45 78 69 73 74 73 22 2c 0a  */ "NotExists",.
baf30 20 20 20 20 20 2f 2a 20 20 35 32 20 2a 2f 20 22       /*  52 */ "
baf40 53 65 71 75 65 6e 63 65 22 2c 0a 20 20 20 20 20  Sequence",.     
baf50 2f 2a 20 20 35 33 20 2a 2f 20 22 4e 65 77 52 6f  /*  53 */ "NewRo
baf60 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35  wid",.     /*  5
baf70 34 20 2a 2f 20 22 49 6e 73 65 72 74 22 2c 0a 20  4 */ "Insert",. 
baf80 20 20 20 20 2f 2a 20 20 35 35 20 2a 2f 20 22 49      /*  55 */ "I
baf90 6e 73 65 72 74 49 6e 74 22 2c 0a 20 20 20 20 20  nsertInt",.     
bafa0 2f 2a 20 20 35 36 20 2a 2f 20 22 44 65 6c 65 74  /*  56 */ "Delet
bafb0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20  e",.     /*  57 
bafc0 2a 2f 20 22 52 65 73 65 74 43 6f 75 6e 74 22 2c  */ "ResetCount",
bafd0 0a 20 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f 20  .     /*  58 */ 
bafe0 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f  "RowKey",.     /
baff0 2a 20 20 35 39 20 2a 2f 20 22 52 6f 77 44 61 74  *  59 */ "RowDat
bb000 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 30 20  a",.     /*  60 
bb010 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 20  */ "Rowid",.    
bb020 20 2f 2a 20 20 36 31 20 2a 2f 20 22 4e 75 6c 6c   /*  61 */ "Null
bb030 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  Row",.     /*  6
bb040 32 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20  2 */ "Last",.   
bb050 20 20 2f 2a 20 20 36 33 20 2a 2f 20 22 53 6f 72    /*  63 */ "Sor
bb060 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 34 20  t",.     /*  64 
bb070 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a 20 20 20  */ "Rewind",.   
bb080 20 20 2f 2a 20 20 36 35 20 2a 2f 20 22 50 72 65    /*  65 */ "Pre
bb090 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36 20  v",.     /*  66 
bb0a0 2a 2f 20 22 4e 65 78 74 22 2c 0a 20 20 20 20 20  */ "Next",.     
bb0b0 2f 2a 20 20 36 37 20 2a 2f 20 22 49 64 78 49 6e  /*  67 */ "IdxIn
bb0c0 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  sert",.     /*  
bb0d0 36 38 20 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 20  68 */ "Or",.    
bb0e0 20 2f 2a 20 20 36 39 20 2a 2f 20 22 41 6e 64 22   /*  69 */ "And"
bb0f0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f  ,.     /*  70 */
bb100 20 22 49 64 78 44 65 6c 65 74 65 22 2c 0a 20 20   "IdxDelete",.  
bb110 20 20 20 2f 2a 20 20 37 31 20 2a 2f 20 22 49 64     /*  71 */ "Id
bb120 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a  xRowid",.     /*
bb130 20 20 37 32 20 2a 2f 20 22 49 64 78 4c 54 22 2c    72 */ "IdxLT",
bb140 0a 20 20 20 20 20 2f 2a 20 20 37 33 20 2a 2f 20  .     /*  73 */ 
bb150 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f  "IsNull",.     /
bb160 2a 20 20 37 34 20 2a 2f 20 22 4e 6f 74 4e 75 6c  *  74 */ "NotNul
bb170 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 35 20  l",.     /*  75 
bb180 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 20 20 2f 2a  */ "Ne",.     /*
bb190 20 20 37 36 20 2a 2f 20 22 45 71 22 2c 0a 20 20    76 */ "Eq",.  
bb1a0 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 22 47 74     /*  77 */ "Gt
bb1b0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38 20 2a  ",.     /*  78 *
bb1c0 2f 20 22 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20  / "Le",.     /* 
bb1d0 20 37 39 20 2a 2f 20 22 4c 74 22 2c 0a 20 20 20   79 */ "Lt",.   
bb1e0 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22 47 65 22    /*  80 */ "Ge"
bb1f0 2c 0a 20 20 20 20 20 2f 2a 20 20 38 31 20 2a 2f  ,.     /*  81 */
bb200 20 22 49 64 78 47 45 22 2c 0a 20 20 20 20 20 2f   "IdxGE",.     /
bb210 2a 20 20 38 32 20 2a 2f 20 22 42 69 74 41 6e 64  *  82 */ "BitAnd
bb220 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 2a  ",.     /*  83 *
bb230 2f 20 22 42 69 74 4f 72 22 2c 0a 20 20 20 20 20  / "BitOr",.     
bb240 2f 2a 20 20 38 34 20 2a 2f 20 22 53 68 69 66 74  /*  84 */ "Shift
bb250 4c 65 66 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Left",.     /*  
bb260 38 35 20 2a 2f 20 22 53 68 69 66 74 52 69 67 68  85 */ "ShiftRigh
bb270 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 36 20  t",.     /*  86 
bb280 2a 2f 20 22 41 64 64 22 2c 0a 20 20 20 20 20 2f  */ "Add",.     /
bb290 2a 20 20 38 37 20 2a 2f 20 22 53 75 62 74 72 61  *  87 */ "Subtra
bb2a0 63 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 38  ct",.     /*  88
bb2b0 20 2a 2f 20 22 4d 75 6c 74 69 70 6c 79 22 2c 0a   */ "Multiply",.
bb2c0 20 20 20 20 20 2f 2a 20 20 38 39 20 2a 2f 20 22       /*  89 */ "
bb2d0 44 69 76 69 64 65 22 2c 0a 20 20 20 20 20 2f 2a  Divide",.     /*
bb2e0 20 20 39 30 20 2a 2f 20 22 52 65 6d 61 69 6e 64    90 */ "Remaind
bb2f0 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 31  er",.     /*  91
bb300 20 2a 2f 20 22 43 6f 6e 63 61 74 22 2c 0a 20 20   */ "Concat",.  
bb310 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20 22 44 65     /*  92 */ "De
bb320 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20  stroy",.     /* 
bb330 20 39 33 20 2a 2f 20 22 42 69 74 4e 6f 74 22 2c   93 */ "BitNot",
bb340 0a 20 20 20 20 20 2f 2a 20 20 39 34 20 2a 2f 20  .     /*  94 */ 
bb350 22 53 74 72 69 6e 67 38 22 2c 0a 20 20 20 20 20  "String8",.     
bb360 2f 2a 20 20 39 35 20 2a 2f 20 22 43 6c 65 61 72  /*  95 */ "Clear
bb370 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 36 20 2a  ",.     /*  96 *
bb380 2f 20 22 43 72 65 61 74 65 49 6e 64 65 78 22 2c  / "CreateIndex",
bb390 0a 20 20 20 20 20 2f 2a 20 20 39 37 20 2a 2f 20  .     /*  97 */ 
bb3a0 22 43 72 65 61 74 65 54 61 62 6c 65 22 2c 0a 20  "CreateTable",. 
bb3b0 20 20 20 20 2f 2a 20 20 39 38 20 2a 2f 20 22 50      /*  98 */ "P
bb3c0 61 72 73 65 53 63 68 65 6d 61 22 2c 0a 20 20 20  arseSchema",.   
bb3d0 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22 4c 6f 61    /*  99 */ "Loa
bb3e0 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20  dAnalysis",.    
bb3f0 20 2f 2a 20 31 30 30 20 2a 2f 20 22 44 72 6f 70   /* 100 */ "Drop
bb400 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20  Table",.     /* 
bb410 31 30 31 20 2a 2f 20 22 44 72 6f 70 49 6e 64 65  101 */ "DropInde
bb420 78 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 32 20  x",.     /* 102 
bb430 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65 72 22  */ "DropTrigger"
bb440 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33 20 2a 2f  ,.     /* 103 */
bb450 20 22 49 6e 74 65 67 72 69 74 79 43 6b 22 2c 0a   "IntegrityCk",.
bb460 20 20 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22       /* 104 */ "
bb470 52 6f 77 53 65 74 41 64 64 22 2c 0a 20 20 20 20  RowSetAdd",.    
bb480 20 2f 2a 20 31 30 35 20 2a 2f 20 22 52 6f 77 53   /* 105 */ "RowS
bb490 65 74 52 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a  etRead",.     /*
bb4a0 20 31 30 36 20 2a 2f 20 22 52 6f 77 53 65 74 54   106 */ "RowSetT
bb4b0 65 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  est",.     /* 10
bb4c0 37 20 2a 2f 20 22 50 72 6f 67 72 61 6d 22 2c 0a  7 */ "Program",.
bb4d0 20 20 20 20 20 2f 2a 20 31 30 38 20 2a 2f 20 22       /* 108 */ "
bb4e0 50 61 72 61 6d 22 2c 0a 20 20 20 20 20 2f 2a 20  Param",.     /* 
bb4f0 31 30 39 20 2a 2f 20 22 46 6b 43 6f 75 6e 74 65  109 */ "FkCounte
bb500 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 30 20  r",.     /* 110 
bb510 2a 2f 20 22 46 6b 49 66 5a 65 72 6f 22 2c 0a 20  */ "FkIfZero",. 
bb520 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f 20 22 4d      /* 111 */ "M
bb530 65 6d 4d 61 78 22 2c 0a 20 20 20 20 20 2f 2a 20  emMax",.     /* 
bb540 31 31 32 20 2a 2f 20 22 49 66 50 6f 73 22 2c 0a  112 */ "IfPos",.
bb550 20 20 20 20 20 2f 2a 20 31 31 33 20 2a 2f 20 22       /* 113 */ "
bb560 49 66 4e 65 67 22 2c 0a 20 20 20 20 20 2f 2a 20  IfNeg",.     /* 
bb570 31 31 34 20 2a 2f 20 22 49 66 5a 65 72 6f 22 2c  114 */ "IfZero",
bb580 0a 20 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f 20  .     /* 115 */ 
bb590 22 41 67 67 53 74 65 70 22 2c 0a 20 20 20 20 20  "AggStep",.     
bb5a0 2f 2a 20 31 31 36 20 2a 2f 20 22 41 67 67 46 69  /* 116 */ "AggFi
bb5b0 6e 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  nal",.     /* 11
bb5c0 37 20 2a 2f 20 22 56 61 63 75 75 6d 22 2c 0a 20  7 */ "Vacuum",. 
bb5d0 20 20 20 20 2f 2a 20 31 31 38 20 2a 2f 20 22 49      /* 118 */ "I
bb5e0 6e 63 72 56 61 63 75 75 6d 22 2c 0a 20 20 20 20  ncrVacuum",.    
bb5f0 20 2f 2a 20 31 31 39 20 2a 2f 20 22 45 78 70 69   /* 119 */ "Expi
bb600 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 30  re",.     /* 120
bb610 20 2a 2f 20 22 54 61 62 6c 65 4c 6f 63 6b 22 2c   */ "TableLock",
bb620 0a 20 20 20 20 20 2f 2a 20 31 32 31 20 2a 2f 20  .     /* 121 */ 
bb630 22 56 42 65 67 69 6e 22 2c 0a 20 20 20 20 20 2f  "VBegin",.     /
bb640 2a 20 31 32 32 20 2a 2f 20 22 56 43 72 65 61 74  * 122 */ "VCreat
bb650 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 33 20  e",.     /* 123 
bb660 2a 2f 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20  */ "VDestroy",. 
bb670 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f 20 22 56      /* 124 */ "V
bb680 4f 70 65 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Open",.     /* 1
bb690 32 35 20 2a 2f 20 22 56 46 69 6c 74 65 72 22 2c  25 */ "VFilter",
bb6a0 0a 20 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20  .     /* 126 */ 
bb6b0 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20  "VColumn",.     
bb6c0 2f 2a 20 31 32 37 20 2a 2f 20 22 56 4e 65 78 74  /* 127 */ "VNext
bb6d0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 38 20 2a  ",.     /* 128 *
bb6e0 2f 20 22 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20  / "VRename",.   
bb6f0 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22 56 55 70    /* 129 */ "VUp
bb700 64 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31  date",.     /* 1
bb710 33 30 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 20 20  30 */ "Real",.  
bb720 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20 22 50 61     /* 131 */ "Pa
bb730 67 65 63 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f  gecount",.     /
bb740 2a 20 31 33 32 20 2a 2f 20 22 54 72 61 63 65 22  * 132 */ "Trace"
bb750 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 2a 2f  ,.     /* 133 */
bb760 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f 2a   "Noop",.     /*
bb770 20 31 33 34 20 2a 2f 20 22 45 78 70 6c 61 69 6e   134 */ "Explain
bb780 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 35 20 2a  ",.     /* 135 *
bb790 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 35 22 2c  / "NotUsed_135",
bb7a0 0a 20 20 20 20 20 2f 2a 20 31 33 36 20 2a 2f 20  .     /* 136 */ 
bb7b0 22 4e 6f 74 55 73 65 64 5f 31 33 36 22 2c 0a 20  "NotUsed_136",. 
bb7c0 20 20 20 20 2f 2a 20 31 33 37 20 2a 2f 20 22 4e      /* 137 */ "N
bb7d0 6f 74 55 73 65 64 5f 31 33 37 22 2c 0a 20 20 20  otUsed_137",.   
bb7e0 20 20 2f 2a 20 31 33 38 20 2a 2f 20 22 4e 6f 74    /* 138 */ "Not
bb7f0 55 73 65 64 5f 31 33 38 22 2c 0a 20 20 20 20 20  Used_138",.     
bb800 2f 2a 20 31 33 39 20 2a 2f 20 22 4e 6f 74 55 73  /* 139 */ "NotUs
bb810 65 64 5f 31 33 39 22 2c 0a 20 20 20 20 20 2f 2a  ed_139",.     /*
bb820 20 31 34 30 20 2a 2f 20 22 4e 6f 74 55 73 65 64   140 */ "NotUsed
bb830 5f 31 34 30 22 2c 0a 20 20 20 20 20 2f 2a 20 31  _140",.     /* 1
bb840 34 31 20 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a  41 */ "ToText",.
bb850 20 20 20 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22       /* 142 */ "
bb860 54 6f 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a  ToBlob",.     /*
bb870 20 31 34 33 20 2a 2f 20 22 54 6f 4e 75 6d 65 72   143 */ "ToNumer
bb880 69 63 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 34  ic",.     /* 144
bb890 20 2a 2f 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20   */ "ToInt",.   
bb8a0 20 20 2f 2a 20 31 34 35 20 2a 2f 20 22 54 6f 52    /* 145 */ "ToR
bb8b0 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74  eal",.  };.  ret
bb8c0 75 72 6e 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d  urn azName[i];.}
bb8d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
bb8e0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
bb8f0 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a  opcodes.c ******
bb900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb920 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
bb930 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
bb940 69 6c 65 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a  ile os_os2.c ***
bb950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb970 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
bb980 30 30 36 20 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a  006 Feb 14.**.**
bb990 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
bb9a0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
bb9b0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
bb9c0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
bb9d0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
bb9e0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
bb9f0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
bba00 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
bba10 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
bba20 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
bba30 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
bba40 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
bba50 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
bba60 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
bba70 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
bba80 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
bba90 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
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 2a 2a 2a 2a 2a  ****************
bbad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
bbaf0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
bbb00 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61  ontains code tha
bbb10 74 20 69 73 20 73 70 65 63 69 66 69 63 20 74 6f  t is specific to
bbb20 20 4f 53 2f 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64   OS/2..**.** $Id
bbb30 3a 20 6f 73 5f 6f 73 32 2e 63 2c 76 20 31 2e 36  : os_os2.c,v 1.6
bbb40 33 20 32 30 30 38 2f 31 32 2f 31 30 20 31 39 3a  3 2008/12/10 19:
bbb50 32 36 3a 32 34 20 64 72 68 20 45 78 70 20 24 0a  26:24 drh Exp $.
bbb60 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  */...#if SQLITE_
bbb70 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20  OS_OS2../*.** A 
bbb80 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72  Note About Memor
bbb90 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a  y Allocation:.**
bbba0 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 65 72 20  .** This driver 
bbbb0 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72  uses malloc()/fr
bbbc0 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 72 61  ee() directly ra
bbbd0 74 68 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 20  ther than going 
bbbe0 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53  through.** the S
bbbf0 51 4c 69 74 65 2d 77 72 61 70 70 65 72 73 20 73  QLite-wrappers s
bbc00 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f  qlite3_malloc()/
bbc10 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 20  sqlite3_free(). 
bbc20 20 54 68 6f 73 65 20 77 72 61 70 70 65 72 73 0a   Those wrappers.
bbc30 2a 2a 20 61 72 65 20 64 65 73 69 67 6e 65 64 20  ** are designed 
bbc40 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d 62 65 64  for use on embed
bbc50 64 65 64 20 73 79 73 74 65 6d 73 20 77 68 65 72  ded systems wher
bbc60 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 63 61 72  e memory is scar
bbc70 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63  ce and.** malloc
bbc80 20 66 61 69 6c 75 72 65 73 20 68 61 70 70 65 6e   failures happen
bbc90 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 20 4f 53   frequently.  OS
bbca0 2f 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70 69  /2 does not typi
bbcb0 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20  cally run on.** 
bbcc0 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73  embedded systems
bbcd0 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64 6f  , and when it do
bbce0 65 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72  es the developer
bbcf0 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20  s normally have 
bbd00 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65  bigger.** proble
bbd10 6d 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75  ms to worry abou
bbd20 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f  t than running o
bbd30 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53  ut of memory.  S
bbd40 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a  o there is not.*
bbd50 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e  * a compelling n
bbd60 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 77  eed to use the w
bbd70 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42  rappers..**.** B
bbd80 75 74 20 74 68 65 72 65 20 69 73 20 61 20 67 6f  ut there is a go
bbd90 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74  od reason to not
bbda0 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72   use the wrapper
bbdb0 73 2e 20 20 49 66 20 77 65 20 75 73 65 20 74 68  s.  If we use th
bbdc0 65 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74 68  e.** wrappers th
bbdd0 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20 73  en we will get s
bbde0 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28  imulated malloc(
bbdf0 29 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69  ) failures withi
bbe00 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65 72  n this.** driver
bbe10 2e 20 20 41 6e 64 20 74 68 61 74 20 63 61 75 73  .  And that caus
bbe20 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20  es all kinds of 
bbe30 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72  problems for our
bbe40 20 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63   tests.  We.** c
bbe50 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51 4c  ould enhance SQL
bbe60 69 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68  ite to deal with
bbe70 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f   simulated mallo
bbe80 63 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69  c failures withi
bbe90 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69 76  n.** the OS driv
bbea0 65 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64 65  er, but the code
bbeb0 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68   to deal with th
bbec0 6f 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75 6c  ose failure woul
bbed0 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72  d not.** be exer
bbee0 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28  cised on Linux (
bbef0 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e  which does not n
bbf00 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20  eed to malloc() 
bbf10 69 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a 2a  in the driver).*
bbf20 2a 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c  * and so we woul
bbf30 64 20 68 61 76 65 20 64 69 66 66 69 63 75 6c 74  d have difficult
bbf40 79 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72 61  y writing covera
bbf50 67 65 20 74 65 73 74 73 20 66 6f 72 20 74 68 61  ge tests for tha
bbf60 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74 74  t.** code.  Bett
bbf70 65 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20  er to leave the 
bbf80 63 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68 69  code out, we thi
bbf90 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  nk..**.** The po
bbfa0 69 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73 63  int of this disc
bbfb0 75 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c  ussion is as fol
bbfc0 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65 61  lows:  When crea
bbfd0 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53  ting a new.** OS
bbfe0 20 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65 6d   layer for an em
bbff0 62 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20 69  bedded system, i
bc000 66 20 79 6f 75 20 75 73 65 20 74 68 69 73 20 66  f you use this f
bc010 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70 6c  ile as an exampl
bc020 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20  e,.** avoid the 
bc030 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f  use of malloc()/
bc040 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 72  free().  Those r
bc050 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20  outines work ok 
bc060 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64 65 73 6b 74  on OS/2.** deskt
bc070 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20 77  ops but not so w
bc080 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64 20  ell in embedded 
bc090 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a  systems..*/../*.
bc0a0 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 64 20 74  ** Macros used t
bc0b0 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74  o determine whet
bc0c0 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73  her or not to us
bc0d0 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69  e threads..*/.#i
bc0e0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
bc0f0 5f 54 48 52 45 41 44 53 41 46 45 29 20 26 26 20  _THREADSAFE) && 
bc100 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
bc110 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
bc120 45 5f 4f 53 32 5f 54 48 52 45 41 44 53 20 31 0a  E_OS2_THREADS 1.
bc130 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e  #endif../*.** In
bc140 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20  clude code that 
bc150 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  is common to all
bc160 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f   os_*.c files.*/
bc170 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bc180 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d   Include os_comm
bc190 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  on.h in the midd
bc1a0 6c 65 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a  le of os_os2.c *
bc1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bc1c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bc1d0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63   Begin file os_c
bc1e0 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
bc1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bc210 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
bc220 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
bc230 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
bc240 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
bc250 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
bc260 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
bc270 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
bc280 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
bc290 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
bc2a0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
bc2b0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
bc2c0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
bc2d0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
bc2e0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
bc2f0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
bc300 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
bc310 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
bc320 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
bc330 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
bc340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2a 2a 2a 2a 2a 2a  ****************
bc380 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
bc390 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
bc3a0 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74  macros and a lit
bc3b0 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20  tle bit of code 
bc3c0 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
bc3d0 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20  o.** all of the 
bc3e0 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69  platform-specifi
bc3f0 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29  c files (os_*.c)
bc400 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65   and is #include
bc410 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20  d into those.** 
bc420 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  files..**.** Thi
bc430 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  s file should be
bc440 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68   #included by th
bc450 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f  e os_*.c files o
bc460 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20  nly.  It is not 
bc470 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72  a.** general pur
bc480 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65  pose header file
bc490 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f  ..**.** $Id: os_
bc4a0 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20  common.h,v 1.38 
bc4b0 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34 30  2009/02/24 18:40
bc4c0 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :50 danielk1977 
bc4d0 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
bc4e0 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23   _OS_COMMON_H_.#
bc4f0 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f  define _OS_COMMO
bc500 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c  N_H_../*.** At l
bc510 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61  east two bugs ha
bc520 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65  ve slipped in be
bc530 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64  cause we changed
bc540 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55   the MEMORY_DEBU
bc550 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51  G.** macro to SQ
bc560 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73  LITE_DEBUG and s
bc570 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69  ome older makefi
bc580 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74  les have not yet
bc590 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69   made the.** swi
bc5a0 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  tch.  The follow
bc5b0 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  ing code should 
bc5c0 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c  catch this probl
bc5d0 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  em at compile-ti
bc5e0 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45  me..*/.#ifdef ME
bc5f0 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72  MORY_DEBUG.# err
bc600 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44  or "The MEMORY_D
bc610 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62  EBUG macro is ob
bc620 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c  solete.  Use SQL
bc630 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61  ITE_DEBUG instea
bc640 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  d.".#endif..#ifd
bc650 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
bc660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
bc670 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  nt sqlite3OSTrac
bc680 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f  e = 0;.#define O
bc690 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20  STRACE1(X)      
bc6a0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53     if( sqlite3OS
bc6b0 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
bc6c0 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64  ebugPrintf(X).#d
bc6d0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58  efine OSTRACE2(X
bc6e0 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 71  ,Y)       if( sq
bc6f0 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73  lite3OSTrace ) s
bc700 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
bc710 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f  f(X,Y).#define O
bc720 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20  STRACE3(X,Y,Z)  
bc730 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53     if( sqlite3OS
bc740 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
bc750 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
bc760 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
bc770 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66  E4(X,Y,Z,A)   if
bc780 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
bc790 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
bc7a0 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23  rintf(X,Y,Z,A).#
bc7b0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28  define OSTRACE5(
bc7c0 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73  X,Y,Z,A,B) if( s
bc7d0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
bc7e0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
bc7f0 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64  tf(X,Y,Z,A,B).#d
bc800 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58  efine OSTRACE6(X
bc810 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20  ,Y,Z,A,B,C) \.  
bc820 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72    if(sqlite3OSTr
bc830 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75  ace) sqlite3Debu
bc840 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c  gPrintf(X,Y,Z,A,
bc850 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  B,C).#define OST
bc860 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c  RACE7(X,Y,Z,A,B,
bc870 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71  C,D) \.    if(sq
bc880 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71  lite3OSTrace) sq
bc890 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
bc8a0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a  (X,Y,Z,A,B,C,D).
bc8b0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53  #else.#define OS
bc8c0 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e  TRACE1(X).#defin
bc8d0 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a  e OSTRACE2(X,Y).
bc8e0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33  #define OSTRACE3
bc8f0 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20  (X,Y,Z).#define 
bc900 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41  OSTRACE4(X,Y,Z,A
bc910 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
bc920 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64  E5(X,Y,Z,A,B).#d
bc930 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58  efine OSTRACE6(X
bc940 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66  ,Y,Z,A,B,C).#def
bc950 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59  ine OSTRACE7(X,Y
bc960 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64  ,Z,A,B,C,D).#end
bc970 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73  if../*.** Macros
bc980 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65   for performance
bc990 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61   tracing.  Norma
bc9a0 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20  lly turned off. 
bc9b0 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f   Only works.** o
bc9c0 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 2e  n i486 hardware.
bc9d0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
bc9e0 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52  E_PERFORMANCE_TR
bc9f0 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69  ACE../* .** hwti
bca00 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e  me.h contains in
bca10 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63  line assembler c
bca20 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e  ode for implemen
bca30 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65  ting .** high-pe
bca40 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67  rformance timing
bca50 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a   routines..*/./*
bca60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
bca70 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69  clude hwtime.h i
bca80 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
bca90 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
bcaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
bcab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
bcac0 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e  gin file hwtime.
bcad0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
bcae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
bcb00 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a  .** 2008 May 27.
bcb10 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
bcb20 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
bcb30 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
bcb40 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
bcb50 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
bcb60 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
bcb70 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
bcb80 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
bcb90 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
bcba0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
bcbb0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
bcbc0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
bcbd0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
bcbe0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
bcbf0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
bcc00 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
bcc10 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
bcc20 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
bcc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcc70 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
bcc80 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c  ile contains inl
bcc90 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72  ine asm code for
bcca0 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67   retrieving "hig
bccb0 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a  h-performance".*
bccc0 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78  * counters for x
bccd0 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a  86 class CPUs..*
bcce0 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65  *.** $Id: hwtime
bccf0 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38  .h,v 1.3 2008/08
bcd00 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61  /01 14:33:15 sha
bcd10 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  ne Exp $.*/.#ifn
bcd20 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23  def _HWTIME_H_.#
bcd30 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48  define _HWTIME_H
bcd40 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  _../*.** The fol
bcd50 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f  lowing routine o
bcd60 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e  nly works on pen
bcd70 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e  tium-class (or n
bcd80 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73  ewer) processors
bcd90 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65  ..** It uses the
bcda0 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f   RDTSC opcode to
bcdb0 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20   read the cycle 
bcdc0 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20  count value out 
bcdd0 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73  of the.** proces
bcde0 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20  sor and returns 
bcdf0 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69  that value.  Thi
bce00 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f  s can be used fo
bce10 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72  r high-res.** pr
bce20 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20  ofiling..*/.#if 
bce30 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f  (defined(__GNUC_
bce40 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d  _) || defined(_M
bce50 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20  SC_VER)) && \.  
bce60 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38      (defined(i38
bce70 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  6) || defined(__
bce80 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e  i386__) || defin
bce90 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20  ed(_M_IX86))..  
bcea0 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e  #if defined(__GN
bceb0 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  UC__)..  __inlin
bcec0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
bced0 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
bcee0 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69  void){.     unsi
bcef0 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b  gned int lo, hi;
bcf00 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f  .     __asm__ __
bcf10 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74  volatile__ ("rdt
bcf20 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c  sc" : "=a" (lo),
bcf30 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20   "=d" (hi));.   
bcf40 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65    return (sqlite
bcf50 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32  _uint64)hi << 32
bcf60 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65   | lo;.  }..  #e
bcf70 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43  lif defined(_MSC
bcf80 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73  _VER)..  __decls
bcf90 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c  pec(naked) __inl
bcfa0 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  ine sqlite_uint6
bcfb0 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65  4 __cdecl sqlite
bcfc0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
bcfd0 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20      __asm {.    
bcfe0 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20      rdtsc.      
bcff0 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65    ret       ; re
bd000 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44  turn value at ED
bd010 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d  X:EAX.     }.  }
bd020 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69  ..  #endif..#eli
bd030 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  f (defined(__GNU
bd040 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28  C__) && defined(
bd050 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20  __x86_64__))..  
bd060 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
bd070 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
bd080 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
bd090 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
bd0a0 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61  g val;.      __a
bd0b0 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
bd0c0 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41  _ ("rdtsc" : "=A
bd0d0 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20  " (val));.      
bd0e0 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a  return val;.  }.
bd0f0 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64   .#elif (defined
bd100 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65  (__GNUC__) && de
bd110 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a  fined(__ppc__)).
bd120 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71  .  __inline__ sq
bd130 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
bd140 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
bd150 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
bd160 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c  long long retval
bd170 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  ;.      unsigned
bd180 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20   long junk;.    
bd190 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61    __asm__ __vola
bd1a0 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20  tile__ ("\n\.   
bd1b0 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d         1:      m
bd1c0 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20  ftbu   %1\n\.   
bd1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
bd1e0 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20  ftb    %L0\n\.  
bd1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd200 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20  mftbu   %0\n\.  
bd210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd220 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c  cmpw    %0,%1\n\
bd230 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
bd240 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20     bne     1b". 
bd250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd260 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29   : "=r" (retval)
bd270 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a  , "=r" (junk));.
bd280 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74        return ret
bd290 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a  val;.  }..#else.
bd2a0 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69  .  #error Need i
bd2b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
bd2c0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
bd2d0 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f   for your platfo
bd2e0 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54  rm...  /*.  ** T
bd2f0 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75  o compile withou
bd300 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73  t implementing s
bd310 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66  qlite3Hwtime() f
bd320 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d  or your platform
bd330 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72  ,.  ** you can r
bd340 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20  emove the above 
bd350 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74  #error and use t
bd360 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a  he following.  *
bd370 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e  * stub function.
bd380 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20    You will lose 
bd390 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66  timing support f
bd3a0 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20  or many.  ** of 
bd3b0 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e  the debugging an
bd3c0 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74  d testing utilit
bd3d0 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75  ies, but it shou
bd3e0 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74  ld at.  ** least
bd3f0 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e   compile and run
bd400 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ..  */.SQLITE_PR
bd410 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75  IVATE   sqlite_u
bd420 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
bd430 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72  ime(void){ retur
bd440 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36  n ((sqlite_uint6
bd450 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a  4)0); }..#endif.
bd460 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
bd470 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20  ned(_HWTIME_H_) 
bd480 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
bd490 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d  *** End of hwtim
bd4a0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
bd4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd4d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
bd4e0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
bd4f0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
bd500 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   in os_common.h 
bd510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd520 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69  **/..static sqli
bd530 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72  te_uint64 g_star
bd540 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  t;.static sqlite
bd550 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65  _uint64 g_elapse
bd560 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  d;.#define TIMER
bd570 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73  _START       g_s
bd580 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69  tart=sqlite3Hwti
bd590 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d  me().#define TIM
bd5a0 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67  ER_END         g
bd5b0 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33  _elapsed=sqlite3
bd5c0 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74  Hwtime()-g_start
bd5d0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
bd5e0 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61  LAPSED     g_ela
bd5f0 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69  psed.#else.#defi
bd600 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23  ne TIMER_START.#
bd610 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44  define TIMER_END
bd620 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
bd630 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c  LAPSED     ((sql
bd640 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65  ite_uint64)0).#e
bd650 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77  ndif../*.** If w
bd660 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74  e compile with t
bd670 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d  he SQLITE_TEST m
bd680 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74  acro set, then t
bd690 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
bd6a0 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69  ck.** of code wi
bd6b0 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 61  ll give us the a
bd6c0 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61  bility to simula
bd6d0 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72  te a disk I/O er
bd6e0 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73  ror.  This.** is
bd6f0 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
bd700 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65  g the I/O recove
bd710 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66  ry logic..*/.#if
bd720 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
bd730 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
bd740 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
bd750 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  hit = 0;        
bd760 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
bd770 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72  ber of I/O Error
bd780 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  s */.SQLITE_API 
bd790 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
bd7a0 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30  rror_hardhit = 0
bd7b0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
bd7c0 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e  er of non-benign
bd7d0 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54   errors */.SQLIT
bd7e0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
bd7f0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
bd800 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  ng = 0;        /
bd810 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20  * Count down to 
bd820 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20  first I/O error 
bd830 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
bd840 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
bd850 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20  or_persist = 0; 
bd860 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
bd870 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72  f I/O errors per
bd880 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  sist */.SQLITE_A
bd890 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
bd8a0 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d  o_error_benign =
bd8b0 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54   0;         /* T
bd8c0 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 72  rue if errors ar
bd8d0 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49  e benign */.SQLI
bd8e0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
bd8f0 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
bd900 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f  ing = 0;.SQLITE_
bd910 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
bd920 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64  diskfull = 0;.#d
bd930 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
bd940 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73  ErrorBenign(X) s
bd950 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
bd960 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69  benign=(X).#defi
bd970 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
bd980 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66  or(CODE)  \.  if
bd990 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  ( (sqlite3_io_er
bd9a0 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 73  ror_persist && s
bd9b0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
bd9c0 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c  hit) \.       ||
bd9d0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
bd9e0 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31  r_pending-- == 1
bd9f0 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20   )  \.          
bda00 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72      { local_ioer
bda10 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61  r(); CODE; }.sta
bda20 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69  tic void local_i
bda30 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43  oerr(){.  IOTRAC
bda40 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a  E(("IOERR\n"));.
bda50 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
bda60 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20  or_hit++;.  if( 
bda70 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f  !sqlite3_io_erro
bda80 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74  r_benign ) sqlit
bda90 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64  e3_io_error_hard
bdaa0 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65  hit++;.}.#define
bdab0 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
bdac0 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20  lError(CODE) \. 
bdad0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69    if( sqlite3_di
bdae0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29  skfull_pending )
bdaf0 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c  { \.     if( sql
bdb00 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
bdb10 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a  nding == 1 ){ \.
bdb20 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65         local_ioe
bdb30 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73  rr(); \.       s
bdb40 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20  qlite3_diskfull 
bdb50 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71  = 1; \.       sq
bdb60 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
bdb70 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20  it = 1; \.      
bdb80 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65   CODE; \.     }e
bdb90 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71  lse{ \.       sq
bdba0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
bdbb0 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20  ending--; \.    
bdbc0 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a   } \.   }.#else.
bdbd0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
bdbe0 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29  IOErrorBenign(X)
bdbf0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
bdc00 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66  eIOError(A).#def
bdc10 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  ine SimulateDisk
bdc20 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e  fullError(A).#en
bdc30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  dif../*.** When 
bdc40 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20  testing, keep a 
bdc50 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d  count of the num
bdc60 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65  ber of open file
bdc70 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  s..*/.#ifdef SQL
bdc80 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
bdc90 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
bdca0 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20  open_file_count 
bdcb0 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65  = 0;.#define Ope
bdcc0 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c  nCounter(X)  sql
bdcd0 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63  ite3_open_file_c
bdce0 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a  ount+=(X).#else.
bdcf0 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e  #define OpenCoun
bdd00 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23  ter(X).#endif..#
bdd10 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
bdd20 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29  d(_OS_COMMON_H_)
bdd30 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
bdd40 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63  **** End of os_c
bdd50 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
bdd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdd80 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
bdd90 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
bdda0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
bddb0 66 20 69 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a  f in os_os2.c **
bddc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bddd0 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ***/../*.** The 
bdde0 6f 73 32 46 69 6c 65 20 73 74 72 75 63 74 75 72  os2File structur
bddf0 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66  e is subclass of
bde00 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70   sqlite3_file sp
bde10 65 63 69 66 69 63 20 66 6f 72 20 74 68 65 20 4f  ecific for the O
bde20 53 2f 32 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69  S/2.** protabili
bde30 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70  ty layer..*/.typ
bde40 65 64 65 66 20 73 74 72 75 63 74 20 6f 73 32 46  edef struct os2F
bde50 69 6c 65 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72  ile os2File;.str
bde60 75 63 74 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20  uct os2File {.  
bde70 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
bde80 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f  _methods *pMetho
bde90 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 74 68  d;  /* Always th
bdea0 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f  e first entry */
bdeb0 0a 20 20 48 46 49 4c 45 20 68 3b 20 20 20 20 20  .  HFILE h;     
bdec0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
bded0 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73  Handle for acces
bdee0 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f  sing the file */
bdef0 0a 20 20 63 68 61 72 2a 20 70 61 74 68 54 6f 44  .  char* pathToD
bdf00 65 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  el;          /* 
bdf10 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
bdf20 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 2c  delete on close,
bdf30 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a   NULL if not */.
bdf40 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
bdf50 6c 6f 63 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54  locktype;   /* T
bdf60 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72  ype of lock curr
bdf70 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68  ently held on th
bdf80 69 73 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23  is file */.};..#
bdf90 64 65 66 69 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45  define LOCK_TIME
bdfa0 4f 55 54 20 31 30 4c 20 2f 2a 20 74 68 65 20 64  OUT 10L /* the d
bdfb0 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 74  efault locking t
bdfc0 69 6d 65 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  imeout */../****
bdfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be010 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  *********.** The
be020 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72   next group of r
be030 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e  outines implemen
be040 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64  t the I/O method
be050 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62  s specified.** b
be060 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f  y the sqlite3_io
be070 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e  _methods object.
be080 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
be090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
be0d0 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
be0e0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
be0f0 69 6e 74 20 6f 73 32 43 6c 6f 73 65 28 20 73 71  int os2Close( sq
be100 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 20 29  lite3_file *id )
be110 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  {.  APIRET rc = 
be120 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46  NO_ERROR;.  os2F
be130 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 69 66  ile *pFile;.  if
be140 28 20 69 64 20 26 26 20 28 70 46 69 6c 65 20 3d  ( id && (pFile =
be150 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 20 21   (os2File*)id) !
be160 3d 20 30 20 29 7b 0a 20 20 20 20 4f 53 54 52 41  = 0 ){.    OSTRA
be170 43 45 32 28 20 22 43 4c 4f 53 45 20 25 64 5c 6e  CE2( "CLOSE %d\n
be180 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20  ", pFile->h );. 
be190 20 20 20 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65     rc = DosClose
be1a0 28 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20  ( pFile->h );.  
be1b0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
be1c0 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20  e = NO_LOCK;.   
be1d0 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 68   if( pFile->path
be1e0 54 6f 44 65 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b  ToDel != NULL ){
be1f0 0a 20 20 20 20 20 20 72 63 20 3d 20 44 6f 73 46  .      rc = DosF
be200 6f 72 63 65 44 65 6c 65 74 65 28 20 28 50 53 5a  orceDelete( (PSZ
be210 29 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65  )pFile->pathToDe
be220 6c 20 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28  l );.      free(
be230 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65   pFile->pathToDe
be240 6c 20 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65  l );.      pFile
be250 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55  ->pathToDel = NU
be260 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 64  LL;.    }.    id
be270 20 3d 20 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f   = 0;.    OpenCo
be280 75 6e 74 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d  unter( -1 );.  }
be290 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d  ..  return rc ==
be2a0 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49   NO_ERROR ? SQLI
be2b0 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49  TE_OK : SQLITE_I
be2c0 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  OERR;.}../*.** R
be2d0 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20  ead data from a 
be2e0 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66  file into a buff
be2f0 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  er.  Return SQLI
be300 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20  TE_OK if all.** 
be310 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20  bytes were read 
be320 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64  successfully and
be330 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66   SQLITE_IOERR if
be340 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a   anything goes.*
be350 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74  * wrong..*/.stat
be360 69 63 20 69 6e 74 20 6f 73 32 52 65 61 64 28 0a  ic int os2Read(.
be370 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
be380 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  id,             
be390 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 72 65 61    /* File to rea
be3a0 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64  d from */.  void
be3b0 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20   *pBuf,         
be3c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
be3d0 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74  rite content int
be3e0 6f 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f  o this buffer */
be3f0 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20  .  int amt,     
be400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be410 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
be420 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f  bytes to read */
be430 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
be440 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20   offset         
be450 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64     /* Begin read
be460 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73  ing at this offs
be470 65 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47  et */.){.  ULONG
be480 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20   fileLocation = 
be490 30 4c 3b 0a 20 20 55 4c 4f 4e 47 20 67 6f 74 3b  0L;.  ULONG got;
be4a0 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
be4b0 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
be4c0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  ;.  assert( id!=
be4d0 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  0 );.  SimulateI
be4e0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
be4f0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
be500 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20   );.  OSTRACE3( 
be510 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64  "READ %d lock=%d
be520 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70  \n", pFile->h, p
be530 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
be540 3b 0a 20 20 69 66 28 20 44 6f 73 53 65 74 46 69  ;.  if( DosSetFi
be550 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20  lePtr(pFile->h, 
be560 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f 42 45 47  offset, FILE_BEG
be570 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f  IN, &fileLocatio
be580 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  n) != NO_ERROR )
be590 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
be5a0 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20  ITE_IOERR;.  }. 
be5b0 20 69 66 28 20 44 6f 73 52 65 61 64 28 20 70 46   if( DosRead( pF
be5c0 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d  ile->h, pBuf, am
be5d0 74 2c 20 26 67 6f 74 20 29 20 21 3d 20 4e 4f 5f  t, &got ) != NO_
be5e0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74  ERROR ){.    ret
be5f0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
be600 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28  _READ;.  }.  if(
be610 20 67 6f 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61   got == (ULONG)a
be620 6d 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20  mt ).    return 
be630 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73  SQLITE_OK;.  els
be640 65 20 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61  e {.    /* Unrea
be650 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68  d portions of th
be660 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 6d  e input buffer m
be670 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c  ust be zero-fill
be680 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74  ed */.    memset
be690 28 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b  (&((char*)pBuf)[
be6a0 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74  got], 0, amt-got
be6b0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
be6c0 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
be6d0 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  _READ;.  }.}../*
be6e0 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66  .** Write data f
be6f0 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74  rom a buffer int
be700 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72  o a file.  Retur
be710 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
be720 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d  uccess.** or som
be730 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f  e other error co
be740 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a  de on failure..*
be750 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
be760 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33  Write(.  sqlite3
be770 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20  _file *id,      
be780 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
be790 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a   to write into *
be7a0 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
be7b0 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20  pBuf,           
be7c0 20 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65 73      /* The bytes
be7d0 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 2a   to be written *
be7e0 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20  /.  int amt,    
be7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be800 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
be810 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 65 20   bytes to write 
be820 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
be830 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20  64 offset       
be840 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
be850 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20  nto the file to 
be860 62 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74  begin writing at
be870 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66   */.){.  ULONG f
be880 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c  ileLocation = 0L
be890 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  ;.  APIRET rc = 
be8a0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e  NO_ERROR;.  ULON
be8b0 47 20 77 72 6f 74 65 3b 0a 20 20 6f 73 32 46 69  G wrote;.  os2Fi
be8c0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32  le *pFile = (os2
be8d0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65  File*)id;.  asse
be8e0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53  rt( id!=0 );.  S
be8f0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
be900 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
be910 45 52 52 5f 57 52 49 54 45 20 29 3b 0a 20 20 53  ERR_WRITE );.  S
be920 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45  imulateDiskfullE
be930 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
be940 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f 53  ITE_FULL );.  OS
be950 54 52 41 43 45 33 28 20 22 57 52 49 54 45 20 25  TRACE3( "WRITE %
be960 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46  d lock=%d\n", pF
be970 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c  ile->h, pFile->l
be980 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 66 28  ocktype );.  if(
be990 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28 70   DosSetFilePtr(p
be9a0 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c  File->h, offset,
be9b0 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69   FILE_BEGIN, &fi
be9c0 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e  leLocation) != N
be9d0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72  O_ERROR ){.    r
be9e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
be9f0 52 52 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  RR;.  }.  assert
bea00 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69  ( amt>0 );.  whi
bea10 6c 65 28 20 61 6d 74 20 3e 20 30 20 26 26 0a 20  le( amt > 0 &&. 
bea20 20 20 20 20 20 20 20 20 28 20 72 63 20 3d 20 44          ( rc = D
bea30 6f 73 57 72 69 74 65 28 20 70 46 69 6c 65 2d 3e  osWrite( pFile->
bea40 68 2c 20 28 50 56 4f 49 44 29 70 42 75 66 2c 20  h, (PVOID)pBuf, 
bea50 61 6d 74 2c 20 26 77 72 6f 74 65 20 29 20 29 20  amt, &wrote ) ) 
bea60 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 26 26 0a 20  == NO_ERROR &&. 
bea70 20 20 20 20 20 20 20 20 77 72 6f 74 65 20 3e 20          wrote > 
bea80 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d  0.  ){.    amt -
bea90 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75  = wrote;.    pBu
beaa0 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75  f = &((char*)pBu
beab0 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a  f)[wrote];.  }..
beac0 20 20 72 65 74 75 72 6e 20 28 20 72 63 20 21 3d    return ( rc !=
bead0 20 4e 4f 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74   NO_ERROR || amt
beae0 20 3e 20 28 69 6e 74 29 77 72 6f 74 65 20 29 20   > (int)wrote ) 
beaf0 3f 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20  ? SQLITE_FULL : 
beb00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
beb10 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20  .** Truncate an 
beb20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73  open file to a s
beb30 70 65 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f  pecified size.*/
beb40 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 54  .static int os2T
beb50 72 75 6e 63 61 74 65 28 20 73 71 6c 69 74 65 33  runcate( sqlite3
beb60 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e  _file *id, i64 n
beb70 42 79 74 65 20 29 7b 0a 20 20 41 50 49 52 45 54  Byte ){.  APIRET
beb80 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a   rc = NO_ERROR;.
beb90 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65    os2File *pFile
beba0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b   = (os2File*)id;
bebb0 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 52  .  OSTRACE3( "TR
bebc0 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e  UNCATE %d %lld\n
bebd0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79  ", pFile->h, nBy
bebe0 74 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  te );.  Simulate
bebf0 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
bec00 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55  SQLITE_IOERR_TRU
bec10 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20 3d 20  NCATE );.  rc = 
bec20 44 6f 73 53 65 74 46 69 6c 65 53 69 7a 65 28 20  DosSetFileSize( 
bec30 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20  pFile->h, nByte 
bec40 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d  );.  return rc =
bec50 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c  = NO_ERROR ? SQL
bec60 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f  ITE_OK : SQLITE_
bec70 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a  IOERR_TRUNCATE;.
bec80 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
bec90 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e  _TEST./*.** Coun
beca0 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
becb0 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f  fullsyncs and no
becc0 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69  rmal syncs.  Thi
becd0 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73  s is used to tes
bece0 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20  t.** that syncs 
becf0 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72  and fullsyncs ar
bed00 65 20 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68  e occuring at th
bed10 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a  e right times..*
bed20 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
bed30 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f   sqlite3_sync_co
bed40 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f  unt = 0;.SQLITE_
bed50 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
bed60 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d  fullsync_count =
bed70 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   0;.#endif../*.*
bed80 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  * Make sure all 
bed90 77 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 74  writes to a part
beda0 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20  icular file are 
bedb0 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73  committed to dis
bedc0 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  k..*/.static int
bedd0 20 6f 73 32 53 79 6e 63 28 20 73 71 6c 69 74 65   os2Sync( sqlite
bede0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
bedf0 66 6c 61 67 73 20 29 7b 0a 20 20 6f 73 32 46 69  flags ){.  os2Fi
bee00 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32  le *pFile = (os2
bee10 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52  File*)id;.  OSTR
bee20 41 43 45 33 28 20 22 53 59 4e 43 20 25 64 20 6c  ACE3( "SYNC %d l
bee30 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  ock=%d\n", pFile
bee40 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
bee50 74 79 70 65 20 29 3b 0a 23 69 66 64 65 66 20 53  type );.#ifdef S
bee60 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28  QLITE_TEST.  if(
bee70 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
bee80 53 59 4e 43 5f 46 55 4c 4c 29 7b 0a 20 20 20 20  SYNC_FULL){.    
bee90 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63  sqlite3_fullsync
beea0 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20  _count++;.  }.  
beeb0 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75  sqlite3_sync_cou
beec0 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f  nt++;.#endif.  /
beed0 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 64  * If we compiled
beee0 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45   with the SQLITE
beef0 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74  _NO_SYNC flag, t
bef00 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73 20 61  hen syncing is a
bef10 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f  .  ** no-op.  */
bef20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e  .#ifdef SQLITE_N
bef30 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f  O_SYNC.  UNUSED_
bef40 50 41 52 41 4d 45 54 45 52 28 70 46 69 6c 65 29  PARAMETER(pFile)
bef50 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
bef60 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 72 65  E_OK;.#else.  re
bef70 74 75 72 6e 20 44 6f 73 52 65 73 65 74 42 75 66  turn DosResetBuf
bef80 66 65 72 28 20 70 46 69 6c 65 2d 3e 68 20 29 20  fer( pFile->h ) 
bef90 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51  == NO_ERROR ? SQ
befa0 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
befb0 5f 49 4f 45 52 52 3b 0a 23 65 6e 64 69 66 0a 7d  _IOERR;.#endif.}
befc0 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e  ../*.** Determin
befd0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69  e the current si
befe0 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20  ze of a file in 
beff0 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20  bytes.*/.static 
bf000 69 6e 74 20 6f 73 32 46 69 6c 65 53 69 7a 65 28  int os2FileSize(
bf010 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
bf020 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  d, sqlite3_int64
bf030 20 2a 70 53 69 7a 65 20 29 7b 0a 20 20 41 50 49   *pSize ){.  API
bf040 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f  RET rc = NO_ERRO
bf050 52 3b 0a 20 20 46 49 4c 45 53 54 41 54 55 53 33  R;.  FILESTATUS3
bf060 20 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 3b 0a   fsts3FileInfo;.
bf070 20 20 6d 65 6d 73 65 74 28 26 66 73 74 73 33 46    memset(&fsts3F
bf080 69 6c 65 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65  ileInfo, 0, size
bf090 6f 66 28 66 73 74 73 33 46 69 6c 65 49 6e 66 6f  of(fsts3FileInfo
bf0a0 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  ));.  assert( id
bf0b0 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74  !=0 );.  Simulat
bf0c0 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
bf0d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53   SQLITE_IOERR_FS
bf0e0 54 41 54 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f  TAT );.  rc = Do
bf0f0 73 51 75 65 72 79 46 69 6c 65 49 6e 66 6f 28 20  sQueryFileInfo( 
bf100 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e  ((os2File*)id)->
bf110 68 2c 20 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c  h, FIL_STANDARD,
bf120 20 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c   &fsts3FileInfo,
bf130 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 54 41 54   sizeof(FILESTAT
bf140 55 53 33 29 20 29 3b 0a 20 20 69 66 28 20 72 63  US3) );.  if( rc
bf150 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
bf160 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 66 73 74      *pSize = fst
bf170 73 33 46 69 6c 65 49 6e 66 6f 2e 63 62 46 69 6c  s3FileInfo.cbFil
bf180 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e;.    return SQ
bf190 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
bf1a0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
bf1b0 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b  ITE_IOERR_FSTAT;
bf1c0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63  .  }.}../*.** Ac
bf1d0 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20 6c  quire a reader l
bf1e0 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ock..*/.static i
bf1f0 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 20  nt getReadLock( 
bf200 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 29  os2File *pFile )
bf210 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f  {.  FILELOCK  Lo
bf220 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20  ckArea,.        
bf230 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a      UnlockArea;.
bf240 20 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20 20    APIRET res;.  
bf250 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61  memset(&LockArea
bf260 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b  , 0, sizeof(Lock
bf270 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74  Area));.  memset
bf280 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c  (&UnlockArea, 0,
bf290 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72   sizeof(UnlockAr
bf2a0 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61  ea));.  LockArea
bf2b0 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45  .lOffset = SHARE
bf2c0 44 5f 46 49 52 53 54 3b 0a 20 20 4c 6f 63 6b 41  D_FIRST;.  LockA
bf2d0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41  rea.lRange = SHA
bf2e0 52 45 44 5f 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f  RED_SIZE;.  Unlo
bf2f0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
bf300 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65   0L;.  UnlockAre
bf310 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20  a.lRange = 0L;. 
bf320 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c   res = DosSetFil
bf330 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68  eLocks( pFile->h
bf340 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
bf350 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54  LockArea, LOCK_T
bf360 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20  IMEOUT, 1L );.  
bf370 4f 53 54 52 41 43 45 33 28 20 22 47 45 54 52 45  OSTRACE3( "GETRE
bf380 41 44 4c 4f 43 4b 20 25 64 20 72 65 73 3d 25 64  ADLOCK %d res=%d
bf390 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
bf3a0 65 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  es );.  return r
bf3b0 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64  es;.}../*.** Und
bf3c0 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a  o a readlock.*/.
bf3d0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63  static int unloc
bf3e0 6b 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69  kReadLock( os2Fi
bf3f0 6c 65 20 2a 69 64 20 29 7b 0a 20 20 46 49 4c 45  le *id ){.  FILE
bf400 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a  LOCK  LockArea,.
bf410 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f              Unlo
bf420 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45 54  ckArea;.  APIRET
bf430 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26   res;.  memset(&
bf440 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  LockArea, 0, siz
bf450 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a  eof(LockArea));.
bf460 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b    memset(&Unlock
bf470 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
bf480 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20  UnlockArea));.  
bf490 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
bf4a0 20 3d 20 30 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65   = 0L;.  LockAre
bf4b0 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20  a.lRange = 0L;. 
bf4c0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66   UnlockArea.lOff
bf4d0 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52  set = SHARED_FIR
bf4e0 53 54 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ST;.  UnlockArea
bf4f0 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44  .lRange = SHARED
bf500 5f 53 49 5a 45 3b 0a 20 20 72 65 73 20 3d 20 44  _SIZE;.  res = D
bf510 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
bf520 69 64 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72  id->h, &UnlockAr
bf530 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c  ea, &LockArea, L
bf540 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20  OCK_TIMEOUT, 1L 
bf550 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22  );.  OSTRACE3( "
bf560 55 4e 4c 4f 43 4b 2d 52 45 41 44 4c 4f 43 4b 20  UNLOCK-READLOCK 
bf570 66 69 6c 65 20 68 61 6e 64 6c 65 3d 25 64 20 72  file handle=%d r
bf580 65 73 3d 25 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68  es=%d?\n", id->h
bf590 2c 20 72 65 73 20 29 3b 0a 20 20 72 65 74 75 72  , res );.  retur
bf5a0 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n res;.}../*.** 
bf5b0 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69  Lock the file wi
bf5c0 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63  th the lock spec
bf5d0 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
bf5e0 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e  er locktype - on
bf5f0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c  e.** of the foll
bf600 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  owing:.**.**    
bf610 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b   (1) SHARED_LOCK
bf620 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45  .**     (2) RESE
bf630 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  RVED_LOCK.**    
bf640 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   (3) PENDING_LOC
bf650 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43  K.**     (4) EXC
bf660 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a  LUSIVE_LOCK.**.*
bf670 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e  * Sometimes when
bf680 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20   requesting one 
bf690 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69  lock state, addi
bf6a0 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74  tional lock stat
bf6b0 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74  es.** are insert
bf6c0 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20  ed in between.  
bf6d0 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68  The locking migh
bf6e0 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66  t fail on one of
bf6f0 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72   the later.** tr
bf700 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e  ansitions leavin
bf710 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65  g the lock state
bf720 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
bf730 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20  what it started 
bf740 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f  but.** still sho
bf750 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20  rt of its goal. 
bf760 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
bf770 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61  hart shows the a
bf780 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69  llowed.** transi
bf790 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e  tions and the in
bf7a0 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69  serted intermedi
bf7b0 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a  ate states:.**.*
bf7c0 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e  *    UNLOCKED ->
bf7d0 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48   SHARED.**    SH
bf7e0 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44  ARED -> RESERVED
bf7f0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
bf800 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
bf810 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45  CLUSIVE.**    RE
bf820 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49  SERVED -> (PENDI
bf830 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
bf840 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d  .**    PENDING -
bf850 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a  > EXCLUSIVE.**.*
bf860 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
bf870 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73  ill only increas
bf880 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68 65 20 6f  e a lock.  The o
bf890 73 32 55 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69  s2Unlock() routi
bf8a0 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c  ne.** erases all
bf8b0 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 65 20 61   locks at once a
bf8c0 6e 64 20 72 65 74 75 72 6e 73 20 75 73 20 69 6d  nd returns us im
bf8d0 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63  mediately to loc
bf8e0 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a  king level 0..**
bf8f0 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   It is not possi
bf900 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 74 68 65  ble to lower the
bf910 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
bf920 6e 65 20 73 74 65 70 20 61 74 20 61 20 74 69 6d  ne step at a tim
bf930 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20  e.  You.** must 
bf940 67 6f 20 73 74 72 61 69 67 68 74 20 74 6f 20 6c  go straight to l
bf950 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a  ocking level 0..
bf960 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
bf970 32 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66  2Lock( sqlite3_f
bf980 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
bf990 6b 74 79 70 65 20 29 7b 0a 20 20 69 6e 74 20 72  ktype ){.  int r
bf9a0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
bf9b0 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
bf9c0 6f 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74  ode from subrout
bf9d0 69 6e 65 73 20 2a 2f 0a 20 20 41 50 49 52 45 54  ines */.  APIRET
bf9e0 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b   res = NO_ERROR;
bf9f0 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66      /* Result of
bfa00 20 61 6e 20 4f 53 2f 32 20 6c 6f 63 6b 20 63 61   an OS/2 lock ca
bfa10 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c  ll */.  int newL
bfa20 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f  ocktype;       /
bfa30 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63  * Set pFile->loc
bfa40 6b 74 79 70 65 20 74 6f 20 74 68 69 73 20 76 61  ktype to this va
bfa50 6c 75 65 20 62 65 66 6f 72 65 20 65 78 69 74 69  lue before exiti
bfa60 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50  ng */.  int gotP
bfa70 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f  endingLock = 0;/
bfa80 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 63 71  * True if we acq
bfa90 75 69 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20  uired a PENDING 
bfaa0 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a  lock this time *
bfab0 2f 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f  /.  FILELOCK  Lo
bfac0 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20  ckArea,.        
bfad0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a      UnlockArea;.
bfae0 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65    os2File *pFile
bfaf0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b   = (os2File*)id;
bfb00 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41  .  memset(&LockA
bfb10 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c  rea, 0, sizeof(L
bfb20 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d  ockArea));.  mem
bfb30 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  set(&UnlockArea,
bfb40 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63   0, sizeof(Unloc
bfb50 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65 72  kArea));.  asser
bfb60 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20  t( pFile!=0 );. 
bfb70 20 4f 53 54 52 41 43 45 34 28 20 22 4c 4f 43 4b   OSTRACE4( "LOCK
bfb80 20 25 64 20 25 64 20 77 61 73 20 25 64 5c 6e 22   %d %d was %d\n"
bfb90 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
bfba0 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63  type, pFile->loc
bfbb0 6b 74 79 70 65 20 29 3b 0a 0a 20 20 2f 2a 20 49  ktype );..  /* I
bfbc0 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61  f there is alrea
bfbd0 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69  dy a lock of thi
bfbe0 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72  s type or more r
bfbf0 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68  estrictive on th
bfc00 65 0a 20 20 2a 2a 20 6f 73 32 46 69 6c 65 2c 20  e.  ** os2File, 
bfc10 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27  do nothing. Don'
bfc20 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f  t use the end_lo
bfc30 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61  ck: exit path, a
bfc40 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  s.  ** sqlite3_m
bfc50 75 74 65 78 5f 65 6e 74 65 72 28 29 20 68 61 73  utex_enter() has
bfc60 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  n't been called 
bfc70 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  yet..  */.  if( 
bfc80 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
bfc90 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
bfca0 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b   OSTRACE3( "LOCK
bfcb0 20 25 64 20 25 64 20 6f 6b 20 28 61 6c 72 65 61   %d %d ok (alrea
bfcc0 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69  dy held)\n", pFi
bfcd0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20  le->h, locktype 
bfce0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
bfcf0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
bfd00 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
bfd10 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63   locking sequenc
bfd20 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a  e is correct.  *
bfd30 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  /.  assert( pFil
bfd40 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f  e->locktype!=NO_
bfd50 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65  LOCK || locktype
bfd60 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  ==SHARED_LOCK );
bfd70 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
bfd80 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype!=PENDING_LOC
bfd90 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  K );.  assert( l
bfda0 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45  ocktype!=RESERVE
bfdb0 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d  D_LOCK || pFile-
bfdc0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  >locktype==SHARE
bfdd0 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20  D_LOCK );..  /* 
bfde0 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 49 4e 47  Lock the PENDING
bfdf0 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 20 77 65  _LOCK byte if we
bfe00 20 6e 65 65 64 20 74 6f 20 61 63 71 75 69 72 65   need to acquire
bfe10 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   a PENDING lock 
bfe20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 41 52 45 44  or.  ** a SHARED
bfe30 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65 20 61 72   lock.  If we ar
bfe40 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53 48  e acquiring a SH
bfe50 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61  ARED lock, the a
bfe60 63 71 75 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20  cquisition of.  
bfe70 2a 2a 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c  ** the PENDING_L
bfe80 4f 43 4b 20 62 79 74 65 20 69 73 20 74 65 6d 70  OCK byte is temp
bfe90 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65  orary..  */.  ne
bfea0 77 4c 6f 63 6b 74 79 70 65 20 3d 20 70 46 69 6c  wLocktype = pFil
bfeb0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69  e->locktype;.  i
bfec0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
bfed0 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20  pe==NO_LOCK.    
bfee0 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d    || (locktype==
bfef0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26  EXCLUSIVE_LOCK &
bff00 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  & pFile->locktyp
bff10 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e==RESERVED_LOCK
bff20 29 0a 20 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41  ).  ){.    LockA
bff30 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45  rea.lOffset = PE
bff40 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20  NDING_BYTE;.    
bff50 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
bff60 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 1L;.    Unlock
bff70 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30  Area.lOffset = 0
bff80 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
bff90 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a  a.lRange = 0L;..
bffa0 20 20 20 20 2f 2a 20 77 61 69 74 20 6c 6f 6e 67      /* wait long
bffb0 65 72 20 74 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d  er than LOCK_TIM
bffc0 45 4f 55 54 20 68 65 72 65 20 6e 6f 74 20 74 6f  EOUT here not to
bffd0 20 68 61 76 65 20 74 6f 20 74 72 79 20 6d 75 6c   have to try mul
bffe0 74 69 70 6c 65 20 74 69 6d 65 73 20 2a 2f 0a 20  tiple times */. 
bfff0 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46     res = DosSetF
c0000 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d  ileLocks( pFile-
c0010 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  >h, &UnlockArea,
c0020 20 26 4c 6f 63 6b 41 72 65 61 2c 20 31 30 30 4c   &LockArea, 100L
c0030 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20  , 0L );.    if( 
c0040 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
c0050 29 7b 0a 20 20 20 20 20 20 67 6f 74 50 65 6e 64  ){.      gotPend
c0060 69 6e 67 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20  ingLock = 1;.   
c0070 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f     OSTRACE3( "LO
c0080 43 4b 20 25 64 20 70 65 6e 64 69 6e 67 20 6c 6f  CK %d pending lo
c0090 63 6b 20 62 6f 6f 6c 65 61 6e 20 73 65 74 2e 20  ck boolean set. 
c00a0 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   res=%d\n", pFil
c00b0 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 20  e->h, res );.   
c00c0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71   }.  }..  /* Acq
c00d0 75 69 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f  uire a shared lo
c00e0 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  ck.  */.  if( lo
c00f0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
c0100 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f  OCK && res == NO
c0110 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 61 73  _ERROR ){.    as
c0120 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
c0130 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29  ktype==NO_LOCK )
c0140 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65 74 52  ;.    res = getR
c0150 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a  eadLock(pFile);.
c0160 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e      if( res == N
c0170 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20  O_ERROR ){.     
c0180 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53   newLocktype = S
c0190 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20  HARED_LOCK;.    
c01a0 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20  }.    OSTRACE3( 
c01b0 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65  "LOCK %d acquire
c01c0 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 72 65   shared lock. re
c01d0 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s=%d\n", pFile->
c01e0 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20  h, res );.  }.. 
c01f0 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52 45   /* Acquire a RE
c0200 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f  SERVED lock.  */
c0210 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
c0220 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26  =RESERVED_LOCK &
c0230 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f  & res == NO_ERRO
c0240 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  R ){.    assert(
c0250 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c0260 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  ==SHARED_LOCK );
c0270 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  .    LockArea.lO
c0280 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44  ffset = RESERVED
c0290 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41  _BYTE;.    LockA
c02a0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b  rea.lRange = 1L;
c02b0 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  .    UnlockArea.
c02c0 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20  lOffset = 0L;.  
c02d0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
c02e0 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65  nge = 0L;.    re
c02f0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f  s = DosSetFileLo
c0300 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26  cks( pFile->h, &
c0310 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
c0320 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45  kArea, LOCK_TIME
c0330 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69  OUT, 0L );.    i
c0340 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52  f( res == NO_ERR
c0350 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c  OR ){.      newL
c0360 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 45 52 56  ocktype = RESERV
c0370 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20  ED_LOCK;.    }. 
c0380 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f     OSTRACE3( "LO
c0390 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 72 65  CK %d acquire re
c03a0 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20 72 65 73  served lock. res
c03b0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
c03c0 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20  , res );.  }..  
c03d0 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50 45 4e  /* Acquire a PEN
c03e0 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20  DING lock.  */. 
c03f0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45   if( locktype==E
c0400 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26  XCLUSIVE_LOCK &&
c0410 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   res == NO_ERROR
c0420 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74   ){.    newLockt
c0430 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f  ype = PENDING_LO
c0440 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 69  CK;.    gotPendi
c0450 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20  ngLock = 0;.    
c0460 4f 53 54 52 41 43 45 32 28 20 22 4c 4f 43 4b 20  OSTRACE2( "LOCK 
c0470 25 64 20 61 63 71 75 69 72 65 20 70 65 6e 64 69  %d acquire pendi
c0480 6e 67 20 6c 6f 63 6b 2e 20 70 65 6e 64 69 6e 67  ng lock. pending
c0490 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e   lock boolean un
c04a0 73 65 74 2e 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  set.\n", pFile->
c04b0 68 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  h );.  }..  /* A
c04c0 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 53  cquire an EXCLUS
c04d0 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20  IVE lock.  */.  
c04e0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58  if( locktype==EX
c04f0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20  CLUSIVE_LOCK && 
c0500 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
c0510 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
c0520 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d  File->locktype>=
c0530 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
c0540 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52     res = unlockR
c0550 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a  eadLock(pFile);.
c0560 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 75      OSTRACE2( "u
c0570 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e  nreadlock = %d\n
c0580 22 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 4c 6f  ", res );.    Lo
c0590 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c05a0 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20   SHARED_FIRST;. 
c05b0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e     LockArea.lRan
c05c0 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45  ge = SHARED_SIZE
c05d0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
c05e0 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20  .lOffset = 0L;. 
c05f0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52     UnlockArea.lR
c0600 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72  ange = 0L;.    r
c0610 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c  es = DosSetFileL
c0620 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20  ocks( pFile->h, 
c0630 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
c0640 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d  ckArea, LOCK_TIM
c0650 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20  EOUT, 0L );.    
c0660 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52  if( res == NO_ER
c0670 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77  ROR ){.      new
c0680 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55  Locktype = EXCLU
c0690 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  SIVE_LOCK;.    }
c06a0 65 6c 73 65 7b 0a 20 20 20 20 20 20 4f 53 54 52  else{.      OSTR
c06b0 41 43 45 32 28 20 22 4f 53 2f 32 20 65 72 72 6f  ACE2( "OS/2 erro
c06c0 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20  r-code = %d\n", 
c06d0 72 65 73 20 29 3b 0a 20 20 20 20 20 20 67 65 74  res );.      get
c06e0 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b  ReadLock(pFile);
c06f0 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41  .    }.    OSTRA
c0700 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 63  CE3( "LOCK %d ac
c0710 71 75 69 72 65 20 65 78 63 6c 75 73 69 76 65 20  quire exclusive 
c0720 6c 6f 63 6b 2e 20 20 72 65 73 3d 25 64 5c 6e 22  lock.  res=%d\n"
c0730 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
c0740 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
c0750 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 61  we are holding a
c0760 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68   PENDING lock th
c0770 61 74 20 6f 75 67 68 74 20 74 6f 20 62 65 20 72  at ought to be r
c0780 65 6c 65 61 73 65 64 2c 20 74 68 65 6e 0a 20 20  eleased, then.  
c0790 2a 2a 20 72 65 6c 65 61 73 65 20 69 74 20 6e 6f  ** release it no
c07a0 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f  w..  */.  if( go
c07b0 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20  tPendingLock && 
c07c0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
c07d0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74  _LOCK ){.    int
c07e0 20 72 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61   r;.    LockArea
c07f0 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20  .lOffset = 0L;. 
c0800 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e     LockArea.lRan
c0810 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c  ge = 0L;.    Unl
c0820 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c0830 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
c0840 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
c0850 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20  Range = 1L;.    
c0860 72 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f  r = DosSetFileLo
c0870 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26  cks( pFile->h, &
c0880 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
c0890 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45  kArea, LOCK_TIME
c08a0 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f  OUT, 0L );.    O
c08b0 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25  STRACE3( "LOCK %
c08c0 64 20 75 6e 6c 6f 63 6b 69 6e 67 20 70 65 6e 64  d unlocking pend
c08d0 69 6e 67 2f 69 73 20 73 68 61 72 65 64 2e 20 72  ing/is shared. r
c08e0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
c08f0 2c 20 72 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  , r );.  }..  /*
c0900 20 55 70 64 61 74 65 20 74 68 65 20 73 74 61 74   Update the stat
c0910 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 61  e of the lock ha
c0920 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 69  s held in the fi
c0930 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68  le descriptor th
c0940 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74  en.  ** return t
c0950 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72  he appropriate r
c0960 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f  esult code..  */
c0970 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f  .  if( res == NO
c0980 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 63  _ERROR ){.    rc
c0990 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
c09a0 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41  }else{.    OSTRA
c09b0 43 45 34 28 20 22 4c 4f 43 4b 20 46 41 49 4c 45  CE4( "LOCK FAILE
c09c0 44 20 25 64 20 74 72 79 69 6e 67 20 66 6f 72 20  D %d trying for 
c09d0 25 64 20 62 75 74 20 67 6f 74 20 25 64 5c 6e 22  %d but got %d\n"
c09e0 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  , pFile->h,.    
c09f0 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79            lockty
c0a00 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20  pe, newLocktype 
c0a10 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
c0a20 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70  TE_BUSY;.  }.  p
c0a30 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
c0a40 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20   newLocktype;.  
c0a50 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20  OSTRACE3( "LOCK 
c0a60 25 64 20 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46  %d now %d\n", pF
c0a70 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c  ile->h, pFile->l
c0a80 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 72 65 74  ocktype );.  ret
c0a90 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
c0aa0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
c0ab0 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73  ecks if there is
c0ac0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
c0ad0 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65   held on the spe
c0ae0 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62  cified.** file b
c0af0 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74  y this or any ot
c0b00 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20  her process. If 
c0b10 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68  such a lock is h
c0b20 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e  eld, return.** n
c0b30 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69  on-zero, otherwi
c0b40 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74  se zero..*/.stat
c0b50 69 63 20 69 6e 74 20 6f 73 32 43 68 65 63 6b 52  ic int os2CheckR
c0b60 65 73 65 72 76 65 64 4c 6f 63 6b 28 20 73 71 6c  eservedLock( sql
c0b70 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
c0b80 6e 74 20 2a 70 4f 75 74 20 29 7b 0a 20 20 69 6e  nt *pOut ){.  in
c0b90 74 20 72 20 3d 20 30 3b 0a 20 20 6f 73 32 46 69  t r = 0;.  os2Fi
c0ba0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32  le *pFile = (os2
c0bb0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65  File*)id;.  asse
c0bc0 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a  rt( pFile!=0 );.
c0bd0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
c0be0 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f  ktype>=RESERVED_
c0bf0 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20  LOCK ){.    r = 
c0c00 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  1;.    OSTRACE3(
c0c10 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25   "TEST WR-LOCK %
c0c20 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c  d %d (local)\n",
c0c30 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a   pFile->h, r );.
c0c40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 46 49 4c    }else{.    FIL
c0c50 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c  ELOCK  LockArea,
c0c60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 55  .              U
c0c70 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 20 20 41  nlockArea;.    A
c0c80 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52  PIRET rc = NO_ER
c0c90 52 4f 52 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  ROR;.    memset(
c0ca0 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69  &LockArea, 0, si
c0cb0 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b  zeof(LockArea));
c0cc0 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c  .    memset(&Unl
c0cd0 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65  ockArea, 0, size
c0ce0 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b  of(UnlockArea));
c0cf0 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  .    LockArea.lO
c0d00 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44  ffset = RESERVED
c0d10 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41  _BYTE;.    LockA
c0d20 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b  rea.lRange = 1L;
c0d30 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  .    UnlockArea.
c0d40 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20  lOffset = 0L;.  
c0d50 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
c0d60 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 63  nge = 0L;.    rc
c0d70 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63   = DosSetFileLoc
c0d80 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55  ks( pFile->h, &U
c0d90 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b  nlockArea, &Lock
c0da0 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f  Area, LOCK_TIMEO
c0db0 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53  UT, 0L );.    OS
c0dc0 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52  TRACE3( "TEST WR
c0dd0 2d 4c 4f 43 4b 20 25 64 20 6c 6f 63 6b 20 72 65  -LOCK %d lock re
c0de0 73 65 72 76 65 64 20 62 79 74 65 20 72 63 3d 25  served byte rc=%
c0df0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
c0e00 72 63 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  rc );.    if( rc
c0e10 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
c0e20 20 20 20 20 20 20 41 50 49 52 45 54 20 72 63 75        APIRET rcu
c0e30 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20   = NO_ERROR; /* 
c0e40 72 65 74 75 72 6e 20 63 6f 64 65 20 66 6f 72 20  return code for 
c0e50 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20  unlocking */.   
c0e60 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
c0e70 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20  set = 0L;.      
c0e80 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
c0e90 3d 20 30 4c 3b 0a 20 20 20 20 20 20 55 6e 6c 6f  = 0L;.      Unlo
c0ea0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c0eb0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a   RESERVED_BYTE;.
c0ec0 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61        UnlockArea
c0ed0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20  .lRange = 1L;.  
c0ee0 20 20 20 20 72 63 75 20 3d 20 44 6f 73 53 65 74      rcu = DosSet
c0ef0 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65  FileLocks( pFile
c0f00 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61  ->h, &UnlockArea
c0f10 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43  , &LockArea, LOC
c0f20 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b  K_TIMEOUT, 0L );
c0f30 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28  .      OSTRACE3(
c0f40 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25   "TEST WR-LOCK %
c0f50 64 20 75 6e 6c 6f 63 6b 20 72 65 73 65 72 76 65  d unlock reserve
c0f60 64 20 62 79 74 65 20 72 3d 25 64 5c 6e 22 2c 20  d byte r=%d\n", 
c0f70 70 46 69 6c 65 2d 3e 68 2c 20 72 63 75 20 29 3b  pFile->h, rcu );
c0f80 0a 20 20 20 20 7d 0a 20 20 20 20 72 20 3d 20 21  .    }.    r = !
c0f90 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29  (rc == NO_ERROR)
c0fa0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20  ;.    OSTRACE3( 
c0fb0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
c0fc0 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c   %d (remote)\n",
c0fd0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a   pFile->h, r );.
c0fe0 20 20 7d 0a 20 20 2a 70 4f 75 74 20 3d 20 72 3b    }.  *pOut = r;
c0ff0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
c1000 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  _OK;.}../*.** Lo
c1010 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
c1020 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65  level on file de
c1030 73 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c  scriptor id to l
c1040 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79  ocktype.  lockty
c1050 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69  pe.** must be ei
c1060 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20  ther NO_LOCK or 
c1070 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a  SHARED_LOCK..**.
c1080 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e  ** If the lockin
c1090 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66  g level of the f
c10a0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
c10b0 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20  s already at or 
c10c0 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71  below.** the req
c10d0 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c  uested locking l
c10e0 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69  evel, this routi
c10f0 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
c1100 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70  *.** It is not p
c1110 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73  ossible for this
c1120 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c   routine to fail
c1130 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   if the second a
c1140 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f  rgument.** is NO
c1150 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 73  _LOCK.  If the s
c1160 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
c1170 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68  s SHARED_LOCK th
c1180 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  en this routine.
c1190 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20  ** might return 
c11a0 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f  SQLITE_IOERR;.*/
c11b0 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 55  .static int os2U
c11c0 6e 6c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66  nlock( sqlite3_f
c11d0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
c11e0 6b 74 79 70 65 20 29 7b 0a 20 20 69 6e 74 20 74  ktype ){.  int t
c11f0 79 70 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  ype;.  os2File *
c1200 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
c1210 2a 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72  *)id;.  APIRET r
c1220 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
c1230 20 41 50 49 52 45 54 20 72 65 73 20 3d 20 4e 4f   APIRET res = NO
c1240 5f 45 52 52 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f  _ERROR;.  FILELO
c1250 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20  CK  LockArea,.  
c1260 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b            Unlock
c1270 41 72 65 61 3b 0a 20 20 6d 65 6d 73 65 74 28 26  Area;.  memset(&
c1280 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  LockArea, 0, siz
c1290 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a  eof(LockArea));.
c12a0 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b    memset(&Unlock
c12b0 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
c12c0 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20  UnlockArea));.  
c12d0 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30  assert( pFile!=0
c12e0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   );.  assert( lo
c12f0 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
c1300 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  OCK );.  OSTRACE
c1310 34 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f  4( "UNLOCK %d to
c1320 20 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70   %d was %d\n", p
c1330 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
c1340 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  e, pFile->lockty
c1350 70 65 20 29 3b 0a 20 20 74 79 70 65 20 3d 20 70  pe );.  type = p
c1360 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a  File->locktype;.
c1370 20 20 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c    if( type>=EXCL
c1380 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20  USIVE_LOCK ){.  
c1390 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
c13a0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63  et = 0L;.    Loc
c13b0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
c13c0 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
c13d0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52  a.lOffset = SHAR
c13e0 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 55 6e  ED_FIRST;.    Un
c13f0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
c1400 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20  = SHARED_SIZE;. 
c1410 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46     res = DosSetF
c1420 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d  ileLocks( pFile-
c1430 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  >h, &UnlockArea,
c1440 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b   &LockArea, LOCK
c1450 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a  _TIMEOUT, 0L );.
c1460 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 55      OSTRACE3( "U
c1470 4e 4c 4f 43 4b 20 25 64 20 65 78 63 6c 75 73 69  NLOCK %d exclusi
c1480 76 65 20 6c 6f 63 6b 20 72 65 73 3d 25 64 5c 6e  ve lock res=%d\n
c1490 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73  ", pFile->h, res
c14a0 20 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b   );.    if( lock
c14b0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
c14c0 4b 20 26 26 20 67 65 74 52 65 61 64 4c 6f 63 6b  K && getReadLock
c14d0 28 70 46 69 6c 65 29 20 21 3d 20 4e 4f 5f 45 52  (pFile) != NO_ER
c14e0 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ROR ){.      /* 
c14f0 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65  This should neve
c1500 72 20 68 61 70 70 65 6e 2e 20 20 57 65 20 73 68  r happen.  We sh
c1510 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 61  ould always be a
c1520 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ble to.      ** 
c1530 72 65 61 63 71 75 69 72 65 20 74 68 65 20 72 65  reacquire the re
c1540 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20  ad lock */.     
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 74 6f 20 25 64 20 67 65 74 52  CK %d to %d getR
c1570 65 61 64 4c 6f 63 6b 28 29 20 66 61 69 6c 65 64  eadLock() failed
c1580 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c  \n", pFile->h, l
c1590 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20  ocktype );.     
c15a0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
c15b0 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d  RR_UNLOCK;.    }
c15c0 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e  .  }.  if( type>
c15d0 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29  =RESERVED_LOCK )
c15e0 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  {.    LockArea.l
c15f0 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20  Offset = 0L;.   
c1600 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65   LockArea.lRange
c1610 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
c1620 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
c1630 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20  RESERVED_BYTE;. 
c1640 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52     UnlockArea.lR
c1650 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72  ange = 1L;.    r
c1660 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c  es = DosSetFileL
c1670 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20  ocks( pFile->h, 
c1680 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
c1690 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d  ckArea, LOCK_TIM
c16a0 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20  EOUT, 0L );.    
c16b0 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43  OSTRACE3( "UNLOC
c16c0 4b 20 25 64 20 72 65 73 65 72 76 65 64 20 72 65  K %d reserved re
c16d0 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s=%d\n", pFile->
c16e0 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20  h, res );.  }.  
c16f0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  if( locktype==NO
c1700 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e 3d 53  _LOCK && type>=S
c1710 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
c1720 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65    res = unlockRe
c1730 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20  adLock(pFile);. 
c1740 20 20 20 4f 53 54 52 41 43 45 35 28 20 22 55 4e     OSTRACE5( "UN
c1750 4c 4f 43 4b 20 25 64 20 69 73 20 25 64 20 77 61  LOCK %d is %d wa
c1760 6e 74 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c  nt %d res=%d\n",
c1770 20 70 46 69 6c 65 2d 3e 68 2c 20 74 79 70 65 2c   pFile->h, type,
c1780 20 6c 6f 63 6b 74 79 70 65 2c 20 72 65 73 20 29   locktype, res )
c1790 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65  ;.  }.  if( type
c17a0 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  >=PENDING_LOCK )
c17b0 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  {.    LockArea.l
c17c0 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20  Offset = 0L;.   
c17d0 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65   LockArea.lRange
c17e0 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
c17f0 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
c1800 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20  PENDING_BYTE;.  
c1810 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
c1820 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65  nge = 1L;.    re
c1830 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f  s = DosSetFileLo
c1840 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26  cks( pFile->h, &
c1850 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
c1860 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45  kArea, LOCK_TIME
c1870 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f  OUT, 0L );.    O
c1880 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b  STRACE3( "UNLOCK
c1890 20 25 64 20 70 65 6e 64 69 6e 67 20 72 65 73 3d   %d pending res=
c18a0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
c18b0 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 70 46   res );.  }.  pF
c18c0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
c18d0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52  locktype;.  OSTR
c18e0 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE3( "UNLOCK %d
c18f0 20 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c   now %d\n", pFil
c1900 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63  e->h, pFile->loc
c1910 6b 74 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72  ktype );.  retur
c1920 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
c1930 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 75 65 72 79  ontrol and query
c1940 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c   of the open fil
c1950 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61  e handle..*/.sta
c1960 74 69 63 20 69 6e 74 20 6f 73 32 46 69 6c 65 43  tic int os2FileC
c1970 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66  ontrol(sqlite3_f
c1980 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c  ile *id, int op,
c1990 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20   void *pArg){.  
c19a0 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20  switch( op ){.  
c19b0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43    case SQLITE_FC
c19c0 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b  NTL_LOCKSTATE: {
c19d0 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41  .      *(int*)pA
c19e0 72 67 20 3d 20 28 28 6f 73 32 46 69 6c 65 2a 29  rg = ((os2File*)
c19f0 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20  id)->locktype;. 
c1a00 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22       OSTRACE3( "
c1a10 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20  FCNTL_LOCKSTATE 
c1a20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28  %d lock=%d\n", (
c1a30 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68  (os2File*)id)->h
c1a40 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29  , ((os2File*)id)
c1a50 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ->locktype );.  
c1a60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
c1a70 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
c1a80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
c1a90 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ERROR;.}../*.** 
c1aa0 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f  Return the secto
c1ab0 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  r size in bytes 
c1ac0 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  of the underlyin
c1ad0 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66  g block device f
c1ae0 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66  or.** the specif
c1af0 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69  ied file. This i
c1b00 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20  s almost always 
c1b10 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d  512 bytes, but m
c1b20 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20  ay be.** larger 
c1b30 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73  for some devices
c1b40 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63  ..**.** SQLite c
c1b50 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73  ode assumes this
c1b60 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74   function cannot
c1b70 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61   fail. It also a
c1b80 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69  ssumes that.** i
c1b90 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20  f two files are 
c1ba0 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73  created in the s
c1bb0 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  ame file-system 
c1bc0 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a  directory (i.e..
c1bd0 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e  ** a database an
c1be0 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69  d its journal fi
c1bf0 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63  le) that the sec
c1c00 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65  tor size will be
c1c10 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72   the.** same for
c1c20 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63   both..*/.static
c1c30 20 69 6e 74 20 6f 73 32 53 65 63 74 6f 72 53 69   int os2SectorSi
c1c40 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ze(sqlite3_file 
c1c50 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 53  *id){.  return S
c1c60 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45  QLITE_DEFAULT_SE
c1c70 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a  CTOR_SIZE;.}../*
c1c80 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 76 65 63  .** Return a vec
c1c90 74 6f 72 20 6f 66 20 64 65 76 69 63 65 20 63 68  tor of device ch
c1ca0 61 72 61 63 74 65 72 69 73 74 69 63 73 2e 0a 2a  aracteristics..*
c1cb0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
c1cc0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
c1cd0 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69  stics(sqlite3_fi
c1ce0 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72  le *id){.  retur
c1cf0 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43  n 0;.}.../*.** C
c1d00 68 61 72 61 63 74 65 72 20 73 65 74 20 63 6f 6e  haracter set con
c1d10 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20  version objects 
c1d20 75 73 65 64 20 62 79 20 63 6f 6e 76 65 72 73 69  used by conversi
c1d30 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  on routines..*/.
c1d40 73 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65  static UconvObje
c1d50 63 74 20 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c  ct ucUtf8 = NULL
c1d60 3b 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65 74  ; /* convert bet
c1d70 77 65 65 6e 20 55 54 46 2d 38 20 61 6e 64 20 55  ween UTF-8 and U
c1d80 43 53 2d 32 20 2a 2f 0a 73 74 61 74 69 63 20 55  CS-2 */.static U
c1d90 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 6c 43 70  convObject uclCp
c1da0 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e   = NULL;  /* con
c1db0 76 65 72 74 20 62 65 74 77 65 65 6e 20 6c 6f 63  vert between loc
c1dc0 61 6c 20 63 6f 64 65 70 61 67 65 20 61 6e 64 20  al codepage and 
c1dd0 55 43 53 2d 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  UCS-2 */../*.** 
c1de0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  Helper function 
c1df0 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  to initialize th
c1e00 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a  e conversion obj
c1e10 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f  ects from and to
c1e20 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69   UTF-8..*/.stati
c1e30 63 20 76 6f 69 64 20 69 6e 69 74 55 63 6f 6e 76  c void initUconv
c1e40 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 20 29 7b  Objects( void ){
c1e50 0a 20 20 69 66 28 20 55 6e 69 43 72 65 61 74 65  .  if( UniCreate
c1e60 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 55 54 46  UconvObject( UTF
c1e70 5f 38 2c 20 26 75 63 55 74 66 38 20 29 20 21 3d  _8, &ucUtf8 ) !=
c1e80 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20   ULS_SUCCESS ). 
c1e90 20 20 20 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c     ucUtf8 = NULL
c1ea0 3b 0a 20 20 69 66 20 28 20 55 6e 69 43 72 65 61  ;.  if ( UniCrea
c1eb0 74 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 28  teUconvObject( (
c1ec0 55 6e 69 43 68 61 72 20 2a 29 4c 22 40 70 61 74  UniChar *)L"@pat
c1ed0 68 3d 79 65 73 22 2c 20 26 75 63 6c 43 70 20 29  h=yes", &uclCp )
c1ee0 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20   != ULS_SUCCESS 
c1ef0 29 0a 20 20 20 20 75 63 6c 43 70 20 3d 20 4e 55  ).    uclCp = NU
c1f00 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c  LL;.}../*.** Hel
c1f10 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  per function to 
c1f20 66 72 65 65 20 74 68 65 20 63 6f 6e 76 65 72 73  free the convers
c1f30 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d  ion objects from
c1f40 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a   and to UTF-8..*
c1f50 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72  /.static void fr
c1f60 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20  eeUconvObjects( 
c1f70 76 6f 69 64 20 29 7b 0a 20 20 69 66 20 28 20 75  void ){.  if ( u
c1f80 63 55 74 66 38 20 29 0a 20 20 20 20 55 6e 69 46  cUtf8 ).    UniF
c1f90 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20  reeUconvObject( 
c1fa0 75 63 55 74 66 38 20 29 3b 0a 20 20 69 66 20 28  ucUtf8 );.  if (
c1fb0 20 75 63 6c 43 70 20 29 0a 20 20 20 20 55 6e 69   uclCp ).    Uni
c1fc0 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28  FreeUconvObject(
c1fd0 20 75 63 6c 43 70 20 29 3b 0a 20 20 75 63 55 74   uclCp );.  ucUt
c1fe0 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c  f8 = NULL;.  ucl
c1ff0 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a  Cp = NULL;.}../*
c2000 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74  .** Helper funct
c2010 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 55  ion to convert U
c2020 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65 73 20 74  TF-8 filenames t
c2030 6f 20 6c 6f 63 61 6c 20 4f 53 2f 32 20 63 6f 64  o local OS/2 cod
c2040 65 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 74 77  epage..** The tw
c2050 6f 2d 73 74 65 70 20 70 72 6f 63 65 73 73 3a 20  o-step process: 
c2060 66 69 72 73 74 20 63 6f 6e 76 65 72 74 20 74 68  first convert th
c2070 65 20 69 6e 63 6f 6d 69 6e 67 20 55 54 46 2d 38  e incoming UTF-8
c2080 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20   string.** into 
c2090 55 43 53 2d 32 20 61 6e 64 20 74 68 65 6e 20 66  UCS-2 and then f
c20a0 72 6f 6d 20 55 43 53 2d 32 20 74 6f 20 74 68 65  rom UCS-2 to the
c20b0 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67   current codepag
c20c0 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  e..** The return
c20d0 65 64 20 63 68 61 72 20 70 6f 69 6e 74 65 72 20  ed char pointer 
c20e0 68 61 73 20 74 6f 20 62 65 20 66 72 65 65 64 2e  has to be freed.
c20f0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
c2100 2a 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68  *convertUtf8Path
c2110 54 6f 43 70 28 20 63 6f 6e 73 74 20 63 68 61 72  ToCp( const char
c2120 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61   *in ){.  UniCha
c2130 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41  r tempPath[CCHMA
c2140 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a  XPATH];.  char *
c2150 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29 63 61  out = (char *)ca
c2160 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 54 48  lloc( CCHMAXPATH
c2170 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f  , 1 );..  if( !o
c2180 75 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20  ut ).    return 
c2190 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63  NULL;..  if( !uc
c21a0 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 70 20 29  Utf8 || !uclCp )
c21b0 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62  .    initUconvOb
c21c0 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 64  jects();..  /* d
c21d0 65 74 65 72 6d 69 6e 65 20 73 74 72 69 6e 67 20  etermine string 
c21e0 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 72 73 69  for the conversi
c21f0 6f 6e 20 6f 66 20 55 54 46 2d 38 20 77 68 69 63  on of UTF-8 whic
c2200 68 20 69 73 20 43 50 31 32 30 38 20 2a 2f 0a 20  h is CP1208 */. 
c2210 20 69 66 28 20 55 6e 69 53 74 72 54 6f 55 63 73   if( UniStrToUcs
c2220 28 20 75 63 55 74 66 38 2c 20 74 65 6d 70 50 61  ( ucUtf8, tempPa
c2230 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c 20  th, (char *)in, 
c2240 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 3d 20  CCHMAXPATH ) != 
c2250 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20  ULS_SUCCESS ).  
c2260 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a    return out; /*
c2270 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66   if conversion f
c2280 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 65  ails, return the
c2290 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 2a 2f   empty string */
c22a0 0a 0a 20 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f  ..  /* conversio
c22b0 6e 20 66 6f 72 20 63 75 72 72 65 6e 74 20 63 6f  n for current co
c22c0 64 65 70 61 67 65 20 77 68 69 63 68 20 63 61 6e  depage which can
c22d0 20 62 65 20 75 73 65 64 20 66 6f 72 20 70 61 74   be used for pat
c22e0 68 73 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72  hs */.  UniStrFr
c22f0 6f 6d 55 63 73 28 20 75 63 6c 43 70 2c 20 6f 75  omUcs( uclCp, ou
c2300 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48  t, tempPath, CCH
c2310 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 20 72 65  MAXPATH );..  re
c2320 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a  turn out;.}../*.
c2330 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69  ** Helper functi
c2340 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 66 69  on to convert fi
c2350 6c 65 6e 61 6d 65 73 20 66 72 6f 6d 20 6c 6f 63  lenames from loc
c2360 61 6c 20 63 6f 64 65 70 61 67 65 20 74 6f 20 55  al codepage to U
c2370 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 74 77 6f  TF-8..** The two
c2380 2d 73 74 65 70 20 70 72 6f 63 65 73 73 3a 20 66  -step process: f
c2390 69 72 73 74 20 63 6f 6e 76 65 72 74 20 74 68 65  irst convert the
c23a0 20 69 6e 63 6f 6d 69 6e 67 20 63 6f 64 65 70 61   incoming codepa
c23b0 67 65 2d 73 70 65 63 69 66 69 63 0a 2a 2a 20 73  ge-specific.** s
c23c0 74 72 69 6e 67 20 69 6e 74 6f 20 55 43 53 2d 32  tring into UCS-2
c23d0 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 55   and then from U
c23e0 43 53 2d 32 20 74 6f 20 74 68 65 20 63 6f 64 65  CS-2 to the code
c23f0 70 61 67 65 20 6f 66 20 55 54 46 2d 38 2e 0a 2a  page of UTF-8..*
c2400 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 63  * The returned c
c2410 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 20  har pointer has 
c2420 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a 0a  to be freed..**.
c2430 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
c2440 20 69 73 20 6e 6f 6e 2d 73 74 61 74 69 63 20 74   is non-static t
c2450 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 75 73 65  o be able to use
c2460 20 74 68 69 73 20 69 6e 20 73 68 65 6c 6c 2e 63   this in shell.c
c2470 20 61 6e 64 0a 2a 2a 20 73 69 6d 69 6c 61 72 20   and.** similar 
c2480 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  applications tha
c2490 74 20 74 61 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c  t take command l
c24a0 69 6e 65 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a  ine arguments..*
c24b0 2f 0a 63 68 61 72 20 2a 63 6f 6e 76 65 72 74 43  /.char *convertC
c24c0 70 50 61 74 68 54 6f 55 74 66 38 28 20 63 6f 6e  pPathToUtf8( con
c24d0 73 74 20 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20  st char *in ){. 
c24e0 20 55 6e 69 43 68 61 72 20 74 65 6d 70 50 61 74   UniChar tempPat
c24f0 68 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20  h[CCHMAXPATH];. 
c2500 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 28 63 68   char *out = (ch
c2510 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48  ar *)calloc( CCH
c2520 4d 41 58 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20  MAXPATH, 1 );.. 
c2530 20 69 66 28 20 21 6f 75 74 20 29 0a 20 20 20 20   if( !out ).    
c2540 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20  return NULL;..  
c2550 69 66 28 20 21 75 63 55 74 66 38 20 7c 7c 20 21  if( !ucUtf8 || !
c2560 75 63 6c 43 70 20 29 0a 20 20 20 20 69 6e 69 74  uclCp ).    init
c2570 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a  UconvObjects();.
c2580 0a 20 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e  .  /* conversion
c2590 20 66 6f 72 20 63 75 72 72 65 6e 74 20 63 6f 64   for current cod
c25a0 65 70 61 67 65 20 77 68 69 63 68 20 63 61 6e 20  epage which can 
c25b0 62 65 20 75 73 65 64 20 66 6f 72 20 70 61 74 68  be used for path
c25c0 73 20 2a 2f 0a 20 20 69 66 28 20 55 6e 69 53 74  s */.  if( UniSt
c25d0 72 54 6f 55 63 73 28 20 75 63 6c 43 70 2c 20 74  rToUcs( uclCp, t
c25e0 65 6d 70 50 61 74 68 2c 20 28 63 68 61 72 20 2a  empPath, (char *
c25f0 29 69 6e 2c 20 43 43 48 4d 41 58 50 41 54 48 20  )in, CCHMAXPATH 
c2600 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53  ) != ULS_SUCCESS
c2610 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 75   ).    return ou
c2620 74 3b 20 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73  t; /* if convers
c2630 69 6f 6e 20 66 61 69 6c 73 2c 20 72 65 74 75 72  ion fails, retur
c2640 6e 20 74 68 65 20 65 6d 70 74 79 20 73 74 72 69  n the empty stri
c2650 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 64 65 74 65  ng */..  /* dete
c2660 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72  rmine string for
c2670 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
c2680 6f 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69  of UTF-8 which i
c2690 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 55 6e  s CP1208 */.  Un
c26a0 69 53 74 72 46 72 6f 6d 55 63 73 28 20 75 63 55  iStrFromUcs( ucU
c26b0 74 66 38 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61  tf8, out, tempPa
c26c0 74 68 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29  th, CCHMAXPATH )
c26d0 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 3b  ;..  return out;
c26e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76  .}../*.** This v
c26f0 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c  ector defines al
c2700 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68  l the methods th
c2710 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f  at can operate o
c2720 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  n an.** sqlite3_
c2730 66 69 6c 65 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f  file for os2..*/
c2740 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
c2750 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
c2760 20 6f 73 32 49 6f 4d 65 74 68 6f 64 20 3d 20 7b   os2IoMethod = {
c2770 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20  .  1,           
c2780 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c2790 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73  iVersion */.  os
c27a0 32 43 6c 6f 73 65 2c 0a 20 20 6f 73 32 52 65 61  2Close,.  os2Rea
c27b0 64 2c 0a 20 20 6f 73 32 57 72 69 74 65 2c 0a 20  d,.  os2Write,. 
c27c0 20 6f 73 32 54 72 75 6e 63 61 74 65 2c 0a 20 20   os2Truncate,.  
c27d0 6f 73 32 53 79 6e 63 2c 0a 20 20 6f 73 32 46 69  os2Sync,.  os2Fi
c27e0 6c 65 53 69 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63  leSize,.  os2Loc
c27f0 6b 2c 0a 20 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a  k,.  os2Unlock,.
c2800 20 20 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76    os2CheckReserv
c2810 65 64 4c 6f 63 6b 2c 0a 20 20 6f 73 32 46 69 6c  edLock,.  os2Fil
c2820 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 6f 73 32 53  eControl,.  os2S
c2830 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 6f 73 32  ectorSize,.  os2
c2840 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
c2850 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a  stics.};../*****
c2860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c28a0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20 65  ******.** Here e
c28b0 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d 65 74 68  nds the I/O meth
c28c0 6f 64 73 20 74 68 61 74 20 66 6f 72 6d 20 74 68  ods that form th
c28d0 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  e sqlite3_io_met
c28e0 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a  hods object..**.
c28f0 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 6c 6f 63  ** The next bloc
c2900 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d  k of code implem
c2910 65 6e 74 73 20 74 68 65 20 56 46 53 20 6d 65 74  ents the VFS met
c2920 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  hods..**********
c2930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2f 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  **/../*.** Creat
c2980 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  e a temporary fi
c2990 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e  le name in zBuf.
c29a0 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62    zBuf must be b
c29b0 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20  ig enough to.** 
c29c0 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d 78  hold at pVfs->mx
c29d0 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 63 74  Pathname charact
c29e0 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ers..*/.static i
c29f0 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 69  nt getTempname(i
c2a00 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
c2a10 42 75 66 20 29 7b 0a 20 20 73 74 61 74 69 63 20  Buf ){.  static 
c2a20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
c2a30 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20  har zChars[] =. 
c2a40 20 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c     "abcdefghijkl
c2a50 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0a  mnopqrstuvwxyz".
c2a60 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b      "ABCDEFGHIJK
c2a70 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22  LMNOPQRSTUVWXYZ"
c2a80 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38 39  .    "0123456789
c2a90 22 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  ";.  int i, j;. 
c2aa0 20 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68 42   char zTempPathB
c2ab0 75 66 5b 33 5d 3b 0a 20 20 50 53 5a 20 7a 54 65  uf[3];.  PSZ zTe
c2ac0 6d 70 50 61 74 68 20 3d 20 28 50 53 5a 29 26 7a  mpPath = (PSZ)&z
c2ad0 54 65 6d 70 50 61 74 68 42 75 66 3b 0a 20 20 69  TempPathBuf;.  i
c2ae0 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  f( sqlite3_temp_
c2af0 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20  directory ){.   
c2b00 20 7a 54 65 6d 70 50 61 74 68 20 3d 20 73 71 6c   zTempPath = sql
c2b10 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
c2b20 6f 72 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ory;.  }else{.  
c2b30 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76    if( DosScanEnv
c2b40 28 20 28 50 53 5a 29 22 54 45 4d 50 22 2c 20 26  ( (PSZ)"TEMP", &
c2b50 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20  zTempPath ) ){. 
c2b60 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e       if( DosScan
c2b70 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 22 2c  Env( (PSZ)"TMP",
c2b80 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b   &zTempPath ) ){
c2b90 0a 20 20 20 20 20 20 20 20 69 66 28 20 44 6f 73  .        if( Dos
c2ba0 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 22 54  ScanEnv( (PSZ)"T
c2bb0 4d 50 44 49 52 22 2c 20 26 7a 54 65 6d 70 50 61  MPDIR", &zTempPa
c2bc0 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 20 20  th ) ){.        
c2bd0 20 20 20 55 4c 4f 4e 47 20 75 6c 44 72 69 76 65     ULONG ulDrive
c2be0 4e 75 6d 20 3d 20 30 2c 20 75 6c 44 72 69 76 65  Num = 0, ulDrive
c2bf0 4d 61 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  Map = 0;.       
c2c00 20 20 20 20 44 6f 73 51 75 65 72 79 43 75 72 72      DosQueryCurr
c2c10 65 6e 74 44 69 73 6b 28 20 26 75 6c 44 72 69 76  entDisk( &ulDriv
c2c20 65 4e 75 6d 2c 20 26 75 6c 44 72 69 76 65 4d 61  eNum, &ulDriveMa
c2c30 70 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  p );.           
c2c40 73 70 72 69 6e 74 66 28 20 28 63 68 61 72 2a 29  sprintf( (char*)
c2c50 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 63 3a 22  zTempPath, "%c:"
c2c60 2c 20 28 63 68 61 72 29 28 20 27 41 27 20 2b 20  , (char)( 'A' + 
c2c70 75 6c 44 72 69 76 65 4e 75 6d 20 2d 20 31 20 29  ulDriveNum - 1 )
c2c80 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   );.        }.  
c2c90 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
c2ca0 20 20 2f 2a 20 53 74 72 69 70 20 6f 66 66 20 61    /* Strip off a
c2cb0 20 74 72 61 69 6c 69 6e 67 20 73 6c 61 73 68 65   trailing slashe
c2cc0 73 20 6f 72 20 62 61 63 6b 73 6c 61 73 68 65 73  s or backslashes
c2cd0 2c 20 6f 74 68 65 72 77 69 73 65 20 77 65 20 77  , otherwise we w
c2ce0 6f 75 6c 64 20 67 65 74 20 2a 0a 20 20 20 2a 20  ould get *.   * 
c2cf0 6d 75 6c 74 69 70 6c 65 20 28 62 61 63 6b 29 73  multiple (back)s
c2d00 6c 61 73 68 65 73 20 77 68 69 63 68 20 63 61 75  lashes which cau
c2d10 73 65 73 20 44 6f 73 4f 70 65 6e 28 29 20 74 6f  ses DosOpen() to
c2d20 20 66 61 69 6c 2e 20 20 20 20 20 20 20 20 20 20   fail.          
c2d30 20 20 20 20 2a 0a 20 20 20 2a 20 54 72 61 69 6c      *.   * Trail
c2d40 69 6e 67 20 73 70 61 63 65 73 20 61 72 65 20 6e  ing spaces are n
c2d50 6f 74 20 61 6c 6c 6f 77 65 64 2c 20 65 69 74 68  ot allowed, eith
c2d60 65 72 2e 20 20 20 20 20 20 20 20 20 20 20 20 20  er.             
c2d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
c2d80 2f 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53  /.  j = sqlite3S
c2d90 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 74  trlen30(zTempPat
c2da0 68 29 3b 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e  h);.  while( j >
c2db0 20 30 20 26 26 20 28 20 7a 54 65 6d 70 50 61 74   0 && ( zTempPat
c2dc0 68 5b 6a 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c  h[j-1] == '\\' |
c2dd0 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d  | zTempPath[j-1]
c2de0 20 3d 3d 20 27 2f 27 0a 20 20 20 20 20 20 20 20   == '/'.        
c2df0 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 7a              || z
c2e00 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d  TempPath[j-1] ==
c2e10 20 27 20 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d   ' ' ) ){.    j-
c2e20 2d 3b 0a 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61  -;.  }.  zTempPa
c2e30 74 68 5b 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  th[j] = '\0';.  
c2e40 69 66 28 20 21 73 71 6c 69 74 65 33 5f 74 65 6d  if( !sqlite3_tem
c2e50 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20  p_directory ){. 
c2e60 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 50 61     char *zTempPa
c2e70 74 68 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43  thUTF = convertC
c2e80 70 50 61 74 68 54 6f 55 74 66 38 28 20 7a 54 65  pPathToUtf8( zTe
c2e90 6d 70 50 61 74 68 20 29 3b 0a 20 20 20 20 73 71  mpPath );.    sq
c2ea0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20  lite3_snprintf( 
c2eb0 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20  nBuf-30, zBuf,. 
c2ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2ed0 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54       "%s\\"SQLIT
c2ee0 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
c2ef0 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 55 54 46  IX, zTempPathUTF
c2f00 20 29 3b 0a 20 20 20 20 66 72 65 65 28 20 7a 54   );.    free( zT
c2f10 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a 20 20  empPathUTF );.  
c2f20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
c2f30 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75  e3_snprintf( nBu
c2f40 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20  f-30, zBuf,.    
c2f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2f60 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54    "%s\\"SQLITE_T
c2f70 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c  EMP_FILE_PREFIX,
c2f80 20 7a 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20   zTempPath );.  
c2f90 7d 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53  }.  j = sqlite3S
c2fa0 74 72 6c 65 6e 33 30 28 20 7a 42 75 66 20 29 3b  trlen30( zBuf );
c2fb0 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f  .  sqlite3_rando
c2fc0 6d 6e 65 73 73 28 20 32 30 2c 20 26 7a 42 75 66  mness( 20, &zBuf
c2fd0 5b 6a 5d 20 29 3b 0a 20 20 66 6f 72 28 20 69 20  [j] );.  for( i 
c2fe0 3d 20 30 3b 20 69 20 3c 20 32 30 3b 20 69 2b 2b  = 0; i < 20; i++
c2ff0 2c 20 6a 2b 2b 20 29 7b 0a 20 20 20 20 7a 42 75  , j++ ){.    zBu
c3000 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68  f[j] = (char)zCh
c3010 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20  ars[ ((unsigned 
c3020 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73  char)zBuf[j])%(s
c3030 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29  izeof(zChars)-1)
c3040 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a   ];.  }.  zBuf[j
c3050 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45  ] = 0;.  OSTRACE
c3060 32 28 20 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d  2( "TEMP FILENAM
c3070 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 20 29  E: %s\n", zBuf )
c3080 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
c3090 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  E_OK;.}.../*.** 
c30a0 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20  Turn a relative 
c30b0 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20  pathname into a 
c30c0 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20  full pathname.  
c30d0 57 72 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a  Write the full.*
c30e0 2a 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20  * pathname into 
c30f0 7a 46 75 6c 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b  zFull[].  zFull[
c3100 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 6c 65 61  ] will be at lea
c3110 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  st pVfs->mxPathn
c3120 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20  ame.** bytes in 
c3130 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  size..*/.static 
c3140 69 6e 74 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e  int os2FullPathn
c3150 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ame(.  sqlite3_v
c3160 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20  fs *pVfs,       
c3170 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
c3180 20 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20   vfs object */. 
c3190 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65   const char *zRe
c31a0 6c 61 74 69 76 65 2c 20 20 20 20 20 20 2f 2a 20  lative,      /* 
c31b0 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76  Possibly relativ
c31c0 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a  e input path */.
c31d0 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20    int nFull,    
c31e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c31f0 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20   Size of output 
c3200 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20  buffer in bytes 
c3210 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c  */.  char *zFull
c3220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3230 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65   /* Output buffe
c3240 72 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a  r */.){.  char *
c3250 7a 52 65 6c 61 74 69 76 65 43 70 20 3d 20 63 6f  zRelativeCp = co
c3260 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43  nvertUtf8PathToC
c3270 70 28 20 7a 52 65 6c 61 74 69 76 65 20 29 3b 0a  p( zRelative );.
c3280 20 20 63 68 61 72 20 7a 46 75 6c 6c 43 70 5b 43    char zFullCp[C
c3290 43 48 4d 41 58 50 41 54 48 5d 20 3d 20 22 5c 30  CHMAXPATH] = "\0
c32a0 22 3b 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c  ";.  char *zFull
c32b0 55 54 46 3b 0a 20 20 41 50 49 52 45 54 20 72 63  UTF;.  APIRET rc
c32c0 20 3d 20 44 6f 73 51 75 65 72 79 50 61 74 68 49   = DosQueryPathI
c32d0 6e 66 6f 28 20 7a 52 65 6c 61 74 69 76 65 43 70  nfo( zRelativeCp
c32e0 2c 20 46 49 4c 5f 51 55 45 52 59 46 55 4c 4c 4e  , FIL_QUERYFULLN
c32f0 41 4d 45 2c 20 7a 46 75 6c 6c 43 70 2c 0a 20 20  AME, zFullCp,.  
c3300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 43 43                CC
c3320 48 4d 41 58 50 41 54 48 20 29 3b 0a 20 20 66 72  HMAXPATH );.  fr
c3330 65 65 28 20 7a 52 65 6c 61 74 69 76 65 43 70 20  ee( zRelativeCp 
c3340 29 3b 0a 20 20 7a 46 75 6c 6c 55 54 46 20 3d 20  );.  zFullUTF = 
c3350 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f 55  convertCpPathToU
c3360 74 66 38 28 20 7a 46 75 6c 6c 43 70 20 29 3b 0a  tf8( zFullCp );.
c3370 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
c3380 74 66 28 20 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c  tf( nFull, zFull
c3390 2c 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20  , zFullUTF );.  
c33a0 66 72 65 65 28 20 7a 46 75 6c 6c 55 54 46 20 29  free( zFullUTF )
c33b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d  ;.  return rc ==
c33c0 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49   NO_ERROR ? SQLI
c33d0 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49  TE_OK : SQLITE_I
c33e0 4f 45 52 52 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  OERR;.}.../*.** 
c33f0 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a  Open a file..*/.
c3400 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4f 70  static int os2Op
c3410 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  en(.  sqlite3_vf
c3420 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
c3430 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20      /* Not used 
c3440 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
c3450 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
c3460 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
c3470 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69  e file */.  sqli
c3480 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20  te3_file *id,   
c3490 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
c34a0 74 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69  te the SQLite fi
c34b0 6c 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a  le handle here *
c34c0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
c34d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c34e0 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66    /* Open mode f
c34f0 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  lags */.  int *p
c3500 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 20 20  OutFlags        
c3510 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75          /* Statu
c3520 73 20 72 65 74 75 72 6e 20 66 6c 61 67 73 20 2a  s return flags *
c3530 2f 0a 29 7b 0a 20 20 48 46 49 4c 45 20 68 3b 0a  /.){.  HFILE h;.
c3540 20 20 55 4c 4f 4e 47 20 75 6c 46 69 6c 65 41 74    ULONG ulFileAt
c3550 74 72 69 62 75 74 65 20 3d 20 46 49 4c 45 5f 4e  tribute = FILE_N
c3560 4f 52 4d 41 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75  ORMAL;.  ULONG u
c3570 6c 4f 70 65 6e 46 6c 61 67 73 20 3d 20 30 3b 0a  lOpenFlags = 0;.
c3580 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f    ULONG ulOpenMo
c3590 64 65 20 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c  de = 0;.  os2Fil
c35a0 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46  e *pFile = (os2F
c35b0 69 6c 65 2a 29 69 64 3b 0a 20 20 41 50 49 52 45  ile*)id;.  APIRE
c35c0 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b  T rc = NO_ERROR;
c35d0 0a 20 20 55 4c 4f 4e 47 20 75 6c 41 63 74 69 6f  .  ULONG ulActio
c35e0 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  n;.  char *zName
c35f0 43 70 3b 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e  Cp;.  char zTmpn
c3600 61 6d 65 5b 43 43 48 4d 41 58 50 41 54 48 2b 31  ame[CCHMAXPATH+1
c3610 5d 3b 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20  ];    /* Buffer 
c3620 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 20 6f 66 20  to hold name of 
c3630 74 65 6d 70 20 66 69 6c 65 20 2a 2f 0a 0a 20 20  temp file */..  
c3640 2f 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64  /* If the second
c3650 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
c3660 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55  s function is NU
c3670 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20 61 20 0a  LL, generate a .
c3680 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66    ** temporary f
c3690 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75 73 65 20  ile name to use 
c36a0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 4e 61  .  */.  if( !zNa
c36b0 6d 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  me ){.    int rc
c36c0 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 43   = getTempname(C
c36d0 43 48 4d 41 58 50 41 54 48 2b 31 2c 20 7a 54 6d  CHMAXPATH+1, zTm
c36e0 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  pname);.    if( 
c36f0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
c3700 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
c3710 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d  ;.    }.    zNam
c3720 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20  e = zTmpname;.  
c3730 7d 0a 0a 0a 20 20 6d 65 6d 73 65 74 28 20 70 46  }...  memset( pF
c3740 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  ile, 0, sizeof(*
c3750 70 46 69 6c 65 29 20 29 3b 0a 0a 20 20 4f 53 54  pFile) );..  OST
c3760 52 41 43 45 32 28 20 22 4f 50 45 4e 20 77 61 6e  RACE2( "OPEN wan
c3770 74 20 25 64 5c 6e 22 2c 20 66 6c 61 67 73 20 29  t %d\n", flags )
c3780 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  ;..  if( flags &
c3790 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
c37a0 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 75 6c  DWRITE ){.    ul
c37b0 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e  OpenMode |= OPEN
c37c0 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
c37d0 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28  E;.    OSTRACE1(
c37e0 20 22 4f 50 45 4e 20 72 65 61 64 2f 77 72 69 74   "OPEN read/writ
c37f0 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  e\n" );.  }else{
c3800 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20  .    ulOpenMode 
c3810 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52  |= OPEN_ACCESS_R
c3820 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 4f 53 54  EADONLY;.    OST
c3830 52 41 43 45 31 28 20 22 4f 50 45 4e 20 72 65 61  RACE1( "OPEN rea
c3840 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d  d only\n" );.  }
c3850 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ..  if( flags & 
c3860 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
c3870 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e  TE ){.    ulOpen
c3880 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 43  Flags |= OPEN_AC
c3890 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49  TION_OPEN_IF_EXI
c38a0 53 54 53 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f  STS | OPEN_ACTIO
c38b0 4e 5f 43 52 45 41 54 45 5f 49 46 5f 4e 45 57 3b  N_CREATE_IF_NEW;
c38c0 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22  .    OSTRACE1( "
c38d0 4f 50 45 4e 20 6f 70 65 6e 20 6e 65 77 2f 63 72  OPEN open new/cr
c38e0 65 61 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c  eate\n" );.  }el
c38f0 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c  se{.    ulOpenFl
c3900 61 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49  ags |= OPEN_ACTI
c3910 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54  ON_OPEN_IF_EXIST
c3920 53 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f  S | OPEN_ACTION_
c3930 46 41 49 4c 5f 49 46 5f 4e 45 57 3b 0a 20 20 20  FAIL_IF_NEW;.   
c3940 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e   OSTRACE1( "OPEN
c3950 20 6f 70 65 6e 20 65 78 69 73 74 69 6e 67 5c 6e   open existing\n
c3960 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  " );.  }..  if( 
c3970 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
c3980 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20  PEN_MAIN_DB ){. 
c3990 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d     ulOpenMode |=
c39a0 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59   OPEN_SHARE_DENY
c39b0 4e 4f 4e 45 3b 0a 20 20 20 20 4f 53 54 52 41 43  NONE;.    OSTRAC
c39c0 45 31 28 20 22 4f 50 45 4e 20 73 68 61 72 65 20  E1( "OPEN share 
c39d0 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b  read/write\n" );
c39e0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c  .  }else{.    ul
c39f0 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e  OpenMode |= OPEN
c3a00 5f 53 48 41 52 45 5f 44 45 4e 59 57 52 49 54 45  _SHARE_DENYWRITE
c3a10 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20  ;.    OSTRACE1( 
c3a20 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 64  "OPEN share read
c3a30 20 6f 6e 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a   only\n" );.  }.
c3a40 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  .  if( flags & S
c3a50 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
c3a60 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 20 20 20 20  EONCLOSE ){.    
c3a70 63 68 61 72 20 70 61 74 68 55 74 66 38 5b 43 43  char pathUtf8[CC
c3a80 48 4d 41 58 50 41 54 48 5d 3b 0a 23 69 66 64 65  HMAXPATH];.#ifde
c3a90 66 20 4e 44 45 42 55 47 20 2f 2a 20 77 68 65 6e  f NDEBUG /* when
c3aa0 20 64 65 62 75 67 67 69 6e 67 20 77 65 20 77 61   debugging we wa
c3ab0 6e 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  nt to make sure 
c3ac0 69 74 20 69 73 20 64 65 6c 65 74 65 64 20 2a 2f  it is deleted */
c3ad0 0a 20 20 20 20 75 6c 46 69 6c 65 41 74 74 72 69  .    ulFileAttri
c3ae0 62 75 74 65 20 3d 20 46 49 4c 45 5f 48 49 44 44  bute = FILE_HIDD
c3af0 45 4e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 6f  EN;.#endif.    o
c3b00 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 20  s2FullPathname( 
c3b10 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 43 43 48  pVfs, zName, CCH
c3b20 4d 41 58 50 41 54 48 2c 20 70 61 74 68 55 74 66  MAXPATH, pathUtf
c3b30 38 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  8 );.    pFile->
c3b40 70 61 74 68 54 6f 44 65 6c 20 3d 20 63 6f 6e 76  pathToDel = conv
c3b50 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28  ertUtf8PathToCp(
c3b60 20 70 61 74 68 55 74 66 38 20 29 3b 0a 20 20 20   pathUtf8 );.   
c3b70 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e   OSTRACE1( "OPEN
c3b80 20 68 69 64 64 65 6e 2f 64 65 6c 65 74 65 20 6f   hidden/delete o
c3b90 6e 20 63 6c 6f 73 65 20 66 69 6c 65 20 61 74 74  n close file att
c3ba0 72 69 62 75 74 65 73 5c 6e 22 20 29 3b 0a 20 20  ributes\n" );.  
c3bb0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c 65  }else{.    pFile
c3bc0 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55  ->pathToDel = NU
c3bd0 4c 4c 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31  LL;.    OSTRACE1
c3be0 28 20 22 4f 50 45 4e 20 6e 6f 72 6d 61 6c 20 66  ( "OPEN normal f
c3bf0 69 6c 65 20 61 74 74 72 69 62 75 74 65 5c 6e 22  ile attribute\n"
c3c00 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c   );.  }..  /* al
c3c10 77 61 79 73 20 6f 70 65 6e 20 69 6e 20 72 61 6e  ways open in ran
c3c20 64 6f 6d 20 61 63 63 65 73 73 20 6d 6f 64 65 20  dom access mode 
c3c30 66 6f 72 20 70 6f 73 73 69 62 6c 79 20 62 65 74  for possibly bet
c3c40 74 65 72 20 73 70 65 65 64 20 2a 2f 0a 20 20 75  ter speed */.  u
c3c50 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45  lOpenMode |= OPE
c3c60 4e 5f 46 4c 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a  N_FLAGS_RANDOM;.
c3c70 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20    ulOpenMode |= 
c3c80 4f 50 45 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f  OPEN_FLAGS_FAIL_
c3c90 4f 4e 5f 45 52 52 4f 52 3b 0a 20 20 75 6c 4f 70  ON_ERROR;.  ulOp
c3ca0 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46  enMode |= OPEN_F
c3cb0 4c 41 47 53 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a  LAGS_NOINHERIT;.
c3cc0 0a 20 20 7a 4e 61 6d 65 43 70 20 3d 20 63 6f 6e  .  zNameCp = con
c3cd0 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70  vertUtf8PathToCp
c3ce0 28 20 7a 4e 61 6d 65 20 29 3b 0a 20 20 72 63 20  ( zName );.  rc 
c3cf0 3d 20 44 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29  = DosOpen( (PSZ)
c3d00 7a 4e 61 6d 65 43 70 2c 0a 20 20 20 20 20 20 20  zNameCp,.       
c3d10 20 20 20 20 20 20 20 20 20 26 68 2c 0a 20 20 20           &h,.   
c3d20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 75 6c               &ul
c3d30 41 63 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 20  Action,.        
c3d40 20 20 20 20 20 20 20 20 30 4c 2c 0a 20 20 20 20          0L,.    
c3d50 20 20 20 20 20 20 20 20 20 20 20 20 75 6c 46 69              ulFi
c3d60 6c 65 41 74 74 72 69 62 75 74 65 2c 0a 20 20 20  leAttribute,.   
c3d70 20 20 20 20 20 20 20 20 20 20 20 20 20 75 6c 4f               ulO
c3d80 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20  penFlags,.      
c3d90 20 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e            ulOpen
c3da0 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Mode,.          
c3db0 20 20 20 20 20 20 28 50 45 41 4f 50 32 29 4e 55        (PEAOP2)NU
c3dc0 4c 4c 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 4e  LL );.  free( zN
c3dd0 61 6d 65 43 70 20 29 3b 0a 20 20 69 66 28 20 72  ameCp );.  if( r
c3de0 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  c != NO_ERROR ){
c3df0 0a 20 20 20 20 4f 53 54 52 41 43 45 37 28 20 22  .    OSTRACE7( "
c3e00 4f 50 45 4e 20 49 6e 76 61 6c 69 64 20 68 61 6e  OPEN Invalid han
c3e10 64 6c 65 20 72 63 3d 25 64 3a 20 7a 4e 61 6d 65  dle rc=%d: zName
c3e20 3d 25 73 2c 20 75 6c 41 63 74 69 6f 6e 3d 25 23  =%s, ulAction=%#
c3e30 6c 78 2c 20 75 6c 41 74 74 72 3d 25 23 6c 78 2c  lx, ulAttr=%#lx,
c3e40 20 75 6c 46 6c 61 67 73 3d 25 23 6c 78 2c 20 75   ulFlags=%#lx, u
c3e50 6c 4d 6f 64 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20  lMode=%#lx\n",. 
c3e60 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 2c               rc,
c3e70 20 7a 4e 61 6d 65 2c 20 75 6c 41 63 74 69 6f 6e   zName, ulAction
c3e80 2c 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74  , ulFileAttribut
c3e90 65 2c 20 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 20  e, ulOpenFlags, 
c3ea0 75 6c 4f 70 65 6e 4d 6f 64 65 20 29 3b 0a 20 20  ulOpenMode );.  
c3eb0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 61 74    if( pFile->pat
c3ec0 68 54 6f 44 65 6c 20 29 0a 20 20 20 20 20 20 66  hToDel ).      f
c3ed0 72 65 65 28 20 70 46 69 6c 65 2d 3e 70 61 74 68  ree( pFile->path
c3ee0 54 6f 44 65 6c 20 29 3b 0a 20 20 20 20 70 46 69  ToDel );.    pFi
c3ef0 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20  le->pathToDel = 
c3f00 4e 55 4c 4c 3b 0a 20 20 20 20 69 66 28 20 66 6c  NULL;.    if( fl
c3f10 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
c3f20 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20  N_READWRITE ){. 
c3f30 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22       OSTRACE2( "
c3f40 4f 50 45 4e 20 25 64 20 49 6e 76 61 6c 69 64 20  OPEN %d Invalid 
c3f50 68 61 6e 64 6c 65 5c 6e 22 2c 20 28 28 66 6c 61  handle\n", ((fla
c3f60 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs | SQLITE_OPEN
c3f70 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51  _READONLY) & ~SQ
c3f80 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
c3f90 49 54 45 29 20 29 3b 0a 20 20 20 20 20 20 72 65  ITE) );.      re
c3fa0 74 75 72 6e 20 6f 73 32 4f 70 65 6e 28 20 70 56  turn os2Open( pV
c3fb0 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20  fs, zName, id,. 
c3fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3fd0 20 20 20 20 20 28 28 66 6c 61 67 73 20 7c 20 53       ((flags | S
c3fe0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
c3ff0 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f  NLY) & ~SQLITE_O
c4000 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 2c 0a  PEN_READWRITE),.
c4010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4020 20 20 20 20 20 20 70 4f 75 74 46 6c 61 67 73 20        pOutFlags 
c4030 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
c4040 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
c4050 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20  E_CANTOPEN;.    
c4060 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 75  }.  }..  if( pOu
c4070 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70  tFlags ){.    *p
c4080 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73  OutFlags = flags
c4090 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
c40a0 45 41 44 57 52 49 54 45 20 3f 20 53 51 4c 49 54  EADWRITE ? SQLIT
c40b0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
c40c0 20 3a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   : SQLITE_OPEN_R
c40d0 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20  EADONLY;.  }..  
c40e0 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d  pFile->pMethod =
c40f0 20 26 6f 73 32 49 6f 4d 65 74 68 6f 64 3b 0a 20   &os2IoMethod;. 
c4100 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20   pFile->h = h;. 
c4110 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29   OpenCounter(+1)
c4120 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 4f  ;.  OSTRACE3( "O
c4130 50 45 4e 20 25 64 20 70 4f 75 74 46 6c 61 67 73  PEN %d pOutFlags
c4140 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
c4150 2c 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20  , pOutFlags );. 
c4160 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c4170 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  K;.}../*.** Dele
c4180 74 65 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c  te the named fil
c4190 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
c41a0 20 6f 73 32 44 65 6c 65 74 65 28 0a 20 20 73 71   os2Delete(.  sq
c41b0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
c41c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c41d0 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64       /* Not used
c41e0 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e   on os2 */.  con
c41f0 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
c4200 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  me,             
c4210 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66      /* Name of f
c4220 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  ile to delete */
c4230 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72 20 20  .  int syncDir  
c4240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4250 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74            /* Not
c4260 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a   used on os2 */.
c4270 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  ){.  APIRET rc =
c4280 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61   NO_ERROR;.  cha
c4290 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d  r *zFilenameCp =
c42a0 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68   convertUtf8Path
c42b0 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20  ToCp( zFilename 
c42c0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
c42d0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
c42e0 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45  ITE_IOERR_DELETE
c42f0 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 44 65   );.  rc = DosDe
c4300 6c 65 74 65 28 20 28 50 53 5a 29 7a 46 69 6c 65  lete( (PSZ)zFile
c4310 6e 61 6d 65 43 70 20 29 3b 0a 20 20 66 72 65 65  nameCp );.  free
c4320 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b  ( zFilenameCp );
c4330 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 44 45  .  OSTRACE2( "DE
c4340 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20  LETE \"%s\"\n", 
c4350 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 72  zFilename );.  r
c4360 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45  eturn rc == NO_E
c4370 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  RROR ? SQLITE_OK
c4380 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   : SQLITE_IOERR_
c4390 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  DELETE;.}../*.**
c43a0 20 43 68 65 63 6b 20 74 68 65 20 65 78 69 73 74   Check the exist
c43b0 61 6e 63 65 20 61 6e 64 20 73 74 61 74 75 73 20  ance and status 
c43c0 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  of a file..*/.st
c43d0 61 74 69 63 20 69 6e 74 20 6f 73 32 41 63 63 65  atic int os2Acce
c43e0 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  ss(.  sqlite3_vf
c43f0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
c4400 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f  /* Not used on o
c4410 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  s2 */.  const ch
c4420 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
c4430 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c    /* Name of fil
c4440 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20  e to check */.  
c4450 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
c4460 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70            /* Typ
c4470 65 20 6f 66 20 74 65 73 74 20 74 6f 20 6d 61 6b  e of test to mak
c4480 65 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a  e on this file *
c4490 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 20 20 20  /.  int *pOut   
c44a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c44b0 20 57 72 69 74 65 20 72 65 73 75 6c 74 73 20 68   Write results h
c44c0 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45  ere */.){.  FILE
c44d0 53 54 41 54 55 53 33 20 66 73 74 73 33 43 6f 6e  STATUS3 fsts3Con
c44e0 66 69 67 49 6e 66 6f 3b 0a 20 20 41 50 49 52 45  figInfo;.  APIRE
c44f0 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b  T rc = NO_ERROR;
c4500 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  .  char *zFilena
c4510 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74  meCp = convertUt
c4520 66 38 50 61 74 68 54 6f 43 70 28 20 7a 46 69 6c  f8PathToCp( zFil
c4530 65 6e 61 6d 65 20 29 3b 0a 0a 20 20 6d 65 6d 73  ename );..  mems
c4540 65 74 28 20 26 66 73 74 73 33 43 6f 6e 66 69 67  et( &fsts3Config
c4550 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Info, 0, sizeof(
c4560 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 29  fsts3ConfigInfo)
c4570 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75   );.  rc = DosQu
c4580 65 72 79 50 61 74 68 49 6e 66 6f 28 20 28 50 53  eryPathInfo( (PS
c4590 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70 2c 20 46  Z)zFilenameCp, F
c45a0 49 4c 5f 53 54 41 4e 44 41 52 44 2c 0a 20 20 20  IL_STANDARD,.   
c45b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c45c0 20 20 20 20 20 20 26 66 73 74 73 33 43 6f 6e 66        &fsts3Conf
c45d0 69 67 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46  igInfo, sizeof(F
c45e0 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b 0a 20  ILESTATUS3) );. 
c45f0 20 66 72 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65   free( zFilename
c4600 43 70 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 34  Cp );.  OSTRACE4
c4610 28 20 22 41 43 43 45 53 53 20 66 73 74 73 33 43  ( "ACCESS fsts3C
c4620 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72 46 69  onfigInfo.attrFi
c4630 6c 65 3d 25 64 20 66 6c 61 67 73 3d 25 64 20 72  le=%d flags=%d r
c4640 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20  c=%d\n",.       
c4650 20 20 20 20 20 66 73 74 73 33 43 6f 6e 66 69 67       fsts3Config
c4660 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 2c 20 66  Info.attrFile, f
c4670 6c 61 67 73 2c 20 72 63 20 29 3b 0a 20 20 73 77  lags, rc );.  sw
c4680 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20  itch( flags ){. 
c4690 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41     case SQLITE_A
c46a0 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20  CCESS_READ:.    
c46b0 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45  case SQLITE_ACCE
c46c0 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20  SS_EXISTS:.     
c46d0 20 72 63 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f   rc = (rc == NO_
c46e0 45 52 52 4f 52 29 3b 0a 20 20 20 20 20 20 4f 53  ERROR);.      OS
c46f0 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53 20  TRACE3( "ACCESS 
c4700 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 61  %s access of rea
c4710 64 20 61 6e 64 20 65 78 69 73 74 73 20 20 72 63  d and exists  rc
c4720 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d  =%d\n", zFilenam
c4730 65 2c 20 72 63 20 29 3b 0a 20 20 20 20 20 20 62  e, rc );.      b
c4740 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
c4750 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
c4760 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63  DWRITE:.      rc
c4770 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52   = (rc == NO_ERR
c4780 4f 52 29 20 26 26 20 28 20 28 66 73 74 73 33 43  OR) && ( (fsts3C
c4790 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72 46 69  onfigInfo.attrFi
c47a0 6c 65 20 26 20 46 49 4c 45 5f 52 45 41 44 4f 4e  le & FILE_READON
c47b0 4c 59 29 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20  LY) == 0 );.    
c47c0 20 20 4f 53 54 52 41 43 45 33 28 20 22 41 43 43    OSTRACE3( "ACC
c47d0 45 53 53 20 25 73 20 61 63 63 65 73 73 20 6f 66  ESS %s access of
c47e0 20 72 65 61 64 2f 77 72 69 74 65 20 20 72 63 3d   read/write  rc=
c47f0 25 64 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65  %d\n", zFilename
c4800 2c 20 72 63 20 29 3b 0a 20 20 20 20 20 20 62 72  , rc );.      br
c4810 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
c4820 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  :.      assert( 
c4830 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20  !"Invalid flags 
c4840 61 72 67 75 6d 65 6e 74 22 20 29 3b 0a 20 20 7d  argument" );.  }
c4850 0a 20 20 2a 70 4f 75 74 20 3d 20 72 63 3b 0a 20  .  *pOut = rc;. 
c4860 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c4870 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  K;.}...#ifndef S
c4880 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
c4890 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20  EXTENSION./*.** 
c48a0 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f  Interfaces for o
c48b0 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20  pening a shared 
c48c0 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67  library, finding
c48d0 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a   entry points.**
c48e0 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72   within the shar
c48f0 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20  ed library, and 
c4900 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72  closing the shar
c4910 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f  ed library..*/./
c4920 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20  *.** Interfaces 
c4930 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68  for opening a sh
c4940 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69  ared library, fi
c4950 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e  nding entry poin
c4960 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  ts.** within the
c4970 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
c4980 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65   and closing the
c4990 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e   shared library.
c49a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
c49b0 2a 6f 73 32 44 6c 4f 70 65 6e 28 73 71 6c 69 74  *os2DlOpen(sqlit
c49c0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f  e3_vfs *pVfs, co
c49d0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
c49e0 61 6d 65 29 7b 0a 20 20 55 43 48 41 52 20 6c 6f  ame){.  UCHAR lo
c49f0 61 64 45 72 72 5b 32 35 36 5d 3b 0a 20 20 48 4d  adErr[256];.  HM
c4a00 4f 44 55 4c 45 20 68 6d 6f 64 3b 0a 20 20 41 50  ODULE hmod;.  AP
c4a10 49 52 45 54 20 72 63 3b 0a 20 20 63 68 61 72 20  IRET rc;.  char 
c4a20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63  *zFilenameCp = c
c4a30 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f  onvertUtf8PathTo
c4a40 43 70 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  Cp(zFilename);. 
c4a50 20 72 63 20 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64   rc = DosLoadMod
c4a60 75 6c 65 28 28 50 53 5a 29 6c 6f 61 64 45 72 72  ule((PSZ)loadErr
c4a70 2c 20 73 69 7a 65 6f 66 28 6c 6f 61 64 45 72 72  , sizeof(loadErr
c4a80 29 2c 20 7a 46 69 6c 65 6e 61 6d 65 43 70 2c 20  ), zFilenameCp, 
c4a90 26 68 6d 6f 64 29 3b 0a 20 20 66 72 65 65 28 7a  &hmod);.  free(z
c4aa0 46 69 6c 65 6e 61 6d 65 43 70 29 3b 0a 20 20 72  FilenameCp);.  r
c4ab0 65 74 75 72 6e 20 72 63 20 21 3d 20 4e 4f 5f 45  eturn rc != NO_E
c4ac0 52 52 4f 52 20 3f 20 30 20 3a 20 28 76 6f 69 64  RROR ? 0 : (void
c4ad0 2a 29 68 6d 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20  *)hmod;.}./*.** 
c4ae0 41 20 6e 6f 2d 6f 70 20 73 69 6e 63 65 20 74 68  A no-op since th
c4af0 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
c4b00 72 65 74 75 72 6e 65 64 20 6f 6e 20 74 68 65 20  returned on the 
c4b10 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 20 63 61  DosLoadModule ca
c4b20 6c 6c 2e 0a 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e  ll..** os2Dlopen
c4b30 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 20 69 66   returns zero if
c4b40 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 20 69   DosLoadModule i
c4b50 73 20 6e 6f 74 20 73 75 63 63 65 73 73 66 75 6c  s not successful
c4b60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
c4b70 20 6f 73 32 44 6c 45 72 72 6f 72 28 73 71 6c 69   os2DlError(sqli
c4b80 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
c4b90 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
c4ba0 42 75 66 4f 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f  BufOut){./* no-o
c4bb0 70 20 2a 2f 0a 7d 0a 73 74 61 74 69 63 20 76 6f  p */.}.static vo
c4bc0 69 64 20 2a 6f 73 32 44 6c 53 79 6d 28 73 71 6c  id *os2DlSym(sql
c4bd0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
c4be0 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63  void *pHandle, c
c4bf0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62  onst char *zSymb
c4c00 6f 6c 29 7b 0a 20 20 50 46 4e 20 70 66 6e 3b 0a  ol){.  PFN pfn;.
c4c10 20 20 41 50 49 52 45 54 20 72 63 3b 0a 20 20 72    APIRET rc;.  r
c4c20 63 20 3d 20 44 6f 73 51 75 65 72 79 50 72 6f 63  c = DosQueryProc
c4c30 41 64 64 72 28 28 48 4d 4f 44 55 4c 45 29 70 48  Addr((HMODULE)pH
c4c40 61 6e 64 6c 65 2c 20 30 4c 2c 20 7a 53 79 6d 62  andle, 0L, zSymb
c4c50 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 69 66 28  ol, &pfn);.  if(
c4c60 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc != NO_ERROR 
c4c70 29 7b 0a 20 20 20 20 2f 2a 20 69 66 20 74 68 65  ){.    /* if the
c4c80 20 73 79 6d 62 6f 6c 20 69 74 73 65 6c 66 20 77   symbol itself w
c4c90 61 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 73 65  as not found, se
c4ca0 61 72 63 68 20 61 67 61 69 6e 20 66 6f 72 20 74  arch again for t
c4cb0 68 65 20 73 61 6d 65 0a 20 20 20 20 20 2a 20 73  he same.     * s
c4cc0 79 6d 62 6f 6c 20 77 69 74 68 20 61 6e 20 65 78  ymbol with an ex
c4cd0 74 72 61 20 75 6e 64 65 72 73 63 6f 72 65 2c 20  tra underscore, 
c4ce0 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 6e 65  that might be ne
c4cf0 65 64 65 64 20 64 65 70 65 6e 64 69 6e 67 0a 20  eded depending. 
c4d00 20 20 20 20 2a 20 6f 6e 20 74 68 65 20 63 61 6c      * on the cal
c4d10 6c 69 6e 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 20  ling convention 
c4d20 2a 2f 0a 20 20 20 20 63 68 61 72 20 5f 7a 53 79  */.    char _zSy
c4d30 6d 62 6f 6c 5b 32 35 36 5d 20 3d 20 22 5f 22 3b  mbol[256] = "_";
c4d40 0a 20 20 20 20 73 74 72 6e 63 61 74 28 5f 7a 53  .    strncat(_zS
c4d50 79 6d 62 6f 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20  ymbol, zSymbol, 
c4d60 32 35 35 29 3b 0a 20 20 20 20 72 63 20 3d 20 44  255);.    rc = D
c4d70 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72 28  osQueryProcAddr(
c4d80 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65  (HMODULE)pHandle
c4d90 2c 20 30 4c 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20  , 0L, _zSymbol, 
c4da0 26 70 66 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74  &pfn);.  }.  ret
c4db0 75 72 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52  urn rc != NO_ERR
c4dc0 4f 52 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29  OR ? 0 : (void*)
c4dd0 70 66 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  pfn;.}.static vo
c4de0 69 64 20 6f 73 32 44 6c 43 6c 6f 73 65 28 73 71  id os2DlClose(sq
c4df0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
c4e00 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b   void *pHandle){
c4e10 0a 20 20 44 6f 73 46 72 65 65 4d 6f 64 75 6c 65  .  DosFreeModule
c4e20 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c  ((HMODULE)pHandl
c4e30 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69  e);.}.#else /* i
c4e40 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
c4e50 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20  AD_EXTENSION is 
c4e60 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64  defined: */.  #d
c4e70 65 66 69 6e 65 20 6f 73 32 44 6c 4f 70 65 6e 20  efine os2DlOpen 
c4e80 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44  0.  #define os2D
c4e90 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69  lError 0.  #defi
c4ea0 6e 65 20 6f 73 32 44 6c 53 79 6d 20 30 0a 20 20  ne os2DlSym 0.  
c4eb0 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 43 6c 6f  #define os2DlClo
c4ec0 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  se 0.#endif.../*
c4ed0 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74 6f 20  .** Write up to 
c4ee0 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61  nBuf bytes of ra
c4ef0 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42  ndomness into zB
c4f00 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  uf..*/.static in
c4f10 74 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73 28  t os2Randomness(
c4f20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
c4f30 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  s, int nBuf, cha
c4f40 72 20 2a 7a 42 75 66 20 29 7b 0a 20 20 69 6e 74  r *zBuf ){.  int
c4f50 20 6e 20 3d 20 30 3b 0a 23 69 66 20 64 65 66 69   n = 0;.#if defi
c4f60 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
c4f70 0a 20 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d  .  n = nBuf;.  m
c4f80 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e  emset(zBuf, 0, n
c4f90 42 75 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 6e  Buf);.#else.  in
c4fa0 74 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20  t sizeofULong = 
c4fb0 73 69 7a 65 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20  sizeof(ULONG);. 
c4fc0 20 69 66 28 20 28 69 6e 74 29 73 69 7a 65 6f 66   if( (int)sizeof
c4fd0 28 44 41 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42  (DATETIME) <= nB
c4fe0 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41  uf - n ){.    DA
c4ff0 54 45 54 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f  TETIME x;.    Do
c5000 73 47 65 74 44 61 74 65 54 69 6d 65 28 26 78 29  sGetDateTime(&x)
c5010 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42  ;.    memcpy(&zB
c5020 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f  uf[n], &x, sizeo
c5030 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  f(x));.    n += 
c5040 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a  sizeof(x);.  }..
c5050 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e    if( sizeofULon
c5060 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b  g <= nBuf - n ){
c5070 0a 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a  .    PPIB ppib;.
c5080 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c      DosGetInfoBl
c5090 6f 63 6b 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62  ocks(NULL, &ppib
c50a0 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
c50b0 42 75 66 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70  Buf[n], &ppib->p
c50c0 69 62 5f 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66  ib_ulpid, sizeof
c50d0 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d  ULong);.    n +=
c50e0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20   sizeofULong;.  
c50f0 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55  }..  if( sizeofU
c5100 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e  Long <= nBuf - n
c5110 20 29 7b 0a 20 20 20 20 50 54 49 42 20 70 74 69   ){.    PTIB pti
c5120 62 3b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66  b;.    DosGetInf
c5130 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e  oBlocks(&ptib, N
c5140 55 4c 4c 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  ULL);.    memcpy
c5150 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62  (&zBuf[n], &ptib
c5160 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62  ->tib_ptib2->tib
c5170 32 5f 75 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55  2_ultid, sizeofU
c5180 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  Long);.    n += 
c5190 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d  sizeofULong;.  }
c51a0 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 73 74 69  ..  /* if we sti
c51b0 6c 6c 20 68 61 76 65 6e 27 74 20 66 69 6c 6c 65  ll haven't fille
c51c0 64 20 74 68 65 20 62 75 66 66 65 72 20 79 65 74  d the buffer yet
c51d0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77   the following w
c51e0 69 6c 6c 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62  ill */.  /* grab
c51f0 20 65 76 65 72 79 74 68 69 6e 67 20 6f 6e 63 65   everything once
c5200 20 69 6e 73 74 65 61 64 20 6f 66 20 6d 61 6b 69   instead of maki
c5210 6e 67 20 73 65 76 65 72 61 6c 20 63 61 6c 6c 73  ng several calls
c5220 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 74   for a single it
c5230 65 6d 20 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65  em */.  if( size
c5240 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20  ofULong <= nBuf 
c5250 2d 20 6e 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47  - n ){.    ULONG
c5260 20 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d   ulSysInfo[QSV_M
c5270 41 58 5d 3b 0a 20 20 20 20 44 6f 73 51 75 65 72  AX];.    DosQuer
c5280 79 53 79 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56  ySysInfo(1L, QSV
c5290 5f 4d 41 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c  _MAX, ulSysInfo,
c52a0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51   sizeofULong * Q
c52b0 53 56 5f 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65  SV_MAX);..    me
c52c0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
c52d0 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53  ulSysInfo[QSV_MS
c52e0 5f 43 4f 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a  _COUNT - 1], siz
c52f0 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e  eofULong);.    n
c5300 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b   += sizeofULong;
c5310 0a 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66  ..    if( sizeof
c5320 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20  ULong <= nBuf - 
c5330 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  n ){.      memcp
c5340 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53  y(&zBuf[n], &ulS
c5350 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52  ysInfo[QSV_TIMER
c5360 5f 49 4e 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20  _INTERVAL - 1], 
c5370 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20  sizeofULong);.  
c5380 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55      n += sizeofU
c5390 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Long;.    }.    
c53a0 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20  if( sizeofULong 
c53b0 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20  <= nBuf - n ){. 
c53c0 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75       memcpy(&zBu
c53d0 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f  f[n], &ulSysInfo
c53e0 5b 51 53 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20  [QSV_TIME_LOW - 
c53f0 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29  1], sizeofULong)
c5400 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ;.      n += siz
c5410 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a  eofULong;.    }.
c5420 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c      if( sizeofUL
c5430 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20  ong <= nBuf - n 
c5440 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
c5450 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73  &zBuf[n], &ulSys
c5460 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49  Info[QSV_TIME_HI
c5470 47 48 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55  GH - 1], sizeofU
c5480 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b  Long);.      n +
c5490 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20  = sizeofULong;. 
c54a0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a     }.    if( siz
c54b0 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66  eofULong <= nBuf
c54c0 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65   - n ){.      me
c54d0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
c54e0 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f  ulSysInfo[QSV_TO
c54f0 54 41 56 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20  TAVAILMEM - 1], 
c5500 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20  sizeofULong);.  
c5510 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55      n += sizeofU
c5520 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Long;.    }.  }.
c5530 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
c5540 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65   n;.}../*.** Sle
c5550 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20  ep for a little 
c5560 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74  while.  Return t
c5570 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d  he amount of tim
c5580 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20  e slept..** The 
c5590 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
c55a0 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73  number of micros
c55b0 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 74  econds we want t
c55c0 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20  o sleep..** The 
c55d0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
c55e0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  the number of mi
c55f0 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c  croseconds of sl
c5600 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20  eep actually.** 
c5610 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74  requested from t
c5620 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70  he underlying op
c5630 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2c 20  erating system, 
c5640 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68 0a 2a  a number which.*
c5650 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65 61 74  * might be great
c5660 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
c5670 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e 74   to the argument
c5680 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a  , but not less.*
c5690 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67 75 6d  * than the argum
c56a0 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ent..*/.static i
c56b0 6e 74 20 6f 73 32 53 6c 65 65 70 28 20 73 71 6c  nt os2Sleep( sql
c56c0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
c56d0 69 6e 74 20 6d 69 63 72 6f 73 65 63 20 29 7b 0a  int microsec ){.
c56e0 20 20 44 6f 73 53 6c 65 65 70 28 20 28 6d 69 63    DosSleep( (mic
c56f0 72 6f 73 65 63 2f 31 30 30 30 29 20 29 3b 0a 20  rosec/1000) );. 
c5700 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63   return microsec
c5710 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
c5720 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
c5730 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e  e, if set to a n
c5740 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62  on-zero value, b
c5750 65 63 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c  ecomes the resul
c5760 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72  t.** returned fr
c5770 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72  om sqlite3OsCurr
c5780 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73  entTime().  This
c5790 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73   is used for tes
c57a0 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ting..*/.#ifdef 
c57b0 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
c57c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c57d0 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20  e3_current_time 
c57e0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
c57f0 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72 72  ** Find the curr
c5800 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69  ent time (in Uni
c5810 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74  versal Coordinat
c5820 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65  ed Time).  Write
c5830 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   the.** current 
c5840 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73  time and date as
c5850 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75   a Julian Day nu
c5860 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77  mber into *prNow
c5870 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30   and.** return 0
c5880 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 74  .  Return 1 if t
c5890 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65  he time and date
c58a0 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
c58b0 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 43 75 72 72  ..*/.int os2Curr
c58c0 65 6e 74 54 69 6d 65 28 20 73 71 6c 69 74 65 33  entTime( sqlite3
c58d0 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62  _vfs *pVfs, doub
c58e0 6c 65 20 2a 70 72 4e 6f 77 20 29 7b 0a 20 20 64  le *prNow ){.  d
c58f0 6f 75 62 6c 65 20 6e 6f 77 3b 0a 20 20 53 48 4f  ouble now;.  SHO
c5900 52 54 20 6d 69 6e 75 74 65 3b 20 2f 2a 20 6e 65  RT minute; /* ne
c5910 65 64 73 20 74 6f 20 62 65 20 61 62 6c 65 20 74  eds to be able t
c5920 6f 20 63 6f 70 65 20 77 69 74 68 20 6e 65 67 61  o cope with nega
c5930 74 69 76 65 20 74 69 6d 65 7a 6f 6e 65 20 6f 66  tive timezone of
c5940 66 73 65 74 20 2a 2f 0a 20 20 55 53 48 4f 52 54  fset */.  USHORT
c5950 20 73 65 63 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20   second, hour,. 
c5960 20 20 20 20 20 20 20 20 64 61 79 2c 20 6d 6f 6e          day, mon
c5970 74 68 2c 20 79 65 61 72 3b 0a 20 20 44 41 54 45  th, year;.  DATE
c5980 54 49 4d 45 20 64 74 3b 0a 20 20 44 6f 73 47 65  TIME dt;.  DosGe
c5990 74 44 61 74 65 54 69 6d 65 28 20 26 64 74 20 29  tDateTime( &dt )
c59a0 3b 0a 20 20 73 65 63 6f 6e 64 20 3d 20 28 55 53  ;.  second = (US
c59b0 48 4f 52 54 29 64 74 2e 73 65 63 6f 6e 64 73 3b  HORT)dt.seconds;
c59c0 0a 20 20 6d 69 6e 75 74 65 20 3d 20 28 53 48 4f  .  minute = (SHO
c59d0 52 54 29 64 74 2e 6d 69 6e 75 74 65 73 20 2b 20  RT)dt.minutes + 
c59e0 64 74 2e 74 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68  dt.timezone;.  h
c59f0 6f 75 72 20 3d 20 28 55 53 48 4f 52 54 29 64 74  our = (USHORT)dt
c5a00 2e 68 6f 75 72 73 3b 0a 20 20 64 61 79 20 3d 20  .hours;.  day = 
c5a10 28 55 53 48 4f 52 54 29 64 74 2e 64 61 79 3b 0a  (USHORT)dt.day;.
c5a20 20 20 6d 6f 6e 74 68 20 3d 20 28 55 53 48 4f 52    month = (USHOR
c5a30 54 29 64 74 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65  T)dt.month;.  ye
c5a40 61 72 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e  ar = (USHORT)dt.
c5a50 79 65 61 72 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63  year;..  /* Calc
c5a60 75 6c 61 74 69 6f 6e 73 20 66 72 6f 6d 20 68 74  ulations from ht
c5a70 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b  tp://www.astro.k
c5a80 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f  eele.ac.uk/~rno/
c5a90 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74  Astronomy/hjd.ht
c5aa0 6d 6c 0a 20 20 20 20 20 68 74 74 70 3a 2f 2f 77  ml.     http://w
c5ab0 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61  ww.astro.keele.a
c5ac0 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e  c.uk/~rno/Astron
c5ad0 6f 6d 79 2f 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f  omy/hjd-0.1.c */
c5ae0 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20  .  /* Calculate 
c5af0 74 68 65 20 4a 75 6c 69 61 6e 20 64 61 79 73 20  the Julian days 
c5b00 2a 2f 0a 20 20 6e 6f 77 20 3d 20 64 61 79 20 2d  */.  now = day -
c5b10 20 33 32 30 37 36 20 2b 0a 20 20 20 20 31 34 36   32076 +.    146
c5b20 31 2a 28 79 65 61 72 20 2b 20 34 38 30 30 20 2b  1*(year + 4800 +
c5b30 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32   (month - 14)/12
c5b40 29 2f 34 20 2b 0a 20 20 20 20 33 36 37 2a 28 6d  )/4 +.    367*(m
c5b50 6f 6e 74 68 20 2d 20 32 20 2d 20 28 6d 6f 6e 74  onth - 2 - (mont
c5b60 68 20 2d 20 31 34 29 2f 31 32 2a 31 32 29 2f 31  h - 14)/12*12)/1
c5b70 32 20 2d 0a 20 20 20 20 33 2a 28 28 79 65 61 72  2 -.    3*((year
c5b80 20 2b 20 34 39 30 30 20 2b 20 28 6d 6f 6e 74 68   + 4900 + (month
c5b90 20 2d 20 31 34 29 2f 31 32 29 2f 31 30 30 29 2f   - 14)/12)/100)/
c5ba0 34 3b 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65  4;..  /* Add the
c5bb0 20 66 72 61 63 74 69 6f 6e 61 6c 20 68 6f 75 72   fractional hour
c5bc0 73 2c 20 6d 69 6e 73 20 61 6e 64 20 73 65 63 6f  s, mins and seco
c5bd0 6e 64 73 20 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20  nds */.  now += 
c5be0 28 68 6f 75 72 20 2b 20 31 32 2e 30 29 2f 32 34  (hour + 12.0)/24
c5bf0 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e  .0;.  now += min
c5c00 75 74 65 2f 31 34 34 30 2e 30 3b 0a 20 20 6e 6f  ute/1440.0;.  no
c5c10 77 20 2b 3d 20 73 65 63 6f 6e 64 2f 38 36 34 30  w += second/8640
c5c20 30 2e 30 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20  0.0;.  *prNow = 
c5c30 6e 6f 77 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  now;.#ifdef SQLI
c5c40 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71  TE_TEST.  if( sq
c5c50 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69  lite3_current_ti
c5c60 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77  me ){.    *prNow
c5c70 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65   = sqlite3_curre
c5c80 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20  nt_time/86400.0 
c5c90 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d  + 2440587.5;.  }
c5ca0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
c5cb0 20 30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e   0;.}..static in
c5cc0 74 20 6f 73 32 47 65 74 4c 61 73 74 45 72 72 6f  t os2GetLastErro
c5cd0 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  r(sqlite3_vfs *p
c5ce0 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63  Vfs, int nBuf, c
c5cf0 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65  har *zBuf){.  re
c5d00 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
c5d10 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20   Initialize and 
c5d20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  deinitialize the
c5d30 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
c5d40 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a  m interface..*/.
c5d50 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
c5d60 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76  qlite3_os_init(v
c5d70 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73  oid){.  static s
c5d80 71 6c 69 74 65 33 5f 76 66 73 20 6f 73 32 56 66  qlite3_vfs os2Vf
c5d90 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 20  s = {.    1,    
c5da0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c5db0 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20  iVersion */.    
c5dc0 73 69 7a 65 6f 66 28 6f 73 32 46 69 6c 65 29 2c  sizeof(os2File),
c5dd0 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a     /* szOsFile *
c5de0 2f 0a 20 20 20 20 43 43 48 4d 41 58 50 41 54 48  /.    CCHMAXPATH
c5df0 2c 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61  ,        /* mxPa
c5e00 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c  thname */.    0,
c5e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5e20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20   /* pNext */.   
c5e30 20 22 6f 73 32 22 2c 20 20 20 20 20 20 20 20 20   "os2",         
c5e40 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a      /* zName */.
c5e50 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
c5e60 20 20 20 20 20 20 20 2f 2a 20 70 41 70 70 44 61         /* pAppDa
c5e70 74 61 20 2a 2f 0a 0a 20 20 20 20 6f 73 32 4f 70  ta */..    os2Op
c5e80 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  en,           /*
c5e90 20 78 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73   xOpen */.    os
c5ea0 32 44 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20  2Delete,        
c5eb0 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20   /* xDelete */. 
c5ec0 20 20 20 6f 73 32 41 63 63 65 73 73 2c 20 20 20     os2Access,   
c5ed0 20 20 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73        /* xAccess
c5ee0 20 2a 2f 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50   */.    os2FullP
c5ef0 61 74 68 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46  athname,   /* xF
c5f00 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20  ullPathname */. 
c5f10 20 20 20 6f 73 32 44 6c 4f 70 65 6e 2c 20 20 20     os2DlOpen,   
c5f20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e        /* xDlOpen
c5f30 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 45 72 72   */.    os2DlErr
c5f40 6f 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44  or,        /* xD
c5f50 6c 45 72 72 6f 72 20 2a 2f 0a 20 20 20 20 6f 73  lError */.    os
c5f60 32 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20  2DlSym,         
c5f70 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20   /* xDlSym */.  
c5f80 20 20 6f 73 32 44 6c 43 6c 6f 73 65 2c 20 20 20    os2DlClose,   
c5f90 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65       /* xDlClose
c5fa0 20 2a 2f 0a 20 20 20 20 6f 73 32 52 61 6e 64 6f   */.    os2Rando
c5fb0 6d 6e 65 73 73 2c 20 20 20 20 20 2f 2a 20 78 52  mness,     /* xR
c5fc0 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20  andomness */.   
c5fd0 20 6f 73 32 53 6c 65 65 70 2c 20 20 20 20 20 20   os2Sleep,      
c5fe0 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f      /* xSleep */
c5ff0 0a 20 20 20 20 6f 73 32 43 75 72 72 65 6e 74 54  .    os2CurrentT
c6000 69 6d 65 2c 20 20 20 20 2f 2a 20 78 43 75 72 72  ime,    /* xCurr
c6010 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 20 20 20 6f  entTime */.    o
c6020 73 32 47 65 74 4c 61 73 74 45 72 72 6f 72 20 20  s2GetLastError  
c6030 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72    /* xGetLastErr
c6040 6f 72 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c  or */.  };.  sql
c6050 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65  ite3_vfs_registe
c6060 72 28 26 6f 73 32 56 66 73 2c 20 31 29 3b 0a 20  r(&os2Vfs, 1);. 
c6070 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74   initUconvObject
c6080 73 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  s();.  return SQ
c6090 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54  LITE_OK;.}.SQLIT
c60a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c60b0 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b 0a  3_os_end(void){.
c60c0 20 20 66 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63    freeUconvObjec
c60d0 74 73 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ts();.  return S
c60e0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e  QLITE_OK;.}..#en
c60f0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53  dif /* SQLITE_OS
c6100 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _OS2 */../******
c6110 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
c6120 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_os2.c *******
c6130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6150 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
c6160 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
c6170 69 6c 65 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a  ile os_unix.c **
c6180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c61a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
c61b0 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a  004 May 22.**.**
c61c0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
c61d0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
c61e0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
c61f0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
c6200 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
c6210 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
c6220 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
c6230 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
c6240 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
c6250 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
c6260 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
c6270 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
c6280 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
c6290 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
c62a0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
c62b0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
c62c0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
c62d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c62e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c62f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
c6320 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
c6330 6f 6e 74 61 69 6e 73 20 74 68 65 20 56 46 53 20  ontains the VFS 
c6340 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66  implementation f
c6350 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65  or unix-like ope
c6360 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 0a 2a  rating systems.*
c6370 2a 20 69 6e 63 6c 75 64 65 20 4c 69 6e 75 78 2c  * include Linux,
c6380 20 4d 61 63 4f 53 58 2c 20 2a 42 53 44 2c 20 51   MacOSX, *BSD, Q
c6390 4e 58 2c 20 56 78 57 6f 72 6b 73 2c 20 41 49 58  NX, VxWorks, AIX
c63a0 2c 20 48 50 55 58 2c 20 61 6e 64 20 6f 74 68 65  , HPUX, and othe
c63b0 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  rs..**.** There 
c63c0 61 72 65 20 61 63 74 75 61 6c 6c 79 20 73 65 76  are actually sev
c63d0 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74 20 56  eral different V
c63e0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  FS implementatio
c63f0 6e 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e  ns in this file.
c6400 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e  .** The differen
c6410 63 65 73 20 61 72 65 20 69 6e 20 74 68 65 20 77  ces are in the w
c6420 61 79 20 74 68 61 74 20 66 69 6c 65 20 6c 6f 63  ay that file loc
c6430 6b 69 6e 67 20 69 73 20 64 6f 6e 65 2e 20 20 54  king is done.  T
c6440 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69 6d  he default.** im
c6450 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65  plementation use
c6460 73 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79  s Posix Advisory
c6470 20 4c 6f 63 6b 73 2e 20 20 41 6c 74 65 72 6e 61   Locks.  Alterna
c6480 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tive implementat
c6490 69 6f 6e 73 0a 2a 2a 20 75 73 65 20 66 6c 6f 63  ions.** use floc
c64a0 6b 28 29 2c 20 64 6f 74 2d 66 69 6c 65 73 2c 20  k(), dot-files, 
c64b0 76 61 72 69 6f 75 73 20 70 72 6f 70 72 69 65 74  various propriet
c64c0 61 72 79 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65  ary locking sche
c64d0 6d 61 73 2c 20 6f 72 20 73 69 6d 70 6c 79 0a 2a  mas, or simply.*
c64e0 2a 20 73 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61  * skip locking a
c64f0 6c 6c 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a  ll together..**.
c6500 2a 2a 20 54 68 69 73 20 73 6f 75 72 63 65 20 66  ** This source f
c6510 69 6c 65 20 69 73 20 6f 72 67 61 6e 69 7a 65 64  ile is organized
c6520 20 69 6e 74 6f 20 64 69 76 69 73 69 6f 6e 73 20   into divisions 
c6530 77 68 65 72 65 20 74 68 65 20 6c 6f 67 69 63 20  where the logic 
c6540 66 6f 72 20 76 61 72 69 6f 75 73 0a 2a 2a 20 73  for various.** s
c6550 75 62 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 63  ubfunctions is c
c6560 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20  ontained within 
c6570 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
c6580 64 69 76 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53  division.  PLEAS
c6590 45 0a 2a 2a 20 4b 45 45 50 20 54 48 45 20 53 54  E.** KEEP THE ST
c65a0 52 55 43 54 55 52 45 20 4f 46 20 54 48 49 53 20  RUCTURE OF THIS 
c65b0 46 49 4c 45 20 49 4e 54 41 43 54 2e 20 20 4e 65  FILE INTACT.  Ne
c65c0 77 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 62 65  w code should be
c65d0 20 70 6c 61 63 65 64 0a 2a 2a 20 69 6e 20 74 68   placed.** in th
c65e0 65 20 63 6f 72 72 65 63 74 20 64 69 76 69 73 69  e correct divisi
c65f0 6f 6e 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65  on and should be
c6600 20 63 6c 65 61 72 6c 79 20 6c 61 62 65 6c 65 64   clearly labeled
c6610 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 61 79 6f  ..**.** The layo
c6620 75 74 20 6f 66 20 64 69 76 69 73 69 6f 6e 73 20  ut of divisions 
c6630 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  is as follows:.*
c6640 2a 0a 2a 2a 20 20 20 2a 20 20 47 65 6e 65 72 61  *.**   *  Genera
c6650 6c 2d 70 75 72 70 6f 73 65 20 64 65 63 6c 61 72  l-purpose declar
c6660 61 74 69 6f 6e 73 20 61 6e 64 20 75 74 69 6c 69  ations and utili
c6670 74 79 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  ty functions..**
c6680 20 20 20 2a 20 20 55 6e 69 71 75 65 20 66 69 6c     *  Unique fil
c6690 65 20 49 44 20 6c 6f 67 69 63 20 75 73 65 64 20  e ID logic used 
c66a0 62 79 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20  by VxWorks..**  
c66b0 20 2a 20 20 56 61 72 69 6f 75 73 20 6c 6f 63 6b   *  Various lock
c66c0 69 6e 67 20 70 72 69 6d 69 74 69 76 65 20 69 6d  ing primitive im
c66d0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 28 61  plementations (a
c66e0 6c 6c 20 65 78 63 65 70 74 20 70 72 6f 78 79 20  ll except proxy 
c66f0 6c 6f 63 6b 69 6e 67 29 3a 0a 2a 2a 20 20 20 20  locking):.**    
c6700 20 20 2b 20 66 6f 72 20 50 6f 73 69 78 20 41 64    + for Posix Ad
c6710 76 69 73 6f 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20  visory Locks.** 
c6720 20 20 20 20 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70       + for no-op
c6730 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b   locks.**      +
c6740 20 66 6f 72 20 64 6f 74 2d 66 69 6c 65 20 6c 6f   for dot-file lo
c6750 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f  cks.**      + fo
c6760 72 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e  r flock() lockin
c6770 67 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20  g.**      + for 
c6780 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20  named semaphore 
c6790 6c 6f 63 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f  locks (VxWorks o
c67a0 6e 6c 79 29 0a 2a 2a 20 20 20 20 20 20 2b 20 66  nly).**      + f
c67b0 6f 72 20 41 46 50 20 66 69 6c 65 73 79 73 74 65  or AFP filesyste
c67c0 6d 20 6c 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20  m locks (MacOSX 
c67d0 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 73 71  only).**   *  sq
c67e0 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f  lite3_file metho
c67f0 64 73 20 6e 6f 74 20 61 73 73 6f 63 69 61 74 65  ds not associate
c6800 64 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a  d with locking..
c6810 2a 2a 20 20 20 2a 20 20 44 65 66 69 6e 69 74 69  **   *  Definiti
c6820 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69  ons of sqlite3_i
c6830 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
c6840 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e  s for all lockin
c6850 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f 64  g.**      method
c6860 73 20 70 6c 75 73 20 22 66 69 6e 64 65 72 22 20  s plus "finder" 
c6870 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 65 61  functions for ea
c6880 63 68 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f  ch locking metho
c6890 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74  d..**   *  sqlit
c68a0 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 20 69 6d  e3_vfs method im
c68b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a  plementations..*
c68c0 2a 20 20 20 2a 20 20 4c 6f 63 6b 69 6e 67 20 70  *   *  Locking p
c68d0 72 69 6d 69 74 69 76 65 73 20 66 6f 72 20 74 68  rimitives for th
c68e0 65 20 70 72 6f 78 79 20 75 62 65 72 2d 6c 6f 63  e proxy uber-loc
c68f0 6b 69 6e 67 2d 6d 65 74 68 6f 64 2e 20 28 4d 61  king-method. (Ma
c6900 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20  cOSX only).**   
c6910 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f  *  Definitions o
c6920 66 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62  f sqlite3_vfs ob
c6930 6a 65 63 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f  jects for all lo
c6940 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 0a 2a 2a  cking methods.**
c6950 20 20 20 20 20 20 70 6c 75 73 20 69 6d 70 6c 65        plus imple
c6960 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 71  mentations of sq
c6970 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20  lite3_os_init() 
c6980 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65  and sqlite3_os_e
c6990 6e 64 28 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  nd()..*/.#if SQL
c69a0 49 54 45 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20  ITE_OS_UNIX     
c69b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
c69c0 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 6f 6e   file is used on
c69d0 20 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f   unix only */../
c69e0 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 76  *.** There are v
c69f0 61 72 69 6f 75 73 20 6d 65 74 68 6f 64 73 20 66  arious methods f
c6a00 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  or file locking 
c6a10 75 73 65 64 20 66 6f 72 20 63 6f 6e 63 75 72 72  used for concurr
c6a20 65 6e 63 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a  ency.** control:
c6a30 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49  .**.**   1. POSI
c6a40 58 20 6c 6f 63 6b 69 6e 67 20 28 74 68 65 20 64  X locking (the d
c6a50 65 66 61 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e  efault),.**   2.
c6a60 20 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20   No locking,.** 
c6a70 20 20 33 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f    3. Dot-file lo
c6a80 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66  cking,.**   4. f
c6a90 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a  lock() locking,.
c6aa0 2a 2a 20 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b  **   5. AFP lock
c6ab0 69 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a  ing (OSX only),.
c6ac0 2a 2a 20 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f  **   6. Named PO
c6ad0 53 49 58 20 73 65 6d 61 70 68 6f 72 65 73 20 28  SIX semaphores (
c6ae0 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a  VXWorks only),.*
c6af0 2a 20 20 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63  *   7. proxy loc
c6b00 6b 69 6e 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29  king. (OSX only)
c6b10 0a 2a 2a 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c  .**.** Styles 4,
c6b20 20 35 2c 20 61 6e 64 20 37 20 61 72 65 20 6f 6e   5, and 7 are on
c6b30 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 66 20  ly available of 
c6b40 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
c6b50 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69  CKING_STYLE.** i
c6b60 73 20 64 65 66 69 6e 65 64 20 74 6f 20 31 2e 20  s defined to 1. 
c6b70 20 54 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42   The SQLITE_ENAB
c6b80 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
c6b90 20 61 6c 73 6f 20 65 6e 61 62 6c 65 73 20 61 75   also enables au
c6ba0 74 6f 6d 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63  tomatic.** selec
c6bb0 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 70 70 72  tion of the appr
c6bc0 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20  opriate locking 
c6bd0 73 74 79 6c 65 20 62 61 73 65 64 20 6f 6e 20 74  style based on t
c6be0 68 65 20 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a  he filesystem.**
c6bf0 20 77 68 65 72 65 20 74 68 65 20 64 61 74 61 62   where the datab
c6c00 61 73 65 20 69 73 20 6c 6f 63 61 74 65 64 2e 20  ase is located. 
c6c10 20 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65   .*/.#if !define
c6c20 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
c6c30 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23  LOCKING_STYLE).#
c6c40 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41    if defined(__A
c6c50 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65 66  PPLE__).#    def
c6c60 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ine SQLITE_ENABL
c6c70 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
c6c80 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64  1.#  else.#    d
c6c90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41  efine SQLITE_ENA
c6ca0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
c6cb0 45 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e  E 0.#  endif.#en
c6cc0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e  dif../*.** Defin
c6cd0 65 20 74 68 65 20 4f 53 5f 56 58 57 4f 52 4b 53  e the OS_VXWORKS
c6ce0 20 70 72 65 2d 70 72 6f 63 65 73 73 6f 72 20 6d   pre-processor m
c6cf0 61 63 72 6f 20 74 6f 20 31 20 69 66 20 62 75 69  acro to 1 if bui
c6d00 6c 64 69 6e 67 20 6f 6e 20 0a 2a 2a 20 76 78 77  lding on .** vxw
c6d10 6f 72 6b 73 2c 20 6f 72 20 30 20 6f 74 68 65 72  orks, or 0 other
c6d20 77 69 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  wise..*/.#ifndef
c6d30 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69   OS_VXWORKS.#  i
c6d40 66 20 64 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f  f defined(__RTP_
c6d50 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57  _) || defined(_W
c6d60 52 53 5f 4b 45 52 4e 45 4c 29 0a 23 20 20 20 20  RS_KERNEL).#    
c6d70 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b  define OS_VXWORK
c6d80 53 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20  S 1.#  else.#   
c6d90 20 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52   define OS_VXWOR
c6da0 4b 53 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65  KS 0.#  endif.#e
c6db0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73  ndif../*.** Thes
c6dc0 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 6c  e #defines shoul
c6dd0 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 69  d enable >2GB fi
c6de0 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f  le support on Po
c6df0 73 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e  six if the.** un
c6e00 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
c6e10 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72  ng system suppor
c6e20 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 4f  ts it.  If the O
c6e30 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65  S lacks.** large
c6e40 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20 74   file support, t
c6e50 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20 6e  hese should be n
c6e60 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72  o-ops..**.** Lar
c6e70 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20  ge file support 
c6e80 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20  can be disabled 
c6e90 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49  using the -DSQLI
c6ea0 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73  TE_DISABLE_LFS s
c6eb0 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20  witch.** on the 
c6ec0 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64  compiler command
c6ed0 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20   line.  This is 
c6ee0 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75  necessary if you
c6ef0 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a   are compiling.*
c6f00 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61  * on a recent ma
c6f10 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61  chine (ex: RedHa
c6f20 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77  t 7.2) but you w
c6f30 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f  ant your code to
c6f40 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f   work.** on an o
c6f50 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78  lder machine (ex
c6f60 3a 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 20  : RedHat 6.0).  
c6f70 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f  If you compile o
c6f80 6e 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20  n RedHat 7.2.** 
c6f90 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74  without this opt
c6fa0 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62  ion, LFS is enab
c6fb0 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65  le.  But LFS doe
c6fc0 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74  s not exist in t
c6fd0 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20  he kernel.** in 
c6fe0 52 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 74  RedHat 6.0, so t
c6ff0 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f  he code won't wo
c7000 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20  rk.  Hence, for 
c7010 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a  maximum binary.*
c7020 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 6f  * portability yo
c7030 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46  u should omit LF
c7040 53 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65  S..**.** The pre
c7050 76 69 6f 75 73 20 70 61 72 61 67 72 61 70 68 20  vious paragraph 
c7060 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e 20 32  was written in 2
c7070 30 30 35 2e 20 20 28 54 68 69 73 20 70 61 72 61  005.  (This para
c7080 67 72 61 70 68 20 69 73 20 77 72 69 74 74 65 6e  graph is written
c7090 0a 2a 2a 20 6f 6e 20 32 30 30 38 2d 31 31 2d 32  .** on 2008-11-2
c70a0 38 2e 29 20 54 68 65 73 65 20 64 61 79 73 2c 20  8.) These days, 
c70b0 61 6c 6c 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c  all Linux kernel
c70c0 73 20 73 75 70 70 6f 72 74 20 6c 61 72 67 65 20  s support large 
c70d0 66 69 6c 65 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75  files, so.** you
c70e0 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79   should probably
c70f0 20 6c 65 61 76 65 20 4c 46 53 20 65 6e 61 62 6c   leave LFS enabl
c7100 65 64 2e 20 20 42 75 74 20 73 6f 6d 65 20 65 6d  ed.  But some em
c7110 62 65 64 64 65 64 20 70 6c 61 74 66 6f 72 6d 73  bedded platforms
c7120 20 6d 69 67 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c   might.** lack L
c7130 46 53 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  FS in which case
c7140 20 74 68 65 20 53 51 4c 49 54 45 5f 44 49 53 41   the SQLITE_DISA
c7150 42 4c 45 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69  BLE_LFS macro mi
c7160 67 68 74 20 73 74 69 6c 6c 20 62 65 20 75 73 65  ght still be use
c7170 66 75 6c 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ful..*/.#ifndef 
c7180 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c  SQLITE_DISABLE_L
c7190 46 53 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52  FS.# define _LAR
c71a0 47 45 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a  GE_FILE       1.
c71b0 23 20 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f  # ifndef _FILE_O
c71c0 46 46 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64  FFSET_BITS.#   d
c71d0 65 66 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53  efine _FILE_OFFS
c71e0 45 54 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64  ET_BITS 64.# end
c71f0 69 66 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52  if.# define _LAR
c7200 47 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a  GEFILE_SOURCE 1.
c7210 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74  #endif../*.** st
c7220 61 6e 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66  andard include f
c7230 69 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64  iles..*/.#includ
c7240 65 20 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a  e <sys/types.h>.
c7250 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74  #include <sys/st
c7260 61 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  at.h>.#include <
c7270 66 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64  fcntl.h>.#includ
c7280 65 20 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e  e <unistd.h>.#in
c7290 63 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e  clude <sys/time.
c72a0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72  h>.#include <err
c72b0 6e 6f 2e 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54  no.h>..#if SQLIT
c72c0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
c72d0 5f 53 54 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65  _STYLE.# include
c72e0 20 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23   <sys/ioctl.h>.#
c72f0 20 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23   if OS_VXWORKS.#
c7300 20 20 69 6e 63 6c 75 64 65 20 3c 73 65 6d 61 70    include <semap
c7310 68 6f 72 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75  hore.h>.#  inclu
c7320 64 65 20 3c 6c 69 6d 69 74 73 2e 68 3e 0a 23 20  de <limits.h>.# 
c7330 65 6c 73 65 0a 23 20 20 69 6e 63 6c 75 64 65 20  else.#  include 
c7340 3c 73 79 73 2f 66 69 6c 65 2e 68 3e 0a 23 20 20  <sys/file.h>.#  
c7350 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 70 61 72  include <sys/par
c7360 61 6d 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65  am.h>.#  include
c7370 20 3c 73 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23   <sys/mount.h>.#
c7380 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a   endif.#endif /*
c7390 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
c73a0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a  OCKING_STYLE */.
c73b0 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65  ./*.** If we are
c73c0 20 74 6f 20 62 65 20 74 68 72 65 61 64 2d 73 61   to be thread-sa
c73d0 66 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 65 20  fe, include the 
c73e0 70 74 68 72 65 61 64 73 20 68 65 61 64 65 72 20  pthreads header 
c73f0 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68  and define.** th
c7400 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48  e SQLITE_UNIX_TH
c7410 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a  READS macro..*/.
c7420 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
c7430 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53  DSAFE.# define S
c7440 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41  QLITE_UNIX_THREA
c7450 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  DS 1.#endif../*.
c7460 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69  ** Default permi
c7470 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61  ssions when crea
c7480 74 69 6e 67 20 61 20 6e 65 77 20 66 69 6c 65 0a  ting a new file.
c7490 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
c74a0 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50  E_DEFAULT_FILE_P
c74b0 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66  ERMISSIONS.# def
c74c0 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ine SQLITE_DEFAU
c74d0 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49  LT_FILE_PERMISSI
c74e0 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64 69 66 0a  ONS 0644.#endif.
c74f0 0a 2f 2a 0a 20 2a 2a 20 44 65 66 61 75 6c 74 20  ./*. ** Default 
c7500 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e  permissions when
c7510 20 63 72 65 61 74 69 6e 67 20 61 75 74 6f 20 70   creating auto p
c7520 72 6f 78 79 20 64 69 72 0a 20 2a 2f 0a 23 69 66  roxy dir. */.#if
c7530 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
c7540 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52  ULT_PROXYDIR_PER
c7550 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e  MISSIONS.# defin
c7560 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
c7570 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53  _PROXYDIR_PERMIS
c7580 53 49 4f 4e 53 20 30 37 35 35 0a 23 65 6e 64 69  SIONS 0755.#endi
c7590 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d  f../*.** Maximum
c75a0 20 73 75 70 70 6f 72 74 65 64 20 70 61 74 68 2d   supported path-
c75b0 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69  length..*/.#defi
c75c0 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20  ne MAX_PATHNAME 
c75d0 35 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20  512../*.** Only 
c75e0 73 65 74 20 74 68 65 20 6c 61 73 74 45 72 72 6e  set the lastErrn
c75f0 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 63  o if the error c
c7600 6f 64 65 20 69 73 20 61 20 72 65 61 6c 20 65 72  ode is a real er
c7610 72 6f 72 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20  ror and not .** 
c7620 61 20 6e 6f 72 6d 61 6c 20 65 78 70 65 63 74 65  a normal expecte
c7630 64 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66  d return code of
c7640 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20   SQLITE_BUSY or 
c7650 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65  SQLITE_OK.*/.#de
c7660 66 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52  fine IS_LOCK_ERR
c7670 4f 52 28 78 29 20 20 28 28 78 20 21 3d 20 53 51  OR(x)  ((x != SQ
c7680 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 78 20 21  LITE_OK) && (x !
c7690 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 29 29 0a  = SQLITE_BUSY)).
c76a0 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65  ../*.** Sometime
c76b0 73 2c 20 61 66 74 65 72 20 61 20 66 69 6c 65 20  s, after a file 
c76c0 68 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64  handle is closed
c76d0 20 62 79 20 53 51 4c 69 74 65 2c 20 74 68 65 20   by SQLite, the 
c76e0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a  file descriptor.
c76f0 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63 6c 6f  ** cannot be clo
c7700 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  sed immediately.
c7710 20 49 6e 20 74 68 65 73 65 20 63 61 73 65 73 2c   In these cases,
c7720 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68   instances of th
c7730 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73  e following.** s
c7740 74 72 75 63 74 75 72 65 20 61 72 65 20 75 73 65  tructure are use
c7750 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66  d to store the f
c7760 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77  ile descriptor w
c7770 68 69 6c 65 20 77 61 69 74 69 6e 67 20 66 6f 72  hile waiting for
c7780 20 61 6e 0a 2a 2a 20 6f 70 70 6f 72 74 75 6e 69   an.** opportuni
c7790 74 79 20 74 6f 20 65 69 74 68 65 72 20 63 6c 6f  ty to either clo
c77a0 73 65 20 6f 72 20 72 65 75 73 65 20 69 74 2e 0a  se or reuse it..
c77b0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
c77c0 74 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 55  t UnixUnusedFd U
c77d0 6e 69 78 55 6e 75 73 65 64 46 64 3b 0a 73 74 72  nixUnusedFd;.str
c77e0 75 63 74 20 55 6e 69 78 55 6e 75 73 65 64 46 64  uct UnixUnusedFd
c77f0 20 7b 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20   {.  int fd;    
c7800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c7810 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * File descripto
c7820 72 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 20 20  r to close */.  
c7830 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
c7840 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
c7850 67 73 20 74 68 69 73 20 66 69 6c 65 20 64 65 73  gs this file des
c7860 63 72 69 70 74 6f 72 20 77 61 73 20 6f 70 65 6e  criptor was open
c7870 65 64 20 77 69 74 68 20 2a 2f 0a 20 20 55 6e 69  ed with */.  Uni
c7880 78 55 6e 75 73 65 64 46 64 20 2a 70 4e 65 78 74  xUnusedFd *pNext
c7890 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 75  ;      /* Next u
c78a0 6e 75 73 65 64 20 66 69 6c 65 20 64 65 73 63 72  nused file descr
c78b0 69 70 74 6f 72 20 6f 6e 20 73 61 6d 65 20 66 69  iptor on same fi
c78c0 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  le */.};../*.** 
c78d0 54 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72  The unixFile str
c78e0 75 63 74 75 72 65 20 69 73 20 73 75 62 63 6c 61  ucture is subcla
c78f0 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69  ss of sqlite3_fi
c7900 6c 65 20 73 70 65 63 69 66 69 63 20 74 6f 20 74  le specific to t
c7910 68 65 20 75 6e 69 78 0a 2a 2a 20 56 46 53 20 69  he unix.** VFS i
c7920 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a  mplementations..
c7930 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
c7940 74 20 75 6e 69 78 46 69 6c 65 20 75 6e 69 78 46  t unixFile unixF
c7950 69 6c 65 3b 0a 73 74 72 75 63 74 20 75 6e 69 78  ile;.struct unix
c7960 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33  File {.  sqlite3
c7970 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73  _io_methods cons
c7980 74 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20  t *pMethod;  /* 
c7990 41 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74  Always the first
c79a0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75   entry */.  stru
c79b0 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a  ct unixOpenCnt *
c79c0 70 4f 70 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20  pOpen;       /* 
c79d0 49 6e 66 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f  Info about all o
c79e0 70 65 6e 20 66 64 27 73 20 6f 6e 20 74 68 69 73  pen fd's on this
c79f0 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75   inode */.  stru
c7a00 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ct unixLockInfo 
c7a10 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20  *pLock;      /* 
c7a20 49 6e 66 6f 20 61 62 6f 75 74 20 6c 6f 63 6b 73  Info about locks
c7a30 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a   on this inode *
c7a40 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20  /.  int h;      
c7a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7a60 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65       /* The file
c7a70 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20   descriptor */. 
c7a80 20 69 6e 74 20 64 69 72 66 64 3b 20 20 20 20 20   int dirfd;     
c7a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7aa0 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69    /* File descri
c7ab0 70 74 6f 72 20 66 6f 72 20 74 68 65 20 64 69 72  ptor for the dir
c7ac0 65 63 74 6f 72 79 20 2a 2f 0a 20 20 75 6e 73 69  ectory */.  unsi
c7ad0 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79  gned char lockty
c7ae0 70 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  pe;          /* 
c7af0 54 68 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b  The type of lock
c7b00 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 64   held on this fd
c7b10 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 73 74 45 72   */.  int lastEr
c7b20 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  rno;            
c7b30 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e         /* The un
c7b40 69 78 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68  ix errno from th
c7b50 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72  e last I/O error
c7b60 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b   */.  void *lock
c7b70 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20  ingContext;     
c7b80 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e         /* Lockin
c7b90 67 20 73 74 79 6c 65 20 73 70 65 63 69 66 69 63  g style specific
c7ba0 20 73 74 61 74 65 20 2a 2f 0a 20 20 55 6e 69 78   state */.  Unix
c7bb0 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65  UnusedFd *pUnuse
c7bc0 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d;           /* 
c7bd0 50 72 65 2d 61 6c 6c 6f 63 61 74 65 64 20 55 6e  Pre-allocated Un
c7be0 69 78 55 6e 75 73 65 64 46 64 20 2a 2f 0a 20 20  ixUnusedFd */.  
c7bf0 69 6e 74 20 66 69 6c 65 46 6c 61 67 73 3b 20 20  int fileFlags;  
c7c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7c10 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 6f 75 73   /* Miscellanous
c7c20 20 66 6c 61 67 73 20 2a 2f 0a 23 69 66 20 53 51   flags */.#if SQ
c7c30 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
c7c40 49 4e 47 5f 53 54 59 4c 45 0a 20 20 69 6e 74 20  ING_STYLE.  int 
c7c50 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20  openFlags;      
c7c60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c7c70 54 68 65 20 66 6c 61 67 73 20 73 70 65 63 69 66  The flags specif
c7c80 69 65 64 20 61 74 20 6f 70 65 6e 28 29 20 2a 2f  ied at open() */
c7c90 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
c7ca0 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
c7cb0 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78   defined(__linux
c7cc0 5f 5f 29 0a 20 20 70 74 68 72 65 61 64 5f 74 20  __).  pthread_t 
c7cd0 74 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  tid;            
c7ce0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 68         /* The th
c7cf0 72 65 61 64 20 74 68 61 74 20 22 6f 77 6e 73 22  read that "owns"
c7d00 20 74 68 69 73 20 75 6e 69 78 46 69 6c 65 20 2a   this unixFile *
c7d10 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 4f 53 5f  /.#endif.#if OS_
c7d20 56 58 57 4f 52 4b 53 0a 20 20 69 6e 74 20 69 73  VXWORKS.  int is
c7d30 44 65 6c 65 74 65 3b 20 20 20 20 20 20 20 20 20  Delete;         
c7d40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
c7d50 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66  lete on close if
c7d60 20 74 72 75 65 20 2a 2f 0a 20 20 73 74 72 75 63   true */.  struc
c7d70 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  t vxworksFileId 
c7d80 2a 70 49 64 3b 20 20 20 20 20 20 20 2f 2a 20 55  *pId;       /* U
c7d90 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 2a 2f  nique file ID */
c7da0 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
c7db0 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 20  NDEBUG.  /* The 
c7dc0 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 76 61  next group of va
c7dd0 72 69 61 62 6c 65 73 20 61 72 65 20 75 73 65 64  riables are used
c7de0 20 74 6f 20 74 72 61 63 6b 20 77 68 65 74 68 65   to track whethe
c7df0 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a 20 20 2a  r or not the.  *
c7e00 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  * transaction co
c7e10 75 6e 74 65 72 20 69 6e 20 62 79 74 65 73 20 32  unter in bytes 2
c7e20 34 2d 32 37 20 6f 66 20 64 61 74 61 62 61 73 65  4-27 of database
c7e30 20 66 69 6c 65 73 20 61 72 65 20 75 70 64 61 74   files are updat
c7e40 65 64 0a 20 20 2a 2a 20 77 68 65 6e 65 76 65 72  ed.  ** whenever
c7e50 20 61 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65   any part of the
c7e60 20 64 61 74 61 62 61 73 65 20 63 68 61 6e 67 65   database change
c7e70 73 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  s.  An assertion
c7e80 20 66 61 75 6c 74 20 77 69 6c 6c 0a 20 20 2a 2a   fault will.  **
c7e90 20 6f 63 63 75 72 20 69 66 20 61 20 66 69 6c 65   occur if a file
c7ea0 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68   is updated with
c7eb0 6f 75 74 20 61 6c 73 6f 20 75 70 64 61 74 69 6e  out also updatin
c7ec0 67 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  g the transactio
c7ed0 6e 0a 20 20 2a 2a 20 63 6f 75 6e 74 65 72 2e 20  n.  ** counter. 
c7ee0 20 54 68 69 73 20 74 65 73 74 20 69 73 20 6d 61   This test is ma
c7ef0 64 65 20 74 6f 20 61 76 6f 69 64 20 6e 65 77 20  de to avoid new 
c7f00 70 72 6f 62 6c 65 6d 73 20 73 69 6d 69 6c 61 72  problems similar
c7f10 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 6f 6e 65   to the.  ** one
c7f20 20 64 65 73 63 72 69 62 65 64 20 62 79 20 74 69   described by ti
c7f30 63 6b 65 74 20 23 33 35 38 34 2e 20 0a 20 20 2a  cket #3584. .  *
c7f40 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
c7f50 72 20 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 3b  r transCntrChng;
c7f60 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
c7f70 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
c7f80 75 6e 74 65 72 20 63 68 61 6e 67 65 64 20 2a 2f  unter changed */
c7f90 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
c7fa0 20 64 62 55 70 64 61 74 65 3b 20 20 20 20 20 20   dbUpdate;      
c7fb0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 79    /* True if any
c7fc0 20 70 61 72 74 20 6f 66 20 64 61 74 61 62 61 73   part of databas
c7fd0 65 20 66 69 6c 65 20 63 68 61 6e 67 65 64 20 2a  e file changed *
c7fe0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
c7ff0 72 20 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3b  r inNormalWrite;
c8000 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e     /* True if in
c8010 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20   a normal write 
c8020 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 23 65 6e  operation */.#en
c8030 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
c8040 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 6e 20 74  E_TEST.  /* In t
c8050 65 73 74 20 6d 6f 64 65 2c 20 69 6e 63 72 65 61  est mode, increa
c8060 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  se the size of t
c8070 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 20  his structure a 
c8080 62 69 74 20 73 6f 20 74 68 61 74 20 0a 20 20 2a  bit so that .  *
c8090 2a 20 69 74 20 69 73 20 6c 61 72 67 65 72 20 74  * it is larger t
c80a0 68 61 6e 20 74 68 65 20 73 74 72 75 63 74 20 43  han the struct C
c80b0 72 61 73 68 46 69 6c 65 20 64 65 66 69 6e 65 64  rashFile defined
c80c0 20 69 6e 20 74 65 73 74 36 2e 63 2e 0a 20 20 2a   in test6.c..  *
c80d0 2f 0a 20 20 63 68 61 72 20 61 50 61 64 64 69 6e  /.  char aPaddin
c80e0 67 5b 33 32 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b  g[32];.#endif.};
c80f0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
c8100 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66  owing macros def
c8110 69 6e 65 20 62 69 74 73 20 69 6e 20 75 6e 69 78  ine bits in unix
c8120 46 69 6c 65 2e 66 69 6c 65 46 6c 61 67 73 0a 2a  File.fileFlags.*
c8130 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
c8140 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b  _WHOLE_FILE_LOCK
c8150 49 4e 47 20 20 30 78 30 30 30 31 20 20 20 2f 2a  ING  0x0001   /*
c8160 20 55 73 65 20 77 68 6f 6c 65 2d 66 69 6c 65 20   Use whole-file 
c8170 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a  locking */../*.*
c8180 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74  * Include code t
c8190 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f  hat is common to
c81a0 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65   all os_*.c file
c81b0 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.*/./**********
c81c0 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f  **** Include os_
c81d0 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20  common.h in the 
c81e0 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69  middle of os_uni
c81f0 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
c8200 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
c8210 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
c8220 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
c8230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8250 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
c8260 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65  May 22.**.** The
c8270 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
c8280 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
c8290 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
c82a0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
c82b0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
c82c0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
c82d0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
c82e0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
c82f0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
c8300 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
c8310 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
c8320 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
c8330 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
c8340 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
c8350 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
c8360 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
c8370 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
c8380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c83a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c83b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c83c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
c83d0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
c83e0 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61  ins macros and a
c83f0 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63   little bit of c
c8400 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d  ode that is comm
c8410 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20  on to.** all of 
c8420 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65  the platform-spe
c8430 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f  cific files (os_
c8440 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63  *.c) and is #inc
c8450 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65  luded into those
c8460 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a  .** files..**.**
c8470 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c   This file shoul
c8480 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62  d be #included b
c8490 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c  y the os_*.c fil
c84a0 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20  es only.  It is 
c84b0 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c  not a.** general
c84c0 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 20   purpose header 
c84d0 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  file..**.** $Id:
c84e0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31   os_common.h,v 1
c84f0 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34 20 31  .38 2009/02/24 1
c8500 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31  8:40:50 danielk1
c8510 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  977 Exp $.*/.#if
c8520 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f  ndef _OS_COMMON_
c8530 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43  H_.#define _OS_C
c8540 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  OMMON_H_../*.** 
c8550 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67  At least two bug
c8560 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69  s have slipped i
c8570 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61  n because we cha
c8580 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f  nged the MEMORY_
c8590 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74  DEBUG.** macro t
c85a0 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61  o SQLITE_DEBUG a
c85b0 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61  nd some older ma
c85c0 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74  kefiles have not
c85d0 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a   yet made the.**
c85e0 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f   switch.  The fo
c85f0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f  llowing code sho
c8600 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70  uld catch this p
c8610 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c  roblem at compil
c8620 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65  e-time..*/.#ifde
c8630 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23  f MEMORY_DEBUG.#
c8640 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f   error "The MEMO
c8650 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69  RY_DEBUG macro i
c8660 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65  s obsolete.  Use
c8670 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e   SQLITE_DEBUG in
c8680 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a  stead.".#endif..
c8690 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
c86a0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
c86b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53  TE int sqlite3OS
c86c0 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69  Trace = 0;.#defi
c86d0 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20  ne OSTRACE1(X)  
c86e0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
c86f0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
c8700 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
c8710 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c8720 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66  E2(X,Y)       if
c8730 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
c8740 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
c8750 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69  rintf(X,Y).#defi
c8760 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c  ne OSTRACE3(X,Y,
c8770 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  Z)     if( sqlit
c8780 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
c8790 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
c87a0 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Y,Z).#define OS
c87b0 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20  TRACE4(X,Y,Z,A) 
c87c0 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
c87d0 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
c87e0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
c87f0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
c8800 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69  CE5(X,Y,Z,A,B) i
c8810 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
c8820 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
c8830 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
c8840 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c8850 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20  E6(X,Y,Z,A,B,C) 
c8860 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33  \.    if(sqlite3
c8870 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33  OSTrace) sqlite3
c8880 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
c8890 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
c88a0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
c88b0 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69  A,B,C,D) \.    i
c88c0 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
c88d0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
c88e0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
c88f0 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  ,D).#else.#defin
c8900 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64  e OSTRACE1(X).#d
c8910 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58  efine OSTRACE2(X
c8920 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
c8930 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66  ACE3(X,Y,Z).#def
c8940 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59  ine OSTRACE4(X,Y
c8950 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Z,A).#define OS
c8960 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE5(X,Y,Z,A,B
c8970 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c8980 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a  E6(X,Y,Z,A,B,C).
c8990 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37  #define OSTRACE7
c89a0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a  (X,Y,Z,A,B,C,D).
c89b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
c89c0 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  cros for perform
c89d0 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e  ance tracing.  N
c89e0 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f  ormally turned o
c89f0 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a  ff.  Only works.
c8a00 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77  ** on i486 hardw
c8a10 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  are..*/.#ifdef S
c8a20 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43  QLITE_PERFORMANC
c8a30 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20  E_TRACE../* .** 
c8a40 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e  hwtime.h contain
c8a50 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c  s inline assembl
c8a60 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c  er code for impl
c8a70 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67  ementing .** hig
c8a80 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69  h-performance ti
c8a90 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  ming routines..*
c8aa0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c8ab0 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65  * Include hwtime
c8ac0 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
c8ad0 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   of os_common.h 
c8ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8af0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c8b00 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74  * Begin file hwt
c8b10 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ime.h **********
c8b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8b40 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79  /./*.** 2008 May
c8b50 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   27.**.** The au
c8b60 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
c8b70 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
c8b80 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
c8b90 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
c8ba0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
c8bb0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
c8bc0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
c8bd0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
c8be0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
c8bf0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
c8c00 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
c8c10 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
c8c20 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
c8c30 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
c8c40 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
c8c50 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
c8c60 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
c8c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8cb0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
c8cc0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
c8cd0 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65   inline asm code
c8ce0 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20   for retrieving 
c8cf0 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63  "high-performanc
c8d00 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66  e".** counters f
c8d10 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55  or x86 class CPU
c8d20 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77  s..**.** $Id: hw
c8d30 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30  time.h,v 1.3 200
c8d40 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35  8/08/01 14:33:15
c8d50 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a   shane Exp $.*/.
c8d60 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f  #ifndef _HWTIME_
c8d70 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49  H_.#define _HWTI
c8d80 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ME_H_../*.** The
c8d90 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
c8da0 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e  ne only works on
c8db0 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28   pentium-class (
c8dc0 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73  or newer) proces
c8dd0 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73  sors..** It uses
c8de0 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64   the RDTSC opcod
c8df0 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79  e to read the cy
c8e00 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20  cle count value 
c8e10 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72  out of the.** pr
c8e20 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75  ocessor and retu
c8e30 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20  rns that value. 
c8e40 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
c8e50 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a  d for high-res.*
c8e60 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a  * profiling..*/.
c8e70 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  #if (defined(__G
c8e80 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  NUC__) || define
c8e90 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20  d(_MSC_VER)) && 
c8ea0 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64  \.      (defined
c8eb0 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65  (i386) || define
c8ec0 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64  d(__i386__) || d
c8ed0 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29  efined(_M_IX86))
c8ee0 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28  ..  #if defined(
c8ef0 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69  __GNUC__)..  __i
c8f00 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
c8f10 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
c8f20 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
c8f30 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c  unsigned int lo,
c8f40 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f   hi;.     __asm_
c8f50 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
c8f60 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28  "rdtsc" : "=a" (
c8f70 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b  lo), "=d" (hi));
c8f80 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71  .     return (sq
c8f90 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c  lite_uint64)hi <
c8fa0 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a  < 32 | lo;.  }..
c8fb0 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28    #elif defined(
c8fc0 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64  _MSC_VER)..  __d
c8fd0 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f  eclspec(naked) _
c8fe0 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75  _inline sqlite_u
c8ff0 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71  int64 __cdecl sq
c9000 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
c9010 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a  ){.     __asm {.
c9020 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20          rdtsc.  
c9030 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20        ret       
c9040 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61  ; return value a
c9050 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d  t EDX:EAX.     }
c9060 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a  .  }..  #endif..
c9070 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f  #elif (defined(_
c9080 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69  _GNUC__) && defi
c9090 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29  ned(__x86_64__))
c90a0 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
c90b0 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
c90c0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
c90d0 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
c90e0 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20   long val;.     
c90f0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
c9100 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a  ile__ ("rdtsc" :
c9110 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20   "=A" (val));.  
c9120 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a      return val;.
c9130 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66    }. .#elif (def
c9140 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
c9150 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f  & defined(__ppc_
c9160 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f  _))..  __inline_
c9170 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  _ sqlite_uint64 
c9180 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
c9190 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67  id){.      unsig
c91a0 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65  ned long long re
c91b0 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69  tval;.      unsi
c91c0 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a  gned long junk;.
c91d0 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f        __asm__ __
c91e0 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c  volatile__ ("\n\
c91f0 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20  .          1:   
c9200 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c     mftbu   %1\n\
c9210 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c9220 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e     mftb    %L0\n
c9230 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
c9240 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e      mftbu   %0\n
c9250 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
c9260 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25      cmpw    %0,%
c9270 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
c9280 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31         bne     1
c9290 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b".             
c92a0 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74       : "=r" (ret
c92b0 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b  val), "=r" (junk
c92c0 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ));.      return
c92d0 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65   retval;.  }..#e
c92e0 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65  lse..  #error Ne
c92f0 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ed implementatio
c9300 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69  n of sqlite3Hwti
c9310 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
c9320 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20  atform...  /*.  
c9330 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69  ** To compile wi
c9340 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69  thout implementi
c9350 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  ng sqlite3Hwtime
c9360 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
c9370 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63  form,.  ** you c
c9380 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62  an remove the ab
c9390 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75  ove #error and u
c93a0 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  se the following
c93b0 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74  .  ** stub funct
c93c0 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c  ion.  You will l
c93d0 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f  ose timing suppo
c93e0 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a  rt for many.  **
c93f0 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e   of the debuggin
c9400 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74  g and testing ut
c9410 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20  ilities, but it 
c9420 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c  should at.  ** l
c9430 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64  east compile and
c9440 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54   run..  */.SQLIT
c9450 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
c9460 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
c9470 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72  3Hwtime(void){ r
c9480 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75  eturn ((sqlite_u
c9490 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e  int64)0); }..#en
c94a0 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
c94b0 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f  defined(_HWTIME_
c94c0 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  H_) */../*******
c94d0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68  ******* End of h
c94e0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
c94f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9510 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
c9520 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
c9530 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
c9540 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f   off in os_commo
c9550 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
c9560 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20  ******/..static 
c9570 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f  sqlite_uint64 g_
c9580 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71  start;.static sq
c9590 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c  lite_uint64 g_el
c95a0 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54  apsed;.#define T
c95b0 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20  IMER_START      
c95c0 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33   g_start=sqlite3
c95d0 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65  Hwtime().#define
c95e0 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20   TIMER_END      
c95f0 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c     g_elapsed=sql
c9600 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73  ite3Hwtime()-g_s
c9610 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d  tart.#define TIM
c9620 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67  ER_ELAPSED     g
c9630 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23  _elapsed.#else.#
c9640 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41  define TIMER_STA
c9650 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  RT.#define TIMER
c9660 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d  _END.#define TIM
c9670 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28  ER_ELAPSED     (
c9680 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
c9690 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
c96a0 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69  If we compile wi
c96b0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45  th the SQLITE_TE
c96c0 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68  ST macro set, th
c96d0 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  en the following
c96e0 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64   block.** of cod
c96f0 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74  e will give us t
c9700 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69  he ability to si
c9710 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f  mulate a disk I/
c9720 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a  O error.  This.*
c9730 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  * is used for te
c9740 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65  sting the I/O re
c9750 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f  covery logic..*/
c9760 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
c9770 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
c9780 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
c9790 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20  ror_hit = 0;    
c97a0 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
c97b0 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45   number of I/O E
c97c0 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f  rrors */.SQLITE_
c97d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c97e0 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74  io_error_hardhit
c97f0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
c9800 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65  Number of non-be
c9810 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53  nign errors */.S
c9820 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
c9830 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
c9840 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20  ending = 0;     
c9850 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e     /* Count down
c9860 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72   to first I/O er
c9870 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ror */.SQLITE_AP
c9880 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
c9890 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d  _error_persist =
c98a0 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
c98b0 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73  ue if I/O errors
c98c0 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49   persist */.SQLI
c98d0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c98e0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69  e3_io_error_beni
c98f0 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  gn = 0;         
c9900 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72  /* True if error
c9910 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a  s are benign */.
c9920 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
c9930 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
c9940 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c  pending = 0;.SQL
c9950 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c9960 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30  te3_diskfull = 0
c9970 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ;.#define Simula
c9980 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
c9990 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  X) sqlite3_io_er
c99a0 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23  ror_benign=(X).#
c99b0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
c99c0 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a  OError(CODE)  \.
c99d0 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69    if( (sqlite3_i
c99e0 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20  o_error_persist 
c99f0 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  && sqlite3_io_er
c9a00 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20  ror_hit) \.     
c9a10 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f    || sqlite3_io_
c9a20 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20  error_pending-- 
c9a30 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20  == 1 )  \.      
c9a40 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f          { local_
c9a50 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d  ioerr(); CODE; }
c9a60 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63  .static void loc
c9a70 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f  al_ioerr(){.  IO
c9a80 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22  TRACE(("IOERR\n"
c9a90 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f  ));.  sqlite3_io
c9aa0 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20  _error_hit++;.  
c9ab0 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f  if( !sqlite3_io_
c9ac0 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73  error_benign ) s
c9ad0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c9ae0 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65  hardhit++;.}.#de
c9af0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73  fine SimulateDis
c9b00 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29  kfullError(CODE)
c9b10 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65   \.   if( sqlite
c9b20 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
c9b30 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28  ng ){ \.     if(
c9b40 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
c9b50 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29  l_pending == 1 )
c9b60 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c  { \.       local
c9b70 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20  _ioerr(); \.    
c9b80 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66     sqlite3_diskf
c9b90 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20  ull = 1; \.     
c9ba0 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
c9bb0 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20  or_hit = 1; \.  
c9bc0 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20       CODE; \.   
c9bd0 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20    }else{ \.     
c9be0 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75    sqlite3_diskfu
c9bf0 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a  ll_pending--; \.
c9c00 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65       } \.   }.#e
c9c10 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  lse.#define Simu
c9c20 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
c9c30 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d  n(X).#define Sim
c9c40 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a  ulateIOError(A).
c9c50 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
c9c60 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29  DiskfullError(A)
c9c70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
c9c80 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65  hen testing, kee
c9c90 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65  p a count of the
c9ca0 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20   number of open 
c9cb0 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  files..*/.#ifdef
c9cc0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
c9cd0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c9ce0 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f  te3_open_file_co
c9cf0 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  unt = 0;.#define
c9d00 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20   OpenCounter(X) 
c9d10 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69   sqlite3_open_fi
c9d20 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65  le_count+=(X).#e
c9d30 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e  lse.#define Open
c9d40 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69  Counter(X).#endi
c9d50 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  f..#endif /* !de
c9d60 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e  fined(_OS_COMMON
c9d70 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _H_) */../******
c9d80 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
c9d90 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
c9da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9dc0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
c9dd0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
c9de0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
c9df0 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78  t off in os_unix
c9e00 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
c9e10 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
c9e20 44 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 6d  Define various m
c9e30 61 63 72 6f 73 20 74 68 61 74 20 61 72 65 20 6d  acros that are m
c9e40 69 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65  issing from some
c9e50 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66   systems..*/.#if
c9e60 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45  ndef O_LARGEFILE
c9e70 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47  .# define O_LARG
c9e80 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23  EFILE 0.#endif.#
c9e90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53  ifdef SQLITE_DIS
c9ea0 41 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66  ABLE_LFS.# undef
c9eb0 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64   O_LARGEFILE.# d
c9ec0 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c  efine O_LARGEFIL
c9ed0 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  E 0.#endif.#ifnd
c9ee0 65 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20  ef O_NOFOLLOW.# 
c9ef0 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f  define O_NOFOLLO
c9f00 57 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  W 0.#endif.#ifnd
c9f10 65 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65  ef O_BINARY.# de
c9f20 66 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a  fine O_BINARY 0.
c9f30 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
c9f40 65 20 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72  e DJGPP compiler
c9f50 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f   environment loo
c9f60 6b 73 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55  ks mostly like U
c9f70 6e 69 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c  nix, but it.** l
c9f80 61 63 6b 73 20 74 68 65 20 66 63 6e 74 6c 28 29  acks the fcntl()
c9f90 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53   system call.  S
c9fa0 6f 20 72 65 64 65 66 69 6e 65 20 66 63 6e 74 6c  o redefine fcntl
c9fb0 28 29 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69  () to be somethi
c9fc0 6e 67 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79  ng.** that alway
c9fd0 73 20 73 75 63 63 65 65 64 73 2e 20 20 54 68 69  s succeeds.  Thi
c9fe0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f 63  s means that loc
c9ff0 6b 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63  king does not oc
ca000 63 75 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47  cur under.** DJG
ca010 50 50 2e 20 20 42 75 74 20 69 74 20 69 73 20 44  PP.  But it is D
ca020 4f 53 20 2d 20 77 68 61 74 20 64 69 64 20 79 6f  OS - what did yo
ca030 75 20 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66  u expect?.*/.#if
ca040 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20  def __DJGPP__.# 
ca050 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42  define fcntl(A,B
ca060 2c 43 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ,C) 0.#endif../*
ca070 0a 2a 2a 20 54 68 65 20 74 68 72 65 61 64 69 64  .** The threadid
ca080 20 6d 61 63 72 6f 20 72 65 73 6f 6c 76 65 73 20   macro resolves 
ca090 74 6f 20 74 68 65 20 74 68 72 65 61 64 2d 69 64  to the thread-id
ca0a0 20 6f 72 20 74 6f 20 30 2e 20 20 55 73 65 64 20   or to 0.  Used 
ca0b0 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61  for.** testing a
ca0c0 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c  nd debugging onl
ca0d0 79 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  y..*/.#if SQLITE
ca0e0 5f 54 48 52 45 41 44 53 41 46 45 0a 23 64 65 66  _THREADSAFE.#def
ca0f0 69 6e 65 20 74 68 72 65 61 64 69 64 20 70 74 68  ine threadid pth
ca100 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73  read_self().#els
ca110 65 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61 64  e.#define thread
ca120 69 64 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  id 0.#endif.../*
ca130 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74  .** Helper funct
ca140 69 6f 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 61  ions to obtain a
ca150 6e 64 20 72 65 6c 69 6e 71 75 69 73 68 20 74 68  nd relinquish th
ca160 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 20  e global mutex. 
ca170 54 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75  The.** global mu
ca180 74 65 78 20 69 73 20 75 73 65 64 20 74 6f 20 70  tex is used to p
ca190 72 6f 74 65 63 74 20 74 68 65 20 75 6e 69 78 4f  rotect the unixO
ca1a0 70 65 6e 43 6e 74 2c 20 75 6e 69 78 4c 6f 63 6b  penCnt, unixLock
ca1b0 49 6e 66 6f 20 61 6e 64 0a 2a 2a 20 76 78 77 6f  Info and.** vxwo
ca1c0 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74  rksFileId object
ca1d0 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 66  s used by this f
ca1e0 69 6c 65 2c 20 61 6c 6c 20 6f 66 20 77 68 69 63  ile, all of whic
ca1f0 68 20 6d 61 79 20 62 65 20 0a 2a 2a 20 73 68 61  h may be .** sha
ca200 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20  red by multiple 
ca210 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46  threads..**.** F
ca220 75 6e 63 74 69 6f 6e 20 75 6e 69 78 4d 75 74 65  unction unixMute
ca230 78 48 65 6c 64 28 29 20 69 73 20 75 73 65 64 20  xHeld() is used 
ca240 74 6f 20 61 73 73 65 72 74 28 29 20 74 68 61 74  to assert() that
ca250 20 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65   the global mute
ca260 78 20 0a 2a 2a 20 69 73 20 68 65 6c 64 20 77 68  x .** is held wh
ca270 65 6e 20 72 65 71 75 69 72 65 64 2e 20 54 68 69  en required. Thi
ca280 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e  s function is on
ca290 6c 79 20 75 73 65 64 20 61 73 20 70 61 72 74 20  ly used as part 
ca2a0 6f 66 20 61 73 73 65 72 74 28 29 20 0a 2a 2a 20  of assert() .** 
ca2b0 73 74 61 74 65 6d 65 6e 74 73 2e 20 65 2e 67 2e  statements. e.g.
ca2c0 0a 2a 2a 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74  .**.**   unixEnt
ca2d0 65 72 4d 75 74 65 78 28 29 0a 2a 2a 20 20 20 20  erMutex().**    
ca2e0 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74   assert( unixMut
ca2f0 65 78 48 65 6c 64 28 29 20 29 3b 0a 2a 2a 20 20  exHeld() );.**  
ca300 20 75 6e 69 78 45 6e 74 65 72 4c 65 61 76 65 28   unixEnterLeave(
ca310 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ).*/.static void
ca320 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
ca330 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33  void){.  sqlite3
ca340 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c  _mutex_enter(sql
ca350 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
ca360 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
ca370 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 73  IC_MASTER));.}.s
ca380 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 4c  tatic void unixL
ca390 65 61 76 65 4d 75 74 65 78 28 76 6f 69 64 29 7b  eaveMutex(void){
ca3a0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
ca3b0 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75  _leave(sqlite3Mu
ca3c0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
ca3d0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
ca3e0 54 45 52 29 29 3b 0a 7d 0a 23 69 66 64 65 66 20  TER));.}.#ifdef 
ca3f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61  SQLITE_DEBUG.sta
ca400 74 69 63 20 69 6e 74 20 75 6e 69 78 4d 75 74 65  tic int unixMute
ca410 78 48 65 6c 64 28 76 6f 69 64 29 20 7b 0a 20 20  xHeld(void) {.  
ca420 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d  return sqlite3_m
ca430 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65  utex_held(sqlite
ca440 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
ca450 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
ca460 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 23 65 6e 64  MASTER));.}.#end
ca470 69 66 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  if...#ifdef SQLI
ca480 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48  TE_DEBUG./*.** H
ca490 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66  elper function f
ca4a0 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20  or printing out 
ca4b0 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f  trace informatio
ca4c0 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67  n from debugging
ca4d0 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68  .** binaries. Th
ca4e0 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  is returns the s
ca4f0 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 74  tring represetat
ca500 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c  ion of the suppl
ca510 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c  ied.** integer l
ca520 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61  ock-type..*/.sta
ca530 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
ca540 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74  locktypeName(int
ca550 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77   locktype){.  sw
ca560 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29  itch( locktype )
ca570 7b 0a 20 20 20 20 63 61 73 65 20 4e 4f 5f 4c 4f  {.    case NO_LO
ca580 43 4b 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45  CK: return "NONE
ca590 22 3b 0a 20 20 20 20 63 61 73 65 20 53 48 41 52  ";.    case SHAR
ca5a0 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20  ED_LOCK: return 
ca5b0 22 53 48 41 52 45 44 22 3b 0a 20 20 20 20 63 61  "SHARED";.    ca
ca5c0 73 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  se RESERVED_LOCK
ca5d0 3a 20 72 65 74 75 72 6e 20 22 52 45 53 45 52 56  : return "RESERV
ca5e0 45 44 22 3b 0a 20 20 20 20 63 61 73 65 20 50 45  ED";.    case PE
ca5f0 4e 44 49 4e 47 5f 4c 4f 43 4b 3a 20 72 65 74 75  NDING_LOCK: retu
ca600 72 6e 20 22 50 45 4e 44 49 4e 47 22 3b 0a 20 20  rn "PENDING";.  
ca610 20 20 63 61 73 65 20 45 58 43 4c 55 53 49 56 45    case EXCLUSIVE
ca620 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 45  _LOCK: return "E
ca630 58 43 4c 55 53 49 56 45 22 3b 0a 20 20 7d 0a 20  XCLUSIVE";.  }. 
ca640 20 72 65 74 75 72 6e 20 22 45 52 52 4f 52 22 3b   return "ERROR";
ca650 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  .}.#endif..#ifde
ca660 66 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52  f SQLITE_LOCK_TR
ca670 41 43 45 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  ACE./*.** Print 
ca680 6f 75 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  out information 
ca690 61 62 6f 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e  about all lockin
ca6a0 67 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a  g operations..**
ca6b0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
ca6c0 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 72 6f   is used for tro
ca6d0 75 62 6c 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63  ubleshooting loc
ca6e0 6b 73 20 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61  ks on multithrea
ca6f0 64 65 64 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73  ded.** platforms
ca700 2e 20 20 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d  .  Enable by com
ca710 70 69 6c 69 6e 67 20 77 69 74 68 20 74 68 65 20  piling with the 
ca720 2d 44 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52  -DSQLITE_LOCK_TR
ca730 41 43 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c  ACE.** command-l
ca740 69 6e 65 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68  ine option on th
ca750 65 20 63 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69  e compiler.  Thi
ca760 73 20 63 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c  s code is normal
ca770 6c 79 0a 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66  ly.** turned off
ca780 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
ca790 6c 6f 63 6b 54 72 61 63 65 28 69 6e 74 20 66 64  lockTrace(int fd
ca7a0 2c 20 69 6e 74 20 6f 70 2c 20 73 74 72 75 63 74  , int op, struct
ca7b0 20 66 6c 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68   flock *p){.  ch
ca7c0 61 72 20 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54  ar *zOpName, *zT
ca7d0 79 70 65 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20  ype;.  int s;.  
ca7e0 69 6e 74 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a  int savedErrno;.
ca7f0 20 20 69 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c    if( op==F_GETL
ca800 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65  K ){.    zOpName
ca810 20 3d 20 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65   = "GETLK";.  }e
ca820 6c 73 65 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45  lse if( op==F_SE
ca830 54 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61  TLK ){.    zOpNa
ca840 6d 65 20 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20  me = "SETLK";.  
ca850 7d 65 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20 66  }else{.    s = f
ca860 63 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b  cntl(fd, op, p);
ca870 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
ca880 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 75  gPrintf("fcntl u
ca890 6e 6b 6e 6f 77 6e 20 25 64 20 25 64 20 25 64 5c  nknown %d %d %d\
ca8a0 6e 22 2c 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a  n", fd, op, s);.
ca8b0 20 20 20 20 72 65 74 75 72 6e 20 73 3b 0a 20 20      return s;.  
ca8c0 7d 0a 20 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70  }.  if( p->l_typ
ca8d0 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20  e==F_RDLCK ){.  
ca8e0 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b    zType = "RDLCK
ca8f0 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  ";.  }else if( p
ca900 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43  ->l_type==F_WRLC
ca910 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d  K ){.    zType =
ca920 20 22 57 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73   "WRLCK";.  }els
ca930 65 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d  e if( p->l_type=
ca940 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_UNLCK ){.    
ca950 7a 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b  zType = "UNLCK";
ca960 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
ca970 73 65 72 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20  sert( 0 );.  }. 
ca980 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68   assert( p->l_wh
ca990 65 6e 63 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29  ence==SEEK_SET )
ca9a0 3b 0a 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64  ;.  s = fcntl(fd
ca9b0 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65  , op, p);.  save
ca9c0 64 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  dErrno = errno;.
ca9d0 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
ca9e0 69 6e 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25  intf("fcntl %d %
ca9f0 64 20 25 73 20 25 73 20 25 64 20 25 64 20 25 64  d %s %s %d %d %d
caa00 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72   %d\n",.     thr
caa10 65 61 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61  eadid, fd, zOpNa
caa20 6d 65 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29  me, zType, (int)
caa30 70 2d 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74  p->l_start, (int
caa40 29 70 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20  )p->l_len,.     
caa50 28 69 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73  (int)p->l_pid, s
caa60 29 3b 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29  );.  if( s==(-1)
caa70 20 26 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20   && op==F_SETLK 
caa80 26 26 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46  && (p->l_type==F
caa90 5f 52 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74  _RDLCK || p->l_t
caaa0 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b  ype==F_WRLCK) ){
caab0 0a 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63  .    struct floc
caac0 6b 20 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a  k l2;.    l2 = *
caad0 70 3b 0a 20 20 20 20 66 63 6e 74 6c 28 66 64 2c  p;.    fcntl(fd,
caae0 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a   F_GETLK, &l2);.
caaf0 20 20 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70      if( l2.l_typ
cab00 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20  e==F_RDLCK ){.  
cab10 20 20 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c      zType = "RDL
cab20 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  CK";.    }else i
cab30 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f  f( l2.l_type==F_
cab40 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a  WRLCK ){.      z
cab50 54 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a  Type = "WRLCK";.
cab60 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32      }else if( l2
cab70 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b  .l_type==F_UNLCK
cab80 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20   ){.      zType 
cab90 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d  = "UNLCK";.    }
caba0 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
cabb0 72 74 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20  rt( 0 );.    }. 
cabc0 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
cabd0 72 69 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69  rintf("fcntl-fai
cabe0 6c 75 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20  lure-reason: %s 
cabf0 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20  %d %d %d\n",.   
cac00 20 20 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29      zType, (int)
cac10 6c 32 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74  l2.l_start, (int
cac20 29 6c 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29  )l2.l_len, (int)
cac30 6c 32 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20  l2.l_pid);.  }. 
cac40 20 65 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72   errno = savedEr
cac50 72 6e 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b  rno;.  return s;
cac60 0a 7d 0a 23 64 65 66 69 6e 65 20 66 63 6e 74 6c  .}.#define fcntl
cac70 20 6c 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 69   lockTrace.#endi
cac80 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  f /* SQLITE_LOCK
cac90 5f 54 52 41 43 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a  _TRACE */..../*.
caca0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
cacb0 74 72 61 6e 73 6c 61 74 65 73 20 61 20 73 74 61  translates a sta
cacc0 6e 64 61 72 64 20 50 4f 53 49 58 20 65 72 72 6e  ndard POSIX errn
cacd0 6f 20 63 6f 64 65 20 69 6e 74 6f 20 73 6f 6d 65  o code into some
cace0 74 68 69 6e 67 0a 2a 2a 20 75 73 65 66 75 6c 20  thing.** useful 
cacf0 74 6f 20 74 68 65 20 63 6c 69 65 6e 74 73 20 6f  to the clients o
cad00 66 20 74 68 65 20 73 71 6c 69 74 65 33 20 66 75  f the sqlite3 fu
cad10 6e 63 74 69 6f 6e 73 2e 20 20 53 70 65 63 69 66  nctions.  Specif
cad20 69 63 61 6c 6c 79 2c 20 69 74 20 69 73 0a 2a 2a  ically, it is.**
cad30 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 74 72 61   intended to tra
cad40 6e 73 6c 61 74 65 20 61 20 76 61 72 69 65 74 79  nslate a variety
cad50 20 6f 66 20 22 74 72 79 20 61 67 61 69 6e 22 20   of "try again" 
cad60 65 72 72 6f 72 73 20 69 6e 74 6f 20 53 51 4c 49  errors into SQLI
cad70 54 45 5f 42 55 53 59 0a 2a 2a 20 61 6e 64 20 61  TE_BUSY.** and a
cad80 20 76 61 72 69 65 74 79 20 6f 66 20 22 70 6c 65   variety of "ple
cad90 61 73 65 20 63 6c 6f 73 65 20 74 68 65 20 66 69  ase close the fi
cada0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 4e 4f  le descriptor NO
cadb0 57 22 20 65 72 72 6f 72 73 20 69 6e 74 6f 20 0a  W" errors into .
cadc0 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a  ** SQLITE_IOERR.
cadd0 2a 2a 20 0a 2a 2a 20 45 72 72 6f 72 73 20 64 75  ** .** Errors du
cade0 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
cadf0 69 6f 6e 20 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72  ion of locks, or
cae00 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70   file system sup
cae10 70 6f 72 74 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a  port for locks,.
cae20 2a 2a 20 73 68 6f 75 6c 64 20 68 61 6e 64 6c 65  ** should handle
cae30 20 45 4e 4f 4c 43 4b 2c 20 45 4e 4f 54 53 55 50   ENOLCK, ENOTSUP
cae40 2c 20 45 4f 50 4e 4f 54 53 55 50 50 20 73 65 70  , EOPNOTSUPP sep
cae50 61 72 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74  arately..*/.stat
cae60 69 63 20 69 6e 74 20 73 71 6c 69 74 65 45 72 72  ic int sqliteErr
cae70 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
cae80 28 69 6e 74 20 70 6f 73 69 78 45 72 72 6f 72 2c  (int posixError,
cae90 20 69 6e 74 20 73 71 6c 69 74 65 49 4f 45 72 72   int sqliteIOErr
caea0 29 20 7b 0a 20 20 73 77 69 74 63 68 20 28 70 6f  ) {.  switch (po
caeb0 73 69 78 45 72 72 6f 72 29 20 7b 0a 20 20 63 61  sixError) {.  ca
caec0 73 65 20 30 3a 20 0a 20 20 20 20 72 65 74 75 72  se 0: .    retur
caed0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
caee0 20 0a 20 20 63 61 73 65 20 45 41 47 41 49 4e 3a   .  case EAGAIN:
caef0 0a 20 20 63 61 73 65 20 45 54 49 4d 45 44 4f 55  .  case ETIMEDOU
caf00 54 3a 0a 20 20 63 61 73 65 20 45 42 55 53 59 3a  T:.  case EBUSY:
caf10 0a 20 20 63 61 73 65 20 45 49 4e 54 52 3a 0a 20  .  case EINTR:. 
caf20 20 63 61 73 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a   case ENOLCK:  .
caf30 20 20 20 20 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46      /* random NF
caf40 53 20 72 65 74 72 79 20 65 72 72 6f 72 2c 20 75  S retry error, u
caf50 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c  nless during fil
caf60 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74  e system support
caf70 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 70   .     * introsp
caf80 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68  ection, in which
caf90 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61   it actually mea
cafa0 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20  ns what it says 
cafb0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  */.    return SQ
cafc0 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 0a  LITE_BUSY;.    .
cafd0 20 20 63 61 73 65 20 45 41 43 43 45 53 3a 20 0a    case EACCES: .
cafe0 20 20 20 20 2f 2a 20 45 41 43 43 45 53 20 69 73      /* EACCES is
caff0 20 6c 69 6b 65 20 45 41 47 41 49 4e 20 64 75 72   like EAGAIN dur
cb000 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72  ing locking oper
cb010 61 74 69 6f 6e 73 2c 20 62 75 74 20 6e 6f 74 20  ations, but not 
cb020 61 6e 79 20 6f 74 68 65 72 20 74 69 6d 65 2a 2f  any other time*/
cb030 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74 65  .    if( (sqlite
cb040 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f  IOErr == SQLITE_
cb050 49 4f 45 52 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a  IOERR_LOCK) || .
cb060 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d  .(sqliteIOErr ==
cb070 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
cb080 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69  LOCK) || ..(sqli
cb090 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54  teIOErr == SQLIT
cb0a0 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 20  E_IOERR_RDLOCK) 
cb0b0 7c 7c 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 72  ||..(sqliteIOErr
cb0c0 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52   == SQLITE_IOERR
cb0d0 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f  _CHECKRESERVEDLO
cb0e0 43 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  CK) ){.      ret
cb0f0 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
cb100 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c  .    }.    /* el
cb110 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20  se fall through 
cb120 2a 2f 0a 20 20 63 61 73 65 20 45 50 45 52 4d 3a  */.  case EPERM:
cb130 20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c   .    return SQL
cb140 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 0a 20  ITE_PERM;.    . 
cb150 20 63 61 73 65 20 45 44 45 41 44 4c 4b 3a 0a 20   case EDEADLK:. 
cb160 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
cb170 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a  _IOERR_BLOCKED;.
cb180 20 20 20 20 0a 23 69 66 20 45 4f 50 4e 4f 54 53      .#if EOPNOTS
cb190 55 50 50 21 3d 45 4e 4f 54 53 55 50 0a 20 20 63  UPP!=ENOTSUP.  c
cb1a0 61 73 65 20 45 4f 50 4e 4f 54 53 55 50 50 3a 20  ase EOPNOTSUPP: 
cb1b0 0a 20 20 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e  .    /* somethin
cb1c0 67 20 77 65 6e 74 20 74 65 72 72 69 62 6c 79 20  g went terribly 
cb1d0 61 77 72 79 2c 20 75 6e 6c 65 73 73 20 64 75 72  awry, unless dur
cb1e0 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20  ing file system 
cb1f0 73 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20  support .     * 
cb200 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69  introspection, i
cb210 6e 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61  n which it actua
cb220 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69  lly means what i
cb230 74 20 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66  t says */.#endif
cb240 0a 23 69 66 64 65 66 20 45 4e 4f 54 53 55 50 0a  .#ifdef ENOTSUP.
cb250 20 20 63 61 73 65 20 45 4e 4f 54 53 55 50 3a 20    case ENOTSUP: 
cb260 0a 20 20 20 20 2f 2a 20 69 6e 76 61 6c 69 64 20  .    /* invalid 
cb270 66 64 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e  fd, unless durin
cb280 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75  g file system su
cb290 70 70 6f 72 74 20 69 6e 74 72 6f 73 70 65 63 74  pport introspect
cb2a0 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 0a 20  ion, in which . 
cb2b0 20 20 20 20 2a 20 69 74 20 61 63 74 75 61 6c 6c      * it actuall
cb2c0 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20  y means what it 
cb2d0 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  says */.#endif. 
cb2e0 20 63 61 73 65 20 45 49 4f 3a 0a 20 20 63 61 73   case EIO:.  cas
cb2f0 65 20 45 42 41 44 46 3a 0a 20 20 63 61 73 65 20  e EBADF:.  case 
cb300 45 49 4e 56 41 4c 3a 0a 20 20 63 61 73 65 20 45  EINVAL:.  case E
cb310 4e 4f 54 43 4f 4e 4e 3a 0a 20 20 63 61 73 65 20  NOTCONN:.  case 
cb320 45 4e 4f 44 45 56 3a 0a 20 20 63 61 73 65 20 45  ENODEV:.  case E
cb330 4e 58 49 4f 3a 0a 20 20 63 61 73 65 20 45 4e 4f  NXIO:.  case ENO
cb340 45 4e 54 3a 0a 20 20 63 61 73 65 20 45 53 54 41  ENT:.  case ESTA
cb350 4c 45 3a 0a 20 20 63 61 73 65 20 45 4e 4f 53 59  LE:.  case ENOSY
cb360 53 3a 0a 20 20 20 20 2f 2a 20 74 68 65 73 65 20  S:.    /* these 
cb370 73 68 6f 75 6c 64 20 66 6f 72 63 65 20 74 68 65  should force the
cb380 20 63 6c 69 65 6e 74 20 74 6f 20 63 6c 6f 73 65   client to close
cb390 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 72 65   the file and re
cb3a0 63 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 20 20 0a  connect */.    .
cb3b0 20 20 64 65 66 61 75 6c 74 3a 20 0a 20 20 20 20    default: .    
cb3c0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 49 4f 45  return sqliteIOE
cb3d0 72 72 3b 0a 20 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a  rr;.  }.}..../**
cb3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
cb430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
cb440 42 65 67 69 6e 20 55 6e 69 71 75 65 20 46 69 6c  Begin Unique Fil
cb450 65 20 49 44 20 55 74 69 6c 69 74 79 20 55 73 65  e ID Utility Use
cb460 64 20 42 79 20 56 78 57 6f 72 6b 73 20 2a 2a 2a  d By VxWorks ***
cb470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
cb480 2a 2a 20 4f 6e 20 6d 6f 73 74 20 76 65 72 73 69  ** On most versi
cb490 6f 6e 73 20 6f 66 20 75 6e 69 78 2c 20 77 65 20  ons of unix, we 
cb4a0 63 61 6e 20 67 65 74 20 61 20 75 6e 69 71 75 65  can get a unique
cb4b0 20 49 44 20 66 6f 72 20 61 20 66 69 6c 65 20 62   ID for a file b
cb4c0 79 20 63 6f 6e 63 61 74 65 6e 61 74 69 6e 67 0a  y concatenating.
cb4d0 2a 2a 20 74 68 65 20 64 65 76 69 63 65 20 6e 75  ** the device nu
cb4e0 6d 62 65 72 20 61 6e 64 20 74 68 65 20 69 6e 6f  mber and the ino
cb4f0 64 65 20 6e 75 6d 62 65 72 2e 20 20 42 75 74 20  de number.  But 
cb500 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f  this does not wo
cb510 72 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a  rk on VxWorks..*
cb520 2a 20 4f 6e 20 56 78 57 6f 72 6b 73 2c 20 61 20  * On VxWorks, a 
cb530 75 6e 69 71 75 65 20 66 69 6c 65 20 69 64 20 6d  unique file id m
cb540 75 73 74 20 62 65 20 62 61 73 65 64 20 6f 6e 20  ust be based on 
cb550 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69  the canonical fi
cb560 6c 65 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20  lename..**.** A 
cb570 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e  pointer to an in
cb580 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
cb590 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
cb5a0 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73  e can be used as
cb5b0 20 61 0a 2a 2a 20 75 6e 69 71 75 65 20 66 69 6c   a.** unique fil
cb5c0 65 20 49 44 20 69 6e 20 56 78 57 6f 72 6b 73 2e  e ID in VxWorks.
cb5d0 20 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65 20    Each instance 
cb5e0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
cb5f0 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20  e contains.** a 
cb600 63 6f 70 79 20 6f 66 20 74 68 65 20 63 61 6e 6f  copy of the cano
cb610 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 20  nical filename. 
cb620 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61   There is also a
cb630 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
cb640 2e 20 20 0a 2a 2a 20 54 68 65 20 73 74 72 75 63  .  .** The struc
cb650 74 75 72 65 20 69 73 20 72 65 63 6c 61 69 6d 65  ture is reclaime
cb660 64 20 77 68 65 6e 20 74 68 65 20 6e 75 6d 62 65  d when the numbe
cb670 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  r of pointers to
cb680 20 69 74 20 64 72 6f 70 73 20 74 6f 0a 2a 2a 20   it drops to.** 
cb690 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  zero..**.** Ther
cb6a0 65 20 61 72 65 20 6e 65 76 65 72 20 76 65 72 79  e are never very
cb6b0 20 6d 61 6e 79 20 66 69 6c 65 73 20 6f 70 65 6e   many files open
cb6c0 20 61 74 20 6f 6e 65 20 74 69 6d 65 20 61 6e 64   at one time and
cb6d0 20 6c 6f 6f 6b 75 70 73 20 61 72 65 20 6e 6f 74   lookups are not
cb6e0 0a 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63  .** a performanc
cb6f0 65 2d 63 72 69 74 69 63 61 6c 20 70 61 74 68 2c  e-critical path,
cb700 20 73 6f 20 69 74 20 69 73 20 73 75 66 66 69 63   so it is suffic
cb710 69 65 6e 74 20 74 6f 20 70 75 74 20 74 68 65 73  ient to put thes
cb720 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20  e.** structures 
cb730 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  on a linked list
cb740 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 76 78 77 6f  ..*/.struct vxwo
cb750 72 6b 73 46 69 6c 65 49 64 20 7b 0a 20 20 73 74  rksFileId {.  st
cb760 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
cb770 49 64 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e  Id *pNext;  /* N
cb780 65 78 74 20 69 6e 20 61 20 6c 69 73 74 20 6f 66  ext in a list of
cb790 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 69   them all */.  i
cb7a0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
cb7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
cb7c0 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65  Number of refere
cb7d0 6e 63 65 73 20 74 6f 20 74 68 69 73 20 6f 6e 65  nces to this one
cb7e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b   */.  int nName;
cb7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb800 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
cb810 20 74 68 65 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e   the zCanonicalN
cb820 61 6d 65 5b 5d 20 73 74 72 69 6e 67 20 2a 2f 0a  ame[] string */.
cb830 20 20 63 68 61 72 20 2a 7a 43 61 6e 6f 6e 69 63    char *zCanonic
cb840 61 6c 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  alName;         
cb850 2f 2a 20 43 61 6e 6f 6e 69 63 61 6c 20 66 69 6c  /* Canonical fil
cb860 65 6e 61 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 69 66  ename */.};..#if
cb870 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 2f 2a 20 0a   OS_VXWORKS./* .
cb880 2a 2a 20 41 6c 6c 20 75 6e 69 71 75 65 20 66 69  ** All unique fi
cb890 6c 65 6e 61 6d 65 73 20 61 72 65 20 68 65 6c 64  lenames are held
cb8a0 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   on a linked lis
cb8b0 74 20 68 65 61 64 65 64 20 62 79 20 74 68 69 73  t headed by this
cb8c0 0a 2a 2a 20 76 61 72 69 61 62 6c 65 3a 0a 2a 2f  .** variable:.*/
cb8d0 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 76  .static struct v
cb8e0 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78  xworksFileId *vx
cb8f0 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20  worksFileList = 
cb900 30 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d 70 6c 69  0;../*.** Simpli
cb910 66 79 20 61 20 66 69 6c 65 6e 61 6d 65 20 69 6e  fy a filename in
cb920 74 6f 20 69 74 73 20 63 61 6e 6f 6e 69 63 61 6c  to its canonical
cb930 20 66 6f 72 6d 0a 2a 2a 20 62 79 20 6d 61 6b 69   form.** by maki
cb940 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ng the following
cb950 20 63 68 61 6e 67 65 73 3a 0a 2a 2a 0a 2a 2a 20   changes:.**.** 
cb960 20 2a 20 72 65 6d 6f 76 69 6e 67 20 61 6e 79 20   * removing any 
cb970 74 72 61 69 6c 69 6e 67 20 61 6e 64 20 64 75 70  trailing and dup
cb980 6c 69 63 61 74 65 20 2f 0a 2a 2a 20 20 2a 20 63  licate /.**  * c
cb990 6f 6e 76 65 72 74 20 2f 2e 2f 20 69 6e 74 6f 20  onvert /./ into 
cb9a0 6a 75 73 74 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e  just /.**  * con
cb9b0 76 65 72 74 20 2f 41 2f 2e 2e 2f 20 77 68 65 72  vert /A/../ wher
cb9c0 65 20 41 20 69 73 20 61 6e 79 20 73 69 6d 70 6c  e A is any simpl
cb9d0 65 20 6e 61 6d 65 20 69 6e 74 6f 20 6a 75 73 74  e name into just
cb9e0 20 2f 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73   /.**.** Changes
cb9f0 20 61 72 65 20 6d 61 64 65 20 69 6e 2d 70 6c 61   are made in-pla
cba00 63 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  ce.  Return the 
cba10 6e 65 77 20 6e 61 6d 65 20 6c 65 6e 67 74 68 2e  new name length.
cba20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69  .**.** The origi
cba30 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  nal filename is 
cba40 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 2e 20 20 52  in z[0..n-1].  R
cba50 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
cba60 20 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74 65 72   of.** character
cba70 73 20 69 6e 20 74 68 65 20 73 69 6d 70 6c 69 66  s in the simplif
cba80 69 65 64 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61  ied name..*/.sta
cba90 74 69 63 20 69 6e 74 20 76 78 77 6f 72 6b 73 53  tic int vxworksS
cbaa0 69 6d 70 6c 69 66 79 4e 61 6d 65 28 63 68 61 72  implifyName(char
cbab0 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69   *z, int n){.  i
cbac0 6e 74 20 69 2c 20 6a 3b 0a 20 20 77 68 69 6c 65  nt i, j;.  while
cbad0 28 20 6e 3e 31 20 26 26 20 7a 5b 6e 2d 31 5d 3d  ( n>1 && z[n-1]=
cbae0 3d 27 2f 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20  ='/' ){ n--; }. 
cbaf0 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 6e 3b   for(i=j=0; i<n;
cbb00 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a   i++){.    if( z
cbb10 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20  [i]=='/' ){.    
cbb20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2f    if( z[i+1]=='/
cbb30 27 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ' ) continue;.  
cbb40 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d      if( z[i+1]==
cbb50 27 2e 27 20 26 26 20 69 2b 32 3c 6e 20 26 26 20  '.' && i+2<n && 
cbb60 7a 5b 69 2b 32 5d 3d 3d 27 2f 27 20 29 7b 0a 20  z[i+2]=='/' ){. 
cbb70 20 20 20 20 20 20 20 69 20 2b 3d 20 31 3b 0a 20         i += 1;. 
cbb80 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
cbb90 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
cbba0 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26  f( z[i+1]=='.' &
cbbb0 26 20 69 2b 33 3c 6e 20 26 26 20 7a 5b 69 2b 32  & i+3<n && z[i+2
cbbc0 5d 3d 3d 27 2e 27 20 26 26 20 7a 5b 69 2b 33 5d  ]=='.' && z[i+3]
cbbd0 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20  =='/' ){.       
cbbe0 20 77 68 69 6c 65 28 20 6a 3e 30 20 26 26 20 7a   while( j>0 && z
cbbf0 5b 6a 2d 31 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d  [j-1]!='/' ){ j-
cbc00 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28  -; }.        if(
cbc10 20 6a 3e 30 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20   j>0 ){ j--; }. 
cbc20 20 20 20 20 20 20 20 69 20 2b 3d 20 32 3b 0a 20         i += 2;. 
cbc30 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
cbc40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
cbc50 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d     z[j++] = z[i]
cbc60 3b 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30  ;.  }.  z[j] = 0
cbc70 3b 0a 20 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a  ;.  return j;.}.
cbc80 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 75 6e  ./*.** Find a un
cbc90 69 71 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72  ique file ID for
cbca0 20 74 68 65 20 67 69 76 65 6e 20 61 62 73 6f 6c   the given absol
cbcb0 75 74 65 20 70 61 74 68 6e 61 6d 65 2e 20 20 52  ute pathname.  R
cbcc0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
cbcd0 65 72 20 74 6f 20 74 68 65 20 76 78 77 6f 72 6b  er to the vxwork
cbce0 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20  sFileId object. 
cbcf0 20 54 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73   This pointer is
cbd00 20 74 68 65 20 75 6e 69 71 75 65 0a 2a 2a 20 66   the unique.** f
cbd10 69 6c 65 20 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68  ile ID..**.** Th
cbd20 65 20 6e 52 65 66 20 66 69 65 6c 64 20 6f 66 20  e nRef field of 
cbd30 74 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  the vxworksFileI
cbd40 64 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 63 72  d object is incr
cbd50 65 6d 65 6e 74 65 64 20 62 65 66 6f 72 65 0a 2a  emented before.*
cbd60 2a 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20  * the object is 
cbd70 72 65 74 75 72 6e 65 64 2e 20 20 41 20 6e 65 77  returned.  A new
cbd80 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f   vxworksFileId o
cbd90 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64  bject is created
cbda0 0a 2a 2a 20 61 6e 64 20 61 64 64 65 64 20 74 6f  .** and added to
cbdb0 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74   the global list
cbdc0 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   if necessary..*
cbdd0 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79  *.** If a memory
cbde0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
cbdf0 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e  r occurs, return
cbe00 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63   NULL..*/.static
cbe10 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46   struct vxworksF
cbe20 69 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69  ileId *vxworksFi
cbe30 6e 64 46 69 6c 65 49 64 28 63 6f 6e 73 74 20 63  ndFileId(const c
cbe40 68 61 72 20 2a 7a 41 62 73 6f 6c 75 74 65 4e 61  har *zAbsoluteNa
cbe50 6d 65 29 7b 0a 20 20 73 74 72 75 63 74 20 76 78  me){.  struct vx
cbe60 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65  worksFileId *pNe
cbe70 77 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 65  w;         /* se
cbe80 61 72 63 68 20 6b 65 79 20 61 6e 64 20 6e 65 77  arch key and new
cbe90 20 66 69 6c 65 20 49 44 20 2a 2f 0a 20 20 73 74   file ID */.  st
cbea0 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
cbeb0 49 64 20 2a 70 43 61 6e 64 69 64 61 74 65 3b 20  Id *pCandidate; 
cbec0 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67    /* For looping
cbed0 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 66   over existing f
cbee0 69 6c 65 20 49 44 73 20 2a 2f 0a 20 20 69 6e 74  ile IDs */.  int
cbef0 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   n;             
cbf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cbf10 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 41   /* Length of zA
cbf20 62 73 6f 6c 75 74 65 4e 61 6d 65 20 73 74 72 69  bsoluteName stri
cbf30 6e 67 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ng */..  assert(
cbf40 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 5b 30   zAbsoluteName[0
cbf50 5d 3d 3d 27 2f 27 20 29 3b 0a 20 20 6e 20 3d 20  ]=='/' );.  n = 
cbf60 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 41 62 73  (int)strlen(zAbs
cbf70 6f 6c 75 74 65 4e 61 6d 65 29 3b 0a 20 20 70 4e  oluteName);.  pN
cbf80 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ew = sqlite3_mal
cbf90 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65  loc( sizeof(*pNe
cbfa0 77 29 20 2b 20 28 6e 2b 31 29 20 29 3b 0a 20 20  w) + (n+1) );.  
cbfb0 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65  if( pNew==0 ) re
cbfc0 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  turn 0;.  pNew->
cbfd0 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d  zCanonicalName =
cbfe0 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d   (char*)&pNew[1]
cbff0 3b 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d  ;.  memcpy(pNew-
cc000 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c  >zCanonicalName,
cc010 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 2c 20   zAbsoluteName, 
cc020 6e 2b 31 29 3b 0a 20 20 6e 20 3d 20 76 78 77 6f  n+1);.  n = vxwo
cc030 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28  rksSimplifyName(
cc040 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c  pNew->zCanonical
cc050 4e 61 6d 65 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20  Name, n);..  /* 
cc060 53 65 61 72 63 68 20 66 6f 72 20 61 6e 20 65 78  Search for an ex
cc070 69 73 74 69 6e 67 20 65 6e 74 72 79 20 74 68 61  isting entry tha
cc080 74 20 6d 61 74 63 68 69 6e 67 20 74 68 65 20 63  t matching the c
cc090 61 6e 6f 6e 69 63 61 6c 20 6e 61 6d 65 2e 0a 20  anonical name.. 
cc0a0 20 2a 2a 20 49 66 20 66 6f 75 6e 64 2c 20 69 6e   ** If found, in
cc0b0 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65  crement the refe
cc0c0 72 65 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20  rence count and 
cc0d0 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
cc0e0 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 65 78 69   to.  ** the exi
cc0f0 73 74 69 6e 67 20 66 69 6c 65 20 49 44 2e 0a 20  sting file ID.. 
cc100 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d   */.  unixEnterM
cc110 75 74 65 78 28 29 3b 0a 20 20 66 6f 72 28 70 43  utex();.  for(pC
cc120 61 6e 64 69 64 61 74 65 3d 76 78 77 6f 72 6b 73  andidate=vxworks
cc130 46 69 6c 65 4c 69 73 74 3b 20 70 43 61 6e 64 69  FileList; pCandi
cc140 64 61 74 65 3b 20 70 43 61 6e 64 69 64 61 74 65  date; pCandidate
cc150 3d 70 43 61 6e 64 69 64 61 74 65 2d 3e 70 4e 65  =pCandidate->pNe
cc160 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 61  xt){.    if( pCa
cc170 6e 64 69 64 61 74 65 2d 3e 6e 4e 61 6d 65 3d 3d  ndidate->nName==
cc180 6e 20 0a 20 20 20 20 20 26 26 20 6d 65 6d 63 6d  n .     && memcm
cc190 70 28 70 43 61 6e 64 69 64 61 74 65 2d 3e 7a 43  p(pCandidate->zC
cc1a0 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 70 4e  anonicalName, pN
cc1b0 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61  ew->zCanonicalNa
cc1c0 6d 65 2c 20 6e 29 3d 3d 30 0a 20 20 20 20 29 7b  me, n)==0.    ){
cc1d0 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
cc1e0 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20  free(pNew);.    
cc1f0 20 20 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e     pCandidate->n
cc200 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 75 6e  Ref++;.       un
cc210 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
cc220 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 43         return pC
cc230 61 6e 64 69 64 61 74 65 3b 0a 20 20 20 20 7d 0a  andidate;.    }.
cc240 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 61 74    }..  /* No mat
cc250 63 68 20 77 61 73 20 66 6f 75 6e 64 2e 20 20 57  ch was found.  W
cc260 65 20 77 69 6c 6c 20 6d 61 6b 65 20 61 20 6e 65  e will make a ne
cc270 77 20 66 69 6c 65 20 49 44 20 2a 2f 0a 20 20 70  w file ID */.  p
cc280 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  New->nRef = 1;. 
cc290 20 70 4e 65 77 2d 3e 6e 4e 61 6d 65 20 3d 20 6e   pNew->nName = n
cc2a0 3b 0a 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20  ;.  pNew->pNext 
cc2b0 3d 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73  = vxworksFileLis
cc2c0 74 3b 0a 20 20 76 78 77 6f 72 6b 73 46 69 6c 65  t;.  vxworksFile
cc2d0 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 75  List = pNew;.  u
cc2e0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
cc2f0 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
cc300 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65  }../*.** Decreme
cc310 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  nt the reference
cc320 20 63 6f 75 6e 74 20 6f 6e 20 61 20 76 78 77 6f   count on a vxwo
cc330 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74  rksFileId object
cc340 2e 20 20 46 72 65 65 0a 2a 2a 20 74 68 65 20 6f  .  Free.** the o
cc350 62 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 72  bject when the r
cc360 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72  eference count r
cc370 65 61 63 68 65 73 20 7a 65 72 6f 2e 0a 2a 2f 0a  eaches zero..*/.
cc380 73 74 61 74 69 63 20 76 6f 69 64 20 76 78 77 6f  static void vxwo
cc390 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64  rksReleaseFileId
cc3a0 28 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46  (struct vxworksF
cc3b0 69 6c 65 49 64 20 2a 70 49 64 29 7b 0a 20 20 75  ileId *pId){.  u
cc3c0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
cc3d0 0a 20 20 61 73 73 65 72 74 28 20 70 49 64 2d 3e  .  assert( pId->
cc3e0 6e 52 65 66 3e 30 20 29 3b 0a 20 20 70 49 64 2d  nRef>0 );.  pId-
cc3f0 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70  >nRef--;.  if( p
cc400 49 64 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20  Id->nRef==0 ){. 
cc410 20 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b     struct vxwork
cc420 73 46 69 6c 65 49 64 20 2a 2a 70 70 3b 0a 20 20  sFileId **pp;.  
cc430 20 20 66 6f 72 28 70 70 3d 26 76 78 77 6f 72 6b    for(pp=&vxwork
cc440 73 46 69 6c 65 4c 69 73 74 3b 20 2a 70 70 20 26  sFileList; *pp &
cc450 26 20 2a 70 70 21 3d 70 49 64 3b 20 70 70 20 3d  & *pp!=pId; pp =
cc460 20 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29   &((*pp)->pNext)
cc470 29 7b 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20  ){}.    assert( 
cc480 2a 70 70 3d 3d 70 49 64 20 29 3b 0a 20 20 20 20  *pp==pId );.    
cc490 2a 70 70 20 3d 20 70 49 64 2d 3e 70 4e 65 78 74  *pp = pId->pNext
cc4a0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
cc4b0 65 65 28 70 49 64 29 3b 0a 20 20 7d 0a 20 20 75  ee(pId);.  }.  u
cc4c0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
cc4d0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f  .}.#endif /* OS_
cc4e0 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a  VXWORKS */./****
cc4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
cc500 6f 66 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49  of Unique File I
cc510 44 20 55 74 69 6c 69 74 79 20 55 73 65 64 20 42  D Utility Used B
cc520 79 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a  y VxWorks ******
cc530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
cc540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a  *********/.../**
cc590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 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 0a 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 20 50 6f 73 69 78 20 41  ******** Posix A
cc600 64 76 69 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 20  dvisory Locking 
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 0a 2a 2a 0a  ************.**.
cc630 2a 2a 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72  ** POSIX advisor
cc640 79 20 6c 6f 63 6b 73 20 61 72 65 20 62 72 6f 6b  y locks are brok
cc650 65 6e 20 62 79 20 64 65 73 69 67 6e 2e 20 20 41  en by design.  A
cc660 4e 53 49 20 53 54 44 20 31 30 30 33 2e 31 20 28  NSI STD 1003.1 (
cc670 31 39 39 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e  1996).** section
cc680 20 36 2e 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34   6.5.2.2 lines 4
cc690 38 33 20 74 68 72 6f 75 67 68 20 34 39 30 20 73  83 through 490 s
cc6a0 70 65 63 69 66 79 20 74 68 61 74 20 77 68 65 6e  pecify that when
cc6b0 20 61 20 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65   a process.** se
cc6c0 74 73 20 6f 72 20 63 6c 65 61 72 73 20 61 20 6c  ts or clears a l
cc6d0 6f 63 6b 2c 20 74 68 61 74 20 6f 70 65 72 61 74  ock, that operat
cc6e0 69 6f 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e  ion overrides an
cc6f0 79 20 70 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65  y prior locks se
cc700 74 0a 2a 2a 20 62 79 20 74 68 65 20 73 61 6d 65  t.** by the same
cc710 20 70 72 6f 63 65 73 73 2e 20 20 49 74 20 64 6f   process.  It do
cc720 65 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c  es not explicitl
cc730 79 20 73 61 79 20 73 6f 2c 20 62 75 74 20 74 68  y say so, but th
cc740 69 73 20 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68  is implies.** th
cc750 61 74 20 69 74 20 6f 76 65 72 72 69 64 65 73 20  at it overrides 
cc760 6c 6f 63 6b 73 20 73 65 74 20 62 79 20 74 68 65  locks set by the
cc770 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 75 73   same process us
cc780 69 6e 67 20 61 20 64 69 66 66 65 72 65 6e 74 0a  ing a different.
cc790 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ** file descript
cc7a0 6f 72 2e 20 20 43 6f 6e 73 69 64 65 72 20 74 68  or.  Consider th
cc7b0 69 73 20 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a  is test case:.**
cc7c0 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64  .**       int fd
cc7d0 31 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65  1 = open("./file
cc7e0 31 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45  1", O_RDWR|O_CRE
cc7f0 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 20 20 20  AT, 0644);.**   
cc800 20 20 20 20 69 6e 74 20 66 64 32 20 3d 20 6f 70      int fd2 = op
cc810 65 6e 28 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f  en("./file2", O_
cc820 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36  RDWR|O_CREAT, 06
cc830 34 34 29 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f  44);.**.** Suppo
cc840 73 65 20 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e  se ./file1 and .
cc850 2f 66 69 6c 65 32 20 61 72 65 20 72 65 61 6c 6c  /file2 are reall
cc860 79 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20  y the same file 
cc870 28 62 65 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20  (because.** one 
cc880 69 73 20 61 20 68 61 72 64 20 6f 72 20 73 79 6d  is a hard or sym
cc890 62 6f 6c 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68  bolic link to th
cc8a0 65 20 6f 74 68 65 72 29 20 74 68 65 6e 20 69 66  e other) then if
cc8b0 20 79 6f 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65   you set.** an e
cc8c0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e  xclusive lock on
cc8d0 20 66 64 31 2c 20 74 68 65 6e 20 74 72 79 20 74   fd1, then try t
cc8e0 6f 20 67 65 74 20 61 6e 20 65 78 63 6c 75 73 69  o get an exclusi
cc8f0 76 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64  ve lock.** on fd
cc900 32 2c 20 69 74 20 77 6f 72 6b 73 2e 20 20 49 20  2, it works.  I 
cc910 77 6f 75 6c 64 20 68 61 76 65 20 65 78 70 65 63  would have expec
cc920 74 65 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6c  ted the second l
cc930 6f 63 6b 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73  ock to.** fail s
cc940 69 6e 63 65 20 74 68 65 72 65 20 77 61 73 20 61  ince there was a
cc950 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e  lready a lock on
cc960 20 74 68 65 20 66 69 6c 65 20 64 75 65 20 74 6f   the file due to
cc970 20 66 64 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74   fd1..** But not
cc980 20 73 6f 2e 20 20 53 69 6e 63 65 20 62 6f 74 68   so.  Since both
cc990 20 6c 6f 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d   locks came from
cc9a0 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73   the same proces
cc9b0 73 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64  s, the.** second
cc9c0 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 20 66   overrides the f
cc9d0 69 72 73 74 2c 20 65 76 65 6e 20 74 68 6f 75 67  irst, even thoug
cc9e0 68 20 74 68 65 79 20 77 65 72 65 20 6f 6e 20 64  h they were on d
cc9f0 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65  ifferent.** file
cca00 20 64 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65   descriptors ope
cca10 6e 65 64 20 6f 6e 20 64 69 66 66 65 72 65 6e 74  ned on different
cca20 20 66 69 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a   file names..**.
cca30 2a 2a 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68  ** This means th
cca40 61 74 20 77 65 20 63 61 6e 6e 6f 74 20 75 73 65  at we cannot use
cca50 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 74 6f 20   POSIX locks to 
cca60 73 79 6e 63 68 72 6f 6e 69 7a 65 20 66 69 6c 65  synchronize file
cca70 20 61 63 63 65 73 73 0a 2a 2a 20 61 6d 6f 6e 67   access.** among
cca80 20 63 6f 6d 70 65 74 69 6e 67 20 74 68 72 65 61   competing threa
cca90 64 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 70  ds of the same p
ccaa0 72 6f 63 65 73 73 2e 20 20 50 4f 53 49 58 20 6c  rocess.  POSIX l
ccab0 6f 63 6b 73 20 77 69 6c 6c 20 77 6f 72 6b 20 66  ocks will work f
ccac0 69 6e 65 0a 2a 2a 20 74 6f 20 73 79 6e 63 68 72  ine.** to synchr
ccad0 6f 6e 69 7a 65 20 61 63 63 65 73 73 20 66 6f 72  onize access for
ccae0 20 74 68 72 65 61 64 73 20 69 6e 20 73 65 70 61   threads in sepa
ccaf0 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 2c 20  rate processes, 
ccb00 62 75 74 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61  but not.** threa
ccb10 64 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61  ds within the sa
ccb20 6d 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a  me process..**.*
ccb30 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64  * To work around
ccb40 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 53 51   the problem, SQ
ccb50 4c 69 74 65 20 68 61 73 20 74 6f 20 6d 61 6e 61  Lite has to mana
ccb60 67 65 20 66 69 6c 65 20 6c 6f 63 6b 73 20 69 6e  ge file locks in
ccb70 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69  ternally.** on i
ccb80 74 73 20 6f 77 6e 2e 20 20 57 68 65 6e 65 76 65  ts own.  Wheneve
ccb90 72 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65  r a new database
ccba0 20 69 73 20 6f 70 65 6e 65 64 2c 20 77 65 20 68   is opened, we h
ccbb0 61 76 65 20 74 6f 20 66 69 6e 64 20 74 68 65 0a  ave to find the.
ccbc0 2a 2a 20 73 70 65 63 69 66 69 63 20 69 6e 6f 64  ** specific inod
ccbd0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
ccbe0 65 20 66 69 6c 65 20 28 74 68 65 20 69 6e 6f 64  e file (the inod
ccbf0 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  e is determined 
ccc00 62 79 20 74 68 65 0a 2a 2a 20 73 74 5f 64 65 76  by the.** st_dev
ccc10 20 61 6e 64 20 73 74 5f 69 6e 6f 20 66 69 65 6c   and st_ino fiel
ccc20 64 73 20 6f 66 20 74 68 65 20 73 74 61 74 20 73  ds of the stat s
ccc30 74 72 75 63 74 75 72 65 20 74 68 61 74 20 66 73  tructure that fs
ccc40 74 61 74 28 29 20 66 69 6c 6c 73 20 69 6e 29 0a  tat() fills in).
ccc50 2a 2a 20 61 6e 64 20 63 68 65 63 6b 20 66 6f 72  ** and check for
ccc60 20 6c 6f 63 6b 73 20 61 6c 72 65 61 64 79 20 65   locks already e
ccc70 78 69 73 74 69 6e 67 20 6f 6e 20 74 68 61 74 20  xisting on that 
ccc80 69 6e 6f 64 65 2e 20 20 57 68 65 6e 20 6c 6f 63  inode.  When loc
ccc90 6b 73 20 61 72 65 0a 2a 2a 20 63 72 65 61 74 65  ks are.** create
ccca0 64 20 6f 72 20 72 65 6d 6f 76 65 64 2c 20 77 65  d or removed, we
cccb0 20 68 61 76 65 20 74 6f 20 6c 6f 6f 6b 20 61 74   have to look at
cccc0 20 6f 75 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61   our own interna
cccd0 6c 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 0a  l record of the.
ccce0 2a 2a 20 6c 6f 63 6b 73 20 74 6f 20 73 65 65 20  ** locks to see 
cccf0 69 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  if another threa
ccd00 64 20 68 61 73 20 70 72 65 76 69 6f 75 73 6c 79  d has previously
ccd10 20 73 65 74 20 61 20 6c 6f 63 6b 20 6f 6e 20 74   set a lock on t
ccd20 68 61 74 20 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64  hat same.** inod
ccd30 65 2e 0a 2a 2a 0a 2a 2a 20 28 41 73 69 64 65 3a  e..**.** (Aside:
ccd40 20 54 68 65 20 75 73 65 20 6f 66 20 69 6e 6f 64   The use of inod
ccd50 65 20 6e 75 6d 62 65 72 73 20 61 73 20 75 6e 69  e numbers as uni
ccd60 71 75 65 20 49 44 73 20 64 6f 65 73 20 6e 6f 74  que IDs does not
ccd70 20 77 6f 72 6b 20 6f 6e 20 56 78 57 6f 72 6b 73   work on VxWorks
ccd80 2e 0a 2a 2a 20 46 6f 72 20 56 78 57 6f 72 6b 73  ..** For VxWorks
ccd90 2c 20 77 65 20 68 61 76 65 20 74 6f 20 75 73 65  , we have to use
ccda0 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65   the alternative
ccdb0 20 75 6e 69 71 75 65 20 49 44 20 73 79 73 74 65   unique ID syste
ccdc0 6d 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 63 61  m based on.** ca
ccdd0 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65  nonical filename
ccde0 20 61 6e 64 20 69 6d 70 6c 65 6d 65 6e 74 65 64   and implemented
ccdf0 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73   in the previous
cce00 20 64 69 76 69 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a   division.).**.*
cce10 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 69  * The sqlite3_fi
cce20 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  le structure for
cce30 20 50 4f 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e   POSIX is no lon
cce40 67 65 72 20 6a 75 73 74 20 61 6e 20 69 6e 74 65  ger just an inte
cce50 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63  ger file.** desc
cce60 72 69 70 74 6f 72 2e 20 20 49 74 20 69 73 20 6e  riptor.  It is n
cce70 6f 77 20 61 20 73 74 72 75 63 74 75 72 65 20 74  ow a structure t
cce80 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 69 6e  hat holds the in
cce90 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65  teger file.** de
ccea0 73 63 72 69 70 74 6f 72 20 61 6e 64 20 61 20 70  scriptor and a p
cceb0 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75  ointer to a stru
ccec0 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72  cture that descr
cced0 69 62 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61  ibes the interna
ccee0 6c 0a 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68  l.** locks on th
ccef0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
ccf00 69 6e 6f 64 65 2e 20 20 54 68 65 72 65 20 69 73  inode.  There is
ccf10 20 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72   one locking str
ccf20 75 63 74 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e  ucture.** per in
ccf30 6f 64 65 2c 20 73 6f 20 69 66 20 74 68 65 20 73  ode, so if the s
ccf40 61 6d 65 20 69 6e 6f 64 65 20 69 73 20 6f 70 65  ame inode is ope
ccf50 6e 65 64 20 74 77 69 63 65 2c 20 62 6f 74 68 20  ned twice, both 
ccf60 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75  unixFile structu
ccf70 72 65 73 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20  res.** point to 
ccf80 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67  the same locking
ccf90 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
ccfa0 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75   locking structu
ccfb0 72 65 20 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65  re keeps.** a re
ccfc0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73  ference count (s
ccfd0 6f 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77  o we will know w
ccfe0 68 65 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74  hen to delete it
ccff0 29 20 61 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a  ) and a "cnt".**
cd000 20 66 69 65 6c 64 20 74 68 61 74 20 74 65 6c 6c   field that tell
cd010 73 20 75 73 20 69 74 73 20 69 6e 74 65 72 6e 61  s us its interna
cd020 6c 20 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20 20  l lock status.  
cd030 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65  cnt==0 means the
cd040 0a 2a 2a 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f  .** file is unlo
cd050 63 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d  cked.  cnt==-1 m
cd060 65 61 6e 73 20 74 68 65 20 66 69 6c 65 20 68 61  eans the file ha
cd070 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  s an exclusive l
cd080 6f 63 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65  ock..** cnt>0 me
cd090 61 6e 73 20 74 68 65 72 65 20 61 72 65 20 63 6e  ans there are cn
cd0a0 74 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f  t shared locks o
cd0b0 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  n the file..**.*
cd0c0 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f  * Any attempt to
cd0d0 20 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20   lock or unlock 
cd0e0 61 20 66 69 6c 65 20 66 69 72 73 74 20 63 68 65  a file first che
cd0f0 63 6b 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a  cks the locking.
cd100 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  ** structure.  T
cd110 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65  he fcntl() syste
cd120 6d 20 63 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69  m call is only i
cd130 6e 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 61 20  nvoked to set a 
cd140 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69  .** POSIX lock i
cd150 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c  f the internal l
cd160 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20 74 72  ock structure tr
cd170 61 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65  ansitions betwee
cd180 6e 0a 2a 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e  n.** a locked an
cd190 64 20 61 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74  d an unlocked st
cd1a0 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 77  ate..**.** But w
cd1b0 61 69 74 3a 20 20 74 68 65 72 65 20 61 72 65 20  ait:  there are 
cd1c0 79 65 74 20 6d 6f 72 65 20 70 72 6f 62 6c 65 6d  yet more problem
cd1d0 73 20 77 69 74 68 20 50 4f 53 49 58 20 61 64 76  s with POSIX adv
cd1e0 69 73 6f 72 79 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a  isory locks..**.
cd1f0 2a 2a 20 49 66 20 79 6f 75 20 63 6c 6f 73 65 20  ** If you close 
cd200 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  a file descripto
cd210 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f  r that points to
cd220 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 73   a file that has
cd230 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c   locks,.** all l
cd240 6f 63 6b 73 20 6f 6e 20 74 68 61 74 20 66 69 6c  ocks on that fil
cd250 65 20 74 68 61 74 20 61 72 65 20 6f 77 6e 65 64  e that are owned
cd260 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20   by the current 
cd270 70 72 6f 63 65 73 73 20 61 72 65 0a 2a 2a 20 72  process are.** r
cd280 65 6c 65 61 73 65 64 2e 20 20 54 6f 20 77 6f 72  eleased.  To wor
cd290 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72  k around this pr
cd2a0 6f 62 6c 65 6d 2c 20 65 61 63 68 20 75 6e 69 78  oblem, each unix
cd2b0 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 63  File structure c
cd2c0 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69  ontains.** a poi
cd2d0 6e 74 65 72 20 74 6f 20 61 6e 20 75 6e 69 78 4f  nter to an unixO
cd2e0 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65  penCnt structure
cd2f0 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20  .  There is one 
cd300 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75  unixOpenCnt stru
cd310 63 74 75 72 65 0a 2a 2a 20 70 65 72 20 6f 70 65  cture.** per ope
cd320 6e 20 69 6e 6f 64 65 2c 20 77 68 69 63 68 20 6d  n inode, which m
cd330 65 61 6e 73 20 74 68 61 74 20 6d 75 6c 74 69 70  eans that multip
cd340 6c 65 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20  le unixFile can 
cd350 70 6f 69 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c  point to a singl
cd360 65 0a 2a 2a 20 75 6e 69 78 4f 70 65 6e 43 6e 74  e.** unixOpenCnt
cd370 2e 20 20 57 68 65 6e 20 61 6e 20 61 74 74 65 6d  .  When an attem
cd380 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c  pt is made to cl
cd390 6f 73 65 20 61 6e 20 75 6e 69 78 46 69 6c 65 2c  ose an unixFile,
cd3a0 20 69 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a   if there are.**
cd3b0 20 6f 74 68 65 72 20 75 6e 69 78 46 69 6c 65 20   other unixFile 
cd3c0 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65  open on the same
cd3d0 20 69 6e 6f 64 65 20 74 68 61 74 20 61 72 65 20   inode that are 
cd3e0 68 6f 6c 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 74  holding locks, t
cd3f0 68 65 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c  he call.** to cl
cd400 6f 73 65 28 29 20 74 68 65 20 66 69 6c 65 20 64  ose() the file d
cd410 65 73 63 72 69 70 74 6f 72 20 69 73 20 64 65 66  escriptor is def
cd420 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20  erred until all 
cd430 6f 66 20 74 68 65 20 6c 6f 63 6b 73 20 63 6c 65  of the locks cle
cd440 61 72 2e 0a 2a 2a 20 54 68 65 20 75 6e 69 78 4f  ar..** The unixO
cd450 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65  penCnt structure
cd460 20 6b 65 65 70 73 20 61 20 6c 69 73 74 20 6f 66   keeps a list of
cd470 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
cd480 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 2a  s that need to.*
cd490 2a 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20  * be closed and 
cd4a0 74 68 61 74 20 6c 69 73 74 20 69 73 20 77 61 6c  that list is wal
cd4b0 6b 65 64 20 28 61 6e 64 20 63 6c 65 61 72 65 64  ked (and cleared
cd4c0 29 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20  ) when the last 
cd4d0 6c 6f 63 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a  lock.** clears..
cd4e0 2a 2a 0a 2a 2a 20 59 65 74 20 61 6e 6f 74 68 65  **.** Yet anothe
cd4f0 72 20 70 72 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75  r problem:  Linu
cd500 78 54 68 72 65 61 64 73 20 64 6f 20 6e 6f 74 20  xThreads do not 
cd510 70 6c 61 79 20 77 65 6c 6c 20 77 69 74 68 20 70  play well with p
cd520 6f 73 69 78 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a  osix locks..**.*
cd530 2a 20 4d 61 6e 79 20 6f 6c 64 65 72 20 76 65 72  * Many older ver
cd540 73 69 6f 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75  sions of linux u
cd550 73 65 20 74 68 65 20 4c 69 6e 75 78 54 68 72 65  se the LinuxThre
cd560 61 64 73 20 6c 69 62 72 61 72 79 20 77 68 69 63  ads library whic
cd570 68 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69  h is.** not posi
cd580 78 20 63 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e  x compliant.  Un
cd590 64 65 72 20 4c 69 6e 75 78 54 68 72 65 61 64 73  der LinuxThreads
cd5a0 2c 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64  , a lock created
cd5b0 20 62 79 20 74 68 72 65 61 64 0a 2a 2a 20 41 20   by thread.** A 
cd5c0 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69  cannot be modifi
cd5d0 65 64 20 6f 72 20 6f 76 65 72 72 69 64 64 65 6e  ed or overridden
cd5e0 20 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 20   by a different 
cd5f0 74 68 72 65 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c  thread B..** Onl
cd600 79 20 74 68 72 65 61 64 20 41 20 63 61 6e 20 6d  y thread A can m
cd610 6f 64 69 66 79 20 74 68 65 20 6c 6f 63 6b 2e 20  odify the lock. 
cd620 20 4c 6f 63 6b 69 6e 67 20 62 65 68 61 76 69 6f   Locking behavio
cd630 72 20 69 73 20 63 6f 72 72 65 63 74 0a 2a 2a 20  r is correct.** 
cd640 69 66 20 74 68 65 20 61 70 70 6c 69 61 74 69 6f  if the appliatio
cd650 6e 20 75 73 65 73 20 74 68 65 20 6e 65 77 65 72  n uses the newer
cd660 20 4e 61 74 69 76 65 20 50 6f 73 69 78 20 54 68   Native Posix Th
cd670 72 65 61 64 20 4c 69 62 72 61 72 79 20 28 4e 50  read Library (NP
cd680 54 4c 29 0a 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20  TL).** on linux 
cd690 2d 20 77 69 74 68 20 4e 50 54 4c 20 61 20 6c 6f  - with NPTL a lo
cd6a0 63 6b 20 63 72 65 61 74 65 64 20 62 79 20 74 68  ck created by th
cd6b0 72 65 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72  read A can overr
cd6c0 69 64 65 20 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20  ide locks.** in 
cd6d0 74 68 72 65 61 64 20 42 2e 20 20 42 75 74 20 74  thread B.  But t
cd6e0 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74  here is no way t
cd6f0 6f 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c  o know at compil
cd700 65 2d 74 69 6d 65 20 77 68 69 63 68 0a 2a 2a 20  e-time which.** 
cd710 74 68 72 65 61 64 69 6e 67 20 6c 69 62 72 61 72  threading librar
cd720 79 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 2e  y is being used.
cd730 20 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f    So there is no
cd740 20 77 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 0a   way to know at.
cd750 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  ** compile-time 
cd760 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
cd770 68 72 65 61 64 20 41 20 63 61 6e 20 6f 76 65 72  hread A can over
cd780 72 69 64 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68  ride locks on th
cd790 72 65 61 64 20 42 2e 0a 2a 2a 20 57 65 20 68 61  read B..** We ha
cd7a0 76 65 20 74 6f 20 64 6f 20 61 20 72 75 6e 2d 74  ve to do a run-t
cd7b0 69 6d 65 20 63 68 65 63 6b 20 74 6f 20 64 69 73  ime check to dis
cd7c0 63 6f 76 65 72 20 74 68 65 20 62 65 68 61 76 69  cover the behavi
cd7d0 6f 72 20 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72  or of the.** cur
cd7e0 72 65 6e 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a  rent process..**
cd7f0 0a 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77  .** On systems w
cd800 68 65 72 65 20 74 68 72 65 61 64 20 41 20 69 73  here thread A is
cd810 20 75 6e 61 62 6c 65 20 74 6f 20 6d 6f 64 69 66   unable to modif
cd820 79 20 6c 6f 63 6b 73 20 63 72 65 61 74 65 64 20  y locks created 
cd830 62 79 0a 2a 2a 20 74 68 72 65 61 64 20 42 2c 20  by.** thread B, 
cd840 77 65 20 68 61 76 65 20 74 6f 20 6b 65 65 70 20  we have to keep 
cd850 74 72 61 63 6b 20 6f 66 20 77 68 69 63 68 20 74  track of which t
cd860 68 72 65 61 64 20 63 72 65 61 74 65 64 20 65 61  hread created ea
cd870 63 68 0a 2a 2a 20 6c 6f 63 6b 2e 20 20 48 65 6e  ch.** lock.  Hen
cd880 63 65 20 74 68 65 72 65 20 69 73 20 61 6e 20 65  ce there is an e
cd890 78 74 72 61 20 66 69 65 6c 64 20 69 6e 20 74 68  xtra field in th
cd8a0 65 20 6b 65 79 20 74 6f 20 74 68 65 20 75 6e 69  e key to the uni
cd8b0 78 4c 6f 63 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72  xLockInfo.** str
cd8c0 75 63 74 75 72 65 20 74 6f 20 72 65 63 6f 72 64  ucture to record
cd8d0 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   this informatio
cd8e0 6e 2e 20 20 41 6e 64 20 6f 6e 20 74 68 6f 73 65  n.  And on those
cd8f0 20 73 79 73 74 65 6d 73 20 69 74 0a 2a 2a 20 69   systems it.** i
cd900 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 62 65 67  s illegal to beg
cd910 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
cd920 20 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 20 61   in one thread a
cd930 6e 64 20 66 69 6e 69 73 68 20 69 74 0a 2a 2a 20  nd finish it.** 
cd940 69 6e 20 61 6e 6f 74 68 65 72 2e 20 20 46 6f 72  in another.  For
cd950 20 74 68 69 73 20 6c 61 74 74 65 72 20 72 65 73   this latter res
cd960 74 72 69 63 74 69 6f 6e 2c 20 74 68 65 72 65 20  triction, there 
cd970 69 73 20 6e 6f 20 77 6f 72 6b 2d 61 72 6f 75 6e  is no work-aroun
cd980 64 2e 0a 2a 2a 20 49 74 20 69 73 20 61 20 6c 69  d..** It is a li
cd990 6d 69 74 61 74 69 6f 6e 20 6f 66 20 4c 69 6e 75  mitation of Linu
cd9a0 78 54 68 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a  xThreads..*/../*
cd9b0 0a 2a 2a 20 53 65 74 20 6f 72 20 63 68 65 63 6b  .** Set or check
cd9c0 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 74 69   the unixFile.ti
cd9d0 64 20 66 69 65 6c 64 2e 20 20 54 68 69 73 20 66  d field.  This f
cd9e0 69 65 6c 64 20 69 73 20 73 65 74 20 77 68 65 6e  ield is set when
cd9f0 20 61 6e 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20   an unixFile.** 
cda00 69 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e  is first opened.
cda10 20 20 41 6c 6c 20 73 75 62 73 65 71 75 65 6e 74    All subsequent
cda20 20 75 73 65 73 20 6f 66 20 74 68 65 20 75 6e 69   uses of the uni
cda30 78 46 69 6c 65 20 76 65 72 69 66 79 20 74 68 61  xFile verify tha
cda40 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68  t the.** same th
cda50 72 65 61 64 20 69 73 20 6f 70 65 72 61 74 69 6e  read is operatin
cda60 67 20 6f 6e 20 74 68 65 20 75 6e 69 78 46 69 6c  g on the unixFil
cda70 65 2e 20 20 53 6f 6d 65 20 6f 70 65 72 61 74 69  e.  Some operati
cda80 6e 67 20 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a  ng systems do.**
cda90 20 6e 6f 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73   not allow locks
cdaa0 20 74 6f 20 62 65 20 6f 76 65 72 72 69 64 64 65   to be overridde
cdab0 6e 20 62 79 20 6f 74 68 65 72 20 74 68 72 65 61  n by other threa
cdac0 64 73 20 61 6e 64 20 74 68 61 74 20 72 65 73 74  ds and that rest
cdad0 72 69 63 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73  riction.** means
cdae0 20 74 68 61 74 20 73 71 6c 69 74 65 33 2a 20 64   that sqlite3* d
cdaf0 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20  atabase handles 
cdb00 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20  cannot be moved 
cdb10 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a  from one thread.
cdb20 2a 2a 20 74 6f 20 61 6e 6f 74 68 65 72 20 77 68  ** to another wh
cdb30 69 6c 65 20 6c 6f 63 6b 73 20 61 72 65 20 68 65  ile locks are he
cdb40 6c 64 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f  ld..**.** Versio
cdb50 6e 20 33 2e 33 2e 31 20 28 32 30 30 36 2d 30 31  n 3.3.1 (2006-01
cdb60 2d 31 35 29 3a 20 20 75 6e 69 78 46 69 6c 65 20  -15):  unixFile 
cdb70 63 61 6e 20 62 65 20 6d 6f 76 65 64 20 66 72 6f  can be moved fro
cdb80 6d 20 6f 6e 65 20 74 68 72 65 61 64 20 74 6f 0a  m one thread to.
cdb90 2a 2a 20 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f  ** another as lo
cdba0 6e 67 20 61 73 20 77 65 20 61 72 65 20 72 75 6e  ng as we are run
cdbb0 6e 69 6e 67 20 6f 6e 20 61 20 73 79 73 74 65 6d  ning on a system
cdbc0 20 74 68 61 74 20 73 75 70 70 6f 72 74 73 20 74   that supports t
cdbd0 68 72 65 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69  hreads.** overri
cdbe0 64 69 6e 67 20 65 61 63 68 20 6f 74 68 65 72 73  ding each others
cdbf0 20 6c 6f 63 6b 73 20 28 77 68 69 63 68 20 69 73   locks (which is
cdc00 20 6e 6f 77 20 74 68 65 20 6d 6f 73 74 20 63 6f   now the most co
cdc10 6d 6d 6f 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a  mmon behavior).*
cdc20 2a 20 6f 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73  * or if no locks
cdc30 20 61 72 65 20 68 65 6c 64 2e 20 20 42 75 74 20   are held.  But 
cdc40 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f  the unixFile.pLo
cdc50 63 6b 20 66 69 65 6c 64 20 6e 65 65 64 73 20 74  ck field needs t
cdc60 6f 20 62 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74  o be.** recomput
cdc70 65 64 20 62 65 63 61 75 73 65 20 69 74 73 20 6b  ed because its k
cdc80 65 79 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20  ey includes the 
cdc90 74 68 72 65 61 64 2d 69 64 2e 20 20 53 65 65 20  thread-id.  See 
cdca0 74 68 65 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72  the .** transfer
cdcb0 4f 77 6e 65 72 73 68 69 70 28 29 20 66 75 6e 63  Ownership() func
cdcc0 74 69 6f 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61  tion below for a
cdcd0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
cdce0 61 74 69 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ation.*/.#if SQL
cdcf0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26  ITE_THREADSAFE &
cdd00 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75  & defined(__linu
cdd10 78 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 53 45  x__).# define SE
cdd20 54 5f 54 48 52 45 41 44 49 44 28 58 29 20 20 20  T_THREADID(X)   
cdd30 28 58 29 2d 3e 74 69 64 20 3d 20 70 74 68 72 65  (X)->tid = pthre
cdd40 61 64 5f 73 65 6c 66 28 29 0a 23 20 64 65 66 69  ad_self().# defi
cdd50 6e 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49  ne CHECK_THREADI
cdd60 44 28 58 29 20 28 74 68 72 65 61 64 73 4f 76 65  D(X) (threadsOve
cdd70 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
cdd80 6f 63 6b 73 3d 3d 30 20 26 26 20 5c 0a 20 20 20  ocks==0 && \.   
cdd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdda0 20 20 20 20 20 20 20 20 20 21 70 74 68 72 65 61           !pthrea
cddb0 64 5f 65 71 75 61 6c 28 28 58 29 2d 3e 74 69 64  d_equal((X)->tid
cddc0 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  , pthread_self()
cddd0 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  )).#else.# defin
cdde0 65 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 58  e SET_THREADID(X
cddf0 29 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b  ).# define CHECK
cde00 5f 54 48 52 45 41 44 49 44 28 58 29 20 30 0a 23  _THREADID(X) 0.#
cde10 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  endif../*.** An 
cde20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
cde30 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
cde40 75 72 65 20 73 65 72 76 65 73 20 61 73 20 74 68  ure serves as th
cde50 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f  e key used.** to
cde60 20 6c 6f 63 61 74 65 20 61 20 70 61 72 74 69 63   locate a partic
cde70 75 6c 61 72 20 75 6e 69 78 4f 70 65 6e 43 6e 74  ular unixOpenCnt
cde80 20 73 74 72 75 63 74 75 72 65 20 67 69 76 65 6e   structure given
cde90 20 69 74 73 20 69 6e 6f 64 65 2e 20 20 54 68 69   its inode.  Thi
cdea0 73 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65  s.** is the same
cdeb0 20 61 73 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b   as the unixLock
cdec0 4b 65 79 20 65 78 63 65 70 74 20 74 68 61 74 20  Key except that 
cded0 74 68 65 20 74 68 72 65 61 64 20 49 44 20 69 73  the thread ID is
cdee0 20 6f 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72   omitted..*/.str
cdef0 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 7b  uct unixFileId {
cdf00 0a 20 20 64 65 76 5f 74 20 64 65 76 3b 20 20 20  .  dev_t dev;   
cdf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
cdf20 2a 20 44 65 76 69 63 65 20 6e 75 6d 62 65 72 20  * Device number 
cdf30 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  */.#if OS_VXWORK
cdf40 53 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72  S.  struct vxwor
cdf50 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20  ksFileId *pId;  
cdf60 2f 2a 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49  /* Unique file I
cdf70 44 20 66 6f 72 20 76 78 77 6f 72 6b 73 2e 20 2a  D for vxworks. *
cdf80 2f 0a 23 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20  /.#else.  ino_t 
cdf90 69 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  ino;            
cdfa0 20 20 20 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e        /* Inode n
cdfb0 75 6d 62 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a  umber */.#endif.
cdfc0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
cdfd0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
cdfe0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
cdff0 20 73 65 72 76 65 73 20 61 73 20 74 68 65 20 6b   serves as the k
ce000 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f  ey used.** to lo
ce010 63 61 74 65 20 61 20 70 61 72 74 69 63 75 6c 61  cate a particula
ce020 72 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73  r unixLockInfo s
ce030 74 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69  tructure given i
ce040 74 73 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ts inode..**.** 
ce050 49 66 20 74 68 72 65 61 64 73 20 63 61 6e 6e 6f  If threads canno
ce060 74 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  t override each 
ce070 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 4c 69  others locks (Li
ce080 6e 75 78 54 68 72 65 61 64 73 29 2c 20 74 68 65  nuxThreads), the
ce090 6e 20 77 65 0a 2a 2a 20 73 65 74 20 74 68 65 20  n we.** set the 
ce0a0 75 6e 69 78 4c 6f 63 6b 4b 65 79 2e 74 69 64 20  unixLockKey.tid 
ce0b0 66 69 65 6c 64 20 74 6f 20 74 68 65 20 74 68 72  field to the thr
ce0c0 65 61 64 20 49 44 2e 20 20 49 66 20 74 68 72 65  ead ID.  If thre
ce0d0 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65  ads can override
ce0e0 0a 2a 2a 20 65 61 63 68 20 6f 74 68 65 72 73 20  .** each others 
ce0f0 6c 6f 63 6b 73 20 28 50 6f 73 69 78 20 61 6e 64  locks (Posix and
ce100 20 4e 50 54 4c 29 20 74 68 65 6e 20 74 69 64 20   NPTL) then tid 
ce110 69 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f  is always set to
ce120 20 7a 65 72 6f 2e 0a 2a 2a 20 74 69 64 20 69 73   zero..** tid is
ce130 20 6f 6d 69 74 74 65 64 20 69 66 20 77 65 20 63   omitted if we c
ce140 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 74  ompile without t
ce150 68 72 65 61 64 69 6e 67 20 73 75 70 70 6f 72 74  hreading support
ce160 20 6f 72 20 6f 6e 20 61 6e 20 4f 53 0a 2a 2a 20   or on an OS.** 
ce170 6f 74 68 65 72 20 74 68 61 6e 20 6c 69 6e 75 78  other than linux
ce180 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78  ..*/.struct unix
ce190 4c 6f 63 6b 4b 65 79 20 7b 0a 20 20 73 74 72 75  LockKey {.  stru
ce1a0 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69  ct unixFileId fi
ce1b0 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20 69 64  d;  /* Unique id
ce1c0 65 6e 74 69 66 69 65 72 20 66 6f 72 20 74 68 65  entifier for the
ce1d0 20 66 69 6c 65 20 2a 2f 0a 23 69 66 20 53 51 4c   file */.#if SQL
ce1e0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26  ITE_THREADSAFE &
ce1f0 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75  & defined(__linu
ce200 78 5f 5f 29 0a 20 20 70 74 68 72 65 61 64 5f 74  x__).  pthread_t
ce210 20 74 69 64 3b 20 20 2f 2a 20 54 68 72 65 61 64   tid;  /* Thread
ce220 20 49 44 20 6f 66 20 6c 6f 63 6b 20 6f 77 6e 65   ID of lock owne
ce230 72 2e 20 5a 65 72 6f 20 69 66 20 6e 6f 74 20 75  r. Zero if not u
ce240 73 69 6e 67 20 4c 69 6e 75 78 54 68 72 65 61 64  sing LinuxThread
ce250 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  s */.#endif.};..
ce260 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
ce270 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
ce280 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ng structure is 
ce290 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61  allocated for ea
ce2a0 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65  ch open.** inode
ce2b0 2e 20 20 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54  .  Or, on LinuxT
ce2c0 68 72 65 61 64 73 2c 20 74 68 65 72 65 20 69 73  hreads, there is
ce2d0 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 73 74   one of these st
ce2e0 72 75 63 74 75 72 65 73 20 66 6f 72 0a 2a 2a 20  ructures for.** 
ce2f0 65 61 63 68 20 69 6e 6f 64 65 20 6f 70 65 6e 65  each inode opene
ce300 64 20 62 79 20 65 61 63 68 20 74 68 72 65 61 64  d by each thread
ce310 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65  ..**.** A single
ce320 20 69 6e 6f 64 65 20 63 61 6e 20 68 61 76 65 20   inode can have 
ce330 6d 75 6c 74 69 70 6c 65 20 66 69 6c 65 20 64 65  multiple file de
ce340 73 63 72 69 70 74 6f 72 73 2c 20 73 6f 20 65 61  scriptors, so ea
ce350 63 68 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73  ch unixFile.** s
ce360 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
ce370 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
ce380 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
ce390 69 73 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68  is object and th
ce3a0 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65  is.** object kee
ce3b0 70 73 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68  ps a count of th
ce3c0 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 78  e number of unix
ce3d0 46 69 6c 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f  File pointing to
ce3e0 20 69 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75   it..*/.struct u
ce3f0 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 7b 0a 20 20  nixLockInfo {.  
ce400 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b  struct unixLockK
ce410 65 79 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 20  ey lockKey;     
ce420 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65  /* The lookup ke
ce430 79 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20  y */.  int cnt; 
ce440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce450 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
ce460 20 6f 66 20 53 48 41 52 45 44 20 6c 6f 63 6b 73   of SHARED locks
ce470 20 68 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c   held */.  int l
ce480 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 20  ocktype;        
ce490 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
ce4a0 65 20 6f 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b  e of SHARED_LOCK
ce4b0 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  , RESERVED_LOCK 
ce4c0 65 74 63 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  etc. */.  int nR
ce4d0 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
ce4e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
ce4f0 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20  ber of pointers 
ce500 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
ce510 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e  e */.  struct un
ce520 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4e 65 78  ixLockInfo *pNex
ce530 74 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f  t;     /* List o
ce540 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e  f all unixLockIn
ce550 66 6f 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20  fo objects */.  
ce560 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49  struct unixLockI
ce570 6e 66 6f 20 2a 70 50 72 65 76 3b 20 20 20 20 20  nfo *pPrev;     
ce580 2f 2a 20 20 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c  /*    .... doubl
ce590 79 20 6c 69 6e 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a  y linked */.};..
ce5a0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
ce5b0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
ce5c0 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ng structure is 
ce5d0 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61  allocated for ea
ce5e0 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65  ch open.** inode
ce5f0 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72  .  This structur
ce600 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66  e keeps track of
ce610 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c   the number of l
ce620 6f 63 6b 73 20 6f 6e 20 74 68 61 74 0a 2a 2a 20  ocks on that.** 
ce630 69 6e 6f 64 65 2e 20 20 49 66 20 61 20 63 6c 6f  inode.  If a clo
ce640 73 65 20 69 73 20 61 74 74 65 6d 70 74 65 64 20  se is attempted 
ce650 61 67 61 69 6e 73 74 20 61 6e 20 69 6e 6f 64 65  against an inode
ce660 20 74 68 61 74 20 69 73 20 68 6f 6c 64 69 6e 67   that is holding
ce670 0a 2a 2a 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63  .** locks, the c
ce680 6c 6f 73 65 20 69 73 20 64 65 66 65 72 72 65 64  lose is deferred
ce690 20 75 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73   until all locks
ce6a0 20 63 6c 65 61 72 20 62 79 20 61 64 64 69 6e 67   clear by adding
ce6b0 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 65 73   the.** file des
ce6c0 63 72 69 70 74 6f 72 20 74 6f 20 62 65 20 63 6c  criptor to be cl
ce6d0 6f 73 65 64 20 74 6f 20 74 68 65 20 70 65 6e 64  osed to the pend
ce6e0 69 6e 67 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ing list..**.** 
ce6f0 54 4f 44 4f 3a 20 20 43 6f 6e 73 69 64 65 72 20  TODO:  Consider 
ce700 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20 73 6f  changing this so
ce710 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6f   that there is o
ce720 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 66 69 6c  nly a single fil
ce730 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20  e.** descriptor 
ce740 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 66 69  for each open fi
ce750 6c 65 2c 20 65 76 65 6e 20 77 68 65 6e 20 69 74  le, even when it
ce760 20 69 73 20 6f 70 65 6e 65 64 20 6d 75 6c 74 69   is opened multi
ce770 70 6c 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 54 68  ple times..** Th
ce780 65 20 63 6c 6f 73 65 28 29 20 73 79 73 74 65 6d  e close() system
ce790 20 63 61 6c 6c 20 77 6f 75 6c 64 20 6f 6e 6c 79   call would only
ce7a0 20 6f 63 63 75 72 20 77 68 65 6e 20 74 68 65 20   occur when the 
ce7b0 6c 61 73 74 20 64 61 74 61 62 61 73 65 0a 2a 2a  last database.**
ce7c0 20 75 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20   using the file 
ce7d0 63 6c 6f 73 65 73 2e 0a 2a 2f 0a 73 74 72 75 63  closes..*/.struc
ce7e0 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 7b 0a  t unixOpenCnt {.
ce7f0 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c    struct unixFil
ce800 65 49 64 20 66 69 6c 65 49 64 3b 20 20 20 2f 2a  eId fileId;   /*
ce810 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20   The lookup key 
ce820 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20  */.  int nRef;  
ce830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce840 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f   /* Number of po
ce850 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73  inters to this s
ce860 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e  tructure */.  in
ce870 74 20 6e 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20  t nLock;        
ce880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
ce890 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69  ber of outstandi
ce8a0 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 55 6e  ng locks */.  Un
ce8b0 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75  ixUnusedFd *pUnu
ce8c0 73 65 64 3b 20 20 20 20 20 20 2f 2a 20 55 6e 75  sed;      /* Unu
ce8d0 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70  sed file descrip
ce8e0 74 6f 72 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f  tors to close */
ce8f0 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
ce900 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20    sem_t *pSem;  
ce910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce920 20 20 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49     /* Named POSI
ce930 58 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20  X semaphore */. 
ce940 20 63 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d   char aSemName[M
ce950 41 58 5f 50 41 54 48 4e 41 4d 45 2b 32 5d 3b 20  AX_PATHNAME+2]; 
ce960 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61    /* Name of tha
ce970 74 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23  t semaphore */.#
ce980 65 6e 64 69 66 0a 20 20 73 74 72 75 63 74 20 75  endif.  struct u
ce990 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4e 65 78  nixOpenCnt *pNex
ce9a0 74 2c 20 2a 70 50 72 65 76 3b 20 20 20 2f 2a 20  t, *pPrev;   /* 
ce9b0 4c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78  List of all unix
ce9c0 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20  OpenCnt objects 
ce9d0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73  */.};../*.** Lis
ce9e0 74 73 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f  ts of all unixLo
ce9f0 63 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f  ckInfo and unixO
cea00 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20  penCnt objects. 
cea10 20 54 68 65 73 65 20 75 73 65 64 20 74 6f 20 62   These used to b
cea20 65 20 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 73  e hash.** tables
cea30 2e 20 20 42 75 74 20 74 68 65 20 6e 75 6d 62 65  .  But the numbe
cea40 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 69 73 20  r of objects is 
cea50 72 61 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e  rarely more than
cea60 20 61 20 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20   a dozen and.** 
cea70 6e 65 76 65 72 20 65 78 63 65 65 64 73 20 61 20  never exceeds a 
cea80 66 65 77 20 74 68 6f 75 73 61 6e 64 2e 20 20 41  few thousand.  A
cea90 6e 64 20 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74  nd lookup is not
ceaa0 20 6f 6e 20 61 20 63 72 69 74 69 63 61 6c 0a 2a   on a critical.*
ceab0 2a 20 70 61 74 68 20 73 6f 20 61 20 73 69 6d 70  * path so a simp
ceac0 6c 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77  le linked list w
cead0 69 6c 6c 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a  ill suffice..*/.
ceae0 73 74 61 74 69 63 20 73 74 72 75 63 74 20 75 6e  static struct un
ceaf0 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b  ixLockInfo *lock
ceb00 4c 69 73 74 20 3d 20 30 3b 0a 73 74 61 74 69 63  List = 0;.static
ceb10 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e   struct unixOpen
ceb20 43 6e 74 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20  Cnt *openList = 
ceb30 30 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76  0;../*.** This v
ceb40 61 72 69 61 62 6c 65 20 72 65 6d 65 6d 62 65 72  ariable remember
ceb50 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  s whether or not
ceb60 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65   threads can ove
ceb70 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72  rride each other
ceb80 73 0a 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a  s.** locks..**.*
ceb90 2a 20 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54 68  *    0:  No.  Th
ceba0 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65  reads cannot ove
cebb0 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72  rride each other
cebc0 73 20 6c 6f 63 6b 73 2e 20 20 28 4c 69 6e 75 78  s locks.  (Linux
cebd0 54 68 72 65 61 64 73 29 0a 2a 2a 20 20 20 20 31  Threads).**    1
cebe0 3a 20 20 59 65 73 2e 20 20 54 68 72 65 61 64 73  :  Yes.  Threads
cebf0 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61   can override ea
cec00 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e  ch others locks.
cec10 20 20 28 50 6f 73 69 78 20 26 20 4e 4c 50 54 29    (Posix & NLPT)
cec20 0a 2a 2a 20 20 20 2d 31 3a 20 20 57 65 20 64 6f  .**   -1:  We do
cec30 6e 27 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a  n't know yet..**
cec40 0a 2a 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74  .** On some syst
cec50 65 6d 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74 20  ems, we know at 
cec60 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66 20  compile-time if 
cec70 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72  threads can over
cec80 72 69 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74 68  ride each.** oth
cec90 65 72 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74  ers locks.  On t
ceca0 68 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 74 68  hose systems, th
cecb0 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f  e SQLITE_THREAD_
cecc0 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d 61  OVERRIDE_LOCK ma
cecd0 63 72 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73  cro.** will be s
cece0 65 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79  et appropriately
cecf0 2e 20 20 4f 6e 20 6f 74 68 65 72 20 73 79 73 74  .  On other syst
ced00 65 6d 73 2c 20 77 65 20 68 61 76 65 20 74 6f 20  ems, we have to 
ced10 63 68 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e 74  check at.** runt
ced20 69 6d 65 2e 20 20 4f 6e 20 74 68 65 73 65 20 6c  ime.  On these l
ced30 61 74 74 65 72 20 73 79 73 74 65 6d 73 2c 20 53  atter systems, S
ced40 51 4c 54 49 45 5f 54 48 52 45 41 44 5f 4f 56 45  QLTIE_THREAD_OVE
ced50 52 52 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a  RRIDE_LOCK is.**
ced60 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a   undefined..**.*
ced70 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20  * This variable 
ced80 6e 6f 72 6d 61 6c 6c 79 20 68 61 73 20 66 69 6c  normally has fil
ced90 65 20 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 42  e scope only.  B
ceda0 75 74 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e  ut during testin
cedb0 67 2c 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 74  g, we make.** it
cedc0 20 61 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68 61   a global so tha
cedd0 74 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 20  t the test code 
cede0 63 61 6e 20 63 68 61 6e 67 65 20 69 74 73 20 76  can change its v
cedf0 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  alue in order to
cee00 20 76 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 20   verify.** that 
cee10 74 68 65 20 72 69 67 68 74 20 73 74 75 66 66 20  the right stuff 
cee20 68 61 70 70 65 6e 73 20 69 6e 20 65 69 74 68 65  happens in eithe
cee30 72 20 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 20 53  r case..*/.#if S
cee40 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
cee50 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69   && defined(__li
cee60 6e 75 78 5f 5f 29 0a 23 20 20 69 66 6e 64 65 66  nux__).#  ifndef
cee70 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f   SQLITE_THREAD_O
cee80 56 45 52 52 49 44 45 5f 4c 4f 43 4b 0a 23 20 20  VERRIDE_LOCK.#  
cee90 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
ceea0 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f  THREAD_OVERRIDE_
ceeb0 4c 4f 43 4b 20 2d 31 0a 23 20 20 65 6e 64 69 66  LOCK -1.#  endif
ceec0 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 45  .#  ifdef SQLITE
ceed0 5f 54 45 53 54 0a 69 6e 74 20 74 68 72 65 61 64  _TEST.int thread
ceee0 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
ceef0 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54  ersLocks = SQLIT
cef00 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44  E_THREAD_OVERRID
cef10 45 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6c 73 65 0a  E_LOCK;.#  else.
cef20 73 74 61 74 69 63 20 69 6e 74 20 74 68 72 65 61  static int threa
cef30 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
cef40 68 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49  hersLocks = SQLI
cef50 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49  TE_THREAD_OVERRI
cef60 44 45 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6e 64 69  DE_LOCK;.#  endi
cef70 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  f.#endif../*.** 
cef80 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68  This structure h
cef90 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  olds information
cefa0 20 70 61 73 73 65 64 20 69 6e 74 6f 20 69 6e 64   passed into ind
cefb0 69 76 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a 20  ividual test.** 
cefc0 74 68 72 65 61 64 73 20 62 79 20 74 68 65 20 74  threads by the t
cefd0 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67  estThreadLocking
cefe0 42 65 68 61 76 69 6f 72 28 29 20 72 6f 75 74 69  Behavior() routi
ceff0 6e 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 68  ne..*/.struct th
cf000 72 65 61 64 54 65 73 74 44 61 74 61 20 7b 0a 20  readTestData {. 
cf010 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20   int fd;        
cf020 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20          /* File 
cf030 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a  to be locked */.
cf040 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
cf050 6f 63 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  ock;     /* The 
cf060 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f  locking operatio
cf070 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75 6c  n */.  int resul
cf080 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
cf090 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6c   Result of the l
cf0a0 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ocking operation
cf0b0 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 53 51 4c 49   */.};..#if SQLI
cf0c0 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
cf0d0 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78   defined(__linux
cf0e0 5f 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  __)./*.** This f
cf0f0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
cf100 61 73 20 74 68 65 20 6d 61 69 6e 20 72 6f 75 74  as the main rout
cf110 69 6e 65 20 66 6f 72 20 61 20 74 68 72 65 61 64  ine for a thread
cf120 20 6c 61 75 6e 63 68 65 64 20 62 79 0a 2a 2a 20   launched by.** 
cf130 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e  testThreadLockin
cf140 67 42 65 68 61 76 69 6f 72 28 29 2e 20 49 74 20  gBehavior(). It 
cf150 74 65 73 74 73 20 77 68 65 74 68 65 72 20 74 68  tests whether th
cf160 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 6f 62  e shared-lock ob
cf170 74 61 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65  tained.** by the
cf180 20 6d 61 69 6e 20 74 68 72 65 61 64 20 69 6e 20   main thread in 
cf190 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e  testThreadLockin
cf1a0 67 42 65 68 61 76 69 6f 72 28 29 20 63 6f 6e 66  gBehavior() conf
cf1b0 6c 69 63 74 73 20 77 69 74 68 20 61 0a 2a 2a 20  licts with a.** 
cf1c0 68 79 70 6f 74 68 65 74 69 63 61 6c 20 77 72 69  hypothetical wri
cf1d0 74 65 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64  te-lock obtained
cf1e0 20 62 79 20 74 68 69 73 20 74 68 72 65 61 64 20   by this thread 
cf1f0 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65  on the same file
cf200 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 72 69 74  ..**.** The writ
cf210 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63  e-lock is not ac
cf220 74 75 61 6c 6c 79 20 61 63 71 75 69 72 65 64 2c  tually acquired,
cf230 20 61 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20   as this is not 
cf240 70 6f 73 73 69 62 6c 65 20 69 66 20 0a 2a 2a 20  possible if .** 
cf250 74 68 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  the file is open
cf260 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f   in read-only mo
cf270 64 65 20 28 73 65 65 20 74 69 63 6b 65 74 20 23  de (see ticket #
cf280 33 34 37 32 29 2e 0a 2a 2f 20 0a 73 74 61 74 69  3472)..*/ .stati
cf290 63 20 76 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f  c void *threadLo
cf2a0 63 6b 69 6e 67 54 65 73 74 28 76 6f 69 64 20 2a  ckingTest(void *
cf2b0 70 41 72 67 29 7b 0a 20 20 73 74 72 75 63 74 20  pArg){.  struct 
cf2c0 74 68 72 65 61 64 54 65 73 74 44 61 74 61 20 2a  threadTestData *
cf2d0 70 44 61 74 61 20 3d 20 28 73 74 72 75 63 74 20  pData = (struct 
cf2e0 74 68 72 65 61 64 54 65 73 74 44 61 74 61 2a 29  threadTestData*)
cf2f0 70 41 72 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72  pArg;.  pData->r
cf300 65 73 75 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44  esult = fcntl(pD
cf310 61 74 61 2d 3e 66 64 2c 20 46 5f 47 45 54 4c 4b  ata->fd, F_GETLK
cf320 2c 20 26 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b  , &pData->lock);
cf330 0a 20 20 72 65 74 75 72 6e 20 70 41 72 67 3b 0a  .  return pArg;.
cf340 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
cf350 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
cf360 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78   defined(__linux
cf370 5f 5f 29 20 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c  __) */...#if SQL
cf380 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26  ITE_THREADSAFE &
cf390 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75  & defined(__linu
cf3a0 78 5f 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  x__)./*.** This 
cf3b0 70 72 6f 63 65 64 75 72 65 20 61 74 74 65 6d 70  procedure attemp
cf3c0 74 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ts to determine 
cf3d0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
cf3e0 68 72 65 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76  hreads.** can ov
cf3f0 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65  erride each othe
cf400 72 73 20 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65  rs locks then se
cf410 74 73 20 74 68 65 20 0a 2a 2a 20 74 68 72 65 61  ts the .** threa
cf420 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
cf430 68 65 72 73 4c 6f 63 6b 73 20 76 61 72 69 61 62  hersLocks variab
cf440 6c 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79  le appropriately
cf450 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
cf460 20 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69   testThreadLocki
cf470 6e 67 42 65 68 61 76 69 6f 72 28 69 6e 74 20 66  ngBehavior(int f
cf480 64 5f 6f 72 69 67 29 7b 0a 20 20 69 6e 74 20 66  d_orig){.  int f
cf490 64 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  d;.  int rc;.  s
cf4a0 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74  truct threadTest
cf4b0 44 61 74 61 20 64 3b 0a 20 20 73 74 72 75 63 74  Data d;.  struct
cf4c0 20 66 6c 6f 63 6b 20 6c 3b 0a 20 20 70 74 68 72   flock l;.  pthr
cf4d0 65 61 64 5f 74 20 74 3b 0a 0a 20 20 66 64 20 3d  ead_t t;..  fd =
cf4e0 20 64 75 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20   dup(fd_orig);. 
cf4f0 20 69 66 28 20 66 64 3c 30 20 29 20 72 65 74 75   if( fd<0 ) retu
cf500 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6c 2c  rn;.  memset(&l,
cf510 20 30 2c 20 73 69 7a 65 6f 66 28 6c 29 29 3b 0a   0, sizeof(l));.
cf520 20 20 6c 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52    l.l_type = F_R
cf530 44 4c 43 4b 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20  DLCK;.  l.l_len 
cf540 3d 20 31 3b 0a 20 20 6c 2e 6c 5f 73 74 61 72 74  = 1;.  l.l_start
cf550 20 3d 20 30 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e   = 0;.  l.l_when
cf560 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20  ce = SEEK_SET;. 
cf570 20 72 63 20 3d 20 66 63 6e 74 6c 28 66 64 5f 6f   rc = fcntl(fd_o
cf580 72 69 67 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c  rig, F_SETLK, &l
cf590 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29  );.  if( rc!=0 )
cf5a0 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65   return;.  memse
cf5b0 74 28 26 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28  t(&d, 0, sizeof(
cf5c0 64 29 29 3b 0a 20 20 64 2e 66 64 20 3d 20 66 64  d));.  d.fd = fd
cf5d0 3b 0a 20 20 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a  ;.  d.lock = l;.
cf5e0 20 20 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20    d.lock.l_type 
cf5f0 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 69 66 28  = F_WRLCK;.  if(
cf600 20 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28   pthread_create(
cf610 26 74 2c 20 30 2c 20 74 68 72 65 61 64 4c 6f 63  &t, 0, threadLoc
cf620 6b 69 6e 67 54 65 73 74 2c 20 26 64 29 3d 3d 30  kingTest, &d)==0
cf630 20 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f   ){.    pthread_
cf640 6a 6f 69 6e 28 74 2c 20 30 29 3b 0a 20 20 7d 0a  join(t, 0);.  }.
cf650 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69    close(fd);.  i
cf660 66 28 20 64 2e 72 65 73 75 6c 74 21 3d 30 20 29  f( d.result!=0 )
cf670 20 72 65 74 75 72 6e 3b 0a 20 20 74 68 72 65 61   return;.  threa
cf680 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
cf690 68 65 72 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c  hersLocks = (d.l
cf6a0 6f 63 6b 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e  ock.l_type==F_UN
cf6b0 4c 43 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  LCK);.}.#endif /
cf6c0 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  * SQLITE_THREADS
cf6d0 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  AFE && defined(_
cf6e0 5f 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a  _linux__) */../*
cf6f0 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e  .** Release a un
cf700 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63  ixLockInfo struc
cf710 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20  ture previously 
cf720 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e  allocated by fin
cf730 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a  dLockInfo()..**.
cf740 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74  ** The mutex ent
cf750 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75  ered using the u
cf760 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20  nixEnterMutex() 
cf770 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  function must be
cf780 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68   held.** when th
cf790 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
cf7a0 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  alled..*/.static
cf7b0 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4c 6f 63   void releaseLoc
cf7c0 6b 49 6e 66 6f 28 73 74 72 75 63 74 20 75 6e 69  kInfo(struct uni
cf7d0 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b  xLockInfo *pLock
cf7e0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69  ){.  assert( uni
cf7f0 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a  xMutexHeld() );.
cf800 20 20 69 66 28 20 70 4c 6f 63 6b 20 29 7b 0a 20    if( pLock ){. 
cf810 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d     pLock->nRef--
cf820 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d  ;.    if( pLock-
cf830 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
cf840 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 50 72    if( pLock->pPr
cf850 65 76 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  ev ){.        as
cf860 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 50 72  sert( pLock->pPr
cf870 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b  ev->pNext==pLock
cf880 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63   );.        pLoc
cf890 6b 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20  k->pPrev->pNext 
cf8a0 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a  = pLock->pNext;.
cf8b0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
cf8c0 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63       assert( loc
cf8d0 6b 4c 69 73 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a  kList==pLock );.
cf8e0 20 20 20 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74          lockList
cf8f0 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b   = pLock->pNext;
cf900 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
cf910 66 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20  f( pLock->pNext 
cf920 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
cf930 74 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d  t( pLock->pNext-
cf940 3e 70 50 72 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b  >pPrev==pLock );
cf950 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e  .        pLock->
cf960 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70  pNext->pPrev = p
cf970 4c 6f 63 6b 2d 3e 70 50 72 65 76 3b 0a 20 20 20  Lock->pPrev;.   
cf980 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
cf990 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a  e3_free(pLock);.
cf9a0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
cf9b0 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69  ** Release a uni
cf9c0 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75  xOpenCnt structu
cf9d0 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  re previously al
cf9e0 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c  located by findL
cf9f0 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a  ockInfo()..**.**
cfa00 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72   The mutex enter
cfa10 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69  ed using the uni
cfa20 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75  xEnterMutex() fu
cfa30 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68  nction must be h
cfa40 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73  eld.** when this
cfa50 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
cfa60 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  led..*/.static v
cfa70 6f 69 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43  oid releaseOpenC
cfa80 6e 74 28 73 74 72 75 63 74 20 75 6e 69 78 4f 70  nt(struct unixOp
cfa90 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 29 7b 0a 20  enCnt *pOpen){. 
cfaa0 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74   assert( unixMut
cfab0 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66  exHeld() );.  if
cfac0 28 20 70 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70  ( pOpen ){.    p
cfad0 4f 70 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  Open->nRef--;.  
cfae0 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65    if( pOpen->nRe
cfaf0 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  f==0 ){.      if
cfb00 28 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29  ( pOpen->pPrev )
cfb10 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
cfb20 28 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e  ( pOpen->pPrev->
cfb30 70 4e 65 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a  pNext==pOpen );.
cfb40 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70          pOpen->p
cfb50 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f  Prev->pNext = pO
cfb60 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pen->pNext;.    
cfb70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
cfb80 20 61 73 73 65 72 74 28 20 6f 70 65 6e 4c 69 73   assert( openLis
cfb90 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20  t==pOpen );.    
cfba0 20 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70      openList = p
cfbb0 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Open->pNext;.   
cfbc0 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
cfbd0 4f 70 65 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  Open->pNext ){. 
cfbe0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
cfbf0 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72  Open->pNext->pPr
cfc00 65 76 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20  ev==pOpen );.   
cfc10 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78       pOpen->pNex
cfc20 74 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e  t->pPrev = pOpen
cfc30 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d  ->pPrev;.      }
cfc40 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
cfc50 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65  ADSAFE && define
cfc60 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 20  d(__linux__).   
cfc70 20 20 20 61 73 73 65 72 74 28 20 21 70 4f 70 65     assert( !pOpe
cfc80 6e 2d 3e 70 55 6e 75 73 65 64 20 7c 7c 20 74 68  n->pUnused || th
cfc90 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63  readsOverrideEac
cfca0 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20  hOthersLocks==0 
cfcb0 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20  );.#endif..     
cfcc0 20 2f 2a 20 49 66 20 70 4f 70 65 6e 2d 3e 70 55   /* If pOpen->pU
cfcd0 6e 75 73 65 64 20 69 73 20 6e 6f 74 20 6e 75 6c  nused is not nul
cfce0 6c 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 61  l, then memory a
cfcf0 6e 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74  nd file-descript
cfd00 6f 72 73 0a 20 20 20 20 20 20 2a 2a 20 61 72 65  ors.      ** are
cfd10 20 6c 65 61 6b 65 64 2e 0a 20 20 20 20 20 20 2a   leaked..      *
cfd20 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20  *.      ** This 
cfd30 77 69 6c 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e  will only happen
cfd40 20 69 66 2c 20 75 6e 64 65 72 20 4c 69 6e 75 78   if, under Linux
cfd50 74 68 72 65 61 64 73 2c 20 74 68 65 20 75 73 65  threads, the use
cfd60 72 20 68 61 73 20 6f 70 65 6e 65 64 0a 20 20 20  r has opened.   
cfd70 20 20 20 2a 2a 20 61 20 74 72 61 6e 73 61 63 74     ** a transact
cfd80 69 6f 6e 20 69 6e 20 6f 6e 65 20 74 68 72 65 61  ion in one threa
cfd90 64 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73  d, then attempts
cfda0 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 64 61   to close the da
cfdb0 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20  tabase.      ** 
cfdc0 68 61 6e 64 6c 65 20 66 72 6f 6d 20 61 6e 6f 74  handle from anot
cfdd0 68 65 72 20 74 68 72 65 61 64 20 28 77 69 74 68  her thread (with
cfde0 6f 75 74 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b  out first unlock
cfdf0 69 6e 67 20 74 68 65 20 64 62 20 66 69 6c 65 29  ing the db file)
cfe00 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20  ..      ** This 
cfe10 69 73 20 61 20 6d 69 73 75 73 65 2e 20 20 2a 2f  is a misuse.  */
cfe20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
cfe30 72 65 65 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20  ree(pOpen);.    
cfe40 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  }.  }.}../*.** G
cfe50 69 76 65 6e 20 61 20 66 69 6c 65 20 64 65 73 63  iven a file desc
cfe60 72 69 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20 75  riptor, locate u
cfe70 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20  nixLockInfo and 
cfe80 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75  unixOpenCnt stru
cfe90 63 74 75 72 65 73 20 74 68 61 74 0a 2a 2a 20 64  ctures that.** d
cfea0 65 73 63 72 69 62 65 73 20 74 68 61 74 20 66 69  escribes that fi
cfeb0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20  le descriptor.  
cfec0 43 72 65 61 74 65 20 6e 65 77 20 6f 6e 65 73 20  Create new ones 
cfed0 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54  if necessary.  T
cfee0 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c  he.** return val
cfef0 75 65 73 20 6d 69 67 68 74 20 62 65 20 75 6e 69  ues might be uni
cff00 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61 6e  nitialized if an
cff10 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a   error occurs..*
cff20 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65  *.** The mutex e
cff30 6e 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65  ntered using the
cff40 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
cff50 29 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20  ) function must 
cff60 62 65 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20  be held.** when 
cff70 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
cff80 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52   called..**.** R
cff90 65 74 75 72 6e 20 61 6e 20 61 70 70 72 6f 70 72  eturn an appropr
cffa0 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e  iate error code.
cffb0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
cffc0 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75  indLockInfo(.  u
cffd0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20  nixFile *pFile, 
cffe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
cfff0 20 55 6e 69 78 20 66 69 6c 65 20 77 69 74 68 20   Unix file with 
d0000 66 69 6c 65 20 64 65 73 63 20 75 73 65 64 20 69  file desc used i
d0010 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73  n the key */.  s
d0020 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e  truct unixLockIn
d0030 66 6f 20 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a  fo **ppLock,  /*
d0040 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69 78   Return the unix
d0050 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75  LockInfo structu
d0060 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72  re here */.  str
d0070 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  uct unixOpenCnt 
d0080 2a 2a 70 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52  **ppOpen    /* R
d0090 65 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4f 70  eturn the unixOp
d00a0 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20  enCnt structure 
d00b0 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
d00c0 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
d00d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
d00e0 79 73 74 65 6d 20 63 61 6c 6c 20 72 65 74 75 72  ystem call retur
d00f0 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  n code */.  int 
d0100 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  fd;             
d0110 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
d0120 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
d0130 72 20 66 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20  r for pFile */. 
d0140 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b   struct unixLock
d0150 4b 65 79 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20  Key lockKey;    
d0160 2f 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f  /* Lookup key fo
d0170 72 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e  r the unixLockIn
d0180 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  fo structure */.
d0190 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c    struct unixFil
d01a0 65 49 64 20 66 69 6c 65 49 64 3b 20 20 20 20 20  eId fileId;     
d01b0 20 2f 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66   /* Lookup key f
d01c0 6f 72 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43  or the unixOpenC
d01d0 6e 74 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 73  nt struct */.  s
d01e0 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74 62  truct stat statb
d01f0 75 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  uf;           /*
d0200 20 4c 6f 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20   Low-level file 
d0210 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20  information */. 
d0220 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b   struct unixLock
d0230 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b  Info *pLock = 0;
d0240 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69  /* Candidate uni
d0250 78 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74  xLockInfo object
d0260 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69   */.  struct uni
d0270 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b  xOpenCnt *pOpen;
d0280 20 20 20 20 20 2f 2a 20 43 61 6e 64 69 64 61 74       /* Candidat
d0290 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62  e unixOpenCnt ob
d02a0 6a 65 63 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72  ject */..  asser
d02b0 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64  t( unixMutexHeld
d02c0 28 29 20 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20  () );..  /* Get 
d02d0 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d  low-level inform
d02e0 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
d02f0 66 69 6c 65 20 74 68 61 74 20 77 65 20 63 61 6e  file that we can
d0300 20 75 73 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72   used to.  ** cr
d0310 65 61 74 65 20 61 20 75 6e 69 71 75 65 20 6e 61  eate a unique na
d0320 6d 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e  me for the file.
d0330 0a 20 20 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69  .  */.  fd = pFi
d0340 6c 65 2d 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73  le->h;.  rc = fs
d0350 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66  tat(fd, &statbuf
d0360 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29  );.  if( rc!=0 )
d0370 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
d0380 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d0390 23 69 66 64 65 66 20 45 4f 56 45 52 46 4c 4f 57  #ifdef EOVERFLOW
d03a0 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  .    if( pFile->
d03b0 6c 61 73 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52  lastErrno==EOVER
d03c0 46 4c 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51  FLOW ) return SQ
d03d0 4c 49 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64  LITE_NOLFS;.#end
d03e0 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  if.    return SQ
d03f0 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a  LITE_IOERR;.  }.
d0400 0a 23 69 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f  .#ifdef __APPLE_
d0410 5f 0a 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f  _.  /* On OS X o
d0420 6e 20 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73  n an msdos files
d0430 79 73 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65  ystem, the inode
d0440 20 6e 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72   number is repor
d0450 74 65 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65  ted.  ** incorre
d0460 63 74 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69  ctly for zero-si
d0470 7a 65 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74  ze files.  See t
d0480 69 63 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f  icket #3260.  To
d0490 20 77 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e   work.  ** aroun
d04a0 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28  d this problem (
d04b0 77 65 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61  we consider it a
d04c0 20 62 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f   bug in OS X, no
d04d0 74 20 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77  t SQLite).  ** w
d04e0 65 20 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73  e always increas
d04f0 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20  e the file size 
d0500 74 6f 20 31 20 62 79 20 77 72 69 74 69 6e 67 20  to 1 by writing 
d0510 61 20 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20  a single byte.  
d0520 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65  ** prior to acce
d0530 73 73 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20  ssing the inode 
d0540 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65  number.  The one
d0550 20 62 79 74 65 20 77 72 69 74 74 65 6e 20 69 73   byte written is
d0560 0a 20 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27  .  ** an ASCII '
d0570 53 27 20 63 68 61 72 61 63 74 65 72 20 77 68 69  S' character whi
d0580 63 68 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20  ch also happens 
d0590 74 6f 20 62 65 20 74 68 65 20 66 69 72 73 74 20  to be the first 
d05a0 62 79 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65  byte.  ** in the
d05b0 20 68 65 61 64 65 72 20 6f 66 20 65 76 65 72 79   header of every
d05c0 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
d05d0 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20  .  In this way, 
d05e0 69 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73  if there.  ** is
d05f0 20 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f   a race conditio
d0600 6e 20 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74  n such that anot
d0610 68 65 72 20 74 68 72 65 61 64 20 68 61 73 20 61  her thread has a
d0620 6c 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64  lready populated
d0630 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20  .  ** the first 
d0640 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
d0650 62 61 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20  base, no damage 
d0660 69 73 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20  is done..  */.  
d0670 69 66 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73  if( statbuf.st_s
d0680 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  ize==0 ){.    rc
d0690 20 3d 20 77 72 69 74 65 28 66 64 2c 20 22 53 22   = write(fd, "S"
d06a0 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 1);.    if( rc
d06b0 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74  !=1 ){.      ret
d06c0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
d06d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
d06e0 20 66 73 74 61 74 28 66 64 2c 20 26 73 74 61 74   fstat(fd, &stat
d06f0 62 75 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63  buf);.    if( rc
d0700 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 46 69  !=0 ){.      pFi
d0710 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d0720 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74  errno;.      ret
d0730 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
d0740 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
d0750 69 66 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f  if..  memset(&lo
d0760 63 6b 4b 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66  ckKey, 0, sizeof
d0770 28 6c 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f  (lockKey));.  lo
d0780 63 6b 4b 65 79 2e 66 69 64 2e 64 65 76 20 3d 20  ckKey.fid.dev = 
d0790 73 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a  statbuf.st_dev;.
d07a0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
d07b0 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64   lockKey.fid.pId
d07c0 20 3d 20 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23   = pFile->pId;.#
d07d0 65 6c 73 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66  else.  lockKey.f
d07e0 69 64 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66  id.ino = statbuf
d07f0 2e 73 74 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a  .st_ino;.#endif.
d0800 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
d0810 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64  DSAFE && defined
d0820 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66  (__linux__).  if
d0830 28 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64  ( threadsOverrid
d0840 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73  eEachOthersLocks
d0850 3c 30 20 29 7b 0a 20 20 20 20 74 65 73 74 54 68  <0 ){.    testTh
d0860 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76  readLockingBehav
d0870 69 6f 72 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c  ior(fd);.  }.  l
d0880 6f 63 6b 4b 65 79 2e 74 69 64 20 3d 20 74 68 72  ockKey.tid = thr
d0890 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
d08a0 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20  OthersLocks ? 0 
d08b0 3a 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  : pthread_self()
d08c0 3b 0a 23 65 6e 64 69 66 0a 20 20 66 69 6c 65 49  ;.#endif.  fileI
d08d0 64 20 3d 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b  d = lockKey.fid;
d08e0 0a 20 20 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30  .  if( ppLock!=0
d08f0 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20   ){.    pLock = 
d0900 6c 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68  lockList;.    wh
d0910 69 6c 65 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65  ile( pLock && me
d0920 6d 63 6d 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26  mcmp(&lockKey, &
d0930 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20  pLock->lockKey, 
d0940 73 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29  sizeof(lockKey))
d0950 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20   ){.      pLock 
d0960 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a  = pLock->pNext;.
d0970 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c      }.    if( pL
d0980 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ock==0 ){.      
d0990 70 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f  pLock = sqlite3_
d09a0 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a  malloc( sizeof(*
d09b0 70 4c 6f 63 6b 29 20 29 3b 0a 20 20 20 20 20 20  pLock) );.      
d09c0 69 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a  if( pLock==0 ){.
d09d0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
d09e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
d09f0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e     goto exit_fin
d0a00 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20  dlockinfo;.     
d0a10 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28   }.      memcpy(
d0a20 26 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c  &pLock->lockKey,
d0a30 26 6c 6f 63 6b 4b 65 79 2c 73 69 7a 65 6f 66 28  &lockKey,sizeof(
d0a40 6c 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 20 20 20  lockKey));.     
d0a50 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 20 3d 20 31   pLock->nRef = 1
d0a60 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63  ;.      pLock->c
d0a70 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c  nt = 0;.      pL
d0a80 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ock->locktype = 
d0a90 30 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e  0;.      pLock->
d0aa0 70 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c 69 73 74  pNext = lockList
d0ab0 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70  ;.      pLock->p
d0ac0 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20  Prev = 0;.      
d0ad0 69 66 28 20 6c 6f 63 6b 4c 69 73 74 20 29 20 6c  if( lockList ) l
d0ae0 6f 63 6b 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d  ockList->pPrev =
d0af0 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 6c 6f   pLock;.      lo
d0b00 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 3b 0a  ckList = pLock;.
d0b10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d0b20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2b 2b 3b 0a   pLock->nRef++;.
d0b30 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4c 6f 63      }.    *ppLoc
d0b40 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 20  k = pLock;.  }. 
d0b50 20 69 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29   if( ppOpen!=0 )
d0b60 7b 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 6f 70  {.    pOpen = op
d0b70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c  enList;.    whil
d0b80 65 28 20 70 4f 70 65 6e 20 26 26 20 6d 65 6d 63  e( pOpen && memc
d0b90 6d 70 28 26 66 69 6c 65 49 64 2c 20 26 70 4f 70  mp(&fileId, &pOp
d0ba0 65 6e 2d 3e 66 69 6c 65 49 64 2c 20 73 69 7a 65  en->fileId, size
d0bb0 6f 66 28 66 69 6c 65 49 64 29 29 20 29 7b 0a 20  of(fileId)) ){. 
d0bc0 20 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 4f 70       pOpen = pOp
d0bd0 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  en->pNext;.    }
d0be0 0a 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d  .    if( pOpen==
d0bf0 30 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e  0 ){.      pOpen
d0c00 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
d0c10 63 28 20 73 69 7a 65 6f 66 28 2a 70 4f 70 65 6e  c( sizeof(*pOpen
d0c20 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ) );.      if( p
d0c30 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Open==0 ){.     
d0c40 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e     releaseLockIn
d0c50 66 6f 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20  fo(pLock);.     
d0c60 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
d0c70 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f  OMEM;.        go
d0c80 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b  to exit_findlock
d0c90 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20  info;.      }.  
d0ca0 20 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 65 6e      memset(pOpen
d0cb0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 70  , 0, sizeof(*pOp
d0cc0 65 6e 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 65  en));.      pOpe
d0cd0 6e 2d 3e 66 69 6c 65 49 64 20 3d 20 66 69 6c 65  n->fileId = file
d0ce0 49 64 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d  Id;.      pOpen-
d0cf0 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20  >nRef = 1;.     
d0d00 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 20 3d 20   pOpen->pNext = 
d0d10 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20  openList;.      
d0d20 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 20 6f  if( openList ) o
d0d30 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d  penList->pPrev =
d0d40 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f 70   pOpen;.      op
d0d50 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b 0a  enList = pOpen;.
d0d60 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d0d70 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b 3b 0a   pOpen->nRef++;.
d0d80 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f 70 65      }.    *ppOpe
d0d90 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d 0a 0a  n = pOpen;.  }..
d0da0 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66  exit_findlockinf
d0db0 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  o:.  return rc;.
d0dc0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61  }../*.** If we a
d0dd0 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  re currently in 
d0de0 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65  a different thre
d0df0 61 64 20 74 68 61 6e 20 74 68 65 20 74 68 72 65  ad than the thre
d0e00 61 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75  ad that the.** u
d0e10 6e 69 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74  nixFile argument
d0e20 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65   belongs to, the
d0e30 6e 20 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72  n transfer owner
d0e40 73 68 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78  ship of the unix
d0e50 46 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20  File.** over to 
d0e60 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65  the current thre
d0e70 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78  ad..**.** A unix
d0e80 46 69 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e  File is only own
d0e90 65 64 20 62 79 20 61 20 74 68 72 65 61 64 20 6f  ed by a thread o
d0ea0 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 75  n systems that u
d0eb0 73 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e  se LinuxThreads.
d0ec0 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 69 70  .**.** Ownership
d0ed0 20 74 72 61 6e 73 66 65 72 20 69 73 20 6f 6e 6c   transfer is onl
d0ee0 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 74 68 65  y allowed if the
d0ef0 20 75 6e 69 78 46 69 6c 65 20 69 73 20 63 75 72   unixFile is cur
d0f00 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 2e  rently unlocked.
d0f10 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 69 78 46  .** If the unixF
d0f20 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 61 6e  ile is locked an
d0f30 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 70 20 69  d an ownership i
d0f40 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20 72 65  s wrong, then re
d0f50 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  turn.** SQLITE_M
d0f60 49 53 55 53 45 2e 20 20 53 51 4c 49 54 45 5f 4f  ISUSE.  SQLITE_O
d0f70 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  K is returned if
d0f80 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b   everything work
d0f90 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  s..*/.#if SQLITE
d0fa0 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64  _THREADSAFE && d
d0fb0 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f  efined(__linux__
d0fc0 29 0a 73 74 61 74 69 63 20 69 6e 74 20 74 72 61  ).static int tra
d0fd0 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 75  nsferOwnership(u
d0fe0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b  nixFile *pFile){
d0ff0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 74 68  .  int rc;.  pth
d1000 72 65 61 64 5f 74 20 68 53 65 6c 66 3b 0a 20 20  read_t hSelf;.  
d1010 69 66 28 20 74 68 72 65 61 64 73 4f 76 65 72 72  if( threadsOverr
d1020 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63  ideEachOthersLoc
d1030 6b 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e  ks ){.    /* Own
d1040 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 73  ership transfers
d1050 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e 20 74   not needed on t
d1060 68 69 73 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20  his system */.  
d1070 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d1080 4f 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 6c 66 20  OK;.  }.  hSelf 
d1090 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
d10a0 3b 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 5f  ;.  if( pthread_
d10b0 65 71 75 61 6c 28 70 46 69 6c 65 2d 3e 74 69 64  equal(pFile->tid
d10c0 2c 20 68 53 65 6c 66 29 20 29 7b 0a 20 20 20 20  , hSelf) ){.    
d10d0 2f 2a 20 57 65 20 61 72 65 20 73 74 69 6c 6c 20  /* We are still 
d10e0 69 6e 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  in the same thre
d10f0 61 64 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43  ad */.    OSTRAC
d1100 45 31 28 22 4e 6f 2d 74 72 61 6e 73 66 65 72 2c  E1("No-transfer,
d1110 20 73 61 6d 65 20 74 68 72 65 61 64 5c 6e 22 29   same thread\n")
d1120 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
d1130 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
d1140 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
d1150 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20  e!=NO_LOCK ){.  
d1160 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 63    /* We cannot c
d1170 68 61 6e 67 65 20 6f 77 6e 65 72 73 68 69 70 20  hange ownership 
d1180 77 68 69 6c 65 20 77 65 20 61 72 65 20 68 6f 6c  while we are hol
d1190 64 69 6e 67 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a  ding a lock! */.
d11a0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d11b0 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
d11c0 4f 53 54 52 41 43 45 34 28 22 54 72 61 6e 73 66  OSTRACE4("Transf
d11d0 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20  er ownership of 
d11e0 25 64 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64  %d from %d to %d
d11f0 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
d1200 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65   pFile->h, pFile
d1210 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 3b 0a 20  ->tid, hSelf);. 
d1220 20 70 46 69 6c 65 2d 3e 74 69 64 20 3d 20 68 53   pFile->tid = hS
d1230 65 6c 66 3b 0a 20 20 69 66 20 28 70 46 69 6c 65  elf;.  if (pFile
d1240 2d 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29  ->pLock != NULL)
d1250 20 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f   {.    releaseLo
d1260 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c  ckInfo(pFile->pL
d1270 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 66  ock);.    rc = f
d1280 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c  indLockInfo(pFil
d1290 65 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  e, &pFile->pLock
d12a0 2c 20 30 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  , 0);.    OSTRAC
d12b0 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 69  E5("LOCK    %d i
d12c0 73 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 5c  s now %s(%s,%d)\
d12d0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
d12e0 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70           locktyp
d12f0 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63  eName(pFile->loc
d1300 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 20 20  ktype),.        
d1310 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28     locktypeName(
d1320 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f  pFile->pLock->lo
d1330 63 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 2d 3e  cktype), pFile->
d1340 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 20  pLock->cnt);.   
d1350 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20   return rc;.  } 
d1360 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72  else {.    retur
d1370 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d1380 0a 7d 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66 20  .}.#else  /* if 
d1390 6e 6f 74 20 53 51 4c 49 54 45 5f 54 48 52 45 41  not SQLITE_THREA
d13a0 44 53 41 46 45 20 2a 2f 0a 20 20 2f 2a 20 4f 6e  DSAFE */.  /* On
d13b0 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64   single-threaded
d13c0 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 68   builds, ownersh
d13d0 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 61  ip transfer is a
d13e0 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69   no-op */.# defi
d13f0 6e 65 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72  ne transferOwner
d1400 73 68 69 70 28 58 29 20 53 51 4c 49 54 45 5f 4f  ship(X) SQLITE_O
d1410 4b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  K.#endif /* SQLI
d1420 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f  TE_THREADSAFE */
d1430 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  .../*.** This ro
d1440 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
d1450 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
d1460 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
d1470 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
d1480 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
d1490 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
d14a0 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
d14b0 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74  ock is held, set
d14c0 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20   *pResOut.** to 
d14d0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  a non-zero value
d14e0 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73   otherwise *pRes
d14f0 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65  Out is set to ze
d1500 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  ro.  The return 
d1510 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20  value.** is set 
d1520 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c  to SQLITE_OK unl
d1530 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72  ess an I/O error
d1540 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c   occurs during l
d1550 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f  ock checking..*/
d1560 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
d1570 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d1580 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d1590 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
d15a0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
d15b0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72  LITE_OK;.  int r
d15c0 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75  eserved = 0;.  u
d15d0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d15e0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
d15f0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
d1600 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
d1610 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
d1620 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 0a 20  ERVEDLOCK; );.. 
d1630 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d1640 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  ;.  unixEnterMut
d1650 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65  ex(); /* Because
d1660 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73   pFile->pLock is
d1670 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74   shared across t
d1680 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20  hreads */..  /* 
d1690 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61  Check if a threa
d16a0 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73  d in this proces
d16b0 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c  s holds such a l
d16c0 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ock */.  if( pFi
d16d0 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74  le->pLock->lockt
d16e0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
d16f0 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20  ){.    reserved 
d1700 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f  = 1;.  }..  /* O
d1710 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 20  therwise see if 
d1720 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65  some other proce
d1730 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a  ss holds it..  *
d1740 2f 0a 23 69 66 6e 64 65 66 20 5f 5f 44 4a 47 50  /.#ifndef __DJGP
d1750 50 5f 5f 0a 20 20 69 66 28 20 21 72 65 73 65 72  P__.  if( !reser
d1760 76 65 64 20 29 7b 0a 20 20 20 20 73 74 72 75 63  ved ){.    struc
d1770 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20  t flock lock;.  
d1780 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20    lock.l_whence 
d1790 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20  = SEEK_SET;.    
d17a0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52  lock.l_start = R
d17b0 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20  ESERVED_BYTE;.  
d17c0 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31    lock.l_len = 1
d17d0 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  ;.    lock.l_typ
d17e0 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20  e = F_WRLCK;.   
d17f0 20 69 66 20 28 2d 31 20 3d 3d 20 66 63 6e 74 6c   if (-1 == fcntl
d1800 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54  (pFile->h, F_GET
d1810 4c 4b 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a 20 20  LK, &lock)) {.  
d1820 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d      int tErrno =
d1830 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63   errno;.      rc
d1840 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72   = sqliteErrorFr
d1850 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72  omPosixError(tEr
d1860 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52  rno, SQLITE_IOER
d1870 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c  R_CHECKRESERVEDL
d1880 4f 43 4b 29 3b 0a 20 20 20 20 20 20 70 46 69 6c  OCK);.      pFil
d1890 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d18a0 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 6c 73  Errno;.    } els
d18b0 65 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70  e if( lock.l_typ
d18c0 65 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20  e!=F_UNLCK ){.  
d18d0 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31      reserved = 1
d18e0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
d18f0 69 66 0a 20 20 0a 20 20 75 6e 69 78 4c 65 61 76  if.  .  unixLeav
d1900 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52  eMutex();.  OSTR
d1910 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f  ACE4("TEST WR-LO
d1920 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20  CK %d %d %d\n", 
d1930 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65  pFile->h, rc, re
d1940 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65  served);..  *pRe
d1950 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b  sOut = reserved;
d1960 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
d1970 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61  ./*.** Perform a
d1980 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 6f 70   file locking op
d1990 65 72 61 74 69 6f 6e 20 6f 6e 20 61 20 72 61 6e  eration on a ran
d19a0 67 65 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61  ge of bytes in a
d19b0 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 22 6f   file..** The "o
d19c0 70 22 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f  p" parameter sho
d19d0 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20 46 5f  uld be one of F_
d19e0 52 44 4c 43 4b 2c 20 46 5f 57 52 4c 43 4b 2c 20  RDLCK, F_WRLCK, 
d19f0 6f 72 20 46 5f 55 4e 4c 43 4b 2e 0a 2a 2a 20 52  or F_UNLCK..** R
d1a00 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65  eturn 0 on succe
d1a10 73 73 20 6f 72 20 2d 31 20 66 6f 72 20 66 61 69  ss or -1 for fai
d1a20 6c 75 72 65 2e 20 20 4f 6e 20 66 61 69 6c 75 72  lure.  On failur
d1a30 65 2c 20 77 72 69 74 65 20 74 68 65 20 65 72 72  e, write the err
d1a40 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 6e 74 6f 20  or.** code into 
d1a50 2a 70 45 72 72 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  *pErrcode..**.**
d1a60 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 57   If the SQLITE_W
d1a70 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e  HOLE_FILE_LOCKIN
d1a80 47 20 62 69 74 20 69 73 20 63 6c 65 61 72 2c 20  G bit is clear, 
d1a90 74 68 65 6e 20 6f 6e 6c 79 20 6c 6f 63 6b 0a 2a  then only lock.*
d1aa0 2a 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62  * the range of b
d1ab0 79 74 65 73 20 6f 6e 20 74 68 65 20 6c 6f 63 6b  ytes on the lock
d1ac0 69 6e 67 20 70 61 67 65 20 62 65 74 77 65 65 6e  ing page between
d1ad0 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 61 6e   SHARED_FIRST an
d1ae0 64 0a 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a 45  d.** SHARED_SIZE
d1af0 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 57 48 4f  .  If SQLITE_WHO
d1b00 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20  LE_FILE_LOCKING 
d1b10 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6c 6f 63  is set, then loc
d1b20 6b 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 66  k all.** bytes f
d1b30 72 6f 6d 20 30 20 75 70 20 74 6f 20 62 75 74 20  rom 0 up to but 
d1b40 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 50 45  not including PE
d1b50 4e 44 49 4e 47 5f 42 59 54 45 2c 20 61 6e 64 20  NDING_BYTE, and 
d1b60 61 6c 6c 20 62 79 74 65 73 0a 2a 2a 20 74 68 61  all bytes.** tha
d1b70 74 20 66 6f 6c 6c 6f 77 20 53 48 41 52 45 44 5f  t follow SHARED_
d1b80 46 49 52 53 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  FIRST..**.** In 
d1b90 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6f 66 20  other words, of 
d1ba0 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c  SQLITE_WHOLE_FIL
d1bb0 45 5f 4c 4f 43 4b 49 4e 47 20 69 66 20 66 61 6c  E_LOCKING if fal
d1bc0 73 65 20 28 74 68 65 20 68 69 73 74 6f 72 69 63  se (the historic
d1bd0 61 6c 0a 2a 2a 20 64 65 66 61 75 6c 74 20 63 61  al.** default ca
d1be0 73 65 29 20 74 68 65 6e 20 6f 6e 6c 79 20 6c 6f  se) then only lo
d1bf0 63 6b 20 61 20 73 6d 61 6c 6c 20 72 61 6e 67 65  ck a small range
d1c00 20 6f 66 20 62 79 74 65 73 20 66 72 6f 6d 20 53   of bytes from S
d1c10 48 41 52 45 44 5f 46 49 52 53 54 0a 2a 2a 20 74  HARED_FIRST.** t
d1c20 68 72 6f 75 67 68 20 53 48 41 52 45 44 5f 46 49  hrough SHARED_FI
d1c30 52 53 54 2b 53 48 41 52 45 44 5f 53 49 5a 45 2d  RST+SHARED_SIZE-
d1c40 31 2e 20 20 42 75 74 20 69 66 20 53 51 4c 49 54  1.  But if SQLIT
d1c50 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43  E_WHOLE_FILE_LOC
d1c60 4b 49 4e 47 20 69 73 0a 2a 2a 20 74 72 75 65 20  KING is.** true 
d1c70 74 68 65 6e 20 6c 6f 63 6b 20 65 76 65 72 79 20  then lock every 
d1c80 62 79 74 65 20 69 6e 20 74 68 65 20 66 69 6c 65  byte in the file
d1c90 20 65 78 63 65 70 74 20 66 6f 72 20 50 45 4e 44   except for PEND
d1ca0 49 4e 47 5f 42 59 54 45 20 61 6e 64 0a 2a 2a 20  ING_BYTE and.** 
d1cb0 52 45 53 45 52 56 45 44 5f 42 59 54 45 2e 0a 2a  RESERVED_BYTE..*
d1cc0 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 57 48 4f 4c  *.** SQLITE_WHOL
d1cd0 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74  E_FILE_LOCKING=t
d1ce0 72 75 65 20 6f 76 65 72 6c 61 70 73 20 53 51 4c  rue overlaps SQL
d1cf0 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c  ITE_WHOLE_FILE_L
d1d00 4f 43 4b 49 4e 47 3d 66 61 6c 73 65 0a 2a 2a 20  OCKING=false.** 
d1d10 61 6e 64 20 73 6f 20 74 68 65 20 6c 6f 63 6b 69  and so the locki
d1d20 6e 67 20 73 63 68 65 6d 65 73 20 61 72 65 20 63  ng schemes are c
d1d30 6f 6d 70 61 74 69 62 6c 65 2e 20 20 4f 6e 65 20  ompatible.  One 
d1d40 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 77 69 6c  type of lock wil
d1d50 6c 0a 2a 2a 20 65 66 66 65 63 74 69 76 65 6c 79  l.** effectively
d1d60 20 65 78 63 6c 75 64 65 20 74 68 65 20 6f 74 68   exclude the oth
d1d70 65 72 20 74 79 70 65 2e 20 20 54 68 65 20 72 65  er type.  The re
d1d80 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20 74  ason for using t
d1d90 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 57 48 4f  he.** SQLITE_WHO
d1da0 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d  LE_FILE_LOCKING=
d1db0 74 72 75 65 20 69 73 20 74 68 61 74 20 62 79 20  true is that by 
d1dc0 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 66  indicating the f
d1dd0 75 6c 6c 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20  ull range.** of 
d1de0 62 79 74 65 73 20 74 6f 20 62 65 20 72 65 61 64  bytes to be read
d1df0 20 6f 72 20 77 72 69 74 74 65 6e 2c 20 77 65 20   or written, we 
d1e00 67 69 76 65 20 68 69 6e 74 73 20 74 6f 20 4e 46  give hints to NF
d1e10 53 20 74 6f 20 68 65 6c 70 20 69 74 0a 2a 2a 20  S to help it.** 
d1e20 6d 61 69 6e 74 61 69 6e 20 63 61 63 68 65 20 63  maintain cache c
d1e30 6f 68 65 72 65 6e 63 79 2e 20 20 4f 6e 20 74 68  oherency.  On th
d1e40 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 77 68  e other hand, wh
d1e50 6f 6c 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  ole file locking
d1e60 0a 2a 2a 20 69 73 20 73 6c 6f 77 65 72 2c 20 73  .** is slower, s
d1e70 6f 20 77 65 20 64 6f 6e 27 74 20 77 61 6e 74 20  o we don't want 
d1e80 74 6f 20 75 73 65 20 69 74 20 65 78 63 65 70 74  to use it except
d1e90 20 66 6f 72 20 4e 46 53 2e 0a 2a 2f 0a 73 74 61   for NFS..*/.sta
d1ea0 74 69 63 20 69 6e 74 20 72 61 6e 67 65 4c 6f 63  tic int rangeLoc
d1eb0 6b 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c  k(unixFile *pFil
d1ec0 65 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a  e, int op, int *
d1ed0 70 45 72 72 63 6f 64 65 29 7b 0a 20 20 73 74 72  pErrcode){.  str
d1ee0 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a  uct flock lock;.
d1ef0 20 20 69 6e 74 20 72 63 3b 0a 20 20 6c 6f 63 6b    int rc;.  lock
d1f00 2e 6c 5f 74 79 70 65 20 3d 20 6f 70 3b 0a 20 20  .l_type = op;.  
d1f10 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53  lock.l_start = S
d1f20 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 6c  HARED_FIRST;.  l
d1f30 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53  ock.l_whence = S
d1f40 45 45 4b 5f 53 45 54 3b 0a 20 20 69 66 28 20 28  EEK_SET;.  if( (
d1f50 70 46 69 6c 65 2d 3e 66 69 6c 65 46 6c 61 67 73  pFile->fileFlags
d1f60 20 26 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f   & SQLITE_WHOLE_
d1f70 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 29 3d 3d 30  FILE_LOCKING)==0
d1f80 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c   ){.    lock.l_l
d1f90 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45  en = SHARED_SIZE
d1fa0 3b 0a 20 20 20 20 72 63 20 3d 20 66 63 6e 74 6c  ;.    rc = fcntl
d1fb0 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54  (pFile->h, F_SET
d1fc0 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20  LK, &lock);.    
d1fd0 2a 70 45 72 72 63 6f 64 65 20 3d 20 65 72 72 6e  *pErrcode = errn
d1fe0 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  o;.  }else{.    
d1ff0 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a  lock.l_len = 0;.
d2000 20 20 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 70      rc = fcntl(p
d2010 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b  File->h, F_SETLK
d2020 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 2a 70  , &lock);.    *p
d2030 45 72 72 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b  Errcode = errno;
d2040 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 6f  .    if( NEVER(o
d2050 70 3d 3d 46 5f 55 4e 4c 43 4b 29 20 7c 7c 20 72  p==F_UNLCK) || r
d2060 63 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20  c!=(-1) ){.     
d2070 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20   lock.l_start = 
d2080 30 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  0;.      lock.l_
d2090 6c 65 6e 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59  len = PENDING_BY
d20a0 54 45 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66  TE;.      rc = f
d20b0 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46  cntl(pFile->h, F
d20c0 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a  _SETLK, &lock);.
d20d0 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
d20e0 28 6f 70 21 3d 46 5f 55 4e 4c 43 4b 29 20 26 26  (op!=F_UNLCK) &&
d20f0 20 72 63 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20   rc==(-1) ){.   
d2100 20 20 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d       *pErrcode =
d2110 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20   errno;.        
d2120 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f  lock.l_type = F_
d2130 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 20 20 6c  UNLCK;.        l
d2140 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48  ock.l_start = SH
d2150 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20  ARED_FIRST;.    
d2160 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
d2170 20 30 3b 0a 20 20 20 20 20 20 20 20 66 63 6e 74   0;.        fcnt
d2180 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45  l(pFile->h, F_SE
d2190 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20  TLK, &lock);.   
d21a0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
d21b0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
d21c0 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69  *.** Lock the fi
d21d0 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b  le with the lock
d21e0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
d21f0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65  rameter locktype
d2200 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65   - one.** of the
d2210 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
d2220 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44  *     (1) SHARED
d2230 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29  _LOCK.**     (2)
d2240 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a   RESERVED_LOCK.*
d2250 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e  *     (3) PENDIN
d2260 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34  G_LOCK.**     (4
d2270 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ) EXCLUSIVE_LOCK
d2280 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
d2290 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67   when requesting
d22a0 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c   one lock state,
d22b0 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b   additional lock
d22c0 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69   states.** are i
d22d0 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65  nserted in betwe
d22e0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  en.  The locking
d22f0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f   might fail on o
d2300 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a  ne of the later.
d2310 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c  ** transitions l
d2320 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  eaving the lock 
d2330 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20  state different 
d2340 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61  from what it sta
d2350 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c  rted but.** stil
d2360 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67  l short of its g
d2370 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  oal.  The follow
d2380 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20  ing chart shows 
d2390 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74  the allowed.** t
d23a0 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74  ransitions and t
d23b0 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65  he inserted inte
d23c0 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a  rmediate states:
d23d0 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b  .**.**    UNLOCK
d23e0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20  ED -> SHARED.** 
d23f0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53     SHARED -> RES
d2400 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  ERVED.**    SHAR
d2410 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
d2420 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
d2430 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28     RESERVED -> (
d2440 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
d2450 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44  USIVE.**    PEND
d2460 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  ING -> EXCLUSIVE
d2470 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
d2480 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e  ine will only in
d2490 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20  crease a lock.  
d24a0 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f  Use the sqlite3O
d24b0 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75  sUnlock().** rou
d24c0 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20  tine to lower a 
d24d0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a  locking level..*
d24e0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
d24f0 78 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  xLock(sqlite3_fi
d2500 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
d2510 74 79 70 65 29 7b 0a 20 20 2f 2a 20 54 68 65 20  type){.  /* The 
d2520 66 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72 69  following descri
d2530 62 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  bes the implemen
d2540 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 61  tation of the va
d2550 72 69 6f 75 73 20 6c 6f 63 6b 73 20 61 6e 64 0a  rious locks and.
d2560 20 20 2a 2a 20 6c 6f 63 6b 20 74 72 61 6e 73 69    ** lock transi
d2570 74 69 6f 6e 73 20 69 6e 20 74 65 72 6d 73 20 6f  tions in terms o
d2580 66 20 74 68 65 20 50 4f 53 49 58 20 61 64 76 69  f the POSIX advi
d2590 73 6f 72 79 20 73 68 61 72 65 64 20 61 6e 64 20  sory shared and 
d25a0 65 78 63 6c 75 73 69 76 65 0a 20 20 2a 2a 20 6c  exclusive.  ** l
d25b0 6f 63 6b 20 70 72 69 6d 69 74 69 76 65 73 20 28  ock primitives (
d25c0 63 61 6c 6c 65 64 20 72 65 61 64 2d 6c 6f 63 6b  called read-lock
d25d0 73 20 61 6e 64 20 77 72 69 74 65 2d 6c 6f 63 6b  s and write-lock
d25e0 73 20 62 65 6c 6f 77 2c 20 74 6f 20 61 76 6f 69  s below, to avoi
d25f0 64 0a 20 20 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e  d.  ** confusion
d2600 20 77 69 74 68 20 53 51 4c 69 74 65 20 6c 6f 63   with SQLite loc
d2610 6b 20 6e 61 6d 65 73 29 2e 20 54 68 65 20 61 6c  k names). The al
d2620 67 6f 72 69 74 68 6d 73 20 61 72 65 20 63 6f 6d  gorithms are com
d2630 70 6c 69 63 61 74 65 64 0a 20 20 2a 2a 20 73 6c  plicated.  ** sl
d2640 69 67 68 74 6c 79 20 69 6e 20 6f 72 64 65 72 20  ightly in order 
d2650 74 6f 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65  to be compatible
d2660 20 77 69 74 68 20 77 69 6e 64 6f 77 73 20 73 79   with windows sy
d2670 73 74 65 6d 73 20 73 69 6d 75 6c 74 61 6e 65 6f  stems simultaneo
d2680 75 73 6c 79 0a 20 20 2a 2a 20 61 63 63 65 73 73  usly.  ** access
d2690 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74  ing the same dat
d26a0 61 62 61 73 65 20 66 69 6c 65 2c 20 69 6e 20 63  abase file, in c
d26b0 61 73 65 20 74 68 61 74 20 69 73 20 65 76 65 72  ase that is ever
d26c0 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a 0a   required..  **.
d26d0 20 20 2a 2a 20 53 79 6d 62 6f 6c 73 20 64 65 66    ** Symbols def
d26e0 69 6e 65 64 20 69 6e 20 6f 73 2e 68 20 69 6e 64  ined in os.h ind
d26f0 65 6e 74 69 66 79 20 74 68 65 20 27 70 65 6e 64  entify the 'pend
d2700 69 6e 67 20 62 79 74 65 27 20 61 6e 64 20 74 68  ing byte' and th
d2710 65 20 27 72 65 73 65 72 76 65 64 0a 20 20 2a 2a  e 'reserved.  **
d2720 20 62 79 74 65 27 2c 20 65 61 63 68 20 73 69 6e   byte', each sin
d2730 67 6c 65 20 62 79 74 65 73 20 61 74 20 77 65 6c  gle bytes at wel
d2740 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 73 2c  l known offsets,
d2750 20 61 6e 64 20 74 68 65 20 27 73 68 61 72 65 64   and the 'shared
d2760 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65   byte.  ** range
d2770 27 2c 20 61 20 72 61 6e 67 65 20 6f 66 20 35 31  ', a range of 51
d2780 30 20 62 79 74 65 73 20 61 74 20 61 20 77 65 6c  0 bytes at a wel
d2790 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 2e 0a  l known offset..
d27a0 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 6f 62 74    **.  ** To obt
d27b0 61 69 6e 20 61 20 53 48 41 52 45 44 20 6c 6f 63  ain a SHARED loc
d27c0 6b 2c 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69  k, a read-lock i
d27d0 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68  s obtained on th
d27e0 65 20 27 70 65 6e 64 69 6e 67 0a 20 20 2a 2a 20  e 'pending.  ** 
d27f0 62 79 74 65 27 2e 20 20 49 66 20 74 68 69 73 20  byte'.  If this 
d2800 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61  is successful, a
d2810 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f   random byte fro
d2820 6d 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79  m the 'shared by
d2830 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69  te.  ** range' i
d2840 73 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20 61 6e  s read-locked an
d2850 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68  d the lock on th
d2860 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27  e 'pending byte'
d2870 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2a 0a   released..  **.
d2880 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d    ** A process m
d2890 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61  ay only obtain a
d28a0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61   RESERVED lock a
d28b0 66 74 65 72 20 69 74 20 68 61 73 20 61 20 53 48  fter it has a SH
d28c0 41 52 45 44 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20  ARED lock..  ** 
d28d0 41 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  A RESERVED lock 
d28e0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62  is implemented b
d28f0 79 20 67 72 61 62 62 69 6e 67 20 61 20 77 72 69  y grabbing a wri
d2900 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20  te-lock on the. 
d2910 20 2a 2a 20 27 72 65 73 65 72 76 65 64 20 62 79   ** 'reserved by
d2920 74 65 27 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  te'. .  **.  ** 
d2930 41 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e  A process may on
d2940 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44  ly obtain a PEND
d2950 49 4e 47 20 6c 6f 63 6b 20 61 66 74 65 72 20 69  ING lock after i
d2960 74 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 61  t has obtained a
d2970 0a 20 20 2a 2a 20 53 48 41 52 45 44 20 6c 6f 63  .  ** SHARED loc
d2980 6b 2e 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63  k. A PENDING loc
d2990 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  k is implemented
d29a0 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 61 20   by obtaining a 
d29b0 77 72 69 74 65 2d 6c 6f 63 6b 0a 20 20 2a 2a 20  write-lock.  ** 
d29c0 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20  on the 'pending 
d29d0 62 79 74 65 27 2e 20 54 68 69 73 20 65 6e 73 75  byte'. This ensu
d29e0 72 65 73 20 74 68 61 74 20 6e 6f 20 6e 65 77 20  res that no new 
d29f0 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 63 61 6e  SHARED locks can
d2a00 20 62 65 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 65   be.  ** obtaine
d2a10 64 2c 20 62 75 74 20 65 78 69 73 74 69 6e 67 20  d, but existing 
d2a20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 61 72 65  SHARED locks are
d2a30 20 61 6c 6c 6f 77 65 64 20 74 6f 20 70 65 72 73   allowed to pers
d2a40 69 73 74 2e 20 41 20 70 72 6f 63 65 73 73 0a 20  ist. A process. 
d2a50 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76   ** does not hav
d2a60 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 52 45  e to obtain a RE
d2a70 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74  SERVED lock on t
d2a80 68 65 20 77 61 79 20 74 6f 20 61 20 50 45 4e 44  he way to a PEND
d2a90 49 4e 47 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 54  ING lock..  ** T
d2aa0 68 69 73 20 70 72 6f 70 65 72 74 79 20 69 73 20  his property is 
d2ab0 75 73 65 64 20 62 79 20 74 68 65 20 61 6c 67 6f  used by the algo
d2ac0 72 69 74 68 6d 20 66 6f 72 20 72 6f 6c 6c 69 6e  rithm for rollin
d2ad0 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c  g back a journal
d2ae0 20 66 69 6c 65 0a 20 20 2a 2a 20 61 66 74 65 72   file.  ** after
d2af0 20 61 20 63 72 61 73 68 2e 0a 20 20 2a 2a 0a 20   a crash..  **. 
d2b00 20 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45   ** An EXCLUSIVE
d2b10 20 6c 6f 63 6b 2c 20 6f 62 74 61 69 6e 65 64 20   lock, obtained 
d2b20 61 66 74 65 72 20 61 20 50 45 4e 44 49 4e 47 20  after a PENDING 
d2b30 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 69 73  lock is held, is
d2b40 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65  .  ** implemente
d2b50 64 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 61  d by obtaining a
d2b60 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74   write-lock on t
d2b70 68 65 20 65 6e 74 69 72 65 20 27 73 68 61 72 65  he entire 'share
d2b80 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67  d byte.  ** rang
d2b90 65 27 2e 20 53 69 6e 63 65 20 61 6c 6c 20 6f 74  e'. Since all ot
d2ba0 68 65 72 20 6c 6f 63 6b 73 20 72 65 71 75 69 72  her locks requir
d2bb0 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e  e a read-lock on
d2bc0 20 6f 6e 65 20 6f 66 20 74 68 65 20 62 79 74 65   one of the byte
d2bd0 73 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 74 68  s.  ** within th
d2be0 69 73 20 72 61 6e 67 65 2c 20 74 68 69 73 20 65  is range, this e
d2bf0 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f 20 6f  nsures that no o
d2c00 74 68 65 72 20 6c 6f 63 6b 73 20 61 72 65 20 68  ther locks are h
d2c10 65 6c 64 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  eld on the.  ** 
d2c20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 2a 2a 0a  database. .  **.
d2c30 20 20 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e 20    ** The reason 
d2c40 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63 61  a single byte ca
d2c50 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 69 6e 73  nnot be used ins
d2c60 74 65 61 64 20 6f 66 20 74 68 65 20 27 73 68 61  tead of the 'sha
d2c70 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61  red byte.  ** ra
d2c80 6e 67 65 27 20 69 73 20 74 68 61 74 20 73 6f 6d  nge' is that som
d2c90 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 69  e versions of wi
d2ca0 6e 64 6f 77 73 20 64 6f 20 6e 6f 74 20 73 75 70  ndows do not sup
d2cb0 70 6f 72 74 20 72 65 61 64 2d 6c 6f 63 6b 73 2e  port read-locks.
d2cc0 20 42 79 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67   By.  ** locking
d2cd0 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66   a random byte f
d2ce0 72 6f 6d 20 61 20 72 61 6e 67 65 2c 20 63 6f 6e  rom a range, con
d2cf0 63 75 72 72 65 6e 74 20 53 48 41 52 45 44 20 6c  current SHARED l
d2d00 6f 63 6b 73 20 6d 61 79 20 65 78 69 73 74 0a 20  ocks may exist. 
d2d10 20 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 65 20   ** even if the 
d2d20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76  locking primitiv
d2d30 65 20 75 73 65 64 20 69 73 20 61 6c 77 61 79 73  e used is always
d2d40 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2e 0a 20   a write-lock.. 
d2d50 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   */.  int rc = S
d2d60 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78  QLITE_OK;.  unix
d2d70 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d2d80 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73  nixFile*)id;.  s
d2d90 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e  truct unixLockIn
d2da0 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 70 46 69 6c  fo *pLock = pFil
d2db0 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75  e->pLock;.  stru
d2dc0 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20  ct flock lock;. 
d2dd0 20 69 6e 74 20 73 20 3d 20 30 3b 0a 20 20 69 6e   int s = 0;.  in
d2de0 74 20 74 45 72 72 6e 6f 3b 0a 0a 20 20 61 73 73  t tErrno;..  ass
d2df0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
d2e00 4f 53 54 52 41 43 45 37 28 22 4c 4f 43 4b 20 20  OSTRACE7("LOCK  
d2e10 20 20 25 64 20 25 73 20 77 61 73 20 25 73 28 25    %d %s was %s(%
d2e20 73 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c  s,%d) pid=%d\n",
d2e30 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20   pFile->h,.     
d2e40 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f   locktypeName(lo
d2e50 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70  cktype), locktyp
d2e60 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63  eName(pFile->loc
d2e70 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f  ktype),.      lo
d2e80 63 6b 74 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b  cktypeName(pLock
d2e90 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f  ->locktype), pLo
d2ea0 63 6b 2d 3e 63 6e 74 20 2c 20 67 65 74 70 69 64  ck->cnt , getpid
d2eb0 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ());..  /* If th
d2ec0 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
d2ed0 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79   lock of this ty
d2ee0 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72  pe or more restr
d2ef0 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20  ictive on the.  
d2f00 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20  ** unixFile, do 
d2f10 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75  nothing. Don't u
d2f20 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a  se the end_lock:
d2f30 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20   exit path, as. 
d2f40 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75 74   ** unixEnterMut
d2f50 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e  ex() hasn't been
d2f60 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a   called yet..  *
d2f70 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
d2f80 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70  ocktype>=locktyp
d2f90 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45  e ){.    OSTRACE
d2fa0 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73  3("LOCK    %d %s
d2fb0 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c   ok (already hel
d2fc0 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  d)\n", pFile->h,
d2fd0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63  .            loc
d2fe0 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79  ktypeName(lockty
d2ff0 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  pe));.    return
d3000 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
d3010 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
d3020 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75  the locking sequ
d3030 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 2e  ence is correct.
d3040 0a 20 20 2a 2a 20 20 28 31 29 20 57 65 20 6e 65  .  **  (1) We ne
d3050 76 65 72 20 6d 6f 76 65 20 66 72 6f 6d 20 75 6e  ver move from un
d3060 6c 6f 63 6b 65 64 20 74 6f 20 61 6e 79 74 68 69  locked to anythi
d3070 6e 67 20 68 69 67 68 65 72 20 74 68 61 6e 20 73  ng higher than s
d3080 68 61 72 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2a  hared lock..  **
d3090 20 20 28 32 29 20 53 51 4c 69 74 65 20 6e 65 76    (2) SQLite nev
d30a0 65 72 20 65 78 70 6c 69 63 69 74 6c 79 20 72 65  er explicitly re
d30b0 71 75 65 73 74 73 20 61 20 70 65 6e 64 69 67 20  quests a pendig 
d30c0 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28 33 29 20  lock..  **  (3) 
d30d0 41 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 73  A shared lock is
d30e0 20 61 6c 77 61 79 73 20 68 65 6c 64 20 77 68 65   always held whe
d30f0 6e 20 61 20 72 65 73 65 72 76 65 20 6c 6f 63 6b  n a reserve lock
d3100 20 69 73 20 72 65 71 75 65 73 74 65 64 2e 0a 20   is requested.. 
d3110 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46   */.  assert( pF
d3120 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e  ile->locktype!=N
d3130 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79  O_LOCK || lockty
d3140 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
d3150 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63  );.  assert( loc
d3160 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c  ktype!=PENDING_L
d3170 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OCK );.  assert(
d3180 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52   locktype!=RESER
d3190 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c  VED_LOCK || pFil
d31a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  e->locktype==SHA
d31b0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f  RED_LOCK );..  /
d31c0 2a 20 54 68 69 73 20 6d 75 74 65 78 20 69 73 20  * This mutex is 
d31d0 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20 70  needed because p
d31e0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73  File->pLock is s
d31f0 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72  hared across thr
d3200 65 61 64 73 0a 20 20 2a 2f 0a 20 20 75 6e 69 78  eads.  */.  unix
d3210 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20  EnterMutex();.. 
d3220 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
d3230 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64  e current thread
d3240 20 6f 77 6e 73 20 74 68 65 20 70 46 69 6c 65 2e   owns the pFile.
d3250 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61  .  */.  rc = tra
d3260 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 70  nsferOwnership(p
d3270 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 21  File);.  if( rc!
d3280 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
d3290 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
d32a0 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  ();.    return r
d32b0 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f 63 6b 20 3d  c;.  }.  pLock =
d32c0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 0a   pFile->pLock;..
d32d0 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 74 68 72    /* If some thr
d32e0 65 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50  ead using this P
d32f0 49 44 20 68 61 73 20 61 20 6c 6f 63 6b 20 76 69  ID has a lock vi
d3300 61 20 61 20 64 69 66 66 65 72 65 6e 74 20 75 6e  a a different un
d3310 69 78 46 69 6c 65 2a 0a 20 20 2a 2a 20 68 61 6e  ixFile*.  ** han
d3320 64 6c 65 20 74 68 61 74 20 70 72 65 63 6c 75 64  dle that preclud
d3330 65 73 20 74 68 65 20 72 65 71 75 65 73 74 65 64  es the requested
d3340 20 6c 6f 63 6b 2c 20 72 65 74 75 72 6e 20 42 55   lock, return BU
d3350 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  SY..  */.  if( (
d3360 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21  pFile->locktype!
d3370 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65  =pLock->locktype
d3380 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 28   && .          (
d3390 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pLock->locktype>
d33a0 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 7c 7c  =PENDING_LOCK ||
d33b0 20 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44   locktype>SHARED
d33c0 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20  _LOCK)).  ){.   
d33d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
d33e0 59 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  Y;.    goto end_
d33f0 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  lock;.  }..  /* 
d3400 49 66 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  If a SHARED lock
d3410 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 61   is requested, a
d3420 6e 64 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75  nd some thread u
d3430 73 69 6e 67 20 74 68 69 73 20 50 49 44 20 61 6c  sing this PID al
d3440 72 65 61 64 79 0a 20 20 2a 2a 20 68 61 73 20 61  ready.  ** has a
d3450 20 53 48 41 52 45 44 20 6f 72 20 52 45 53 45 52   SHARED or RESER
d3460 56 45 44 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 69  VED lock, then i
d3470 6e 63 72 65 6d 65 6e 74 20 72 65 66 65 72 65 6e  ncrement referen
d3480 63 65 20 63 6f 75 6e 74 73 20 61 6e 64 0a 20 20  ce counts and.  
d3490 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ** return SQLITE
d34a0 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  _OK..  */.  if( 
d34b0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
d34c0 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 20 20 20 20  _LOCK && .      
d34d0 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65  (pLock->locktype
d34e0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c  ==SHARED_LOCK ||
d34f0 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
d3500 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29  ==RESERVED_LOCK)
d3510 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
d3520 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
d3530 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73  _LOCK );.    ass
d3540 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ert( pFile->lock
d3550 74 79 70 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61  type==0 );.    a
d3560 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e  ssert( pLock->cn
d3570 74 3e 30 20 29 3b 0a 20 20 20 20 70 46 69 6c 65  t>0 );.    pFile
d3580 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41  ->locktype = SHA
d3590 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c  RED_LOCK;.    pL
d35a0 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20  ock->cnt++;.    
d35b0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c  pFile->pOpen->nL
d35c0 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20  ock++;.    goto 
d35d0 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 0a  end_lock;.  }...
d35e0 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c    /* A PENDING l
d35f0 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65  ock is needed be
d3600 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 61  fore acquiring a
d3610 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64   SHARED lock and
d3620 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71   before.  ** acq
d3630 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53  uiring an EXCLUS
d3640 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74  IVE lock.  For t
d3650 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20  he SHARED lock, 
d3660 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c  the PENDING will
d3670 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65  .  ** be release
d3680 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 2e 6c  d..  */.  lock.l
d3690 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 6c 6f 63  _len = 1L;.  loc
d36a0 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45  k.l_whence = SEE
d36b0 4b 5f 53 45 54 3b 0a 20 20 69 66 28 20 6c 6f 63  K_SET;.  if( loc
d36c0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
d36d0 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f  CK .      || (lo
d36e0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
d36f0 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d  E_LOCK && pFile-
d3700 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e  >locktype<PENDIN
d3710 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20  G_LOCK).  ){.   
d3720 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 28   lock.l_type = (
d3730 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
d3740 5f 4c 4f 43 4b 3f 46 5f 52 44 4c 43 4b 3a 46 5f  _LOCK?F_RDLCK:F_
d3750 57 52 4c 43 4b 29 3b 0a 20 20 20 20 6c 6f 63 6b  WRLCK);.    lock
d3760 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49  .l_start = PENDI
d3770 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 73 20 3d  NG_BYTE;.    s =
d3780 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
d3790 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
d37a0 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31  ;.    if( s==(-1
d37b0 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72 6e  ) ){.      tErrn
d37c0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
d37d0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
d37e0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
d37f0 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
d3800 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  OERR_LOCK);.    
d3810 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d3820 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
d3830 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
d3840 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20  rno = tErrno;.  
d3850 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f      }.      goto
d3860 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d   end_lock;.    }
d3870 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63  .  }...  /* If c
d3880 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74  ontrol gets to t
d3890 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20  his point, then 
d38a0 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61  actually go ahea
d38b0 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20  d and make.  ** 
d38c0 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
d38d0 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73   calls for the s
d38e0 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20  pecified lock.. 
d38f0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79   */.  if( lockty
d3900 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
d3910 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
d3920 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a  Lock->cnt==0 );.
d3930 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63      assert( pLoc
d3940 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29  k->locktype==0 )
d3950 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65  ;..    /* Now ge
d3960 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20  t the read-lock 
d3970 2a 2f 0a 20 20 20 20 73 20 3d 20 72 61 6e 67 65  */.    s = range
d3980 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 46 5f 52 44  Lock(pFile, F_RD
d3990 4c 43 4b 2c 20 26 74 45 72 72 6e 6f 29 3b 0a 0a  LCK, &tErrno);..
d39a0 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20      /* Drop the 
d39b0 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e  temporary PENDIN
d39c0 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f  G lock */.    lo
d39d0 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e  ck.l_start = PEN
d39e0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c  DING_BYTE;.    l
d39f0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a  ock.l_len = 1L;.
d3a00 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20      lock.l_type 
d3a10 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 69  = F_UNLCK;.    i
d3a20 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e  f( fcntl(pFile->
d3a30 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63  h, F_SETLK, &loc
d3a40 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  k)!=0 ){.      i
d3a50 66 28 20 73 20 21 3d 20 2d 31 20 29 7b 0a 20 20  f( s != -1 ){.  
d3a60 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f        /* This co
d3a70 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74 68 20  uld happen with 
d3a80 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e 74 20  a network mount 
d3a90 2a 2f 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e  */.        tErrn
d3aa0 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a 20 20 20 20  o = errno; .    
d3ab0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d3ac0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d3ad0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d3ae0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
d3af0 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53   .        if( IS
d3b00 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
d3b10 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  ){.          pFi
d3b20 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d3b30 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20  tErrno;.        
d3b40 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  }.        goto e
d3b50 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d  nd_lock;.      }
d3b60 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
d3b70 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ==(-1) ){.      
d3b80 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
d3b90 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
d3ba0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
d3bb0 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20  ERR_LOCK);.     
d3bc0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
d3bd0 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20  OR(rc) ){.      
d3be0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d3bf0 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d3c00 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
d3c10 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
d3c20 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c  ktype = SHARED_L
d3c30 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c 65  OCK;.      pFile
d3c40 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b  ->pOpen->nLock++
d3c50 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63  ;.      pLock->c
d3c60 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  nt = 1;.    }.  
d3c70 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79  }else if( lockty
d3c80 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
d3c90 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 6e 74  CK && pLock->cnt
d3ca0 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20  >1 ){.    /* We 
d3cb0 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61  are trying for a
d3cc0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
d3cd0 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72   but another thr
d3ce0 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 20 20  ead in this.    
d3cf0 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20  ** same process 
d3d00 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67  is still holding
d3d10 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20   a shared lock. 
d3d20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  */.    rc = SQLI
d3d30 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65  TE_BUSY;.  }else
d3d40 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71  {.    /* The req
d3d50 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 20 52  uest was for a R
d3d60 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55  ESERVED or EXCLU
d3d70 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69  SIVE lock.  It i
d3d80 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 64  s.    ** assumed
d3d90 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61   that there is a
d3da0 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 74   SHARED or great
d3db0 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66  er lock on the f
d3dc0 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61  ile.    ** alrea
d3dd0 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  dy..    */.    a
d3de0 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d  ssert( 0!=pFile-
d3df0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20  >locktype );.   
d3e00 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46   lock.l_type = F
d3e10 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77 69 74  _WRLCK;.    swit
d3e20 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  ch( locktype ){.
d3e30 20 20 20 20 20 20 63 61 73 65 20 52 45 53 45 52        case RESER
d3e40 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20  VED_LOCK:.      
d3e50 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
d3e60 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a   RESERVED_BYTE;.
d3e70 20 20 20 20 20 20 20 20 73 20 3d 20 66 63 6e 74          s = fcnt
d3e80 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45  l(pFile->h, F_SE
d3e90 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20  TLK, &lock);.   
d3ea0 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72       tErrno = er
d3eb0 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 62 72 65  rno;.        bre
d3ec0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 45  ak;.      case E
d3ed0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20  XCLUSIVE_LOCK:. 
d3ee0 20 20 20 20 20 20 20 73 20 3d 20 72 61 6e 67 65         s = range
d3ef0 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 46 5f 57 52  Lock(pFile, F_WR
d3f00 4c 43 4b 2c 20 26 74 45 72 72 6e 6f 29 3b 0a 20  LCK, &tErrno);. 
d3f10 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
d3f20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
d3f30 20 20 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a       assert(0);.
d3f40 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 3d      }.    if( s=
d3f50 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 72  =(-1) ){.      r
d3f60 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
d3f70 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
d3f80 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
d3f90 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  RR_LOCK);.      
d3fa0 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
d3fb0 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20  R(rc) ){.       
d3fc0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d3fd0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d3fe0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
d3ff0 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
d4000 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 74 68 65  .  /* Set up the
d4010 20 74 72 61 6e 73 61 63 74 69 6f 6e 2d 63 6f 75   transaction-cou
d4020 6e 74 65 72 20 63 68 61 6e 67 65 20 63 68 65 63  nter change chec
d4030 6b 69 6e 67 20 66 6c 61 67 73 20 77 68 65 6e 0a  king flags when.
d4040 20 20 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 69    ** transitioni
d4050 6e 67 20 66 72 6f 6d 20 61 20 53 48 41 52 45 44  ng from a SHARED
d4060 20 74 6f 20 61 20 52 45 53 45 52 56 45 44 20 6c   to a RESERVED l
d4070 6f 63 6b 2e 20 20 54 68 65 20 63 68 61 6e 67 65  ock.  The change
d4080 0a 20 20 2a 2a 20 66 72 6f 6d 20 53 48 41 52 45  .  ** from SHARE
d4090 44 20 74 6f 20 52 45 53 45 52 56 45 44 20 6d 61  D to RESERVED ma
d40a0 72 6b 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rks the beginnin
d40b0 67 20 6f 66 20 61 20 6e 6f 72 6d 61 6c 0a 20 20  g of a normal.  
d40c0 2a 2a 20 77 72 69 74 65 20 6f 70 65 72 61 74 69  ** write operati
d40d0 6f 6e 20 28 6e 6f 74 20 61 20 68 6f 74 20 6a 6f  on (not a hot jo
d40e0 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e  urnal rollback).
d40f0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  .  */.  if( rc==
d4100 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 26 26 20  SQLITE_OK.   && 
d4110 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c  pFile->locktype<
d4120 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 20 20 20  =SHARED_LOCK.   
d4130 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53  && locktype==RES
d4140 45 52 56 45 44 5f 4c 4f 43 4b 0a 20 20 29 7b 0a  ERVED_LOCK.  ){.
d4150 20 20 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73      pFile->trans
d4160 43 6e 74 72 43 68 6e 67 20 3d 20 30 3b 0a 20 20  CntrChng = 0;.  
d4170 20 20 70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74    pFile->dbUpdat
d4180 65 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65  e = 0;.    pFile
d4190 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20  ->inNormalWrite 
d41a0 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  = 1;.  }.#endif.
d41b0 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
d41c0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69  TE_OK ){.    pFi
d41d0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
d41e0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 70 4c 6f  ocktype;.    pLo
d41f0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  ck->locktype = l
d4200 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65  ocktype;.  }else
d4210 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45   if( locktype==E
d4220 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b  XCLUSIVE_LOCK ){
d4230 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
d4240 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c  type = PENDING_L
d4250 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  OCK;.    pLock->
d4260 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49  locktype = PENDI
d4270 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 0a 65 6e  NG_LOCK;.  }..en
d4280 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65  d_lock:.  unixLe
d4290 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53  aveMutex();.  OS
d42a0 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20  TRACE4("LOCK    
d42b0 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69  %d %s %s\n", pFi
d42c0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e  le->h, locktypeN
d42d0 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a  ame(locktype), .
d42e0 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45        rc==SQLITE
d42f0 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61  _OK ? "ok" : "fa
d4300 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e  iled");.  return
d4310 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c   rc;.}../*.** Cl
d4320 6f 73 65 20 61 6c 6c 20 66 69 6c 65 20 64 65 73  ose all file des
d4330 63 72 69 70 74 6f 72 73 20 61 63 63 75 6d 75 61  criptors accumua
d4340 74 65 64 20 69 6e 20 74 68 65 20 75 6e 69 78 4f  ted in the unixO
d4350 70 65 6e 43 6e 74 2d 3e 70 55 6e 75 73 65 64 20  penCnt->pUnused 
d4360 6c 69 73 74 2e 0a 2a 2a 20 49 66 20 61 6c 6c 20  list..** If all 
d4370 73 75 63 68 20 66 69 6c 65 20 64 65 73 63 72 69  such file descri
d4380 70 74 6f 72 73 20 61 72 65 20 63 6c 6f 73 65 64  ptors are closed
d4390 20 77 69 74 68 6f 75 74 20 65 72 72 6f 72 2c 20   without error, 
d43a0 74 68 65 20 6c 69 73 74 20 69 73 0a 2a 2a 20 63  the list is.** c
d43b0 6c 65 61 72 65 64 20 61 6e 64 20 53 51 4c 49 54  leared and SQLIT
d43c0 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a  E_OK returned..*
d43d0 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  *.** Otherwise, 
d43e0 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
d43f0 72 73 2c 20 74 68 65 6e 20 73 75 63 63 65 73 73  rs, then success
d4400 66 75 6c 6c 79 20 63 6c 6f 73 65 64 20 66 69 6c  fully closed fil
d4410 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20  e descriptor.** 
d4420 65 6e 74 72 69 65 73 20 61 72 65 20 72 65 6d 6f  entries are remo
d4430 76 65 64 20 66 72 6f 6d 20 74 68 65 20 6c 69 73  ved from the lis
d4440 74 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f  t, and SQLITE_IO
d4450 45 52 52 5f 43 4c 4f 53 45 20 72 65 74 75 72 6e  ERR_CLOSE return
d4460 65 64 2e 20 0a 2a 2a 20 6e 6f 74 20 64 65 6c 65  ed. .** not dele
d4470 74 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 49  ted and SQLITE_I
d4480 4f 45 52 52 5f 43 4c 4f 53 45 20 72 65 74 75 72  OERR_CLOSE retur
d4490 6e 65 64 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20  ned..*/ .static 
d44a0 69 6e 74 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67  int closePending
d44b0 46 64 73 28 75 6e 69 78 46 69 6c 65 20 2a 70 46  Fds(unixFile *pF
d44c0 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ile){.  int rc =
d44d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 74   SQLITE_OK;.  st
d44e0 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74  ruct unixOpenCnt
d44f0 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d   *pOpen = pFile-
d4500 3e 70 4f 70 65 6e 3b 0a 20 20 55 6e 69 78 55 6e  >pOpen;.  UnixUn
d4510 75 73 65 64 46 64 20 2a 70 45 72 72 6f 72 20 3d  usedFd *pError =
d4520 20 30 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64   0;.  UnixUnused
d4530 46 64 20 2a 70 3b 0a 20 20 55 6e 69 78 55 6e 75  Fd *p;.  UnixUnu
d4540 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 0a 20 20  sedFd *pNext;.  
d4550 66 6f 72 28 70 3d 70 4f 70 65 6e 2d 3e 70 55 6e  for(p=pOpen->pUn
d4560 75 73 65 64 3b 20 70 3b 20 70 3d 70 4e 65 78 74  used; p; p=pNext
d4570 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70  ){.    pNext = p
d4580 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 28  ->pNext;.    if(
d4590 20 63 6c 6f 73 65 28 70 2d 3e 66 64 29 20 29 7b   close(p->fd) ){
d45a0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
d45b0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
d45c0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
d45d0 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a  TE_IOERR_CLOSE;.
d45e0 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d        p->pNext =
d45f0 20 70 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 70   pError;.      p
d4600 45 72 72 6f 72 20 3d 20 70 3b 0a 20 20 20 20 7d  Error = p;.    }
d4610 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
d4620 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20  te3_free(p);.   
d4630 20 7d 0a 20 20 7d 0a 20 20 70 4f 70 65 6e 2d 3e   }.  }.  pOpen->
d4640 70 55 6e 75 73 65 64 20 3d 20 70 45 72 72 6f 72  pUnused = pError
d4650 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
d4660 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  ../*.** Add the 
d4670 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
d4680 75 73 65 64 20 62 79 20 66 69 6c 65 20 68 61 6e  used by file han
d4690 64 6c 65 20 70 46 69 6c 65 20 74 6f 20 74 68 65  dle pFile to the
d46a0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a   corresponding.*
d46b0 2a 20 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a  * pUnused list..
d46c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
d46d0 65 74 50 65 6e 64 69 6e 67 46 64 28 75 6e 69 78  etPendingFd(unix
d46e0 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20  File *pFile){.  
d46f0 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43  struct unixOpenC
d4700 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c  nt *pOpen = pFil
d4710 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 55 6e 69 78  e->pOpen;.  Unix
d4720 55 6e 75 73 65 64 46 64 20 2a 70 20 3d 20 70 46  UnusedFd *p = pF
d4730 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 20  ile->pUnused;.  
d4740 70 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e  p->pNext = pOpen
d4750 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 20 70 4f 70  ->pUnused;.  pOp
d4760 65 6e 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 3b  en->pUnused = p;
d4770 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31  .  pFile->h = -1
d4780 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73  ;.  pFile->pUnus
d4790 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ed = 0;.}../*.**
d47a0 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   Lower the locki
d47b0 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65  ng level on file
d47c0 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c   descriptor pFil
d47d0 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20  e to locktype.  
d47e0 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74  locktype.** must
d47f0 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
d4800 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43  CK or SHARED_LOC
d4810 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  K..**.** If the 
d4820 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66  locking level of
d4830 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
d4840 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  ptor is already 
d4850 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74  at or below.** t
d4860 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
d4870 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73  king level, this
d4880 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
d4890 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -op..*/.static i
d48a0 6e 74 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 73 71  nt unixUnlock(sq
d48b0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
d48c0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20  int locktype){. 
d48d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d48e0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d48f0 3b 20 2f 2a 20 54 68 65 20 6f 70 65 6e 20 66 69  ; /* The open fi
d4900 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75  le */.  struct u
d4910 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f  nixLockInfo *pLo
d4920 63 6b 3b 20 20 20 20 20 20 2f 2a 20 53 74 72 75  ck;      /* Stru
d4930 63 74 75 72 65 20 64 65 73 63 72 69 62 69 6e 67  cture describing
d4940 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b 20 73 74   current lock st
d4950 61 74 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ate */.  struct 
d4960 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20  flock lock;     
d4970 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66            /* Inf
d4980 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 20  ormation passed 
d4990 69 6e 74 6f 20 66 63 6e 74 6c 28 29 20 2a 2f 0a  into fcntl() */.
d49a0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
d49b0 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
d49c0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
d49d0 65 20 66 72 6f 6d 20 74 68 69 73 20 69 6e 74 65  e from this inte
d49e0 72 66 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 68  rface */.  int h
d49f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
d4a00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
d4a10 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69  he underlying fi
d4a20 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f  le descriptor */
d4a30 0a 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 20 20  .  int tErrno;  
d4a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d4a50 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64      /* Error cod
d4a60 65 20 66 72 6f 6d 20 73 79 73 74 65 6d 20 63 61  e from system ca
d4a70 6c 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 0a 20 20  ll errors */..  
d4a80 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
d4a90 0a 20 20 4f 53 54 52 41 43 45 37 28 22 55 4e 4c  .  OSTRACE7("UNL
d4aa0 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25  OCK  %d %d was %
d4ab0 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25 64 5c  d(%d,%d) pid=%d\
d4ac0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
d4ad0 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 70 46  cktype,.      pF
d4ae0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70  ile->locktype, p
d4af0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63  File->pLock->loc
d4b00 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c  ktype, pFile->pL
d4b10 6f 63 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 69 64  ock->cnt, getpid
d4b20 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ());..  assert( 
d4b30 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44  locktype<=SHARED
d4b40 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70  _LOCK );.  if( p
d4b50 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d  File->locktype<=
d4b60 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20  locktype ){.    
d4b70 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d4b80 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43  ;.  }.  if( CHEC
d4b90 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 6c 65  K_THREADID(pFile
d4ba0 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
d4bb0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
d4bc0 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75   }.  unixEnterMu
d4bd0 74 65 78 28 29 3b 0a 20 20 68 20 3d 20 70 46 69  tex();.  h = pFi
d4be0 6c 65 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d  le->h;.  pLock =
d4bf0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20   pFile->pLock;. 
d4c00 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
d4c10 63 6e 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  cnt!=0 );.  if( 
d4c20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
d4c30 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
d4c40 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
d4c50 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c  ->locktype==pFil
d4c60 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
d4c70 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72     SimulateIOErr
d4c80 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20  orBenign(1);.   
d4c90 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d4ca0 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53  ( h=(-1) ).    S
d4cb0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
d4cc0 6e 69 67 6e 28 30 29 3b 0a 0a 23 69 66 6e 64 65  nign(0);..#ifnde
d4cd0 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20  f NDEBUG.    /* 
d4ce0 57 68 65 6e 20 72 65 64 75 63 69 6e 67 20 61 20  When reducing a 
d4cf0 6c 6f 63 6b 20 73 75 63 68 20 74 68 61 74 20 6f  lock such that o
d4d00 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 63  ther processes c
d4d10 61 6e 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20  an start.    ** 
d4d20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61  reading the data
d4d30 62 61 73 65 20 66 69 6c 65 20 61 67 61 69 6e 2c  base file again,
d4d40 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
d4d50 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73  the.    ** trans
d4d60 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77  action counter w
d4d70 61 73 20 75 70 64 61 74 65 64 20 69 66 20 61 6e  as updated if an
d4d80 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  y part of the da
d4d90 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 69  tabase.    ** fi
d4da0 6c 65 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20  le changed.  If 
d4db0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
d4dc0 63 6f 75 6e 74 65 72 20 69 73 20 6e 6f 74 20 75  counter is not u
d4dd0 70 64 61 74 65 64 2c 0a 20 20 20 20 2a 2a 20 6f  pdated,.    ** o
d4de0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  ther connections
d4df0 20 74 6f 20 74 68 65 20 73 61 6d 65 20 66 69 6c   to the same fil
d4e00 65 20 6d 69 67 68 74 20 6e 6f 74 20 72 65 61 6c  e might not real
d4e10 69 7a 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  ize that.    ** 
d4e20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61  the file has cha
d4e30 6e 67 65 64 20 61 6e 64 20 68 65 6e 63 65 20 6d  nged and hence m
d4e40 69 67 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 74 6f  ight not know to
d4e50 20 66 6c 75 73 68 20 74 68 65 69 72 0a 20 20 20   flush their.   
d4e60 20 2a 2a 20 63 61 63 68 65 2e 20 20 54 68 65 20   ** cache.  The 
d4e70 75 73 65 20 6f 66 20 61 20 73 74 61 6c 65 20 63  use of a stale c
d4e80 61 63 68 65 20 63 61 6e 20 6c 65 61 64 20 74 6f  ache can lead to
d4e90 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
d4ea0 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
d4eb0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
d4ec0 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3d 3d 30  inNormalWrite==0
d4ed0 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69  .         || pFi
d4ee0 6c 65 2d 3e 64 62 55 70 64 61 74 65 3d 3d 30 0a  le->dbUpdate==0.
d4ef0 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c           || pFil
d4f00 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67  e->transCntrChng
d4f10 3d 3d 31 20 29 3b 0a 20 20 20 20 70 46 69 6c 65  ==1 );.    pFile
d4f20 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20  ->inNormalWrite 
d4f30 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 20 20  = 0;.#endif...  
d4f40 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
d4f50 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
d4f60 20 20 20 20 20 69 66 28 20 72 61 6e 67 65 4c 6f       if( rangeLo
d4f70 63 6b 28 70 46 69 6c 65 2c 20 46 5f 52 44 4c 43  ck(pFile, F_RDLC
d4f80 4b 2c 20 26 74 45 72 72 6e 6f 29 3d 3d 28 2d 31  K, &tErrno)==(-1
d4f90 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  ) ){.        rc 
d4fa0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
d4fb0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
d4fc0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
d4fd0 5f 52 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  _RDLOCK);.      
d4fe0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d4ff0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
d5000 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d5010 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
d5020 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
d5030 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63    goto end_unloc
d5040 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
d5050 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65  .    lock.l_type
d5060 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20   = F_UNLCK;.    
d5070 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  lock.l_whence = 
d5080 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f  SEEK_SET;.    lo
d5090 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e  ck.l_start = PEN
d50a0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c  DING_BYTE;.    l
d50b0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20  ock.l_len = 2L; 
d50c0 20 61 73 73 65 72 74 28 20 50 45 4e 44 49 4e 47   assert( PENDING
d50d0 5f 42 59 54 45 2b 31 3d 3d 52 45 53 45 52 56 45  _BYTE+1==RESERVE
d50e0 44 5f 42 59 54 45 20 29 3b 0a 20 20 20 20 69 66  D_BYTE );.    if
d50f0 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54  ( fcntl(h, F_SET
d5100 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29  LK, &lock)!=(-1)
d5110 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d   ){.      pLock-
d5120 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  >locktype = SHAR
d5130 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c  ED_LOCK;.    }el
d5140 73 65 7b 0a 20 20 20 20 20 20 74 45 72 72 6e 6f  se{.      tErrno
d5150 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d5160 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
d5170 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
d5180 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
d5190 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20  ERR_UNLOCK);.   
d51a0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d51b0 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
d51c0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d51d0 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
d51e0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74       }.      got
d51f0 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20  o end_unlock;.  
d5200 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f    }.  }.  if( lo
d5210 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20  cktype==NO_LOCK 
d5220 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75 6e  ){.    struct un
d5230 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e  ixOpenCnt *pOpen
d5240 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d  ;..    /* Decrem
d5250 65 6e 74 20 74 68 65 20 73 68 61 72 65 64 20 6c  ent the shared l
d5260 6f 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20 52 65  ock counter.  Re
d5270 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 20 75  lease the lock u
d5280 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f  sing an.    ** O
d5290 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e  S call only when
d52a0 20 61 6c 6c 20 74 68 72 65 61 64 73 20 69 6e 20   all threads in 
d52b0 74 68 69 73 20 73 61 6d 65 20 70 72 6f 63 65 73  this same proces
d52c0 73 20 68 61 76 65 20 72 65 6c 65 61 73 65 64 0a  s have released.
d52d0 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e      ** the lock.
d52e0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c 6f 63  .    */.    pLoc
d52f0 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20 69 66  k->cnt--;.    if
d5300 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20  ( pLock->cnt==0 
d5310 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  ){.      lock.l_
d5320 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a  type = F_UNLCK;.
d5330 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65        lock.l_whe
d5340 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a  nce = SEEK_SET;.
d5350 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61        lock.l_sta
d5360 72 74 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  rt = lock.l_len 
d5370 3d 20 30 4c 3b 0a 20 20 20 20 20 20 53 69 6d 75  = 0L;.      Simu
d5380 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
d5390 6e 28 31 29 3b 0a 20 20 20 20 20 20 53 69 6d 75  n(1);.      Simu
d53a0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28  lateIOError( h=(
d53b0 2d 31 29 20 29 0a 20 20 20 20 20 20 53 69 6d 75  -1) ).      Simu
d53c0 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
d53d0 6e 28 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  n(0);.      if( 
d53e0 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b  fcntl(h, F_SETLK
d53f0 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29  , &lock)!=(-1) )
d5400 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d  {.        pLock-
d5410 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c  >locktype = NO_L
d5420 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  OCK;.      }else
d5430 7b 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f  {.        tErrno
d5440 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d5450 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
d5460 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d5470 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d5480 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20  IOERR_UNLOCK);. 
d5490 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f         if( IS_LO
d54a0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
d54b0 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d            pFile-
d54c0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d54d0 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  rno;.        }. 
d54e0 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f         pLock->lo
d54f0 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b  cktype = NO_LOCK
d5500 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  ;.        pFile-
d5510 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c  >locktype = NO_L
d5520 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  OCK;.      }.   
d5530 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65   }..    /* Decre
d5540 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 20 6f  ment the count o
d5550 66 20 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74 20  f locks against 
d5560 74 68 69 73 20 73 61 6d 65 20 66 69 6c 65 2e 20  this same file. 
d5570 20 57 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a   When the.    **
d5580 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a   count reaches z
d5590 65 72 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f  ero, close any o
d55a0 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 72 69  ther file descri
d55b0 70 74 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f 73  ptors whose clos
d55c0 65 0a 20 20 20 20 2a 2a 20 77 61 73 20 64 65 66  e.    ** was def
d55d0 65 72 72 65 64 20 62 65 63 61 75 73 65 20 6f 66  erred because of
d55e0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63   outstanding loc
d55f0 6b 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  ks..    */.    p
d5600 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f  Open = pFile->pO
d5610 70 65 6e 3b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e  pen;.    pOpen->
d5620 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61 73 73  nLock--;.    ass
d5630 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63  ert( pOpen->nLoc
d5640 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  k>=0 );.    if( 
d5650 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20  pOpen->nLock==0 
d5660 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32  ){.      int rc2
d5670 20 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46   = closePendingF
d5680 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20  ds(pFile);.     
d5690 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
d56a0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
d56b0 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a   = rc2;.      }.
d56c0 20 20 20 20 7d 0a 20 20 7d 0a 09 0a 65 6e 64 5f      }.  }...end_
d56d0 75 6e 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65  unlock:.  unixLe
d56e0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66  aveMutex();.  if
d56f0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
d5700 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ) pFile->locktyp
d5710 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
d5720 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
d5730 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
d5740 6e 20 70 65 72 66 6f 72 6d 73 20 74 68 65 20 70  n performs the p
d5750 61 72 74 73 20 6f 66 20 74 68 65 20 22 63 6c 6f  arts of the "clo
d5760 73 65 20 66 69 6c 65 22 20 6f 70 65 72 61 74 69  se file" operati
d5770 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f  on .** common to
d5780 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 73 63 68   all locking sch
d5790 65 6d 65 73 2e 20 49 74 20 63 6c 6f 73 65 73 20  emes. It closes 
d57a0 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 6e  the directory an
d57b0 64 20 66 69 6c 65 0a 2a 2a 20 68 61 6e 64 6c 65  d file.** handle
d57c0 73 2c 20 69 66 20 74 68 65 79 20 61 72 65 20 76  s, if they are v
d57d0 61 6c 69 64 2c 20 61 6e 64 20 73 65 74 73 20 61  alid, and sets a
d57e0 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  ll fields of the
d57f0 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72   unixFile.** str
d5800 75 63 74 75 72 65 20 74 6f 20 30 2e 0a 2a 2a 0a  ucture to 0..**.
d5810 2a 2a 20 49 74 20 69 73 20 2a 6e 6f 74 2a 20 6e  ** It is *not* n
d5820 65 63 65 73 73 61 72 79 20 74 6f 20 68 6f 6c 64  ecessary to hold
d5830 20 74 68 65 20 6d 75 74 65 78 20 77 68 65 6e 20   the mutex when 
d5840 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
d5850 63 61 6c 6c 65 64 2c 0a 2a 2a 20 65 76 65 6e 20  called,.** even 
d5860 6f 6e 20 56 78 57 6f 72 6b 73 2e 20 20 41 20 6d  on VxWorks.  A m
d5870 75 74 65 78 20 77 69 6c 6c 20 62 65 20 61 63 71  utex will be acq
d5880 75 69 72 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73  uired on VxWorks
d5890 20 62 79 20 74 68 65 0a 2a 2a 20 76 78 77 6f 72   by the.** vxwor
d58a0 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28  ksReleaseFileId(
d58b0 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74  ) routine..*/.st
d58c0 61 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 55 6e  atic int closeUn
d58d0 69 78 46 69 6c 65 28 73 71 6c 69 74 65 33 5f 66  ixFile(sqlite3_f
d58e0 69 6c 65 20 2a 69 64 29 7b 0a 20 20 75 6e 69 78  ile *id){.  unix
d58f0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d5900 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69  nixFile*)id;.  i
d5910 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20  f( pFile ){.    
d5920 69 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64  if( pFile->dirfd
d5930 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  >=0 ){.      int
d5940 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69   err = close(pFi
d5950 6c 65 2d 3e 64 69 72 66 64 29 3b 0a 20 20 20 20  le->dirfd);.    
d5960 20 20 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20    if( err ){.   
d5970 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d5980 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d5990 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
d59a0 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43  LITE_IOERR_DIR_C
d59b0 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73  LOSE;.      }els
d59c0 65 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  e{.        pFile
d59d0 2d 3e 64 69 72 66 64 3d 2d 31 3b 0a 20 20 20 20  ->dirfd=-1;.    
d59e0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
d59f0 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b  ( pFile->h>=0 ){
d5a00 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d  .      int err =
d5a10 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29   close(pFile->h)
d5a20 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 20  ;.      if( err 
d5a30 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  ){.        pFile
d5a40 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
d5a50 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74  rno;.        ret
d5a60 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
d5a70 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 0a  _CLOSE;.      }.
d5a80 20 20 20 20 7d 0a 23 69 66 20 4f 53 5f 56 58 57      }.#if OS_VXW
d5a90 4f 52 4b 53 0a 20 20 20 20 69 66 28 20 70 46 69  ORKS.    if( pFi
d5aa0 6c 65 2d 3e 70 49 64 20 29 7b 0a 20 20 20 20 20  le->pId ){.     
d5ab0 20 69 66 28 20 70 46 69 6c 65 2d 3e 69 73 44 65   if( pFile->isDe
d5ac0 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  lete ){.        
d5ad0 75 6e 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e 70 49  unlink(pFile->pI
d5ae0 64 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d  d->zCanonicalNam
d5af0 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
d5b00 20 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65    vxworksRelease
d5b10 46 69 6c 65 49 64 28 70 46 69 6c 65 2d 3e 70 49  FileId(pFile->pI
d5b20 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  d);.      pFile-
d5b30 3e 70 49 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  >pId = 0;.    }.
d5b40 23 65 6e 64 69 66 0a 20 20 20 20 4f 53 54 52 41  #endif.    OSTRA
d5b50 43 45 32 28 22 43 4c 4f 53 45 20 20 20 25 2d 33  CE2("CLOSE   %-3
d5b60 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b  d\n", pFile->h);
d5b70 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72  .    OpenCounter
d5b80 28 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (-1);.    sqlite
d5b90 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 70 55  3_free(pFile->pU
d5ba0 6e 75 73 65 64 29 3b 0a 20 20 20 20 6d 65 6d 73  nused);.    mems
d5bb0 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a  et(pFile, 0, siz
d5bc0 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a  eof(unixFile));.
d5bd0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
d5be0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
d5bf0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a   Close a file..*
d5c00 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
d5c10 78 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  xClose(sqlite3_f
d5c20 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20  ile *id){.  int 
d5c30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d5c40 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20    if( id ){.    
d5c50 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d5c60 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64  = (unixFile *)id
d5c70 3b 0a 20 20 20 20 75 6e 69 78 55 6e 6c 6f 63 6b  ;.    unixUnlock
d5c80 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  (id, NO_LOCK);. 
d5c90 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65     unixEnterMute
d5ca0 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69  x();.    if( pFi
d5cb0 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69  le->pOpen && pFi
d5cc0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b  le->pOpen->nLock
d5cd0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
d5ce0 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61  there are outsta
d5cf0 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20  nding locks, do 
d5d00 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f  not actually clo
d5d10 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74  se the file just
d5d20 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65  .      ** yet be
d5d30 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64  cause that would
d5d40 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63   clear those loc
d5d50 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64  ks.  Instead, ad
d5d60 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20  d the file.     
d5d70 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74   ** descriptor t
d5d80 6f 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64  o pOpen->pUnused
d5d90 20 6c 69 73 74 2e 20 20 49 74 20 77 69 6c 6c 20   list.  It will 
d5da0 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  be automatically
d5db0 20 63 6c 6f 73 65 64 20 0a 20 20 20 20 20 20 2a   closed .      *
d5dc0 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20  * when the last 
d5dd0 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e  lock is cleared.
d5de0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
d5df0 73 65 74 50 65 6e 64 69 6e 67 46 64 28 70 46 69  setPendingFd(pFi
d5e00 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  le);.    }.    r
d5e10 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70  eleaseLockInfo(p
d5e20 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20  File->pLock);.  
d5e30 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74    releaseOpenCnt
d5e40 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a  (pFile->pOpen);.
d5e50 20 20 20 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e      rc = closeUn
d5e60 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 20  ixFile(id);.    
d5e70 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
d5e80 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
d5e90 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  c;.}../*********
d5ea0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65  ***** End of the
d5eb0 20 70 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20   posix advisory 
d5ec0 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lock implementat
d5ed0 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ion ************
d5ee0 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
d5ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5f30 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
d5f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5f80 2a 2a 2a 2a 2a 2a 0a 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 20 4e 6f 2d 6f 70 20 4c 6f 63 6b  ***** No-op Lock
d5fb0 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ing ************
d5fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5fd0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 66 20  ******.**.** Of 
d5fe0 74 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b  the various lock
d5ff0 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ing implementati
d6000 6f 6e 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74  ons available, t
d6010 68 69 73 20 69 73 20 62 79 20 66 61 72 20 74 68  his is by far th
d6020 65 0a 2a 2a 20 73 69 6d 70 6c 65 73 74 3a 20 20  e.** simplest:  
d6030 6c 6f 63 6b 69 6e 67 20 69 73 20 69 67 6e 6f 72  locking is ignor
d6040 65 64 2e 20 20 4e 6f 20 61 74 74 65 6d 70 74 20  ed.  No attempt 
d6050 69 73 20 6d 61 64 65 20 74 6f 20 6c 6f 63 6b 20  is made to lock 
d6060 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
d6070 66 69 6c 65 20 66 6f 72 20 72 65 61 64 69 6e 67  file for reading
d6080 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a   or writing..**.
d6090 2a 2a 20 54 68 69 73 20 6c 6f 63 6b 69 6e 67 20  ** This locking 
d60a0 6d 6f 64 65 20 69 73 20 61 70 70 72 6f 70 72 69  mode is appropri
d60b0 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 72  ate for use on r
d60c0 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73  ead-only databas
d60d0 65 73 0a 2a 2a 20 28 65 78 3a 20 64 61 74 61 62  es.** (ex: datab
d60e0 61 73 65 73 20 74 68 61 74 20 61 72 65 20 62 75  ases that are bu
d60f0 72 6e 65 64 20 69 6e 74 6f 20 43 44 2d 52 4f 4d  rned into CD-ROM
d6100 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 29 20  , for example.) 
d6110 20 49 74 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20   It can.** also 
d6120 62 65 20 75 73 65 64 20 69 66 20 74 68 65 20 61  be used if the a
d6130 70 70 6c 69 63 61 74 69 6f 6e 20 65 6d 70 6c 6f  pplication emplo
d6140 79 73 20 73 6f 6d 65 20 65 78 74 65 72 6e 61 6c  ys some external
d6150 20 6d 65 63 68 61 6e 69 73 6d 20 74 6f 0a 2a 2a   mechanism to.**
d6160 20 70 72 65 76 65 6e 74 20 73 69 6d 75 6c 74 61   prevent simulta
d6170 6e 65 6f 75 73 20 61 63 63 65 73 73 20 6f 66 20  neous access of 
d6180 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
d6190 65 20 62 79 20 74 77 6f 20 6f 72 20 6d 6f 72 65  e by two or more
d61a0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
d61b0 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 74  nections.  But t
d61c0 68 65 72 65 20 69 73 20 61 20 73 65 72 69 6f 75  here is a seriou
d61d0 73 20 72 69 73 6b 20 6f 66 20 64 61 74 61 62 61  s risk of databa
d61e0 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e  se.** corruption
d61f0 20 69 66 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67   if this locking
d6200 20 6d 6f 64 65 20 69 73 20 75 73 65 64 20 69 6e   mode is used in
d6210 20 73 69 74 75 61 74 69 6f 6e 73 20 77 68 65 72   situations wher
d6220 65 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 64 61  e multiple.** da
d6230 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
d6240 6e 73 20 61 72 65 20 61 63 63 65 73 73 69 6e 67  ns are accessing
d6250 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
d6260 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 73  se file at the s
d6270 61 6d 65 0a 2a 2a 20 74 69 6d 65 20 61 6e 64 20  ame.** time and 
d6280 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  one or more of t
d6290 68 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  hose connections
d62a0 20 61 72 65 20 77 72 69 74 69 6e 67 2e 0a 2a 2f   are writing..*/
d62b0 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c  ..static int nol
d62c0 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64  ockCheckReserved
d62d0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
d62e0 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20  e *NotUsed, int 
d62f0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 55 4e 55  *pResOut){.  UNU
d6300 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
d6310 74 55 73 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f  tUsed);.  *pResO
d6320 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  ut = 0;.  return
d6330 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74   SQLITE_OK;.}.st
d6340 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 4c  atic int nolockL
d6350 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
d6360 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e   *NotUsed, int N
d6370 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53  otUsed2){.  UNUS
d6380 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
d6390 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
d63a0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
d63b0 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69  E_OK;.}.static i
d63c0 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 28  nt nolockUnlock(
d63d0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f  sqlite3_file *No
d63e0 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73  tUsed, int NotUs
d63f0 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  ed2){.  UNUSED_P
d6400 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
d6410 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
d6420 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d6430 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
d6440 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74   the file..*/.st
d6450 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43  atic int nolockC
d6460 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
d6470 65 20 2a 69 64 29 20 7b 0a 20 20 72 65 74 75 72  e *id) {.  retur
d6480 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28  n closeUnixFile(
d6490 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  id);.}../*******
d64a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
d64b0 20 6f 66 20 74 68 65 20 6e 6f 2d 6f 70 20 6c 6f   of the no-op lo
d64c0 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ck implementatio
d64d0 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n **************
d64e0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
d64f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6530 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
d6540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6580 2a 2a 2a 2a 2a 2a 2a 2a 0a 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 20 42 65 67 69 6e 20 64 6f 74 2d 66 69 6c  ** Begin dot-fil
d65b0 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a  e Locking ******
d65c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d65d0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
d65e0 68 65 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69  he dotfile locki
d65f0 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ng implementatio
d6600 6e 20 75 73 65 73 20 74 68 65 20 65 78 69 73 74  n uses the exist
d6610 61 6e 63 65 20 6f 66 20 73 65 70 61 72 61 74 65  ance of separate
d6620 20 6c 6f 63 6b 0a 2a 2a 20 66 69 6c 65 73 20 69   lock.** files i
d6630 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 72  n order to contr
d6640 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  ol access to the
d6650 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73   database.  This
d6660 20 77 6f 72 6b 73 20 6f 6e 20 6a 75 73 74 0a 2a   works on just.*
d6670 2a 20 61 62 6f 75 74 20 65 76 65 72 79 20 66 69  * about every fi
d6680 6c 65 73 79 73 74 65 6d 20 69 6d 61 67 69 6e 61  lesystem imagina
d6690 62 6c 65 2e 20 20 42 75 74 20 74 68 65 72 65 20  ble.  But there 
d66a0 61 72 65 20 73 65 72 69 6f 75 73 20 64 6f 77 6e  are serious down
d66b0 73 69 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  sides:.**.**    
d66c0 28 31 29 20 20 54 68 65 72 65 20 69 73 20 7a 65  (1)  There is ze
d66d0 72 6f 20 63 6f 6e 63 75 72 72 65 6e 63 79 2e 20  ro concurrency. 
d66e0 20 41 20 73 69 6e 67 6c 65 20 72 65 61 64 65 72   A single reader
d66f0 20 62 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74 68 65   blocks all othe
d6700 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e  r.**         con
d6710 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 20 72 65  nections from re
d6720 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67  ading or writing
d6730 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a   the database..*
d6740 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 41 6e 20  *.**    (2)  An 
d6750 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 72 61 73  application cras
d6760 68 20 6f 72 20 70 6f 77 65 72 20 6c 6f 73 73 20  h or power loss 
d6770 63 61 6e 20 6c 65 61 76 65 20 73 74 61 6c 65 20  can leave stale 
d6780 6c 6f 63 6b 20 66 69 6c 65 73 0a 2a 2a 20 20 20  lock files.**   
d6790 20 20 20 20 20 20 73 69 74 74 69 6e 67 20 61 72        sitting ar
d67a0 6f 75 6e 64 20 74 68 61 74 20 6e 65 65 64 20 74  ound that need t
d67b0 6f 20 62 65 20 63 6c 65 61 72 65 64 20 6d 61 6e  o be cleared man
d67c0 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76  ually..**.** Nev
d67d0 65 72 74 68 65 6c 65 73 73 2c 20 61 20 64 6f 74  ertheless, a dot
d67e0 6c 6f 63 6b 20 69 73 20 61 6e 20 61 70 70 72 6f  lock is an appro
d67f0 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d  priate locking m
d6800 6f 64 65 20 66 6f 72 20 75 73 65 20 69 66 20 6e  ode for use if n
d6810 6f 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69  o.** other locki
d6820 6e 67 20 73 74 72 61 74 65 67 79 20 69 73 20 61  ng strategy is a
d6830 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  vailable..**.** 
d6840 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  Dotfile locking 
d6850 77 6f 72 6b 73 20 62 79 20 63 72 65 61 74 69 6e  works by creatin
d6860 67 20 61 20 66 69 6c 65 20 69 6e 20 74 68 65 20  g a file in the 
d6870 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79 20 61  same directory a
d6880 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  s the.** databas
d6890 65 20 61 6e 64 20 77 69 74 68 20 74 68 65 20 73  e and with the s
d68a0 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69 74  ame name but wit
d68b0 68 20 61 20 22 2e 6c 6f 63 6b 22 20 65 78 74 65  h a ".lock" exte
d68c0 6e 73 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2a 20  nsion added..** 
d68d0 54 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66  The existance of
d68e0 20 61 20 6c 6f 63 6b 20 66 69 6c 65 20 69 6d 70   a lock file imp
d68f0 6c 69 65 73 20 61 6e 20 45 58 43 4c 55 53 49 56  lies an EXCLUSIV
d6900 45 20 6c 6f 63 6b 2e 20 20 41 6c 6c 20 6f 74 68  E lock.  All oth
d6910 65 72 20 6c 6f 63 6b 0a 2a 2a 20 74 79 70 65 73  er lock.** types
d6920 20 28 53 48 41 52 45 44 2c 20 52 45 53 45 52 56   (SHARED, RESERV
d6930 45 44 2c 20 50 45 4e 44 49 4e 47 29 20 61 72 65  ED, PENDING) are
d6940 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 45 58 43   mapped into EXC
d6950 4c 55 53 49 56 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  LUSIVE..*/../*.*
d6960 2a 20 54 68 65 20 66 69 6c 65 20 73 75 66 66 69  * The file suffi
d6970 78 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 64  x added to the d
d6980 61 74 61 20 62 61 73 65 20 66 69 6c 65 6e 61 6d  ata base filenam
d6990 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72  e in order to cr
d69a0 65 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b  eate the.** lock
d69b0 20 66 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e   file..*/.#defin
d69c0 65 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58  e DOTLOCK_SUFFIX
d69d0 20 22 2e 6c 6f 63 6b 22 0a 0a 2f 2a 0a 2a 2a 20   ".lock"../*.** 
d69e0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
d69f0 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20  cks if there is 
d6a00 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
d6a10 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63  held on the spec
d6a20 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79  ified.** file by
d6a30 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68   this or any oth
d6a40 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73  er process. If s
d6a50 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65  uch a lock is he
d6a60 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74  ld, set *pResOut
d6a70 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72  .** to a non-zer
d6a80 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73  o value otherwis
d6a90 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65  e *pResOut is se
d6aa0 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20  t to zero.  The 
d6ab0 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
d6ac0 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  is set to SQLITE
d6ad0 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f  _OK unless an I/
d6ae0 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  O error occurs d
d6af0 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b  uring lock check
d6b00 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 64 6f  ing..**.** In do
d6b10 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 65  tfile locking, e
d6b20 69 74 68 65 72 20 61 20 6c 6f 63 6b 20 65 78 69  ither a lock exi
d6b30 73 74 73 20 6f 72 20 69 74 20 64 6f 65 73 20 6e  sts or it does n
d6b40 6f 74 2e 20 20 53 6f 20 69 6e 20 74 68 69 73 0a  ot.  So in this.
d6b50 2a 2a 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20  ** variation of 
d6b60 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d6b70 6b 28 29 2c 20 2a 70 52 65 73 4f 75 74 20 69 73  k(), *pResOut is
d6b80 20 73 65 74 20 74 6f 20 74 72 75 65 20 69 66 20   set to true if 
d6b90 61 6e 79 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 68  any lock.** is h
d6ba0 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 20  eld on the file 
d6bb0 61 6e 64 20 66 61 6c 73 65 20 69 66 20 74 68 65  and false if the
d6bc0 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65   file is unlocke
d6bd0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
d6be0 20 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73   dotlockCheckRes
d6bf0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
d6c00 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
d6c10 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e  *pResOut) {.  in
d6c20 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
d6c30 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64  ;.  int reserved
d6c40 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65   = 0;.  unixFile
d6c50 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
d6c60 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75  ile*)id;..  Simu
d6c70 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
d6c80 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
d6c90 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f  _CHECKRESERVEDLO
d6ca0 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65  CK; );.  .  asse
d6cb0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20  rt( pFile );..  
d6cc0 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68  /* Check if a th
d6cd0 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f  read in this pro
d6ce0 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20  cess holds such 
d6cf0 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20  a lock */.  if( 
d6d00 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
d6d10 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
d6d20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74 68 69     /* Either thi
d6d30 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20  s connection or 
d6d40 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65  some other conne
d6d50 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 61 6d  ction in the sam
d6d60 65 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a  e process.    **
d6d70 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e   holds a lock on
d6d80 20 74 68 65 20 66 69 6c 65 2e 20 20 4e 6f 20 6e   the file.  No n
d6d90 65 65 64 20 74 6f 20 63 68 65 63 6b 20 66 75 72  eed to check fur
d6da0 74 68 65 72 2e 20 2a 2f 0a 20 20 20 20 72 65 73  ther. */.    res
d6db0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c  erved = 1;.  }el
d6dc0 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 6c  se{.    /* The l
d6dd0 6f 63 6b 20 69 73 20 68 65 6c 64 20 69 66 20 61  ock is held if a
d6de0 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 6c  nd only if the l
d6df0 6f 63 6b 66 69 6c 65 20 65 78 69 73 74 73 20 2a  ockfile exists *
d6e00 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  /.    const char
d6e10 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63   *zLockFile = (c
d6e20 6f 6e 73 74 20 63 68 61 72 2a 29 70 46 69 6c 65  onst char*)pFile
d6e30 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
d6e40 3b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d  ;.    reserved =
d6e50 20 61 63 63 65 73 73 28 7a 4c 6f 63 6b 46 69 6c   access(zLockFil
d6e60 65 2c 20 30 29 3d 3d 30 3b 0a 20 20 7d 0a 20 20  e, 0)==0;.  }.  
d6e70 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57  OSTRACE4("TEST W
d6e80 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c  R-LOCK %d %d %d\
d6e90 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63  n", pFile->h, rc
d6ea0 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 20 20 2a  , reserved);.  *
d6eb0 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76  pResOut = reserv
d6ec0 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ed;.  return rc;
d6ed0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
d6ee0 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
d6ef0 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
d6f00 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
d6f10 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
d6f20 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
d6f30 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
d6f40 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
d6f50 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
d6f60 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
d6f70 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
d6f80 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
d6f90 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
d6fa0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
d6fb0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
d6fc0 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
d6fd0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
d6fe0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
d6ff0 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
d7000 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
d7010 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
d7020 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
d7030 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
d7040 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
d7050 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
d7060 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
d7070 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
d7080 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
d7090 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
d70a0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
d70b0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
d70c0 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
d70d0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
d70e0 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
d70f0 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
d7100 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
d7110 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
d7120 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
d7130 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
d7140 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
d7150 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
d7160 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
d7170 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
d7180 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73  USIVE.**.** This
d7190 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
d71a0 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
d71b0 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c  ck.  Use the sql
d71c0 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a  ite3OsUnlock().*
d71d0 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77  * routine to low
d71e0 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  er a locking lev
d71f0 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 64  el..**.** With d
d7200 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20  otfile locking, 
d7210 77 65 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 20 73  we really only s
d7220 75 70 70 6f 72 74 20 73 74 61 74 65 20 28 34 29  upport state (4)
d7230 3a 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2a 20  : EXCLUSIVE..** 
d7240 42 75 74 20 77 65 20 74 72 61 63 6b 20 74 68 65  But we track the
d7250 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6c   other locking l
d7260 65 76 65 6c 73 20 69 6e 74 65 72 6e 61 6c 6c 79  evels internally
d7270 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
d7280 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69  dotlockLock(sqli
d7290 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
d72a0 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20  t locktype) {.  
d72b0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d72c0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d72d0 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 63 68 61  .  int fd;.  cha
d72e0 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28  r *zLockFile = (
d72f0 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f  char *)pFile->lo
d7300 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
d7310 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
d7320 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20 49 66 20 77 65  OK;...  /* If we
d7330 20 68 61 76 65 20 61 6e 79 20 6c 6f 63 6b 2c 20   have any lock, 
d7340 74 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 66 69  then the lock fi
d7350 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  le already exist
d7360 73 2e 20 20 41 6c 6c 20 77 65 20 68 61 76 65 0a  s.  All we have.
d7370 20 20 2a 2a 20 74 6f 20 64 6f 20 69 73 20 61 64    ** to do is ad
d7380 6a 75 73 74 20 6f 75 72 20 69 6e 74 65 72 6e 61  just our interna
d7390 6c 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20  l record of the 
d73a0 6c 6f 63 6b 20 6c 65 76 65 6c 2e 0a 20 20 2a 2f  lock level..  */
d73b0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
d73c0 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b  cktype > NO_LOCK
d73d0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
d73e0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
d73f0 70 65 3b 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f  pe;.#if !OS_VXWO
d7400 52 4b 53 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79  RKS.    /* Alway
d7410 73 20 75 70 64 61 74 65 20 74 68 65 20 74 69 6d  s update the tim
d7420 65 73 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f 6c  estamp on the ol
d7430 64 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 75 74  d file */.    ut
d7440 69 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20  imes(zLockFile, 
d7450 4e 55 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20  NULL);.#endif.  
d7460 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d7470 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  OK;.  }.  .  /* 
d7480 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76  grab an exclusiv
d7490 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 66 64 20 3d  e lock */.  fd =
d74a0 20 6f 70 65 6e 28 7a 4c 6f 63 6b 46 69 6c 65 2c   open(zLockFile,
d74b0 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54  O_RDONLY|O_CREAT
d74c0 7c 4f 5f 45 58 43 4c 2c 30 36 30 30 29 3b 0a 20  |O_EXCL,0600);. 
d74d0 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20   if( fd<0 ){.   
d74e0 20 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20 6f 70   /* failed to op
d74f0 65 6e 2f 63 72 65 61 74 65 20 74 68 65 20 66 69  en/create the fi
d7500 6c 65 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65  le, someone else
d7510 20 6d 61 79 20 68 61 76 65 20 73 74 6f 6c 65 6e   may have stolen
d7520 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20   the lock */.   
d7530 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72   int tErrno = er
d7540 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 45 45 58  rno;.    if( EEX
d7550 49 53 54 20 3d 3d 20 74 45 72 72 6e 6f 20 29 7b  IST == tErrno ){
d7560 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
d7570 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 20 65  TE_BUSY;.    } e
d7580 6c 73 65 20 7b 0a 20 20 20 20 20 20 72 63 20 3d  lse {.      rc =
d7590 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
d75a0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
d75b0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
d75c0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28  LOCK);.      if(
d75d0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72   IS_LOCK_ERROR(r
d75e0 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46  c) ){.        pF
d75f0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d7600 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d   tErrno;.      }
d7610 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
d7620 6e 20 72 63 3b 0a 20 20 7d 20 0a 20 20 69 66 28  n rc;.  } .  if(
d7630 20 63 6c 6f 73 65 28 66 64 29 20 29 7b 0a 20 20   close(fd) ){.  
d7640 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d7650 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d7660 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
d7670 52 5f 43 4c 4f 53 45 3b 0a 20 20 7d 0a 20 20 0a  R_CLOSE;.  }.  .
d7680 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74    /* got it, set
d7690 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65   the type and re
d76a0 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69  turn ok */.  pFi
d76b0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
d76c0 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72  ocktype;.  retur
d76d0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
d76e0 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
d76f0 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
d7700 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20  escriptor pFile 
d7710 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
d7720 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
d7730 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
d7740 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
d7750 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
d7760 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
d7770 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
d7780 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
d7790 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
d77a0 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
d77b0 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
d77c0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
d77d0 70 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  p..**.** When th
d77e0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
d77f0 72 65 61 63 68 65 73 20 4e 4f 5f 4c 4f 43 4b 2c  reaches NO_LOCK,
d7800 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b   delete the lock
d7810 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
d7820 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f   int dotlockUnlo
d7830 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d7840 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
d7850 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  e) {.  unixFile 
d7860 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d7870 6c 65 2a 29 69 64 3b 0a 20 20 63 68 61 72 20 2a  le*)id;.  char *
d7880 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61  zLockFile = (cha
d7890 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  r *)pFile->locki
d78a0 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73  ngContext;..  as
d78b0 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
d78c0 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43   OSTRACE5("UNLOC
d78d0 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20  K  %d %d was %d 
d78e0 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  pid=%d\n", pFile
d78f0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09  ->h, locktype,..
d7900 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d7910 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20  pe, getpid());. 
d7920 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
d7930 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
d7940 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20  ;.  .  /* no-op 
d7950 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20  if possible */. 
d7960 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d7970 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29  type==locktype )
d7980 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
d7990 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f  ITE_OK;.  }..  /
d79a0 2a 20 54 6f 20 64 6f 77 6e 67 72 61 64 65 20 74  * To downgrade t
d79b0 6f 20 73 68 61 72 65 64 2c 20 73 69 6d 70 6c 79  o shared, simply
d79c0 20 75 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65   update our inte
d79d0 72 6e 61 6c 20 6e 6f 74 69 6f 6e 20 6f 66 20 74  rnal notion of t
d79e0 68 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 73 74 61  he.  ** lock sta
d79f0 74 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20  te.  No need to 
d7a00 6d 65 73 73 20 77 69 74 68 20 74 68 65 20 66 69  mess with the fi
d7a10 6c 65 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f  le on disk..  */
d7a20 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
d7a30 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
d7a40 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d7a50 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43  ype = SHARED_LOC
d7a60 4b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  K;.    return SQ
d7a70 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a  LITE_OK;.  }.  .
d7a80 20 20 2f 2a 20 54 6f 20 66 75 6c 6c 79 20 75 6e    /* To fully un
d7a90 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73  lock the databas
d7aa0 65 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f  e, delete the lo
d7ab0 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 73  ck file */.  ass
d7ac0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e  ert( locktype==N
d7ad0 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20  O_LOCK );.  if( 
d7ae0 75 6e 6c 69 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65  unlink(zLockFile
d7af0 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  ) ){.    int rc 
d7b00 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 74 45 72  = 0;.    int tEr
d7b10 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d7b20 20 69 66 28 20 45 4e 4f 45 4e 54 20 21 3d 20 74   if( ENOENT != t
d7b30 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72  Errno ){.      r
d7b40 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
d7b50 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
d7b60 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
d7b70 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20  RR_UNLOCK);.    
d7b80 7d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43  }.    if( IS_LOC
d7b90 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20  K_ERROR(rc) ){. 
d7ba0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d7bb0 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
d7bc0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
d7bd0 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c   rc; .  }.  pFil
d7be0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f  e->locktype = NO
d7bf0 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20  _LOCK;.  return 
d7c00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
d7c10 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65  .** Close a file
d7c20 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  .  Make sure the
d7c30 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 72   lock has been r
d7c40 65 6c 65 61 73 65 64 20 62 65 66 6f 72 65 20 63  eleased before c
d7c50 6c 6f 73 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  losing..*/.stati
d7c60 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f  c int dotlockClo
d7c70 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  se(sqlite3_file 
d7c80 2a 69 64 29 20 7b 0a 20 20 69 6e 74 20 72 63 3b  *id) {.  int rc;
d7c90 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20  .  if( id ){.   
d7ca0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d7cb0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d7cc0 3b 0a 20 20 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c  ;.    dotlockUnl
d7cd0 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29  ock(id, NO_LOCK)
d7ce0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
d7cf0 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e  ee(pFile->lockin
d7d00 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20  gContext);.  }. 
d7d10 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46   rc = closeUnixF
d7d20 69 6c 65 28 69 64 29 3b 0a 20 20 72 65 74 75 72  ile(id);.  retur
d7d30 6e 20 72 63 3b 0a 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a  n rc;.}./*******
d7d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
d7d50 6f 66 20 74 68 65 20 64 6f 74 2d 66 69 6c 65 20  of the dot-file 
d7d60 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lock implementat
d7d70 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ion ************
d7d80 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
d7d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7dd0 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
d7de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7e20 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
d7e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7e40 2a 2a 2a 20 42 65 67 69 6e 20 66 6c 6f 63 6b 20  *** Begin flock 
d7e50 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a  Locking ********
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 0a 2a 2a 0a 2a 2a 20 55  ********.**.** U
d7e80 73 65 20 74 68 65 20 66 6c 6f 63 6b 28 29 20 73  se the flock() s
d7e90 79 73 74 65 6d 20 63 61 6c 6c 20 74 6f 20 64 6f  ystem call to do
d7ea0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a   file locking..*
d7eb0 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6c 6f 63  *.** flock() loc
d7ec0 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20 64 6f 74  king is like dot
d7ed0 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6e  -file locking in
d7ee0 20 74 68 61 74 20 74 68 65 20 76 61 72 69 6f 75   that the variou
d7ef0 73 0a 2a 2a 20 66 69 6e 65 2d 67 72 61 69 6e 20  s.** fine-grain 
d7f00 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 73 20 73  locking levels s
d7f10 75 70 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69  upported by SQLi
d7f20 74 65 20 61 72 65 20 63 6f 6c 6c 61 70 73 65 64  te are collapsed
d7f30 20 69 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c   into.** a singl
d7f40 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
d7f50 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
d7f60 73 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45 52  s, SHARED, RESER
d7f70 56 45 44 2c 20 61 6e 64 0a 2a 2a 20 50 45 4e 44  VED, and.** PEND
d7f80 49 4e 47 20 6c 6f 63 6b 73 20 61 72 65 20 74 68  ING locks are th
d7f90 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20  e same thing as 
d7fa0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
d7fb0 6b 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 73 74  k.  SQLite.** st
d7fc0 69 6c 6c 20 77 6f 72 6b 73 20 77 68 65 6e 20 79  ill works when y
d7fd0 6f 75 20 64 6f 20 74 68 69 73 2c 20 62 75 74 20  ou do this, but 
d7fe0 63 6f 6e 63 75 72 72 65 6e 63 79 20 69 73 20 72  concurrency is r
d7ff0 65 64 75 63 65 64 20 73 69 6e 63 65 0a 2a 2a 20  educed since.** 
d8000 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72  only a single pr
d8010 6f 63 65 73 73 20 63 61 6e 20 62 65 20 72 65 61  ocess can be rea
d8020 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73  ding the databas
d8030 65 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a  e at a time..**.
d8040 2a 2a 20 4f 6d 69 74 20 74 68 69 73 20 73 65 63  ** Omit this sec
d8050 74 69 6f 6e 20 69 66 20 53 51 4c 49 54 45 5f 45  tion if SQLITE_E
d8060 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
d8070 59 4c 45 20 69 73 20 74 75 72 6e 65 64 20 6f 66  YLE is turned of
d8080 66 20 6f 72 20 69 66 0a 2a 2a 20 63 6f 6d 70 69  f or if.** compi
d8090 6c 69 6e 67 20 66 6f 72 20 56 58 57 4f 52 4b 53  ling for VXWORKS
d80a0 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
d80b0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
d80c0 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f  TYLE && !OS_VXWO
d80d0 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  RKS../*.** This 
d80e0 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
d80f0 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
d8100 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20  ERVED lock held 
d8110 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
d8120 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73  .** file by this
d8130 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72   or any other pr
d8140 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61  ocess. If such a
d8150 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73   lock is held, s
d8160 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74  et *pResOut.** t
d8170 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  o a non-zero val
d8180 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52  ue otherwise *pR
d8190 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20  esOut is set to 
d81a0 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72  zero.  The retur
d81b0 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  n value.** is se
d81c0 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75  t to SQLITE_OK u
d81d0 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72  nless an I/O err
d81e0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
d81f0 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a   lock checking..
d8200 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c  */.static int fl
d8210 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64  ockCheckReserved
d8220 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
d8230 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73  e *id, int *pRes
d8240 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  Out){.  int rc =
d8250 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
d8260 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a  t reserved = 0;.
d8270 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d8280 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d8290 64 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65  d;.  .  Simulate
d82a0 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
d82b0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
d82c0 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20  CKRESERVEDLOCK; 
d82d0 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20  );.  .  assert( 
d82e0 70 46 69 6c 65 20 29 3b 0a 20 20 0a 20 20 2f 2a  pFile );.  .  /*
d82f0 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65   Check if a thre
d8300 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65  ad in this proce
d8310 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20  ss holds such a 
d8320 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46  lock */.  if( pF
d8330 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48  ile->locktype>SH
d8340 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
d8350 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20   reserved = 1;. 
d8360 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72   }.  .  /* Other
d8370 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65  wise see if some
d8380 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68   other process h
d8390 6f 6c 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66  olds it. */.  if
d83a0 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20  ( !reserved ){. 
d83b0 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f     /* attempt to
d83c0 20 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 2a 2f   get the lock */
d83d0 0a 20 20 20 20 69 6e 74 20 6c 72 63 20 3d 20 66  .    int lrc = f
d83e0 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c  lock(pFile->h, L
d83f0 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42  OCK_EX | LOCK_NB
d8400 29 3b 0a 20 20 20 20 69 66 28 20 21 6c 72 63 20  );.    if( !lrc 
d8410 29 7b 0a 20 20 20 20 20 20 2f 2a 20 67 6f 74 20  ){.      /* got 
d8420 74 68 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b  the lock, unlock
d8430 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63   it */.      lrc
d8440 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e   = flock(pFile->
d8450 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20  h, LOCK_UN);.   
d8460 20 20 20 69 66 20 28 20 6c 72 63 20 29 20 7b 0a     if ( lrc ) {.
d8470 20 20 20 20 20 20 20 20 69 6e 74 20 74 45 72 72          int tErr
d8480 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d8490 20 20 20 20 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61      /* unlock fa
d84a0 69 6c 65 64 20 77 69 74 68 20 61 6e 20 65 72 72  iled with an err
d84b0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 72  or */.        lr
d84c0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
d84d0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
d84e0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
d84f0 52 52 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20  RR_UNLOCK); .   
d8500 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b       if( IS_LOCK
d8510 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20  _ERROR(lrc) ){. 
d8520 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
d8530 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
d8540 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63  no;.          rc
d8550 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 20 20   = lrc;.        
d8560 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20  }.      }.    } 
d8570 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 69 6e 74  else {.      int
d8580 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
d8590 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20  .      reserved 
d85a0 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a 20 73 6f  = 1;.      /* so
d85b0 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 69 67 68 74  meone else might
d85c0 20 68 61 76 65 20 69 74 20 72 65 73 65 72 76 65   have it reserve
d85d0 64 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d  d */.      lrc =
d85e0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
d85f0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
d8600 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
d8610 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 69 66  LOCK); .      if
d8620 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
d8630 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  lrc) ){.        
d8640 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d8650 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20   = tErrno;.     
d8660 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20     rc = lrc;.   
d8670 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
d8680 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20   OSTRACE4("TEST 
d8690 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64  WR-LOCK %d %d %d
d86a0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
d86b0 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 23  c, reserved);..#
d86c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e  ifdef SQLITE_IGN
d86d0 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45  ORE_FLOCK_LOCK_E
d86e0 52 52 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20  RRORS.  if( (rc 
d86f0 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20  & SQLITE_IOERR) 
d8700 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  == SQLITE_IOERR 
d8710 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
d8720 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 65 73 65 72  TE_OK;.    reser
d8730 76 65 64 3d 31 3b 0a 20 20 7d 0a 23 65 6e 64 69  ved=1;.  }.#endi
d8740 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f  f /* SQLITE_IGNO
d8750 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52  RE_FLOCK_LOCK_ER
d8760 52 4f 52 53 20 2a 2f 0a 20 20 2a 70 52 65 73 4f  RORS */.  *pResO
d8770 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20  ut = reserved;. 
d8780 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
d8790 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69  *.** Lock the fi
d87a0 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b  le with the lock
d87b0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
d87c0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65  rameter locktype
d87d0 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65   - one.** of the
d87e0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
d87f0 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44  *     (1) SHARED
d8800 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29  _LOCK.**     (2)
d8810 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a   RESERVED_LOCK.*
d8820 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e  *     (3) PENDIN
d8830 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34  G_LOCK.**     (4
d8840 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ) EXCLUSIVE_LOCK
d8850 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
d8860 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67   when requesting
d8870 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c   one lock state,
d8880 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b   additional lock
d8890 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69   states.** are i
d88a0 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65  nserted in betwe
d88b0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  en.  The locking
d88c0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f   might fail on o
d88d0 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a  ne of the later.
d88e0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c  ** transitions l
d88f0 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  eaving the lock 
d8900 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20  state different 
d8910 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61  from what it sta
d8920 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c  rted but.** stil
d8930 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67  l short of its g
d8940 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  oal.  The follow
d8950 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20  ing chart shows 
d8960 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74  the allowed.** t
d8970 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74  ransitions and t
d8980 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65  he inserted inte
d8990 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a  rmediate states:
d89a0 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b  .**.**    UNLOCK
d89b0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20  ED -> SHARED.** 
d89c0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53     SHARED -> RES
d89d0 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  ERVED.**    SHAR
d89e0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
d89f0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
d8a00 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28     RESERVED -> (
d8a10 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
d8a20 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44  USIVE.**    PEND
d8a30 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  ING -> EXCLUSIVE
d8a40 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6f  .**.** flock() o
d8a50 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f  nly really suppo
d8a60 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  rt EXCLUSIVE loc
d8a70 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e  ks.  We track in
d8a80 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f  termediate.** lo
d8a90 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 68 65  ck states in the
d8aa0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74   sqlite3_file st
d8ab0 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c  ructure, but all
d8ac0 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72   locks SHARED or
d8ad0 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65  .** above are re
d8ae0 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c  ally EXCLUSIVE l
d8af0 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65  ocks and exclude
d8b00 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65   all other proce
d8b10 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63  sses from.** acc
d8b20 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a  ess the file..**
d8b30 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
d8b40 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65   will only incre
d8b50 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65  ase a lock.  Use
d8b60 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e   the sqlite3OsUn
d8b70 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e  lock().** routin
d8b80 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63  e to lower a loc
d8b90 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73  king level..*/.s
d8ba0 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c  tatic int flockL
d8bb0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
d8bc0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
d8bd0 70 65 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pe) {.  int rc =
d8be0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e   SQLITE_OK;.  un
d8bf0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
d8c00 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a  (unixFile*)id;..
d8c10 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
d8c20 29 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61  );..  /* if we a
d8c30 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f  lready have a lo
d8c40 63 6b 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73  ck, it is exclus
d8c50 69 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74  ive.  .  ** Just
d8c60 20 61 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e   adjust level an
d8c70 64 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20  d punt on outta 
d8c80 68 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70  here. */.  if (p
d8c90 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e  File->locktype >
d8ca0 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20   NO_LOCK) {.    
d8cb0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d8cc0 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20  = locktype;.    
d8cd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d8ce0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72  ;.  }.  .  /* gr
d8cf0 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  ab an exclusive 
d8d00 6c 6f 63 6b 20 2a 2f 0a 20 20 0a 20 20 69 66 20  lock */.  .  if 
d8d10 28 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c  (flock(pFile->h,
d8d20 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f   LOCK_EX | LOCK_
d8d30 4e 42 29 29 20 7b 0a 20 20 20 20 69 6e 74 20 74  NB)) {.    int t
d8d40 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d8d50 20 20 20 2f 2a 20 64 69 64 6e 27 74 20 67 65 74     /* didn't get
d8d60 2c 20 6d 75 73 74 20 62 65 20 62 75 73 79 20 2a  , must be busy *
d8d70 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
d8d80 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
d8d90 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
d8da0 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b  ITE_IOERR_LOCK);
d8db0 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b  .    if( IS_LOCK
d8dc0 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
d8dd0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d8de0 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
d8df0 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a     }.  } else {.
d8e00 20 20 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73      /* got it, s
d8e10 65 74 20 74 68 65 20 74 79 70 65 20 61 6e 64 20  et the type and 
d8e20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20  return ok */.   
d8e30 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d8e40 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d   = locktype;.  }
d8e50 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43  .  OSTRACE4("LOC
d8e60 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22  K    %d %s %s\n"
d8e70 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
d8e80 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70  typeName(locktyp
d8e90 65 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  e), .           
d8ea0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20  rc==SQLITE_OK ? 
d8eb0 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29  "ok" : "failed")
d8ec0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
d8ed0 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43  IGNORE_FLOCK_LOC
d8ee0 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28 20 28  K_ERRORS.  if( (
d8ef0 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc & SQLITE_IOER
d8f00 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45  R) == SQLITE_IOE
d8f10 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  RR ){.    rc = S
d8f20 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a  QLITE_BUSY;.  }.
d8f30 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
d8f40 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f  _IGNORE_FLOCK_LO
d8f50 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 72  CK_ERRORS */.  r
d8f60 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
d8f70 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
d8f80 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
d8f90 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
d8fa0 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  File to locktype
d8fb0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
d8fc0 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
d8fd0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
d8fe0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
d8ff0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
d9000 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
d9010 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
d9020 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
d9030 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
d9040 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
d9050 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
d9060 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
d9070 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 6c 6f 63  c int flockUnloc
d9080 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d9090 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
d90a0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ) {.  unixFile *
d90b0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d90c0 65 2a 29 69 64 3b 0a 20 20 0a 20 20 61 73 73 65  e*)id;.  .  asse
d90d0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f  rt( pFile );.  O
d90e0 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20  STRACE5("UNLOCK 
d90f0 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70 69   %d %d was %d pi
d9100 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d=%d\n", pFile->
d9110 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20  h, locktype,.   
d9120 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
d9130 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28  ocktype, getpid(
d9140 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f  ));.  assert( lo
d9150 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
d9160 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e  OCK );.  .  /* n
d9170 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65  o-op if possible
d9180 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
d9190 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74  >locktype==lockt
d91a0 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ype ){.    retur
d91b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d91c0 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 20  .  .  /* shared 
d91d0 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20  can just be set 
d91e0 62 65 63 61 75 73 65 20 77 65 20 61 6c 77 61 79  because we alway
d91f0 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73  s have an exclus
d9200 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63  ive */.  if (loc
d9210 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
d9220 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d  CK) {.    pFile-
d9230 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
d9240 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e  type;.    return
d9250 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
d9260 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c    .  /* no, real
d9270 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20  ly, unlock. */. 
d9280 20 69 6e 74 20 72 63 20 3d 20 66 6c 6f 63 6b 28   int rc = flock(
d9290 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55  pFile->h, LOCK_U
d92a0 4e 29 3b 0a 20 20 69 66 20 28 72 63 29 20 7b 0a  N);.  if (rc) {.
d92b0 20 20 20 20 69 6e 74 20 72 2c 20 74 45 72 72 6e      int r, tErrn
d92c0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72  o = errno;.    r
d92d0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72   = sqliteErrorFr
d92e0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72  omPosixError(tEr
d92f0 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52  rno, SQLITE_IOER
d9300 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69  R_UNLOCK);.    i
d9310 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
d9320 28 72 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69  (r) ){.      pFi
d9330 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d9340 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 23 69  tErrno;.    }.#i
d9350 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f  fdef SQLITE_IGNO
d9360 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52  RE_FLOCK_LOCK_ER
d9370 52 4f 52 53 0a 20 20 20 20 69 66 28 20 28 72 20  RORS.    if( (r 
d9380 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20  & SQLITE_IOERR) 
d9390 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  == SQLITE_IOERR 
d93a0 29 7b 0a 20 20 20 20 20 20 72 20 3d 20 53 51 4c  ){.      r = SQL
d93b0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a  ITE_BUSY;.    }.
d93c0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
d93d0 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f  _IGNORE_FLOCK_LO
d93e0 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20  CK_ERRORS */.   
d93f0 20 0a 20 20 20 20 72 65 74 75 72 6e 20 72 3b 0a   .    return r;.
d9400 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 70    } else {.    p
d9410 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d9420 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65   NO_LOCK;.    re
d9430 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d9440 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f    }.}../*.** Clo
d9450 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  se a file..*/.st
d9460 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 6c  atic int flockCl
d9470 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
d9480 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64   *id) {.  if( id
d9490 20 29 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c   ){.    flockUnl
d94a0 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29  ock(id, NO_LOCK)
d94b0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63  ;.  }.  return c
d94c0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29  loseUnixFile(id)
d94d0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
d94e0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
d94f0 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f  KING_STYLE && !O
d9500 53 5f 56 58 57 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a  S_VXWORK */../**
d9510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9520 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 66 6c 6f  * End of the flo
d9530 63 6b 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  ck lock implemen
d9540 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tation *********
d9550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
d9560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d95a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
d95b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d95c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d95d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d95e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d95f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d9600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9610 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 4e 61 6d  ****** Begin Nam
d9620 65 64 20 53 65 6d 61 70 68 6f 72 65 20 4c 6f 63  ed Semaphore Loc
d9630 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  king ***********
d9640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d9650 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68  .** Named semaph
d9660 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f  ore locking is o
d9670 6e 6c 79 20 73 75 70 70 6f 72 74 65 64 20 6f 6e  nly supported on
d9680 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20   VxWorks..**.** 
d9690 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e  Semaphore lockin
d96a0 67 20 69 73 20 6c 69 6b 65 20 64 6f 74 2d 6c 6f  g is like dot-lo
d96b0 63 6b 20 61 6e 64 20 66 6c 6f 63 6b 20 69 6e 20  ck and flock in 
d96c0 74 68 61 74 20 69 74 20 72 65 61 6c 6c 79 20 6f  that it really o
d96d0 6e 6c 79 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20  nly.** supports 
d96e0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 69 6e  EXCLUSIVE lockin
d96f0 67 2e 20 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c  g.  Only a singl
d9700 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 72 65  e process can re
d9710 61 64 20 6f 72 20 77 72 69 74 65 0a 2a 2a 20 74  ad or write.** t
d9720 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
d9730 20 61 74 20 61 20 74 69 6d 65 2e 20 20 54 68 69   at a time.  Thi
d9740 73 20 72 65 64 75 63 65 73 20 70 6f 74 65 6e 74  s reduces potent
d9750 69 61 6c 20 63 6f 6e 63 75 72 72 65 6e 63 79 2c  ial concurrency,
d9760 20 62 75 74 0a 2a 2a 20 6d 61 6b 65 73 20 74 68   but.** makes th
d9770 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  e lock implement
d9780 61 74 69 6f 6e 20 6d 75 63 68 20 65 61 73 69 65  ation much easie
d9790 72 2e 0a 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57  r..*/.#if OS_VXW
d97a0 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ORKS../*.** This
d97b0 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20   routine checks 
d97c0 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45  if there is a RE
d97d0 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64  SERVED lock held
d97e0 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65   on the specifie
d97f0 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69  d.** file by thi
d9800 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70  s or any other p
d9810 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20  rocess. If such 
d9820 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20  a lock is held, 
d9830 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20  set *pResOut.** 
d9840 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61  to a non-zero va
d9850 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70  lue otherwise *p
d9860 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f  ResOut is set to
d9870 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75   zero.  The retu
d9880 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73  rn value.** is s
d9890 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20  et to SQLITE_OK 
d98a0 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72  unless an I/O er
d98b0 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
d98c0 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e  g lock checking.
d98d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
d98e0 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  emCheckReservedL
d98f0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
d9900 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f   *id, int *pResO
d9910 75 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ut) {.  int rc =
d9920 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
d9930 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a  t reserved = 0;.
d9940 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d9950 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d9960 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  d;..  SimulateIO
d9970 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
d9980 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b  LITE_IOERR_CHECK
d9990 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b  RESERVEDLOCK; );
d99a0 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46  .  .  assert( pF
d99b0 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65  ile );..  /* Che
d99c0 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69  ck if a thread i
d99d0 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68  n this process h
d99e0 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b  olds such a lock
d99f0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
d9a00 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44  >locktype>SHARED
d9a10 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73  _LOCK ){.    res
d9a20 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  erved = 1;.  }. 
d9a30 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65   .  /* Otherwise
d9a40 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68   see if some oth
d9a50 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73  er process holds
d9a60 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72   it. */.  if( !r
d9a70 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73  eserved ){.    s
d9a80 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69  em_t *pSem = pFi
d9a90 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b  le->pOpen->pSem;
d9aa0 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74  .    struct stat
d9ab0 20 73 74 61 74 42 75 66 3b 0a 0a 20 20 20 20 69   statBuf;..    i
d9ac0 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70  f( sem_trywait(p
d9ad0 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20  Sem)==-1 ){.    
d9ae0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
d9af0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20  rrno;.      if( 
d9b00 45 41 47 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f  EAGAIN != tErrno
d9b10 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
d9b20 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
d9b30 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
d9b40 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
d9b50 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43  CHECKRESERVEDLOC
d9b60 4b 29 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c  K);.        pFil
d9b70 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d9b80 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65  Errno;.      } e
d9b90 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a  lse {.        /*
d9ba0 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61   someone else ha
d9bb0 73 20 74 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20  s the lock when 
d9bc0 77 65 20 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43  we are in NO_LOC
d9bd0 4b 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 73  K */.        res
d9be0 65 72 76 65 64 20 3d 20 28 70 46 69 6c 65 2d 3e  erved = (pFile->
d9bf0 6c 6f 63 6b 74 79 70 65 20 3c 20 53 48 41 52 45  locktype < SHARE
d9c00 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d  D_LOCK);.      }
d9c10 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
d9c20 20 20 2f 2a 20 77 65 20 63 6f 75 6c 64 20 68 61    /* we could ha
d9c30 76 65 20 69 74 20 69 66 20 77 65 20 77 61 6e 74  ve it if we want
d9c40 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 73 65 6d   it */.      sem
d9c50 5f 70 6f 73 74 28 70 53 65 6d 29 3b 0a 20 20 20  _post(pSem);.   
d9c60 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45   }.  }.  OSTRACE
d9c70 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20  4("TEST WR-LOCK 
d9c80 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69  %d %d %d\n", pFi
d9c90 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72  le->h, rc, reser
d9ca0 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75  ved);..  *pResOu
d9cb0 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20  t = reserved;.  
d9cc0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
d9cd0 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c  .** Lock the fil
d9ce0 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20  e with the lock 
d9cf0 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72  specified by par
d9d00 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20  ameter locktype 
d9d10 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20  - one.** of the 
d9d20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
d9d30 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f       (1) SHARED_
d9d40 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20  LOCK.**     (2) 
d9d50 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a  RESERVED_LOCK.**
d9d60 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47       (3) PENDING
d9d70 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29  _LOCK.**     (4)
d9d80 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a   EXCLUSIVE_LOCK.
d9d90 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20  **.** Sometimes 
d9da0 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20  when requesting 
d9db0 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20  one lock state, 
d9dc0 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20  additional lock 
d9dd0 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e  states.** are in
d9de0 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65  serted in betwee
d9df0 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20  n.  The locking 
d9e00 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e  might fail on on
d9e10 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a  e of the later.*
d9e20 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65  * transitions le
d9e30 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73  aving the lock s
d9e40 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66  tate different f
d9e50 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72  rom what it star
d9e60 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c  ted but.** still
d9e70 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f   short of its go
d9e80 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  al.  The followi
d9e90 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74  ng chart shows t
d9ea0 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72  he allowed.** tr
d9eb0 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68  ansitions and th
d9ec0 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72  e inserted inter
d9ed0 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a  mediate states:.
d9ee0 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45  **.**    UNLOCKE
d9ef0 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20  D -> SHARED.**  
d9f00 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45    SHARED -> RESE
d9f10 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  RVED.**    SHARE
d9f20 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d  D -> (PENDING) -
d9f30 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20  > EXCLUSIVE.**  
d9f40 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50    RESERVED -> (P
d9f50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
d9f60 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49  SIVE.**    PENDI
d9f70 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  NG -> EXCLUSIVE.
d9f80 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20  **.** Semaphore 
d9f90 6c 6f 63 6b 73 20 6f 6e 6c 79 20 72 65 61 6c 6c  locks only reall
d9fa0 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53  y support EXCLUS
d9fb0 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74  IVE locks.  We t
d9fc0 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74  rack intermediat
d9fd0 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73  e.** lock states
d9fe0 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f   in the sqlite3_
d9ff0 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20  file structure, 
da000 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48  but all locks SH
da010 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65  ARED or.** above
da020 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c   are really EXCL
da030 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20  USIVE locks and 
da040 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65  exclude all othe
da050 72 20 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d  r processes from
da060 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65 20 66  .** access the f
da070 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ile..**.** This 
da080 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
da090 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
da0a0 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69  k.  Use the sqli
da0b0 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a  te3OsUnlock().**
da0c0 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65   routine to lowe
da0d0 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  r a locking leve
da0e0 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
da0f0 20 73 65 6d 4c 6f 63 6b 28 73 71 6c 69 74 65 33   semLock(sqlite3
da100 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
da110 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69  ocktype) {.  uni
da120 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
da130 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
da140 69 6e 74 20 66 64 3b 0a 20 20 73 65 6d 5f 74 20  int fd;.  sem_t 
da150 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70  *pSem = pFile->p
da160 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 69 6e  Open->pSem;.  in
da170 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
da180 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c  ;..  /* if we al
da190 72 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63  ready have a loc
da1a0 6b 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73 69  k, it is exclusi
da1b0 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20  ve.  .  ** Just 
da1c0 61 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64  adjust level and
da1d0 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68   punt on outta h
da1e0 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46  ere. */.  if (pF
da1f0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20  ile->locktype > 
da200 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70  NO_LOCK) {.    p
da210 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
da220 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72   locktype;.    r
da230 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
da240 20 20 20 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f     goto sem_end_
da250 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  lock;.  }.  .  /
da260 2a 20 6c 6f 63 6b 20 73 65 6d 61 70 68 6f 72 65  * lock semaphore
da270 20 6e 6f 77 20 62 75 74 20 62 61 69 6c 20 6f 75   now but bail ou
da280 74 20 77 68 65 6e 20 61 6c 72 65 61 64 79 20 6c  t when already l
da290 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20  ocked. */.  if( 
da2a0 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 65 6d  sem_trywait(pSem
da2b0 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 72 63 20  )==-1 ){.    rc 
da2c0 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
da2d0 20 20 20 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f     goto sem_end_
da2e0 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  lock;.  }..  /* 
da2f0 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20  got it, set the 
da300 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20  type and return 
da310 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c  ok */.  pFile->l
da320 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
da330 70 65 3b 0a 0a 20 73 65 6d 5f 65 6e 64 5f 6c 6f  pe;.. sem_end_lo
da340 63 6b 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ck:.  return rc;
da350 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20  .}../*.** Lower 
da360 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
da370 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69  l on file descri
da380 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f  ptor pFile to lo
da390 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70  cktype.  locktyp
da3a0 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  e.** must be eit
da3b0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
da3c0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
da3d0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
da3e0 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
da3f0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
da400 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
da410 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75  elow.** the requ
da420 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
da430 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
da440 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  e is a no-op..*/
da450 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 55  .static int semU
da460 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
da470 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
da480 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69  type) {.  unixFi
da490 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
da4a0 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73 65 6d  xFile*)id;.  sem
da4b0 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65  _t *pSem = pFile
da4c0 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 0a  ->pOpen->pSem;..
da4d0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
da4e0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 65  );.  assert( pSe
da4f0 6d 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28  m );.  OSTRACE5(
da500 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77  "UNLOCK  %d %d w
da510 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c  as %d pid=%d\n",
da520 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
da530 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e  ype,..   pFile->
da540 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64  locktype, getpid
da550 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  ());.  assert( l
da560 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f  ocktype<=SHARED_
da570 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20  LOCK );.  .  /* 
da580 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c  no-op if possibl
da590 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  e */.  if( pFile
da5a0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b  ->locktype==lock
da5b0 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75  type ){.    retu
da5c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
da5d0 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 64  }.  .  /* shared
da5e0 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65 74   can just be set
da5f0 20 62 65 63 61 75 73 65 20 77 65 20 61 6c 77 61   because we alwa
da600 79 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75  ys have an exclu
da610 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f  sive */.  if (lo
da620 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
da630 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65  OCK) {.    pFile
da640 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
da650 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72  ktype;.    retur
da660 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
da670 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61  .  .  /* no, rea
da680 6c 6c 79 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20  lly unlock. */. 
da690 20 69 66 20 28 20 73 65 6d 5f 70 6f 73 74 28 70   if ( sem_post(p
da6a0 53 65 6d 29 3d 3d 2d 31 20 29 20 7b 0a 20 20 20  Sem)==-1 ) {.   
da6b0 20 69 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f 20   int rc, tErrno 
da6c0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20  = errno;.    rc 
da6d0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
da6e0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
da6f0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
da700 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  _UNLOCK);.    if
da710 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
da720 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69  rc) ){.      pFi
da730 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
da740 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20  tErrno;.    }.  
da750 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 20    return rc; .  
da760 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  }.  pFile->lockt
da770 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20  ype = NO_LOCK;. 
da780 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
da790 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f  K;.}../*. ** Clo
da7a0 73 65 20 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73  se a file.. */.s
da7b0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43 6c 6f  tatic int semClo
da7c0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  se(sqlite3_file 
da7d0 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20  *id) {.  if( id 
da7e0 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  ){.    unixFile 
da7f0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
da800 6c 65 2a 29 69 64 3b 0a 20 20 20 20 73 65 6d 55  le*)id;.    semU
da810 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43  nlock(id, NO_LOC
da820 4b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  K);.    assert( 
da830 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 75 6e 69  pFile );.    uni
da840 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  xEnterMutex();. 
da850 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e     releaseLockIn
da860 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29  fo(pFile->pLock)
da870 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65  ;.    releaseOpe
da880 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65  nCnt(pFile->pOpe
da890 6e 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76  n);.    unixLeav
da8a0 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 63 6c  eMutex();.    cl
da8b0 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b  oseUnixFile(id);
da8c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
da8d0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64  LITE_OK;.}..#end
da8e0 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53  if /* OS_VXWORKS
da8f0 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 64 20   */./*.** Named 
da900 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e  semaphore lockin
da910 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  g is only availa
da920 62 6c 65 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a  ble on VxWorks..
da930 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
da940 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61  ** End of the na
da950 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f  med semaphore lo
da960 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ck implementatio
da970 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n **************
da980 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
da990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da9d0 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */.../**********
da9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daa20 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
daa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daa40 20 42 65 67 69 6e 20 41 46 50 20 4c 6f 63 6b 69   Begin AFP Locki
daa50 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ng *************
daa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daa70 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 20 69  ****.**.** AFP i
daa80 73 20 74 68 65 20 41 70 70 6c 65 20 46 69 6c 69  s the Apple Fili
daa90 6e 67 20 50 72 6f 74 6f 63 6f 6c 2e 20 20 41 46  ng Protocol.  AF
daaa0 50 20 69 73 20 61 20 6e 65 74 77 6f 72 6b 20 66  P is a network f
daab0 69 6c 65 73 79 73 74 65 6d 20 66 6f 75 6e 64 0a  ilesystem found.
daac0 2a 2a 20 6f 6e 20 41 70 70 6c 65 20 4d 61 63 69  ** on Apple Maci
daad0 6e 74 6f 73 68 20 63 6f 6d 70 75 74 65 72 73 20  ntosh computers 
daae0 2d 20 62 6f 74 68 20 4f 53 39 20 61 6e 64 20 4f  - both OS9 and O
daaf0 53 58 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 72 64 2d  SX..**.** Third-
dab00 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61  party implementa
dab10 74 69 6f 6e 73 20 6f 66 20 41 46 50 20 61 72 65  tions of AFP are
dab20 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 42 75 74   available.  But
dab30 20 74 68 69 73 20 63 6f 64 65 20 68 65 72 65 0a   this code here.
dab40 2a 2a 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e  ** only works on
dab50 20 4f 53 58 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65   OSX..*/..#if de
dab60 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
dab70 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
dab80 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
dab90 2f 2a 0a 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63  /*.** The afpLoc
daba0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75  kingContext stru
dabb0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
dabc0 6c 6c 20 61 66 70 20 6c 6f 63 6b 20 73 70 65 63  ll afp lock spec
dabd0 69 66 69 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79  ific state.*/.ty
dabe0 70 65 64 65 66 20 73 74 72 75 63 74 20 61 66 70  pedef struct afp
dabf0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61  LockingContext a
dac00 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
dac10 3b 0a 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b  ;.struct afpLock
dac20 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75  ingContext {.  u
dac30 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
dac40 67 20 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20  g sharedByte;.  
dac50 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61  const char *dbPa
dac60 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  th;             
dac70 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f  /* Name of the o
dac80 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a  pen file */.};..
dac90 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65  struct ByteRange
daca0 4c 6f 63 6b 50 42 32 0a 7b 0a 20 20 75 6e 73 69  LockPB2.{.  unsi
dacb0 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f  gned long long o
dacc0 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a  ffset;        /*
dacd0 20 6f 66 66 73 65 74 20 74 6f 20 66 69 72 73 74   offset to first
dace0 20 62 79 74 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f   byte to lock */
dacf0 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  .  unsigned long
dad00 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 3b 20 20 20   long length;   
dad10 20 20 20 20 20 2f 2a 20 6e 62 72 20 6f 66 20 62       /* nbr of b
dad20 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a  ytes to lock */.
dad30 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
dad40 6c 6f 6e 67 20 72 65 74 52 61 6e 67 65 53 74 61  long retRangeSta
dad50 72 74 3b 20 2f 2a 20 6e 62 72 20 6f 66 20 31 73  rt; /* nbr of 1s
dad60 74 20 62 79 74 65 20 6c 6f 63 6b 65 64 20 69 66  t byte locked if
dad70 20 73 75 63 63 65 73 73 66 75 6c 20 2a 2f 0a 20   successful */. 
dad80 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75   unsigned char u
dad90 6e 4c 6f 63 6b 46 6c 61 67 3b 20 20 20 20 20 20  nLockFlag;      
dada0 20 20 20 2f 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b     /* 1 = unlock
dadb0 2c 20 30 20 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20  , 0 = lock */.  
dadc0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 74  unsigned char st
dadd0 61 72 74 45 6e 64 46 6c 61 67 3b 20 20 20 20 20  artEndFlag;     
dade0 20 20 2f 2a 20 31 3d 72 65 6c 20 74 6f 20 65 6e    /* 1=rel to en
dadf0 64 20 6f 66 20 66 6f 72 6b 2c 20 30 3d 72 65 6c  d of fork, 0=rel
dae00 20 74 6f 20 73 74 61 72 74 20 2a 2f 0a 20 20 69   to start */.  i
dae10 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20  nt fd;          
dae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dae30 20 2f 2a 20 66 69 6c 65 20 64 65 73 63 20 74 6f   /* file desc to
dae40 20 61 73 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b   assoc this lock
dae50 20 77 69 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65   with */.};..#de
dae60 66 69 6e 65 20 61 66 70 66 73 42 79 74 65 52 61  fine afpfsByteRa
dae70 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20  ngeLock2FSCTL   
dae80 20 20 20 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20       _IOWR('z', 
dae90 32 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52  23, struct ByteR
daea0 61 6e 67 65 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a  angeLockPB2)../*
daeb0 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 75 74  .** This is a ut
daec0 69 6c 69 74 79 20 66 6f 72 20 73 65 74 74 69 6e  ility for settin
daed0 67 20 6f 72 20 63 6c 65 61 72 69 6e 67 20 61 20  g or clearing a 
daee0 62 69 74 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 6f  bit-range lock o
daef0 6e 20 61 6e 0a 2a 2a 20 41 46 50 20 66 69 6c 65  n an.** AFP file
daf00 73 79 73 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52  system..** .** R
daf10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
daf20 6f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49  on success, SQLI
daf30 54 45 5f 42 55 53 59 20 6f 6e 20 66 61 69 6c 75  TE_BUSY on failu
daf40 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  re..*/.static in
daf50 74 20 61 66 70 53 65 74 4c 6f 63 6b 28 0a 20 20  t afpSetLock(.  
daf60 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68  const char *path
daf70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
daf80 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69  * Name of the fi
daf90 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20  le to be locked 
dafa0 6f 72 20 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0a 20  or unlocked */. 
dafb0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
dafc0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
dafd0 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73  /* Open file des
dafe0 63 72 69 70 74 6f 72 20 6f 6e 20 70 61 74 68 20  criptor on path 
daff0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f  */.  unsigned lo
db000 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20  ng long offset, 
db010 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
db020 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a  e to be locked *
db030 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  /.  unsigned lon
db040 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 20  g long length,  
db050 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
db060 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f  bytes to lock */
db070 0a 20 20 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c  .  int setLockFl
db080 61 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ag              
db090 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74    /* True to set
db0a0 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f   lock.  False to
db0b0 20 63 6c 65 61 72 20 6c 6f 63 6b 20 2a 2f 0a 29   clear lock */.)
db0c0 7b 0a 20 20 73 74 72 75 63 74 20 42 79 74 65 52  {.  struct ByteR
db0d0 61 6e 67 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0a  angeLockPB2 pb;.
db0e0 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 0a 20 20    int err;.  .  
db0f0 70 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20  pb.unLockFlag = 
db100 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20  setLockFlag ? 0 
db110 3a 20 31 3b 0a 20 20 70 62 2e 73 74 61 72 74 45  : 1;.  pb.startE
db120 6e 64 46 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62  ndFlag = 0;.  pb
db130 2e 6f 66 66 73 65 74 20 3d 20 6f 66 66 73 65 74  .offset = offset
db140 3b 0a 20 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20  ;.  pb.length = 
db150 6c 65 6e 67 74 68 3b 20 0a 20 20 70 62 2e 66 64  length; .  pb.fd
db160 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 0a   = pFile->h;.  .
db170 20 20 4f 53 54 52 41 43 45 36 28 22 41 46 50 53    OSTRACE6("AFPS
db180 45 54 4c 4f 43 4b 20 5b 25 73 5d 20 66 6f 72 20  ETLOCK [%s] for 
db190 25 64 25 73 20 69 6e 20 72 61 6e 67 65 20 25 6c  %d%s in range %l
db1a0 6c 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20  lx:%llx\n", .   
db1b0 20 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f   (setLockFlag?"O
db1c0 4e 22 3a 22 4f 46 46 22 29 2c 20 70 46 69 6c 65  N":"OFF"), pFile
db1d0 2d 3e 68 2c 20 28 70 62 2e 66 64 3d 3d 2d 31 3f  ->h, (pb.fd==-1?
db1e0 22 5b 74 65 73 74 76 61 6c 2d 31 5d 22 3a 22 22  "[testval-1]":""
db1f0 29 2c 0a 20 20 20 20 6f 66 66 73 65 74 2c 20 6c  ),.    offset, l
db200 65 6e 67 74 68 29 3b 0a 20 20 65 72 72 20 3d 20  ength);.  err = 
db210 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66  fsctl(path, afpf
db220 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46  sByteRangeLock2F
db230 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20  SCTL, &pb, 0);. 
db240 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20   if ( err==-1 ) 
db250 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
db260 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
db270 72 72 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43  rrno;.    OSTRAC
db280 45 34 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 66  E4("AFPSETLOCK f
db290 61 69 6c 65 64 20 74 6f 20 66 73 63 74 6c 28 29  ailed to fsctl()
db2a0 20 27 25 73 27 20 25 64 20 25 73 5c 6e 22 2c 0a   '%s' %d %s\n",.
db2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74               pat
db2c0 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72 65 72  h, tErrno, strer
db2d0 72 6f 72 28 74 45 72 72 6e 6f 29 29 3b 0a 23 69  ror(tErrno));.#i
db2e0 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f  fdef SQLITE_IGNO
db2f0 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f  RE_AFP_LOCK_ERRO
db300 52 53 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  RS.    rc = SQLI
db310 54 45 5f 42 55 53 59 3b 0a 23 65 6c 73 65 0a 20  TE_BUSY;.#else. 
db320 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72     rc = sqliteEr
db330 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
db340 72 28 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20  r(tErrno,.      
db350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 65                se
db360 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 53 51 4c 49  tLockFlag ? SQLI
db370 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 3a 20  TE_IOERR_LOCK : 
db380 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
db390 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  OCK);.#endif /* 
db3a0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46  SQLITE_IGNORE_AF
db3b0 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f  P_LOCK_ERRORS */
db3c0 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b  .    if( IS_LOCK
db3d0 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
db3e0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
db3f0 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
db400 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
db410 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20  rc;.  } else {. 
db420 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
db430 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  _OK;.  }.}../*.*
db440 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
db450 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69  hecks if there i
db460 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  s a RESERVED loc
db470 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70  k held on the sp
db480 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20  ecified.** file 
db490 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f  by this or any o
db4a0 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66  ther process. If
db4b0 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20   such a lock is 
db4c0 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f  held, set *pResO
db4d0 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a  ut.** to a non-z
db4e0 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77  ero value otherw
db4f0 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20  ise *pResOut is 
db500 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68  set to zero.  Th
db510 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
db520 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49  * is set to SQLI
db530 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20  TE_OK unless an 
db540 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73  I/O error occurs
db550 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65   during lock che
db560 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  cking..*/.static
db570 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52 65 73   int afpCheckRes
db580 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
db590 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
db5a0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74  *pResOut){.  int
db5b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
db5c0 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20  .  int reserved 
db5d0 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  = 0;.  unixFile 
db5e0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
db5f0 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d  le*)id;.  .  Sim
db600 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65  ulateIOError( re
db610 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
db620 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c  R_CHECKRESERVEDL
db630 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73  OCK; );.  .  ass
db640 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
db650 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
db660 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 66  t *context = (af
db670 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  pLockingContext 
db680 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e  *) pFile->lockin
db690 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 2f  gContext;.  .  /
db6a0 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72  * Check if a thr
db6b0 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63  ead in this proc
db6c0 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61  ess holds such a
db6d0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70   lock */.  if( p
db6e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53  File->locktype>S
db6f0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
db700 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
db710 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65    }.  .  /* Othe
db720 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d  rwise see if som
db730 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  e other process 
db740 68 6f 6c 64 73 20 69 74 2e 0a 20 20 20 2a 2f 0a  holds it..   */.
db750 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 20    if( !reserved 
db760 29 7b 0a 20 20 20 20 2f 2a 20 6c 6f 63 6b 20 74  ){.    /* lock t
db770 68 65 20 52 45 53 45 52 56 45 44 20 62 79 74 65  he RESERVED byte
db780 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 63 20   */.    int lrc 
db790 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e  = afpSetLock(con
db7a0 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46  text->dbPath, pF
db7b0 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59  ile, RESERVED_BY
db7c0 54 45 2c 20 31 2c 31 29 3b 20 20 0a 20 20 20 20  TE, 1,1);  .    
db7d0 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 6c  if( SQLITE_OK==l
db7e0 72 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69  rc ){.      /* i
db7f0 66 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69  f we succeeded i
db800 6e 20 74 61 6b 69 6e 67 20 74 68 65 20 72 65 73  n taking the res
db810 65 72 76 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c 6f  erved lock, unlo
db820 63 6b 20 69 74 20 74 6f 20 72 65 73 74 6f 72 65  ck it to restore
db830 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6f 72  .      ** the or
db840 69 67 69 6e 61 6c 20 73 74 61 74 65 20 2a 2f 0a  iginal state */.
db850 20 20 20 20 20 20 6c 72 63 20 3d 20 61 66 70 53        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 20 30 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20   0);.    } else 
db8a0 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65  {.      /* if we
db8b0 20 66 61 69 6c 65 64 20 74 6f 20 67 65 74 20 74   failed to get t
db8c0 68 65 20 6c 6f 63 6b 20 74 68 65 6e 20 73 6f 6d  he lock then som
db8d0 65 6f 6e 65 20 65 6c 73 65 20 6d 75 73 74 20 68  eone else must h
db8e0 61 76 65 20 69 74 20 2a 2f 0a 20 20 20 20 20 20  ave it */.      
db8f0 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20  reserved = 1;.  
db900 20 20 7d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c    }.    if( IS_L
db910 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29  OCK_ERROR(lrc) )
db920 7b 0a 20 20 20 20 20 20 72 63 3d 6c 72 63 3b 0a  {.      rc=lrc;.
db930 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 4f      }.  }.  .  O
db940 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52  STRACE4("TEST WR
db950 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e  -LOCK %d %d %d\n
db960 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c  ", pFile->h, rc,
db970 20 72 65 73 65 72 76 65 64 29 3b 0a 20 20 0a 20   reserved);.  . 
db980 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65   *pResOut = rese
db990 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72  rved;.  return r
db9a0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b  c;.}../*.** Lock
db9b0 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74   the file with t
db9c0 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65  he lock specifie
db9d0 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c  d by parameter l
db9e0 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a  ocktype - one.**
db9f0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
dba00 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29  g:.**.**     (1)
dba10 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20   SHARED_LOCK.** 
dba20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44      (2) RESERVED
dba30 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29  _LOCK.**     (3)
dba40 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a   PENDING_LOCK.**
dba50 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49       (4) EXCLUSI
dba60 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f  VE_LOCK.**.** So
dba70 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71  metimes when req
dba80 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b  uesting one lock
dba90 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e   state, addition
dbaa0 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a  al lock states.*
dbab0 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69  * are inserted i
dbac0 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20  n between.  The 
dbad0 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61  locking might fa
dbae0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  il on one of the
dbaf0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69   later.** transi
dbb00 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68  tions leaving th
dbb10 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66  e lock state dif
dbb20 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74  ferent from what
dbb30 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a   it started but.
dbb40 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f  ** still short o
dbb50 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65  f its goal.  The
dbb60 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
dbb70 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77   shows the allow
dbb80 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  ed.** transition
dbb90 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74  s and the insert
dbba0 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ed intermediate 
dbbb0 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  states:.**.**   
dbbc0 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41   UNLOCKED -> SHA
dbbd0 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  RED.**    SHARED
dbbe0 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20   -> RESERVED.** 
dbbf0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45     SHARED -> (PE
dbc00 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
dbc10 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56  IVE.**    RESERV
dbc20 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
dbc30 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
dbc40 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58     PENDING -> EX
dbc50 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68  CLUSIVE.**.** Th
dbc60 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
dbc70 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20  only increase a 
dbc80 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73  lock.  Use the s
dbc90 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29  qlite3OsUnlock()
dbca0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c  .** routine to l
dbcb0 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c  ower a locking l
dbcc0 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  evel..*/.static 
dbcd0 69 6e 74 20 61 66 70 4c 6f 63 6b 28 73 71 6c 69  int afpLock(sqli
dbce0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
dbcf0 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69  t locktype){.  i
dbd00 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
dbd10 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  K;.  unixFile *p
dbd20 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
dbd30 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69  *)id;.  afpLocki
dbd40 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  ngContext *conte
dbd50 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67  xt = (afpLocking
dbd60 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c 65  Context *) pFile
dbd70 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
dbd80 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70  ;.  .  assert( p
dbd90 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
dbda0 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25  E5("LOCK    %d %
dbdb0 73 20 77 61 73 20 25 73 20 70 69 64 3d 25 64 5c  s was %s pid=%d\
dbdc0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
dbdd0 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e         locktypeN
dbde0 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c  ame(locktype), l
dbdf0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c  ocktypeName(pFil
dbe00 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 67 65  e->locktype), ge
dbe10 74 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49  tpid());..  /* I
dbe20 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61  f there is alrea
dbe30 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69  dy a lock of thi
dbe40 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72  s type or more r
dbe50 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68  estrictive on th
dbe60 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c  e.  ** unixFile,
dbe70 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e   do nothing. Don
dbe80 27 74 20 75 73 65 20 74 68 65 20 61 66 70 5f 65  't use the afp_e
dbe90 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61  nd_lock: exit pa
dbea0 74 68 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78  th, as.  ** unix
dbeb0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73  EnterMutex() has
dbec0 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  n't been called 
dbed0 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  yet..  */.  if( 
dbee0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
dbef0 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
dbf00 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20   OSTRACE3("LOCK 
dbf10 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72     %d %s ok (alr
dbf20 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70  eady held)\n", p
dbf30 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
dbf40 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65      locktypeName
dbf50 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20  (locktype));.   
dbf60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
dbf70 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b  K;.  }..  /* Mak
dbf80 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69  e sure the locki
dbf90 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63  ng sequence is c
dbfa0 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73  orrect.  */.  as
dbfb0 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
dbfc0 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c  ktype!=NO_LOCK |
dbfd0 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  | locktype==SHAR
dbfe0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
dbff0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50  ert( locktype!=P
dc000 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20  ENDING_LOCK );. 
dc010 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
dc020 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e!=RESERVED_LOCK
dc030 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   || pFile->lockt
dc040 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
dc050 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73   );.  .  /* This
dc060 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64   mutex is needed
dc070 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e   because pFile->
dc080 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20  pLock is shared 
dc090 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20  across threads. 
dc0a0 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d   */.  unixEnterM
dc0b0 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61  utex();..  /* Ma
dc0c0 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 72 72  ke sure the curr
dc0d0 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20  ent thread owns 
dc0e0 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a  the pFile..  */.
dc0f0 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f    rc = transferO
dc100 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b  wnership(pFile);
dc110 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
dc120 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78  E_OK ){.    unix
dc130 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
dc140 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
dc150 0a 20 20 20 20 0a 20 20 2f 2a 20 41 20 50 45 4e  .    .  /* A PEN
dc160 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65  DING lock is nee
dc170 64 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 69  ded before acqui
dc180 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f  ring a SHARED lo
dc190 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20  ck and before.  
dc1a0 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20  ** acquiring an 
dc1b0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
dc1c0 20 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 20   For the SHARED 
dc1d0 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e  lock, the PENDIN
dc1e0 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72  G will.  ** be r
dc1f0 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20  eleased..  */.  
dc200 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
dc210 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20  ARED_LOCK .     
dc220 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45   || (locktype==E
dc230 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26  XCLUSIVE_LOCK &&
dc240 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dc250 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20  <PENDING_LOCK). 
dc260 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 6c   ){.    int fail
dc270 65 64 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 3d  ed;.    failed =
dc280 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
dc290 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
dc2a0 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  le, PENDING_BYTE
dc2b0 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 20  , 1, 1);.    if 
dc2c0 28 66 61 69 6c 65 64 29 20 7b 0a 20 20 20 20 20  (failed) {.     
dc2d0 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20   rc = failed;.  
dc2e0 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64      goto afp_end
dc2f0 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  _lock;.    }.  }
dc300 0a 20 20 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74  .  .  /* If cont
dc310 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69 73  rol gets to this
dc320 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 74   point, then act
dc330 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 61  ually go ahead a
dc340 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65  nd make.  ** ope
dc350 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61  rating system ca
dc360 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65 63  lls for the spec
dc370 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f  ified lock..  */
dc380 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
dc390 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
dc3a0 20 20 20 20 69 6e 74 20 6c 6b 2c 20 6c 72 63 31      int lk, lrc1
dc3b0 2c 20 6c 72 63 32 3b 0a 20 20 20 20 69 6e 74 20  , lrc2;.    int 
dc3c0 6c 72 63 31 45 72 72 6e 6f 20 3d 20 30 3b 0a 20  lrc1Errno = 0;. 
dc3d0 20 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67     .    /* Now g
dc3e0 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b  et the read-lock
dc3f0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a   SHARED_LOCK */.
dc400 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61 74      /* note that
dc410 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66 20   the quality of 
dc420 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 64  the randomness d
dc430 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 74 68  oesn't matter th
dc440 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20 6c  at much */.    l
dc450 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a 20  k = random(); . 
dc460 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72     context->shar
dc470 65 64 42 79 74 65 20 3d 20 28 6c 6b 20 26 20 30  edByte = (lk & 0
dc480 78 37 66 66 66 66 66 66 66 29 25 28 53 48 41 52  x7fffffff)%(SHAR
dc490 45 44 5f 53 49 5a 45 20 2d 20 31 29 3b 0a 20 20  ED_SIZE - 1);.  
dc4a0 20 20 6c 72 63 31 20 3d 20 61 66 70 53 65 74 4c    lrc1 = afpSetL
dc4b0 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50  ock(context->dbP
dc4c0 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20  ath, pFile, .   
dc4d0 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49         SHARED_FI
dc4e0 52 53 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 61  RST+context->sha
dc4f0 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a  redByte, 1, 1);.
dc500 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
dc510 45 52 52 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20  ERROR(lrc1) ){. 
dc520 20 20 20 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d       lrc1Errno =
dc530 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
dc540 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20  o;.    }.    /* 
dc550 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61  Drop the tempora
dc560 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20  ry PENDING lock 
dc570 2a 2f 0a 20 20 20 20 6c 72 63 32 20 3d 20 61 66  */.    lrc2 = af
dc580 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74  pSetLock(context
dc590 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
dc5a0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31   PENDING_BYTE, 1
dc5b0 2c 20 30 29 3b 0a 20 20 20 20 0a 20 20 20 20 69  , 0);.    .    i
dc5c0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
dc5d0 28 6c 72 63 31 29 20 29 20 7b 0a 20 20 20 20 20  (lrc1) ) {.     
dc5e0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
dc5f0 6f 20 3d 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20  o = lrc1Errno;. 
dc600 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a       rc = lrc1;.
dc610 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65        goto afp_e
dc620 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65  nd_lock;.    } e
dc630 6c 73 65 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f  lse if( IS_LOCK_
dc640 45 52 52 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20  ERROR(lrc2) ){. 
dc650 20 20 20 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a       rc = lrc2;.
dc660 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65        goto afp_e
dc670 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65  nd_lock;.    } e
dc680 6c 73 65 20 69 66 28 20 6c 72 63 31 20 21 3d 20  lse if( lrc1 != 
dc690 53 51 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20  SQLITE_OK ) {.  
dc6a0 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20      rc = lrc1;. 
dc6b0 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
dc6c0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
dc6d0 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b  e = SHARED_LOCK;
dc6e0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f  .      pFile->pO
dc6f0 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20  pen->nLock++;.  
dc700 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
dc710 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 20   /* The request 
dc720 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 52 56  was for a RESERV
dc730 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20  ED or EXCLUSIVE 
dc740 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20  lock.  It is.   
dc750 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74   ** assumed that
dc760 20 74 68 65 72 65 20 69 73 20 61 20 53 48 41 52   there is a SHAR
dc770 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f  ED or greater lo
dc780 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20  ck on the file. 
dc790 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20     ** already.. 
dc7a0 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61     */.    int fa
dc7b0 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 61 73  iled = 0;.    as
dc7c0 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e  sert( 0!=pFile->
dc7d0 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20  locktype );.    
dc7e0 69 66 20 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20  if (locktype >= 
dc7f0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26  RESERVED_LOCK &&
dc800 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dc810 20 3c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b   < RESERVED_LOCK
dc820 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41  ) {.        /* A
dc830 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45  cquire a RESERVE
dc840 44 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20  D lock */.      
dc850 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65    failed = afpSe
dc860 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
dc870 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45  bPath, pFile, RE
dc880 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31  SERVED_BYTE, 1,1
dc890 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20  );.    }.    if 
dc8a0 28 21 66 61 69 6c 65 64 20 26 26 20 6c 6f 63 6b  (!failed && lock
dc8b0 74 79 70 65 20 3d 3d 20 45 58 43 4c 55 53 49 56  type == EXCLUSIV
dc8c0 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20  E_LOCK) {.      
dc8d0 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58  /* Acquire an EX
dc8e0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a  CLUSIVE lock */.
dc8f0 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 2f          .      /
dc900 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 68 61  * Remove the sha
dc910 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65 20  red lock before 
dc920 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e 67 65  trying the range
dc930 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20 74 6f  .  we'll need to
dc940 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65 73 74   .      ** reest
dc950 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 65  ablish the share
dc960 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63 61 6e  d lock if we can
dc970 27 74 20 67 65 74 20 74 68 65 20 20 61 66 70 55  't get the  afpU
dc980 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20  nlock.      */. 
dc990 20 20 20 20 20 69 66 28 20 21 28 66 61 69 6c 65       if( !(faile
dc9a0 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  d = afpSetLock(c
dc9b0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
dc9c0 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49  pFile, SHARED_FI
dc9d0 52 53 54 20 2b 0a 20 20 20 20 20 20 20 20 20 20  RST +.          
dc9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
dc9f0 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79  ontext->sharedBy
dca00 74 65 2c 20 31 2c 20 30 29 29 20 29 7b 0a 20 20  te, 1, 0)) ){.  
dca10 20 20 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64        int failed
dca20 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  2 = SQLITE_OK;. 
dca30 20 20 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74         /* now at
dca40 74 65 6d 6d 70 74 20 74 6f 20 67 65 74 20 74 68  temmpt to get th
dca50 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
dca60 20 72 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20   range */.      
dca70 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65    failed = afpSe
dca80 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
dca90 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48  bPath, pFile, SH
dcaa0 41 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20  ARED_FIRST, .   
dcab0 20 20 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 20 53 48 41 52              SHAR
dcad0 45 44 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20  ED_SIZE, 1);.   
dcae0 20 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20       if( failed 
dcaf0 26 26 20 28 66 61 69 6c 65 64 32 20 3d 20 61 66  && (failed2 = af
dcb00 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74  pSetLock(context
dcb10 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
dcb20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
dcb30 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f           SHARED_
dcb40 46 49 52 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d  FIRST + context-
dcb50 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20  >sharedByte, 1, 
dcb60 31 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  1)) ){.         
dcb70 20 2f 2a 20 43 61 6e 27 74 20 72 65 65 73 74 61   /* Can't reesta
dcb80 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 64  blish the shared
dcb90 20 6c 6f 63 6b 2e 20 20 53 71 6c 69 74 65 20 63   lock.  Sqlite c
dcba0 61 6e 27 74 20 64 65 61 6c 2c 20 74 68 69 73 20  an't deal, this 
dcbb0 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  is.          ** 
dcbc0 61 20 63 72 69 74 69 63 61 6c 20 49 2f 4f 20 65  a critical I/O e
dcbd0 72 72 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a  rror.          *
dcbe0 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  /.          rc =
dcbf0 20 28 28 66 61 69 6c 65 64 20 26 20 53 51 4c 49   ((failed & SQLI
dcc00 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c  TE_IOERR) == SQL
dcc10 49 54 45 5f 49 4f 45 52 52 29 20 3f 20 66 61 69  ITE_IOERR) ? fai
dcc20 6c 65 64 32 20 3a 20 0a 20 20 20 20 20 20 20 20  led2 : .        
dcc30 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f         SQLITE_IO
dcc40 45 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20  ERR_LOCK;.      
dcc50 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64      goto afp_end
dcc60 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d  _lock;.        }
dcc70 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20   .      }else{. 
dcc80 20 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c         rc = fail
dcc90 65 64 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20  ed; .      }.   
dcca0 20 7d 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65   }.    if( faile
dccb0 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  d ){.      rc = 
dccc0 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
dccd0 7d 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53  }.  .  if( rc==S
dcce0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
dccf0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
dcd00 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65  = locktype;.  }e
dcd10 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  lse if( locktype
dcd20 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
dcd30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
dcd40 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e  ocktype = PENDIN
dcd50 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61  G_LOCK;.  }.  .a
dcd60 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75  fp_end_lock:.  u
dcd70 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
dcd80 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43  .  OSTRACE4("LOC
dcd90 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22  K    %d %s %s\n"
dcda0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
dcdb0 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70  typeName(locktyp
dcdc0 65 29 2c 20 0a 20 20 20 20 20 20 20 20 20 72 63  e), .         rc
dcdd0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f  ==SQLITE_OK ? "o
dcde0 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a  k" : "failed");.
dcdf0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
dce00 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20  /*.** Lower the 
dce10 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e  locking level on
dce20 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
dce30 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79   pFile to lockty
dce40 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a  pe.  locktype.**
dce50 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
dce60 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45  NO_LOCK or SHARE
dce70 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  D_LOCK..**.** If
dce80 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
dce90 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
dcea0 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
dceb0 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
dcec0 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65  .** the requeste
dced0 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c  d locking level,
dcee0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
dcef0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61   a no-op..*/.sta
dcf00 74 69 63 20 69 6e 74 20 61 66 70 55 6e 6c 6f 63  tic int afpUnloc
dcf10 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
dcf20 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
dcf30 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ) {.  int rc = S
dcf40 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78  QLITE_OK;.  unix
dcf50 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
dcf60 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61  nixFile*)id;.  a
dcf70 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
dcf80 20 2a 70 43 74 78 20 3d 20 28 61 66 70 4c 6f 63   *pCtx = (afpLoc
dcf90 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70  kingContext *) p
dcfa0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
dcfb0 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  text;..  assert(
dcfc0 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52   pFile );.  OSTR
dcfd0 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64  ACE5("UNLOCK  %d
dcfe0 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25   %d was %d pid=%
dcff0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
dd000 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20  locktype,.      
dd010 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
dd020 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a  pe, getpid());..
dd030 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
dd040 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe<=SHARED_LOCK 
dd050 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  );.  if( pFile->
dd060 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79  locktype<=lockty
dd070 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  pe ){.    return
dd080 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
dd090 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45    if( CHECK_THRE
dd0a0 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20  ADID(pFile) ){. 
dd0b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
dd0c0 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75  _MISUSE;.  }.  u
dd0d0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
dd0e0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
dd0f0 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f  cktype>SHARED_LO
dd100 43 4b 20 29 7b 0a 20 20 20 20 0a 20 20 20 20 69  CK ){.    .    i
dd110 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
dd120 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
dd130 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  CK ){.      rc =
dd140 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78   afpSetLock(pCtx
dd150 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
dd160 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 53   SHARED_FIRST, S
dd170 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a  HARED_SIZE, 0);.
dd180 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
dd190 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74  LITE_OK && lockt
dd1a0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
dd1b0 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f   ){.        /* o
dd1c0 6e 6c 79 20 72 65 2d 65 73 74 61 62 6c 69 73 68  nly re-establish
dd1d0 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b   the shared lock
dd1e0 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f   if necessary */
dd1f0 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 68 61  .        int sha
dd200 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48  redLockByte = SH
dd210 41 52 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d  ARED_FIRST+pCtx-
dd220 3e 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20  >sharedByte;.   
dd230 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74       rc = afpSet
dd240 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74  Lock(pCtx->dbPat
dd250 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 72 65 64  h, pFile, shared
dd260 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b  LockByte, 1, 1);
dd270 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
dd280 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
dd290 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c  E_OK && pFile->l
dd2a0 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47  ocktype>=PENDING
dd2b0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72  _LOCK ){.      r
dd2c0 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70  c = afpSetLock(p
dd2d0 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  Ctx->dbPath, pFi
dd2e0 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  le, PENDING_BYTE
dd2f0 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a  , 1, 0);.    } .
dd300 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
dd310 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e  TE_OK && pFile->
dd320 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56  locktype>=RESERV
dd330 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20  ED_LOCK ){.     
dd340 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b   rc = afpSetLock
dd350 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70  (pCtx->dbPath, p
dd360 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42  File, RESERVED_B
dd370 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  YTE, 1, 0);.    
dd380 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f  }.  }else if( lo
dd390 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20  cktype==NO_LOCK 
dd3a0 29 7b 0a 20 20 20 20 2f 2a 20 63 6c 65 61 72 20  ){.    /* clear 
dd3b0 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  the shared lock 
dd3c0 2a 2f 0a 20 20 20 20 69 6e 74 20 73 68 61 72 65  */.    int share
dd3d0 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52  dLockByte = SHAR
dd3e0 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73  ED_FIRST+pCtx->s
dd3f0 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 72  haredByte;.    r
dd400 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70  c = afpSetLock(p
dd410 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  Ctx->dbPath, pFi
dd420 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79  le, sharedLockBy
dd430 74 65 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a  te, 1, 0);.  }..
dd440 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
dd450 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c  _OK ){.    if( l
dd460 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
dd470 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74   ){.      struct
dd480 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f   unixOpenCnt *pO
dd490 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70  pen = pFile->pOp
dd4a0 65 6e 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d  en;.      pOpen-
dd4b0 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20  >nLock--;.      
dd4c0 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e  assert( pOpen->n
dd4d0 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20  Lock>=0 );.     
dd4e0 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63   if( pOpen->nLoc
dd4f0 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  k==0 ){.        
dd500 72 63 20 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e  rc = closePendin
dd510 67 46 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20  gFds(pFile);.   
dd520 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
dd530 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
dd540 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
dd550 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46  ITE_OK ){.    pF
dd560 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
dd570 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20  locktype;.  }.  
dd580 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
dd590 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65  .** Close a file
dd5a0 20 26 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73   & cleanup AFP s
dd5b0 70 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20  pecific locking 
dd5c0 63 6f 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74  context .*/.stat
dd5d0 69 63 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28  ic int afpClose(
dd5e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
dd5f0 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a  ) {.  if( id ){.
dd600 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46      unixFile *pF
dd610 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
dd620 29 69 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f  )id;.    afpUnlo
dd630 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b  ck(id, NO_LOCK);
dd640 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  .    unixEnterMu
dd650 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70  tex();.    if( p
dd660 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70  File->pOpen && p
dd670 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f  File->pOpen->nLo
dd680 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  ck ){.      /* I
dd690 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73  f there are outs
dd6a0 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64  tanding locks, d
dd6b0 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63  o not actually c
dd6c0 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75  lose the file ju
dd6d0 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20  st.      ** yet 
dd6e0 62 65 63 61 75 73 65 20 74 68 61 74 20 77 6f 75  because that wou
dd6f0 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c  ld clear those l
dd700 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20  ocks.  Instead, 
dd710 61 64 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20  add the file.   
dd720 20 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72     ** descriptor
dd730 20 74 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64   to pOpen->aPend
dd740 69 6e 67 2e 20 20 49 74 20 77 69 6c 6c 20 62 65  ing.  It will be
dd750 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63   automatically c
dd760 6c 6f 73 65 64 20 77 68 65 6e 0a 20 20 20 20 20  losed when.     
dd770 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 6c 6f 63   ** the last loc
dd780 6b 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20  k is cleared..  
dd790 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 65 74      */.      set
dd7a0 50 65 6e 64 69 6e 67 46 64 28 70 46 69 6c 65 29  PendingFd(pFile)
dd7b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65  ;.    }.    rele
dd7c0 61 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65  aseOpenCnt(pFile
dd7d0 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 73 71  ->pOpen);.    sq
dd7e0 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65  lite3_free(pFile
dd7f0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
dd800 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78  );.    closeUnix
dd810 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 6e  File(id);.    un
dd820 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
dd830 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
dd840 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69  ITE_OK;.}..#endi
dd850 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41  f /* defined(__A
dd860 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54  PPLE__) && SQLIT
dd870 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
dd880 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20  _STYLE */./*.** 
dd890 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69  The code above i
dd8a0 73 20 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69  s the AFP lock i
dd8b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20  mplementation.  
dd8c0 54 68 65 20 63 6f 64 65 20 69 73 20 73 70 65 63  The code is spec
dd8d0 69 66 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53  ific.** to MacOS
dd8e0 58 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77  X and does not w
dd8f0 6f 72 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69  ork on other uni
dd900 78 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f  x platforms.  No
dd910 20 61 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20   alternative.** 
dd920 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49  is available.  I
dd930 66 20 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70  f you don't comp
dd940 69 6c 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74  ile for a mac, t
dd950 68 65 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66  hen the "unix-af
dd960 70 22 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74  p".** VFS is not
dd970 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a   available..**.*
dd980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd990 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20  **** End of the 
dd9a0 41 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65  AFP lock impleme
dd9b0 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a  ntation ********
dd9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
dd9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dda00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dda10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
dda20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
dda30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dda40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dda50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dda60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dda70 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
dda80 2a 20 4e 6f 6e 2d 6c 6f 63 6b 69 6e 67 20 73 71  * Non-locking sq
dda90 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f  lite3_file metho
ddaa0 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ds *************
ddab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddac0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  .**.** The next 
ddad0 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e  division contain
ddae0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
ddaf0 73 20 66 6f 72 20 61 6c 6c 20 6d 65 74 68 6f 64  s for all method
ddb00 73 20 6f 66 20 74 68 65 20 0a 2a 2a 20 73 71 6c  s of the .** sql
ddb10 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74  ite3_file object
ddb20 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20   other than the 
ddb30 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 2e  locking methods.
ddb40 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a    The locking.**
ddb50 20 6d 65 74 68 6f 64 73 20 77 65 72 65 20 64 65   methods were de
ddb60 66 69 6e 65 64 20 69 6e 20 64 69 76 69 73 69 6f  fined in divisio
ddb70 6e 73 20 61 62 6f 76 65 20 28 6f 6e 65 20 6c 6f  ns above (one lo
ddb80 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 70 65 72  cking method per
ddb90 0a 2a 2a 20 64 69 76 69 73 69 6f 6e 29 2e 20 20  .** division).  
ddba0 54 68 6f 73 65 20 6d 65 74 68 6f 64 73 20 74 68  Those methods th
ddbb0 61 74 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f  at are common to
ddbc0 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64   all locking mod
ddbd0 65 73 0a 2a 2a 20 61 72 65 20 67 61 74 68 65 72  es.** are gather
ddbe0 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 74   together into t
ddbf0 68 69 73 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f  his division..*/
ddc00 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20  ../*.** Seek to 
ddc10 74 68 65 20 6f 66 66 73 65 74 20 70 61 73 73 65  the offset passe
ddc20 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
ddc30 61 72 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72  argument, then r
ddc40 65 61 64 20 63 6e 74 20 0a 2a 2a 20 62 79 74 65  ead cnt .** byte
ddc50 73 20 69 6e 74 6f 20 70 42 75 66 2e 20 52 65 74  s into pBuf. Ret
ddc60 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
ddc70 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79  f bytes actually
ddc80 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a   read..**.** NB:
ddc90 20 20 49 66 20 79 6f 75 20 64 65 66 69 6e 65 20    If you define 
ddca0 55 53 45 5f 50 52 45 41 44 20 6f 72 20 55 53 45  USE_PREAD or USE
ddcb0 5f 50 52 45 41 44 36 34 2c 20 74 68 65 6e 20 69  _PREAD64, then i
ddcc0 74 20 6d 69 67 68 74 20 61 6c 73 6f 0a 2a 2a 20  t might also.** 
ddcd0 62 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20  be necessary to 
ddce0 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f  define _XOPEN_SO
ddcf0 55 52 43 45 20 74 6f 20 62 65 20 35 30 30 2e 20  URCE to be 500. 
ddd00 20 54 68 69 73 20 76 61 72 69 65 73 20 66 72 6f   This varies fro
ddd10 6d 0a 2a 2a 20 6f 6e 65 20 73 79 73 74 65 6d 20  m.** one system 
ddd20 74 6f 20 61 6e 6f 74 68 65 72 2e 20 20 53 69 6e  to another.  Sin
ddd30 63 65 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e  ce SQLite does n
ddd40 6f 74 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52  ot define USE_PR
ddd50 45 41 44 0a 2a 2a 20 61 6e 79 20 61 6e 79 20 66  EAD.** any any f
ddd60 6f 72 6d 20 62 79 20 64 65 66 61 75 6c 74 2c 20  orm by default, 
ddd70 77 65 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65  we will not atte
ddd80 6d 70 74 20 74 6f 20 64 65 66 69 6e 65 20 5f 58  mpt to define _X
ddd90 4f 50 45 4e 5f 53 4f 55 52 43 45 2e 0a 2a 2a 20  OPEN_SOURCE..** 
ddda0 53 65 65 20 74 69 63 6b 65 74 73 20 23 32 37 34  See tickets #274
dddb0 31 20 61 6e 64 20 23 32 36 38 31 2e 0a 2a 2a 0a  1 and #2681..**.
dddc0 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d  ** To avoid stom
dddd0 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76  ping the errno v
ddde0 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64  alue on a failed
dddf0 20 72 65 61 64 20 74 68 65 20 6c 61 73 74 45 72   read the lastEr
dde00 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  rno value.** is 
dde10 73 65 74 20 62 65 66 6f 72 65 20 72 65 74 75 72  set before retur
dde20 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ning..*/.static 
dde30 69 6e 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28  int seekAndRead(
dde40 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71  unixFile *id, sq
dde50 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
dde60 65 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  et, void *pBuf, 
dde70 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20  int cnt){.  int 
dde80 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66  got;.  i64 newOf
dde90 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54  fset;.  TIMER_ST
ddea0 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64  ART;.#if defined
ddeb0 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f  (USE_PREAD).  go
ddec0 74 20 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c  t = pread(id->h,
dded0 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73   pBuf, cnt, offs
ddee0 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  et);.  SimulateI
ddef0 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31  OError( got = -1
ddf00 20 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65   );.#elif define
ddf10 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20  d(USE_PREAD64). 
ddf20 20 67 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69   got = pread64(i
ddf30 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c  d->h, pBuf, cnt,
ddf40 20 6f 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75   offset);.  Simu
ddf50 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74  lateIOError( got
ddf60 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20   = -1 );.#else. 
ddf70 20 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65   newOffset = lse
ddf80 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74  ek(id->h, offset
ddf90 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53  , SEEK_SET);.  S
ddfa0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
ddfb0 6e 65 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20  newOffset-- );. 
ddfc0 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d   if( newOffset!=
ddfd0 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66  offset ){.    if
ddfe0 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d  ( newOffset == -
ddff0 31 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69  1 ){.      ((uni
de000 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74  xFile*)id)->last
de010 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
de020 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
de030 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
de040 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09  >lastErrno = 0;.
de050 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  ...    }.    ret
de060 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f  urn -1;.  }.  go
de070 74 20 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20  t = read(id->h, 
de080 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64  pBuf, cnt);.#end
de090 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a  if.  TIMER_END;.
de0a0 20 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20    if( got<0 ){. 
de0b0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
de0c0 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  d)->lastErrno = 
de0d0 65 72 72 6e 6f 3b 0a 20 20 7d 0a 20 20 4f 53 54  errno;.  }.  OST
de0e0 52 41 43 45 35 28 22 52 45 41 44 20 20 20 20 25  RACE5("READ    %
de0f0 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c  -3d %5d %7lld %l
de100 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f  lu\n", id->h, go
de110 74 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52  t, offset, TIMER
de120 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74  _ELAPSED);.  ret
de130 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn got;.}../*.*
de140 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d  * Read data from
de150 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62   a file into a b
de160 75 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53  uffer.  Return S
de170 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a  QLITE_OK if all.
de180 2a 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65  ** bytes were re
de190 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  ad successfully 
de1a0 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  and SQLITE_IOERR
de1b0 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65   if anything goe
de1c0 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73  s.** wrong..*/.s
de1d0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52 65  tatic int unixRe
de1e0 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  ad(.  sqlite3_fi
de1f0 6c 65 20 2a 69 64 2c 20 0a 20 20 76 6f 69 64 20  le *id, .  void 
de200 2a 70 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d  *pBuf, .  int am
de210 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  t,.  sqlite3_int
de220 36 34 20 6f 66 66 73 65 74 0a 29 7b 0a 20 20 75  64 offset.){.  u
de230 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
de240 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b   (unixFile *)id;
de250 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 73  .  int got;.  as
de260 73 65 72 74 28 20 69 64 20 29 3b 0a 0a 20 20 2f  sert( id );..  /
de270 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 64  * If this is a d
de280 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f  atabase file (no
de290 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73  t a journal, mas
de2a0 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74  ter-journal or t
de2b0 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20  emp.  ** file), 
de2c0 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 65  the bytes in the
de2d0 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 73   locking range s
de2e0 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72  hould never be r
de2f0 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20  ead or written. 
de300 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  */.  assert( pFi
de310 6c 65 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a 20  le->pUnused==0. 
de320 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e        || offset>
de330 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31  =PENDING_BYTE+51
de340 32 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73  2.       || offs
de350 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f  et+amt<=PENDING_
de360 42 59 54 45 20 0a 20 20 29 3b 0a 0a 20 20 67 6f  BYTE .  );..  go
de370 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28  t = seekAndRead(
de380 70 46 69 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70  pFile, offset, p
de390 42 75 66 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28  Buf, amt);.  if(
de3a0 20 67 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20   got==amt ){.   
de3b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
de3c0 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67  K;.  }else if( g
de3d0 6f 74 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c  ot<0 ){.    /* l
de3e0 61 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20  astErrno set by 
de3f0 73 65 65 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20  seekAndRead */. 
de400 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
de410 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d  _IOERR_READ;.  }
de420 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d  else{.    pFile-
de430 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20  >lastErrno = 0; 
de440 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20  /* not a system 
de450 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20  error */.    /* 
de460 55 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20  Unread parts of 
de470 74 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20  the buffer must 
de480 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a  be zero-filled *
de490 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28  /.    memset(&((
de4a0 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d  char*)pBuf)[got]
de4b0 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20  , 0, amt-got);. 
de4c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
de4d0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
de4e0 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  D;.  }.}../*.** 
de4f0 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73  Seek to the offs
de500 65 74 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74  et in id->offset
de510 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62   then read cnt b
de520 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a  ytes into pBuf..
de530 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
de540 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63  mber of bytes ac
de550 74 75 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70  tually read.  Up
de560 64 61 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e  date the offset.
de570 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20  .**.** To avoid 
de580 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72  stomping the err
de590 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61  no value on a fa
de5a0 69 6c 65 64 20 77 72 69 74 65 20 74 68 65 20 6c  iled write the l
de5b0 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a  astErrno value.*
de5c0 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20  * is set before 
de5d0 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74  returning..*/.st
de5e0 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64  atic int seekAnd
de5f0 57 72 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a  Write(unixFile *
de600 69 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20  id, i64 offset, 
de610 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66  const void *pBuf
de620 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e  , int cnt){.  in
de630 74 20 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77  t got;.  i64 new
de640 4f 66 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f  Offset;.  TIMER_
de650 53 54 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e  START;.#if defin
de660 65 64 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20  ed(USE_PREAD).  
de670 67 6f 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d  got = pwrite(id-
de680 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f  >h, pBuf, cnt, o
de690 66 66 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65  ffset);.#elif de
de6a0 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36  fined(USE_PREAD6
de6b0 34 29 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74  4).  got = pwrit
de6c0 65 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c  e64(id->h, pBuf,
de6d0 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23   cnt, offset);.#
de6e0 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74  else.  newOffset
de6f0 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20   = lseek(id->h, 
de700 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54  offset, SEEK_SET
de710 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73  );.  if( newOffs
de720 65 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20  et!=offset ){.  
de730 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 20    if( newOffset 
de740 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 28  == -1 ){.      (
de750 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
de760 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
de770 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  o;.    }else{.  
de780 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
de790 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  id)->lastErrno =
de7a0 20 30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 20   0;....    }.   
de7b0 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a   return -1;.  }.
de7c0 20 20 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64    got = write(id
de7d0 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b  ->h, pBuf, cnt);
de7e0 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f  .#endif.  TIMER_
de7f0 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30  END;.  if( got<0
de800 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69   ){.    ((unixFi
de810 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72  le*)id)->lastErr
de820 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a  no = errno;.  }.
de830 0a 20 20 4f 53 54 52 41 43 45 35 28 22 57 52 49  .  OSTRACE5("WRI
de840 54 45 20 20 20 25 2d 33 64 20 25 35 64 20 25 37  TE   %-3d %5d %7
de850 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d  lld %llu\n", id-
de860 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c  >h, got, offset,
de870 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b   TIMER_ELAPSED);
de880 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d  .  return got;.}
de890 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64  .../*.** Write d
de8a0 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65  ata from a buffe
de8b0 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20  r into a file.  
de8c0 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
de8d0 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f   on success.** o
de8e0 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72  r some other err
de8f0 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75  or code on failu
de900 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  re..*/.static in
de910 74 20 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73  t unixWrite(.  s
de920 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
de930 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
de940 70 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74  pBuf, .  int amt
de950 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ,.  sqlite3_int6
de960 34 20 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 75  4 offset .){.  u
de970 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
de980 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
de990 20 20 69 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b    int wrote = 0;
de9a0 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b  .  assert( id );
de9b0 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30  .  assert( amt>0
de9c0 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69   );..  /* If thi
de9d0 73 20 69 73 20 61 20 64 61 74 61 62 61 73 65 20  s is a database 
de9e0 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72  file (not a jour
de9f0 6e 61 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72  nal, master-jour
dea00 6e 61 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a 2a  nal or temp.  **
dea10 20 66 69 6c 65 29 2c 20 74 68 65 20 62 79 74 65   file), the byte
dea20 73 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67  s in the locking
dea30 20 72 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65   range should ne
dea40 76 65 72 20 62 65 20 72 65 61 64 20 6f 72 20 77  ver be read or w
dea50 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 73  ritten. */.  ass
dea60 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75  ert( pFile->pUnu
dea70 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c  sed==0.       ||
dea80 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47   offset>=PENDING
dea90 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20  _BYTE+512.      
deaa0 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d   || offset+amt<=
deab0 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20  PENDING_BYTE .  
deac0 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  );..#ifndef NDEB
dead0 55 47 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72  UG.  /* If we ar
deae0 65 20 64 6f 69 6e 67 20 61 20 6e 6f 72 6d 61 6c  e doing a normal
deaf0 20 77 72 69 74 65 20 74 6f 20 61 20 64 61 74 61   write to a data
deb00 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 6f 70  base file (as op
deb10 70 6f 73 65 64 20 74 6f 0a 20 20 2a 2a 20 64 6f  posed to.  ** do
deb20 69 6e 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  ing a hot-journa
deb30 6c 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20  l rollback or a 
deb40 77 72 69 74 65 20 74 6f 20 73 6f 6d 65 20 66 69  write to some fi
deb50 6c 65 20 6f 74 68 65 72 20 74 68 61 6e 20 61 0a  le other than a.
deb60 20 20 2a 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61    ** normal data
deb70 62 61 73 65 20 66 69 6c 65 29 20 74 68 65 6e 20  base file) then 
deb80 72 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20  record the fact 
deb90 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73  that the databas
deba0 65 0a 20 20 2a 2a 20 68 61 73 20 63 68 61 6e 67  e.  ** has chang
debb0 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e  ed.  If the tran
debc0 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20  saction counter 
debd0 69 73 20 6d 6f 64 69 66 69 65 64 2c 20 72 65 63  is modified, rec
debe0 6f 72 64 20 74 68 61 74 0a 20 20 2a 2a 20 66 61  ord that.  ** fa
debf0 63 74 20 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69  ct too..  */.  i
dec00 66 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d  f( pFile->inNorm
dec10 61 6c 57 72 69 74 65 20 29 7b 0a 20 20 20 20 70  alWrite ){.    p
dec20 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d  File->dbUpdate =
dec30 20 31 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61   1;  /* The data
dec40 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 6d 6f  base has been mo
dec50 64 69 66 69 65 64 20 2a 2f 0a 20 20 20 20 69 66  dified */.    if
dec60 28 20 6f 66 66 73 65 74 3c 3d 32 34 20 26 26 20  ( offset<=24 && 
dec70 6f 66 66 73 65 74 2b 61 6d 74 3e 3d 32 37 20 29  offset+amt>=27 )
dec80 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a  {.      int rc;.
dec90 20 20 20 20 20 20 63 68 61 72 20 6f 6c 64 43 6e        char oldCn
deca0 74 72 5b 34 5d 3b 0a 20 20 20 20 20 20 53 69 6d  tr[4];.      Sim
decb0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
decc0 67 6e 28 31 29 3b 0a 20 20 20 20 20 20 72 63 20  gn(1);.      rc 
decd0 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46  = seekAndRead(pF
dece0 69 6c 65 2c 20 32 34 2c 20 6f 6c 64 43 6e 74 72  ile, 24, oldCntr
decf0 2c 20 34 29 3b 0a 20 20 20 20 20 20 53 69 6d 75  , 4);.      Simu
ded00 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
ded10 6e 28 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  n(0);.      if( 
ded20 72 63 21 3d 34 20 7c 7c 20 6d 65 6d 63 6d 70 28  rc!=4 || memcmp(
ded30 6f 6c 64 43 6e 74 72 2c 20 26 28 28 63 68 61 72  oldCntr, &((char
ded40 2a 29 70 42 75 66 29 5b 32 34 2d 6f 66 66 73 65  *)pBuf)[24-offse
ded50 74 5d 2c 20 34 29 21 3d 30 20 29 7b 0a 20 20 20  t], 4)!=0 ){.   
ded60 20 20 20 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e       pFile->tran
ded70 73 43 6e 74 72 43 68 6e 67 20 3d 20 31 3b 20 20  sCntrChng = 1;  
ded80 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69  /* The transacti
ded90 6f 6e 20 63 6f 75 6e 74 65 72 20 68 61 73 20 63  on counter has c
deda0 68 61 6e 67 65 64 20 2a 2f 0a 20 20 20 20 20 20  hanged */.      
dedb0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  }.    }.  }.#end
dedc0 69 66 0a 0a 20 20 77 68 69 6c 65 28 20 61 6d 74  if..  while( amt
dedd0 3e 30 20 26 26 20 28 77 72 6f 74 65 20 3d 20 73  >0 && (wrote = s
dede0 65 65 6b 41 6e 64 57 72 69 74 65 28 70 46 69 6c  eekAndWrite(pFil
dedf0 65 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c  e, offset, pBuf,
dee00 20 61 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20   amt))>0 ){.    
dee10 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20  amt -= wrote;.  
dee20 20 20 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74    offset += wrot
dee30 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28  e;.    pBuf = &(
dee40 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f  (char*)pBuf)[wro
dee50 74 65 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c  te];.  }.  Simul
dee60 61 74 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f  ateIOError(( wro
dee70 74 65 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29  te=(-1), amt=1 )
dee80 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73  );.  SimulateDis
dee90 6b 66 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f  kfullError(( wro
deea0 74 65 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a  te=0, amt=1 ));.
deeb0 20 20 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20    if( amt>0 ){. 
deec0 20 20 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29     if( wrote<0 )
deed0 7b 0a 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45  {.      /* lastE
deee0 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b  rrno set by seek
deef0 41 6e 64 57 72 69 74 65 20 2a 2f 0a 20 20 20 20  AndWrite */.    
def00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
def10 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20  IOERR_WRITE;.   
def20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46   }else{.      pF
def30 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
def40 20 30 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73   0; /* not a sys
def50 74 65 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20  tem error */.   
def60 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
def70 5f 46 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d  _FULL;.    }.  }
def80 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
def90 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53  _OK;.}..#ifdef S
defa0 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  QLITE_TEST./*.**
defb0 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65   Count the numbe
defc0 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61  r of fullsyncs a
defd0 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e  nd normal syncs.
defe0 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74    This is used t
deff0 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73  o test.** that s
df000 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e  yncs and fullsyn
df010 63 73 20 61 72 65 20 6f 63 63 75 72 72 69 6e 67  cs are occurring
df020 20 61 74 20 74 68 65 20 72 69 67 68 74 20 74 69   at the right ti
df030 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  mes..*/.SQLITE_A
df040 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
df050 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53  ync_count = 0;.S
df060 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
df070 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63  lite3_fullsync_c
df080 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
df090 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 64 6f 20 6e 6f  ../*.** We do no
df0a0 74 20 74 72 75 73 74 20 73 79 73 74 65 6d 73 20  t trust systems 
df0b0 74 6f 20 70 72 6f 76 69 64 65 20 61 20 77 6f 72  to provide a wor
df0c0 6b 69 6e 67 20 66 64 61 74 61 73 79 6e 63 28 29  king fdatasync()
df0d0 2e 20 20 53 6f 6d 65 20 64 6f 2e 0a 2a 2a 20 4f  .  Some do..** O
df0e0 74 68 65 72 73 20 64 6f 20 6e 6f 2e 20 20 54 6f  thers do no.  To
df0f0 20 62 65 20 73 61 66 65 2c 20 77 65 20 77 69 6c   be safe, we wil
df100 6c 20 73 74 69 63 6b 20 77 69 74 68 20 74 68 65  l stick with the
df110 20 28 73 6c 6f 77 65 72 29 20 66 73 79 6e 63 28   (slower) fsync(
df120 29 2e 0a 2a 2a 20 49 66 20 79 6f 75 20 6b 6e 6f  )..** If you kno
df130 77 20 74 68 61 74 20 79 6f 75 72 20 73 79 73 74  w that your syst
df140 65 6d 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20  em does support 
df150 66 64 61 74 61 73 79 6e 63 28 29 20 63 6f 72 72  fdatasync() corr
df160 65 63 74 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 73  ectly,.** then s
df170 69 6d 70 6c 79 20 63 6f 6d 70 69 6c 65 20 77 69  imply compile wi
df180 74 68 20 2d 44 66 64 61 74 61 73 79 6e 63 3d 66  th -Dfdatasync=f
df190 64 61 74 61 73 79 6e 63 0a 2a 2f 0a 23 69 66 20  datasync.*/.#if 
df1a0 21 64 65 66 69 6e 65 64 28 66 64 61 74 61 73 79  !defined(fdatasy
df1b0 6e 63 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  nc) && !defined(
df1c0 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66  __linux__).# def
df1d0 69 6e 65 20 66 64 61 74 61 73 79 6e 63 20 66 73  ine fdatasync fs
df1e0 79 6e 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ync.#endif../*.*
df1f0 2a 20 44 65 66 69 6e 65 20 48 41 56 45 5f 46 55  * Define HAVE_FU
df200 4c 4c 46 53 59 4e 43 20 74 6f 20 30 20 6f 72 20  LLFSYNC to 0 or 
df210 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77  1 depending on w
df220 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a  hether or not.**
df230 20 74 68 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43   the F_FULLFSYNC
df240 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65   macro is define
df250 64 2e 20 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20  d.  F_FULLFSYNC 
df260 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  is currently.** 
df270 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f  only available o
df280 6e 20 4d 61 63 20 4f 53 20 58 2e 20 20 42 75 74  n Mac OS X.  But
df290 20 74 68 61 74 20 63 6f 75 6c 64 20 63 68 61 6e   that could chan
df2a0 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 46 5f  ge..*/.#ifdef F_
df2b0 46 55 4c 4c 46 53 59 4e 43 0a 23 20 64 65 66 69  FULLFSYNC.# defi
df2c0 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e  ne HAVE_FULLFSYN
df2d0 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  C 1.#else.# defi
df2e0 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e  ne HAVE_FULLFSYN
df2f0 43 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  C 0.#endif.../*.
df300 2a 2a 20 54 68 65 20 66 73 79 6e 63 28 29 20 73  ** The fsync() s
df310 79 73 74 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20  ystem call does 
df320 6e 6f 74 20 77 6f 72 6b 20 61 73 20 61 64 76 65  not work as adve
df330 72 74 69 73 65 64 20 6f 6e 20 6d 61 6e 79 0a 2a  rtised on many.*
df340 2a 20 75 6e 69 78 20 73 79 73 74 65 6d 73 2e 20  * unix systems. 
df350 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70   The following p
df360 72 6f 63 65 64 75 72 65 20 69 73 20 61 6e 20 61  rocedure is an a
df370 74 74 65 6d 70 74 20 74 6f 20 6d 61 6b 65 0a 2a  ttempt to make.*
df380 2a 20 69 74 20 77 6f 72 6b 20 62 65 74 74 65 72  * it work better
df390 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ..**.** The SQLI
df3a0 54 45 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 6f  TE_NO_SYNC macro
df3b0 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 66 73   disables all fs
df3c0 79 6e 63 28 29 73 2e 20 20 54 68 69 73 20 69 73  ync()s.  This is
df3d0 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74   useful.** for t
df3e0 65 73 74 69 6e 67 20 77 68 65 6e 20 77 65 20 77  esting when we w
df3f0 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 72 6f 75  ant to run throu
df400 67 68 20 74 68 65 20 74 65 73 74 20 73 75 69 74  gh the test suit
df410 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 6f  e quickly..** Yo
df420 75 20 61 72 65 20 73 74 72 6f 6e 67 6c 79 20 61  u are strongly a
df430 64 76 69 73 65 64 20 2a 6e 6f 74 2a 20 74 6f 20  dvised *not* to 
df440 64 65 70 6c 6f 79 20 77 69 74 68 20 53 51 4c 49  deploy with SQLI
df450 54 45 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 6e  TE_NO_SYNC.** en
df460 61 62 6c 65 64 2c 20 68 6f 77 65 76 65 72 2c 20  abled, however, 
df470 73 69 6e 63 65 20 77 69 74 68 20 53 51 4c 49 54  since with SQLIT
df480 45 5f 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 6c 65  E_NO_SYNC enable
df490 64 2c 20 61 6e 20 4f 53 20 63 72 61 73 68 0a 2a  d, an OS crash.*
df4a0 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c 75  * or power failu
df4b0 72 65 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63  re will likely c
df4c0 6f 72 72 75 70 74 20 74 68 65 20 64 61 74 61 62  orrupt the datab
df4d0 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
df4e0 53 51 4c 69 74 65 20 73 65 74 73 20 74 68 65 20  SQLite sets the 
df4f0 64 61 74 61 4f 6e 6c 79 20 66 6c 61 67 20 69 66  dataOnly flag if
df500 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
df510 20 66 69 6c 65 20 69 73 20 75 6e 63 68 61 6e 67   file is unchang
df520 65 64 2e 0a 2a 2a 20 54 68 65 20 69 64 65 61 20  ed..** The idea 
df530 62 65 68 69 6e 64 20 64 61 74 61 4f 6e 6c 79 20  behind dataOnly 
df540 69 73 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c  is that it shoul
df550 64 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 65  d only write the
df560 20 66 69 6c 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a   file content.**
df570 20 74 6f 20 64 69 73 6b 2c 20 6e 6f 74 20 74 68   to disk, not th
df580 65 20 69 6e 6f 64 65 2e 20 20 57 65 20 6f 6e 6c  e inode.  We onl
df590 79 20 73 65 74 20 64 61 74 61 4f 6e 6c 79 20 69  y set dataOnly i
df5a0 66 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20  f the file size 
df5b0 69 73 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64  is .** unchanged
df5c0 20 73 69 6e 63 65 20 74 68 65 20 66 69 6c 65 20   since the file 
df5d0 73 69 7a 65 20 69 73 20 70 61 72 74 20 6f 66 20  size is part of 
df5e0 74 68 65 20 69 6e 6f 64 65 2e 20 20 48 6f 77 65  the inode.  Howe
df5f0 76 65 72 2c 20 0a 2a 2a 20 54 65 64 20 54 73 27  ver, .** Ted Ts'
df600 6f 20 74 65 6c 6c 73 20 75 73 20 74 68 61 74 20  o tells us that 
df610 66 64 61 74 61 73 79 6e 63 28 29 20 77 69 6c 6c  fdatasync() will
df620 20 61 6c 73 6f 20 77 72 69 74 65 20 74 68 65 20   also write the 
df630 69 6e 6f 64 65 20 69 66 20 74 68 65 0a 2a 2a 20  inode if the.** 
df640 66 69 6c 65 20 73 69 7a 65 20 68 61 73 20 63 68  file size has ch
df650 61 6e 67 65 64 2e 20 20 54 68 65 20 6f 6e 6c 79  anged.  The only
df660 20 72 65 61 6c 20 64 69 66 66 65 72 65 6e 63 65   real difference
df670 20 62 65 74 77 65 65 6e 20 66 64 61 74 61 73 79   between fdatasy
df680 6e 63 28 29 0a 2a 2a 20 61 6e 64 20 66 73 79 6e  nc().** and fsyn
df690 63 28 29 2c 20 54 65 64 20 74 65 6c 6c 73 20 75  c(), Ted tells u
df6a0 73 2c 20 69 73 20 74 68 61 74 20 66 64 61 74 61  s, is that fdata
df6b0 73 79 6e 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20  sync() will not 
df6c0 66 6c 75 73 68 20 74 68 65 0a 2a 2a 20 69 6e 6f  flush the.** ino
df6d0 64 65 20 69 66 20 74 68 65 20 6d 74 69 6d 65 20  de if the mtime 
df6e0 6f 72 20 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65  or owner or othe
df6f0 72 20 69 6e 6f 64 65 20 61 74 74 72 69 62 75 74  r inode attribut
df700 65 73 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e  es have changed.
df710 0a 2a 2a 20 57 65 20 6f 6e 6c 79 20 63 61 72 65  .** We only care
df720 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20   about the file 
df730 73 69 7a 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74  size, not the ot
df740 68 65 72 20 66 69 6c 65 20 61 74 74 72 69 62 75  her file attribu
df750 74 65 73 2c 20 73 6f 0a 2a 2a 20 61 73 20 66 61  tes, so.** as fa
df760 72 20 61 73 20 53 51 4c 69 74 65 20 69 73 20 63  r as SQLite is c
df770 6f 6e 63 65 72 6e 65 64 2c 20 61 6e 20 66 64 61  oncerned, an fda
df780 74 61 73 79 6e 63 28 29 20 69 73 20 61 6c 77 61  tasync() is alwa
df790 79 73 20 61 64 65 71 75 61 74 65 2e 0a 2a 2a 20  ys adequate..** 
df7a0 53 6f 2c 20 77 65 20 61 6c 77 61 79 73 20 75 73  So, we always us
df7b0 65 20 66 64 61 74 61 73 79 6e 63 28 29 20 69 66  e fdatasync() if
df7c0 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65   it is available
df7d0 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 0a  , regardless of.
df7e0 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
df7f0 74 68 65 20 64 61 74 61 4f 6e 6c 79 20 66 6c 61  the dataOnly fla
df800 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
df810 20 66 75 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20   full_fsync(int 
df820 66 64 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63  fd, int fullSync
df830 2c 20 69 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b  , int dataOnly){
df840 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a  .  int rc;..  /*
df850 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 22   The following "
df860 69 66 64 65 66 2f 65 6c 69 66 2f 65 6c 73 65 2f  ifdef/elif/else/
df870 22 20 62 6c 6f 63 6b 20 68 61 73 20 74 68 65 20  " block has the 
df880 73 61 6d 65 20 73 74 72 75 63 74 75 72 65 20 61  same structure a
df890 73 0a 20 20 2a 2a 20 74 68 65 20 6f 6e 65 20 62  s.  ** the one b
df8a0 65 6c 6f 77 2e 20 49 74 20 69 73 20 72 65 70 6c  elow. It is repl
df8b0 69 63 61 74 65 64 20 68 65 72 65 20 73 6f 6c 65  icated here sole
df8c0 6c 79 20 74 6f 20 61 76 6f 69 64 20 63 6c 75 74  ly to avoid clut
df8d0 74 65 72 69 6e 67 20 0a 20 20 2a 2a 20 75 70 20  tering .  ** up 
df8e0 74 68 65 20 72 65 61 6c 20 63 6f 64 65 20 77 69  the real code wi
df8f0 74 68 20 74 68 65 20 55 4e 55 53 45 44 5f 50 41  th the UNUSED_PA
df900 52 41 4d 45 54 45 52 28 29 20 6d 61 63 72 6f 73  RAMETER() macros
df910 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  ..  */.#ifdef SQ
df920 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55  LITE_NO_SYNC.  U
df930 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
df940 66 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  fd);.  UNUSED_PA
df950 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63  RAMETER(fullSync
df960 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
df970 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b  METER(dataOnly);
df980 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c  .#elif HAVE_FULL
df990 46 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50  FSYNC.  UNUSED_P
df9a0 41 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c  ARAMETER(dataOnl
df9b0 79 29 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53  y);.#else.  UNUS
df9c0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 75 6c  ED_PARAMETER(ful
df9d0 6c 53 79 6e 63 29 3b 0a 20 20 55 4e 55 53 45 44  lSync);.  UNUSED
df9e0 5f 50 41 52 41 4d 45 54 45 52 28 64 61 74 61 4f  _PARAMETER(dataO
df9f0 6e 6c 79 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  nly);.#endif..  
dfa00 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20 6e 75  /* Record the nu
dfa10 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
dfa20 61 74 20 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61  at we do a norma
dfa30 6c 20 66 73 79 6e 63 28 29 20 61 6e 64 20 0a 20  l fsync() and . 
dfa40 20 2a 2a 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54   ** FULLSYNC.  T
dfa50 68 69 73 20 69 73 20 75 73 65 64 20 64 75 72 69  his is used duri
dfa60 6e 67 20 74 65 73 74 69 6e 67 20 74 6f 20 76 65  ng testing to ve
dfa70 72 69 66 79 20 74 68 61 74 20 74 68 69 73 20 70  rify that this p
dfa80 72 6f 63 65 64 75 72 65 0a 20 20 2a 2a 20 67 65  rocedure.  ** ge
dfa90 74 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74  ts called with t
dfaa0 68 65 20 63 6f 72 72 65 63 74 20 61 72 67 75 6d  he correct argum
dfab0 65 6e 74 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65  ents..  */.#ifde
dfac0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
dfad0 69 66 28 20 66 75 6c 6c 53 79 6e 63 20 29 20 73  if( fullSync ) s
dfae0 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f  qlite3_fullsync_
dfaf0 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74  count++;.  sqlit
dfb00 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b  e3_sync_count++;
dfb10 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
dfb20 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   we compiled wit
dfb30 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f  h the SQLITE_NO_
dfb40 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20  SYNC flag, then 
dfb50 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a  syncing is a.  *
dfb60 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66  * no-op.  */.#if
dfb70 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  def SQLITE_NO_SY
dfb80 4e 43 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  NC.  rc = SQLITE
dfb90 5f 4f 4b 3b 0a 23 65 6c 69 66 20 48 41 56 45 5f  _OK;.#elif HAVE_
dfba0 46 55 4c 4c 46 53 59 4e 43 0a 20 20 69 66 28 20  FULLFSYNC.  if( 
dfbb0 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20  fullSync ){.    
dfbc0 72 63 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46  rc = fcntl(fd, F
dfbd0 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a  _FULLFSYNC, 0);.
dfbe0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
dfbf0 3d 20 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66  = 1;.  }.  /* If
dfc00 20 74 68 65 20 46 55 4c 4c 46 53 59 4e 43 20 66   the FULLFSYNC f
dfc10 61 69 6c 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b  ailed, fall back
dfc20 20 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 61   to attempting a
dfc30 6e 20 66 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20  n fsync()..  ** 
dfc40 49 74 20 73 68 6f 75 6c 64 6e 27 74 20 62 65 20  It shouldn't be 
dfc50 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 66 75 6c  possible for ful
dfc60 6c 66 73 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f  lfsync to fail o
dfc70 6e 20 74 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a  n the local .  *
dfc80 2a 20 66 69 6c 65 20 73 79 73 74 65 6d 20 28 6f  * file system (o
dfc90 6e 20 4f 53 58 29 2c 20 73 6f 20 66 61 69 6c 75  n OSX), so failu
dfca0 72 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  re indicates tha
dfcb0 74 20 46 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a  t FULLFSYNC.  **
dfcc0 20 69 73 6e 27 74 20 73 75 70 70 6f 72 74 65 64   isn't supported
dfcd0 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 73   for this file s
dfce0 79 73 74 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d  ystem. So, attem
dfcf0 70 74 20 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a  pt an fsync .  *
dfd00 2a 20 61 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20  * and (for now) 
dfd10 69 67 6e 6f 72 65 20 74 68 65 20 6f 76 65 72 68  ignore the overh
dfd20 65 61 64 20 6f 66 20 61 20 73 75 70 65 72 66 6c  ead of a superfl
dfd30 75 6f 75 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e  uous fcntl call.
dfd40 20 20 0a 20 20 2a 2a 20 49 74 27 64 20 62 65 20    .  ** It'd be 
dfd50 62 65 74 74 65 72 20 74 6f 20 64 65 74 65 63 74  better to detect
dfd60 20 66 75 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f   fullfsync suppo
dfd70 72 74 20 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69  rt once and avoi
dfd80 64 20 0a 20 20 2a 2a 20 74 68 65 20 66 63 6e 74  d .  ** the fcnt
dfd90 6c 20 63 61 6c 6c 20 65 76 65 72 79 20 74 69 6d  l call every tim
dfda0 65 20 73 79 6e 63 20 69 73 20 63 61 6c 6c 65 64  e sync is called
dfdb0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 20  ..  */.  if( rc 
dfdc0 29 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29  ) rc = fsync(fd)
dfdd0 3b 0a 0a 23 65 6c 73 65 20 0a 20 20 72 63 20 3d  ;..#else .  rc =
dfde0 20 66 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a   fdatasync(fd);.
dfdf0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
dfe00 20 69 66 28 20 72 63 3d 3d 2d 31 20 26 26 20 65   if( rc==-1 && e
dfe10 72 72 6e 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b  rrno==ENOTSUP ){
dfe20 0a 20 20 20 20 72 63 20 3d 20 66 73 79 6e 63 28  .    rc = fsync(
dfe30 66 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  fd);.  }.#endif 
dfe40 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f  /* OS_VXWORKS */
dfe50 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 64 65 66  .#endif /* ifdef
dfe60 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20   SQLITE_NO_SYNC 
dfe70 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53  elif HAVE_FULLFS
dfe80 59 4e 43 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53  YNC */..  if( OS
dfe90 5f 56 58 57 4f 52 4b 53 20 26 26 20 72 63 21 3d  _VXWORKS && rc!=
dfea0 20 2d 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20   -1 ){.    rc = 
dfeb0 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
dfec0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  rc;.}../*.** Mak
dfed0 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65  e sure all write
dfee0 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61  s to a particula
dfef0 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69  r file are commi
dff00 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a  tted to disk..**
dff10 0a 2a 2a 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d  .** If dataOnly=
dff20 3d 30 20 74 68 65 6e 20 62 6f 74 68 20 74 68 65  =0 then both the
dff30 20 66 69 6c 65 20 69 74 73 65 6c 66 20 61 6e 64   file itself and
dff40 20 69 74 73 20 6d 65 74 61 64 61 74 61 20 28 66   its metadata (f
dff50 69 6c 65 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63  ile.** size, acc
dff60 65 73 73 20 74 69 6d 65 2c 20 65 74 63 29 20 61  ess time, etc) a
dff70 72 65 20 73 79 6e 63 65 64 2e 20 20 49 66 20 64  re synced.  If d
dff80 61 74 61 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20  ataOnly!=0 then 
dff90 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65  only the.** file
dffa0 20 64 61 74 61 20 69 73 20 73 79 6e 63 65 64 2e   data is synced.
dffb0 0a 2a 2a 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69  .**.** Under Uni
dffc0 78 2c 20 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72  x, also make sur
dffd0 65 20 74 68 61 74 20 74 68 65 20 64 69 72 65 63  e that the direc
dffe0 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74  tory entry for t
dfff0 68 65 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62  he file.** has b
e0000 65 65 6e 20 63 72 65 61 74 65 64 20 62 79 20 66  een created by f
e0010 73 79 6e 63 2d 69 6e 67 20 74 68 65 20 64 69 72  sync-ing the dir
e0020 65 63 74 6f 72 79 20 74 68 61 74 20 63 6f 6e 74  ectory that cont
e0030 61 69 6e 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a  ains the file..*
e0040 2a 20 49 66 20 77 65 20 64 6f 20 6e 6f 74 20 64  * If we do not d
e0050 6f 20 74 68 69 73 20 61 6e 64 20 77 65 20 65 6e  o this and we en
e0060 63 6f 75 6e 74 65 72 20 61 20 70 6f 77 65 72 20  counter a power 
e0070 66 61 69 6c 75 72 65 2c 20 74 68 65 20 64 69 72  failure, the dir
e0080 65 63 74 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20  ectory.** entry 
e0090 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  for the journal 
e00a0 6d 69 67 68 74 20 6e 6f 74 20 65 78 69 73 74 20  might not exist 
e00b0 61 66 74 65 72 20 77 65 20 72 65 62 6f 6f 74 2e  after we reboot.
e00c0 20 20 54 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51    The next.** SQ
e00d0 4c 69 74 65 20 74 6f 20 61 63 63 65 73 73 20 74  Lite to access t
e00e0 68 65 20 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74  he file will not
e00f0 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6a   know that the j
e0100 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 20 28 62  ournal exists (b
e0110 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 64 69  ecause.** the di
e0120 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 66 6f  rectory entry fo
e0130 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61  r the journal wa
e0140 73 20 6e 65 76 65 72 20 63 72 65 61 74 65 64 29  s never created)
e0150 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63   and the transac
e0160 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74  tion.** will not
e0170 20 72 6f 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73   roll back - pos
e0180 73 69 62 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f  sibly leading to
e0190 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
e01a0 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
e01b0 69 6e 74 20 75 6e 69 78 53 79 6e 63 28 73 71 6c  int unixSync(sql
e01c0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
e01d0 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74  nt flags){.  int
e01e0 20 72 63 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20   rc;.  unixFile 
e01f0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
e0200 6c 65 2a 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69  le*)id;..  int i
e0210 73 44 61 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61  sDataOnly = (fla
e0220 67 73 26 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44  gs&SQLITE_SYNC_D
e0230 41 54 41 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20  ATAONLY);.  int 
e0240 69 73 46 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c  isFullsync = (fl
e0250 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54  ags&0x0F)==SQLIT
e0260 45 5f 53 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20  E_SYNC_FULL;..  
e0270 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 6f 6e  /* Check that on
e0280 65 20 6f 66 20 53 51 4c 49 54 45 5f 53 59 4e 43  e of SQLITE_SYNC
e0290 5f 4e 4f 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20  _NORMAL or FULL 
e02a0 77 61 73 20 70 61 73 73 65 64 20 2a 2f 0a 20 20  was passed */.  
e02b0 61 73 73 65 72 74 28 28 66 6c 61 67 73 26 30 78  assert((flags&0x
e02c0 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43  0F)==SQLITE_SYNC
e02d0 5f 4e 4f 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c  _NORMAL.      ||
e02e0 20 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53   (flags&0x0F)==S
e02f0 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a  QLITE_SYNC_FULL.
e0300 20 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20    );..  /* Unix 
e0310 63 61 6e 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65  cannot, but some
e0320 20 73 79 73 74 65 6d 73 20 6d 61 79 20 72 65 74   systems may ret
e0330 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  urn SQLITE_FULL 
e0340 66 72 6f 6d 20 68 65 72 65 2e 20 54 68 69 73 0a  from here. This.
e0350 20 20 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20    ** line is to 
e0360 74 65 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20  test that doing 
e0370 73 6f 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73  so does not caus
e0380 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a  e any problems..
e0390 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44    */.  SimulateD
e03a0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65  iskfullError( re
e03b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
e03c0 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
e03d0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
e03e0 45 32 28 22 53 59 4e 43 20 20 20 20 25 2d 33 64  E2("SYNC    %-3d
e03f0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a  \n", pFile->h);.
e0400 20 20 72 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e    rc = full_fsyn
e0410 63 28 70 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75  c(pFile->h, isFu
e0420 6c 6c 73 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e  llsync, isDataOn
e0430 6c 79 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  ly);.  SimulateI
e0440 4f 45 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a  OError( rc=1 );.
e0450 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
e0460 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
e0470 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65   = errno;.    re
e0480 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
e0490 52 5f 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69  R_FSYNC;.  }.  i
e04a0 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e  f( pFile->dirfd>
e04b0 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 72  =0 ){.    int er
e04c0 72 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28  r;.    OSTRACE4(
e04d0 22 44 49 52 53 59 4e 43 20 25 2d 33 64 20 28 68  "DIRSYNC %-3d (h
e04e0 61 76 65 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 64  ave_fullfsync=%d
e04f0 20 66 75 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e 22   fullsync=%d)\n"
e0500 2c 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 0a  , pFile->dirfd,.
e0510 20 20 20 20 20 20 20 20 20 20 20 20 48 41 56 45              HAVE
e0520 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 69 73 46 75  _FULLFSYNC, isFu
e0530 6c 6c 73 79 6e 63 29 3b 0a 23 69 66 6e 64 65 66  llsync);.#ifndef
e0540 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f   SQLITE_DISABLE_
e0550 44 49 52 53 59 4e 43 0a 20 20 20 20 2f 2a 20 54  DIRSYNC.    /* T
e0560 68 65 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e  he directory syn
e0570 63 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70  c is only attemp
e0580 74 65 64 20 69 66 20 66 75 6c 6c 5f 66 73 79 6e  ted if full_fsyn
e0590 63 20 69 73 0a 20 20 20 20 2a 2a 20 74 75 72 6e  c is.    ** turn
e05a0 65 64 20 6f 66 66 20 6f 72 20 75 6e 61 76 61 69  ed off or unavai
e05b0 6c 61 62 6c 65 2e 20 20 49 66 20 61 20 66 75 6c  lable.  If a ful
e05c0 6c 5f 66 73 79 6e 63 20 6f 63 63 75 72 72 65 64  l_fsync occurred
e05d0 20 61 62 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 74   above,.    ** t
e05e0 68 65 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72  hen the director
e05f0 79 20 73 79 6e 63 20 69 73 20 73 75 70 65 72 66  y sync is superf
e0600 6c 75 6f 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  luous..    */.  
e0610 20 20 69 66 28 20 28 21 48 41 56 45 5f 46 55 4c    if( (!HAVE_FUL
e0620 4c 46 53 59 4e 43 20 7c 7c 20 21 69 73 46 75 6c  LFSYNC || !isFul
e0630 6c 73 79 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66  lsync) && full_f
e0640 73 79 6e 63 28 70 46 69 6c 65 2d 3e 64 69 72 66  sync(pFile->dirf
e0650 64 2c 30 2c 30 29 20 29 7b 0a 20 20 20 20 20 20  d,0,0) ){.      
e0660 20 2f 2a 0a 20 20 20 20 20 20 20 2a 2a 20 57 65   /*.       ** We
e0670 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 6d   have received m
e0680 75 6c 74 69 70 6c 65 20 72 65 70 6f 72 74 73 20  ultiple reports 
e0690 6f 66 20 66 73 79 6e 63 28 29 20 72 65 74 75 72  of fsync() retur
e06a0 6e 69 6e 67 0a 20 20 20 20 20 20 20 2a 2a 20 65  ning.       ** e
e06b0 72 72 6f 72 73 20 77 68 65 6e 20 61 70 70 6c 69  rrors when appli
e06c0 65 64 20 74 6f 20 64 69 72 65 63 74 6f 72 69 65  ed to directorie
e06d0 73 20 6f 6e 20 63 65 72 74 61 69 6e 20 66 69 6c  s on certain fil
e06e0 65 20 73 79 73 74 65 6d 73 2e 0a 20 20 20 20 20  e systems..     
e06f0 20 20 2a 2a 20 41 20 66 61 69 6c 65 64 20 64 69    ** A failed di
e0700 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20  rectory sync is 
e0710 6e 6f 74 20 61 20 62 69 67 20 64 65 61 6c 2e 20  not a big deal. 
e0720 20 53 6f 20 69 74 20 73 65 65 6d 73 0a 20 20 20   So it seems.   
e0730 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 74 6f      ** better to
e0740 20 69 67 6e 6f 72 65 20 74 68 65 20 65 72 72 6f   ignore the erro
e0750 72 2e 20 20 54 69 63 6b 65 74 20 23 31 36 35 37  r.  Ticket #1657
e0760 0a 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  .       */.     
e0770 20 20 2f 2a 20 70 46 69 6c 65 2d 3e 6c 61 73 74    /* pFile->last
e0780 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 2a  Errno = errno; *
e0790 2f 0a 20 20 20 20 20 20 20 2f 2a 20 72 65 74 75  /.       /* retu
e07a0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
e07b0 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66   */.    }.#endif
e07c0 0a 20 20 20 20 65 72 72 20 3d 20 63 6c 6f 73 65  .    err = close
e07d0 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20  (pFile->dirfd); 
e07e0 2f 2a 20 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20  /* Only need to 
e07f0 73 79 6e 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c  sync once, so cl
e0800 6f 73 65 20 74 68 65 20 2a 2f 0a 20 20 20 20 69  ose the */.    i
e0810 66 28 20 65 72 72 3d 3d 30 20 29 7b 20 20 20 20  f( err==0 ){    
e0820 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 69 72            /* dir
e0830 65 63 74 6f 72 79 20 77 68 65 6e 20 77 65 20 61  ectory when we a
e0840 72 65 20 64 6f 6e 65 20 2a 2f 0a 20 20 20 20 20  re done */.     
e0850 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20   pFile->dirfd = 
e0860 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  -1;.    }else{. 
e0870 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
e0880 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
e0890 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
e08a0 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45  _IOERR_DIR_CLOSE
e08b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
e08c0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
e08d0 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70  * Truncate an op
e08e0 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65  en file to a spe
e08f0 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73  cified size.*/.s
e0900 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 54 72  tatic int unixTr
e0910 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66  uncate(sqlite3_f
e0920 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79  ile *id, i64 nBy
e0930 74 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  te){.  int rc;. 
e0940 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20   assert( id );. 
e0950 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
e0960 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
e0970 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29  IOERR_TRUNCATE )
e0980 3b 0a 20 20 72 63 20 3d 20 66 74 72 75 6e 63 61  ;.  rc = ftrunca
e0990 74 65 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69  te(((unixFile*)i
e09a0 64 29 2d 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42  d)->h, (off_t)nB
e09b0 79 74 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  yte);.  if( rc )
e09c0 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65  {.    ((unixFile
e09d0 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f  *)id)->lastErrno
e09e0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65   = errno;.    re
e09f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
e0a00 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d 65  R_TRUNCATE;.  }e
e0a10 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
e0a20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d  SQLITE_OK;.  }.}
e0a30 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e  ../*.** Determin
e0a40 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69  e the current si
e0a50 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20  ze of a file in 
e0a60 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20  bytes.*/.static 
e0a70 69 6e 74 20 75 6e 69 78 46 69 6c 65 53 69 7a 65  int unixFileSize
e0a80 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
e0a90 64 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a  d, i64 *pSize){.
e0aa0 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75    int rc;.  stru
e0ab0 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 61  ct stat buf;.  a
e0ac0 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 72  ssert( id );.  r
e0ad0 63 20 3d 20 66 73 74 61 74 28 28 28 75 6e 69 78  c = fstat(((unix
e0ae0 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 26 62  File*)id)->h, &b
e0af0 75 66 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  uf);.  SimulateI
e0b00 4f 45 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a  OError( rc=1 );.
e0b10 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20    if( rc!=0 ){. 
e0b20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
e0b30 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  d)->lastErrno = 
e0b40 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72  errno;.    retur
e0b50 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46  n SQLITE_IOERR_F
e0b60 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69  STAT;.  }.  *pSi
e0b70 7a 65 20 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65  ze = buf.st_size
e0b80 3b 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65  ;..  /* When ope
e0b90 6e 69 6e 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65  ning a zero-size
e0ba0 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 66   database, the f
e0bb0 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20 70 72  indLockInfo() pr
e0bc0 6f 63 65 64 75 72 65 0a 20 20 2a 2a 20 77 72 69  ocedure.  ** wri
e0bd0 74 65 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74  tes a single byt
e0be0 65 20 69 6e 74 6f 20 74 68 61 74 20 66 69 6c 65  e into that file
e0bf0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 77 6f 72   in order to wor
e0c00 6b 20 61 72 6f 75 6e 64 20 61 20 62 75 67 0a 20  k around a bug. 
e0c10 20 2a 2a 20 69 6e 20 74 68 65 20 4f 53 2d 58 20   ** in the OS-X 
e0c20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65 6d  msdos filesystem
e0c30 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 61  .  In order to a
e0c40 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 20 77 69  void problems wi
e0c50 74 68 20 75 70 70 65 72 0a 20 20 2a 2a 20 6c 61  th upper.  ** la
e0c60 79 65 72 73 2c 20 77 65 20 6e 65 65 64 20 74 6f  yers, we need to
e0c70 20 72 65 70 6f 72 74 20 74 68 69 73 20 66 69 6c   report this fil
e0c80 65 20 73 69 7a 65 20 61 73 20 7a 65 72 6f 20 65  e size as zero e
e0c90 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20 69 73  ven though it is
e0ca0 0a 20 20 2a 2a 20 72 65 61 6c 6c 79 20 31 2e 20  .  ** really 1. 
e0cb0 20 20 54 69 63 6b 65 74 20 23 33 32 36 30 2e 0a    Ticket #3260..
e0cc0 20 20 2a 2f 0a 20 20 69 66 28 20 2a 70 53 69 7a    */.  if( *pSiz
e0cd0 65 3d 3d 31 20 29 20 2a 70 53 69 7a 65 20 3d 20  e==1 ) *pSize = 
e0ce0 30 3b 0a 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  0;...  return SQ
e0cf0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20  LITE_OK;.}..#if 
e0d00 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
e0d10 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64  CKING_STYLE && d
e0d20 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
e0d30 29 0a 2f 2a 0a 2a 2a 20 48 61 6e 64 6c 65 72 20  )./*.** Handler 
e0d40 66 6f 72 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e  for proxy-lockin
e0d50 67 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 76  g file-control v
e0d60 65 72 62 73 2e 20 20 44 65 66 69 6e 65 64 20 62  erbs.  Defined b
e0d70 65 6c 6f 77 20 69 6e 20 74 68 65 0a 2a 2a 20 70  elow in the.** p
e0d80 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20  roxying locking 
e0d90 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  division..*/.sta
e0da0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 6c  tic int proxyFil
e0db0 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33  eControl(sqlite3
e0dc0 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a  _file*,int,void*
e0dd0 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  );.#endif.../*.*
e0de0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e  * Information an
e0df0 64 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 61 6e 20  d control of an 
e0e00 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65  open file handle
e0e10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e0e20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  unixFileControl(
e0e30 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
e0e40 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a  , int op, void *
e0e50 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28  pArg){.  switch(
e0e60 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
e0e70 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
e0e80 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20  KSTATE: {.      
e0e90 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28  *(int*)pArg = ((
e0ea0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
e0eb0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72  ocktype;.      r
e0ec0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e0ed0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
e0ee0 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e  SQLITE_LAST_ERRN
e0ef0 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74  O: {.      *(int
e0f00 2a 29 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46  *)pArg = ((unixF
e0f10 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
e0f20 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 72  rno;.      retur
e0f30 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
e0f40 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55   }.#ifndef NDEBU
e0f50 47 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67  G.    /* The pag
e0f60 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 6d 65  er calls this me
e0f70 74 68 6f 64 20 74 6f 20 73 69 67 6e 61 6c 20 74  thod to signal t
e0f80 68 61 74 20 69 74 20 68 61 73 20 64 6f 6e 65 0a  hat it has done.
e0f90 20 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 63      ** a rollbac
e0fa0 6b 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 64  k and that the d
e0fb0 61 74 61 62 61 73 65 20 69 73 20 74 68 65 72 65  atabase is there
e0fc0 66 6f 72 65 20 75 6e 63 68 61 6e 67 65 64 20 61  fore unchanged a
e0fd0 6e 64 0a 20 20 20 20 2a 2a 20 69 74 20 68 65 6e  nd.    ** it hen
e0fe0 63 65 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20  ce it is OK for 
e0ff0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
e1000 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 74  change counter t
e1010 6f 20 62 65 0a 20 20 20 20 2a 2a 20 75 6e 63 68  o be.    ** unch
e1020 61 6e 67 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  anged..    */.  
e1030 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43    case SQLITE_FC
e1040 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44  NTL_DB_UNCHANGED
e1050 3a 20 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78  : {.      ((unix
e1060 46 69 6c 65 2a 29 69 64 29 2d 3e 64 62 55 70 64  File*)id)->dbUpd
e1070 61 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 72  ate = 0;.      r
e1080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e1090 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69  .    }.#endif.#i
e10a0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
e10b0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26  LOCKING_STYLE &&
e10c0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
e10d0 5f 5f 29 0a 20 20 20 20 63 61 73 65 20 53 51 4c  __).    case SQL
e10e0 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_SET_LOCKPROX
e10f0 59 46 49 4c 45 3a 0a 20 20 20 20 63 61 73 65 20  YFILE:.    case 
e1100 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50  SQLITE_GET_LOCKP
e1110 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20  ROXYFILE: {.    
e1120 20 20 72 65 74 75 72 6e 20 70 72 6f 78 79 46 69    return proxyFi
e1130 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c 6f 70 2c  leControl(id,op,
e1140 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  pArg);.    }.#en
e1150 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e  dif /* SQLITE_EN
e1160 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
e1170 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  LE && defined(__
e1180 41 50 50 4c 45 5f 5f 29 20 2a 2f 0a 20 20 7d 0a  APPLE__) */.  }.
e1190 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e11a0 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ERROR;.}../*.** 
e11b0 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f  Return the secto
e11c0 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  r size in bytes 
e11d0 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  of the underlyin
e11e0 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66  g block device f
e11f0 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66  or.** the specif
e1200 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69  ied file. This i
e1210 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20  s almost always 
e1220 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d  512 bytes, but m
e1230 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20  ay be.** larger 
e1240 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73  for some devices
e1250 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63  ..**.** SQLite c
e1260 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73  ode assumes this
e1270 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74   function cannot
e1280 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61   fail. It also a
e1290 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69  ssumes that.** i
e12a0 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20  f two files are 
e12b0 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73  created in the s
e12c0 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  ame file-system 
e12d0 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a  directory (i.e..
e12e0 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e  ** a database an
e12f0 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69  d its journal fi
e1300 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63  le) that the sec
e1310 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65  tor size will be
e1320 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72   the.** same for
e1330 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63   both..*/.static
e1340 20 69 6e 74 20 75 6e 69 78 53 65 63 74 6f 72 53   int unixSectorS
e1350 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ize(sqlite3_file
e1360 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
e1370 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
e1380 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
e1390 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  n SQLITE_DEFAULT
e13a0 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a  _SECTOR_SIZE;.}.
e13b0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
e13c0 65 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74  e device charact
e13d0 65 72 69 73 74 69 63 73 20 66 6f 72 20 74 68 65  eristics for the
e13e0 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61   file. This is a
e13f0 6c 77 61 79 73 20 30 20 66 6f 72 20 75 6e 69 78  lways 0 for unix
e1400 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e1410 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63  unixDeviceCharac
e1420 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65  teristics(sqlite
e1430 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 29  3_file *NotUsed)
e1440 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
e1450 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
e1460 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
e1470 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68  .** Here ends th
e1480 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
e1490 20 6f 66 20 61 6c 6c 20 73 71 6c 69 74 65 33 5f   of all sqlite3_
e14a0 66 69 6c 65 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a  file methods..**
e14b0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
e14c0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 73 71 6c 69  ******* End sqli
e14d0 74 65 33 5f 66 69 6c 65 20 4d 65 74 68 6f 64 73  te3_file Methods
e14e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
e14f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1500 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
e1510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
e1550 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76  ../*.** This div
e1560 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 64  ision contains d
e1570 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71  efinitions of sq
e1580 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
e1590 20 6f 62 6a 65 63 74 73 20 74 68 61 74 0a 2a 2a   objects that.**
e15a0 20 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f   implement vario
e15b0 75 73 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  us file locking 
e15c0 73 74 72 61 74 65 67 69 65 73 2e 20 20 49 74 20  strategies.  It 
e15d0 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 64 65  also contains de
e15e0 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20  finitions.** of 
e15f0 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f  "finder" functio
e1600 6e 73 2e 20 20 41 20 66 69 6e 64 65 72 2d 66 75  ns.  A finder-fu
e1610 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
e1620 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 61 70 70  o locate the app
e1630 72 6f 70 72 69 61 74 65 0a 2a 2a 20 73 71 6c 69  ropriate.** sqli
e1640 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  te3_io_methods o
e1650 62 6a 65 63 74 20 66 6f 72 20 61 20 70 61 72 74  bject for a part
e1660 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20  icular database 
e1670 66 69 6c 65 2e 20 20 54 68 65 20 70 41 70 70 44  file.  The pAppD
e1680 61 74 61 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20  ata.** field of 
e1690 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20  the sqlite3_vfs 
e16a0 56 46 53 20 6f 62 6a 65 63 74 73 20 61 72 65 20  VFS objects are 
e16b0 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 62  initialized to b
e16c0 65 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a  e pointers to.**
e16d0 20 74 68 65 20 63 6f 72 72 65 63 74 20 66 69 6e   the correct fin
e16e0 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 66 6f 72  der-function for
e16f0 20 74 68 61 74 20 56 46 53 2e 0a 2a 2a 0a 2a 2a   that VFS..**.**
e1700 20 4d 6f 73 74 20 66 69 6e 64 65 72 20 66 75 6e   Most finder fun
e1710 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 61 20  ctions return a 
e1720 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 69 78  pointer to a fix
e1730 65 64 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  ed sqlite3_io_me
e1740 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e  thods.** object.
e1750 20 20 54 68 65 20 6f 6e 6c 79 20 69 6e 74 65 72    The only inter
e1760 65 73 74 69 6e 67 20 66 69 6e 64 65 72 2d 66 75  esting finder-fu
e1770 6e 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6c 6f  nction is autolo
e1780 63 6b 49 6f 46 69 6e 64 65 72 2c 20 77 68 69 63  ckIoFinder, whic
e1790 68 0a 2a 2a 20 6c 6f 6f 6b 73 20 61 74 20 74 68  h.** looks at th
e17a0 65 20 66 69 6c 65 73 79 73 74 65 6d 20 74 79 70  e filesystem typ
e17b0 65 20 61 6e 64 20 74 72 69 65 73 20 74 6f 20 67  e and tries to g
e17c0 75 65 73 73 20 74 68 65 20 62 65 73 74 20 6c 6f  uess the best lo
e17d0 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 61 74 65 67  cking.** strateg
e17e0 79 20 66 72 6f 6d 20 74 68 61 74 2e 0a 2a 2a 0a  y from that..**.
e17f0 2a 2a 20 46 6f 72 20 66 69 6e 64 65 72 2d 66 75  ** For finder-fu
e1800 6e 74 69 6f 6e 20 46 2c 20 74 77 6f 20 6f 62 6a  ntion F, two obj
e1810 65 63 74 73 20 61 72 65 20 63 72 65 61 74 65 64  ects are created
e1820 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 54  :.**.**    (1) T
e1830 68 65 20 72 65 61 6c 20 66 69 6e 64 65 72 2d 66  he real finder-f
e1840 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 22 46  unction named "F
e1850 49 6d 70 74 28 29 22 2e 0a 2a 2a 0a 2a 2a 20 20  Impt()"..**.**  
e1860 20 20 28 32 29 20 41 20 63 6f 6e 73 74 61 6e 74    (2) A constant
e1870 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
e1880 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20   function named 
e1890 6a 75 73 74 20 22 46 22 2e 0a 2a 2a 0a 2a 2a 0a  just "F"..**.**.
e18a0 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** A pointer to 
e18b0 74 68 65 20 46 20 70 6f 69 6e 74 65 72 20 69 73  the F pointer is
e18c0 20 75 73 65 64 20 61 73 20 74 68 65 20 70 41 70   used as the pAp
e18d0 70 44 61 74 61 20 76 61 6c 75 65 20 66 6f 72 20  pData value for 
e18e0 56 46 53 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 20  VFS.** objects. 
e18f0 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 74   We have to do t
e1900 68 69 73 20 69 6e 73 74 65 61 64 20 6f 66 20 6c  his instead of l
e1910 65 74 74 69 6e 67 20 70 41 70 70 44 61 74 61 20  etting pAppData 
e1920 70 6f 69 6e 74 0a 2a 2a 20 64 69 72 65 63 74 6c  point.** directl
e1930 79 20 61 74 20 74 68 65 20 66 69 6e 64 65 72 2d  y at the finder-
e1940 66 75 6e 63 74 69 6f 6e 20 73 69 6e 63 65 20 43  function since C
e1950 39 30 20 72 75 6c 65 73 20 70 72 65 76 65 6e 74  90 rules prevent
e1960 20 61 20 76 6f 69 64 2a 0a 2a 2a 20 66 72 6f 6d   a void*.** from
e1970 20 62 65 20 63 61 73 74 20 69 6e 74 6f 20 61 20   be cast into a 
e1980 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
e1990 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  ..**.**.** Each 
e19a0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
e19b0 20 6d 61 63 72 6f 20 67 65 6e 65 72 61 74 65 73   macro generates
e19c0 20 74 77 6f 20 6f 62 6a 65 63 74 73 3a 0a 2a 2a   two objects:.**
e19d0 0a 2a 2a 20 20 20 2a 20 20 41 20 63 6f 6e 73 74  .**   *  A const
e19e0 61 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  ant sqlite3_io_m
e19f0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 63 61  ethods object ca
e1a00 6c 6c 20 4d 45 54 48 4f 44 20 74 68 61 74 20 68  ll METHOD that h
e1a10 61 73 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20  as locking.**   
e1a20 20 20 20 6d 65 74 68 6f 64 73 20 43 4c 4f 53 45     methods CLOSE
e1a30 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20  , LOCK, UNLOCK, 
e1a40 43 4b 52 45 53 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a  CKRESLOCK..**.**
e1a50 20 20 20 2a 20 20 41 6e 20 49 2f 4f 20 6d 65 74     *  An I/O met
e1a60 68 6f 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74  hod finder funct
e1a70 69 6f 6e 20 63 61 6c 6c 65 64 20 46 49 4e 44 45  ion called FINDE
e1a80 52 20 74 68 61 74 20 72 65 74 75 72 6e 73 20 61  R that returns a
e1a90 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20   pointer.**     
e1aa0 20 74 6f 20 74 68 65 20 4d 45 54 48 4f 44 20 6f   to the METHOD o
e1ab0 62 6a 65 63 74 20 69 6e 20 74 68 65 20 70 72 65  bject in the pre
e1ac0 76 69 6f 75 73 20 62 75 6c 6c 65 74 2e 0a 2a 2f  vious bullet..*/
e1ad0 0a 23 64 65 66 69 6e 65 20 49 4f 4d 45 54 48 4f  .#define IOMETHO
e1ae0 44 53 28 46 49 4e 44 45 52 2c 20 4d 45 54 48 4f  DS(FINDER, METHO
e1af0 44 2c 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20  D, CLOSE, LOCK, 
e1b00 55 4e 4c 4f 43 4b 2c 20 43 4b 4c 4f 43 4b 29 20  UNLOCK, CKLOCK) 
e1b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e1b20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
e1b30 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
e1b40 4d 45 54 48 4f 44 20 3d 20 7b 20 20 20 20 20 20  METHOD = {      
e1b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b60 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e1b70 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
e1b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e1b90 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20   iVersion */    
e1ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1bb0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e1bc0 20 43 4c 4f 53 45 2c 20 20 20 20 20 20 20 20 20   CLOSE,         
e1bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e1be0 78 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20  xClose */       
e1bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1c00 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e1c10 75 6e 69 78 52 65 61 64 2c 20 20 20 20 20 20 20  unixRead,       
e1c20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e1c30 52 65 61 64 20 2a 2f 20 20 20 20 20 20 20 20 20  Read */         
e1c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1c50 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75            \.   u
e1c60 6e 69 78 57 72 69 74 65 2c 20 20 20 20 20 20 20  nixWrite,       
e1c70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 57             /* xW
e1c80 72 69 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20  rite */         
e1c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ca0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e           \.   un
e1cb0 69 78 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20  ixTruncate,     
e1cc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72            /* xTr
e1cd0 75 6e 63 61 74 65 20 2a 2f 20 20 20 20 20 20 20  uncate */       
e1ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1cf0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69          \.   uni
e1d00 78 53 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20  xSync,          
e1d10 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 79 6e           /* xSyn
e1d20 63 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  c */            
e1d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d40 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78         \.   unix
e1d50 46 69 6c 65 53 69 7a 65 2c 20 20 20 20 20 20 20  FileSize,       
e1d60 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65          /* xFile
e1d70 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20 20  Size */         
e1d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d90 20 20 20 20 20 20 5c 0a 20 20 20 4c 4f 43 4b 2c        \.   LOCK,
e1da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1db0 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
e1dc0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e1dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1de0 20 20 20 20 20 5c 0a 20 20 20 55 4e 4c 4f 43 4b       \.   UNLOCK
e1df0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e1e00 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
e1e10 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
e1e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e30 20 20 20 20 5c 0a 20 20 20 43 4b 4c 4f 43 4b 2c      \.   CKLOCK,
e1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e50 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65       /* xCheckRe
e1e60 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 20 20 20  servedLock */   
e1e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e80 20 20 20 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65     \.   unixFile
e1e90 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 20 20 20 20  Control,        
e1ea0 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74      /* xFileCont
e1eb0 72 6f 6c 20 2a 2f 20 20 20 20 20 20 20 20 20 20  rol */          
e1ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ed0 20 20 5c 0a 20 20 20 75 6e 69 78 53 65 63 74 6f    \.   unixSecto
e1ee0 72 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20  rSize,          
e1ef0 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a     /* xSectorSiz
e1f00 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
e1f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f20 20 5c 0a 20 20 20 75 6e 69 78 44 65 76 69 63 65   \.   unixDevice
e1f30 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  Characteristics 
e1f40 20 20 2f 2a 20 78 44 65 76 69 63 65 43 61 70 61    /* xDeviceCapa
e1f50 62 69 6c 69 74 69 65 73 20 2a 2f 20 20 20 20 20  bilities */     
e1f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f70 5c 0a 7d 3b 20 20 20 20 20 20 20 20 20 20 20 20  \.};            
e1f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e1fc0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
e1fd0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
e1fe0 20 2a 46 49 4e 44 45 52 23 23 49 6d 70 6c 28 63   *FINDER##Impl(c
e1ff0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 75 6e  onst char *z, un
e2000 69 78 46 69 6c 65 20 2a 70 29 7b 20 20 20 5c 0a  ixFile *p){   \.
e2010 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
e2020 45 52 28 7a 29 3b 20 55 4e 55 53 45 44 5f 50 41  ER(z); UNUSED_PA
e2030 52 41 4d 45 54 45 52 28 70 29 3b 20 20 20 20 20  RAMETER(p);     
e2040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2050 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e2060 20 72 65 74 75 72 6e 20 26 4d 45 54 48 4f 44 3b   return &METHOD;
e2070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e20a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 20              \.} 
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 20 20 20 20 20 20 20 20 20                  
e20d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e20e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e20f0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61             \.sta
e2100 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
e2110 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a  3_io_methods *(*
e2120 63 6f 6e 73 74 20 46 49 4e 44 45 52 29 28 63 6f  const FINDER)(co
e2130 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69  nst char*,unixFi
e2140 6c 65 20 2a 70 29 20 20 20 20 5c 0a 20 20 20 20  le *p)    \.    
e2150 3d 20 46 49 4e 44 45 52 23 23 49 6d 70 6c 3b 0a  = FINDER##Impl;.
e2160 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 61 72 65 20  ./*.** Here are 
e2170 61 6c 6c 20 6f 66 20 74 68 65 20 73 71 6c 69 74  all of the sqlit
e2180 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
e2190 6a 65 63 74 73 20 66 6f 72 20 65 61 63 68 20 6f  jects for each o
e21a0 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67  f the.** locking
e21b0 20 73 74 72 61 74 65 67 69 65 73 2e 20 20 46 75   strategies.  Fu
e21c0 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 72 65 74  nctions that ret
e21d0 75 72 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  urn pointers to 
e21e0 74 68 65 73 65 20 6d 65 74 68 6f 64 73 0a 2a 2a  these methods.**
e21f0 20 61 72 65 20 61 6c 73 6f 20 63 72 65 61 74 65   are also create
e2200 64 2e 0a 2a 2f 0a 49 4f 4d 45 54 48 4f 44 53 28  d..*/.IOMETHODS(
e2210 0a 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72  .  posixIoFinder
e2220 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
e2230 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20  Finder function 
e2240 6e 61 6d 65 20 2a 2f 0a 20 20 70 6f 73 69 78 49  name */.  posixI
e2250 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20  oMethods,       
e2260 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69      /* sqlite3_i
e2270 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
e2280 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 6e 69 78 43   name */.  unixC
e2290 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  lose,           
e22a0 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d       /* xClose m
e22b0 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 4c  ethod */.  unixL
e22c0 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
e22d0 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65       /* xLock me
e22e0 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 55 6e  thod */.  unixUn
e22f0 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  lock,           
e2300 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d      /* xUnlock m
e2310 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 43  ethod */.  unixC
e2320 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
e2330 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65       /* xCheckRe
e2340 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f  servedLock metho
e2350 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 48 4f 44 53  d */.).IOMETHODS
e2360 28 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64  (.  nolockIoFind
e2370 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  er,           /*
e2380 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e   Finder function
e2390 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63   name */.  noloc
e23a0 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20  kIoMethods,     
e23b0 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f       /* sqlite3_
e23c0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63  io_methods objec
e23d0 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e 6f 6c 6f  t name */.  nolo
e23e0 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  ckClose,        
e23f0 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20        /* xClose 
e2400 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f  method */.  nolo
e2410 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  ckLock,         
e2420 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d        /* xLock m
e2430 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63  ethod */.  noloc
e2440 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  kUnlock,        
e2450 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20       /* xUnlock 
e2460 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f  method */.  nolo
e2470 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  ckCheckReservedL
e2480 6f 63 6b 20 20 20 2f 2a 20 78 43 68 65 63 6b 52  ock   /* xCheckR
e2490 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68  eservedLock meth
e24a0 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 48 4f 44  od */.).IOMETHOD
e24b0 53 28 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69  S(.  dotlockIoFi
e24c0 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 2f  nder,          /
e24d0 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f  * Finder functio
e24e0 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c  n name */.  dotl
e24f0 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20  ockIoMethods,   
e2500 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33        /* sqlite3
e2510 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
e2520 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 6f 74  ct name */.  dot
e2530 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20  lockClose,      
e2540 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
e2550 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74   method */.  dot
e2560 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20  lockLock,       
e2570 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
e2580 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c  method */.  dotl
e2590 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20  ockUnlock,      
e25a0 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
e25b0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74   method */.  dot
e25c0 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65  lockCheckReserve
e25d0 64 4c 6f 63 6b 20 20 2f 2a 20 78 43 68 65 63 6b  dLock  /* xCheck
e25e0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74  ReservedLock met
e25f0 68 6f 64 20 2a 2f 0a 29 0a 0a 23 69 66 20 53 51  hod */.)..#if SQ
e2600 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
e2610 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53  ING_STYLE && !OS
e2620 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f  _VXWORKS.IOMETHO
e2630 44 53 28 0a 20 20 66 6c 6f 63 6b 49 6f 46 69 6e  DS(.  flockIoFin
e2640 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  der,            
e2650 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69  /* Finder functi
e2660 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f  on name */.  flo
e2670 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20  ckIoMethods,    
e2680 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
e2690 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
e26a0 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c  ect name */.  fl
e26b0 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  ockClose,       
e26c0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73          /* xClos
e26d0 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c  e method */.  fl
e26e0 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20  ockLock,        
e26f0 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b          /* xLock
e2700 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f   method */.  flo
e2710 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20  ckUnlock,       
e2720 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63         /* xUnloc
e2730 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c  k method */.  fl
e2740 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64  ockCheckReserved
e2750 4c 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68 65 63  Lock    /* xChec
e2760 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65  kReservedLock me
e2770 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66  thod */.).#endif
e2780 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  ..#if OS_VXWORKS
e2790 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 73 65  .IOMETHODS(.  se
e27a0 6d 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20  mIoFinder,      
e27b0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
e27c0 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
e27d0 2a 2f 0a 20 20 73 65 6d 49 6f 4d 65 74 68 6f 64  */.  semIoMethod
e27e0 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  s,             /
e27f0 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  * sqlite3_io_met
e2800 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hods object name
e2810 20 2a 2f 0a 20 20 73 65 6d 43 6c 6f 73 65 2c 20   */.  semClose, 
e2820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2830 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
e2840 20 2a 2f 0a 20 20 73 65 6d 4c 6f 63 6b 2c 20 20   */.  semLock,  
e2850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2860 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
e2870 2a 2f 0a 20 20 73 65 6d 55 6e 6c 6f 63 6b 2c 20  */.  semUnlock, 
e2880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e2890 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
e28a0 20 2a 2f 0a 20 20 73 65 6d 43 68 65 63 6b 52 65   */.  semCheckRe
e28b0 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 20  servedLock      
e28c0 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
e28d0 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
e28e0 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 64 65  ).#endif..#if de
e28f0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
e2900 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
e2910 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
e2920 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 61 66 70  IOMETHODS(.  afp
e2930 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
e2940 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72         /* Finder
e2950 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a   function name *
e2960 2f 0a 20 20 61 66 70 49 6f 4d 65 74 68 6f 64 73  /.  afpIoMethods
e2970 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
e2980 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e2990 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20  ods object name 
e29a0 2a 2f 0a 20 20 61 66 70 43 6c 6f 73 65 2c 20 20  */.  afpClose,  
e29b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e29c0 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20  * xClose method 
e29d0 2a 2f 0a 20 20 61 66 70 4c 6f 63 6b 2c 20 20 20  */.  afpLock,   
e29e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e29f0 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  * xLock method *
e2a00 2f 0a 20 20 61 66 70 55 6e 6c 6f 63 6b 2c 20 20  /.  afpUnlock,  
e2a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e2a20 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20   xUnlock method 
e2a30 2a 2f 0a 20 20 61 66 70 43 68 65 63 6b 52 65 73  */.  afpCheckRes
e2a40 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 20 2f  ervedLock      /
e2a50 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
e2a60 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29  Lock method */.)
e2a70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
e2a80 68 65 20 22 57 68 6f 6c 65 20 46 69 6c 65 20 4c  he "Whole File L
e2a90 6f 63 6b 69 6e 67 22 20 66 69 6e 64 65 72 20 72  ocking" finder r
e2aa0 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20  eturns the same 
e2ab0 73 65 74 20 6f 66 20 6d 65 74 68 6f 64 73 20 61  set of methods a
e2ac0 73 0a 2a 2a 20 74 68 65 20 70 6f 73 69 78 20 6c  s.** the posix l
e2ad0 6f 63 6b 69 6e 67 20 66 69 6e 64 65 72 2e 20 20  ocking finder.  
e2ae0 42 75 74 20 69 74 20 61 6c 73 6f 20 73 65 74 73  But it also sets
e2af0 20 74 68 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c   the SQLITE_WHOL
e2b00 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 0a 2a  E_FILE_LOCKING.*
e2b10 2a 20 66 6c 61 67 20 74 6f 20 66 6f 72 63 65 20  * flag to force 
e2b20 74 68 65 20 70 6f 73 69 78 20 61 64 76 69 73 6f  the posix adviso
e2b30 72 79 20 6c 6f 63 6b 73 20 74 6f 20 63 6f 76 65  ry locks to cove
e2b40 72 20 74 68 65 20 77 68 6f 6c 65 20 66 69 6c 65  r the whole file
e2b50 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 6a   instead.** of j
e2b60 75 73 74 20 61 20 73 6d 61 6c 6c 20 73 70 61 6e  ust a small span
e2b70 20 6f 66 20 62 79 74 65 73 20 6e 65 61 72 20 74   of bytes near t
e2b80 68 65 20 31 47 69 42 20 62 6f 75 6e 64 61 72 79  he 1GiB boundary
e2b90 2e 20 20 57 68 6f 6c 65 20 46 69 6c 65 20 4c 6f  .  Whole File Lo
e2ba0 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 75 73 65 66  cking.** is usef
e2bb0 75 6c 20 6f 6e 20 4e 46 53 2d 6d 6f 75 6e 74 65  ul on NFS-mounte
e2bc0 64 20 66 69 6c 65 73 20 73 69 6e 63 65 20 69 74  d files since it
e2bd0 20 68 65 6c 70 73 20 4e 46 53 20 74 6f 20 6d 61   helps NFS to ma
e2be0 69 6e 74 61 69 6e 20 63 61 63 68 65 0a 2a 2a 20  intain cache.** 
e2bf0 63 6f 68 65 72 65 6e 63 79 2e 20 20 42 75 74 20  coherency.  But 
e2c00 69 74 20 69 73 20 61 20 64 65 74 72 69 6d 65 6e  it is a detrimen
e2c10 74 20 74 6f 20 6f 74 68 65 72 20 66 69 6c 65 73  t to other files
e2c20 79 73 74 65 6d 73 20 73 69 6e 63 65 20 69 74 20  ystems since it 
e2c30 72 75 6e 73 0a 2a 2a 20 73 6c 6f 77 65 72 2e 0a  runs.** slower..
e2c40 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
e2c50 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e2c60 64 73 20 2a 70 6f 73 69 78 57 66 6c 49 6f 46 69  ds *posixWflIoFi
e2c70 6e 64 65 72 49 6d 70 6c 28 63 6f 6e 73 74 20 63  nderImpl(const c
e2c80 68 61 72 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 2a  har*z, unixFile*
e2c90 70 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  p){.  UNUSED_PAR
e2ca0 41 4d 45 54 45 52 28 7a 29 3b 0a 20 20 70 2d 3e  AMETER(z);.  p->
e2cb0 66 69 6c 65 46 6c 61 67 73 20 3d 20 53 51 4c 49  fileFlags = SQLI
e2cc0 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f  TE_WHOLE_FILE_LO
e2cd0 43 4b 49 4e 47 3b 0a 20 20 72 65 74 75 72 6e 20  CKING;.  return 
e2ce0 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b  &posixIoMethods;
e2cf0 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .}.static const 
e2d00 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e2d10 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 70  ds .  *(*const p
e2d20 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64 65 72 29  osixWflIoFinder)
e2d30 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69  (const char*,uni
e2d40 78 46 69 6c 65 20 2a 70 29 20 3d 20 70 6f 73 69  xFile *p) = posi
e2d50 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d 70 6c  xWflIoFinderImpl
e2d60 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f  ;../*.** The pro
e2d70 78 79 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f  xy locking metho
e2d80 64 20 69 73 20 61 20 22 73 75 70 65 72 2d 6d 65  d is a "super-me
e2d90 74 68 6f 64 22 20 69 6e 20 74 68 65 20 73 65 6e  thod" in the sen
e2da0 73 65 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 70  se that it.** op
e2db0 65 6e 73 20 73 65 63 6f 6e 64 61 72 79 20 66 69  ens secondary fi
e2dc0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 66  le descriptors f
e2dd0 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 61 6e 64  or the conch and
e2de0 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 6e 64 0a   lock files and.
e2df0 2a 2a 20 69 74 20 75 73 65 73 20 70 72 6f 78 79  ** it uses proxy
e2e00 2c 20 64 6f 74 2d 66 69 6c 65 2c 20 41 46 50 2c  , dot-file, AFP,
e2e10 20 61 6e 64 20 66 6c 6f 63 6b 28 29 20 6c 6f 63   and flock() loc
e2e20 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20  king methods on 
e2e30 74 68 6f 73 65 0a 2a 2a 20 73 65 63 6f 6e 64 61  those.** seconda
e2e40 72 79 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 74  ry files.  For t
e2e50 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20  his reason, the 
e2e60 64 69 76 69 73 69 6f 6e 20 74 68 61 74 20 69 6d  division that im
e2e70 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 70 72 6f 78  plements.** prox
e2e80 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 6f 63  y locking is loc
e2e90 61 74 65 64 20 6d 75 63 68 20 66 75 72 74 68 65  ated much furthe
e2ea0 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 66 69  r down in the fi
e2eb0 6c 65 2e 20 20 42 75 74 20 77 65 20 6e 65 65 64  le.  But we need
e2ec0 0a 2a 2a 20 74 6f 20 67 6f 20 61 68 65 61 64 20  .** to go ahead 
e2ed0 61 6e 64 20 64 65 66 69 6e 65 20 74 68 65 20 73  and define the s
e2ee0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e2ef0 73 20 61 6e 64 20 66 69 6e 64 65 72 20 66 75 6e  s and finder fun
e2f00 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f  ction.** for pro
e2f10 78 79 20 6c 6f 63 6b 69 6e 67 20 68 65 72 65 2e  xy locking here.
e2f20 20 20 53 6f 20 77 65 20 66 6f 72 77 61 72 64 20    So we forward 
e2f30 64 65 63 6c 61 72 65 20 74 68 65 20 49 2f 4f 20  declare the I/O 
e2f40 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20  methods..*/.#if 
e2f50 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
e2f60 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  _) && SQLITE_ENA
e2f70 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
e2f80 45 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  E.static int pro
e2f90 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  xyClose(sqlite3_
e2fa0 66 69 6c 65 2a 29 3b 0a 73 74 61 74 69 63 20 69  file*);.static i
e2fb0 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c  nt proxyLock(sql
e2fc0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29  ite3_file*, int)
e2fd0 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  ;.static int pro
e2fe0 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  xyUnlock(sqlite3
e2ff0 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74  _file*, int);.st
e3000 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68  atic int proxyCh
e3010 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
e3020 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
e3030 6e 74 2a 29 3b 0a 49 4f 4d 45 54 48 4f 44 53 28  nt*);.IOMETHODS(
e3040 0a 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72  .  proxyIoFinder
e3050 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
e3060 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20  Finder function 
e3070 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 49  name */.  proxyI
e3080 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20  oMethods,       
e3090 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69      /* sqlite3_i
e30a0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
e30b0 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79   name */.  proxy
e30c0 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
e30d0 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d       /* xClose m
e30e0 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79  ethod */.  proxy
e30f0 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  Lock,           
e3100 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65       /* xLock me
e3110 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 55  thod */.  proxyU
e3120 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  nlock,          
e3130 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d      /* xUnlock m
e3140 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79  ethod */.  proxy
e3150 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
e3160 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65  k    /* xCheckRe
e3170 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f  servedLock metho
e3180 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 0a  d */.).#endif...
e3190 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50  #if defined(__AP
e31a0 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
e31b0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
e31c0 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69  STYLE./* .** Thi
e31d0 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74  s "finder" funct
e31e0 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ion attempts to 
e31f0 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 62 65  determine the be
e3200 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74  st locking strat
e3210 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20  egy .** for the 
e3220 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 22 66  database file "f
e3230 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20 74 68  ilePath".  It th
e3240 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  en returns the s
e3250 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e3260 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74  s.** object that
e3270 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74   implements that
e3280 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a   strategy..**.**
e3290 20 54 68 69 73 20 69 73 20 66 6f 72 20 4d 61 63   This is for Mac
e32a0 4f 53 58 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61  OSX only..*/.sta
e32b0 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
e32c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75  3_io_methods *au
e32d0 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d  tolockIoFinderIm
e32e0 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  pl(.  const char
e32f0 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f   *filePath,    /
e3300 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * name of the da
e3310 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
e3320 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20   unixFile *pNew 
e3330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 70 65            /* ope
e3340 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 66 6f  n file object fo
e3350 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  r the database f
e3360 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74  ile */.){.  stat
e3370 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
e3380 4d 61 70 70 69 6e 67 20 7b 0a 20 20 20 20 63 6f  Mapping {.    co
e3390 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 73  nst char *zFiles
e33a0 79 73 74 65 6d 3b 20 20 20 20 20 20 20 20 20 20  ystem;          
e33b0 20 20 20 20 2f 2a 20 46 69 6c 65 73 79 73 74 65      /* Filesyste
e33c0 6d 20 74 79 70 65 20 6e 61 6d 65 20 2a 2f 0a 20  m type name */. 
e33d0 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33     const sqlite3
e33e0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65  _io_methods *pMe
e33f0 74 68 6f 64 73 3b 20 20 20 2f 2a 20 41 70 70 72  thods;   /* Appr
e3400 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20  opriate locking 
e3410 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7d 20 61 4d  method */.  } aM
e3420 61 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22  ap[] = {.    { "
e3430 68 66 73 22 2c 20 20 20 20 26 70 6f 73 69 78 49  hfs",    &posixI
e3440 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20  oMethods },.    
e3450 7b 20 22 75 66 73 22 2c 20 20 20 20 26 70 6f 73  { "ufs",    &pos
e3460 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20  ixIoMethods },. 
e3470 20 20 20 7b 20 22 61 66 70 66 73 22 2c 20 20 26     { "afpfs",  &
e3480 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a  afpIoMethods },.
e3490 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
e34a0 41 42 4c 45 5f 41 46 50 5f 4c 4f 43 4b 49 4e 47  ABLE_AFP_LOCKING
e34b0 5f 53 4d 42 0a 20 20 20 20 7b 20 22 73 6d 62 66  _SMB.    { "smbf
e34c0 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74 68 6f  s",  &afpIoMetho
e34d0 64 73 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 20 20  ds },.#else.    
e34e0 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 66 6c 6f  { "smbfs",  &flo
e34f0 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23  ckIoMethods },.#
e3500 65 6e 64 69 66 0a 20 20 20 20 7b 20 22 77 65 62  endif.    { "web
e3510 64 61 76 22 2c 20 26 6e 6f 6c 6f 63 6b 49 6f 4d  dav", &nolockIoM
e3520 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20  ethods },.    { 
e3530 30 2c 20 30 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e  0, 0 }.  };.  in
e3540 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 73 74  t i;.  struct st
e3550 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 73  atfs fsInfo;.  s
e3560 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b  truct flock lock
e3570 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 69  Info;..  if( !fi
e3580 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20 2f 2a  lePath ){.    /*
e3590 20 49 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55   If filePath==NU
e35a0 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65  LL that means we
e35b0 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
e35c0 68 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69  h a transient fi
e35d0 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 64  le.    ** that d
e35e0 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  oes not need to 
e35f0 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20  be locked. */.  
e3600 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b    return &nolock
e3610 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 20  IoMethods;.  }. 
e3620 20 69 66 28 20 73 74 61 74 66 73 28 66 69 6c 65   if( statfs(file
e3630 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 21  Path, &fsInfo) !
e3640 3d 20 2d 31 20 29 7b 0a 20 20 20 20 69 66 28 20  = -1 ){.    if( 
e3650 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 20 26  fsInfo.f_flags &
e3660 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 29 7b 0a 20   MNT_RDONLY ){. 
e3670 20 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c       return &nol
e3680 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20  ockIoMethods;.  
e3690 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
e36a0 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79   aMap[i].zFilesy
e36b0 73 74 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  stem; i++){.    
e36c0 20 20 69 66 28 20 73 74 72 63 6d 70 28 66 73 49    if( strcmp(fsI
e36d0 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65  nfo.f_fstypename
e36e0 2c 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73  , aMap[i].zFiles
e36f0 79 73 74 65 6d 29 3d 3d 30 20 29 7b 0a 20 20 20  ystem)==0 ){.   
e3700 20 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61 70       return aMap
e3710 5b 69 5d 2e 70 4d 65 74 68 6f 64 73 3b 0a 20 20  [i].pMethods;.  
e3720 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
e3730 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 61  .  /* Default ca
e3740 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20 61 6d 6f  se. Handles, amo
e3750 6e 67 73 74 20 6f 74 68 65 72 73 2c 20 22 6e 66  ngst others, "nf
e3760 73 22 2e 0a 20 20 2a 2a 20 54 65 73 74 20 62 79  s"..  ** Test by
e3770 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 75 73  te-range lock us
e3780 69 6e 67 20 66 63 6e 74 6c 28 29 2e 20 49 66 20  ing fcntl(). If 
e3790 74 68 65 20 63 61 6c 6c 20 73 75 63 63 65 65 64  the call succeed
e37a0 73 2c 20 0a 20 20 2a 2a 20 61 73 73 75 6d 65 20  s, .  ** assume 
e37b0 74 68 61 74 20 74 68 65 20 66 69 6c 65 2d 73 79  that the file-sy
e37c0 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 50 4f  stem supports PO
e37d0 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e  SIX style locks.
e37e0 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66   .  */.  lockInf
e37f0 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c  o.l_len = 1;.  l
e3800 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20  ockInfo.l_start 
e3810 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e  = 0;.  lockInfo.
e3820 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
e3830 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e  SET;.  lockInfo.
e3840 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b  l_type = F_RDLCK
e3850 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c 28 70 4e  ;.  if( fcntl(pN
e3860 65 77 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20  ew->h, F_GETLK, 
e3870 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29  &lockInfo)!=-1 )
e3880 20 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 66 69 6c   {.    pNew->fil
e3890 65 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f  eFlags = SQLITE_
e38a0 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49  WHOLE_FILE_LOCKI
e38b0 4e 47 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 26  NG;.    return &
e38c0 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a  posixIoMethods;.
e38d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
e38e0 75 72 6e 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65  urn &dotlockIoMe
e38f0 74 68 6f 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61  thods;.  }.}.sta
e3900 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
e3910 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20  3_io_methods .  
e3920 2a 28 2a 63 6f 6e 73 74 20 61 75 74 6f 6c 6f 63  *(*const autoloc
e3930 6b 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74  kIoFinder)(const
e3940 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a   char*,unixFile*
e3950 29 20 3d 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69  ) = autolockIoFi
e3960 6e 64 65 72 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69  nderImpl;..#endi
e3970 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41  f /* defined(__A
e3980 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54  PPLE__) && SQLIT
e3990 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
e39a0 5f 53 54 59 4c 45 20 2a 2f 0a 0a 23 69 66 20 4f  _STYLE */..#if O
e39b0 53 5f 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c  S_VXWORKS && SQL
e39c0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e39d0 4e 47 5f 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20  NG_STYLE./* .** 
e39e0 54 68 69 73 20 22 66 69 6e 64 65 72 22 20 66 75  This "finder" fu
e39f0 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20  nction attempts 
e3a00 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
e3a10 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74   best locking st
e3a20 72 61 74 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74  rategy .** for t
e3a30 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
e3a40 20 22 66 69 6c 65 50 61 74 68 22 2e 20 20 49 74   "filePath".  It
e3a50 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68   then returns th
e3a60 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  e sqlite3_io_met
e3a70 68 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74  hods.** object t
e3a80 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
e3a90 68 61 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a  hat strategy..**
e3aa0 0a 2a 2a 20 54 68 69 73 20 69 73 20 66 6f 72 20  .** This is for 
e3ab0 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 2e 0a 2a 2f  VXWorks only..*/
e3ac0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
e3ad0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
e3ae0 20 2a 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64   *autolockIoFind
e3af0 65 72 49 6d 70 6c 28 0a 20 20 63 6f 6e 73 74 20  erImpl(.  const 
e3b00 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68 2c 20  char *filePath, 
e3b10 20 20 20 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68     /* name of th
e3b20 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
e3b30 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  */.  unixFile *p
e3b40 4e 65 77 20 20 20 20 20 20 20 20 20 20 20 2f 2a  New           /*
e3b50 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 6f   the open file o
e3b60 62 6a 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 73 74  bject */.){.  st
e3b70 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49  ruct flock lockI
e3b80 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 69 6c  nfo;..  if( !fil
e3b90 65 50 61 74 68 20 29 7b 0a 20 20 20 20 2f 2a 20  ePath ){.    /* 
e3ba0 49 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c  If filePath==NUL
e3bb0 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65 20  L that means we 
e3bc0 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
e3bd0 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c   a transient fil
e3be0 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 64 6f  e.    ** that do
e3bf0 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
e3c00 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20  e locked. */.   
e3c10 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49   return &nolockI
e3c20 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 0a 20  oMethods;.  }.. 
e3c30 20 2f 2a 20 54 65 73 74 20 69 66 20 66 63 6e 74   /* Test if fcnt
e3c40 6c 28 29 20 69 73 20 73 75 70 70 6f 72 74 65 64  l() is supported
e3c50 20 61 6e 64 20 75 73 65 20 50 4f 53 49 58 20 73   and use POSIX s
e3c60 74 79 6c 65 20 6c 6f 63 6b 73 2e 0a 20 20 2a 2a  tyle locks..  **
e3c70 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20   Otherwise fall 
e3c80 62 61 63 6b 20 74 6f 20 74 68 65 20 6e 61 6d 65  back to the name
e3c90 64 20 73 65 6d 61 70 68 6f 72 65 20 6d 65 74 68  d semaphore meth
e3ca0 6f 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49  od..  */.  lockI
e3cb0 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20  nfo.l_len = 1;. 
e3cc0 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72   lockInfo.l_star
e3cd0 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66  t = 0;.  lockInf
e3ce0 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45  o.l_whence = SEE
e3cf0 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66  K_SET;.  lockInf
e3d00 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c  o.l_type = F_RDL
e3d10 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c 28  CK;.  if( fcntl(
e3d20 70 4e 65 77 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b  pNew->h, F_GETLK
e3d30 2c 20 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31  , &lockInfo)!=-1
e3d40 20 29 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20   ) {.    return 
e3d50 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b  &posixIoMethods;
e3d60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
e3d70 74 75 72 6e 20 26 73 65 6d 49 6f 4d 65 74 68 6f  turn &semIoMetho
e3d80 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  ds;.  }.}.static
e3d90 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e3da0 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a 28 2a  o_methods .  *(*
e3db0 63 6f 6e 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f  const autolockIo
e3dc0 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 63 68  Finder)(const ch
e3dd0 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 20 3d  ar*,unixFile*) =
e3de0 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65   autolockIoFinde
e3df0 72 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f  rImpl;..#endif /
e3e00 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 26 26 20  * OS_VXWORKS && 
e3e10 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
e3e20 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a  CKING_STYLE */..
e3e30 2f 2a 0a 2a 2a 20 41 6e 20 61 62 73 74 72 61 63  /*.** An abstrac
e3e40 74 20 74 79 70 65 20 66 6f 72 20 61 20 70 6f 69  t type for a poi
e3e50 6e 74 65 72 20 74 6f 20 61 20 49 4f 20 6d 65 74  nter to a IO met
e3e60 68 6f 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74  hod finder funct
e3e70 69 6f 6e 3a 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ion:.*/.typedef 
e3e80 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
e3e90 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 66 69 6e 64  _methods *(*find
e3ea0 65 72 5f 74 79 70 65 29 28 63 6f 6e 73 74 20 63  er_type)(const c
e3eb0 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 3b  har*,unixFile*);
e3ec0 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
e3ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3f10 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
e3f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71  ************* sq
e3f30 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64  lite3_vfs method
e3f40 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s **************
e3f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
e3f60 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 69 73 69  *.** This divisi
e3f70 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  on contains the 
e3f80 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
e3f90 66 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 65  f methods on the
e3fa0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 73 20  .** sqlite3_vfs 
e3fb0 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  object..*/../*.*
e3fc0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
e3fd0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
e3fe0 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74   unixFile struct
e3ff0 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ure pointed to b
e4000 79 20 70 49 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  y pId..*/.static
e4010 20 69 6e 74 20 66 69 6c 6c 49 6e 55 6e 69 78 46   int fillInUnixF
e4020 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ile(.  sqlite3_v
e4030 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 2f  fs *pVfs,      /
e4040 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73  * Pointer to vfs
e4050 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
e4060 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   h,             
e4070 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c       /* Open fil
e4080 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 66 20  e descriptor of 
e4090 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65  file being opene
e40a0 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64  d */.  int dirfd
e40b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e40c0 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 6c 65  * Directory file
e40d0 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20   descriptor */. 
e40e0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
e40f0 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74  Id,      /* Writ
e4100 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46 69 6c  e to the unixFil
e4110 65 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65  e structure here
e4120 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
e4130 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a   *zFilename,  /*
e4140 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
e4150 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a  e being opened *
e4160 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 2c 20  /.  int noLock, 
e4170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
e4180 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 69 66 20 74  mit locking if t
e4190 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44  rue */.  int isD
e41a0 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20  elete           
e41b0 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c   /* Delete on cl
e41c0 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  ose if true */.)
e41d0 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  {.  const sqlite
e41e0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4c  3_io_methods *pL
e41f0 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 75  ockingStyle;.  u
e4200 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 3d 20  nixFile *pNew = 
e4210 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 49 64 3b  (unixFile *)pId;
e4220 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
e4230 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74  TE_OK;..  assert
e4240 28 20 70 4e 65 77 2d 3e 70 4c 6f 63 6b 3d 3d 4e  ( pNew->pLock==N
e4250 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ULL );.  assert(
e4260 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 3d 3d 4e 55   pNew->pOpen==NU
e4270 4c 4c 20 29 3b 0a 0a 20 20 2f 2a 20 50 61 72 61  LL );..  /* Para
e4280 6d 65 74 65 72 20 69 73 44 65 6c 65 74 65 20 69  meter isDelete i
e4290 73 20 6f 6e 6c 79 20 75 73 65 64 20 6f 6e 20 76  s only used on v
e42a0 78 77 6f 72 6b 73 2e 20 45 78 70 72 65 73 73 20  xworks. Express 
e42b0 74 68 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20  this explicitly 
e42c0 0a 20 20 2a 2a 20 68 65 72 65 20 74 6f 20 70 72  .  ** here to pr
e42d0 65 76 65 6e 74 20 63 6f 6d 70 69 6c 65 72 20 77  event compiler w
e42e0 61 72 6e 69 6e 67 73 20 61 62 6f 75 74 20 75 6e  arnings about un
e42f0 75 73 65 64 20 70 61 72 61 6d 65 74 65 72 73 2e  used parameters.
e4300 0a 20 20 2a 2f 0a 20 20 55 4e 55 53 45 44 5f 50  .  */.  UNUSED_P
e4310 41 52 41 4d 45 54 45 52 28 69 73 44 65 6c 65 74  ARAMETER(isDelet
e4320 65 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 33 28  e);..  OSTRACE3(
e4330 22 4f 50 45 4e 20 20 20 20 25 2d 33 64 20 25 73  "OPEN    %-3d %s
e4340 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d  \n", h, zFilenam
e4350 65 29 3b 20 20 20 20 0a 20 20 70 4e 65 77 2d 3e  e);    .  pNew->
e4360 68 20 3d 20 68 3b 0a 20 20 70 4e 65 77 2d 3e 64  h = h;.  pNew->d
e4370 69 72 66 64 20 3d 20 64 69 72 66 64 3b 0a 20 20  irfd = dirfd;.  
e4380 53 45 54 5f 54 48 52 45 41 44 49 44 28 70 4e 65  SET_THREADID(pNe
e4390 77 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 6c 65  w);.  pNew->file
e43a0 46 6c 61 67 73 20 3d 20 30 3b 0a 0a 23 69 66 20  Flags = 0;..#if 
e43b0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 70 4e 65  OS_VXWORKS.  pNe
e43c0 77 2d 3e 70 49 64 20 3d 20 76 78 77 6f 72 6b 73  w->pId = vxworks
e43d0 46 69 6e 64 46 69 6c 65 49 64 28 7a 46 69 6c 65  FindFileId(zFile
e43e0 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4e 65  name);.  if( pNe
e43f0 77 2d 3e 70 49 64 3d 3d 30 20 29 7b 0a 20 20 20  w->pId==0 ){.   
e4400 20 6e 6f 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20   noLock = 1;.   
e4410 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
e4420 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  EM;.  }.#endif..
e4430 20 20 69 66 28 20 6e 6f 4c 6f 63 6b 20 29 7b 0a    if( noLock ){.
e4440 20 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c      pLockingStyl
e4450 65 20 3d 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74  e = &nolockIoMet
e4460 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  hods;.  }else{. 
e4470 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65     pLockingStyle
e4480 20 3d 20 28 2a 2a 28 66 69 6e 64 65 72 5f 74 79   = (**(finder_ty
e4490 70 65 2a 29 70 56 66 73 2d 3e 70 41 70 70 44 61  pe*)pVfs->pAppDa
e44a0 74 61 29 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 70  ta)(zFilename, p
e44b0 4e 65 77 29 3b 0a 23 69 66 20 53 51 4c 49 54 45  New);.#if SQLITE
e44c0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
e44d0 53 54 59 4c 45 0a 20 20 20 20 2f 2a 20 43 61 63  STYLE.    /* Cac
e44e0 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 69 6e 20  he zFilename in 
e44f0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74  the locking cont
e4500 65 78 74 20 28 41 46 50 20 61 6e 64 20 64 6f 74  ext (AFP and dot
e4510 6c 6f 63 6b 20 6f 76 65 72 72 69 64 65 29 20 66  lock override) f
e4520 6f 72 0a 20 20 20 20 2a 2a 20 70 72 6f 78 79 4c  or.    ** proxyL
e4530 6f 63 6b 20 61 63 74 69 76 61 74 69 6f 6e 20 69  ock activation i
e4540 73 20 70 6f 73 73 69 62 6c 65 20 28 72 65 6d 6f  s possible (remo
e4550 74 65 20 70 72 6f 78 79 20 69 73 20 62 61 73 65  te proxy is base
e4560 64 20 6f 6e 20 64 62 20 6e 61 6d 65 29 0a 20 20  d on db name).  
e4570 20 20 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 72    ** zFilename r
e4580 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74  emains valid unt
e4590 69 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65  il file is close
e45a0 64 2c 20 74 6f 20 73 75 70 70 6f 72 74 20 2a 2f  d, to support */
e45b0 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69  .    pNew->locki
e45c0 6e 67 43 6f 6e 74 65 78 74 20 3d 20 28 76 6f 69  ngContext = (voi
e45d0 64 2a 29 7a 46 69 6c 65 6e 61 6d 65 3b 0a 23 65  d*)zFilename;.#e
e45e0 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 69 66 28 20  ndif.  }..  if( 
e45f0 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d  pLockingStyle ==
e4600 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73   &posixIoMethods
e4610 20 29 7b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65   ){.    unixEnte
e4620 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 63  rMutex();.    rc
e4630 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28   = findLockInfo(
e4640 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f  pNew, &pNew->pLo
e4650 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 65 6e  ck, &pNew->pOpen
e4660 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
e4670 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e4680 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72    /* If an error
e4690 20 6f 63 63 75 72 65 64 20 69 6e 20 66 69 6e 64   occured in find
e46a0 4c 6f 63 6b 49 6e 66 6f 28 29 2c 20 63 6c 6f 73  LockInfo(), clos
e46b0 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  e the file descr
e46c0 69 70 74 6f 72 0a 20 20 20 20 20 20 2a 2a 20 69  iptor.      ** i
e46d0 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f  mmediately, befo
e46e0 72 65 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65  re releasing the
e46f0 20 6d 75 74 65 78 2e 20 66 69 6e 64 4c 6f 63 6b   mutex. findLock
e4700 49 6e 66 6f 28 29 20 6d 61 79 20 66 61 69 6c 0a  Info() may fail.
e4710 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 77 6f 20        ** in two 
e4720 73 63 65 6e 61 72 69 6f 73 3a 0a 20 20 20 20 20  scenarios:.     
e4730 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 28   **.      **   (
e4740 61 29 20 41 20 63 61 6c 6c 20 74 6f 20 66 73 74  a) A call to fst
e4750 61 74 28 29 20 66 61 69 6c 65 64 2e 0a 20 20 20  at() failed..   
e4760 20 20 20 2a 2a 20 20 20 28 62 29 20 41 20 6d 61     **   (b) A ma
e4770 6c 6c 6f 63 20 66 61 69 6c 65 64 2e 0a 20 20 20  lloc failed..   
e4780 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53     **.      ** S
e4790 63 65 6e 61 72 69 6f 20 28 62 29 20 6d 61 79 20  cenario (b) may 
e47a0 6f 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 74 68  only occur if th
e47b0 65 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c  e process is hol
e47c0 64 69 6e 67 20 6e 6f 20 6f 74 68 65 72 0a 20 20  ding no other.  
e47d0 20 20 20 20 2a 2a 20 66 69 6c 65 20 64 65 73 63      ** file desc
e47e0 72 69 70 74 6f 72 73 20 6f 70 65 6e 20 6f 6e 20  riptors open on 
e47f0 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 20 49  the same file. I
e4800 66 20 74 68 65 72 65 20 77 65 72 65 20 6f 74 68  f there were oth
e4810 65 72 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a  er file.      **
e4820 20 64 65 73 63 72 69 70 74 6f 72 73 20 6f 6e 20   descriptors on 
e4830 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20  this file, then 
e4840 6e 6f 20 6d 61 6c 6c 6f 63 20 77 6f 75 6c 64 20  no malloc would 
e4850 62 65 20 72 65 71 75 69 72 65 64 20 62 79 0a 20  be required by. 
e4860 20 20 20 20 20 2a 2a 20 66 69 6e 64 4c 6f 63 6b       ** findLock
e4870 49 6e 66 6f 28 29 2e 20 49 66 20 74 68 69 73 20  Info(). If this 
e4880 69 73 20 74 68 65 20 63 61 73 65 2c 20 69 74 20  is the case, it 
e4890 69 73 20 71 75 69 74 65 20 73 61 66 65 20 74 6f  is quite safe to
e48a0 20 63 6c 6f 73 65 0a 20 20 20 20 20 20 2a 2a 20   close.      ** 
e48b0 68 61 6e 64 6c 65 20 68 20 2d 20 61 73 20 69 74  handle h - as it
e48c0 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
e48d0 68 61 74 20 6e 6f 20 70 6f 73 69 78 20 6c 6f 63  hat no posix loc
e48e0 6b 73 20 77 69 6c 6c 20 62 65 20 72 65 6c 65 61  ks will be relea
e48f0 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20  sed.      ** by 
e4900 64 6f 69 6e 67 20 73 6f 2e 0a 20 20 20 20 20 20  doing so..      
e4910 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 73  **.      ** If s
e4920 63 65 6e 61 72 69 6f 20 28 61 29 20 63 61 75 73  cenario (a) caus
e4930 65 64 20 74 68 65 20 65 72 72 6f 72 20 74 68 65  ed the error the
e4940 6e 20 74 68 69 6e 67 73 20 61 72 65 20 6e 6f 74  n things are not
e4950 20 73 6f 20 73 61 66 65 2e 20 54 68 65 0a 20 20   so safe. The.  
e4960 20 20 20 20 2a 2a 20 69 6d 70 6c 69 63 69 74 20      ** implicit 
e4970 61 73 73 75 6d 70 74 69 6f 6e 20 68 65 72 65 20  assumption here 
e4980 69 73 20 74 68 61 74 20 69 66 20 66 73 74 61 74  is that if fstat
e4990 28 29 20 66 61 69 6c 73 2c 20 74 68 69 6e 67 73  () fails, things
e49a0 20 61 72 65 20 69 6e 0a 20 20 20 20 20 20 2a 2a   are in.      **
e49b0 20 73 75 63 68 20 62 61 64 20 73 68 61 70 65 20   such bad shape 
e49c0 74 68 61 74 20 64 72 6f 70 70 69 6e 67 20 61 20  that dropping a 
e49d0 6c 6f 63 6b 20 6f 72 20 74 77 6f 20 64 6f 65 73  lock or two does
e49e0 6e 27 74 20 6d 61 74 74 65 72 20 6d 75 63 68 2e  n't matter much.
e49f0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
e4a00 63 6c 6f 73 65 28 68 29 3b 0a 20 20 20 20 20 20  close(h);.      
e4a10 68 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20  h = -1;.    }.  
e4a20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
e4a30 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 20 53 51 4c  ();.  }..#if SQL
e4a40 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e4a50 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69  NG_STYLE && defi
e4a60 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20  ned(__APPLE__). 
e4a70 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b 69   else if( pLocki
e4a80 6e 67 53 74 79 6c 65 20 3d 3d 20 26 61 66 70 49  ngStyle == &afpI
e4a90 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
e4aa0 2f 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 75  /* AFP locking u
e4ab0 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74  ses the file pat
e4ac0 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f  h so it needs to
e4ad0 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a   be included in.
e4ae0 20 20 20 20 2a 2a 20 74 68 65 20 61 66 70 4c 6f      ** the afpLo
e4af0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2e 0a 20 20  ckingContext..  
e4b00 20 20 2a 2f 0a 20 20 20 20 61 66 70 4c 6f 63 6b    */.    afpLock
e4b10 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78  ingContext *pCtx
e4b20 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b  ;.    pNew->lock
e4b30 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74  ingContext = pCt
e4b40 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  x = sqlite3_mall
e4b50 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78  oc( sizeof(*pCtx
e4b60 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 74  ) );.    if( pCt
e4b70 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63  x==0 ){.      rc
e4b80 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
e4b90 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
e4ba0 20 20 2f 2a 20 4e 42 3a 20 7a 46 69 6c 65 6e 61    /* NB: zFilena
e4bb0 6d 65 20 65 78 69 73 74 73 20 61 6e 64 20 72 65  me exists and re
e4bc0 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69  mains valid unti
e4bd0 6c 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 6c  l the file is cl
e4be0 6f 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 61 63  osed.      ** ac
e4bf0 63 6f 72 64 69 6e 67 20 74 6f 20 72 65 71 75 69  cording to requi
e4c00 72 65 6d 65 6e 74 20 46 31 31 31 34 31 2e 20 20  rement F11141.  
e4c10 53 6f 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65  So we do not nee
e4c20 64 20 74 6f 20 6d 61 6b 65 20 61 0a 20 20 20 20  d to make a.    
e4c30 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74 68 65    ** copy of the
e4c40 20 66 69 6c 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20   filename. */.  
e4c50 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74 68      pCtx->dbPath
e4c60 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20   = zFilename;.  
e4c70 20 20 20 20 73 72 61 6e 64 6f 6d 64 65 76 28 29      srandomdev()
e4c80 3b 0a 20 20 20 20 20 20 75 6e 69 78 45 6e 74 65  ;.      unixEnte
e4c90 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20  rMutex();.      
e4ca0 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66  rc = findLockInf
e4cb0 6f 28 70 4e 65 77 2c 20 4e 55 4c 4c 2c 20 26 70  o(pNew, NULL, &p
e4cc0 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20  New->pOpen);.   
e4cd0 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65     unixLeaveMute
e4ce0 78 28 29 3b 20 20 20 20 20 20 20 20 0a 20 20 20  x();        .   
e4cf0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
e4d00 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b 69   else if( pLocki
e4d10 6e 67 53 74 79 6c 65 20 3d 3d 20 26 64 6f 74 6c  ngStyle == &dotl
e4d20 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a  ockIoMethods ){.
e4d30 20 20 20 20 2f 2a 20 44 6f 74 66 69 6c 65 20 6c      /* Dotfile l
e4d40 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20  ocking uses the 
e4d50 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20  file path so it 
e4d60 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c  needs to be incl
e4d70 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74  uded in.    ** t
e4d80 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e  he dotlockLockin
e4d90 67 43 6f 6e 74 65 78 74 20 0a 20 20 20 20 2a 2f  gContext .    */
e4da0 0a 20 20 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b  .    char *zLock
e4db0 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 20 6e 46  File;.    int nF
e4dc0 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 6e 46 69  ilename;.    nFi
e4dd0 6c 65 6e 61 6d 65 20 3d 20 28 69 6e 74 29 73 74  lename = (int)st
e4de0 72 6c 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 29 20  rlen(zFilename) 
e4df0 2b 20 36 3b 0a 20 20 20 20 7a 4c 6f 63 6b 46 69  + 6;.    zLockFi
e4e00 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  le = (char *)sql
e4e10 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 46 69 6c  ite3_malloc(nFil
e4e20 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  ename);.    if( 
e4e30 7a 4c 6f 63 6b 46 69 6c 65 3d 3d 30 20 29 7b 0a  zLockFile==0 ){.
e4e40 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e4e50 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c  E_NOMEM;.    }el
e4e60 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
e4e70 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 69 6c 65  3_snprintf(nFile
e4e80 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 6c 65 2c  name, zLockFile,
e4e90 20 22 25 73 22 20 44 4f 54 4c 4f 43 4b 5f 53 55   "%s" DOTLOCK_SU
e4ea0 46 46 49 58 2c 20 7a 46 69 6c 65 6e 61 6d 65 29  FFIX, zFilename)
e4eb0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77  ;.    }.    pNew
e4ec0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
e4ed0 20 3d 20 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20   = zLockFile;.  
e4ee0 7d 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  }..#if OS_VXWORK
e4ef0 53 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f  S.  else if( pLo
e4f00 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 73  ckingStyle == &s
e4f10 65 6d 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20  emIoMethods ){. 
e4f20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 73 65 6d 61     /* Named sema
e4f30 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 75 73  phore locking us
e4f40 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74 68  es the file path
e4f50 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20   so it needs to 
e4f60 62 65 0a 20 20 20 20 2a 2a 20 69 6e 63 6c 75 64  be.    ** includ
e4f70 65 64 20 69 6e 20 74 68 65 20 73 65 6d 4c 6f 63  ed in the semLoc
e4f80 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 20 20 20 20  kingContext.    
e4f90 2a 2f 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72  */.    unixEnter
e4fa0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 63 20  Mutex();.    rc 
e4fb0 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70  = findLockInfo(p
e4fc0 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63  New, &pNew->pLoc
e4fd0 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29  k, &pNew->pOpen)
e4fe0 3b 0a 20 20 20 20 69 66 28 20 28 72 63 3d 3d 53  ;.    if( (rc==S
e4ff0 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 70 4e  QLITE_OK) && (pN
e5000 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3d  ew->pOpen->pSem=
e5010 3d 4e 55 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20  =NULL) ){.      
e5020 63 68 61 72 20 2a 7a 53 65 6d 4e 61 6d 65 20 3d  char *zSemName =
e5030 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53   pNew->pOpen->aS
e5040 65 6d 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 6e  emName;.      in
e5050 74 20 6e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  t n;.      sqlit
e5060 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f  e3_snprintf(MAX_
e5070 50 41 54 48 4e 41 4d 45 2c 20 7a 53 65 6d 4e 61  PATHNAME, zSemNa
e5080 6d 65 2c 20 22 2f 25 73 2e 73 65 6d 22 2c 0a 20  me, "/%s.sem",. 
e5090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e50a0 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 49 64 2d        pNew->pId-
e50b0 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29  >zCanonicalName)
e50c0 3b 0a 20 20 20 20 20 20 66 6f 72 28 20 6e 3d 31  ;.      for( n=1
e50d0 3b 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3b 20 6e  ; zSemName[n]; n
e50e0 2b 2b 20 29 0a 20 20 20 20 20 20 20 20 69 66 28  ++ ).        if(
e50f0 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3d 3d 27 2f   zSemName[n]=='/
e5100 27 20 29 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 20  ' ) zSemName[n] 
e5110 3d 20 27 5f 27 3b 0a 20 20 20 20 20 20 70 4e 65  = '_';.      pNe
e5120 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d  w->pOpen->pSem =
e5130 20 73 65 6d 5f 6f 70 65 6e 28 7a 53 65 6d 4e 61   sem_open(zSemNa
e5140 6d 65 2c 20 4f 5f 43 52 45 41 54 2c 20 30 36 36  me, O_CREAT, 066
e5150 36 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28  6, 1);.      if(
e5160 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53   pNew->pOpen->pS
e5170 65 6d 20 3d 3d 20 53 45 4d 5f 46 41 49 4c 45 44  em == SEM_FAILED
e5180 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
e5190 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
e51a0 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70         pNew->pOp
e51b0 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b 30 5d 20  en->aSemName[0] 
e51c0 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 7d 0a  = '\0';.      }.
e51d0 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65      }.    unixLe
e51e0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  aveMutex();.  }.
e51f0 23 65 6e 64 69 66 0a 20 20 0a 20 20 70 4e 65 77  #endif.  .  pNew
e5200 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b  ->lastErrno = 0;
e5210 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
e5220 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
e5230 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 69 6e  _OK ){.    unlin
e5240 6b 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  k(zFilename);.  
e5250 20 20 69 73 44 65 6c 65 74 65 20 3d 20 30 3b 0a    isDelete = 0;.
e5260 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 69 73 44 65    }.  pNew->isDe
e5270 6c 65 74 65 20 3d 20 69 73 44 65 6c 65 74 65 3b  lete = isDelete;
e5280 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 72 63  .#endif.  if( rc
e5290 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
e52a0 20 20 20 69 66 28 20 64 69 72 66 64 3e 3d 30 20     if( dirfd>=0 
e52b0 29 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20  ) close(dirfd); 
e52c0 2f 2a 20 73 69 6c 65 6e 74 20 6c 65 61 6b 20 69  /* silent leak i
e52d0 66 20 66 61 69 6c 2c 20 61 6c 72 65 61 64 79 20  f fail, already 
e52e0 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  in error */.    
e52f0 69 66 28 20 68 3e 3d 30 20 29 20 63 6c 6f 73 65  if( h>=0 ) close
e5300 28 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  (h);.  }else{.  
e5310 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20    pNew->pMethod 
e5320 3d 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b  = pLockingStyle;
e5330 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72  .    OpenCounter
e5340 28 2b 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  (+1);.  }.  retu
e5350 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
e5360 4f 70 65 6e 20 61 20 66 69 6c 65 20 64 65 73 63  Open a file desc
e5370 72 69 70 74 6f 72 20 74 6f 20 74 68 65 20 64 69  riptor to the di
e5380 72 65 63 74 6f 72 79 20 63 6f 6e 74 61 69 6e 69  rectory containi
e5390 6e 67 20 66 69 6c 65 20 7a 46 69 6c 65 6e 61 6d  ng file zFilenam
e53a0 65 2e 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73  e..** If success
e53b0 66 75 6c 2c 20 2a 70 46 64 20 69 73 20 73 65 74  ful, *pFd is set
e53c0 20 74 6f 20 74 68 65 20 6f 70 65 6e 65 64 20 66   to the opened f
e53d0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 61  ile descriptor a
e53e0 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  nd.** SQLITE_OK 
e53f0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
e5400 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
e5410 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4e   either SQLITE_N
e5420 4f 4d 45 4d 0a 2a 2a 20 6f 72 20 53 51 4c 49 54  OMEM.** or SQLIT
e5430 45 5f 43 41 4e 54 4f 50 45 4e 20 69 73 20 72 65  E_CANTOPEN is re
e5440 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 46 64 20  turned and *pFd 
e5450 69 73 20 73 65 74 20 74 6f 20 61 6e 20 75 6e 64  is set to an und
e5460 65 66 69 6e 65 64 0a 2a 2a 20 76 61 6c 75 65 2e  efined.** value.
e5470 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  .**.** If SQLITE
e5480 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c  _OK is returned,
e5490 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72   the caller is r
e54a0 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63  esponsible for c
e54b0 6c 6f 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69  losing.** the fi
e54c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 70  le descriptor *p
e54d0 46 64 20 75 73 69 6e 67 20 63 6c 6f 73 65 28 29  Fd using close()
e54e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e54f0 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 63 6f  openDirectory(co
e5500 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
e5510 61 6d 65 2c 20 69 6e 74 20 2a 70 46 64 29 7b 0a  ame, int *pFd){.
e5520 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20    int ii;.  int 
e5530 66 64 20 3d 20 2d 31 3b 0a 20 20 63 68 61 72 20  fd = -1;.  char 
e5540 7a 44 69 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 54  zDirname[MAX_PAT
e5550 48 4e 41 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 6c  HNAME+1];..  sql
e5560 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41  ite3_snprintf(MA
e5570 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 44 69 72  X_PATHNAME, zDir
e5580 6e 61 6d 65 2c 20 22 25 73 22 2c 20 7a 46 69 6c  name, "%s", zFil
e5590 65 6e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 69  ename);.  for(ii
e55a0 3d 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 44 69  =(int)strlen(zDi
e55b0 72 6e 61 6d 65 29 3b 20 69 69 3e 31 20 26 26 20  rname); ii>1 && 
e55c0 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 21 3d 27 2f  zDirname[ii]!='/
e55d0 27 3b 20 69 69 2d 2d 29 3b 0a 20 20 69 66 28 20  '; ii--);.  if( 
e55e0 69 69 3e 30 20 29 7b 0a 20 20 20 20 7a 44 69 72  ii>0 ){.    zDir
e55f0 6e 61 6d 65 5b 69 69 5d 20 3d 20 27 5c 30 27 3b  name[ii] = '\0';
e5600 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a  .    fd = open(z
e5610 44 69 72 6e 61 6d 65 2c 20 4f 5f 52 44 4f 4e 4c  Dirname, O_RDONL
e5620 59 7c 4f 5f 42 49 4e 41 52 59 2c 20 30 29 3b 0a  Y|O_BINARY, 0);.
e5630 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b      if( fd>=0 ){
e5640 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f 45 58  .#ifdef FD_CLOEX
e5650 45 43 0a 20 20 20 20 20 20 66 63 6e 74 6c 28 66  EC.      fcntl(f
e5660 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e 74  d, F_SETFD, fcnt
e5670 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 30  l(fd, F_GETFD, 0
e5680 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b  ) | FD_CLOEXEC);
e5690 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 4f 53  .#endif.      OS
e56a0 54 52 41 43 45 33 28 22 4f 50 45 4e 44 49 52 20  TRACE3("OPENDIR 
e56b0 25 2d 33 64 20 25 73 5c 6e 22 2c 20 66 64 2c 20  %-3d %s\n", fd, 
e56c0 7a 44 69 72 6e 61 6d 65 29 3b 0a 20 20 20 20 7d  zDirname);.    }
e56d0 0a 20 20 7d 0a 20 20 2a 70 46 64 20 3d 20 66 64  .  }.  *pFd = fd
e56e0 3b 0a 20 20 72 65 74 75 72 6e 20 28 66 64 3e 3d  ;.  return (fd>=
e56f0 30 3f 53 51 4c 49 54 45 5f 4f 4b 3a 53 51 4c 49  0?SQLITE_OK:SQLI
e5700 54 45 5f 43 41 4e 54 4f 50 45 4e 29 3b 0a 7d 0a  TE_CANTOPEN);.}.
e5710 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
e5720 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e  temporary file n
e5730 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42  ame in zBuf.  zB
e5740 75 66 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63  uf must be alloc
e5750 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 63  ated.** by the c
e5760 61 6c 6c 69 6e 67 20 70 72 6f 63 65 73 73 20 61  alling process a
e5770 6e 64 20 6d 75 73 74 20 62 65 20 62 69 67 20 65  nd must be big e
e5780 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 61 74  nough to hold at
e5790 20 6c 65 61 73 74 0a 2a 2a 20 70 56 66 73 2d 3e   least.** pVfs->
e57a0 6d 78 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73  mxPathname bytes
e57b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e57c0 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20  getTempname(int 
e57d0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
e57e0 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
e57f0 74 20 63 68 61 72 20 2a 61 7a 44 69 72 73 5b 5d  t char *azDirs[]
e5800 20 3d 20 7b 0a 20 20 20 20 20 30 2c 0a 20 20 20   = {.     0,.   
e5810 20 20 30 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f    0,.     "/var/
e5820 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 75 73 72  tmp",.     "/usr
e5830 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d  /tmp",.     "/tm
e5840 70 22 2c 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20  p",.     ".",.  
e5850 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
e5860 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
e5870 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22  zChars[] =.    "
e5880 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70  abcdefghijklmnop
e5890 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20  qrstuvwxyz".    
e58a0 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  "ABCDEFGHIJKLMNO
e58b0 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20  PQRSTUVWXYZ".   
e58c0 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20   "0123456789";. 
e58d0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c   unsigned int i,
e58e0 20 6a 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61   j;.  struct sta
e58f0 74 20 62 75 66 3b 0a 20 20 63 6f 6e 73 74 20 63  t buf;.  const c
e5900 68 61 72 20 2a 7a 44 69 72 20 3d 20 22 2e 22 3b  har *zDir = ".";
e5910 0a 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20  ..  /* It's odd 
e5920 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69  to simulate an i
e5930 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75  o-error here, bu
e5940 74 20 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73  t really this is
e5950 20 6a 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67   just.  ** using
e5960 20 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e   the io-error in
e5970 66 72 61 73 74 72 75 63 74 75 72 65 20 74 6f 20  frastructure to 
e5980 74 65 73 74 20 74 68 61 74 20 53 51 4c 69 74 65  test that SQLite
e5990 20 68 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 20   handles this.  
e59a0 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c  ** function fail
e59b0 69 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20 53 69 6d  ing. .  */.  Sim
e59c0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65  ulateIOError( re
e59d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
e59e0 52 20 29 3b 0a 0a 20 20 61 7a 44 69 72 73 5b 30  R );..  azDirs[0
e59f0 5d 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d 70  ] = sqlite3_temp
e5a00 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 69 66  _directory;.  if
e5a10 20 28 4e 55 4c 4c 20 3d 3d 20 61 7a 44 69 72 73   (NULL == azDirs
e5a20 5b 31 5d 29 20 7b 0a 20 20 20 20 61 7a 44 69 72  [1]) {.    azDir
e5a30 73 5b 31 5d 20 3d 20 67 65 74 65 6e 76 28 22 54  s[1] = getenv("T
e5a40 4d 50 44 49 52 22 29 3b 0a 20 20 7d 0a 20 20 0a  MPDIR");.  }.  .
e5a50 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a    for(i=0; i<siz
e5a60 65 6f 66 28 61 7a 44 69 72 73 29 2f 73 69 7a 65  eof(azDirs)/size
e5a70 6f 66 28 61 7a 44 69 72 73 5b 30 5d 29 3b 20 69  of(azDirs[0]); i
e5a80 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 7a 44  ++){.    if( azD
e5a90 69 72 73 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  irs[i]==0 ) cont
e5aa0 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 73 74  inue;.    if( st
e5ab0 61 74 28 61 7a 44 69 72 73 5b 69 5d 2c 20 26 62  at(azDirs[i], &b
e5ac0 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  uf) ) continue;.
e5ad0 20 20 20 20 69 66 28 20 21 53 5f 49 53 44 49 52      if( !S_ISDIR
e5ae0 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 29 20  (buf.st_mode) ) 
e5af0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66  continue;.    if
e5b00 28 20 61 63 63 65 73 73 28 61 7a 44 69 72 73 5b  ( access(azDirs[
e5b10 69 5d 2c 20 30 37 29 20 29 20 63 6f 6e 74 69 6e  i], 07) ) contin
e5b20 75 65 3b 0a 20 20 20 20 7a 44 69 72 20 3d 20 61  ue;.    zDir = a
e5b30 7a 44 69 72 73 5b 69 5d 3b 0a 20 20 20 20 62 72  zDirs[i];.    br
e5b40 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  eak;.  }..  /* C
e5b50 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 6f 75  heck that the ou
e5b60 74 70 75 74 20 62 75 66 66 65 72 20 69 73 20 6c  tput buffer is l
e5b70 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f 72 20  arge enough for 
e5b80 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69  the temporary fi
e5b90 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20 49  le .  ** name. I
e5ba0 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 65 74  f it is not, ret
e5bb0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
e5bc0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 73 74  ..  */.  if( (st
e5bd0 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73 74 72  rlen(zDir) + str
e5be0 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f  len(SQLITE_TEMP_
e5bf0 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b 20 31  FILE_PREFIX) + 1
e5c00 37 29 20 3e 3d 20 28 73 69 7a 65 5f 74 29 6e 42  7) >= (size_t)nB
e5c10 75 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  uf ){.    return
e5c20 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
e5c30 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 73 71   }..  do{.    sq
e5c40 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
e5c50 42 75 66 2d 31 37 2c 20 7a 42 75 66 2c 20 22 25  Buf-17, zBuf, "%
e5c60 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46  s/"SQLITE_TEMP_F
e5c70 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 44 69 72  ILE_PREFIX, zDir
e5c80 29 3b 0a 20 20 20 20 6a 20 3d 20 28 69 6e 74 29  );.    j = (int)
e5c90 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20  strlen(zBuf);.  
e5ca0 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
e5cb0 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66 5b 6a  ness(15, &zBuf[j
e5cc0 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ]);.    for(i=0;
e5cd0 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29   i<15; i++, j++)
e5ce0 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20  {.      zBuf[j] 
e5cf0 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20  = (char)zChars[ 
e5d00 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  ((unsigned char)
e5d10 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66  zBuf[j])%(sizeof
e5d20 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20  (zChars)-1) ];. 
e5d30 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6a 5d     }.    zBuf[j]
e5d40 20 3d 20 30 3b 0a 20 20 7d 77 68 69 6c 65 28 20   = 0;.  }while( 
e5d50 61 63 63 65 73 73 28 7a 42 75 66 2c 30 29 3d 3d  access(zBuf,0)==
e5d60 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  0 );.  return SQ
e5d70 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20  LITE_OK;.}..#if 
e5d80 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
e5d90 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64  CKING_STYLE && d
e5da0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
e5db0 29 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20  )./*.** Routine 
e5dc0 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 61 20 75  to transform a u
e5dd0 6e 69 78 46 69 6c 65 20 69 6e 74 6f 20 61 20 70  nixFile into a p
e5de0 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 75 6e 69  roxy-locking uni
e5df0 78 46 69 6c 65 2e 0a 2a 2a 20 49 6d 70 6c 65 6d  xFile..** Implem
e5e00 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 65 20  entation in the 
e5e10 70 72 6f 78 79 2d 6c 6f 63 6b 20 64 69 76 69 73  proxy-lock divis
e5e20 69 6f 6e 2c 20 62 75 74 20 75 73 65 64 20 62 79  ion, but used by
e5e30 20 75 6e 69 78 4f 70 65 6e 28 29 0a 2a 2a 20 69   unixOpen().** i
e5e40 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f  f SQLITE_PREFER_
e5e50 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 69 73  PROXY_LOCKING is
e5e60 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61   defined..*/.sta
e5e70 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54 72 61  tic int proxyTra
e5e80 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 75  nsformUnixFile(u
e5e90 6e 69 78 46 69 6c 65 2a 2c 20 63 6f 6e 73 74 20  nixFile*, const 
e5ea0 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  char*);.#endif..
e5eb0 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f 72  /*.** Search for
e5ec0 20 61 6e 20 75 6e 75 73 65 64 20 66 69 6c 65 20   an unused file 
e5ed0 64 65 73 63 72 69 70 74 6f 72 20 74 68 61 74 20  descriptor that 
e5ee0 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68  was opened on th
e5ef0 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 66  e database .** f
e5f00 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e  ile (not a journ
e5f10 61 6c 20 6f 72 20 6d 61 73 74 65 72 2d 6a 6f 75  al or master-jou
e5f20 72 6e 61 6c 20 66 69 6c 65 29 20 69 64 65 6e 74  rnal file) ident
e5f30 69 66 69 65 64 20 62 79 20 70 61 74 68 6e 61 6d  ified by pathnam
e5f40 65 0a 2a 2a 20 7a 50 61 74 68 20 77 69 74 68 20  e.** zPath with 
e5f50 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 58 58 58 20  SQLITE_OPEN_XXX 
e5f60 66 6c 61 67 73 20 6d 61 74 63 68 69 6e 67 20 74  flags matching t
e5f70 68 6f 73 65 20 70 61 73 73 65 64 20 61 73 20 74  hose passed as t
e5f80 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67  he second.** arg
e5f90 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75  ument to this fu
e5fa0 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 75  nction..**.** Su
e5fb0 63 68 20 61 20 66 69 6c 65 20 64 65 73 63 72 69  ch a file descri
e5fc0 70 74 6f 72 20 6d 61 79 20 65 78 69 73 74 20 69  ptor may exist i
e5fd0 66 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  f a database con
e5fe0 6e 65 63 74 69 6f 6e 20 77 61 73 20 63 6c 6f 73  nection was clos
e5ff0 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20 61 73  ed.** but the as
e6000 73 6f 63 69 61 74 65 64 20 66 69 6c 65 20 64 65  sociated file de
e6010 73 63 72 69 70 74 6f 72 20 63 6f 75 6c 64 20 6e  scriptor could n
e6020 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 62 65 63  ot be closed bec
e6030 61 75 73 65 20 73 6f 6d 65 0a 2a 2a 20 6f 74 68  ause some.** oth
e6040 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  er file descript
e6050 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73  or open on the s
e6060 61 6d 65 20 66 69 6c 65 20 69 73 20 68 6f 6c 64  ame file is hold
e6070 69 6e 67 20 61 20 66 69 6c 65 2d 6c 6f 63 6b 2e  ing a file-lock.
e6080 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 63 6f 6d  .** Refer to com
e6090 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 75 6e 69  ments in the uni
e60a0 78 43 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 6f  xClose() functio
e60b0 6e 20 61 6e 64 20 74 68 65 20 6c 65 6e 67 74 68  n and the length
e60c0 79 20 63 6f 6d 6d 65 6e 74 0a 2a 2a 20 64 65 73  y comment.** des
e60d0 63 72 69 62 69 6e 67 20 22 50 6f 73 69 78 20 41  cribing "Posix A
e60e0 64 76 69 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 22  dvisory Locking"
e60f0 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
e6100 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 0a   this file for .
e6110 2a 2a 20 66 75 72 74 68 65 72 20 64 65 74 61 69  ** further detai
e6120 6c 73 2e 20 41 6c 73 6f 2c 20 74 69 63 6b 65 74  ls. Also, ticket
e6130 20 23 34 30 31 38 2e 0a 2a 2a 0a 2a 2a 20 49 66   #4018..**.** If
e6140 20 61 20 73 75 69 74 61 62 6c 65 20 66 69 6c 65   a suitable file
e6150 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 66   descriptor is f
e6160 6f 75 6e 64 2c 20 74 68 65 6e 20 69 74 20 69 73  ound, then it is
e6170 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 6e 6f   returned. If no
e6180 0a 2a 2a 20 73 75 63 68 20 66 69 6c 65 20 64 65  .** such file de
e6190 73 63 72 69 70 74 6f 72 20 69 73 20 6c 6f 63 61  scriptor is loca
e61a0 74 65 64 2c 20 2d 31 20 69 73 20 72 65 74 75 72  ted, -1 is retur
e61b0 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 55  ned..*/.static U
e61c0 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 66 69 6e  nixUnusedFd *fin
e61d0 64 52 65 75 73 61 62 6c 65 46 64 28 63 6f 6e 73  dReusableFd(cons
e61e0 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69  t char *zPath, i
e61f0 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 55 6e 69  nt flags){.  Uni
e6200 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73  xUnusedFd *pUnus
e6210 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44 6f  ed = 0;..  /* Do
e6220 20 6e 6f 74 20 73 65 61 72 63 68 20 66 6f 72 20   not search for 
e6230 61 6e 20 75 6e 75 73 65 64 20 66 69 6c 65 20 64  an unused file d
e6240 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 76 78 77  escriptor on vxw
e6250 6f 72 6b 73 2e 20 4e 6f 74 20 62 65 63 61 75 73  orks. Not becaus
e6260 65 0a 20 20 2a 2a 20 76 78 77 6f 72 6b 73 20 77  e.  ** vxworks w
e6270 6f 75 6c 64 20 6e 6f 74 20 62 65 6e 65 66 69 74  ould not benefit
e6280 20 66 72 6f 6d 20 74 68 65 20 63 68 61 6e 67 65   from the change
e6290 20 28 69 74 20 6d 69 67 68 74 2c 20 77 65 27 72   (it might, we'r
e62a0 65 20 6e 6f 74 20 73 75 72 65 29 2c 0a 20 20 2a  e not sure),.  *
e62b0 2a 20 62 75 74 20 62 65 63 61 75 73 65 20 6e 6f  * but because no
e62c0 20 77 61 79 20 74 6f 20 74 65 73 74 20 69 74 20   way to test it 
e62d0 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 76 61  is currently ava
e62e0 69 6c 61 62 6c 65 2e 20 49 74 20 69 73 20 62 65  ilable. It is be
e62f0 74 74 65 72 20 0a 20 20 2a 2a 20 6e 6f 74 20 74  tter .  ** not t
e6300 6f 20 72 69 73 6b 20 62 72 65 61 6b 69 6e 67 20  o risk breaking 
e6310 76 78 77 6f 72 6b 73 20 73 75 70 70 6f 72 74 20  vxworks support 
e6320 66 6f 72 20 74 68 65 20 73 61 6b 65 20 6f 66 20  for the sake of 
e6330 73 75 63 68 20 61 6e 20 6f 62 73 63 75 72 65 20  such an obscure 
e6340 0a 20 20 2a 2a 20 66 65 61 74 75 72 65 2e 20 20  .  ** feature.  
e6350 2a 2f 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52  */.#if !OS_VXWOR
e6360 4b 53 0a 20 20 73 74 72 75 63 74 20 73 74 61 74  KS.  struct stat
e6370 20 73 53 74 61 74 3b 20 20 20 20 20 20 20 20 20   sStat;         
e6380 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
e6390 75 6c 74 73 20 6f 66 20 73 74 61 74 28 29 20 63  ults of stat() c
e63a0 61 6c 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 41 20 73  all */..  /* A s
e63b0 74 61 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 66  tat() call may f
e63c0 61 69 6c 20 66 6f 72 20 76 61 72 69 6f 75 73 20  ail for various 
e63d0 72 65 61 73 6f 6e 73 2e 20 49 66 20 74 68 69 73  reasons. If this
e63e0 20 68 61 70 70 65 6e 73 2c 20 69 74 20 69 73 0a   happens, it is.
e63f0 20 20 2a 2a 20 61 6c 6d 6f 73 74 20 63 65 72 74    ** almost cert
e6400 61 69 6e 20 74 68 61 74 20 61 6e 20 6f 70 65 6e  ain that an open
e6410 28 29 20 63 61 6c 6c 20 6f 6e 20 74 68 65 20 73  () call on the s
e6420 61 6d 65 20 70 61 74 68 20 77 69 6c 6c 20 61 6c  ame path will al
e6430 73 6f 20 66 61 69 6c 2e 0a 20 20 2a 2a 20 46 6f  so fail..  ** Fo
e6440 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 69  r this reason, i
e6450 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
e6460 73 20 69 6e 20 74 68 65 20 73 74 61 74 28 29 20  s in the stat() 
e6470 63 61 6c 6c 20 68 65 72 65 2c 20 69 74 20 69 73  call here, it is
e6480 0a 20 20 2a 2a 20 69 67 6e 6f 72 65 64 20 61 6e  .  ** ignored an
e6490 64 20 2d 31 20 69 73 20 72 65 74 75 72 6e 65 64  d -1 is returned
e64a0 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c  . The caller wil
e64b0 6c 20 74 72 79 20 74 6f 20 6f 70 65 6e 20 61 20  l try to open a 
e64c0 6e 65 77 20 66 69 6c 65 0a 20 20 2a 2a 20 64 65  new file.  ** de
e64d0 73 63 72 69 70 74 6f 72 20 6f 6e 20 74 68 65 20  scriptor on the 
e64e0 73 61 6d 65 20 70 61 74 68 2c 20 66 61 69 6c 2c  same path, fail,
e64f0 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65   and return an e
e6500 72 72 6f 72 20 74 6f 20 53 51 4c 69 74 65 2e 0a  rror to SQLite..
e6510 20 20 2a 2a 0a 20 20 2a 2a 20 45 76 65 6e 20 69    **.  ** Even i
e6520 66 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 6f  f a subsequent o
e6530 70 65 6e 28 29 20 63 61 6c 6c 20 64 6f 65 73 20  pen() call does 
e6540 73 75 63 63 65 65 64 2c 20 74 68 65 20 63 6f 6e  succeed, the con
e6550 73 65 71 75 65 6e 63 65 73 20 6f 66 0a 20 20 2a  sequences of.  *
e6560 2a 20 6e 6f 74 20 73 65 61 72 63 68 69 6e 67 20  * not searching 
e6570 66 6f 72 20 61 20 72 65 73 75 73 61 62 6c 65 20  for a resusable 
e6580 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e6590 61 72 65 20 6e 6f 74 20 64 69 72 65 2e 20 20 2a  are not dire.  *
e65a0 2f 0a 20 20 69 66 28 20 30 3d 3d 73 74 61 74 28  /.  if( 0==stat(
e65b0 7a 50 61 74 68 2c 20 26 73 53 74 61 74 29 20 29  zPath, &sStat) )
e65c0 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75 6e 69  {.    struct uni
e65d0 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 3b 0a 20 20  xOpenCnt *pO;.  
e65e0 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c    struct unixFil
e65f0 65 49 64 20 69 64 3b 0a 20 20 20 20 69 64 2e 64  eId id;.    id.d
e6600 65 76 20 3d 20 73 53 74 61 74 2e 73 74 5f 64 65  ev = sStat.st_de
e6610 76 3b 0a 20 20 20 20 69 64 2e 69 6e 6f 20 3d 20  v;.    id.ino = 
e6620 73 53 74 61 74 2e 73 74 5f 69 6e 6f 3b 0a 0a 20  sStat.st_ino;.. 
e6630 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65     unixEnterMute
e6640 78 28 29 3b 0a 20 20 20 20 66 6f 72 28 70 4f 3d  x();.    for(pO=
e6650 6f 70 65 6e 4c 69 73 74 3b 20 70 4f 20 26 26 20  openList; pO && 
e6660 6d 65 6d 63 6d 70 28 26 69 64 2c 20 26 70 4f 2d  memcmp(&id, &pO-
e6670 3e 66 69 6c 65 49 64 2c 20 73 69 7a 65 6f 66 28  >fileId, sizeof(
e6680 69 64 29 29 3b 20 70 4f 3d 70 4f 2d 3e 70 4e 65  id)); pO=pO->pNe
e6690 78 74 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 20  xt);.    if( pO 
e66a0 29 7b 0a 20 20 20 20 20 20 55 6e 69 78 55 6e 75  ){.      UnixUnu
e66b0 73 65 64 46 64 20 2a 2a 70 70 3b 0a 20 20 20 20  sedFd **pp;.    
e66c0 20 20 66 6f 72 28 70 70 3d 26 70 4f 2d 3e 70 55    for(pp=&pO->pU
e66d0 6e 75 73 65 64 3b 20 2a 70 70 20 26 26 20 28 2a  nused; *pp && (*
e66e0 70 70 29 2d 3e 66 6c 61 67 73 21 3d 66 6c 61 67  pp)->flags!=flag
e66f0 73 3b 20 70 70 3d 26 28 28 2a 70 70 29 2d 3e 70  s; pp=&((*pp)->p
e6700 4e 65 78 74 29 29 3b 0a 20 20 20 20 20 20 70 55  Next));.      pU
e6710 6e 75 73 65 64 20 3d 20 2a 70 70 3b 0a 20 20 20  nused = *pp;.   
e6720 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20 29     if( pUnused )
e6730 7b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 3d 20  {.        *pp = 
e6740 70 55 6e 75 73 65 64 2d 3e 70 4e 65 78 74 3b 0a  pUnused->pNext;.
e6750 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
e6760 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
e6770 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 20  ();.  }.#endif  
e6780 20 20 2f 2a 20 69 66 20 21 4f 53 5f 56 58 57 4f    /* if !OS_VXWO
e6790 52 4b 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  RKS */.  return 
e67a0 70 55 6e 75 73 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a  pUnused;.}../*.*
e67b0 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20  * Open the file 
e67c0 7a 50 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 50 72  zPath..** .** Pr
e67d0 65 76 69 6f 75 73 6c 79 2c 20 74 68 65 20 53 51  eviously, the SQ
e67e0 4c 69 74 65 20 4f 53 20 6c 61 79 65 72 20 75 73  Lite OS layer us
e67f0 65 64 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f  ed three functio
e6800 6e 73 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74  ns in place of t
e6810 68 69 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a  his.** one:.**.*
e6820 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f  *     sqlite3OsO
e6830 70 65 6e 52 65 61 64 57 72 69 74 65 28 29 3b 0a  penReadWrite();.
e6840 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  **     sqlite3Os
e6850 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 29 3b 0a  OpenReadOnly();.
e6860 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  **     sqlite3Os
e6870 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 3b  OpenExclusive();
e6880 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c  .**.** These cal
e6890 6c 73 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f  ls correspond to
e68a0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
e68b0 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 66  ombinations of f
e68c0 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lags:.**.**     
e68d0 52 65 61 64 57 72 69 74 65 28 29 20 2d 3e 20 20  ReadWrite() ->  
e68e0 20 20 20 28 52 45 41 44 57 52 49 54 45 20 7c 20     (READWRITE | 
e68f0 43 52 45 41 54 45 29 0a 2a 2a 20 20 20 20 20 52  CREATE).**     R
e6900 65 61 64 4f 6e 6c 79 28 29 20 20 2d 3e 20 20 20  eadOnly()  ->   
e6910 20 20 28 52 45 41 44 4f 4e 4c 59 29 20 0a 2a 2a    (READONLY) .**
e6920 20 20 20 20 20 4f 70 65 6e 45 78 63 6c 75 73 69       OpenExclusi
e6930 76 65 28 29 20 2d 3e 20 28 52 45 41 44 57 52 49  ve() -> (READWRI
e6940 54 45 20 7c 20 43 52 45 41 54 45 20 7c 20 45 58  TE | CREATE | EX
e6950 43 4c 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a 20 54  CLUSIVE).**.** T
e6960 68 65 20 6f 6c 64 20 4f 70 65 6e 45 78 63 6c 75  he old OpenExclu
e6970 73 69 76 65 28 29 20 61 63 63 65 70 74 65 64 20  sive() accepted 
e6980 61 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65  a boolean argume
e6990 6e 74 20 2d 20 22 64 65 6c 46 6c 61 67 22 2e 20  nt - "delFlag". 
e69a0 49 66 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 20  If.** true, the 
e69b0 66 69 6c 65 20 77 61 73 20 63 6f 6e 66 69 67 75  file was configu
e69c0 72 65 64 20 74 6f 20 62 65 20 61 75 74 6f 6d 61  red to be automa
e69d0 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20  tically deleted 
e69e0 77 68 65 6e 20 74 68 65 0a 2a 2a 20 66 69 6c 65  when the.** file
e69f0 20 68 61 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20   handle closed. 
e6a00 54 6f 20 61 63 68 69 65 76 65 20 74 68 65 20 73  To achieve the s
e6a10 61 6d 65 20 65 66 66 65 63 74 20 75 73 69 6e 67  ame effect using
e6a20 20 74 68 69 73 20 6e 65 77 20 0a 2a 2a 20 69 6e   this new .** in
e6a30 74 65 72 66 61 63 65 2c 20 61 64 64 20 74 68 65  terface, add the
e6a40 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66   DELETEONCLOSE f
e6a50 6c 61 67 20 74 6f 20 74 68 6f 73 65 20 73 70 65  lag to those spe
e6a60 63 69 66 69 65 64 20 61 62 6f 76 65 20 66 6f 72  cified above for
e6a70 20 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69   .** OpenExclusi
e6a80 76 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ve()..*/.static 
e6a90 69 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0a 20 20  int unixOpen(.  
e6aa0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
e6ab0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
e6ac0 54 68 65 20 56 46 53 20 66 6f 72 20 77 68 69 63  The VFS for whic
e6ad0 68 20 74 68 69 73 20 69 73 20 74 68 65 20 78 4f  h this is the xO
e6ae0 70 65 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  pen method */.  
e6af0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
e6b00 68 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  h,           /* 
e6b10 50 61 74 68 6e 61 6d 65 20 6f 66 20 66 69 6c 65  Pathname of file
e6b20 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f   to be opened */
e6b30 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
e6b40 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20  *pFile,         
e6b50 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63  /* The file desc
e6b60 72 69 70 74 6f 72 20 74 6f 20 62 65 20 66 69 6c  riptor to be fil
e6b70 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20  led in */.  int 
e6b80 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
e6b90 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75           /* Inpu
e6ba0 74 20 66 6c 61 67 73 20 74 6f 20 63 6f 6e 74 72  t flags to contr
e6bb0 6f 6c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 2a  ol the opening *
e6bc0 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61  /.  int *pOutFla
e6bd0 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  gs              
e6be0 20 2f 2a 20 4f 75 74 70 75 74 20 66 6c 61 67 73   /* Output flags
e6bf0 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53 51 4c   returned to SQL
e6c00 69 74 65 20 63 6f 72 65 20 2a 2f 0a 29 7b 0a 20  ite core */.){. 
e6c10 20 75 6e 69 78 46 69 6c 65 20 2a 70 20 3d 20 28   unixFile *p = (
e6c20 75 6e 69 78 46 69 6c 65 20 2a 29 70 46 69 6c 65  unixFile *)pFile
e6c30 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b  ;.  int fd = -1;
e6c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6c50 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72     /* File descr
e6c60 69 70 74 6f 72 20 72 65 74 75 72 6e 65 64 20 62  iptor returned b
e6c70 79 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e  y open() */.  in
e6c80 74 20 64 69 72 66 64 20 3d 20 2d 31 3b 20 20 20  t dirfd = -1;   
e6c90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e6ca0 44 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64  Directory file d
e6cb0 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69  escriptor */.  i
e6cc0 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d 20 30  nt openFlags = 0
e6cd0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
e6ce0 20 46 6c 61 67 73 20 74 6f 20 70 61 73 73 20 74   Flags to pass t
e6cf0 6f 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e  o open() */.  in
e6d00 74 20 65 54 79 70 65 20 3d 20 66 6c 61 67 73 26  t eType = flags&
e6d10 30 78 46 46 46 46 46 46 30 30 3b 20 20 2f 2a 20  0xFFFFFF00;  /* 
e6d20 54 79 70 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Type of file to 
e6d30 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  open */.  int no
e6d40 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  Lock;           
e6d50 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
e6d60 20 74 6f 20 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67   to omit locking
e6d70 20 70 72 69 6d 69 74 69 76 65 73 20 2a 2f 0a 20   primitives */. 
e6d80 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
e6d90 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
e6da0 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 52 65 74 75  /* Function Retu
e6db0 72 6e 20 43 6f 64 65 20 2a 2f 0a 0a 20 20 69 6e  rn Code */..  in
e6dc0 74 20 69 73 45 78 63 6c 75 73 69 76 65 20 20 3d  t isExclusive  =
e6dd0 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
e6de0 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29  _OPEN_EXCLUSIVE)
e6df0 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65  ;.  int isDelete
e6e00 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20       = (flags & 
e6e10 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
e6e20 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20 69 6e  TEONCLOSE);.  in
e6e30 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20 3d  t isCreate     =
e6e40 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
e6e50 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20  _OPEN_CREATE);. 
e6e60 20 69 6e 74 20 69 73 52 65 61 64 6f 6e 6c 79 20   int isReadonly 
e6e70 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c    = (flags & SQL
e6e80 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
e6e90 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 61 64  Y);.  int isRead
e6ea0 57 72 69 74 65 20 20 3d 20 28 66 6c 61 67 73 20  Write  = (flags 
e6eb0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  & SQLITE_OPEN_RE
e6ec0 41 44 57 52 49 54 45 29 3b 0a 0a 20 20 2f 2a 20  ADWRITE);..  /* 
e6ed0 49 66 20 63 72 65 61 74 69 6e 67 20 61 20 6d 61  If creating a ma
e6ee0 73 74 65 72 20 6f 72 20 6d 61 69 6e 2d 66 69 6c  ster or main-fil
e6ef0 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 69 73 20  e journal, this 
e6f00 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 6f 70  function will op
e6f10 65 6e 0a 20 20 2a 2a 20 61 20 66 69 6c 65 2d 64  en.  ** a file-d
e6f20 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74 68 65  escriptor on the
e6f30 20 64 69 72 65 63 74 6f 72 79 20 74 6f 6f 2e 20   directory too. 
e6f40 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 75  The first time u
e6f50 6e 69 78 53 79 6e 63 28 29 0a 20 20 2a 2a 20 69  nixSync().  ** i
e6f60 73 20 63 61 6c 6c 65 64 20 74 68 65 20 64 69 72  s called the dir
e6f70 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63  ectory file desc
e6f80 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 66  riptor will be f
e6f90 73 79 6e 63 28 29 65 64 20 61 6e 64 20 63 6c 6f  sync()ed and clo
e6fa0 73 65 28 29 64 2e 0a 20 20 2a 2f 0a 20 20 69 6e  se()d..  */.  in
e6fb0 74 20 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72  t isOpenDirector
e6fc0 79 20 3d 20 28 69 73 43 72 65 61 74 65 20 26 26  y = (isCreate &&
e6fd0 20 0a 20 20 20 20 20 20 28 65 54 79 70 65 3d 3d   .      (eType==
e6fe0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54  SQLITE_OPEN_MAST
e6ff0 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54  ER_JOURNAL || eT
e7000 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
e7010 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a 20  _MAIN_JOURNAL). 
e7020 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 72 67   );..  /* If arg
e7030 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73 20 61  ument zPath is a
e7040 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74   NULL pointer, t
e7050 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
e7060 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70 65 6e  required to open
e7070 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72  .  ** a temporar
e7080 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68 69 73  y file. Use this
e7090 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f 72 65   buffer to store
e70a0 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 20 69   the file name i
e70b0 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 7a  n..  */.  char z
e70c0 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48  Tmpname[MAX_PATH
e70d0 4e 41 4d 45 2b 31 5d 3b 0a 20 20 63 6f 6e 73 74  NAME+1];.  const
e70e0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 7a   char *zName = z
e70f0 50 61 74 68 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  Path;..  /* Chec
e7100 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  k the following 
e7110 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 74  statements are t
e7120 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  rue: .  **.  ** 
e7130 20 20 28 61 29 20 45 78 61 63 74 6c 79 20 6f 6e    (a) Exactly on
e7140 65 20 6f 66 20 74 68 65 20 52 45 41 44 57 52 49  e of the READWRI
e7150 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c 59 20  TE and READONLY 
e7160 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73 65  flags must be se
e7170 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 20 20 28  t, and .  **   (
e7180 62 29 20 69 66 20 43 52 45 41 54 45 20 69 73 20  b) if CREATE is 
e7190 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44 57 52  set, then READWR
e71a0 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65  ITE must also be
e71b0 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a 2a 20 20   set, and.  **  
e71c0 20 28 63 29 20 69 66 20 45 58 43 4c 55 53 49 56   (c) if EXCLUSIV
e71d0 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 43  E is set, then C
e71e0 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 6f 20  REATE must also 
e71f0 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 20 20 28  be set..  **   (
e7200 64 29 20 69 66 20 44 45 4c 45 54 45 4f 4e 43 4c  d) if DELETEONCL
e7210 4f 53 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e  OSE is set, then
e7220 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73   CREATE must als
e7230 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2f 0a 20  o be set..  */. 
e7240 20 61 73 73 65 72 74 28 28 69 73 52 65 61 64 6f   assert((isReado
e7250 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64  nly==0 || isRead
e7260 57 72 69 74 65 3d 3d 30 29 20 26 26 20 28 69 73  Write==0) && (is
e7270 52 65 61 64 57 72 69 74 65 20 7c 7c 20 69 73 52  ReadWrite || isR
e7280 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 61 73 73  eadonly));.  ass
e7290 65 72 74 28 69 73 43 72 65 61 74 65 3d 3d 30 20  ert(isCreate==0 
e72a0 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 29 3b  || isReadWrite);
e72b0 0a 20 20 61 73 73 65 72 74 28 69 73 45 78 63 6c  .  assert(isExcl
e72c0 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73 43 72  usive==0 || isCr
e72d0 65 61 74 65 29 3b 0a 20 20 61 73 73 65 72 74 28  eate);.  assert(
e72e0 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c 20 69  isDelete==0 || i
e72f0 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f 2a 20  sCreate);..  /* 
e7300 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d 61 69  The main DB, mai
e7310 6e 20 6a 6f 75 72 6e 61 6c 2c 20 61 6e 64 20 6d  n journal, and m
e7320 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 72  aster journal ar
e7330 65 20 6e 65 76 65 72 20 61 75 74 6f 6d 61 74 69  e never automati
e7340 63 61 6c 6c 79 0a 20 20 2a 2a 20 64 65 6c 65 74  cally.  ** delet
e7350 65 64 2e 20 4e 6f 72 20 61 72 65 20 74 68 65 79  ed. Nor are they
e7360 20 65 76 65 72 20 74 65 6d 70 6f 72 61 72 79 20   ever temporary 
e7370 66 69 6c 65 73 2e 20 20 2a 2f 0a 20 20 61 73 73  files.  */.  ass
e7380 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20  ert( (!isDelete 
e7390 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79  && zName) || eTy
e73a0 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe!=SQLITE_OPEN_
e73b0 4d 41 49 4e 5f 44 42 20 29 3b 0a 20 20 61 73 73  MAIN_DB );.  ass
e73c0 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20  ert( (!isDelete 
e73d0 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79  && zName) || eTy
e73e0 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe!=SQLITE_OPEN_
e73f0 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a  MAIN_JOURNAL );.
e7400 20 20 61 73 73 65 72 74 28 20 28 21 69 73 44 65    assert( (!isDe
e7410 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 29 20 7c  lete && zName) |
e7420 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f  | eType!=SQLITE_
e7430 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
e7440 4e 41 4c 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73  NAL );..  /* Ass
e7450 65 72 74 20 74 68 61 74 20 74 68 65 20 75 70 70  ert that the upp
e7460 65 72 20 6c 61 79 65 72 20 68 61 73 20 73 65 74  er layer has set
e7470 20 6f 6e 65 20 6f 66 20 74 68 65 20 22 66 69 6c   one of the "fil
e7480 65 2d 74 79 70 65 22 20 66 6c 61 67 73 2e 20 2a  e-type" flags. *
e7490 2f 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70  /.  assert( eTyp
e74a0 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e==SQLITE_OPEN_M
e74b0 41 49 4e 5f 44 42 20 20 20 20 20 20 7c 7c 20 65  AIN_DB      || e
e74c0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
e74d0 4e 5f 54 45 4d 50 5f 44 42 20 0a 20 20 20 20 20  N_TEMP_DB .     
e74e0 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49    || eType==SQLI
e74f0 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
e7500 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53  RNAL || eType==S
e7510 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
e7520 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20  JOURNAL .       
e7530 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  || eType==SQLITE
e7540 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c  _OPEN_SUBJOURNAL
e7550 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c     || eType==SQL
e7560 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f  ITE_OPEN_MASTER_
e7570 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20  JOURNAL .       
e7580 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  || eType==SQLITE
e7590 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f  _OPEN_TRANSIENT_
e75a0 44 42 0a 20 20 29 3b 0a 0a 20 20 6d 65 6d 73 65  DB.  );..  memse
e75b0 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75  t(p, 0, sizeof(u
e75c0 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 69 66  nixFile));..  if
e75d0 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  ( eType==SQLITE_
e75e0 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a  OPEN_MAIN_DB ){.
e75f0 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64      UnixUnusedFd
e7600 20 2a 70 55 6e 75 73 65 64 3b 0a 20 20 20 20 70   *pUnused;.    p
e7610 55 6e 75 73 65 64 20 3d 20 66 69 6e 64 52 65 75  Unused = findReu
e7620 73 61 62 6c 65 46 64 28 7a 4e 61 6d 65 2c 20 66  sableFd(zName, f
e7630 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 70  lags);.    if( p
e7640 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 20 20  Unused ){.      
e7650 66 64 20 3d 20 70 55 6e 75 73 65 64 2d 3e 66 64  fd = pUnused->fd
e7660 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
e7670 20 20 20 70 55 6e 75 73 65 64 20 3d 20 73 71 6c     pUnused = sql
e7680 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
e7690 6f 66 28 2a 70 55 6e 75 73 65 64 29 29 3b 0a 20  of(*pUnused));. 
e76a0 20 20 20 20 20 69 66 28 20 21 70 55 6e 75 73 65       if( !pUnuse
e76b0 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  d ){.        ret
e76c0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
e76d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
e76e0 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 64 20 3d      p->pUnused =
e76f0 20 70 55 6e 75 73 65 64 3b 0a 20 20 7d 65 6c 73   pUnused;.  }els
e7700 65 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a  e if( !zName ){.
e7710 20 20 20 20 2f 2a 20 49 66 20 7a 4e 61 6d 65 20      /* If zName 
e7720 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 75 70 70  is NULL, the upp
e7730 65 72 20 6c 61 79 65 72 20 69 73 20 72 65 71 75  er layer is requ
e7740 65 73 74 69 6e 67 20 61 20 74 65 6d 70 20 66 69  esting a temp fi
e7750 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  le. */.    asser
e7760 74 28 69 73 44 65 6c 65 74 65 20 26 26 20 21 69  t(isDelete && !i
e7770 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 29 3b  sOpenDirectory);
e7780 0a 20 20 20 20 72 63 20 3d 20 67 65 74 54 65 6d  .    rc = getTem
e7790 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 4e 41  pname(MAX_PATHNA
e77a0 4d 45 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b  ME+1, zTmpname);
e77b0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
e77c0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
e77d0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
e77e0 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d  .    zName = zTm
e77f0 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  pname;.  }..  /*
e7800 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 76   Determine the v
e7810 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6c 61 67  alue of the flag
e7820 73 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 73  s parameter pass
e7830 65 64 20 74 6f 20 50 4f 53 49 58 20 66 75 6e 63  ed to POSIX func
e7840 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 28 29  tion.  ** open()
e7850 2e 20 54 68 65 73 65 20 6d 75 73 74 20 62 65 20  . These must be 
e7860 63 61 6c 63 75 6c 61 74 65 64 20 65 76 65 6e 20  calculated even 
e7870 69 66 20 6f 70 65 6e 28 29 20 69 73 20 6e 6f 74  if open() is not
e7880 20 63 61 6c 6c 65 64 2c 20 61 73 0a 20 20 2a 2a   called, as.  **
e7890 20 74 68 65 79 20 6d 61 79 20 62 65 20 73 74 6f   they may be sto
e78a0 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  red as part of t
e78b0 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 61  he file handle a
e78c0 6e 64 20 75 73 65 64 20 62 79 20 74 68 65 20 0a  nd used by the .
e78d0 20 20 2a 2a 20 27 63 6f 6e 63 68 20 66 69 6c 65    ** 'conch file
e78e0 27 20 6c 6f 63 6b 69 6e 67 20 66 75 6e 63 74 69  ' locking functi
e78f0 6f 6e 73 20 6c 61 74 65 72 20 6f 6e 2e 20 20 2a  ons later on.  *
e7900 2f 0a 20 20 69 66 28 20 69 73 52 65 61 64 6f 6e  /.  if( isReadon
e7910 6c 79 20 29 20 20 6f 70 65 6e 46 6c 61 67 73 20  ly )  openFlags 
e7920 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 69  |= O_RDONLY;.  i
e7930 66 28 20 69 73 52 65 61 64 57 72 69 74 65 20 29  f( isReadWrite )
e7940 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f   openFlags |= O_
e7950 52 44 57 52 3b 0a 20 20 69 66 28 20 69 73 43 72  RDWR;.  if( isCr
e7960 65 61 74 65 20 29 20 20 20 20 6f 70 65 6e 46 6c  eate )    openFl
e7970 61 67 73 20 7c 3d 20 4f 5f 43 52 45 41 54 3b 0a  ags |= O_CREAT;.
e7980 20 20 69 66 28 20 69 73 45 78 63 6c 75 73 69 76    if( isExclusiv
e7990 65 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d  e ) openFlags |=
e79a0 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f 4c   (O_EXCL|O_NOFOL
e79b0 4c 4f 57 29 3b 0a 20 20 6f 70 65 6e 46 6c 61 67  LOW);.  openFlag
e79c0 73 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 49 4c  s |= (O_LARGEFIL
e79d0 45 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 20  E|O_BINARY);..  
e79e0 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20  if( fd<0 ){.    
e79f0 6d 6f 64 65 5f 74 20 6f 70 65 6e 4d 6f 64 65 20  mode_t openMode 
e7a00 3d 20 28 69 73 44 65 6c 65 74 65 3f 30 36 30 30  = (isDelete?0600
e7a10 3a 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  :SQLITE_DEFAULT_
e7a20 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53  FILE_PERMISSIONS
e7a30 29 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e  );.    fd = open
e7a40 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67  (zName, openFlag
e7a50 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 20  s, openMode);.  
e7a60 20 20 4f 53 54 52 41 43 45 34 28 22 4f 50 45 4e    OSTRACE4("OPEN
e7a70 58 20 20 20 25 2d 33 64 20 25 73 20 30 25 6f 5c  X   %-3d %s 0%o\
e7a80 6e 22 2c 20 66 64 2c 20 7a 4e 61 6d 65 2c 20 6f  n", fd, zName, o
e7a90 70 65 6e 46 6c 61 67 73 29 3b 0a 20 20 20 20 69  penFlags);.    i
e7aa0 66 28 20 66 64 3c 30 20 26 26 20 65 72 72 6e 6f  f( fd<0 && errno
e7ab0 21 3d 45 49 53 44 49 52 20 26 26 20 69 73 52 65  !=EISDIR && isRe
e7ac0 61 64 57 72 69 74 65 20 26 26 20 21 69 73 45 78  adWrite && !isEx
e7ad0 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 20  clusive ){.     
e7ae0 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f 70   /* Failed to op
e7af0 65 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20  en the file for 
e7b00 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73  read/write acces
e7b10 73 2e 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79  s. Try read-only
e7b20 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6c 61 67 73  . */.      flags
e7b30 20 26 3d 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45   &= ~(SQLITE_OPE
e7b40 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49  N_READWRITE|SQLI
e7b50 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b  TE_OPEN_CREATE);
e7b60 0a 20 20 20 20 20 20 6f 70 65 6e 46 6c 61 67 73  .      openFlags
e7b70 20 26 3d 20 7e 28 4f 5f 52 44 57 52 7c 4f 5f 43   &= ~(O_RDWR|O_C
e7b80 52 45 41 54 29 3b 0a 20 20 20 20 20 20 66 6c 61  REAT);.      fla
e7b90 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45  gs |= SQLITE_OPE
e7ba0 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20  N_READONLY;.    
e7bb0 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f    openFlags |= O
e7bc0 5f 52 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 66  _RDONLY;.      f
e7bd0 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20  d = open(zName, 
e7be0 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e 4d  openFlags, openM
e7bf0 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ode);.    }.    
e7c00 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20  if( fd<0 ){.    
e7c10 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41    rc = SQLITE_CA
e7c20 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 20 20 67 6f  NTOPEN;.      go
e7c30 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64  to open_finished
e7c40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73  ;.    }.  }.  as
e7c50 73 65 72 74 28 20 66 64 3e 3d 30 20 29 3b 0a 20  sert( fd>=0 );. 
e7c60 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29   if( pOutFlags )
e7c70 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73  {.    *pOutFlags
e7c80 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20   = flags;.  }.. 
e7c90 20 69 66 28 20 70 2d 3e 70 55 6e 75 73 65 64 20   if( p->pUnused 
e7ca0 29 7b 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 65  ){.    p->pUnuse
e7cb0 64 2d 3e 66 64 20 3d 20 66 64 3b 0a 20 20 20 20  d->fd = fd;.    
e7cc0 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 6c 61 67  p->pUnused->flag
e7cd0 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a  s = flags;.  }..
e7ce0 20 20 69 66 28 20 69 73 44 65 6c 65 74 65 20 29    if( isDelete )
e7cf0 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  {.#if OS_VXWORKS
e7d00 0a 20 20 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61  .    zPath = zNa
e7d10 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 6e  me;.#else.    un
e7d20 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b 0a 23 65 6e  link(zName);.#en
e7d30 64 69 66 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49  dif.  }.#if SQLI
e7d40 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
e7d50 47 5f 53 54 59 4c 45 0a 20 20 65 6c 73 65 7b 0a  G_STYLE.  else{.
e7d60 20 20 20 20 70 2d 3e 6f 70 65 6e 46 6c 61 67 73      p->openFlags
e7d70 20 3d 20 6f 70 65 6e 46 6c 61 67 73 3b 0a 20 20   = openFlags;.  
e7d80 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  }.#endif..  if( 
e7d90 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20  isOpenDirectory 
e7da0 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e  ){.    rc = open
e7db0 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c  Directory(zPath,
e7dc0 20 26 64 69 72 66 64 29 3b 0a 20 20 20 20 69 66   &dirfd);.    if
e7dd0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
e7de0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 69  ){.      /* It i
e7df0 73 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 20  s safe to close 
e7e00 66 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74  fd at this point
e7e10 2c 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20  , because it is 
e7e20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 0a 20  guaranteed not. 
e7e30 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6f 70       ** to be op
e7e40 65 6e 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  en on a database
e7e50 20 66 69 6c 65 2e 20 49 66 20 69 74 20 77 65 72   file. If it wer
e7e60 65 20 6f 70 65 6e 20 6f 6e 20 61 20 64 61 74 61  e open on a data
e7e70 62 61 73 65 20 66 69 6c 65 2c 0a 20 20 20 20 20  base file,.     
e7e80 20 2a 2a 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74   ** it would not
e7e90 20 62 65 20 73 61 66 65 20 74 6f 20 63 6c 6f 73   be safe to clos
e7ea0 65 20 61 73 20 74 68 69 73 20 77 6f 75 6c 64 20  e as this would 
e7eb0 72 65 6c 65 61 73 65 20 61 6e 79 20 6c 6f 63 6b  release any lock
e7ec0 73 20 68 65 6c 64 0a 20 20 20 20 20 20 2a 2a 20  s held.      ** 
e7ed0 6f 6e 20 74 68 65 20 66 69 6c 65 20 62 79 20 74  on the file by t
e7ee0 68 69 73 20 70 72 6f 63 65 73 73 2e 20 20 2a 2f  his process.  */
e7ef0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65  .      assert( e
e7f00 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45  Type!=SQLITE_OPE
e7f10 4e 5f 4d 41 49 4e 5f 44 42 20 29 3b 0a 20 20 20  N_MAIN_DB );.   
e7f20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 20 20     close(fd);   
e7f30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 69 6c            /* sil
e7f40 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61  ently leak if fa
e7f50 69 6c 2c 20 61 6c 72 65 61 64 79 20 69 6e 20 65  il, already in e
e7f60 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 67 6f  rror */.      go
e7f70 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64  to open_finished
e7f80 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66  ;.    }.  }..#if
e7f90 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20  def FD_CLOEXEC. 
e7fa0 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54   fcntl(fd, F_SET
e7fb0 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f  FD, fcntl(fd, F_
e7fc0 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43  GETFD, 0) | FD_C
e7fd0 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a  LOEXEC);.#endif.
e7fe0 0a 20 20 6e 6f 4c 6f 63 6b 20 3d 20 65 54 79 70  .  noLock = eTyp
e7ff0 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e!=SQLITE_OPEN_M
e8000 41 49 4e 5f 44 42 3b 0a 0a 23 69 66 20 53 51 4c  AIN_DB;..#if SQL
e8010 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59  ITE_PREFER_PROXY
e8020 5f 4c 4f 43 4b 49 4e 47 0a 20 20 69 66 28 20 7a  _LOCKING.  if( z
e8030 50 61 74 68 21 3d 4e 55 4c 4c 20 26 26 20 21 6e  Path!=NULL && !n
e8040 6f 4c 6f 63 6b 20 26 26 20 70 56 66 73 2d 3e 78  oLock && pVfs->x
e8050 4f 70 65 6e 20 29 7b 0a 20 20 20 20 63 68 61 72  Open ){.    char
e8060 20 2a 65 6e 76 66 6f 72 63 65 20 3d 20 67 65 74   *envforce = get
e8070 65 6e 76 28 22 53 51 4c 49 54 45 5f 46 4f 52 43  env("SQLITE_FORC
e8080 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 22  E_PROXY_LOCKING"
e8090 29 3b 0a 20 20 20 20 69 6e 74 20 75 73 65 50 72  );.    int usePr
e80a0 6f 78 79 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a  oxy = 0;..    /*
e80b0 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52   SQLITE_FORCE_PR
e80c0 4f 58 59 5f 4c 4f 43 4b 49 4e 47 3d 3d 31 20 6d  OXY_LOCKING==1 m
e80d0 65 61 6e 73 20 66 6f 72 63 65 20 61 6c 77 61 79  eans force alway
e80e0 73 20 75 73 65 20 70 72 6f 78 79 2c 20 30 20 6d  s use proxy, 0 m
e80f0 65 61 6e 73 20 0a 20 20 20 20 2a 2a 20 6e 65 76  eans .    ** nev
e8100 65 72 20 75 73 65 20 70 72 6f 78 79 2c 20 4e 55  er use proxy, NU
e8110 4c 4c 20 6d 65 61 6e 73 20 75 73 65 20 70 72 6f  LL means use pro
e8120 78 79 20 66 6f 72 20 6e 6f 6e 2d 6c 6f 63 61 6c  xy for non-local
e8130 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 2a 2f   files only.  */
e8140 0a 20 20 20 20 69 66 28 20 65 6e 76 66 6f 72 63  .    if( envforc
e8150 65 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20  e!=NULL ){.     
e8160 20 75 73 65 50 72 6f 78 79 20 3d 20 61 74 6f 69   useProxy = atoi
e8170 28 65 6e 76 66 6f 72 63 65 29 3e 30 3b 0a 20 20  (envforce)>0;.  
e8180 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
e8190 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73 49  truct statfs fsI
e81a0 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 73  nfo;.      if( s
e81b0 74 61 74 66 73 28 7a 50 61 74 68 2c 20 26 66 73  tatfs(zPath, &fs
e81c0 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 7b 0a 20  Info) == -1 ){. 
e81d0 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 65         /* In the
e81e0 6f 72 79 2c 20 74 68 65 20 63 6c 6f 73 65 28 66  ory, the close(f
e81f0 64 29 20 63 61 6c 6c 20 69 73 20 73 75 62 2d 6f  d) call is sub-o
e8200 70 74 69 6d 61 6c 2e 20 49 66 20 74 68 65 20 66  ptimal. If the f
e8210 69 6c 65 20 6f 70 65 6e 65 64 0a 20 20 20 20 20  ile opened.     
e8220 20 20 20 2a 2a 20 77 69 74 68 20 66 64 20 69 73     ** with fd is
e8230 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
e8240 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20  , and there are 
e8250 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  other connection
e8260 73 20 6f 70 65 6e 0a 20 20 20 20 20 20 20 20 2a  s open.        *
e8270 2a 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 74  * on that file t
e8280 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c  hat are currentl
e8290 79 20 68 6f 6c 64 69 6e 67 20 61 64 76 69 73 6f  y holding adviso
e82a0 72 79 20 6c 6f 63 6b 73 20 6f 6e 20 69 74 2c 0a  ry locks on it,.
e82b0 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20          ** then 
e82c0 74 68 65 20 63 61 6c 6c 20 74 6f 20 63 6c 6f 73  the call to clos
e82d0 65 28 29 20 77 69 6c 6c 20 63 61 6e 63 65 6c 20  e() will cancel 
e82e0 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 49 6e 20  those locks. In 
e82f0 70 72 61 63 74 69 63 65 2c 0a 20 20 20 20 20 20  practice,.      
e8300 20 20 2a 2a 20 77 65 27 72 65 20 61 73 73 75 6d    ** we're assum
e8310 69 6e 67 20 74 68 61 74 20 73 74 61 74 66 73 28  ing that statfs(
e8320 29 20 64 6f 65 73 6e 27 74 20 66 61 69 6c 20 76  ) doesn't fail v
e8330 65 72 79 20 6f 66 74 65 6e 2e 20 41 74 20 6c 65  ery often. At le
e8340 61 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e  ast.        ** n
e8350 6f 74 20 77 68 69 6c 65 20 6f 74 68 65 72 20 66  ot while other f
e8360 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20  ile descriptors 
e8370 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 73 61  opened by the sa
e8380 6d 65 20 70 72 6f 63 65 73 73 20 6f 6e 0a 20 20  me process on.  
e8390 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 61 6d        ** the sam
e83a0 65 20 66 69 6c 65 20 61 72 65 20 77 6f 72 6b 69  e file are worki
e83b0 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  ng.  */.        
e83c0 70 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  p->lastErrno = e
e83d0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 69 66  rrno;.        if
e83e0 28 20 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20  ( dirfd>=0 ){.  
e83f0 20 20 20 20 20 20 20 20 63 6c 6f 73 65 28 64 69          close(di
e8400 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c  rfd); /* silentl
e8410 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20  y leak if fail, 
e8420 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  in error */.    
e8430 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 6c      }.        cl
e8440 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65  ose(fd); /* sile
e8450 6e 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69  ntly leak if fai
e8460 6c 2c 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20  l, in error */. 
e8470 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
e8480 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 3b  TE_IOERR_ACCESS;
e8490 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6f 70  .        goto op
e84a0 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20  en_finished;.   
e84b0 20 20 20 7d 0a 20 20 20 20 20 20 75 73 65 50 72     }.      usePr
e84c0 6f 78 79 20 3d 20 21 28 66 73 49 6e 66 6f 2e 66  oxy = !(fsInfo.f
e84d0 5f 66 6c 61 67 73 26 4d 4e 54 5f 4c 4f 43 41 4c  _flags&MNT_LOCAL
e84e0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
e84f0 20 75 73 65 50 72 6f 78 79 20 29 7b 0a 20 20 20   useProxy ){.   
e8500 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e     rc = fillInUn
e8510 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66 64 2c  ixFile(pVfs, fd,
e8520 20 64 69 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a   dirfd, pFile, z
e8530 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73  Path, noLock, is
e8540 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 69  Delete);.      i
e8550 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
e8560 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
e8570 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55   proxyTransformU
e8580 6e 69 78 46 69 6c 65 28 28 75 6e 69 78 46 69 6c  nixFile((unixFil
e8590 65 2a 29 70 46 69 6c 65 2c 20 22 3a 61 75 74 6f  e*)pFile, ":auto
e85a0 3a 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  :");.      }.   
e85b0 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e     goto open_fin
e85c0 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d  ished;.    }.  }
e85d0 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 72 63 20  .#endif.  .  rc 
e85e0 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65  = fillInUnixFile
e85f0 28 70 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64  (pVfs, fd, dirfd
e8600 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20  , pFile, zPath, 
e8610 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65  noLock, isDelete
e8620 29 3b 0a 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64  );.open_finished
e8630 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  :.  if( rc!=SQLI
e8640 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
e8650 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 70 55 6e  ite3_free(p->pUn
e8660 75 73 65 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74  used);.  }.  ret
e8670 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
e8680 2a 20 44 65 6c 65 74 65 20 74 68 65 20 66 69 6c  * Delete the fil
e8690 65 20 61 74 20 7a 50 61 74 68 2e 20 49 66 20 74  e at zPath. If t
e86a0 68 65 20 64 69 72 53 79 6e 63 20 61 72 67 75 6d  he dirSync argum
e86b0 65 6e 74 20 69 73 20 74 72 75 65 2c 20 66 73 79  ent is true, fsy
e86c0 6e 63 28 29 0a 2a 2a 20 74 68 65 20 64 69 72 65  nc().** the dire
e86d0 63 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65  ctory after dele
e86e0 74 69 6e 67 20 74 68 65 20 66 69 6c 65 2e 0a 2a  ting the file..*
e86f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
e8700 78 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74  xDelete(.  sqlit
e8710 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
e8720 20 20 20 20 20 2f 2a 20 56 46 53 20 63 6f 6e 74       /* VFS cont
e8730 61 69 6e 69 6e 67 20 74 68 69 73 20 61 73 20 74  aining this as t
e8740 68 65 20 78 44 65 6c 65 74 65 20 6d 65 74 68 6f  he xDelete metho
e8750 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
e8760 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 20  r *zPath,       
e8770 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65   /* Name of file
e8780 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a   to be deleted *
e8790 2f 0a 20 20 69 6e 74 20 64 69 72 53 79 6e 63 20  /.  int dirSync 
e87a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e87b0 20 49 66 20 74 72 75 65 2c 20 66 73 79 6e 63 28   If true, fsync(
e87c0 29 20 64 69 72 65 63 74 6f 72 79 20 61 66 74 65  ) directory afte
e87d0 72 20 64 65 6c 65 74 69 6e 67 20 66 69 6c 65 20  r deleting file 
e87e0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
e87f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 55 4e   SQLITE_OK;.  UN
e8800 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
e8810 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c  otUsed);.  Simul
e8820 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72  ateIOError(retur
e8830 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  n SQLITE_IOERR_D
e8840 45 4c 45 54 45 29 3b 0a 20 20 75 6e 6c 69 6e 6b  ELETE);.  unlink
e8850 28 7a 50 61 74 68 29 3b 0a 23 69 66 6e 64 65 66  (zPath);.#ifndef
e8860 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f   SQLITE_DISABLE_
e8870 44 49 52 53 59 4e 43 0a 20 20 69 66 28 20 64 69  DIRSYNC.  if( di
e8880 72 53 79 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74  rSync ){.    int
e8890 20 66 64 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70   fd;.    rc = op
e88a0 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74  enDirectory(zPat
e88b0 68 2c 20 26 66 64 29 3b 0a 20 20 20 20 69 66 28  h, &fd);.    if(
e88c0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
e88d0 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  {.#if OS_VXWORKS
e88e0 0a 20 20 20 20 20 20 69 66 28 20 66 73 79 6e 63  .      if( fsync
e88f0 28 66 64 29 3d 3d 2d 31 20 29 0a 23 65 6c 73 65  (fd)==-1 ).#else
e8900 0a 20 20 20 20 20 20 69 66 28 20 66 73 79 6e 63  .      if( fsync
e8910 28 66 64 29 20 29 0a 23 65 6e 64 69 66 0a 20 20  (fd) ).#endif.  
e8920 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 72 63      {.        rc
e8930 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
e8940 44 49 52 5f 46 53 59 4e 43 3b 0a 20 20 20 20 20  DIR_FSYNC;.     
e8950 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 6c 6f   }.      if( clo
e8960 73 65 28 66 64 29 26 26 21 72 63 20 29 7b 0a 20  se(fd)&&!rc ){. 
e8970 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
e8980 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f  TE_IOERR_DIR_CLO
e8990 53 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  SE;.      }.    
e89a0 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  }.  }.#endif.  r
e89b0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
e89c0 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78 69 73  ** Test the exis
e89d0 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63 63 65  tance of or acce
e89e0 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f  ss permissions o
e89f0 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20 54 68  f file zPath. Th
e8a00 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66 6f 72  e.** test perfor
e8a10 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20 74  med depends on t
e8a20 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c 61 67  he value of flag
e8a30 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c  s:.**.**     SQL
e8a40 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
e8a50 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74  S: Return 1 if t
e8a60 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 0a 2a  he file exists.*
e8a70 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43  *     SQLITE_ACC
e8a80 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 20 52  ESS_READWRITE: R
e8a90 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 66  eturn 1 if the f
e8aa0 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e 64 20  ile is read and 
e8ab0 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20  writable..**    
e8ac0 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52   SQLITE_ACCESS_R
e8ad0 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72 6e 20  EADONLY: Return 
e8ae0 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73  1 if the file is
e8af0 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a   readable..**.**
e8b00 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
e8b10 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  n 0..*/.static i
e8b20 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28 0a 20  nt unixAccess(. 
e8b30 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f   sqlite3_vfs *No
e8b40 74 55 73 65 64 2c 20 20 20 2f 2a 20 54 68 65 20  tUsed,   /* The 
e8b50 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  VFS containing t
e8b60 68 69 73 20 78 41 63 63 65 73 73 20 6d 65 74 68  his xAccess meth
e8b70 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  od */.  const ch
e8b80 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20  ar *zPath,      
e8b90 2f 2a 20 50 61 74 68 20 6f 66 20 74 68 65 20 66  /* Path of the f
e8ba0 69 6c 65 20 74 6f 20 65 78 61 6d 69 6e 65 20 2a  ile to examine *
e8bb0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
e8bc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
e8bd0 68 61 74 20 64 6f 20 77 65 20 77 61 6e 74 20 74  hat do we want t
e8be0 6f 20 6c 65 61 72 6e 20 61 62 6f 75 74 20 74 68  o learn about th
e8bf0 65 20 7a 50 61 74 68 20 66 69 6c 65 3f 20 2a 2f  e zPath file? */
e8c00 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 20  .  int *pResOut 
e8c10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
e8c20 69 74 65 20 72 65 73 75 6c 74 20 62 6f 6f 6c 65  ite result boole
e8c30 61 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  an here */.){.  
e8c40 69 6e 74 20 61 6d 6f 64 65 20 3d 20 30 3b 0a 20  int amode = 0;. 
e8c50 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e8c60 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 69  R(NotUsed);.  Si
e8c70 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
e8c80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
e8c90 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a 20 20  RR_ACCESS; );.  
e8ca0 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b  switch( flags ){
e8cb0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
e8cc0 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a  _ACCESS_EXISTS:.
e8cd0 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 46 5f        amode = F_
e8ce0 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OK;.      break;
e8cf0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
e8d00 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
e8d10 45 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d  E:.      amode =
e8d20 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20 20   W_OK|R_OK;.    
e8d30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
e8d40 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
e8d50 52 45 41 44 3a 0a 20 20 20 20 20 20 61 6d 6f 64  READ:.      amod
e8d60 65 20 3d 20 52 5f 4f 4b 3b 0a 20 20 20 20 20 20  e = R_OK;.      
e8d70 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61  break;..    defa
e8d80 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72  ult:.      asser
e8d90 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 67  t(!"Invalid flag
e8da0 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20  s argument");.  
e8db0 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 28  }.  *pResOut = (
e8dc0 61 63 63 65 73 73 28 7a 50 61 74 68 2c 20 61 6d  access(zPath, am
e8dd0 6f 64 65 29 3d 3d 30 29 3b 0a 20 20 72 65 74 75  ode)==0);.  retu
e8de0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
e8df0 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72  ../*.** Turn a r
e8e00 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65  elative pathname
e8e10 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74   into a full pat
e8e20 68 6e 61 6d 65 2e 20 54 68 65 20 72 65 6c 61 74  hname. The relat
e8e30 69 76 65 20 70 61 74 68 0a 2a 2a 20 69 73 20 73  ive path.** is s
e8e40 74 6f 72 65 64 20 61 73 20 61 20 6e 75 6c 2d 74  tored as a nul-t
e8e50 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67  erminated string
e8e60 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20 70   in the buffer p
e8e70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20  ointed to by.** 
e8e80 7a 50 61 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f  zPath. .**.** zO
e8e90 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62  ut points to a b
e8ea0 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 61 73  uffer of at leas
e8eb0 74 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78  t sqlite3_vfs.mx
e8ec0 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73 20 0a  Pathname bytes .
e8ed0 2a 2a 20 28 69 6e 20 74 68 69 73 20 63 61 73 65  ** (in this case
e8ee0 2c 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 62  , MAX_PATHNAME b
e8ef0 79 74 65 73 29 2e 20 54 68 65 20 66 75 6c 6c 2d  ytes). The full-
e8f00 70 61 74 68 20 69 73 20 77 72 69 74 74 65 6e 20  path is written 
e8f10 74 6f 0a 2a 2a 20 74 68 69 73 20 62 75 66 66 65  to.** this buffe
e8f20 72 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  r before returni
e8f30 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
e8f40 74 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61  t unixFullPathna
e8f50 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  me(.  sqlite3_vf
e8f60 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
e8f70 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
e8f80 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a  o vfs object */.
e8f90 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
e8fa0 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 20  ath,            
e8fb0 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c 61  /* Possibly rela
e8fc0 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68 20  tive input path 
e8fd0 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 2c 20 20  */.  int nOut,  
e8fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8ff0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75     /* Size of ou
e9000 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 62  tput buffer in b
e9010 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ytes */.  char *
e9020 7a 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20  zOut            
e9030 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
e9040 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 0a  t buffer */.){..
e9050 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f    /* It's odd to
e9060 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d   simulate an io-
e9070 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20  error here, but 
e9080 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a  really this is j
e9090 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74  ust.  ** using t
e90a0 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72  he io-error infr
e90b0 61 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65  astructure to te
e90c0 73 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68  st that SQLite h
e90d0 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a  andles this.  **
e90e0 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e   function failin
e90f0 67 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  g. This function
e9100 20 63 6f 75 6c 64 20 66 61 69 6c 20 69 66 2c 20   could fail if, 
e9110 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  for example, the
e9120 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f  .  ** current wo
e9130 72 6b 69 6e 67 20 64 69 72 65 63 74 6f 72 79 20  rking directory 
e9140 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65  has been unlinke
e9150 64 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61  d..  */.  Simula
e9160 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
e9170 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29  n SQLITE_ERROR )
e9180 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 56 66  ;..  assert( pVf
e9190 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3d 3d 4d  s->mxPathname==M
e91a0 41 58 5f 50 41 54 48 4e 41 4d 45 20 29 3b 0a 20  AX_PATHNAME );. 
e91b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e91c0 52 28 70 56 66 73 29 3b 0a 0a 20 20 7a 4f 75 74  R(pVfs);..  zOut
e91d0 5b 6e 4f 75 74 2d 31 5d 20 3d 20 27 5c 30 27 3b  [nOut-1] = '\0';
e91e0 0a 20 20 69 66 28 20 7a 50 61 74 68 5b 30 5d 3d  .  if( zPath[0]=
e91f0 3d 27 2f 27 20 29 7b 0a 20 20 20 20 73 71 6c 69  ='/' ){.    sqli
e9200 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75  te3_snprintf(nOu
e9210 74 2c 20 7a 4f 75 74 2c 20 22 25 73 22 2c 20 7a  t, zOut, "%s", z
e9220 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Path);.  }else{.
e9230 20 20 20 20 69 6e 74 20 6e 43 77 64 3b 0a 20 20      int nCwd;.  
e9240 20 20 69 66 28 20 67 65 74 63 77 64 28 7a 4f 75    if( getcwd(zOu
e9250 74 2c 20 6e 4f 75 74 2d 31 29 3d 3d 30 20 29 7b  t, nOut-1)==0 ){
e9260 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
e9270 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20  LITE_CANTOPEN;. 
e9280 20 20 20 7d 0a 20 20 20 20 6e 43 77 64 20 3d 20     }.    nCwd = 
e9290 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 4f 75 74  (int)strlen(zOut
e92a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  );.    sqlite3_s
e92b0 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2d 6e 43 77  nprintf(nOut-nCw
e92c0 64 2c 20 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c 20  d, &zOut[nCwd], 
e92d0 22 2f 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20  "/%s", zPath);. 
e92e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
e92f0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64  TE_OK;.}...#ifnd
e9300 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
e9310 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a  OAD_EXTENSION./*
e9320 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66  .** Interfaces f
e9330 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61  or opening a sha
e9340 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e  red library, fin
e9350 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74  ding entry point
e9360 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20  s.** within the 
e9370 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20  shared library, 
e9380 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20  and closing the 
e9390 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a  shared library..
e93a0 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64 6c 66  */.#include <dlf
e93b0 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76 6f 69  cn.h>.static voi
e93c0 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28 73 71  d *unixDlOpen(sq
e93d0 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73  lite3_vfs *NotUs
e93e0 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ed, const char *
e93f0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 4e  zFilename){.  UN
e9400 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
e9410 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
e9420 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e 61  n dlopen(zFilena
e9430 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c 20 52  me, RTLD_NOW | R
e9440 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a  TLD_GLOBAL);.}..
e9450 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 6c  /*.** SQLite cal
e9460 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ls this function
e9470 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74   immediately aft
e9480 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 6e 69  er a call to uni
e9490 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a 20 75  xDlSym() or.** u
e94a0 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 69 6c  nixDlOpen() fail
e94b0 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e 75 6c  s (returns a nul
e94c0 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 20 61  l pointer). If a
e94d0 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 65   more detailed e
e94e0 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20  rror.** message 
e94f0 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69 74  is available, it
e9500 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 7a   is written to z
e9510 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 65 72  BufOut. If no er
e9520 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69  ror message.** i
e9530 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a 42 75  s available, zBu
e9540 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75 6e 6d  fOut is left unm
e9550 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51 4c 69  odified and SQLi
e9560 74 65 20 75 73 65 73 20 61 20 64 65 66 61 75 6c  te uses a defaul
e9570 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61  t.** error messa
e9580 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ge..*/.static vo
e9590 69 64 20 75 6e 69 78 44 6c 45 72 72 6f 72 28 73  id unixDlError(s
e95a0 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55  qlite3_vfs *NotU
e95b0 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63  sed, int nBuf, c
e95c0 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20  har *zBufOut){. 
e95d0 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 55   char *zErr;.  U
e95e0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e95f0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 75 6e 69 78  NotUsed);.  unix
e9600 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
e9610 7a 45 72 72 20 3d 20 64 6c 65 72 72 6f 72 28 29  zErr = dlerror()
e9620 3b 0a 20 20 69 66 28 20 7a 45 72 72 20 29 7b 0a  ;.  if( zErr ){.
e9630 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
e9640 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 4f  intf(nBuf, zBufO
e9650 75 74 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 3b  ut, "%s", zErr);
e9660 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65  .  }.  unixLeave
e9670 4d 75 74 65 78 28 29 3b 0a 7d 0a 73 74 61 74 69  Mutex();.}.stati
e9680 63 20 76 6f 69 64 20 28 2a 75 6e 69 78 44 6c 53  c void (*unixDlS
e9690 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ym(sqlite3_vfs *
e96a0 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70  NotUsed, void *p
e96b0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a 53 79  , const char*zSy
e96c0 6d 29 29 28 76 6f 69 64 29 7b 0a 20 20 2f 2a 20  m))(void){.  /* 
e96d0 0a 20 20 2a 2a 20 47 43 43 20 77 69 74 68 20 2d  .  ** GCC with -
e96e0 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 73 20  pedantic-errors 
e96f0 73 61 79 73 20 74 68 61 74 20 43 39 30 20 64 6f  says that C90 do
e9700 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 76  es not allow a v
e9710 6f 69 64 2a 20 74 6f 20 62 65 0a 20 20 2a 2a 20  oid* to be.  ** 
e9720 63 61 73 74 20 69 6e 74 6f 20 61 20 70 6f 69 6e  cast into a poin
e9730 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
e9740 6e 2e 20 20 41 6e 64 20 79 65 74 20 74 68 65 20  n.  And yet the 
e9750 6c 69 62 72 61 72 79 20 64 6c 73 79 6d 28 29 20  library dlsym() 
e9760 72 6f 75 74 69 6e 65 0a 20 20 2a 2a 20 72 65 74  routine.  ** ret
e9770 75 72 6e 73 20 61 20 76 6f 69 64 2a 20 77 68 69  urns a void* whi
e9780 63 68 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70  ch is really a p
e9790 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
e97a0 74 69 6f 6e 2e 20 20 53 6f 20 68 6f 77 20 64 6f  tion.  So how do
e97b0 20 77 65 0a 20 20 2a 2a 20 75 73 65 20 64 6c 73   we.  ** use dls
e97c0 79 6d 28 29 20 77 69 74 68 20 2d 70 65 64 61 6e  ym() with -pedan
e97d0 74 69 63 2d 65 72 72 6f 72 73 3f 0a 20 20 2a 2a  tic-errors?.  **
e97e0 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20 78  .  ** Variable x
e97f0 20 62 65 6c 6f 77 20 69 73 20 64 65 66 69 6e 65   below is define
e9800 64 20 74 6f 20 62 65 20 61 20 70 6f 69 6e 74 65  d to be a pointe
e9810 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20  r to a function 
e9820 74 61 6b 69 6e 67 0a 20 20 2a 2a 20 70 61 72 61  taking.  ** para
e9830 6d 65 74 65 72 73 20 76 6f 69 64 2a 20 61 6e 64  meters void* and
e9840 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 61 6e 64   const char* and
e9850 20 72 65 74 75 72 6e 69 6e 67 20 61 20 70 6f 69   returning a poi
e9860 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69  nter to a functi
e9870 6f 6e 2e 0a 20 20 2a 2a 20 57 65 20 69 6e 69 74  on..  ** We init
e9880 69 61 6c 69 7a 65 20 78 20 62 79 20 61 73 73 69  ialize x by assi
e9890 67 6e 69 6e 67 20 69 74 20 61 20 70 6f 69 6e 74  gning it a point
e98a0 65 72 20 74 6f 20 74 68 65 20 64 6c 73 79 6d 28  er to the dlsym(
e98b0 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a  ) function..  **
e98c0 20 28 54 68 61 74 20 61 73 73 69 67 6e 6d 65 6e   (That assignmen
e98d0 74 20 72 65 71 75 69 72 65 73 20 61 20 63 61 73  t requires a cas
e98e0 74 2e 29 20 20 54 68 65 6e 20 77 65 20 63 61 6c  t.)  Then we cal
e98f0 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74  l the function t
e9900 68 61 74 0a 20 20 2a 2a 20 78 20 70 6f 69 6e 74  hat.  ** x point
e9910 73 20 74 6f 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a  s to.  .  **.  *
e9920 2a 20 54 68 69 73 20 77 6f 72 6b 2d 61 72 6f 75  * This work-arou
e9930 6e 64 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 74  nd is unlikely t
e9940 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79  o work correctly
e9950 20 6f 6e 20 61 6e 79 20 73 79 73 74 65 6d 20 77   on any system w
e9960 68 65 72 65 0a 20 20 2a 2a 20 79 6f 75 20 72 65  here.  ** you re
e9970 61 6c 6c 79 20 63 61 6e 6e 6f 74 20 63 61 73 74  ally cannot cast
e9980 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e   a function poin
e9990 74 65 72 20 69 6e 74 6f 20 76 6f 69 64 2a 2e 20  ter into void*. 
e99a0 20 42 75 74 20 74 68 65 6e 2c 20 6f 6e 20 74 68   But then, on th
e99b0 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 68 61 6e  e.  ** other han
e99c0 64 2c 20 64 6c 73 79 6d 28 29 20 77 69 6c 6c 20  d, dlsym() will 
e99d0 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 73 75 63 68  not work on such
e99e0 20 61 20 73 79 73 74 65 6d 20 65 69 74 68 65 72   a system either
e99f0 2c 20 73 6f 20 77 65 20 68 61 76 65 0a 20 20 2a  , so we have.  *
e9a00 2a 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6c 6f 73  * not really los
e9a10 74 20 61 6e 79 74 68 69 6e 67 2e 0a 20 20 2a 2f  t anything..  */
e9a20 0a 20 20 76 6f 69 64 20 28 2a 28 2a 78 29 28 76  .  void (*(*x)(v
e9a30 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  oid*,const char*
e9a40 29 29 28 76 6f 69 64 29 3b 0a 20 20 55 4e 55 53  ))(void);.  UNUS
e9a50 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
e9a60 55 73 65 64 29 3b 0a 20 20 78 20 3d 20 28 76 6f  Used);.  x = (vo
e9a70 69 64 28 2a 28 2a 29 28 76 6f 69 64 2a 2c 63 6f  id(*(*)(void*,co
e9a80 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64  nst char*))(void
e9a90 29 29 64 6c 73 79 6d 3b 0a 20 20 72 65 74 75 72  ))dlsym;.  retur
e9aa0 6e 20 28 2a 78 29 28 70 2c 20 7a 53 79 6d 29 3b  n (*x)(p, zSym);
e9ab0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  .}.static void u
e9ac0 6e 69 78 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74  nixDlClose(sqlit
e9ad0 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
e9ae0 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b   void *pHandle){
e9af0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
e9b00 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
e9b10 64 6c 63 6c 6f 73 65 28 70 48 61 6e 64 6c 65 29  dlclose(pHandle)
e9b20 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20  ;.}.#else /* if 
e9b30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
e9b40 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65  _EXTENSION is de
e9b50 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66  fined: */.  #def
e9b60 69 6e 65 20 75 6e 69 78 44 6c 4f 70 65 6e 20 20  ine unixDlOpen  
e9b70 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78  0.  #define unix
e9b80 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66  DlError 0.  #def
e9b90 69 6e 65 20 75 6e 69 78 44 6c 53 79 6d 20 20 20  ine unixDlSym   
e9ba0 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78  0.  #define unix
e9bb0 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66  DlClose 0.#endif
e9bc0 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6e 42  ../*.** Write nB
e9bd0 75 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64  uf bytes of rand
e9be0 6f 6d 20 64 61 74 61 20 74 6f 20 74 68 65 20 73  om data to the s
e9bf0 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 20 7a  upplied buffer z
e9c00 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  Buf..*/.static i
e9c10 6e 74 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73  nt unixRandomnes
e9c20 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e  s(sqlite3_vfs *N
e9c30 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66  otUsed, int nBuf
e9c40 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
e9c50 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e9c60 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73  R(NotUsed);.  as
e9c70 73 65 72 74 28 28 73 69 7a 65 5f 74 29 6e 42 75  sert((size_t)nBu
e9c80 66 3e 3d 28 73 69 7a 65 6f 66 28 74 69 6d 65 5f  f>=(sizeof(time_
e9c90 74 29 2b 73 69 7a 65 6f 66 28 69 6e 74 29 29 29  t)+sizeof(int)))
e9ca0 3b 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 65 20  ;..  /* We have 
e9cb0 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 7a 42  to initialize zB
e9cc0 75 66 20 74 6f 20 70 72 65 76 65 6e 74 20 76 61  uf to prevent va
e9cd0 6c 67 72 69 6e 64 20 66 72 6f 6d 20 72 65 70 6f  lgrind from repo
e9ce0 72 74 69 6e 67 0a 20 20 2a 2a 20 65 72 72 6f 72  rting.  ** error
e9cf0 73 2e 20 20 54 68 65 20 72 65 70 6f 72 74 73 20  s.  The reports 
e9d00 69 73 73 75 65 64 20 62 79 20 76 61 6c 67 72 69  issued by valgri
e9d10 6e 64 20 61 72 65 20 69 6e 63 6f 72 72 65 63 74  nd are incorrect
e9d20 20 2d 20 77 65 20 77 6f 75 6c 64 0a 20 20 2a 2a   - we would.  **
e9d30 20 70 72 65 66 65 72 20 74 68 61 74 20 74 68 65   prefer that the
e9d40 20 72 61 6e 64 6f 6d 6e 65 73 73 20 62 65 20 69   randomness be i
e9d50 6e 63 72 65 61 73 65 64 20 62 79 20 6d 61 6b 69  ncreased by maki
e9d60 6e 67 20 75 73 65 20 6f 66 20 74 68 65 0a 20 20  ng use of the.  
e9d70 2a 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  ** uninitialized
e9d80 20 73 70 61 63 65 20 69 6e 20 7a 42 75 66 20 2d   space in zBuf -
e9d90 20 62 75 74 20 76 61 6c 67 72 69 6e 64 20 65 72   but valgrind er
e9da0 72 6f 72 73 20 74 65 6e 64 20 74 6f 20 77 6f 72  rors tend to wor
e9db0 72 79 0a 20 20 2a 2a 20 73 6f 6d 65 20 75 73 65  ry.  ** some use
e9dc0 72 73 2e 20 20 52 61 74 68 65 72 20 74 68 61 6e  rs.  Rather than
e9dd0 20 61 72 67 75 65 2c 20 69 74 20 73 65 65 6d 73   argue, it seems
e9de0 20 65 61 73 69 65 72 20 6a 75 73 74 20 74 6f 20   easier just to 
e9df0 69 6e 69 74 69 61 6c 69 7a 65 0a 20 20 2a 2a 20  initialize.  ** 
e9e00 74 68 65 20 77 68 6f 6c 65 20 61 72 72 61 79 20  the whole array 
e9e10 61 6e 64 20 73 69 6c 65 6e 63 65 20 76 61 6c 67  and silence valg
e9e20 72 69 6e 64 2c 20 65 76 65 6e 20 69 66 20 74 68  rind, even if th
e9e30 61 74 20 6d 65 61 6e 73 20 6c 65 73 73 20 72 61  at means less ra
e9e40 6e 64 6f 6d 6e 65 73 73 0a 20 20 2a 2a 20 69 6e  ndomness.  ** in
e9e50 20 74 68 65 20 72 61 6e 64 6f 6d 20 73 65 65 64   the random seed
e9e60 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e  ..  **.  ** When
e9e70 20 74 65 73 74 69 6e 67 2c 20 69 6e 69 74 69 61   testing, initia
e9e80 6c 69 7a 69 6e 67 20 7a 42 75 66 5b 5d 20 74 6f  lizing zBuf[] to
e9e90 20 7a 65 72 6f 20 69 73 20 61 6c 6c 20 77 65 20   zero is all we 
e9ea0 64 6f 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 0a  do.  That means.
e9eb0 20 20 2a 2a 20 74 68 61 74 20 77 65 20 61 6c 77    ** that we alw
e9ec0 61 79 73 20 75 73 65 20 74 68 65 20 73 61 6d 65  ays use the same
e9ed0 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 73   random number s
e9ee0 65 71 75 65 6e 63 65 2e 20 20 54 68 69 73 20 6d  equence.  This m
e9ef0 61 6b 65 73 20 74 68 65 0a 20 20 2a 2a 20 74 65  akes the.  ** te
e9f00 73 74 73 20 72 65 70 65 61 74 61 62 6c 65 2e 0a  sts repeatable..
e9f10 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 7a 42    */.  memset(zB
e9f20 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 69  uf, 0, nBuf);.#i
e9f30 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
e9f40 45 5f 54 45 53 54 29 0a 20 20 7b 0a 20 20 20 20  E_TEST).  {.    
e9f50 69 6e 74 20 70 69 64 2c 20 66 64 3b 0a 20 20 20  int pid, fd;.   
e9f60 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76   fd = open("/dev
e9f70 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f  /urandom", O_RDO
e9f80 4e 4c 59 29 3b 0a 20 20 20 20 69 66 28 20 66 64  NLY);.    if( fd
e9f90 3c 30 20 29 7b 0a 20 20 20 20 20 20 74 69 6d 65  <0 ){.      time
e9fa0 5f 74 20 74 3b 0a 20 20 20 20 20 20 74 69 6d 65  _t t;.      time
e9fb0 28 26 74 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63  (&t);.      memc
e9fc0 70 79 28 7a 42 75 66 2c 20 26 74 2c 20 73 69 7a  py(zBuf, &t, siz
e9fd0 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 20 20 70  eof(t));.      p
e9fe0 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20  id = getpid();. 
e9ff0 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75       memcpy(&zBu
ea000 66 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70  f[sizeof(t)], &p
ea010 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29  id, sizeof(pid))
ea020 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
ea030 73 69 7a 65 6f 66 28 74 29 2b 73 69 7a 65 6f 66  sizeof(t)+sizeof
ea040 28 70 69 64 29 3c 3d 28 73 69 7a 65 5f 74 29 6e  (pid)<=(size_t)n
ea050 42 75 66 20 29 3b 0a 20 20 20 20 20 20 6e 42 75  Buf );.      nBu
ea060 66 20 3d 20 73 69 7a 65 6f 66 28 74 29 20 2b 20  f = sizeof(t) + 
ea070 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20 20  sizeof(pid);.   
ea080 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 42   }else{.      nB
ea090 75 66 20 3d 20 72 65 61 64 28 66 64 2c 20 7a 42  uf = read(fd, zB
ea0a0 75 66 2c 20 6e 42 75 66 29 3b 0a 20 20 20 20 20  uf, nBuf);.     
ea0b0 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 20 20   close(fd);.    
ea0c0 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  }.  }.#endif.  r
ea0d0 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a 0a  eturn nBuf;.}...
ea0e0 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20  /*.** Sleep for 
ea0f0 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20  a little while. 
ea100 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
ea110 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74  nt of time slept
ea120 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e  ..** The argumen
ea130 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
ea140 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20  of microseconds 
ea150 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70  we want to sleep
ea160 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  ..** The return 
ea170 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d  value is the num
ea180 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f  ber of microseco
ea190 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74  nds of sleep act
ea1a0 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74  ually.** request
ea1b0 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65  ed from the unde
ea1c0 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67  rlying operating
ea1d0 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65   system, a numbe
ea1e0 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74  r which.** might
ea1f0 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e   be greater than
ea200 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
ea210 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e   argument, but n
ea220 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  ot less.** than 
ea230 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  the argument..*/
ea240 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
ea250 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66  Sleep(sqlite3_vf
ea260 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20  s *NotUsed, int 
ea270 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23  microseconds){.#
ea280 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
ea290 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 20  struct timespec 
ea2a0 73 70 3b 0a 0a 20 20 73 70 2e 74 76 5f 73 65 63  sp;..  sp.tv_sec
ea2b0 20 3d 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20   = microseconds 
ea2c0 2f 20 31 30 30 30 30 30 30 3b 0a 20 20 73 70 2e  / 1000000;.  sp.
ea2d0 74 76 5f 6e 73 65 63 20 3d 20 28 6d 69 63 72 6f  tv_nsec = (micro
ea2e0 73 65 63 6f 6e 64 73 20 25 20 31 30 30 30 30 30  seconds % 100000
ea2f0 30 29 20 2a 20 31 30 30 30 3b 0a 20 20 6e 61 6e  0) * 1000;.  nan
ea300 6f 73 6c 65 65 70 28 26 73 70 2c 20 4e 55 4c 4c  osleep(&sp, NULL
ea310 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
ea320 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
ea330 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65    return microse
ea340 63 6f 6e 64 73 3b 0a 23 65 6c 69 66 20 64 65 66  conds;.#elif def
ea350 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50  ined(HAVE_USLEEP
ea360 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50  ) && HAVE_USLEEP
ea370 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 72 6f 73  .  usleep(micros
ea380 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45  econds);.  UNUSE
ea390 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
ea3a0 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d  sed);.  return m
ea3b0 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 65 6c  icroseconds;.#el
ea3c0 73 65 0a 20 20 69 6e 74 20 73 65 63 6f 6e 64 73  se.  int seconds
ea3d0 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73   = (microseconds
ea3e0 2b 39 39 39 39 39 39 29 2f 31 30 30 30 30 30 30  +999999)/1000000
ea3f0 3b 0a 20 20 73 6c 65 65 70 28 73 65 63 6f 6e 64  ;.  sleep(second
ea400 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  s);.  UNUSED_PAR
ea410 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
ea420 0a 20 20 72 65 74 75 72 6e 20 73 65 63 6f 6e 64  .  return second
ea430 73 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e 64 69  s*1000000;.#endi
ea440 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  f.}../*.** The f
ea450 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
ea460 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e  e, if set to a n
ea470 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 69  on-zero value, i
ea480 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  s interpreted as
ea490 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
ea4a0 66 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20  f seconds since 
ea4b0 31 39 37 30 20 61 6e 64 20 69 73 20 75 73 65 64  1970 and is used
ea4c0 20 74 6f 20 73 65 74 20 74 68 65 20 72 65 73 75   to set the resu
ea4d0 6c 74 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33  lt of.** sqlite3
ea4e0 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29 20  OsCurrentTime() 
ea4f0 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2e 0a  during testing..
ea500 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
ea510 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
ea520 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72   int sqlite3_cur
ea530 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 20 20  rent_time = 0;  
ea540 2f 2a 20 46 61 6b 65 20 73 79 73 74 65 6d 20 74  /* Fake system t
ea550 69 6d 65 20 69 6e 20 73 65 63 6f 6e 64 73 20 73  ime in seconds s
ea560 69 6e 63 65 20 31 39 37 30 2e 20 2a 2f 0a 23 65  ince 1970. */.#e
ea570 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  ndif../*.** Find
ea580 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d   the current tim
ea590 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20  e (in Universal 
ea5a0 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65  Coordinated Time
ea5b0 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a  ).  Write the.**
ea5c0 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e   current time an
ea5d0 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69  d date as a Juli
ea5e0 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e  an Day number in
ea5f0 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a  to *prNow and.**
ea600 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75   return 0.  Retu
ea610 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65  rn 1 if the time
ea620 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74   and date cannot
ea630 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74   be found..*/.st
ea640 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 75 72  atic int unixCur
ea650 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33  rentTime(sqlite3
ea660 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 64  _vfs *NotUsed, d
ea670 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 23  ouble *prNow){.#
ea680 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
ea690 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
ea6a0 50 4f 49 4e 54 29 0a 20 20 74 69 6d 65 5f 74 20  POINT).  time_t 
ea6b0 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20  t;.  time(&t);. 
ea6c0 20 2a 70 72 4e 6f 77 20 3d 20 28 28 28 73 71 6c   *prNow = (((sql
ea6d0 69 74 65 33 5f 69 6e 74 36 34 29 74 29 2f 38 36  ite3_int64)t)/86
ea6e0 34 30 20 2b 20 32 34 34 30 35 38 37 35 29 2f 31  40 + 24405875)/1
ea6f0 30 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64  0;.#elif defined
ea700 28 4e 4f 5f 47 45 54 54 4f 44 29 0a 20 20 74 69  (NO_GETTOD).  ti
ea710 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65 28 26  me_t t;.  time(&
ea720 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 74  t);.  *prNow = t
ea730 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35  /86400.0 + 24405
ea740 38 37 2e 35 3b 0a 23 65 6c 69 66 20 4f 53 5f 56  87.5;.#elif OS_V
ea750 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20  XWORKS.  struct 
ea760 74 69 6d 65 73 70 65 63 20 73 4e 6f 77 3b 0a 20  timespec sNow;. 
ea770 20 63 6c 6f 63 6b 5f 67 65 74 74 69 6d 65 28 43   clock_gettime(C
ea780 4c 4f 43 4b 5f 52 45 41 4c 54 49 4d 45 2c 20 26  LOCK_REALTIME, &
ea790 73 4e 6f 77 29 3b 0a 20 20 2a 70 72 4e 6f 77 20  sNow);.  *prNow 
ea7a0 3d 20 32 34 34 30 35 38 37 2e 35 20 2b 20 73 4e  = 2440587.5 + sN
ea7b0 6f 77 2e 74 76 5f 73 65 63 2f 38 36 34 30 30 2e  ow.tv_sec/86400.
ea7c0 30 20 2b 20 73 4e 6f 77 2e 74 76 5f 6e 73 65 63  0 + sNow.tv_nsec
ea7d0 2f 38 36 34 30 30 30 30 30 30 30 30 30 30 30 2e  /86400000000000.
ea7e0 30 3b 0a 23 65 6c 73 65 0a 20 20 73 74 72 75 63  0;.#else.  struc
ea7f0 74 20 74 69 6d 65 76 61 6c 20 73 4e 6f 77 3b 0a  t timeval sNow;.
ea800 20 20 67 65 74 74 69 6d 65 6f 66 64 61 79 28 26    gettimeofday(&
ea810 73 4e 6f 77 2c 20 30 29 3b 0a 20 20 2a 70 72 4e  sNow, 0);.  *prN
ea820 6f 77 20 3d 20 32 34 34 30 35 38 37 2e 35 20 2b  ow = 2440587.5 +
ea830 20 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38 36 34   sNow.tv_sec/864
ea840 30 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76 5f 75  00.0 + sNow.tv_u
ea850 73 65 63 2f 38 36 34 30 30 30 30 30 30 30 30 2e  sec/86400000000.
ea860 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  0;.#endif..#ifde
ea870 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
ea880 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72 72  if( sqlite3_curr
ea890 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20  ent_time ){.    
ea8a0 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33  *prNow = sqlite3
ea8b0 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36  _current_time/86
ea8c0 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e  400.0 + 2440587.
ea8d0 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  5;.  }.#endif.  
ea8e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
ea8f0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74  (NotUsed);.  ret
ea900 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
ea910 57 65 20 61 64 64 65 64 20 74 68 65 20 78 47 65  We added the xGe
ea920 74 4c 61 73 74 45 72 72 6f 72 28 29 20 6d 65 74  tLastError() met
ea930 68 6f 64 20 77 69 74 68 20 74 68 65 20 69 6e 74  hod with the int
ea940 65 6e 74 69 6f 6e 20 6f 66 20 70 72 6f 76 69 64  ention of provid
ea950 69 6e 67 0a 2a 2a 20 62 65 74 74 65 72 20 6c 6f  ing.** better lo
ea960 77 2d 6c 65 76 65 6c 20 65 72 72 6f 72 20 6d 65  w-level error me
ea970 73 73 61 67 65 73 20 77 68 65 6e 20 6f 70 65 72  ssages when oper
ea980 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 70 72 6f  ating-system pro
ea990 62 6c 65 6d 73 20 63 6f 6d 65 20 75 70 0a 2a 2a  blems come up.**
ea9a0 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65 20 6f   during SQLite o
ea9b0 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 73  peration.  But s
ea9c0 6f 20 66 61 72 2c 20 6e 6f 6e 65 20 6f 66 20 74  o far, none of t
ea9d0 68 61 74 20 68 61 73 20 62 65 65 6e 20 69 6d 70  hat has been imp
ea9e0 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 69 6e 20 74  lemented.** in t
ea9f0 68 65 20 63 6f 72 65 2e 20 20 53 6f 20 74 68 69  he core.  So thi
eaa00 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76  s routine is nev
eaa10 65 72 20 63 61 6c 6c 65 64 2e 20 20 46 6f 72 20  er called.  For 
eaa20 6e 6f 77 2c 20 69 74 20 69 73 20 6d 65 72 65 6c  now, it is merel
eaa30 79 0a 2a 2a 20 61 20 70 6c 61 63 65 2d 68 6f 6c  y.** a place-hol
eaa40 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  der..*/.static i
eaa50 6e 74 20 75 6e 69 78 47 65 74 4c 61 73 74 45 72  nt unixGetLastEr
eaa60 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
eaa70 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f  *NotUsed, int No
eaa80 74 55 73 65 64 32 2c 20 63 68 61 72 20 2a 4e 6f  tUsed2, char *No
eaa90 74 55 73 65 64 33 29 7b 0a 20 20 55 4e 55 53 45  tUsed3){.  UNUSE
eaaa0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
eaab0 73 65 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  sed);.  UNUSED_P
eaac0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
eaad0 32 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  2);.  UNUSED_PAR
eaae0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 33 29  AMETER(NotUsed3)
eaaf0 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
eab00 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./*.************
eab10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
eab20 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66 73 20   of sqlite3_vfs 
eab30 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a  methods ********
eab40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eab50 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***.************
eab60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eab70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eab80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eab90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaba0 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
eabb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eabc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eabd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eabe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eabf0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
eac00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
eac10 42 65 67 69 6e 20 50 72 6f 78 79 20 4c 6f 63 6b  Begin Proxy Lock
eac20 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ing ************
eac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eac40 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79  ****.**.** Proxy
eac50 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 20 22 75   locking is a "u
eac60 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 74 68  ber-locking-meth
eac70 6f 64 22 20 69 6e 20 74 68 69 73 20 73 65 6e 73  od" in this sens
eac80 65 3a 20 20 49 74 20 75 73 65 73 20 74 68 65 0a  e:  It uses the.
eac90 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67  ** other locking
eaca0 20 6d 65 74 68 6f 64 73 20 6f 6e 20 73 65 63 6f   methods on seco
eacb0 6e 64 61 72 79 20 6c 6f 63 6b 20 66 69 6c 65 73  ndary lock files
eacc0 2e 20 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  .  Proxy locking
eacd0 20 69 73 20 61 0a 2a 2a 20 6d 65 74 61 2d 6c 61   is a.** meta-la
eace0 79 65 72 20 6f 76 65 72 20 74 6f 70 20 6f 66 20  yer over top of 
eacf0 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 6c 6f  the primitive lo
ead00 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 65  cking implemente
ead10 64 20 61 62 6f 76 65 2e 20 20 46 6f 72 0a 2a 2a  d above.  For.**
ead20 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68   this reason, th
ead30 65 20 64 69 76 69 73 69 6f 6e 20 74 68 61 74 20  e division that 
ead40 69 6d 70 6c 65 6d 65 6e 74 73 20 6f 66 20 70 72  implements of pr
ead50 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 64  oxy locking is d
ead60 65 66 65 72 72 65 64 0a 2a 2a 20 75 6e 74 69 6c  eferred.** until
ead70 20 6c 61 74 65 20 69 6e 20 74 68 65 20 66 69 6c   late in the fil
ead80 65 20 28 68 65 72 65 29 20 61 66 74 65 72 20 61  e (here) after a
ead90 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20  ll of the other 
eada0 49 2f 4f 20 6d 65 74 68 6f 64 73 20 68 61 76 65  I/O methods have
eadb0 0a 2a 2a 20 62 65 65 6e 20 64 65 66 69 6e 65 64  .** been defined
eadc0 20 2d 20 73 6f 20 74 68 61 74 20 74 68 65 20 70   - so that the p
eadd0 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67  rimitive locking
eade0 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61 76 61   methods are ava
eadf0 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 73 65 72  ilable.** as ser
eae00 76 69 63 65 73 20 74 6f 20 68 65 6c 70 20 77 69  vices to help wi
eae10 74 68 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  th the implement
eae20 61 74 69 6f 6e 20 6f 66 20 70 72 6f 78 79 20 6c  ation of proxy l
eae30 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 2a 2a 0a  ocking..**.****.
eae40 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  **.** The defaul
eae50 74 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65  t locking scheme
eae60 73 20 69 6e 20 53 51 4c 69 74 65 20 75 73 65 20  s in SQLite use 
eae70 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73  byte-range locks
eae80 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   on the.** datab
eae90 61 73 65 20 66 69 6c 65 20 74 6f 20 63 6f 6f 72  ase file to coor
eaea0 64 69 6e 61 74 65 20 73 61 66 65 2c 20 63 6f 6e  dinate safe, con
eaeb0 63 75 72 72 65 6e 74 20 61 63 63 65 73 73 20 62  current access b
eaec0 79 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65  y multiple reade
eaed0 72 73 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 72  rs.** and writer
eaee0 73 20 5b 68 74 74 70 3a 2f 2f 73 71 6c 69 74 65  s [http://sqlite
eaef0 2e 6f 72 67 2f 6c 6f 63 6b 69 6e 67 76 33 2e 68  .org/lockingv3.h
eaf00 74 6d 6c 5d 2e 20 20 54 68 65 20 66 69 76 65 20  tml].  The five 
eaf10 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20  file locking.** 
eaf20 73 74 61 74 65 73 20 28 55 4e 4c 4f 43 4b 45 44  states (UNLOCKED
eaf30 2c 20 50 45 4e 44 49 4e 47 2c 20 53 48 41 52 45  , PENDING, SHARE
eaf40 44 2c 20 52 45 53 45 52 56 45 44 2c 20 45 58 43  D, RESERVED, EXC
eaf50 4c 55 53 49 56 45 29 20 61 72 65 20 69 6d 70 6c  LUSIVE) are impl
eaf60 65 6d 65 6e 74 65 64 0a 2a 2a 20 61 73 20 50 4f  emented.** as PO
eaf70 53 49 58 20 72 65 61 64 20 26 20 77 72 69 74 65  SIX read & write
eaf80 20 6c 6f 63 6b 73 20 6f 76 65 72 20 66 69 78 65   locks over fixe
eaf90 64 20 73 65 74 20 6f 66 20 6c 6f 63 61 74 69 6f  d set of locatio
eafa0 6e 73 20 28 76 69 61 20 66 73 63 74 6c 29 2c 0a  ns (via fsctl),.
eafb0 2a 2a 20 6f 6e 20 41 46 50 20 61 6e 64 20 53 4d  ** on AFP and SM
eafc0 42 20 6f 6e 6c 79 20 65 78 63 6c 75 73 69 76 65  B only exclusive
eafd0 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b   byte-range lock
eafe0 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20  s are available 
eaff0 76 69 61 20 66 73 63 74 6c 0a 2a 2a 20 77 69 74  via fsctl.** wit
eb000 68 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c  h _IOWR('z', 23,
eb010 20 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67   struct ByteRang
eb020 65 4c 6f 63 6b 50 42 32 29 20 74 6f 20 74 72 61  eLockPB2) to tra
eb030 63 6b 20 74 68 65 20 73 61 6d 65 20 35 20 73 74  ck the same 5 st
eb040 61 74 65 73 2e 0a 2a 2a 20 54 6f 20 73 69 6d 75  ates..** To simu
eb050 6c 61 74 65 20 61 20 46 5f 52 44 4c 43 4b 20 6f  late a F_RDLCK o
eb060 6e 20 74 68 65 20 73 68 61 72 65 64 20 72 61 6e  n the shared ran
eb070 67 65 2c 20 6f 6e 20 41 46 50 20 61 20 72 61 6e  ge, on AFP a ran
eb080 64 6f 6d 6c 79 20 73 65 6c 65 63 74 65 64 0a 2a  domly selected.*
eb090 2a 20 61 64 64 72 65 73 73 20 69 6e 20 74 68 65  * address in the
eb0a0 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 73   shared range is
eb0b0 20 74 61 6b 65 6e 20 66 6f 72 20 61 20 53 48 41   taken for a SHA
eb0c0 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 65 6e  RED lock, the en
eb0d0 74 69 72 65 0a 2a 2a 20 73 68 61 72 65 64 20 72  tire.** shared r
eb0e0 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 66 6f  ange is taken fo
eb0f0 72 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  r an EXCLUSIVE l
eb100 6f 63 6b 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ock):.**.**     
eb110 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 20 20   PENDING_BYTE   
eb120 20 20 20 20 20 30 78 34 30 30 30 30 30 30 30 09       0x40000000.
eb130 09 20 20 20 09 0a 2a 2a 20 20 20 20 20 20 52 45  .   ..**      RE
eb140 53 45 52 56 45 44 5f 42 59 54 45 20 20 20 20 20  SERVED_BYTE     
eb150 20 20 30 78 34 30 30 30 30 30 30 31 0a 2a 2a 20    0x40000001.** 
eb160 20 20 20 20 20 53 48 41 52 45 44 5f 52 41 4e 47       SHARED_RANG
eb170 45 20 20 20 20 20 20 20 20 30 78 34 30 30 30 30  E        0x40000
eb180 30 30 32 20 2d 3e 20 30 78 34 30 30 30 30 32 30  002 -> 0x4000020
eb190 30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72  0.**.** This wor
eb1a0 6b 73 20 77 65 6c 6c 20 6f 6e 20 74 68 65 20 6c  ks well on the l
eb1b0 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d  ocal file system
eb1c0 2c 20 62 75 74 20 73 68 6f 77 73 20 61 20 6e 65  , but shows a ne
eb1d0 61 72 6c 79 20 31 30 30 78 0a 2a 2a 20 73 6c 6f  arly 100x.** slo
eb1e0 77 64 6f 77 6e 20 69 6e 20 72 65 61 64 20 70 65  wdown in read pe
eb1f0 72 66 6f 72 6d 61 6e 63 65 20 6f 6e 20 41 46 50  rformance on AFP
eb200 20 62 65 63 61 75 73 65 20 74 68 65 20 41 46 50   because the AFP
eb210 20 63 6c 69 65 6e 74 20 64 69 73 61 62 6c 65 73   client disables
eb220 0a 2a 2a 20 74 68 65 20 72 65 61 64 20 63 61 63  .** the read cac
eb230 68 65 20 77 68 65 6e 20 62 79 74 65 2d 72 61 6e  he when byte-ran
eb240 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 70 72 65  ge locks are pre
eb250 73 65 6e 74 2e 20 20 45 6e 61 62 6c 69 6e 67 20  sent.  Enabling 
eb260 74 68 65 20 72 65 61 64 0a 2a 2a 20 63 61 63 68  the read.** cach
eb270 65 20 65 78 70 6f 73 65 73 20 61 20 63 61 63 68  e exposes a cach
eb280 65 20 63 6f 68 65 72 65 6e 63 79 20 70 72 6f 62  e coherency prob
eb290 6c 65 6d 20 74 68 61 74 20 69 73 20 70 72 65 73  lem that is pres
eb2a0 65 6e 74 20 6f 6e 20 61 6c 6c 20 4f 53 20 58 0a  ent on all OS X.
eb2b0 2a 2a 20 73 75 70 70 6f 72 74 65 64 20 6e 65 74  ** supported net
eb2c0 77 6f 72 6b 20 66 69 6c 65 20 73 79 73 74 65 6d  work file system
eb2d0 73 2e 20 20 4e 46 53 20 61 6e 64 20 41 46 50 20  s.  NFS and AFP 
eb2e0 62 6f 74 68 20 6f 62 73 65 72 76 65 20 74 68 65  both observe the
eb2f0 0a 2a 2a 20 63 6c 6f 73 65 2d 74 6f 2d 6f 70 65  .** close-to-ope
eb300 6e 20 73 65 6d 61 6e 74 69 63 73 20 66 6f 72 20  n semantics for 
eb310 65 6e 73 75 72 69 6e 67 20 63 61 63 68 65 20 63  ensuring cache c
eb320 6f 68 65 72 65 6e 63 79 0a 2a 2a 20 5b 68 74 74  oherency.** [htt
eb330 70 3a 2f 2f 6e 66 73 2e 73 6f 75 72 63 65 66 6f  p://nfs.sourcefo
eb340 72 67 65 2e 6e 65 74 2f 23 66 61 71 5f 61 38 5d  rge.net/#faq_a8]
eb350 2c 20 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74  , which does not
eb360 20 65 66 66 65 63 74 69 76 65 6c 79 0a 2a 2a 20   effectively.** 
eb370 61 64 64 72 65 73 73 20 74 68 65 20 72 65 71 75  address the requ
eb380 69 72 65 6d 65 6e 74 73 20 66 6f 72 20 63 6f 6e  irements for con
eb390 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65  current database
eb3a0 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69   access by multi
eb3b0 70 6c 65 0a 2a 2a 20 72 65 61 64 65 72 73 20 61  ple.** readers a
eb3c0 6e 64 20 77 72 69 74 65 72 73 0a 2a 2a 20 5b 68  nd writers.** [h
eb3d0 74 74 70 3a 2f 2f 77 77 77 2e 6e 61 62 62 6c 65  ttp://www.nabble
eb3e0 2e 63 6f 6d 2f 53 51 4c 69 74 65 2d 6f 6e 2d 4e  .com/SQLite-on-N
eb3f0 46 53 2d 63 61 63 68 65 2d 63 6f 68 65 72 65 6e  FS-cache-coheren
eb400 63 79 2d 74 64 31 35 36 35 35 37 30 31 2e 68 74  cy-td15655701.ht
eb410 6d 6c 5d 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 64  ml]..**.** To ad
eb420 64 72 65 73 73 20 74 68 65 20 70 65 72 66 6f 72  dress the perfor
eb430 6d 61 6e 63 65 20 61 6e 64 20 63 61 63 68 65 20  mance and cache 
eb440 63 6f 68 65 72 65 6e 63 79 20 69 73 73 75 65 73  coherency issues
eb450 2c 20 70 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63  , proxy file loc
eb460 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e 67 65 73 20  king.** changes 
eb470 74 68 65 20 77 61 79 20 64 61 74 61 62 61 73 65  the way database
eb480 20 61 63 63 65 73 73 20 69 73 20 63 6f 6e 74 72   access is contr
eb490 6f 6c 6c 65 64 20 62 79 20 6c 69 6d 69 74 69 6e  olled by limitin
eb4a0 67 20 61 63 63 65 73 73 20 74 6f 20 61 0a 2a 2a  g access to a.**
eb4b0 20 73 69 6e 67 6c 65 20 68 6f 73 74 20 61 74 20   single host at 
eb4c0 61 20 74 69 6d 65 20 61 6e 64 20 6d 6f 76 69 6e  a time and movin
eb4d0 67 20 66 69 6c 65 20 6c 6f 63 6b 73 20 6f 66 66  g file locks off
eb4e0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
eb4f0 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6f 6e 74   file.** and ont
eb500 6f 20 61 20 70 72 6f 78 79 20 66 69 6c 65 20 6f  o a proxy file o
eb510 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65  n the local file
eb520 20 73 79 73 74 65 6d 2e 20 20 0a 2a 2a 0a 2a 2a   system.  .**.**
eb530 0a 2a 2a 20 55 73 69 6e 67 20 70 72 6f 78 79 20  .** Using proxy 
eb540 6c 6f 63 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d  locks.** -------
eb550 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a  ----------.**.**
eb560 20 43 20 41 50 49 73 0a 2a 2a 0a 2a 2a 20 20 73   C APIs.**.**  s
eb570 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
eb580 72 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20  rol(db, dbname, 
eb590 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50  SQLITE_SET_LOCKP
eb5a0 52 4f 58 59 46 49 4c 45 2c 0a 2a 2a 20 20 20 20  ROXYFILE,.**    
eb5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eb5c0 20 20 20 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20     <proxy_path> 
eb5d0 7c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 2a 2a 20  | ":auto:");.** 
eb5e0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f   sqlite3_file_co
eb5f0 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65  ntrol(db, dbname
eb600 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43  , SQLITE_GET_LOC
eb610 4b 50 52 4f 58 59 46 49 4c 45 2c 20 26 3c 70 72  KPROXYFILE, &<pr
eb620 6f 78 79 5f 70 61 74 68 3e 29 3b 0a 2a 2a 0a 2a  oxy_path>);.**.*
eb630 2a 0a 2a 2a 20 53 51 4c 20 70 72 61 67 6d 61 73  *.** SQL pragmas
eb640 0a 2a 2a 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b  .**.**  PRAGMA [
eb650 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70  database.]lock_p
eb660 72 6f 78 79 5f 66 69 6c 65 3d 3c 70 72 6f 78 79  roxy_file=<proxy
eb670 5f 70 61 74 68 3e 20 7c 20 3a 61 75 74 6f 3a 0a  _path> | :auto:.
eb680 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
eb690 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79  base.]lock_proxy
eb6a0 5f 66 69 6c 65 0a 2a 2a 0a 2a 2a 20 53 70 65 63  _file.**.** Spec
eb6b0 69 66 79 69 6e 67 20 22 3a 61 75 74 6f 3a 22 20  ifying ":auto:" 
eb6c0 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20 74 68  means that if th
eb6d0 65 72 65 20 69 73 20 61 20 63 6f 6e 63 68 20 66  ere is a conch f
eb6e0 69 6c 65 20 77 69 74 68 20 61 20 6d 61 74 63 68  ile with a match
eb6f0 69 6e 67 0a 2a 2a 20 68 6f 73 74 20 49 44 20 69  ing.** host ID i
eb700 6e 20 69 74 2c 20 74 68 65 20 70 72 6f 78 79 20  n it, the proxy 
eb710 70 61 74 68 20 69 6e 20 74 68 65 20 63 6f 6e 63  path in the conc
eb720 68 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75  h file will be u
eb730 73 65 64 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a  sed, otherwise.*
eb740 2a 20 61 20 70 72 6f 78 79 20 70 61 74 68 20 62  * a proxy path b
eb750 61 73 65 64 20 6f 6e 20 74 68 65 20 75 73 65 72  ased on the user
eb760 27 73 20 74 65 6d 70 20 64 69 72 0a 2a 2a 20 28  's temp dir.** (
eb770 76 69 61 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f  via confstr(_CS_
eb780 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50  DARWIN_USER_TEMP
eb790 5f 44 49 52 2c 2e 2e 2e 29 29 20 77 69 6c 6c 20  _DIR,...)) will 
eb7a0 62 65 20 75 73 65 64 20 61 6e 64 20 74 68 65 0a  be used and the.
eb7b0 2a 2a 20 61 63 74 75 61 6c 20 70 72 6f 78 79 20  ** actual proxy 
eb7c0 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 67 65 6e  file name is gen
eb7d0 65 72 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20  erated from the 
eb7e0 6e 61 6d 65 20 61 6e 64 20 70 61 74 68 20 6f 66  name and path of
eb7f0 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
eb800 20 66 69 6c 65 2e 20 20 46 6f 72 20 65 78 61 6d   file.  For exam
eb810 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ple:.**.**      
eb820 20 46 6f 72 20 64 61 74 61 62 61 73 65 20 70 61   For database pa
eb830 74 68 20 22 2f 55 73 65 72 73 2f 6d 65 2f 66 6f  th "/Users/me/fo
eb840 6f 2e 64 62 22 20 0a 2a 2a 20 20 20 20 20 20 20  o.db" .**       
eb850 54 68 65 20 6c 6f 63 6b 20 70 61 74 68 20 77 69  The lock path wi
eb860 6c 6c 20 62 65 20 22 3c 74 6d 70 64 69 72 3e 2f  ll be "<tmpdir>/
eb870 73 71 6c 69 74 65 70 6c 6f 63 6b 73 2f 5f 55 73  sqliteplocks/_Us
eb880 65 72 73 5f 6d 65 5f 66 6f 6f 2e 64 62 3a 61 75  ers_me_foo.db:au
eb890 74 6f 3a 22 29 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65  to:").**.** Once
eb8a0 20 61 20 6c 6f 63 6b 20 70 72 6f 78 79 20 69 73   a lock proxy is
eb8b0 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20   configured for 
eb8c0 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
eb8d0 63 74 69 6f 6e 2c 20 69 74 20 63 61 6e 20 6e 6f  ction, it can no
eb8e0 74 0a 2a 2a 20 62 65 20 72 65 6d 6f 76 65 64 2c  t.** be removed,
eb8f0 20 68 6f 77 65 76 65 72 20 69 74 20 6d 61 79 20   however it may 
eb900 62 65 20 73 77 69 74 63 68 65 64 20 74 6f 20 61  be switched to a
eb910 20 64 69 66 66 65 72 65 6e 74 20 70 72 6f 78 79   different proxy
eb920 20 70 61 74 68 20 76 69 61 0a 2a 2a 20 74 68 65   path via.** the
eb930 20 61 62 6f 76 65 20 41 50 49 73 20 28 61 73 73   above APIs (ass
eb940 75 6d 69 6e 67 20 74 68 65 20 63 6f 6e 63 68 20  uming the conch 
eb950 66 69 6c 65 20 69 73 20 6e 6f 74 20 62 65 69 6e  file is not bein
eb960 67 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65  g held by anothe
eb970 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  r.** connection 
eb980 6f 72 20 70 72 6f 63 65 73 73 29 2e 20 0a 2a 2a  or process). .**
eb990 0a 2a 2a 0a 2a 2a 20 48 6f 77 20 70 72 6f 78 79  .**.** How proxy
eb9a0 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 0a 2a   locking works.*
eb9b0 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
eb9c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
eb9d0 50 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69  Proxy file locki
eb9e0 6e 67 20 72 65 6c 69 65 73 20 70 72 69 6d 61 72  ng relies primar
eb9f0 69 6c 79 20 6f 6e 20 74 77 6f 20 6e 65 77 20 73  ily on two new s
eba00 75 70 70 6f 72 74 69 6e 67 20 66 69 6c 65 73 3a  upporting files:
eba10 20 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 63 6f 6e   .**.**   *  con
eba20 63 68 20 66 69 6c 65 20 74 6f 20 6c 69 6d 69 74  ch file to limit
eba30 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 64   access to the d
eba40 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20  atabase file to 
eba50 61 20 73 69 6e 67 6c 65 20 68 6f 73 74 0a 2a 2a  a single host.**
eba60 20 20 20 20 20 20 61 74 20 61 20 74 69 6d 65 0a        at a time.
eba70 2a 2a 0a 2a 2a 20 20 20 2a 20 20 70 72 6f 78 79  **.**   *  proxy
eba80 20 66 69 6c 65 20 74 6f 20 61 63 74 20 61 73 20   file to act as 
eba90 61 20 70 72 6f 78 79 20 66 6f 72 20 74 68 65 20  a proxy for the 
ebaa0 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 6e  advisory locks n
ebab0 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 20  ormally.**      
ebac0 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 61 74  taken on the dat
ebad0 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 54 68 65 20  abase.**.** The 
ebae0 63 6f 6e 63 68 20 66 69 6c 65 20 2d 20 74 6f 20  conch file - to 
ebaf0 75 73 65 20 61 20 70 72 6f 78 79 20 66 69 6c 65  use a proxy file
ebb00 2c 20 73 71 6c 69 74 65 20 6d 75 73 74 20 66 69  , sqlite must fi
ebb10 72 73 74 20 22 68 6f 6c 64 20 74 68 65 20 63 6f  rst "hold the co
ebb20 6e 63 68 22 0a 2a 2a 20 62 79 20 74 61 6b 69 6e  nch".** by takin
ebb30 67 20 61 6e 20 73 71 6c 69 74 65 2d 73 74 79 6c  g an sqlite-styl
ebb40 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e  e shared lock on
ebb50 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2c   the conch file,
ebb60 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a 2a 20   reading the.** 
ebb70 63 6f 6e 74 65 6e 74 73 20 61 6e 64 20 63 6f 6d  contents and com
ebb80 70 61 72 69 6e 67 20 74 68 65 20 68 6f 73 74 27  paring the host'
ebb90 73 20 75 6e 69 71 75 65 20 68 6f 73 74 20 49 44  s unique host ID
ebba0 20 28 73 65 65 20 62 65 6c 6f 77 29 20 61 6e 64   (see below) and
ebbb0 20 6c 6f 63 6b 0a 2a 2a 20 70 72 6f 78 79 20 70   lock.** proxy p
ebbc0 61 74 68 20 61 67 61 69 6e 73 74 20 74 68 65 20  ath against the 
ebbd0 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e  values stored in
ebbe0 20 74 68 65 20 63 6f 6e 63 68 2e 20 20 54 68 65   the conch.  The
ebbf0 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 0a 2a   conch file is.*
ebc00 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  * stored in the 
ebc10 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79 20 61  same directory a
ebc20 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
ebc30 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69 6c 65  ile and the file
ebc40 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 70 61 74 74   name.** is patt
ebc50 65 72 6e 65 64 20 61 66 74 65 72 20 74 68 65 20  erned after the 
ebc60 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61  database file na
ebc70 6d 65 20 61 73 20 22 2e 3c 64 61 74 61 62 61 73  me as ".<databas
ebc80 65 6e 61 6d 65 3e 2d 63 6f 6e 63 68 22 2e 0a 2a  ename>-conch"..*
ebc90 2a 20 49 66 20 74 68 65 20 63 6f 6e 63 68 20 66  * If the conch f
ebca0 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ile does not exi
ebcb0 73 74 2c 20 6f 72 20 69 74 27 73 20 63 6f 6e 74  st, or it's cont
ebcc0 65 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 74 63  ents do not matc
ebcd0 68 20 74 68 65 0a 2a 2a 20 68 6f 73 74 20 49 44  h the.** host ID
ebce0 20 61 6e 64 2f 6f 72 20 70 72 6f 78 79 20 70 61   and/or proxy pa
ebcf0 74 68 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f 63  th, then the loc
ebd00 6b 20 69 73 20 65 73 63 61 6c 61 74 65 64 20 74  k is escalated t
ebd10 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 0a 2a  o an exclusive.*
ebd20 2a 20 6c 6f 63 6b 20 61 6e 64 20 74 68 65 20 63  * lock and the c
ebd30 6f 6e 63 68 20 66 69 6c 65 20 63 6f 6e 74 65 6e  onch file conten
ebd40 74 73 20 69 73 20 75 70 64 61 74 65 64 20 77 69  ts is updated wi
ebd50 74 68 20 74 68 65 20 68 6f 73 74 20 49 44 20 61  th the host ID a
ebd60 6e 64 20 70 72 6f 78 79 0a 2a 2a 20 70 61 74 68  nd proxy.** path
ebd70 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 69 73   and the lock is
ebd80 20 64 6f 77 6e 67 72 61 64 65 64 20 74 6f 20 61   downgraded to a
ebd90 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 67 61   shared lock aga
ebda0 69 6e 2e 20 20 49 66 20 74 68 65 20 63 6f 6e 63  in.  If the conc
ebdb0 68 0a 2a 2a 20 69 73 20 68 65 6c 64 20 62 79 20  h.** is held by 
ebdc0 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  another process 
ebdd0 28 77 69 74 68 20 61 20 73 68 61 72 65 64 20 6c  (with a shared l
ebde0 6f 63 6b 29 2c 20 74 68 65 20 65 78 63 6c 75 73  ock), the exclus
ebdf0 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 77 69 6c 6c  ive lock.** will
ebe00 20 66 61 69 6c 20 61 6e 64 20 53 51 4c 49 54 45   fail and SQLITE
ebe10 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65  _BUSY is returne
ebe20 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f  d..**.** The pro
ebe30 78 79 20 66 69 6c 65 20 2d 20 61 20 73 69 6e 67  xy file - a sing
ebe40 6c 65 2d 62 79 74 65 20 66 69 6c 65 20 75 73 65  le-byte file use
ebe50 64 20 66 6f 72 20 61 6c 6c 20 61 64 76 69 73 6f  d for all adviso
ebe60 72 79 20 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a 2a  ry file locks.**
ebe70 20 6e 6f 72 6d 61 6c 6c 79 20 74 61 6b 65 6e 20   normally taken 
ebe80 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
ebe90 66 69 6c 65 2e 20 20 20 54 68 69 73 20 61 6c 6c  file.   This all
ebea0 6f 77 73 20 66 6f 72 20 73 61 66 65 20 73 68 61  ows for safe sha
ebeb0 72 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 64  ring.** of the d
ebec0 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72  atabase file for
ebed0 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72   multiple reader
ebee0 73 20 61 6e 64 20 77 72 69 74 65 72 73 20 6f 6e  s and writers on
ebef0 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 68 6f 73   the same.** hos
ebf00 74 20 28 74 68 65 20 63 6f 6e 63 68 20 65 6e 73  t (the conch ens
ebf10 75 72 65 73 20 74 68 61 74 20 74 68 65 79 20 61  ures that they a
ebf20 6c 6c 20 75 73 65 20 74 68 65 20 73 61 6d 65 20  ll use the same 
ebf30 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 29  local lock file)
ebf40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73  ..**.** There is
ebf50 20 61 20 74 68 69 72 64 20 66 69 6c 65 20 2d 20   a third file - 
ebf60 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65  the host ID file
ebf70 20 2d 20 75 73 65 64 20 61 73 20 61 20 70 65 72   - used as a per
ebf80 73 69 73 74 65 6e 74 20 72 65 63 6f 72 64 0a 2a  sistent record.*
ebf90 2a 20 6f 66 20 61 20 75 6e 69 71 75 65 20 69 64  * of a unique id
ebfa0 65 6e 74 69 66 69 65 72 20 66 6f 72 20 74 68 65  entifier for the
ebfb0 20 68 6f 73 74 2c 20 61 20 31 32 38 2d 62 79 74   host, a 128-byt
ebfc0 65 20 75 6e 69 71 75 65 20 68 6f 73 74 20 69 64  e unique host id
ebfd0 20 66 69 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20   file.** in the 
ebfe0 70 61 74 68 20 64 65 66 69 6e 65 64 20 62 79 20  path defined by 
ebff0 74 68 65 20 48 4f 53 54 49 44 50 41 54 48 20 6d  the HOSTIDPATH m
ec000 61 63 72 6f 20 28 64 65 66 61 75 6c 74 20 76 61  acro (default va
ec010 6c 75 65 20 69 73 0a 2a 2a 20 2f 4c 69 62 72 61  lue is.** /Libra
ec020 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d 2e 61  ry/Caches/.com.a
ec030 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e 63 68  pple.sqliteConch
ec040 48 6f 73 74 49 64 29 2e 0a 2a 2a 0a 2a 2a 20 52  HostId)..**.** R
ec050 65 71 75 65 73 74 69 6e 67 20 74 68 65 20 6c 6f  equesting the lo
ec060 63 6b 20 70 72 6f 78 79 20 64 6f 65 73 20 6e 6f  ck proxy does no
ec070 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 61  t immediately ta
ec080 6b 65 20 74 68 65 20 63 6f 6e 63 68 2c 20 69 74  ke the conch, it
ec090 20 69 73 0a 2a 2a 20 6f 6e 6c 79 20 74 61 6b 65   is.** only take
ec0a0 6e 20 77 68 65 6e 20 74 68 65 20 66 69 72 73 74  n when the first
ec0b0 20 72 65 71 75 65 73 74 20 74 6f 20 6c 6f 63 6b   request to lock
ec0c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
ec0d0 73 20 6d 61 64 65 2e 20 20 0a 2a 2a 20 54 68 69  s made.  .** Thi
ec0e0 73 20 6d 61 74 63 68 65 73 20 74 68 65 20 73 65  s matches the se
ec0f0 6d 61 6e 74 69 63 73 20 6f 66 20 74 68 65 20 74  mantics of the t
ec100 72 61 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 69  raditional locki
ec110 6e 67 20 62 65 68 61 76 69 6f 72 2c 20 77 68 65  ng behavior, whe
ec120 72 65 0a 2a 2a 20 6f 70 65 6e 69 6e 67 20 61 20  re.** opening a 
ec130 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20  connection to a 
ec140 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 64 6f  database file do
ec150 65 73 20 6e 6f 74 20 74 61 6b 65 20 61 20 6c 6f  es not take a lo
ec160 63 6b 20 6f 6e 20 69 74 2e 0a 2a 2a 20 54 68 65  ck on it..** The
ec170 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64   shared lock and
ec180 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 64 65   an open file de
ec190 73 63 72 69 70 74 6f 72 20 61 72 65 20 6d 61 69  scriptor are mai
ec1a0 6e 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 0a 2a  ntained until .*
ec1b0 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  * the connection
ec1c0 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
ec1d0 20 69 73 20 63 6c 6f 73 65 64 2e 20 0a 2a 2a 0a   is closed. .**.
ec1e0 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 66 69 6c  ** The proxy fil
ec1f0 65 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 66  e and the lock f
ec200 69 6c 65 20 61 72 65 20 6e 65 76 65 72 20 64 65  ile are never de
ec210 6c 65 74 65 64 20 73 6f 20 74 68 65 79 20 6f 6e  leted so they on
ec220 6c 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65  ly need.** to be
ec230 20 63 72 65 61 74 65 64 20 74 68 65 20 66 69 72   created the fir
ec240 73 74 20 74 69 6d 65 20 74 68 65 79 20 61 72 65  st time they are
ec250 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e   used..**.** Con
ec260 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
ec270 6e 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ns.** ----------
ec280 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a  -----------.**.*
ec290 2a 20 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52  *  SQLITE_PREFER
ec2a0 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0a 2a  _PROXY_LOCKING.*
ec2b0 2a 0a 2a 2a 20 20 20 20 20 20 20 44 61 74 61 62  *.**       Datab
ec2c0 61 73 65 20 66 69 6c 65 73 20 61 63 63 65 73 73  ase files access
ec2d0 65 64 20 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61 6c 20  ed on non-local 
ec2e0 66 69 6c 65 20 73 79 73 74 65 6d 73 20 61 72 65  file systems are
ec2f0 0a 2a 2a 20 20 20 20 20 20 20 61 75 74 6f 6d 61  .**       automa
ec300 74 69 63 61 6c 6c 79 20 63 6f 6e 66 69 67 75 72  tically configur
ec310 65 64 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63  ed for proxy loc
ec320 6b 69 6e 67 2c 20 6c 6f 63 6b 20 66 69 6c 65 73  king, lock files
ec330 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 20 6e 61   are.**       na
ec340 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  med automaticall
ec350 79 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65  y using the same
ec360 20 6c 6f 67 69 63 20 61 73 0a 2a 2a 20 20 20 20   logic as.**    
ec370 20 20 20 50 52 41 47 4d 41 20 6c 6f 63 6b 5f 70     PRAGMA lock_p
ec380 72 6f 78 79 5f 66 69 6c 65 3d 22 3a 61 75 74 6f  roxy_file=":auto
ec390 3a 22 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 53 51  :".**    .**  SQ
ec3a0 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47  LITE_PROXY_DEBUG
ec3b0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 45 6e 61  .**.**       Ena
ec3c0 62 6c 65 73 20 74 68 65 20 6c 6f 67 67 69 6e 67  bles the logging
ec3d0 20 6f 66 20 65 72 72 6f 72 20 6d 65 73 73 61 67   of error messag
ec3e0 65 73 20 64 75 72 69 6e 67 20 68 6f 73 74 20 69  es during host i
ec3f0 64 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 20 20  d file.**       
ec400 72 65 74 72 69 65 76 61 6c 20 61 6e 64 20 63 72  retrieval and cr
ec410 65 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 48 4f  eation.**.**  HO
ec420 53 54 49 44 50 41 54 48 0a 2a 2a 0a 2a 2a 20 20  STIDPATH.**.**  
ec430 20 20 20 20 20 4f 76 65 72 72 69 64 65 73 20 74       Overrides t
ec440 68 65 20 64 65 66 61 75 6c 74 20 68 6f 73 74 20  he default host 
ec450 49 44 20 66 69 6c 65 20 70 61 74 68 20 6c 6f 63  ID file path loc
ec460 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43  ation.**.**  LOC
ec470 4b 50 52 4f 58 59 44 49 52 0a 2a 2a 0a 2a 2a 20  KPROXYDIR.**.** 
ec480 20 20 20 20 20 20 4f 76 65 72 72 69 64 65 73 20        Overrides 
ec490 74 68 65 20 64 65 66 61 75 6c 74 20 64 69 72 65  the default dire
ec4a0 63 74 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c  ctory used for l
ec4b0 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c 65 73 20  ock proxy files 
ec4c0 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 61 72  that.**       ar
ec4d0 65 20 6e 61 6d 65 64 20 61 75 74 6f 6d 61 74 69  e named automati
ec4e0 63 61 6c 6c 79 20 76 69 61 20 74 68 65 20 22 3a  cally via the ":
ec4f0 61 75 74 6f 3a 22 20 73 65 74 74 69 6e 67 0a 2a  auto:" setting.*
ec500 2a 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 44 45 46  *.**  SQLITE_DEF
ec510 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45  AULT_PROXYDIR_PE
ec520 52 4d 49 53 53 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20  RMISSIONS.**.** 
ec530 20 20 20 20 20 20 50 65 72 6d 69 73 73 69 6f 6e        Permission
ec540 73 20 74 6f 20 75 73 65 20 77 68 65 6e 20 63 72  s to use when cr
ec550 65 61 74 69 6e 67 20 61 20 64 69 72 65 63 74 6f  eating a directo
ec560 72 79 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74  ry for storing t
ec570 68 65 0a 2a 2a 20 20 20 20 20 20 20 6c 6f 63 6b  he.**       lock
ec580 20 70 72 6f 78 79 20 66 69 6c 65 73 2c 20 6f 6e   proxy files, on
ec590 6c 79 20 75 73 65 64 20 77 68 65 6e 20 4c 4f 43  ly used when LOC
ec5a0 4b 50 52 4f 58 59 44 49 52 20 69 73 20 6e 6f 74  KPROXYDIR is not
ec5b0 20 73 65 74 2e 0a 2a 2a 20 20 20 20 0a 2a 2a 20   set..**    .** 
ec5c0 20 20 20 0a 2a 2a 20 41 73 20 6d 65 6e 74 69 6f     .** As mentio
ec5d0 6e 65 64 20 61 62 6f 76 65 2c 20 77 68 65 6e 20  ned above, when 
ec5e0 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51  compiled with SQ
ec5f0 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58  LITE_PREFER_PROX
ec600 59 5f 4c 4f 43 4b 49 4e 47 2c 0a 2a 2a 20 73 65  Y_LOCKING,.** se
ec610 74 74 69 6e 67 20 74 68 65 20 65 6e 76 69 72 6f  tting the enviro
ec620 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20 53  nment variable S
ec630 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58  QLITE_FORCE_PROX
ec640 59 5f 4c 4f 43 4b 49 4e 47 20 74 6f 20 31 20 77  Y_LOCKING to 1 w
ec650 69 6c 6c 0a 2a 2a 20 66 6f 72 63 65 20 70 72 6f  ill.** force pro
ec660 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65  xy locking to be
ec670 20 75 73 65 64 20 66 6f 72 20 65 76 65 72 79 20   used for every 
ec680 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 70  database file op
ec690 65 6e 65 64 2c 20 61 6e 64 20 30 0a 2a 2a 20 77  ened, and 0.** w
ec6a0 69 6c 6c 20 66 6f 72 63 65 20 61 75 74 6f 6d 61  ill force automa
ec6b0 74 69 63 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  tic proxy lockin
ec6c0 67 20 74 6f 20 62 65 20 64 69 73 61 62 6c 65 64  g to be disabled
ec6d0 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73   for all databas
ec6e0 65 0a 2a 2a 20 66 69 6c 65 73 20 28 65 78 70 6c  e.** files (expl
ec6f0 69 63 69 74 79 20 63 61 6c 6c 69 6e 67 20 74 68  icity calling th
ec700 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43  e SQLITE_SET_LOC
ec710 4b 50 52 4f 58 59 46 49 4c 45 20 70 72 61 67 6d  KPROXYFILE pragm
ec720 61 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 5f 66  a or.** sqlite_f
ec730 69 6c 65 5f 63 6f 6e 74 72 6f 6c 20 41 50 49 20  ile_control API 
ec740 69 73 20 6e 6f 74 20 61 66 66 65 63 74 65 64 20  is not affected 
ec750 62 79 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f  by SQLITE_FORCE_
ec760 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 29 2e 0a  PROXY_LOCKING)..
ec770 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 78 79 20  */../*.** Proxy 
ec780 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20  locking is only 
ec790 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63  available on Mac
ec7a0 4f 53 58 20 0a 2a 2f 0a 23 69 66 20 64 65 66 69  OSX .*/.#if defi
ec7b0 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26  ned(__APPLE__) &
ec7c0 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
ec7d0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 0a 23  LOCKING_STYLE..#
ec7e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
ec7f0 54 0a 2f 2a 20 73 69 6d 75 6c 61 74 65 20 6d 75  T./* simulate mu
ec800 6c 74 69 70 6c 65 20 68 6f 73 74 73 20 62 79 20  ltiple hosts by 
ec810 63 72 65 61 74 69 6e 67 20 75 6e 69 71 75 65 20  creating unique 
ec820 68 6f 73 74 69 64 20 66 69 6c 65 20 70 61 74 68  hostid file path
ec830 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  s */.SQLITE_API 
ec840 69 6e 74 20 73 71 6c 69 74 65 33 5f 68 6f 73 74  int sqlite3_host
ec850 69 64 5f 6e 75 6d 20 3d 20 30 3b 0a 23 65 6e 64  id_num = 0;.#end
ec860 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72  if../*.** The pr
ec870 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
ec880 74 20 68 61 73 20 74 68 65 20 70 61 74 68 20 61  t has the path a
ec890 6e 64 20 66 69 6c 65 20 73 74 72 75 63 74 75 72  nd file structur
ec8a0 65 73 20 66 6f 72 20 74 68 65 20 72 65 6d 6f 74  es for the remot
ec8b0 65 20 0a 2a 2a 20 61 6e 64 20 6c 6f 63 61 6c 20  e .** and local 
ec8c0 70 72 6f 78 79 20 66 69 6c 65 73 20 69 6e 20 69  proxy files in i
ec8d0 74 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  t.*/.typedef str
ec8e0 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67  uct proxyLocking
ec8f0 43 6f 6e 74 65 78 74 20 70 72 6f 78 79 4c 6f 63  Context proxyLoc
ec900 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72  kingContext;.str
ec910 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67  uct proxyLocking
ec920 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e 69 78  Context {.  unix
ec930 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b  File *conchFile;
ec940 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e           /* Open
ec950 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 20   conch file */. 
ec960 20 63 68 61 72 20 2a 63 6f 6e 63 68 46 69 6c 65   char *conchFile
ec970 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a  Path;         /*
ec980 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e   Name of the con
ec990 63 68 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69  ch file */.  uni
ec9a0 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79  xFile *lockProxy
ec9b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65  ;         /* Ope
ec9c0 6e 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c  n proxy lock fil
ec9d0 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c 6f 63  e */.  char *loc
ec9e0 6b 50 72 6f 78 79 50 61 74 68 3b 20 20 20 20 20  kProxyPath;     
ec9f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
eca00 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69  he proxy lock fi
eca10 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 64 62  le */.  char *db
eca20 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  Path;           
eca30 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
eca40 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f  the open file */
eca50 0a 20 20 69 6e 74 20 63 6f 6e 63 68 48 65 6c 64  .  int conchHeld
eca60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
eca70 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 63  /* True if the c
eca80 6f 6e 63 68 20 69 73 20 63 75 72 72 65 6e 74 6c  onch is currentl
eca90 79 20 68 65 6c 64 20 2a 2f 0a 20 20 76 6f 69 64  y held */.  void
ecaa0 20 2a 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74   *oldLockingCont
ecab0 65 78 74 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67  ext;     /* Orig
ecac0 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 63 6f 6e 74  inal lockingcont
ecad0 65 78 74 20 74 6f 20 72 65 73 74 6f 72 65 20 6f  ext to restore o
ecae0 6e 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 73 71 6c  n close */.  sql
ecaf0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
ecb00 63 6f 6e 73 74 20 2a 70 4f 6c 64 4d 65 74 68 6f  const *pOldMetho
ecb10 64 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e  d;     /* Origin
ecb20 61 6c 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 66  al I/O methods f
ecb30 6f 72 20 63 6c 6f 73 65 20 2a 2f 0a 7d 3b 0a 0a  or close */.};..
ecb40 2f 2a 20 48 4f 53 54 49 44 4c 45 4e 20 61 6e 64  /* HOSTIDLEN and
ecb50 20 43 4f 4e 43 48 4c 45 4e 20 62 6f 74 68 20 69   CONCHLEN both i
ecb60 6e 63 6c 75 64 65 20 73 70 61 63 65 20 66 6f 72  nclude space for
ecb70 20 74 68 65 20 73 74 72 69 6e 67 20 0a 2a 2a 20   the string .** 
ecb80 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 20  terminating nul 
ecb90 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 48 4f 53 54  .*/.#define HOST
ecba0 49 44 4c 45 4e 20 20 20 20 20 20 20 20 20 31 32  IDLEN         12
ecbb0 38 0a 23 64 65 66 69 6e 65 20 43 4f 4e 43 48 4c  8.#define CONCHL
ecbc0 45 4e 20 20 20 20 20 20 20 20 20 20 28 4d 41 58  EN          (MAX
ecbd0 50 41 54 48 4c 45 4e 2b 48 4f 53 54 49 44 4c 45  PATHLEN+HOSTIDLE
ecbe0 4e 2b 31 29 0a 23 69 66 6e 64 65 66 20 48 4f 53  N+1).#ifndef HOS
ecbf0 54 49 44 50 41 54 48 0a 23 20 64 65 66 69 6e 65  TIDPATH.# define
ecc00 20 48 4f 53 54 49 44 50 41 54 48 20 20 20 20 20   HOSTIDPATH     
ecc10 20 20 22 2f 4c 69 62 72 61 72 79 2f 43 61 63 68    "/Library/Cach
ecc20 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71  es/.com.apple.sq
ecc30 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 22  liteConchHostId"
ecc40 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 62 61 73 69  .#endif../* basi
ecc50 63 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20  cally a copy of 
ecc60 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 20 77  unixRandomness w
ecc70 69 74 68 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a  ith different.**
ecc80 20 74 65 73 74 20 62 65 68 61 76 69 6f 72 20 62   test behavior b
ecc90 75 69 6c 74 20 69 6e 20 2a 2f 0a 73 74 61 74 69  uilt in */.stati
ecca0 63 20 69 6e 74 20 70 72 6f 78 79 47 65 6e 65 72  c int proxyGener
eccb0 61 74 65 48 6f 73 74 49 44 28 63 68 61 72 20 2a  ateHostID(char *
eccc0 70 48 6f 73 74 49 44 29 7b 0a 20 20 69 6e 74 20  pHostID){.  int 
eccd0 70 69 64 2c 20 66 64 2c 20 6c 65 6e 3b 0a 20 20  pid, fd, len;.  
ecce0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6b  unsigned char *k
eccf0 65 79 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ey = (unsigned c
ecd00 68 61 72 20 2a 29 70 48 6f 73 74 49 44 3b 0a 20  har *)pHostID;. 
ecd10 20 0a 20 20 6d 65 6d 73 65 74 28 6b 65 79 2c 20   .  memset(key, 
ecd20 30 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20  0, HOSTIDLEN);. 
ecd30 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 66 64 20 3d   len = 0;.  fd =
ecd40 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e   open("/dev/uran
ecd50 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b  dom", O_RDONLY);
ecd60 0a 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a  .  if( fd>=0 ){.
ecd70 20 20 20 20 6c 65 6e 20 3d 20 72 65 61 64 28 66      len = read(f
ecd80 64 2c 20 6b 65 79 2c 20 48 4f 53 54 49 44 4c 45  d, key, HOSTIDLE
ecd90 4e 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28 66 64  N);.    close(fd
ecda0 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c  ); /* silently l
ecdb0 65 61 6b 20 74 68 65 20 66 64 20 69 66 20 69 74  eak the fd if it
ecdc0 20 66 61 69 6c 73 20 2a 2f 0a 20 20 7d 0a 20 20   fails */.  }.  
ecdd0 69 66 28 20 6c 65 6e 20 3c 20 48 4f 53 54 49 44  if( len < HOSTID
ecde0 4c 45 4e 20 29 7b 0a 20 20 20 20 74 69 6d 65 5f  LEN ){.    time_
ecdf0 74 20 74 3b 0a 20 20 20 20 74 69 6d 65 28 26 74  t t;.    time(&t
ece00 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6b 65  );.    memcpy(ke
ece10 79 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29  y, &t, sizeof(t)
ece20 29 3b 0a 20 20 20 20 70 69 64 20 3d 20 67 65 74  );.    pid = get
ece30 70 69 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70  pid();.    memcp
ece40 79 28 26 6b 65 79 5b 73 69 7a 65 6f 66 28 74 29  y(&key[sizeof(t)
ece50 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28  ], &pid, sizeof(
ece60 70 69 64 29 29 3b 0a 20 20 7d 0a 20 20 0a 23 69  pid));.  }.  .#i
ece70 66 64 65 66 20 4d 41 4b 45 5f 50 52 45 54 54 59  fdef MAKE_PRETTY
ece80 5f 48 4f 53 54 49 44 0a 20 20 7b 0a 20 20 20 20  _HOSTID.  {.    
ece90 69 6e 74 20 69 3b 0a 20 20 20 20 2f 2a 20 66 69  int i;.    /* fi
ecea0 6c 74 65 72 20 74 68 65 20 62 79 74 65 73 20 69  lter the bytes i
eceb0 6e 74 6f 20 70 72 69 6e 74 61 62 6c 65 20 61 73  nto printable as
ecec0 63 69 69 20 63 68 61 72 61 63 74 65 72 73 20 61  cii characters a
eced0 6e 64 20 4e 55 4c 20 74 65 72 6d 69 6e 61 74 65  nd NUL terminate
ecee0 20 2a 2f 0a 20 20 20 20 6b 65 79 5b 28 48 4f 53   */.    key[(HOS
ecef0 54 49 44 4c 45 4e 2d 31 29 5d 20 3d 20 30 78 30  TIDLEN-1)] = 0x0
ecf00 30 3b 0a 20 20 20 20 66 6f 72 28 20 69 3d 30 3b  0;.    for( i=0;
ecf10 20 69 3c 28 48 4f 53 54 49 44 4c 45 4e 2d 31 29   i<(HOSTIDLEN-1)
ecf20 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 75  ; i++ ){.      u
ecf30 6e 73 69 67 6e 65 64 20 63 68 61 72 20 70 61 20  nsigned char pa 
ecf40 3d 20 6b 65 79 5b 69 5d 26 30 78 37 46 3b 0a 20  = key[i]&0x7F;. 
ecf50 20 20 20 20 20 69 66 28 20 70 61 3c 30 78 32 30       if( pa<0x20
ecf60 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 5b   ){.        key[
ecf70 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38  i] = (key[i]&0x8
ecf80 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 2b  0 == 0x80) ? pa+
ecf90 30 78 34 30 20 3a 20 70 61 2b 30 78 32 30 3b 0a  0x40 : pa+0x20;.
ecfa0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
ecfb0 70 61 3d 3d 30 78 37 46 20 29 7b 0a 20 20 20 20  pa==0x7F ){.    
ecfc0 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65      key[i] = (ke
ecfd0 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 38  y[i]&0x80 == 0x8
ecfe0 30 29 20 3f 20 70 61 3d 30 78 32 30 20 3a 20 70  0) ? pa=0x20 : p
ecff0 61 2b 30 78 37 45 3b 0a 20 20 20 20 20 20 7d 0a  a+0x7E;.      }.
ed000 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
ed010 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
ed020 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 77 72 69 74 65  _OK;.}../* write
ed030 73 20 74 68 65 20 68 6f 73 74 20 69 64 20 70 61  s the host id pa
ed040 74 68 20 74 6f 20 70 61 74 68 2c 20 70 61 74 68  th to path, path
ed050 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 70 72   should be an pr
ed060 65 2d 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66  e-allocated buff
ed070 65 72 0a 2a 2a 20 77 69 74 68 20 65 6e 6f 75 67  er.** with enoug
ed080 68 20 73 70 61 63 65 20 66 6f 72 20 61 20 70 61  h space for a pa
ed090 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  th .*/.static vo
ed0a0 69 64 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49  id proxyGetHostI
ed0b0 44 50 61 74 68 28 63 68 61 72 20 2a 70 61 74 68  DPath(char *path
ed0c0 2c 20 73 69 7a 65 5f 74 20 6c 65 6e 29 7b 0a 20  , size_t len){. 
ed0d0 20 73 74 72 6c 63 70 79 28 70 61 74 68 2c 20 48   strlcpy(path, H
ed0e0 4f 53 54 49 44 50 41 54 48 2c 20 6c 65 6e 29 3b  OSTIDPATH, len);
ed0f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
ed100 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  EST.  if( sqlite
ed110 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 3e 30 20 29  3_hostid_num>0 )
ed120 7b 0a 20 20 20 20 63 68 61 72 20 73 75 66 66 69  {.    char suffi
ed130 78 5b 32 5d 20 3d 20 22 31 22 3b 0a 20 20 20 20  x[2] = "1";.    
ed140 73 75 66 66 69 78 5b 30 5d 20 3d 20 73 75 66 66  suffix[0] = suff
ed150 69 78 5b 30 5d 20 2b 20 73 71 6c 69 74 65 33 5f  ix[0] + sqlite3_
ed160 68 6f 73 74 69 64 5f 6e 75 6d 3b 0a 20 20 20 20  hostid_num;.    
ed170 73 74 72 6c 63 61 74 28 70 61 74 68 2c 20 73 75  strlcat(path, su
ed180 66 66 69 78 2c 20 6c 65 6e 29 3b 0a 20 20 7d 0a  ffix, len);.  }.
ed190 23 65 6e 64 69 66 0a 20 20 4f 53 54 52 41 43 45  #endif.  OSTRACE
ed1a0 33 28 22 47 45 54 48 4f 53 54 49 44 50 41 54 48  3("GETHOSTIDPATH
ed1b0 20 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20    %s pid=%d\n", 
ed1c0 70 61 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b  path, getpid());
ed1d0 0a 7d 0a 0a 2f 2a 20 67 65 74 20 74 68 65 20 68  .}../* get the h
ed1e0 6f 73 74 20 49 44 20 66 72 6f 6d 20 61 20 73 71  ost ID from a sq
ed1f0 6c 69 74 65 20 68 6f 73 74 69 64 20 66 69 6c 65  lite hostid file
ed200 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 0a   stored in the .
ed210 2a 2a 20 75 73 65 72 2d 73 70 65 63 69 66 69 63  ** user-specific
ed220 20 74 6d 70 20 64 69 72 65 63 74 6f 72 79 2c 20   tmp directory, 
ed230 63 72 65 61 74 65 20 74 68 65 20 49 44 20 69 66  create the ID if
ed240 20 69 74 27 73 20 6e 6f 74 20 74 68 65 72 65 20   it's not there 
ed250 61 6c 72 65 61 64 79 20 0a 2a 2f 0a 73 74 61 74  already .*/.stat
ed260 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 74 48  ic int proxyGetH
ed270 6f 73 74 49 44 28 63 68 61 72 20 2a 70 48 6f 73  ostID(char *pHos
ed280 74 49 44 2c 20 69 6e 74 20 2a 70 45 72 72 6f 72  tID, int *pError
ed290 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 63  ){.  int fd;.  c
ed2a0 68 61 72 20 70 61 74 68 5b 4d 41 58 50 41 54 48  har path[MAXPATH
ed2b0 4c 45 4e 5d 3b 20 0a 20 20 73 69 7a 65 5f 74 20  LEN]; .  size_t 
ed2c0 6c 65 6e 3b 0a 20 20 69 6e 74 20 72 63 3d 53 51  len;.  int rc=SQ
ed2d0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 70 72 6f 78  LITE_OK;..  prox
ed2e0 79 47 65 74 48 6f 73 74 49 44 50 61 74 68 28 70  yGetHostIDPath(p
ed2f0 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29  ath, MAXPATHLEN)
ed300 3b 0a 20 20 2f 2a 20 74 72 79 20 74 6f 20 63 72  ;.  /* try to cr
ed310 65 61 74 65 20 74 68 65 20 68 6f 73 74 20 49 44  eate the host ID
ed320 20 66 69 6c 65 2c 20 69 66 20 69 74 20 61 6c 72   file, if it alr
ed330 65 61 64 79 20 65 78 69 73 74 73 20 72 65 61 64  eady exists read
ed340 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 2a 2f   the contents */
ed350 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74  .  fd = open(pat
ed360 68 2c 20 4f 5f 43 52 45 41 54 7c 4f 5f 57 52 4f  h, O_CREAT|O_WRO
ed370 4e 4c 59 7c 4f 5f 45 58 43 4c 2c 20 30 36 34 34  NLY|O_EXCL, 0644
ed380 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b  );.  if( fd<0 ){
ed390 0a 20 20 20 20 69 6e 74 20 65 72 72 3d 65 72 72  .    int err=err
ed3a0 6e 6f 3b 0a 09 09 0a 20 20 20 20 69 66 28 20 65  no;....    if( e
ed3b0 72 72 21 3d 45 45 58 49 53 54 20 29 7b 0a 23 69  rr!=EEXIST ){.#i
ed3c0 66 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f 58  fdef SQLITE_PROX
ed3d0 59 5f 44 45 42 55 47 20 2f 2a 20 73 65 74 20 74  Y_DEBUG /* set t
ed3e0 68 65 20 73 71 6c 69 74 65 20 65 72 72 6f 72 20  he sqlite error 
ed3f0 6d 65 73 73 61 67 65 20 69 6e 73 74 65 61 64 20  message instead 
ed400 2a 2f 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66  */.      fprintf
ed410 28 73 74 64 65 72 72 2c 20 22 73 71 6c 69 74 65  (stderr, "sqlite
ed420 20 65 72 72 6f 72 20 63 72 65 61 74 69 6e 67 20   error creating 
ed430 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25 73 3a  host ID file %s:
ed440 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %s\n",.        
ed450 20 20 20 20 20 20 70 61 74 68 2c 20 73 74 72 65        path, stre
ed460 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65 6e 64  rror(err));.#end
ed470 69 66 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  if.      return 
ed480 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20  SQLITE_PERM;.   
ed490 20 7d 0a 20 20 20 20 2f 2a 20 63 6f 75 6c 64 6e   }.    /* couldn
ed4a0 27 74 20 63 72 65 61 74 65 20 74 68 65 20 66 69  't create the fi
ed4b0 6c 65 2c 20 72 65 61 64 20 69 74 20 69 6e 73 74  le, read it inst
ed4c0 65 61 64 20 2a 2f 0a 20 20 20 20 66 64 20 3d 20  ead */.    fd = 
ed4d0 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 52 44 4f  open(path, O_RDO
ed4e0 4e 4c 59 7c 4f 5f 45 58 43 4c 29 3b 0a 20 20 20  NLY|O_EXCL);.   
ed4f0 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 23 69 66   if( fd<0 ){.#if
ed500 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 59  def SQLITE_PROXY
ed510 5f 44 45 42 55 47 20 2f 2a 20 73 65 74 20 74 68  _DEBUG /* set th
ed520 65 20 73 71 6c 69 74 65 20 65 72 72 6f 72 20 6d  e sqlite error m
ed530 65 73 73 61 67 65 20 69 6e 73 74 65 61 64 20 2a  essage instead *
ed540 2f 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 20  /.      int err 
ed550 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 66  = errno;.      f
ed560 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
ed570 73 71 6c 69 74 65 20 65 72 72 6f 72 20 6f 70 65  sqlite error ope
ed580 6e 69 6e 67 20 68 6f 73 74 20 49 44 20 66 69 6c  ning host ID fil
ed590 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a 20 20 20  e %s: %s\n",.   
ed5a0 20 20 20 20 20 20 20 20 20 20 20 70 61 74 68 2c             path,
ed5b0 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 3b   strerror(err));
ed5c0 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 65  .#endif.      re
ed5d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d  turn SQLITE_PERM
ed5e0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20  ;.    }.    len 
ed5f0 3d 20 70 72 65 61 64 28 66 64 2c 20 70 48 6f 73  = pread(fd, pHos
ed600 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20  tID, HOSTIDLEN, 
ed610 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c  0);.    if( len<
ed620 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72  0 ){.      *pErr
ed630 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  or = errno;.    
ed640 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
ed650 45 52 52 5f 52 45 41 44 3b 0a 20 20 20 20 7d 65  ERR_READ;.    }e
ed660 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f 53 54  lse if( len<HOST
ed670 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 20 2a  IDLEN ){.      *
ed680 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pError = 0;.    
ed690 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
ed6a0 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a  ERR_SHORT_READ;.
ed6b0 20 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28      }.    close(
ed6c0 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79  fd); /* silently
ed6d0 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 20   leak the fd if 
ed6e0 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20  it fails */.    
ed6f0 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 4f 53  OSTRACE3("GETHOS
ed700 54 49 44 20 20 72 65 61 64 20 25 73 20 70 69 64  TID  read %s pid
ed710 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c  =%d\n", pHostID,
ed720 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20   getpid());.    
ed730 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c  return rc;.  }el
ed740 73 65 7b 0a 20 20 20 20 2f 2a 20 77 65 27 72 65  se{.    /* we're
ed750 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 68 6f   creating the ho
ed760 73 74 20 49 44 20 66 69 6c 65 20 28 75 73 65 20  st ID file (use 
ed770 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e 67 20  a random string 
ed780 6f 66 20 62 79 74 65 73 29 20 2a 2f 0a 20 20 20  of bytes) */.   
ed790 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65 48 6f   proxyGenerateHo
ed7a0 73 74 49 44 28 70 48 6f 73 74 49 44 29 3b 0a 20  stID(pHostID);. 
ed7b0 20 20 20 6c 65 6e 20 3d 20 70 77 72 69 74 65 28     len = pwrite(
ed7c0 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f 53  fd, pHostID, HOS
ed7d0 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20  TIDLEN, 0);.    
ed7e0 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20  if( len<0 ){.   
ed7f0 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 72     *pError = err
ed800 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  no;.      rc = S
ed810 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54  QLITE_IOERR_WRIT
ed820 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  E;.    }else if(
ed830 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29   len<HOSTIDLEN )
ed840 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20  {.      *pError 
ed850 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
ed860 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49  SQLITE_IOERR_WRI
ed870 54 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 6c  TE;.    }.    cl
ed880 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65  ose(fd); /* sile
ed890 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 66 64  ntly leak the fd
ed8a0 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a 2f 0a   if it fails */.
ed8b0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 47 45      OSTRACE3("GE
ed8c0 54 48 4f 53 54 49 44 20 20 77 72 6f 74 65 20 25  THOSTID  wrote %
ed8d0 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 48 6f  s pid=%d\n", pHo
ed8e0 73 74 49 44 2c 20 67 65 74 70 69 64 28 29 29 3b  stID, getpid());
ed8f0 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
ed900 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e    }.}..static in
ed910 74 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61  t proxyGetLockPa
ed920 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 64  th(const char *d
ed930 62 50 61 74 68 2c 20 63 68 61 72 20 2a 6c 50 61  bPath, char *lPa
ed940 74 68 2c 20 73 69 7a 65 5f 74 20 6d 61 78 4c 65  th, size_t maxLe
ed950 6e 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20  n){.  int len;. 
ed960 20 69 6e 74 20 64 62 4c 65 6e 3b 0a 20 20 69 6e   int dbLen;.  in
ed970 74 20 69 3b 0a 0a 23 69 66 64 65 66 20 4c 4f 43  t i;..#ifdef LOC
ed980 4b 50 52 4f 58 59 44 49 52 0a 20 20 6c 65 6e 20  KPROXYDIR.  len 
ed990 3d 20 73 74 72 6c 63 70 79 28 6c 50 61 74 68 2c  = strlcpy(lPath,
ed9a0 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 2c 20 6d   LOCKPROXYDIR, m
ed9b0 61 78 4c 65 6e 29 3b 0a 23 65 6c 73 65 0a 23 20  axLen);.#else.# 
ed9c0 69 66 64 65 66 20 5f 43 53 5f 44 41 52 57 49 4e  ifdef _CS_DARWIN
ed9d0 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 0a 20  _USER_TEMP_DIR. 
ed9e0 20 7b 0a 20 20 20 20 63 6f 6e 66 73 74 72 28 5f   {.    confstr(_
ed9f0 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54  CS_DARWIN_USER_T
eda00 45 4d 50 5f 44 49 52 2c 20 6c 50 61 74 68 2c 20  EMP_DIR, lPath, 
eda10 6d 61 78 4c 65 6e 29 3b 0a 20 20 20 20 6c 65 6e  maxLen);.    len
eda20 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68   = strlcat(lPath
eda30 2c 20 22 73 71 6c 69 74 65 70 6c 6f 63 6b 73 22  , "sqliteplocks"
eda40 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 20 20 69  , maxLen);.    i
eda50 66 28 20 6d 6b 64 69 72 28 6c 50 61 74 68 2c 20  f( mkdir(lPath, 
eda60 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
eda70 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49  ROXYDIR_PERMISSI
eda80 4f 4e 53 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  ONS) ){.      /*
eda90 20 69 66 20 6d 6b 64 69 72 20 66 61 69 6c 73 2c   if mkdir fails,
edaa0 20 68 61 6e 64 6c 65 20 61 73 20 6c 6f 63 6b 20   handle as lock 
edab0 66 69 6c 65 20 63 72 65 61 74 69 6f 6e 20 66 61  file creation fa
edac0 69 6c 75 72 65 20 2a 2f 0a 23 20 20 69 66 64 65  ilure */.#  ifde
edad0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
edae0 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 65       int err = e
edaf0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20  rrno;.      if( 
edb00 65 72 72 21 3d 45 45 58 49 53 54 20 29 7b 0a 20  err!=EEXIST ){. 
edb10 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73         fprintf(s
edb20 74 64 65 72 72 2c 20 22 70 72 6f 78 79 47 65 74  tderr, "proxyGet
edb30 4c 6f 63 6b 50 61 74 68 3a 20 6d 6b 64 69 72 28  LockPath: mkdir(
edb40 25 73 2c 30 25 6f 29 20 65 72 72 6f 72 20 25 64  %s,0%o) error %d
edb50 20 25 73 5c 6e 22 2c 20 6c 50 61 74 68 2c 0a 20   %s\n", lPath,. 
edb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
edb70 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52  QLITE_DEFAULT_PR
edb80 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f  OXYDIR_PERMISSIO
edb90 4e 53 2c 20 65 72 72 2c 20 73 74 72 65 72 72 6f  NS, err, strerro
edba0 72 28 65 72 72 29 29 3b 0a 20 20 20 20 20 20 7d  r(err));.      }
edbb0 0a 23 20 20 65 6e 64 69 66 0a 20 20 20 20 7d 65  .#  endif.    }e
edbc0 6c 73 65 7b 0a 20 20 20 20 20 20 4f 53 54 52 41  lse{.      OSTRA
edbd0 43 45 33 28 22 47 45 54 4c 4f 43 4b 50 41 54 48  CE3("GETLOCKPATH
edbe0 20 20 6d 6b 64 69 72 20 25 73 20 70 69 64 3d 25    mkdir %s pid=%
edbf0 64 5c 6e 22 2c 20 6c 50 61 74 68 2c 20 67 65 74  d\n", lPath, get
edc00 70 69 64 28 29 29 3b 0a 20 20 20 20 7d 0a 20 20  pid());.    }.  
edc10 20 20 0a 20 20 7d 0a 23 20 65 6c 73 65 0a 20 20    .  }.# else.  
edc20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 28 6c 50  len = strlcpy(lP
edc30 61 74 68 2c 20 22 2f 74 6d 70 2f 22 2c 20 6d 61  ath, "/tmp/", ma
edc40 78 4c 65 6e 29 3b 0a 23 20 65 6e 64 69 66 0a 23  xLen);.# endif.#
edc50 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6c 50 61  endif..  if( lPa
edc60 74 68 5b 6c 65 6e 2d 31 5d 21 3d 27 2f 27 20 29  th[len-1]!='/' )
edc70 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c  {.    len = strl
edc80 63 61 74 28 6c 50 61 74 68 2c 20 22 2f 22 2c 20  cat(lPath, "/", 
edc90 6d 61 78 4c 65 6e 29 3b 0a 20 20 7d 0a 20 20 0a  maxLen);.  }.  .
edca0 20 20 2f 2a 20 74 72 61 6e 73 66 6f 72 6d 20 74    /* transform t
edcb0 68 65 20 64 62 20 70 61 74 68 20 74 6f 20 61 20  he db path to a 
edcc0 75 6e 69 71 75 65 20 63 61 63 68 65 20 6e 61 6d  unique cache nam
edcd0 65 20 2a 2f 0a 20 20 64 62 4c 65 6e 20 3d 20 28  e */.  dbLen = (
edce0 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74  int)strlen(dbPat
edcf0 68 29 3b 0a 20 20 66 6f 72 28 20 69 3d 30 3b 20  h);.  for( i=0; 
edd00 69 3c 64 62 4c 65 6e 20 26 26 20 28 69 2b 6c 65  i<dbLen && (i+le
edd10 6e 2b 37 29 3c 6d 61 78 4c 65 6e 3b 20 69 2b 2b  n+7)<maxLen; i++
edd20 29 7b 0a 20 20 20 20 63 68 61 72 20 63 20 3d 20  ){.    char c = 
edd30 64 62 50 61 74 68 5b 69 5d 3b 0a 20 20 20 20 6c  dbPath[i];.    l
edd40 50 61 74 68 5b 69 2b 6c 65 6e 5d 20 3d 20 28 63  Path[i+len] = (c
edd50 3d 3d 27 2f 27 29 3f 27 5f 27 3a 63 3b 0a 20 20  =='/')?'_':c;.  
edd60 7d 0a 20 20 6c 50 61 74 68 5b 69 2b 6c 65 6e 5d  }.  lPath[i+len]
edd70 3d 27 5c 30 27 3b 0a 20 20 73 74 72 6c 63 61 74  ='\0';.  strlcat
edd80 28 6c 50 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22  (lPath, ":auto:"
edd90 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 72 65 74  , maxLen);.  ret
edda0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
eddb0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
eddc0 20 6e 65 77 20 56 46 53 20 66 69 6c 65 20 64 65   new VFS file de
eddd0 73 63 72 69 70 74 6f 72 20 28 73 74 6f 72 65 64  scriptor (stored
edde0 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69   in memory obtai
eddf0 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69  ned from.** sqli
ede00 74 65 33 5f 6d 61 6c 6c 6f 63 29 20 61 6e 64 20  te3_malloc) and 
ede10 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 6e 61  open the file na
ede20 6d 65 64 20 22 70 61 74 68 22 20 69 6e 20 74 68  med "path" in th
ede30 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
ede40 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c  r..**.** The cal
ede50 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ler is responsib
ede60 6c 65 20 6e 6f 74 20 6f 6e 6c 79 20 66 6f 72 20  le not only for 
ede70 63 6c 6f 73 69 6e 67 20 74 68 65 20 66 69 6c 65  closing the file
ede80 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 62   descriptor.** b
ede90 75 74 20 61 6c 73 6f 20 66 6f 72 20 66 72 65 65  ut also for free
edea0 69 6e 67 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ing the memory a
edeb0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
edec0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
eded0 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  or..*/.static in
edee0 74 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69  t proxyCreateUni
edef0 78 46 69 6c 65 28 63 6f 6e 73 74 20 63 68 61 72  xFile(const char
edf00 20 2a 70 61 74 68 2c 20 75 6e 69 78 46 69 6c 65   *path, unixFile
edf10 20 2a 2a 70 70 46 69 6c 65 29 20 7b 0a 20 20 75   **ppFile) {.  u
edf20 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 3b 0a 20  nixFile *pNew;. 
edf30 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 53 51 4c   int flags = SQL
edf40 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
edf50 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45  |SQLITE_OPEN_CRE
edf60 41 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ATE|SQLITE_OPEN_
edf70 52 45 41 44 57 52 49 54 45 3b 0a 20 20 69 6e 74  READWRITE;.  int
edf80 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
edf90 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 64  .  sqlite3_vfs d
edfa0 75 6d 6d 79 56 66 73 3b 0a 0a 20 20 70 4e 65 77  ummyVfs;..  pNew
edfb0 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 73   = (unixFile *)s
edfc0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
edfd0 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b  zeof(unixFile));
edfe0 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a  .  if( !pNew ){.
edff0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ee000 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d  E_NOMEM;.  }.  m
ee010 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c 20 73  emset(pNew, 0, s
ee020 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29  izeof(unixFile))
ee030 3b 0a 0a 20 20 2f 2a 20 43 61 6c 6c 20 75 6e 69  ;..  /* Call uni
ee040 78 4f 70 65 6e 28 29 20 74 6f 20 6f 70 65 6e 20  xOpen() to open 
ee050 74 68 65 20 70 72 6f 78 79 20 66 69 6c 65 2e 20  the proxy file. 
ee060 54 68 65 20 66 6c 61 67 73 20 70 61 73 73 65 64  The flags passed
ee070 20 74 6f 20 75 6e 69 78 4f 70 65 6e 28 29 0a 20   to unixOpen(). 
ee080 20 2a 2a 20 73 75 67 67 65 73 74 20 74 68 61 74   ** suggest that
ee090 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 20   the file being 
ee0a0 6f 70 65 6e 65 64 20 69 73 20 61 20 22 6d 61 69  opened is a "mai
ee0b0 6e 20 64 61 74 61 62 61 73 65 22 2e 20 54 68 69  n database". Thi
ee0c0 73 20 69 73 0a 20 20 2a 2a 20 6e 65 63 65 73 73  s is.  ** necess
ee0d0 61 72 79 20 61 73 20 6f 74 68 65 72 20 66 69 6c  ary as other fil
ee0e0 65 20 74 79 70 65 73 20 64 6f 20 6e 6f 74 20 6e  e types do not n
ee0f0 65 63 65 73 73 61 72 69 6c 79 20 73 75 70 70 6f  ecessarily suppo
ee100 72 74 20 6c 6f 63 6b 69 6e 67 2e 20 49 74 0a 20  rt locking. It. 
ee110 20 2a 2a 20 69 73 20 62 65 74 74 65 72 20 74 6f   ** is better to
ee120 20 75 73 65 20 75 6e 69 78 4f 70 65 6e 28 29 20   use unixOpen() 
ee130 69 6e 73 74 65 61 64 20 6f 66 20 6f 70 65 6e 69  instead of openi
ee140 6e 67 20 74 68 65 20 66 69 6c 65 20 64 69 72 65  ng the file dire
ee150 63 74 6c 79 20 77 69 74 68 0a 20 20 2a 2a 20 6f  ctly with.  ** o
ee160 70 65 6e 28 29 2c 20 61 73 20 75 6e 69 78 4f 70  pen(), as unixOp
ee170 65 6e 28 29 20 73 65 74 73 20 75 70 20 74 68 65  en() sets up the
ee180 20 76 61 72 69 6f 75 73 20 6d 65 63 68 61 6e 69   various mechani
ee190 73 6d 73 20 72 65 71 75 69 72 65 64 20 74 6f 0a  sms required to.
ee1a0 20 20 2a 2a 20 6d 61 6b 65 20 73 75 72 65 20 61    ** make sure a
ee1b0 20 63 61 6c 6c 20 74 6f 20 63 6c 6f 73 65 28 29   call to close()
ee1c0 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20   does not cause 
ee1d0 74 68 65 20 73 79 73 74 65 6d 20 74 6f 20 64 69  the system to di
ee1e0 73 63 61 72 64 0a 20 20 2a 2a 20 50 4f 53 49 58  scard.  ** POSIX
ee1f0 20 6c 6f 63 6b 73 20 70 72 65 6d 61 74 75 72 65   locks premature
ee200 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 74  ly..  **.  ** It
ee210 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68   is important th
ee220 61 74 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 6d  at the xOpen mem
ee230 62 65 72 20 6f 66 20 74 68 65 20 56 46 53 20 6f  ber of the VFS o
ee240 62 6a 65 63 74 20 70 61 73 73 65 64 20 74 6f 20  bject passed to 
ee250 0a 20 20 2a 2a 20 75 6e 69 78 4f 70 65 6e 28 29  .  ** unixOpen()
ee260 20 69 73 20 4e 55 4c 4c 2e 20 54 68 69 73 20 74   is NULL. This t
ee270 65 6c 6c 73 20 75 6e 69 78 4f 70 65 6e 28 29 20  ells unixOpen() 
ee280 6d 61 79 20 74 72 79 20 74 6f 20 6f 70 65 6e 20  may try to open 
ee290 61 20 70 72 6f 78 79 2d 66 69 6c 65 20 0a 20 20  a proxy-file .  
ee2a0 2a 2a 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79  ** for the proxy
ee2b0 2d 66 69 6c 65 20 28 63 72 65 61 74 69 6e 67 20  -file (creating 
ee2c0 61 20 70 6f 74 65 6e 74 69 61 6c 20 69 6e 66 69  a potential infi
ee2d0 6e 69 74 65 20 6c 6f 6f 70 29 2e 0a 20 20 2a 2f  nite loop)..  */
ee2e0 0a 20 20 64 75 6d 6d 79 56 66 73 2e 70 41 70 70  .  dummyVfs.pApp
ee2f0 44 61 74 61 20 3d 20 28 76 6f 69 64 2a 29 26 61  Data = (void*)&a
ee300 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b  utolockIoFinder;
ee310 0a 20 20 64 75 6d 6d 79 56 66 73 2e 78 4f 70 65  .  dummyVfs.xOpe
ee320 6e 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 75 6e  n = 0;.  rc = un
ee330 69 78 4f 70 65 6e 28 26 64 75 6d 6d 79 56 66 73  ixOpen(&dummyVfs
ee340 2c 20 70 61 74 68 2c 20 28 73 71 6c 69 74 65 33  , path, (sqlite3
ee350 5f 66 69 6c 65 20 2a 29 70 4e 65 77 2c 20 66 6c  _file *)pNew, fl
ee360 61 67 73 2c 20 26 66 6c 61 67 73 29 3b 0a 20 20  ags, &flags);.  
ee370 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
ee380 4b 20 26 26 20 28 66 6c 61 67 73 26 53 51 4c 49  K && (flags&SQLI
ee390 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
ee3a0 29 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 70  ) ){.    pNew->p
ee3b0 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28  Method->xClose((
ee3c0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 70  sqlite3_file *)p
ee3d0 4e 65 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  New);.    rc = S
ee3e0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
ee3f0 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d 53    }..  if( rc!=S
ee400 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
ee410 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65  sqlite3_free(pNe
ee420 77 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 30  w);.    pNew = 0
ee430 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 46 69 6c 65  ;.  }..  *ppFile
ee440 20 3d 20 70 4e 65 77 3b 0a 20 20 72 65 74 75 72   = pNew;.  retur
ee450 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 74 61 6b 65  n rc;.}../* take
ee460 73 20 74 68 65 20 63 6f 6e 63 68 20 62 79 20 74  s the conch by t
ee470 61 6b 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c  aking a shared l
ee480 6f 63 6b 20 61 6e 64 20 72 65 61 64 20 74 68 65  ock and read the
ee490 20 63 6f 6e 74 65 6e 74 73 20 63 6f 6e 63 68 2c   contents conch,
ee4a0 20 69 66 20 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68   if .** lockPath
ee4b0 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68   is non-NULL, th
ee4c0 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20 6c 6f  e host ID and lo
ee4d0 63 6b 20 66 69 6c 65 20 70 61 74 68 20 6d 75 73  ck file path mus
ee4e0 74 20 6d 61 74 63 68 2e 20 20 41 20 4e 55 4c 4c  t match.  A NULL
ee4f0 20 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 6d 65   .** lockPath me
ee500 61 6e 73 20 74 68 61 74 20 74 68 65 20 6c 6f 63  ans that the loc
ee510 6b 50 61 74 68 20 69 6e 20 74 68 65 20 63 6f 6e  kPath in the con
ee520 63 68 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20  ch file will be 
ee530 75 73 65 64 20 69 66 20 74 68 65 20 0a 2a 2a 20  used if the .** 
ee540 68 6f 73 74 20 49 44 73 20 6d 61 74 63 68 2c 20  host IDs match, 
ee550 6f 72 20 61 20 6e 65 77 20 6c 6f 63 6b 20 70 61  or a new lock pa
ee560 74 68 20 77 69 6c 6c 20 62 65 20 67 65 6e 65 72  th will be gener
ee570 61 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  ated automatical
ee580 6c 79 20 0a 2a 2a 20 61 6e 64 20 77 72 69 74 74  ly .** and writt
ee590 65 6e 20 74 6f 20 74 68 65 20 63 6f 6e 63 68 20  en to the conch 
ee5a0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
ee5b0 69 6e 74 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e  int proxyTakeCon
ee5c0 63 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  ch(unixFile *pFi
ee5d0 6c 65 29 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b  le){.  proxyLock
ee5e0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78  ingContext *pCtx
ee5f0 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67   = (proxyLocking
ee600 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d  Context *)pFile-
ee610 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
ee620 20 0a 20 20 0a 20 20 69 66 28 20 70 43 74 78 2d   .  .  if( pCtx-
ee630 3e 63 6f 6e 63 68 48 65 6c 64 3e 30 20 29 7b 0a  >conchHeld>0 ){.
ee640 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ee650 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
ee660 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e     unixFile *con
ee670 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63  chFile = pCtx->c
ee680 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 63 68  onchFile;.    ch
ee690 61 72 20 74 65 73 74 56 61 6c 75 65 5b 43 4f 4e  ar testValue[CON
ee6a0 43 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72  CHLEN];.    char
ee6b0 20 63 6f 6e 63 68 56 61 6c 75 65 5b 43 4f 4e 43   conchValue[CONC
ee6c0 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20  HLEN];.    char 
ee6d0 6c 6f 63 6b 50 61 74 68 5b 4d 41 58 50 41 54 48  lockPath[MAXPATH
ee6e0 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 2a  LEN];.    char *
ee6f0 74 4c 6f 63 6b 50 61 74 68 20 3d 20 4e 55 4c 4c  tLockPath = NULL
ee700 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53  ;.    int rc = S
ee710 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e  QLITE_OK;.    in
ee720 74 20 72 65 61 64 52 63 20 3d 20 53 51 4c 49 54  t readRc = SQLIT
ee730 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 73 79  E_OK;.    int sy
ee740 6e 63 50 65 72 6d 73 20 3d 20 30 3b 0a 0a 20 20  ncPerms = 0;..  
ee750 20 20 4f 53 54 52 41 43 45 34 28 22 54 41 4b 45    OSTRACE4("TAKE
ee760 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73  CONCH  %d for %s
ee770 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63   pid=%d\n", conc
ee780 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  hFile->h,.      
ee790 20 20 20 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f         (pCtx->lo
ee7a0 63 6b 50 72 6f 78 79 50 61 74 68 20 3f 20 70 43  ckProxyPath ? pC
ee7b0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
ee7c0 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67  h : ":auto:"), g
ee7d0 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 20 20 72  etpid());..    r
ee7e0 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70  c = conchFile->p
ee7f0 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73  Method->xLock((s
ee800 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e  qlite3_file*)con
ee810 63 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 4c  chFile, SHARED_L
ee820 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63  OCK);.    if( rc
ee830 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
ee840 20 20 20 20 20 69 6e 74 20 70 45 72 72 6f 72 20       int pError 
ee850 3d 20 30 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65  = 0;.      memse
ee860 74 28 74 65 73 74 56 61 6c 75 65 2c 20 30 2c 20  t(testValue, 0, 
ee870 43 4f 4e 43 48 4c 45 4e 29 3b 20 2f 2a 20 63 6f  CONCHLEN); /* co
ee880 6e 63 68 20 69 73 20 66 69 78 65 64 20 73 69 7a  nch is fixed siz
ee890 65 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  e */.      rc = 
ee8a0 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 28 74  proxyGetHostID(t
ee8b0 65 73 74 56 61 6c 75 65 2c 20 26 70 45 72 72 6f  estValue, &pErro
ee8c0 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 72  r);.      if( (r
ee8d0 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f  c&0xff)==SQLITE_
ee8e0 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 20 20  IOERR ){.       
ee8f0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
ee900 6f 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 20 20  o = pError;.    
ee910 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43    }.      if( pC
ee920 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
ee930 68 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72  h ){.        str
ee940 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b  lcpy(&testValue[
ee950 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 70 43 74 78  HOSTIDLEN], pCtx
ee960 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c  ->lockProxyPath,
ee970 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20   MAXPATHLEN);.  
ee980 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
ee990 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
ee9a0 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
ee9b0 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20  end_takeconch;. 
ee9c0 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 72 65     }.    .    re
ee9d0 61 64 52 63 20 3d 20 75 6e 69 78 52 65 61 64 28  adRc = unixRead(
ee9e0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
ee9f0 63 6f 6e 63 68 46 69 6c 65 2c 20 63 6f 6e 63 68  conchFile, conch
eea00 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c  Value, CONCHLEN,
eea10 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 61   0);.    if( rea
eea20 64 52 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52  dRc!=SQLITE_IOER
eea30 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a  R_SHORT_READ ){.
eea40 20 20 20 20 20 20 69 66 28 20 72 65 61 64 52 63        if( readRc
eea50 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
eea60 20 20 20 20 20 20 20 69 66 28 20 28 72 63 26 30         if( (rc&0
eea70 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45  xff)==SQLITE_IOE
eea80 52 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  RR ){.          
eea90 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
eeaa0 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 6c 61   = conchFile->la
eeab0 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  stErrno;.       
eeac0 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20   }.        rc = 
eead0 72 65 61 64 52 63 3b 0a 20 20 20 20 20 20 20 20  readRc;.        
eeae0 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e  goto end_takecon
eeaf0 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ch;.      }.    
eeb00 20 20 2f 2a 20 69 66 20 74 68 65 20 63 6f 6e 63    /* if the conc
eeb10 68 20 68 61 73 20 64 61 74 61 20 63 6f 6d 70 61  h has data compa
eeb20 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  re the contents 
eeb30 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 70 43  */.      if( !pC
eeb40 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
eeb50 68 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  h ){.        /* 
eeb60 66 6f 72 20 61 75 74 6f 2d 6e 61 6d 65 64 20 6c  for auto-named l
eeb70 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 2c 20  ocal lock file, 
eeb80 6a 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 68  just check the h
eeb90 6f 73 74 20 49 44 20 61 6e 64 20 77 65 27 6c 6c  ost ID and we'll
eeba0 0a 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65  .         ** use
eebb0 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20   the local lock 
eebc0 66 69 6c 65 20 70 61 74 68 20 74 68 61 74 27 73  file path that's
eebd0 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 72   already in ther
eebe0 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  e */.        if(
eebf0 20 21 6d 65 6d 63 6d 70 28 74 65 73 74 56 61 6c   !memcmp(testVal
eec00 75 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20  ue, conchValue, 
eec10 48 4f 53 54 49 44 4c 45 4e 29 20 29 7b 0a 20 20  HOSTIDLEN) ){.  
eec20 20 20 20 20 20 20 20 20 74 4c 6f 63 6b 50 61 74          tLockPat
eec30 68 20 3d 20 28 63 68 61 72 20 2a 29 26 63 6f 6e  h = (char *)&con
eec40 63 68 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45  chValue[HOSTIDLE
eec50 4e 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  N];.          go
eec60 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68  to end_takeconch
eec70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
eec80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
eec90 20 2f 2a 20 77 65 27 76 65 20 67 6f 74 20 74 68   /* we've got th
eeca0 65 20 63 6f 6e 63 68 20 69 66 20 63 6f 6e 63 68  e conch if conch
eecb0 56 61 6c 75 65 20 6d 61 74 63 68 65 73 20 6f 75  Value matches ou
eecc0 72 20 70 61 74 68 20 61 6e 64 20 68 6f 73 74 20  r path and host 
eecd0 49 44 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  ID */.        if
eece0 28 20 21 6d 65 6d 63 6d 70 28 74 65 73 74 56 61  ( !memcmp(testVa
eecf0 6c 75 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c  lue, conchValue,
eed00 20 43 4f 4e 43 48 4c 45 4e 29 20 29 7b 0a 20 20   CONCHLEN) ){.  
eed10 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
eed20 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20  _takeconch;.    
eed30 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
eed40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
eed50 2a 20 61 20 73 68 6f 72 74 20 72 65 61 64 20 6d  * a short read m
eed60 65 61 6e 73 20 77 65 27 72 65 20 22 63 72 65 61  eans we're "crea
eed70 74 69 6e 67 22 20 74 68 65 20 63 6f 6e 63 68 20  ting" the conch 
eed80 28 65 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20  (even though it 
eed90 63 6f 75 6c 64 20 0a 20 20 20 20 20 20 2a 2a 20  could .      ** 
eeda0 68 61 76 65 20 62 65 65 6e 20 75 73 65 72 2d 69  have been user-i
eedb0 6e 74 65 72 76 65 6e 74 69 6f 6e 29 2c 20 69 66  ntervention), if
eedc0 20 77 65 20 61 63 71 75 69 72 65 20 74 68 65 20   we acquire the 
eedd0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 0a  exclusive lock,.
eede0 20 20 20 20 20 20 2a 2a 20 77 65 27 6c 6c 20 74        ** we'll t
eedf0 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20  ry to match the 
eee00 63 75 72 72 65 6e 74 20 6f 6e 2d 64 69 73 6b 20  current on-disk 
eee10 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 74  permissions of t
eee20 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  he database.    
eee30 20 20 2a 2f 0a 20 20 20 20 20 20 73 79 6e 63 50    */.      syncP
eee40 65 72 6d 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  erms = 1;.    }.
eee50 20 20 20 20 0a 20 20 20 20 2f 2a 20 65 69 74 68      .    /* eith
eee60 65 72 20 63 6f 6e 63 68 20 77 61 73 20 65 6d 74  er conch was emt
eee70 70 79 20 6f 72 20 64 69 64 6e 27 74 20 6d 61 74  py or didn't mat
eee80 63 68 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70  ch */.    if( !p
eee90 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
eeea0 74 68 20 29 7b 0a 20 20 20 20 20 20 70 72 6f 78  th ){.      prox
eeeb0 79 47 65 74 4c 6f 63 6b 50 61 74 68 28 70 43 74  yGetLockPath(pCt
eeec0 78 2d 3e 64 62 50 61 74 68 2c 20 6c 6f 63 6b 50  x->dbPath, lockP
eeed0 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29  ath, MAXPATHLEN)
eeee0 3b 0a 20 20 20 20 20 20 74 4c 6f 63 6b 50 61 74  ;.      tLockPat
eeef0 68 20 3d 20 6c 6f 63 6b 50 61 74 68 3b 0a 20 20  h = lockPath;.  
eef00 20 20 20 20 73 74 72 6c 63 70 79 28 26 74 65 73      strlcpy(&tes
eef10 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e  tValue[HOSTIDLEN
eef20 5d 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58  ], lockPath, MAX
eef30 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 7d 0a  PATHLEN);.    }.
eef40 20 20 20 20 0a 20 20 20 20 2f 2a 20 75 70 64 61      .    /* upda
eef50 74 65 20 63 6f 6e 63 68 20 77 69 74 68 20 68 6f  te conch with ho
eef60 73 74 20 61 6e 64 20 70 61 74 68 20 28 74 68 69  st and path (thi
eef70 73 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 20 6f  s will fail if o
eef80 74 68 65 72 20 70 72 6f 63 65 73 73 0a 20 20 20  ther process.   
eef90 20 20 2a 2a 20 68 61 73 20 61 20 73 68 61 72 65    ** has a share
eefa0 64 20 6c 6f 63 6b 20 61 6c 72 65 61 64 79 29 20  d lock already) 
eefb0 2a 2f 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e 63  */.    rc = conc
eefc0 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e  hFile->pMethod->
eefd0 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66  xLock((sqlite3_f
eefe0 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20  ile*)conchFile, 
eeff0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b  EXCLUSIVE_LOCK);
ef000 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
ef010 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
ef020 72 63 20 3d 20 75 6e 69 78 57 72 69 74 65 28 28  rc = unixWrite((
ef030 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 63  sqlite3_file *)c
ef040 6f 6e 63 68 46 69 6c 65 2c 20 74 65 73 74 56 61  onchFile, testVa
ef050 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30  lue, CONCHLEN, 0
ef060 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
ef070 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 79  =SQLITE_OK && sy
ef080 6e 63 50 65 72 6d 73 20 29 7b 0a 20 20 20 20 20  ncPerms ){.     
ef090 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62     struct stat b
ef0a0 75 66 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  uf;.        int 
ef0b0 65 72 72 20 3d 20 66 73 74 61 74 28 70 46 69 6c  err = fstat(pFil
ef0c0 65 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 20  e->h, &buf);.   
ef0d0 20 20 20 20 20 69 66 28 20 65 72 72 3d 3d 30 20       if( err==0 
ef0e0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
ef0f0 74 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65  try to match the
ef100 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 70   database file p
ef110 65 72 6d 69 73 73 69 6f 6e 73 2c 20 69 67 6e 6f  ermissions, igno
ef120 72 65 20 66 61 69 6c 75 72 65 20 2a 2f 0a 23 69  re failure */.#i
ef130 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f  fndef SQLITE_PRO
ef140 58 59 5f 44 45 42 55 47 0a 20 20 20 20 20 20 20  XY_DEBUG.       
ef150 20 20 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46     fchmod(conchF
ef160 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d  ile->h, buf.st_m
ef170 6f 64 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20  ode);.#else.    
ef180 20 20 20 20 20 20 69 66 28 20 66 63 68 6d 6f 64        if( fchmod
ef190 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 62  (conchFile->h, b
ef1a0 75 66 2e 73 74 5f 6d 6f 64 65 29 21 3d 30 20 29  uf.st_mode)!=0 )
ef1b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
ef1c0 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a  t code = errno;.
ef1d0 20 20 20 20 20 20 20 20 20 20 20 20 66 70 72 69              fpri
ef1e0 6e 74 66 28 73 74 64 65 72 72 2c 20 22 66 63 68  ntf(stderr, "fch
ef1f0 6d 6f 64 20 25 6f 20 46 41 49 4c 45 44 20 77 69  mod %o FAILED wi
ef200 74 68 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20  th %d %s\n",.   
ef210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef220 20 20 20 20 20 20 20 20 20 20 62 75 66 2e 73 74            buf.st
ef230 5f 6d 6f 64 65 2c 20 63 6f 64 65 2c 20 73 74 72  _mode, code, str
ef240 65 72 72 6f 72 28 63 6f 64 65 29 29 3b 0a 20 20  error(code));.  
ef250 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b          } else {
ef260 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 70 72  .            fpr
ef270 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 66 63  intf(stderr, "fc
ef280 68 6d 6f 64 20 25 6f 20 53 55 43 43 45 44 45 44  hmod %o SUCCEDED
ef290 5c 6e 22 2c 62 75 66 2e 73 74 5f 6d 6f 64 65 29  \n",buf.st_mode)
ef2a0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
ef2b0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
ef2c0 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20         int code 
ef2d0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
ef2e0 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72     fprintf(stder
ef2f0 72 2c 20 22 53 54 41 54 20 46 41 49 4c 45 44 5b  r, "STAT FAILED[
ef300 25 64 5d 20 77 69 74 68 20 25 64 20 25 73 5c 6e  %d] with %d %s\n
ef310 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  ", .            
ef320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72                er
ef330 72 2c 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f  r, code, strerro
ef340 72 28 63 6f 64 65 29 29 3b 0a 23 65 6e 64 69 66  r(code));.#endif
ef350 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
ef360 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 6f 6e   }.    }.    con
ef370 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d  chFile->pMethod-
ef380 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65  >xUnlock((sqlite
ef390 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c  3_file*)conchFil
ef3a0 65 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b  e, SHARED_LOCK);
ef3b0 0a 20 20 0a 65 6e 64 5f 74 61 6b 65 63 6f 6e 63  .  .end_takeconc
ef3c0 68 3a 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28  h:.    OSTRACE2(
ef3d0 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 43 4c 4f  "TRANSPROXY: CLO
ef3e0 53 45 20 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  SE  %d\n", pFile
ef3f0 2d 3e 68 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ->h);.    if( rc
ef400 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
ef410 46 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 20  File->openFlags 
ef420 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 46 69  ){.      if( pFi
ef430 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0a 23 69 66 64  le->h>=0 ){.#ifd
ef440 65 66 20 53 54 52 49 43 54 5f 43 4c 4f 53 45 5f  ef STRICT_CLOSE_
ef450 45 52 52 4f 52 0a 20 20 20 20 20 20 20 20 69 66  ERROR.        if
ef460 28 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68  ( close(pFile->h
ef470 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
ef480 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
ef490 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
ef4a0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
ef4b0 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20  _IOERR_CLOSE;.  
ef4c0 20 20 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20        }.#else.  
ef4d0 20 20 20 20 20 20 63 6c 6f 73 65 28 70 46 69 6c        close(pFil
ef4e0 65 2d 3e 68 29 3b 20 2f 2a 20 73 69 6c 65 6e 74  e->h); /* silent
ef4f0 6c 79 20 6c 65 61 6b 20 66 64 20 69 66 20 66 61  ly leak fd if fa
ef500 69 6c 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 20  il */.#endif.   
ef510 20 20 20 7d 0a 20 20 20 20 20 20 70 46 69 6c 65     }.      pFile
ef520 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  ->h = -1;.      
ef530 69 6e 74 20 66 64 20 3d 20 6f 70 65 6e 28 70 43  int fd = open(pC
ef540 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  tx->dbPath, pFil
ef550 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20  e->openFlags,.  
ef560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef570 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54    SQLITE_DEFAULT
ef580 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e  _FILE_PERMISSION
ef590 53 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43  S);.      OSTRAC
ef5a0 45 32 28 22 54 52 41 4e 53 50 52 4f 58 59 3a 20  E2("TRANSPROXY: 
ef5b0 4f 50 45 4e 20 20 25 64 5c 6e 22 2c 20 66 64 29  OPEN  %d\n", fd)
ef5c0 3b 0a 20 20 20 20 20 20 69 66 28 20 66 64 3e 3d  ;.      if( fd>=
ef5d0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  0 ){.        pFi
ef5e0 6c 65 2d 3e 68 20 3d 20 66 64 3b 0a 20 20 20 20  le->h = fd;.    
ef5f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
ef600 20 72 63 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f   rc=SQLITE_CANTO
ef610 50 45 4e 3b 20 2f 2a 20 53 51 4c 49 54 45 5f 42  PEN; /* SQLITE_B
ef620 55 53 59 3f 20 70 72 6f 78 79 54 61 6b 65 43 6f  USY? proxyTakeCo
ef630 6e 63 68 20 63 61 6c 6c 65 64 0a 20 20 20 20 20  nch called.     
ef640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef650 20 20 20 20 20 20 20 20 20 20 64 75 72 69 6e 67            during
ef660 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20   locking */.    
ef670 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
ef680 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
ef690 26 26 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  && !pCtx->lockPr
ef6a0 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 63 68 61  oxy ){.      cha
ef6b0 72 20 2a 70 61 74 68 20 3d 20 74 4c 6f 63 6b 50  r *path = tLockP
ef6c0 61 74 68 20 3f 20 74 4c 6f 63 6b 50 61 74 68 20  ath ? tLockPath 
ef6d0 3a 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  : pCtx->lockProx
ef6e0 79 50 61 74 68 3b 0a 20 20 20 20 20 20 2f 2a 20  yPath;.      /* 
ef6f0 41 43 53 3a 20 4e 65 65 64 20 74 6f 20 6d 61 6b  ACS: Need to mak
ef700 65 20 61 20 63 6f 70 79 20 6f 66 20 70 61 74 68  e a copy of path
ef710 20 73 6f 6d 65 74 69 6d 65 73 20 2a 2f 0a 20 20   sometimes */.  
ef720 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72      rc = proxyCr
ef730 65 61 74 65 55 6e 69 78 46 69 6c 65 28 70 61 74  eateUnixFile(pat
ef740 68 2c 20 26 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  h, &pCtx->lockPr
ef750 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  oxy);.    }.    
ef760 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
ef770 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 74 78 2d  K ){.      pCtx-
ef780 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 31 3b 0a  >conchHeld = 1;.
ef790 0a 20 20 20 20 20 20 69 66 28 20 74 4c 6f 63 6b  .      if( tLock
ef7a0 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20  Path ){.        
ef7b0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
ef7c0 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ath = sqlite3DbS
ef7d0 74 72 44 75 70 28 30 2c 20 74 4c 6f 63 6b 50 61  trDup(0, tLockPa
ef7e0 74 68 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  th);.        if(
ef7f0 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
ef800 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66  ->pMethod == &af
ef810 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  pIoMethods ){.  
ef820 20 20 20 20 20 20 20 20 28 28 61 66 70 4c 6f 63          ((afpLoc
ef830 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 43  kingContext *)pC
ef840 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 6c  tx->lockProxy->l
ef850 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e  ockingContext)->
ef860 64 62 50 61 74 68 20 3d 0a 20 20 20 20 20 20 20  dbPath =.       
ef870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
ef880 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
ef890 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  h;.        }.   
ef8a0 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20     }.    } else 
ef8b0 7b 0a 20 20 20 20 20 20 63 6f 6e 63 68 46 69 6c  {.      conchFil
ef8c0 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c  e->pMethod->xUnl
ef8d0 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock((sqlite3_fil
ef8e0 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f  e*)conchFile, NO
ef8f0 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20  _LOCK);.    }.  
ef900 20 20 4f 53 54 52 41 43 45 33 28 22 54 41 4b 45    OSTRACE3("TAKE
ef910 43 4f 4e 43 48 20 20 25 64 20 25 73 5c 6e 22 2c  CONCH  %d %s\n",
ef920 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 72   conchFile->h, r
ef930 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 22 6f 6b  c==SQLITE_OK?"ok
ef940 22 3a 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 20  ":"failed");.   
ef950 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
ef960 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c  }../*.** If pFil
ef970 65 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f  e holds a lock o
ef980 6e 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 2c 20  n a conch file, 
ef990 74 68 65 6e 20 72 65 6c 65 61 73 65 20 74 68 61  then release tha
ef9a0 74 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69  t lock..*/.stati
ef9b0 63 20 69 6e 74 20 70 72 6f 78 79 52 65 6c 65 61  c int proxyRelea
ef9c0 73 65 43 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65  seConch(unixFile
ef9d0 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20   *pFile){.  int 
ef9e0 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
ef9f0 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 72 6f          /* Subro
efa00 75 74 69 6e 65 20 72 65 74 75 72 6e 20 63 6f 64  utine return cod
efa10 65 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b  e */.  proxyLock
efa20 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78  ingContext *pCtx
efa30 3b 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e  ;  /* The lockin
efa40 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 74 68  g context for th
efa50 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 2a 2f 0a  e proxy lock */.
efa60 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63    unixFile *conc
efa70 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a  hFile;        /*
efa80 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e   Name of the con
efa90 63 68 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 70 43  ch file */..  pC
efaa0 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
efab0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
efac0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
efad0 74 3b 0a 20 20 63 6f 6e 63 68 46 69 6c 65 20 3d  t;.  conchFile =
efae0 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65   pCtx->conchFile
efaf0 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 52 45  ;.  OSTRACE4("RE
efb00 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64 20 66  LEASECONCH  %d f
efb10 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c  or %s pid=%d\n",
efb20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20   conchFile->h,. 
efb30 20 20 20 20 20 20 20 20 20 20 28 70 43 74 78 2d            (pCtx-
efb40 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3f  >lockProxyPath ?
efb50 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
efb60 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29  Path : ":auto:")
efb70 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 67 65  , .           ge
efb80 74 70 69 64 28 29 29 3b 0a 20 20 70 43 74 78 2d  tpid());.  pCtx-
efb90 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30 3b 0a  >conchHeld = 0;.
efba0 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65    rc = conchFile
efbb0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f  ->pMethod->xUnlo
efbc0 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck((sqlite3_file
efbd0 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f  *)conchFile, NO_
efbe0 4c 4f 43 4b 29 3b 0a 20 20 4f 53 54 52 41 43 45  LOCK);.  OSTRACE
efbf0 33 28 22 52 45 4c 45 41 53 45 43 4f 4e 43 48 20  3("RELEASECONCH 
efc00 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68   %d %s\n", conch
efc10 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
efc20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f      (rc==SQLITE_
efc30 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69  OK ? "ok" : "fai
efc40 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 75 72 6e  led"));.  return
efc50 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69   rc;.}../*.** Gi
efc60 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ven the name of 
efc70 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  a database file,
efc80 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e 61 6d   compute the nam
efc90 65 20 6f 66 20 69 74 73 20 63 6f 6e 63 68 20 66  e of its conch f
efca0 69 6c 65 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68  ile..** Store th
efcb0 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65  e conch filename
efcc0 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69   in memory obtai
efcd0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
efce0 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 4d 61  _malloc()..** Ma
efcf0 6b 65 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 70  ke *pConchPath p
efd00 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 77 20  oint to the new 
efd10 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 20 53 51  name.  Return SQ
efd20 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
efd30 73 73 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f  ss.** or SQLITE_
efd40 4e 4f 4d 45 4d 20 69 66 20 75 6e 61 62 6c 65 20  NOMEM if unable 
efd50 74 6f 20 6f 62 74 61 69 6e 20 6d 65 6d 6f 72 79  to obtain memory
efd60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
efd70 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  er is responsibl
efd80 65 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74  e for ensuring t
efd90 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 65  hat the allocate
efda0 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63  d memory.** spac
efdb0 65 20 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 20  e is eventually 
efdc0 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 43  freed..**.** *pC
efdd0 6f 6e 63 68 50 61 74 68 20 69 73 20 73 65 74 20  onchPath is set 
efde0 74 6f 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d  to NULL if a mem
efdf0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
efe00 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a  rror occurs..*/.
efe10 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
efe20 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68 6e  CreateConchPathn
efe30 61 6d 65 28 63 68 61 72 20 2a 64 62 50 61 74 68  ame(char *dbPath
efe40 2c 20 63 68 61 72 20 2a 2a 70 43 6f 6e 63 68 50  , char **pConchP
efe50 61 74 68 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20  ath){.  int i;  
efe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
efe70 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
efe80 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6c  unter */.  int l
efe90 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e  en = (int)strlen
efea0 28 64 62 50 61 74 68 29 3b 20 2f 2a 20 4c 65 6e  (dbPath); /* Len
efeb0 67 74 68 20 6f 66 20 64 61 74 61 62 61 73 65 20  gth of database 
efec0 66 69 6c 65 6e 61 6d 65 20 2d 20 64 62 50 61 74  filename - dbPat
efed0 68 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63 6f 6e  h */.  char *con
efee0 63 68 50 61 74 68 3b 20 20 20 20 20 20 20 20 20  chPath;         
efef0 20 20 20 20 20 2f 2a 20 62 75 66 66 65 72 20 69       /* buffer i
eff00 6e 20 77 68 69 63 68 20 74 6f 20 63 6f 6e 73 74  n which to const
eff10 72 75 63 74 20 63 6f 6e 63 68 20 6e 61 6d 65 20  ruct conch name 
eff20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  */..  /* Allocat
eff30 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20  e space for the 
eff40 63 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65 20 61  conch filename a
eff50 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  nd initialize th
eff60 65 20 6e 61 6d 65 20 74 6f 0a 20 20 2a 2a 20 74  e name to.  ** t
eff70 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f  he name of the o
eff80 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65  riginal database
eff90 20 66 69 6c 65 2e 20 2a 2f 20 20 0a 20 20 2a 70   file. */  .  *p
effa0 43 6f 6e 63 68 50 61 74 68 20 3d 20 63 6f 6e 63  ConchPath = conc
effb0 68 50 61 74 68 20 3d 20 28 63 68 61 72 20 2a 29  hPath = (char *)
effc0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6c  sqlite3_malloc(l
effd0 65 6e 20 2b 20 38 29 3b 0a 20 20 69 66 28 20 63  en + 8);.  if( c
effe0 6f 6e 63 68 50 61 74 68 3d 3d 30 20 29 7b 0a 20  onchPath==0 ){. 
efff0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f0000 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65  _NOMEM;.  }.  me
f0010 6d 63 70 79 28 63 6f 6e 63 68 50 61 74 68 2c 20  mcpy(conchPath, 
f0020 64 62 50 61 74 68 2c 20 6c 65 6e 2b 31 29 3b 0a  dbPath, len+1);.
f0030 20 20 0a 20 20 2f 2a 20 6e 6f 77 20 69 6e 73 65    .  /* now inse
f0040 72 74 20 61 20 22 2e 22 20 62 65 66 6f 72 65 20  rt a "." before 
f0050 74 68 65 20 6c 61 73 74 20 2f 20 63 68 61 72 61  the last / chara
f0060 63 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28 20 69  cter */.  for( i
f0070 3d 28 6c 65 6e 2d 31 29 3b 20 69 3e 3d 30 3b 20  =(len-1); i>=0; 
f0080 69 2d 2d 20 29 7b 0a 20 20 20 20 69 66 28 20 63  i-- ){.    if( c
f0090 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 3d 27 2f 27  onchPath[i]=='/'
f00a0 20 29 7b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20   ){.      i++;. 
f00b0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
f00c0 7d 0a 20 20 7d 0a 20 20 63 6f 6e 63 68 50 61 74  }.  }.  conchPat
f00d0 68 5b 69 5d 3d 27 2e 27 3b 0a 20 20 77 68 69 6c  h[i]='.';.  whil
f00e0 65 20 28 20 69 3c 6c 65 6e 20 29 7b 0a 20 20 20  e ( i<len ){.   
f00f0 20 63 6f 6e 63 68 50 61 74 68 5b 69 2b 31 5d 3d   conchPath[i+1]=
f0100 64 62 50 61 74 68 5b 69 5d 3b 0a 20 20 20 20 69  dbPath[i];.    i
f0110 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 70  ++;.  }..  /* ap
f0120 70 65 6e 64 20 74 68 65 20 22 2d 63 6f 6e 63 68  pend the "-conch
f0130 22 20 73 75 66 66 69 78 20 74 6f 20 74 68 65 20  " suffix to the 
f0140 66 69 6c 65 20 2a 2f 0a 20 20 6d 65 6d 63 70 79  file */.  memcpy
f0150 28 26 63 6f 6e 63 68 50 61 74 68 5b 69 2b 31 5d  (&conchPath[i+1]
f0160 2c 20 22 2d 63 6f 6e 63 68 22 2c 20 37 29 3b 0a  , "-conch", 7);.
f0170 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73    assert( (int)s
f0180 74 72 6c 65 6e 28 63 6f 6e 63 68 50 61 74 68 29  trlen(conchPath)
f0190 20 3d 3d 20 6c 65 6e 2b 37 20 29 3b 0a 0a 20 20   == len+7 );..  
f01a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f01b0 3b 0a 7d 0a 0a 0a 2f 2a 20 54 61 6b 65 73 20 61  ;.}.../* Takes a
f01c0 20 66 75 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65   fully configure
f01d0 64 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2d  d proxy locking-
f01e0 73 74 79 6c 65 20 75 6e 69 78 20 66 69 6c 65 20  style unix file 
f01f0 61 6e 64 20 73 77 69 74 63 68 65 73 0a 2a 2a 20  and switches.** 
f0200 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66  the local lock f
f0210 69 6c 65 20 70 61 74 68 20 0a 2a 2f 0a 73 74 61  ile path .*/.sta
f0220 74 69 63 20 69 6e 74 20 73 77 69 74 63 68 4c 6f  tic int switchLo
f0230 63 6b 50 72 6f 78 79 50 61 74 68 28 75 6e 69 78  ckProxyPath(unix
f0240 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e  File *pFile, con
f0250 73 74 20 63 68 61 72 20 2a 70 61 74 68 29 20 7b  st char *path) {
f0260 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43  .  proxyLockingC
f0270 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28  ontext *pCtx = (
f0280 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f0290 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ext*)pFile->lock
f02a0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 68  ingContext;.  ch
f02b0 61 72 20 2a 6f 6c 64 50 61 74 68 20 3d 20 70 43  ar *oldPath = pC
f02c0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
f02d0 68 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  h;.  int rc = SQ
f02e0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20  LITE_OK;..  if( 
f02f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21  pFile->locktype!
f0300 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  =NO_LOCK ){.    
f0310 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
f0320 53 59 3b 0a 20 20 7d 20 20 0a 0a 20 20 2f 2a 20  SY;.  }  ..  /* 
f0330 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 66  nothing to do if
f0340 20 74 68 65 20 70 61 74 68 20 69 73 20 4e 55 4c   the path is NUL
f0350 4c 2c 20 3a 61 75 74 6f 3a 20 6f 72 20 6d 61 74  L, :auto: or mat
f0360 63 68 65 73 20 74 68 65 20 65 78 69 73 74 69 6e  ches the existin
f0370 67 20 70 61 74 68 20 2a 2f 0a 20 20 69 66 28 20  g path */.  if( 
f0380 21 70 61 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d  !path || path[0]
f0390 3d 3d 27 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d  =='\0' || !strcm
f03a0 70 28 70 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22  p(path, ":auto:"
f03b0 29 20 7c 7c 0a 20 20 20 20 28 6f 6c 64 50 61 74  ) ||.    (oldPat
f03c0 68 20 26 26 20 21 73 74 72 6e 63 6d 70 28 6f 6c  h && !strncmp(ol
f03d0 64 50 61 74 68 2c 20 70 61 74 68 2c 20 4d 41 58  dPath, path, MAX
f03e0 50 41 54 48 4c 45 4e 29 29 20 29 7b 0a 20 20 20  PATHLEN)) ){.   
f03f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f0400 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
f0410 75 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72  unixFile *lockPr
f0420 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b  oxy = pCtx->lock
f0430 50 72 6f 78 79 3b 0a 20 20 20 20 70 43 74 78 2d  Proxy;.    pCtx-
f0440 3e 6c 6f 63 6b 50 72 6f 78 79 3d 4e 55 4c 4c 3b  >lockProxy=NULL;
f0450 0a 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68  .    pCtx->conch
f0460 48 65 6c 64 20 3d 20 30 3b 0a 20 20 20 20 69 66  Held = 0;.    if
f0470 28 20 6c 6f 63 6b 50 72 6f 78 79 21 3d 4e 55 4c  ( lockProxy!=NUL
f0480 4c 20 29 7b 0a 20 20 20 20 20 20 72 63 3d 6c 6f  L ){.      rc=lo
f0490 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64  ckProxy->pMethod
f04a0 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65  ->xClose((sqlite
f04b0 33 5f 66 69 6c 65 20 2a 29 6c 6f 63 6b 50 72 6f  3_file *)lockPro
f04c0 78 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  xy);.      if( r
f04d0 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
f04e0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
f04f0 65 28 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20  e(lockProxy);.  
f0500 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
f0510 66 72 65 65 28 6f 6c 64 50 61 74 68 29 3b 0a 20  free(oldPath);. 
f0520 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f     pCtx->lockPro
f0530 78 79 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33  xyPath = sqlite3
f0540 44 62 53 74 72 44 75 70 28 30 2c 20 70 61 74 68  DbStrDup(0, path
f0550 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 72 65 74 75  );.  }.  .  retu
f0560 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
f0570 70 46 69 6c 65 20 69 73 20 61 20 66 69 6c 65 20  pFile is a file 
f0580 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 6f 70  that has been op
f0590 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20  ened by a prior 
f05a0 78 4f 70 65 6e 20 63 61 6c 6c 2e 20 20 64 62 50  xOpen call.  dbP
f05b0 61 74 68 0a 2a 2a 20 69 73 20 61 20 73 74 72 69  ath.** is a stri
f05c0 6e 67 20 62 75 66 66 65 72 20 61 74 20 6c 65 61  ng buffer at lea
f05d0 73 74 20 4d 41 58 50 41 54 48 4c 45 4e 2b 31 20  st MAXPATHLEN+1 
f05e0 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 73 69  characters in si
f05f0 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ze..**.** This r
f0600 6f 75 74 69 6e 65 20 66 69 6e 64 20 74 68 65 20  outine find the 
f0610 66 69 6c 65 6e 61 6d 65 20 61 73 73 6f 63 69 61  filename associa
f0620 74 65 64 20 77 69 74 68 20 70 46 69 6c 65 20 61  ted with pFile a
f0630 6e 64 20 77 72 69 74 65 73 20 69 74 0a 2a 2a 20  nd writes it.** 
f0640 69 6e 74 20 64 62 50 61 74 68 2e 0a 2a 2f 0a 73  int dbPath..*/.s
f0650 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47  tatic int proxyG
f0660 65 74 44 62 50 61 74 68 46 6f 72 55 6e 69 78 46  etDbPathForUnixF
f0670 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46  ile(unixFile *pF
f0680 69 6c 65 2c 20 63 68 61 72 20 2a 64 62 50 61 74  ile, char *dbPat
f0690 68 29 7b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  h){.#if defined(
f06a0 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 69 66 28  __APPLE__).  if(
f06b0 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20   pFile->pMethod 
f06c0 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73  == &afpIoMethods
f06d0 20 29 7b 0a 20 20 20 20 2f 2a 20 61 66 70 20 73   ){.    /* afp s
f06e0 74 79 6c 65 20 6b 65 65 70 73 20 61 20 72 65 66  tyle keeps a ref
f06f0 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 64 62  erence to the db
f0700 20 70 61 74 68 20 69 6e 20 74 68 65 20 66 69 6c   path in the fil
f0710 65 50 61 74 68 20 66 69 65 6c 64 20 0a 20 20 20  ePath field .   
f0720 20 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 75 63   ** of the struc
f0730 74 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  t */.    assert(
f0740 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 28 63 68   (int)strlen((ch
f0750 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  ar*)pFile->locki
f0760 6e 67 43 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 50  ngContext)<=MAXP
f0770 41 54 48 4c 45 4e 20 29 3b 0a 20 20 20 20 73 74  ATHLEN );.    st
f0780 72 63 70 79 28 64 62 50 61 74 68 2c 20 28 28 61  rcpy(dbPath, ((a
f0790 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
f07a0 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
f07b0 67 43 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74  gContext)->dbPat
f07c0 68 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  h);.  }else.#end
f07d0 69 66 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  if.  if( pFile->
f07e0 70 4d 65 74 68 6f 64 20 3d 3d 20 26 64 6f 74 6c  pMethod == &dotl
f07f0 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a  ockIoMethods ){.
f0800 20 20 20 20 2f 2a 20 64 6f 74 20 6c 6f 63 6b 20      /* dot lock 
f0810 73 74 79 6c 65 20 75 73 65 73 20 74 68 65 20 6c  style uses the l
f0820 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 74  ocking context t
f0830 6f 20 73 74 6f 72 65 20 74 68 65 20 64 6f 74 20  o store the dot 
f0840 6c 6f 63 6b 0a 20 20 20 20 2a 2a 20 66 69 6c 65  lock.    ** file
f0850 20 70 61 74 68 20 2a 2f 0a 20 20 20 20 69 6e 74   path */.    int
f0860 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 28 28 63   len = strlen((c
f0870 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  har *)pFile->loc
f0880 6b 69 6e 67 43 6f 6e 74 65 78 74 29 20 2d 20 73  kingContext) - s
f0890 74 72 6c 65 6e 28 44 4f 54 4c 4f 43 4b 5f 53 55  trlen(DOTLOCK_SU
f08a0 46 46 49 58 29 3b 0a 20 20 20 20 6d 65 6d 63 70  FFIX);.    memcp
f08b0 79 28 64 62 50 61 74 68 2c 20 28 63 68 61 72 20  y(dbPath, (char 
f08c0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
f08d0 43 6f 6e 74 65 78 74 2c 20 6c 65 6e 20 2b 20 31  Context, len + 1
f08e0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
f08f0 2f 2a 20 61 6c 6c 20 6f 74 68 65 72 20 73 74 79  /* all other sty
f0900 6c 65 73 20 75 73 65 20 74 68 65 20 6c 6f 63 6b  les use the lock
f0910 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 73  ing context to s
f0920 74 6f 72 65 20 74 68 65 20 64 62 20 66 69 6c 65  tore the db file
f0930 20 70 61 74 68 20 2a 2f 0a 20 20 20 20 61 73 73   path */.    ass
f0940 65 72 74 28 20 73 74 72 6c 65 6e 28 28 63 68 61  ert( strlen((cha
f0950 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e  r*)pFile->lockin
f0960 67 43 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 50 41  gContext)<=MAXPA
f0970 54 48 4c 45 4e 20 29 3b 0a 20 20 20 20 73 74 72  THLEN );.    str
f0980 63 70 79 28 64 62 50 61 74 68 2c 20 28 63 68 61  cpy(dbPath, (cha
f0990 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  r *)pFile->locki
f09a0 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a  ngContext);.  }.
f09b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f09c0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 6b  OK;.}../*.** Tak
f09d0 65 73 20 61 6e 20 61 6c 72 65 61 64 79 20 66 69  es an already fi
f09e0 6c 6c 65 64 20 69 6e 20 75 6e 69 78 20 66 69 6c  lled in unix fil
f09f0 65 20 61 6e 64 20 61 6c 74 65 72 73 20 69 74 20  e and alters it 
f0a00 73 6f 20 61 6c 6c 20 66 69 6c 65 20 6c 6f 63 6b  so all file lock
f0a10 69 6e 67 20 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  ing .** will be 
f0a20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65  performed on the
f0a30 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 6c 6f 63   local proxy loc
f0a40 6b 20 66 69 6c 65 2e 20 20 54 68 65 20 66 6f 6c  k file.  The fol
f0a50 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 0a 2a 2a  lowing fields.**
f0a60 20 61 72 65 20 70 72 65 73 65 72 76 65 64 20 69   are preserved i
f0a70 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f  n the locking co
f0a80 6e 74 65 78 74 20 73 6f 20 74 68 61 74 20 74 68  ntext so that th
f0a90 65 79 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72  ey can be restor
f0aa0 65 64 20 61 6e 64 20 0a 2a 2a 20 74 68 65 20 75  ed and .** the u
f0ab0 6e 69 78 20 73 74 72 75 63 74 75 72 65 20 70 72  nix structure pr
f0ac0 6f 70 65 72 6c 79 20 63 6c 65 61 6e 65 64 20 75  operly cleaned u
f0ad0 70 20 61 74 20 63 6c 6f 73 65 20 74 69 6d 65 3a  p at close time:
f0ae0 0a 2a 2a 20 20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  .**  ->lockingCo
f0af0 6e 74 65 78 74 0a 2a 2a 20 20 2d 3e 70 4d 65 74  ntext.**  ->pMet
f0b00 68 6f 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  hod.*/.static in
f0b10 74 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d  t proxyTransform
f0b20 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c  UnixFile(unixFil
f0b30 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20  e *pFile, const 
f0b40 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 20  char *path) {.  
f0b50 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f0b60 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 63 68 61  ext *pCtx;.  cha
f0b70 72 20 64 62 50 61 74 68 5b 4d 41 58 50 41 54 48  r dbPath[MAXPATH
f0b80 4c 45 4e 2b 31 5d 3b 20 20 20 20 20 20 20 2f 2a  LEN+1];       /*
f0b90 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
f0ba0 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
f0bb0 63 68 61 72 20 2a 6c 6f 63 6b 50 61 74 68 3d 4e  char *lockPath=N
f0bc0 55 4c 4c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ULL;.  int rc = 
f0bd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 0a 20 20  SQLITE_OK;.  .  
f0be0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
f0bf0 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a  ype!=NO_LOCK ){.
f0c00 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
f0c10 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 72  E_BUSY;.  }.  pr
f0c20 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f 72 55  oxyGetDbPathForU
f0c30 6e 69 78 46 69 6c 65 28 70 46 69 6c 65 2c 20 64  nixFile(pFile, d
f0c40 62 50 61 74 68 29 3b 0a 20 20 69 66 28 20 21 70  bPath);.  if( !p
f0c50 61 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d  ath || path[0]==
f0c60 27 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d 70 28  '\0' || !strcmp(
f0c70 70 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20  path, ":auto:") 
f0c80 29 7b 0a 20 20 20 20 6c 6f 63 6b 50 61 74 68 3d  ){.    lockPath=
f0c90 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  NULL;.  }else{. 
f0ca0 20 20 20 6c 6f 63 6b 50 61 74 68 3d 28 63 68 61     lockPath=(cha
f0cb0 72 20 2a 29 70 61 74 68 3b 0a 20 20 7d 0a 20 20  r *)path;.  }.  
f0cc0 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 52 41  .  OSTRACE4("TRA
f0cd0 4e 53 50 52 4f 58 59 20 20 25 64 20 66 6f 72 20  NSPROXY  %d for 
f0ce0 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  %s pid=%d\n", pF
f0cf0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
f0d00 20 20 20 28 6c 6f 63 6b 50 61 74 68 20 3f 20 6c     (lockPath ? l
f0d10 6f 63 6b 50 61 74 68 20 3a 20 22 3a 61 75 74 6f  ockPath : ":auto
f0d20 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a  :"), getpid());.
f0d30 0a 20 20 70 43 74 78 20 3d 20 73 71 6c 69 74 65  .  pCtx = sqlite
f0d40 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  3_malloc( sizeof
f0d50 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 69 66 28  (*pCtx) );.  if(
f0d60 20 70 43 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20   pCtx==0 ){.    
f0d70 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
f0d80 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  MEM;.  }.  memse
f0d90 74 28 70 43 74 78 2c 20 30 2c 20 73 69 7a 65 6f  t(pCtx, 0, sizeo
f0da0 66 28 2a 70 43 74 78 29 29 3b 0a 0a 20 20 72 63  f(*pCtx));..  rc
f0db0 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 43 6f   = proxyCreateCo
f0dc0 6e 63 68 50 61 74 68 6e 61 6d 65 28 64 62 50 61  nchPathname(dbPa
f0dd0 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68  th, &pCtx->conch
f0de0 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 69 66 28  FilePath);.  if(
f0df0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
f0e00 7b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79  {.    rc = proxy
f0e10 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 70  CreateUnixFile(p
f0e20 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61  Ctx->conchFilePa
f0e30 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68  th, &pCtx->conch
f0e40 46 69 6c 65 29 3b 0a 20 20 7d 20 20 0a 20 20 69  File);.  }  .  i
f0e50 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
f0e60 20 26 26 20 6c 6f 63 6b 50 61 74 68 20 29 7b 0a   && lockPath ){.
f0e70 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72      pCtx->lockPr
f0e80 6f 78 79 50 61 74 68 20 3d 20 73 71 6c 69 74 65  oxyPath = sqlite
f0e90 33 44 62 53 74 72 44 75 70 28 30 2c 20 6c 6f 63  3DbStrDup(0, loc
f0ea0 6b 50 61 74 68 29 3b 0a 20 20 7d 0a 0a 20 20 69  kPath);.  }..  i
f0eb0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
f0ec0 20 29 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6d   ){.    /* all m
f0ed0 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74  emory is allocat
f0ee0 65 64 2c 20 70 72 6f 78 79 73 20 61 72 65 20 63  ed, proxys are c
f0ef0 72 65 61 74 65 64 20 61 6e 64 20 61 73 73 69 67  reated and assig
f0f00 6e 65 64 2c 20 0a 20 20 20 20 2a 2a 20 73 77 69  ned, .    ** swi
f0f10 74 63 68 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  tch the locking 
f0f20 63 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d 65 74  context and pMet
f0f30 68 6f 64 20 74 68 65 6e 20 72 65 74 75 72 6e 2e  hod then return.
f0f40 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43 74 78  .    */.    pCtx
f0f50 2d 3e 64 62 50 61 74 68 20 3d 20 73 71 6c 69 74  ->dbPath = sqlit
f0f60 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 64 62  e3DbStrDup(0, db
f0f70 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78 2d  Path);.    pCtx-
f0f80 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  >oldLockingConte
f0f90 78 74 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  xt = pFile->lock
f0fa0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  ingContext;.    
f0fb0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
f0fc0 6e 74 65 78 74 20 3d 20 70 43 74 78 3b 0a 20 20  ntext = pCtx;.  
f0fd0 20 20 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68    pCtx->pOldMeth
f0fe0 6f 64 20 3d 20 70 46 69 6c 65 2d 3e 70 4d 65 74  od = pFile->pMet
f0ff0 68 6f 64 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  hod;.    pFile->
f1000 70 4d 65 74 68 6f 64 20 3d 20 26 70 72 6f 78 79  pMethod = &proxy
f1010 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c  IoMethods;.  }el
f1020 73 65 7b 0a 20 20 20 20 69 66 28 20 70 43 74 78  se{.    if( pCtx
f1030 2d 3e 63 6f 6e 63 68 46 69 6c 65 20 29 7b 20 0a  ->conchFile ){ .
f1040 20 20 20 20 20 20 72 63 20 3d 20 70 43 74 78 2d        rc = pCtx-
f1050 3e 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74  >conchFile->pMet
f1060 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c  hod->xClose((sql
f1070 69 74 65 33 5f 66 69 6c 65 20 2a 29 70 43 74 78  ite3_file *)pCtx
f1080 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20  ->conchFile);.  
f1090 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
f10a0 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71  urn rc;.      sq
f10b0 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d  lite3_free(pCtx-
f10c0 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20  >conchFile);.   
f10d0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
f10e0 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46  ree(pCtx->conchF
f10f0 69 6c 65 50 61 74 68 29 3b 20 0a 20 20 20 20 73  ilePath); .    s
f1100 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78  qlite3_free(pCtx
f1110 29 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45  );.  }.  OSTRACE
f1120 33 28 22 54 52 41 4e 53 50 52 4f 58 59 20 20 25  3("TRANSPROXY  %
f1130 64 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d %s\n", pFile->
f1140 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 72  h,.           (r
f1150 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22  c==SQLITE_OK ? "
f1160 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29  ok" : "failed"))
f1170 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
f1180 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  .../*.** This ro
f1190 75 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 73 71  utine handles sq
f11a0 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
f11b0 6f 6c 28 29 20 63 61 6c 6c 73 20 74 68 61 74 20  ol() calls that 
f11c0 61 72 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20  are specific.** 
f11d0 74 6f 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  to proxy locking
f11e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f11f0 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c  proxyFileControl
f1200 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
f1210 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20  d, int op, void 
f1220 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68  *pArg){.  switch
f1230 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ( op ){.    case
f1240 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b   SQLITE_GET_LOCK
f1250 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20  PROXYFILE: {.   
f1260 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69     unixFile *pFi
f1270 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
f1280 69 64 3b 0a 20 20 20 20 20 20 69 66 28 20 70 46  id;.      if( pF
f1290 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20  ile->pMethod == 
f12a0 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 20  &proxyIoMethods 
f12b0 29 7b 0a 20 20 20 20 20 20 20 20 70 72 6f 78 79  ){.        proxy
f12c0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
f12d0 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63  pCtx = (proxyLoc
f12e0 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69  kingContext*)pFi
f12f0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f1300 78 74 3b 0a 20 20 20 20 20 20 20 20 70 72 6f 78  xt;.        prox
f1310 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65  yTakeConch(pFile
f1320 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
f1330 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
f1340 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  th ){.          
f1350 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29  *(const char **)
f1360 70 41 72 67 20 3d 20 70 43 74 78 2d 3e 6c 6f 63  pArg = pCtx->loc
f1370 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20  kProxyPath;.    
f1380 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
f1390 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 68 61       *(const cha
f13a0 72 20 2a 2a 29 70 41 72 67 20 3d 20 22 3a 61 75  r **)pArg = ":au
f13b0 74 6f 3a 20 28 6e 6f 74 20 68 65 6c 64 29 22 3b  to: (not held)";
f13c0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
f13d0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
f13e0 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a    *(const char *
f13f0 2a 29 70 41 72 67 20 3d 20 4e 55 4c 4c 3b 0a 20  *)pArg = NULL;. 
f1400 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
f1410 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
f1420 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
f1430 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_SET_LOCKPRO
f1440 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20  XYFILE: {.      
f1450 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
f1460 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
f1470 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20  .      int rc = 
f1480 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
f1490 20 69 6e 74 20 69 73 50 72 6f 78 79 53 74 79 6c   int isProxyStyl
f14a0 65 20 3d 20 28 70 46 69 6c 65 2d 3e 70 4d 65 74  e = (pFile->pMet
f14b0 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d  hod == &proxyIoM
f14c0 65 74 68 6f 64 73 29 3b 0a 20 20 20 20 20 20 69  ethods);.      i
f14d0 66 28 20 70 41 72 67 3d 3d 4e 55 4c 4c 20 7c 7c  f( pArg==NULL ||
f14e0 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70   (const char *)p
f14f0 41 72 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Arg==0 ){.      
f1500 20 20 69 66 28 20 69 73 50 72 6f 78 79 53 74 79    if( isProxySty
f1510 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  le ){.          
f1520 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78  /* turn off prox
f1530 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 6e 6f 74 20  y locking - not 
f1540 73 75 70 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20  supported */.   
f1550 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
f1560 54 45 5f 45 52 52 4f 52 20 2f 2a 53 51 4c 49 54  TE_ERROR /*SQLIT
f1570 45 5f 50 52 4f 54 4f 43 4f 4c 3f 20 53 51 4c 49  E_PROTOCOL? SQLI
f1580 54 45 5f 4d 49 53 55 53 45 3f 2a 2f 3b 0a 20 20  TE_MISUSE?*/;.  
f1590 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
f15a0 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f         /* turn o
f15b0 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  ff proxy locking
f15c0 20 2d 20 61 6c 72 65 61 64 79 20 6f 66 66 20 2d   - already off -
f15d0 20 4e 4f 4f 50 20 2a 2f 0a 20 20 20 20 20 20 20   NOOP */.       
f15e0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
f15f0 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  K;.        }.   
f1600 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
f1610 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72    const char *pr
f1620 6f 78 79 50 61 74 68 20 3d 20 28 63 6f 6e 73 74  oxyPath = (const
f1630 20 63 68 61 72 20 2a 29 70 41 72 67 3b 0a 20 20   char *)pArg;.  
f1640 20 20 20 20 20 20 69 66 28 20 69 73 50 72 6f 78        if( isProx
f1650 79 53 74 79 6c 65 20 29 7b 0a 20 20 20 20 20 20  yStyle ){.      
f1660 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67      proxyLocking
f1670 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20  Context *pCtx = 
f1680 0a 20 20 20 20 20 20 20 20 20 20 20 20 28 70 72  .            (pr
f1690 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
f16a0 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e  t*)pFile->lockin
f16b0 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20  gContext;.      
f16c0 20 20 20 20 69 66 28 20 21 73 74 72 63 6d 70 28      if( !strcmp(
f16d0 70 41 72 67 2c 20 22 3a 61 75 74 6f 3a 22 29 20  pArg, ":auto:") 
f16e0 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28  .           || (
f16f0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
f1700 61 74 68 20 26 26 0a 20 20 20 20 20 20 20 20 20  ath &&.         
f1710 20 20 20 20 20 20 21 73 74 72 6e 63 6d 70 28 70        !strncmp(p
f1720 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
f1730 74 68 2c 20 70 72 6f 78 79 50 61 74 68 2c 20 4d  th, proxyPath, M
f1740 41 58 50 41 54 48 4c 45 4e 29 29 0a 20 20 20 20  AXPATHLEN)).    
f1750 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
f1760 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
f1770 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  _OK;.          }
f1780 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
f1790 20 20 72 63 20 3d 20 73 77 69 74 63 68 4c 6f 63    rc = switchLoc
f17a0 6b 50 72 6f 78 79 50 61 74 68 28 70 46 69 6c 65  kProxyPath(pFile
f17b0 2c 20 70 72 6f 78 79 50 61 74 68 29 3b 0a 20 20  , proxyPath);.  
f17c0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
f17d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
f17e0 20 20 20 2f 2a 20 74 75 72 6e 20 6f 6e 20 70 72     /* turn on pr
f17f0 6f 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  oxy file locking
f1800 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63   */.          rc
f1810 20 3d 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72   = proxyTransfor
f1820 6d 55 6e 69 78 46 69 6c 65 28 70 46 69 6c 65 2c  mUnixFile(pFile,
f1830 20 70 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 20   proxyPath);.   
f1840 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
f1850 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
f1860 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
f1870 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t: {.      asser
f1880 74 28 20 30 20 29 3b 20 20 2f 2a 20 54 68 65 20  t( 0 );  /* The 
f1890 63 61 6c 6c 20 61 73 73 75 72 65 73 20 74 68 61  call assures tha
f18a0 74 20 6f 6e 6c 79 20 76 61 6c 69 64 20 6f 70 63  t only valid opc
f18b0 6f 64 65 73 20 61 72 65 20 73 65 6e 74 20 2a 2f  odes are sent */
f18c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e  .    }.  }.  /*N
f18d0 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 72 65  OTREACHED*/.  re
f18e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
f18f0 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 69 74 68  R;.}../*.** With
f1900 69 6e 20 74 68 69 73 20 64 69 76 69 73 69 6f 6e  in this division
f1910 20 28 74 68 65 20 70 72 6f 78 79 69 6e 67 20 6c   (the proxying l
f1920 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74  ocking implement
f1930 61 74 69 6f 6e 29 20 74 68 65 20 70 72 6f 63 65  ation) the proce
f1940 64 75 72 65 73 0a 2a 2a 20 61 62 6f 76 65 20 74  dures.** above t
f1950 68 69 73 20 70 6f 69 6e 74 20 61 72 65 20 61 6c  his point are al
f1960 6c 20 75 74 69 6c 69 74 69 65 73 2e 20 20 54 68  l utilities.  Th
f1970 65 20 6c 6f 63 6b 2d 72 65 6c 61 74 65 64 20 6d  e lock-related m
f1980 65 74 68 6f 64 73 20 6f 66 20 74 68 65 0a 2a 2a  ethods of the.**
f1990 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 73   proxy-locking s
f19a0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
f19b0 20 6f 62 6a 65 63 74 20 66 6f 6c 6c 6f 77 2e 0a   object follow..
f19c0 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  */.../*.** This 
f19d0 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
f19e0 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
f19f0 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20  ERVED lock held 
f1a00 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
f1a10 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73  .** file by this
f1a20 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72   or any other pr
f1a30 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61  ocess. If such a
f1a40 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73   lock is held, s
f1a50 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74  et *pResOut.** t
f1a60 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  o a non-zero val
f1a70 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52  ue otherwise *pR
f1a80 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20  esOut is set to 
f1a90 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72  zero.  The retur
f1aa0 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  n value.** is se
f1ab0 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75  t to SQLITE_OK u
f1ac0 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72  nless an I/O err
f1ad0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
f1ae0 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a   lock checking..
f1af0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  */.static int pr
f1b00 6f 78 79 43 68 65 63 6b 52 65 73 65 72 76 65 64  oxyCheckReserved
f1b10 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
f1b20 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73  e *id, int *pRes
f1b30 4f 75 74 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c  Out) {.  unixFil
f1b40 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
f1b50 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20  File*)id;.  int 
f1b60 72 63 20 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f  rc = proxyTakeCo
f1b70 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 69 66  nch(pFile);.  if
f1b80 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
f1b90 29 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b  ){.    proxyLock
f1ba0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78  ingContext *pCtx
f1bb0 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67   = (proxyLocking
f1bc0 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d  Context *)pFile-
f1bd0 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
f1be0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
f1bf0 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63  roxy = pCtx->loc
f1c00 6b 50 72 6f 78 79 3b 0a 20 20 20 20 72 65 74 75  kProxy;.    retu
f1c10 72 6e 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f  rn proxy->pMetho
f1c20 64 2d 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65  d->xCheckReserve
f1c30 64 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66  dLock((sqlite3_f
f1c40 69 6c 65 2a 29 70 72 6f 78 79 2c 20 70 52 65 73  ile*)proxy, pRes
f1c50 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Out);.  }.  retu
f1c60 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
f1c70 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69  Lock the file wi
f1c80 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63  th the lock spec
f1c90 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
f1ca0 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e  er locktype - on
f1cb0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c  e.** of the foll
f1cc0 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  owing:.**.**    
f1cd0 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b   (1) SHARED_LOCK
f1ce0 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45  .**     (2) RESE
f1cf0 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  RVED_LOCK.**    
f1d00 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   (3) PENDING_LOC
f1d10 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43  K.**     (4) EXC
f1d20 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a  LUSIVE_LOCK.**.*
f1d30 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e  * Sometimes when
f1d40 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20   requesting one 
f1d50 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69  lock state, addi
f1d60 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74  tional lock stat
f1d70 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74  es.** are insert
f1d80 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20  ed in between.  
f1d90 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68  The locking migh
f1da0 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66  t fail on one of
f1db0 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72   the later.** tr
f1dc0 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e  ansitions leavin
f1dd0 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65  g the lock state
f1de0 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
f1df0 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20  what it started 
f1e00 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f  but.** still sho
f1e10 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20  rt of its goal. 
f1e20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
f1e30 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61  hart shows the a
f1e40 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69  llowed.** transi
f1e50 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e  tions and the in
f1e60 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69  serted intermedi
f1e70 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a  ate states:.**.*
f1e80 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e  *    UNLOCKED ->
f1e90 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48   SHARED.**    SH
f1ea0 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44  ARED -> RESERVED
f1eb0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
f1ec0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
f1ed0 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45  CLUSIVE.**    RE
f1ee0 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49  SERVED -> (PENDI
f1ef0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
f1f00 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d  .**    PENDING -
f1f10 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a  > EXCLUSIVE.**.*
f1f20 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
f1f30 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73  ill only increas
f1f40 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74  e a lock.  Use t
f1f50 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  he sqlite3OsUnlo
f1f60 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  ck().** routine 
f1f70 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69  to lower a locki
f1f80 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61  ng level..*/.sta
f1f90 74 69 63 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63  tic int proxyLoc
f1fa0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
f1fb0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
f1fc0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ) {.  unixFile *
f1fd0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
f1fe0 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20  e*)id;.  int rc 
f1ff0 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68  = proxyTakeConch
f2000 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72  (pFile);.  if( r
f2010 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
f2020 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67      proxyLocking
f2030 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20  Context *pCtx = 
f2040 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e  (proxyLockingCon
f2050 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f  text *)pFile->lo
f2060 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
f2070 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78    unixFile *prox
f2080 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  y = pCtx->lockPr
f2090 6f 78 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 72  oxy;.    rc = pr
f20a0 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c  oxy->pMethod->xL
f20b0 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock((sqlite3_fil
f20c0 65 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79  e*)proxy, lockty
f20d0 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  pe);.    pFile->
f20e0 6c 6f 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79  locktype = proxy
f20f0 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a  ->locktype;.  }.
f2100 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
f2110 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  ./*.** Lower the
f2120 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
f2130 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
f2140 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74  r pFile to lockt
f2150 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a  ype.  locktype.*
f2160 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  * must be either
f2170 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52   NO_LOCK or SHAR
f2180 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49  ED_LOCK..**.** I
f2190 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  f the locking le
f21a0 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20  vel of the file 
f21b0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c  descriptor is al
f21c0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f  ready at or belo
f21d0 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74  w.** the request
f21e0 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  ed locking level
f21f0 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
f2200 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
f2210 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55 6e  atic int proxyUn
f2220 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
f2230 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
f2240 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c  ype) {.  unixFil
f2250 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
f2260 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20  File*)id;.  int 
f2270 72 63 20 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f  rc = proxyTakeCo
f2280 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 69 66  nch(pFile);.  if
f2290 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
f22a0 29 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b  ){.    proxyLock
f22b0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78  ingContext *pCtx
f22c0 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67   = (proxyLocking
f22d0 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d  Context *)pFile-
f22e0 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
f22f0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
f2300 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63  roxy = pCtx->loc
f2310 6b 50 72 6f 78 79 3b 0a 20 20 20 20 72 63 20 3d  kProxy;.    rc =
f2320 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d   proxy->pMethod-
f2330 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65  >xUnlock((sqlite
f2340 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c 20 6c  3_file*)proxy, l
f2350 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 70 46  ocktype);.    pF
f2360 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
f2370 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 79 70 65 3b  proxy->locktype;
f2380 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
f2390 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
f23a0 20 61 20 66 69 6c 65 20 74 68 61 74 20 75 73 65   a file that use
f23b0 73 20 70 72 6f 78 79 20 6c 6f 63 6b 73 2e 0a 2a  s proxy locks..*
f23c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
f23d0 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  xyClose(sqlite3_
f23e0 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66  file *id) {.  if
f23f0 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78  ( id ){.    unix
f2400 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
f2410 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20  nixFile*)id;.   
f2420 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
f2430 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72  text *pCtx = (pr
f2440 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
f2450 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  t *)pFile->locki
f2460 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75  ngContext;.    u
f2470 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f  nixFile *lockPro
f2480 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  xy = pCtx->lockP
f2490 72 6f 78 79 3b 0a 20 20 20 20 75 6e 69 78 46 69  roxy;.    unixFi
f24a0 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d 20  le *conchFile = 
f24b0 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b  pCtx->conchFile;
f24c0 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51  .    int rc = SQ
f24d0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20  LITE_OK;.    .  
f24e0 20 20 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79 20    if( lockProxy 
f24f0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f  ){.      rc = lo
f2500 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64  ckProxy->pMethod
f2510 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74  ->xUnlock((sqlit
f2520 65 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f  e3_file*)lockPro
f2530 78 79 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20  xy, NO_LOCK);.  
f2540 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
f2550 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72 63  urn rc;.      rc
f2560 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d   = lockProxy->pM
f2570 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73  ethod->xClose((s
f2580 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 6c 6f 63  qlite3_file*)loc
f2590 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 69  kProxy);.      i
f25a0 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
f25b0 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
f25c0 5f 66 72 65 65 28 6c 6f 63 6b 50 72 6f 78 79 29  _free(lockProxy)
f25d0 3b 0a 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f  ;.      pCtx->lo
f25e0 63 6b 50 72 6f 78 79 20 3d 20 30 3b 0a 20 20 20  ckProxy = 0;.   
f25f0 20 7d 0a 20 20 20 20 69 66 28 20 63 6f 6e 63 68   }.    if( conch
f2600 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66  File ){.      if
f2610 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c  ( pCtx->conchHel
f2620 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  d ){.        rc 
f2630 3d 20 70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f  = proxyReleaseCo
f2640 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  nch(pFile);.    
f2650 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
f2660 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
f2670 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68        rc = conch
f2680 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  File->pMethod->x
f2690 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66  Close((sqlite3_f
f26a0 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 29 3b  ile*)conchFile);
f26b0 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
f26c0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
f26d0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 6f   sqlite3_free(co
f26e0 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  nchFile);.    }.
f26f0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
f2700 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79  (pCtx->lockProxy
f2710 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74  Path);.    sqlit
f2720 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 6f  e3_free(pCtx->co
f2730 6e 63 68 46 69 6c 65 50 61 74 68 29 3b 0a 20 20  nchFilePath);.  
f2740 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
f2750 43 74 78 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20  Ctx->dbPath);.  
f2760 20 20 2f 2a 20 72 65 73 74 6f 72 65 20 74 68 65    /* restore the
f2770 20 6f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e   original lockin
f2780 67 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d  g context and pM
f2790 65 74 68 6f 64 20 74 68 65 6e 20 63 6c 6f 73 65  ethod then close
f27a0 20 69 74 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65   it */.    pFile
f27b0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
f27c0 20 3d 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b   = pCtx->oldLock
f27d0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  ingContext;.    
f27e0 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d  pFile->pMethod =
f27f0 20 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f   pCtx->pOldMetho
f2800 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  d;.    sqlite3_f
f2810 72 65 65 28 70 43 74 78 29 3b 0a 20 20 20 20 72  ree(pCtx);.    r
f2820 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 70 4d 65  eturn pFile->pMe
f2830 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 69 64 29  thod->xClose(id)
f2840 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
f2850 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 23  QLITE_OK;.}....#
f2860 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64  endif /* defined
f2870 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
f2880 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
f2890 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a  KING_STYLE */./*
f28a0 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f  .** The proxy lo
f28b0 63 6b 69 6e 67 20 73 74 79 6c 65 20 69 73 20 69  cking style is i
f28c0 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
f28d0 77 69 74 68 20 41 46 50 20 66 69 6c 65 73 79 73  with AFP filesys
f28e0 74 65 6d 73 2e 0a 2a 2a 20 41 6e 64 20 73 69 6e  tems..** And sin
f28f0 63 65 20 41 46 50 20 69 73 20 6f 6e 6c 79 20 73  ce AFP is only s
f2900 75 70 70 6f 72 74 65 64 20 6f 6e 20 4d 61 63 4f  upported on MacO
f2910 53 58 2c 20 74 68 65 20 70 72 6f 78 79 20 6c 6f  SX, the proxy lo
f2920 63 6b 69 6e 67 20 69 73 20 61 6c 73 6f 0a 2a 2a  cking is also.**
f2930 20 72 65 73 74 72 69 63 74 65 64 20 74 6f 20 4d   restricted to M
f2940 61 63 4f 53 58 2e 0a 2a 2a 20 0a 2a 2a 0a 2a 2a  acOSX..** .**.**
f2950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2960 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 70 72 6f  * End of the pro
f2970 78 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  xy lock implemen
f2980 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tation *********
f2990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
f29a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f29b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f29c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f29d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f29e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
f29f0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
f2a00 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
f2a10 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a  stem interface..
f2a20 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
f2a30 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 6c 6c  ne registers all
f2a40 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74   VFS implementat
f2a50 69 6f 6e 73 20 66 6f 72 20 75 6e 69 78 2d 6c 69  ions for unix-li
f2a60 6b 65 20 6f 70 65 72 61 74 69 6e 67 0a 2a 2a 20  ke operating.** 
f2a70 73 79 73 74 65 6d 73 2e 20 20 54 68 69 73 20 72  systems.  This r
f2a80 6f 75 74 69 6e 65 2c 20 61 6e 64 20 74 68 65 20  outine, and the 
f2a90 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29  sqlite3_os_end()
f2aa0 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 66 6f   routine that fo
f2ab0 6c 6c 6f 77 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64  llows,.** should
f2ac0 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75   be the only rou
f2ad0 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
f2ae0 6c 65 20 74 68 61 74 20 61 72 65 20 76 69 73 69  le that are visi
f2af0 62 6c 65 20 66 72 6f 6d 20 6f 74 68 65 72 0a 2a  ble from other.*
f2b00 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  * files..**.** T
f2b10 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
f2b20 61 6c 6c 65 64 20 6f 6e 63 65 20 64 75 72 69 6e  alled once durin
f2b30 67 20 53 51 4c 69 74 65 20 69 6e 69 74 69 61 6c  g SQLite initial
f2b40 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62 79 20 61  ization and by a
f2b50 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 68 72 65 61  .** single threa
f2b60 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61  d.  The memory a
f2b70 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 6d 75  llocation and mu
f2b80 74 65 78 20 73 75 62 73 79 73 74 65 6d 73 20 68  tex subsystems h
f2b90 61 76 65 20 6e 6f 74 0a 2a 2a 20 6e 65 63 65 73  ave not.** neces
f2ba0 73 61 72 69 6c 79 20 62 65 65 6e 20 69 6e 69 74  sarily been init
f2bb0 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 74 68 69  ialized when thi
f2bc0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
f2bd0 6c 65 64 2c 20 61 6e 64 20 73 6f 20 74 68 65 79  led, and so they
f2be0 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  .** should not b
f2bf0 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  e used..*/.SQLIT
f2c00 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
f2c10 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b  3_os_init(void){
f2c20 20 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 54 68 65   .  /* .  ** The
f2c30 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
f2c40 20 64 65 66 69 6e 65 73 20 61 6e 20 69 6e 69 74   defines an init
f2c50 69 61 6c 69 7a 65 72 20 66 6f 72 20 61 6e 20 73  ializer for an s
f2c60 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
f2c70 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 61 6d 65  t..  ** The name
f2c80 20 6f 66 20 74 68 65 20 56 46 53 20 69 73 20 4e   of the VFS is N
f2c90 41 4d 45 2e 20 20 54 68 65 20 70 41 70 70 44 61  AME.  The pAppDa
f2ca0 74 61 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ta is a pointer 
f2cb0 74 6f 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a  to a pointer.  *
f2cc0 2a 20 74 6f 20 74 68 65 20 22 66 69 6e 64 65 72  * to the "finder
f2cd0 22 20 66 75 6e 63 74 69 6f 6e 2e 20 20 28 70 41  " function.  (pA
f2ce0 70 70 44 61 74 61 20 69 73 20 61 20 70 6f 69 6e  ppData is a poin
f2cf0 74 65 72 20 74 6f 20 61 20 70 6f 69 6e 74 65 72  ter to a pointer
f2d00 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 73 69   because.  ** si
f2d10 6c 6c 79 20 43 39 30 20 72 75 6c 65 73 20 70 72  lly C90 rules pr
f2d20 6f 68 69 62 69 74 20 61 20 76 6f 69 64 2a 20 66  ohibit a void* f
f2d30 72 6f 6d 20 62 65 69 6e 67 20 63 61 73 74 20 74  rom being cast t
f2d40 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  o a function poi
f2d50 6e 74 65 72 0a 20 20 2a 2a 20 61 6e 64 20 73 6f  nter.  ** and so
f2d60 20 77 65 20 68 61 76 65 20 74 6f 20 67 6f 20 74   we have to go t
f2d70 68 72 6f 75 67 68 20 74 68 65 20 69 6e 74 65 72  hrough the inter
f2d80 6d 65 64 69 61 74 65 20 70 6f 69 6e 74 65 72 20  mediate pointer 
f2d90 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d  to avoid problem
f2da0 73 0a 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70  s.  ** when comp
f2db0 69 6c 69 6e 67 20 77 69 74 68 20 2d 70 65 64 61  iling with -peda
f2dc0 6e 74 69 63 2d 65 72 72 6f 72 73 20 6f 6e 20 47  ntic-errors on G
f2dd0 43 43 2e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  CC.).  **.  ** T
f2de0 68 65 20 46 49 4e 44 45 52 20 70 61 72 61 6d 65  he FINDER parame
f2df0 74 65 72 20 74 6f 20 74 68 69 73 20 6d 61 63 72  ter to this macr
f2e00 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  o is the name of
f2e10 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20   the pointer to 
f2e20 74 68 65 0a 20 20 2a 2a 20 66 69 6e 64 65 72 2d  the.  ** finder-
f2e30 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 66  function.  The f
f2e40 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72  inder-function r
f2e50 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
f2e60 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c   to the.  ** sql
f2e70 69 74 65 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  ite_io_methods o
f2e80 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65  bject that imple
f2e90 6d 65 6e 74 73 20 74 68 65 20 64 65 73 69 72 65  ments the desire
f2ea0 64 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 62  d locking.  ** b
f2eb0 65 68 61 76 69 6f 72 73 2e 20 20 53 65 65 20 74  ehaviors.  See t
f2ec0 68 65 20 64 69 76 69 73 69 6f 6e 20 61 62 6f 76  he division abov
f2ed0 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
f2ee0 74 68 65 20 49 4f 4d 45 54 48 4f 44 53 0a 20 20  the IOMETHODS.  
f2ef0 2a 2a 20 6d 61 63 72 6f 20 66 6f 72 20 61 64 64  ** macro for add
f2f00 69 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  ition informatio
f2f10 6e 20 6f 6e 20 66 69 6e 64 65 72 2d 66 75 6e 63  n on finder-func
f2f20 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
f2f30 20 4d 6f 73 74 20 66 69 6e 64 65 72 73 20 73 69   Most finders si
f2f40 6d 70 6c 79 20 72 65 74 75 72 6e 20 61 20 70 6f  mply return a po
f2f50 69 6e 74 65 72 20 74 6f 20 61 20 66 69 78 65 64  inter to a fixed
f2f60 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
f2f70 6f 64 73 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 2e  ods.  ** object.
f2f80 20 20 42 75 74 20 74 68 65 20 22 61 75 74 6f 6c    But the "autol
f2f90 6f 63 6b 49 6f 46 69 6e 64 65 72 22 20 61 76 61  ockIoFinder" ava
f2fa0 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58  ilable on MacOSX
f2fb0 20 64 6f 65 73 20 61 20 6c 69 74 74 6c 65 0a 20   does a little. 
f2fc0 20 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 74 68   ** more than th
f2fd0 61 74 3b 20 69 74 20 6c 6f 6f 6b 73 20 61 74 20  at; it looks at 
f2fe0 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 74  the filesystem t
f2ff0 79 70 65 20 74 68 61 74 20 68 6f 73 74 73 20 74  ype that hosts t
f3000 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  he .  ** databas
f3010 65 20 66 69 6c 65 20 61 6e 64 20 74 72 69 65 73  e file and tries
f3020 20 74 6f 20 63 68 6f 6f 73 65 20 61 6e 20 6c 6f   to choose an lo
f3030 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 61 70 70  cking method app
f3040 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 20 20 2a  ropriate for.  *
f3050 2a 20 74 68 61 74 20 66 69 6c 65 73 79 73 74 65  * that filesyste
f3060 6d 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 23  m time..  */.  #
f3070 64 65 66 69 6e 65 20 55 4e 49 58 56 46 53 28 56  define UNIXVFS(V
f3080 46 53 4e 41 4d 45 2c 20 46 49 4e 44 45 52 29 20  FSNAME, FINDER) 
f3090 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
f30a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 31           \.    1
f30b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
f30c0 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
f30d0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f30e0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73 69 7a         \.    siz
f30f0 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 2c 20 20  eof(unixFile),  
f3100 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a     /* szOsFile *
f3110 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f3120 20 20 20 20 20 5c 0a 20 20 20 20 4d 41 58 5f 50       \.    MAX_P
f3130 41 54 48 4e 41 4d 45 2c 20 20 20 20 20 20 20 20  ATHNAME,        
f3140 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a   /* mxPathname *
f3150 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f3160 20 20 20 5c 0a 20 20 20 20 30 2c 20 20 20 20 20     \.    0,     
f3170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f3180 2a 20 70 4e 65 78 74 20 2a 2f 20 20 20 20 20 20  * pNext */      
f3190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f31a0 20 5c 0a 20 20 20 20 56 46 53 4e 41 4d 45 2c 20   \.    VFSNAME, 
f31b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f31c0 7a 4e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20  zName */        
f31d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
f31e0 0a 20 20 20 20 28 76 6f 69 64 2a 29 26 46 49 4e  .    (void*)&FIN
f31f0 44 45 52 2c 20 20 20 20 20 20 20 2f 2a 20 70 41  DER,       /* pA
f3200 70 70 44 61 74 61 20 2a 2f 20 20 20 20 20 20 20  ppData */       
f3210 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
f3220 20 20 20 75 6e 69 78 4f 70 65 6e 2c 20 20 20 20     unixOpen,    
f3230 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65           /* xOpe
f3240 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  n */            
f3250 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
f3260 20 75 6e 69 78 44 65 6c 65 74 65 2c 20 20 20 20   unixDelete,    
f3270 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74         /* xDelet
f3280 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
f3290 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
f32a0 6e 69 78 41 63 63 65 73 73 2c 20 20 20 20 20 20  nixAccess,      
f32b0 20 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20       /* xAccess 
f32c0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f32d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
f32e0 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20  xFullPathname,  
f32f0 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e     /* xFullPathn
f3300 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ame */          
f3310 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44       \.    unixD
f3320 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20  lOpen,          
f3330 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 20 20   /* xDlOpen */  
f3340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3350 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 45     \.    unixDlE
f3360 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f  rror,          /
f3370 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20  * xDlError */   
f3380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3390 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 53 79 6d   \.    unixDlSym
f33a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
f33b0 78 44 6c 53 79 6d 20 2a 2f 20 20 20 20 20 20 20  xDlSym */       
f33c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
f33d0 0a 20 20 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65  .    unixDlClose
f33e0 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44  ,          /* xD
f33f0 6c 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20  lClose */       
f3400 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
f3410 20 20 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73     unixRandomnes
f3420 73 2c 20 20 20 20 20 20 20 2f 2a 20 78 52 61 6e  s,       /* xRan
f3430 64 6f 6d 6e 65 73 73 20 2a 2f 20 20 20 20 20 20  domness */      
f3440 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
f3450 20 75 6e 69 78 53 6c 65 65 70 2c 20 20 20 20 20   unixSleep,     
f3460 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70         /* xSleep
f3470 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f3480 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
f3490 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 2c 20  nixCurrentTime, 
f34a0 20 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74       /* xCurrent
f34b0 54 69 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20  Time */         
f34c0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
f34d0 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20  xGetLastError   
f34e0 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 72     /* xGetLastEr
f34f0 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ror */          
f3500 20 20 20 20 20 5c 0a 20 20 7d 0a 0a 20 20 2f 2a       \.  }..  /*
f3510 0a 20 20 2a 2a 20 41 6c 6c 20 64 65 66 61 75 6c  .  ** All defaul
f3520 74 20 56 46 53 65 73 20 66 6f 72 20 75 6e 69 78  t VFSes for unix
f3530 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69   are contained i
f3540 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
f3550 61 72 72 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  array..  **.  **
f3560 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73   Note that the s
f3570 71 6c 69 74 65 33 5f 76 66 73 2e 70 4e 65 78 74  qlite3_vfs.pNext
f3580 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 56 46   field of the VF
f3590 53 20 6f 62 6a 65 63 74 20 69 73 20 6d 6f 64 69  S object is modi
f35a0 66 69 65 64 0a 20 20 2a 2a 20 62 79 20 74 68 65  fied.  ** by the
f35b0 20 53 51 4c 69 74 65 20 63 6f 72 65 20 77 68 65   SQLite core whe
f35c0 6e 20 74 68 65 20 56 46 53 20 69 73 20 72 65 67  n the VFS is reg
f35d0 69 73 74 65 72 65 64 2e 20 20 53 6f 20 74 68 65  istered.  So the
f35e0 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20   following.  ** 
f35f0 61 72 72 61 79 20 63 61 6e 6e 6f 74 20 62 65 20  array cannot be 
f3600 63 6f 6e 73 74 2e 0a 20 20 2a 2f 0a 20 20 73 74  const..  */.  st
f3610 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73  atic sqlite3_vfs
f3620 20 61 56 66 73 5b 5d 20 3d 20 7b 0a 23 69 66 20   aVfs[] = {.#if 
f3630 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
f3640 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 28  CKING_STYLE && (
f3650 4f 53 5f 56 58 57 4f 52 4b 53 20 7c 7c 20 64 65  OS_VXWORKS || de
f3660 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
f3670 29 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75  ).    UNIXVFS("u
f3680 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 61  nix",          a
f3690 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20  utolockIoFinder 
f36a0 29 2c 0a 23 65 6c 73 65 0a 20 20 20 20 55 4e 49  ),.#else.    UNI
f36b0 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 20 20  XVFS("unix",    
f36c0 20 20 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e        posixIoFin
f36d0 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20  der ),.#endif.  
f36e0 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d    UNIXVFS("unix-
f36f0 6e 6f 6e 65 22 2c 20 20 20 20 20 6e 6f 6c 6f 63  none",     noloc
f3700 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20  kIoFinder ),.   
f3710 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 64   UNIXVFS("unix-d
f3720 6f 74 66 69 6c 65 22 2c 20 20 64 6f 74 6c 6f 63  otfile",  dotloc
f3730 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20  kIoFinder ),.   
f3740 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 77   UNIXVFS("unix-w
f3750 66 6c 22 2c 20 20 20 20 20 20 70 6f 73 69 78 57  fl",      posixW
f3760 66 6c 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69  flIoFinder ),.#i
f3770 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20  f OS_VXWORKS.   
f3780 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 6e   UNIXVFS("unix-n
f3790 61 6d 65 64 73 65 6d 22 2c 20 73 65 6d 49 6f 46  amedsem", semIoF
f37a0 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a  inder ),.#endif.
f37b0 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
f37c0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
f37d0 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69      UNIXVFS("uni
f37e0 78 2d 70 6f 73 69 78 22 2c 20 20 20 20 70 6f 73  x-posix",    pos
f37f0 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69  ixIoFinder ),.#i
f3800 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  f !OS_VXWORKS.  
f3810 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d    UNIXVFS("unix-
f3820 66 6c 6f 63 6b 22 2c 20 20 20 20 66 6c 6f 63 6b  flock",    flock
f3830 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64  IoFinder ),.#end
f3840 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  if.#endif.#if SQ
f3850 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
f3860 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66  ING_STYLE && def
f3870 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a  ined(__APPLE__).
f3880 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69      UNIXVFS("uni
f3890 78 2d 61 66 70 22 2c 20 20 20 20 20 20 61 66 70  x-afp",      afp
f38a0 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 20  IoFinder ),.    
f38b0 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70 72  UNIXVFS("unix-pr
f38c0 6f 78 79 22 2c 20 20 20 20 70 72 6f 78 79 49 6f  oxy",    proxyIo
f38d0 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 66  Finder ),.#endif
f38e0 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 64  .  };.  unsigned
f38f0 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
f3900 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
f3910 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74   */..  /* Regist
f3920 65 72 20 61 6c 6c 20 56 46 53 65 73 20 64 65 66  er all VFSes def
f3930 69 6e 65 64 20 69 6e 20 74 68 65 20 61 56 66 73  ined in the aVfs
f3940 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20 20 66 6f  [] array */.  fo
f3950 72 28 69 3d 30 3b 20 69 3c 28 73 69 7a 65 6f 66  r(i=0; i<(sizeof
f3960 28 61 56 66 73 29 2f 73 69 7a 65 6f 66 28 73 71  (aVfs)/sizeof(sq
f3970 6c 69 74 65 33 5f 76 66 73 29 29 3b 20 69 2b 2b  lite3_vfs)); i++
f3980 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
f3990 66 73 5f 72 65 67 69 73 74 65 72 28 26 61 56 66  fs_register(&aVf
f39a0 73 5b 69 5d 2c 20 69 3d 3d 30 29 3b 0a 20 20 7d  s[i], i==0);.  }
f39b0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
f39c0 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  _OK; .}../*.** S
f39d0 68 75 74 64 6f 77 6e 20 74 68 65 20 6f 70 65 72  hutdown the oper
f39e0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74  ating system int
f39f0 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f  erface..**.** So
f3a00 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  me operating sys
f3a10 74 65 6d 73 20 6d 69 67 68 74 20 6e 65 65 64 20  tems might need 
f3a20 74 6f 20 64 6f 20 73 6f 6d 65 20 63 6c 65 61 6e  to do some clean
f3a30 75 70 20 69 6e 20 74 68 69 73 20 72 6f 75 74 69  up in this routi
f3a40 6e 65 2c 0a 2a 2a 20 74 6f 20 72 65 6c 65 61 73  ne,.** to releas
f3a50 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  e dynamically al
f3a60 6c 6f 63 61 74 65 64 20 6f 62 6a 65 63 74 73 2e  located objects.
f3a70 20 20 42 75 74 20 6e 6f 74 20 6f 6e 20 75 6e 69    But not on uni
f3a80 78 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  x..** This routi
f3a90 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f  ne is a no-op fo
f3aa0 72 20 75 6e 69 78 2e 0a 2a 2f 0a 53 51 4c 49 54  r unix..*/.SQLIT
f3ab0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
f3ac0 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20  3_os_end(void){ 
f3ad0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
f3ae0 5f 4f 4b 3b 20 0a 7d 0a 20 0a 23 65 6e 64 69 66  _OK; .}. .#endif
f3af0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e   /* SQLITE_OS_UN
f3b00 49 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  IX */../********
f3b10 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
f3b20 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  _unix.c ********
f3b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3b50 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
f3b60 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
f3b70 65 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a  e os_win.c *****
f3b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ba0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
f3bb0 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54  4 May 22.**.** T
f3bc0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
f3bd0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
f3be0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
f3bf0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
f3c00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
f3c10 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
f3c20 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
f3c30 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
f3c40 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
f3c50 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
f3c60 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
f3c70 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
f3c80 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
f3c90 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
f3ca0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
f3cb0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
f3cc0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
f3cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
f3d20 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
f3d30 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20  tains code that 
f3d40 69 73 20 73 70 65 63 69 66 69 63 20 74 6f 20 77  is specific to w
f3d50 69 6e 64 6f 77 73 2e 0a 2a 2f 0a 23 69 66 20 53  indows..*/.#if S
f3d60 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 20 20 20  QLITE_OS_WIN    
f3d70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
f3d80 69 73 20 66 69 6c 65 20 69 73 20 75 73 65 64 20  is file is used 
f3d90 66 6f 72 20 77 69 6e 64 6f 77 73 20 6f 6e 6c 79  for windows only
f3da0 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f   */.../*.** A No
f3db0 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20  te About Memory 
f3dc0 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a  Allocation:.**.*
f3dd0 2a 20 54 68 69 73 20 64 72 69 76 65 72 20 75 73  * This driver us
f3de0 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65  es malloc()/free
f3df0 28 29 20 64 69 72 65 63 74 6c 79 20 72 61 74 68  () directly rath
f3e00 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68  er than going th
f3e10 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c  rough.** the SQL
f3e20 69 74 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c  ite-wrappers sql
f3e30 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71  ite3_malloc()/sq
f3e40 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54  lite3_free().  T
f3e50 68 6f 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a  hose wrappers.**
f3e60 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f   are designed fo
f3e70 72 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65  r use on embedde
f3e80 64 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20  d systems where 
f3e90 6d 65 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65  memory is scarce
f3ea0 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66   and.** malloc f
f3eb0 61 69 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66  ailures happen f
f3ec0 72 65 71 75 65 6e 74 6c 79 2e 20 20 57 69 6e 33  requently.  Win3
f3ed0 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70 69 63  2 does not typic
f3ee0 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65  ally run on.** e
f3ef0 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 2c  mbedded systems,
f3f00 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64 6f 65   and when it doe
f3f10 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73  s the developers
f3f20 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 62   normally have b
f3f30 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d  igger.** problem
f3f40 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74  s to worry about
f3f50 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75   than running ou
f3f60 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f  t of memory.  So
f3f70 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a   there is not.**
f3f80 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65   a compelling ne
f3f90 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 77 72  ed to use the wr
f3fa0 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75  appers..**.** Bu
f3fb0 74 20 74 68 65 72 65 20 69 73 20 61 20 67 6f 6f  t there is a goo
f3fc0 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20  d reason to not 
f3fd0 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73  use the wrappers
f3fe0 2e 20 20 49 66 20 77 65 20 75 73 65 20 74 68 65  .  If we use the
f3ff0 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74 68 65  .** wrappers the
f4000 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20 73 69  n we will get si
f4010 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29  mulated malloc()
f4020 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e   failures within
f4030 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e   this.** driver.
f4040 20 20 41 6e 64 20 74 68 61 74 20 63 61 75 73 65    And that cause
f4050 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70  s all kinds of p
f4060 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20  roblems for our 
f4070 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f  tests.  We.** co
f4080 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51 4c 69  uld enhance SQLi
f4090 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20  te to deal with 
f40a0 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63  simulated malloc
f40b0 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e   failures within
f40c0 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69 76 65  .** the OS drive
f40d0 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64 65 20  r, but the code 
f40e0 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68 6f  to deal with tho
f40f0 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75 6c 64  se failure would
f4100 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72 63   not.** be exerc
f4110 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77  ised on Linux (w
f4120 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65  hich does not ne
f4130 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69  ed to malloc() i
f4140 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a 2a 2a  n the driver).**
f4150 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c 64   and so we would
f4160 20 68 61 76 65 20 64 69 66 66 69 63 75 6c 74 79   have difficulty
f4170 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72 61 67   writing coverag
f4180 65 20 74 65 73 74 73 20 66 6f 72 20 74 68 61 74  e tests for that
f4190 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74 74 65  .** code.  Bette
f41a0 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63  r to leave the c
f41b0 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68 69 6e  ode out, we thin
f41c0 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69  k..**.** The poi
f41d0 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73 63 75  nt of this discu
f41e0 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c  ssion is as foll
f41f0 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65 61 74  ows:  When creat
f4200 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20  ing a new.** OS 
f4210 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65 6d 62  layer for an emb
f4220 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20 69 66  edded system, if
f4230 20 79 6f 75 20 75 73 65 20 74 68 69 73 20 66 69   you use this fi
f4240 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70 6c 65  le as an example
f4250 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 75  ,.** avoid the u
f4260 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66  se of malloc()/f
f4270 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 72 6f  ree().  Those ro
f4280 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f  utines work ok o
f4290 6e 20 77 69 6e 64 6f 77 73 0a 2a 2a 20 64 65 73  n windows.** des
f42a0 6b 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 6f  ktops but not so
f42b0 20 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65   well in embedde
f42c0 64 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 23  d systems..*/..#
f42d0 69 6e 63 6c 75 64 65 20 3c 77 69 6e 62 61 73 65  include <winbase
f42e0 2e 68 3e 0a 0a 23 69 66 64 65 66 20 5f 5f 43 59  .h>..#ifdef __CY
f42f0 47 57 49 4e 5f 5f 0a 23 20 69 6e 63 6c 75 64 65  GWIN__.# include
f4300 20 3c 73 79 73 2f 63 79 67 77 69 6e 2e 68 3e 0a   <sys/cygwin.h>.
f4310 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
f4320 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74  cros used to det
f4330 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
f4340 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72  r not to use thr
f4350 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  eads..*/.#if def
f4360 69 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 29  ined(THREADSAFE)
f4370 20 26 26 20 54 48 52 45 41 44 53 41 46 45 0a 23   && THREADSAFE.#
f4380 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57   define SQLITE_W
f4390 33 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e  32_THREADS 1.#en
f43a0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75  dif../*.** Inclu
f43b0 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  de code that is 
f43c0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73  common to all os
f43d0 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a  _*.c files.*/./*
f43e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
f43f0 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  clude os_common.
f4400 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
f4410 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a  of os_win.c ****
f4420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
f4430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
f4440 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d  gin file os_comm
f4450 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
f4460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
f4480 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a  .** 2004 May 22.
f4490 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
f44a0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
f44b0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
f44c0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
f44d0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
f44e0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
f44f0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
f4500 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
f4510 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
f4520 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
f4530 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
f4540 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
f4550 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
f4560 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
f4570 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
f4580 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
f4590 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
f45a0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
f45b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f45c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f45d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f45e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f45f0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
f4600 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63  ile contains mac
f4610 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65  ros and a little
f4620 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61   bit of code tha
f4630 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a  t is common to.*
f4640 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61  * all of the pla
f4650 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66  tform-specific f
f4660 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e  iles (os_*.c) an
f4670 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69  d is #included i
f4680 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c  nto those.** fil
f4690 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  es..**.** This f
f46a0 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69  ile should be #i
f46b0 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f  ncluded by the o
f46c0 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79  s_*.c files only
f46d0 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a  .  It is not a.*
f46e0 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  * general purpos
f46f0 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a  e header file..*
f4700 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d  *.** $Id: os_com
f4710 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30  mon.h,v 1.38 200
f4720 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30  9/02/24 18:40:50
f4730 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
f4740 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f   $.*/.#ifndef _O
f4750 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66  S_COMMON_H_.#def
f4760 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  ine _OS_COMMON_H
f4770 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73  _../*.** At leas
f4780 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20  t two bugs have 
f4790 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75  slipped in becau
f47a0 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68  se we changed th
f47b0 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a  e MEMORY_DEBUG.*
f47c0 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54  * macro to SQLIT
f47d0 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65  E_DEBUG and some
f47e0 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73   older makefiles
f47f0 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61   have not yet ma
f4800 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68  de the.** switch
f4810 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
f4820 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74   code should cat
f4830 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ch this problem 
f4840 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e  at compile-time.
f4850 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52  .*/.#ifdef MEMOR
f4860 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20  Y_DEBUG.# error 
f4870 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55  "The MEMORY_DEBU
f4880 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c  G macro is obsol
f4890 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45  ete.  Use SQLITE
f48a0 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22  _DEBUG instead."
f48b0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
f48c0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
f48d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
f48e0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d  sqlite3OSTrace =
f48f0 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52   0;.#define OSTR
f4900 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20  ACE1(X)         
f4910 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
f4920 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
f4930 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69  gPrintf(X).#defi
f4940 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
f4950 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
f4960 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
f4970 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
f4980 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
f4990 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20  ACE3(X,Y,Z)     
f49a0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
f49b0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
f49c0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23  gPrintf(X,Y,Z).#
f49d0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28  define OSTRACE4(
f49e0 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73  X,Y,Z,A)   if( s
f49f0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
f4a00 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
f4a10 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66  tf(X,Y,Z,A).#def
f4a20 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59  ine OSTRACE5(X,Y
f4a30 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69  ,Z,A,B) if( sqli
f4a40 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
f4a50 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
f4a60 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
f4a70 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
f4a80 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69  Z,A,B,C) \.    i
f4a90 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
f4aa0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
f4ab0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
f4ac0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
f4ad0 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
f4ae0 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
f4af0 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
f4b00 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
f4b10 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c  Y,Z,A,B,C,D).#el
f4b20 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  se.#define OSTRA
f4b30 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f  CE1(X).#define O
f4b40 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65  STRACE2(X,Y).#de
f4b50 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
f4b60 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Y,Z).#define OST
f4b70 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23  RACE4(X,Y,Z,A).#
f4b80 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28  define OSTRACE5(
f4b90 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
f4ba0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
f4bb0 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
f4bc0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
f4bd0 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a  A,B,C,D).#endif.
f4be0 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  ./*.** Macros fo
f4bf0 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72  r performance tr
f4c00 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79  acing.  Normally
f4c10 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e   turned off.  On
f4c20 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69  ly works.** on i
f4c30 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f  486 hardware..*/
f4c40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50  .#ifdef SQLITE_P
f4c50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45  ERFORMANCE_TRACE
f4c60 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e  ../* .** hwtime.
f4c70 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e  h contains inlin
f4c80 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65  e assembler code
f4c90 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e   for implementin
f4ca0 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f  g .** high-perfo
f4cb0 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f  rmance timing ro
f4cc0 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a  utines..*/./****
f4cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
f4ce0 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74  de hwtime.h in t
f4cf0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f  he middle of os_
f4d00 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
f4d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
f4d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
f4d30 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a   file hwtime.h *
f4d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
f4d70 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a   2008 May 27.**.
f4d80 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
f4d90 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
f4da0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
f4db0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
f4dc0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
f4dd0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
f4de0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
f4df0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
f4e00 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
f4e10 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
f4e20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
f4e30 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
f4e40 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
f4e50 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
f4e60 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
f4e70 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
f4e80 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
f4e90 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
f4ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4ee0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
f4ef0 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65   contains inline
f4f00 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65   asm code for re
f4f10 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70  trieving "high-p
f4f20 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63  erformance".** c
f4f30 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20  ounters for x86 
f4f40 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a  class CPUs..**.*
f4f50 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c  * $Id: hwtime.h,
f4f60 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31  v 1.3 2008/08/01
f4f70 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20   14:33:15 shane 
f4f80 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
f4f90 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66   _HWTIME_H_.#def
f4fa0 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a  ine _HWTIME_H_..
f4fb0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
f4fc0 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  ing routine only
f4fd0 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75   works on pentiu
f4fe0 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65  m-class (or newe
f4ff0 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a  r) processors..*
f5000 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44  * It uses the RD
f5010 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65  TSC opcode to re
f5020 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75  ad the cycle cou
f5030 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20  nt value out of 
f5040 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72  the.** processor
f5050 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61   and returns tha
f5060 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63  t value.  This c
f5070 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68  an be used for h
f5080 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69  igh-res.** profi
f5090 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65  ling..*/.#if (de
f50a0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
f50b0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  || defined(_MSC_
f50c0 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20  VER)) && \.     
f50d0 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20   (defined(i386) 
f50e0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38  || defined(__i38
f50f0 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  6__) || defined(
f5100 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66  _M_IX86))..  #if
f5110 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f   defined(__GNUC_
f5120 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  _)..  __inline__
f5130 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
f5140 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
f5150 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65  d){.     unsigne
f5160 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20  d int lo, hi;.  
f5170 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
f5180 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
f5190 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d   : "=a" (lo), "=
f51a0 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72  d" (hi));.     r
f51b0 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69  eturn (sqlite_ui
f51c0 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20  nt64)hi << 32 | 
f51d0 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66  lo;.  }..  #elif
f51e0 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
f51f0 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63  R)..  __declspec
f5200 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65  (naked) __inline
f5210 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f   sqlite_uint64 _
f5220 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77  _cdecl sqlite3Hw
f5230 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
f5240 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20   __asm {.       
f5250 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72   rdtsc.        r
f5260 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72  et       ; retur
f5270 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45  n value at EDX:E
f5280 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  AX.     }.  }.. 
f5290 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28   #endif..#elif (
f52a0 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
f52b0 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78  ) && defined(__x
f52c0 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69  86_64__))..  __i
f52d0 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
f52e0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
f52f0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
f5300 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76   unsigned long v
f5310 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  al;.      __asm_
f5320 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
f5330 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28  "rdtsc" : "=A" (
f5340 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74  val));.      ret
f5350 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23  urn val;.  }. .#
f5360 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  elif (defined(__
f5370 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e  GNUC__) && defin
f5380 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20  ed(__ppc__))..  
f5390 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
f53a0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
f53b0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
f53c0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
f53d0 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20  g long retval;. 
f53e0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
f53f0 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f  ng junk;.      _
f5400 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
f5410 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20  e__ ("\n\.      
f5420 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62      1:      mftb
f5430 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20  u   %1\n\.      
f5440 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62              mftb
f5450 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20      %L0\n\.     
f5460 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74               mft
f5470 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20  bu   %0\n\.     
f5480 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70               cmp
f5490 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20  w    %0,%1\n\.  
f54a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f54b0 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20  bne     1b".    
f54c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20                : 
f54d0 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22  "=r" (retval), "
f54e0 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20  =r" (junk));.   
f54f0 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c     return retval
f5500 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20  ;.  }..#else..  
f5510 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c  #error Need impl
f5520 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
f5530 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f  lite3Hwtime() fo
f5540 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e  r your platform.
f5550 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63  ..  /*.  ** To c
f5560 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69  ompile without i
f5570 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69  mplementing sqli
f5580 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20  te3Hwtime() for 
f5590 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20  your platform,. 
f55a0 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f   ** you can remo
f55b0 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72  ve the above #er
f55c0 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20  ror and use the 
f55d0 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73  following.  ** s
f55e0 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59  tub function.  Y
f55f0 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d  ou will lose tim
f5600 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20  ing support for 
f5610 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65  many.  ** of the
f5620 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74   debugging and t
f5630 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73  esting utilities
f5640 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20  , but it should 
f5650 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f  at.  ** least co
f5660 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20  mpile and run.. 
f5670 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
f5680 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74  TE   sqlite_uint
f5690 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
f56a0 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28  (void){ return (
f56b0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
f56c0 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65  ); }..#endif..#e
f56d0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
f56e0 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a  (_HWTIME_H_) */.
f56f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
f5700 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68   End of hwtime.h
f5710 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
f5720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
f5740 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
f5750 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
f5760 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
f5770 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
f5780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
f5790 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f  ..static sqlite_
f57a0 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a  uint64 g_start;.
f57b0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69  static sqlite_ui
f57c0 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a  nt64 g_elapsed;.
f57d0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54  #define TIMER_ST
f57e0 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72  ART       g_star
f57f0 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  t=sqlite3Hwtime(
f5800 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  ).#define TIMER_
f5810 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c  END         g_el
f5820 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74  apsed=sqlite3Hwt
f5830 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64  ime()-g_start.#d
f5840 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
f5850 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65  SED     g_elapse
f5860 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  d.#else.#define 
f5870 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66  TIMER_START.#def
f5880 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64  ine TIMER_END.#d
f5890 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
f58a0 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65  SED     ((sqlite
f58b0 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69  _uint64)0).#endi
f58c0 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63  f../*.** If we c
f58d0 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20  ompile with the 
f58e0 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72  SQLITE_TEST macr
f58f0 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  o set, then the 
f5900 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a  following block.
f5910 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20  ** of code will 
f5920 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c  give us the abil
f5930 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20  ity to simulate 
f5940 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72  a disk I/O error
f5950 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73  .  This.** is us
f5960 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74  ed for testing t
f5970 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20  he I/O recovery 
f5980 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66  logic..*/.#ifdef
f5990 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
f59a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f59b0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
f59c0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
f59d0 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
f59e0 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a   of I/O Errors *
f59f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
f5a00 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
f5a10 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20  r_hardhit = 0;  
f5a20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
f5a30 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72  of non-benign er
f5a40 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  rors */.SQLITE_A
f5a50 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
f5a60 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20  o_error_pending 
f5a70 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  = 0;        /* C
f5a80 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72  ount down to fir
f5a90 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a  st I/O error */.
f5aa0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
f5ab0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
f5ac0 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20  persist = 0;    
f5ad0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49      /* True if I
f5ae0 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73  /O errors persis
f5af0 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  t */.SQLITE_API 
f5b00 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
f5b10 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b  rror_benign = 0;
f5b20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
f5b30 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62   if errors are b
f5b40 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f  enign */.SQLITE_
f5b50 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
f5b60 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
f5b70 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49   = 0;.SQLITE_API
f5b80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73   int sqlite3_dis
f5b90 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69  kfull = 0;.#defi
f5ba0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
f5bb0 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69  orBenign(X) sqli
f5bc0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e  te3_io_error_ben
f5bd0 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20  ign=(X).#define 
f5be0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
f5bf0 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28  CODE)  \.  if( (
f5c00 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
f5c10 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69  _persist && sqli
f5c20 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
f5c30 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71  ) \.       || sq
f5c40 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
f5c50 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20  ending-- == 1 ) 
f5c60 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
f5c70 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29   { local_ioerr()
f5c80 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63  ; CODE; }.static
f5c90 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72   void local_ioer
f5ca0 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28  r(){.  IOTRACE((
f5cb0 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73  "IOERR\n"));.  s
f5cc0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
f5cd0 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71  hit++;.  if( !sq
f5ce0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
f5cf0 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f  enign ) sqlite3_
f5d00 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74  io_error_hardhit
f5d10 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69  ++;.}.#define Si
f5d20 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
f5d30 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69  ror(CODE) \.   i
f5d40 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  f( sqlite3_diskf
f5d50 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c  ull_pending ){ \
f5d60 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  .     if( sqlite
f5d70 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
f5d80 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20  ng == 1 ){ \.   
f5d90 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28      local_ioerr(
f5da0 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69  ); \.       sqli
f5db0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31  te3_diskfull = 1
f5dc0 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  ; \.       sqlit
f5dd0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20  e3_io_error_hit 
f5de0 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f  = 1; \.       CO
f5df0 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65  DE; \.     }else
f5e00 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  { \.       sqlit
f5e10 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
f5e20 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20  ing--; \.     } 
f5e30 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65  \.   }.#else.#de
f5e40 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
f5e50 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64  rrorBenign(X).#d
f5e60 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
f5e70 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65  Error(A).#define
f5e80 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
f5e90 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66  lError(A).#endif
f5ea0 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73  ../*.** When tes
f5eb0 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75  ting, keep a cou
f5ec0 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  nt of the number
f5ed0 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a   of open files..
f5ee0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
f5ef0 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
f5f00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
f5f10 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30  n_file_count = 0
f5f20 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f  ;.#define OpenCo
f5f30 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65  unter(X)  sqlite
f5f40 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e  3_open_file_coun
f5f50 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65  t+=(X).#else.#de
f5f60 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72  fine OpenCounter
f5f70 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64  (X).#endif..#end
f5f80 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
f5f90 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f  OS_COMMON_H_) */
f5fa0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
f5fb0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d  * End of os_comm
f5fc0 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
f5fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5ff0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
f6000 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
f6010 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
f6020 6e 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a  n os_win.c *****
f6030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6040 2f 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69  /../*.** Some mi
f6050 63 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72  crosoft compiler
f6060 73 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69  s lack this defi
f6070 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64  nition..*/.#ifnd
f6080 65 66 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f  ef INVALID_FILE_
f6090 41 54 54 52 49 42 55 54 45 53 0a 23 20 64 65 66  ATTRIBUTES.# def
f60a0 69 6e 65 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45  ine INVALID_FILE
f60b0 5f 41 54 54 52 49 42 55 54 45 53 20 28 28 44 57  _ATTRIBUTES ((DW
f60c0 4f 52 44 29 2d 31 29 20 0a 23 65 6e 64 69 66 0a  ORD)-1) .#endif.
f60d0 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65  ./*.** Determine
f60e0 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69   if we are deali
f60f0 6e 67 20 77 69 74 68 20 57 69 6e 64 6f 77 73 43  ng with WindowsC
f6100 45 20 2d 20 77 68 69 63 68 20 68 61 73 20 61 20  E - which has a 
f6110 6d 75 63 68 0a 2a 2a 20 72 65 64 75 63 65 64 20  much.** reduced 
f6120 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  API..*/.#if SQLI
f6130 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65  TE_OS_WINCE.# de
f6140 66 69 6e 65 20 41 72 65 46 69 6c 65 41 70 69 73  fine AreFileApis
f6150 41 4e 53 49 28 29 20 31 0a 23 20 64 65 66 69 6e  ANSI() 1.# defin
f6160 65 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57  e FormatMessageW
f6170 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 2c 67 29 20  (a,b,c,d,e,f,g) 
f6180 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
f6190 57 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69  WinCE lacks nati
f61a0 76 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66  ve support for f
f61b0 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77  ile locking so w
f61c0 65 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69  e have to fake i
f61d0 74 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63  t.** with some c
f61e0 6f 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a  ode of our own..
f61f0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
f6200 5f 57 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73  _WINCE.typedef s
f6210 74 72 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20  truct winceLock 
f6220 7b 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73  {.  int nReaders
f6230 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
f6240 72 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b  r of reader lock
f6250 73 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20  s obtained */.  
f6260 42 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20  BOOL bPending;  
f6270 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73      /* Indicates
f6280 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20   a pending lock 
f6290 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65  has been obtaine
f62a0 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73  d */.  BOOL bRes
f62b0 65 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e  erved;     /* In
f62c0 64 69 63 61 74 65 73 20 61 20 72 65 73 65 72 76  dicates a reserv
f62d0 65 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e  ed lock has been
f62e0 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42   obtained */.  B
f62f0 4f 4f 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20  OOL bExclusive; 
f6300 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20     /* Indicates 
f6310 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
f6320 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69  k has been obtai
f6330 6e 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f  ned */.} winceLo
f6340 63 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ck;.#endif../*.*
f6350 2a 20 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74  * The winFile st
f6360 72 75 63 74 75 72 65 20 69 73 20 61 20 73 75 62  ructure is a sub
f6370 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33  class of sqlite3
f6380 5f 66 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20  _file* specific 
f6390 74 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20  to the win32.** 
f63a0 70 6f 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65  portability laye
f63b0 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  r..*/.typedef st
f63c0 72 75 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e  ruct winFile win
f63d0 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e  File;.struct win
f63e0 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73  File {.  const s
f63f0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
f6400 73 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75  s *pMethod;/* Mu
f6410 73 74 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20  st be first */. 
f6420 20 48 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20   HANDLE h;      
f6430 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64           /* Hand
f6440 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67  le for accessing
f6450 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75   the file */.  u
f6460 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63  nsigned char loc
f6470 6b 74 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f  ktype; /* Type o
f6480 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79  f lock currently
f6490 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69   held on this fi
f64a0 6c 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68  le */.  short sh
f64b0 61 72 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20  aredLockByte;   
f64c0 2f 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73  /* Randomly chos
f64d0 65 6e 20 62 79 74 65 20 75 73 65 64 20 61 73 20  en byte used as 
f64e0 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f  a shared lock */
f64f0 0a 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72  .  DWORD lastErr
f6500 6e 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  no;        /* Th
f6510 65 20 57 69 6e 64 6f 77 73 20 65 72 72 6e 6f 20  e Windows errno 
f6520 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f  from the last I/
f6530 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 20 44 57 4f  O error */.  DWO
f6540 52 44 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20  RD sectorSize;  
f6550 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73       /* Sector s
f6560 69 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63  ize of the devic
f6570 65 20 66 69 6c 65 20 69 73 20 6f 6e 20 2a 2f 0a  e file is on */.
f6580 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
f6590 4e 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65  NCE.  WCHAR *zDe
f65a0 6c 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a  leteOnClose;  /*
f65b0 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f   Name of file to
f65c0 20 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f   delete when clo
f65d0 73 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45  sing */.  HANDLE
f65e0 20 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20   hMutex;        
f65f0 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20    /* Mutex used 
f6600 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73  to control acces
f6610 73 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b  s to shared lock
f6620 20 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68   */  .  HANDLE h
f6630 53 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20  Shared;         
f6640 2f 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79  /* Shared memory
f6650 20 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f   segment used fo
f6660 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77  r locking */.  w
f6670 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20  inceLock local; 
f6680 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20         /* Locks 
f6690 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73  obtained by this
f66a0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e   instance of win
f66b0 46 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c  File */.  winceL
f66c0 6f 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20  ock *shared;    
f66d0 20 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72    /* Global shar
f66e0 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66  ed lock memory f
f66f0 6f 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a  or the file  */.
f6700 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
f6710 20 46 6f 72 77 61 72 64 20 70 72 6f 74 6f 74 79   Forward prototy
f6720 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  pes..*/.static i
f6730 6e 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65  nt getSectorSize
f6740 28 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66  (.    sqlite3_vf
f6750 73 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e  s *pVfs,.    con
f6760 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69  st char *zRelati
f6770 76 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20  ve     /* UTF-8 
f6780 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a  file name */.);.
f6790 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
f67a0 77 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73  wing variable is
f67b0 20 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20   (normally) set 
f67c0 6f 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63  once and never c
f67d0 68 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61  hanges.** therea
f67e0 66 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64  fter.  It record
f67f0 73 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70  s whether the op
f6800 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
f6810 73 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69  s Win95.** or Wi
f6820 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20  nNT..**.** 0:   
f6830 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  Operating system
f6840 20 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20   unknown..** 1: 
f6850 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74    Operating syst
f6860 65 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20  em is Win95..** 
f6870 32 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73  2:   Operating s
f6880 79 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a  ystem is WinNT..
f6890 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74  **.** In order t
f68a0 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73  o facilitate tes
f68b0 74 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20  ting on a WinNT 
f68c0 73 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74  system, the test
f68d0 20 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20   fixture.** can 
f68e0 6d 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69  manually set thi
f68f0 73 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20  s value to 1 to 
f6900 65 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65  emulate Win98 be
f6910 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65  havior..*/.#ifde
f6920 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
f6930 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
f6940 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30  ite3_os_type = 0
f6950 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69  ;.#else.static i
f6960 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79  nt sqlite3_os_ty
f6970 70 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  pe = 0;.#endif..
f6980 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
f6990 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20  e (non-zero) if 
f69a0 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75  we are running u
f69b0 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32  nder WinNT, Win2
f69c0 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20  K, WinXP,.** or 
f69d0 57 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66  WinCE.  Return f
f69e0 61 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20  alse (zero) for 
f69f0 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72  Win95, Win98, or
f6a00 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65   WinME..**.** He
f6a10 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73  re is an interes
f6a20 74 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e  ting observation
f6a30 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c  :  Win95, Win98,
f6a40 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a   and WinME lack.
f6a50 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45  ** the LockFileE
f6a60 78 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65  x() API.  But we
f6a70 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69   can still stati
f6a80 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e  cally link again
f6a90 73 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61  st that.** API a
f6aa0 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e  s long as we don
f6ab0 27 74 20 63 61 6c 6c 20 69 74 20 77 68 65 6e 20  't call it when 
f6ac0 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38  running Win95/98
f6ad0 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a  /ME.  A call to.
f6ae0 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
f6af0 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  is used to deter
f6b00 6d 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74  mine if the host
f6b10 20 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20   is Win95/98/ME 
f6b20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58  or.** WinNT/2K/X
f6b30 50 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c  P so that we wil
f6b40 6c 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f  l know whether o
f6b50 72 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66  r not we can saf
f6b60 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20  ely call.** the 
f6b70 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49  LockFileEx() API
f6b80 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
f6b90 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e  OS_WINCE.# defin
f6ba0 65 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65  e isNT()  (1).#e
f6bb0 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74  lse.  static int
f6bc0 20 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20   isNT(void){.   
f6bd0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f   if( sqlite3_os_
f6be0 74 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  type==0 ){.     
f6bf0 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73   OSVERSIONINFO s
f6c00 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66  Info;.      sInf
f6c10 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66  o.dwOSVersionInf
f6c20 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73  oSize = sizeof(s
f6c30 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74  Info);.      Get
f6c40 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f  VersionEx(&sInfo
f6c50 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
f6c60 5f 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f  _os_type = sInfo
f6c70 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56  .dwPlatformId==V
f6c80 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33  ER_PLATFORM_WIN3
f6c90 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20  2_NT ? 2 : 1;.  
f6ca0 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73    }.    return s
f6cb0 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d  qlite3_os_type==
f6cc0 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  2;.  }.#endif /*
f6cd0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
f6ce0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65   */../*.** Conve
f6cf0 72 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e  rt a UTF-8 strin
f6d00 67 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75  g to microsoft u
f6d10 6e 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29  nicode (UTF-16?)
f6d20 2e 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74  . .**.** Space t
f6d30 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72  o hold the retur
f6d40 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62  ned string is ob
f6d50 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
f6d60 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43  oc..*/.static WC
f6d70 48 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f  HAR *utf8ToUnico
f6d80 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  de(const char *z
f6d90 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74  Filename){.  int
f6da0 20 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20   nChar;.  WCHAR 
f6db0 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a  *zWideFilename;.
f6dc0 0a 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69  .  nChar = Multi
f6dd0 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43  ByteToWideChar(C
f6de0 50 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65  P_UTF8, 0, zFile
f6df0 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20  name, -1, NULL, 
f6e00 30 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e  0);.  zWideFilen
f6e10 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43  ame = malloc( nC
f6e20 68 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65  har*sizeof(zWide
f6e30 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a  Filename[0]) );.
f6e40 20 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e    if( zWideFilen
f6e50 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ame==0 ){.    re
f6e60 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43  turn 0;.  }.  nC
f6e70 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54  har = MultiByteT
f6e80 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46  oWideChar(CP_UTF
f6e90 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  8, 0, zFilename,
f6ea0 20 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61   -1, zWideFilena
f6eb0 6d 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66  me, nChar);.  if
f6ec0 28 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20  ( nChar==0 ){.  
f6ed0 20 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65    free(zWideFile
f6ee0 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65  name);.    zWide
f6ef0 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20  Filename = 0;.  
f6f00 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65  }.  return zWide
f6f10 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  Filename;.}../*.
f6f20 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f  ** Convert micro
f6f30 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20  soft unicode to 
f6f40 55 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f  UTF-8.  Space to
f6f50 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e   hold the return
f6f60 65 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20  ed string is.** 
f6f70 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61  obtained from ma
f6f80 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  lloc()..*/.stati
f6f90 63 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54  c char *unicodeT
f6fa0 6f 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41  oUtf8(const WCHA
f6fb0 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65  R *zWideFilename
f6fc0 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a  ){.  int nByte;.
f6fd0 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d    char *zFilenam
f6fe0 65 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69  e;..  nByte = Wi
f6ff0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74  deCharToMultiByt
f7000 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57  e(CP_UTF8, 0, zW
f7010 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c  ideFilename, -1,
f7020 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20   0, 0, 0, 0);.  
f7030 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c  zFilename = mall
f7040 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69  oc( nByte );.  i
f7050 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20  f( zFilename==0 
f7060 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
f7070 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57  .  }.  nByte = W
f7080 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79  ideCharToMultiBy
f7090 74 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a  te(CP_UTF8, 0, z
f70a0 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31  WideFilename, -1
f70b0 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79  , zFilename, nBy
f70c0 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  te,.            
f70d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f70e0 20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e    0, 0);.  if( n
f70f0 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20  Byte == 0 ){.   
f7100 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29   free(zFilename)
f7110 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20  ;.    zFilename 
f7120 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
f7130 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a  n zFilename;.}..
f7140 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e  /*.** Convert an
f7150 20 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20   ansi string to 
f7160 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64  microsoft unicod
f7170 65 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a  e, based on the.
f7180 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70  ** current codep
f7190 61 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72  age settings for
f71a0 20 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a   file apis..** .
f71b0 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  ** Space to hold
f71c0 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
f71d0 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64  ring is obtained
f71e0 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e  .** from malloc.
f71f0 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52  .*/.static WCHAR
f7200 20 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28   *mbcsToUnicode(
f7210 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
f7220 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42  ename){.  int nB
f7230 79 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d  yte;.  WCHAR *zM
f7240 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69  bcsFilename;.  i
f7250 6e 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72  nt codepage = Ar
f7260 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20  eFileApisANSI() 
f7270 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45  ? CP_ACP : CP_OE
f7280 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20  MCP;..  nByte = 
f7290 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43  MultiByteToWideC
f72a0 68 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c  har(codepage, 0,
f72b0 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20   zFilename, -1, 
f72c0 4e 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57  NULL,0)*sizeof(W
f72d0 43 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69  CHAR);.  zMbcsFi
f72e0 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28  lename = malloc(
f72f0 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d   nByte*sizeof(zM
f7300 62 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20  bcsFilename[0]) 
f7310 29 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69  );.  if( zMbcsFi
f7320 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  lename==0 ){.   
f7330 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
f7340 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79   nByte = MultiBy
f7350 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64  teToWideChar(cod
f7360 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e  epage, 0, zFilen
f7370 61 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69  ame, -1, zMbcsFi
f7380 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a  lename, nByte);.
f7390 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29    if( nByte==0 )
f73a0 7b 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73  {.    free(zMbcs
f73b0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a  Filename);.    z
f73c0 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30  MbcsFilename = 0
f73d0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
f73e0 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a  MbcsFilename;.}.
f73f0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d  ./*.** Convert m
f7400 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65  icrosoft unicode
f7410 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68   to multibyte ch
f7420 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20  aracter string, 
f7430 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20  based on the.** 
f7440 75 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65  user's Ansi code
f7450 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63  page..**.** Spac
f7460 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65  e to hold the re
f7470 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73  turned string is
f7480 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
f7490 2a 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73  * malloc()..*/.s
f74a0 74 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63  tatic char *unic
f74b0 6f 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20  odeToMbcs(const 
f74c0 57 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65  WCHAR *zWideFile
f74d0 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79  name){.  int nBy
f74e0 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c  te;.  char *zFil
f74f0 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64  ename;.  int cod
f7500 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41  epage = AreFileA
f7510 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41  pisANSI() ? CP_A
f7520 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a  CP : CP_OEMCP;..
f7530 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68    nByte = WideCh
f7540 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f  arToMultiByte(co
f7550 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65  depage, 0, zWide
f7560 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c  Filename, -1, 0,
f7570 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69   0, 0, 0);.  zFi
f7580 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28  lename = malloc(
f7590 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20   nByte );.  if( 
f75a0 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a  zFilename==0 ){.
f75b0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
f75c0 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65  }.  nByte = Wide
f75d0 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28  CharToMultiByte(
f75e0 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69  codepage, 0, zWi
f75f0 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20  deFilename, -1, 
f7600 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65  zFilename, nByte
f7610 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
f7620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7630 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79  0, 0);.  if( nBy
f7640 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66  te == 0 ){.    f
f7650 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ree(zFilename);.
f7660 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20      zFilename = 
f7670 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
f7680 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a  zFilename;.}../*
f7690 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74  .** Convert mult
f76a0 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20  ibyte character 
f76b0 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e  string to UTF-8.
f76c0 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
f76d0 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  the.** returned 
f76e0 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e  string is obtain
f76f0 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
f7700 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
f7710 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69  char *sqlite3_wi
f7720 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38  n32_mbcs_to_utf8
f7730 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
f7740 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20  lename){.  char 
f7750 2a 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a  *zFilenameUtf8;.
f7760 20 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64    WCHAR *zTmpWid
f7770 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d  e;..  zTmpWide =
f7780 20 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a   mbcsToUnicode(z
f7790 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28  Filename);.  if(
f77a0 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a   zTmpWide==0 ){.
f77b0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
f77c0 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66  }.  zFilenameUtf
f77d0 38 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66  8 = unicodeToUtf
f77e0 38 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66  8(zTmpWide);.  f
f77f0 72 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20  ree(zTmpWide);. 
f7800 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d   return zFilenam
f7810 65 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eUtf8;.}../*.** 
f7820 43 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f  Convert UTF-8 to
f7830 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61   multibyte chara
f7840 63 74 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70  cter string.  Sp
f7850 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ace to hold the 
f7860 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72  .** returned str
f7870 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ing is obtained 
f7880 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a  from malloc()..*
f7890 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75  /.static char *u
f78a0 74 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20  tf8ToMbcs(const 
f78b0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
f78c0 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  {.  char *zFilen
f78d0 61 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52  ameMbcs;.  WCHAR
f78e0 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a   *zTmpWide;..  z
f78f0 54 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f  TmpWide = utf8To
f7900 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d  Unicode(zFilenam
f7910 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69  e);.  if( zTmpWi
f7920 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  de==0 ){.    ret
f7930 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69  urn 0;.  }.  zFi
f7940 6c 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69  lenameMbcs = uni
f7950 63 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57  codeToMbcs(zTmpW
f7960 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d  ide);.  free(zTm
f7970 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e  pWide);.  return
f7980 20 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a   zFilenameMbcs;.
f7990 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  }..#if SQLITE_OS
f79a0 5f 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  _WINCE./********
f79b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f79c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f79d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f79e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f79f0 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f  *.** This sectio
f7a00 6e 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  n contains code 
f7a10 66 6f 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a  for WinCE only..
f7a20 2a 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73  */./*.** Windows
f7a30 43 45 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  CE does not have
f7a40 20 61 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66   a localtime() f
f7a50 75 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65  unction.  So cre
f7a60 61 74 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74  ate a.** substit
f7a70 75 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74  ute..*/.struct t
f7a80 6d 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c  m *__cdecl local
f7a90 74 69 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f  time(const time_
f7aa0 74 20 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63  t *t).{.  static
f7ab0 20 73 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20   struct tm y;.  
f7ac0 46 49 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54  FILETIME uTm, lT
f7ad0 6d 3b 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20  m;.  SYSTEMTIME 
f7ae0 70 54 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  pTm;.  sqlite3_i
f7af0 6e 74 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20  nt64 t64;.  t64 
f7b00 3d 20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74  = *t;.  t64 = (t
f7b10 36 34 20 2b 20 31 31 36 34 34 34 37 33 36 30 30  64 + 11644473600
f7b20 29 2a 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54  )*10000000;.  uT
f7b30 6d 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20  m.dwLowDateTime 
f7b40 3d 20 28 44 57 4f 52 44 29 28 74 36 34 20 26 20  = (DWORD)(t64 & 
f7b50 30 78 46 46 46 46 46 46 46 46 29 3b 0a 20 20 75  0xFFFFFFFF);.  u
f7b60 54 6d 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d  Tm.dwHighDateTim
f7b70 65 3d 20 28 44 57 4f 52 44 29 28 74 36 34 20 3e  e= (DWORD)(t64 >
f7b80 3e 20 33 32 29 3b 0a 20 20 46 69 6c 65 54 69 6d  > 32);.  FileTim
f7b90 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65  eToLocalFileTime
f7ba0 28 26 75 54 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46  (&uTm,&lTm);.  F
f7bb0 69 6c 65 54 69 6d 65 54 6f 53 79 73 74 65 6d 54  ileTimeToSystemT
f7bc0 69 6d 65 28 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a  ime(&lTm,&pTm);.
f7bd0 20 20 79 2e 74 6d 5f 79 65 61 72 20 3d 20 70 54    y.tm_year = pT
f7be0 6d 2e 77 59 65 61 72 20 2d 20 31 39 30 30 3b 0a  m.wYear - 1900;.
f7bf0 20 20 79 2e 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d    y.tm_mon = pTm
f7c00 2e 77 4d 6f 6e 74 68 20 2d 20 31 3b 0a 20 20 79  .wMonth - 1;.  y
f7c10 2e 74 6d 5f 77 64 61 79 20 3d 20 70 54 6d 2e 77  .tm_wday = pTm.w
f7c20 44 61 79 4f 66 57 65 65 6b 3b 0a 20 20 79 2e 74  DayOfWeek;.  y.t
f7c30 6d 5f 6d 64 61 79 20 3d 20 70 54 6d 2e 77 44 61  m_mday = pTm.wDa
f7c40 79 3b 0a 20 20 79 2e 74 6d 5f 68 6f 75 72 20 3d  y;.  y.tm_hour =
f7c50 20 70 54 6d 2e 77 48 6f 75 72 3b 0a 20 20 79 2e   pTm.wHour;.  y.
f7c60 74 6d 5f 6d 69 6e 20 3d 20 70 54 6d 2e 77 4d 69  tm_min = pTm.wMi
f7c70 6e 75 74 65 3b 0a 20 20 79 2e 74 6d 5f 73 65 63  nute;.  y.tm_sec
f7c80 20 3d 20 70 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a   = pTm.wSecond;.
f7c90 20 20 72 65 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a    return &y;.}..
f7ca0 2f 2a 20 54 68 69 73 20 77 69 6c 6c 20 6e 65 76  /* This will nev
f7cb0 65 72 20 62 65 20 63 61 6c 6c 65 64 2c 20 62 75  er be called, bu
f7cc0 74 20 64 65 66 69 6e 65 64 20 74 6f 20 6d 61 6b  t defined to mak
f7cd0 65 20 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 69  e the code compi
f7ce0 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 47 65  le */.#define Ge
f7cf0 74 54 65 6d 70 50 61 74 68 41 28 61 2c 62 29 0a  tTempPathA(a,b).
f7d00 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c  .#define LockFil
f7d10 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20  e(a,b,c,d,e)    
f7d20 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65     winceLockFile
f7d30 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29  (&a, b, c, d, e)
f7d40 0a 23 64 65 66 69 6e 65 20 55 6e 6c 6f 63 6b 46  .#define UnlockF
f7d50 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20  ile(a,b,c,d,e)  
f7d60 20 20 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69     winceUnlockFi
f7d70 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20  le(&a, b, c, d, 
f7d80 65 29 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46  e).#define LockF
f7d90 69 6c 65 45 78 28 61 2c 62 2c 63 2c 64 2c 65 2c  ileEx(a,b,c,d,e,
f7da0 66 29 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69  f)   winceLockFi
f7db0 6c 65 45 78 28 26 61 2c 20 62 2c 20 63 2c 20 64  leEx(&a, b, c, d
f7dc0 2c 20 65 2c 20 66 29 0a 0a 23 64 65 66 69 6e 65  , e, f)..#define
f7dd0 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49   HANDLE_TO_WINFI
f7de0 4c 45 28 61 29 20 28 77 69 6e 46 69 6c 65 2a 29  LE(a) (winFile*)
f7df0 26 28 28 63 68 61 72 2a 29 61 29 5b 2d 28 69 6e  &((char*)a)[-(in
f7e00 74 29 6f 66 66 73 65 74 6f 66 28 77 69 6e 46 69  t)offsetof(winFi
f7e10 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 41 63  le,h)]../*.** Ac
f7e20 71 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f 6e 20  quire a lock on 
f7e30 74 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a 2f 0a  the handle h.*/.
f7e40 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63  static void winc
f7e50 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 48 41  eMutexAcquire(HA
f7e60 4e 44 4c 45 20 68 29 7b 0a 20 20 20 44 57 4f 52  NDLE h){.   DWOR
f7e70 44 20 64 77 45 72 72 3b 0a 20 20 20 64 6f 20 7b  D dwErr;.   do {
f7e80 0a 20 20 20 20 20 64 77 45 72 72 20 3d 20 57 61  .     dwErr = Wa
f7e90 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63  itForSingleObjec
f7ea0 74 28 68 2c 20 49 4e 46 49 4e 49 54 45 29 3b 0a  t(h, INFINITE);.
f7eb0 20 20 20 7d 20 77 68 69 6c 65 20 28 64 77 45 72     } while (dwEr
f7ec0 72 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45 43 54  r != WAIT_OBJECT
f7ed0 5f 30 20 26 26 20 64 77 45 72 72 20 21 3d 20 57  _0 && dwErr != W
f7ee0 41 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29 3b 0a  AIT_ABANDONED);.
f7ef0 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  }./*.** Release 
f7f00 61 20 6c 6f 63 6b 20 61 63 71 75 69 72 65 64 20  a lock acquired 
f7f10 62 79 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71  by winceMutexAcq
f7f20 75 69 72 65 28 29 0a 2a 2f 0a 23 64 65 66 69 6e  uire().*/.#defin
f7f30 65 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65  e winceMutexRele
f7f40 61 73 65 28 68 29 20 52 65 6c 65 61 73 65 4d 75  ase(h) ReleaseMu
f7f50 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a 20 43 72  tex(h)../*.** Cr
f7f60 65 61 74 65 20 74 68 65 20 6d 75 74 65 78 20 61  eate the mutex a
f7f70 6e 64 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  nd shared memory
f7f80 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e   used for lockin
f7f90 67 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a  g in the file.**
f7fa0 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c   descriptor pFil
f7fb0 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c  e.*/.static BOOL
f7fc0 20 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b   winceCreateLock
f7fd0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
f7fe0 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 69 6c 65 20  lename, winFile 
f7ff0 2a 70 46 69 6c 65 29 7b 0a 20 20 57 43 48 41 52  *pFile){.  WCHAR
f8000 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 41 52 20   *zTok;.  WCHAR 
f8010 2a 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54 6f 55  *zName = utf8ToU
f8020 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65  nicode(zFilename
f8030 29 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e 69 74 20  );.  BOOL bInit 
f8040 3d 20 54 52 55 45 3b 0a 0a 20 20 2f 2a 20 49 6e  = TRUE;..  /* In
f8050 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63  itialize the loc
f8060 61 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f 0a 20  al lockdata */. 
f8070 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 70 46 69   ZeroMemory(&pFi
f8080 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 7a 65 6f  le->local, sizeo
f8090 66 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29  f(pFile->local))
f80a0 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20  ;..  /* Replace 
f80b0 74 68 65 20 62 61 63 6b 73 6c 61 73 68 65 73 20  the backslashes 
f80c0 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e 61 6d  from the filenam
f80d0 65 20 61 6e 64 20 6c 6f 77 65 72 63 61 73 65 20  e and lowercase 
f80e0 69 74 0a 20 20 2a 2a 20 74 6f 20 64 65 72 69 76  it.  ** to deriv
f80f0 65 20 61 20 6d 75 74 65 78 20 6e 61 6d 65 2e 20  e a mutex name. 
f8100 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 68 61 72  */.  zTok = Char
f8110 4c 6f 77 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20  LowerW(zName);. 
f8120 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f   for (;*zTok;zTo
f8130 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 20 28 2a 7a  k++){.    if (*z
f8140 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a 7a 54  Tok == '\\') *zT
f8150 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a 0a 20  ok = '_';.  }.. 
f8160 20 2f 2a 20 43 72 65 61 74 65 2f 6f 70 65 6e 20   /* Create/open 
f8170 74 68 65 20 6e 61 6d 65 64 20 6d 75 74 65 78 20  the named mutex 
f8180 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74  */.  pFile->hMut
f8190 65 78 20 3d 20 43 72 65 61 74 65 4d 75 74 65 78  ex = CreateMutex
f81a0 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a  W(NULL, FALSE, z
f81b0 4e 61 6d 65 29 3b 0a 20 20 69 66 20 28 21 70 46  Name);.  if (!pF
f81c0 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20  ile->hMutex){.  
f81d0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
f81e0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  no = GetLastErro
f81f0 72 28 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 4e  r();.    free(zN
f8200 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ame);.    return
f8210 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f   FALSE;.  }..  /
f8220 2a 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75  * Acquire the mu
f8230 74 65 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69  tex before conti
f8240 6e 75 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65  nuing */.  wince
f8250 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69  MutexAcquire(pFi
f8260 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 0a  le->hMutex);.  .
f8270 20 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 6e    /* Since the n
f8280 61 6d 65 73 20 6f 66 20 6e 61 6d 65 64 20 6d 75  ames of named mu
f8290 74 65 78 65 73 2c 20 73 65 6d 61 70 68 6f 72 65  texes, semaphore
f82a0 73 2c 20 66 69 6c 65 20 6d 61 70 70 69 6e 67 73  s, file mappings
f82b0 20 65 74 63 20 61 72 65 20 0a 20 20 2a 2a 20 63   etc are .  ** c
f82c0 61 73 65 2d 73 65 6e 73 69 74 69 76 65 2c 20 74  ase-sensitive, t
f82d0 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66  ake advantage of
f82e0 20 74 68 61 74 20 62 79 20 75 70 70 65 72 63 61   that by upperca
f82f0 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 20 6e  sing the mutex n
f8300 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 75 73 69  ame.  ** and usi
f8310 6e 67 20 74 68 61 74 20 61 73 20 74 68 65 20 73  ng that as the s
f8320 68 61 72 65 64 20 66 69 6c 65 6d 61 70 70 69 6e  hared filemappin
f8330 67 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43  g name..  */.  C
f8340 68 61 72 55 70 70 65 72 57 28 7a 4e 61 6d 65 29  harUpperW(zName)
f8350 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72  ;.  pFile->hShar
f8360 65 64 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d  ed = CreateFileM
f8370 61 70 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f  appingW(INVALID_
f8380 48 41 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55  HANDLE_VALUE, NU
f8390 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LL,.            
f83a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f83b0 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 5f             PAGE_
f83c0 52 45 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69  READWRITE, 0, si
f83d0 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c  zeof(winceLock),
f83e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
f83f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8400 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20          zName); 
f8410 20 0a 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c   ..  /* Set a fl
f8420 61 67 20 74 68 61 74 20 69 6e 64 69 63 61 74 65  ag that indicate
f8430 73 20 77 65 27 72 65 20 74 68 65 20 66 69 72 73  s we're the firs
f8440 74 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  t to create the 
f8450 6d 65 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20  memory so it .  
f8460 2a 2a 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d  ** must be zero-
f8470 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
f8480 20 69 66 20 28 47 65 74 4c 61 73 74 45 72 72 6f   if (GetLastErro
f8490 72 28 29 20 3d 3d 20 45 52 52 4f 52 5f 41 4c 52  r() == ERROR_ALR
f84a0 45 41 44 59 5f 45 58 49 53 54 53 29 7b 0a 20 20  EADY_EXISTS){.  
f84b0 20 20 62 49 6e 69 74 20 3d 20 46 41 4c 53 45 3b    bInit = FALSE;
f84c0 0a 20 20 7d 0a 0a 20 20 66 72 65 65 28 7a 4e 61  .  }..  free(zNa
f84d0 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65  me);..  /* If we
f84e0 20 73 75 63 63 65 65 64 65 64 20 69 6e 20 6d 61   succeeded in ma
f84f0 6b 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  king the shared 
f8500 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 2c 20 6d  memory handle, m
f8510 61 70 20 69 74 2e 20 2a 2f 0a 20 20 69 66 20 28  ap it. */.  if (
f8520 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 7b  pFile->hShared){
f8530 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72  .    pFile->shar
f8540 65 64 20 3d 20 28 77 69 6e 63 65 4c 6f 63 6b 2a  ed = (winceLock*
f8550 29 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70  )MapViewOfFile(p
f8560 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 2c 20 0a  File->hShared, .
f8570 20 20 20 20 20 20 20 20 20 20 20 20 20 46 49 4c               FIL
f8580 45 5f 4d 41 50 5f 52 45 41 44 7c 46 49 4c 45 5f  E_MAP_READ|FILE_
f8590 4d 41 50 5f 57 52 49 54 45 2c 20 30 2c 20 30 2c  MAP_WRITE, 0, 0,
f85a0 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63   sizeof(winceLoc
f85b0 6b 29 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 6d  k));.    /* If m
f85c0 61 70 70 69 6e 67 20 66 61 69 6c 65 64 2c 20 63  apping failed, c
f85d0 6c 6f 73 65 20 74 68 65 20 73 68 61 72 65 64 20  lose the shared 
f85e0 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 61 6e  memory handle an
f85f0 64 20 65 72 61 73 65 20 69 74 20 2a 2f 0a 20 20  d erase it */.  
f8600 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 73 68    if (!pFile->sh
f8610 61 72 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69  ared){.      pFi
f8620 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
f8630 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a  GetLastError();.
f8640 20 20 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c        CloseHandl
f8650 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64  e(pFile->hShared
f8660 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  );.      pFile->
f8670 68 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c 3b 0a  hShared = NULL;.
f8680 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
f8690 49 66 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  If shared memory
f86a0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 72   could not be cr
f86b0 65 61 74 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73  eated, then clos
f86c0 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e 64 20  e the mutex and 
f86d0 66 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28 70 46  fail */.  if (pF
f86e0 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 3d 20  ile->hShared == 
f86f0 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e 63 65  NULL){.    wince
f8700 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69  MutexRelease(pFi
f8710 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20  le->hMutex);.   
f8720 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69   CloseHandle(pFi
f8730 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20  le->hMutex);.   
f8740 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d   pFile->hMutex =
f8750 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72   NULL;.    retur
f8760 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 20 20 0a  n FALSE;.  }.  .
f8770 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
f8780 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72  the shared memor
f8790 79 20 69 66 20 77 65 27 72 65 20 73 75 70 70 6f  y if we're suppo
f87a0 73 65 64 20 74 6f 20 2a 2f 0a 20 20 69 66 20 28  sed to */.  if (
f87b0 62 49 6e 69 74 29 20 7b 0a 20 20 20 20 5a 65 72  bInit) {.    Zer
f87c0 6f 4d 65 6d 6f 72 79 28 70 46 69 6c 65 2d 3e 73  oMemory(pFile->s
f87d0 68 61 72 65 64 2c 20 73 69 7a 65 6f 66 28 77 69  hared, sizeof(wi
f87e0 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d 0a 0a  nceLock));.  }..
f87f0 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65    winceMutexRele
f8800 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  ase(pFile->hMute
f8810 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 52 55  x);.  return TRU
f8820 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74  E;.}../*.** Dest
f8830 72 6f 79 20 74 68 65 20 70 61 72 74 20 6f 66 20  roy the part of 
f8840 77 69 6e 46 69 6c 65 20 74 68 61 74 20 64 65 61  winFile that dea
f8850 6c 73 20 77 69 74 68 20 77 69 6e 63 65 20 6c 6f  ls with wince lo
f8860 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  cks.*/.static vo
f8870 69 64 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c  id winceDestroyL
f8880 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69  ock(winFile *pFi
f8890 6c 65 29 7b 0a 20 20 69 66 20 28 70 46 69 6c 65  le){.  if (pFile
f88a0 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 20 2f  ->hMutex){.    /
f88b0 2a 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75  * Acquire the mu
f88c0 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65  tex */.    wince
f88d0 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69  MutexAcquire(pFi
f88e0 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20  le->hMutex);..  
f88f0 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
f8900 6e 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75 6c 64  ng blocks should
f8910 20 70 72 6f 62 61 62 6c 79 20 61 73 73 65 72 74   probably assert
f8920 20 69 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20   in debug mode, 
f8930 62 75 74 20 74 68 65 79 0a 20 20 20 20 20 20 20  but they.       
f8940 61 72 65 20 74 6f 20 63 6c 65 61 6e 75 70 20 69  are to cleanup i
f8950 6e 20 63 61 73 65 20 61 6e 79 20 6c 6f 63 6b 73  n case any locks
f8960 20 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e 20 2a   remained open *
f8970 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  /.    if (pFile-
f8980 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 29  >local.nReaders)
f8990 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  {.      pFile->s
f89a0 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20  hared->nReaders 
f89b0 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  --;.    }.    if
f89c0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62   (pFile->local.b
f89d0 52 65 73 65 72 76 65 64 29 7b 0a 20 20 20 20 20  Reserved){.     
f89e0 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
f89f0 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53  bReserved = FALS
f8a00 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20  E;.    }.    if 
f8a10 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50  (pFile->local.bP
f8a20 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70  ending){.      p
f8a30 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50  File->shared->bP
f8a40 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a  ending = FALSE;.
f8a50 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46      }.    if (pF
f8a60 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c  ile->local.bExcl
f8a70 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20 70 46  usive){.      pF
f8a80 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78  ile->shared->bEx
f8a90 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b  clusive = FALSE;
f8aa0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
f8ab0 65 2d 72 65 66 65 72 65 6e 63 65 20 61 6e 64 20  e-reference and 
f8ac0 63 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79 20 6f  close our copy o
f8ad0 66 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d  f the shared mem
f8ae0 6f 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ory handle */.  
f8af0 20 20 55 6e 6d 61 70 56 69 65 77 4f 66 46 69 6c    UnmapViewOfFil
f8b00 65 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29  e(pFile->shared)
f8b10 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c  ;.    CloseHandl
f8b20 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64  e(pFile->hShared
f8b30 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 6e 65 20  );..    /* Done 
f8b40 77 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 2a  with the mutex *
f8b50 2f 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78  /.    winceMutex
f8b60 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68  Release(pFile->h
f8b70 4d 75 74 65 78 29 3b 20 20 20 20 0a 20 20 20 20  Mutex);    .    
f8b80 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c  CloseHandle(pFil
f8b90 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20  e->hMutex);.    
f8ba0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20  pFile->hMutex = 
f8bb0 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  NULL;.  }.}../* 
f8bc0 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74  .** An implement
f8bd0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63  ation of the Loc
f8be0 6b 46 69 6c 65 28 29 20 41 50 49 20 6f 66 20 77  kFile() API of w
f8bf0 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65  indows for wince
f8c00 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20  .*/.static BOOL 
f8c10 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 0a 20  winceLockFile(. 
f8c20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c   HANDLE *phFile,
f8c30 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f  .  DWORD dwFileO
f8c40 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52  ffsetLow,.  DWOR
f8c50 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69  D dwFileOffsetHi
f8c60 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d  gh,.  DWORD nNum
f8c70 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b  berOfBytesToLock
f8c80 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75  Low,.  DWORD nNu
f8c90 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63  mberOfBytesToLoc
f8ca0 6b 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69  kHigh.){.  winFi
f8cb0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44  le *pFile = HAND
f8cc0 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68  LE_TO_WINFILE(ph
f8cd0 46 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52  File);.  BOOL bR
f8ce0 65 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a  eturn = FALSE;..
f8cf0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
f8d00 45 52 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48  ER(dwFileOffsetH
f8d10 69 67 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  igh);.  UNUSED_P
f8d20 41 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72  ARAMETER(nNumber
f8d30 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67  OfBytesToLockHig
f8d40 68 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c  h);..  if (!pFil
f8d50 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72  e->hMutex) retur
f8d60 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d  n TRUE;.  winceM
f8d70 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
f8d80 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f  e->hMutex);..  /
f8d90 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63  * Wanting an exc
f8da0 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a  lusive lock? */.
f8db0 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73    if (dwFileOffs
f8dc0 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29  etLow == (DWORD)
f8dd0 53 48 41 52 45 44 5f 46 49 52 53 54 0a 20 20 20  SHARED_FIRST.   
f8de0 20 20 20 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66      && nNumberOf
f8df0 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d  BytesToLockLow =
f8e00 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f  = (DWORD)SHARED_
f8e10 53 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 28 70  SIZE){.    if (p
f8e20 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52  File->shared->nR
f8e30 65 61 64 65 72 73 20 3d 3d 20 30 20 26 26 20 70  eaders == 0 && p
f8e40 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45  File->shared->bE
f8e50 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a  xclusive == 0){.
f8e60 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68         pFile->sh
f8e70 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65  ared->bExclusive
f8e80 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 20   = TRUE;.       
f8e90 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78  pFile->local.bEx
f8ea0 63 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a  clusive = TRUE;.
f8eb0 20 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d         bReturn =
f8ec0 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d   TRUE;.    }.  }
f8ed0 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65  ..  /* Want a re
f8ee0 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 2a 2f  ad-only lock? */
f8ef0 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69  .  else if (dwFi
f8f00 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28  leOffsetLow == (
f8f10 44 57 4f 52 44 29 53 48 41 52 45 44 5f 46 49 52  DWORD)SHARED_FIR
f8f20 53 54 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  ST &&.          
f8f30 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
f8f40 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a  oLockLow == 1){.
f8f50 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73      if (pFile->s
f8f60 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76  hared->bExclusiv
f8f70 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20 20 20 70  e == 0){.      p
f8f80 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61  File->local.nRea
f8f90 64 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20 69  ders ++;.      i
f8fa0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
f8fb0 6e 52 65 61 64 65 72 73 20 3d 3d 20 31 29 7b 0a  nReaders == 1){.
f8fc0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73          pFile->s
f8fd0 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20  hared->nReaders 
f8fe0 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ++;.      }.    
f8ff0 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45    bReturn = TRUE
f9000 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
f9010 2a 20 57 61 6e 74 20 61 20 70 65 6e 64 69 6e 67  * Want a pending
f9020 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65   lock? */.  else
f9030 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65   if (dwFileOffse
f9040 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 50  tLow == (DWORD)P
f9050 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 6e  ENDING_BYTE && n
f9060 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
f9070 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20  ockLow == 1){.  
f9080 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65 6e 64 69    /* If no pendi
f9090 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e  ng lock has been
f90a0 20 61 63 71 75 69 72 65 64 2c 20 74 68 65 6e 20   acquired, then 
f90b0 61 63 71 75 69 72 65 20 69 74 20 2a 2f 0a 20 20  acquire it */.  
f90c0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61    if (pFile->sha
f90d0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 3d  red->bPending ==
f90e0 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c   0) {.      pFil
f90f0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64  e->shared->bPend
f9100 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  ing = TRUE;.    
f9110 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62    pFile->local.b
f9120 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a  Pending = TRUE;.
f9130 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
f9140 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
f9150 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 73  .  /* Want a res
f9160 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f 0a 20  erved lock? */. 
f9170 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65   else if (dwFile
f9180 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57  OffsetLow == (DW
f9190 4f 52 44 29 52 45 53 45 52 56 45 44 5f 42 59 54  ORD)RESERVED_BYT
f91a0 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79  E && nNumberOfBy
f91b0 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20  tesToLockLow == 
f91c0 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c  1){.    if (pFil
f91d0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65  e->shared->bRese
f91e0 72 76 65 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20  rved == 0) {.   
f91f0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
f9200 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52  ->bReserved = TR
f9210 55 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  UE;.      pFile-
f9220 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64  >local.bReserved
f9230 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 62   = TRUE;.      b
f9240 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20  Return = TRUE;. 
f9250 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63     }.  }..  winc
f9260 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46  eMutexRelease(pF
f9270 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20  ile->hMutex);.  
f9280 72 65 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a  return bReturn;.
f9290 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c  }../*.** An impl
f92a0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
f92b0 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49  e UnlockFile API
f92c0 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20   of windows for 
f92d0 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20  wince.*/.static 
f92e0 42 4f 4f 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b  BOOL winceUnlock
f92f0 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a  File(.  HANDLE *
f9300 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20  phFile,.  DWORD 
f9310 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c  dwFileOffsetLow,
f9320 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f  .  DWORD dwFileO
f9330 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f  ffsetHigh,.  DWO
f9340 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  RD nNumberOfByte
f9350 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20  sToUnlockLow,.  
f9360 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42  DWORD nNumberOfB
f9370 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68  ytesToUnlockHigh
f9380 0a 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  .){.  winFile *p
f9390 46 69 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f  File = HANDLE_TO
f93a0 5f 57 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29  _WINFILE(phFile)
f93b0 3b 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e  ;.  BOOL bReturn
f93c0 20 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 55 4e 55   = FALSE;..  UNU
f93d0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 77  SED_PARAMETER(dw
f93e0 46 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29 3b  FileOffsetHigh);
f93f0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
f9400 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74  TER(nNumberOfByt
f9410 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 29 3b  esToUnlockHigh);
f9420 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e  ..  if (!pFile->
f9430 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e 20 54  hMutex) return T
f9440 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65  RUE;.  winceMute
f9450 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e  xAcquire(pFile->
f9460 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 52  hMutex);..  /* R
f9470 65 6c 65 61 73 69 6e 67 20 61 20 72 65 61 64 65  eleasing a reade
f9480 72 20 6c 6f 63 6b 20 6f 72 20 61 6e 20 65 78 63  r lock or an exc
f9490 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20  lusive lock */. 
f94a0 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65   if (dwFileOffse
f94b0 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53  tLow == (DWORD)S
f94c0 48 41 52 45 44 5f 46 49 52 53 54 29 7b 0a 20 20  HARED_FIRST){.  
f94d0 20 20 2f 2a 20 44 69 64 20 77 65 20 68 61 76 65    /* Did we have
f94e0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
f94f0 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 66 20 28 70  ck? */.    if (p
f9500 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63  File->local.bExc
f9510 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20 61  lusive){.      a
f9520 73 73 65 72 74 28 6e 4e 75 6d 62 65 72 4f 66 42  ssert(nNumberOfB
f9530 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20  ytesToUnlockLow 
f9540 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44  == (DWORD)SHARED
f9550 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 70 46  _SIZE);.      pF
f9560 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c  ile->local.bExcl
f9570 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20  usive = FALSE;. 
f9580 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
f9590 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d  ed->bExclusive =
f95a0 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52   FALSE;.      bR
f95b0 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20  eturn = TRUE;.  
f95c0 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 64 20    }..    /* Did 
f95d0 77 65 20 6a 75 73 74 20 68 61 76 65 20 61 20 72  we just have a r
f95e0 65 61 64 65 72 20 6c 6f 63 6b 3f 20 2a 2f 0a 20  eader lock? */. 
f95f0 20 20 20 65 6c 73 65 20 69 66 20 28 70 46 69 6c     else if (pFil
f9600 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72  e->local.nReader
f9610 73 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  s){.      assert
f9620 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54  (nNumberOfBytesT
f9630 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44  oUnlockLow == (D
f9640 57 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45  WORD)SHARED_SIZE
f9650 20 7c 7c 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74   || nNumberOfByt
f9660 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d  esToUnlockLow ==
f9670 20 31 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65   1);.      pFile
f9680 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73  ->local.nReaders
f9690 20 2d 2d 3b 0a 20 20 20 20 20 20 69 66 20 28 70   --;.      if (p
f96a0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61  File->local.nRea
f96b0 64 65 72 73 20 3d 3d 20 30 29 0a 20 20 20 20 20  ders == 0).     
f96c0 20 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65   {.        pFile
f96d0 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65  ->shared->nReade
f96e0 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20  rs --;.      }. 
f96f0 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54       bReturn = T
f9700 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  RUE;.    }.  }..
f9710 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61    /* Releasing a
f9720 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 2a 2f   pending lock */
f9730 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69  .  else if (dwFi
f9740 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28  leOffsetLow == (
f9750 44 57 4f 52 44 29 50 45 4e 44 49 4e 47 5f 42 59  DWORD)PENDING_BY
f9760 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42  TE && nNumberOfB
f9770 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20  ytesToUnlockLow 
f9780 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70  == 1){.    if (p
f9790 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e  File->local.bPen
f97a0 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69  ding){.      pFi
f97b0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69  le->local.bPendi
f97c0 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ng = FALSE;.    
f97d0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f97e0 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53  >bPending = FALS
f97f0 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
f9800 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
f9810 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e   }.  /* Releasin
f9820 67 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63  g a reserved loc
f9830 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  k */.  else if (
f9840 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
f9850 3d 3d 20 28 44 57 4f 52 44 29 52 45 53 45 52 56  == (DWORD)RESERV
f9860 45 44 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62  ED_BYTE && nNumb
f9870 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63  erOfBytesToUnloc
f9880 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20  kLow == 1){.    
f9890 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
f98a0 2e 62 52 65 73 65 72 76 65 64 29 20 7b 0a 20 20  .bReserved) {.  
f98b0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c      pFile->local
f98c0 2e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c  .bReserved = FAL
f98d0 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  SE;.      pFile-
f98e0 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76  >shared->bReserv
f98f0 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ed = FALSE;.    
f9900 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45    bReturn = TRUE
f9910 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77  ;.    }.  }..  w
f9920 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65  inceMutexRelease
f9930 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
f9940 0a 20 20 72 65 74 75 72 6e 20 62 52 65 74 75 72  .  return bRetur
f9950 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  n;.}../*.** An i
f9960 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
f9970 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28   the LockFileEx(
f9980 29 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73  ) API of windows
f9990 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74   for wince.*/.st
f99a0 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c  atic BOOL winceL
f99b0 6f 63 6b 46 69 6c 65 45 78 28 0a 20 20 48 41 4e  ockFileEx(.  HAN
f99c0 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44  DLE *phFile,.  D
f99d0 57 4f 52 44 20 64 77 46 6c 61 67 73 2c 0a 20 20  WORD dwFlags,.  
f99e0 44 57 4f 52 44 20 64 77 52 65 73 65 72 76 65 64  DWORD dwReserved
f99f0 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65  ,.  DWORD nNumbe
f9a00 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f  rOfBytesToLockLo
f9a10 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62  w,.  DWORD nNumb
f9a20 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48  erOfBytesToLockH
f9a30 69 67 68 2c 0a 20 20 4c 50 4f 56 45 52 4c 41 50  igh,.  LPOVERLAP
f9a40 50 45 44 20 6c 70 4f 76 65 72 6c 61 70 70 65 64  PED lpOverlapped
f9a50 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  .){.  UNUSED_PAR
f9a60 41 4d 45 54 45 52 28 64 77 52 65 73 65 72 76 65  AMETER(dwReserve
f9a70 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d);.  UNUSED_PAR
f9a80 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66  AMETER(nNumberOf
f9a90 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 29  BytesToLockHigh)
f9aa0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63  ;..  /* If the c
f9ab0 61 6c 6c 65 72 20 77 61 6e 74 73 20 61 20 73 68  aller wants a sh
f9ac0 61 72 65 64 20 72 65 61 64 20 6c 6f 63 6b 2c 20  ared read lock, 
f9ad0 66 6f 72 77 61 72 64 20 74 68 69 73 20 63 61 6c  forward this cal
f9ae0 6c 0a 20 20 2a 2a 20 74 6f 20 77 69 6e 63 65 4c  l.  ** to winceL
f9af0 6f 63 6b 46 69 6c 65 20 2a 2f 0a 20 20 69 66 20  ockFile */.  if 
f9b00 28 6c 70 4f 76 65 72 6c 61 70 70 65 64 2d 3e 4f  (lpOverlapped->O
f9b10 66 66 73 65 74 20 3d 3d 20 28 44 57 4f 52 44 29  ffset == (DWORD)
f9b20 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a  SHARED_FIRST &&.
f9b30 20 20 20 20 20 20 64 77 46 6c 61 67 73 20 3d 3d        dwFlags ==
f9b40 20 31 20 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d   1 &&.      nNum
f9b50 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b  berOfBytesToLock
f9b60 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48  Low == (DWORD)SH
f9b70 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20  ARED_SIZE){.    
f9b80 72 65 74 75 72 6e 20 77 69 6e 63 65 4c 6f 63 6b  return winceLock
f9b90 46 69 6c 65 28 70 68 46 69 6c 65 2c 20 53 48 41  File(phFile, SHA
f9ba0 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 31 2c  RED_FIRST, 0, 1,
f9bb0 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   0);.  }.  retur
f9bc0 6e 20 46 41 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a  n FALSE;.}./*.**
f9bd0 20 45 6e 64 20 6f 66 20 74 68 65 20 73 70 65 63   End of the spec
f9be0 69 61 6c 20 63 6f 64 65 20 66 6f 72 20 77 69 6e  ial code for win
f9bf0 63 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ce.*************
f9c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9c40 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  /.#endif /* SQLI
f9c50 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a  TE_OS_WINCE */..
f9c60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
f9c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
f9cb0 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70  * The next group
f9cc0 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 69 6d 70   of routines imp
f9cd0 6c 65 6d 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d  lement the I/O m
f9ce0 65 74 68 6f 64 73 20 73 70 65 63 69 66 69 65 64  ethods specified
f9cf0 0a 2a 2a 20 62 79 20 74 68 65 20 73 71 6c 69 74  .** by the sqlit
f9d00 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
f9d10 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ject..**********
f9d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9d60 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  ****/../*.** Clo
f9d70 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  se a file..**.**
f9d80 20 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20   It is reported 
f9d90 74 68 61 74 20 61 6e 20 61 74 74 65 6d 70 74 20  that an attempt 
f9da0 74 6f 20 63 6c 6f 73 65 20 61 20 68 61 6e 64 6c  to close a handl
f9db0 65 20 6d 69 67 68 74 20 73 6f 6d 65 74 69 6d 65  e might sometime
f9dc0 73 0a 2a 2a 20 66 61 69 6c 2e 20 20 54 68 69 73  s.** fail.  This
f9dd0 20 69 73 20 61 20 76 65 72 79 20 75 6e 72 65 61   is a very unrea
f9de0 73 6f 6e 61 62 6c 65 20 72 65 73 75 6c 74 2c 20  sonable result, 
f9df0 62 75 74 20 77 69 6e 64 6f 77 73 20 69 73 20 6e  but windows is n
f9e00 6f 74 6f 72 69 6f 75 73 0a 2a 2a 20 66 6f 72 20  otorious.** for 
f9e10 62 65 69 6e 67 20 75 6e 72 65 61 73 6f 6e 61 62  being unreasonab
f9e20 6c 65 20 73 6f 20 49 20 64 6f 20 6e 6f 74 20 64  le so I do not d
f9e30 6f 75 62 74 20 74 68 61 74 20 69 74 20 6d 69 67  oubt that it mig
f9e40 68 74 20 68 61 70 70 65 6e 2e 20 20 49 66 0a 2a  ht happen.  If.*
f9e50 2a 20 74 68 65 20 63 6c 6f 73 65 20 66 61 69 6c  * the close fail
f9e60 73 2c 20 77 65 20 70 61 75 73 65 20 66 6f 72 20  s, we pause for 
f9e70 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73  100 milliseconds
f9e80 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 20   and try again. 
f9e90 20 41 73 0a 2a 2a 20 6d 61 6e 79 20 61 73 20 4d   As.** many as M
f9ea0 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20  X_CLOSE_ATTEMPT 
f9eb0 61 74 74 65 6d 70 74 73 20 74 6f 20 63 6c 6f 73  attempts to clos
f9ec0 65 20 74 68 65 20 68 61 6e 64 6c 65 20 61 72 65  e the handle are
f9ed0 20 6d 61 64 65 20 62 65 66 6f 72 65 0a 2a 2a 20   made before.** 
f9ee0 67 69 76 69 6e 67 20 75 70 20 61 6e 64 20 72 65  giving up and re
f9ef0 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72  turning an error
f9f00 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f  ..*/.#define MX_
f9f10 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 33 0a  CLOSE_ATTEMPT 3.
f9f20 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 6c  static int winCl
f9f30 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
f9f40 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 2c   *id){.  int rc,
f9f50 20 63 6e 74 20 3d 20 30 3b 0a 20 20 77 69 6e 46   cnt = 0;.  winF
f9f60 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
f9f70 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73  nFile*)id;..  as
f9f80 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
f9f90 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45   OSTRACE2("CLOSE
f9fa0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
f9fb0 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 72 63 20  );.  do{.    rc 
f9fc0 3d 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46  = CloseHandle(pF
f9fd0 69 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77 68 69 6c  ile->h);.  }whil
f9fe0 65 28 20 72 63 3d 3d 30 20 26 26 20 2b 2b 63 6e  e( rc==0 && ++cn
f9ff0 74 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54  t < MX_CLOSE_ATT
fa000 45 4d 50 54 20 26 26 20 28 53 6c 65 65 70 28 31  EMPT && (Sleep(1
fa010 30 30 29 2c 20 31 29 20 29 3b 0a 23 69 66 20 53  00), 1) );.#if S
fa020 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23  QLITE_OS_WINCE.#
fa030 64 65 66 69 6e 65 20 57 49 4e 43 45 5f 44 45 4c  define WINCE_DEL
fa040 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 33  ETION_ATTEMPTS 3
fa050 0a 20 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c  .  winceDestroyL
fa060 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 69 66  ock(pFile);.  if
fa070 28 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65  ( pFile->zDelete
fa080 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20 20 69  OnClose ){.    i
fa090 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20  nt cnt = 0;.    
fa0a0 77 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20 20  while(.         
fa0b0 20 20 44 65 6c 65 74 65 46 69 6c 65 57 28 70 46    DeleteFileW(pF
fa0c0 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c  ile->zDeleteOnCl
fa0d0 6f 73 65 29 3d 3d 30 0a 20 20 20 20 20 20 20 20  ose)==0.        
fa0e0 26 26 20 47 65 74 46 69 6c 65 41 74 74 72 69 62  && GetFileAttrib
fa0f0 75 74 65 73 57 28 70 46 69 6c 65 2d 3e 7a 44 65  utesW(pFile->zDe
fa100 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 21 3d 30 78  leteOnClose)!=0x
fa110 66 66 66 66 66 66 66 66 20 0a 20 20 20 20 20 20  ffffffff .      
fa120 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 57 49 4e    && cnt++ < WIN
fa130 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45  CE_DELETION_ATTE
fa140 4d 50 54 53 0a 20 20 20 20 29 7b 0a 20 20 20 20  MPTS.    ){.    
fa150 20 20 20 53 6c 65 65 70 28 31 30 30 29 3b 20 20     Sleep(100);  
fa160 2f 2a 20 57 61 69 74 20 61 20 6c 69 74 74 6c 65  /* Wait a little
fa170 20 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20 61   before trying a
fa180 67 61 69 6e 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  gain */.    }.  
fa190 20 20 66 72 65 65 28 70 46 69 6c 65 2d 3e 7a 44    free(pFile->zD
fa1a0 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b 0a 20  eleteOnClose);. 
fa1b0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 70 65 6e   }.#endif.  Open
fa1c0 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 72  Counter(-1);.  r
fa1d0 65 74 75 72 6e 20 72 63 20 3f 20 53 51 4c 49 54  eturn rc ? SQLIT
fa1e0 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f  E_OK : SQLITE_IO
fa1f0 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f  ERR;.}../*.** So
fa200 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f 6d  me microsoft com
fa210 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 73  pilers lack this
fa220 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a   definition..*/.
fa230 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 5f  #ifndef INVALID_
fa240 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52  SET_FILE_POINTER
fa250 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49  .# define INVALI
fa260 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54  D_SET_FILE_POINT
fa270 45 52 20 28 28 44 57 4f 52 44 29 2d 31 29 0a 23  ER ((DWORD)-1).#
fa280 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  endif../*.** Rea
fa290 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69  d data from a fi
fa2a0 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72  le into a buffer
fa2b0 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
fa2c0 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79  _OK if all.** by
fa2d0 74 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75  tes were read su
fa2e0 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53  ccessfully and S
fa2f0 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61  QLITE_IOERR if a
fa300 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20  nything goes.** 
fa310 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  wrong..*/.static
fa320 20 69 6e 74 20 77 69 6e 52 65 61 64 28 0a 20 20   int winRead(.  
fa330 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
fa340 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ,          /* Fi
fa350 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20  le to read from 
fa360 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c  */.  void *pBuf,
fa370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fa380 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74  /* Write content
fa390 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65   into this buffe
fa3a0 72 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20  r */.  int amt, 
fa3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fa3c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
fa3d0 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a  ytes to read */.
fa3e0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
fa3f0 6f 66 66 73 65 74 20 20 20 20 20 20 20 2f 2a 20  offset       /* 
fa400 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74  Begin reading at
fa410 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a   this offset */.
fa420 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42  ){.  LONG upperB
fa430 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66  its = (LONG)((of
fa440 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66  fset>>32) & 0x7f
fa450 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20  ffffff);.  LONG 
fa460 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e  lowerBits = (LON
fa470 47 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 66  G)(offset & 0xff
fa480 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f 52 44  ffffff);.  DWORD
fa490 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a   rc;.  winFile *
fa4a0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
fa4b0 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72  *)id;.  DWORD er
fa4c0 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20 67 6f 74  ror;.  DWORD got
fa4d0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21  ;..  assert( id!
fa4e0 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  =0 );.  Simulate
fa4f0 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53  IOError(return S
fa500 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
fa510 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 52  );.  OSTRACE3("R
fa520 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e  EAD %d lock=%d\n
fa530 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69  ", pFile->h, pFi
fa540 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20  le->locktype);. 
fa550 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69   rc = SetFilePoi
fa560 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c  nter(pFile->h, l
fa570 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 72  owerBits, &upper
fa580 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e  Bits, FILE_BEGIN
fa590 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56  );.  if( rc==INV
fa5a0 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f  ALID_SET_FILE_PO
fa5b0 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 3d  INTER && (error=
fa5c0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21  GetLastError())!
fa5d0 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20  =NO_ERROR ){.   
fa5e0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
fa5f0 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72  o = error;.    r
fa600 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c  eturn SQLITE_FUL
fa610 4c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 52 65  L;.  }.  if( !Re
fa620 61 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  adFile(pFile->h,
fa630 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74   pBuf, amt, &got
fa640 2c 20 30 29 20 29 7b 0a 20 20 20 20 70 46 69 6c  , 0) ){.    pFil
fa650 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47  e->lastErrno = G
fa660 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
fa670 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fa680 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d  _IOERR_READ;.  }
fa690 0a 20 20 69 66 28 20 67 6f 74 3d 3d 28 44 57 4f  .  if( got==(DWO
fa6a0 52 44 29 61 6d 74 20 29 7b 0a 20 20 20 20 72 65  RD)amt ){.    re
fa6b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
fa6c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
fa6d0 55 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20  Unread parts of 
fa6e0 74 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20  the buffer must 
fa6f0 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a  be zero-filled *
fa700 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28  /.    memset(&((
fa710 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d  char*)pBuf)[got]
fa720 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20  , 0, amt-got);. 
fa730 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fa740 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
fa750 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  D;.  }.}../*.** 
fa760 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20  Write data from 
fa770 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20  a buffer into a 
fa780 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51  file.  Return SQ
fa790 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
fa7a0 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74  ss.** or some ot
fa7b0 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f  her error code o
fa7c0 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74  n failure..*/.st
fa7d0 61 74 69 63 20 69 6e 74 20 77 69 6e 57 72 69 74  atic int winWrit
fa7e0 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  e(.  sqlite3_fil
fa7f0 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f  e *id,         /
fa800 2a 20 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20  * File to write 
fa810 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  into */.  const 
fa820 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20  void *pBuf,     
fa830 20 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65 73      /* The bytes
fa840 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 2a   to be written *
fa850 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20  /.  int amt,    
fa860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
fa870 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
fa880 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73   to write */.  s
fa890 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66  qlite3_int64 off
fa8a0 73 65 74 20 20 20 20 20 20 2f 2a 20 4f 66 66 73  set      /* Offs
fa8b0 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65  et into the file
fa8c0 20 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e   to begin writin
fa8d0 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e  g at */.){.  LON
fa8e0 47 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c  G upperBits = (L
fa8f0 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32  ONG)((offset>>32
fa900 29 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b  ) & 0x7fffffff);
fa910 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74  .  LONG lowerBit
fa920 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 73 65  s = (LONG)(offse
fa930 74 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b  t & 0xffffffff);
fa940 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77  .  DWORD rc;.  w
fa950 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  inFile *pFile = 
fa960 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (winFile*)id;.  
fa970 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20 20 44  DWORD error;.  D
fa980 57 4f 52 44 20 77 72 6f 74 65 20 3d 20 30 3b 0a  WORD wrote = 0;.
fa990 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
fa9a0 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   );.  SimulateIO
fa9b0 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c  Error(return SQL
fa9c0 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 29  ITE_IOERR_WRITE)
fa9d0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  ;.  SimulateDisk
fa9e0 66 75 6c 6c 45 72 72 6f 72 28 72 65 74 75 72 6e  fullError(return
fa9f0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 3b 0a 20   SQLITE_FULL);. 
faa00 20 4f 53 54 52 41 43 45 33 28 22 57 52 49 54 45   OSTRACE3("WRITE
faa10 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20   %d lock=%d\n", 
faa20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d  pFile->h, pFile-
faa30 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63  >locktype);.  rc
faa40 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65   = SetFilePointe
faa50 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65  r(pFile->h, lowe
faa60 72 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74  rBits, &upperBit
faa70 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a  s, FILE_BEGIN);.
faa80 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49    if( rc==INVALI
faa90 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54  D_SET_FILE_POINT
faaa0 45 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74  ER && (error=Get
faab0 4c 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f  LastError())!=NO
faac0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46  _ERROR ){.    pF
faad0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
faae0 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75   error;.    retu
faaf0 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a  rn SQLITE_FULL;.
fab00 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d    }.  assert( am
fab10 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 0a  t>0 );.  while(.
fab20 20 20 20 20 20 61 6d 74 3e 30 0a 20 20 20 20 20       amt>0.     
fab30 26 26 20 28 72 63 20 3d 20 57 72 69 74 65 46 69  && (rc = WriteFi
fab40 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75  le(pFile->h, pBu
fab50 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 2c 20  f, amt, &wrote, 
fab60 30 29 29 21 3d 30 0a 20 20 20 20 20 26 26 20 77  0))!=0.     && w
fab70 72 6f 74 65 3e 30 0a 20 20 29 7b 0a 20 20 20 20  rote>0.  ){.    
fab80 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20  amt -= wrote;.  
fab90 20 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72    pBuf = &((char
faba0 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a  *)pBuf)[wrote];.
fabb0 20 20 7d 0a 20 20 69 66 28 20 21 72 63 20 7c 7c    }.  if( !rc ||
fabc0 20 61 6d 74 3e 28 69 6e 74 29 77 72 6f 74 65 20   amt>(int)wrote 
fabd0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  ){.    pFile->la
fabe0 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73  stErrno = GetLas
fabf0 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65  tError();.    re
fac00 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
fac10 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
fac20 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
fac30 2a 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f  ** Truncate an o
fac40 70 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70  pen file to a sp
fac50 65 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a  ecified size.*/.
fac60 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 54 72  static int winTr
fac70 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66  uncate(sqlite3_f
fac80 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33  ile *id, sqlite3
fac90 5f 69 6e 74 36 34 20 6e 42 79 74 65 29 7b 0a 20  _int64 nByte){. 
faca0 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20   LONG upperBits 
facb0 3d 20 28 4c 4f 4e 47 29 28 28 6e 42 79 74 65 3e  = (LONG)((nByte>
facc0 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66  >32) & 0x7ffffff
facd0 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72  f);.  LONG lower
face0 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6e 42  Bits = (LONG)(nB
facf0 79 74 65 20 26 20 30 78 66 66 66 66 66 66 66 66  yte & 0xffffffff
fad00 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20  );.  DWORD rc;. 
fad10 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
fad20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
fad30 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a    DWORD error;..
fad40 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20    assert( id!=0 
fad50 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54  );.  OSTRACE3("T
fad60 52 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 5c  RUNCATE %d %lld\
fad70 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42  n", pFile->h, nB
fad80 79 74 65 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  yte);.  Simulate
fad90 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53  IOError(return S
fada0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e  QLITE_IOERR_TRUN
fadb0 43 41 54 45 29 3b 0a 20 20 72 63 20 3d 20 53 65  CATE);.  rc = Se
fadc0 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69  tFilePointer(pFi
fadd0 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73  le->h, lowerBits
fade0 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49  , &upperBits, FI
fadf0 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28  LE_BEGIN);.  if(
fae00 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54   rc==INVALID_SET
fae10 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26  _FILE_POINTER &&
fae20 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45   (error=GetLastE
fae30 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f  rror())!=NO_ERRO
fae40 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  R ){.    pFile->
fae50 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f  lastErrno = erro
fae60 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  r;.    return SQ
fae70 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
fae80 41 54 45 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 65  ATE;.  }.  /* Se
fae90 74 45 6e 64 4f 66 46 69 6c 65 20 77 69 6c 6c 20  tEndOfFile will 
faea0 66 61 69 6c 20 69 66 20 6e 42 79 74 65 20 69 73  fail if nByte is
faeb0 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 69   negative */.  i
faec0 66 28 20 21 53 65 74 45 6e 64 4f 66 46 69 6c 65  f( !SetEndOfFile
faed0 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20  (pFile->h) ){.  
faee0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
faef0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  no = GetLastErro
faf00 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  r();.    return 
faf10 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55  SQLITE_IOERR_TRU
faf20 4e 43 41 54 45 3b 0a 20 20 7d 0a 20 20 72 65 74  NCATE;.  }.  ret
faf30 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
faf40 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
faf50 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74  TEST./*.** Count
faf60 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66   the number of f
faf70 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72  ullsyncs and nor
faf80 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73  mal syncs.  This
faf90 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74   is used to test
fafa0 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61  .** that syncs a
fafb0 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65  nd fullsyncs are
fafc0 20 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65   occuring at the
fafd0 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f   right times..*/
fafe0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
faff0 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75  sqlite3_sync_cou
fb000 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41  nt = 0;.SQLITE_A
fb010 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
fb020 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20  ullsync_count = 
fb030 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
fb040 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77   Make sure all w
fb050 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69  rites to a parti
fb060 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63  cular file are c
fb070 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b  ommitted to disk
fb080 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
fb090 77 69 6e 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  winSync(sqlite3_
fb0a0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c  file *id, int fl
fb0b0 61 67 73 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  ags){.#ifndef SQ
fb0c0 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 77  LITE_NO_SYNC.  w
fb0d0 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  inFile *pFile = 
fb0e0 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20  (winFile*)id;.. 
fb0f0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
fb100 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 53 59  ;.  OSTRACE3("SY
fb110 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22  NC %d lock=%d\n"
fb120 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c  , pFile->h, pFil
fb130 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 23 65  e->locktype);.#e
fb140 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  lse.  UNUSED_PAR
fb150 41 4d 45 54 45 52 28 69 64 29 3b 0a 23 65 6e 64  AMETER(id);.#end
fb160 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
fb170 45 5f 54 45 53 54 0a 20 20 55 4e 55 53 45 44 5f  E_TEST.  UNUSED_
fb180 50 41 52 41 4d 45 54 45 52 28 66 6c 61 67 73 29  PARAMETER(flags)
fb190 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 66 6c  ;.#else.  if( fl
fb1a0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e  ags & SQLITE_SYN
fb1b0 43 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71  C_FULL ){.    sq
fb1c0 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63  lite3_fullsync_c
fb1d0 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71  ount++;.  }.  sq
fb1e0 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74  lite3_sync_count
fb1f0 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20  ++;.#endif.  /* 
fb200 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77  If we compiled w
fb210 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e  ith the SQLITE_N
fb220 4f 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65  O_SYNC flag, the
fb230 6e 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20  n syncing is a. 
fb240 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23   ** no-op.  */.#
fb250 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f  ifdef SQLITE_NO_
fb260 53 59 4e 43 0a 20 20 20 20 72 65 74 75 72 6e 20  SYNC.    return 
fb270 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65  SQLITE_OK;.#else
fb280 0a 20 20 69 66 28 20 46 6c 75 73 68 46 69 6c 65  .  if( FlushFile
fb290 42 75 66 66 65 72 73 28 70 46 69 6c 65 2d 3e 68  Buffers(pFile->h
fb2a0 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
fb2b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
fb2c0 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  se{.    pFile->l
fb2d0 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61  astErrno = GetLa
fb2e0 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72  stError();.    r
fb2f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
fb300 52 52 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  RR;.  }.#endif.}
fb310 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e  ../*.** Determin
fb320 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69  e the current si
fb330 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20  ze of a file in 
fb340 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20  bytes.*/.static 
fb350 69 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28  int winFileSize(
fb360 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
fb370 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
fb380 2a 70 53 69 7a 65 29 7b 0a 20 20 44 57 4f 52 44  *pSize){.  DWORD
fb390 20 75 70 70 65 72 42 69 74 73 3b 0a 20 20 44 57   upperBits;.  DW
fb3a0 4f 52 44 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20  ORD lowerBits;. 
fb3b0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
fb3c0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
fb3d0 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a    DWORD error;..
fb3e0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20    assert( id!=0 
fb3f0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
fb400 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49  rror(return SQLI
fb410 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29 3b  TE_IOERR_FSTAT);
fb420 0a 20 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 47  .  lowerBits = G
fb430 65 74 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65  etFileSize(pFile
fb440 2d 3e 68 2c 20 26 75 70 70 65 72 42 69 74 73 29  ->h, &upperBits)
fb450 3b 0a 20 20 69 66 28 20 20 20 28 6c 6f 77 65 72  ;.  if(   (lower
fb460 42 69 74 73 20 3d 3d 20 49 4e 56 41 4c 49 44 5f  Bits == INVALID_
fb470 46 49 4c 45 5f 53 49 5a 45 29 0a 20 20 20 20 20  FILE_SIZE).     
fb480 26 26 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74  && ((error = Get
fb490 4c 61 73 74 45 72 72 6f 72 28 29 29 20 21 3d 20  LastError()) != 
fb4a0 4e 4f 5f 45 52 52 4f 52 29 20 29 0a 20 20 7b 0a  NO_ERROR) ).  {.
fb4b0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
fb4c0 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20  rrno = error;.  
fb4d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fb4e0 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d  IOERR_FSTAT;.  }
fb4f0 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 28 73  .  *pSize = (((s
fb500 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 75 70 70  qlite3_int64)upp
fb510 65 72 42 69 74 73 29 3c 3c 33 32 29 20 2b 20 6c  erBits)<<32) + l
fb520 6f 77 65 72 42 69 74 73 3b 0a 20 20 72 65 74 75  owerBits;.  retu
fb530 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
fb540 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f  ./*.** LOCKFILE_
fb550 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59  FAIL_IMMEDIATELY
fb560 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 6f 6e   is undefined on
fb570 20 73 6f 6d 65 20 57 69 6e 64 6f 77 73 20 73 79   some Windows sy
fb580 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65  stems..*/.#ifnde
fb590 66 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f  f LOCKFILE_FAIL_
fb5a0 49 4d 4d 45 44 49 41 54 45 4c 59 0a 23 20 64 65  IMMEDIATELY.# de
fb5b0 66 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 5f 46 41  fine LOCKFILE_FA
fb5c0 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 31  IL_IMMEDIATELY 1
fb5d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41  .#endif../*.** A
fb5e0 63 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20  cquire a reader 
fb5f0 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 65 72 65  lock..** Differe
fb600 6e 74 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20  nt API routines 
fb610 61 72 65 20 63 61 6c 6c 65 64 20 64 65 70 65 6e  are called depen
fb620 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20  ding on whether 
fb630 6f 72 20 6e 6f 74 20 74 68 69 73 0a 2a 2a 20 69  or not this.** i
fb640 73 20 57 69 6e 39 35 20 6f 72 20 57 69 6e 4e 54  s Win95 or WinNT
fb650 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
fb660 67 65 74 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46  getReadLock(winF
fb670 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69  ile *pFile){.  i
fb680 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73  nt res;.  if( is
fb690 4e 54 28 29 20 29 7b 0a 20 20 20 20 4f 56 45 52  NT() ){.    OVER
fb6a0 4c 41 50 50 45 44 20 6f 76 6c 70 3b 0a 20 20 20  LAPPED ovlp;.   
fb6b0 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20 3d 20 53   ovlp.Offset = S
fb6c0 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20  HARED_FIRST;.   
fb6d0 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69 67 68   ovlp.OffsetHigh
fb6e0 20 3d 20 30 3b 0a 20 20 20 20 6f 76 6c 70 2e 68   = 0;.    ovlp.h
fb6f0 45 76 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 72  Event = 0;.    r
fb700 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 45 78 28  es = LockFileEx(
fb710 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49  pFile->h, LOCKFI
fb720 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54  LE_FAIL_IMMEDIAT
fb730 45 4c 59 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ELY,.           
fb740 20 20 20 20 20 20 20 20 20 20 30 2c 20 53 48 41            0, SHA
fb750 52 45 44 5f 53 49 5a 45 2c 20 30 2c 20 26 6f 76  RED_SIZE, 0, &ov
fb760 6c 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69  lp);./* isNT() i
fb770 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53  s 1 if SQLITE_OS
fb780 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68  _WINCE==1, so th
fb790 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72  is else is never
fb7a0 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23   executed. .*/.#
fb7b0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
fb7c0 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
fb7d0 20 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 20 20 73     int lk;.    s
fb7e0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
fb7f0 73 28 73 69 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c  s(sizeof(lk), &l
fb800 6b 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73  k);.    pFile->s
fb810 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20  haredLockByte = 
fb820 28 73 68 6f 72 74 29 28 28 6c 6b 20 26 20 30 78  (short)((lk & 0x
fb830 37 66 66 66 66 66 66 66 29 25 28 53 48 41 52 45  7fffffff)%(SHARE
fb840 44 5f 53 49 5a 45 20 2d 20 31 29 29 3b 0a 20 20  D_SIZE - 1));.  
fb850 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65    res = LockFile
fb860 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45  (pFile->h, SHARE
fb870 44 5f 46 49 52 53 54 2b 70 46 69 6c 65 2d 3e 73  D_FIRST+pFile->s
fb880 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30  haredLockByte, 0
fb890 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a  , 1, 0);.#endif.
fb8a0 20 20 7d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d    }.  if( res ==
fb8b0 20 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d   0 ){.    pFile-
fb8c0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74  >lastErrno = Get
fb8d0 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d  LastError();.  }
fb8e0 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d  .  return res;.}
fb8f0 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72  ../*.** Undo a r
fb900 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69  eadlock.*/.stati
fb910 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64  c int unlockRead
fb920 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46  Lock(winFile *pF
fb930 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 3b  ile){.  int res;
fb940 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b  .  if( isNT() ){
fb950 0a 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f 63  .    res = Unloc
fb960 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
fb970 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c  SHARED_FIRST, 0,
fb980 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29   SHARED_SIZE, 0)
fb990 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31  ;./* isNT() is 1
fb9a0 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   if SQLITE_OS_WI
fb9b0 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20  NCE==1, so this 
fb9c0 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78  else is never ex
fb9d0 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20  ecuted. .*/.#if 
fb9e0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
fb9f0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
fba00 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65  res = UnlockFile
fba10 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45  (pFile->h, SHARE
fba20 44 5f 46 49 52 53 54 20 2b 20 70 46 69 6c 65 2d  D_FIRST + pFile-
fba30 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c  >sharedLockByte,
fba40 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69   0, 1, 0);.#endi
fba50 66 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73 20  f.  }.  if( res 
fba60 3d 3d 20 30 20 29 7b 0a 20 20 20 20 70 46 69 6c  == 0 ){.    pFil
fba70 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47  e->lastErrno = G
fba80 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
fba90 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b   }.  return res;
fbaa0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
fbab0 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
fbac0 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
fbad0 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
fbae0 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
fbaf0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
fbb00 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
fbb10 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
fbb20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
fbb30 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
fbb40 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
fbb50 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
fbb60 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
fbb70 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
fbb80 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
fbb90 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
fbba0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
fbbb0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
fbbc0 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
fbbd0 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
fbbe0 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
fbbf0 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
fbc00 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
fbc10 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
fbc20 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
fbc30 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
fbc40 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
fbc50 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
fbc60 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
fbc70 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
fbc80 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
fbc90 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
fbca0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
fbcb0 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
fbcc0 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
fbcd0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
fbce0 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
fbcf0 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
fbd00 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
fbd10 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
fbd20 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
fbd30 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
fbd40 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
fbd50 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73  USIVE.**.** This
fbd60 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
fbd70 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
fbd80 63 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f  ck.  The winUnlo
fbd90 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  ck() routine.** 
fbda0 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73  erases all locks
fbdb0 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74   at once and ret
fbdc0 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74  urns us immediat
fbdd0 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c  ely to locking l
fbde0 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73  evel 0..** It is
fbdf0 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
fbe00 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   lower the locki
fbe10 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65  ng level one ste
fbe20 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f  p at a time.  Yo
fbe30 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72  u.** must go str
fbe40 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67  aight to locking
fbe50 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61   level 0..*/.sta
fbe60 74 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28  tic int winLock(
fbe70 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
fbe80 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b  , int locktype){
fbe90 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
fbea0 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74  TE_OK;    /* Ret
fbeb0 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75  urn code from su
fbec0 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69  broutines */.  i
fbed0 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20  nt res = 1;     
fbee0 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
fbef0 6f 66 20 61 20 77 69 6e 64 6f 77 73 20 6c 6f 63  of a windows loc
fbf00 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20  k call */.  int 
fbf10 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20  newLocktype;    
fbf20 20 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d     /* Set pFile-
fbf30 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69  >locktype to thi
fbf40 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65  s value before e
fbf50 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  xiting */.  int 
fbf60 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d  gotPendingLock =
fbf70 20 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65   0;/* True if we
fbf80 20 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44   acquired a PEND
fbf90 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69  ING lock this ti
fbfa0 6d 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20  me */.  winFile 
fbfb0 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
fbfc0 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65  e*)id;.  DWORD e
fbfd0 72 72 6f 72 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b  rror = NO_ERROR;
fbfe0 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  ..  assert( id!=
fbff0 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28  0 );.  OSTRACE5(
fc000 22 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 73 20  "LOCK %d %d was 
fc010 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 20  %d(%d)\n",.     
fc020 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 6c       pFile->h, l
fc030 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
fc040 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d  locktype, pFile-
fc050 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29  >sharedLockByte)
fc060 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
fc070 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f   is already a lo
fc080 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ck of this type 
fc090 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74  or more restrict
fc0a0 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  ive on the.  ** 
fc0b0 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69  OsFile, do nothi
fc0c0 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68  ng. Don't use th
fc0d0 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74  e end_lock: exit
fc0e0 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 73   path, as.  ** s
fc0f0 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74  qlite3OsEnterMut
fc100 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e  ex() hasn't been
fc110 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a   called yet..  *
fc120 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
fc130 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70  ocktype>=locktyp
fc140 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
fc150 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
fc160 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
fc170 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65  he locking seque
fc180 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20  nce is correct. 
fc190 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46   */.  assert( pF
fc1a0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e  ile->locktype!=N
fc1b0 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79  O_LOCK || lockty
fc1c0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
fc1d0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63  );.  assert( loc
fc1e0 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c  ktype!=PENDING_L
fc1f0 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OCK );.  assert(
fc200 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52   locktype!=RESER
fc210 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c  VED_LOCK || pFil
fc220 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  e->locktype==SHA
fc230 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f  RED_LOCK );..  /
fc240 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 49  * Lock the PENDI
fc250 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 20  NG_LOCK byte if 
fc260 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71 75 69  we need to acqui
fc270 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  re a PENDING loc
fc280 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 41 52  k or.  ** a SHAR
fc290 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65 20  ED lock.  If we 
fc2a0 61 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20  are acquiring a 
fc2b0 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65  SHARED lock, the
fc2c0 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f 66 0a   acquisition of.
fc2d0 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 4e 47    ** the PENDING
fc2e0 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 74 65  _LOCK byte is te
fc2f0 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a 20 20  mporary..  */.  
fc300 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 70 46  newLocktype = pF
fc310 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20  ile->locktype;. 
fc320 20 69 66 28 20 20 20 28 70 46 69 6c 65 2d 3e 6c   if(   (pFile->l
fc330 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
fc340 29 0a 20 20 20 20 20 7c 7c 20 28 20 20 20 28 6c  ).     || (   (l
fc350 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
fc360 56 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 20 20  VE_LOCK).       
fc370 20 20 26 26 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    && (pFile->loc
fc380 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f  ktype==RESERVED_
fc390 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20 20  LOCK)).  ){.    
fc3a0 69 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 20 20 20  int cnt = 3;.   
fc3b0 20 77 68 69 6c 65 28 20 63 6e 74 2d 2d 3e 30 20   while( cnt-->0 
fc3c0 26 26 20 28 72 65 73 20 3d 20 4c 6f 63 6b 46 69  && (res = LockFi
fc3d0 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e  le(pFile->h, PEN
fc3e0 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c  DING_BYTE, 0, 1,
fc3f0 20 30 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20   0))==0 ){.     
fc400 20 2f 2a 20 54 72 79 20 33 20 74 69 6d 65 73 20   /* Try 3 times 
fc410 74 6f 20 67 65 74 20 74 68 65 20 70 65 6e 64 69  to get the pendi
fc420 6e 67 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 65  ng lock.  The pe
fc430 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 67 68 74  nding lock might
fc440 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 68 65 6c   be.      ** hel
fc450 64 20 62 79 20 61 6e 6f 74 68 65 72 20 72 65 61  d by another rea
fc460 64 65 72 20 70 72 6f 63 65 73 73 20 77 68 6f 20  der process who 
fc470 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 69 74 20  will release it 
fc480 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a 20 20 20  momentarily..   
fc490 20 20 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52     */.      OSTR
fc4a0 41 43 45 32 28 22 63 6f 75 6c 64 20 6e 6f 74 20  ACE2("could not 
fc4b0 67 65 74 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  get a PENDING lo
fc4c0 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c 20 63  ck. cnt=%d\n", c
fc4d0 6e 74 29 3b 0a 20 20 20 20 20 20 53 6c 65 65 70  nt);.      Sleep
fc4e0 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67  (1);.    }.    g
fc4f0 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20  otPendingLock = 
fc500 72 65 73 3b 0a 20 20 20 20 69 66 28 20 21 72 65  res;.    if( !re
fc510 73 20 29 7b 0a 20 20 20 20 20 20 65 72 72 6f 72  s ){.      error
fc520 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28   = GetLastError(
fc530 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
fc540 2f 2a 20 41 63 71 75 69 72 65 20 61 20 73 68 61  /* Acquire a sha
fc550 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20  red lock.  */.  
fc560 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
fc570 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73  ARED_LOCK && res
fc580 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
fc590 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
fc5a0 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  =NO_LOCK );.    
fc5b0 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63  res = getReadLoc
fc5c0 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66  k(pFile);.    if
fc5d0 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e  ( res ){.      n
fc5e0 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41  ewLocktype = SHA
fc5f0 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65  RED_LOCK;.    }e
fc600 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72  lse{.      error
fc610 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28   = GetLastError(
fc620 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
fc630 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53  /* Acquire a RES
fc640 45 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a  ERVED lock.  */.
fc650 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
fc660 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26  RESERVED_LOCK &&
fc670 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65   res ){.    asse
fc680 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  rt( pFile->lockt
fc690 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
fc6a0 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f   );.    res = Lo
fc6b0 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
fc6c0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20   RESERVED_BYTE, 
fc6d0 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 1, 0);.    if
fc6e0 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e  ( res ){.      n
fc6f0 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53  ewLocktype = RES
fc700 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20  ERVED_LOCK;.    
fc710 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72  }else{.      err
fc720 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  or = GetLastErro
fc730 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  r();.    }.  }..
fc740 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50    /* Acquire a P
fc750 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f  ENDING lock.  */
fc760 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
fc770 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
fc780 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 6e 65  && res ){.    ne
fc790 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44  wLocktype = PEND
fc7a0 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f  ING_LOCK;.    go
fc7b0 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30  tPendingLock = 0
fc7c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
fc7d0 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ire an EXCLUSIVE
fc7e0 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
fc7f0 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
fc800 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73  SIVE_LOCK && res
fc810 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
fc820 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
fc830 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
fc840 20 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b      res = unlock
fc850 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b  ReadLock(pFile);
fc860 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 75  .    OSTRACE2("u
fc870 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e  nreadlock = %d\n
fc880 22 2c 20 72 65 73 29 3b 0a 20 20 20 20 72 65 73  ", res);.    res
fc890 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   = LockFile(pFil
fc8a0 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52  e->h, SHARED_FIR
fc8b0 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49  ST, 0, SHARED_SI
fc8c0 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ZE, 0);.    if( 
fc8d0 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77  res ){.      new
fc8e0 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55  Locktype = EXCLU
fc8f0 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  SIVE_LOCK;.    }
fc900 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f  else{.      erro
fc910 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  r = GetLastError
fc920 28 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43  ();.      OSTRAC
fc930 45 32 28 22 65 72 72 6f 72 2d 63 6f 64 65 20 3d  E2("error-code =
fc940 20 25 64 5c 6e 22 2c 20 65 72 72 6f 72 29 3b 0a   %d\n", error);.
fc950 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63        getReadLoc
fc960 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  k(pFile);.    }.
fc970 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20    }..  /* If we 
fc980 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45  are holding a PE
fc990 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20  NDING lock that 
fc9a0 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65  ought to be rele
fc9b0 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  ased, then.  ** 
fc9c0 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a  release it now..
fc9d0 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65    */.  if( gotPe
fc9e0 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63  ndingLock && loc
fc9f0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
fca00 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b  CK ){.    Unlock
fca10 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50  File(pFile->h, P
fca20 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20  ENDING_BYTE, 0, 
fca30 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  1, 0);.  }..  /*
fca40 20 55 70 64 61 74 65 20 74 68 65 20 73 74 61 74   Update the stat
fca50 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 61  e of the lock ha
fca60 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 69  s held in the fi
fca70 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68  le descriptor th
fca80 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74  en.  ** return t
fca90 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72  he appropriate r
fcaa0 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f  esult code..  */
fcab0 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20  .  if( res ){.  
fcac0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
fcad0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f  ;.  }else{.    O
fcae0 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 46 41  STRACE4("LOCK FA
fcaf0 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 66  ILED %d trying f
fcb00 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 64  or %d but got %d
fcb10 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20  \n", pFile->h,. 
fcb20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79            lockty
fcb30 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 29  pe, newLocktype)
fcb40 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  ;.    pFile->las
fcb50 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a  tErrno = error;.
fcb60 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
fcb70 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c  BUSY;.  }.  pFil
fcb80 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 75  e->locktype = (u
fcb90 38 29 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20  8)newLocktype;. 
fcba0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
fcbb0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
fcbc0 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
fcbd0 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
fcbe0 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
fcbf0 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69   specified.** fi
fcc00 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e  le by this or an
fcc10 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e  y other process.
fcc20 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
fcc30 69 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a  is held, return.
fcc40 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68  ** non-zero, oth
fcc50 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a  erwise zero..*/.
fcc60 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 68  static int winCh
fcc70 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
fcc80 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
fcc90 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b  , int *pResOut){
fcca0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 77 69 6e  .  int rc;.  win
fccb0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77  File *pFile = (w
fccc0 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61  inFile*)id;..  a
fccd0 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
fcce0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
fccf0 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f  ktype>=RESERVED_
fcd00 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  LOCK ){.    rc =
fcd10 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33   1;.    OSTRACE3
fcd20 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25  ("TEST WR-LOCK %
fcd30 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c  d %d (local)\n",
fcd40 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a   pFile->h, rc);.
fcd50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
fcd60 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  = LockFile(pFile
fcd70 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59  ->h, RESERVED_BY
fcd80 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20  TE, 0, 1, 0);.  
fcd90 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
fcda0 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69    UnlockFile(pFi
fcdb0 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f  le->h, RESERVED_
fcdc0 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a  BYTE, 0, 1, 0);.
fcdd0 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 21      }.    rc = !
fcde0 72 63 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33  rc;.    OSTRACE3
fcdf0 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25  ("TEST WR-LOCK %
fce00 64 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e 22  d %d (remote)\n"
fce10 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b  , pFile->h, rc);
fce20 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20  .  }.  *pResOut 
fce30 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53  = rc;.  return S
fce40 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
fce50 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63  ** Lower the loc
fce60 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69  king level on fi
fce70 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 64  le descriptor id
fce80 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c   to locktype.  l
fce90 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20  ocktype.** must 
fcea0 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43  be either NO_LOC
fceb0 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b  K or SHARED_LOCK
fcec0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
fced0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20  ocking level of 
fcee0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
fcef0 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61  tor is already a
fcf00 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68  t or below.** th
fcf10 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
fcf20 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20  ing level, this 
fcf30 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
fcf40 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  op..**.** It is 
fcf50 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72  not possible for
fcf60 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
fcf70 20 66 61 69 6c 20 69 66 20 74 68 65 20 73 65 63   fail if the sec
fcf80 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ond argument.** 
fcf90 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20  is NO_LOCK.  If 
fcfa0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
fcfb0 65 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f  ent is SHARED_LO
fcfc0 43 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  CK then this rou
fcfd0 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65  tine.** might re
fcfe0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
fcff0 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  R;.*/.static int
fd000 20 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 6c 69 74   winUnlock(sqlit
fd010 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
fd020 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e   locktype){.  in
fd030 74 20 74 79 70 65 3b 0a 20 20 77 69 6e 46 69 6c  t type;.  winFil
fd040 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46  e *pFile = (winF
fd050 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72  ile*)id;.  int r
fd060 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
fd070 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d   assert( pFile!=
fd080 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  0 );.  assert( l
fd090 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f  ocktype<=SHARED_
fd0a0 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41 43  LOCK );.  OSTRAC
fd0b0 45 35 28 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f  E5("UNLOCK %d to
fd0c0 20 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e   %d was %d(%d)\n
fd0d0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63  ", pFile->h, loc
fd0e0 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20  ktype,.         
fd0f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
fd100 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c  , pFile->sharedL
fd110 6f 63 6b 42 79 74 65 29 3b 0a 20 20 74 79 70 65  ockByte);.  type
fd120 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79   = pFile->lockty
fd130 70 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d  pe;.  if( type>=
fd140 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29  EXCLUSIVE_LOCK )
fd150 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65  {.    UnlockFile
fd160 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45  (pFile->h, SHARE
fd170 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52  D_FIRST, 0, SHAR
fd180 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20  ED_SIZE, 0);.   
fd190 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
fd1a0 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 21 67  HARED_LOCK && !g
fd1b0 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65  etReadLock(pFile
fd1c0 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  ) ){.      /* Th
fd1d0 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  is should never 
fd1e0 68 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75  happen.  We shou
fd1f0 6c 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c  ld always be abl
fd200 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65  e to.      ** re
fd210 61 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64  acquire the read
fd220 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72   lock */.      r
fd230 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
fd240 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20  _UNLOCK;.    }. 
fd250 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52   }.  if( type>=R
fd260 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a  ESERVED_LOCK ){.
fd270 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70      UnlockFile(p
fd280 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45  File->h, RESERVE
fd290 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29  D_BYTE, 0, 1, 0)
fd2a0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b  ;.  }.  if( lock
fd2b0 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26  type==NO_LOCK &&
fd2c0 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f   type>=SHARED_LO
fd2d0 43 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b  CK ){.    unlock
fd2e0 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b  ReadLock(pFile);
fd2f0 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e  .  }.  if( type>
fd300 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b  =PENDING_LOCK ){
fd310 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  .    UnlockFile(
fd320 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e  pFile->h, PENDIN
fd330 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29  G_BYTE, 0, 1, 0)
fd340 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c  ;.  }.  pFile->l
fd350 6f 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6c 6f  ocktype = (u8)lo
fd360 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e  cktype;.  return
fd370 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   rc;.}../*.** Co
fd380 6e 74 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20  ntrol and query 
fd390 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65  of the open file
fd3a0 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74   handle..*/.stat
fd3b0 69 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 43 6f  ic int winFileCo
fd3c0 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69  ntrol(sqlite3_fi
fd3d0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20  le *id, int op, 
fd3e0 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73  void *pArg){.  s
fd3f0 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20  witch( op ){.   
fd400 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e   case SQLITE_FCN
fd410 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a  TL_LOCKSTATE: {.
fd420 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72        *(int*)pAr
fd430 67 20 3d 20 28 28 77 69 6e 46 69 6c 65 2a 29 69  g = ((winFile*)i
fd440 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  d)->locktype;.  
fd450 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
fd460 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E_OK;.    }.    
fd470 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54  case SQLITE_LAST
fd480 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20  _ERRNO: {.      
fd490 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 69  *(int*)pArg = (i
fd4a0 6e 74 29 28 28 77 69 6e 46 69 6c 65 2a 29 69 64  nt)((winFile*)id
fd4b0 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20  )->lastErrno;.  
fd4c0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
fd4d0 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
fd4e0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fd4f0 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ERROR;.}../*.** 
fd500 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f  Return the secto
fd510 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  r size in bytes 
fd520 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  of the underlyin
fd530 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66  g block device f
fd540 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66  or.** the specif
fd550 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69  ied file. This i
fd560 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20  s almost always 
fd570 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d  512 bytes, but m
fd580 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20  ay be.** larger 
fd590 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73  for some devices
fd5a0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63  ..**.** SQLite c
fd5b0 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73  ode assumes this
fd5c0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74   function cannot
fd5d0 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61   fail. It also a
fd5e0 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69  ssumes that.** i
fd5f0 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20  f two files are 
fd600 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73  created in the s
fd610 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  ame file-system 
fd620 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a  directory (i.e..
fd630 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e  ** a database an
fd640 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69  d its journal fi
fd650 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63  le) that the sec
fd660 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65  tor size will be
fd670 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72   the.** same for
fd680 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63   both..*/.static
fd690 20 69 6e 74 20 77 69 6e 53 65 63 74 6f 72 53 69   int winSectorSi
fd6a0 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ze(sqlite3_file 
fd6b0 2a 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20  *id){.  assert( 
fd6c0 69 64 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  id!=0 );.  retur
fd6d0 6e 20 28 69 6e 74 29 28 28 28 77 69 6e 46 69 6c  n (int)(((winFil
fd6e0 65 2a 29 69 64 29 2d 3e 73 65 63 74 6f 72 53 69  e*)id)->sectorSi
fd6f0 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ze);.}../*.** Re
fd700 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66  turn a vector of
fd710 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65   device characte
fd720 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74  ristics..*/.stat
fd730 69 63 20 69 6e 74 20 77 69 6e 44 65 76 69 63 65  ic int winDevice
fd740 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
fd750 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
fd760 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
fd770 4d 45 54 45 52 28 69 64 29 3b 0a 20 20 72 65 74  METER(id);.  ret
fd780 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
fd790 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 69  This vector defi
fd7a0 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68  nes all the meth
fd7b0 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65  ods that can ope
fd7c0 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71  rate on an.** sq
fd7d0 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 77  lite3_file for w
fd7e0 69 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  in32..*/.static 
fd7f0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
fd800 5f 6d 65 74 68 6f 64 73 20 77 69 6e 49 6f 4d 65  _methods winIoMe
fd810 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20  thod = {.  1,   
fd820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd830 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
fd840 20 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 65 2c 0a   */.  winClose,.
fd850 20 20 77 69 6e 52 65 61 64 2c 0a 20 20 77 69 6e    winRead,.  win
fd860 57 72 69 74 65 2c 0a 20 20 77 69 6e 54 72 75 6e  Write,.  winTrun
fd870 63 61 74 65 2c 0a 20 20 77 69 6e 53 79 6e 63 2c  cate,.  winSync,
fd880 0a 20 20 77 69 6e 46 69 6c 65 53 69 7a 65 2c 0a  .  winFileSize,.
fd890 20 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e    winLock,.  win
fd8a0 55 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e 43 68 65  Unlock,.  winChe
fd8b0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a  ckReservedLock,.
fd8c0 20 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c    winFileControl
fd8d0 2c 0a 20 20 77 69 6e 53 65 63 74 6f 72 53 69 7a  ,.  winSectorSiz
fd8e0 65 2c 0a 20 20 77 69 6e 44 65 76 69 63 65 43 68  e,.  winDeviceCh
fd8f0 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b  aracteristics.};
fd900 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
fd910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
fd950 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20  * Here ends the 
fd960 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74  I/O methods that
fd970 20 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65   form the sqlite
fd980 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
fd990 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  ect..**.** The n
fd9a0 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64  ext block of cod
fd9b0 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
fd9c0 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a   VFS methods..**
fd9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fda00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fda10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
fda20 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46  ** Convert a UTF
fda30 2d 38 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f  -8 filename into
fda40 20 77 68 61 74 65 76 65 72 20 66 6f 72 6d 20 74   whatever form t
fda50 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a  he underlying.**
fda60 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
fda70 6d 20 77 61 6e 74 73 20 66 69 6c 65 6e 61 6d 65  m wants filename
fda80 73 20 69 6e 2e 20 20 53 70 61 63 65 20 74 6f 20  s in.  Space to 
fda90 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 0a  hold the result.
fdaa0 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66  ** is obtained f
fdab0 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d  rom malloc and m
fdac0 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20  ust be freed by 
fdad0 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66  the calling.** f
fdae0 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
fdaf0 69 63 20 76 6f 69 64 20 2a 63 6f 6e 76 65 72 74  ic void *convert
fdb00 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 63 6f 6e  Utf8Filename(con
fdb10 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
fdb20 6d 65 29 7b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  me){.  void *zCo
fdb30 6e 76 65 72 74 65 64 20 3d 20 30 3b 0a 20 20 69  nverted = 0;.  i
fdb40 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
fdb50 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74   zConverted = ut
fdb60 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c  f8ToUnicode(zFil
fdb70 65 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73 4e 54 28  ename);./* isNT(
fdb80 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
fdb90 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
fdba0 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
fdbb0 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
fdbc0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
fdbd0 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65  WINCE==0.  }else
fdbe0 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64  {.    zConverted
fdbf0 20 3d 20 75 74 66 38 54 6f 4d 62 63 73 28 7a 46   = utf8ToMbcs(zF
fdc00 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66  ilename);.#endif
fdc10 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 6c 6c 65 72  .  }.  /* caller
fdc20 20 77 69 6c 6c 20 68 61 6e 64 6c 65 20 6f 75 74   will handle out
fdc30 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20   of memory */.  
fdc40 72 65 74 75 72 6e 20 7a 43 6f 6e 76 65 72 74 65  return zConverte
fdc50 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  d;.}../*.** Crea
fdc60 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  te a temporary f
fdc70 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66  ile name in zBuf
fdc80 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20  .  zBuf must be 
fdc90 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a  big enough to.**
fdca0 20 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d   hold at pVfs->m
fdcb0 78 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 63  xPathname charac
fdcc0 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ters..*/.static 
fdcd0 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 28  int getTempname(
fdce0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
fdcf0 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20  zBuf){.  static 
fdd00 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a  char zChars[] =.
fdd10 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b      "abcdefghijk
fdd20 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22  lmnopqrstuvwxyz"
fdd30 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a  .    "ABCDEFGHIJ
fdd40 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a  KLMNOPQRSTUVWXYZ
fdd50 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38  ".    "012345678
fdd60 39 22 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 20  9";.  size_t i, 
fdd70 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50  j;.  char zTempP
fdd80 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b  ath[MAX_PATH+1];
fdd90 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74  .  if( sqlite3_t
fdda0 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b  emp_directory ){
fddb0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
fddc0 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33  rintf(MAX_PATH-3
fddd0 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25  0, zTempPath, "%
fdde0 73 22 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70  s", sqlite3_temp
fddf0 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 7d  _directory);.  }
fde00 65 6c 73 65 20 69 66 28 20 69 73 4e 54 28 29 20  else if( isNT() 
fde10 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 75  ){.    char *zMu
fde20 6c 74 69 3b 0a 20 20 20 20 57 43 48 41 52 20 7a  lti;.    WCHAR z
fde30 57 69 64 65 50 61 74 68 5b 4d 41 58 5f 50 41 54  WidePath[MAX_PAT
fde40 48 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50  H];.    GetTempP
fde50 61 74 68 57 28 4d 41 58 5f 50 41 54 48 2d 33 30  athW(MAX_PATH-30
fde60 2c 20 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 20  , zWidePath);.  
fde70 20 20 7a 4d 75 6c 74 69 20 3d 20 75 6e 69 63 6f    zMulti = unico
fde80 64 65 54 6f 55 74 66 38 28 7a 57 69 64 65 50 61  deToUtf8(zWidePa
fde90 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 4d 75  th);.    if( zMu
fdea0 6c 74 69 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  lti ){.      sql
fdeb0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41  ite3_snprintf(MA
fdec0 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70  X_PATH-30, zTemp
fded0 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 4d 75 6c  Path, "%s", zMul
fdee0 74 69 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28  ti);.      free(
fdef0 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 7d 65 6c  zMulti);.    }el
fdf00 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  se{.      return
fdf10 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
fdf20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 29 20 69     }./* isNT() i
fdf30 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53  s 1 if SQLITE_OS
fdf40 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68  _WINCE==1, so th
fdf50 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72  is else is never
fdf60 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53   executed. .** S
fdf70 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76  ince the ASCII v
fdf80 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  ersion of these 
fdf90 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e  Windows API do n
fdfa0 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e  ot exist for WIN
fdfb0 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f  CE,.** it's impo
fdfc0 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66  rtant to not ref
fdfd0 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20  erence them for 
fdfe0 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f  WINCE builds..*/
fdff0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
fe000 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b  INCE==0.  }else{
fe010 0a 20 20 20 20 63 68 61 72 20 2a 7a 55 74 66 38  .    char *zUtf8
fe020 3b 0a 20 20 20 20 63 68 61 72 20 7a 4d 62 63 73  ;.    char zMbcs
fe030 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a  Path[MAX_PATH];.
fe040 20 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 41      GetTempPathA
fe050 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 4d  (MAX_PATH-30, zM
fe060 62 63 73 50 61 74 68 29 3b 0a 20 20 20 20 7a 55  bcsPath);.    zU
fe070 74 66 38 20 3d 20 73 71 6c 69 74 65 33 5f 77 69  tf8 = sqlite3_wi
fe080 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38  n32_mbcs_to_utf8
fe090 28 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20  (zMbcsPath);.   
fe0a0 20 69 66 28 20 7a 55 74 66 38 20 29 7b 0a 20 20   if( zUtf8 ){.  
fe0b0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
fe0c0 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30  intf(MAX_PATH-30
fe0d0 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73  , zTempPath, "%s
fe0e0 22 2c 20 7a 55 74 66 38 29 3b 0a 20 20 20 20 20  ", zUtf8);.     
fe0f0 20 66 72 65 65 28 7a 55 74 66 38 29 3b 0a 20 20   free(zUtf8);.  
fe100 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
fe110 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
fe120 45 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  EM;.    }.#endif
fe130 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 73 71 6c  .  }.  for(i=sql
fe140 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65  ite3Strlen30(zTe
fe150 6d 70 50 61 74 68 29 3b 20 69 3e 30 20 26 26 20  mpPath); i>0 && 
fe160 7a 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d 3d 3d  zTempPath[i-1]==
fe170 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a  '\\'; i--){}.  z
fe180 54 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20 30 3b  TempPath[i] = 0;
fe190 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
fe1a0 6e 74 66 28 6e 42 75 66 2d 33 30 2c 20 7a 42 75  ntf(nBuf-30, zBu
fe1b0 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  f,.             
fe1c0 20 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49        "%s\\"SQLI
fe1d0 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45  TE_TEMP_FILE_PRE
fe1e0 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 29 3b  FIX, zTempPath);
fe1f0 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74  .  j = sqlite3St
fe200 72 6c 65 6e 33 30 28 7a 42 75 66 29 3b 0a 20 20  rlen30(zBuf);.  
fe210 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
fe220 73 73 28 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 29  ss(20, &zBuf[j])
fe230 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32  ;.  for(i=0; i<2
fe240 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20  0; i++, j++){.  
fe250 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61    zBuf[j] = (cha
fe260 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69  r)zChars[ ((unsi
fe270 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a  gned char)zBuf[j
fe280 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72  ])%(sizeof(zChar
fe290 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a  s)-1) ];.  }.  z
fe2a0 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53  Buf[j] = 0;.  OS
fe2b0 54 52 41 43 45 32 28 22 54 45 4d 50 20 46 49 4c  TRACE2("TEMP FIL
fe2c0 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42  ENAME: %s\n", zB
fe2d0 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  uf);.  return SQ
fe2e0 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a  LITE_OK; .}../*.
fe2f0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  ** The return va
fe300 6c 75 65 20 6f 66 20 67 65 74 4c 61 73 74 45 72  lue of getLastEr
fe310 72 6f 72 4d 73 67 0a 2a 2a 20 69 73 20 7a 65 72  rorMsg.** is zer
fe320 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d  o if the error m
fe330 65 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74  essage fits in t
fe340 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f  he buffer, or no
fe350 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77  n-zero.** otherw
fe360 69 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73  ise (if the mess
fe370 61 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65  age was truncate
fe380 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  d)..*/.static in
fe390 74 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73  t getLastErrorMs
fe3a0 67 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  g(int nBuf, char
fe3b0 20 2a 7a 42 75 66 29 7b 0a 20 20 2f 2a 20 46 6f   *zBuf){.  /* Fo
fe3c0 72 6d 61 74 4d 65 73 73 61 67 65 20 72 65 74 75  rmatMessage retu
fe3d0 72 6e 73 20 30 20 6f 6e 20 66 61 69 6c 75 72 65  rns 0 on failure
fe3e0 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74 0a  .  Otherwise it.
fe3f0 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 74 68 65    ** returns the
fe400 20 6e 75 6d 62 65 72 20 6f 66 20 54 43 48 41 52   number of TCHAR
fe410 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  s written to the
fe420 20 6f 75 74 70 75 74 0a 20 20 2a 2a 20 62 75 66   output.  ** buf
fe430 66 65 72 2c 20 65 78 63 6c 75 64 69 6e 67 20 74  fer, excluding t
fe440 68 65 20 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e  he terminating n
fe450 75 6c 6c 20 63 68 61 72 2e 0a 20 20 2a 2f 0a 20  ull char..  */. 
fe460 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d 20 47   DWORD error = G
fe470 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
fe480 20 44 57 4f 52 44 20 64 77 4c 65 6e 20 3d 20 30   DWORD dwLen = 0
fe490 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 3d  ;.  char *zOut =
fe4a0 20 30 3b 0a 0a 20 20 69 66 28 20 69 73 4e 54 28   0;..  if( isNT(
fe4b0 29 20 29 7b 0a 20 20 20 20 57 43 48 41 52 20 2a  ) ){.    WCHAR *
fe4c0 7a 54 65 6d 70 57 69 64 65 20 3d 20 4e 55 4c 4c  zTempWide = NULL
fe4d0 3b 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20 46 6f  ;.    dwLen = Fo
fe4e0 72 6d 61 74 4d 65 73 73 61 67 65 57 28 46 4f 52  rmatMessageW(FOR
fe4f0 4d 41 54 5f 4d 45 53 53 41 47 45 5f 41 4c 4c 4f  MAT_MESSAGE_ALLO
fe500 43 41 54 45 5f 42 55 46 46 45 52 20 7c 20 46 4f  CATE_BUFFER | FO
fe510 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 4f  RMAT_MESSAGE_FRO
fe520 4d 5f 53 59 53 54 45 4d 20 7c 20 46 4f 52 4d 41  M_SYSTEM | FORMA
fe530 54 5f 4d 45 53 53 41 47 45 5f 49 47 4e 4f 52 45  T_MESSAGE_IGNORE
fe540 5f 49 4e 53 45 52 54 53 2c 0a 20 20 20 20 20 20  _INSERTS,.      
fe550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe560 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20       NULL,.     
fe570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe580 20 20 20 20 20 20 65 72 72 6f 72 2c 0a 20 20 20        error,.   
fe590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe5a0 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20          0,.     
fe5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe5c0 20 20 20 20 20 20 28 4c 50 57 53 54 52 29 20 26        (LPWSTR) &
fe5d0 7a 54 65 6d 70 57 69 64 65 2c 0a 20 20 20 20 20  zTempWide,.     
fe5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe5f0 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20        0,.       
fe600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe610 20 20 20 20 30 29 3b 0a 20 20 20 20 69 66 28 20      0);.    if( 
fe620 64 77 4c 65 6e 20 3e 20 30 20 29 7b 0a 20 20 20  dwLen > 0 ){.   
fe630 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74 65 20 61     /* allocate a
fe640 20 62 75 66 66 65 72 20 61 6e 64 20 63 6f 6e 76   buffer and conv
fe650 65 72 74 20 74 6f 20 55 54 46 38 20 2a 2f 0a 20  ert to UTF8 */. 
fe660 20 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63       zOut = unic
fe670 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 57  odeToUtf8(zTempW
fe680 69 64 65 29 3b 0a 20 20 20 20 20 20 2f 2a 20 66  ide);.      /* f
fe690 72 65 65 20 74 68 65 20 73 79 73 74 65 6d 20 62  ree the system b
fe6a0 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20  uffer allocated 
fe6b0 62 79 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65  by FormatMessage
fe6c0 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 61 6c 46   */.      LocalF
fe6d0 72 65 65 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a  ree(zTempWide);.
fe6e0 20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 29 20      }./* isNT() 
fe6f0 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f  is 1 if SQLITE_O
fe700 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74  S_WINCE==1, so t
fe710 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65  his else is neve
fe720 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20  r executed. .** 
fe730 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20  Since the ASCII 
fe740 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65  version of these
fe750 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20   Windows API do 
fe760 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49  not exist for WI
fe770 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70  NCE,.** it's imp
fe780 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65  ortant to not re
fe790 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72  ference them for
fe7a0 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a   WINCE builds..*
fe7b0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
fe7c0 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65  WINCE==0.  }else
fe7d0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d  {.    char *zTem
fe7e0 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 77  p = NULL;.    dw
fe7f0 4c 65 6e 20 3d 20 46 6f 72 6d 61 74 4d 65 73 73  Len = FormatMess
fe800 61 67 65 41 28 46 4f 52 4d 41 54 5f 4d 45 53 53  ageA(FORMAT_MESS
fe810 41 47 45 5f 41 4c 4c 4f 43 41 54 45 5f 42 55 46  AGE_ALLOCATE_BUF
fe820 46 45 52 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 53  FER | FORMAT_MES
fe830 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54 45 4d  SAGE_FROM_SYSTEM
fe840 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47   | FORMAT_MESSAG
fe850 45 5f 49 47 4e 4f 52 45 5f 49 4e 53 45 52 54 53  E_IGNORE_INSERTS
fe860 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
fe870 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c               NUL
fe880 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  L,.             
fe890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72                er
fe8a0 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ror,.           
fe8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe8c0 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
fe8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4c                (L
fe8e0 50 53 54 52 29 20 26 7a 54 65 6d 70 2c 0a 20 20  PSTR) &zTemp,.  
fe8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe900 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20           0,.    
fe910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe920 20 20 20 20 20 20 20 30 29 3b 0a 20 20 20 20 69         0);.    i
fe930 66 28 20 64 77 4c 65 6e 20 3e 20 30 20 29 7b 0a  f( dwLen > 0 ){.
fe940 20 20 20 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74        /* allocat
fe950 65 20 61 20 62 75 66 66 65 72 20 61 6e 64 20 63  e a buffer and c
fe960 6f 6e 76 65 72 74 20 74 6f 20 55 54 46 38 20 2a  onvert to UTF8 *
fe970 2f 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d 20 73  /.      zOut = s
fe980 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63  qlite3_win32_mbc
fe990 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 6d 70 29  s_to_utf8(zTemp)
fe9a0 3b 0a 20 20 20 20 20 20 2f 2a 20 66 72 65 65 20  ;.      /* free 
fe9b0 74 68 65 20 73 79 73 74 65 6d 20 62 75 66 66 65  the system buffe
fe9c0 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 46  r allocated by F
fe9d0 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 2a 2f 0a  ormatMessage */.
fe9e0 20 20 20 20 20 20 4c 6f 63 61 6c 46 72 65 65 28        LocalFree(
fe9f0 7a 54 65 6d 70 29 3b 0a 20 20 20 20 7d 0a 23 65  zTemp);.    }.#e
fea00 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 30  ndif.  }.  if( 0
fea10 20 3d 3d 20 64 77 4c 65 6e 20 29 7b 0a 20 20 20   == dwLen ){.   
fea20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
fea30 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f  f(nBuf, zBuf, "O
fea40 73 45 72 72 6f 72 20 30 78 25 78 20 28 25 75 29  sError 0x%x (%u)
fea50 22 2c 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29  ", error, error)
fea60 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
fea70 2a 20 63 6f 70 79 20 61 20 6d 61 78 69 6d 75 6d  * copy a maximum
fea80 20 6f 66 20 6e 42 75 66 20 63 68 61 72 73 20 74   of nBuf chars t
fea90 6f 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  o output buffer 
feaa0 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  */.    sqlite3_s
feab0 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42  nprintf(nBuf, zB
feac0 75 66 2c 20 22 25 73 22 2c 20 7a 4f 75 74 29 3b  uf, "%s", zOut);
fead0 0a 20 20 20 20 2f 2a 20 66 72 65 65 20 74 68 65  .    /* free the
feae0 20 55 54 46 38 20 62 75 66 66 65 72 20 2a 2f 0a   UTF8 buffer */.
feaf0 20 20 20 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a      free(zOut);.
feb00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
feb10 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20  }../*.** Open a 
feb20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
feb30 69 6e 74 20 77 69 6e 4f 70 65 6e 28 0a 20 20 73  int winOpen(.  s
feb40 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
feb50 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20  ,        /* Not 
feb60 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  used */.  const 
feb70 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
feb80 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
feb90 68 65 20 66 69 6c 65 20 28 55 54 46 2d 38 29 20  he file (UTF-8) 
feba0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
febb0 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f  e *id,         /
febc0 2a 20 57 72 69 74 65 20 74 68 65 20 53 51 4c 69  * Write the SQLi
febd0 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68  te file handle h
febe0 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  ere */.  int fla
febf0 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
fec00 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 65 20     /* Open mode 
fec10 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  flags */.  int *
fec20 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 20  pOutFlags       
fec30 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72       /* Status r
fec40 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29  eturn flags */.)
fec50 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20  {.  HANDLE h;.  
fec60 44 57 4f 52 44 20 64 77 44 65 73 69 72 65 64 41  DWORD dwDesiredA
fec70 63 63 65 73 73 3b 0a 20 20 44 57 4f 52 44 20 64  ccess;.  DWORD d
fec80 77 53 68 61 72 65 4d 6f 64 65 3b 0a 20 20 44 57  wShareMode;.  DW
fec90 4f 52 44 20 64 77 43 72 65 61 74 69 6f 6e 44 69  ORD dwCreationDi
feca0 73 70 6f 73 69 74 69 6f 6e 3b 0a 20 20 44 57 4f  sposition;.  DWO
fecb0 52 44 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74  RD dwFlagsAndAtt
fecc0 72 69 62 75 74 65 73 20 3d 20 30 3b 0a 23 69 66  ributes = 0;.#if
fecd0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
fece0 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 20 3d 20  .  int isTemp = 
fecf0 30 3b 0a 23 65 6e 64 69 66 0a 20 20 77 69 6e 46  0;.#endif.  winF
fed00 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
fed10 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 76 6f 69  nFile*)id;.  voi
fed20 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 20 20  d *zConverted;  
fed30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
fed40 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20 4f 53  * Filename in OS
fed50 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63   encoding */.  c
fed60 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 74 66 38  onst char *zUtf8
fed70 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 20 20 20  Name = zName;   
fed80 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20   /* Filename in 
fed90 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 2a  UTF-8 encoding *
feda0 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d  /.  char zTmpnam
fedb0 65 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 20 20  e[MAX_PATH+1];  
fedc0 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20        /* Buffer 
fedd0 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74  used to create t
fede0 65 6d 70 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a  emp filename */.
fedf0 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
fee00 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   );.  UNUSED_PAR
fee10 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 0a 20  AMETER(pVfs);.. 
fee20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e   /* If the secon
fee30 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
fee40 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e  is function is N
fee50 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20 61 20  ULL, generate a 
fee60 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20  .  ** temporary 
fee70 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75 73 65  file name to use
fee80 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 55   .  */.  if( !zU
fee90 74 66 38 4e 61 6d 65 20 29 7b 0a 20 20 20 20 69  tf8Name ){.    i
feea0 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e  nt rc = getTempn
feeb0 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b 31 2c 20  ame(MAX_PATH+1, 
feec0 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69  zTmpname);.    i
feed0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
feee0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
feef0 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a   rc;.    }.    z
fef00 55 74 66 38 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e  Utf8Name = zTmpn
fef10 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  ame;.  }..  /* C
fef20 6f 6e 76 65 72 74 20 74 68 65 20 66 69 6c 65 6e  onvert the filen
fef30 61 6d 65 20 74 6f 20 74 68 65 20 73 79 73 74 65  ame to the syste
fef40 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20  m encoding. */. 
fef50 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f   zConverted = co
fef60 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d  nvertUtf8Filenam
fef70 65 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0a 20 20  e(zUtf8Name);.  
fef80 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d  if( zConverted==
fef90 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
fefa0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
fefb0 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  }..  if( flags &
fefc0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
fefd0 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 64 77  DWRITE ){.    dw
fefe0 44 65 73 69 72 65 64 41 63 63 65 73 73 20 3d 20  DesiredAccess = 
feff0 47 45 4e 45 52 49 43 5f 52 45 41 44 20 7c 20 47  GENERIC_READ | G
ff000 45 4e 45 52 49 43 5f 57 52 49 54 45 3b 0a 20 20  ENERIC_WRITE;.  
ff010 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 44 65 73  }else{.    dwDes
ff020 69 72 65 64 41 63 63 65 73 73 20 3d 20 47 45 4e  iredAccess = GEN
ff030 45 52 49 43 5f 52 45 41 44 3b 0a 20 20 7d 0a 20  ERIC_READ;.  }. 
ff040 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   /* SQLITE_OPEN_
ff050 45 58 43 4c 55 53 49 56 45 20 69 73 20 75 73 65  EXCLUSIVE is use
ff060 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  d to make sure t
ff070 68 61 74 20 61 20 6e 65 77 20 66 69 6c 65 20 69  hat a new file i
ff080 73 20 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 2e  s .  ** created.
ff090 20 53 51 4c 69 74 65 20 64 6f 65 73 6e 27 74 20   SQLite doesn't 
ff0a0 75 73 65 20 69 74 20 74 6f 20 69 6e 64 69 63 61  use it to indica
ff0b0 74 65 20 22 65 78 63 6c 75 73 69 76 65 20 61 63  te "exclusive ac
ff0c0 63 65 73 73 22 20 0a 20 20 2a 2a 20 61 73 20 69  cess" .  ** as i
ff0d0 74 20 69 73 20 75 73 75 61 6c 6c 79 20 75 6e 64  t is usually und
ff0e0 65 72 73 74 6f 6f 64 2e 0a 20 20 2a 2f 0a 20 20  erstood..  */.  
ff0f0 61 73 73 65 72 74 28 21 28 66 6c 61 67 73 20 26  assert(!(flags &
ff100 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
ff110 4c 55 53 49 56 45 29 20 7c 7c 20 28 66 6c 61 67  LUSIVE) || (flag
ff120 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
ff130 43 52 45 41 54 45 29 29 3b 0a 20 20 69 66 28 20  CREATE));.  if( 
ff140 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
ff150 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 29 7b  PEN_EXCLUSIVE ){
ff160 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 73 20  .    /* Creates 
ff170 61 20 6e 65 77 20 66 69 6c 65 2c 20 6f 6e 6c 79  a new file, only
ff180 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   if it does not 
ff190 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 20 2a  already exist. *
ff1a0 2f 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  /.    /* If the 
ff1b0 66 69 6c 65 20 65 78 69 73 74 73 2c 20 69 74 20  file exists, it 
ff1c0 66 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 64 77  fails. */.    dw
ff1d0 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
ff1e0 69 6f 6e 20 3d 20 43 52 45 41 54 45 5f 4e 45 57  ion = CREATE_NEW
ff1f0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c  ;.  }else if( fl
ff200 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
ff210 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20  N_CREATE ){.    
ff220 2f 2a 20 4f 70 65 6e 20 65 78 69 73 74 69 6e 67  /* Open existing
ff230 20 66 69 6c 65 2c 20 6f 72 20 63 72 65 61 74 65   file, or create
ff240 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 65   if it doesn't e
ff250 78 69 73 74 20 2a 2f 0a 20 20 20 20 64 77 43 72  xist */.    dwCr
ff260 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f  eationDispositio
ff270 6e 20 3d 20 4f 50 45 4e 5f 41 4c 57 41 59 53 3b  n = OPEN_ALWAYS;
ff280 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
ff290 20 4f 70 65 6e 73 20 61 20 66 69 6c 65 2c 20 6f   Opens a file, o
ff2a0 6e 6c 79 20 69 66 20 69 74 20 65 78 69 73 74 73  nly if it exists
ff2b0 2e 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74  . */.    dwCreat
ff2c0 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d  ionDisposition =
ff2d0 20 4f 50 45 4e 5f 45 58 49 53 54 49 4e 47 3b 0a   OPEN_EXISTING;.
ff2e0 20 20 7d 0a 20 20 64 77 53 68 61 72 65 4d 6f 64    }.  dwShareMod
ff2f0 65 20 3d 20 46 49 4c 45 5f 53 48 41 52 45 5f 52  e = FILE_SHARE_R
ff300 45 41 44 20 7c 20 46 49 4c 45 5f 53 48 41 52 45  EAD | FILE_SHARE
ff310 5f 57 52 49 54 45 3b 0a 20 20 69 66 28 20 66 6c  _WRITE;.  if( fl
ff320 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
ff330 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20  N_DELETEONCLOSE 
ff340 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  ){.#if SQLITE_OS
ff350 5f 57 49 4e 43 45 0a 20 20 20 20 64 77 46 6c 61  _WINCE.    dwFla
ff360 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20  gsAndAttributes 
ff370 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  = FILE_ATTRIBUTE
ff380 5f 48 49 44 44 45 4e 3b 0a 20 20 20 20 69 73 54  _HIDDEN;.    isT
ff390 65 6d 70 20 3d 20 31 3b 0a 23 65 6c 73 65 0a 20  emp = 1;.#else. 
ff3a0 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74     dwFlagsAndAtt
ff3b0 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41  ributes = FILE_A
ff3c0 54 54 52 49 42 55 54 45 5f 54 45 4d 50 4f 52 41  TTRIBUTE_TEMPORA
ff3d0 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  RY.             
ff3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff3f0 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49 42 55    | FILE_ATTRIBU
ff400 54 45 5f 48 49 44 44 45 4e 0a 20 20 20 20 20 20  TE_HIDDEN.      
ff410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff420 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f           | FILE_
ff430 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f 43  FLAG_DELETE_ON_C
ff440 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  LOSE;.#endif.  }
ff450 65 6c 73 65 7b 0a 20 20 20 20 64 77 46 6c 61 67  else{.    dwFlag
ff460 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d  sAndAttributes =
ff470 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f   FILE_ATTRIBUTE_
ff480 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f 2a  NORMAL;.  }.  /*
ff490 20 52 65 70 6f 72 74 73 20 66 72 6f 6d 20 74 68   Reports from th
ff4a0 65 20 69 6e 74 65 72 6e 65 74 20 61 72 65 20 74  e internet are t
ff4b0 68 61 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  hat performance 
ff4c0 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 62  is always.  ** b
ff4d0 65 74 74 65 72 20 69 66 20 46 49 4c 45 5f 46 4c  etter if FILE_FL
ff4e0 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 53  AG_RANDOM_ACCESS
ff4f0 20 69 73 20 75 73 65 64 2e 20 20 54 69 63 6b 65   is used.  Ticke
ff500 74 20 23 32 36 39 39 2e 20 2a 2f 0a 23 69 66 20  t #2699. */.#if 
ff510 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
ff520 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72    dwFlagsAndAttr
ff530 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f 46  ibutes |= FILE_F
ff540 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53  LAG_RANDOM_ACCES
ff550 53 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20  S;.#endif.  if( 
ff560 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 20  isNT() ){.    h 
ff570 3d 20 43 72 65 61 74 65 46 69 6c 65 57 28 28 57  = CreateFileW((W
ff580 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
ff590 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 72  ,.       dwDesir
ff5a0 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20  edAccess,.      
ff5b0 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20   dwShareMode,.  
ff5c0 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20       NULL,.     
ff5d0 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70    dwCreationDisp
ff5e0 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20  osition,.       
ff5f0 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62  dwFlagsAndAttrib
ff600 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c  utes,.       NUL
ff610 4c 0a 20 20 20 20 29 3b 0a 2f 2a 20 69 73 4e 54  L.    );./* isNT
ff620 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54  () is 1 if SQLIT
ff630 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73  E_OS_WINCE==1, s
ff640 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e  o this else is n
ff650 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a  ever executed. .
ff660 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43  ** Since the ASC
ff670 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  II version of th
ff680 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20  ese Windows API 
ff690 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72  do not exist for
ff6a0 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20   WINCE,.** it's 
ff6b0 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74  important to not
ff6c0 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20   reference them 
ff6d0 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73  for WINCE builds
ff6e0 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
ff6f0 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65  OS_WINCE==0.  }e
ff700 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 43 72 65  lse{.    h = Cre
ff710 61 74 65 46 69 6c 65 41 28 28 63 68 61 72 2a 29  ateFileA((char*)
ff720 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20  zConverted,.    
ff730 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65     dwDesiredAcce
ff740 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61  ss,.       dwSha
ff750 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e  reMode,.       N
ff760 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72  ULL,.       dwCr
ff770 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f  eationDispositio
ff780 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67  n,.       dwFlag
ff790 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a  sAndAttributes,.
ff7a0 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20         NULL.    
ff7b0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  );.#endif.  }.  
ff7c0 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49 44 5f 48  if( h==INVALID_H
ff7d0 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 7b 0a 20  ANDLE_VALUE ){. 
ff7e0 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74     free(zConvert
ff7f0 65 64 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61  ed);.    if( fla
ff800 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
ff810 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20  _READWRITE ){.  
ff820 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e 4f 70      return winOp
ff830 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20  en(pVfs, zName, 
ff840 69 64 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  id, .           
ff850 20 20 28 28 66 6c 61 67 73 7c 53 51 4c 49 54 45    ((flags|SQLITE
ff860 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 26  _OPEN_READONLY)&
ff870 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  ~SQLITE_OPEN_REA
ff880 44 57 52 49 54 45 29 2c 20 70 4f 75 74 46 6c 61  DWRITE), pOutFla
ff890 67 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  gs);.    }else{.
ff8a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
ff8b0 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20  ITE_CANTOPEN;.  
ff8c0 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f    }.  }.  if( pO
ff8d0 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 69  utFlags ){.    i
ff8e0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
ff8f0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
ff900 20 29 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46   ){.      *pOutF
ff910 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50  lags = SQLITE_OP
ff920 45 4e 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20  EN_READWRITE;.  
ff930 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a    }else{.      *
ff940 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49  pOutFlags = SQLI
ff950 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
ff960 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65  ;.    }.  }.  me
ff970 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73  mset(pFile, 0, s
ff980 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 29 3b 0a  izeof(*pFile));.
ff990 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64    pFile->pMethod
ff9a0 20 3d 20 26 77 69 6e 49 6f 4d 65 74 68 6f 64 3b   = &winIoMethod;
ff9b0 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b  .  pFile->h = h;
ff9c0 0a 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72  .  pFile->lastEr
ff9d0 72 6e 6f 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a  rno = NO_ERROR;.
ff9e0 20 20 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53    pFile->sectorS
ff9f0 69 7a 65 20 3d 20 67 65 74 53 65 63 74 6f 72 53  ize = getSectorS
ffa00 69 7a 65 28 70 56 66 73 2c 20 7a 55 74 66 38 4e  ize(pVfs, zUtf8N
ffa10 61 6d 65 29 3b 0a 23 69 66 20 53 51 4c 49 54 45  ame);.#if SQLITE
ffa20 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 66 28 20  _OS_WINCE.  if( 
ffa30 28 66 6c 61 67 73 20 26 20 28 53 51 4c 49 54 45  (flags & (SQLITE
ffa40 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c  _OPEN_READWRITE|
ffa50 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
ffa60 5f 44 42 29 29 20 3d 3d 0a 20 20 20 20 20 20 20  _DB)) ==.       
ffa70 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
ffa80 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53  OPEN_READWRITE|S
ffa90 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
ffaa0 44 42 29 0a 20 20 20 20 20 20 20 26 26 20 21 77  DB).       && !w
ffab0 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 28 7a  inceCreateLock(z
ffac0 4e 61 6d 65 2c 20 70 46 69 6c 65 29 0a 20 20 29  Name, pFile).  )
ffad0 7b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c  {.    CloseHandl
ffae0 65 28 68 29 3b 0a 20 20 20 20 66 72 65 65 28 7a  e(h);.    free(z
ffaf0 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
ffb00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
ffb10 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20 69 66  NTOPEN;.  }.  if
ffb20 28 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20  ( isTemp ){.    
ffb30 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e  pFile->zDeleteOn
ffb40 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e 76 65 72 74  Close = zConvert
ffb50 65 64 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  ed;.  }else.#end
ffb60 69 66 0a 20 20 7b 0a 20 20 20 20 66 72 65 65 28  if.  {.    free(
ffb70 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 7d  zConverted);.  }
ffb80 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b  .  OpenCounter(+
ffb90 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  1);.  return SQL
ffba0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
ffbb0 20 44 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65   Delete the name
ffbc0 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  d file..**.** No
ffbd0 74 65 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20  te that windows 
ffbe0 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61  does not allow a
ffbf0 20 66 69 6c 65 20 74 6f 20 62 65 20 64 65 6c 65   file to be dele
ffc00 74 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65  ted if some othe
ffc10 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 68 61 73  r.** process has
ffc20 20 69 74 20 6f 70 65 6e 2e 20 20 53 6f 6d 65 74   it open.  Somet
ffc30 69 6d 65 73 20 61 20 76 69 72 75 73 20 73 63 61  imes a virus sca
ffc40 6e 6e 65 72 20 6f 72 20 69 6e 64 65 78 69 6e 67  nner or indexing
ffc50 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 77 69 6c 6c   program.** will
ffc60 20 6f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20   open a journal 
ffc70 66 69 6c 65 20 73 68 6f 72 74 6c 79 20 61 66 74  file shortly aft
ffc80 65 72 20 69 74 20 69 73 20 63 72 65 61 74 65 64  er it is created
ffc90 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 0a   in order to do.
ffca0 2a 2a 20 77 68 61 74 65 76 65 72 20 69 74 20 64  ** whatever it d
ffcb0 6f 65 73 2e 20 20 57 68 69 6c 65 20 74 68 69 73  oes.  While this
ffcc0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 69   other process i
ffcd0 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a  s holding the.**
ffce0 20 66 69 6c 65 20 6f 70 65 6e 2c 20 77 65 20 77   file open, we w
ffcf0 69 6c 6c 20 62 65 20 75 6e 61 62 6c 65 20 74 6f  ill be unable to
ffd00 20 64 65 6c 65 74 65 20 69 74 2e 20 20 54 6f 20   delete it.  To 
ffd10 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73  work around this
ffd20 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20  .** problem, we 
ffd30 64 65 6c 61 79 20 31 30 30 20 6d 69 6c 6c 69 73  delay 100 millis
ffd40 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 79 20 74  econds and try t
ffd50 6f 20 64 65 6c 65 74 65 20 61 67 61 69 6e 2e 20  o delete again. 
ffd60 20 55 70 0a 2a 2a 20 74 6f 20 4d 58 5f 44 45 4c   Up.** to MX_DEL
ffd70 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 73 20 64  ETION_ATTEMPTs d
ffd80 65 6c 65 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  eletion attempts
ffd90 20 61 72 65 20 72 75 6e 20 62 65 66 6f 72 65 20   are run before 
ffda0 67 69 76 69 6e 67 0a 2a 2a 20 75 70 20 61 6e 64  giving.** up and
ffdb0 20 72 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72   returning an er
ffdc0 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ror..*/.#define 
ffdd0 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45  MX_DELETION_ATTE
ffde0 4d 50 54 53 20 35 0a 73 74 61 74 69 63 20 69 6e  MPTS 5.static in
ffdf0 74 20 77 69 6e 44 65 6c 65 74 65 28 0a 20 20 73  t winDelete(.  s
ffe00 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
ffe10 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f  ,          /* No
ffe20 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20  t used on win32 
ffe30 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
ffe40 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20  *zFilename,     
ffe50 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65   /* Name of file
ffe60 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
ffe70 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20  int syncDir     
ffe80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
ffe90 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 32  ot used on win32
ffea0 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 6e 74   */.){.  int cnt
ffeb0 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20 72 63   = 0;.  DWORD rc
ffec0 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20  ;.  DWORD error 
ffed0 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  = 0;.  void *zCo
ffee0 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72  nverted = conver
ffef0 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46  tUtf8Filename(zF
fff00 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53  ilename);.  UNUS
fff10 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
fff20 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  s);.  UNUSED_PAR
fff30 41 4d 45 54 45 52 28 73 79 6e 63 44 69 72 29 3b  AMETER(syncDir);
fff40 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65  .  if( zConverte
fff50 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  d==0 ){.    retu
fff60 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
fff70 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49  .  }.  SimulateI
fff80 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51  OError(return SQ
fff90 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54  LITE_IOERR_DELET
fffa0 45 29 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29  E);.  if( isNT()
fffb0 20 29 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20   ){.    do{.    
fffc0 20 20 44 65 6c 65 74 65 46 69 6c 65 57 28 7a 43    DeleteFileW(zC
fffd0 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d  onverted);.    }
fffe0 77 68 69 6c 65 28 20 20 20 28 20 20 20 28 28 72  while(   (   ((r
ffff0 63 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69  c = GetFileAttri
10000 62 75 74 65 73 57 28 7a 43 6f 6e 76 65 72 74 65  butesW(zConverte
10001 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46  d)) != INVALID_F
10002 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a  ILE_ATTRIBUTES).
10003 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
10004 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c  | ((error = GetL
10005 61 73 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45  astError()) == E
10006 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49  RROR_ACCESS_DENI
10007 45 44 29 29 0a 20 20 20 20 20 20 20 20 20 20 20  ED)).           
10008 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44  && (++cnt < MX_D
10009 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53  ELETION_ATTEMPTS
1000a 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20  ).           && 
1000b 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20  (Sleep(100), 1) 
1000c 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
1000d 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
1000e 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
1000f 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
10010 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e  xecuted. .** Sin
10011 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72  ce the ASCII ver
10012 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69  sion of these Wi
10013 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74  ndows API do not
10014 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45   exist for WINCE
10015 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74  ,.** it's import
10016 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72  ant to not refer
10017 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49  ence them for WI
10018 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23  NCE builds..*/.#
10019 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
1001a 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
1001b 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c     do{.      Del
1001c 65 74 65 46 69 6c 65 41 28 7a 43 6f 6e 76 65 72  eteFileA(zConver
1001d 74 65 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65  ted);.    }while
1001e 28 20 20 20 28 20 20 20 28 28 72 63 20 3d 20 47  (   (   ((rc = G
1001f 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73  etFileAttributes
10020 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21  A(zConverted)) !
10021 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41  = INVALID_FILE_A
10022 54 54 52 49 42 55 54 45 53 29 0a 20 20 20 20 20  TTRIBUTES).     
10023 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65            || ((e
10024 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72  rror = GetLastEr
10025 72 6f 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f  ror()) == ERROR_
10026 41 43 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a  ACCESS_DENIED)).
10027 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 2b             && (+
10028 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49  +cnt < MX_DELETI
10029 4f 4e 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20  ON_ATTEMPTS).   
1002a 20 20 20 20 20 20 20 20 26 26 20 28 53 6c 65 65          && (Slee
1002b 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 65  p(100), 1) );.#e
1002c 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28  ndif.  }.  free(
1002d 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 4f  zConverted);.  O
1002e 53 54 52 41 43 45 32 28 22 44 45 4c 45 54 45 20  STRACE2("DELETE 
1002f 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65  \"%s\"\n", zFile
10030 6e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  name);.  return 
10031 28 20 20 20 28 72 63 20 3d 3d 20 49 4e 56 41 4c  (   (rc == INVAL
10032 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54  ID_FILE_ATTRIBUT
10033 45 53 29 20 0a 20 20 20 20 20 20 20 20 20 20 26  ES) .          &
10034 26 20 28 65 72 72 6f 72 20 3d 3d 20 45 52 52 4f  & (error == ERRO
10035 52 5f 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44  R_FILE_NOT_FOUND
10036 29 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  )) ? SQLITE_OK :
10037 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45   SQLITE_IOERR_DE
10038 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  LETE;.}../*.** C
10039 68 65 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e  heck the existan
1003a 63 65 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66  ce and status of
1003b 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74   a file..*/.stat
1003c 69 63 20 69 6e 74 20 77 69 6e 41 63 63 65 73 73  ic int winAccess
1003d 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
1003e 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 2f  *pVfs,         /
1003f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69  * Not used on wi
10040 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  n32 */.  const c
10041 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
10042 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66      /* Name of f
10043 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a  ile to check */.
10044 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20    int flags,    
10045 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10046 54 79 70 65 20 6f 66 20 74 65 73 74 20 74 6f 20  Type of test to 
10047 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66 69 6c  make on this fil
10048 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73  e */.  int *pRes
10049 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20 20  Out             
1004a 20 20 2f 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74    /* OUT: Result
1004b 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 61   */.){.  DWORD a
1004c 74 74 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ttr;.  int rc = 
1004d 30 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76  0;.  void *zConv
1004e 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55  erted = convertU
1004f 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c  tf8Filename(zFil
10050 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53 45 44  ename);.  UNUSED
10051 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
10052 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74  ;.  if( zConvert
10053 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ed==0 ){.    ret
10054 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
10055 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e 54  ;.  }.  if( isNT
10056 28 29 20 29 7b 0a 20 20 20 20 61 74 74 72 20 3d  () ){.    attr =
10057 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74   GetFileAttribut
10058 65 73 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e  esW((WCHAR*)zCon
10059 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54  verted);./* isNT
1005a 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54  () is 1 if SQLIT
1005b 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73  E_OS_WINCE==1, s
1005c 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e  o this else is n
1005d 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a  ever executed. .
1005e 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43  ** Since the ASC
1005f 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  II version of th
10060 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20  ese Windows API 
10061 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72  do not exist for
10062 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20   WINCE,.** it's 
10063 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74  important to not
10064 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20   reference them 
10065 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73  for WINCE builds
10066 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
10067 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65  OS_WINCE==0.  }e
10068 6c 73 65 7b 0a 20 20 20 20 61 74 74 72 20 3d 20  lse{.    attr = 
10069 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65  GetFileAttribute
1006a 73 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65  sA((char*)zConve
1006b 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20  rted);.#endif.  
1006c 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72  }.  free(zConver
1006d 74 65 64 29 3b 0a 20 20 73 77 69 74 63 68 28 20  ted);.  switch( 
1006e 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73  flags ){.    cas
1006f 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
10070 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53  READ:.    case S
10071 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
10072 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d 20  STS:.      rc = 
10073 61 74 74 72 21 3d 49 4e 56 41 4c 49 44 5f 46 49  attr!=INVALID_FI
10074 4c 45 5f 41 54 54 52 49 42 55 54 45 53 3b 0a 20  LE_ATTRIBUTES;. 
10075 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
10076 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45  case SQLITE_ACCE
10077 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20  SS_READWRITE:.  
10078 20 20 20 20 72 63 20 3d 20 28 61 74 74 72 20 26      rc = (attr &
10079 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f   FILE_ATTRIBUTE_
1007a 52 45 41 44 4f 4e 4c 59 29 3d 3d 30 3b 0a 20 20  READONLY)==0;.  
1007b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
1007c 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73  efault:.      as
1007d 73 65 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66  sert(!"Invalid f
1007e 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b  lags argument");
1007f 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20  .  }.  *pResOut 
10080 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53  = rc;.  return S
10081 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
10082 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74  .** Turn a relat
10083 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74  ive pathname int
10084 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d  o a full pathnam
10085 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 75  e.  Write the fu
10086 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69  ll.** pathname i
10087 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a 4f 75  nto zOut[].  zOu
10088 74 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 6c  t[] will be at l
10089 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 74  east pVfs->mxPat
1008a 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 69  hname.** bytes i
1008b 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69  n size..*/.stati
1008c 63 20 69 6e 74 20 77 69 6e 46 75 6c 6c 50 61 74  c int winFullPat
1008d 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33  hname(.  sqlite3
1008e 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
1008f 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
10090 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20  r to vfs object 
10091 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
10092 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20  *zRelative,     
10093 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72     /* Possibly r
10094 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 61  elative input pa
10095 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c  th */.  int nFul
10096 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
10097 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
10098 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69   output buffer i
10099 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61  n bytes */.  cha
1009a 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20  r *zFull        
1009b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75             /* Ou
1009c 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29  tput buffer */.)
1009d 7b 0a 20 20 0a 23 69 66 20 64 65 66 69 6e 65 64  {.  .#if defined
1009e 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 55  (__CYGWIN__).  U
1009f 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
100a0 6e 46 75 6c 6c 29 3b 0a 20 20 63 79 67 77 69 6e  nFull);.  cygwin
100a1 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c 6c 5f 77 69  _conv_to_full_wi
100a2 6e 33 32 5f 70 61 74 68 28 7a 52 65 6c 61 74 69  n32_path(zRelati
100a3 76 65 2c 20 7a 46 75 6c 6c 29 3b 0a 20 20 72 65  ve, zFull);.  re
100a4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
100a5 23 65 6e 64 69 66 0a 0a 23 69 66 20 53 51 4c 49  #endif..#if SQLI
100a6 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 55 4e  TE_OS_WINCE.  UN
100a7 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e  USED_PARAMETER(n
100a8 46 75 6c 6c 29 3b 0a 20 20 2f 2a 20 57 69 6e 43  Full);.  /* WinC
100a9 45 20 68 61 73 20 6e 6f 20 63 6f 6e 63 65 70 74  E has no concept
100aa 20 6f 66 20 61 20 72 65 6c 61 74 69 76 65 20 70   of a relative p
100ab 61 74 68 6e 61 6d 65 2c 20 6f 72 20 73 6f 20 49  athname, or so I
100ac 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f 0a 20 20 73   am told. */.  s
100ad 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
100ae 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
100af 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c 20 7a  , zFull, "%s", z
100b0 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 72 65 74  Relative);.  ret
100b1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23  urn SQLITE_OK;.#
100b2 65 6e 64 69 66 0a 0a 23 69 66 20 21 53 51 4c 49  endif..#if !SQLI
100b3 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26 20 21  TE_OS_WINCE && !
100b4 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e  defined(__CYGWIN
100b5 5f 5f 29 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b  __).  int nByte;
100b6 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72  .  void *zConver
100b7 74 65 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75  ted;.  char *zOu
100b8 74 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  t;.  UNUSED_PARA
100b9 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20  METER(nFull);.  
100ba 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e  zConverted = con
100bb 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
100bc 28 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 69  (zRelative);.  i
100bd 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
100be 20 57 43 48 41 52 20 2a 7a 54 65 6d 70 3b 0a 20   WCHAR *zTemp;. 
100bf 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75     nByte = GetFu
100c0 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48  llPathNameW((WCH
100c1 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20  AR*)zConverted, 
100c2 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20  0, 0, 0) + 3;.  
100c3 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63    zTemp = malloc
100c4 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a  ( nByte*sizeof(z
100c5 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20  Temp[0]) );.    
100c6 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a  if( zTemp==0 ){.
100c7 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76        free(zConv
100c8 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65  erted);.      re
100c9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
100ca 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74  M;.    }.    Get
100cb 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 57  FullPathNameW((W
100cc 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
100cd 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20  , nByte, zTemp, 
100ce 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f  0);.    free(zCo
100cf 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f  nverted);.    zO
100d0 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74  ut = unicodeToUt
100d1 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66  f8(zTemp);.    f
100d2 72 65 65 28 7a 54 65 6d 70 29 3b 0a 2f 2a 20 69  ree(zTemp);./* i
100d3 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51  sNT() is 1 if SQ
100d4 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31  LITE_OS_WINCE==1
100d5 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69  , so this else i
100d6 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64  s never executed
100d7 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20  . .** Since the 
100d8 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66  ASCII version of
100d9 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41   these Windows A
100da 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20  PI do not exist 
100db 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74  for WINCE,.** it
100dc 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  's important to 
100dd 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68  not reference th
100de 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69  em for WINCE bui
100df 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  lds..*/.#if SQLI
100e0 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20  TE_OS_WINCE==0. 
100e1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72   }else{.    char
100e2 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79   *zTemp;.    nBy
100e3 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68  te = GetFullPath
100e4 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 6f  NameA((char*)zCo
100e5 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30  nverted, 0, 0, 0
100e6 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70  ) + 3;.    zTemp
100e7 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65   = malloc( nByte
100e8 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d  *sizeof(zTemp[0]
100e9 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65  ) );.    if( zTe
100ea 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66  mp==0 ){.      f
100eb 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
100ec 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
100ed 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
100ee 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74  }.    GetFullPat
100ef 68 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 7a 43  hNameA((char*)zC
100f0 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c  onverted, nByte,
100f1 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20   zTemp, 0);.    
100f2 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
100f3 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c  ;.    zOut = sql
100f4 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f  ite3_win32_mbcs_
100f5 74 6f 5f 75 74 66 38 28 7a 54 65 6d 70 29 3b 0a  to_utf8(zTemp);.
100f6 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70 29 3b      free(zTemp);
100f7 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66  .#endif.  }.  if
100f8 28 20 7a 4f 75 74 20 29 7b 0a 20 20 20 20 73 71  ( zOut ){.    sq
100f9 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 70  lite3_snprintf(p
100fa 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2c  Vfs->mxPathname,
100fb 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c 20 7a 4f   zFull, "%s", zO
100fc 75 74 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 4f  ut);.    free(zO
100fd 75 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ut);.    return 
100fe 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
100ff 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  se{.    return S
10100 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
10101 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
10102 20 47 65 74 20 74 68 65 20 73 65 63 74 6f 72 20   Get the sector 
10103 73 69 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69  size of the devi
10104 63 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ce used to store
10105 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  .** file..*/.sta
10106 74 69 63 20 69 6e 74 20 67 65 74 53 65 63 74 6f  tic int getSecto
10107 72 53 69 7a 65 28 0a 20 20 20 20 73 71 6c 69 74  rSize(.    sqlit
10108 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 0a 20 20  e3_vfs *pVfs,.  
10109 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52    const char *zR
1010a 65 6c 61 74 69 76 65 20 20 20 20 20 2f 2a 20 55  elative     /* U
1010b 54 46 2d 38 20 66 69 6c 65 20 6e 61 6d 65 20 2a  TF-8 file name *
1010c 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 62 79 74  /.){.  DWORD byt
1010d 65 73 50 65 72 53 65 63 74 6f 72 20 3d 20 53 51  esPerSector = SQ
1010e 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43  LITE_DEFAULT_SEC
1010f 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 2f 2a 20 47  TOR_SIZE;.  /* G
10110 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 20  etDiskFreeSpace 
10111 69 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64  is not supported
10112 20 75 6e 64 65 72 20 57 49 4e 43 45 20 2a 2f 0a   under WINCE */.
10113 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
10114 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  NCE.  UNUSED_PAR
10115 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
10116 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
10117 28 7a 52 65 6c 61 74 69 76 65 29 3b 0a 23 65 6c  (zRelative);.#el
10118 73 65 0a 20 20 63 68 61 72 20 7a 46 75 6c 6c 70  se.  char zFullp
10119 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b  ath[MAX_PATH+1];
1011a 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 57 4f  .  int rc;.  DWO
1011b 52 44 20 64 77 52 65 74 20 3d 20 30 3b 0a 20 20  RD dwRet = 0;.  
1011c 44 57 4f 52 44 20 64 77 44 75 6d 6d 79 3b 0a 0a  DWORD dwDummy;..
1011d 20 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 6e 65 65    /*.  ** We nee
1011e 64 20 74 6f 20 67 65 74 20 74 68 65 20 66 75 6c  d to get the ful
1011f 6c 20 70 61 74 68 20 6e 61 6d 65 20 6f 66 20 74  l path name of t
10120 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 6f 20  he file.  ** to 
10121 67 65 74 20 74 68 65 20 64 72 69 76 65 20 6c 65  get the drive le
10122 74 74 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 20  tter to look up 
10123 74 68 65 20 73 65 63 74 6f 72 0a 20 20 2a 2a 20  the sector.  ** 
10124 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  size..  */.  rc 
10125 3d 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d  = winFullPathnam
10126 65 28 70 56 66 73 2c 20 7a 52 65 6c 61 74 69 76  e(pVfs, zRelativ
10127 65 2c 20 4d 41 58 5f 50 41 54 48 2c 20 7a 46 75  e, MAX_PATH, zFu
10128 6c 6c 70 61 74 68 29 3b 0a 20 20 69 66 28 20 72  llpath);.  if( r
10129 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29  c == SQLITE_OK )
1012a 0a 20 20 7b 0a 20 20 20 20 76 6f 69 64 20 2a 7a  .  {.    void *z
1012b 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76  Converted = conv
1012c 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28  ertUtf8Filename(
1012d 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20 20 20  zFullpath);.    
1012e 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 20 29  if( zConverted )
1012f 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 4e 54  {.      if( isNT
10130 28 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  () ){.        /*
10131 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a 75   trim path to ju
10132 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 6e  st drive referen
10133 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 57 43  ce */.        WC
10134 48 41 52 20 2a 70 20 3d 20 7a 43 6f 6e 76 65 72  HAR *p = zConver
10135 74 65 64 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  ted;.        for
10136 28 3b 2a 70 3b 70 2b 2b 29 7b 0a 20 20 20 20 20  (;*p;p++){.     
10137 20 20 20 20 20 69 66 28 20 2a 70 20 3d 3d 20 27       if( *p == '
10138 5c 5c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  \\' ){.         
10139 20 20 20 2a 70 20 3d 20 27 5c 30 27 3b 0a 20 20     *p = '\0';.  
1013a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
1013b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1013c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
1013d 77 52 65 74 20 3d 20 47 65 74 44 69 73 6b 46 72  wRet = GetDiskFr
1013e 65 65 53 70 61 63 65 57 28 28 57 43 48 41 52 2a  eeSpaceW((WCHAR*
1013f 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20  )zConverted,.   
10140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10141 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
10142 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 20 20  dwDummy,.       
10143 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10144 20 20 20 20 20 20 20 20 20 20 20 26 62 79 74 65             &byte
10145 73 50 65 72 53 65 63 74 6f 72 2c 0a 20 20 20 20  sPerSector,.    
10146 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10147 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64                &d
10148 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 20 20 20  wDummy,.        
10149 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1014a 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d            &dwDum
1014b 6d 79 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  my);.      }else
1014c 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 74 72 69  {.        /* tri
1014d 6d 20 70 61 74 68 20 74 6f 20 6a 75 73 74 20 64  m path to just d
1014e 72 69 76 65 20 72 65 66 65 72 65 6e 63 65 20 2a  rive reference *
1014f 2f 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  /.        char *
10150 70 20 3d 20 28 63 68 61 72 20 2a 29 7a 43 6f 6e  p = (char *)zCon
10151 76 65 72 74 65 64 3b 0a 20 20 20 20 20 20 20 20  verted;.        
10152 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 7b 0a 20 20  for(;*p;p++){.  
10153 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 20 3d          if( *p =
10154 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 20 20 20 20  = '\\' ){.      
10155 20 20 20 20 20 20 2a 70 20 3d 20 27 5c 30 27 3b        *p = '\0';
10156 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
10157 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
10158 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
10159 20 20 64 77 52 65 74 20 3d 20 47 65 74 44 69 73    dwRet = GetDis
1015a 6b 46 72 65 65 53 70 61 63 65 41 28 28 63 68 61  kFreeSpaceA((cha
1015b 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20  r*)zConverted,. 
1015c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1015d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1015e 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20   &dwDummy,.     
1015f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10160 20 20 20 20 20 20 20 20 20 20 20 20 20 26 62 79               &by
10161 74 65 73 50 65 72 53 65 63 74 6f 72 2c 0a 20 20  tesPerSector,.  
10162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10163 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10164 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 20  &dwDummy,.      
10165 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10166 20 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44              &dwD
10167 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ummy);.      }. 
10168 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65       free(zConve
10169 72 74 65 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rted);.    }.   
1016a 20 69 66 28 20 21 64 77 52 65 74 20 29 7b 0a 20   if( !dwRet ){. 
1016b 20 20 20 20 20 62 79 74 65 73 50 65 72 53 65 63       bytesPerSec
1016c 74 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44 45 46  tor = SQLITE_DEF
1016d 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45  AULT_SECTOR_SIZE
1016e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
1016f 69 66 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74  if.  return (int
10170 29 20 62 79 74 65 73 50 65 72 53 65 63 74 6f 72  ) bytesPerSector
10171 3b 20 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  ; .}..#ifndef SQ
10172 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
10173 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49  XTENSION./*.** I
10174 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70  nterfaces for op
10175 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c  ening a shared l
10176 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20  ibrary, finding 
10177 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20  entry points.** 
10178 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65  within the share
10179 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63  d library, and c
1017a 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65  losing the share
1017b 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a  d library..*/./*
1017c 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66  .** Interfaces f
1017d 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61  or opening a sha
1017e 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e  red library, fin
1017f 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74  ding entry point
10180 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20  s.** within the 
10181 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20  shared library, 
10182 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20  and closing the 
10183 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a  shared library..
10184 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
10185 77 69 6e 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65  winDlOpen(sqlite
10186 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e  3_vfs *pVfs, con
10187 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
10188 6d 65 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b  me){.  HANDLE h;
10189 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72  .  void *zConver
1018a 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  ted = convertUtf
1018b 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e  8Filename(zFilen
1018c 61 6d 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ame);.  UNUSED_P
1018d 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a  ARAMETER(pVfs);.
1018e 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64    if( zConverted
1018f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
10190 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  n 0;.  }.  if( i
10191 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 20 3d  sNT() ){.    h =
10192 20 4c 6f 61 64 4c 69 62 72 61 72 79 57 28 28 57   LoadLibraryW((W
10193 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
10194 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
10195 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
10196 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
10197 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
10198 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e  xecuted. .** Sin
10199 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72  ce the ASCII ver
1019a 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69  sion of these Wi
1019b 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74  ndows API do not
1019c 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45   exist for WINCE
1019d 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74  ,.** it's import
1019e 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72  ant to not refer
1019f 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49  ence them for WI
101a0 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23  NCE builds..*/.#
101a1 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
101a2 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
101a3 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61     h = LoadLibra
101a4 72 79 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76  ryA((char*)zConv
101a5 65 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20  erted);.#endif. 
101a6 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65   }.  free(zConve
101a7 72 74 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  rted);.  return 
101a8 28 76 6f 69 64 2a 29 68 3b 0a 7d 0a 73 74 61 74  (void*)h;.}.stat
101a9 69 63 20 76 6f 69 64 20 77 69 6e 44 6c 45 72 72  ic void winDlErr
101aa 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  or(sqlite3_vfs *
101ab 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20  pVfs, int nBuf, 
101ac 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a  char *zBufOut){.
101ad 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
101ae 45 52 28 70 56 66 73 29 3b 0a 20 20 67 65 74 4c  ER(pVfs);.  getL
101af 61 73 74 45 72 72 6f 72 4d 73 67 28 6e 42 75 66  astErrorMsg(nBuf
101b0 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 76 6f  , zBufOut);.}.vo
101b1 69 64 20 28 2a 77 69 6e 44 6c 53 79 6d 28 73 71  id (*winDlSym(sq
101b2 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
101b3 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20   void *pHandle, 
101b4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d  const char *zSym
101b5 62 6f 6c 29 29 28 76 6f 69 64 29 7b 0a 20 20 55  bol))(void){.  U
101b6 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
101b7 70 56 66 73 29 3b 0a 23 69 66 20 53 51 4c 49 54  pVfs);.#if SQLIT
101b8 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20  E_OS_WINCE.  /* 
101b9 54 68 65 20 47 65 74 50 72 6f 63 41 64 64 72 65  The GetProcAddre
101ba 73 73 41 28 29 20 72 6f 75 74 69 6e 65 20 69 73  ssA() routine is
101bb 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
101bc 6f 6e 20 77 69 6e 63 65 2e 20 2a 2f 0a 20 20 72  on wince. */.  r
101bd 65 74 75 72 6e 20 28 76 6f 69 64 28 2a 29 28 76  eturn (void(*)(v
101be 6f 69 64 29 29 47 65 74 50 72 6f 63 41 64 64 72  oid))GetProcAddr
101bf 65 73 73 41 28 28 48 41 4e 44 4c 45 29 70 48 61  essA((HANDLE)pHa
101c0 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a  ndle, zSymbol);.
101c1 23 65 6c 73 65 0a 20 20 2f 2a 20 41 6c 6c 20 6f  #else.  /* All o
101c2 74 68 65 72 20 77 69 6e 64 6f 77 73 20 70 6c 61  ther windows pla
101c3 74 66 6f 72 6d 73 20 65 78 70 65 63 74 20 47 65  tforms expect Ge
101c4 74 50 72 6f 63 41 64 64 72 65 73 73 28 29 20 74  tProcAddress() t
101c5 6f 20 74 61 6b 65 0a 20 20 2a 2a 20 61 6e 20 41  o take.  ** an A
101c6 6e 73 69 20 73 74 72 69 6e 67 20 72 65 67 61 72  nsi string regar
101c7 64 6c 65 73 73 20 6f 66 20 74 68 65 20 5f 55 4e  dless of the _UN
101c8 49 43 4f 44 45 20 73 65 74 74 69 6e 67 20 2a 2f  ICODE setting */
101c9 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 28  .  return (void(
101ca 2a 29 28 76 6f 69 64 29 29 47 65 74 50 72 6f 63  *)(void))GetProc
101cb 41 64 64 72 65 73 73 28 28 48 41 4e 44 4c 45 29  Address((HANDLE)
101cc 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c  pHandle, zSymbol
101cd 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 76 6f 69 64  );.#endif.}.void
101ce 20 77 69 6e 44 6c 43 6c 6f 73 65 28 73 71 6c 69   winDlClose(sqli
101cf 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76  te3_vfs *pVfs, v
101d0 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20  oid *pHandle){. 
101d1 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
101d2 52 28 70 56 66 73 29 3b 0a 20 20 46 72 65 65 4c  R(pVfs);.  FreeL
101d3 69 62 72 61 72 79 28 28 48 41 4e 44 4c 45 29 70  ibrary((HANDLE)p
101d4 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65  Handle);.}.#else
101d5 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d   /* if SQLITE_OM
101d6 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
101d7 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f  N is defined: */
101d8 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c  .  #define winDl
101d9 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e  Open  0.  #defin
101da 65 20 77 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20  e winDlError 0. 
101db 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 53 79   #define winDlSy
101dc 6d 20 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20  m   0.  #define 
101dd 77 69 6e 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e  winDlClose 0.#en
101de 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  dif.../*.** Writ
101df 65 20 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74  e up to nBuf byt
101e0 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73  es of randomness
101e1 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73   into zBuf..*/.s
101e2 74 61 74 69 63 20 69 6e 74 20 77 69 6e 52 61 6e  tatic int winRan
101e3 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f  domness(sqlite3_
101e4 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
101e5 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29  Buf, char *zBuf)
101e6 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20  {.  int n = 0;. 
101e7 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
101e8 52 28 70 56 66 73 29 3b 0a 23 69 66 20 64 65 66  R(pVfs);.#if def
101e9 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
101ea 29 0a 20 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20  ).  n = nBuf;.  
101eb 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20  memset(zBuf, 0, 
101ec 6e 42 75 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69  nBuf);.#else.  i
101ed 66 28 20 73 69 7a 65 6f 66 28 53 59 53 54 45 4d  f( sizeof(SYSTEM
101ee 54 49 4d 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b  TIME)<=nBuf-n ){
101ef 0a 20 20 20 20 53 59 53 54 45 4d 54 49 4d 45 20  .    SYSTEMTIME 
101f0 78 3b 0a 20 20 20 20 47 65 74 53 79 73 74 65 6d  x;.    GetSystem
101f1 54 69 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d 65  Time(&x);.    me
101f2 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
101f3 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20  x, sizeof(x));. 
101f4 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78     n += sizeof(x
101f5 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a  );.  }.  if( siz
101f6 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66  eof(DWORD)<=nBuf
101f7 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20  -n ){.    DWORD 
101f8 70 69 64 20 3d 20 47 65 74 43 75 72 72 65 6e 74  pid = GetCurrent
101f9 50 72 6f 63 65 73 73 49 64 28 29 3b 0a 20 20 20  ProcessId();.   
101fa 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
101fb 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 70  , &pid, sizeof(p
101fc 69 64 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73  id));.    n += s
101fd 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20 7d 0a  izeof(pid);.  }.
101fe 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f    if( sizeof(DWO
101ff 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20  RD)<=nBuf-n ){. 
10200 20 20 20 44 57 4f 52 44 20 63 6e 74 20 3d 20 47     DWORD cnt = G
10201 65 74 54 69 63 6b 43 6f 75 6e 74 28 29 3b 0a 20  etTickCount();. 
10202 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
10203 6e 5d 2c 20 26 63 6e 74 2c 20 73 69 7a 65 6f 66  n], &cnt, sizeof
10204 28 63 6e 74 29 29 3b 0a 20 20 20 20 6e 20 2b 3d  (cnt));.    n +=
10205 20 73 69 7a 65 6f 66 28 63 6e 74 29 3b 0a 20 20   sizeof(cnt);.  
10206 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 4c  }.  if( sizeof(L
10207 41 52 47 45 5f 49 4e 54 45 47 45 52 29 3c 3d 6e  ARGE_INTEGER)<=n
10208 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 4c 41 52  Buf-n ){.    LAR
10209 47 45 5f 49 4e 54 45 47 45 52 20 69 3b 0a 20 20  GE_INTEGER i;.  
1020a 20 20 51 75 65 72 79 50 65 72 66 6f 72 6d 61 6e    QueryPerforman
1020b 63 65 43 6f 75 6e 74 65 72 28 26 69 29 3b 0a 20  ceCounter(&i);. 
1020c 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
1020d 6e 5d 2c 20 26 69 2c 20 73 69 7a 65 6f 66 28 69  n], &i, sizeof(i
1020e 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ));.    n += siz
1020f 65 6f 66 28 69 29 3b 0a 20 20 7d 0a 23 65 6e 64  eof(i);.  }.#end
10210 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d  if.  return n;.}
10211 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66  .../*.** Sleep f
10212 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c  or a little whil
10213 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61  e.  Return the a
10214 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c  mount of time sl
10215 65 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ept..*/.static i
10216 6e 74 20 77 69 6e 53 6c 65 65 70 28 73 71 6c 69  nt winSleep(sqli
10217 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
10218 6e 74 20 6d 69 63 72 6f 73 65 63 29 7b 0a 20 20  nt microsec){.  
10219 53 6c 65 65 70 28 28 6d 69 63 72 6f 73 65 63 2b  Sleep((microsec+
1021a 39 39 39 29 2f 31 30 30 30 29 3b 0a 20 20 55 4e  999)/1000);.  UN
1021b 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
1021c 56 66 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  Vfs);.  return (
1021d 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31  (microsec+999)/1
1021e 30 30 30 29 2a 31 30 30 30 3b 0a 7d 0a 0a 2f 2a  000)*1000;.}../*
1021f 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
10220 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73  g variable, if s
10221 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  et to a non-zero
10222 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20   value, becomes 
10223 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65  the result.** re
10224 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  turned from sqli
10225 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
10226 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  ().  This is use
10227 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a  d for testing..*
10228 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
10229 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
1022a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72  int sqlite3_curr
1022b 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65  ent_time = 0;.#e
1022c 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  ndif../*.** Find
1022d 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d   the current tim
1022e 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20  e (in Universal 
1022f 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65  Coordinated Time
10230 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a  ).  Write the.**
10231 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e   current time an
10232 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69  d date as a Juli
10233 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e  an Day number in
10234 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a  to *prNow and.**
10235 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75   return 0.  Retu
10236 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65  rn 1 if the time
10237 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74   and date cannot
10238 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e   be found..*/.in
10239 74 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65  t winCurrentTime
1023a 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
1023b 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f  fs, double *prNo
1023c 77 29 7b 0a 20 20 46 49 4c 45 54 49 4d 45 20 66  w){.  FILETIME f
1023d 74 3b 0a 20 20 2f 2a 20 46 49 4c 45 54 49 4d 45  t;.  /* FILETIME
1023e 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 20   structure is a 
1023f 36 34 2d 62 69 74 20 76 61 6c 75 65 20 72 65 70  64-bit value rep
10240 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 6e 75  resenting the nu
10241 6d 62 65 72 20 6f 66 20 0a 20 20 20 20 20 31 30  mber of .     10
10242 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74  0-nanosecond int
10243 65 72 76 61 6c 73 20 73 69 6e 63 65 20 4a 61 6e  ervals since Jan
10244 75 61 72 79 20 31 2c 20 31 36 30 31 20 28 3d 20  uary 1, 1601 (= 
10245 4a 44 20 32 33 30 35 38 31 33 2e 35 29 2e 20 0a  JD 2305813.5). .
10246 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69    */.  sqlite3_i
10247 6e 74 36 34 20 74 69 6d 65 57 3b 20 20 20 2f 2a  nt64 timeW;   /*
10248 20 57 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20   Whole days */. 
10249 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74   sqlite3_int64 t
1024a 69 6d 65 46 3b 20 20 20 2f 2a 20 46 72 61 63 74  imeF;   /* Fract
1024b 69 6f 6e 61 6c 20 44 61 79 73 20 2a 2f 0a 0a 20  ional Days */.. 
1024c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30   /* Number of 10
1024d 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74  0-nanosecond int
1024e 65 72 76 61 6c 73 20 69 6e 20 61 20 73 69 6e 67  ervals in a sing
1024f 6c 65 20 64 61 79 20 2a 2f 0a 20 20 73 74 61 74  le day */.  stat
10250 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
10251 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 44 61 79  _int64 ntuPerDay
10252 20 3d 20 0a 20 20 20 20 20 20 31 30 30 30 30 30   = .      100000
10253 30 30 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36  00*(sqlite3_int6
10254 34 29 38 36 34 30 30 3b 0a 0a 20 20 2f 2a 20 4e  4)86400;..  /* N
10255 75 6d 62 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e  umber of 100-nan
10256 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c  osecond interval
10257 73 20 69 6e 20 68 61 6c 66 20 6f 66 20 61 20 64  s in half of a d
10258 61 79 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63  ay */.  static c
10259 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74  onst sqlite3_int
1025a 36 34 20 6e 74 75 50 65 72 48 61 6c 66 44 61 79  64 ntuPerHalfDay
1025b 20 3d 20 0a 20 20 20 20 20 20 31 30 30 30 30 30   = .      100000
1025c 30 30 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36  00*(sqlite3_int6
1025d 34 29 34 33 32 30 30 3b 0a 0a 20 20 2f 2a 20 32  4)43200;..  /* 2
1025e 5e 33 32 20 2d 20 74 6f 20 61 76 6f 69 64 20 75  ^32 - to avoid u
1025f 73 65 20 6f 66 20 4c 4c 20 61 6e 64 20 77 61 72  se of LL and war
10260 6e 69 6e 67 73 20 69 6e 20 67 63 63 20 2a 2f 0a  nings in gcc */.
10261 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
10262 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6d 61 78  qlite3_int64 max
10263 33 32 42 69 74 56 61 6c 75 65 20 3d 20 0a 20 20  32BitValue = .  
10264 20 20 20 20 28 73 71 6c 69 74 65 33 5f 69 6e 74      (sqlite3_int
10265 36 34 29 32 30 30 30 30 30 30 30 30 30 20 2b 20  64)2000000000 + 
10266 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32  (sqlite3_int64)2
10267 30 30 30 30 30 30 30 30 30 20 2b 20 28 73 71 6c  000000000 + (sql
10268 69 74 65 33 5f 69 6e 74 36 34 29 32 39 34 39 36  ite3_int64)29496
10269 37 32 39 36 3b 0a 0a 23 69 66 20 53 51 4c 49 54  7296;..#if SQLIT
1026a 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 53 59 53  E_OS_WINCE.  SYS
1026b 54 45 4d 54 49 4d 45 20 74 69 6d 65 3b 0a 20 20  TEMTIME time;.  
1026c 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 74  GetSystemTime(&t
1026d 69 6d 65 29 3b 0a 20 20 2f 2a 20 69 66 20 53 79  ime);.  /* if Sy
1026e 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69  stemTimeToFileTi
1026f 6d 65 28 29 20 66 61 69 6c 73 2c 20 69 74 20 72  me() fails, it r
10270 65 74 75 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f 0a  eturns zero. */.
10271 20 20 69 66 20 28 21 53 79 73 74 65 6d 54 69 6d    if (!SystemTim
10272 65 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 6d  eToFileTime(&tim
10273 65 2c 26 66 74 29 29 7b 0a 20 20 20 20 72 65 74  e,&ft)){.    ret
10274 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6c 73 65  urn 1;.  }.#else
10275 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65  .  GetSystemTime
10276 41 73 46 69 6c 65 54 69 6d 65 28 20 26 66 74 20  AsFileTime( &ft 
10277 29 3b 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53  );.#endif.  UNUS
10278 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
10279 73 29 3b 0a 20 20 74 69 6d 65 57 20 3d 20 28 28  s);.  timeW = ((
1027a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 66  (sqlite3_int64)f
1027b 74 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d 65  t.dwHighDateTime
1027c 29 2a 6d 61 78 33 32 42 69 74 56 61 6c 75 65 29  )*max32BitValue)
1027d 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   + (sqlite3_int6
1027e 34 29 66 74 2e 64 77 4c 6f 77 44 61 74 65 54 69  4)ft.dwLowDateTi
1027f 6d 65 3b 0a 20 20 74 69 6d 65 46 20 3d 20 74 69  me;.  timeF = ti
10280 6d 65 57 20 25 20 6e 74 75 50 65 72 44 61 79 3b  meW % ntuPerDay;
10281 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 72 61            /* fra
10282 63 74 69 6f 6e 61 6c 20 64 61 79 73 20 28 31 30  ctional days (10
10283 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 73 29 20 2a  0-nanoseconds) *
10284 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65  /.  timeW = time
10285 57 20 2f 20 6e 74 75 50 65 72 44 61 79 3b 20 20  W / ntuPerDay;  
10286 20 20 20 20 20 20 20 20 2f 2a 20 77 68 6f 6c 65          /* whole
10287 20 64 61 79 73 20 2a 2f 0a 20 20 74 69 6d 65 57   days */.  timeW
10288 20 3d 20 74 69 6d 65 57 20 2b 20 32 33 30 35 38   = timeW + 23058
10289 31 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  13;            /
1028a 2a 20 61 64 64 20 77 68 6f 6c 65 20 64 61 79 73  * add whole days
1028b 20 28 66 72 6f 6d 20 32 33 30 35 38 31 33 2e 35   (from 2305813.5
1028c 29 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74  ) */.  timeF = t
1028d 69 6d 65 46 20 2b 20 6e 74 75 50 65 72 48 61 6c  imeF + ntuPerHal
1028e 66 44 61 79 3b 20 20 20 20 20 20 2f 2a 20 61 64  fDay;      /* ad
1028f 64 20 68 61 6c 66 20 61 20 64 61 79 20 28 66 72  d half a day (fr
10290 6f 6d 20 32 33 30 35 38 31 33 2e 35 29 20 2a 2f  om 2305813.5) */
10291 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 57  .  timeW = timeW
10292 20 2b 20 28 74 69 6d 65 46 2f 6e 74 75 50 65 72   + (timeF/ntuPer
10293 44 61 79 29 3b 20 20 2f 2a 20 61 64 64 20 77 68  Day);  /* add wh
10294 6f 6c 65 20 64 61 79 20 69 66 20 68 61 6c 66 20  ole day if half 
10295 64 61 79 20 6d 61 64 65 20 6f 6e 65 20 2a 2f 0a  day made one */.
10296 20 20 74 69 6d 65 46 20 3d 20 74 69 6d 65 46 20    timeF = timeF 
10297 25 20 6e 74 75 50 65 72 44 61 79 3b 20 20 20 20  % ntuPerDay;    
10298 20 20 20 20 20 20 2f 2a 20 63 6f 6d 70 75 74 65        /* compute
10299 20 6e 65 77 20 66 72 61 63 74 69 6f 6e 61 6c 20   new fractional 
1029a 64 61 79 73 20 2a 2f 0a 20 20 2a 70 72 4e 6f 77  days */.  *prNow
1029b 20 3d 20 28 64 6f 75 62 6c 65 29 74 69 6d 65 57   = (double)timeW
1029c 20 2b 20 28 28 64 6f 75 62 6c 65 29 74 69 6d 65   + ((double)time
1029d 46 20 2f 20 28 64 6f 75 62 6c 65 29 6e 74 75 50  F / (double)ntuP
1029e 65 72 44 61 79 29 3b 0a 23 69 66 64 65 66 20 53  erDay);.#ifdef S
1029f 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28  QLITE_TEST.  if(
102a0 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
102a1 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72  _time ){.    *pr
102a2 4e 6f 77 20 3d 20 28 28 64 6f 75 62 6c 65 29 73  Now = ((double)s
102a3 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74  qlite3_current_t
102a4 69 6d 65 20 2b 20 28 64 6f 75 62 6c 65 29 34 33  ime + (double)43
102a5 32 30 30 29 20 2f 20 28 64 6f 75 62 6c 65 29 38  200) / (double)8
102a6 36 34 30 30 20 2b 20 28 64 6f 75 62 6c 65 29 32  6400 + (double)2
102a7 34 34 30 35 38 37 3b 0a 20 20 7d 0a 23 65 6e 64  440587;.  }.#end
102a8 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  if.  return 0;.}
102a9 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 64 65 61  ../*.** The idea
102aa 20 69 73 20 74 68 61 74 20 74 68 69 73 20 66 75   is that this fu
102ab 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b  nction works lik
102ac 65 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  e a combination 
102ad 6f 66 0a 2a 2a 20 47 65 74 4c 61 73 74 45 72 72  of.** GetLastErr
102ae 6f 72 28 29 20 61 6e 64 20 46 6f 72 6d 61 74 4d  or() and FormatM
102af 65 73 73 61 67 65 28 29 20 6f 6e 20 77 69 6e 64  essage() on wind
102b0 6f 77 73 20 28 6f 72 20 65 72 72 6e 6f 20 61 6e  ows (or errno an
102b1 64 0a 2a 2a 20 73 74 72 65 72 72 6f 72 5f 72 28  d.** strerror_r(
102b2 29 20 6f 6e 20 75 6e 69 78 29 2e 20 41 66 74 65  ) on unix). Afte
102b3 72 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65  r an error is re
102b4 74 75 72 6e 65 64 20 62 79 20 61 6e 20 4f 53 0a  turned by an OS.
102b5 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 4c  ** function, SQL
102b6 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 66  ite calls this f
102b7 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 7a 42 75  unction with zBu
102b8 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a  f pointing to.**
102b9 20 61 20 62 75 66 66 65 72 20 6f 66 20 6e 42 75   a buffer of nBu
102ba 66 20 62 79 74 65 73 2e 20 54 68 65 20 4f 53 20  f bytes. The OS 
102bb 6c 61 79 65 72 20 73 68 6f 75 6c 64 20 70 6f 70  layer should pop
102bc 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20 62 75 66  ulate the.** buf
102bd 66 65 72 20 77 69 74 68 20 61 20 6e 75 6c 2d 74  fer with a nul-t
102be 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20  erminated UTF-8 
102bf 65 6e 63 6f 64 65 64 20 65 72 72 6f 72 20 6d 65  encoded error me
102c0 73 73 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 62  ssage.** describ
102c1 69 6e 67 20 74 68 65 20 6c 61 73 74 20 49 4f 20  ing the last IO 
102c2 65 72 72 6f 72 20 74 6f 20 68 61 76 65 20 6f 63  error to have oc
102c3 63 75 72 72 65 64 20 77 69 74 68 69 6e 20 74 68  curred within th
102c4 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 72  e calling.** thr
102c5 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ead..**.** If th
102c6 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
102c7 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72  is too large for
102c8 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 75   the supplied bu
102c9 66 66 65 72 2c 0a 2a 2a 20 69 74 20 73 68 6f 75  ffer,.** it shou
102ca 6c 64 20 62 65 20 74 72 75 6e 63 61 74 65 64 2e  ld be truncated.
102cb 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
102cc 65 20 6f 66 20 78 47 65 74 4c 61 73 74 45 72 72  e of xGetLastErr
102cd 6f 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66  or.** is zero if
102ce 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
102cf 67 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62  ge fits in the b
102d0 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65  uffer, or non-ze
102d1 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20  ro.** otherwise 
102d2 28 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20  (if the message 
102d3 77 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 20  was truncated). 
102d4 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72  If non-zero is r
102d5 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 74 68 65 6e  eturned,.** then
102d6 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73   it is not neces
102d7 73 61 72 79 20 74 6f 20 69 6e 63 6c 75 64 65 20  sary to include 
102d8 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  the nul-terminat
102d9 6f 72 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20  or character.** 
102da 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  in the output bu
102db 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20  ffer..**.** Not 
102dc 73 75 70 70 6c 79 69 6e 67 20 61 6e 20 65 72 72  supplying an err
102dd 6f 72 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20  or message will 
102de 68 61 76 65 20 6e 6f 20 61 64 76 65 72 73 65 20  have no adverse 
102df 65 66 66 65 63 74 0a 2a 2a 20 6f 6e 20 53 51 4c  effect.** on SQL
102e0 69 74 65 2e 20 49 74 20 69 73 20 66 69 6e 65 20  ite. It is fine 
102e1 74 6f 20 68 61 76 65 20 61 6e 20 69 6d 70 6c 65  to have an imple
102e2 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6e  mentation that n
102e3 65 76 65 72 0a 2a 2a 20 72 65 74 75 72 6e 73 20  ever.** returns 
102e4 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
102e5 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 6e 74 20 78 47  :.**.**   int xG
102e6 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69  etLastError(sqli
102e7 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
102e8 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
102e9 42 75 66 29 7b 0a 2a 2a 20 20 20 20 20 61 73 73  Buf){.**     ass
102ea 65 72 74 28 7a 42 75 66 5b 30 5d 3d 3d 27 5c 30  ert(zBuf[0]=='\0
102eb 27 29 3b 0a 2a 2a 20 20 20 20 20 72 65 74 75 72  ');.**     retur
102ec 6e 20 30 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a  n 0;.**   }.**.*
102ed 2a 20 48 6f 77 65 76 65 72 20 69 66 20 61 6e 20  * However if an 
102ee 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73  error message is
102ef 20 73 75 70 70 6c 69 65 64 2c 20 69 74 20 77 69   supplied, it wi
102f0 6c 6c 20 62 65 20 69 6e 63 6f 72 70 6f 72 61 74  ll be incorporat
102f1 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 20  ed.** by sqlite 
102f2 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d  into the error m
102f3 65 73 73 61 67 65 20 61 76 61 69 6c 61 62 6c 65  essage available
102f4 20 74 6f 20 74 68 65 20 75 73 65 72 20 75 73 69   to the user usi
102f5 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72  ng.** sqlite3_er
102f6 72 6d 73 67 28 29 2c 20 70 6f 73 73 69 62 6c 79  rmsg(), possibly
102f7 20 6d 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f 72   making IO error
102f8 73 20 65 61 73 69 65 72 20 74 6f 20 64 65 62 75  s easier to debu
102f9 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
102fa 20 77 69 6e 47 65 74 4c 61 73 74 45 72 72 6f 72   winGetLastError
102fb 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
102fc 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68  fs, int nBuf, ch
102fd 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e 55  ar *zBuf){.  UNU
102fe 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56  SED_PARAMETER(pV
102ff 66 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 65  fs);.  return ge
10300 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 6e 42  tLastErrorMsg(nB
10301 75 66 2c 20 7a 42 75 66 29 3b 0a 7d 0a 0a 2f 2a  uf, zBuf);.}../*
10302 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
10303 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20  nd deinitialize 
10304 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
10305 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a  stem interface..
10306 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
10307 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  t sqlite3_os_ini
10308 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  t(void){.  stati
10309 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 77 69  c sqlite3_vfs wi
1030a 6e 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20  nVfs = {.    1, 
1030b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1030c 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
1030d 20 20 20 73 69 7a 65 6f 66 28 77 69 6e 46 69 6c     sizeof(winFil
1030e 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c  e),   /* szOsFil
1030f 65 20 2a 2f 0a 20 20 20 20 4d 41 58 5f 50 41 54  e */.    MAX_PAT
10310 48 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d  H,          /* m
10311 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20  xPathname */.   
10312 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
10313 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a      /* pNext */.
10314 20 20 20 20 22 77 69 6e 33 32 22 2c 20 20 20 20      "win32",    
10315 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20         /* zName 
10316 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
10317 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 70            /* pAp
10318 70 44 61 74 61 20 2a 2f 0a 20 0a 20 20 20 20 77  pData */. .    w
10319 69 6e 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20  inOpen,         
1031a 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 20    /* xOpen */.  
1031b 20 20 77 69 6e 44 65 6c 65 74 65 2c 20 20 20 20    winDelete,    
1031c 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20       /* xDelete 
1031d 2a 2f 0a 20 20 20 20 77 69 6e 41 63 63 65 73 73  */.    winAccess
1031e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63  ,         /* xAc
1031f 63 65 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 46  cess */.    winF
10320 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 2f  ullPathname,   /
10321 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20  * xFullPathname 
10322 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 4f 70 65 6e  */.    winDlOpen
10323 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c  ,         /* xDl
10324 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44  Open */.    winD
10325 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 2f  lError,        /
10326 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20 20  * xDlError */.  
10327 20 20 77 69 6e 44 6c 53 79 6d 2c 20 20 20 20 20    winDlSym,     
10328 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a       /* xDlSym *
10329 2f 0a 20 20 20 20 77 69 6e 44 6c 43 6c 6f 73 65  /.    winDlClose
1032a 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43  ,        /* xDlC
1032b 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 77 69 6e 52  lose */.    winR
1032c 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 2f  andomness,     /
1032d 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f  * xRandomness */
1032e 0a 20 20 20 20 77 69 6e 53 6c 65 65 70 2c 20 20  .    winSleep,  
1032f 20 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65          /* xSlee
10330 70 20 2a 2f 0a 20 20 20 20 77 69 6e 43 75 72 72  p */.    winCurr
10331 65 6e 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20 78  entTime,    /* x
10332 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a 20  CurrentTime */. 
10333 20 20 20 77 69 6e 47 65 74 4c 61 73 74 45 72 72     winGetLastErr
10334 6f 72 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73  or    /* xGetLas
10335 74 45 72 72 6f 72 20 2a 2f 0a 20 20 7d 3b 0a 0a  tError */.  };..
10336 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65    sqlite3_vfs_re
10337 67 69 73 74 65 72 28 26 77 69 6e 56 66 73 2c 20  gister(&winVfs, 
10338 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  1);.  return SQL
10339 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53 51 4c 49 54  ITE_OK; .}.SQLIT
1033a 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1033b 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20  3_os_end(void){ 
1033c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1033d 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  _OK;.}..#endif /
1033e 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20  * SQLITE_OS_WIN 
1033f 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
10340 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 77 69  *** End of os_wi
10341 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
10342 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10343 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10344 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
10345 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
10346 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  itvec.c ********
10347 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10348 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10349 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 46  **/./*.** 2008 F
1034a 65 62 72 75 61 72 79 20 31 36 0a 2a 2a 0a 2a 2a  ebruary 16.**.**
1034b 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1034c 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1034d 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1034e 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1034f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
10350 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
10351 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
10352 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
10353 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
10354 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
10355 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
10356 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
10357 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
10358 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
10359 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1035a 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1035b 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1035c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1035d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1035e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1035f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10360 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
10361 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
10362 73 20 61 6e 20 6f 62 6a 65 63 74 20 74 68 61 74  s an object that
10363 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 66 69   represents a fi
10364 78 65 64 2d 6c 65 6e 67 74 68 0a 2a 2a 20 62 69  xed-length.** bi
10365 74 6d 61 70 2e 20 20 42 69 74 73 20 61 72 65 20  tmap.  Bits are 
10366 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e  numbered startin
10367 67 20 77 69 74 68 20 31 2e 0a 2a 2a 0a 2a 2a 20  g with 1..**.** 
10368 41 20 62 69 74 6d 61 70 20 69 73 20 75 73 65 64  A bitmap is used
10369 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 63 68   to record which
1036a 20 70 61 67 65 73 20 6f 66 20 61 20 64 61 74 61   pages of a data
1036b 62 61 73 65 20 66 69 6c 65 20 68 61 76 65 20 62  base file have b
1036c 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65  een.** journalle
1036d 64 20 64 75 72 69 6e 67 20 61 20 74 72 61 6e 73  d during a trans
1036e 61 63 74 69 6f 6e 2c 20 6f 72 20 77 68 69 63 68  action, or which
1036f 20 70 61 67 65 73 20 68 61 76 65 20 74 68 65 20   pages have the 
10370 22 64 6f 6e 74 2d 77 72 69 74 65 22 0a 2a 2a 20  "dont-write".** 
10371 70 72 6f 70 65 72 74 79 2e 20 20 55 73 75 61 6c  property.  Usual
10372 6c 79 20 6f 6e 6c 79 20 61 20 66 65 77 20 70 61  ly only a few pa
10373 67 65 73 20 61 72 65 20 6d 65 65 74 20 65 69 74  ges are meet eit
10374 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a  her condition..*
10375 2a 20 53 6f 20 74 68 65 20 62 69 74 6d 61 70 20  * So the bitmap 
10376 69 73 20 75 73 75 61 6c 6c 79 20 73 70 61 72 73  is usually spars
10377 65 20 61 6e 64 20 68 61 73 20 6c 6f 77 20 63 61  e and has low ca
10378 72 64 69 6e 61 6c 69 74 79 2e 0a 2a 2a 20 42 75  rdinality..** Bu
10379 74 20 73 6f 6d 65 74 69 6d 65 73 20 28 66 6f 72  t sometimes (for
1037a 20 65 78 61 6d 70 6c 65 20 77 68 65 6e 20 64 75   example when du
1037b 72 69 6e 67 20 61 20 44 52 4f 50 20 6f 66 20 61  ring a DROP of a
1037c 20 6c 61 72 67 65 20 74 61 62 6c 65 29 20 6d 6f   large table) mo
1037d 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c 20 6f 66 20  st.** or all of 
1037e 74 68 65 20 70 61 67 65 73 20 69 6e 20 61 20 64  the pages in a d
1037f 61 74 61 62 61 73 65 20 63 61 6e 20 67 65 74 20  atabase can get 
10380 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 20 49 6e 20  journalled.  In 
10381 74 68 6f 73 65 20 63 61 73 65 73 2c 20 0a 2a 2a  those cases, .**
10382 20 74 68 65 20 62 69 74 6d 61 70 20 62 65 63 6f   the bitmap beco
10383 6d 65 73 20 64 65 6e 73 65 20 77 69 74 68 20 68  mes dense with h
10384 69 67 68 20 63 61 72 64 69 6e 61 6c 69 74 79 2e  igh cardinality.
10385 20 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 20    The algorithm 
10386 6e 65 65 64 73 20 0a 2a 2a 20 74 6f 20 68 61 6e  needs .** to han
10387 64 6c 65 20 62 6f 74 68 20 63 61 73 65 73 20 77  dle both cases w
10388 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ell..**.** The s
10389 69 7a 65 20 6f 66 20 74 68 65 20 62 69 74 6d 61  ize of the bitma
1038a 70 20 69 73 20 66 69 78 65 64 20 77 68 65 6e 20  p is fixed when 
1038b 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 63 72  the object is cr
1038c 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c  eated..**.** All
1038d 20 62 69 74 73 20 61 72 65 20 63 6c 65 61 72 20   bits are clear 
1038e 77 68 65 6e 20 74 68 65 20 62 69 74 6d 61 70 20  when the bitmap 
1038f 69 73 20 63 72 65 61 74 65 64 2e 20 20 49 6e 64  is created.  Ind
10390 69 76 69 64 75 61 6c 20 62 69 74 73 0a 2a 2a 20  ividual bits.** 
10391 6d 61 79 20 62 65 20 73 65 74 20 6f 72 20 63 6c  may be set or cl
10392 65 61 72 65 64 20 6f 6e 65 20 61 74 20 61 20 74  eared one at a t
10393 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74 20  ime..**.** Test 
10394 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 61  operations are a
10395 62 6f 75 74 20 31 30 30 20 74 69 6d 65 73 20 6d  bout 100 times m
10396 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20  ore common that 
10397 73 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a  set operations..
10398 2a 2a 20 43 6c 65 61 72 20 6f 70 65 72 61 74 69  ** Clear operati
10399 6f 6e 73 20 61 72 65 20 65 78 63 65 65 64 69 6e  ons are exceedin
1039a 67 6c 79 20 72 61 72 65 2e 20 20 54 68 65 72 65  gly rare.  There
1039b 20 61 72 65 20 75 73 75 61 6c 6c 79 20 62 65 74   are usually bet
1039c 77 65 65 6e 0a 2a 2a 20 35 20 61 6e 64 20 35 30  ween.** 5 and 50
1039d 30 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e 73  0 set operations
1039e 20 70 65 72 20 42 69 74 76 65 63 20 6f 62 6a 65   per Bitvec obje
1039f 63 74 2c 20 74 68 6f 75 67 68 20 74 68 65 20 6e  ct, though the n
103a0 75 6d 62 65 72 20 6f 66 20 73 65 74 73 20 63 61  umber of sets ca
103a1 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20 67  n.** sometimes g
103a2 72 6f 77 20 69 6e 74 6f 20 74 65 6e 73 20 6f 66  row into tens of
103a3 20 74 68 6f 75 73 61 6e 64 73 20 6f 72 20 6c 61   thousands or la
103a4 72 67 65 72 2e 20 20 54 68 65 20 73 69 7a 65 20  rger.  The size 
103a5 6f 66 20 74 68 65 0a 2a 2a 20 42 69 74 76 65 63  of the.** Bitvec
103a6 20 6f 62 6a 65 63 74 20 69 73 20 74 68 65 20 6e   object is the n
103a7 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
103a8 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
103a9 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 73 74  ile at the.** st
103aa 61 72 74 20 6f 66 20 61 20 74 72 61 6e 73 61 63  art of a transac
103ab 74 69 6f 6e 2c 20 61 6e 64 20 69 73 20 74 68 75  tion, and is thu
103ac 73 20 75 73 75 61 6c 6c 79 20 6c 65 73 73 20 74  s usually less t
103ad 68 61 6e 20 61 20 66 65 77 20 74 68 6f 75 73 61  han a few thousa
103ae 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 61 6e 20 62  nd,.** but can b
103af 65 20 61 73 20 6c 61 72 67 65 20 61 73 20 32 20  e as large as 2 
103b0 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 61 20 72 65  billion for a re
103b1 61 6c 6c 79 20 62 69 67 20 64 61 74 61 62 61 73  ally big databas
103b2 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  e..**.** @(#) $I
103b3 64 3a 20 62 69 74 76 65 63 2e 63 2c 76 20 31 2e  d: bitvec.c,v 1.
103b4 31 37 20 32 30 30 39 2f 30 37 2f 32 35 20 31 37  17 2009/07/25 17
103b5 3a 33 33 3a 32 36 20 64 72 68 20 45 78 70 20 24  :33:26 drh Exp $
103b6 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20  .*/../* Size of 
103b7 74 68 65 20 42 69 74 76 65 63 20 73 74 72 75 63  the Bitvec struc
103b8 74 75 72 65 20 69 6e 20 62 79 74 65 73 2e 20 2a  ture in bytes. *
103b9 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43  /.#define BITVEC
103ba 5f 53 5a 20 20 20 20 20 20 20 20 28 73 69 7a 65  _SZ        (size
103bb 6f 66 28 76 6f 69 64 2a 29 2a 31 32 38 29 20 20  of(void*)*128)  
103bc 2f 2a 20 35 31 32 20 6f 6e 20 33 32 62 69 74 2e  /* 512 on 32bit.
103bd 20 20 31 30 32 34 20 6f 6e 20 36 34 62 69 74 20    1024 on 64bit 
103be 2a 2f 0a 0a 2f 2a 20 52 6f 75 6e 64 20 74 68 65  */../* Round the
103bf 20 75 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77 6e   union size down
103c0 20 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20   to the nearest 
103c1 70 6f 69 6e 74 65 72 20 62 6f 75 6e 64 61 72 79  pointer boundary
103c2 2c 20 73 69 6e 63 65 20 74 68 61 74 27 73 20 68  , since that's h
103c3 6f 77 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 62  ow .** it will b
103c4 65 20 61 6c 69 67 6e 65 64 20 77 69 74 68 69 6e  e aligned within
103c5 20 74 68 65 20 42 69 74 76 65 63 20 73 74 72 75   the Bitvec stru
103c6 63 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  ct. */.#define B
103c7 49 54 56 45 43 5f 55 53 49 5a 45 20 20 20 20 20  ITVEC_USIZE     
103c8 28 28 28 42 49 54 56 45 43 5f 53 5a 2d 28 33 2a  (((BITVEC_SZ-(3*
103c9 73 69 7a 65 6f 66 28 75 33 32 29 29 29 2f 73 69  sizeof(u32)))/si
103ca 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 2a 73  zeof(Bitvec*))*s
103cb 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 0a  izeof(Bitvec*)).
103cc 0a 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20  ./* Type of the 
103cd 61 72 72 61 79 20 22 65 6c 65 6d 65 6e 74 22 20  array "element" 
103ce 66 6f 72 20 74 68 65 20 62 69 74 6d 61 70 20 72  for the bitmap r
103cf 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 0a  epresentation. .
103d0 2a 2a 20 53 68 6f 75 6c 64 20 62 65 20 61 20 70  ** Should be a p
103d1 6f 77 65 72 20 6f 66 20 32 2c 20 61 6e 64 20 69  ower of 2, and i
103d2 64 65 61 6c 6c 79 2c 20 65 76 65 6e 6c 79 20 64  deally, evenly d
103d3 69 76 69 64 65 20 69 6e 74 6f 20 42 49 54 56 45  ivide into BITVE
103d4 43 5f 55 53 49 5a 45 2e 20 0a 2a 2a 20 53 65 74  C_USIZE. .** Set
103d5 74 69 6e 67 20 74 68 69 73 20 74 6f 20 74 68 65  ting this to the
103d6 20 22 6e 61 74 75 72 61 6c 20 77 6f 72 64 22 20   "natural word" 
103d7 73 69 7a 65 20 6f 66 20 79 6f 75 72 20 43 50 55  size of your CPU
103d8 20 6d 61 79 20 69 6d 70 72 6f 76 65 0a 2a 2a 20   may improve.** 
103d9 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a  performance. */.
103da 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 54  #define BITVEC_T
103db 45 4c 45 4d 20 20 20 20 20 75 38 0a 2f 2a 20 53  ELEM     u8./* S
103dc 69 7a 65 2c 20 69 6e 20 62 69 74 73 2c 20 6f 66  ize, in bits, of
103dd 20 74 68 65 20 62 69 74 6d 61 70 20 65 6c 65 6d   the bitmap elem
103de 65 6e 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ent. */.#define 
103df 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 20 20 20  BITVEC_SZELEM   
103e0 20 38 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20   8./* Number of 
103e1 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 20 62 69  elements in a bi
103e2 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23  tmap array. */.#
103e3 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 45  define BITVEC_NE
103e4 4c 45 4d 20 20 20 20 20 28 42 49 54 56 45 43 5f  LEM     (BITVEC_
103e5 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 49 54  USIZE/sizeof(BIT
103e6 56 45 43 5f 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e  VEC_TELEM))./* N
103e7 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e  umber of bits in
103e8 20 74 68 65 20 62 69 74 6d 61 70 20 61 72 72 61   the bitmap arra
103e9 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49  y. */.#define BI
103ea 54 56 45 43 5f 4e 42 49 54 20 20 20 20 20 20 28  TVEC_NBIT      (
103eb 42 49 54 56 45 43 5f 4e 45 4c 45 4d 2a 42 49 54  BITVEC_NELEM*BIT
103ec 56 45 43 5f 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20  VEC_SZELEM)../* 
103ed 4e 75 6d 62 65 72 20 6f 66 20 75 33 32 20 76 61  Number of u32 va
103ee 6c 75 65 73 20 69 6e 20 68 61 73 68 20 74 61 62  lues in hash tab
103ef 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  le. */.#define B
103f0 49 54 56 45 43 5f 4e 49 4e 54 20 20 20 20 20 20  ITVEC_NINT      
103f1 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69  (BITVEC_USIZE/si
103f2 7a 65 6f 66 28 75 33 32 29 29 0a 2f 2a 20 4d 61  zeof(u32))./* Ma
103f3 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
103f4 65 6e 74 72 69 65 73 20 69 6e 20 68 61 73 68 20  entries in hash 
103f5 74 61 62 6c 65 20 62 65 66 6f 72 65 20 0a 2a 2a  table before .**
103f6 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 6e   sub-dividing an
103f7 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f  d re-hashing. */
103f8 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f  .#define BITVEC_
103f9 4d 58 48 41 53 48 20 20 20 20 28 42 49 54 56 45  MXHASH    (BITVE
103fa 43 5f 4e 49 4e 54 2f 32 29 0a 2f 2a 20 48 61 73  C_NINT/2)./* Has
103fb 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66 6f  hing function fo
103fc 72 20 74 68 65 20 61 48 61 73 68 20 72 65 70 72  r the aHash repr
103fd 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45  esentation..** E
103fe 6d 70 69 72 69 63 61 6c 20 74 65 73 74 69 6e 67  mpirical testing
103ff 20 73 68 6f 77 65 64 20 74 68 61 74 20 74 68 65   showed that the
10400 20 2a 33 37 20 6d 75 6c 74 69 70 6c 69 65 72 20   *37 multiplier 
10401 0a 2a 2a 20 28 61 6e 20 61 72 62 69 74 72 61 72  .** (an arbitrar
10402 79 20 70 72 69 6d 65 29 69 6e 20 74 68 65 20 68  y prime)in the h
10403 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 70 72 6f  ash function pro
10404 76 69 64 65 64 20 0a 2a 2a 20 6e 6f 20 66 65 77  vided .** no few
10405 65 72 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 74 68  er collisions th
10406 61 6e 20 74 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e  an the no-op *1.
10407 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56   */.#define BITV
10408 45 43 5f 48 41 53 48 28 58 29 20 20 20 28 28 28  EC_HASH(X)   (((
10409 58 29 2a 31 29 25 42 49 54 56 45 43 5f 4e 49 4e  X)*1)%BITVEC_NIN
1040a 54 29 0a 0a 23 64 65 66 69 6e 65 20 42 49 54 56  T)..#define BITV
1040b 45 43 5f 4e 50 54 52 20 20 20 20 20 20 28 42 49  EC_NPTR      (BI
1040c 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f  TVEC_USIZE/sizeo
1040d 66 28 42 69 74 76 65 63 20 2a 29 29 0a 0a 0a 2f  f(Bitvec *)).../
1040e 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73  *.** A bitmap is
1040f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
10410 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
10411 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
10412 68 69 73 20 62 69 74 6d 61 70 20 72 65 63 6f 72  his bitmap recor
10413 64 73 20 74 68 65 20 65 78 69 73 74 61 6e 63 65  ds the existance
10414 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65   of zero or more
10415 20 62 69 74 73 0a 2a 2a 20 77 69 74 68 20 76 61   bits.** with va
10416 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61  lues between 1 a
10417 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63 6c 75 73  nd iSize, inclus
10418 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ive..**.** There
10419 20 61 72 65 20 74 68 72 65 65 20 70 6f 73 73 69   are three possi
1041a 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69  ble representati
1041b 6f 6e 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61  ons of the bitma
1041c 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3c 3d  p..** If iSize<=
1041d 42 49 54 56 45 43 5f 4e 42 49 54 2c 20 74 68 65  BITVEC_NBIT, the
1041e 6e 20 42 69 74 76 65 63 2e 75 2e 61 42 69 74 6d  n Bitvec.u.aBitm
1041f 61 70 5b 5d 20 69 73 20 61 20 73 74 72 61 69 67  ap[] is a straig
10420 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 54  ht.** bitmap.  T
10421 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69  he least signifi
10422 63 61 6e 74 20 62 69 74 20 69 73 20 62 69 74 20  cant bit is bit 
10423 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 53 69 7a  1..**.** If iSiz
10424 65 3e 42 49 54 56 45 43 5f 4e 42 49 54 20 61 6e  e>BITVEC_NBIT an
10425 64 20 69 44 69 76 69 73 6f 72 3d 3d 30 20 74 68  d iDivisor==0 th
10426 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 48 61 73  en Bitvec.u.aHas
10427 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 61 73 68  h[] is.** a hash
10428 20 74 61 62 6c 65 20 74 68 61 74 20 77 69 6c 6c   table that will
10429 20 68 6f 6c 64 20 75 70 20 74 6f 20 42 49 54 56   hold up to BITV
1042a 45 43 5f 4d 58 48 41 53 48 20 64 69 73 74 69 6e  EC_MXHASH distin
1042b 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a  ct values..**.**
1042c 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
1042d 76 61 6c 75 65 20 69 20 69 73 20 72 65 64 69 72  value i is redir
1042e 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 20 6f  ected into one o
1042f 66 20 42 49 54 56 45 43 5f 4e 50 54 52 0a 2a 2a  f BITVEC_NPTR.**
10430 20 73 75 62 2d 62 69 74 6d 61 70 73 20 70 6f 69   sub-bitmaps poi
10431 6e 74 65 64 20 74 6f 20 62 79 20 42 69 74 76 65  nted to by Bitve
10432 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 20 45 61  c.u.apSub[].  Ea
10433 63 68 20 73 75 62 62 69 74 6d 61 70 0a 2a 2a 20  ch subbitmap.** 
10434 68 61 6e 64 6c 65 73 20 75 70 20 74 6f 20 69 44  handles up to iD
10435 69 76 69 73 6f 72 20 73 65 70 61 72 61 74 65 20  ivisor separate 
10436 76 61 6c 75 65 73 20 6f 66 20 69 2e 20 20 61 70  values of i.  ap
10437 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20  Sub[0] holds.** 
10438 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31  values between 1
10439 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 20 20   and iDivisor.  
1043a 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64 73 20 76  apSub[1] holds v
1043b 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a  alues between.**
1043c 20 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20   iDivisor+1 and 
1043d 32 2a 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53  2*iDivisor.  apS
1043e 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 61 6c 75  ub[N] holds valu
1043f 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a  es between.** N*
10440 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 28  iDivisor+1 and (
10441 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72 2e 20 20  N+1)*iDivisor.  
10442 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 20 69  Each subbitmap i
10443 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20  s normalized.** 
10444 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20 77 69 74  to hold deal wit
10445 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e  h values between
10446 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e   1 and iDivisor.
10447 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69 74 76 65  .*/.struct Bitve
10448 63 20 7b 0a 20 20 75 33 32 20 69 53 69 7a 65 3b  c {.  u32 iSize;
10449 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
1044a 20 62 69 74 20 69 6e 64 65 78 2e 20 20 4d 61 78   bit index.  Max
1044b 20 69 53 69 7a 65 20 69 73 20 34 2c 32 39 34 2c   iSize is 4,294,
1044c 39 36 37 2c 32 39 36 2e 20 2a 2f 0a 20 20 75 33  967,296. */.  u3
1044d 32 20 6e 53 65 74 3b 20 20 20 20 20 20 20 2f 2a  2 nSet;       /*
1044e 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20   Number of bits 
1044f 74 68 61 74 20 61 72 65 20 73 65 74 20 2d 20 6f  that are set - o
10450 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61 48  nly valid for aH
10451 61 73 68 0a 20 20 20 20 20 20 20 20 20 20 20 20  ash.            
10452 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74        ** element
10453 2e 20 20 4d 61 78 20 69 73 20 42 49 54 56 45 43  .  Max is BITVEC
10454 5f 4e 49 4e 54 2e 20 20 46 6f 72 20 42 49 54 56  _NINT.  For BITV
10455 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c 0a 20 20  EC_SZ of 512,.  
10456 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10457 2a 2a 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65  ** this would be
10458 20 31 32 35 2e 20 2a 2f 0a 20 20 75 33 32 20 69   125. */.  u32 i
10459 44 69 76 69 73 6f 72 3b 20 20 20 2f 2a 20 4e 75  Divisor;   /* Nu
1045a 6d 62 65 72 20 6f 66 20 62 69 74 73 20 68 61 6e  mber of bits han
1045b 64 6c 65 64 20 62 79 20 65 61 63 68 20 61 70 53  dled by each apS
1045c 75 62 5b 5d 20 65 6e 74 72 79 2e 20 2a 2f 0a 20  ub[] entry. */. 
1045d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1045e 20 2f 2a 20 53 68 6f 75 6c 64 20 3e 3d 30 20 66   /* Should >=0 f
1045f 6f 72 20 61 70 53 75 62 20 65 6c 65 6d 65 6e 74  or apSub element
10460 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  . */.           
10461 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 69 44         /* Max iD
10462 69 76 69 73 6f 72 20 69 73 20 6d 61 78 28 75 33  ivisor is max(u3
10463 32 29 20 2f 20 42 49 54 56 45 43 5f 4e 50 54 52  2) / BITVEC_NPTR
10464 20 2b 20 31 2e 20 20 2a 2f 0a 20 20 20 20 20 20   + 1.  */.      
10465 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
10466 6f 72 20 61 20 42 49 54 56 45 43 5f 53 5a 20 6f  or a BITVEC_SZ o
10467 66 20 35 31 32 2c 20 74 68 69 73 20 77 6f 75 6c  f 512, this woul
10468 64 20 62 65 20 33 34 2c 33 35 39 2c 37 33 39 2e  d be 34,359,739.
10469 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20   */.  union {.  
1046a 20 20 42 49 54 56 45 43 5f 54 45 4c 45 4d 20 61    BITVEC_TELEM a
1046b 42 69 74 6d 61 70 5b 42 49 54 56 45 43 5f 4e 45  Bitmap[BITVEC_NE
1046c 4c 45 4d 5d 3b 20 20 20 20 2f 2a 20 42 69 74 6d  LEM];    /* Bitm
1046d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ap representatio
1046e 6e 20 2a 2f 0a 20 20 20 20 75 33 32 20 61 48 61  n */.    u32 aHa
1046f 73 68 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b  sh[BITVEC_NINT];
10470 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61        /* Hash ta
10471 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69  ble representati
10472 6f 6e 20 2a 2f 0a 20 20 20 20 42 69 74 76 65 63  on */.    Bitvec
10473 20 2a 61 70 53 75 62 5b 42 49 54 56 45 43 5f 4e   *apSub[BITVEC_N
10474 50 54 52 5d 3b 20 20 2f 2a 20 52 65 63 75 72 73  PTR];  /* Recurs
10475 69 76 65 20 72 65 70 72 65 73 65 6e 74 61 74 69  ive representati
10476 6f 6e 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a  on */.  } u;.};.
10477 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
10478 6e 65 77 20 62 69 74 6d 61 70 20 6f 62 6a 65 63  new bitmap objec
10479 74 20 61 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65  t able to handle
1047a 20 62 69 74 73 20 62 65 74 77 65 65 6e 20 30 20   bits between 0 
1047b 61 6e 64 20 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e  and iSize,.** in
1047c 63 6c 75 73 69 76 65 2e 20 20 52 65 74 75 72 6e  clusive.  Return
1047d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1047e 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 20 52  e new object.  R
1047f 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a  eturn NULL if .*
10480 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a  * malloc fails..
10481 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
10482 45 20 42 69 74 76 65 63 20 2a 73 71 6c 69 74 65  E Bitvec *sqlite
10483 33 42 69 74 76 65 63 43 72 65 61 74 65 28 75 33  3BitvecCreate(u3
10484 32 20 69 53 69 7a 65 29 7b 0a 20 20 42 69 74 76  2 iSize){.  Bitv
10485 65 63 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  ec *p;.  assert(
10486 20 73 69 7a 65 6f 66 28 2a 70 29 3d 3d 42 49 54   sizeof(*p)==BIT
10487 56 45 43 5f 53 5a 20 29 3b 0a 20 20 70 20 3d 20  VEC_SZ );.  p = 
10488 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
10489 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b  o( sizeof(*p) );
1048a 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
1048b 70 2d 3e 69 53 69 7a 65 20 3d 20 69 53 69 7a 65  p->iSize = iSize
1048c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
1048d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  ;.}../*.** Check
1048e 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 69   to see if the i
1048f 2d 74 68 20 62 69 74 20 69 73 20 73 65 74 2e 20  -th bit is set. 
10490 20 52 65 74 75 72 6e 20 74 72 75 65 20 6f 72 20   Return true or 
10491 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 70 20 69  false..** If p i
10492 73 20 4e 55 4c 4c 20 28 69 66 20 74 68 65 20 62  s NULL (if the b
10493 69 74 6d 61 70 20 68 61 73 20 6e 6f 74 20 62 65  itmap has not be
10494 65 6e 20 63 72 65 61 74 65 64 29 20 6f 72 20 69  en created) or i
10495 66 0a 2a 2a 20 69 20 69 73 20 6f 75 74 20 6f 66  f.** i is out of
10496 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 72 65 74   range, then ret
10497 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51  urn false..*/.SQ
10498 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
10499 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65   sqlite3BitvecTe
1049a 73 74 28 42 69 74 76 65 63 20 2a 70 2c 20 75 33  st(Bitvec *p, u3
1049b 32 20 69 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30  2 i){.  if( p==0
1049c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
1049d 66 28 20 69 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c  f( i>p->iSize ||
1049e 20 69 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   i==0 ) return 0
1049f 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65  ;.  i--;.  while
104a0 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b  ( p->iDivisor ){
104a1 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69  .    u32 bin = i
104a2 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20  /p->iDivisor;.  
104a3 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69    i = i%p->iDivi
104a4 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e  sor;.    p = p->
104a5 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20  u.apSub[bin];.  
104a6 20 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20    if (!p) {.    
104a7 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
104a8 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69  }.  }.  if( p->i
104a9 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49  Size<=BITVEC_NBI
104aa 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  T ){.    return 
104ab 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f  (p->u.aBitmap[i/
104ac 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26  BITVEC_SZELEM] &
104ad 20 28 31 3c 3c 28 69 26 28 42 49 54 56 45 43 5f   (1<<(i&(BITVEC_
104ae 53 5a 45 4c 45 4d 2d 31 29 29 29 29 21 3d 30 3b  SZELEM-1))))!=0;
104af 0a 20 20 7d 20 65 6c 73 65 7b 0a 20 20 20 20 75  .  } else{.    u
104b0 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41  32 h = BITVEC_HA
104b1 53 48 28 69 2b 2b 29 3b 0a 20 20 20 20 77 68 69  SH(i++);.    whi
104b2 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68  le( p->u.aHash[h
104b3 5d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ] ){.      if( p
104b4 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20  ->u.aHash[h]==i 
104b5 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
104b6 20 20 68 20 3d 20 28 68 2b 31 29 20 25 20 42 49    h = (h+1) % BI
104b7 54 56 45 43 5f 4e 49 4e 54 3b 0a 20 20 20 20 7d  TVEC_NINT;.    }
104b8 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
104b9 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20   }.}../*.** Set 
104ba 74 68 65 20 69 2d 74 68 20 62 69 74 2e 20 20 52  the i-th bit.  R
104bb 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65  eturn 0 on succe
104bc 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  ss and an error 
104bd 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68  code if.** anyth
104be 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a  ing goes wrong..
104bf 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
104c0 6e 65 20 6d 69 67 68 74 20 63 61 75 73 65 20 73  ne might cause s
104c1 75 62 2d 62 69 74 6d 61 70 73 20 74 6f 20 62 65  ub-bitmaps to be
104c2 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 61 69   allocated.  Fai
104c3 6c 69 6e 67 0a 2a 2a 20 74 6f 20 67 65 74 20 74  ling.** to get t
104c4 68 65 20 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64  he memory needed
104c5 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 75 62   to hold the sub
104c6 2d 62 69 74 6d 61 70 20 69 73 20 74 68 65 20 6f  -bitmap is the o
104c7 6e 6c 79 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20  nly.** that can 
104c8 67 6f 20 77 72 6f 6e 67 20 77 69 74 68 20 61 6e  go wrong with an
104c9 20 69 6e 73 65 72 74 2c 20 61 73 73 75 6d 69 6e   insert, assumin
104ca 67 20 70 20 61 6e 64 20 69 20 61 72 65 20 76 61  g p and i are va
104cb 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  lid..**.** The c
104cc 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
104cd 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74  must ensure that
104ce 20 70 20 69 73 20 61 20 76 61 6c 69 64 20 42 69   p is a valid Bi
104cf 74 76 65 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 61  tvec object.** a
104d0 6e 64 20 74 68 61 74 20 74 68 65 20 76 61 6c 75  nd that the valu
104d1 65 20 66 6f 72 20 22 69 22 20 69 73 20 77 69 74  e for "i" is wit
104d2 68 69 6e 20 72 61 6e 67 65 20 6f 66 20 74 68 65  hin range of the
104d3 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 0a   Bitvec object..
104d4 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 74 68 65  ** Otherwise the
104d5 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
104d6 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
104d7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
104d8 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 42  lite3BitvecSet(B
104d9 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29  itvec *p, u32 i)
104da 7b 0a 20 20 75 33 32 20 68 3b 0a 20 20 69 66 28  {.  u32 h;.  if(
104db 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53   p==0 ) return S
104dc 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65  QLITE_OK;.  asse
104dd 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 61 73 73  rt( i>0 );.  ass
104de 65 72 74 28 20 69 3c 3d 70 2d 3e 69 53 69 7a 65  ert( i<=p->iSize
104df 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69   );.  i--;.  whi
104e0 6c 65 28 28 70 2d 3e 69 53 69 7a 65 20 3e 20 42  le((p->iSize > B
104e1 49 54 56 45 43 5f 4e 42 49 54 29 20 26 26 20 70  ITVEC_NBIT) && p
104e2 2d 3e 69 44 69 76 69 73 6f 72 29 20 7b 0a 20 20  ->iDivisor) {.  
104e3 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d    u32 bin = i/p-
104e4 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69  >iDivisor;.    i
104e5 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72   = i%p->iDivisor
104e6 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61  ;.    if( p->u.a
104e7 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a  pSub[bin]==0 ){.
104e8 20 20 20 20 20 20 70 2d 3e 75 2e 61 70 53 75 62        p->u.apSub
104e9 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 74 65 33 42  [bin] = sqlite3B
104ea 69 74 76 65 63 43 72 65 61 74 65 28 20 70 2d 3e  itvecCreate( p->
104eb 69 44 69 76 69 73 6f 72 20 29 3b 0a 20 20 20 20  iDivisor );.    
104ec 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62    if( p->u.apSub
104ed 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72  [bin]==0 ) retur
104ee 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
104ef 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d      }.    p = p-
104f0 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20  >u.apSub[bin];. 
104f1 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a   }.  if( p->iSiz
104f2 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29  e<=BITVEC_NBIT )
104f3 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d  {.    p->u.aBitm
104f4 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c  ap[i/BITVEC_SZEL
104f5 45 4d 5d 20 7c 3d 20 31 20 3c 3c 20 28 69 26 28  EM] |= 1 << (i&(
104f6 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29  BITVEC_SZELEM-1)
104f7 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
104f8 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68  LITE_OK;.  }.  h
104f9 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 69   = BITVEC_HASH(i
104fa 2b 2b 29 3b 0a 20 20 2f 2a 20 69 66 20 74 68 65  ++);.  /* if the
104fb 72 65 20 77 61 73 6e 27 74 20 61 20 68 61 73 68  re wasn't a hash
104fc 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 61 6e 64 20   collision, and 
104fd 74 68 69 73 20 64 6f 65 73 6e 27 74 20 2a 2f 0a  this doesn't */.
104fe 20 20 2f 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20    /* completely 
104ff 66 69 6c 6c 20 74 68 65 20 68 61 73 68 2c 20 74  fill the hash, t
10500 68 65 6e 20 6a 75 73 74 20 61 64 64 20 69 74 20  hen just add it 
10501 77 69 74 68 6f 75 74 20 2a 2f 0a 20 20 2f 2a 20  without */.  /* 
10502 77 6f 72 72 69 6e 67 20 61 62 6f 75 74 20 73 75  worring about su
10503 62 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72  b-dividing and r
10504 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 20 20  e-hashing. */.  
10505 69 66 28 20 21 70 2d 3e 75 2e 61 48 61 73 68 5b  if( !p->u.aHash[
10506 68 5d 20 29 7b 0a 20 20 20 20 69 66 20 28 70 2d  h] ){.    if (p-
10507 3e 6e 53 65 74 3c 28 42 49 54 56 45 43 5f 4e 49  >nSet<(BITVEC_NI
10508 4e 54 2d 31 29 29 20 7b 0a 20 20 20 20 20 20 67  NT-1)) {.      g
10509 6f 74 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 65  oto bitvec_set_e
1050a 6e 64 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b  nd;.    } else {
1050b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 69 74 76  .      goto bitv
1050c 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3b 0a 20  ec_set_rehash;. 
1050d 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 74 68     }.  }.  /* th
1050e 65 72 65 20 77 61 73 20 61 20 63 6f 6c 6c 69 73  ere was a collis
1050f 69 6f 6e 2c 20 63 68 65 63 6b 20 74 6f 20 73 65  ion, check to se
10510 65 20 69 66 20 69 74 27 73 20 61 6c 72 65 61 64  e if it's alread
10511 79 20 2a 2f 0a 20 20 2f 2a 20 69 6e 20 68 61 73  y */.  /* in has
10512 68 2c 20 69 66 20 6e 6f 74 2c 20 74 72 79 20 74  h, if not, try t
10513 6f 20 66 69 6e 64 20 61 20 73 70 6f 74 20 66 6f  o find a spot fo
10514 72 20 69 74 20 2a 2f 0a 20 20 64 6f 20 7b 0a 20  r it */.  do {. 
10515 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 73     if( p->u.aHas
10516 68 5b 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e  h[h]==i ) return
10517 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
10518 68 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3e 3d  h++;.    if( h>=
10519 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20  BITVEC_NINT ) h 
1051a 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20  = 0;.  } while( 
1051b 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 3b  p->u.aHash[h] );
1051c 0a 20 20 2f 2a 20 77 65 20 64 69 64 6e 27 74 20  .  /* we didn't 
1051d 66 69 6e 64 20 69 74 20 69 6e 20 74 68 65 20 68  find it in the h
1051e 61 73 68 2e 20 20 68 20 70 6f 69 6e 74 73 20 74  ash.  h points t
1051f 6f 20 74 68 65 20 66 69 72 73 74 20 2a 2f 0a 20  o the first */. 
10520 20 2f 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 72   /* available fr
10521 65 65 20 73 70 6f 74 2e 20 63 68 65 63 6b 20 74  ee spot. check t
10522 6f 20 73 65 65 20 69 66 20 74 68 69 73 20 69 73  o see if this is
10523 20 67 6f 69 6e 67 20 74 6f 20 2a 2f 0a 20 20 2f   going to */.  /
10524 2a 20 6d 61 6b 65 20 6f 75 72 20 68 61 73 68 20  * make our hash 
10525 74 6f 6f 20 22 66 75 6c 6c 22 2e 20 20 2a 2f 0a  too "full".  */.
10526 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73  bitvec_set_rehas
10527 68 3a 0a 20 20 69 66 28 20 70 2d 3e 6e 53 65 74  h:.  if( p->nSet
10528 3e 3d 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20  >=BITVEC_MXHASH 
10529 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  ){.    unsigned 
1052a 69 6e 74 20 6a 3b 0a 20 20 20 20 69 6e 74 20 72  int j;.    int r
1052b 63 3b 0a 20 20 20 20 75 33 32 20 2a 61 69 56 61  c;.    u32 *aiVa
1052c 6c 75 65 73 20 3d 20 73 71 6c 69 74 65 33 53 74  lues = sqlite3St
1052d 61 63 6b 41 6c 6c 6f 63 52 61 77 28 30 2c 20 73  ackAllocRaw(0, s
1052e 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68  izeof(p->u.aHash
1052f 29 29 3b 0a 20 20 20 20 69 66 28 20 61 69 56 61  ));.    if( aiVa
10530 6c 75 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20  lues==0 ){.     
10531 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
10532 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  OMEM;.    }else{
10533 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 61 69  .      memcpy(ai
10534 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61  Values, p->u.aHa
10535 73 68 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e  sh, sizeof(p->u.
10536 61 48 61 73 68 29 29 3b 0a 20 20 20 20 20 20 6d  aHash));.      m
10537 65 6d 73 65 74 28 70 2d 3e 75 2e 61 70 53 75 62  emset(p->u.apSub
10538 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75  , 0, sizeof(p->u
10539 2e 61 70 53 75 62 29 29 3b 0a 20 20 20 20 20 20  .apSub));.      
1053a 70 2d 3e 69 44 69 76 69 73 6f 72 20 3d 20 28 70  p->iDivisor = (p
1053b 2d 3e 69 53 69 7a 65 20 2b 20 42 49 54 56 45 43  ->iSize + BITVEC
1053c 5f 4e 50 54 52 20 2d 20 31 29 2f 42 49 54 56 45  _NPTR - 1)/BITVE
1053d 43 5f 4e 50 54 52 3b 0a 20 20 20 20 20 20 72 63  C_NPTR;.      rc
1053e 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   = sqlite3Bitvec
1053f 53 65 74 28 70 2c 20 69 29 3b 0a 20 20 20 20 20  Set(p, i);.     
10540 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54 56   for(j=0; j<BITV
10541 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20  EC_NINT; j++){. 
10542 20 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c         if( aiVal
10543 75 65 73 5b 6a 5d 20 29 20 72 63 20 7c 3d 20 73  ues[j] ) rc |= s
10544 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
10545 70 2c 20 61 69 56 61 6c 75 65 73 5b 6a 5d 29 3b  p, aiValues[j]);
10546 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
10547 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28  qlite3StackFree(
10548 30 2c 20 61 69 56 61 6c 75 65 73 29 3b 0a 20 20  0, aiValues);.  
10549 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1054a 20 20 20 7d 0a 20 20 7d 0a 62 69 74 76 65 63 5f     }.  }.bitvec_
1054b 73 65 74 5f 65 6e 64 3a 0a 20 20 70 2d 3e 6e 53  set_end:.  p->nS
1054c 65 74 2b 2b 3b 0a 20 20 70 2d 3e 75 2e 61 48 61  et++;.  p->u.aHa
1054d 73 68 5b 68 5d 20 3d 20 69 3b 0a 20 20 72 65 74  sh[h] = i;.  ret
1054e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1054f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68  ../*.** Clear th
10550 65 20 69 2d 74 68 20 62 69 74 2e 0a 2a 2a 0a 2a  e i-th bit..**.*
10551 2a 20 70 42 75 66 20 6d 75 73 74 20 62 65 20 61  * pBuf must be a
10552 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 74 20 6c   pointer to at l
10553 65 61 73 74 20 42 49 54 56 45 43 5f 53 5a 20 62  east BITVEC_SZ b
10554 79 74 65 73 20 6f 66 20 74 65 6d 70 6f 72 61 72  ytes of temporar
10555 79 20 73 74 6f 72 61 67 65 0a 2a 2a 20 74 68 61  y storage.** tha
10556 74 20 42 69 74 76 65 63 43 6c 65 61 72 20 63 61  t BitvecClear ca
10557 6e 20 75 73 65 20 74 6f 20 72 65 62 75 69 6c 74  n use to rebuilt
10558 20 69 74 73 20 68 61 73 68 20 74 61 62 6c 65 2e   its hash table.
10559 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1055a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
1055b 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 65  itvecClear(Bitve
1055c 63 20 2a 70 2c 20 75 33 32 20 69 2c 20 76 6f 69  c *p, u32 i, voi
1055d 64 20 2a 70 42 75 66 29 7b 0a 20 20 69 66 28 20  d *pBuf){.  if( 
1055e 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
1055f 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a   assert( i>0 );.
10560 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20    i--;.  while( 
10561 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20  p->iDivisor ){. 
10562 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70     u32 bin = i/p
10563 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20  ->iDivisor;.    
10564 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f  i = i%p->iDiviso
10565 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e  r;.    p = p->u.
10566 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20  apSub[bin];.    
10567 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20 20 20  if (!p) {.      
10568 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
10569 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65  }.  if( p->iSize
1056a 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b  <=BITVEC_NBIT ){
1056b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61  .    p->u.aBitma
1056c 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45  p[i/BITVEC_SZELE
1056d 4d 5d 20 26 3d 20 7e 28 31 20 3c 3c 20 28 69 26  M] &= ~(1 << (i&
1056e 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31  (BITVEC_SZELEM-1
1056f 29 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  )));.  }else{.  
10570 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6a    unsigned int j
10571 3b 0a 20 20 20 20 75 33 32 20 2a 61 69 56 61 6c  ;.    u32 *aiVal
10572 75 65 73 20 3d 20 70 42 75 66 3b 0a 20 20 20 20  ues = pBuf;.    
10573 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 65 73 2c  memcpy(aiValues,
10574 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a   p->u.aHash, siz
10575 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29  eof(p->u.aHash))
10576 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e  ;.    memset(p->
10577 75 2e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65  u.aHash, 0, size
10578 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b  of(p->u.aHash));
10579 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20 3d 20 30  .    p->nSet = 0
1057a 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  ;.    for(j=0; j
1057b 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b  <BITVEC_NINT; j+
1057c 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 69  +){.      if( ai
1057d 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20 61 69 56  Values[j] && aiV
1057e 61 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b 31 29 20  alues[j]!=(i+1) 
1057f 29 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 68  ){.        u32 h
10580 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 61   = BITVEC_HASH(a
10581 69 56 61 6c 75 65 73 5b 6a 5d 2d 31 29 3b 0a 20  iValues[j]-1);. 
10582 20 20 20 20 20 20 20 70 2d 3e 6e 53 65 74 2b 2b         p->nSet++
10583 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
10584 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29   p->u.aHash[h] )
10585 7b 0a 20 20 20 20 20 20 20 20 20 20 68 2b 2b 3b  {.          h++;
10586 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 68  .          if( h
10587 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20  >=BITVEC_NINT ) 
10588 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  h = 0;.        }
10589 0a 20 20 20 20 20 20 20 20 70 2d 3e 75 2e 61 48  .        p->u.aH
1058a 61 73 68 5b 68 5d 20 3d 20 61 69 56 61 6c 75 65  ash[h] = aiValue
1058b 73 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  s[j];.      }.  
1058c 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1058d 20 44 65 73 74 72 6f 79 20 61 20 62 69 74 6d 61   Destroy a bitma
1058e 70 20 6f 62 6a 65 63 74 2e 20 20 52 65 63 6c 61  p object.  Recla
1058f 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73  im all memory us
10590 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
10591 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
10592 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
10593 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 69 66  Bitvec *p){.  if
10594 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( p==0 ) return;
10595 0a 20 20 69 66 28 20 70 2d 3e 69 44 69 76 69 73  .  if( p->iDivis
10596 6f 72 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e  or ){.    unsign
10597 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f  ed int i;.    fo
10598 72 28 69 3d 30 3b 20 69 3c 42 49 54 56 45 43 5f  r(i=0; i<BITVEC_
10599 4e 50 54 52 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  NPTR; i++){.    
1059a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
1059b 65 73 74 72 6f 79 28 70 2d 3e 75 2e 61 70 53 75  estroy(p->u.apSu
1059c 62 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  b[i]);.    }.  }
1059d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
1059e 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  p);.}../*.** Ret
1059f 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66  urn the value of
105a0 20 74 68 65 20 69 53 69 7a 65 20 70 61 72 61 6d   the iSize param
105a1 65 74 65 72 20 73 70 65 63 69 66 69 65 64 20 77  eter specified w
105a2 68 65 6e 20 42 69 74 76 65 63 20 2a 70 0a 2a 2a  hen Bitvec *p.**
105a3 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a 2a 2f   was created..*/
105a4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
105a5 75 33 32 20 73 71 6c 69 74 65 33 42 69 74 76 65  u32 sqlite3Bitve
105a6 63 53 69 7a 65 28 42 69 74 76 65 63 20 2a 70 29  cSize(Bitvec *p)
105a7 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 53  {.  return p->iS
105a8 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ize;.}..#ifndef 
105a9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
105aa 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4c  TIN_TEST./*.** L
105ab 65 74 20 56 5b 5d 20 62 65 20 61 6e 20 61 72 72  et V[] be an arr
105ac 61 79 20 6f 66 20 75 6e 73 69 67 6e 65 64 20 63  ay of unsigned c
105ad 68 61 72 61 63 74 65 72 73 20 73 75 66 66 69 63  haracters suffic
105ae 69 65 6e 74 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20  ient to hold.** 
105af 75 70 20 74 6f 20 4e 20 62 69 74 73 2e 20 20 4c  up to N bits.  L
105b0 65 74 20 49 20 62 65 20 61 6e 20 69 6e 74 65 67  et I be an integ
105b1 65 72 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64  er between 0 and
105b2 20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20   N.  0<=I<N..** 
105b3 54 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  Then the followi
105b4 6e 67 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65  ng macros can be
105b5 20 75 73 65 64 20 74 6f 20 73 65 74 2c 20 63 6c   used to set, cl
105b6 65 61 72 2c 20 6f 72 20 74 65 73 74 0a 2a 2a 20  ear, or test.** 
105b7 69 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73 20  individual bits 
105b8 77 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a 23 64 65  within V..*/.#de
105b9 66 69 6e 65 20 53 45 54 42 49 54 28 56 2c 49 29  fine SETBIT(V,I)
105ba 20 20 20 20 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d        V[I>>3] |=
105bb 20 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 65 66   (1<<(I&7)).#def
105bc 69 6e 65 20 43 4c 45 41 52 42 49 54 28 56 2c 49  ine CLEARBIT(V,I
105bd 29 20 20 20 20 56 5b 49 3e 3e 33 5d 20 26 3d 20  )    V[I>>3] &= 
105be 7e 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 65 66  ~(1<<(I&7)).#def
105bf 69 6e 65 20 54 45 53 54 42 49 54 28 56 2c 49 29  ine TESTBIT(V,I)
105c0 20 20 20 20 20 28 56 5b 49 3e 3e 33 5d 26 28 31       (V[I>>3]&(1
105c1 3c 3c 28 49 26 37 29 29 29 21 3d 30 0a 0a 2f 2a  <<(I&7)))!=0../*
105c2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
105c3 20 72 75 6e 73 20 61 6e 20 65 78 74 65 6e 73 69   runs an extensi
105c4 76 65 20 74 65 73 74 20 6f 66 20 74 68 65 20 42  ve test of the B
105c5 69 74 76 65 63 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  itvec code..**.*
105c6 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73 20 61  * The input is a
105c7 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74 65 67  n array of integ
105c8 65 72 73 20 74 68 61 74 20 61 63 74 73 20 61 73  ers that acts as
105c9 20 61 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 74 6f   a program.** to
105ca 20 74 65 73 74 20 74 68 65 20 42 69 74 76 65 63   test the Bitvec
105cb 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72 73 20  .  The integers 
105cc 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 6c 6c  are opcodes foll
105cd 6f 77 65 64 0a 2a 2a 20 62 79 20 30 2c 20 31 2c  owed.** by 0, 1,
105ce 20 6f 72 20 33 20 6f 70 65 72 61 6e 64 73 2c 20   or 3 operands, 
105cf 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65  depending on the
105d0 20 6f 70 63 6f 64 65 2e 20 20 41 6e 6f 74 68 65   opcode.  Anothe
105d1 72 0a 2a 2a 20 6f 70 63 6f 64 65 20 66 6f 6c 6c  r.** opcode foll
105d2 6f 77 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ows immediately 
105d3 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 6f  after the last o
105d4 70 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  perand..**.** Th
105d5 65 72 65 20 61 72 65 20 36 20 6f 70 63 6f 64 65  ere are 6 opcode
105d6 73 20 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20  s numbered from 
105d7 30 20 74 68 72 6f 75 67 68 20 35 2e 20 20 30 20  0 through 5.  0 
105d8 69 73 20 74 68 65 0a 2a 2a 20 22 68 61 6c 74 22  is the.** "halt"
105d9 20 6f 70 63 6f 64 65 20 61 6e 64 20 63 61 75 73   opcode and caus
105da 65 73 20 74 68 65 20 74 65 73 74 20 74 6f 20 65  es the test to e
105db 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 20 20  nd..**.**    0  
105dc 20 20 20 20 20 20 20 20 48 61 6c 74 20 61 6e 64          Halt and
105dd 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   return the numb
105de 65 72 20 6f 66 20 65 72 72 6f 72 73 0a 2a 2a 20  er of errors.** 
105df 20 20 20 31 20 4e 20 53 20 58 20 20 20 20 53 65     1 N S X    Se
105e0 74 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e 69  t N bits beginni
105e1 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 69 6e  ng with S and in
105e2 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 0a  crementing by X.
105e3 2a 2a 20 20 20 20 32 20 4e 20 53 20 58 20 20 20  **    2 N S X   
105e4 20 43 6c 65 61 72 20 4e 20 62 69 74 73 20 62 65   Clear N bits be
105e5 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61  ginning with S a
105e6 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  nd incrementing 
105e7 62 79 20 58 0a 2a 2a 20 20 20 20 33 20 4e 20 20  by X.**    3 N  
105e8 20 20 20 20 20 20 53 65 74 20 4e 20 72 61 6e 64        Set N rand
105e9 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73  omly chosen bits
105ea 0a 2a 2a 20 20 20 20 34 20 4e 20 20 20 20 20 20  .**    4 N      
105eb 20 20 43 6c 65 61 72 20 4e 20 72 61 6e 64 6f 6d    Clear N random
105ec 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a  ly chosen bits.*
105ed 2a 20 20 20 20 35 20 4e 20 53 20 58 20 20 20 20  *    5 N S X    
105ee 53 65 74 20 4e 20 62 69 74 73 20 66 72 6f 6d 20  Set N bits from 
105ef 53 20 69 6e 63 72 65 6d 65 6e 74 20 58 20 69 6e  S increment X in
105f0 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74   array only, not
105f1 20 69 6e 20 62 69 74 76 65 63 0a 2a 2a 0a 2a 2a   in bitvec.**.**
105f2 20 54 68 65 20 6f 70 63 6f 64 65 73 20 31 20 74   The opcodes 1 t
105f3 68 72 6f 75 67 68 20 34 20 70 65 72 66 6f 72 6d  hrough 4 perform
105f4 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72 20 6f   set and clear o
105f5 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 70 65  perations are pe
105f6 72 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e 20 62 6f  rformed.** on bo
105f7 74 68 20 61 20 42 69 74 76 65 63 20 6f 62 6a 65  th a Bitvec obje
105f8 63 74 20 61 6e 64 20 6f 6e 20 61 20 6c 69 6e 65  ct and on a line
105f9 61 72 20 61 72 72 61 79 20 6f 66 20 62 69 74 73  ar array of bits
105fa 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d   obtained from m
105fb 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65  alloc..** Opcode
105fc 20 35 20 77 6f 72 6b 73 20 6f 6e 20 74 68 65 20   5 works on the 
105fd 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 6e 6c  linear array onl
105fe 79 2c 20 6e 6f 74 20 6f 6e 20 74 68 65 20 42 69  y, not on the Bi
105ff 74 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20  tvec..** Opcode 
10600 35 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 6c  5 is used to del
10601 69 62 65 72 61 74 65 6c 79 20 69 6e 64 75 63 65  iberately induce
10602 20 61 20 66 61 75 6c 74 20 69 6e 20 6f 72 64 65   a fault in orde
10603 72 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69 72 6d 20  r to.** confirm 
10604 74 68 61 74 20 65 72 72 6f 72 20 64 65 74 65 63  that error detec
10605 74 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a  tion works..**.*
10606 2a 20 41 74 20 74 68 65 20 63 6f 6e 63 6c 75 73  * At the conclus
10607 69 6f 6e 20 6f 66 20 74 68 65 20 74 65 73 74 20  ion of the test 
10608 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79  the linear array
10609 20 69 73 20 63 6f 6d 70 61 72 65 64 0a 2a 2a 20   is compared.** 
1060a 61 67 61 69 6e 73 74 20 74 68 65 20 42 69 74 76  against the Bitv
1060b 65 63 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 74  ec object.  If t
1060c 68 65 72 65 20 61 72 65 20 61 6e 79 20 64 69 66  here are any dif
1060d 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 61 6e 20  ferences,.** an 
1060e 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
1060f 64 2e 20 20 49 66 20 74 68 65 79 20 61 72 65 20  d.  If they are 
10610 74 68 65 20 73 61 6d 65 2c 20 7a 65 72 6f 20 69  the same, zero i
10611 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
10612 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  * If a memory al
10613 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f  location error o
10614 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 2d 31  ccurs, return -1
10615 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10616 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
10617 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74  itvecBuiltinTest
10618 28 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a 61 4f  (int sz, int *aO
10619 70 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 42  p){.  Bitvec *pB
1061a 69 74 76 65 63 20 3d 20 30 3b 0a 20 20 75 6e 73  itvec = 0;.  uns
1061b 69 67 6e 65 64 20 63 68 61 72 20 2a 70 56 20 3d  igned char *pV =
1061c 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 2d   0;.  int rc = -
1061d 31 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 78 2c 20  1;.  int i, nx, 
1061e 70 63 2c 20 6f 70 3b 0a 20 20 76 6f 69 64 20 2a  pc, op;.  void *
1061f 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 2f 2a  pTmpSpace;..  /*
10620 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 42 69   Allocate the Bi
10621 74 76 65 63 20 74 6f 20 62 65 20 74 65 73 74 65  tvec to be teste
10622 64 20 61 6e 64 20 61 20 6c 69 6e 65 61 72 20 61  d and a linear a
10623 72 72 61 79 20 6f 66 0a 20 20 2a 2a 20 62 69 74  rray of.  ** bit
10624 73 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20  s to act as the 
10625 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 70  reference */.  p
10626 42 69 74 76 65 63 20 3d 20 73 71 6c 69 74 65 33  Bitvec = sqlite3
10627 42 69 74 76 65 63 43 72 65 61 74 65 28 20 73 7a  BitvecCreate( sz
10628 20 29 3b 0a 20 20 70 56 20 3d 20 73 71 6c 69 74   );.  pV = sqlit
10629 65 33 5f 6d 61 6c 6c 6f 63 28 20 28 73 7a 2b 37  e3_malloc( (sz+7
1062a 29 2f 38 20 2b 20 31 20 29 3b 0a 20 20 70 54 6d  )/8 + 1 );.  pTm
1062b 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33  pSpace = sqlite3
1062c 5f 6d 61 6c 6c 6f 63 28 42 49 54 56 45 43 5f 53  _malloc(BITVEC_S
1062d 5a 29 3b 0a 20 20 69 66 28 20 70 42 69 74 76 65  Z);.  if( pBitve
1062e 63 3d 3d 30 20 7c 7c 20 70 56 3d 3d 30 20 7c 7c  c==0 || pV==0 ||
1062f 20 70 54 6d 70 53 70 61 63 65 3d 3d 30 20 20 29   pTmpSpace==0  )
10630 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64   goto bitvec_end
10631 3b 0a 20 20 6d 65 6d 73 65 74 28 70 56 2c 20 30  ;.  memset(pV, 0
10632 2c 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 29 3b  , (sz+7)/8 + 1);
10633 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 70 42 69 74  ..  /* NULL pBit
10634 76 65 63 20 74 65 73 74 73 20 2a 2f 0a 20 20 73  vec tests */.  s
10635 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
10636 30 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  0, 1);.  sqlite3
10637 42 69 74 76 65 63 43 6c 65 61 72 28 30 2c 20 31  BitvecClear(0, 1
10638 2c 20 70 54 6d 70 53 70 61 63 65 29 3b 0a 0a 20  , pTmpSpace);.. 
10639 20 2f 2a 20 52 75 6e 20 74 68 65 20 70 72 6f 67   /* Run the prog
1063a 72 61 6d 20 2a 2f 0a 20 20 70 63 20 3d 20 30 3b  ram */.  pc = 0;
1063b 0a 20 20 77 68 69 6c 65 28 20 28 6f 70 20 3d 20  .  while( (op = 
1063c 61 4f 70 5b 70 63 5d 29 21 3d 30 20 29 7b 0a 20  aOp[pc])!=0 ){. 
1063d 20 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b     switch( op ){
1063e 0a 20 20 20 20 20 20 63 61 73 65 20 31 3a 0a 20  .      case 1:. 
1063f 20 20 20 20 20 63 61 73 65 20 32 3a 0a 20 20 20       case 2:.   
10640 20 20 20 63 61 73 65 20 35 3a 20 7b 0a 20 20 20     case 5: {.   
10641 20 20 20 20 20 6e 78 20 3d 20 34 3b 0a 20 20 20       nx = 4;.   
10642 20 20 20 20 20 69 20 3d 20 61 4f 70 5b 70 63 2b       i = aOp[pc+
10643 32 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20  2] - 1;.        
10644 61 4f 70 5b 70 63 2b 32 5d 20 2b 3d 20 61 4f 70  aOp[pc+2] += aOp
10645 5b 70 63 2b 33 5d 3b 0a 20 20 20 20 20 20 20 20  [pc+3];.        
10646 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
10647 20 20 20 20 20 63 61 73 65 20 33 3a 0a 20 20 20       case 3:.   
10648 20 20 20 63 61 73 65 20 34 3a 20 0a 20 20 20 20     case 4: .    
10649 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
1064a 20 20 20 20 20 6e 78 20 3d 20 32 3b 0a 20 20 20       nx = 2;.   
1064b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e       sqlite3_ran
1064c 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 69  domness(sizeof(i
1064d 29 2c 20 26 69 29 3b 0a 20 20 20 20 20 20 20 20  ), &i);.        
1064e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1064f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 2d 2d     }.    if( (--
10650 61 4f 70 5b 70 63 2b 31 5d 29 20 3e 20 30 20 29  aOp[pc+1]) > 0 )
10651 20 6e 78 20 3d 20 30 3b 0a 20 20 20 20 70 63 20   nx = 0;.    pc 
10652 2b 3d 20 6e 78 3b 0a 20 20 20 20 69 20 3d 20 28  += nx;.    i = (
10653 69 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25  i & 0x7fffffff)%
10654 73 7a 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 20  sz;.    if( (op 
10655 26 20 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  & 1)!=0 ){.     
10656 20 53 45 54 42 49 54 28 70 56 2c 20 28 69 2b 31   SETBIT(pV, (i+1
10657 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70  ));.      if( op
10658 21 3d 35 20 29 7b 0a 20 20 20 20 20 20 20 20 69  !=5 ){.        i
10659 66 28 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  f( sqlite3Bitvec
1065a 53 65 74 28 70 42 69 74 76 65 63 2c 20 69 2b 31  Set(pBitvec, i+1
1065b 29 20 29 20 67 6f 74 6f 20 62 69 74 76 65 63 5f  ) ) goto bitvec_
1065c 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  end;.      }.   
1065d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 43 4c   }else{.      CL
1065e 45 41 52 42 49 54 28 70 56 2c 20 28 69 2b 31 29  EARBIT(pV, (i+1)
1065f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
10660 42 69 74 76 65 63 43 6c 65 61 72 28 70 42 69 74  BitvecClear(pBit
10661 76 65 63 2c 20 69 2b 31 2c 20 70 54 6d 70 53 70  vec, i+1, pTmpSp
10662 61 63 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ace);.    }.  }.
10663 0a 20 20 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61  .  /* Test to ma
10664 6b 65 20 73 75 72 65 20 74 68 65 20 6c 69 6e 65  ke sure the line
10665 61 72 20 61 72 72 61 79 20 65 78 61 63 74 6c 79  ar array exactly
10666 20 6d 61 74 63 68 65 73 20 74 68 65 0a 20 20 2a   matches the.  *
10667 2a 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e  * Bitvec object.
10668 20 20 53 74 61 72 74 20 77 69 74 68 20 74 68 65    Start with the
10669 20 61 73 73 75 6d 70 74 69 6f 6e 20 74 68 61 74   assumption that
1066a 20 74 68 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61   they do.  ** ma
1066b 74 63 68 20 28 72 63 3d 3d 30 29 2e 20 20 43 68  tch (rc==0).  Ch
1066c 61 6e 67 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a  ange rc to non-z
1066d 65 72 6f 20 69 66 20 61 20 64 69 73 63 72 65 70  ero if a discrep
1066e 61 6e 63 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75  ancy.  ** is fou
1066f 6e 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  nd..  */.  rc = 
10670 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73  sqlite3BitvecTes
10671 74 28 30 2c 30 29 20 2b 20 73 71 6c 69 74 65 33  t(0,0) + sqlite3
10672 42 69 74 76 65 63 54 65 73 74 28 70 42 69 74 76  BitvecTest(pBitv
10673 65 63 2c 20 73 7a 2b 31 29 0a 20 20 20 20 20 20  ec, sz+1).      
10674 20 20 20 20 2b 20 73 71 6c 69 74 65 33 42 69 74      + sqlite3Bit
10675 76 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c  vecTest(pBitvec,
10676 20 30 29 0a 20 20 20 20 20 20 20 20 20 20 2b 20   0).          + 
10677 28 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69  (sqlite3BitvecSi
10678 7a 65 28 70 42 69 74 76 65 63 29 20 2d 20 73 7a  ze(pBitvec) - sz
10679 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  );.  for(i=1; i<
1067a 3d 73 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  =sz; i++){.    i
1067b 66 28 20 20 28 54 45 53 54 42 49 54 28 70 56 2c  f(  (TESTBIT(pV,
1067c 69 29 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76  i))!=sqlite3Bitv
1067d 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 69  ecTest(pBitvec,i
1067e 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) ){.      rc = 
1067f 69 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  i;.      break;.
10680 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
10681 46 72 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73  Free allocated s
10682 74 72 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76  tructure */.bitv
10683 65 63 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65  ec_end:.  sqlite
10684 33 5f 66 72 65 65 28 70 54 6d 70 53 70 61 63 65  3_free(pTmpSpace
10685 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
10686 65 28 70 56 29 3b 0a 20 20 73 71 6c 69 74 65 33  e(pV);.  sqlite3
10687 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 42  BitvecDestroy(pB
10688 69 74 76 65 63 29 3b 0a 20 20 72 65 74 75 72 6e  itvec);.  return
10689 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a   rc;.}.#endif /*
1068a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49   SQLITE_OMIT_BUI
1068b 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a  LTIN_TEST */../*
1068c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1068d 64 20 6f 66 20 62 69 74 76 65 63 2e 63 20 2a 2a  d of bitvec.c **
1068e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1068f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10691 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
10692 67 69 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e  gin file pcache.
10693 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
10694 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10695 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10696 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20  .** 2008 August 
10697 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  05.**.** The aut
10698 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
10699 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1069a 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1069b 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1069c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1069d 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1069e 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1069f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
106a0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
106a1 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
106a2 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
106a3 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
106a4 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
106a5 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
106a6 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
106a7 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
106a8 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
106a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106ad 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69  *.** This file i
106ae 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 70  mplements that p
106af 61 67 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a  age cache..**.**
106b0 20 40 28 23 29 20 24 49 64 3a 20 70 63 61 63 68   @(#) $Id: pcach
106b1 65 2e 63 2c 76 20 31 2e 34 37 20 32 30 30 39 2f  e.c,v 1.47 2009/
106b2 30 37 2f 32 35 20 31 31 3a 34 36 3a 34 39 20 64  07/25 11:46:49 d
106b3 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
106b4 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6d  .*/../*.** A com
106b5 70 6c 65 74 65 20 70 61 67 65 20 63 61 63 68 65  plete page cache
106b6 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
106b7 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
106b8 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 43 61  e..*/.struct PCa
106b9 63 68 65 20 7b 0a 20 20 50 67 48 64 72 20 2a 70  che {.  PgHdr *p
106ba 44 69 72 74 79 2c 20 2a 70 44 69 72 74 79 54 61  Dirty, *pDirtyTa
106bb 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  il;         /* L
106bc 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67  ist of dirty pag
106bd 65 73 20 69 6e 20 4c 52 55 20 6f 72 64 65 72 20  es in LRU order 
106be 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 53 79 6e  */.  PgHdr *pSyn
106bf 63 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ced;            
106c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74           /* Last
106c1 20 73 79 6e 63 65 64 20 70 61 67 65 20 69 6e 20   synced page in 
106c2 64 69 72 74 79 20 70 61 67 65 20 6c 69 73 74 20  dirty page list 
106c3 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20  */.  int nRef;  
106c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106c5 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
106c6 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 64  er of referenced
106c7 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20   pages */.  int 
106c8 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20  nMax;           
106c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106ca 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 63 61  /* Configured ca
106cb 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e  che size */.  in
106cc 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20  t szPage;       
106cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106ce 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65    /* Size of eve
106cf 72 79 20 70 61 67 65 20 69 6e 20 74 68 69 73 20  ry page in this 
106d0 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 73  cache */.  int s
106d1 7a 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20  zExtra;         
106d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
106d3 2a 20 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20  * Size of extra 
106d4 73 70 61 63 65 20 66 6f 72 20 65 61 63 68 20 70  space for each p
106d5 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75  age */.  int bPu
106d6 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20  rgeable;        
106d7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
106d8 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72  True if pages ar
106d9 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f  e on backing sto
106da 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  re */.  int (*xS
106db 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48  tress)(void*,PgH
106dc 64 72 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43  dr*);       /* C
106dd 61 6c 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20  all to try make 
106de 61 20 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a  a page clean */.
106df 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b    void *pStress;
106e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106e1 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
106e2 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a  t to xStress */.
106e3 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65    sqlite3_pcache
106e4 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20   *pCache;       
106e5 20 20 20 20 20 20 2f 2a 20 50 6c 75 67 67 61 62        /* Pluggab
106e6 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 20  le cache module 
106e7 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67  */.  PgHdr *pPag
106e8 65 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  e1;             
106e9 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65           /* Refe
106ea 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20  rence to page 1 
106eb 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d  */.};../*.** Som
106ec 65 20 6f 66 20 74 68 65 20 61 73 73 65 72 74 28  e of the assert(
106ed 29 20 6d 61 63 72 6f 73 20 69 6e 20 74 68 69 73  ) macros in this
106ee 20 63 6f 64 65 20 61 72 65 20 74 6f 6f 20 65 78   code are too ex
106ef 70 65 6e 73 69 76 65 20 74 6f 20 72 75 6e 0a 2a  pensive to run.*
106f0 2a 20 65 76 65 6e 20 64 75 72 69 6e 67 20 6e 6f  * even during no
106f1 72 6d 61 6c 20 64 65 62 75 67 67 69 6e 67 2e 20  rmal debugging. 
106f2 20 55 73 65 20 74 68 65 6d 20 6f 6e 6c 79 20 72   Use them only r
106f3 61 72 65 6c 79 20 6f 6e 20 6c 6f 6e 67 2d 72 75  arely on long-ru
106f4 6e 6e 69 6e 67 0a 2a 2a 20 74 65 73 74 73 2e 20  nning.** tests. 
106f5 20 45 6e 61 62 6c 65 20 74 68 65 20 65 78 70 65   Enable the expe
106f6 6e 73 69 76 65 20 61 73 73 65 72 74 73 20 75 73  nsive asserts us
106f7 69 6e 67 20 74 68 65 0a 2a 2a 20 2d 44 53 51 4c  ing the.** -DSQL
106f8 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e  ITE_ENABLE_EXPEN
106f9 53 49 56 45 5f 41 53 53 45 52 54 3d 31 20 63 6f  SIVE_ASSERT=1 co
106fa 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f  mpile-time optio
106fb 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  n..*/.#ifdef SQL
106fc 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e  ITE_ENABLE_EXPEN
106fd 53 49 56 45 5f 41 53 53 45 52 54 0a 23 20 64 65  SIVE_ASSERT.# de
106fe 66 69 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61  fine expensive_a
106ff 73 73 65 72 74 28 58 29 20 20 61 73 73 65 72 74  ssert(X)  assert
10700 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  (X).#else.# defi
10701 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73  ne expensive_ass
10702 65 72 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f  ert(X).#endif../
10703 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10704 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10705 2a 2a 20 4c 69 6e 6b 65 64 20 4c 69 73 74 20 4d  ** Linked List M
10706 61 6e 61 67 65 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a  anagement ******
10707 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
10708 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
10709 45 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 64  EBUG) && defined
1070a 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45  (SQLITE_ENABLE_E
1070b 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 29  XPENSIVE_ASSERT)
1070c 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61  ./*.** Check tha
1070d 74 20 74 68 65 20 70 43 61 63 68 65 2d 3e 70 53  t the pCache->pS
1070e 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69  ynced variable i
1070f 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e  s set correctly.
10710 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74   If it.** is not
10711 2c 20 65 69 74 68 65 72 20 66 61 69 6c 20 61 6e  , either fail an
10712 20 61 73 73 65 72 74 20 6f 72 20 72 65 74 75 72   assert or retur
10713 6e 20 7a 65 72 6f 2e 20 4f 74 68 65 72 77 69 73  n zero. Otherwis
10714 65 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e  e, return.** non
10715 2d 7a 65 72 6f 2e 20 54 68 69 73 20 69 73 20 6f  -zero. This is o
10716 6e 6c 79 20 75 73 65 64 20 69 6e 20 64 65 62 75  nly used in debu
10717 67 67 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 73  gging builds, as
10718 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
10719 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65    expensive_asse
1071a 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53  rt( pcacheCheckS
1071b 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 3b  ynced(pCache) );
1071c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
1071d 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64  cacheCheckSynced
1071e 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29  (PCache *pCache)
1071f 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20  {.  PgHdr *p;.  
10720 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44  for(p=pCache->pD
10721 69 72 74 79 54 61 69 6c 3b 20 70 21 3d 70 43 61  irtyTail; p!=pCa
10722 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 70 3d  che->pSynced; p=
10723 70 2d 3e 70 44 69 72 74 79 50 72 65 76 29 7b 0a  p->pDirtyPrev){.
10724 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e      assert( p->n
10725 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73  Ref || (p->flags
10726 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
10727 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ) );.  }.  retur
10728 6e 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52  n (p==0 || p->nR
10729 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26  ef || (p->flags&
1072a 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
1072b 3d 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  ==0);.}.#endif /
1072c 2a 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 4c  * !NDEBUG && SQL
1072d 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e  ITE_ENABLE_EXPEN
1072e 53 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a 0a  SIVE_ASSERT */..
1072f 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 70 61 67  /*.** Remove pag
10730 65 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65  e pPage from the
10731 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70   list of dirty p
10732 61 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ages..*/.static 
10733 76 6f 69 64 20 70 63 61 63 68 65 52 65 6d 6f 76  void pcacheRemov
10734 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 50  eFromDirtyList(P
10735 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20  gHdr *pPage){.  
10736 50 43 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67  PCache *p = pPag
10737 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 73  e->pCache;..  as
10738 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69  sert( pPage->pDi
10739 72 74 79 4e 65 78 74 20 7c 7c 20 70 50 61 67 65  rtyNext || pPage
1073a 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20  ==p->pDirtyTail 
1073b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
1073c 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 7c  ge->pDirtyPrev |
1073d 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72  | pPage==p->pDir
1073e 74 79 20 29 3b 0a 0a 20 20 2f 2a 20 55 70 64 61  ty );..  /* Upda
1073f 74 65 20 74 68 65 20 50 43 61 63 68 65 31 2e 70  te the PCache1.p
10740 53 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20  Synced variable 
10741 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f  if necessary. */
10742 0a 20 20 69 66 28 20 70 2d 3e 70 53 79 6e 63 65  .  if( p->pSynce
10743 64 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20  d==pPage ){.    
10744 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 20 3d  PgHdr *pSynced =
10745 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72   pPage->pDirtyPr
10746 65 76 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70  ev;.    while( p
10747 53 79 6e 63 65 64 20 26 26 20 28 70 53 79 6e 63  Synced && (pSync
10748 65 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  ed->flags&PGHDR_
10749 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20  NEED_SYNC) ){.  
1074a 20 20 20 20 70 53 79 6e 63 65 64 20 3d 20 70 53      pSynced = pS
1074b 79 6e 63 65 64 2d 3e 70 44 69 72 74 79 50 72 65  ynced->pDirtyPre
1074c 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  v;.    }.    p->
1074d 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 65  pSynced = pSynce
1074e 64 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  d;.  }..  if( pP
1074f 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20  age->pDirtyNext 
10750 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44  ){.    pPage->pD
10751 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79  irtyNext->pDirty
10752 50 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 44  Prev = pPage->pD
10753 69 72 74 79 50 72 65 76 3b 0a 20 20 7d 65 6c 73  irtyPrev;.  }els
10754 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
10755 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 54  Page==p->pDirtyT
10756 61 69 6c 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44  ail );.    p->pD
10757 69 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65  irtyTail = pPage
10758 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20  ->pDirtyPrev;.  
10759 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70  }.  if( pPage->p
1075a 44 69 72 74 79 50 72 65 76 20 29 7b 0a 20 20 20  DirtyPrev ){.   
1075b 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72   pPage->pDirtyPr
1075c 65 76 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d  ev->pDirtyNext =
1075d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65   pPage->pDirtyNe
1075e 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  xt;.  }else{.   
1075f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 3d 3d   assert( pPage==
10760 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 20 20 20  p->pDirty );.   
10761 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61   p->pDirty = pPa
10762 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a  ge->pDirtyNext;.
10763 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 70 44 69    }.  pPage->pDi
10764 72 74 79 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70  rtyNext = 0;.  p
10765 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76  Page->pDirtyPrev
10766 20 3d 20 30 3b 0a 0a 20 20 65 78 70 65 6e 73 69   = 0;..  expensi
10767 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68  ve_assert( pcach
10768 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 20  eCheckSynced(p) 
10769 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  );.}../*.** Add 
1076a 70 61 67 65 20 70 50 61 67 65 20 74 6f 20 74 68  page pPage to th
1076b 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 69  e head of the di
1076c 72 74 79 20 6c 69 73 74 20 28 50 43 61 63 68 65  rty list (PCache
1076d 31 2e 70 44 69 72 74 79 20 69 73 20 73 65 74 20  1.pDirty is set 
1076e 74 6f 0a 2a 2a 20 70 50 61 67 65 29 2e 0a 2a 2f  to.** pPage)..*/
1076f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10770 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73  cheAddToDirtyLis
10771 74 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b  t(PgHdr *pPage){
10772 0a 20 20 50 43 61 63 68 65 20 2a 70 20 3d 20 70  .  PCache *p = p
10773 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20  Page->pCache;.. 
10774 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
10775 70 44 69 72 74 79 4e 65 78 74 3d 3d 30 20 26 26  pDirtyNext==0 &&
10776 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72   pPage->pDirtyPr
10777 65 76 3d 3d 30 20 26 26 20 70 2d 3e 70 44 69 72  ev==0 && p->pDir
10778 74 79 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20 20  ty!=pPage );..  
10779 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78  pPage->pDirtyNex
1077a 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20  t = p->pDirty;. 
1077b 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72   if( pPage->pDir
1077c 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 73  tyNext ){.    as
1077d 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69  sert( pPage->pDi
1077e 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50  rtyNext->pDirtyP
1077f 72 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50  rev==0 );.    pP
10780 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d  age->pDirtyNext-
10781 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50  >pDirtyPrev = pP
10782 61 67 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 44  age;.  }.  p->pD
10783 69 72 74 79 20 3d 20 70 50 61 67 65 3b 0a 20 20  irty = pPage;.  
10784 69 66 28 20 21 70 2d 3e 70 44 69 72 74 79 54 61  if( !p->pDirtyTa
10785 69 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69  il ){.    p->pDi
10786 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 3b  rtyTail = pPage;
10787 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 2d 3e 70  .  }.  if( !p->p
10788 53 79 6e 63 65 64 20 26 26 20 30 3d 3d 28 70 50  Synced && 0==(pP
10789 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  age->flags&PGHDR
1078a 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20  _NEED_SYNC) ){. 
1078b 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20     p->pSynced = 
1078c 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 65 78 70  pPage;.  }.  exp
1078d 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70  ensive_assert( p
1078e 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64  cacheCheckSynced
1078f 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (p) );.}../*.** 
10790 57 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74  Wrapper around t
10791 68 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63  he pluggable cac
10792 68 65 73 20 78 55 6e 70 69 6e 20 6d 65 74 68 6f  hes xUnpin metho
10793 64 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 20  d. If the cache 
10794 69 73 0a 2a 2a 20 62 65 69 6e 67 20 75 73 65 64  is.** being used
10795 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72   for an in-memor
10796 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 69 73  y database, this
10797 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
10798 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
10799 76 6f 69 64 20 70 63 61 63 68 65 55 6e 70 69 6e  void pcacheUnpin
1079a 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43  (PgHdr *p){.  PC
1079b 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70  ache *pCache = p
1079c 2d 3e 70 43 61 63 68 65 3b 0a 20 20 69 66 28 20  ->pCache;.  if( 
1079d 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
1079e 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  le ){.    if( p-
1079f 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20  >pgno==1 ){.    
107a0 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31    pCache->pPage1
107a1 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
107a2 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
107a3 66 69 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69  fig.pcache.xUnpi
107a4 6e 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  n(pCache->pCache
107a5 2c 20 70 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a  , p, 0);.  }.}..
107a6 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
107a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107a9 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6e 74  **** General Int
107aa 65 72 66 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a  erfaces ******.*
107ab 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
107ac 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65  and shutdown the
107ad 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73   page cache subs
107ae 79 73 74 65 6d 2e 20 4e 65 69 74 68 65 72 20 6f  ystem. Neither o
107af 66 20 74 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63  f these .** func
107b0 74 69 6f 6e 73 20 61 72 65 20 74 68 72 65 61 64  tions are thread
107b1 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  safe..*/.SQLITE_
107b2 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
107b3 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c  te3PcacheInitial
107b4 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 69 66 28  ize(void){.  if(
107b5 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
107b6 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69  nfig.pcache.xIni
107b7 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  t==0 ){.    sqli
107b8 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61  te3PCacheSetDefa
107b9 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ult();.  }.  ret
107ba 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  urn sqlite3Globa
107bb 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78  lConfig.pcache.x
107bc 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62  Init(sqlite3Glob
107bd 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
107be 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  pArg);.}.SQLITE_
107bf 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
107c0 69 74 65 33 50 63 61 63 68 65 53 68 75 74 64 6f  ite3PcacheShutdo
107c1 77 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  wn(void){.  if( 
107c2 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
107c3 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75 74  fig.pcache.xShut
107c4 64 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69  down ){.    sqli
107c5 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
107c6 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e  pcache.xShutdown
107c7 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
107c8 6e 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 67  nfig.pcache.pArg
107c9 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
107ca 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
107cb 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 43  in bytes of a PC
107cc 61 63 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  ache object..*/.
107cd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
107ce 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
107cf 53 69 7a 65 28 76 6f 69 64 29 7b 20 72 65 74 75  Size(void){ retu
107d0 72 6e 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65  rn sizeof(PCache
107d1 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  ); }../*.** Crea
107d2 74 65 20 61 20 6e 65 77 20 50 43 61 63 68 65 20  te a new PCache 
107d3 6f 62 6a 65 63 74 2e 20 53 74 6f 72 61 67 65 20  object. Storage 
107d4 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  space to hold th
107d5 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 68 61 73 20  e object.** has 
107d6 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c  already been all
107d7 6f 63 61 74 65 64 20 61 6e 64 20 69 73 20 70 61  ocated and is pa
107d8 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 70  ssed in as the p
107d9 20 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20 54 68   pointer. .** Th
107da 65 20 63 61 6c 6c 65 72 20 64 69 73 63 6f 76 65  e caller discove
107db 72 73 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63  rs how much spac
107dc 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6c  e needs to be al
107dd 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 63  located by .** c
107de 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 63  alling sqlite3Pc
107df 61 63 68 65 53 69 7a 65 28 29 2e 0a 2a 2f 0a 53  acheSize()..*/.S
107e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
107e1 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
107e2 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61  Open(.  int szPa
107e3 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ge,             
107e4 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
107e5 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20  every page */.  
107e6 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20  int szExtra,    
107e7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
107e8 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73 6f  Extra space asso
107e9 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68  ciated with each
107ea 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62   page */.  int b
107eb 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20  Purgeable,      
107ec 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
107ed 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20  if pages are on 
107ee 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f  backing store */
107ef 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73  .  int (*xStress
107f0 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 2c  )(void*,PgHdr*),
107f1 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 74  /* Call to try t
107f2 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65  o make pages cle
107f3 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  an */.  void *pS
107f4 74 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 20  tress,          
107f5 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
107f6 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20   to xStress */. 
107f7 20 50 43 61 63 68 65 20 2a 70 20 20 20 20 20 20   PCache *p      
107f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
107f9 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70   Preallocated sp
107fa 61 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63  ace for the PCac
107fb 68 65 20 2a 2f 0a 29 7b 0a 20 20 6d 65 6d 73 65  he */.){.  memse
107fc 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50  t(p, 0, sizeof(P
107fd 43 61 63 68 65 29 29 3b 0a 20 20 70 2d 3e 73 7a  Cache));.  p->sz
107fe 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20  Page = szPage;. 
107ff 20 70 2d 3e 73 7a 45 78 74 72 61 20 3d 20 73 7a   p->szExtra = sz
10800 45 78 74 72 61 3b 0a 20 20 70 2d 3e 62 50 75 72  Extra;.  p->bPur
10801 67 65 61 62 6c 65 20 3d 20 62 50 75 72 67 65 61  geable = bPurgea
10802 62 6c 65 3b 0a 20 20 70 2d 3e 78 53 74 72 65 73  ble;.  p->xStres
10803 73 20 3d 20 78 53 74 72 65 73 73 3b 0a 20 20 70  s = xStress;.  p
10804 2d 3e 70 53 74 72 65 73 73 20 3d 20 70 53 74 72  ->pStress = pStr
10805 65 73 73 3b 0a 20 20 70 2d 3e 6e 4d 61 78 20 3d  ess;.  p->nMax =
10806 20 31 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   100;.}../*.** C
10807 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73  hange the page s
10808 69 7a 65 20 66 6f 72 20 50 43 61 63 68 65 20 6f  ize for PCache o
10809 62 6a 65 63 74 2e 20 54 68 65 20 63 61 6c 6c 65  bject. The calle
1080a 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68  r must ensure th
1080b 61 74 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20  at there.** are 
1080c 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70  no outstanding p
1080d 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 77  age references w
1080e 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
1080f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  n is called..*/.
10810 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10811 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
10812 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61  eSetPageSize(PCa
10813 63 68 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74  che *pCache, int
10814 20 73 7a 50 61 67 65 29 7b 0a 20 20 61 73 73 65   szPage){.  asse
10815 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 52 65 66  rt( pCache->nRef
10816 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70  ==0 && pCache->p
10817 44 69 72 74 79 3d 3d 30 20 29 3b 0a 20 20 69 66  Dirty==0 );.  if
10818 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  ( pCache->pCache
10819 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   ){.    sqlite3G
1081a 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
1081b 68 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63  he.xDestroy(pCac
1081c 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 20  he->pCache);.   
1081d 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
1081e 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68  = 0;.  }.  pCach
1081f 65 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61  e->szPage = szPa
10820 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79  ge;.}../*.** Try
10821 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 61 67   to obtain a pag
10822 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65  e from the cache
10823 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10824 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
10825 63 61 63 68 65 46 65 74 63 68 28 0a 20 20 50 43  cacheFetch(.  PC
10826 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 20 20  ache *pCache,   
10827 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68      /* Obtain th
10828 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 69 73  e page from this
10829 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f   cache */.  Pgno
1082a 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20   pgno,          
1082b 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
1082c 20 74 6f 20 6f 62 74 61 69 6e 20 2a 2f 0a 20 20   to obtain */.  
1082d 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20  int createFlag, 
1082e 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65        /* If true
1082f 2c 20 63 72 65 61 74 65 20 70 61 67 65 20 69 66  , create page if
10830 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69   it does not exi
10831 73 74 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20  st already */.  
10832 50 67 48 64 72 20 2a 2a 70 70 50 61 67 65 20 20  PgHdr **ppPage  
10833 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
10834 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a  he page here */.
10835 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67  ){.  PgHdr *pPag
10836 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 65 43 72  e = 0;.  int eCr
10837 65 61 74 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  eate;..  assert(
10838 20 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20   pCache!=0 );.  
10839 61 73 73 65 72 74 28 20 63 72 65 61 74 65 46 6c  assert( createFl
1083a 61 67 3d 3d 31 20 7c 7c 20 63 72 65 61 74 65 46  ag==1 || createF
1083b 6c 61 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  lag==0 );.  asse
1083c 72 74 28 20 70 67 6e 6f 3e 30 20 29 3b 0a 0a 20  rt( pgno>0 );.. 
1083d 20 2f 2a 20 49 66 20 74 68 65 20 70 6c 75 67 67   /* If the plugg
1083e 61 62 6c 65 20 63 61 63 68 65 20 28 73 71 6c 69  able cache (sqli
1083f 74 65 33 5f 70 63 61 63 68 65 2a 29 20 68 61 73  te3_pcache*) has
10840 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61   not been alloca
10841 74 65 64 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61  ted,.  ** alloca
10842 74 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a  te it now..  */.
10843 20 20 69 66 28 20 21 70 43 61 63 68 65 2d 3e 70    if( !pCache->p
10844 43 61 63 68 65 20 26 26 20 63 72 65 61 74 65 46  Cache && createF
10845 6c 61 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  lag ){.    sqlit
10846 65 33 5f 70 63 61 63 68 65 20 2a 70 3b 0a 20 20  e3_pcache *p;.  
10847 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20    int nByte;.   
10848 20 6e 42 79 74 65 20 3d 20 70 43 61 63 68 65 2d   nByte = pCache-
10849 3e 73 7a 50 61 67 65 20 2b 20 70 43 61 63 68 65  >szPage + pCache
1084a 2d 3e 73 7a 45 78 74 72 61 20 2b 20 73 69 7a 65  ->szExtra + size
1084b 6f 66 28 50 67 48 64 72 29 3b 0a 20 20 20 20 70  of(PgHdr);.    p
1084c 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
1084d 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 43  Config.pcache.xC
1084e 72 65 61 74 65 28 6e 42 79 74 65 2c 20 70 43 61  reate(nByte, pCa
1084f 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 29  che->bPurgeable)
10850 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a  ;.    if( !p ){.
10851 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
10852 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
10853 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
10854 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
10855 78 43 61 63 68 65 73 69 7a 65 28 70 2c 20 70 43  xCachesize(p, pC
10856 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20  ache->nMax);.   
10857 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
10858 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 65 43 72 65  = p;.  }..  eCre
10859 61 74 65 20 3d 20 63 72 65 61 74 65 46 6c 61 67  ate = createFlag
1085a 20 2a 20 28 31 20 2b 20 28 21 70 43 61 63 68 65   * (1 + (!pCache
1085b 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20  ->bPurgeable || 
1085c 21 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29  !pCache->pDirty)
1085d 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  );.  if( pCache-
1085e 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 70  >pCache ){.    p
1085f 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c  Page = sqlite3Gl
10860 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
10861 65 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 2d  e.xFetch(pCache-
10862 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 65  >pCache, pgno, e
10863 43 72 65 61 74 65 29 3b 0a 20 20 7d 0a 0a 20 20  Create);.  }..  
10864 69 66 28 20 21 70 50 61 67 65 20 26 26 20 65 43  if( !pPage && eC
10865 72 65 61 74 65 3d 3d 31 20 29 7b 0a 20 20 20 20  reate==1 ){.    
10866 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 20  PgHdr *pPg;..   
10867 20 2f 2a 20 46 69 6e 64 20 61 20 64 69 72 74 79   /* Find a dirty
10868 20 70 61 67 65 20 74 6f 20 77 72 69 74 65 2d 6f   page to write-o
10869 75 74 20 61 6e 64 20 72 65 63 79 63 6c 65 2e 20  ut and recycle. 
1086a 46 69 72 73 74 20 74 72 79 20 74 6f 20 66 69 6e  First try to fin
1086b 64 20 61 20 0a 20 20 20 20 2a 2a 20 70 61 67 65  d a .    ** page
1086c 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72   that does not r
1086d 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c  equire a journal
1086e 2d 73 79 6e 63 20 28 6f 6e 65 20 77 69 74 68 20  -sync (one with 
1086f 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 0a  PGHDR_NEED_SYNC.
10870 20 20 20 20 2a 2a 20 63 6c 65 61 72 65 64 29 2c      ** cleared),
10871 20 62 75 74 20 69 66 20 74 68 61 74 20 69 73 20   but if that is 
10872 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 73 65 74  not possible set
10873 74 6c 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 65  tle for any othe
10874 72 20 0a 20 20 20 20 2a 2a 20 75 6e 72 65 66 65  r .    ** unrefe
10875 72 65 6e 63 65 64 20 64 69 72 74 79 20 70 61 67  renced dirty pag
10876 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 78  e..    */.    ex
10877 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20  pensive_assert( 
10878 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65  pcacheCheckSynce
10879 64 28 70 43 61 63 68 65 29 20 29 3b 0a 20 20 20  d(pCache) );.   
1087a 20 66 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d   for(pPg=pCache-
1087b 3e 70 53 79 6e 63 65 64 3b 20 0a 20 20 20 20 20  >pSynced; .     
1087c 20 20 20 70 50 67 20 26 26 20 28 70 50 67 2d 3e     pPg && (pPg->
1087d 6e 52 65 66 20 7c 7c 20 28 70 50 67 2d 3e 66 6c  nRef || (pPg->fl
1087e 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
1087f 59 4e 43 29 29 3b 20 0a 20 20 20 20 20 20 20 20  YNC)); .        
10880 70 50 67 3d 70 50 67 2d 3e 70 44 69 72 74 79 50  pPg=pPg->pDirtyP
10881 72 65 76 0a 20 20 20 20 29 3b 0a 20 20 20 20 69  rev.    );.    i
10882 66 28 20 21 70 50 67 20 29 7b 0a 20 20 20 20 20  f( !pPg ){.     
10883 20 66 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d   for(pPg=pCache-
10884 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70 50 67  >pDirtyTail; pPg
10885 20 26 26 20 70 50 67 2d 3e 6e 52 65 66 3b 20 70   && pPg->nRef; p
10886 50 67 3d 70 50 67 2d 3e 70 44 69 72 74 79 50 72  Pg=pPg->pDirtyPr
10887 65 76 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ev);.    }.    i
10888 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 20 20  f( pPg ){.      
10889 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63  int rc;.      rc
1088a 20 3d 20 70 43 61 63 68 65 2d 3e 78 53 74 72 65   = pCache->xStre
1088b 73 73 28 70 43 61 63 68 65 2d 3e 70 53 74 72 65  ss(pCache->pStre
1088c 73 73 2c 20 70 50 67 29 3b 0a 20 20 20 20 20 20  ss, pPg);.      
1088d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1088e 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  K && rc!=SQLITE_
1088f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20  BUSY ){.        
10890 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
10891 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50   }.    }..    pP
10892 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f  age = sqlite3Glo
10893 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
10894 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e  .xFetch(pCache->
10895 70 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 32 29  pCache, pgno, 2)
10896 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61  ;.  }..  if( pPa
10897 67 65 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70  ge ){.    if( !p
10898 50 61 67 65 2d 3e 70 44 61 74 61 20 29 7b 0a 20  Page->pData ){. 
10899 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67       memset(pPag
1089a 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 67 48  e, 0, sizeof(PgH
1089b 64 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a  dr) + pCache->sz
1089c 45 78 74 72 61 29 3b 0a 20 20 20 20 20 20 70 50  Extra);.      pP
1089d 61 67 65 2d 3e 70 45 78 74 72 61 20 3d 20 28 76  age->pExtra = (v
1089e 6f 69 64 2a 29 26 70 50 61 67 65 5b 31 5d 3b 0a  oid*)&pPage[1];.
1089f 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 44 61        pPage->pDa
108a0 74 61 20 3d 20 28 76 6f 69 64 20 2a 29 26 28 28  ta = (void *)&((
108a1 63 68 61 72 20 2a 29 70 50 61 67 65 29 5b 73 69  char *)pPage)[si
108a2 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43  zeof(PgHdr) + pC
108a3 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 5d 3b 0a  ache->szExtra];.
108a4 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61        pPage->pCa
108a5 63 68 65 20 3d 20 70 43 61 63 68 65 3b 0a 20 20  che = pCache;.  
108a6 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20      pPage->pgno 
108a7 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20  = pgno;.    }.  
108a8 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
108a9 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20  >pCache==pCache 
108aa 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
108ab 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f  Page->pgno==pgno
108ac 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
108ad 70 50 61 67 65 2d 3e 70 45 78 74 72 61 3d 3d 28  pPage->pExtra==(
108ae 76 6f 69 64 20 2a 29 26 70 50 61 67 65 5b 31 5d  void *)&pPage[1]
108af 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d   );..    if( 0==
108b0 70 50 61 67 65 2d 3e 6e 52 65 66 20 29 7b 0a 20  pPage->nRef ){. 
108b1 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65       pCache->nRe
108b2 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  f++;.    }.    p
108b3 50 61 67 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  Page->nRef++;.  
108b4 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b    if( pgno==1 ){
108b5 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70  .      pCache->p
108b6 50 61 67 65 31 20 3d 20 70 50 61 67 65 3b 0a 20  Page1 = pPage;. 
108b7 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 70 50 61     }.  }.  *ppPa
108b8 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 72 65  ge = pPage;.  re
108b9 74 75 72 6e 20 28 70 50 61 67 65 3d 3d 30 20 26  turn (pPage==0 &
108ba 26 20 65 43 72 65 61 74 65 29 20 3f 20 53 51 4c  & eCreate) ? SQL
108bb 49 54 45 5f 4e 4f 4d 45 4d 20 3a 20 53 51 4c 49  ITE_NOMEM : SQLI
108bc 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
108bd 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65  Decrement the re
108be 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e  ference count on
108bf 20 61 20 70 61 67 65 2e 20 49 66 20 74 68 65 20   a page. If the 
108c0 70 61 67 65 20 69 73 20 63 6c 65 61 6e 20 61 6e  page is clean an
108c1 64 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e  d the.** referen
108c2 63 65 20 63 6f 75 6e 74 20 64 72 6f 70 73 20 74  ce count drops t
108c3 6f 20 30 2c 20 74 68 65 6e 20 69 74 20 69 73 20  o 0, then it is 
108c4 6d 61 64 65 20 65 6c 69 62 6c 65 20 66 6f 72 20  made elible for 
108c5 72 65 63 79 63 6c 69 6e 67 2e 0a 2a 2f 0a 53 51  recycling..*/.SQ
108c6 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
108c7 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52  d sqlite3PcacheR
108c8 65 6c 65 61 73 65 28 50 67 48 64 72 20 2a 70 29  elease(PgHdr *p)
108c9 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  {.  assert( p->n
108ca 52 65 66 3e 30 20 29 3b 0a 20 20 70 2d 3e 6e 52  Ref>0 );.  p->nR
108cb 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  ef--;.  if( p->n
108cc 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 50 43  Ref==0 ){.    PC
108cd 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70  ache *pCache = p
108ce 2d 3e 70 43 61 63 68 65 3b 0a 20 20 20 20 70 43  ->pCache;.    pC
108cf 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  ache->nRef--;.  
108d0 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26    if( (p->flags&
108d1 50 47 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20  PGHDR_DIRTY)==0 
108d2 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 55  ){.      pcacheU
108d3 6e 70 69 6e 28 70 29 3b 0a 20 20 20 20 7d 65 6c  npin(p);.    }el
108d4 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76  se{.      /* Mov
108d5 65 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68  e the page to th
108d6 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 69  e head of the di
108d7 72 74 79 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20  rty list. */.   
108d8 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46     pcacheRemoveF
108d9 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b  romDirtyList(p);
108da 0a 20 20 20 20 20 20 70 63 61 63 68 65 41 64 64  .      pcacheAdd
108db 54 6f 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a  ToDirtyList(p);.
108dc 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
108dd 2a 2a 20 49 6e 63 72 65 61 73 65 20 74 68 65 20  ** Increase the 
108de 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20  reference count 
108df 6f 66 20 61 20 73 75 70 70 6c 69 65 64 20 70 61  of a supplied pa
108e0 67 65 20 62 79 20 31 2e 0a 2a 2f 0a 53 51 4c 49  ge by 1..*/.SQLI
108e1 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
108e2 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
108e3 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73  (PgHdr *p){.  as
108e4 73 65 72 74 28 70 2d 3e 6e 52 65 66 3e 30 29 3b  sert(p->nRef>0);
108e5 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a  .  p->nRef++;.}.
108e6 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 70 61  ./*.** Drop a pa
108e7 67 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68  ge from the cach
108e8 65 2e 20 54 68 65 72 65 20 6d 75 73 74 20 62 65  e. There must be
108e9 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 72 65 66   exactly one ref
108ea 65 72 65 6e 63 65 20 74 6f 20 74 68 65 0a 2a 2a  erence to the.**
108eb 20 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63   page. This func
108ec 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 61  tion deletes tha
108ed 74 20 72 65 66 65 72 65 6e 63 65 2c 20 73 6f 20  t reference, so 
108ee 61 66 74 65 72 20 69 74 20 72 65 74 75 72 6e 73  after it returns
108ef 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 6f 69   the.** page poi
108f0 6e 74 65 64 20 74 6f 20 62 79 20 70 20 69 73 20  nted to by p is 
108f1 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 53 51 4c 49  invalid..*/.SQLI
108f2 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
108f3 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f  sqlite3PcacheDro
108f4 70 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50  p(PgHdr *p){.  P
108f5 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b 0a 20  Cache *pCache;. 
108f6 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
108f7 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  ==1 );.  if( p->
108f8 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
108f9 59 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52  Y ){.    pcacheR
108fa 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69  emoveFromDirtyLi
108fb 73 74 28 70 29 3b 0a 20 20 7d 0a 20 20 70 43 61  st(p);.  }.  pCa
108fc 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b  che = p->pCache;
108fd 0a 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d  .  pCache->nRef-
108fe 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f  -;.  if( p->pgno
108ff 3d 3d 31 20 29 7b 0a 20 20 20 20 70 43 61 63 68  ==1 ){.    pCach
10900 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20  e->pPage1 = 0;. 
10901 20 7d 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62   }.  sqlite3Glob
10902 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
10903 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e 70  xUnpin(pCache->p
10904 43 61 63 68 65 2c 20 70 2c 20 31 29 3b 0a 7d 0a  Cache, p, 1);.}.
10905 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
10906 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72   the page is mar
10907 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 49 66  ked as dirty. If
10908 20 69 74 20 69 73 6e 27 74 20 64 69 72 74 79 20   it isn't dirty 
10909 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65  already,.** make
1090a 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c 49 54   it so..*/.SQLIT
1090b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1090c 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
1090d 44 69 72 74 79 28 50 67 48 64 72 20 2a 70 29 7b  Dirty(PgHdr *p){
1090e 0a 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  .  p->flags &= ~
1090f 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45  PGHDR_DONT_WRITE
10910 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
10911 52 65 66 3e 30 20 29 3b 0a 20 20 69 66 28 20 30  Ref>0 );.  if( 0
10912 3d 3d 28 70 2d 3e 66 6c 61 67 73 20 26 20 50 47  ==(p->flags & PG
10913 48 44 52 5f 44 49 52 54 59 29 20 29 7b 0a 20 20  HDR_DIRTY) ){.  
10914 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47    p->flags |= PG
10915 48 44 52 5f 44 49 52 54 59 3b 0a 20 20 20 20 70  HDR_DIRTY;.    p
10916 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c  cacheAddToDirtyL
10917 69 73 74 28 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  ist( p);.  }.}..
10918 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
10919 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b  the page is mark
1091a 65 64 20 61 73 20 63 6c 65 61 6e 2e 20 49 66 20  ed as clean. If 
1091b 69 74 20 69 73 6e 27 74 20 63 6c 65 61 6e 20 61  it isn't clean a
1091c 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65 20  lready,.** make 
1091d 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45  it so..*/.SQLITE
1091e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1091f 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
10920 6c 65 61 6e 28 50 67 48 64 72 20 2a 70 29 7b 0a  lean(PgHdr *p){.
10921 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20    if( (p->flags 
10922 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20 29  & PGHDR_DIRTY) )
10923 7b 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f  {.    pcacheRemo
10924 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28  veFromDirtyList(
10925 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73  p);.    p->flags
10926 20 26 3d 20 7e 28 50 47 48 44 52 5f 44 49 52 54   &= ~(PGHDR_DIRT
10927 59 7c 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  Y|PGHDR_NEED_SYN
10928 43 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e  C);.    if( p->n
10929 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Ref==0 ){.      
1092a 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a  pcacheUnpin(p);.
1092b 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
1092c 2a 2a 20 4d 61 6b 65 20 65 76 65 72 79 20 70 61  ** Make every pa
1092d 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20  ge in the cache 
1092e 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  clean..*/.SQLITE
1092f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
10930 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e  lite3PcacheClean
10931 41 6c 6c 28 50 43 61 63 68 65 20 2a 70 43 61 63  All(PCache *pCac
10932 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b  he){.  PgHdr *p;
10933 0a 20 20 77 68 69 6c 65 28 20 28 70 20 3d 20 70  .  while( (p = p
10934 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 21 3d  Cache->pDirty)!=
10935 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
10936 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28  PcacheMakeClean(
10937 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
10938 20 43 6c 65 61 72 20 74 68 65 20 50 47 48 44 52   Clear the PGHDR
10939 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20  _NEED_SYNC flag 
1093a 66 72 6f 6d 20 61 6c 6c 20 64 69 72 74 79 20 70  from all dirty p
1093b 61 67 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ages..*/.SQLITE_
1093c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1093d 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53  ite3PcacheClearS
1093e 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 20  yncFlags(PCache 
1093f 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64  *pCache){.  PgHd
10940 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43  r *p;.  for(p=pC
10941 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b  ache->pDirty; p;
10942 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65 78 74   p=p->pDirtyNext
10943 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  ){.    p->flags 
10944 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 53  &= ~PGHDR_NEED_S
10945 59 4e 43 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68  YNC;.  }.  pCach
10946 65 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 43 61  e->pSynced = pCa
10947 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b  che->pDirtyTail;
10948 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
10949 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
1094a 20 6f 66 20 70 61 67 65 20 70 20 74 6f 20 6e 65   of page p to ne
1094b 77 50 67 6e 6f 2e 20 0a 2a 2f 0a 53 51 4c 49 54  wPgno. .*/.SQLIT
1094c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1094d 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65  qlite3PcacheMove
1094e 28 50 67 48 64 72 20 2a 70 2c 20 50 67 6e 6f 20  (PgHdr *p, Pgno 
1094f 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 50 43 61 63  newPgno){.  PCac
10950 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e  he *pCache = p->
10951 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74  pCache;.  assert
10952 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
10953 20 61 73 73 65 72 74 28 20 6e 65 77 50 67 6e 6f   assert( newPgno
10954 3e 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 47  >0 );.  sqlite3G
10955 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
10956 68 65 2e 78 52 65 6b 65 79 28 70 43 61 63 68 65  he.xRekey(pCache
10957 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 70 2d 3e  ->pCache, p, p->
10958 70 67 6e 6f 2c 20 6e 65 77 50 67 6e 6f 29 3b 0a  pgno, newPgno);.
10959 20 20 70 2d 3e 70 67 6e 6f 20 3d 20 6e 65 77 50    p->pgno = newP
1095a 67 6e 6f 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66  gno;.  if( (p->f
1095b 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59  lags&PGHDR_DIRTY
1095c 29 20 26 26 20 28 70 2d 3e 66 6c 61 67 73 26 50  ) && (p->flags&P
1095d 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20  GHDR_NEED_SYNC) 
1095e 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d  ){.    pcacheRem
1095f 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74  oveFromDirtyList
10960 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 41  (p);.    pcacheA
10961 64 64 54 6f 44 69 72 74 79 4c 69 73 74 28 70 29  ddToDirtyList(p)
10962 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
10963 72 6f 70 20 65 76 65 72 79 20 63 61 63 68 65 20  rop every cache 
10964 65 6e 74 72 79 20 77 68 6f 73 65 20 70 61 67 65  entry whose page
10965 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 61 74   number is great
10966 65 72 20 74 68 61 6e 20 22 70 67 6e 6f 22 2e 20  er than "pgno". 
10967 54 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 6d 75  The.** caller mu
10968 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  st ensure that t
10969 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73  here are no outs
1096a 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63  tanding referenc
1096b 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 0a  es to any pages.
1096c 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 70 61  ** other than pa
1096d 67 65 20 31 20 77 69 74 68 20 61 20 70 61 67 65  ge 1 with a page
1096e 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20   number greater 
1096f 74 68 61 6e 20 70 67 6e 6f 2e 0a 2a 2a 0a 2a 2a  than pgno..**.**
10970 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 72   If there is a r
10971 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65  eference to page
10972 20 31 20 61 6e 64 20 74 68 65 20 70 67 6e 6f 20   1 and the pgno 
10973 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64  parameter passed
10974 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63   to this.** func
10975 74 69 6f 6e 20 69 73 20 30 2c 20 74 68 65 6e 20  tion is 0, then 
10976 74 68 65 20 64 61 74 61 20 61 72 65 61 20 61 73  the data area as
10977 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 61  sociated with pa
10978 67 65 20 31 20 69 73 20 7a 65 72 6f 65 64 2c 20  ge 1 is zeroed, 
10979 62 75 74 0a 2a 2a 20 74 68 65 20 70 61 67 65 20  but.** the page 
1097a 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 64 72  object is not dr
1097b 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  opped..*/.SQLITE
1097c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1097d 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63  lite3PcacheTrunc
1097e 61 74 65 28 50 43 61 63 68 65 20 2a 70 43 61 63  ate(PCache *pCac
1097f 68 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  he, Pgno pgno){.
10980 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43    if( pCache->pC
10981 61 63 68 65 20 29 7b 0a 20 20 20 20 50 67 48 64  ache ){.    PgHd
10982 72 20 2a 70 3b 0a 20 20 20 20 50 67 48 64 72 20  r *p;.    PgHdr 
10983 2a 70 4e 65 78 74 3b 0a 20 20 20 20 66 6f 72 28  *pNext;.    for(
10984 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  p=pCache->pDirty
10985 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20  ; p; p=pNext){. 
10986 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e       pNext = p->
10987 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 20 20  pDirtyNext;.    
10988 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3e 70 67    if( p->pgno>pg
10989 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  no ){.        as
1098a 73 65 72 74 28 20 70 2d 3e 66 6c 61 67 73 26 50  sert( p->flags&P
1098b 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20  GHDR_DIRTY );.  
1098c 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61        sqlite3Pca
1098d 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 29 3b  cheMakeClean(p);
1098e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1098f 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 26     if( pgno==0 &
10990 26 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31  & pCache->pPage1
10991 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74   ){.      memset
10992 28 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 2d  (pCache->pPage1-
10993 3e 70 44 61 74 61 2c 20 30 2c 20 70 43 61 63 68  >pData, 0, pCach
10994 65 2d 3e 73 7a 50 61 67 65 29 3b 0a 20 20 20 20  e->szPage);.    
10995 20 20 70 67 6e 6f 20 3d 20 31 3b 0a 20 20 20 20    pgno = 1;.    
10996 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  }.    sqlite3Glo
10997 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
10998 2e 78 54 72 75 6e 63 61 74 65 28 70 43 61 63 68  .xTruncate(pCach
10999 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2b  e->pCache, pgno+
1099a 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  1);.  }.}../*.**
1099b 20 43 6c 6f 73 65 20 61 20 63 61 63 68 65 2e 0a   Close a cache..
1099c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1099d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
1099e 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68 65  acheClose(PCache
1099f 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 66 28   *pCache){.  if(
109a0 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
109a1 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
109a2 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
109a3 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63 68  e.xDestroy(pCach
109a4 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 7d 0a  e->pCache);.  }.
109a5 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 69 73 63 61 72  }../* .** Discar
109a6 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  d the contents o
109a7 66 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a  f the cache..*/.
109a8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
109a9 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
109aa 65 43 6c 65 61 72 28 50 43 61 63 68 65 20 2a 70  eClear(PCache *p
109ab 43 61 63 68 65 29 7b 0a 20 20 73 71 6c 69 74 65  Cache){.  sqlite
109ac 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28  3PcacheTruncate(
109ad 70 43 61 63 68 65 2c 20 30 29 3b 0a 7d 0a 0a 2f  pCache, 0);.}../
109ae 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c  *.** Merge two l
109af 69 73 74 73 20 6f 66 20 70 61 67 65 73 20 63 6f  ists of pages co
109b0 6e 6e 65 63 74 65 64 20 62 79 20 70 44 69 72 74  nnected by pDirt
109b1 79 20 61 6e 64 20 69 6e 20 70 67 6e 6f 20 6f 72  y and in pgno or
109b2 64 65 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 62  der..** Do not b
109b3 6f 74 68 20 66 69 78 69 6e 67 20 74 68 65 20 70  oth fixing the p
109b4 44 69 72 74 79 50 72 65 76 20 70 6f 69 6e 74 65  DirtyPrev pointe
109b5 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67  rs..*/.static Pg
109b6 48 64 72 20 2a 70 63 61 63 68 65 4d 65 72 67 65  Hdr *pcacheMerge
109b7 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 20  DirtyList(PgHdr 
109b8 2a 70 41 2c 20 50 67 48 64 72 20 2a 70 42 29 7b  *pA, PgHdr *pB){
109b9 0a 20 20 50 67 48 64 72 20 72 65 73 75 6c 74 2c  .  PgHdr result,
109ba 20 2a 70 54 61 69 6c 3b 0a 20 20 70 54 61 69 6c   *pTail;.  pTail
109bb 20 3d 20 26 72 65 73 75 6c 74 3b 0a 20 20 77 68   = &result;.  wh
109bc 69 6c 65 28 20 70 41 20 26 26 20 70 42 20 29 7b  ile( pA && pB ){
109bd 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 70 67 6e  .    if( pA->pgn
109be 6f 3c 70 42 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20  o<pB->pgno ){.  
109bf 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74      pTail->pDirt
109c0 79 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 70 54  y = pA;.      pT
109c1 61 69 6c 20 3d 20 70 41 3b 0a 20 20 20 20 20 20  ail = pA;.      
109c2 70 41 20 3d 20 70 41 2d 3e 70 44 69 72 74 79 3b  pA = pA->pDirty;
109c3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
109c4 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20    pTail->pDirty 
109c5 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 54 61 69  = pB;.      pTai
109c6 6c 20 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42  l = pB;.      pB
109c7 20 3d 20 70 42 2d 3e 70 44 69 72 74 79 3b 0a 20   = pB->pDirty;. 
109c8 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
109c9 41 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e  A ){.    pTail->
109ca 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 7d  pDirty = pA;.  }
109cb 65 6c 73 65 20 69 66 28 20 70 42 20 29 7b 0a 20  else if( pB ){. 
109cc 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79     pTail->pDirty
109cd 20 3d 20 70 42 3b 0a 20 20 7d 65 6c 73 65 7b 0a   = pB;.  }else{.
109ce 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74      pTail->pDirt
109cf 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  y = 0;.  }.  ret
109d0 75 72 6e 20 72 65 73 75 6c 74 2e 70 44 69 72 74  urn result.pDirt
109d1 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74  y;.}../*.** Sort
109d2 20 74 68 65 20 6c 69 73 74 20 6f 66 20 70 61 67   the list of pag
109d3 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20  es in accending 
109d4 6f 72 64 65 72 20 62 79 20 70 67 6e 6f 2e 20 20  order by pgno.  
109d5 50 61 67 65 73 20 61 72 65 0a 2a 2a 20 63 6f 6e  Pages are.** con
109d6 6e 65 63 74 65 64 20 62 79 20 70 44 69 72 74 79  nected by pDirty
109d7 20 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 20   pointers.  The 
109d8 70 44 69 72 74 79 50 72 65 76 20 70 6f 69 6e 74  pDirtyPrev point
109d9 65 72 73 20 61 72 65 0a 2a 2a 20 63 6f 72 72 75  ers are.** corru
109da 70 74 65 64 20 62 79 20 74 68 69 73 20 73 6f 72  pted by this sor
109db 74 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 74  t..**.** Since t
109dc 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 6d  here cannot be m
109dd 6f 72 65 20 74 68 61 6e 20 32 5e 33 31 20 64 69  ore than 2^31 di
109de 73 74 69 6e 63 74 20 70 61 67 65 73 20 69 6e 20  stinct pages in 
109df 61 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 20 74  a database,.** t
109e0 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 6d  here cannot be m
109e1 6f 72 65 20 74 68 61 6e 20 33 31 20 62 75 63 6b  ore than 31 buck
109e2 65 74 73 20 72 65 71 75 69 72 65 64 20 62 79 20  ets required by 
109e3 74 68 65 20 6d 65 72 67 65 20 73 6f 72 74 65 72  the merge sorter
109e4 2e 0a 2a 2a 20 4f 6e 65 20 65 78 74 72 61 20 62  ..** One extra b
109e5 75 63 6b 65 74 20 69 73 20 61 64 64 65 64 20 74  ucket is added t
109e6 6f 20 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77  o catch overflow
109e7 20 69 6e 20 63 61 73 65 20 73 6f 6d 65 74 68 69   in case somethi
109e8 6e 67 0a 2a 2a 20 65 76 65 72 20 63 68 61 6e 67  ng.** ever chang
109e9 65 73 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 70  es to make the p
109ea 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65  revious sentence
109eb 20 69 6e 63 6f 72 72 65 63 74 2e 0a 2a 2f 0a 23   incorrect..*/.#
109ec 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55  define N_SORT_BU
109ed 43 4b 45 54 20 20 33 32 0a 73 74 61 74 69 63 20  CKET  32.static 
109ee 50 67 48 64 72 20 2a 70 63 61 63 68 65 53 6f 72  PgHdr *pcacheSor
109ef 74 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72  tDirtyList(PgHdr
109f0 20 2a 70 49 6e 29 7b 0a 20 20 50 67 48 64 72 20   *pIn){.  PgHdr 
109f1 2a 61 5b 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  *a[N_SORT_BUCKET
109f2 5d 2c 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a  ], *p;.  int i;.
109f3 20 20 6d 65 6d 73 65 74 28 61 2c 20 30 2c 20 73    memset(a, 0, s
109f4 69 7a 65 6f 66 28 61 29 29 3b 0a 20 20 77 68 69  izeof(a));.  whi
109f5 6c 65 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 70  le( pIn ){.    p
109f6 20 3d 20 70 49 6e 3b 0a 20 20 20 20 70 49 6e 20   = pIn;.    pIn 
109f7 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20  = p->pDirty;.   
109f8 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a   p->pDirty = 0;.
109f9 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 41 4c 57      for(i=0; ALW
109fa 41 59 53 28 69 3c 4e 5f 53 4f 52 54 5f 42 55 43  AYS(i<N_SORT_BUC
109fb 4b 45 54 2d 31 29 3b 20 69 2b 2b 29 7b 0a 20 20  KET-1); i++){.  
109fc 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d 30 20      if( a[i]==0 
109fd 29 7b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20  ){.        a[i] 
109fe 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = p;.        bre
109ff 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ak;.      }else{
10a00 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 63 61  .        p = pca
10a01 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73  cheMergeDirtyLis
10a02 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20  t(a[i], p);.    
10a03 20 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a 20 20      a[i] = 0;.  
10a04 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
10a05 69 66 28 20 4e 45 56 45 52 28 69 3d 3d 4e 5f 53  if( NEVER(i==N_S
10a06 4f 52 54 5f 42 55 43 4b 45 54 2d 31 29 20 29 7b  ORT_BUCKET-1) ){
10a07 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 67 65 74  .      /* To get
10a08 20 68 65 72 65 2c 20 74 68 65 72 65 20 6e 65 65   here, there nee
10a09 64 20 74 6f 20 62 65 20 32 5e 28 4e 5f 53 4f 52  d to be 2^(N_SOR
10a0a 54 5f 42 55 43 4b 45 54 29 20 65 6c 65 6d 65 6e  T_BUCKET) elemen
10a0b 74 73 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 74  ts in.      ** t
10a0c 68 65 20 69 6e 70 75 74 20 6c 69 73 74 2e 20 20  he input list.  
10a0d 42 75 74 20 74 68 61 74 20 69 73 20 69 6d 70 6f  But that is impo
10a0e 73 73 69 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2f  ssible..      */
10a0f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 63  .      a[i] = pc
10a10 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69  acheMergeDirtyLi
10a11 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20  st(a[i], p);.   
10a12 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b 30   }.  }.  p = a[0
10a13 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  ];.  for(i=1; i<
10a14 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20 69  N_SORT_BUCKET; i
10a15 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 70 63 61  ++){.    p = pca
10a16 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73  cheMergeDirtyLis
10a17 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20 7d 0a  t(p, a[i]);.  }.
10a18 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
10a19 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69  *.** Return a li
10a1a 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20  st of all dirty 
10a1b 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63  pages in the cac
10a1c 68 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61  he, sorted by pa
10a1d 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53 51  ge number..*/.SQ
10a1e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 48  LITE_PRIVATE PgH
10a1f 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68  dr *sqlite3Pcach
10a20 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 63 68  eDirtyList(PCach
10a21 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67  e *pCache){.  Pg
10a22 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d  Hdr *p;.  for(p=
10a23 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20  pCache->pDirty; 
10a24 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65  p; p=p->pDirtyNe
10a25 78 74 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72  xt){.    p->pDir
10a26 74 79 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65  ty = p->pDirtyNe
10a27 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  xt;.  }.  return
10a28 20 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 79   pcacheSortDirty
10a29 4c 69 73 74 28 70 43 61 63 68 65 2d 3e 70 44 69  List(pCache->pDi
10a2a 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  rty);.}../* .** 
10a2b 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c  Return the total
10a2c 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   number of refer
10a2d 65 6e 63 65 64 20 70 61 67 65 73 20 68 65 6c 64  enced pages held
10a2e 20 62 79 20 74 68 65 20 63 61 63 68 65 2e 0a 2a   by the cache..*
10a2f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10a30 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
10a31 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 68  heRefCount(PCach
10a32 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65  e *pCache){.  re
10a33 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 52 65  turn pCache->nRe
10a34 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  f;.}../*.** Retu
10a35 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
10a36 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
10a37 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 64  he page supplied
10a38 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e   as an argument.
10a39 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10a3a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
10a3b 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74  achePageRefcount
10a3c 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 72 65  (PgHdr *p){.  re
10a3d 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b 0a 7d 0a  turn p->nRef;.}.
10a3e 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./* .** Return t
10a3f 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
10a40 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
10a41 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cache..*/.SQLITE
10a42 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10a43 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f  ite3PcachePageco
10a44 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43 61 63  unt(PCache *pCac
10a45 68 65 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65  he){.  int nPage
10a46 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 61 63   = 0;.  if( pCac
10a47 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20  he->pCache ){.  
10a48 20 20 6e 50 61 67 65 20 3d 20 73 71 6c 69 74 65    nPage = sqlite
10a49 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
10a4a 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 28  ache.xPagecount(
10a4b 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b  pCache->pCache);
10a4c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 50  .  }.  return nP
10a4d 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53  age;.}..#ifdef S
10a4e 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  QLITE_TEST./*.**
10a4f 20 47 65 74 20 74 68 65 20 73 75 67 67 65 73 74   Get the suggest
10a50 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61  ed cache-size va
10a51 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  lue..*/.SQLITE_P
10a52 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
10a53 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65  e3PcacheGetCache
10a54 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61  size(PCache *pCa
10a55 63 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  che){.  return p
10a56 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 7d 0a 23  Cache->nMax;.}.#
10a57 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  endif../*.** Set
10a58 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63   the suggested c
10a59 61 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e  ache-size value.
10a5a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10a5b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
10a5c 63 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a  cacheSetCachesiz
10a5d 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  e(PCache *pCache
10a5e 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20  , int mxPage){. 
10a5f 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20   pCache->nMax = 
10a60 6d 78 50 61 67 65 3b 0a 20 20 69 66 28 20 70 43  mxPage;.  if( pC
10a61 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a  ache->pCache ){.
10a62 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
10a63 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78  lConfig.pcache.x
10a64 43 61 63 68 65 73 69 7a 65 28 70 43 61 63 68 65  Cachesize(pCache
10a65 2d 3e 70 43 61 63 68 65 2c 20 6d 78 50 61 67 65  ->pCache, mxPage
10a66 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 64 65  );.  }.}..#if de
10a67 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 48 45  fined(SQLITE_CHE
10a68 43 4b 5f 50 41 47 45 53 29 20 7c 7c 20 64 65 66  CK_PAGES) || def
10a69 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
10a6a 47 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6c 6c  G)./*.** For all
10a6b 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72   dirty pages cur
10a6c 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 63 61  rently in the ca
10a6d 63 68 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20  che, invoke the 
10a6e 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 63 61 6c  specified.** cal
10a6f 6c 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 6f  lback. This is o
10a70 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20  nly used if the 
10a71 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
10a72 45 53 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 64  ES macro is.** d
10a73 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
10a74 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
10a75 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72  qlite3PcacheIter
10a76 61 74 65 44 69 72 74 79 28 50 43 61 63 68 65 20  ateDirty(PCache 
10a77 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a  *pCache, void (*
10a78 78 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 29  xIter)(PgHdr *))
10a79 7b 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74  {.  PgHdr *pDirt
10a7a 79 3b 0a 20 20 66 6f 72 28 70 44 69 72 74 79 3d  y;.  for(pDirty=
10a7b 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20  pCache->pDirty; 
10a7c 70 44 69 72 74 79 3b 20 70 44 69 72 74 79 3d 70  pDirty; pDirty=p
10a7d 44 69 72 74 79 2d 3e 70 44 69 72 74 79 4e 65 78  Dirty->pDirtyNex
10a7e 74 29 7b 0a 20 20 20 20 78 49 74 65 72 28 70 44  t){.    xIter(pD
10a7f 69 72 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  irty);.  }.}.#en
10a80 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
10a81 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63  **** End of pcac
10a82 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  he.c ***********
10a83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a85 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
10a86 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
10a87 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a  pcache1.c ******
10a88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a8a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
10a8b 4e 6f 76 65 6d 62 65 72 20 30 35 0a 2a 2a 0a 2a  November 05.**.*
10a8c 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
10a8d 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
10a8e 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
10a8f 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
10a90 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
10a91 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
10a92 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
10a93 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
10a94 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
10a95 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
10a96 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
10a97 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
10a98 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
10a99 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
10a9a 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
10a9b 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
10a9c 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
10a9d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
10a9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10aa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
10aa2 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
10aa3 6d 65 6e 74 73 20 74 68 65 20 64 65 66 61 75 6c  ments the defaul
10aa4 74 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70  t page cache imp
10aa5 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 74 68 65  lementation (the
10aa6 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63  .** sqlite3_pcac
10aa7 68 65 20 69 6e 74 65 72 66 61 63 65 29 2e 20 49  he interface). I
10aa8 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20  t also contains 
10aa9 70 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70 6c  part of the impl
10aaa 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66  ementation.** of
10aab 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46   the SQLITE_CONF
10aac 49 47 5f 50 41 47 45 43 41 43 48 45 20 61 6e 64  IG_PAGECACHE and
10aad 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
10aae 5f 6d 65 6d 6f 72 79 28 29 20 66 65 61 74 75 72  _memory() featur
10aaf 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65  es..** If the de
10ab0 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65  fault page cache
10ab1 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
10ab2 69 73 20 6f 76 65 72 72 69 64 65 6e 2c 20 74 68  is overriden, th
10ab3 65 6e 20 6e 65 69 74 68 65 72 20 6f 66 0a 2a 2a  en neither of.**
10ab4 20 74 68 65 73 65 20 74 77 6f 20 66 65 61 74 75   these two featu
10ab5 72 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  res are availabl
10ab6 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  e..**.** @(#) $I
10ab7 64 3a 20 70 63 61 63 68 65 31 2e 63 2c 76 20 31  d: pcache1.c,v 1
10ab8 2e 31 39 20 32 30 30 39 2f 30 37 2f 31 37 20 31  .19 2009/07/17 1
10ab9 31 3a 34 34 3a 30 37 20 64 72 68 20 45 78 70 20  1:44:07 drh Exp 
10aba 24 0a 2a 2f 0a 0a 0a 74 79 70 65 64 65 66 20 73  $.*/...typedef s
10abb 74 72 75 63 74 20 50 43 61 63 68 65 31 20 50 43  truct PCache1 PC
10abc 61 63 68 65 31 3b 0a 74 79 70 65 64 65 66 20 73  ache1;.typedef s
10abd 74 72 75 63 74 20 50 67 48 64 72 31 20 50 67 48  truct PgHdr1 PgH
10abe 64 72 31 3b 0a 74 79 70 65 64 65 66 20 73 74 72  dr1;.typedef str
10abf 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20 50  uct PgFreeslot P
10ac0 67 46 72 65 65 73 6c 6f 74 3b 0a 0a 2f 2a 20 50  gFreeslot;../* P
10ac1 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 75 63  ointers to struc
10ac2 74 75 72 65 73 20 6f 66 20 74 68 69 73 20 74 79  tures of this ty
10ac3 70 65 20 61 72 65 20 63 61 73 74 20 61 6e 64 20  pe are cast and 
10ac4 72 65 74 75 72 6e 65 64 20 61 73 20 0a 2a 2a 20  returned as .** 
10ac5 6f 70 61 71 75 65 20 73 71 6c 69 74 65 33 5f 70  opaque sqlite3_p
10ac6 63 61 63 68 65 2a 20 68 61 6e 64 6c 65 73 0a 2a  cache* handles.*
10ac7 2f 0a 73 74 72 75 63 74 20 50 43 61 63 68 65 31  /.struct PCache1
10ac8 20 7b 0a 20 20 2f 2a 20 43 61 63 68 65 20 63 6f   {.  /* Cache co
10ac9 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 61 72 61  nfiguration para
10aca 6d 65 74 65 72 73 2e 20 50 61 67 65 20 73 69 7a  meters. Page siz
10acb 65 20 28 73 7a 50 61 67 65 29 20 61 6e 64 20 74  e (szPage) and t
10acc 68 65 20 70 75 72 67 65 61 62 6c 65 0a 20 20 2a  he purgeable.  *
10acd 2a 20 66 6c 61 67 20 28 62 50 75 72 67 65 61 62  * flag (bPurgeab
10ace 6c 65 29 20 61 72 65 20 73 65 74 20 77 68 65 6e  le) are set when
10acf 20 74 68 65 20 63 61 63 68 65 20 69 73 20 63 72   the cache is cr
10ad0 65 61 74 65 64 2e 20 6e 4d 61 78 20 6d 61 79 20  eated. nMax may 
10ad1 62 65 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65  be .  ** modifie
10ad2 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20 62 79  d at any time by
10ad3 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 70   a call to the p
10ad4 63 61 63 68 65 31 43 61 63 68 65 53 69 7a 65 28  cache1CacheSize(
10ad5 29 20 6d 65 74 68 6f 64 2e 0a 20 20 2a 2a 20 54  ) method..  ** T
10ad6 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20  he global mutex 
10ad7 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65  must be held whe
10ad8 6e 20 61 63 63 65 73 73 69 6e 67 20 6e 4d 61 78  n accessing nMax
10ad9 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50  ..  */.  int szP
10ada 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
10adb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10adc 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65  Size of allocate
10add 64 20 70 61 67 65 73 20 69 6e 20 62 79 74 65 73  d pages in bytes
10ade 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65   */.  int bPurge
10adf 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  able;           
10ae0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
10ae1 65 20 69 66 20 63 61 63 68 65 20 69 73 20 70 75  e if cache is pu
10ae2 72 67 65 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73  rgeable */.  uns
10ae3 69 67 6e 65 64 20 69 6e 74 20 6e 4d 69 6e 3b 20  igned int nMin; 
10ae4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ae5 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6e 75 6d 62   /* Minimum numb
10ae6 65 72 20 6f 66 20 70 61 67 65 73 20 72 65 73 65  er of pages rese
10ae7 72 76 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  rved */.  unsign
10ae8 65 64 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20  ed int nMax;    
10ae9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10aea 20 43 6f 6e 66 69 67 75 72 65 64 20 22 63 61 63   Configured "cac
10aeb 68 65 5f 73 69 7a 65 22 20 76 61 6c 75 65 20 2a  he_size" value *
10aec 2f 0a 0a 20 20 2f 2a 20 48 61 73 68 20 74 61 62  /..  /* Hash tab
10aed 6c 65 20 6f 66 20 61 6c 6c 20 70 61 67 65 73 2e  le of all pages.
10aee 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
10aef 61 72 69 61 62 6c 65 73 20 6d 61 79 20 6f 6e 6c  ariables may onl
10af0 79 20 62 65 20 61 63 63 65 73 73 65 64 0a 20 20  y be accessed.  
10af1 2a 2a 20 77 68 65 6e 20 74 68 65 20 61 63 63 65  ** when the acce
10af2 73 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67 20  ssor is holding 
10af3 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  the global mutex
10af4 20 28 73 65 65 20 70 63 61 63 68 65 31 45 6e 74   (see pcache1Ent
10af5 65 72 4d 75 74 65 78 28 29 20 0a 20 20 2a 2a 20  erMutex() .  ** 
10af6 61 6e 64 20 70 63 61 63 68 65 31 4c 65 61 76 65  and pcache1Leave
10af7 4d 75 74 65 78 28 29 29 2e 0a 20 20 2a 2f 0a 20  Mutex())..  */. 
10af8 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 52   unsigned int nR
10af9 65 63 79 63 6c 61 62 6c 65 3b 20 20 20 20 20 20  ecyclable;      
10afa 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
10afb 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 4c  f pages in the L
10afc 52 55 20 6c 69 73 74 20 2a 2f 0a 20 20 75 6e 73  RU list */.  uns
10afd 69 67 6e 65 64 20 69 6e 74 20 6e 50 61 67 65 3b  igned int nPage;
10afe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10aff 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
10b00 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 48   of pages in apH
10b01 61 73 68 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ash */.  unsigne
10b02 64 20 69 6e 74 20 6e 48 61 73 68 3b 20 20 20 20  d int nHash;    
10b03 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b04 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20  Number of slots 
10b05 69 6e 20 61 70 48 61 73 68 5b 5d 20 2a 2f 0a 20  in apHash[] */. 
10b06 20 50 67 48 64 72 31 20 2a 2a 61 70 48 61 73 68   PgHdr1 **apHash
10b07 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10b08 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62       /* Hash tab
10b09 6c 65 20 66 6f 72 20 66 61 73 74 20 6c 6f 6f 6b  le for fast look
10b0a 75 70 20 62 79 20 6b 65 79 20 2a 2f 0a 0a 20 20  up by key */..  
10b0b 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4d 61  unsigned int iMa
10b0c 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  xKey;           
10b0d 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 6b      /* Largest k
10b0e 65 79 20 73 65 65 6e 20 73 69 6e 63 65 20 78 54  ey seen since xT
10b0f 72 75 6e 63 61 74 65 28 29 20 2a 2f 0a 7d 3b 0a  runcate() */.};.
10b10 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 63 61 63 68  ./*.** Each cach
10b11 65 20 65 6e 74 72 79 20 69 73 20 72 65 70 72 65  e entry is repre
10b12 73 65 6e 74 65 64 20 62 79 20 61 6e 20 69 6e 73  sented by an ins
10b13 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
10b14 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63  lowing .** struc
10b15 74 75 72 65 2e 20 41 20 62 75 66 66 65 72 20 6f  ture. A buffer o
10b16 66 20 50 67 48 64 72 31 2e 70 43 61 63 68 65 2d  f PgHdr1.pCache-
10b17 3e 73 7a 50 61 67 65 20 62 79 74 65 73 20 69 73  >szPage bytes is
10b18 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a 20 64   allocated .** d
10b19 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 20 74  irectly before t
10b1a 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 6e  his structure in
10b1b 20 6d 65 6d 6f 72 79 20 28 73 65 65 20 74 68 65   memory (see the
10b1c 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28   PGHDR1_TO_PAGE(
10b1d 29 20 0a 2a 2a 20 6d 61 63 72 6f 20 62 65 6c 6f  ) .** macro belo
10b1e 77 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67  w)..*/.struct Pg
10b1f 48 64 72 31 20 7b 0a 20 20 75 6e 73 69 67 6e 65  Hdr1 {.  unsigne
10b20 64 20 69 6e 74 20 69 4b 65 79 3b 20 20 20 20 20  d int iKey;     
10b21 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 76          /* Key v
10b22 61 6c 75 65 20 28 70 61 67 65 20 6e 75 6d 62 65  alue (page numbe
10b23 72 29 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a  r) */.  PgHdr1 *
10b24 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  pNext;          
10b25 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69         /* Next i
10b26 6e 20 68 61 73 68 20 74 61 62 6c 65 20 63 68 61  n hash table cha
10b27 69 6e 20 2a 2f 0a 20 20 50 43 61 63 68 65 31 20  in */.  PCache1 
10b28 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 20  *pCache;        
10b29 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20         /* Cache 
10b2a 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 6f  that currently o
10b2b 77 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f  wns this page */
10b2c 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 4e  .  PgHdr1 *pLruN
10b2d 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
10b2e 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 4c 52 55    /* Next in LRU
10b2f 20 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65   list of unpinne
10b30 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48  d pages */.  PgH
10b31 64 72 31 20 2a 70 4c 72 75 50 72 65 76 3b 20 20  dr1 *pLruPrev;  
10b32 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
10b33 72 65 76 69 6f 75 73 20 69 6e 20 4c 52 55 20 6c  revious in LRU l
10b34 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20  ist of unpinned 
10b35 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  pages */.};../*.
10b36 2a 2a 20 46 72 65 65 20 73 6c 6f 74 73 20 69 6e  ** Free slots in
10b37 20 74 68 65 20 61 6c 6c 6f 63 61 74 6f 72 20 75   the allocator u
10b38 73 65 64 20 74 6f 20 64 69 76 69 64 65 20 75 70  sed to divide up
10b39 20 74 68 65 20 62 75 66 66 65 72 20 70 72 6f 76   the buffer prov
10b3a 69 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68  ided using.** th
10b3b 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
10b3c 50 41 47 45 43 41 43 48 45 20 6d 65 63 68 61 6e  PAGECACHE mechan
10b3d 69 73 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50  ism..*/.struct P
10b3e 67 46 72 65 65 73 6c 6f 74 20 7b 0a 20 20 50 67  gFreeslot {.  Pg
10b3f 46 72 65 65 73 6c 6f 74 20 2a 70 4e 65 78 74 3b  Freeslot *pNext;
10b40 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20 73    /* Next free s
10b41 6c 6f 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  lot */.};../*.**
10b42 20 47 6c 6f 62 61 6c 20 64 61 74 61 20 75 73 65   Global data use
10b43 64 20 62 79 20 74 68 69 73 20 63 61 63 68 65 2e  d by this cache.
10b44 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54  .*/.static SQLIT
10b45 45 5f 57 53 44 20 73 74 72 75 63 74 20 50 43 61  E_WSD struct PCa
10b46 63 68 65 47 6c 6f 62 61 6c 20 7b 0a 20 20 73 71  cheGlobal {.  sq
10b47 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
10b48 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ex;             
10b49 20 20 2f 2a 20 73 74 61 74 69 63 20 6d 75 74 65    /* static mute
10b4a 78 20 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  x MUTEX_STATIC_L
10b4b 52 55 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 4d 61  RU */..  int nMa
10b4c 78 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  xPage;          
10b4d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b4e 53 75 6d 20 6f 66 20 6e 4d 61 78 50 61 67 65 20  Sum of nMaxPage 
10b4f 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 61  for purgeable ca
10b50 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  ches */.  int nM
10b51 69 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  inPage;         
10b52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b53 20 53 75 6d 20 6f 66 20 6e 4d 69 6e 50 61 67 65   Sum of nMinPage
10b54 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63   for purgeable c
10b55 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  aches */.  int n
10b56 43 75 72 72 65 6e 74 50 61 67 65 3b 20 20 20 20  CurrentPage;    
10b57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10b58 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 75 72 67  * Number of purg
10b59 65 61 62 6c 65 20 70 61 67 65 73 20 61 6c 6c 6f  eable pages allo
10b5a 63 61 74 65 64 20 2a 2f 0a 20 20 50 67 48 64 72  cated */.  PgHdr
10b5b 31 20 2a 70 4c 72 75 48 65 61 64 2c 20 2a 70 4c  1 *pLruHead, *pL
10b5c 72 75 54 61 69 6c 3b 20 20 20 20 20 20 20 20 2f  ruTail;        /
10b5d 2a 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e  * LRU list of un
10b5e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a  pinned pages */.
10b5f 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 20  .  /* Variables 
10b60 72 65 6c 61 74 65 64 20 74 6f 20 53 51 4c 49 54  related to SQLIT
10b61 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
10b62 48 45 20 73 65 74 74 69 6e 67 73 2e 20 2a 2f 0a  HE settings. */.
10b63 20 20 69 6e 74 20 73 7a 53 6c 6f 74 3b 20 20 20    int szSlot;   
10b64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b65 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
10b66 20 65 61 63 68 20 66 72 65 65 20 73 6c 6f 74 20   each free slot 
10b67 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72  */.  void *pStar
10b68 74 2c 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20  t, *pEnd;       
10b69 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 75 6e           /* Boun
10b6a 64 73 20 6f 66 20 70 61 67 65 63 61 63 68 65 20  ds of pagecache 
10b6b 6d 61 6c 6c 6f 63 20 72 61 6e 67 65 20 2a 2f 0a  malloc range */.
10b6c 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 46    PgFreeslot *pF
10b6d 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ree;            
10b6e 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 70 61        /* Free pa
10b6f 67 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69  ge blocks */.  i
10b70 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20  nt isInit;      
10b71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b72 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e     /* True if in
10b73 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 7d 20 70  itialized */.} p
10b74 63 61 63 68 65 31 5f 67 3b 0a 0a 2f 2a 0a 2a 2a  cache1_g;../*.**
10b75 20 41 6c 6c 20 63 6f 64 65 20 69 6e 20 74 68 69   All code in thi
10b76 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 63  s file should ac
10b77 63 65 73 73 20 74 68 65 20 67 6c 6f 62 61 6c 20  cess the global 
10b78 73 74 72 75 63 74 75 72 65 20 61 62 6f 76 65 20  structure above 
10b79 76 69 61 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73  via the.** alias
10b7a 20 22 70 63 61 63 68 65 31 22 2e 20 54 68 69 73   "pcache1". This
10b7b 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 74 68   ensures that th
10b7c 65 20 57 53 44 20 65 6d 75 6c 61 74 69 6f 6e 20  e WSD emulation 
10b7d 69 73 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20  is used when.** 
10b7e 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 73 79  compiling for sy
10b7f 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f  stems that do no
10b80 74 20 73 75 70 70 6f 72 74 20 72 65 61 6c 20 57  t support real W
10b81 53 44 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70  SD..*/.#define p
10b82 63 61 63 68 65 31 20 28 47 4c 4f 42 41 4c 28 73  cache1 (GLOBAL(s
10b83 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62  truct PCacheGlob
10b84 61 6c 2c 20 70 63 61 63 68 65 31 5f 67 29 29 0a  al, pcache1_g)).
10b85 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 50 67  ./*.** When a Pg
10b86 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20 69  Hdr1 structure i
10b87 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65  s allocated, the
10b88 20 61 73 73 6f 63 69 61 74 65 64 20 50 43 61 63   associated PCac
10b89 68 65 31 2e 73 7a 50 61 67 65 0a 2a 2a 20 62 79  he1.szPage.** by
10b8a 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20  tes of data are 
10b8b 6c 6f 63 61 74 65 64 20 64 69 72 65 63 74 6c 79  located directly
10b8c 20 62 65 66 6f 72 65 20 69 74 20 69 6e 20 6d 65   before it in me
10b8d 6d 6f 72 79 20 28 69 2e 65 2e 20 74 68 65 20 74  mory (i.e. the t
10b8e 6f 74 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66 20  otal.** size of 
10b8f 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  the allocation i
10b90 73 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29  s sizeof(PgHdr1)
10b91 2b 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65 20  +PCache1.szPage 
10b92 62 79 74 65 29 2e 20 54 68 65 0a 2a 2a 20 50 47  byte). The.** PG
10b93 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 29 20 6d  HDR1_TO_PAGE() m
10b94 61 63 72 6f 20 74 61 6b 65 73 20 61 20 70 6f 69  acro takes a poi
10b95 6e 74 65 72 20 74 6f 20 61 20 50 67 48 64 72 31  nter to a PgHdr1
10b96 20 73 74 72 75 63 74 75 72 65 20 61 73 0a 2a 2a   structure as.**
10b97 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 61 6e 64   an argument and
10b98 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
10b99 65 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69  er to the associ
10b9a 61 74 65 64 20 62 6c 6f 63 6b 20 6f 66 20 73 7a  ated block of sz
10b9b 50 61 67 65 0a 2a 2a 20 62 79 74 65 73 2e 20 54  Page.** bytes. T
10b9c 68 65 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52  he PAGE_TO_PGHDR
10b9d 31 28 29 20 6d 61 63 72 6f 20 64 6f 65 73 20 74  1() macro does t
10b9e 68 65 20 6f 70 70 6f 73 69 74 65 3a 20 69 74 73  he opposite: its
10b9f 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20   argument is.** 
10ba0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62  a pointer to a b
10ba1 6c 6f 63 6b 20 6f 66 20 73 7a 50 61 67 65 20 62  lock of szPage b
10ba2 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 6e 64  ytes of data and
10ba3 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
10ba4 65 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  e is.** a pointe
10ba5 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 61  r to the associa
10ba6 74 65 64 20 50 67 48 64 72 31 20 73 74 72 75 63  ted PgHdr1 struc
10ba7 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 73  ture..**.**   as
10ba8 73 65 72 74 28 20 50 47 48 44 52 31 5f 54 4f 5f  sert( PGHDR1_TO_
10ba9 50 41 47 45 28 50 41 47 45 5f 54 4f 5f 50 47 48  PAGE(PAGE_TO_PGH
10baa 44 52 31 28 70 43 61 63 68 65 2c 20 58 29 29 3d  DR1(pCache, X))=
10bab 3d 58 20 29 3b 0a 2a 2f 0a 23 64 65 66 69 6e 65  =X );.*/.#define
10bac 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28   PGHDR1_TO_PAGE(
10bad 70 29 20 20 20 20 28 76 6f 69 64 2a 29 28 28 28  p)    (void*)(((
10bae 63 68 61 72 2a 29 70 29 20 2d 20 70 2d 3e 70 43  char*)p) - p->pC
10baf 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 0a 23 64  ache->szPage).#d
10bb0 65 66 69 6e 65 20 50 41 47 45 5f 54 4f 5f 50 47  efine PAGE_TO_PG
10bb1 48 44 52 31 28 63 2c 20 70 29 20 28 50 67 48 64  HDR1(c, p) (PgHd
10bb2 72 31 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20  r1*)(((char*)p) 
10bb3 2b 20 63 2d 3e 73 7a 50 61 67 65 29 0a 0a 2f 2a  + c->szPage)../*
10bb4 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 65 6e  .** Macros to en
10bb5 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 74 68  ter and leave th
10bb6 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6d 75 74  e global LRU mut
10bb7 65 78 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70  ex..*/.#define p
10bb8 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
10bb9 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  () sqlite3_mutex
10bba 5f 65 6e 74 65 72 28 70 63 61 63 68 65 31 2e 6d  _enter(pcache1.m
10bbb 75 74 65 78 29 0a 23 64 65 66 69 6e 65 20 70 63  utex).#define pc
10bbc 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28  ache1LeaveMutex(
10bbd 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ) sqlite3_mutex_
10bbe 6c 65 61 76 65 28 70 63 61 63 68 65 31 2e 6d 75  leave(pcache1.mu
10bbf 74 65 78 29 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  tex)../*********
10bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bc1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bc2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bc3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bc4 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
10bc5 20 50 61 67 65 20 41 6c 6c 6f 63 61 74 69 6f 6e   Page Allocation
10bc6 2f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  /SQLITE_CONFIG_P
10bc7 43 41 43 48 45 20 52 65 6c 61 74 65 64 20 46 75  CACHE Related Fu
10bc8 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a  nctions ********
10bc9 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ******/../*.** T
10bca 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
10bcb 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 69 6e  called during in
10bcc 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 66 20  itialization if 
10bcd 61 20 73 74 61 74 69 63 20 62 75 66 66 65 72 20  a static buffer 
10bce 69 73 20 0a 2a 2a 20 73 75 70 70 6c 69 65 64 20  is .** supplied 
10bcf 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 70  to use for the p
10bd0 61 67 65 2d 63 61 63 68 65 20 62 79 20 70 61 73  age-cache by pas
10bd1 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f  sing the SQLITE_
10bd2 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10bd3 0a 2a 2a 20 76 65 72 62 20 74 6f 20 73 71 6c 69  .** verb to sqli
10bd4 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 20 50 61  te3_config(). Pa
10bd5 72 61 6d 65 74 65 72 20 70 42 75 66 20 70 6f 69  rameter pBuf poi
10bd6 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61  nts to an alloca
10bd7 74 69 6f 6e 20 6c 61 72 67 65 0a 2a 2a 20 65 6e  tion large.** en
10bd8 6f 75 67 68 20 74 6f 20 63 6f 6e 74 61 69 6e 20  ough to contain 
10bd9 27 6e 27 20 62 75 66 66 65 72 73 20 6f 66 20 27  'n' buffers of '
10bda 73 7a 27 20 62 79 74 65 73 20 65 61 63 68 2e 0a  sz' bytes each..
10bdb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
10bdc 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43  E void sqlite3PC
10bdd 61 63 68 65 42 75 66 66 65 72 53 65 74 75 70 28  acheBufferSetup(
10bde 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20  void *pBuf, int 
10bdf 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 66  sz, int n){.  if
10be0 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74  ( pcache1.isInit
10be1 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 6c   ){.    PgFreesl
10be2 6f 74 20 2a 70 3b 0a 20 20 20 20 73 7a 20 3d 20  ot *p;.    sz = 
10be3 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 0a  ROUNDDOWN8(sz);.
10be4 20 20 20 20 70 63 61 63 68 65 31 2e 73 7a 53 6c      pcache1.szSl
10be5 6f 74 20 3d 20 73 7a 3b 0a 20 20 20 20 70 63 61  ot = sz;.    pca
10be6 63 68 65 31 2e 70 53 74 61 72 74 20 3d 20 70 42  che1.pStart = pB
10be7 75 66 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e  uf;.    pcache1.
10be8 70 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 77  pFree = 0;.    w
10be9 68 69 6c 65 28 20 6e 2d 2d 20 29 7b 0a 20 20 20  hile( n-- ){.   
10bea 20 20 20 70 20 3d 20 28 50 67 46 72 65 65 73 6c     p = (PgFreesl
10beb 6f 74 2a 29 70 42 75 66 3b 0a 20 20 20 20 20 20  ot*)pBuf;.      
10bec 70 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68  p->pNext = pcach
10bed 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 20 20  e1.pFree;.      
10bee 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20  pcache1.pFree = 
10bef 70 3b 0a 20 20 20 20 20 20 70 42 75 66 20 3d 20  p;.      pBuf = 
10bf0 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29  (void*)&((char*)
10bf1 70 42 75 66 29 5b 73 7a 5d 3b 0a 20 20 20 20 7d  pBuf)[sz];.    }
10bf2 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 45 6e  .    pcache1.pEn
10bf3 64 20 3d 20 70 42 75 66 3b 0a 20 20 7d 0a 7d 0a  d = pBuf;.  }.}.
10bf4 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75  ./*.** Malloc fu
10bf5 6e 63 74 69 6f 6e 20 75 73 65 64 20 77 69 74 68  nction used with
10bf6 69 6e 20 74 68 69 73 20 66 69 6c 65 20 74 6f 20  in this file to 
10bf7 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66  allocate space f
10bf8 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 0a 2a  rom the buffer.*
10bf9 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69  * configured usi
10bfa 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  ng sqlite3_confi
10bfb 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  g(SQLITE_CONFIG_
10bfc 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69 6f  PAGECACHE) optio
10bfd 6e 2e 20 49 66 20 6e 6f 20 0a 2a 2a 20 73 75 63  n. If no .** suc
10bfe 68 20 62 75 66 66 65 72 20 65 78 69 73 74 73 20  h buffer exists 
10bff 6f 72 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73  or there is no s
10c00 70 61 63 65 20 6c 65 66 74 20 69 6e 20 69 74 2c  pace left in it,
10c01 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66   this function f
10c02 61 6c 6c 73 20 0a 2a 2a 20 62 61 63 6b 20 74 6f  alls .** back to
10c03 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29   sqlite3Malloc()
10c04 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
10c05 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 69   *pcache1Alloc(i
10c06 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69  nt nByte){.  voi
10c07 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  d *p;.  assert( 
10c08 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
10c09 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78  ld(pcache1.mutex
10c0a 29 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65  ) );.  if( nByte
10c0b 3c 3d 70 63 61 63 68 65 31 2e 73 7a 53 6c 6f 74  <=pcache1.szSlot
10c0c 20 26 26 20 70 63 61 63 68 65 31 2e 70 46 72 65   && pcache1.pFre
10c0d 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  e ){.    assert(
10c0e 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74 20   pcache1.isInit 
10c0f 29 3b 0a 20 20 20 20 70 20 3d 20 28 50 67 48 64  );.    p = (PgHd
10c10 72 31 20 2a 29 70 63 61 63 68 65 31 2e 70 46 72  r1 *)pcache1.pFr
10c11 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e  ee;.    pcache1.
10c12 70 46 72 65 65 20 3d 20 70 63 61 63 68 65 31 2e  pFree = pcache1.
10c13 70 46 72 65 65 2d 3e 70 4e 65 78 74 3b 0a 20 20  pFree->pNext;.  
10c14 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53    sqlite3StatusS
10c15 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  et(SQLITE_STATUS
10c16 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 2c  _PAGECACHE_SIZE,
10c17 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c   nByte);.    sql
10c18 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51  ite3StatusAdd(SQ
10c19 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
10c1a 43 41 43 48 45 5f 55 53 45 44 2c 20 31 29 3b 0a  CACHE_USED, 1);.
10c1b 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 2f 2a    }else{..    /*
10c1c 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
10c1d 62 75 66 66 65 72 20 75 73 69 6e 67 20 73 71 6c  buffer using sql
10c1e 69 74 65 33 4d 61 6c 6c 6f 63 2e 20 42 65 66 6f  ite3Malloc. Befo
10c1f 72 65 20 64 6f 69 6e 67 20 73 6f 2c 20 65 78 69  re doing so, exi
10c20 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 67 6c 6f  t the.    ** glo
10c21 62 61 6c 20 70 63 61 63 68 65 20 6d 75 74 65 78  bal pcache mutex
10c22 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20   and unlock the 
10c23 70 61 67 65 72 2d 63 61 63 68 65 20 6f 62 6a 65  pager-cache obje
10c24 63 74 20 70 43 61 63 68 65 2e 20 54 68 69 73 20  ct pCache. This 
10c25 69 73 20 0a 20 20 20 20 2a 2a 20 73 6f 20 74 68  is .    ** so th
10c26 61 74 20 69 66 20 74 68 65 20 61 74 74 65 6d 70  at if the attemp
10c27 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  t to allocate a 
10c28 6e 65 77 20 62 75 66 66 65 72 20 63 61 75 73 65  new buffer cause
10c29 73 20 74 68 65 20 74 68 65 20 0a 20 20 20 20 2a  s the the .    *
10c2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 73 6f 66  * configured sof
10c2b 74 2d 68 65 61 70 2d 6c 69 6d 69 74 20 74 6f 20  t-heap-limit to 
10c2c 62 65 20 62 72 65 61 63 68 65 64 2c 20 69 74 20  be breached, it 
10c2d 77 69 6c 6c 20 62 65 20 70 6f 73 73 69 62 6c 65  will be possible
10c2e 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 63 6c 61   to.    ** recla
10c2f 69 6d 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74  im memory from t
10c30 68 69 73 20 70 61 67 65 72 2d 63 61 63 68 65 2e  his pager-cache.
10c31 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 61 63  .    */.    pcac
10c32 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  he1LeaveMutex();
10c33 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
10c34 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20  Malloc(nByte);. 
10c35 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d     pcache1EnterM
10c36 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20  utex();.    if( 
10c37 70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  p ){.      int s
10c38 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  z = sqlite3Mallo
10c39 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20  cSize(p);.      
10c3a 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
10c3b 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50  (SQLITE_STATUS_P
10c3c 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f  AGECACHE_OVERFLO
10c3d 57 2c 20 73 7a 29 3b 0a 20 20 20 20 7d 0a 20 20  W, sz);.    }.  
10c3e 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
10c3f 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61  ./*.** Free an a
10c40 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20  llocated buffer 
10c41 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 70 63  obtained from pc
10c42 61 63 68 65 31 41 6c 6c 6f 63 28 29 2e 0a 2a 2f  ache1Alloc()..*/
10c43 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10c44 63 68 65 31 46 72 65 65 28 76 6f 69 64 20 2a 70  che1Free(void *p
10c45 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
10c46 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10c47 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29  pcache1.mutex) )
10c48 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
10c49 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 3e 3d  eturn;.  if( p>=
10c4a 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 20 26  pcache1.pStart &
10c4b 26 20 70 3c 70 63 61 63 68 65 31 2e 70 45 6e 64  & p<pcache1.pEnd
10c4c 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 6c   ){.    PgFreesl
10c4d 6f 74 20 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 73  ot *pSlot;.    s
10c4e 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
10c4f 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
10c50 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d 31  GECACHE_USED, -1
10c51 29 3b 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20 28  );.    pSlot = (
10c52 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b 0a 20  PgFreeslot*)p;. 
10c53 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20     pSlot->pNext 
10c54 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b  = pcache1.pFree;
10c55 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72  .    pcache1.pFr
10c56 65 65 20 3d 20 70 53 6c 6f 74 3b 0a 20 20 7d 65  ee = pSlot;.  }e
10c57 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 53 69  lse{.    int iSi
10c58 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ze = sqlite3Mall
10c59 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 73  ocSize(p);.    s
10c5a 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
10c5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
10c5c 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57  GECACHE_OVERFLOW
10c5d 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 73  , -iSize);.    s
10c5e 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
10c5f 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c    }.}../*.** All
10c60 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65  ocate a new page
10c61 20 6f 62 6a 65 63 74 20 69 6e 69 74 69 61 6c 6c   object initiall
10c62 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
10c63 68 20 63 61 63 68 65 20 70 43 61 63 68 65 2e 0a  h cache pCache..
10c64 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 31  */.static PgHdr1
10c65 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 61   *pcache1AllocPa
10c66 67 65 28 50 43 61 63 68 65 31 20 2a 70 43 61 63  ge(PCache1 *pCac
10c67 68 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65  he){.  int nByte
10c68 20 3d 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31   = sizeof(PgHdr1
10c69 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 50 61  ) + pCache->szPa
10c6a 67 65 3b 0a 20 20 76 6f 69 64 20 2a 70 50 67 20  ge;.  void *pPg 
10c6b 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 6e  = pcache1Alloc(n
10c6c 42 79 74 65 29 3b 0a 20 20 50 67 48 64 72 31 20  Byte);.  PgHdr1 
10c6d 2a 70 3b 0a 20 20 69 66 28 20 70 50 67 20 29 7b  *p;.  if( pPg ){
10c6e 0a 20 20 20 20 70 20 3d 20 50 41 47 45 5f 54 4f  .    p = PAGE_TO
10c6f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c 20  _PGHDR1(pCache, 
10c70 70 50 67 29 3b 0a 20 20 20 20 69 66 28 20 70 43  pPg);.    if( pC
10c71 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
10c72 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65   ){.      pcache
10c73 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 2b 2b  1.nCurrentPage++
10c74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
10c75 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 0a  .    p = 0;.  }.
10c76 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
10c77 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 67 65  *.** Free a page
10c78 20 6f 62 6a 65 63 74 20 61 6c 6c 6f 63 61 74 65   object allocate
10c79 64 20 62 79 20 70 63 61 63 68 65 31 41 6c 6c 6f  d by pcache1Allo
10c7a 63 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  cPage()..**.** T
10c7b 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c  he pointer is al
10c7c 6c 6f 77 65 64 20 74 6f 20 62 65 20 4e 55 4c 4c  lowed to be NULL
10c7d 2c 20 77 68 69 63 68 20 69 73 20 70 72 75 64 65  , which is prude
10c7e 6e 74 2e 20 20 42 75 74 20 69 74 20 74 75 72 6e  nt.  But it turn
10c7f 73 20 6f 75 74 0a 2a 2a 20 74 68 61 74 20 74 68  s out.** that th
10c80 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d  e current implem
10c81 65 6e 74 61 74 69 6f 6e 20 68 61 70 70 65 6e 73  entation happens
10c82 20 74 6f 20 6e 65 76 65 72 20 63 61 6c 6c 20 74   to never call t
10c83 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77  his routine.** w
10c84 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ith a NULL point
10c85 65 72 2c 20 73 6f 20 77 65 20 6d 61 72 6b 20 74  er, so we mark t
10c86 68 65 20 4e 55 4c 4c 20 74 65 73 74 20 77 69 74  he NULL test wit
10c87 68 20 41 4c 57 41 59 53 28 29 2e 0a 2a 2f 0a 73  h ALWAYS()..*/.s
10c88 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10c89 65 31 46 72 65 65 50 61 67 65 28 50 67 48 64 72  e1FreePage(PgHdr
10c8a 31 20 2a 70 29 7b 0a 20 20 69 66 28 20 41 4c 57  1 *p){.  if( ALW
10c8b 41 59 53 28 70 29 20 29 7b 0a 20 20 20 20 69 66  AYS(p) ){.    if
10c8c 28 20 70 2d 3e 70 43 61 63 68 65 2d 3e 62 50 75  ( p->pCache->bPu
10c8d 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  rgeable ){.     
10c8e 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
10c8f 74 50 61 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20  tPage--;.    }. 
10c90 20 20 20 70 63 61 63 68 65 31 46 72 65 65 28 50     pcache1Free(P
10c91 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 29  GHDR1_TO_PAGE(p)
10c92 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
10c93 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20  Malloc function 
10c94 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 74  used by SQLite t
10c95 6f 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66  o obtain space f
10c96 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 63  rom the buffer c
10c97 6f 6e 66 69 67 75 72 65 64 0a 2a 2a 20 75 73 69  onfigured.** usi
10c98 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  ng sqlite3_confi
10c99 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  g(SQLITE_CONFIG_
10c9a 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69 6f  PAGECACHE) optio
10c9b 6e 2e 20 49 66 20 6e 6f 20 73 75 63 68 20 62 75  n. If no such bu
10c9c 66 66 65 72 0a 2a 2a 20 65 78 69 73 74 73 2c 20  ffer.** exists, 
10c9d 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 61  this function fa
10c9e 6c 6c 73 20 62 61 63 6b 20 74 6f 20 73 71 6c 69  lls back to sqli
10c9f 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a  te3Malloc()..*/.
10ca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10ca1 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65  oid *sqlite3Page
10ca2 4d 61 6c 6c 6f 63 28 69 6e 74 20 73 7a 29 7b 0a  Malloc(int sz){.
10ca3 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 70 63 61    void *p;.  pca
10ca4 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10ca5 3b 0a 20 20 70 20 3d 20 70 63 61 63 68 65 31 41  ;.  p = pcache1A
10ca6 6c 6c 6f 63 28 73 7a 29 3b 0a 20 20 70 63 61 63  lloc(sz);.  pcac
10ca7 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  he1LeaveMutex();
10ca8 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
10ca9 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61 6c  /*.** Free an al
10caa 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 6f  located buffer o
10cab 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
10cac 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 29  ite3PageMalloc()
10cad 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10cae 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
10caf 50 61 67 65 46 72 65 65 28 76 6f 69 64 20 2a 70  PageFree(void *p
10cb0 29 7b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65  ){.  pcache1Ente
10cb1 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63  rMutex();.  pcac
10cb2 68 65 31 46 72 65 65 28 70 29 3b 0a 20 20 70 63  he1Free(p);.  pc
10cb3 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28  ache1LeaveMutex(
10cb4 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
10cb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cb9 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
10cba 20 47 65 6e 65 72 61 6c 20 49 6d 70 6c 65 6d 65   General Impleme
10cbb 6e 74 61 74 69 6f 6e 20 46 75 6e 63 74 69 6f 6e  ntation Function
10cbc 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s **************
10cbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cbe 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ******/../*.** T
10cbf 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
10cc0 75 73 65 64 20 74 6f 20 72 65 73 69 7a 65 20 74  used to resize t
10cc1 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 75 73  he hash table us
10cc2 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20  ed by the cache 
10cc3 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65  passed.** as the
10cc4 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
10cc5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61  .**.** The globa
10cc6 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  l mutex must be 
10cc7 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
10cc8 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10cc9 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
10cca 20 70 63 61 63 68 65 31 52 65 73 69 7a 65 48 61   pcache1ResizeHa
10ccb 73 68 28 50 43 61 63 68 65 31 20 2a 70 29 7b 0a  sh(PCache1 *p){.
10ccc 20 20 50 67 48 64 72 31 20 2a 2a 61 70 4e 65 77    PgHdr1 **apNew
10ccd 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ;.  unsigned int
10cce 20 6e 4e 65 77 3b 0a 20 20 75 6e 73 69 67 6e 65   nNew;.  unsigne
10ccf 64 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65  d int i;..  asse
10cd0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
10cd1 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d  x_held(pcache1.m
10cd2 75 74 65 78 29 20 29 3b 0a 0a 20 20 6e 4e 65 77  utex) );..  nNew
10cd3 20 3d 20 70 2d 3e 6e 48 61 73 68 2a 32 3b 0a 20   = p->nHash*2;. 
10cd4 20 69 66 28 20 6e 4e 65 77 3c 32 35 36 20 29 7b   if( nNew<256 ){
10cd5 0a 20 20 20 20 6e 4e 65 77 20 3d 20 32 35 36 3b  .    nNew = 256;
10cd6 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c  .  }..  pcache1L
10cd7 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69  eaveMutex();.  i
10cd8 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20 73  f( p->nHash ){ s
10cd9 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
10cda 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 61  nMalloc(); }.  a
10cdb 70 4e 65 77 20 3d 20 28 50 67 48 64 72 31 20 2a  pNew = (PgHdr1 *
10cdc 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
10cdd 28 73 69 7a 65 6f 66 28 50 67 48 64 72 31 20 2a  (sizeof(PgHdr1 *
10cde 29 2a 6e 4e 65 77 29 3b 0a 20 20 69 66 28 20 70  )*nNew);.  if( p
10cdf 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71 6c 69 74  ->nHash ){ sqlit
10ce0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
10ce1 63 28 29 3b 20 7d 0a 20 20 70 63 61 63 68 65 31  c(); }.  pcache1
10ce2 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
10ce3 69 66 28 20 61 70 4e 65 77 20 29 7b 0a 20 20 20  if( apNew ){.   
10ce4 20 6d 65 6d 73 65 74 28 61 70 4e 65 77 2c 20 30   memset(apNew, 0
10ce5 2c 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 20  , sizeof(PgHdr1 
10ce6 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 20 20 66 6f  *)*nNew);.    fo
10ce7 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 48 61 73  r(i=0; i<p->nHas
10ce8 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50  h; i++){.      P
10ce9 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 20  gHdr1 *pPage;.  
10cea 20 20 20 20 50 67 48 64 72 31 20 2a 70 4e 65 78      PgHdr1 *pNex
10ceb 74 20 3d 20 70 2d 3e 61 70 48 61 73 68 5b 69 5d  t = p->apHash[i]
10cec 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28  ;.      while( (
10ced 70 50 61 67 65 20 3d 20 70 4e 65 78 74 29 21 3d  pPage = pNext)!=
10cee 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 73  0 ){.        uns
10cef 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 70 50  igned int h = pP
10cf0 61 67 65 2d 3e 69 4b 65 79 20 25 20 6e 4e 65 77  age->iKey % nNew
10cf1 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 20  ;.        pNext 
10cf2 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a  = pPage->pNext;.
10cf3 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70          pPage->p
10cf4 4e 65 78 74 20 3d 20 61 70 4e 65 77 5b 68 5d 3b  Next = apNew[h];
10cf5 0a 20 20 20 20 20 20 20 20 61 70 4e 65 77 5b 68  .        apNew[h
10cf6 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20  ] = pPage;.     
10cf7 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
10cf8 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70 48  ite3_free(p->apH
10cf9 61 73 68 29 3b 0a 20 20 20 20 70 2d 3e 61 70 48  ash);.    p->apH
10cfa 61 73 68 20 3d 20 61 70 4e 65 77 3b 0a 20 20 20  ash = apNew;.   
10cfb 20 70 2d 3e 6e 48 61 73 68 20 3d 20 6e 4e 65 77   p->nHash = nNew
10cfc 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
10cfd 28 70 2d 3e 61 70 48 61 73 68 20 3f 20 53 51 4c  (p->apHash ? SQL
10cfe 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f  ITE_OK : SQLITE_
10cff 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  NOMEM);.}../*.**
10d00 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
10d01 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c  s used internall
10d02 79 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20  y to remove the 
10d03 70 61 67 65 20 70 50 61 67 65 20 66 72 6f 6d 20  page pPage from 
10d04 74 68 65 20 0a 2a 2a 20 67 6c 6f 62 61 6c 20 4c  the .** global L
10d05 52 55 20 6c 69 73 74 2c 20 69 66 20 69 73 20 70  RU list, if is p
10d06 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20 70 50  art of it. If pP
10d07 61 67 65 20 69 73 20 6e 6f 74 20 70 61 72 74 20  age is not part 
10d08 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 0a 2a 2a  of the global.**
10d09 20 4c 52 55 20 6c 69 73 74 2c 20 74 68 65 6e 20   LRU list, then 
10d0a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
10d0b 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
10d0c 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  The global mutex
10d0d 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
10d0e 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
10d0f 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73   is called..*/.s
10d10 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10d11 65 31 50 69 6e 50 61 67 65 28 50 67 48 64 72 31  e1PinPage(PgHdr1
10d12 20 2a 70 50 61 67 65 29 7b 0a 20 20 61 73 73 65   *pPage){.  asse
10d13 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
10d14 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d  x_held(pcache1.m
10d15 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
10d16 50 61 67 65 20 26 26 20 28 70 50 61 67 65 2d 3e  Page && (pPage->
10d17 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 50 61 67  pLruNext || pPag
10d18 65 3d 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 54  e==pcache1.pLruT
10d19 61 69 6c 29 20 29 7b 0a 20 20 20 20 69 66 28 20  ail) ){.    if( 
10d1a 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 20  pPage->pLruPrev 
10d1b 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e  ){.      pPage->
10d1c 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e 65  pLruPrev->pLruNe
10d1d 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75  xt = pPage->pLru
10d1e 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Next;.    }.    
10d1f 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e  if( pPage->pLruN
10d20 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61  ext ){.      pPa
10d21 67 65 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 4c  ge->pLruNext->pL
10d22 72 75 50 72 65 76 20 3d 20 70 50 61 67 65 2d 3e  ruPrev = pPage->
10d23 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a  pLruPrev;.    }.
10d24 20 20 20 20 69 66 28 20 70 63 61 63 68 65 31 2e      if( pcache1.
10d25 70 4c 72 75 48 65 61 64 3d 3d 70 50 61 67 65 20  pLruHead==pPage 
10d26 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  ){.      pcache1
10d27 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 67  .pLruHead = pPag
10d28 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20  e->pLruNext;.   
10d29 20 7d 0a 20 20 20 20 69 66 28 20 70 63 61 63 68   }.    if( pcach
10d2a 65 31 2e 70 4c 72 75 54 61 69 6c 3d 3d 70 50 61  e1.pLruTail==pPa
10d2b 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63  ge ){.      pcac
10d2c 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 3d 20 70  he1.pLruTail = p
10d2d 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a  Page->pLruPrev;.
10d2e 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d      }.    pPage-
10d2f 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20  >pLruNext = 0;. 
10d30 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72     pPage->pLruPr
10d31 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  ev = 0;.    pPag
10d32 65 2d 3e 70 43 61 63 68 65 2d 3e 6e 52 65 63 79  e->pCache->nRecy
10d33 63 6c 61 62 6c 65 2d 2d 3b 0a 20 20 7d 0a 7d 0a  clable--;.  }.}.
10d34 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74  ../*.** Remove t
10d35 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 64  he page supplied
10d36 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
10d37 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61  from the hash ta
10d38 62 6c 65 20 0a 2a 2a 20 28 50 43 61 63 68 65 31  ble .** (PCache1
10d39 2e 61 70 48 61 73 68 20 73 74 72 75 63 74 75 72  .apHash structur
10d3a 65 29 20 74 68 61 74 20 69 74 20 69 73 20 63 75  e) that it is cu
10d3b 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69  rrently stored i
10d3c 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f  n..**.** The glo
10d3d 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62  bal mutex must b
10d3e 65 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73  e held when this
10d3f 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
10d40 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  led..*/.static v
10d41 6f 69 64 20 70 63 61 63 68 65 31 52 65 6d 6f 76  oid pcache1Remov
10d42 65 46 72 6f 6d 48 61 73 68 28 50 67 48 64 72 31  eFromHash(PgHdr1
10d43 20 2a 70 50 61 67 65 29 7b 0a 20 20 75 6e 73 69   *pPage){.  unsi
10d44 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 50 43  gned int h;.  PC
10d45 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20  ache1 *pCache = 
10d46 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 20  pPage->pCache;. 
10d47 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a 0a 20   PgHdr1 **pp;.. 
10d48 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79   h = pPage->iKey
10d49 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68   % pCache->nHash
10d4a 3b 0a 20 20 66 6f 72 28 70 70 3d 26 70 43 61 63  ;.  for(pp=&pCac
10d4b 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 28  he->apHash[h]; (
10d4c 2a 70 70 29 21 3d 70 50 61 67 65 3b 20 70 70 3d  *pp)!=pPage; pp=
10d4d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 3b 0a  &(*pp)->pNext);.
10d4e 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70    *pp = (*pp)->p
10d4f 4e 65 78 74 3b 0a 0a 20 20 70 43 61 63 68 65 2d  Next;..  pCache-
10d50 3e 6e 50 61 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a  >nPage--;.}../*.
10d51 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  ** If there are 
10d52 63 75 72 72 65 6e 74 6c 79 20 6d 6f 72 65 20 74  currently more t
10d53 68 61 6e 20 70 63 61 63 68 65 2e 6e 4d 61 78 50  han pcache.nMaxP
10d54 61 67 65 20 70 61 67 65 73 20 61 6c 6c 6f 63 61  age pages alloca
10d55 74 65 64 2c 20 74 72 79 0a 2a 2a 20 74 6f 20 72  ted, try.** to r
10d56 65 63 79 63 6c 65 20 70 61 67 65 73 20 74 6f 20  ecycle pages to 
10d57 72 65 64 75 63 65 20 74 68 65 20 6e 75 6d 62 65  reduce the numbe
10d58 72 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 70  r allocated to p
10d59 63 61 63 68 65 2e 6e 4d 61 78 50 61 67 65 2e 0a  cache.nMaxPage..
10d5a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
10d5b 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78  cache1EnforceMax
10d5c 50 61 67 65 28 76 6f 69 64 29 7b 0a 20 20 61 73  Page(void){.  as
10d5d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
10d5e 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31  tex_held(pcache1
10d5f 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 77 68 69  .mutex) );.  whi
10d60 6c 65 28 20 70 63 61 63 68 65 31 2e 6e 43 75 72  le( pcache1.nCur
10d61 72 65 6e 74 50 61 67 65 3e 70 63 61 63 68 65 31  rentPage>pcache1
10d62 2e 6e 4d 61 78 50 61 67 65 20 26 26 20 70 63 61  .nMaxPage && pca
10d63 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 29 7b  che1.pLruTail ){
10d64 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 20 3d  .    PgHdr1 *p =
10d65 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69   pcache1.pLruTai
10d66 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 69  l;.    pcache1Pi
10d67 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 20 70 63  nPage(p);.    pc
10d68 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48  ache1RemoveFromH
10d69 61 73 68 28 70 29 3b 0a 20 20 20 20 70 63 61 63  ash(p);.    pcac
10d6a 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b 0a  he1FreePage(p);.
10d6b 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73    }.}../*.** Dis
10d6c 63 61 72 64 20 61 6c 6c 20 70 61 67 65 73 20 66  card all pages f
10d6d 72 6f 6d 20 63 61 63 68 65 20 70 43 61 63 68 65  rom cache pCache
10d6e 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d   with a page num
10d6f 62 65 72 20 28 6b 65 79 20 76 61 6c 75 65 29 20  ber (key value) 
10d70 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e  .** greater than
10d71 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 69 4c 69   or equal to iLi
10d72 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e 65 64 20  mit. Any pinned 
10d73 70 61 67 65 73 20 74 68 61 74 20 6d 65 65 74 20  pages that meet 
10d74 74 68 69 73 20 0a 2a 2a 20 63 72 69 74 65 72 69  this .** criteri
10d75 61 20 61 72 65 20 75 6e 70 69 6e 6e 65 64 20 62  a are unpinned b
10d76 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 64  efore they are d
10d77 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20  iscarded..**.** 
10d78 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  The global mutex
10d79 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
10d7a 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
10d7b 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73   is called..*/.s
10d7c 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10d7d 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65  e1TruncateUnsafe
10d7e 28 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61  (.  PCache1 *pCa
10d7f 63 68 65 2c 20 0a 20 20 75 6e 73 69 67 6e 65 64  che, .  unsigned
10d80 20 69 6e 74 20 69 4c 69 6d 69 74 20 0a 29 7b 0a   int iLimit .){.
10d81 20 20 54 45 53 54 4f 4e 4c 59 28 20 75 6e 73 69    TESTONLY( unsi
10d82 67 6e 65 64 20 69 6e 74 20 6e 50 61 67 65 20 3d  gned int nPage =
10d83 20 30 3b 20 29 20 20 20 20 20 20 2f 2a 20 55 73   0; )      /* Us
10d84 65 64 20 74 6f 20 61 73 73 65 72 74 20 70 43 61  ed to assert pCa
10d85 63 68 65 2d 3e 6e 50 61 67 65 20 69 73 20 63 6f  che->nPage is co
10d86 72 72 65 63 74 20 2a 2f 0a 20 20 75 6e 73 69 67  rrect */.  unsig
10d87 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 61 73 73  ned int h;.  ass
10d88 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
10d89 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e  ex_held(pcache1.
10d8a 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28  mutex) );.  for(
10d8b 68 3d 30 3b 20 68 3c 70 43 61 63 68 65 2d 3e 6e  h=0; h<pCache->n
10d8c 48 61 73 68 3b 20 68 2b 2b 29 7b 0a 20 20 20 20  Hash; h++){.    
10d8d 50 67 48 64 72 31 20 2a 2a 70 70 20 3d 20 26 70  PgHdr1 **pp = &p
10d8e 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d  Cache->apHash[h]
10d8f 3b 20 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70  ; .    PgHdr1 *p
10d90 50 61 67 65 3b 0a 20 20 20 20 77 68 69 6c 65 28  Page;.    while(
10d91 20 28 70 50 61 67 65 20 3d 20 2a 70 70 29 21 3d   (pPage = *pp)!=
10d92 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  0 ){.      if( p
10d93 50 61 67 65 2d 3e 69 4b 65 79 3e 3d 69 4c 69 6d  Page->iKey>=iLim
10d94 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  it ){.        pC
10d95 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 20  ache->nPage--;. 
10d96 20 20 20 20 20 20 20 2a 70 70 20 3d 20 70 50 61         *pp = pPa
10d97 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  ge->pNext;.     
10d98 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67     pcache1PinPag
10d99 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
10d9a 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67    pcache1FreePag
10d9b 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
10d9c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
10d9d 70 20 3d 20 26 70 50 61 67 65 2d 3e 70 4e 65 78  p = &pPage->pNex
10d9e 74 3b 0a 20 20 20 20 20 20 20 20 54 45 53 54 4f  t;.        TESTO
10d9f 4e 4c 59 28 20 6e 50 61 67 65 2b 2b 3b 20 29 0a  NLY( nPage++; ).
10da0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
10da1 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63  }.  assert( pCac
10da2 68 65 2d 3e 6e 50 61 67 65 3d 3d 6e 50 61 67 65  he->nPage==nPage
10da3 20 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a   );.}../********
10da4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10da5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10da6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10da7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10da8 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
10da9 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  * sqlite3_pcache
10daa 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a   Methods *******
10dab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dad 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
10dae 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
10daf 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
10db0 61 63 68 65 2e 78 49 6e 69 74 20 6d 65 74 68 6f  ache.xInit metho
10db1 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
10db2 20 70 63 61 63 68 65 31 49 6e 69 74 28 76 6f 69   pcache1Init(voi
10db3 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
10db4 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
10db5 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65  NotUsed);.  asse
10db6 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e  rt( pcache1.isIn
10db7 69 74 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65  it==0 );.  memse
10db8 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c 20 73  t(&pcache1, 0, s
10db9 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 29 3b  izeof(pcache1));
10dba 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
10dbb 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65  obalConfig.bCore
10dbc 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 70 63 61  Mutex ){.    pca
10dbd 63 68 65 31 2e 6d 75 74 65 78 20 3d 20 73 71 6c  che1.mutex = sql
10dbe 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
10dbf 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
10dc0 41 54 49 43 5f 4c 52 55 29 3b 0a 20 20 7d 0a 20  ATIC_LRU);.  }. 
10dc1 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74 20   pcache1.isInit 
10dc2 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  = 1;.  return SQ
10dc3 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
10dc4 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
10dc5 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
10dc6 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e  pcache.xShutdown
10dc7 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 4e 6f 74 65   method..** Note
10dc8 20 74 68 61 74 20 74 68 65 20 73 74 61 74 69 63   that the static
10dc9 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 65 64   mutex allocated
10dca 20 69 6e 20 78 49 6e 69 74 20 64 6f 65 73 20 0a   in xInit does .
10dcb 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  ** not need to b
10dcc 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 74  e freed..*/.stat
10dcd 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 53  ic void pcache1S
10dce 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f  hutdown(void *No
10dcf 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44  tUsed){.  UNUSED
10dd0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
10dd1 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ed);.  assert( p
10dd2 63 61 63 68 65 31 2e 69 73 49 6e 69 74 21 3d 30  cache1.isInit!=0
10dd3 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 63   );.  memset(&pc
10dd4 61 63 68 65 31 2c 20 30 2c 20 73 69 7a 65 6f 66  ache1, 0, sizeof
10dd5 28 70 63 61 63 68 65 31 29 29 3b 0a 7d 0a 0a 2f  (pcache1));.}../
10dd6 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
10dd7 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
10dd8 65 33 5f 70 63 61 63 68 65 2e 78 43 72 65 61 74  e3_pcache.xCreat
10dd9 65 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20  e method..**.** 
10dda 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 63  Allocate a new c
10ddb 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ache..*/.static 
10ddc 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
10ddd 70 63 61 63 68 65 31 43 72 65 61 74 65 28 69 6e  pcache1Create(in
10dde 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 50  t szPage, int bP
10ddf 75 72 67 65 61 62 6c 65 29 7b 0a 20 20 50 43 61  urgeable){.  PCa
10de0 63 68 65 31 20 2a 70 43 61 63 68 65 3b 0a 0a 20  che1 *pCache;.. 
10de1 20 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68   pCache = (PCach
10de2 65 31 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  e1 *)sqlite3_mal
10de3 6c 6f 63 28 73 69 7a 65 6f 66 28 50 43 61 63 68  loc(sizeof(PCach
10de4 65 31 29 29 3b 0a 20 20 69 66 28 20 70 43 61 63  e1));.  if( pCac
10de5 68 65 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74  he ){.    memset
10de6 28 70 43 61 63 68 65 2c 20 30 2c 20 73 69 7a 65  (pCache, 0, size
10de7 6f 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20  of(PCache1));.  
10de8 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65    pCache->szPage
10de9 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 20 20 70   = szPage;.    p
10dea 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c  Cache->bPurgeabl
10deb 65 20 3d 20 28 62 50 75 72 67 65 61 62 6c 65 20  e = (bPurgeable 
10dec 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 69 66  ? 1 : 0);.    if
10ded 28 20 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a  ( bPurgeable ){.
10dee 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d        pCache->nM
10def 69 6e 20 3d 20 31 30 3b 0a 20 20 20 20 20 20 70  in = 10;.      p
10df0 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
10df1 28 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65  ();.      pcache
10df2 31 2e 6e 4d 69 6e 50 61 67 65 20 2b 3d 20 70 43  1.nMinPage += pC
10df3 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 20 20  ache->nMin;.    
10df4 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
10df5 74 65 78 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  tex();.    }.  }
10df6 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74  .  return (sqlit
10df7 65 33 5f 70 63 61 63 68 65 20 2a 29 70 43 61 63  e3_pcache *)pCac
10df8 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  he;.}../*.** Imp
10df9 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
10dfa 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
10dfb 65 2e 78 43 61 63 68 65 73 69 7a 65 20 6d 65 74  e.xCachesize met
10dfc 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66  hod. .**.** Conf
10dfd 69 67 75 72 65 20 74 68 65 20 63 61 63 68 65 5f  igure the cache_
10dfe 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 61  size limit for a
10dff 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69   cache..*/.stati
10e00 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 43 61  c void pcache1Ca
10e01 63 68 65 73 69 7a 65 28 73 71 6c 69 74 65 33 5f  chesize(sqlite3_
10e02 70 63 61 63 68 65 20 2a 70 2c 20 69 6e 74 20 6e  pcache *p, int n
10e03 4d 61 78 29 7b 0a 20 20 50 43 61 63 68 65 31 20  Max){.  PCache1 
10e04 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68  *pCache = (PCach
10e05 65 31 20 2a 29 70 3b 0a 20 20 69 66 28 20 70 43  e1 *)p;.  if( pC
10e06 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
10e07 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 45   ){.    pcache1E
10e08 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
10e09 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67   pcache1.nMaxPag
10e0a 65 20 2b 3d 20 28 6e 4d 61 78 20 2d 20 70 43 61  e += (nMax - pCa
10e0b 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20 20  che->nMax);.    
10e0c 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20 6e  pCache->nMax = n
10e0d 4d 61 78 3b 0a 20 20 20 20 70 63 61 63 68 65 31  Max;.    pcache1
10e0e 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29  EnforceMaxPage()
10e0f 3b 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 61  ;.    pcache1Lea
10e10 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 7d  veMutex();.  }.}
10e11 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
10e12 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
10e13 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 50 61  lite3_pcache.xPa
10e14 67 65 63 6f 75 6e 74 20 6d 65 74 68 6f 64 2e 20  gecount method. 
10e15 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
10e16 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e 74 28  cache1Pagecount(
10e17 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
10e18 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 70  p){.  int n;.  p
10e19 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
10e1a 28 29 3b 0a 20 20 6e 20 3d 20 28 28 50 43 61 63  ();.  n = ((PCac
10e1b 68 65 31 20 2a 29 70 29 2d 3e 6e 50 61 67 65 3b  he1 *)p)->nPage;
10e1c 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
10e1d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e  utex();.  return
10e1e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70   n;.}../*.** Imp
10e1f 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
10e20 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
10e21 65 2e 78 46 65 74 63 68 20 6d 65 74 68 6f 64 2e  e.xFetch method.
10e22 20 0a 2a 2a 0a 2a 2a 20 46 65 74 63 68 20 61 20   .**.** Fetch a 
10e23 70 61 67 65 20 62 79 20 6b 65 79 20 76 61 6c 75  page by key valu
10e24 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72  e..**.** Whether
10e25 20 6f 72 20 6e 6f 74 20 61 20 6e 65 77 20 70 61   or not a new pa
10e26 67 65 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61  ge may be alloca
10e27 74 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63  ted by this func
10e28 74 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 0a  tion depends on.
10e29 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
10e2a 74 68 65 20 63 72 65 61 74 65 46 6c 61 67 20 61  the createFlag a
10e2b 72 67 75 6d 65 6e 74 2e 20 20 30 20 6d 65 61 6e  rgument.  0 mean
10e2c 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74  s do not allocat
10e2d 65 20 61 20 6e 65 77 0a 2a 2a 20 70 61 67 65 2e  e a new.** page.
10e2e 20 20 31 20 6d 65 61 6e 73 20 61 6c 6c 6f 63 61    1 means alloca
10e2f 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 69 66  te a new page if
10e30 20 73 70 61 63 65 20 69 73 20 65 61 73 69 6c 79   space is easily
10e31 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 32 20 0a   available.  2 .
10e32 2a 2a 20 6d 65 61 6e 73 20 74 6f 20 74 72 79 20  ** means to try 
10e33 72 65 61 6c 6c 79 20 68 61 72 64 20 74 6f 20 61  really hard to a
10e34 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61  llocate a new pa
10e35 67 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20  ge..**.** For a 
10e36 6e 6f 6e 2d 70 75 72 67 65 61 62 6c 65 20 63 61  non-purgeable ca
10e37 63 68 65 20 28 61 20 63 61 63 68 65 20 75 73 65  che (a cache use
10e38 64 20 61 73 20 74 68 65 20 73 74 6f 72 61 67 65  d as the storage
10e39 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72   for an in-memor
10e3a 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 20 74  y.** database) t
10e3b 68 65 72 65 20 69 73 20 72 65 61 6c 6c 79 20 6e  here is really n
10e3c 6f 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  o difference bet
10e3d 77 65 65 6e 20 63 72 65 61 74 65 46 6c 61 67 20  ween createFlag 
10e3e 31 20 61 6e 64 20 32 2e 20 20 53 6f 0a 2a 2a 20  1 and 2.  So.** 
10e3f 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
10e40 74 69 6f 6e 20 28 70 63 61 63 68 65 2e 63 29 20  tion (pcache.c) 
10e41 77 69 6c 6c 20 6e 65 76 65 72 20 68 61 76 65 20  will never have 
10e42 61 20 63 72 65 61 74 65 46 6c 61 67 20 6f 66 20  a createFlag of 
10e43 31 20 6f 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 70 75  1 on.** a non-pu
10e44 72 67 61 62 6c 65 20 63 61 63 68 65 2e 0a 2a 2a  rgable cache..**
10e45 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68  .** There are th
10e46 72 65 65 20 64 69 66 66 65 72 65 6e 74 20 61 70  ree different ap
10e47 70 72 6f 61 63 68 65 73 20 74 6f 20 6f 62 74 61  proaches to obta
10e48 69 6e 69 6e 67 20 73 70 61 63 65 20 66 6f 72 20  ining space for 
10e49 61 20 70 61 67 65 2c 0a 2a 2a 20 64 65 70 65 6e  a page,.** depen
10e4a 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75  ding on the valu
10e4b 65 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20 63  e of parameter c
10e4c 72 65 61 74 65 46 6c 61 67 20 28 77 68 69 63 68  reateFlag (which
10e4d 20 6d 61 79 20 62 65 20 30 2c 20 31 20 6f 72 20   may be 0, 1 or 
10e4e 32 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 52  2)..**.**   1. R
10e4f 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
10e50 20 76 61 6c 75 65 20 6f 66 20 63 72 65 61 74 65   value of create
10e51 46 6c 61 67 2c 20 74 68 65 20 63 61 63 68 65 20  Flag, the cache 
10e52 69 73 20 73 65 61 72 63 68 65 64 20 66 6f 72 20  is searched for 
10e53 61 20 0a 2a 2a 20 20 20 20 20 20 63 6f 70 79 20  a .**      copy 
10e54 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  of the requested
10e55 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 20 69 73   page. If one is
10e56 20 66 6f 75 6e 64 2c 20 69 74 20 69 73 20 72 65   found, it is re
10e57 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  turned..**.**   
10e58 32 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67  2. If createFlag
10e59 3d 3d 30 20 61 6e 64 20 74 68 65 20 70 61 67 65  ==0 and the page
10e5a 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
10e5b 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 4e 55  in the cache, NU
10e5c 4c 4c 20 69 73 0a 2a 2a 20 20 20 20 20 20 72 65  LL is.**      re
10e5d 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  turned..**.**   
10e5e 33 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67  3. If createFlag
10e5f 20 69 73 20 31 2c 20 61 6e 64 20 74 68 65 20 70   is 1, and the p
10e60 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  age is not alrea
10e61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c  dy in the cache,
10e62 0a 2a 2a 20 20 20 20 20 20 61 6e 64 20 69 66 20  .**      and if 
10e63 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f  either of the fo
10e64 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65  llowing are true
10e65 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3a 0a 2a  , return NULL:.*
10e66 2a 0a 2a 2a 20 20 20 20 20 20 20 28 61 29 20 74  *.**       (a) t
10e67 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
10e68 65 73 20 70 69 6e 6e 65 64 20 62 79 20 74 68 65  es pinned by the
10e69 20 63 61 63 68 65 20 69 73 20 67 72 65 61 74 65   cache is greate
10e6a 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20  r than.**       
10e6b 20 20 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 78      PCache1.nMax
10e6c 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 28 62  , or.**       (b
10e6d 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  ) the number of 
10e6e 70 61 67 65 73 20 70 69 6e 6e 65 64 20 62 79 20  pages pinned by 
10e6f 74 68 65 20 63 61 63 68 65 20 69 73 20 67 72 65  the cache is gre
10e70 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20  ater than.**    
10e71 20 20 20 20 20 20 20 74 68 65 20 73 75 6d 20 6f         the sum o
10e72 66 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 20 70  f nMax for all p
10e73 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 2c  urgeable caches,
10e74 20 6c 65 73 73 20 74 68 65 20 73 75 6d 20 6f 66   less the sum of
10e75 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6e   .**           n
10e76 4d 69 6e 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65  Min for all othe
10e77 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68  r purgeable cach
10e78 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 34 2e 20  es. .**.**   4. 
10e79 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66  If none of the f
10e7a 69 72 73 74 20 74 68 72 65 65 20 63 6f 6e 64 69  irst three condi
10e7b 74 69 6f 6e 73 20 61 70 70 6c 79 20 61 6e 64 20  tions apply and 
10e7c 74 68 65 20 63 61 63 68 65 20 69 73 20 6d 61 72  the cache is mar
10e7d 6b 65 64 0a 2a 2a 20 20 20 20 20 20 61 73 20 70  ked.**      as p
10e7e 75 72 67 65 61 62 6c 65 2c 20 61 6e 64 20 69 66  urgeable, and if
10e7f 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
10e80 6f 77 69 6e 67 20 69 73 20 74 72 75 65 3a 0a 2a  owing is true:.*
10e81 2a 0a 2a 2a 20 20 20 20 20 20 20 28 61 29 20 54  *.**       (a) T
10e82 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
10e83 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
10e84 20 74 68 65 20 63 61 63 68 65 20 69 73 20 61 6c   the cache is al
10e85 72 65 61 64 79 20 0a 2a 2a 20 20 20 20 20 20 20  ready .**       
10e86 20 20 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 78      PCache1.nMax
10e87 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  , or.**.**      
10e88 20 28 62 29 20 54 68 65 20 6e 75 6d 62 65 72 20   (b) The number 
10e89 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74  of pages allocat
10e8a 65 64 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 65  ed for all purge
10e8b 61 62 6c 65 20 63 61 63 68 65 73 20 69 73 0a 2a  able caches is.*
10e8c 2a 20 20 20 20 20 20 20 20 20 20 20 61 6c 72 65  *           alre
10e8d 61 64 79 20 65 71 75 61 6c 20 74 6f 20 6f 72 20  ady equal to or 
10e8e 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65  greater than the
10e8f 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72   sum of nMax for
10e90 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20   all.**         
10e91 20 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68    purgeable cach
10e92 65 73 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 74  es,.**.**      t
10e93 68 65 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72  hen attempt to r
10e94 65 63 79 63 6c 65 20 61 20 70 61 67 65 20 66 72  ecycle a page fr
10e95 6f 6d 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e  om the LRU list.
10e96 20 49 66 20 69 74 20 69 73 20 74 68 65 20 72 69   If it is the ri
10e97 67 68 74 0a 2a 2a 20 20 20 20 20 20 73 69 7a 65  ght.**      size
10e98 2c 20 72 65 74 75 72 6e 20 74 68 65 20 72 65 63  , return the rec
10e99 79 63 6c 65 64 20 62 75 66 66 65 72 2e 20 4f 74  ycled buffer. Ot
10e9a 68 65 72 77 69 73 65 2c 20 66 72 65 65 20 74 68  herwise, free th
10e9b 65 20 62 75 66 66 65 72 20 61 6e 64 0a 2a 2a 20  e buffer and.** 
10e9c 20 20 20 20 20 70 72 6f 63 65 65 64 20 74 6f 20       proceed to 
10e9d 73 74 65 70 20 35 2e 20 0a 2a 2a 0a 2a 2a 20 20  step 5. .**.**  
10e9e 20 35 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61   5. Otherwise, a
10e9f 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75  llocate and retu
10ea0 72 6e 20 61 20 6e 65 77 20 70 61 67 65 20 62 75  rn a new page bu
10ea1 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ffer..*/.static 
10ea2 76 6f 69 64 20 2a 70 63 61 63 68 65 31 46 65 74  void *pcache1Fet
10ea3 63 68 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  ch(sqlite3_pcach
10ea4 65 20 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 69  e *p, unsigned i
10ea5 6e 74 20 69 4b 65 79 2c 20 69 6e 74 20 63 72 65  nt iKey, int cre
10ea6 61 74 65 46 6c 61 67 29 7b 0a 20 20 75 6e 73 69  ateFlag){.  unsi
10ea7 67 6e 65 64 20 69 6e 74 20 6e 50 69 6e 6e 65 64  gned int nPinned
10ea8 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61  ;.  PCache1 *pCa
10ea9 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a  che = (PCache1 *
10eaa 29 70 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 50  )p;.  PgHdr1 *pP
10eab 61 67 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65  age = 0;..  asse
10eac 72 74 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72  rt( pCache->bPur
10ead 67 65 61 62 6c 65 20 7c 7c 20 63 72 65 61 74 65  geable || create
10eae 46 6c 61 67 21 3d 31 20 29 3b 0a 20 20 70 63 61  Flag!=1 );.  pca
10eaf 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10eb0 3b 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c  ;.  if( createFl
10eb1 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 65 33 42  ag==1 ) sqlite3B
10eb2 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
10eb3 28 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68  ();..  /* Search
10eb4 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
10eb5 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20  for an existing 
10eb6 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20  entry. */.  if( 
10eb7 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3e 30 20  pCache->nHash>0 
10eb8 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  ){.    unsigned 
10eb9 69 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 20 70  int h = iKey % p
10eba 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20  Cache->nHash;.  
10ebb 20 20 66 6f 72 28 70 50 61 67 65 3d 70 43 61 63    for(pPage=pCac
10ebc 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 70  he->apHash[h]; p
10ebd 50 61 67 65 26 26 70 50 61 67 65 2d 3e 69 4b 65  Page&&pPage->iKe
10ebe 79 21 3d 69 4b 65 79 3b 20 70 50 61 67 65 3d 70  y!=iKey; pPage=p
10ebf 50 61 67 65 2d 3e 70 4e 65 78 74 29 3b 0a 20 20  Page->pNext);.  
10ec0 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 7c  }..  if( pPage |
10ec1 7c 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 20  | createFlag==0 
10ec2 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 50 69  ){.    pcache1Pi
10ec3 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  nPage(pPage);.  
10ec4 20 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74    goto fetch_out
10ec5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  ;.  }..  /* Step
10ec6 20 33 20 6f 66 20 68 65 61 64 65 72 20 63 6f 6d   3 of header com
10ec7 6d 65 6e 74 2e 20 2a 2f 0a 20 20 6e 50 69 6e 6e  ment. */.  nPinn
10ec8 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 6e 50 61  ed = pCache->nPa
10ec9 67 65 20 2d 20 70 43 61 63 68 65 2d 3e 6e 52 65  ge - pCache->nRe
10eca 63 79 63 6c 61 62 6c 65 3b 0a 20 20 69 66 28 20  cyclable;.  if( 
10ecb 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 26 26  createFlag==1 &&
10ecc 20 28 0a 20 20 20 20 20 20 20 20 6e 50 69 6e 6e   (.        nPinn
10ecd 65 64 3e 3d 28 70 63 61 63 68 65 31 2e 6e 4d 61  ed>=(pcache1.nMa
10ece 78 50 61 67 65 2b 70 43 61 63 68 65 2d 3e 6e 4d  xPage+pCache->nM
10ecf 69 6e 2d 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50  in-pcache1.nMinP
10ed0 61 67 65 29 0a 20 20 20 20 20 7c 7c 20 6e 50 69  age).     || nPi
10ed1 6e 6e 65 64 3e 3d 28 70 43 61 63 68 65 2d 3e 6e  nned>=(pCache->n
10ed2 4d 61 78 20 2a 20 39 20 2f 20 31 30 29 0a 20 20  Max * 9 / 10).  
10ed3 29 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74  )){.    goto fet
10ed4 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 69  ch_out;.  }..  i
10ed5 66 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65  f( pCache->nPage
10ed6 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 20  >=pCache->nHash 
10ed7 26 26 20 70 63 61 63 68 65 31 52 65 73 69 7a 65  && pcache1Resize
10ed8 48 61 73 68 28 70 43 61 63 68 65 29 20 29 7b 0a  Hash(pCache) ){.
10ed9 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f      goto fetch_o
10eda 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  ut;.  }..  /* St
10edb 65 70 20 34 2e 20 54 72 79 20 74 6f 20 72 65 63  ep 4. Try to rec
10edc 79 63 6c 65 20 61 20 70 61 67 65 20 62 75 66 66  ycle a page buff
10edd 65 72 20 69 66 20 61 70 70 72 6f 70 72 69 61 74  er if appropriat
10ede 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63  e. */.  if( pCac
10edf 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26  he->bPurgeable &
10ee0 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  & pcache1.pLruTa
10ee1 69 6c 20 26 26 20 28 0a 20 20 20 20 20 28 70 43  il && (.     (pC
10ee2 61 63 68 65 2d 3e 6e 50 61 67 65 2b 31 3e 3d 70  ache->nPage+1>=p
10ee3 43 61 63 68 65 2d 3e 6e 4d 61 78 29 20 7c 7c 20  Cache->nMax) || 
10ee4 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74  pcache1.nCurrent
10ee5 50 61 67 65 3e 3d 70 63 61 63 68 65 31 2e 6e 4d  Page>=pcache1.nM
10ee6 61 78 50 61 67 65 0a 20 20 29 29 7b 0a 20 20 20  axPage.  )){.   
10ee7 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31   pPage = pcache1
10ee8 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70  .pLruTail;.    p
10ee9 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d  cache1RemoveFrom
10eea 48 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20  Hash(pPage);.   
10eeb 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28   pcache1PinPage(
10eec 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  pPage);.    if( 
10eed 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e 73  pPage->pCache->s
10eee 7a 50 61 67 65 21 3d 70 43 61 63 68 65 2d 3e 73  zPage!=pCache->s
10eef 7a 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70  zPage ){.      p
10ef0 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70  cache1FreePage(p
10ef1 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 50 61  Page);.      pPa
10ef2 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  ge = 0;.    }els
10ef3 65 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  e{.      pcache1
10ef4 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 20 2d 3d  .nCurrentPage -=
10ef5 20 28 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d   (pPage->pCache-
10ef6 3e 62 50 75 72 67 65 61 62 6c 65 20 2d 20 70 43  >bPurgeable - pC
10ef7 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
10ef8 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
10ef9 2f 2a 20 53 74 65 70 20 35 2e 20 49 66 20 61 20  /* Step 5. If a 
10efa 75 73 61 62 6c 65 20 70 61 67 65 20 62 75 66 66  usable page buff
10efb 65 72 20 68 61 73 20 73 74 69 6c 6c 20 6e 6f 74  er has still not
10efc 20 62 65 65 6e 20 66 6f 75 6e 64 2c 20 0a 20 20   been found, .  
10efd 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c  ** attempt to al
10efe 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 6f 6e 65  locate a new one
10eff 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70  . .  */.  if( !p
10f00 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67  Page ){.    pPag
10f01 65 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63  e = pcache1Alloc
10f02 50 61 67 65 28 70 43 61 63 68 65 29 3b 0a 20 20  Page(pCache);.  
10f03 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 29  }..  if( pPage )
10f04 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69  {.    unsigned i
10f05 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 20 70 43  nt h = iKey % pC
10f06 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 20  ache->nHash;.   
10f07 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b 2b   pCache->nPage++
10f08 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 4b 65  ;.    pPage->iKe
10f09 79 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 70 50  y = iKey;.    pP
10f0a 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43 61  age->pNext = pCa
10f0b 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 0a  che->apHash[h];.
10f0c 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68      pPage->pCach
10f0d 65 20 3d 20 70 43 61 63 68 65 3b 0a 20 20 20 20  e = pCache;.    
10f0e 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 20  pPage->pLruPrev 
10f0f 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  = 0;.    pPage->
10f10 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20 20  pLruNext = 0;.  
10f11 20 20 2a 28 76 6f 69 64 20 2a 2a 29 28 50 47 48    *(void **)(PGH
10f12 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 67  DR1_TO_PAGE(pPag
10f13 65 29 29 20 3d 20 30 3b 0a 20 20 20 20 70 43 61  e)) = 0;.    pCa
10f14 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d  che->apHash[h] =
10f15 20 70 50 61 67 65 3b 0a 20 20 7d 0a 0a 66 65 74   pPage;.  }..fet
10f16 63 68 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 50  ch_out:.  if( pP
10f17 61 67 65 20 26 26 20 69 4b 65 79 3e 70 43 61 63  age && iKey>pCac
10f18 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20  he->iMaxKey ){. 
10f19 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b     pCache->iMaxK
10f1a 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 7d 0a 20  ey = iKey;.  }. 
10f1b 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d   if( createFlag=
10f1c 3d 31 20 29 20 73 71 6c 69 74 65 33 45 6e 64 42  =1 ) sqlite3EndB
10f1d 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
10f1e 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10f1f 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  ex();.  return (
10f20 70 50 61 67 65 20 3f 20 50 47 48 44 52 31 5f 54  pPage ? PGHDR1_T
10f21 4f 5f 50 41 47 45 28 70 50 61 67 65 29 20 3a 20  O_PAGE(pPage) : 
10f22 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d  0);.}.../*.** Im
10f23 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
10f24 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  the sqlite3_pcac
10f25 68 65 2e 78 55 6e 70 69 6e 20 6d 65 74 68 6f 64  he.xUnpin method
10f26 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 70  ..**.** Mark a p
10f27 61 67 65 20 61 73 20 75 6e 70 69 6e 6e 65 64 20  age as unpinned 
10f28 28 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 61 73  (eligible for as
10f29 79 6e 63 68 72 6f 6e 6f 75 73 20 72 65 63 79 63  ynchronous recyc
10f2a 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ling)..*/.static
10f2b 20 76 6f 69 64 20 70 63 61 63 68 65 31 55 6e 70   void pcache1Unp
10f2c 69 6e 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  in(sqlite3_pcach
10f2d 65 20 2a 70 2c 20 76 6f 69 64 20 2a 70 50 67 2c  e *p, void *pPg,
10f2e 20 69 6e 74 20 72 65 75 73 65 55 6e 6c 69 6b 65   int reuseUnlike
10f2f 6c 79 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a  ly){.  PCache1 *
10f30 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65  pCache = (PCache
10f31 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20  1 *)p;.  PgHdr1 
10f32 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 4f  *pPage = PAGE_TO
10f33 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c 20  _PGHDR1(pCache, 
10f34 70 50 67 29 3b 0a 20 0a 20 20 61 73 73 65 72 74  pPg);. .  assert
10f35 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d  ( pPage->pCache=
10f36 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 70 63 61  =pCache );.  pca
10f37 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10f38 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 61 6e  ;..  /* It is an
10f39 20 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74   error to call t
10f3a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20  his function if 
10f3b 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65  the page is alre
10f3c 61 64 79 20 0a 20 20 2a 2a 20 70 61 72 74 20 6f  ady .  ** part o
10f3d 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55  f the global LRU
10f3e 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 61 73   list..  */.  as
10f3f 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 4c 72  sert( pPage->pLr
10f40 75 50 72 65 76 3d 3d 30 20 26 26 20 70 50 61 67  uPrev==0 && pPag
10f41 65 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d 30 20 29  e->pLruNext==0 )
10f42 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63  ;.  assert( pcac
10f43 68 65 31 2e 70 4c 72 75 48 65 61 64 21 3d 70 50  he1.pLruHead!=pP
10f44 61 67 65 20 26 26 20 70 63 61 63 68 65 31 2e 70  age && pcache1.p
10f45 4c 72 75 54 61 69 6c 21 3d 70 50 61 67 65 20 29  LruTail!=pPage )
10f46 3b 0a 0a 20 20 69 66 28 20 72 65 75 73 65 55 6e  ;..  if( reuseUn
10f47 6c 69 6b 65 6c 79 20 7c 7c 20 70 63 61 63 68 65  likely || pcache
10f48 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 70  1.nCurrentPage>p
10f49 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20  cache1.nMaxPage 
10f4a 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65  ){.    pcache1Re
10f4b 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 61  moveFromHash(pPa
10f4c 67 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31  ge);.    pcache1
10f4d 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b  FreePage(pPage);
10f4e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
10f4f 20 41 64 64 20 74 68 65 20 70 61 67 65 20 74 6f   Add the page to
10f50 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20   the global LRU 
10f51 6c 69 73 74 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20  list. Normally, 
10f52 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65  the page is adde
10f53 64 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  d to.    ** the 
10f54 68 65 61 64 20 6f 66 20 74 68 65 20 6c 69 73 74  head of the list
10f55 20 28 6c 61 73 74 20 70 61 67 65 20 74 6f 20 62   (last page to b
10f56 65 20 72 65 63 79 63 6c 65 64 29 2e 20 48 6f 77  e recycled). How
10f57 65 76 65 72 2c 20 69 66 20 74 68 65 20 0a 20 20  ever, if the .  
10f58 20 20 2a 2a 20 72 65 75 73 65 55 6e 6c 69 6b 65    ** reuseUnlike
10f59 6c 79 20 66 6c 61 67 20 70 61 73 73 65 64 20 74  ly flag passed t
10f5a 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  o this function 
10f5b 69 73 20 74 72 75 65 2c 20 74 68 65 20 70 61 67  is true, the pag
10f5c 65 20 69 73 20 61 64 64 65 64 0a 20 20 20 20 2a  e is added.    *
10f5d 2a 20 74 6f 20 74 68 65 20 74 61 69 6c 20 6f 66  * to the tail of
10f5e 20 74 68 65 20 6c 69 73 74 20 28 66 69 72 73 74   the list (first
10f5f 20 70 61 67 65 20 74 6f 20 62 65 20 72 65 63 79   page to be recy
10f60 63 6c 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  cled)..    */.  
10f61 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 4c    if( pcache1.pL
10f62 72 75 48 65 61 64 20 29 7b 0a 20 20 20 20 20 20  ruHead ){.      
10f63 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64  pcache1.pLruHead
10f64 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 50 61  ->pLruPrev = pPa
10f65 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d  ge;.      pPage-
10f66 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 63 61 63  >pLruNext = pcac
10f67 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 0a 20 20  he1.pLruHead;.  
10f68 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75      pcache1.pLru
10f69 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 20  Head = pPage;.  
10f6a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
10f6b 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20  cache1.pLruTail 
10f6c 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70  = pPage;.      p
10f6d 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20  cache1.pLruHead 
10f6e 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20  = pPage;.    }. 
10f6f 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 63 79     pCache->nRecy
10f70 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a 20  clable++;.  }.. 
10f71 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10f72 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ex();.}../*.** I
10f73 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
10f74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
10f75 63 68 65 2e 78 52 65 6b 65 79 20 6d 65 74 68 6f  che.xRekey metho
10f76 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  d. .*/.static vo
10f77 69 64 20 70 63 61 63 68 65 31 52 65 6b 65 79 28  id pcache1Rekey(
10f78 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  .  sqlite3_pcach
10f79 65 20 2a 70 2c 0a 20 20 76 6f 69 64 20 2a 70 50  e *p,.  void *pP
10f7a 67 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  g,.  unsigned in
10f7b 74 20 69 4f 6c 64 2c 0a 20 20 75 6e 73 69 67 6e  t iOld,.  unsign
10f7c 65 64 20 69 6e 74 20 69 4e 65 77 0a 29 7b 0a 20  ed int iNew.){. 
10f7d 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65   PCache1 *pCache
10f7e 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b   = (PCache1 *)p;
10f7f 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65  .  PgHdr1 *pPage
10f80 20 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52   = PAGE_TO_PGHDR
10f81 31 28 70 43 61 63 68 65 2c 20 70 50 67 29 3b 0a  1(pCache, pPg);.
10f82 20 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a 20    PgHdr1 **pp;. 
10f83 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b   unsigned int h;
10f84 20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67   .  assert( pPag
10f85 65 2d 3e 69 4b 65 79 3d 3d 69 4f 6c 64 20 29 3b  e->iKey==iOld );
10f86 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
10f87 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65  ->pCache==pCache
10f88 20 29 3b 0a 0a 20 20 70 63 61 63 68 65 31 45 6e   );..  pcache1En
10f89 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 68  terMutex();..  h
10f8a 20 3d 20 69 4f 6c 64 25 70 43 61 63 68 65 2d 3e   = iOld%pCache->
10f8b 6e 48 61 73 68 3b 0a 20 20 70 70 20 3d 20 26 70  nHash;.  pp = &p
10f8c 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d  Cache->apHash[h]
10f8d 3b 0a 20 20 77 68 69 6c 65 28 20 28 2a 70 70 29  ;.  while( (*pp)
10f8e 21 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70  !=pPage ){.    p
10f8f 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78  p = &(*pp)->pNex
10f90 74 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20 70  t;.  }.  *pp = p
10f91 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20  Page->pNext;..  
10f92 68 20 3d 20 69 4e 65 77 25 70 43 61 63 68 65 2d  h = iNew%pCache-
10f93 3e 6e 48 61 73 68 3b 0a 20 20 70 50 61 67 65 2d  >nHash;.  pPage-
10f94 3e 69 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 20  >iKey = iNew;.  
10f95 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 70  pPage->pNext = p
10f96 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d  Cache->apHash[h]
10f97 3b 0a 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61  ;.  pCache->apHa
10f98 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 0a  sh[h] = pPage;..
10f99 20 20 2f 2a 20 54 68 65 20 78 52 65 6b 65 79 28    /* The xRekey(
10f9a 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f  ) interface is o
10f9b 6e 6c 79 20 75 73 65 64 20 74 6f 20 6d 6f 76 65  nly used to move
10f9c 20 70 61 67 65 73 20 65 61 72 6c 69 65 72 20 69   pages earlier i
10f9d 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  n the.  ** datab
10f9e 61 73 65 20 66 69 6c 65 20 28 69 6e 20 6f 72 64  ase file (in ord
10f9f 65 72 20 74 6f 20 6d 6f 76 65 20 61 6c 6c 20 66  er to move all f
10fa0 72 65 65 20 70 61 67 65 73 20 74 6f 20 74 68 65  ree pages to the
10fa1 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a   end of the.  **
10fa2 20 66 69 6c 65 20 77 68 65 72 65 20 74 68 65 79   file where they
10fa3 20 63 61 6e 20 62 65 20 74 72 75 6e 63 61 74 65   can be truncate
10fa4 64 20 6f 66 66 2e 29 20 20 48 65 6e 63 65 2c 20  d off.)  Hence, 
10fa5 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  it is not possib
10fa6 6c 65 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20  le.  ** for the 
10fa7 6e 65 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20  new page number 
10fa8 74 6f 20 62 65 20 67 72 65 61 74 65 72 20 74 68  to be greater th
10fa9 61 6e 20 74 68 65 20 6c 61 72 67 65 73 74 20 70  an the largest p
10faa 72 65 76 69 6f 75 73 6c 79 0a 20 20 2a 2a 20 66  reviously.  ** f
10fab 65 74 63 68 65 64 20 70 61 67 65 2e 20 20 42 75  etched page.  Bu
10fac 74 20 77 65 20 72 65 74 61 69 6e 20 74 68 65 20  t we retain the 
10fad 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73 74 20 69  following test i
10fae 6e 20 63 61 73 65 20 78 52 65 6b 65 79 28 29 0a  n case xRekey().
10faf 20 20 2a 2a 20 62 65 67 69 6e 73 20 74 6f 20 62    ** begins to b
10fb0 65 20 75 73 65 64 20 69 6e 20 64 69 66 66 65 72  e used in differ
10fb1 65 6e 74 20 77 61 79 73 20 69 6e 20 74 68 65 20  ent ways in the 
10fb2 66 75 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 69  future..  */.  i
10fb3 66 28 20 4e 45 56 45 52 28 69 4e 65 77 3e 70 43  f( NEVER(iNew>pC
10fb4 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 29 20 29  ache->iMaxKey) )
10fb5 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 69 4d  {.    pCache->iM
10fb6 61 78 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 20  axKey = iNew;.  
10fb7 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  }..  pcache1Leav
10fb8 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a  eMutex();.}../*.
10fb9 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
10fba 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
10fbb 5f 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74  _pcache.xTruncat
10fbc 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a  e method. .**.**
10fbd 20 44 69 73 63 61 72 64 20 61 6c 6c 20 75 6e 70   Discard all unp
10fbe 69 6e 6e 65 64 20 70 61 67 65 73 20 69 6e 20 74  inned pages in t
10fbf 68 65 20 63 61 63 68 65 20 77 69 74 68 20 61 20  he cache with a 
10fc0 70 61 67 65 20 6e 75 6d 62 65 72 20 65 71 75 61  page number equa
10fc1 6c 20 74 6f 0a 2a 2a 20 6f 72 20 67 72 65 61 74  l to.** or great
10fc2 65 72 20 74 68 61 6e 20 70 61 72 61 6d 65 74 65  er than paramete
10fc3 72 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69  r iLimit. Any pi
10fc4 6e 6e 65 64 20 70 61 67 65 73 20 77 69 74 68 20  nned pages with 
10fc5 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a  a page number.**
10fc6 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 65   equal to or gre
10fc7 61 74 65 72 20 74 68 61 6e 20 69 4c 69 6d 69 74  ater than iLimit
10fc8 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20   are implicitly 
10fc9 75 6e 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73 74 61  unpinned..*/.sta
10fca 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31  tic void pcache1
10fcb 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33  Truncate(sqlite3
10fcc 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69  _pcache *p, unsi
10fcd 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 74 29  gned int iLimit)
10fce 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61  {.  PCache1 *pCa
10fcf 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a  che = (PCache1 *
10fd0 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74  )p;.  pcache1Ent
10fd1 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28  erMutex();.  if(
10fd2 20 69 4c 69 6d 69 74 3c 3d 70 43 61 63 68 65 2d   iLimit<=pCache-
10fd3 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20  >iMaxKey ){.    
10fd4 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 55  pcache1TruncateU
10fd5 6e 73 61 66 65 28 70 43 61 63 68 65 2c 20 69 4c  nsafe(pCache, iL
10fd6 69 6d 69 74 29 3b 0a 20 20 20 20 70 43 61 63 68  imit);.    pCach
10fd7 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4c 69  e->iMaxKey = iLi
10fd8 6d 69 74 2d 31 3b 0a 20 20 7d 0a 20 20 70 63 61  mit-1;.  }.  pca
10fd9 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10fda 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
10fdb 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
10fdc 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e   sqlite3_pcache.
10fdd 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 2e  xDestroy method.
10fde 20 0a 2a 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20   .**.** Destroy 
10fdf 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65  a cache allocate
10fe0 64 20 75 73 69 6e 67 20 70 63 61 63 68 65 31 43  d using pcache1C
10fe1 72 65 61 74 65 28 29 2e 0a 2a 2f 0a 73 74 61 74  reate()..*/.stat
10fe2 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 44  ic void pcache1D
10fe3 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 70  estroy(sqlite3_p
10fe4 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 50 43 61  cache *p){.  PCa
10fe5 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28  che1 *pCache = (
10fe6 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70  PCache1 *)p;.  p
10fe7 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
10fe8 28 29 3b 0a 20 20 70 63 61 63 68 65 31 54 72 75  ();.  pcache1Tru
10fe9 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 63  ncateUnsafe(pCac
10fea 68 65 2c 20 30 29 3b 0a 20 20 70 63 61 63 68 65  he, 0);.  pcache
10feb 31 2e 6e 4d 61 78 50 61 67 65 20 2d 3d 20 70 43  1.nMaxPage -= pC
10fec 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20 70 63  ache->nMax;.  pc
10fed 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 2d  ache1.nMinPage -
10fee 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a  = pCache->nMin;.
10fef 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65    pcache1Enforce
10ff0 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 70 63 61  MaxPage();.  pca
10ff1 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10ff2 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
10ff3 28 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 29  (pCache->apHash)
10ff4 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
10ff5 28 70 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a  (pCache);.}../*.
10ff6 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
10ff7 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69 6e   is called durin
10ff8 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  g initialization
10ff9 20 28 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61   (sqlite3_initia
10ffa 6c 69 7a 65 28 29 29 20 74 6f 0a 2a 2a 20 69 6e  lize()) to.** in
10ffb 73 74 61 6c 6c 20 74 68 65 20 64 65 66 61 75 6c  stall the defaul
10ffc 74 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68  t pluggable cach
10ffd 65 20 6d 6f 64 75 6c 65 2c 20 61 73 73 75 6d 69  e module, assumi
10ffe 6e 67 20 74 68 65 20 75 73 65 72 20 68 61 73 20  ng the user has 
10fff 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 70  not.** already p
11000 72 6f 76 69 64 65 64 20 61 6e 20 61 6c 74 65 72  rovided an alter
11001 6e 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54  native..*/.SQLIT
11002 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
11003 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 44  qlite3PCacheSetD
11004 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20  efault(void){.  
11005 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70  static sqlite3_p
11006 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 64 65  cache_methods de
11007 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b  faultMethods = {
11008 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
11009 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1100a 20 70 41 72 67 20 2a 2f 0a 20 20 20 20 70 63 61   pArg */.    pca
1100b 63 68 65 31 49 6e 69 74 2c 20 20 20 20 20 20 20  che1Init,       
1100c 20 20 20 20 20 20 2f 2a 20 78 49 6e 69 74 20 2a        /* xInit *
1100d 2f 0a 20 20 20 20 70 63 61 63 68 65 31 53 68 75  /.    pcache1Shu
1100e 74 64 6f 77 6e 2c 20 20 20 20 20 20 20 20 20 2f  tdown,         /
1100f 2a 20 78 53 68 75 74 64 6f 77 6e 20 2a 2f 0a 20  * xShutdown */. 
11010 20 20 20 70 63 61 63 68 65 31 43 72 65 61 74 65     pcache1Create
11011 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  ,           /* x
11012 43 72 65 61 74 65 20 2a 2f 0a 20 20 20 20 70 63  Create */.    pc
11013 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 2c 20  ache1Cachesize, 
11014 20 20 20 20 20 20 20 2f 2a 20 78 43 61 63 68 65         /* xCache
11015 73 69 7a 65 20 2a 2f 0a 20 20 20 20 70 63 61 63  size */.    pcac
11016 68 65 31 50 61 67 65 63 6f 75 6e 74 2c 20 20 20  he1Pagecount,   
11017 20 20 20 20 20 2f 2a 20 78 50 61 67 65 63 6f 75       /* xPagecou
11018 6e 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  nt */.    pcache
11019 31 46 65 74 63 68 2c 20 20 20 20 20 20 20 20 20  1Fetch,         
1101a 20 20 20 2f 2a 20 78 46 65 74 63 68 20 2a 2f 0a     /* xFetch */.
1101b 20 20 20 20 70 63 61 63 68 65 31 55 6e 70 69 6e      pcache1Unpin
1101c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
1101d 78 55 6e 70 69 6e 20 2a 2f 0a 20 20 20 20 70 63  xUnpin */.    pc
1101e 61 63 68 65 31 52 65 6b 65 79 2c 20 20 20 20 20  ache1Rekey,     
1101f 20 20 20 20 20 20 20 2f 2a 20 78 52 65 6b 65 79         /* xRekey
11020 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 54   */.    pcache1T
11021 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20  runcate,        
11022 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f   /* xTruncate */
11023 0a 20 20 20 20 70 63 61 63 68 65 31 44 65 73 74  .    pcache1Dest
11024 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a  roy           /*
11025 20 78 44 65 73 74 72 6f 79 20 2a 2f 0a 20 20 7d   xDestroy */.  }
11026 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ;.  sqlite3_conf
11027 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
11028 5f 50 43 41 43 48 45 2c 20 26 64 65 66 61 75 6c  _PCACHE, &defaul
11029 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 69  tMethods);.}..#i
1102a 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
1102b 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
1102c 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  MENT./*.** This 
1102d 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1102e 65 64 20 74 6f 20 66 72 65 65 20 73 75 70 65 72  ed to free super
1102f 66 6c 75 6f 75 73 20 64 79 6e 61 6d 69 63 61 6c  fluous dynamical
11030 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d  ly allocated mem
11031 6f 72 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 74  ory.** held by t
11032 68 65 20 70 61 67 65 72 20 73 79 73 74 65 6d 2e  he pager system.
11033 20 4d 65 6d 6f 72 79 20 69 6e 20 75 73 65 20 62   Memory in use b
11034 79 20 61 6e 79 20 53 51 4c 69 74 65 20 70 61 67  y any SQLite pag
11035 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20  er allocated.** 
11036 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  by the current t
11037 68 72 65 61 64 20 6d 61 79 20 62 65 20 73 71 6c  hread may be sql
11038 69 74 65 33 5f 66 72 65 65 28 29 65 64 2e 0a 2a  ite3_free()ed..*
11039 2a 0a 2a 2a 20 6e 52 65 71 20 69 73 20 74 68 65  *.** nReq is the
1103a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1103b 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 71 75 69   of memory requi
1103c 72 65 64 2e 20 4f 6e 63 65 20 74 68 69 73 20 6d  red. Once this m
1103d 75 63 68 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20  uch has.** been 
1103e 72 65 6c 65 61 73 65 64 2c 20 74 68 65 20 66 75  released, the fu
1103f 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20  nction returns. 
11040 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
11041 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75   is the total nu
11042 6d 62 65 72 20 0a 2a 2a 20 6f 66 20 62 79 74 65  mber .** of byte
11043 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 6c 65  s of memory rele
11044 61 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ased..*/.SQLITE_
11045 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
11046 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65  te3PcacheRelease
11047 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 52 65 71 29  Memory(int nReq)
11048 7b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d 20  {.  int nFree = 
11049 30 3b 0a 20 20 69 66 28 20 70 63 61 63 68 65 31  0;.  if( pcache1
1104a 2e 70 53 74 61 72 74 3d 3d 30 20 29 7b 0a 20 20  .pStart==0 ){.  
1104b 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 20    PgHdr1 *p;.   
1104c 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
1104d 65 78 28 29 3b 0a 20 20 20 20 77 68 69 6c 65 28  ex();.    while(
1104e 20 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 46 72 65   (nReq<0 || nFre
1104f 65 3c 6e 52 65 71 29 20 26 26 20 28 70 3d 70 63  e<nReq) && (p=pc
11050 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 29 20  ache1.pLruTail) 
11051 29 7b 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b  ){.      nFree +
11052 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  = sqlite3MallocS
11053 69 7a 65 28 50 47 48 44 52 31 5f 54 4f 5f 50 41  ize(PGHDR1_TO_PA
11054 47 45 28 70 29 29 3b 0a 20 20 20 20 20 20 70 63  GE(p));.      pc
11055 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b  ache1PinPage(p);
11056 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 52 65  .      pcache1Re
11057 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b  moveFromHash(p);
11058 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72  .      pcache1Fr
11059 65 65 50 61 67 65 28 70 29 3b 0a 20 20 20 20 7d  eePage(p);.    }
1105a 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 76  .    pcache1Leav
1105b 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20  eMutex();.  }.  
1105c 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a  return nFree;.}.
1105d 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1105e 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
1105f 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a 23 69  ANAGEMENT */..#i
11060 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
11061 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
11062 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20  tion is used by 
11063 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 20  test procedures 
11064 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20 69  to inspect the i
11065 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 0a 2a 2a  nternal state.**
11066 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 63   of the global c
11067 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ache..*/.SQLITE_
11068 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
11069 69 74 65 33 50 63 61 63 68 65 53 74 61 74 73 28  ite3PcacheStats(
1106a 0a 20 20 69 6e 74 20 2a 70 6e 43 75 72 72 65 6e  .  int *pnCurren
1106b 74 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  t,      /* OUT: 
1106c 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
1106d 70 61 67 65 73 20 63 61 63 68 65 64 20 2a 2f 0a  pages cached */.
1106e 20 20 69 6e 74 20 2a 70 6e 4d 61 78 2c 20 20 20    int *pnMax,   
1106f 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 47         /* OUT: G
11070 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 63 61  lobal maximum ca
11071 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e  che size */.  in
11072 74 20 2a 70 6e 4d 69 6e 2c 20 20 20 20 20 20 20  t *pnMin,       
11073 20 20 20 2f 2a 20 4f 55 54 3a 20 53 75 6d 20 6f     /* OUT: Sum o
11074 66 20 50 43 61 63 68 65 31 2e 6e 4d 69 6e 20 66  f PCache1.nMin f
11075 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 63  or purgeable cac
11076 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  hes */.  int *pn
11077 52 65 63 79 63 6c 61 62 6c 65 20 20 20 20 2f 2a  Recyclable    /*
11078 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62   OUT: Total numb
11079 65 72 20 6f 66 20 70 61 67 65 73 20 61 76 61 69  er of pages avai
1107a 6c 61 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c  lable for recycl
1107b 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64  ing */.){.  PgHd
1107c 72 31 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 52 65  r1 *p;.  int nRe
1107d 63 79 63 6c 61 62 6c 65 20 3d 20 30 3b 0a 20 20  cyclable = 0;.  
1107e 66 6f 72 28 70 3d 70 63 61 63 68 65 31 2e 70 4c  for(p=pcache1.pL
1107f 72 75 48 65 61 64 3b 20 70 3b 20 70 3d 70 2d 3e  ruHead; p; p=p->
11080 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 6e  pLruNext){.    n
11081 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20  Recyclable++;.  
11082 7d 0a 20 20 2a 70 6e 43 75 72 72 65 6e 74 20 3d  }.  *pnCurrent =
11083 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
11084 74 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 78 20  tPage;.  *pnMax 
11085 3d 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61  = pcache1.nMaxPa
11086 67 65 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d 20 70  ge;.  *pnMin = p
11087 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 3b  cache1.nMinPage;
11088 0a 20 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65  .  *pnRecyclable
11089 20 3d 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a   = nRecyclable;.
1108a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  }.#endif../*****
1108b 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1108c 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a   pcache1.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 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
11090 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
11091 66 69 6c 65 20 72 6f 77 73 65 74 2e 63 20 2a 2a  file 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 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
11095 32 30 30 38 20 44 65 63 65 6d 62 65 72 20 33 0a  2008 December 3.
11096 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
11097 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
11098 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
11099 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1109a 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1109b 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1109c 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1109d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1109e 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1109f 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
110a0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
110a1 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
110a2 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
110a3 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
110a4 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
110a5 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
110a6 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
110a7 2e 0a 2a 2a 0a 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 2a 2a 2a 2a 0a 2a  **************.*
110ac 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65  *.** This module
110ad 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f   implements an o
110ae 62 6a 65 63 74 20 77 65 20 63 61 6c 6c 20 61 20  bject we call a 
110af 22 52 6f 77 53 65 74 22 2e 0a 2a 2a 0a 2a 2a 20  "RowSet"..**.** 
110b0 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a 65 63  The RowSet objec
110b1 74 20 69 73 20 61 20 63 6f 6c 6c 65 63 74 69 6f  t is a collectio
110b2 6e 20 6f 66 20 72 6f 77 69 64 73 2e 20 20 52 6f  n of rowids.  Ro
110b3 77 69 64 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65  wids.** are inse
110b4 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 52 6f  rted into the Ro
110b5 77 53 65 74 20 69 6e 20 61 6e 20 61 72 62 69 74  wSet in an arbit
110b6 72 61 72 79 20 6f 72 64 65 72 2e 20 20 49 6e 73  rary order.  Ins
110b7 65 72 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 69  erts.** can be i
110b8 6e 74 65 72 6d 69 78 65 64 20 77 69 74 68 20 74  ntermixed with t
110b9 65 73 74 73 20 74 6f 20 73 65 65 20 69 66 20 61  ests to see if a
110ba 20 67 69 76 65 6e 20 72 6f 77 69 64 20 68 61 73   given rowid has
110bb 20 62 65 65 6e 0a 2a 2a 20 70 72 65 76 69 6f 75   been.** previou
110bc 73 6c 79 20 69 6e 73 65 72 74 65 64 20 69 6e 74  sly inserted int
110bd 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a  o the RowSet..**
110be 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 69 6e  .** After all in
110bf 73 65 72 74 73 20 61 72 65 20 66 69 6e 69 73 68  serts are finish
110c0 65 64 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62  ed, it is possib
110c1 6c 65 20 74 6f 20 65 78 74 72 61 63 74 20 74 68  le to extract th
110c2 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 6f 66  e.** elements of
110c3 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e 20 73   the RowSet in s
110c4 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 4f 6e  orted order.  On
110c5 63 65 20 74 68 69 73 20 65 78 74 72 61 63 74 69  ce this extracti
110c6 6f 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 20 68 61  on.** process ha
110c7 73 20 73 74 61 72 74 65 64 2c 20 6e 6f 20 6e 65  s started, no ne
110c8 77 20 65 6c 65 6d 65 6e 74 73 20 6d 61 79 20 62  w elements may b
110c9 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a  e inserted..**.*
110ca 2a 20 48 65 6e 63 65 2c 20 74 68 65 20 70 72 69  * Hence, the pri
110cb 6d 69 74 69 76 65 20 6f 70 65 72 61 74 69 6f 6e  mitive operation
110cc 73 20 66 6f 72 20 61 20 52 6f 77 53 65 74 20 61  s for a RowSet a
110cd 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 43 52 45  re:.**.**    CRE
110ce 41 54 45 0a 2a 2a 20 20 20 20 49 4e 53 45 52 54  ATE.**    INSERT
110cf 0a 2a 2a 20 20 20 20 54 45 53 54 0a 2a 2a 20 20  .**    TEST.**  
110d0 20 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 20 20    SMALLEST.**   
110d1 20 44 45 53 54 52 4f 59 0a 2a 2a 0a 2a 2a 20 54   DESTROY.**.** T
110d2 68 65 20 43 52 45 41 54 45 20 61 6e 64 20 44 45  he CREATE and DE
110d3 53 54 52 4f 59 20 70 72 69 6d 69 74 69 76 65 73  STROY primitives
110d4 20 61 72 65 20 74 68 65 20 63 6f 6e 73 74 72 75   are the constru
110d5 63 74 6f 72 20 61 6e 64 20 64 65 73 74 72 75 63  ctor and destruc
110d6 74 6f 72 2c 0a 2a 2a 20 6f 62 76 69 6f 75 73 6c  tor,.** obviousl
110d7 79 2e 20 20 54 68 65 20 49 4e 53 45 52 54 20 70  y.  The INSERT p
110d8 72 69 6d 69 74 69 76 65 20 61 64 64 73 20 61 20  rimitive adds a 
110d9 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74  new element to t
110da 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 54 45  he RowSet..** TE
110db 53 54 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65  ST checks to see
110dc 20 69 66 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69   if an element i
110dd 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
110de 20 52 6f 77 53 65 74 2e 20 20 53 4d 41 4c 4c 45   RowSet.  SMALLE
110df 53 54 0a 2a 2a 20 65 78 74 72 61 63 74 73 20 74  ST.** extracts t
110e0 68 65 20 6c 65 61 73 74 20 76 61 6c 75 65 20 66  he least value f
110e1 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74 2e 0a  rom the RowSet..
110e2 2a 2a 0a 2a 2a 20 54 68 65 20 49 4e 53 45 52 54  **.** The INSERT
110e3 20 70 72 69 6d 69 74 69 76 65 20 6d 69 67 68 74   primitive might
110e4 20 61 6c 6c 6f 63 61 74 65 20 61 64 64 69 74 69   allocate additi
110e5 6f 6e 61 6c 20 6d 65 6d 6f 72 79 2e 20 20 4d 65  onal memory.  Me
110e6 6d 6f 72 79 20 69 73 0a 2a 2a 20 61 6c 6c 6f 63  mory is.** alloc
110e7 61 74 65 64 20 69 6e 20 63 68 75 6e 6b 73 20 73  ated in chunks s
110e8 6f 20 6d 6f 73 74 20 49 4e 53 45 52 54 73 20 64  o most INSERTs d
110e9 6f 20 6e 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  o no allocation.
110ea 20 20 54 68 65 72 65 20 69 73 20 61 6e 20 0a 2a    There is an .*
110eb 2a 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e  * upper bound on
110ec 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6c 6c   the size of all
110ed 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 20  ocated memory.  
110ee 4e 6f 20 6d 65 6d 6f 72 79 20 69 73 20 66 72 65  No memory is fre
110ef 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 44 45 53 54  ed.** until DEST
110f0 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54  ROY..**.** The T
110f1 45 53 54 20 70 72 69 6d 69 74 69 76 65 20 69 6e  EST primitive in
110f2 63 6c 75 64 65 73 20 61 20 22 62 61 74 63 68 22  cludes a "batch"
110f3 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 54 45   number.  The TE
110f4 53 54 20 70 72 69 6d 69 74 69 76 65 0a 2a 2a 20  ST primitive.** 
110f5 77 69 6c 6c 20 6f 6e 6c 79 20 73 65 65 20 65 6c  will only see el
110f6 65 6d 65 6e 74 73 20 74 68 61 74 20 77 65 72 65  ements that were
110f7 20 69 6e 73 65 72 74 65 64 20 62 65 66 6f 72 65   inserted before
110f8 20 74 68 65 20 6c 61 73 74 20 63 68 61 6e 67 65   the last change
110f9 0a 2a 2a 20 69 6e 20 74 68 65 20 62 61 74 63 68  .** in the batch
110fa 20 6e 75 6d 62 65 72 2e 20 20 49 6e 20 6f 74 68   number.  In oth
110fb 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61 6e 20  er words, if an 
110fc 49 4e 53 45 52 54 20 6f 63 63 75 72 73 20 62 65  INSERT occurs be
110fd 74 77 65 65 6e 0a 2a 2a 20 74 77 6f 20 54 45 53  tween.** two TES
110fe 54 73 20 77 68 65 72 65 20 74 68 65 20 54 45 53  Ts where the TES
110ff 54 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65  Ts have the same
11100 20 62 61 74 63 68 20 6e 75 62 6d 65 72 2c 20 74   batch nubmer, t
11101 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65  hen the.** value
11102 20 61 64 64 65 64 20 62 79 20 74 68 65 20 49 4e   added by the IN
11103 53 45 52 54 20 77 69 6c 6c 20 6e 6f 74 20 62 65  SERT will not be
11104 20 76 69 73 69 62 6c 65 20 74 6f 20 74 68 65 20   visible to the 
11105 73 65 63 6f 6e 64 20 54 45 53 54 2e 0a 2a 2a 20  second TEST..** 
11106 54 68 65 20 69 6e 69 74 69 61 6c 20 62 61 74 63  The initial batc
11107 68 20 6e 75 6d 62 65 72 20 69 73 20 7a 65 72 6f  h number is zero
11108 2c 20 73 6f 20 69 66 20 74 68 65 20 76 65 72 79  , so if the very
11109 20 66 69 72 73 74 20 54 45 53 54 20 63 6f 6e 74   first TEST cont
1110a 61 69 6e 73 0a 2a 2a 20 61 20 6e 6f 6e 2d 7a 65  ains.** a non-ze
1110b 72 6f 20 62 61 74 63 68 20 6e 75 6d 62 65 72 2c  ro batch number,
1110c 20 69 74 20 77 69 6c 6c 20 73 65 65 20 61 6c 6c   it will see all
1110d 20 70 72 69 6f 72 20 49 4e 53 45 52 54 73 2e 0a   prior INSERTs..
1110e 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e 53 45 52 54 73  **.** No INSERTs
1110f 20 6d 61 79 20 6f 63 63 75 72 73 20 61 66 74 65   may occurs afte
11110 72 20 61 20 53 4d 41 4c 4c 45 53 54 2e 20 20 41  r a SMALLEST.  A
11111 6e 20 61 73 73 65 72 74 69 6f 6e 20 77 69 6c 6c  n assertion will
11112 20 66 61 69 6c 20 69 66 0a 2a 2a 20 74 68 61 74   fail if.** that
11113 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e 0a 2a   is attempted..*
11114 2a 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20 6f 66  *.** The cost of
11115 20 61 6e 20 49 4e 53 45 52 54 20 69 73 20 72 6f   an INSERT is ro
11116 75 67 68 6c 79 20 63 6f 6e 73 74 61 6e 74 2e 20  ughly constant. 
11117 20 28 53 6f 6d 65 74 69 6d 65 20 6e 65 77 20 6d   (Sometime new m
11118 65 6d 6f 72 79 0a 2a 2a 20 68 61 73 20 74 6f 20  emory.** has to 
11119 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20  be allocated on 
1111a 61 6e 20 49 4e 53 45 52 54 2e 29 20 20 54 68 65  an INSERT.)  The
1111b 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53 54 20   cost of a TEST 
1111c 77 69 74 68 20 61 20 6e 65 77 0a 2a 2a 20 62 61  with a new.** ba
1111d 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 4f 28  tch number is O(
1111e 4e 6c 6f 67 4e 29 20 77 68 65 72 65 20 4e 20 69  NlogN) where N i
1111f 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
11120 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  elements in the 
11121 52 6f 77 53 65 74 2e 0a 2a 2a 20 54 68 65 20 63  RowSet..** The c
11122 6f 73 74 20 6f 66 20 61 20 54 45 53 54 20 75 73  ost of a TEST us
11123 69 6e 67 20 74 68 65 20 73 61 6d 65 20 62 61 74  ing the same bat
11124 63 68 20 6e 75 6d 62 65 72 20 69 73 20 4f 28 6c  ch number is O(l
11125 6f 67 4e 29 2e 20 20 54 68 65 20 63 6f 73 74 0a  ogN).  The cost.
11126 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ** of the first 
11127 53 4d 41 4c 4c 45 53 54 20 69 73 20 4f 28 4e 6c  SMALLEST is O(Nl
11128 6f 67 4e 29 2e 20 20 53 65 63 6f 6e 64 20 61 6e  ogN).  Second an
11129 64 20 73 75 62 73 65 71 75 65 6e 74 20 53 4d 41  d subsequent SMA
1112a 4c 4c 45 53 54 0a 2a 2a 20 70 72 69 6d 69 74 69  LLEST.** primiti
1112b 76 65 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74  ves are constant
1112c 20 74 69 6d 65 2e 20 20 54 68 65 20 63 6f 73 74   time.  The cost
1112d 20 6f 66 20 44 45 53 54 52 4f 59 20 69 73 20 4f   of DESTROY is O
1112e 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  (N)..**.** There
1112f 20 69 73 20 61 6e 20 61 64 64 65 64 20 63 6f 73   is an added cos
11130 74 20 6f 66 20 4f 28 4e 29 20 77 68 65 6e 20 73  t of O(N) when s
11131 77 69 74 63 68 69 6e 67 20 62 65 74 77 65 65 6e  witching between
11132 20 54 45 53 54 20 61 6e 64 0a 2a 2a 20 53 4d 41   TEST and.** SMA
11133 4c 4c 45 53 54 20 70 72 69 6d 69 74 69 76 65 73  LLEST primitives
11134 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 6f 77  ..**.** $Id: row
11135 73 65 74 2e 63 2c 76 20 31 2e 37 20 32 30 30 39  set.c,v 1.7 2009
11136 2f 30 35 2f 32 32 20 30 31 3a 30 30 3a 31 33 20  /05/22 01:00:13 
11137 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f  drh Exp $.*/.../
11138 2a 0a 2a 2a 20 54 61 72 67 65 74 20 73 69 7a 65  *.** Target size
11139 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   for allocation 
1113a 63 68 75 6e 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69  chunks..*/.#defi
1113b 6e 65 20 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41  ne ROWSET_ALLOCA
1113c 54 49 4f 4e 5f 53 49 5a 45 20 31 30 32 34 0a 0a  TION_SIZE 1024..
1113d 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72  /*.** The number
1113e 20 6f 66 20 72 6f 77 73 65 74 20 65 6e 74 72 69   of rowset entri
1113f 65 73 20 70 65 72 20 61 6c 6c 6f 63 61 74 69 6f  es per allocatio
11140 6e 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23 64 65 66  n chunk..*/.#def
11141 69 6e 65 20 52 4f 57 53 45 54 5f 45 4e 54 52 59  ine ROWSET_ENTRY
11142 5f 50 45 52 5f 43 48 55 4e 4b 20 20 5c 0a 20 20  _PER_CHUNK  \.  
11143 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11144 20 20 20 20 20 28 28 52 4f 57 53 45 54 5f 41 4c       ((ROWSET_AL
11145 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 2d 38 29  LOCATION_SIZE-8)
11146 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 52  /sizeof(struct R
11147 6f 77 53 65 74 45 6e 74 72 79 29 29 0a 0a 2f 2a  owSetEntry))../*
11148 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69  .** Each entry i
11149 6e 20 61 20 52 6f 77 53 65 74 20 69 73 20 61 6e  n a RowSet is an
1114a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
1114b 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63   following objec
1114c 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77  t..*/.struct Row
1114d 53 65 74 45 6e 74 72 79 20 7b 20 20 20 20 20 20  SetEntry {      
1114e 20 20 20 20 20 20 0a 20 20 69 36 34 20 76 3b 20        .  i64 v; 
1114f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11150 20 20 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20         /* ROWID 
11151 76 61 6c 75 65 20 66 6f 72 20 74 68 69 73 20 65  value for this e
11152 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74  ntry */.  struct
11153 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 52   RowSetEntry *pR
11154 69 67 68 74 3b 20 20 20 2f 2a 20 52 69 67 68 74  ight;   /* Right
11155 20 73 75 62 74 72 65 65 20 28 6c 61 72 67 65 72   subtree (larger
11156 20 65 6e 74 72 69 65 73 29 20 6f 72 20 6c 69 73   entries) or lis
11157 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f  t */.  struct Ro
11158 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 66 74  wSetEntry *pLeft
11159 3b 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 75 62  ;    /* Left sub
1115a 74 72 65 65 20 28 73 6d 61 6c 6c 65 72 20 65 6e  tree (smaller en
1115b 74 72 69 65 73 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  tries) */.};../*
1115c 0a 2a 2a 20 52 6f 77 53 65 74 45 6e 74 72 79 20  .** RowSetEntry 
1115d 6f 62 6a 65 63 74 73 20 61 72 65 20 61 6c 6c 6f  objects are allo
1115e 63 61 74 65 64 20 69 6e 20 6c 61 72 67 65 20 63  cated in large c
1115f 68 75 6e 6b 73 20 28 69 6e 73 74 61 6e 63 65 73  hunks (instances
11160 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f   of the.** follo
11161 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 29 20  wing structure) 
11162 74 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79  to reduce memory
11163 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 76 65 72   allocation over
11164 68 65 61 64 2e 20 20 54 68 65 0a 2a 2a 20 63 68  head.  The.** ch
11165 75 6e 6b 73 20 61 72 65 20 6b 65 70 74 20 6f 6e  unks are kept on
11166 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73   a linked list s
11167 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20  o that they can 
11168 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 0a 2a  be deallocated.*
11169 2a 20 77 68 65 6e 20 74 68 65 20 52 6f 77 53 65  * when the RowSe
1116a 74 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e 0a  t is destroyed..
1116b 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65 74  */.struct RowSet
1116c 43 68 75 6e 6b 20 7b 0a 20 20 73 74 72 75 63 74  Chunk {.  struct
1116d 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e   RowSetChunk *pN
1116e 65 78 74 43 68 75 6e 6b 3b 20 20 20 20 20 20 20  extChunk;       
1116f 20 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 6f   /* Next chunk o
11170 6e 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61  n list of them a
11171 6c 6c 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  ll */.  struct R
11172 6f 77 53 65 74 45 6e 74 72 79 20 61 45 6e 74 72  owSetEntry aEntr
11173 79 5b 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50  y[ROWSET_ENTRY_P
11174 45 52 5f 43 48 55 4e 4b 5d 3b 20 2f 2a 20 41 6c  ER_CHUNK]; /* Al
11175 6c 6f 63 61 74 65 64 20 65 6e 74 72 69 65 73 20  located entries 
11176 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 52  */.};../*.** A R
11177 6f 77 53 65 74 20 69 6e 20 61 6e 20 69 6e 73 74  owSet in an inst
11178 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
11179 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
1117a 0a 2a 2a 0a 2a 2a 20 41 20 74 79 70 65 64 65 66  .**.** A typedef
1117b 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
1117c 72 65 20 69 66 20 66 6f 75 6e 64 20 69 6e 20 73  re if found in s
1117d 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 73  qliteInt.h..*/.s
1117e 74 72 75 63 74 20 52 6f 77 53 65 74 20 7b 0a 20  truct RowSet {. 
1117f 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68   struct RowSetCh
11180 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20  unk *pChunk;    
11181 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 63  /* List of all c
11182 68 75 6e 6b 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  hunk allocations
11183 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
11184 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
11185 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
11186 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
11187 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
11188 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b  etEntry *pEntry;
11189 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 65      /* List of e
1118a 6e 74 72 69 65 73 20 75 73 69 6e 67 20 70 52 69  ntries using pRi
1118b 67 68 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ght */.  struct 
1118c 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 61  RowSetEntry *pLa
1118d 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74 20  st;     /* Last 
1118e 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 45 6e  entry on the pEn
1118f 74 72 79 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74  try list */.  st
11190 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11191 20 2a 70 46 72 65 73 68 3b 20 20 20 20 2f 2a 20   *pFresh;    /* 
11192 53 6f 75 72 63 65 20 6f 66 20 6e 65 77 20 65 6e  Source of new en
11193 74 72 79 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20  try objects */. 
11194 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
11195 74 72 79 20 2a 70 54 72 65 65 3b 20 20 20 20 20  try *pTree;     
11196 2f 2a 20 42 69 6e 61 72 79 20 74 72 65 65 20 6f  /* Binary tree o
11197 66 20 65 6e 74 72 69 65 73 20 2a 2f 0a 20 20 75  f entries */.  u
11198 31 36 20 6e 46 72 65 73 68 3b 20 20 20 20 20 20  16 nFresh;      
11199 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1119a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 63   Number of objec
1119b 74 73 20 6f 6e 20 70 46 72 65 73 68 20 2a 2f 0a  ts on pFresh */.
1119c 20 20 75 38 20 69 73 53 6f 72 74 65 64 3b 20 20    u8 isSorted;  
1119d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1119e 20 2f 2a 20 54 72 75 65 20 69 66 20 70 45 6e 74   /* True if pEnt
1119f 72 79 20 69 73 20 73 6f 72 74 65 64 20 2a 2f 0a  ry is sorted */.
111a0 20 20 75 38 20 69 42 61 74 63 68 3b 20 20 20 20    u8 iBatch;    
111a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
111a2 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 73 65   /* Current inse
111a3 72 74 20 62 61 74 63 68 20 2a 2f 0a 7d 3b 0a 0a  rt batch */.};..
111a4 2f 2a 0a 2a 2a 20 54 75 72 6e 20 62 75 6c 6b 20  /*.** Turn bulk 
111a5 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 52 6f  memory into a Ro
111a6 77 53 65 74 20 6f 62 6a 65 63 74 2e 20 20 4e 20  wSet object.  N 
111a7 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a  bytes of memory.
111a8 2a 2a 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  ** are available
111a9 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 65   at pSpace.  The
111aa 20 64 62 20 70 6f 69 6e 74 65 72 20 69 73 20 75   db pointer is u
111ab 73 65 64 20 61 73 20 61 20 6d 65 6d 6f 72 79 20  sed as a memory 
111ac 63 6f 6e 74 65 78 74 0a 2a 2a 20 66 6f 72 20 61  context.** for a
111ad 6e 79 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c  ny subsequent al
111ae 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6e  locations that n
111af 65 65 64 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a  eed to occur..**
111b0 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
111b1 72 20 74 6f 20 74 68 65 20 6e 65 77 20 52 6f 77  r to the new Row
111b2 53 65 74 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  Set object..**.*
111b3 2a 20 49 74 20 6d 75 73 74 20 62 65 20 74 68 65  * It must be the
111b4 20 63 61 73 65 20 74 68 61 74 20 4e 20 69 73 20   case that N is 
111b5 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 6d 61  sufficient to ma
111b6 6b 65 20 61 20 52 6f 77 73 65 74 2e 20 20 49 66  ke a Rowset.  If
111b7 20 6e 6f 74 0a 2a 2a 20 61 6e 20 61 73 73 65 72   not.** an asser
111b8 74 69 6f 6e 20 66 61 75 6c 74 20 6f 63 63 75 72  tion fault occur
111b9 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 4e 20 69  s..** .** If N i
111ba 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
111bb 65 20 6d 69 6e 69 6d 75 6d 2c 20 75 73 65 20 74  e minimum, use t
111bc 68 65 20 73 75 72 70 6c 75 73 20 61 73 20 61 6e  he surplus as an
111bd 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 61 6c 6c 6f   initial.** allo
111be 63 61 74 69 6f 6e 20 6f 66 20 65 6e 74 72 69 65  cation of entrie
111bf 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 62  s available to b
111c0 65 20 66 69 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c  e filled..*/.SQL
111c1 49 54 45 5f 50 52 49 56 41 54 45 20 52 6f 77 53  ITE_PRIVATE RowS
111c2 65 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 53 65  et *sqlite3RowSe
111c3 74 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64  tInit(sqlite3 *d
111c4 62 2c 20 76 6f 69 64 20 2a 70 53 70 61 63 65 2c  b, void *pSpace,
111c5 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 4e 29   unsigned int N)
111c6 7b 0a 20 20 52 6f 77 53 65 74 20 2a 70 3b 0a 20  {.  RowSet *p;. 
111c7 20 61 73 73 65 72 74 28 20 4e 20 3e 3d 20 52 4f   assert( N >= RO
111c8 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29 29  UND8(sizeof(*p))
111c9 20 29 3b 0a 20 20 70 20 3d 20 70 53 70 61 63 65   );.  p = pSpace
111ca 3b 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20  ;.  p->pChunk = 
111cb 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b  0;.  p->db = db;
111cc 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30  .  p->pEntry = 0
111cd 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30  ;.  p->pLast = 0
111ce 3b 0a 20 20 70 2d 3e 70 54 72 65 65 20 3d 20 30  ;.  p->pTree = 0
111cf 3b 0a 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20  ;.  p->pFresh = 
111d0 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e  (struct RowSetEn
111d1 74 72 79 2a 29 28 52 4f 55 4e 44 38 28 73 69 7a  try*)(ROUND8(siz
111d2 65 6f 66 28 2a 70 29 29 20 2b 20 28 63 68 61 72  eof(*p)) + (char
111d3 2a 29 70 29 3b 0a 20 20 70 2d 3e 6e 46 72 65 73  *)p);.  p->nFres
111d4 68 20 3d 20 28 75 31 36 29 28 28 4e 20 2d 20 52  h = (u16)((N - R
111d5 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29  OUND8(sizeof(*p)
111d6 29 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74  ))/sizeof(struct
111d7 20 52 6f 77 53 65 74 45 6e 74 72 79 29 29 3b 0a   RowSetEntry));.
111d8 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20    p->isSorted = 
111d9 31 3b 0a 20 20 70 2d 3e 69 42 61 74 63 68 20 3d  1;.  p->iBatch =
111da 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a   0;.  return p;.
111db 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63  }../*.** Dealloc
111dc 61 74 65 20 61 6c 6c 20 63 68 75 6e 6b 73 20 66  ate all chunks f
111dd 72 6f 6d 20 61 20 52 6f 77 53 65 74 2e 20 20 54  rom a RowSet.  T
111de 68 69 73 20 66 72 65 65 73 20 61 6c 6c 20 6d 65  his frees all me
111df 6d 6f 72 79 20 74 68 61 74 0a 2a 2a 20 74 68 65  mory that.** the
111e0 20 52 6f 77 53 65 74 20 68 61 73 20 61 6c 6c 6f   RowSet has allo
111e1 63 61 74 65 64 20 6f 76 65 72 20 69 74 73 20 6c  cated over its l
111e2 69 66 65 74 69 6d 65 2e 20 20 54 68 69 73 20 72  ifetime.  This r
111e3 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 74 68 65  outine is.** the
111e4 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20   destructor for 
111e5 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2f 0a 53  the RowSet..*/.S
111e6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
111e7 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  id sqlite3RowSet
111e8 43 6c 65 61 72 28 52 6f 77 53 65 74 20 2a 70 29  Clear(RowSet *p)
111e9 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  {.  struct RowSe
111ea 74 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20  tChunk *pChunk, 
111eb 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 66  *pNextChunk;.  f
111ec 6f 72 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 43 68  or(pChunk=p->pCh
111ed 75 6e 6b 3b 20 70 43 68 75 6e 6b 3b 20 70 43 68  unk; pChunk; pCh
111ee 75 6e 6b 20 3d 20 70 4e 65 78 74 43 68 75 6e 6b  unk = pNextChunk
111ef 29 7b 0a 20 20 20 20 70 4e 65 78 74 43 68 75 6e  ){.    pNextChun
111f0 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78  k = pChunk->pNex
111f1 74 43 68 75 6e 6b 3b 0a 20 20 20 20 73 71 6c 69  tChunk;.    sqli
111f2 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c  te3DbFree(p->db,
111f3 20 70 43 68 75 6e 6b 29 3b 0a 20 20 7d 0a 20 20   pChunk);.  }.  
111f4 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20  p->pChunk = 0;. 
111f5 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 30 3b 0a   p->nFresh = 0;.
111f6 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b    p->pEntry = 0;
111f7 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b  .  p->pLast = 0;
111f8 0a 20 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b  .  p->pTree = 0;
111f9 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d  .  p->isSorted =
111fa 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73   1;.}../*.** Ins
111fb 65 72 74 20 61 20 6e 65 77 20 76 61 6c 75 65 20  ert a new value 
111fc 69 6e 74 6f 20 61 20 52 6f 77 53 65 74 2e 0a 2a  into a RowSet..*
111fd 2a 0a 2a 2a 20 54 68 65 20 6d 61 6c 6c 6f 63 46  *.** The mallocF
111fe 61 69 6c 65 64 20 66 6c 61 67 20 6f 66 20 74 68  ailed flag of th
111ff 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
11200 63 74 69 6f 6e 20 69 73 20 73 65 74 20 69 66 20  ction is set if 
11201 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  a.** memory allo
11202 63 61 74 69 6f 6e 20 66 61 69 6c 73 2e 0a 2a 2f  cation fails..*/
11203 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
11204 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53  void sqlite3RowS
11205 65 74 49 6e 73 65 72 74 28 52 6f 77 53 65 74 20  etInsert(RowSet 
11206 2a 70 2c 20 69 36 34 20 72 6f 77 69 64 29 7b 0a  *p, i64 rowid){.
11207 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
11208 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b 20 20 2f  ntry *pEntry;  /
11209 2a 20 54 68 65 20 6e 65 77 20 65 6e 74 72 79 20  * The new entry 
1120a 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
1120b 65 74 45 6e 74 72 79 20 2a 70 4c 61 73 74 3b 20  etEntry *pLast; 
1120c 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 70 72    /* The last pr
1120d 69 6f 72 20 65 6e 74 72 79 20 2a 2f 0a 20 20 61  ior entry */.  a
1120e 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
1120f 20 69 66 28 20 70 2d 3e 6e 46 72 65 73 68 3d 3d   if( p->nFresh==
11210 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  0 ){.    struct 
11211 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e 65  RowSetChunk *pNe
11212 77 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71  w;.    pNew = sq
11213 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
11214 28 70 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a  (p->db, sizeof(*
11215 70 4e 65 77 29 29 3b 0a 20 20 20 20 69 66 28 20  pNew));.    if( 
11216 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pNew==0 ){.     
11217 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
11218 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 43 68     pNew->pNextCh
11219 75 6e 6b 20 3d 20 70 2d 3e 70 43 68 75 6e 6b 3b  unk = p->pChunk;
1121a 0a 20 20 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d  .    p->pChunk =
1121b 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 70 46   pNew;.    p->pF
1121c 72 65 73 68 20 3d 20 70 4e 65 77 2d 3e 61 45 6e  resh = pNew->aEn
1121d 74 72 79 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 65  try;.    p->nFre
1121e 73 68 20 3d 20 52 4f 57 53 45 54 5f 45 4e 54 52  sh = ROWSET_ENTR
1121f 59 5f 50 45 52 5f 43 48 55 4e 4b 3b 0a 20 20 7d  Y_PER_CHUNK;.  }
11220 0a 20 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70  .  pEntry = p->p
11221 46 72 65 73 68 2b 2b 3b 0a 20 20 70 2d 3e 6e 46  Fresh++;.  p->nF
11222 72 65 73 68 2d 2d 3b 0a 20 20 70 45 6e 74 72 79  resh--;.  pEntry
11223 2d 3e 76 20 3d 20 72 6f 77 69 64 3b 0a 20 20 70  ->v = rowid;.  p
11224 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d 20  Entry->pRight = 
11225 30 3b 0a 20 20 70 4c 61 73 74 20 3d 20 70 2d 3e  0;.  pLast = p->
11226 70 4c 61 73 74 3b 0a 20 20 69 66 28 20 70 4c 61  pLast;.  if( pLa
11227 73 74 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  st ){.    if( p-
11228 3e 69 73 53 6f 72 74 65 64 20 26 26 20 72 6f 77  >isSorted && row
11229 69 64 3c 3d 70 4c 61 73 74 2d 3e 76 20 29 7b 0a  id<=pLast->v ){.
1122a 20 20 20 20 20 20 70 2d 3e 69 73 53 6f 72 74 65        p->isSorte
1122b 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  d = 0;.    }.   
1122c 20 70 4c 61 73 74 2d 3e 70 52 69 67 68 74 20 3d   pLast->pRight =
1122d 20 70 45 6e 74 72 79 3b 0a 20 20 7d 65 6c 73 65   pEntry;.  }else
1122e 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  {.    assert( p-
1122f 3e 70 45 6e 74 72 79 3d 3d 30 20 29 3b 20 2f 2a  >pEntry==0 ); /*
11230 20 46 69 72 65 73 20 69 66 20 49 4e 53 45 52 54   Fires if INSERT
11231 20 61 66 74 65 72 20 53 4d 41 4c 4c 45 53 54 20   after SMALLEST 
11232 2a 2f 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79  */.    p->pEntry
11233 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 7d 0a 20   = pEntry;.  }. 
11234 20 70 2d 3e 70 4c 61 73 74 20 3d 20 70 45 6e 74   p->pLast = pEnt
11235 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72  ry;.}../*.** Mer
11236 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20  ge two lists of 
11237 52 6f 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65  RowSetEntry obje
11238 63 74 73 2e 20 20 52 65 6d 6f 76 65 20 64 75 70  cts.  Remove dup
11239 6c 69 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  licates..**.** T
1123a 68 65 20 69 6e 70 75 74 20 6c 69 73 74 73 20 61  he input lists a
1123b 72 65 20 63 6f 6e 6e 65 63 74 65 64 20 76 69 61  re connected via
1123c 20 70 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73   pRight pointers
1123d 20 61 6e 64 20 61 72 65 20 0a 2a 2a 20 61 73 73   and are .** ass
1123e 75 6d 65 64 20 74 6f 20 65 61 63 68 20 61 6c 72  umed to each alr
1123f 65 61 64 79 20 62 65 20 69 6e 20 73 6f 72 74 65  eady be in sorte
11240 64 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74  d order..*/.stat
11241 69 63 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  ic struct RowSet
11242 45 6e 74 72 79 20 2a 72 6f 77 53 65 74 4d 65 72  Entry *rowSetMer
11243 67 65 28 0a 20 20 73 74 72 75 63 74 20 52 6f 77  ge(.  struct Row
11244 53 65 74 45 6e 74 72 79 20 2a 70 41 2c 20 20 20  SetEntry *pA,   
11245 20 2f 2a 20 46 69 72 73 74 20 73 6f 72 74 65 64   /* First sorted
11246 20 6c 69 73 74 20 74 6f 20 62 65 20 6d 65 72 67   list to be merg
11247 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  ed */.  struct R
11248 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 42 20 20  owSetEntry *pB  
11249 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 73 6f 72     /* Second sor
1124a 74 65 64 20 6c 69 73 74 20 74 6f 20 62 65 20 6d  ted list to be m
1124b 65 72 67 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 74  erged */.){.  st
1124c 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
1124d 20 68 65 61 64 3b 0a 20 20 73 74 72 75 63 74 20   head;.  struct 
1124e 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 54 61  RowSetEntry *pTa
1124f 69 6c 3b 0a 0a 20 20 70 54 61 69 6c 20 3d 20 26  il;..  pTail = &
11250 68 65 61 64 3b 0a 20 20 77 68 69 6c 65 28 20 70  head;.  while( p
11251 41 20 26 26 20 70 42 20 29 7b 0a 20 20 20 20 61  A && pB ){.    a
11252 73 73 65 72 74 28 20 70 41 2d 3e 70 52 69 67 68  ssert( pA->pRigh
11253 74 3d 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70  t==0 || pA->v<=p
11254 41 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a  A->pRight->v );.
11255 20 20 20 20 61 73 73 65 72 74 28 20 70 42 2d 3e      assert( pB->
11256 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42 2d  pRight==0 || pB-
11257 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d 3e  >v<=pB->pRight->
11258 76 20 29 3b 0a 20 20 20 20 69 66 28 20 70 41 2d  v );.    if( pA-
11259 3e 76 3c 70 42 2d 3e 76 20 29 7b 0a 20 20 20 20  >v<pB->v ){.    
1125a 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20    pTail->pRight 
1125b 3d 20 70 41 3b 0a 20 20 20 20 20 20 70 41 20 3d  = pA;.      pA =
1125c 20 70 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20   pA->pRight;.   
1125d 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61 69 6c     pTail = pTail
1125e 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65  ->pRight;.    }e
1125f 6c 73 65 20 69 66 28 20 70 42 2d 3e 76 3c 70 41  lse if( pB->v<pA
11260 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61  ->v ){.      pTa
11261 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 42 3b  il->pRight = pB;
11262 0a 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e  .      pB = pB->
11263 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54  pRight;.      pT
11264 61 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69  ail = pTail->pRi
11265 67 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ght;.    }else{.
11266 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70        pA = pA->p
11267 52 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  Right;.    }.  }
11268 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20  .  if( pA ){.   
11269 20 61 73 73 65 72 74 28 20 70 41 2d 3e 70 52 69   assert( pA->pRi
1126a 67 68 74 3d 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c  ght==0 || pA->v<
1126b 3d 70 41 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29  =pA->pRight->v )
1126c 3b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69  ;.    pTail->pRi
1126d 67 68 74 20 3d 20 70 41 3b 0a 20 20 7d 65 6c 73  ght = pA;.  }els
1126e 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
1126f 42 3d 3d 30 20 7c 7c 20 70 42 2d 3e 70 52 69 67  B==0 || pB->pRig
11270 68 74 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d  ht==0 || pB->v<=
11271 70 42 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b  pB->pRight->v );
11272 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 67  .    pTail->pRig
11273 68 74 20 3d 20 70 42 3b 0a 20 20 7d 0a 20 20 72  ht = pB;.  }.  r
11274 65 74 75 72 6e 20 68 65 61 64 2e 70 52 69 67 68  eturn head.pRigh
11275 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74  t;.}../*.** Sort
11276 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 6e   all elements on
11277 20 74 68 65 20 70 45 6e 74 72 79 20 6c 69 73 74   the pEntry list
11278 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74 20 69   of the RowSet i
11279 6e 74 6f 20 61 73 63 65 6e 64 69 6e 67 20 6f 72  nto ascending or
1127a 64 65 72 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20  der..*/ .static 
1127b 76 6f 69 64 20 72 6f 77 53 65 74 53 6f 72 74 28  void rowSetSort(
1127c 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 75 6e  RowSet *p){.  un
1127d 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20  signed int i;.  
1127e 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
1127f 72 79 20 2a 70 45 6e 74 72 79 3b 0a 20 20 73 74  ry *pEntry;.  st
11280 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11281 20 2a 61 42 75 63 6b 65 74 5b 34 30 5d 3b 0a 0a   *aBucket[40];..
11282 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73 53    assert( p->isS
11283 6f 72 74 65 64 3d 3d 30 20 29 3b 0a 20 20 6d 65  orted==0 );.  me
11284 6d 73 65 74 28 61 42 75 63 6b 65 74 2c 20 30 2c  mset(aBucket, 0,
11285 20 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29   sizeof(aBucket)
11286 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70  );.  while( p->p
11287 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 45 6e  Entry ){.    pEn
11288 74 72 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 3b  try = p->pEntry;
11289 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d  .    p->pEntry =
1128a 20 70 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 3b   pEntry->pRight;
1128b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 70 52 69  .    pEntry->pRi
1128c 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  ght = 0;.    for
1128d 28 69 3d 30 3b 20 61 42 75 63 6b 65 74 5b 69 5d  (i=0; aBucket[i]
1128e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 45  ; i++){.      pE
1128f 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 72  ntry = rowSetMer
11290 67 65 28 61 42 75 63 6b 65 74 5b 69 5d 2c 20 70  ge(aBucket[i], p
11291 45 6e 74 72 79 29 3b 0a 20 20 20 20 20 20 61 42  Entry);.      aB
11292 75 63 6b 65 74 5b 69 5d 20 3d 20 30 3b 0a 20 20  ucket[i] = 0;.  
11293 20 20 7d 0a 20 20 20 20 61 42 75 63 6b 65 74 5b    }.    aBucket[
11294 69 5d 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 7d  i] = pEntry;.  }
11295 0a 20 20 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20  .  pEntry = 0;. 
11296 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65   for(i=0; i<size
11297 6f 66 28 61 42 75 63 6b 65 74 29 2f 73 69 7a 65  of(aBucket)/size
11298 6f 66 28 61 42 75 63 6b 65 74 5b 30 5d 29 3b 20  of(aBucket[0]); 
11299 69 2b 2b 29 7b 0a 20 20 20 20 70 45 6e 74 72 79  i++){.    pEntry
1129a 20 3d 20 72 6f 77 53 65 74 4d 65 72 67 65 28 70   = rowSetMerge(p
1129b 45 6e 74 72 79 2c 20 61 42 75 63 6b 65 74 5b 69  Entry, aBucket[i
1129c 5d 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 45 6e  ]);.  }.  p->pEn
1129d 74 72 79 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20  try = pEntry;.  
1129e 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20  p->pLast = 0;.  
1129f 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b  p->isSorted = 1;
112a0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69  .}.../*.** The i
112a1 6e 70 75 74 2c 20 70 49 6e 2c 20 69 73 20 61 20  nput, pIn, is a 
112a2 62 69 6e 61 72 79 20 74 72 65 65 20 28 6f 72 20  binary tree (or 
112a3 73 75 62 74 72 65 65 29 20 6f 66 20 52 6f 77 53  subtree) of RowS
112a4 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e  etEntry objects.
112a5 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 73  .** Convert this
112a6 20 74 72 65 65 20 69 6e 74 6f 20 61 20 6c 69 6e   tree into a lin
112a7 6b 65 64 20 6c 69 73 74 20 63 6f 6e 6e 65 63 74  ked list connect
112a8 65 64 20 62 79 20 74 68 65 20 70 52 69 67 68 74  ed by the pRight
112a9 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 61 6e 64   pointers.** and
112aa 20 72 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 73   return pointers
112ab 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 61 6e   to the first an
112ac 64 20 6c 61 73 74 20 65 6c 65 6d 65 6e 74 73 20  d last elements 
112ad 6f 66 20 74 68 65 20 6e 65 77 20 6c 69 73 74 2e  of the new list.
112ae 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
112af 72 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74  rowSetTreeToList
112b0 28 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  (.  struct RowSe
112b1 74 45 6e 74 72 79 20 2a 70 49 6e 2c 20 20 20 20  tEntry *pIn,    
112b2 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 20       /* Root of 
112b3 74 68 65 20 69 6e 70 75 74 20 74 72 65 65 20 2a  the input tree *
112b4 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  /.  struct RowSe
112b5 74 45 6e 74 72 79 20 2a 2a 70 70 46 69 72 73 74  tEntry **ppFirst
112b6 2c 20 20 20 20 2f 2a 20 57 72 69 74 65 20 68 65  ,    /* Write he
112b7 61 64 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74  ad of the output
112b8 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 20 20   list here */.  
112b9 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
112ba 72 79 20 2a 2a 70 70 4c 61 73 74 20 20 20 20 20  ry **ppLast     
112bb 20 2f 2a 20 57 72 69 74 65 20 74 61 69 6c 20 6f   /* Write tail o
112bc 66 20 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73  f the output lis
112bd 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 61  t here */.){.  a
112be 73 73 65 72 74 28 20 70 49 6e 21 3d 30 20 29 3b  ssert( pIn!=0 );
112bf 0a 20 20 69 66 28 20 70 49 6e 2d 3e 70 4c 65 66  .  if( pIn->pLef
112c0 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  t ){.    struct 
112c1 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a  RowSetEntry *p;.
112c2 20 20 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f      rowSetTreeTo
112c3 4c 69 73 74 28 70 49 6e 2d 3e 70 4c 65 66 74 2c  List(pIn->pLeft,
112c4 20 70 70 46 69 72 73 74 2c 20 26 70 29 3b 0a 20   ppFirst, &p);. 
112c5 20 20 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 70     p->pRight = p
112c6 49 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  In;.  }else{.   
112c7 20 2a 70 70 46 69 72 73 74 20 3d 20 70 49 6e 3b   *ppFirst = pIn;
112c8 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 2d 3e  .  }.  if( pIn->
112c9 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 72 6f  pRight ){.    ro
112ca 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70  wSetTreeToList(p
112cb 49 6e 2d 3e 70 52 69 67 68 74 2c 20 26 70 49 6e  In->pRight, &pIn
112cc 2d 3e 70 52 69 67 68 74 2c 20 70 70 4c 61 73 74  ->pRight, ppLast
112cd 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
112ce 2a 70 70 4c 61 73 74 20 3d 20 70 49 6e 3b 0a 20  *ppLast = pIn;. 
112cf 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70   }.  assert( (*p
112d0 70 4c 61 73 74 29 2d 3e 70 52 69 67 68 74 3d 3d  pLast)->pRight==
112d1 30 20 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43  0 );.}.../*.** C
112d2 6f 6e 76 65 72 74 20 61 20 73 6f 72 74 65 64 20  onvert a sorted 
112d3 6c 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73  list of elements
112d4 20 28 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70   (connected by p
112d5 52 69 67 68 74 29 20 69 6e 74 6f 20 61 20 62 69  Right) into a bi
112d6 6e 61 72 79 0a 2a 2a 20 74 72 65 65 20 77 69 74  nary.** tree wit
112d7 68 20 64 65 70 74 68 20 6f 66 20 69 44 65 70 74  h depth of iDept
112d8 68 2e 20 20 41 20 64 65 70 74 68 20 6f 66 20 31  h.  A depth of 1
112d9 20 6d 65 61 6e 73 20 74 68 65 20 74 72 65 65 20   means the tree 
112da 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c  contains a singl
112db 65 0a 2a 2a 20 6e 6f 64 65 20 74 61 6b 65 6e 20  e.** node taken 
112dc 66 72 6f 6d 20 74 68 65 20 68 65 61 64 20 6f 66  from the head of
112dd 20 2a 70 70 4c 69 73 74 2e 20 20 41 20 64 65 70   *ppList.  A dep
112de 74 68 20 6f 66 20 32 20 6d 65 61 6e 73 20 61 20  th of 2 means a 
112df 74 72 65 65 20 77 69 74 68 0a 2a 2a 20 74 68 72  tree with.** thr
112e0 65 65 20 6e 6f 64 65 73 2e 20 20 41 6e 64 20 73  ee nodes.  And s
112e1 6f 20 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 55  o forth..**.** U
112e2 73 65 20 61 73 20 6d 61 6e 79 20 65 6e 74 72 69  se as many entri
112e3 65 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75  es from the inpu
112e4 74 20 6c 69 73 74 20 61 73 20 72 65 71 75 69 72  t list as requir
112e5 65 64 20 61 6e 64 20 75 70 64 61 74 65 20 74 68  ed and update th
112e6 65 0a 2a 2a 20 2a 70 70 4c 69 73 74 20 74 6f 20  e.** *ppList to 
112e7 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 75 6e 75  point to the unu
112e8 73 65 64 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  sed elements of 
112e9 74 68 65 20 6c 69 73 74 2e 20 20 49 66 20 74 68  the list.  If th
112ea 65 20 69 6e 70 75 74 0a 2a 2a 20 6c 69 73 74 20  e input.** list 
112eb 63 6f 6e 74 61 69 6e 73 20 74 6f 6f 20 66 65 77  contains too few
112ec 20 65 6c 65 6d 65 6e 74 73 2c 20 74 68 65 6e 20   elements, then 
112ed 63 6f 6e 73 74 72 75 63 74 20 61 6e 20 69 6e 63  construct an inc
112ee 6f 6d 70 6c 65 74 65 20 74 72 65 65 0a 2a 2a 20  omplete tree.** 
112ef 61 6e 64 20 6c 65 61 76 65 20 2a 70 70 4c 69 73  and leave *ppLis
112f0 74 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  t set to NULL..*
112f1 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
112f2 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 6f 6f  inter to the roo
112f3 74 20 6f 66 20 74 68 65 20 63 6f 6e 73 74 72 75  t of the constru
112f4 63 74 65 64 20 62 69 6e 61 72 79 20 74 72 65 65  cted binary tree
112f5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
112f6 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
112f7 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 28  rowSetNDeepTree(
112f8 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
112f9 45 6e 74 72 79 20 2a 2a 70 70 4c 69 73 74 2c 0a  Entry **ppList,.
112fa 20 20 69 6e 74 20 69 44 65 70 74 68 0a 29 7b 0a    int iDepth.){.
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 3b 20 20 20 20 20 20 20 20  ntry *p;        
112fd 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20   /* Root of the 
112fe 6e 65 77 20 74 72 65 65 20 2a 2f 0a 20 20 73 74  new tree */.  st
112ff 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11300 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20   *pLeft;     /* 
11301 4c 65 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a  Left subtree */.
11302 20 20 69 66 28 20 2a 70 70 4c 69 73 74 3d 3d 30    if( *ppList==0
11303 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
11304 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 44 65 70  ;.  }.  if( iDep
11305 74 68 3d 3d 31 20 29 7b 0a 20 20 20 20 70 20 3d  th==1 ){.    p =
11306 20 2a 70 70 4c 69 73 74 3b 0a 20 20 20 20 2a 70   *ppList;.    *p
11307 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68  pList = p->pRigh
11308 74 3b 0a 20 20 20 20 70 2d 3e 70 4c 65 66 74 20  t;.    p->pLeft 
11309 3d 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b  = p->pRight = 0;
1130a 0a 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20  .    return p;. 
1130b 20 7d 0a 20 20 70 4c 65 66 74 20 3d 20 72 6f 77   }.  pLeft = row
1130c 53 65 74 4e 44 65 65 70 54 72 65 65 28 70 70 4c  SetNDeepTree(ppL
1130d 69 73 74 2c 20 69 44 65 70 74 68 2d 31 29 3b 0a  ist, iDepth-1);.
1130e 20 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20    p = *ppList;. 
1130f 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20   if( p==0 ){.   
11310 20 72 65 74 75 72 6e 20 70 4c 65 66 74 3b 0a 20   return pLeft;. 
11311 20 7d 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20   }.  p->pLeft = 
11312 70 4c 65 66 74 3b 0a 20 20 2a 70 70 4c 69 73 74  pLeft;.  *ppList
11313 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20   = p->pRight;.  
11314 70 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f 77 53  p->pRight = rowS
11315 65 74 4e 44 65 65 70 54 72 65 65 28 70 70 4c 69  etNDeepTree(ppLi
11316 73 74 2c 20 69 44 65 70 74 68 2d 31 29 3b 0a 20  st, iDepth-1);. 
11317 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
11318 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f  .** Convert a so
11319 72 74 65 64 20 6c 69 73 74 20 6f 66 20 65 6c 65  rted list of ele
1131a 6d 65 6e 74 73 20 69 6e 74 6f 20 61 20 62 69 6e  ments into a bin
1131b 61 72 79 20 74 72 65 65 2e 20 4d 61 6b 65 20 74  ary tree. Make t
1131c 68 65 20 74 72 65 65 0a 2a 2a 20 61 73 20 64 65  he tree.** as de
1131d 65 70 20 61 73 20 69 74 20 6e 65 65 64 73 20 74  ep as it needs t
1131e 6f 20 62 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  o be in order to
1131f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 65 6e 74   contain the ent
11320 69 72 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61  ire list..*/.sta
11321 74 69 63 20 73 74 72 75 63 74 20 52 6f 77 53 65  tic struct RowSe
11322 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 74 4c 69  tEntry *rowSetLi
11323 73 74 54 6f 54 72 65 65 28 73 74 72 75 63 74 20  stToTree(struct 
11324 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 69  RowSetEntry *pLi
11325 73 74 29 7b 0a 20 20 69 6e 74 20 69 44 65 70 74  st){.  int iDept
11326 68 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  h;           /* 
11327 44 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 65  Depth of the tre
11328 65 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 73 74  e so far */.  st
11329 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
1132a 20 2a 70 3b 20 20 20 20 20 20 20 2f 2a 20 43 75   *p;       /* Cu
1132b 72 72 65 6e 74 20 74 72 65 65 20 72 6f 6f 74 20  rrent tree root 
1132c 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
1132d 65 74 45 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20  etEntry *pLeft; 
1132e 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65    /* Left subtre
1132f 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
11330 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 70 20  pList!=0 );.  p 
11331 3d 20 70 4c 69 73 74 3b 0a 20 20 70 4c 69 73 74  = pList;.  pList
11332 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20   = p->pRight;.  
11333 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52  p->pLeft = p->pR
11334 69 67 68 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28  ight = 0;.  for(
11335 69 44 65 70 74 68 3d 31 3b 20 70 4c 69 73 74 3b  iDepth=1; pList;
11336 20 69 44 65 70 74 68 2b 2b 29 7b 0a 20 20 20 20   iDepth++){.    
11337 70 4c 65 66 74 20 3d 20 70 3b 0a 20 20 20 20 70  pLeft = p;.    p
11338 20 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 70 4c   = pList;.    pL
11339 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b  ist = p->pRight;
1133a 0a 20 20 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20  .    p->pLeft = 
1133b 70 4c 65 66 74 3b 0a 20 20 20 20 70 2d 3e 70 52  pLeft;.    p->pR
1133c 69 67 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65  ight = rowSetNDe
1133d 65 70 54 72 65 65 28 26 70 4c 69 73 74 2c 20 69  epTree(&pList, i
1133e 44 65 70 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65  Depth);.  }.  re
1133f 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
11340 20 43 6f 6e 76 65 72 74 20 74 68 65 20 6c 69 73   Convert the lis
11341 74 20 69 6e 20 70 2d 3e 70 45 6e 74 72 79 20 69  t in p->pEntry i
11342 6e 74 6f 20 61 20 73 6f 72 74 65 64 20 6c 69 73  nto a sorted lis
11343 74 20 69 66 20 69 74 20 69 73 20 6e 6f 74 0a 2a  t if it is not.*
11344 2a 20 73 6f 72 74 65 64 20 61 6c 72 65 61 64 79  * sorted already
11345 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 61  .  If there is a
11346 20 62 69 6e 61 72 79 20 74 72 65 65 20 6f 6e 20   binary tree on 
11347 70 2d 3e 70 54 72 65 65 2c 20 74 68 65 6e 0a 2a  p->pTree, then.*
11348 2a 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74  * convert it int
11349 6f 20 61 20 6c 69 73 74 20 74 6f 6f 20 61 6e 64  o a list too and
1134a 20 6d 65 72 67 65 20 69 74 20 69 6e 74 6f 20 74   merge it into t
1134b 68 65 20 70 2d 3e 70 45 6e 74 72 79 20 6c 69 73  he p->pEntry lis
1134c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
1134d 64 20 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 52  d rowSetToList(R
1134e 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 69 66 28  owSet *p){.  if(
1134f 20 21 70 2d 3e 69 73 53 6f 72 74 65 64 20 29 7b   !p->isSorted ){
11350 0a 20 20 20 20 72 6f 77 53 65 74 53 6f 72 74 28  .    rowSetSort(
11351 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  p);.  }.  if( p-
11352 3e 70 54 72 65 65 20 29 7b 0a 20 20 20 20 73 74  >pTree ){.    st
11353 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11354 20 2a 70 48 65 61 64 2c 20 2a 70 54 61 69 6c 3b   *pHead, *pTail;
11355 0a 20 20 20 20 72 6f 77 53 65 74 54 72 65 65 54  .    rowSetTreeT
11356 6f 4c 69 73 74 28 70 2d 3e 70 54 72 65 65 2c 20  oList(p->pTree, 
11357 26 70 48 65 61 64 2c 20 26 70 54 61 69 6c 29 3b  &pHead, &pTail);
11358 0a 20 20 20 20 70 2d 3e 70 54 72 65 65 20 3d 20  .    p->pTree = 
11359 30 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79  0;.    p->pEntry
1135a 20 3d 20 72 6f 77 53 65 74 4d 65 72 67 65 28 70   = rowSetMerge(p
1135b 2d 3e 70 45 6e 74 72 79 2c 20 70 48 65 61 64 29  ->pEntry, pHead)
1135c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  ;.  }.}../*.** E
1135d 78 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c  xtract the small
1135e 65 73 74 20 65 6c 65 6d 65 6e 74 20 66 72 6f 6d  est element from
1135f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20   the RowSet..** 
11360 57 72 69 74 65 20 74 68 65 20 65 6c 65 6d 65 6e  Write the elemen
11361 74 20 69 6e 74 6f 20 2a 70 52 6f 77 69 64 2e 20  t into *pRowid. 
11362 20 52 65 74 75 72 6e 20 31 20 6f 6e 20 73 75 63   Return 1 on suc
11363 63 65 73 73 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  cess.  Return.**
11364 20 30 20 69 66 20 74 68 65 20 52 6f 77 53 65 74   0 if the RowSet
11365 20 69 73 20 61 6c 72 65 61 64 79 20 65 6d 70 74   is already empt
11366 79 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74  y..**.** After t
11367 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20  his routine has 
11368 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 74 68 65  been called, the
11369 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e   sqlite3RowSetIn
1136a 73 65 72 74 28 29 0a 2a 2a 20 72 6f 75 74 69 6e  sert().** routin
1136b 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 61 6c  e may not be cal
1136c 6c 65 64 20 61 67 61 69 6e 2e 20 20 0a 2a 2f 0a  led again.  .*/.
1136d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1136e 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  nt sqlite3RowSet
1136f 4e 65 78 74 28 52 6f 77 53 65 74 20 2a 70 2c 20  Next(RowSet *p, 
11370 69 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20  i64 *pRowid){.  
11371 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 29 3b  rowSetToList(p);
11372 0a 20 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79  .  if( p->pEntry
11373 20 29 7b 0a 20 20 20 20 2a 70 52 6f 77 69 64 20   ){.    *pRowid 
11374 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e 76 3b 0a  = p->pEntry->v;.
11375 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20      p->pEntry = 
11376 70 2d 3e 70 45 6e 74 72 79 2d 3e 70 52 69 67 68  p->pEntry->pRigh
11377 74 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 45  t;.    if( p->pE
11378 6e 74 72 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ntry==0 ){.     
11379 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c   sqlite3RowSetCl
1137a 65 61 72 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20  ear(p);.    }.  
1137b 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65    return 1;.  }e
1137c 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
1137d 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
1137e 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
1137f 65 6c 65 6d 65 6e 74 20 69 52 6f 77 69 64 20 77  element iRowid w
11380 61 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  as inserted into
11381 20 74 68 65 20 74 68 65 20 72 6f 77 73 65 74 20   the the rowset 
11382 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 6e  as.** part of an
11383 79 20 69 6e 73 65 72 74 20 62 61 74 63 68 20 70  y insert batch p
11384 72 69 6f 72 20 74 6f 20 69 42 61 74 63 68 2e 20  rior to iBatch. 
11385 20 52 65 74 75 72 6e 20 31 20 6f 72 20 30 2e 0a   Return 1 or 0..
11386 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
11387 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77  E int sqlite3Row
11388 53 65 74 54 65 73 74 28 52 6f 77 53 65 74 20 2a  SetTest(RowSet *
11389 70 52 6f 77 53 65 74 2c 20 75 38 20 69 42 61 74  pRowSet, u8 iBat
1138a 63 68 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ch, sqlite3_int6
1138b 34 20 69 52 6f 77 69 64 29 7b 0a 20 20 73 74 72  4 iRowid){.  str
1138c 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
1138d 2a 70 3b 0a 20 20 69 66 28 20 69 42 61 74 63 68  *p;.  if( iBatch
1138e 21 3d 70 52 6f 77 53 65 74 2d 3e 69 42 61 74 63  !=pRowSet->iBatc
1138f 68 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52 6f  h ){.    if( pRo
11390 77 53 65 74 2d 3e 70 45 6e 74 72 79 20 29 7b 0a  wSet->pEntry ){.
11391 20 20 20 20 20 20 72 6f 77 53 65 74 54 6f 4c 69        rowSetToLi
11392 73 74 28 70 52 6f 77 53 65 74 29 3b 0a 20 20 20  st(pRowSet);.   
11393 20 20 20 70 52 6f 77 53 65 74 2d 3e 70 54 72 65     pRowSet->pTre
11394 65 20 3d 20 72 6f 77 53 65 74 4c 69 73 74 54 6f  e = rowSetListTo
11395 54 72 65 65 28 70 52 6f 77 53 65 74 2d 3e 70 45  Tree(pRowSet->pE
11396 6e 74 72 79 29 3b 0a 20 20 20 20 20 20 70 52 6f  ntry);.      pRo
11397 77 53 65 74 2d 3e 70 45 6e 74 72 79 20 3d 20 30  wSet->pEntry = 0
11398 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d  ;.      pRowSet-
11399 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 20 20  >pLast = 0;.    
1139a 7d 0a 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 69  }.    pRowSet->i
1139b 42 61 74 63 68 20 3d 20 69 42 61 74 63 68 3b 0a  Batch = iBatch;.
1139c 20 20 7d 0a 20 20 70 20 3d 20 70 52 6f 77 53 65    }.  p = pRowSe
1139d 74 2d 3e 70 54 72 65 65 3b 0a 20 20 77 68 69 6c  t->pTree;.  whil
1139e 65 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20  e( p ){.    if( 
1139f 70 2d 3e 76 3c 69 52 6f 77 69 64 20 29 7b 0a 20  p->v<iRowid ){. 
113a0 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67       p = p->pRig
113a1 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ht;.    }else if
113a2 28 20 70 2d 3e 76 3e 69 52 6f 77 69 64 20 29 7b  ( p->v>iRowid ){
113a3 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c  .      p = p->pL
113a4 65 66 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  eft;.    }else{.
113a5 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
113a6 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
113a7 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  rn 0;.}../******
113a8 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
113a9 72 6f 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  rowset.c *******
113aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
113ad 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
113ae 69 6c 65 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a  ile pager.c ****
113af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113b1 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
113b2 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
113b3 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
113b4 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
113b5 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
113b6 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
113b7 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
113b8 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
113b9 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
113ba 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
113bb 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
113bc 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
113bd 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
113be 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
113bf 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
113c0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
113c1 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
113c2 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
113c3 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
113c4 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
113c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
113c9 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69  ** This is the i
113ca 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
113cb 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20   the page cache 
113cc 73 75 62 73 79 73 74 65 6d 20 6f 72 20 22 70 61  subsystem or "pa
113cd 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65  ger"..** .** The
113ce 20 70 61 67 65 72 20 69 73 20 75 73 65 64 20 74   pager is used t
113cf 6f 20 61 63 63 65 73 73 20 61 20 64 61 74 61 62  o access a datab
113d0 61 73 65 20 64 69 73 6b 20 66 69 6c 65 2e 20 20  ase disk file.  
113d1 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a  It implements.**
113d2 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20 61   atomic commit a
113d3 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 72 6f  nd rollback thro
113d4 75 67 68 20 74 68 65 20 75 73 65 20 6f 66 20 61  ugh the use of a
113d5 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68   journal file th
113d6 61 74 0a 2a 2a 20 69 73 20 73 65 70 61 72 61 74  at.** is separat
113d7 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  e from the datab
113d8 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70  ase file.  The p
113d9 61 67 65 72 20 61 6c 73 6f 20 69 6d 70 6c 65 6d  ager also implem
113da 65 6e 74 73 20 66 69 6c 65 0a 2a 2a 20 6c 6f 63  ents file.** loc
113db 6b 69 6e 67 20 74 6f 20 70 72 65 76 65 6e 74 20  king to prevent 
113dc 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 66 72  two processes fr
113dd 6f 6d 20 77 72 69 74 69 6e 67 20 74 68 65 20 73  om writing the s
113de 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ame database.** 
113df 66 69 6c 65 20 73 69 6d 75 6c 74 61 6e 65 6f 75  file simultaneou
113e0 73 6c 79 2c 20 6f 72 20 6f 6e 65 20 70 72 6f 63  sly, or one proc
113e1 65 73 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67  ess from reading
113e2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 68   the database wh
113e3 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 69  ile.** another i
113e4 73 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a  s writing..**.**
113e5 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65 72   @(#) $Id: pager
113e6 2e 63 2c 76 20 31 2e 36 32 39 20 32 30 30 39 2f  .c,v 1.629 2009/
113e7 30 38 2f 31 30 20 31 37 3a 34 38 3a 35 37 20 64  08/10 17:48:57 d
113e8 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  rh Exp $.*/.#ifn
113e9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
113ea 44 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d 61  DISKIO../*.** Ma
113eb 63 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c 65  cros for trouble
113ec 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d 61  shooting.  Norma
113ed 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a  lly turned off.*
113ee 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69  /.#if 0.int sqli
113ef 74 65 33 50 61 67 65 72 54 72 61 63 65 3d 31 3b  te3PagerTrace=1;
113f0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61    /* True to ena
113f1 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23  ble tracing */.#
113f2 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65  define sqlite3De
113f3 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74 66  bugPrintf printf
113f4 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52  .#define PAGERTR
113f5 41 43 45 28 58 29 20 20 20 20 20 69 66 28 20 73  ACE(X)     if( s
113f6 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 65  qlite3PagerTrace
113f7 20 29 7b 20 73 71 6c 69 74 65 33 44 65 62 75 67   ){ sqlite3Debug
113f8 50 72 69 6e 74 66 20 58 3b 20 7d 0a 23 65 6c 73  Printf X; }.#els
113f9 65 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54  e.#define PAGERT
113fa 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a  RACE(X).#endif..
113fb 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
113fc 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f 73 20 61  ing two macros a
113fd 72 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 74  re used within t
113fe 68 65 20 50 41 47 45 52 54 52 41 43 45 28 29 20  he PAGERTRACE() 
113ff 6d 61 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20  macros above.** 
11400 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 66 69 6c  to print out fil
11401 65 2d 64 65 73 63 72 69 70 74 6f 72 73 2e 20 0a  e-descriptors. .
11402 2a 2a 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 20  **.** PAGERID() 
11403 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20  takes a pointer 
11404 74 6f 20 61 20 50 61 67 65 72 20 73 74 72 75 63  to a Pager struc
11405 74 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e  t as its argumen
11406 74 2e 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69  t. The.** associ
11407 61 74 65 64 20 66 69 6c 65 2d 64 65 73 63 72 69  ated file-descri
11408 70 74 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  ptor is returned
11409 2e 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 29  . FILEHANDLEID()
1140a 20 74 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 65   takes an sqlite
1140b 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74  3_file.** struct
1140c 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74   as its argument
1140d 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47  ..*/.#define PAG
1140e 45 52 49 44 28 70 29 20 28 28 69 6e 74 29 28 70  ERID(p) ((int)(p
1140f 2d 3e 66 64 29 29 0a 23 64 65 66 69 6e 65 20 46  ->fd)).#define F
11410 49 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29 20  ILEHANDLEID(fd) 
11411 28 28 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a  ((int)fd)../*.**
11412 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
11413 61 73 20 61 20 77 68 6f 6c 65 20 69 73 20 61 6c  as a whole is al
11414 77 61 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74  ways in one of t
11415 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
11416 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  states:.**.**   
11417 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20  PAGER_UNLOCK    
11418 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63      The page cac
11419 68 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  he is not curren
1141a 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20 0a  tly reading or .
1141b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1141c 20 20 20 20 20 20 20 20 20 77 72 69 74 69 6e 67           writing
1141d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1141e 6c 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f  le.  There is no
1141f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
11420 20 20 20 20 20 20 20 20 20 20 64 61 74 61 20 68            data h
11421 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20  eld in memory.  
11422 54 68 69 73 20 69 73 20 74 68 65 20 69 6e 69 74  This is the init
11423 69 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ial.**          
11424 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61               sta
11425 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45  te..**.**   PAGE
11426 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 20 20  R_SHARED        
11427 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69  The page cache i
11428 73 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61  s reading the da
11429 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20  tabase..**      
1142a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1142b 20 57 72 69 74 69 6e 67 20 69 73 20 6e 6f 74 20   Writing is not 
1142c 70 65 72 6d 69 74 74 65 64 2e 20 20 54 68 65 72  permitted.  Ther
1142d 65 20 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20 20  e can be.**     
1142e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1142f 20 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65    multiple reade
11430 72 73 20 61 63 63 65 73 73 69 6e 67 20 74 68 65  rs accessing the
11431 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a   same database.*
11432 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
11433 20 20 20 20 20 20 20 20 66 69 6c 65 20 61 74 20          file at 
11434 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a  the same time..*
11435 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 45 53  *.**   PAGER_RES
11436 45 52 56 45 44 20 20 20 20 20 20 54 68 69 73 20  ERVED      This 
11437 70 72 6f 63 65 73 73 20 68 61 73 20 72 65 73 65  process has rese
11438 72 76 65 64 20 74 68 65 20 64 61 74 61 62 61 73  rved the databas
11439 65 20 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a  e for writing.**
1143a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1143b 20 20 20 20 20 20 20 62 75 74 20 68 61 73 20 6e         but has n
1143c 6f 74 20 79 65 74 20 6d 61 64 65 20 61 6e 79 20  ot yet made any 
1143d 63 68 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 6f  changes.  Only o
1143e 6e 65 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20 20  ne process.**   
1143f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11440 20 20 20 20 61 74 20 61 20 74 69 6d 65 20 63 61      at a time ca
11441 6e 20 72 65 73 65 72 76 65 20 74 68 65 20 64 61  n reserve the da
11442 74 61 62 61 73 65 2e 20 20 54 68 65 20 6f 72 69  tabase.  The ori
11443 67 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20  ginal.**        
11444 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
11445 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73  atabase file has
11446 20 6e 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 69   not been modifi
11447 65 64 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 20  ed so other.**  
11448 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11449 20 20 20 20 20 70 72 6f 63 65 73 73 65 73 20 6d       processes m
1144a 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61 64  ay still be read
1144b 69 6e 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b 0a  ing the on-disk.
1144c 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1144d 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73           databas
1144e 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  e file..**.**   
1144f 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20  PAGER_EXCLUSIVE 
11450 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63      The page cac
11451 68 65 20 69 73 20 77 72 69 74 69 6e 67 20 74 68  he is writing th
11452 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20  e database..**  
11453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11454 20 20 20 20 20 41 63 63 65 73 73 20 69 73 20 65       Access is e
11455 78 63 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f 74  xclusive.  No ot
11456 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 6f 72  her processes or
11457 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
11458 20 20 20 20 20 20 20 20 20 20 74 68 72 65 61 64            thread
11459 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67  s can be reading
1145a 20 6f 72 20 77 72 69 74 69 6e 67 20 77 68 69 6c   or writing whil
1145b 65 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20  e one.**        
1145c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1145d 72 6f 63 65 73 73 20 69 73 20 77 72 69 74 69 6e  rocess is writin
1145e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52  g..**.**   PAGER
1145f 5f 53 59 4e 43 45 44 20 20 20 20 20 20 20 20 54  _SYNCED        T
11460 68 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74  he pager moves t
11461 6f 20 74 68 69 73 20 73 74 61 74 65 20 66 72 6f  o this state fro
11462 6d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  m PAGER_EXCLUSIV
11463 45 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  E.**            
11464 20 20 20 20 20 20 20 20 20 20 20 61 66 74 65 72             after
11465 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
11466 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74   have been writt
11467 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20  en to the.**    
11468 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11469 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65     database file
1146a 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68 61   and the file ha
1146b 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f  s been synced to
1146c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1146d 20 20 20 20 20 20 20 20 20 20 64 69 73 6b 2e 20            disk. 
1146e 41 6c 6c 20 74 68 61 74 20 72 65 6d 61 69 6e 73  All that remains
1146f 20 74 6f 20 64 6f 20 69 73 20 74 6f 20 72 65 6d   to do is to rem
11470 6f 76 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  ove or.**       
11471 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11472 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75  truncate the jou
11473 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 74 68  rnal file and th
11474 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a  e transaction .*
11475 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
11476 20 20 20 20 20 20 20 20 77 69 6c 6c 20 62 65 20          will be 
11477 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a  committed..**.**
11478 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
11479 63 6f 6d 65 73 20 75 70 20 69 6e 20 50 41 47 45  comes up in PAGE
1147a 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66  R_UNLOCK.  The f
1147b 69 72 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20 73  irst time a.** s
1147c 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
1147d 20 6f 63 63 75 72 73 2c 20 74 68 65 20 73 74 61   occurs, the sta
1147e 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74  te transitions t
1147f 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 2e 0a  o PAGER_SHARED..
11480 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 70 61 67  ** After all pag
11481 65 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c  es have been rel
11482 65 61 73 65 64 20 75 73 69 6e 67 20 73 71 6c 69  eased using sqli
11483 74 65 5f 70 61 67 65 5f 75 6e 72 65 66 28 29 2c  te_page_unref(),
11484 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 74 72  .** the state tr
11485 61 6e 73 69 74 69 6f 6e 73 20 62 61 63 6b 20 74  ansitions back t
11486 6f 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20  o PAGER_UNLOCK. 
11487 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 0a   The first time.
11488 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 50  ** that sqlite3P
11489 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63  agerWrite() is c
1148a 61 6c 6c 65 64 2c 20 74 68 65 20 73 74 61 74 65  alled, the state
1148b 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 0a   transitions to.
1148c 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45  ** PAGER_RESERVE
1148d 44 2e 20 20 28 4e 6f 74 65 20 74 68 61 74 20 73  D.  (Note that s
1148e 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
1148f 28 29 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a  () can only be.*
11490 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f  * called on an o
11491 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20  utstanding page 
11492 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74  which means that
11493 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 0a   the pager must.
11494 2a 2a 20 62 65 20 69 6e 20 50 41 47 45 52 5f 53  ** be in PAGER_S
11495 48 41 52 45 44 20 62 65 66 6f 72 65 20 69 74 20  HARED before it 
11496 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50  transitions to P
11497 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 29 0a  AGER_RESERVED.).
11498 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45  ** PAGER_RESERVE
11499 44 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  D means that the
1149a 72 65 20 69 73 20 61 6e 20 6f 70 65 6e 20 72 6f  re is an open ro
1149b 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a  llback journal..
1149c 2a 2a 20 54 68 65 20 74 72 61 6e 73 69 74 69 6f  ** The transitio
1149d 6e 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55  n to PAGER_EXCLU
1149e 53 49 56 45 20 6f 63 63 75 72 73 20 62 65 66 6f  SIVE occurs befo
1149f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 2a  re any changes.*
114a0 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68  * are made to th
114a1 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
114a2 20 74 68 6f 75 67 68 20 77 72 69 74 65 73 20 74   though writes t
114a3 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a  o the rollback.*
114a4 2a 20 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72 73  * journal occurs
114a5 20 77 69 74 68 20 6a 75 73 74 20 50 41 47 45 52   with just PAGER
114a6 5f 52 45 53 45 52 56 45 44 2e 20 20 41 66 74 65  _RESERVED.  Afte
114a7 72 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65  r an sqlite3Page
114a8 72 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f  rRollback().** o
114a9 72 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  r sqlite3PagerCo
114aa 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 2c 20  mmitPhaseTwo(), 
114ab 74 68 65 20 73 74 61 74 65 20 63 61 6e 20 67 6f  the state can go
114ac 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53   back to PAGER_S
114ad 48 41 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 20  HARED,.** or it 
114ae 63 61 6e 20 73 74 61 79 20 61 74 20 50 41 47 45  can stay at PAGE
114af 52 5f 45 58 43 4c 55 53 49 56 45 20 69 66 20 77  R_EXCLUSIVE if w
114b0 65 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69  e are in exclusi
114b1 76 65 20 61 63 63 65 73 73 20 6d 6f 64 65 2e 0a  ve access mode..
114b2 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
114b3 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a 23  _UNLOCK      0.#
114b4 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 48 41  define PAGER_SHA
114b5 52 45 44 20 20 20 20 20 20 31 20 20 20 2f 2a 20  RED      1   /* 
114b6 73 61 6d 65 20 61 73 20 53 48 41 52 45 44 5f 4c  same as SHARED_L
114b7 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  OCK */.#define P
114b8 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 20  AGER_RESERVED   
114b9 20 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20   2   /* same as 
114ba 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f  RESERVED_LOCK */
114bb 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 45  .#define PAGER_E
114bc 58 43 4c 55 53 49 56 45 20 20 20 34 20 20 20 2f  XCLUSIVE   4   /
114bd 2a 20 73 61 6d 65 20 61 73 20 45 58 43 4c 55 53  * same as EXCLUS
114be 49 56 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66  IVE_LOCK */.#def
114bf 69 6e 65 20 50 41 47 45 52 5f 53 59 4e 43 45 44  ine PAGER_SYNCED
114c0 20 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41        5../*.** A
114c1 20 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72 20   macro used for 
114c2 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 64  invoking the cod
114c3 65 63 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  ec if there is o
114c4 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ne.*/.#ifdef SQL
114c5 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 20  ITE_HAS_CODEC.# 
114c6 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c  define CODEC1(P,
114c7 44 2c 4e 2c 58 2c 45 29 20 5c 0a 20 20 20 20 69  D,N,X,E) \.    i
114c8 66 28 20 50 2d 3e 78 43 6f 64 65 63 20 26 26 20  f( P->xCodec && 
114c9 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f  P->xCodec(P->pCo
114ca 64 65 63 2c 44 2c 4e 2c 58 29 3d 3d 30 20 29 7b  dec,D,N,X)==0 ){
114cb 20 45 3b 20 7d 0a 23 20 64 65 66 69 6e 65 20 43   E; }.# define C
114cc 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c  ODEC2(P,D,N,X,E,
114cd 4f 29 20 5c 0a 20 20 20 20 69 66 28 20 50 2d 3e  O) \.    if( P->
114ce 78 43 6f 64 65 63 3d 3d 30 20 29 7b 20 4f 3d 28  xCodec==0 ){ O=(
114cf 63 68 61 72 2a 29 44 3b 20 7d 65 6c 73 65 20 5c  char*)D; }else \
114d0 0a 20 20 20 20 69 66 28 20 28 4f 3d 28 63 68 61  .    if( (O=(cha
114d1 72 2a 29 28 50 2d 3e 78 43 6f 64 65 63 28 50 2d  r*)(P->xCodec(P-
114d2 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 29 29  >pCodec,D,N,X)))
114d3 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 65 6c 73  ==0 ){ E; }.#els
114d4 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43  e.# define CODEC
114d5 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 20 20 2f  1(P,D,N,X,E)   /
114d6 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66  * NO-OP */.# def
114d7 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e  ine CODEC2(P,D,N
114d8 2c 58 2c 45 2c 4f 29 20 4f 3d 28 63 68 61 72 2a  ,X,E,O) O=(char*
114d9 29 44 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  )D.#endif../*.**
114da 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c   The maximum all
114db 6f 77 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65  owed sector size
114dc 2e 20 36 34 4b 69 42 2e 20 49 66 20 74 68 65 20  . 64KiB. If the 
114dd 78 53 65 63 74 6f 72 73 69 7a 65 28 29 20 6d 65  xSectorsize() me
114de 74 68 6f 64 20 0a 2a 2a 20 72 65 74 75 72 6e 73  thod .** returns
114df 20 61 20 76 61 6c 75 65 20 6c 61 72 67 65 72 20   a value larger 
114e0 74 68 61 6e 20 74 68 69 73 2c 20 74 68 65 6e 20  than this, then 
114e1 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20  MAX_SECTOR_SIZE 
114e2 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e  is used instead.
114e3 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 63  .** This could c
114e4 6f 6e 63 65 69 76 61 62 6c 79 20 63 61 75 73 65  onceivably cause
114e5 20 63 6f 72 72 75 70 74 69 6f 6e 20 66 6f 6c 6c   corruption foll
114e6 6f 77 69 6e 67 20 61 20 70 6f 77 65 72 20 66 61  owing a power fa
114e7 69 6c 75 72 65 20 6f 6e 0a 2a 2a 20 73 75 63 68  ilure on.** such
114e8 20 61 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20   a system. This 
114e9 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20  is currently an 
114ea 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20 6c 69 6d  undocumented lim
114eb 69 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  it..*/.#define M
114ec 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 30  AX_SECTOR_SIZE 0
114ed 78 31 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6e  x10000../*.** An
114ee 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
114ef 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
114f0 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65  ture is allocate
114f1 64 20 66 6f 72 20 65 61 63 68 20 61 63 74 69 76  d for each activ
114f2 65 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 61  e.** savepoint a
114f3 6e 64 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  nd statement tra
114f4 6e 73 61 63 74 69 6f 6e 20 69 6e 20 74 68 65 20  nsaction in the 
114f5 73 79 73 74 65 6d 2e 20 41 6c 6c 20 73 75 63 68  system. All such
114f6 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 61   structures.** a
114f7 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  re stored in the
114f8 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e   Pager.aSavepoin
114f9 74 5b 5d 20 61 72 72 61 79 2c 20 77 68 69 63 68  t[] array, which
114fa 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
114fb 64 0a 2a 2a 20 72 65 73 69 7a 65 64 20 75 73 69  d.** resized usi
114fc 6e 67 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f  ng sqlite3Reallo
114fd 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  c()..**.** When 
114fe 61 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 63  a savepoint is c
114ff 72 65 61 74 65 64 2c 20 74 68 65 20 50 61 67 65  reated, the Page
11500 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f  rSavepoint.iHdrO
11501 66 66 73 65 74 20 66 69 65 6c 64 20 69 73 0a 2a  ffset field is.*
11502 2a 20 73 65 74 20 74 6f 20 30 2e 20 49 66 20 61  * set to 0. If a
11503 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20   journal-header 
11504 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  is written into 
11505 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
11506 20 77 68 69 6c 65 0a 2a 2a 20 74 68 65 20 73 61   while.** the sa
11507 76 65 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76  vepoint is activ
11508 65 2c 20 74 68 65 6e 20 69 48 64 72 4f 66 66 73  e, then iHdrOffs
11509 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  et is set to the
1150a 20 62 79 74 65 20 6f 66 66 73 65 74 20 0a 2a 2a   byte offset .**
1150b 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c   immediately fol
1150c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20  lowing the last 
1150d 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 77  journal record w
1150e 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
1150f 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  main.** journal 
11510 62 65 66 6f 72 65 20 74 68 65 20 6a 6f 75 72 6e  before the journ
11511 61 6c 2d 68 65 61 64 65 72 2e 20 54 68 69 73 20  al-header. This 
11512 69 73 20 72 65 71 75 69 72 65 64 20 64 75 72 69  is required duri
11513 6e 67 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20  ng savepoint.** 
11514 72 6f 6c 6c 62 61 63 6b 20 28 73 65 65 20 70 61  rollback (see pa
11515 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70  gerPlaybackSavep
11516 6f 69 6e 74 28 29 29 2e 0a 2a 2f 0a 74 79 70 65  oint())..*/.type
11517 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72  def struct Pager
11518 53 61 76 65 70 6f 69 6e 74 20 50 61 67 65 72 53  Savepoint PagerS
11519 61 76 65 70 6f 69 6e 74 3b 0a 73 74 72 75 63 74  avepoint;.struct
1151a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20   PagerSavepoint 
1151b 7b 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b  {.  i64 iOffset;
1151c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1151d 20 2f 2a 20 53 74 61 72 74 69 6e 67 20 6f 66 66   /* Starting off
1151e 73 65 74 20 69 6e 20 6d 61 69 6e 20 6a 6f 75 72  set in main jour
1151f 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64  nal */.  i64 iHd
11520 72 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  rOffset;        
11521 20 20 20 20 20 20 2f 2a 20 53 65 65 20 61 62 6f        /* See abo
11522 76 65 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a  ve */.  Bitvec *
11523 70 49 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20  pInSavepoint;   
11524 20 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 70       /* Set of p
11525 61 67 65 73 20 69 6e 20 74 68 69 73 20 73 61 76  ages in this sav
11526 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 50 67 6e 6f  epoint */.  Pgno
11527 20 6e 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20   nOrig;         
11528 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67           /* Orig
11529 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  inal number of p
1152a 61 67 65 73 20 69 6e 20 66 69 6c 65 20 2a 2f 0a  ages in file */.
1152b 20 20 50 67 6e 6f 20 69 53 75 62 52 65 63 3b 20    Pgno iSubRec; 
1152c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1152d 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72 73 74  * Index of first
1152e 20 72 65 63 6f 72 64 20 69 6e 20 73 75 62 2d 6a   record in sub-j
1152f 6f 75 72 6e 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ournal */.};../*
11530 0a 2a 2a 20 41 20 6f 70 65 6e 20 70 61 67 65 20  .** A open page 
11531 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74  cache is an inst
11532 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
11533 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
11534 0a 2a 2a 0a 2a 2a 20 65 72 72 43 6f 64 65 0a 2a  .**.** errCode.*
11535 2a 0a 2a 2a 20 20 20 50 61 67 65 72 2e 65 72 72  *.**   Pager.err
11536 43 6f 64 65 20 6d 61 79 20 62 65 20 73 65 74 20  Code may be set 
11537 74 6f 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c  to SQLITE_IOERR,
11538 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c   SQLITE_CORRUPT,
11539 20 6f 72 0a 2a 2a 20 20 20 6f 72 20 53 51 4c 49   or.**   or SQLI
1153a 54 45 5f 46 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e  TE_FULL. Once on
1153b 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74  e of the first t
1153c 68 72 65 65 20 65 72 72 6f 72 73 20 6f 63 63 75  hree errors occu
1153d 72 73 2c 20 69 74 20 70 65 72 73 69 73 74 73 0a  rs, it persists.
1153e 2a 2a 20 20 20 61 6e 64 20 69 73 20 72 65 74 75  **   and is retu
1153f 72 6e 65 64 20 61 73 20 74 68 65 20 72 65 73 75  rned as the resu
11540 6c 74 20 6f 66 20 65 76 65 72 79 20 6d 61 6a 6f  lt of every majo
11541 72 20 70 61 67 65 72 20 41 50 49 20 63 61 6c 6c  r pager API call
11542 2e 20 20 54 68 65 0a 2a 2a 20 20 20 53 51 4c 49  .  The.**   SQLI
11543 54 45 5f 46 55 4c 4c 20 72 65 74 75 72 6e 20 63  TE_FULL return c
11544 6f 64 65 20 69 73 20 73 6c 69 67 68 74 6c 79 20  ode is slightly 
11545 64 69 66 66 65 72 65 6e 74 2e 20 49 74 20 70 65  different. It pe
11546 72 73 69 73 74 73 20 6f 6e 6c 79 20 75 6e 74 69  rsists only unti
11547 6c 20 74 68 65 0a 2a 2a 20 20 20 6e 65 78 74 20  l the.**   next 
11548 73 75 63 63 65 73 73 66 75 6c 20 72 6f 6c 6c 62  successful rollb
11549 61 63 6b 20 69 73 20 70 65 72 66 6f 72 6d 65 64  ack is performed
1154a 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 63 61   on the pager ca
1154b 63 68 65 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 20 20  che. Also,.**   
1154c 53 51 4c 49 54 45 5f 46 55 4c 4c 20 64 6f 65 73  SQLITE_FULL does
1154d 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65 20   not affect the 
1154e 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
1154f 29 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67  ) and sqlite3Pag
11550 65 72 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 20 20  erLookup().**   
11551 41 50 49 73 2c 20 74 68 65 79 20 6d 61 79 20 73  APIs, they may s
11552 74 69 6c 6c 20 62 65 20 75 73 65 64 20 73 75 63  till be used suc
11553 63 65 73 73 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a  cessfully..**.**
11554 20 64 62 53 69 7a 65 56 61 6c 69 64 2c 20 64 62   dbSizeValid, db
11555 53 69 7a 65 2c 20 64 62 4f 72 69 67 53 69 7a 65  Size, dbOrigSize
11556 2c 20 64 62 46 69 6c 65 53 69 7a 65 0a 2a 2a 0a  , dbFileSize.**.
11557 2a 2a 20 20 20 4d 61 6e 61 67 69 6e 67 20 74 68  **   Managing th
11558 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  e size of the da
11559 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70  tabase file in p
1155a 61 67 65 73 20 69 73 20 61 20 6c 69 74 74 6c 65  ages is a little
1155b 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 0a 2a 2a   complicated..**
1155c 20 20 20 54 68 65 20 76 61 72 69 61 62 6c 65 20     The variable 
1155d 50 61 67 65 72 2e 64 62 53 69 7a 65 20 63 6f 6e  Pager.dbSize con
1155e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  tains the number
1155f 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 74   of pages that t
11560 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20  he database.**  
11561 20 69 6d 61 67 65 20 63 75 72 72 65 6e 74 6c 79   image currently
11562 20 63 6f 6e 74 61 69 6e 73 2e 20 41 73 20 74 68   contains. As th
11563 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  e database image
11564 20 67 72 6f 77 73 20 6f 72 20 73 68 72 69 6e 6b   grows or shrink
11565 73 20 74 68 69 73 0a 2a 2a 20 20 20 76 61 72 69  s this.**   vari
11566 61 62 6c 65 20 69 73 20 75 70 64 61 74 65 64 2e  able is updated.
11567 20 54 68 65 20 76 61 72 69 61 62 6c 65 20 50 61   The variable Pa
11568 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 63  ger.dbFileSize c
11569 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62  ontains the numb
1156a 65 72 0a 2a 2a 20 20 20 6f 66 20 70 61 67 65 73  er.**   of pages
1156b 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1156c 20 66 69 6c 65 2e 20 54 68 69 73 20 6d 61 79 20   file. This may 
1156d 62 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  be different fro
1156e 6d 20 50 61 67 65 72 2e 64 62 53 69 7a 65 0a 2a  m Pager.dbSize.*
1156f 2a 20 20 20 69 66 20 73 6f 6d 65 20 70 61 67 65  *   if some page
11570 73 20 68 61 76 65 20 62 65 65 6e 20 61 70 70 65  s have been appe
11571 6e 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 61  nded to the data
11572 62 61 73 65 20 69 6d 61 67 65 20 62 75 74 20 6e  base image but n
11573 6f 74 20 79 65 74 20 77 72 69 74 74 65 6e 0a 2a  ot yet written.*
11574 2a 20 20 20 6f 75 74 20 66 72 6f 6d 20 74 68 65  *   out from the
11575 20 63 61 63 68 65 20 74 6f 20 74 68 65 20 61 63   cache to the ac
11576 74 75 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73  tual file on dis
11577 6b 2e 20 4f 72 20 69 66 20 74 68 65 20 69 6d 61  k. Or if the ima
11578 67 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 20  ge has been.**  
11579 20 74 72 75 6e 63 61 74 65 64 20 62 79 20 61 6e   truncated by an
1157a 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63   incremental-vac
1157b 75 75 6d 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54  uum operation. T
1157c 68 65 20 50 61 67 65 72 2e 64 62 4f 72 69 67 53  he Pager.dbOrigS
1157d 69 7a 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20  ize variable.** 
1157e 20 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e    contains the n
1157f 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
11580 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  n the database i
11581 6d 61 67 65 20 77 68 65 6e 20 74 68 65 20 63 75  mage when the cu
11582 72 72 65 6e 74 0a 2a 2a 20 20 20 74 72 61 6e 73  rrent.**   trans
11583 61 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65  action was opene
11584 64 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20  d. The contents 
11585 6f 66 20 61 6c 6c 20 74 68 72 65 65 20 6f 66 20  of all three of 
11586 74 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 20  these variables 
11587 69 73 0a 2a 2a 20 20 20 6f 6e 6c 79 20 67 75 61  is.**   only gua
11588 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 63 6f  ranteed to be co
11589 72 72 65 63 74 20 69 66 20 74 68 65 20 62 6f 6f  rrect if the boo
1158a 6c 65 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a  lean Pager.dbSiz
1158b 65 56 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a  eValid is true..
1158c 2a 2a 0a 2a 2a 20 20 20 54 4f 44 4f 3a 20 55 6e  **.**   TODO: Un
1158d 64 65 72 20 77 68 61 74 20 63 6f 6e 64 69 74 69  der what conditi
1158e 6f 6e 73 20 69 73 20 64 62 53 69 7a 65 56 61 6c  ons is dbSizeVal
1158f 69 64 20 73 65 74 3f 20 43 6c 65 61 72 65 64 3f  id set? Cleared?
11590 0a 2a 2a 0a 2a 2a 20 63 68 61 6e 67 65 43 6f 75  .**.** changeCou
11591 6e 74 44 6f 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54  ntDone.**.**   T
11592 68 69 73 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69  his boolean vari
11593 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20  able is used to 
11594 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
11595 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  he change-counte
11596 72 20 0a 2a 2a 20 20 20 28 74 68 65 20 34 2d 62  r .**   (the 4-b
11597 79 74 65 20 68 65 61 64 65 72 20 66 69 65 6c 64  yte header field
11598 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 20   at byte offset 
11599 32 34 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  24 of the databa
1159a 73 65 20 66 69 6c 65 29 20 69 73 20 0a 2a 2a 20  se file) is .** 
1159b 20 20 6e 6f 74 20 75 70 64 61 74 65 64 20 6d 6f    not updated mo
1159c 72 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 6e 65  re often than ne
1159d 63 65 73 73 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20  cessary. .**.** 
1159e 20 20 49 74 20 69 73 20 73 65 74 20 74 6f 20 74    It is set to t
1159f 72 75 65 20 77 68 65 6e 20 74 68 65 20 63 68 61  rue when the cha
115a0 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c  nge-counter fiel
115a1 64 20 69 73 20 75 70 64 61 74 65 64 2c 20 77 68  d is updated, wh
115a2 69 63 68 20 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e  ich .**   can on
115a3 6c 79 20 68 61 70 70 65 6e 20 69 66 20 61 6e 20  ly happen if an 
115a4 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69  exclusive lock i
115a5 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61  s held on the da
115a6 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20  tabase file..** 
115a7 20 20 49 74 20 69 73 20 63 6c 65 61 72 65 64 20    It is cleared 
115a8 28 73 65 74 20 74 6f 20 66 61 6c 73 65 29 20 77  (set to false) w
115a9 68 65 6e 65 76 65 72 20 61 6e 20 65 78 63 6c 75  henever an exclu
115aa 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a  sive lock is .**
115ab 20 20 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20     relinquished 
115ac 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
115ad 66 69 6c 65 2e 20 45 61 63 68 20 74 69 6d 65 20  file. Each time 
115ae 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
115af 20 63 6f 6d 6d 69 74 74 65 64 2c 0a 2a 2a 20 20   committed,.**  
115b0 20 54 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74   The changeCount
115b1 44 6f 6e 65 20 66 6c 61 67 20 69 73 20 69 6e 73  Done flag is ins
115b2 70 65 63 74 65 64 2e 20 49 66 20 69 74 20 69 73  pected. If it is
115b3 20 74 72 75 65 2c 20 74 68 65 20 77 6f 72 6b 20   true, the work 
115b4 6f 66 0a 2a 2a 20 20 20 75 70 64 61 74 69 6e 67  of.**   updating
115b5 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
115b6 74 65 72 20 69 73 20 6f 6d 69 74 74 65 64 20 66  ter is omitted f
115b7 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  or the current t
115b8 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  ransaction..**.*
115b9 2a 20 20 20 54 68 69 73 20 6d 65 63 68 61 6e 69  *   This mechani
115ba 73 6d 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68  sm means that wh
115bb 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78  en running in ex
115bc 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 61 20  clusive mode, a 
115bd 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 20  connection .**  
115be 20 6e 65 65 64 20 6f 6e 6c 79 20 75 70 64 61 74   need only updat
115bf 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75  e the change-cou
115c0 6e 74 65 72 20 6f 6e 63 65 2c 20 66 6f 72 20 74  nter once, for t
115c1 68 65 20 66 69 72 73 74 20 74 72 61 6e 73 61 63  he first transac
115c2 74 69 6f 6e 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74  tion.**   commit
115c3 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 62 4d 6f 64  ted..**.** dbMod
115c4 69 66 69 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68  ified.**.**   Th
115c5 65 20 64 62 4d 6f 64 69 66 69 65 64 20 66 6c 61  e dbModified fla
115c6 67 20 69 73 20 73 65 74 20 77 68 65 6e 65 76 65  g is set wheneve
115c7 72 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67  r a database pag
115c8 65 20 69 73 20 64 69 72 74 69 65 64 2e 0a 2a 2a  e is dirtied..**
115c9 20 20 20 49 74 20 69 73 20 63 6c 65 61 72 65 64     It is cleared
115ca 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65   at the end of e
115cb 61 63 68 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ach transaction.
115cc 0a 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 75  .**.**   It is u
115cd 73 65 64 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74  sed when committ
115ce 69 6e 67 20 6f 72 20 6f 74 68 65 72 77 69 73 65  ing or otherwise
115cf 20 65 6e 64 69 6e 67 20 61 20 74 72 61 6e 73 61   ending a transa
115d0 63 74 69 6f 6e 2e 20 49 66 0a 2a 2a 20 20 20 74  ction. If.**   t
115d1 68 65 20 64 62 4d 6f 64 69 66 69 65 64 20 66 6c  he dbModified fl
115d2 61 67 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e  ag is clear then
115d3 20 6c 65 73 73 20 77 6f 72 6b 20 68 61 73 20 74   less work has t
115d4 6f 20 62 65 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a  o be done..**.**
115d5 20 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 0a   journalStarted.
115d6 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 66 6c 61  **.**   This fla
115d7 67 20 69 73 20 73 65 74 20 77 68 65 6e 65 76 65  g is set wheneve
115d8 72 20 74 68 65 20 74 68 65 20 6d 61 69 6e 20 6a  r the the main j
115d9 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64  ournal is synced
115da 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 70  . .**.**   The p
115db 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 66 6c 61  oint of this fla
115dc 67 20 69 73 20 74 68 61 74 20 69 74 20 6d 75 73  g is that it mus
115dd 74 20 62 65 20 73 65 74 20 61 66 74 65 72 20 74  t be set after t
115de 68 65 20 0a 2a 2a 20 20 20 66 69 72 73 74 20 6a  he .**   first j
115df 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6e  ournal header in
115e0 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20   a journal file 
115e1 68 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20  has been synced 
115e2 74 6f 20 64 69 73 6b 2e 0a 2a 2a 20 20 20 41 66  to disk..**   Af
115e3 74 65 72 20 74 68 69 73 20 68 61 73 20 68 61 70  ter this has hap
115e4 70 65 6e 65 64 2c 20 6e 65 77 20 70 61 67 65 73  pened, new pages
115e5 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65   appended to the
115e6 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 20 20   database .**   
115e7 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 20  do not need the 
115e8 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20  PGHDR_NEED_SYNC 
115e9 66 6c 61 67 20 73 65 74 2c 20 61 73 20 74 68 65  flag set, as the
115ea 79 20 64 6f 20 6e 6f 74 20 6e 65 65 64 0a 2a 2a  y do not need.**
115eb 20 20 20 74 6f 20 77 61 69 74 20 66 6f 72 20 61     to wait for a
115ec 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20 62 65   journal sync be
115ed 66 6f 72 65 20 74 68 65 79 20 63 61 6e 20 62 65  fore they can be
115ee 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a   written out to.
115ef 2a 2a 20 20 20 74 68 65 20 64 61 74 61 62 61 73  **   the databas
115f0 65 20 66 69 6c 65 20 28 73 65 65 20 66 75 6e 63  e file (see func
115f1 74 69 6f 6e 20 70 61 67 65 72 5f 77 72 69 74 65  tion pager_write
115f2 28 29 29 2e 0a 2a 2a 20 20 20 0a 2a 2a 20 73 65  ())..**   .** se
115f3 74 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20  tMaster.**.**   
115f4 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73  This variable is
115f5 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20   used to ensure 
115f6 74 68 61 74 20 74 68 65 20 6d 61 73 74 65 72 20  that the master 
115f7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  journal file nam
115f8 65 0a 2a 2a 20 20 20 28 69 66 20 61 6e 79 29 20  e.**   (if any) 
115f9 69 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20  is only written 
115fa 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
115fb 20 66 69 6c 65 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a   file once..**.*
115fc 2a 20 20 20 57 68 65 6e 20 63 6f 6d 6d 69 74 74  *   When committ
115fd 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ing a transactio
115fe 6e 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  n, the master jo
115ff 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
11600 28 69 66 20 61 6e 79 29 0a 2a 2a 20 20 20 6d 61  (if any).**   ma
11601 79 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74  y be written int
11602 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
11603 6c 65 20 77 68 69 6c 65 20 74 68 65 20 70 61 67  le while the pag
11604 65 72 20 69 73 20 73 74 69 6c 6c 20 69 6e 0a 2a  er is still in.*
11605 2a 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 56  *   PAGER_RESERV
11606 45 44 20 73 74 61 74 65 20 28 73 65 65 20 43 6f  ED state (see Co
11607 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 66  mmitPhaseOne() f
11608 6f 72 20 74 68 65 20 61 63 74 69 6f 6e 29 2e 20  or the action). 
11609 49 74 0a 2a 2a 20 20 20 74 68 65 6e 20 61 74 74  It.**   then att
1160a 65 6d 70 74 73 20 74 6f 20 75 70 67 72 61 64 65  empts to upgrade
1160b 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65   to an exclusive
1160c 20 6c 6f 63 6b 2e 20 49 66 20 74 68 69 73 20 61   lock. If this a
1160d 74 74 65 6d 70 74 0a 2a 2a 20 20 20 66 61 69 6c  ttempt.**   fail
1160e 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 42  s, then SQLITE_B
1160f 55 53 59 20 6d 61 79 20 62 65 20 72 65 74 75 72  USY may be retur
11610 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 20  ned to the user 
11611 61 6e 64 20 74 68 65 20 75 73 65 72 0a 2a 2a 20  and the user.** 
11612 20 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f    may attempt to
11613 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e   commit the tran
11614 73 61 63 74 69 6f 6e 20 61 67 61 69 6e 20 6c 61  saction again la
11615 74 65 72 20 28 63 61 6c 6c 69 6e 67 0a 2a 2a 20  ter (calling.** 
11616 20 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65    CommitPhaseOne
11617 28 29 20 61 67 61 69 6e 29 2e 20 54 68 69 73 20  () again). This 
11618 66 6c 61 67 20 69 73 20 75 73 65 64 20 74 6f 20  flag is used to 
11619 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20  ensure that the 
1161a 0a 2a 2a 20 20 20 6d 61 73 74 65 72 20 6a 6f 75  .**   master jou
1161b 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20 6f 6e 6c  rnal name is onl
1161c 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  y written to the
1161d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68   journal file th
1161e 65 20 66 69 72 73 74 0a 2a 2a 20 20 20 74 69 6d  e first.**   tim
1161f 65 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  e CommitPhaseOne
11620 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  () is called..**
11621 0a 2a 2a 20 64 6f 4e 6f 74 53 79 6e 63 0a 2a 2a  .** doNotSync.**
11622 0a 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69 61  .**   This varia
11623 62 6c 65 20 69 73 20 73 65 74 20 61 6e 64 20 63  ble is set and c
11624 6c 65 61 72 65 64 20 62 79 20 73 71 6c 69 74 65  leared by sqlite
11625 33 50 61 67 65 72 57 72 69 74 65 28 29 2e 0a 2a  3PagerWrite()..*
11626 2a 0a 2a 2a 20 6e 65 65 64 53 79 6e 63 0a 2a 2a  *.** needSync.**
11627 0a 2a 2a 20 20 20 54 4f 44 4f 3a 20 49 74 20 6d  .**   TODO: It m
11628 69 67 68 74 20 62 65 20 65 61 73 69 65 72 20 74  ight be easier t
11629 6f 20 73 65 74 20 74 68 69 73 20 76 61 72 69 61  o set this varia
1162a 62 6c 65 20 69 6e 20 77 72 69 74 65 4a 6f 75 72  ble in writeJour
1162b 6e 61 6c 48 64 72 28 29 0a 2a 2a 20 20 20 61 6e  nalHdr().**   an
1162c 64 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75  d writeMasterJou
1162d 72 6e 61 6c 28 29 20 6f 6e 6c 79 2e 20 43 68 61  rnal() only. Cha
1162e 6e 67 65 20 69 74 73 20 6d 65 61 6e 69 6e 67 20  nge its meaning 
1162f 74 6f 20 22 75 6e 73 79 6e 63 65 64 20 64 61 74  to "unsynced dat
11630 61 0a 2a 2a 20 20 20 68 61 73 20 62 65 65 6e 20  a.**   has been 
11631 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
11632 6f 75 72 6e 61 6c 22 2e 0a 2a 2a 0a 2a 2a 20 73  ournal"..**.** s
11633 75 62 6a 49 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a  ubjInMemory.**.*
11634 2a 20 20 20 54 68 69 73 20 69 73 20 61 20 62 6f  *   This is a bo
11635 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 2e 20  olean variable. 
11636 49 66 20 74 72 75 65 2c 20 74 68 65 6e 20 61 6e  If true, then an
11637 79 20 72 65 71 75 69 72 65 64 20 73 75 62 2d 6a  y required sub-j
11638 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 69 73 20 6f  ournal.**   is o
11639 70 65 6e 65 64 20 61 73 20 61 6e 20 69 6e 2d 6d  pened as an in-m
1163a 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69  emory journal fi
1163b 6c 65 2e 20 49 66 20 66 61 6c 73 65 2c 20 74 68  le. If false, th
1163c 65 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20  en in-memory.** 
1163d 20 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 61    sub-journals a
1163e 72 65 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72  re only used for
1163f 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72   in-memory pager
11640 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63   files..*/.struc
11641 74 20 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69  t Pager {.  sqli
11642 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20  te3_vfs *pVfs;  
11643 20 20 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75          /* OS fu
11644 6e 63 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66  nctions to use f
11645 6f 72 20 49 4f 20 2a 2f 0a 20 20 75 38 20 65 78  or IO */.  u8 ex
11646 63 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20 20 20  clusiveMode;    
11647 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61         /* Boolea
11648 6e 2e 20 54 72 75 65 20 69 66 20 6c 6f 63 6b 69  n. True if locki
11649 6e 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49  ng_mode==EXCLUSI
1164a 56 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e  VE */.  u8 journ
1164b 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20  alMode;         
1164c 20 20 20 20 2f 2a 20 4f 6e 20 6f 66 20 74 68 65      /* On of the
1164d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
1164e 44 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20  DE_* values */. 
1164f 20 75 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20   u8 useJournal; 
11650 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11651 55 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a  Use a rollback j
11652 6f 75 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66  ournal on this f
11653 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65  ile */.  u8 noRe
11654 61 64 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20  adlock;         
11655 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62       /* Do not b
11656 6f 74 68 65 72 20 74 6f 20 6f 62 74 61 69 6e 20  other to obtain 
11657 72 65 61 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75  readlocks */.  u
11658 38 20 6e 6f 53 79 6e 63 3b 20 20 20 20 20 20 20  8 noSync;       
11659 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f             /* Do
1165a 20 6e 6f 74 20 73 79 6e 63 20 74 68 65 20 6a 6f   not sync the jo
1165b 75 72 6e 61 6c 20 69 66 20 74 72 75 65 20 2a 2f  urnal if true */
1165c 0a 20 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b 20  .  u8 fullSync; 
1165d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1165e 2a 20 44 6f 20 65 78 74 72 61 20 73 79 6e 63 73  * Do extra syncs
1165f 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
11660 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 20 2a  for robustness *
11661 2f 0a 20 20 75 38 20 73 79 6e 63 5f 66 6c 61 67  /.  u8 sync_flag
11662 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
11663 2f 2a 20 4f 6e 65 20 6f 66 20 53 59 4e 43 5f 4e  /* One of SYNC_N
11664 4f 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55  ORMAL or SYNC_FU
11665 4c 4c 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 46  LL */.  u8 tempF
11666 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ile;            
11667 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65      /* zFilename
11668 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20   is a temporary 
11669 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 72 65 61  file */.  u8 rea
1166a 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20  dOnly;          
1166b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
1166c 72 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61  r a read-only da
1166d 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 6d  tabase */.  u8 m
1166e 65 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20  emDb;           
1166f 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
11670 74 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66  to inhibit all f
11671 69 6c 65 20 49 2f 4f 20 2a 2f 0a 0a 20 20 2f 2a  ile I/O */..  /*
11672 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   The following b
11673 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68  lock contains th
11674 6f 73 65 20 63 6c 61 73 73 20 6d 65 6d 62 65 72  ose class member
11675 73 20 74 68 61 74 20 61 72 65 20 64 79 6e 61 6d  s that are dynam
11676 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 6d 6f 64 69  ically.  ** modi
11677 66 69 65 64 20 64 75 72 69 6e 67 20 6e 6f 72 6d  fied during norm
11678 61 6c 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 54  al operations. T
11679 68 65 20 6f 74 68 65 72 20 76 61 72 69 61 62 6c  he other variabl
1167a 65 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63  es in this struc
1167b 74 75 72 65 0a 20 20 2a 2a 20 61 72 65 20 65 69  ture.  ** are ei
1167c 74 68 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68  ther constant th
1167d 72 6f 75 67 68 6f 75 74 20 74 68 65 20 6c 69 66  roughout the lif
1167e 65 74 69 6d 65 20 6f 66 20 74 68 65 20 70 61 67  etime of the pag
1167f 65 72 2c 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a  er, or else.  **
11680 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 63   used to store c
11681 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 61 72  onfiguration par
11682 61 6d 65 74 65 72 73 20 74 68 61 74 20 61 66 66  ameters that aff
11683 65 63 74 20 74 68 65 20 77 61 79 20 74 68 65 20  ect the way the 
11684 70 61 67 65 72 20 0a 20 20 2a 2a 20 6f 70 65 72  pager .  ** oper
11685 61 74 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ates..  **.  ** 
11686 54 68 65 20 27 73 74 61 74 65 27 20 76 61 72 69  The 'state' vari
11687 61 62 6c 65 20 69 73 20 64 65 73 63 72 69 62 65  able is describe
11688 64 20 69 6e 20 6d 6f 72 65 20 64 65 74 61 69 6c  d in more detail
11689 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 0a   along with the.
1168a 20 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e    ** description
1168b 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20  s of the values 
1168c 69 74 20 6d 61 79 20 74 61 6b 65 20 2d 20 50 41  it may take - PA
1168d 47 45 52 5f 55 4e 4c 4f 43 4b 20 65 74 63 2e 20  GER_UNLOCK etc. 
1168e 4d 61 6e 79 20 6f 66 20 74 68 65 0a 20 20 2a 2a  Many of the.  **
1168f 20 6f 74 68 65 72 20 76 61 72 69 61 62 6c 65 73   other variables
11690 20 69 6e 20 74 68 69 73 20 62 6c 6f 63 6b 20 61   in this block a
11691 72 65 20 64 65 73 63 72 69 62 65 64 20 69 6e 20  re described in 
11692 74 68 65 20 63 6f 6d 6d 65 6e 74 20 64 69 72 65  the comment dire
11693 63 74 6c 79 20 0a 20 20 2a 2a 20 61 62 6f 76 65  ctly .  ** above
11694 20 74 68 69 73 20 63 6c 61 73 73 20 64 65 66 69   this class defi
11695 6e 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 75  nition..  */.  u
11696 38 20 73 74 61 74 65 3b 20 20 20 20 20 20 20 20  8 state;        
11697 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41             /* PA
11698 47 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41  GER_UNLOCK, _SHA
11699 52 45 44 2c 20 5f 52 45 53 45 52 56 45 44 2c 20  RED, _RESERVED, 
1169a 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 64 62 4d  etc. */.  u8 dbM
1169b 6f 64 69 66 69 65 64 3b 20 20 20 20 20 20 20 20  odified;        
1169c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1169d 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 63   there are any c
1169e 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 44 62  hanges to the Db
1169f 20 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53 79 6e   */.  u8 needSyn
116a0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
116a1 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20    /* True if an 
116a2 66 73 79 6e 63 28 29 20 69 73 20 6e 65 65 64 65  fsync() is neede
116a3 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  d on the journal
116a4 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c   */.  u8 journal
116a5 53 74 61 72 74 65 64 3b 20 20 20 20 20 20 20 20  Started;        
116a6 20 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65 61    /* True if hea
116a7 64 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 69  der of journal i
116a8 73 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75 38  s synced */.  u8
116a9 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65   changeCountDone
116aa 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74  ;         /* Set
116ab 20 61 66 74 65 72 20 69 6e 63 72 65 6d 65 6e 74   after increment
116ac 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63  ing the change-c
116ad 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 73  ounter */.  u8 s
116ae 65 74 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20  etMaster;       
116af 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
116b0 69 66 20 61 20 6d 2d 6a 20 6e 61 6d 65 20 68 61  if a m-j name ha
116b1 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  s been written t
116b2 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75 38 20 64  o jrnl */.  u8 d
116b3 6f 4e 6f 74 53 79 6e 63 3b 20 20 20 20 20 20 20  oNotSync;       
116b4 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65          /* Boole
116b5 61 6e 2e 20 57 68 69 6c 65 20 74 72 75 65 2c 20  an. While true, 
116b6 64 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68 65  do not spill the
116b7 20 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20 64   cache */.  u8 d
116b8 62 53 69 7a 65 56 61 6c 69 64 3b 20 20 20 20 20  bSizeValid;     
116b9 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 77          /* Set w
116ba 68 65 6e 20 64 62 53 69 7a 65 20 69 73 20 63 6f  hen dbSize is co
116bb 72 72 65 63 74 20 2a 2f 0a 20 20 75 38 20 73 75  rrect */.  u8 su
116bc 62 6a 49 6e 4d 65 6d 6f 72 79 3b 20 20 20 20 20  bjInMemory;     
116bd 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
116be 6f 20 75 73 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  o use in-memory 
116bf 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a  sub-journals */.
116c0 20 20 50 67 6e 6f 20 64 62 53 69 7a 65 3b 20 20    Pgno dbSize;  
116c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
116c2 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
116c3 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
116c4 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 4f 72 69   */.  Pgno dbOri
116c5 67 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  gSize;          
116c6 20 20 2f 2a 20 64 62 53 69 7a 65 20 62 65 66 6f    /* dbSize befo
116c7 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  re the current t
116c8 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
116c9 50 67 6e 6f 20 64 62 46 69 6c 65 53 69 7a 65 3b  Pgno dbFileSize;
116ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
116cb 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
116cc 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
116cd 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72  ile */.  int err
116ce 43 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Code;           
116cf 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 73       /* One of s
116d0 65 76 65 72 61 6c 20 6b 69 6e 64 73 20 6f 66 20  everal kinds of 
116d1 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20  errors */.  int 
116d2 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20  nRec;           
116d3 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73          /* Pages
116d4 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 73 69 6e 63   journalled sinc
116d5 65 20 6c 61 73 74 20 6a 2d 68 65 61 64 65 72 20  e last j-header 
116d6 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 75 33 32  written */.  u32
116d7 20 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20   cksumInit;     
116d8 20 20 20 20 20 20 20 20 20 2f 2a 20 51 75 61 73           /* Quas
116d9 69 2d 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61  i-random value a
116da 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 63 68  dded to every ch
116db 65 63 6b 73 75 6d 20 2a 2f 0a 20 20 75 33 32 20  ecksum */.  u32 
116dc 6e 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 20  nSubRec;        
116dd 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
116de 72 20 6f 66 20 72 65 63 6f 72 64 73 20 77 72 69  r of records wri
116df 74 74 65 6e 20 74 6f 20 73 75 62 2d 6a 6f 75 72  tten to sub-jour
116e0 6e 61 6c 20 2a 2f 0a 20 20 42 69 74 76 65 63 20  nal */.  Bitvec 
116e1 2a 70 49 6e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20  *pInJournal;    
116e2 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69 74 20       /* One bit 
116e3 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 69 6e  for each page in
116e4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
116e5 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  le */.  sqlite3_
116e6 66 69 6c 65 20 2a 66 64 3b 20 20 20 20 20 20 20  file *fd;       
116e7 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63      /* File desc
116e8 72 69 70 74 6f 72 20 66 6f 72 20 64 61 74 61 62  riptor for datab
116e9 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ase */.  sqlite3
116ea 5f 66 69 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20  _file *jfd;     
116eb 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73       /* File des
116ec 63 72 69 70 74 6f 72 20 66 6f 72 20 6d 61 69 6e  criptor for main
116ed 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 73 71   journal */.  sq
116ee 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 6a 66 64  lite3_file *sjfd
116ef 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c  ;         /* Fil
116f0 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72  e descriptor for
116f1 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   sub-journal */.
116f2 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 4f 66 66    i64 journalOff
116f3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
116f4 20 43 75 72 72 65 6e 74 20 77 72 69 74 65 20 6f   Current write o
116f5 66 66 73 65 74 20 69 6e 20 74 68 65 20 6a 6f 75  ffset in the jou
116f6 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69  rnal file */.  i
116f7 36 34 20 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 20  64 journalHdr;  
116f8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
116f9 74 65 20 6f 66 66 73 65 74 20 74 6f 20 70 72 65  te offset to pre
116fa 76 69 6f 75 73 20 6a 6f 75 72 6e 61 6c 20 68 65  vious journal he
116fb 61 64 65 72 20 2a 2f 0a 20 20 50 61 67 65 72 53  ader */.  PagerS
116fc 61 76 65 70 6f 69 6e 74 20 2a 61 53 61 76 65 70  avepoint *aSavep
116fd 6f 69 6e 74 3b 20 2f 2a 20 41 72 72 61 79 20 6f  oint; /* Array o
116fe 66 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69  f active savepoi
116ff 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 61  nts */.  int nSa
11700 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20  vepoint;        
11701 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
11702 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 53  f elements in aS
11703 61 76 65 70 6f 69 6e 74 5b 5d 20 2a 2f 0a 20 20  avepoint[] */.  
11704 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b  char dbFileVers[
11705 31 36 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  16];        /* C
11706 68 61 6e 67 65 73 20 77 68 65 6e 65 76 65 72 20  hanges whenever 
11707 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 68  database file ch
11708 61 6e 67 65 73 20 2a 2f 0a 20 20 75 33 32 20 73  anges */.  u32 s
11709 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20  ectorSize;      
1170a 20 20 20 20 20 20 20 2f 2a 20 41 73 73 75 6d 65         /* Assume
1170b 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 64 75  d sector size du
1170c 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f  ring rollback */
1170d 0a 0a 20 20 75 31 36 20 6e 45 78 74 72 61 3b 20  ..  u16 nExtra; 
1170e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1170f 2f 2a 20 41 64 64 20 74 68 69 73 20 6d 61 6e 79  /* Add this many
11710 20 62 79 74 65 73 20 74 6f 20 65 61 63 68 20 69   bytes to each i
11711 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f  n-memory page */
11712 0a 20 20 69 31 36 20 6e 52 65 73 65 72 76 65 3b  .  i16 nReserve;
11713 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11714 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 73  * Number of unus
11715 65 64 20 62 79 74 65 73 20 61 74 20 65 6e 64 20  ed bytes at end 
11716 6f 66 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  of each page */.
11717 20 20 75 33 32 20 76 66 73 46 6c 61 67 73 3b 20    u32 vfsFlags; 
11718 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11719 20 46 6c 61 67 73 20 66 6f 72 20 73 71 6c 69 74   Flags for sqlit
1171a 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a  e3_vfs.xOpen() *
1171b 2f 0a 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65  /.  int pageSize
1171c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1171d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1171e 65 73 20 69 6e 20 61 20 70 61 67 65 20 2a 2f 0a  es in a page */.
1171f 20 20 50 67 6e 6f 20 6d 78 50 67 6e 6f 3b 20 20    Pgno mxPgno;  
11720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11721 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64   Maximum allowed
11722 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
11723 61 62 61 73 65 20 2a 2f 0a 20 20 63 68 61 72 20  abase */.  char 
11724 2a 7a 46 69 6c 65 6e 61 6d 65 3b 20 20 20 20 20  *zFilename;     
11725 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
11726 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
11727 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ile */.  char *z
11728 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20  Journal;        
11729 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
1172a 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1172b 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73   */.  int (*xBus
1172c 79 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 2a 29  yHandler)(void*)
1172d 3b 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f  ; /* Function to
1172e 20 63 61 6c 6c 20 77 68 65 6e 20 62 75 73 79 20   call when busy 
1172f 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79  */.  void *pBusy
11730 48 61 6e 64 6c 65 72 41 72 67 3b 20 20 20 20 20  HandlerArg;     
11731 20 2f 2a 20 43 6f 6e 74 65 78 74 20 61 72 67 75   /* Context argu
11732 6d 65 6e 74 20 66 6f 72 20 78 42 75 73 79 48 61  ment for xBusyHa
11733 6e 64 6c 65 72 20 2a 2f 0a 23 69 66 64 65 66 20  ndler */.#ifdef 
11734 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 6e  SQLITE_TEST.  in
11735 74 20 6e 48 69 74 2c 20 6e 4d 69 73 73 3b 20 20  t nHit, nMiss;  
11736 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63            /* Cac
11737 68 65 20 68 69 74 73 20 61 6e 64 20 6d 69 73 73  he hits and miss
11738 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ing */.  int nRe
11739 61 64 2c 20 6e 57 72 69 74 65 3b 20 20 20 20 20  ad, nWrite;     
1173a 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1173b 20 70 61 67 65 73 20 72 65 61 64 2f 77 72 69 74   pages read/writ
1173c 74 65 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ten */.#endif.  
1173d 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 65 72  void (*xReiniter
1173e 29 28 44 62 50 61 67 65 2a 29 3b 20 2f 2a 20 43  )(DbPage*); /* C
1173f 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
11740 20 77 68 65 6e 20 72 65 6c 6f 61 64 69 6e 67 20   when reloading 
11741 70 61 67 65 73 20 2a 2f 0a 23 69 66 64 65 66 20  pages */.#ifdef 
11742 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
11743 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64 65  .  void *(*xCode
11744 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50  c)(void*,void*,P
11745 67 6e 6f 2c 69 6e 74 29 3b 20 2f 2a 20 52 6f 75  gno,int); /* Rou
11746 74 69 6e 65 20 66 6f 72 20 65 6e 2f 64 65 63 6f  tine for en/deco
11747 64 69 6e 67 20 64 61 74 61 20 2a 2f 0a 20 20 76  ding data */.  v
11748 6f 69 64 20 28 2a 78 43 6f 64 65 63 53 69 7a 65  oid (*xCodecSize
11749 43 68 6e 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  Chng)(void*,int,
1174a 69 6e 74 29 3b 20 2f 2a 20 4e 6f 74 69 66 79 20  int); /* Notify 
1174b 6f 66 20 70 61 67 65 20 73 69 7a 65 20 63 68 61  of page size cha
1174c 6e 67 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28  nges */.  void (
1174d 2a 78 43 6f 64 65 63 46 72 65 65 29 28 76 6f 69  *xCodecFree)(voi
1174e 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  d*);            
1174f 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66   /* Destructor f
11750 6f 72 20 74 68 65 20 63 6f 64 65 63 20 2a 2f 0a  or the codec */.
11751 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 3b 20    void *pCodec; 
11752 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11753 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   First argument 
11754 74 6f 20 78 43 6f 64 65 63 2e 2e 2e 20 6d 65 74  to xCodec... met
11755 68 6f 64 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  hods */.#endif. 
11756 20 63 68 61 72 20 2a 70 54 6d 70 53 70 61 63 65   char *pTmpSpace
11757 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
11758 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 20 62  Pager.pageSize b
11759 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f  ytes of space fo
1175a 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 20 20 69  r tmp use */.  i
1175b 36 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  64 journalSizeLi
1175c 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 69  mit;       /* Si
1175d 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 70 65 72  ze limit for per
1175e 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  sistent journal 
1175f 66 69 6c 65 73 20 2a 2f 0a 20 20 50 43 61 63 68  files */.  PCach
11760 65 20 2a 70 50 43 61 63 68 65 3b 20 20 20 20 20  e *pPCache;     
11761 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
11762 72 20 74 6f 20 70 61 67 65 20 63 61 63 68 65 20  r to page cache 
11763 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69  object */.  sqli
11764 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63  te3_backup *pBac
11765 6b 75 70 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74  kup;    /* Point
11766 65 72 20 74 6f 20 6c 69 73 74 20 6f 66 20 6f 6e  er to list of on
11767 67 6f 69 6e 67 20 62 61 63 6b 75 70 20 70 72 6f  going backup pro
11768 63 65 73 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  cesses */.};../*
11769 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
1176a 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  g global variabl
1176b 65 73 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73  es hold counters
1176c 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73   used for.** tes
1176d 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e  ting purposes on
1176e 6c 79 2e 20 20 54 68 65 73 65 20 76 61 72 69 61  ly.  These varia
1176f 62 6c 65 73 20 64 6f 20 6e 6f 74 20 65 78 69 73  bles do not exis
11770 74 20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65  t in.** a non-te
11771 73 74 69 6e 67 20 62 75 69 6c 64 2e 20 20 54 68  sting build.  Th
11772 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 61 72  ese variables ar
11773 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66  e not thread-saf
11774 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
11775 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
11776 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
11777 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75  pager_readdb_cou
11778 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75  nt = 0;    /* Nu
11779 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67  mber of full pag
1177a 65 73 20 72 65 61 64 20 66 72 6f 6d 20 44 42 20  es read from DB 
1177b 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1177c 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f  t sqlite3_pager_
1177d 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20  writedb_count = 
1177e 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  0;   /* Number o
1177f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 77 72 69  f full pages wri
11780 74 74 65 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 51  tten to DB */.SQ
11781 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
11782 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65  ite3_pager_write
11783 6a 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20  j_count = 0;    
11784 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
11785 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f  es written to jo
11786 75 72 6e 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e  urnal */.# defin
11787 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 20  e PAGER_INCR(v) 
11788 20 76 2b 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66   v++.#else.# def
11789 69 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76  ine PAGER_INCR(v
1178a 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a  ).#endif..../*.*
1178b 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20  * Journal files 
1178c 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 66  begin with the f
1178d 6f 6c 6c 6f 77 69 6e 67 20 6d 61 67 69 63 20 73  ollowing magic s
1178e 74 72 69 6e 67 2e 20 20 54 68 65 20 64 61 74 61  tring.  The data
1178f 0a 2a 2a 20 77 61 73 20 6f 62 74 61 69 6e 65 64  .** was obtained
11790 20 66 72 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f   from /dev/rando
11791 6d 2e 20 20 49 74 20 69 73 20 75 73 65 64 20 6f  m.  It is used o
11792 6e 6c 79 20 61 73 20 61 20 73 61 6e 69 74 79 20  nly as a sanity 
11793 63 68 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e  check..**.** Sin
11794 63 65 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30  ce version 2.8.0
11795 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f  , the journal fo
11796 72 6d 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 64  rmat contains ad
11797 64 69 74 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a  ditional sanity.
11798 2a 2a 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f  ** checking info
11799 72 6d 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65  rmation.  If the
1179a 20 70 6f 77 65 72 20 66 61 69 6c 73 20 77 68 69   power fails whi
1179b 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  le the journal i
1179c 73 20 62 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74  s being.** writt
1179d 65 6e 2c 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20  en, semi-random 
1179e 67 61 72 62 61 67 65 20 64 61 74 61 20 6d 69 67  garbage data mig
1179f 68 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 65  ht appear in the
117a0 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65   journal.** file
117a1 20 61 66 74 65 72 20 70 6f 77 65 72 20 69 73 20   after power is 
117a2 72 65 73 74 6f 72 65 64 2e 20 20 49 66 20 61 6e  restored.  If an
117a3 20 61 74 74 65 6d 70 74 20 69 73 20 74 68 65 6e   attempt is then
117a4 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c   made.** to roll
117a5 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63   the journal bac
117a6 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  k, the database 
117a7 63 6f 75 6c 64 20 62 65 20 63 6f 72 72 75 70 74  could be corrupt
117a8 65 64 2e 20 20 54 68 65 20 61 64 64 69 74 69 6f  ed.  The additio
117a9 6e 61 6c 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68  nal.** sanity ch
117aa 65 63 6b 69 6e 67 20 64 61 74 61 20 69 73 20 61  ecking data is a
117ab 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 64 69 73  n attempt to dis
117ac 63 6f 76 65 72 20 74 68 65 20 67 61 72 62 61 67  cover the garbag
117ad 65 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72  e in the.** jour
117ae 6e 61 6c 20 61 6e 64 20 69 67 6e 6f 72 65 20 69  nal and ignore i
117af 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e  t..**.** The san
117b0 69 74 79 20 63 68 65 63 6b 69 6e 67 20 69 6e 66  ity checking inf
117b1 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  ormation for the
117b2 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72   new journal for
117b3 6d 61 74 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20  mat consists.** 
117b4 6f 66 20 61 20 33 32 2d 62 69 74 20 63 68 65 63  of a 32-bit chec
117b5 6b 73 75 6d 20 6f 6e 20 65 61 63 68 20 70 61 67  ksum on each pag
117b6 65 20 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20  e of data.  The 
117b7 63 68 65 63 6b 73 75 6d 20 63 6f 76 65 72 73 20  checksum covers 
117b8 62 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 67 65  both.** the page
117b9 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20   number and the 
117ba 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
117bb 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 66   bytes of data f
117bc 6f 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20  or the page..** 
117bd 54 68 69 73 20 63 6b 73 75 6d 20 69 73 20 69 6e  This cksum is in
117be 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 33  itialized to a 3
117bf 32 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c  2-bit random val
117c0 75 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ue that appears 
117c1 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61  in the.** journa
117c2 6c 20 66 69 6c 65 20 72 69 67 68 74 20 61 66 74  l file right aft
117c3 65 72 20 74 68 65 20 68 65 61 64 65 72 2e 20 20  er the header.  
117c4 54 68 65 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69  The random initi
117c5 61 6c 69 7a 65 72 20 69 73 20 69 6d 70 6f 72 74  alizer is import
117c6 61 6e 74 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20  ant,.** because 
117c7 67 61 72 62 61 67 65 20 64 61 74 61 20 74 68 61  garbage data tha
117c8 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65  t appears at the
117c9 20 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61   end of a journa
117ca 6c 20 69 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64  l is likely.** d
117cb 61 74 61 20 74 68 61 74 20 77 61 73 20 6f 6e 63  ata that was onc
117cc 65 20 69 6e 20 6f 74 68 65 72 20 66 69 6c 65 73  e in other files
117cd 20 74 68 61 74 20 68 61 76 65 20 6e 6f 77 20 62   that have now b
117ce 65 65 6e 20 64 65 6c 65 74 65 64 2e 20 20 49 66  een deleted.  If
117cf 20 74 68 65 0a 2a 2a 20 67 61 72 62 61 67 65 20   the.** garbage 
117d0 64 61 74 61 20 63 61 6d 65 20 66 72 6f 6d 20 61  data came from a
117d1 6e 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e  n obsolete journ
117d2 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 63 68 65  al file, the che
117d3 63 6b 73 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20  cksums might.** 
117d4 62 65 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74  be correct.  But
117d5 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67   by initializing
117d6 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 74 6f   the checksum to
117d7 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68   random value wh
117d8 69 63 68 0a 2a 2a 20 69 73 20 64 69 66 66 65 72  ich.** is differ
117d9 65 6e 74 20 66 6f 72 20 65 76 65 72 79 20 6a 6f  ent for every jo
117da 75 72 6e 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69  urnal, we minimi
117db 7a 65 20 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f  ze that risk..*/
117dc 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e  .static const un
117dd 73 69 67 6e 65 64 20 63 68 61 72 20 61 4a 6f 75  signed char aJou
117de 72 6e 61 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a  rnalMagic[] = {.
117df 20 20 30 78 64 39 2c 20 30 78 64 35 2c 20 30 78    0xd9, 0xd5, 0x
117e0 30 35 2c 20 30 78 66 39 2c 20 30 78 32 30 2c 20  05, 0xf9, 0x20, 
117e1 30 78 61 31 2c 20 30 78 36 33 2c 20 30 78 64 37  0xa1, 0x63, 0xd7
117e2 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ,.};../*.** The 
117e3 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 66 20 65  size of the of e
117e4 61 63 68 20 70 61 67 65 20 72 65 63 6f 72 64 20  ach page record 
117e5 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  in the journal i
117e6 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68  s given by.** th
117e7 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
117e8 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f  o..*/.#define JO
117e9 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67  URNAL_PG_SZ(pPag
117ea 65 72 29 20 20 28 28 70 50 61 67 65 72 2d 3e 70  er)  ((pPager->p
117eb 61 67 65 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f  ageSize) + 8)../
117ec 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c  *.** The journal
117ed 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 6f 72   header size for
117ee 20 74 68 69 73 20 70 61 67 65 72 2e 20 54 68 69   this pager. Thi
117ef 73 20 69 73 20 75 73 75 61 6c 6c 79 20 74 68 65  s is usually the
117f0 20 73 61 6d 65 20 0a 2a 2a 20 73 69 7a 65 20 61   same .** size a
117f1 73 20 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20  s a single disk 
117f2 73 65 63 74 6f 72 2e 20 53 65 65 20 61 6c 73 6f  sector. See also
117f3 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 29   setSectorSize()
117f4 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55  ..*/.#define JOU
117f5 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
117f6 65 72 29 20 28 70 50 61 67 65 72 2d 3e 73 65 63  er) (pPager->sec
117f7 74 6f 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20  torSize)../*.** 
117f8 54 68 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20  The macro MEMDB 
117f9 69 73 20 74 72 75 65 20 69 66 20 77 65 20 61 72  is true if we ar
117fa 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61  e dealing with a
117fb 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
117fc 62 61 73 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74  base..** We do t
117fd 68 69 73 20 61 73 20 61 20 6d 61 63 72 6f 20 73  his as a macro s
117fe 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 53 51  o that if the SQ
117ff 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59  LITE_OMIT_MEMORY
11800 44 42 20 6d 61 63 72 6f 20 69 73 20 73 65 74 2c  DB macro is set,
11801 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66  .** the value of
11802 20 4d 45 4d 44 42 20 77 69 6c 6c 20 62 65 20 61   MEMDB will be a
11803 20 63 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74 68   constant and th
11804 65 20 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20  e compiler will 
11805 6f 70 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20  optimize.** out 
11806 63 6f 64 65 20 74 68 61 74 20 77 6f 75 6c 64 20  code that would 
11807 6e 65 76 65 72 20 65 78 65 63 75 74 65 2e 0a 2a  never execute..*
11808 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
11809 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20  OMIT_MEMORYDB.# 
1180a 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23  define MEMDB 0.#
1180b 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 4d 45  else.# define ME
1180c 4d 44 42 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44  MDB pPager->memD
1180d 62 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  b.#endif../*.** 
1180e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61  The maximum lega
1180f 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73  l page number is
11810 20 28 32 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a   (2^31 - 1)..*/.
11811 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 41  #define PAGER_MA
11812 58 5f 50 47 4e 4f 20 32 31 34 37 34 38 33 36 34  X_PGNO 214748364
11813 37 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  7..#ifndef NDEBU
11814 47 20 0a 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a  G ./*.** Usage:.
11815 2a 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20  **.**   assert( 
11816 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61  assert_pager_sta
11817 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 2a 2f  te(pPager) );.*/
11818 0a 73 74 61 74 69 63 20 69 6e 74 20 61 73 73 65  .static int asse
11819 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 50  rt_pager_state(P
1181a 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a  ager *pPager){..
1181b 20 20 2f 2a 20 41 20 74 65 6d 70 2d 66 69 6c 65    /* A temp-file
1181c 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 50 41   is always in PA
1181d 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 72  GER_EXCLUSIVE or
1181e 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 73 74   PAGER_SYNCED st
1181f 61 74 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ate. */.  assert
11820 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  ( pPager->tempFi
11821 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d  le==0 || pPager-
11822 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58  >state>=PAGER_EX
11823 43 4c 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a  CLUSIVE );..  /*
11824 20 54 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74   The changeCount
11825 44 6f 6e 65 20 66 6c 61 67 20 69 73 20 61 6c 77  Done flag is alw
11826 61 79 73 20 73 65 74 20 66 6f 72 20 74 65 6d 70  ays set for temp
11827 2d 66 69 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65  -files */.  asse
11828 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70  rt( pPager->temp
11829 46 69 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65  File==0 || pPage
1182a 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
1182b 6e 65 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ne );..  return 
1182c 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  1;.}.#endif../*.
1182d 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
1182e 66 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72  f it is necessar
1182f 79 20 74 6f 20 77 72 69 74 65 20 70 61 67 65 20  y to write page 
11830 2a 70 50 67 20 69 6e 74 6f 20 74 68 65 20 73 75  *pPg into the su
11831 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20  b-journal..** A 
11832 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65  page needs to be
11833 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
11834 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66  e sub-journal if
11835 20 74 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e   there exists on
11836 65 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65  e.** or more ope
11837 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 66 6f 72  n savepoints for
11838 20 77 68 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20   which:.**.**   
11839 2a 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65  * The page-numbe
1183a 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f  r is less than o
1183b 72 20 65 71 75 61 6c 20 74 6f 20 50 61 67 65 72  r equal to Pager
1183c 53 61 76 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c  Savepoint.nOrig,
1183d 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20   and.**   * The 
1183e 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  bit correspondin
1183f 67 20 74 6f 20 74 68 65 20 70 61 67 65 2d 6e 75  g to the page-nu
11840 6d 62 65 72 20 69 73 20 6e 6f 74 20 73 65 74 20  mber is not set 
11841 69 6e 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53  in.**     PagerS
11842 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65  avepoint.pInSave
11843 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  point..*/.static
11844 20 69 6e 74 20 73 75 62 6a 52 65 71 75 69 72 65   int subjRequire
11845 73 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67  sPage(PgHdr *pPg
11846 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d  ){.  Pgno pgno =
11847 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61   pPg->pgno;.  Pa
11848 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
11849 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74  g->pPager;.  int
1184a 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
1184b 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f  <pPager->nSavepo
1184c 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50  int; i++){.    P
1184d 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70  agerSavepoint *p
1184e 20 3d 20 26 70 50 61 67 65 72 2d 3e 61 53 61 76   = &pPager->aSav
1184f 65 70 6f 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69  epoint[i];.    i
11850 66 28 20 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e  f( p->nOrig>=pgn
11851 6f 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42  o && 0==sqlite3B
11852 69 74 76 65 63 54 65 73 74 28 70 2d 3e 70 49 6e  itvecTest(p->pIn
11853 53 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29  Savepoint, pgno)
11854 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
11855 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
11856 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
11857 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
11858 66 20 74 68 65 20 70 61 67 65 20 69 73 20 61 6c  f the page is al
11859 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75  ready in the jou
1185a 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  rnal file..*/.st
1185b 61 74 69 63 20 69 6e 74 20 70 61 67 65 49 6e 4a  atic int pageInJ
1185c 6f 75 72 6e 61 6c 28 50 67 48 64 72 20 2a 70 50  ournal(PgHdr *pP
1185d 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  g){.  return sql
1185e 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
1185f 50 67 2d 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a  Pg->pPager->pInJ
11860 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e  ournal, pPg->pgn
11861 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  o);.}../*.** Rea
11862 64 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  d a 32-bit integ
11863 65 72 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65  er from the give
11864 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
11865 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20 69 6e  r.  Store the in
11866 74 65 67 65 72 0a 2a 2a 20 74 68 61 74 20 69 73  teger.** that is
11867 20 72 65 61 64 20 69 6e 20 2a 70 52 65 73 2e 20   read in *pRes. 
11868 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
11869 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20  K if everything 
1186a 77 6f 72 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a  worked, or an.**
1186b 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73   error code is s
1186c 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72  omething goes wr
1186d 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76  ong..**.** All v
1186e 61 6c 75 65 73 20 61 72 65 20 73 74 6f 72 65 64  alues are stored
1186f 20 6f 6e 20 64 69 73 6b 20 61 73 20 62 69 67 2d   on disk as big-
11870 65 6e 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69  endian..*/.stati
11871 63 20 69 6e 74 20 72 65 61 64 33 32 62 69 74 73  c int read32bits
11872 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66  (sqlite3_file *f
11873 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75  d, i64 offset, u
11874 33 32 20 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73  32 *pRes){.  uns
11875 69 67 6e 65 64 20 63 68 61 72 20 61 63 5b 34 5d  igned char ac[4]
11876 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
11877 69 74 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61  ite3OsRead(fd, a
11878 63 2c 20 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f  c, sizeof(ac), o
11879 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63  ffset);.  if( rc
1187a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1187b 20 20 20 2a 70 52 65 73 20 3d 20 73 71 6c 69 74     *pRes = sqlit
1187c 65 33 47 65 74 34 62 79 74 65 28 61 63 29 3b 0a  e3Get4byte(ac);.
1187d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1187e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
1187f 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  a 32-bit integer
11880 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 62   into a string b
11881 75 66 66 65 72 20 69 6e 20 62 69 67 2d 65 6e 64  uffer in big-end
11882 69 61 6e 20 62 79 74 65 20 6f 72 64 65 72 2e 0a  ian byte order..
11883 2a 2f 0a 23 64 65 66 69 6e 65 20 70 75 74 33 32  */.#define put32
11884 62 69 74 73 28 41 2c 42 29 20 20 73 71 6c 69 74  bits(A,B)  sqlit
11885 65 33 50 75 74 34 62 79 74 65 28 28 75 38 2a 29  e3Put4byte((u8*)
11886 41 2c 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  A,B)../*.** Writ
11887 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  e a 32-bit integ
11888 65 72 20 69 6e 74 6f 20 74 68 65 20 67 69 76 65  er into the give
11889 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
1188a 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  r.  Return SQLIT
1188b 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65  E_OK.** on succe
1188c 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63  ss or an error c
1188d 6f 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67  ode is something
1188e 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a   goes wrong..*/.
1188f 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65  static int write
11890 33 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66  32bits(sqlite3_f
11891 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66  ile *fd, i64 off
11892 73 65 74 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20  set, u32 val){. 
11893 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70   char ac[4];.  p
11894 75 74 33 32 62 69 74 73 28 61 63 2c 20 76 61 6c  ut32bits(ac, val
11895 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
11896 74 65 33 4f 73 57 72 69 74 65 28 66 64 2c 20 61  te3OsWrite(fd, a
11897 63 2c 20 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d  c, 4, offset);.}
11898 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75  ../*.** The argu
11899 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 6d 61 63  ment to this mac
1189a 72 6f 20 69 73 20 61 20 66 69 6c 65 20 64 65 73  ro is a file des
1189b 63 72 69 70 74 6f 72 20 28 74 79 70 65 20 73 71  criptor (type sq
1189c 6c 69 74 65 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a  lite3_file*)..**
1189d 20 52 65 74 75 72 6e 20 30 20 69 66 20 69 74 20   Return 0 if it 
1189e 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20  is not open, or 
1189f 6e 6f 6e 2d 7a 65 72 6f 20 28 62 75 74 20 6e 6f  non-zero (but no
118a0 74 20 31 29 20 69 66 20 69 74 20 69 73 2e 0a 2a  t 1) if it is..*
118a1 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 73 6f 20  *.** This is so 
118a2 74 68 61 74 20 65 78 70 72 65 73 73 69 6f 6e 73  that expressions
118a3 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20   can be written 
118a4 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20  as:.**.**   if( 
118a5 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
118a6 66 64 29 20 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a  fd) ){ ....**.**
118a7 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a   instead of.**.*
118a8 2a 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  *   if( pPager->
118a9 6a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b  jfd->pMethods ){
118aa 20 2e 2e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20   ....*/.#define 
118ab 69 73 4f 70 65 6e 28 70 46 64 29 20 28 28 70 46  isOpen(pFd) ((pF
118ac 64 29 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f  d)->pMethods)../
118ad 2a 0a 2a 2a 20 49 66 20 66 69 6c 65 20 70 46 64  *.** If file pFd
118ae 20 69 73 20 6f 70 65 6e 2c 20 63 61 6c 6c 20 73   is open, call s
118af 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29  qlite3OsUnlock()
118b0 20 6f 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69   on it..*/.stati
118b1 63 20 69 6e 74 20 6f 73 55 6e 6c 6f 63 6b 28 73  c int osUnlock(s
118b2 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 64  qlite3_file *pFd
118b3 2c 20 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20  , int eLock){.  
118b4 69 66 28 20 21 69 73 4f 70 65 6e 28 70 46 64 29  if( !isOpen(pFd)
118b5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
118b6 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
118b7 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73  return sqlite3Os
118b8 55 6e 6c 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63  Unlock(pFd, eLoc
118b9 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  k);.}../*.** Thi
118ba 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72  s function deter
118bb 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72  mines whether or
118bc 20 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d   not the atomic-
118bd 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  write optimizati
118be 6f 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  on.** can be use
118bf 64 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65  d with this page
118c0 72 2e 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74  r. The optimizat
118c1 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20  ion can be used 
118c2 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74  if:.**.**  (a) t
118c3 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
118c4 64 20 62 79 20 4f 73 44 65 76 69 63 65 43 68 61  d by OsDeviceCha
118c5 72 61 63 74 65 72 69 73 74 69 63 73 28 29 20 69  racteristics() i
118c6 6e 64 69 63 61 74 65 73 20 74 68 61 74 0a 2a 2a  ndicates that.**
118c7 20 20 20 20 20 20 61 20 64 61 74 61 62 61 73 65        a database
118c8 20 70 61 67 65 20 6d 61 79 20 62 65 20 77 72 69   page may be wri
118c9 74 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c  tten atomically,
118ca 20 61 6e 64 0a 2a 2a 20 20 28 62 29 20 74 68 65   and.**  (b) the
118cb 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
118cc 62 79 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28  by OsSectorSize(
118cd 29 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f  ) is less than o
118ce 72 20 65 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20  r equal.**      
118cf 74 6f 20 74 68 65 20 70 61 67 65 20 73 69 7a 65  to the page size
118d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69  ..**.** The opti
118d1 6d 69 7a 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f  mization is also
118d2 20 61 6c 77 61 79 73 20 65 6e 61 62 6c 65 64 20   always enabled 
118d3 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69  for temporary fi
118d4 6c 65 73 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e  les. It is.** an
118d5 20 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74   error to call t
118d6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20  his function if 
118d7 70 50 61 67 65 72 20 69 73 20 6f 70 65 6e 65 64  pPager is opened
118d8 20 6f 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79   on an in-memory
118d9 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  .** database..**
118da 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d  .** If the optim
118db 69 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62  ization cannot b
118dc 65 20 75 73 65 64 2c 20 30 20 69 73 20 72 65 74  e used, 0 is ret
118dd 75 72 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e  urned. If it can
118de 20 62 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65   be used,.** the
118df 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  n the value retu
118e0 72 6e 65 64 20 69 73 20 74 68 65 20 73 69 7a 65  rned is the size
118e1 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
118e2 66 69 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20  file when it.** 
118e3 63 6f 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63  contains rollbac
118e4 6b 20 64 61 74 61 20 66 6f 72 20 65 78 61 63 74  k data for exact
118e5 6c 79 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a  ly one page..*/.
118e6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
118e7 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
118e8 45 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e  E.static int jrn
118e9 6c 42 75 66 66 65 72 53 69 7a 65 28 50 61 67 65  lBufferSize(Page
118ea 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73  r *pPager){.  as
118eb 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a  sert( !MEMDB );.
118ec 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74    if( !pPager->t
118ed 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69  empFile ){.    i
118ee 6e 74 20 64 63 3b 20 20 20 20 20 20 20 20 20 20  nt dc;          
118ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118f0 20 2f 2a 20 44 65 76 69 63 65 20 63 68 61 72 61   /* Device chara
118f1 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20  cteristics */.  
118f2 20 20 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20    int nSector;  
118f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118f4 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69      /* Sector si
118f5 7a 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a  ze */.    int sz
118f6 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
118f7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
118f8 61 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20  age size */..   
118f9 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
118fa 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20  pPager->fd) );. 
118fb 20 20 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f     dc = sqlite3O
118fc 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
118fd 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66  istics(pPager->f
118fe 64 29 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72 20  d);.    nSector 
118ff 3d 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72  = pPager->sector
11900 53 69 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67 65  Size;.    szPage
11901 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53   = pPager->pageS
11902 69 7a 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 74  ize;..    assert
11903 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54  (SQLITE_IOCAP_AT
11904 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38  OMIC512==(512>>8
11905 29 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 53  ));.    assert(S
11906 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
11907 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38  IC64K==(65536>>8
11908 29 29 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28  ));.    if( 0==(
11909 64 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50  dc&(SQLITE_IOCAP
1190a 5f 41 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e  _ATOMIC|(szPage>
1190b 3e 38 29 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e  >8)) || nSector>
1190c 73 7a 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20  szPage) ){.     
1190d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
1190e 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a  .  }..  return J
1190f 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
11910 61 67 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f  ager) + JOURNAL_
11911 50 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d  PG_SZ(pPager);.}
11912 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
11913 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
11914 41 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20  AGES is defined 
11915 74 68 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20  then we do some 
11916 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a  sanity checking.
11917 2a 2a 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20  ** on the cache 
11918 75 73 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e  using a hash fun
11919 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20  ction.  This is 
1191a 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
1191b 0a 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e  .** and debuggin
1191c 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65  g only..*/.#ifde
1191d 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
1191e 41 47 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  AGES./*.** Retur
1191f 6e 20 61 20 33 32 2d 62 69 74 20 68 61 73 68 20  n a 32-bit hash 
11920 6f 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61  of the page data
11921 20 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73   for pPage..*/.s
11922 74 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f  tatic u32 pager_
11923 64 61 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79  datahash(int nBy
11924 74 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61  te, unsigned cha
11925 72 20 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32  r *pData){.  u32
11926 20 68 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74   hash = 0;.  int
11927 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
11928 3c 6e 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20  <nByte; i++){.  
11929 20 20 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31    hash = (hash*1
1192a 30 33 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b  039) + pData[i];
1192b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61  .  }.  return ha
1192c 73 68 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32  sh;.}.static u32
1192d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28   pager_pagehash(
1192e 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20  PgHdr *pPage){. 
1192f 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61   return pager_da
11930 74 61 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50  tahash(pPage->pP
11931 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
11932 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
11933 29 70 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a  )pPage->pData);.
11934 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  }.static void pa
11935 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68  ger_set_pagehash
11936 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a  (PgHdr *pPage){.
11937 20 20 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73    pPage->pageHas
11938 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61  h = pager_pageha
11939 73 68 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a  sh(pPage);.}../*
1193a 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41  .** The CHECK_PA
1193b 47 45 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61  GE macro takes a
1193c 20 50 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72   PgHdr* as an ar
1193d 67 75 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54  gument. If SQLIT
1193e 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a  E_CHECK_PAGES.**
1193f 20 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64   is defined, and
11940 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64   NDEBUG is not d
11941 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72  efined, an asser
11942 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68  t() statement ch
11943 65 63 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65  ecks.** that the
11944 20 70 61 67 65 20 69 73 20 65 69 74 68 65 72 20   page is either 
11945 64 69 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d  dirty or still m
11946 61 74 63 68 65 73 20 74 68 65 20 63 61 6c 63 75  atches the calcu
11947 6c 61 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e  lated page-hash.
11948 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43  .*/.#define CHEC
11949 4b 5f 50 41 47 45 28 78 29 20 63 68 65 63 6b 50  K_PAGE(x) checkP
1194a 61 67 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f  age(x).static vo
1194b 69 64 20 63 68 65 63 6b 50 61 67 65 28 50 67 48  id checkPage(PgH
1194c 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65  dr *pPg){.  Page
1194d 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d  r *pPager = pPg-
1194e 3e 70 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72  >pPager;.  asser
1194f 74 28 20 21 70 50 67 2d 3e 70 61 67 65 48 61 73  t( !pPg->pageHas
11950 68 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72 72  h || pPager->err
11951 43 6f 64 65 0a 20 20 20 20 20 20 7c 7c 20 28 70  Code.      || (p
11952 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
11953 44 49 52 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70  DIRTY) || pPg->p
11954 61 67 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70  ageHash==pager_p
11955 61 67 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a  agehash(pPg) );.
11956 7d 0a 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  }..#else.#define
11957 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28   pager_datahash(
11958 58 2c 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20  X,Y)  0.#define 
11959 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 58  pager_pagehash(X
1195a 29 20 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45  )  0.#define CHE
1195b 43 4b 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69  CK_PAGE(x).#endi
1195c 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45  f  /* SQLITE_CHE
1195d 43 4b 5f 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a  CK_PAGES */../*.
1195e 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20  ** When this is 
1195f 63 61 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e  called the journ
11960 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65  al file for page
11961 72 20 70 50 61 67 65 72 20 6d 75 73 74 20 62 65  r pPager must be
11962 20 6f 70 65 6e 2e 0a 2a 2a 20 54 68 69 73 20 66   open..** This f
11963 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
11964 20 74 6f 20 72 65 61 64 20 61 20 6d 61 73 74 65   to read a maste
11965 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e  r journal file n
11966 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a  ame from the .**
11967 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65   end of the file
11968 20 61 6e 64 2c 20 69 66 20 73 75 63 63 65 73 73   and, if success
11969 66 75 6c 2c 20 63 6f 70 69 65 73 20 69 74 20 69  ful, copies it i
1196a 6e 74 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c  nto memory suppl
1196b 69 65 64 20 0a 2a 2a 20 62 79 20 74 68 65 20 63  ied .** by the c
1196c 61 6c 6c 65 72 2e 20 53 65 65 20 63 6f 6d 6d 65  aller. See comme
1196d 6e 74 73 20 61 62 6f 76 65 20 77 72 69 74 65 4d  nts above writeM
1196e 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66  asterJournal() f
1196f 6f 72 20 74 68 65 20 66 6f 72 6d 61 74 0a 2a 2a  or the format.**
11970 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61   used to store a
11971 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11972 66 69 6c 65 20 6e 61 6d 65 20 61 74 20 74 68 65  file name at the
11973 20 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61   end of a journa
11974 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d  l file..**.** zM
11975 61 73 74 65 72 20 6d 75 73 74 20 70 6f 69 6e 74  aster must point
11976 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20   to a buffer of 
11977 61 74 20 6c 65 61 73 74 20 6e 4d 61 73 74 65 72  at least nMaster
11978 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64   bytes allocated
11979 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65   by.** the calle
1197a 72 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  r. This should b
1197b 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78  e sqlite3_vfs.mx
1197c 50 61 74 68 6e 61 6d 65 2b 31 20 28 74 6f 20 65  Pathname+1 (to e
1197d 6e 73 75 72 65 20 74 68 65 72 65 20 69 73 0a 2a  nsure there is.*
1197e 2a 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 74  * enough space t
1197f 6f 20 77 72 69 74 65 20 74 68 65 20 6d 61 73 74  o write the mast
11980 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 29  er journal name)
11981 2e 20 49 66 20 74 68 65 20 6d 61 73 74 65 72 20  . If the master 
11982 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20  journal.** name 
11983 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  in the journal i
11984 73 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 6e 4d  s longer than nM
11985 61 73 74 65 72 20 62 79 74 65 73 20 28 69 6e 63  aster bytes (inc
11986 6c 75 64 69 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d  luding a.** nul-
11987 74 65 72 6d 69 6e 61 74 6f 72 29 2c 20 74 68 65  terminator), the
11988 6e 20 74 68 69 73 20 69 73 20 68 61 6e 64 6c 65  n this is handle
11989 64 20 61 73 20 69 66 20 6e 6f 20 6d 61 73 74 65  d as if no maste
1198a 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a  r journal name.*
1198b 2a 20 77 65 72 65 20 70 72 65 73 65 6e 74 20 69  * were present i
1198c 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a  n the journal..*
1198d 2a 0a 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72  *.** If a master
1198e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61   journal file na
1198f 6d 65 20 69 73 20 70 72 65 73 65 6e 74 20 61 74  me is present at
11990 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
11991 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c  journal.** file,
11992 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69   then it is copi
11993 65 64 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66  ed into the buff
11994 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  er pointed to by
11995 20 7a 4d 61 73 74 65 72 2e 20 41 0a 2a 2a 20 6e   zMaster. A.** n
11996 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79  ul-terminator by
11997 74 65 20 69 73 20 61 70 70 65 6e 64 65 64 20 74  te is appended t
11998 6f 20 74 68 65 20 62 75 66 66 65 72 20 66 6f 6c  o the buffer fol
11999 6c 6f 77 69 6e 67 20 74 68 65 20 6d 61 73 74 65  lowing the maste
1199a 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  r.** journal fil
1199b 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e name..**.** If
1199c 20 69 74 20 69 73 20 64 65 74 65 72 6d 69 6e 65   it is determine
1199d 64 20 74 68 61 74 20 6e 6f 20 6d 61 73 74 65 72  d that no master
1199e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61   journal file na
1199f 6d 65 20 69 73 20 70 72 65 73 65 6e 74 20 0a 2a  me is present .*
119a0 2a 20 7a 4d 61 73 74 65 72 5b 30 5d 20 69 73 20  * zMaster[0] is 
119a1 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c  set to 0 and SQL
119a2 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e  ITE_OK returned.
119a3 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
119a4 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
119a5 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65  reading from the
119a6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 61   journal file, a
119a7 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 65 72 72 6f  n SQLite.** erro
119a8 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
119a9 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
119aa 74 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72  t readMasterJour
119ab 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  nal(sqlite3_file
119ac 20 2a 70 4a 72 6e 6c 2c 20 63 68 61 72 20 2a 7a   *pJrnl, char *z
119ad 4d 61 73 74 65 72 2c 20 75 33 32 20 6e 4d 61 73  Master, u32 nMas
119ae 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  ter){.  int rc; 
119af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119b0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
119b1 65 20 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 3b 20  e */.  u32 len; 
119b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119b3 20 20 2f 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62    /* Length in b
119b4 79 74 65 73 20 6f 66 20 6d 61 73 74 65 72 20 6a  ytes of master j
119b5 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20  ournal name */. 
119b6 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20   i64 szJ;       
119b7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
119b8 6f 74 61 6c 20 73 69 7a 65 20 69 6e 20 62 79 74  otal size in byt
119b9 65 73 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69  es of journal fi
119ba 6c 65 20 70 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33  le pJrnl */.  u3
119bb 32 20 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20  2 cksum;        
119bc 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 4a 20 63           /* MJ c
119bd 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 20 72 65  hecksum value re
119be 61 64 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20  ad from journal 
119bf 2a 2f 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20  */.  u32 u;     
119c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119c1 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70  /* Unsigned loop
119c2 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e   counter */.  un
119c3 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67  signed char aMag
119c4 69 63 5b 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75  ic[8];   /* A bu
119c5 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  ffer to hold the
119c6 20 6d 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f   magic header */
119c7 0a 20 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20  .  zMaster[0] = 
119c8 27 5c 30 27 3b 0a 0a 20 20 69 66 28 20 53 51 4c  '\0';..  if( SQL
119c9 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
119ca 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
119cb 70 4a 72 6e 6c 2c 20 26 73 7a 4a 29 29 0a 20 20  pJrnl, &szJ)).  
119cc 20 7c 7c 20 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c   || szJ<16.   ||
119cd 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
119ce 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a 72  = read32bits(pJr
119cf 6e 6c 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e  nl, szJ-16, &len
119d0 29 29 0a 20 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d  )).   || len>=nM
119d1 61 73 74 65 72 20 0a 20 20 20 7c 7c 20 53 51 4c  aster .   || SQL
119d2 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
119d3 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20  ad32bits(pJrnl, 
119d4 73 7a 4a 2d 31 32 2c 20 26 63 6b 73 75 6d 29 29  szJ-12, &cksum))
119d5 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b  .   || SQLITE_OK
119d6 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  !=(rc = sqlite3O
119d7 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61  sRead(pJrnl, aMa
119d8 67 69 63 2c 20 38 2c 20 73 7a 4a 2d 38 29 29 0a  gic, 8, szJ-8)).
119d9 20 20 20 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61     || memcmp(aMa
119da 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67  gic, aJournalMag
119db 69 63 2c 20 38 29 0a 20 20 20 7c 7c 20 53 51 4c  ic, 8).   || SQL
119dc 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
119dd 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e  lite3OsRead(pJrn
119de 6c 2c 20 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c  l, zMaster, len,
119df 20 73 7a 4a 2d 31 36 2d 6c 65 6e 29 29 0a 20 20   szJ-16-len)).  
119e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
119e1 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20  ;.  }..  /* See 
119e2 69 66 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20  if the checksum 
119e3 6d 61 74 63 68 65 73 20 74 68 65 20 6d 61 73 74  matches the mast
119e4 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20  er journal name 
119e5 2a 2f 0a 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c  */.  for(u=0; u<
119e6 6c 65 6e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63  len; u++){.    c
119e7 6b 73 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b  ksum -= zMaster[
119e8 75 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 6b  u];.  }.  if( ck
119e9 73 75 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  sum ){.    /* If
119ea 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f   the checksum do
119eb 65 73 6e 27 74 20 61 64 64 20 75 70 2c 20 74 68  esn't add up, th
119ec 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f  en one or more o
119ed 66 20 74 68 65 20 64 69 73 6b 20 73 65 63 74 6f  f the disk secto
119ee 72 73 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69  rs.    ** contai
119ef 6e 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20  ning the master 
119f0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  journal filename
119f1 20 69 73 20 63 6f 72 72 75 70 74 65 64 2e 20 54   is corrupted. T
119f2 68 69 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a  his means.    **
119f3 20 64 65 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c   definitely roll
119f4 20 62 61 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72   back, so just r
119f5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
119f6 61 6e 64 20 72 65 70 6f 72 74 20 61 20 28 6e 75  and report a (nu
119f7 6c 29 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72  l).    ** master
119f8 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d  -journal filenam
119f9 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65  e..    */.    le
119fa 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61  n = 0;.  }.  zMa
119fb 73 74 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27  ster[len] = '\0'
119fc 3b 0a 20 20 20 0a 20 20 72 65 74 75 72 6e 20 53  ;.   .  return S
119fd 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
119fe 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 66  ** Return the of
119ff 66 73 65 74 20 6f 66 20 74 68 65 20 73 65 63 74  fset of the sect
11a00 6f 72 20 62 6f 75 6e 64 61 72 79 20 61 74 20 6f  or boundary at o
11a01 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a  r immediately .*
11a02 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  * following the 
11a03 76 61 6c 75 65 20 69 6e 20 70 50 61 67 65 72 2d  value in pPager-
11a04 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73  >journalOff, ass
11a05 75 6d 69 6e 67 20 61 20 73 65 63 74 6f 72 20 0a  uming a sector .
11a06 2a 2a 20 73 69 7a 65 20 6f 66 20 70 50 61 67 65  ** size of pPage
11a07 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 62 79  r->sectorSize by
11a08 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66  tes..**.** i.e f
11a09 6f 72 20 61 20 73 65 63 74 6f 72 20 73 69 7a 65  or a sector size
11a0a 20 6f 66 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20   of 512:.**.**  
11a0b 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66   Pager.journalOf
11a0c 66 20 20 20 20 20 20 20 20 20 20 52 65 74 75 72  f          Retur
11a0d 6e 20 76 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d  n value.**   ---
11a0e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a0f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a10 2d 2d 2d 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20  ----.**   0     
11a11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a12 20 20 20 20 30 0a 2a 2a 20 20 20 35 31 32 20 20      0.**   512  
11a13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a14 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 31 30       512.**   10
11a15 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
11a16 20 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 20          512.**  
11a17 20 32 30 30 30 20 20 20 20 20 20 20 20 20 20 20   2000           
11a18 20 20 20 20 20 20 20 20 20 20 20 32 30 34 38 0a             2048.
11a19 2a 2a 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36  ** .*/.static i6
11a1a 34 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73  4 journalHdrOffs
11a1b 65 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  et(Pager *pPager
11a1c 29 7b 0a 20 20 69 36 34 20 6f 66 66 73 65 74 20  ){.  i64 offset 
11a1d 3d 20 30 3b 0a 20 20 69 36 34 20 63 20 3d 20 70  = 0;.  i64 c = p
11a1e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11a1f 66 3b 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20  f;.  if( c ){.  
11a20 20 20 6f 66 66 73 65 74 20 3d 20 28 28 63 2d 31    offset = ((c-1
11a21 29 2f 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  )/JOURNAL_HDR_SZ
11a22 28 70 50 61 67 65 72 29 20 2b 20 31 29 20 2a 20  (pPager) + 1) * 
11a23 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
11a24 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73  Pager);.  }.  as
11a25 73 65 72 74 28 20 6f 66 66 73 65 74 25 4a 4f 55  sert( offset%JOU
11a26 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
11a27 65 72 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  er)==0 );.  asse
11a28 72 74 28 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b  rt( offset>=c );
11a29 0a 20 20 61 73 73 65 72 74 28 20 28 6f 66 66 73  .  assert( (offs
11a2a 65 74 2d 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44  et-c)<JOURNAL_HD
11a2b 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a  R_SZ(pPager) );.
11a2c 20 20 72 65 74 75 72 6e 20 6f 66 66 73 65 74 3b    return offset;
11a2d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f  .}../*.** The jo
11a2e 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20  urnal file must 
11a2f 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69  be open when thi
11a30 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
11a31 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  lled..**.** This
11a32 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
11a33 6f 2d 6f 70 20 69 66 20 74 68 65 20 6a 6f 75 72  o-op if the jour
11a34 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 6e 6f 74  nal file has not
11a35 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f   been written to
11a36 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 63  .** within the c
11a37 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
11a38 6f 6e 20 28 69 2e 65 2e 20 69 66 20 50 61 67 65  on (i.e. if Page
11a39 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 29  r.journalOff==0)
11a3a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 6f 54 72 75  ..**.** If doTru
11a3b 6e 63 61 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72  ncate is non-zer
11a3c 6f 20 6f 72 20 74 68 65 20 50 61 67 65 72 2e 6a  o or the Pager.j
11a3d 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20  ournalSizeLimit 
11a3e 76 61 72 69 61 62 6c 65 20 69 73 0a 2a 2a 20 73  variable is.** s
11a3f 65 74 20 74 6f 20 30 2c 20 74 68 65 6e 20 74 72  et to 0, then tr
11a40 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e  uncate the journ
11a41 61 6c 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20  al file to zero 
11a42 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f  bytes in size. O
11a43 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20 7a 65 72  therwise,.** zer
11a44 6f 20 74 68 65 20 32 38 2d 62 79 74 65 20 68 65  o the 28-byte he
11a45 61 64 65 72 20 61 74 20 74 68 65 20 73 74 61 72  ader at the star
11a46 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t of the journal
11a47 20 66 69 6c 65 2e 20 49 6e 20 65 69 74 68 65 72   file. In either
11a48 20 63 61 73 65 2c 20 0a 2a 2a 20 69 66 20 74 68   case, .** if th
11a49 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69  e pager is not i
11a4a 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20  n no-sync mode, 
11a4b 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  sync the journal
11a4c 20 66 69 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c   file immediatel
11a4d 79 20 0a 2a 2a 20 61 66 74 65 72 20 77 72 69 74  y .** after writ
11a4e 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e  ing or truncatin
11a4f 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  g it..**.** If P
11a50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65  ager.journalSize
11a51 4c 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20  Limit is set to 
11a52 61 20 70 6f 73 69 74 69 76 65 2c 20 6e 6f 6e 2d  a positive, non-
11a53 7a 65 72 6f 20 76 61 6c 75 65 2c 20 61 6e 64 0a  zero value, and.
11a54 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ** following the
11a55 20 74 72 75 6e 63 61 74 69 6f 6e 20 6f 72 20 7a   truncation or z
11a56 65 72 6f 69 6e 67 20 64 65 73 63 72 69 62 65 64  eroing described
11a57 20 61 62 6f 76 65 20 74 68 65 20 73 69 7a 65 20   above the size 
11a58 6f 66 20 74 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e  of the .** journ
11a59 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73  al file in bytes
11a5a 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
11a5b 74 68 69 73 20 76 61 6c 75 65 2c 20 74 68 65 6e  this value, then
11a5c 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a   truncate the.**
11a5d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f   journal file to
11a5e 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69   Pager.journalSi
11a5f 7a 65 4c 69 6d 69 74 20 62 79 74 65 73 2e 20 54  zeLimit bytes. T
11a60 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11a61 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64  does.** not need
11a62 20 74 6f 20 62 65 20 73 79 6e 63 65 64 20 66 6f   to be synced fo
11a63 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65  llowing this ope
11a64 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  ration..**.** If
11a65 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
11a66 75 72 73 2c 20 61 62 61 6e 64 6f 6e 20 70 72 6f  urs, abandon pro
11a67 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75  cessing and retu
11a68 72 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20  rn the IO error 
11a69 63 6f 64 65 2e 0a 2a 2a 20 4f 74 68 65 72 77 69  code..** Otherwi
11a6a 73 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  se, return SQLIT
11a6b 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  E_OK..*/.static 
11a6c 69 6e 74 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48  int zeroJournalH
11a6d 64 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  dr(Pager *pPager
11a6e 2c 20 69 6e 74 20 64 6f 54 72 75 6e 63 61 74 65  , int doTruncate
11a6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
11a70 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
11a71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a72 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
11a73 20 63 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72   code */.  asser
11a74 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
11a75 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 69 66 28 20  ->jfd) );.  if( 
11a76 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11a77 66 66 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  ff ){.    const 
11a78 69 36 34 20 69 4c 69 6d 69 74 20 3d 20 70 50 61  i64 iLimit = pPa
11a79 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65  ger->journalSize
11a7a 4c 69 6d 69 74 3b 20 20 20 20 2f 2a 20 4c 6f 63  Limit;    /* Loc
11a7b 61 6c 20 63 61 63 68 65 20 6f 66 20 6a 73 6c 20  al cache of jsl 
11a7c 2a 2f 0a 0a 20 20 20 20 49 4f 54 52 41 43 45 28  */..    IOTRACE(
11a7d 28 22 4a 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22  ("JZEROHDR %p\n"
11a7e 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 69  , pPager)).    i
11a7f 66 28 20 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c  f( doTruncate ||
11a80 20 69 4c 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20   iLimit==0 ){.  
11a81 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
11a82 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65  OsTruncate(pPage
11a83 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20  r->jfd, 0);.    
11a84 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 61  }else{.      sta
11a85 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  tic const char z
11a86 65 72 6f 48 64 72 5b 32 38 5d 20 3d 20 7b 30 7d  eroHdr[28] = {0}
11a87 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
11a88 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
11a89 65 72 2d 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72  er->jfd, zeroHdr
11a8a 2c 20 73 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72  , sizeof(zeroHdr
11a8b 29 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ), 0);.    }.   
11a8c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
11a8d 4f 4b 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e  OK && !pPager->n
11a8e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72  oSync ){.      r
11a8f 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e  c = sqlite3OsSyn
11a90 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53  c(pPager->jfd, S
11a91 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f  QLITE_SYNC_DATAO
11a92 4e 4c 59 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63  NLY|pPager->sync
11a93 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a  _flags);.    }..
11a94 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70      /* At this p
11a95 6f 69 6e 74 20 74 68 65 20 74 72 61 6e 73 61 63  oint the transac
11a96 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65  tion is committe
11a97 64 20 62 75 74 20 74 68 65 20 77 72 69 74 65 20  d but the write 
11a98 6c 6f 63 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20  lock .    ** is 
11a99 73 74 69 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68  still held on th
11a9a 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65  e file. If there
11a9b 20 69 73 20 61 20 73 69 7a 65 20 6c 69 6d 69 74   is a size limit
11a9c 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20   configured for 
11a9d 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 65 72 73  .    ** the pers
11a9e 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61  istent journal a
11a9f 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  nd the journal f
11aa0 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f  ile currently co
11aa1 6e 73 75 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20  nsumes more.    
11aa2 2a 2a 20 73 70 61 63 65 20 74 68 61 6e 20 74 68  ** space than th
11aa3 61 74 20 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20  at limit allows 
11aa4 66 6f 72 2c 20 74 72 75 6e 63 61 74 65 20 69 74  for, truncate it
11aa5 20 6e 6f 77 2e 20 54 68 65 72 65 20 69 73 20 6e   now. There is n
11aa6 6f 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f  o need.    ** to
11aa7 20 73 79 6e 63 20 74 68 65 20 66 69 6c 65 20 66   sync the file f
11aa8 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70  ollowing this op
11aa9 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  eration..    */.
11aaa 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
11aab 54 45 5f 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e  TE_OK && iLimit>
11aac 30 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 73  0 ){.      i64 s
11aad 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  z;.      rc = sq
11aae 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
11aaf 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a  pPager->jfd, &sz
11ab0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
11ab1 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a  =SQLITE_OK && sz
11ab2 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20  >iLimit ){.     
11ab3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
11ab4 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  sTruncate(pPager
11ab5 2d 3e 6a 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a  ->jfd, iLimit);.
11ab6 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
11ab7 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
11ab8 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72  ../*.** The jour
11ab9 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65  nal file must be
11aba 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20   open when this 
11abb 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
11abc 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  d. A journal.** 
11abd 68 65 61 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f  header (JOURNAL_
11abe 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73  HDR_SZ bytes) is
11abf 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
11ac0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  e journal file a
11ac1 74 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74  t the.** current
11ac2 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   location..**.**
11ac3 20 54 68 65 20 66 6f 72 6d 61 74 20 66 6f 72 20   The format for 
11ac4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
11ac5 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  er is as follows
11ac6 3a 0a 2a 2a 20 2d 20 38 20 62 79 74 65 73 3a 20  :.** - 8 bytes: 
11ac7 4d 61 67 69 63 20 69 64 65 6e 74 69 66 79 69 6e  Magic identifyin
11ac8 67 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74  g journal format
11ac9 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20  ..** - 4 bytes: 
11aca 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64  Number of record
11acb 73 20 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72  s in journal, or
11acc 20 2d 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65   -1 no-sync mode
11acd 20 69 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62   is on..** - 4 b
11ace 79 74 65 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d  ytes: Random num
11acf 62 65 72 20 75 73 65 64 20 66 6f 72 20 70 61 67  ber used for pag
11ad0 65 20 68 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62  e hash..** - 4 b
11ad1 79 74 65 73 3a 20 49 6e 69 74 69 61 6c 20 64 61  ytes: Initial da
11ad2 74 61 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e  tabase page coun
11ad3 74 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a  t..** - 4 bytes:
11ad4 20 53 65 63 74 6f 72 20 73 69 7a 65 20 75 73 65   Sector size use
11ad5 64 20 62 79 20 74 68 65 20 70 72 6f 63 65 73 73  d by the process
11ad6 20 74 68 61 74 20 77 72 6f 74 65 20 74 68 69 73   that wrote this
11ad7 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34   journal..** - 4
11ad8 20 62 79 74 65 73 3a 20 44 61 74 61 62 61 73 65   bytes: Database
11ad9 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a   page size..** .
11ada 2a 2a 20 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28  ** Followed by (
11adb 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d  JOURNAL_HDR_SZ -
11adc 20 32 38 29 20 62 79 74 65 73 20 6f 66 20 75 6e   28) bytes of un
11add 75 73 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73  used space..*/.s
11ade 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 4a  tatic int writeJ
11adf 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20  ournalHdr(Pager 
11ae0 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20  *pPager){.  int 
11ae1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
11ae2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ae3 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
11ae4 2f 0a 20 20 63 68 61 72 20 2a 7a 48 65 61 64 65  /.  char *zHeade
11ae5 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70  r = pPager->pTmp
11ae6 53 70 61 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f  Space;  /* Tempo
11ae7 72 61 72 79 20 73 70 61 63 65 20 75 73 65 64 20  rary space used 
11ae8 74 6f 20 62 75 69 6c 64 20 68 65 61 64 65 72 20  to build header 
11ae9 2a 2f 0a 20 20 75 33 32 20 6e 48 65 61 64 65 72  */.  u32 nHeader
11aea 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53   = pPager->pageS
11aeb 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65  ize;     /* Size
11aec 20 6f 66 20 62 75 66 66 65 72 20 70 6f 69 6e 74   of buffer point
11aed 65 64 20 74 6f 20 62 79 20 7a 48 65 61 64 65 72  ed to by zHeader
11aee 20 2a 2f 0a 20 20 75 33 32 20 6e 57 72 69 74 65   */.  u32 nWrite
11aef 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11af0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
11af1 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 65 63  es of header sec
11af2 74 6f 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20  tor written */. 
11af3 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20   int ii;        
11af4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11af5 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
11af6 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72  nter */..  asser
11af7 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
11af8 2d 3e 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f  ->jfd) );      /
11af9 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d  * Journal file m
11afa 75 73 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a  ust be open. */.
11afb 0a 20 20 69 66 28 20 6e 48 65 61 64 65 72 3e 4a  .  if( nHeader>J
11afc 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
11afd 61 67 65 72 29 20 29 7b 0a 20 20 20 20 6e 48 65  ager) ){.    nHe
11afe 61 64 65 72 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48  ader = JOURNAL_H
11aff 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20  DR_SZ(pPager);. 
11b00 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72   }..  /* If ther
11b01 65 20 61 72 65 20 61 63 74 69 76 65 20 73 61 76  e are active sav
11b02 65 70 6f 69 6e 74 73 20 61 6e 64 20 61 6e 79 20  epoints and any 
11b03 6f 66 20 74 68 65 6d 20 77 65 72 65 20 63 72 65  of them were cre
11b04 61 74 65 64 20 0a 20 20 2a 2a 20 73 69 6e 63 65  ated .  ** since
11b05 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
11b06 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
11b07 77 61 73 20 77 72 69 74 74 65 6e 2c 20 75 70 64  was written, upd
11b08 61 74 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 61  ate the .  ** Pa
11b09 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64  gerSavepoint.iHd
11b0a 72 4f 66 66 73 65 74 20 66 69 65 6c 64 73 20 6e  rOffset fields n
11b0b 6f 77 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ow..  */.  for(i
11b0c 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e  i=0; ii<pPager->
11b0d 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b  nSavepoint; ii++
11b0e 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  ){.    if( pPage
11b0f 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69  r->aSavepoint[ii
11b10 5d 2e 69 48 64 72 4f 66 66 73 65 74 3d 3d 30 20  ].iHdrOffset==0 
11b11 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
11b12 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e  >aSavepoint[ii].
11b13 69 48 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61  iHdrOffset = pPa
11b14 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
11b15 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50  .    }.  }..  pP
11b16 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
11b17 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   = pPager->journ
11b18 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48  alOff = journalH
11b19 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29  drOffset(pPager)
11b1a 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 57 72  ;..  /* .  ** Wr
11b1b 69 74 65 20 74 68 65 20 6e 52 65 63 20 46 69 65  ite the nRec Fie
11b1c 6c 64 20 2d 20 74 68 65 20 6e 75 6d 62 65 72 20  ld - the number 
11b1d 6f 66 20 70 61 67 65 20 72 65 63 6f 72 64 73 20  of page records 
11b1e 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 69 73  that follow this
11b1f 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65  .  ** journal he
11b20 61 64 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20  ader. Normally, 
11b21 7a 65 72 6f 20 69 73 20 77 72 69 74 74 65 6e 20  zero is written 
11b22 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 61 74  to this value at
11b23 20 74 68 69 73 20 74 69 6d 65 2e 0a 20 20 2a 2a   this time..  **
11b24 20 41 66 74 65 72 20 74 68 65 20 72 65 63 6f 72   After the recor
11b25 64 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20  ds are added to 
11b26 74 68 65 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64  the journal (and
11b27 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73 79 6e   the journal syn
11b28 63 65 64 2c 20 0a 20 20 2a 2a 20 69 66 20 69 6e  ced, .  ** if in
11b29 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 29   full-sync mode)
11b2a 2c 20 74 68 65 20 7a 65 72 6f 20 69 73 20 6f 76  , the zero is ov
11b2b 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 74  erwritten with t
11b2c 68 65 20 74 72 75 65 20 6e 75 6d 62 65 72 0a 20  he true number. 
11b2d 20 2a 2a 20 6f 66 20 72 65 63 6f 72 64 73 20 28   ** of records (
11b2e 73 65 65 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28  see syncJournal(
11b2f 29 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20  ))..  **.  ** A 
11b30 66 61 73 74 65 72 20 61 6c 74 65 72 6e 61 74 69  faster alternati
11b31 76 65 20 69 73 20 74 6f 20 77 72 69 74 65 20 30  ve is to write 0
11b32 78 46 46 46 46 46 46 46 46 20 74 6f 20 74 68 65  xFFFFFFFF to the
11b33 20 6e 52 65 63 20 66 69 65 6c 64 2e 20 57 68 65   nRec field. Whe
11b34 6e 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74  n.  ** reading t
11b35 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 69 73 20  he journal this 
11b36 76 61 6c 75 65 20 74 65 6c 6c 73 20 53 51 4c 69  value tells SQLi
11b37 74 65 20 74 6f 20 61 73 73 75 6d 65 20 74 68 61  te to assume tha
11b38 74 20 74 68 65 0a 20 20 2a 2a 20 72 65 73 74 20  t the.  ** rest 
11b39 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
11b3a 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 76 61 6c  ile contains val
11b3b 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 2e  id page records.
11b3c 20 54 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e   This assumption
11b3d 0a 20 20 2a 2a 20 69 73 20 64 61 6e 67 65 72 6f  .  ** is dangero
11b3e 75 73 2c 20 61 73 20 69 66 20 61 20 66 61 69 6c  us, as if a fail
11b3f 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 68 69  ure occurred whi
11b40 6c 73 74 20 77 72 69 74 69 6e 67 20 74 6f 20 74  lst writing to t
11b41 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20  he journal.  ** 
11b42 66 69 6c 65 20 69 74 20 6d 61 79 20 63 6f 6e 74  file it may cont
11b43 61 69 6e 20 73 6f 6d 65 20 67 61 72 62 61 67 65  ain some garbage
11b44 20 64 61 74 61 2e 20 54 68 65 72 65 20 61 72 65   data. There are
11b45 20 74 77 6f 20 73 63 65 6e 61 72 69 6f 73 0a 20   two scenarios. 
11b46 20 2a 2a 20 77 68 65 72 65 20 74 68 69 73 20 72   ** where this r
11b47 69 73 6b 20 63 61 6e 20 62 65 20 69 67 6e 6f 72  isk can be ignor
11b48 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  ed:.  **.  **   
11b49 2a 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 72  * When the pager
11b4a 20 69 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d   is in no-sync m
11b4b 6f 64 65 2e 20 43 6f 72 72 75 70 74 69 6f 6e 20  ode. Corruption 
11b4c 63 61 6e 20 66 6f 6c 6c 6f 77 20 61 0a 20 20 2a  can follow a.  *
11b4d 2a 20 20 20 20 20 70 6f 77 65 72 20 66 61 69 6c  *     power fail
11b4e 75 72 65 20 69 6e 20 74 68 69 73 20 63 61 73 65  ure in this case
11b4f 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2a 0a 20 20   anyway..  **.  
11b50 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65 20  **   * When the 
11b51 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46  SQLITE_IOCAP_SAF
11b52 45 5f 41 50 50 45 4e 44 20 66 6c 61 67 20 69 73  E_APPEND flag is
11b53 20 73 65 74 2e 20 54 68 69 73 20 67 75 61 72 61   set. This guara
11b54 6e 74 65 65 73 0a 20 20 2a 2a 20 20 20 20 20 74  ntees.  **     t
11b55 68 61 74 20 67 61 72 62 61 67 65 20 64 61 74 61  hat garbage data
11b56 20 69 73 20 6e 65 76 65 72 20 61 70 70 65 6e 64   is never append
11b57 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  ed to the journa
11b58 6c 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61  l file..  */.  a
11b59 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
11b5a 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61  ager->fd) || pPa
11b5b 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 3b 0a 20  ger->noSync );. 
11b5c 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6e 6f   if( (pPager->no
11b5d 53 79 6e 63 29 20 7c 7c 20 28 70 50 61 67 65 72  Sync) || (pPager
11b5e 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
11b5f 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
11b60 5f 4d 45 4d 4f 52 59 29 0a 20 20 20 7c 7c 20 28  _MEMORY).   || (
11b61 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43  sqlite3OsDeviceC
11b62 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70  haracteristics(p
11b63 50 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54  Pager->fd)&SQLIT
11b64 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50  E_IOCAP_SAFE_APP
11b65 45 4e 44 29 20 0a 20 20 29 7b 0a 20 20 20 20 6d  END) .  ){.    m
11b66 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61  emcpy(zHeader, a
11b67 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69  JournalMagic, si
11b68 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
11b69 69 63 29 29 3b 0a 20 20 20 20 70 75 74 33 32 62  ic));.    put32b
11b6a 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a  its(&zHeader[siz
11b6b 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  eof(aJournalMagi
11b6c 63 29 5d 2c 20 30 78 66 66 66 66 66 66 66 66 29  c)], 0xffffffff)
11b6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d  ;.  }else{.    m
11b6e 65 6d 73 65 74 28 7a 48 65 61 64 65 72 2c 20 30  emset(zHeader, 0
11b6f 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  , sizeof(aJourna
11b70 6c 4d 61 67 69 63 29 2b 34 29 3b 0a 20 20 7d 0a  lMagic)+4);.  }.
11b71 0a 20 20 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d  .  /* The random
11b72 20 63 68 65 63 6b 2d 68 61 73 68 20 69 6e 69 74   check-hash init
11b73 69 61 6c 69 73 65 72 20 2a 2f 20 0a 20 20 73 71  ialiser */ .  sq
11b74 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
11b75 28 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e  (sizeof(pPager->
11b76 63 6b 73 75 6d 49 6e 69 74 29 2c 20 26 70 50 61  cksumInit), &pPa
11b77 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b  ger->cksumInit);
11b78 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48  .  put32bits(&zH
11b79 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f  eader[sizeof(aJo
11b7a 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20  urnalMagic)+4], 
11b7b 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
11b7c 74 29 3b 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69  t);.  /* The ini
11b7d 74 69 61 6c 20 64 61 74 61 62 61 73 65 20 73 69  tial database si
11b7e 7a 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74  ze */.  put32bit
11b7f 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f  s(&zHeader[sizeo
11b80 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
11b81 2b 38 5d 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f  +8], pPager->dbO
11b82 72 69 67 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54  rigSize);.  /* T
11b83 68 65 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f  he assumed secto
11b84 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20  r size for this 
11b85 70 72 6f 63 65 73 73 20 2a 2f 0a 20 20 70 75 74  process */.  put
11b86 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b  32bits(&zHeader[
11b87 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d  sizeof(aJournalM
11b88 61 67 69 63 29 2b 31 32 5d 2c 20 70 50 61 67 65  agic)+12], pPage
11b89 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a  r->sectorSize);.
11b8a 0a 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 73  .  /* The page s
11b8b 69 7a 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69  ize */.  put32bi
11b8c 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65  ts(&zHeader[size
11b8d 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
11b8e 29 2b 31 36 5d 2c 20 70 50 61 67 65 72 2d 3e 70  )+16], pPager->p
11b8f 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20  ageSize);..  /* 
11b90 49 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65  Initializing the
11b91 20 74 61 69 6c 20 6f 66 20 74 68 65 20 62 75 66   tail of the buf
11b92 66 65 72 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  fer is not neces
11b93 73 61 72 79 2e 20 20 45 76 65 72 79 74 68 69 6e  sary.  Everythin
11b94 67 0a 20 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e  g.  ** works fin
11b95 64 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  d if the followi
11b96 6e 67 20 6d 65 6d 73 65 74 28 29 20 69 73 20 6f  ng memset() is o
11b97 6d 69 74 74 65 64 2e 20 20 42 75 74 20 69 6e 69  mitted.  But ini
11b98 74 69 61 6c 69 7a 69 6e 67 0a 20 20 2a 2a 20 74  tializing.  ** t
11b99 68 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e  he memory preven
11b9a 74 73 20 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d  ts valgrind from
11b9b 20 63 6f 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f   complaining, so
11b9c 20 77 65 20 61 72 65 20 77 69 6c 6c 69 6e 67 20   we are willing 
11b9d 74 6f 0a 20 20 2a 2a 20 74 61 6b 65 20 74 68 65  to.  ** take the
11b9e 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74   performance hit
11b9f 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  ..  */.  memset(
11ba0 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
11ba1 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32  aJournalMagic)+2
11ba2 30 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  0], 0,.         
11ba3 6e 48 65 61 64 65 72 2d 28 73 69 7a 65 6f 66 28  nHeader-(sizeof(
11ba4 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32  aJournalMagic)+2
11ba5 30 29 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 74 68  0));..  /* In th
11ba6 65 6f 72 79 2c 20 69 74 20 69 73 20 6f 6e 6c 79  eory, it is only
11ba7 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72   necessary to wr
11ba8 69 74 65 20 74 68 65 20 32 38 20 62 79 74 65 73  ite the 28 bytes
11ba9 20 74 68 61 74 20 74 68 65 20 0a 20 20 2a 2a 20   that the .  ** 
11baa 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 63  journal header c
11bab 6f 6e 73 75 6d 65 73 20 74 6f 20 74 68 65 20 6a  onsumes to the j
11bac 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65  ournal file here
11bad 2e 20 54 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74  . Then increment
11bae 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72   the .  ** Pager
11baf 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 76 61 72 69  .journalOff vari
11bb0 61 62 6c 65 20 62 79 20 4a 4f 55 52 4e 41 4c 5f  able by JOURNAL_
11bb1 48 44 52 5f 53 5a 20 73 6f 20 74 68 61 74 20 74  HDR_SZ so that t
11bb2 68 65 20 6e 65 78 74 20 0a 20 20 2a 2a 20 72 65  he next .  ** re
11bb3 63 6f 72 64 20 69 73 20 77 72 69 74 74 65 6e 20  cord is written 
11bb4 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  to the following
11bb5 20 73 65 63 74 6f 72 20 28 6c 65 61 76 69 6e 67   sector (leaving
11bb6 20 61 20 67 61 70 20 69 6e 20 74 68 65 20 66 69   a gap in the fi
11bb7 6c 65 0a 20 20 2a 2a 20 74 68 61 74 20 77 69 6c  le.  ** that wil
11bb8 6c 20 62 65 20 69 6d 70 6c 69 63 69 74 6c 79 20  l be implicitly 
11bb9 66 69 6c 6c 65 64 20 69 6e 20 62 79 20 74 68 65  filled in by the
11bba 20 4f 53 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20   OS)..  **.  ** 
11bbb 48 6f 77 65 76 65 72 20 69 74 20 68 61 73 20 62  However it has b
11bbc 65 65 6e 20 64 69 73 63 6f 76 65 72 65 64 20 74  een discovered t
11bbd 68 61 74 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74  hat on some syst
11bbe 65 6d 73 20 74 68 69 73 20 70 61 74 74 65 72 6e  ems this pattern
11bbf 20 63 61 6e 20 0a 20 20 2a 2a 20 62 65 20 73 69   can .  ** be si
11bc0 67 6e 69 66 69 63 61 6e 74 6c 79 20 73 6c 6f 77  gnificantly slow
11bc1 65 72 20 74 68 61 6e 20 63 6f 6e 74 69 67 75 6f  er than contiguo
11bc2 75 73 6c 79 20 77 72 69 74 69 6e 67 20 64 61 74  usly writing dat
11bc3 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 0a 20  a to the file,. 
11bc4 20 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 61 74   ** even if that
11bc5 20 6d 65 61 6e 73 20 65 78 70 6c 69 63 69 74 6c   means explicitl
11bc6 79 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74  y writing data t
11bc7 6f 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0a  o the block of .
11bc8 20 20 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44    ** (JOURNAL_HD
11bc9 52 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73  R_SZ - 28) bytes
11bca 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62   that will not b
11bcb 65 20 75 73 65 64 2e 20 53 6f 20 74 68 61 74 20  e used. So that 
11bcc 69 73 20 77 68 61 74 0a 20 20 2a 2a 20 69 73 20  is what.  ** is 
11bcd 64 6f 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a  done. .  **.  **
11bce 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 72 65 71   The loop is req
11bcf 75 69 72 65 64 20 68 65 72 65 20 69 6e 20 63 61  uired here in ca
11bd0 73 65 20 74 68 65 20 73 65 63 74 6f 72 2d 73 69  se the sector-si
11bd1 7a 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61  ze is larger tha
11bd2 6e 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61  n the .  ** data
11bd3 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e 20  base page size. 
11bd4 53 69 6e 63 65 20 74 68 65 20 7a 48 65 61 64 65  Since the zHeade
11bd5 72 20 62 75 66 66 65 72 20 69 73 20 6f 6e 6c 79  r buffer is only
11bd6 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 0a   Pager.pageSize.
11bd7 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69    ** bytes in si
11bd8 7a 65 2c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  ze, more than on
11bd9 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  e call to sqlite
11bda 33 4f 73 57 72 69 74 65 28 29 20 6d 61 79 20 62  3OsWrite() may b
11bdb 65 20 72 65 71 75 69 72 65 64 0a 20 20 2a 2a 20  e required.  ** 
11bdc 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20  to populate the 
11bdd 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c 20 68  entire journal h
11bde 65 61 64 65 72 20 73 65 63 74 6f 72 2e 0a 20 20  eader sector..  
11bdf 2a 2f 20 0a 20 20 66 6f 72 28 6e 57 72 69 74 65  */ .  for(nWrite
11be0 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
11be1 4b 26 26 6e 57 72 69 74 65 3c 4a 4f 55 52 4e 41  K&&nWrite<JOURNA
11be2 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
11be3 3b 20 6e 57 72 69 74 65 2b 3d 6e 48 65 61 64 65  ; nWrite+=nHeade
11be4 72 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28  r){.    IOTRACE(
11be5 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25  ("JHDR %p %lld %
11be6 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50  d\n", pPager, pP
11be7 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
11be8 2c 20 6e 48 65 61 64 65 72 29 29 0a 20 20 20 20  , nHeader)).    
11be9 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
11bea 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ite(pPager->jfd,
11beb 20 7a 48 65 61 64 65 72 2c 20 6e 48 65 61 64 65   zHeader, nHeade
11bec 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  r, pPager->journ
11bed 61 6c 4f 66 66 29 3b 0a 20 20 20 20 70 50 61 67  alOff);.    pPag
11bee 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b  er->journalOff +
11bef 3d 20 6e 48 65 61 64 65 72 3b 0a 20 20 7d 0a 0a  = nHeader;.  }..
11bf0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
11bf1 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61  /*.** The journa
11bf2 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f  l file must be o
11bf3 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 69 73  pen when this is
11bf4 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e   called. A journ
11bf5 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 0a 2a  al header file.*
11bf6 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  * (JOURNAL_HDR_S
11bf7 5a 20 62 79 74 65 73 29 20 69 73 20 72 65 61 64  Z bytes) is read
11bf8 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e   from the curren
11bf9 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68  t location in th
11bfa 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c  e journal.** fil
11bfb 65 2e 20 54 68 65 20 63 75 72 72 65 6e 74 20 6c  e. The current l
11bfc 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a  ocation in the j
11bfd 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 67  ournal file is g
11bfe 69 76 65 6e 20 62 79 0a 2a 2a 20 70 50 61 67 65  iven by.** pPage
11bff 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2e 20 53  r->journalOff. S
11c00 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76  ee comments abov
11c01 65 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65  e function write
11c02 4a 6f 75 72 6e 61 6c 48 64 72 28 29 20 66 6f 72  JournalHdr() for
11c03 0a 2a 2a 20 61 20 64 65 73 63 72 69 70 74 69 6f  .** a descriptio
11c04 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n of the journal
11c05 20 68 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a   header format..
11c06 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 68 65 61  **.** If the hea
11c07 64 65 72 20 69 73 20 72 65 61 64 20 73 75 63 63  der is read succ
11c08 65 73 73 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63  essfully, *pNRec
11c09 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
11c0a 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65  umber of.** page
11c0b 20 72 65 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69   records followi
11c0c 6e 67 20 74 68 69 73 20 68 65 61 64 65 72 20 61  ng this header a
11c0d 6e 64 20 2a 70 44 62 53 69 7a 65 20 69 73 20 73  nd *pDbSize is s
11c0e 65 74 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f  et to the size o
11c0f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  f the.** databas
11c10 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61  e before the tra
11c11 6e 73 61 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20  nsaction began, 
11c12 69 6e 20 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20  in pages. Also, 
11c13 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
11c14 74 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74  t.** is set to t
11c15 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  he value read fr
11c16 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  om the journal h
11c17 65 61 64 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b  eader. SQLITE_OK
11c18 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20   is returned.** 
11c19 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
11c1a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e  .** If the journ
11c1b 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 20 61  al header file a
11c1c 70 70 65 61 72 73 20 74 6f 20 62 65 20 63 6f 72  ppears to be cor
11c1d 72 75 70 74 65 64 2c 20 53 51 4c 49 54 45 5f 44  rupted, SQLITE_D
11c1e 4f 4e 45 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  ONE is.** return
11c1f 65 64 20 61 6e 64 20 2a 70 4e 52 65 63 20 61 6e  ed and *pNRec an
11c20 64 20 2a 50 44 62 53 69 7a 65 20 61 72 65 20 75  d *PDbSize are u
11c21 6e 64 65 66 69 6e 65 64 2e 20 20 49 66 20 4a 4f  ndefined.  If JO
11c22 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74  URNAL_HDR_SZ byt
11c23 65 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20  es.** cannot be 
11c24 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f  read from the jo
11c25 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72  urnal file an er
11c26 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
11c27 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
11c28 69 6e 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48  int readJournalH
11c29 64 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61  dr(.  Pager *pPa
11c2a 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  ger,            
11c2b 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65     /* Pager obje
11c2c 63 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 48 6f  ct */.  int isHo
11c2d 74 2c 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c  t,.  i64 journal
11c2e 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20  Size,           
11c2f 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
11c30 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69   open journal fi
11c31 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  le in bytes */. 
11c32 20 75 33 32 20 2a 70 4e 52 65 63 2c 20 20 20 20   u32 *pNRec,    
11c33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11c34 20 4f 55 54 3a 20 56 61 6c 75 65 20 72 65 61 64   OUT: Value read
11c35 20 66 72 6f 6d 20 74 68 65 20 6e 52 65 63 20 66   from the nRec f
11c36 69 65 6c 64 20 2a 2f 0a 20 20 75 33 32 20 2a 70  ield */.  u32 *p
11c37 44 62 53 69 7a 65 20 20 20 20 20 20 20 20 20 20  DbSize          
11c38 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56         /* OUT: V
11c39 61 6c 75 65 20 6f 66 20 6f 72 69 67 69 6e 61 6c  alue of original
11c3a 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 66   database size f
11c3b 69 65 6c 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ield */.){.  int
11c3c 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
11c3d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
11c3e 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 6e  urn code */.  un
11c3f 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67  signed char aMag
11c40 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 41 20  ic[8];     /* A 
11c41 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74  buffer to hold t
11c42 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72 20  he magic header 
11c43 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66  */.  i64 iHdrOff
11c44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11c45 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 6a    /* Offset of j
11c46 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 62 65  ournal header be
11c47 69 6e 67 20 72 65 61 64 20 2a 2f 0a 0a 20 20 61  ing read */..  a
11c48 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
11c49 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 20  ager->jfd) );   
11c4a 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 69     /* Journal fi
11c4b 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e  le must be open.
11c4c 20 2a 2f 0a 0a 20 20 2f 2a 20 41 64 76 61 6e 63   */..  /* Advanc
11c4d 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f  e Pager.journalO
11c4e 66 66 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  ff to the start 
11c4f 6f 66 20 74 68 65 20 6e 65 78 74 20 73 65 63 74  of the next sect
11c50 6f 72 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a 20  or. If the.  ** 
11c51 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
11c52 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68  too small for th
11c53 65 72 65 20 74 6f 20 62 65 20 61 20 68 65 61 64  ere to be a head
11c54 65 72 20 73 74 6f 72 65 64 20 61 74 20 74 68 69  er stored at thi
11c55 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 2c 20 72 65  s.  ** point, re
11c56 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
11c57 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d  ..  */.  pPager-
11c58 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f  >journalOff = jo
11c59 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70  urnalHdrOffset(p
11c5a 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50  Pager);.  if( pP
11c5b 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11c5c 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  +JOURNAL_HDR_SZ(
11c5d 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61  pPager) > journa
11c5e 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74  lSize ){.    ret
11c5f 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
11c60 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d  .  }.  iHdrOff =
11c61 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11c62 4f 66 66 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20  Off;..  /* Read 
11c63 69 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 62  in the first 8 b
11c64 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72  ytes of the jour
11c65 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66 20 74  nal header. If t
11c66 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68  hey do not match
11c67 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67 69 63  .  ** the  magic
11c68 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20 61 74   string found at
11c69 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 61   the start of ea
11c6a 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  ch journal heade
11c6b 72 2c 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 53  r, return.  ** S
11c6c 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66 20 61  QLITE_DONE. If a
11c6d 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
11c6e 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  s, return an err
11c6f 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77 69  or code. Otherwi
11c70 73 65 2c 0a 20 20 2a 2a 20 70 72 6f 63 65 65 64  se,.  ** proceed
11c71 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 48  ..  */.  if( isH
11c72 6f 74 20 7c 7c 20 69 48 64 72 4f 66 66 21 3d 70  ot || iHdrOff!=p
11c73 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
11c74 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  r ){.    rc = sq
11c75 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67  lite3OsRead(pPag
11c76 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c  er->jfd, aMagic,
11c77 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c   sizeof(aMagic),
11c78 20 69 48 64 72 4f 66 66 29 3b 0a 20 20 20 20 69   iHdrOff);.    i
11c79 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
11c7a 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
11c7b 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 61      if( memcmp(a
11c7c 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d  Magic, aJournalM
11c7d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61  agic, sizeof(aMa
11c7e 67 69 63 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  gic))!=0 ){.    
11c7f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
11c80 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  DONE;.    }.  }.
11c81 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 66  .  /* Read the f
11c82 69 72 73 74 20 74 68 72 65 65 20 33 32 2d 62 69  irst three 32-bi
11c83 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  t fields of the 
11c84 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 3a 20  journal header: 
11c85 54 68 65 20 6e 52 65 63 0a 20 20 2a 2a 20 66 69  The nRec.  ** fi
11c86 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b 73 75  eld, the checksu
11c87 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20 61 6e  m-initializer an
11c88 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
11c89 69 7a 65 20 61 74 20 74 68 65 20 73 74 61 72 74  ize at the start
11c8a 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 72 61  .  ** of the tra
11c8b 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75 72 6e  nsaction. Return
11c8c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
11c8d 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20  f anything goes 
11c8e 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66  wrong..  */.  if
11c8f 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
11c90 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50   = read32bits(pP
11c91 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f  ager->jfd, iHdrO
11c92 66 66 2b 38 2c 20 70 4e 52 65 63 29 29 0a 20 20  ff+8, pNRec)).  
11c93 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
11c94 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
11c95 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
11c96 72 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 65 72  rOff+12, &pPager
11c97 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 29 0a 20 20  ->cksumInit)).  
11c98 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
11c99 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
11c9a 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
11c9b 72 4f 66 66 2b 31 36 2c 20 70 44 62 53 69 7a 65  rOff+16, pDbSize
11c9c 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75  )).  ){.    retu
11c9d 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66  rn rc;.  }..  if
11c9e 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
11c9f 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 75  lOff==0 ){.    u
11ca0 33 32 20 69 50 61 67 65 53 69 7a 65 3b 20 20 20  32 iPageSize;   
11ca1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
11ca2 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f  age-size field o
11ca3 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  f journal header
11ca4 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 53 65 63   */.    u32 iSec
11ca5 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20  torSize;        
11ca6 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 2d 73       /* Sector-s
11ca7 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f 75  ize field of jou
11ca8 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20  rnal header */. 
11ca9 20 20 20 75 31 36 20 69 50 61 67 65 53 69 7a 65     u16 iPageSize
11caa 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  16;             
11cab 2f 2a 20 43 6f 70 79 20 6f 66 20 69 50 61 67 65  /* Copy of iPage
11cac 53 69 7a 65 20 69 6e 20 31 36 2d 62 69 74 20 76  Size in 16-bit v
11cad 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20  ariable */..    
11cae 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65  /* Read the page
11caf 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72  -size and sector
11cb0 2d 73 69 7a 65 20 6a 6f 75 72 6e 61 6c 20 68 65  -size journal he
11cb1 61 64 65 72 20 66 69 65 6c 64 73 2e 20 2a 2f 0a  ader fields. */.
11cb2 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
11cb3 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62  K!=(rc = read32b
11cb4 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
11cb5 20 69 48 64 72 4f 66 66 2b 32 30 2c 20 26 69 53   iHdrOff+20, &iS
11cb6 65 63 74 6f 72 53 69 7a 65 29 29 0a 20 20 20 20  ectorSize)).    
11cb7 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
11cb8 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
11cb9 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
11cba 72 4f 66 66 2b 32 34 2c 20 26 69 50 61 67 65 53  rOff+24, &iPageS
11cbb 69 7a 65 29 29 0a 20 20 20 20 29 7b 0a 20 20 20  ize)).    ){.   
11cbc 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
11cbd 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63    }..    /* Chec
11cbe 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65  k that the value
11cbf 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  s read from the 
11cc0 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65  page-size and se
11cc1 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 73  ctor-size fields
11cc2 0a 20 20 20 20 2a 2a 20 61 72 65 20 77 69 74 68  .    ** are with
11cc3 69 6e 20 72 61 6e 67 65 2e 20 54 6f 20 62 65 20  in range. To be 
11cc4 27 69 6e 20 72 61 6e 67 65 27 2c 20 62 6f 74 68  'in range', both
11cc5 20 76 61 6c 75 65 73 20 6e 65 65 64 20 74 6f 20   values need to 
11cc6 62 65 20 61 20 70 6f 77 65 72 0a 20 20 20 20 2a  be a power.    *
11cc7 2a 20 6f 66 20 74 77 6f 20 67 72 65 61 74 65 72  * of two greater
11cc8 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
11cc9 6f 20 35 31 32 2c 20 61 6e 64 20 6e 6f 74 20 67  o 512, and not g
11cca 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 69  reater than thei
11ccb 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 65 63  r .    ** respec
11ccc 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 69 6d  tive compile tim
11ccd 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 74 73  e maximum limits
11cce 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
11ccf 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 20 20   iPageSize<512  
11cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11cd1 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3c 35  || iSectorSize<5
11cd2 31 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67 65  12.     || iPage
11cd3 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  Size>SQLITE_MAX_
11cd4 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53 65  PAGE_SIZE || iSe
11cd5 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43  ctorSize>MAX_SEC
11cd6 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c 7c  TOR_SIZE.     ||
11cd7 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29 26   ((iPageSize-1)&
11cd8 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20 20  iPageSize)!=0   
11cd9 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a 65  || ((iSectorSize
11cda 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65 29  -1)&iSectorSize)
11cdb 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  !=0 .    ){.    
11cdc 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74 68    /* If the eith
11cdd 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  er the page-size
11cde 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65 20   or sector-size 
11cdf 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68  in the journal-h
11ce0 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20 20  eader is .      
11ce1 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65 6e  ** invalid, then
11ce2 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61   the process tha
11ce3 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75 72  t wrote the jour
11ce4 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74 20  nal-header must 
11ce5 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20 63  have .      ** c
11ce6 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74 68  rashed before th
11ce7 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79 6e  e header was syn
11ce8 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ced. In this cas
11ce9 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20 0a  e stop reading .
11cea 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75        ** the jou
11ceb 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 0a  rnal file here..
11cec 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
11ced 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
11cee 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  E;.    }..    /*
11cef 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67 65   Update the page
11cf0 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74  -size to match t
11cf1 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  he value read fr
11cf2 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  om the journal. 
11cf3 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74 65  .    ** Use a te
11cf4 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 74  stcase() macro t
11cf5 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
11cf6 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20   malloc failure 
11cf7 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 50  within .    ** P
11cf8 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28  agerSetPagesize(
11cf9 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20 20  ) is tested..   
11cfa 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 69 7a   */.    iPageSiz
11cfb 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 67 65  e16 = (u16)iPage
11cfc 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73  Size;.    rc = s
11cfd 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
11cfe 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26  gesize(pPager, &
11cff 69 50 61 67 65 53 69 7a 65 31 36 2c 20 2d 31 29  iPageSize16, -1)
11d00 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
11d01 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
11d02 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21  .    assert( rc!
11d03 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 50  =SQLITE_OK || iP
11d04 61 67 65 53 69 7a 65 31 36 3d 3d 28 75 31 36 29  ageSize16==(u16)
11d05 69 50 61 67 65 53 69 7a 65 20 29 3b 0a 0a 20 20  iPageSize );..  
11d06 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
11d07 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73  assumed sector-s
11d08 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65  ize to match the
11d09 20 76 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a   value used by .
11d0a 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65      ** the proce
11d0b 73 73 20 74 68 61 74 20 63 72 65 61 74 65 64 20  ss that created 
11d0c 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66  this journal. If
11d0d 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61   this journal wa
11d0e 73 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64  s.    ** created
11d0f 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74   by a process ot
11d10 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e  her than this on
11d11 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  e, then this rou
11d12 74 69 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20 62  tine.    ** is b
11d13 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d  eing called from
11d14 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 70 6c   within pager_pl
11d15 61 79 62 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f  ayback(). The lo
11d16 63 61 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a  cal value.    **
11d17 20 6f 66 20 50 61 67 65 72 2e 73 65 63 74 6f 72   of Pager.sector
11d18 53 69 7a 65 20 69 73 20 72 65 73 74 6f 72 65 64  Size is restored
11d19 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
11d1a 68 61 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20 20  hat routine..   
11d1b 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   */.    pPager->
11d1c 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 65  sectorSize = iSe
11d1d 63 74 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20  ctorSize;.  }.. 
11d1e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11d1f 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48  Off += JOURNAL_H
11d20 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20  DR_SZ(pPager);. 
11d21 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
11d22 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  /*.** Write the 
11d23 73 75 70 70 6c 69 65 64 20 6d 61 73 74 65 72 20  supplied master 
11d24 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74  journal name int
11d25 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
11d26 6c 65 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a 20  le for pager.** 
11d27 70 50 61 67 65 72 20 61 74 20 74 68 65 20 63 75  pPager at the cu
11d28 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 20  rrent location. 
11d29 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  The master journ
11d2a 61 6c 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20  al name must be 
11d2b 74 68 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69 6e  the last.** thin
11d2c 67 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20 6a  g written to a j
11d2d 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
11d2e 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
11d2f 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20  full-sync mode, 
11d30 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  the.** journal f
11d31 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
11d32 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20 74 68  s advanced to th
11d33 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 62 6f  e next sector bo
11d34 75 6e 64 61 72 79 20 62 65 66 6f 72 65 0a 2a 2a  undary before.**
11d35 20 61 6e 79 74 68 69 6e 67 20 69 73 20 77 72 69   anything is wri
11d36 74 74 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61 74  tten. The format
11d37 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 34   is:.**.**   + 4
11d38 20 62 79 74 65 73 3a 20 50 41 47 45 52 5f 4d 4a   bytes: PAGER_MJ
11d39 5f 50 47 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e 20  _PGNO..**   + N 
11d3a 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f  bytes: Master jo
11d3b 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69  urnal filename i
11d3c 6e 20 75 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b 20  n utf-8..**   + 
11d3d 34 20 62 79 74 65 73 3a 20 4e 20 28 6c 65 6e 67  4 bytes: N (leng
11d3e 74 68 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75  th of master jou
11d3f 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 20 62 79 74  rnal name in byt
11d40 65 73 2c 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69  es, no nul-termi
11d41 6e 61 74 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20 34  nator)..**   + 4
11d42 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a   bytes: Master j
11d43 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63  ournal name chec
11d44 6b 73 75 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20 62  ksum..**   + 8 b
11d45 79 74 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61  ytes: aJournalMa
11d46 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  gic[]..**.** The
11d47 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11d48 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 69 73  page checksum is
11d49 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20   the sum of the 
11d4a 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61 73  bytes in the mas
11d4b 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e  ter.** journal n
11d4c 61 6d 65 2c 20 77 68 65 72 65 20 65 61 63 68 20  ame, where each 
11d4d 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 65  byte is interpre
11d4e 74 65 64 20 61 73 20 61 20 73 69 67 6e 65 64 20  ted as a signed 
11d4f 38 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a  8-bit integer..*
11d50 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72 20  *.** If zMaster 
11d51 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  is a NULL pointe
11d52 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61 20  r (occurs for a 
11d53 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20  single database 
11d54 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a 2a  transaction), .*
11d55 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61  * this call is a
11d56 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
11d57 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74 65  c int writeMaste
11d58 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a  rJournal(Pager *
11d59 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68  pPager, const ch
11d5a 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20  ar *zMaster){.  
11d5b 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
11d5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d5d 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
11d5e 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65 72  */.  int nMaster
11d5f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11d60 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
11d61 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65  of string zMaste
11d62 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f  r */.  i64 iHdrO
11d63 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ff;             
11d64 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
11d65 74 20 6f 66 20 68 65 61 64 65 72 20 69 6e 20 6a  t of header in j
11d66 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20  ournal file */. 
11d67 20 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 20 20   i64 jrnlSize;  
11d68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d69 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f 75    /* Size of jou
11d6a 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73  rnal file on dis
11d6b 6b 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d  k */.  u32 cksum
11d6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
11d6d 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b          /* Check
11d6e 73 75 6d 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d  sum of string zM
11d6f 61 73 74 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20  aster */..  if( 
11d70 21 7a 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 67  !zMaster || pPag
11d71 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 0a 20 20  er->setMaster.  
11d72 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   || pPager->jour
11d73 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
11d74 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
11d75 59 20 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 2d  Y .   || pPager-
11d76 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
11d77 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
11d78 4f 46 46 20 0a 20 20 29 7b 0a 20 20 20 20 72 65  OFF .  ){.    re
11d79 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
11d7a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 65    }.  pPager->se
11d7b 74 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 61  tMaster = 1;.  a
11d7c 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
11d7d 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20  ager->jfd) );.. 
11d7e 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68   /* Calculate th
11d7f 65 20 6c 65 6e 67 74 68 20 69 6e 20 62 79 74 65  e length in byte
11d80 73 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b 73  s and the checks
11d81 75 6d 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a 2f  um of zMaster */
11d82 0a 20 20 66 6f 72 28 6e 4d 61 73 74 65 72 3d 30  .  for(nMaster=0
11d83 3b 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65  ; zMaster[nMaste
11d84 72 5d 3b 20 6e 4d 61 73 74 65 72 2b 2b 29 7b 0a  r]; nMaster++){.
11d85 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61      cksum += zMa
11d86 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a 20  ster[nMaster];. 
11d87 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66   }..  /* If in f
11d88 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61  ull-sync mode, a
11d89 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65  dvance to the ne
11d8a 78 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20 62  xt disk sector b
11d8b 65 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 20  efore writing.  
11d8c 2a 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  ** the master jo
11d8d 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73  urnal name. This
11d8e 20 69 73 20 69 6e 20 63 61 73 65 20 74 68 65 20   is in case the 
11d8f 70 72 65 76 69 6f 75 73 20 70 61 67 65 20 77 72  previous page wr
11d90 69 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68  itten to.  ** th
11d91 65 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c  e journal has al
11d92 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65  ready been synce
11d93 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  d..  */.  if( pP
11d94 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29  ager->fullSync )
11d95 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
11d96 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e  urnalOff = journ
11d97 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67  alHdrOffset(pPag
11d98 65 72 29 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f  er);.  }.  iHdrO
11d99 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ff = pPager->jou
11d9a 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 57  rnalOff;..  /* W
11d9b 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20  rite the master 
11d9c 6a 6f 75 72 6e 61 6c 20 64 61 74 61 20 74 6f 20  journal data to 
11d9d 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a  the end of the j
11d9e 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 0a  ournal file. If.
11d9f 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63    ** an error oc
11da0 63 75 72 73 2c 20 72 65 74 75 72 6e 20 74 68 65  curs, return the
11da1 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 74   error code to t
11da2 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f 0a  he caller..  */.
11da3 20 20 69 66 28 20 28 30 20 21 3d 20 28 72 63 20    if( (0 != (rc 
11da4 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50  = write32bits(pP
11da5 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f  ager->jfd, iHdrO
11da6 66 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  ff, PAGER_MJ_PGN
11da7 4f 28 70 50 61 67 65 72 29 29 29 29 0a 20 20 20  O(pPager)))).   
11da8 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73  || (0 != (rc = s
11da9 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
11daa 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74  ager->jfd, zMast
11dab 65 72 2c 20 6e 4d 61 73 74 65 72 2c 20 69 48 64  er, nMaster, iHd
11dac 72 4f 66 66 2b 34 29 29 29 0a 20 20 20 7c 7c 20  rOff+4))).   || 
11dad 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74  (0 != (rc = writ
11dae 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  e32bits(pPager->
11daf 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e  jfd, iHdrOff+4+n
11db0 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 29  Master, nMaster)
11db1 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28  )).   || (0 != (
11db2 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73  rc = write32bits
11db3 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48  (pPager->jfd, iH
11db4 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b  drOff+4+nMaster+
11db5 34 2c 20 63 6b 73 75 6d 29 29 29 0a 20 20 20 7c  4, cksum))).   |
11db6 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71  | (0 != (rc = sq
11db7 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61  lite3OsWrite(pPa
11db8 67 65 72 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72 6e  ger->jfd, aJourn
11db9 61 6c 4d 61 67 69 63 2c 20 38 2c 20 69 48 64 72  alMagic, 8, iHdr
11dba 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 38 29  Off+4+nMaster+8)
11dbb 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75  )).  ){.    retu
11dbc 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 61  rn rc;.  }.  pPa
11dbd 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
11dbe 2b 3d 20 28 6e 4d 61 73 74 65 72 2b 32 30 29 3b  += (nMaster+20);
11dbf 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  .  pPager->needS
11dc0 79 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e 6e  ync = !pPager->n
11dc1 6f 53 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 20  oSync;..  /* If 
11dc2 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
11dc3 70 65 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61  peristent-journa
11dc4 6c 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65  l mode, then the
11dc5 20 70 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a 20   physical .  ** 
11dc6 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 79  journal-file may
11dc7 20 65 78 74 65 6e 64 20 70 61 73 74 20 74 68 65   extend past the
11dc8 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74   end of the mast
11dc9 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a  er-journal name.
11dca 20 20 2a 2a 20 61 6e 64 20 38 20 62 79 74 65 73    ** and 8 bytes
11dcb 20 6f 66 20 6d 61 67 69 63 20 64 61 74 61 20 6a   of magic data j
11dcc 75 73 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74  ust written to t
11dcd 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73  he file. This is
11dce 20 0a 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75 73   .  ** dangerous
11dcf 20 62 65 63 61 75 73 65 20 74 68 65 20 63 6f 64   because the cod
11dd0 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20  e to rollback a 
11dd1 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  hot-journal file
11dd2 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62  .  ** will not b
11dd3 65 20 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 74  e able to find t
11dd4 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61  he master-journa
11dd5 6c 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d  l name to determ
11dd6 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65 74 68 65  ine .  ** whethe
11dd7 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75  r or not the jou
11dd8 72 6e 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20 20  rnal is hot. .  
11dd9 2a 2a 0a 20 20 2a 2a 20 45 61 73 69 65 73 74 20  **.  ** Easiest 
11dda 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20 74  thing to do in t
11ddb 68 69 73 20 73 63 65 6e 61 72 69 6f 20 69 73 20  his scenario is 
11ddc 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 20  to truncate the 
11ddd 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69  journal .  ** fi
11dde 6c 65 20 74 6f 20 74 68 65 20 72 65 71 75 69 72  le to the requir
11ddf 65 64 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20  ed size..  */ . 
11de0 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
11de1 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46  (rc = sqlite3OsF
11de2 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e  ileSize(pPager->
11de3 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 29  jfd, &jrnlSize))
11de4 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65 3e  .   && jrnlSize>
11de5 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11de6 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d  ff.  ){.    rc =
11de7 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
11de8 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
11de9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11dea 66 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ff);.  }.  retur
11deb 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  n rc;.}../*.** F
11dec 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 68  ind a page in th
11ded 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 76  e hash table giv
11dee 65 6e 20 69 74 73 20 70 61 67 65 20 6e 75 6d 62  en its page numb
11def 65 72 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20  er. Return.** a 
11df0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
11df1 61 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  age or NULL if t
11df2 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67  he requested pag
11df3 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c 72  e is not .** alr
11df4 65 61 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a  eady in memory..
11df5 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20  */.static PgHdr 
11df6 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 61  *pager_lookup(Pa
11df7 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e  ger *pPager, Pgn
11df8 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72  o pgno){.  PgHdr
11df9 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   *p;            
11dfa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11dfb 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0a  Return value */.
11dfc 0a 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20  .  /* It is not 
11dfd 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20 63  possible for a c
11dfe 61 6c 6c 20 74 6f 20 50 63 61 63 68 65 46 65 74  all to PcacheFet
11dff 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 65  ch() with create
11e00 46 6c 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a 20  Flag==0 to.  ** 
11e01 66 61 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20 61  fail, since no a
11e02 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61  ttempt to alloca
11e03 74 65 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72  te dynamic memor
11e04 79 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e 0a  y will be made..
11e05 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c    */.  (void)sql
11e06 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28  ite3PcacheFetch(
11e07 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c  pPager->pPCache,
11e08 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a 20   pgno, 0, &p);. 
11e09 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
11e0a 0a 2a 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 70  .** Unless the p
11e0b 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72  ager is in error
11e0c 2d 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 20  -state, discard 
11e0d 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61  all in-memory pa
11e0e 67 65 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 70  ges. If.** the p
11e0f 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72  ager is in error
11e10 2d 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68 69  -state, then thi
11e11 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f  s call is a no-o
11e12 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 57  p..**.** TODO: W
11e13 68 79 20 63 61 6e 20 77 65 20 6e 6f 74 20 72 65  hy can we not re
11e14 73 65 74 20 74 68 65 20 70 61 67 65 72 20 77 68  set the pager wh
11e15 69 6c 65 20 69 6e 20 65 72 72 6f 72 20 73 74 61  ile in error sta
11e16 74 65 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  te?.*/.static vo
11e17 69 64 20 70 61 67 65 72 5f 72 65 73 65 74 28 50  id pager_reset(P
11e18 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
11e19 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
11e1a 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
11e1b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 61  ){.    sqlite3Ba
11e1c 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61 67  ckupRestart(pPag
11e1d 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20  er->pBackup);.  
11e1e 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43    sqlite3PcacheC
11e1f 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 50 43  lear(pPager->pPC
11e20 61 63 68 65 29 3b 0a 20 20 20 20 70 50 61 67 65  ache);.    pPage
11e21 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d  r->dbSizeValid =
11e22 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
11e23 20 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63 74   Free all struct
11e24 75 72 65 73 20 69 6e 20 74 68 65 20 50 61 67 65  ures in the Page
11e25 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61  r.aSavepoint[] a
11e26 72 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f 74  rray and set bot
11e27 68 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76 65  h.** Pager.aSave
11e28 70 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72 2e  point and Pager.
11e29 6e 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a 65  nSavepoint to ze
11e2a 72 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73 75  ro. Close the su
11e2b 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 20  b-journal.** if 
11e2c 69 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74  it is open and t
11e2d 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20  he pager is not 
11e2e 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  in exclusive mod
11e2f 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
11e30 64 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65  d releaseAllSave
11e31 70 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70 50  points(Pager *pP
11e32 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69 3b  ager){.  int ii;
11e33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11e34 2a 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20 6c  * Iterator for l
11e35 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 50  ooping through P
11e36 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20  ager.aSavepoint 
11e37 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  */.  for(ii=0; i
11e38 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  i<pPager->nSavep
11e39 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  oint; ii++){.   
11e3a 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
11e3b 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 53  stroy(pPager->aS
11e3c 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e  avepoint[ii].pIn
11e3d 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a  Savepoint);.  }.
11e3e 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65    if( !pPager->e
11e3f 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20  xclusiveMode || 
11e40 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72  sqlite3IsMemJour
11e41 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  nal(pPager->sjfd
11e42 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
11e43 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
11e44 73 6a 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  sjfd);.  }.  sql
11e45 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 72  ite3_free(pPager
11e46 2d 3e 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20  ->aSavepoint);. 
11e47 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f   pPager->aSavepo
11e48 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65  int = 0;.  pPage
11e49 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20  r->nSavepoint = 
11e4a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 75  0;.  pPager->nSu
11e4b 62 52 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  bRec = 0;.}../*.
11e4c 2a 2a 20 53 65 74 20 74 68 65 20 62 69 74 20 6e  ** Set the bit n
11e4d 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 74 68  umber pgno in th
11e4e 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  e PagerSavepoint
11e4f 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a 2a  .pInSavepoint .*
11e50 2a 20 62 69 74 76 65 63 73 20 6f 66 20 61 6c 6c  * bitvecs of all
11e51 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73   open savepoints
11e52 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  . Return SQLITE_
11e53 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  OK if successful
11e54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f  .** or SQLITE_NO
11e55 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20  MEM if a malloc 
11e56 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a  failure occurs..
11e57 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64  */.static int ad
11e58 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76  dToSavepointBitv
11e59 65 63 73 28 50 61 67 65 72 20 2a 70 50 61 67 65  ecs(Pager *pPage
11e5a 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20  r, Pgno pgno){. 
11e5b 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20   int ii;        
11e5c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
11e5d 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
11e5e 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
11e5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73  OK;       /* Res
11e60 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 66  ult code */..  f
11e61 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67  or(ii=0; ii<pPag
11e62 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20  er->nSavepoint; 
11e63 69 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72  ii++){.    Pager
11e64 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26  Savepoint *p = &
11e65 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69  pPager->aSavepoi
11e66 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20  nt[ii];.    if( 
11e67 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 29  pgno<=p->nOrig )
11e68 7b 0a 20 20 20 20 20 20 72 63 20 7c 3d 20 73 71  {.      rc |= sq
11e69 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70  lite3BitvecSet(p
11e6a 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20  ->pInSavepoint, 
11e6b 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65 73  pgno);.      tes
11e6c 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54  tcase( rc==SQLIT
11e6d 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20  E_NOMEM );.     
11e6e 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
11e6f 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51  ITE_OK || rc==SQ
11e70 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20  LITE_NOMEM );.  
11e71 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
11e72 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e   rc;.}../*.** Un
11e73 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73  lock the databas
11e74 65 20 66 69 6c 65 2e 20 54 68 69 73 20 66 75 6e  e file. This fun
11e75 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
11e76 20 69 66 20 74 68 65 20 70 61 67 65 72 0a 2a 2a   if the pager.**
11e77 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65   is in exclusive
11e78 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   mode..**.** If 
11e79 74 68 65 20 70 61 67 65 72 20 69 73 20 63 75 72  the pager is cur
11e7a 72 65 6e 74 6c 79 20 69 6e 20 65 72 72 6f 72 20  rently in error 
11e7b 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 20 74  state, discard t
11e7c 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 0a  he contents of .
11e7d 2a 2a 20 74 68 65 20 63 61 63 68 65 20 61 6e 64  ** the cache and
11e7e 20 72 65 73 65 74 20 74 68 65 20 50 61 67 65 72   reset the Pager
11e7f 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 65 72   structure inter
11e80 6e 61 6c 20 73 74 61 74 65 2e 20 49 66 20 74 68  nal state. If th
11e81 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 6f 70 65  ere is.** an ope
11e82 6e 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 2c 20  n journal-file, 
11e83 74 68 65 6e 20 74 68 65 20 6e 65 78 74 20 74 69  then the next ti
11e84 6d 65 20 61 20 73 68 61 72 65 64 2d 6c 6f 63 6b  me a shared-lock
11e85 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20   is obtained.** 
11e86 6f 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 6c  on the pager fil
11e87 65 20 28 62 79 20 74 68 69 73 20 6f 72 20 61 6e  e (by this or an
11e88 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 29  y other process)
11e89 2c 20 69 74 20 77 69 6c 6c 20 62 65 0a 2a 2a 20  , it will be.** 
11e8a 74 72 65 61 74 65 64 20 61 73 20 61 20 68 6f 74  treated as a hot
11e8b 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 72 6f 6c  -journal and rol
11e8c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61  led back..*/.sta
11e8d 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 75  tic void pager_u
11e8e 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61  nlock(Pager *pPa
11e8f 67 65 72 29 7b 0a 20 20 69 66 28 20 21 70 50 61  ger){.  if( !pPa
11e90 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
11e91 64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  de ){.    int rc
11e92 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11e93 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
11e94 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a   code */..    /*
11e95 20 41 6c 77 61 79 73 20 63 6c 6f 73 65 20 74 68   Always close th
11e96 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77  e journal file w
11e97 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 68 65  hen dropping the
11e98 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e 0a   database lock..
11e99 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65      ** Otherwise
11e9a 2c 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63  , another connec
11e9b 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e 61  tion with journa
11e9c 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d 69  l_mode=delete mi
11e9d 67 68 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74  ght.    ** delet
11e9e 65 20 74 68 65 20 66 69 6c 65 20 6f 75 74 20 66  e the file out f
11e9f 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20 20  rom under us..  
11ea0 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
11ea1 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
11ea2 6a 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  jfd);.    sqlite
11ea3 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
11ea4 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
11ea5 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  l);.    pPager->
11ea6 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a  pInJournal = 0;.
11ea7 20 20 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61      releaseAllSa
11ea8 76 65 70 6f 69 6e 74 73 28 70 50 61 67 65 72 29  vepoints(pPager)
11ea9 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ;..    /* If the
11eaa 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65   file is unlocke
11eab 64 2c 20 73 6f 6d 65 62 6f 64 79 20 65 6c 73 65  d, somebody else
11eac 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 74   might change it
11ead 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c  . The.    ** val
11eae 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 50 61  ues stored in Pa
11eaf 67 65 72 2e 64 62 53 69 7a 65 20 65 74 63 2e 20  ger.dbSize etc. 
11eb0 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 69 6e 76  might become inv
11eb1 61 6c 69 64 20 69 66 0a 20 20 20 20 2a 2a 20 74  alid if.    ** t
11eb2 68 69 73 20 68 61 70 70 65 6e 73 2e 20 54 4f 44  his happens. TOD
11eb3 4f 3a 20 52 65 61 6c 6c 79 2c 20 74 68 69 73 20  O: Really, this 
11eb4 64 6f 65 73 6e 27 74 20 6e 65 65 64 20 74 6f 20  doesn't need to 
11eb5 62 65 20 63 6c 65 61 72 65 64 0a 20 20 20 20 2a  be cleared.    *
11eb6 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 68 61 6e  * until the chan
11eb7 67 65 2d 63 6f 75 6e 74 65 72 20 63 68 65 63 6b  ge-counter check
11eb8 20 66 61 69 6c 73 20 69 6e 20 50 61 67 65 72 53   fails in PagerS
11eb9 68 61 72 65 64 4c 6f 63 6b 28 29 2e 0a 20 20 20  haredLock()..   
11eba 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   */.    pPager->
11ebb 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b  dbSizeValid = 0;
11ebc 0a 0a 20 20 20 20 72 63 20 3d 20 6f 73 55 6e 6c  ..    rc = osUnl
11ebd 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ock(pPager->fd, 
11ebe 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  NO_LOCK);.    if
11ebf 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 70 50  ( rc ){.      pP
11ec0 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20  ager->errCode = 
11ec1 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f  rc;.    }.    IO
11ec2 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 25  TRACE(("UNLOCK %
11ec3 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a  p\n", pPager))..
11ec4 20 20 20 20 2f 2a 20 49 66 20 50 61 67 65 72 2e      /* If Pager.
11ec5 65 72 72 43 6f 64 65 20 69 73 20 73 65 74 2c 20  errCode is set, 
11ec6 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
11ec7 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20  the pager cache 
11ec8 63 61 6e 6e 6f 74 20 62 65 0a 20 20 20 20 2a 2a  cannot be.    **
11ec9 20 74 72 75 73 74 65 64 2e 20 4e 6f 77 20 74 68   trusted. Now th
11eca 61 74 20 74 68 65 20 70 61 67 65 72 20 66 69 6c  at the pager fil
11ecb 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 74  e is unlocked, t
11ecc 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
11ecd 68 65 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 20  he.    ** cache 
11ece 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65 64  can be discarded
11ecf 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63   and the error c
11ed0 6f 64 65 20 73 61 66 65 6c 79 20 63 6c 65 61 72  ode safely clear
11ed1 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ed..    */.    i
11ed2 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  f( pPager->errCo
11ed3 64 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  de ){.      if( 
11ed4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
11ed5 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
11ed6 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54  >errCode = SQLIT
11ed7 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  E_OK;.      }.  
11ed8 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28      pager_reset(
11ed9 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a  pPager);.    }..
11eda 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e      pPager->chan
11edb 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b  geCountDone = 0;
11edc 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61  .    pPager->sta
11edd 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43  te = PAGER_UNLOC
11ede 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  K;.  }.}../*.** 
11edf 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68  This function sh
11ee0 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77  ould be called w
11ee1 68 65 6e 20 61 6e 20 49 4f 45 52 52 2c 20 43 4f  hen an IOERR, CO
11ee2 52 52 55 50 54 20 6f 72 20 46 55 4c 4c 20 65 72  RRUPT or FULL er
11ee3 72 6f 72 0a 2a 2a 20 6d 61 79 20 68 61 76 65 20  ror.** may have 
11ee4 6f 63 63 75 72 72 65 64 2e 20 54 68 65 20 66 69  occurred. The fi
11ee5 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
11ee6 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
11ee7 20 70 61 67 65 72 20 0a 2a 2a 20 73 74 72 75 63   pager .** struc
11ee8 74 75 72 65 2c 20 74 68 65 20 73 65 63 6f 6e 64  ture, the second
11ee9 20 74 68 65 20 65 72 72 6f 72 2d 63 6f 64 65 20   the error-code 
11eea 61 62 6f 75 74 20 74 6f 20 62 65 20 72 65 74 75  about to be retu
11eeb 72 6e 65 64 20 62 79 20 61 20 70 61 67 65 72 20  rned by a pager 
11eec 0a 2a 2a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  .** API function
11eed 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  . The value retu
11eee 72 6e 65 64 20 69 73 20 61 20 63 6f 70 79 20 6f  rned is a copy o
11eef 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
11ef0 75 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 74 68 69  ument .** to thi
11ef1 73 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a  s function. .**.
11ef2 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64  ** If the second
11ef3 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c   argument is SQL
11ef4 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54  ITE_IOERR, SQLIT
11ef5 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 20 53 51  E_CORRUPT, or SQ
11ef6 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65  LITE_FULL.** the
11ef7 20 65 72 72 6f 72 20 62 65 63 6f 6d 65 73 20 70   error becomes p
11ef8 65 72 73 69 73 74 65 6e 74 2e 20 55 6e 74 69 6c  ersistent. Until
11ef9 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 20 65   the persisten e
11efa 72 72 6f 72 20 69 73 20 63 6c 65 61 72 65 64 2c  rror is cleared,
11efb 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20 41  .** subsequent A
11efc 50 49 20 63 61 6c 6c 73 20 6f 6e 20 74 68 69 73  PI calls on this
11efd 20 50 61 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65   Pager will imme
11efe 64 69 61 74 65 6c 79 20 72 65 74 75 72 6e 20 74  diately return t
11eff 68 65 20 73 61 6d 65 20 0a 2a 2a 20 65 72 72 6f  he same .** erro
11f00 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20  r code..**.** A 
11f01 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72  persistent error
11f02 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
11f03 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
11f04 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20  the pager-cache 
11f05 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 74 72  .** cannot be tr
11f06 75 73 74 65 64 2e 20 54 68 69 73 20 73 74 61 74  usted. This stat
11f07 65 20 63 61 6e 20 62 65 20 63 6c 65 61 72 65 64  e can be cleared
11f08 20 62 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64   by completely d
11f09 69 73 63 61 72 64 69 6e 67 20 0a 2a 2a 20 74 68  iscarding .** th
11f0a 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
11f0b 65 20 70 61 67 65 72 2d 63 61 63 68 65 2e 20 49  e pager-cache. I
11f0c 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
11f0d 77 61 73 20 61 63 74 69 76 65 20 77 68 65 6e 0a  was active when.
11f0e 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 65 6e  ** the persisten
11f0f 74 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  t error occurred
11f10 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62  , then the rollb
11f11 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20  ack journal may 
11f12 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 72 65  need.** to be re
11f13 70 6c 61 79 65 64 20 74 6f 20 72 65 73 74 6f 72  played to restor
11f14 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  e the contents o
11f15 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
11f16 69 6c 65 20 28 61 73 20 69 66 0a 2a 2a 20 69 74  ile (as if.** it
11f17 20 77 65 72 65 20 61 20 68 6f 74 2d 6a 6f 75 72   were a hot-jour
11f18 6e 61 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nal)..*/.static 
11f19 69 6e 74 20 70 61 67 65 72 5f 65 72 72 6f 72 28  int pager_error(
11f1a 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
11f1b 6e 74 20 72 63 29 7b 0a 20 20 69 6e 74 20 72 63  nt rc){.  int rc
11f1c 32 20 3d 20 72 63 20 26 20 30 78 66 66 3b 0a 20  2 = rc & 0xff;. 
11f1d 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
11f1e 49 54 45 5f 4f 4b 20 7c 7c 20 21 4d 45 4d 44 42  ITE_OK || !MEMDB
11f1f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 0a 20 20   );.  assert(.  
11f20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72       pPager->err
11f21 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46 55 4c  Code==SQLITE_FUL
11f22 4c 20 7c 7c 0a 20 20 20 20 20 20 20 70 50 61 67  L ||.       pPag
11f23 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c  er->errCode==SQL
11f24 49 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20 20 20 20  ITE_OK ||.      
11f25 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64   (pPager->errCod
11f26 65 20 26 20 30 78 66 66 29 3d 3d 53 51 4c 49 54  e & 0xff)==SQLIT
11f27 45 5f 49 4f 45 52 52 0a 20 20 29 3b 0a 20 20 69  E_IOERR.  );.  i
11f28 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 46  f( rc2==SQLITE_F
11f29 55 4c 4c 20 7c 7c 20 72 63 32 3d 3d 53 51 4c 49  ULL || rc2==SQLI
11f2a 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20  TE_IOERR ){.    
11f2b 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
11f2c 3d 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75  = rc;.  }.  retu
11f2d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
11f2e 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c 62 61  Execute a rollba
11f2f 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61 63 74  ck if a transact
11f30 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e  ion is active an
11f31 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a 2a  d unlock the .**
11f32 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
11f33 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
11f34 67 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20  ger has already 
11f35 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f  entered the erro
11f36 72 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 20  r state, do not 
11f37 61 74 74 65 6d 70 74 20 0a 2a 2a 20 74 68 65 20  attempt .** the 
11f38 72 6f 6c 6c 62 61 63 6b 20 61 74 20 74 68 69 73  rollback at this
11f39 20 74 69 6d 65 2e 20 49 6e 73 74 65 61 64 2c 20   time. Instead, 
11f3a 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69  pager_unlock() i
11f3b 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 0a 2a 2a  s called. The.**
11f3c 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75   call to pager_u
11f3d 6e 6c 6f 63 6b 28 29 20 77 69 6c 6c 20 64 69 73  nlock() will dis
11f3e 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f  card all in-memo
11f3f 72 79 20 70 61 67 65 73 2c 20 75 6e 6c 6f 63 6b  ry pages, unlock
11f40 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
11f41 20 66 69 6c 65 20 61 6e 64 20 63 6c 65 61 72 20   file and clear 
11f42 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e  the error state.
11f43 20 49 66 20 74 68 69 73 20 6d 65 61 6e 73 20 74   If this means t
11f44 68 61 74 0a 2a 2a 20 74 68 65 72 65 20 69 73 20  hat.** there is 
11f45 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 6c 65  a hot-journal le
11f46 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73  ft in the file-s
11f47 79 73 74 65 6d 2c 20 74 68 65 20 6e 65 78 74 20  ystem, the next 
11f48 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 74 6f  connection.** to
11f49 20 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65 64   obtain a shared
11f4a 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 70 61 67   lock on the pag
11f4b 65 72 20 28 77 68 69 63 68 20 6d 61 79 20 62 65  er (which may be
11f4c 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 0a   this one) will.
11f4d 2a 2a 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e  ** roll it back.
11f4e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
11f4f 67 65 72 20 68 61 73 20 6e 6f 74 20 61 6c 72 65  ger has not alre
11f50 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 20  ady entered the 
11f51 65 72 72 6f 72 20 73 74 61 74 65 2c 20 62 75 74  error state, but
11f52 20 61 6e 20 49 4f 20 6f 72 0a 2a 2a 20 6d 61 6c   an IO or.** mal
11f53 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73  loc error occurs
11f54 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61   during a rollba
11f55 63 6b 2c 20 74 68 65 6e 20 74 68 69 73 20 77 69  ck, then this wi
11f56 6c 6c 20 69 74 73 65 6c 66 20 63 61 75 73 65 20  ll itself cause 
11f57 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 20 74 6f  .** the pager to
11f58 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72   enter the error
11f59 20 73 74 61 74 65 2e 20 57 68 69 63 68 20 77 69   state. Which wi
11f5a 6c 6c 20 62 65 20 63 6c 65 61 72 65 64 20 62 79  ll be cleared by
11f5b 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20   the.** call to 
11f5c 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 2c 20  pager_unlock(), 
11f5d 61 73 20 64 65 73 63 72 69 62 65 64 20 61 62 6f  as described abo
11f5e 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ve..*/.static vo
11f5f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e  id pagerUnlockAn
11f60 64 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20  dRollback(Pager 
11f61 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20  *pPager){.  if( 
11f62 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d  pPager->errCode=
11f63 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50  =SQLITE_OK && pP
11f64 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
11f65 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20  ER_RESERVED ){. 
11f66 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42     sqlite3BeginB
11f67 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
11f68 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52     sqlite3PagerR
11f69 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b  ollback(pPager);
11f6a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42  .    sqlite3EndB
11f6b 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
11f6c 20 7d 0a 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63   }.  pager_unloc
11f6d 6b 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a  k(pPager);.}../*
11f6e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
11f6f 20 65 6e 64 73 20 61 20 74 72 61 6e 73 61 63 74   ends a transact
11f70 69 6f 6e 2e 20 41 20 74 72 61 6e 73 61 63 74 69  ion. A transacti
11f71 6f 6e 20 69 73 20 75 73 75 61 6c 6c 79 20 65 6e  on is usually en
11f72 64 65 64 20 62 79 20 0a 2a 2a 20 65 69 74 68 65  ded by .** eithe
11f73 72 20 61 20 43 4f 4d 4d 49 54 20 6f 72 20 61 20  r a COMMIT or a 
11f74 52 4f 4c 4c 42 41 43 4b 20 6f 70 65 72 61 74 69  ROLLBACK operati
11f75 6f 6e 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65  on. This routine
11f76 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 0a   may be called .
11f77 2a 2a 20 61 66 74 65 72 20 72 6f 6c 6c 62 61 63  ** after rollbac
11f78 6b 20 6f 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  k of a hot-journ
11f79 61 6c 2c 20 6f 72 20 69 66 20 61 6e 20 65 72 72  al, or if an err
11f7a 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
11f7b 6f 70 65 6e 69 6e 67 0a 2a 2a 20 74 68 65 20 6a  opening.** the j
11f7c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 77  ournal file or w
11f7d 72 69 74 69 6e 67 20 74 68 65 20 76 65 72 79 20  riting the very 
11f7e 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 2d 68 65  first journal-he
11f7f 61 64 65 72 20 6f 66 20 61 0a 2a 2a 20 64 61 74  ader of a.** dat
11f80 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f  abase transactio
11f81 6e 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65  n..** .** If the
11f82 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 41 47   pager is in PAG
11f83 45 52 5f 53 48 41 52 45 44 20 6f 72 20 50 41 47  ER_SHARED or PAG
11f84 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 20  ER_UNLOCK state 
11f85 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75  when this.** rou
11f86 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
11f87 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 20 28 72  it is a no-op (r
11f88 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
11f89 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  )..**.** Otherwi
11f8a 73 65 2c 20 61 6e 79 20 61 63 74 69 76 65 20 73  se, any active s
11f8b 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 72 65  avepoints are re
11f8c 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  leased..**.** If
11f8d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11f8e 65 20 69 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20  e is open, then 
11f8f 69 74 20 69 73 20 22 66 69 6e 61 6c 69 7a 65 64  it is "finalized
11f90 22 2e 20 4f 6e 63 65 20 61 20 6a 6f 75 72 6e 61  ". Once a journa
11f91 6c 20 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20 62  l .** file has b
11f92 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 20 69 74  een finalized it
11f93 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
11f94 20 74 6f 20 75 73 65 20 69 74 20 74 6f 20 72 6f   to use it to ro
11f95 6c 6c 20 62 61 63 6b 20 61 20 0a 2a 2a 20 74 72  ll back a .** tr
11f96 61 6e 73 61 63 74 69 6f 6e 2e 20 4e 6f 72 20 77  ansaction. Nor w
11f97 69 6c 6c 20 69 74 20 62 65 20 63 6f 6e 73 69 64  ill it be consid
11f98 65 72 65 64 20 74 6f 20 62 65 20 61 20 68 6f 74  ered to be a hot
11f99 2d 6a 6f 75 72 6e 61 6c 20 62 79 20 74 68 69 73  -journal by this
11f9a 0a 2a 2a 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  .** or any other
11f9b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
11f9c 74 69 6f 6e 2e 20 45 78 61 63 74 6c 79 20 68 6f  tion. Exactly ho
11f9d 77 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 66  w a journal is f
11f9e 69 6e 61 6c 69 7a 65 64 0a 2a 2a 20 64 65 70 65  inalized.** depe
11f9f 6e 64 73 20 6f 6e 20 77 68 65 74 68 65 72 20 6f  nds on whether o
11fa0 72 20 6e 6f 74 20 74 68 65 20 70 61 67 65 72 20  r not the pager 
11fa1 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78  is running in ex
11fa2 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e 64  clusive mode and
11fa3 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20  .** the current 
11fa4 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 28 50 61  journal-mode (Pa
11fa5 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20  ger.journalMode 
11fa6 76 61 6c 75 65 29 2c 20 61 73 20 66 6f 6c 6c 6f  value), as follo
11fa7 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72  ws:.**.**   jour
11fa8 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59 0a  nalMode==MEMORY.
11fa9 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66  **     Journal f
11faa 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
11fab 73 20 73 69 6d 70 6c 79 20 63 6c 6f 73 65 64 2e  s simply closed.
11fac 20 54 68 69 73 20 64 65 73 74 72 6f 79 73 20 61   This destroys a
11fad 6e 20 0a 2a 2a 20 20 20 20 20 69 6e 2d 6d 65 6d  n .**     in-mem
11fae 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a  ory journal..**.
11faf 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65  **   journalMode
11fb0 3d 3d 54 52 55 4e 43 41 54 45 0a 2a 2a 20 20 20  ==TRUNCATE.**   
11fb1 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69    Journal file i
11fb2 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a  s truncated to z
11fb3 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 7a  ero bytes in siz
11fb4 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e  e..**.**   journ
11fb5 61 6c 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54 0a  alMode==PERSIST.
11fb6 2a 2a 20 20 20 20 20 54 68 65 20 66 69 72 73 74  **     The first
11fb7 20 32 38 20 62 79 74 65 73 20 6f 66 20 74 68 65   28 bytes of the
11fb8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 72   journal file ar
11fb9 65 20 7a 65 72 6f 65 64 2e 20 54 68 69 73 20 69  e zeroed. This i
11fba 6e 76 61 6c 69 64 61 74 65 73 0a 2a 2a 20 20 20  nvalidates.**   
11fbb 20 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72    the first jour
11fbc 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 74 68  nal header in th
11fbd 65 20 66 69 6c 65 2c 20 61 6e 64 20 68 65 6e 63  e file, and henc
11fbe 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f 75  e the entire jou
11fbf 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65  rnal.**     file
11fc0 2e 20 41 6e 20 69 6e 76 61 6c 69 64 20 6a 6f 75  . An invalid jou
11fc1 72 6e 61 6c 20 66 69 6c 65 20 63 61 6e 6e 6f 74  rnal file cannot
11fc2 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   be rolled back.
11fc3 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c  .**.**   journal
11fc4 4d 6f 64 65 3d 3d 44 45 4c 45 54 45 0a 2a 2a 20  Mode==DELETE.** 
11fc5 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20      The journal 
11fc6 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 61  file is closed a
11fc7 6e 64 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67  nd deleted using
11fc8 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
11fc9 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 66  ()..**.**     If
11fca 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75   the pager is ru
11fcb 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69  nning in exclusi
11fcc 76 65 20 6d 6f 64 65 2c 20 74 68 69 73 20 6d 65  ve mode, this me
11fcd 74 68 6f 64 20 6f 66 20 66 69 6e 61 6c 69 7a 69  thod of finalizi
11fce 6e 67 0a 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f  ng.**     the jo
11fcf 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 65  urnal file is ne
11fd0 76 65 72 20 75 73 65 64 2e 20 49 6e 73 74 65 61  ver used. Instea
11fd1 64 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61  d, if the journa
11fd2 6c 4d 6f 64 65 20 69 73 0a 2a 2a 20 20 20 20 20  lMode is.**     
11fd3 44 45 4c 45 54 45 20 61 6e 64 20 74 68 65 20 70  DELETE and the p
11fd4 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75  ager is in exclu
11fd5 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 20 6d  sive mode, the m
11fd6 65 74 68 6f 64 20 64 65 73 63 72 69 62 65 64 20  ethod described 
11fd7 75 6e 64 65 72 0a 2a 2a 20 20 20 20 20 6a 6f 75  under.**     jou
11fd8 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 53 49 53  rnalMode==PERSIS
11fd9 54 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 61  T is used instea
11fda 64 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74  d..**.** After t
11fdb 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 66 69  he journal is fi
11fdc 6e 61 6c 69 7a 65 64 2c 20 69 66 20 72 75 6e 6e  nalized, if runn
11fdd 69 6e 67 20 69 6e 20 6e 6f 6e 2d 65 78 63 6c 75  ing in non-exclu
11fde 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 0a 2a  sive mode, the.*
11fdf 2a 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f  * pager moves to
11fe0 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 74   PAGER_SHARED st
11fe1 61 74 65 20 28 61 6e 64 20 64 6f 77 6e 67 72 61  ate (and downgra
11fe2 64 65 73 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20  des the lock on 
11fe3 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
11fe4 66 69 6c 65 20 61 63 63 6f 72 64 69 6e 67 6c 79  file accordingly
11fe5 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  )..**.** If the 
11fe6 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67  pager is running
11fe7 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
11fe8 64 65 20 61 6e 64 20 69 73 20 69 6e 20 50 41 47  de and is in PAG
11fe9 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65 2c  ER_SYNCED state,
11fea 0a 2a 2a 20 69 74 20 6d 6f 76 65 73 20 74 6f 20  .** it moves to 
11feb 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 2e  PAGER_EXCLUSIVE.
11fec 20 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 64 6f   No locks are do
11fed 77 6e 67 72 61 64 65 64 20 77 68 65 6e 20 72 75  wngraded when ru
11fee 6e 6e 69 6e 67 20 69 6e 0a 2a 2a 20 65 78 63 6c  nning in.** excl
11fef 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a  usive mode..**.*
11ff0 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
11ff1 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 65 72  eturned if no er
11ff2 72 6f 72 20 6f 63 63 75 72 73 2e 20 49 66 20 61  ror occurs. If a
11ff3 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  n error occurs d
11ff4 75 72 69 6e 67 0a 2a 2a 20 61 6e 79 20 6f 66 20  uring.** any of 
11ff5 74 68 65 20 49 4f 20 6f 70 65 72 61 74 69 6f 6e  the IO operation
11ff6 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68  s to finalize th
11ff7 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  e journal file o
11ff8 72 20 75 6e 6c 6f 63 6b 20 74 68 65 0a 2a 2a 20  r unlock the.** 
11ff9 64 61 74 61 62 61 73 65 20 74 68 65 6e 20 74 68  database then th
11ffa 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  e IO error code 
11ffb 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
11ffc 68 65 20 75 73 65 72 2e 20 49 66 20 74 68 65 20  he user. If the 
11ffd 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f  .** operation to
11ffe 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f   finalize the jo
11fff 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73  urnal file fails
12000 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 64 65 20  , then the code 
12001 73 74 69 6c 6c 0a 2a 2a 20 74 72 69 65 73 20 74  still.** tries t
12002 6f 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74  o unlock the dat
12003 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 6e 6f  abase file if no
12004 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  t in exclusive m
12005 6f 64 65 2e 20 49 66 20 74 68 65 0a 2a 2a 20 75  ode. If the.** u
12006 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20  nlock operation 
12007 66 61 69 6c 73 20 61 73 20 77 65 6c 6c 2c 20 74  fails as well, t
12008 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 65 72  hen the first er
12009 72 6f 72 20 63 6f 64 65 20 72 65 6c 61 74 65 64  ror code related
1200a 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74  .** to the first
1200b 20 65 72 72 6f 72 20 65 6e 63 6f 75 6e 74 65 72   error encounter
1200c 65 64 20 28 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ed (the journal 
1200d 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 6e 65  finalization one
1200e 29 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  ) is.** returned
1200f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
12010 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61  pager_end_transa
12011 63 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61  ction(Pager *pPa
12012 67 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73 74  ger, int hasMast
12013 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  er){.  int rc = 
12014 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
12015 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72  /* Error code fr
12016 6f 6d 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c  om journal final
12017 69 7a 61 74 69 6f 6e 20 6f 70 65 72 61 74 69 6f  ization operatio
12018 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 32 20 3d  n */.  int rc2 =
12019 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
1201a 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72  /* Error code fr
1201b 6f 6d 20 64 62 20 66 69 6c 65 20 75 6e 6c 6f 63  om db file unloc
1201c 6b 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a  k operation */..
1201d 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
1201e 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52 56  ate<PAGER_RESERV
1201f 45 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ED ){.    return
12020 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
12021 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65    releaseAllSave
12022 70 6f 69 6e 74 73 28 70 50 61 67 65 72 29 3b 0a  points(pPager);.
12023 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65  .  assert( isOpe
12024 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c  n(pPager->jfd) |
12025 7c 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  | pPager->pInJou
12026 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28  rnal==0 );.  if(
12027 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
12028 6a 66 64 29 20 29 7b 0a 0a 20 20 20 20 2f 2a 20  jfd) ){..    /* 
12029 46 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75  Finalize the jou
1202a 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20  rnal file. */.  
1202b 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4d    if( sqlite3IsM
1202c 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  emJournal(pPager
1202d 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20  ->jfd) ){.      
1202e 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
1202f 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
12030 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d  ER_JOURNALMODE_M
12031 45 4d 4f 52 59 20 29 3b 0a 20 20 20 20 20 20 73  EMORY );.      s
12032 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
12033 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20  ager->jfd);.    
12034 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72  }else if( pPager
12035 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
12036 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
12037 5f 54 52 55 4e 43 41 54 45 20 29 7b 0a 20 20 20  _TRUNCATE ){.   
12038 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a     if( pPager->j
12039 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a  ournalOff==0 ){.
1203a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1203b 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 65  ITE_OK;.      }e
1203c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20  lse{.        rc 
1203d 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
1203e 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ate(pPager->jfd,
1203f 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
12040 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
12041 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20  alOff = 0;.     
12042 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12043 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20  Started = 0;.   
12044 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65   }else if( pPage
12045 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
12046 20 0a 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72   .     || pPager
12047 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
12048 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
12049 5f 50 45 52 53 49 53 54 0a 20 20 20 20 29 7b 0a  _PERSIST.    ){.
1204a 20 20 20 20 20 20 72 63 20 3d 20 7a 65 72 6f 4a        rc = zeroJ
1204b 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72  ournalHdr(pPager
1204c 2c 20 68 61 73 4d 61 73 74 65 72 29 3b 0a 20 20  , hasMaster);.  
1204d 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28      pager_error(
1204e 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20  pPager, rc);.   
1204f 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
12050 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20  alOff = 0;.     
12051 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12052 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20  Started = 0;.   
12053 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
12054 20 54 68 69 73 20 62 72 61 6e 63 68 20 6d 61 79   This branch may
12055 20 62 65 20 65 78 65 63 75 74 65 64 20 77 69 74   be executed wit
12056 68 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d  h Pager.journalM
12057 6f 64 65 3d 3d 4d 45 4d 4f 52 59 20 69 66 0a 20  ode==MEMORY if. 
12058 20 20 20 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f       ** a hot-jo
12059 75 72 6e 61 6c 20 77 61 73 20 6a 75 73 74 20 72  urnal was just r
1205a 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 6e 20 74  olled back. In t
1205b 68 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75  his case the jou
1205c 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69  rnal.      ** fi
1205d 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f  le should be clo
1205e 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 2e  sed and deleted.
1205f 20 49 66 20 74 68 69 73 20 63 6f 6e 6e 65 63 74   If this connect
12060 69 6f 6e 20 77 72 69 74 65 73 20 74 6f 0a 20 20  ion writes to.  
12061 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62      ** the datab
12062 61 73 65 20 66 69 6c 65 2c 20 69 74 20 77 69 6c  ase file, it wil
12063 6c 20 64 6f 20 73 6f 20 75 73 69 6e 67 20 61 6e  l do so using an
12064 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   in-memory journ
12065 61 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 61 73  al.  */.      as
12066 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f  sert( pPager->jo
12067 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
12068 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c  _JOURNALMODE_DEL
12069 45 54 45 20 0a 20 20 20 20 20 20 20 20 20 20 20  ETE .           
1206a 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  || pPager->journ
1206b 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
1206c 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
1206d 20 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20   .      );.     
1206e 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
1206f 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20  pPager->jfd);.  
12070 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d      if( !pPager-
12071 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20  >tempFile ){.   
12072 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
12073 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72  3OsDelete(pPager
12074 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  ->pVfs, pPager->
12075 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20  zJournal, 0);.  
12076 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66      }.    }..#if
12077 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  def SQLITE_CHECK
12078 5f 50 41 47 45 53 0a 20 20 20 20 73 71 6c 69 74  _PAGES.    sqlit
12079 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44  e3PcacheIterateD
1207a 69 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43  irty(pPager->pPC
1207b 61 63 68 65 2c 20 70 61 67 65 72 5f 73 65 74 5f  ache, pager_set_
1207c 70 61 67 65 68 61 73 68 29 3b 0a 23 65 6e 64 69  pagehash);.#endi
1207d 66 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  f..    sqlite3Pc
1207e 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61  acheCleanAll(pPa
1207f 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20  ger->pPCache);. 
12080 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63     sqlite3Bitvec
12081 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e  Destroy(pPager->
12082 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20  pInJournal);.   
12083 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
12084 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  nal = 0;.    pPa
12085 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20  ger->nRec = 0;. 
12086 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65   }..  if( !pPage
12087 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
12088 20 29 7b 0a 20 20 20 20 72 63 32 20 3d 20 6f 73   ){.    rc2 = os
12089 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66  Unlock(pPager->f
1208a 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b  d, SHARED_LOCK);
1208b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61  .    pPager->sta
1208c 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45  te = PAGER_SHARE
1208d 44 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63  D;.    pPager->c
1208e 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d  hangeCountDone =
1208f 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
12090 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
12091 41 47 45 52 5f 53 59 4e 43 45 44 20 29 7b 0a 20  AGER_SYNCED ){. 
12092 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65     pPager->state
12093 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49   = PAGER_EXCLUSI
12094 56 45 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72  VE;.  }.  pPager
12095 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b  ->setMaster = 0;
12096 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  .  pPager->needS
12097 79 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61 67 65  ync = 0;.  pPage
12098 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20  r->dbModified = 
12099 30 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49  0;..  /* TODO: I
1209a 73 20 74 68 69 73 20 6f 70 74 69 6d 61 6c 3f 20  s this optimal? 
1209b 57 68 79 20 69 73 20 74 68 65 20 64 62 20 73 69  Why is the db si
1209c 7a 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 68  ze invalidated h
1209d 65 72 65 20 0a 20 20 2a 2a 20 77 68 65 6e 20 74  ere .  ** when t
1209e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1209f 20 69 73 20 6e 6f 74 20 75 6e 6c 6f 63 6b 65 64   is not unlocked
120a0 3f 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64  ? */.  pPager->d
120a1 62 4f 72 69 67 53 69 7a 65 20 3d 20 30 3b 0a 20  bOrigSize = 0;. 
120a2 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72   sqlite3PcacheTr
120a3 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 70  uncate(pPager->p
120a4 50 43 61 63 68 65 2c 20 70 50 61 67 65 72 2d 3e  PCache, pPager->
120a5 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 21  dbSize);.  if( !
120a6 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 61  MEMDB ){.    pPa
120a7 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
120a8 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74   = 0;.  }..  ret
120a9 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  urn (rc==SQLITE_
120aa 4f 4b 3f 72 63 32 3a 72 63 29 3b 0a 7d 0a 0a 2f  OK?rc2:rc);.}../
120ab 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 61  *.** Parameter a
120ac 44 61 74 61 20 6d 75 73 74 20 70 6f 69 6e 74 20  Data must point 
120ad 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 70  to a buffer of p
120ae 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20  Pager->pageSize 
120af 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61  bytes.** of data
120b0 2e 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 72 65  . Compute and re
120b1 74 75 72 6e 20 61 20 63 68 65 63 6b 73 75 6d 20  turn a checksum 
120b2 62 61 73 65 64 20 6f 6e 74 20 74 68 65 20 63 6f  based ont the co
120b3 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 0a 2a  ntents of the .*
120b4 2a 20 70 61 67 65 20 6f 66 20 64 61 74 61 20 61  * page of data a
120b5 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  nd the current v
120b6 61 6c 75 65 20 6f 66 20 70 50 61 67 65 72 2d 3e  alue of pPager->
120b7 63 6b 73 75 6d 49 6e 69 74 2e 0a 2a 2a 0a 2a 2a  cksumInit..**.**
120b8 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20 72   This is not a r
120b9 65 61 6c 20 63 68 65 63 6b 73 75 6d 2e 20 49 74  eal checksum. It
120ba 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20   is really just 
120bb 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 0a  the sum of the .
120bc 2a 2a 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61  ** random initia
120bd 6c 20 76 61 6c 75 65 20 28 70 50 61 67 65 72 2d  l value (pPager-
120be 3e 63 6b 73 75 6d 49 6e 69 74 29 20 61 6e 64 20  >cksumInit) and 
120bf 65 76 65 72 79 20 32 30 30 74 68 20 62 79 74 65  every 200th byte
120c0 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20  .** of the page 
120c1 64 61 74 61 2c 20 73 74 61 72 74 69 6e 67 20 77  data, starting w
120c2 69 74 68 20 62 79 74 65 20 6f 66 66 73 65 74 20  ith byte offset 
120c3 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  (pPager->pageSiz
120c4 65 25 32 30 30 29 2e 0a 2a 2a 20 45 61 63 68 20  e%200)..** Each 
120c5 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 65  byte is interpre
120c6 74 65 64 20 61 73 20 61 6e 20 38 2d 62 69 74 20  ted as an 8-bit 
120c7 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
120c8 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67  ..**.** Changing
120c9 20 74 68 65 20 66 6f 72 6d 75 6c 61 20 75 73 65   the formula use
120ca 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 69  d to compute thi
120cb 73 20 63 68 65 63 6b 73 75 6d 20 72 65 73 75 6c  s checksum resul
120cc 74 73 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f  ts in an.** inco
120cd 6d 70 61 74 69 62 6c 65 20 6a 6f 75 72 6e 61 6c  mpatible journal
120ce 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a   file format..**
120cf 0a 2a 2a 20 49 66 20 6a 6f 75 72 6e 61 6c 20 63  .** If journal c
120d0 6f 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73  orruption occurs
120d1 20 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20   due to a power 
120d2 66 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73  failure, the mos
120d3 74 20 6c 69 6b 65 6c 79 20 0a 2a 2a 20 73 63 65  t likely .** sce
120d4 6e 61 72 69 6f 20 69 73 20 74 68 61 74 20 6f 6e  nario is that on
120d5 65 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 68  e end or the oth
120d6 65 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  er of the record
120d7 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64   will be changed
120d8 2e 20 0a 2a 2a 20 49 74 20 69 73 20 6d 75 63 68  . .** It is much
120d9 20 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 61   less likely tha
120da 74 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20 6f  t the two ends o
120db 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65  f the journal re
120dc 63 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20  cord will be.** 
120dd 63 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65 20  correct and the 
120de 6d 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75 70  middle be corrup
120df 74 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20 22  t.  Thus, this "
120e0 63 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d 65  checksum" scheme
120e1 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 74  ,.** though fast
120e2 20 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 74   and simple, cat
120e3 63 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 20  ches the mostly 
120e4 6c 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63  likely kind of c
120e5 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  orruption..*/.st
120e6 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 63  atic u32 pager_c
120e7 6b 73 75 6d 28 50 61 67 65 72 20 2a 70 50 61 67  ksum(Pager *pPag
120e8 65 72 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 44  er, const u8 *aD
120e9 61 74 61 29 7b 0a 20 20 75 33 32 20 63 6b 73 75  ata){.  u32 cksu
120ea 6d 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 75  m = pPager->cksu
120eb 6d 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 2f  mInit;         /
120ec 2a 20 43 68 65 63 6b 73 75 6d 20 76 61 6c 75 65  * Checksum value
120ed 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20   to return */.  
120ee 69 6e 74 20 69 20 3d 20 70 50 61 67 65 72 2d 3e  int i = pPager->
120ef 70 61 67 65 53 69 7a 65 2d 32 30 30 3b 20 20 20  pageSize-200;   
120f0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
120f1 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 77 68 69 6c  ounter */.  whil
120f2 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 63 6b  e( i>0 ){.    ck
120f3 73 75 6d 20 2b 3d 20 61 44 61 74 61 5b 69 5d 3b  sum += aData[i];
120f4 0a 20 20 20 20 69 20 2d 3d 20 32 30 30 3b 0a 20  .    i -= 200;. 
120f5 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6b 73 75   }.  return cksu
120f6 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  m;.}../*.** Read
120f7 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 66   a single page f
120f8 72 6f 6d 20 65 69 74 68 65 72 20 74 68 65 20 6a  rom either the j
120f9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 69 66 20  ournal file (if 
120fa 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 31 29 20 6f  isMainJrnl==1) o
120fb 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 75  r.** from the su
120fc 62 2d 6a 6f 75 72 6e 61 6c 20 28 69 66 20 69 73  b-journal (if is
120fd 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 29 20 61 6e 64  MainJrnl==0) and
120fe 20 70 6c 61 79 62 61 63 6b 20 74 68 61 74 20 70   playback that p
120ff 61 67 65 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65  age..** The page
12100 20 62 65 67 69 6e 73 20 61 74 20 6f 66 66 73 65   begins at offse
12101 74 20 2a 70 4f 66 66 73 65 74 20 69 6e 74 6f 20  t *pOffset into 
12102 74 68 65 20 66 69 6c 65 2e 20 54 68 65 20 2a 70  the file. The *p
12103 4f 66 66 73 65 74 0a 2a 2a 20 76 61 6c 75 65 20  Offset.** value 
12104 69 73 20 69 6e 63 72 65 61 73 65 64 20 74 6f 20  is increased to 
12105 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
12106 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68   next page in th
12107 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a  e journal..**.**
12108 20 54 68 65 20 69 73 4d 61 69 6e 4a 72 6e 6c 20   The isMainJrnl 
12109 66 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 20  flag is true if 
1210a 74 68 69 73 20 69 73 20 74 68 65 20 6d 61 69 6e  this is the main
1210b 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
1210c 6c 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 66  l and.** false f
1210d 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  or the statement
1210e 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d   journal.  The m
1210f 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  ain rollback jou
12110 72 6e 61 6c 20 75 73 65 73 0a 2a 2a 20 63 68 65  rnal uses.** che
12111 63 6b 73 75 6d 73 20 2d 20 74 68 65 20 73 74 61  cksums - the sta
12112 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 64  tement journal d
12113 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49  oes not..**.** I
12114 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  f the page numbe
12115 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 72 65  r of the page re
12116 63 6f 72 64 20 72 65 61 64 20 66 72 6f 6d 20 74  cord read from t
12117 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c  he (sub-)journal
12118 20 66 69 6c 65 0a 2a 2a 20 69 73 20 67 72 65 61   file.** is grea
12119 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72  ter than the cur
1211a 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 61  rent value of Pa
1211b 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 65 6e  ger.dbSize, then
1211c 20 70 6c 61 79 62 61 63 6b 20 69 73 0a 2a 2a 20   playback is.** 
1211d 73 6b 69 70 70 65 64 20 61 6e 64 20 53 51 4c 49  skipped and SQLI
1211e 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
1211f 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f 6e  d..**.** If pDon
12120 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  e is not NULL, t
12121 68 65 6e 20 69 74 20 69 73 20 61 20 72 65 63 6f  hen it is a reco
12122 72 64 20 6f 66 20 70 61 67 65 73 20 74 68 61 74  rd of pages that
12123 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a   have already.**
12124 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63   been played bac
12125 6b 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20  k.  If the page 
12126 61 74 20 2a 70 4f 66 66 73 65 74 20 68 61 73 20  at *pOffset has 
12127 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c 61  already been pla
12128 79 65 64 20 62 61 63 6b 0a 2a 2a 20 28 69 66 20  yed back.** (if 
12129 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
1212a 67 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20 73  g pDone bit is s
1212b 65 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74 68  et) then skip th
1212c 65 20 70 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20 4d  e playback..** M
1212d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 44 6f  ake sure the pDo
1212e 6e 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e  ne bit correspon
1212f 64 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f 66  ding to the *pOf
12130 66 73 65 74 20 70 61 67 65 20 69 73 20 73 65 74  fset page is set
12131 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74  .** prior to ret
12132 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66  urning..**.** If
12133 20 74 68 65 20 70 61 67 65 20 72 65 63 6f 72 64   the page record
12134 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79   is successfully
12135 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28   read from the (
12136 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c  sub-)journal fil
12137 65 0a 2a 2a 20 61 6e 64 20 70 6c 61 79 65 64 20  e.** and played 
12138 62 61 63 6b 2c 20 74 68 65 6e 20 53 51 4c 49 54  back, then SQLIT
12139 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
1213a 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  . If an IO error
1213b 20 6f 63 63 75 72 73 0a 2a 2a 20 77 68 69 6c 65   occurs.** while
1213c 20 72 65 61 64 69 6e 67 20 74 68 65 20 72 65 63   reading the rec
1213d 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75  ord from the (su
1213e 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  b-)journal file 
1213f 6f 72 20 77 68 69 6c 65 20 77 72 69 74 69 6e 67  or while writing
12140 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62  .** to the datab
12141 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 74  ase file, then t
12142 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  he IO error code
12143 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
12144 20 64 61 74 61 0a 2a 2a 20 69 73 20 73 75 63 63   data.** is succ
12145 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72  essfully read fr
12146 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75  om the (sub-)jou
12147 72 6e 61 6c 20 66 69 6c 65 20 62 75 74 20 61 70  rnal file but ap
12148 70 65 61 72 73 20 74 6f 20 62 65 0a 2a 2a 20 63  pears to be.** c
12149 6f 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54 45  orrupted, SQLITE
1214a 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65  _DONE is returne
1214b 64 2e 20 44 61 74 61 20 69 73 20 63 6f 6e 73 69  d. Data is consi
1214c 64 65 72 65 64 20 63 6f 72 72 75 70 74 65 64 20  dered corrupted 
1214d 69 6e 0a 2a 2a 20 74 77 6f 20 63 69 72 63 75 6d  in.** two circum
1214e 73 74 61 6e 63 65 73 3a 0a 2a 2a 20 0a 2a 2a 20  stances:.** .** 
1214f 20 20 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72    * If the recor
12150 64 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73  d page-number is
12151 20 69 6c 6c 65 67 61 6c 20 28 30 20 6f 72 20 50   illegal (0 or P
12152 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 29 2c 20 6f  AGER_MJ_PGNO), o
12153 72 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20  r.**   * If the 
12154 72 65 63 6f 72 64 20 69 73 20 62 65 69 6e 67 20  record is being 
12155 72 6f 6c 6c 65 64 20 62 61 63 6b 20 66 72 6f 6d  rolled back from
12156 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
12157 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 61 6e  l file.**     an
12158 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 66  d the checksum f
12159 69 65 6c 64 20 64 6f 65 73 20 6e 6f 74 20 6d 61  ield does not ma
1215a 74 63 68 20 74 68 65 20 72 65 63 6f 72 64 20 63  tch the record c
1215b 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 65  ontent..**.** Ne
1215c 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74  ither of these t
1215d 77 6f 20 73 63 65 6e 61 72 69 6f 73 20 61 72 65  wo scenarios are
1215e 20 70 6f 73 73 69 62 6c 65 20 64 75 72 69 6e 67   possible during
1215f 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c   a savepoint rol
12160 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  lback..**.** If 
12161 74 68 69 73 20 69 73 20 61 20 73 61 76 65 70 6f  this is a savepo
12162 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68  int rollback, th
12163 65 6e 20 6d 65 6d 6f 72 79 20 6d 61 79 20 68 61  en memory may ha
12164 76 65 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 63  ve to be dynamic
12165 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  ally.** allocate
12166 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
12167 6f 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20 74  on. If this is t
12168 68 65 20 63 61 73 65 20 61 6e 64 20 61 6e 20 61  he case and an a
12169 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
1216a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  .** SQLITE_NOMEM
1216b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
1216c 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
1216d 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
1216e 61 67 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50  age(.  Pager *pP
1216f 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  ager,           
12170 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
12171 72 20 62 65 69 6e 67 20 70 6c 61 79 65 64 20 62  r being played b
12172 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d  ack */.  int isM
12173 61 69 6e 4a 72 6e 6c 2c 20 20 20 20 20 20 20 20  ainJrnl,        
12174 20 20 20 20 20 20 20 2f 2a 20 31 20 2d 3e 20 6d         /* 1 -> m
12175 61 69 6e 20 6a 6f 75 72 6e 61 6c 2e 20 30 20 2d  ain journal. 0 -
12176 3e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 2a  > sub-journal. *
12177 2f 0a 20 20 69 6e 74 20 69 73 55 6e 73 79 6e 63  /.  int isUnsync
12178 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
12179 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 61    /* True if rea
1217a 64 69 6e 67 20 66 72 6f 6d 20 75 6e 73 79 6e 63  ding from unsync
1217b 65 64 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  ed main journal 
1217c 2a 2f 0a 20 20 69 36 34 20 2a 70 4f 66 66 73 65  */.  i64 *pOffse
1217d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
1217e 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20     /* Offset of 
1217f 72 65 63 6f 72 64 20 74 6f 20 70 6c 61 79 62 61  record to playba
12180 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 53 61  ck */.  int isSa
12181 76 65 70 6e 74 2c 20 20 20 20 20 20 20 20 20 20  vepnt,          
12182 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
12183 72 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f  r a savepoint ro
12184 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 42 69 74 76  llback */.  Bitv
12185 65 63 20 2a 70 44 6f 6e 65 20 20 20 20 20 20 20  ec *pDone       
12186 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74            /* Bit
12187 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72  vec of pages alr
12188 65 61 64 79 20 70 6c 61 79 65 64 20 62 61 63 6b  eady played back
12189 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
1218a 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 20 20  .  PgHdr *pPg;  
1218b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1218c 20 2f 2a 20 41 6e 20 65 78 69 73 74 69 6e 67 20   /* An existing 
1218d 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 68  page in the cach
1218e 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
1218f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
12190 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
12191 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 70 61 67   number of a pag
12192 65 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  e in journal */.
12193 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 20    u32 cksum;    
12194 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12195 2f 2a 20 43 68 65 63 6b 73 75 6d 20 75 73 65 64  /* Checksum used
12196 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 63   for sanity chec
12197 6b 69 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 61 44  king */.  u8 *aD
12198 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
12199 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f          /* Tempo
1219a 72 61 72 79 20 73 74 6f 72 61 67 65 20 66 6f 72  rary storage for
1219b 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 73   the page */.  s
1219c 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64  qlite3_file *jfd
1219d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1219e 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  The file descrip
1219f 74 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 75 72  tor for the jour
121a0 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61  nal file */..  a
121a1 73 73 65 72 74 28 20 28 69 73 4d 61 69 6e 4a 72  ssert( (isMainJr
121a2 6e 6c 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20  nl&~1)==0 );    
121a3 20 20 2f 2a 20 69 73 4d 61 69 6e 4a 72 6e 6c 20    /* isMainJrnl 
121a4 69 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61  is 0 or 1 */.  a
121a5 73 73 65 72 74 28 20 28 69 73 53 61 76 65 70 6e  ssert( (isSavepn
121a6 74 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 20  t&~1)==0 );     
121a7 20 20 2f 2a 20 69 73 53 61 76 65 70 6e 74 20 69    /* isSavepnt i
121a8 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73  s 0 or 1 */.  as
121a9 73 65 72 74 28 20 69 73 4d 61 69 6e 4a 72 6e 6c  sert( isMainJrnl
121aa 20 7c 7c 20 70 44 6f 6e 65 20 29 3b 20 20 20 20   || pDone );    
121ab 20 2f 2a 20 70 44 6f 6e 65 20 61 6c 77 61 79 73   /* pDone always
121ac 20 75 73 65 64 20 6f 6e 20 73 75 62 2d 6a 6f 75   used on sub-jou
121ad 72 6e 61 6c 73 20 2a 2f 0a 20 20 61 73 73 65 72  rnals */.  asser
121ae 74 28 20 69 73 53 61 76 65 70 6e 74 20 7c 7c 20  t( isSavepnt || 
121af 70 44 6f 6e 65 3d 3d 30 20 29 3b 20 20 20 2f 2a  pDone==0 );   /*
121b0 20 70 44 6f 6e 65 20 6e 65 76 65 72 20 75 73 65   pDone never use
121b1 64 20 6f 6e 20 6e 6f 6e 2d 73 61 76 65 70 6f 69  d on non-savepoi
121b2 6e 74 20 2a 2f 0a 0a 20 20 61 44 61 74 61 20 3d  nt */..  aData =
121b3 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 54   (u8*)pPager->pT
121b4 6d 70 53 70 61 63 65 3b 0a 20 20 61 73 73 65 72  mpSpace;.  asser
121b5 74 28 20 61 44 61 74 61 20 29 3b 20 20 20 20 20  t( aData );     
121b6 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72      /* Temp stor
121b7 61 67 65 20 6d 75 73 74 20 68 61 76 65 20 61 6c  age must have al
121b8 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63  ready been alloc
121b9 61 74 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65  ated */..  /* Re
121ba 61 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  ad the page numb
121bb 65 72 20 61 6e 64 20 70 61 67 65 20 64 61 74 61  er and page data
121bc 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
121bd 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 6c  l or sub-journal
121be 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 52 65 74 75  .  ** file. Retu
121bf 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  rn an error code
121c0 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69   to the caller i
121c1 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63  f an IO error oc
121c2 63 75 72 73 2e 0a 20 20 2a 2f 0a 20 20 6a 66 64  curs..  */.  jfd
121c3 20 3d 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 3f 20   = isMainJrnl ? 
121c4 70 50 61 67 65 72 2d 3e 6a 66 64 20 3a 20 70 50  pPager->jfd : pP
121c5 61 67 65 72 2d 3e 73 6a 66 64 3b 0a 20 20 72 63  ager->sjfd;.  rc
121c6 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a 66   = read32bits(jf
121c7 64 2c 20 2a 70 4f 66 66 73 65 74 2c 20 26 70 67  d, *pOffset, &pg
121c8 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  no);.  if( rc!=S
121c9 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
121ca 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c  n rc;.  rc = sql
121cb 69 74 65 33 4f 73 52 65 61 64 28 6a 66 64 2c 20  ite3OsRead(jfd, 
121cc 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70  aData, pPager->p
121cd 61 67 65 53 69 7a 65 2c 20 28 2a 70 4f 66 66 73  ageSize, (*pOffs
121ce 65 74 29 2b 34 29 3b 0a 20 20 69 66 28 20 72 63  et)+4);.  if( rc
121cf 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
121d0 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 4f 66 66  turn rc;.  *pOff
121d1 73 65 74 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70  set += pPager->p
121d2 61 67 65 53 69 7a 65 20 2b 20 34 20 2b 20 69 73  ageSize + 4 + is
121d3 4d 61 69 6e 4a 72 6e 6c 2a 34 3b 0a 0a 20 20 2f  MainJrnl*4;..  /
121d4 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  * Sanity checkin
121d5 67 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 20 20  g on the page.  
121d6 54 68 69 73 20 69 73 20 6d 6f 72 65 20 69 6d 70  This is more imp
121d7 6f 72 74 61 6e 74 20 74 68 61 74 20 49 20 6f 72  ortant that I or
121d8 69 67 69 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74 68  iginally.  ** th
121d9 6f 75 67 68 74 2e 20 20 49 66 20 61 20 70 6f 77  ought.  If a pow
121da 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72  er failure occur
121db 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72  s while the jour
121dc 6e 61 6c 20 69 73 20 62 65 69 6e 67 20 77 72 69  nal is being wri
121dd 74 74 65 6e 2c 0a 20 20 2a 2a 20 69 74 20 63 6f  tten,.  ** it co
121de 75 6c 64 20 63 61 75 73 65 20 69 6e 76 61 6c 69  uld cause invali
121df 64 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69  d data to be wri
121e0 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f  tten into the jo
121e1 75 72 6e 61 6c 2e 20 20 57 65 20 6e 65 65 64 20  urnal.  We need 
121e2 74 6f 0a 20 20 2a 2a 20 64 65 74 65 63 74 20 74  to.  ** detect t
121e3 68 69 73 20 69 6e 76 61 6c 69 64 20 64 61 74 61  his invalid data
121e4 20 28 77 69 74 68 20 68 69 67 68 20 70 72 6f 62   (with high prob
121e5 61 62 69 6c 69 74 79 29 20 61 6e 64 20 69 67 6e  ability) and ign
121e6 6f 72 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69  ore it..  */.  i
121e7 66 28 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67  f( pgno==0 || pg
121e8 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  no==PAGER_MJ_PGN
121e9 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20  O(pPager) ){.   
121ea 20 61 73 73 65 72 74 28 20 21 69 73 53 61 76 65   assert( !isSave
121eb 70 6e 74 20 29 3b 0a 20 20 20 20 72 65 74 75 72  pnt );.    retur
121ec 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
121ed 20 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 50   }.  if( pgno>(P
121ee 67 6e 6f 29 70 50 61 67 65 72 2d 3e 64 62 53 69  gno)pPager->dbSi
121ef 7a 65 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74  ze || sqlite3Bit
121f0 76 65 63 54 65 73 74 28 70 44 6f 6e 65 2c 20 70  vecTest(pDone, p
121f1 67 6e 6f 29 20 29 7b 0a 20 20 20 20 72 65 74 75  gno) ){.    retu
121f2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
121f3 7d 0a 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72  }.  if( isMainJr
121f4 6e 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72  nl ){.    rc = r
121f5 65 61 64 33 32 62 69 74 73 28 6a 66 64 2c 20 28  ead32bits(jfd, (
121f6 2a 70 4f 66 66 73 65 74 29 2d 34 2c 20 26 63 6b  *pOffset)-4, &ck
121f7 73 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63  sum);.    if( rc
121f8 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
121f9 20 20 69 66 28 20 21 69 73 53 61 76 65 70 6e 74    if( !isSavepnt
121fa 20 26 26 20 70 61 67 65 72 5f 63 6b 73 75 6d 28   && pager_cksum(
121fb 70 50 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d  pPager, aData)!=
121fc 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72  cksum ){.      r
121fd 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
121fe 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  E;.    }.  }..  
121ff 69 66 28 20 70 44 6f 6e 65 20 26 26 20 28 72 63  if( pDone && (rc
12200 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   = sqlite3Bitvec
12201 53 65 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29  Set(pDone, pgno)
12202 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  )!=SQLITE_OK ){.
12203 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
12204 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50   }..  assert( pP
12205 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
12206 45 52 5f 52 45 53 45 52 56 45 44 20 7c 7c 20 70  ER_RESERVED || p
12207 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
12208 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b  GER_EXCLUSIVE );
12209 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61  ..  /* If the pa
1220a 67 65 72 20 69 73 20 69 6e 20 52 45 53 45 52 56  ger is in RESERV
1220b 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 74  ED state, then t
1220c 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 63  here must be a c
1220d 6f 70 79 20 6f 66 20 74 68 69 73 0a 20 20 2a 2a  opy of this.  **
1220e 20 70 61 67 65 20 69 6e 20 74 68 65 20 70 61 67   page in the pag
1220f 65 72 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69  er cache. In thi
12210 73 20 63 61 73 65 20 6a 75 73 74 20 75 70 64 61  s case just upda
12211 74 65 20 74 68 65 20 70 61 67 65 72 20 63 61 63  te the pager cac
12212 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20 74 68 65  he,.  ** not the
12213 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
12214 54 68 65 20 70 61 67 65 20 69 73 20 6c 65 66 74  The page is left
12215 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20 69 6e   marked dirty in
12216 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a   this case..  **
12217 0a 20 20 2a 2a 20 41 6e 20 65 78 63 65 70 74 69  .  ** An excepti
12218 6f 6e 20 74 6f 20 74 68 65 20 61 62 6f 76 65 20  on to the above 
12219 72 75 6c 65 3a 20 49 66 20 74 68 65 20 64 61 74  rule: If the dat
1221a 61 62 61 73 65 20 69 73 20 69 6e 20 6e 6f 2d 73  abase is in no-s
1221b 79 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a 20 61 6e  ync mode.  ** an
1221c 64 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65  d a page is move
1221d 64 20 64 75 72 69 6e 67 20 61 6e 20 69 6e 63 72  d during an incr
1221e 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 74  emental vacuum t
1221f 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79  hen the page may
12220 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 69 6e 20  .  ** not be in 
12221 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e  the pager cache.
12222 20 4c 61 74 65 72 3a 20 69 66 20 61 20 6d 61 6c   Later: if a mal
12223 6c 6f 63 28 29 20 6f 72 20 49 4f 20 65 72 72 6f  loc() or IO erro
12224 72 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20 64 75  r occurs.  ** du
12225 72 69 6e 67 20 61 20 4d 6f 76 65 70 61 67 65 28  ring a Movepage(
12226 29 20 63 61 6c 6c 2c 20 74 68 65 6e 20 74 68 65  ) call, then the
12227 20 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65   page may not be
12228 20 69 6e 20 74 68 65 20 63 61 63 68 65 0a 20 20   in the cache.  
12229 2a 2a 20 65 69 74 68 65 72 2e 20 53 6f 20 74 68  ** either. So th
1222a 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 73 63  e condition desc
1222b 72 69 62 65 64 20 69 6e 20 74 68 65 20 61 62 6f  ribed in the abo
1222c 76 65 20 70 61 72 61 67 72 61 70 68 20 69 73 20  ve paragraph is 
1222d 6e 6f 74 0a 20 20 2a 2a 20 61 73 73 65 72 74 28  not.  ** assert(
1222e 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  )able..  **.  **
1222f 20 49 66 20 69 6e 20 45 58 43 4c 55 53 49 56 45   If in EXCLUSIVE
12230 20 73 74 61 74 65 2c 20 74 68 65 6e 20 77 65 20   state, then we 
12231 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65 72  update the pager
12232 20 63 61 63 68 65 20 69 66 20 69 74 20 65 78 69   cache if it exi
12233 73 74 73 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  sts.  ** and the
12234 20 6d 61 69 6e 20 66 69 6c 65 2e 20 54 68 65 20   main file. The 
12235 70 61 67 65 20 69 73 20 74 68 65 6e 20 6d 61 72  page is then mar
12236 6b 65 64 20 6e 6f 74 20 64 69 72 74 79 2e 0a 20  ked not dirty.. 
12237 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20   **.  ** Ticket 
12238 23 31 31 37 31 3a 20 20 54 68 65 20 73 74 61 74  #1171:  The stat
12239 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6d 69  ement journal mi
1223a 67 68 74 20 63 6f 6e 74 61 69 6e 20 70 61 67 65  ght contain page
1223b 20 63 6f 6e 74 65 6e 74 20 74 68 61 74 20 69 73   content that is
1223c 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20  .  ** different 
1223d 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 63 6f  from the page co
1223e 6e 74 65 6e 74 20 61 74 20 74 68 65 20 73 74 61  ntent at the sta
1223f 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61  rt of the transa
12240 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 69 73  ction..  ** This
12241 20 6f 63 63 75 72 73 20 77 68 65 6e 20 61 20 70   occurs when a p
12242 61 67 65 20 69 73 20 63 68 61 6e 67 65 64 20 70  age is changed p
12243 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72  rior to the star
12244 74 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74  t of a statement
12245 0a 20 20 2a 2a 20 74 68 65 6e 20 63 68 61 6e 67  .  ** then chang
12246 65 64 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20  ed again within 
12247 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  the statement.  
12248 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63  When rolling bac
12249 6b 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 74  k such a.  ** st
1224a 61 74 65 6d 65 6e 74 20 77 65 20 6d 75 73 74 20  atement we must 
1224b 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 65  not write to the
1224c 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61   original databa
1224d 73 65 20 75 6e 6c 65 73 73 20 77 65 20 6b 6e 6f  se unless we kno
1224e 77 0a 20 20 2a 2a 20 66 6f 72 20 63 65 72 74 61  w.  ** for certa
1224f 69 6e 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c  in that original
12250 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 61   page contents a
12251 72 65 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74  re synced into t
12252 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b  he main rollback
12253 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20  .  ** journal.  
12254 4f 74 68 65 72 77 69 73 65 2c 20 61 20 70 6f 77  Otherwise, a pow
12255 65 72 20 6c 6f 73 73 20 6d 69 67 68 74 20 6c 65  er loss might le
12256 61 76 65 20 6d 6f 64 69 66 69 65 64 20 64 61 74  ave modified dat
12257 61 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61  a in the.  ** da
12258 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68  tabase file with
12259 6f 75 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 20  out an entry in 
1225a 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
1225b 72 6e 61 6c 20 74 68 61 74 20 63 61 6e 0a 20 20  rnal that can.  
1225c 2a 2a 20 72 65 73 74 6f 72 65 20 74 68 65 20 64  ** restore the d
1225d 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f  atabase to its o
1225e 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e 20 20 54  riginal form.  T
1225f 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6d 75  wo conditions mu
12260 73 74 20 62 65 0a 20 20 2a 2a 20 6d 65 74 20 62  st be.  ** met b
12261 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f  efore writing to
12262 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
12263 6c 65 73 2e 20 28 31 29 20 74 68 65 20 64 61 74  les. (1) the dat
12264 61 62 61 73 65 20 6d 75 73 74 20 62 65 0a 20 20  abase must be.  
12265 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 32 29 20  ** locked.  (2) 
12266 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65  we know that the
12267 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63   original page c
12268 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c 6c 79 20  ontent is fully 
12269 73 79 6e 63 65 64 0a 20 20 2a 2a 20 69 6e 20 74  synced.  ** in t
1226a 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
1226b 65 69 74 68 65 72 20 62 65 63 61 75 73 65 20 74  either because t
1226c 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69  he page is not i
1226d 6e 20 63 61 63 68 65 20 6f 72 20 65 6c 73 65 0a  n cache or else.
1226e 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 69 73    ** the page is
1226f 20 6d 61 72 6b 65 64 20 61 73 20 6e 65 65 64 53   marked as needS
12270 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a 20 20 2a  ync==0..  **.  *
12271 2a 20 32 30 30 38 2d 30 34 2d 31 34 3a 20 20 57  * 2008-04-14:  W
12272 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74  hen attempting t
12273 6f 20 76 61 63 75 75 6d 20 61 20 63 6f 72 72 75  o vacuum a corru
12274 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  pt database file
12275 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73  , it.  ** is pos
12276 73 69 62 6c 65 20 74 6f 20 66 61 69 6c 20 61 20  sible to fail a 
12277 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 61 20 64  statement on a d
12278 61 74 61 62 61 73 65 20 74 68 61 74 20 64 6f 65  atabase that doe
12279 73 20 6e 6f 74 20 79 65 74 20 65 78 69 73 74 2e  s not yet exist.
1227a 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 61 74 74  .  ** Do not att
1227b 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 69 66  empt to write if
1227c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   database file h
1227d 61 73 20 6e 65 76 65 72 20 62 65 65 6e 20 6f 70  as never been op
1227e 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 67  ened..  */.  pPg
1227f 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28   = pager_lookup(
12280 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20  pPager, pgno);. 
12281 20 61 73 73 65 72 74 28 20 70 50 67 20 7c 7c 20   assert( pPg || 
12282 21 4d 45 4d 44 42 20 29 3b 0a 20 20 50 41 47 45  !MEMDB );.  PAGE
12283 52 54 52 41 43 45 28 28 22 50 4c 41 59 42 41 43  RTRACE(("PLAYBAC
12284 4b 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73  K %d page %d has
12285 68 28 25 30 38 78 29 20 25 73 5c 6e 22 2c 0a 20  h(%08x) %s\n",. 
12286 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41                PA
12287 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
12288 67 6e 6f 2c 20 70 61 67 65 72 5f 64 61 74 61 68  gno, pager_datah
12289 61 73 68 28 70 50 61 67 65 72 2d 3e 70 61 67 65  ash(pPager->page
1228a 53 69 7a 65 2c 20 61 44 61 74 61 29 2c 0a 20 20  Size, aData),.  
1228b 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 73               (is
1228c 4d 61 69 6e 4a 72 6e 6c 3f 22 6d 61 69 6e 2d 6a  MainJrnl?"main-j
1228d 6f 75 72 6e 61 6c 22 3a 22 73 75 62 2d 6a 6f 75  ournal":"sub-jou
1228e 72 6e 61 6c 22 29 0a 20 20 29 29 3b 0a 20 20 69  rnal").  ));.  i
1228f 66 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74  f( (pPager->stat
12290 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49  e>=PAGER_EXCLUSI
12291 56 45 29 0a 20 20 20 26 26 20 28 70 50 67 3d 3d  VE).   && (pPg==
12292 30 20 7c 7c 20 30 3d 3d 28 70 50 67 2d 3e 66 6c  0 || 0==(pPg->fl
12293 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
12294 59 4e 43 29 29 0a 20 20 20 26 26 20 69 73 4f 70  YNC)).   && isOp
12295 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 0a 20  en(pPager->fd). 
12296 20 20 26 26 20 21 69 73 55 6e 73 79 6e 63 0a 20    && !isUnsync. 
12297 20 29 7b 0a 20 20 20 20 69 36 34 20 6f 66 73 74   ){.    i64 ofst
12298 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34   = (pgno-1)*(i64
12299 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  )pPager->pageSiz
1229a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e;.    rc = sqli
1229b 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
1229c 72 2d 3e 66 64 2c 20 61 44 61 74 61 2c 20 70 50  r->fd, aData, pP
1229d 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
1229e 6f 66 73 74 29 3b 0a 20 20 20 20 69 66 28 20 70  ofst);.    if( p
1229f 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69  gno>pPager->dbFi
122a0 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  leSize ){.      
122a1 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69  pPager->dbFileSi
122a2 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d  ze = pgno;.    }
122a3 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
122a4 3e 70 42 61 63 6b 75 70 20 29 7b 0a 20 20 20 20  >pBackup ){.    
122a5 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c    CODEC1(pPager,
122a6 20 61 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 2c   aData, pgno, 3,
122a7 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   rc=SQLITE_NOMEM
122a8 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
122a9 42 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61  BackupUpdate(pPa
122aa 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67  ger->pBackup, pg
122ab 6e 6f 2c 20 61 44 61 74 61 29 3b 0a 20 20 20 20  no, aData);.    
122ac 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c    CODEC1(pPager,
122ad 20 61 44 61 74 61 2c 20 70 67 6e 6f 2c 20 30 2c   aData, pgno, 0,
122ae 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   rc=SQLITE_NOMEM
122af 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
122b0 20 69 66 28 20 21 69 73 4d 61 69 6e 4a 72 6e 6c   if( !isMainJrnl
122b1 20 26 26 20 70 50 67 3d 3d 30 20 29 7b 0a 20 20   && pPg==0 ){.  
122b2 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
122b3 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20  a rollback of a 
122b4 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 64 61  savepoint and da
122b5 74 61 20 77 61 73 20 6e 6f 74 20 77 72 69 74 74  ta was not writt
122b6 65 6e 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65  en to.    ** the
122b7 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68   database and th
122b8 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e  e page is not in
122b9 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 72 65 20 69  -memory, there i
122ba 73 20 61 20 70 6f 74 65 6e 74 69 61 6c 0a 20 20  s a potential.  
122bb 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 2e 20 57 68    ** problem. Wh
122bc 65 6e 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  en the page is n
122bd 65 78 74 20 66 65 74 63 68 65 64 20 62 79 20 74  ext fetched by t
122be 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65 72 2c  he b-tree layer,
122bf 20 69 74 20 0a 20 20 20 20 2a 2a 20 77 69 6c 6c   it .    ** will
122c0 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68   be read from th
122c1 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
122c2 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61   which may or ma
122c3 79 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 2a 2a  y not be .    **
122c4 20 63 75 72 72 65 6e 74 2e 20 0a 20 20 20 20 2a   current. .    *
122c5 2a 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61  *.    ** There a
122c6 72 65 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64  re a couple of d
122c7 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 74 68  ifferent ways th
122c8 69 73 20 63 61 6e 20 68 61 70 70 65 6e 2e 20 41  is can happen. A
122c9 6c 6c 20 61 72 65 20 71 75 69 74 65 0a 20 20 20  ll are quite.   
122ca 20 2a 2a 20 6f 62 73 63 75 72 65 2e 20 57 68 65   ** obscure. Whe
122cb 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 79 6e  n running in syn
122cc 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 74  chronous mode, t
122cd 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70  his can only hap
122ce 70 65 6e 20 0a 20 20 20 20 2a 2a 20 69 66 20 74  pen .    ** if t
122cf 68 65 20 70 61 67 65 20 69 73 20 6f 6e 20 74 68  he page is on th
122d0 65 20 66 72 65 65 2d 6c 69 73 74 20 61 74 20 74  e free-list at t
122d1 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
122d2 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
122d3 6e 0a 20 20 20 20 2a 2a 20 70 6f 70 75 6c 61 74  n.    ** populat
122d4 65 64 2c 20 74 68 65 6e 20 6d 6f 76 65 64 20 75  ed, then moved u
122d5 73 69 6e 67 20 73 71 6c 69 74 65 33 50 61 67 65  sing sqlite3Page
122d6 72 4d 6f 76 65 70 61 67 65 28 29 2e 0a 20 20 20  rMovepage()..   
122d7 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73   **.    ** The s
122d8 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20 61 64  olution is to ad
122d9 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70  d an in-memory p
122da 61 67 65 20 74 6f 20 74 68 65 20 63 61 63 68 65  age to the cache
122db 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 20 20 20 20   containing.    
122dc 2a 2a 20 74 68 65 20 64 61 74 61 20 6a 75 73 74  ** the data just
122dd 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 73   read from the s
122de 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 4d 61 72 6b  ub-journal. Mark
122df 20 74 68 65 20 70 61 67 65 20 61 73 20 64 69 72   the page as dir
122e0 74 79 20 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69  ty .    ** and i
122e1 66 20 74 68 65 20 70 61 67 65 72 20 72 65 71 75  f the pager requ
122e2 69 72 65 73 20 61 20 6a 6f 75 72 6e 61 6c 2d 73  ires a journal-s
122e3 79 6e 63 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74  ync, then mark t
122e4 68 65 20 70 61 67 65 20 61 73 20 0a 20 20 20 20  he page as .    
122e5 2a 2a 20 72 65 71 75 69 72 69 6e 67 20 61 20 6a  ** requiring a j
122e6 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 62 65 66 6f  ournal-sync befo
122e7 72 65 20 69 74 20 69 73 20 77 72 69 74 74 65 6e  re it is written
122e8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
122e9 65 72 74 28 20 69 73 53 61 76 65 70 6e 74 20 29  ert( isSavepnt )
122ea 3b 0a 20 20 20 20 69 66 28 20 28 72 63 20 3d 20  ;.    if( (rc = 
122eb 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75  sqlite3PagerAcqu
122ec 69 72 65 28 70 50 61 67 65 72 2c 20 70 67 6e 6f  ire(pPager, pgno
122ed 2c 20 26 70 50 67 2c 20 31 29 29 21 3d 53 51 4c  , &pPg, 1))!=SQL
122ee 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
122ef 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
122f0 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20  .    pPg->flags 
122f1 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 52  &= ~PGHDR_NEED_R
122f2 45 41 44 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  EAD;.    sqlite3
122f3 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28  PcacheMakeDirty(
122f4 70 50 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  pPg);.  }.  if( 
122f5 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f  pPg ){.    /* No
122f6 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65 76 65   page should eve
122f7 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c 79 20  r be explicitly 
122f8 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 61 74  rolled back that
122f9 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78 63 65   is in use, exce
122fa 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70 61  pt.    ** for pa
122fb 67 65 20 31 20 77 68 69 63 68 20 69 73 20 68 65  ge 1 which is he
122fc 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f 72 64  ld in use in ord
122fd 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 6c  er to keep the l
122fe 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a  ock on the.    *
122ff 2a 20 64 61 74 61 62 61 73 65 20 61 63 74 69 76  * database activ
12300 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63 68 20  e. However such 
12301 61 20 70 61 67 65 20 6d 61 79 20 62 65 20 72 6f  a page may be ro
12302 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20 72  lled back as a r
12303 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66 20  esult.    ** of 
12304 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72 72 6f  an internal erro
12305 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e 20 61  r resulting in a
12306 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c  n automatic call
12307 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   to.    ** sqlit
12308 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
12309 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76 6f  )..    */.    vo
1230a 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20 20 70  id *pData;.    p
1230b 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74  Data = pPg->pDat
1230c 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 44  a;.    memcpy(pD
1230d 61 74 61 2c 20 61 44 61 74 61 2c 20 70 50 61 67  ata, aData, pPag
1230e 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
1230f 20 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e     pPager->xRein
12310 69 74 65 72 28 70 50 67 29 3b 0a 20 20 20 20 69  iter(pPg);.    i
12311 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26  f( isMainJrnl &&
12312 20 28 21 69 73 53 61 76 65 70 6e 74 20 7c 7c 20   (!isSavepnt || 
12313 2a 70 4f 66 66 73 65 74 3c 3d 70 50 61 67 65 72  *pOffset<=pPager
12314 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 20 29 7b  ->journalHdr) ){
12315 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
12316 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69   contents of thi
12317 73 20 70 61 67 65 20 77 65 72 65 20 6a 75 73 74  s page were just
12318 20 72 65 73 74 6f 72 65 64 20 66 72 6f 6d 20 74   restored from t
12319 68 65 20 6d 61 69 6e 20 0a 20 20 20 20 20 20 2a  he main .      *
1231a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  * journal file, 
1231b 74 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74  then its content
1231c 20 6d 75 73 74 20 62 65 20 61 73 20 74 68 65 79   must be as they
1231d 20 77 65 72 65 20 77 68 65 6e 20 74 68 65 20 0a   were when the .
1231e 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63        ** transac
1231f 74 69 6f 6e 20 77 61 73 20 66 69 72 73 74 20 6f  tion was first o
12320 70 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63  pened. In this c
12321 61 73 65 20 77 65 20 63 61 6e 20 6d 61 72 6b 20  ase we can mark 
12322 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a  the page.      *
12323 2a 20 61 73 20 63 6c 65 61 6e 2c 20 73 69 6e 63  * as clean, sinc
12324 65 20 74 68 65 72 65 20 77 69 6c 6c 20 62 65 20  e there will be 
12325 6e 6f 20 6e 65 65 64 20 74 6f 20 77 72 69 74 65  no need to write
12326 20 69 74 20 6f 75 74 20 74 6f 20 74 68 65 2e 0a   it out to the..
12327 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
12328 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 65  * There is one e
12329 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 69 73  xception to this
1232a 20 72 75 6c 65 2e 20 49 66 20 74 68 65 20 70 61   rule. If the pa
1232b 67 65 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c  ge is being roll
1232c 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 61 63 6b  ed.      ** back
1232d 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 73 61   as part of a sa
1232e 76 65 70 6f 69 6e 74 20 28 6f 72 20 73 74 61 74  vepoint (or stat
1232f 65 6d 65 6e 74 29 20 72 6f 6c 6c 62 61 63 6b 20  ement) rollback 
12330 66 72 6f 6d 20 61 6e 20 0a 20 20 20 20 20 20 2a  from an .      *
12331 2a 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 74 69  * unsynced porti
12332 6f 6e 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  on of the main j
12333 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65  ournal file, the
12334 6e 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65  n it is not safe
12335 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 72  .      ** to mar
12336 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c  k the page as cl
12337 65 61 6e 2e 20 54 68 69 73 20 69 73 20 62 65 63  ean. This is bec
12338 61 75 73 65 20 6d 61 72 6b 69 6e 67 20 74 68 65  ause marking the
12339 20 70 61 67 65 20 61 73 0a 20 20 20 20 20 20 2a   page as.      *
1233a 2a 20 63 6c 65 61 6e 20 77 69 6c 6c 20 63 6c 65  * clean will cle
1233b 61 72 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45  ar the PGHDR_NEE
1233c 44 5f 53 59 4e 43 20 66 6c 61 67 2e 20 53 69 6e  D_SYNC flag. Sin
1233d 63 65 20 74 68 65 20 70 61 67 65 20 69 73 0a 20  ce the page is. 
1233e 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20       ** already 
1233f 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  in the journal f
12340 69 6c 65 20 28 72 65 63 6f 72 64 65 64 20 69 6e  ile (recorded in
12341 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61   Pager.pInJourna
12342 6c 29 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20  l) and.      ** 
12343 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  the PGHDR_NEED_S
12344 59 4e 43 20 66 6c 61 67 20 69 73 20 63 6c 65 61  YNC flag is clea
12345 72 65 64 2c 20 69 66 20 74 68 65 20 70 61 67 65  red, if the page
12346 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 20   is written to. 
12347 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69       ** again wi
12348 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61  thin this transa
12349 63 74 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62  ction, it will b
1234a 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74  e marked as dirt
1234b 79 20 62 75 74 0a 20 20 20 20 20 20 2a 2a 20 74  y but.      ** t
1234c 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
1234d 4e 43 20 66 6c 61 67 20 77 69 6c 6c 20 6e 6f 74  NC flag will not
1234e 20 62 65 20 73 65 74 2e 20 49 74 20 63 6f 75 6c   be set. It coul
1234f 64 20 74 68 65 6e 20 70 6f 74 65 6e 74 69 61 6c  d then potential
12350 6c 79 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 77  ly.      ** be w
12351 72 69 74 74 65 6e 20 6f 75 74 20 69 6e 74 6f 20  ritten out into 
12352 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12353 65 20 62 65 66 6f 72 65 20 69 74 73 20 6a 6f 75  e before its jou
12354 72 6e 61 6c 20 66 69 6c 65 0a 20 20 20 20 20 20  rnal file.      
12355 2a 2a 20 73 65 67 6d 65 6e 74 20 69 73 20 73 79  ** segment is sy
12356 6e 63 65 64 2e 20 49 66 20 61 20 63 72 61 73 68  nced. If a crash
12357 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6f   occurs during o
12358 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73  r following this
12359 2c 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62  ,.      ** datab
1235a 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d  ase corruption m
1235b 61 79 20 65 6e 73 75 65 2e 0a 20 20 20 20 20 20  ay ensue..      
1235c 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
1235d 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28  PcacheMakeClean(
1235e 70 50 67 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64  pPg);.    }.#ifd
1235f 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f  ef SQLITE_CHECK_
12360 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70  PAGES.    pPg->p
12361 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f  ageHash = pager_
12362 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23  pagehash(pPg);.#
12363 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20 49 66 20  endif.    /* If 
12364 74 68 69 73 20 77 61 73 20 70 61 67 65 20 31 2c  this was page 1,
12365 20 74 68 65 6e 20 72 65 73 74 6f 72 65 20 74 68   then restore th
12366 65 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72  e value of Pager
12367 2e 64 62 46 69 6c 65 56 65 72 73 2e 0a 20 20 20  .dbFileVers..   
12368 20 2a 2a 20 44 6f 20 74 68 69 73 20 62 65 66 6f   ** Do this befo
12369 72 65 20 61 6e 79 20 64 65 63 6f 64 69 6e 67 2e  re any decoding.
1236a 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f   */.    if( pgno
1236b 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ==1 ){.      mem
1236c 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46  cpy(&pPager->dbF
1236d 69 6c 65 56 65 72 73 2c 20 26 28 28 75 38 2a 29  ileVers, &((u8*)
1236e 70 44 61 74 61 29 5b 32 34 5d 2c 73 69 7a 65 6f  pData)[24],sizeo
1236f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  f(pPager->dbFile
12370 56 65 72 73 29 29 3b 0a 20 20 20 20 7d 0a 0a 20  Vers));.    }.. 
12371 20 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65     /* Decode the
12372 20 70 61 67 65 20 6a 75 73 74 20 72 65 61 64 20   page just read 
12373 66 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a 20 20 20  from disk */.   
12374 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20   CODEC1(pPager, 
12375 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f  pData, pPg->pgno
12376 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e  , 3, rc=SQLITE_N
12377 4f 4d 45 4d 29 3b 0a 20 20 20 20 73 71 6c 69 74  OMEM);.    sqlit
12378 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28  e3PcacheRelease(
12379 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  pPg);.  }.  retu
1237a 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1237b 50 61 72 61 6d 65 74 65 72 20 7a 4d 61 73 74 65  Parameter zMaste
1237c 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  r is the name of
1237d 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
1237e 6c 20 66 69 6c 65 2e 20 41 20 73 69 6e 67 6c 65  l file. A single
1237f 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65   journal.** file
12380 20 74 68 61 74 20 72 65 66 65 72 72 65 64 20 74   that referred t
12381 6f 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  o the master jou
12382 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 6a 75  rnal file has ju
12383 73 74 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62  st been rolled b
12384 61 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ack..** This rou
12385 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 69  tine checks if i
12386 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f  t is possible to
12387 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74   delete the mast
12388 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c  er journal file,
12389 0a 2a 2a 20 61 6e 64 20 64 6f 65 73 20 73 6f 20  .** and does so 
1238a 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20  if it is..**.** 
1238b 41 72 67 75 6d 65 6e 74 20 7a 4d 61 73 74 65 72  Argument zMaster
1238c 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 50 61   may point to Pa
1238d 67 65 72 2e 70 54 6d 70 53 70 61 63 65 2e 20 53  ger.pTmpSpace. S
1238e 6f 20 74 68 61 74 20 62 75 66 66 65 72 20 69 73  o that buffer is
1238f 20 6e 6f 74 20 0a 2a 2a 20 61 76 61 69 6c 61 62   not .** availab
12390 6c 65 20 66 6f 72 20 75 73 65 20 77 69 74 68 69  le for use withi
12391 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  n this function.
12392 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 6d 61  .**.** When a ma
12393 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
12394 65 20 69 73 20 63 72 65 61 74 65 64 2c 20 69 74  e is created, it
12395 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69   is populated wi
12396 74 68 20 74 68 65 20 6e 61 6d 65 73 20 0a 2a 2a  th the names .**
12397 20 6f 66 20 61 6c 6c 20 6f 66 20 69 74 73 20 63   of all of its c
12398 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2c 20 6f  hild journals, o
12399 6e 65 20 61 66 74 65 72 20 61 6e 6f 74 68 65 72  ne after another
1239a 2c 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20 75  , formatted as u
1239b 74 66 2d 38 20 0a 2a 2a 20 65 6e 63 6f 64 65 64  tf-8 .** encoded
1239c 20 74 65 78 74 2e 20 54 68 65 20 65 6e 64 20 6f   text. The end o
1239d 66 20 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75  f each child jou
1239e 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6d 61 72  rnal file is mar
1239f 6b 65 64 20 77 69 74 68 20 61 20 0a 2a 2a 20 6e  ked with a .** n
123a0 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79  ul-terminator by
123a1 74 65 20 28 30 78 30 30 29 2e 20 69 2e 65 2e 20  te (0x00). i.e. 
123a2 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65  the entire conte
123a3 6e 74 73 20 6f 66 20 61 20 6d 61 73 74 65 72 20  nts of a master 
123a4 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20  journal.** file 
123a5 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f  for a transactio
123a6 6e 20 69 6e 76 6f 6c 76 69 6e 67 20 74 77 6f 20  n involving two 
123a7 64 61 74 61 62 61 73 65 73 20 6d 69 67 68 74 20  databases might 
123a8 62 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 2f 68 6f  be:.**.**   "/ho
123a9 6d 65 2f 62 69 6c 6c 2f 61 2e 64 62 2d 6a 6f 75  me/bill/a.db-jou
123aa 72 6e 61 6c 5c 78 30 30 2f 68 6f 6d 65 2f 62 69  rnal\x00/home/bi
123ab 6c 6c 2f 62 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c  ll/b.db-journal\
123ac 78 30 30 22 0a 2a 2a 0a 2a 2a 20 41 20 6d 61 73  x00".**.** A mas
123ad 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
123ae 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 64 65 6c   may only be del
123af 65 74 65 64 20 6f 6e 63 65 20 61 6c 6c 20 6f 66  eted once all of
123b0 20 69 74 73 20 63 68 69 6c 64 20 0a 2a 2a 20 6a   its child .** j
123b1 6f 75 72 6e 61 6c 73 20 68 61 76 65 20 62 65 65  ournals have bee
123b2 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  n rolled back..*
123b3 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
123b4 6f 6e 20 72 65 61 64 73 20 74 68 65 20 63 6f 6e  on reads the con
123b5 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73  tents of the mas
123b6 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter-journal file
123b7 20 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79   into .** memory
123b8 20 61 6e 64 20 6c 6f 6f 70 73 20 74 68 72 6f 75   and loops throu
123b9 67 68 20 65 61 63 68 20 6f 66 20 74 68 65 20 63  gh each of the c
123ba 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  hild journal nam
123bb 65 73 2e 20 46 6f 72 0a 2a 2a 20 65 61 63 68 20  es. For.** each 
123bc 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 2c 20 69  child journal, i
123bd 74 20 63 68 65 63 6b 73 20 69 66 3a 0a 2a 2a 0a  t checks if:.**.
123be 2a 2a 20 20 20 2a 20 69 66 20 74 68 65 20 63 68  **   * if the ch
123bf 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73  ild journal exis
123c0 74 73 2c 20 61 6e 64 20 69 66 20 73 6f 0a 2a 2a  ts, and if so.**
123c1 20 20 20 2a 20 69 66 20 74 68 65 20 63 68 69 6c     * if the chil
123c2 64 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69  d journal contai
123c3 6e 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  ns a reference t
123c4 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  o master journal
123c5 20 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 7a 4d   .**     file zM
123c6 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 49 66 20 61  aster.**.** If a
123c7 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63   child journal c
123c8 61 6e 20 62 65 20 66 6f 75 6e 64 20 74 68 61 74  an be found that
123c9 20 6d 61 74 63 68 65 73 20 62 6f 74 68 20 6f 66   matches both of
123ca 20 74 68 65 20 63 72 69 74 65 72 69 61 0a 2a 2a   the criteria.**
123cb 20 61 62 6f 76 65 2c 20 74 68 69 73 20 66 75 6e   above, this fun
123cc 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69  ction returns wi
123cd 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74  thout doing anyt
123ce 68 69 6e 67 2e 20 4f 74 68 65 72 77 69 73 65 2c  hing. Otherwise,
123cf 20 69 66 0a 2a 2a 20 6e 6f 20 73 75 63 68 20 63   if.** no such c
123d0 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 61 6e  hild journal can
123d1 20 62 65 20 66 6f 75 6e 64 2c 20 66 69 6c 65 20   be found, file 
123d2 7a 4d 61 73 74 65 72 20 69 73 20 64 65 6c 65 74  zMaster is delet
123d3 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 66  ed from.** the f
123d4 69 6c 65 2d 73 79 73 74 65 6d 20 75 73 69 6e 67  ile-system using
123d5 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
123d6 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ()..**.** If an 
123d7 49 4f 20 65 72 72 6f 72 20 77 69 74 68 69 6e 20  IO error within 
123d8 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61  this function, a
123d9 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  n error code is 
123da 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a  returned. This.*
123db 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63  * function alloc
123dc 61 74 65 73 20 6d 65 6d 6f 72 79 20 62 79 20 63  ates memory by c
123dd 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 4d 61  alling sqlite3Ma
123de 6c 6c 6f 63 28 29 2e 20 49 66 20 61 6e 20 61 6c  lloc(). If an al
123df 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c  location.** fail
123e0 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  s, SQLITE_NOMEM 
123e1 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  is returned. Oth
123e2 65 72 77 69 73 65 2c 20 69 66 20 6e 6f 20 49 4f  erwise, if no IO
123e3 20 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72   or malloc error
123e4 73 20 0a 2a 2a 20 6f 63 63 75 72 2c 20 53 51 4c  s .** occur, SQL
123e5 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
123e6 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20  ed..**.** TODO: 
123e7 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  This function al
123e8 6c 6f 63 61 74 65 73 20 61 20 73 69 6e 67 6c 65  locates a single
123e9 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79   block of memory
123ea 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 74 68 65 20   to load.** the 
123eb 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20  entire contents 
123ec 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  of the master jo
123ed 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73  urnal file. This
123ee 20 63 6f 75 6c 64 20 62 65 0a 2a 2a 20 61 20 63   could be.** a c
123ef 6f 75 70 6c 65 20 6f 66 20 6b 69 6c 6f 62 79 74  ouple of kilobyt
123f0 65 73 20 6f 72 20 73 6f 20 2d 20 70 6f 74 65 6e  es or so - poten
123f1 74 69 61 6c 6c 79 20 6c 61 72 67 65 72 20 74 68  tially larger th
123f2 61 6e 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20  an the page .** 
123f3 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  size..*/.static 
123f4 69 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d 61 73  int pager_delmas
123f5 74 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65  ter(Pager *pPage
123f6 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r, const char *z
123f7 4d 61 73 74 65 72 29 7b 0a 20 20 73 71 6c 69 74  Master){.  sqlit
123f8 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70  e3_vfs *pVfs = p
123f9 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69  Pager->pVfs;.  i
123fa 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
123fb 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
123fc 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c  rn code */.  sql
123fd 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74  ite3_file *pMast
123fe 65 72 3b 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63  er;    /* Malloc
123ff 27 64 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61  'd master-journa
12400 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  l file descripto
12401 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  r */.  sqlite3_f
12402 69 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b 20 20  ile *pJournal;  
12403 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 63 68 69   /* Malloc'd chi
12404 6c 64 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ld-journal file 
12405 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20  descriptor */.  
12406 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 4a 6f 75  char *zMasterJou
12407 72 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 43 6f 6e  rnal = 0; /* Con
12408 74 65 6e 74 73 20 6f 66 20 6d 61 73 74 65 72 20  tents of master 
12409 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
1240a 20 20 69 36 34 20 6e 4d 61 73 74 65 72 4a 6f 75    i64 nMasterJou
1240b 72 6e 61 6c 3b 20 20 20 20 20 20 20 2f 2a 20 53  rnal;       /* S
1240c 69 7a 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f  ize of master jo
1240d 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20  urnal file */.. 
1240e 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61   /* Allocate spa
1240f 63 65 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20  ce for both the 
12410 70 4a 6f 75 72 6e 61 6c 20 61 6e 64 20 70 4d 61  pJournal and pMa
12411 73 74 65 72 20 66 69 6c 65 20 64 65 73 63 72 69  ster file descri
12412 70 74 6f 72 73 2e 0a 20 20 2a 2a 20 49 66 20 73  ptors..  ** If s
12413 75 63 63 65 73 73 66 75 6c 2c 20 6f 70 65 6e 20  uccessful, open 
12414 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
12415 61 6c 20 66 69 6c 65 20 66 6f 72 20 72 65 61 64  al file for read
12416 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73  ing..  */.  pMas
12417 74 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 66  ter = (sqlite3_f
12418 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c  ile *)sqlite3Mal
12419 6c 6f 63 5a 65 72 6f 28 70 56 66 73 2d 3e 73 7a  locZero(pVfs->sz
1241a 4f 73 46 69 6c 65 20 2a 20 32 29 3b 0a 20 20 70  OsFile * 2);.  p
1241b 4a 6f 75 72 6e 61 6c 20 3d 20 28 73 71 6c 69 74  Journal = (sqlit
1241c 65 33 5f 66 69 6c 65 20 2a 29 28 28 28 75 38 20  e3_file *)(((u8 
1241d 2a 29 70 4d 61 73 74 65 72 29 20 2b 20 70 56 66  *)pMaster) + pVf
1241e 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20  s->szOsFile);.  
1241f 69 66 28 20 21 70 4d 61 73 74 65 72 20 29 7b 0a  if( !pMaster ){.
12420 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
12421 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NOMEM;.  }else{.
12422 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c      const int fl
12423 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50  ags = (SQLITE_OP
12424 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49  EN_READONLY|SQLI
12425 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a  TE_OPEN_MASTER_J
12426 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 72 63 20  OURNAL);.    rc 
12427 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  = sqlite3OsOpen(
12428 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 70  pVfs, zMaster, p
12429 4d 61 73 74 65 72 2c 20 66 6c 61 67 73 2c 20 30  Master, flags, 0
1242a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21  );.  }.  if( rc!
1242b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
1242c 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b  o delmaster_out;
1242d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
1242e 4f 73 46 69 6c 65 53 69 7a 65 28 70 4d 61 73 74  OsFileSize(pMast
1242f 65 72 2c 20 26 6e 4d 61 73 74 65 72 4a 6f 75 72  er, &nMasterJour
12430 6e 61 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  nal);.  if( rc!=
12431 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
12432 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
12433 0a 20 20 69 66 28 20 6e 4d 61 73 74 65 72 4a 6f  .  if( nMasterJo
12434 75 72 6e 61 6c 3e 30 20 29 7b 0a 20 20 20 20 63  urnal>0 ){.    c
12435 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 0a 20  har *zJournal;. 
12436 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72     char *zMaster
12437 50 74 72 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  Ptr = 0;.    int
12438 20 6e 4d 61 73 74 65 72 50 74 72 20 3d 20 70 56   nMasterPtr = pV
12439 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  fs->mxPathname+1
1243a 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74  ;..    /* Load t
1243b 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72  he entire master
1243c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e   journal file in
1243d 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65  to space obtaine
1243e 64 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 73 71  d from.    ** sq
1243f 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61  lite3_malloc() a
12440 6e 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  nd pointed to by
12441 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2e   zMasterJournal.
12442 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61   .    */.    zMa
12443 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 73 71  sterJournal = sq
12444 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74  lite3Malloc((int
12445 29 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20  )nMasterJournal 
12446 2b 20 6e 4d 61 73 74 65 72 50 74 72 20 2b 20 31  + nMasterPtr + 1
12447 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73  );.    if( !zMas
12448 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20  terJournal ){.  
12449 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1244a 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74  NOMEM;.      got
1244b 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b  o delmaster_out;
1244c 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74  .    }.    zMast
1244d 65 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72  erPtr = &zMaster
1244e 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a  Journal[nMasterJ
1244f 6f 75 72 6e 61 6c 2b 31 5d 3b 0a 20 20 20 20 72  ournal+1];.    r
12450 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
12451 64 28 70 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74  d(pMaster, zMast
12452 65 72 4a 6f 75 72 6e 61 6c 2c 20 28 69 6e 74 29  erJournal, (int)
12453 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20  nMasterJournal, 
12454 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  0);.    if( rc!=
12455 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
12456 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
12457 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e      zMasterJourn
12458 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61  al[nMasterJourna
12459 6c 5d 20 3d 20 30 3b 0a 0a 20 20 20 20 7a 4a 6f  l] = 0;..    zJo
1245a 75 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 65 72 4a  urnal = zMasterJ
1245b 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 77 68 69 6c  ournal;.    whil
1245c 65 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d 61  e( (zJournal-zMa
1245d 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d 61  sterJournal)<nMa
1245e 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20  sterJournal ){. 
1245f 20 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b       int exists;
12460 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
12461 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73  te3OsAccess(pVfs
12462 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49  , zJournal, SQLI
12463 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
12464 2c 20 26 65 78 69 73 74 73 29 3b 0a 20 20 20 20  , &exists);.    
12465 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12466 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
12467 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75  oto delmaster_ou
12468 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
12469 20 69 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20   if( exists ){. 
1246a 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
1246b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f   the journals po
1246c 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20  inted to by the 
1246d 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65  master journal e
1246e 78 69 73 74 73 2e 0a 20 20 20 20 20 20 20 20 2a  xists..        *
1246f 2a 20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 68  * Open it and ch
12470 65 63 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 73  eck if it points
12471 20 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a   at the master j
12472 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20  ournal. If.     
12473 20 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e     ** so, return
12474 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e   without deletin
12475 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  g the master jou
12476 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20  rnal file..     
12477 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e     */.        in
12478 74 20 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  t c;.        int
12479 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45   flags = (SQLITE
1247a 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53  _OPEN_READONLY|S
1247b 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
1247c 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20  JOURNAL);.      
1247d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1247e 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72  Open(pVfs, zJour
1247f 6e 61 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66  nal, pJournal, f
12480 6c 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20  lags, 0);.      
12481 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12482 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
12483 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
12484 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  out;.        }..
12485 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61          rc = rea
12486 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70  dMasterJournal(p
12487 4a 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72  Journal, zMaster
12488 50 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29  Ptr, nMasterPtr)
12489 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1248a 33 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61  3OsClose(pJourna
1248b 6c 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  l);.        if( 
1248c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1248d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1248e 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20  delmaster_out;. 
1248f 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
12490 20 20 63 20 3d 20 7a 4d 61 73 74 65 72 50 74 72    c = zMasterPtr
12491 5b 30 5d 21 3d 30 20 26 26 20 73 74 72 63 6d 70  [0]!=0 && strcmp
12492 28 7a 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61  (zMasterPtr, zMa
12493 73 74 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20  ster)==0;.      
12494 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20    if( c ){.     
12495 20 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20       /* We have 
12496 61 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20  a match. Do not 
12497 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65  delete the maste
12498 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  r journal file. 
12499 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  */.          got
1249a 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b  o delmaster_out;
1249b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1249c 20 7d 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61   }.      zJourna
1249d 6c 20 2b 3d 20 28 73 71 6c 69 74 65 33 53 74 72  l += (sqlite3Str
1249e 6c 65 6e 33 30 28 7a 4a 6f 75 72 6e 61 6c 29 2b  len30(zJournal)+
1249f 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  1);.    }.  }.  
124a0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
124a1 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d  sDelete(pVfs, zM
124a2 61 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65 6c 6d  aster, 0);..delm
124a3 61 73 74 65 72 5f 6f 75 74 3a 0a 20 20 69 66 28  aster_out:.  if(
124a4 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20   zMasterJournal 
124a5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
124a6 72 65 65 28 7a 4d 61 73 74 65 72 4a 6f 75 72 6e  ree(zMasterJourn
124a7 61 6c 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28  al);.  }  .  if(
124a8 20 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20   pMaster ){.    
124a9 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
124aa 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 61 73 73  Master);.    ass
124ab 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 4a 6f  ert( !isOpen(pJo
124ac 75 72 6e 61 6c 29 20 29 3b 0a 20 20 7d 0a 20 20  urnal) );.  }.  
124ad 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61  sqlite3_free(pMa
124ae 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  ster);.  return 
124af 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  rc;.}.../*.** Th
124b0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
124b1 73 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68  sed to change th
124b2 65 20 61 63 74 75 61 6c 20 73 69 7a 65 20 6f 66  e actual size of
124b3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a   the database .*
124b4 2a 20 66 69 6c 65 20 69 6e 20 74 68 65 20 66 69  * file in the fi
124b5 6c 65 2d 73 79 73 74 65 6d 2e 20 54 68 69 73 20  le-system. This 
124b6 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65  only happens whe
124b7 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74  n committing a t
124b8 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 6f  ransaction,.** o
124b9 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61  r rolling back a
124ba 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 6e   transaction (in
124bb 63 6c 75 64 69 6e 67 20 72 6f 6c 6c 69 6e 67 20  cluding rolling 
124bc 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  back a hot-journ
124bd 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  al)..**.** If th
124be 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
124bf 66 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70 65 6e  file is not open
124c0 2c 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76  , or an exclusiv
124c1 65 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 0a 2a 2a  e lock is not.**
124c2 20 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63   held, this func
124c3 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
124c4 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
124c5 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
124c6 20 69 73 0a 2a 2a 20 63 68 61 6e 67 65 64 20 74   is.** changed t
124c7 6f 20 6e 50 61 67 65 20 70 61 67 65 73 20 28 6e  o nPage pages (n
124c8 50 61 67 65 2a 70 50 61 67 65 72 2d 3e 70 61 67  Page*pPager->pag
124c9 65 53 69 7a 65 20 62 79 74 65 73 29 2e 20 49 66  eSize bytes). If
124ca 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20   the file.** on 
124cb 64 69 73 6b 20 69 73 20 63 75 72 72 65 6e 74 6c  disk is currentl
124cc 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 6e 50  y larger than nP
124cd 61 67 65 20 70 61 67 65 73 2c 20 74 68 65 6e 20  age pages, then 
124ce 75 73 65 20 74 68 65 20 56 46 53 0a 2a 2a 20 78  use the VFS.** x
124cf 54 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f  Truncate() metho
124d0 64 20 74 6f 20 74 72 75 6e 63 61 74 65 20 69 74  d to truncate it
124d1 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 74 20 6d  ..**.** Or, it m
124d2 69 67 68 74 20 6d 69 67 68 74 20 62 65 20 74 68  ight might be th
124d3 65 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20  e case that the 
124d4 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20  file on disk is 
124d5 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 0a 2a 2a  smaller than .**
124d6 20 6e 50 61 67 65 20 70 61 67 65 73 2e 20 53 6f   nPage pages. So
124d7 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  me operating sys
124d8 74 65 6d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tem implementati
124d9 6f 6e 73 20 63 61 6e 20 67 65 74 20 63 6f 6e 66  ons can get conf
124da 75 73 65 64 20 69 66 20 0a 2a 2a 20 79 6f 75 20  used if .** you 
124db 74 72 79 20 74 6f 20 74 72 75 6e 63 61 74 65 20  try to truncate 
124dc 61 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65 20 73  a file to some s
124dd 69 7a 65 20 74 68 61 74 20 69 73 20 6c 61 72 67  ize that is larg
124de 65 72 20 74 68 61 6e 20 69 74 20 0a 2a 2a 20 63  er than it .** c
124df 75 72 72 65 6e 74 6c 79 20 69 73 2c 20 73 6f 20  urrently is, so 
124e0 64 65 74 65 63 74 20 74 68 69 73 20 63 61 73 65  detect this case
124e1 20 61 6e 64 20 77 72 69 74 65 20 61 20 73 69 6e   and write a sin
124e2 67 6c 65 20 7a 65 72 6f 20 62 79 74 65 20 74 6f  gle zero byte to
124e3 20 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20   .** the end of 
124e4 74 68 65 20 6e 65 77 20 66 69 6c 65 20 69 6e 73  the new file ins
124e5 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  tead..**.** If s
124e6 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72  uccessful, retur
124e7 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20  n SQLITE_OK. If 
124e8 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
124e9 72 73 20 77 68 69 6c 65 20 6d 6f 64 69 66 79 69  rs while modifyi
124ea 6e 67 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  ng.** the databa
124eb 73 65 20 66 69 6c 65 2c 20 72 65 74 75 72 6e 20  se file, return 
124ec 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74  the error code t
124ed 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f  o the caller..*/
124ee 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
124ef 72 5f 74 72 75 6e 63 61 74 65 28 50 61 67 65 72  r_truncate(Pager
124f0 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e   *pPager, Pgno n
124f1 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  Page){.  int rc 
124f2 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
124f3 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
124f4 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  >=PAGER_EXCLUSIV
124f5 45 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67  E && isOpen(pPag
124f6 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 69  er->fd) ){.    i
124f7 36 34 20 63 75 72 72 65 6e 74 53 69 7a 65 2c 20  64 currentSize, 
124f8 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20 2f 2a 20  newSize;.    /* 
124f9 54 4f 44 4f 3a 20 49 73 20 69 74 20 73 61 66 65  TODO: Is it safe
124fa 20 74 6f 20 75 73 65 20 50 61 67 65 72 2e 64 62   to use Pager.db
124fb 46 69 6c 65 53 69 7a 65 20 68 65 72 65 3f 20 2a  FileSize here? *
124fc 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
124fd 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61  e3OsFileSize(pPa
124fe 67 65 72 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e  ger->fd, &curren
124ff 74 53 69 7a 65 29 3b 0a 20 20 20 20 6e 65 77 53  tSize);.    newS
12500 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61  ize = pPager->pa
12501 67 65 53 69 7a 65 2a 28 69 36 34 29 6e 50 61 67  geSize*(i64)nPag
12502 65 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  e;.    if( rc==S
12503 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72  QLITE_OK && curr
12504 65 6e 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65  entSize!=newSize
12505 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75   ){.      if( cu
12506 72 72 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a  rrentSize>newSiz
12507 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  e ){.        rc 
12508 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
12509 61 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ate(pPager->fd, 
1250a 6e 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  newSize);.      
1250b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
1250c 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
1250d 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 22  te(pPager->fd, "
1250e 22 2c 20 31 2c 20 6e 65 77 53 69 7a 65 2d 31 29  ", 1, newSize-1)
1250f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
12510 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12511 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61  K ){.        pPa
12512 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20  ger->dbFileSize 
12513 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 7d  = nPage;.      }
12514 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
12515 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
12516 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f   Set the value o
12517 66 20 74 68 65 20 50 61 67 65 72 2e 73 65 63 74  f the Pager.sect
12518 6f 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20  orSize variable 
12519 66 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a  for the given.**
1251a 20 70 61 67 65 72 20 62 61 73 65 64 20 6f 6e 20   pager based on 
1251b 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
1251c 65 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f  ed by the xSecto
1251d 72 53 69 7a 65 20 6d 65 74 68 6f 64 0a 2a 2a 20  rSize method.** 
1251e 6f 66 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61  of the open data
1251f 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 73  base file. The s
12520 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20  ector size will 
12521 62 65 20 75 73 65 64 20 75 73 65 64 20 0a 2a 2a  be used used .**
12522 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
12523 65 20 73 69 7a 65 20 61 6e 64 20 61 6c 69 67 6e  e size and align
12524 6d 65 6e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20  ment of journal 
12525 68 65 61 64 65 72 20 61 6e 64 20 0a 2a 2a 20 6d  header and .** m
12526 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f  aster journal po
12527 69 6e 74 65 72 73 20 77 69 74 68 69 6e 20 63 72  inters within cr
12528 65 61 74 65 64 20 6a 6f 75 72 6e 61 6c 20 66 69  eated journal fi
12529 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  les..**.** For t
1252a 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 74  emporary files t
1252b 68 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63  he effective sec
1252c 74 6f 72 20 73 69 7a 65 20 69 73 20 61 6c 77 61  tor size is alwa
1252d 79 73 20 35 31 32 20 62 79 74 65 73 2e 0a 2a 2a  ys 512 bytes..**
1252e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66  .** Otherwise, f
1252f 6f 72 20 6e 6f 6e 2d 74 65 6d 70 6f 72 61 72 79  or non-temporary
12530 20 66 69 6c 65 73 2c 20 74 68 65 20 65 66 66 65   files, the effe
12531 63 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a  ctive sector siz
12532 65 20 69 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75  e is.** the valu
12533 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  e returned by th
12534 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20  e xSectorSize() 
12535 6d 65 74 68 6f 64 20 72 6f 75 6e 64 65 64 20 75  method rounded u
12536 70 20 74 6f 20 35 31 32 20 69 66 0a 2a 2a 20 69  p to 512 if.** i
12537 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 35  t is less than 5
12538 31 32 2c 20 6f 72 20 72 6f 75 6e 64 65 64 20 64  12, or rounded d
12539 6f 77 6e 20 74 6f 20 4d 41 58 5f 53 45 43 54 4f  own to MAX_SECTO
1253a 52 5f 53 49 5a 45 20 69 66 20 69 74 0a 2a 2a 20  R_SIZE if it.** 
1253b 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
1253c 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 2e  MAX_SECTOR_SIZE.
1253d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1253e 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 50 61  setSectorSize(Pa
1253f 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
12540 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
12541 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50  Pager->fd) || pP
12542 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
12543 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 72  ;..  if( !pPager
12544 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20  ->tempFile ){.  
12545 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65    /* Sector size
12546 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20   doesn't matter 
12547 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69  for temporary fi
12548 6c 65 73 2e 20 41 6c 73 6f 2c 20 74 68 65 20 66  les. Also, the f
12549 69 6c 65 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e  ile.    ** may n
1254a 6f 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65  ot have been ope
1254b 6e 65 64 20 79 65 74 2c 20 69 6e 20 77 68 69 63  ned yet, in whic
1254c 68 20 63 61 73 65 20 74 68 65 20 4f 73 53 65 63  h case the OsSec
1254d 74 6f 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a  torSize().    **
1254e 20 63 61 6c 6c 20 77 69 6c 6c 20 73 65 67 66 61   call will segfa
1254f 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ult..    */.    
12550 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
12551 7a 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65  ze = sqlite3OsSe
12552 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 2d  ctorSize(pPager-
12553 3e 66 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  >fd);.  }.  if( 
12554 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
12555 7a 65 3c 35 31 32 20 29 7b 0a 20 20 20 20 70 50  ze<512 ){.    pP
12556 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
12557 20 3d 20 35 31 32 3b 0a 20 20 7d 0a 20 20 69 66   = 512;.  }.  if
12558 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72  ( pPager->sector
12559 53 69 7a 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f  Size>MAX_SECTOR_
1255a 53 49 5a 45 20 29 7b 0a 20 20 20 20 61 73 73 65  SIZE ){.    asse
1255b 72 74 28 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53  rt( MAX_SECTOR_S
1255c 49 5a 45 3e 3d 35 31 32 20 29 3b 0a 20 20 20 20  IZE>=512 );.    
1255d 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
1255e 7a 65 20 3d 20 4d 41 58 5f 53 45 43 54 4f 52 5f  ze = MAX_SECTOR_
1255f 53 49 5a 45 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  SIZE;.  }.}../*.
12560 2a 2a 20 50 6c 61 79 62 61 63 6b 20 74 68 65 20  ** Playback the 
12561 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 75 73  journal and thus
12562 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74   restore the dat
12563 61 62 61 73 65 20 66 69 6c 65 20 74 6f 0a 2a 2a  abase file to.**
12564 20 74 68 65 20 73 74 61 74 65 20 69 74 20 77 61   the state it wa
12565 73 20 69 6e 20 62 65 66 6f 72 65 20 77 65 20 73  s in before we s
12566 74 61 72 74 65 64 20 6d 61 6b 69 6e 67 20 63 68  tarted making ch
12567 61 6e 67 65 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  anges.  .**.** T
12568 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
12569 66 6f 72 6d 61 74 20 69 73 20 61 73 20 66 6f 6c  format is as fol
1256a 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 28 31  lows: .**.**  (1
1256b 29 20 20 38 20 62 79 74 65 20 70 72 65 66 69 78  )  8 byte prefix
1256c 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 61 4a 6f  .  A copy of aJo
1256d 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a  urnalMagic[]..**
1256e 20 20 28 32 29 20 20 34 20 62 79 74 65 20 62 69    (2)  4 byte bi
1256f 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72  g-endian integer
12570 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6e 75   which is the nu
12571 6d 62 65 72 20 6f 66 20 76 61 6c 69 64 20 70 61  mber of valid pa
12572 67 65 20 72 65 63 6f 72 64 73 0a 2a 2a 20 20 20  ge records.**   
12573 20 20 20 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e      in the journ
12574 61 6c 2e 20 20 49 66 20 74 68 69 73 20 76 61 6c  al.  If this val
12575 75 65 20 69 73 20 30 78 66 66 66 66 66 66 66 66  ue is 0xffffffff
12576 2c 20 74 68 65 6e 20 63 6f 6d 70 75 74 65 20 74  , then compute t
12577 68 65 0a 2a 2a 20 20 20 20 20 20 20 6e 75 6d 62  he.**       numb
12578 65 72 20 6f 66 20 70 61 67 65 20 72 65 63 6f 72  er of page recor
12579 64 73 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ds from the jour
1257a 6e 61 6c 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 33  nal size..**  (3
1257b 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e  )  4 byte big-en
1257c 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  dian integer whi
1257d 63 68 20 69 73 20 74 68 65 20 69 6e 69 74 69 61  ch is the initia
1257e 6c 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  l value for the 
1257f 0a 2a 2a 20 20 20 20 20 20 20 73 61 6e 69 74 79  .**       sanity
12580 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 20 28   checksum..**  (
12581 34 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67  4)  4 byte integ
12582 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20  er which is the 
12583 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
12584 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a  to truncate the.
12585 2a 2a 20 20 20 20 20 20 20 64 61 74 61 62 61 73  **       databas
12586 65 20 74 6f 20 64 75 72 69 6e 67 20 61 20 72 6f  e to during a ro
12587 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 20 28 35 29 20  llback..**  (5) 
12588 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69   4 byte big-endi
12589 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68  an integer which
1258a 20 69 73 20 74 68 65 20 73 65 63 74 6f 72 20 73   is the sector s
1258b 69 7a 65 2e 20 20 54 68 65 20 68 65 61 64 65 72  ize.  The header
1258c 0a 2a 2a 20 20 20 20 20 20 20 69 73 20 74 68 69  .**       is thi
1258d 73 20 6d 61 6e 79 20 62 79 74 65 73 20 69 6e 20  s many bytes in 
1258e 73 69 7a 65 2e 0a 2a 2a 20 20 28 36 29 20 20 34  size..**  (6)  4
1258f 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e   byte big-endian
12590 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69   integer which i
12591 73 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e  s the page size.
12592 0a 2a 2a 20 20 28 37 29 20 20 7a 65 72 6f 20 70  .**  (7)  zero p
12593 61 64 64 69 6e 67 20 6f 75 74 20 74 6f 20 74 68  adding out to th
12594 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 73 69  e next sector si
12595 7a 65 2e 0a 2a 2a 20 20 28 38 29 20 20 5a 65 72  ze..**  (8)  Zer
12596 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20  o or more pages 
12597 69 6e 73 74 61 6e 63 65 73 2c 20 65 61 63 68 20  instances, each 
12598 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20  as follows:.**  
12599 20 20 20 20 20 20 2b 20 20 34 20 62 79 74 65 20        +  4 byte 
1259a 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  page number..** 
1259b 20 20 20 20 20 20 20 2b 20 20 70 50 61 67 65 72         +  pPager
1259c 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  ->pageSize bytes
1259d 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 20 20 20   of data..**    
1259e 20 20 20 20 2b 20 20 34 20 62 79 74 65 20 63 68      +  4 byte ch
1259f 65 63 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65  ecksum.**.** Whe
125a0 6e 20 77 65 20 73 70 65 61 6b 20 6f 66 20 74 68  n we speak of th
125a1 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
125a2 2c 20 77 65 20 6d 65 61 6e 20 74 68 65 20 66 69  , we mean the fi
125a3 72 73 74 20 37 20 69 74 65 6d 73 20 61 62 6f 76  rst 7 items abov
125a4 65 2e 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72 79  e..** Each entry
125a5 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
125a6 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
125a7 66 20 74 68 65 20 38 74 68 20 69 74 65 6d 2e 0a  f the 8th item..
125a8 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76  **.** Call the v
125a9 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 73 65  alue from the se
125aa 63 6f 6e 64 20 62 75 6c 6c 65 74 20 22 6e 52 65  cond bullet "nRe
125ab 63 22 2e 20 20 6e 52 65 63 20 69 73 20 74 68 65  c".  nRec is the
125ac 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 76 61   number of.** va
125ad 6c 69 64 20 70 61 67 65 20 65 6e 74 72 69 65 73  lid page entries
125ae 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e   in the journal.
125af 20 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c    In most cases,
125b0 20 79 6f 75 20 63 61 6e 20 63 6f 6d 70 75 74 65   you can compute
125b1 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66   the.** value of
125b2 20 6e 52 65 63 20 66 72 6f 6d 20 74 68 65 20 73   nRec from the s
125b3 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ize of the journ
125b4 61 6c 20 66 69 6c 65 2e 20 20 42 75 74 20 69 66  al file.  But if
125b5 20 61 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c   a power.** fail
125b6 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 68 69  ure occurred whi
125b7 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77  le the journal w
125b8 61 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e  as being written
125b9 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68  , it could be th
125ba 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 74  e.** case that t
125bb 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a  he size of the j
125bc 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 64 20  ournal file had 
125bd 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 63  already been inc
125be 72 65 61 73 65 64 20 62 75 74 0a 2a 2a 20 74 68  reased but.** th
125bf 65 20 65 78 74 72 61 20 65 6e 74 72 69 65 73 20  e extra entries 
125c0 68 61 64 20 6e 6f 74 20 79 65 74 20 6d 61 64 65  had not yet made
125c1 20 69 74 20 73 61 66 65 6c 79 20 74 6f 20 64 69   it safely to di
125c2 73 6b 2e 20 20 49 6e 20 73 75 63 68 20 61 20 63  sk.  In such a c
125c3 61 73 65 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75  ase,.** the valu
125c4 65 20 6f 66 20 6e 52 65 63 20 63 6f 6d 70 75 74  e of nRec comput
125c5 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65  ed from the file
125c6 20 73 69 7a 65 20 77 6f 75 6c 64 20 62 65 20 74   size would be t
125c7 6f 6f 20 6c 61 72 67 65 2e 20 20 46 6f 72 0a 2a  oo large.  For.*
125c8 2a 20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77  * that reason, w
125c9 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 68 65  e always use the
125ca 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 20 74   nRec value in t
125cb 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a  he header..**.**
125cc 20 49 66 20 74 68 65 20 6e 52 65 63 20 76 61 6c   If the nRec val
125cd 75 65 20 69 73 20 30 78 66 66 66 66 66 66 66 66  ue is 0xffffffff
125ce 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 6e   it means that n
125cf 52 65 63 20 73 68 6f 75 6c 64 20 62 65 20 63 6f  Rec should be co
125d0 6d 70 75 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74  mputed.** from t
125d1 68 65 20 66 69 6c 65 20 73 69 7a 65 2e 20 20 54  he file size.  T
125d2 68 69 73 20 76 61 6c 75 65 20 69 73 20 75 73 65  his value is use
125d3 64 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20  d when the user 
125d4 73 65 6c 65 63 74 73 20 74 68 65 0a 2a 2a 20 6e  selects the.** n
125d5 6f 2d 73 79 6e 63 20 6f 70 74 69 6f 6e 20 66 6f  o-sync option fo
125d6 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  r the journal.  
125d7 41 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  A power failure 
125d8 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 63 6f  could lead to co
125d9 72 72 75 70 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74  rruption.** in t
125da 68 69 73 20 63 61 73 65 2e 20 20 42 75 74 20 66  his case.  But f
125db 6f 72 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74  or things like t
125dc 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28  emporary table (
125dd 77 68 69 63 68 20 77 69 6c 6c 20 62 65 0a 2a 2a  which will be.**
125de 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68   deleted when th
125df 65 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f  e power is resto
125e0 72 65 64 29 20 77 65 20 64 6f 6e 27 74 20 63 61  red) we don't ca
125e1 72 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  re.  .**.** If t
125e2 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 61  he file opened a
125e3 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  s the journal fi
125e4 6c 65 20 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c  le is not a well
125e5 2d 66 6f 72 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e  -formed.** journ
125e6 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c  al file then all
125e7 20 70 61 67 65 73 20 75 70 20 74 6f 20 74 68 65   pages up to the
125e8 20 66 69 72 73 74 20 63 6f 72 72 75 70 74 65 64   first corrupted
125e9 20 70 61 67 65 20 61 72 65 20 72 6f 6c 6c 65 64   page are rolled
125ea 0a 2a 2a 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20  .** back (or no 
125eb 70 61 67 65 73 20 69 66 20 74 68 65 20 6a 6f 75  pages if the jou
125ec 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 63  rnal header is c
125ed 6f 72 72 75 70 74 65 64 29 2e 20 54 68 65 20 6a  orrupted). The j
125ee 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69  ournal file.** i
125ef 73 20 74 68 65 6e 20 64 65 6c 65 74 65 64 20 61  s then deleted a
125f0 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
125f1 75 72 6e 65 64 2c 20 6a 75 73 74 20 61 73 20 69  urned, just as i
125f2 66 20 6e 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20  f no corruption 
125f3 68 61 64 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f  had.** been enco
125f4 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  untered..**.** I
125f5 66 20 61 6e 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c  f an I/O or mall
125f6 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72  oc() error occur
125f7 73 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66  s, the journal-f
125f8 69 6c 65 20 69 73 20 6e 6f 74 20 64 65 6c 65 74  ile is not delet
125f9 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72  ed.** and an err
125fa 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
125fb 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ned..**.** The i
125fc 73 48 6f 74 20 70 61 72 61 6d 65 74 65 72 20 69  sHot parameter i
125fd 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 77 65  ndicates that we
125fe 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 72   are trying to r
125ff 6f 6c 6c 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61  ollback a journa
12600 6c 0a 2a 2a 20 74 68 61 74 20 6d 69 67 68 74 20  l.** that might 
12601 62 65 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c  be a hot journal
12602 2e 20 20 4f 72 2c 20 69 74 20 63 6f 75 6c 64 20  .  Or, it could 
12603 62 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72  be that the jour
12604 6e 61 6c 20 69 73 20 0a 2a 2a 20 70 72 65 73 65  nal is .** prese
12605 72 76 65 64 20 62 65 63 61 75 73 65 20 6f 66 20  rved because of 
12606 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53  JOURNALMODE_PERS
12607 49 53 54 20 6f 72 20 4a 4f 55 52 4e 41 4c 4d 4f  IST or JOURNALMO
12608 44 45 5f 54 52 55 4e 43 41 54 45 2e 0a 2a 2a 20  DE_TRUNCATE..** 
12609 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72  If the journal r
1260a 65 61 6c 6c 79 20 69 73 20 68 6f 74 2c 20 72 65  eally is hot, re
1260b 73 65 74 20 74 68 65 20 70 61 67 65 72 20 63 61  set the pager ca
1260c 63 68 65 20 70 72 69 6f 72 20 72 6f 6c 6c 69 6e  che prior rollin
1260d 67 0a 2a 2a 20 62 61 63 6b 20 61 6e 79 20 63 6f  g.** back any co
1260e 6e 74 65 6e 74 2e 20 20 49 66 20 74 68 65 20 6a  ntent.  If the j
1260f 6f 75 72 6e 61 6c 20 69 73 20 6d 65 72 65 6c 79  ournal is merely
12610 20 70 65 72 73 69 73 74 65 6e 74 2c 20 6e 6f 20   persistent, no 
12611 72 65 73 65 74 20 69 73 0a 2a 2a 20 6e 65 65 64  reset is.** need
12612 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
12613 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  t pager_playback
12614 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
12615 69 6e 74 20 69 73 48 6f 74 29 7b 0a 20 20 73 71  int isHot){.  sq
12616 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20  lite3_vfs *pVfs 
12617 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a  = pPager->pVfs;.
12618 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20    i64 szJ;      
12619 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
1261a 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ze of the journa
1261b 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20  l file in bytes 
1261c 2a 2f 0a 20 20 75 33 32 20 6e 52 65 63 3b 20 20  */.  u32 nRec;  
1261d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1261e 20 4e 75 6d 62 65 72 20 6f 66 20 52 65 63 6f 72   Number of Recor
1261f 64 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ds in the journa
12620 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b 20 20 20  l */.  u32 u;   
12621 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12622 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70  /* Unsigned loop
12623 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 50 67   counter */.  Pg
12624 6e 6f 20 6d 78 50 67 20 3d 20 30 3b 20 20 20 20  no mxPg = 0;    
12625 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
12626 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 66  f the original f
12627 69 6c 65 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a  ile in pages */.
12628 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
12629 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1262a 73 75 6c 74 20 63 6f 64 65 20 6f 66 20 61 20 73  sult code of a s
1262b 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  ubroutine */.  i
1262c 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20  nt res = 1;     
1262d 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
1262e 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
1262f 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 2a  ite3OsAccess() *
12630 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  /.  char *zMaste
12631 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  r = 0;       /* 
12632 4e 61 6d 65 20 6f 66 20 6d 61 73 74 65 72 20 6a  Name of master j
12633 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 61  ournal file if a
12634 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 65 64  ny */.  int need
12635 50 61 67 65 72 52 65 73 65 74 3b 20 20 20 20 20  PagerReset;     
12636 20 2f 2a 20 54 72 75 65 20 74 6f 20 72 65 73 65   /* True to rese
12637 74 20 70 61 67 65 20 70 72 69 6f 72 20 74 6f 20  t page prior to 
12638 66 69 72 73 74 20 70 61 67 65 20 72 6f 6c 6c 62  first page rollb
12639 61 63 6b 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67  ack */..  /* Fig
1263a 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
1263b 20 72 65 63 6f 72 64 73 20 61 72 65 20 69 6e 20   records are in 
1263c 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 62  the journal.  Ab
1263d 6f 72 74 20 65 61 72 6c 79 20 69 66 0a 20 20 2a  ort early if.  *
1263e 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  * the journal is
1263f 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 61   empty..  */.  a
12640 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
12641 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20  ager->jfd) );.  
12642 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69  rc = sqlite3OsFi
12643 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a  leSize(pPager->j
12644 66 64 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28  fd, &szJ);.  if(
12645 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
12646 7c 20 73 7a 4a 3d 3d 30 20 29 7b 0a 20 20 20 20  | szJ==0 ){.    
12647 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63  goto end_playbac
12648 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61  k;.  }..  /* Rea
12649 64 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  d the master jou
1264a 72 6e 61 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74  rnal name from t
1264b 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69  he journal, if i
1264c 74 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20  t is present..  
1264d 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a  ** If a master j
1264e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
1264f 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 62   is specified, b
12650 75 74 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e  ut the file is n
12651 6f 74 0a 20 20 2a 2a 20 70 72 65 73 65 6e 74 20  ot.  ** present 
12652 6f 6e 20 64 69 73 6b 2c 20 74 68 65 6e 20 74 68  on disk, then th
12653 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74  e journal is not
12654 20 68 6f 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f   hot and does no
12655 74 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 2a  t need to be.  *
12656 2a 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20  * played back.. 
12657 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 54   **.  ** TODO: T
12658 65 63 68 6e 69 63 61 6c 6c 79 20 74 68 65 20 66  echnically the f
12659 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 6e 20 65  ollowing is an e
1265a 72 72 6f 72 20 62 65 63 61 75 73 65 20 69 74 20  rror because it 
1265b 61 73 73 75 6d 65 73 20 74 68 61 74 0a 20 20 2a  assumes that.  *
1265c 2a 20 62 75 66 66 65 72 20 50 61 67 65 72 2e 70  * buffer Pager.p
1265d 54 6d 70 53 70 61 63 65 20 69 73 20 28 6d 78 50  TmpSpace is (mxP
1265e 61 74 68 6e 61 6d 65 2b 31 29 20 62 79 74 65 73  athname+1) bytes
1265f 20 6f 72 20 6c 61 72 67 65 72 2e 20 69 2e 65 2e   or larger. i.e.
12660 20 74 68 61 74 0a 20 20 2a 2a 20 28 70 50 61 67   that.  ** (pPag
12661 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3e 3d 20  er->pageSize >= 
12662 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78  pPager->pVfs->mx
12663 50 61 74 68 6e 61 6d 65 2b 31 29 2e 20 55 73 69  Pathname+1). Usi
12664 6e 67 20 6f 73 5f 75 6e 69 78 2e 63 2c 0a 20 20  ng os_unix.c,.  
12665 2a 2a 20 20 6d 78 50 61 74 68 6e 61 6d 65 20 69  **  mxPathname i
12666 73 20 35 31 32 2c 20 77 68 69 63 68 20 69 73 20  s 512, which is 
12667 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
12668 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 61 62 6c  minimum allowabl
12669 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 66 6f 72  e value.  ** for
1266a 20 70 61 67 65 53 69 7a 65 2e 0a 20 20 2a 2f 0a   pageSize..  */.
1266b 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 67    zMaster = pPag
1266c 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20  er->pTmpSpace;. 
1266d 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72   rc = readMaster
1266e 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e  Journal(pPager->
1266f 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50  jfd, zMaster, pP
12670 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61  ager->pVfs->mxPa
12671 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 69 66 28  thname+1);.  if(
12672 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
12673 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 29 7b 0a  & zMaster[0] ){.
12674 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12675 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a  OsAccess(pVfs, z
12676 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41  Master, SQLITE_A
12677 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72  CCESS_EXISTS, &r
12678 65 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74  es);.  }.  zMast
12679 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63  er = 0;.  if( rc
1267a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21  !=SQLITE_OK || !
1267b 72 65 73 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  res ){.    goto 
1267c 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20  end_playback;.  
1267d 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  }.  pPager->jour
1267e 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 6e 65  nalOff = 0;.  ne
1267f 65 64 50 61 67 65 72 52 65 73 65 74 20 3d 20 69  edPagerReset = i
12680 73 48 6f 74 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  sHot;..  /* This
12681 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 65 73   loop terminates
12682 20 65 69 74 68 65 72 20 77 68 65 6e 20 61 20 72   either when a r
12683 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 29 20  eadJournalHdr() 
12684 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 72 5f 70  or .  ** pager_p
12685 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65  layback_one_page
12686 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 20  () call returns 
12687 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61  SQLITE_DONE or a
12688 6e 20 49 4f 20 65 72 72 6f 72 20 0a 20 20 2a 2a  n IO error .  **
12689 20 6f 63 63 75 72 73 2e 20 0a 20 20 2a 2f 0a 20   occurs. .  */. 
1268a 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20   while( 1 ){.   
1268b 20 69 6e 74 20 69 73 55 6e 73 79 6e 63 20 3d 20   int isUnsync = 
1268c 30 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20  0;..    /* Read 
1268d 74 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c  the next journal
1268e 20 68 65 61 64 65 72 20 66 72 6f 6d 20 74 68 65   header from the
1268f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20   journal file.  
12690 49 66 20 74 68 65 72 65 20 61 72 65 0a 20 20 20  If there are.   
12691 20 2a 2a 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62   ** not enough b
12692 79 74 65 73 20 6c 65 66 74 20 69 6e 20 74 68 65  ytes left in the
12693 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f   journal file fo
12694 72 20 61 20 63 6f 6d 70 6c 65 74 65 20 68 65 61  r a complete hea
12695 64 65 72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69  der, or.    ** i
12696 74 20 69 73 20 63 6f 72 72 75 70 74 65 64 2c 20  t is corrupted, 
12697 74 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20 6d  then a process m
12698 75 73 74 20 6f 66 20 66 61 69 6c 65 64 20 77 68  ust of failed wh
12699 69 6c 65 20 77 72 69 74 69 6e 67 20 69 74 2e 0a  ile writing it..
1269a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 6e 64 69      ** This indi
1269b 63 61 74 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f  cates nothing mo
1269c 72 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  re needs to be r
1269d 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 20 20 20 20  olled back..    
1269e 2a 2f 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64  */.    rc = read
1269f 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65  JournalHdr(pPage
126a0 72 2c 20 69 73 48 6f 74 2c 20 73 7a 4a 2c 20 26  r, isHot, szJ, &
126a1 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b 0a 20 20  nRec, &mxPg);.  
126a2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
126a3 5f 4f 4b 20 29 7b 20 0a 20 20 20 20 20 20 69 66  _OK ){ .      if
126a4 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
126a5 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  E ){.        rc 
126a6 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
126a7 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20     }.      goto 
126a8 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20  end_playback;.  
126a9 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e    }..    /* If n
126aa 52 65 63 20 69 73 20 30 78 66 66 66 66 66 66 66  Rec is 0xfffffff
126ab 66 2c 20 74 68 65 6e 20 74 68 69 73 20 6a 6f 75  f, then this jou
126ac 72 6e 61 6c 20 77 61 73 20 63 72 65 61 74 65 64  rnal was created
126ad 20 62 79 20 61 20 70 72 6f 63 65 73 73 0a 20 20   by a process.  
126ae 20 20 2a 2a 20 77 6f 72 6b 69 6e 67 20 69 6e 20    ** working in 
126af 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 54 68  no-sync mode. Th
126b0 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  is means that th
126b1 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f  e rest of the jo
126b2 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c  urnal.    ** fil
126b3 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 70 61  e consists of pa
126b4 67 65 73 2c 20 74 68 65 72 65 20 61 72 65 20 6e  ges, there are n
126b5 6f 20 6d 6f 72 65 20 6a 6f 75 72 6e 61 6c 20 68  o more journal h
126b6 65 61 64 65 72 73 2e 20 43 6f 6d 70 75 74 65 0a  eaders. Compute.
126b7 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65      ** the value
126b8 20 6f 66 20 6e 52 65 63 20 62 61 73 65 64 20 6f   of nRec based o
126b9 6e 20 74 68 69 73 20 61 73 73 75 6d 70 74 69 6f  n this assumptio
126ba 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  n..    */.    if
126bb 28 20 6e 52 65 63 3d 3d 30 78 66 66 66 66 66 66  ( nRec==0xffffff
126bc 66 66 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  ff ){.      asse
126bd 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  rt( pPager->jour
126be 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f  nalOff==JOURNAL_
126bf 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29  HDR_SZ(pPager) )
126c0 3b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20 28  ;.      nRec = (
126c1 69 6e 74 29 28 28 73 7a 4a 20 2d 20 4a 4f 55 52  int)((szJ - JOUR
126c2 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
126c3 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53  r))/JOURNAL_PG_S
126c4 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20  Z(pPager));.    
126c5 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65  }..    /* If nRe
126c6 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 73 20  c is 0 and this 
126c7 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 20 61  rollback is of a
126c8 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 72 65   transaction cre
126c9 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 20 20  ated by this.   
126ca 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e 64 20   ** process and 
126cb 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  if this is the f
126cc 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 74  inal header in t
126cd 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 6e  he journal, then
126ce 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a   it means.    **
126cf 20 74 68 61 74 20 74 68 69 73 20 70 61 72 74 20   that this part 
126d0 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77  of the journal w
126d1 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65 64 20  as being filled 
126d2 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20  but has not yet 
126d3 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79 6e 63  been.    ** sync
126d4 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43 6f 6d  ed to disk.  Com
126d5 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  pute the number 
126d6 6f 66 20 70 61 67 65 73 20 62 61 73 65 64 20 6f  of pages based o
126d7 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e 67 0a  n the remaining.
126d8 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 74      ** size of t
126d9 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a  he file..    **.
126da 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69 72 64      ** The third
126db 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74 65 73   term of the tes
126dc 74 20 77 61 73 20 61 64 64 65 64 20 74 6f 20 66  t was added to f
126dd 69 78 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e  ix ticket #2565.
126de 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 72 6f 6c  .    ** When rol
126df 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 20  ling back a hot 
126e0 6a 6f 75 72 6e 61 6c 2c 20 6e 52 65 63 3d 3d 30  journal, nRec==0
126e1 20 61 6c 77 61 79 73 20 6d 65 61 6e 73 20 74 68   always means th
126e2 61 74 20 74 68 65 20 6e 65 78 74 0a 20 20 20 20  at the next.    
126e3 2a 2a 20 63 68 75 6e 6b 20 6f 66 20 74 68 65 20  ** chunk of the 
126e4 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e 73  journal contains
126e5 20 7a 65 72 6f 20 70 61 67 65 73 20 74 6f 20 62   zero pages to b
126e6 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20  e rolled back.  
126e7 42 75 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20  But.    ** when 
126e8 64 6f 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b  doing a ROLLBACK
126e9 20 61 6e 64 20 74 68 65 20 6e 52 65 63 3d 3d 30   and the nRec==0
126ea 20 63 68 75 6e 6b 20 69 73 20 74 68 65 20 6c 61   chunk is the la
126eb 73 74 20 63 68 75 6e 6b 20 69 6e 0a 20 20 20 20  st chunk in.    
126ec 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20  ** the journal, 
126ed 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
126ee 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20  e journal might 
126ef 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69 6f 6e  contain addition
126f0 61 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 73 20  al.    ** pages 
126f1 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20  that need to be 
126f2 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 20  rolled back and 
126f3 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20  that the number 
126f4 6f 66 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a  of pages .    **
126f5 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75   should be compu
126f6 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ted based on the
126f7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 69   journal file si
126f8 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ze..    */.    i
126f9 66 28 20 6e 52 65 63 3d 3d 30 20 26 26 20 21 69  f( nRec==0 && !i
126fa 73 48 6f 74 20 26 26 0a 20 20 20 20 20 20 20 20  sHot &&.        
126fb 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48  pPager->journalH
126fc 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  dr+JOURNAL_HDR_S
126fd 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65  Z(pPager)==pPage
126fe 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b  r->journalOff ){
126ff 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20 28 69  .      nRec = (i
12700 6e 74 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65  nt)((szJ - pPage
12701 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f  r->journalOff) /
12702 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70   JOURNAL_PG_SZ(p
12703 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20 69  Pager));.      i
12704 73 55 6e 73 79 6e 63 20 3d 20 31 3b 0a 20 20 20  sUnsync = 1;.   
12705 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
12706 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20  is is the first 
12707 68 65 61 64 65 72 20 72 65 61 64 20 66 72 6f 6d  header read from
12708 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 72   the journal, tr
12709 75 6e 63 61 74 65 20 74 68 65 0a 20 20 20 20 2a  uncate the.    *
1270a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
1270b 62 61 63 6b 20 74 6f 20 69 74 73 20 6f 72 69 67  back to its orig
1270c 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20 20 20 2a  inal size..    *
1270d 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  /.    if( pPager
1270e 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f  ->journalOff==JO
1270f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
12710 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63  ger) ){.      rc
12711 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74   = pager_truncat
12712 65 28 70 50 61 67 65 72 2c 20 6d 78 50 67 29 3b  e(pPager, mxPg);
12713 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
12714 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
12715 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61      goto end_pla
12716 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  yback;.      }. 
12717 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53       pPager->dbS
12718 69 7a 65 20 3d 20 6d 78 50 67 3b 0a 20 20 20 20  ize = mxPg;.    
12719 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f  }..    /* Copy o
1271a 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 6f 75  riginal pages ou
1271b 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t of the journal
1271c 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f 20 74   and back into t
1271d 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  he .    ** datab
1271e 61 73 65 20 66 69 6c 65 20 61 6e 64 2f 6f 72 20  ase file and/or 
1271f 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 20 20  page cache..    
12720 2a 2f 0a 20 20 20 20 66 6f 72 28 75 3d 30 3b 20  */.    for(u=0; 
12721 75 3c 6e 52 65 63 3b 20 75 2b 2b 29 7b 0a 20 20  u<nRec; u++){.  
12722 20 20 20 20 69 66 28 20 6e 65 65 64 50 61 67 65      if( needPage
12723 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20  rReset ){.      
12724 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50    pager_reset(pP
12725 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 6e  ager);.        n
12726 65 65 64 50 61 67 65 72 52 65 73 65 74 20 3d 20  eedPagerReset = 
12727 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
12728 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79   rc = pager_play
12729 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50  back_one_page(pP
1272a 61 67 65 72 2c 31 2c 69 73 55 6e 73 79 6e 63 2c  ager,1,isUnsync,
1272b 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  &pPager->journal
1272c 4f 66 66 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20  Off,0,0);.      
1272d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1272e 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  K ){.        if(
1272f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
12730 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
12731 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
12732 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
12733 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a  journalOff = szJ
12734 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
12735 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  k;.        }else
12736 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  {.          /* I
12737 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20  f we are unable 
12738 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 71 75 69  to rollback, qui
12739 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  t and return the
1273a 20 65 72 72 6f 72 0a 20 20 20 20 20 20 20 20 20   error.         
1273b 20 2a 2a 20 63 6f 64 65 2e 20 20 54 68 69 73 20   ** code.  This 
1273c 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20 70  will cause the p
1273d 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68  ager to enter th
1273e 65 20 65 72 72 6f 72 20 73 74 61 74 65 0a 20 20  e error state.  
1273f 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 74 68          ** so th
12740 61 74 20 6e 6f 20 66 75 72 74 68 65 72 20 68 61  at no further ha
12741 72 6d 20 77 69 6c 6c 20 62 65 20 64 6f 6e 65 2e  rm will be done.
12742 20 20 50 65 72 68 61 70 73 20 74 68 65 20 6e 65    Perhaps the ne
12743 78 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  xt.          ** 
12744 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 6d 65 20  process to come 
12745 61 6c 6f 6e 67 20 77 69 6c 6c 20 62 65 20 61 62  along will be ab
12746 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  le to rollback t
12747 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20  he database..   
12748 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
12749 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61      goto end_pla
1274a 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d  yback;.        }
1274b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1274c 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45   }.  /*NOTREACHE
1274d 44 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 20  D*/.  assert( 0 
1274e 29 3b 0a 0a 65 6e 64 5f 70 6c 61 79 62 61 63 6b  );..end_playback
1274f 3a 0a 20 20 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67  :.  /* Following
12750 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65   a rollback, the
12751 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73   database file s
12752 68 6f 75 6c 64 20 62 65 20 62 61 63 6b 20 69 6e  hould be back in
12753 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 0a 20 20   its original.  
12754 2a 2a 20 73 74 61 74 65 20 70 72 69 6f 72 20 74  ** state prior t
12755 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  o the start of t
12756 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  he transaction, 
12757 73 6f 20 69 6e 76 6f 6b 65 20 74 68 65 0a 20 20  so invoke the.  
12758 2a 2a 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  ** SQLITE_FCNTL_
12759 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 66 69 6c  DB_UNCHANGED fil
1275a 65 2d 63 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64  e-control method
1275b 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 0a   to disable the.
1275c 20 20 2a 2a 20 61 73 73 65 72 74 69 6f 6e 20 74    ** assertion t
1275d 68 61 74 20 74 68 65 20 74 72 61 6e 73 61 63 74  hat the transact
1275e 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 61 73 20  ion counter was 
1275f 6d 6f 64 69 66 69 65 64 2e 0a 20 20 2a 2f 0a 20  modified..  */. 
12760 20 61 73 73 65 72 74 28 0a 20 20 20 20 70 50 61   assert(.    pPa
12761 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64  ger->fd->pMethod
12762 73 3d 3d 30 20 7c 7c 0a 20 20 20 20 73 71 6c 69  s==0 ||.    sqli
12763 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
12764 28 70 50 61 67 65 72 2d 3e 66 64 2c 53 51 4c 49  (pPager->fd,SQLI
12765 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48  TE_FCNTL_DB_UNCH
12766 41 4e 47 45 44 2c 30 29 3e 3d 53 51 4c 49 54 45  ANGED,0)>=SQLITE
12767 5f 4f 4b 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49  _OK.  );..  /* I
12768 66 20 74 68 69 73 20 70 6c 61 79 62 61 63 6b 20  f this playback 
12769 69 73 20 68 61 70 70 65 6e 69 6e 67 20 61 75 74  is happening aut
1276a 6f 6d 61 74 69 63 61 6c 6c 79 20 61 73 20 61 20  omatically as a 
1276b 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 49 4f 20  result of an IO 
1276c 6f 72 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20  or .  ** malloc 
1276d 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72  error that occur
1276e 72 65 64 20 61 66 74 65 72 20 74 68 65 20 63 68  red after the ch
1276f 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 77 61 73  ange-counter was
12770 20 75 70 64 61 74 65 64 20 62 75 74 20 0a 20 20   updated but .  
12771 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 74 72  ** before the tr
12772 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 63 6f  ansaction was co
12773 6d 6d 69 74 74 65 64 2c 20 74 68 65 6e 20 74 68  mmitted, then th
12774 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
12775 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74   .  ** modificat
12776 69 6f 6e 20 6d 61 79 20 6a 75 73 74 20 68 61 76  ion may just hav
12777 65 20 62 65 65 6e 20 72 65 76 65 72 74 65 64 2e  e been reverted.
12778 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73   If this happens
12779 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 0a 20   in exclusive . 
1277a 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20 73   ** mode, then s
1277b 75 62 73 65 71 75 65 6e 74 20 74 72 61 6e 73 61  ubsequent transa
1277c 63 74 69 6f 6e 73 20 70 65 72 66 6f 72 6d 65 64  ctions performed
1277d 20 62 79 20 74 68 65 20 63 6f 6e 6e 65 63 74 69   by the connecti
1277e 6f 6e 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a  on will not.  **
1277f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e   update the chan
12780 67 65 2d 63 6f 75 6e 74 65 72 20 61 74 20 61 6c  ge-counter at al
12781 6c 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 64  l. This may lead
12782 20 74 6f 20 63 61 63 68 65 20 69 6e 63 6f 6e 73   to cache incons
12783 69 73 74 65 6e 63 79 0a 20 20 2a 2a 20 70 72 6f  istency.  ** pro
12784 62 6c 65 6d 73 20 66 6f 72 20 6f 74 68 65 72 20  blems for other 
12785 70 72 6f 63 65 73 73 65 73 20 61 74 20 73 6f 6d  processes at som
12786 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66  e point in the f
12787 75 74 75 72 65 2e 20 53 6f 2c 20 6a 75 73 74 0a  uture. So, just.
12788 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 69    ** in case thi
12789 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20  s has happened, 
1278a 63 6c 65 61 72 20 74 68 65 20 63 68 61 6e 67 65  clear the change
1278b 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 6e  CountDone flag n
1278c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65  ow..  */.  pPage
1278d 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
1278e 6e 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d  ne = pPager->tem
1278f 70 46 69 6c 65 3b 0a 0a 20 20 69 66 28 20 72 63  pFile;..  if( rc
12790 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
12791 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61     zMaster = pPa
12792 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a  ger->pTmpSpace;.
12793 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73      rc = readMas
12794 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  terJournal(pPage
12795 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c  r->jfd, zMaster,
12796 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d   pPager->pVfs->m
12797 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20  xPathname+1);.  
12798 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d    testcase( rc!=
12799 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d  SQLITE_OK );.  }
1279a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1279b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
1279c 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73   pager_end_trans
1279d 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 7a  action(pPager, z
1279e 4d 61 73 74 65 72 5b 30 5d 21 3d 27 5c 30 27 29  Master[0]!='\0')
1279f 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
127a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
127a1 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
127a2 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73  QLITE_OK && zMas
127a3 74 65 72 5b 30 5d 20 26 26 20 72 65 73 20 29 7b  ter[0] && res ){
127a4 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
127a5 20 77 61 73 20 61 20 6d 61 73 74 65 72 20 6a 6f   was a master jo
127a6 75 72 6e 61 6c 20 61 6e 64 20 74 68 69 73 20 72  urnal and this r
127a7 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75  outine will retu
127a8 72 6e 20 73 75 63 63 65 73 73 2c 0a 20 20 20 20  rn success,.    
127a9 2a 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 20  ** see if it is 
127aa 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65  possible to dele
127ab 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
127ac 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20  urnal..    */.  
127ad 20 20 72 63 20 3d 20 70 61 67 65 72 5f 64 65 6c    rc = pager_del
127ae 6d 61 73 74 65 72 28 70 50 61 67 65 72 2c 20 7a  master(pPager, z
127af 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 74 65 73  Master);.    tes
127b0 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54  tcase( rc!=SQLIT
127b1 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  E_OK );.  }..  /
127b2 2a 20 54 68 65 20 50 61 67 65 72 2e 73 65 63 74  * The Pager.sect
127b3 6f 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20  orSize variable 
127b4 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 75 70  may have been up
127b5 64 61 74 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c  dated while roll
127b6 69 6e 67 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20  ing.  ** back a 
127b7 6a 6f 75 72 6e 61 6c 20 63 72 65 61 74 65 64 20  journal created 
127b8 62 79 20 61 20 70 72 6f 63 65 73 73 20 77 69 74  by a process wit
127b9 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 73 65  h a different se
127ba 63 74 6f 72 20 73 69 7a 65 0a 20 20 2a 2a 20 76  ctor size.  ** v
127bb 61 6c 75 65 2e 20 52 65 73 65 74 20 69 74 20 74  alue. Reset it t
127bc 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 76 61  o the correct va
127bd 6c 75 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f  lue for this pro
127be 63 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 65 74  cess..  */.  set
127bf 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65  SectorSize(pPage
127c0 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  r);.  return rc;
127c1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61  .}../*.** Playba
127c2 63 6b 20 73 61 76 65 70 6f 69 6e 74 20 70 53 61  ck savepoint pSa
127c3 76 65 70 6f 69 6e 74 2e 20 4f 72 2c 20 69 66 20  vepoint. Or, if 
127c4 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c  pSavepoint==NULL
127c5 2c 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 0a  , then playback.
127c6 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61  ** the entire ma
127c7 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
127c8 65 2e 20 54 68 65 20 63 61 73 65 20 70 53 61 76  e. The case pSav
127c9 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 20 6f 63 63  epoint==NULL occ
127ca 75 72 73 20 77 68 65 6e 20 0a 2a 2a 20 61 20 52  urs when .** a R
127cb 4f 4c 4c 42 41 43 4b 20 54 4f 20 63 6f 6d 6d 61  OLLBACK TO comma
127cc 6e 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  nd is invoked on
127cd 20 61 20 53 41 56 45 50 4f 49 4e 54 20 74 68 61   a SAVEPOINT tha
127ce 74 20 69 73 20 61 20 74 72 61 6e 73 61 63 74 69  t is a transacti
127cf 6f 6e 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74  on .** savepoint
127d0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 53 61  ..**.** When pSa
127d1 76 65 70 6f 69 6e 74 20 69 73 20 6e 6f 74 20 4e  vepoint is not N
127d2 55 4c 4c 20 28 6d 65 61 6e 69 6e 67 20 61 20 6e  ULL (meaning a n
127d3 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73  on-transaction s
127d4 61 76 65 70 6f 69 6e 74 20 69 73 20 0a 2a 2a 20  avepoint is .** 
127d5 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63  being rolled bac
127d6 6b 29 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 6c  k), then the rol
127d7 6c 62 61 63 6b 20 63 6f 6e 73 69 73 74 73 20 6f  lback consists o
127d8 66 20 75 70 20 74 6f 20 74 68 72 65 65 20 73 74  f up to three st
127d9 61 67 65 73 2c 0a 2a 2a 20 70 65 72 66 6f 72 6d  ages,.** perform
127da 65 64 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20  ed in the order 
127db 73 70 65 63 69 66 69 65 64 3a 0a 2a 2a 0a 2a 2a  specified:.**.**
127dc 20 20 20 2a 20 50 61 67 65 73 20 61 72 65 20 70     * Pages are p
127dd 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20  layed back from 
127de 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
127df 20 73 74 61 72 74 69 6e 67 20 61 74 20 62 79 74   starting at byt
127e0 65 0a 2a 2a 20 20 20 20 20 6f 66 66 73 65 74 20  e.**     offset 
127e1 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69  PagerSavepoint.i
127e2 4f 66 66 73 65 74 20 61 6e 64 20 63 6f 6e 74 69  Offset and conti
127e3 6e 75 69 6e 67 20 74 6f 20 0a 2a 2a 20 20 20 20  nuing to .**    
127e4 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e   PagerSavepoint.
127e5 69 48 64 72 4f 66 66 73 65 74 2c 20 6f 72 20 74  iHdrOffset, or t
127e6 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
127e7 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a   main journal.**
127e8 20 20 20 20 20 66 69 6c 65 20 69 66 20 50 61 67       file if Pag
127e9 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72  erSavepoint.iHdr
127ea 4f 66 66 73 65 74 20 69 73 20 7a 65 72 6f 2e 0a  Offset is zero..
127eb 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 50 61 67  **.**   * If Pag
127ec 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72  erSavepoint.iHdr
127ed 4f 66 66 73 65 74 20 69 73 20 6e 6f 74 20 7a 65  Offset is not ze
127ee 72 6f 2c 20 74 68 65 6e 20 70 61 67 65 73 20 61  ro, then pages a
127ef 72 65 20 70 6c 61 79 65 64 0a 2a 2a 20 20 20 20  re played.**    
127f0 20 62 61 63 6b 20 73 74 61 72 74 69 6e 67 20 66   back starting f
127f1 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  rom the journal 
127f2 68 65 61 64 65 72 20 69 6d 6d 65 64 69 61 74 65  header immediate
127f3 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a  ly following .**
127f4 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f       PagerSavepo
127f5 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 74  int.iHdrOffset t
127f6 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
127f7 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69   main journal fi
127f8 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61  le..**.**   * Pa
127f9 67 65 73 20 61 72 65 20 74 68 65 6e 20 70 6c 61  ges are then pla
127fa 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68  yed back from th
127fb 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69  e sub-journal fi
127fc 6c 65 2c 20 73 74 61 72 74 69 6e 67 0a 2a 2a 20  le, starting.** 
127fd 20 20 20 20 77 69 74 68 20 74 68 65 20 50 61 67      with the Pag
127fe 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 53 75 62  erSavepoint.iSub
127ff 52 65 63 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69  Rec and continui
12800 6e 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  ng to the end of
12801 0a 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f 75 72  .**     the jour
12802 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  nal file..**.** 
12803 54 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20 72  Throughout the r
12804 6f 6c 6c 62 61 63 6b 20 70 72 6f 63 65 73 73 2c  ollback process,
12805 20 65 61 63 68 20 74 69 6d 65 20 61 20 70 61 67   each time a pag
12806 65 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  e is rolled back
12807 2c 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70  , the.** corresp
12808 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 20 73 65  onding bit is se
12809 74 20 69 6e 20 61 20 62 69 74 76 65 63 20 73 74  t in a bitvec st
1280a 72 75 63 74 75 72 65 20 28 76 61 72 69 61 62 6c  ructure (variabl
1280b 65 20 70 44 6f 6e 65 20 69 6e 20 74 68 65 0a 2a  e pDone in the.*
1280c 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
1280d 20 62 65 6c 6f 77 29 2e 20 54 68 69 73 20 69 73   below). This is
1280e 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20   used to ensure 
1280f 74 68 61 74 20 61 20 70 61 67 65 20 69 73 20 6f  that a page is o
12810 6e 6c 79 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61  nly.** rolled ba
12811 63 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  ck the first tim
12812 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e 74 65  e it is encounte
12813 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 6a 6f  red in either jo
12814 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urnal..**.** If 
12815 70 53 61 76 65 70 6f 69 6e 74 20 69 73 20 4e 55  pSavepoint is NU
12816 4c 4c 2c 20 74 68 65 6e 20 70 61 67 65 73 20 61  LL, then pages a
12817 72 65 20 6f 6e 6c 79 20 70 6c 61 79 65 64 20 62  re only played b
12818 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69  ack from the mai
12819 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  n.** journal fil
1281a 65 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e  e. There is no n
1281b 65 65 64 20 66 6f 72 20 61 20 62 69 74 76 65 63  eed for a bitvec
1281c 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a   in this case..*
1281d 2a 0a 2a 2a 20 49 6e 20 65 69 74 68 65 72 20 63  *.** In either c
1281e 61 73 65 2c 20 62 65 66 6f 72 65 20 70 6c 61 79  ase, before play
1281f 62 61 63 6b 20 63 6f 6d 6d 65 6e 63 65 73 20 74  back commences t
12820 68 65 20 50 61 67 65 72 2e 64 62 53 69 7a 65 20  he Pager.dbSize 
12821 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 72  variable.** is r
12822 65 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75  eset to the valu
12823 65 20 74 68 61 74 20 69 74 20 68 65 6c 64 20 61  e that it held a
12824 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
12825 68 65 20 73 61 76 65 70 6f 69 6e 74 20 0a 2a 2a  he savepoint .**
12826 20 28 6f 72 20 74 72 61 6e 73 61 63 74 69 6f 6e   (or transaction
12827 29 2e 20 4e 6f 20 70 61 67 65 20 77 69 74 68 20  ). No page with 
12828 61 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 67 72  a page-number gr
12829 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 20  eater than this 
1282a 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 6c 61 79  value.** is play
1282b 65 64 20 62 61 63 6b 2e 20 49 66 20 6f 6e 65 20  ed back. If one 
1282c 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69  is encountered i
1282d 74 20 69 73 20 73 69 6d 70 6c 79 20 73 6b 69 70  t is simply skip
1282e 70 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ped..*/.static i
1282f 6e 74 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b  nt pagerPlayback
12830 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20  Savepoint(Pager 
12831 2a 70 50 61 67 65 72 2c 20 50 61 67 65 72 53 61  *pPager, PagerSa
12832 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f  vepoint *pSavepo
12833 69 6e 74 29 7b 0a 20 20 69 36 34 20 73 7a 4a 3b  int){.  i64 szJ;
12834 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12835 20 2f 2a 20 45 66 66 65 63 74 69 76 65 20 73 69   /* Effective si
12836 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  ze of the main j
12837 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20  ournal */.  i64 
12838 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20  iHdrOff;        
12839 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 66       /* End of f
1283a 69 72 73 74 20 73 65 67 6d 65 6e 74 20 6f 66 20  irst segment of 
1283b 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 20 72 65 63  main-journal rec
1283c 6f 72 64 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ords */.  int rc
1283d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
1283e 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
1283f 65 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70  e */.  Bitvec *p
12840 44 6f 6e 65 20 3d 20 30 3b 20 20 20 20 20 20 20  Done = 0;       
12841 2f 2a 20 42 69 74 76 65 63 20 74 6f 20 65 6e 73  /* Bitvec to ens
12842 75 72 65 20 70 61 67 65 73 20 70 6c 61 79 65 64  ure pages played
12843 20 62 61 63 6b 20 6f 6e 6c 79 20 6f 6e 63 65 20   back only once 
12844 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
12845 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
12846 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20 20  ER_SHARED );..  
12847 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62 69  /* Allocate a bi
12848 74 76 65 63 20 74 6f 20 75 73 65 20 74 6f 20 73  tvec to use to s
12849 74 6f 72 65 20 74 68 65 20 73 65 74 20 6f 66 20  tore the set of 
1284a 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61 63  pages rolled bac
1284b 6b 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65  k */.  if( pSave
1284c 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 70 44 6f  point ){.    pDo
1284d 6e 65 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  ne = sqlite3Bitv
1284e 65 63 43 72 65 61 74 65 28 70 53 61 76 65 70 6f  ecCreate(pSavepo
1284f 69 6e 74 2d 3e 6e 4f 72 69 67 29 3b 0a 20 20 20  int->nOrig);.   
12850 20 69 66 28 20 21 70 44 6f 6e 65 20 29 7b 0a 20   if( !pDone ){. 
12851 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
12852 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
12853 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68    }..  /* Set th
12854 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  e database size 
12855 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c 75  back to the valu
12856 65 20 69 74 20 77 61 73 20 62 65 66 6f 72 65 20  e it was before 
12857 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 0a 20  the savepoint . 
12858 20 2a 2a 20 62 65 69 6e 67 20 72 65 76 65 72 74   ** being revert
12859 65 64 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 20  ed was opened.. 
1285a 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62   */.  pPager->db
1285b 53 69 7a 65 20 3d 20 70 53 61 76 65 70 6f 69 6e  Size = pSavepoin
1285c 74 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e  t ? pSavepoint->
1285d 6e 4f 72 69 67 20 3a 20 70 50 61 67 65 72 2d 3e  nOrig : pPager->
1285e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 0a 20 20 2f  dbOrigSize;..  /
1285f 2a 20 55 73 65 20 70 50 61 67 65 72 2d 3e 6a 6f  * Use pPager->jo
12860 75 72 6e 61 6c 4f 66 66 20 61 73 20 74 68 65 20  urnalOff as the 
12861 65 66 66 65 63 74 69 76 65 20 73 69 7a 65 20 6f  effective size o
12862 66 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62  f the main rollb
12863 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c  ack.  ** journal
12864 2e 20 20 54 68 65 20 61 63 74 75 61 6c 20 66 69  .  The actual fi
12865 6c 65 20 6d 69 67 68 74 20 62 65 20 6c 61 72 67  le might be larg
12866 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 6e 0a  er than this in.
12867 20 20 2a 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e    ** PAGER_JOURN
12868 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20  ALMODE_TRUNCATE 
12869 6f 72 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  or PAGER_JOURNAL
1286a 4d 4f 44 45 5f 50 45 52 53 49 53 54 2e 20 20 42  MODE_PERSIST.  B
1286b 75 74 20 61 6e 79 74 68 69 6e 67 0a 20 20 2a 2a  ut anything.  **
1286c 20 70 61 73 74 20 70 50 61 67 65 72 2d 3e 6a 6f   past pPager->jo
1286d 75 72 6e 61 6c 4f 66 66 20 69 73 20 6f 66 66 2d  urnalOff is off-
1286e 6c 69 6d 69 74 73 20 74 6f 20 75 73 2e 0a 20 20  limits to us..  
1286f 2a 2f 0a 20 20 73 7a 4a 20 3d 20 70 50 61 67 65  */.  szJ = pPage
12870 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a  r->journalOff;..
12871 20 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 72 6f    /* Begin by ro
12872 6c 6c 69 6e 67 20 62 61 63 6b 20 72 65 63 6f 72  lling back recor
12873 64 73 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e  ds from the main
12874 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e   journal startin
12875 67 20 61 74 0a 20 20 2a 2a 20 50 61 67 65 72 53  g at.  ** PagerS
12876 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74  avepoint.iOffset
12877 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20   and continuing 
12878 74 6f 20 74 68 65 20 6e 65 78 74 20 6a 6f 75 72  to the next jour
12879 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a  nal header..  **
1287a 20 54 68 65 72 65 20 6d 69 67 68 74 20 62 65 20   There might be 
1287b 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 6d  records in the m
1287c 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74  ain journal that
1287d 20 68 61 76 65 20 61 20 70 61 67 65 20 6e 75 6d   have a page num
1287e 62 65 72 0a 20 20 2a 2a 20 67 72 65 61 74 65 72  ber.  ** greater
1287f 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e   than the curren
12880 74 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  t database size 
12881 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29  (pPager->dbSize)
12882 20 62 75 74 20 74 68 6f 73 65 0a 20 20 2a 2a 20   but those.  ** 
12883 77 69 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 20  will be skipped 
12884 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20  automatically.  
12885 50 61 67 65 73 20 61 72 65 20 61 64 64 65 64 20  Pages are added 
12886 74 6f 20 70 44 6f 6e 65 20 61 73 20 74 68 65 79  to pDone as they
12887 0a 20 20 2a 2a 20 61 72 65 20 70 6c 61 79 65 64  .  ** are played
12888 20 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66   back..  */.  if
12889 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a  ( pSavepoint ){.
1288a 20 20 20 20 69 48 64 72 4f 66 66 20 3d 20 70 53      iHdrOff = pS
1288b 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66  avepoint->iHdrOf
1288c 66 73 65 74 20 3f 20 70 53 61 76 65 70 6f 69 6e  fset ? pSavepoin
1288d 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3a 20  t->iHdrOffset : 
1288e 73 7a 4a 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  szJ;.    pPager-
1288f 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 70 53  >journalOff = pS
12890 61 76 65 70 6f 69 6e 74 2d 3e 69 4f 66 66 73 65  avepoint->iOffse
12891 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 72 63  t;.    while( rc
12892 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
12893 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
12894 66 3c 69 48 64 72 4f 66 66 20 29 7b 0a 20 20 20  f<iHdrOff ){.   
12895 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c     rc = pager_pl
12896 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28  ayback_one_page(
12897 70 50 61 67 65 72 2c 20 31 2c 20 30 2c 20 26 70  pPager, 1, 0, &p
12898 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
12899 66 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20  f, 1, pDone);.  
1289a 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1289b 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
1289c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1289d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
1289e 66 66 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  ff = 0;.  }..  /
1289f 2a 20 43 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69  * Continue rolli
128a0 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20  ng back records 
128a1 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  out of the main 
128a2 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67  journal starting
128a3 20 61 74 0a 20 20 2a 2a 20 74 68 65 20 66 69 72   at.  ** the fir
128a4 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  st journal heade
128a5 72 20 73 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69  r seen and conti
128a6 6e 75 69 6e 67 20 75 6e 74 69 6c 20 74 68 65 20  nuing until the 
128a7 65 66 66 65 63 74 69 76 65 20 65 6e 64 0a 20 20  effective end.  
128a8 2a 2a 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  ** of the main j
128a9 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f  ournal file.  Co
128aa 6e 74 69 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f  ntinue to skip o
128ab 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65  ut-of-range page
128ac 73 20 61 6e 64 0a 20 20 2a 2a 20 63 6f 6e 74 69  s and.  ** conti
128ad 6e 75 65 20 61 64 64 69 6e 67 20 70 61 67 65 73  nue adding pages
128ae 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 6f 20   rolled back to 
128af 70 44 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 77 68  pDone..  */.  wh
128b0 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
128b1 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f  OK && pPager->jo
128b2 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a  urnalOff<szJ ){.
128b3 20 20 20 20 75 33 32 20 69 69 3b 20 20 20 20 20      u32 ii;     
128b4 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
128b5 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 33  ounter */.    u3
128b6 32 20 6e 4a 52 65 63 20 3d 20 30 3b 20 20 20 20  2 nJRec = 0;    
128b7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f   /* Number of Jo
128b8 75 72 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f  urnal Records */
128b9 0a 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a  .    u32 dummy;.
128ba 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75      rc = readJou
128bb 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20  rnalHdr(pPager, 
128bc 30 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20  0, szJ, &nJRec, 
128bd 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73  &dummy);.    ass
128be 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
128bf 44 4f 4e 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a  DONE );..    /*.
128c0 20 20 20 20 2a 2a 20 54 68 65 20 22 70 50 61 67      ** The "pPag
128c1 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a  er->journalHdr+J
128c2 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
128c3 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a  ager)==pPager->j
128c4 6f 75 72 6e 61 6c 4f 66 66 22 0a 20 20 20 20 2a  ournalOff".    *
128c5 2a 20 74 65 73 74 20 69 73 20 72 65 6c 61 74 65  * test is relate
128c6 64 20 74 6f 20 74 69 63 6b 65 74 20 23 32 35 36  d to ticket #256
128c7 35 2e 20 20 53 65 65 20 74 68 65 20 64 69 73 63  5.  See the disc
128c8 75 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 20 20  ussion in the.  
128c9 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62    ** pager_playb
128ca 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ack() function f
128cb 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
128cc 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a  formation..    *
128cd 2f 0a 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d  /.    if( nJRec=
128ce 3d 30 20 0a 20 20 20 20 20 26 26 20 70 50 61 67  =0 .     && pPag
128cf 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a  er->journalHdr+J
128d0 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
128d1 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a  ager)==pPager->j
128d2 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b  ournalOff.    ){
128d3 0a 20 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28  .      nJRec = (
128d4 75 33 32 29 28 28 73 7a 4a 20 2d 20 70 50 61 67  u32)((szJ - pPag
128d5 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f  er->journalOff)/
128d6 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50  JOURNAL_PG_SZ(pP
128d7 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20  ager));.    }.  
128d8 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d    for(ii=0; rc==
128d9 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c  SQLITE_OK && ii<
128da 6e 4a 52 65 63 20 26 26 20 70 50 61 67 65 72 2d  nJRec && pPager-
128db 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b  >journalOff<szJ;
128dc 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63   ii++){.      rc
128dd 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63   = pager_playbac
128de 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65  k_one_page(pPage
128df 72 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72  r, 1, 0, &pPager
128e0 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c  ->journalOff, 1,
128e1 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20   pDone);.    }. 
128e2 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
128e3 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20  QLITE_DONE );.  
128e4 7d 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d  }.  assert( rc!=
128e5 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61  SQLITE_OK || pPa
128e6 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
128e7 3d 73 7a 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69  =szJ );..  /* Fi
128e8 6e 61 6c 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b  nally,  rollback
128e9 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20   pages from the 
128ea 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61  sub-journal.  Pa
128eb 67 65 20 74 68 61 74 20 77 65 72 65 0a 20 20 2a  ge that were.  *
128ec 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c  * previously rol
128ed 6c 65 64 20 62 61 63 6b 20 6f 75 74 20 6f 66 20  led back out of 
128ee 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
128ef 20 28 61 6e 64 20 61 72 65 20 68 65 6e 63 65 20   (and are hence 
128f0 69 6e 20 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77  in pDone).  ** w
128f1 69 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 2e 20  ill be skipped. 
128f2 20 4f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61   Out-of-range pa
128f3 67 65 73 20 61 72 65 20 61 6c 73 6f 20 73 6b 69  ges are also ski
128f4 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  pped..  */.  if(
128f5 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20   pSavepoint ){. 
128f6 20 20 20 75 33 32 20 69 69 3b 20 20 20 20 20 20     u32 ii;      
128f7 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
128f8 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 36 34  unter */.    i64
128f9 20 6f 66 66 73 65 74 20 3d 20 70 53 61 76 65 70   offset = pSavep
128fa 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 2a 28 34  oint->iSubRec*(4
128fb 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  +pPager->pageSiz
128fc 65 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 70  e);.    for(ii=p
128fd 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52  Savepoint->iSubR
128fe 65 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  ec; rc==SQLITE_O
128ff 4b 20 26 26 20 69 69 3c 70 50 61 67 65 72 2d 3e  K && ii<pPager->
12900 6e 53 75 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a  nSubRec; ii++){.
12901 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 66        assert( of
12902 66 73 65 74 3d 3d 69 69 2a 28 34 2b 70 50 61 67  fset==ii*(4+pPag
12903 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 3b  er->pageSize) );
12904 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
12905 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
12906 61 67 65 28 70 50 61 67 65 72 2c 20 30 2c 20 30  age(pPager, 0, 0
12907 2c 20 26 6f 66 66 73 65 74 2c 20 31 2c 20 70 44  , &offset, 1, pD
12908 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  one);.    }.    
12909 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
1290a 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 0a  TE_DONE );.  }..
1290b 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
1290c 65 73 74 72 6f 79 28 70 44 6f 6e 65 29 3b 0a 20  estroy(pDone);. 
1290d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1290e 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  OK ){.    pPager
1290f 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73  ->journalOff = s
12910 7a 4a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  zJ;.  }.  return
12911 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68   rc;.}../*.** Ch
12912 61 6e 67 65 20 74 68 65 20 6d 61 78 69 6d 75 6d  ange the maximum
12913 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65   number of in-me
12914 6d 6f 72 79 20 70 61 67 65 73 20 74 68 61 74 20  mory pages that 
12915 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a  are allowed..*/.
12916 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
12917 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
12918 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67  SetCachesize(Pag
12919 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
1291a 6d 78 50 61 67 65 29 7b 0a 20 20 73 71 6c 69 74  mxPage){.  sqlit
1291b 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65  e3PcacheSetCache
1291c 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43  size(pPager->pPC
1291d 61 63 68 65 2c 20 6d 78 50 61 67 65 29 3b 0a 7d  ache, mxPage);.}
1291e 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74  ../*.** Adjust t
1291f 68 65 20 72 6f 62 75 73 74 6e 65 73 73 20 6f 66  he robustness of
12920 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f   the database to
12921 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f   damage due to O
12922 53 20 63 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20  S crashes.** or 
12923 70 6f 77 65 72 20 66 61 69 6c 75 72 65 73 20 62  power failures b
12924 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 6e  y changing the n
12925 75 6d 62 65 72 20 6f 66 20 73 79 6e 63 73 28 29  umber of syncs()
12926 73 20 77 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a  s when writing.*
12927 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  * the rollback j
12928 6f 75 72 6e 61 6c 2e 20 20 54 68 65 72 65 20 61  ournal.  There a
12929 72 65 20 74 68 72 65 65 20 6c 65 76 65 6c 73 3a  re three levels:
1292a 0a 2a 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20  .**.**    OFF   
1292b 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e      sqlite3OsSyn
1292c 63 28 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c  c() is never cal
1292d 6c 65 64 2e 20 20 54 68 69 73 20 69 73 20 74 68  led.  This is th
1292e 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20  e default.**    
1292f 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 74 65            for te
12930 6d 70 6f 72 61 72 79 20 61 6e 64 20 74 72 61 6e  mporary and tran
12931 73 69 65 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a  sient files..**.
12932 2a 2a 20 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20  **    NORMAL    
12933 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73  The journal is s
12934 79 6e 63 65 64 20 6f 6e 63 65 20 62 65 66 6f 72  ynced once befor
12935 65 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f  e writes begin o
12936 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  n the.**        
12937 20 20 20 20 20 20 64 61 74 61 62 61 73 65 2e 20        database. 
12938 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c   This is normall
12939 79 20 61 64 65 71 75 61 74 65 20 70 72 6f 74 65  y adequate prote
1293a 63 74 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20  ction, but.**   
1293b 20 20 20 20 20 20 20 20 20 20 20 69 74 20 69 73             it is
1293c 20 74 68 65 6f 72 65 74 69 63 61 6c 6c 79 20 70   theoretically p
1293d 6f 73 73 69 62 6c 65 2c 20 74 68 6f 75 67 68 20  ossible, though 
1293e 76 65 72 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a  very unlikely,.*
1293f 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74  *              t
12940 68 61 74 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e  hat an inopertun
12941 65 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  e power failure 
12942 63 6f 75 6c 64 20 6c 65 61 76 65 20 74 68 65 20  could leave the 
12943 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20  journal.**      
12944 20 20 20 20 20 20 20 20 69 6e 20 61 20 73 74 61          in a sta
12945 74 65 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63  te which would c
12946 61 75 73 65 20 64 61 6d 61 67 65 20 74 6f 20 74  ause damage to t
12947 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20  he database.**  
12948 20 20 20 20 20 20 20 20 20 20 20 20 77 68 65 6e              when
12949 20 69 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61   it is rolled ba
1294a 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c  ck..**.**    FUL
1294b 4c 20 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e  L      The journ
1294c 61 6c 20 69 73 20 73 79 6e 63 65 64 20 74 77 69  al is synced twi
1294d 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73  ce before writes
1294e 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a   begin on the.**
1294f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61                da
12950 74 61 62 61 73 65 20 28 77 69 74 68 20 73 6f 6d  tabase (with som
12951 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  e additional inf
12952 6f 72 6d 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e  ormation - the n
12953 52 65 63 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20  Rec field.**    
12954 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65            of the
12955 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
12956 2d 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20  - being written 
12957 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 74  in between the t
12958 77 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  wo.**           
12959 20 20 20 73 79 6e 63 73 29 2e 20 20 49 66 20 77     syncs).  If w
1295a 65 20 61 73 73 75 6d 65 20 74 68 61 74 20 77 72  e assume that wr
1295b 69 74 69 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20  iting a.**      
1295c 20 20 20 20 20 20 20 20 73 69 6e 67 6c 65 20 64          single d
1295d 69 73 6b 20 73 65 63 74 6f 72 20 69 73 20 61 74  isk sector is at
1295e 6f 6d 69 63 2c 20 74 68 65 6e 20 74 68 69 73 20  omic, then this 
1295f 6d 6f 64 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a  mode provides.**
12960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73                as
12961 73 75 72 61 6e 63 65 20 74 68 61 74 20 74 68 65  surance that the
12962 20 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f   journal will no
12963 74 20 62 65 20 63 6f 72 72 75 70 74 65 64 20 74  t be corrupted t
12964 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  o the.**        
12965 20 20 20 20 20 20 70 6f 69 6e 74 20 6f 66 20 63        point of c
12966 61 75 73 69 6e 67 20 64 61 6d 61 67 65 20 74 6f  ausing damage to
12967 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64 75   the database du
12968 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a  ring rollback..*
12969 2a 0a 2a 2a 20 4e 75 6d 65 72 69 63 20 76 61 6c  *.** Numeric val
1296a 75 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ues associated w
1296b 69 74 68 20 74 68 65 73 65 20 73 74 61 74 65 73  ith these states
1296c 20 61 72 65 20 4f 46 46 3d 3d 31 2c 20 4e 4f 52   are OFF==1, NOR
1296d 4d 41 4c 3d 32 2c 0a 2a 2a 20 61 6e 64 20 46 55  MAL=2,.** and FU
1296e 4c 4c 3d 33 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  LL=3..*/.#ifndef
1296f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
12970 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54  ER_PRAGMAS.SQLIT
12971 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
12972 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61  qlite3PagerSetSa
12973 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 20  fetyLevel(Pager 
12974 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c 65 76  *pPager, int lev
12975 65 6c 2c 20 69 6e 74 20 62 46 75 6c 6c 46 73 79  el, int bFullFsy
12976 6e 63 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 6e  nc){.  pPager->n
12977 6f 53 79 6e 63 20 3d 20 20 28 6c 65 76 65 6c 3d  oSync =  (level=
12978 3d 31 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65  =1 || pPager->te
12979 6d 70 46 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20  mpFile) ?1:0;.  
1297a 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63  pPager->fullSync
1297b 20 3d 20 28 6c 65 76 65 6c 3d 3d 33 20 26 26 20   = (level==3 && 
1297c 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
1297d 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65  e) ?1:0;.  pPage
1297e 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20  r->sync_flags = 
1297f 28 62 46 75 6c 6c 46 73 79 6e 63 3f 53 51 4c 49  (bFullFsync?SQLI
12980 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c  TE_SYNC_FULL:SQL
12981 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29  ITE_SYNC_NORMAL)
12982 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
12983 6e 6f 53 79 6e 63 20 29 20 70 50 61 67 65 72 2d  noSync ) pPager-
12984 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 7d  >needSync = 0;.}
12985 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
12986 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f  he following glo
12987 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20  bal variable is 
12988 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e  incremented when
12989 65 76 65 72 20 74 68 65 20 6c 69 62 72 61 72 79  ever the library
1298a 0a 2a 2a 20 61 74 74 65 6d 70 74 73 20 74 6f 20  .** attempts to 
1298b 6f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79  open a temporary
1298c 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69 6e 66   file.  This inf
1298d 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64  ormation is used
1298e 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20   for.** testing 
1298f 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c  and analysis onl
12990 79 2e 20 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53  y.  .*/.#ifdef S
12991 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
12992 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
12993 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74  3_opentemp_count
12994 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a   = 0;.#endif../*
12995 0a 2a 2a 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f  .** Open a tempo
12996 72 61 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  rary file..**.**
12997 20 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20   Write the file 
12998 64 65 73 63 72 69 70 74 6f 72 20 69 6e 74 6f 20  descriptor into 
12999 2a 70 46 69 6c 65 2e 20 52 65 74 75 72 6e 20 53  *pFile. Return S
1299a 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
1299b 65 73 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20  ess .** or some 
1299c 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65  other error code
1299d 20 69 66 20 77 65 20 66 61 69 6c 2e 20 54 68 65   if we fail. The
1299e 20 4f 53 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74   OS will automat
1299f 69 63 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 74  ically .** delet
129a0 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  e the temporary 
129a1 66 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73 20  file when it is 
129a2 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  closed..**.** Th
129a3 65 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 74  e flags passed t
129a4 6f 20 74 68 65 20 56 46 53 20 6c 61 79 65 72 20  o the VFS layer 
129a5 78 4f 70 65 6e 28 29 20 63 61 6c 6c 20 61 72 65  xOpen() call are
129a6 20 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64   those specified
129a7 0a 2a 2a 20 62 79 20 70 61 72 61 6d 65 74 65 72  .** by parameter
129a8 20 76 66 73 46 6c 61 67 73 20 4f 52 65 64 20 77   vfsFlags ORed w
129a9 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ith the followin
129aa 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c  g:.**.**     SQL
129ab 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
129ac 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  TE.**     SQLITE
129ad 5f 4f 50 45 4e 5f 43 52 45 41 54 45 0a 2a 2a 20  _OPEN_CREATE.** 
129ae 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
129af 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
129b0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
129b1 45 54 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74  ETEONCLOSE.*/.st
129b2 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 4f 70  atic int pagerOp
129b3 65 6e 74 65 6d 70 28 0a 20 20 50 61 67 65 72 20  entemp(.  Pager 
129b4 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20  *pPager,        
129b5 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 62 6a  /* The pager obj
129b6 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ect */.  sqlite3
129b7 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f  _file *pFile,  /
129b8 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 6c 65  * Write the file
129b9 20 64 65 73 63 72 69 70 74 6f 72 20 68 65 72 65   descriptor here
129ba 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61   */.  int vfsFla
129bb 67 73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  gs          /* F
129bc 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f  lags passed thro
129bd 75 67 68 20 74 6f 20 74 68 65 20 56 46 53 20 2a  ugh to the VFS *
129be 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20  /.){.  int rc;  
129bf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
129c0 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a  Return code */..
129c1 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
129c2 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65  ST.  sqlite3_ope
129c3 6e 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20  ntemp_count++;  
129c4 2f 2a 20 55 73 65 64 20 66 6f 72 20 74 65 73 74  /* Used for test
129c5 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73  ing and analysis
129c6 20 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a   only */.#endif.
129c7 0a 20 20 76 66 73 46 6c 61 67 73 20 7c 3d 20 20  .  vfsFlags |=  
129c8 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
129c9 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f  WRITE | SQLITE_O
129ca 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20  PEN_CREATE |.   
129cb 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
129cc 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c  OPEN_EXCLUSIVE |
129cd 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
129ce 45 54 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63  ETEONCLOSE;.  rc
129cf 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e   = sqlite3OsOpen
129d0 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 30  (pPager->pVfs, 0
129d1 2c 20 70 46 69 6c 65 2c 20 76 66 73 46 6c 61 67  , pFile, vfsFlag
129d2 73 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28  s, 0);.  assert(
129d3 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
129d4 7c 20 69 73 4f 70 65 6e 28 70 46 69 6c 65 29 20  | isOpen(pFile) 
129d5 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
129d6 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
129d7 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 75   busy handler fu
129d8 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  nction..**.** Th
129d9 65 20 70 61 67 65 72 20 69 6e 76 6f 6b 65 73 20  e pager invokes 
129da 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72  the busy-handler
129db 20 69 66 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63   if sqlite3OsLoc
129dc 6b 28 29 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20  k() returns .** 
129dd 53 51 4c 49 54 45 5f 42 55 53 59 20 77 68 65 6e  SQLITE_BUSY when
129de 20 74 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61   trying to upgra
129df 64 65 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20  de from no-lock 
129e0 74 6f 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  to a SHARED lock
129e1 2c 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 72 79  ,.** or when try
129e2 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20 66  ing to upgrade f
129e3 72 6f 6d 20 61 20 52 45 53 45 52 56 45 44 20 6c  rom a RESERVED l
129e4 6f 63 6b 20 74 6f 20 61 6e 20 45 58 43 4c 55 53  ock to an EXCLUS
129e5 49 56 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74  IVE .** lock. It
129e6 20 64 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f   does *not* invo
129e7 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  ke the busy hand
129e8 6c 65 72 20 77 68 65 6e 20 75 70 67 72 61 64 69  ler when upgradi
129e9 6e 67 20 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 45  ng from.** SHARE
129ea 44 20 74 6f 20 52 45 53 45 52 56 45 44 2c 20 6f  D to RESERVED, o
129eb 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67  r when upgrading
129ec 20 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20   from SHARED to 
129ed 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 28 77 68  EXCLUSIVE.** (wh
129ee 69 63 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ich occurs durin
129ef 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f  g hot-journal ro
129f0 6c 6c 62 61 63 6b 29 2e 20 53 75 6d 6d 61 72 79  llback). Summary
129f1 3a 0a 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 69  :.**.**   Transi
129f2 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20  tion            
129f3 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 49 6e              | In
129f4 76 6f 6b 65 73 20 78 42 75 73 79 48 61 6e 64 6c  vokes xBusyHandl
129f5 65 72 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  er.**   --------
129f6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
129f7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
129f8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
129f9 0a 2a 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20  .**   NO_LOCK   
129fa 20 20 20 20 2d 3e 20 53 48 41 52 45 44 5f 4c 4f      -> SHARED_LO
129fb 43 4b 20 20 20 20 20 20 7c 20 59 65 73 0a 2a 2a  CK      | Yes.**
129fc 20 20 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20     SHARED_LOCK  
129fd 20 2d 3e 20 52 45 53 45 52 56 45 44 5f 4c 4f 43   -> RESERVED_LOC
129fe 4b 20 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53  K    | No.**   S
129ff 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20  HARED_LOCK   -> 
12a00 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20  EXCLUSIVE_LOCK  
12a01 20 7c 20 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 52   | No.**   RESER
12a02 56 45 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c  VED_LOCK -> EXCL
12a03 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 59  USIVE_LOCK   | Y
12a04 65 73 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  es.**.** If the 
12a05 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c  busy-handler cal
12a06 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f  lback returns no
12a07 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b  n-zero, the lock
12a08 20 69 73 20 0a 2a 2a 20 72 65 74 72 69 65 64 2e   is .** retried.
12a09 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 7a   If it returns z
12a0a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51  ero, then the SQ
12a0b 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20  LITE_BUSY error 
12a0c 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 74  is.** returned t
12a0d 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 6f 66 20  o the caller of 
12a0e 74 68 65 20 70 61 67 65 72 20 41 50 49 20 66 75  the pager API fu
12a0f 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  nction..*/.SQLIT
12a10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
12a11 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75  qlite3PagerSetBu
12a12 73 79 68 61 6e 64 6c 65 72 28 0a 20 20 50 61 67  syhandler(.  Pag
12a13 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20  er *pPager,     
12a14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12a15 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63    /* Pager objec
12a16 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75  t */.  int (*xBu
12a17 73 79 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 20  syHandler)(void 
12a18 2a 29 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50  *),         /* P
12a19 6f 69 6e 74 65 72 20 74 6f 20 62 75 73 79 2d 68  ointer to busy-h
12a1a 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 20  andler function 
12a1b 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79  */.  void *pBusy
12a1c 48 61 6e 64 6c 65 72 41 72 67 20 20 20 20 20 20  HandlerArg      
12a1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
12a1e 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f  ument to pass to
12a1f 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f   xBusyHandler */
12a20 0a 29 7b 20 20 0a 20 20 70 50 61 67 65 72 2d 3e  .){  .  pPager->
12a21 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 78  xBusyHandler = x
12a22 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70  BusyHandler;.  p
12a23 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64  Pager->pBusyHand
12a24 6c 65 72 41 72 67 20 3d 20 70 42 75 73 79 48 61  lerArg = pBusyHa
12a25 6e 64 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a  ndlerArg;.}../*.
12a26 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 20 63 75  ** Report the cu
12a27 72 72 65 6e 74 20 70 61 67 65 20 73 69 7a 65 20  rrent page size 
12a28 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  and number of re
12a29 73 65 72 76 65 64 20 62 79 74 65 73 20 62 61 63  served bytes bac
12a2a 6b 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f 64 65  k.** to the code
12a2b 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  c..*/.#ifdef SQL
12a2c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 73 74  ITE_HAS_CODEC.st
12a2d 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 52  atic void pagerR
12a2e 65 70 6f 72 74 53 69 7a 65 28 50 61 67 65 72 20  eportSize(Pager 
12a2f 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20  *pPager){.  if( 
12a30 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69  pPager->xCodecSi
12a31 7a 65 43 68 6e 67 20 29 7b 0a 20 20 20 20 70 50  zeChng ){.    pP
12a32 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65  ager->xCodecSize
12a33 43 68 6e 67 28 70 50 61 67 65 72 2d 3e 70 43 6f  Chng(pPager->pCo
12a34 64 65 63 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  dec, pPager->pag
12a35 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20  eSize,.         
12a36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12a37 20 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 6e    (int)pPager->n
12a38 52 65 73 65 72 76 65 29 3b 0a 20 20 7d 0a 7d 0a  Reserve);.  }.}.
12a39 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 70  #else.# define p
12a3a 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 58  agerReportSize(X
12a3b 29 20 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 69  )     /* No-op i
12a3c 66 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 70 70  f we do not supp
12a3d 6f 72 74 20 61 20 63 6f 64 65 63 20 2a 2f 0a 23  ort a codec */.#
12a3e 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  endif../*.** Cha
12a3f 6e 67 65 20 74 68 65 20 70 61 67 65 20 73 69 7a  nge the page siz
12a40 65 20 75 73 65 64 20 62 79 20 74 68 65 20 50 61  e used by the Pa
12a41 67 65 72 20 6f 62 6a 65 63 74 2e 20 54 68 65 20  ger object. The 
12a42 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 0a 2a  new page size .*
12a43 2a 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 2a  * is passed in *
12a44 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a  pPageSize..**.**
12a45 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73   If the pager is
12a46 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74   in the error st
12a47 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 75  ate when this fu
12a48 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
12a49 2c 20 69 74 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d  , it.** is a no-
12a4a 6f 70 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65  op. The value re
12a4b 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 65 72  turned is the er
12a4c 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20  ror state error 
12a4d 63 6f 64 65 20 28 69 2e 65 2e 20 0a 2a 2a 20 6f  code (i.e. .** o
12a4e 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 49 4f 45  ne of SQLITE_IOE
12a4f 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  RR, SQLITE_CORRU
12a50 50 54 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c  PT or SQLITE_FUL
12a51 4c 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  L)..**.** Otherw
12a52 69 73 65 2c 20 69 66 20 61 6c 6c 20 6f 66 20 74  ise, if all of t
12a53 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
12a54 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a   true:.**.**   *
12a55 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 73 69   the new page si
12a56 7a 65 20 28 76 61 6c 75 65 20 6f 66 20 2a 70 50  ze (value of *pP
12a57 61 67 65 53 69 7a 65 29 20 69 73 20 76 61 6c 69  ageSize) is vali
12a58 64 20 28 61 20 70 6f 77 65 72 20 0a 2a 2a 20 20  d (a power .**  
12a59 20 20 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65     of two betwee
12a5a 6e 20 35 31 32 20 61 6e 64 20 53 51 4c 49 54 45  n 512 and SQLITE
12a5b 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 2c 20  _MAX_PAGE_SIZE, 
12a5c 69 6e 63 6c 75 73 69 76 65 29 2c 20 61 6e 64 0a  inclusive), and.
12a5d 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 72 65 20  **.**   * there 
12a5e 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69  are no outstandi
12a5f 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 6e 63  ng page referenc
12a60 65 73 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20  es, and.**.**   
12a61 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  * the database i
12a62 73 20 65 69 74 68 65 72 20 6e 6f 74 20 61 6e 20  s either not an 
12a63 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
12a64 73 65 20 6f 72 20 69 74 20 69 73 0a 2a 2a 20 20  se or it is.**  
12a65 20 20 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20     an in-memory 
12a66 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63 75  database that cu
12a67 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 73 74 73  rrently consists
12a68 20 6f 66 20 7a 65 72 6f 20 70 61 67 65 73 2e 0a   of zero pages..
12a69 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70  **.** then the p
12a6a 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61 67 65  ager object page
12a6b 20 73 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20   size is set to 
12a6c 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a  *pPageSize..**.*
12a6d 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 73 69  * If the page si
12a6e 7a 65 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74  ze is changed, t
12a6f 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
12a70 6e 20 75 73 65 73 20 73 71 6c 69 74 65 33 50 61  n uses sqlite3Pa
12a71 67 65 72 4d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20  gerMalloc() .** 
12a72 74 6f 20 6f 62 74 61 69 6e 20 61 20 6e 65 77 20  to obtain a new 
12a73 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20  Pager.pTmpSpace 
12a74 62 75 66 66 65 72 2e 20 49 66 20 74 68 69 73 20  buffer. If this 
12a75 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d  allocation attem
12a76 70 74 20 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51  pt .** fails, SQ
12a77 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65  LITE_NOMEM is re
12a78 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70  turned and the p
12a79 61 67 65 20 73 69 7a 65 20 72 65 6d 61 69 6e 73  age size remains
12a7a 20 75 6e 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20   unchanged. .** 
12a7b 49 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 61 73  In all other cas
12a7c 65 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  es, SQLITE_OK is
12a7d 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
12a7e 20 49 66 20 74 68 65 20 70 61 67 65 20 73 69 7a   If the page siz
12a7f 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64  e is not changed
12a80 2c 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65  , either because
12a81 20 6f 6e 65 20 6f 66 20 74 68 65 20 65 6e 75 6d   one of the enum
12a82 65 72 61 74 65 64 0a 2a 2a 20 63 6f 6e 64 69 74  erated.** condit
12a83 69 6f 6e 73 20 61 62 6f 76 65 20 69 73 20 6e 6f  ions above is no
12a84 74 20 74 72 75 65 2c 20 74 68 65 20 70 61 67 65  t true, the page
12a85 72 20 77 61 73 20 69 6e 20 65 72 72 6f 72 20 73  r was in error s
12a86 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 2a  tate when this.*
12a87 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63  * function was c
12a88 61 6c 6c 65 64 2c 20 6f 72 20 62 65 63 61 75 73  alled, or becaus
12a89 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  e the memory all
12a8a 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20  ocation attempt 
12a8b 66 61 69 6c 65 64 2c 20 0a 2a 2a 20 74 68 65 6e  failed, .** then
12a8c 20 2a 70 50 61 67 65 53 69 7a 65 20 69 73 20 73   *pPageSize is s
12a8d 65 74 20 74 6f 20 74 68 65 20 6f 6c 64 2c 20 72  et to the old, r
12a8e 65 74 61 69 6e 65 64 20 70 61 67 65 20 73 69 7a  etained page siz
12a8f 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  e before returni
12a90 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
12a91 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
12a92 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a  3PagerSetPagesiz
12a93 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  e(Pager *pPager,
12a94 20 75 31 36 20 2a 70 50 61 67 65 53 69 7a 65 2c   u16 *pPageSize,
12a95 20 69 6e 74 20 6e 52 65 73 65 72 76 65 29 7b 0a   int nReserve){.
12a96 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67 65    int rc = pPage
12a97 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 69  r->errCode;..  i
12a98 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12a99 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67 65   ){.    u16 page
12a9a 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 53 69 7a  Size = *pPageSiz
12a9b 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e;.    assert( p
12a9c 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70  ageSize==0 || (p
12a9d 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20  ageSize>=512 && 
12a9e 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45  pageSize<=SQLITE
12a9f 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 20  _MAX_PAGE_SIZE) 
12aa0 29 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 67  );.    if( (pPag
12aa1 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20  er->memDb==0 || 
12aa2 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d  pPager->dbSize==
12aa3 30 29 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74  0).     && sqlit
12aa4 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74  e3PcacheRefCount
12aa5 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
12aa6 29 3d 3d 30 20 0a 20 20 20 20 20 26 26 20 70 61  )==0 .     && pa
12aa7 67 65 53 69 7a 65 20 26 26 20 70 61 67 65 53 69  geSize && pageSi
12aa8 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70 61 67 65  ze!=pPager->page
12aa9 53 69 7a 65 20 0a 20 20 20 20 29 7b 0a 20 20 20  Size .    ){.   
12aaa 20 20 20 63 68 61 72 20 2a 70 4e 65 77 20 3d 20     char *pNew = 
12aab 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50  (char *)sqlite3P
12aac 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69  ageMalloc(pageSi
12aad 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ze);.      if( !
12aae 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  pNew ){.        
12aaf 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
12ab0 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  M;.      }else{.
12ab1 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65          pager_re
12ab2 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20  set(pPager);.   
12ab3 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67       pPager->pag
12ab4 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65  eSize = pageSize
12ab5 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
12ab6 33 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72  3PageFree(pPager
12ab7 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20  ->pTmpSpace);.  
12ab8 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 54        pPager->pT
12ab9 6d 70 53 70 61 63 65 20 3d 20 70 4e 65 77 3b 0a  mpSpace = pNew;.
12aba 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
12abb 63 61 63 68 65 53 65 74 50 61 67 65 53 69 7a 65  cacheSetPageSize
12abc 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
12abd 2c 20 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  , pageSize);.   
12abe 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2a     }.    }.    *
12abf 70 50 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36  pPageSize = (u16
12ac0 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  )pPager->pageSiz
12ac1 65 3b 0a 20 20 20 20 69 66 28 20 6e 52 65 73 65  e;.    if( nRese
12ac2 72 76 65 3c 30 20 29 20 6e 52 65 73 65 72 76 65  rve<0 ) nReserve
12ac3 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 65   = pPager->nRese
12ac4 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  rve;.    assert(
12ac5 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 26 20   nReserve>=0 && 
12ac6 6e 52 65 73 65 72 76 65 3c 31 30 30 30 20 29 3b  nReserve<1000 );
12ac7 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65  .    pPager->nRe
12ac8 73 65 72 76 65 20 3d 20 28 69 31 36 29 6e 52 65  serve = (i16)nRe
12ac9 73 65 72 76 65 3b 0a 20 20 20 20 70 61 67 65 72  serve;.    pager
12aca 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65  ReportSize(pPage
12acb 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  r);.  }.  return
12acc 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
12acd 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
12ace 6f 20 74 68 65 20 22 74 65 6d 70 6f 72 61 72 79  o the "temporary
12acf 20 70 61 67 65 22 20 62 75 66 66 65 72 20 68 65   page" buffer he
12ad0 6c 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a  ld internally.**
12ad1 20 62 79 20 74 68 65 20 70 61 67 65 72 2e 20 20   by the pager.  
12ad2 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72  This is a buffer
12ad3 20 74 68 61 74 20 69 73 20 62 69 67 20 65 6e 6f   that is big eno
12ad4 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a  ugh to hold the.
12ad5 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e  ** entire conten
12ad6 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  t of a database 
12ad7 70 61 67 65 2e 20 20 54 68 69 73 20 62 75 66 66  page.  This buff
12ad8 65 72 20 69 73 20 75 73 65 64 20 69 6e 74 65 72  er is used inter
12ad9 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20  nally.** during 
12ada 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c  rollback and wil
12adb 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e  l be overwritten
12adc 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c   whenever a roll
12add 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20  back.** occurs. 
12ade 20 42 75 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c   But other modul
12adf 65 73 20 61 72 65 20 66 72 65 65 20 74 6f 20 75  es are free to u
12ae0 73 65 20 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f  se it too, as lo
12ae1 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c  ng as.** no roll
12ae2 62 61 63 6b 73 20 61 72 65 20 68 61 70 70 65 6e  backs are happen
12ae3 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
12ae4 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
12ae5 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61  ite3PagerTempSpa
12ae6 63 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ce(Pager *pPager
12ae7 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
12ae8 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 7d  er->pTmpSpace;.}
12ae9 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ../*.** Attempt 
12aea 74 6f 20 73 65 74 20 74 68 65 20 6d 61 78 69 6d  to set the maxim
12aeb 75 6d 20 64 61 74 61 62 61 73 65 20 70 61 67 65  um database page
12aec 20 63 6f 75 6e 74 20 69 66 20 6d 78 50 61 67 65   count if mxPage
12aed 20 69 73 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a   is positive. .*
12aee 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65  * Make no change
12aef 73 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 7a  s if mxPage is z
12af0 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 2e  ero or negative.
12af1 20 20 41 6e 64 20 6e 65 76 65 72 20 72 65 64 75    And never redu
12af2 63 65 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75  ce the.** maximu
12af3 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 62 65 6c  m page count bel
12af4 6f 77 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  ow the current s
12af5 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
12af6 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72  ase..**.** Regar
12af7 64 6c 65 73 73 20 6f 66 20 6d 78 50 61 67 65 2c  dless of mxPage,
12af8 20 72 65 74 75 72 6e 20 74 68 65 20 63 75 72 72   return the curr
12af9 65 6e 74 20 6d 61 78 69 6d 75 6d 20 70 61 67 65  ent maximum page
12afa 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54   count..*/.SQLIT
12afb 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
12afc 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50 61 67  lite3PagerMaxPag
12afd 65 43 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50  eCount(Pager *pP
12afe 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65  ager, int mxPage
12aff 29 7b 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e  ){.  if( mxPage>
12b00 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  0 ){.    pPager-
12b01 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65  >mxPgno = mxPage
12b02 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 50  ;.  }.  sqlite3P
12b03 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50  agerPagecount(pP
12b04 61 67 65 72 2c 20 30 29 3b 0a 20 20 72 65 74 75  ager, 0);.  retu
12b05 72 6e 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e  rn pPager->mxPgn
12b06 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  o;.}../*.** The 
12b07 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66  following set of
12b08 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73   routines are us
12b09 65 64 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68  ed to disable th
12b0a 65 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 49  e simulated.** I
12b0b 2f 4f 20 65 72 72 6f 72 20 6d 65 63 68 61 6e 69  /O error mechani
12b0c 73 6d 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69  sm.  These routi
12b0d 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  nes are used to 
12b0e 61 76 6f 69 64 20 73 69 6d 75 6c 61 74 65 64 0a  avoid simulated.
12b0f 2a 2a 20 65 72 72 6f 72 73 20 69 6e 20 70 6c 61  ** errors in pla
12b10 63 65 73 20 77 68 65 72 65 20 77 65 20 64 6f 20  ces where we do 
12b11 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 65  not care about e
12b12 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c  rrors..**.** Unl
12b13 65 73 73 20 2d 44 53 51 4c 49 54 45 5f 54 45 53  ess -DSQLITE_TES
12b14 54 3d 31 20 69 73 20 75 73 65 64 2c 20 74 68 65  T=1 is used, the
12b15 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
12b16 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e  all no-ops.** an
12b17 64 20 67 65 6e 65 72 61 74 65 20 6e 6f 20 63 6f  d generate no co
12b18 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  de..*/.#ifdef SQ
12b19 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
12b1a 5f 41 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20  _API extern int 
12b1b 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
12b1c 5f 70 65 6e 64 69 6e 67 3b 0a 53 51 4c 49 54 45  _pending;.SQLITE
12b1d 5f 41 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20  _API extern int 
12b1e 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
12b1f 5f 68 69 74 3b 0a 73 74 61 74 69 63 20 69 6e 74  _hit;.static int
12b20 20 73 61 76 65 64 5f 63 6e 74 3b 0a 76 6f 69 64   saved_cnt;.void
12b21 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74   disable_simulat
12b22 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69  ed_io_errors(voi
12b23 64 29 7b 0a 20 20 73 61 76 65 64 5f 63 6e 74 20  d){.  saved_cnt 
12b24 3d 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  = sqlite3_io_err
12b25 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 73 71  or_pending;.  sq
12b26 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
12b27 65 6e 64 69 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76  ending = -1;.}.v
12b28 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c  oid enable_simul
12b29 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76  ated_io_errors(v
12b2a 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  oid){.  sqlite3_
12b2b 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67  io_error_pending
12b2c 20 3d 20 73 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a   = saved_cnt;.}.
12b2d 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64  #else.# define d
12b2e 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  isable_simulated
12b2f 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64  _io_errors().# d
12b30 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d  efine enable_sim
12b31 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
12b32 28 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  ().#endif../*.**
12b33 20 52 65 61 64 20 74 68 65 20 66 69 72 73 74 20   Read the first 
12b34 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65  N bytes from the
12b35 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
12b36 65 20 66 69 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f  e file into memo
12b37 72 79 0a 2a 2a 20 74 68 61 74 20 70 44 65 73 74  ry.** that pDest
12b38 20 70 6f 69 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a   points to. .**.
12b39 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20  ** If the pager 
12b3a 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20  was opened on a 
12b3b 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 20 28  transient file (
12b3c 7a 46 69 6c 65 6e 61 6d 65 3d 3d 22 22 29 2c 20  zFilename==""), 
12b3d 6f 72 0a 2a 2a 20 6f 70 65 6e 65 64 20 6f 6e 20  or.** opened on 
12b3e 61 20 66 69 6c 65 20 6c 65 73 73 20 74 68 61 6e  a file less than
12b3f 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65   N bytes in size
12b40 2c 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  , the output buf
12b41 66 65 72 20 69 73 0a 2a 2a 20 7a 65 72 6f 65 64  fer is.** zeroed
12b42 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72   and SQLITE_OK r
12b43 65 74 75 72 6e 65 64 2e 20 54 68 65 20 72 61 74  eturned. The rat
12b44 69 6f 6e 61 6c 65 20 66 6f 72 20 74 68 69 73 20  ionale for this 
12b45 69 73 20 74 68 61 74 20 74 68 69 73 20 0a 2a 2a  is that this .**
12b46 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
12b47 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 62 61  d to read databa
12b48 73 65 20 68 65 61 64 65 72 73 2c 20 61 6e 64 20  se headers, and 
12b49 61 20 6e 65 77 20 74 72 61 6e 73 69 65 6e 74 20  a new transient 
12b4a 6f 72 0a 2a 2a 20 7a 65 72 6f 20 73 69 7a 65 64  or.** zero sized
12b4b 20 64 61 74 61 62 61 73 65 20 68 61 73 20 61 20   database has a 
12b4c 68 65 61 64 65 72 20 74 68 61 6e 20 63 6f 6e 73  header than cons
12b4d 69 73 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 66  ists entirely of
12b4e 20 7a 65 72 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49   zeroes..**.** I
12b4f 66 20 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 61  f any IO error a
12b50 70 61 72 74 20 66 72 6f 6d 20 53 51 4c 49 54 45  part from SQLITE
12b51 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
12b52 44 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  D is encountered
12b53 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63  ,.** the error c
12b54 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
12b55 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6e  to the caller an
12b56 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  d the contents o
12b57 66 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20  f the.** output 
12b58 62 75 66 66 65 72 20 75 6e 64 65 66 69 6e 65 64  buffer undefined
12b59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
12b5a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
12b5b 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64  agerReadFilehead
12b5c 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  er(Pager *pPager
12b5d 2c 20 69 6e 74 20 4e 2c 20 75 6e 73 69 67 6e 65  , int N, unsigne
12b5e 64 20 63 68 61 72 20 2a 70 44 65 73 74 29 7b 0a  d char *pDest){.
12b5f 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
12b60 45 5f 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 70  E_OK;.  memset(p
12b61 44 65 73 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 61  Dest, 0, N);.  a
12b62 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
12b63 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61  ager->fd) || pPa
12b64 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
12b65 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50  .  if( isOpen(pP
12b66 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20  ager->fd) ){.   
12b67 20 49 4f 54 52 41 43 45 28 28 22 44 42 48 44 52   IOTRACE(("DBHDR
12b68 20 25 70 20 30 20 25 64 5c 6e 22 2c 20 70 50 61   %p 0 %d\n", pPa
12b69 67 65 72 2c 20 4e 29 29 0a 20 20 20 20 72 63 20  ger, N)).    rc 
12b6a 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
12b6b 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 65 73  pPager->fd, pDes
12b6c 74 2c 20 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66  t, N, 0);.    if
12b6d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45  ( rc==SQLITE_IOE
12b6e 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b  RR_SHORT_READ ){
12b6f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
12b70 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  TE_OK;.    }.  }
12b71 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
12b72 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
12b73 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
12b74 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
12b75 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 73 73  atabase file ass
12b76 6f 63 69 61 74 65 64 20 0a 2a 2a 20 77 69 74 68  ociated .** with
12b77 20 70 50 61 67 65 72 2e 20 4e 6f 72 6d 61 6c 6c   pPager. Normall
12b78 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 63 75  y, this is calcu
12b79 6c 61 74 65 64 20 61 73 20 28 3c 64 62 20 66 69  lated as (<db fi
12b7a 6c 65 20 73 69 7a 65 3e 2f 3c 70 61 67 65 2d 73  le size>/<page-s
12b7b 69 7a 65 3e 29 2e 0a 2a 2a 20 48 6f 77 65 76 65  ize>)..** Howeve
12b7c 72 2c 20 69 66 20 74 68 65 20 66 69 6c 65 20 69  r, if the file i
12b7d 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20  s between 1 and 
12b7e 3c 70 61 67 65 2d 73 69 7a 65 3e 20 62 79 74 65  <page-size> byte
12b7f 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 6e 20  s in size, then 
12b80 0a 2a 2a 20 74 68 69 73 20 69 73 20 63 6f 6e 73  .** this is cons
12b81 69 64 65 72 65 64 20 61 20 31 20 70 61 67 65 20  idered a 1 page 
12b82 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  file..**.** If t
12b83 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65  he pager is in e
12b84 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20  rror state when 
12b85 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
12b86 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 74 68   called, then th
12b87 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 61 74 65  e.** error state
12b88 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
12b89 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 6e 50  eturned and *pnP
12b8a 61 67 65 20 6c 65 66 74 20 75 6e 63 68 61 6e 67  age left unchang
12b8b 65 64 2e 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68  ed. Or,.** if th
12b8c 65 20 66 69 6c 65 20 73 79 73 74 65 6d 20 68 61  e file system ha
12b8d 73 20 74 6f 20 62 65 20 71 75 65 72 69 65 64 20  s to be queried 
12b8e 66 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20  for the size of 
12b8f 74 68 65 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20  the file and.** 
12b90 74 68 65 20 71 75 65 72 79 20 61 74 74 65 6d 70  the query attemp
12b91 74 20 72 65 74 75 72 6e 73 20 61 6e 20 49 4f 20  t returns an IO 
12b92 65 72 72 6f 72 2c 20 74 68 65 20 49 4f 20 65 72  error, the IO er
12b93 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
12b94 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50  rned.** and *pnP
12b95 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e 63 68  age is left unch
12b96 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  anged..**.** Oth
12b97 65 72 77 69 73 65 2c 20 69 66 20 65 76 65 72 79  erwise, if every
12b98 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73  thing is success
12b99 66 75 6c 2c 20 74 68 65 6e 20 53 51 4c 49 54 45  ful, then SQLITE
12b9a 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 0a  _OK is returned.
12b9b 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69  ** and *pnPage i
12b9c 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d  s set to the num
12b9d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
12b9e 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  the database..*/
12b9f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12ba0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
12ba1 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65 72 20  Pagecount(Pager 
12ba2 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 6e  *pPager, int *pn
12ba3 50 61 67 65 29 7b 0a 20 20 50 67 6e 6f 20 6e 50  Page){.  Pgno nP
12ba4 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
12ba5 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72     /* Value to r
12ba6 65 74 75 72 6e 20 76 69 61 20 2a 70 6e 50 61 67  eturn via *pnPag
12ba7 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68  e */..  /* If th
12ba8 65 20 70 61 67 65 72 20 69 73 20 61 6c 72 65 61  e pager is alrea
12ba9 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20  dy in the error 
12baa 73 74 61 74 65 2c 20 72 65 74 75 72 6e 20 74 68  state, return th
12bab 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f  e error code. */
12bac 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65  .  if( pPager->e
12bad 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 65  rrCode ){.    re
12bae 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72  turn pPager->err
12baf 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Code;.  }..  /* 
12bb0 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 75  Determine the nu
12bb1 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
12bb2 20 74 68 65 20 66 69 6c 65 2e 20 53 74 6f 72 65   the file. Store
12bb3 20 74 68 69 73 20 69 6e 20 6e 50 61 67 65 2e 20   this in nPage. 
12bb4 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  */.  if( pPager-
12bb5 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 7b 0a  >dbSizeValid ){.
12bb6 20 20 20 20 6e 50 61 67 65 20 3d 20 70 50 61 67      nPage = pPag
12bb7 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 65  er->dbSize;.  }e
12bb8 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b  lse{.    int rc;
12bb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12bba 20 2f 2a 20 45 72 72 6f 72 20 72 65 74 75 72 6e   /* Error return
12bbb 65 64 20 62 79 20 4f 73 46 69 6c 65 53 69 7a 65  ed by OsFileSize
12bbc 28 29 20 2a 2f 0a 20 20 20 20 69 36 34 20 6e 20  () */.    i64 n 
12bbd 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
12bbe 20 20 2f 2a 20 46 69 6c 65 20 73 69 7a 65 20 69    /* File size i
12bbf 6e 20 62 79 74 65 73 20 72 65 74 75 72 6e 65 64  n bytes returned
12bc0 20 62 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 29   by OsFileSize()
12bc1 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28   */..    assert(
12bc2 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
12bc3 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74  fd) || pPager->t
12bc4 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 69  empFile );.    i
12bc5 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  f( isOpen(pPager
12bc6 2d 3e 66 64 29 20 26 26 20 28 30 20 21 3d 20 28  ->fd) && (0 != (
12bc7 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69  rc = sqlite3OsFi
12bc8 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66  leSize(pPager->f
12bc9 64 2c 20 26 6e 29 29 29 20 29 7b 0a 20 20 20 20  d, &n))) ){.    
12bca 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50    pager_error(pP
12bcb 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20  ager, rc);.     
12bcc 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
12bcd 7d 0a 20 20 20 20 69 66 28 20 6e 3e 30 20 26 26  }.    if( n>0 &&
12bce 20 6e 3c 70 50 61 67 65 72 2d 3e 70 61 67 65 53   n<pPager->pageS
12bcf 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6e 50 61  ize ){.      nPa
12bd0 67 65 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  ge = 1;.    }els
12bd1 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d  e{.      nPage =
12bd2 20 28 50 67 6e 6f 29 28 6e 20 2f 20 70 50 61 67   (Pgno)(n / pPag
12bd3 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
12bd4 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61     }.    if( pPa
12bd5 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45  ger->state!=PAGE
12bd6 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  R_UNLOCK ){.    
12bd7 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
12bd8 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20   = nPage;.      
12bd9 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69  pPager->dbFileSi
12bda 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20  ze = nPage;.    
12bdb 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
12bdc 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 7d  Valid = 1;.    }
12bdd 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
12bde 65 20 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72  e current number
12bdf 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
12be0 20 66 69 6c 65 20 69 73 20 67 72 65 61 74 65 72   file is greater
12be1 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a 2a 20   than the .  ** 
12be2 63 6f 6e 66 69 67 75 72 65 64 20 6d 61 78 69 6d  configured maxim
12be3 75 6d 20 70 61 67 65 72 20 6e 75 6d 62 65 72 2c  um pager number,
12be4 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 61 6c   increase the al
12be5 6c 6f 77 65 64 20 6c 69 6d 69 74 20 73 6f 0a 20  lowed limit so. 
12be6 20 2a 2a 20 74 68 61 74 20 74 68 65 20 66 69 6c   ** that the fil
12be7 65 20 63 61 6e 20 62 65 20 72 65 61 64 2e 0a 20  e can be read.. 
12be8 20 2a 2f 0a 20 20 69 66 28 20 6e 50 61 67 65 3e   */.  if( nPage>
12be9 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29  pPager->mxPgno )
12bea 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78  {.    pPager->mx
12beb 50 67 6e 6f 20 3d 20 28 50 67 6e 6f 29 6e 50 61  Pgno = (Pgno)nPa
12bec 67 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  ge;.  }..  /* Se
12bed 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 72  t the output var
12bee 69 61 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e  iable and return
12bef 20 53 51 4c 49 54 45 5f 4f 4b 20 2a 2f 0a 20 20   SQLITE_OK */.  
12bf0 69 66 28 20 70 6e 50 61 67 65 20 29 7b 0a 20 20  if( pnPage ){.  
12bf1 20 20 2a 70 6e 50 61 67 65 20 3d 20 6e 50 61 67    *pnPage = nPag
12bf2 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
12bf3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
12bf4 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61  *.** Try to obta
12bf5 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70  in a lock of typ
12bf6 65 20 6c 6f 63 6b 74 79 70 65 20 6f 6e 20 74 68  e locktype on th
12bf7 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
12bf8 20 49 66 0a 2a 2a 20 61 20 73 69 6d 69 6c 61 72   If.** a similar
12bf9 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b   or greater lock
12bfa 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64   is already held
12bfb 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
12bfc 69 73 20 61 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 72  is a no-op.** (r
12bfd 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f  eturning SQLITE_
12bfe 4f 4b 20 69 6d 6d 65 64 69 61 74 65 6c 79 29 2e  OK immediately).
12bff 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  .**.** Otherwise
12c00 2c 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 74  , attempt to obt
12c01 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 75 73 69  ain the lock usi
12c02 6e 67 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b  ng sqlite3OsLock
12c03 28 29 2e 20 49 6e 76 6f 6b 65 20 0a 2a 2a 20 74  (). Invoke .** t
12c04 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  he busy callback
12c05 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20   if the lock is 
12c06 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20 61 76  currently not av
12c07 61 69 6c 61 62 6c 65 2e 20 52 65 70 65 61 74 20  ailable. Repeat 
12c08 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 62 75  .** until the bu
12c09 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  sy callback retu
12c0a 72 6e 73 20 66 61 6c 73 65 20 6f 72 20 75 6e 74  rns false or unt
12c0b 69 6c 20 74 68 65 20 61 74 74 65 6d 70 74 20 74  il the attempt t
12c0c 6f 20 0a 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65  o .** obtain the
12c0d 20 6c 6f 63 6b 20 73 75 63 63 65 65 64 73 2e 0a   lock succeeds..
12c0e 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
12c0f 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
12c10 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63  s and an error c
12c11 6f 64 65 20 69 66 20 77 65 20 63 61 6e 6e 6f 74  ode if we cannot
12c12 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65 20 6c   obtain.** the l
12c13 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f 63 6b  ock. If the lock
12c14 20 69 73 20 6f 62 74 61 69 6e 65 64 20 73 75 63   is obtained suc
12c15 63 65 73 73 66 75 6c 6c 79 2c 20 73 65 74 20 74  cessfully, set t
12c16 68 65 20 50 61 67 65 72 2e 73 74 61 74 65 20 0a  he Pager.state .
12c17 2a 2a 20 76 61 72 69 61 62 6c 65 20 74 6f 20 6c  ** variable to l
12c18 6f 63 6b 74 79 70 65 20 62 65 66 6f 72 65 20 72  ocktype before r
12c19 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61  eturning..*/.sta
12c1a 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 61  tic int pager_wa
12c1b 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 72  it_on_lock(Pager
12c1c 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c 6f   *pPager, int lo
12c1d 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72  cktype){.  int r
12c1e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
12c1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12c20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
12c21 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 4f 53 20 6c  /..  /* The OS l
12c22 6f 63 6b 20 76 61 6c 75 65 73 20 6d 75 73 74 20  ock values must 
12c23 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  be the same as t
12c24 68 65 20 50 61 67 65 72 20 6c 6f 63 6b 20 76 61  he Pager lock va
12c25 6c 75 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74  lues */.  assert
12c26 28 20 50 41 47 45 52 5f 53 48 41 52 45 44 3d 3d  ( PAGER_SHARED==
12c27 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
12c28 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 52   assert( PAGER_R
12c29 45 53 45 52 56 45 44 3d 3d 52 45 53 45 52 56 45  ESERVED==RESERVE
12c2a 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  D_LOCK );.  asse
12c2b 72 74 28 20 50 41 47 45 52 5f 45 58 43 4c 55 53  rt( PAGER_EXCLUS
12c2c 49 56 45 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  IVE==EXCLUSIVE_L
12c2d 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  OCK );..  /* If 
12c2e 74 68 65 20 66 69 6c 65 20 69 73 20 63 75 72 72  the file is curr
12c2f 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 20 74  ently unlocked t
12c30 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6d 75 73  hen the size mus
12c31 74 20 62 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a  t be unknown */.
12c32 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12c33 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53  ->state>=PAGER_S
12c34 48 41 52 45 44 20 7c 7c 20 70 50 61 67 65 72 2d  HARED || pPager-
12c35 3e 64 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20  >dbSizeValid==0 
12c36 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  );..  /* Check t
12c37 68 61 74 20 74 68 69 73 20 69 73 20 65 69 74 68  hat this is eith
12c38 65 72 20 61 20 6e 6f 2d 6f 70 20 28 62 65 63 61  er a no-op (beca
12c39 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65  use the requeste
12c3a 64 20 6c 6f 63 6b 20 69 73 20 0a 20 20 2a 2a 20  d lock is .  ** 
12c3b 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 72  already held, or
12c3c 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 72 61 6e   one of the tran
12c3d 73 69 73 74 69 6f 6e 73 20 74 68 61 74 20 74 68  sistions that th
12c3e 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 20  e busy-handler. 
12c3f 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b   ** may be invok
12c40 65 64 20 64 75 72 69 6e 67 2c 20 61 63 63 6f 72  ed during, accor
12c41 64 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 6d 6d  ding to the comm
12c42 65 6e 74 20 61 62 6f 76 65 0a 20 20 2a 2a 20 73  ent above.  ** s
12c43 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75  qlite3PagerSetBu
12c44 73 79 68 61 6e 64 6c 65 72 28 29 2e 0a 20 20 2a  syhandler()..  *
12c45 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 50 61  /.  assert( (pPa
12c46 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b  ger->state>=lock
12c47 74 79 70 65 29 0a 20 20 20 20 20 20 20 7c 7c 20  type).       || 
12c48 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d  (pPager->state==
12c49 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 26 26 20  PAGER_UNLOCK && 
12c4a 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f  locktype==PAGER_
12c4b 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 20 7c  SHARED).       |
12c4c 7c 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65  | (pPager->state
12c4d 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  ==PAGER_RESERVED
12c4e 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41   && locktype==PA
12c4f 47 45 52 5f 45 58 43 4c 55 53 49 56 45 29 0a 20  GER_EXCLUSIVE). 
12c50 20 29 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65   );..  if( pPage
12c51 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79  r->state>=lockty
12c52 70 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  pe ){.    rc = S
12c53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
12c54 65 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  e{.    do {.    
12c55 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
12c56 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c  Lock(pPager->fd,
12c57 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20   locktype);.    
12c58 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49  }while( rc==SQLI
12c59 54 45 5f 42 55 53 59 20 26 26 20 70 50 61 67 65  TE_BUSY && pPage
12c5a 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 28  r->xBusyHandler(
12c5b 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e  pPager->pBusyHan
12c5c 64 6c 65 72 41 72 67 29 20 29 3b 0a 20 20 20 20  dlerArg) );.    
12c5d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12c5e 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  K ){.      pPage
12c5f 72 2d 3e 73 74 61 74 65 20 3d 20 28 75 38 29 6c  r->state = (u8)l
12c60 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 49  ocktype;.      I
12c61 4f 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 25 70  OTRACE(("LOCK %p
12c62 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %d\n", pPager, 
12c63 6c 6f 63 6b 74 79 70 65 29 29 0a 20 20 20 20 7d  locktype)).    }
12c64 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
12c65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74  ;.}../*.** Funct
12c66 69 6f 6e 20 61 73 73 65 72 74 54 72 75 6e 63 61  ion assertTrunca
12c67 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61  teConstraint(pPa
12c68 67 65 72 29 20 63 68 65 63 6b 73 20 74 68 61 74  ger) checks that
12c69 20 6f 6e 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20   one of the .** 
12c6a 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75  following is tru
12c6b 65 20 66 6f 72 20 61 6c 6c 20 64 69 72 74 79 20  e for all dirty 
12c6c 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20  pages currently 
12c6d 69 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 68  in the page-cach
12c6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 54 68  e:.**.**   a) Th
12c6f 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73  e page number is
12c70 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
12c71 75 61 6c 20 74 6f 20 74 68 65 20 73 69 7a 65 20  ual to the size 
12c72 6f 66 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20  of the .**      
12c73 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65  current database
12c74 20 69 6d 61 67 65 2c 20 69 6e 20 70 61 67 65 73   image, in pages
12c75 2c 20 4f 52 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20  , OR.**.**   b) 
12c76 69 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74  if the page cont
12c77 65 6e 74 20 77 65 72 65 20 77 72 69 74 74 65 6e  ent were written
12c78 20 61 74 20 74 68 69 73 20 74 69 6d 65 2c 20 69   at this time, i
12c79 74 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 20  t would not.**  
12c7a 20 20 20 20 62 65 20 6e 65 63 65 73 73 61 72 79      be necessary
12c7b 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75   to write the cu
12c7c 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 75  rrent content ou
12c7d 74 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75  t to the sub-jou
12c7e 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 28 61 73  rnal.**      (as
12c7f 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 66   determined by f
12c80 75 6e 63 74 69 6f 6e 20 73 75 62 6a 52 65 71 75  unction subjRequ
12c81 69 72 65 73 50 61 67 65 28 29 29 2e 0a 2a 2a 0a  iresPage())..**.
12c82 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e 64 69 74  ** If the condit
12c83 69 6f 6e 20 61 73 73 65 72 74 65 64 20 62 79 20  ion asserted by 
12c84 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 65  this function we
12c85 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e 64  re not true, and
12c86 20 74 68 65 0a 2a 2a 20 64 69 72 74 79 20 70 61   the.** dirty pa
12c87 67 65 20 77 65 72 65 20 74 6f 20 62 65 20 64 69  ge were to be di
12c88 73 63 61 72 64 65 64 20 66 72 6f 6d 20 74 68 65  scarded from the
12c89 20 63 61 63 68 65 20 76 69 61 20 74 68 65 20 70   cache via the p
12c8a 61 67 65 72 53 74 72 65 73 73 28 29 0a 2a 2a 20  agerStress().** 
12c8b 72 6f 75 74 69 6e 65 2c 20 70 61 67 65 72 53 74  routine, pagerSt
12c8c 72 65 73 73 28 29 20 77 6f 75 6c 64 20 6e 6f 74  ress() would not
12c8d 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65   write the curre
12c8e 6e 74 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20  nt page content 
12c8f 74 6f 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  to.** the databa
12c90 73 65 20 66 69 6c 65 2e 20 49 66 20 61 20 73 61  se file. If a sa
12c91 76 65 70 6f 69 6e 74 20 74 72 61 6e 73 61 63 74  vepoint transact
12c92 69 6f 6e 20 77 65 72 65 20 72 6f 6c 6c 65 64 20  ion were rolled 
12c93 62 61 63 6b 20 61 66 74 65 72 0a 2a 2a 20 74 68  back after.** th
12c94 69 73 20 68 61 70 70 65 6e 65 64 2c 20 74 68 65  is happened, the
12c95 20 63 6f 72 72 65 63 74 20 62 65 68 61 76 69 6f   correct behavio
12c96 75 72 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 72  ur would be to r
12c97 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 72 65  estore the curre
12c98 6e 74 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66  nt.** content of
12c99 20 74 68 65 20 70 61 67 65 2e 20 48 6f 77 65 76   the page. Howev
12c9a 65 72 2c 20 73 69 6e 63 65 20 74 68 69 73 20 63  er, since this c
12c9b 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 70 72  ontent is not pr
12c9c 65 73 65 6e 74 20 69 6e 20 65 69 74 68 65 72 0a  esent in either.
12c9d 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
12c9e 66 69 6c 65 20 6f 72 20 74 68 65 20 70 6f 72 74  file or the port
12c9f 69 6f 6e 20 6f 66 20 74 68 65 20 72 6f 6c 6c 62  ion of the rollb
12ca0 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  ack journal and 
12ca1 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  .** sub-journal 
12ca2 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20  rolled back the 
12ca3 63 6f 6e 74 65 6e 74 20 63 6f 75 6c 64 20 6e 6f  content could no
12ca4 74 20 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e  t be restored an
12ca5 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  d the.** databas
12ca6 65 20 69 6d 61 67 65 20 77 6f 75 6c 64 20 62 65  e image would be
12ca7 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 20 49 74  come corrupt. It
12ca8 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 66 6f   is therefore fo
12ca9 72 74 75 6e 61 74 65 20 74 68 61 74 20 0a 2a 2a  rtunate that .**
12caa 20 74 68 69 73 20 63 69 72 63 75 6d 73 74 61 6e   this circumstan
12cab 63 65 20 63 61 6e 6e 6f 74 20 61 72 69 73 65 2e  ce cannot arise.
12cac 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
12cad 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 74  SQLITE_DEBUG).st
12cae 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74  atic void assert
12caf 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69  TruncateConstrai
12cb0 6e 74 43 62 28 50 67 48 64 72 20 2a 70 50 67 29  ntCb(PgHdr *pPg)
12cb1 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d  {.  assert( pPg-
12cb2 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
12cb3 54 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  TY );.  assert( 
12cb4 21 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67  !subjRequiresPag
12cb5 65 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70  e(pPg) || pPg->p
12cb6 67 6e 6f 3c 3d 70 50 67 2d 3e 70 50 61 67 65 72  gno<=pPg->pPager
12cb7 2d 3e 64 62 53 69 7a 65 20 29 3b 0a 7d 0a 73 74  ->dbSize );.}.st
12cb8 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74  atic void assert
12cb9 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69  TruncateConstrai
12cba 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nt(Pager *pPager
12cbb 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  ){.  sqlite3Pcac
12cbc 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 70  heIterateDirty(p
12cbd 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20  Pager->pPCache, 
12cbe 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f  assertTruncateCo
12cbf 6e 73 74 72 61 69 6e 74 43 62 29 3b 0a 7d 0a 23  nstraintCb);.}.#
12cc0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 61 73  else.# define as
12cc1 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73  sertTruncateCons
12cc2 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 0a 23  traint(pPager).#
12cc3 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  endif../*.** Tru
12cc4 6e 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d  ncate the in-mem
12cc5 6f 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c  ory database fil
12cc6 65 20 69 6d 61 67 65 20 74 6f 20 6e 50 61 67 65  e image to nPage
12cc7 20 70 61 67 65 73 2e 20 54 68 69 73 20 0a 2a 2a   pages. This .**
12cc8 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e   function does n
12cc9 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69  ot actually modi
12cca 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20  fy the database 
12ccb 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 49 74  file on disk. It
12ccc 20 0a 2a 2a 20 6a 75 73 74 20 73 65 74 73 20 74   .** just sets t
12ccd 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74  he internal stat
12cce 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 6f  e of the pager o
12ccf 62 6a 65 63 74 20 73 6f 20 74 68 61 74 20 74 68  bject so that th
12cd0 65 20 0a 2a 2a 20 74 72 75 6e 63 61 74 69 6f 6e  e .** truncation
12cd1 20 77 69 6c 6c 20 62 65 20 64 6f 6e 65 20 77 68   will be done wh
12cd2 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  en the current t
12cd3 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
12cd4 6d 6d 69 74 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  mmitted..*/.SQLI
12cd5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
12cd6 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e  sqlite3PagerTrun
12cd7 63 61 74 65 49 6d 61 67 65 28 50 61 67 65 72 20  cateImage(Pager 
12cd8 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50  *pPager, Pgno nP
12cd9 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  age){.  assert( 
12cda 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61  pPager->dbSizeVa
12cdb 6c 69 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28  lid );.  assert(
12cdc 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e   pPager->dbSize>
12cdd 3d 6e 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65  =nPage );.  asse
12cde 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
12cdf 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45  e>=PAGER_RESERVE
12ce0 44 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64  D );.  pPager->d
12ce1 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20  bSize = nPage;. 
12ce2 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43   assertTruncateC
12ce3 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65 72  onstraint(pPager
12ce4 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74  );.}../*.** Shut
12ce5 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61  down the page ca
12ce6 63 68 65 2e 20 20 46 72 65 65 20 61 6c 6c 20 6d  che.  Free all m
12ce7 65 6d 6f 72 79 20 61 6e 64 20 63 6c 6f 73 65 20  emory and close 
12ce8 61 6c 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a  all files..**.**
12ce9 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   If a transactio
12cea 6e 20 77 61 73 20 69 6e 20 70 72 6f 67 72 65 73  n was in progres
12ceb 73 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  s when this rout
12cec 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  ine is called, t
12ced 68 61 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  hat.** transacti
12cee 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63  on is rolled bac
12cef 6b 2e 20 20 41 6c 6c 20 6f 75 74 73 74 61 6e 64  k.  All outstand
12cf0 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 69 6e  ing pages are in
12cf1 76 61 6c 69 64 61 74 65 64 0a 2a 2a 20 61 6e 64  validated.** and
12cf2 20 74 68 65 69 72 20 6d 65 6d 6f 72 79 20 69 73   their memory is
12cf3 20 66 72 65 65 64 2e 20 20 41 6e 79 20 61 74 74   freed.  Any att
12cf4 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 70 61  empt to use a pa
12cf5 67 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  ge associated.**
12cf6 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65 20   with this page 
12cf7 63 61 63 68 65 20 61 66 74 65 72 20 74 68 69 73  cache after this
12cf8 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
12cf9 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a 2a  s will likely.**
12cfa 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72   result in a cor
12cfb 65 64 75 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  edump..**.** Thi
12cfc 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79  s function alway
12cfd 73 20 73 75 63 63 65 65 64 73 2e 20 49 66 20 61  s succeeds. If a
12cfe 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
12cff 61 63 74 69 76 65 20 61 6e 20 61 74 74 65 6d 70  active an attemp
12d00 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20  t.** is made to 
12d01 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 49 66  roll it back. If
12d02 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
12d03 20 64 75 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c   during the roll
12d04 62 61 63 6b 20 0a 2a 2a 20 61 20 68 6f 74 20 6a  back .** a hot j
12d05 6f 75 72 6e 61 6c 20 6d 61 79 20 62 65 20 6c 65  ournal may be le
12d06 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 73 79  ft in the filesy
12d07 73 74 65 6d 20 62 75 74 20 6e 6f 20 65 72 72 6f  stem but no erro
12d08 72 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a  r is returned.**
12d09 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   to the caller..
12d0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12d0b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
12d0c 65 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70  erClose(Pager *p
12d0d 50 61 67 65 72 29 7b 0a 20 20 64 69 73 61 62 6c  Pager){.  disabl
12d0e 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
12d0f 72 72 6f 72 73 28 29 3b 0a 20 20 73 71 6c 69 74  rrors();.  sqlit
12d10 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
12d11 6c 6f 63 28 29 3b 0a 20 20 70 50 61 67 65 72 2d  loc();.  pPager-
12d12 3e 65 72 72 43 6f 64 65 20 3d 20 30 3b 0a 20 20  >errCode = 0;.  
12d13 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
12d14 65 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 70 61 67  eMode = 0;.  pag
12d15 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29  er_reset(pPager)
12d16 3b 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b  ;.  if( MEMDB ){
12d17 0a 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63  .    pager_unloc
12d18 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c  k(pPager);.  }el
12d19 73 65 7b 0a 20 20 20 20 2f 2a 20 53 65 74 20 50  se{.    /* Set P
12d1a 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 48 64 72 20  ager.journalHdr 
12d1b 74 6f 20 2d 31 20 66 6f 72 20 74 68 65 20 62 65  to -1 for the be
12d1c 6e 65 66 69 74 20 6f 66 20 74 68 65 20 70 61 67  nefit of the pag
12d1d 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 0a 20  er_playback() . 
12d1e 20 20 20 2a 2a 20 63 61 6c 6c 20 77 68 69 63 68     ** call which
12d1f 20 6d 61 79 20 62 65 20 6d 61 64 65 20 66 72 6f   may be made fro
12d20 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 55 6e  m within pagerUn
12d21 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28  lockAndRollback(
12d22 29 2e 20 49 66 20 69 74 0a 20 20 20 20 2a 2a 20  ). If it.    ** 
12d23 69 73 20 6e 6f 74 20 2d 31 2c 20 74 68 65 6e 20  is not -1, then 
12d24 74 68 65 20 75 6e 73 79 6e 63 65 64 20 70 6f 72  the unsynced por
12d25 74 69 6f 6e 20 6f 66 20 61 6e 20 6f 70 65 6e 20  tion of an open 
12d26 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79  journal file may
12d27 0a 20 20 20 20 2a 2a 20 62 65 20 70 6c 61 79 65  .    ** be playe
12d28 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  d back into the 
12d29 64 61 74 61 62 61 73 65 2e 20 49 66 20 61 20 70  database. If a p
12d2a 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63  ower failure occ
12d2b 75 72 73 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a  urs while.    **
12d2c 20 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69   this is happeni
12d2d 6e 67 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  ng, the database
12d2e 20 6d 61 79 20 62 65 63 6f 6d 65 20 63 6f 72 72   may become corr
12d2f 75 70 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  upt..    */.    
12d30 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48  pPager->journalH
12d31 64 72 20 3d 20 2d 31 3b 0a 20 20 20 20 70 61 67  dr = -1;.    pag
12d32 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62  erUnlockAndRollb
12d33 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  ack(pPager);.  }
12d34 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e  .  sqlite3EndBen
12d35 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65  ignMalloc();.  e
12d36 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
12d37 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 50  io_errors();.  P
12d38 41 47 45 52 54 52 41 43 45 28 28 22 43 4c 4f 53  AGERTRACE(("CLOS
12d39 45 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  E %d\n", PAGERID
12d3a 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 49 4f  (pPager)));.  IO
12d3b 54 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70  TRACE(("CLOSE %p
12d3c 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20  \n", pPager)).  
12d3d 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
12d3e 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71  Pager->fd);.  sq
12d3f 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 50  lite3PageFree(pP
12d40 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29  ager->pTmpSpace)
12d41 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  ;.  sqlite3Pcach
12d42 65 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 70  eClose(pPager->p
12d43 50 43 61 63 68 65 29 3b 0a 0a 23 69 66 64 65 66  PCache);..#ifdef
12d44 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
12d45 43 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  C.  if( pPager->
12d46 78 43 6f 64 65 63 46 72 65 65 20 29 20 70 50 61  xCodecFree ) pPa
12d47 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 28  ger->xCodecFree(
12d48 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b  pPager->pCodec);
12d49 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72  .#endif..  asser
12d4a 74 28 20 21 70 50 61 67 65 72 2d 3e 61 53 61 76  t( !pPager->aSav
12d4b 65 70 6f 69 6e 74 20 26 26 20 21 70 50 61 67 65  epoint && !pPage
12d4c 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b  r->pInJournal );
12d4d 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70  .  assert( !isOp
12d4e 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
12d4f 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  && !isOpen(pPage
12d50 72 2d 3e 73 6a 66 64 29 20 29 3b 0a 0a 20 20 73  r->sjfd) );..  s
12d51 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67  qlite3_free(pPag
12d52 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  er);.  return SQ
12d53 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20  LITE_OK;.}..#if 
12d54 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
12d55 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
12d56 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52  TE_TEST)./*.** R
12d57 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 6e  eturn the page n
12d58 75 6d 62 65 72 20 66 6f 72 20 70 61 67 65 20 70  umber for page p
12d59 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  Pg..*/.SQLITE_PR
12d5a 49 56 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74  IVATE Pgno sqlit
12d5b 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65  e3PagerPagenumbe
12d5c 72 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a  r(DbPage *pPg){.
12d5d 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 67    return pPg->pg
12d5e 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  no;.}.#endif../*
12d5f 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68  .** Increment th
12d60 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
12d61 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a  t for page pPg..
12d62 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12d63 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
12d64 67 65 72 52 65 66 28 44 62 50 61 67 65 20 2a 70  gerRef(DbPage *p
12d65 50 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63  Pg){.  sqlite3Pc
12d66 61 63 68 65 52 65 66 28 70 50 67 29 3b 0a 7d 0a  acheRef(pPg);.}.
12d67 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20  ./*.** Sync the 
12d68 6a 6f 75 72 6e 61 6c 2e 20 49 6e 20 6f 74 68 65  journal. In othe
12d69 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75  r words, make su
12d6a 72 65 20 61 6c 6c 20 74 68 65 20 70 61 67 65 73  re all the pages
12d6b 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 62 65   that have.** be
12d6c 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  en written to th
12d6d 65 20 6a 6f 75 72 6e 61 6c 20 68 61 76 65 20 61  e journal have a
12d6e 63 74 75 61 6c 6c 79 20 72 65 61 63 68 65 64 20  ctually reached 
12d6f 74 68 65 20 73 75 72 66 61 63 65 20 6f 66 20 74  the surface of t
12d70 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 63  he.** disk and c
12d71 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 69  an be restored i
12d72 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61  n the event of a
12d73 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
12d74 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  lback..**.** If 
12d75 74 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79  the Pager.needSy
12d76 6e 63 20 66 6c 61 67 20 69 73 20 6e 6f 74 20 73  nc flag is not s
12d77 65 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  et, then this fu
12d78 6e 63 74 69 6f 6e 20 69 73 20 61 0a 2a 2a 20 6e  nction is a.** n
12d79 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c  o-op. Otherwise,
12d7a 20 74 68 65 20 61 63 74 69 6f 6e 73 20 72 65 71   the actions req
12d7b 75 69 72 65 64 20 64 65 70 65 6e 64 20 6f 6e 20  uired depend on 
12d7c 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65  the journal-mode
12d7d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 65 76 69  .** and the devi
12d7e 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69  ce characteristi
12d7f 63 73 20 6f 66 20 74 68 65 20 74 68 65 20 66 69  cs of the the fi
12d80 6c 65 2d 73 79 73 74 65 6d 2c 20 61 73 20 66 6f  le-system, as fo
12d81 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  llows:.**.**   *
12d82 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
12d83 66 69 6c 65 20 69 73 20 61 6e 20 69 6e 2d 6d 65  file is an in-me
12d84 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  mory journal fil
12d85 65 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 6e 65 65  e, no action nee
12d86 64 0a 2a 2a 20 20 20 20 20 62 65 20 74 61 6b 65  d.**     be take
12d87 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 4f 74 68  n..**.**   * Oth
12d88 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 64  erwise, if the d
12d89 65 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73  evice does not s
12d8a 75 70 70 6f 72 74 20 74 68 65 20 53 41 46 45 5f  upport the SAFE_
12d8b 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2c  APPEND property,
12d8c 0a 2a 2a 20 20 20 20 20 74 68 65 6e 20 74 68 65  .**     then the
12d8d 20 6e 52 65 63 20 66 69 65 6c 64 20 6f 66 20 74   nRec field of t
12d8e 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
12d8f 20 77 72 69 74 74 65 6e 20 6a 6f 75 72 6e 61 6c   written journal
12d90 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 69   header.**     i
12d91 73 20 75 70 64 61 74 65 64 20 74 6f 20 63 6f 6e  s updated to con
12d92 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  tain the number 
12d93 6f 66 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72  of journal recor
12d94 64 73 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20  ds that have.** 
12d95 20 20 20 20 62 65 65 6e 20 77 72 69 74 74 65 6e      been written
12d96 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 74 2e 20 49   following it. I
12d97 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6f  f the pager is o
12d98 70 65 72 61 74 69 6e 67 20 69 6e 20 66 75 6c 6c  perating in full
12d99 2d 73 79 6e 63 0a 2a 2a 20 20 20 20 20 6d 6f 64  -sync.**     mod
12d9a 65 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72  e, then the jour
12d9b 6e 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63  nal file is sync
12d9c 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66  ed before this f
12d9d 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2e  ield is updated.
12d9e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68  .**.**   * If th
12d9f 65 20 64 65 76 69 63 65 20 64 6f 65 73 20 6e 6f  e device does no
12da0 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 53 45  t support the SE
12da1 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74  QUENTIAL propert
12da2 79 2c 20 74 68 65 6e 20 0a 2a 2a 20 20 20 20 20  y, then .**     
12da3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
12da4 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72  synced..**.** Or
12da5 2c 20 69 6e 20 70 73 65 75 64 6f 2d 63 6f 64 65  , in pseudo-code
12da6 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 4e 4f  :.**.**   if( NO
12da7 54 20 3c 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  T <in-memory jou
12da8 72 6e 61 6c 3e 20 29 7b 0a 2a 2a 20 20 20 20 20  rnal> ){.**     
12da9 69 66 28 20 4e 4f 54 20 53 41 46 45 5f 41 50 50  if( NOT SAFE_APP
12daa 45 4e 44 20 29 7b 0a 2a 2a 20 20 20 20 20 20 20  END ){.**       
12dab 69 66 28 20 3c 66 75 6c 6c 2d 73 79 6e 63 20 6d  if( <full-sync m
12dac 6f 64 65 3e 20 29 20 78 53 79 6e 63 28 3c 6a 6f  ode> ) xSync(<jo
12dad 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a  urnal file>);.**
12dae 20 20 20 20 20 20 20 3c 75 70 64 61 74 65 20 6e         <update n
12daf 52 65 63 20 66 69 65 6c 64 3e 0a 2a 2a 20 20 20  Rec field>.**   
12db0 20 20 7d 20 0a 2a 2a 20 20 20 20 20 69 66 28 20    } .**     if( 
12db1 4e 4f 54 20 53 45 51 55 45 4e 54 49 41 4c 20 29  NOT SEQUENTIAL )
12db2 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20   xSync(<journal 
12db3 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 7d 0a 2a  file>);.**   }.*
12db4 2a 0a 2a 2a 20 54 68 65 20 50 61 67 65 72 2e 6e  *.** The Pager.n
12db5 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20  eedSync flag is 
12db6 6e 65 76 65 72 20 62 65 20 73 65 74 20 66 6f 72  never be set for
12db7 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
12db8 2c 20 6f 72 20 61 6e 79 0a 2a 2a 20 66 69 6c 65  , or any.** file
12db9 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 6e 6f   operating in no
12dba 2d 73 79 6e 63 20 6d 6f 64 65 20 28 50 61 67 65  -sync mode (Page
12dbb 72 2e 6e 6f 53 79 6e 63 20 73 65 74 20 74 6f 20  r.noSync set to 
12dbc 6e 6f 6e 2d 7a 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a  non-zero)..**.**
12dbd 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
12dbe 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6c 65  this routine cle
12dbf 61 72 73 20 74 68 65 20 50 47 48 44 52 5f 4e 45  ars the PGHDR_NE
12dc0 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 6f 66 20  ED_SYNC flag of 
12dc1 65 76 65 72 79 20 0a 2a 2a 20 70 61 67 65 20 63  every .** page c
12dc2 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 69 6e  urrently held in
12dc3 20 6d 65 6d 6f 72 79 20 62 65 66 6f 72 65 20 72   memory before r
12dc4 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f  eturning SQLITE_
12dc5 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 0a 2a 2a 20  OK. If an IO.** 
12dc6 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
12dc7 65 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 49  ered, then the I
12dc8 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  O error code is 
12dc9 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
12dca 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  caller..*/.stati
12dcb 63 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61  c int syncJourna
12dcc 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  l(Pager *pPager)
12dcd 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  {.  if( pPager->
12dce 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20  needSync ){.    
12dcf 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d  assert( !pPager-
12dd0 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20  >tempFile );.   
12dd1 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75   if( pPager->jou
12dd2 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f  rnalMode!=PAGER_
12dd3 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
12dd4 52 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  RY ){.      int 
12dd5 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
12dd6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12dd7 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
12dd8 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69  */.      const i
12dd9 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33  nt iDc = sqlite3
12dda 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65  OsDeviceCharacte
12ddb 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e  ristics(pPager->
12ddc 66 64 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  fd);.      asser
12ddd 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
12dde 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 20 20 20  ->jfd) );..     
12ddf 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c   if( 0==(iDc&SQL
12de0 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
12de1 50 50 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 20  PPEND) ){.      
12de2 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20    /* This block 
12de3 64 65 61 6c 73 20 77 69 74 68 20 61 6e 20 6f 62  deals with an ob
12de4 73 63 75 72 65 20 70 72 6f 62 6c 65 6d 2e 20 49  scure problem. I
12de5 66 20 74 68 65 20 6c 61 73 74 20 63 6f 6e 6e 65  f the last conne
12de6 63 74 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a  ction.        **
12de7 20 74 68 61 74 20 77 72 6f 74 65 20 74 6f 20 74   that wrote to t
12de8 68 69 73 20 64 61 74 61 62 61 73 65 20 77 61 73  his database was
12de9 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 70 65   operating in pe
12dea 72 73 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c  rsistent-journal
12deb 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 65  .        ** mode
12dec 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , then the journ
12ded 61 6c 20 66 69 6c 65 20 6d 61 79 20 61 74 20 74  al file may at t
12dee 68 69 73 20 70 6f 69 6e 74 20 61 63 74 75 61 6c  his point actual
12def 6c 79 20 62 65 20 6c 61 72 67 65 72 0a 20 20 20  ly be larger.   
12df0 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67       ** than Pag
12df1 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 62 79  er.journalOff by
12df2 74 65 73 2e 20 49 66 20 74 68 65 20 6e 65 78 74  tes. If the next
12df3 20 74 68 69 6e 67 20 69 6e 20 74 68 65 20 6a 6f   thing in the jo
12df4 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a  urnal.        **
12df5 20 66 69 6c 65 20 68 61 70 70 65 6e 73 20 74 6f   file happens to
12df6 20 62 65 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65   be a journal-he
12df7 61 64 65 72 20 28 77 72 69 74 74 65 6e 20 61 73  ader (written as
12df8 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20 20 20   part of the.   
12df9 20 20 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73       ** previous
12dfa 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 61   connections tra
12dfb 6e 73 61 63 74 69 6f 6e 29 2c 20 61 6e 64 20 61  nsaction), and a
12dfc 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 2d   crash or power-
12dfd 66 61 69 6c 75 72 65 20 0a 20 20 20 20 20 20 20  failure .       
12dfe 20 2a 2a 20 6f 63 63 75 72 73 20 61 66 74 65 72   ** occurs after
12dff 20 6e 52 65 63 20 69 73 20 75 70 64 61 74 65 64   nRec is updated
12e00 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 69 73   but before this
12e01 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74   connection writ
12e02 65 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  es .        ** a
12e03 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 74 6f 20  nything else to 
12e04 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12e05 20 28 6f 72 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c   (or commits/rol
12e06 6c 73 20 62 61 63 6b 20 69 74 73 20 0a 20 20 20  ls back its .   
12e07 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74       ** transact
12e08 69 6f 6e 29 2c 20 74 68 65 6e 20 53 51 4c 69 74  ion), then SQLit
12e09 65 20 6d 61 79 20 62 65 63 6f 6d 65 20 63 6f 6e  e may become con
12e0a 66 75 73 65 64 20 77 68 65 6e 20 64 6f 69 6e 67  fused when doing
12e0b 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a   the .        **
12e0c 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
12e0d 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20  lback following 
12e0e 72 65 63 6f 76 65 72 79 2e 20 49 74 20 6d 61 79  recovery. It may
12e0f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 6c 6c 0a 20   roll back all. 
12e10 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 69         ** of thi
12e11 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 61  s connections da
12e12 74 61 2c 20 74 68 65 6e 20 70 72 6f 63 65 65 64  ta, then proceed
12e13 20 74 6f 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b   to rolling back
12e14 20 74 68 65 20 6f 6c 64 2c 0a 20 20 20 20 20 20   the old,.      
12e15 20 20 2a 2a 20 6f 75 74 2d 6f 66 2d 64 61 74 65    ** out-of-date
12e16 20 64 61 74 61 20 74 68 61 74 20 66 6f 6c 6c 6f   data that follo
12e17 77 73 20 69 74 2e 20 44 61 74 61 62 61 73 65 20  ws it. Database 
12e18 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20  corruption..    
12e19 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
12e1a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64  * To work around
12e1b 20 74 68 69 73 2c 20 69 66 20 74 68 65 20 6a 6f   this, if the jo
12e1c 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20  urnal file does 
12e1d 61 70 70 65 61 72 20 74 6f 20 63 6f 6e 74 61 69  appear to contai
12e1e 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 76  n.        ** a v
12e1f 61 6c 69 64 20 68 65 61 64 65 72 20 66 6f 6c 6c  alid header foll
12e20 6f 77 69 6e 67 20 50 61 67 65 72 2e 6a 6f 75 72  owing Pager.jour
12e21 6e 61 6c 4f 66 66 2c 20 74 68 65 6e 20 77 72 69  nalOff, then wri
12e22 74 65 20 61 20 30 78 30 30 0a 20 20 20 20 20 20  te a 0x00.      
12e23 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 74 68 65    ** byte to the
12e24 20 73 74 61 72 74 20 6f 66 20 69 74 20 74 6f 20   start of it to 
12e25 70 72 65 76 65 6e 74 20 69 74 20 66 72 6f 6d 20  prevent it from 
12e26 62 65 69 6e 67 20 72 65 63 6f 67 6e 69 7a 65 64  being recognized
12e27 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
12e28 20 20 20 20 20 2a 2a 20 56 61 72 69 61 62 6c 65       ** Variable
12e29 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20   iNextHdrOffset 
12e2a 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f 66  is set to the of
12e2b 66 73 65 74 20 61 74 20 77 68 69 63 68 20 74 68  fset at which th
12e2c 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72  is.        ** pr
12e2d 6f 62 6c 65 6d 61 74 69 63 20 68 65 61 64 65 72  oblematic header
12e2e 20 77 69 6c 6c 20 6f 63 63 75 72 2c 20 69 66 20   will occur, if 
12e2f 69 74 20 65 78 69 73 74 73 2e 20 61 4d 61 67 69  it exists. aMagi
12e30 63 20 69 73 20 75 73 65 64 20 0a 20 20 20 20 20  c is used .     
12e31 20 20 20 2a 2a 20 61 73 20 61 20 74 65 6d 70 6f     ** as a tempo
12e32 72 61 72 79 20 62 75 66 66 65 72 20 74 6f 20 69  rary buffer to i
12e33 6e 73 70 65 63 74 20 74 68 65 20 66 69 72 73 74  nspect the first
12e34 20 63 6f 75 70 6c 65 20 6f 66 20 62 79 74 65 73   couple of bytes
12e35 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 74   of.        ** t
12e36 68 65 20 70 6f 74 65 6e 74 69 61 6c 20 6a 6f 75  he potential jou
12e37 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 20  rnal header..   
12e38 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
12e39 69 36 34 20 69 4e 65 78 74 48 64 72 4f 66 66 73  i64 iNextHdrOffs
12e3a 65 74 3b 0a 20 20 20 20 20 20 20 20 75 38 20 61  et;.        u8 a
12e3b 4d 61 67 69 63 5b 38 5d 3b 0a 09 75 38 20 7a 48  Magic[8];..u8 zH
12e3c 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f  eader[sizeof(aJo
12e3d 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 3b 0a  urnalMagic)+4];.
12e3e 0a 09 6d 65 6d 63 70 79 28 7a 48 65 61 64 65 72  ..memcpy(zHeader
12e3f 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c  , aJournalMagic,
12e40 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c   sizeof(aJournal
12e41 4d 61 67 69 63 29 29 3b 0a 09 70 75 74 33 32 62  Magic));..put32b
12e42 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a  its(&zHeader[siz
12e43 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  eof(aJournalMagi
12e44 63 29 5d 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65  c)], pPager->nRe
12e45 63 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 4e 65  c);..        iNe
12e46 78 74 48 64 72 4f 66 66 73 65 74 20 3d 20 6a 6f  xtHdrOffset = jo
12e47 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70  urnalHdrOffset(p
12e48 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20  Pager);.        
12e49 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
12e4a 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ad(pPager->jfd, 
12e4b 61 4d 61 67 69 63 2c 20 38 2c 20 69 4e 65 78 74  aMagic, 8, iNext
12e4c 48 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  HdrOffset);.    
12e4d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
12e4e 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 6d 65 6d 63  TE_OK && 0==memc
12e4f 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72  mp(aMagic, aJour
12e50 6e 61 6c 4d 61 67 69 63 2c 20 38 29 20 29 7b 0a  nalMagic, 8) ){.
12e51 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63            static
12e52 20 63 6f 6e 73 74 20 75 38 20 7a 65 72 6f 62 79   const u8 zeroby
12e53 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  te = 0;.        
12e54 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
12e55 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  Write(pPager->jf
12e56 64 2c 20 26 7a 65 72 6f 62 79 74 65 2c 20 31 2c  d, &zerobyte, 1,
12e57 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29   iNextHdrOffset)
12e58 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
12e59 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
12e5a 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c  TE_OK && rc!=SQL
12e5b 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
12e5c 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 20  READ ){.        
12e5d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
12e5e 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
12e5f 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65  /* Write the nRe
12e60 63 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65  c value into the
12e61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65   journal file he
12e62 61 64 65 72 2e 20 49 66 20 69 6e 0a 20 20 20 20  ader. If in.    
12e63 20 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 79 6e 63      ** full-sync
12e64 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 73 79  hronous mode, sy
12e65 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  nc the journal f
12e66 69 72 73 74 2e 20 54 68 69 73 20 65 6e 73 75 72  irst. This ensur
12e67 65 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 20  es that.        
12e68 2a 2a 20 61 6c 6c 20 64 61 74 61 20 68 61 73 20  ** all data has 
12e69 72 65 61 6c 6c 79 20 68 69 74 20 74 68 65 20 64  really hit the d
12e6a 69 73 6b 20 62 65 66 6f 72 65 20 6e 52 65 63 20  isk before nRec 
12e6b 69 73 20 75 70 64 61 74 65 64 20 74 6f 20 6d 61  is updated to ma
12e6c 72 6b 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74  rk.        ** it
12e6d 20 61 73 20 61 20 63 61 6e 64 69 64 61 74 65 20   as a candidate 
12e6e 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20  for rollback..  
12e6f 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
12e70 20 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20   ** This is not 
12e71 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65 20  required if the 
12e72 70 65 72 73 69 73 74 65 6e 74 20 6d 65 64 69 61  persistent media
12e73 20 73 75 70 70 6f 72 74 73 20 74 68 65 0a 20 20   supports the.  
12e74 20 20 20 20 20 20 2a 2a 20 53 41 46 45 5f 41 50        ** SAFE_AP
12e75 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2e 20 42  PEND property. B
12e76 65 63 61 75 73 65 20 69 6e 20 74 68 69 73 20 63  ecause in this c
12e77 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20 70 6f  ase it is not po
12e78 73 73 69 62 6c 65 20 0a 20 20 20 20 20 20 20 20  ssible .        
12e79 2a 2a 20 66 6f 72 20 67 61 72 62 61 67 65 20 64  ** for garbage d
12e7a 61 74 61 20 74 6f 20 62 65 20 61 70 70 65 6e 64  ata to be append
12e7b 65 64 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 20  ed to the file, 
12e7c 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 0a 20  the nRec field. 
12e7d 20 20 20 20 20 20 20 2a 2a 20 69 73 20 70 6f 70         ** is pop
12e7e 75 6c 61 74 65 64 20 77 69 74 68 20 30 78 46 46  ulated with 0xFF
12e7f 46 46 46 46 46 46 20 77 68 65 6e 20 74 68 65 20  FFFFFF when the 
12e80 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69  journal header i
12e81 73 20 77 72 69 74 74 65 6e 0a 20 20 20 20 20 20  s written.      
12e82 20 20 2a 2a 20 61 6e 64 20 6e 65 76 65 72 20 6e    ** and never n
12e83 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 74  eeds to be updat
12e84 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ed..        */. 
12e85 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
12e86 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 30  r->fullSync && 0
12e87 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f  ==(iDc&SQLITE_IO
12e88 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20  CAP_SEQUENTIAL) 
12e89 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 41 47  ){.          PAG
12e8a 45 52 54 52 41 43 45 28 28 22 53 59 4e 43 20 6a  ERTRACE(("SYNC j
12e8b 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c  ournal of %d\n",
12e8c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
12e8d 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 49 4f  ));.          IO
12e8e 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70  TRACE(("JSYNC %p
12e8f 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20  \n", pPager)).  
12e90 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
12e91 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65  ite3OsSync(pPage
12e92 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e  r->jfd, pPager->
12e93 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20  sync_flags);.   
12e94 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
12e95 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
12e96 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
12e97 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28          IOTRACE(
12e98 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 5c 6e  ("JHDR %p %lld\n
12e99 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65  ", pPager, pPage
12e9a 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 29 3b  r->journalHdr));
12e9b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
12e9c 6c 69 74 65 33 4f 73 57 72 69 74 65 28 0a 20 20  lite3OsWrite(.  
12e9d 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
12e9e 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20  ->jfd, zHeader, 
12e9f 73 69 7a 65 6f 66 28 7a 48 65 61 64 65 72 29 2c  sizeof(zHeader),
12ea0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12ea1 48 64 72 0a 09 29 3b 0a 20 20 20 20 20 20 20 20  Hdr..);.        
12ea2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12ea3 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
12ea4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
12ea5 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f   0==(iDc&SQLITE_
12ea6 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c  IOCAP_SEQUENTIAL
12ea7 29 20 29 7b 0a 20 20 20 20 20 20 20 20 50 41 47  ) ){.        PAG
12ea8 45 52 54 52 41 43 45 28 28 22 53 59 4e 43 20 6a  ERTRACE(("SYNC j
12ea9 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c  ournal of %d\n",
12eaa 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
12eab 29 29 3b 0a 20 20 20 20 20 20 20 20 49 4f 54 52  ));.        IOTR
12eac 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e  ACE(("JSYNC %p\n
12ead 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 20  ", pPager)).    
12eae 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12eaf 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a  OsSync(pPager->j
12eb0 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63  fd, pPager->sync
12eb1 5f 66 6c 61 67 73 7c 20 0a 20 20 20 20 20 20 20  _flags| .       
12eb2 20 20 20 28 70 50 61 67 65 72 2d 3e 73 79 6e 63     (pPager->sync
12eb3 5f 66 6c 61 67 73 3d 3d 53 51 4c 49 54 45 5f 53  _flags==SQLITE_S
12eb4 59 4e 43 5f 46 55 4c 4c 3f 53 51 4c 49 54 45 5f  YNC_FULL?SQLITE_
12eb5 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 3a 30 29  SYNC_DATAONLY:0)
12eb6 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
12eb7 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
12eb8 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
12eb9 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
12eba 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75  ..    /* The jou
12ebb 72 6e 61 6c 20 66 69 6c 65 20 77 61 73 20 6a 75  rnal file was ju
12ebc 73 74 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  st successfully 
12ebd 73 79 6e 63 65 64 2e 20 53 65 74 20 50 61 67 65  synced. Set Page
12ebe 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 20  r.needSync .    
12ebf 2a 2a 20 74 6f 20 7a 65 72 6f 20 61 6e 64 20 63  ** to zero and c
12ec0 6c 65 61 72 20 74 68 65 20 50 47 48 44 52 5f 4e  lear the PGHDR_N
12ec1 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 6f 6e  EED_SYNC flag on
12ec2 20 61 6c 6c 20 70 61 67 65 73 73 2e 0a 20 20 20   all pagess..   
12ec3 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   */.    pPager->
12ec4 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20  needSync = 0;.  
12ec5 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
12ec6 6c 53 74 61 72 74 65 64 20 3d 20 31 3b 0a 20 20  lStarted = 1;.  
12ec7 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43    sqlite3PcacheC
12ec8 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 70 50  learSyncFlags(pP
12ec9 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
12eca 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
12ecb 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
12ecc 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
12ecd 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61  s the first in a
12ece 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
12ecf 64 69 72 74 79 20 70 61 67 65 73 20 63 6f 6e 6e  dirty pages conn
12ed0 65 63 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  ected.** by the 
12ed1 50 67 48 64 72 2e 70 44 69 72 74 79 20 70 6f 69  PgHdr.pDirty poi
12ed2 6e 74 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74  nter. This funct
12ed3 69 6f 6e 20 77 72 69 74 65 73 20 65 61 63 68 20  ion writes each 
12ed4 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6e  one of the.** in
12ed5 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 69 6e  -memory pages in
12ed6 20 74 68 65 20 6c 69 73 74 20 74 6f 20 74 68 65   the list to the
12ed7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
12ed8 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6d 61 79  The argument may
12ed9 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2c 20 72 65 70  .** be NULL, rep
12eda 72 65 73 65 6e 74 69 6e 67 20 61 6e 20 65 6d 70  resenting an emp
12edb 74 79 20 6c 69 73 74 2e 20 49 6e 20 74 68 69 73  ty list. In this
12edc 20 63 61 73 65 20 74 68 69 73 20 66 75 6e 63 74   case this funct
12edd 69 6f 6e 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f  ion is.** a no-o
12ede 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67  p..**.** The pag
12edf 65 72 20 6d 75 73 74 20 68 6f 6c 64 20 61 74 20  er must hold at 
12ee0 6c 65 61 73 74 20 61 20 52 45 53 45 52 56 45 44  least a RESERVED
12ee1 20 6c 6f 63 6b 20 77 68 65 6e 20 74 68 69 73 20   lock when this 
12ee2 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63  function.** is c
12ee3 61 6c 6c 65 64 2e 20 42 65 66 6f 72 65 20 77 72  alled. Before wr
12ee4 69 74 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 74  iting anything t
12ee5 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
12ee6 69 6c 65 2c 20 74 68 69 73 20 6c 6f 63 6b 0a 2a  ile, this lock.*
12ee7 2a 20 69 73 20 75 70 67 72 61 64 65 64 20 74 6f  * is upgraded to
12ee8 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
12ee9 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f 63 6b 20  ck. If the lock 
12eea 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e  cannot be obtain
12eeb 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  ed,.** SQLITE_BU
12eec 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  SY is returned a
12eed 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72  nd no data is wr
12eee 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74  itten to the dat
12eef 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a  abase file..** .
12ef0 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20  ** If the pager 
12ef1 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 70  is a temp-file p
12ef2 61 67 65 72 20 61 6e 64 20 74 68 65 20 61 63 74  ager and the act
12ef3 75 61 6c 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  ual file-system 
12ef4 66 69 6c 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 79  file.** is not y
12ef5 65 74 20 6f 70 65 6e 2c 20 69 74 20 69 73 20 63  et open, it is c
12ef6 72 65 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65  reated and opene
12ef7 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64 61 74  d before any dat
12ef8 61 20 69 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e  a is .** written
12ef9 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65   out..**.** Once
12efa 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 62 65   the lock has be
12efb 65 6e 20 75 70 67 72 61 64 65 64 20 61 6e 64 2c  en upgraded and,
12efc 20 69 66 20 6e 65 63 65 73 73 61 72 79 2c 20 74   if necessary, t
12efd 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 0a  he file opened,.
12efe 2a 2a 20 74 68 65 20 70 61 67 65 73 20 61 72 65  ** the pages are
12eff 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20   written out to 
12f00 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12f01 65 20 69 6e 20 6c 69 73 74 20 6f 72 64 65 72 2e  e in list order.
12f02 20 57 72 69 74 69 6e 67 0a 2a 2a 20 61 20 70 61   Writing.** a pa
12f03 67 65 20 69 73 20 73 6b 69 70 70 65 64 20 69 66  ge is skipped if
12f04 20 69 74 20 6d 65 65 74 73 20 65 69 74 68 65 72   it meets either
12f05 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
12f06 67 20 63 72 69 74 65 72 69 61 3a 0a 2a 2a 0a 2a  g criteria:.**.*
12f07 2a 20 20 20 2a 20 54 68 65 20 70 61 67 65 20 6e  *   * The page n
12f08 75 6d 62 65 72 20 69 73 20 67 72 65 61 74 65 72  umber is greater
12f09 20 74 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69   than Pager.dbSi
12f0a 7a 65 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 54 68  ze, or.**   * Th
12f0b 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49  e PGHDR_DONT_WRI
12f0c 54 45 20 66 6c 61 67 20 69 73 20 73 65 74 20 6f  TE flag is set o
12f0d 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a  n the page..**.*
12f0e 2a 20 49 66 20 77 72 69 74 69 6e 67 20 6f 75 74  * If writing out
12f0f 20 61 20 70 61 67 65 20 63 61 75 73 65 73 20 74   a page causes t
12f10 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12f11 20 74 6f 20 67 72 6f 77 2c 20 50 61 67 65 72 2e   to grow, Pager.
12f12 64 62 46 69 6c 65 53 69 7a 65 0a 2a 2a 20 69 73  dbFileSize.** is
12f13 20 75 70 64 61 74 65 64 20 61 63 63 6f 72 64 69   updated accordi
12f14 6e 67 6c 79 2e 20 49 66 20 70 61 67 65 20 31 20  ngly. If page 1 
12f15 69 73 20 77 72 69 74 74 65 6e 20 6f 75 74 2c 20  is written out, 
12f16 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 63  then the value c
12f17 61 63 68 65 64 0a 2a 2a 20 69 6e 20 50 61 67 65  ached.** in Page
12f18 72 2e 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 69  r.dbFileVers[] i
12f19 73 20 75 70 64 61 74 65 64 20 74 6f 20 6d 61 74  s updated to mat
12f1a 63 68 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65  ch the new value
12f1b 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 74 68   stored in.** th
12f1c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
12f1d 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76 65 72 79 74  .**.** If everyt
12f1e 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66  hing is successf
12f1f 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  ul, SQLITE_OK is
12f20 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e   returned. If an
12f21 20 49 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63   IO error .** oc
12f22 63 75 72 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f  curs, an IO erro
12f23 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
12f24 65 64 2e 20 4f 72 2c 20 69 66 20 74 68 65 20 45  ed. Or, if the E
12f25 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 63 61  XCLUSIVE lock ca
12f26 6e 6e 6f 74 0a 2a 2a 20 62 65 20 6f 62 74 61 69  nnot.** be obtai
12f27 6e 65 64 2c 20 53 51 4c 49 54 45 5f 42 55 53 59  ned, SQLITE_BUSY
12f28 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
12f29 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
12f2a 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74  r_write_pagelist
12f2b 28 50 67 48 64 72 20 2a 70 4c 69 73 74 29 7b 0a  (PgHdr *pList){.
12f2c 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
12f2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f2e 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20         /* Pager 
12f2f 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20  object */.  int 
12f30 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
12f31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f32 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
12f33 2a 2f 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28  */..  if( NEVER(
12f34 70 4c 69 73 74 3d 3d 30 29 20 29 20 72 65 74 75  pList==0) ) retu
12f35 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
12f36 70 50 61 67 65 72 20 3d 20 70 4c 69 73 74 2d 3e  pPager = pList->
12f37 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a 20 41 74  pPager;..  /* At
12f38 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72   this point ther
12f39 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20  e may be either 
12f3a 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58  a RESERVED or EX
12f3b 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
12f3c 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
12f3d 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65  e file. If there
12f3e 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e 20 45   is already an E
12f3f 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 74  XCLUSIVE lock, t
12f40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a  he following.  *
12f41 2a 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f  * call is a no-o
12f42 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 76  p..  **.  ** Mov
12f43 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 66 72 6f  ing the lock fro
12f44 6d 20 52 45 53 45 52 56 45 44 20 74 6f 20 45 58  m RESERVED to EX
12f45 43 4c 55 53 49 56 45 20 61 63 74 75 61 6c 6c 79  CLUSIVE actually
12f46 20 69 6e 76 6f 6c 76 65 73 20 67 6f 69 6e 67 0a   involves going.
12f47 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 61 6e 20    ** through an 
12f48 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
12f49 74 65 20 50 45 4e 44 49 4e 47 2e 20 20 20 41 20  te PENDING.   A 
12f4a 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 70 72 65  PENDING lock pre
12f4b 76 65 6e 74 73 20 6e 65 77 0a 20 20 2a 2a 20 72  vents new.  ** r
12f4c 65 61 64 65 72 73 20 66 72 6f 6d 20 61 74 74 61  eaders from atta
12f4d 63 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74  ching to the dat
12f4e 61 62 61 73 65 20 62 75 74 20 69 73 20 75 6e 73  abase but is uns
12f4f 75 66 66 69 63 69 65 6e 74 20 66 6f 72 20 75 73  ufficient for us
12f50 20 74 6f 0a 20 20 2a 2a 20 77 72 69 74 65 2e 20   to.  ** write. 
12f51 20 54 68 65 20 69 64 65 61 20 6f 66 20 61 20 50   The idea of a P
12f52 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 74  ENDING lock is t
12f53 6f 20 70 72 65 76 65 6e 74 20 6e 65 77 20 72 65  o prevent new re
12f54 61 64 65 72 73 20 66 72 6f 6d 0a 20 20 2a 2a 20  aders from.  ** 
12f55 63 6f 6d 69 6e 67 20 69 6e 20 77 68 69 6c 65 20  coming in while 
12f56 77 65 20 77 61 69 74 20 66 6f 72 20 65 78 69 73  we wait for exis
12f57 74 69 6e 67 20 72 65 61 64 65 72 73 20 74 6f 20  ting readers to 
12f58 63 6c 65 61 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  clear..  **.  **
12f59 20 57 68 69 6c 65 20 74 68 65 20 70 61 67 65 72   While the pager
12f5a 20 69 73 20 69 6e 20 74 68 65 20 52 45 53 45 52   is in the RESER
12f5b 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 20 6f  VED state, the o
12f5c 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65  riginal database
12f5d 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20 75 6e   file.  ** is un
12f5e 63 68 61 6e 67 65 64 20 61 6e 64 20 77 65 20 63  changed and we c
12f5f 61 6e 20 72 6f 6c 6c 62 61 63 6b 20 77 69 74 68  an rollback with
12f60 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 70 6c  out having to pl
12f61 61 79 62 61 63 6b 20 74 68 65 0a 20 20 2a 2a 20  ayback the.  ** 
12f62 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f 20 74 68 65  journal into the
12f63 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61   original databa
12f64 73 65 20 66 69 6c 65 2e 20 20 4f 6e 63 65 20 77  se file.  Once w
12f65 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74 6f 0a  e transition to.
12f66 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 2c 20    ** EXCLUSIVE, 
12f67 69 74 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74  it means the dat
12f68 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62  abase file has b
12f69 65 65 6e 20 63 68 61 6e 67 65 64 20 61 6e 64 20  een changed and 
12f6a 61 6e 79 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a  any rollback.  *
12f6b 2a 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20 61  * will require a
12f6c 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79 62 61 63   journal playbac
12f6d 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  k..  */.  assert
12f6e 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
12f6f 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  =PAGER_RESERVED 
12f70 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f  );.  rc = pager_
12f71 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61  wait_on_lock(pPa
12f72 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c  ger, EXCLUSIVE_L
12f73 4f 43 4b 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  OCK);..  /* If t
12f74 68 65 20 66 69 6c 65 20 69 73 20 61 20 74 65 6d  he file is a tem
12f75 70 2d 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 79  p-file has not y
12f76 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20  et been opened, 
12f77 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20 49 74 0a  open it now. It.
12f78 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73    ** is not poss
12f79 69 62 6c 65 20 66 6f 72 20 72 63 20 74 6f 20 62  ible for rc to b
12f7a 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c  e other than SQL
12f7b 49 54 45 5f 4f 4b 20 69 66 20 74 68 69 73 20 62  ITE_OK if this b
12f7c 72 61 6e 63 68 0a 20 20 2a 2a 20 69 73 20 74 61  ranch.  ** is ta
12f7d 6b 65 6e 2c 20 61 73 20 70 61 67 65 72 5f 77 61  ken, as pager_wa
12f7e 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 29 20 69 73 20  it_on_lock() is 
12f7f 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 74 65 6d 70  a no-op for temp
12f80 2d 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  -files..  */.  i
12f81 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  f( !isOpen(pPage
12f82 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 61 73  r->fd) ){.    as
12f83 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65  sert( pPager->te
12f84 6d 70 46 69 6c 65 20 26 26 20 72 63 3d 3d 53 51  mpFile && rc==SQ
12f85 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72  LITE_OK );.    r
12f86 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 74 65 6d  c = pagerOpentem
12f87 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  p(pPager, pPager
12f88 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 76 66  ->fd, pPager->vf
12f89 73 46 6c 61 67 73 29 3b 0a 20 20 7d 0a 0a 20 20  sFlags);.  }..  
12f8a 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
12f8b 45 5f 4f 4b 20 26 26 20 70 4c 69 73 74 20 29 7b  E_OK && pList ){
12f8c 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d  .    Pgno pgno =
12f8d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 3b 0a 0a 20   pList->pgno;.. 
12f8e 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61     /* If there a
12f8f 72 65 20 64 69 72 74 79 20 70 61 67 65 73 20 69  re dirty pages i
12f90 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
12f91 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65   with page numbe
12f92 72 73 20 67 72 65 61 74 65 72 0a 20 20 20 20 2a  rs greater.    *
12f93 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 64 62 53  * than Pager.dbS
12f94 69 7a 65 2c 20 74 68 69 73 20 6d 65 61 6e 73 20  ize, this means 
12f95 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e  sqlite3PagerTrun
12f96 63 61 74 65 49 6d 61 67 65 28 29 20 77 61 73 20  cateImage() was 
12f97 63 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20 2a 2a  called to.    **
12f98 20 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20 73   make the file s
12f99 6d 61 6c 6c 65 72 20 28 70 72 65 73 75 6d 61 62  maller (presumab
12f9a 6c 79 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75  ly by auto-vacuu
12f9b 6d 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20  m code). Do not 
12f9c 77 72 69 74 65 0a 20 20 20 20 2a 2a 20 61 6e 79  write.    ** any
12f9d 20 73 75 63 68 20 70 61 67 65 73 20 74 6f 20 74   such pages to t
12f9e 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a  he file..    **.
12f9f 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 64 6f 20      ** Also, do 
12fa0 6e 6f 74 20 77 72 69 74 65 20 6f 75 74 20 61 6e  not write out an
12fa1 79 20 70 61 67 65 20 74 68 61 74 20 68 61 73 20  y page that has 
12fa2 74 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57  the PGHDR_DONT_W
12fa3 52 49 54 45 20 66 6c 61 67 0a 20 20 20 20 2a 2a  RITE flag.    **
12fa4 20 73 65 74 20 28 73 65 74 20 62 79 20 73 71 6c   set (set by sql
12fa5 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69  ite3PagerDontWri
12fa6 74 65 28 29 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  te())..    */.  
12fa7 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67    if( pgno<=pPag
12fa8 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d  er->dbSize && 0=
12fa9 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50  =(pList->flags&P
12faa 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29  GHDR_DONT_WRITE)
12fab 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66   ){.      i64 of
12fac 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a  fset = (pgno-1)*
12fad 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67  (i64)pPager->pag
12fae 65 53 69 7a 65 3b 20 20 20 2f 2a 20 4f 66 66 73  eSize;   /* Offs
12faf 65 74 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20  et to write */. 
12fb0 20 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61       char *pData
12fb1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
12fb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12fb3 20 20 20 20 2f 2a 20 44 61 74 61 20 74 6f 20 77      /* Data to w
12fb4 72 69 74 65 20 2a 2f 20 20 20 20 0a 0a 20 20 20  rite */    ..   
12fb5 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65     /* Encode the
12fb6 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20   database */.   
12fb7 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 72     CODEC2(pPager
12fb8 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20  , pList->pData, 
12fb9 70 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20  pgno, 6, return 
12fba 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44  SQLITE_NOMEM, pD
12fbb 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ata);..      /* 
12fbc 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 61  Write out the pa
12fbd 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20  ge data. */.    
12fbe 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
12fbf 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64  Write(pPager->fd
12fc0 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d  , pData, pPager-
12fc1 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65  >pageSize, offse
12fc2 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66  t);..      /* If
12fc3 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 73 74   page 1 was just
12fc4 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65   written, update
12fc5 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72   Pager.dbFileVer
12fc6 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20 20  s to match.     
12fc7 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e 6f   ** the value no
12fc8 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  w stored in the 
12fc9 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
12fca 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 0a  f writing this .
12fcb 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63 61        ** page ca
12fcc 75 73 65 64 20 74 68 65 20 64 61 74 61 62 61 73  used the databas
12fcd 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20  e file to grow, 
12fce 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 69 7a  update dbFileSiz
12fcf 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  e. .      */.   
12fd0 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29     if( pgno==1 )
12fd1 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
12fd2 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  (&pPager->dbFile
12fd3 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d  Vers, &pData[24]
12fd4 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d  , sizeof(pPager-
12fd5 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20  >dbFileVers));. 
12fd6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
12fd7 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62   pgno>pPager->db
12fd8 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  FileSize ){.    
12fd9 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69      pPager->dbFi
12fda 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20  leSize = pgno;. 
12fdb 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
12fdc 20 55 70 64 61 74 65 20 61 6e 79 20 62 61 63 6b   Update any back
12fdd 75 70 20 6f 62 6a 65 63 74 73 20 63 6f 70 79 69  up objects copyi
12fde 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ng the contents 
12fdf 6f 66 20 74 68 69 73 20 70 61 67 65 72 2e 20 2a  of this pager. *
12fe0 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  /.      sqlite3B
12fe1 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67  ackupUpdate(pPag
12fe2 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e  er->pBackup, pgn
12fe3 6f 2c 20 28 75 38 2a 29 70 4c 69 73 74 2d 3e 70  o, (u8*)pList->p
12fe4 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 50 41  Data);..      PA
12fe5 47 45 52 54 52 41 43 45 28 28 22 53 54 4f 52 45  GERTRACE(("STORE
12fe6 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68   %d page %d hash
12fe7 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20  (%08x)\n",.     
12fe8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41                PA
12fe9 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
12fea 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68  gno, pager_pageh
12feb 61 73 68 28 70 4c 69 73 74 29 29 29 3b 0a 20 20  ash(pList)));.  
12fec 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 50 47      IOTRACE(("PG
12fed 4f 55 54 20 25 70 20 25 64 5c 6e 22 2c 20 70 50  OUT %p %d\n", pP
12fee 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20  ager, pgno));.  
12fef 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73      PAGER_INCR(s
12ff0 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69  qlite3_pager_wri
12ff1 74 65 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 20  tedb_count);.   
12ff2 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50     PAGER_INCR(pP
12ff3 61 67 65 72 2d 3e 6e 57 72 69 74 65 29 3b 0a 20  ager->nWrite);. 
12ff4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
12ff5 50 41 47 45 52 54 52 41 43 45 28 28 22 4e 4f 53  PAGERTRACE(("NOS
12ff6 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 5c  TORE %d page %d\
12ff7 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
12ff8 65 72 29 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20  er), pgno));.   
12ff9 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   }.#ifdef SQLITE
12ffa 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20  _CHECK_PAGES.   
12ffb 20 70 4c 69 73 74 2d 3e 70 61 67 65 48 61 73 68   pList->pageHash
12ffc 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73   = pager_pagehas
12ffd 68 28 70 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66  h(pList);.#endif
12ffe 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 4c 69  .    pList = pLi
12fff 73 74 2d 3e 70 44 69 72 74 79 3b 0a 20 20 7d 0a  st->pDirty;.  }.
13000 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
13001 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20  ./*.** Append a 
13002 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 63 75  record of the cu
13003 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 70  rrent state of p
13004 61 67 65 20 70 50 67 20 74 6f 20 74 68 65 20 73  age pPg to the s
13005 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 0a 2a 2a 20  ub-journal. .** 
13006 49 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72  It is the caller
13007 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  s responsibility
13008 20 74 6f 20 75 73 65 20 73 75 62 6a 52 65 71 75   to use subjRequ
13009 69 72 65 73 50 61 67 65 28 29 20 74 6f 20 63 68  iresPage() to ch
1300a 65 63 6b 20 0a 2a 2a 20 74 68 61 74 20 69 74 20  eck .** that it 
1300b 69 73 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72  is really requir
1300c 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e  ed before callin
1300d 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  g this function.
1300e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
1300f 73 66 75 6c 2c 20 73 65 74 20 74 68 65 20 62 69  sful, set the bi
13010 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  t corresponding 
13011 74 6f 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 20  to pPg->pgno in 
13012 74 68 65 20 62 69 74 76 65 63 73 0a 2a 2a 20 66  the bitvecs.** f
13013 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65  or all open save
13014 70 6f 69 6e 74 73 20 62 65 66 6f 72 65 20 72 65  points before re
13015 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  turning..**.** T
13016 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
13017 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 69  urns SQLITE_OK i
13018 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20  f everything is 
13019 73 75 63 63 65 73 73 66 75 6c 2c 20 61 6e 20 49  successful, an I
1301a 4f 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20  O.** error code 
1301b 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74  if the attempt t
1301c 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 73  o write to the s
1301d 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 61 69 6c 73  ub-journal fails
1301e 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  , or .** SQLITE_
1301f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f  NOMEM if a mallo
13020 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 73 65  c fails while se
13021 74 74 69 6e 67 20 61 20 62 69 74 20 69 6e 20 61  tting a bit in a
13022 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 62 69   savepoint.** bi
13023 74 76 65 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tvec..*/.static 
13024 69 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61  int subjournalPa
13025 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  ge(PgHdr *pPg){.
13026 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
13027 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70  E_OK;.  Pager *p
13028 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
13029 67 65 72 3b 0a 20 20 69 66 28 20 69 73 4f 70 65  ger;.  if( isOpe
1302a 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20  n(pPager->sjfd) 
1302b 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 61  ){.    void *pDa
1302c 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b  ta = pPg->pData;
1302d 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20  .    i64 offset 
1302e 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65  = pPager->nSubRe
1302f 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67  c*(4+pPager->pag
13030 65 53 69 7a 65 29 3b 0a 20 20 20 20 63 68 61 72  eSize);.    char
13031 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 43   *pData2;..    C
13032 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 44  ODEC2(pPager, pD
13033 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  ata, pPg->pgno, 
13034 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  7, return SQLITE
13035 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b  _NOMEM, pData2);
13036 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28  .    PAGERTRACE(
13037 28 22 53 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25  ("STMT-JOURNAL %
13038 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41  d page %d\n", PA
13039 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
1303a 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 0a 20  Pg->pgno));.  . 
1303b 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 49     assert( pageI
1303c 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c  nJournal(pPg) ||
1303d 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65   pPg->pgno>pPage
1303e 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 3b  r->dbOrigSize );
1303f 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33  .    rc = write3
13040 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 73 6a  2bits(pPager->sj
13041 66 64 2c 20 6f 66 66 73 65 74 2c 20 70 50 67 2d  fd, offset, pPg-
13042 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20  >pgno);.    if( 
13043 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
13044 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
13045 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
13046 72 2d 3e 73 6a 66 64 2c 20 70 44 61 74 61 32 2c  r->sjfd, pData2,
13047 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
13048 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20  e, offset+4);.  
13049 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63    }.  }.  if( rc
1304a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1304b 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52     pPager->nSubR
1304c 65 63 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74  ec++;.    assert
1304d 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  ( pPager->nSavep
1304e 6f 69 6e 74 3e 30 20 29 3b 0a 20 20 20 20 72 63  oint>0 );.    rc
1304f 20 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e   = addToSavepoin
13050 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c  tBitvecs(pPager,
13051 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d   pPg->pgno);.  }
13052 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
13053 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
13054 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
13055 62 79 20 74 68 65 20 70 63 61 63 68 65 20 6c 61  by the pcache la
13056 79 65 72 20 77 68 65 6e 20 69 74 20 68 61 73 20  yer when it has 
13057 72 65 61 63 68 65 64 20 73 6f 6d 65 0a 2a 2a 20  reached some.** 
13058 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c 69 6d 69  soft memory limi
13059 74 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67  t. The first arg
1305a 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74  ument is a point
1305b 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 6f 62  er to a Pager ob
1305c 6a 65 63 74 0a 2a 2a 20 28 63 61 73 74 20 61 73  ject.** (cast as
1305d 20 61 20 76 6f 69 64 2a 29 2e 20 54 68 65 20 70   a void*). The p
1305e 61 67 65 72 20 69 73 20 61 6c 77 61 79 73 20 27  ager is always '
1305f 70 75 72 67 65 61 62 6c 65 27 20 28 6e 6f 74 20  purgeable' (not 
13060 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20  an in-memory.** 
13061 64 61 74 61 62 61 73 65 29 2e 20 54 68 65 20 73  database). The s
13062 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
13063 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  s a reference to
13064 20 61 20 70 61 67 65 20 74 68 61 74 20 69 73 20   a page that is 
13065 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 64 69  .** currently di
13066 72 74 79 20 62 75 74 20 68 61 73 20 6e 6f 20 6f  rty but has no o
13067 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
13068 65 6e 63 65 73 2e 20 54 68 65 20 70 61 67 65 0a  ences. The page.
13069 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 73 73  ** is always ass
1306a 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
1306b 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61   Pager object pa
1306c 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
1306d 74 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a  t .** argument..
1306e 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 62 20 6f 66  **.** The job of
1306f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13070 73 20 74 6f 20 6d 61 6b 65 20 70 50 67 20 63 6c  s to make pPg cl
13071 65 61 6e 20 62 79 20 77 72 69 74 69 6e 67 20 69  ean by writing i
13072 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f  ts contents.** o
13073 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
13074 73 65 20 66 69 6c 65 2c 20 69 66 20 70 6f 73 73  se file, if poss
13075 69 62 6c 65 2e 20 54 68 69 73 20 6d 61 79 20 69  ible. This may i
13076 6e 76 6f 6c 76 65 20 73 79 6e 63 69 6e 67 20 74  nvolve syncing t
13077 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  he.** journal fi
13078 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  le. .**.** If su
13079 63 63 65 73 73 66 75 6c 2c 20 73 71 6c 69 74 65  ccessful, sqlite
1307a 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e  3PcacheMakeClean
1307b 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20  () is called on 
1307c 74 68 65 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20  the page and.** 
1307d 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
1307e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72  ed. If an IO err
1307f 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
13080 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74  trying to make t
13081 68 65 0a 2a 2a 20 70 61 67 65 20 63 6c 65 61 6e  he.** page clean
13082 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63  , the IO error c
13083 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
13084 20 49 66 20 74 68 65 20 70 61 67 65 20 63 61 6e   If the page can
13085 6e 6f 74 20 62 65 0a 2a 2a 20 6d 61 64 65 20 63  not be.** made c
13086 6c 65 61 6e 20 66 6f 72 20 73 6f 6d 65 20 6f 74  lean for some ot
13087 68 65 72 20 72 65 61 73 6f 6e 2c 20 62 75 74 20  her reason, but 
13088 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  no error occurs,
13089 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a   then SQLITE_OK.
1308a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 62  ** is returned b
1308b 79 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d  y sqlite3PcacheM
1308c 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20 6e 6f  akeClean() is no
1308d 74 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  t called..*/.sta
1308e 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 74 72  tic int pagerStr
1308f 65 73 73 28 76 6f 69 64 20 2a 70 2c 20 50 67 48  ess(void *p, PgH
13090 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65  dr *pPg){.  Page
13091 72 20 2a 70 50 61 67 65 72 20 3d 20 28 50 61 67  r *pPager = (Pag
13092 65 72 20 2a 29 70 3b 0a 20 20 69 6e 74 20 72 63  er *)p;.  int rc
13093 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
13094 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 50   assert( pPg->pP
13095 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a  ager==pPager );.
13096 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66    assert( pPg->f
13097 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59  lags&PGHDR_DIRTY
13098 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 64 6f   );..  /* The do
13099 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 69 73 20  NotSync flag is 
1309a 73 65 74 20 62 79 20 74 68 65 20 73 71 6c 69 74  set by the sqlit
1309b 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 66  e3PagerWrite() f
1309c 75 6e 63 74 69 6f 6e 20 77 68 69 6c 65 20 69 74  unction while it
1309d 0a 20 20 2a 2a 20 69 73 20 6a 6f 75 72 6e 61 6c  .  ** is journal
1309e 6c 69 6e 67 20 61 20 73 65 74 20 6f 66 20 74 77  ling a set of tw
1309f 6f 20 6f 72 20 6d 6f 72 65 20 64 61 74 61 62 61  o or more databa
130a0 73 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72  se pages that ar
130a1 65 20 73 74 6f 72 65 64 0a 20 20 2a 2a 20 6f 6e  e stored.  ** on
130a2 20 74 68 65 20 73 61 6d 65 20 64 69 73 6b 20 73   the same disk s
130a3 65 63 74 6f 72 2e 20 53 79 6e 63 69 6e 67 20 74  ector. Syncing t
130a4 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f  he journal is no
130a5 74 20 61 6c 6c 6f 77 65 64 20 77 68 69 6c 65 0a  t allowed while.
130a6 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 70    ** this is hap
130a7 70 65 6e 69 6e 67 20 61 73 20 69 74 20 69 73 20  pening as it is 
130a8 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61  important that a
130a9 6c 6c 20 6d 65 6d 62 65 72 73 20 6f 66 20 73 75  ll members of su
130aa 63 68 20 61 0a 20 20 2a 2a 20 73 65 74 20 6f 66  ch a.  ** set of
130ab 20 70 61 67 65 73 20 61 72 65 20 73 79 6e 63 65   pages are synce
130ac 64 20 74 6f 20 64 69 73 6b 20 74 6f 67 65 74 68  d to disk togeth
130ad 65 72 2e 20 53 6f 2c 20 69 66 20 74 68 65 20 70  er. So, if the p
130ae 61 67 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  age this functio
130af 6e 0a 20 20 2a 2a 20 69 73 20 74 72 79 69 6e 67  n.  ** is trying
130b0 20 74 6f 20 6d 61 6b 65 20 63 6c 65 61 6e 20 77   to make clean w
130b1 69 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f  ill require a jo
130b2 75 72 6e 61 6c 20 73 79 6e 63 20 61 6e 64 20 74  urnal sync and t
130b3 68 65 20 64 6f 4e 6f 74 53 79 6e 63 0a 20 20 2a  he doNotSync.  *
130b4 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 72  * flag is set, r
130b5 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f  eturn without do
130b6 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 54 68  ing anything. Th
130b7 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20 77  e pcache layer w
130b8 69 6c 6c 0a 20 20 2a 2a 20 6a 75 73 74 20 68 61  ill.  ** just ha
130b9 76 65 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61  ve to go ahead a
130ba 6e 64 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  nd allocate a ne
130bb 77 20 70 61 67 65 20 62 75 66 66 65 72 20 69 6e  w page buffer in
130bc 73 74 65 61 64 20 6f 66 0a 20 20 2a 2a 20 72 65  stead of.  ** re
130bd 75 73 69 6e 67 20 70 50 67 2e 0a 20 20 2a 2a 0a  using pPg..  **.
130be 20 20 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20    ** Similarly, 
130bf 69 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73  if the pager has
130c0 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64   already entered
130c1 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
130c2 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 74 72  , do not.  ** tr
130c3 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63  y to write the c
130c4 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 67 20 74  ontents of pPg t
130c5 6f 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69  o disk..  */.  i
130c6 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d  f( NEVER(pPager-
130c7 3e 65 72 72 43 6f 64 65 29 0a 20 20 20 7c 7c 20  >errCode).   || 
130c8 28 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79  (pPager->doNotSy
130c9 6e 63 20 26 26 20 70 50 67 2d 3e 66 6c 61 67 73  nc && pPg->flags
130ca 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
130cb 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ).  ){.    retur
130cc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
130cd 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20  ..  /* Sync the 
130ce 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20  journal file if 
130cf 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69  required. */.  i
130d0 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  f( pPg->flags&PG
130d1 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b  HDR_NEED_SYNC ){
130d2 0a 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f  .    rc = syncJo
130d3 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
130d4 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
130d5 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e  E_OK && pPager->
130d6 66 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 20 20  fullSync && .   
130d7 20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75     !(pPager->jou
130d8 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
130d9 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
130da 52 59 29 20 26 26 0a 20 20 20 20 20 20 21 28 73  RY) &&.      !(s
130db 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68  qlite3OsDeviceCh
130dc 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50  aracteristics(pP
130dd 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45  ager->fd)&SQLITE
130de 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45  _IOCAP_SAFE_APPE
130df 4e 44 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ND).    ){.     
130e0 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20   pPager->nRec = 
130e1 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72  0;.      rc = wr
130e2 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  iteJournalHdr(pP
130e3 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ager);.    }.  }
130e4 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61  ..  /* If the pa
130e5 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 69  ge number of thi
130e6 73 20 70 61 67 65 20 69 73 20 6c 61 72 67 65 72  s page is larger
130e7 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e   than the curren
130e8 74 20 73 69 7a 65 20 6f 66 0a 20 20 2a 2a 20 74  t size of.  ** t
130e9 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67  he database imag
130ea 65 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74  e, it may need t
130eb 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20  o be written to 
130ec 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e  the sub-journal.
130ed 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65  .  ** This is be
130ee 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74  cause the call t
130ef 6f 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61  o pager_write_pa
130f0 67 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 20 77  gelist() below w
130f1 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 61 63 74  ill not.  ** act
130f2 75 61 6c 6c 79 20 77 72 69 74 65 20 64 61 74 61  ually write data
130f3 20 74 6f 20 74 68 65 20 66 69 6c 65 20 69 6e 20   to the file in 
130f4 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a  this case..  **.
130f5 20 20 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68    ** Consider th
130f6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75  e following sequ
130f7 65 6e 63 65 20 6f 66 20 65 76 65 6e 74 73 3a 0a  ence of events:.
130f8 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49    **.  **   BEGI
130f9 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75  N;.  **     <jou
130fa 72 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20 20 2a  rnal page X>.  *
130fb 2a 20 20 20 20 20 3c 6d 6f 64 69 66 79 20 70 61  *     <modify pa
130fc 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 53  ge X>.  **     S
130fd 41 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20 20 2a  AVEPOINT sp;.  *
130fe 2a 20 20 20 20 20 20 20 3c 73 68 72 69 6e 6b 20  *       <shrink 
130ff 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f  database file to
13100 20 59 20 70 61 67 65 73 3e 0a 20 20 2a 2a 20 20   Y pages>.  **  
13101 20 20 20 20 20 70 61 67 65 72 53 74 72 65 73 73       pagerStress
13102 28 70 61 67 65 20 58 29 0a 20 20 2a 2a 20 20 20  (page X).  **   
13103 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70    ROLLBACK TO sp
13104 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 28  ;.  **.  ** If (
13105 58 3e 59 29 2c 20 74 68 65 6e 20 77 68 65 6e 20  X>Y), then when 
13106 70 61 67 65 72 53 74 72 65 73 73 20 69 73 20 63  pagerStress is c
13107 61 6c 6c 65 64 20 70 61 67 65 20 58 20 77 69 6c  alled page X wil
13108 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e  l not be written
13109 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65  .  ** out to the
1310a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
1310b 62 75 74 20 77 69 6c 6c 20 62 65 20 64 72 6f 70  but will be drop
1310c 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63  ped from the cac
1310d 68 65 2e 20 54 68 65 6e 2c 0a 20 20 2a 2a 20 66  he. Then,.  ** f
1310e 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 52 4f  ollowing the "RO
1310f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 73 74  LLBACK TO sp" st
13110 61 74 65 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67  atement, reading
13111 20 70 61 67 65 20 58 20 77 69 6c 6c 20 72 65 61   page X will rea
13112 64 0a 20 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d  d.  ** data from
13113 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13114 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65  le. This will be
13115 20 74 68 65 20 63 6f 70 79 20 6f 66 20 70 61 67   the copy of pag
13116 65 20 58 20 61 73 20 69 74 0a 20 20 2a 2a 20 77  e X as it.  ** w
13117 61 73 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e  as when the tran
13118 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c  saction started,
13119 20 6e 6f 74 20 61 73 20 69 74 20 77 61 73 20 77   not as it was w
1311a 68 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 20 73  hen "SAVEPOINT s
1311b 70 22 0a 20 20 2a 2a 20 77 61 73 20 65 78 65 63  p".  ** was exec
1311c 75 74 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  uted..  **.  ** 
1311d 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20  The solution is 
1311e 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72  to write the cur
1311f 72 65 6e 74 20 64 61 74 61 20 66 6f 72 20 70 61  rent data for pa
13120 67 65 20 58 20 69 6e 74 6f 20 74 68 65 20 0a 20  ge X into the . 
13121 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20   ** sub-journal 
13122 66 69 6c 65 20 6e 6f 77 20 28 69 66 20 69 74 20  file now (if it 
13123 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 74  is not already t
13124 68 65 72 65 29 2c 20 73 6f 20 74 68 61 74 20 69  here), so that i
13125 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72  t will.  ** be r
13126 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 63  estored to its c
13127 75 72 72 65 6e 74 20 76 61 6c 75 65 20 77 68 65  urrent value whe
13128 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20  n the "ROLLBACK 
13129 54 4f 20 73 70 22 20 69 73 20 0a 20 20 2a 2a 20  TO sp" is .  ** 
1312a 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2f 0a 20  executed..  */. 
1312b 20 69 66 28 20 4e 45 56 45 52 28 0a 20 20 20 20   if( NEVER(.    
1312c 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20    rc==SQLITE_OK 
1312d 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61  && pPg->pgno>pPa
1312e 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 73  ger->dbSize && s
1312f 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28  ubjRequiresPage(
13130 70 50 67 29 0a 20 20 29 20 29 7b 0a 20 20 20 20  pPg).  ) ){.    
13131 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50  rc = subjournalP
13132 61 67 65 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20  age(pPg);.  }.. 
13133 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f   /* Write the co
13134 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
13135 67 65 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61  ge out to the da
13136 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a  tabase file. */.
13137 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13138 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e  _OK ){.    pPg->
13139 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20  pDirty = 0;.    
1313a 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65  rc = pager_write
1313b 5f 70 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a  _pagelist(pPg);.
1313c 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74    }..  /* Mark t
1313d 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e  he page as clean
1313e 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53  . */.  if( rc==S
1313f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13140 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54 52  PAGERTRACE(("STR
13141 45 53 53 20 25 64 20 70 61 67 65 20 25 64 5c 6e  ESS %d page %d\n
13142 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  ", PAGERID(pPage
13143 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b  r), pPg->pgno));
13144 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63  .    sqlite3Pcac
13145 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29  heMakeClean(pPg)
13146 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
13147 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
13148 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  er, rc);.}.../*.
13149 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20  ** Allocate and 
1314a 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77  initialize a new
1314b 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 61 6e   Pager object an
1314c 64 20 70 75 74 20 61 20 70 6f 69 6e 74 65 72 20  d put a pointer 
1314d 74 6f 20 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50  to it.** in *ppP
1314e 61 67 65 72 2e 20 54 68 65 20 70 61 67 65 72 20  ager. The pager 
1314f 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c  should eventuall
13150 79 20 62 65 20 66 72 65 65 64 20 62 79 20 70 61  y be freed by pa
13151 73 73 69 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73  ssing it.** to s
13152 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65  qlite3PagerClose
13153 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46  ()..**.** The zF
13154 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74  ilename argument
13155 20 69 73 20 74 68 65 20 70 61 74 68 20 74 6f 20   is the path to 
13156 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13157 65 20 74 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66  e to open..** If
13158 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55   zFilename is NU
13159 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d  LL then a random
1315a 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61  ly-named tempora
1315b 72 79 20 66 69 6c 65 20 69 73 20 63 72 65 61 74  ry file is creat
1315c 65 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 61  ed.** and used a
1315d 73 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65  s the file to be
1315e 20 63 61 63 68 65 64 2e 20 54 65 6d 70 6f 72 61   cached. Tempora
1315f 72 79 20 66 69 6c 65 73 20 61 72 65 20 62 65 20  ry files are be 
13160 64 65 6c 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d  deleted.** autom
13161 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68  atically when th
13162 65 79 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 49  ey are closed. I
13163 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22  f zFilename is "
13164 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a  :memory:" then .
13165 2a 2a 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69  ** all informati
13166 6f 6e 20 69 73 20 68 65 6c 64 20 69 6e 20 63 61  on is held in ca
13167 63 68 65 2e 20 49 74 20 69 73 20 6e 65 76 65 72  che. It is never
13168 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b   written to disk
13169 2e 20 0a 2a 2a 20 54 68 69 73 20 63 61 6e 20 62  . .** This can b
1316a 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  e used to implem
1316b 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ent an in-memory
1316c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
1316d 20 54 68 65 20 6e 45 78 74 72 61 20 70 61 72 61   The nExtra para
1316e 6d 65 74 65 72 20 73 70 65 63 69 66 69 65 73 20  meter specifies 
1316f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
13170 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 6c 6c  tes of space all
13171 6f 63 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20  ocated.** along 
13172 77 69 74 68 20 65 61 63 68 20 70 61 67 65 20 72  with each page r
13173 65 66 65 72 65 6e 63 65 2e 20 54 68 69 73 20 73  eference. This s
13174 70 61 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c  pace is availabl
13175 65 20 74 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a  e to the user.**
13176 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33   via the sqlite3
13177 50 61 67 65 72 47 65 74 45 78 74 72 61 28 29 20  PagerGetExtra() 
13178 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  API..**.** The f
13179 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 69 73  lags argument is
1317a 20 75 73 65 64 20 74 6f 20 73 70 65 63 69 66 79   used to specify
1317b 20 70 72 6f 70 65 72 74 69 65 73 20 74 68 61 74   properties that
1317c 20 61 66 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f   affect the.** o
1317d 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  peration of the 
1317e 70 61 67 65 72 2e 20 49 74 20 73 68 6f 75 6c 64  pager. It should
1317f 20 62 65 20 70 61 73 73 65 64 20 73 6f 6d 65 20   be passed some 
13180 62 69 74 77 69 73 65 20 63 6f 6d 62 69 6e 61 74  bitwise combinat
13181 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41  ion.** of the PA
13182 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c  GER_OMIT_JOURNAL
13183 20 61 6e 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45   and PAGER_NO_RE
13184 41 44 4c 4f 43 4b 20 66 6c 61 67 73 2e 0a 2a 2a  ADLOCK flags..**
13185 0a 2a 2a 20 54 68 65 20 76 66 73 46 6c 61 67 73  .** The vfsFlags
13186 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20   parameter is a 
13187 62 69 74 6d 61 73 6b 20 74 6f 20 70 61 73 73 20  bitmask to pass 
13188 74 6f 20 74 68 65 20 66 6c 61 67 73 20 70 61 72  to the flags par
13189 61 6d 65 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65  ameter.** of the
1318a 20 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20   xOpen() method 
1318b 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  of the supplied 
1318c 56 46 53 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67  VFS when opening
1318d 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49   files. .**.** I
1318e 66 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65  f the pager obje
1318f 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ct is allocated 
13190 61 6e 64 20 74 68 65 20 73 70 65 63 69 66 69 65  and the specifie
13191 64 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 0a 2a  d file opened .*
13192 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20  * successfully, 
13193 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
13194 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67  urned and *ppPag
13195 65 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  er set to point 
13196 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 70 61  to.** the new pa
13197 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61  ger object. If a
13198 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
13199 2a 70 70 50 61 67 65 72 20 69 73 20 73 65 74 20  *ppPager is set 
1319a 74 6f 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65  to NULL.** and e
1319b 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e  rror code return
1319c 65 64 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ed. This functio
1319d 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  n may return SQL
1319e 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71  ITE_NOMEM.** (sq
1319f 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 69 73  lite3Malloc() is
131a0 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74   used to allocat
131a1 65 20 6d 65 6d 6f 72 79 29 2c 20 53 51 4c 49 54  e memory), SQLIT
131a2 45 5f 43 41 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a  E_CANTOPEN or .*
131a3 2a 20 76 61 72 69 6f 75 73 20 53 51 4c 49 54 45  * various SQLITE
131a4 5f 49 4f 5f 58 58 58 20 65 72 72 6f 72 73 2e 0a  _IO_XXX errors..
131a5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
131a6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
131a7 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  erOpen(.  sqlite
131a8 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
131a9 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
131aa 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 20 74 6f  l file system to
131ab 20 75 73 65 20 2a 2f 0a 20 20 50 61 67 65 72 20   use */.  Pager 
131ac 2a 2a 70 70 50 61 67 65 72 2c 20 20 20 20 20 20  **ppPager,      
131ad 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72     /* OUT: Retur
131ae 6e 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75  n the Pager stru
131af 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20  cture here */.  
131b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
131b1 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65  ename,   /* Name
131b2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
131b3 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f   file to open */
131b4 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20  .  int nExtra,  
131b5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
131b6 78 74 72 61 20 62 79 74 65 73 20 61 70 70 65 6e  xtra bytes appen
131b7 64 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d  d to each in-mem
131b8 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  ory page */.  in
131b9 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
131ba 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20         /* flags 
131bb 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73  controlling this
131bc 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76   file */.  int v
131bd 66 73 46 6c 61 67 73 2c 20 20 20 20 20 20 20 20  fsFlags,        
131be 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73      /* flags pas
131bf 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73  sed through to s
131c0 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e  qlite3_vfs.xOpen
131c1 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  () */.  void (*x
131c2 52 65 69 6e 69 74 29 28 44 62 50 61 67 65 2a 29  Reinit)(DbPage*)
131c3 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20   /* Function to 
131c4 72 65 69 6e 69 74 69 61 6c 69 7a 65 20 70 61 67  reinitialize pag
131c5 65 73 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70  es */.){.  u8 *p
131c6 50 74 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  Ptr;.  Pager *pP
131c7 61 67 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20  ager = 0;       
131c8 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  /* Pager object 
131c9 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20  to allocate and 
131ca 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20  return */.  int 
131cb 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
131cc 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
131cd 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d  ode */.  int tem
131ce 70 46 69 6c 65 20 3d 20 30 3b 20 20 20 20 20 20  pFile = 0;      
131cf 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 74 65    /* True for te
131d0 6d 70 20 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20  mp files (incl. 
131d1 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29  in-memory files)
131d2 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20   */.  int memDb 
131d3 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f  = 0;           /
131d4 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
131d5 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66  s an in-memory f
131d6 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61  ile */.  int rea
131d7 64 4f 6e 6c 79 20 3d 20 30 3b 20 20 20 20 20 20  dOnly = 0;      
131d8 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
131d9 73 20 69 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79  s is a read-only
131da 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a   file */.  int j
131db 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20  ournalFileSize; 
131dc 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20      /* Bytes to 
131dd 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 65 61 63  allocate for eac
131de 68 20 6a 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a  h journal fd */.
131df 20 20 63 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d    char *zPathnam
131e0 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 46 75  e = 0;     /* Fu
131e1 6c 6c 20 70 61 74 68 20 74 6f 20 64 61 74 61 62  ll path to datab
131e2 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e  ase file */.  in
131e3 74 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b  t nPathname = 0;
131e4 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
131e5 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 50 61   of bytes in zPa
131e6 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20  thname */.  int 
131e7 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c  useJournal = (fl
131e8 61 67 73 20 26 20 50 41 47 45 52 5f 4f 4d 49 54  ags & PAGER_OMIT
131e9 5f 4a 4f 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a  _JOURNAL)==0; /*
131ea 20 46 61 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a   False to omit j
131eb 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20  ournal */.  int 
131ec 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c  noReadlock = (fl
131ed 61 67 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52  ags & PAGER_NO_R
131ee 45 41 44 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a  EADLOCK)!=0;  /*
131ef 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 72 65   True to omit re
131f0 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  ad-lock */.  int
131f1 20 70 63 61 63 68 65 53 69 7a 65 20 3d 20 73 71   pcacheSize = sq
131f2 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28  lite3PcacheSize(
131f3 29 3b 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65  );       /* Byte
131f4 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f  s to allocate fo
131f5 72 20 50 43 61 63 68 65 20 2a 2f 0a 20 20 75 31  r PCache */.  u1
131f6 36 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53  6 szPageDflt = S
131f7 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
131f8 47 45 5f 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66  GE_SIZE;  /* Def
131f9 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 2a  ault page size *
131fa 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  /..  /* Figure o
131fb 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63  ut how much spac
131fc 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f  e is required fo
131fd 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66  r each journal f
131fe 69 6c 65 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20  ile-handle.  ** 
131ff 28 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 6f  (there are two o
13200 66 20 74 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e  f them, the main
13201 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65   journal and the
13202 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54   sub-journal). T
13203 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20  his.  ** is the 
13204 6d 61 78 69 6d 75 6d 20 73 70 61 63 65 20 72 65  maximum space re
13205 71 75 69 72 65 64 20 66 6f 72 20 61 6e 20 69 6e  quired for an in
13206 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20  -memory journal 
13207 66 69 6c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a  file handle .  *
13208 2a 20 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20  * and a regular 
13209 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e  journal file-han
1320a 64 6c 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61  dle. Note that a
1320b 20 22 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61   "regular journa
1320c 6c 2d 68 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d  l-handle".  ** m
1320d 61 79 20 62 65 20 61 20 77 72 61 70 70 65 72 20  ay be a wrapper 
1320e 63 61 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69  capable of cachi
1320f 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 6f 72  ng the first por
13210 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72  tion of the jour
13211 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e  nal.  ** file in
13212 20 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65   memory to imple
13213 6d 65 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d  ment the atomic-
13214 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  write optimizati
13215 6f 6e 20 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f  on (see .  ** so
13216 75 72 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61  urce file journa
13217 6c 2e 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  l.c)..  */.  if(
13218 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
13219 69 7a 65 28 70 56 66 73 29 3e 73 71 6c 69 74 65  ize(pVfs)>sqlite
1321a 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  3MemJournalSize(
1321b 29 20 29 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c  ) ){.    journal
1321c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44  FileSize = ROUND
1321d 38 28 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  8(sqlite3Journal
1321e 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 7d  Size(pVfs));.  }
1321f 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61  else{.    journa
13220 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e  lFileSize = ROUN
13221 44 38 28 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  D8(sqlite3MemJou
13222 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d  rnalSize());.  }
13223 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f  ..  /* Set the o
13224 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 74  utput variable t
13225 6f 20 4e 55 4c 4c 20 69 6e 20 63 61 73 65 20 61  o NULL in case a
13226 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20  n error occurs. 
13227 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20  */.  *ppPager = 
13228 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65  0;..  /* Compute
13229 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 66   and store the f
1322a 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 69 6e 20  ull pathname in 
1322b 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66  an allocated buf
1322c 66 65 72 20 70 6f 69 6e 74 65 64 0a 20 20 2a 2a  fer pointed.  **
1322d 20 74 6f 20 62 79 20 7a 50 61 74 68 6e 61 6d 65   to by zPathname
1322e 2c 20 6c 65 6e 67 74 68 20 6e 50 61 74 68 6e 61  , length nPathna
1322f 6d 65 2e 20 4f 72 2c 20 69 66 20 74 68 69 73 20  me. Or, if this 
13230 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  is a temporary f
13231 69 6c 65 2c 0a 20 20 2a 2a 20 6c 65 61 76 65 20  ile,.  ** leave 
13232 62 6f 74 68 20 6e 50 61 74 68 6e 61 6d 65 20 61  both nPathname a
13233 6e 64 20 7a 50 61 74 68 6e 61 6d 65 20 73 65 74  nd zPathname set
13234 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66   to 0..  */.  if
13235 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a  ( zFilename && z
13236 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20  Filename[0] ){. 
13237 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 70     nPathname = p
13238 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b  Vfs->mxPathname+
13239 31 3b 0a 20 20 20 20 7a 50 61 74 68 6e 61 6d 65  1;.    zPathname
1323a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
1323b 28 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20  (nPathname*2);. 
1323c 20 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65     if( zPathname
1323d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
1323e 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1323f 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
13240 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f  SQLITE_OMIT_MEMO
13241 52 59 44 42 0a 20 20 20 20 69 66 28 20 73 74 72  RYDB.    if( str
13242 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a  cmp(zFilename,":
13243 6d 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a  memory:")==0 ){.
13244 20 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b        memDb = 1;
13245 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65  .      zPathname
13246 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  [0] = 0;.    }el
13247 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a  se.#endif.    {.
13248 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b        zPathname[
13249 30 5d 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20  0] = 0; /* Make 
1324a 73 75 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  sure initialized
1324b 20 65 76 65 6e 20 69 66 20 46 75 6c 6c 50 61 74   even if FullPat
1324c 68 6e 61 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f  hname() fails */
1324d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1324e 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d  te3OsFullPathnam
1324f 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d  e(pVfs, zFilenam
13250 65 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50  e, nPathname, zP
13251 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  athname);.    }.
13252 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d  .    nPathname =
13253 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
13254 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20  (zPathname);.   
13255 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13256 4f 4b 20 26 26 20 6e 50 61 74 68 6e 61 6d 65 2b  OK && nPathname+
13257 38 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  8>pVfs->mxPathna
13258 6d 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  me ){.      /* T
13259 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 74 61  his branch is ta
1325a 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 6a 6f 75  ken when the jou
1325b 72 6e 61 6c 20 70 61 74 68 20 72 65 71 75 69 72  rnal path requir
1325c 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 74  ed by.      ** t
1325d 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e  he database bein
1325e 67 20 6f 70 65 6e 65 64 20 77 69 6c 6c 20 62 65  g opened will be
1325f 20 6d 6f 72 65 20 74 68 61 6e 20 70 56 66 73 2d   more than pVfs-
13260 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 20 20 20 20  >mxPathname.    
13261 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 6c 65    ** bytes in le
13262 6e 67 74 68 2e 20 54 68 69 73 20 6d 65 61 6e 73  ngth. This means
13263 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61   the database ca
13264 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2c 0a  nnot be opened,.
13265 20 20 20 20 20 20 2a 2a 20 61 73 20 69 74 20 77        ** as it w
13266 69 6c 6c 20 6e 6f 74 20 62 65 20 70 6f 73 73 69  ill not be possi
13267 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  ble to open the 
13268 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20  journal file or 
13269 65 76 65 6e 0a 20 20 20 20 20 20 2a 2a 20 63 68  even.      ** ch
1326a 65 63 6b 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f  eck for a hot-jo
1326b 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 72 65 61  urnal before rea
1326c 64 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ding..      */. 
1326d 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1326e 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d  _CANTOPEN;.    }
1326f 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
13270 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
13271 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61  sqlite3_free(zPa
13272 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72  thname);.      r
13273 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
13274 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61    }..  /* Alloca
13275 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68  te memory for th
13276 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72  e Pager structur
13277 65 2c 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74  e, PCache object
13278 2c 20 74 68 65 0a 20 20 2a 2a 20 74 68 72 65 65  , the.  ** three
13279 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
1327a 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  s, the database 
1327b 66 69 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68  file name and th
1327c 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20  e journal .  ** 
1327d 66 69 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c  file name. The l
1327e 61 79 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20  ayout in memory 
1327f 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20  is as follows:. 
13280 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 50 61 67   **.  **     Pag
13281 65 72 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20  er object       
13282 20 20 20 20 20 20 20 20 20 20 20 20 20 28 73 69               (si
13283 7a 65 6f 66 28 50 61 67 65 72 29 20 62 79 74 65  zeof(Pager) byte
13284 73 29 0a 20 20 2a 2a 20 20 20 20 20 50 43 61 63  s).  **     PCac
13285 68 65 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20  he object       
13286 20 20 20 20 20 20 20 20 20 20 20 20 28 73 71 6c              (sql
13287 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29  ite3PcacheSize()
13288 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20   bytes).  **    
13289 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   Database file h
1328a 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 20 20  andle           
1328b 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65   (pVfs->szOsFile
1328c 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20   bytes).  **    
1328d 20 53 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   Sub-journal fil
1328e 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20  e handle        
1328f 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a   (journalFileSiz
13290 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20  e bytes).  **   
13291 20 20 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66    Main journal f
13292 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20  ile handle      
13293 20 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69    (journalFileSi
13294 7a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20  ze bytes).  **  
13295 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65     Database file
13296 20 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20   name           
13297 20 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20     (nPathname+1 
13298 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20  bytes).  **     
13299 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  Journal file nam
1329a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
1329b 28 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62  (nPathname+8+1 b
1329c 79 74 65 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74  ytes).  */.  pPt
1329d 72 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65  r = (u8 *)sqlite
1329e 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20  3MallocZero(.   
1329f 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a   ROUND8(sizeof(*
132a0 70 50 61 67 65 72 29 29 20 2b 20 20 20 20 20 20  pPager)) +      
132a1 2f 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75  /* Pager structu
132a2 72 65 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38  re */.    ROUND8
132a3 28 70 63 61 63 68 65 53 69 7a 65 29 20 2b 20 20  (pcacheSize) +  
132a4 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43 61 63           /* PCac
132a5 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20  he object */.   
132a6 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a   ROUND8(pVfs->sz
132a7 4f 73 46 69 6c 65 29 20 2b 20 20 20 20 20 20 20  OsFile) +       
132a8 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66  /* The main db f
132a9 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e  ile */.    journ
132aa 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b  alFileSize * 2 +
132ab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
132ac 20 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   two journal fil
132ad 65 73 20 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68  es */ .    nPath
132ae 6e 61 6d 65 20 2b 20 31 20 2b 20 20 20 20 20 20  name + 1 +      
132af 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69            /* zFi
132b0 6c 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50  lename */.    nP
132b1 61 74 68 6e 61 6d 65 20 2b 20 38 20 2b 20 31 20  athname + 8 + 1 
132b2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
132b3 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b  zJournal */.  );
132b4 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54  .  assert( EIGHT
132b5 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  _BYTE_ALIGNMENT(
132b6 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54  SQLITE_INT_TO_PT
132b7 52 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a  R(journalFileSiz
132b8 65 29 29 20 29 3b 0a 20 20 69 66 28 20 21 70 50  e)) );.  if( !pP
132b9 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  tr ){.    sqlite
132ba 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65  3_free(zPathname
132bb 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
132bc 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
132bd 20 20 70 50 61 67 65 72 20 3d 20 20 20 20 20 20    pPager =      
132be 20 20 20 20 20 20 20 20 28 50 61 67 65 72 2a 29          (Pager*)
132bf 28 70 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72  (pPtr);.  pPager
132c0 2d 3e 70 50 43 61 63 68 65 20 3d 20 20 20 20 28  ->pPCache =    (
132c1 50 43 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d  PCache*)(pPtr +=
132c2 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a   ROUND8(sizeof(*
132c3 70 50 61 67 65 72 29 29 29 3b 0a 20 20 70 50 61  pPager)));.  pPa
132c4 67 65 72 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c  ger->fd =   (sql
132c5 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72  ite3_file*)(pPtr
132c6 20 2b 3d 20 52 4f 55 4e 44 38 28 70 63 61 63 68   += ROUND8(pcach
132c7 65 53 69 7a 65 29 29 3b 0a 20 20 70 50 61 67 65  eSize));.  pPage
132c8 72 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74  r->sjfd = (sqlit
132c9 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b  e3_file*)(pPtr +
132ca 3d 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73  = ROUND8(pVfs->s
132cb 7a 4f 73 46 69 6c 65 29 29 3b 0a 20 20 70 50 61  zOsFile));.  pPa
132cc 67 65 72 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c  ger->jfd =  (sql
132cd 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72  ite3_file*)(pPtr
132ce 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53   += journalFileS
132cf 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  ize);.  pPager->
132d0 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28  zFilename =    (
132d1 63 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a  char*)(pPtr += j
132d2 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b  ournalFileSize);
132d3 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54  .  assert( EIGHT
132d4 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  _BYTE_ALIGNMENT(
132d5 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a  pPager->jfd) );.
132d6 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68  .  /* Fill in th
132d7 65 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d  e Pager.zFilenam
132d8 65 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75  e and Pager.zJou
132d9 72 6e 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66  rnal buffers, if
132da 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20   required. */.  
132db 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b  if( zPathname ){
132dc 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f  .    pPager->zJo
132dd 75 72 6e 61 6c 20 3d 20 20 20 28 63 68 61 72 2a  urnal =   (char*
132de 29 28 70 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e  )(pPtr += nPathn
132df 61 6d 65 20 2b 20 31 29 3b 0a 20 20 20 20 6d 65  ame + 1);.    me
132e0 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 46 69  mcpy(pPager->zFi
132e1 6c 65 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d  lename, zPathnam
132e2 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20  e, nPathname);. 
132e3 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72     memcpy(pPager
132e4 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74  ->zJournal, zPat
132e5 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65  hname, nPathname
132e6 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70  );.    memcpy(&p
132e7 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b  Pager->zJournal[
132e8 6e 50 61 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f  nPathname], "-jo
132e9 75 72 6e 61 6c 22 2c 20 38 29 3b 0a 20 20 20 20  urnal", 8);.    
132ea 69 66 28 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c  if( pPager->zFil
132eb 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 20 70 50  ename[0]==0 ) pP
132ec 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 30  ager->zJournal[0
132ed 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  ] = 0;.    sqlit
132ee 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d  e3_free(zPathnam
132ef 65 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72  e);.  }.  pPager
132f0 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20  ->pVfs = pVfs;. 
132f1 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67   pPager->vfsFlag
132f2 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a 0a 20  s = vfsFlags;.. 
132f3 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 70 61 67   /* Open the pag
132f4 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20  er file..  */.  
132f5 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26  if( zFilename &&
132f6 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 26 26   zFilename[0] &&
132f7 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 69   !memDb ){.    i
132f8 6e 74 20 66 6f 75 74 20 3d 20 30 3b 20 20 20 20  nt fout = 0;    
132f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132fa 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 72 65 74  /* VFS flags ret
132fb 75 72 6e 65 64 20 62 79 20 78 4f 70 65 6e 28 29  urned by xOpen()
132fc 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
132fd 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c  ite3OsOpen(pVfs,
132fe 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
132ff 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20  me, pPager->fd, 
13300 76 66 73 46 6c 61 67 73 2c 20 26 66 6f 75 74 29  vfsFlags, &fout)
13301 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d  ;.    readOnly =
13302 20 28 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50   (fout&SQLITE_OP
13303 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20  EN_READONLY);.. 
13304 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c     /* If the fil
13305 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c  e was successful
13306 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65  ly opened for re
13307 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2c  ad/write access,
13308 0a 20 20 20 20 2a 2a 20 63 68 6f 6f 73 65 20 61  .    ** choose a
13309 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69   default page si
1330a 7a 65 20 69 6e 20 63 61 73 65 20 77 65 20 68 61  ze in case we ha
1330b 76 65 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  ve to create the
1330c 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
1330d 20 66 69 6c 65 2e 20 54 68 65 20 64 65 66 61 75   file. The defau
1330e 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 73 20  lt page size is 
1330f 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 3a 0a  the maximum of:.
13310 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
13311 20 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   + SQLITE_DEFAUL
13312 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20  T_PAGE_SIZE,.   
13313 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61 6c   **    + The val
13314 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
13315 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69  qlite3OsSectorSi
13316 7a 65 28 29 0a 20 20 20 20 2a 2a 20 20 20 20 2b  ze().    **    +
13317 20 54 68 65 20 6c 61 72 67 65 73 74 20 70 61 67   The largest pag
13318 65 20 73 69 7a 65 20 74 68 61 74 20 63 61 6e 20  e size that can 
13319 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69  be written atomi
1331a 63 61 6c 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  cally..    */.  
1331b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1331c 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f 6e 6c 79  _OK && !readOnly
1331d 20 29 7b 0a 20 20 20 20 20 20 73 65 74 53 65 63   ){.      setSec
1331e 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b  torSize(pPager);
1331f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51  .      assert(SQ
13320 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
13321 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 54 45 5f 4d  E_SIZE<=SQLITE_M
13322 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
13323 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 69 66 28  SIZE);.      if(
13324 20 73 7a 50 61 67 65 44 66 6c 74 3c 70 50 61 67   szPageDflt<pPag
13325 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 29  er->sectorSize )
13326 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  {.        if( pP
13327 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
13328 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41  >SQLITE_MAX_DEFA
13329 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 29 7b  ULT_PAGE_SIZE ){
1332a 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 67  .          szPag
1332b 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 4d  eDflt = SQLITE_M
1332c 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
1332d 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d 65  SIZE;.        }e
1332e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73  lse{.          s
1332f 7a 50 61 67 65 44 66 6c 74 20 3d 20 28 75 31 36  zPageDflt = (u16
13330 29 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53  )pPager->sectorS
13331 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ize;.        }. 
13332 20 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51       }.#ifdef SQ
13333 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d  LITE_ENABLE_ATOM
13334 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20 7b  IC_WRITE.      {
13335 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 63  .        int iDc
13336 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69   = sqlite3OsDevi
13337 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
13338 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20  s(pPager->fd);. 
13339 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20         int ii;. 
1333a 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51         assert(SQ
1333b 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
1333c 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b  C512==(512>>8));
1333d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1333e 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
1333f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e  MIC64K==(65536>>
13340 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  8));.        ass
13341 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44  ert(SQLITE_MAX_D
13342 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
13343 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20  <=65536);.      
13344 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 65 44    for(ii=szPageD
13345 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45 5f  flt; ii<=SQLITE_
13346 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  MAX_DEFAULT_PAGE
13347 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29 7b  _SIZE; ii=ii*2){
13348 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69  .          if( i
13349 44 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50  Dc&(SQLITE_IOCAP
1334a 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e 38 29 29  _ATOMIC|(ii>>8))
1334b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1334c 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 69 69 3b  szPageDflt = ii;
1334d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1334e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23       }.      }.#
1334f 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 65  endif.    }.  }e
13350 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61  lse{.    /* If a
13351 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
13352 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74  is requested, it
13353 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69   is not opened i
13354 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20  mmediately..    
13355 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  ** In this case 
13356 77 65 20 61 63 63 65 70 74 20 74 68 65 20 64 65  we accept the de
13357 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20  fault page size 
13358 61 6e 64 20 64 65 6c 61 79 20 61 63 74 75 61 6c  and delay actual
13359 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e  ly.    ** openin
1335a 67 20 74 68 65 20 66 69 6c 65 20 75 6e 74 69 6c  g the file until
1335b 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   the first call 
1335c 74 6f 20 4f 73 57 72 69 74 65 28 29 2e 0a 20 20  to OsWrite()..  
1335d 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73    **.    ** This
1335e 20 62 72 61 6e 63 68 20 69 73 20 61 6c 73 6f 20   branch is also 
1335f 72 75 6e 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65  run for an in-me
13360 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 20 41  mory database. A
13361 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 20  n in-memory.    
13362 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 74  ** database is t
13363 68 65 20 73 61 6d 65 20 61 73 20 61 20 74 65 6d  he same as a tem
13364 70 2d 66 69 6c 65 20 74 68 61 74 20 69 73 20 6e  p-file that is n
13365 65 76 65 72 20 77 72 69 74 74 65 6e 20 6f 75 74  ever written out
13366 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 6b 20   to.    ** disk 
13367 61 6e 64 20 75 73 65 73 20 61 6e 20 69 6e 2d 6d  and uses an in-m
13368 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a  emory rollback j
13369 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a  ournal..    */ .
1336a 20 20 20 20 74 65 6d 70 46 69 6c 65 20 3d 20 31      tempFile = 1
1336b 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  ;.    pPager->st
1336c 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c  ate = PAGER_EXCL
1336d 55 53 49 56 45 3b 0a 20 20 20 20 72 65 61 64 4f  USIVE;.    readO
1336e 6e 6c 79 20 3d 20 28 76 66 73 46 6c 61 67 73 26  nly = (vfsFlags&
1336f 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
13370 4f 4e 4c 59 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ONLY);.  }..  /*
13371 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
13372 61 6c 6c 20 74 6f 20 50 61 67 65 72 53 65 74 50  all to PagerSetP
13373 61 67 65 73 69 7a 65 28 29 20 73 65 72 76 65 73  agesize() serves
13374 20 74 6f 20 73 65 74 20 74 68 65 20 76 61 6c 75   to set the valu
13375 65 20 6f 66 20 0a 20 20 2a 2a 20 50 61 67 65 72  e of .  ** Pager
13376 2e 70 61 67 65 53 69 7a 65 20 61 6e 64 20 74 6f  .pageSize and to
13377 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 50 61   allocate the Pa
13378 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75  ger.pTmpSpace bu
13379 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ffer..  */.  if(
1337a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1337b 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
1337c 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 29  ager->memDb==0 )
1337d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1337e 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69  e3PagerSetPagesi
1337f 7a 65 28 70 50 61 67 65 72 2c 20 26 73 7a 50 61  ze(pPager, &szPa
13380 67 65 44 66 6c 74 2c 20 2d 31 29 3b 0a 20 20 20  geDflt, -1);.   
13381 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53   testcase( rc!=S
13382 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a  QLITE_OK );.  }.
13383 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f  .  /* If an erro
13384 72 20 6f 63 63 75 72 72 65 64 20 69 6e 20 65 69  r occurred in ei
13385 74 68 65 72 20 6f 66 20 74 68 65 20 62 6c 6f 63  ther of the bloc
13386 6b 73 20 61 62 6f 76 65 2c 20 66 72 65 65 20 74  ks above, free t
13387 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 20 73  he .  ** Pager s
13388 74 72 75 63 74 75 72 65 20 61 6e 64 20 63 6c 6f  tructure and clo
13389 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a  se the file..  *
1338a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  /.  if( rc!=SQLI
1338b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73  TE_OK ){.    ass
1338c 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 70 54  ert( !pPager->pT
1338d 6d 70 53 70 61 63 65 20 29 3b 0a 20 20 20 20 73  mpSpace );.    s
1338e 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
1338f 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 73  ager->fd);.    s
13390 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67  qlite3_free(pPag
13391 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  er);.    return 
13392 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e  rc;.  }..  /* In
13393 69 74 69 61 6c 69 7a 65 20 74 68 65 20 50 43 61  itialize the PCa
13394 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20  che object. */. 
13395 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61 3c   assert( nExtra<
13396 31 30 30 30 20 29 3b 0a 20 20 6e 45 78 74 72 61  1000 );.  nExtra
13397 20 3d 20 52 4f 55 4e 44 38 28 6e 45 78 74 72 61   = ROUND8(nExtra
13398 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  );.  sqlite3Pcac
13399 68 65 4f 70 65 6e 28 73 7a 50 61 67 65 44 66 6c  heOpen(szPageDfl
1339a 74 2c 20 6e 45 78 74 72 61 2c 20 21 6d 65 6d 44  t, nExtra, !memD
1339b 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
1339c 20 20 20 20 20 20 20 21 6d 65 6d 44 62 3f 70 61         !memDb?pa
1339d 67 65 72 53 74 72 65 73 73 3a 30 2c 20 28 76 6f  gerStress:0, (vo
1339e 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70 50 61  id *)pPager, pPa
1339f 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a  ger->pPCache);..
133a0 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4f    PAGERTRACE(("O
133a1 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c 20 46 49  PEN %d %s\n", FI
133a2 4c 45 48 41 4e 44 4c 45 49 44 28 70 50 61 67 65  LEHANDLEID(pPage
133a3 72 2d 3e 66 64 29 2c 20 70 50 61 67 65 72 2d 3e  r->fd), pPager->
133a4 7a 46 69 6c 65 6e 61 6d 65 29 29 3b 0a 20 20 49  zFilename));.  I
133a5 4f 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 70  OTRACE(("OPEN %p
133a6 20 25 73 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %s\n", pPager, 
133a7 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
133a8 65 29 29 0a 0a 20 20 70 50 61 67 65 72 2d 3e 75  e))..  pPager->u
133a9 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29  seJournal = (u8)
133aa 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70 50  useJournal;.  pP
133ab 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b  ager->noReadlock
133ac 20 3d 20 28 6e 6f 52 65 61 64 6c 6f 63 6b 20 26   = (noReadlock &
133ad 26 20 72 65 61 64 4f 6e 6c 79 29 20 3f 31 3a 30  & readOnly) ?1:0
133ae 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73  ;.  /* pPager->s
133af 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a  tmtOpen = 0; */.
133b0 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d    /* pPager->stm
133b1 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f 0a 20  tInUse = 0; */. 
133b2 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52 65 66   /* pPager->nRef
133b3 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65   = 0; */.  pPage
133b4 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d  r->dbSizeValid =
133b5 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 2f 2a   (u8)memDb;.  /*
133b6 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a   pPager->stmtSiz
133b7 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70  e = 0; */.  /* p
133b8 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65  Pager->stmtJSize
133b9 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50   = 0; */.  /* pP
133ba 61 67 65 72 2d 3e 6e 50 61 67 65 20 3d 20 30 3b  ager->nPage = 0;
133bb 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78   */.  pPager->mx
133bc 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41  Pgno = SQLITE_MA
133bd 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20  X_PAGE_COUNT;.  
133be 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  /* pPager->state
133bf 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b   = PAGER_UNLOCK;
133c0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
133c1 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28  ager->state == (
133c2 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 52  tempFile ? PAGER
133c3 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 47  _EXCLUSIVE : PAG
133c4 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20  ER_UNLOCK) );.  
133c5 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 4d 61  /* pPager->errMa
133c6 73 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  sk = 0; */.  pPa
133c7 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3d 20  ger->tempFile = 
133c8 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 0a 20 20  (u8)tempFile;.  
133c9 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 6c 65  assert( tempFile
133ca 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
133cb 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20 20  ODE_NORMAL .    
133cc 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 69 6c        || tempFil
133cd 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
133ce 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 29  MODE_EXCLUSIVE )
133cf 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45  ;.  assert( PAGE
133d0 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58  R_LOCKINGMODE_EX
133d1 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a 20 20  CLUSIVE==1 );.  
133d2 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
133d3 65 4d 6f 64 65 20 3d 20 28 75 38 29 74 65 6d 70  eMode = (u8)temp
133d4 46 69 6c 65 3b 20 0a 20 20 70 50 61 67 65 72 2d  File; .  pPager-
133d5 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65  >changeCountDone
133d6 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46   = pPager->tempF
133d7 69 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6d  ile;.  pPager->m
133d8 65 6d 44 62 20 3d 20 28 75 38 29 6d 65 6d 44 62  emDb = (u8)memDb
133d9 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65 61 64  ;.  pPager->read
133da 4f 6e 6c 79 20 3d 20 28 75 38 29 72 65 61 64 4f  Only = (u8)readO
133db 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72  nly;.  /* pPager
133dc 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 20  ->needSync = 0; 
133dd 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 73 65  */.  assert( use
133de 4a 6f 75 72 6e 61 6c 20 7c 7c 20 70 50 61 67 65  Journal || pPage
133df 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20  r->tempFile );. 
133e0 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20   pPager->noSync 
133e1 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  = pPager->tempFi
133e2 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 75  le;.  pPager->fu
133e3 6c 6c 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d  llSync = pPager-
133e4 3e 6e 6f 53 79 6e 63 20 3f 30 3a 31 3b 0a 20 20  >noSync ?0:1;.  
133e5 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61  pPager->sync_fla
133e6 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43  gs = SQLITE_SYNC
133e7 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50  _NORMAL;.  /* pP
133e8 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 30  ager->pFirst = 0
133e9 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72  ; */.  /* pPager
133ea 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d  ->pFirstSynced =
133eb 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
133ec 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a  er->pLast = 0; *
133ed 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78 74  /.  pPager->nExt
133ee 72 61 20 3d 20 28 75 31 36 29 6e 45 78 74 72 61  ra = (u16)nExtra
133ef 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  ;.  pPager->jour
133f0 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53  nalSizeLimit = S
133f1 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f  QLITE_DEFAULT_JO
133f2 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54  URNAL_SIZE_LIMIT
133f3 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  ;.  assert( isOp
133f4 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c  en(pPager->fd) |
133f5 7c 20 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20  | tempFile );.  
133f6 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50  setSectorSize(pP
133f7 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21 75 73  ager);.  if( !us
133f8 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  eJournal ){.    
133f9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
133fa 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52  ode = PAGER_JOUR
133fb 4e 41 4c 4d 4f 44 45 5f 4f 46 46 3b 0a 20 20 7d  NALMODE_OFF;.  }
133fc 65 6c 73 65 20 69 66 28 20 6d 65 6d 44 62 20 29  else if( memDb )
133fd 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
133fe 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45  urnalMode = PAGE
133ff 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
13400 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70  MORY;.  }.  /* p
13401 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64  Pager->xBusyHand
13402 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a  ler = 0; */.  /*
13403 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61   pPager->pBusyHa
13404 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f  ndlerArg = 0; */
13405 0a 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e  .  pPager->xRein
13406 69 74 65 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a  iter = xReinit;.
13407 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50 61 67    /* memset(pPag
13408 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20 73 69  er->aHash, 0, si
13409 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61 48 61  zeof(pPager->aHa
1340a 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 70 50 61  sh)); */.  *ppPa
1340b 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 20 20  ger = pPager;.  
1340c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1340d 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ;.}..../*.** Thi
1340e 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
1340f 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61 6e 73  lled after trans
13410 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41  itioning from PA
13411 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a  GER_UNLOCK to.**
13412 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 74   PAGER_SHARED st
13413 61 74 65 2e 20 49 74 20 74 65 73 74 73 20 69 66  ate. It tests if
13414 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20   there is a hot 
13415 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e 74 20  journal present 
13416 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73  in.** the file-s
13417 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 67 69  ystem for the gi
13418 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68 6f 74  ven pager. A hot
13419 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20   journal is one 
1341a 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74  that .** needs t
1341b 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b  o be played back
1341c 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  . According to t
1341d 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20  his function, a 
1341e 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66  hot-journal.** f
1341f 69 6c 65 20 65 78 69 73 74 73 20 69 66 20 74 68  ile exists if th
13420 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74  e following crit
13421 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a  eria are met:.**
13422 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f 75 72  .**   * The jour
13423 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20  nal file exists 
13424 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79 73 74  in the file syst
13425 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e  em, and.**   * N
13426 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  o process holds 
13427 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 67 72  a RESERVED or gr
13428 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68  eater lock on th
13429 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
1342a 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20   and.**   * The 
1342b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 74  database file it
1342c 73 65 6c 66 20 69 73 20 67 72 65 61 74 65 72 20  self is greater 
1342d 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 6e 20  than 0 bytes in 
1342e 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a  size, and.**   *
1342f 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20   The first byte 
13430 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
13431 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 69  ile exists and i
13432 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a  s not 0x00..**.*
13433 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74  * If the current
13434 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
13435 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 30 20  abase file is 0 
13436 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  but a journal fi
13437 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68  le.** exists, th
13438 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 20 61  at is probably a
13439 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65  n old journal le
1343a 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 20 70  ft over from a p
1343b 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 73 65  rior.** database
1343c 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e   with the same n
1343d 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ame. In this cas
1343e 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
1343f 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65  le is.** just de
13440 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73 44 65  leted using OsDe
13441 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73 20 69  lete, *pExists i
13442 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53  s set to 0 and S
13443 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72  QLITE_OK.** is r
13444 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
13445 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  his routine does
13446 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20 74 68   not check if th
13447 65 72 65 20 69 73 20 61 20 6d 61 73 74 65 72 20  ere is a master 
13448 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  journal filename
13449 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f  .** at the end o
1344a 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 74  f the file. If t
1344b 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74 68 61  here is, and tha
1344c 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  t master journal
1344d 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f   file.** does no
1344e 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 74 68  t exist, then th
1344f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
13450 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74  s not really hot
13451 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73  . In this.** cas
13452 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  e this routine w
13453 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66 61 6c  ill return a fal
13454 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54 68 65  se-positive. The
13455 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
13456 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c  ).** routine wil
13457 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 74 20  l discover that 
13458 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
13459 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68   is not really h
1345a 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20  ot and .** will 
1345b 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b  not roll it back
1345c 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f  . .**.** If a ho
1345d 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  t-journal file i
1345e 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 73 74  s found to exist
1345f 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65  , *pExists is se
13460 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53  t to 1 and .** S
13461 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
13462 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75  d. If no hot-jou
13463 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65  rnal file is pre
13464 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 20 69  sent, *pExists i
13465 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 61 6e  s.** set to 0 an
13466 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  d SQLITE_OK retu
13467 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65  rned. If an IO e
13468 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
13469 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64  e trying.** to d
1346a 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
1346b 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f   or not a hot-jo
1346c 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74  urnal file exist
1346d 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 0a  s, the IO error.
1346e 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  ** code is retur
1346f 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 6c 75  ned and the valu
13470 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20 69 73  e of *pExists is
13471 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73   undefined..*/.s
13472 74 61 74 69 63 20 69 6e 74 20 68 61 73 48 6f 74  tatic int hasHot
13473 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70  Journal(Pager *p
13474 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45 78 69  Pager, int *pExi
13475 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  sts){.  sqlite3_
13476 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73  vfs * const pVfs
13477 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b   = pPager->pVfs;
13478 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
13479 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1347a 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
1347b 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74 73 3b  */.  int exists;
1347c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1347d 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20     /* True if a 
1347e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
1347f 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 61 73  present */..  as
13480 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20  sert( pPager!=0 
13481 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
13482 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
13483 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  );.  assert( isO
13484 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
13485 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73  );.  assert( !is
13486 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
13487 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
13488 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3c 3d 20  Pager->state <= 
13489 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a  PAGER_SHARED );.
1348a 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 3b  .  *pExists = 0;
1348b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
1348c 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 70 50  sAccess(pVfs, pP
1348d 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  ager->zJournal, 
1348e 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
1348f 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a  ISTS, &exists);.
13490 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13491 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 29 7b  _OK && exists ){
13492 0a 20 20 20 20 69 6e 74 20 6c 6f 63 6b 65 64 3b  .    int locked;
13493 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13494 20 2f 2a 20 54 72 75 65 20 69 66 20 73 6f 6d 65   /* True if some
13495 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61   process holds a
13496 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a   RESERVED lock *
13497 2f 0a 0a 20 20 20 20 2f 2a 20 52 61 63 65 20 63  /..    /* Race c
13498 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 3a 20 20  ondition here:  
13499 41 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  Another process 
1349a 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20  might have been 
1349b 68 6f 6c 64 69 6e 67 20 74 68 65 0a 20 20 20 20  holding the.    
1349c 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45 44 20  ** the RESERVED 
1349d 6c 6f 63 6b 20 61 6e 64 20 68 61 76 65 20 61 20  lock and have a 
1349e 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 20 61 74 20  journal open at 
1349f 74 68 65 20 73 71 6c 69 74 65 33 4f 73 41 63 63  the sqlite3OsAcc
134a0 65 73 73 28 29 20 0a 20 20 20 20 2a 2a 20 63 61  ess() .    ** ca
134a1 6c 6c 20 61 62 6f 76 65 2c 20 62 75 74 20 74 68  ll above, but th
134a2 65 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f  en delete the jo
134a3 75 72 6e 61 6c 20 61 6e 64 20 64 72 6f 70 20 74  urnal and drop t
134a4 68 65 20 6c 6f 63 6b 20 62 65 66 6f 72 65 0a 20  he lock before. 
134a5 20 20 20 2a 2a 20 77 65 20 67 65 74 20 74 6f 20     ** we get to 
134a6 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71  the following sq
134a7 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65  lite3OsCheckRese
134a8 72 76 65 64 4c 6f 63 6b 28 29 20 63 61 6c 6c 2e  rvedLock() call.
134a9 20 20 49 66 20 74 68 61 74 0a 20 20 20 20 2a 2a    If that.    **
134aa 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
134ab 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74  is routine might
134ac 20 74 68 69 6e 6b 20 74 68 65 72 65 20 69 73 20   think there is 
134ad 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 77 68  a hot journal wh
134ae 65 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 66 61 63  en.    ** in fac
134af 74 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65 2e  t there is none.
134b0 20 20 54 68 69 73 20 72 65 73 75 6c 74 73 20 69    This results i
134b1 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 74 69  n a false-positi
134b2 76 65 20 77 68 69 63 68 20 77 69 6c 6c 0a 20 20  ve which will.  
134b3 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 69    ** be dealt wi
134b4 74 68 20 62 79 20 74 68 65 20 70 6c 61 79 62 61  th by the playba
134b5 63 6b 20 72 6f 75 74 69 6e 65 2e 20 20 54 69 63  ck routine.  Tic
134b6 6b 65 74 20 23 33 38 38 33 2e 0a 20 20 20 20 2a  ket #3883..    *
134b7 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
134b8 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65  e3OsCheckReserve
134b9 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64  dLock(pPager->fd
134ba 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 20 20  , &locked);.    
134bb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
134bc 4b 20 26 26 20 21 6c 6f 63 6b 65 64 20 29 7b 0a  K && !locked ){.
134bd 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b        int nPage;
134be 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b  ..      /* Check
134bf 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
134c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
134c1 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73 20 6f  If it consists o
134c2 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20 20 20  f 0 pages,.     
134c3 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74 65 20   ** then delete 
134c4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
134c5 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72  . See the header
134c6 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 66   comment above f
134c7 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  or .      ** the
134c8 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72 65 2e   reasoning here.
134c9 20 20 44 65 6c 65 74 65 20 74 68 65 20 6f 62 73    Delete the obs
134ca 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69  olete journal fi
134cb 6c 65 20 75 6e 64 65 72 0a 20 20 20 20 20 20 2a  le under.      *
134cc 2a 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  * a RESERVED loc
134cd 6b 20 74 6f 20 61 76 6f 69 64 20 72 61 63 65 20  k to avoid race 
134ce 63 6f 6e 64 69 74 69 6f 6e 73 20 61 6e 64 20 74  conditions and t
134cf 6f 20 61 76 6f 69 64 20 76 69 6f 6c 61 74 69 6e  o avoid violatin
134d0 67 0a 20 20 20 20 20 20 2a 2a 20 5b 48 33 33 30  g.      ** [H330
134d1 32 30 5d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  20]..      */.  
134d2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
134d3 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  PagerPagecount(p
134d4 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a  Pager, &nPage);.
134d5 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
134d6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
134d7 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 20     if( nPage==0 
134d8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
134d9 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
134da 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20  alloc();.       
134db 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 73     if( sqlite3Os
134dc 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c  Lock(pPager->fd,
134dd 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3d   RESERVED_LOCK)=
134de 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
134df 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
134e0 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20  3OsDelete(pVfs, 
134e1 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
134e2 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
134e3 20 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63    sqlite3OsUnloc
134e4 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 48  k(pPager->fd, SH
134e5 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  ARED_LOCK);.    
134e6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
134e7 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
134e8 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
134e9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
134ea 20 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72       /* The jour
134eb 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20  nal file exists 
134ec 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e  and no other con
134ed 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 20 72 65  nection has a re
134ee 73 65 72 76 65 64 0a 20 20 20 20 20 20 20 20 20  served.         
134ef 20 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 20 6c   ** or greater l
134f0 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
134f1 61 73 65 20 66 69 6c 65 2e 20 4e 6f 77 20 63 68  ase file. Now ch
134f2 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 69  eck that there i
134f3 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  s.          ** a
134f4 74 20 6c 65 61 73 74 20 6f 6e 65 20 6e 6f 6e 2d  t least one non-
134f5 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68  zero bytes at th
134f6 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a  e start of the j
134f7 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20  ournal file..   
134f8 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65         ** If the
134f9 72 65 20 69 73 2c 20 74 68 65 6e 20 77 65 20 63  re is, then we c
134fa 6f 6e 73 69 64 65 72 20 74 68 69 73 20 6a 6f 75  onsider this jou
134fb 72 6e 61 6c 20 74 6f 20 62 65 20 68 6f 74 2e 20  rnal to be hot. 
134fc 49 66 20 6e 6f 74 2c 20 0a 20 20 20 20 20 20 20  If not, .       
134fd 20 20 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20     ** it can be 
134fe 69 67 6e 6f 72 65 64 2e 0a 20 20 20 20 20 20 20  ignored..       
134ff 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
13500 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f  int f = SQLITE_O
13501 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c  PEN_READONLY|SQL
13502 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
13503 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20  URNAL;.         
13504 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
13505 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72  pen(pVfs, pPager
13506 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67  ->zJournal, pPag
13507 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 29 3b  er->jfd, f, &f);
13508 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
13509 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1350a 20 20 20 20 20 20 20 20 20 20 20 20 75 38 20 66              u8 f
1350b 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  irst = 0;.      
1350c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1350d 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d  e3OsRead(pPager-
1350e 3e 6a 66 64 2c 20 28 76 6f 69 64 20 2a 29 26 66  >jfd, (void *)&f
1350f 69 72 73 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20  irst, 1, 0);.   
13510 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d           if( rc=
13511 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  =SQLITE_IOERR_SH
13512 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20  ORT_READ ){.    
13513 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
13514 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
13515 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13516 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
13517 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b  se(pPager->jfd);
13518 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 45  .            *pE
13519 78 69 73 74 73 20 3d 20 28 66 69 72 73 74 21 3d  xists = (first!=
1351a 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  0);.          }e
1351b 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
1351c 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29 7b 0a 20  TE_CANTOPEN ){. 
1351d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
1351e 20 77 65 20 63 61 6e 6e 6f 74 20 6f 70 65 6e 20   we cannot open 
1351f 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
13520 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 6f 72 64  rnal file in ord
13521 65 72 20 74 6f 20 73 65 65 20 69 66 0a 20 20 20  er to see if.   
13522 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 73 20           ** its 
13523 68 61 73 20 61 20 7a 65 72 6f 20 68 65 61 64 65  has a zero heade
13524 72 2c 20 74 68 61 74 20 6d 69 67 68 74 20 62 65  r, that might be
13525 20 64 75 65 20 74 6f 20 61 6e 20 49 2f 4f 20 65   due to an I/O e
13526 72 72 6f 72 2c 20 6f 72 0a 20 20 20 20 20 20 20  rror, or.       
13527 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 67 68 74       ** it might
13528 20 62 65 20 64 75 65 20 74 6f 20 74 68 65 20 72   be due to the r
13529 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65  ace condition de
1352a 73 63 72 69 62 65 64 20 61 62 6f 76 65 20 61 6e  scribed above an
1352b 64 20 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20  d in.           
1352c 20 2a 2a 20 74 69 63 6b 65 74 20 23 33 38 38 33   ** ticket #3883
1352d 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 61  .  Either way, a
1352e 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 6a  ssume that the j
1352f 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e 0a 20  ournal is hot.. 
13530 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 68             ** Th
13531 69 73 20 6d 69 67 68 74 20 62 65 20 61 20 66 61  is might be a fa
13532 6c 73 65 20 70 6f 73 69 74 69 76 65 2e 20 20 42  lse positive.  B
13533 75 74 20 69 66 20 69 74 20 69 73 2c 20 74 68 65  ut if it is, the
13534 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  n the.          
13535 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 20 6a    ** automatic j
13536 6f 75 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 20  ournal playback 
13537 61 6e 64 20 72 65 63 6f 76 65 72 79 20 6d 65 63  and recovery mec
13538 68 61 6e 69 73 6d 20 77 69 6c 6c 20 64 65 61 6c  hanism will deal
13539 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
1353a 77 69 74 68 20 69 74 20 75 6e 64 65 72 20 61 6e  with it under an
1353b 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
1353c 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74 20  where we do not 
1353d 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20  need to.        
1353e 20 20 20 20 2a 2a 20 77 6f 72 72 79 20 73 6f 20      ** worry so 
1353f 6d 75 63 68 20 77 69 74 68 20 72 61 63 65 20 63  much with race c
13540 6f 6e 64 69 74 69 6f 6e 73 2e 0a 20 20 20 20 20  onditions..     
13541 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
13542 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 20 3d        *pExists =
13543 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
13544 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
13545 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
13546 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
13547 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
13548 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
13549 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ead the content 
1354a 66 6f 72 20 70 61 67 65 20 70 50 67 20 6f 75 74  for page pPg out
1354b 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1354c 20 66 69 6c 65 20 61 6e 64 20 69 6e 74 6f 20 0a   file and into .
1354d 2a 2a 20 70 50 67 2d 3e 70 44 61 74 61 2e 20 41  ** pPg->pData. A
1354e 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 72 20   shared lock or 
1354f 67 72 65 61 74 65 72 20 6d 75 73 74 20 62 65 20  greater must be 
13550 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61  held on the data
13551 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 62 65 66  base.** file bef
13552 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ore this functio
13553 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  n is called..**.
13554 2a 2a 20 49 66 20 70 61 67 65 20 31 20 69 73 20  ** If page 1 is 
13555 72 65 61 64 2c 20 74 68 65 6e 20 74 68 65 20 76  read, then the v
13556 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62  alue of Pager.db
13557 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 73 65  FileVers[] is se
13558 74 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61 6c 75  t to.** the valu
13559 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  e read from the 
1355a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
1355b 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72  *.** If an IO er
1355c 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e  ror occurs, then
1355d 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 69 73   the IO error is
1355e 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
1355f 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 4f 74 68 65   caller..** Othe
13560 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b  rwise, SQLITE_OK
13561 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
13562 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64  .static int read
13563 44 62 50 61 67 65 28 50 67 48 64 72 20 2a 70 50  DbPage(PgHdr *pP
13564 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61  g){.  Pager *pPa
13565 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65  ger = pPg->pPage
13566 72 3b 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65  r; /* Pager obje
13567 63 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ct associated wi
13568 74 68 20 70 61 67 65 20 70 50 67 20 2a 2f 0a 20  th page pPg */. 
13569 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67   Pgno pgno = pPg
1356a 2d 3e 70 67 6e 6f 3b 20 20 20 20 20 20 20 2f 2a  ->pgno;       /*
1356b 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20   Page number to 
1356c 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63  read */.  int rc
1356d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1356e 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
1356f 20 63 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20 69   code */.  i64 i
13570 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
13571 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20          /* Byte 
13572 6f 66 66 73 65 74 20 6f 66 20 66 69 6c 65 20 74  offset of file t
13573 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 0a  o read from */..
13574 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13575 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53  ->state>=PAGER_S
13576 48 41 52 45 44 20 26 26 20 21 4d 45 4d 44 42 20  HARED && !MEMDB 
13577 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  );.  assert( isO
13578 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
13579 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28  );..  if( NEVER(
1357a 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
1357b 66 64 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65  fd)) ){.    asse
1357c 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70  rt( pPager->temp
1357d 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73  File );.    mems
1357e 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 30  et(pPg->pData, 0
1357f 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
13580 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ze);.    return 
13581 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
13582 20 69 4f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f   iOffset = (pgno
13583 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d  -1)*(i64)pPager-
13584 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 63 20  >pageSize;.  rc 
13585 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
13586 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d  pPager->fd, pPg-
13587 3e 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e  >pData, pPager->
13588 70 61 67 65 53 69 7a 65 2c 20 69 4f 66 66 73 65  pageSize, iOffse
13589 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
1358a 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
1358b 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 72 63 20  _READ ){.    rc 
1358c 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
1358d 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29  .  if( pgno==1 )
1358e 7b 0a 20 20 20 20 75 38 20 2a 64 62 46 69 6c 65  {.    u8 *dbFile
1358f 56 65 72 73 20 3d 20 26 28 28 75 38 2a 29 70 50  Vers = &((u8*)pP
13590 67 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b 0a 20  g->pData)[24];. 
13591 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65     memcpy(&pPage
13592 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64  r->dbFileVers, d
13593 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f  bFileVers, sizeo
13594 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  f(pPager->dbFile
13595 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 43 4f  Vers));.  }.  CO
13596 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 67  DEC1(pPager, pPg
13597 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33  ->pData, pgno, 3
13598 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  , rc = SQLITE_NO
13599 4d 45 4d 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49  MEM);..  PAGER_I
1359a 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65  NCR(sqlite3_page
1359b 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b  r_readdb_count);
1359c 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50  .  PAGER_INCR(pP
1359d 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20  ager->nRead);.  
1359e 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e 20 25  IOTRACE(("PGIN %
1359f 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  p %d\n", pPager,
135a0 20 70 67 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52   pgno));.  PAGER
135a1 54 52 41 43 45 28 28 22 46 45 54 43 48 20 25 64  TRACE(("FETCH %d
135a2 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30   page %d hash(%0
135a3 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  8x)\n",.        
135a4 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70         PAGERID(p
135a5 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61  Pager), pgno, pa
135a6 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67  ger_pagehash(pPg
135a7 29 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72  )));..  return r
135a8 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
135a9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
135aa 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 20  led to obtain a 
135ab 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74  shared lock on t
135ac 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
135ad 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67  ..** It is illeg
135ae 61 6c 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74  al to call sqlit
135af 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 29  e3PagerAcquire()
135b0 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 69   until after thi
135b1 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 68 61  s function.** ha
135b2 73 20 62 65 65 6e 20 73 75 63 63 65 73 73 66 75  s been successfu
135b3 6c 6c 79 20 63 61 6c 6c 65 64 2e 20 49 66 20 61  lly called. If a
135b4 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20   shared-lock is 
135b5 61 6c 72 65 61 64 79 20 68 65 6c 64 20 77 68 65  already held whe
135b6 6e 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69  n.** this functi
135b7 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74  on is called, it
135b8 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
135b9 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
135ba 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20   operations are 
135bb 61 6c 73 6f 20 70 65 72 66 6f 72 6d 65 64 20 62  also performed b
135bc 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  y this function.
135bd 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66 20 74  .**.**   1) If t
135be 68 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72  he pager is curr
135bf 65 6e 74 6c 79 20 69 6e 20 50 41 47 45 52 5f 55  ently in PAGER_U
135c0 4e 4c 4f 43 4b 20 73 74 61 74 65 20 28 6e 6f 20  NLOCK state (no 
135c1 6c 6f 63 6b 20 68 65 6c 64 0a 2a 2a 20 20 20 20  lock held.**    
135c2 20 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73    on the databas
135c3 65 20 66 69 6c 65 29 2c 20 74 68 65 6e 20 61 6e  e file), then an
135c4 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
135c5 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a 2a 20   to obtain a.** 
135c6 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f 63 6b       SHARED lock
135c7 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
135c8 20 66 69 6c 65 2e 20 49 6d 6d 65 64 69 61 74 65   file. Immediate
135c9 6c 79 20 61 66 74 65 72 20 6f 62 74 61 69 6e 69  ly after obtaini
135ca 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 53  ng.**      the S
135cb 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20  HARED lock, the 
135cc 66 69 6c 65 2d 73 79 73 74 65 6d 20 69 73 20 63  file-system is c
135cd 68 65 63 6b 65 64 20 66 6f 72 20 61 20 68 6f 74  hecked for a hot
135ce 2d 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a 20 20 20 20  -journal,.**    
135cf 20 20 77 68 69 63 68 20 69 73 20 70 6c 61 79 65    which is playe
135d0 64 20 62 61 63 6b 20 69 66 20 70 72 65 73 65 6e  d back if presen
135d1 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79  t. Following any
135d2 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 0a 2a 2a   hot-journal .**
135d3 20 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b 2c 20        rollback, 
135d4 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
135d5 74 68 65 20 63 61 63 68 65 20 61 72 65 20 76 61  the cache are va
135d6 6c 69 64 61 74 65 64 20 62 79 20 63 68 65 63 6b  lidated by check
135d7 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20  ing.**      the 
135d8 27 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 27  'change-counter'
135d9 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 64 61   field of the da
135da 74 61 62 61 73 65 20 66 69 6c 65 20 68 65 61 64  tabase file head
135db 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 64  er and.**      d
135dc 69 73 63 61 72 64 65 64 20 69 66 20 74 68 65 79  iscarded if they
135dd 20 61 72 65 20 66 6f 75 6e 64 20 74 6f 20 62 65   are found to be
135de 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20   invalid..**.** 
135df 20 20 32 29 20 49 66 20 74 68 65 20 70 61 67 65    2) If the page
135e0 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20  r is running in 
135e1 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 2c 20  exclusive-mode, 
135e2 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 63 75  and there are cu
135e3 72 72 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20  rrently.**      
135e4 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72  no outstanding r
135e5 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79  eferences to any
135e6 20 70 61 67 65 73 2c 20 61 6e 64 20 69 73 20 69   pages, and is i
135e7 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  n the error stat
135e8 65 2c 0a 2a 2a 20 20 20 20 20 20 74 68 65 6e 20  e,.**      then 
135e9 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  an attempt is ma
135ea 64 65 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20  de to clear the 
135eb 65 72 72 6f 72 20 73 74 61 74 65 20 62 79 20 64  error state by d
135ec 69 73 63 61 72 64 69 6e 67 0a 2a 2a 20 20 20 20  iscarding.**    
135ed 20 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f    the contents o
135ee 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  f the page cache
135ef 20 61 6e 64 20 72 6f 6c 6c 69 6e 67 20 62 61 63   and rolling bac
135f0 6b 20 61 6e 79 20 6f 70 65 6e 20 6a 6f 75 72 6e  k any open journ
135f1 61 6c 0a 2a 2a 20 20 20 20 20 20 66 69 6c 65 2e  al.**      file.
135f2 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70  .**.** If the op
135f3 65 72 61 74 69 6f 6e 20 64 65 73 63 72 69 62 65  eration describe
135f4 64 20 62 79 20 28 32 29 20 61 62 6f 76 65 20 69  d by (2) above i
135f5 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 65 64 2c  s not attempted,
135f6 20 61 6e 64 20 69 66 20 74 68 65 0a 2a 2a 20 70   and if the.** p
135f7 61 67 65 72 20 69 73 20 69 6e 20 61 6e 20 65 72  ager is in an er
135f8 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 20  ror state other 
135f9 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  than SQLITE_FULL
135fa 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 61   when this is ca
135fb 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72  lled,.** the err
135fc 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 63  or state error c
135fd 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
135fe 20 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 64   It is permitted
135ff 20 74 6f 20 72 65 61 64 20 74 68 65 0a 2a 2a 20   to read the.** 
13600 64 61 74 61 62 61 73 65 20 77 68 65 6e 20 69 6e  database when in
13601 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 72 72   SQLITE_FULL err
13602 6f 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  or state..**.** 
13603 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 65 76  Otherwise, if ev
13604 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63  erything is succ
13605 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f  essful, SQLITE_O
13606 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  K is returned. I
13607 66 20 61 6e 0a 2a 2a 20 49 4f 20 65 72 72 6f 72  f an.** IO error
13608 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6c 6f   occurs while lo
13609 63 6b 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  cking the databa
1360a 73 65 2c 20 63 68 65 63 6b 69 6e 67 20 66 6f 72  se, checking for
1360b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a   a hot-journal.*
1360c 2a 20 66 69 6c 65 20 6f 72 20 72 6f 6c 6c 69 6e  * file or rollin
1360d 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c  g back a journal
1360e 20 66 69 6c 65 2c 20 74 68 65 20 49 4f 20 65 72   file, the IO er
1360f 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
13610 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
13611 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
13612 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c 6f  te3PagerSharedLo
13613 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ck(Pager *pPager
13614 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
13615 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
13616 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
13617 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  n code */.  int 
13618 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20 30  isErrorReset = 0
13619 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1361a 2a 20 54 72 75 65 20 69 66 20 72 65 63 6f 76 65  * True if recove
1361b 72 69 6e 67 20 66 72 6f 6d 20 65 72 72 6f 72 20  ring from error 
1361c 73 74 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54  state */..  /* T
1361d 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6f  his routine is o
1361e 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20  nly called from 
1361f 62 2d 74 72 65 65 20 61 6e 64 20 6f 6e 6c 79 20  b-tree and only 
13620 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e  when there are n
13621 6f 0a 20 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69  o.  ** outstandi
13622 6e 67 20 70 61 67 65 73 20 2a 2f 0a 20 20 61 73  ng pages */.  as
13623 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61  sert( sqlite3Pca
13624 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67  cheRefCount(pPag
13625 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20  er->pPCache)==0 
13626 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 4d  );.  if( NEVER(M
13627 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e  EMDB && pPager->
13628 65 72 72 43 6f 64 65 29 20 29 7b 20 72 65 74 75  errCode) ){ retu
13629 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  rn pPager->errCo
1362a 64 65 3b 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  de; }..  /* If t
1362b 68 69 73 20 64 61 74 61 62 61 73 65 20 69 73 20  his database is 
1362c 69 6e 20 61 6e 20 65 72 72 6f 72 2d 73 74 61 74  in an error-stat
1362d 65 2c 20 6e 6f 77 20 69 73 20 61 20 63 68 61 6e  e, now is a chan
1362e 63 65 20 74 6f 20 63 6c 65 61 72 0a 20 20 2a 2a  ce to clear.  **
1362f 20 74 68 65 20 65 72 72 6f 72 2e 20 44 69 73 63   the error. Disc
13630 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ard the contents
13631 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61   of the pager-ca
13632 63 68 65 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b  che and rollback
13633 0a 20 20 2a 2a 20 61 6e 79 20 68 6f 74 20 6a 6f  .  ** any hot jo
13634 75 72 6e 61 6c 20 69 6e 20 74 68 65 20 66 69 6c  urnal in the fil
13635 65 2d 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20  e-system..  */. 
13636 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
13637 43 6f 64 65 20 29 7b 0a 20 20 20 20 69 66 28 20  Code ){.    if( 
13638 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
13639 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 7a  fd) || pPager->z
1363a 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20  Journal ){.     
1363b 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20   isErrorReset = 
1363c 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  1;.    }.    pPa
1363d 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53  ger->errCode = S
1363e 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61  QLITE_OK;.    pa
1363f 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
13640 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  );.  }..  if( pP
13641 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
13642 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c 20 69 73 45  ER_UNLOCK || isE
13643 72 72 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20  rrorReset ){.   
13644 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63   sqlite3_vfs * c
13645 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67  onst pVfs = pPag
13646 65 72 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e  er->pVfs;.    in
13647 74 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 3d  t isHotJournal =
13648 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   0;.    assert( 
13649 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73  !MEMDB );.    as
1364a 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61  sert( sqlite3Pca
1364b 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67  cheRefCount(pPag
1364c 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20  er->pPCache)==0 
1364d 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  );.    if( pPage
1364e 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b  r->noReadlock ){
1364f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
13650 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20  Pager->readOnly 
13651 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  );.      pPager-
13652 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53  >state = PAGER_S
13653 48 41 52 45 44 3b 0a 20 20 20 20 7d 65 6c 73 65  HARED;.    }else
13654 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  {.      rc = pag
13655 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28  er_wait_on_lock(
13656 70 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c  pPager, SHARED_L
13657 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  OCK);.      if( 
13658 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
13659 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1365a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d   pPager->state==
1365b 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a  PAGER_UNLOCK );.
1365c 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70          return p
1365d 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
1365e 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a  r, rc);.      }.
1365f 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
13660 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
13661 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
13662 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 6a 6f 75  .    /* If a jou
13663 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73  rnal file exists
13664 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e  , and there is n
13665 6f 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  o RESERVED lock 
13666 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61  on the.    ** da
13667 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
13668 6e 20 69 74 20 65 69 74 68 65 72 20 6e 65 65 64  n it either need
13669 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62  s to be played b
1366a 61 63 6b 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a  ack or deleted..
1366b 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
1366c 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b 0a  isErrorReset ){.
1366d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
1366e 61 67 65 72 2d 3e 73 74 61 74 65 20 3c 3d 20 50  ager->state <= P
1366f 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20  AGER_SHARED );. 
13670 20 20 20 20 20 72 63 20 3d 20 68 61 73 48 6f 74       rc = hasHot
13671 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20  Journal(pPager, 
13672 26 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a  &isHotJournal);.
13673 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
13674 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13675 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a     goto failed;.
13676 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
13677 20 20 69 66 28 20 69 73 45 72 72 6f 72 52 65 73    if( isErrorRes
13678 65 74 20 7c 7c 20 69 73 48 6f 74 4a 6f 75 72 6e  et || isHotJourn
13679 61 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47  al ){.      /* G
1367a 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  et an EXCLUSIVE 
1367b 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
1367c 62 61 73 65 20 66 69 6c 65 2e 20 41 74 20 74 68  base file. At th
1367d 69 73 20 70 6f 69 6e 74 20 69 74 20 69 73 0a 20  is point it is. 
1367e 20 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74 61 6e       ** importan
1367f 74 20 74 68 61 74 20 61 20 52 45 53 45 52 56 45  t that a RESERVE
13680 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 6f 62  D lock is not ob
13681 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 77 61  tained on the wa
13682 79 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a  y to the.      *
13683 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  * EXCLUSIVE lock
13684 2e 20 49 66 20 69 74 20 77 65 72 65 2c 20 61 6e  . If it were, an
13685 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d 69  other process mi
13686 67 68 74 20 6f 70 65 6e 20 74 68 65 0a 20 20 20  ght open the.   
13687 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66     ** database f
13688 69 6c 65 2c 20 64 65 74 65 63 74 20 74 68 65 20  ile, detect the 
13689 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 61  RESERVED lock, a
1368a 6e 64 20 63 6f 6e 63 6c 75 64 65 20 74 68 61 74  nd conclude that
1368b 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61   the.      ** da
1368c 74 61 62 61 73 65 20 69 73 20 73 61 66 65 20 74  tabase is safe t
1368d 6f 20 72 65 61 64 20 77 68 69 6c 65 20 74 68 69  o read while thi
1368e 73 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 69  s process is sti
1368f 6c 6c 20 72 6f 6c 6c 69 6e 67 20 74 68 65 20 0a  ll rolling the .
13690 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75        ** hot-jou
13691 72 6e 61 6c 20 62 61 63 6b 2e 0a 20 20 20 20 20  rnal back..     
13692 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 42 65   ** .      ** Be
13693 63 61 75 73 65 20 74 68 65 20 69 6e 74 65 72 6d  cause the interm
13694 65 64 69 61 74 65 20 52 45 53 45 52 56 45 44 20  ediate RESERVED 
13695 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 71 75  lock is not requ
13696 65 73 74 65 64 2c 20 61 6e 79 0a 20 20 20 20 20  ested, any.     
13697 20 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65 73   ** other proces
13698 73 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  s attempting to 
13699 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62  access the datab
1369a 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 67 65  ase file will ge
1369b 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 74  t to .      ** t
1369c 68 69 73 20 70 6f 69 6e 74 20 69 6e 20 74 68 65  his point in the
1369d 20 63 6f 64 65 20 61 6e 64 20 66 61 69 6c 20 74   code and fail t
1369e 6f 20 6f 62 74 61 69 6e 20 69 74 73 20 6f 77 6e  o obtain its own
1369f 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
136a0 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65  .      ** on the
136a1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
136a2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
136a3 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
136a4 3c 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  <EXCLUSIVE_LOCK 
136a5 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
136a6 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50  sqlite3OsLock(pP
136a7 61 67 65 72 2d 3e 66 64 2c 20 45 58 43 4c 55 53  ager->fd, EXCLUS
136a8 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20  IVE_LOCK);.     
136a9 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
136aa 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
136ab 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72    rc = pager_err
136ac 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a  or(pPager, rc);.
136ad 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66            goto f
136ae 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d  ailed;.        }
136af 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
136b0 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45  >state = PAGER_E
136b1 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 20 20 20  XCLUSIVE;.      
136b2 7d 0a 20 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65  }. .      /* Ope
136b3 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f  n the journal fo
136b4 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63  r read/write acc
136b5 65 73 73 2e 20 54 68 69 73 20 69 73 20 62 65 63  ess. This is bec
136b6 61 75 73 65 20 69 6e 20 0a 20 20 20 20 20 20 2a  ause in .      *
136b7 2a 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65  * exclusive-acce
136b8 73 73 20 6d 6f 64 65 20 74 68 65 20 66 69 6c 65  ss mode the file
136b9 20 64 65 73 63 72 69 70 74 6f 72 20 77 69 6c 6c   descriptor will
136ba 20 62 65 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e   be kept open an
136bb 64 0a 20 20 20 20 20 20 2a 2a 20 70 6f 73 73 69  d.      ** possi
136bc 62 6c 79 20 75 73 65 64 20 66 6f 72 20 61 20 74  bly used for a t
136bd 72 61 6e 73 61 63 74 69 6f 6e 20 6c 61 74 65 72  ransaction later
136be 20 6f 6e 2e 20 4f 6e 20 73 6f 6d 65 20 73 79 73   on. On some sys
136bf 74 65 6d 73 2c 20 74 68 65 0a 20 20 20 20 20 20  tems, the.      
136c0 2a 2a 20 4f 73 54 72 75 6e 63 61 74 65 28 29 20  ** OsTruncate() 
136c1 63 61 6c 6c 20 75 73 65 64 20 69 6e 20 65 78 63  call used in exc
136c2 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f  lusive-access mo
136c3 64 65 20 61 6c 73 6f 20 72 65 71 75 69 72 65 73  de also requires
136c4 0a 20 20 20 20 20 20 2a 2a 20 61 20 72 65 61 64  .      ** a read
136c5 2f 77 72 69 74 65 20 66 69 6c 65 20 68 61 6e 64  /write file hand
136c6 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  le..      */.   
136c7 20 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70     if( !isOpen(p
136c8 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20  Pager->jfd) ){. 
136c9 20 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a         int res;.
136ca 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
136cb 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66  ite3OsAccess(pVf
136cc 73 2c 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e  s,pPager->zJourn
136cd 61 6c 2c 53 51 4c 49 54 45 5f 41 43 43 45 53 53  al,SQLITE_ACCESS
136ce 5f 45 58 49 53 54 53 2c 26 72 65 73 29 3b 0a 20  _EXISTS,&res);. 
136cf 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
136d0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
136d1 20 20 20 20 20 20 69 66 28 20 72 65 73 20 29 7b        if( res ){
136d2 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
136d3 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20 20 20 20   fout = 0;.     
136d4 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20 53         int f = S
136d5 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
136d6 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  RITE|SQLITE_OPEN
136d7 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20  _MAIN_JOURNAL;. 
136d8 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
136d9 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70  t( !pPager->temp
136da 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20  File );.        
136db 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
136dc 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61  OsOpen(pVfs, pPa
136dd 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70  ger->zJournal, p
136de 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26  Pager->jfd, f, &
136df 66 6f 75 74 29 3b 0a 20 20 20 20 20 20 20 20 20  fout);.         
136e0 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
136e1 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70  QLITE_OK || isOp
136e2 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
136e3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
136e4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
136e5 20 26 26 20 66 6f 75 74 26 53 51 4c 49 54 45 5f   && fout&SQLITE_
136e6 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b  OPEN_READONLY ){
136e7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
136e8 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  c = SQLITE_CANTO
136e9 50 45 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20  PEN;.           
136ea 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
136eb 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a  e(pPager->jfd);.
136ec 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
136ed 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
136ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
136ef 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 64 6f 65   the journal doe
136f0 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 69 74 20  s not exist, it 
136f1 75 73 75 61 6c 6c 79 20 6d 65 61 6e 73 20 74 68  usually means th
136f2 61 74 20 73 6f 6d 65 20 0a 20 20 20 20 20 20 20  at some .       
136f3 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f       ** other co
136f4 6e 6e 65 63 74 69 6f 6e 20 6d 61 6e 61 67 65 64  nnection managed
136f5 20 74 6f 20 67 65 74 20 69 6e 20 61 6e 64 20 72   to get in and r
136f6 6f 6c 6c 20 69 74 20 62 61 63 6b 20 62 65 66 6f  oll it back befo
136f7 72 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  re .            
136f8 2a 2a 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69  ** this connecti
136f9 6f 6e 20 6f 62 74 61 69 6e 65 64 20 74 68 65 20  on obtained the 
136fa 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 61  exclusive lock a
136fb 62 6f 76 65 2e 20 4f 72 2c 20 69 74 20 0a 20 20  bove. Or, it .  
136fc 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 79            ** may
136fd 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 70   mean that the p
136fe 61 67 65 72 20 77 61 73 20 69 6e 20 74 68 65 20  ager was in the 
136ff 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68 65 6e  error-state when
13700 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20   this.          
13701 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61    ** function wa
13702 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68 65  s called and the
13703 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f   journal file do
13704 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 2a  es not exist.  *
13705 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  /.            rc
13706 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
13707 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
13708 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   0);.          }
13709 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1370a 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21   }.      if( rc!
1370b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1370c 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65        goto faile
1370d 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  d;.      }..    
1370e 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 79 20 61    /* TODO: Why a
1370f 72 65 20 74 68 65 73 65 20 63 6c 65 61 72 65 64  re these cleared
13710 20 68 65 72 65 3f 20 49 73 20 69 74 20 6e 65 63   here? Is it nec
13711 65 73 73 61 72 79 3f 20 2a 2f 0a 20 20 20 20 20  essary? */.     
13712 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13713 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20  Started = 0;.   
13714 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
13715 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20  alOff = 0;.     
13716 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
13717 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  er = 0;.      pP
13718 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
13719 20 3d 20 30 3b 0a 20 0a 20 20 20 20 20 20 2f 2a   = 0;. .      /*
1371a 20 50 6c 61 79 62 61 63 6b 20 61 6e 64 20 64 65   Playback and de
1371b 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lete the journal
1371c 2e 20 20 44 72 6f 70 20 74 68 65 20 64 61 74 61  .  Drop the data
1371d 62 61 73 65 20 77 72 69 74 65 0a 20 20 20 20 20  base write.     
1371e 20 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61   ** lock and rea
1371f 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20  cquire the read 
13720 6c 6f 63 6b 2e 20 50 75 72 67 65 20 74 68 65 20  lock. Purge the 
13721 63 61 63 68 65 20 62 65 66 6f 72 65 0a 20 20 20  cache before.   
13722 20 20 20 2a 2a 20 70 6c 61 79 69 6e 67 20 62 61     ** playing ba
13723 63 6b 20 74 68 65 20 68 6f 74 2d 6a 6f 75 72 6e  ck the hot-journ
13724 61 6c 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f  al so that we do
13725 6e 27 74 20 65 6e 64 20 75 70 20 77 69 74 68 0a  n't end up with.
13726 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f        ** an inco
13727 6e 73 69 73 74 65 6e 74 20 63 61 63 68 65 2e 0a  nsistent cache..
13728 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
13729 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  f( isOpen(pPager
1372a 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20  ->jfd) ){.      
1372b 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61    rc = pager_pla
1372c 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31 29  yback(pPager, 1)
1372d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
1372e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1372f 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61           rc = pa
13730 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72  ger_error(pPager
13731 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 20  , rc);.         
13732 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20   goto failed;.  
13733 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
13734 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
13735 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41  Pager->state==PA
13736 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20  GER_SHARED).    
13737 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65         || (pPage
13738 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
13739 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 74   && pPager->stat
1373a 65 3e 50 41 47 45 52 5f 53 48 41 52 45 44 29 0a  e>PAGER_SHARED).
1373b 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a        );.    }..
1373c 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
1373d 70 42 61 63 6b 75 70 20 7c 7c 20 73 71 6c 69 74  pBackup || sqlit
1373e 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e  e3PcachePagecoun
1373f 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
13740 65 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  e)>0 ){.      /*
13741 20 54 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b   The shared-lock
13742 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 61   has just been a
13743 63 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20 64  cquired on the d
13744 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20  atabase file.   
13745 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20     ** and there 
13746 61 72 65 20 61 6c 72 65 61 64 79 20 70 61 67 65  are already page
13747 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 28  s in the cache (
13748 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0a  from a previous.
13749 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f 72        ** read or
1374a 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1374b 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20 73  on).  Check to s
1374c 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ee if the databa
1374d 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20  se.      ** has 
1374e 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20  been modified.  
1374f 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
13750 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66 6c 75  has changed, flu
13751 73 68 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  sh the.      ** 
13752 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a  cache..      **.
13753 20 20 20 20 20 20 2a 2a 20 44 61 74 61 62 61 73        ** Databas
13754 65 20 63 68 61 6e 67 65 73 20 69 73 20 64 65 74  e changes is det
13755 65 63 74 65 64 20 62 79 20 6c 6f 6f 6b 69 6e 67  ected by looking
13756 20 61 74 20 31 35 20 62 79 74 65 73 20 62 65 67   at 15 bytes beg
13757 69 6e 6e 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20  inning.      ** 
13758 61 74 20 6f 66 66 73 65 74 20 32 34 20 69 6e 74  at offset 24 int
13759 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65  o the file.  The
1375a 20 66 69 72 73 74 20 34 20 6f 66 20 74 68 65 73   first 4 of thes
1375b 65 20 31 36 20 62 79 74 65 73 20 61 72 65 0a 20  e 16 bytes are. 
1375c 20 20 20 20 20 2a 2a 20 61 20 33 32 2d 62 69 74       ** a 32-bit
1375d 20 63 6f 75 6e 74 65 72 20 74 68 61 74 20 69 73   counter that is
1375e 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74   incremented wit
1375f 68 20 65 61 63 68 20 63 68 61 6e 67 65 2e 20 20  h each change.  
13760 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68  The.      ** oth
13761 65 72 20 62 79 74 65 73 20 63 68 61 6e 67 65 20  er bytes change 
13762 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 61  randomly with ea
13763 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20 77  ch file change w
13764 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 61 20 63  hen.      ** a c
13765 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e 0a  odec is in use..
13766 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20        ** .      
13767 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 76 61  ** There is a va
13768 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20  nishingly small 
13769 63 68 61 6e 63 65 20 74 68 61 74 20 61 20 63 68  chance that a ch
1376a 61 6e 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65  ange will not be
1376b 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 74 65 63   .      ** detec
1376c 74 65 64 2e 20 20 54 68 65 20 63 68 61 6e 63 65  ted.  The chance
1376d 20 6f 66 20 61 6e 20 75 6e 64 65 74 65 63 74 65   of an undetecte
1376e 64 20 63 68 61 6e 67 65 20 69 73 20 73 6f 20 73  d change is so s
1376f 6d 61 6c 6c 20 74 68 61 74 0a 20 20 20 20 20 20  mall that.      
13770 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 6e 65 67  ** it can be neg
13771 6c 65 63 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f  lected..      */
13772 0a 20 20 20 20 20 20 63 68 61 72 20 64 62 46 69  .      char dbFi
13773 6c 65 56 65 72 73 5b 73 69 7a 65 6f 66 28 70 50  leVers[sizeof(pP
13774 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73  ager->dbFileVers
13775 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  )];.      sqlite
13776 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
13777 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 20 20  pPager, 0);..   
13778 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65     if( pPager->e
13779 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  rrCode ){.      
1377a 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65    rc = pPager->e
1377b 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 20 20  rrCode;.        
1377c 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20  goto failed;.   
1377d 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65     }..      asse
1377e 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  rt( pPager->dbSi
1377f 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20  zeValid );.     
13780 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53   if( pPager->dbS
13781 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ize>0 ){.       
13782 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 45 52   IOTRACE(("CKVER
13783 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  S %p %d\n", pPag
13784 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c  er, sizeof(dbFil
13785 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 20 20  eVers)));.      
13786 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
13787 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c  Read(pPager->fd,
13788 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69   &dbFileVers, si
13789 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29  zeof(dbFileVers)
1378a 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20 69  , 24);.        i
1378b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1378c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
1378d 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20  to failed;.     
1378e 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
1378f 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  {.        memset
13790 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30 2c 20  (dbFileVers, 0, 
13791 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72  sizeof(dbFileVer
13792 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  s));.      }..  
13793 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70      if( memcmp(p
13794 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72  Pager->dbFileVer
13795 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c 20 73  s, dbFileVers, s
13796 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73  izeof(dbFileVers
13797 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ))!=0 ){.       
13798 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61   pager_reset(pPa
13799 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ger);.      }.  
1379a 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1379b 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
1379c 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d  eMode || pPager-
1379d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48  >state==PAGER_SH
1379e 41 52 45 44 20 29 3b 0a 20 20 7d 0a 0a 20 66 61  ARED );.  }.. fa
1379f 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 21 3d  iled:.  if( rc!=
137a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
137a1 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b   /* pager_unlock
137a2 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f  () is a no-op fo
137a3 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  r exclusive mode
137a4 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 64   and in-memory d
137a5 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20 20 20  atabases. */.   
137a6 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50   pager_unlock(pP
137a7 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ager);.  }.  ret
137a8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
137a9 20 49 66 20 74 68 65 20 72 65 66 65 72 65 6e 63   If the referenc
137aa 65 20 63 6f 75 6e 74 20 68 61 73 20 72 65 61 63  e count has reac
137ab 68 65 64 20 7a 65 72 6f 2c 20 72 6f 6c 6c 62 61  hed zero, rollba
137ac 63 6b 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a  ck any active.**
137ad 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
137ae 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65   unlock the page
137af 72 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c  r..**.** Except,
137b0 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65   in locking_mode
137b1 3d 45 58 43 4c 55 53 49 56 45 20 77 68 65 6e 20  =EXCLUSIVE when 
137b2 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67  there is nothing
137b3 20 74 6f 20 69 6e 0a 2a 2a 20 74 68 65 20 72 6f   to in.** the ro
137b4 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20  llback journal, 
137b5 74 68 65 20 75 6e 6c 6f 63 6b 20 69 73 20 6e 6f  the unlock is no
137b6 74 20 70 65 72 66 6f 72 6d 65 64 20 61 6e 64 20  t performed and 
137b7 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 74 68  there is.** noth
137b8 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c  ing to rollback,
137b9 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   so this routine
137ba 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 20   is a no-op..*/ 
137bb 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67  .static void pag
137bc 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64  erUnlockIfUnused
137bd 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
137be 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 50  .  if( (sqlite3P
137bf 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50  cacheRefCount(pP
137c0 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d  ager->pPCache)==
137c1 30 29 0a 20 20 20 26 26 20 28 21 70 50 61 67 65  0).   && (!pPage
137c2 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
137c3 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   || pPager->jour
137c4 6e 61 6c 4f 66 66 3e 30 29 20 0a 20 20 29 7b 0a  nalOff>0) .  ){.
137c5 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41      pagerUnlockA
137c6 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65  ndRollback(pPage
137c7 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  r);.  }.}../*.**
137c8 20 41 63 71 75 69 72 65 20 61 20 72 65 66 65 72   Acquire a refer
137c9 65 6e 63 65 20 74 6f 20 70 61 67 65 20 6e 75 6d  ence to page num
137ca 62 65 72 20 70 67 6e 6f 20 69 6e 20 70 61 67 65  ber pgno in page
137cb 72 20 70 50 61 67 65 72 20 28 61 20 70 61 67 65  r pPager (a page
137cc 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 68 61  .** reference ha
137cd 73 20 74 79 70 65 20 44 62 50 61 67 65 2a 29 2e  s type DbPage*).
137ce 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65   If the requeste
137cf 64 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 0a  d reference is .
137d0 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  ** successfully 
137d1 6f 62 74 61 69 6e 65 64 2c 20 69 74 20 69 73 20  obtained, it is 
137d2 63 6f 70 69 65 64 20 74 6f 20 2a 70 70 50 61 67  copied to *ppPag
137d3 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  e and SQLITE_OK 
137d4 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
137d5 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  If the requested
137d6 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79   page is already
137d7 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 69   in the cache, i
137d8 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a  t is returned. .
137d9 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20  ** Otherwise, a 
137da 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20  new page object 
137db 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
137dc 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20   populated with 
137dd 64 61 74 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f  data.** read fro
137de 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
137df 69 6c 65 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73  ile. In some cas
137e0 65 73 2c 20 74 68 65 20 70 63 61 63 68 65 20 6d  es, the pcache m
137e1 6f 64 75 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f  odule may.** cho
137e2 6f 73 65 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63  ose not to alloc
137e3 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 6f  ate a new page o
137e4 62 6a 65 63 74 20 61 6e 64 20 6d 61 79 20 72 65  bject and may re
137e5 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  use an existing.
137e6 2a 2a 20 6f 62 6a 65 63 74 20 77 69 74 68 20 6e  ** object with n
137e7 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65  o outstanding re
137e8 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20  ferences..**.** 
137e9 54 68 65 20 65 78 74 72 61 20 64 61 74 61 20 61  The extra data a
137ea 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61 67  ppended to a pag
137eb 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 74  e is always init
137ec 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73  ialized to zeros
137ed 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 74   the .** first t
137ee 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c 6f  ime a page is lo
137ef 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  aded into memory
137f0 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 72 65  . If the page re
137f1 71 75 65 73 74 65 64 20 69 73 20 0a 2a 2a 20 61  quested is .** a
137f2 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61  lready in the ca
137f3 63 68 65 20 77 68 65 6e 20 74 68 69 73 20 66 75  che when this fu
137f4 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
137f5 2c 20 74 68 65 6e 20 74 68 65 20 65 78 74 72 61  , then the extra
137f6 0a 2a 2a 20 64 61 74 61 20 69 73 20 6c 65 66 74  .** data is left
137f7 20 61 73 20 69 74 20 77 61 73 20 77 68 65 6e 20   as it was when 
137f8 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20  the page object 
137f9 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a  was last used..*
137fa 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61  *.** If the data
137fb 62 61 73 65 20 69 6d 61 67 65 20 69 73 20 73 6d  base image is sm
137fc 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 72  aller than the r
137fd 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6f 72  equested page or
137fe 20 69 66 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65   if a .** non-ze
137ff 72 6f 20 76 61 6c 75 65 20 69 73 20 70 61 73 73  ro value is pass
13800 65 64 20 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74  ed as the noCont
13801 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 61 6e  ent parameter an
13802 64 20 74 68 65 20 0a 2a 2a 20 72 65 71 75 65 73  d the .** reques
13803 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20  ted page is not 
13804 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20 69  already stored i
13805 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65  n the cache, the
13806 6e 20 6e 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20  n no .** actual 
13807 64 69 73 6b 20 72 65 61 64 20 6f 63 63 75 72 73  disk read occurs
13808 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
13809 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20  he memory image 
1380a 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20  of the .** page 
1380b 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  is initialized t
1380c 6f 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a  o all zeros. .**
1380d 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74  .** If noContent
1380e 20 69 73 20 74 72 75 65 2c 20 69 74 20 6d 65 61   is true, it mea
1380f 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f  ns that we do no
13810 74 20 63 61 72 65 20 61 62 6f 75 74 20 74 68 65  t care about the
13811 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20   contents.** of 
13812 74 68 65 20 70 61 67 65 2e 20 54 68 69 73 20 6f  the page. This o
13813 63 63 75 72 73 20 69 6e 20 74 77 6f 20 73 65 70  ccurs in two sep
13814 65 72 61 74 65 20 73 63 65 6e 61 72 69 6f 73 3a  erate scenarios:
13815 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e  .**.**   a) When
13816 20 72 65 61 64 69 6e 67 20 61 20 66 72 65 65 2d   reading a free-
13817 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 66  list leaf page f
13818 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
13819 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29  , and.**.**   b)
1381a 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e   When a savepoin
1381b 74 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65  t is being rolle
1381c 64 20 62 61 63 6b 20 61 6e 64 20 77 65 20 6e 65  d back and we ne
1381d 65 64 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20  ed to load.**   
1381e 20 20 20 61 20 6e 65 77 20 70 61 67 65 20 69 6e     a new page in
1381f 74 6f 20 74 68 65 20 63 61 63 68 65 20 74 6f 20  to the cache to 
13820 70 6f 70 75 6c 61 74 65 20 77 69 74 68 20 74 68  populate with th
13821 65 20 64 61 74 61 20 72 65 61 64 0a 2a 2a 20 20  e data read.**  
13822 20 20 20 20 66 72 6f 6d 20 74 68 65 20 73 61 76      from the sav
13823 65 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a  epoint journal..
13824 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65  **.** If noConte
13825 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  nt is true, then
13826 20 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e   the data return
13827 65 64 20 69 73 20 7a 65 72 6f 65 64 20 69 6e 73  ed is zeroed ins
13828 74 65 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67  tead of.** being
13829 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64   read from the d
1382a 61 74 61 62 61 73 65 2e 20 41 64 64 69 74 69 6f  atabase. Additio
1382b 6e 61 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 20  nally, the bits 
1382c 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
1382d 20 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 65   to pgno in Page
1382e 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69  r.pInJournal (bi
1382f 74 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c  tvec of pages al
13830 72 65 61 64 79 20 77 72 69 74 74 65 6e 20 74 6f  ready written to
13831 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
13832 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 50 61  file) and the Pa
13833 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e  gerSavepoint.pIn
13834 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63  Savepoint bitvec
13835 73 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a  s of any open.**
13836 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20   savepoints are 
13837 73 65 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  set. This means 
13838 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6d  if the page is m
13839 61 64 65 20 77 72 69 74 61 62 6c 65 20 61 74 20  ade writable at 
1383a 61 6e 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20  any.** point in 
1383b 74 68 65 20 66 75 74 75 72 65 2c 20 75 73 69 6e  the future, usin
1383c 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  g a call to sqli
1383d 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2c  te3PagerWrite(),
1383e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a   its contents.**
1383f 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75   will not be jou
13840 72 6e 61 6c 65 64 2e 20 54 68 69 73 20 73 61 76  rnaled. This sav
13841 65 73 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  es IO..**.** The
13842 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67   acquisition mig
13843 68 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65  ht fail for seve
13844 72 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e  ral reasons.  In
13845 20 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61   all cases,.** a
13846 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72  n appropriate er
13847 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
13848 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65  rned and *ppPage
13849 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
1384a 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
1384b 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b  sqlite3PagerLook
1384c 75 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73  up().  Both this
1384d 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f   routine and Loo
1384e 6b 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a  kup() attempt.**
1384f 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20   to find a page 
13850 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  in the in-memory
13851 20 63 61 63 68 65 20 66 69 72 73 74 2e 20 20 49   cache first.  I
13852 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  f the page is no
13853 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20  t already.** in 
13854 6d 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75  memory, this rou
13855 74 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73  tine goes to dis
13856 6b 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20  k to read it in 
13857 77 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29  whereas Lookup()
13858 0a 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73  .** just returns
13859 20 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e   0.  This routin
1385a 65 20 61 63 71 75 69 72 65 73 20 61 20 72 65 61  e acquires a rea
1385b 64 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74  d-lock the first
1385c 20 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20   time it.** has 
1385d 74 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61  to go to disk, a
1385e 6e 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c  nd could also pl
1385f 61 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f  ayback an old jo
13860 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61  urnal if necessa
13861 72 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f  ry..** Since Loo
13862 6b 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73  kup() never goes
13863 20 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76   to disk, it nev
13864 65 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77  er has to deal w
13865 69 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20  ith locks.** or 
13866 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a  journal files..*
13867 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13868 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
13869 72 41 63 71 75 69 72 65 28 0a 20 20 50 61 67 65  rAcquire(.  Page
1386a 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20  r *pPager,      
1386b 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70 65  /* The pager ope
1386c 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  n on the databas
1386d 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  e file */.  Pgno
1386e 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20   pgno,          
1386f 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74  /* Page number t
13870 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50  o fetch */.  DbP
13871 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20  age **ppPage,   
13872 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e   /* Write a poin
13873 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20  ter to the page 
13874 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  here */.  int no
13875 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a  Content       /*
13876 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72   Do not bother r
13877 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66  eading content f
13878 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65  rom disk if true
13879 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
1387a 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a  .  PgHdr *pPg;..
1387b 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74    assert( assert
1387c 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61  _pager_state(pPa
1387d 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ger) );.  assert
1387e 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
1387f 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a  PAGER_UNLOCK );.
13880 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29  .  if( pgno==0 )
13881 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
13882 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
13883 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
13884 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74  he pager is in t
13885 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20  he error state, 
13886 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
13887 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20 20  immediately. .  
13888 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65  ** Otherwise, re
13889 71 75 65 73 74 20 74 68 65 20 70 61 67 65 20 66  quest the page f
1388a 72 6f 6d 20 74 68 65 20 50 43 61 63 68 65 20 6c  rom the PCache l
1388b 61 79 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ayer. */.  if( p
1388c 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d  Pager->errCode!=
1388d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61  SQLITE_OK && pPa
1388e 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51  ger->errCode!=SQ
1388f 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20  LITE_FULL ){.   
13890 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72   rc = pPager->er
13891 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rCode;.  }else{.
13892 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13893 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67  PcacheFetch(pPag
13894 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e  er->pPCache, pgn
13895 6f 2c 20 31 2c 20 70 70 50 61 67 65 29 3b 0a 20  o, 1, ppPage);. 
13896 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51   }..  if( rc!=SQ
13897 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f  LITE_OK ){.    /
13898 2a 20 45 69 74 68 65 72 20 74 68 65 20 63 61 6c  * Either the cal
13899 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 63 61 63  l to sqlite3Pcac
1389a 68 65 46 65 74 63 68 28 29 20 72 65 74 75 72 6e  heFetch() return
1389b 65 64 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 74  ed an error or t
1389c 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20  he.    ** pager 
1389d 77 61 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74  was already in t
1389e 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77  he error-state w
1389f 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
138a0 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20  n was called..  
138a1 20 20 2a 2a 20 53 65 74 20 70 50 67 20 74 6f 20    ** Set pPg to 
138a2 30 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68  0 and jump to th
138a3 65 20 65 78 63 65 70 74 69 6f 6e 20 68 61 6e 64  e exception hand
138a4 6c 65 72 2e 20 20 2a 2f 0a 20 20 20 20 70 50 67  ler.  */.    pPg
138a5 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 70   = 0;.    goto p
138a6 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72  ager_acquire_err
138a7 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
138a8 28 2a 70 70 50 61 67 65 29 2d 3e 70 67 6e 6f 3d  (*ppPage)->pgno=
138a9 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72  =pgno );.  asser
138aa 74 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50  t( (*ppPage)->pP
138ab 61 67 65 72 3d 3d 70 50 61 67 65 72 20 7c 7c 20  ager==pPager || 
138ac 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65  (*ppPage)->pPage
138ad 72 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28  r==0 );..  if( (
138ae 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72  *ppPage)->pPager
138af 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68   ){.    /* In th
138b0 69 73 20 63 61 73 65 20 74 68 65 20 70 63 61 63  is case the pcac
138b1 68 65 20 61 6c 72 65 61 64 79 20 63 6f 6e 74 61  he already conta
138b2 69 6e 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a  ins an initializ
138b3 65 64 20 63 6f 70 79 20 6f 66 0a 20 20 20 20 2a  ed copy of.    *
138b4 2a 20 74 68 65 20 70 61 67 65 2e 20 52 65 74 75  * the page. Retu
138b5 72 6e 20 77 69 74 68 6f 75 74 20 66 75 72 74 68  rn without furth
138b6 65 72 20 61 64 6f 2e 20 20 2a 2f 0a 20 20 20 20  er ado.  */.    
138b7 61 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d 50 41  assert( pgno<=PA
138b8 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 26 26 20  GER_MAX_PGNO && 
138b9 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50  pgno!=PAGER_MJ_P
138ba 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a 20  GNO(pPager) );. 
138bb 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50     PAGER_INCR(pP
138bc 61 67 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 20  ager->nHit);.   
138bd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
138be 4b 3b 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  K;..  }else{.   
138bf 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 63 61   /* The pager ca
138c0 63 68 65 20 68 61 73 20 63 72 65 61 74 65 64 20  che has created 
138c1 61 20 6e 65 77 20 70 61 67 65 2e 20 49 74 73 20  a new page. Its 
138c2 63 6f 6e 74 65 6e 74 20 6e 65 65 64 73 20 74 6f  content needs to
138c3 20 0a 20 20 20 20 2a 2a 20 62 65 20 69 6e 69 74   .    ** be init
138c4 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 20  ialized.  */.   
138c5 20 69 6e 74 20 6e 4d 61 78 3b 0a 0a 20 20 20 20   int nMax;..    
138c6 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65  PAGER_INCR(pPage
138c7 72 2d 3e 6e 4d 69 73 73 29 3b 0a 20 20 20 20 70  r->nMiss);.    p
138c8 50 67 20 3d 20 2a 70 70 50 61 67 65 3b 0a 20 20  Pg = *ppPage;.  
138c9 20 20 70 50 67 2d 3e 70 50 61 67 65 72 20 3d 20    pPg->pPager = 
138ca 70 50 61 67 65 72 3b 0a 0a 20 20 20 20 2f 2a 20  pPager;..    /* 
138cb 54 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65  The maximum page
138cc 20 6e 75 6d 62 65 72 20 69 73 20 32 5e 33 31 2e   number is 2^31.
138cd 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   Return SQLITE_C
138ce 4f 52 52 55 50 54 20 69 66 20 61 20 70 61 67 65  ORRUPT if a page
138cf 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67  .    ** number g
138d0 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 73  reater than this
138d1 2c 20 6f 72 20 74 68 65 20 75 6e 75 73 65 64 20  , or the unused 
138d2 6c 6f 63 6b 69 6e 67 2d 70 61 67 65 2c 20 69 73  locking-page, is
138d3 20 72 65 71 75 65 73 74 65 64 2e 20 2a 2f 0a 20   requested. */. 
138d4 20 20 20 69 66 28 20 70 67 6e 6f 3e 50 41 47 45     if( pgno>PAGE
138d5 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67  R_MAX_PGNO || pg
138d6 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  no==PAGER_MJ_PGN
138d7 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20  O(pPager) ){.   
138d8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
138d9 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
138da 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63     goto pager_ac
138db 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d  quire_err;.    }
138dc 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
138dd 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
138de 28 70 50 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b  (pPager, &nMax);
138df 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
138e0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
138e1 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69  goto pager_acqui
138e2 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20  re_err;.    }.. 
138e3 20 20 20 69 66 28 20 6e 4d 61 78 3c 28 69 6e 74     if( nMax<(int
138e4 29 70 67 6e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c  )pgno || MEMDB |
138e5 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20  | noContent ){. 
138e6 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50       if( pgno>pP
138e7 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a  ager->mxPgno ){.
138e8 09 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c  .rc = SQLITE_FUL
138e9 4c 3b 0a 09 67 6f 74 6f 20 70 61 67 65 72 5f 61  L;..goto pager_a
138ea 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20  cquire_err;.    
138eb 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e 6f    }.      if( no
138ec 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20  Content ){.     
138ed 20 20 20 2f 2a 20 46 61 69 6c 75 72 65 20 74 6f     /* Failure to
138ee 20 73 65 74 20 74 68 65 20 62 69 74 73 20 69 6e   set the bits in
138ef 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61 6c 20 62   the InJournal b
138f0 69 74 2d 76 65 63 74 6f 72 73 20 69 73 20 62 65  it-vectors is be
138f1 6e 69 67 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a  nign..        **
138f2 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 61 6e 73   It merely means
138f3 20 74 68 61 74 20 77 65 20 6d 69 67 68 74 20 64   that we might d
138f4 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 77 6f 72  o some extra wor
138f5 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 61 20 0a  k to journal a .
138f6 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20          ** page 
138f7 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65  that does not ne
138f8 65 64 20 74 6f 20 62 65 20 6a 6f 75 72 6e 61 6c  ed to be journal
138f9 65 64 2e 20 20 4e 65 76 65 72 74 68 65 6c 65 73  ed.  Nevertheles
138fa 73 2c 20 62 65 20 73 75 72 65 20 0a 20 20 20 20  s, be sure .    
138fb 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74      ** to test t
138fc 68 65 20 63 61 73 65 20 77 68 65 72 65 20 61 20  he case where a 
138fd 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63  malloc error occ
138fe 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67  urs while trying
138ff 20 74 6f 20 73 65 74 20 0a 20 20 20 20 20 20 20   to set .       
13900 20 2a 2a 20 61 20 62 69 74 20 69 6e 20 61 20 62   ** a bit in a b
13901 69 74 20 76 65 63 74 6f 72 2e 0a 20 20 20 20 20  it vector..     
13902 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71     */.        sq
13903 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
13904 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20  Malloc();.      
13905 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67    if( pgno<=pPag
13906 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29  er->dbOrigSize )
13907 7b 0a 20 20 20 20 20 20 20 20 20 20 54 45 53 54  {.          TEST
13908 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 71 6c  ONLY( rc = ) sql
13909 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50  ite3BitvecSet(pP
1390a 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
1390b 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  , pgno);.       
1390c 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d     testcase( rc=
1390d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
1390e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1390f 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20     TESTONLY( rc 
13910 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69  = ) addToSavepoi
13911 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72  ntBitvecs(pPager
13912 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  , pgno);.       
13913 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53   testcase( rc==S
13914 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
13915 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e         sqlite3En
13916 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
13917 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
13918 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67        memset(pPg
13919 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67  ->pData, 0, pPag
1391a 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
1391b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 49 4f 54       }.      IOT
1391c 52 41 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25  RACE(("ZERO %p %
1391d 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67  d\n", pPager, pg
1391e 6e 6f 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  no));.    }else{
1391f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
13920 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67  Pg->pPager==pPag
13921 65 72 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  er );.      rc =
13922 20 72 65 61 64 44 62 50 61 67 65 28 70 50 67 29   readDbPage(pPg)
13923 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
13924 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13925 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f       goto pager_
13926 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20  acquire_err;.   
13927 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65     }.    }.#ifde
13928 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
13929 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61  AGES.    pPg->pa
1392a 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70  geHash = pager_p
1392b 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65  agehash(pPg);.#e
1392c 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ndif.  }..  retu
1392d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70  rn SQLITE_OK;..p
1392e 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72  ager_acquire_err
1392f 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d  :.  assert( rc!=
13930 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69  SQLITE_OK );.  i
13931 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 73 71  f( pPg ){.    sq
13932 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28  lite3PcacheDrop(
13933 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65  pPg);.  }.  page
13934 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28  rUnlockIfUnused(
13935 70 50 61 67 65 72 29 3b 0a 0a 20 20 2a 70 70 50  pPager);..  *ppP
13936 61 67 65 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  age = 0;.  retur
13937 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
13938 63 71 75 69 72 65 20 61 20 70 61 67 65 20 69 66  cquire a page if
13939 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 69   it is already i
1393a 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  n the in-memory 
1393b 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f  cache.  Do.** no
1393c 74 20 72 65 61 64 20 74 68 65 20 70 61 67 65 20  t read the page 
1393d 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 75  from disk.  Retu
1393e 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
1393f 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20  the page,.** or 
13940 30 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73  0 if the page is
13941 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 41   not in cache. A
13942 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 69 66  lso, return 0 if
13943 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 72 20 69   the .** pager i
13944 73 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43  s in PAGER_UNLOC
13945 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69  K state when thi
13946 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
13947 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 66 20 74  lled,.** or if t
13948 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 61  he pager is in a
13949 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74  n error state ot
1394a 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  her than SQLITE_
1394b 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  FULL..**.** See 
1394c 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 67 65  also sqlite3Page
1394d 72 47 65 74 28 29 2e 20 20 54 68 65 20 64 69 66  rGet().  The dif
1394e 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
1394f 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
13950 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  and sqlite3Pager
13951 47 65 74 28 29 20 69 73 20 74 68 61 74 20 5f 67  Get() is that _g
13952 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20  et() will go to 
13953 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72 65 61  the disk and rea
13954 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65  d.** in the page
13955 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
13956 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 63  not already in c
13957 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f 75 74  ache.  This rout
13958 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e  ine.** returns N
13959 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67 65 20  ULL if the page 
1395a 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20  is not in cache 
1395b 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49 2f 4f  or if a disk I/O
1395c 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65   error .** has e
1395d 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f  ver happened..*/
1395e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1395f 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 33 50  DbPage *sqlite3P
13960 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72  agerLookup(Pager
13961 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70   *pPager, Pgno p
13962 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70  gno){.  PgHdr *p
13963 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  Pg = 0;.  assert
13964 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20  ( pPager!=0 );. 
13965 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30   assert( pgno!=0
13966 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
13967 61 67 65 72 2d 3e 70 50 43 61 63 68 65 21 3d 30  ager->pPCache!=0
13968 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
13969 61 67 65 72 2d 3e 73 74 61 74 65 20 3e 20 50 41  ager->state > PA
1396a 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20  GER_UNLOCK );.  
1396b 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
1396c 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ch(pPager->pPCac
1396d 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 50  he, pgno, 0, &pP
1396e 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 67  g);.  return pPg
1396f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  ;.}../*.** Relea
13970 73 65 20 61 20 70 61 67 65 20 72 65 66 65 72 65  se a page refere
13971 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  nce..**.** If th
13972 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  e number of refe
13973 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61  rences to the pa
13974 67 65 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2c  ge drop to zero,
13975 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 70 61 67   then the.** pag
13976 65 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68  e is added to th
13977 65 20 4c 52 55 20 6c 69 73 74 2e 20 20 57 68 65  e LRU list.  Whe
13978 6e 20 61 6c 6c 20 72 65 66 65 72 65 6e 63 65 73  n all references
13979 20 74 6f 20 61 6c 6c 20 70 61 67 65 73 0a 2a 2a   to all pages.**
1397a 20 61 72 65 20 72 65 6c 65 61 73 65 64 2c 20 61   are released, a
1397b 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73   rollback occurs
1397c 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e   and the lock on
1397d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
1397e 0a 2a 2a 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a  .** removed..*/.
1397f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
13980 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
13981 55 6e 72 65 66 28 44 62 50 61 67 65 20 2a 70 50  Unref(DbPage *pP
13982 67 29 7b 0a 20 20 69 66 28 20 70 50 67 20 29 7b  g){.  if( pPg ){
13983 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67  .    Pager *pPag
13984 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
13985 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  ;.    sqlite3Pca
13986 63 68 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b  cheRelease(pPg);
13987 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b  .    pagerUnlock
13988 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 29  IfUnused(pPager)
13989 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
1398a 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  f the main journ
1398b 61 6c 20 66 69 6c 65 20 68 61 73 20 61 6c 72 65  al file has alre
1398c 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c  ady been opened,
1398d 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65   ensure that the
1398e 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  .** sub-journal 
1398f 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 74 6f 6f  file is open too
13990 2e 20 49 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f  . If the main jo
13991 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 6f 70 65  urnal is not ope
13992 6e 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74  n,.** this funct
13993 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ion is a no-op..
13994 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  **.** SQLITE_OK 
13995 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 65  is returned if e
13996 76 65 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61  verything goes a
13997 63 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e  ccording to plan
13998 2e 20 0a 2a 2a 20 41 6e 20 53 51 4c 49 54 45 5f  . .** An SQLITE_
13999 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20  IOERR_XXX error 
1399a 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
1399b 20 69 66 20 61 20 63 61 6c 6c 20 74 6f 20 0a 2a   if a call to .*
1399c 2a 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  * sqlite3OsOpen(
1399d 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74  ) fails..*/.stat
1399e 69 63 20 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f  ic int openSubJo
1399f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61  urnal(Pager *pPa
139a0 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ger){.  int rc =
139a1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
139a2 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
139a3 3e 6a 66 64 29 20 26 26 20 21 69 73 4f 70 65 6e  >jfd) && !isOpen
139a4 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29  (pPager->sjfd) )
139a5 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  {.    if( pPager
139a6 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
139a7 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
139a8 5f 4d 45 4d 4f 52 59 20 7c 7c 20 70 50 61 67 65  _MEMORY || pPage
139a9 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20  r->subjInMemory 
139aa 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
139ab 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70  MemJournalOpen(p
139ac 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20  Pager->sjfd);.  
139ad 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
139ae 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 74 65 6d  c = pagerOpentem
139af 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  p(pPager, pPager
139b0 2d 3e 73 6a 66 64 2c 20 53 51 4c 49 54 45 5f 4f  ->sjfd, SQLITE_O
139b1 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 3b  PEN_SUBJOURNAL);
139b2 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
139b3 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
139b4 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
139b5 73 20 63 61 6c 6c 65 64 20 61 74 20 74 68 65 20  s called at the 
139b6 73 74 61 72 74 20 6f 66 20 65 76 65 72 79 20 77  start of every w
139b7 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
139b8 2e 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20  ..** There must 
139b9 61 6c 72 65 61 64 79 20 62 65 20 61 20 52 45 53  already be a RES
139ba 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49  ERVED or EXCLUSI
139bb 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  VE lock on the d
139bc 61 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65  atabase .** file
139bd 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
139be 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  ne is called..**
139bf 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75  .** Open the jou
139c0 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61  rnal file for pa
139c1 67 65 72 20 70 50 61 67 65 72 20 61 6e 64 20 77  ger pPager and w
139c2 72 69 74 65 20 61 20 6a 6f 75 72 6e 61 6c 20 68  rite a journal h
139c3 65 61 64 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  eader.** to the 
139c4 73 74 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20  start of it. If 
139c5 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65  there are active
139c6 20 73 61 76 65 70 6f 69 6e 74 73 2c 20 6f 70 65   savepoints, ope
139c7 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  n the sub-journa
139c8 6c 0a 2a 2a 20 61 73 20 77 65 6c 6c 2e 20 54 68  l.** as well. Th
139c9 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f  is function is o
139ca 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 74 68  nly used when th
139cb 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
139cc 73 20 62 65 69 6e 67 20 0a 2a 2a 20 6f 70 65 6e  s being .** open
139cd 65 64 20 74 6f 20 77 72 69 74 65 20 61 20 72 6f  ed to write a ro
139ce 6c 6c 62 61 63 6b 20 6c 6f 67 20 66 6f 72 20 61  llback log for a
139cf 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 74   transaction. It
139d0 20 69 73 20 6e 6f 74 20 75 73 65 64 20 0a 2a 2a   is not used .**
139d1 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20   when opening a 
139d2 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  hot journal file
139d3 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b   to roll it back
139d4 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a  ..**.** If the j
139d5 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61  ournal file is a
139d6 6c 72 65 61 64 79 20 6f 70 65 6e 20 28 61 73 20  lready open (as 
139d7 69 74 20 6d 61 79 20 62 65 20 69 6e 20 65 78 63  it may be in exc
139d8 6c 75 73 69 76 65 20 6d 6f 64 65 29 2c 0a 2a 2a  lusive mode),.**
139d9 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
139da 69 6f 6e 20 6a 75 73 74 20 77 72 69 74 65 73 20  ion just writes 
139db 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  a journal header
139dc 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
139dd 20 74 68 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20   the.** already 
139de 6f 70 65 6e 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a  open file. .**.*
139df 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
139e0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
139e1 65 20 69 73 20 6f 70 65 6e 65 64 20 62 79 20 74  e is opened by t
139e2 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  his function, th
139e3 65 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f  e.** Pager.pInJo
139e4 75 72 6e 61 6c 20 62 69 74 76 65 63 20 73 74 72  urnal bitvec str
139e5 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61  ucture is alloca
139e6 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ted..**.** Retur
139e7 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65  n SQLITE_OK if e
139e8 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63  verything is suc
139e9 63 65 73 73 66 75 6c 2e 20 4f 74 68 65 72 77 69  cessful. Otherwi
139ea 73 65 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53  se, return .** S
139eb 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 74  QLITE_NOMEM if t
139ec 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c  he attempt to al
139ed 6c 6f 63 61 74 65 20 50 61 67 65 72 2e 70 49 6e  locate Pager.pIn
139ee 4a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f  Journal fails, o
139ef 72 20 0a 2a 2a 20 61 6e 20 49 4f 20 65 72 72 6f  r .** an IO erro
139f0 72 20 63 6f 64 65 20 69 66 20 6f 70 65 6e 69 6e  r code if openin
139f1 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65  g or writing the
139f2 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 61   journal file fa
139f3 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ils..*/.static i
139f4 6e 74 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f  nt pager_open_jo
139f5 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61  urnal(Pager *pPa
139f6 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ger){.  int rc =
139f7 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
139f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139f9 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
139fa 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  e */.  sqlite3_v
139fb 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20  fs * const pVfs 
139fc 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 20  = pPager->pVfs; 
139fd 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65    /* Local cache
139fe 20 6f 66 20 76 66 73 20 70 6f 69 6e 74 65 72 20   of vfs pointer 
139ff 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
13a00 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
13a01 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20  ER_RESERVED );. 
13a02 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13a03 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20  >useJournal );. 
13a04 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13a05 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41  >journalMode!=PA
13a06 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
13a07 4f 46 46 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OFF );.  assert(
13a08 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
13a09 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f  nal==0 );.  .  /
13a0a 2a 20 49 66 20 61 6c 72 65 61 64 79 20 69 6e 20  * If already in 
13a0b 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c  the error state,
13a0c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13a0d 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 42 75 74 20  s a no-op.  But 
13a0e 6f 6e 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65  on.  ** the othe
13a0f 72 20 68 61 6e 64 2c 20 74 68 69 73 20 72 6f 75  r hand, this rou
13a10 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61  tine is never ca
13a11 6c 6c 65 64 20 69 66 20 77 65 20 61 72 65 20 61  lled if we are a
13a12 6c 72 65 61 64 79 20 69 6e 0a 20 20 2a 2a 20 61  lready in.  ** a
13a13 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 2a  n error state. *
13a14 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50  /.  if( NEVER(pP
13a15 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29  ager->errCode) )
13a16 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
13a17 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54  errCode;..  /* T
13a18 4f 44 4f 3a 20 49 73 20 69 74 20 72 65 61 6c 6c  ODO: Is it reall
13a19 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65  y possible to ge
13a1a 74 20 68 65 72 65 20 77 69 74 68 20 64 62 53 69  t here with dbSi
13a1b 7a 65 56 61 6c 69 64 3d 3d 30 3f 20 49 66 20 6e  zeValid==0? If n
13a1c 6f 74 2c 0a 20 20 2a 2a 20 74 68 65 20 63 61 6c  ot,.  ** the cal
13a1d 6c 20 74 6f 20 50 61 67 65 72 50 61 67 65 63 6f  l to PagerPageco
13a1e 75 6e 74 28 29 20 63 61 6e 20 62 65 20 72 65 6d  unt() can be rem
13a1f 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 74 65 73  oved..  */.  tes
13a20 74 63 61 73 65 28 20 70 50 61 67 65 72 2d 3e 64  tcase( pPager->d
13a21 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b  bSizeValid==0 );
13a22 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  .  sqlite3PagerP
13a23 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
13a24 20 30 29 3b 0a 0a 20 20 70 50 61 67 65 72 2d 3e   0);..  pPager->
13a25 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c  pInJournal = sql
13a26 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
13a27 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29  (pPager->dbSize)
13a28 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
13a29 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b  pInJournal==0 ){
13a2a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
13a2b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20  TE_NOMEM;.  }.. 
13a2c 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75   /* Open the jou
13a2d 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 69 74 20  rnal file if it 
13a2e 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f  is not already o
13a2f 70 65 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 21 69  pen. */.  if( !i
13a30 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
13a31 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  d) ){.    if( pP
13a32 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
13a33 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
13a34 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20  MODE_MEMORY ){. 
13a35 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a       sqlite3MemJ
13a36 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65  ournalOpen(pPage
13a37 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c  r->jfd);.    }el
13a38 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  se{.      const 
13a39 69 6e 74 20 66 6c 61 67 73 20 3d 20 20 20 20 20  int flags =     
13a3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13a3b 20 56 46 53 20 66 6c 61 67 73 20 74 6f 20 6f 70   VFS flags to op
13a3c 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  en journal file 
13a3d 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54  */.        SQLIT
13a3e 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
13a3f 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45  |SQLITE_OPEN_CRE
13a40 41 54 45 7c 0a 20 20 20 20 20 20 20 20 28 70 50  ATE|.        (pP
13a41 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f  ager->tempFile ?
13a42 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c   .          (SQL
13a43 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
13a44 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50  NCLOSE|SQLITE_OP
13a45 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29  EN_TEMP_JOURNAL)
13a46 3a 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c  :.          (SQL
13a47 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
13a48 55 52 4e 41 4c 29 0a 20 20 20 20 20 20 20 20 29  URNAL).        )
13a49 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
13a4a 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
13a4b 49 54 45 0a 20 20 20 20 20 20 72 63 20 3d 20 73  ITE.      rc = s
13a4c 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65  qlite3JournalOpe
13a4d 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70 56 66  n(.          pVf
13a4e 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
13a4f 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  nal, pPager->jfd
13a50 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66  , flags, jrnlBuf
13a51 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 0a  ferSize(pPager).
13a52 20 20 20 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20        );.#else. 
13a53 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13a54 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50  3OsOpen(pVfs, pP
13a55 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  ager->zJournal, 
13a56 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61  pPager->jfd, fla
13a57 67 73 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20  gs, 0);.#endif. 
13a58 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
13a59 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
13a5a 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  | isOpen(pPager-
13a5b 3e 6a 66 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a 20  >jfd) );.  }... 
13a5c 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 66 69   /* Write the fi
13a5d 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  rst journal head
13a5e 65 72 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  er to the journa
13a5f 6c 20 66 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20  l file and open 
13a60 0a 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f  .  ** the sub-jo
13a61 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61  urnal if necessa
13a62 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  ry..  */.  if( r
13a63 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
13a64 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65      /* TODO: Che
13a65 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65  ck if all of the
13a66 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65  se are really re
13a67 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 70  quired. */.    p
13a68 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a  Pager->dbOrigSiz
13a69 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69  e = pPager->dbSi
13a6a 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  ze;.    pPager->
13a6b 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d  journalStarted =
13a6c 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
13a6d 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20  needSync = 0;.  
13a6e 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d    pPager->nRec =
13a6f 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
13a70 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a  journalOff = 0;.
13a71 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d      pPager->setM
13a72 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70  aster = 0;.    p
13a73 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
13a74 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  r = 0;.    rc = 
13a75 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
13a76 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69  pPager);.  }.  i
13a77 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13a78 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76   && pPager->nSav
13a79 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 72 63  epoint ){.    rc
13a7a 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61   = openSubJourna
13a7b 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a  l(pPager);.  }..
13a7c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13a7d 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
13a7e 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
13a7f 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
13a80 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  al);.    pPager-
13a81 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b  >pInJournal = 0;
13a82 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
13a83 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e  ;.}../*.** Begin
13a84 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63   a write-transac
13a85 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 70 65 63  tion on the spec
13a86 69 66 69 65 64 20 70 61 67 65 72 20 6f 62 6a 65  ified pager obje
13a87 63 74 2e 20 49 66 20 61 20 0a 2a 2a 20 77 72 69  ct. If a .** wri
13a88 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68  te-transaction h
13a89 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
13a8a 6f 70 65 6e 65 64 2c 20 74 68 69 73 20 66 75 6e  opened, this fun
13a8b 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
13a8c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65  ..**.** If the e
13a8d 78 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69  xFlag argument i
13a8e 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 63  s false, then ac
13a8f 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 20 61  quire at least a
13a90 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63   RESERVED.** loc
13a91 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
13a92 65 20 66 69 6c 65 2e 20 49 66 20 65 78 46 6c 61  e file. If exFla
13a93 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  g is true, then 
13a94 61 63 71 75 69 72 65 20 61 74 20 6c 65 61 73 74  acquire at least
13a95 0a 2a 2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45  .** an EXCLUSIVE
13a96 20 6c 6f 63 6b 2e 20 49 66 20 73 75 63 68 20 61   lock. If such a
13a97 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79   lock is already
13a98 20 68 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e   held, no lockin
13a99 67 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  g .** functions 
13a9a 6e 65 65 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a  need be called..
13a9b 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73  **.** If this is
13a9c 20 6e 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79   not a temporary
13a9d 20 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69   or in-memory fi
13a9e 6c 65 20 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72  le and, the jour
13a9f 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20  nal file is .** 
13aa0 6f 70 65 6e 65 64 20 69 66 20 69 74 20 68 61 73  opened if it has
13aa1 20 6e 6f 74 20 62 65 65 6e 20 61 6c 72 65 61 64   not been alread
13aa2 79 2e 20 46 6f 72 20 61 20 74 65 6d 70 6f 72 61  y. For a tempora
13aa3 72 79 20 66 69 6c 65 2c 20 74 68 65 20 6f 70 65  ry file, the ope
13aa4 6e 69 6e 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20  ning .** of the 
13aa5 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
13aa6 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74  deferred until t
13aa7 68 65 72 65 20 69 73 20 61 6e 20 61 63 74 75 61  here is an actua
13aa8 6c 20 6e 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72  l need to .** wr
13aa9 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ite to the journ
13aaa 61 6c 2e 20 54 4f 44 4f 3a 20 57 68 79 20 68 61  al. TODO: Why ha
13aab 6e 64 6c 65 20 74 65 6d 70 6f 72 61 72 79 20 66  ndle temporary f
13aac 69 6c 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79  iles differently
13aad 3f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a  ?.**.** If the j
13aae 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f  ournal file is o
13aaf 70 65 6e 65 64 20 28 6f 72 20 69 66 20 69 74 20  pened (or if it 
13ab0 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29  is already open)
13ab1 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72  , then a.** jour
13ab2 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72  nal-header is wr
13ab3 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 74 61  itten to the sta
13ab4 72 74 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  rt of it..**.** 
13ab5 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d  If the subjInMem
13ab6 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ory argument is 
13ab7 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61  non-zero, then a
13ab8 6e 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f  ny sub-journal o
13ab9 70 65 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20  pened.** within 
13aba 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
13abb 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20   will be opened 
13abc 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  as an in-memory 
13abd 66 69 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61  file. This.** ha
13abe 73 20 6e 6f 20 65 66 66 65 63 74 20 69 66 20 74  s no effect if t
13abf 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69  he sub-journal i
13ac0 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64  s already opened
13ac1 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 77   (as it may be w
13ac2 68 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69  hen.** running i
13ac3 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
13ac4 29 20 6f 72 20 69 66 20 74 68 65 20 74 72 61 6e  ) or if the tran
13ac5 73 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  saction does not
13ac6 20 72 65 71 75 69 72 65 20 61 0a 2a 2a 20 73 75   require a.** su
13ac7 62 2d 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68  b-journal. If th
13ac8 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61  e subjInMemory a
13ac9 72 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c  rgument is zero,
13aca 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72   then any requir
13acb 65 64 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61  ed.** sub-journa
13acc 6c 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  l is implemented
13acd 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50   in-memory if pP
13ace 61 67 65 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65  ager is an in-me
13acf 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 0a  mory database, .
13ad0 2a 2a 20 6f 72 20 75 73 69 6e 67 20 61 20 74 65  ** or using a te
13ad1 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6f 74 68  mporary file oth
13ad2 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erwise..*/.SQLIT
13ad3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13ad4 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28  lite3PagerBegin(
13ad5 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
13ad6 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 73  nt exFlag, int s
13ad7 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20  ubjInMemory){.  
13ad8 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
13ad9 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  OK;.  assert( pP
13ada 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47  ager->state!=PAG
13adb 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70  ER_UNLOCK );.  p
13adc 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d  Pager->subjInMem
13add 6f 72 79 20 3d 20 28 75 38 29 73 75 62 6a 49 6e  ory = (u8)subjIn
13ade 4d 65 6d 6f 72 79 3b 0a 20 20 69 66 28 20 70 50  Memory;.  if( pP
13adf 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
13ae0 45 52 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20  ER_SHARED ){.   
13ae1 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13ae2 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29  >pInJournal==0 )
13ae3 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d  ;.    assert( !M
13ae4 45 4d 44 42 20 26 26 20 21 70 50 61 67 65 72 2d  EMDB && !pPager-
13ae5 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20  >tempFile );..  
13ae6 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 52 45    /* Obtain a RE
13ae7 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74  SERVED lock on t
13ae8 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13ae9 2e 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20  . If the exFlag 
13aea 70 61 72 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a  parameter.    **
13aeb 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 69   is true, then i
13aec 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 67 72 61  mmediately upgra
13aed 64 65 20 74 68 69 73 20 74 6f 20 61 6e 20 45 58  de this to an EX
13aee 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68  CLUSIVE lock. Th
13aef 65 0a 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61  e.    ** busy-ha
13af0 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63  ndler callback c
13af1 61 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20  an be used when 
13af2 75 70 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65  upgrading to the
13af3 20 45 58 43 4c 55 53 49 56 45 0a 20 20 20 20 2a   EXCLUSIVE.    *
13af4 2a 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20  * lock, but not 
13af5 77 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74  when obtaining t
13af6 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b  he RESERVED lock
13af7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
13af8 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  = sqlite3OsLock(
13af9 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45  pPager->fd, RESE
13afa 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  RVED_LOCK);.    
13afb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
13afc 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  K ){.      pPage
13afd 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52  r->state = PAGER
13afe 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20 20 20  _RESERVED;.     
13aff 20 69 66 28 20 65 78 46 6c 61 67 20 29 7b 0a 20   if( exFlag ){. 
13b00 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65         rc = page
13b01 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70  r_wait_on_lock(p
13b02 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45  Pager, EXCLUSIVE
13b03 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a  _LOCK);.      }.
13b04 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
13b05 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f   the required lo
13b06 63 6b 73 20 77 65 72 65 20 73 75 63 63 65 73 73  cks were success
13b07 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20  fully obtained, 
13b08 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  open the journal
13b09 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64  .    ** file and
13b0a 20 77 72 69 74 65 20 74 68 65 20 66 69 72 73 74   write the first
13b0b 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20   journal-header 
13b0c 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  to it..    */.  
13b0d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13b0e 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a  _OK && pPager->j
13b0f 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45  ournalMode!=PAGE
13b10 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
13b11 46 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  F ){.      rc = 
13b12 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e  pager_open_journ
13b13 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  al(pPager);.    
13b14 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73  }.  }else if( is
13b15 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
13b16 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ) && pPager->jou
13b17 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20  rnalOff==0 ){.  
13b18 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e    /* This happen
13b19 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72  s when the pager
13b1a 20 77 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76   was in exclusiv
13b1b 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68  e-access mode th
13b1c 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69  e last.    ** ti
13b1d 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20 77 72  me a (read or wr
13b1e 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e  ite) transaction
13b1f 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c   was successfull
13b20 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20  y concluded.    
13b21 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65  ** by this conne
13b22 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f  ction. Instead o
13b23 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a  f deleting the j
13b24 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77  ournal file it w
13b25 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20  as .    ** kept 
13b26 6f 70 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20  open and either 
13b27 77 61 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f  was truncated to
13b28 20 30 20 62 79 74 65 73 20 6f 72 20 69 74 73 20   0 bytes or its 
13b29 68 65 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a  header was.    *
13b2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69  * overwritten wi
13b2b 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f  th zeros..    */
13b2c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
13b2d 67 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a  ger->nRec==0 );.
13b2e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
13b2f 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d  er->dbOrigSize==
13b30 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
13b31 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
13b32 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63  nal==0 );.    rc
13b33 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f   = pager_open_jo
13b34 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
13b35 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45   }..  PAGERTRACE
13b36 28 28 22 54 52 41 4e 53 41 43 54 49 4f 4e 20 25  (("TRANSACTION %
13b37 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50  d\n", PAGERID(pP
13b38 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72  ager)));.  asser
13b39 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  t( !isOpen(pPage
13b3a 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65  r->jfd) || pPage
13b3b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20  r->journalOff>0 
13b3c 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  || rc!=SQLITE_OK
13b3d 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
13b3e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61  LITE_OK ){.    a
13b3f 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e  ssert( !pPager->
13b40 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a 20 20  dbModified );.  
13b41 20 20 2f 2a 20 49 67 6e 6f 72 65 20 61 6e 79 20    /* Ignore any 
13b42 49 4f 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63  IO error that oc
13b43 63 75 72 73 20 77 69 74 68 69 6e 20 70 61 67 65  curs within page
13b44 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
13b45 6e 28 29 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20  n(). The.    ** 
13b46 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20  purpose of this 
13b47 63 61 6c 6c 20 69 73 20 74 6f 20 72 65 73 65 74  call is to reset
13b48 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74   the internal st
13b49 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 65 72  ate of the pager
13b4a 0a 20 20 20 20 2a 2a 20 73 75 62 2d 73 79 73 74  .    ** sub-syst
13b4b 65 6d 2e 20 49 74 20 64 6f 65 73 6e 27 74 20 6d  em. It doesn't m
13b4c 61 74 74 65 72 20 69 66 20 74 68 65 20 6a 6f 75  atter if the jou
13b4d 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e 6f 74  rnal-file is not
13b4e 20 70 72 6f 70 65 72 6c 79 0a 20 20 20 20 2a 2a   properly.    **
13b4f 20 66 69 6e 61 6c 69 7a 65 64 20 61 74 20 74 68   finalized at th
13b50 69 73 20 70 6f 69 6e 74 20 28 73 69 6e 63 65 20  is point (since 
13b51 69 74 20 69 73 20 6e 6f 74 20 61 20 76 61 6c 69  it is not a vali
13b52 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  d journal file a
13b53 6e 79 77 61 79 29 2e 0a 20 20 20 20 2a 2f 0a 20  nyway)..    */. 
13b54 20 20 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61     pager_end_tra
13b55 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
13b56 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   0);.  }.  retur
13b57 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  n rc;.}../*.** M
13b58 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 64 61 74  ark a single dat
13b59 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61  a page as writea
13b5a 62 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73  ble. The page is
13b5b 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
13b5c 65 20 0a 2a 2a 20 6d 61 69 6e 20 6a 6f 75 72 6e  e .** main journ
13b5d 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61  al or sub-journa
13b5e 6c 20 61 73 20 72 65 71 75 69 72 65 64 2e 20 49  l as required. I
13b5f 66 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72  f the page is wr
13b60 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 6f 6e  itten into.** on
13b61 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
13b62 73 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  s, the correspon
13b63 64 69 6e 67 20 62 69 74 20 69 73 20 73 65 74 20  ding bit is set 
13b64 69 6e 20 74 68 65 20 0a 2a 2a 20 50 61 67 65 72  in the .** Pager
13b65 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76  .pInJournal bitv
13b66 65 63 20 61 6e 64 20 74 68 65 20 50 61 67 65 72  ec and the Pager
13b67 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76  Savepoint.pInSav
13b68 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 0a 2a  epoint bitvecs.*
13b69 2a 20 6f 66 20 61 6e 79 20 6f 70 65 6e 20 73 61  * of any open sa
13b6a 76 65 70 6f 69 6e 74 73 20 61 73 20 61 70 70 72  vepoints as appr
13b6b 6f 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74  opriate..*/.stat
13b6c 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69  ic int pager_wri
13b6d 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  te(PgHdr *pPg){.
13b6e 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20    void *pData = 
13b6f 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 50 61  pPg->pData;.  Pa
13b70 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
13b71 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74  g->pPager;.  int
13b72 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
13b73 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74  ..  /* This rout
13b74 69 6e 65 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65  ine is not calle
13b75 64 20 75 6e 6c 65 73 73 20 61 20 74 72 61 6e 73  d unless a trans
13b76 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61  action has alrea
13b77 64 79 20 62 65 65 6e 0a 20 20 2a 2a 20 73 74 61  dy been.  ** sta
13b78 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  rted..  */.  ass
13b79 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
13b7a 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56  te>=PAGER_RESERV
13b7b 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  ED );..  /* If a
13b7c 6e 20 65 72 72 6f 72 20 68 61 73 20 62 65 65 6e  n error has been
13b7d 20 70 72 65 76 69 6f 75 73 6c 79 20 64 65 74 65   previously dete
13b7e 63 74 65 64 2c 20 77 65 20 73 68 6f 75 6c 64 20  cted, we should 
13b7f 6e 6f 74 20 62 65 0a 20 20 2a 2a 20 63 61 6c 6c  not be.  ** call
13b80 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
13b81 2e 20 20 52 65 70 65 61 74 20 74 68 65 20 65 72  .  Repeat the er
13b82 72 6f 72 20 66 6f 72 20 72 6f 62 75 73 74 6e 65  ror for robustne
13b83 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e  ss..  */.  if( N
13b84 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72  EVER(pPager->err
13b85 43 6f 64 65 29 20 29 20 20 72 65 74 75 72 6e 20  Code) )  return 
13b86 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
13b87 0a 0a 20 20 2f 2a 20 48 69 67 68 65 72 2d 6c 65  ..  /* Higher-le
13b88 76 65 6c 20 72 6f 75 74 69 6e 65 73 20 6e 65 76  vel routines nev
13b89 65 72 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e  er call this fun
13b8a 63 74 69 6f 6e 20 69 66 20 64 61 74 61 62 61 73  ction if databas
13b8b 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 77 72  e is not.  ** wr
13b8c 69 74 61 62 6c 65 2e 20 20 42 75 74 20 63 68 65  itable.  But che
13b8d 63 6b 20 61 6e 79 77 61 79 2c 20 6a 75 73 74 20  ck anyway, just 
13b8e 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 20  for robustness. 
13b8f 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  */.  if( NEVER(p
13b90 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 29  Pager->readOnly)
13b91 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
13b92 5f 50 45 52 4d 3b 0a 0a 20 20 61 73 73 65 72 74  _PERM;..  assert
13b93 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61  ( !pPager->setMa
13b94 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b  ster );..  CHECK
13b95 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f  _PAGE(pPg);..  /
13b96 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20  * Mark the page 
13b97 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 68  as dirty.  If th
13b98 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61  e page has alrea
13b99 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a  dy been written.
13b9a 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72    ** to the jour
13b9b 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20  nal then we can 
13b9c 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61  return right awa
13b9d 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  y..  */.  sqlite
13b9e 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79  3PcacheMakeDirty
13b9f 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67  (pPg);.  if( pag
13ba0 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20  eInJournal(pPg) 
13ba1 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 73  && !subjRequires
13ba2 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20  Page(pPg) ){.   
13ba3 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66   pPager->dbModif
13ba4 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  ied = 1;.  }else
13ba5 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  {..    /* If we 
13ba6 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74  get this far, it
13ba7 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
13ba8 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65  page needs to be
13ba9 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 20  .    ** written 
13baa 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  to the transacti
13bab 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68  on journal or th
13bac 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75  e ckeckpoint jou
13bad 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62  rnal.    ** or b
13bae 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  oth..    **.    
13baf 2a 2a 20 48 69 67 68 65 72 20 6c 65 76 65 6c 20  ** Higher level 
13bb0 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20  routines should 
13bb1 68 61 76 65 20 61 6c 72 65 61 64 79 20 73 74 61  have already sta
13bb2 72 74 65 64 20 61 20 74 72 61 6e 73 61 63 74 69  rted a transacti
13bb3 6f 6e 2c 0a 20 20 20 20 2a 2a 20 77 68 69 63 68  on,.    ** which
13bb4 20 6d 65 61 6e 73 20 74 68 65 79 20 68 61 76 65   means they have
13bb5 20 61 63 71 75 69 72 65 64 20 74 68 65 20 6e 65   acquired the ne
13bb6 63 65 73 73 61 72 79 20 6c 6f 63 6b 73 20 61 6e  cessary locks an
13bb7 64 20 6f 70 65 6e 65 64 0a 20 20 20 20 2a 2a 20  d opened.    ** 
13bb8 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  a rollback journ
13bb9 61 6c 2e 20 20 44 6f 75 62 6c 65 2d 63 68 65 63  al.  Double-chec
13bba 6b 20 74 6f 20 6d 61 6b 65 73 20 73 75 72 65 20  k to makes sure 
13bbb 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65  this is the case
13bbc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
13bbd 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 65  = sqlite3PagerBe
13bbe 67 69 6e 28 70 50 61 67 65 72 2c 20 30 2c 20 70  gin(pPager, 0, p
13bbf 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d  Pager->subjInMem
13bc0 6f 72 79 29 3b 0a 20 20 20 20 69 66 28 20 4e 45  ory);.    if( NE
13bc1 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f  VER(rc!=SQLITE_O
13bc2 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  K) ){.      retu
13bc3 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
13bc4 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61   if( !isOpen(pPa
13bc5 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61  ger->jfd) && pPa
13bc6 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
13bc7 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  !=PAGER_JOURNALM
13bc8 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20  ODE_OFF ){.     
13bc9 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13bca 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20  >useJournal );. 
13bcb 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
13bcc 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61  open_journal(pPa
13bcd 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ger);.      if( 
13bce 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
13bcf 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
13bd0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4d  .    pPager->dbM
13bd1 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 20 20 0a  odified = 1;.  .
13bd2 20 20 20 20 2f 2a 20 54 68 65 20 74 72 61 6e 73      /* The trans
13bd3 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e  action journal n
13bd4 6f 77 20 65 78 69 73 74 73 20 61 6e 64 20 77 65  ow exists and we
13bd5 20 68 61 76 65 20 61 20 52 45 53 45 52 56 45 44   have a RESERVED
13bd6 20 6f 72 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58   or an.    ** EX
13bd7 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
13bd8 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
13bd9 65 20 66 69 6c 65 2e 20 20 57 72 69 74 65 20 74  e file.  Write t
13bda 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20  he current page 
13bdb 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 72  to.    ** the tr
13bdc 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61  ansaction journa
13bdd 6c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 74  l if it is not t
13bde 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20  here already..  
13bdf 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 61    */.    if( !pa
13be0 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29  geInJournal(pPg)
13be1 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65   && isOpen(pPage
13be2 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20  r->jfd) ){.     
13be3 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d   if( pPg->pgno<=
13be4 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69  pPager->dbOrigSi
13be5 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 75 33  ze ){.        u3
13be6 32 20 63 6b 73 75 6d 3b 0a 20 20 20 20 20 20 20  2 cksum;.       
13be7 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b 0a 0a   char *pData2;..
13be8 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 73 68          /* We sh
13be9 6f 75 6c 64 20 6e 65 76 65 72 20 77 72 69 74 65  ould never write
13bea 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
13beb 66 69 6c 65 20 74 68 65 20 70 61 67 65 20 74 68  file the page th
13bec 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  at.        ** co
13bed 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 61 62  ntains the datab
13bee 61 73 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20  ase locks.  The 
13bef 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
13bf0 20 76 65 72 69 66 69 65 73 0a 20 20 20 20 20 20   verifies.      
13bf1 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64 6f 20    ** that we do 
13bf2 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  not. */.        
13bf3 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 67 6e  assert( pPg->pgn
13bf4 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  o!=PAGER_MJ_PGNO
13bf5 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20  (pPager) );.    
13bf6 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65      CODEC2(pPage
13bf7 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70  r, pData, pPg->p
13bf8 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e 20 53  gno, 7, return S
13bf9 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61  QLITE_NOMEM, pDa
13bfa 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20 63 6b  ta2);.        ck
13bfb 73 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b 73 75  sum = pager_cksu
13bfc 6d 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 70  m(pPager, (u8*)p
13bfd 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20  Data2);.        
13bfe 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73  rc = write32bits
13bff 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
13c00 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
13c01 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20  , pPg->pgno);.  
13c02 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
13c03 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13c04 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13c05 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d  3OsWrite(pPager-
13c06 3e 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50  >jfd, pData2, pP
13c07 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a  ager->pageSize,.
13c08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50                pP
13c0a 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
13c0b 20 2b 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20   + 4);.         
13c0c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13c0d 4f 66 66 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70  Off += pPager->p
13c0e 61 67 65 53 69 7a 65 2b 34 3b 0a 20 20 20 20 20  ageSize+4;.     
13c0f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
13c10 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13c11 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
13c12 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61   write32bits(pPa
13c13 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72  ger->jfd, pPager
13c14 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 63 6b  ->journalOff, ck
13c15 73 75 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  sum);.          
13c16 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
13c17 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20  ff += 4;.       
13c18 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41   }.        IOTRA
13c19 43 45 28 28 22 4a 4f 55 54 20 25 70 20 25 64 20  CE(("JOUT %p %d 
13c1a 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67  %lld %d\n", pPag
13c1b 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a  er, pPg->pgno, .
13c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c1d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13c1e 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  Off, pPager->pag
13c1f 65 53 69 7a 65 29 29 3b 0a 20 20 20 20 20 20 20  eSize));.       
13c20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69   PAGER_INCR(sqli
13c21 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a  te3_pager_writej
13c22 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 20  _count);.       
13c23 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4a 4f   PAGERTRACE(("JO
13c24 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64  URNAL %d page %d
13c25 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61 73   needSync=%d has
13c26 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20  h(%08x)\n",.    
13c27 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44           PAGERID
13c28 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70  (pPager), pPg->p
13c29 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20  gno, .          
13c2a 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26     ((pPg->flags&
13c2b 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
13c2c 3f 31 3a 30 29 2c 20 70 61 67 65 72 5f 70 61 67  ?1:0), pager_pag
13c2d 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20  ehash(pPg)));.. 
13c2e 20 20 20 20 20 20 20 2f 2a 20 45 76 65 6e 20 69         /* Even i
13c2f 66 20 61 6e 20 49 4f 20 6f 72 20 64 69 73 6b 66  f an IO or diskf
13c30 75 6c 6c 20 65 72 72 6f 72 20 6f 63 63 75 72 72  ull error occurr
13c31 65 64 20 77 68 69 6c 65 20 6a 6f 75 72 6e 61 6c  ed while journal
13c32 6c 69 6e 67 20 74 68 65 0a 20 20 20 20 20 20 20  ling the.       
13c33 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20   ** page in the 
13c34 62 6c 6f 63 6b 20 61 62 6f 76 65 2c 20 73 65 74  block above, set
13c35 20 74 68 65 20 6e 65 65 64 2d 73 79 6e 63 20 66   the need-sync f
13c36 6c 61 67 20 66 6f 72 20 74 68 65 20 70 61 67 65  lag for the page
13c37 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 74 68  ..        ** Oth
13c38 65 72 77 69 73 65 2c 20 77 68 65 6e 20 74 68 65  erwise, when the
13c39 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
13c3a 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65  rolled back, the
13c3b 20 6c 6f 67 69 63 20 69 6e 0a 20 20 20 20 20 20   logic in.      
13c3c 20 20 2a 2a 20 70 6c 61 79 62 61 63 6b 5f 6f 6e    ** playback_on
13c3d 65 5f 70 61 67 65 28 29 20 77 69 6c 6c 20 74 68  e_page() will th
13c3e 69 6e 6b 20 74 68 61 74 20 74 68 65 20 70 61 67  ink that the pag
13c3f 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  e needs to be re
13c40 73 74 6f 72 65 64 0a 20 20 20 20 20 20 20 20 2a  stored.        *
13c41 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  * in the databas
13c42 65 20 66 69 6c 65 2e 20 41 6e 64 20 69 66 20 61  e file. And if a
13c43 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
13c44 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f  s while doing so
13c45 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  ,.        ** the
13c46 6e 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d 61 79  n corruption may
13c47 20 66 6f 6c 6c 6f 77 2e 0a 20 20 20 20 20 20 20   follow..       
13c48 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
13c49 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20  !pPager->noSync 
13c4a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67  ){.          pPg
13c4b 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
13c4c 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20  _NEED_SYNC;.    
13c4d 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65        pPager->ne
13c4e 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20  edSync = 1;.    
13c4f 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f      }..        /
13c50 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  * An error has o
13c51 63 63 75 72 72 65 64 20 77 72 69 74 69 6e 67 20  ccurred writing 
13c52 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
13c53 69 6c 65 2e 20 54 68 65 20 0a 20 20 20 20 20 20  ile. The .      
13c54 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
13c55 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20   will be rolled 
13c56 62 61 63 6b 20 62 79 20 74 68 65 20 6c 61 79 65  back by the laye
13c57 72 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20  r above..       
13c58 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
13c59 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
13c5a 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
13c5b 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
13c5c 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
13c5d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20  >nRec++;.       
13c5e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13c5f 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29  >pInJournal!=0 )
13c60 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
13c61 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
13c62 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
13c63 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  al, pPg->pgno);.
13c64 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
13c65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
13c66 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  EM );.        as
13c67 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
13c68 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  _OK || rc==SQLIT
13c69 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20  E_NOMEM );.     
13c6a 20 20 20 72 63 20 7c 3d 20 61 64 64 54 6f 53 61     rc |= addToSa
13c6b 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70  vepointBitvecs(p
13c6c 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
13c6d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
13c6e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
13c6f 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
13c70 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
13c71 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  EM );.          
13c72 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
13c73 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
13c74 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  {.        if( !p
13c75 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74  Pager->journalSt
13c76 61 72 74 65 64 20 26 26 20 21 70 50 61 67 65 72  arted && !pPager
13c77 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20  ->noSync ){.    
13c78 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73        pPg->flags
13c79 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53   |= PGHDR_NEED_S
13c7a 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70  YNC;.          p
13c7b 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
13c7c 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
13c7d 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
13c7e 45 28 28 22 41 50 50 45 4e 44 20 25 64 20 70 61  E(("APPEND %d pa
13c7f 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d 25  ge %d needSync=%
13c80 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
13c81 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50        PAGERID(pP
13c82 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f  ager), pPg->pgno
13c83 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
13c84 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47   ((pPg->flags&PG
13c85 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31  HDR_NEED_SYNC)?1
13c86 3a 30 29 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  :0)));.      }. 
13c87 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
13c88 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
13c89 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20  journal is open 
13c8a 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20  and the page is 
13c8b 6e 6f 74 20 69 6e 20 69 74 2c 0a 20 20 20 20 2a  not in it,.    *
13c8c 2a 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65  * then write the
13c8d 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f   current page to
13c8e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a   the statement j
13c8f 6f 75 72 6e 61 6c 2e 20 20 4e 6f 74 65 20 74 68  ournal.  Note th
13c90 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74  at.    ** the st
13c91 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
13c92 66 6f 72 6d 61 74 20 64 69 66 66 65 72 73 20 66  format differs f
13c93 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64  rom the standard
13c94 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 0a   journal format.
13c95 20 20 20 20 2a 2a 20 69 6e 20 74 68 61 74 20 69      ** in that i
13c96 74 20 6f 6d 69 74 73 20 74 68 65 20 63 68 65 63  t omits the chec
13c97 6b 73 75 6d 73 20 61 6e 64 20 74 68 65 20 68 65  ksums and the he
13c98 61 64 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ader..    */.   
13c99 20 69 66 28 20 73 75 62 6a 52 65 71 75 69 72 65   if( subjRequire
13c9a 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20  sPage(pPg) ){.  
13c9b 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 72      rc = subjour
13c9c 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 20  nalPage(pPg);.  
13c9d 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70    }.  }..  /* Up
13c9e 64 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73  date the databas
13c9f 65 20 73 69 7a 65 20 61 6e 64 20 72 65 74 75 72  e size and retur
13ca0 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  n..  */.  assert
13ca1 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
13ca2 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b  =PAGER_SHARED );
13ca3 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64  .  if( pPager->d
13ca4 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e 6f 20  bSize<pPg->pgno 
13ca5 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64  ){.    pPager->d
13ca6 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e  bSize = pPg->pgn
13ca7 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  o;.  }.  return 
13ca8 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72  rc;.}../*.** Mar
13ca9 6b 20 61 20 64 61 74 61 20 70 61 67 65 20 61 73  k a data page as
13caa 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 69 73   writeable. This
13cab 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65   routine must be
13cac 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 0a   called before .
13cad 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65  ** making change
13cae 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54 68 65  s to a page. The
13caf 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 68 65   caller must che
13cb0 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ck the return va
13cb1 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69 73 20  lue .** of this 
13cb2 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62 65 20  function and be 
13cb3 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f 20 63  careful not to c
13cb4 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65 20 64  hange any page d
13cb5 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a 20 74  ata unless .** t
13cb6 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
13cb7 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  rns SQLITE_OK..*
13cb8 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65  *.** The differe
13cb9 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73  nce between this
13cba 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 70 61   function and pa
13cbb 67 65 72 5f 77 72 69 74 65 28 29 20 69 73 20 74  ger_write() is t
13cbc 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63  hat this.** func
13cbd 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c 73 20  tion also deals 
13cbe 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c  with the special
13cbf 20 63 61 73 65 20 77 68 65 72 65 20 32 20 6f 72   case where 2 or
13cc0 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a 20 66   more pages.** f
13cc1 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64  it on a single d
13cc2 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e 20 74  isk sector. In t
13cc3 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63 6f 2d  his case all co-
13cc4 72 65 73 69 64 65 6e 74 20 70 61 67 65 73 0a 2a  resident pages.*
13cc5 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e  * must have been
13cc6 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
13cc7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 65 66  journal file bef
13cc8 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
13cc9 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
13cca 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f   occurs, SQLITE_
13ccb 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65  NOMEM or an IO e
13ccc 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
13ccd 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70 70 72  urned.** as appr
13cce 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72 77 69  opriate. Otherwi
13ccf 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  se, SQLITE_OK..*
13cd0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13cd1 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
13cd2 72 57 72 69 74 65 28 44 62 50 61 67 65 20 2a 70  rWrite(DbPage *p
13cd3 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72  DbPage){.  int r
13cd4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
13cd5 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70    PgHdr *pPg = p
13cd6 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20  DbPage;.  Pager 
13cd7 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
13cd8 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20 6e 50  Pager;.  Pgno nP
13cd9 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d 20 28  agePerSector = (
13cda 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
13cdb 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67 65 53  ze/pPager->pageS
13cdc 69 7a 65 29 3b 0a 0a 20 20 69 66 28 20 6e 50 61  ize);..  if( nPa
13cdd 67 65 50 65 72 53 65 63 74 6f 72 3e 31 20 29 7b  gePerSector>1 ){
13cde 0a 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 43  .    Pgno nPageC
13cdf 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f  ount;          /
13ce0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
13ce1 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62  f pages in datab
13ce2 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  ase file */.    
13ce3 50 67 6e 6f 20 70 67 31 3b 20 20 20 20 20 20 20  Pgno pg1;       
13ce4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
13ce5 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73  st page of the s
13ce6 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63  ector pPg is loc
13ce7 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20  ated on. */.    
13ce8 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20  int nPage;      
13ce9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
13cea 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 61  ber of pages sta
13ceb 72 74 69 6e 67 20 61 74 20 70 67 31 20 74 6f 20  rting at pg1 to 
13cec 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 69  journal */.    i
13ced 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
13cee 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
13cef 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
13cf0 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30  int needSync = 0
13cf1 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
13cf2 65 20 69 66 20 61 6e 79 20 70 61 67 65 20 68 61  e if any page ha
13cf3 73 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s PGHDR_NEED_SYN
13cf4 43 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 74  C */..    /* Set
13cf5 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66   the doNotSync f
13cf6 6c 61 67 20 74 6f 20 31 2e 20 54 68 69 73 20 69  lag to 1. This i
13cf7 73 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e  s because we can
13cf8 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72  not allow a jour
13cf9 6e 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 64 65  nal.    ** heade
13cfa 72 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20  r to be written 
13cfb 62 65 74 77 65 65 6e 20 74 68 65 20 70 61 67 65  between the page
13cfc 73 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 20 74  s journaled by t
13cfd 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20  his function..  
13cfe 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
13cff 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61   !MEMDB );.    a
13d00 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64  ssert( pPager->d
13d01 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20  oNotSync==0 );. 
13d02 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74     pPager->doNot
13d03 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f  Sync = 1;..    /
13d04 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 73 73  * This trick ass
13d05 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20 74  umes that both t
13d06 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64  he page-size and
13d07 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72 65   sector-size are
13d08 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67  .    ** an integ
13d09 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 49  er power of 2. I
13d0a 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20  t sets variable 
13d0b 70 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e 74  pg1 to the ident
13d0c 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20  ifier.    ** of 
13d0d 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f  the first page o
13d0e 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67  f the sector pPg
13d0f 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a   is located on..
13d10 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d      */.    pg1 =
13d11 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20   ((pPg->pgno-1) 
13d12 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63 74  & ~(nPagePerSect
13d13 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20  or-1)) + 1;..   
13d14 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
13d15 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 28  ecount(pPager, (
13d16 69 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e  int *)&nPageCoun
13d17 74 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 2d  t);.    if( pPg-
13d18 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74  >pgno>nPageCount
13d19 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20   ){.      nPage 
13d1a 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70  = (pPg->pgno - p
13d1b 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65  g1)+1;.    }else
13d1c 20 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65 50   if( (pg1+nPageP
13d1d 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67  erSector-1)>nPag
13d1e 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20  eCount ){.      
13d1f 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f 75  nPage = nPageCou
13d20 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65  nt+1-pg1;.    }e
13d21 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65  lse{.      nPage
13d22 20 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f   = nPagePerSecto
13d23 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  r;.    }.    ass
13d24 65 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20  ert(nPage>0);.  
13d25 20 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70 50    assert(pg1<=pP
13d26 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73  g->pgno);.    as
13d27 73 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65 29  sert((pg1+nPage)
13d28 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20  >pPg->pgno);..  
13d29 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e    for(ii=0; ii<n
13d2a 50 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c 49  Page && rc==SQLI
13d2b 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20  TE_OK; ii++){.  
13d2c 20 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 67      Pgno pg = pg
13d2d 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48 64  1+ii;.      PgHd
13d2e 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20  r *pPage;.      
13d2f 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e  if( pg==pPg->pgn
13d30 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 74  o || !sqlite3Bit
13d31 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e  vecTest(pPager->
13d32 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20  pInJournal, pg) 
13d33 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
13d34 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  g!=PAGER_MJ_PGNO
13d35 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20  (pPager) ){.    
13d36 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
13d37 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65  e3PagerGet(pPage
13d38 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b 0a  r, pg, &pPage);.
13d39 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
13d3a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
13d3b 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
13d3c 70 61 67 65 72 5f 77 72 69 74 65 28 70 50 61 67  pager_write(pPag
13d3d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
13d3e 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73  if( pPage->flags
13d3f 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
13d40 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
13d41 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a    needSync = 1;.
13d42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73                as
13d43 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65  sert(pPager->nee
13d44 64 53 79 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  dSync);.        
13d45 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
13d46 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
13d47 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20  ref(pPage);.    
13d48 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13d49 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  }.      }else if
13d4a 28 20 28 70 50 61 67 65 20 3d 20 70 61 67 65 72  ( (pPage = pager
13d4b 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20  _lookup(pPager, 
13d4c 70 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  pg))!=0 ){.     
13d4d 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c     if( pPage->fl
13d4e 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
13d4f 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20  YNC ){.         
13d50 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20   needSync = 1;. 
13d51 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
13d52 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
13d53 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ef(pPage);.     
13d54 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
13d55 20 49 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45   If the PGHDR_NE
13d56 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20  ED_SYNC flag is 
13d57 73 65 74 20 66 6f 72 20 61 6e 79 20 6f 66 20 74  set for any of t
13d58 68 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 0a  he nPage pages .
13d59 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20      ** starting 
13d5a 61 74 20 70 67 31 2c 20 74 68 65 6e 20 69 74 20  at pg1, then it 
13d5b 6e 65 65 64 73 20 74 6f 20 62 65 20 73 65 74 20  needs to be set 
13d5c 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e  for all of them.
13d5d 20 42 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20   Because.    ** 
13d5e 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f  writing to any o
13d5f 66 20 74 68 65 73 65 20 6e 50 61 67 65 20 70 61  f these nPage pa
13d60 67 65 73 20 6d 61 79 20 64 61 6d 61 67 65 20 74  ges may damage t
13d61 68 65 20 6f 74 68 65 72 73 2c 20 74 68 65 0a 20  he others, the. 
13d62 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69     ** journal fi
13d63 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20  le must contain 
13d64 73 79 6e 63 28 29 65 64 20 63 6f 70 69 65 73 20  sync()ed copies 
13d65 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20  of all of them. 
13d66 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79     ** before any
13d67 20 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 65 20   of them can be 
13d68 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74  written out to t
13d69 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13d6a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
13d6b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
13d6c 26 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20  & needSync ){.  
13d6d 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d      assert( !MEM
13d6e 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f  DB && pPager->no
13d6f 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Sync==0 );.     
13d70 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50   for(ii=0; ii<nP
13d71 61 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  age; ii++){.    
13d72 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65      PgHdr *pPage
13d73 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28   = pager_lookup(
13d74 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69 29 3b  pPager, pg1+ii);
13d75 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
13d76 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ge ){.          
13d77 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c 3d 20  pPage->flags |= 
13d78 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b  PGHDR_NEED_SYNC;
13d79 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
13d7a 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61  e3PagerUnref(pPa
13d7b 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ge);.        }. 
13d7c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
13d7d 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64  ert(pPager->need
13d7e 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Sync);.    }..  
13d7f 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13d80 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31 20 29  ->doNotSync==1 )
13d81 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f  ;.    pPager->do
13d82 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d  NotSync = 0;.  }
13d83 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70  else{.    rc = p
13d84 61 67 65 72 5f 77 72 69 74 65 28 70 44 62 50 61  ager_write(pDbPa
13d85 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ge);.  }.  retur
13d86 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
13d87 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
13d88 65 20 70 61 67 65 20 67 69 76 65 6e 20 69 6e 20  e page given in 
13d89 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 61 73  the argument was
13d8a 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61 73 73   previously pass
13d8b 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  ed.** to sqlite3
13d8c 50 61 67 65 72 57 72 69 74 65 28 29 2e 20 20 49  PagerWrite().  I
13d8d 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72  n other words, r
13d8e 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69 74  eturn TRUE if it
13d8f 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61   is ok.** to cha
13d90 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  nge the content 
13d91 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a  of the page..*/.
13d92 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53  #ifndef NDEBUG.S
13d93 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
13d94 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  t sqlite3PagerIs
13d95 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65  writeable(DbPage
13d96 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e   *pPg){.  return
13d97 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44   pPg->flags&PGHD
13d98 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69  R_DIRTY;.}.#endi
13d99 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20  f../*.** A call 
13d9a 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
13d9b 74 65 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20  tells the pager 
13d9c 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e  that it is not n
13d9d 65 63 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77  ecessary to.** w
13d9e 72 69 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61  rite the informa
13d9f 74 69 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67  tion on page pPg
13da0 20 62 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73   back to the dis
13da1 6b 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a  k, even though.*
13da2 2a 20 74 68 61 74 20 70 61 67 65 20 6d 69 67 68  * that page migh
13da3 74 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64  t be marked as d
13da4 69 72 74 79 2e 20 20 54 68 69 73 20 68 61 70 70  irty.  This happ
13da5 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  ens, for example
13da6 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61  , when.** the pa
13da7 67 65 20 68 61 73 20 62 65 65 6e 20 61 64 64 65  ge has been adde
13da8 64 20 61 73 20 61 20 6c 65 61 66 20 6f 66 20 74  d as a leaf of t
13da9 68 65 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20  he freelist and 
13daa 73 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e  so its.** conten
13dab 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74  t no longer matt
13dac 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  ers..**.** The o
13dad 76 65 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72  verlying softwar
13dae 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68  e layer calls th
13daf 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20  is routine when 
13db0 61 6c 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a  all of the data.
13db1 2a 2a 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20  ** on the given 
13db2 70 61 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20  page is unused. 
13db3 54 68 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20  The pager marks 
13db4 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61  the page as clea
13db5 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20  n so.** that it 
13db6 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69  does not get wri
13db7 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a  tten to disk..**
13db8 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74  .** Tests show t
13db9 68 61 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a  hat this optimiz
13dba 61 74 69 6f 6e 20 63 61 6e 20 71 75 61 64 72 75  ation can quadru
13dbb 70 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f 66  ple the speed of
13dbc 20 6c 61 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54   large .** DELET
13dbd 45 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f  E operations..*/
13dbe 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13dbf 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
13dc0 72 44 6f 6e 74 57 72 69 74 65 28 50 67 48 64 72  rDontWrite(PgHdr
13dc1 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20   *pPg){.  Pager 
13dc2 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
13dc3 50 61 67 65 72 3b 0a 20 20 69 66 28 20 28 70 50  Pager;.  if( (pP
13dc4 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  g->flags&PGHDR_D
13dc5 49 52 54 59 29 20 26 26 20 70 50 61 67 65 72 2d  IRTY) && pPager-
13dc6 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29  >nSavepoint==0 )
13dc7 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45  {.    PAGERTRACE
13dc8 28 28 22 44 4f 4e 54 5f 57 52 49 54 45 20 70 61  (("DONT_WRITE pa
13dc9 67 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20  ge %d of %d\n", 
13dca 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52  pPg->pgno, PAGER
13dcb 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20  ID(pPager)));.  
13dcc 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41    IOTRACE(("CLEA
13dcd 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  N %p %d\n", pPag
13dce 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a  er, pPg->pgno)).
13dcf 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c      pPg->flags |
13dd0 3d 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49  = PGHDR_DONT_WRI
13dd1 54 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  TE;.#ifdef SQLIT
13dd2 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
13dd3 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20    pPg->pageHash 
13dd4 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  = pager_pagehash
13dd5 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20  (pPg);.#endif.  
13dd6 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
13dd7 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
13dd8 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74  d to increment t
13dd9 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
13dda 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 0a 2a  database file .*
13ddb 2a 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  * change-counter
13ddc 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 34 2d  , stored as a 4-
13ddd 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
13dde 69 6e 74 65 67 65 72 20 73 74 61 72 74 69 6e 67  integer starting
13ddf 20 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f 66 66   at .** byte off
13de0 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 70 61  set 24 of the pa
13de1 67 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ger file..**.** 
13de2 49 66 20 74 68 65 20 69 73 44 69 72 65 63 74 4d  If the isDirectM
13de3 6f 64 65 20 66 6c 61 67 20 69 73 20 7a 65 72 6f  ode flag is zero
13de4 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 64  , then this is d
13de5 6f 6e 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 0a  one by calling .
13de6 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  ** sqlite3PagerW
13de7 72 69 74 65 28 29 20 6f 6e 20 70 61 67 65 20 31  rite() on page 1
13de8 2c 20 74 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67  , then modifying
13de9 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
13dea 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 64 61 74   the.** page dat
13deb 61 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  a. In this case 
13dec 74 68 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65  the file will be
13ded 20 75 70 64 61 74 65 64 20 77 68 65 6e 20 74 68   updated when th
13dee 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61  e current.** tra
13def 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d  nsaction is comm
13df0 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  itted..**.** The
13df1 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c   isDirectMode fl
13df2 61 67 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6e  ag may only be n
13df3 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 6c  on-zero if the l
13df4 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69  ibrary was compi
13df5 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  led.** with the 
13df6 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54  SQLITE_ENABLE_AT
13df7 4f 4d 49 43 5f 57 52 49 54 45 20 6d 61 63 72 6f  OMIC_WRITE macro
13df8 20 64 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69   defined. In thi
13df9 73 20 63 61 73 65 2c 0a 2a 2a 20 69 66 20 69 73  s case,.** if is
13dfa 44 69 72 65 63 74 20 69 73 20 6e 6f 6e 2d 7a 65  Direct is non-ze
13dfb 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74  ro, then the dat
13dfc 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 75 70  abase file is up
13dfd 64 61 74 65 64 20 64 69 72 65 63 74 6c 79 0a 2a  dated directly.*
13dfe 2a 20 62 79 20 77 72 69 74 69 6e 67 20 61 6e 20  * by writing an 
13dff 75 70 64 61 74 65 64 20 76 65 72 73 69 6f 6e 20  updated version 
13e00 6f 66 20 70 61 67 65 20 31 20 75 73 69 6e 67 20  of page 1 using 
13e01 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 0a 2a  a call to the .*
13e02 2a 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  * sqlite3OsWrite
13e03 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  () function..*/.
13e04 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
13e05 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e  _incr_changecoun
13e06 74 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65  ter(Pager *pPage
13e07 72 2c 20 69 6e 74 20 69 73 44 69 72 65 63 74 4d  r, int isDirectM
13e08 6f 64 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ode){.  int rc =
13e09 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f   SQLITE_OK;..  /
13e0a 2a 20 44 65 63 6c 61 72 65 20 61 6e 64 20 69 6e  * Declare and in
13e0b 69 74 69 61 6c 69 7a 65 20 63 6f 6e 73 74 61 6e  itialize constan
13e0c 74 20 69 6e 74 65 67 65 72 20 27 69 73 44 69 72  t integer 'isDir
13e0d 65 63 74 27 2e 20 49 66 20 74 68 65 0a 20 20 2a  ect'. If the.  *
13e0e 2a 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f  * atomic-write o
13e0f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 65  ptimization is e
13e10 6e 61 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62  nabled in this b
13e11 75 69 6c 64 2c 20 74 68 65 6e 20 69 73 44 69 72  uild, then isDir
13e12 65 63 74 0a 20 20 2a 2a 20 69 73 20 69 6e 69 74  ect.  ** is init
13e13 69 61 6c 69 7a 65 64 20 74 6f 20 74 68 65 20 76  ialized to the v
13e14 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74  alue passed as t
13e15 68 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20  he isDirectMode 
13e16 70 61 72 61 6d 65 74 65 72 0a 20 20 2a 2a 20 74  parameter.  ** t
13e17 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  o this function.
13e18 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 69   Otherwise, it i
13e19 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20  s always set to 
13e1a 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  zero..  **.  ** 
13e1b 54 68 65 20 69 64 65 61 20 69 73 20 74 68 61 74  The idea is that
13e1c 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77   if the atomic-w
13e1d 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  rite optimizatio
13e1e 6e 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 6e  n is not.  ** en
13e1f 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  abled at compile
13e20 20 74 69 6d 65 2c 20 74 68 65 20 63 6f 6d 70 69   time, the compi
13e21 6c 65 72 20 63 61 6e 20 6f 6d 69 74 20 74 68 65  ler can omit the
13e22 20 74 65 73 74 73 20 6f 66 0a 20 20 2a 2a 20 27   tests of.  ** '
13e23 69 73 44 69 72 65 63 74 27 20 62 65 6c 6f 77 2c  isDirect' below,
13e24 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20   as well as the 
13e25 62 6c 6f 63 6b 20 65 6e 63 6c 6f 73 65 64 20 69  block enclosed i
13e26 6e 20 74 68 65 0a 20 20 2a 2a 20 22 69 66 28 20  n the.  ** "if( 
13e27 69 73 44 69 72 65 63 74 20 29 22 20 63 6f 6e 64  isDirect )" cond
13e28 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66 6e  ition..  */.#ifn
13e29 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
13e2a 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 23  E_ATOMIC_WRITE.#
13e2b 20 64 65 66 69 6e 65 20 44 49 52 45 43 54 5f 4d   define DIRECT_M
13e2c 4f 44 45 20 30 0a 20 20 61 73 73 65 72 74 28 20  ODE 0.  assert( 
13e2d 69 73 44 69 72 65 63 74 4d 6f 64 65 3d 3d 30 20  isDirectMode==0 
13e2e 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
13e2f 4d 45 54 45 52 28 69 73 44 69 72 65 63 74 4d 6f  METER(isDirectMo
13e30 64 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  de);.#else.# def
13e31 69 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45 20  ine DIRECT_MODE 
13e32 69 73 44 69 72 65 63 74 4d 6f 64 65 0a 23 65 6e  isDirectMode.#en
13e33 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70  dif..  assert( p
13e34 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
13e35 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a  GER_RESERVED );.
13e36 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 63    if( !pPager->c
13e37 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26  hangeCountDone &
13e38 26 20 41 4c 57 41 59 53 28 70 50 61 67 65 72 2d  & ALWAYS(pPager-
13e39 3e 64 62 53 69 7a 65 3e 30 29 20 29 7b 0a 20 20  >dbSize>0) ){.  
13e3a 20 20 50 67 48 64 72 20 2a 70 50 67 48 64 72 3b    PgHdr *pPgHdr;
13e3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13e3c 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20  /* Reference to 
13e3d 70 61 67 65 20 31 20 2a 2f 0a 20 20 20 20 75 33  page 1 */.    u3
13e3e 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72  2 change_counter
13e3f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ;           /* I
13e40 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 6f 66 20  nitial value of 
13e41 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66  change-counter f
13e42 69 65 6c 64 20 2a 2f 0a 0a 20 20 20 20 61 73 73  ield */..    ass
13e43 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65  ert( !pPager->te
13e44 6d 70 46 69 6c 65 20 26 26 20 69 73 4f 70 65 6e  mpFile && isOpen
13e45 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a  (pPager->fd) );.
13e46 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67  .    /* Open pag
13e47 65 20 31 20 6f 66 20 74 68 65 20 66 69 6c 65 20  e 1 of the file 
13e48 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a  for writing. */.
13e49 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13e4a 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c  PagerGet(pPager,
13e4b 20 31 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20   1, &pPgHdr);.  
13e4c 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64 72    assert( pPgHdr
13e4d 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  ==0 || rc==SQLIT
13e4e 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  E_OK );..    /* 
13e4f 49 66 20 70 61 67 65 20 6f 6e 65 20 77 61 73 20  If page one was 
13e50 66 65 74 63 68 65 64 20 73 75 63 63 65 73 73 66  fetched successf
13e51 75 6c 6c 79 2c 20 61 6e 64 20 74 68 69 73 20 66  ully, and this f
13e52 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 20  unction is not. 
13e53 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20     ** operating 
13e54 69 6e 20 64 69 72 65 63 74 2d 6d 6f 64 65 2c 20  in direct-mode, 
13e55 6d 61 6b 65 20 70 61 67 65 20 31 20 77 72 69 74  make page 1 writ
13e56 61 62 6c 65 2e 20 20 57 68 65 6e 20 6e 6f 74 20  able.  When not 
13e57 69 6e 20 0a 20 20 20 20 2a 2a 20 64 69 72 65 63  in .    ** direc
13e58 74 20 6d 6f 64 65 2c 20 70 61 67 65 20 31 20 69  t mode, page 1 i
13e59 73 20 61 6c 77 61 79 73 20 68 65 6c 64 20 69 6e  s always held in
13e5a 20 63 61 63 68 65 20 61 6e 64 20 68 65 6e 63 65   cache and hence
13e5b 20 74 68 65 20 50 61 67 65 72 47 65 74 28 29 0a   the PagerGet().
13e5c 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 69 73 20      ** above is 
13e5d 61 6c 77 61 79 73 20 73 75 63 63 65 73 73 66 75  always successfu
13e5e 6c 20 2d 20 68 65 6e 63 65 20 74 68 65 20 41 4c  l - hence the AL
13e5f 57 41 59 53 20 6f 6e 20 72 63 3d 3d 53 51 4c 49  WAYS on rc==SQLI
13e60 54 45 5f 4f 4b 2e 0a 20 20 20 20 2a 2f 0a 20 20  TE_OK..    */.  
13e61 20 20 69 66 28 20 21 44 49 52 45 43 54 5f 4d 4f    if( !DIRECT_MO
13e62 44 45 20 26 26 20 41 4c 57 41 59 53 28 72 63 3d  DE && ALWAYS(rc=
13e63 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20  =SQLITE_OK) ){. 
13e64 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13e65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 48  3PagerWrite(pPgH
13e66 64 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  dr);.    }..    
13e67 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
13e68 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e  K ){.      /* In
13e69 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75  crement the valu
13e6a 65 20 6a 75 73 74 20 72 65 61 64 20 61 6e 64 20  e just read and 
13e6b 77 72 69 74 65 20 69 74 20 62 61 63 6b 20 74 6f  write it back to
13e6c 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20 20 20   byte 24. */.   
13e6d 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65     change_counte
13e6e 72 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62  r = sqlite3Get4b
13e6f 79 74 65 28 28 75 38 2a 29 70 50 61 67 65 72 2d  yte((u8*)pPager-
13e70 3e 64 62 46 69 6c 65 56 65 72 73 29 3b 0a 20 20  >dbFileVers);.  
13e71 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74      change_count
13e72 65 72 2b 2b 3b 0a 20 20 20 20 20 20 70 75 74 33  er++;.      put3
13e73 32 62 69 74 73 28 28 28 63 68 61 72 2a 29 70 50  2bits(((char*)pP
13e74 67 48 64 72 2d 3e 70 44 61 74 61 29 2b 32 34 2c  gHdr->pData)+24,
13e75 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 29   change_counter)
13e76 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72  ;..      /* If r
13e77 75 6e 6e 69 6e 67 20 69 6e 20 64 69 72 65 63 74  unning in direct
13e78 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 74 68 65   mode, write the
13e79 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67   contents of pag
13e7a 65 20 31 20 74 6f 20 74 68 65 20 66 69 6c 65 2e  e 1 to the file.
13e7b 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 44 49   */.      if( DI
13e7c 52 45 43 54 5f 4d 4f 44 45 20 29 7b 0a 20 20 20  RECT_MODE ){.   
13e7d 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20       const void 
13e7e 2a 7a 42 75 66 20 3d 20 70 50 67 48 64 72 2d 3e  *zBuf = pPgHdr->
13e7f 70 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 61  pData;.        a
13e80 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64  ssert( pPager->d
13e81 62 46 69 6c 65 53 69 7a 65 3e 30 20 29 3b 0a 20  bFileSize>0 );. 
13e82 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
13e83 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
13e84 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70 50 61  r->fd, zBuf, pPa
13e85 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 30  ger->pageSize, 0
13e86 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
13e87 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
13e88 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
13e89 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e  ->changeCountDon
13e8a 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  e = 1;.        }
13e8b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
13e8c 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68        pPager->ch
13e8d 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20  angeCountDone = 
13e8e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
13e8f 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65  ..    /* Release
13e90 20 74 68 65 20 70 61 67 65 20 72 65 66 65 72 65   the page refere
13e91 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  nce. */.    sqli
13e92 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
13e93 67 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74  gHdr);.  }.  ret
13e94 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
13e95 20 53 79 6e 63 20 74 68 65 20 70 61 67 65 72 20   Sync the pager 
13e96 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20 54 68  file to disk. Th
13e97 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f  is is a no-op fo
13e98 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65  r in-memory file
13e99 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20 77 69  s.** or pages wi
13e9a 74 68 20 74 68 65 20 50 61 67 65 72 2e 6e 6f 53  th the Pager.noS
13e9b 79 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a  ync flag set..**
13e9c 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
13e9d 6c 2c 20 6f 72 20 63 61 6c 6c 65 64 20 6f 6e 20  l, or called on 
13e9e 61 20 70 61 67 65 72 20 66 6f 72 20 77 68 69 63  a pager for whic
13e9f 68 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2c  h it is a no-op,
13ea0 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   this.** functio
13ea1 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  n returns SQLITE
13ea2 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  _OK. Otherwise, 
13ea3 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  an IO error code
13ea4 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
13ea5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13ea6 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
13ea7 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67  Sync(Pager *pPag
13ea8 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20  er){.  int rc;  
13ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13eaa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
13eab 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
13eac 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29  assert( !MEMDB )
13ead 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
13eae 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63  noSync ){.    rc
13eaf 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
13eb0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
13eb1 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50  sqlite3OsSync(pP
13eb2 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72  ager->fd, pPager
13eb3 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20  ->sync_flags);. 
13eb4 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
13eb5 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68  }../*.** Sync th
13eb6 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
13eb7 66 6f 72 20 74 68 65 20 70 61 67 65 72 20 70 50  for the pager pP
13eb8 61 67 65 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f  ager. zMaster po
13eb9 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 61 6d 65  ints to the name
13eba 0a 2a 2a 20 6f 66 20 61 20 6d 61 73 74 65 72 20  .** of a master 
13ebb 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61  journal file tha
13ebc 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74  t should be writ
13ebd 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e 64  ten into the ind
13ebe 69 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e  ividual.** journ
13ebf 61 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72  al file. zMaster
13ec0 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68   may be NULL, wh
13ec1 69 63 68 20 69 73 20 69 6e 74 65 72 70 72 65 74  ich is interpret
13ec2 65 64 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a  ed as no master.
13ec3 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69  ** journal (a si
13ec4 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72  ngle database tr
13ec5 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a  ansaction)..**.*
13ec6 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65  * This routine e
13ec7 6e 73 75 72 65 73 20 74 68 61 74 3a 0a 2a 2a 0a  nsures that:.**.
13ec8 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61 62  **   * The datab
13ec9 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 2d  ase file change-
13eca 63 6f 75 6e 74 65 72 20 69 73 20 75 70 64 61 74  counter is updat
13ecb 65 64 2c 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6a  ed,.**   * the j
13ecc 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64  ournal is synced
13ecd 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 74 6f   (unless the ato
13ece 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69  mic-write optimi
13ecf 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64 29 2c  zation is used),
13ed0 0a 2a 2a 20 20 20 2a 20 61 6c 6c 20 64 69 72 74  .**   * all dirt
13ed1 79 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74  y pages are writ
13ed2 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62  ten to the datab
13ed3 61 73 65 20 66 69 6c 65 2c 20 0a 2a 2a 20 20 20  ase file, .**   
13ed4 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
13ed5 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64  ile is truncated
13ed6 20 28 69 66 20 72 65 71 75 69 72 65 64 29 2c 20   (if required), 
13ed7 61 6e 64 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64  and.**   * the d
13ed8 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 79 6e  atabase file syn
13ed9 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ced. .**.** The 
13eda 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68 61 74 20  only thing that 
13edb 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69  remains to commi
13edc 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  t the transactio
13edd 6e 20 69 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65  n is to finalize
13ede 20 0a 2a 2a 20 28 64 65 6c 65 74 65 2c 20 74 72   .** (delete, tr
13edf 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20 74  uncate or zero t
13ee0 68 65 20 66 69 72 73 74 20 70 61 72 74 20 6f 66  he first part of
13ee1 29 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  ) the journal fi
13ee2 6c 65 20 28 6f 72 20 0a 2a 2a 20 64 65 6c 65 74  le (or .** delet
13ee3 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
13ee4 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 73 70 65  rnal file if spe
13ee5 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e  cified)..**.** N
13ee6 6f 74 65 20 74 68 61 74 20 69 66 20 7a 4d 61 73  ote that if zMas
13ee7 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20  ter==NULL, this 
13ee8 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72 77 72 69  does not overwri
13ee9 74 65 20 61 20 70 72 65 76 69 6f 75 73 20 76 61  te a previous va
13eea 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f  lue.** passed to
13eeb 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72   an sqlite3Pager
13eec 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29  CommitPhaseOne()
13eed 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   call..**.** If 
13eee 74 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65  the final parame
13eef 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 2d 20 69  ter - noSync - i
13ef0 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65  s true, then the
13ef1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
13ef2 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e 6f 74 20  tself.** is not 
13ef3 73 79 6e 63 65 64 2e 20 54 68 65 20 63 61 6c 6c  synced. The call
13ef4 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c  er must call sql
13ef5 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 29 20  ite3PagerSync() 
13ef6 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a 2a 20 73  directly to.** s
13ef7 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65  ync the database
13ef8 20 66 69 6c 65 20 62 65 66 6f 72 65 20 63 61 6c   file before cal
13ef9 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 61 73 65  ling CommitPhase
13efa 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 74 65 20  Two() to delete 
13efb 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  the.** journal f
13efc 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 73 65  ile in this case
13efd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
13efe 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
13eff 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f  agerCommitPhaseO
13f00 6e 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61  ne(.  Pager *pPa
13f01 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  ger,            
13f02 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f        /* Pager o
13f03 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  bject */.  const
13f04 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20   char *zMaster, 
13f05 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
13f06 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20 6d   not NULL, the m
13f07 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
13f08 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 53 79  me */.  int noSy
13f09 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nc              
13f0a 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
13f0b 74 6f 20 6f 6d 69 74 20 74 68 65 20 78 53 79 6e  to omit the xSyn
13f0c 63 20 6f 6e 20 74 68 65 20 64 62 20 66 69 6c 65  c on the db file
13f0d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
13f0e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
13f0f 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
13f10 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a  rn code */..  /*
13f11 20 54 68 65 20 64 62 4f 72 69 67 53 69 7a 65 20   The dbOrigSize 
13f12 69 73 20 6e 65 76 65 72 20 73 65 74 20 69 66 20  is never set if 
13f13 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46  journal_mode=OFF
13f14 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
13f15 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
13f16 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e!=PAGER_JOURNAL
13f17 4d 4f 44 45 5f 4f 46 46 20 7c 7c 20 70 50 61 67  MODE_OFF || pPag
13f18 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d  er->dbOrigSize==
13f19 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20  0 );..  /* If a 
13f1a 70 72 69 6f 72 20 65 72 72 6f 72 20 6f 63 63 75  prior error occu
13f1b 72 72 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69  rred, this routi
13f1c 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  ne should not be
13f1d 20 63 61 6c 6c 65 64 2e 20 20 52 4f 4c 4c 42 41   called.  ROLLBA
13f1e 43 4b 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 61  CK.  ** is the a
13f1f 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 70 6f  ppropriate respo
13f20 6e 73 65 20 74 6f 20 61 6e 20 65 72 72 6f 72 2c  nse to an error,
13f21 20 6e 6f 74 20 43 4f 4d 4d 49 54 2e 20 20 47 75   not COMMIT.  Gu
13f22 61 72 64 20 61 67 61 69 6e 73 74 0a 20 20 2a 2a  ard against.  **
13f23 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 73 20 62   coding errors b
13f24 79 20 72 65 70 65 61 74 69 6e 67 20 74 68 65 20  y repeating the 
13f25 70 72 69 6f 72 20 65 72 72 6f 72 2e 20 2a 2f 0a  prior error. */.
13f26 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67    if( NEVER(pPag
13f27 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72  er->errCode) ) r
13f28 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72  eturn pPager->er
13f29 72 43 6f 64 65 3b 0a 0a 20 20 50 41 47 45 52 54  rCode;..  PAGERT
13f2a 52 41 43 45 28 28 22 44 41 54 41 42 41 53 45 20  RACE(("DATABASE 
13f2b 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a 4d  SYNC: File=%s zM
13f2c 61 73 74 65 72 3d 25 73 20 6e 53 69 7a 65 3d 25  aster=%s nSize=%
13f2d 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70 50 61  d\n", .      pPa
13f2e 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20  ger->zFilename, 
13f2f 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d  zMaster, pPager-
13f30 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20 20 69 66  >dbSize));..  if
13f31 28 20 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65  ( MEMDB && pPage
13f32 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 7b  r->dbModified ){
13f33 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
13f34 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  is an in-memory 
13f35 64 62 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73 20  db, or no pages 
13f36 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65  have been writte
13f37 6e 20 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20 20  n to, or this.  
13f38 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61    ** function ha
13f39 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  s already been c
13f3a 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 6d 6f 73  alled, it is mos
13f3b 74 6c 79 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 6f  tly a no-op.  Ho
13f3c 77 65 76 65 72 2c 20 61 6e 79 0a 20 20 20 20 2a  wever, any.    *
13f3d 2a 20 62 61 63 6b 75 70 20 69 6e 20 70 72 6f 67  * backup in prog
13f3e 72 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62 65  ress needs to be
13f3f 20 72 65 73 74 61 72 74 65 64 2e 0a 20 20 20 20   restarted..    
13f40 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 61  */.    sqlite3Ba
13f41 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61 67  ckupRestart(pPag
13f42 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20  er->pBackup);.  
13f43 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72  }else if( pPager
13f44 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 53  ->state!=PAGER_S
13f45 59 4e 43 45 44 20 26 26 20 70 50 61 67 65 72 2d  YNCED && pPager-
13f46 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 7b 0a 0a  >dbModified ){..
13f47 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f      /* The follo
13f48 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70 64 61 74  wing block updat
13f49 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f  es the change-co
13f4a 75 6e 74 65 72 2e 20 45 78 61 63 74 6c 79 20 68  unter. Exactly h
13f4b 6f 77 20 69 74 0a 20 20 20 20 2a 2a 20 64 6f 65  ow it.    ** doe
13f4c 73 20 74 68 69 73 20 64 65 70 65 6e 64 73 20 6f  s this depends o
13f4d 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  n whether or not
13f4e 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 61   the atomic-upda
13f4f 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a  te optimization.
13f50 20 20 20 20 2a 2a 20 77 61 73 20 65 6e 61 62 6c      ** was enabl
13f51 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69  ed at compile ti
13f52 6d 65 2c 20 61 6e 64 20 69 66 20 74 68 69 73 20  me, and if this 
13f53 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 65 65 74  transaction meet
13f54 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 75  s the .    ** ru
13f55 6e 74 69 6d 65 20 63 72 69 74 65 72 69 61 20 74  ntime criteria t
13f56 6f 20 75 73 65 20 74 68 65 20 6f 70 65 72 61 74  o use the operat
13f57 69 6f 6e 3a 20 0a 20 20 20 20 2a 2a 0a 20 20 20  ion: .    **.   
13f58 20 2a 2a 20 20 20 20 2a 20 54 68 65 20 66 69 6c   **    * The fil
13f59 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74  e-system support
13f5a 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69  s the atomic-wri
13f5b 74 65 20 70 72 6f 70 65 72 74 79 20 66 6f 72 0a  te property for.
13f5c 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f 63      **      bloc
13f5d 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67 65 2d  ks of size page-
13f5e 73 69 7a 65 2c 20 61 6e 64 20 0a 20 20 20 20 2a  size, and .    *
13f5f 2a 20 20 20 20 2a 20 54 68 69 73 20 63 6f 6d 6d  *    * This comm
13f60 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  it is not part o
13f61 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20 74  f a multi-file t
13f62 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a  ransaction, and.
13f63 20 20 20 20 2a 2a 20 20 20 20 2a 20 45 78 61 63      **    * Exac
13f64 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68 61 73  tly one page has
13f65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 61   been modified a
13f66 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68 65 20  nd store in the 
13f67 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20  journal file..  
13f68 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74    **.    ** If t
13f69 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
13f6a 77 61 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20  was not enabled 
13f6b 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c  at compile time,
13f6c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a   then the.    **
13f6d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e   pager_incr_chan
13f6e 67 65 63 6f 75 6e 74 65 72 28 29 20 66 75 6e 63  gecounter() func
13f6f 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
13f70 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61  o update the cha
13f71 6e 67 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e 74  nge.    ** count
13f72 65 72 20 69 6e 20 27 69 6e 64 69 72 65 63 74 2d  er in 'indirect-
13f73 6d 6f 64 65 27 2e 20 49 66 20 74 68 65 20 6f 70  mode'. If the op
13f74 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 63 6f  timization is co
13f75 6d 70 69 6c 65 64 20 69 6e 20 62 75 74 0a 20 20  mpiled in but.  
13f76 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 70 70 6c    ** is not appl
13f77 69 63 61 62 6c 65 20 74 6f 20 74 68 69 73 20 74  icable to this t
13f78 72 61 6e 73 61 63 74 69 6f 6e 2c 20 63 61 6c 6c  ransaction, call
13f79 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43   sqlite3JournalC
13f7a 72 65 61 74 65 28 29 0a 20 20 20 20 2a 2a 20 74  reate().    ** t
13f7b 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
13f7c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73  journal file has
13f7d 20 61 63 74 75 61 6c 6c 79 20 62 65 65 6e 20 63   actually been c
13f7e 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 61 6c  reated, then cal
13f7f 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 69  l.    ** pager_i
13f80 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65  ncr_changecounte
13f81 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74 68  r() to update th
13f82 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
13f83 20 69 6e 20 69 6e 64 69 72 65 63 74 0a 20 20 20   in indirect.   
13f84 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20 20 20 2a   ** mode. .    *
13f85 2a 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69  *.    ** Otherwi
13f86 73 65 2c 20 69 66 20 74 68 65 20 6f 70 74 69 6d  se, if the optim
13f87 69 7a 61 74 69 6f 6e 20 69 73 20 62 6f 74 68 20  ization is both 
13f88 65 6e 61 62 6c 65 64 20 61 6e 64 20 61 70 70 6c  enabled and appl
13f89 69 63 61 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 74  icable,.    ** t
13f8a 68 65 6e 20 63 61 6c 6c 20 70 61 67 65 72 5f 69  hen call pager_i
13f8b 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65  ncr_changecounte
13f8c 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74 68  r() to update th
13f8d 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
13f8e 0a 20 20 20 20 2a 2a 20 69 6e 20 27 64 69 72 65  .    ** in 'dire
13f8f 63 74 27 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69  ct' mode. In thi
13f90 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72 6e  s case the journ
13f91 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 6e 65 76  al file will nev
13f92 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 63 72 65  er be.    ** cre
13f93 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 74 72  ated for this tr
13f94 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 2a  ansaction..    *
13f95 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
13f96 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
13f97 49 54 45 0a 20 20 20 20 50 67 48 64 72 20 2a 70  ITE.    PgHdr *p
13f98 50 67 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  Pg;.    assert( 
13f99 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
13f9a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a  fd) || pPager->j
13f9b 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
13f9c 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
13f9d 46 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d  F );.    if( !zM
13f9e 61 73 74 65 72 20 26 26 20 69 73 4f 70 65 6e 28  aster && isOpen(
13f9f 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 0a 20 20  pPager->jfd) .  
13fa0 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f     && pPager->jo
13fa1 75 72 6e 61 6c 4f 66 66 3d 3d 6a 72 6e 6c 42 75  urnalOff==jrnlBu
13fa2 66 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29  fferSize(pPager)
13fa3 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 72   .     && pPager
13fa4 2d 3e 64 62 53 69 7a 65 3e 3d 70 50 61 67 65 72  ->dbSize>=pPager
13fa5 2d 3e 64 62 46 69 6c 65 53 69 7a 65 0a 20 20 20  ->dbFileSize.   
13fa6 20 20 26 26 20 28 30 3d 3d 28 70 50 67 20 3d 20    && (0==(pPg = 
13fa7 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72  sqlite3PcacheDir
13fa8 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e 70  tyList(pPager->p
13fa9 50 43 61 63 68 65 29 29 20 7c 7c 20 30 3d 3d 70  PCache)) || 0==p
13faa 50 67 2d 3e 70 44 69 72 74 79 29 0a 20 20 20 20  Pg->pDirty).    
13fab 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61  ){.      /* Upda
13fac 74 65 20 74 68 65 20 64 62 20 66 69 6c 65 20 63  te the db file c
13fad 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 76 69  hange counter vi
13fae 61 20 74 68 65 20 64 69 72 65 63 74 2d 77 72 69  a the direct-wri
13faf 74 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 0a  te method. The .
13fb0 20 20 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69        ** followi
13fb1 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f 64  ng call will mod
13fb2 69 66 79 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ify the in-memor
13fb3 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  y representation
13fb4 20 6f 66 20 70 61 67 65 20 31 20 0a 20 20 20 20   of page 1 .    
13fb5 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 64 65 20    ** to include 
13fb6 74 68 65 20 75 70 64 61 74 65 64 20 63 68 61 6e  the updated chan
13fb7 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 20 74  ge counter and t
13fb8 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 20 31  hen write page 1
13fb9 20 0a 20 20 20 20 20 20 2a 2a 20 64 69 72 65 63   .      ** direc
13fba 74 6c 79 20 74 6f 20 74 68 65 20 64 61 74 61 62  tly to the datab
13fbb 61 73 65 20 66 69 6c 65 2e 20 42 65 63 61 75 73  ase file. Becaus
13fbc 65 20 6f 66 20 74 68 65 20 61 74 6f 6d 69 63 2d  e of the atomic-
13fbd 77 72 69 74 65 20 0a 20 20 20 20 20 20 2a 2a 20  write .      ** 
13fbe 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20  property of the 
13fbf 68 6f 73 74 20 66 69 6c 65 2d 73 79 73 74 65 6d  host file-system
13fc0 2c 20 74 68 69 73 20 69 73 20 73 61 66 65 2e 0a  , this is safe..
13fc1 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
13fc2 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63  c = pager_incr_c
13fc3 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61  hangecounter(pPa
13fc4 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 7d 65 6c  ger, 1);.    }el
13fc5 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  se{.      rc = s
13fc6 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65  qlite3JournalCre
13fc7 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  ate(pPager->jfd)
13fc8 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
13fc9 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13fca 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
13fcb 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74  incr_changecount
13fcc 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20  er(pPager, 0);. 
13fcd 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6c       }.    }.#el
13fce 73 65 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  se.    rc = page
13fcf 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75  r_incr_changecou
13fd0 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b  nter(pPager, 0);
13fd1 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20  .#endif.    if( 
13fd2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
13fd3 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73  goto commit_phas
13fd4 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20  e_one_exit;..   
13fd5 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72 61 6e   /* If this tran
13fd6 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 65  saction has made
13fd7 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 6d   the database sm
13fd8 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20  aller, then all 
13fd9 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 62 65 69  pages.    ** bei
13fda 6e 67 20 64 69 73 63 61 72 64 65 64 20 62 79 20  ng discarded by 
13fdb 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 6d  the truncation m
13fdc 75 73 74 20 62 65 20 77 72 69 74 74 65 6e 20 74  ust be written t
13fdd 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20  o the journal.  
13fde 20 20 2a 2a 20 66 69 6c 65 2e 20 54 68 69 73 20    ** file. This 
13fdf 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20  can only happen 
13fe0 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d  in auto-vacuum m
13fe1 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ode..    **.    
13fe2 2a 2a 20 42 65 66 6f 72 65 20 72 65 61 64 69 6e  ** Before readin
13fe3 67 20 74 68 65 20 70 61 67 65 73 20 77 69 74 68  g the pages with
13fe4 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6c 61   page numbers la
13fe5 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20  rger than the . 
13fe6 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 76 61     ** current va
13fe7 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 53  lue of Pager.dbS
13fe8 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a 65 20  ize, set dbSize 
13fe9 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c 75  back to the valu
13fea 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 69 74  e.    ** that it
13feb 20 74 6f 6f 6b 20 61 74 20 74 68 65 20 73 74 61   took at the sta
13fec 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61  rt of the transa
13fed 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65  ction. Otherwise
13fee 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c  , the.    ** cal
13fef 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67  ls to sqlite3Pag
13ff0 65 72 47 65 74 28 29 20 72 65 74 75 72 6e 20 7a  erGet() return z
13ff1 65 72 6f 65 64 20 70 61 67 65 73 20 69 6e 73 74  eroed pages inst
13ff2 65 61 64 20 6f 66 20 0a 20 20 20 20 2a 2a 20 72  ead of .    ** r
13ff3 65 61 64 69 6e 67 20 64 61 74 61 20 66 72 6f 6d  eading data from
13ff4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13ff5 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  le..    **.    *
13ff6 2a 20 57 68 65 6e 20 6a 6f 75 72 6e 61 6c 5f 6d  * When journal_m
13ff7 6f 64 65 3d 3d 4f 46 46 20 74 68 65 20 64 62 4f  ode==OFF the dbO
13ff8 72 69 67 53 69 7a 65 20 69 73 20 61 6c 77 61 79  rigSize is alway
13ff9 73 20 7a 65 72 6f 2c 20 73 6f 20 74 68 69 73 0a  s zero, so this.
13ffa 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 6e 65 76      ** block nev
13ffb 65 72 20 72 75 6e 73 20 69 66 20 6a 6f 75 72 6e  er runs if journ
13ffc 61 6c 5f 6d 6f 64 65 3d 4f 46 46 2e 0a 20 20 20  al_mode=OFF..   
13ffd 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
13ffe 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
13fff 55 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  UM.    if( pPage
14000 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 67 65 72  r->dbSize<pPager
14001 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 0a 20 20  ->dbOrigSize .  
14002 20 20 20 26 26 20 41 4c 57 41 59 53 28 70 50 61     && ALWAYS(pPa
14003 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
14004 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  !=PAGER_JOURNALM
14005 4f 44 45 5f 4f 46 46 29 0a 20 20 20 20 29 7b 0a  ODE_OFF).    ){.
14006 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 20 20 20        Pgno i;   
14007 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14009 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
1400a 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f  able */.      co
1400b 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69 70 20 3d  nst Pgno iSkip =
1400c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70   PAGER_MJ_PGNO(p
1400d 50 61 67 65 72 29 3b 20 2f 2a 20 50 65 6e 64 69  Pager); /* Pendi
1400e 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20 2a 2f 0a  ng lock page */.
1400f 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f        const Pgno
14010 20 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72   dbSize = pPager
14011 2d 3e 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20  ->dbSize;       
14012 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6d 61 67  /* Database imag
14013 65 20 73 69 7a 65 20 2a 2f 20 0a 20 20 20 20 20  e size */ .     
14014 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20   pPager->dbSize 
14015 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67  = pPager->dbOrig
14016 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 6f 72 28  Size;.      for(
14017 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 69 3c 3d   i=dbSize+1; i<=
14018 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69  pPager->dbOrigSi
14019 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20  ze; i++ ){.     
1401a 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 42     if( !sqlite3B
1401b 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72  itvecTest(pPager
1401c 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69 29  ->pInJournal, i)
1401d 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29 7b 0a   && i!=iSkip ){.
1401e 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72 20            PgHdr 
1401f 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  *pPage;         
14020 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 6a      /* Page to j
14021 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 20 20  ournal */.      
14022 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
14023 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c  PagerGet(pPager,
14024 20 69 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20   i, &pPage);.   
14025 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
14026 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
14027 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65  commit_phase_one
14028 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 20  _exit;.         
14029 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1402a 65 72 57 72 69 74 65 28 70 50 61 67 65 29 3b 0a  erWrite(pPage);.
1402b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1402c 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67  3PagerUnref(pPag
1402d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  e);.          if
1402e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1402f 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68  ) goto commit_ph
14030 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20  ase_one_exit;.  
14031 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20        }.      } 
14032 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64  .      pPager->d
14033 62 53 69 7a 65 20 3d 20 64 62 53 69 7a 65 3b 0a  bSize = dbSize;.
14034 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
14035 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6d    /* Write the m
14036 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
14037 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  me into the jour
14038 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 20 6d  nal file. If a m
14039 61 73 74 65 72 20 0a 20 20 20 20 2a 2a 20 6a 6f  aster .    ** jo
1403a 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
1403b 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
1403c 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
1403d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 0a 20  journal file, . 
1403e 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a 4d 61 73     ** or if zMas
1403f 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 6e 6f 20  ter is NULL (no 
14040 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 29 2c  master journal),
14041 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 20   then this call 
14042 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20  is a no-op..    
14043 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74  */.    rc = writ
14044 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70  eMasterJournal(p
14045 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b  Pager, zMaster);
14046 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
14047 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f  ITE_OK ) goto co
14048 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65  mmit_phase_one_e
14049 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 53 79 6e  xit;..    /* Syn
1404a 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  c the journal fi
1404b 6c 65 2e 20 49 66 20 74 68 65 20 61 74 6f 6d 69  le. If the atomi
1404c 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69 7a  c-update optimiz
1404d 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 0a 20  ation is being. 
1404e 20 20 20 2a 2a 20 75 73 65 64 2c 20 74 68 69 73     ** used, this
1404f 20 63 61 6c 6c 20 77 69 6c 6c 20 6e 6f 74 20 63   call will not c
14050 72 65 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  reate the journa
14051 6c 20 66 69 6c 65 20 6f 72 20 70 65 72 66 6f 72  l file or perfor
14052 6d 20 61 6e 79 0a 20 20 20 20 2a 2a 20 72 65 61  m any.    ** rea
14053 6c 20 49 4f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  l IO..    */.   
14054 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61   rc = syncJourna
14055 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  l(pPager);.    i
14056 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
14057 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70   ) goto commit_p
14058 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a  hase_one_exit;..
14059 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6c 6c      /* Write all
1405a 20 64 69 72 74 79 20 70 61 67 65 73 20 74 6f 20   dirty pages to 
1405b 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1405c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70  e. */.    rc = p
1405d 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c  ager_write_pagel
1405e 69 73 74 28 73 71 6c 69 74 65 33 50 63 61 63 68  ist(sqlite3Pcach
1405f 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 65  eDirtyList(pPage
14060 72 2d 3e 70 50 43 61 63 68 65 29 29 3b 0a 20 20  r->pPCache));.  
14061 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
14062 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73  _OK ){.      ass
14063 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
14064 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b  IOERR_BLOCKED );
14065 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6d 6d  .      goto comm
14066 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69  it_phase_one_exi
14067 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  t;.    }.    sql
14068 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41  ite3PcacheCleanA
14069 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ll(pPager->pPCac
1406a 68 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  he);..    /* If 
1406b 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b  the file on disk
1406c 20 69 73 20 6e 6f 74 20 74 68 65 20 73 61 6d 65   is not the same
1406d 20 73 69 7a 65 20 61 73 20 74 68 65 20 64 61 74   size as the dat
1406e 61 62 61 73 65 20 69 6d 61 67 65 2c 0a 20 20 20  abase image,.   
1406f 20 2a 2a 20 74 68 65 6e 20 75 73 65 20 70 61 67   ** then use pag
14070 65 72 5f 74 72 75 6e 63 61 74 65 20 74 6f 20 67  er_truncate to g
14071 72 6f 77 20 6f 72 20 73 68 72 69 6e 6b 20 74 68  row or shrink th
14072 65 20 66 69 6c 65 20 68 65 72 65 2e 0a 20 20 20  e file here..   
14073 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67   */.    if( pPag
14074 65 72 2d 3e 64 62 53 69 7a 65 21 3d 70 50 61 67  er->dbSize!=pPag
14075 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29  er->dbFileSize )
14076 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e 4e 65  {.      Pgno nNe
14077 77 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69  w = pPager->dbSi
14078 7a 65 20 2d 20 28 70 50 61 67 65 72 2d 3e 64 62  ze - (pPager->db
14079 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50  Size==PAGER_MJ_P
1407a 47 4e 4f 28 70 50 61 67 65 72 29 29 3b 0a 20 20  GNO(pPager));.  
1407b 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
1407c 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
1407d 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20  _EXCLUSIVE );.  
1407e 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74      rc = pager_t
1407f 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20  runcate(pPager, 
14080 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 69 66 28  nNew);.      if(
14081 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
14082 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61   goto commit_pha
14083 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20  se_one_exit;.   
14084 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c   }..    /* Final
14085 6c 79 2c 20 73 79 6e 63 20 74 68 65 20 64 61 74  ly, sync the dat
14086 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20  abase file. */. 
14087 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e     if( !pPager->
14088 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f 53 79 6e  noSync && !noSyn
14089 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  c ){.      rc = 
1408a 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50  sqlite3OsSync(pP
1408b 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72  ager->fd, pPager
1408c 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20  ->sync_flags);. 
1408d 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 45     }.    IOTRACE
1408e 28 28 22 44 42 53 59 4e 43 20 25 70 5c 6e 22 2c  (("DBSYNC %p\n",
1408f 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 70   pPager))..    p
14090 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50  Pager->state = P
14091 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a 20 20 7d  AGER_SYNCED;.  }
14092 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f  ..commit_phase_o
14093 6e 65 5f 65 78 69 74 3a 0a 20 20 72 65 74 75 72  ne_exit:.  retur
14094 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
14095 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  When this functi
14096 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  on is called, th
14097 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
14098 68 61 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74  has been complet
14099 65 6c 79 0a 2a 2a 20 75 70 64 61 74 65 64 20 74  ely.** updated t
1409a 6f 20 72 65 66 6c 65 63 74 20 74 68 65 20 63 68  o reflect the ch
1409b 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68  anges made by th
1409c 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
1409d 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79 6e  ction and.** syn
1409e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 54 68 65  ced to disk. The
1409f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 74   journal file st
140a0 69 6c 6c 20 65 78 69 73 74 73 20 69 6e 20 74 68  ill exists in th
140a1 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 0a 2a  e file-system .*
140a2 2a 20 74 68 6f 75 67 68 2c 20 61 6e 64 20 69 66  * though, and if
140a3 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72   a failure occur
140a4 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20  s at this point 
140a5 69 74 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c  it will eventual
140a6 6c 79 0a 2a 2a 20 62 65 20 75 73 65 64 20 61 73  ly.** be used as
140a7 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61   a hot-journal a
140a8 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  nd the current t
140a9 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 65  ransaction rolle
140aa 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  d back..**.** Th
140ab 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61  is function fina
140ac 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61  lizes the journa
140ad 6c 20 66 69 6c 65 2c 20 65 69 74 68 65 72 20 62  l file, either b
140ae 79 20 64 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a 20  y deleting, .** 
140af 74 72 75 6e 63 61 74 69 6e 67 20 6f 72 20 70 61  truncating or pa
140b0 72 74 69 61 6c 6c 79 20 7a 65 72 6f 69 6e 67 20  rtially zeroing 
140b1 69 74 2c 20 73 6f 20 74 68 61 74 20 69 74 20 63  it, so that it c
140b2 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 0a 2a  annot be used .*
140b3 2a 20 66 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e 61  * for hot-journa
140b4 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63 65  l rollback. Once
140b5 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 74 68   this is done th
140b6 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e transaction is
140b7 0a 2a 2a 20 69 72 72 65 76 6f 63 61 62 6c 79 20  .** irrevocably 
140b8 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a  committed..**.**
140b9 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
140ba 75 72 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72  urs, an IO error
140bb 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
140bc 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 0a  d and the pager.
140bd 2a 2a 20 6d 6f 76 65 73 20 69 6e 74 6f 20 74 68  ** moves into th
140be 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 4f  e error state. O
140bf 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45  therwise, SQLITE
140c0 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
140c1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
140c2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
140c3 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  gerCommitPhaseTw
140c4 6f 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  o(Pager *pPager)
140c5 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
140c6 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20  ITE_OK;         
140c7 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
140c8 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a  rn code */..  /*
140c9 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   This routine sh
140ca 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c  ould not be call
140cb 65 64 20 69 66 20 61 20 70 72 69 6f 72 20 65 72  ed if a prior er
140cc 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64  ror has occurred
140cd 2e 0a 20 20 2a 2a 20 42 75 74 20 69 66 20 28 64  ..  ** But if (d
140ce 75 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65  ue to a coding e
140cf 72 72 6f 72 20 65 6c 73 65 77 68 65 72 65 20 69  rror elsewhere i
140d0 6e 20 74 68 65 20 73 79 73 74 65 6d 29 20 69 74  n the system) it
140d1 20 64 6f 65 73 20 67 65 74 0a 20 20 2a 2a 20 63   does get.  ** c
140d2 61 6c 6c 65 64 2c 20 6a 75 73 74 20 72 65 74 75  alled, just retu
140d3 72 6e 20 74 68 65 20 73 61 6d 65 20 65 72 72 6f  rn the same erro
140d4 72 20 63 6f 64 65 20 77 69 74 68 6f 75 74 20 64  r code without d
140d5 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 2a  oing anything. *
140d6 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50  /.  if( NEVER(pP
140d7 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29  ager->errCode) )
140d8 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
140d9 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54  errCode;..  /* T
140da 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  his function sho
140db 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65  uld not be calle
140dc 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20 69  d if the pager i
140dd 73 20 6e 6f 74 20 69 6e 20 61 74 20 6c 65 61 73  s not in at leas
140de 74 0a 20 20 2a 2a 20 50 41 47 45 52 5f 52 45 53  t.  ** PAGER_RES
140df 45 52 56 45 44 20 73 74 61 74 65 2e 20 41 6e 64  ERVED state. And
140e0 20 69 6e 64 65 65 64 20 53 51 4c 69 74 65 20 6e   indeed SQLite n
140e1 65 76 65 72 20 64 6f 65 73 20 74 68 69 73 2e 20  ever does this. 
140e2 42 75 74 20 69 74 20 69 73 0a 20 20 2a 2a 20 6e  But it is.  ** n
140e3 69 63 65 20 74 6f 20 68 61 76 65 20 74 68 69 73  ice to have this
140e4 20 64 65 66 65 6e 73 69 76 65 20 74 65 73 74 20   defensive test 
140e5 68 65 72 65 20 61 6e 79 77 61 79 2e 0a 20 20 2a  here anyway..  *
140e6 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50  /.  if( NEVER(pP
140e7 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45  ager->state<PAGE
140e8 52 5f 52 45 53 45 52 56 45 44 29 20 29 20 72 65  R_RESERVED) ) re
140e9 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
140ea 52 3b 0a 0a 20 20 2f 2a 20 41 6e 20 6f 70 74 69  R;..  /* An opti
140eb 6d 69 7a 61 74 69 6f 6e 2e 20 49 66 20 74 68 65  mization. If the
140ec 20 64 61 74 61 62 61 73 65 20 77 61 73 20 6e 6f   database was no
140ed 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66  t actually modif
140ee 69 65 64 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20  ied during.  ** 
140ef 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
140f0 2c 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72  , the pager is r
140f1 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73  unning in exclus
140f2 69 76 65 2d 6d 6f 64 65 20 61 6e 64 20 69 73 0a  ive-mode and is.
140f3 20 20 2a 2a 20 75 73 69 6e 67 20 70 65 72 73 69    ** using persi
140f4 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2c 20  stent journals, 
140f5 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  then this functi
140f6 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20  on is a no-op.. 
140f7 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 61   **.  ** The sta
140f8 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  rt of the journa
140f9 6c 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79  l file currently
140fa 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67   contains a sing
140fb 6c 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a  le journal .  **
140fc 20 68 65 61 64 65 72 20 77 69 74 68 20 74 68 65   header with the
140fd 20 6e 52 65 63 20 66 69 65 6c 64 20 73 65 74 20   nRec field set 
140fe 74 6f 20 30 2e 20 49 66 20 73 75 63 68 20 61 20  to 0. If such a 
140ff 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20  journal is used 
14100 61 73 0a 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f  as.  ** a hot-jo
14101 75 72 6e 61 6c 20 64 75 72 69 6e 67 20 68 6f 74  urnal during hot
14102 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63  -journal rollbac
14103 6b 2c 20 30 20 63 68 61 6e 67 65 73 20 77 69 6c  k, 0 changes wil
14104 6c 20 62 65 20 6d 61 64 65 0a 20 20 2a 2a 20 74  l be made.  ** t
14105 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
14106 69 6c 65 2e 20 53 6f 20 74 68 65 72 65 20 69 73  ile. So there is
14107 20 6e 6f 20 6e 65 65 64 20 74 6f 20 7a 65 72 6f   no need to zero
14108 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20   the journal .  
14109 2a 2a 20 68 65 61 64 65 72 2e 20 53 69 6e 63 65  ** header. Since
1410a 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
1410b 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c   exclusive mode,
1410c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
1410d 64 0a 20 20 2a 2a 20 74 6f 20 64 72 6f 70 20 61  d.  ** to drop a
1410e 6e 79 20 6c 6f 63 6b 73 20 65 69 74 68 65 72 2e  ny locks either.
1410f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  .  */.  if( pPag
14110 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d  er->dbModified==
14111 30 20 26 26 20 70 50 61 67 65 72 2d 3e 65 78 63  0 && pPager->exc
14112 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 26  lusiveMode .   &
14113 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
14114 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
14115 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
14116 0a 20 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  .  ){.    assert
14117 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
14118 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44  lOff==JOURNAL_HD
14119 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a  R_SZ(pPager) );.
1411a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1411b 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50 41 47  E_OK;.  }..  PAG
1411c 45 52 54 52 41 43 45 28 28 22 43 4f 4d 4d 49 54  ERTRACE(("COMMIT
1411d 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28   %d\n", PAGERID(
1411e 70 50 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73  pPager)));.  ass
1411f 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
14120 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44  te==PAGER_SYNCED
14121 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 21 70 50   || MEMDB || !pP
14122 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
14123 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72   );.  rc = pager
14124 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
14125 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d  (pPager, pPager-
14126 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 72  >setMaster);.  r
14127 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f  eturn pager_erro
14128 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d  r(pPager, rc);.}
14129 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b  ../*.** Rollback
1412a 20 61 6c 6c 20 63 68 61 6e 67 65 73 2e 20 54 68   all changes. Th
1412b 65 20 64 61 74 61 62 61 73 65 20 66 61 6c 6c 73  e database falls
1412c 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53   back to PAGER_S
1412d 48 41 52 45 44 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a  HARED mode..**.*
1412e 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1412f 70 65 72 66 6f 72 6d 73 20 74 77 6f 20 74 61 73  performs two tas
14130 6b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49  ks:.**.**   1) I
14131 74 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65  t rolls back the
14132 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 72   journal file, r
14133 65 73 74 6f 72 69 6e 67 20 61 6c 6c 20 64 61 74  estoring all dat
14134 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 0a  abase file and .
14135 2a 2a 20 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72  **      in-memor
14136 79 20 63 61 63 68 65 20 70 61 67 65 73 20 74 6f  y cache pages to
14137 20 74 68 65 20 73 74 61 74 65 20 74 68 65 79 20   the state they 
14138 77 65 72 65 20 69 6e 20 77 68 65 6e 20 74 68 65  were in when the
14139 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20   transaction.** 
1413a 20 20 20 20 20 77 61 73 20 6f 70 65 6e 65 64 2c       was opened,
1413b 20 61 6e 64 0a 2a 2a 20 20 20 32 29 20 49 74 20   and.**   2) It 
1413c 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f  finalizes the jo
1413d 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 6f 20 74  urnal file, so t
1413e 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 75 73  hat it is not us
1413f 65 64 20 66 6f 72 20 68 6f 74 0a 2a 2a 20 20 20  ed for hot.**   
14140 20 20 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61     rollback at a
14141 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20  ny point in the 
14142 66 75 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 73 75  future..**.** su
14143 62 6a 65 63 74 20 74 6f 20 74 68 65 20 66 6f 6c  bject to the fol
14144 6c 6f 77 69 6e 67 20 71 75 61 6c 69 66 69 63 61  lowing qualifica
14145 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49  tions:.**.** * I
14146 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
14147 6c 65 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70  le is not yet op
14148 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  en when this fun
14149 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c  ction is called,
1414a 0a 2a 2a 20 20 20 74 68 65 6e 20 6f 6e 6c 79 20  .**   then only 
1414b 28 32 29 20 69 73 20 70 65 72 66 6f 72 6d 65 64  (2) is performed
1414c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
1414d 68 65 72 65 20 69 73 20 6e 6f 20 6a 6f 75 72 6e  here is no journ
1414e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 74 6f 20  al file.**   to 
1414f 72 6f 6c 6c 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a  roll back..**.**
14150 20 2a 20 49 66 20 69 6e 20 61 6e 20 65 72 72 6f   * If in an erro
14151 72 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 68  r state other th
14152 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2c 20  an SQLITE_FULL, 
14153 74 68 65 6e 20 74 61 73 6b 20 28 31 29 20 69 73  then task (1) is
14154 20 0a 2a 2a 20 20 20 70 65 72 66 6f 72 6d 65 64   .**   performed
14155 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  . If successful,
14156 20 74 61 73 6b 20 28 32 29 2e 20 52 65 67 61 72   task (2). Regar
14157 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6f 75 74  dless of the out
14158 63 6f 6d 65 0a 2a 2a 20 20 20 6f 66 20 65 69 74  come.**   of eit
14159 68 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20 73  her, the error s
1415a 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  tate error code 
1415b 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
1415c 68 65 20 63 61 6c 6c 65 72 0a 2a 2a 20 20 20 28  he caller.**   (
1415d 69 2e 65 2e 20 65 69 74 68 65 72 20 53 51 4c 49  i.e. either SQLI
1415e 54 45 5f 49 4f 45 52 52 20 6f 72 20 53 51 4c 49  TE_IOERR or SQLI
1415f 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a  TE_CORRUPT)..**.
14160 2a 2a 20 2a 20 49 66 20 74 68 65 20 70 61 67 65  ** * If the page
14161 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f 52 45  r is in PAGER_RE
14162 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68  SERVED state, th
14163 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20  en attempt (1). 
14164 57 68 65 74 68 65 72 0a 2a 2a 20 20 20 6f 72 20  Whether.**   or 
14165 6e 6f 74 20 28 31 29 20 69 73 20 73 75 63 63 75  not (1) is succu
14166 73 73 66 75 6c 2c 20 61 6c 73 6f 20 61 74 74 65  ssful, also atte
14167 6d 70 74 20 28 32 29 2e 20 49 66 20 73 75 63 63  mpt (2). If succ
14168 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a  essful, return.*
14169 2a 20 20 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f  *   SQLITE_OK. O
1416a 74 68 65 72 77 69 73 65 2c 20 65 6e 74 65 72 20  therwise, enter 
1416b 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20  the error state 
1416c 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 66  and return the f
1416d 69 72 73 74 20 0a 2a 2a 20 20 20 65 72 72 6f 72  irst .**   error
1416e 20 63 6f 64 65 20 65 6e 63 6f 75 6e 74 65 72 65   code encountere
1416f 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74  d. .**.**   In t
14170 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 69  his case there i
14171 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74  s no chance that
14172 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 61   the database wa
14173 73 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 0a 2a  s written to. .*
14174 2a 20 20 20 53 6f 20 69 73 20 73 61 66 65 20 74  *   So is safe t
14175 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a  o finalize the j
14176 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 76 65 6e  ournal file even
14177 20 69 66 20 74 68 65 20 70 6c 61 79 62 61 63 6b   if the playback
14178 20 0a 2a 2a 20 20 20 28 6f 70 65 72 61 74 69 6f   .**   (operatio
14179 6e 20 31 29 20 66 61 69 6c 65 64 2e 20 48 6f 77  n 1) failed. How
1417a 65 76 65 72 20 74 68 65 20 70 61 67 65 72 20 6d  ever the pager m
1417b 75 73 74 20 65 6e 74 65 72 20 74 68 65 20 65 72  ust enter the er
1417c 72 6f 72 20 73 74 61 74 65 0a 2a 2a 20 20 20 61  ror state.**   a
1417d 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  s the contents o
1417e 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  f the in-memory 
1417f 63 61 63 68 65 20 61 72 65 20 6e 6f 77 20 73 75  cache are now su
14180 73 70 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46  spect..**.** * F
14181 69 6e 61 6c 6c 79 2c 20 69 66 20 69 6e 20 50 41  inally, if in PA
14182 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 73 74  GER_EXCLUSIVE st
14183 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 6d 70  ate, then attemp
14184 74 20 28 31 29 2e 20 4f 6e 6c 79 0a 2a 2a 20 20  t (1). Only.**  
14185 20 61 74 74 65 6d 70 74 20 28 32 29 20 69 66 20   attempt (2) if 
14186 28 31 29 20 69 73 20 73 75 63 63 65 73 73 66 75  (1) is successfu
14187 6c 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  l. Return SQLITE
14188 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75  _OK if successfu
14189 6c 2c 0a 2a 2a 20 20 20 6f 74 68 65 72 77 69 73  l,.**   otherwis
1418a 65 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f  e enter the erro
1418b 72 20 73 74 61 74 65 20 61 6e 64 20 72 65 74 75  r state and retu
1418c 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  rn the error cod
1418d 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 20  e from the .**  
1418e 20 66 61 69 6c 69 6e 67 20 6f 70 65 72 61 74 69   failing operati
1418f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74  on..**.**   In t
14190 68 69 73 20 63 61 73 65 20 74 68 65 20 64 61 74  his case the dat
14191 61 62 61 73 65 20 66 69 6c 65 20 6d 61 79 20 68  abase file may h
14192 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ave been written
14193 20 74 6f 2e 20 53 6f 20 69 66 20 74 68 65 0a 2a   to. So if the.*
14194 2a 20 20 20 70 6c 61 79 62 61 63 6b 20 6f 70 65  *   playback ope
14195 72 61 74 69 6f 6e 20 64 69 64 20 6e 6f 74 20 73  ration did not s
14196 75 63 63 65 65 64 20 69 74 20 77 6f 75 6c 64 20  ucceed it would 
14197 6e 6f 74 20 62 65 20 73 61 66 65 20 74 6f 20 66  not be safe to f
14198 69 6e 61 6c 69 7a 65 0a 2a 2a 20 20 20 74 68 65  inalize.**   the
14199 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49   journal file. I
1419a 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 6c 65  t needs to be le
1419b 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73  ft in the file-s
1419c 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a  ystem so that.**
1419d 20 20 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72     some other pr
1419e 6f 63 65 73 73 20 63 61 6e 20 75 73 65 20 69 74  ocess can use it
1419f 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20   to restore the 
141a0 64 61 74 61 62 61 73 65 20 73 74 61 74 65 20 28  database state (
141a1 62 79 0a 2a 2a 20 20 20 68 6f 74 2d 6a 6f 75 72  by.**   hot-jour
141a2 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 2a  nal rollback)..*
141a3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
141a4 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
141a5 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20  rRollback(Pager 
141a6 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20  *pPager){.  int 
141a7 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
141a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
141a9 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
141aa 2a 2f 0a 20 20 50 41 47 45 52 54 52 41 43 45 28  */.  PAGERTRACE(
141ab 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e 22  ("ROLLBACK %d\n"
141ac 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72  , PAGERID(pPager
141ad 29 29 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67  )));.  if( !pPag
141ae 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 7c  er->dbModified |
141af 7c 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  | !isOpen(pPager
141b0 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 72 63  ->jfd) ){.    rc
141b1 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
141b2 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
141b3 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
141b4 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  er);.  }else if(
141b5 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
141b6 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43   && pPager->errC
141b7 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c  ode!=SQLITE_FULL
141b8 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67   ){.    if( pPag
141b9 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
141ba 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20  _EXCLUSIVE ){.  
141bb 20 20 20 20 70 61 67 65 72 5f 70 6c 61 79 62 61      pager_playba
141bc 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20  ck(pPager, 0);. 
141bd 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70 50     }.    rc = pP
141be 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
141bf 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
141c0 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
141c1 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b  AGER_RESERVED ){
141c2 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a  .      int rc2;.
141c3 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
141c4 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72  _playback(pPager
141c5 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32 20  , 0);.      rc2 
141c6 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e  = pager_end_tran
141c7 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20  saction(pPager, 
141c8 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65  pPager->setMaste
141c9 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  r);.      if( rc
141ca 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
141cb 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b         rc = rc2;
141cc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
141cd 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70  se{.      rc = p
141ce 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50  ager_playback(pP
141cf 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  ager, 0);.    }.
141d0 0a 20 20 20 20 69 66 28 20 21 4d 45 4d 44 42 20  .    if( !MEMDB 
141d1 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
141d2 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30  >dbSizeValid = 0
141d3 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
141d4 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
141d5 72 73 20 64 75 72 69 6e 67 20 61 20 52 4f 4c 4c  rs during a ROLL
141d6 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f 20  BACK, we can no 
141d7 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 74 68 65  longer trust the
141d8 20 70 61 67 65 72 0a 20 20 20 20 2a 2a 20 63 61   pager.    ** ca
141d9 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67  che. So call pag
141da 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74 68  er_error() on th
141db 65 20 77 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b  e way out to mak
141dc 65 20 61 6e 79 20 65 72 72 6f 72 20 0a 20 20 20  e any error .   
141dd 20 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 2e 0a   ** persistent..
141de 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
141df 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
141e0 65 72 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72  er, rc);.  }.  r
141e1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
141e2 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
141e3 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
141e4 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 72 65  ile is opened re
141e5 61 64 2d 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e  ad-only.  Return
141e6 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65   FALSE.** if the
141e7 20 64 61 74 61 62 61 73 65 20 69 73 20 28 69 6e   database is (in
141e8 20 74 68 65 6f 72 79 29 20 77 72 69 74 61 62 6c   theory) writabl
141e9 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
141ea 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 50  VATE u8 sqlite3P
141eb 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50  agerIsreadonly(P
141ec 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
141ed 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
141ee 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a  readOnly;.}../*.
141ef 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
141f0 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63  mber of referenc
141f1 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 72 2e  es to the pager.
141f2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
141f3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
141f4 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65  gerRefcount(Page
141f5 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65  r *pPager){.  re
141f6 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63  turn sqlite3Pcac
141f7 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65  heRefCount(pPage
141f8 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a  r->pPCache);.}..
141f9 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
141fa 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   number of refer
141fb 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 70 65  ences to the spe
141fc 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a  cified page..*/.
141fd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
141fe 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  nt sqlite3PagerP
141ff 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61  ageRefcount(DbPa
14200 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65  ge *pPage){.  re
14201 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63  turn sqlite3Pcac
14202 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70  hePageRefcount(p
14203 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 66  Page);.}..#ifdef
14204 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a   SQLITE_TEST./*.
14205 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
14206 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
14207 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73  ing and analysis
14208 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45   only..*/.SQLITE
14209 5f 50 52 49 56 41 54 45 20 69 6e 74 20 2a 73 71  _PRIVATE int *sq
1420a 6c 69 74 65 33 50 61 67 65 72 53 74 61 74 73 28  lite3PagerStats(
1420b 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
1420c 20 20 73 74 61 74 69 63 20 69 6e 74 20 61 5b 31    static int a[1
1420d 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71 6c  1];.  a[0] = sql
1420e 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75  ite3PcacheRefCou
1420f 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  nt(pPager->pPCac
14210 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 71  he);.  a[1] = sq
14211 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63  lite3PcachePagec
14212 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
14213 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d 20  ache);.  a[2] = 
14214 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74  sqlite3PcacheGet
14215 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 65 72  Cachesize(pPager
14216 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b  ->pPCache);.  a[
14217 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  3] = pPager->dbS
14218 69 7a 65 56 61 6c 69 64 20 3f 20 28 69 6e 74 29  izeValid ? (int)
14219 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20   pPager->dbSize 
1421a 3a 20 2d 31 3b 0a 20 20 61 5b 34 5d 20 3d 20 70  : -1;.  a[4] = p
1421b 50 61 67 65 72 2d 3e 73 74 61 74 65 3b 0a 20 20  Pager->state;.  
1421c 61 5b 35 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65  a[5] = pPager->e
1421d 72 72 43 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 3d  rrCode;.  a[6] =
1421e 20 70 50 61 67 65 72 2d 3e 6e 48 69 74 3b 0a 20   pPager->nHit;. 
1421f 20 61 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[7] = pPager->
14220 6e 4d 69 73 73 3b 0a 20 20 61 5b 38 5d 20 3d 20  nMiss;.  a[8] = 
14221 30 3b 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 62  0;  /* Used to b
14222 65 20 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c 20  e pPager->nOvfl 
14223 2a 2f 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61 67  */.  a[9] = pPag
14224 65 72 2d 3e 6e 52 65 61 64 3b 0a 20 20 61 5b 31  er->nRead;.  a[1
14225 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57 72  0] = pPager->nWr
14226 69 74 65 3b 0a 20 20 72 65 74 75 72 6e 20 61 3b  ite;.  return a;
14227 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
14228 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
14229 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65  this is an in-me
1422a 6d 6f 72 79 20 70 61 67 65 72 2e 0a 2a 2f 0a 53  mory pager..*/.S
1422b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1422c 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  t sqlite3PagerIs
1422d 4d 65 6d 64 62 28 50 61 67 65 72 20 2a 70 50 61  Memdb(Pager *pPa
1422e 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 4d  ger){.  return M
1422f 45 4d 44 42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  EMDB;.}../*.** C
14230 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 20  heck that there 
14231 61 72 65 20 61 74 20 6c 65 61 73 74 20 6e 53 61  are at least nSa
14232 76 65 70 6f 69 6e 74 20 73 61 76 65 70 6f 69 6e  vepoint savepoin
14233 74 73 20 6f 70 65 6e 2e 20 49 66 20 74 68 65 72  ts open. If ther
14234 65 20 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74  e are.** current
14235 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 6e 53 61  ly less than nSa
14236 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2c 20 74  vepoints open, t
14237 68 65 6e 20 6f 70 65 6e 20 6f 6e 65 20 6f 72 20  hen open one or 
14238 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 73 0a  more savepoints.
14239 2a 2a 20 74 6f 20 6d 61 6b 65 20 75 70 20 74 68  ** to make up th
1423a 65 20 64 69 66 66 65 72 65 6e 63 65 2e 20 49 66  e difference. If
1423b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
1423c 61 76 65 70 6f 69 6e 74 73 20 69 73 20 61 6c 72  avepoints is alr
1423d 65 61 64 79 0a 2a 2a 20 65 71 75 61 6c 20 74 6f  eady.** equal to
1423e 20 6e 53 61 76 65 70 6f 69 6e 74 2c 20 74 68 65   nSavepoint, the
1423f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
14240 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
14241 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  * If a memory al
14242 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20  location fails, 
14243 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20  SQLITE_NOMEM is 
14244 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20  returned. If an 
14245 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73  error .** occurs
14246 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 20 74   while opening t
14247 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66  he sub-journal f
14248 69 6c 65 2c 20 74 68 65 6e 20 61 6e 20 49 4f 20  ile, then an IO 
14249 65 72 72 6f 72 20 63 6f 64 65 20 69 73 0a 2a 2a  error code is.**
1424a 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
1424b 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e  wise, SQLITE_OK.
1424c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1424d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
1424e 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74  gerOpenSavepoint
1424f 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
14250 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 29 7b  int nSavepoint){
14251 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
14252 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
14253 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14254 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
14255 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 20 3d 20   int nCurrent = 
14256 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69  pPager->nSavepoi
14257 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75  nt;        /* Cu
14258 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
14259 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 0a 20  savepoints */.. 
1425a 20 69 66 28 20 6e 53 61 76 65 70 6f 69 6e 74 3e   if( nSavepoint>
1425b 6e 43 75 72 72 65 6e 74 20 26 26 20 70 50 61 67  nCurrent && pPag
1425c 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29  er->useJournal )
1425d 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20  {.    int ii;   
1425e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1425f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14260 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
14261 6c 65 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 53  le */.    PagerS
14262 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65 77 3b 20  avepoint *aNew; 
14263 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14264 20 20 2f 2a 20 4e 65 77 20 50 61 67 65 72 2e 61    /* New Pager.a
14265 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20  Savepoint array 
14266 2a 2f 0a 0a 20 20 20 20 2f 2a 20 45 69 74 68 65  */..    /* Eithe
14267 72 20 74 68 65 72 65 20 69 73 20 6e 6f 20 61 63  r there is no ac
14268 74 69 76 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20  tive journal or 
14269 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  the sub-journal 
1426a 69 73 20 6f 70 65 6e 20 6f 72 20 0a 20 20 20 20  is open or .    
1426b 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  ** the journal i
1426c 73 20 61 6c 77 61 79 73 20 73 74 6f 72 65 64 20  s always stored 
1426d 69 6e 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20  in memory */.   
1426e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1426f 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c  >nSavepoint==0 |
14270 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  | isOpen(pPager-
14271 3e 73 6a 66 64 29 20 7c 7c 0a 20 20 20 20 20 20  >sjfd) ||.      
14272 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
14273 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
14274 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
14275 4f 52 59 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 47  ORY );..    /* G
14276 72 6f 77 20 74 68 65 20 50 61 67 65 72 2e 61 53  row the Pager.aS
14277 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20 75  avepoint array u
14278 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29 2e 20  sing realloc(). 
14279 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  Return SQLITE_NO
1427a 4d 45 4d 0a 20 20 20 20 2a 2a 20 69 66 20 74 68  MEM.    ** if th
1427b 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  e allocation fai
1427c 6c 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 7a  ls. Otherwise, z
1427d 65 72 6f 20 74 68 65 20 6e 65 77 20 70 6f 72 74  ero the new port
1427e 69 6f 6e 20 69 6e 20 63 61 73 65 20 61 20 0a 20  ion in case a . 
1427f 20 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69     ** malloc fai
14280 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c  lure occurs whil
14281 65 20 70 6f 70 75 6c 61 74 69 6e 67 20 69 74 20  e populating it 
14282 69 6e 20 74 68 65 20 66 6f 72 28 2e 2e 2e 29 20  in the for(...) 
14283 6c 6f 6f 70 20 62 65 6c 6f 77 2e 0a 20 20 20 20  loop below..    
14284 2a 2f 0a 20 20 20 20 61 4e 65 77 20 3d 20 28 50  */.    aNew = (P
14285 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 29  agerSavepoint *)
14286 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 0a  sqlite3Realloc(.
14287 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
14288 61 53 61 76 65 70 6f 69 6e 74 2c 20 73 69 7a 65  aSavepoint, size
14289 6f 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e  of(PagerSavepoin
1428a 74 29 2a 6e 53 61 76 65 70 6f 69 6e 74 0a 20 20  t)*nSavepoint.  
1428b 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 61 4e    );.    if( !aN
1428c 65 77 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ew ){.      retu
1428d 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1428e 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65  .    }.    memse
1428f 74 28 26 61 4e 65 77 5b 6e 43 75 72 72 65 6e 74  t(&aNew[nCurrent
14290 5d 2c 20 30 2c 20 28 6e 53 61 76 65 70 6f 69 6e  ], 0, (nSavepoin
14291 74 2d 6e 43 75 72 72 65 6e 74 29 20 2a 20 73 69  t-nCurrent) * si
14292 7a 65 6f 66 28 50 61 67 65 72 53 61 76 65 70 6f  zeof(PagerSavepo
14293 69 6e 74 29 29 3b 0a 20 20 20 20 70 50 61 67 65  int));.    pPage
14294 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d 20  r->aSavepoint = 
14295 61 4e 65 77 3b 0a 20 20 20 20 70 50 61 67 65 72  aNew;.    pPager
14296 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e  ->nSavepoint = n
14297 53 61 76 65 70 6f 69 6e 74 3b 0a 0a 20 20 20 20  Savepoint;..    
14298 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  /* Populate the 
14299 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 73  PagerSavepoint s
1429a 74 72 75 63 74 75 72 65 73 20 6a 75 73 74 20 61  tructures just a
1429b 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 20 20 20  llocated. */.   
1429c 20 66 6f 72 28 69 69 3d 6e 43 75 72 72 65 6e 74   for(ii=nCurrent
1429d 3b 20 69 69 3c 6e 53 61 76 65 70 6f 69 6e 74 3b  ; ii<nSavepoint;
1429e 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73   ii++){.      as
1429f 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62  sert( pPager->db
142a0 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20  SizeValid );.   
142a1 20 20 20 61 4e 65 77 5b 69 69 5d 2e 6e 4f 72 69     aNew[ii].nOri
142a2 67 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69  g = pPager->dbSi
142a3 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73  ze;.      if( is
142a4 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
142a5 29 20 26 26 20 41 4c 57 41 59 53 28 70 50 61 67  ) && ALWAYS(pPag
142a6 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30  er->journalOff>0
142a7 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 4e 65  ) ){.        aNe
142a8 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20  w[ii].iOffset = 
142a9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
142aa 66 66 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ff;.      }else{
142ab 0a 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69 69  .        aNew[ii
142ac 5d 2e 69 4f 66 66 73 65 74 20 3d 20 4a 4f 55 52  ].iOffset = JOUR
142ad 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
142ae 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
142af 20 20 61 4e 65 77 5b 69 69 5d 2e 69 53 75 62 52    aNew[ii].iSubR
142b0 65 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75  ec = pPager->nSu
142b1 62 52 65 63 3b 0a 20 20 20 20 20 20 61 4e 65 77  bRec;.      aNew
142b2 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e  [ii].pInSavepoin
142b3 74 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  t = sqlite3Bitve
142b4 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e  cCreate(pPager->
142b5 64 62 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69  dbSize);.      i
142b6 66 28 20 21 61 4e 65 77 5b 69 69 5d 2e 70 49 6e  f( !aNew[ii].pIn
142b7 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20  Savepoint ){.   
142b8 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
142b9 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
142ba 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
142bb 4f 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75  Open the sub-jou
142bc 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 6e  rnal, if it is n
142bd 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65  ot already opene
142be 64 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6f  d. */.    rc = o
142bf 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50  penSubJournal(pP
142c0 61 67 65 72 29 3b 0a 20 20 20 20 61 73 73 65 72  ager);.    asser
142c1 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61  tTruncateConstra
142c2 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  int(pPager);.  }
142c3 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
142c4 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
142c5 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
142c6 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72  to rollback or r
142c7 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20  elease (commit) 
142c8 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20  a savepoint..** 
142c9 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f  The savepoint to
142ca 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c   release or roll
142cb 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62 65  back need not be
142cc 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
142cd 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65 64 20 73  ly .** created s
142ce 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20  avepoint..**.** 
142cf 50 61 72 61 6d 65 74 65 72 20 6f 70 20 69 73 20  Parameter op is 
142d0 61 6c 77 61 79 73 20 65 69 74 68 65 72 20 53 41  always either SA
142d1 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
142d2 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 45   or SAVEPOINT_RE
142d3 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20 69 74 20  LEASE..** If it 
142d4 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  is SAVEPOINT_REL
142d5 45 41 53 45 2c 20 74 68 65 6e 20 72 65 6c 65 61  EASE, then relea
142d6 73 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20 74  se and destroy t
142d7 68 65 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74  he savepoint wit
142d8 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53 61 76 65  h.** index iSave
142d9 70 6f 69 6e 74 2e 20 49 66 20 69 74 20 69 73 20  point. If it is 
142da 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
142db 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c 62 61 63  CK, then rollbac
142dc 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a 2a  k all changes.**
142dd 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 75 72   that have occur
142de 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 73 70  red since the sp
142df 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69 6e  ecified savepoin
142e0 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a 2a  t was created..*
142e1 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65 70 6f 69  *.** The savepoi
142e2 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f  nt to rollback o
142e3 72 20 72 65 6c 65 61 73 65 20 69 73 20 69 64 65  r release is ide
142e4 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ntified by param
142e5 65 74 65 72 20 0a 2a 2a 20 69 53 61 76 65 70 6f  eter .** iSavepo
142e6 69 6e 74 2e 20 41 20 76 61 6c 75 65 20 6f 66 20  int. A value of 
142e7 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70 65 72 61  0 means to opera
142e8 74 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 6d  te on the outerm
142e9 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a  ost savepoint.**
142ea 20 28 74 68 65 20 66 69 72 73 74 20 63 72 65 61   (the first crea
142eb 74 65 64 29 2e 20 41 20 76 61 6c 75 65 20 6f 66  ted). A value of
142ec 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69   (Pager.nSavepoi
142ed 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f 70 65 72  nt-1) means oper
142ee 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 6f  ate.** on the mo
142ef 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 65 61  st recently crea
142f0 74 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49  ted savepoint. I
142f1 66 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20  f iSavepoint is 
142f2 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20  greater than.** 
142f3 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e  (Pager.nSavepoin
142f4 74 2d 31 29 2c 20 74 68 65 6e 20 74 68 69 73 20  t-1), then this 
142f5 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
142f6 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  -op..**.** If a 
142f7 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 69  negative value i
142f8 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73  s passed to this
142f9 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20   function, then 
142fa 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74  the current.** t
142fb 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f  ransaction is ro
142fc 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20  lled back. This 
142fd 69 73 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20  is different to 
142fe 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69  calling .** sqli
142ff 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
14300 28 29 20 62 65 63 61 75 73 65 20 74 68 69 73 20  () because this 
14301 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  function does no
14302 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74  t terminate.** t
14303 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  he transaction o
14304 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74  r unlock the dat
14305 61 62 61 73 65 2c 20 69 74 20 6a 75 73 74 20 72  abase, it just r
14306 65 73 74 6f 72 65 73 20 74 68 65 20 0a 2a 2a 20  estores the .** 
14307 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
14308 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 20  database to its 
14309 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 2e 20  original state. 
1430a 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79 20 63 61  .**.** In any ca
1430b 73 65 2c 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e  se, all savepoin
1430c 74 73 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78  ts with an index
1430d 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 53   greater than iS
1430e 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 61 72 65  avepoint .** are
1430f 20 64 65 73 74 72 6f 79 65 64 2e 20 49 66 20 74   destroyed. If t
14310 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 65  his is a release
14311 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f 70 3d 3d   operation (op==
14312 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
14313 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73 61 76 65  E),.** then save
14314 70 6f 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74  point iSavepoint
14315 20 69 73 20 61 6c 73 6f 20 64 65 73 74 72 6f 79   is also destroy
14316 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ed..**.** This f
14317 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75  unction may retu
14318 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  rn SQLITE_NOMEM 
14319 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  if a memory allo
1431a 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a  cation fails,.**
1431b 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   or an IO error 
1431c 63 6f 64 65 20 69 66 20 61 6e 20 49 4f 20 65 72  code if an IO er
1431d 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ror occurs while
1431e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20   rolling back a 
1431f 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 20 49  .** savepoint. I
14320 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f 63 63 75  f no errors occu
14321 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  r, SQLITE_OK is 
14322 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53 51  returned..*/ .SQ
14323 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
14324 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76   sqlite3PagerSav
14325 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50  epoint(Pager *pP
14326 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  ager, int op, in
14327 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20  t iSavepoint){. 
14328 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
14329 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  _OK;..  assert( 
1432a 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  op==SAVEPOINT_RE
1432b 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56  LEASE || op==SAV
1432c 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1432d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 61  );.  assert( iSa
1432e 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70  vepoint>=0 || op
1432f 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  ==SAVEPOINT_ROLL
14330 42 41 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 69  BACK );..  if( i
14331 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67 65 72  Savepoint<pPager
14332 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a  ->nSavepoint ){.
14333 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20      int ii;     
14334 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
14335 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  or variable */. 
14336 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20     int nNew;    
14337 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
14338 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20 73 61 76  of remaining sav
14339 65 70 6f 69 6e 74 73 20 61 66 74 65 72 20 74 68  epoints after th
1433a 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 2f  is op. */..    /
1433b 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77  * Figure out how
1433c 20 6d 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73   many savepoints
1433d 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 20 61   will still be a
1433e 63 74 69 76 65 20 61 66 74 65 72 20 74 68 69 73  ctive after this
1433f 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f  .    ** operatio
14340 6e 2e 20 53 74 6f 72 65 20 74 68 69 73 20 76 61  n. Store this va
14341 6c 75 65 20 69 6e 20 6e 4e 65 77 2e 20 54 68 65  lue in nNew. The
14342 6e 20 66 72 65 65 20 72 65 73 6f 75 72 63 65 73  n free resources
14343 20 61 73 73 6f 63 69 61 74 65 64 20 0a 20 20 20   associated .   
14344 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 73 61 76   ** with any sav
14345 65 70 6f 69 6e 74 73 20 74 68 61 74 20 61 72 65  epoints that are
14346 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 74 68   destroyed by th
14347 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20  is operation..  
14348 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 77 20 3d 20    */.    nNew = 
14349 69 53 61 76 65 70 6f 69 6e 74 20 2b 20 28 6f 70  iSavepoint + (op
1434a 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  ==SAVEPOINT_ROLL
1434b 42 41 43 4b 29 3b 0a 20 20 20 20 66 6f 72 28 69  BACK);.    for(i
1434c 69 3d 6e 4e 65 77 3b 20 69 69 3c 70 50 61 67 65  i=nNew; ii<pPage
1434d 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69  r->nSavepoint; i
1434e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
1434f 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79  te3BitvecDestroy
14350 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f  (pPager->aSavepo
14351 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70  int[ii].pInSavep
14352 6f 69 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20  oint);.    }.   
14353 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f   pPager->nSavepo
14354 69 6e 74 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20  int = nNew;..   
14355 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
14356 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 72 61 74   rollback operat
14357 69 6f 6e 2c 20 70 6c 61 79 62 61 63 6b 20 74 68  ion, playback th
14358 65 20 73 70 65 63 69 66 69 65 64 20 73 61 76 65  e specified save
14359 70 6f 69 6e 74 2e 0a 20 20 20 20 2a 2a 20 49 66  point..    ** If
1435a 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 2d   this is a temp-
1435b 66 69 6c 65 2c 20 69 74 20 69 73 20 70 6f 73 73  file, it is poss
1435c 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 6a 6f  ible that the jo
1435d 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 0a 20  urnal file has. 
1435e 20 20 20 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65     ** not yet be
1435f 65 6e 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68  en opened. In th
14360 69 73 20 63 61 73 65 20 74 68 65 72 65 20 68 61  is case there ha
14361 76 65 20 62 65 65 6e 20 6e 6f 20 63 68 61 6e 67  ve been no chang
14362 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65  es to.    ** the
14363 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
14364 73 6f 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20  so the playback 
14365 6f 70 65 72 61 74 69 6f 6e 20 63 61 6e 20 62 65  operation can be
14366 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 20 2a 2f   skipped..    */
14367 0a 20 20 20 20 69 66 28 20 6f 70 3d 3d 53 41 56  .    if( op==SAV
14368 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
14369 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  && isOpen(pPager
1436a 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20  ->jfd) ){.      
1436b 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a  PagerSavepoint *
1436c 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 28 6e 4e  pSavepoint = (nN
1436d 65 77 3d 3d 30 29 3f 30 3a 26 70 50 61 67 65 72  ew==0)?0:&pPager
1436e 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 6e 4e 65  ->aSavepoint[nNe
1436f 77 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d  w-1];.      rc =
14370 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61   pagerPlaybackSa
14371 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2c 20  vepoint(pPager, 
14372 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20  pSavepoint);.   
14373 20 20 20 61 73 73 65 72 74 28 72 63 21 3d 53 51     assert(rc!=SQ
14374 4c 49 54 45 5f 44 4f 4e 45 29 3b 0a 20 20 20 20  LITE_DONE);.    
14375 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74  }.  .    /* If t
14376 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 65  his is a release
14377 20 6f 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73   of the outermos
14378 74 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 72 75  t savepoint, tru
14379 6e 63 61 74 65 20 0a 20 20 20 20 2a 2a 20 74 68  ncate .    ** th
1437a 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 74 6f  e sub-journal to
1437b 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73   zero bytes in s
1437c 69 7a 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ize. */.    if( 
1437d 6e 4e 65 77 3d 3d 30 20 26 26 20 6f 70 3d 3d 53  nNew==0 && op==S
1437e 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
1437f 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65   && isOpen(pPage
14380 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20  r->sjfd) ){.    
14381 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
14382 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20  LITE_OK );.     
14383 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
14384 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e  runcate(pPager->
14385 73 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20  sjfd, 0);.      
14386 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20  pPager->nSubRec 
14387 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
14388 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
14389 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1438a 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66  full pathname of
1438b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1438c 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
1438d 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
1438e 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69   *sqlite3PagerFi
1438f 6c 65 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50  lename(Pager *pP
14390 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
14391 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
14392 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  e;.}../*.** Retu
14393 72 6e 20 74 68 65 20 56 46 53 20 73 74 72 75 63  rn the VFS struc
14394 74 75 72 65 20 66 6f 72 20 74 68 65 20 70 61 67  ture for the pag
14395 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
14396 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69  IVATE const sqli
14397 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33  te3_vfs *sqlite3
14398 50 61 67 65 72 56 66 73 28 50 61 67 65 72 20 2a  PagerVfs(Pager *
14399 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72  pPager){.  retur
1439a 6e 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a  n pPager->pVfs;.
1439b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1439c 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20  the file handle 
1439d 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
1439e 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64   file associated
1439f 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 70 61 67  .** with the pag
143a0 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20  er.  This might 
143a1 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74  return NULL if t
143a2 68 65 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e  he file has.** n
143a3 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e  ot yet been open
143a4 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
143a5 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69  IVATE sqlite3_fi
143a6 6c 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  le *sqlite3Pager
143a7 46 69 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67  File(Pager *pPag
143a8 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
143a9 61 67 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a  ager->fd;.}../*.
143aa 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75  ** Return the fu
143ab 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74  ll pathname of t
143ac 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
143ad 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
143ae 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
143af 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
143b0 61 6c 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50  alname(Pager *pP
143b1 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
143b2 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
143b3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
143b4 6e 20 74 72 75 65 20 69 66 20 66 73 79 6e 63 28  n true if fsync(
143b5 29 20 63 61 6c 6c 73 20 61 72 65 20 64 69 73 61  ) calls are disa
143b6 62 6c 65 64 20 66 6f 72 20 74 68 69 73 20 70 61  bled for this pa
143b7 67 65 72 2e 20 20 52 65 74 75 72 6e 20 46 41 4c  ger.  Return FAL
143b8 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 29  SE.** if fsync()
143b9 73 20 61 72 65 20 65 78 65 63 75 74 65 64 20 6e  s are executed n
143ba 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  ormally..*/.SQLI
143bb 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
143bc 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e  qlite3PagerNosyn
143bd 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  c(Pager *pPager)
143be 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
143bf 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69  r->noSync;.}..#i
143c0 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
143c1 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20  CODEC./*.** Set 
143c2 6f 72 20 72 65 74 72 69 65 76 65 20 74 68 65 20  or retrieve the 
143c3 63 6f 64 65 63 20 66 6f 72 20 74 68 69 73 20 70  codec for this p
143c4 61 67 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ager.*/.static v
143c5 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
143c6 53 65 74 43 6f 64 65 63 28 0a 20 20 50 61 67 65  SetCodec(.  Page
143c7 72 20 2a 70 50 61 67 65 72 2c 0a 20 20 76 6f 69  r *pPager,.  voi
143c8 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69  d *(*xCodec)(voi
143c9 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e  d*,void*,Pgno,in
143ca 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f  t),.  void (*xCo
143cb 64 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69  decSizeChng)(voi
143cc 64 2a 2c 69 6e 74 2c 69 6e 74 29 2c 0a 20 20 76  d*,int,int),.  v
143cd 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65 65  oid (*xCodecFree
143ce 29 28 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64  )(void*),.  void
143cf 20 2a 70 43 6f 64 65 63 0a 29 7b 0a 20 20 69 66   *pCodec.){.  if
143d0 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63  ( pPager->xCodec
143d1 46 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78  Free ) pPager->x
143d2 43 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 72  CodecFree(pPager
143d3 2d 3e 70 43 6f 64 65 63 29 3b 0a 20 20 70 50 61  ->pCodec);.  pPa
143d4 67 65 72 2d 3e 78 43 6f 64 65 63 20 3d 20 78 43  ger->xCodec = xC
143d5 6f 64 65 63 3b 0a 20 20 70 50 61 67 65 72 2d 3e  odec;.  pPager->
143d6 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 20 3d  xCodecSizeChng =
143d7 20 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 3b   xCodecSizeChng;
143d8 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65  .  pPager->xCode
143d9 63 46 72 65 65 20 3d 20 78 43 6f 64 65 63 46 72  cFree = xCodecFr
143da 65 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 43  ee;.  pPager->pC
143db 6f 64 65 63 20 3d 20 70 43 6f 64 65 63 3b 0a 20  odec = pCodec;. 
143dc 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65   pagerReportSize
143dd 28 70 50 61 67 65 72 29 3b 0a 7d 0a 73 74 61 74  (pPager);.}.stat
143de 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  ic void *sqlite3
143df 50 61 67 65 72 47 65 74 43 6f 64 65 63 28 50 61  PagerGetCodec(Pa
143e0 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
143e1 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70  return pPager->p
143e2 43 6f 64 65 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a  Codec;.}.#endif.
143e3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
143e4 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
143e5 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 70  /*.** Move the p
143e6 61 67 65 20 70 50 67 20 74 6f 20 6c 6f 63 61 74  age pPg to locat
143e7 69 6f 6e 20 70 67 6e 6f 20 69 6e 20 74 68 65 20  ion pgno in the 
143e8 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  file..**.** Ther
143e9 65 20 6d 75 73 74 20 62 65 20 6e 6f 20 72 65 66  e must be no ref
143ea 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70  erences to the p
143eb 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20 6c  age previously l
143ec 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 70 67 6e  ocated at.** pgn
143ed 6f 20 28 77 68 69 63 68 20 77 65 20 63 61 6c 6c  o (which we call
143ee 20 70 50 67 4f 6c 64 29 20 74 68 6f 75 67 68 20   pPgOld) though 
143ef 74 68 61 74 20 70 61 67 65 20 69 73 20 61 6c 6c  that page is all
143f0 6f 77 65 64 20 74 6f 20 62 65 0a 2a 2a 20 69 6e  owed to be.** in
143f1 20 63 61 63 68 65 2e 20 20 49 66 20 74 68 65 20   cache.  If the 
143f2 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20  page previously 
143f3 6c 6f 63 61 74 65 64 20 61 74 20 70 67 6e 6f 20  located at pgno 
143f4 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a  is not already.*
143f5 2a 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  * in the rollbac
143f6 6b 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 69 73  k journal, it is
143f7 20 6e 6f 74 20 70 75 74 20 74 68 65 72 65 20 62   not put there b
143f8 79 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e  y by this routin
143f9 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 6e  e..**.** Referen
143fa 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20  ces to the page 
143fb 70 50 67 20 72 65 6d 61 69 6e 20 76 61 6c 69 64  pPg remain valid
143fc 2e 20 55 70 64 61 74 69 6e 67 20 61 6e 79 0a 2a  . Updating any.*
143fd 2a 20 6d 65 74 61 2d 64 61 74 61 20 61 73 73 6f  * meta-data asso
143fe 63 69 61 74 65 64 20 77 69 74 68 20 70 50 67 20  ciated with pPg 
143ff 28 69 2e 65 2e 20 64 61 74 61 20 73 74 6f 72 65  (i.e. data store
14400 64 20 69 6e 20 74 68 65 20 6e 45 78 74 72 61 20  d in the nExtra 
14401 62 79 74 65 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74  bytes.** allocat
14402 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  ed along with th
14403 65 20 70 61 67 65 29 20 69 73 20 74 68 65 20 72  e page) is the r
14404 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66  esponsibility of
14405 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a   the caller..**.
14406 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e  ** A transaction
14407 20 6d 75 73 74 20 62 65 20 61 63 74 69 76 65 20   must be active 
14408 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
14409 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20  e is called. It 
1440a 75 73 65 64 20 74 6f 20 62 65 0a 2a 2a 20 72 65  used to be.** re
1440b 71 75 69 72 65 64 20 74 68 61 74 20 61 20 73 74  quired that a st
1440c 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1440d 69 6f 6e 20 77 61 73 20 6e 6f 74 20 61 63 74 69  ion was not acti
1440e 76 65 2c 20 62 75 74 20 74 68 69 73 20 72 65 73  ve, but this res
1440f 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20  triction.** has 
14410 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 28 43 52  been removed (CR
14411 45 41 54 45 20 49 4e 44 45 58 20 6e 65 65 64 73  EATE INDEX needs
14412 20 74 6f 20 6d 6f 76 65 20 61 20 70 61 67 65 20   to move a page 
14413 77 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74  when a statement
14414 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
14415 69 73 20 61 63 74 69 76 65 29 2e 0a 2a 2a 0a 2a  is active)..**.*
14416 2a 20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20  * If the fourth 
14417 61 72 67 75 6d 65 6e 74 2c 20 69 73 43 6f 6d 6d  argument, isComm
14418 69 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  it, is non-zero,
14419 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 65 20   then this page 
1441a 69 73 20 62 65 69 6e 67 0a 2a 2a 20 6d 6f 76 65  is being.** move
1441b 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 64  d as part of a d
1441c 61 74 61 62 61 73 65 20 72 65 6f 72 67 61 6e 69  atabase reorgani
1441d 7a 61 74 69 6f 6e 20 6a 75 73 74 20 62 65 66 6f  zation just befo
1441e 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  re the transacti
1441f 6f 6e 20 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20  on .** is being 
14420 63 6f 6d 6d 69 74 74 65 64 2e 20 49 6e 20 74 68  committed. In th
14421 69 73 20 63 61 73 65 2c 20 69 74 20 69 73 20 67  is case, it is g
14422 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 74  uaranteed that t
14423 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
14424 20 0a 2a 2a 20 70 50 67 20 72 65 66 65 72 73 20   .** pPg refers 
14425 74 6f 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77  to will not be w
14426 72 69 74 74 65 6e 20 74 6f 20 61 67 61 69 6e 20  ritten to again 
14427 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e  within this tran
14428 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  saction..**.** T
14429 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
1442a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
1442b 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65 72  OMEM or an IO er
1442c 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 65  ror code if an e
1442d 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20  rror.** occurs. 
1442e 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 72 65  Otherwise, it re
1442f 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e  turns SQLITE_OK.
14430 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14431 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
14432 67 65 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65  gerMovepage(Page
14433 72 20 2a 70 50 61 67 65 72 2c 20 44 62 50 61 67  r *pPager, DbPag
14434 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20 70 67 6e  e *pPg, Pgno pgn
14435 6f 2c 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 29  o, int isCommit)
14436 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 4f 6c  {.  PgHdr *pPgOl
14437 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
14438 20 2f 2a 20 54 68 65 20 70 61 67 65 20 62 65 69   /* The page bei
14439 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20  ng overwritten. 
1443a 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 65 64 53 79  */.  Pgno needSy
1443b 6e 63 50 67 6e 6f 20 3d 20 30 3b 20 20 20 20 20  ncPgno = 0;     
1443c 20 20 2f 2a 20 4f 6c 64 20 76 61 6c 75 65 20 6f    /* Old value o
1443d 66 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 69 66 20  f pPg->pgno, if 
1443e 73 79 6e 63 20 69 73 20 72 65 71 75 69 72 65 64  sync is required
1443f 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
14440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14441 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
14442 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f 72 69 67  e */.  Pgno orig
14443 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20  Pgno;           
14444 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69      /* The origi
14445 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20  nal page number 
14446 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
14447 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 0a 20 20  g->nRef>0 );..  
14448 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 62  /* If the page b
14449 65 69 6e 67 20 6d 6f 76 65 64 20 69 73 20 64 69  eing moved is di
1444a 72 74 79 20 61 6e 64 20 68 61 73 20 6e 6f 74 20  rty and has not 
1444b 62 65 65 6e 20 73 61 76 65 64 20 62 79 20 74 68  been saved by th
1444c 65 20 6c 61 74 65 73 74 0a 20 20 2a 2a 20 73 61  e latest.  ** sa
1444d 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 73 61  vepoint, then sa
1444e 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  ve the current c
1444f 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70  ontents of the p
14450 61 67 65 20 69 6e 74 6f 20 74 68 65 20 0a 20 20  age into the .  
14451 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6e  ** sub-journal n
14452 6f 77 2e 20 54 68 69 73 20 69 73 20 72 65 71 75  ow. This is requ
14453 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 74  ired to handle t
14454 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63 65  he following sce
14455 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  nario:.  **.  **
14456 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a 20 20     BEGIN;.  **  
14457 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 67 65     <journal page
14458 20 58 2c 20 74 68 65 6e 20 6d 6f 64 69 66 79 20   X, then modify 
14459 69 74 20 69 6e 20 6d 65 6d 6f 72 79 3e 0a 20 20  it in memory>.  
1445a 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e 54  **     SAVEPOINT
1445b 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 20 20 20 20   one;.  **      
1445c 20 3c 4d 6f 76 65 20 70 61 67 65 20 58 20 74 6f   <Move page X to
1445d 20 6c 6f 63 61 74 69 6f 6e 20 59 3e 0a 20 20 2a   location Y>.  *
1445e 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54  *     ROLLBACK T
1445f 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a  O one;.  **.  **
14460 20 49 66 20 70 61 67 65 20 58 20 77 65 72 65 20   If page X were 
14461 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74  not written to t
14462 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 68  he sub-journal h
14463 65 72 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f  ere, it would no
14464 74 0a 20 20 2a 2a 20 62 65 20 70 6f 73 73 69 62  t.  ** be possib
14465 6c 65 20 74 6f 20 72 65 73 74 6f 72 65 20 69 74  le to restore it
14466 73 20 63 6f 6e 74 65 6e 74 73 20 77 68 65 6e 20  s contents when 
14467 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f  the "ROLLBACK TO
14468 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 74 61 74 65   one".  ** state
14469 6d 65 6e 74 20 77 65 72 65 20 69 73 20 70 72 6f  ment were is pro
1446a 63 65 73 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a  cessed..  **.  *
1446b 2a 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65  * subjournalPage
1446c 28 29 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 61  () may need to a
1446d 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 74 6f  llocate space to
1446e 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f   store pPg->pgno
1446f 20 69 6e 74 6f 0a 20 20 2a 2a 20 6f 6e 65 20 6f   into.  ** one o
14470 72 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e 74  r more savepoint
14471 20 62 69 74 76 65 63 73 2e 20 54 68 69 73 20 69   bitvecs. This i
14472 73 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68 69  s the reason thi
14473 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20  s function.  ** 
14474 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54  may return SQLIT
14475 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20  E_NOMEM..  */.  
14476 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50  if( pPg->flags&P
14477 47 48 44 52 5f 44 49 52 54 59 20 0a 20 20 20 26  GHDR_DIRTY .   &
14478 26 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61  & subjRequiresPa
14479 67 65 28 70 50 67 29 0a 20 20 20 26 26 20 53 51  ge(pPg).   && SQ
1447a 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73  LITE_OK!=(rc = s
1447b 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50  ubjournalPage(pP
1447c 67 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  g)).  ){.    ret
1447d 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 50  urn rc;.  }..  P
1447e 41 47 45 52 54 52 41 43 45 28 28 22 4d 4f 56 45  AGERTRACE(("MOVE
1447f 20 25 64 20 70 61 67 65 20 25 64 20 28 6e 65 65   %d page %d (nee
14480 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20  dSync=%d) moves 
14481 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20  to %d\n", .     
14482 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
14483 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 50  , pPg->pgno, (pP
14484 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
14485 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 70  EED_SYNC)?1:0, p
14486 67 6e 6f 29 29 3b 0a 20 20 49 4f 54 52 41 43 45  gno));.  IOTRACE
14487 28 28 22 4d 4f 56 45 20 25 70 20 25 64 20 25 64  (("MOVE %p %d %d
14488 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67  \n", pPager, pPg
14489 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a  ->pgno, pgno))..
1448a 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72    /* If the jour
1448b 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20  nal needs to be 
1448c 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20  sync()ed before 
1448d 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 63  page pPg->pgno c
1448e 61 6e 0a 20 20 2a 2a 20 62 65 20 77 72 69 74 74  an.  ** be writt
1448f 65 6e 20 74 6f 2c 20 73 74 6f 72 65 20 70 50 67  en to, store pPg
14490 2d 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c 20  ->pgno in local 
14491 76 61 72 69 61 62 6c 65 20 6e 65 65 64 53 79 6e  variable needSyn
14492 63 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  cPgno..  **.  **
14493 20 49 66 20 74 68 65 20 69 73 43 6f 6d 6d 69 74   If the isCommit
14494 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
14495 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74  ere is no need t
14496 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74 0a  o remember that.
14497 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c    ** the journal
14498 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e   needs to be syn
14499 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74  c()ed before dat
1449a 61 62 61 73 65 20 70 61 67 65 20 70 50 67 2d 3e  abase page pPg->
1449b 70 67 6e 6f 20 0a 20 20 2a 2a 20 63 61 6e 20 62  pgno .  ** can b
1449c 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 54 68  e written to. Th
1449d 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 72  e caller has alr
1449e 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20 6e 6f  eady promised no
1449f 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 69 74  t to write to it
144a0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50  ..  */.  if( (pP
144a1 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
144a2 45 45 44 5f 53 59 4e 43 29 20 26 26 20 21 69 73  EED_SYNC) && !is
144a3 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 6e 65  Commit ){.    ne
144a4 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 70 50 67  edSyncPgno = pPg
144a5 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 73 73 65  ->pgno;.    asse
144a6 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61  rt( pageInJourna
144a7 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70  l(pPg) || pPg->p
144a8 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72  gno>pPager->dbOr
144a9 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 61 73  igSize );.    as
144aa 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73  sert( pPg->flags
144ab 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a  &PGHDR_DIRTY );.
144ac 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
144ad 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b 0a  er->needSync );.
144ae 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
144af 20 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 73 20   cache contains 
144b0 61 20 70 61 67 65 20 77 69 74 68 20 70 61 67 65  a page with page
144b1 2d 6e 75 6d 62 65 72 20 70 67 6e 6f 2c 20 72 65  -number pgno, re
144b2 6d 6f 76 65 20 69 74 0a 20 20 2a 2a 20 66 72 6f  move it.  ** fro
144b3 6d 20 69 74 73 20 68 61 73 68 20 63 68 61 69 6e  m its hash chain
144b4 2e 20 41 6c 73 6f 2c 20 69 66 20 74 68 65 20 50  . Also, if the P
144b5 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 77 61  gHdr.needSync wa
144b6 73 20 73 65 74 20 66 6f 72 20 0a 20 20 2a 2a 20  s set for .  ** 
144b7 70 61 67 65 20 70 67 6e 6f 20 62 65 66 6f 72 65  page pgno before
144b8 20 74 68 65 20 27 6d 6f 76 65 27 20 6f 70 65 72   the 'move' oper
144b9 61 74 69 6f 6e 2c 20 69 74 20 6e 65 65 64 73 20  ation, it needs 
144ba 74 6f 20 62 65 20 72 65 74 61 69 6e 65 64 20 0a  to be retained .
144bb 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 61 67    ** for the pag
144bc 65 20 6d 6f 76 65 64 20 74 68 65 72 65 2e 0a 20  e moved there.. 
144bd 20 2a 2f 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73   */.  pPg->flags
144be 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f   &= ~PGHDR_NEED_
144bf 53 59 4e 43 3b 0a 20 20 70 50 67 4f 6c 64 20 3d  SYNC;.  pPgOld =
144c0 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50   pager_lookup(pP
144c1 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61  ager, pgno);.  a
144c2 73 73 65 72 74 28 20 21 70 50 67 4f 6c 64 20 7c  ssert( !pPgOld |
144c3 7c 20 70 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d 3d  | pPgOld->nRef==
144c4 31 20 29 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c  1 );.  if( pPgOl
144c5 64 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 66 6c  d ){.    pPg->fl
144c6 61 67 73 20 7c 3d 20 28 70 50 67 4f 6c 64 2d 3e  ags |= (pPgOld->
144c7 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
144c8 5f 53 59 4e 43 29 3b 0a 20 20 20 20 73 71 6c 69  _SYNC);.    sqli
144c9 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50  te3PcacheDrop(pP
144ca 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 6f 72  gOld);.  }..  or
144cb 69 67 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67  igPgno = pPg->pg
144cc 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61  no;.  sqlite3Pca
144cd 63 68 65 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e  cheMove(pPg, pgn
144ce 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61  o);.  sqlite3Pca
144cf 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67  cheMakeDirty(pPg
144d0 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4d  );.  pPager->dbM
144d1 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20  odified = 1;..  
144d2 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f  if( needSyncPgno
144d3 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65   ){.    /* If ne
144d4 65 64 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f  edSyncPgno is no
144d5 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n-zero, then the
144d6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65   journal file ne
144d7 65 64 73 20 74 6f 20 62 65 20 0a 20 20 20 20 2a  eds to be .    *
144d8 2a 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72  * sync()ed befor
144d9 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 77 72  e any data is wr
144da 69 74 74 65 6e 20 74 6f 20 64 61 74 61 62 61 73  itten to databas
144db 65 20 66 69 6c 65 20 70 61 67 65 20 6e 65 65 64  e file page need
144dc 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a  SyncPgno..    **
144dd 20 43 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73   Currently, no s
144de 75 63 68 20 70 61 67 65 20 65 78 69 73 74 73 20  uch page exists 
144df 69 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 68  in the page-cach
144e0 65 20 61 6e 64 20 74 68 65 20 0a 20 20 20 20 2a  e and the .    *
144e1 2a 20 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22  * "is journaled"
144e2 20 62 69 74 76 65 63 20 66 6c 61 67 20 68 61 73   bitvec flag has
144e3 20 62 65 65 6e 20 73 65 74 2e 20 54 68 69 73 20   been set. This 
144e4 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d 65  needs to be reme
144e5 64 69 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c  died by.    ** l
144e6 6f 61 64 69 6e 67 20 74 68 65 20 70 61 67 65 20  oading the page 
144e7 69 6e 74 6f 20 74 68 65 20 70 61 67 65 72 2d 63  into the pager-c
144e8 61 63 68 65 20 61 6e 64 20 73 65 74 74 69 6e 67  ache and setting
144e9 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53   the PgHdr.needS
144ea 79 6e 63 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67  ync .    ** flag
144eb 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
144ec 49 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74  If the attempt t
144ed 6f 20 6c 6f 61 64 20 74 68 65 20 70 61 67 65 20  o load the page 
144ee 69 6e 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61  into the page-ca
144ef 63 68 65 20 66 61 69 6c 73 2c 20 28 64 75 65 0a  che fails, (due.
144f0 20 20 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c      ** to a mall
144f1 6f 63 28 29 20 6f 72 20 49 4f 20 66 61 69 6c 75  oc() or IO failu
144f2 72 65 29 2c 20 63 6c 65 61 72 20 74 68 65 20 62  re), clear the b
144f3 69 74 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75  it in the pInJou
144f4 72 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72  rnal[].    ** ar
144f5 72 61 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ray. Otherwise, 
144f6 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6c  if the page is l
144f7 6f 61 64 65 64 20 61 6e 64 20 77 72 69 74 74 65  oaded and writte
144f8 6e 20 61 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a  n again in.    *
144f9 2a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  * this transacti
144fa 6f 6e 2c 20 69 74 20 6d 61 79 20 62 65 20 77 72  on, it may be wr
144fb 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74  itten to the dat
144fc 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72  abase file befor
144fd 65 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 73  e.    ** it is s
144fe 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6a  ynced into the j
144ff 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69  ournal file. Thi
14500 73 20 77 61 79 2c 20 69 74 20 6d 61 79 20 65 6e  s way, it may en
14501 64 20 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74  d up in.    ** t
14502 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
14503 74 77 69 63 65 2c 20 62 75 74 20 74 68 61 74 20  twice, but that 
14504 69 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d  is not a problem
14505 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
14506 54 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72  The sqlite3Pager
14507 47 65 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 63  Get() call may c
14508 61 75 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  ause the journal
14509 20 74 6f 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b   to sync. So mak
1450a 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 74 68  e.    ** sure th
1450b 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63  e Pager.needSync
1450c 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 6f   flag is set too
1450d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 50 67 48  ..    */.    PgH
1450e 64 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 20 20  dr *pPgHdr;.    
1450f 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
14510 6e 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 20 20  needSync );.    
14511 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
14512 72 47 65 74 28 70 50 61 67 65 72 2c 20 6e 65 65  rGet(pPager, nee
14513 64 53 79 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48  dSyncPgno, &pPgH
14514 64 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  dr);.    if( rc!
14515 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
14516 20 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63      if( needSync
14517 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62  Pgno<=pPager->db
14518 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20  OrigSize ){.    
14519 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
1451a 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 21 3d 30  er->pTmpSpace!=0
1451b 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
1451c 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 70  te3BitvecClear(p
1451d 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
1451e 6c 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c  l, needSyncPgno,
1451f 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61   pPager->pTmpSpa
14520 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ce);.      }.   
14521 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
14522 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e    }.    pPager->
14523 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20  needSync = 1;.  
14524 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
14525 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26 26 20 21  ->noSync==0 && !
14526 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 70 50 67  MEMDB );.    pPg
14527 48 64 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47  Hdr->flags |= PG
14528 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20  HDR_NEED_SYNC;. 
14529 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
1452a 4d 61 6b 65 44 69 72 74 79 28 70 50 67 48 64 72  MakeDirty(pPgHdr
1452b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  );.    sqlite3Pa
1452c 67 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29  gerUnref(pPgHdr)
1452d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ;.  }..  /*.  **
1452e 20 46 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72   For an in-memor
1452f 79 20 64 61 74 61 62 61 73 65 2c 20 6d 61 6b 65  y database, make
14530 20 73 75 72 65 20 74 68 65 20 6f 72 69 67 69 6e   sure the origin
14531 61 6c 20 70 61 67 65 20 63 6f 6e 74 69 6e 75 65  al page continue
14532 73 0a 20 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c  s.  ** to exist,
14533 20 69 6e 20 63 61 73 65 20 74 68 65 20 74 72 61   in case the tra
14534 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74  nsaction needs t
14535 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 20 57 65  o roll back.  We
14536 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 74   allocate.  ** t
14537 68 65 20 70 61 67 65 20 6e 6f 77 2c 20 69 6e 73  he page now, ins
14538 74 65 61 64 20 6f 66 20 61 74 20 72 6f 6c 6c 62  tead of at rollb
14539 61 63 6b 2c 20 62 65 63 61 75 73 65 20 77 65 20  ack, because we 
1453a 63 61 6e 20 62 65 74 74 65 72 20 64 65 61 6c 0a  can better deal.
1453b 20 20 2a 2a 20 77 69 74 68 20 61 6e 20 6f 75 74    ** with an out
1453c 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72  -of-memory error
1453d 20 6e 6f 77 2e 20 20 54 69 63 6b 65 74 20 23 33   now.  Ticket #3
1453e 37 36 31 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  761..  */.  if( 
1453f 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 44 62 50  MEMDB ){.    DbP
14540 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 20 20 72  age *pNew;.    r
14541 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
14542 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20  Acquire(pPager, 
14543 6f 72 69 67 50 67 6e 6f 2c 20 26 70 4e 65 77 2c  origPgno, &pNew,
14544 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21   1);.    if( rc!
14545 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
14546 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
14547 65 4d 6f 76 65 28 70 50 67 2c 20 6f 72 69 67 50  eMove(pPg, origP
14548 67 6e 6f 29 3b 0a 20 20 20 20 20 20 72 65 74 75  gno);.      retu
14549 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
1454a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
1454b 65 66 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20  ef(pNew);.  }.. 
1454c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1454d 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  K;.}.#endif../*.
1454e 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
1454f 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61 20  ter to the data 
14550 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65  for the specifie
14551 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54  d page..*/.SQLIT
14552 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
14553 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
14554 61 74 61 28 44 62 50 61 67 65 20 2a 70 50 67 29  ata(DbPage *pPg)
14555 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d  {.  assert( pPg-
14556 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e  >nRef>0 || pPg->
14557 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b  pPager->memDb );
14558 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70  .  return pPg->p
14559 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  Data;.}../*.** R
1455a 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1455b 74 6f 20 74 68 65 20 50 61 67 65 72 2e 6e 45 78  to the Pager.nEx
1455c 74 72 61 20 62 79 74 65 73 20 6f 66 20 22 65 78  tra bytes of "ex
1455d 74 72 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 61  tra" space .** a
1455e 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77  llocated along w
1455f 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 65  ith the specifie
14560 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54  d page..*/.SQLIT
14561 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
14562 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
14563 78 74 72 61 28 44 62 50 61 67 65 20 2a 70 50 67  xtra(DbPage *pPg
14564 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d  ){.  return pPg-
14565 3e 70 45 78 74 72 61 3b 0a 7d 0a 0a 2f 2a 0a 2a  >pExtra;.}../*.*
14566 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 6c 6f  * Get/set the lo
14567 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 74  cking-mode for t
14568 68 69 73 20 70 61 67 65 72 2e 20 50 61 72 61 6d  his pager. Param
14569 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 74 20  eter eMode must 
1456a 62 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 47  be one.** of PAG
1456b 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51  ER_LOCKINGMODE_Q
1456c 55 45 52 59 2c 20 50 41 47 45 52 5f 4c 4f 43 4b  UERY, PAGER_LOCK
1456d 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f  INGMODE_NORMAL o
1456e 72 20 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b  r .** PAGER_LOCK
1456f 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
14570 45 2e 20 49 66 20 74 68 65 20 70 61 72 61 6d 65  E. If the parame
14571 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52  ter is not _QUER
14572 59 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6c  Y, then.** the l
14573 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 69 73 20 73  ocking-mode is s
14574 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  et to the value 
14575 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a  specified..**.**
14576 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61   The returned va
14577 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 50 41  lue is either PA
14578 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
14579 4e 4f 52 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 47  NORMAL or.** PAG
1457a 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
1457b 58 43 4c 55 53 49 56 45 2c 20 69 6e 64 69 63 61  XCLUSIVE, indica
1457c 74 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74  ting the current
1457d 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74   (possibly updat
1457e 65 64 29 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d 6d  ed).** locking-m
1457f 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ode..*/.SQLITE_P
14580 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
14581 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
14582 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  de(Pager *pPager
14583 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20  , int eMode){.  
14584 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50  assert( eMode==P
14585 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
14586 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20 20  _QUERY.         
14587 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
14588 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
14589 4f 52 4d 41 4c 0a 20 20 20 20 20 20 20 20 20 20  ORMAL.          
1458a 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45    || eMode==PAGE
1458b 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58  R_LOCKINGMODE_EX
1458c 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73  CLUSIVE );.  ass
1458d 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49  ert( PAGER_LOCKI
1458e 4e 47 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29  NGMODE_QUERY<0 )
1458f 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45  ;.  assert( PAGE
14590 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f  R_LOCKINGMODE_NO
14591 52 4d 41 4c 3e 3d 30 20 26 26 20 50 41 47 45 52  RMAL>=0 && PAGER
14592 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
14593 4c 55 53 49 56 45 3e 3d 30 20 29 3b 0a 20 20 69  LUSIVE>=0 );.  i
14594 66 28 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21  f( eMode>=0 && !
14595 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
14596 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
14597 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20  exclusiveMode = 
14598 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20  (u8)eMode;.  }. 
14599 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61   return (int)pPa
1459a 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
1459b 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  de;.}../*.** Get
1459c 2f 73 65 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c  /set the journal
1459d 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70  -mode for this p
1459e 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20  ager. Parameter 
1459f 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e  eMode must be on
145a0 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50  e of:.**.**    P
145a1 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
145a2 5f 51 55 45 52 59 0a 2a 2a 20 20 20 20 50 41 47  _QUERY.**    PAG
145a3 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44  ER_JOURNALMODE_D
145a4 45 4c 45 54 45 0a 2a 2a 20 20 20 20 50 41 47 45  ELETE.**    PAGE
145a5 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52  R_JOURNALMODE_TR
145a6 55 4e 43 41 54 45 0a 2a 2a 20 20 20 20 50 41 47  UNCATE.**    PAG
145a7 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50  ER_JOURNALMODE_P
145a8 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 50 41 47  ERSIST.**    PAG
145a9 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
145aa 46 46 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a  FF.**    PAGER_J
145ab 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
145ac 59 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  Y.**.** If the p
145ad 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20  arameter is not 
145ae 5f 51 55 45 52 59 2c 20 74 68 65 6e 20 74 68 65  _QUERY, then the
145af 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 69 73   journal_mode is
145b0 20 73 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76   set to the.** v
145b1 61 6c 75 65 20 73 70 65 63 69 66 69 65 64 20 69  alue specified i
145b2 66 20 74 68 65 20 63 68 61 6e 67 65 20 69 73 20  f the change is 
145b3 61 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 63 68  allowed.  The ch
145b4 61 6e 67 65 20 69 73 20 64 69 73 61 6c 6c 6f 77  ange is disallow
145b5 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 66 6f  ed.** for the fo
145b6 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a  llowing reasons:
145b7 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 69  .**.**   *  An i
145b8 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
145b9 65 20 63 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20  e can only have 
145ba 69 74 73 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  its journal_mode
145bb 20 73 65 74 20 74 6f 20 5f 4f 46 46 0a 2a 2a 20   set to _OFF.** 
145bc 20 20 20 20 20 6f 72 20 5f 4d 45 4d 4f 52 59 2e       or _MEMORY.
145bd 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 54 68 65 20  .**.**   *  The 
145be 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 6d 61 79  journal mode may
145bf 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20   not be changed 
145c0 77 68 69 6c 65 20 61 20 74 72 61 6e 73 61 63 74  while a transact
145c1 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a  ion is active..*
145c2 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  *.** The returne
145c3 64 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 63  d indicate the c
145c4 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62 6c 79  urrent (possibly
145c5 20 75 70 64 61 74 65 64 29 20 6a 6f 75 72 6e 61   updated) journa
145c6 6c 2d 6d 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54  l-mode..*/.SQLIT
145c7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
145c8 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
145c9 6c 4d 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61  lMode(Pager *pPa
145ca 67 65 72 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b  ger, int eMode){
145cb 0a 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65  .  assert( eMode
145cc 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
145cd 4f 44 45 5f 51 55 45 52 59 0a 20 20 20 20 20 20  ODE_QUERY.      
145ce 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d        || eMode==
145cf 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
145d0 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20 20  E_DELETE.       
145d1 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50       || eMode==P
145d2 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
145d3 5f 54 52 55 4e 43 41 54 45 0a 20 20 20 20 20 20  _TRUNCATE.      
145d4 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d        || eMode==
145d5 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
145d6 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 20 20  E_PERSIST.      
145d7 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d        || eMode==
145d8 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
145d9 45 5f 4f 46 46 20 0a 20 20 20 20 20 20 20 20 20  E_OFF .         
145da 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
145db 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d  ER_JOURNALMODE_M
145dc 45 4d 4f 52 59 20 29 3b 0a 20 20 61 73 73 65 72  EMORY );.  asser
145dd 74 28 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  t( PAGER_JOURNAL
145de 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a  MODE_QUERY<0 );.
145df 20 20 69 66 28 20 65 4d 6f 64 65 3e 3d 30 0a 20    if( eMode>=0. 
145e0 20 20 26 26 20 28 21 4d 45 4d 44 42 20 7c 7c 20    && (!MEMDB || 
145e1 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
145e2 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
145e3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c  .              |
145e4 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
145e5 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a  OURNALMODE_OFF).
145e6 20 20 20 26 26 20 21 70 50 61 67 65 72 2d 3e 64     && !pPager->d
145e7 62 4d 6f 64 69 66 69 65 64 0a 20 20 20 26 26 20  bModified.   && 
145e8 28 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  (!isOpen(pPager-
145e9 3e 6a 66 64 29 20 7c 7c 20 30 3d 3d 70 50 61 67  >jfd) || 0==pPag
145ea 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 0a  er->journalOff).
145eb 20 20 29 7b 0a 20 20 20 20 69 66 28 20 69 73 4f    ){.    if( isO
145ec 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
145ed 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
145ee 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
145ef 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20  >jfd);.    }.   
145f0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
145f1 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 65  Mode = (u8)eMode
145f2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
145f3 69 6e 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72  int)pPager->jour
145f4 6e 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  nalMode;.}../*.*
145f5 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 73 69  * Get/set the si
145f6 7a 65 2d 6c 69 6d 69 74 20 75 73 65 64 20 66 6f  ze-limit used fo
145f7 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75  r persistent jou
145f8 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a  rnal files..**.*
145f9 2a 20 53 65 74 74 69 6e 67 20 74 68 65 20 73 69  * Setting the si
145fa 7a 65 20 6c 69 6d 69 74 20 74 6f 20 2d 31 20 6d  ze limit to -1 m
145fb 65 61 6e 73 20 6e 6f 20 6c 69 6d 69 74 20 69 73  eans no limit is
145fc 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 41 6e   enforced..** An
145fd 20 61 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20   attempt to set 
145fe 61 20 6c 69 6d 69 74 20 73 6d 61 6c 6c 65 72 20  a limit smaller 
145ff 74 68 61 6e 20 2d 31 20 69 73 20 61 20 6e 6f 2d  than -1 is a no-
14600 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  op..*/.SQLITE_PR
14601 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65  IVATE i64 sqlite
14602 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a  3PagerJournalSiz
14603 65 4c 69 6d 69 74 28 50 61 67 65 72 20 2a 70 50  eLimit(Pager *pP
14604 61 67 65 72 2c 20 69 36 34 20 69 4c 69 6d 69 74  ager, i64 iLimit
14605 29 7b 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e  ){.  if( iLimit>
14606 3d 2d 31 20 29 7b 0a 20 20 20 20 70 50 61 67 65  =-1 ){.    pPage
14607 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  r->journalSizeLi
14608 6d 69 74 20 3d 20 69 4c 69 6d 69 74 3b 0a 20 20  mit = iLimit;.  
14609 7d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  }.  return pPage
1460a 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  r->journalSizeLi
1460b 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  mit;.}../*.** Re
1460c 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
1460d 6f 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70 42  o the pPager->pB
1460e 61 63 6b 75 70 20 76 61 72 69 61 62 6c 65 2e 20  ackup variable. 
1460f 54 68 65 20 62 61 63 6b 75 70 20 6d 6f 64 75 6c  The backup modul
14610 65 0a 2a 2a 20 69 6e 20 62 61 63 6b 75 70 2e 63  e.** in backup.c
14611 20 6d 61 69 6e 74 61 69 6e 73 20 74 68 65 20 63   maintains the c
14612 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 76  ontent of this v
14613 61 72 69 61 62 6c 65 2e 20 54 68 69 73 20 6d 6f  ariable. This mo
14614 64 75 6c 65 0a 2a 2a 20 75 73 65 73 20 69 74 20  dule.** uses it 
14615 6f 70 61 71 75 65 6c 79 20 61 73 20 61 6e 20 61  opaquely as an a
14616 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
14617 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 28  e3BackupRestart(
14618 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ) and.** sqlite3
14619 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 6f  BackupUpdate() o
1461a 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly..*/.SQLITE_P
1461b 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 62  RIVATE sqlite3_b
1461c 61 63 6b 75 70 20 2a 2a 73 71 6c 69 74 65 33 50  ackup **sqlite3P
1461d 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 50 61  agerBackupPtr(Pa
1461e 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
1461f 72 65 74 75 72 6e 20 26 70 50 61 67 65 72 2d 3e  return &pPager->
14620 70 42 61 63 6b 75 70 3b 0a 7d 0a 0a 23 65 6e 64  pBackup;.}..#end
14621 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
14622 54 5f 44 49 53 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a  T_DISKIO */../**
14623 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
14624 20 6f 66 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a   of pager.c ****
14625 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14626 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14627 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
14628 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
14629 69 6e 20 66 69 6c 65 20 62 74 6d 75 74 65 78 2e  in file btmutex.
1462a 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1462b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1462c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
1462d 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32  ** 2007 August 2
1462e 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
1462f 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
14630 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
14631 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
14632 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
14633 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
14634 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
14635 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
14636 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
14637 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
14638 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
14639 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1463a 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1463b 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1463c 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1463d 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1463e 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1463f 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
14640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14641 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14642 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14643 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14644 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 6d 75  .**.** $Id: btmu
14645 74 65 78 2e 63 2c 76 20 31 2e 31 37 20 32 30 30  tex.c,v 1.17 200
14646 39 2f 30 37 2f 32 30 20 31 32 3a 33 33 3a 33 33  9/07/20 12:33:33
14647 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a   drh Exp $.**.**
14648 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
14649 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  ins code used to
1464a 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
1464b 65 73 20 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65  es on Btree obje
1464c 63 74 73 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64  cts..** This cod
1464d 65 20 72 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73  e really belongs
1464e 20 69 6e 20 62 74 72 65 65 2e 63 2e 20 20 42 75   in btree.c.  Bu
1464f 74 20 62 74 72 65 65 2e 63 20 69 73 20 67 65 74  t btree.c is get
14650 74 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20  ting too.** big 
14651 61 6e 64 20 77 65 20 77 61 6e 74 20 74 6f 20 62  and we want to b
14652 72 65 61 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d  reak it down som
14653 65 2e 20 20 54 68 69 73 20 70 61 63 6b 61 67 65  e.  This package
14654 64 20 73 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a  d seemed like.**
14655 20 61 20 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74   a good breakout
14656 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..*/./**********
14657 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72  **** Include btr
14658 65 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d  eeInt.h in the m
14659 69 64 64 6c 65 20 6f 66 20 62 74 6d 75 74 65 78  iddle of btmutex
1465a 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1465b 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1465c 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1465d 62 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  btreeInt.h *****
1465e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1465f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14660 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
14661 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68  April 6.**.** Th
14662 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
14663 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
14664 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
14665 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
14666 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
14667 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
14668 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
14669 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1466a 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1466b 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1466c 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1466d 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1466e 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1466f 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
14670 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
14671 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
14672 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
14673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14674 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14675 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14676 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14677 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62  ******.** $Id: b
14678 74 72 65 65 49 6e 74 2e 68 2c 76 20 31 2e 35 32  treeInt.h,v 1.52
14679 20 32 30 30 39 2f 30 37 2f 31 35 20 31 37 3a 32   2009/07/15 17:2
1467a 35 3a 34 36 20 64 72 68 20 45 78 70 20 24 0a 2a  5:46 drh Exp $.*
1467b 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69  *.** This file i
1467c 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65  mplements a exte
1467d 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64  rnal (disk-based
1467e 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67  ) database using
1467f 20 42 54 72 65 65 73 2e 0a 2a 2a 20 46 6f 72 20   BTrees..** For 
14680 61 20 64 65 74 61 69 6c 65 64 20 64 69 73 63 75  a detailed discu
14681 73 73 69 6f 6e 20 6f 66 20 42 54 72 65 65 73 2c  ssion of BTrees,
14682 20 72 65 66 65 72 20 74 6f 0a 2a 2a 0a 2a 2a 20   refer to.**.** 
14683 20 20 20 20 44 6f 6e 61 6c 64 20 45 2e 20 4b 6e      Donald E. Kn
14684 75 74 68 2c 20 54 48 45 20 41 52 54 20 4f 46 20  uth, THE ART OF 
14685 43 4f 4d 50 55 54 45 52 20 50 52 4f 47 52 41 4d  COMPUTER PROGRAM
14686 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65 20 33 3a 0a  MING, Volume 3:.
14687 2a 2a 20 20 20 20 20 22 53 6f 72 74 69 6e 67 20  **     "Sorting 
14688 41 6e 64 20 53 65 61 72 63 68 69 6e 67 22 2c 20  And Searching", 
14689 70 61 67 65 73 20 34 37 33 2d 34 38 30 2e 20 41  pages 473-480. A
1468a 64 64 69 73 6f 6e 2d 57 65 73 6c 65 79 0a 2a 2a  ddison-Wesley.**
1468b 20 20 20 20 20 50 75 62 6c 69 73 68 69 6e 67 20       Publishing 
1468c 43 6f 6d 70 61 6e 79 2c 20 52 65 61 64 69 6e 67  Company, Reading
1468d 2c 20 4d 61 73 73 61 63 68 75 73 65 74 74 73 2e  , Massachusetts.
1468e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63  .**.** The basic
1468f 20 69 64 65 61 20 69 73 20 74 68 61 74 20 65 61   idea is that ea
14690 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 66  ch page of the f
14691 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 4e 20 64  ile contains N d
14692 61 74 61 62 61 73 65 0a 2a 2a 20 65 6e 74 72 69  atabase.** entri
14693 65 73 20 61 6e 64 20 4e 2b 31 20 70 6f 69 6e 74  es and N+1 point
14694 65 72 73 20 74 6f 20 73 75 62 70 61 67 65 73 2e  ers to subpages.
14695 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  .**.**   -------
14696 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14697 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14698 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14699 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 7c  ---------.**   |
1469a 20 20 50 74 72 28 30 29 20 7c 20 4b 65 79 28 30    Ptr(0) | Key(0
1469b 29 20 7c 20 50 74 72 28 31 29 20 7c 20 4b 65 79  ) | Ptr(1) | Key
1469c 28 31 29 20 7c 20 2e 2e 2e 20 7c 20 4b 65 79 28  (1) | ... | Key(
1469d 4e 2d 31 29 20 7c 20 50 74 72 28 4e 29 20 7c 0a  N-1) | Ptr(N) |.
1469e 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
1469f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146a1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146a2 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  -----.**.** All 
146a3 6f 66 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 74  of the keys on t
146a4 68 65 20 70 61 67 65 20 74 68 61 74 20 50 74 72  he page that Ptr
146a5 28 30 29 20 70 6f 69 6e 74 73 20 74 6f 20 68 61  (0) points to ha
146a6 76 65 20 76 61 6c 75 65 73 20 6c 65 73 73 0a 2a  ve values less.*
146a7 2a 20 74 68 61 6e 20 4b 65 79 28 30 29 2e 20 20  * than Key(0).  
146a8 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20  All of the keys 
146a9 6f 6e 20 70 61 67 65 20 50 74 72 28 31 29 20 61  on page Ptr(1) a
146aa 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73 20  nd its subpages 
146ab 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73 20 67  have.** values g
146ac 72 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28  reater than Key(
146ad 30 29 20 61 6e 64 20 6c 65 73 73 20 74 68 61 6e  0) and less than
146ae 20 4b 65 79 28 31 29 2e 20 20 41 6c 6c 20 6f 66   Key(1).  All of
146af 20 74 68 65 20 6b 65 79 73 0a 2a 2a 20 6f 6e 20   the keys.** on 
146b0 50 74 72 28 4e 29 20 61 6e 64 20 69 74 73 20 73  Ptr(N) and its s
146b1 75 62 70 61 67 65 73 20 68 61 76 65 20 76 61 6c  ubpages have val
146b2 75 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e  ues greater than
146b3 20 4b 65 79 28 4e 2d 31 29 2e 20 20 41 6e 64 0a   Key(N-1).  And.
146b4 2a 2a 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 0a  ** so forth..**.
146b5 2a 2a 20 46 69 6e 64 69 6e 67 20 61 20 70 61 72  ** Finding a par
146b6 74 69 63 75 6c 61 72 20 6b 65 79 20 72 65 71 75  ticular key requ
146b7 69 72 65 73 20 72 65 61 64 69 6e 67 20 4f 28 6c  ires reading O(l
146b8 6f 67 28 4d 29 29 20 70 61 67 65 73 20 66 72 6f  og(M)) pages fro
146b9 6d 20 74 68 65 20 0a 2a 2a 20 64 69 73 6b 20 77  m the .** disk w
146ba 68 65 72 65 20 4d 20 69 73 20 74 68 65 20 6e 75  here M is the nu
146bb 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
146bc 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a  in the tree..**.
146bd 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c 65  ** In this imple
146be 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 20 73 69 6e  mentation, a sin
146bf 67 6c 65 20 66 69 6c 65 20 63 61 6e 20 68 6f 6c  gle file can hol
146c0 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 65  d one or more se
146c1 70 61 72 61 74 65 20 0a 2a 2a 20 42 54 72 65 65  parate .** BTree
146c2 73 2e 20 20 45 61 63 68 20 42 54 72 65 65 20 69  s.  Each BTree i
146c3 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  s identified by 
146c4 74 68 65 20 69 6e 64 65 78 20 6f 66 20 69 74 73  the index of its
146c5 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 54 68 65   root page.  The
146c6 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61  .** key and data
146c7 20 66 6f 72 20 61 6e 79 20 65 6e 74 72 79 20 61   for any entry a
146c8 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66  re combined to f
146c9 6f 72 6d 20 74 68 65 20 22 70 61 79 6c 6f 61 64  orm the "payload
146ca 22 2e 20 20 41 0a 2a 2a 20 66 69 78 65 64 20 61  ".  A.** fixed a
146cb 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64  mount of payload
146cc 20 63 61 6e 20 62 65 20 63 61 72 72 69 65 64 20   can be carried 
146cd 64 69 72 65 63 74 6c 79 20 6f 6e 20 74 68 65 20  directly on the 
146ce 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67 65  database.** page
146cf 2e 20 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61  .  If the payloa
146d0 64 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  d is larger than
146d1 20 74 68 65 20 70 72 65 73 65 74 20 61 6d 6f 75   the preset amou
146d2 6e 74 20 74 68 65 6e 20 73 75 72 70 6c 75 73 0a  nt then surplus.
146d3 2a 2a 20 62 79 74 65 73 20 61 72 65 20 73 74 6f  ** bytes are sto
146d4 72 65 64 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20  red on overflow 
146d5 70 61 67 65 73 2e 20 20 54 68 65 20 70 61 79 6c  pages.  The payl
146d6 6f 61 64 20 66 6f 72 20 61 6e 20 65 6e 74 72 79  oad for an entry
146d7 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 65 63  .** and the prec
146d8 65 64 69 6e 67 20 70 6f 69 6e 74 65 72 20 61 72  eding pointer ar
146d9 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66 6f  e combined to fo
146da 72 6d 20 61 20 22 43 65 6c 6c 22 2e 20 20 45 61  rm a "Cell".  Ea
146db 63 68 20 0a 2a 2a 20 70 61 67 65 20 68 61 73 20  ch .** page has 
146dc 61 20 73 6d 61 6c 6c 20 68 65 61 64 65 72 20 77  a small header w
146dd 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 20 74 68  hich contains th
146de 65 20 50 74 72 28 4e 29 20 70 6f 69 6e 74 65 72  e Ptr(N) pointer
146df 20 61 6e 64 20 6f 74 68 65 72 0a 2a 2a 20 69 6e   and other.** in
146e0 66 6f 72 6d 61 74 69 6f 6e 20 73 75 63 68 20 61  formation such a
146e1 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 6b 65  s the size of ke
146e2 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 0a 2a  y and data..**.*
146e3 2a 20 46 4f 52 4d 41 54 20 44 45 54 41 49 4c 53  * FORMAT DETAILS
146e4 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20  .**.** The file 
146e5 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20  is divided into 
146e6 70 61 67 65 73 2e 20 20 54 68 65 20 66 69 72 73  pages.  The firs
146e7 74 20 70 61 67 65 20 69 73 20 63 61 6c 6c 65 64  t page is called
146e8 20 70 61 67 65 20 31 2c 0a 2a 2a 20 74 68 65 20   page 1,.** the 
146e9 73 65 63 6f 6e 64 20 69 73 20 70 61 67 65 20 32  second is page 2
146ea 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20  , and so forth. 
146eb 20 41 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f   A page number o
146ec 66 20 7a 65 72 6f 20 69 6e 64 69 63 61 74 65 73  f zero indicates
146ed 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 20 70 61 67  .** "no such pag
146ee 65 22 2e 20 20 54 68 65 20 70 61 67 65 20 73 69  e".  The page si
146ef 7a 65 20 63 61 6e 20 62 65 20 61 6e 79 20 70 6f  ze can be any po
146f0 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 6e  wer of 2 between
146f1 20 35 31 32 20 61 6e 64 20 33 32 37 36 38 2e 0a   512 and 32768..
146f2 2a 2a 20 45 61 63 68 20 70 61 67 65 20 63 61 6e  ** Each page can
146f3 20 62 65 20 65 69 74 68 65 72 20 61 20 62 74 72   be either a btr
146f4 65 65 20 70 61 67 65 2c 20 61 20 66 72 65 65 6c  ee page, a freel
146f5 69 73 74 20 70 61 67 65 2c 20 61 6e 20 6f 76 65  ist page, an ove
146f6 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 6f  rflow.** page, o
146f7 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  r a pointer-map 
146f8 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  page..**.** The 
146f9 66 69 72 73 74 20 70 61 67 65 20 69 73 20 61 6c  first page is al
146fa 77 61 79 73 20 61 20 62 74 72 65 65 20 70 61 67  ways a btree pag
146fb 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 31 30  e.  The first 10
146fc 30 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 66  0 bytes of the f
146fd 69 72 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e  irst.** page con
146fe 74 61 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68  tain a special h
146ff 65 61 64 65 72 20 28 74 68 65 20 22 66 69 6c 65  eader (the "file
14700 20 68 65 61 64 65 72 22 29 20 74 68 61 74 20 64   header") that d
14701 65 73 63 72 69 62 65 73 20 74 68 65 20 66 69 6c  escribes the fil
14702 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74  e..** The format
14703 20 6f 66 20 74 68 65 20 66 69 6c 65 20 68 65 61   of the file hea
14704 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  der is as follow
14705 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45  s:.**.**   OFFSE
14706 54 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43  T   SIZE    DESC
14707 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20  RIPTION.**      
14708 30 20 20 20 20 20 20 31 36 20 20 20 20 20 48 65  0      16     He
14709 61 64 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51  ader string: "SQ
1470a 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30  Lite format 3\00
1470b 30 22 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20  0".**     16    
1470c 20 20 20 32 20 20 20 20 20 50 61 67 65 20 73 69     2     Page si
1470d 7a 65 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a  ze in bytes.  .*
1470e 2a 20 20 20 20 20 31 38 20 20 20 20 20 20 20 31  *     18       1
1470f 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74       File format
14710 20 77 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a   write version.*
14711 2a 20 20 20 20 20 31 39 20 20 20 20 20 20 20 31  *     19       1
14712 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74       File format
14713 20 72 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a   read version.**
14714 20 20 20 20 20 32 30 20 20 20 20 20 20 20 31 20       20       1 
14715 20 20 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75      Bytes of unu
14716 73 65 64 20 73 70 61 63 65 20 61 74 20 74 68 65  sed space at the
14717 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67   end of each pag
14718 65 0a 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20  e.**     21     
14719 20 20 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65    1     Max embe
1471a 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61  dded payload fra
1471b 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20  ction.**     22 
1471c 20 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20        1     Min 
1471d 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64  embedded payload
1471e 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20   fraction.**    
1471f 20 32 33 20 20 20 20 20 20 20 31 20 20 20 20 20   23       1     
14720 4d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64  Min leaf payload
14721 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20   fraction.**    
14722 20 32 34 20 20 20 20 20 20 20 34 20 20 20 20 20   24       4     
14723 46 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e  File change coun
14724 74 65 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20  ter.**     28   
14725 20 20 20 20 34 20 20 20 20 20 52 65 73 65 72 76      4     Reserv
14726 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73  ed for future us
14727 65 0a 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20  e.**     32     
14728 20 20 34 20 20 20 20 20 46 69 72 73 74 20 66 72    4     First fr
14729 65 65 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20  eelist page.**  
1472a 20 20 20 33 36 20 20 20 20 20 20 20 34 20 20 20     36       4   
1472b 20 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65    Number of free
1472c 6c 69 73 74 20 70 61 67 65 73 20 69 6e 20 74 68  list pages in th
1472d 65 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30  e file.**     40
1472e 20 20 20 20 20 20 36 30 20 20 20 20 20 31 35 20        60     15 
1472f 34 2d 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75  4-byte meta valu
14730 65 73 20 70 61 73 73 65 64 20 74 6f 20 68 69 67  es passed to hig
14731 68 65 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a  her layers.**.**
14732 20 20 20 20 20 34 30 20 20 20 20 20 20 20 34 20       40       4 
14733 20 20 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69      Schema cooki
14734 65 0a 2a 2a 20 20 20 20 20 34 34 20 20 20 20 20  e.**     44     
14735 20 20 34 20 20 20 20 20 46 69 6c 65 20 66 6f 72    4     File for
14736 6d 61 74 20 6f 66 20 73 63 68 65 6d 61 20 6c 61  mat of schema la
14737 79 65 72 0a 2a 2a 20 20 20 20 20 34 38 20 20 20  yer.**     48   
14738 20 20 20 20 34 20 20 20 20 20 53 69 7a 65 20 6f      4     Size o
14739 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20  f page cache.** 
1473a 20 20 20 20 35 32 20 20 20 20 20 20 20 34 20 20      52       4  
1473b 20 20 20 4c 61 72 67 65 73 74 20 72 6f 6f 74 2d     Largest root-
1473c 70 61 67 65 20 28 61 75 74 6f 2f 69 6e 63 72 5f  page (auto/incr_
1473d 76 61 63 75 75 6d 29 0a 2a 2a 20 20 20 20 20 35  vacuum).**     5
1473e 36 20 20 20 20 20 20 20 34 20 20 20 20 20 31 3d  6       4     1=
1473f 55 54 46 2d 38 20 32 3d 55 54 46 31 36 6c 65 20  UTF-8 2=UTF16le 
14740 33 3d 55 54 46 31 36 62 65 0a 2a 2a 20 20 20 20  3=UTF16be.**    
14741 20 36 30 20 20 20 20 20 20 20 34 20 20 20 20 20   60       4     
14742 55 73 65 72 20 76 65 72 73 69 6f 6e 0a 2a 2a 20  User version.** 
14743 20 20 20 20 36 34 20 20 20 20 20 20 20 34 20 20      64       4  
14744 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76     Incremental v
14745 61 63 75 75 6d 20 6d 6f 64 65 0a 2a 2a 20 20 20  acuum mode.**   
14746 20 20 36 38 20 20 20 20 20 20 20 34 20 20 20 20    68       4    
14747 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 37   unused.**     7
14748 32 20 20 20 20 20 20 20 34 20 20 20 20 20 75 6e  2       4     un
14749 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 36 20 20  used.**     76  
1474a 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 73 65       4     unuse
1474b 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74  d.**.** All of t
1474c 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  he integer value
1474d 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69 61 6e  s are big-endian
1474e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61   (most significa
1474f 6e 74 20 62 79 74 65 20 66 69 72 73 74 29 2e 0a  nt byte first)..
14750 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 63  **.** The file c
14751 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73  hange counter is
14752 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65   incremented whe
14753 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  n the database i
14754 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54 68 69  s changed.** Thi
14755 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f 77 73  s counter allows
14756 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73   other processes
14757 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 74 68   to know when th
14758 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 6e 67  e file has chang
14759 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73 20 77  ed.** and thus w
1475a 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20 74 6f  hen they need to
1475b 20 66 6c 75 73 68 20 74 68 65 69 72 20 63 61 63   flush their cac
1475c 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61  he..**.** The ma
1475d 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f  x embedded paylo
1475e 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 74  ad fraction is t
1475f 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65  he amount of the
14760 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a 2a 2a   total usable.**
14761 20 73 70 61 63 65 20 69 6e 20 61 20 70 61 67 65   space in a page
14762 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6e   that can be con
14763 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e 67 6c  sumed by a singl
14764 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61 6e 64  e cell for stand
14765 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20 28 6e  ard.** B-tree (n
14766 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74 61 62  on-LEAFDATA) tab
14767 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20 6f 66  les.  A value of
14768 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30 25 2e   255 means 100%.
14769 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a    The default.**
1476a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74 68 65   is to limit the
1476b 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69   maximum cell si
1476c 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65  ze so that at le
1476d 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69 6c 6c  ast 4 cells will
1476e 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 70   fit.** on one p
1476f 61 67 65 2e 20 20 54 68 75 73 20 74 68 65 20 64  age.  Thus the d
14770 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62 65 64  efault max embed
14771 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63  ded payload frac
14772 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a 2a  tion is 64..**.*
14773 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64  * If the payload
14774 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73 20 6c   for a cell is l
14775 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d  arger than the m
14776 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 6e  ax payload, then
14777 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c 6f 61   extra.** payloa
14778 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20  d is spilled to 
14779 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
1477a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f   Once an overflo
1477b 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61  w page is alloca
1477c 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79 20  ted,.** as many 
1477d 62 79 74 65 73 20 61 73 20 70 6f 73 73 69 62 6c  bytes as possibl
1477e 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e 74 6f  e are moved into
1477f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
14780 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65 74 74  ges without lett
14781 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20  ing.** the cell 
14782 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f 77 20  size drop below 
14783 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64  the min embedded
14784 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
14785 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e  n..**.** The min
14786 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72   leaf payload fr
14787 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65 20 74  action is like t
14788 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20  he min embedded 
14789 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e  payload fraction
1478a 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20  .** except that 
1478b 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20 6c 65  it applies to le
1478c 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20 4c 45  af nodes in a LE
1478d 41 46 44 41 54 41 20 74 72 65 65 2e 20 20 54 68  AFDATA tree.  Th
1478e 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 61 79  e maximum.** pay
1478f 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 66 6f  load fraction fo
14790 72 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 65  r a LEAFDATA tre
14791 65 20 69 73 20 61 6c 77 61 79 73 20 31 30 30 25  e is always 100%
14792 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20 69 74   (or 255) and it
14793 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66 69 65  .** not specifie
14794 64 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e  d in the header.
14795 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74 72 65  .**.** Each btre
14796 65 20 70 61 67 65 73 20 69 73 20 64 69 76 69 64  e pages is divid
14797 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20 73 65  ed into three se
14798 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68 65 61  ctions:  The hea
14799 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65 6c 6c  der, the.** cell
1479a 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 2c 20   pointer array, 
1479b 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e  and the cell con
1479c 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61 67 65  tent area.  Page
1479d 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20 31 30   1 also has a 10
1479e 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65 20 68  0-byte.** file h
1479f 65 61 64 65 72 20 74 68 61 74 20 6f 63 63 75 72  eader that occur
147a0 73 20 62 65 66 6f 72 65 20 74 68 65 20 70 61 67  s before the pag
147a1 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20  e header..**.** 
147a2 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       |----------
147a3 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20  ------|.**      
147a4 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20 20 20  | file header   
147a5 20 7c 20 20 20 31 30 30 20 62 79 74 65 73 2e 20   |   100 bytes. 
147a6 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a 2a   Page 1 only..**
147a7 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d        |---------
147a8 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20  -------|.**     
147a9 20 7c 20 70 61 67 65 20 68 65 61 64 65 72 20 20   | page header  
147aa 20 20 7c 20 20 20 38 20 62 79 74 65 73 20 66 6f    |   8 bytes fo
147ab 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20 62 79  r leaves.  12 by
147ac 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69 6f 72  tes for interior
147ad 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 7c   nodes.**      |
147ae 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
147af 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c  |.**      | cell
147b0 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20 20 7c   pointer   |   |
147b1 20 20 32 20 62 79 74 65 73 20 70 65 72 20 63 65    2 bytes per ce
147b2 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72 64 65  ll.  Sorted orde
147b3 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 72  r..**      | arr
147b4 61 79 20 20 20 20 20 20 20 20 20 20 7c 20 20 20  ay          |   
147b5 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77 61 72  |  Grows downwar
147b6 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20 20 20  d.**      |     
147b7 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 76             |   v
147b8 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d  .**      |------
147b9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20  ----------|.**  
147ba 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 74 65      | unallocate
147bb 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c  d    |.**      |
147bc 20 73 70 61 63 65 20 20 20 20 20 20 20 20 20 20   space          
147bd 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d  |.**      |-----
147be 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20 5e  -----------|   ^
147bf 20 20 47 72 6f 77 73 20 75 70 77 61 72 64 73 0a    Grows upwards.
147c0 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 63  **      | cell c
147c1 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c 20 20  ontent   |   |  
147c2 41 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 20  Arbitrary order 
147c3 69 6e 74 65 72 73 70 65 72 73 65 64 20 77 69 74  interspersed wit
147c4 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a 2a  h freeblocks..**
147c5 20 20 20 20 20 20 7c 20 61 72 65 61 20 20 20 20        | area    
147c6 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 61 6e         |   |  an
147c7 64 20 66 72 65 65 20 73 70 61 63 65 20 66 72 61  d free space fra
147c8 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 20 20  gments..**      
147c9 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  |---------------
147ca 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67  -|.**.** The pag
147cb 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b 73 20  e headers looks 
147cc 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
147cd 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45     OFFSET   SIZE
147ce 20 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e       DESCRIPTION
147cf 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20  .**      0      
147d0 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e 20 31   1      Flags. 1
147d1 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 72  : intkey, 2: zer
147d2 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66 64 61  odata, 4: leafda
147d3 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a 20 20  ta, 8: leaf.**  
147d4 20 20 20 20 31 20 20 20 20 20 20 20 32 20 20 20      1       2   
147d5 20 20 20 62 79 74 65 20 6f 66 66 73 65 74 20 74     byte offset t
147d6 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65  o the first free
147d7 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 33 20  block.**      3 
147d8 20 20 20 20 20 20 32 20 20 20 20 20 20 6e 75 6d        2      num
147d9 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20  ber of cells on 
147da 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20  this page.**    
147db 20 20 35 20 20 20 20 20 20 20 32 20 20 20 20 20    5       2     
147dc 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74   first byte of t
147dd 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
147de 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37 20 20  area.**      7  
147df 20 20 20 20 20 31 20 20 20 20 20 20 6e 75 6d 62       1      numb
147e0 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 64  er of fragmented
147e1 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a 20 20   free bytes.**  
147e2 20 20 20 20 38 20 20 20 20 20 20 20 34 20 20 20      8       4   
147e3 20 20 20 52 69 67 68 74 20 63 68 69 6c 64 20 28     Right child (
147e4 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c 75 65  the Ptr(N) value
147e5 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e 20 6c  ).  Omitted on l
147e6 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  eaves..**.** The
147e7 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20 74 68   flags define th
147e8 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 69 73  e format of this
147e9 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 54 68   btree page.  Th
147ea 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65 61 6e  e leaf flag mean
147eb 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73 20 70  s that.** this p
147ec 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69 6c 64  age has no child
147ed 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f 64 61  ren.  The zeroda
147ee 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68  ta flag means th
147ef 61 74 20 74 68 69 73 20 70 61 67 65 20 63 61 72  at this page car
147f0 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65 79  ries.** only key
147f1 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e 20 20  s and no data.  
147f2 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61 67 20  The intkey flag 
147f3 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6b  means that the k
147f4 65 79 20 69 73 20 61 20 69 6e 74 65 67 65 72 0a  ey is a integer.
147f5 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74 6f 72  ** which is stor
147f6 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20 73 69  ed in the key si
147f7 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20  ze entry of the 
147f8 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61 74 68  cell header rath
147f9 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 74 68  er than in.** th
147fa 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61 2e 0a  e payload area..
147fb 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70  **.** The cell p
147fc 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62 65 67  ointer array beg
147fd 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72 73 74  ins on the first
147fe 20 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20   byte after the 
147ff 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 20  page header..** 
14800 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  The cell pointer
14801 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20   array contains 
14802 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32 2d 62  zero or more 2-b
14803 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68 69 63  yte numbers whic
14804 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65 74 73  h are.** offsets
14805 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e   from the beginn
14806 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 20  ing of the page 
14807 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  to the cell cont
14808 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a  ent in the cell.
14809 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e  ** content area.
1480a 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74    The cell point
1480b 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73 6f 72  ers occur in sor
1480c 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 65 20  ted order.  The 
1480d 73 79 73 74 65 6d 20 73 74 72 69 76 65 73 0a 2a  system strives.*
1480e 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65 20 73  * to keep free s
1480f 70 61 63 65 20 61 66 74 65 72 20 74 68 65 20 6c  pace after the l
14810 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  ast cell pointer
14811 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63 65 6c   so that new cel
14812 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65 61 73  ls can.** be eas
14813 69 6c 79 20 61 64 64 65 64 20 77 69 74 68 6f 75  ily added withou
14814 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65 66 72  t having to defr
14815 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65 2e  agment the page.
14816 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74  .**.** Cell cont
14817 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 61 74  ent is stored at
14818 20 74 68 65 20 76 65 72 79 20 65 6e 64 20 6f 66   the very end of
14819 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 67 72   the page and gr
1481a 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65 0a 2a  ows toward the.*
1481b 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  * beginning of t
1481c 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 55  he page..**.** U
1481d 6e 75 73 65 64 20 73 70 61 63 65 20 77 69 74 68  nused space with
1481e 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  in the cell cont
1481f 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f 6c 6c  ent area is coll
14820 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c 69 6e  ected into a lin
14821 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 66  ked list of.** f
14822 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61 63 68  reeblocks.  Each
14823 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 61 74   freeblock is at
14824 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 20 69   least 4 bytes i
14825 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62 79 74  n size.  The byt
14826 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f 20 74  e offset.** to t
14827 68 65 20 66 69 72 73 74 20 66 72 65 65 62 6c 6f  he first freeblo
14828 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e 20 74  ck is given in t
14829 68 65 20 68 65 61 64 65 72 2e 20 20 46 72 65 65  he header.  Free
1482a 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69 6e 0a  blocks occur in.
1482b 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20 6f 72  ** increasing or
1482c 64 65 72 2e 20 20 42 65 63 61 75 73 65 20 61 20  der.  Because a 
1482d 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74 20 62  freeblock must b
1482e 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74  e at least 4 byt
1482f 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a 20 61  es in size,.** a
14830 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20 6f 72  ny group of 3 or
14831 20 66 65 77 65 72 20 75 6e 75 73 65 64 20 62 79   fewer unused by
14832 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 20  tes in the cell 
14833 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63 61 6e  content area can
14834 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20  not.** exist on 
14835 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20 63 68  the freeblock ch
14836 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20 6f 66  ain.  A group of
14837 20 33 20 6f 72 20 66 65 77 65 72 20 66 72 65 65   3 or fewer free
14838 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c 65 64   bytes is called
14839 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74 2e 20  .** a fragment. 
1483a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   The total numbe
1483b 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 6c  r of bytes in al
1483c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73 20 72  l fragments is r
1483d 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e 20 74  ecorded..** in t
1483e 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 61  he page header a
1483f 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a 0a 2a  t offset 7..**.*
14840 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53  *    SIZE    DES
14841 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
14842 20 32 20 20 20 20 20 42 79 74 65 20 6f 66 66 73   2     Byte offs
14843 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 66  et of the next f
14844 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20  reeblock.**     
14845 20 32 20 20 20 20 20 42 79 74 65 73 20 69 6e 20   2     Bytes in 
14846 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b 0a 2a  this freeblock.*
14847 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65 20 6f  *.** Cells are o
14848 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74  f variable lengt
14849 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20 73 74  h.  Cells are st
1484a 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65 6c 6c  ored in the cell
1484b 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 61 74   content area at
1484c 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74  .** the end of t
1484d 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e 74 65  he page.  Pointe
1484e 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c 73 20  rs to the cells 
1484f 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c 6c 20  are in the cell 
14850 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 2a 2a  pointer array.**
14851 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74 65 6c   that immediatel
14852 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 70 61  y follows the pa
14853 67 65 20 68 65 61 64 65 72 2e 20 20 43 65 6c 6c  ge header.  Cell
14854 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61  s is not necessa
14855 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 75 6f  rily.** contiguo
14856 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72 2c 20  us or in order, 
14857 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  but cell pointer
14858 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73  s are contiguous
14859 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e 0a 2a   and in order..*
1485a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e  *.** Cell conten
1485b 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66 20 76  t makes use of v
1485c 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69  ariable length i
1485d 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61 72 69  ntegers.  A vari
1485e 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 69  able.** length i
1485f 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f 20 39  nteger is 1 to 9
14860 20 62 79 74 65 73 20 77 68 65 72 65 20 74 68 65   bytes where the
14861 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f 66   lower 7 bits of
14862 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65 20 61   each .** byte a
14863 72 65 20 75 73 65 64 2e 20 20 54 68 65 20 69 6e  re used.  The in
14864 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f  teger consists o
14865 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61 74  f all bytes that
14866 20 68 61 76 65 20 62 69 74 20 38 20 73 65 74 20   have bit 8 set 
14867 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  and.** the first
14868 20 62 79 74 65 20 77 69 74 68 20 62 69 74 20 38   byte with bit 8
14869 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d 6f 73   clear.  The mos
1486a 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 79  t significant by
1486b 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  te of the intege
1486c 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66 69 72  r.** appears fir
1486d 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c 65 2d  st.  A variable-
1486e 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 6d  length integer m
1486f 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74  ay not be more t
14870 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f 6e 67  han 9 bytes long
14871 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63 69 61  ..** As a specia
14872 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20 62 79  l case, all 8 by
14873 74 65 73 20 6f 66 20 74 68 65 20 39 74 68 20 62  tes of the 9th b
14874 79 74 65 20 61 72 65 20 75 73 65 64 20 61 73 20  yte are used as 
14875 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a 20 61  data.  This.** a
14876 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74 20 69  llows a 64-bit i
14877 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65 6e 63  nteger to be enc
14878 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65 73 2e  oded in 9 bytes.
14879 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30 20 20  .**.**    0x00  
1487a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1487b 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30      becomes  0x0
1487c 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20 30 78  0000000.**    0x
1487d 37 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  7f              
1487e 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20          becomes 
1487f 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20   0x0000007f.**  
14880 20 20 30 78 38 31 20 30 78 30 30 20 20 20 20 20    0x81 0x00     
14881 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f              beco
14882 6d 65 73 20 20 30 78 30 30 30 30 30 30 38 30 0a  mes  0x00000080.
14883 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30 30 20  **    0x82 0x00 
14884 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14885 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30  becomes  0x00000
14886 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30 20 30  100.**    0x80 0
14887 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20 20  x7f             
14888 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30      becomes  0x0
14889 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 30 78  000007f.**    0x
1488a 38 61 20 30 78 39 31 20 30 78 64 31 20 30 78 61  8a 0x91 0xd1 0xa
1488b 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65 73 20  c 0x78  becomes 
1488c 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a 20 20   0x12345678.**  
1488d 20 20 30 78 38 31 20 30 78 38 31 20 30 78 38 31    0x81 0x81 0x81
1488e 20 30 78 38 31 20 30 78 30 31 20 20 62 65 63 6f   0x81 0x01  beco
1488f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38 31 0a  mes  0x10204081.
14890 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 6c  **.** Variable l
14891 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 20 61  ength integers a
14892 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f 77 69  re used for rowi
14893 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64 20 74  ds and to hold t
14894 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
14895 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61 6e 64  bytes of key and
14896 20 64 61 74 61 20 69 6e 20 61 20 62 74 72 65 65   data in a btree
14897 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   cell..**.** The
14898 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 65   content of a ce
14899 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68  ll looks like th
1489a 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a  is:.**.**    SIZ
1489b 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e  E    DESCRIPTION
1489c 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 50  .**      4     P
1489d 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
1489e 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20 4f 6d  e left child. Om
1489f 69 74 74 65 64 20 69 66 20 6c 65 61 66 20 66 6c  itted if leaf fl
148a0 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20  ag is set..**   
148a1 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20    var    Number 
148a2 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  of bytes of data
148a3 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74 68 65  . Omitted if the
148a4 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20 69   zerodata flag i
148a5 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76 61  s set..**     va
148a6 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 62  r    Number of b
148a7 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f 72 20  ytes of key. Or 
148a8 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66 20 69  the key itself i
148a9 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73  f intkey flag is
148aa 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20 2a 20   set..**      * 
148ab 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a 20 20      Payload.**  
148ac 20 20 20 20 34 20 20 20 20 20 46 69 72 73 74 20      4     First 
148ad 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76 65 72  page of the over
148ae 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f 6d 69  flow chain.  Omi
148af 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65 72 66  tted if no overf
148b0 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 66 6c  low.**.** Overfl
148b1 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20 61 20  ow pages form a 
148b2 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 45 61  linked list.  Ea
148b3 63 68 20 70 61 67 65 20 65 78 63 65 70 74 20 74  ch page except t
148b4 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d 70 6c  he last is compl
148b5 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 64 20  etely.** filled 
148b6 77 69 74 68 20 64 61 74 61 20 28 70 61 67 65 73  with data (pages
148b7 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29 2e 20  ize - 4 bytes). 
148b8 20 54 68 65 20 6c 61 73 74 20 70 61 67 65 20 63   The last page c
148b9 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74 74 6c  an have as littl
148ba 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65 20 6f  e.** as 1 byte o
148bb 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 20 20  f data..**.**   
148bc 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50   SIZE    DESCRIP
148bd 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20  TION.**      4  
148be 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f     Page number o
148bf 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20  f next overflow 
148c0 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20  page.**      *  
148c1 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 46 72     Data.**.** Fr
148c2 65 65 6c 69 73 74 20 70 61 67 65 73 20 63 6f 6d  eelist pages com
148c3 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79 70 65  e in two subtype
148c4 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73 20 61  s: trunk pages a
148c5 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20  nd leaf pages.  
148c6 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64  The.** file head
148c7 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  er points to the
148c8 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b   first in a link
148c9 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75 6e 6b  ed list of trunk
148ca 20 70 61 67 65 2e 20 20 45 61 63 68 20 74 72 75   page.  Each tru
148cb 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74  nk.** page point
148cc 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20 6c 65  s to multiple le
148cd 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 20 63  af pages.  The c
148ce 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65 61 66  ontent of a leaf
148cf 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e 73 70   page is.** unsp
148d0 65 63 69 66 69 65 64 2e 20 20 41 20 74 72 75 6e  ecified.  A trun
148d1 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c 69 6b  k page looks lik
148d2 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
148d3 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50   SIZE    DESCRIP
148d4 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20  TION.**      4  
148d5 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f     Page number o
148d6 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70 61 67  f next trunk pag
148d7 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20  e.**      4     
148d8 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 70  Number of leaf p
148d9 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69 73 20  ointers on this 
148da 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20  page.**      *  
148db 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20     zero or more 
148dc 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 6f 66  pages numbers of
148dd 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f 2a 20   leaves.*/.../* 
148de 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
148df 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d  lue is the maxim
148e0 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61 73 73  um cell size ass
148e1 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d 20  uming a maximum 
148e2 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69 76  page.** size giv
148e3 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65 66  e above..*/.#def
148e4 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45  ine MX_CELL_SIZE
148e5 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70 61 67  (pBt)  (pBt->pag
148e6 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68 65  eSize-8)../* The
148e7 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
148e8 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 69  of cells on a si
148e9 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 68 65  ngle page of the
148ea 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73   database.  This
148eb 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 6d 69  .** assumes a mi
148ec 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20  nimum cell size 
148ed 6f 66 20 36 20 62 79 74 65 73 20 20 28 34 20 62  of 6 bytes  (4 b
148ee 79 74 65 73 20 66 6f 72 20 74 68 65 20 63 65 6c  ytes for the cel
148ef 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75 73  l itself.** plus
148f0 20 32 20 62 79 74 65 73 20 66 6f 72 20 74 68 65   2 bytes for the
148f1 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 63 65   index to the ce
148f2 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65 20 68  ll in the page h
148f3 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a 2a 2a  eader).  Such.**
148f4 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69 6c   small cells wil
148f5 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74 20 74  l be rare, but t
148f6 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 6c 65  hey are possible
148f7 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f  ..*/.#define MX_
148f8 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 74 2d  CELL(pBt) ((pBt-
148f9 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36 29 0a  >pageSize-8)/6).
148fa 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  ./* Forward decl
148fb 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 65  arations */.type
148fc 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 50 61  def struct MemPa
148fd 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 65  ge MemPage;.type
148fe 64 65 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63  def struct BtLoc
148ff 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a  k BtLock;../*.**
14900 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 69 63   This is a magic
14901 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 70   string that app
14902 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 69  ears at the begi
14903 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a  nning of every.*
14904 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  * SQLite databas
14905 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 64  e in order to id
14906 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c 65 20  entify the file 
14907 61 73 20 61 20 72 65 61 6c 20 64 61 74 61 62 61  as a real databa
14908 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63 61  se..**.** You ca
14909 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20 76 61  n change this va
1490a 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lue at compile-t
1490b 69 6d 65 20 62 79 20 73 70 65 63 69 66 79 69 6e  ime by specifyin
1490c 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f  g a.** -DSQLITE_
1490d 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e 2e  FILE_HEADER="...
1490e 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  " on the compile
1490f 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e 20  r command-line. 
14910 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72 20 6d   The.** header m
14911 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 31  ust be exactly 1
14912 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64 69 6e  6 bytes includin
14913 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69  g the zero-termi
14914 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65 20  nator so.** the 
14915 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20 73 68  string itself sh
14916 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61 72 61  ould be 15 chara
14917 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66 20  cters long.  If 
14918 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68  you change.** th
14919 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e 20 79  e header, then y
1491a 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61  our custom libra
1491b 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61  ry will not be a
1491c 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a 20  ble to read .** 
1491d 64 61 74 61 62 61 73 65 73 20 67 65 6e 65 72 61  databases genera
1491e 74 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64  ted by the stand
1491f 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74 68  ard tools and th
14920 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73  e standard tools
14921 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  .** will not be 
14922 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64 61 74  able to read dat
14923 61 62 61 73 65 73 20 63 72 65 61 74 65 64 20 62  abases created b
14924 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69  y your custom li
14925 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65  brary..*/.#ifnde
14926 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45  f SQLITE_FILE_HE
14927 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36 37 38  ADER /* 12345678
14928 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20 20 64  9 123456 */.#  d
14929 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 49 4c  efine SQLITE_FIL
1492a 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69 74 65  E_HEADER "SQLite
1492b 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64 69   format 3".#endi
1492c 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74 79  f../*.** Page ty
1492d 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f 52  pe flags.  An OR
1492e 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f  ed combination o
1492f 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 61 70  f these flags ap
14930 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a 20 66  pear as the.** f
14931 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f 6e 2d  irst byte of on-
14932 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 65 76  disk image of ev
14933 65 72 79 20 42 54 72 65 65 20 70 61 67 65 2e 0a  ery BTree page..
14934 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 49  */.#define PTF_I
14935 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a 23 64  NTKEY    0x01.#d
14936 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44 41  efine PTF_ZERODA
14937 54 41 20 20 30 78 30 32 0a 23 64 65 66 69 6e 65  TA  0x02.#define
14938 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 20 30   PTF_LEAFDATA  0
14939 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54 46 5f  x04.#define PTF_
1493a 4c 45 41 46 20 20 20 20 20 20 30 78 30 38 0a 0a  LEAF      0x08..
1493b 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20 70 61  /*.** As each pa
1493c 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ge of the file i
1493d 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65  s loaded into me
1493e 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63  mory, an instanc
1493f 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
14940 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  ng.** structure 
14941 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20  is appended and 
14942 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a  initialized to z
14943 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72 75 63  ero.  This struc
14944 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20 69  ture stores.** i
14945 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
14946 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 69   the page that i
14947 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d 20 74  s decoded from t
14948 68 65 20 72 61 77 20 66 69 6c 65 20 70 61 67 65  he raw file page
14949 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72  ..**.** The pPar
1494a 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73  ent field points
1494b 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 61 72   back to the par
1494c 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69 73 20  ent page.  This 
1494d 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a 20  allows us to.** 
1494e 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54 72 65  walk up the BTre
1494f 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 20  e from any leaf 
14950 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 61  to the root.  Ca
14951 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b 65 6e  re must be taken
14952 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20 74   to.** unref() t
14953 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 70  he parent page p
14954 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68 69 73  ointer when this
14955 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67   page is no long
14956 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a  er referenced..*
14957 2a 20 54 68 65 20 70 61 67 65 44 65 73 74 72 75  * The pageDestru
14958 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 68  ctor() routine h
14959 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68 6f 72  andles that chor
1495a 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20  e..**.** Access 
1495b 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66  to all fields of
1495c 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
1495d 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79  is controlled by
1495e 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73 74   the mutex.** st
1495f 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 65 2e  ored in MemPage.
14960 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a 73  pBt->mutex..*/.s
14961 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 7b 0a  truct MemPage {.
14962 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20 20 20    u8 isInit;    
14963 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
14964 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 69  f previously ini
14965 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20 42  tialized. MUST B
14966 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20 75 38  E FIRST! */.  u8
14967 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20   nOverflow;     
14968 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
14969 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62 6f  overflow cell bo
1496a 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d 20  dies in aCell[] 
1496b 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b 20  */.  u8 intKey; 
1496c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1496d 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67  e if intkey flag
1496e 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20   is set */.  u8 
1496f 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20  leaf;           
14970 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 65 61    /* True if lea
14971 66 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f  f flag is set */
14972 0a 20 20 75 38 20 68 61 73 44 61 74 61 3b 20 20  .  u8 hasData;  
14973 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
14974 69 66 20 74 68 69 73 20 70 61 67 65 20 73 74 6f  if this page sto
14975 72 65 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38  res data */.  u8
14976 20 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20   hdrOffset;     
14977 20 20 20 2f 2a 20 31 30 30 20 66 6f 72 20 70 61     /* 100 for pa
14978 67 65 20 31 2e 20 20 30 20 6f 74 68 65 72 77 69  ge 1.  0 otherwi
14979 73 65 20 2a 2f 0a 20 20 75 38 20 63 68 69 6c 64  se */.  u8 child
1497a 50 74 72 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20  PtrSize;     /* 
1497b 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e 20 20 34  0 if leaf==1.  4
1497c 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 0a 20   if leaf==0 */. 
1497d 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20   u16 maxLocal;  
1497e 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
1497f 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f 63   BtShared.maxLoc
14980 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d  al or BtShared.m
14981 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20  axLeaf */.  u16 
14982 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20  minLocal;       
14983 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68   /* Copy of BtSh
14984 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f 72  ared.minLocal or
14985 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 61   BtShared.minLea
14986 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c 4f  f */.  u16 cellO
14987 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 49  ffset;      /* I
14988 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20 6f 66  ndex in aData of
14989 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e   first cell poin
1498a 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72  ter */.  u16 nFr
1498b 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ee;           /*
1498c 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20   Number of free 
1498d 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67  bytes on the pag
1498e 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c 6c  e */.  u16 nCell
1498f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
14990 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
14991 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c 6f 63  n this page, loc
14992 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a 20  al and ovfl */. 
14993 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b 20 20   u16 maskPage;  
14994 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f        /* Mask fo
14995 72 20 70 61 67 65 20 6f 66 66 73 65 74 20 2a 2f  r page offset */
14996 0a 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43  .  struct _OvflC
14997 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c 73  ell {   /* Cells
14998 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 66   that will not f
14999 69 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a 2f  it on aData[] */
1499a 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20  .    u8 *pCell; 
1499b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
1499c 74 65 72 73 20 74 6f 20 74 68 65 20 62 6f 64 79  ters to the body
1499d 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   of the overflow
1499e 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31 36   cell */.    u16
1499f 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20   idx;           
149a0 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 69 73 20   /* Insert this 
149a1 63 65 6c 6c 20 62 65 66 6f 72 65 20 69 64 78 2d  cell before idx-
149a2 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20  th non-overflow 
149a3 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76 66  cell */.  } aOvf
149a4 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72 65 64  l[5];.  BtShared
149a5 20 2a 70 42 74 3b 20 20 20 20 20 20 20 2f 2a 20   *pBt;       /* 
149a6 50 6f 69 6e 74 65 72 20 74 6f 20 42 74 53 68 61  Pointer to BtSha
149a7 72 65 64 20 74 68 61 74 20 74 68 69 73 20 70 61  red that this pa
149a8 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f  ge is part of */
149a9 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20  .  u8 *aData;   
149aa 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
149ab 65 72 20 74 6f 20 64 69 73 6b 20 69 6d 61 67 65  er to disk image
149ac 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 61 74   of the page dat
149ad 61 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70  a */.  DbPage *p
149ae 44 62 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50  DbPage;     /* P
149af 61 67 65 72 20 70 61 67 65 20 68 61 6e 64 6c 65  ager page handle
149b0 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b   */.  Pgno pgno;
149b1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
149b2 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  ge number for th
149b3 69 73 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f  is page */.};../
149b4 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d 6f  *.** The in-memo
149b5 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20 64 69  ry image of a di
149b6 73 6b 20 70 61 67 65 20 68 61 73 20 74 68 65 20  sk page has the 
149b7 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d  auxiliary inform
149b8 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a 2a  ation appended.*
149b9 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20 20 45  * to the end.  E
149ba 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74 68 65  XTRA_SIZE is the
149bb 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
149bc 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65 64   of space needed
149bd 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61 74   to hold.** that
149be 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69   extra informati
149bf 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45  on..*/.#define E
149c0 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f 66  XTRA_SIZE sizeof
149c1 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 0a 2a 2a  (MemPage)../*.**
149c2 20 41 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   A linked list o
149c3 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
149c4 73 74 72 75 63 74 75 72 65 73 20 69 73 20 73 74  structures is st
149c5 6f 72 65 64 20 61 74 20 42 74 53 68 61 72 65 64  ored at BtShared
149c6 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73  .pLock..** Locks
149c7 20 61 72 65 20 61 64 64 65 64 20 28 6f 72 20 75   are added (or u
149c8 70 67 72 61 64 65 64 20 66 72 6f 6d 20 52 45 41  pgraded from REA
149c9 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 49 54 45 5f  D_LOCK to WRITE_
149ca 4c 4f 43 4b 29 20 77 68 65 6e 20 61 20 63 75 72  LOCK) when a cur
149cb 73 6f 72 20 0a 2a 2a 20 69 73 20 6f 70 65 6e 65  sor .** is opene
149cc 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77  d on the table w
149cd 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 42 74  ith root page Bt
149ce 53 68 61 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c  Shared.iTable. L
149cf 6f 63 6b 73 20 61 72 65 20 72 65 6d 6f 76 65 64  ocks are removed
149d0 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69  .** from this li
149d1 73 74 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61  st when a transa
149d2 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74  ction is committ
149d3 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63  ed or rolled bac
149d4 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 61 20  k, or when.** a 
149d5 62 74 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20  btree handle is 
149d6 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63  closed..*/.struc
149d7 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 74 72  t BtLock {.  Btr
149d8 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20  ee *pBtree;     
149d9 20 20 20 2f 2a 20 42 74 72 65 65 20 68 61 6e 64     /* Btree hand
149da 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20  le holding this 
149db 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  lock */.  Pgno i
149dc 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
149dd 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20  /* Root page of 
149de 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 4c  table */.  u8 eL
149df 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ock;            
149e0 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72   /* READ_LOCK or
149e1 20 57 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f 0a 20   WRITE_LOCK */. 
149e2 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 3b 20   BtLock *pNext; 
149e3 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69         /* Next i
149e4 6e 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b  n BtShared.pLock
149e5 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20   list */.};../* 
149e6 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73  Candidate values
149e7 20 66 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63   for BtLock.eLoc
149e8 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41  k */.#define REA
149e9 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65  D_LOCK     1.#de
149ea 66 69 6e 65 20 57 52 49 54 45 5f 4c 4f 43 4b 20  fine WRITE_LOCK 
149eb 20 20 20 32 0a 0a 2f 2a 20 41 20 42 74 72 65 65     2../* A Btree
149ec 20 68 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 20   handle.**.** A 
149ed 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
149ee 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70  ion contains a p
149ef 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
149f0 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73  tance of.** this
149f1 20 6f 62 6a 65 63 74 20 66 6f 72 20 65 76 65 72   object for ever
149f2 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  y database file 
149f3 74 68 61 74 20 69 74 20 68 61 73 20 6f 70 65 6e  that it has open
149f4 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72  .  This structur
149f5 65 0a 2a 2a 20 69 73 20 6f 70 61 71 75 65 20 74  e.** is opaque t
149f6 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  o the database c
149f7 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20  onnection.  The 
149f8 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
149f9 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 73 65  ion cannot.** se
149fa 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 73 20  e the internals 
149fb 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
149fc 65 20 61 6e 64 20 6f 6e 6c 79 20 64 65 61 6c 73  e and only deals
149fd 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
149fe 6f 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74  o.** this struct
149ff 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73  ure..**.** For s
14a00 6f 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ome database fil
14a01 65 73 2c 20 74 68 65 20 73 61 6d 65 20 75 6e 64  es, the same und
14a02 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65  erlying database
14a03 20 63 61 63 68 65 20 6d 69 67 68 74 20 62 65 20   cache might be 
14a04 0a 2a 2a 20 73 68 61 72 65 64 20 62 65 74 77 65  .** shared betwe
14a05 65 6e 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 6e  en multiple conn
14a06 65 63 74 69 6f 6e 73 2e 20 20 49 6e 20 74 68 61  ections.  In tha
14a07 74 20 63 61 73 65 2c 20 65 61 63 68 20 63 6f 6e  t case, each con
14a08 6e 65 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 69  nection.** has i
14a09 74 20 6f 77 6e 20 69 6e 73 74 61 6e 63 65 20 6f  t own instance o
14a0a 66 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 20 20  f this object.  
14a0b 42 75 74 20 65 61 63 68 20 69 6e 73 74 61 6e 63  But each instanc
14a0c 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
14a0d 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  .** points to th
14a0e 65 20 73 61 6d 65 20 42 74 53 68 61 72 65 64 20  e same BtShared 
14a0f 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 64 61 74  object.  The dat
14a10 61 62 61 73 65 20 63 61 63 68 65 20 61 6e 64 20  abase cache and 
14a11 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 61 73  the.** schema as
14a12 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
14a13 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
14a14 61 72 65 20 61 6c 6c 20 63 6f 6e 74 61 69 6e 65  are all containe
14a15 64 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20  d within.** the 
14a16 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e  BtShared object.
14a17 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66 69 65 6c 64  .**.** All field
14a18 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74  s in this struct
14a19 75 72 65 20 61 72 65 20 61 63 63 65 73 73 65 64  ure are accessed
14a1a 20 75 6e 64 65 72 20 73 71 6c 69 74 65 33 2e 6d   under sqlite3.m
14a1b 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 70 42 74  utex..** The pBt
14a1c 20 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 20   pointer itself 
14a1d 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67  may not be chang
14a1e 65 64 20 77 68 69 6c 65 20 74 68 65 72 65 20 65  ed while there e
14a1f 78 69 73 74 73 20 63 75 72 73 6f 72 73 20 0a 2a  xists cursors .*
14a20 2a 20 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e  * in the referen
14a21 63 65 64 20 42 74 53 68 61 72 65 64 20 74 68 61  ced BtShared tha
14a22 74 20 70 6f 69 6e 74 20 62 61 63 6b 20 74 6f 20  t point back to 
14a23 74 68 69 73 20 42 74 72 65 65 20 73 69 6e 63 65  this Btree since
14a24 20 74 68 6f 73 65 0a 2a 2a 20 63 75 72 73 6f 72   those.** cursor
14a25 73 20 68 61 76 65 20 74 6f 20 64 6f 20 67 6f 20  s have to do go 
14a26 74 68 72 6f 75 67 68 20 74 68 69 73 20 42 74 72  through this Btr
14a27 65 65 20 74 6f 20 66 69 6e 64 20 74 68 65 69 72  ee to find their
14a28 20 42 74 53 68 61 72 65 64 20 61 6e 64 0a 2a 2a   BtShared and.**
14a29 20 74 68 65 79 20 6f 66 74 65 6e 20 64 6f 20 73   they often do s
14a2a 6f 20 77 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e  o without holdin
14a2b 67 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e  g sqlite3.mutex.
14a2c 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 72 65 65  .*/.struct Btree
14a2d 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
14a2e 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64  ;       /* The d
14a2f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14a30 6f 6e 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20  on holding this 
14a31 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61  btree */.  BtSha
14a32 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a  red *pBt;     /*
14a33 20 53 68 61 72 61 62 6c 65 20 63 6f 6e 74 65 6e   Sharable conten
14a34 74 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20  t of this btree 
14a35 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 3b  */.  u8 inTrans;
14a36 20 20 20 20 20 20 20 20 2f 2a 20 54 52 41 4e 53          /* TRANS
14a37 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53 5f 52 45 41  _NONE, TRANS_REA
14a38 44 20 6f 72 20 54 52 41 4e 53 5f 57 52 49 54 45  D or TRANS_WRITE
14a39 20 2a 2f 0a 20 20 75 38 20 73 68 61 72 61 62 6c   */.  u8 sharabl
14a3a 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  e;       /* True
14a3b 20 69 66 20 77 65 20 63 61 6e 20 73 68 61 72 65   if we can share
14a3c 20 70 42 74 20 77 69 74 68 20 61 6e 6f 74 68 65   pBt with anothe
14a3d 72 20 64 62 20 2a 2f 0a 20 20 75 38 20 6c 6f 63  r db */.  u8 loc
14a3e 6b 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ked;         /* 
14a3f 54 72 75 65 20 69 66 20 64 62 20 63 75 72 72 65  True if db curre
14a40 6e 74 6c 79 20 68 61 73 20 70 42 74 20 6c 6f 63  ntly has pBt loc
14a41 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 77 61 6e  ked */.  int wan
14a42 74 54 6f 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e  tToLock;    /* N
14a43 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20  umber of nested 
14a44 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
14a45 42 74 72 65 65 45 6e 74 65 72 28 29 20 2a 2f 0a  BtreeEnter() */.
14a46 20 20 69 6e 74 20 6e 42 61 63 6b 75 70 3b 20 20    int nBackup;  
14a47 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14a48 66 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  f backup operati
14a49 6f 6e 73 20 72 65 61 64 69 6e 67 20 74 68 69 73  ons reading this
14a4a 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 72 65   btree */.  Btre
14a4b 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f  e *pNext;      /
14a4c 2a 20 4c 69 73 74 20 6f 66 20 6f 74 68 65 72 20  * List of other 
14a4d 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20  sharable Btrees 
14a4e 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 64 62  from the same db
14a4f 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 50 72   */.  Btree *pPr
14a50 65 76 3b 20 20 20 20 20 20 2f 2a 20 42 61 63 6b  ev;      /* Back
14a51 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20   pointer of the 
14a52 73 61 6d 65 20 6c 69 73 74 20 2a 2f 0a 23 69 66  same list */.#if
14a53 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
14a54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
14a55 42 74 4c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20  BtLock lock;    
14a56 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20 75 73 65     /* Object use
14a57 64 20 74 6f 20 6c 6f 63 6b 20 70 61 67 65 20 31  d to lock page 1
14a58 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
14a59 2a 0a 2a 2a 20 42 74 72 65 65 2e 69 6e 54 72 61  *.** Btree.inTra
14a5a 6e 73 20 6d 61 79 20 74 61 6b 65 20 6f 6e 65 20  ns may take one 
14a5b 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
14a5c 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49   values..**.** I
14a5d 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74  f the shared-dat
14a5e 61 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 65  a extension is e
14a5f 6e 61 62 6c 65 64 2c 20 74 68 65 72 65 20 6d 61  nabled, there ma
14a60 79 20 62 65 20 6d 75 6c 74 69 70 6c 65 20 75 73  y be multiple us
14a61 65 72 73 0a 2a 2a 20 6f 66 20 74 68 65 20 42 74  ers.** of the Bt
14a62 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 20 41  ree structure. A
14a63 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 74 68  t most one of th
14a64 65 73 65 20 6d 61 79 20 6f 70 65 6e 20 61 20 77  ese may open a w
14a65 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
14a66 2c 0a 2a 2a 20 62 75 74 20 61 6e 79 20 6e 75 6d  ,.** but any num
14a67 62 65 72 20 6d 61 79 20 68 61 76 65 20 61 63 74  ber may have act
14a68 69 76 65 20 72 65 61 64 20 74 72 61 6e 73 61 63  ive read transac
14a69 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  tions..*/.#defin
14a6a 65 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a  e TRANS_NONE  0.
14a6b 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 52 45  #define TRANS_RE
14a6c 41 44 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52  AD  1.#define TR
14a6d 41 4e 53 5f 57 52 49 54 45 20 32 0a 0a 2f 2a 0a  ANS_WRITE 2../*.
14a6e 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
14a6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65  f this object re
14a70 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c  presents a singl
14a71 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
14a72 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69 6e 67 6c 65  .** .** A single
14a73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63   database file c
14a74 61 6e 20 62 65 20 69 6e 20 75 73 65 20 61 73 20  an be in use as 
14a75 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 62 79  the same time by
14a76 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20   two.** or more 
14a77 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
14a78 69 6f 6e 73 2e 20 20 57 68 65 6e 20 74 77 6f 20  ions.  When two 
14a79 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e 65 63 74 69  or more connecti
14a7a 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 68 61 72 69  ons are.** shari
14a7b 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  ng the same data
14a7c 62 61 73 65 20 66 69 6c 65 2c 20 65 61 63 68 20  base file, each 
14a7d 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 69  connection has i
14a7e 74 20 6f 77 6e 0a 2a 2a 20 70 72 69 76 61 74 65  t own.** private
14a7f 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 66 6f   Btree object fo
14a80 72 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 65  r the file and e
14a81 61 63 68 20 6f 66 20 74 68 6f 73 65 20 42 74 72  ach of those Btr
14a82 65 65 73 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f  ees points.** to
14a83 20 74 68 69 73 20 6f 6e 65 20 42 74 53 68 61 72   this one BtShar
14a84 65 64 20 6f 62 6a 65 63 74 2e 20 20 42 74 53 68  ed object.  BtSh
14a85 61 72 65 64 2e 6e 52 65 66 20 69 73 20 74 68 65  ared.nRef is the
14a86 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 6f   number of.** co
14a87 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 65 6e  nnections curren
14a88 74 6c 79 20 73 68 61 72 69 6e 67 20 74 68 69 73  tly sharing this
14a89 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
14a8a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20  **.** Fields in 
14a8b 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61  this structure a
14a8c 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65  re accessed unde
14a8d 72 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6d  r the BtShared.m
14a8e 75 74 65 78 0a 2a 2a 20 6d 75 74 65 78 2c 20 65  utex.** mutex, e
14a8f 78 63 65 70 74 20 66 6f 72 20 6e 52 65 66 20 61  xcept for nRef a
14a90 6e 64 20 70 4e 65 78 74 20 77 68 69 63 68 20 61  nd pNext which a
14a91 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65  re accessed unde
14a92 72 20 74 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20  r the.** global 
14a93 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
14a94 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
14a95 2e 20 20 54 68 65 20 70 50 61 67 65 72 20 66 69  .  The pPager fi
14a96 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 62  eld.** may not b
14a97 65 20 6d 6f 64 69 66 69 65 64 20 6f 6e 63 65 20  e modified once 
14a98 69 74 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20  it is initially 
14a99 73 65 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e  set as long as n
14a9a 52 65 66 3e 30 2e 0a 2a 2a 20 54 68 65 20 70 53  Ref>0..** The pS
14a9b 63 68 65 6d 61 20 66 69 65 6c 64 20 6d 61 79 20  chema field may 
14a9c 62 65 20 73 65 74 20 6f 6e 63 65 20 75 6e 64 65  be set once unde
14a9d 72 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78  r BtShared.mutex
14a9e 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 61 66 74   and.** thereaft
14a9f 65 72 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20  er is unchanged 
14aa0 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e  as long as nRef>
14aa1 30 2e 0a 2a 2a 0a 2a 2a 20 69 73 50 65 6e 64 69  0..**.** isPendi
14aa2 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 61  ng:.**.**   If a
14aa3 20 42 74 53 68 61 72 65 64 20 63 6c 69 65 6e 74   BtShared client
14aa4 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e   fails to obtain
14aa5 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e   a write-lock on
14aa6 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20   a database.**  
14aa7 20 74 61 62 6c 65 20 28 62 65 63 61 75 73 65 20   table (because 
14aa8 74 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65  there exists one
14aa9 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6c 6f   or more read-lo
14aaa 63 6b 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  cks on the table
14aab 29 2c 0a 2a 2a 20 20 20 74 68 65 20 73 68 61 72  ),.**   the shar
14aac 65 64 2d 63 61 63 68 65 20 65 6e 74 65 72 73 20  ed-cache enters 
14aad 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73  'pending-lock' s
14aae 74 61 74 65 20 61 6e 64 20 69 73 50 65 6e 64 69  tate and isPendi
14aaf 6e 67 20 69 73 0a 2a 2a 20 20 20 73 65 74 20 74  ng is.**   set t
14ab0 6f 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  o true..**.**   
14ab1 54 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65  The shared-cache
14ab2 20 6c 65 61 76 65 73 20 74 68 65 20 27 70 65 6e   leaves the 'pen
14ab3 64 69 6e 67 20 6c 6f 63 6b 27 20 73 74 61 74 65  ding lock' state
14ab4 20 77 68 65 6e 20 65 69 74 68 65 72 20 6f 66 0a   when either of.
14ab5 2a 2a 20 20 20 74 68 65 20 66 6f 6c 6c 6f 77 69  **   the followi
14ab6 6e 67 20 6f 63 63 75 72 3a 0a 2a 2a 0a 2a 2a 20  ng occur:.**.** 
14ab7 20 20 20 20 31 29 20 54 68 65 20 63 75 72 72 65      1) The curre
14ab8 6e 74 20 77 72 69 74 65 72 20 28 42 74 53 68 61  nt writer (BtSha
14ab9 72 65 64 2e 70 57 72 69 74 65 72 29 20 63 6f 6e  red.pWriter) con
14aba 63 6c 75 64 65 73 20 69 74 73 20 74 72 61 6e 73  cludes its trans
14abb 61 63 74 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 20  action, OR.**   
14abc 20 20 32 29 20 54 68 65 20 6e 75 6d 62 65 72 20    2) The number 
14abd 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79  of locks held by
14abe 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
14abf 6e 73 20 64 72 6f 70 73 20 74 6f 20 7a 65 72 6f  ns drops to zero
14ac0 2e 0a 2a 2a 0a 2a 2a 20 20 20 77 68 69 6c 65 20  ..**.**   while 
14ac1 69 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 2d  in the 'pending-
14ac2 6c 6f 63 6b 27 20 73 74 61 74 65 2c 20 6e 6f 20  lock' state, no 
14ac3 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 73  connection may s
14ac4 74 61 72 74 20 61 20 6e 65 77 0a 2a 2a 20 20 20  tart a new.**   
14ac5 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
14ac6 2a 2a 20 20 20 54 68 69 73 20 66 65 61 74 75 72  **   This featur
14ac7 65 20 69 73 20 69 6e 63 6c 75 64 65 64 20 74 6f  e is included to
14ac8 20 68 65 6c 70 20 70 72 65 76 65 6e 74 20 77 72   help prevent wr
14ac9 69 74 65 72 2d 73 74 61 72 76 61 74 69 6f 6e 2e  iter-starvation.
14aca 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 53 68 61  .*/.struct BtSha
14acb 72 65 64 20 7b 0a 20 20 50 61 67 65 72 20 2a 70  red {.  Pager *p
14acc 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 2f 2a  Pager;        /*
14acd 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
14ace 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
14acf 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
14ad0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14ad1 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73 69 6e  n currently usin
14ad2 67 20 74 68 69 73 20 42 74 72 65 65 20 2a 2f 0a  g this Btree */.
14ad3 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
14ad4 73 6f 72 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73  sor;    /* A lis
14ad5 74 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75  t of all open cu
14ad6 72 73 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61  rsors */.  MemPa
14ad7 67 65 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20  ge *pPage1;     
14ad8 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f   /* First page o
14ad9 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  f the database *
14ada 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b  /.  u8 readOnly;
14adb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
14adc 65 20 69 66 20 74 68 65 20 75 6e 64 65 72 6c 79  e if the underly
14add 69 6e 67 20 66 69 6c 65 20 69 73 20 72 65 61 64  ing file is read
14ade 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 70 61 67  only */.  u8 pag
14adf 65 53 69 7a 65 46 69 78 65 64 3b 20 20 20 20 20  eSizeFixed;     
14ae0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 70  /* True if the p
14ae1 61 67 65 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20  age size can no 
14ae2 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65  longer be change
14ae3 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  d */.#ifndef SQL
14ae4 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
14ae5 55 55 4d 0a 20 20 75 38 20 61 75 74 6f 56 61 63  UUM.  u8 autoVac
14ae6 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  uum;        /* T
14ae7 72 75 65 20 69 66 20 61 75 74 6f 2d 76 61 63 75  rue if auto-vacu
14ae8 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f  um is enabled */
14ae9 0a 20 20 75 38 20 69 6e 63 72 56 61 63 75 75 6d  .  u8 incrVacuum
14aea 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
14aeb 20 69 66 20 69 6e 63 72 2d 76 61 63 75 75 6d 20   if incr-vacuum 
14aec 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 65  is enabled */.#e
14aed 6e 64 69 66 0a 20 20 75 31 36 20 70 61 67 65 53  ndif.  u16 pageS
14aee 69 7a 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ize;         /* 
14aef 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
14af0 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20  bytes on a page 
14af1 2a 2f 0a 20 20 75 31 36 20 75 73 61 62 6c 65 53  */.  u16 usableS
14af2 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  ize;       /* Nu
14af3 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20 62  mber of usable b
14af4 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67  ytes on each pag
14af5 65 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f  e */.  u16 maxLo
14af6 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  cal;         /* 
14af7 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61  Maximum local pa
14af8 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41  yload in non-LEA
14af9 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a  FDATA tables */.
14afa 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20    u16 minLocal; 
14afb 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d          /* Minim
14afc 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  um local payload
14afd 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41   in non-LEAFDATA
14afe 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36   tables */.  u16
14aff 20 6d 61 78 4c 65 61 66 3b 20 20 20 20 20 20 20   maxLeaf;       
14b00 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f     /* Maximum lo
14b01 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61  cal payload in a
14b02 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 20   LEAFDATA table 
14b03 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 65 61 66  */.  u16 minLeaf
14b04 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69  ;          /* Mi
14b05 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c  nimum local payl
14b06 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54  oad in a LEAFDAT
14b07 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20  A table */.  u8 
14b08 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20  inTransaction;  
14b09 20 20 20 2f 2a 20 54 72 61 6e 73 61 63 74 69 6f     /* Transactio
14b0a 6e 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74  n state */.  int
14b0b 20 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20   nTransaction;  
14b0c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
14b0d 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e  open transaction
14b0e 73 20 28 72 65 61 64 20 2b 20 77 72 69 74 65 29  s (read + write)
14b0f 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 68   */.  void *pSch
14b10 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  ema;        /* P
14b11 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63 65 20  ointer to space 
14b12 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c  allocated by sql
14b13 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28  ite3BtreeSchema(
14b14 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46  ) */.  void (*xF
14b15 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69 64 2a  reeSchema)(void*
14b16 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f  );  /* Destructo
14b17 72 20 66 6f 72 20 42 74 53 68 61 72 65 64 2e 70  r for BtShared.p
14b18 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69  Schema */.  sqli
14b19 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
14b1a 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69  ; /* Non-recursi
14b1b 76 65 20 6d 75 74 65 78 20 72 65 71 75 69 72 65  ve mutex require
14b1c 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73  d to access this
14b1d 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 69 74   struct */.  Bit
14b1e 76 65 63 20 2a 70 48 61 73 43 6f 6e 74 65 6e 74  vec *pHasContent
14b1f 3b 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67  ;  /* Set of pag
14b20 65 73 20 6d 6f 76 65 64 20 74 6f 20 66 72 65 65  es moved to free
14b21 2d 6c 69 73 74 20 74 68 69 73 20 74 72 61 6e 73  -list this trans
14b22 61 63 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65  action */.#ifnde
14b23 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
14b24 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74  ARED_CACHE.  int
14b25 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20   nRef;          
14b26 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
14b27 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
14b28 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  is structure */.
14b29 20 20 42 74 53 68 61 72 65 64 20 2a 70 4e 65 78    BtShared *pNex
14b2a 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20  t;      /* Next 
14b2b 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 73 68 61  on a list of sha
14b2c 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 20 73  rable BtShared s
14b2d 74 72 75 63 74 73 20 2a 2f 0a 20 20 42 74 4c 6f  tructs */.  BtLo
14b2e 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20  ck *pLock;      
14b2f 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c 6f 63    /* List of loc
14b30 6b 73 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20  ks held on this 
14b31 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 74 72  shared-btree str
14b32 75 63 74 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  uct */.  Btree *
14b33 70 57 72 69 74 65 72 3b 20 20 20 20 20 20 20 2f  pWriter;       /
14b34 2a 20 42 74 72 65 65 20 77 69 74 68 20 63 75 72  * Btree with cur
14b35 72 65 6e 74 6c 79 20 6f 70 65 6e 20 77 72 69 74  rently open writ
14b36 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  e transaction */
14b37 0a 20 20 75 38 20 69 73 45 78 63 6c 75 73 69 76  .  u8 isExclusiv
14b38 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  e;       /* True
14b39 20 69 66 20 70 57 72 69 74 65 72 20 68 61 73 20   if pWriter has 
14b3a 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
14b3b 6b 20 6f 6e 20 74 68 65 20 64 62 20 2a 2f 0a 20  k on the db */. 
14b3c 20 75 38 20 69 73 50 65 6e 64 69 6e 67 3b 20 20   u8 isPending;  
14b3d 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 61 69         /* If wai
14b3e 74 69 6e 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f  ting for read-lo
14b3f 63 6b 73 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a  cks to clear */.
14b40 23 65 6e 64 69 66 0a 20 20 75 38 20 2a 70 54 6d  #endif.  u8 *pTm
14b41 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 2f  pSpace;        /
14b42 2a 20 42 74 53 68 61 72 65 64 2e 70 61 67 65 53  * BtShared.pageS
14b43 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 61  ize bytes of spa
14b44 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a  ce for tmp use *
14b45 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
14b46 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
14b47 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
14b48 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 6f  re is used to ho
14b49 6c 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  ld information.*
14b4a 2a 20 61 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20  * about a cell. 
14b4b 20 54 68 65 20 70 61 72 73 65 43 65 6c 6c 50 74   The parseCellPt
14b4c 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c  r() function fil
14b4d 6c 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63  ls in this struc
14b4e 74 75 72 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e  ture.** based on
14b4f 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74   information ext
14b50 72 61 63 74 20 66 72 6f 6d 20 74 68 65 20 72 61  ract from the ra
14b51 77 20 64 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a  w disk page..*/.
14b52 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 43  typedef struct C
14b53 65 6c 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f  ellInfo CellInfo
14b54 3b 0a 73 74 72 75 63 74 20 43 65 6c 6c 49 6e 66  ;.struct CellInf
14b55 6f 20 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b  o {.  u8 *pCell;
14b56 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
14b57 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
14b58 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a  cell content */.
14b59 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20    i64 nKey;     
14b5a 20 2f 2a 20 54 68 65 20 6b 65 79 20 66 6f 72 20   /* The key for 
14b5b 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f  INTKEY tables, o
14b5c 72 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  r number of byte
14b5d 73 20 69 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33  s in key */.  u3
14b5e 32 20 6e 44 61 74 61 3b 20 20 20 20 20 2f 2a 20  2 nData;     /* 
14b5f 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
14b60 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20 75 33 32  of data */.  u32
14b61 20 6e 50 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54   nPayload;  /* T
14b62 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70  otal amount of p
14b63 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20  ayload */.  u16 
14b64 6e 48 65 61 64 65 72 3b 20 20 20 2f 2a 20 53 69  nHeader;   /* Si
14b65 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  ze of the cell c
14b66 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 20 69 6e  ontent header in
14b67 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 31 36 20   bytes */.  u16 
14b68 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d  nLocal;    /* Am
14b69 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
14b6a 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a  held locally */.
14b6b 20 20 75 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b    u16 iOverflow;
14b6c 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 6f 76   /* Offset to ov
14b6d 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62  erflow page numb
14b6e 65 72 2e 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20  er.  Zero if no 
14b6f 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31  overflow */.  u1
14b70 36 20 6e 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20  6 nSize;     /* 
14b71 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  Size of the cell
14b72 20 63 6f 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20   content on the 
14b73 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67 65  main b-tree page
14b74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61   */.};../*.** Ma
14b75 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 61  ximum depth of a
14b76 6e 20 53 51 4c 69 74 65 20 42 2d 54 72 65 65 20  n SQLite B-Tree 
14b77 73 74 72 75 63 74 75 72 65 2e 20 41 6e 79 20 42  structure. Any B
14b78 2d 54 72 65 65 20 64 65 65 70 65 72 20 74 68 61  -Tree deeper tha
14b79 6e 0a 2a 2a 20 74 68 69 73 20 77 69 6c 6c 20 62  n.** this will b
14b7a 65 20 64 65 63 6c 61 72 65 64 20 63 6f 72 72 75  e declared corru
14b7b 70 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 69  pt. This value i
14b7c 73 20 63 61 6c 63 75 6c 61 74 65 64 20 62 61 73  s calculated bas
14b7d 65 64 20 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d  ed on a.** maxim
14b7e 75 6d 20 64 61 74 61 62 61 73 65 20 73 69 7a 65  um database size
14b7f 20 6f 66 20 32 5e 33 31 20 70 61 67 65 73 20 61   of 2^31 pages a
14b80 20 6d 69 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20   minimum fanout 
14b81 6f 66 20 32 20 66 6f 72 20 61 0a 2a 2a 20 72 6f  of 2 for a.** ro
14b82 6f 74 2d 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f  ot-node and 3 fo
14b83 72 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65  r all other inte
14b84 72 6e 61 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a  rnal nodes..**.*
14b85 2a 20 49 66 20 61 20 74 72 65 65 20 74 68 61 74  * If a tree that
14b86 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 74   appears to be t
14b87 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 73 20  aller than this 
14b88 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20  is encountered, 
14b89 69 74 20 69 73 0a 2a 2a 20 61 73 73 75 6d 65 64  it is.** assumed
14b8a 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61   that the databa
14b8b 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a  se is corrupt..*
14b8c 2f 0a 23 64 65 66 69 6e 65 20 42 54 43 55 52 53  /.#define BTCURS
14b8d 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 32 30 0a  OR_MAX_DEPTH 20.
14b8e 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20  ./*.** A cursor 
14b8f 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
14b90 61 20 70 61 72 74 69 63 75 6c 61 72 20 65 6e 74  a particular ent
14b91 72 79 20 77 69 74 68 69 6e 20 61 20 70 61 72 74  ry within a part
14b92 69 63 75 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65  icular.** b-tree
14b93 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 62 61   within a databa
14b94 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  se file..**.** T
14b95 68 65 20 65 6e 74 72 79 20 69 73 20 69 64 65 6e  he entry is iden
14b96 74 69 66 69 65 64 20 62 79 20 69 74 73 20 4d 65  tified by its Me
14b97 6d 50 61 67 65 20 61 6e 64 20 74 68 65 20 69 6e  mPage and the in
14b98 64 65 78 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67  dex in.** MemPag
14b99 65 2e 61 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65  e.aCell[] of the
14b9a 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 41 20   entry..**.** A 
14b9b 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20  single database 
14b9c 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65 64 20  file can shared 
14b9d 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61 74 61  by two more data
14b9e 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
14b9f 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f 72 73  ,.** but cursors
14ba0 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61 72 65   cannot be share
14ba1 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f 72 20  d.  Each cursor 
14ba2 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  is associated wi
14ba3 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63 75 6c  th a.** particul
14ba4 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ar database conn
14ba5 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66 69 65  ection identifie
14ba6 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74 72 65  d BtCursor.pBtre
14ba7 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c  e.db..**.** Fiel
14ba8 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63  ds in this struc
14ba9 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73 65  ture are accesse
14baa 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 53 68  d under the BtSh
14bab 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 66 6f  ared.mutex.** fo
14bac 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70 42 74  und at self->pBt
14bad 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73 74 72  ->mutex. .*/.str
14bae 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b 0a 20  uct BtCursor {. 
14baf 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20   Btree *pBtree; 
14bb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
14bb1 65 20 42 74 72 65 65 20 74 6f 20 77 68 69 63 68  e Btree to which
14bb2 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 6c   this cursor bel
14bb3 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68 61 72  ongs */.  BtShar
14bb4 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20  ed *pBt;        
14bb5 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53 68 61      /* The BtSha
14bb6 72 65 64 20 74 68 69 73 20 63 75 72 73 6f 72 20  red this cursor 
14bb7 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20 20 42  points to */.  B
14bb8 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74 2c 20  tCursor *pNext, 
14bb9 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f 72 6d  *pPrev;  /* Form
14bba 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  s a linked list 
14bbb 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 2a  of all cursors *
14bbc 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e  /.  struct KeyIn
14bbd 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 2f 2a  fo *pKeyInfo; /*
14bbe 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   Argument passed
14bbf 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66   to comparison f
14bc0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e  unction */.  Pgn
14bc1 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 20 20  o pgnoRoot;     
14bc2 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f         /* The ro
14bc3 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 73 20  ot page of this 
14bc4 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  tree */.  sqlite
14bc5 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64 52 6f  3_int64 cachedRo
14bc6 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72 6f 77  wid; /* Next row
14bc7 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d 65 61  id cache.  0 mea
14bc8 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a 2f 0a  ns not valid */.
14bc9 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b    CellInfo info;
14bca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
14bcb 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 63 65   parse of the ce
14bcc 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e 74 69  ll we are pointi
14bcd 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20 77 72  ng at */.  u8 wr
14bce 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 20 20  Flag;           
14bcf 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
14bd0 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38  writable */.  u8
14bd1 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20 20 20   atLast;        
14bd2 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
14bd3 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  r pointing to th
14bd4 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a 2f 0a  e last entry */.
14bd5 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79 3b 20    u8 validNKey; 
14bd6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
14bd7 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b 65 79  rue if info.nKey
14bd8 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 75   is valid */.  u
14bd9 38 20 65 53 74 61 74 65 3b 20 20 20 20 20 20 20  8 eState;       
14bda 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
14bdb 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f 58 58  of the CURSOR_XX
14bdc 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73 65 65  X constants (see
14bdd 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76 6f 69   below) */.  voi
14bde 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 2f 2a  d *pKey;      /*
14bdf 20 53 61 76 65 64 20 6b 65 79 20 74 68 61 74 20   Saved key that 
14be0 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c 61 73  was cursor's las
14be1 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69 6f 6e  t known position
14be2 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20   */.  i64 nKey; 
14be3 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
14be4 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73 74 20  f pKey, or last 
14be5 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f 0a 20  integer key */. 
14be6 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b 20 20   int skipNext;  
14be7 20 20 2f 2a 20 50 72 65 76 28 29 20 69 73 20 6e    /* Prev() is n
14be8 6f 6f 70 20 69 66 20 6e 65 67 61 74 69 76 65 2e  oop if negative.
14be9 20 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f 70 20   Next() is noop 
14bea 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 23  if positive */.#
14beb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14bec 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 75 38  IT_INCRBLOB.  u8
14bed 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c   isIncrblobHandl
14bee 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  e;      /* True 
14bef 69 66 20 74 68 69 73 20 63 75 72 73 6f 72 20 69  if this cursor i
14bf0 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 68 61  s an incr. io ha
14bf1 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a  ndle */.  Pgno *
14bf2 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20  aOverflow;      
14bf3 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f 66 20      /* Cache of 
14bf4 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 6f  overflow page lo
14bf5 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69  cations */.#endi
14bf6 66 0a 20 20 69 31 36 20 69 50 61 67 65 3b 20 20  f.  i16 iPage;  
14bf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14bf8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
14bf9 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 70 61  ex of current pa
14bfa 67 65 20 69 6e 20 61 70 50 61 67 65 20 2a 2f 0a  ge in apPage */.
14bfb 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 50 61 67    MemPage *apPag
14bfc 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44  e[BTCURSOR_MAX_D
14bfd 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67 65 73  EPTH];  /* Pages
14bfe 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20 63 75   from root to cu
14bff 72 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20  rrent page */.  
14c00 75 31 36 20 61 69 49 64 78 5b 42 54 43 55 52 53  u16 aiIdx[BTCURS
14c01 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20  OR_MAX_DEPTH];  
14c02 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
14c03 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61 67 65   index in apPage
14c04 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  [i] */.};../*.**
14c05 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c 75 65   Potential value
14c06 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72 2e 65  s for BtCursor.e
14c07 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 55 52  State..**.** CUR
14c08 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 20 20  SOR_VALID:.**   
14c09 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f  Cursor points to
14c0a 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20   a valid entry. 
14c0b 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65 74 63  getPayload() etc
14c0c 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 2e  . may be called.
14c0d 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e  .**.** CURSOR_IN
14c0e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73  VALID:.**   Curs
14c0f 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f 69 6e  or does not poin
14c10 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74  t to a valid ent
14c11 72 79 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70  ry. This can hap
14c12 70 65 6e 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  pen (for example
14c13 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 73 65 20  ) .**   because 
14c14 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
14c15 74 79 20 6f 72 20 62 65 63 61 75 73 65 20 42 74  ty or because Bt
14c16 72 65 65 43 75 72 73 6f 72 46 69 72 73 74 28 29  reeCursorFirst()
14c17 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a   has not been.**
14c18 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a     called..**.**
14c19 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
14c1a 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 74 61  EEK:.**   The ta
14c1b 62 6c 65 20 74 68 61 74 20 74 68 69 73 20 63 75  ble that this cu
14c1c 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20  rsor was opened 
14c1d 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74 73 2c  on still exists,
14c1e 20 62 75 74 20 68 61 73 20 62 65 65 6e 20 0a 2a   but has been .*
14c1f 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73 69 6e  *   modified sin
14c20 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 77 61  ce the cursor wa
14c21 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54 68 65  s last used. The
14c22 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e   cursor position
14c23 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20 20 69   is saved.**   i
14c24 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75  n variables BtCu
14c25 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20 42 74  rsor.pKey and Bt
14c26 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 68 65  Cursor.nKey. Whe
14c27 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20 69 6e  n a cursor is in
14c28 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74 61 74   .**   this stat
14c29 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72  e, restoreCursor
14c2a 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e 20 62  Position() can b
14c2b 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 74 65  e called to atte
14c2c 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 65 6b  mpt to.**   seek
14c2d 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74   the cursor to t
14c2e 68 65 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f  he saved positio
14c2f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f  n..**.** CURSOR_
14c30 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 75 6e  FAULT:.**   A un
14c31 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72 72 6f  recoverable erro
14c32 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20  r (an I/O error 
14c33 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  or a malloc fail
14c34 75 72 65 29 20 68 61 73 20 6f 63 63 75 72 72 65  ure) has occurre
14c35 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 66 66  d.**   on a diff
14c36 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e  erent connection
14c37 20 74 68 61 74 20 73 68 61 72 65 73 20 74 68 65   that shares the
14c38 20 42 74 53 68 61 72 65 64 20 63 61 63 68 65 20   BtShared cache 
14c39 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20 20 63  with this.**   c
14c3a 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72 72 6f  ursor.  The erro
14c3b 72 20 68 61 73 20 6c 65 66 74 20 74 68 65 20 63  r has left the c
14c3c 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63 6f 6e  ache in an incon
14c3d 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a  sistent state..*
14c3e 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67 20 65  *   Do nothing e
14c3f 6c 73 65 20 77 69 74 68 20 74 68 69 73 20 63 75  lse with this cu
14c40 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74 65 6d  rsor.  Any attem
14c41 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 63 75  pt to use the cu
14c42 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 6c 64  rsor.**   should
14c43 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   return the erro
14c44 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e  r code stored in
14c45 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70 0a 2a   BtCursor.skip.*
14c46 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52  /.#define CURSOR
14c47 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 20 20  _INVALID        
14c48 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43 55 52     0.#define CUR
14c49 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 20 20  SOR_VALID       
14c4a 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
14c4b 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
14c4c 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65 66 69  EK       2.#defi
14c4d 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  ne CURSOR_FAULT 
14c4e 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 0a 2f              3../
14c4f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 74 61 62 61  * .** The databa
14c50 73 65 20 70 61 67 65 20 74 68 65 20 50 45 4e 44  se page the PEND
14c51 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70 69 65  ING_BYTE occupie
14c52 73 2e 20 54 68 69 73 20 70 61 67 65 20 69 73 20  s. This page is 
14c53 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2f 0a 23  never used..*/.#
14c54 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f   define PENDING_
14c55 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 50  BYTE_PAGE(pBt) P
14c56 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 42 74  AGER_MJ_PGNO(pBt
14c57 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d  )../*.** These m
14c58 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 68 65  acros define the
14c59 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65   location of the
14c5a 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74   pointer-map ent
14c5b 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 64 61 74  ry for a .** dat
14c5c 61 62 61 73 65 20 70 61 67 65 2e 20 54 68 65 20  abase page. The 
14c5d 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
14c5e 6f 20 65 61 63 68 20 69 73 20 74 68 65 20 6e 75  o each is the nu
14c5f 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 0a 2a  mber of usable.*
14c60 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20  * bytes on each 
14c61 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
14c62 62 61 73 65 20 28 6f 66 74 65 6e 20 31 30 32 34  base (often 1024
14c63 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73  ). The second is
14c64 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e 75 6d   the.** page num
14c65 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 20 69  ber to look up i
14c66 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  n the pointer ma
14c67 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  p..**.** PTRMAP_
14c68 50 41 47 45 4e 4f 20 72 65 74 75 72 6e 73 20 74  PAGENO returns t
14c69 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
14c6a 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70   number of the p
14c6b 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 70 61  ointer-map.** pa
14c6c 67 65 20 74 68 61 74 20 73 74 6f 72 65 73 20 74  ge that stores t
14c6d 68 65 20 72 65 71 75 69 72 65 64 20 70 6f 69 6e  he required poin
14c6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 54 52 4f  ter. PTRMAP_PTRO
14c6f 46 46 53 45 54 20 72 65 74 75 72 6e 73 0a 2a 2a  FFSET returns.**
14c70 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74   the offset of t
14c71 68 65 20 72 65 71 75 65 73 74 65 64 20 6d 61 70  he requested map
14c72 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66   entry..**.** If
14c73 20 74 68 65 20 70 67 6e 6f 20 61 72 67 75 6d 65   the pgno argume
14c74 6e 74 20 70 61 73 73 65 64 20 74 6f 20 50 54 52  nt passed to PTR
14c75 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 20 61 20  MAP_PAGENO is a 
14c76 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
14c77 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e 6f 20 69  ,.** then pgno i
14c78 73 20 72 65 74 75 72 6e 65 64 2e 20 53 6f 20 28  s returned. So (
14c79 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 41 47  pgno==PTRMAP_PAG
14c7a 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e 6f 29 29  ENO(pgsz, pgno))
14c7b 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 20   can be.** used 
14c7c 74 6f 20 74 65 73 74 20 69 66 20 70 67 6e 6f 20  to test if pgno 
14c7d 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70  is a pointer-map
14c7e 20 70 61 67 65 2e 20 50 54 52 4d 41 50 5f 49 53   page. PTRMAP_IS
14c7f 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e 74 73 0a  PAGE implements.
14c80 2a 2a 20 74 68 69 73 20 74 65 73 74 2e 0a 2a 2f  ** this test..*/
14c81 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f  .#define PTRMAP_
14c82 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67 6e 6f  PAGENO(pBt, pgno
14c83 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 70  ) ptrmapPageno(p
14c84 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 66 69 6e  Bt, pgno).#defin
14c85 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53  e PTRMAP_PTROFFS
14c86 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70 67 6e  ET(pgptrmap, pgn
14c87 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67 70 74  o) (5*(pgno-pgpt
14c88 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 69 6e 65  rmap-1)).#define
14c89 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70   PTRMAP_ISPAGE(p
14c8a 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52 4d 41  Bt, pgno) (PTRMA
14c8b 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29 2c 28  P_PAGENO((pBt),(
14c8c 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 29 0a  pgno))==(pgno)).
14c8d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74  ./*.** The point
14c8e 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f 6f 6b  er map is a look
14c8f 75 70 20 74 61 62 6c 65 20 74 68 61 74 20 69 64  up table that id
14c90 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 72  entifies the par
14c91 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a 2a 20  ent page for.** 
14c92 65 61 63 68 20 63 68 69 6c 64 20 70 61 67 65 20  each child page 
14c93 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
14c94 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72 65 6e  file.  The paren
14c95 74 20 70 61 67 65 20 69 73 20 74 68 65 20 70 61  t page is the pa
14c96 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e 74 61  ge that.** conta
14c97 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ins a pointer to
14c98 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45 76 65   the child.  Eve
14c99 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 64  ry page in the d
14c9a 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 73  atabase contains
14c9b 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72 65 6e  .** 0 or 1 paren
14c9c 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20 74 68  t pages.  (In th
14c9d 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61 74 61  is context 'data
14c9e 62 61 73 65 20 70 61 67 65 27 20 72 65 66 65 72  base page' refer
14c9f 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61 67 65  s.** to any page
14ca0 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72   that is not par
14ca1 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72  t of the pointer
14ca2 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20 20 45   map itself.)  E
14ca3 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61 70 0a  ach pointer map.
14ca4 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69 73 74  ** entry consist
14ca5 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 62 79  s of a single by
14ca6 74 65 20 27 74 79 70 65 27 20 61 6e 64 20 61 20  te 'type' and a 
14ca7 34 20 62 79 74 65 20 70 61 72 65 6e 74 20 70 61  4 byte parent pa
14ca8 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 54 68  ge number..** Th
14ca9 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69 64 65  e PTRMAP_XXX ide
14caa 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77 20 61  ntifiers below a
14cab 72 65 20 74 68 65 20 76 61 6c 69 64 20 74 79 70  re the valid typ
14cac 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 75  es..**.** The pu
14cad 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70 6f 69  rpose of the poi
14cae 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f 20 66  nter map is to f
14caf 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67 20 70  acility moving p
14cb0 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a  ages from one.**
14cb1 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65   position in the
14cb2 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68 65 72   file to another
14cb3 20 61 73 20 70 61 72 74 20 6f 66 20 61 75 74 6f   as part of auto
14cb4 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20 61 20  vacuum.  When a 
14cb5 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 65 64  page.** is moved
14cb6 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 6e  , the pointer in
14cb7 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75 73 74   its parent must
14cb8 20 62 65 20 75 70 64 61 74 65 64 20 74 6f 20 70   be updated to p
14cb9 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 6e  oint to the.** n
14cba 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68  ew location.  Th
14cbb 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73  e pointer map is
14cbc 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20   used to locate 
14cbd 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
14cbe 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50  quickly..**.** P
14cbf 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 3a 20  TRMAP_ROOTPAGE: 
14cc0 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  The database pag
14cc1 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65  e is a root-page
14cc2 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65  . The page-numbe
14cc3 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20  r is not.**     
14cc4 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65               use
14cc5 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  d in this case..
14cc6 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 52 45  **.** PTRMAP_FRE
14cc7 45 50 41 47 45 3a 20 54 68 65 20 64 61 74 61 62  EPAGE: The datab
14cc8 61 73 65 20 70 61 67 65 20 69 73 20 61 6e 20 75  ase page is an u
14cc9 6e 75 73 65 64 20 28 66 72 65 65 29 20 70 61 67  nused (free) pag
14cca 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62  e. The page-numb
14ccb 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  er .**          
14ccc 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74 20 75          is not u
14ccd 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65  sed in this case
14cce 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f  ..**.** PTRMAP_O
14ccf 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20 64 61  VERFLOW1: The da
14cd0 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 74  tabase page is t
14cd1 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e  he first page in
14cd2 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a 20 20   a list of .**  
14cd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14cd4 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
14cd5 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   The page number
14cd6 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
14cd7 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20 20 20  page that.**    
14cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
14cd9 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c  ontains the cell
14cda 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20   with a pointer 
14cdb 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77  to this overflow
14cdc 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52   page..**.** PTR
14cdd 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 20 54  MAP_OVERFLOW2: T
14cde 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
14cdf 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 6f   is the second o
14ce0 72 20 6c 61 74 65 72 20 70 61 67 65 20 69 6e 20  r later page in 
14ce1 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20 20  a list of.**    
14ce2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
14ce3 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54  verflow pages. T
14ce4 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69  he page-number i
14ce5 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 72  dentifies the pr
14ce6 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20 20 20  evious.**       
14ce7 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65              page
14ce8 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   in the overflow
14ce9 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a   page list..**.*
14cea 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20  * PTRMAP_BTREE: 
14ceb 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  The database pag
14cec 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f 74 20  e is a non-root 
14ced 62 74 72 65 65 20 70 61 67 65 2e 20 54 68 65 20  btree page. The 
14cee 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20  page number.**  
14cef 20 20 20 20 20 20 20 20 20 20 20 20 20 69 64 65               ide
14cf0 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 72 65  ntifies the pare
14cf1 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 62  nt page in the b
14cf2 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tree..*/.#define
14cf3 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45   PTRMAP_ROOTPAGE
14cf4 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41   1.#define PTRMA
14cf5 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23 64 65  P_FREEPAGE 2.#de
14cf6 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 52  fine PTRMAP_OVER
14cf7 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e 65 20  FLOW1 3.#define 
14cf8 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
14cf9 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41   4.#define PTRMA
14cfa 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20 41 20  P_BTREE 5../* A 
14cfb 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72 74 28  bunch of assert(
14cfc 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20  ) statements to 
14cfd 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e 73 61  check the transa
14cfe 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61 72 69  ction state vari
14cff 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 6e 64  ables.** of hand
14d00 6c 65 20 70 20 28 74 79 70 65 20 42 74 72 65 65  le p (type Btree
14d01 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61 6c 6c  *) are internall
14d02 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 2a 2f  y consistent..*/
14d03 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65 49 6e  .#define btreeIn
14d04 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20 20 61  tegrity(p) \.  a
14d05 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69  ssert( p->pBt->i
14d06 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 52  nTransaction!=TR
14d07 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e 70  ANS_NONE || p->p
14d08 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e  Bt->nTransaction
14d09 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73 65 72  ==0 ); \.  asser
14d0a 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61  t( p->pBt->inTra
14d0b 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 6e 54  nsaction>=p->inT
14d0c 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a  rans ); .../*.**
14d0d 20 54 68 65 20 49 53 41 55 54 4f 56 41 43 55 55   The ISAUTOVACUU
14d0e 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20  M macro is used 
14d0f 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65 5f 6e  within balance_n
14d10 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65 74 65  onroot() to dete
14d11 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68 65 20  rmine.** if the 
14d12 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74  database support
14d13 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f 72  s auto-vacuum or
14d14 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20 69 74   not. Because it
14d15 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69 74 68   is used.** with
14d16 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  in an expression
14d17 20 74 68 61 74 20 69 73 20 61 6e 20 61 72 67 75   that is an argu
14d18 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65 72 20  ment to another 
14d19 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c 69 74  macro .** (sqlit
14d1a 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69 74 20  eMallocRaw), it 
14d1b 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
14d1c 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69 6f 6e  to use condition
14d1d 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 0a  al compilation..
14d1e 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61 63 72  ** So, this macr
14d1f 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73  o is defined ins
14d20 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tead..*/.#ifndef
14d21 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
14d22 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e 65 20  OVACUUM.#define 
14d23 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28 70 42  ISAUTOVACUUM (pB
14d24 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 0a 23  t->autoVacuum).#
14d25 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49 53 41  else.#define ISA
14d26 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e 64  UTOVACUUM 0.#end
14d27 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  if.../*.** This 
14d28 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73  structure is pas
14d29 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 6f 75  sed around throu
14d2a 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e 69 74  gh all the sanit
14d2b 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 74 69  y checking routi
14d2c 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  nes.** in order 
14d2d 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f 66  to keep track of
14d2e 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 74 61   some global sta
14d2f 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  te information..
14d30 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
14d31 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 49 6e  t IntegrityCk In
14d32 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 75 63  tegrityCk;.struc
14d33 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 7b 0a  t IntegrityCk {.
14d34 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b    BtShared *pBt;
14d35 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 65 20      /* The tree 
14d36 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 6f 75  being checked ou
14d37 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 50  t */.  Pager *pP
14d38 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 65 20  ager;    /* The 
14d39 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 65 72  associated pager
14d3a 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 69 62  .  Also accessib
14d3b 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 67 65  le by pBt->pPage
14d3c 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 67  r */.  Pgno nPag
14d3d 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  e;       /* Numb
14d3e 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
14d3f 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
14d40 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20 20 20   int *anRef;    
14d41 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
14d42 74 69 6d 65 73 20 65 61 63 68 20 70 61 67 65 20  times each page 
14d43 69 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f  is referenced */
14d44 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20 20 20  .  int mxErr;   
14d45 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61 63 63       /* Stop acc
14d46 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f 72 73  umulating errors
14d47 20 77 68 65 6e 20 74 68 69 73 20 72 65 61 63 68   when this reach
14d48 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e 74  es zero */.  int
14d49 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 2f   nErr;         /
14d4a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 73 73  * Number of mess
14d4b 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20  ages written to 
14d4c 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 72 20 2a  zErrMsg so far *
14d4d 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63 46 61  /.  int mallocFa
14d4e 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 6d 6f 72  iled; /* A memor
14d4f 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
14d50 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 20  or has occurred 
14d51 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d 20 65 72  */.  StrAccum er
14d52 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 75 6d 75  rMsg;  /* Accumu
14d53 6c 61 74 65 20 74 68 65 20 65 72 72 6f 72 20 6d  late the error m
14d54 65 73 73 61 67 65 20 74 65 78 74 20 68 65 72 65  essage text here
14d55 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65   */.};../*.** Re
14d56 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 74 77  ad or write a tw
14d57 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 79 74 65  o- and four-byte
14d58 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65   big-endian inte
14d59 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23  ger values..*/.#
14d5a 64 65 66 69 6e 65 20 67 65 74 32 62 79 74 65 28  define get2byte(
14d5b 78 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c 38 20  x)   ((x)[0]<<8 
14d5c 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 66 69 6e  | (x)[1]).#defin
14d5d 65 20 70 75 74 32 62 79 74 65 28 70 2c 76 29 20  e put2byte(p,v) 
14d5e 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29 28 28  ((p)[0] = (u8)((
14d5f 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d 20 3d  v)>>8), (p)[1] =
14d60 20 28 75 38 29 28 76 29 29 0a 23 64 65 66 69 6e   (u8)(v)).#defin
14d61 65 20 67 65 74 34 62 79 74 65 20 73 71 6c 69 74  e get4byte sqlit
14d62 65 33 47 65 74 34 62 79 74 65 0a 23 64 65 66 69  e3Get4byte.#defi
14d63 6e 65 20 70 75 74 34 62 79 74 65 20 73 71 6c 69  ne put4byte sqli
14d64 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f 2a 2a  te3Put4byte../**
14d65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
14d66 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68 20 2a   of btreeInt.h *
14d67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14d68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14d69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
14d6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
14d6b 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
14d6c 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62 74 6d   left off in btm
14d6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  utex.c *********
14d6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66  ***********/.#if
14d6f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
14d70 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 23 69  _SHARED_CACHE.#i
14d71 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
14d72 41 46 45 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69  AFE../*.** Obtai
14d73 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d  n the BtShared m
14d74 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  utex associated 
14d75 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 6e 64  with B-Tree hand
14d76 6c 65 20 70 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 73  le p. Also,.** s
14d77 65 74 20 42 74 53 68 61 72 65 64 2e 64 62 20 74  et BtShared.db t
14d78 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  o the database h
14d79 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 74 65 64  andle associated
14d7a 20 77 69 74 68 20 70 20 61 6e 64 20 74 68 65 0a   with p and the.
14d7b 2a 2a 20 70 2d 3e 6c 6f 63 6b 65 64 20 62 6f 6f  ** p->locked boo
14d7c 6c 65 61 6e 20 74 6f 20 74 72 75 65 2e 0a 2a 2f  lean to true..*/
14d7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63  .static void loc
14d7e 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74 72 65  kBtreeMutex(Btre
14d7f 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  e *p){.  assert(
14d80 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b   p->locked==0 );
14d81 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
14d82 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
14d83 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  (p->pBt->mutex) 
14d84 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
14d85 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14d86 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
14d87 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
14d88 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 2d 3e  x_enter(p->pBt->
14d89 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 70 42 74  mutex);.  p->pBt
14d8a 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  ->db = p->db;.  
14d8b 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 7d  p->locked = 1;.}
14d8c 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
14d8d 74 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74  the BtShared mut
14d8e 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ex associated wi
14d8f 74 68 20 42 2d 54 72 65 65 20 68 61 6e 64 6c 65  th B-Tree handle
14d90 20 70 20 61 6e 64 0a 2a 2a 20 63 6c 65 61 72 20   p and.** clear 
14d91 74 68 65 20 70 2d 3e 6c 6f 63 6b 65 64 20 62 6f  the p->locked bo
14d92 6f 6c 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  olean..*/.static
14d93 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65   void unlockBtre
14d94 65 4d 75 74 65 78 28 42 74 72 65 65 20 2a 70 29  eMutex(Btree *p)
14d95 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c  {.  assert( p->l
14d96 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a 20 20 61 73  ocked==1 );.  as
14d97 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14d98 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d  tex_held(p->pBt-
14d99 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
14d9a 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14d9b 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
14d9c 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
14d9d 74 28 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 74  t( p->db==p->pBt
14d9e 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74  ->db );..  sqlit
14d9f 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
14da0 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20  ->pBt->mutex);. 
14da1 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a   p->locked = 0;.
14da2 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61  }../*.** Enter a
14da3 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 67 69   mutex on the gi
14da4 76 65 6e 20 42 54 72 65 65 20 6f 62 6a 65 63 74  ven BTree object
14da5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f  ..**.** If the o
14da6 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 73 68 61  bject is not sha
14da7 72 61 62 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d  rable, then no m
14da8 75 74 65 78 20 69 73 20 65 76 65 72 20 72 65 71  utex is ever req
14da9 75 69 72 65 64 0a 2a 2a 20 61 6e 64 20 74 68 69  uired.** and thi
14daa 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
14dab 6f 2d 6f 70 2e 20 20 54 68 65 20 75 6e 64 65 72  o-op.  The under
14dac 6c 79 69 6e 67 20 6d 75 74 65 78 20 69 73 20 6e  lying mutex is n
14dad 6f 6e 2d 72 65 63 75 72 73 69 76 65 2e 0a 2a 2a  on-recursive..**
14dae 20 42 75 74 20 77 65 20 6b 65 65 70 20 61 20 72   But we keep a r
14daf 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 69  eference count i
14db0 6e 20 42 74 72 65 65 2e 77 61 6e 74 54 6f 4c 6f  n Btree.wantToLo
14db1 63 6b 20 73 6f 20 74 68 65 20 62 65 68 61 76 69  ck so the behavi
14db2 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 73 20 69 6e  or.** of this in
14db3 74 65 72 66 61 63 65 20 69 73 20 72 65 63 75 72  terface is recur
14db4 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61  sive..**.** To a
14db5 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73 2c 20  void deadlocks, 
14db6 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65 73 20  multiple Btrees 
14db7 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68  are locked in th
14db8 65 20 73 61 6d 65 20 6f 72 64 65 72 0a 2a 2a 20  e same order.** 
14db9 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20  by all database 
14dba 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68  connections.  Th
14dbb 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20 61 20  e p->pNext is a 
14dbc 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0a 2a 2a  list of other.**
14dbd 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67 69 6e   Btrees belongin
14dbe 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61  g to the same da
14dbf 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14dc0 6e 20 61 73 20 74 68 65 20 70 20 42 74 72 65 65  n as the p Btree
14dc1 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20 74  .** which need t
14dc2 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61 66 74 65  o be locked afte
14dc3 72 20 70 2e 20 20 49 66 20 77 65 20 63 61 6e 6e  r p.  If we cann
14dc4 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b 20 6f 6e  ot get a lock on
14dc5 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 66 69 72 73  .** p, then firs
14dc6 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c 20 6f 66 20  t unlock all of 
14dc7 74 68 65 20 6f 74 68 65 72 73 20 6f 6e 20 70 2d  the others on p-
14dc8 3e 70 4e 65 78 74 2c 20 74 68 65 6e 20 77 61 69  >pNext, then wai
14dc9 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6c 6f 63  t.** for the loc
14dca 6b 20 74 6f 20 62 65 63 6f 6d 65 20 61 76 61 69  k to become avai
14dcb 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 74 68 65 6e  lable on p, then
14dcc 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 6f 66 20 74   relock all of t
14dcd 68 65 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74  he.** subsequent
14dce 20 42 74 72 65 65 73 20 74 68 61 74 20 64 65 73   Btrees that des
14dcf 69 72 65 20 61 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53  ire a lock..*/.S
14dd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14dd1 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
14dd2 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a  nter(Btree *p){.
14dd3 20 20 42 74 72 65 65 20 2a 70 4c 61 74 65 72 3b    Btree *pLater;
14dd4 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69  ..  /* Some basi
14dd5 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  c sanity checkin
14dd6 67 20 6f 6e 20 74 68 65 20 42 74 72 65 65 2e 20  g on the Btree. 
14dd7 20 54 68 65 20 6c 69 73 74 20 6f 66 20 42 74 72   The list of Btr
14dd8 65 65 73 0a 20 20 2a 2a 20 63 6f 6e 6e 65 63 74  ees.  ** connect
14dd9 65 64 20 62 79 20 70 4e 65 78 74 20 61 6e 64 20  ed by pNext and 
14dda 70 50 72 65 76 20 73 68 6f 75 6c 64 20 62 65 20  pPrev should be 
14ddb 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20  in sorted order 
14ddc 62 79 0a 20 20 2a 2a 20 42 74 72 65 65 2e 70 42  by.  ** Btree.pB
14ddd 74 20 76 61 6c 75 65 2e 20 41 6c 6c 20 65 6c 65  t value. All ele
14dde 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 73  ments of the lis
14ddf 74 20 73 68 6f 75 6c 64 20 62 65 6c 6f 6e 67 20  t should belong 
14de0 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61 6d 65  to.  ** the same
14de1 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 6c   connection. Onl
14de2 79 20 73 68 61 72 65 64 20 42 74 72 65 65 73 20  y shared Btrees 
14de3 61 72 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e  are on the list.
14de4 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
14de5 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e  >pNext==0 || p->
14de6 70 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e 70 42  pNext->pBt>p->pB
14de7 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t );.  assert( p
14de8 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 70 2d  ->pPrev==0 || p-
14de9 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d 3e 70  >pPrev->pBt<p->p
14dea 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Bt );.  assert( 
14deb 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70  p->pNext==0 || p
14dec 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70 2d 3e  ->pNext->db==p->
14ded 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  db );.  assert( 
14dee 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 70  p->pPrev==0 || p
14def 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70 2d 3e  ->pPrev->db==p->
14df0 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  db );.  assert( 
14df1 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 28  p->sharable || (
14df2 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26 20 70  p->pNext==0 && p
14df3 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29 3b 0a 0a  ->pPrev==0) );..
14df4 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6c    /* Check for l
14df5 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 73 74 65 6e  ocking consisten
14df6 63 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  cy */.  assert( 
14df7 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d  !p->locked || p-
14df8 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b  >wantToLock>0 );
14df9 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68  .  assert( p->sh
14dfa 61 72 61 62 6c 65 20 7c 7c 20 70 2d 3e 77 61 6e  arable || p->wan
14dfb 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20  tToLock==0 );.. 
14dfc 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c   /* We should al
14dfd 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63  ready hold a loc
14dfe 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
14dff 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
14e00 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
14e01 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
14e02 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
14e03 20 2f 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 64   /* Unless the d
14e04 61 74 61 62 61 73 65 20 69 73 20 73 68 61 72 61  atabase is shara
14e05 62 6c 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 65 64  ble and unlocked
14e06 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 64 2e  , then BtShared.
14e07 64 62 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 61  db.  ** should a
14e08 6c 72 65 61 64 79 20 62 65 20 73 65 74 20 63 6f  lready be set co
14e09 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 61 73  rrectly. */.  as
14e0a 73 65 72 74 28 20 28 70 2d 3e 6c 6f 63 6b 65 64  sert( (p->locked
14e0b 3d 3d 30 20 26 26 20 70 2d 3e 73 68 61 72 61 62  ==0 && p->sharab
14e0c 6c 65 29 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 64  le) || p->pBt->d
14e0d 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 0a 20 20 69  b==p->db );..  i
14e0e 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20  f( !p->sharable 
14e0f 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d 3e 77  ) return;.  p->w
14e10 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 69  antToLock++;.  i
14e11 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 20 72  f( p->locked ) r
14e12 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 20  eturn;..  /* In 
14e13 6d 6f 73 74 20 63 61 73 65 73 2c 20 77 65 20 73  most cases, we s
14e14 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20 74 6f  hould be able to
14e15 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63   acquire the loc
14e16 6b 20 77 65 0a 20 20 2a 2a 20 77 61 6e 74 20 77  k we.  ** want w
14e17 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f  ithout having to
14e18 20 67 6f 20 74 68 72 6f 75 67 68 74 20 74 68 65   go throught the
14e19 20 61 73 63 65 6e 64 69 6e 67 20 6c 6f 63 6b 0a   ascending lock.
14e1a 20 20 2a 2a 20 70 72 6f 63 65 64 75 72 65 20 74    ** procedure t
14e1b 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 20 20 4a 75  hat follows.  Ju
14e1c 73 74 20 62 65 20 73 75 72 65 20 6e 6f 74 20 74  st be sure not t
14e1d 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  o block..  */.  
14e1e 69 66 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  if( sqlite3_mute
14e1f 78 5f 74 72 79 28 70 2d 3e 70 42 74 2d 3e 6d 75  x_try(p->pBt->mu
14e20 74 65 78 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  tex)==SQLITE_OK 
14e21 29 7b 0a 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64  ){.    p->pBt->d
14e22 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 70  b = p->db;.    p
14e23 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20  ->locked = 1;.  
14e24 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
14e25 20 2f 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61   /* To avoid dea
14e26 64 6c 6f 63 6b 2c 20 66 69 72 73 74 20 72 65 6c  dlock, first rel
14e27 65 61 73 65 20 61 6c 6c 20 6c 6f 63 6b 73 20 77  ease all locks w
14e28 69 74 68 20 61 20 6c 61 72 67 65 72 0a 20 20 2a  ith a larger.  *
14e29 2a 20 42 74 53 68 61 72 65 64 20 61 64 64 72 65  * BtShared addre
14e2a 73 73 2e 20 20 54 68 65 6e 20 61 63 71 75 69 72  ss.  Then acquir
14e2b 65 20 6f 75 72 20 6c 6f 63 6b 2e 20 20 54 68 65  e our lock.  The
14e2c 6e 20 72 65 61 63 71 75 69 72 65 0a 20 20 2a 2a  n reacquire.  **
14e2d 20 74 68 65 20 6f 74 68 65 72 20 42 74 53 68 61   the other BtSha
14e2e 72 65 64 20 6c 6f 63 6b 73 20 74 68 61 74 20 77  red locks that w
14e2f 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69  e used to hold i
14e30 6e 20 61 73 63 65 6e 64 69 6e 67 0a 20 20 2a 2a  n ascending.  **
14e31 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66   order..  */.  f
14e32 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65  or(pLater=p->pNe
14e33 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74  xt; pLater; pLat
14e34 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74  er=pLater->pNext
14e35 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
14e36 4c 61 74 65 72 2d 3e 73 68 61 72 61 62 6c 65 20  Later->sharable 
14e37 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
14e38 4c 61 74 65 72 2d 3e 70 4e 65 78 74 3d 3d 30 20  Later->pNext==0 
14e39 7c 7c 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74  || pLater->pNext
14e3a 2d 3e 70 42 74 3e 70 4c 61 74 65 72 2d 3e 70 42  ->pBt>pLater->pB
14e3b 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
14e3c 20 21 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64   !pLater->locked
14e3d 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74   || pLater->want
14e3e 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20  ToLock>0 );.    
14e3f 69 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b  if( pLater->lock
14e40 65 64 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f  ed ){.      unlo
14e41 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c 61  ckBtreeMutex(pLa
14e42 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ter);.    }.  }.
14e43 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78    lockBtreeMutex
14e44 28 70 29 3b 0a 20 20 66 6f 72 28 70 4c 61 74 65  (p);.  for(pLate
14e45 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74  r=p->pNext; pLat
14e46 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65  er; pLater=pLate
14e47 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  r->pNext){.    i
14e48 66 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74 54  f( pLater->wantT
14e49 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6c  oLock ){.      l
14e4a 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c  ockBtreeMutex(pL
14e4b 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ater);.    }.  }
14e4c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69 74 20 74  .}../*.** Exit t
14e4d 68 65 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  he recursive mut
14e4e 65 78 20 6f 6e 20 61 20 42 74 72 65 65 2e 0a 2a  ex on a Btree..*
14e4f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14e50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
14e51 65 65 4c 65 61 76 65 28 42 74 72 65 65 20 2a 70  eeLeave(Btree *p
14e52 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72  ){.  if( p->shar
14e53 61 62 6c 65 20 29 7b 0a 20 20 20 20 61 73 73 65  able ){.    asse
14e54 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  rt( p->wantToLoc
14e55 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 77 61  k>0 );.    p->wa
14e56 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20  ntToLock--;.    
14e57 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  if( p->wantToLoc
14e58 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 75 6e  k==0 ){.      un
14e59 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70  lockBtreeMutex(p
14e5a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
14e5b 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f  #ifndef NDEBUG./
14e5c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
14e5d 20 69 66 20 74 68 65 20 42 74 53 68 61 72 65 64   if the BtShared
14e5e 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 6f   mutex is held o
14e5f 6e 20 74 68 65 20 62 74 72 65 65 2c 20 6f 72 20  n the btree, or 
14e60 69 66 20 74 68 65 0a 2a 2a 20 42 2d 54 72 65 65  if the.** B-Tree
14e61 20 69 73 20 6e 6f 74 20 6d 61 72 6b 65 64 20 61   is not marked a
14e62 73 20 73 68 61 72 61 62 6c 65 2e 0a 2a 2a 0a 2a  s sharable..**.*
14e63 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
14e64 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 72 6f 6d  s used only from
14e65 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29   within assert()
14e66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a   statements..*/.
14e67 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
14e68 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  nt sqlite3BtreeH
14e69 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 20  oldsMutex(Btree 
14e6a 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
14e6b 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c  ->sharable==0 ||
14e6c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c 7c   p->locked==0 ||
14e6d 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30   p->wantToLock>0
14e6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
14e6f 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 20  >sharable==0 || 
14e70 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20  p->locked==0 || 
14e71 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 74 2d 3e 64  p->db==p->pBt->d
14e72 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  b );.  assert( p
14e73 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c  ->sharable==0 ||
14e74 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c 7c   p->locked==0 ||
14e75 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14e76 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65  eld(p->pBt->mute
14e77 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
14e78 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c  p->sharable==0 |
14e79 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c  | p->locked==0 |
14e7a 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
14e7b 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  held(p->db->mute
14e7c 78 29 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  x) );..  return 
14e7d 28 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20  (p->sharable==0 
14e7e 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 29 3b 0a 7d  || p->locked);.}
14e7f 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65  .#endif...#ifnde
14e80 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
14e81 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45 6e 74  CRBLOB./*.** Ent
14e82 65 72 20 61 6e 64 20 6c 65 61 76 65 20 61 20 6d  er and leave a m
14e83 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65 20  utex on a Btree 
14e84 67 69 76 65 6e 20 61 20 63 75 72 73 6f 72 20 6f  given a cursor o
14e85 77 6e 65 64 20 62 79 20 74 68 61 74 0a 2a 2a 20  wned by that.** 
14e86 42 74 72 65 65 2e 20 20 54 68 65 73 65 20 65 6e  Btree.  These en
14e87 74 72 79 20 70 6f 69 6e 74 73 20 61 72 65 20 75  try points are u
14e88 73 65 64 20 62 79 20 69 6e 63 72 65 6d 65 6e 74  sed by increment
14e89 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e 20 62  al I/O and can b
14e8a 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69 66 20  e.** omitted if 
14e8b 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 73 20 6e  that module is n
14e8c 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49  ot used..*/.SQLI
14e8d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
14e8e 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
14e8f 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  rCursor(BtCursor
14e90 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c 69 74   *pCur){.  sqlit
14e91 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 43 75  e3BtreeEnter(pCu
14e92 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a 53 51  r->pBtree);.}.SQ
14e93 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
14e94 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65  d sqlite3BtreeLe
14e95 61 76 65 43 75 72 73 6f 72 28 42 74 43 75 72 73  aveCursor(BtCurs
14e96 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c  or *pCur){.  sql
14e97 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
14e98 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a  Cur->pBtree);.}.
14e99 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
14e9a 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a  _OMIT_INCRBLOB *
14e9b 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20  /.../*.** Enter 
14e9c 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65 76 65  the mutex on eve
14e9d 72 79 20 42 74 72 65 65 20 61 73 73 6f 63 69 61  ry Btree associa
14e9e 74 65 64 20 77 69 74 68 20 61 20 64 61 74 61 62  ted with a datab
14e9f 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  ase.** connectio
14ea0 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 64  n.  This is need
14ea1 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29  ed (for example)
14ea2 20 70 72 69 6f 72 20 74 6f 20 70 61 72 73 69 6e   prior to parsin
14ea3 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 6e 74  g.** a statement
14ea4 20 73 69 6e 63 65 20 77 65 20 77 69 6c 6c 20 62   since we will b
14ea5 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 61 62 6c  e comparing tabl
14ea6 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  e and column nam
14ea7 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 61 6c  es.** against al
14ea8 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 20 77 65  l schemas and we
14ea9 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 6f   do not want tho
14eaa 73 65 20 73 63 68 65 6d 61 73 20 62 65 69 6e 67  se schemas being
14eab 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 20 66 72  .** reset out fr
14eac 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a 2a 0a  om under us..**.
14ead 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 63 6f  ** There is a co
14eae 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65 61 76  rresponding leav
14eaf 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72 65 73  e-all procedures
14eb0 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68  ..**.** Enter th
14eb1 65 20 6d 75 74 65 78 65 73 20 69 6e 20 61 63 63  e mutexes in acc
14eb2 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20  ending order by 
14eb3 42 74 53 68 61 72 65 64 20 70 6f 69 6e 74 65 72  BtShared pointer
14eb4 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f 20 61   address.** to a
14eb5 76 6f 69 64 20 74 68 65 20 70 6f 73 73 69 62 69  void the possibi
14eb6 6c 69 74 79 20 6f 66 20 64 65 61 64 6c 6f 63 6b  lity of deadlock
14eb7 20 77 68 65 6e 20 74 77 6f 20 74 68 72 65 61 64   when two thread
14eb8 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20 6f 72  s with.** two or
14eb9 20 6d 6f 72 65 20 62 74 72 65 65 73 20 69 6e 20   more btrees in 
14eba 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72 79 20  common both try 
14ebb 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68 65 69  to lock all thei
14ebc 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 74 20 74  r btrees.** at t
14ebd 68 65 20 73 61 6d 65 20 69 6e 73 74 61 6e 74 2e  he same instant.
14ebe 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14ebf 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
14ec0 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c  treeEnterAll(sql
14ec1 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
14ec2 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 2c 20   i;.  Btree *p, 
14ec3 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 73 65 72  *pLater;.  asser
14ec4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14ec5 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
14ec6 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   );.  for(i=0; i
14ec7 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
14ec8 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b      p = db->aDb[
14ec9 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 73 65  i].pBt;.    asse
14eca 72 74 28 20 21 70 20 7c 7c 20 28 70 2d 3e 6c 6f  rt( !p || (p->lo
14ecb 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e 73 68  cked==0 && p->sh
14ecc 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e 70 42  arable) || p->pB
14ecd 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a  t->db==p->db );.
14ece 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e      if( p && p->
14ecf 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
14ed0 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b    p->wantToLock+
14ed1 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d  +;.      if( !p-
14ed2 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20  >locked ){.     
14ed3 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61     assert( p->wa
14ed4 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20  ntToLock==1 );. 
14ed5 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d         while( p-
14ed6 3e 70 50 72 65 76 20 29 20 70 20 3d 20 70 2d 3e  >pPrev ) p = p->
14ed7 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20 20 2f  pPrev;.        /
14ed8 2a 20 52 65 61 73 6f 6e 20 66 6f 72 20 41 4c 57  * Reason for ALW
14ed9 41 59 53 3a 20 20 54 68 65 72 65 20 6d 75 73 74  AYS:  There must
14eda 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 20   be at least on 
14edb 75 6e 6c 6f 63 6b 65 64 20 42 74 72 65 65 20 69  unlocked Btree i
14edc 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  n.        ** the
14edd 20 63 68 61 69 6e 2e 20 20 4f 74 68 65 72 77 69   chain.  Otherwi
14ede 73 65 20 74 68 65 20 21 70 2d 3e 6c 6f 63 6b 65  se the !p->locke
14edf 64 20 74 65 73 74 20 61 62 6f 76 65 20 77 6f 75  d test above wou
14ee0 6c 64 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a  ld have failed *
14ee1 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  /.        while(
14ee2 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 41 4c   p->locked && AL
14ee3 57 41 59 53 28 70 2d 3e 70 4e 65 78 74 29 20 29  WAYS(p->pNext) )
14ee4 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20   p = p->pNext;. 
14ee5 20 20 20 20 20 20 20 66 6f 72 28 70 4c 61 74 65         for(pLate
14ee6 72 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 70 4c  r = p->pNext; pL
14ee7 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61  ater; pLater=pLa
14ee8 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
14ee9 20 20 20 20 20 20 20 69 66 28 20 70 4c 61 74 65         if( pLate
14eea 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20  r->locked ){.   
14eeb 20 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42           unlockB
14eec 74 72 65 65 4d 75 74 65 78 28 70 4c 61 74 65 72  treeMutex(pLater
14eed 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
14eee 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
14eef 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
14ef0 20 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 65         lockBtree
14ef1 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 20 20  Mutex(p);.      
14ef2 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74      p = p->pNext
14ef3 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
14ef4 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
14ef5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
14ef6 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
14ef7 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33  LeaveAll(sqlite3
14ef8 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   *db){.  int i;.
14ef9 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 61 73    Btree *p;.  as
14efa 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14efb 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
14efc 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30  ex) );.  for(i=0
14efd 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
14efe 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61  ){.    p = db->a
14eff 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69  Db[i].pBt;.    i
14f00 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61  f( p && p->shara
14f01 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ble ){.      ass
14f02 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  ert( p->wantToLo
14f03 63 6b 3e 30 20 29 3b 0a 20 20 20 20 20 20 70 2d  ck>0 );.      p-
14f04 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20  >wantToLock--;. 
14f05 20 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74       if( p->want
14f06 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20  ToLock==0 ){.   
14f07 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65       unlockBtree
14f08 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 20 20  Mutex(p);.      
14f09 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23  }.    }.  }.}..#
14f0a 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
14f0b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
14f0c 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  if the current t
14f0d 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65 20  hread holds the 
14f0e 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
14f0f 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64  ion.** mutex and
14f10 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20 42 74   all required Bt
14f11 53 68 61 72 65 64 20 6d 75 74 65 78 65 73 2e 0a  Shared mutexes..
14f12 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
14f13 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 64  ne is used insid
14f14 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  e assert() state
14f15 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53  ments only..*/.S
14f16 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14f17 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  t sqlite3BtreeHo
14f18 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 73 71  ldsAllMutexes(sq
14f19 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e  lite3 *db){.  in
14f1a 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  t i;.  if( !sqli
14f1b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
14f1c 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20 20  b->mutex) ){.   
14f1d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
14f1e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
14f1f 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42  nDb; i++){.    B
14f20 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70 20 3d  tree *p;.    p =
14f21 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
14f22 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d  .    if( p && p-
14f23 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20 20 20  >sharable &&.   
14f24 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74 54 6f        (p->wantTo
14f25 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71 6c 69  Lock==0 || !sqli
14f26 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14f27 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 20 29  ->pBt->mutex)) )
14f28 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
14f29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
14f2a 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66  turn 1;.}.#endif
14f2b 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f   /* NDEBUG */../
14f2c 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 42  *.** Add a new B
14f2d 74 72 65 65 20 70 6f 69 6e 74 65 72 20 74 6f 20  tree pointer to 
14f2e 61 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  a BtreeMutexArra
14f2f 79 2e 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 6f  y. .** if the po
14f30 69 6e 74 65 72 20 63 61 6e 20 70 6f 73 73 69 62  inter can possib
14f31 6c 79 20 62 65 20 73 68 61 72 65 64 20 77 69 74  ly be shared wit
14f32 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 64 61 74  h.** another dat
14f33 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
14f34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ..**.** The poin
14f35 74 65 72 73 20 61 72 65 20 6b 65 70 74 20 69 6e  ters are kept in
14f36 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20 62 79   sorted order by
14f37 20 70 42 74 72 65 65 2d 3e 70 42 74 2e 20 20 54   pBtree->pBt.  T
14f38 68 61 74 0a 2a 2a 20 77 61 79 20 77 68 65 6e 20  hat.** way when 
14f39 77 65 20 67 6f 20 74 6f 20 65 6e 74 65 72 20 61  we go to enter a
14f3a 6c 6c 20 74 68 65 20 6d 75 74 65 78 65 73 2c 20  ll the mutexes, 
14f3b 77 65 20 63 61 6e 20 65 6e 74 65 72 20 74 68 65  we can enter the
14f3c 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 77 69  m.** in order wi
14f3d 74 68 6f 75 74 20 65 76 65 72 79 20 68 61 76 69  thout every havi
14f3e 6e 67 20 74 6f 20 62 61 63 6b 75 70 20 61 6e 64  ng to backup and
14f3f 20 72 65 74 72 79 20 61 6e 64 20 77 69 74 68 6f   retry and witho
14f40 75 74 0a 2a 2a 20 77 6f 72 72 79 69 6e 67 20 61  ut.** worrying a
14f41 62 6f 75 74 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a  bout deadlock..*
14f42 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20  *.** The number 
14f43 6f 66 20 73 68 61 72 65 64 20 62 74 72 65 65 73  of shared btrees
14f44 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20   will always be 
14f45 73 6d 61 6c 6c 20 28 75 73 75 61 6c 6c 79 20 30  small (usually 0
14f46 20 6f 72 20 31 29 0a 2a 2a 20 73 6f 20 61 6e 20   or 1).** so an 
14f47 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 20 69  insertion sort i
14f48 73 20 61 6e 20 61 64 65 71 75 61 74 65 20 61 6c  s an adequate al
14f49 67 6f 72 69 74 68 6d 20 68 65 72 65 2e 0a 2a 2f  gorithm here..*/
14f4a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14f4b 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
14f4c 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72  eMutexArrayInser
14f4d 74 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  t(BtreeMutexArra
14f4e 79 20 2a 70 41 72 72 61 79 2c 20 42 74 72 65 65  y *pArray, Btree
14f4f 20 2a 70 42 74 72 65 65 29 7b 0a 20 20 69 6e 74   *pBtree){.  int
14f50 20 69 2c 20 6a 3b 0a 20 20 42 74 53 68 61 72 65   i, j;.  BtShare
14f51 64 20 2a 70 42 74 3b 0a 20 20 69 66 28 20 70 42  d *pBt;.  if( pB
14f52 74 72 65 65 3d 3d 30 20 7c 7c 20 70 42 74 72 65  tree==0 || pBtre
14f53 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 29  e->sharable==0 )
14f54 20 72 65 74 75 72 6e 3b 0a 23 69 66 6e 64 65 66   return;.#ifndef
14f55 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20 20 20   NDEBUG.  {.    
14f56 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61  for(i=0; i<pArra
14f57 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b  y->nMutex; i++){
14f58 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
14f59 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d  Array->aBtree[i]
14f5a 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20  !=pBtree );.    
14f5b 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 61  }.  }.#endif.  a
14f5c 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e 6e  ssert( pArray->n
14f5d 4d 75 74 65 78 3e 3d 30 20 29 3b 0a 20 20 61 73  Mutex>=0 );.  as
14f5e 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e 6e 4d  sert( pArray->nM
14f5f 75 74 65 78 3c 41 72 72 61 79 53 69 7a 65 28 70  utex<ArraySize(p
14f60 41 72 72 61 79 2d 3e 61 42 74 72 65 65 29 2d 31  Array->aBtree)-1
14f61 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42 74 72   );.  pBt = pBtr
14f62 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 28 69  ee->pBt;.  for(i
14f63 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d  =0; i<pArray->nM
14f64 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  utex; i++){.    
14f65 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e  assert( pArray->
14f66 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72 65  aBtree[i]!=pBtre
14f67 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 41 72  e );.    if( pAr
14f68 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 2d 3e  ray->aBtree[i]->
14f69 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20 20 20  pBt>pBt ){.     
14f6a 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d 3e 6e   for(j=pArray->n
14f6b 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d 2d 29  Mutex; j>i; j--)
14f6c 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72 61 79  {.        pArray
14f6d 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20 70 41  ->aBtree[j] = pA
14f6e 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a 2d 31  rray->aBtree[j-1
14f6f 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ];.      }.     
14f70 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14f71 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20 20 20  i] = pBtree;.   
14f72 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65     pArray->nMute
14f73 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72  x++;.      retur
14f74 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  n;.    }.  }.  p
14f75 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 70 41  Array->aBtree[pA
14f76 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b 5d 20  rray->nMutex++] 
14f77 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f 2a 0a  = pBtree;.}../*.
14f78 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74  ** Enter the mut
14f79 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72 65  ex of every btre
14f7a 65 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20  e in the array. 
14f7b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
14f7c 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 74 68  .** called at th
14f7d 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 73  e beginning of s
14f7e 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29  qlite3VdbeExec()
14f7f 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 61  .  The mutexes a
14f80 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61 74 20  re.** exited at 
14f81 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73  the end of the s
14f82 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  ame function..*/
14f83 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14f84 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
14f85 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72  eMutexArrayEnter
14f86 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  (BtreeMutexArray
14f87 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74   *pArray){.  int
14f88 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
14f89 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b  <pArray->nMutex;
14f8a 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65   i++){.    Btree
14f8b 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42   *p = pArray->aB
14f8c 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20  tree[i];.    /* 
14f8d 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74  Some basic sanit
14f8e 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20  y checking */.  
14f8f 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c    assert( i==0 |
14f90 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65  | pArray->aBtree
14f91 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42  [i-1]->pBt<p->pB
14f92 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
14f93 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70   !p->locked || p
14f94 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29  ->wantToLock>0 )
14f95 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f  ;..    /* We sho
14f96 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64  uld already hold
14f97 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64   a lock on the d
14f98 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14f99 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  on */.    assert
14f9a 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
14f9b 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  held(p->db->mute
14f9c 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68  x) );..    /* Th
14f9d 65 20 42 74 72 65 65 20 69 73 20 73 68 61 72 61  e Btree is shara
14f9e 62 6c 65 20 62 65 63 61 75 73 65 20 6f 6e 6c 79  ble because only
14f9f 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73   sharable Btrees
14fa0 20 61 72 65 20 65 6e 74 65 72 65 64 0a 20 20 20   are entered.   
14fa1 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61 72 72   ** into the arr
14fa2 61 79 20 69 6e 20 74 68 65 20 66 69 72 73 74 20  ay in the first 
14fa3 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 20 20 61 73  place. */.    as
14fa4 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c  sert( p->sharabl
14fa5 65 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e  e );..    p->wan
14fa6 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 69  tToLock++;.    i
14fa7 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b  f( !p->locked ){
14fa8 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 65  .      lockBtree
14fa9 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d 0a  Mutex(p);.    }.
14faa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 61    }.}../*.** Lea
14fab 76 65 20 74 68 65 20 6d 75 74 65 78 20 6f 66 20  ve the mutex of 
14fac 65 76 65 72 79 20 62 74 72 65 65 20 69 6e 20 74  every btree in t
14fad 68 65 20 67 72 6f 75 70 2e 0a 2a 2f 0a 53 51 4c  he group..*/.SQL
14fae 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
14faf 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
14fb0 65 78 41 72 72 61 79 4c 65 61 76 65 28 42 74 72  exArrayLeave(Btr
14fb1 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41  eeMutexArray *pA
14fb2 72 72 61 79 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  rray){.  int i;.
14fb3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72    for(i=0; i<pAr
14fb4 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b  ray->nMutex; i++
14fb5 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20  ){.    Btree *p 
14fb6 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65  = pArray->aBtree
14fb7 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65  [i];.    /* Some
14fb8 20 62 61 73 69 63 20 73 61 6e 69 74 79 20 63 68   basic sanity ch
14fb9 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 61 73  ecking */.    as
14fba 73 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20 70 41  sert( i==0 || pA
14fbb 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 2d 31  rray->aBtree[i-1
14fbc 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b  ]->pBt<p->pBt );
14fbd 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
14fbe 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 20 20 61 73  locked );.    as
14fbf 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c  sert( p->wantToL
14fc0 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a  ock>0 );..    /*
14fc1 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61   We should alrea
14fc2 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f  dy hold a lock o
14fc3 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  n the database c
14fc4 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20  onnection */.   
14fc5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
14fc6 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
14fc7 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
14fc8 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d    p->wantToLock-
14fc9 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 61  -;.    if( p->wa
14fca 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20  ntToLock==0 ){. 
14fcb 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65       unlockBtree
14fcc 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d 0a  Mutex(p);.    }.
14fcd 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 53 51 4c    }.}..#else.SQL
14fce 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
14fcf 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
14fd0 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  er(Btree *p){.  
14fd1 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e  p->pBt->db = p->
14fd2 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  db;.}.SQLITE_PRI
14fd3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14fd4 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73  3BtreeEnterAll(s
14fd5 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
14fd6 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
14fd7 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
14fd8 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d  {.    Btree *p =
14fd9 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
14fda 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20  .    if( p ){.  
14fdb 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d      p->pBt->db =
14fdc 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d 0a 20 20   p->db;.    }.  
14fdd 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66  }.}.#endif /* if
14fde 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
14fdf 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  FE */.#endif /* 
14fe0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14fe1 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20  IT_SHARED_CACHE 
14fe2 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
14fe3 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d 75 74  *** End of btmut
14fe4 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ex.c ***********
14fe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fe6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fe7 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
14fe8 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
14fe9 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tree.c *********
14fea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14feb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fec 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41  **/./*.** 2004 A
14fed 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  pril 6.**.** The
14fee 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
14fef 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
14ff0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
14ff1 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
14ff2 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
14ff3 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
14ff4 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
14ff5 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
14ff6 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
14ff7 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
14ff8 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
14ff9 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
14ffa 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
14ffb 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
14ffc 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
14ffd 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
14ffe 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
14fff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15003 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 74  *****.** $Id: bt
15004 72 65 65 2e 63 2c 76 20 31 2e 37 30 35 20 32 30  ree.c,v 1.705 20
15005 30 39 2f 30 38 2f 31 30 20 30 33 3a 35 37 3a 35  09/08/10 03:57:5
15006 38 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2a  8 shane Exp $.**
15007 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
15008 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65 72  plements a exter
15009 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64 29  nal (disk-based)
1500a 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20   database using 
1500b 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65 20 74  BTrees..** See t
1500c 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  he header commen
1500d 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74 2e 68  t on "btreeInt.h
1500e 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  " for additional
1500f 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
15010 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64 65 73   Including a des
15011 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69 6c 65  cription of file
15012 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e 20 6f   format and an o
15013 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65 72 61  verview of opera
15014 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  tion..*/../*.** 
15015 54 68 65 20 68 65 61 64 65 72 20 73 74 72 69 6e  The header strin
15016 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61  g that appears a
15017 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
15018 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c 69  of every.** SQLi
15019 74 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a  te database..*/.
1501a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
1501b 72 20 7a 4d 61 67 69 63 48 65 61 64 65 72 5b 5d  r zMagicHeader[]
1501c 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48   = SQLITE_FILE_H
1501d 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65  EADER;../*.** Se
1501e 74 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61  t this global va
1501f 72 69 61 62 6c 65 20 74 6f 20 31 20 74 6f 20 65  riable to 1 to e
15020 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 75 73  nable tracing us
15021 69 6e 67 20 74 68 65 20 54 52 41 43 45 0a 2a 2a  ing the TRACE.**
15022 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 30   macro..*/.#if 0
15023 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65  .int sqlite3Btre
15024 65 54 72 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72  eTrace=1;  /* Tr
15025 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61  ue to enable tra
15026 63 69 6e 67 20 2a 2f 0a 23 20 64 65 66 69 6e 65  cing */.# define
15027 20 54 52 41 43 45 28 58 29 20 20 69 66 28 73 71   TRACE(X)  if(sq
15028 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65 29  lite3BtreeTrace)
15029 7b 70 72 69 6e 74 66 20 58 3b 66 66 6c 75 73 68  {printf X;fflush
1502a 28 73 74 64 6f 75 74 29 3b 7d 0a 23 65 6c 73 65  (stdout);}.#else
1502b 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28  .# define TRACE(
1502c 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 66  X).#endif....#if
1502d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1502e 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a  _SHARED_CACHE./*
1502f 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 42 74  .** A list of Bt
15030 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73 20 74  Shared objects t
15031 68 61 74 20 61 72 65 20 65 6c 69 67 69 62 6c 65  hat are eligible
15032 20 66 6f 72 20 70 61 72 74 69 63 69 70 61 74 69   for participati
15033 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65 64 20  on.** in shared 
15034 63 61 63 68 65 2e 20 20 54 68 69 73 20 76 61 72  cache.  This var
15035 69 61 62 6c 65 20 68 61 73 20 66 69 6c 65 20 73  iable has file s
15036 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f 72 6d  cope during norm
15037 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20 62 75  al builds,.** bu
15038 74 20 74 68 65 20 74 65 73 74 20 68 61 72 6e 65  t the test harne
15039 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63 63 65  ss needs to acce
1503a 73 73 20 69 74 20 73 6f 20 77 65 20 6d 61 6b 65  ss it so we make
1503b 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72 20 0a   it global for .
1503c 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73 2e 0a  ** test builds..
1503d 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f 20  **.** Access to 
1503e 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73  this variable is
1503f 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 53 51   protected by SQ
15040 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
15041 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a 23 69 66  C_MASTER..*/.#if
15042 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
15043 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42  SQLITE_PRIVATE B
15044 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f  tShared *SQLITE_
15045 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65  WSD sqlite3Share
15046 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a  dCacheList = 0;.
15047 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42 74 53  #else.static BtS
15048 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 53  hared *SQLITE_WS
15049 44 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 43  D sqlite3SharedC
1504a 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65  acheList = 0;.#e
1504b 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ndif.#endif /* S
1504c 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1504d 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e  D_CACHE */..#ifn
1504e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1504f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a  SHARED_CACHE./*.
15050 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73  ** Enable or dis
15051 61 62 6c 65 20 74 68 65 20 73 68 61 72 65 64 20  able the shared 
15052 70 61 67 65 72 20 61 6e 64 20 73 63 68 65 6d 61  pager and schema
15053 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a   features..**.**
15054 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61   This routine ha
15055 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 65  s no effect on e
15056 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65  xisting database
15057 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a   connections..**
15058 20 54 68 65 20 73 68 61 72 65 64 20 63 61 63 68   The shared cach
15059 65 20 73 65 74 74 69 6e 67 20 65 66 66 65 63 74  e setting effect
1505a 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 63 61  s only future ca
1505b 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  lls to.** sqlite
1505c 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 74 65  3_open(), sqlite
1505d 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 20 73  3_open16(), or s
1505e 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
1505f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
15060 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62  int sqlite3_enab
15061 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28  le_shared_cache(
15062 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20 20 73  int enable){.  s
15063 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
15064 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 45 6e  ig.sharedCacheEn
15065 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a  abled = enable;.
15066 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15067 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 0a  OK;.}.#endif....
15068 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
15069 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
1506a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 75    /*.  ** The fu
1506b 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 53 68 61  nctions querySha
1506c 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
1506d 6b 28 29 2c 20 73 65 74 53 68 61 72 65 64 43 61  k(), setSharedCa
1506e 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 0a  cheTableLock(),.
1506f 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 41 6c    ** and clearAl
15070 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  lSharedCacheTabl
15071 65 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20 6d 61  eLocks().  ** ma
15072 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69 65 73  nipulate entries
15073 20 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64   in the BtShared
15074 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c 69  .pLock linked li
15075 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  st used to store
15076 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63  .  ** shared-cac
15077 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c 20 6c  he table level l
15078 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c 69 62  ocks. If the lib
15079 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64  rary is compiled
1507a 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73   with the.  ** s
1507b 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74  hared-cache feat
1507c 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74 68  ure disabled, th
1507d 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79  en there is only
1507e 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72 0a 20   ever one user. 
1507f 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74 53 68   ** of each BtSh
15080 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 61  ared structure a
15081 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63 6b 69  nd so this locki
15082 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  ng is not necess
15083 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20 64 65  ary. .  ** So de
15084 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20 72 65  fine the lock re
15085 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e 73 20  lated functions 
15086 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2f 0a  as no-ops..  */.
15087 20 20 23 64 65 66 69 6e 65 20 71 75 65 72 79 53    #define queryS
15088 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
15089 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54  ock(a,b,c) SQLIT
1508a 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 73  E_OK.  #define s
1508b 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62  etSharedCacheTab
1508c 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51  leLock(a,b,c) SQ
1508d 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e  LITE_OK.  #defin
1508e 65 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64  e clearAllShared
1508f 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28  CacheTableLocks(
15090 61 29 0a 20 20 23 64 65 66 69 6e 65 20 64 6f 77  a).  #define dow
15091 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43  ngradeAllSharedC
15092 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 61  acheTableLocks(a
15093 29 0a 20 20 23 64 65 66 69 6e 65 20 68 61 73 53  ).  #define hasS
15094 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
15095 6f 63 6b 28 61 2c 62 2c 63 2c 64 29 20 31 0a 20  ock(a,b,c,d) 1. 
15096 20 23 64 65 66 69 6e 65 20 68 61 73 52 65 61 64   #define hasRead
15097 43 6f 6e 66 6c 69 63 74 73 28 61 2c 20 62 29 20  Conflicts(a, b) 
15098 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  0.#endif..#ifnde
15099 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
1509a 41 52 45 44 5f 43 41 43 48 45 0a 0a 23 69 66 64  ARED_CACHE..#ifd
1509b 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1509c 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 73 20 66 75 6e  /*.**** This fun
1509d 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73  ction is only us
1509e 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e  ed as part of an
1509f 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
150a0 65 6e 74 2e 20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 43  ent. ***.**.** C
150a1 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 70  heck to see if p
150a2 42 74 72 65 65 20 68 6f 6c 64 73 20 74 68 65 20  Btree holds the 
150a3 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 74  required locks t
150a4 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20  o read or write 
150a5 74 6f 20 74 68 65 20 0a 2a 2a 20 74 61 62 6c 65  to the .** table
150a6 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20   with root page 
150a7 69 52 6f 6f 74 2e 20 20 20 52 65 74 75 72 6e 20  iRoot.   Return 
150a8 31 20 69 66 20 69 74 20 64 6f 65 73 20 61 6e 64  1 if it does and
150a9 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a   0 if not..**.**
150aa 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68   For example, wh
150ab 65 6e 20 77 72 69 74 69 6e 67 20 74 6f 20 61 20  en writing to a 
150ac 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d  table with root-
150ad 70 61 67 65 20 69 52 6f 6f 74 20 76 69 61 20 0a  page iRoot via .
150ae 2a 2a 20 42 74 72 65 65 20 63 6f 6e 6e 65 63 74  ** Btree connect
150af 69 6f 6e 20 70 42 74 72 65 65 3a 0a 2a 2a 0a 2a  ion pBtree:.**.*
150b0 2a 20 20 20 20 61 73 73 65 72 74 28 20 68 61 73  *    assert( has
150b1 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
150b2 4c 6f 63 6b 28 70 42 74 72 65 65 2c 20 69 52 6f  Lock(pBtree, iRo
150b3 6f 74 2c 20 30 2c 20 57 52 49 54 45 5f 4c 4f 43  ot, 0, WRITE_LOC
150b4 4b 29 20 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  K) );.**.** When
150b5 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 20 69   writing to an i
150b6 6e 64 65 78 20 74 68 61 74 20 72 65 73 69 64 65  ndex that reside
150b7 73 20 69 6e 20 61 20 73 68 61 72 61 62 6c 65 20  s in a sharable 
150b8 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 0a 2a  database, the .*
150b9 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20  * caller should 
150ba 68 61 76 65 20 66 69 72 73 74 20 6f 62 74 61 69  have first obtai
150bb 6e 65 64 20 61 20 6c 6f 63 6b 20 73 70 65 63 69  ned a lock speci
150bc 66 79 69 6e 67 20 74 68 65 20 72 6f 6f 74 20 70  fying the root p
150bd 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f  age of.** the co
150be 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c  rresponding tabl
150bf 65 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68  e. This makes th
150c0 69 6e 67 73 20 61 20 62 69 74 20 6d 6f 72 65 20  ings a bit more 
150c1 63 6f 6d 70 6c 69 63 61 74 65 64 2c 0a 2a 2a 20  complicated,.** 
150c2 61 73 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 74  as this module t
150c3 72 65 61 74 73 20 65 61 63 68 20 74 61 62 6c 65  reats each table
150c4 20 61 73 20 61 20 73 65 70 61 72 61 74 65 20 73   as a separate s
150c5 74 72 75 63 74 75 72 65 2e 20 54 6f 20 64 65 74  tructure. To det
150c6 65 72 6d 69 6e 65 0a 2a 2a 20 74 68 65 20 74 61  ermine.** the ta
150c7 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  ble correspondin
150c8 67 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 62  g to the index b
150c9 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 74 68  eing written, th
150ca 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68  is.** function h
150cb 61 73 20 74 6f 20 73 65 61 72 63 68 20 74 68 72  as to search thr
150cc 6f 75 67 68 20 74 68 65 20 64 61 74 61 62 61 73  ough the databas
150cd 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20  e schema..**.** 
150ce 49 6e 73 74 65 61 64 20 6f 66 20 61 20 6c 6f 63  Instead of a loc
150cf 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2f 69  k on the table/i
150d0 6e 64 65 78 20 72 6f 6f 74 65 64 20 61 74 20 70  ndex rooted at p
150d1 61 67 65 20 69 52 6f 6f 74 2c 20 74 68 65 20 63  age iRoot, the c
150d2 61 6c 6c 65 72 20 6d 61 79 0a 2a 2a 20 68 6f 6c  aller may.** hol
150d3 64 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f  d a write-lock o
150d4 6e 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62  n the schema tab
150d5 6c 65 20 28 72 6f 6f 74 20 70 61 67 65 20 31 29  le (root page 1)
150d6 2e 20 54 68 69 73 20 69 73 20 61 6c 73 6f 0a 2a  . This is also.*
150d7 2a 20 61 63 63 65 70 74 61 62 6c 65 2e 0a 2a 2f  * acceptable..*/
150d8 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 53  .static int hasS
150d9 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
150da 6f 63 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 42  ock(.  Btree *pB
150db 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  tree,         /*
150dc 20 48 61 6e 64 6c 65 20 74 68 61 74 20 6d 75 73   Handle that mus
150dd 74 20 68 6f 6c 64 20 6c 6f 63 6b 20 2a 2f 0a 20  t hold lock */. 
150de 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 20 20 20   Pgno iRoot,    
150df 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20          /* Root 
150e0 70 61 67 65 20 6f 66 20 62 2d 74 72 65 65 20 2a  page of b-tree *
150e1 2f 0a 20 20 69 6e 74 20 69 73 49 6e 64 65 78 2c  /.  int isIndex,
150e2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
150e3 75 65 20 69 66 20 69 52 6f 6f 74 20 69 73 20 74  ue if iRoot is t
150e4 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e 20 69 6e  he root of an in
150e5 64 65 78 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20  dex b-tree */.  
150e6 69 6e 74 20 65 4c 6f 63 6b 54 79 70 65 20 20 20  int eLockType   
150e7 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75 69 72         /* Requir
150e8 65 64 20 6c 6f 63 6b 20 74 79 70 65 20 28 52 45  ed lock type (RE
150e9 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45  AD_LOCK or WRITE
150ea 5f 4c 4f 43 4b 29 20 2a 2f 0a 29 7b 0a 20 20 53  _LOCK) */.){.  S
150eb 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
150ec 20 28 53 63 68 65 6d 61 20 2a 29 70 42 74 72 65   (Schema *)pBtre
150ed 65 2d 3e 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b  e->pBt->pSchema;
150ee 0a 20 20 50 67 6e 6f 20 69 54 61 62 20 3d 20 30  .  Pgno iTab = 0
150ef 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63  ;.  BtLock *pLoc
150f0 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  k;..  /* If this
150f1 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74   database is not
150f2 20 73 68 61 72 65 61 62 6c 65 2c 20 6f 72 20 69   shareable, or i
150f3 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20  f the client is 
150f4 72 65 61 64 69 6e 67 0a 20 20 2a 2a 20 61 6e 64  reading.  ** and
150f5 20 68 61 73 20 74 68 65 20 72 65 61 64 2d 75 6e   has the read-un
150f6 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 73  committed flag s
150f7 65 74 2c 20 74 68 65 6e 20 6e 6f 20 6c 6f 63 6b  et, then no lock
150f8 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 0a 20   is required. . 
150f9 20 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20   ** Return true 
150fa 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 2a  immediately..  *
150fb 2f 0a 20 20 69 66 28 20 28 70 42 74 72 65 65 2d  /.  if( (pBtree-
150fc 3e 73 68 61 72 61 62 6c 65 3d 3d 30 29 0a 20 20  >sharable==0).  
150fd 20 7c 7c 20 28 65 4c 6f 63 6b 54 79 70 65 3d 3d   || (eLockType==
150fe 52 45 41 44 5f 4c 4f 43 4b 20 26 26 20 28 70 42  READ_LOCK && (pB
150ff 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20  tree->db->flags 
15100 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63  & SQLITE_ReadUnc
15101 6f 6d 6d 69 74 74 65 64 29 29 0a 20 20 29 7b 0a  ommitted)).  ){.
15102 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
15103 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63  }..  /* If the c
15104 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67  lient is reading
15105 20 20 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 20    or writing an 
15106 69 6e 64 65 78 20 61 6e 64 20 74 68 65 20 73 63  index and the sc
15107 68 65 6d 61 20 69 73 0a 20 20 2a 2a 20 6e 6f 74  hema is.  ** not
15108 20 6c 6f 61 64 65 64 2c 20 74 68 65 6e 20 69 74   loaded, then it
15109 20 69 73 20 74 6f 6f 20 64 69 66 66 69 63 75 6c   is too difficul
1510a 74 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 63 68  t to actually ch
1510b 65 63 6b 20 74 6f 20 73 65 65 20 69 66 0a 20 20  eck to see if.  
1510c 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6c  ** the correct l
1510d 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 20 20  ocks are held.  
1510e 53 6f 20 64 6f 20 6e 6f 74 20 62 6f 74 68 65 72  So do not bother
1510f 20 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74   - just return t
15110 72 75 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 63  rue..  ** This c
15111 61 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6d  ase does not com
15112 65 20 75 70 20 76 65 72 79 20 6f 66 74 65 6e 20  e up very often 
15113 61 6e 79 68 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69  anyhow..  */.  i
15114 66 28 20 69 73 49 6e 64 65 78 20 26 26 20 28 21  f( isIndex && (!
15115 70 53 63 68 65 6d 61 20 7c 7c 20 28 70 53 63 68  pSchema || (pSch
15116 65 6d 61 2d 3e 66 6c 61 67 73 26 44 42 5f 53 63  ema->flags&DB_Sc
15117 68 65 6d 61 4c 6f 61 64 65 64 29 3d 3d 30 29 20  hemaLoaded)==0) 
15118 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
15119 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72  .  }..  /* Figur
1511a 65 20 6f 75 74 20 74 68 65 20 72 6f 6f 74 2d 70  e out the root-p
1511b 61 67 65 20 74 68 61 74 20 74 68 65 20 6c 6f 63  age that the loc
1511c 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64  k should be held
1511d 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c 65 0a 20   on. For table. 
1511e 20 2a 2a 20 62 2d 74 72 65 65 73 2c 20 74 68 69   ** b-trees, thi
1511f 73 20 69 73 20 6a 75 73 74 20 74 68 65 20 72 6f  s is just the ro
15120 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62  ot page of the b
15121 2d 74 72 65 65 20 62 65 69 6e 67 20 72 65 61 64  -tree being read
15122 20 6f 72 0a 20 20 2a 2a 20 77 72 69 74 74 65 6e   or.  ** written
15123 2e 20 46 6f 72 20 69 6e 64 65 78 20 62 2d 74 72  . For index b-tr
15124 65 65 73 2c 20 69 74 20 69 73 20 74 68 65 20 72  ees, it is the r
15125 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  oot page of the 
15126 61 73 73 6f 63 69 61 74 65 64 0a 20 20 2a 2a 20  associated.  ** 
15127 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28  table.  */.  if(
15128 20 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20   isIndex ){.    
15129 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20  HashElem *p;.   
1512a 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73   for(p=sqliteHas
1512b 68 46 69 72 73 74 28 26 70 53 63 68 65 6d 61 2d  hFirst(&pSchema-
1512c 3e 69 64 78 48 61 73 68 29 3b 20 70 3b 20 70 3d  >idxHash); p; p=
1512d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
1512e 29 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20  )){.      Index 
1512f 2a 70 49 64 78 20 3d 20 28 49 6e 64 65 78 20 2a  *pIdx = (Index *
15130 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28  )sqliteHashData(
15131 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  p);.      if( pI
15132 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 6e 74 29 69  dx->tnum==(int)i
15133 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Root ){.        
15134 69 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61  iTab = pIdx->pTa
15135 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20  ble->tnum;.     
15136 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
15137 7b 0a 20 20 20 20 69 54 61 62 20 3d 20 69 52 6f  {.    iTab = iRo
15138 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  ot;.  }..  /* Se
15139 61 72 63 68 20 66 6f 72 20 74 68 65 20 72 65 71  arch for the req
1513a 75 69 72 65 64 20 6c 6f 63 6b 2e 20 45 69 74 68  uired lock. Eith
1513b 65 72 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  er a write-lock 
1513c 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61  on root-page iTa
1513d 62 2c 20 61 20 0a 20 20 2a 2a 20 77 72 69 74 65  b, a .  ** write
1513e 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 63 68  -lock on the sch
1513f 65 6d 61 20 74 61 62 6c 65 2c 20 6f 72 20 28 69  ema table, or (i
15140 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20  f the client is 
15141 72 65 61 64 69 6e 67 29 20 61 0a 20 20 2a 2a 20  reading) a.  ** 
15142 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 69 54 61  read-lock on iTa
15143 62 20 77 69 6c 6c 20 73 75 66 66 69 63 65 2e 20  b will suffice. 
15144 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79 20  Return 1 if any 
15145 6f 66 20 74 68 65 73 65 20 61 72 65 20 66 6f 75  of these are fou
15146 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c  nd.  */.  for(pL
15147 6f 63 6b 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d  ock=pBtree->pBt-
15148 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70  >pLock; pLock; p
15149 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78  Lock=pLock->pNex
1514a 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63  t){.    if( pLoc
1514b 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 42 74 72 65  k->pBtree==pBtre
1514c 65 20 0a 20 20 20 20 20 26 26 20 28 70 4c 6f 63  e .     && (pLoc
1514d 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20  k->iTable==iTab 
1514e 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b  || (pLock->eLock
1514f 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 26 26 20  ==WRITE_LOCK && 
15150 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 31  pLock->iTable==1
15151 29 29 0a 20 20 20 20 20 26 26 20 70 4c 6f 63 6b  )).     && pLock
15152 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 54 79  ->eLock>=eLockTy
15153 70 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  pe .    ){.     
15154 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
15155 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 61 69 6c 65  .  }..  /* Faile
15156 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 72 65  d to find the re
15157 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a  quired lock. */.
15158 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65    return 0;.}.#e
15159 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44  ndif /* SQLITE_D
1515a 45 42 55 47 20 2a 2f 0a 0a 23 69 66 64 65 66 20  EBUG */..#ifdef 
1515b 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
1515c 2a 2a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  **** This functi
1515d 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 20 61  on may be used a
1515e 73 20 70 61 72 74 20 6f 66 20 61 73 73 65 72 74  s part of assert
1515f 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e  () statements on
15160 6c 79 2e 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52  ly. ****.**.** R
15161 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 74  eturn true if it
15162 20 77 6f 75 6c 64 20 62 65 20 69 6c 6c 65 67 61   would be illega
15163 6c 20 66 6f 72 20 70 42 74 72 65 65 20 74 6f 20  l for pBtree to 
15164 77 72 69 74 65 20 69 6e 74 6f 20 74 68 65 0a 2a  write into the.*
15165 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  * table or index
15166 20 72 6f 6f 74 65 64 20 61 74 20 69 52 6f 6f 74   rooted at iRoot
15167 20 62 65 63 61 75 73 65 20 6f 74 68 65 72 20 73   because other s
15168 68 61 72 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  hared connection
15169 73 20 61 72 65 0a 2a 2a 20 73 69 6d 75 6c 74 61  s are.** simulta
1516a 6e 65 6f 75 73 6c 79 20 72 65 61 64 69 6e 67 20  neously reading 
1516b 74 68 61 74 20 73 61 6d 65 20 74 61 62 6c 65 20  that same table 
1516c 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  or index..**.** 
1516d 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f  It is illegal fo
1516e 72 20 70 42 74 72 65 65 20 74 6f 20 77 72 69 74  r pBtree to writ
1516f 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  e if some other 
15170 42 74 72 65 65 20 6f 62 6a 65 63 74 20 74 68 61  Btree object tha
15171 74 0a 2a 2a 20 73 68 61 72 65 73 20 74 68 65 20  t.** shares the 
15172 73 61 6d 65 20 42 74 53 68 61 72 65 64 20 6f 62  same BtShared ob
15173 6a 65 63 74 20 69 73 20 63 75 72 72 65 6e 74 6c  ject is currentl
15174 79 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69  y reading or wri
15175 74 69 6e 67 0a 2a 2a 20 74 68 65 20 69 52 6f 6f  ting.** the iRoo
15176 74 20 74 61 62 6c 65 2e 20 20 45 78 63 65 70 74  t table.  Except
15177 2c 20 69 66 20 74 68 65 20 6f 74 68 65 72 20 42  , if the other B
15178 74 72 65 65 20 6f 62 6a 65 63 74 20 68 61 73 20  tree object has 
15179 74 68 65 0a 2a 2a 20 72 65 61 64 2d 75 6e 63 6f  the.** read-unco
1517a 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74  mmitted flag set
1517b 2c 20 74 68 65 6e 20 69 74 20 69 73 20 4f 4b 20  , then it is OK 
1517c 66 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f 62  for the other ob
1517d 6a 65 63 74 20 74 6f 0a 2a 2a 20 68 61 76 65 20  ject to.** have 
1517e 61 20 72 65 61 64 20 63 75 72 73 6f 72 2e 0a 2a  a read cursor..*
1517f 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65  *.** For example
15180 2c 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67  , before writing
15181 20 74 6f 20 61 6e 79 20 70 61 72 74 20 6f 66 20   to any part of 
15182 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  the table or ind
15183 65 78 0a 2a 2a 20 72 6f 6f 74 65 64 20 61 74 20  ex.** rooted at 
15184 70 61 67 65 20 69 52 6f 6f 74 2c 20 6f 6e 65 20  page iRoot, one 
15185 73 68 6f 75 6c 64 20 63 61 6c 6c 3a 0a 2a 2a 0a  should call:.**.
15186 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 21 68  **    assert( !h
15187 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28  asReadConflicts(
15188 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 29 20 29  pBtree, iRoot) )
15189 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.*/.static int 
1518a 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73  hasReadConflicts
1518b 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20  (Btree *pBtree, 
1518c 50 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a 20 20 42  Pgno iRoot){.  B
1518d 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f  tCursor *p;.  fo
1518e 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d  r(p=pBtree->pBt-
1518f 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70  >pCursor; p; p=p
15190 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66  ->pNext){.    if
15191 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69  ( p->pgnoRoot==i
15192 52 6f 6f 74 20 0a 20 20 20 20 20 26 26 20 70 2d  Root .     && p-
15193 3e 70 42 74 72 65 65 21 3d 70 42 74 72 65 65 0a  >pBtree!=pBtree.
15194 20 20 20 20 20 26 26 20 30 3d 3d 28 70 2d 3e 70       && 0==(p->p
15195 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73  Btree->db->flags
15196 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e   & SQLITE_ReadUn
15197 63 6f 6d 6d 69 74 74 65 64 29 0a 20 20 20 20 29  committed).    )
15198 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
15199 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1519a 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66  turn 0;.}.#endif
1519b 20 20 20 20 2f 2a 20 23 69 66 64 65 66 20 53 51      /* #ifdef SQ
1519c 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f  LITE_DEBUG */../
1519d 2a 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65  *.** Query to se
1519e 65 20 69 66 20 42 74 72 65 65 20 68 61 6e 64 6c  e if Btree handl
1519f 65 20 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61  e p may obtain a
151a0 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c   lock of type eL
151a1 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f  ock .** (READ_LO
151a2 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b  CK or WRITE_LOCK
151a3 29 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77  ) on the table w
151a4 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54  ith root-page iT
151a5 61 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51  ab. Return.** SQ
151a6 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c  LITE_OK if the l
151a7 6f 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69  ock may be obtai
151a8 6e 65 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 0a  ned (by calling.
151a9 2a 2a 20 73 65 74 53 68 61 72 65 64 43 61 63 68  ** setSharedCach
151aa 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f  eTableLock()), o
151ab 72 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  r SQLITE_LOCKED 
151ac 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69  if not..*/.stati
151ad 63 20 69 6e 74 20 71 75 65 72 79 53 68 61 72 65  c int queryShare
151ae 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
151af 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69  Btree *p, Pgno i
151b0 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a  Tab, u8 eLock){.
151b1 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
151b2 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f  = p->pBt;.  BtLo
151b3 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73  ck *pIter;..  as
151b4 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
151b5 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20  eeHoldsMutex(p) 
151b6 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f  );.  assert( eLo
151b7 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c  ck==READ_LOCK ||
151b8 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f   eLock==WRITE_LO
151b9 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
151ba 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 61 73  p->db!=0 );.  as
151bb 73 65 72 74 28 20 21 28 70 2d 3e 64 62 2d 3e 66  sert( !(p->db->f
151bc 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64  lags&SQLITE_Read
151bd 55 6e 63 6f 6d 6d 69 74 74 65 64 29 7c 7c 65 4c  Uncommitted)||eL
151be 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 7c  ock==WRITE_LOCK|
151bf 7c 69 54 61 62 3d 3d 31 20 29 3b 0a 20 20 0a 20  |iTab==1 );.  . 
151c0 20 2f 2a 20 49 66 20 72 65 71 75 65 73 74 69 6e   /* If requestin
151c1 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20  g a write-lock, 
151c2 74 68 65 6e 20 74 68 65 20 42 74 72 65 65 20 6d  then the Btree m
151c3 75 73 74 20 68 61 76 65 20 61 6e 20 6f 70 65 6e  ust have an open
151c4 20 77 72 69 74 65 0a 20 20 2a 2a 20 74 72 61 6e   write.  ** tran
151c5 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20  saction on this 
151c6 66 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 76 69 6f  file. And, obvio
151c7 75 73 6c 79 2c 20 66 6f 72 20 74 68 69 73 20 74  usly, for this t
151c8 6f 20 62 65 20 73 6f 20 74 68 65 72 65 20 0a 20  o be so there . 
151c9 20 2a 2a 20 6d 75 73 74 20 62 65 20 61 6e 20 6f   ** must be an o
151ca 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61  pen write transa
151cb 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 66 69 6c  ction on the fil
151cc 65 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20  e itself..  */. 
151cd 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d   assert( eLock==
151ce 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d  READ_LOCK || (p=
151cf 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 26 26  =pBt->pWriter &&
151d0 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
151d1 4e 53 5f 57 52 49 54 45 29 20 29 3b 0a 20 20 61  NS_WRITE) );.  a
151d2 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45  ssert( eLock==RE
151d3 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e  AD_LOCK || pBt->
151d4 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
151d5 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20  RANS_WRITE );.  
151d6 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69  .  /* This routi
151d7 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  ne is a no-op if
151d8 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68   the shared-cach
151d9 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64  e is not enabled
151da 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68   */.  if( !p->sh
151db 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65  arable ){.    re
151dc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
151dd 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d    }..  /* If som
151de 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  e other connecti
151df 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 6e  on is holding an
151e0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c   exclusive lock,
151e1 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 75 65 73   the.  ** reques
151e2 74 65 64 20 6c 6f 63 6b 20 6d 61 79 20 6e 6f 74  ted lock may not
151e3 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 20 20   be obtained..  
151e4 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57  */.  if( pBt->pW
151e5 72 69 74 65 72 21 3d 70 20 26 26 20 70 42 74 2d  riter!=p && pBt-
151e6 3e 69 73 45 78 63 6c 75 73 69 76 65 20 29 7b 0a  >isExclusive ){.
151e7 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65      sqlite3Conne
151e8 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e  ctionBlocked(p->
151e9 64 62 2c 20 70 42 74 2d 3e 70 57 72 69 74 65 72  db, pBt->pWriter
151ea 2d 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72  ->db);.    retur
151eb 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f  n SQLITE_LOCKED_
151ec 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d  SHAREDCACHE;.  }
151ed 0a 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42  ..  for(pIter=pB
151ee 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b  t->pLock; pIter;
151ef 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e   pIter=pIter->pN
151f0 65 78 74 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  ext){.    /* The
151f1 20 63 6f 6e 64 69 74 69 6f 6e 20 28 70 49 74 65   condition (pIte
151f2 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 29  r->eLock!=eLock)
151f3 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   in the followin
151f4 67 20 69 66 28 2e 2e 2e 29 20 0a 20 20 20 20 2a  g if(...) .    *
151f5 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61  * statement is a
151f6 20 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 20   simplification 
151f7 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  of:.    **.    *
151f8 2a 20 20 20 28 65 4c 6f 63 6b 3d 3d 57 52 49 54  *   (eLock==WRIT
151f9 45 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d  E_LOCK || pIter-
151fa 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f  >eLock==WRITE_LO
151fb 43 4b 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  CK).    **.    *
151fc 2a 20 73 69 6e 63 65 20 77 65 20 6b 6e 6f 77 20  * since we know 
151fd 74 68 61 74 20 69 66 20 65 4c 6f 63 6b 3d 3d 57  that if eLock==W
151fe 52 49 54 45 5f 4c 4f 43 4b 2c 20 74 68 65 6e 20  RITE_LOCK, then 
151ff 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74  no other connect
15200 69 6f 6e 0a 20 20 20 20 2a 2a 20 6d 61 79 20 68  ion.    ** may h
15201 6f 6c 64 20 61 20 57 52 49 54 45 5f 4c 4f 43 4b  old a WRITE_LOCK
15202 20 6f 6e 20 61 6e 79 20 74 61 62 6c 65 20 69 6e   on any table in
15203 20 74 68 69 73 20 66 69 6c 65 20 28 73 69 6e 63   this file (sinc
15204 65 20 74 68 65 72 65 20 63 61 6e 0a 20 20 20 20  e there can.    
15205 2a 2a 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e  ** only be a sin
15206 67 6c 65 20 77 72 69 74 65 72 29 2e 0a 20 20 20  gle writer)..   
15207 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
15208 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45  pIter->eLock==RE
15209 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72  AD_LOCK || pIter
1520a 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c  ->eLock==WRITE_L
1520b 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72  OCK );.    asser
1520c 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c  t( eLock==READ_L
1520d 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 70 42  OCK || pIter->pB
1520e 74 72 65 65 3d 3d 70 20 7c 7c 20 70 49 74 65 72  tree==p || pIter
1520f 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f  ->eLock==READ_LO
15210 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 70 49 74  CK);.    if( pIt
15211 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 26 26  er->pBtree!=p &&
15212 20 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d   pIter->iTable==
15213 69 54 61 62 20 26 26 20 70 49 74 65 72 2d 3e 65  iTab && pIter->e
15214 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 20 29 7b 0a 20  Lock!=eLock ){. 
15215 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e       sqlite3Conn
15216 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d  ectionBlocked(p-
15217 3e 64 62 2c 20 70 49 74 65 72 2d 3e 70 42 74 72  >db, pIter->pBtr
15218 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 69  ee->db);.      i
15219 66 28 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f  f( eLock==WRITE_
1521a 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  LOCK ){.        
1521b 61 73 73 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e  assert( p==pBt->
1521c 70 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20  pWriter );.     
1521d 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e     pBt->isPendin
1521e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  g = 1;.      }. 
1521f 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
15220 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
15221 43 41 43 48 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  CACHE;.    }.  }
15222 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15223 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  _OK;.}.#endif /*
15224 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48   !SQLITE_OMIT_SH
15225 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23  ARED_CACHE */..#
15226 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
15227 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
15228 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b  /*.** Add a lock
15229 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69   on the table wi
1522a 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61  th root-page iTa
1522b 62 6c 65 20 74 6f 20 74 68 65 20 73 68 61 72 65  ble to the share
1522c 64 2d 62 74 72 65 65 20 75 73 65 64 0a 2a 2a 20  d-btree used.** 
1522d 62 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20  by Btree handle 
1522e 70 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4c 6f  p. Parameter eLo
1522f 63 6b 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ck must be eithe
15230 72 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a  r READ_LOCK or .
15231 2a 2a 20 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a  ** WRITE_LOCK..*
15232 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
15233 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 66  on assumes the f
15234 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
15235 20 20 28 61 29 20 54 68 65 20 73 70 65 63 69 66    (a) The specif
15236 69 65 64 20 42 74 72 65 65 20 6f 62 6a 65 63 74  ied Btree object
15237 20 70 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20   p is connected 
15238 74 6f 20 61 20 73 68 61 72 61 62 6c 65 0a 2a 2a  to a sharable.**
15239 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20         database 
1523a 28 6f 6e 65 20 77 69 74 68 20 74 68 65 20 42 74  (one with the Bt
1523b 53 68 61 72 65 64 2e 73 68 61 72 61 62 6c 65 20  Shared.sharable 
1523c 66 6c 61 67 20 73 65 74 29 2c 20 61 6e 64 0a 2a  flag set), and.*
1523d 2a 0a 2a 2a 20 20 20 28 62 29 20 4e 6f 20 6f 74  *.**   (b) No ot
1523e 68 65 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74  her Btree object
1523f 73 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 74 68  s hold a lock th
15240 61 74 20 63 6f 6e 66 6c 69 63 74 73 0a 2a 2a 20  at conflicts.** 
15241 20 20 20 20 20 20 77 69 74 68 20 74 68 65 20 72        with the r
15242 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 28 69  equested lock (i
15243 2e 65 2e 20 71 75 65 72 79 53 68 61 72 65 64 43  .e. querySharedC
15244 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20  acheTableLock() 
15245 68 61 73 0a 2a 2a 20 20 20 20 20 20 20 61 6c 72  has.**       alr
15246 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64  eady been called
15247 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 53 51   and returned SQ
15248 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20  LITE_OK)..**.** 
15249 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
1524a 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63  urned if the loc
1524b 6b 20 69 73 20 61 64 64 65 64 20 73 75 63 63 65  k is added succe
1524c 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f  ssfully. SQLITE_
1524d 4e 4f 4d 45 4d 20 0a 2a 2a 20 69 73 20 72 65 74  NOMEM .** is ret
1524e 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f  urned if a mallo
1524f 63 20 61 74 74 65 6d 70 74 20 66 61 69 6c 73 2e  c attempt fails.
15250 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
15251 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62  etSharedCacheTab
15252 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c  leLock(Btree *p,
15253 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38   Pgno iTable, u8
15254 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61   eLock){.  BtSha
15255 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
15256 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f  t;.  BtLock *pLo
15257 63 6b 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b  ck = 0;.  BtLock
15258 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65   *pIter;..  asse
15259 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
1525a 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b  HoldsMutex(p) );
1525b 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b  .  assert( eLock
1525c 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65  ==READ_LOCK || e
1525d 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
1525e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1525f 3e 64 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  >db!=0 );..  /* 
15260 41 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74  A connection wit
15261 68 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d  h the read-uncom
15262 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74 20  mitted flag set 
15263 77 69 6c 6c 20 6e 65 76 65 72 20 74 72 79 20 74  will never try t
15264 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 20 61 20  o.  ** obtain a 
15265 72 65 61 64 2d 6c 6f 63 6b 20 75 73 69 6e 67 20  read-lock using 
15266 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54  this function. T
15267 68 65 20 6f 6e 6c 79 20 72 65 61 64 2d 6c 6f 63  he only read-loc
15268 6b 20 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20  k obtained.  ** 
15269 62 79 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  by a connection 
1526a 69 6e 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74  in read-uncommit
1526b 74 65 64 20 6d 6f 64 65 20 69 73 20 6f 6e 20 74  ted mode is on t
1526c 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
1526d 20 0a 20 20 2a 2a 20 74 61 62 6c 65 2c 20 61 6e   .  ** table, an
1526e 64 20 74 68 61 74 20 6c 6f 63 6b 20 69 73 20 6f  d that lock is o
1526f 62 74 61 69 6e 65 64 20 69 6e 20 42 74 72 65 65  btained in Btree
15270 42 65 67 69 6e 54 72 61 6e 73 28 29 2e 20 20 2a  BeginTrans().  *
15271 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28  /.  assert( 0==(
15272 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  p->db->flags&SQL
15273 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74  ITE_ReadUncommit
15274 74 65 64 29 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57  ted) || eLock==W
15275 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20  RITE_LOCK );..  
15276 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
15277 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20   should only be 
15278 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72  called on a shar
15279 61 62 6c 65 20 62 2d 74 72 65 65 20 61 66 74 65  able b-tree afte
1527a 72 20 69 74 20 0a 20 20 2a 2a 20 68 61 73 20 62  r it .  ** has b
1527b 65 65 6e 20 64 65 74 65 72 6d 69 6e 65 64 20 74  een determined t
1527c 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 62 2d 74  hat no other b-t
1527d 72 65 65 20 68 6f 6c 64 73 20 61 20 63 6f 6e 66  ree holds a conf
1527e 6c 69 63 74 69 6e 67 20 6c 6f 63 6b 2e 20 20 2a  licting lock.  *
1527f 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73  /.  assert( p->s
15280 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 61 73 73  harable );.  ass
15281 65 72 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d  ert( SQLITE_OK==
15282 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65  querySharedCache
15283 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61  TableLock(p, iTa
15284 62 6c 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a  ble, eLock) );..
15285 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63    /* First searc
15286 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61  h the list for a
15287 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20  n existing lock 
15288 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a  on this table. *
15289 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42  /.  for(pIter=pB
1528a 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b  t->pLock; pIter;
1528b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e   pIter=pIter->pN
1528c 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49  ext){.    if( pI
1528d 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61  ter->iTable==iTa
1528e 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42  ble && pIter->pB
1528f 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20  tree==p ){.     
15290 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a   pLock = pIter;.
15291 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
15292 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
15293 74 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68  the above search
15294 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20   did not find a 
15295 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73  BtLock struct as
15296 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20  sociating Btree 
15297 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c  p.  ** with tabl
15298 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61  e iTable, alloca
15299 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20  te one and link 
1529a 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74  it into the list
1529b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c  ..  */.  if( !pL
1529c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b  ock ){.    pLock
1529d 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c   = (BtLock *)sql
1529e 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73  ite3MallocZero(s
1529f 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a  izeof(BtLock));.
152a0 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29      if( !pLock )
152a1 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
152a2 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
152a3 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54   }.    pLock->iT
152a4 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20  able = iTable;. 
152a5 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65     pLock->pBtree
152a6 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d   = p;.    pLock-
152a7 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c  >pNext = pBt->pL
152a8 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c  ock;.    pBt->pL
152a9 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d  ock = pLock;.  }
152aa 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42  ..  /* Set the B
152ab 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69  tLock.eLock vari
152ac 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69  able to the maxi
152ad 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65  mum of the curre
152ae 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64  nt lock.  ** and
152af 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
152b0 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  ock. This means 
152b1 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  if a write-lock 
152b2 77 61 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64  was already held
152b3 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64  .  ** and a read
152b4 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c  -lock requested,
152b5 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72   we don't incorr
152b6 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20  ectly downgrade 
152b7 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20  the lock..  */. 
152b8 20 61 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c   assert( WRITE_L
152b9 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b  OCK>READ_LOCK );
152ba 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f  .  if( eLock>pLo
152bb 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20  ck->eLock ){.   
152bc 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20   pLock->eLock = 
152bd 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65  eLock;.  }..  re
152be 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
152bf 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
152c0 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
152c1 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  CACHE */..#ifnde
152c2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
152c3 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
152c4 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65   Release all the
152c5 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f   table locks (lo
152c6 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61  cks obtained via
152c7 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74 68 65   calls to.** the
152c8 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54   setSharedCacheT
152c9 61 62 6c 65 4c 6f 63 6b 28 29 20 70 72 6f 63 65  ableLock() proce
152ca 64 75 72 65 29 20 68 65 6c 64 20 62 79 20 42 74  dure) held by Bt
152cb 72 65 65 20 6f 62 6a 65 63 74 20 70 2e 0a 2a 2a  ree object p..**
152cc 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
152cd 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 42  n assumes that B
152ce 74 72 65 65 20 70 20 68 61 73 20 61 6e 20 6f 70  tree p has an op
152cf 65 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65  en read or write
152d0 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e   .** transaction
152d1 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74  . If it does not
152d2 2c 20 74 68 65 6e 20 74 68 65 20 42 74 53 68 61  , then the BtSha
152d3 72 65 64 2e 69 73 50 65 6e 64 69 6e 67 20 76 61  red.isPending va
152d4 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 79 20 62 65  riable.** may be
152d5 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 63 6c 65   incorrectly cle
152d6 61 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ared..*/.static 
152d7 76 6f 69 64 20 63 6c 65 61 72 41 6c 6c 53 68 61  void clearAllSha
152d8 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
152d9 6b 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  ks(Btree *p){.  
152da 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
152db 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b  p->pBt;.  BtLock
152dc 20 2a 2a 70 70 49 74 65 72 20 3d 20 26 70 42 74   **ppIter = &pBt
152dd 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 61 73 73 65  ->pLock;..  asse
152de 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
152df 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b  HoldsMutex(p) );
152e0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68  .  assert( p->sh
152e1 61 72 61 62 6c 65 20 7c 7c 20 30 3d 3d 2a 70 70  arable || 0==*pp
152e2 49 74 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74  Iter );.  assert
152e3 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 30 20 29  ( p->inTrans>0 )
152e4 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a 70 70 49  ;..  while( *ppI
152e5 74 65 72 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63  ter ){.    BtLoc
152e6 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70 70 49 74  k *pLock = *ppIt
152e7 65 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  er;.    assert( 
152e8 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65  pBt->isExclusive
152e9 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69  ==0 || pBt->pWri
152ea 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e 70 42 74 72  ter==pLock->pBtr
152eb 65 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ee );.    assert
152ec 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 2d  ( pLock->pBtree-
152ed 3e 69 6e 54 72 61 6e 73 3e 3d 70 4c 6f 63 6b 2d  >inTrans>=pLock-
152ee 3e 65 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 69 66  >eLock );.    if
152ef 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d  ( pLock->pBtree=
152f0 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 49  =p ){.      *ppI
152f1 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65  ter = pLock->pNe
152f2 78 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  xt;.      assert
152f3 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 21  ( pLock->iTable!
152f4 3d 31 20 7c 7c 20 70 4c 6f 63 6b 3d 3d 26 70 2d  =1 || pLock==&p-
152f5 3e 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 69  >lock );.      i
152f6 66 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65  f( pLock->iTable
152f7 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73  !=1 ){.        s
152f8 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f 63  qlite3_free(pLoc
152f9 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  k);.      }.    
152fa 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 70 49  }else{.      ppI
152fb 74 65 72 20 3d 20 26 70 4c 6f 63 6b 2d 3e 70 4e  ter = &pLock->pN
152fc 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ext;.    }.  }..
152fd 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69    assert( pBt->i
152fe 73 50 65 6e 64 69 6e 67 3d 3d 30 20 7c 7c 20 70  sPending==0 || p
152ff 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20  Bt->pWriter );. 
15300 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65   if( pBt->pWrite
15301 72 3d 3d 70 20 29 7b 0a 20 20 20 20 70 42 74 2d  r==p ){.    pBt-
15302 3e 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20  >pWriter = 0;.  
15303 20 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69    pBt->isExclusi
15304 76 65 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d  ve = 0;.    pBt-
15305 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a  >isPending = 0;.
15306 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 74 2d    }else if( pBt-
15307 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 32  >nTransaction==2
15308 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
15309 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1530a 65 64 20 77 68 65 6e 20 42 74 72 65 65 20 70 20  ed when Btree p 
1530b 69 73 20 63 6f 6e 63 6c 75 64 69 6e 67 20 69 74  is concluding it
1530c 73 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61  s .    ** transa
1530d 63 74 69 6f 6e 2e 20 49 66 20 74 68 65 72 65 20  ction. If there 
1530e 63 75 72 72 65 6e 74 6c 79 20 65 78 69 73 74 73  currently exists
1530f 20 61 20 77 72 69 74 65 72 2c 20 61 6e 64 20 70   a writer, and p
15310 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74   is not.    ** t
15311 68 61 74 20 77 72 69 74 65 72 2c 20 74 68 65 6e  hat writer, then
15312 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c   the number of l
15313 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 63 6f 6e  ocks held by con
15314 6e 65 63 74 69 6f 6e 73 20 6f 74 68 65 72 0a 20  nections other. 
15315 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65 20 77     ** than the w
15316 72 69 74 65 72 20 6d 75 73 74 20 62 65 20 61 62  riter must be ab
15317 6f 75 74 20 74 6f 20 64 72 6f 70 20 74 6f 20 7a  out to drop to z
15318 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ero. In this cas
15319 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68 65  e.    ** set the
1531a 20 69 73 50 65 6e 64 69 6e 67 20 66 6c 61 67 20   isPending flag 
1531b 74 6f 20 30 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  to 0..    **.   
1531c 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   ** If there is 
1531d 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 20  not currently a 
1531e 77 72 69 74 65 72 2c 20 74 68 65 6e 20 42 74 53  writer, then BtS
1531f 68 61 72 65 64 2e 69 73 50 65 6e 64 69 6e 67 20  hared.isPending 
15320 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a  must.    ** be z
15321 65 72 6f 20 61 6c 72 65 61 64 79 2e 20 53 6f 20  ero already. So 
15322 74 68 69 73 20 6e 65 78 74 20 6c 69 6e 65 20 69  this next line i
15323 73 20 68 61 72 6d 6c 65 73 73 20 69 6e 20 74 68  s harmless in th
15324 61 74 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a  at case..    */.
15325 20 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69      pBt->isPendi
15326 6e 67 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ng = 0;.  }.}../
15327 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
15328 6f 6e 20 63 68 61 6e 67 65 73 20 61 6c 6c 20 77  on changes all w
15329 72 69 74 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20  rite-locks held 
1532a 62 79 20 42 74 72 65 65 20 70 20 69 6e 74 6f 20  by Btree p into 
1532b 72 65 61 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73  read-locks..*/.s
1532c 74 61 74 69 63 20 76 6f 69 64 20 64 6f 77 6e 67  tatic void downg
1532d 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 63  radeAllSharedCac
1532e 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72  heTableLocks(Btr
1532f 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72  ee *p){.  BtShar
15330 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
15331 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57 72  ;.  if( pBt->pWr
15332 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20 42  iter==p ){.    B
15333 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 20  tLock *pLock;.  
15334 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d    pBt->pWriter =
15335 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 45   0;.    pBt->isE
15336 78 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20 20  xclusive = 0;.  
15337 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67    pBt->isPending
15338 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 4c   = 0;.    for(pL
15339 6f 63 6b 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20  ock=pBt->pLock; 
1533a 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f  pLock; pLock=pLo
1533b 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  ck->pNext){.    
1533c 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d    assert( pLock-
1533d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43  >eLock==READ_LOC
1533e 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 72  K || pLock->pBtr
1533f 65 65 3d 3d 70 20 29 3b 0a 20 20 20 20 20 20 70  ee==p );.      p
15340 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 45  Lock->eLock = RE
15341 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20  AD_LOCK;.    }. 
15342 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20   }.}..#endif /* 
15343 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
15344 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 73 74 61  ED_CACHE */..sta
15345 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65  tic void release
15346 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Page(MemPage *pP
15347 61 67 65 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72  age);  /* Forwar
15348 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 0a  d reference */..
15349 2f 2a 0a 2a 2a 2a 2a 2a 20 54 68 69 73 20 72 6f  /*.***** This ro
1534a 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e  utine is used in
1534b 73 69 64 65 20 6f 66 20 61 73 73 65 72 74 28 29  side of assert()
1534c 20 6f 6e 6c 79 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a   only ****.**.**
1534d 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65   Verify that the
1534e 20 63 75 72 73 6f 72 20 68 6f 6c 64 73 20 74 68   cursor holds th
1534f 65 20 6d 75 74 65 78 20 6f 6e 20 69 74 73 20 42  e mutex on its B
15350 74 53 68 61 72 65 64 0a 2a 2f 0a 23 69 66 64 65  tShared.*/.#ifde
15351 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73  f SQLITE_DEBUG.s
15352 74 61 74 69 63 20 69 6e 74 20 63 75 72 73 6f 72  tatic int cursor
15353 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 43 75 72  HoldsMutex(BtCur
15354 73 6f 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  sor *p){.  retur
15355 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  n sqlite3_mutex_
15356 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74  held(p->pBt->mut
15357 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  ex);.}.#endif...
15358 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
15359 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a  MIT_INCRBLOB./*.
1535a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68  ** Invalidate th
1535b 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  e overflow page-
1535c 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 63  list cache for c
1535d 75 72 73 6f 72 20 70 43 75 72 2c 20 69 66 20 61  ursor pCur, if a
1535e 6e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ny..*/.static vo
1535f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65  id invalidateOve
15360 72 66 6c 6f 77 43 61 63 68 65 28 42 74 43 75 72  rflowCache(BtCur
15361 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73  sor *pCur){.  as
15362 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
15363 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
15364 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
15365 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 3b  Cur->aOverflow);
15366 0a 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  .  pCur->aOverfl
15367 6f 77 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ow = 0;.}../*.**
15368 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20   Invalidate the 
15369 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69  overflow page-li
1536a 73 74 20 63 61 63 68 65 20 66 6f 72 20 61 6c 6c  st cache for all
1536b 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a   cursors opened.
1536c 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64  ** on the shared
1536d 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65   btree structure
1536e 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   pBt..*/.static 
1536f 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 41  void invalidateA
15370 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  llOverflowCache(
15371 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
15372 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20    BtCursor *p;. 
15373 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15374 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
15375 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72  >mutex) );.  for
15376 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b  (p=pBt->pCursor;
15377 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
15378 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f  .    invalidateO
15379 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 29 3b  verflowCache(p);
1537a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
1537b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
1537c 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 6d 6f 64  alled before mod
1537d 69 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65  ifying the conte
1537e 6e 74 73 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a  nts of a table.*
1537f 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20  * to invalidate 
15380 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75 72  any incrblob cur
15381 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6f 70  sors that are op
15382 65 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 72 6f 77  en on the.** row
15383 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 72   or one of the r
15384 6f 77 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69  ows being modifi
15385 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67  ed..**.** If arg
15386 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62  ument isClearTab
15387 6c 65 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  le is true, then
15388 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74   the entire cont
15389 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 74  ents of the.** t
1538a 61 62 6c 65 20 69 73 20 61 62 6f 75 74 20 74 6f  able is about to
1538b 20 62 65 20 64 65 6c 65 74 65 64 2e 20 49 6e 20   be deleted. In 
1538c 74 68 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69  this case invali
1538d 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f  date all incrblo
1538e 62 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65  b.** cursors ope
1538f 6e 20 6f 6e 20 61 6e 79 20 72 6f 77 20 77 69 74  n on any row wit
15390 68 69 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69  hin the table wi
15391 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 70 67 6e  th root-page pgn
15392 6f 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  oRoot..**.** Oth
15393 65 72 77 69 73 65 2c 20 69 66 20 61 72 67 75 6d  erwise, if argum
15394 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65  ent isClearTable
15395 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20   is false, then 
15396 74 68 65 20 72 6f 77 20 77 69 74 68 0a 2a 2a 20  the row with.** 
15397 72 6f 77 69 64 20 69 52 6f 77 20 69 73 20 62 65  rowid iRow is be
15398 69 6e 67 20 72 65 70 6c 61 63 65 64 20 6f 72 20  ing replaced or 
15399 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 69 73  deleted. In this
1539a 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61 74 65   case invalidate
1539b 0a 2a 2a 20 6f 6e 6c 79 20 74 68 6f 73 65 20 69  .** only those i
1539c 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20  ncrblob cursors 
1539d 6f 70 65 6e 20 6f 6e 20 74 68 61 74 20 73 70 65  open on that spe
1539e 63 69 66 69 63 20 72 6f 77 2e 0a 2a 2f 0a 73 74  cific row..*/.st
1539f 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69  atic void invali
153a0 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73  dateIncrblobCurs
153a1 6f 72 73 28 0a 20 20 42 74 72 65 65 20 2a 70 42  ors(.  Btree *pB
153a2 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 2f  tree,          /
153a3 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66  * The database f
153a4 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a  ile to check */.
153a5 20 20 69 36 34 20 69 52 6f 77 2c 20 20 20 20 20    i64 iRow,     
153a6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
153a7 20 72 6f 77 69 64 20 74 68 61 74 20 6d 69 67 68   rowid that migh
153a8 74 20 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f  t be changing */
153a9 0a 20 20 69 6e 74 20 69 73 43 6c 65 61 72 54 61  .  int isClearTa
153aa 62 6c 65 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ble        /* Tr
153ab 75 65 20 69 66 20 61 6c 6c 20 72 6f 77 73 20 61  ue if all rows a
153ac 72 65 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64  re being deleted
153ad 20 2a 2f 0a 29 7b 0a 20 20 42 74 43 75 72 73 6f   */.){.  BtCurso
153ae 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72 65 64  r *p;.  BtShared
153af 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65 2d 3e   *pBt = pBtree->
153b0 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73  pBt;.  assert( s
153b1 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
153b2 4d 75 74 65 78 28 70 42 74 72 65 65 29 20 29 3b  Mutex(pBtree) );
153b3 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43  .  for(p=pBt->pC
153b4 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70  ursor; p; p=p->p
153b5 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70  Next){.    if( p
153b6 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64  ->isIncrblobHand
153b7 6c 65 20 26 26 20 28 69 73 43 6c 65 61 72 54 61  le && (isClearTa
153b8 62 6c 65 20 7c 7c 20 70 2d 3e 69 6e 66 6f 2e 6e  ble || p->info.n
153b9 4b 65 79 3d 3d 69 52 6f 77 29 20 29 7b 0a 20 20  Key==iRow) ){.  
153ba 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20      p->eState = 
153bb 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
153bc 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c      }.  }.}..#el
153bd 73 65 0a 20 20 2f 2a 20 53 74 75 62 20 66 75 6e  se.  /* Stub fun
153be 63 74 69 6f 6e 73 20 77 68 65 6e 20 49 4e 43 52  ctions when INCR
153bf 42 4c 4f 42 20 69 73 20 6f 6d 69 74 74 65 64 20  BLOB is omitted 
153c0 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76  */.  #define inv
153c1 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43  alidateOverflowC
153c2 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 69 6e  ache(x).  #defin
153c3 65 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f  e invalidateAllO
153c4 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a  verflowCache(x).
153c5 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69    #define invali
153c6 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73  dateIncrblobCurs
153c7 6f 72 73 28 78 2c 79 2c 7a 29 0a 23 65 6e 64 69  ors(x,y,z).#endi
153c8 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
153c9 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a  _INCRBLOB */../*
153ca 0a 2a 2a 20 53 65 74 20 62 69 74 20 70 67 6e 6f  .** Set bit pgno
153cb 20 6f 66 20 74 68 65 20 42 74 53 68 61 72 65 64   of the BtShared
153cc 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74  .pHasContent bit
153cd 76 65 63 2e 20 54 68 69 73 20 69 73 20 63 61 6c  vec. This is cal
153ce 6c 65 64 20 0a 2a 2a 20 77 68 65 6e 20 61 20 70  led .** when a p
153cf 61 67 65 20 74 68 61 74 20 70 72 65 76 69 6f 75  age that previou
153d0 73 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 64 61  sly contained da
153d1 74 61 20 62 65 63 6f 6d 65 73 20 61 20 66 72 65  ta becomes a fre
153d2 65 2d 6c 69 73 74 20 6c 65 61 66 20 0a 2a 2a 20  e-list leaf .** 
153d3 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  page..**.** The 
153d4 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e  BtShared.pHasCon
153d5 74 65 6e 74 20 62 69 74 76 65 63 20 65 78 69 73  tent bitvec exis
153d6 74 73 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  ts to work aroun
153d7 64 20 61 6e 20 6f 62 73 63 75 72 65 0a 2a 2a 20  d an obscure.** 
153d8 62 75 67 20 63 61 75 73 65 64 20 62 79 20 74 68  bug caused by th
153d9 65 20 69 6e 74 65 72 61 63 74 69 6f 6e 20 6f 66  e interaction of
153da 20 74 77 6f 20 75 73 65 66 75 6c 20 49 4f 20 6f   two useful IO o
153db 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 73 75 72  ptimizations sur
153dc 72 6f 75 6e 64 69 6e 67 0a 2a 2a 20 66 72 65 65  rounding.** free
153dd 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 73  -list leaf pages
153de 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65  :.**.**   1) Whe
153df 6e 20 61 6c 6c 20 64 61 74 61 20 69 73 20 64 65  n all data is de
153e0 6c 65 74 65 64 20 66 72 6f 6d 20 61 20 70 61 67  leted from a pag
153e1 65 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 62  e and the page b
153e2 65 63 6f 6d 65 73 0a 2a 2a 20 20 20 20 20 20 61  ecomes.**      a
153e3 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20   free-list leaf 
153e4 70 61 67 65 2c 20 74 68 65 20 70 61 67 65 20 69  page, the page i
153e5 73 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f  s not written to
153e6 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
153e7 20 20 20 20 20 20 28 61 73 20 66 72 65 65 2d 6c        (as free-l
153e8 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 20 63  ist leaf pages c
153e9 6f 6e 74 61 69 6e 20 6e 6f 20 6d 65 61 6e 69 6e  ontain no meanin
153ea 67 66 75 6c 20 64 61 74 61 29 2e 20 53 6f 6d 65  gful data). Some
153eb 74 69 6d 65 73 0a 2a 2a 20 20 20 20 20 20 73 75  times.**      su
153ec 63 68 20 61 20 70 61 67 65 20 69 73 20 6e 6f 74  ch a page is not
153ed 20 65 76 65 6e 20 6a 6f 75 72 6e 61 6c 6c 65 64   even journalled
153ee 20 28 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74   (as it will not
153ef 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a   be modified,.**
153f0 20 20 20 20 20 20 77 68 79 20 62 6f 74 68 65 72        why bother
153f1 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 69 74 3f   journalling it?
153f2 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68  )..**.**   2) Wh
153f3 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c  en a free-list l
153f4 65 61 66 20 70 61 67 65 20 69 73 20 72 65 75 73  eaf page is reus
153f5 65 64 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 20  ed, its content 
153f6 69 73 20 6e 6f 74 20 72 65 61 64 0a 2a 2a 20 20  is not read.**  
153f7 20 20 20 20 66 72 6f 6d 20 74 68 65 20 64 61 74      from the dat
153f8 61 62 61 73 65 20 6f 72 20 77 72 69 74 74 65 6e  abase or written
153f9 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
153fa 66 69 6c 65 20 28 77 68 79 20 73 68 6f 75 6c 64  file (why should
153fb 20 69 74 0a 2a 2a 20 20 20 20 20 20 62 65 2c 20   it.**      be, 
153fc 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 74 20  if it is not at 
153fd 61 6c 6c 20 6d 65 61 6e 69 6e 67 66 75 6c 3f 29  all meaningful?)
153fe 2e 0a 2a 2a 0a 2a 2a 20 42 79 20 74 68 65 6d 73  ..**.** By thems
153ff 65 6c 76 65 73 2c 20 74 68 65 73 65 20 6f 70 74  elves, these opt
15400 69 6d 69 7a 61 74 69 6f 6e 73 20 77 6f 72 6b 20  imizations work 
15401 66 69 6e 65 20 61 6e 64 20 70 72 6f 76 69 64 65  fine and provide
15402 20 61 20 68 61 6e 64 79 0a 2a 2a 20 70 65 72 66   a handy.** perf
15403 6f 72 6d 61 6e 63 65 20 62 6f 6f 73 74 20 74 6f  ormance boost to
15404 20 62 75 6c 6b 20 64 65 6c 65 74 65 20 6f 72 20   bulk delete or 
15405 69 6e 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e  insert operation
15406 73 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 0a 2a  s. However, if.*
15407 2a 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65  * a page is move
15408 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69  d to the free-li
15409 73 74 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73  st and then reus
1540a 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 61  ed within the sa
1540b 6d 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  me.** transactio
1540c 6e 2c 20 61 20 70 72 6f 62 6c 65 6d 20 63 6f 6d  n, a problem com
1540d 65 73 20 75 70 2e 20 49 66 20 74 68 65 20 70 61  es up. If the pa
1540e 67 65 20 69 73 20 6e 6f 74 20 6a 6f 75 72 6e 61  ge is not journa
1540f 6c 6c 65 64 20 77 68 65 6e 0a 2a 2a 20 69 74 20  lled when.** it 
15410 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20  is moved to the 
15411 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 69 74  free-list and it
15412 20 69 73 20 61 6c 73 6f 20 6e 6f 74 20 6a 6f 75   is also not jou
15413 72 6e 61 6c 6c 65 64 20 77 68 65 6e 20 69 74 0a  rnalled when it.
15414 2a 2a 20 69 73 20 65 78 74 72 61 63 74 65 64 20  ** is extracted 
15415 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69  from the free-li
15416 73 74 20 61 6e 64 20 72 65 75 73 65 64 2c 20 74  st and reused, t
15417 68 65 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  hen the original
15418 20 64 61 74 61 0a 2a 2a 20 6d 61 79 20 62 65 20   data.** may be 
15419 6c 6f 73 74 2e 20 49 6e 20 74 68 65 20 65 76 65  lost. In the eve
1541a 6e 74 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b  nt of a rollback
1541b 2c 20 69 74 20 6d 61 79 20 6e 6f 74 20 62 65 20  , it may not be 
1541c 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 72  possible.** to r
1541d 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62  estore the datab
1541e 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69  ase to its origi
1541f 6e 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  nal configuratio
15420 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 6c  n..**.** The sol
15421 75 74 69 6f 6e 20 69 73 20 74 68 65 20 42 74 53  ution is the BtS
15422 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e  hared.pHasConten
15423 74 20 62 69 74 76 65 63 2e 20 57 68 65 6e 65 76  t bitvec. Whenev
15424 65 72 20 61 20 70 61 67 65 20 69 73 20 0a 2a 2a  er a page is .**
15425 20 6d 6f 76 65 64 20 74 6f 20 62 65 63 6f 6d 65   moved to become
15426 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61   a free-list lea
15427 66 20 70 61 67 65 2c 20 74 68 65 20 63 6f 72 72  f page, the corr
15428 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 73  esponding bit is
15429 0a 2a 2a 20 73 65 74 20 69 6e 20 74 68 65 20 62  .** set in the b
1542a 69 74 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20  itvec. Whenever 
1542b 61 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 65  a leaf page is e
1542c 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68  xtracted from th
1542d 65 20 66 72 65 65 2d 6c 69 73 74 2c 0a 2a 2a 20  e free-list,.** 
1542e 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 32 20 61  optimization 2 a
1542f 62 6f 76 65 20 69 73 20 6f 6d 69 74 74 65 64 20  bove is omitted 
15430 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  if the correspon
15431 64 69 6e 67 20 62 69 74 20 69 73 20 61 6c 72 65  ding bit is alre
15432 61 64 79 0a 2a 2a 20 73 65 74 20 69 6e 20 42 74  ady.** set in Bt
15433 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65  Shared.pHasConte
15434 6e 74 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73  nt. The contents
15435 20 6f 66 20 74 68 65 20 62 69 74 76 65 63 20 61   of the bitvec a
15436 72 65 20 63 6c 65 61 72 65 64 0a 2a 2a 20 61 74  re cleared.** at
15437 20 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72   the end of ever
15438 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  y transaction..*
15439 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72  /.static int btr
1543a 65 65 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28  eeSetHasContent(
1543b 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50  BtShared *pBt, P
1543c 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74  gno pgno){.  int
1543d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1543e 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61  .  if( !pBt->pHa
1543f 73 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20  sContent ){.    
15440 69 6e 74 20 6e 50 61 67 65 20 3d 20 31 30 30 3b  int nPage = 100;
15441 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
15442 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e  rPagecount(pBt->
15443 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b  pPager, &nPage);
15444 0a 20 20 20 20 2f 2a 20 49 66 20 73 71 6c 69 74  .    /* If sqlit
15445 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
15446 28 29 20 66 61 69 6c 73 20 74 68 65 72 65 20 69  () fails there i
15447 73 20 6e 6f 20 68 61 72 6d 20 62 65 63 61 75 73  s no harm becaus
15448 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 50 61  e the.    ** nPa
15449 67 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 75  ge variable is u
1544a 6e 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 74  nchanged from it
1544b 73 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20  s default value 
1544c 6f 66 20 31 30 30 20 2a 2f 0a 20 20 20 20 70 42  of 100 */.    pB
1544d 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d  t->pHasContent =
1544e 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72   sqlite3BitvecCr
1544f 65 61 74 65 28 28 75 33 32 29 6e 50 61 67 65 29  eate((u32)nPage)
15450 3b 0a 20 20 20 20 69 66 28 20 21 70 42 74 2d 3e  ;.    if( !pBt->
15451 70 48 61 73 43 6f 6e 74 65 6e 74 20 29 7b 0a 20  pHasContent ){. 
15452 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
15453 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
15454 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
15455 54 45 5f 4f 4b 20 26 26 20 70 67 6e 6f 3c 3d 73  TE_OK && pgno<=s
15456 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a 65  qlite3BitvecSize
15457 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e  (pBt->pHasConten
15458 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  t) ){.    rc = s
15459 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
1545a 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74  pBt->pHasContent
1545b 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72  , pgno);.  }.  r
1545c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1545d 2a 2a 20 51 75 65 72 79 20 74 68 65 20 42 74 53  ** Query the BtS
1545e 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e  hared.pHasConten
1545f 74 20 76 65 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20  t vector..**.** 
15460 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
15461 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 66   called when a f
15462 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61  ree-list leaf pa
15463 67 65 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72  ge is removed fr
15464 6f 6d 20 74 68 65 0a 2a 2a 20 66 72 65 65 2d 6c  om the.** free-l
15465 69 73 74 20 66 6f 72 20 72 65 75 73 65 2e 20 49  ist for reuse. I
15466 74 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20  t returns false 
15467 69 66 20 69 74 20 69 73 20 73 61 66 65 20 74 6f  if it is safe to
15468 20 72 65 74 72 69 65 76 65 20 74 68 65 0a 2a 2a   retrieve the.**
15469 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70   page from the p
1546a 61 67 65 72 20 6c 61 79 65 72 20 77 69 74 68 20  ager layer with 
1546b 74 68 65 20 27 6e 6f 2d 63 6f 6e 74 65 6e 74 27  the 'no-content'
1546c 20 66 6c 61 67 20 73 65 74 2e 20 54 72 75 65 20   flag set. True 
1546d 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
1546e 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 47 65  atic int btreeGe
1546f 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 68  tHasContent(BtSh
15470 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20  ared *pBt, Pgno 
15471 70 67 6e 6f 29 7b 0a 20 20 42 69 74 76 65 63 20  pgno){.  Bitvec 
15472 2a 70 20 3d 20 70 42 74 2d 3e 70 48 61 73 43 6f  *p = pBt->pHasCo
15473 6e 74 65 6e 74 3b 0a 20 20 72 65 74 75 72 6e 20  ntent;.  return 
15474 28 70 20 26 26 20 28 70 67 6e 6f 3e 73 71 6c 69  (p && (pgno>sqli
15475 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 29  te3BitvecSize(p)
15476 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76 65   || sqlite3Bitve
15477 63 54 65 73 74 28 70 2c 20 70 67 6e 6f 29 29 29  cTest(p, pgno)))
15478 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  ;.}../*.** Clear
15479 20 28 64 65 73 74 72 6f 79 29 20 74 68 65 20 42   (destroy) the B
1547a 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74  tShared.pHasCont
1547b 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73  ent bitvec. This
1547c 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 6e   should be.** in
1547d 76 6f 6b 65 64 20 61 74 20 74 68 65 20 63 6f 6e  voked at the con
1547e 63 6c 75 73 69 6f 6e 20 6f 66 20 65 61 63 68 20  clusion of each 
1547f 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
15480 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
15481 64 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 43  d btreeClearHasC
15482 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20  ontent(BtShared 
15483 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  *pBt){.  sqlite3
15484 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 42  BitvecDestroy(pB
15485 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 3b  t->pHasContent);
15486 0a 20 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74  .  pBt->pHasCont
15487 65 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  ent = 0;.}../*.*
15488 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72 65  * Save the curre
15489 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69  nt cursor positi
1548a 6f 6e 20 69 6e 20 74 68 65 20 76 61 72 69 61 62  on in the variab
1548b 6c 65 73 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65  les BtCursor.nKe
1548c 79 20 0a 2a 2a 20 61 6e 64 20 42 74 43 75 72 73  y .** and BtCurs
1548d 6f 72 2e 70 4b 65 79 2e 20 54 68 65 20 63 75 72  or.pKey. The cur
1548e 73 6f 72 27 73 20 73 74 61 74 65 20 69 73 20 73  sor's state is s
1548f 65 74 20 74 6f 20 43 55 52 53 4f 52 5f 52 45 51  et to CURSOR_REQ
15490 55 49 52 45 53 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20  UIRESEEK..**.** 
15491 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  The caller must 
15492 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20  ensure that the 
15493 63 75 72 73 6f 72 20 69 73 20 76 61 6c 69 64 20  cursor is valid 
15494 28 68 61 73 20 65 53 74 61 74 65 3d 3d 43 55 52  (has eState==CUR
15495 53 4f 52 5f 56 41 4c 49 44 29 0a 2a 2a 20 70 72  SOR_VALID).** pr
15496 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74  ior to calling t
15497 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 0a 2a  his routine.  .*
15498 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76  /.static int sav
15499 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
1549a 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
1549b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
1549c 73 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c  sert( CURSOR_VAL
1549d 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65  ID==pCur->eState
1549e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d   );.  assert( 0=
1549f 3d 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 20  =pCur->pKey );. 
154a0 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
154a1 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
154a2 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  );..  rc = sqlit
154a3 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70  e3BtreeKeySize(p
154a4 43 75 72 2c 20 26 70 43 75 72 2d 3e 6e 4b 65 79  Cur, &pCur->nKey
154a5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  );.  assert( rc=
154a6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 2f  =SQLITE_OK );  /
154a7 2a 20 4b 65 79 53 69 7a 65 28 29 20 63 61 6e 6e  * KeySize() cann
154a8 6f 74 20 66 61 69 6c 20 2a 2f 0a 0a 20 20 2f 2a  ot fail */..  /*
154a9 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69   If this is an i
154aa 6e 74 4b 65 79 20 74 61 62 6c 65 2c 20 74 68 65  ntKey table, the
154ab 6e 20 74 68 65 20 61 62 6f 76 65 20 63 61 6c 6c  n the above call
154ac 20 74 6f 20 42 74 72 65 65 4b 65 79 53 69 7a 65   to BtreeKeySize
154ad 28 29 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 74  ().  ** stores t
154ae 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 69  he integer key i
154af 6e 20 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e  n pCur->nKey. In
154b0 20 74 68 69 73 20 63 61 73 65 20 74 68 69 73 20   this case this 
154b1 76 61 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61 6c  value is.  ** al
154b2 6c 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72  l that is requir
154b3 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ed. Otherwise, i
154b4 66 20 70 43 75 72 20 69 73 20 6e 6f 74 20 6f 70  f pCur is not op
154b5 65 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 0a  en on an intKey.
154b6 20 20 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65 6e    ** table, then
154b7 20 6d 61 6c 6c 6f 63 20 73 70 61 63 65 20 66 6f   malloc space fo
154b8 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  r and store the 
154b9 70 43 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65 73  pCur->nKey bytes
154ba 20 6f 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64 61   of key .  ** da
154bb 74 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30  ta..  */.  if( 0
154bc 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30  ==pCur->apPage[0
154bd 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  ]->intKey ){.   
154be 20 76 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 71   void *pKey = sq
154bf 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 69 6e  lite3Malloc( (in
154c0 74 29 70 43 75 72 2d 3e 6e 4b 65 79 20 29 3b 0a  t)pCur->nKey );.
154c1 20 20 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a      if( pKey ){.
154c2 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
154c3 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c  e3BtreeKey(pCur,
154c4 20 30 2c 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e   0, (int)pCur->n
154c5 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20  Key, pKey);.    
154c6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
154c7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70  _OK ){.        p
154c8 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79  Cur->pKey = pKey
154c9 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
154ca 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
154cb 72 65 65 28 70 4b 65 79 29 3b 0a 20 20 20 20 20  ree(pKey);.     
154cc 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
154cd 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
154ce 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d  NOMEM;.    }.  }
154cf 0a 20 20 61 73 73 65 72 74 28 20 21 70 43 75 72  .  assert( !pCur
154d0 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74  ->apPage[0]->int
154d1 4b 65 79 20 7c 7c 20 21 70 43 75 72 2d 3e 70 4b  Key || !pCur->pK
154d2 65 79 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d  ey );..  if( rc=
154d3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
154d4 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
154d5 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69  (i=0; i<=pCur->i
154d6 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Page; i++){.    
154d7 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43    releasePage(pC
154d8 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a  ur->apPage[i]);.
154d9 20 20 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61        pCur->apPa
154da 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d  ge[i] = 0;.    }
154db 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65  .    pCur->iPage
154dc 20 3d 20 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d   = -1;.    pCur-
154dd 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
154de 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0a 20 20  _REQUIRESEEK;.  
154df 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 4f  }..  invalidateO
154e0 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75  verflowCache(pCu
154e1 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  r);.  return rc;
154e2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74  .}../*.** Save t
154e3 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20  he positions of 
154e4 61 6c 6c 20 63 75 72 73 6f 72 73 20 28 65 78 63  all cursors (exc
154e5 65 70 74 20 70 45 78 63 65 70 74 29 20 74 68 61  ept pExcept) tha
154e6 74 20 61 72 65 20 6f 70 65 6e 20 6f 6e 0a 2a 2a  t are open on.**
154e7 20 74 68 65 20 74 61 62 6c 65 20 20 77 69 74 68   the table  with
154e8 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74   root-page iRoot
154e9 2e 20 55 73 75 61 6c 6c 79 2c 20 74 68 69 73 20  . Usually, this 
154ea 69 73 20 63 61 6c 6c 65 64 20 6a 75 73 74 20 62  is called just b
154eb 65 66 6f 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20  efore cursor.** 
154ec 70 45 78 63 65 70 74 20 69 73 20 75 73 65 64 20  pExcept is used 
154ed 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 74 61  to modify the ta
154ee 62 6c 65 20 28 42 74 72 65 65 44 65 6c 65 74 65  ble (BtreeDelete
154ef 28 29 20 6f 72 20 42 74 72 65 65 49 6e 73 65 72  () or BtreeInser
154f0 74 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  t())..*/.static 
154f1 69 6e 74 20 73 61 76 65 41 6c 6c 43 75 72 73 6f  int saveAllCurso
154f2 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  rs(BtShared *pBt
154f3 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74  , Pgno iRoot, Bt
154f4 43 75 72 73 6f 72 20 2a 70 45 78 63 65 70 74 29  Cursor *pExcept)
154f5 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b  {.  BtCursor *p;
154f6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
154f7 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
154f8 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
154f9 73 73 65 72 74 28 20 70 45 78 63 65 70 74 3d 3d  ssert( pExcept==
154fa 30 20 7c 7c 20 70 45 78 63 65 70 74 2d 3e 70 42  0 || pExcept->pB
154fb 74 3d 3d 70 42 74 20 29 3b 0a 20 20 66 6f 72 28  t==pBt );.  for(
154fc 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20  p=pBt->pCursor; 
154fd 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a  p; p=p->pNext){.
154fe 20 20 20 20 69 66 28 20 70 21 3d 70 45 78 63 65      if( p!=pExce
154ff 70 74 20 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20  pt && (0==iRoot 
15500 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d  || p->pgnoRoot==
15501 69 52 6f 6f 74 29 20 26 26 20 0a 20 20 20 20 20  iRoot) && .     
15502 20 20 20 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55     p->eState==CU
15503 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20  RSOR_VALID ){.  
15504 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 61 76      int rc = sav
15505 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
15506 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51  p);.      if( SQ
15507 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20  LITE_OK!=rc ){. 
15508 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
15509 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1550a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
1550b 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
1550c 20 43 6c 65 61 72 20 74 68 65 20 63 75 72 72 65   Clear the curre
1550d 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69  nt cursor positi
1550e 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
1550f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
15510 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73  e3BtreeClearCurs
15511 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  or(BtCursor *pCu
15512 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75  r){.  assert( cu
15513 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
15514 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  Cur) );.  sqlite
15515 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b 65  3_free(pCur->pKe
15516 79 29 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79  y);.  pCur->pKey
15517 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53   = 0;.  pCur->eS
15518 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e  tate = CURSOR_IN
15519 56 41 4c 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  VALID;.}../*.** 
1551a 49 6e 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20  In this version 
1551b 6f 66 20 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20  of BtreeMoveto, 
1551c 70 4b 65 79 20 69 73 20 61 20 70 61 63 6b 65 64  pKey is a packed
1551d 20 69 6e 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a   index record.**
1551e 20 73 75 63 68 20 61 73 20 69 73 20 67 65 6e 65   such as is gene
1551f 72 61 74 65 64 20 62 79 20 74 68 65 20 4f 50 5f  rated by the OP_
15520 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64  MakeRecord opcod
15521 65 2e 20 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a  e.  Unpack the.*
15522 2a 20 72 65 63 6f 72 64 20 61 6e 64 20 74 68 65  * record and the
15523 6e 20 63 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65  n call BtreeMove
15524 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 74 6f 20  toUnpacked() to 
15525 64 6f 20 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a  do the work..*/.
15526 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65  static int btree
15527 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72 73  Moveto(.  BtCurs
15528 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a  or *pCur,     /*
15529 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20   Cursor open on 
1552a 74 68 65 20 62 74 72 65 65 20 74 6f 20 62 65 20  the btree to be 
1552b 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f  searched */.  co
1552c 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
1552d 20 20 2f 2a 20 50 61 63 6b 65 64 20 6b 65 79 20    /* Packed key 
1552e 69 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20  if the btree is 
1552f 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36  an index */.  i6
15530 34 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20  4 nKey,         
15531 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 6b 65 79    /* Integer key
15532 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 53 69   for tables.  Si
15533 7a 65 20 6f 66 20 70 4b 65 79 20 66 6f 72 20 69  ze of pKey for i
15534 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20  ndices */.  int 
15535 62 69 61 73 2c 20 20 20 20 20 20 20 20 20 20 20  bias,           
15536 2f 2a 20 42 69 61 73 20 73 65 61 72 63 68 20 74  /* Bias search t
15537 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20 2a  o the high end *
15538 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20 20  /.  int *pRes   
15539 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
1553a 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 20   search results 
1553b 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
1553c 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
1553d 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75          /* Statu
1553e 73 20 63 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61  s code */.  Unpa
1553f 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78  ckedRecord *pIdx
15540 4b 65 79 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b  Key;   /* Unpack
15541 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a  ed index key */.
15542 20 20 63 68 61 72 20 61 53 70 61 63 65 5b 31 35    char aSpace[15
15543 30 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  0];          /* 
15544 54 65 6d 70 20 73 70 61 63 65 20 66 6f 72 20 70  Temp space for p
15545 49 64 78 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69  IdxKey - to avoi
15546 64 20 61 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 20  d a malloc */.. 
15547 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20   if( pKey ){.   
15548 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28   assert( nKey==(
15549 69 36 34 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b  i64)(int)nKey );
1554a 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73  .    pIdxKey = s
1554b 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64  qlite3VdbeRecord
1554c 55 6e 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65  Unpack(pCur->pKe
1554d 79 49 6e 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79  yInfo, (int)nKey
1554e 2c 20 70 4b 65 79 2c 0a 20 20 20 20 20 20 20 20  , pKey,.        
1554f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 53                aS
15551 70 61 63 65 2c 20 73 69 7a 65 6f 66 28 61 53 70  pace, sizeof(aSp
15552 61 63 65 29 29 3b 0a 20 20 20 20 69 66 28 20 70  ace));.    if( p
15553 49 64 78 4b 65 79 3d 3d 30 20 29 20 72 65 74 75  IdxKey==0 ) retu
15554 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
15555 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49  .  }else{.    pI
15556 64 78 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20  dxKey = 0;.  }. 
15557 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
15558 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
15559 28 70 43 75 72 2c 20 70 49 64 78 4b 65 79 2c 20  (pCur, pIdxKey, 
1555a 6e 4b 65 79 2c 20 62 69 61 73 2c 20 70 52 65 73  nKey, bias, pRes
1555b 29 3b 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b  );.  if( pKey ){
1555c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1555d 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65  DeleteUnpackedRe
1555e 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a 20  cord(pIdxKey);. 
1555f 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
15560 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65  }../*.** Restore
15561 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74   the cursor to t
15562 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 77  he position it w
15563 61 73 20 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f  as in (or as clo
15564 73 65 20 74 6f 20 61 73 20 70 6f 73 73 69 62 6c  se to as possibl
15565 65 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 43  e).** when saveC
15566 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20  ursorPosition() 
15567 77 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65  was called. Note
15568 20 74 68 61 74 20 74 68 69 73 20 63 61 6c 6c 20   that this call 
15569 64 65 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a 20  deletes the .** 
1556a 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69  saved position i
1556b 6e 66 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61  nfo stored by sa
1556c 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  veCursorPosition
1556d 28 29 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e  (), so there can
1556e 20 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f   be.** at most o
1556f 6e 65 20 65 66 66 65 63 74 69 76 65 20 72 65 73  ne effective res
15570 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69  toreCursorPositi
15571 6f 6e 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20  on() call after 
15572 65 61 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 72  each .** saveCur
15573 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a  sorPosition()..*
15574 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72  /.static int btr
15575 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72 50  eeRestoreCursorP
15576 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72  osition(BtCursor
15577 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72   *pCur){.  int r
15578 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  c;.  assert( cur
15579 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
1557a 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
1557b 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43   pCur->eState>=C
1557c 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
1557d 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  K );.  if( pCur-
1557e 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
1557f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74  FAULT ){.    ret
15580 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65  urn pCur->skipNe
15581 78 74 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e  xt;.  }.  pCur->
15582 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
15583 49 4e 56 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20  INVALID;.  rc = 
15584 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72  btreeMoveto(pCur
15585 2c 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43  , pCur->pKey, pC
15586 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43  ur->nKey, 0, &pC
15587 75 72 2d 3e 73 6b 69 70 4e 65 78 74 29 3b 0a 20  ur->skipNext);. 
15588 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
15589 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
1558a 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b 65  3_free(pCur->pKe
1558b 79 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b  y);.    pCur->pK
1558c 65 79 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65  ey = 0;.    asse
1558d 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
1558e 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c  ==CURSOR_VALID |
1558f 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  | pCur->eState==
15590 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29  CURSOR_INVALID )
15591 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
15592 63 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65  c;.}..#define re
15593 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
15594 69 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65  ion(p) \.  (p->e
15595 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45  State>=CURSOR_RE
15596 51 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20  QUIRESEEK ? \.  
15597 20 20 20 20 20 20 20 62 74 72 65 65 52 65 73 74         btreeRest
15598 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
15599 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20 20 20 20  n(p) : \.       
1559a 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a    SQLITE_OK)../*
1559b 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 68  .** Determine wh
1559c 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 63  ether or not a c
1559d 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 20  ursor has moved 
1559e 66 72 6f 6d 20 74 68 65 20 70 6f 73 69 74 69 6f  from the positio
1559f 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74  n it.** was last
155a0 20 70 6c 61 63 65 64 20 61 74 2e 20 20 43 75 72   placed at.  Cur
155a1 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 77 68  sors can move wh
155a2 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 79 20  en the row they 
155a3 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20  are pointing.** 
155a4 61 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f 75  at is deleted ou
155a5 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65  t from under the
155a6 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  m..**.** This ro
155a7 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 6e  utine returns an
155a8 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 73   error code if s
155a9 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72  omething goes wr
155aa 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 74  ong.  The.** int
155ab 65 67 65 72 20 2a 70 48 61 73 4d 6f 76 65 64 20  eger *pHasMoved 
155ac 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 69 66  is set to one if
155ad 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20   the cursor has 
155ae 6d 6f 76 65 64 20 61 6e 64 20 30 20 69 66 20 6e  moved and 0 if n
155af 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ot..*/.SQLITE_PR
155b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
155b1 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d  3BtreeCursorHasM
155b2 6f 76 65 64 28 42 74 43 75 72 73 6f 72 20 2a 70  oved(BtCursor *p
155b3 43 75 72 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f  Cur, int *pHasMo
155b4 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ved){.  int rc;.
155b5 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43  .  rc = restoreC
155b6 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43  ursorPosition(pC
155b7 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  ur);.  if( rc ){
155b8 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20  .    *pHasMoved 
155b9 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
155ba 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43  rc;.  }.  if( pC
155bb 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53  ur->eState!=CURS
155bc 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72  OR_VALID || pCur
155bd 2d 3e 73 6b 69 70 4e 65 78 74 21 3d 30 20 29 7b  ->skipNext!=0 ){
155be 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20  .    *pHasMoved 
155bf 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 1;.  }else{.  
155c0 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 30    *pHasMoved = 0
155c1 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
155c2 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66  QLITE_OK;.}..#if
155c3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
155c4 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a  _AUTOVACUUM./*.*
155c5 2a 20 47 69 76 65 6e 20 61 20 70 61 67 65 20 6e  * Given a page n
155c6 75 6d 62 65 72 20 6f 66 20 61 20 72 65 67 75 6c  umber of a regul
155c7 61 72 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ar database page
155c8 2c 20 72 65 74 75 72 6e 20 74 68 65 20 70 61 67  , return the pag
155c9 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20  e.** number for 
155ca 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  the pointer-map 
155cb 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69  page that contai
155cc 6e 73 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72  ns the entry for
155cd 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61   the.** input pa
155ce 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74  ge number..*/.st
155cf 61 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70  atic Pgno ptrmap
155d0 50 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20  Pageno(BtShared 
155d1 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  *pBt, Pgno pgno)
155d2 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65  {.  int nPagesPe
155d3 72 4d 61 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f  rMapPage;.  Pgno
155d4 20 69 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20   iPtrMap, ret;. 
155d5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
155d6 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
155d7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 50 61  >mutex) );.  nPa
155d8 67 65 73 50 65 72 4d 61 70 50 61 67 65 20 3d 20  gesPerMapPage = 
155d9 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
155da 2f 35 29 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70  /5)+1;.  iPtrMap
155db 20 3d 20 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67   = (pgno-2)/nPag
155dc 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20  esPerMapPage;.  
155dd 72 65 74 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e  ret = (iPtrMap*n
155de 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 29  PagesPerMapPage)
155df 20 2b 20 32 3b 20 0a 20 20 69 66 28 20 72 65 74   + 2; .  if( ret
155e0 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  ==PENDING_BYTE_P
155e1 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
155e2 72 65 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74  ret++;.  }.  ret
155e3 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn ret;.}../*.*
155e4 2a 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79  * Write an entry
155e5 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65   into the pointe
155e6 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  r map..**.** Thi
155e7 73 20 72 6f 75 74 69 6e 65 20 75 70 64 61 74 65  s routine update
155e8 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  s the pointer ma
155e9 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65  p entry for page
155ea 20 6e 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a   number 'key'.**
155eb 20 73 6f 20 74 68 61 74 20 69 74 20 6d 61 70 73   so that it maps
155ec 20 74 6f 20 74 79 70 65 20 27 65 54 79 70 65 27   to type 'eType'
155ed 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65   and parent page
155ee 20 6e 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a   number 'pgno'..
155ef 2a 2a 0a 2a 2a 20 49 66 20 2a 70 52 43 20 69 73  **.** If *pRC is
155f0 20 69 6e 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a   initially non-z
155f1 65 72 6f 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f  ero (non-SQLITE_
155f2 4f 4b 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f  OK) then this ro
155f3 75 74 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f  utine is.** a no
155f4 2d 6f 70 2e 20 20 49 66 20 61 6e 20 65 72 72 6f  -op.  If an erro
155f5 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 61 70  r occurs, the ap
155f6 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20  propriate error 
155f7 63 6f 64 65 20 69 73 20 77 72 69 74 74 65 6e 0a  code is written.
155f8 2a 2a 20 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f  ** into *pRC..*/
155f9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 72  .static void ptr
155fa 6d 61 70 50 75 74 28 42 74 53 68 61 72 65 64 20  mapPut(BtShared 
155fb 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20  *pBt, Pgno key, 
155fc 75 38 20 65 54 79 70 65 2c 20 50 67 6e 6f 20 70  u8 eType, Pgno p
155fd 61 72 65 6e 74 2c 20 69 6e 74 20 2a 70 52 43 29  arent, int *pRC)
155fe 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50  {.  DbPage *pDbP
155ff 61 67 65 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69  age;  /* The poi
15600 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f  nter map page */
15601 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20  .  u8 *pPtrmap; 
15602 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e       /* The poin
15603 74 65 72 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a  ter map data */.
15604 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20    Pgno iPtrmap; 
15605 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74      /* The point
15606 65 72 20 6d 61 70 20 70 61 67 65 20 6e 75 6d 62  er map page numb
15607 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73  er */.  int offs
15608 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66  et;       /* Off
15609 73 65 74 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d  set in pointer m
1560a 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ap page */.  int
1560b 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 2f   rc;           /
1560c 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72  * Return code fr
1560d 6f 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20  om subfunctions 
1560e 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29  */..  if( *pRC )
1560f 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65   return;..  asse
15610 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15611 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
15612 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d  x) );.  /* The m
15613 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61  aster-journal pa
15614 67 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e  ge number must n
15615 65 76 65 72 20 62 65 20 75 73 65 64 20 61 73 20  ever be used as 
15616 61 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  a pointer map pa
15617 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  ge */.  assert( 
15618 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50 41 47 45  0==PTRMAP_ISPAGE
15619 28 70 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59  (pBt, PENDING_BY
1561a 54 45 5f 50 41 47 45 28 70 42 74 29 29 20 29 3b  TE_PAGE(pBt)) );
1561b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d  ..  assert( pBt-
1561c 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20  >autoVacuum );. 
1561d 20 69 66 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20   if( key==0 ){. 
1561e 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45     *pRC = SQLITE
1561f 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
15620 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
15621 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41   iPtrmap = PTRMA
15622 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65  P_PAGENO(pBt, ke
15623 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  y);.  rc = sqlit
15624 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e  e3PagerGet(pBt->
15625 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c  pPager, iPtrmap,
15626 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66   &pDbPage);.  if
15627 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
15628 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63  ){.    *pRC = rc
15629 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
1562a 7d 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52  }.  offset = PTR
1562b 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50  MAP_PTROFFSET(iP
1562c 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69  trmap, key);.  i
1562d 66 28 20 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20  f( offset<0 ){. 
1562e 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45     *pRC = SQLITE
1562f 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
15630 20 20 20 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65     goto ptrmap_e
15631 78 69 74 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d  xit;.  }.  pPtrm
15632 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74  ap = (u8 *)sqlit
15633 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
15634 44 62 50 61 67 65 29 3b 0a 0a 20 20 69 66 28 20  DbPage);..  if( 
15635 65 54 79 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f  eType!=pPtrmap[o
15636 66 66 73 65 74 5d 20 7c 7c 20 67 65 74 34 62 79  ffset] || get4by
15637 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73  te(&pPtrmap[offs
15638 65 74 2b 31 5d 29 21 3d 70 61 72 65 6e 74 20 29  et+1])!=parent )
15639 7b 0a 20 20 20 20 54 52 41 43 45 28 28 22 50 54  {.    TRACE(("PT
1563a 52 4d 41 50 5f 55 50 44 41 54 45 3a 20 25 64 2d  RMAP_UPDATE: %d-
1563b 3e 28 25 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79  >(%d,%d)\n", key
1563c 2c 20 65 54 79 70 65 2c 20 70 61 72 65 6e 74 29  , eType, parent)
1563d 29 3b 0a 20 20 20 20 2a 70 52 43 3d 20 72 63 20  );.    *pRC= rc 
1563e 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1563f 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ite(pDbPage);.  
15640 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
15641 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 74  _OK ){.      pPt
15642 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 3d 20 65  rmap[offset] = e
15643 54 79 70 65 3b 0a 20 20 20 20 20 20 70 75 74 34  Type;.      put4
15644 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66  byte(&pPtrmap[of
15645 66 73 65 74 2b 31 5d 2c 20 70 61 72 65 6e 74 29  fset+1], parent)
15646 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 70 74 72  ;.    }.  }..ptr
15647 6d 61 70 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69  map_exit:.  sqli
15648 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44  te3PagerUnref(pD
15649 62 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  bPage);.}../*.**
1564a 20 52 65 61 64 20 61 6e 20 65 6e 74 72 79 20 66   Read an entry f
1564b 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 65 72 20  rom the pointer 
1564c 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  map..**.** This 
1564d 72 6f 75 74 69 6e 65 20 72 65 74 72 69 65 76 65  routine retrieve
1564e 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  s the pointer ma
1564f 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65  p entry for page
15650 20 27 6b 65 79 27 2c 20 77 72 69 74 69 6e 67 0a   'key', writing.
15651 2a 2a 20 74 68 65 20 74 79 70 65 20 61 6e 64 20  ** the type and 
15652 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62  parent page numb
15653 65 72 20 74 6f 20 2a 70 45 54 79 70 65 20 61 6e  er to *pEType an
15654 64 20 2a 70 50 67 6e 6f 20 72 65 73 70 65 63 74  d *pPgno respect
15655 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 72  ively..** An err
15656 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
15657 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 6e 67  ned if something
15658 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68   goes wrong, oth
15659 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b  erwise SQLITE_OK
1565a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1565b 70 74 72 6d 61 70 47 65 74 28 42 74 53 68 61 72  ptrmapGet(BtShar
1565c 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65  ed *pBt, Pgno ke
1565d 79 2c 20 75 38 20 2a 70 45 54 79 70 65 2c 20 50  y, u8 *pEType, P
1565e 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 44  gno *pPgno){.  D
1565f 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20  bPage *pDbPage; 
15660 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72    /* The pointer
15661 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69   map page */.  i
15662 6e 74 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20  nt iPtrmap;     
15663 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70    /* Pointer map
15664 20 70 61 67 65 20 69 6e 64 65 78 20 2a 2f 0a 20   page index */. 
15665 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20   u8 *pPtrmap;   
15666 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d      /* Pointer m
15667 61 70 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a  ap page data */.
15668 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20    int offset;   
15669 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f       /* Offset o
1566a 66 20 65 6e 74 72 79 20 69 6e 20 70 6f 69 6e 74  f entry in point
1566b 65 72 20 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 20  er map */.  int 
1566c 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  rc;..  assert( s
1566d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1566e 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
1566f 0a 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 50 54  ..  iPtrmap = PT
15670 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c  RMAP_PAGENO(pBt,
15671 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 73 71   key);.  rc = sq
15672 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42  lite3PagerGet(pB
15673 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 72 6d  t->pPager, iPtrm
15674 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20  ap, &pDbPage);. 
15675 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20   if( rc!=0 ){.  
15676 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
15677 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38  .  pPtrmap = (u8
15678 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47   *)sqlite3PagerG
15679 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b  etData(pDbPage);
1567a 0a 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52  ..  offset = PTR
1567b 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50  MAP_PTROFFSET(iP
1567c 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 61  trmap, key);.  a
1567d 73 73 65 72 74 28 20 70 45 54 79 70 65 21 3d 30  ssert( pEType!=0
1567e 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20 3d 20   );.  *pEType = 
1567f 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b  pPtrmap[offset];
15680 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29 20 2a  .  if( pPgno ) *
15681 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65  pPgno = get4byte
15682 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74  (&pPtrmap[offset
15683 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  +1]);..  sqlite3
15684 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61  PagerUnref(pDbPa
15685 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45 54 79  ge);.  if( *pETy
15686 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e  pe<1 || *pEType>
15687 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  5 ) return SQLIT
15688 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
15689 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1568a 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20  OK;.}..#else /* 
1568b 69 66 20 64 65 66 69 6e 65 64 20 53 51 4c 49 54  if defined SQLIT
1568c 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1568d 4d 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 70  M */.  #define p
1568e 74 72 6d 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a  trmapPut(w,x,y,z
1568f 2c 72 63 29 0a 20 20 23 64 65 66 69 6e 65 20 70  ,rc).  #define p
15690 74 72 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a  trmapGet(w,x,y,z
15691 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64  ) SQLITE_OK.  #d
15692 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 4f  efine ptrmapPutO
15693 76 66 6c 50 74 72 28 78 2c 20 79 2c 20 72 63 29  vflPtr(x, y, rc)
15694 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47  .#endif../*.** G
15695 69 76 65 6e 20 61 20 62 74 72 65 65 20 70 61 67  iven a btree pag
15696 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64  e and a cell ind
15697 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68 65 20  ex (0 means the 
15698 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a  first cell on.**
15699 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d 65 61   the page, 1 mea
1569a 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20 63 65  ns the second ce
1569b 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  ll, and so forth
1569c 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  ) return a point
1569d 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c  er.** to the cel
1569e 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a  l content..**.**
1569f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f   This routine wo
156a0 72 6b 73 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67  rks only for pag
156a1 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 63  es that do not c
156a2 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20  ontain overflow 
156a3 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  cells..*/.#defin
156a4 65 20 66 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20  e findCell(P,I) 
156a5 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74 61 20  \.  ((P)->aData 
156a6 2b 20 28 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65  + ((P)->maskPage
156a7 20 26 20 67 65 74 32 62 79 74 65 28 26 28 50 29   & get2byte(&(P)
156a8 2d 3e 61 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c  ->aData[(P)->cel
156a9 6c 4f 66 66 73 65 74 2b 32 2a 28 49 29 5d 29 29  lOffset+2*(I)]))
156aa 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20  )../*.** This a 
156ab 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 76 65 72  more complex ver
156ac 73 69 6f 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c  sion of findCell
156ad 28 29 20 74 68 61 74 20 77 6f 72 6b 73 20 66 6f  () that works fo
156ae 72 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20  r.** pages that 
156af 64 6f 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66  do contain overf
156b0 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74  low cells..*/.st
156b1 61 74 69 63 20 75 38 20 2a 66 69 6e 64 4f 76 65  atic u8 *findOve
156b2 72 66 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67  rflowCell(MemPag
156b3 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43  e *pPage, int iC
156b4 65 6c 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ell){.  int i;. 
156b5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
156b6 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
156b7 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
156b8 3b 0a 20 20 66 6f 72 28 69 3d 70 50 61 67 65 2d  ;.  for(i=pPage-
156b9 3e 6e 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e  >nOverflow-1; i>
156ba 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e  =0; i--){.    in
156bb 74 20 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20  t k;.    struct 
156bc 5f 4f 76 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c  _OvflCell *pOvfl
156bd 3b 0a 20 20 20 20 70 4f 76 66 6c 20 3d 20 26 70  ;.    pOvfl = &p
156be 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a  Page->aOvfl[i];.
156bf 20 20 20 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69      k = pOvfl->i
156c0 64 78 3b 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69  dx;.    if( k<=i
156c1 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66  Cell ){.      if
156c2 28 20 6b 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20  ( k==iCell ){.  
156c3 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 4f 76        return pOv
156c4 66 6c 2d 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20  fl->pCell;.     
156c5 20 7d 0a 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d   }.      iCell--
156c6 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
156c7 74 75 72 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50  turn findCell(pP
156c8 61 67 65 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a  age, iCell);.}..
156c9 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 63 65  /*.** Parse a ce
156ca 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b  ll content block
156cb 20 61 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68 65   and fill in the
156cc 20 43 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74   CellInfo struct
156cd 75 72 65 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61  ure.  There.** a
156ce 72 65 20 74 77 6f 20 76 65 72 73 69 6f 6e 73 20  re two versions 
156cf 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  of this function
156d0 2e 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c  .  btreeParseCel
156d1 6c 28 29 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20  l() takes a .** 
156d2 63 65 6c 6c 20 69 6e 64 65 78 20 61 73 20 74 68  cell index as th
156d3 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
156d4 74 20 61 6e 64 20 62 74 72 65 65 50 61 72 73 65  t and btreeParse
156d5 43 65 6c 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61  CellPtr() .** ta
156d6 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  kes a pointer to
156d7 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65   the body of the
156d8 20 63 65 6c 6c 20 61 73 20 69 74 73 20 73 65 63   cell as its sec
156d9 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  ond argument..**
156da 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20  .** Within this 
156db 66 69 6c 65 2c 20 74 68 65 20 70 61 72 73 65 43  file, the parseC
156dc 65 6c 6c 28 29 20 6d 61 63 72 6f 20 63 61 6e 20  ell() macro can 
156dd 62 65 20 63 61 6c 6c 65 64 20 69 6e 73 74 65 61  be called instea
156de 64 20 6f 66 0a 2a 2a 20 62 74 72 65 65 50 61 72  d of.** btreePar
156df 73 65 43 65 6c 6c 50 74 72 28 29 2e 20 55 73 69  seCellPtr(). Usi
156e0 6e 67 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72  ng some compiler
156e1 73 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20  s, this will be 
156e2 66 61 73 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  faster..*/.stati
156e3 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 73  c void btreePars
156e4 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50  eCellPtr(.  MemP
156e5 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20  age *pPage,     
156e6 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74      /* Page cont
156e7 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20  aining the cell 
156e8 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20  */.  u8 *pCell, 
156e9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
156ea 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  Pointer to the c
156eb 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43  ell text. */.  C
156ec 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20  ellInfo *pInfo  
156ed 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69         /* Fill i
156ee 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
156ef 20 2a 2f 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20   */.){.  u16 n; 
156f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156f1 20 2f 2a 20 4e 75 6d 62 65 72 20 62 79 74 65 73   /* Number bytes
156f2 20 69 6e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74   in cell content
156f3 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32   header */.  u32
156f4 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 20 20 20   nPayload;      
156f5 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
156f6 66 20 62 79 74 65 73 20 6f 66 20 63 65 6c 6c 20  f bytes of cell 
156f7 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73  payload */..  as
156f8 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
156f9 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
156fa 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  pBt->mutex) );..
156fb 20 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d    pInfo->pCell =
156fc 20 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74   pCell;.  assert
156fd 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30  ( pPage->leaf==0
156fe 20 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d   || pPage->leaf=
156ff 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67  =1 );.  n = pPag
15700 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b  e->childPtrSize;
15701 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d  .  assert( n==4-
15702 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b  4*pPage->leaf );
15703 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e  .  if( pPage->in
15704 74 4b 65 79 20 29 7b 0a 20 20 20 20 69 66 28 20  tKey ){.    if( 
15705 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29  pPage->hasData )
15706 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 67 65 74  {.      n += get
15707 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b  Varint32(&pCell[
15708 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20  n], nPayload);. 
15709 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1570a 6e 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20  nPayload = 0;.  
1570b 20 20 7d 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74    }.    n += get
1570c 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d  Varint(&pCell[n]
1570d 2c 20 28 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e  , (u64*)&pInfo->
1570e 6e 4b 65 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f  nKey);.    pInfo
1570f 2d 3e 6e 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f  ->nData = nPaylo
15710 61 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ad;.  }else{.   
15711 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20   pInfo->nData = 
15712 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56  0;.    n += getV
15713 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e  arint32(&pCell[n
15714 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20  ], nPayload);.  
15715 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20    pInfo->nKey = 
15716 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20  nPayload;.  }.  
15717 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20  pInfo->nPayload 
15718 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49  = nPayload;.  pI
15719 6e 66 6f 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e  nfo->nHeader = n
1571a 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50  ;.  testcase( nP
1571b 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d  ayload==pPage->m
1571c 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73  axLocal );.  tes
1571d 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d  tcase( nPayload=
1571e 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c  =pPage->maxLocal
1571f 2b 31 20 29 3b 0a 20 20 69 66 28 20 6c 69 6b 65  +1 );.  if( like
15720 6c 79 28 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61  ly(nPayload<=pPa
15721 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b  ge->maxLocal) ){
15722 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20  .    /* This is 
15723 74 68 65 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f  the (easy) commo
15724 6e 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65  n case where the
15725 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 20   entire payload 
15726 66 69 74 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74  fits.    ** on t
15727 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20  he local page.  
15728 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 69 73 20 72  No overflow is r
15729 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a  equired..    */.
1572a 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 3b 20 20      int nSize;  
1572b 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
1572c 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 20 63 6f   size of cell co
1572d 6e 74 65 6e 74 20 69 6e 20 62 79 74 65 73 20 2a  ntent in bytes *
1572e 2f 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 6e 50  /.    nSize = nP
1572f 61 79 6c 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 20  ayload + n;.    
15730 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20  pInfo->nLocal = 
15731 28 75 31 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20  (u16)nPayload;. 
15732 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66     pInfo->iOverf
15733 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  low = 0;.    if(
15734 20 28 6e 53 69 7a 65 20 26 20 7e 33 29 3d 3d 30   (nSize & ~3)==0
15735 20 29 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20   ){.      nSize 
15736 3d 20 34 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  = 4;        /* M
15737 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  inimum cell size
15738 20 69 73 20 34 20 2a 2f 0a 20 20 20 20 7d 0a 20   is 4 */.    }. 
15739 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20     pInfo->nSize 
1573a 3d 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a 20 20  = (u16)nSize;.  
1573b 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66  }else{.    /* If
1573c 20 74 68 65 20 70 61 79 6c 6f 61 64 20 77 69 6c   the payload wil
1573d 6c 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65  l not fit comple
1573e 74 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61  tely on the loca
1573f 6c 20 70 61 67 65 2c 20 77 65 20 68 61 76 65 0a  l page, we have.
15740 20 20 20 20 2a 2a 20 74 6f 20 64 65 63 69 64 65      ** to decide
15741 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 74 6f   how much to sto
15742 72 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68  re locally and h
15743 6f 77 20 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c  ow much to spill
15744 20 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65   onto.    ** ove
15745 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68  rflow pages.  Th
15746 65 20 73 74 72 61 74 65 67 79 20 69 73 20 74 6f  e strategy is to
15747 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d   minimize the am
15748 6f 75 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a 20  ount of unused. 
15749 20 20 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f     ** space on o
1574a 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 77 68  verflow pages wh
1574b 69 6c 65 20 6b 65 65 70 69 6e 67 20 74 68 65 20  ile keeping the 
1574c 61 6d 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20  amount of local 
1574d 73 74 6f 72 61 67 65 0a 20 20 20 20 2a 2a 20 69  storage.    ** i
1574e 6e 20 62 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63  n between minLoc
1574f 61 6c 20 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e  al and maxLocal.
15750 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57  .    **.    ** W
15751 61 72 6e 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e  arning:  changin
15752 67 20 74 68 65 20 77 61 79 20 6f 76 65 72 66 6c  g the way overfl
15753 6f 77 20 70 61 79 6c 6f 61 64 20 69 73 20 64 69  ow payload is di
15754 73 74 72 69 62 75 74 65 64 20 69 6e 20 61 6e 79  stributed in any
15755 0a 20 20 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c  .    ** way will
15756 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e   result in an in
15757 63 6f 6d 70 61 74 69 62 6c 65 20 66 69 6c 65 20  compatible file 
15758 66 6f 72 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20  format..    */. 
15759 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b     int minLocal;
1575a 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f    /* Minimum amo
1575b 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68  unt of payload h
1575c 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20  eld locally */. 
1575d 20 20 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b     int maxLocal;
1575e 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f    /* Maximum amo
1575f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68  unt of payload h
15760 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20  eld locally */. 
15761 20 20 20 69 6e 74 20 73 75 72 70 6c 75 73 3b 20     int surplus; 
15762 20 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61    /* Overflow pa
15763 79 6c 6f 61 64 20 61 76 61 69 6c 61 62 6c 65 20  yload available 
15764 66 6f 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67  for local storag
15765 65 20 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63  e */..    minLoc
15766 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c  al = pPage->minL
15767 6f 63 61 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63  ocal;.    maxLoc
15768 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c  al = pPage->maxL
15769 6f 63 61 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75  ocal;.    surplu
1576a 73 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28  s = minLocal + (
1576b 6e 50 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f  nPayload - minLo
1576c 63 61 6c 29 25 28 70 50 61 67 65 2d 3e 70 42 74  cal)%(pPage->pBt
1576d 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34  ->usableSize - 4
1576e 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
1576f 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63   surplus==maxLoc
15770 61 6c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  al );.    testca
15771 73 65 28 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78  se( surplus==max
15772 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69  Local+1 );.    i
15773 66 28 20 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61  f( surplus <= ma
15774 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20  xLocal ){.      
15775 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20  pInfo->nLocal = 
15776 28 75 31 36 29 73 75 72 70 6c 75 73 3b 0a 20 20  (u16)surplus;.  
15777 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
15778 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28  Info->nLocal = (
15779 75 31 36 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20  u16)minLocal;.  
1577a 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69    }.    pInfo->i
1577b 4f 76 65 72 66 6c 6f 77 20 3d 20 28 75 31 36 29  Overflow = (u16)
1577c 28 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b  (pInfo->nLocal +
1577d 20 6e 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e   n);.    pInfo->
1577e 6e 53 69 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69  nSize = pInfo->i
1577f 4f 76 65 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20  Overflow + 4;.  
15780 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 70 61 72 73  }.}.#define pars
15781 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65  eCell(pPage, iCe
15782 6c 6c 2c 20 70 49 6e 66 6f 29 20 5c 0a 20 20 62  ll, pInfo) \.  b
15783 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
15784 28 28 70 50 61 67 65 29 2c 20 66 69 6e 64 43 65  ((pPage), findCe
15785 6c 6c 28 28 70 50 61 67 65 29 2c 20 28 69 43 65  ll((pPage), (iCe
15786 6c 6c 29 29 2c 20 28 70 49 6e 66 6f 29 29 0a 73  ll)), (pInfo)).s
15787 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65  tatic void btree
15788 50 61 72 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d  ParseCell(.  Mem
15789 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20  Page *pPage,    
1578a 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e       /* Page con
1578b 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c  taining the cell
1578c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c   */.  int iCell,
1578d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1578e 20 54 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e   The cell index.
1578f 20 20 46 69 72 73 74 20 63 65 6c 6c 20 69 73 20    First cell is 
15790 30 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20  0 */.  CellInfo 
15791 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20 2f  *pInfo         /
15792 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 73  * Fill in this s
15793 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20  tructure */.){. 
15794 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65   parseCell(pPage
15795 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b  , iCell, pInfo);
15796 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74  .}../*.** Comput
15797 65 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  e the total numb
15798 65 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 74  er of bytes that
15799 20 61 20 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e   a Cell needs in
1579a 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74   the cell.** dat
1579b 61 20 61 72 65 61 20 6f 66 20 74 68 65 20 62 74  a area of the bt
1579c 72 65 65 2d 70 61 67 65 2e 20 20 54 68 65 20 72  ree-page.  The r
1579d 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63  eturn number inc
1579e 6c 75 64 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a  ludes the cell.*
1579f 2a 20 64 61 74 61 20 68 65 61 64 65 72 20 61 6e  * data header an
157a0 64 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c  d the local payl
157a1 6f 61 64 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79  oad, but not any
157a2 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6f   overflow page o
157a3 72 0a 2a 2a 20 74 68 65 20 73 70 61 63 65 20 75  r.** the space u
157a4 73 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c 20  sed by the cell 
157a5 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74  pointer..*/.stat
157a6 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 50  ic u16 cellSizeP
157a7 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  tr(MemPage *pPag
157a8 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20  e, u8 *pCell){. 
157a9 20 75 38 20 2a 70 49 74 65 72 20 3d 20 26 70 43   u8 *pIter = &pC
157aa 65 6c 6c 5b 70 50 61 67 65 2d 3e 63 68 69 6c 64  ell[pPage->child
157ab 50 74 72 53 69 7a 65 5d 3b 0a 20 20 75 33 32 20  PtrSize];.  u32 
157ac 6e 53 69 7a 65 3b 0a 0a 23 69 66 64 65 66 20 53  nSize;..#ifdef S
157ad 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a  QLITE_DEBUG.  /*
157ae 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
157af 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63  ned by this func
157b0 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61  tion should alwa
157b1 79 73 20 62 65 20 74 68 65 20 73 61 6d 65 20 61  ys be the same a
157b2 73 0a 20 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c  s.  ** the (Cell
157b3 49 6e 66 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75  Info.nSize) valu
157b4 65 20 66 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67  e found by doing
157b5 20 61 20 66 75 6c 6c 20 70 61 72 73 65 20 6f 66   a full parse of
157b6 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20   the.  ** cell. 
157b7 49 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  If SQLITE_DEBUG 
157b8 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 20 61  is defined, an a
157b9 73 73 65 72 74 28 29 20 61 74 20 74 68 65 20 62  ssert() at the b
157ba 6f 74 74 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68  ottom of.  ** th
157bb 69 73 20 66 75 6e 63 74 69 6f 6e 20 76 65 72 69  is function veri
157bc 66 69 65 73 20 74 68 61 74 20 74 68 69 73 20 69  fies that this i
157bd 6e 76 61 72 69 61 6e 74 20 69 73 20 6e 6f 74 20  nvariant is not 
157be 76 69 6f 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43  violated. */.  C
157bf 65 6c 6c 49 6e 66 6f 20 64 65 62 75 67 69 6e 66  ellInfo debuginf
157c0 6f 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43  o;.  btreeParseC
157c1 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
157c2 65 6c 6c 2c 20 26 64 65 62 75 67 69 6e 66 6f 29  ell, &debuginfo)
157c3 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  ;.#endif..  if( 
157c4 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
157c5 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 0a 20  .    u8 *pEnd;. 
157c6 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61     if( pPage->ha
157c7 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 70  sData ){.      p
157c8 49 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69 6e  Iter += getVarin
157c9 74 33 32 28 70 49 74 65 72 2c 20 6e 53 69 7a 65  t32(pIter, nSize
157ca 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
157cb 20 20 20 20 6e 53 69 7a 65 20 3d 20 30 3b 0a 20      nSize = 0;. 
157cc 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 74     }..    /* pIt
157cd 65 72 20 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74  er now points at
157ce 20 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74 65   the 64-bit inte
157cf 67 65 72 20 6b 65 79 20 76 61 6c 75 65 2c 20 61  ger key value, a
157d0 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   variable length
157d1 20 0a 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72   .    ** integer
157d2 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
157d3 62 6c 6f 63 6b 20 6d 6f 76 65 73 20 70 49 74 65  block moves pIte
157d4 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  r to point at th
157d5 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20 20  e first byte.   
157d6 20 2a 2a 20 70 61 73 74 20 74 68 65 20 65 6e 64   ** past the end
157d7 20 6f 66 20 74 68 65 20 6b 65 79 20 76 61 6c 75   of the key valu
157d8 65 2e 20 2a 2f 0a 20 20 20 20 70 45 6e 64 20 3d  e. */.    pEnd =
157d9 20 26 70 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20   &pIter[9];.    
157da 77 68 69 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b  while( (*pIter++
157db 29 26 30 78 38 30 20 26 26 20 70 49 74 65 72 3c  )&0x80 && pIter<
157dc 70 45 6e 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  pEnd );.  }else{
157dd 0a 20 20 20 20 70 49 74 65 72 20 2b 3d 20 67 65  .    pIter += ge
157de 74 56 61 72 69 6e 74 33 32 28 70 49 74 65 72 2c  tVarint32(pIter,
157df 20 6e 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20   nSize);.  }..  
157e0 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d  testcase( nSize=
157e1 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c  =pPage->maxLocal
157e2 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
157e3 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61  nSize==pPage->ma
157e4 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66  xLocal+1 );.  if
157e5 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d  ( nSize>pPage->m
157e6 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69  axLocal ){.    i
157e7 6e 74 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50  nt minLocal = pP
157e8 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20  age->minLocal;. 
157e9 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f     nSize = minLo
157ea 63 61 6c 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d  cal + (nSize - m
157eb 69 6e 4c 6f 63 61 6c 29 20 25 20 28 70 50 61 67  inLocal) % (pPag
157ec 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
157ed 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 74 65 73  ze - 4);.    tes
157ee 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50  tcase( nSize==pP
157ef 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b  age->maxLocal );
157f0 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e  .    testcase( n
157f1 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78  Size==pPage->max
157f2 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69  Local+1 );.    i
157f3 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e  f( nSize>pPage->
157f4 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20  maxLocal ){.    
157f5 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63    nSize = minLoc
157f6 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53  al;.    }.    nS
157f7 69 7a 65 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20  ize += 4;.  }.  
157f8 6e 53 69 7a 65 20 2b 3d 20 28 75 33 32 29 28 70  nSize += (u32)(p
157f9 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a  Iter - pCell);..
157fa 20 20 2f 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d    /* The minimum
157fb 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c   size of any cel
157fc 6c 20 69 73 20 34 20 62 79 74 65 73 2e 20 2a 2f  l is 4 bytes. */
157fd 0a 20 20 69 66 28 20 6e 53 69 7a 65 3c 34 20 29  .  if( nSize<4 )
157fe 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b  {.    nSize = 4;
157ff 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
15800 6e 53 69 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f  nSize==debuginfo
15801 2e 6e 53 69 7a 65 20 29 3b 0a 20 20 72 65 74 75  .nSize );.  retu
15802 72 6e 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d  rn (u16)nSize;.}
15803 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
15804 44 45 42 55 47 0a 2f 2a 20 54 68 69 73 20 76 61  DEBUG./* This va
15805 72 69 61 74 69 6f 6e 20 6f 6e 20 63 65 6c 6c 53  riation on cellS
15806 69 7a 65 50 74 72 28 29 20 69 73 20 75 73 65 64  izePtr() is used
15807 20 69 6e 73 69 64 65 20 6f 66 20 61 73 73 65 72   inside of asser
15808 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a  t() statements.*
15809 2a 20 6f 6e 6c 79 2e 20 2a 2f 0a 73 74 61 74 69  * only. */.stati
1580a 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 28 4d  c u16 cellSize(M
1580b 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
1580c 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 72 65 74  nt iCell){.  ret
1580d 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  urn cellSizePtr(
1580e 70 50 61 67 65 2c 20 66 69 6e 64 43 65 6c 6c 28  pPage, findCell(
1580f 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 29 3b 0a  pPage, iCell));.
15810 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
15811 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
15812 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49  TOVACUUM./*.** I
15813 66 20 74 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c  f the cell pCell
15814 2c 20 70 61 72 74 20 6f 66 20 70 61 67 65 20 70  , part of page p
15815 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 61 20  Page contains a 
15816 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e  pointer.** to an
15817 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20   overflow page, 
15818 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 79 20  insert an entry 
15819 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72  into the pointer
1581a 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20  -map.** for the 
1581b 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a  overflow page..*
1581c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74  /.static void pt
1581d 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 4d  rmapPutOvflPtr(M
1581e 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75  emPage *pPage, u
1581f 38 20 2a 70 43 65 6c 6c 2c 20 69 6e 74 20 2a 70  8 *pCell, int *p
15820 52 43 29 7b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20  RC){.  CellInfo 
15821 69 6e 66 6f 3b 0a 20 20 69 66 28 20 2a 70 52 43  info;.  if( *pRC
15822 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
15823 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b  ert( pCell!=0 );
15824 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c  .  btreeParseCel
15825 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  lPtr(pPage, pCel
15826 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73  l, &info);.  ass
15827 65 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61  ert( (info.nData
15828 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f  +(pPage->intKey?
15829 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69  0:info.nKey))==i
1582a 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a  nfo.nPayload );.
1582b 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72    if( info.iOver
1582c 66 6c 6f 77 20 29 7b 0a 20 20 20 20 50 67 6e 6f  flow ){.    Pgno
1582d 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65   ovfl = get4byte
1582e 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76  (&pCell[info.iOv
1582f 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 70 74  erflow]);.    pt
15830 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d 3e 70  rmapPut(pPage->p
15831 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d 41 50  Bt, ovfl, PTRMAP
15832 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 61 67  _OVERFLOW1, pPag
15833 65 2d 3e 70 67 6e 6f 2c 20 70 52 43 29 3b 0a 20  e->pgno, pRC);. 
15834 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a   }.}.#endif.../*
15835 0a 2a 2a 20 44 65 66 72 61 67 6d 65 6e 74 20 74  .** Defragment t
15836 68 65 20 70 61 67 65 20 67 69 76 65 6e 2e 20 20  he page given.  
15837 41 6c 6c 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f  All Cells are mo
15838 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e  ved to the.** en
15839 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e  d of the page an
1583a 64 20 61 6c 6c 20 66 72 65 65 20 73 70 61 63 65  d all free space
1583b 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e   is collected in
1583c 74 6f 20 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72  to one.** big Fr
1583d 65 65 42 6c 6b 20 74 68 61 74 20 6f 63 63 75 72  eeBlk that occur
1583e 73 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65  s in between the
1583f 20 68 65 61 64 65 72 20 61 6e 64 20 63 65 6c 6c   header and cell
15840 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 72 72 61  .** pointer arra
15841 79 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63  y and the cell c
15842 6f 6e 74 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a  ontent area..*/.
15843 73 74 61 74 69 63 20 69 6e 74 20 64 65 66 72 61  static int defra
15844 67 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50 61 67  gmentPage(MemPag
15845 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74  e *pPage){.  int
15846 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
15847 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
15848 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
15849 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   pc;            
1584a 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
1584b 73 73 20 6f 66 20 61 20 69 2d 74 68 20 63 65 6c  ss of a i-th cel
1584c 6c 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20  l */.  int hdr; 
1584d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1584e 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74    /* Offset to t
1584f 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a  he page header *
15850 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20  /.  int size;   
15851 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15852 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c  * Size of a cell
15853 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65   */.  int usable
15854 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
15855 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73   /* Number of us
15856 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 61 20  able bytes on a 
15857 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65  page */.  int ce
15858 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20  llOffset;       
15859 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
1585a 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74  o the cell point
1585b 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e  er array */.  in
1585c 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20 20 20  t cbrk;         
1585d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
1585e 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63  et to the cell c
1585f 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20  ontent area */. 
15860 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20   int nCell;     
15861 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
15862 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
15863 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20  n the page */.  
15864 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64  unsigned char *d
15865 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  ata;       /* Th
15866 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20  e page data */. 
15867 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
15868 74 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a 20 54  temp;       /* T
15869 65 6d 70 20 61 72 65 61 20 66 6f 72 20 63 65 6c  emp area for cel
1586a 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69  l content */.  i
1586b 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 3b 20 20  nt iCellFirst;  
1586c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
1586d 73 74 20 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c  st allowable cel
1586e 6c 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  l index */.  int
1586f 20 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20   iCellLast;     
15870 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20          /* Last 
15871 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c 20 69 6e  possible cell in
15872 64 65 78 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72  dex */...  asser
15873 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
15874 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
15875 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
15876 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
15877 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  Bt!=0 );.  asser
15878 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  t( pPage->pBt->u
15879 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 51 4c  sableSize <= SQL
1587a 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
1587b 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  E );.  assert( p
1587c 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d  Page->nOverflow=
1587d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1587e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1587f 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
15880 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d 70 20  utex) );.  temp 
15881 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 65  = sqlite3PagerTe
15882 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d 3e 70  mpSpace(pPage->p
15883 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 64  Bt->pPager);.  d
15884 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
15885 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67  ta;.  hdr = pPag
15886 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
15887 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61  cellOffset = pPa
15888 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a  ge->cellOffset;.
15889 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d    nCell = pPage-
1588a 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74  >nCell;.  assert
1588b 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 79 74  ( nCell==get2byt
1588c 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 20  e(&data[hdr+3]) 
1588d 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20  );.  usableSize 
1588e 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  = pPage->pBt->us
1588f 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 72 6b  ableSize;.  cbrk
15890 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
15891 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 6d  a[hdr+5]);.  mem
15892 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b 5d 2c  cpy(&temp[cbrk],
15893 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73   &data[cbrk], us
15894 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 6b 29  ableSize - cbrk)
15895 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 62 6c  ;.  cbrk = usabl
15896 65 53 69 7a 65 3b 0a 20 20 69 43 65 6c 6c 46 69  eSize;.  iCellFi
15897 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74  rst = cellOffset
15898 20 2b 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 69 43   + 2*nCell;.  iC
15899 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65  ellLast = usable
1589a 53 69 7a 65 20 2d 20 34 3b 0a 20 20 66 6f 72 28  Size - 4;.  for(
1589b 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b  i=0; i<nCell; i+
1589c 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 64  +){.    u8 *pAdd
1589d 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d  r;     /* The i-
1589e 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  th cell pointer 
1589f 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d 20 26  */.    pAddr = &
158a0 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20  data[cellOffset 
158a1 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d  + i*2];.    pc =
158a2 20 67 65 74 32 62 79 74 65 28 70 41 64 64 72 29   get2byte(pAddr)
158a3 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
158a4 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 20 29  pc==iCellFirst )
158a5 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
158a6 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b  pc==iCellLast );
158a7 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
158a8 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52  LITE_ENABLE_OVER
158a9 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29  SIZE_CELL_CHECK)
158aa 0a 20 20 20 20 2f 2a 20 54 68 65 73 65 20 63 6f  .    /* These co
158ab 6e 64 69 74 69 6f 6e 73 20 68 61 76 65 20 61 6c  nditions have al
158ac 72 65 61 64 79 20 62 65 65 6e 20 76 65 72 69 66  ready been verif
158ad 69 65 64 20 69 6e 20 62 74 72 65 65 49 6e 69 74  ied in btreeInit
158ae 50 61 67 65 28 29 0a 20 20 20 20 2a 2a 20 69 66  Page().    ** if
158af 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f   SQLITE_ENABLE_O
158b0 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45  VERSIZE_CELL_CHE
158b1 43 4b 20 69 73 20 64 65 66 69 6e 65 64 20 0a 20  CK is defined . 
158b2 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63     */.    if( pc
158b3 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70  <iCellFirst || p
158b4 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20  c>iCellLast ){. 
158b5 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
158b6 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
158b7 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
158b8 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 69 43    assert( pc>=iC
158b9 65 6c 6c 46 69 72 73 74 20 26 26 20 70 63 3c 3d  ellFirst && pc<=
158ba 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20  iCellLast );.   
158bb 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65   size = cellSize
158bc 50 74 72 28 70 50 61 67 65 2c 20 26 74 65 6d 70  Ptr(pPage, &temp
158bd 5b 70 63 5d 29 3b 0a 20 20 20 20 63 62 72 6b 20  [pc]);.    cbrk 
158be 2d 3d 20 73 69 7a 65 3b 0a 23 69 66 20 64 65 66  -= size;.#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 69 66 28 20  _CHECK).    if( 
158c2 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20  cbrk<iCellFirst 
158c3 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
158c4 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
158c5 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65  KPT;.    }.#else
158c6 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c 69 43  .    if( cbrk<iC
158c7 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 2b 73  ellFirst || pc+s
158c8 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29  ize>usableSize )
158c9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
158ca 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
158cb 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  PT;.    }.#endif
158cc 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 62 72  .    assert( cbr
158cd 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69  k+size<=usableSi
158ce 7a 65 20 26 26 20 63 62 72 6b 3e 3d 69 43 65 6c  ze && cbrk>=iCel
158cf 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 74 65  lFirst );.    te
158d0 73 74 63 61 73 65 28 20 63 62 72 6b 2b 73 69 7a  stcase( cbrk+siz
158d1 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b  e==usableSize );
158d2 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
158d3 63 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69  c+size==usableSi
158d4 7a 65 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  ze );.    memcpy
158d5 28 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74  (&data[cbrk], &t
158d6 65 6d 70 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a  emp[pc], size);.
158d7 20 20 20 20 70 75 74 32 62 79 74 65 28 70 41 64      put2byte(pAd
158d8 64 72 2c 20 63 62 72 6b 29 3b 0a 20 20 7d 0a 20  dr, cbrk);.  }. 
158d9 20 61 73 73 65 72 74 28 20 63 62 72 6b 3e 3d 69   assert( cbrk>=i
158da 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 70  CellFirst );.  p
158db 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  ut2byte(&data[hd
158dc 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20 20 64  r+5], cbrk);.  d
158dd 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30 3b 0a  ata[hdr+1] = 0;.
158de 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20 3d 20    data[hdr+2] = 
158df 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d  0;.  data[hdr+7]
158e0 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26   = 0;.  memset(&
158e1 64 61 74 61 5b 69 43 65 6c 6c 46 69 72 73 74 5d  data[iCellFirst]
158e2 2c 20 30 2c 20 63 62 72 6b 2d 69 43 65 6c 6c 46  , 0, cbrk-iCellF
158e3 69 72 73 74 29 3b 0a 20 20 61 73 73 65 72 74 28  irst);.  assert(
158e4 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
158e5 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
158e6 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66  pDbPage) );.  if
158e7 28 20 63 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73  ( cbrk-iCellFirs
158e8 74 21 3d 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  t!=pPage->nFree 
158e9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
158ea 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
158eb 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  T;.  }.  return 
158ec 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
158ed 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79  .** Allocate nBy
158ee 74 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  te bytes of spac
158ef 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68  e from within th
158f0 65 20 42 2d 54 72 65 65 20 70 61 67 65 20 70 61  e B-Tree page pa
158f1 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66  ssed.** as the f
158f2 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 57  irst argument. W
158f3 72 69 74 65 20 69 6e 74 6f 20 2a 70 49 64 78 20  rite into *pIdx 
158f4 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 70  the index into p
158f5 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 0a 2a 2a  Page->aData[].**
158f6 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 62 79   of the first by
158f7 74 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20  te of allocated 
158f8 73 70 61 63 65 2e 20 52 65 74 75 72 6e 20 65 69  space. Return ei
158f9 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  ther SQLITE_OK o
158fa 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f  r.** an error co
158fb 64 65 20 28 75 73 75 61 6c 6c 79 20 53 51 4c 49  de (usually SQLI
158fc 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a  TE_CORRUPT)..**.
158fd 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75  ** The caller gu
158fe 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68  arantees that th
158ff 65 72 65 20 69 73 20 73 75 66 66 69 63 69 65 6e  ere is sufficien
15900 74 20 73 70 61 63 65 20 74 6f 20 6d 61 6b 65 20  t space to make 
15901 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  the.** allocatio
15902 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  n.  This routine
15903 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 64   might need to d
15904 65 66 72 61 67 6d 65 6e 74 20 69 6e 20 6f 72 64  efragment in ord
15905 65 72 20 74 6f 20 62 72 69 6e 67 0a 2a 2a 20 61  er to bring.** a
15906 6c 6c 20 74 68 65 20 73 70 61 63 65 20 74 6f 67  ll the space tog
15907 65 74 68 65 72 2c 20 68 6f 77 65 76 65 72 2e 20  ether, however. 
15908 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
15909 6c 6c 20 61 76 6f 69 64 20 75 73 69 6e 67 0a 2a  ll avoid using.*
1590a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77 6f 20  * the first two 
1590b 62 79 74 65 73 20 70 61 73 74 20 74 68 65 20 63  bytes past the c
1590c 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 65 61  ell pointer area
1590d 20 73 69 6e 63 65 20 70 72 65 73 75 6d 61 62 6c   since presumabl
1590e 79 20 74 68 69 73 0a 2a 2a 20 61 6c 6c 6f 63 61  y this.** alloca
1590f 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 6d 61  tion is being ma
15910 64 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69  de in order to i
15911 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c  nsert a new cell
15912 2c 20 73 6f 20 77 65 20 77 69 6c 6c 0a 2a 2a 20  , so we will.** 
15913 61 6c 73 6f 20 65 6e 64 20 75 70 20 6e 65 65 64  also end up need
15914 69 6e 67 20 61 20 6e 65 77 20 63 65 6c 6c 20 70  ing a new cell p
15915 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  ointer..*/.stati
15916 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 53 70  c int allocateSp
15917 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  ace(MemPage *pPa
15918 67 65 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 69  ge, int nByte, i
15919 6e 74 20 2a 70 49 64 78 29 7b 0a 20 20 63 6f 6e  nt *pIdx){.  con
1591a 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70 50 61  st int hdr = pPa
1591b 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 20  ge->hdrOffset;  
1591c 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65    /* Local cache
1591d 20 6f 66 20 70 50 61 67 65 2d 3e 68 64 72 4f 66   of pPage->hdrOf
1591e 66 73 65 74 20 2a 2f 0a 20 20 75 38 20 2a 20 63  fset */.  u8 * c
1591f 6f 6e 73 74 20 64 61 74 61 20 3d 20 70 50 61 67  onst data = pPag
15920 65 2d 3e 61 44 61 74 61 3b 20 20 20 20 20 20 2f  e->aData;      /
15921 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66  * Local cache of
15922 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f   pPage->aData */
15923 0a 20 20 69 6e 74 20 6e 46 72 61 67 3b 20 20 20  .  int nFrag;   
15924 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15925 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
15926 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20  r of fragmented 
15927 62 79 74 65 73 20 6f 6e 20 70 50 61 67 65 20 2a  bytes on pPage *
15928 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20  /.  int top;    
15929 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1592a 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
1592b 74 20 62 79 74 65 20 6f 66 20 63 65 6c 6c 20 63  t byte of cell c
1592c 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20  ontent area */. 
1592d 20 69 6e 74 20 67 61 70 3b 20 20 20 20 20 20 20   int gap;       
1592e 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f   /* First byte o
1592f 66 20 67 61 70 20 62 65 74 77 65 65 6e 20 63 65  f gap between ce
15930 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20  ll pointers and 
15931 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a  cell content */.
15932 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
15933 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 72 65 74    /* Integer ret
15934 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 0a 20  urn code */.  . 
15935 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15936 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
15937 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
15938 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
15939 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73  age->pBt );.  as
1593a 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1593b 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1593c 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
1593d 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 3d   assert( nByte>=
1593e 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d  0 );  /* Minimum
1593f 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20   cell size is 4 
15940 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
15941 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65  ge->nFree>=nByte
15942 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
15943 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d  age->nOverflow==
15944 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
15945 42 79 74 65 3c 70 50 61 67 65 2d 3e 70 42 74 2d  Byte<pPage->pBt-
15946 3e 75 73 61 62 6c 65 53 69 7a 65 2d 38 20 29 3b  >usableSize-8 );
15947 0a 0a 20 20 6e 46 72 61 67 20 3d 20 64 61 74 61  ..  nFrag = data
15948 5b 68 64 72 2b 37 5d 3b 0a 20 20 61 73 73 65 72  [hdr+7];.  asser
15949 74 28 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66  t( pPage->cellOf
1594a 66 73 65 74 20 3d 3d 20 68 64 72 20 2b 20 31 32  fset == hdr + 12
1594b 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66   - 4*pPage->leaf
1594c 20 29 3b 0a 20 20 67 61 70 20 3d 20 70 50 61 67   );.  gap = pPag
1594d 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20  e->cellOffset + 
1594e 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a  2*pPage->nCell;.
1594f 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65    top = get2byte
15950 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a  (&data[hdr+5]);.
15951 20 20 69 66 28 20 67 61 70 3e 74 6f 70 20 29 20    if( gap>top ) 
15952 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
15953 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 74 65  RRUPT_BKPT;.  te
15954 73 74 63 61 73 65 28 20 67 61 70 2b 32 3d 3d 74  stcase( gap+2==t
15955 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  op );.  testcase
15956 28 20 67 61 70 2b 31 3d 3d 74 6f 70 20 29 3b 0a  ( gap+1==top );.
15957 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 3d    testcase( gap=
15958 3d 74 6f 70 20 29 3b 0a 0a 20 20 69 66 28 20 6e  =top );..  if( n
15959 46 72 61 67 3e 3d 36 30 20 29 7b 0a 20 20 20 20  Frag>=60 ){.    
1595a 2f 2a 20 41 6c 77 61 79 73 20 64 65 66 72 61 67  /* Always defrag
1595b 6d 65 6e 74 20 68 69 67 68 6c 79 20 66 72 61 67  ment highly frag
1595c 6d 65 6e 74 65 64 20 70 61 67 65 73 20 2a 2f 0a  mented pages */.
1595d 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d      rc = defragm
1595e 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a  entPage(pPage);.
1595f 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
15960 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20  urn rc;.    top 
15961 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
15962 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 7d 65 6c 73  [hdr+5]);.  }els
15963 65 20 69 66 28 20 67 61 70 2b 32 3c 3d 74 6f 70  e if( gap+2<=top
15964 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 61 72 63   ){.    /* Searc
15965 68 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 6c  h the freelist l
15966 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66 72 65  ooking for a fre
15967 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f 75 67  e slot big enoug
15968 68 20 74 6f 20 73 61 74 69 73 66 79 20 0a 20 20  h to satisfy .  
15969 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74    ** the request
1596a 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  . The allocation
1596b 20 69 73 20 6d 61 64 65 20 66 72 6f 6d 20 74 68   is made from th
1596c 65 20 66 69 72 73 74 20 66 72 65 65 20 73 6c 6f  e first free slo
1596d 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65  t in .    ** the
1596e 20 6c 69 73 74 20 74 68 61 74 20 69 73 20 6c 61   list that is la
1596f 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 61 63  rge enough to ac
15970 63 6f 6d 61 64 61 74 65 20 69 74 2e 0a 20 20 20  comadate it..   
15971 20 2a 2f 0a 20 20 20 20 69 6e 74 20 70 63 2c 20   */.    int pc, 
15972 61 64 64 72 3b 0a 20 20 20 20 66 6f 72 28 61 64  addr;.    for(ad
15973 64 72 3d 68 64 72 2b 31 3b 20 28 70 63 20 3d 20  dr=hdr+1; (pc = 
15974 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 61  get2byte(&data[a
15975 64 64 72 5d 29 29 3e 30 3b 20 61 64 64 72 3d 70  ddr]))>0; addr=p
15976 63 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69  c){.      int si
15977 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  ze = get2byte(&d
15978 61 74 61 5b 70 63 2b 32 5d 29 3b 20 20 20 20 20  ata[pc+2]);     
15979 2f 2a 20 53 69 7a 65 20 6f 66 20 66 72 65 65 20  /* Size of free 
1597a 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69 66  slot */.      if
1597b 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29 7b  ( size>=nByte ){
1597c 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d  .        int x =
1597d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b 0a 20   size - nByte;. 
1597e 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
1597f 20 78 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 20   x==4 );.       
15980 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d 33 20   testcase( x==3 
15981 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78  );.        if( x
15982 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <4 ){.          
15983 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 6c  /* Remove the sl
15984 6f 74 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65  ot from the free
15985 2d 6c 69 73 74 2e 20 55 70 64 61 74 65 20 74 68  -list. Update th
15986 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20  e number of.    
15987 20 20 20 20 20 20 2a 2a 20 66 72 61 67 6d 65 6e        ** fragmen
15988 74 65 64 20 62 79 74 65 73 20 77 69 74 68 69 6e  ted bytes within
15989 20 74 68 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20   the page. */.  
1598a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
1598b 64 61 74 61 5b 61 64 64 72 5d 2c 20 26 64 61 74  data[addr], &dat
1598c 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20  a[pc], 2);.     
1598d 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d       data[hdr+7]
1598e 20 3d 20 28 75 38 29 28 6e 46 72 61 67 20 2b 20   = (u8)(nFrag + 
1598f 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  x);.        }els
15990 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e{.          /* 
15991 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69 6e 73  The slot remains
15992 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   on the free-lis
15993 74 2e 20 52 65 64 75 63 65 20 69 74 73 20 73 69  t. Reduce its si
15994 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a 20 20  ze to account.  
15995 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74          ** for t
15996 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 65 64 20  he portion used 
15997 62 79 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63  by the new alloc
15998 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20  ation. */.      
15999 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
1599a 74 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a 20 20  ta[pc+2], x);.  
1599b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1599c 2a 70 49 64 78 20 3d 20 70 63 20 2b 20 78 3b 0a  *pIdx = pc + x;.
1599d 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
1599e 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
1599f 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
159a0 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  * Check to make 
159a1 73 75 72 65 20 74 68 65 72 65 20 69 73 20 65 6e  sure there is en
159a2 6f 75 67 68 20 73 70 61 63 65 20 69 6e 20 74 68  ough space in th
159a3 65 20 67 61 70 20 74 6f 20 73 61 74 69 73 66 79  e gap to satisfy
159a4 0a 20 20 2a 2a 20 74 68 65 20 61 6c 6c 6f 63 61  .  ** the alloca
159a5 74 69 6f 6e 2e 20 20 49 66 20 6e 6f 74 2c 20 64  tion.  If not, d
159a6 65 66 72 61 67 6d 65 6e 74 2e 0a 20 20 2a 2f 0a  efragment..  */.
159a7 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 2b    testcase( gap+
159a8 32 2b 6e 42 79 74 65 3d 3d 74 6f 70 20 29 3b 0a  2+nByte==top );.
159a9 20 20 69 66 28 20 67 61 70 2b 32 2b 6e 42 79 74    if( gap+2+nByt
159aa 65 3e 74 6f 70 20 29 7b 0a 20 20 20 20 72 63 20  e>top ){.    rc 
159ab 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65  = defragmentPage
159ac 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  (pPage);.    if(
159ad 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
159ae 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62  .    top = get2b
159af 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
159b0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67  );.    assert( g
159b1 61 70 2b 6e 42 79 74 65 3c 3d 74 6f 70 20 29 3b  ap+nByte<=top );
159b2 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  .  }...  /* Allo
159b3 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 72 6f 6d  cate memory from
159b4 20 74 68 65 20 67 61 70 20 69 6e 20 62 65 74 77   the gap in betw
159b5 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f 69  een the cell poi
159b6 6e 74 65 72 20 61 72 72 61 79 0a 20 20 2a 2a 20  nter array.  ** 
159b7 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e  and the cell con
159b8 74 65 6e 74 20 61 72 65 61 2e 20 20 54 68 65 20  tent area.  The 
159b9 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20  btreeInitPage() 
159ba 63 61 6c 6c 20 68 61 73 20 61 6c 72 65 61 64 79  call has already
159bb 0a 20 20 2a 2a 20 76 61 6c 69 64 61 74 65 64 20  .  ** validated 
159bc 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 47  the freelist.  G
159bd 69 76 65 6e 20 74 68 61 74 20 74 68 65 20 66 72  iven that the fr
159be 65 65 6c 69 73 74 20 69 73 20 76 61 6c 69 64 2c  eelist is valid,
159bf 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 6e   there.  ** is n
159c0 6f 20 77 61 79 20 74 68 61 74 20 74 68 65 20 61  o way that the a
159c1 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 20 65 78  llocation can ex
159c2 74 65 6e 64 20 6f 66 66 20 74 68 65 20 65 6e 64  tend off the end
159c3 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 20 20   of the page..  
159c4 2a 2a 20 54 68 65 20 61 73 73 65 72 74 28 29 20  ** The assert() 
159c5 62 65 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74  below verifies t
159c6 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74  he previous sent
159c7 65 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70  ence..  */.  top
159c8 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 70 75 74   -= nByte;.  put
159c9 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
159ca 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 61 73 73 65  5], top);.  asse
159cb 72 74 28 20 74 6f 70 2b 6e 42 79 74 65 20 3c 3d  rt( top+nByte <=
159cc 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61   pPage->pBt->usa
159cd 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 2a 70 49  bleSize );.  *pI
159ce 64 78 20 3d 20 74 6f 70 3b 0a 20 20 72 65 74 75  dx = top;.  retu
159cf 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
159d0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
159d1 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 70  section of the p
159d2 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f 20 74  Page->aData to t
159d3 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 20  he freelist..** 
159d4 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f  The first byte o
159d5 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 20 62  f the new free b
159d6 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d 3e 61  lock is pPage->a
159d7 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a 20 61  Disk[start].** a
159d8 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  nd the size of t
159d9 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 69 7a  he block is "siz
159da 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  e" bytes..**.** 
159db 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 66 6f  Most of the effo
159dc 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 6f 6c  rt here is invol
159dd 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 6e 67  ved in coalesing
159de 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 72 65   adjacent.** fre
159df 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 61 20  e blocks into a 
159e0 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 65 20  single big free 
159e1 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63  block..*/.static
159e2 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 28 4d   int freeSpace(M
159e3 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
159e4 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20 73 69  nt start, int si
159e5 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c  ze){.  int addr,
159e6 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a 20 20   pbegin, hdr;.  
159e7 69 6e 74 20 69 4c 61 73 74 3b 20 20 20 20 20 20  int iLast;      
159e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
159e9 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 6f 73    /* Largest pos
159ea 73 69 62 6c 65 20 66 72 65 65 62 6c 6f 63 6b 20  sible freeblock 
159eb 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 6e 73 69  offset */.  unsi
159ec 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 20  gned char *data 
159ed 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a  = pPage->aData;.
159ee 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
159ef 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73  ->pBt!=0 );.  as
159f0 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
159f1 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
159f2 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
159f3 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 72 74  .  assert( start
159f4 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  >=pPage->hdrOffs
159f5 65 74 2b 36 2b 70 50 61 67 65 2d 3e 63 68 69 6c  et+6+pPage->chil
159f6 64 50 74 72 53 69 7a 65 20 29 3b 0a 20 20 61 73  dPtrSize );.  as
159f7 73 65 72 74 28 20 28 73 74 61 72 74 20 2b 20 73  sert( (start + s
159f8 69 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70 42 74  ize)<=pPage->pBt
159f9 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a  ->usableSize );.
159fa 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
159fb 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
159fc 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
159fd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  );.  assert( siz
159fe 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d 69 6e  e>=0 );   /* Min
159ff 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69  imum cell size i
15a00 73 20 34 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53  s 4 */..#ifdef S
15a01 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c  QLITE_SECURE_DEL
15a02 45 54 45 0a 20 20 2f 2a 20 4f 76 65 72 77 72 69  ETE.  /* Overwri
15a03 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72  te deleted infor
15a04 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f  mation with zero
15a05 73 20 77 68 65 6e 20 74 68 65 20 53 45 43 55 52  s when the SECUR
15a06 45 5f 44 45 4c 45 54 45 20 0a 20 20 2a 2a 20 6f  E_DELETE .  ** o
15a07 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64  ption is enabled
15a08 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
15a09 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 64 61   */.  memset(&da
15a0a 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c 20 73 69  ta[start], 0, si
15a0b 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  ze);.#endif..  /
15a0c 2a 20 41 64 64 20 74 68 65 20 73 70 61 63 65 20  * Add the space 
15a0d 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 6c 69  back into the li
15a0e 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66 72 65  nked list of fre
15a0f 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 74 65 20 74  eblocks.  Note t
15a10 68 61 74 0a 20 20 2a 2a 20 65 76 65 6e 20 74 68  hat.  ** even th
15a11 6f 75 67 68 20 74 68 65 20 66 72 65 65 62 6c 6f  ough the freeblo
15a12 63 6b 20 6c 69 73 74 20 77 61 73 20 63 68 65 63  ck list was chec
15a13 6b 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74  ked by btreeInit
15a14 50 61 67 65 28 29 2c 0a 20 20 2a 2a 20 62 74 72  Page(),.  ** btr
15a15 65 65 49 6e 69 74 50 61 67 65 28 29 20 64 69 64  eeInitPage() did
15a16 20 6e 6f 74 20 64 65 74 65 63 74 20 6f 76 65 72   not detect over
15a17 6c 61 70 70 69 6e 67 20 63 65 6c 6c 73 20 6f 72  lapping cells or
15a18 0a 20 20 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73  .  ** freeblocks
15a19 20 74 68 61 74 20 6f 76 65 72 6c 61 70 70 65 64   that overlapped
15a1a 20 63 65 6c 6c 73 2e 20 20 20 4e 6f 72 20 64 6f   cells.   Nor do
15a1b 65 73 20 69 74 20 64 65 74 65 63 74 20 77 68 65  es it detect whe
15a1c 6e 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 20  n the.  ** cell 
15a1d 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 65 78 63  content area exc
15a1e 65 65 64 73 20 74 68 65 20 76 61 6c 75 65 20 69  eeds the value i
15a1f 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65  n the page heade
15a20 72 2e 20 20 49 66 20 74 68 65 73 65 0a 20 20 2a  r.  If these.  *
15a21 2a 20 73 69 74 75 61 74 69 6f 6e 73 20 61 72 69  * situations ari
15a22 73 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75  se, then subsequ
15a23 65 6e 74 20 69 6e 73 65 72 74 20 6f 70 65 72 61  ent insert opera
15a24 74 69 6f 6e 73 20 6d 69 67 68 74 20 63 6f 72 72  tions might corr
15a25 75 70 74 0a 20 20 2a 2a 20 74 68 65 20 66 72 65  upt.  ** the fre
15a26 65 6c 69 73 74 2e 20 20 53 6f 20 77 65 20 64 6f  elist.  So we do
15a27 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 66   need to check f
15a28 6f 72 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 68  or corruption wh
15a29 69 6c 65 20 73 63 61 6e 6e 69 6e 67 0a 20 20 2a  ile scanning.  *
15a2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a  * the freelist..
15a2b 20 20 2a 2f 0a 20 20 68 64 72 20 3d 20 70 50 61    */.  hdr = pPa
15a2c 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20  ge->hdrOffset;. 
15a2d 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b   addr = hdr + 1;
15a2e 0a 20 20 69 4c 61 73 74 20 3d 20 70 50 61 67 65  .  iLast = pPage
15a2f 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
15a30 65 20 2d 20 34 3b 0a 20 20 61 73 73 65 72 74 28  e - 4;.  assert(
15a31 20 73 74 61 72 74 3c 3d 69 4c 61 73 74 20 29 3b   start<=iLast );
15a32 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69  .  while( (pbegi
15a33 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  n = get2byte(&da
15a34 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61 72 74  ta[addr]))<start
15a35 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29 7b 0a   && pbegin>0 ){.
15a36 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 3c 61      if( pbegin<a
15a37 64 64 72 2b 34 20 29 7b 0a 20 20 20 20 20 20 72  ddr+4 ){.      r
15a38 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15a39 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
15a3a 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 67  .    addr = pbeg
15a3b 69 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 62  in;.  }.  if( pb
15a3c 65 67 69 6e 3e 69 4c 61 73 74 20 29 7b 0a 20 20  egin>iLast ){.  
15a3d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15a3e 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
15a3f 7d 0a 20 20 61 73 73 65 72 74 28 20 70 62 65 67  }.  assert( pbeg
15a40 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 67 69  in>addr || pbegi
15a41 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 62 79  n==0 );.  put2by
15a42 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20  te(&data[addr], 
15a43 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32 62 79  start);.  put2by
15a44 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c  te(&data[start],
15a45 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 74 32   pbegin);.  put2
15a46 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74  byte(&data[start
15a47 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 70 50  +2], size);.  pP
15a48 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70 50 61  age->nFree = pPa
15a49 67 65 2d 3e 6e 46 72 65 65 20 2b 20 28 75 31 36  ge->nFree + (u16
15a4a 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f 61  )size;..  /* Coa
15a4b 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66  lesce adjacent f
15a4c 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20  ree blocks */.  
15a4d 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b 0a  addr = hdr + 1;.
15a4e 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e    while( (pbegin
15a4f 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
15a50 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20  a[addr]))>0 ){. 
15a51 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73     int pnext, ps
15a52 69 7a 65 2c 20 78 3b 0a 20 20 20 20 61 73 73 65  ize, x;.    asse
15a53 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20  rt( pbegin>addr 
15a54 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
15a55 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42  begin<=pPage->pB
15a56 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20  t->usableSize-4 
15a57 29 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d 20 67  );.    pnext = g
15a58 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62  et2byte(&data[pb
15a59 65 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69 7a  egin]);.    psiz
15a5a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  e = get2byte(&da
15a5b 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20  ta[pbegin+2]);. 
15a5c 20 20 20 69 66 28 20 70 62 65 67 69 6e 20 2b 20     if( pbegin + 
15a5d 70 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e 65  psize + 3 >= pne
15a5e 78 74 20 26 26 20 70 6e 65 78 74 3e 30 20 29 7b  xt && pnext>0 ){
15a5f 0a 20 20 20 20 20 20 69 6e 74 20 66 72 61 67 20  .      int frag 
15a60 3d 20 70 6e 65 78 74 20 2d 20 28 70 62 65 67 69  = pnext - (pbegi
15a61 6e 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20 20 20  n+psize);.      
15a62 69 66 28 20 28 66 72 61 67 3c 30 29 20 7c 7c 20  if( (frag<0) || 
15a63 28 66 72 61 67 3e 28 69 6e 74 29 64 61 74 61 5b  (frag>(int)data[
15a64 68 64 72 2b 37 5d 29 20 29 7b 0a 20 20 20 20 20  hdr+7]) ){.     
15a65 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15a66 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
15a67 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 61 74       }.      dat
15a68 61 5b 68 64 72 2b 37 5d 20 2d 3d 20 28 75 38 29  a[hdr+7] -= (u8)
15a69 66 72 61 67 3b 0a 20 20 20 20 20 20 78 20 3d 20  frag;.      x = 
15a6a 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
15a6b 6e 65 78 74 5d 29 3b 0a 20 20 20 20 20 20 70 75  next]);.      pu
15a6c 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65  t2byte(&data[pbe
15a6d 67 69 6e 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20  gin], x);.      
15a6e 78 20 3d 20 70 6e 65 78 74 20 2b 20 67 65 74 32  x = pnext + get2
15a6f 62 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74  byte(&data[pnext
15a70 2b 32 5d 29 20 2d 20 70 62 65 67 69 6e 3b 0a 20  +2]) - pbegin;. 
15a71 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64       put2byte(&d
15a72 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 2c 20 78  ata[pbegin+2], x
15a73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
15a74 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 67 69      addr = pbegi
15a75 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
15a76 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 63  /* If the cell c
15a77 6f 6e 74 65 6e 74 20 61 72 65 61 20 62 65 67 69  ontent area begi
15a78 6e 73 20 77 69 74 68 20 61 20 66 72 65 65 62 6c  ns with a freebl
15a79 6f 63 6b 2c 20 72 65 6d 6f 76 65 20 69 74 2e 20  ock, remove it. 
15a7a 2a 2f 0a 20 20 69 66 28 20 64 61 74 61 5b 68 64  */.  if( data[hd
15a7b 72 2b 31 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 35  r+1]==data[hdr+5
15a7c 5d 20 26 26 20 64 61 74 61 5b 68 64 72 2b 32 5d  ] && data[hdr+2]
15a7d 3d 3d 64 61 74 61 5b 68 64 72 2b 36 5d 20 29 7b  ==data[hdr+6] ){
15a7e 0a 20 20 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20  .    int top;.  
15a7f 20 20 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62    pbegin = get2b
15a80 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d  yte(&data[hdr+1]
15a81 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64  );.    memcpy(&d
15a82 61 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64 61 74  ata[hdr+1], &dat
15a83 61 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b 0a 20  a[pbegin], 2);. 
15a84 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74     top = get2byt
15a85 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20  e(&data[hdr+5]) 
15a86 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  + get2byte(&data
15a87 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20  [pbegin+2]);.   
15a88 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
15a89 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20  hdr+5], top);.  
15a8a 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  }.  assert( sqli
15a8b 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
15a8c 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
15a8d 67 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ge) );.  return 
15a8e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
15a8f 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65 20 66  .** Decode the f
15a90 6c 61 67 73 20 62 79 74 65 20 28 74 68 65 20 66  lags byte (the f
15a91 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65  irst byte of the
15a92 20 68 65 61 64 65 72 29 20 66 6f 72 20 61 20 70   header) for a p
15a93 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 69  age.** and initi
15a94 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f 66 20  alize fields of 
15a95 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74 72 75  the MemPage stru
15a96 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e 67 6c  cture accordingl
15a97 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68  y..**.** Only th
15a98 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62  e following comb
15a99 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73 75 70  inations are sup
15a9a 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68 69 6e  ported.  Anythin
15a9b 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 69  g different.** i
15a9c 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72 72 75  ndicates a corru
15a9d 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  pt database file
15a9e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
15a9f 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a 2a 2a   PTF_ZERODATA.**
15aa0 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52           PTF_ZER
15aa1 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 41 46  ODATA | PTF_LEAF
15aa2 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f  .**         PTF_
15aa3 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49  LEAFDATA | PTF_I
15aa4 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20 20 20  NTKEY.**        
15aa5 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20   PTF_LEAFDATA | 
15aa6 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50 54 46  PTF_INTKEY | PTF
15aa7 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69 63 20  _LEAF.*/.static 
15aa8 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67 73 28  int decodeFlags(
15aa9 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
15aaa 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b 0a 20  int flagByte){. 
15aab 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20   BtShared *pBt; 
15aac 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66      /* A copy of
15aad 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f 0a 0a   pPage->pBt */..
15aae 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15aaf 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70 50 61  >hdrOffset==(pPa
15ab0 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30  ge->pgno==1 ? 10
15ab1 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73 73 65  0 : 0) );.  asse
15ab2 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15ab3 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
15ab4 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  t->mutex) );.  p
15ab5 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28 75 38  Page->leaf = (u8
15ab6 29 28 66 6c 61 67 42 79 74 65 3e 3e 33 29 3b 20  )(flagByte>>3); 
15ab7 20 61 73 73 65 72 74 28 20 50 54 46 5f 4c 45 41   assert( PTF_LEA
15ab8 46 20 3d 3d 20 31 3c 3c 33 20 29 3b 0a 20 20 66  F == 1<<3 );.  f
15ab9 6c 61 67 42 79 74 65 20 26 3d 20 7e 50 54 46 5f  lagByte &= ~PTF_
15aba 4c 45 41 46 3b 0a 20 20 70 50 61 67 65 2d 3e 63  LEAF;.  pPage->c
15abb 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20 34 2d  hildPtrSize = 4-
15abc 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20  4*pPage->leaf;. 
15abd 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42   pBt = pPage->pB
15abe 74 3b 0a 20 20 69 66 28 20 66 6c 61 67 42 79 74  t;.  if( flagByt
15abf 65 3d 3d 28 50 54 46 5f 4c 45 41 46 44 41 54 41  e==(PTF_LEAFDATA
15ac0 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 29 20 29   | PTF_INTKEY) )
15ac1 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74  {.    pPage->int
15ac2 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 70 50 61  Key = 1;.    pPa
15ac3 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 70 50  ge->hasData = pP
15ac4 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 70  age->leaf;.    p
15ac5 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d  Page->maxLocal =
15ac6 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 3b 0a 20   pBt->maxLeaf;. 
15ac7 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63     pPage->minLoc
15ac8 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 65 61  al = pBt->minLea
15ac9 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  f;.  }else if( f
15aca 6c 61 67 42 79 74 65 3d 3d 50 54 46 5f 5a 45 52  lagByte==PTF_ZER
15acb 4f 44 41 54 41 20 29 7b 0a 20 20 20 20 70 50 61  ODATA ){.    pPa
15acc 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 30 3b 0a  ge->intKey = 0;.
15acd 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61      pPage->hasDa
15ace 74 61 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  ta = 0;.    pPag
15acf 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42  e->maxLocal = pB
15ad0 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20  t->maxLocal;.   
15ad1 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c   pPage->minLocal
15ad2 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c   = pBt->minLocal
15ad3 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
15ad4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15ad5 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20  RUPT_BKPT;.  }. 
15ad6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
15ad7 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  K;.}../*.** Init
15ad8 69 61 6c 69 7a 65 20 74 68 65 20 61 75 78 69 6c  ialize the auxil
15ad9 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  iary information
15ada 20 66 6f 72 20 61 20 64 69 73 6b 20 62 6c 6f 63   for a disk bloc
15adb 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  k..**.** Return 
15adc 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
15add 63 65 73 73 2e 20 20 49 66 20 77 65 20 73 65 65  cess.  If we see
15ade 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 64   that the page d
15adf 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61  oes.** not conta
15ae0 69 6e 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  in a well-formed
15ae1 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20   database page, 
15ae2 74 68 65 6e 20 72 65 74 75 72 6e 20 0a 2a 2a 20  then return .** 
15ae3 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 20  SQLITE_CORRUPT. 
15ae4 20 4e 6f 74 65 20 74 68 61 74 20 61 20 72 65 74   Note that a ret
15ae5 75 72 6e 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4b  urn of SQLITE_OK
15ae6 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 67 75 61   does not.** gua
15ae7 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20  rantee that the 
15ae8 70 61 67 65 20 69 73 20 77 65 6c 6c 2d 66 6f 72  page is well-for
15ae9 6d 65 64 2e 20 20 49 74 20 6f 6e 6c 79 20 73 68  med.  It only sh
15aea 6f 77 73 20 74 68 61 74 0a 2a 2a 20 77 65 20 66  ows that.** we f
15aeb 61 69 6c 65 64 20 74 6f 20 64 65 74 65 63 74 20  ailed to detect 
15aec 61 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a  any corruption..
15aed 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74  */.static int bt
15aee 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65 6d 50  reeInitPage(MemP
15aef 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 0a 20 20  age *pPage){..  
15af0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
15af1 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  Bt!=0 );.  asser
15af2 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
15af3 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
15af4 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
15af5 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e  sert( pPage->pgn
15af6 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 50  o==sqlite3PagerP
15af7 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d  agenumber(pPage-
15af8 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61  >pDbPage) );.  a
15af9 73 73 65 72 74 28 20 70 50 61 67 65 20 3d 3d 20  ssert( pPage == 
15afa 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
15afb 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50  xtra(pPage->pDbP
15afc 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74  age) );.  assert
15afd 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d  ( pPage->aData =
15afe 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
15aff 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62  tData(pPage->pDb
15b00 50 61 67 65 29 20 29 3b 0a 0a 20 20 69 66 28 20  Page) );..  if( 
15b01 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29  !pPage->isInit )
15b02 7b 0a 20 20 20 20 75 31 36 20 70 63 3b 20 20 20  {.    u16 pc;   
15b03 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
15b04 65 73 73 20 6f 66 20 61 20 66 72 65 65 62 6c 6f  ess of a freeblo
15b05 63 6b 20 77 69 74 68 69 6e 20 70 50 61 67 65 2d  ck within pPage-
15b06 3e 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20  >aData[] */.    
15b07 75 38 20 68 64 72 3b 20 20 20 20 20 20 20 20 20  u8 hdr;         
15b08 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20     /* Offset to 
15b09 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 70 61 67  beginning of pag
15b0a 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20  e header */.    
15b0b 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20  u8 *data;       
15b0c 20 20 20 2f 2a 20 45 71 75 61 6c 20 74 6f 20 70     /* Equal to p
15b0d 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20  Page->aData */. 
15b0e 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74     BtShared *pBt
15b0f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
15b10 6d 61 69 6e 20 62 74 72 65 65 20 73 74 72 75 63  main btree struc
15b11 74 75 72 65 20 2a 2f 0a 20 20 20 20 75 31 36 20  ture */.    u16 
15b12 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 2f  usableSize;    /
15b13 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75 73 61 62  * Amount of usab
15b14 6c 65 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68  le space on each
15b15 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 75 31 36   page */.    u16
15b16 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20   cellOffset;    
15b17 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 73  /* Offset from s
15b18 74 61 72 74 20 6f 66 20 70 61 67 65 20 74 6f 20  tart of page to 
15b19 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74  first cell point
15b1a 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20 6e 46  er */.    u16 nF
15b1b 72 65 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ree;         /* 
15b1c 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 64  Number of unused
15b1d 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61   bytes on the pa
15b1e 67 65 20 2a 2f 0a 20 20 20 20 75 31 36 20 74 6f  ge */.    u16 to
15b1f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p;           /* 
15b20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  First byte of th
15b21 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  e cell content a
15b22 72 65 61 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  rea */.    int i
15b23 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 2f 2a  CellFirst;    /*
15b24 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62 6c 65   First allowable
15b25 20 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 6c 6f   cell or freeblo
15b26 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 20  ck offset */.   
15b27 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74 3b 20   int iCellLast; 
15b28 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 73      /* Last poss
15b29 69 62 6c 65 20 63 65 6c 6c 20 6f 72 20 66 72 65  ible cell or fre
15b2a 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f  eblock offset */
15b2b 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50 61 67  ..    pBt = pPag
15b2c 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68 64 72  e->pBt;..    hdr
15b2d 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
15b2e 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20 3d 20  set;.    data = 
15b2f 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20  pPage->aData;.  
15b30 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c 61 67    if( decodeFlag
15b31 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b 68 64  s(pPage, data[hd
15b32 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c  r]) ) return SQL
15b33 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15b34 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
15b35 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32  t->pageSize>=512
15b36 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a   && pBt->pageSiz
15b37 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 20 20  e<=32768 );.    
15b38 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20  pPage->maskPage 
15b39 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  = pBt->pageSize 
15b3a 2d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  - 1;.    pPage->
15b3b 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20  nOverflow = 0;. 
15b3c 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20     usableSize = 
15b3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b  pBt->usableSize;
15b3e 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c  .    pPage->cell
15b3f 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66  Offset = cellOff
15b40 73 65 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d  set = hdr + 12 -
15b41 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a   4*pPage->leaf;.
15b42 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79      top = get2by
15b43 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
15b44 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65  ;.    pPage->nCe
15b45 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  ll = get2byte(&d
15b46 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20  ata[hdr+3]);.   
15b47 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c   if( pPage->nCel
15b48 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 29  l>MX_CELL(pBt) )
15b49 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 6d 61  {.      /* To ma
15b4a 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 20 73  ny cells for a s
15b4b 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54 68 65  ingle page.  The
15b4c 20 70 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f   page must be co
15b4d 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20 20 72  rrupt */.      r
15b4e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15b4f 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
15b50 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
15b51 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 4d 58 5f  Page->nCell==MX_
15b52 43 45 4c 4c 28 70 42 74 29 20 29 3b 0a 0a 20 20  CELL(pBt) );..  
15b53 20 20 2f 2a 20 41 20 6d 61 6c 66 6f 72 6d 65 64    /* A malformed
15b54 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 6d   database page m
15b55 69 67 68 74 20 63 61 75 73 65 20 75 73 20 74 6f  ight cause us to
15b56 20 72 65 61 64 20 70 61 73 74 20 74 68 65 20 65   read past the e
15b57 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 70 61 67  nd.    ** of pag
15b58 65 20 77 68 65 6e 20 70 61 72 73 69 6e 67 20 61  e when parsing a
15b59 20 63 65 6c 6c 2e 20 20 0a 20 20 20 20 2a 2a 0a   cell.  .    **.
15b5a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f      ** The follo
15b5b 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 63 6f  wing block of co
15b5c 64 65 20 63 68 65 63 6b 73 20 65 61 72 6c 79 20  de checks early 
15b5d 74 6f 20 73 65 65 20 69 66 20 61 20 63 65 6c 6c  to see if a cell
15b5e 20 65 78 74 65 6e 64 73 0a 20 20 20 20 2a 2a 20   extends.    ** 
15b5f 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
15b60 61 20 70 61 67 65 20 62 6f 75 6e 64 61 72 79 20  a page boundary 
15b61 61 6e 64 20 63 61 75 73 65 73 20 53 51 4c 49 54  and causes SQLIT
15b62 45 5f 43 4f 52 52 55 50 54 20 74 6f 20 62 65 20  E_CORRUPT to be 
15b63 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64  .    ** returned
15b64 20 69 66 20 69 74 20 64 6f 65 73 2e 0a 20 20 20   if it does..   
15b65 20 2a 2f 0a 20 20 20 20 69 43 65 6c 6c 46 69 72   */.    iCellFir
15b66 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20  st = cellOffset 
15b67 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  + 2*pPage->nCell
15b68 3b 0a 20 20 20 20 69 43 65 6c 6c 4c 61 73 74 20  ;.    iCellLast 
15b69 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34  = usableSize - 4
15b6a 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ;.#if defined(SQ
15b6b 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52  LITE_ENABLE_OVER
15b6c 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29  SIZE_CELL_CHECK)
15b6d 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74  .    {.      int
15b6e 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   i;            /
15b6f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 74 68 65  * Index into the
15b70 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72   cell pointer ar
15b71 72 61 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ray */.      int
15b72 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f   sz;           /
15b73 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c  * Size of a cell
15b74 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 20 21   */..      if( !
15b75 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43  pPage->leaf ) iC
15b76 65 6c 6c 4c 61 73 74 2d 2d 3b 0a 20 20 20 20 20  ellLast--;.     
15b77 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67   for(i=0; i<pPag
15b78 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a  e->nCell; i++){.
15b79 20 20 20 20 20 20 20 20 70 63 20 3d 20 67 65 74          pc = get
15b7a 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c  2byte(&data[cell
15b7b 4f 66 66 73 65 74 2b 69 2a 32 5d 29 3b 0a 20 20  Offset+i*2]);.  
15b7c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
15b7d 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 20 29  pc==iCellFirst )
15b7e 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
15b7f 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 73  se( pc==iCellLas
15b80 74 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  t );.        if(
15b81 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c   pc<iCellFirst |
15b82 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29  | pc>iCellLast )
15b83 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
15b84 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
15b85 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20  T_BKPT;.        
15b86 7d 0a 20 20 20 20 20 20 20 20 73 7a 20 3d 20 63  }.        sz = c
15b87 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65  ellSizePtr(pPage
15b88 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20  , &data[pc]);.  
15b89 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
15b8a 70 63 2b 73 7a 3d 3d 75 73 61 62 6c 65 53 69 7a  pc+sz==usableSiz
15b8b 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  e );.        if(
15b8c 20 70 63 2b 73 7a 3e 75 73 61 62 6c 65 53 69 7a   pc+sz>usableSiz
15b8d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  e ){.          r
15b8e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15b8f 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
15b90 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
15b91 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
15b92 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2b  eaf ) iCellLast+
15b93 2b 3b 0a 20 20 20 20 7d 20 20 0a 23 65 6e 64 69  +;.    }  .#endi
15b94 66 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74  f..    /* Comput
15b95 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72 65 65  e the total free
15b96 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61   space on the pa
15b97 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 67  ge */.    pc = g
15b98 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
15b99 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65 65  r+1]);.    nFree
15b9a 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 2b   = data[hdr+7] +
15b9b 20 74 6f 70 3b 0a 20 20 20 20 77 68 69 6c 65 28   top;.    while(
15b9c 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 75   pc>0 ){.      u
15b9d 31 36 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a 20  16 next, size;. 
15b9e 20 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c       if( pc<iCel
15b9f 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65  lFirst || pc>iCe
15ba0 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20  llLast ){.      
15ba1 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 66 72    /* Start of fr
15ba2 65 65 20 62 6c 6f 63 6b 20 69 73 20 6f 66 66 20  ee block is off 
15ba3 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 20  the page */.    
15ba4 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
15ba5 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20  E_CORRUPT_BKPT; 
15ba6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e  .      }.      n
15ba7 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26  ext = get2byte(&
15ba8 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20  data[pc]);.     
15ba9 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65   size = get2byte
15baa 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20  (&data[pc+2]);. 
15bab 20 20 20 20 20 69 66 28 20 28 6e 65 78 74 3e 30       if( (next>0
15bac 20 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73 69 7a   && next<=pc+siz
15bad 65 2b 33 29 20 7c 7c 20 70 63 2b 73 69 7a 65 3e  e+3) || pc+size>
15bae 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20  usableSize ){.  
15baf 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c        /* Free bl
15bb0 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69 6e 20  ocks must be in 
15bb1 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e  ascending order.
15bb2 20 41 6e 64 20 74 68 65 20 6c 61 73 74 20 62 79   And the last by
15bb3 74 65 20 6f 66 0a 09 2a 2a 20 74 68 65 20 66 72  te of..** the fr
15bb4 65 65 2d 62 6c 6f 63 6b 20 6d 75 73 74 20 6c 69  ee-block must li
15bb5 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  e on the databas
15bb6 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20  e page.  */.    
15bb7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
15bb8 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20  E_CORRUPT_BKPT; 
15bb9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e  .      }.      n
15bba 46 72 65 65 20 3d 20 6e 46 72 65 65 20 2b 20 73  Free = nFree + s
15bbb 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 3d 20  ize;.      pc = 
15bbc 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  next;.    }..   
15bbd 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   /* At this poin
15bbe 74 2c 20 6e 46 72 65 65 20 63 6f 6e 74 61 69 6e  t, nFree contain
15bbf 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65  s the sum of the
15bc0 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 73   offset to the s
15bc1 74 61 72 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74  tart.    ** of t
15bc2 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 20  he cell-content 
15bc3 61 72 65 61 20 70 6c 75 73 20 74 68 65 20 6e 75  area plus the nu
15bc4 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62 79 74  mber of free byt
15bc5 65 73 20 77 69 74 68 69 6e 0a 20 20 20 20 2a 2a  es within.    **
15bc6 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e   the cell-conten
15bc7 74 20 61 72 65 61 2e 20 49 66 20 74 68 69 73 20  t area. If this 
15bc8 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
15bc9 74 68 65 20 75 73 61 62 6c 65 2d 73 69 7a 65 0a  the usable-size.
15bca 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61      ** of the pa
15bcb 67 65 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67  ge, then the pag
15bcc 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70  e must be corrup
15bcd 74 65 64 2e 20 54 68 69 73 20 63 68 65 63 6b 20  ted. This check 
15bce 61 6c 73 6f 0a 20 20 20 20 2a 2a 20 73 65 72 76  also.    ** serv
15bcf 65 73 20 74 6f 20 76 65 72 69 66 79 20 74 68 61  es to verify tha
15bd0 74 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20  t the offset to 
15bd1 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
15bd2 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 0a 20 20   cell-content.  
15bd3 20 20 2a 2a 20 61 72 65 61 2c 20 61 63 63 6f 72    ** area, accor
15bd4 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61 67 65  ding to the page
15bd5 20 68 65 61 64 65 72 2c 20 6c 69 65 73 20 77 69   header, lies wi
15bd6 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e 0a 20  thin the page.. 
15bd7 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 46     */.    if( nF
15bd8 72 65 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29  ree>usableSize )
15bd9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
15bda 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
15bdb 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20 70  PT; .    }.    p
15bdc 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75  Page->nFree = (u
15bdd 31 36 29 28 6e 46 72 65 65 20 2d 20 69 43 65 6c  16)(nFree - iCel
15bde 6c 46 69 72 73 74 29 3b 0a 20 20 20 20 70 50 61  lFirst);.    pPa
15bdf 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a  ge->isInit = 1;.
15be0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
15be1 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
15be2 20 53 65 74 20 75 70 20 61 20 72 61 77 20 70 61   Set up a raw pa
15be3 67 65 20 73 6f 20 74 68 61 74 20 69 74 20 6c 6f  ge so that it lo
15be4 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 74 61 62  oks like a datab
15be5 61 73 65 20 70 61 67 65 20 68 6f 6c 64 69 6e 67  ase page holding
15be6 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 73 2e 0a  .** no entries..
15be7 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 7a  */.static void z
15be8 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 67 65 20  eroPage(MemPage 
15be9 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67  *pPage, int flag
15bea 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  s){.  unsigned c
15beb 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67  har *data = pPag
15bec 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 74 53 68  e->aData;.  BtSh
15bed 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67  ared *pBt = pPag
15bee 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 68 64 72  e->pBt;.  u8 hdr
15bef 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
15bf0 73 65 74 3b 0a 20 20 75 31 36 20 66 69 72 73 74  set;.  u16 first
15bf1 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
15bf2 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d  ite3PagerPagenum
15bf3 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ber(pPage->pDbPa
15bf4 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f  ge)==pPage->pgno
15bf5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
15bf6 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
15bf7 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  ra(pPage->pDbPag
15bf8 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61  e) == (void*)pPa
15bf9 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ge );.  assert( 
15bfa 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
15bfb 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ata(pPage->pDbPa
15bfc 67 65 29 20 3d 3d 20 64 61 74 61 20 29 3b 0a 20  ge) == data );. 
15bfd 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15bfe 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
15bff 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
15c00 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
15c01 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
15c02 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
15c03 20 20 2f 2a 6d 65 6d 73 65 74 28 26 64 61 74 61    /*memset(&data
15c04 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d 3e 75  [hdr], 0, pBt->u
15c05 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64 72 29  sableSize - hdr)
15c06 3b 2a 2f 0a 20 20 64 61 74 61 5b 68 64 72 5d 20  ;*/.  data[hdr] 
15c07 3d 20 28 63 68 61 72 29 66 6c 61 67 73 3b 0a 20  = (char)flags;. 
15c08 20 66 69 72 73 74 20 3d 20 68 64 72 20 2b 20 38   first = hdr + 8
15c09 20 2b 20 34 2a 28 28 66 6c 61 67 73 26 50 54 46   + 4*((flags&PTF
15c0a 5f 4c 45 41 46 29 3d 3d 30 20 3f 31 3a 30 29 3b  _LEAF)==0 ?1:0);
15c0b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b  .  memset(&data[
15c0c 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 3b 0a 20  hdr+1], 0, 4);. 
15c0d 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 30   data[hdr+7] = 0
15c0e 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
15c0f 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 74 2d 3e  ta[hdr+5], pBt->
15c10 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 70  usableSize);.  p
15c11 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70 42  Page->nFree = pB
15c12 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
15c13 66 69 72 73 74 3b 0a 20 20 64 65 63 6f 64 65 46  first;.  decodeF
15c14 6c 61 67 73 28 70 50 61 67 65 2c 20 66 6c 61 67  lags(pPage, flag
15c15 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72  s);.  pPage->hdr
15c16 4f 66 66 73 65 74 20 3d 20 68 64 72 3b 0a 20 20  Offset = hdr;.  
15c17 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65  pPage->cellOffse
15c18 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70 50 61  t = first;.  pPa
15c19 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20  ge->nOverflow = 
15c1a 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  0;.  assert( pBt
15c1b 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20  ->pageSize>=512 
15c1c 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  && pBt->pageSize
15c1d 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 70 50 61  <=32768 );.  pPa
15c1e 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 70  ge->maskPage = p
15c1f 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31  Bt->pageSize - 1
15c20 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ;.  pPage->nCell
15c21 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 69   = 0;.  pPage->i
15c22 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f  sInit = 1;.}.../
15c23 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 44  *.** Convert a D
15c24 62 50 61 67 65 20 6f 62 74 61 69 6e 65 64 20 66  bPage obtained f
15c25 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 69 6e  rom the pager in
15c26 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 75 73 65  to a MemPage use
15c27 64 20 62 79 0a 2a 2a 20 74 68 65 20 62 74 72 65  d by.** the btre
15c28 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74 61 74  e layer..*/.stat
15c29 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 72 65  ic MemPage *btre
15c2a 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28  ePageFromDbPage(
15c2b 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 2c  DbPage *pDbPage,
15c2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 74 53 68   Pgno pgno, BtSh
15c2d 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65  ared *pBt){.  Me
15c2e 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 28  mPage *pPage = (
15c2f 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69 74 65 33  MemPage*)sqlite3
15c30 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44  PagerGetExtra(pD
15c31 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65 2d  bPage);.  pPage-
15c32 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33  >aData = sqlite3
15c33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62  PagerGetData(pDb
15c34 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e  Page);.  pPage->
15c35 70 44 62 50 61 67 65 20 3d 20 70 44 62 50 61 67  pDbPage = pDbPag
15c36 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70 42 74 20  e;.  pPage->pBt 
15c37 3d 20 70 42 74 3b 0a 20 20 70 50 61 67 65 2d 3e  = pBt;.  pPage->
15c38 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 70  pgno = pgno;.  p
15c39 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20  Page->hdrOffset 
15c3a 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31  = pPage->pgno==1
15c3b 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20 20 72 65   ? 100 : 0;.  re
15c3c 74 75 72 6e 20 70 50 61 67 65 3b 20 0a 7d 0a 0a  turn pPage; .}..
15c3d 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 67 65  /*.** Get a page
15c3e 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 2e   from the pager.
15c3f 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65    Initialize the
15c40 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64   MemPage.pBt and
15c41 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74  .** MemPage.aDat
15c42 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65  a elements if ne
15c43 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  eded..**.** If t
15c44 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 66 6c 61  he noContent fla
15c45 67 20 69 73 20 73 65 74 2c 20 69 74 20 6d 65 61  g is set, it mea
15c46 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f  ns that we do no
15c47 74 20 63 61 72 65 20 61 62 6f 75 74 0a 2a 2a 20  t care about.** 
15c48 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
15c49 68 65 20 70 61 67 65 20 61 74 20 74 68 69 73 20  he page at this 
15c4a 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 6e 6f 74  time.  So do not
15c4b 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 6b 0a   go to the disk.
15c4c 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 68 65 20  ** to fetch the 
15c4d 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 74 20 66  content.  Just f
15c4e 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f 6e 74 65  ill in the conte
15c4f 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 20 66 6f  nt with zeros fo
15c50 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69 6e 20  r now..** If in 
15c51 74 68 65 20 66 75 74 75 72 65 20 77 65 20 63 61  the future we ca
15c52 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  ll sqlite3PagerW
15c53 72 69 74 65 28 29 20 6f 6e 20 74 68 69 73 20 70  rite() on this p
15c54 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 6d 65 61  age, that.** mea
15c55 6e 73 20 77 65 20 68 61 76 65 20 73 74 61 72 74  ns we have start
15c56 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 65 72 6e  ed to be concern
15c57 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 65 6e 74  ed about content
15c58 20 61 6e 64 20 74 68 65 20 64 69 73 6b 0a 2a 2a   and the disk.**
15c59 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f 63 63   read should occ
15c5a 75 72 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74  ur at that point
15c5b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
15c5c 62 74 72 65 65 47 65 74 50 61 67 65 28 0a 20 20  btreeGetPage(.  
15c5d 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20  BtShared *pBt,  
15c5e 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65       /* The btre
15c5f 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
15c60 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
15c61 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67  umber of the pag
15c62 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20  e to fetch */.  
15c63 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65  MemPage **ppPage
15c64 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74  ,    /* Return t
15c65 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 20  he page in this 
15c66 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 69  parameter */.  i
15c67 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 20  nt noContent    
15c68 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 6c 6f      /* Do not lo
15c69 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20  ad page content 
15c6a 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20  if true */.){.  
15c6b 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61 67 65  int rc;.  DbPage
15c6c 20 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20 61 73   *pDbPage;..  as
15c6d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15c6e 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
15c6f 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73  tex) );.  rc = s
15c70 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69  qlite3PagerAcqui
15c71 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  re(pBt->pPager, 
15c72 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a 2a 29  pgno, (DbPage**)
15c73 26 70 44 62 50 61 67 65 2c 20 6e 6f 43 6f 6e 74  &pDbPage, noCont
15c74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ent);.  if( rc )
15c75 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70   return rc;.  *p
15c76 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 61 67  pPage = btreePag
15c77 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44 62 50  eFromDbPage(pDbP
15c78 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 29 3b  age, pgno, pBt);
15c79 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15c7a 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  _OK;.}../*.** Re
15c7b 74 72 69 65 76 65 20 61 20 70 61 67 65 20 66 72  trieve a page fr
15c7c 6f 6d 20 74 68 65 20 70 61 67 65 72 20 63 61 63  om the pager cac
15c7d 68 65 2e 20 49 66 20 74 68 65 20 72 65 71 75 65  he. If the reque
15c7e 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74  sted page is not
15c7f 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 74  .** already in t
15c80 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 72  he pager cache r
15c81 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 49 6e 69 74  eturn NULL. Init
15c82 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61  ialize the MemPa
15c83 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65  ge.pBt and.** Me
15c84 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d  mPage.aData elem
15c85 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0a  ents if needed..
15c86 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67  */.static MemPag
15c87 65 20 2a 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b  e *btreePageLook
15c88 75 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  up(BtShared *pBt
15c89 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20  , Pgno pgno){.  
15c8a 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b  DbPage *pDbPage;
15c8b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15c8c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
15c8d 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  t->mutex) );.  p
15c8e 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33  DbPage = sqlite3
15c8f 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42 74 2d  PagerLookup(pBt-
15c90 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a  >pPager, pgno);.
15c91 20 20 69 66 28 20 70 44 62 50 61 67 65 20 29 7b    if( pDbPage ){
15c92 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74 72 65  .    return btre
15c93 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28  ePageFromDbPage(
15c94 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70  pDbPage, pgno, p
15c95 42 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Bt);.  }.  retur
15c96 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n 0;.}../*.** Re
15c97 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  turn the size of
15c98 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
15c99 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49 66 20  le in pages. If 
15c9a 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b 69 6e  there is any kin
15c9b 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c 20 72  d of.** error, r
15c9c 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e 65 64  eturn ((unsigned
15c9d 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73 74 61   int)-1)..*/.sta
15c9e 74 69 63 20 50 67 6e 6f 20 70 61 67 65 72 50 61  tic Pgno pagerPa
15c9f 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72 65 64  gecount(BtShared
15ca0 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 6e 50   *pBt){.  int nP
15ca1 61 67 65 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20  age = -1;.  int 
15ca2 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42  rc;.  assert( pB
15ca3 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 20 72  t->pPage1 );.  r
15ca4 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
15ca5 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70  Pagecount(pBt->p
15ca6 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a  Pager, &nPage);.
15ca7 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
15ca8 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61 67 65  LITE_OK || nPage
15ca9 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 74 75 72 6e  ==-1 );.  return
15caa 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 7d 0a   (Pgno)nPage;.}.
15cab 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 67  ./*.** Get a pag
15cac 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72  e from the pager
15cad 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20   and initialize 
15cae 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  it.  This routin
15caf 65 20 69 73 20 6a 75 73 74 20 61 0a 2a 2a 20 63  e is just a.** c
15cb0 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70  onvenience wrapp
15cb1 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61 72 61  er around separa
15cb2 74 65 20 63 61 6c 6c 73 20 74 6f 20 62 74 72 65  te calls to btre
15cb3 65 47 65 74 50 61 67 65 28 29 20 61 6e 64 20 0a  eGetPage() and .
15cb4 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65  ** btreeInitPage
15cb5 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ()..**.** If an 
15cb6 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68  error occurs, th
15cb7 65 6e 20 74 68 65 20 76 61 6c 75 65 20 2a 70 70  en the value *pp
15cb8 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 69  Page is set to i
15cb9 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 49 74 0a  s undefined. It.
15cba 2a 2a 20 6d 61 79 20 72 65 6d 61 69 6e 20 75 6e  ** may remain un
15cbb 63 68 61 6e 67 65 64 2c 20 6f 72 20 69 74 20 6d  changed, or it m
15cbc 61 79 20 62 65 20 73 65 74 20 74 6f 20 61 6e 20  ay be set to an 
15cbd 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 2e 0a 2a  invalid value..*
15cbe 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
15cbf 41 6e 64 49 6e 69 74 50 61 67 65 28 0a 20 20 42  AndInitPage(.  B
15cc0 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20  tShared *pBt,   
15cc1 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
15cc2 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
15cc3 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20   Pgno pgno,     
15cc4 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
15cc5 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 67  of the page to g
15cc6 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  et */.  MemPage 
15cc7 2a 2a 70 70 50 61 67 65 20 20 20 20 20 2f 2a 20  **ppPage     /* 
15cc8 57 72 69 74 65 20 74 68 65 20 70 61 67 65 20 70  Write the page p
15cc9 6f 69 6e 74 65 72 20 68 65 72 65 20 2a 2f 0a 29  ointer here */.)
15cca 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 45  {.  int rc;.  TE
15ccb 53 54 4f 4e 4c 59 28 20 50 67 6e 6f 20 69 4c 61  STONLY( Pgno iLa
15ccc 73 74 50 67 20 3d 20 70 61 67 65 72 50 61 67 65  stPg = pagerPage
15ccd 63 6f 75 6e 74 28 70 42 74 29 3b 20 29 0a 20 20  count(pBt); ).  
15cce 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
15ccf 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
15cd0 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 72 63 20  mutex) );..  rc 
15cd1 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
15cd2 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65  Bt, pgno, ppPage
15cd3 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , 0);.  if( rc==
15cd4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15cd5 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69 74 50   rc = btreeInitP
15cd6 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20  age(*ppPage);.  
15cd7 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
15cd8 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c  _OK ){.      rel
15cd9 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65  easePage(*ppPage
15cda 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
15cdb 2f 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73  /* If the reques
15cdc 74 65 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ted page number 
15cdd 77 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20  was either 0 or 
15cde 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65  greater than the
15cdf 20 70 61 67 65 0a 20 20 2a 2a 20 6e 75 6d 62 65   page.  ** numbe
15ce0 72 20 6f 66 20 74 68 65 20 6c 61 73 74 20 70 61  r of the last pa
15ce1 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ge in the databa
15ce2 73 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  se, this functio
15ce3 6e 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 0a  n should return.
15ce4 20 20 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52    ** SQLITE_CORR
15ce5 55 50 54 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65  UPT or some othe
15ce6 72 20 65 72 72 6f 72 20 28 69 2e 65 2e 20 53 51  r error (i.e. SQ
15ce7 4c 49 54 45 5f 46 55 4c 4c 29 2e 20 43 68 65 63  LITE_FULL). Chec
15ce8 6b 20 74 68 61 74 20 74 68 69 73 0a 20 20 2a 2a  k that this.  **
15ce9 20 69 73 20 74 68 65 20 63 61 73 65 2e 20 20 2a   is the case.  *
15cea 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 67 6e  /.  assert( (pgn
15ceb 6f 3e 30 20 26 26 20 70 67 6e 6f 3c 3d 69 4c 61  o>0 && pgno<=iLa
15cec 73 74 50 67 29 20 7c 7c 20 72 63 21 3d 53 51 4c  stPg) || rc!=SQL
15ced 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 74 65 73 74  ITE_OK );.  test
15cee 63 61 73 65 28 20 70 67 6e 6f 3d 3d 30 20 29 3b  case( pgno==0 );
15cef 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 67 6e  .  testcase( pgn
15cf0 6f 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a 0a 20  o==iLastPg );.. 
15cf1 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
15cf2 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 4d  *.** Release a M
15cf3 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20 73 68  emPage.  This sh
15cf4 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 6f  ould be called o
15cf5 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70 72 69  nce for each pri
15cf6 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74  or.** call to bt
15cf7 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f 0a 73  reeGetPage..*/.s
15cf8 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61  tatic void relea
15cf9 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a  sePage(MemPage *
15cfa 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20 70 50  pPage){.  if( pP
15cfb 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72  age ){.    asser
15cfc 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
15cfd 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  low==0 || sqlite
15cfe 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75  3PagerPageRefcou
15cff 6e 74 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  nt(pPage->pDbPag
15d00 65 29 3e 31 20 29 3b 0a 20 20 20 20 61 73 73 65  e)>1 );.    asse
15d01 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61  rt( pPage->aData
15d02 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15d03 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20  pPage->pBt );.  
15d04 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15d05 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70  3PagerGetExtra(p
15d06 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d  Page->pDbPage) =
15d07 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 20 29  = (void*)pPage )
15d08 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  ;.    assert( sq
15d09 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
15d0a 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  a(pPage->pDbPage
15d0b 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 20  )==pPage->aData 
15d0c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
15d0d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15d0e 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
15d0f 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69  tex) );.    sqli
15d10 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
15d11 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
15d12 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69   }.}../*.** Duri
15d13 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 77  ng a rollback, w
15d14 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 72 65  hen the pager re
15d15 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f  loads informatio
15d16 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65  n into the cache
15d17 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20  .** so that the 
15d18 63 61 63 68 65 20 69 73 20 72 65 73 74 6f 72 65  cache is restore
15d19 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61  d to its origina
15d1a 6c 20 73 74 61 74 65 20 61 74 20 74 68 65 20 73  l state at the s
15d1b 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 74  tart of.** the t
15d1c 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f 72 20  ransaction, for 
15d1d 65 61 63 68 20 70 61 67 65 20 72 65 73 74 6f 72  each page restor
15d1e 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ed this routine 
15d1f 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a  is called..**.**
15d20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65   This routine ne
15d21 65 64 73 20 74 6f 20 72 65 73 65 74 20 74 68 65  eds to reset the
15d22 20 65 78 74 72 61 20 64 61 74 61 20 73 65 63 74   extra data sect
15d23 69 6f 6e 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ion at the end o
15d24 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f  f the.** page to
15d25 20 61 67 72 65 65 20 77 69 74 68 20 74 68 65 20   agree with the 
15d26 72 65 73 74 6f 72 65 64 20 64 61 74 61 2e 0a 2a  restored data..*
15d27 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  /.static void pa
15d28 67 65 52 65 69 6e 69 74 28 44 62 50 61 67 65 20  geReinit(DbPage 
15d29 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 6d 50 61  *pData){.  MemPa
15d2a 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70 50 61  ge *pPage;.  pPa
15d2b 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29  ge = (MemPage *)
15d2c 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
15d2d 78 74 72 61 28 70 44 61 74 61 29 3b 0a 20 20 61  xtra(pData);.  a
15d2e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
15d2f 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28  gerPageRefcount(
15d30 70 44 61 74 61 29 3e 30 20 29 3b 0a 20 20 69 66  pData)>0 );.  if
15d31 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20  ( pPage->isInit 
15d32 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  ){.    assert( s
15d33 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15d34 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
15d35 74 65 78 29 20 29 3b 0a 20 20 20 20 70 50 61 67  tex) );.    pPag
15d36 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20  e->isInit = 0;. 
15d37 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61     if( sqlite3Pa
15d38 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28  gerPageRefcount(
15d39 70 44 61 74 61 29 3e 31 20 29 7b 0a 20 20 20 20  pData)>1 ){.    
15d3a 20 20 2f 2a 20 70 50 61 67 65 20 6d 69 67 68 74    /* pPage might
15d3b 20 6e 6f 74 20 62 65 20 61 20 62 74 72 65 65 20   not be a btree 
15d3c 70 61 67 65 3b 20 20 69 74 20 6d 69 67 68 74 20  page;  it might 
15d3d 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  be an overflow p
15d3e 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20  age.      ** or 
15d3f 70 74 72 6d 61 70 20 70 61 67 65 20 6f 72 20 61  ptrmap page or a
15d40 20 66 72 65 65 20 70 61 67 65 2e 20 20 49 6e 20   free page.  In 
15d41 74 68 6f 73 65 20 63 61 73 65 73 2c 20 74 68 65  those cases, the
15d42 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 20   following.     
15d43 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 72 65   ** call to btre
15d44 65 49 6e 69 74 50 61 67 65 28 29 20 77 69 6c 6c  eInitPage() will
15d45 20 6c 69 6b 65 6c 79 20 72 65 74 75 72 6e 20 53   likely return S
15d46 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 0a 20  QLITE_CORRUPT.. 
15d47 20 20 20 20 20 2a 2a 20 42 75 74 20 6e 6f 20 68       ** But no h
15d48 61 72 6d 20 69 73 20 64 6f 6e 65 20 62 79 20 74  arm is done by t
15d49 68 69 73 2e 20 20 41 6e 64 20 69 74 20 69 73 20  his.  And it is 
15d4a 76 65 72 79 20 69 6d 70 6f 72 74 61 6e 74 20 74  very important t
15d4b 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 62 74 72  hat.      ** btr
15d4c 65 65 49 6e 69 74 50 61 67 65 28 29 20 62 65 20  eeInitPage() be 
15d4d 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72 79 20  called on every 
15d4e 62 74 72 65 65 20 70 61 67 65 20 73 6f 20 77 65  btree page so we
15d4f 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20 74   make.      ** t
15d50 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 76 65 72  he call for ever
15d51 79 20 70 61 67 65 20 74 68 61 74 20 63 6f 6d 65  y page that come
15d52 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e 69 74  s in for re-init
15d53 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 62 74  ing. */.      bt
15d54 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67  reeInitPage(pPag
15d55 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  e);.    }.  }.}.
15d56 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
15d57 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66  e busy handler f
15d58 6f 72 20 61 20 62 74 72 65 65 2e 0a 2a 2f 0a 73  or a btree..*/.s
15d59 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 49  tatic int btreeI
15d5a 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72  nvokeBusyHandler
15d5b 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20  (void *pArg){.  
15d5c 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
15d5d 28 42 74 53 68 61 72 65 64 2a 29 70 41 72 67 3b  (BtShared*)pArg;
15d5e 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
15d5f 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  db );.  assert( 
15d60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
15d61 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d 75 74 65  ld(pBt->db->mute
15d62 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  x) );.  return s
15d63 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79  qlite3InvokeBusy
15d64 48 61 6e 64 6c 65 72 28 26 70 42 74 2d 3e 64 62  Handler(&pBt->db
15d65 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 29 3b 0a  ->busyHandler);.
15d66 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20  }../*.** Open a 
15d67 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
15d68 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 20  * .** zFilename 
15d69 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
15d6a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15d6b 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20  .  If zFilename 
15d6c 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20 6e 65 77  is NULL.** a new
15d6d 20 64 61 74 61 62 61 73 65 20 77 69 74 68 20 61   database with a
15d6e 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20 69 73 20   random name is 
15d6f 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 20 72  created.  This r
15d70 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64 0a 2a 2a  andomly named.**
15d71 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
15d72 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 20 77  ill be deleted w
15d73 68 65 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65  hen sqlite3Btree
15d74 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c 6c 65  Close() is calle
15d75 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61  d..** If zFilena
15d76 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22  me is ":memory:"
15d77 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f   then an in-memo
15d78 72 79 20 64 61 74 61 62 61 73 65 20 69 73 20 63  ry database is c
15d79 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74 20 69  reated.** that i
15d7a 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
15d7b 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20 69  destroyed when i
15d7c 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a  t is closed..**.
15d7d 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  ** If the databa
15d7e 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70  se is already op
15d7f 65 6e 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  ened in the same
15d80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
15d81 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 77 65 20 61  tion.** and we a
15d82 72 65 20 69 6e 20 73 68 61 72 65 64 20 63 61 63  re in shared cac
15d83 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68  he mode, then th
15d84 65 20 6f 70 65 6e 20 77 69 6c 6c 20 66 61 69 6c  e open will fail
15d85 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49   with an.** SQLI
15d86 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 65 72  TE_CONSTRAINT er
15d87 72 6f 72 2e 20 20 57 65 20 63 61 6e 6e 6f 74 20  ror.  We cannot 
15d88 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 20 6d 6f 72  allow two or mor
15d89 65 20 42 74 53 68 61 72 65 64 0a 2a 2a 20 6f 62  e BtShared.** ob
15d8a 6a 65 63 74 73 20 69 6e 20 74 68 65 20 73 61 6d  jects in the sam
15d8b 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
15d8c 63 74 69 6f 6e 20 73 69 6e 63 65 20 64 6f 69 6e  ction since doin
15d8d 67 20 73 6f 20 77 69 6c 6c 20 6c 65 61 64 0a 2a  g so will lead.*
15d8e 2a 20 74 6f 20 70 72 6f 62 6c 65 6d 73 20 77 69  * to problems wi
15d8f 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 53  th locking..*/.S
15d90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
15d91 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70  t sqlite3BtreeOp
15d92 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  en(.  const char
15d93 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a   *zFilename,  /*
15d94 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
15d95 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
15d96 20 42 54 72 65 65 20 64 61 74 61 62 61 73 65 20   BTree database 
15d97 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
15d98 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
15d99 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62  Associated datab
15d9a 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
15d9b 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 2c  Btree **ppBtree,
15d9c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
15d9d 65 72 20 74 6f 20 6e 65 77 20 42 74 72 65 65 20  er to new Btree 
15d9e 6f 62 6a 65 63 74 20 77 72 69 74 74 65 6e 20 68  object written h
15d9f 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  ere */.  int fla
15da0 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
15da1 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f 0a 20   /* Options */. 
15da2 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20   int vfsFlags   
15da3 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
15da4 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68  s passed through
15da5 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e   to sqlite3_vfs.
15da6 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20  xOpen() */.){.  
15da7 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
15da8 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  s;             /
15da9 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 73 65  * The VFS to use
15daa 20 66 6f 72 20 74 68 69 73 20 62 74 72 65 65 20   for this btree 
15dab 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  */.  BtShared *p
15dac 42 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  Bt = 0;         
15dad 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 70 61      /* Shared pa
15dae 72 74 20 6f 66 20 62 74 72 65 65 20 73 74 72 75  rt of btree stru
15daf 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 72 65 65  cture */.  Btree
15db0 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   *p;            
15db1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e            /* Han
15db2 64 6c 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  dle to return */
15db3 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
15db4 20 2a 6d 75 74 65 78 4f 70 65 6e 20 3d 20 30 3b   *mutexOpen = 0;
15db5 20 20 2f 2a 20 50 72 65 76 65 6e 74 73 20 61 20    /* Prevents a 
15db6 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 2e 20  race condition. 
15db7 54 69 63 6b 65 74 20 23 33 35 33 37 20 2a 2f 0a  Ticket #3537 */.
15db8 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
15db9 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
15dba 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20   /* Result code 
15dbb 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69  from this functi
15dbc 6f 6e 20 2a 2f 0a 20 20 75 38 20 6e 52 65 73 65  on */.  u8 nRese
15dbd 72 76 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  rve;            
15dbe 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f         /* Byte o
15dbf 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 20 6f  f unused space o
15dc0 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20  n each page */. 
15dc1 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a   unsigned char z
15dc2 44 62 48 65 61 64 65 72 5b 31 30 30 5d 3b 20 20  DbHeader[100];  
15dc3 2f 2a 20 44 61 74 61 62 61 73 65 20 68 65 61 64  /* Database head
15dc4 65 72 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20  er content */.. 
15dc5 20 2f 2a 20 53 65 74 20 74 68 65 20 76 61 72 69   /* Set the vari
15dc6 61 62 6c 65 20 69 73 4d 65 6d 64 62 20 74 6f 20  able isMemdb to 
15dc7 74 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d  true for an in-m
15dc8 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20  emory database, 
15dc9 6f 72 20 0a 20 20 2a 2a 20 66 61 6c 73 65 20 66  or .  ** false f
15dca 6f 72 20 61 20 66 69 6c 65 2d 62 61 73 65 64 20  or a file-based 
15dcb 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 20 73  database. This s
15dcc 79 6d 62 6f 6c 20 69 73 20 6f 6e 6c 79 20 72 65  ymbol is only re
15dcd 71 75 69 72 65 64 20 69 66 0a 20 20 2a 2a 20 65  quired if.  ** e
15dce 69 74 68 65 72 20 6f 66 20 74 68 65 20 73 68 61  ither of the sha
15dcf 72 65 64 2d 64 61 74 61 20 6f 72 20 61 75 74 6f  red-data or auto
15dd0 76 61 63 75 75 6d 20 66 65 61 74 75 72 65 73 20  vacuum features 
15dd1 61 72 65 20 63 6f 6d 70 69 6c 65 64 20 0a 20 20  are compiled .  
15dd2 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 69 62 72  ** into the libr
15dd3 61 72 79 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64  ary..  */.#if !d
15dd4 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
15dd5 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29  IT_SHARED_CACHE)
15dd6 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
15dd7 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
15dd8 55 55 4d 29 0a 20 20 23 69 66 64 65 66 20 53 51  UUM).  #ifdef SQ
15dd9 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59  LITE_OMIT_MEMORY
15dda 44 42 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  DB.    const int
15ddb 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b 0a 20 20   isMemdb = 0;.  
15ddc 23 65 6c 73 65 0a 20 20 20 20 63 6f 6e 73 74 20  #else.    const 
15ddd 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 7a 46  int isMemdb = zF
15dde 69 6c 65 6e 61 6d 65 20 26 26 20 21 73 74 72 63  ilename && !strc
15ddf 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 3a  mp(zFilename, ":
15de0 6d 65 6d 6f 72 79 3a 22 29 3b 0a 20 20 23 65 6e  memory:");.  #en
15de1 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73  dif.#endif..  as
15de2 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20  sert( db!=0 );. 
15de3 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15de4 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
15de5 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 56 66  mutex) );..  pVf
15de6 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20  s = db->pVfs;.  
15de7 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  p = sqlite3Mallo
15de8 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 74 72  cZero(sizeof(Btr
15de9 65 65 29 29 3b 0a 20 20 69 66 28 20 21 70 20 29  ee));.  if( !p )
15dea 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
15deb 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
15dec 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52   p->inTrans = TR
15ded 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 64  ANS_NONE;.  p->d
15dee 62 20 3d 20 64 62 3b 0a 23 69 66 6e 64 65 66 20  b = db;.#ifndef 
15def 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
15df0 45 44 5f 43 41 43 48 45 0a 20 20 70 2d 3e 6c 6f  ED_CACHE.  p->lo
15df1 63 6b 2e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20  ck.pBtree = p;. 
15df2 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c 65 20   p->lock.iTable 
15df3 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  = 1;.#endif..#if
15df4 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
15df5 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
15df6 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  HE) && !defined(
15df7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
15df8 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66  IO).  /*.  ** If
15df9 20 74 68 69 73 20 42 74 72 65 65 20 69 73 20 61   this Btree is a
15dfa 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 20 73   candidate for s
15dfb 68 61 72 65 64 20 63 61 63 68 65 2c 20 74 72 79  hared cache, try
15dfc 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20 20 2a 2a   to find an.  **
15dfd 20 65 78 69 73 74 69 6e 67 20 42 74 53 68 61 72   existing BtShar
15dfe 65 64 20 6f 62 6a 65 63 74 20 74 68 61 74 20 77  ed object that w
15dff 65 20 63 61 6e 20 73 68 61 72 65 20 77 69 74 68  e can share with
15e00 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 4d 65  .  */.  if( isMe
15e01 6d 64 62 3d 3d 30 20 26 26 20 7a 46 69 6c 65 6e  mdb==0 && zFilen
15e02 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65  ame && zFilename
15e03 5b 30 5d 20 29 7b 0a 20 20 20 20 69 66 28 20 76  [0] ){.    if( v
15e04 66 73 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45  fsFlags & SQLITE
15e05 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48  _OPEN_SHAREDCACH
15e06 45 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  E ){.      int n
15e07 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 70  FullPathname = p
15e08 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b  Vfs->mxPathname+
15e09 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  1;.      char *z
15e0a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 73  FullPathname = s
15e0b 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 46 75  qlite3Malloc(nFu
15e0c 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20  llPathname);.   
15e0d 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
15e0e 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20   *mutexShared;. 
15e0f 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65       p->sharable
15e10 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20   = 1;.      if( 
15e11 21 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29  !zFullPathname )
15e12 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
15e13 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20  3_free(p);.     
15e14 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15e15 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a  _NOMEM;.      }.
15e16 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46        sqlite3OsF
15e17 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73  ullPathname(pVfs
15e18 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 46 75  , zFilename, nFu
15e19 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c  llPathname, zFul
15e1a 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  lPathname);.    
15e1b 20 20 6d 75 74 65 78 4f 70 65 6e 20 3d 20 73 71    mutexOpen = sq
15e1c 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
15e1d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
15e1e 54 49 43 5f 4f 50 45 4e 29 3b 0a 20 20 20 20 20  TIC_OPEN);.     
15e1f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
15e20 6e 74 65 72 28 6d 75 74 65 78 4f 70 65 6e 29 3b  nter(mutexOpen);
15e21 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 61 72  .      mutexShar
15e22 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ed = sqlite3Mute
15e23 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
15e24 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
15e25 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  R);.      sqlite
15e26 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
15e27 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20  texShared);.    
15e28 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 41 4c    for(pBt=GLOBAL
15e29 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74  (BtShared*,sqlit
15e2a 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73  e3SharedCacheLis
15e2b 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 42 74  t); pBt; pBt=pBt
15e2c 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
15e2d 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e    assert( pBt->n
15e2e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  Ref>0 );.       
15e2f 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 28 7a   if( 0==strcmp(z
15e30 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 73 71  FullPathname, sq
15e31 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61  lite3PagerFilena
15e32 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 29  me(pBt->pPager))
15e33 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
15e34 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 67 65    && sqlite3Page
15e35 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67 65 72  rVfs(pBt->pPager
15e36 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20  )==pVfs ){.     
15e37 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20       int iDb;.  
15e38 20 20 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d          for(iDb=
15e39 64 62 2d 3e 6e 44 62 2d 31 3b 20 69 44 62 3e 3d  db->nDb-1; iDb>=
15e3a 30 3b 20 69 44 62 2d 2d 29 7b 0a 20 20 20 20 20  0; iDb--){.     
15e3b 20 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 45         Btree *pE
15e3c 78 69 73 74 69 6e 67 20 3d 20 64 62 2d 3e 61 44  xisting = db->aD
15e3d 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20  b[iDb].pBt;.    
15e3e 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78 69          if( pExi
15e3f 73 74 69 6e 67 20 26 26 20 70 45 78 69 73 74 69  sting && pExisti
15e40 6e 67 2d 3e 70 42 74 3d 3d 70 42 74 20 29 7b 0a  ng->pBt==pBt ){.
15e41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
15e42 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
15e43 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a  e(mutexShared);.
15e44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
15e45 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
15e46 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20  e(mutexOpen);.  
15e47 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
15e48 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61  te3_free(zFullPa
15e49 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  thname);.       
15e4a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
15e4b 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20  ree(p);.        
15e4c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15e4d 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a  ITE_CONSTRAINT;.
15e4e 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
15e4f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15e50 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74      p->pBt = pBt
15e51 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 74 2d  ;.          pBt-
15e52 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20  >nRef++;.       
15e53 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
15e54 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
15e55 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
15e56 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65  leave(mutexShare
15e57 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  d);.      sqlite
15e58 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 68  3_free(zFullPath
15e59 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66  name);.    }.#if
15e5a 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
15e5b 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20  .    else{.     
15e5c 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d 6f 64   /* In debug mod
15e5d 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c 20 70  e, we mark all p
15e5e 65 72 73 69 73 74 65 6e 74 20 64 61 74 61 62 61  ersistent databa
15e5f 73 65 73 20 61 73 20 73 68 61 72 61 62 6c 65 0a  ses as sharable.
15e60 20 20 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68        ** even wh
15e61 65 6e 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e  en they are not.
15e62 20 20 54 68 69 73 20 65 78 65 72 63 69 73 65 73    This exercises
15e63 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 64   the locking cod
15e64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 67  e and.      ** g
15e65 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f 72 74  ives more opport
15e66 75 6e 69 74 79 20 66 6f 72 20 61 73 73 65 72 74  unity for assert
15e67 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  s(sqlite3_mutex_
15e68 68 65 6c 64 28 29 29 0a 20 20 20 20 20 20 2a 2a  held()).      **
15e69 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 66   statements to f
15e6a 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 62  ind locking prob
15e6b 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  lems..      */. 
15e6c 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65       p->sharable
15e6d 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64   = 1;.    }.#end
15e6e 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  if.  }.#endif.  
15e6f 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20  if( pBt==0 ){.  
15e70 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    /*.    ** The 
15e71 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
15e72 73 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  s make sure that
15e73 20 73 74 72 75 63 74 75 72 65 73 20 75 73 65 64   structures used
15e74 20 62 79 20 74 68 65 20 62 74 72 65 65 20 61 72   by the btree ar
15e75 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 69 67  e.    ** the rig
15e76 68 74 20 73 69 7a 65 2e 20 20 54 68 69 73 20 69  ht size.  This i
15e77 73 20 74 6f 20 67 75 61 72 64 20 61 67 61 69 6e  s to guard again
15e78 73 74 20 73 69 7a 65 20 63 68 61 6e 67 65 73 20  st size changes 
15e79 74 68 61 74 20 72 65 73 75 6c 74 0a 20 20 20 20  that result.    
15e7a 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e  ** when compilin
15e7b 67 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74  g on a different
15e7c 20 61 72 63 68 69 74 65 63 74 75 72 65 2e 0a 20   architecture.. 
15e7d 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
15e7e 28 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 38  ( sizeof(i64)==8
15e7f 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34 29 3d   || sizeof(i64)=
15e80 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =4 );.    assert
15e81 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d 38  ( sizeof(u64)==8
15e82 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34 29 3d   || sizeof(u64)=
15e83 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =4 );.    assert
15e84 28 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34  ( sizeof(u32)==4
15e85 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15e86 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32 20 29  sizeof(u16)==2 )
15e87 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69  ;.    assert( si
15e88 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20 29 3b  zeof(Pgno)==4 );
15e89 0a 20 20 0a 20 20 20 20 70 42 74 20 3d 20 73 71  .  .    pBt = sq
15e8a 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28  lite3MallocZero(
15e8b 20 73 69 7a 65 6f 66 28 2a 70 42 74 29 20 29 3b   sizeof(*pBt) );
15e8c 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20  .    if( pBt==0 
15e8d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
15e8e 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
15e8f 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65    goto btree_ope
15e90 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  n_out;.    }.   
15e91 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
15e92 65 72 4f 70 65 6e 28 70 56 66 73 2c 20 26 70 42  erOpen(pVfs, &pB
15e93 74 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69 6c 65  t->pPager, zFile
15e94 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  name,.          
15e95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15e96 45 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c 61 67  EXTRA_SIZE, flag
15e97 73 2c 20 76 66 73 46 6c 61 67 73 2c 20 70 61 67  s, vfsFlags, pag
15e98 65 52 65 69 6e 69 74 29 3b 0a 20 20 20 20 69 66  eReinit);.    if
15e99 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
15e9a 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
15e9b 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69  lite3PagerReadFi
15e9c 6c 65 68 65 61 64 65 72 28 70 42 74 2d 3e 70 50  leheader(pBt->pP
15e9d 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a 44 62 48  ager,sizeof(zDbH
15e9e 65 61 64 65 72 29 2c 7a 44 62 48 65 61 64 65 72  eader),zDbHeader
15e9f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
15ea0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
15ea1 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72  {.      goto btr
15ea2 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20  ee_open_out;.   
15ea3 20 7d 0a 20 20 20 20 70 42 74 2d 3e 64 62 20 3d   }.    pBt->db =
15ea4 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   db;.    sqlite3
15ea5 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64  PagerSetBusyhand
15ea6 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  ler(pBt->pPager,
15ea7 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79   btreeInvokeBusy
15ea8 48 61 6e 64 6c 65 72 2c 20 70 42 74 29 3b 0a 20  Handler, pBt);. 
15ea9 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b     p->pBt = pBt;
15eaa 0a 20 20 0a 20 20 20 20 70 42 74 2d 3e 70 43 75  .  .    pBt->pCu
15eab 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 70 42  rsor = 0;.    pB
15eac 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20  t->pPage1 = 0;. 
15ead 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79     pBt->readOnly
15eae 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 49   = sqlite3PagerI
15eaf 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d 3e 70  sreadonly(pBt->p
15eb0 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42 74 2d  Pager);.    pBt-
15eb1 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32  >pageSize = get2
15eb2 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b  byte(&zDbHeader[
15eb3 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 42  16]);.    if( pB
15eb4 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31 32 20  t->pageSize<512 
15eb5 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  || pBt->pageSize
15eb6 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  >SQLITE_MAX_PAGE
15eb7 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20 20 7c  _SIZE.         |
15eb8 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a  | ((pBt->pageSiz
15eb9 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 53 69  e-1)&pBt->pageSi
15eba 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ze)!=0 ){.      
15ebb 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20  pBt->pageSize = 
15ebc 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  0;.#ifndef SQLIT
15ebd 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
15ebe 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  M.      /* If th
15ebf 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d  e magic name ":m
15ec0 65 6d 6f 72 79 3a 22 20 77 69 6c 6c 20 63 72 65  emory:" will cre
15ec1 61 74 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ate an in-memory
15ec2 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a   database, then.
15ec3 20 20 20 20 20 20 2a 2a 20 6c 65 61 76 65 20 74        ** leave t
15ec4 68 65 20 61 75 74 6f 56 61 63 75 75 6d 20 6d 6f  he autoVacuum mo
15ec5 64 65 20 61 74 20 30 20 28 64 6f 20 6e 6f 74 20  de at 0 (do not 
15ec6 61 75 74 6f 2d 76 61 63 75 75 6d 29 2c 20 65 76  auto-vacuum), ev
15ec7 65 6e 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53  en if.      ** S
15ec8 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55  QLITE_DEFAULT_AU
15ec9 54 4f 56 41 43 55 55 4d 20 69 73 20 74 72 75 65  TOVACUUM is true
15eca 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68  . On the other h
15ecb 61 6e 64 2c 20 69 66 0a 20 20 20 20 20 20 2a 2a  and, if.      **
15ecc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d   SQLITE_OMIT_MEM
15ecd 4f 52 59 44 42 20 68 61 73 20 62 65 65 6e 20 64  ORYDB has been d
15ece 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 22 3a 6d  efined, then ":m
15ecf 65 6d 6f 72 79 3a 22 20 69 73 20 6a 75 73 74 20  emory:" is just 
15ed0 61 0a 20 20 20 20 20 20 2a 2a 20 72 65 67 75 6c  a.      ** regul
15ed1 61 72 20 66 69 6c 65 2d 6e 61 6d 65 2e 20 49 6e  ar file-name. In
15ed2 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 61   this case the a
15ed3 75 74 6f 2d 76 61 63 75 75 6d 20 61 70 70 6c 69  uto-vacuum appli
15ed4 65 73 20 61 73 20 70 65 72 20 6e 6f 72 6d 61 6c  es as per normal
15ed5 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
15ed6 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26   if( zFilename &
15ed7 26 20 21 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20  & !isMemdb ){.  
15ed8 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56        pBt->autoV
15ed9 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f  acuum = (SQLITE_
15eda 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55  DEFAULT_AUTOVACU
15edb 55 4d 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20  UM ? 1 : 0);.   
15edc 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61       pBt->incrVa
15edd 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44  cuum = (SQLITE_D
15ede 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55  EFAULT_AUTOVACUU
15edf 4d 3d 3d 32 20 3f 20 31 20 3a 20 30 29 3b 0a 20  M==2 ? 1 : 0);. 
15ee0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
15ee1 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 30      nReserve = 0
15ee2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
15ee3 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 7a 44     nReserve = zD
15ee4 62 48 65 61 64 65 72 5b 32 30 5d 3b 0a 20 20 20  bHeader[20];.   
15ee5 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65     pBt->pageSize
15ee6 46 69 78 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64  Fixed = 1;.#ifnd
15ee7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
15ee8 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
15ee9 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
15eea 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a 44 62  = (get4byte(&zDb
15eeb 48 65 61 64 65 72 5b 33 36 20 2b 20 34 2a 34 5d  Header[36 + 4*4]
15eec 29 3f 31 3a 30 29 3b 0a 20 20 20 20 20 20 70 42  )?1:0);.      pB
15eed 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20  t->incrVacuum = 
15eee 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 48 65  (get4byte(&zDbHe
15eef 61 64 65 72 5b 33 36 20 2b 20 37 2a 34 5d 29 3f  ader[36 + 7*4])?
15ef0 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  1:0);.#endif.   
15ef1 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
15ef2 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73  te3PagerSetPages
15ef3 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  ize(pBt->pPager,
15ef4 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c   &pBt->pageSize,
15ef5 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 20 20   nReserve);.    
15ef6 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 74  if( rc ) goto bt
15ef7 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20  ree_open_out;.  
15ef8 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a    pBt->usableSiz
15ef9 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  e = pBt->pageSiz
15efa 65 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a 20 20  e - nReserve;.  
15efb 20 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e    assert( (pBt->
15efc 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30  pageSize & 7)==0
15efd 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 20 61   );  /* 8-byte a
15efe 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 67 65  lignment of page
15eff 53 69 7a 65 20 2a 2f 0a 20 20 20 0a 23 69 66 20  Size */.   .#if 
15f00 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
15f01 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
15f02 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  E) && !defined(S
15f03 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
15f04 4f 29 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68  O).    /* Add th
15f05 65 20 6e 65 77 20 42 74 53 68 61 72 65 64 20 6f  e new BtShared o
15f06 62 6a 65 63 74 20 74 6f 20 74 68 65 20 6c 69 6e  bject to the lin
15f07 6b 65 64 20 6c 69 73 74 20 73 68 61 72 61 62 6c  ked list sharabl
15f08 65 20 42 74 53 68 61 72 65 64 73 2e 0a 20 20 20  e BtShareds..   
15f09 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 73   */.    if( p->s
15f0a 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  harable ){.     
15f0b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
15f0c 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20 20 20  mutexShared;.   
15f0d 20 20 20 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31     pBt->nRef = 1
15f0e 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 61  ;.      mutexSha
15f0f 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  red = sqlite3Mut
15f10 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
15f11 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
15f12 45 52 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53  ER);.      if( S
15f13 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
15f14 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   && sqlite3Globa
15f15 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
15f16 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42  ex ){.        pB
15f17 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  t->mutex = sqlit
15f18 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
15f19 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b  ITE_MUTEX_FAST);
15f1a 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 42 74  .        if( pBt
15f1b 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20  ->mutex==0 ){.  
15f1c 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
15f1d 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
15f1e 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
15f1f 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20  ailed = 0;.     
15f20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f       goto btree_
15f21 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20  open_out;.      
15f22 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
15f23 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
15f24 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 65  enter(mutexShare
15f25 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70  d);.      pBt->p
15f26 4e 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74  Next = GLOBAL(Bt
15f27 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53  Shared*,sqlite3S
15f28 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b  haredCacheList);
15f29 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74  .      GLOBAL(Bt
15f2a 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53  Shared*,sqlite3S
15f2b 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20  haredCacheList) 
15f2c 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73 71 6c  = pBt;.      sql
15f2d 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
15f2e 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20  (mutexShared);. 
15f2f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
15f30 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
15f31 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
15f32 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69  _CACHE) && !defi
15f33 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
15f34 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 20  DISKIO).  /* If 
15f35 74 68 65 20 6e 65 77 20 42 74 72 65 65 20 75 73  the new Btree us
15f36 65 73 20 61 20 73 68 61 72 61 62 6c 65 20 70 42  es a sharable pB
15f37 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20 6c 69  tShared, then li
15f38 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20  nk the new.  ** 
15f39 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65 20 6c  Btree into the l
15f3a 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61 72 61  ist of all shara
15f3b 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72 20 74  ble Btrees for t
15f3c 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69  he same connecti
15f3d 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73  on..  ** The lis
15f3e 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61 73 63  t is kept in asc
15f3f 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20  ending order by 
15f40 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20 20 2a  pBt address..  *
15f41 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61  /.  if( p->shara
15f42 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ble ){.    int i
15f43 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 53 69  ;.    Btree *pSi
15f44 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  b;.    for(i=0; 
15f45 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
15f46 0a 20 20 20 20 20 20 69 66 28 20 28 70 53 69 62  .      if( (pSib
15f47 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
15f48 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d 3e 73  t)!=0 && pSib->s
15f49 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  harable ){.     
15f4a 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e     while( pSib->
15f4b 70 50 72 65 76 20 29 7b 20 70 53 69 62 20 3d 20  pPrev ){ pSib = 
15f4c 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d 0a 20  pSib->pPrev; }. 
15f4d 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 42         if( p->pB
15f4e 74 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20  t<pSib->pBt ){. 
15f4f 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78           p->pNex
15f50 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 20  t = pSib;.      
15f51 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 30      p->pPrev = 0
15f52 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 62  ;.          pSib
15f53 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20  ->pPrev = p;.   
15f54 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
15f55 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 69        while( pSi
15f56 62 2d 3e 70 4e 65 78 74 20 26 26 20 70 53 69 62  b->pNext && pSib
15f57 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70 2d 3e  ->pNext->pBt<p->
15f58 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  pBt ){.         
15f59 20 20 20 70 53 69 62 20 3d 20 70 53 69 62 2d 3e     pSib = pSib->
15f5a 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20  pNext;.         
15f5b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e   }.          p->
15f5c 70 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e  pNext = pSib->pN
15f5d 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70  ext;.          p
15f5e 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62 3b 0a  ->pPrev = pSib;.
15f5f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d            if( p-
15f60 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20  >pNext ){.      
15f61 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e        p->pNext->
15f62 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20  pPrev = p;.     
15f63 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
15f64 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70   pSib->pNext = p
15f65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
15f66 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
15f67 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e   }.    }.  }.#en
15f68 64 69 66 0a 20 20 2a 70 70 42 74 72 65 65 20 3d  dif.  *ppBtree =
15f69 20 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f   p;..btree_open_
15f6a 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21 3d 53  out:.  if( rc!=S
15f6b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
15f6c 69 66 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e  if( pBt && pBt->
15f6d 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 20 20  pPager ){.      
15f6e 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73  sqlite3PagerClos
15f6f 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a  e(pBt->pPager);.
15f70 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
15f71 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 20 20  3_free(pBt);.   
15f72 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
15f73 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65 20 3d  ;.    *ppBtree =
15f74 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6d 75   0;.  }.  if( mu
15f75 74 65 78 4f 70 65 6e 20 29 7b 0a 20 20 20 20 61  texOpen ){.    a
15f76 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
15f77 75 74 65 78 5f 68 65 6c 64 28 6d 75 74 65 78 4f  utex_held(mutexO
15f78 70 65 6e 29 20 29 3b 0a 20 20 20 20 73 71 6c 69  pen) );.    sqli
15f79 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
15f7a 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 7d 0a  mutexOpen);.  }.
15f7b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
15f7c 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20  /*.** Decrement 
15f7d 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65  the BtShared.nRe
15f7e 66 20 63 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e  f counter.  When
15f7f 20 69 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f   it reaches zero
15f80 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20  ,.** remove the 
15f81 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75  BtShared structu
15f82 72 65 20 66 72 6f 6d 20 74 68 65 20 73 68 61 72  re from the shar
15f83 69 6e 67 20 6c 69 73 74 2e 20 20 52 65 74 75 72  ing list.  Retur
15f84 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 74 68 65  n.** true if the
15f85 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63   BtShared.nRef c
15f86 6f 75 6e 74 65 72 20 72 65 61 63 68 65 73 20 7a  ounter reaches z
15f87 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a  ero and return.*
15f88 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73  * false if it is
15f89 20 73 74 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e   still positive.
15f8a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
15f8b 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67  emoveFromSharing
15f8c 4c 69 73 74 28 42 74 53 68 61 72 65 64 20 2a 70  List(BtShared *p
15f8d 42 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  Bt){.#ifndef SQL
15f8e 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
15f8f 43 41 43 48 45 0a 20 20 73 71 6c 69 74 65 33 5f  CACHE.  sqlite3_
15f90 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a  mutex *pMaster;.
15f91 20 20 42 74 53 68 61 72 65 64 20 2a 70 4c 69 73    BtShared *pLis
15f92 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f 76 65 64  t;.  int removed
15f93 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
15f94 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
15f95 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  otheld(pBt->mute
15f96 78 29 20 29 3b 0a 20 20 70 4d 61 73 74 65 72 20  x) );.  pMaster 
15f97 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
15f98 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
15f99 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b  _STATIC_MASTER);
15f9a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
15f9b 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b  _enter(pMaster);
15f9c 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a  .  pBt->nRef--;.
15f9d 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c    if( pBt->nRef<
15f9e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 47 4c  =0 ){.    if( GL
15f9f 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73  OBAL(BtShared*,s
15fa0 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68  qlite3SharedCach
15fa1 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b 0a 20  eList)==pBt ){. 
15fa2 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68       GLOBAL(BtSh
15fa3 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61  ared*,sqlite3Sha
15fa4 72 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20  redCacheList) = 
15fa5 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pBt->pNext;.    
15fa6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 69  }else{.      pLi
15fa7 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68  st = GLOBAL(BtSh
15fa8 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61  ared*,sqlite3Sha
15fa9 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20  redCacheList);. 
15faa 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c 57 41       while( ALWA
15fab 59 53 28 70 4c 69 73 74 29 20 26 26 20 70 4c 69  YS(pList) && pLi
15fac 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74 20 29  st->pNext!=pBt )
15fad 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 3d  {.        pList=
15fae 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20  pList->pNext;.  
15faf 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
15fb0 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 29 7b  ALWAYS(pList) ){
15fb1 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 2d 3e  .        pList->
15fb2 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65  pNext = pBt->pNe
15fb3 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  xt;.      }.    
15fb4 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  }.    if( SQLITE
15fb5 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b 0a 20  _THREADSAFE ){. 
15fb6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
15fb7 65 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d 75 74  ex_free(pBt->mut
15fb8 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ex);.    }.    r
15fb9 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  emoved = 1;.  }.
15fba 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
15fbb 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a  leave(pMaster);.
15fbc 20 20 72 65 74 75 72 6e 20 72 65 6d 6f 76 65 64    return removed
15fbd 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e  ;.#else.  return
15fbe 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a   1;.#endif.}../*
15fbf 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 42  .** Make sure pB
15fc0 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 70 6f 69  t->pTmpSpace poi
15fc1 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61  nts to an alloca
15fc2 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43  tion of .** MX_C
15fc3 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 62 79  ELL_SIZE(pBt) by
15fc4 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tes..*/.static v
15fc5 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70  oid allocateTemp
15fc6 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20 2a  Space(BtShared *
15fc7 70 42 74 29 7b 0a 20 20 69 66 28 20 21 70 42 74  pBt){.  if( !pBt
15fc8 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b 0a 20  ->pTmpSpace ){. 
15fc9 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63     pBt->pTmpSpac
15fca 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d  e = sqlite3PageM
15fcb 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 65  alloc( pBt->page
15fcc 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Size );.  }.}../
15fcd 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 70 42  *.** Free the pB
15fce 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61 6c 6c  t->pTmpSpace all
15fcf 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69  ocation.*/.stati
15fd0 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d 70 53  c void freeTempS
15fd1 70 61 63 65 28 42 74 53 68 61 72 65 64 20 2a 70  pace(BtShared *p
15fd2 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61  Bt){.  sqlite3Pa
15fd3 67 65 46 72 65 65 28 20 70 42 74 2d 3e 70 54 6d  geFree( pBt->pTm
15fd4 70 53 70 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e  pSpace);.  pBt->
15fd5 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d  pTmpSpace = 0;.}
15fd6 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e  ../*.** Close an
15fd7 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 61   open database a
15fd8 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c  nd invalidate al
15fd9 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51  l cursors..*/.SQ
15fda 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
15fdb 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
15fdc 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  se(Btree *p){.  
15fdd 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
15fde 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 72 73  p->pBt;.  BtCurs
15fdf 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20  or *pCur;..  /* 
15fe0 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72  Close all cursor
15fe1 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 69  s opened via thi
15fe2 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20  s handle.  */.  
15fe3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
15fe4 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
15fe5 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
15fe6 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
15fe7 70 29 3b 0a 20 20 70 43 75 72 20 3d 20 70 42 74  p);.  pCur = pBt
15fe8 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 68 69  ->pCursor;.  whi
15fe9 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20  le( pCur ){.    
15fea 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d  BtCursor *pTmp =
15feb 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 72 20   pCur;.    pCur 
15fec 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20  = pCur->pNext;. 
15fed 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 42 74     if( pTmp->pBt
15fee 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20  ree==p ){.      
15fef 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
15ff0 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20  eCursor(pTmp);. 
15ff1 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52     }.  }..  /* R
15ff2 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 69  ollback any acti
15ff3 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  ve transaction a
15ff4 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 6e 64  nd free the hand
15ff5 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20  le structure..  
15ff6 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73  ** The call to s
15ff7 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
15ff8 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e 79 20  ack() drops any 
15ff9 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64  table-locks held
15ffa 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 68 61   by.  ** this ha
15ffb 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  ndle..  */.  sql
15ffc 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63  ite3BtreeRollbac
15ffd 6b 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  k(p);.  sqlite3B
15ffe 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20  treeLeave(p);.. 
15fff 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
16000 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74   still other out
16001 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e  standing referen
16002 63 65 73 20 74 6f 20 74 68 65 20 73 68 61 72 65  ces to the share
16003 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 74 72  d-btree.  ** str
16004 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e 20 6e  ucture, return n
16005 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65  ow. The remainde
16006 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64  r of this proced
16007 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a  ure cleans .  **
16008 20 75 70 20 74 68 65 20 73 68 61 72 65 64 2d 62   up the shared-b
16009 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  tree..  */.  ass
1600a 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  ert( p->wantToLo
1600b 63 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b  ck==0 && p->lock
1600c 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 21  ed==0 );.  if( !
1600d 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 72  p->sharable || r
1600e 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67  emoveFromSharing
1600f 4c 69 73 74 28 70 42 74 29 20 29 7b 0a 20 20 20  List(pBt) ){.   
16010 20 2f 2a 20 54 68 65 20 70 42 74 20 69 73 20 6e   /* The pBt is n
16011 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20  o longer on the 
16012 73 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f  sharing list, so
16013 20 77 65 20 63 61 6e 20 61 63 63 65 73 73 0a 20   we can access. 
16014 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f 75 74     ** it without
16015 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20   having to hold 
16016 74 68 65 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a  the mutex..    *
16017 2a 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f  *.    ** Clean o
16018 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68  ut and delete th
16019 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63  e BtShared objec
1601a 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  t..    */.    as
1601b 73 65 72 74 28 20 21 70 42 74 2d 3e 70 43 75 72  sert( !pBt->pCur
1601c 73 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  sor );.    sqlit
1601d 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74  e3PagerClose(pBt
1601e 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  ->pPager);.    i
1601f 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68  f( pBt->xFreeSch
16020 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 63 68  ema && pBt->pSch
16021 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 42 74  ema ){.      pBt
16022 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28 70 42  ->xFreeSchema(pB
16023 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  t->pSchema);.   
16024 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
16025 72 65 65 28 70 42 74 2d 3e 70 53 63 68 65 6d 61  ree(pBt->pSchema
16026 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53  );.    freeTempS
16027 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 73  pace(pBt);.    s
16028 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29  qlite3_free(pBt)
16029 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
1602a 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1602b 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 74  D_CACHE.  assert
1602c 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  ( p->wantToLock=
1602d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1602e 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a  p->locked==0 );.
1602f 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29    if( p->pPrev )
16030 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74   p->pPrev->pNext
16031 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69   = p->pNext;.  i
16032 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d  f( p->pNext ) p-
16033 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20  >pNext->pPrev = 
16034 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66  p->pPrev;.#endif
16035 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
16036 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  (p);.  return SQ
16037 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
16038 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d  * Change the lim
16039 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72  it on the number
1603a 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65   of pages allowe
1603b 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a  d in the cache..
1603c 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  **.** The maximu
1603d 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68  m number of cach
1603e 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74  e pages is set t
1603f 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a  o the absolute.*
16040 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67  * value of mxPag
16041 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 73  e.  If mxPage is
16042 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 70   negative, the p
16043 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65  ager will.** ope
16044 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75  rate asynchronou
16045 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f  sly - it will no
16046 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79  t stop to do fsy
16047 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75  nc()s.** to insu
16048 72 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74  re data is writt
16049 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73  en to the disk s
1604a 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a  urface before.**
1604b 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72   continuing.  Tr
1604c 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c  ansactions still
1604d 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f   work if synchro
1604e 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20  nous is off,.** 
1604f 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  and the database
16050 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75   cannot be corru
16051 70 74 65 64 20 69 66 20 74 68 69 73 20 70 72 6f  pted if this pro
16052 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e  gram.** crashes.
16053 20 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 65    But if the ope
16054 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72  rating system cr
16055 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 69  ashes or there i
16056 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70  s.** an abrupt p
16057 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 65  ower failure whe
16058 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73  n synchronous is
16059 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61   off, the databa
1605a 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c  se.** could be l
1605b 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73  eft in an incons
1605c 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63  istent and unrec
1605d 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a  overable state..
1605e 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69  ** Synchronous i
1605f 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20  s on by default 
16060 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  so database corr
16061 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a  uption is not.**
16062 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72   normally a worr
16063 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
16064 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16065 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a  BtreeSetCacheSiz
16066 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  e(Btree *p, int 
16067 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61  mxPage){.  BtSha
16068 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
16069 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  t;.  assert( sql
1606a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1606b 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
1606c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1606d 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74  nter(p);.  sqlit
1606e 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73  e3PagerSetCaches
1606f 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  ize(pBt->pPager,
16070 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69   mxPage);.  sqli
16071 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
16072 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
16073 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
16074 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 64 61  hange the way da
16075 74 61 20 69 73 20 73 79 6e 63 65 64 20 74 6f 20  ta is synced to 
16076 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f  disk in order to
16077 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64 65 63   increase or dec
16078 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c  rease.** how wel
16079 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72  l the database r
1607a 65 73 69 73 74 73 20 64 61 6d 61 67 65 20 64 75  esists damage du
1607b 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 20  e to OS crashes 
1607c 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69  and power.** fai
1607d 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 31 20  lures.  Level 1 
1607e 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61  is the same as a
1607f 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20  synchronous (no 
16080 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20 61 6e  syncs() occur an
16081 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20  d.** there is a 
16082 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79  high probability
16083 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c 65 76   of damage)  Lev
16084 65 6c 20 32 20 69 73 20 74 68 65 20 64 65 66 61  el 2 is the defa
16085 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69  ult.  There.** i
16086 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62 75 74  s a very low but
16087 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62   non-zero probab
16088 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 2e  ility of damage.
16089 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75 63 65    Level 3 reduce
1608a 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69  s the.** probabi
1608b 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 20 74  lity of damage t
1608c 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 74 20  o near zero but 
1608d 77 69 74 68 20 61 20 77 72 69 74 65 20 70 65 72  with a write per
1608e 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 74 69  formance reducti
1608f 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  on..*/.#ifndef S
16090 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
16091 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f  _PRAGMAS.SQLITE_
16092 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
16093 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74  te3BtreeSetSafet
16094 79 4c 65 76 65 6c 28 42 74 72 65 65 20 2a 70 2c  yLevel(Btree *p,
16095 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20   int level, int 
16096 66 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53  fullSync){.  BtS
16097 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
16098 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73  pBt;.  assert( s
16099 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1609a 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
1609b 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
1609c 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c  eEnter(p);.  sql
1609d 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65  ite3PagerSetSafe
1609e 74 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61  tyLevel(pBt->pPa
1609f 67 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c  ger, level, full
160a0 53 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  Sync);.  sqlite3
160a1 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
160a2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
160a3 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  K;.}.#endif../*.
160a4 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
160a5 66 20 74 68 65 20 67 69 76 65 6e 20 62 74 72 65  f the given btre
160a6 65 20 69 73 20 73 65 74 20 74 6f 20 73 61 66 65  e is set to safe
160a7 74 79 20 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20  ty level 1.  In 
160a8 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20  other.** words, 
160a9 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 6e  return TRUE if n
160aa 6f 20 73 79 6e 63 28 29 20 6f 63 63 75 72 73 20  o sync() occurs 
160ab 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65  on the disk file
160ac 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
160ad 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
160ae 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65  BtreeSyncDisable
160af 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42  d(Btree *p){.  B
160b0 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
160b1 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b  ->pBt;.  int rc;
160b2 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
160b3 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
160b4 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20  >db->mutex) );  
160b5 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
160b6 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72  nter(p);.  asser
160b7 74 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70  t( pBt && pBt->p
160b8 50 61 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20  Pager );.  rc = 
160b9 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79  sqlite3PagerNosy
160ba 6e 63 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  nc(pBt->pPager);
160bb 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
160bc 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
160bd 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65  n rc;.}..#if !de
160be 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
160bf 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29  T_PAGER_PRAGMAS)
160c0 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
160c1 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29  ITE_OMIT_VACUUM)
160c2 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
160c3 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20  e default pages 
160c4 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d  size and the num
160c5 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20  ber of reserved 
160c6 62 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a  bytes per page..
160c7 2a 2a 20 4f 72 2c 20 69 66 20 74 68 65 20 70 61  ** Or, if the pa
160c8 67 65 20 73 69 7a 65 20 68 61 73 20 61 6c 72 65  ge size has alre
160c9 61 64 79 20 62 65 65 6e 20 66 69 78 65 64 2c 20  ady been fixed, 
160ca 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45  return SQLITE_RE
160cb 41 44 4f 4e 4c 59 20 0a 2a 2a 20 77 69 74 68 6f  ADONLY .** witho
160cc 75 74 20 63 68 61 6e 67 69 6e 67 20 61 6e 79 74  ut changing anyt
160cd 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  hing..**.** The 
160ce 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20 62  page size must b
160cf 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20 62  e a power of 2 b
160d0 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36  etween 512 and 6
160d1 35 35 33 36 2e 20 20 49 66 20 74 68 65 20 70 61  5536.  If the pa
160d2 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c  ge.** size suppl
160d3 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65  ied does not mee
160d4 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  t this constrain
160d5 74 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20  t then the page 
160d6 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63  size is not.** c
160d7 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61  hanged..**.** Pa
160d8 67 65 20 73 69 7a 65 73 20 61 72 65 20 63 6f 6e  ge sizes are con
160d9 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65 20 61  strained to be a
160da 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 73 6f   power of two so
160db 20 74 68 61 74 20 74 68 65 20 72 65 67 69 6f 6e   that the region
160dc 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62  .** of the datab
160dd 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f  ase file used fo
160de 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e  r locking (begin
160df 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e 47 5f  ning at PENDING_
160e0 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72  BYTE,.** the fir
160e1 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65  st byte past the
160e2 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c 20 30   1GB boundary, 0
160e3 78 34 30 30 30 30 30 30 30 29 20 6e 65 65 64 73  x40000000) needs
160e4 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20   to occur.** at 
160e5 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
160e6 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49   a page..**.** I
160e7 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52 65 73  f parameter nRes
160e8 65 72 76 65 20 69 73 20 6c 65 73 73 20 74 68 61  erve is less tha
160e9 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n zero, then the
160ea 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72   number of reser
160eb 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 65 72  ved.** bytes per
160ec 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e   page is left un
160ed 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  changed..**.** I
160ee 66 20 74 68 65 20 69 46 69 78 21 3d 30 20 74 68  f the iFix!=0 th
160ef 65 6e 20 74 68 65 20 70 61 67 65 53 69 7a 65 46  en the pageSizeF
160f0 69 78 65 64 20 66 6c 61 67 20 69 73 20 73 65 74  ixed flag is set
160f1 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 67   so that the pag
160f2 65 20 73 69 7a 65 0a 2a 2a 20 61 6e 64 20 61 75  e size.** and au
160f3 74 6f 76 61 63 75 75 6d 20 6d 6f 64 65 20 63 61  tovacuum mode ca
160f4 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63  n no longer be c
160f5 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  hanged..*/.SQLIT
160f6 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
160f7 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67  lite3BtreeSetPag
160f8 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20  eSize(Btree *p, 
160f9 69 6e 74 20 70 61 67 65 53 69 7a 65 2c 20 69 6e  int pageSize, in
160fa 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 20  t nReserve, int 
160fb 69 46 69 78 29 7b 0a 20 20 69 6e 74 20 72 63 20  iFix){.  int rc 
160fc 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42  = SQLITE_OK;.  B
160fd 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
160fe 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28  ->pBt;.  assert(
160ff 20 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20 26 26   nReserve>=-1 &&
16100 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20 29   nReserve<=255 )
16101 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
16102 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20  Enter(p);.  if( 
16103 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78  pBt->pageSizeFix
16104 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ed ){.    sqlite
16105 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
16106 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16107 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a  E_READONLY;.  }.
16108 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30    if( nReserve<0
16109 20 29 7b 0a 20 20 20 20 6e 52 65 73 65 72 76 65   ){.    nReserve
1610a 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65   = pBt->pageSize
1610b 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   - pBt->usableSi
1610c 7a 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ze;.  }.  assert
1610d 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 26  ( nReserve>=0 &&
1610e 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20 29   nReserve<=255 )
1610f 3b 0a 20 20 69 66 28 20 70 61 67 65 53 69 7a 65  ;.  if( pageSize
16110 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a  >=512 && pageSiz
16111 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  e<=SQLITE_MAX_PA
16112 47 45 5f 53 49 5a 45 20 26 26 0a 20 20 20 20 20  GE_SIZE &&.     
16113 20 20 20 28 28 70 61 67 65 53 69 7a 65 2d 31 29     ((pageSize-1)
16114 26 70 61 67 65 53 69 7a 65 29 3d 3d 30 20 29 7b  &pageSize)==0 ){
16115 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 61  .    assert( (pa
16116 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29  geSize & 7)==0 )
16117 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  ;.    assert( !p
16118 42 74 2d 3e 70 50 61 67 65 31 20 26 26 20 21 70  Bt->pPage1 && !p
16119 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20  Bt->pCursor );. 
1611a 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65     pBt->pageSize
1611b 20 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a 65   = (u16)pageSize
1611c 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 70  ;.    freeTempSp
1611d 61 63 65 28 70 42 74 29 3b 0a 20 20 7d 0a 20 20  ace(pBt);.  }.  
1611e 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1611f 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74  rSetPagesize(pBt
16120 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e  ->pPager, &pBt->
16121 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73 65 72  pageSize, nReser
16122 76 65 29 3b 0a 20 20 70 42 74 2d 3e 75 73 61 62  ve);.  pBt->usab
16123 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61  leSize = pBt->pa
16124 67 65 53 69 7a 65 20 2d 20 28 75 31 36 29 6e 52  geSize - (u16)nR
16125 65 73 65 72 76 65 3b 0a 20 20 69 66 28 20 69 46  eserve;.  if( iF
16126 69 78 20 29 20 70 42 74 2d 3e 70 61 67 65 53 69  ix ) pBt->pageSi
16127 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 20 20 73  zeFixed = 1;.  s
16128 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
16129 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
1612a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1612b 6e 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20  n the currently 
1612c 64 65 66 69 6e 65 64 20 70 61 67 65 20 73 69 7a  defined page siz
1612d 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e.*/.SQLITE_PRIV
1612e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
1612f 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28  treeGetPageSize(
16130 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74  Btree *p){.  ret
16131 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65  urn p->pBt->page
16132 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  Size;.}../*.** R
16133 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
16134 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61   of bytes of spa
16135 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  ce at the end of
16136 20 65 76 65 72 79 20 70 61 67 65 20 74 68 61 74   every page that
16137 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 74 75 61  .** are intentua
16138 6c 6c 79 20 6c 65 66 74 20 75 6e 75 73 65 64 2e  lly left unused.
16139 20 20 54 68 69 73 20 69 73 20 74 68 65 20 22 72    This is the "r
1613a 65 73 65 72 76 65 64 22 20 73 70 61 63 65 20 74  eserved" space t
1613b 68 61 74 20 69 73 0a 2a 2a 20 73 6f 6d 65 74 69  hat is.** someti
1613c 6d 65 73 20 75 73 65 64 20 62 79 20 65 78 74 65  mes used by exte
1613d 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  nsions..*/.SQLIT
1613e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1613f 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73  lite3BtreeGetRes
16140 65 72 76 65 28 42 74 72 65 65 20 2a 70 29 7b 0a  erve(Btree *p){.
16141 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74    int n;.  sqlit
16142 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
16143 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 74 2d 3e 70  .  n = p->pBt->p
16144 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70 42 74  ageSize - p->pBt
16145 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20  ->usableSize;.  
16146 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
16147 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e  e(p);.  return n
16148 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
16149 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20  he maximum page 
1614a 63 6f 75 6e 74 20 66 6f 72 20 61 20 64 61 74 61  count for a data
1614b 62 61 73 65 20 69 66 20 6d 78 50 61 67 65 20 69  base if mxPage i
1614c 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a 20 4e  s positive..** N
1614d 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61  o changes are ma
1614e 64 65 20 69 66 20 6d 78 50 61 67 65 20 69 73 20  de if mxPage is 
1614f 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e 0a 2a  0 or negative..*
16150 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20  * Regardless of 
16151 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 78 50  the value of mxP
16152 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20  age, return the 
16153 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75  maximum page cou
16154 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
16155 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
16156 33 42 74 72 65 65 4d 61 78 50 61 67 65 43 6f 75  3BtreeMaxPageCou
16157 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  nt(Btree *p, int
16158 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 6e 74 20   mxPage){.  int 
16159 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  n;.  sqlite3Btre
1615a 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d  eEnter(p);.  n =
1615b 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 78   sqlite3PagerMax
1615c 50 61 67 65 43 6f 75 6e 74 28 70 2d 3e 70 42 74  PageCount(p->pBt
1615d 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 67 65  ->pPager, mxPage
1615e 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
1615f 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
16160 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn n;.}.#endif 
16161 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
16162 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
16163 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e  AGMAS) || !defin
16164 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
16165 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ACUUM) */../*.**
16166 20 43 68 61 6e 67 65 20 74 68 65 20 27 61 75 74   Change the 'aut
16167 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72  o-vacuum' proper
16168 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ty of the databa
16169 73 65 2e 20 49 66 20 74 68 65 20 27 61 75 74 6f  se. If the 'auto
1616a 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72 61 6d  Vacuum'.** param
1616b 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  eter is non-zero
1616c 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61 63 75  , then auto-vacu
1616d 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c  um mode is enabl
1616e 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69 74 0a  ed. If zero, it.
1616f 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 2e 20  ** is disabled. 
16170 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  The default valu
16171 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f 2d 76  e for the auto-v
16172 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79 20 69  acuum property i
16173 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 64  s .** determined
16174 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 44   by the SQLITE_D
16175 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55  EFAULT_AUTOVACUU
16176 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51 4c 49  M macro..*/.SQLI
16177 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16178 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75  qlite3BtreeSetAu
16179 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a  toVacuum(Btree *
1617a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75  p, int autoVacuu
1617b 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  m){.#ifdef SQLIT
1617c 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1617d 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  M.  return SQLIT
1617e 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65 6c 73  E_READONLY;.#els
1617f 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  e.  BtShared *pB
16180 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e  t = p->pBt;.  in
16181 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
16182 3b 0a 20 20 75 38 20 61 76 20 3d 20 28 75 38 29  ;.  u8 av = (u8)
16183 61 75 74 6f 56 61 63 75 75 6d 3b 0a 0a 20 20 73  autoVacuum;..  s
16184 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
16185 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e  (p);.  if( pBt->
16186 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 26 26  pageSizeFixed &&
16187 20 28 61 76 20 3f 31 3a 30 29 21 3d 70 42 74 2d   (av ?1:0)!=pBt-
16188 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20  >autoVacuum ){. 
16189 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52     rc = SQLITE_R
1618a 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c 73 65  EADONLY;.  }else
1618b 7b 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56  {.    pBt->autoV
1618c 61 63 75 75 6d 20 3d 20 61 76 20 3f 31 3a 30 3b  acuum = av ?1:0;
1618d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61  .    pBt->incrVa
1618e 63 75 75 6d 20 3d 20 61 76 3d 3d 32 20 3f 31 3a  cuum = av==2 ?1:
1618f 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
16190 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
16191 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64   return rc;.#end
16192 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  if.}../*.** Retu
16193 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  rn the value of 
16194 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d  the 'auto-vacuum
16195 27 20 70 72 6f 70 65 72 74 79 2e 20 49 66 20 61  ' property. If a
16196 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 0a 2a  uto-vacuum is .*
16197 2a 20 65 6e 61 62 6c 65 64 20 31 20 69 73 20 72  * enabled 1 is r
16198 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69  eturned. Otherwi
16199 73 65 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  se 0..*/.SQLITE_
1619a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1619b 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56  te3BtreeGetAutoV
1619c 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 29 7b  acuum(Btree *p){
1619d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
1619e 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1619f 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55   return BTREE_AU
161a0 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 23  TOVACUUM_NONE;.#
161a1 65 6c 73 65 0a 20 20 69 6e 74 20 72 63 3b 0a 20  else.  int rc;. 
161a2 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
161a3 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 28 0a  er(p);.  rc = (.
161a4 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 61 75      (!p->pBt->au
161a5 74 6f 56 61 63 75 75 6d 29 3f 42 54 52 45 45 5f  toVacuum)?BTREE_
161a6 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3a  AUTOVACUUM_NONE:
161a7 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 69  .    (!p->pBt->i
161a8 6e 63 72 56 61 63 75 75 6d 29 3f 42 54 52 45 45  ncrVacuum)?BTREE
161a9 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c  _AUTOVACUUM_FULL
161aa 3a 0a 20 20 20 20 42 54 52 45 45 5f 41 55 54 4f  :.    BTREE_AUTO
161ab 56 41 43 55 55 4d 5f 49 4e 43 52 0a 20 20 29 3b  VACUUM_INCR.  );
161ac 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
161ad 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
161ae 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  n rc;.#endif.}..
161af 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 72 65 66  ./*.** Get a ref
161b0 65 72 65 6e 63 65 20 74 6f 20 70 50 61 67 65 31  erence to pPage1
161b1 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
161b2 20 66 69 6c 65 2e 20 20 54 68 69 73 20 77 69 6c   file.  This wil
161b3 6c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75 69 72  l.** also acquir
161b4 65 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f 6e 20  e a readlock on 
161b5 74 68 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  that file..**.**
161b6 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
161b7 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73  turned on succes
161b8 73 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65 20  s.  If the file 
161b9 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 77 65 6c 6c  is not a.** well
161ba 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65  -formed database
161bb 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49   file, then SQLI
161bc 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20 72 65  TE_CORRUPT is re
161bd 74 75 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c 49 54  turned..** SQLIT
161be 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e  E_BUSY is return
161bf 65 64 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ed if the databa
161c0 73 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 53  se is locked.  S
161c1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69  QLITE_NOMEM.** i
161c2 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 77 65  s returned if we
161c3 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f   run out of memo
161c4 72 79 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ry. .*/.static i
161c5 6e 74 20 6c 6f 63 6b 42 74 72 65 65 28 42 74 53  nt lockBtree(BtS
161c6 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69  hared *pBt){.  i
161c7 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65  nt rc;.  MemPage
161c8 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 20   *pPage1;.  int 
161c9 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74  nPage;..  assert
161ca 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
161cb 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
161cc 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42   );.  assert( pB
161cd 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 29 3b 0a  t->pPage1==0 );.
161ce 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
161cf 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 70 42  gerSharedLock(pB
161d0 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 69 66  t->pPager);.  if
161d1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
161d2 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72  ) return rc;.  r
161d3 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
161d4 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31  (pBt, 1, &pPage1
161d5 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
161d6 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
161d7 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 20  rn rc;..  /* Do 
161d8 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20 74 6f  some checking to
161d9 20 68 65 6c 70 20 69 6e 73 75 72 65 20 74 68 65   help insure the
161da 20 66 69 6c 65 20 77 65 20 6f 70 65 6e 65 64 20   file we opened 
161db 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 61  really is.  ** a
161dc 20 76 61 6c 69 64 20 64 61 74 61 62 61 73 65 20   valid database 
161dd 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 72 63  file. .  */.  rc
161de 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50   = sqlite3PagerP
161df 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50  agecount(pBt->pP
161e0 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20  ager, &nPage);. 
161e1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
161e2 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70  OK ){.    goto p
161e3 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
161e4 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 50  ;.  }else if( nP
161e5 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74  age>0 ){.    int
161e6 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 69   pageSize;.    i
161e7 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20  nt usableSize;. 
161e8 20 20 20 75 38 20 2a 70 61 67 65 31 20 3d 20 70     u8 *page1 = p
161e9 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20  Page1->aData;.  
161ea 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
161eb 54 41 44 42 3b 0a 20 20 20 20 69 66 28 20 6d 65  TADB;.    if( me
161ec 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d 61 67  mcmp(page1, zMag
161ed 69 63 48 65 61 64 65 72 2c 20 31 36 29 21 3d 30  icHeader, 16)!=0
161ee 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70   ){.      goto p
161ef 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
161f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
161f1 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a 20  page1[18]>1 ){. 
161f2 20 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e       pBt->readOn
161f3 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ly = 1;.    }.  
161f4 20 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e    if( page1[19]>
161f5 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  1 ){.      goto 
161f6 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
161f7 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  d;.    }..    /*
161f8 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 65 6d 62   The maximum emb
161f9 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d  edded fraction m
161fa 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 32  ust be exactly 2
161fb 35 25 2e 20 20 41 6e 64 20 74 68 65 20 6d 69 6e  5%.  And the min
161fc 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 6d 62 65  imum.    ** embe
161fd 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d 75  dded fraction mu
161fe 73 74 20 62 65 20 31 32 2e 35 25 20 66 6f 72 20  st be 12.5% for 
161ff 62 6f 74 68 20 6c 65 61 66 2d 64 61 74 61 20 61  both leaf-data a
16200 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 61  nd non-leaf-data
16201 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f 72 69  ..    ** The ori
16202 67 69 6e 61 6c 20 64 65 73 69 67 6e 20 61 6c 6c  ginal design all
16203 6f 77 65 64 20 74 68 65 73 65 20 61 6d 6f 75 6e  owed these amoun
16204 74 73 20 74 6f 20 76 61 72 79 2c 20 62 75 74 20  ts to vary, but 
16205 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20 76 65 72  as of.    ** ver
16206 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 20 72  sion 3.6.0, we r
16207 65 71 75 69 72 65 20 74 68 65 6d 20 74 6f 20 62  equire them to b
16208 65 20 66 69 78 65 64 2e 0a 20 20 20 20 2a 2f 0a  e fixed..    */.
16209 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26      if( memcmp(&
1620a 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31 30 30  page1[21], "\100
1620b 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 3d 30 20  \040\040",3)!=0 
1620c 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61  ){.      goto pa
1620d 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b  ge1_init_failed;
1620e 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 65 53  .    }.    pageS
1620f 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26  ize = get2byte(&
16210 70 61 67 65 31 5b 31 36 5d 29 3b 0a 20 20 20 20  page1[16]);.    
16211 69 66 28 20 28 28 70 61 67 65 53 69 7a 65 2d 31  if( ((pageSize-1
16212 29 26 70 61 67 65 53 69 7a 65 29 21 3d 30 20 7c  )&pageSize)!=0 |
16213 7c 20 70 61 67 65 53 69 7a 65 3c 35 31 32 20 7c  | pageSize<512 |
16214 7c 0a 20 20 20 20 20 20 20 20 28 53 51 4c 49 54  |.        (SQLIT
16215 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 3c  E_MAX_PAGE_SIZE<
16216 33 32 37 36 38 20 26 26 20 70 61 67 65 53 69 7a  32768 && pageSiz
16217 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  e>SQLITE_MAX_PAG
16218 45 5f 53 49 5a 45 29 0a 20 20 20 20 29 7b 0a 20  E_SIZE).    ){. 
16219 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f       goto page1_
1621a 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20  init_failed;.   
1621b 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28   }.    assert( (
1621c 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30  pageSize & 7)==0
1621d 20 29 3b 0a 20 20 20 20 75 73 61 62 6c 65 53 69   );.    usableSi
1621e 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 20 2d 20  ze = pageSize - 
1621f 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20 20 69  page1[20];.    i
16220 66 28 20 70 61 67 65 53 69 7a 65 21 3d 70 42 74  f( pageSize!=pBt
16221 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20  ->pageSize ){.  
16222 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72 65 61      /* After rea
16223 64 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70  ding the first p
16224 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  age of the datab
16225 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61 20 70  ase assuming a p
16226 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20 20 2a  age size.      *
16227 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e 70 61  * of BtShared.pa
16228 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76 65 20  geSize, we have 
16229 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20  discovered that 
1622a 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73  the page-size is
1622b 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75 61 6c  .      ** actual
1622c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55 6e 6c  ly pageSize. Unl
1622d 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65  ock the database
1622e 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61  , leave pBt->pPa
1622f 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a 2a 20  ge1 at.      ** 
16230 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 20  zero and return 
16231 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20 63  SQLITE_OK. The c
16232 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c 20  aller will call 
16233 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20  this function.  
16234 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 74      ** again wit
16235 68 20 74 68 65 20 63 6f 72 72 65 63 74 20 70 61  h the correct pa
16236 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20 2a  ge-size..      *
16237 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  /.      releaseP
16238 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20  age(pPage1);.   
16239 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69     pBt->usableSi
1623a 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65  ze = (u16)usable
1623b 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 2d  Size;.      pBt-
1623c 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36  >pageSize = (u16
1623d 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20  )pageSize;.     
1623e 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70   freeTempSpace(p
1623f 42 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  Bt);.      rc = 
16240 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50  sqlite3PagerSetP
16241 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61  agesize(pBt->pPa
16242 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53  ger, &pBt->pageS
16243 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ize,.           
16244 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16245 20 20 20 20 20 20 20 20 70 61 67 65 53 69 7a 65          pageSize
16246 2d 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20  -usableSize);.  
16247 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
16248 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 73 61     }.    if( usa
16249 62 6c 65 53 69 7a 65 3c 34 38 30 20 29 7b 0a 20  bleSize<480 ){. 
1624a 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f       goto page1_
1624b 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20  init_failed;.   
1624c 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65   }.    pBt->page
1624d 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 67 65  Size = (u16)page
1624e 53 69 7a 65 3b 0a 20 20 20 20 70 42 74 2d 3e 75  Size;.    pBt->u
1624f 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 31 36  sableSize = (u16
16250 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23 69 66  )usableSize;.#if
16251 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16252 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
16253 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
16254 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61 67  = (get4byte(&pag
16255 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a  e1[36 + 4*4])?1:
16256 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 63  0);.    pBt->inc
16257 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62  rVacuum = (get4b
16258 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20  yte(&page1[36 + 
16259 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64  7*4])?1:0);.#end
1625a 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d 61 78  if.  }..  /* max
1625b 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d 61 78  Local is the max
1625c 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70  imum amount of p
1625d 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72 65 20  ayload to store 
1625e 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20 2a 2a  locally for.  **
1625f 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65 20 73   a cell.  Make s
16260 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c 6c 20  ure it is small 
16261 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74 20 61  enough so that a
16262 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e 6f 75  t least minFanou
16263 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63 61 6e  t.  ** cells can
16264 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f 6e 65   will fit on one
16265 20 70 61 67 65 2e 20 20 57 65 20 61 73 73 75 6d   page.  We assum
16266 65 20 61 20 31 30 2d 62 79 74 65 20 70 61 67 65  e a 10-byte page
16267 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 42 65   header..  ** Be
16268 73 69 64 65 73 20 74 68 65 20 70 61 79 6c 6f 61  sides the payloa
16269 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75 73 74  d, the cell must
1626a 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20 20 20   store:.  **    
1626b 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65 72 20   2-byte pointer 
1626c 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20 2a 2a  to the cell.  **
1626d 20 20 20 20 20 34 2d 62 79 74 65 20 63 68 69 6c       4-byte chil
1626e 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 20  d pointer.  **  
1626f 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79 20 76     9-byte nKey v
16270 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d  alue.  **     4-
16271 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c 75 65  byte nData value
16272 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65  .  **     4-byte
16273 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70   overflow page p
16274 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f 20 61  ointer.  ** So a
16275 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f   cell consists o
16276 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69 6e 65  f a 2-byte poine
16277 72 2c 20 61 20 68 65 61 64 65 72 20 77 68 69 63  r, a header whic
16278 68 20 69 73 20 61 73 20 6d 75 63 68 20 61 73 0a  h is as much as.
16279 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20 6c 6f    ** 17 bytes lo
1627a 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74 65 73  ng, 0 to N bytes
1627b 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61 6e 64   of payload, and
1627c 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34 20 62   an optional 4 b
1627d 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 2a  yte overflow.  *
1627e 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2e 0a  * page pointer..
1627f 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61 78 4c    */.  pBt->maxL
16280 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75 73 61  ocal = (pBt->usa
16281 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34 2f 32  bleSize-12)*64/2
16282 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74 2d 3e  55 - 23;.  pBt->
16283 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d  minLocal = (pBt-
16284 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a  >usableSize-12)*
16285 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 70  32/255 - 23;.  p
16286 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20 70 42  Bt->maxLeaf = pB
16287 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
16288 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 65  35;.  pBt->minLe
16289 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c  af = (pBt->usabl
1628a 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35  eSize-12)*32/255
1628b 20 2d 20 32 33 3b 0a 20 20 61 73 73 65 72 74 28   - 23;.  assert(
1628c 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 2b 20   pBt->maxLeaf + 
1628d 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f 53 49  23 <= MX_CELL_SI
1628e 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 70 42 74  ZE(pBt) );.  pBt
1628f 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 67 65  ->pPage1 = pPage
16290 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  1;.  return SQLI
16291 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f 69 6e  TE_OK;..page1_in
16292 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72 65 6c  it_failed:.  rel
16293 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29  easePage(pPage1)
16294 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20  ;.  pBt->pPage1 
16295 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  = 0;.  return rc
16296 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
16297 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74  ere are no outst
16298 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 20 61  anding cursors a
16299 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e  nd we are not in
1629a 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f   the middle.** o
1629b 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
1629c 62 75 74 20 74 68 65 72 65 20 69 73 20 61 20 72  but there is a r
1629d 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead lock on the 
1629e 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a  database, then.*
1629f 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 75  * this routine u
162a0 6e 72 65 66 73 20 74 68 65 20 66 69 72 73 74 20  nrefs the first 
162a1 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
162a2 62 61 73 65 20 66 69 6c 65 20 77 68 69 63 68 20  base file which 
162a3 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66 66 65  .** has the effe
162a4 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e 67 20  ct of releasing 
162a5 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a  the read lock..*
162a6 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  *.** If there is
162a7 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
162a8 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 69 73  n progress, this
162a9 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
162aa 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  -op..*/.static v
162ab 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49  oid unlockBtreeI
162ac 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 65 64  fUnused(BtShared
162ad 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 72 74   *pBt){.  assert
162ae 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
162af 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
162b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42   );.  assert( pB
162b1 74 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 7c 7c  t->pCursor==0 ||
162b2 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
162b3 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ion>TRANS_NONE )
162b4 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 54  ;.  if( pBt->inT
162b5 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e  ransaction==TRAN
162b6 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 70  S_NONE && pBt->p
162b7 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 20 20  Page1!=0 ){.    
162b8 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61  assert( pBt->pPa
162b9 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20  ge1->aData );.  
162ba 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
162bb 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70  3PagerRefcount(p
162bc 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 31 20 29  Bt->pPager)==1 )
162bd 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
162be 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
162bf 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50   );.    releaseP
162c0 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 31 29  age(pBt->pPage1)
162c1 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65  ;.    pBt->pPage
162c2 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  1 = 0;.  }.}../*
162c3 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69 6e 74  .** If pBt point
162c4 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 66 69  s to an empty fi
162c5 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20  le then convert 
162c6 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c 65 0a  that empty file.
162c7 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20 65 6d  ** into a new em
162c8 70 74 79 20 64 61 74 61 62 61 73 65 20 62 79 20  pty database by 
162c9 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65  initializing the
162ca 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 0a 2a   first page of.*
162cb 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  * the database..
162cc 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65  */.static int ne
162cd 77 44 61 74 61 62 61 73 65 28 42 74 53 68 61 72  wDatabase(BtShar
162ce 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50  ed *pBt){.  MemP
162cf 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 69  age *pP1;.  unsi
162d0 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b  gned char *data;
162d1 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
162d2 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72   nPage;..  asser
162d3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
162d4 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
162d5 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 64 61  ) );.  /* The da
162d6 74 61 62 61 73 65 20 73 69 7a 65 20 68 61 73 20  tabase size has 
162d7 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d 65 61  already been mea
162d8 73 75 72 65 64 20 61 6e 64 20 63 61 63 68 65 64  sured and cached
162d9 2c 20 73 6f 20 66 61 69 6c 75 72 65 0a 20 20 2a  , so failure.  *
162da 2a 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20  * is impossible 
162db 68 65 72 65 2e 20 20 49 66 20 74 68 65 20 6f 72  here.  If the or
162dc 69 67 69 6e 61 6c 20 73 69 7a 65 20 6d 65 61 73  iginal size meas
162dd 75 72 65 6d 65 6e 74 20 66 61 69 6c 65 64 2c 20  urement failed, 
162de 74 68 65 6e 0a 20 20 2a 2a 20 70 72 6f 63 65 73  then.  ** proces
162df 73 69 6e 67 20 61 62 6f 72 74 73 20 62 65 66 6f  sing aborts befo
162e0 72 65 20 65 6e 74 65 72 69 6e 67 20 74 68 69 73  re entering this
162e1 20 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20 20 72   routine. */.  r
162e2 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
162e3 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70  Pagecount(pBt->p
162e4 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a  Pager, &nPage);.
162e5 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d    if( NEVER(rc!=
162e6 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c 20 6e 50  SQLITE_OK) || nP
162e7 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 72 65 74  age>0 ){.    ret
162e8 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50  urn rc;.  }.  pP
162e9 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b  1 = pBt->pPage1;
162ea 0a 20 20 61 73 73 65 72 74 28 20 70 50 31 21 3d  .  assert( pP1!=
162eb 30 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50  0 );.  data = pP
162ec 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20 3d  1->aData;.  rc =
162ed 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
162ee 74 65 28 70 50 31 2d 3e 70 44 62 50 61 67 65 29  te(pP1->pDbPage)
162ef 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
162f0 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63 70 79  urn rc;.  memcpy
162f1 28 64 61 74 61 2c 20 7a 4d 61 67 69 63 48 65 61  (data, zMagicHea
162f2 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d 61 67  der, sizeof(zMag
162f3 69 63 48 65 61 64 65 72 29 29 3b 0a 20 20 61 73  icHeader));.  as
162f4 73 65 72 74 28 20 73 69 7a 65 6f 66 28 7a 4d 61  sert( sizeof(zMa
162f5 67 69 63 48 65 61 64 65 72 29 3d 3d 31 36 20 29  gicHeader)==16 )
162f6 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
162f7 74 61 5b 31 36 5d 2c 20 70 42 74 2d 3e 70 61 67  ta[16], pBt->pag
162f8 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 31  eSize);.  data[1
162f9 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 31  8] = 1;.  data[1
162fa 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74  9] = 1;.  assert
162fb 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ( pBt->usableSiz
162fc 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  e<=pBt->pageSize
162fd 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c 65 53   && pBt->usableS
162fe 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e 70 61  ize+255>=pBt->pa
162ff 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b  geSize);.  data[
16300 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74 2d 3e  20] = (u8)(pBt->
16301 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e  pageSize - pBt->
16302 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 64  usableSize);.  d
16303 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20 20  ata[21] = 64;.  
16304 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a 20  data[22] = 32;. 
16305 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b 0a   data[23] = 32;.
16306 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 32    memset(&data[2
16307 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b 0a  4], 0, 100-24);.
16308 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c 20    zeroPage(pP1, 
16309 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c  PTF_INTKEY|PTF_L
1630a 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54 41  EAF|PTF_LEAFDATA
1630b 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 67 65 53   );.  pBt->pageS
1630c 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23 69  izeFixed = 1;.#i
1630d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1630e 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 61  T_AUTOVACUUM.  a
1630f 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f  ssert( pBt->auto
16310 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74  Vacuum==1 || pBt
16311 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 30 20  ->autoVacuum==0 
16312 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  );.  assert( pBt
16313 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 31 20  ->incrVacuum==1 
16314 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75  || pBt->incrVacu
16315 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74 34 62  um==0 );.  put4b
16316 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 34  yte(&data[36 + 4
16317 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f 56 61  *4], pBt->autoVa
16318 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 62 79 74  cuum);.  put4byt
16319 65 28 26 64 61 74 61 5b 33 36 20 2b 20 37 2a 34  e(&data[36 + 7*4
1631a 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75  ], pBt->incrVacu
1631b 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65  um);.#endif.  re
1631c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1631d 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74  }../*.** Attempt
1631e 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65 77 20   to start a new 
1631f 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 77  transaction. A w
16320 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
16321 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 64 20 69  .** is started i
16322 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
16323 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a 65 72 6f  ument is nonzero
16324 2c 20 6f 74 68 65 72 77 69 73 65 20 61 20 72 65  , otherwise a re
16325 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  ad-.** transacti
16326 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f  on.  If the seco
16327 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 32  nd argument is 2
16328 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 65 78 63   or more and exc
16329 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61  lusive.** transa
1632a 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64  ction is started
1632b 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 6e  , meaning that n
1632c 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  o other process 
1632d 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 6f  is allowed.** to
1632e 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61   access the data
1632f 62 61 73 65 2e 20 20 41 20 70 72 65 65 78 69 73  base.  A preexis
16330 74 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f 6e  ting transaction
16331 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a 2a 20 75   may not be.** u
16332 70 67 72 61 64 65 64 20 74 6f 20 65 78 63 6c 75  pgraded to exclu
16333 73 69 76 65 20 62 79 20 63 61 6c 6c 69 6e 67 20  sive by calling 
16334 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 20 73  this routine a s
16335 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 74 68 65  econd time - the
16336 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 74 79 20  .** exclusivity 
16337 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  flag only works 
16338 66 6f 72 20 61 20 6e 65 77 20 74 72 61 6e 73 61  for a new transa
16339 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 77  ction..**.** A w
1633a 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
1633b 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65 64   must be started
1633c 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69   before attempti
1633d 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 61 6e 67  ng any .** chang
1633e 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  es to the databa
1633f 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65  se.  None of the
16340 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
16341 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 77 6f 72  nes .** will wor
16342 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 61 6e 73  k unless a trans
16343 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65  action is starte
16344 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a 2a 20 20  d first:.**.**  
16345 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
16346 43 72 65 61 74 65 54 61 62 6c 65 28 29 0a 2a 2a  CreateTable().**
16347 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
16348 65 65 43 72 65 61 74 65 49 6e 64 65 78 28 29 0a  eeCreateIndex().
16349 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  **      sqlite3B
1634a 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 29  treeClearTable()
1634b 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
1634c 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 29  BtreeDropTable()
1634d 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
1634e 42 74 72 65 65 49 6e 73 65 72 74 28 29 0a 2a 2a  BtreeInsert().**
1634f 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
16350 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a 20 20 20  eeDelete().**   
16351 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 55     sqlite3BtreeU
16352 70 64 61 74 65 4d 65 74 61 28 29 0a 2a 2a 0a 2a  pdateMeta().**.*
16353 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 61 6c 20  * If an initial 
16354 61 74 74 65 6d 70 74 20 74 6f 20 61 63 71 75 69  attempt to acqui
16355 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 61 69 6c  re the lock fail
16356 73 20 62 65 63 61 75 73 65 20 6f 66 20 6c 6f 63  s because of loc
16357 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a 2a 2a 20  k contention.** 
16358 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  and the database
16359 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20   was previously 
1635a 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 69  unlocked, then i
1635b 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68  nvoke the busy h
1635c 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 74 68 65  andler.** if the
1635d 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 75 74 20  re is one.  But 
1635e 69 66 20 74 68 65 72 65 20 77 61 73 20 70 72 65  if there was pre
1635f 76 69 6f 75 73 6c 79 20 61 20 72 65 61 64 2d 6c  viously a read-l
16360 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 69  ock, do not.** i
16361 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68  nvoke the busy h
16362 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 20 72 65  andler - just re
16363 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
16364 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69  .  SQLITE_BUSY i
16365 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 77  s .** returned w
16366 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6c 72  hen there is alr
16367 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b  eady a read-lock
16368 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f   in order to avo
16369 69 64 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a  id a deadlock..*
1636a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65  *.** Suppose the
1636b 72 65 20 61 72 65 20 74 77 6f 20 70 72 6f 63 65  re are two proce
1636c 73 73 65 73 20 41 20 61 6e 64 20 42 2e 20 20 41  sses A and B.  A
1636d 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b   has a read lock
1636e 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a 20 61 20   and B has.** a 
1636f 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20 20  reserved lock.  
16370 42 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f  B tries to promo
16371 74 65 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20  te to exclusive 
16372 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62  but is blocked b
16373 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 41 27 73  ecause.** of A's
16374 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 41 20 74   read lock.  A t
16375 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20  ries to promote 
16376 74 6f 20 72 65 73 65 72 76 65 64 20 62 75 74 20  to reserved but 
16377 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 42 2e  is blocked by B.
16378 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65 20 6f  .** One or the o
16379 74 68 65 72 20 6f 66 20 74 68 65 20 74 77 6f 20  ther of the two 
1637a 70 72 6f 63 65 73 73 65 73 20 6d 75 73 74 20 67  processes must g
1637b 69 76 65 20 77 61 79 20 6f 72 20 74 68 65 72 65  ive way or there
1637c 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f 20 70 72   can be.** no pr
1637d 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65 74 75  ogress.  By retu
1637e 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42 55 53  rning SQLITE_BUS
1637f 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f 6b 69  Y and not invoki
16380 6e 67 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  ng the busy call
16381 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 41 20 61  back.** when A a
16382 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 65 61  lready has a rea
16383 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63 6f 75  d lock, we encou
16384 72 61 67 65 20 41 20 74 6f 20 67 69 76 65 20 75  rage A to give u
16385 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a 2a 20 70  p and let B.** p
16386 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  roceed..*/.SQLIT
16387 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
16388 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
16389 72 61 6e 73 28 42 74 72 65 65 20 2a 70 2c 20 69  rans(Btree *p, i
1638a 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 20 73 71  nt wrflag){.  sq
1638b 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 20 3d 20  lite3 *pBlock = 
1638c 30 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  0;.  BtShared *p
1638d 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69  Bt = p->pBt;.  i
1638e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1638f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72  K;..  sqlite3Btr
16390 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 62 74  eeEnter(p);.  bt
16391 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b  reeIntegrity(p);
16392 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 62 74  ..  /* If the bt
16393 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 69  ree is already i
16394 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61  n a write-transa
16395 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20 20 2a  ction, or it.  *
16396 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  * is already in 
16397 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69  a read-transacti
16398 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d 74 72  on and a read-tr
16399 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 69  ansaction.  ** i
1639a 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 69  s requested, thi
1639b 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20  s is a no-op..  
1639c 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  */.  if( p->inTr
1639d 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
1639e 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d   || (p->inTrans=
1639f 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26 20 21  =TRANS_READ && !
163a0 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20 20 67  wrflag) ){.    g
163a1 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b  oto trans_begun;
163a2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74 65  .  }..  /* Write
163a3 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72   transactions ar
163a4 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 6f  e not possible o
163a5 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61  n a read-only da
163a6 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66 28 20  tabase */.  if( 
163a7 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26  pBt->readOnly &&
163a8 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 72   wrflag ){.    r
163a9 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  c = SQLITE_READO
163aa 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72  NLY;.    goto tr
163ab 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a  ans_begun;.  }..
163ac 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
163ad 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
163ae 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65 72  .  /* If another
163af 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
163b0 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f 70 65   has already ope
163b1 6e 65 64 20 61 20 77 72 69 74 65 20 74 72 61 6e  ned a write tran
163b2 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f 6e  saction .  ** on
163b3 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 74 72   this shared-btr
163b4 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  ee structure and
163b5 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74 65 20   a second write 
163b6 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 20  transaction is. 
163b7 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c 20 72   ** requested, r
163b8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
163b9 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  KED..  */.  if( 
163ba 28 77 72 66 6c 61 67 20 26 26 20 70 42 74 2d 3e  (wrflag && pBt->
163bb 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
163bc 52 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c 20 70  RANS_WRITE) || p
163bd 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 29 7b  Bt->isPending ){
163be 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 42  .    pBlock = pB
163bf 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 3b 0a  t->pWriter->db;.
163c0 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 66 6c    }else if( wrfl
163c1 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74 4c 6f  ag>1 ){.    BtLo
163c2 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20 20 66  ck *pIter;.    f
163c3 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c  or(pIter=pBt->pL
163c4 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65  ock; pIter; pIte
163c5 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b  r=pIter->pNext){
163c6 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 72  .      if( pIter
163c7 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b 0a 20  ->pBtree!=p ){. 
163c8 20 20 20 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20         pBlock = 
163c9 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64  pIter->pBtree->d
163ca 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  b;.        break
163cb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
163cc 20 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f 63 6b    }.  if( pBlock
163cd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43   ){.    sqlite3C
163ce 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64  onnectionBlocked
163cf 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b 29 3b  (p->db, pBlock);
163d0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
163d1 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41  _LOCKED_SHAREDCA
163d2 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72  CHE;.    goto tr
163d3 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 23  ans_begun;.  }.#
163d4 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e 79 20  endif..  /* Any 
163d5 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 61  read-only or rea
163d6 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  d-write transact
163d7 69 6f 6e 20 69 6d 70 6c 69 65 73 20 61 20 72 65  ion implies a re
163d8 61 64 2d 6c 6f 63 6b 20 6f 6e 20 0a 20 20 2a 2a  ad-lock on .  **
163d9 20 70 61 67 65 20 31 2e 20 53 6f 20 69 66 20 73   page 1. So if s
163da 6f 6d 65 20 6f 74 68 65 72 20 73 68 61 72 65 64  ome other shared
163db 2d 63 61 63 68 65 20 63 6c 69 65 6e 74 20 61 6c  -cache client al
163dc 72 65 61 64 79 20 68 61 73 20 61 20 77 72 69 74  ready has a writ
163dd 65 2d 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f 6e 20  e-lock .  ** on 
163de 70 61 67 65 20 31 2c 20 74 68 65 20 74 72 61 6e  page 1, the tran
163df 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62  saction cannot b
163e0 65 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 72  e opened. */.  r
163e1 63 20 3d 20 71 75 65 72 79 53 68 61 72 65 64 43  c = querySharedC
163e2 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  acheTableLock(p,
163e3 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45   MASTER_ROOT, RE
163e4 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20  AD_LOCK);.  if( 
163e5 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 20  SQLITE_OK!=rc ) 
163e6 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e  goto trans_begun
163e7 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 2f 2a  ;..  do {.    /*
163e8 20 43 61 6c 6c 20 6c 6f 63 6b 42 74 72 65 65 28   Call lockBtree(
163e9 29 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 70  ) until either p
163ea 42 74 2d 3e 70 50 61 67 65 31 20 69 73 20 70 6f  Bt->pPage1 is po
163eb 70 75 6c 61 74 65 64 20 6f 72 0a 20 20 20 20 2a  pulated or.    *
163ec 2a 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 72 65  * lockBtree() re
163ed 74 75 72 6e 73 20 73 6f 6d 65 74 68 69 6e 67 20  turns something 
163ee 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54  other than SQLIT
163ef 45 5f 4f 4b 2e 20 6c 6f 63 6b 42 74 72 65 65 28  E_OK. lockBtree(
163f0 29 0a 20 20 20 20 2a 2a 20 6d 61 79 20 72 65 74  ).    ** may ret
163f1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 62 75  urn SQLITE_OK bu
163f2 74 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61  t leave pBt->pPa
163f3 67 65 31 20 73 65 74 20 74 6f 20 30 20 69 66 20  ge1 set to 0 if 
163f4 61 66 74 65 72 0a 20 20 20 20 2a 2a 20 72 65 61  after.    ** rea
163f5 64 69 6e 67 20 70 61 67 65 20 31 20 69 74 20 64  ding page 1 it d
163f6 69 73 63 6f 76 65 72 73 20 74 68 61 74 20 74 68  iscovers that th
163f7 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74  e page-size of t
163f8 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 20  he database .   
163f9 20 2a 2a 20 66 69 6c 65 20 69 73 20 6e 6f 74 20   ** file is not 
163fa 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2e 20 49  pBt->pageSize. I
163fb 6e 20 74 68 69 73 20 63 61 73 65 20 6c 6f 63 6b  n this case lock
163fc 42 74 72 65 65 28 29 20 77 69 6c 6c 20 75 70 64  Btree() will upd
163fd 61 74 65 0a 20 20 20 20 2a 2a 20 70 42 74 2d 3e  ate.    ** pBt->
163fe 70 61 67 65 53 69 7a 65 20 74 6f 20 74 68 65 20  pageSize to the 
163ff 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74 68 65  page-size of the
16400 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 0a 20   file on disk.. 
16401 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28     */.    while(
16402 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20   pBt->pPage1==0 
16403 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
16404 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 28 70 42  c = lockBtree(pB
16405 74 29 29 20 29 3b 0a 0a 20 20 20 20 69 66 28 20  t)) );..    if( 
16406 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
16407 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 20   wrflag ){.     
16408 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e   if( pBt->readOn
16409 6c 79 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  ly ){.        rc
1640a 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e   = SQLITE_READON
1640b 4c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  LY;.      }else{
1640c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1640d 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28  lite3PagerBegin(
1640e 70 42 74 2d 3e 70 50 61 67 65 72 2c 77 72 66 6c  pBt->pPager,wrfl
1640f 61 67 3e 31 2c 73 71 6c 69 74 65 33 54 65 6d 70  ag>1,sqlite3Temp
16410 49 6e 4d 65 6d 6f 72 79 28 70 2d 3e 64 62 29 29  InMemory(p->db))
16411 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
16412 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
16413 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 65           rc = ne
16414 77 44 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a  wDatabase(pBt);.
16415 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
16416 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69  }.    }.  .    i
16417 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
16418 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b   ){.      unlock
16419 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42  BtreeIfUnused(pB
1641a 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69  t);.    }.  }whi
1641b 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42  le( rc==SQLITE_B
1641c 55 53 59 20 26 26 20 70 42 74 2d 3e 69 6e 54 72  USY && pBt->inTr
1641d 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
1641e 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20 20 20 20  _NONE &&.       
1641f 20 20 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75     btreeInvokeBu
16420 73 79 48 61 6e 64 6c 65 72 28 70 42 74 29 20 29  syHandler(pBt) )
16421 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
16422 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
16423 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
16424 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20  ANS_NONE ){.    
16425 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74    pBt->nTransact
16426 69 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 53  ion++;.#ifndef S
16427 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
16428 44 5f 43 41 43 48 45 0a 20 20 20 20 20 20 69 66  D_CACHE.      if
16429 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b  ( p->sharable ){
1642a 0a 09 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63  ..assert( p->loc
1642b 6b 2e 70 42 74 72 65 65 3d 3d 70 20 26 26 20 70  k.pBtree==p && p
1642c 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c 65 3d 3d 31  ->lock.iTable==1
1642d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c   );.        p->l
1642e 6f 63 6b 2e 65 4c 6f 63 6b 20 3d 20 52 45 41 44  ock.eLock = READ
1642f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 70  _LOCK;.        p
16430 2d 3e 6c 6f 63 6b 2e 70 4e 65 78 74 20 3d 20 70  ->lock.pNext = p
16431 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 20  Bt->pLock;.     
16432 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20     pBt->pLock = 
16433 26 70 2d 3e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20  &p->lock;.      
16434 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20  }.#endif.    }. 
16435 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20     p->inTrans = 
16436 28 77 72 66 6c 61 67 3f 54 52 41 4e 53 5f 57 52  (wrflag?TRANS_WR
16437 49 54 45 3a 54 52 41 4e 53 5f 52 45 41 44 29 3b  ITE:TRANS_READ);
16438 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  .    if( p->inTr
16439 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73  ans>pBt->inTrans
1643a 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  action ){.      
1643b 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
1643c 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 6e 73 3b  on = p->inTrans;
1643d 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
1643e 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1643f 44 5f 43 41 43 48 45 0a 20 20 20 20 69 66 28 20  D_CACHE.    if( 
16440 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20  wrflag ){.      
16441 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 57  assert( !pBt->pW
16442 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 70  riter );.      p
16443 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 70 3b  Bt->pWriter = p;
16444 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 73 45 78  .      pBt->isEx
16445 63 6c 75 73 69 76 65 20 3d 20 28 75 38 29 28 77  clusive = (u8)(w
16446 72 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20 7d 0a  rflag>1);.    }.
16447 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0a 74 72 61  #endif.  }...tra
16448 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 66 28 20  ns_begun:.  if( 
16449 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1644a 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 2f   wrflag ){.    /
1644b 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 61 6b 65  * This call make
1644c 73 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  s sure that the 
1644d 70 61 67 65 72 20 68 61 73 20 74 68 65 20 63 6f  pager has the co
1644e 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 0a  rrect number of.
1644f 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61 76 65      ** open save
16450 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 65 20 73  points. If the s
16451 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
16452 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
16453 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68 65  0 and.    ** the
16454 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20   sub-journal is 
16455 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e  not already open
16456 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62  , then it will b
16457 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e 0a 20  e opened here.. 
16458 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73     */.    rc = s
16459 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53  qlite3PagerOpenS
1645a 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50  avepoint(pBt->pP
1645b 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e 6e 53 61  ager, p->db->nSa
1645c 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 0a 20  vepoint);.  }.. 
1645d 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28   btreeIntegrity(
1645e 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  p);.  sqlite3Btr
1645f 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
16460 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e  turn rc;.}..#ifn
16461 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
16462 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a 0a 2a  AUTOVACUUM../*.*
16463 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 65  * Set the pointe
16464 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f  r-map entries fo
16465 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f  r all children o
16466 66 20 70 61 67 65 20 70 50 61 67 65 2e 20 41 6c  f page pPage. Al
16467 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 65 20  so, if.** pPage 
16468 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 20 74  contains cells t
16469 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65  hat point to ove
1646a 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 65 74  rflow pages, set
1646b 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20   the pointer.** 
1646c 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20  map entries for 
1646d 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  the overflow pag
1646e 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 73  es as well..*/.s
1646f 74 61 74 69 63 20 69 6e 74 20 73 65 74 43 68 69  tatic int setChi
16470 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 61 67  ldPtrmaps(MemPag
16471 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74  e *pPage){.  int
16472 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
16473 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16474 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69 61  /* Counter varia
16475 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65  ble */.  int nCe
16476 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
16477 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
16478 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69  umber of cells i
16479 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a 2f 0a  n page pPage */.
1647a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
1647b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1647c 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
1647d 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65  ode */.  BtShare
1647e 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  d *pBt = pPage->
1647f 70 42 74 3b 0a 20 20 75 38 20 69 73 49 6e 69 74  pBt;.  u8 isInit
16480 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73  Orig = pPage->is
16481 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e  Init;.  Pgno pgn
16482 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3b  o = pPage->pgno;
16483 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
16484 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
16485 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
16486 29 20 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65  ) );.  rc = btre
16487 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29  eInitPage(pPage)
16488 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
16489 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74  TE_OK ){.    got
1648a 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d  o set_child_ptrm
1648b 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e  aps_out;.  }.  n
1648c 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43  Cell = pPage->nC
1648d 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  ell;..  for(i=0;
1648e 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a   i<nCell; i++){.
1648f 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20      u8 *pCell = 
16490 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
16491 69 29 3b 0a 0a 20 20 20 20 70 74 72 6d 61 70 50  i);..    ptrmapP
16492 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c  utOvflPtr(pPage,
16493 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 0a 20   pCell, &rc);.. 
16494 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
16495 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e  eaf ){.      Pgn
16496 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65  o childPgno = ge
16497 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20  t4byte(pCell);. 
16498 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70       ptrmapPut(p
16499 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50  Bt, childPgno, P
1649a 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e  TRMAP_BTREE, pgn
1649b 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20  o, &rc);.    }. 
1649c 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65   }..  if( !pPage
1649d 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67  ->leaf ){.    Pg
1649e 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67  no childPgno = g
1649f 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
164a0 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
164a1 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
164a2 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 63  ptrmapPut(pBt, c
164a3 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d 41 50  hildPgno, PTRMAP
164a4 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20 26 72  _BTREE, pgno, &r
164a5 63 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 68 69  c);.  }..set_chi
164a6 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3a 0a  ld_ptrmaps_out:.
164a7 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
164a8 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20  = isInitOrig;.  
164a9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
164aa 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f 6e  .** Somewhere on
164ab 20 70 50 61 67 65 20 69 73 20 61 20 70 6f 69 6e   pPage is a poin
164ac 74 65 72 20 74 6f 20 70 61 67 65 20 69 46 72 6f  ter to page iFro
164ad 6d 2e 20 20 4d 6f 64 69 66 79 20 74 68 69 73 20  m.  Modify this 
164ae 70 6f 69 6e 74 65 72 20 73 6f 0a 2a 2a 20 74 68  pointer so.** th
164af 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
164b0 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 20 65  iTo. Parameter e
164b1 54 79 70 65 20 64 65 73 63 72 69 62 65 73 20 74  Type describes t
164b2 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 6e 74  he type of point
164b3 65 72 20 74 6f 0a 2a 2a 20 62 65 20 6d 6f 64 69  er to.** be modi
164b4 66 69 65 64 2c 20 61 73 20 20 66 6f 6c 6c 6f 77  fied, as  follow
164b5 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  s:.**.** PTRMAP_
164b6 42 54 52 45 45 3a 20 20 20 20 20 70 50 61 67 65  BTREE:     pPage
164b7 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 67 65   is a btree-page
164b8 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f  . The pointer po
164b9 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c 64 20  ints at a child 
164ba 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
164bb 20 20 20 20 20 20 70 61 67 65 20 6f 66 20 70 50        page of pP
164bc 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  age..**.** PTRMA
164bd 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 50 61  P_OVERFLOW1: pPa
164be 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61  ge is a btree-pa
164bf 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  ge. The pointer 
164c0 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f 76 65  points at an ove
164c1 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  rflow.**        
164c2 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20             page 
164c3 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 6f 6e  pointed to by on
164c4 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f  e of the cells o
164c5 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50  n pPage..**.** P
164c6 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a  TRMAP_OVERFLOW2:
164c7 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65   pPage is an ove
164c8 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 65 20  rflow-page. The 
164c9 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61  pointer points a
164ca 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20  t the next.**   
164cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
164cc 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e  overflow page in
164cd 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74   the list..*/.st
164ce 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 79 50  atic int modifyP
164cf 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d 50 61  agePointer(MemPa
164d0 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e 6f 20  ge *pPage, Pgno 
164d1 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 6f 2c  iFrom, Pgno iTo,
164d2 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 61 73   u8 eType){.  as
164d3 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
164d4 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
164d5 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
164d6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
164d7 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
164d8 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
164d9 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 65 3d   );.  if( eType=
164da 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57  =PTRMAP_OVERFLOW
164db 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  2 ){.    /* The 
164dc 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79  pointer is alway
164dd 73 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79  s the first 4 by
164de 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20  tes of the page 
164df 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a  in this case.  *
164e0 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 62 79  /.    if( get4by
164e1 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29  te(pPage->aData)
164e2 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  !=iFrom ){.     
164e3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
164e4 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
164e5 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 65 28   }.    put4byte(
164e6 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54  pPage->aData, iT
164e7 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  o);.  }else{.   
164e8 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 20 3d   u8 isInitOrig =
164e9 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a   pPage->isInit;.
164ea 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69      int i;.    i
164eb 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 62  nt nCell;..    b
164ec 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61  treeInitPage(pPa
164ed 67 65 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d  ge);.    nCell =
164ee 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a   pPage->nCell;..
164ef 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
164f0 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Cell; i++){.    
164f1 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69    u8 *pCell = fi
164f2 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29  ndCell(pPage, i)
164f3 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70  ;.      if( eTyp
164f4 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  e==PTRMAP_OVERFL
164f5 4f 57 31 20 29 7b 0a 20 20 20 20 20 20 20 20 43  OW1 ){.        C
164f6 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20  ellInfo info;.  
164f7 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 65        btreeParse
164f8 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
164f9 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  Cell, &info);.  
164fa 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69        if( info.i
164fb 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
164fc 20 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d        if( iFrom=
164fd 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c  =get4byte(&pCell
164fe 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
164ff 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
16500 20 70 75 74 34 62 79 74 65 28 26 70 43 65 6c 6c   put4byte(&pCell
16501 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
16502 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20  , iTo);.        
16503 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
16504 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
16505 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
16506 20 20 20 20 20 20 69 66 28 20 67 65 74 34 62 79        if( get4by
16507 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d  te(pCell)==iFrom
16508 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75   ){.          pu
16509 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54  t4byte(pCell, iT
1650a 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  o);.          br
1650b 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
1650c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a       }.    }.  .
1650d 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c      if( i==nCell
1650e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 54   ){.      if( eT
1650f 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 52 45  ype!=PTRMAP_BTRE
16510 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20  E || .          
16511 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
16512 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
16513 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 46 72  rOffset+8])!=iFr
16514 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  om ){.        re
16515 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
16516 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
16517 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  }.      put4byte
16518 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  (&pPage->aData[p
16519 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
1651a 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 7d 0a  8], iTo);.    }.
1651b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e  .    pPage->isIn
1651c 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b  it = isInitOrig;
1651d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
1651e 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
1651f 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 65 6e  ** Move the open
16520 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70   database page p
16521 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 74 69  DbPage to locati
16522 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69 6e 20  on iFreePage in 
16523 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  the .** database
16524 2e 20 54 68 65 20 70 44 62 50 61 67 65 20 72 65  . The pDbPage re
16525 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e 73 20  ference remains 
16526 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  valid..**.** The
16527 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69   isCommit flag i
16528 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68  ndicates that th
16529 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74  ere is no need t
1652a 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74 0a  o remember that.
1652b 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e  ** the journal n
1652c 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28  eeds to be sync(
1652d 29 65 64 20 62 65 66 6f 72 65 20 64 61 74 61 62  )ed before datab
1652e 61 73 65 20 70 61 67 65 20 70 44 62 50 61 67 65  ase page pDbPage
1652f 2d 3e 70 67 6e 6f 20 0a 2a 2a 20 63 61 6e 20 62  ->pgno .** can b
16530 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 54 68  e written to. Th
16531 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 72  e caller has alr
16532 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20 6e 6f  eady promised no
16533 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68  t to write to th
16534 61 74 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2f 0a 73  at.** page..*/.s
16535 74 61 74 69 63 20 69 6e 74 20 72 65 6c 6f 63 61  tatic int reloca
16536 74 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 72  tePage(.  BtShar
16537 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20  ed *pBt,        
16538 20 20 20 2f 2a 20 42 74 72 65 65 20 2a 2f 0a 20     /* Btree */. 
16539 20 4d 65 6d 50 61 67 65 20 2a 70 44 62 50 61 67   MemPage *pDbPag
1653a 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65  e,        /* Ope
1653b 6e 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 2a  n page to move *
1653c 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20  /.  u8 eType,   
1653d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1653e 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 74 79 70  Pointer map 'typ
1653f 65 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62  e' entry for pDb
16540 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  Page */.  Pgno i
16541 50 74 72 50 61 67 65 2c 20 20 20 20 20 20 20 20  PtrPage,        
16542 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61     /* Pointer ma
16543 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 6e 74 72  p 'page-no' entr
16544 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f  y for pDbPage */
16545 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 50 61 67  .  Pgno iFreePag
16546 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e,          /* T
16547 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 6d  he location to m
16548 6f 76 65 20 70 44 62 50 61 67 65 20 74 6f 20 2a  ove pDbPage to *
16549 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74  /.  int isCommit
1654a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1654b 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 70 61  isCommit flag pa
1654c 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 50  ssed to sqlite3P
1654d 61 67 65 72 4d 6f 76 65 70 61 67 65 20 2a 2f 0a  agerMovepage */.
1654e 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  ){.  MemPage *pP
1654f 74 72 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65  trPage;   /* The
16550 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61   page that conta
16551 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ins a pointer to
16552 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67   pDbPage */.  Pg
16553 6e 6f 20 69 44 62 50 61 67 65 20 3d 20 70 44 62  no iDbPage = pDb
16554 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61  Page->pgno;.  Pa
16555 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42  ger *pPager = pB
16556 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74  t->pPager;.  int
16557 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
16558 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56  eType==PTRMAP_OV
16559 45 52 46 4c 4f 57 32 20 7c 7c 20 65 54 79 70 65  ERFLOW2 || eType
1655a 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  ==PTRMAP_OVERFLO
1655b 57 31 20 7c 7c 20 0a 20 20 20 20 20 20 65 54 79  W1 || .      eTy
1655c 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45  pe==PTRMAP_BTREE
1655d 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41   || eType==PTRMA
1655e 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20  P_ROOTPAGE );.  
1655f 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
16560 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
16561 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
16562 72 74 28 20 70 44 62 50 61 67 65 2d 3e 70 42 74  rt( pDbPage->pBt
16563 3d 3d 70 42 74 20 29 3b 0a 0a 20 20 2f 2a 20 4d  ==pBt );..  /* M
16564 6f 76 65 20 70 61 67 65 20 69 44 62 50 61 67 65  ove page iDbPage
16565 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72 65 6e   from its curren
16566 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 70 61  t location to pa
16567 67 65 20 6e 75 6d 62 65 72 20 69 46 72 65 65 50  ge number iFreeP
16568 61 67 65 20 2a 2f 0a 20 20 54 52 41 43 45 28 28  age */.  TRACE((
16569 22 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d 6f 76  "AUTOVACUUM: Mov
1656a 69 6e 67 20 25 64 20 74 6f 20 66 72 65 65 20 70  ing %d to free p
1656b 61 67 65 20 25 64 20 28 70 74 72 20 70 61 67 65  age %d (ptr page
1656c 20 25 64 20 74 79 70 65 20 25 64 29 5c 6e 22 2c   %d type %d)\n",
1656d 20 0a 20 20 20 20 20 20 69 44 62 50 61 67 65 2c   .      iDbPage,
1656e 20 69 46 72 65 65 50 61 67 65 2c 20 69 50 74 72   iFreePage, iPtr
1656f 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b 0a 20  Page, eType));. 
16570 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
16571 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61 67 65  erMovepage(pPage
16572 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44 62 50  r, pDbPage->pDbP
16573 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20  age, iFreePage, 
16574 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 66 28  isCommit);.  if(
16575 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
16576 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
16577 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65 2d 3e  .  }.  pDbPage->
16578 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61 67 65  pgno = iFreePage
16579 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62 50 61  ;..  /* If pDbPa
1657a 67 65 20 77 61 73 20 61 20 62 74 72 65 65 2d 70  ge was a btree-p
1657b 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d 61 79  age, then it may
1657c 20 68 61 76 65 20 63 68 69 6c 64 20 70 61 67 65   have child page
1657d 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a 20  s and/or cells. 
1657e 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 20 74   ** that point t
1657f 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
16580 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  . The pointer ma
16581 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c  p entries for al
16582 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70 61 67  l these.  ** pag
16583 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 63 68  es need to be ch
16584 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  anged..  **.  **
16585 20 49 66 20 70 44 62 50 61 67 65 20 69 73 20 61   If pDbPage is a
16586 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c  n overflow page,
16587 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   then the first 
16588 34 20 62 79 74 65 73 20 6d 61 79 20 73 74 6f 72  4 bytes may stor
16589 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72  e a.  ** pointer
1658a 20 74 6f 20 61 20 73 75 62 73 65 71 75 65 6e 74   to a subsequent
1658b 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20   overflow page. 
1658c 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
1658d 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 74  ase, then.  ** t
1658e 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 6e  he pointer map n
1658f 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 74  eeds to be updat
16590 65 64 20 66 6f 72 20 74 68 65 20 73 75 62 73 65  ed for the subse
16591 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70  quent overflow p
16592 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  age..  */.  if( 
16593 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54  eType==PTRMAP_BT
16594 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54  REE || eType==PT
16595 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b  RMAP_ROOTPAGE ){
16596 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 68 69  .    rc = setChi
16597 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 61 67  ldPtrmaps(pDbPag
16598 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
16599 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1659a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1659b 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
1659c 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20 3d   Pgno nextOvfl =
1659d 20 67 65 74 34 62 79 74 65 28 70 44 62 50 61 67   get4byte(pDbPag
1659e 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 69  e->aData);.    i
1659f 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20 29  f( nextOvfl!=0 )
165a0 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75  {.      ptrmapPu
165a1 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 6c 2c  t(pBt, nextOvfl,
165a2 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
165a3 32 2c 20 69 46 72 65 65 50 61 67 65 2c 20 26 72  2, iFreePage, &r
165a4 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  c);.      if( rc
165a5 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
165a6 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
165a7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
165a8 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74 68    }..  /* Fix th
165a9 65 20 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74  e database point
165aa 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74 72 50  er on page iPtrP
165ab 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74 65 64  age that pointed
165ac 20 61 74 20 69 44 62 50 61 67 65 20 73 6f 0a 20   at iDbPage so. 
165ad 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e   ** that it poin
165ae 74 73 20 61 74 20 69 46 72 65 65 50 61 67 65 2e  ts at iFreePage.
165af 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20 70 6f   Also fix the po
165b0 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20  inter map entry 
165b1 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50 61 67  for.  ** iPtrPag
165b2 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54  e..  */.  if( eT
165b3 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54  ype!=PTRMAP_ROOT
165b4 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d  PAGE ){.    rc =
165b5 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42   btreeGetPage(pB
165b6 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26 70 50  t, iPtrPage, &pP
165b7 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20  trPage, 0);.    
165b8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
165b9 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
165ba 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
165bb 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
165bc 72 57 72 69 74 65 28 70 50 74 72 50 61 67 65 2d  rWrite(pPtrPage-
165bd 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  >pDbPage);.    i
165be 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
165bf 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73   ){.      releas
165c0 65 50 61 67 65 28 70 50 74 72 50 61 67 65 29 3b  ePage(pPtrPage);
165c1 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
165c2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
165c3 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74   modifyPagePoint
165c4 65 72 28 70 50 74 72 50 61 67 65 2c 20 69 44 62  er(pPtrPage, iDb
165c5 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c  Page, iFreePage,
165c6 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72 65 6c   eType);.    rel
165c7 65 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67  easePage(pPtrPag
165c8 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  e);.    if( rc==
165c9 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
165ca 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
165cb 2c 20 69 46 72 65 65 50 61 67 65 2c 20 65 54 79  , iFreePage, eTy
165cc 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 26 72  pe, iPtrPage, &r
165cd 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  c);.    }.  }.  
165ce 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
165cf 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
165d0 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20 62 79  tion required by
165d1 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28   incrVacuumStep(
165d2 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ). */.static int
165d3 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
165d4 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c 20 4d  ge(BtShared *, M
165d5 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e 6f 20  emPage **, Pgno 
165d6 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a 0a 2f  *, Pgno, u8);../
165d7 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 73  *.** Perform a s
165d8 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 61 6e  ingle step of an
165d9 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63   incremental-vac
165da 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73 73 66  uum. If successf
165db 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51  ul,.** return SQ
165dc 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 72  LITE_OK. If ther
165dd 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74 6f 20  e is no work to 
165de 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66 6f 72  do (and therefor
165df 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e  e no.** point in
165e0 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75   calling this fu
165e1 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c 20 72  nction again), r
165e2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
165e3 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20 73 70  E..**.** More sp
165e4 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73 20 66  ecificly, this f
165e5 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
165e6 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a 65 20   to re-organize 
165e7 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  the .** database
165e8 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c 61 73   so that the las
165e9 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69  t page of the fi
165ea 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  le currently in 
165eb 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f 6e  use.** is no lon
165ec 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a  ger in use..**.*
165ed 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20 70 61  * If the nFin pa
165ee 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a  rameter is non-z
165ef 65 72 6f 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ero, this functi
165f0 6f 6e 20 61 73 73 75 6d 65 73 0a 2a 2a 20 74 68  on assumes.** th
165f1 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69  at the caller wi
165f2 6c 6c 20 6b 65 65 70 20 63 61 6c 6c 69 6e 67 20  ll keep calling 
165f3 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 29  incrVacuumStep()
165f4 20 75 6e 74 69 6c 0a 2a 2a 20 69 74 20 72 65 74   until.** it ret
165f5 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45  urns SQLITE_DONE
165f6 20 6f 72 20 61 6e 20 65 72 72 6f 72 2c 20 61 6e   or an error, an
165f7 64 20 74 68 61 74 20 6e 46 69 6e 20 69 73 20 74  d that nFin is t
165f8 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
165f9 70 61 67 65 73 20 74 68 65 20 64 61 74 61 62 61  pages the databa
165fa 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 63 6f 6e  se file will con
165fb 74 61 69 6e 20 61 66 74 65 72 20 74 68 69 73 20  tain after this 
165fc 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69 73 20 63  .** process is c
165fd 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 6e 46 69  omplete.  If nFi
165fe 6e 20 69 73 20 7a 65 72 6f 2c 20 69 74 20 69 73  n is zero, it is
165ff 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a   assumed that.**
16600 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28   incrVacuumStep(
16601 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64  ) will be called
16602 20 61 20 66 69 6e 69 74 65 20 61 6d 6f 75 6e 74   a finite amount
16603 20 6f 66 20 74 69 6d 65 73 0a 2a 2a 20 77 68 69   of times.** whi
16604 63 68 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f  ch may or may no
16605 74 20 65 6d 70 74 79 20 74 68 65 20 66 72 65 65  t empty the free
16606 6c 69 73 74 2e 20 20 41 20 66 75 6c 6c 20 61 75  list.  A full au
16607 74 6f 76 61 63 75 75 6d 0a 2a 2a 20 68 61 73 20  tovacuum.** has 
16608 6e 46 69 6e 3e 30 2e 20 20 41 20 22 50 52 41 47  nFin>0.  A "PRAG
16609 4d 41 20 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76  MA incremental_v
1660a 61 63 75 75 6d 22 20 68 61 73 20 6e 46 69 6e 3d  acuum" has nFin=
1660b 3d 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  =0..*/.static in
1660c 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70  t incrVacuumStep
1660d 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20  (BtShared *pBt, 
1660e 50 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e 6f 20  Pgno nFin, Pgno 
1660f 69 4c 61 73 74 50 67 29 7b 0a 20 20 50 67 6e 6f  iLastPg){.  Pgno
16610 20 6e 46 72 65 65 4c 69 73 74 3b 20 20 20 20 20   nFreeList;     
16611 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
16612 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 6f  of pages still o
16613 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  n the free-list 
16614 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  */..  assert( sq
16615 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
16616 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
16617 20 20 61 73 73 65 72 74 28 20 69 4c 61 73 74 50    assert( iLastP
16618 67 3e 6e 46 69 6e 20 29 3b 0a 0a 20 20 69 66 28  g>nFin );..  if(
16619 20 21 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28   !PTRMAP_ISPAGE(
1661a 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 26 26  pBt, iLastPg) &&
1661b 20 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e   iLastPg!=PENDIN
1661c 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
1661d 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a   ){.    int rc;.
1661e 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20      u8 eType;.  
1661f 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b    Pgno iPtrPage;
16620 0a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 74 20  ..    nFreeList 
16621 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d  = get4byte(&pBt-
16622 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
16623 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 46 72  6]);.    if( nFr
16624 65 65 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20  eeList==0 ){.   
16625 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
16626 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20  _DONE;.    }..  
16627 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74    rc = ptrmapGet
16628 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26  (pBt, iLastPg, &
16629 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 67 65  eType, &iPtrPage
1662a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
1662b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1662c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1662d 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79 70 65   }.    if( eType
1662e 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  ==PTRMAP_ROOTPAG
1662f 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  E ){.      retur
16630 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
16631 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20  _BKPT;.    }..  
16632 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52    if( eType==PTR
16633 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a  MAP_FREEPAGE ){.
16634 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e 3d 3d        if( nFin==
16635 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
16636 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20  Remove the page 
16637 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 20 66  from the files f
16638 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 20 69  ree-list. This i
16639 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 0a 20  s not required. 
1663a 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e 46 69         ** if nFi
1663b 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49  n is non-zero. I
1663c 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 68 65  n that case, the
1663d 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c 6c 20   free-list will 
1663e 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72  be.        ** tr
1663f 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20  uncated to zero 
16640 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74  after this funct
16641 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 6f 20  ion returns, so 
16642 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 20 20  it doesn't .    
16643 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 69 66      ** matter if
16644 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69   it still contai
16645 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67 65 20  ns some garbage 
16646 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20  entries..       
16647 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 6e 6f   */.        Pgno
16648 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20   iFreePg;.      
16649 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65    MemPage *pFree
1664a 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  Pg;.        rc =
1664b 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
1664c 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67  ge(pBt, &pFreePg
1664d 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c 61 73  , &iFreePg, iLas
1664e 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  tPg, 1);.       
1664f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16650 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
16651 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
16652 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73     }.        ass
16653 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d 69 4c  ert( iFreePg==iL
16654 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 20  astPg );.       
16655 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72   releasePage(pFr
16656 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20  eePg);.      }. 
16657 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
16658 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 20    Pgno iFreePg; 
16659 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1665a 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70 61 67  ndex of free pag
1665b 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 74 50  e to move pLastP
1665c 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 4d 65  g to */.      Me
1665d 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67 3b 0a  mPage *pLastPg;.
1665e 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65  .      rc = btre
1665f 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c  eGetPage(pBt, iL
16660 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50 67 2c  astPg, &pLastPg,
16661 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
16662 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
16663 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
16664 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  c;.      }..    
16665 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69 73 20    /* If nFin is 
16666 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f 70 20  zero, this loop 
16667 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f 6e 63  runs exactly onc
16668 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61 73 74  e and page pLast
16669 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73  Pg.      ** is s
1666a 77 61 70 70 65 64 20 77 69 74 68 20 74 68 65 20  wapped with the 
1666b 66 69 72 73 74 20 66 72 65 65 20 70 61 67 65 20  first free page 
1666c 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65 20 66  pulled off the f
1666d 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20 20 20  ree list..      
1666e 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e 20 74  **.      ** On t
1666f 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69  he other hand, i
16670 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61 74 65  f nFin is greate
16671 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65  r than zero, the
16672 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a 2a 20  n keep.      ** 
16673 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 20  looping until a 
16674 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61 74 65  free-page locate
16675 64 20 77 69 74 68 69 6e 20 74 68 65 20 66 69 72  d within the fir
16676 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a 20 20  st nFin pages.  
16677 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69      ** of the fi
16678 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 20  le is found..   
16679 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b     */.      do {
1667a 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65  .        MemPage
1667b 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20   *pFreePg;.     
1667c 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65     rc = allocate
1667d 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26  BtreePage(pBt, &
1667e 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50  pFreePg, &iFreeP
1667f 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  g, 0, 0);.      
16680 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16681 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
16682 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61   releasePage(pLa
16683 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20  stPg);.         
16684 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
16685 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65      }.        re
16686 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 65 50  leasePage(pFreeP
16687 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65  g);.      }while
16688 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69 46 72  ( nFin!=0 && iFr
16689 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20 20 20  eePg>nFin );.   
1668a 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65     assert( iFree
1668b 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a 20 20  Pg<iLastPg );.  
1668c 20 20 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20      .      rc = 
1668d 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1668e 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62 50 61  e(pLastPg->pDbPa
1668f 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
16690 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
16691 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c          rc = rel
16692 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 70  ocatePage(pBt, p
16693 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c 20 69  LastPg, eType, i
16694 50 74 72 50 61 67 65 2c 20 69 46 72 65 65 50 67  PtrPage, iFreePg
16695 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a 20 20 20 20  , nFin!=0);.    
16696 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73    }.      releas
16697 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a  ePage(pLastPg);.
16698 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
16699 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1669a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1669b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1669c 0a 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30 20 29  .  if( nFin==0 )
1669d 7b 0a 20 20 20 20 69 4c 61 73 74 50 67 2d 2d 3b  {.    iLastPg--;
1669e 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c 61 73  .    while( iLas
1669f 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  tPg==PENDING_BYT
166a0 45 5f 50 41 47 45 28 70 42 74 29 7c 7c 50 54 52  E_PAGE(pBt)||PTR
166a1 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
166a2 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20  iLastPg) ){.    
166a3 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 53 50    if( PTRMAP_ISP
166a4 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67  AGE(pBt, iLastPg
166a5 29 20 29 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d  ) ){.        Mem
166a6 50 61 67 65 20 2a 70 50 67 3b 0a 20 20 20 20 20  Page *pPg;.     
166a7 20 20 20 69 6e 74 20 72 63 20 3d 20 62 74 72 65     int rc = btre
166a8 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c  eGetPage(pBt, iL
166a9 61 73 74 50 67 2c 20 26 70 50 67 2c 20 30 29 3b  astPg, &pPg, 0);
166aa 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
166ab 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
166ac 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
166ad 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
166ae 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
166af 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 2d  3PagerWrite(pPg-
166b0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
166b1 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
166b2 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  Pg);.        if(
166b3 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
166b4 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
166b5 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn rc;.        }
166b6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
166b7 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 7d 0a  LastPg--;.    }.
166b8 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
166b9 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 70 42  TruncateImage(pB
166ba 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c 61 73 74  t->pPager, iLast
166bb 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Pg);.  }.  retur
166bc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
166bd 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72  /*.** A write-tr
166be 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
166bf 65 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20  e opened before 
166c0 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e  calling this fun
166c1 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20 70 65 72  ction..** It per
166c2 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c 65 20 75  forms a single u
166c3 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74 6f 77 61  nit of work towa
166c4 72 64 73 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74  rds an increment
166c5 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a  al vacuum..**.**
166c6 20 49 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e   If the incremen
166c7 74 61 6c 20 76 61 63 75 75 6d 20 69 73 20 66 69  tal vacuum is fi
166c8 6e 69 73 68 65 64 20 61 66 74 65 72 20 74 68 69  nished after thi
166c9 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 72  s function has r
166ca 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 4f  un,.** SQLITE_DO
166cb 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  NE is returned. 
166cc 49 66 20 69 74 20 69 73 20 6e 6f 74 20 66 69 6e  If it is not fin
166cd 69 73 68 65 64 2c 20 62 75 74 20 6e 6f 20 65 72  ished, but no er
166ce 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 0a 2a 2a  ror occurred,.**
166cf 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
166d0 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
166d1 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  e an SQLite erro
166d2 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 51 4c 49  r code. .*/.SQLI
166d3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
166d4 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 56  qlite3BtreeIncrV
166d5 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 29 7b  acuum(Btree *p){
166d6 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53  .  int rc;.  BtS
166d7 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
166d8 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  pBt;..  sqlite3B
166d9 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
166da 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54  assert( pBt->inT
166db 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e  ransaction==TRAN
166dc 53 5f 57 52 49 54 45 20 26 26 20 70 2d 3e 69 6e  S_WRITE && p->in
166dd 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
166de 54 45 20 29 3b 0a 20 20 69 66 28 20 21 70 42 74  TE );.  if( !pBt
166df 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
166e0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
166e1 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  DONE;.  }else{. 
166e2 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c     invalidateAll
166e3 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42  OverflowCache(pB
166e4 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 69 6e 63  t);.    rc = inc
166e5 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 74 2c  rVacuumStep(pBt,
166e6 20 30 2c 20 70 61 67 65 72 50 61 67 65 63 6f 75   0, pagerPagecou
166e7 6e 74 28 70 42 74 29 29 3b 0a 20 20 7d 0a 20 20  nt(pBt));.  }.  
166e8 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
166e9 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
166ea 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
166eb 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
166ec 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 6c 69  ed prior to sqli
166ed 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 20 77  te3PagerCommit w
166ee 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  hen a transactio
166ef 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 65 64  n.** is commited
166f0 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 61 63   for an auto-vac
166f1 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  uum database..**
166f2 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b  .** If SQLITE_OK
166f3 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68   is returned, th
166f4 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 20 73  en *pnTrunc is s
166f5 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  et to the number
166f6 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 68 65   of pages.** the
166f7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73   database file s
166f8 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61 74  hould be truncat
166f9 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 68 65  ed to during the
166fa 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e   commit process.
166fb 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 64 61   .** i.e. the da
166fc 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20  tabase has been 
166fd 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f 20 74  reorganized so t
166fe 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 69 72  hat only the fir
166ff 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20 70  st *pnTrunc.** p
16700 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 65 2e  ages are in use.
16701 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
16702 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28  utoVacuumCommit(
16703 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
16704 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
16705 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70  E_OK;.  Pager *p
16706 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61  Pager = pBt->pPa
16707 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28  ger;.  VVA_ONLY(
16708 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 6c 69   int nRef = sqli
16709 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
1670a 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 61  (pPager) );..  a
1670b 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1670c 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
1670d 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76 61 6c  utex) );.  inval
1670e 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77  idateAllOverflow
1670f 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 61 73  Cache(pBt);.  as
16710 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f 56 61  sert(pBt->autoVa
16711 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21 70 42  cuum);.  if( !pB
16712 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 29 7b  t->incrVacuum ){
16713 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e 3b 20  .    Pgno nFin; 
16714 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
16715 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64 61  r of pages in da
16716 74 61 62 61 73 65 20 61 66 74 65 72 20 61 75 74  tabase after aut
16717 6f 76 61 63 75 75 6d 69 6e 67 20 2a 2f 0a 20 20  ovacuuming */.  
16718 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b 20 20 20    Pgno nFree;   
16719 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1671a 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66  f pages on the f
1671b 72 65 65 6c 69 73 74 20 69 6e 69 74 69 61 6c 6c  reelist initiall
1671c 79 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 50  y */.    Pgno nP
1671d 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 4e  trmap;      /* N
1671e 75 6d 62 65 72 20 6f 66 20 50 74 72 4d 61 70 20  umber of PtrMap 
1671f 70 61 67 65 73 20 74 6f 20 62 65 20 66 72 65 65  pages to be free
16720 64 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 46  d */.    Pgno iF
16721 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ree;        /* T
16722 68 65 20 6e 65 78 74 20 70 61 67 65 20 74 6f 20  he next page to 
16723 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20  be freed */.    
16724 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20 20 20  int nEntry;     
16725 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
16726 65 6e 74 72 69 65 73 20 6f 6e 20 6f 6e 65 20 70  entries on one p
16727 74 72 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20  trmap page */.  
16728 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20    Pgno nOrig;   
16729 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1672a 20 73 69 7a 65 20 62 65 66 6f 72 65 20 66 72 65   size before fre
1672b 65 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 6e 4f 72  eing */..    nOr
1672c 69 67 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f  ig = pagerPageco
1672d 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 69 66  unt(pBt);.    if
1672e 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28  ( PTRMAP_ISPAGE(
1672f 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20 6e  pBt, nOrig) || n
16730 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59  Orig==PENDING_BY
16731 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a  TE_PAGE(pBt) ){.
16732 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e        /* It is n
16733 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63  ot possible to c
16734 72 65 61 74 65 20 61 20 64 61 74 61 62 61 73 65  reate a database
16735 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 66   for which the f
16736 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20 20 20  inal page.      
16737 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 70  ** is either a p
16738 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20  ointer-map page 
16739 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 62  or the pending-b
1673a 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f 6e 65  yte page. If one
1673b 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 6e 63  .      ** is enc
1673c 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73 20 69  ountered, this i
1673d 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75 70 74  ndicates corrupt
1673e 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ion..      */.  
1673f 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16740 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
16741 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72 65 65      }..    nFree
16742 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74   = get4byte(&pBt
16743 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
16744 33 36 5d 29 3b 0a 20 20 20 20 6e 45 6e 74 72 79  36]);.    nEntry
16745 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
16746 7a 65 2f 35 3b 0a 20 20 20 20 6e 50 74 72 6d 61  ze/5;.    nPtrma
16747 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67  p = (nFree-nOrig
16748 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70  +PTRMAP_PAGENO(p
16749 42 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e 74 72  Bt, nOrig)+nEntr
1674a 79 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 20 20 6e  y)/nEntry;.    n
1674b 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 6e 46  Fin = nOrig - nF
1674c 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b 0a 20  ree - nPtrmap;. 
1674d 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50 45 4e     if( nOrig>PEN
1674e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
1674f 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45 4e 44  Bt) && nFin<PEND
16750 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
16751 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e  t) ){.      nFin
16752 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68  --;.    }.    wh
16753 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41  ile( PTRMAP_ISPA
16754 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c  GE(pBt, nFin) ||
16755 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42   nFin==PENDING_B
16756 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
16757 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20  .      nFin--;. 
16758 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 46 69     }.    if( nFi
16759 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74 75 72 6e  n>nOrig ) return
1675a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1675b 42 4b 50 54 3b 0a 0a 20 20 20 20 66 6f 72 28 69  BKPT;..    for(i
1675c 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 72 65  Free=nOrig; iFre
1675d 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d 53 51  e>nFin && rc==SQ
1675e 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 2d 2d  LITE_OK; iFree--
1675f 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e  ){.      rc = in
16760 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 74  crVacuumStep(pBt
16761 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 29 3b 0a  , nFin, iFree);.
16762 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 72      }.    if( (r
16763 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 7c  c==SQLITE_DONE |
16764 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29  | rc==SQLITE_OK)
16765 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b 0a 20   && nFree>0 ){. 
16766 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
16767 5f 4f 4b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  _OK;.      rc = 
16768 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
16769 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70  e(pBt->pPage1->p
1676a 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70  DbPage);.      p
1676b 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50  ut4byte(&pBt->pP
1676c 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c  age1->aData[32],
1676d 20 30 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62   0);.      put4b
1676e 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31  yte(&pBt->pPage1
1676f 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 29 3b  ->aData[36], 0);
16770 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61  .      sqlite3Pa
16771 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65  gerTruncateImage
16772 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 46  (pBt->pPager, nF
16773 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  in);.    }.    i
16774 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
16775 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
16776 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70  3PagerRollback(p
16777 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  Pager);.    }.  
16778 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65  }..  assert( nRe
16779 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 52  f==sqlite3PagerR
1677a 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20  efcount(pPager) 
1677b 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1677c 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 6e 64  }..#else /* ifnd
1677d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1677e 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23 20 64  UTOVACUUM */.# d
1677f 65 66 69 6e 65 20 73 65 74 43 68 69 6c 64 50 74  efine setChildPt
16780 72 6d 61 70 73 28 78 29 20 53 51 4c 49 54 45 5f  rmaps(x) SQLITE_
16781 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  OK.#endif../*.**
16782 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f   This routine do
16783 65 73 20 74 68 65 20 66 69 72 73 74 20 70 68 61  es the first pha
16784 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 61 73  se of a two-phas
16785 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 73 20  e commit.  This 
16786 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 73 65  routine.** cause
16787 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  s a rollback jou
16788 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 61 74  rnal to be creat
16789 65 64 20 28 69 66 20 69 74 20 64 6f 65 73 20 6e  ed (if it does n
1678a 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
1678b 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c 61 74  ).** and populat
1678c 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68 20 69  ed with enough i
1678d 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 74 68  nformation so th
1678e 61 74 20 69 66 20 61 20 70 6f 77 65 72 20 6c 6f  at if a power lo
1678f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68 65  ss occurs.** the
16790 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 65   database can be
16791 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73   restored to its
16792 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20   original state 
16793 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 0a  by playing back.
16794 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  ** the journal. 
16795 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e   Then the conten
16796 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ts of the journa
16797 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20 6f 75  l are flushed ou
16798 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 73 6b  t to.** the disk
16799 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a 6f 75  .  After the jou
1679a 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 20 6f  rnal is safely o
1679b 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63 68 61  n oxide, the cha
1679c 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64  nges to the.** d
1679d 61 74 61 62 61 73 65 20 61 72 65 20 77 72 69 74  atabase are writ
1679e 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ten into the dat
1679f 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 66  abase file and f
167a0 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64 65 2e  lushed to oxide.
167a1 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20 6f  .** At the end o
167a2 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 68 65  f this call, the
167a3 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
167a4 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 6f  l still exists o
167a5 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e  n the.** disk an
167a6 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c 20 68  d we are still h
167a7 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 6b 73  olding all locks
167a8 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73 61 63  , so the transac
167a9 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a 2a 20  tion has not.** 
167aa 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 65 20  committed.  See 
167ab 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
167ac 69 74 50 68 61 73 65 54 77 6f 28 29 20 66 6f 72  itPhaseTwo() for
167ad 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73   the second phas
167ae 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 6d  e of the.** comm
167af 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a  it process..**.*
167b0 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61  * This call is a
167b1 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77 72 69   no-op if no wri
167b2 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te-transaction i
167b3 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69  s currently acti
167b4 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a 2a 2a  ve on pBt..**.**
167b5 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79 6e 63   Otherwise, sync
167b6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
167b7 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72 65 65  le for the btree
167b8 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20 70 6f   pBt. zMaster po
167b9 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20 6e  ints to.** the n
167ba 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20  ame of a master 
167bb 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61  journal file tha
167bc 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74  t should be writ
167bd 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20  ten into the.** 
167be 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e  individual journ
167bf 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 20 4e  al file, or is N
167c0 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e 67 20  ULL, indicating 
167c1 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  no master journa
167c2 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69 6e 67  l file .** (sing
167c3 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e  le database tran
167c4 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20  saction)..**.** 
167c5 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c  When this is cal
167c6 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65 72 20  led, the master 
167c7 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 20 61  journal should a
167c8 6c 72 65 61 64 79 20 68 61 76 65 20 62 65 65 6e  lready have been
167c9 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70 6f 70  .** created, pop
167ca 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ulated with this
167cb 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72   journal pointer
167cc 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f 20 64   and synced to d
167cd 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20  isk..**.** Once 
167ce 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e 65 20  this is routine 
167cf 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68  has returned, th
167d0 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72 65 71  e only thing req
167d1 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69 74 0a  uired to commit.
167d2 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74 72 61  ** the write-tra
167d3 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74 68 69  nsaction for thi
167d4 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  s database file 
167d5 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65  is to delete the
167d6 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 53 51 4c   journal..*/.SQL
167d7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
167d8 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
167d9 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 65 65  itPhaseOne(Btree
167da 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *p, const char 
167db 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74  *zMaster){.  int
167dc 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
167dd 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e  .  if( p->inTran
167de 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
167df 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a  {.    BtShared *
167e0 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
167e1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
167e2 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 65 66 20  ter(p);.#ifndef 
167e3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
167e4 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70  VACUUM.    if( p
167e5 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
167e6 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 75 74  {.      rc = aut
167e7 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 70 42  oVacuumCommit(pB
167e8 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  t);.      if( rc
167e9 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
167ea 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
167eb 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20  reeLeave(p);.   
167ec 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
167ed 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
167ee 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71  ndif.    rc = sq
167ef 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
167f0 50 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e 70 50  PhaseOne(pBt->pP
167f1 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c 20 30  ager, zMaster, 0
167f2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
167f3 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d  reeLeave(p);.  }
167f4 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
167f5 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
167f6 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 66  tion is called f
167f7 72 6f 6d 20 62 6f 74 68 20 42 74 72 65 65 43 6f  rom both BtreeCo
167f8 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61  mmitPhaseTwo() a
167f9 6e 64 20 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b  nd BtreeRollback
167fa 28 29 0a 2a 2a 20 61 74 20 74 68 65 20 63 6f 6e  ().** at the con
167fb 63 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74 72 61  clusion of a tra
167fc 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  nsaction..*/.sta
167fd 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 45 6e  tic void btreeEn
167fe 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 42 74 72  dTransaction(Btr
167ff 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72  ee *p){.  BtShar
16800 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
16801 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
16802 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
16803 65 78 28 70 29 20 29 3b 0a 0a 20 20 62 74 72 65  ex(p) );..  btre
16804 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74  eClearHasContent
16805 28 70 42 74 29 3b 0a 20 20 69 66 28 20 70 2d 3e  (pBt);.  if( p->
16806 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f  inTrans>TRANS_NO
16807 4e 45 20 26 26 20 70 2d 3e 64 62 2d 3e 61 63 74  NE && p->db->act
16808 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a  iveVdbeCnt>1 ){.
16809 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
1680a 61 72 65 20 6f 74 68 65 72 20 61 63 74 69 76 65  are other active
1680b 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74   statements that
1680c 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 69 73 20   belong to this 
1680d 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20  database.    ** 
1680e 68 61 6e 64 6c 65 2c 20 64 6f 77 6e 67 72 61 64  handle, downgrad
1680f 65 20 74 6f 20 61 20 72 65 61 64 2d 6f 6e 6c 79  e to a read-only
16810 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68   transaction. Th
16811 65 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e  e other statemen
16812 74 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 73 74  ts.    ** may st
16813 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67 20 66  ill be reading f
16814 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
16815 2e 20 20 2a 2f 0a 20 20 20 20 64 6f 77 6e 67 72  .  */.    downgr
16816 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68  adeAllSharedCach
16817 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a  eTableLocks(p);.
16818 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d      p->inTrans =
16819 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d   TRANS_READ;.  }
1681a 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20  else{.    /* If 
1681b 74 68 65 20 68 61 6e 64 6c 65 20 68 61 64 20 61  the handle had a
1681c 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72 61 6e 73  ny kind of trans
1681d 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 64 65 63  action open, dec
1681e 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 20 20  rement the .    
1681f 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63  ** transaction c
16820 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68 61 72  ount of the shar
16821 65 64 20 62 74 72 65 65 2e 20 49 66 20 74 68 65  ed btree. If the
16822 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75   transaction cou
16823 6e 74 20 0a 20 20 20 20 2a 2a 20 72 65 61 63 68  nt .    ** reach
16824 65 73 20 30 2c 20 73 65 74 20 74 68 65 20 73 68  es 0, set the sh
16825 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 52  ared state to TR
16826 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e  ANS_NONE. The un
16827 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65  lockBtreeIfUnuse
16828 64 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20  d().    ** call 
16829 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e 6c 6f 63  below will unloc
1682a 6b 20 74 68 65 20 70 61 67 65 72 2e 20 20 2a 2f  k the pager.  */
1682b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  .    if( p->inTr
1682c 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20  ans!=TRANS_NONE 
1682d 29 7b 0a 20 20 20 20 20 20 63 6c 65 61 72 41 6c  ){.      clearAl
1682e 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  lSharedCacheTabl
1682f 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 20 20 20  eLocks(p);.     
16830 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69   pBt->nTransacti
16831 6f 6e 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20  on--;.      if( 
16832 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63  0==pBt->nTransac
16833 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  tion ){.        
16834 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
16835 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b  on = TRANS_NONE;
16836 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
16837 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 63      /* Set the c
16838 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
16839 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e  on state to TRAN
1683a 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63  S_NONE and unloc
1683b 6b 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 70 61  k the .    ** pa
1683c 67 65 72 20 69 66 20 74 68 69 73 20 63 61 6c 6c  ger if this call
1683d 20 63 6c 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79   closed the only
1683e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74   read or write t
1683f 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 2a 2f 0a  ransaction.  */.
16840 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d      p->inTrans =
16841 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20   TRANS_NONE;.   
16842 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e   unlockBtreeIfUn
16843 75 73 65 64 28 70 42 74 29 3b 0a 20 20 7d 0a 0a  used(pBt);.  }..
16844 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79    btreeIntegrity
16845 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  (p);.}../*.** Co
16846 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63  mmit the transac
16847 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69  tion currently i
16848 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a  n progress..**.*
16849 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1684a 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65  mplements the se
1684b 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20  cond phase of a 
1684c 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20  2-phase commit. 
1684d 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42   The.** sqlite3B
1684e 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
1684f 6e 65 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  ne() routine doe
16850 73 20 74 68 65 20 66 69 72 73 74 20 70 68 61 73  s the first phas
16851 65 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20  e and should.** 
16852 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72  be invoked prior
16853 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73   to calling this
16854 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73   routine.  The s
16855 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
16856 74 50 68 61 73 65 4f 6e 65 28 29 0a 2a 2a 20 72  tPhaseOne().** r
16857 6f 75 74 69 6e 65 20 64 69 64 20 61 6c 6c 20 74  outine did all t
16858 68 65 20 77 6f 72 6b 20 6f 66 20 77 72 69 74 69  he work of writi
16859 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  ng information o
1685a 75 74 20 74 6f 20 64 69 73 6b 20 61 6e 64 20 66  ut to disk and f
1685b 6c 75 73 68 69 6e 67 20 74 68 65 0a 2a 2a 20 63  lushing the.** c
1685c 6f 6e 74 65 6e 74 73 20 73 6f 20 74 68 61 74 20  ontents so that 
1685d 74 68 65 79 20 61 72 65 20 77 72 69 74 74 65 6e  they are written
1685e 20 6f 6e 74 6f 20 74 68 65 20 64 69 73 6b 20 70   onto the disk p
1685f 6c 61 74 74 65 72 2e 20 20 41 6c 6c 20 74 68 69  latter.  All thi
16860 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 68 61 73  s.** routine has
16861 20 74 6f 20 64 6f 20 69 73 20 64 65 6c 65 74 65   to do is delete
16862 20 6f 72 20 74 72 75 6e 63 61 74 65 20 6f 72 20   or truncate or 
16863 7a 65 72 6f 20 74 68 65 20 68 65 61 64 65 72 20  zero the header 
16864 69 6e 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 6f  in the.** the ro
16865 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 28  llback journal (
16866 77 68 69 63 68 20 63 61 75 73 65 73 20 74 68 65  which causes the
16867 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20   transaction to 
16868 63 6f 6d 6d 69 74 29 20 61 6e 64 0a 2a 2a 20 64  commit) and.** d
16869 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a  rop locks..**.**
1686a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61   This will relea
1686b 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63  se the write loc
1686c 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
1686d 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72  e file.  If ther
1686e 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69  e.** are no acti
1686f 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61  ve cursors, it a
16870 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65  lso releases the
16871 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53   read lock..*/.S
16872 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16873 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
16874 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72  mmitPhaseTwo(Btr
16875 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72  ee *p){.  BtShar
16876 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
16877 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
16878 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 62 74 72  eEnter(p);.  btr
16879 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a  eeIntegrity(p);.
1687a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e  .  /* If the han
1687b 64 6c 65 20 68 61 73 20 61 20 77 72 69 74 65 2d  dle has a write-
1687c 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
1687d 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61  , commit the sha
1687e 72 65 64 2d 62 74 72 65 65 73 20 0a 20 20 2a 2a  red-btrees .  **
1687f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
16880 20 73 65 74 20 74 68 65 20 73 68 61 72 65 64 20   set the shared 
16881 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 52  state to TRANS_R
16882 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  EAD..  */.  if( 
16883 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
16884 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69  S_WRITE ){.    i
16885 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72  nt rc;.    asser
16886 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  t( pBt->inTransa
16887 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
16888 54 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  TE );.    assert
16889 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74  ( pBt->nTransact
1688a 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72 63 20  ion>0 );.    rc 
1688b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  = sqlite3PagerCo
1688c 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42 74  mmitPhaseTwo(pBt
1688d 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  ->pPager);.    i
1688e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1688f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
16890 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
16891 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
16892 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e  .    }.    pBt->
16893 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  inTransaction = 
16894 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a  TRANS_READ;.  }.
16895 0a 20 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73  .  btreeEndTrans
16896 61 63 74 69 6f 6e 28 70 29 3b 0a 20 20 73 71 6c  action(p);.  sql
16897 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
16898 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
16899 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
1689a 44 6f 20 62 6f 74 68 20 70 68 61 73 65 73 20 6f  Do both phases o
1689b 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53  f a commit..*/.S
1689c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1689d 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
1689e 6d 6d 69 74 28 42 74 72 65 65 20 2a 70 29 7b 0a  mmit(Btree *p){.
1689f 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
168a0 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
168a1 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
168a2 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
168a3 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20 69 66 28  One(p, 0);.  if(
168a4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
168a5 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
168a6 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
168a7 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d 0a 20 20  seTwo(p);.  }.  
168a8 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
168a9 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
168aa 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44  c;.}..#ifndef ND
168ab 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  EBUG./*.** Retur
168ac 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
168ad 77 72 69 74 65 2d 63 75 72 73 6f 72 73 20 6f 70  write-cursors op
168ae 65 6e 20 6f 6e 20 74 68 69 73 20 68 61 6e 64 6c  en on this handl
168af 65 2e 20 54 68 69 73 20 69 73 20 66 6f 72 20 75  e. This is for u
168b0 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74 28  se.** in assert(
168b1 29 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 73  ) expressions, s
168b2 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d  o it is only com
168b3 70 69 6c 65 64 20 69 66 20 4e 44 45 42 55 47 20  piled if NDEBUG 
168b4 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65  is not.** define
168b5 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65  d..**.** For the
168b6 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69   purposes of thi
168b7 73 20 72 6f 75 74 69 6e 65 2c 20 61 20 77 72 69  s routine, a wri
168b8 74 65 2d 63 75 72 73 6f 72 20 69 73 20 61 6e 79  te-cursor is any
168b9 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20   cursor that.** 
168ba 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 77 72  is capable of wr
168bb 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74  iting to the dat
168bc 61 62 73 65 2e 20 20 54 68 61 74 20 6d 65 61 6e  abse.  That mean
168bd 73 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73  s the cursor was
168be 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 6f  .** originally o
168bf 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e  pened for writin
168c0 67 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72  g and the cursor
168c1 20 68 61 73 20 6e 6f 74 20 62 65 20 64 69 73 61   has not be disa
168c2 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61 76 69 6e  bled.** by havin
168c3 67 20 69 74 73 20 73 74 61 74 65 20 63 68 61 6e  g its state chan
168c4 67 65 64 20 74 6f 20 43 55 52 53 4f 52 5f 46 41  ged to CURSOR_FA
168c5 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ULT..*/.static i
168c6 6e 74 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72  nt countWriteCur
168c7 73 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a 70  sors(BtShared *p
168c8 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20  Bt){.  BtCursor 
168c9 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d  *pCur;.  int r =
168ca 20 30 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d 70   0;.  for(pCur=p
168cb 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 75  Bt->pCursor; pCu
168cc 72 3b 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e  r; pCur=pCur->pN
168cd 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43  ext){.    if( pC
168ce 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 43  ur->wrFlag && pC
168cf 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53  ur->eState!=CURS
168d0 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20  OR_FAULT ) r++; 
168d1 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b  .  }.  return r;
168d2 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
168d3 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
168d4 74 73 20 74 68 65 20 73 74 61 74 65 20 74 6f 20  ts the state to 
168d5 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 61 6e 64  CURSOR_FAULT and
168d6 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f   the error.** co
168d7 64 65 20 74 6f 20 65 72 72 43 6f 64 65 20 66 6f  de to errCode fo
168d8 72 20 65 76 65 72 79 20 63 75 72 73 6f 72 20 6f  r every cursor o
168d9 6e 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20  n BtShared that 
168da 70 42 74 72 65 65 0a 2a 2a 20 72 65 66 65 72 65  pBtree.** refere
168db 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 72  nces..**.** Ever
168dc 79 20 63 75 72 73 6f 72 20 69 73 20 74 72 69 70  y cursor is trip
168dd 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63  ped, including c
168de 75 72 73 6f 72 73 20 74 68 61 74 20 62 65 6c 6f  ursors that belo
168df 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 72 20 64  ng.** to other d
168e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
168e1 6f 6e 73 20 74 68 61 74 20 68 61 70 70 65 6e 20  ons that happen 
168e2 74 6f 20 62 65 20 73 68 61 72 69 6e 67 0a 2a 2a  to be sharing.**
168e3 20 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20   the cache with 
168e4 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  pBtree..**.** Th
168e5 69 73 20 72 6f 75 74 69 6e 65 20 67 65 74 73 20  is routine gets 
168e6 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 72 6f  called when a ro
168e7 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 2e 0a 2a  llback occurs..*
168e8 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 75 73  * All cursors us
168e9 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63 61 63  ing the same cac
168ea 68 65 20 6d 75 73 74 20 62 65 20 74 72 69 70 70  he must be tripp
168eb 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74  ed.** to prevent
168ec 20 74 68 65 6d 20 66 72 6f 6d 20 74 72 79 69 6e   them from tryin
168ed 67 20 74 6f 20 75 73 65 20 74 68 65 20 62 74 72  g to use the btr
168ee 65 65 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20  ee after.** the 
168ef 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 72  rollback.  The r
168f0 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 65  ollback may have
168f1 20 64 65 6c 65 74 65 64 20 74 61 62 6c 65 73 0a   deleted tables.
168f2 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72 6f 6f 74  ** or moved root
168f3 20 70 61 67 65 73 2c 20 73 6f 20 69 74 20 69 73   pages, so it is
168f4 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20   not sufficient 
168f5 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68 65 20 73  to.** save the s
168f6 74 61 74 65 20 6f 66 20 74 68 65 20 63 75 72 73  tate of the curs
168f7 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20  or.  The cursor 
168f8 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e 76 61 6c  must be.** inval
168f9 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  idated..*/.SQLIT
168fa 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
168fb 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41  qlite3BtreeTripA
168fc 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 65 65 20  llCursors(Btree 
168fd 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 65 72 72  *pBtree, int err
168fe 43 6f 64 65 29 7b 0a 20 20 42 74 43 75 72 73 6f  Code){.  BtCurso
168ff 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 42  r *p;.  sqlite3B
16900 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65  treeEnter(pBtree
16901 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 65  );.  for(p=pBtre
16902 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b  e->pBt->pCursor;
16903 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
16904 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
16905 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61  sqlite3BtreeClea
16906 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 20 20 20  rCursor(p);.    
16907 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  p->eState = CURS
16908 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20 20 70 2d  OR_FAULT;.    p-
16909 3e 73 6b 69 70 4e 65 78 74 20 3d 20 65 72 72 43  >skipNext = errC
1690a 6f 64 65 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  ode;.    for(i=0
1690b 3b 20 69 3c 3d 70 2d 3e 69 50 61 67 65 3b 20 69  ; i<=p->iPage; i
1690c 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61  ++){.      relea
1690d 73 65 50 61 67 65 28 70 2d 3e 61 70 50 61 67 65  sePage(p->apPage
1690e 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  [i]);.      p->a
1690f 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20  pPage[i] = 0;.  
16910 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
16911 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72  3BtreeLeave(pBtr
16912 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f  ee);.}../*.** Ro
16913 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73  llback the trans
16914 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65  action in progre
16915 73 73 2e 20 20 41 6c 6c 20 63 75 72 73 6f 72 73  ss.  All cursors
16916 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76 61   will be.** inva
16917 6c 69 64 65 64 20 62 79 20 74 68 69 73 20 6f 70  lided by this op
16918 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74  eration.  Any at
16919 74 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 63  tempt to use a c
1691a 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74 20 77 61  ursor.** that wa
1691b 73 20 6f 70 65 6e 20 61 74 20 74 68 65 20 62 65  s open at the be
1691c 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20  ginning of this 
1691d 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72  operation will r
1691e 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20 65  esult.** in an e
1691f 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rror..**.** This
16920 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68   will release th
16921 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20  e write lock on 
16922 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
16923 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20  e.  If there.** 
16924 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75  are no active cu
16925 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72  rsors, it also r
16926 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 64  eleases the read
16927 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45   lock..*/.SQLITE
16928 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
16929 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63  ite3BtreeRollbac
1692a 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69  k(Btree *p){.  i
1692b 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65  nt rc;.  BtShare
1692c 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
1692d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
1692e 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74  e1;..  sqlite3Bt
1692f 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72  reeEnter(p);.  r
16930 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f  c = saveAllCurso
16931 72 73 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a 23  rs(pBt, 0, 0);.#
16932 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16933 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
16934 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16935 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  _OK ){.    /* Th
16936 69 73 20 69 73 20 61 20 68 6f 72 72 69 62 6c 65  is is a horrible
16937 20 73 69 74 75 61 74 69 6f 6e 2e 20 41 6e 20 49   situation. An I
16938 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72  O or malloc() er
16939 72 6f 72 20 6f 63 63 75 72 72 65 64 20 77 68 69  ror occurred whi
1693a 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72 79 69 6e  lst.    ** tryin
1693b 67 20 74 6f 20 73 61 76 65 20 63 75 72 73 6f 72  g to save cursor
1693c 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49 66 20 74   positions. If t
1693d 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 6d 61  his is an automa
1693e 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 28 61 73  tic rollback (as
1693f 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 65 73 75  .    ** the resu
16940 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74 72 61 69  lt of a constrai
16941 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  nt, malloc() fai
16942 6c 75 72 65 20 6f 72 20 49 4f 20 65 72 72 6f 72  lure or IO error
16943 29 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74  ) then .    ** t
16944 68 65 20 63 61 63 68 65 20 6d 61 79 20 62 65 20  he cache may be 
16945 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e 63 6f 6e  internally incon
16946 73 69 73 74 65 6e 74 20 28 6e 6f 74 20 63 6f 6e  sistent (not con
16947 74 61 69 6e 20 76 61 6c 69 64 20 74 72 65 65 73  tain valid trees
16948 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77 65 20 63  ) so.    ** we c
16949 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 72 65 74  annot simply ret
1694a 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 74 6f  urn the error to
1694b 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 73   the caller. Ins
1694c 74 65 61 64 2c 20 61 62 6f 72 74 20 0a 20 20 20  tead, abort .   
1694d 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69 65 73 20   ** all queries 
1694e 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 69 6e  that may be usin
1694f 67 20 61 6e 79 20 6f 66 20 74 68 65 20 63 75 72  g any of the cur
16950 73 6f 72 73 20 74 68 61 74 20 66 61 69 6c 65 64  sors that failed
16951 20 74 6f 20 73 61 76 65 2e 0a 20 20 20 20 2a 2f   to save..    */
16952 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
16953 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28  eTripAllCursors(
16954 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23 65 6e 64  p, rc);.  }.#end
16955 69 66 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72  if.  btreeIntegr
16956 69 74 79 28 70 29 3b 0a 0a 20 20 69 66 28 20 70  ity(p);..  if( p
16957 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
16958 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 6e  _WRITE ){.    in
16959 74 20 72 63 32 3b 0a 0a 20 20 20 20 61 73 73 65  t rc2;..    asse
1695a 72 74 28 20 54 52 41 4e 53 5f 57 52 49 54 45 3d  rt( TRANS_WRITE=
1695b 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74  =pBt->inTransact
1695c 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63 32 20 3d  ion );.    rc2 =
1695d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c   sqlite3PagerRol
1695e 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 61 67 65  lback(pBt->pPage
1695f 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 32 21  r);.    if( rc2!
16960 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
16961 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
16962 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20    }..    /* The 
16963 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76  rollback may hav
16964 65 20 64 65 73 74 72 6f 79 65 64 20 74 68 65 20  e destroyed the 
16965 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 76 61  pPage1->aData va
16966 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20 2a 2a 20  lue.  So.    ** 
16967 63 61 6c 6c 20 62 74 72 65 65 47 65 74 50 61 67  call btreeGetPag
16968 65 28 29 20 6f 6e 20 70 61 67 65 20 31 20 61 67  e() on page 1 ag
16969 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 20 20 20  ain to make.    
1696a 2a 2a 20 73 75 72 65 20 70 50 61 67 65 31 2d 3e  ** sure pPage1->
1696b 61 44 61 74 61 20 69 73 20 73 65 74 20 63 6f 72  aData is set cor
1696c 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 69  rectly. */.    i
1696d 66 28 20 62 74 72 65 65 47 65 74 50 61 67 65 28  f( btreeGetPage(
1696e 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c  pBt, 1, &pPage1,
1696f 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   0)==SQLITE_OK )
16970 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  {.      releaseP
16971 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20  age(pPage1);.   
16972 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 63   }.    assert( c
16973 6f 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73  ountWriteCursors
16974 28 70 42 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20  (pBt)==0 );.    
16975 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
16976 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b  on = TRANS_READ;
16977 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e 64  .  }..  btreeEnd
16978 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b 0a  Transaction(p);.
16979 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1697a 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
1697b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74   rc;.}../*.** St
1697c 61 72 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20  art a statement 
1697d 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  subtransaction. 
1697e 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69  The subtransacti
1697f 6f 6e 20 63 61 6e 20 63 61 6e 20 62 65 20 72 6f  on can can be ro
16980 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 64  lled.** back ind
16981 65 70 65 6e 64 65 6e 74 6c 79 20 6f 66 20 74 68  ependently of th
16982 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69  e main transacti
16983 6f 6e 2e 20 59 6f 75 20 6d 75 73 74 20 73 74 61  on. You must sta
16984 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  rt a transaction
16985 20 0a 2a 2a 20 62 65 66 6f 72 65 20 73 74 61 72   .** before star
16986 74 69 6e 67 20 61 20 73 75 62 74 72 61 6e 73 61  ting a subtransa
16987 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72  ction. The subtr
16988 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 65 6e 64  ansaction is end
16989 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ed automatically
1698a 20 0a 2a 2a 20 69 66 20 74 68 65 20 6d 61 69 6e   .** if the main
1698b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d   transaction com
1698c 6d 69 74 73 20 6f 72 20 72 6f 6c 6c 73 20 62 61  mits or rolls ba
1698d 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d  ck..**.** Statem
1698e 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69  ent subtransacti
1698f 6f 6e 73 20 61 72 65 20 75 73 65 64 20 61 72 6f  ons are used aro
16990 75 6e 64 20 69 6e 64 69 76 69 64 75 61 6c 20 53  und individual S
16991 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  QL statements.**
16992 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 61 69   that are contai
16993 6e 65 64 20 77 69 74 68 69 6e 20 61 20 42 45 47  ned within a BEG
16994 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62 6c 6f 63  IN...COMMIT bloc
16995 6b 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 61  k.  If a constra
16996 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63  int.** error occ
16997 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 73  urs within the s
16998 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 20 65 66  tatement, the ef
16999 66 65 63 74 20 6f 66 20 74 68 61 74 20 6f 6e 65  fect of that one
1699a 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61   statement.** ca
1699b 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n be rolled back
1699c 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
1699d 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  to rollback the 
1699e 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69  entire transacti
1699f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 74 61 74  on..**.** A stat
169a0 65 6d 65 6e 74 20 73 75 62 2d 74 72 61 6e 73 61  ement sub-transa
169a1 63 74 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65  ction is impleme
169a2 6e 74 65 64 20 61 73 20 61 6e 20 61 6e 6f 6e 79  nted as an anony
169a3 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2e 20  mous savepoint. 
169a4 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 70 61 73  The.** value pas
169a5 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
169a6 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  d parameter is t
169a7 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
169a8 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 2c 0a 2a  of savepoints,.*
169a9 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20  * including the 
169aa 6e 65 77 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61  new anonymous sa
169ab 76 65 70 6f 69 6e 74 2c 20 6f 70 65 6e 20 6f 6e  vepoint, open on
169ac 20 74 68 65 20 42 2d 54 72 65 65 2e 20 69 2e 65   the B-Tree. i.e
169ad 2e 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72  . if there.** ar
169ae 65 20 6e 6f 20 61 63 74 69 76 65 20 73 61 76 65  e no active save
169af 70 6f 69 6e 74 73 20 61 6e 64 20 6e 6f 20 6f 74  points and no ot
169b0 68 65 72 20 73 74 61 74 65 6d 65 6e 74 2d 74 72  her statement-tr
169b1 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 2c  ansactions open,
169b2 0a 2a 2a 20 69 53 74 61 74 65 6d 65 6e 74 20 69  .** iStatement i
169b3 73 20 31 2e 20 54 68 69 73 20 61 6e 6f 6e 79 6d  s 1. This anonym
169b4 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 20 63 61  ous savepoint ca
169b5 6e 20 62 65 20 72 65 6c 65 61 73 65 64 20 6f 72  n be released or
169b6 20 72 6f 6c 6c 65 64 20 62 61 63 6b 0a 2a 2a 20   rolled back.** 
169b7 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  using the sqlite
169b8 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28  3BtreeSavepoint(
169b9 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  ) function..*/.S
169ba 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
169bb 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  t sqlite3BtreeBe
169bc 67 69 6e 53 74 6d 74 28 42 74 72 65 65 20 2a 70  ginStmt(Btree *p
169bd 2c 20 69 6e 74 20 69 53 74 61 74 65 6d 65 6e 74  , int iStatement
169be 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  ){.  int rc;.  B
169bf 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
169c0 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33  ->pBt;.  sqlite3
169c1 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
169c2 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72   assert( p->inTr
169c3 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
169c4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42   );.  assert( pB
169c5 74 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29  t->readOnly==0 )
169c6 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 61  ;.  assert( iSta
169c7 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20 20 61 73  tement>0 );.  as
169c8 73 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e 74  sert( iStatement
169c9 3e 70 2d 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69  >p->db->nSavepoi
169ca 6e 74 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45  nt );.  if( NEVE
169cb 52 28 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52  R(p->inTrans!=TR
169cc 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 70 42 74  ANS_WRITE || pBt
169cd 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 7b 0a 20  ->readOnly) ){. 
169ce 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
169cf 4e 54 45 52 4e 41 4c 3b 0a 20 20 7d 65 6c 73 65  NTERNAL;.  }else
169d0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  {.    assert( pB
169d1 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
169d2 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
169d3 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 65 20 70  .    /* At the p
169d4 61 67 65 72 20 6c 65 76 65 6c 2c 20 61 20 73 74  ager level, a st
169d5 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
169d6 69 6f 6e 20 69 73 20 61 20 73 61 76 65 70 6f 69  ion is a savepoi
169d7 6e 74 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61  nt with.    ** a
169d8 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 72 20  n index greater 
169d9 74 68 61 6e 20 61 6c 6c 20 73 61 76 65 70 6f 69  than all savepoi
169da 6e 74 73 20 63 72 65 61 74 65 64 20 65 78 70 6c  nts created expl
169db 69 63 69 74 6c 79 20 75 73 69 6e 67 0a 20 20 20  icitly using.   
169dc 20 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   ** SQL statemen
169dd 74 73 2e 20 49 74 20 69 73 20 69 6c 6c 65 67 61  ts. It is illega
169de 6c 20 74 6f 20 6f 70 65 6e 2c 20 72 65 6c 65 61  l to open, relea
169df 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 61  se or rollback a
169e0 6e 79 0a 20 20 20 20 2a 2a 20 73 75 63 68 20 73  ny.    ** such s
169e1 61 76 65 70 6f 69 6e 74 73 20 77 68 69 6c 65 20  avepoints while 
169e2 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72  the statement tr
169e3 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f  ansaction savepo
169e4 69 6e 74 20 69 73 20 61 63 74 69 76 65 2e 0a 20  int is active.. 
169e5 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73     */.    rc = s
169e6 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53  qlite3PagerOpenS
169e7 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50  avepoint(pBt->pP
169e8 61 67 65 72 2c 20 69 53 74 61 74 65 6d 65 6e 74  ager, iStatement
169e9 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
169ea 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
169eb 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
169ec 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
169ed 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73  argument to this
169ee 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 70 2c 20 69   function, op, i
169ef 73 20 61 6c 77 61 79 73 20 53 41 56 45 50 4f 49  s always SAVEPOI
169f0 4e 54 5f 52 4f 4c 4c 42 41 43 4b 0a 2a 2a 20 6f  NT_ROLLBACK.** o
169f1 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  r SAVEPOINT_RELE
169f2 41 53 45 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ASE. This functi
169f3 6f 6e 20 65 69 74 68 65 72 20 72 65 6c 65 61 73  on either releas
169f4 65 73 20 6f 72 20 72 6f 6c 6c 73 20 62 61 63 6b  es or rolls back
169f5 20 74 68 65 0a 2a 2a 20 73 61 76 65 70 6f 69 6e   the.** savepoin
169f6 74 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  t identified by 
169f7 70 61 72 61 6d 65 74 65 72 20 69 53 61 76 65 70  parameter iSavep
169f8 6f 69 6e 74 2c 20 64 65 70 65 6e 64 69 6e 67 20  oint, depending 
169f9 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 0a 2a 2a  on the value .**
169fa 20 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f   of op..**.** No
169fb 72 6d 61 6c 6c 79 2c 20 69 53 61 76 65 70 6f 69  rmally, iSavepoi
169fc 6e 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68  nt is greater th
169fd 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a  an or equal to z
169fe 65 72 6f 2e 20 48 6f 77 65 76 65 72 2c 20 69 66  ero. However, if
169ff 20 6f 70 20 69 73 0a 2a 2a 20 53 41 56 45 50 4f   op is.** SAVEPO
16a00 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68  INT_ROLLBACK, th
16a01 65 6e 20 69 53 61 76 65 70 6f 69 6e 74 20 6d 61  en iSavepoint ma
16a02 79 20 61 6c 73 6f 20 62 65 20 2d 31 2e 20 49 6e  y also be -1. In
16a03 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 0a   this case the .
16a04 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  ** contents of t
16a05 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61  he entire transa
16a06 63 74 69 6f 6e 20 61 72 65 20 72 6f 6c 6c 65 64  ction are rolled
16a07 20 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 64   back. This is d
16a08 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d  ifferent.** from
16a09 20 61 20 6e 6f 72 6d 61 6c 20 74 72 61 6e 73 61   a normal transa
16a0a 63 74 69 6f 6e 20 72 6f 6c 6c 62 61 63 6b 2c 20  ction rollback, 
16a0b 61 73 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20  as no locks are 
16a0c 72 65 6c 65 61 73 65 64 20 61 6e 64 20 74 68 65  released and the
16a0d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
16a0e 72 65 6d 61 69 6e 73 20 6f 70 65 6e 2e 0a 2a 2f  remains open..*/
16a0f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16a10 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16a11 53 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20  Savepoint(Btree 
16a12 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20  *p, int op, int 
16a13 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69  iSavepoint){.  i
16a14 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
16a15 4b 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  K;.  if( p && p-
16a16 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f  >inTrans==TRANS_
16a17 57 52 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53  WRITE ){.    BtS
16a18 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
16a19 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  pBt;.    assert(
16a1a 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52   op==SAVEPOINT_R
16a1b 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41  ELEASE || op==SA
16a1c 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
16a1d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
16a1e 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c  iSavepoint>=0 ||
16a1f 20 28 69 53 61 76 65 70 6f 69 6e 74 3d 3d 2d 31   (iSavepoint==-1
16a20 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e   && op==SAVEPOIN
16a21 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29 3b 0a 20  T_ROLLBACK) );. 
16a22 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45     sqlite3BtreeE
16a23 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 72 63 20  nter(p);.    rc 
16a24 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61  = sqlite3PagerSa
16a25 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61  vepoint(pBt->pPa
16a26 67 65 72 2c 20 6f 70 2c 20 69 53 61 76 65 70 6f  ger, op, iSavepo
16a27 69 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  int);.    if( rc
16a28 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
16a29 20 20 20 20 20 72 63 20 3d 20 6e 65 77 44 61 74       rc = newDat
16a2a 61 62 61 73 65 28 70 42 74 29 3b 0a 20 20 20 20  abase(pBt);.    
16a2b 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  }.    sqlite3Btr
16a2c 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a  eeLeave(p);.  }.
16a2d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
16a2e 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
16a2f 65 77 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68  ew cursor for th
16a30 65 20 42 54 72 65 65 20 77 68 6f 73 65 20 72 6f  e BTree whose ro
16a31 6f 74 20 69 73 20 6f 6e 20 74 68 65 20 70 61 67  ot is on the pag
16a32 65 0a 2a 2a 20 69 54 61 62 6c 65 2e 20 49 66 20  e.** iTable. If 
16a33 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73  a read-only curs
16a34 6f 72 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  or is requested,
16a35 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   it is assumed t
16a36 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65  hat.** the calle
16a37 72 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 74  r already has at
16a38 20 6c 65 61 73 74 20 61 20 72 65 61 64 2d 6f 6e   least a read-on
16a39 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  ly transaction o
16a3a 70 65 6e 0a 2a 2a 20 6f 6e 20 74 68 65 20 64 61  pen.** on the da
16a3b 74 61 62 61 73 65 20 61 6c 72 65 61 64 79 2e 20  tabase already. 
16a3c 49 66 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f  If a write-curso
16a3d 72 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  r is requested, 
16a3e 74 68 65 6e 0a 2a 2a 20 74 68 65 20 63 61 6c 6c  then.** the call
16a3f 65 72 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  er is assumed to
16a40 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72   have an open wr
16a41 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ite transaction.
16a42 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67  .**.** If wrFlag
16a43 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 63 75  ==0, then the cu
16a44 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 20 62 65  rsor can only be
16a45 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e   used for readin
16a46 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d  g..** If wrFlag=
16a47 3d 31 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  =1, then the cur
16a48 73 6f 72 20 63 61 6e 20 62 65 20 75 73 65 64 20  sor can be used 
16a49 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 66  for reading or f
16a4a 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 69 66  or.** writing if
16a4b 20 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e   other condition
16a4c 73 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 72  s for writing ar
16a4d 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68 65  e also met.  The
16a4e 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 63 6f  se.** are the co
16a4f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 6d 75  nditions that mu
16a50 73 74 20 62 65 20 6d 65 74 20 69 6e 20 6f 72 64  st be met in ord
16a51 65 72 20 66 6f 72 20 77 72 69 74 69 6e 67 20 74  er for writing t
16a52 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 65 64 3a  o.** be allowed:
16a53 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 65 20 63  .**.** 1:  The c
16a54 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20  ursor must have 
16a55 62 65 65 6e 20 6f 70 65 6e 65 64 20 77 69 74 68  been opened with
16a56 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a   wrFlag==1.**.**
16a57 20 32 3a 20 20 4f 74 68 65 72 20 64 61 74 61 62   2:  Other datab
16a58 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
16a59 74 68 61 74 20 73 68 61 72 65 20 74 68 65 20 73  that share the s
16a5a 61 6d 65 20 70 61 67 65 72 20 63 61 63 68 65 0a  ame pager cache.
16a5b 2a 2a 20 20 20 20 20 62 75 74 20 77 68 69 63 68  **     but which
16a5c 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20   are not in the 
16a5d 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44  READ_UNCOMMITTED
16a5e 20 73 74 61 74 65 20 6d 61 79 20 6e 6f 74 20 68   state may not h
16a5f 61 76 65 0a 2a 2a 20 20 20 20 20 63 75 72 73 6f  ave.**     curso
16a60 72 73 20 6f 70 65 6e 20 77 69 74 68 20 77 72 46  rs open with wrF
16a61 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65 20 73 61  lag==0 on the sa
16a62 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74 68 65 72  me table.  Other
16a63 77 69 73 65 0a 2a 2a 20 20 20 20 20 74 68 65 20  wise.**     the 
16a64 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20  changes made by 
16a65 74 68 69 73 20 77 72 69 74 65 20 63 75 72 73 6f  this write curso
16a66 72 20 77 6f 75 6c 64 20 62 65 20 76 69 73 69 62  r would be visib
16a67 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68 65  le to.**     the
16a68 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 69 6e   read cursors in
16a69 20 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 62   the other datab
16a6a 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ase connection..
16a6b 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 20 64 61  **.** 3:  The da
16a6c 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 77  tabase must be w
16a6d 72 69 74 61 62 6c 65 20 28 6e 6f 74 20 6f 6e 20  ritable (not on 
16a6e 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 29  read-only media)
16a6f 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 65 72 65  .**.** 4:  There
16a70 20 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74 69   must be an acti
16a71 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  ve transaction..
16a72 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69 6e  **.** No checkin
16a73 67 20 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61 6b  g is done to mak
16a74 65 20 73 75 72 65 20 74 68 61 74 20 70 61 67 65  e sure that page
16a75 20 69 54 61 62 6c 65 20 72 65 61 6c 6c 79 20 69   iTable really i
16a76 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61  s the.** root pa
16a77 67 65 20 6f 66 20 61 20 62 2d 74 72 65 65 2e 20  ge of a b-tree. 
16a78 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 74   If it is not, t
16a79 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 61  hen the cursor a
16a7a 63 71 75 69 72 65 64 0a 2a 2a 20 77 69 6c 6c 20  cquired.** will 
16a7b 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74  not work correct
16a7c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  ly..**.** It is 
16a7d 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65  assumed that the
16a7e 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
16a7f 73 6f 72 53 69 7a 65 28 29 20 62 79 74 65 73 20  sorSize() bytes 
16a80 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 70 6f  of memory .** po
16a81 69 6e 74 65 64 20 74 6f 20 62 79 20 70 43 75 72  inted to by pCur
16a82 20 68 61 76 65 20 62 65 65 6e 20 7a 65 72 6f 65   have been zeroe
16a83 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e  d by the caller.
16a84 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
16a85 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74  treeCursor(.  Bt
16a86 72 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ree *p,         
16a87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a88 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65       /* The btre
16a89 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c  e */.  int iTabl
16a8a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
16a8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16a8c 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61   Root page of ta
16a8d 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20  ble to open */. 
16a8e 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20   int wrFlag,    
16a8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a90 20 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20          /* 1 to 
16a91 77 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e  write. 0 read-on
16a92 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b  ly */.  struct K
16a93 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
16a94 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
16a95 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 63  * First arg to c
16a96 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
16a97 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72  on */.  BtCursor
16a98 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20   *pCur          
16a99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16a9a 2a 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77 20  * Space for new 
16a9b 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 42  cursor */.){.  B
16a9c 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
16a9d 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20  ->pBt;          
16a9e 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20        /* Shared 
16a9f 62 2d 74 72 65 65 20 68 61 6e 64 6c 65 20 2a 2f  b-tree handle */
16aa0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
16aa1 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
16aa2 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72  ex(p) );.  asser
16aa3 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20  t( wrFlag==0 || 
16aa4 77 72 46 6c 61 67 3d 3d 31 20 29 3b 0a 0a 20 20  wrFlag==1 );..  
16aa5 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
16aa6 20 61 73 73 65 72 74 20 73 74 61 74 65 6d 65 6e   assert statemen
16aa7 74 73 20 76 65 72 69 66 79 20 74 68 61 74 20 69  ts verify that i
16aa8 66 20 74 68 69 73 20 69 73 20 61 20 73 68 61 72  f this is a shar
16aa9 61 62 6c 65 20 0a 20 20 2a 2a 20 62 2d 74 72 65  able .  ** b-tre
16aaa 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20  e database, the 
16aab 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f  connection is ho
16aac 6c 64 69 6e 67 20 74 68 65 20 72 65 71 75 69 72  lding the requir
16aad 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 2c 20  ed table locks, 
16aae 0a 20 20 2a 2a 20 61 6e 64 20 74 68 61 74 20 6e  .  ** and that n
16aaf 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  o other connecti
16ab0 6f 6e 20 68 61 73 20 61 6e 79 20 6f 70 65 6e 20  on has any open 
16ab1 63 75 72 73 6f 72 20 74 68 61 74 20 63 6f 6e 66  cursor that conf
16ab2 6c 69 63 74 73 20 77 69 74 68 20 0a 20 20 2a 2a  licts with .  **
16ab3 20 74 68 69 73 20 6c 6f 63 6b 2e 20 20 2a 2f 0a   this lock.  */.
16ab4 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61    assert( hasSha
16ab5 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
16ab6 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 4b 65  k(p, iTable, pKe
16ab7 79 49 6e 66 6f 21 3d 30 2c 20 77 72 46 6c 61 67  yInfo!=0, wrFlag
16ab8 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  +1) );.  assert(
16ab9 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 21 68   wrFlag==0 || !h
16aba 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28  asReadConflicts(
16abb 70 2c 20 69 54 61 62 6c 65 29 20 29 3b 0a 0a 20  p, iTable) );.. 
16abc 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20   /* Assert that 
16abd 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f  the caller has o
16abe 70 65 6e 65 64 20 74 68 65 20 72 65 71 75 69 72  pened the requir
16abf 65 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ed transaction. 
16ac0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  */.  assert( p->
16ac1 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f  inTrans>TRANS_NO
16ac2 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  NE );.  assert( 
16ac3 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 70 2d 3e  wrFlag==0 || p->
16ac4 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
16ac5 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74  RITE );.  assert
16ac6 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26  ( pBt->pPage1 &&
16ac7 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44   pBt->pPage1->aD
16ac8 61 74 61 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45  ata );..  if( NE
16ac9 56 45 52 28 77 72 46 6c 61 67 20 26 26 20 70 42  VER(wrFlag && pB
16aca 74 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 7b 0a  t->readOnly) ){.
16acb 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16acc 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a  E_READONLY;.  }.
16acd 20 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20    if( iTable==1 
16ace 26 26 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e  && pagerPagecoun
16acf 74 28 70 42 74 29 3d 3d 30 20 29 7b 0a 20 20 20  t(pBt)==0 ){.   
16ad0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
16ad1 4d 50 54 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  MPTY;.  }..  /* 
16ad2 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 68 65  Now that no othe
16ad3 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f 63 63  r errors can occ
16ad4 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c 6c 69  ur, finish filli
16ad5 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75 72 73  ng in the BtCurs
16ad6 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65  or.  ** variable
16ad7 73 20 61 6e 64 20 6c 69 6e 6b 20 74 68 65 20 63  s and link the c
16ad8 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 42  ursor into the B
16ad9 74 53 68 61 72 65 64 20 6c 69 73 74 2e 20 20 2a  tShared list.  *
16ada 2f 0a 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f  /.  pCur->pgnoRo
16adb 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54 61 62 6c  ot = (Pgno)iTabl
16adc 65 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65  e;.  pCur->iPage
16add 20 3d 20 2d 31 3b 0a 20 20 70 43 75 72 2d 3e 70   = -1;.  pCur->p
16ade 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e  KeyInfo = pKeyIn
16adf 66 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 72  fo;.  pCur->pBtr
16ae0 65 65 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e  ee = p;.  pCur->
16ae1 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 43 75  pBt = pBt;.  pCu
16ae2 72 2d 3e 77 72 46 6c 61 67 20 3d 20 28 75 38 29  r->wrFlag = (u8)
16ae3 77 72 46 6c 61 67 3b 0a 20 20 70 43 75 72 2d 3e  wrFlag;.  pCur->
16ae4 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 43 75  pNext = pBt->pCu
16ae5 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 75 72  rsor;.  if( pCur
16ae6 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 70  ->pNext ){.    p
16ae7 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  Cur->pNext->pPre
16ae8 76 20 3d 20 70 43 75 72 3b 0a 20 20 7d 0a 20 20  v = pCur;.  }.  
16ae9 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70  pBt->pCursor = p
16aea 43 75 72 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74  Cur;.  pCur->eSt
16aeb 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56  ate = CURSOR_INV
16aec 41 4c 49 44 3b 0a 20 20 70 43 75 72 2d 3e 63 61  ALID;.  pCur->ca
16aed 63 68 65 64 52 6f 77 69 64 20 3d 20 30 3b 0a 20  chedRowid = 0;. 
16aee 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
16aef 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  K;.}.SQLITE_PRIV
16af0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
16af1 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74  treeCursor(.  Bt
16af2 72 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ree *p,         
16af3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16af4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
16af5 20 62 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20   btree */.  int 
16af6 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  iTable,         
16af7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16af8 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20          /* Root 
16af9 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f  page of table to
16afa 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77   open */.  int w
16afb 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20  rFlag,          
16afc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16afd 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77         /* 1 to w
16afe 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c  rite. 0 read-onl
16aff 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65  y */.  struct Ke
16b00 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c  yInfo *pKeyInfo,
16b01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b02 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20     /* First arg 
16b03 74 6f 20 78 43 6f 6d 70 61 72 65 28 29 20 2a 2f  to xCompare() */
16b04 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
16b05 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
16b06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16b07 2a 20 57 72 69 74 65 20 6e 65 77 20 63 75 72 73  * Write new curs
16b08 6f 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  or here */.){.  
16b09 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
16b0a 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
16b0b 20 20 72 63 20 3d 20 62 74 72 65 65 43 75 72 73    rc = btreeCurs
16b0c 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c 20 77 72  or(p, iTable, wr
16b0d 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66 6f 2c 20  Flag, pKeyInfo, 
16b0e 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  pCur);.  sqlite3
16b0f 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
16b10 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
16b11 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
16b12 73 69 7a 65 20 6f 66 20 61 20 42 74 43 75 72 73  size of a BtCurs
16b13 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20 62 79 74  or object in byt
16b14 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  es..**.** This i
16b15 6e 74 65 72 66 61 63 65 73 20 69 73 20 6e 65 65  nterfaces is nee
16b16 64 65 64 20 73 6f 20 74 68 61 74 20 75 73 65 72  ded so that user
16b17 73 20 6f 66 20 63 75 72 73 6f 72 73 20 63 61 6e  s of cursors can
16b18 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20   preallocate.** 
16b19 73 75 66 66 69 63 69 65 6e 74 20 73 74 6f 72 61  sufficient stora
16b1a 67 65 20 74 6f 20 68 6f 6c 64 20 61 20 63 75 72  ge to hold a cur
16b1b 73 6f 72 2e 20 20 54 68 65 20 42 74 43 75 72 73  sor.  The BtCurs
16b1c 6f 72 20 6f 62 6a 65 63 74 20 69 73 20 6f 70 61  or object is opa
16b1d 71 75 65 0a 2a 2a 20 74 6f 20 75 73 65 72 73 20  que.** to users 
16b1e 73 6f 20 74 68 65 79 20 63 61 6e 6e 6f 74 20 64  so they cannot d
16b1f 6f 20 74 68 65 20 73 69 7a 65 6f 66 28 29 20 74  o the sizeof() t
16b20 68 65 6d 73 65 6c 76 65 73 20 2d 20 74 68 65 79  hemselves - they
16b21 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 74 68   must call.** th
16b22 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53  is routine..*/.S
16b23 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16b24 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
16b25 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 7b 0a  rsorSize(void){.
16b26 20 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28    return sizeof(
16b27 42 74 43 75 72 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a  BtCursor);.}../*
16b28 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 61 63 68  .** Set the cach
16b29 65 64 20 72 6f 77 69 64 20 76 61 6c 75 65 20 6f  ed rowid value o
16b2a 66 20 65 76 65 72 79 20 63 75 72 73 6f 72 20 69  f every cursor i
16b2b 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  n the same datab
16b2c 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 73 20 70  ase file.** as p
16b2d 43 75 72 20 61 6e 64 20 68 61 76 69 6e 67 20 74  Cur and having t
16b2e 68 65 20 73 61 6d 65 20 72 6f 6f 74 20 70 61 67  he same root pag
16b2f 65 20 6e 75 6d 62 65 72 20 61 73 20 70 43 75 72  e number as pCur
16b30 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a  .  The value is.
16b31 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f 77 69 64  ** set to iRowid
16b32 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 70 6f 73  ..**.** Only pos
16b33 69 74 69 76 65 20 72 6f 77 69 64 20 76 61 6c 75  itive rowid valu
16b34 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65  es are considere
16b35 64 20 76 61 6c 69 64 20 66 6f 72 20 74 68 69 73  d valid for this
16b36 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68 65 20 63   cache..** The c
16b37 61 63 68 65 20 69 73 20 69 6e 69 74 69 61 6c 69  ache is initiali
16b38 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20 69 6e 64  zed to zero, ind
16b39 69 63 61 74 69 6e 67 20 61 6e 20 69 6e 76 61 6c  icating an inval
16b3a 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20 41 20 62  id cache..** A b
16b3b 74 72 65 65 20 77 69 6c 6c 20 77 6f 72 6b 20 66  tree will work f
16b3c 69 6e 65 20 77 69 74 68 20 7a 65 72 6f 20 6f 72  ine with zero or
16b3d 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69 64 73   negative rowids
16b3e 2e 20 20 57 65 20 6a 75 73 74 20 63 61 6e 6e 6f  .  We just canno
16b3f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65 72 6f 20  t.** cache zero 
16b40 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69  or negative rowi
16b41 64 73 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20  ds, which means 
16b42 74 61 62 6c 65 73 20 74 68 61 74 20 75 73 65 20  tables that use 
16b43 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61 74  zero or.** negat
16b44 69 76 65 20 72 6f 77 69 64 73 20 6d 69 67 68 74  ive rowids might
16b45 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c   run a little sl
16b46 6f 77 65 72 2e 20 20 42 75 74 20 69 6e 20 70 72  ower.  But in pr
16b47 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a 2a 2a 20  actice, zero.** 
16b48 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69  or negative rowi
16b49 64 73 20 61 72 65 20 76 65 72 79 20 75 6e 63 6f  ds are very unco
16b4a 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20 73 68 6f  mmon so this sho
16b4b 75 6c 64 20 6e 6f 74 20 62 65 20 61 20 70 72 6f  uld not be a pro
16b4c 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  blem..*/.SQLITE_
16b4d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
16b4e 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
16b4f 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72  edRowid(BtCursor
16b50 20 2a 70 43 75 72 2c 20 73 71 6c 69 74 65 33 5f   *pCur, sqlite3_
16b51 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a 20  int64 iRowid){. 
16b52 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20   BtCursor *p;.  
16b53 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70 42 74 2d  for(p=pCur->pBt-
16b54 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70  >pCursor; p; p=p
16b55 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66  ->pNext){.    if
16b56 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 70  ( p->pgnoRoot==p
16b57 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 20  Cur->pgnoRoot ) 
16b58 70 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d  p->cachedRowid =
16b59 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a 20 20 61   iRowid;.  }.  a
16b5a 73 73 65 72 74 28 20 70 43 75 72 2d 3e 63 61 63  ssert( pCur->cac
16b5b 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f 77 69 64  hedRowid==iRowid
16b5c 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   );.}../*.** Ret
16b5d 75 72 6e 20 74 68 65 20 63 61 63 68 65 64 20 72  urn the cached r
16b5e 6f 77 69 64 20 66 6f 72 20 74 68 65 20 67 69 76  owid for the giv
16b5f 65 6e 20 63 75 72 73 6f 72 2e 20 20 41 20 6e 65  en cursor.  A ne
16b60 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 0a 2a  gative or zero.*
16b61 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  * return value i
16b62 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68  ndicates that th
16b63 65 20 72 6f 77 69 64 20 63 61 63 68 65 20 69 73  e rowid cache is
16b64 20 69 6e 76 61 6c 69 64 20 61 6e 64 20 73 68 6f   invalid and sho
16b65 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e 6f 72 65  uld be.** ignore
16b66 64 2e 20 20 49 66 20 74 68 65 20 72 6f 77 69 64  d.  If the rowid
16b67 20 63 61 63 68 65 20 68 61 73 20 6e 65 76 65 72   cache has never
16b68 20 62 65 66 6f 72 65 20 62 65 65 6e 20 73 65 74   before been set
16b69 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a 65 72 6f  , then a.** zero
16b6a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
16b6b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16b6c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71  sqlite3_int64 sq
16b6d 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 61 63  lite3BtreeGetCac
16b6e 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f  hedRowid(BtCurso
16b6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 74 75  r *pCur){.  retu
16b70 72 6e 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52  rn pCur->cachedR
16b71 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  owid;.}../*.** C
16b72 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 2e 20 20  lose a cursor.  
16b73 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e  The read lock on
16b74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
16b75 6c 65 20 69 73 20 72 65 6c 65 61 73 65 64 0a 2a  le is released.*
16b76 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20  * when the last 
16b77 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64  cursor is closed
16b78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
16b79 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
16b7a 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28  treeCloseCursor(
16b7b 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
16b7c 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65  .  Btree *pBtree
16b7d 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b   = pCur->pBtree;
16b7e 0a 20 20 69 66 28 20 70 42 74 72 65 65 20 29 7b  .  if( pBtree ){
16b7f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
16b80 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
16b81 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 20 20 73  pCur->pBt;.    s
16b82 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
16b83 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 73 71  (pBtree);.    sq
16b84 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43  lite3BtreeClearC
16b85 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 20  ursor(pCur);.   
16b86 20 69 66 28 20 70 43 75 72 2d 3e 70 50 72 65 76   if( pCur->pPrev
16b87 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e   ){.      pCur->
16b88 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70  pPrev->pNext = p
16b89 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Cur->pNext;.    
16b8a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 42 74  }else{.      pBt
16b8b 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72  ->pCursor = pCur
16b8c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20  ->pNext;.    }. 
16b8d 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65     if( pCur->pNe
16b8e 78 74 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72  xt ){.      pCur
16b8f 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d  ->pNext->pPrev =
16b90 20 70 43 75 72 2d 3e 70 50 72 65 76 3b 0a 20 20   pCur->pPrev;.  
16b91 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
16b92 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b   i<=pCur->iPage;
16b93 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c   i++){.      rel
16b94 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61  easePage(pCur->a
16b95 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d  pPage[i]);.    }
16b96 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65  .    unlockBtree
16b97 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20  IfUnused(pBt);. 
16b98 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65     invalidateOve
16b99 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29  rflowCache(pCur)
16b9a 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33  ;.    /* sqlite3
16b9b 5f 66 72 65 65 28 70 43 75 72 29 3b 20 2a 2f 0a  _free(pCur); */.
16b9c 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
16b9d 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 20  Leave(pBtree);. 
16b9e 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
16b9f 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
16ba0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 42 74  Make sure the Bt
16ba1 43 75 72 73 6f 72 2a 20 67 69 76 65 6e 20 69 6e  Cursor* given in
16ba2 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 68 61   the argument ha
16ba3 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20 42 74 43  s a valid.** BtC
16ba4 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74 72 75 63  ursor.info struc
16ba5 74 75 72 65 2e 20 20 49 66 20 69 74 20 69 73 20  ture.  If it is 
16ba6 6e 6f 74 20 61 6c 72 65 61 64 79 20 76 61 6c 69  not already vali
16ba7 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74 72 65 65  d, call.** btree
16ba8 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 66  ParseCell() to f
16ba9 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a  ill it in..**.**
16baa 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 69   BtCursor.info i
16bab 73 20 61 20 63 61 63 68 65 20 6f 66 20 74 68 65  s a cache of the
16bac 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20   information in 
16bad 74 68 65 20 63 75 72 72 65 6e 74 20 63 65 6c 6c  the current cell
16bae 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 20  ..** Using this 
16baf 63 61 63 68 65 20 72 65 64 75 63 65 73 20 74 68  cache reduces th
16bb0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c  e number of call
16bb1 73 20 74 6f 20 62 74 72 65 65 50 61 72 73 65 43  s to btreeParseC
16bb2 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20 32 30 30  ell()..**.** 200
16bb3 37 2d 30 36 2d 32 35 3a 20 20 54 68 65 72 65 20  7-06-25:  There 
16bb4 69 73 20 61 20 62 75 67 20 69 6e 20 73 6f 6d 65  is a bug in some
16bb5 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 4d 53 56   versions of MSV
16bb6 43 20 74 68 61 74 20 63 61 75 73 65 20 74 68 65  C that cause the
16bb7 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 74 6f 20  .** compiler to 
16bb8 63 72 61 73 68 20 77 68 65 6e 20 67 65 74 43 65  crash when getCe
16bb9 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69 6d 70 6c  llInfo() is impl
16bba 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61 63  emented as a mac
16bbb 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68 65 72 65  ro..** But there
16bbc 20 69 73 20 61 20 6d 65 61 73 75 72 65 61 62 6c   is a measureabl
16bbd 65 20 73 70 65 65 64 20 61 64 76 61 6e 74 61 67  e speed advantag
16bbe 65 20 74 6f 20 75 73 69 6e 67 20 74 68 65 20 6d  e to using the m
16bbf 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a 2a 20 28  acro on gcc.** (
16bc0 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d 70 69 6c  when less compil
16bc1 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  er optimizations
16bc2 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20 2d 4f 30   like -Os or -O0
16bc3 20 61 72 65 20 75 73 65 64 20 61 6e 64 20 74 68   are used and th
16bc4 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 69 73  e.** compiler is
16bc5 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67 72 65 73   not doing agres
16bc6 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67 2e 29 20  sive inlining.) 
16bc7 20 53 6f 20 77 65 20 75 73 65 20 61 20 72 65 61   So we use a rea
16bc8 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f  l function.** fo
16bc9 72 20 4d 53 56 43 20 61 6e 64 20 61 20 6d 61 63  r MSVC and a mac
16bca 72 6f 20 66 6f 72 20 65 76 65 72 79 74 68 69 6e  ro for everythin
16bcb 67 20 65 6c 73 65 2e 20 20 54 69 63 6b 65 74 20  g else.  Ticket 
16bcc 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65  #2457..*/.#ifnde
16bcd 66 20 4e 44 45 42 55 47 0a 20 20 73 74 61 74 69  f NDEBUG.  stati
16bce 63 20 76 6f 69 64 20 61 73 73 65 72 74 43 65 6c  c void assertCel
16bcf 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20 2a  lInfo(BtCursor *
16bd0 70 43 75 72 29 7b 0a 20 20 20 20 43 65 6c 6c 49  pCur){.    CellI
16bd1 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 69 6e  nfo info;.    in
16bd2 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e  t iPage = pCur->
16bd3 69 50 61 67 65 3b 0a 20 20 20 20 6d 65 6d 73 65  iPage;.    memse
16bd4 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73 69 7a 65  t(&info, 0, size
16bd5 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20 20 20 62  of(info));.    b
16bd6 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43  treeParseCell(pC
16bd7 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65  ur->apPage[iPage
16bd8 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69  ], pCur->aiIdx[i
16bd9 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20  Page], &info);. 
16bda 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 63 6d     assert( memcm
16bdb 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 72 2d 3e  p(&info, &pCur->
16bdc 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e 66  info, sizeof(inf
16bdd 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 23 65  o))==0 );.  }.#e
16bde 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 61 73  lse.  #define as
16bdf 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78 29 0a  sertCellInfo(x).
16be0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 5f 4d  #endif.#ifdef _M
16be1 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73 65 20  SC_VER.  /* Use 
16be2 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 20  a real function 
16be3 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72 6b 20  in MSVC to work 
16be4 61 72 6f 75 6e 64 20 62 75 67 73 20 69 6e 20 74  around bugs in t
16be5 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f  hat compiler. */
16be6 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 67  .  static void g
16be7 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72  etCellInfo(BtCur
16be8 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20  sor *pCur){.    
16be9 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  if( pCur->info.n
16bea 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Size==0 ){.     
16beb 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75   int iPage = pCu
16bec 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 20 20  r->iPage;.      
16bed 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70  btreeParseCell(p
16bee 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67  Cur->apPage[iPag
16bef 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69  e],pCur->aiIdx[i
16bf0 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66  Page],&pCur->inf
16bf1 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e  o);.      pCur->
16bf2 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20  validNKey = 1;. 
16bf3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
16bf4 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70  assertCellInfo(p
16bf5 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Cur);.    }.  }.
16bf6 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74 20  #else /* if not 
16bf7 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20 2f 2a  _MSC_VER */.  /*
16bf8 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 6e 20   Use a macro in 
16bf9 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69 6c  all other compil
16bfa 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 65 20  ers so that the 
16bfb 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c 69  function is inli
16bfc 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 67  ned */.#define g
16bfd 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29  etCellInfo(pCur)
16bfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c01 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 70 43        \.  if( pC
16c02 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d  ur->info.nSize==
16c03 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
16c04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c06 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74         \.    int
16c07 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69   iPage = pCur->i
16c08 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
16c09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c0b 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 62 74          \.    bt
16c0c 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75  reeParseCell(pCu
16c0d 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d  r->apPage[iPage]
16c0e 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61  ,pCur->aiIdx[iPa
16c0f 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29  ge],&pCur->info)
16c10 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d 3e 76 61  ; \.    pCur->va
16c11 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20 20 20 20  lidNKey = 1;    
16c12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c15 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20     \.  }else{   
16c16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c1a 20 20 20 20 5c 0a 20 20 20 20 61 73 73 65 72 74      \.    assert
16c1b 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 20  CellInfo(pCur); 
16c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c1f 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6e 64 69       \.  }.#endi
16c20 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 20 2a 2f  f /* _MSC_VER */
16c21 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
16c22 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 72 6f    /* The next ro
16c23 75 74 69 6e 65 20 75 73 65 64 20 6f 6e 6c 79 20  utine used only 
16c24 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 20  within assert() 
16c25 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 2f 2a  statements */./*
16c26 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
16c27 69 66 20 74 68 65 20 67 69 76 65 6e 20 42 74 43  if the given BtC
16c28 75 72 73 6f 72 20 69 73 20 76 61 6c 69 64 2e 20  ursor is valid. 
16c29 20 41 20 76 61 6c 69 64 20 63 75 72 73 6f 72 20   A valid cursor 
16c2a 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 69  is one.** that i
16c2b 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  s currently poin
16c2c 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 69 6e  ting to a row in
16c2d 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 79 29 20 74   a (non-empty) t
16c2e 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 73 20 69 73  able..** This is
16c2f 20 61 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20   a verification 
16c30 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
16c31 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65  only within asse
16c32 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e  rt() statements.
16c33 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
16c34 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
16c35 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64  reeCursorIsValid
16c36 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
16c37 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 75 72 20  {.  return pCur 
16c38 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  && pCur->eState=
16c39 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 7d  =CURSOR_VALID;.}
16c3a 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55  .#endif /* NDEBU
16c3b 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  G */../*.** Set 
16c3c 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 73 69  *pSize to the si
16c3d 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72  ze of the buffer
16c3e 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20   needed to hold 
16c3f 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20  the value of.** 
16c40 74 68 65 20 6b 65 79 20 66 6f 72 20 74 68 65 20  the key for the 
16c41 63 75 72 72 65 6e 74 20 65 6e 74 72 79 2e 20 20  current entry.  
16c42 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  If the cursor is
16c43 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a   not pointing.**
16c44 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72   to a valid entr
16c45 79 2c 20 2a 70 53 69 7a 65 20 69 73 20 73 65 74  y, *pSize is set
16c46 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f   to 0. .**.** Fo
16c47 72 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 74  r a table with t
16c48 68 65 20 49 4e 54 4b 45 59 20 66 6c 61 67 20 73  he INTKEY flag s
16c49 65 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  et, this routine
16c4a 20 72 65 74 75 72 6e 73 20 74 68 65 20 6b 65 79   returns the key
16c4b 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e 6f 74 20  .** itself, not 
16c4c 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
16c4d 74 65 73 20 69 6e 20 74 68 65 20 6b 65 79 2e 0a  tes in the key..
16c4e 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72  **.** The caller
16c4f 20 6d 75 73 74 20 70 6f 73 69 74 69 6f 6e 20 74   must position t
16c50 68 65 20 63 75 72 73 6f 72 20 70 72 69 6f 72 20  he cursor prior 
16c51 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73  to invoking this
16c52 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a   routine..** .**
16c53 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
16c54 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49 74 20 61  nnot fail.  It a
16c55 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53 51  lways returns SQ
16c56 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a 2f 0a 53 51  LITE_OK.  .*/.SQ
16c57 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
16c58 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
16c59 53 69 7a 65 28 42 74 43 75 72 73 6f 72 20 2a 70  Size(BtCursor *p
16c5a 43 75 72 2c 20 69 36 34 20 2a 70 53 69 7a 65 29  Cur, i64 *pSize)
16c5b 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  {.  assert( curs
16c5c 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
16c5d 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
16c5e 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
16c5f 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c 20  RSOR_INVALID || 
16c60 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
16c61 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
16c62 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  if( pCur->eState
16c63 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  !=CURSOR_VALID )
16c64 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 30  {.    *pSize = 0
16c65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 67  ;.  }else{.    g
16c66 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29  etCellInfo(pCur)
16c67 3b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 70  ;.    *pSize = p
16c68 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a  Cur->info.nKey;.
16c69 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
16c6a 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
16c6b 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74   Set *pSize to t
16c6c 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
16c6d 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68  es of data in th
16c6e 65 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 63  e entry the.** c
16c6f 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20  ursor currently 
16c70 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a  points to..**.**
16c71 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   The caller must
16c72 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
16c73 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 70 6f  the cursor is po
16c74 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e 6f 6e 2d  inting to a non-
16c75 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 64 20 65 6e  NULL.** valid en
16c76 74 72 79 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  try.  In other w
16c77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c 6c 69 6e  ords, the callin
16c78 67 20 70 72 6f 63 65 64 75 72 65 20 6d 75 73 74  g procedure must
16c79 20 67 75 61 72 61 6e 74 65 65 0a 2a 2a 20 74 68   guarantee.** th
16c7a 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 68 61  at the cursor ha
16c7b 73 20 43 75 72 73 6f 72 2e 65 53 74 61 74 65 3d  s Cursor.eState=
16c7c 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a  =CURSOR_VALID..*
16c7d 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73 20  *.** Failure is 
16c7e 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 54  not possible.  T
16c7f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77  his function alw
16c80 61 79 73 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ays returns SQLI
16c81 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 20 6d 69 67  TE_OK..** It mig
16c82 68 74 20 6a 75 73 74 20 61 73 20 77 65 6c 6c 20  ht just as well 
16c83 62 65 20 61 20 70 72 6f 63 65 64 75 72 65 20 28  be a procedure (
16c84 72 65 74 75 72 6e 69 6e 67 20 76 6f 69 64 29 20  returning void) 
16c85 62 75 74 20 77 65 20 63 6f 6e 74 69 6e 75 65 0a  but we continue.
16c86 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20  ** to return an 
16c87 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74 20 63  integer result c
16c88 6f 64 65 20 66 6f 72 20 68 69 73 74 6f 72 69 63  ode for historic
16c89 61 6c 20 72 65 61 73 6f 6e 73 2e 0a 2a 2f 0a 53  al reasons..*/.S
16c8a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16c8b 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  t sqlite3BtreeDa
16c8c 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 20  taSize(BtCursor 
16c8d 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69 7a  *pCur, u32 *pSiz
16c8e 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75  e){.  assert( cu
16c8f 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
16c90 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
16c91 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
16c92 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
16c93 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43    getCellInfo(pC
16c94 75 72 29 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20  ur);.  *pSize = 
16c95 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61  pCur->info.nData
16c96 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
16c97 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  E_OK;.}../*.** G
16c98 69 76 65 6e 20 74 68 65 20 70 61 67 65 20 6e 75  iven the page nu
16c99 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 65 72 66  mber of an overf
16c9a 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20  low page in the 
16c9b 64 61 74 61 62 61 73 65 20 28 70 61 72 61 6d 65  database (parame
16c9c 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c 20 74 68  ter.** ovfl), th
16c9d 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 64  is function find
16c9e 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  s the page numbe
16c9f 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 61  r of the next pa
16ca0 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 6c 69  ge in the .** li
16ca1 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 6f 76 65  nked list of ove
16ca2 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 49 66 20  rflow pages. If 
16ca3 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 75 73 65  possible, it use
16ca4 73 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75  s the auto-vacuu
16ca5 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d 6d 61 70  m.** pointer-map
16ca6 20 64 61 74 61 20 69 6e 73 74 65 61 64 20 6f 66   data instead of
16ca7 20 72 65 61 64 69 6e 67 20 74 68 65 20 63 6f 6e   reading the con
16ca8 74 65 6e 74 20 6f 66 20 70 61 67 65 20 6f 76 66  tent of page ovf
16ca9 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a 2a 2a 0a  l to do so. .**.
16caa 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
16cab 63 63 75 72 73 20 61 6e 20 53 51 4c 69 74 65 20  ccurs an SQLite 
16cac 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
16cad 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
16cae 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67  e:.**.** The pag
16caf 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
16cb0 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61  next overflow pa
16cb1 67 65 20 69 6e 20 74 68 65 20 6c 69 6e 6b 65 64  ge in the linked
16cb2 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20 77 72 69   list is .** wri
16cb3 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f 4e 65  tten to *pPgnoNe
16cb4 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 66 6c  xt. If page ovfl
16cb5 20 69 73 20 74 68 65 20 6c 61 73 74 20 70 61 67   is the last pag
16cb6 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b 65 64 20  e in its linked 
16cb7 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50 67 6e 6f  .** list, *pPgno
16cb8 4e 65 78 74 20 69 73 20 73 65 74 20 74 6f 20 7a  Next is set to z
16cb9 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70  ero. .**.** If p
16cba 70 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c  pPage is not NUL
16cbb 4c 2c 20 61 6e 64 20 61 20 72 65 66 65 72 65 6e  L, and a referen
16cbc 63 65 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67  ce to the MemPag
16cbd 65 20 6f 62 6a 65 63 74 20 63 6f 72 72 65 73 70  e object corresp
16cbe 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 61 67  onding.** to pag
16cbf 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 6c 20 77  e number pOvfl w
16cc0 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 74 68 65  as obtained, the
16cc1 6e 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74  n *ppPage is set
16cc2 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61   to point to tha
16cc3 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 2e 20  t.** reference. 
16cc4 49 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e  It is the respon
16cc5 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20  sibility of the 
16cc6 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c 6c 20 72  caller to call r
16cc7 65 6c 65 61 73 65 50 61 67 65 28 29 0a 2a 2a 20  eleasePage().** 
16cc8 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f 20 66 72  on *ppPage to fr
16cc9 65 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  ee the reference
16cca 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 65 6e 63  . In no referenc
16ccb 65 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 28  e was obtained (
16ccc 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 70  because.** the p
16ccd 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61 73 20 75  ointer-map was u
16cce 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 74 68  sed to obtain th
16ccf 65 20 76 61 6c 75 65 20 66 6f 72 20 2a 70 50 67  e value for *pPg
16cd0 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e 0a 2a 2a  noNext), then.**
16cd1 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 20   *ppPage is set 
16cd2 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74  to zero..*/.stat
16cd3 69 63 20 69 6e 74 20 67 65 74 4f 76 65 72 66 6c  ic int getOverfl
16cd4 6f 77 50 61 67 65 28 0a 20 20 42 74 53 68 61 72  owPage(.  BtShar
16cd5 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20  ed *pBt,        
16cd6 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
16cd7 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
16cd8 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20 20   Pgno ovfl,     
16cd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16cda 20 43 75 72 72 65 6e 74 20 6f 76 65 72 66 6c 6f   Current overflo
16cdb 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f  w page number */
16cdc 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50  .  MemPage **ppP
16cdd 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  age,            
16cde 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67 65 20  /* OUT: MemPage 
16cdf 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65 20 4e  handle (may be N
16ce0 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a  ULL) */.  Pgno *
16ce1 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20 20 20  pPgnoNext       
16ce2 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e         /* OUT: N
16ce3 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ext overflow pag
16ce4 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20  e number */.){. 
16ce5 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30 3b 0a   Pgno next = 0;.
16ce6 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
16ce7 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d   = 0;.  int rc =
16ce8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61   SQLITE_OK;..  a
16ce9 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
16cea 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
16ceb 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
16cec 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a 23  t(pPgnoNext);..#
16ced 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16cee 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
16cef 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 74  /* Try to find t
16cf0 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20  he next page in 
16cf1 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73  the overflow lis
16cf2 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a  t using the.  **
16cf3 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f 69 6e   autovacuum poin
16cf4 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 20 47  ter-map pages. G
16cf5 75 65 73 73 20 74 68 61 74 20 74 68 65 20 6e 65  uess that the ne
16cf6 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 2a 2a  xt page in .  **
16cf7 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69   the overflow li
16cf8 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d 62 65  st is page numbe
16cf9 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20 74  r (ovfl+1). If t
16cfa 68 61 74 20 67 75 65 73 73 20 74 75 72 6e 73 20  hat guess turns 
16cfb 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 20  .  ** out to be 
16cfc 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63 6b  wrong, fall back
16cfd 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 20   to loading the 
16cfe 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a 20 20  data of page .  
16cff 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20 74  ** number ovfl t
16d00 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
16d01 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 65 72  next page number
16d02 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74  ..  */.  if( pBt
16d03 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
16d04 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20      Pgno pgno;. 
16d05 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 20 3d     Pgno iGuess =
16d06 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38 20   ovfl+1;.    u8 
16d07 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 69 6c  eType;..    whil
16d08 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  e( PTRMAP_ISPAGE
16d09 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c 7c  (pBt, iGuess) ||
16d0a 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e 47   iGuess==PENDING
16d0b 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
16d0c 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 73 2b  ){.      iGuess+
16d0d 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  +;.    }..    if
16d0e 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65 72 50  ( iGuess<=pagerP
16d0f 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b  agecount(pBt) ){
16d10 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d  .      rc = ptrm
16d11 61 70 47 65 74 28 70 42 74 2c 20 69 47 75 65 73  apGet(pBt, iGues
16d12 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67 6e 6f  s, &eType, &pgno
16d13 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
16d14 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 54  =SQLITE_OK && eT
16d15 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52  ype==PTRMAP_OVER
16d16 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d 6f  FLOW2 && pgno==o
16d17 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  vfl ){.        n
16d18 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a 20 20  ext = iGuess;.  
16d19 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
16d1a 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a  E_DONE;.      }.
16d1b 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
16d1c 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 65 78 74  ..  assert( next
16d1d 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  ==0 || rc==SQLIT
16d1e 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20  E_DONE );.  if( 
16d1f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
16d20 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47  .    rc = btreeG
16d21 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c  etPage(pBt, ovfl
16d22 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 20  , &pPage, 0);.  
16d23 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
16d24 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65  LITE_OK || pPage
16d25 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 72  ==0 );.    if( r
16d26 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
16d27 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74        next = get
16d28 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61  4byte(pPage->aDa
16d29 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ta);.    }.  }..
16d2a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20 3d 20 6e    *pPgnoNext = n
16d2b 65 78 74 3b 0a 20 20 69 66 28 20 70 70 50 61 67  ext;.  if( ppPag
16d2c 65 20 29 7b 0a 20 20 20 20 2a 70 70 50 61 67 65  e ){.    *ppPage
16d2d 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 65 6c 73   = pPage;.  }els
16d2e 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  e{.    releasePa
16d2f 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20  ge(pPage);.  }. 
16d30 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c   return (rc==SQL
16d31 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49 54  ITE_DONE ? SQLIT
16d32 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f  E_OK : rc);.}../
16d33 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74 61 20 66  *.** Copy data f
16d34 72 6f 6d 20 61 20 62 75 66 66 65 72 20 74 6f 20  rom a buffer to 
16d35 61 20 70 61 67 65 2c 20 6f 72 20 66 72 6f 6d 20  a page, or from 
16d36 61 20 70 61 67 65 20 74 6f 20 61 20 62 75 66 66  a page to a buff
16d37 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 79 6c 6f  er..**.** pPaylo
16d38 61 64 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ad is a pointer 
16d39 74 6f 20 64 61 74 61 20 73 74 6f 72 65 64 20 6f  to data stored o
16d3a 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  n database page 
16d3b 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49 66 20 61  pDbPage..** If a
16d3c 72 67 75 6d 65 6e 74 20 65 4f 70 20 69 73 20 66  rgument eOp is f
16d3d 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42 79 74 65  alse, then nByte
16d3e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 61   bytes of data a
16d3f 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66 72 6f  re copied.** fro
16d40 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f 20 74 68  m pPayload to th
16d41 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64  e buffer pointed
16d42 20 61 74 20 62 79 20 70 42 75 66 2e 20 49 66 20   at by pBuf. If 
16d43 65 4f 70 20 69 73 20 74 72 75 65 2c 0a 2a 2a 20  eOp is true,.** 
16d44 74 68 65 6e 20 73 71 6c 69 74 65 33 50 61 67 65  then sqlite3Page
16d45 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c 6c  rWrite() is call
16d46 65 64 20 6f 6e 20 70 44 62 50 61 67 65 20 61 6e  ed on pDbPage an
16d47 64 20 6e 42 79 74 65 20 62 79 74 65 73 0a 2a 2a  d nByte bytes.**
16d48 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70   of data are cop
16d49 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 66  ied from the buf
16d4a 66 65 72 20 70 42 75 66 20 74 6f 20 70 50 61 79  fer pBuf to pPay
16d4b 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  load..**.** SQLI
16d4c 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
16d4d 64 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 6f 74  d on success, ot
16d4e 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72  herwise an error
16d4f 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
16d50 20 69 6e 74 20 63 6f 70 79 50 61 79 6c 6f 61 64   int copyPayload
16d51 28 0a 20 20 76 6f 69 64 20 2a 70 50 61 79 6c 6f  (.  void *pPaylo
16d52 61 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ad,           /*
16d53 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65   Pointer to page
16d54 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20   data */.  void 
16d55 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 20  *pBuf,          
16d56 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
16d57 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69  to buffer */.  i
16d58 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20  nt nByte,       
16d59 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
16d5a 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 63  er of bytes to c
16d5b 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 70  opy */.  int eOp
16d5c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
16d5d 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f 70 79 20     /* 0 -> copy 
16d5e 66 72 6f 6d 20 70 61 67 65 2c 20 31 20 2d 3e 20  from page, 1 -> 
16d5f 63 6f 70 79 20 74 6f 20 70 61 67 65 20 2a 2f 0a  copy to page */.
16d60 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
16d61 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e           /* P
16d62 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70  age containing p
16d63 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20  Payload */.){.  
16d64 69 66 28 20 65 4f 70 20 29 7b 0a 20 20 20 20 2f  if( eOp ){.    /
16d65 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d  * Copy data from
16d66 20 62 75 66 66 65 72 20 74 6f 20 70 61 67 65 20   buffer to page 
16d67 28 61 20 77 72 69 74 65 20 6f 70 65 72 61 74 69  (a write operati
16d68 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72  on) */.    int r
16d69 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
16d6a 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a  Write(pDbPage);.
16d6b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
16d6c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
16d6d 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
16d6e 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 79 6c      memcpy(pPayl
16d6f 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42 79 74 65  oad, pBuf, nByte
16d70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
16d71 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f  /* Copy data fro
16d72 6d 20 70 61 67 65 20 74 6f 20 62 75 66 66 65 72  m page to buffer
16d73 20 28 61 20 72 65 61 64 20 6f 70 65 72 61 74 69   (a read operati
16d74 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65 6d 63 70  on) */.    memcp
16d75 79 28 70 42 75 66 2c 20 70 50 61 79 6c 6f 61 64  y(pBuf, pPayload
16d76 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20 20  , nByte);.  }.  
16d77 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
16d78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
16d79 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
16d7a 20 74 6f 20 72 65 61 64 20 6f 72 20 6f 76 65 72   to read or over
16d7b 77 72 69 74 65 20 70 61 79 6c 6f 61 64 20 69 6e  write payload in
16d7c 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72  formation.** for
16d7d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20   the entry that 
16d7e 74 68 65 20 70 43 75 72 20 63 75 72 73 6f 72 20  the pCur cursor 
16d7f 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20  is pointing to. 
16d80 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a 20 70 61  If the eOp.** pa
16d81 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68  rameter is 0, th
16d82 69 73 20 69 73 20 61 20 72 65 61 64 20 6f 70 65  is is a read ope
16d83 72 61 74 69 6f 6e 20 28 64 61 74 61 20 63 6f 70  ration (data cop
16d84 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62 75 66 66  ied into.** buff
16d85 65 72 20 70 42 75 66 29 2e 20 49 66 20 69 74 20  er pBuf). If it 
16d86 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 20 77  is non-zero, a w
16d87 72 69 74 65 20 28 64 61 74 61 20 63 6f 70 69 65  rite (data copie
16d88 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66 66 65 72  d from.** buffer
16d89 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a 20 41 20   pBuf)..**.** A 
16d8a 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 22 20 62  total of "amt" b
16d8b 79 74 65 73 20 61 72 65 20 72 65 61 64 20 6f 72  ytes are read or
16d8c 20 77 72 69 74 74 65 6e 20 62 65 67 69 6e 6e 69   written beginni
16d8d 6e 67 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a  ng at "offset"..
16d8e 2a 2a 20 44 61 74 61 20 69 73 20 72 65 61 64 20  ** Data is read 
16d8f 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 65 20 62  to or from the b
16d90 75 66 66 65 72 20 70 42 75 66 2e 0a 2a 2a 0a 2a  uffer pBuf..**.*
16d91 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 62 65  * The content be
16d92 69 6e 67 20 72 65 61 64 20 6f 72 20 77 72 69 74  ing read or writ
16d93 74 65 6e 20 6d 69 67 68 74 20 61 70 70 65 61 72  ten might appear
16d94 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 70 61 67   on the main pag
16d95 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63 61 74 74  e.** or be scatt
16d96 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75 6c 74  ered out on mult
16d97 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20 70 61  iple overflow pa
16d98 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ges..**.** If th
16d99 65 20 42 74 43 75 72 73 6f 72 2e 69 73 49 6e 63  e BtCursor.isInc
16d9a 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67  rblobHandle flag
16d9b 20 69 73 20 73 65 74 2c 20 61 6e 64 20 74 68 65   is set, and the
16d9c 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 75 72 73   current.** curs
16d9d 6f 72 20 65 6e 74 72 79 20 75 73 65 73 20 6f 6e  or entry uses on
16d9e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c  e or more overfl
16d9f 6f 77 20 70 61 67 65 73 2c 20 74 68 69 73 20 66  ow pages, this f
16da0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63  unction.** alloc
16da1 61 74 65 73 20 73 70 61 63 65 20 66 6f 72 20 61  ates space for a
16da2 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70 6c 75 61  nd lazily poplua
16da3 74 65 73 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  tes the overflow
16da4 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a 2a 20 63   page-list .** c
16da5 61 63 68 65 20 61 72 72 61 79 20 28 42 74 43 75  ache array (BtCu
16da6 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 29 2e  rsor.aOverflow).
16da7 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   Subsequent call
16da8 73 20 75 73 65 20 74 68 69 73 0a 2a 2a 20 63 61  s use this.** ca
16da9 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 65 65 6b  che to make seek
16daa 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 6c  ing to the suppl
16dab 69 65 64 20 6f 66 66 73 65 74 20 6d 6f 72 65 20  ied offset more 
16dac 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a  efficient..**.**
16dad 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f   Once an overflo
16dae 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68  w page-list cach
16daf 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63  e has been alloc
16db0 61 74 65 64 2c 20 69 74 20 6d 61 79 20 62 65 0a  ated, it may be.
16db1 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 20 69  ** invalidated i
16db2 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 75 72  f some other cur
16db3 73 6f 72 20 77 72 69 74 65 73 20 74 6f 20 74 68  sor writes to th
16db4 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 6f 72  e same table, or
16db5 20 69 66 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f   if.** the curso
16db6 72 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 61 20  r is moved to a 
16db7 64 69 66 66 65 72 65 6e 74 20 72 6f 77 2e 20 41  different row. A
16db8 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 6e 20  dditionally, in 
16db9 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 6d  auto-vacuum.** m
16dba 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ode, the followi
16dbb 6e 67 20 65 76 65 6e 74 73 20 6d 61 79 20 69 6e  ng events may in
16dbc 76 61 6c 69 64 61 74 65 20 61 6e 20 6f 76 65 72  validate an over
16dbd 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63  flow page-list c
16dbe 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  ache..**.**   * 
16dbf 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76  An incremental v
16dc0 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a 20 41 20  acuum,.**   * A 
16dc1 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74 6f 5f 76  commit in auto_v
16dc2 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20 6d 6f 64  acuum="full" mod
16dc3 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65 61 74 69  e,.**   * Creati
16dc4 6e 67 20 61 20 74 61 62 6c 65 20 28 6d 61 79 20  ng a table (may 
16dc5 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 20 61  require moving a
16dc6 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 29  n overflow page)
16dc7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
16dc8 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 0a 20  accessPayload(. 
16dc9 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c   BtCursor *pCur,
16dca 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
16dcb 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72  pointing to entr
16dcc 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a  y to read from *
16dcd 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74 2c 20  /.  u32 offset, 
16dce 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69           /* Begi
16dcf 6e 20 72 65 61 64 69 6e 67 20 74 68 69 73 20 66  n reading this f
16dd0 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f 61 64 20  ar into payload 
16dd1 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c 20 20 20  */.  u32 amt,   
16dd2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61            /* Rea
16dd3 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65  d this many byte
16dd4 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  s */.  unsigned 
16dd5 63 68 61 72 20 2a 70 42 75 66 2c 20 2f 2a 20 57  char *pBuf, /* W
16dd6 72 69 74 65 20 74 68 65 20 62 79 74 65 73 20 69  rite the bytes i
16dd7 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72 20  nto this buffer 
16dd8 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70 20 20 20  */ .  int eOp   
16dd9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 65             /* ze
16dda 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d  ro to read. non-
16ddb 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a  zero to write. *
16ddc 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  /.){.  unsigned 
16ddd 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a  char *aPayload;.
16dde 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
16ddf 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79  E_OK;.  u32 nKey
16de0 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30  ;.  int iIdx = 0
16de1 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
16de2 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
16de3 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20  e[pCur->iPage]; 
16de4 2f 2a 20 42 74 72 65 65 20 70 61 67 65 20 6f 66  /* Btree page of
16de5 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 20 2a   current entry *
16de6 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  /.  BtShared *pB
16de7 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 20 20  t = pCur->pBt;  
16de8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16de9 2f 2a 20 42 74 72 65 65 20 74 68 69 73 20 63 75  /* Btree this cu
16dea 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20  rsor belongs to 
16deb 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
16dec 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  age );.  assert(
16ded 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
16dee 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
16def 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
16df0 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
16df1 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  ]<pPage->nCell )
16df2 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ;.  assert( curs
16df3 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
16df4 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c  r) );..  getCell
16df5 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50  Info(pCur);.  aP
16df6 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69  ayload = pCur->i
16df7 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72  nfo.pCell + pCur
16df8 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a  ->info.nHeader;.
16df9 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 2d    nKey = (pPage-
16dfa 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20 28 69  >intKey ? 0 : (i
16dfb 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b  nt)pCur->info.nK
16dfc 65 79 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45  ey);..  if( NEVE
16dfd 52 28 6f 66 66 73 65 74 2b 61 6d 74 20 3e 20 6e  R(offset+amt > n
16dfe 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  Key+pCur->info.n
16dff 44 61 74 61 29 20 0a 20 20 20 7c 7c 20 26 61 50  Data) .   || &aP
16e00 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66  ayload[pCur->inf
16e01 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70 50 61  o.nLocal] > &pPa
16e02 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75  ge->aData[pBt->u
16e03 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 29 7b 0a  sableSize].  ){.
16e04 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f      /* Trying to
16e05 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 70   read or write p
16e06 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
16e07 68 65 20 64 61 74 61 20 69 73 20 61 6e 20 65 72  he data is an er
16e08 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ror */.    retur
16e09 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
16e0a 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  _BKPT;.  }..  /*
16e0b 20 43 68 65 63 6b 20 69 66 20 64 61 74 61 20 6d   Check if data m
16e0c 75 73 74 20 62 65 20 72 65 61 64 2f 77 72 69 74  ust be read/writ
16e0d 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 65 20  ten to/from the 
16e0e 62 74 72 65 65 20 70 61 67 65 20 69 74 73 65 6c  btree page itsel
16e0f 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73  f. */.  if( offs
16e10 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c  et<pCur->info.nL
16e11 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20  ocal ){.    int 
16e12 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 66 28  a = amt;.    if(
16e13 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 2d 3e   a+offset>pCur->
16e14 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20  info.nLocal ){. 
16e15 20 20 20 20 20 61 20 3d 20 70 43 75 72 2d 3e 69       a = pCur->i
16e16 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66  nfo.nLocal - off
16e17 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  set;.    }.    r
16e18 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28  c = copyPayload(
16e19 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74  &aPayload[offset
16e1a 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c  ], pBuf, a, eOp,
16e1b 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29   pPage->pDbPage)
16e1c 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30  ;.    offset = 0
16e1d 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b  ;.    pBuf += a;
16e1e 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20  .    amt -= a;. 
16e1f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73   }else{.    offs
16e20 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f  et -= pCur->info
16e21 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20  .nLocal;.  }..  
16e22 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16e23 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20  K && amt>0 ){.  
16e24 20 20 63 6f 6e 73 74 20 75 33 32 20 6f 76 66 6c    const u32 ovfl
16e25 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62  Size = pBt->usab
16e26 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20  leSize - 4;  /* 
16e27 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20 70 65  Bytes content pe
16e28 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20  r ovfl page */. 
16e29 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61 67 65     Pgno nextPage
16e2a 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67 65 20  ;..    nextPage 
16e2b 3d 20 67 65 74 34 62 79 74 65 28 26 61 50 61 79  = get4byte(&aPay
16e2c 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e  load[pCur->info.
16e2d 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64  nLocal]);..#ifnd
16e2e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
16e2f 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 49  NCRBLOB.    /* I
16e30 66 20 74 68 65 20 69 73 49 6e 63 72 62 6c 6f 62  f the isIncrblob
16e31 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73  Handle flag is s
16e32 65 74 20 61 6e 64 20 74 68 65 20 42 74 43 75 72  et and the BtCur
16e33 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a  sor.aOverflow[].
16e34 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62      ** has not b
16e35 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61  een allocated, a
16e36 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 20  llocate it now. 
16e37 54 68 65 20 61 72 72 61 79 20 69 73 20 73 69 7a  The array is siz
16e38 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f 6e 65  ed at.    ** one
16e39 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20   entry for each 
16e3a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e  overflow page in
16e3b 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68   the overflow ch
16e3c 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20  ain. The.    ** 
16e3d 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  page number of t
16e3e 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f  he first overflo
16e3f 77 20 70 61 67 65 20 69 73 20 73 74 6f 72 65 64  w page is stored
16e40 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b 30 5d   in aOverflow[0]
16e41 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20 41 20  ,.    ** etc. A 
16e42 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20 74 68  value of 0 in th
16e43 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72  e aOverflow[] ar
16e44 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74 20 79  ray means "not y
16e45 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a  et known".    **
16e46 20 28 74 68 65 20 63 61 63 68 65 20 69 73 20 6c   (the cache is l
16e47 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 64 29  azily populated)
16e48 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
16e49 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f   pCur->isIncrblo
16e4a 62 48 61 6e 64 6c 65 20 26 26 20 21 70 43 75 72  bHandle && !pCur
16e4b 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20  ->aOverflow ){. 
16e4c 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d       int nOvfl =
16e4d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61   (pCur->info.nPa
16e4e 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f  yload-pCur->info
16e4f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65  .nLocal+ovflSize
16e50 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20  -1)/ovflSize;.  
16e51 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66      pCur->aOverf
16e52 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29 73 71  low = (Pgno *)sq
16e53 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28  lite3MallocZero(
16e54 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76  sizeof(Pgno)*nOv
16e55 66 6c 29 3b 0a 20 20 20 20 20 20 2f 2a 20 6e 4f  fl);.      /* nO
16e56 76 66 6c 20 69 73 20 61 6c 77 61 79 73 20 70 6f  vfl is always po
16e57 73 69 74 69 76 65 2e 20 20 49 66 20 69 74 20 77  sitive.  If it w
16e58 65 72 65 20 7a 65 72 6f 2c 20 66 65 74 63 68 50  ere zero, fetchP
16e59 61 79 6c 6f 61 64 20 77 6f 75 6c 64 20 68 61 76  ayload would hav
16e5a 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 65 6e 20  e.      ** been 
16e5b 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20  used instead of 
16e5c 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 2a 2f  this routine. */
16e5d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59  .      if( ALWAY
16e5e 53 28 6e 4f 76 66 6c 29 20 26 26 20 21 70 43 75  S(nOvfl) && !pCu
16e5f 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a  r->aOverflow ){.
16e60 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
16e61 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
16e62 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
16e63 20 49 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   If the overflow
16e64 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65   page-list cache
16e65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61   has been alloca
16e66 74 65 64 20 61 6e 64 20 74 68 65 0a 20 20 20 20  ted and the.    
16e67 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  ** entry for the
16e68 20 66 69 72 73 74 20 72 65 71 75 69 72 65 64 20   first required 
16e69 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73  overflow page is
16e6a 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a 20 20 20   valid, skip.   
16e6b 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20   ** directly to 
16e6c 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  it..    */.    i
16e6d 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  f( pCur->aOverfl
16e6e 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 65  ow && pCur->aOve
16e6f 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f 76 66  rflow[offset/ovf
16e70 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 20 20 20 20  lSize] ){.      
16e71 69 49 64 78 20 3d 20 28 6f 66 66 73 65 74 2f 6f  iIdx = (offset/o
16e72 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  vflSize);.      
16e73 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 72 2d  nextPage = pCur-
16e74 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d  >aOverflow[iIdx]
16e75 3b 0a 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d  ;.      offset =
16e76 20 28 6f 66 66 73 65 74 25 6f 76 66 6c 53 69 7a   (offset%ovflSiz
16e77 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  e);.    }.#endif
16e78 0a 0a 20 20 20 20 66 6f 72 28 20 3b 20 72 63 3d  ..    for( ; rc=
16e79 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d  =SQLITE_OK && am
16e7a 74 3e 30 20 26 26 20 6e 65 78 74 50 61 67 65 3b  t>0 && nextPage;
16e7b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 69 66 6e 64   iIdx++){..#ifnd
16e7c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
16e7d 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 2f 2a  NCRBLOB.      /*
16e7e 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 70 6f   If required, po
16e7f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 72 66  pulate the overf
16e80 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61  low page-list ca
16e81 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  che. */.      if
16e82 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  ( pCur->aOverflo
16e83 77 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  w ){.        ass
16e84 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72  ert(!pCur->aOver
16e85 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c 20 70 43  flow[iIdx] || pC
16e86 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49  ur->aOverflow[iI
16e87 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65 29 3b 0a  dx]==nextPage);.
16e88 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f          pCur->aO
16e89 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 3d 20  verflow[iIdx] = 
16e8a 6e 65 78 74 50 61 67 65 3b 0a 20 20 20 20 20 20  nextPage;.      
16e8b 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20  }.#endif..      
16e8c 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f 76 66 6c  if( offset>=ovfl
16e8d 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Size ){.        
16e8e 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 73  /* The only reas
16e8f 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 69 73 20  on to read this 
16e90 70 61 67 65 20 69 73 20 74 6f 20 6f 62 74 61 69  page is to obtai
16e91 6e 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20  n the page.     
16e92 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72     ** number for
16e93 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69   the next page i
16e94 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63  n the overflow c
16e95 68 61 69 6e 2e 20 54 68 65 20 70 61 67 65 0a 20  hain. The page. 
16e96 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 20 69         ** data i
16e97 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 20  s not required. 
16e98 53 6f 20 66 69 72 73 74 20 74 72 79 20 74 6f 20  So first try to 
16e99 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76 65 72 66  lookup the overf
16e9a 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  low.        ** p
16e9b 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2c 20  age-list cache, 
16e9c 69 66 20 61 6e 79 2c 20 74 68 65 6e 20 66 61 6c  if any, then fal
16e9d 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 67 65  l back to the ge
16e9e 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 29 0a  tOverflowPage().
16e9f 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74          ** funct
16ea0 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ion..        */.
16ea1 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
16ea2 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20  MIT_INCRBLOB.   
16ea3 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61       if( pCur->a
16ea4 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 75 72  Overflow && pCur
16ea5 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78  ->aOverflow[iIdx
16ea6 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  +1] ){.         
16ea7 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 72   nextPage = pCur
16ea8 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78  ->aOverflow[iIdx
16ea9 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 7d 20 65  +1];.        } e
16eaa 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20 20 20  lse .#endif.    
16eab 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76        rc = getOv
16eac 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20  erflowPage(pBt, 
16ead 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 26 6e 65  nextPage, 0, &ne
16eae 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  xtPage);.       
16eaf 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76 66 6c 53   offset -= ovflS
16eb0 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ize;.      }else
16eb1 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 65  {.        /* Nee
16eb2 64 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 70  d to read this p
16eb3 61 67 65 20 70 72 6f 70 65 72 6c 79 2e 20 49 74  age properly. It
16eb4 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 6f   contains some o
16eb5 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  f the.        **
16eb6 20 72 61 6e 67 65 20 6f 66 20 64 61 74 61 20 74   range of data t
16eb7 68 61 74 20 69 73 20 62 65 69 6e 67 20 72 65 61  hat is being rea
16eb8 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72 20 77 72  d (eOp==0) or wr
16eb9 69 74 74 65 6e 20 28 65 4f 70 21 3d 30 29 2e 0a  itten (eOp!=0)..
16eba 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
16ebb 20 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61     DbPage *pDbPa
16ebc 67 65 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ge;.        int 
16ebd 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 20 20 20  a = amt;.       
16ebe 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
16ebf 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65  erGet(pBt->pPage
16ec0 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 26 70 44  r, nextPage, &pD
16ec1 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
16ec2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16ec3 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  K ){.          a
16ec4 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c 69 74 65  Payload = sqlite
16ec5 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44  3PagerGetData(pD
16ec6 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
16ec7 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74    nextPage = get
16ec8 34 62 79 74 65 28 61 50 61 79 6c 6f 61 64 29 3b  4byte(aPayload);
16ec9 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61  .          if( a
16eca 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f 76 66 6c   + offset > ovfl
16ecb 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Size ){.        
16ecc 20 20 20 20 61 20 3d 20 6f 76 66 6c 53 69 7a 65      a = ovflSize
16ecd 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20   - offset;.     
16ece 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
16ecf 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61   rc = copyPayloa
16ed0 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73  d(&aPayload[offs
16ed1 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 61 2c 20  et+4], pBuf, a, 
16ed2 65 4f 70 2c 20 70 44 62 50 61 67 65 29 3b 0a 20  eOp, pDbPage);. 
16ed3 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
16ed4 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61  PagerUnref(pDbPa
16ed5 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f  ge);.          o
16ed6 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20  ffset = 0;.     
16ed7 20 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20       amt -= a;. 
16ed8 20 20 20 20 20 20 20 20 20 70 42 75 66 20 2b 3d           pBuf +=
16ed9 20 61 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   a;.        }.  
16eda 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
16edb 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
16edc 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b  E_OK && amt>0 ){
16edd 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
16ede 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
16edf 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
16ee0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
16ee1 70 61 72 74 20 6f 66 20 74 68 65 20 6b 65 79 20  part of the key 
16ee2 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
16ee3 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78  cursor pCur.  Ex
16ee4 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62  actly.** "amt" b
16ee5 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61  ytes will be tra
16ee6 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75  nsfered into pBu
16ee7 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66  f[].  The transf
16ee8 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20  er.** begins at 
16ee9 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20  "offset"..**.** 
16eea 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  The caller must 
16eeb 65 6e 73 75 72 65 20 74 68 61 74 20 70 43 75 72  ensure that pCur
16eec 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   is pointing to 
16eed 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a 2a 20 69  a valid row.** i
16eee 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  n the table..**.
16eef 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
16ef0 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f  _OK on success o
16ef1 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  r an error code 
16ef2 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  if anything goes
16ef3 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65  .** wrong.  An e
16ef4 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
16ef5 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22   if "offset+amt"
16ef6 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a   is larger than.
16ef7 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65  ** the available
16ef8 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c   payload..*/.SQL
16ef9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
16efa 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28  sqlite3BtreeKey(
16efb 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
16efc 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
16efd 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29  amt, void *pBuf)
16efe 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  {.  assert( curs
16eff 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
16f00 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
16f01 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
16f02 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
16f03 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50  assert( pCur->iP
16f04 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e  age>=0 && pCur->
16f05 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
16f06 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge] );.  assert(
16f07 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
16f08 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e  r->iPage]<pCur->
16f09 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
16f0a 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  ge]->nCell );.  
16f0b 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 79  return accessPay
16f0c 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65  load(pCur, offse
16f0d 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65  t, amt, (unsigne
16f0e 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20 30 29  d char*)pBuf, 0)
16f0f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
16f10 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  part of the data
16f11 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
16f12 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45   cursor pCur.  E
16f13 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20  xactly.** "amt" 
16f14 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72  bytes will be tr
16f15 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42  ansfered into pB
16f16 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73  uf[].  The trans
16f17 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74  fer.** begins at
16f18 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a   "offset"..**.**
16f19 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
16f1a 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20  K on success or 
16f1b 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  an error code if
16f1c 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a   anything goes.*
16f1d 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72  * wrong.  An err
16f1e 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  or is returned i
16f1f 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 69  f "offset+amt" i
16f20 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a  s larger than.**
16f21 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 70   the available p
16f22 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54  ayload..*/.SQLIT
16f23 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
16f24 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 42  lite3BtreeData(B
16f25 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75  tCursor *pCur, u
16f26 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61  32 offset, u32 a
16f27 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b  mt, void *pBuf){
16f28 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 6e  .  int rc;..#ifn
16f29 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
16f2a 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 20  INCRBLOB.  if ( 
16f2b 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
16f2c 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a  RSOR_INVALID ){.
16f2d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16f2e 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 6e  E_ABORT;.  }.#en
16f2f 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 63  dif..  assert( c
16f30 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
16f31 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20  pCur) );.  rc = 
16f32 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  restoreCursorPos
16f33 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69  ition(pCur);.  i
16f34 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
16f35 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
16f36 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
16f37 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
16f38 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16f39 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72  iPage>=0 && pCur
16f3a 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
16f3b 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 73  Page] );.    ass
16f3c 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
16f3d 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43  [pCur->iPage]<pC
16f3e 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
16f3f 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29  >iPage]->nCell )
16f40 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 73  ;.    rc = acces
16f41 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f  sPayload(pCur, o
16f42 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 66  ffset, amt, pBuf
16f43 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
16f44 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
16f45 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
16f46 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f   to payload info
16f47 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65  rmation from the
16f48 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20   entry that the 
16f49 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f 72 20  .** pCur cursor 
16f4a 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20  is pointing to. 
16f4b 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20   The pointer is 
16f4c 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  to the beginning
16f4d 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 69   of.** the key i
16f4e 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61 6e 64  f skipKey==0 and
16f4f 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
16f50 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 64  e beginning of d
16f51 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70 4b 65  ata if.** skipKe
16f52 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d 62 65  y==1.  The numbe
16f53 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76  r of bytes of av
16f54 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61 74 61  ailable key/data
16f55 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69   is written.** i
16f56 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66 20 2a  nto *pAmt.  If *
16f57 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68  pAmt==0, then th
16f58 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
16f59 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a 2a 20   will not be.** 
16f5a 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e  a valid pointer.
16f5b 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
16f5c 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69 6d 69  ine is an optimi
16f5d 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73 20 63  zation.  It is c
16f5e 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20 65 6e  ommon for the en
16f5f 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e 64 20  tire key.** and 
16f60 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e 20 74  data to fit on t
16f61 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20 61 6e  he local page an
16f62 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62  d for there to b
16f63 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a  e no overflow.**
16f64 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20 74 68   pages.  When th
16f65 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73 20 72  at is so, this r
16f66 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73  outine can be us
16f67 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ed to access the
16f68 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61  .** key and data
16f69 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e 67 20   without making 
16f6a 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68 65 20  a copy.  If the 
16f6b 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74 61 20  key and/or data 
16f6c 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 6f  spills.** onto o
16f6d 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74  verflow pages, t
16f6e 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61  hen accessPayloa
16f6f 64 28 29 20 6d 75 73 74 20 62 65 20 75 73 65 64  d() must be used
16f70 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 65 0a 2a   to reassemble.*
16f71 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61 20 61  * the key/data a
16f72 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74 6f 20  nd copy it into 
16f73 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 62  a preallocated b
16f74 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  uffer..**.** The
16f75 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65   pointer returne
16f76 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e  d by this routin
16f77 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74 6c 79  e looks directly
16f78 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 64   into the cached
16f79 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68 65 20  .** page of the 
16f7a 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 64  database.  The d
16f7b 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e 67 65  ata might change
16f7c 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e 65 78   or move the nex
16f7d 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 62 74  t time.** any bt
16f7e 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73 20 63  ree routine is c
16f7f 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  alled..*/.static
16f80 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
16f81 63 68 61 72 20 2a 66 65 74 63 68 50 61 79 6c 6f  char *fetchPaylo
16f82 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  ad(.  BtCursor *
16f83 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75  pCur,      /* Cu
16f84 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f  rsor pointing to
16f85 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66   entry to read f
16f86 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41  rom */.  int *pA
16f87 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  mt,           /*
16f88 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65   Write the numbe
16f89 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62  r of available b
16f8a 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 69  ytes here */.  i
16f8b 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20 20 20  nt skipKey      
16f8c 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65 67 69      /* read begi
16f8d 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20 69 66  nning at data if
16f8e 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f   this is true */
16f8f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  .){.  unsigned c
16f90 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20  har *aPayload;. 
16f91 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
16f92 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 75  .  u32 nKey;.  u
16f93 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20 61 73  32 nLocal;..  as
16f94 73 65 72 74 28 20 70 43 75 72 21 3d 30 20 26 26  sert( pCur!=0 &&
16f95 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20   pCur->iPage>=0 
16f96 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  && pCur->apPage[
16f97 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20  pCur->iPage]);. 
16f98 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
16f99 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
16f9a 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28  LID );.  assert(
16f9b 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
16f9c 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70 50 61  x(pCur) );.  pPa
16f9d 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
16f9e 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
16f9f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16fa0 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
16fa1 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  e]<pPage->nCell 
16fa2 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  );.  if( NEVER(p
16fa3 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d  Cur->info.nSize=
16fa4 3d 30 29 20 29 7b 0a 20 20 20 20 62 74 72 65 65  =0) ){.    btree
16fa5 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e  ParseCell(pCur->
16fa6 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
16fa7 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 78  ge], pCur->aiIdx
16fa8 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c 0a 20  [pCur->iPage],. 
16fa9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16faa 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a    &pCur->info);.
16fab 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61 64 20 3d    }.  aPayload =
16fac 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c   pCur->info.pCel
16fad 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d  l;.  aPayload +=
16fae 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61   pCur->info.nHea
16faf 64 65 72 3b 0a 20 20 69 66 28 20 70 50 61 67 65  der;.  if( pPage
16fb0 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20  ->intKey ){.    
16fb1 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  nKey = 0;.  }els
16fb2 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 28 69  e{.    nKey = (i
16fb3 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b  nt)pCur->info.nK
16fb4 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 6b  ey;.  }.  if( sk
16fb5 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 50 61  ipKey ){.    aPa
16fb6 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a 20  yload += nKey;. 
16fb7 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72     nLocal = pCur
16fb8 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20  ->info.nLocal - 
16fb9 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  nKey;.  }else{. 
16fba 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72     nLocal = pCur
16fbb 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20  ->info.nLocal;. 
16fbc 20 20 20 61 73 73 65 72 74 28 20 6e 4c 6f 63 61     assert( nLoca
16fbd 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20 7d 0a 20  l<=nKey );.  }. 
16fbe 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 6c 3b   *pAmt = nLocal;
16fbf 0a 20 20 72 65 74 75 72 6e 20 61 50 61 79 6c 6f  .  return aPaylo
16fc0 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f  ad;.}.../*.** Fo
16fc1 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  r the entry that
16fc2 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20   cursor pCur is 
16fc3 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 6e  point to, return
16fc4 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74 65   as.** many byte
16fc5 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 72 20  s of the key or 
16fc6 64 61 74 61 20 61 73 20 61 72 65 20 61 76 61 69  data as are avai
16fc7 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63  lable on the loc
16fc8 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61 67  al.** b-tree pag
16fc9 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 6e 75  e.  Write the nu
16fca 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c  mber of availabl
16fcb 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a 70 41  e bytes into *pA
16fcc 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  mt..**.** The po
16fcd 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 69  inter returned i
16fce 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 54 68  s ephemeral.  Th
16fcf 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 20 6d  e key/data may m
16fd0 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65 73  ove.** or be des
16fd1 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e 65  troyed on the ne
16fd2 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 42  xt call to any B
16fd3 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a  tree routine,.**
16fd4 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73   including calls
16fd5 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 72 65   from other thre
16fd6 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 65 20  ads against the 
16fd7 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20 48  same cache..** H
16fd8 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 6f 6e  ence, a mutex on
16fd9 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 68   the BtShared sh
16fda 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 72 69  ould be held pri
16fdb 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a  or to calling.**
16fdc 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
16fdd 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
16fde 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f 20 67  nes is used to g
16fdf 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 73 20  et quick access 
16fe0 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 61 0a  to key and data.
16fe1 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e  ** in the common
16fe2 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f 20 6f   case where no o
16fe3 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72  verflow pages ar
16fe4 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  e used..*/.SQLIT
16fe5 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
16fe6 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72  void *sqlite3Btr
16fe7 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 72  eeKeyFetch(BtCur
16fe8 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
16fe9 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 76  pAmt){.  const v
16fea 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 73  oid *p = 0;.  as
16feb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
16fec 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70  tex_held(pCur->p
16fed 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78  Btree->db->mutex
16fee 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63  ) );.  assert( c
16fef 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
16ff0 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 41  pCur) );.  if( A
16ff1 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61  LWAYS(pCur->eSta
16ff2 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
16ff3 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 6f  ) ){.    p = (co
16ff4 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50  nst void*)fetchP
16ff5 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d  ayload(pCur, pAm
16ff6 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  t, 0);.  }.  ret
16ff7 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 54 45 5f  urn p;.}.SQLITE_
16ff8 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f  PRIVATE const vo
16ff9 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  id *sqlite3Btree
16ffa 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73  DataFetch(BtCurs
16ffb 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  or *pCur, int *p
16ffc 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f  Amt){.  const vo
16ffd 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 73 73  id *p = 0;.  ass
16ffe 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
16fff 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42  ex_held(pCur->pB
17000 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
17001 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75   );.  assert( cu
17002 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
17003 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 41 4c  Cur) );.  if( AL
17004 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61 74  WAYS(pCur->eStat
17005 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 29  e==CURSOR_VALID)
17006 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 6f 6e   ){.    p = (con
17007 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50 61  st void*)fetchPa
17008 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d 74  yload(pCur, pAmt
17009 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 1);.  }.  retu
1700a 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn p;.}.../*.** 
1700b 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
1700c 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 63 68  down to a new ch
1700d 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 20 6e  ild page.  The n
1700e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20  ewPgno argument 
1700f 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e  is the.** page n
17010 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63 68 69  umber of the chi
17011 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20  ld page to move 
17012 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  to..**.** This f
17013 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
17014 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69  SQLITE_CORRUPT i
17015 66 20 74 68 65 20 70 61 67 65 2d 68 65 61 64 65  f the page-heade
17016 72 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66  r flags field of
17017 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 68 69 6c  .** the new chil
17018 64 20 70 61 67 65 20 64 6f 65 73 20 6e 6f 74 20  d page does not 
17019 6d 61 74 63 68 20 74 68 65 20 66 6c 61 67 73 20  match the flags 
1701a 66 69 65 6c 64 20 6f 66 20 74 68 65 20 70 61 72  field of the par
1701b 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 69 66 20  ent (i.e..** if 
1701c 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 65 20 61  an intkey page a
1701d 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 68 65  ppears to be the
1701e 20 70 61 72 65 6e 74 20 6f 66 20 61 20 6e 6f 6e   parent of a non
1701f 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c 20 6f 72  -intkey page, or
17020 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 61 29 2e  .** vice-versa).
17021 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
17022 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 75 72  oveToChild(BtCur
17023 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6e  sor *pCur, u32 n
17024 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72  ewPgno){.  int r
17025 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 43 75  c;.  int i = pCu
17026 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65 6d 50  r->iPage;.  MemP
17027 61 67 65 20 2a 70 4e 65 77 50 61 67 65 3b 0a 20  age *pNewPage;. 
17028 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
17029 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 61   pCur->pBt;..  a
1702a 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
1702b 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
1702c 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
1702d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
1702e 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72  VALID );.  asser
1702f 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3c 42  t( pCur->iPage<B
17030 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54  TCURSOR_MAX_DEPT
17031 48 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  H );.  if( pCur-
17032 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 52 53 4f  >iPage>=(BTCURSO
17033 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 29 20 29  R_MAX_DEPTH-1) )
17034 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
17035 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
17036 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 74  ;.  }.  rc = get
17037 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c  AndInitPage(pBt,
17038 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65 77 50   newPgno, &pNewP
17039 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
1703a 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 43   return rc;.  pC
1703b 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31 5d 20  ur->apPage[i+1] 
1703c 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20 70 43  = pNewPage;.  pC
1703d 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d 20 3d  ur->aiIdx[i+1] =
1703e 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67   0;.  pCur->iPag
1703f 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e  e++;..  pCur->in
17040 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
17041 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
17042 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 65 77 50  = 0;.  if( pNewP
17043 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 7c 7c 20  age->nCell<1 || 
17044 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 4b 65 79  pNewPage->intKey
17045 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  !=pCur->apPage[i
17046 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  ]->intKey ){.   
17047 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
17048 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
17049 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1704a 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  _OK;.}..#ifndef 
1704b 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 61 67  NDEBUG./*.** Pag
1704c 65 20 70 50 61 72 65 6e 74 20 69 73 20 61 6e 20  e pParent is an 
1704d 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65  internal (non-le
1704e 61 66 29 20 74 72 65 65 20 70 61 67 65 2e 20 54  af) tree page. T
1704f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a  his function .**
17050 20 61 73 73 65 72 74 73 20 74 68 61 74 20 70 61   asserts that pa
17051 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64  ge number iChild
17052 20 69 73 20 74 68 65 20 6c 65 66 74 2d 63 68 69   is the left-chi
17053 6c 64 20 69 66 20 74 68 65 20 69 49 64 78 27 74  ld if the iIdx't
17054 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61 67  h.** cell in pag
17055 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20 69  e pParent. Or, i
17056 66 20 69 49 64 78 20 69 73 20 65 71 75 61 6c 20  f iIdx is equal 
17057 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  to the total num
17058 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20  ber of.** cells 
17059 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 68 61 74  in pParent, that
1705a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68   page number iCh
1705b 69 6c 64 20 69 73 20 74 68 65 20 72 69 67 68 74  ild is the right
1705c 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 68 65  -child of.** the
1705d 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   page..*/.static
1705e 20 76 6f 69 64 20 61 73 73 65 72 74 50 61 72 65   void assertPare
1705f 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 67 65 20  ntIndex(MemPage 
17060 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 69 49  *pParent, int iI
17061 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 6c 64 29  dx, Pgno iChild)
17062 7b 0a 20 20 61 73 73 65 72 74 28 20 69 49 64 78  {.  assert( iIdx
17063 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  <=pParent->nCell
17064 20 29 3b 0a 20 20 69 66 28 20 69 49 64 78 3d 3d   );.  if( iIdx==
17065 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29  pParent->nCell )
17066 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65  {.    assert( ge
17067 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d  t4byte(&pParent-
17068 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e  >aData[pParent->
17069 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 69  hdrOffset+8])==i
1706a 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 6c 73 65  Child );.  }else
1706b 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65  {.    assert( ge
1706c 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28  t4byte(findCell(
1706d 70 50 61 72 65 6e 74 2c 20 69 49 64 78 29 29 3d  pParent, iIdx))=
1706e 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 0a 7d  =iChild );.  }.}
1706f 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  .#else.#  define
17070 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64   assertParentInd
17071 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 6e 64 69  ex(x,y,z) .#endi
17072 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68  f../*.** Move th
17073 65 20 63 75 72 73 6f 72 20 75 70 20 74 6f 20 74  e cursor up to t
17074 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a  he parent page..
17075 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 78 20  **.** pCur->idx 
17076 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 63 65  is set to the ce
17077 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 20 63 6f  ll index that co
17078 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74  ntains the point
17079 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 67  er.** to the pag
1707a 65 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20  e we are coming 
1707b 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 61 72 65  from.  If we are
1707c 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 68 65   coming from the
1707d 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 63  .** right-most c
1707e 68 69 6c 64 20 70 61 67 65 20 74 68 65 6e 20 70  hild page then p
1707f 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20  Cur->idx is set 
17080 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 6e  to one more than
17081 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20  .** the largest 
17082 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f 0a 73  cell index..*/.s
17083 74 61 74 69 63 20 76 6f 69 64 20 6d 6f 76 65 54  tatic void moveT
17084 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73 6f 72  oParent(BtCursor
17085 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72   *pCur){.  asser
17086 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
17087 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61  tex(pCur) );.  a
17088 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
17089 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
1708a 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  D );.  assert( p
1708b 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a  Cur->iPage>0 );.
1708c 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1708d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1708e 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 50  ge] );.  assertP
1708f 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 20 20 20  arentIndex(.    
17090 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
17091 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20  r->iPage-1], .  
17092 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43    pCur->aiIdx[pC
17093 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20  ur->iPage-1], . 
17094 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b     pCur->apPage[
17095 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 67  pCur->iPage]->pg
17096 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c 65 61 73  no.  );.  releas
17097 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61  ePage(pCur->apPa
17098 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  ge[pCur->iPage])
17099 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d  ;.  pCur->iPage-
1709a 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  -;.  pCur->info.
1709b 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75  nSize = 0;.  pCu
1709c 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30  r->validNKey = 0
1709d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  ;.}../*.** Move 
1709e 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 70 6f  the cursor to po
1709f 69 6e 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 20  int to the root 
170a0 70 61 67 65 20 6f 66 20 69 74 73 20 62 2d 74 72  page of its b-tr
170a1 65 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ee structure..**
170a2 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65  .** If the table
170a3 20 68 61 73 20 61 20 76 69 72 74 75 61 6c 20 72   has a virtual r
170a4 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 6e 20 74  oot page, then t
170a5 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d 6f 76  he cursor is mov
170a6 65 64 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74  ed to point.** t
170a7 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 72 6f  o the virtual ro
170a8 6f 74 20 70 61 67 65 20 69 6e 73 74 65 61 64 20  ot page instead 
170a9 6f 66 20 74 68 65 20 61 63 74 75 61 6c 20 72 6f  of the actual ro
170aa 6f 74 20 70 61 67 65 2e 20 41 20 74 61 62 6c 65  ot page. A table
170ab 20 68 61 73 20 61 0a 2a 2a 20 76 69 72 74 75 61   has a.** virtua
170ac 6c 20 72 6f 6f 74 20 70 61 67 65 20 77 68 65 6e  l root page when
170ad 20 74 68 65 20 61 63 74 75 61 6c 20 72 6f 6f 74   the actual root
170ae 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 6e   page contains n
170af 6f 20 63 65 6c 6c 73 20 61 6e 64 20 61 20 0a 2a  o cells and a .*
170b0 2a 20 73 69 6e 67 6c 65 20 63 68 69 6c 64 20 70  * single child p
170b1 61 67 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e  age. This can on
170b2 6c 79 20 68 61 70 70 65 6e 20 77 69 74 68 20 74  ly happen with t
170b3 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20  he table rooted 
170b4 61 74 20 70 61 67 65 20 31 2e 0a 2a 2a 0a 2a 2a  at page 1..**.**
170b5 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 20 73   If the b-tree s
170b6 74 72 75 63 74 75 72 65 20 69 73 20 65 6d 70 74  tructure is empt
170b7 79 2c 20 74 68 65 20 63 75 72 73 6f 72 20 73 74  y, the cursor st
170b8 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 0a 2a  ate is set to .*
170b9 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  * CURSOR_INVALID
170ba 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65  . Otherwise, the
170bb 20 63 75 72 73 6f 72 20 69 73 20 73 65 74 20 74   cursor is set t
170bc 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  o point to the f
170bd 69 72 73 74 0a 2a 2a 20 63 65 6c 6c 20 6c 6f 63  irst.** cell loc
170be 61 74 65 64 20 6f 6e 20 74 68 65 20 72 6f 6f 74  ated on the root
170bf 20 28 6f 72 20 76 69 72 74 75 61 6c 20 72 6f 6f   (or virtual roo
170c0 74 29 20 70 61 67 65 20 61 6e 64 20 74 68 65 20  t) page and the 
170c1 63 75 72 73 6f 72 20 73 74 61 74 65 0a 2a 2a 20  cursor state.** 
170c2 69 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f 52  is set to CURSOR
170c3 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 49 66  _VALID..**.** If
170c4 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
170c5 65 74 75 72 6e 73 20 73 75 63 63 65 73 73 66 75  eturns successfu
170c6 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62 65 20 61  lly, it may be a
170c7 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 0a  ssumed that the.
170c8 2a 2a 20 70 61 67 65 2d 68 65 61 64 65 72 20 66  ** page-header f
170c9 6c 61 67 73 20 69 6e 64 69 63 61 74 65 20 74 68  lags indicate th
170ca 61 74 20 74 68 65 20 5b 76 69 72 74 75 61 6c 5d  at the [virtual]
170cb 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 74 68   root-page is th
170cc 65 20 65 78 70 65 63 74 65 64 20 0a 2a 2a 20 6b  e expected .** k
170cd 69 6e 64 20 6f 66 20 62 2d 74 72 65 65 20 70 61  ind of b-tree pa
170ce 67 65 20 28 69 2e 65 2e 20 69 66 20 77 68 65 6e  ge (i.e. if when
170cf 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 63 75 72   opening the cur
170d0 73 6f 72 20 74 68 65 20 63 61 6c 6c 65 72 20 64  sor the caller d
170d1 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65 63 69 66  id not.** specif
170d2 79 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  y a KeyInfo stru
170d3 63 74 75 72 65 20 74 68 65 20 66 6c 61 67 73 20  cture the flags 
170d4 62 79 74 65 20 69 73 20 73 65 74 20 74 6f 20 30  byte is set to 0
170d5 78 30 35 20 6f 72 20 30 78 30 44 2c 0a 2a 2a 20  x05 or 0x0D,.** 
170d6 69 6e 64 69 63 61 74 69 6e 67 20 61 20 74 61 62  indicating a tab
170d7 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72 20 69 66  le b-tree, or if
170d8 20 74 68 65 20 63 61 6c 6c 65 72 20 64 69 64 20   the caller did 
170d9 73 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e 66  specify a KeyInf
170da 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  o .** structure 
170db 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 69  the flags byte i
170dc 73 20 73 65 74 20 74 6f 20 30 78 30 32 20 6f 72  s set to 0x02 or
170dd 20 30 78 30 41 2c 20 69 6e 64 69 63 61 74 69 6e   0x0A, indicatin
170de 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 62 2d  g an index.** b-
170df 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  tree)..*/.static
170e0 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f 74 28   int moveToRoot(
170e1 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
170e2 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f 6f  .  MemPage *pRoo
170e3 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  t;.  int rc = SQ
170e4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72 65 65  LITE_OK;.  Btree
170e5 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72   *p = pCur->pBtr
170e6 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ee;.  BtShared *
170e7 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20  pBt = p->pBt;.. 
170e8 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
170e9 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
170ea 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 52  );.  assert( CUR
170eb 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20 43 55  SOR_INVALID < CU
170ec 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
170ed 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55   );.  assert( CU
170ee 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c 20 43  RSOR_VALID   < C
170ef 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
170f0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43  K );.  assert( C
170f1 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 3e 20  URSOR_FAULT   > 
170f2 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
170f3 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72  EK );.  if( pCur
170f4 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52  ->eState>=CURSOR
170f5 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 7b 0a  _REQUIRESEEK ){.
170f6 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53      if( pCur->eS
170f7 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55  tate==CURSOR_FAU
170f8 4c 54 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  LT ){.      asse
170f9 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65  rt( pCur->skipNe
170fa 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  xt!=SQLITE_OK );
170fb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 43  .      return pC
170fc 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20  ur->skipNext;.  
170fd 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42    }.    sqlite3B
170fe 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28  treeClearCursor(
170ff 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  pCur);.  }..  if
17100 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30  ( pCur->iPage>=0
17101 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
17102 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70     for(i=1; i<=p
17103 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29  Cur->iPage; i++)
17104 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  {.      releaseP
17105 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65  age(pCur->apPage
17106 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  [i]);.    }.    
17107 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 30 3b  pCur->iPage = 0;
17108 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
17109 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67   = getAndInitPag
1710a 65 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e  e(pBt, pCur->pgn
1710b 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70  oRoot, &pCur->ap
1710c 50 61 67 65 5b 30 5d 29 3b 0a 20 20 20 20 69 66  Page[0]);.    if
1710d 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1710e 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 65  ){.      pCur->e
1710f 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49  State = CURSOR_I
17110 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 72 65  NVALID;.      re
17111 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
17112 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d     pCur->iPage =
17113 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70   0;..    /* If p
17114 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 69 73  Cur->pKeyInfo is
17115 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
17116 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20  the caller that 
17117 6f 70 65 6e 65 64 20 74 68 69 73 20 63 75 72 73  opened this curs
17118 6f 72 0a 20 20 20 20 2a 2a 20 65 78 70 65 63 74  or.    ** expect
17119 65 64 20 74 6f 20 6f 70 65 6e 20 69 74 20 6f 6e  ed to open it on
1711a 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65   an index b-tree
1711b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
1711c 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20 20 20 20  pKeyInfo is.    
1711d 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 63 61 6c  ** NULL, the cal
1711e 6c 65 72 20 65 78 70 65 63 74 73 20 61 20 74 61  ler expects a ta
1711f 62 6c 65 20 62 2d 74 72 65 65 2e 20 49 66 20 74  ble b-tree. If t
17120 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65 20 63  his is not the c
17121 61 73 65 2c 0a 20 20 20 20 2a 2a 20 72 65 74 75  ase,.    ** retu
17122 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f 52  rn an SQLITE_COR
17123 52 55 50 54 20 65 72 72 6f 72 2e 20 20 2a 2f 0a  RUPT error.  */.
17124 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
17125 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74  ->apPage[0]->int
17126 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 75 72 2d 3e  Key==1 || pCur->
17127 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65  apPage[0]->intKe
17128 79 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  y==0 );.    if( 
17129 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d  (pCur->pKeyInfo=
1712a 3d 30 29 21 3d 70 43 75 72 2d 3e 61 70 50 61 67  =0)!=pCur->apPag
1712b 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a  e[0]->intKey ){.
1712c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1712d 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1712e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
1712f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68  * Assert that th
17130 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 6f  e root page is o
17131 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 74 79  f the correct ty
17132 70 65 2e 20 54 68 69 73 20 6d 75 73 74 20 62 65  pe. This must be
17133 20 74 68 65 0a 20 20 2a 2a 20 63 61 73 65 20 61   the.  ** case a
17134 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68  s the call to th
17135 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  is function that
17136 20 6c 6f 61 64 65 64 20 74 68 65 20 72 6f 6f 74   loaded the root
17137 2d 70 61 67 65 20 28 65 69 74 68 65 72 0a 20 20  -page (either.  
17138 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 6f 72 20  ** this call or 
17139 61 20 70 72 65 76 69 6f 75 73 20 69 6e 76 6f 63  a previous invoc
1713a 61 74 69 6f 6e 29 20 77 6f 75 6c 64 20 68 61 76  ation) would hav
1713b 65 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 75  e detected corru
1713c 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 69 66 20 74  ption .  ** if t
1713d 68 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 77 65  he assumption we
1713e 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e 64  re not true, and
1713f 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   it is not possi
17140 62 6c 65 20 66 6f 72 20 74 68 65 20 66 6c 61 67  ble for the flag
17141 73 20 0a 20 20 2a 2a 20 62 79 74 65 20 74 6f 20  s .  ** byte to 
17142 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69  have been modifi
17143 65 64 20 77 68 69 6c 65 20 74 68 69 73 20 63 75  ed while this cu
17144 72 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67 20  rsor is holding 
17145 61 20 72 65 66 65 72 65 6e 63 65 0a 20 20 2a 2a  a reference.  **
17146 20 74 6f 20 74 68 65 20 70 61 67 65 2e 20 20 2a   to the page.  *
17147 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 75 72  /.  pRoot = pCur
17148 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 61  ->apPage[0];.  a
17149 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67  ssert( pRoot->pg
1714a 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f  no==pCur->pgnoRo
1714b 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ot );.  assert( 
1714c 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 20 26 26  pRoot->isInit &&
1714d 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f   (pCur->pKeyInfo
1714e 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e 69 6e 74  ==0)==pRoot->int
1714f 4b 65 79 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e  Key );..  pCur->
17150 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20  aiIdx[0] = 0;.  
17151 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
17152 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 61 74   = 0;.  pCur->at
17153 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 43 75 72  Last = 0;.  pCur
17154 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b  ->validNKey = 0;
17155 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e  ..  if( pRoot->n
17156 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70 52 6f 6f  Cell==0 && !pRoo
17157 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50  t->leaf ){.    P
17158 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a 20 20 20  gno subpage;.   
17159 20 69 66 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f   if( pRoot->pgno
1715a 21 3d 31 20 29 20 72 65 74 75 72 6e 20 53 51 4c  !=1 ) return SQL
1715b 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1715c 3b 0a 20 20 20 20 73 75 62 70 61 67 65 20 3d 20  ;.    subpage = 
1715d 67 65 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d  get4byte(&pRoot-
1715e 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64  >aData[pRoot->hd
1715f 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20  rOffset+8]);.   
17160 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
17161 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20  CURSOR_VALID;.  
17162 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69    rc = moveToChi
17163 6c 64 28 70 43 75 72 2c 20 73 75 62 70 61 67 65  ld(pCur, subpage
17164 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
17165 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 28  pCur->eState = (
17166 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30 29  (pRoot->nCell>0)
17167 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 43 55  ?CURSOR_VALID:CU
17168 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b 0a 20  RSOR_INVALID);. 
17169 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1716a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68  }../*.** Move th
1716b 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f  e cursor down to
1716c 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c   the left-most l
1716d 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74  eaf entry beneat
1716e 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 20 74  h the.** entry t
1716f 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63 75  o which it is cu
17170 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
17171 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 74  ..**.** The left
17172 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 74 68  -most leaf is th
17173 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 73  e one with the s
17174 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 74 68  mallest key - th
17175 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 61 73  e first.** in as
17176 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a  cending order..*
17177 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76  /.static int mov
17178 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 43 75  eToLeftmost(BtCu
17179 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50  rsor *pCur){.  P
1717a 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20  gno pgno;.  int 
1717b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1717c 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
1717d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
1717e 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
1717f 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
17180 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
17181 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
17182 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49   while( rc==SQLI
17183 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65  TE_OK && !(pPage
17184 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
17185 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c  pCur->iPage])->l
17186 65 61 66 20 29 7b 0a 20 20 20 20 61 73 73 65 72  eaf ){.    asser
17187 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  t( pCur->aiIdx[p
17188 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67  Cur->iPage]<pPag
17189 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20  e->nCell );.    
1718a 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
1718b 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
1718c 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
1718d 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20 20 20  ->iPage]));.    
1718e 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64  rc = moveToChild
1718f 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20  (pCur, pgno);.  
17190 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
17191 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65  ../*.** Move the
17192 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20   cursor down to 
17193 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c  the right-most l
17194 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74  eaf entry beneat
17195 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f  h the.** page to
17196 20 77 68 69 63 68 20 69 74 20 69 73 20 63 75 72   which it is cur
17197 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e  rently pointing.
17198 20 20 4e 6f 74 69 63 65 20 74 68 65 20 64 69 66    Notice the dif
17199 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74 77 65  ference.** betwe
1719a 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73  en moveToLeftmos
1719b 74 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f 52 69  t() and moveToRi
1719c 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f 76 65  ghtmost().  move
1719d 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a 2a 20  ToLeftmost().** 
1719e 66 69 6e 64 73 20 74 68 65 20 6c 65 66 74 2d 6d  finds the left-m
1719f 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74  ost entry beneat
171a0 68 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 77 68  h the *entry* wh
171a1 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 67 68  ereas moveToRigh
171a2 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73  tmost().** finds
171a3 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
171a4 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68  entry beneath th
171a5 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20  e *page*..**.** 
171a6 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65  The right-most e
171a7 6e 74 72 79 20 69 73 20 74 68 65 20 6f 6e 65 20  ntry is the one 
171a8 77 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 74  with the largest
171a9 20 6b 65 79 20 2d 20 74 68 65 20 6c 61 73 74 0a   key - the last.
171aa 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63 65 6e 64  ** key in ascend
171ab 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74  ing order..*/.st
171ac 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52  atic int moveToR
171ad 69 67 68 74 6d 6f 73 74 28 42 74 43 75 72 73 6f  ightmost(BtCurso
171ae 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f  r *pCur){.  Pgno
171af 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20   pgno;.  int rc 
171b0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d  = SQLITE_OK;.  M
171b1 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
171b2 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  0;..  assert( cu
171b3 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
171b4 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
171b5 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
171b6 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
171b7 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c    while( rc==SQL
171b8 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67  ITE_OK && !(pPag
171b9 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
171ba 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e  [pCur->iPage])->
171bb 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f  leaf ){.    pgno
171bc 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61   = get4byte(&pPa
171bd 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d  ge->aData[pPage-
171be 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a  >hdrOffset+8]);.
171bf 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b      pCur->aiIdx[
171c0 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70  pCur->iPage] = p
171c1 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20  Page->nCell;.   
171c2 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c   rc = moveToChil
171c3 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20  d(pCur, pgno);. 
171c4 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
171c5 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43  ITE_OK ){.    pC
171c6 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
171c7 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e  iPage] = pPage->
171c8 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 43 75  nCell-1;.    pCu
171c9 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20  r->info.nSize = 
171ca 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c  0;.    pCur->val
171cb 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a  idNKey = 0;.  }.
171cc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
171cd 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73  /* Move the curs
171ce 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  or to the first 
171cf 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62  entry in the tab
171d0 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  le.  Return SQLI
171d1 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63  TE_OK.** on succ
171d2 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 20  ess.  Set *pRes 
171d3 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 73  to 0 if the curs
171d4 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e  or actually poin
171d5 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a  ts to something.
171d6 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 20  ** or set *pRes 
171d7 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 6c  to 1 if the tabl
171d8 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53  e is empty..*/.S
171d9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
171da 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69  t sqlite3BtreeFi
171db 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  rst(BtCursor *pC
171dc 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a  ur, int *pRes){.
171dd 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
171de 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
171df 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
171e0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
171e1 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72  _mutex_held(pCur
171e2 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75  ->pBtree->db->mu
171e3 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d  tex) );.  rc = m
171e4 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b  oveToRoot(pCur);
171e5 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
171e6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
171e7 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
171e8 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a  RSOR_INVALID ){.
171e9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43        assert( pC
171ea 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
171eb 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d  >iPage]->nCell==
171ec 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73  0 );.      *pRes
171ed 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 1;.      rc =
171ee 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
171ef 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
171f0 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
171f1 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
171f2 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 20  nCell>0 );.     
171f3 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20   *pRes = 0;.    
171f4 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66    rc = moveToLef
171f5 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20  tmost(pCur);.   
171f6 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
171f7 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74  rc;.}../* Move t
171f8 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
171f9 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74   last entry in t
171fa 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72  he table.  Retur
171fb 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f  n SQLITE_OK.** o
171fc 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 74 20  n success.  Set 
171fd 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 74 68  *pRes to 0 if th
171fe 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c  e cursor actuall
171ff 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65  y points to some
17200 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20  thing.** or set 
17201 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20 74 68  *pRes to 1 if th
17202 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79  e table is empty
17203 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
17204 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
17205 74 72 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f  treeLast(BtCurso
17206 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52  r *pCur, int *pR
17207 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  es){.  int rc;. 
17208 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
17209 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
1720a 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
1720b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1720c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e  d(pCur->pBtree->
1720d 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
1720e 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f   /* If the curso
1720f 72 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 73  r already points
17210 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74   to the last ent
17211 72 79 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f  ry, this is a no
17212 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 43 55  -op. */.  if( CU
17213 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72  RSOR_VALID==pCur
17214 2d 3e 65 53 74 61 74 65 20 26 26 20 70 43 75 72  ->eState && pCur
17215 2d 3e 61 74 4c 61 73 74 20 29 7b 0a 23 69 66 64  ->atLast ){.#ifd
17216 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
17217 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63      /* This bloc
17218 6b 20 73 65 72 76 65 73 20 74 6f 20 61 73 73 65  k serves to asse
17219 72 74 28 29 20 74 68 61 74 20 74 68 65 20 63 75  rt() that the cu
1721a 72 73 6f 72 20 72 65 61 6c 6c 79 20 64 6f 65 73  rsor really does
1721b 20 70 6f 69 6e 74 20 0a 20 20 20 20 2a 2a 20 74   point .    ** t
1721c 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  o the last entry
1721d 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 2e 20   in the b-tree. 
1721e 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20  */.    int ii;. 
1721f 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c     for(ii=0; ii<
17220 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b  pCur->iPage; ii+
17221 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  +){.      assert
17222 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 69  ( pCur->aiIdx[ii
17223 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  ]==pCur->apPage[
17224 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  ii]->nCell );.  
17225 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
17226 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
17227 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75 72 2d 3e  ->iPage]==pCur->
17228 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
17229 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a  ge]->nCell-1 );.
1722a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
1722b 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
1722c 50 61 67 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a 23  Page]->leaf );.#
1722d 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e  endif.    return
1722e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
1722f 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f  .  rc = moveToRo
17230 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20  ot(pCur);.  if( 
17231 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
17232 0a 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f  .    if( CURSOR_
17233 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65  INVALID==pCur->e
17234 53 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61  State ){.      a
17235 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
17236 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
17237 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20  ->nCell==0 );.  
17238 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20      *pRes = 1;. 
17239 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1723a 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53  assert( pCur->eS
1723b 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
1723c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65  ID );.      *pRe
1723d 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20  s = 0;.      rc 
1723e 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73  = moveToRightmos
1723f 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70  t(pCur);.      p
17240 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 72 63  Cur->atLast = rc
17241 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 31 3a 30  ==SQLITE_OK ?1:0
17242 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
17243 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d  turn rc;.}../* M
17244 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 73  ove the cursor s
17245 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73  o that it points
17246 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65 61   to an entry nea
17247 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a 20 73 70  r the key .** sp
17248 65 63 69 66 69 65 64 20 62 79 20 70 49 64 78 4b  ecified by pIdxK
17249 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20 20  ey or intKey.   
1724a 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73  Return a success
1724b 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72   code..**.** For
1724c 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20   INTKEY tables, 
1724d 74 68 65 20 69 6e 74 4b 65 79 20 70 61 72 61 6d  the intKey param
1724e 65 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 70  eter is used.  p
1724f 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 74 20  IdxKey .** must 
17250 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 6e  be NULL.  For in
17251 64 65 78 20 74 61 62 6c 65 73 2c 20 70 49 64 78  dex tables, pIdx
17252 4b 65 79 20 69 73 20 75 73 65 64 20 61 6e 64 20  Key is used and 
17253 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 69 67 6e  intKey.** is ign
17254 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ored..**.** If a
17255 6e 20 65 78 61 63 74 20 6d 61 74 63 68 20 69 73  n exact match is
17256 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 68 65 6e   not found, then
17257 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 61   the cursor is a
17258 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 20 70 6f  lways.** left po
17259 69 6e 74 69 6e 67 20 61 74 20 61 20 6c 65 61 66  inting at a leaf
1725a 20 70 61 67 65 20 77 68 69 63 68 20 77 6f 75 6c   page which woul
1725b 64 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 72 79  d hold the entry
1725c 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 65 20 70   if it.** were p
1725d 72 65 73 65 6e 74 2e 20 20 54 68 65 20 63 75 72  resent.  The cur
1725e 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 6e 74 20  sor might point 
1725f 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74  to an entry that
17260 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 65   comes.** before
17261 20 6f 72 20 61 66 74 65 72 20 74 68 65 20 6b 65   or after the ke
17262 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65  y..**.** An inte
17263 67 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 69  ger is written i
17264 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 63 68 20  nto *pRes which 
17265 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  is the result of
17266 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 20 74 68  .** comparing th
17267 65 20 6b 65 79 20 77 69 74 68 20 74 68 65 20 65  e key with the e
17268 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68  ntry to which th
17269 65 20 63 75 72 73 6f 72 20 69 73 20 0a 2a 2a 20  e cursor is .** 
1726a 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 65 20 6d  pointing.  The m
1726b 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 20 69 6e  eaning of the in
1726c 74 65 67 65 72 20 77 72 69 74 74 65 6e 20 69 6e  teger written in
1726d 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 73 20 61  to.** *pRes is a
1726e 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
1726f 20 20 20 20 20 2a 70 52 65 73 3c 30 20 20 20 20       *pRes<0    
17270 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20    The cursor is 
17271 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
17272 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a   an entry that.*
17273 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
17274 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68     is smaller th
17275 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65  an intKey/pIdxKe
17276 79 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c  y or if the tabl
17277 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20  e is empty.**   
17278 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
17279 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  nd the cursor is
1727a 20 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74 20   therefore left 
1727b 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67  point to nothing
1727c 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65  ..**.**     *pRe
1727d 73 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75 72  s==0     The cur
1727e 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  sor is left poin
1727f 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79  ting at an entry
17280 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
17281 20 20 20 20 20 20 20 20 20 20 65 78 61 63 74 6c            exactl
17282 79 20 6d 61 74 63 68 65 73 20 69 6e 74 4b 65 79  y matches intKey
17283 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20  /pIdxKey..**.** 
17284 20 20 20 20 2a 70 52 65 73 3e 30 20 20 20 20 20      *pRes>0     
17285 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c   The cursor is l
17286 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  eft pointing at 
17287 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a  an entry that.**
17288 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17289 20 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e    is larger than
1728a 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e   intKey/pIdxKey.
1728b 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  .**.*/.SQLITE_PR
1728c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1728d 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61  3BtreeMovetoUnpa
1728e 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f 72  cked(.  BtCursor
1728f 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20   *pCur,         
17290 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74   /* The cursor t
17291 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20 20  o be moved */.  
17292 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
17293 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e 70 61  pIdxKey, /* Unpa
17294 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a  cked index key *
17295 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c 20  /.  i64 intKey, 
17296 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17297 54 68 65 20 74 61 62 6c 65 20 6b 65 79 20 2a 2f  The table key */
17298 0a 20 20 69 6e 74 20 62 69 61 73 52 69 67 68 74  .  int biasRight
17299 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ,           /* I
1729a 66 20 74 72 75 65 2c 20 62 69 61 73 20 74 68 65  f true, bias the
1729b 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 68   search to the h
1729c 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74  igh end */.  int
1729d 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20   *pRes          
1729e 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73        /* Write s
1729f 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65  earch results he
172a0 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  re */.){.  int r
172a1 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  c;..  assert( cu
172a2 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
172a3 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
172a4 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
172a5 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65  held(pCur->pBtre
172a6 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  e->db->mutex) );
172a7 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 73 20  .  assert( pRes 
172a8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 49  );.  assert( (pI
172a9 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 43 75 72  dxKey==0)==(pCur
172aa 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 20 29  ->pKeyInfo==0) )
172ab 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63  ;..  /* If the c
172ac 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79  ursor is already
172ad 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 74 20 74   positioned at t
172ae 68 65 20 70 6f 69 6e 74 20 77 65 20 61 72 65 20  he point we are 
172af 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 6d  trying.  ** to m
172b0 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a 75 73  ove to, then jus
172b1 74 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74  t return without
172b2 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 20   doing any work 
172b3 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65  */.  if( pCur->e
172b4 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
172b5 4c 49 44 20 26 26 20 70 43 75 72 2d 3e 76 61 6c  LID && pCur->val
172b6 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 20 70 43  idNKey .   && pC
172b7 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69  ur->apPage[0]->i
172b8 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 20 20 20  ntKey .  ){.    
172b9 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  if( pCur->info.n
172ba 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20  Key==intKey ){. 
172bb 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a       *pRes = 0;.
172bc 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
172bd 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
172be 20 20 69 66 28 20 70 43 75 72 2d 3e 61 74 4c 61    if( pCur->atLa
172bf 73 74 20 26 26 20 70 43 75 72 2d 3e 69 6e 66 6f  st && pCur->info
172c0 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a  .nKey<intKey ){.
172c1 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31        *pRes = -1
172c2 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
172c3 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
172c4 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65    }..  rc = move
172c5 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20  ToRoot(pCur);.  
172c6 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
172c7 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61  turn rc;.  }.  a
172c8 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
172c9 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
172ca 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
172cb 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
172cc 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e 69 74 20  >iPage]->isInit 
172cd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
172ce 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
172cf 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20  iPage]->nCell>0 
172d0 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  || pCur->eState=
172d1 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
172d2 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65  );.  if( pCur->e
172d3 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
172d4 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 52  VALID ){.    *pR
172d5 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73  es = -1;.    ass
172d6 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
172d7 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
172d8 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  nCell==0 );.    
172d9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
172da 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
172db 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d  pCur->apPage[0]-
172dc 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78 4b  >intKey || pIdxK
172dd 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b  ey );.  for(;;){
172de 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20 75 70  .    int lwr, up
172df 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68 6c 64  r;.    Pgno chld
172e0 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20  Pg;.    MemPage 
172e1 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61  *pPage = pCur->a
172e2 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
172e3 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63 3b 0a 0a  e];.    int c;..
172e4 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e 6e 43      /* pPage->nC
172e5 65 6c 6c 20 6d 75 73 74 20 62 65 20 67 72 65 61  ell must be grea
172e6 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e 20 49  ter than zero. I
172e7 66 20 74 68 69 73 20 69 73 20 74 68 65 20 72 6f  f this is the ro
172e8 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a 20 74  ot-page.    ** t
172e9 68 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 20  he cursor would 
172ea 68 61 76 65 20 62 65 65 6e 20 49 4e 56 41 4c 49  have been INVALI
172eb 44 20 61 62 6f 76 65 20 61 6e 64 20 74 68 69 73  D above and this
172ec 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 0a 20 20   for(;;) loop.  
172ed 20 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e 20 49 66    ** not run. If
172ee 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65   this is not the
172ef 20 72 6f 6f 74 2d 70 61 67 65 2c 20 74 68 65 6e   root-page, then
172f0 20 74 68 65 20 6d 6f 76 65 54 6f 43 68 69 6c 64   the moveToChild
172f1 28 29 20 72 6f 75 74 69 6e 65 0a 20 20 20 20 2a  () routine.    *
172f2 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 61 6c 72  * would have alr
172f3 65 61 64 79 20 64 65 74 65 63 74 65 64 20 64 62  eady detected db
172f4 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 53 69 6d   corruption. Sim
172f5 69 6c 61 72 6c 79 2c 20 70 50 61 67 65 20 6d 75  ilarly, pPage mu
172f6 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 74 68 65  st.    ** be the
172f7 20 72 69 67 68 74 20 6b 69 6e 64 20 28 69 6e 64   right kind (ind
172f8 65 78 20 6f 72 20 74 61 62 6c 65 29 20 6f 66 20  ex or table) of 
172f9 62 2d 74 72 65 65 20 70 61 67 65 2e 20 4f 74 68  b-tree page. Oth
172fa 65 72 77 69 73 65 0a 20 20 20 20 2a 2a 20 61 20  erwise.    ** a 
172fb 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20 6f 72  moveToChild() or
172fc 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20 63 61   moveToRoot() ca
172fd 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65 20 64 65  ll would have de
172fe 74 65 63 74 65 64 20 63 6f 72 72 75 70 74 69 6f  tected corruptio
172ff 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  n.  */.    asser
17300 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e  t( pPage->nCell>
17301 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
17302 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3d 3d   pPage->intKey==
17303 28 70 49 64 78 4b 65 79 3d 3d 30 29 20 29 3b 0a  (pIdxKey==0) );.
17304 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a 20 20 20      lwr = 0;.   
17305 20 75 70 72 20 3d 20 70 50 61 67 65 2d 3e 6e 43   upr = pPage->nC
17306 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 62  ell-1;.    if( b
17307 69 61 73 52 69 67 68 74 20 29 7b 0a 20 20 20 20  iasRight ){.    
17308 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43    pCur->aiIdx[pC
17309 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31  ur->iPage] = (u1
1730a 36 29 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73 65  6)upr;.    }else
1730b 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69  {.      pCur->ai
1730c 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
1730d 20 3d 20 28 75 31 36 29 28 28 75 70 72 2b 6c 77   = (u16)((upr+lw
1730e 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20  r)/2);.    }.   
1730f 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20   for(;;){.      
17310 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d 3e  int idx = pCur->
17311 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
17312 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20  e]; /* Index of 
17313 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e 20  current cell in 
17314 70 50 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 75  pPage */.      u
17315 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20  8 *pCell;       
17316 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17317 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
17318 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e   current cell in
17319 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 20 20 20   pPage */..     
1731a 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
1731b 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 43 65  e = 0;.      pCe
1731c 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
1731d 61 67 65 2c 20 69 64 78 29 20 2b 20 70 50 61 67  age, idx) + pPag
1731e 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b  e->childPtrSize;
1731f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65  .      if( pPage
17320 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20  ->intKey ){.    
17321 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79      i64 nCellKey
17322 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  ;.        if( pP
17323 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a  age->hasData ){.
17324 20 20 20 20 20 20 20 20 20 20 75 33 32 20 64 75            u32 du
17325 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 20 20 70  mmy;.          p
17326 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61 72 69 6e  Cell += getVarin
17327 74 33 32 28 70 43 65 6c 6c 2c 20 64 75 6d 6d 79  t32(pCell, dummy
17328 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
17329 20 20 20 20 20 67 65 74 56 61 72 69 6e 74 28 70       getVarint(p
1732a 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 6e 43 65  Cell, (u64*)&nCe
1732b 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20  llKey);.        
1732c 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d 3d 69 6e  if( nCellKey==in
1732d 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  tKey ){.        
1732e 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20    c = 0;.       
1732f 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 65 6c 6c   }else if( nCell
17330 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 20  Key<intKey ){.  
17331 20 20 20 20 20 20 20 20 63 20 3d 20 2d 31 3b 0a          c = -1;.
17332 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
17333 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
17334 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74 4b 65 79   nCellKey>intKey
17335 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20   );.          c 
17336 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a  = +1;.        }.
17337 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 76 61          pCur->va
17338 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20  lidNKey = 1;.   
17339 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e       pCur->info.
1733a 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 79 3b  nKey = nCellKey;
1733b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1733c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78        /* The max
1733d 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20 70  imum supported p
1733e 61 67 65 2d 73 69 7a 65 20 69 73 20 33 32 37 36  age-size is 3276
1733f 38 20 62 79 74 65 73 2e 20 54 68 69 73 20 6d 65  8 bytes. This me
17340 61 6e 73 20 74 68 61 74 0a 20 20 20 20 20 20 20  ans that.       
17341 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   ** the maximum 
17342 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64  number of record
17343 20 62 79 74 65 73 20 73 74 6f 72 65 64 20 6f 6e   bytes stored on
17344 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 72 65 65   an index B-Tree
17345 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65  .        ** page
17346 20 69 73 20 61 74 20 6d 6f 73 74 20 38 31 39 38   is at most 8198
17347 20 62 79 74 65 73 2c 20 77 68 69 63 68 20 6d 61   bytes, which ma
17348 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 61  y be stored as a
17349 20 32 2d 62 79 74 65 0a 20 20 20 20 20 20 20 20   2-byte.        
1734a 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 69 73 20  ** varint. This 
1734b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75  information is u
1734c 73 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20 74  sed to attempt t
1734d 6f 20 61 76 6f 69 64 20 70 61 72 73 69 6e 67 20  o avoid parsing 
1734e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
1734f 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 79 20 63  entire cell by c
17350 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 68 65 20  hecking for the 
17351 63 61 73 65 73 20 77 68 65 72 65 20 74 68 65 20  cases where the 
17352 72 65 63 6f 72 64 20 69 73 20 0a 20 20 20 20 20  record is .     
17353 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 65 6e 74     ** stored ent
17354 69 72 65 6c 79 20 77 69 74 68 69 6e 20 74 68 65  irely within the
17355 20 62 2d 74 72 65 65 20 70 61 67 65 20 62 79 20   b-tree page by 
17356 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65 20 66  inspecting the f
17357 69 72 73 74 20 0a 20 20 20 20 20 20 20 20 2a 2a  irst .        **
17358 20 32 20 62 79 74 65 73 20 6f 66 20 74 68 65 20   2 bytes of the 
17359 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f  cell..        */
1735a 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 43 65  .        int nCe
1735b 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d 3b 0a 20  ll = pCell[0];. 
1735c 20 20 20 20 20 20 20 69 66 28 20 21 28 6e 43 65         if( !(nCe
1735d 6c 6c 20 26 20 30 78 38 30 29 20 26 26 20 6e 43  ll & 0x80) && nC
1735e 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c  ell<=pPage->maxL
1735f 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  ocal ){.        
17360 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68    /* This branch
17361 20 72 75 6e 73 20 69 66 20 74 68 65 20 72 65 63   runs if the rec
17362 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f  ord-size field o
17363 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 61 0a  f the cell is a.
17364 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69 6e            ** sin
17365 67 6c 65 20 62 79 74 65 20 76 61 72 69 6e 74 20  gle byte varint 
17366 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 66  and the record f
17367 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20  its entirely on 
17368 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 20 20 20  the main.       
17369 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 70 61 67     ** b-tree pag
1736a 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  e.  */.         
1736b 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65   c = sqlite3Vdbe
1736c 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43  RecordCompare(nC
1736d 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 65  ell, (void*)&pCe
1736e 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65 79 29 3b  ll[1], pIdxKey);
1736f 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
17370 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d 20 26 20  f( !(pCell[1] & 
17371 30 78 38 30 29 20 0a 20 20 20 20 20 20 20 20 20  0x80) .         
17372 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20 28 28 6e   && (nCell = ((n
17373 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37 29 20 2b  Cell&0x7f)<<7) +
17374 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 50 61 67   pCell[1])<=pPag
17375 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 20 20 20  e->maxLocal.    
17376 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
17377 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 2d 73   /* The record-s
17378 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 20 32  ize field is a 2
17379 20 62 79 74 65 20 76 61 72 69 6e 74 20 61 6e 64   byte varint and
1737a 20 74 68 65 20 72 65 63 6f 72 64 20 0a 20 20 20   the record .   
1737b 20 20 20 20 20 20 20 2a 2a 20 66 69 74 73 20 65         ** fits e
1737c 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 65 20 6d  ntirely on the m
1737d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67 65 2e  ain b-tree page.
1737e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63    */.          c
1737f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
17380 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c  cordCompare(nCel
17381 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 65 6c 6c  l, (void*)&pCell
17382 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20  [2], pIdxKey);. 
17383 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
17384 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
17385 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f 76 65 72  ecord flows over
17386 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72   onto one or mor
17387 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  e overflow pages
17388 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 20 20 2a  . In.          *
17389 2a 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  * this case the 
1738a 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 65 64 73  whole cell needs
1738b 20 74 6f 20 62 65 20 70 61 72 73 65 64 2c 20 61   to be parsed, a
1738c 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65   buffer allocate
1738d 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  d.          ** a
1738e 6e 64 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64  nd accessPayload
1738f 28 29 20 75 73 65 64 20 74 6f 20 72 65 74 72 69  () used to retri
17390 65 76 65 20 74 68 65 20 72 65 63 6f 72 64 20 69  eve the record i
17391 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20  nto the.        
17392 20 20 2a 2a 20 62 75 66 66 65 72 20 62 65 66 6f    ** buffer befo
17393 72 65 20 56 64 62 65 52 65 63 6f 72 64 43 6f 6d  re VdbeRecordCom
17394 70 61 72 65 28 29 20 63 61 6e 20 62 65 20 63 61  pare() can be ca
17395 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20  lled. */.       
17396 20 20 20 76 6f 69 64 20 2a 70 43 65 6c 6c 4b 65     void *pCellKe
17397 79 3b 0a 20 20 20 20 20 20 20 20 20 20 75 38 20  y;.          u8 
17398 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c 42 6f 64  * const pCellBod
17399 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 50 61 67  y = pCell - pPag
1739a 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b  e->childPtrSize;
1739b 0a 20 20 20 20 20 20 20 20 20 20 62 74 72 65 65  .          btree
1739c 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61  ParseCellPtr(pPa
1739d 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 2c 20 26  ge, pCellBody, &
1739e 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20  pCur->info);.   
1739f 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20 28         nCell = (
173a0 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  int)pCur->info.n
173a1 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 70  Key;.          p
173a2 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74 65  CellKey = sqlite
173a3 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 20 29  3Malloc( nCell )
173a4 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
173a5 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a 20  pCellKey==0 ){. 
173a6 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
173a7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
173a8 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d            goto m
173a9 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20  oveto_finish;.  
173aa 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
173ab 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73 50      rc = accessP
173ac 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 30 2c 20  ayload(pCur, 0, 
173ad 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67 6e 65 64  nCell, (unsigned
173ae 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b 65 79 2c   char*)pCellKey,
173af 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69   0);.          i
173b0 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
173b1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
173b2 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20  e(pCellKey);.   
173b3 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f           goto mo
173b4 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20  veto_finish;.   
173b5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
173b6 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64     c = sqlite3Vd
173b7 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28  beRecordCompare(
173b8 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65 79 2c  nCell, pCellKey,
173b9 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20   pIdxKey);.     
173ba 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
173bb 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20  e(pCellKey);.   
173bc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
173bd 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b       if( c==0 ){
173be 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
173bf 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70  ge->intKey && !p
173c0 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
173c1 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64          lwr = id
173c2 78 3b 0a 20 20 20 20 20 20 20 20 20 20 75 70 72  x;.          upr
173c3 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 20 20 20   = lwr - 1;.    
173c4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
173c5 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
173c6 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b        *pRes = 0;
173c7 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
173c8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
173c9 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f       goto moveto
173ca 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20  _finish;.       
173cb 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
173cc 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20   if( c<0 ){.    
173cd 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b      lwr = idx+1;
173ce 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
173cf 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d        upr = idx-
173d0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
173d1 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 0a   if( lwr>upr ){.
173d2 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
173d3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75       }.      pCu
173d4 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
173d5 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28 6c  Page] = (u16)((l
173d6 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20 20 20  wr+upr)/2);.    
173d7 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 77  }.    assert( lw
173d8 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 20 20  r==upr+1 );.    
173d9 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
173da 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 28  sInit );.    if(
173db 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a   pPage->leaf ){.
173dc 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 30        chldPg = 0
173dd 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
173de 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  lwr>=pPage->nCel
173df 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50  l ){.      chldP
173e0 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  g = get4byte(&pP
173e1 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
173e2 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
173e3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
173e4 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 62    chldPg = get4b
173e5 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61  yte(findCell(pPa
173e6 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20 7d  ge, lwr));.    }
173e7 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67 3d  .    if( chldPg=
173e8 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
173e9 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
173ea 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75  pCur->iPage]<pCu
173eb 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
173ec 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b  iPage]->nCell );
173ed 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 63  .      *pRes = c
173ee 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
173ef 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 67 6f  ITE_OK;.      go
173f0 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68  to moveto_finish
173f1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72  ;.    }.    pCur
173f2 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
173f3 61 67 65 5d 20 3d 20 28 75 31 36 29 6c 77 72 3b  age] = (u16)lwr;
173f4 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  .    pCur->info.
173f5 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70  nSize = 0;.    p
173f6 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
173f7 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76   0;.    rc = mov
173f8 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 63  eToChild(pCur, c
173f9 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 66 28 20  hldPg);.    if( 
173fa 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 65 74 6f  rc ) goto moveto
173fb 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 6d 6f 76  _finish;.  }.mov
173fc 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 72 65  eto_finish:.  re
173fd 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
173fe 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
173ff 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
17400 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  not pointing at 
17401 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20  an entry of the 
17402 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52 55  table..**.** TRU
17403 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e  E will be return
17404 65 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20  ed after a call 
17405 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e  to sqlite3BtreeN
17406 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a 20 70  ext() moves.** p
17407 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 6e 74  ast the last ent
17408 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ry in the table 
17409 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  or sqlite3BtreeP
1740a 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 73 74  rev() moves past
1740b 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 65 6e  .** the first en
1740c 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 61 6c  try.  TRUE is al
1740d 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  so returned if t
1740e 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74  he table is empt
1740f 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
17410 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
17411 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 6f  BtreeEof(BtCurso
17412 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54  r *pCur){.  /* T
17413 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 68 65  ODO: What if the
17414 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 43 55   cursor is in CU
17415 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
17416 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 20 65   but all table e
17417 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 76 65  ntries.  ** have
17418 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f 20 54   been deleted? T
17419 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e 65 65  his API will nee
1741a 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f 20 72  d to change to r
1741b 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
1741c 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 6c 6c  ode.  ** as well
1741d 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20   as the boolean 
1741e 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a 20 20  result value..  
1741f 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 55 52  */.  return (CUR
17420 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 72 2d  SOR_VALID!=pCur-
17421 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a  >eState);.}../*.
17422 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20 63  ** Advance the c
17423 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6e 65 78  ursor to the nex
17424 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64  t entry in the d
17425 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a 20  atabase.  If.** 
17426 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e 20  successful then 
17427 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 66  set *pRes=0.  If
17428 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77   the cursor.** w
17429 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74  as already point
1742a 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20  ing to the last 
1742b 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74  entry in the dat
1742c 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20  abase before.** 
1742d 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73  this routine was
1742e 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65   called, then se
1742f 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51  t *pRes=1..*/.SQ
17430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
17431 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78   sqlite3BtreeNex
17432 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  t(BtCursor *pCur
17433 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20  , int *pRes){.  
17434 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 64  int rc;.  int id
17435 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  x;.  MemPage *pP
17436 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  age;..  assert( 
17437 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
17438 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d  (pCur) );.  rc =
17439 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
1743a 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20  sition(pCur);.  
1743b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1743c 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
1743d 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rc;.  }.  assert
1743e 28 20 70 52 65 73 21 3d 30 20 29 3b 0a 20 20 69  ( pRes!=0 );.  i
1743f 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  f( CURSOR_INVALI
17440 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20  D==pCur->eState 
17441 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31  ){.    *pRes = 1
17442 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
17443 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
17444 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74  ( pCur->skipNext
17445 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e  >0 ){.    pCur->
17446 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20  skipNext = 0;.  
17447 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20    *pRes = 0;.   
17448 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
17449 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73  K;.  }.  pCur->s
1744a 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20  kipNext = 0;..  
1744b 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
1744c 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1744d 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43 75  ];.  idx = ++pCu
1744e 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
1744f 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28  Page];.  assert(
17450 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
17451 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3c  ;.  assert( idx<
17452 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b  =pPage->nCell );
17453 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  ..  pCur->info.n
17454 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72  Size = 0;.  pCur
17455 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b  ->validNKey = 0;
17456 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 61 67  .  if( idx>=pPag
17457 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20  e->nCell ){.    
17458 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
17459 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d   ){.      rc = m
1745a 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c  oveToChild(pCur,
1745b 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
1745c 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
1745d 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20  drOffset+8]));. 
1745e 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65       if( rc ) re
1745f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72  turn rc;.      r
17460 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f  c = moveToLeftmo
17461 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20  st(pCur);.      
17462 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20  *pRes = 0;.     
17463 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
17464 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20  }.    do{.      
17465 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d  if( pCur->iPage=
17466 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70  =0 ){.        *p
17467 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  Res = 1;.       
17468 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
17469 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
1746a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
1746b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
1746c 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 61  }.      moveToPa
1746d 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20  rent(pCur);.    
1746e 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e    pPage = pCur->
1746f 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
17470 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  ge];.    }while(
17471 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
17472 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65  r->iPage]>=pPage
17473 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 2a  ->nCell );.    *
17474 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 66  pRes = 0;.    if
17475 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
17476 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
17477 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70  lite3BtreeNext(p
17478 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20  Cur, pRes);.    
17479 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
1747a 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
1747b 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63   }.    return rc
1747c 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d 20  ;.  }.  *pRes = 
1747d 30 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  0;.  if( pPage->
1747e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 65 74 75  leaf ){.    retu
1747f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
17480 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c  }.  rc = moveToL
17481 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20  eftmost(pCur);. 
17482 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
17483 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 65 20 63  /*.** Step the c
17484 75 72 73 6f 72 20 74 6f 20 74 68 65 20 62 61 63  ursor to the bac
17485 6b 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75  k to the previou
17486 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64  s entry in the d
17487 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a 20  atabase.  If.** 
17488 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e 20  successful then 
17489 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 66  set *pRes=0.  If
1748a 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77   the cursor.** w
1748b 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74  as already point
1748c 69 6e 67 20 74 6f 20 74 68 65 20 66 69 72 73 74  ing to the first
1748d 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61   entry in the da
1748e 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a  tabase before.**
1748f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   this routine wa
17490 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73  s called, then s
17491 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53  et *pRes=1..*/.S
17492 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
17493 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72  t sqlite3BtreePr
17494 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72 20  evious(BtCursor 
17495 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73  *pCur, int *pRes
17496 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d  ){.  int rc;.  M
17497 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a  emPage *pPage;..
17498 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
17499 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
1749a 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f   );.  rc = resto
1749b 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
1749c 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
1749d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1749e 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1749f 7d 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74  }.  pCur->atLast
174a0 20 3d 20 30 3b 0a 20 20 69 66 28 20 43 55 52 53   = 0;.  if( CURS
174a1 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72  OR_INVALID==pCur
174a2 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20  ->eState ){.    
174a3 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 72  *pRes = 1;.    r
174a4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
174a5 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d  .  }.  if( pCur-
174a6 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29 7b 0a 20  >skipNext<0 ){. 
174a7 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78     pCur->skipNex
174a8 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52 65 73  t = 0;.    *pRes
174a9 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
174aa 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
174ab 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74    pCur->skipNext
174ac 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d   = 0;..  pPage =
174ad 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
174ae 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73  ur->iPage];.  as
174af 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49  sert( pPage->isI
174b0 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50  nit );.  if( !pP
174b1 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
174b2 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d   int idx = pCur-
174b3 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
174b4 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f  ge];.    rc = mo
174b5 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
174b6 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c  get4byte(findCel
174b7 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b  l(pPage, idx)));
174b8 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
174b9 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
174ba 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d      }.    rc = m
174bb 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70  oveToRightmost(p
174bc 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Cur);.  }else{. 
174bd 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 2d 3e     while( pCur->
174be 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
174bf 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  e]==0 ){.      i
174c0 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d  f( pCur->iPage==
174c1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75  0 ){.        pCu
174c2 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
174c3 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20  OR_INVALID;.    
174c4 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20      *pRes = 1;. 
174c5 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
174c6 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
174c7 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 61 72  .      moveToPar
174c8 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 7d  ent(pCur);.    }
174c9 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  .    pCur->info.
174ca 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70  nSize = 0;.    p
174cb 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
174cc 20 30 3b 0a 0a 20 20 20 20 70 43 75 72 2d 3e 61   0;..    pCur->a
174cd 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
174ce 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67 65 20 3d  ]--;.    pPage =
174cf 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
174d0 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
174d1 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
174d2 79 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61  y && !pPage->lea
174d3 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  f ){.      rc = 
174d4 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76  sqlite3BtreePrev
174d5 69 6f 75 73 28 70 43 75 72 2c 20 70 52 65 73 29  ious(pCur, pRes)
174d6 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
174d7 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
174d8 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a  K;.    }.  }.  *
174d9 70 52 65 73 20 3d 20 30 3b 0a 20 20 72 65 74 75  pRes = 0;.  retu
174da 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
174db 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  Allocate a new p
174dc 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74  age from the dat
174dd 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
174de 2a 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 69  * The new page i
174df 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74  s marked as dirt
174e0 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 20 77 6f  y.  (In other wo
174e1 72 64 73 2c 20 73 71 6c 69 74 65 33 50 61 67 65  rds, sqlite3Page
174e2 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 61 73 20  rWrite().** has 
174e3 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c  already been cal
174e4 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 77 20 70  led on the new p
174e5 61 67 65 2e 29 20 20 54 68 65 20 6e 65 77 20 70  age.)  The new p
174e6 61 67 65 20 68 61 73 20 61 6c 73 6f 0a 2a 2a 20  age has also.** 
174e7 62 65 65 6e 20 72 65 66 65 72 65 6e 63 65 64 20  been referenced 
174e8 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  and the calling 
174e9 72 6f 75 74 69 6e 65 20 69 73 20 72 65 73 70 6f  routine is respo
174ea 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69  nsible for calli
174eb 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67  ng.** sqlite3Pag
174ec 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 74 68 65  erUnref() on the
174ed 20 6e 65 77 20 70 61 67 65 20 77 68 65 6e 20 69   new page when i
174ee 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a  t is done..**.**
174ef 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
174f0 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73  turned on succes
174f1 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 20 72 65  s.  Any other re
174f2 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63  turn value indic
174f3 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72  ates.** an error
174f4 2e 20 20 2a 70 70 50 61 67 65 20 61 6e 64 20 2a  .  *ppPage and *
174f5 70 50 67 6e 6f 20 61 72 65 20 75 6e 64 65 66 69  pPgno are undefi
174f6 6e 65 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74  ned in the event
174f7 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a   of an error..**
174f8 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 73   Do not invoke s
174f9 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
174fa 28 29 20 6f 6e 20 2a 70 70 50 61 67 65 20 69 66  () on *ppPage if
174fb 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
174fc 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
174fd 74 68 65 20 22 6e 65 61 72 62 79 22 20 70 61 72  the "nearby" par
174fe 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c  ameter is not 0,
174ff 20 74 68 65 6e 20 61 20 28 66 65 65 62 6c 65 29   then a (feeble)
17500 20 65 66 66 6f 72 74 20 69 73 20 6d 61 64 65 20   effort is made 
17501 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65 20 61 20  to .** locate a 
17502 70 61 67 65 20 63 6c 6f 73 65 20 74 6f 20 74 68  page close to th
17503 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 22 6e  e page number "n
17504 65 61 72 62 79 22 2e 20 20 54 68 69 73 20 63 61  earby".  This ca
17505 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61 6e 0a  n be used in an.
17506 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 6b 65  ** attempt to ke
17507 65 70 20 72 65 6c 61 74 65 64 20 70 61 67 65 73  ep related pages
17508 20 63 6c 6f 73 65 20 74 6f 20 65 61 63 68 20 6f   close to each o
17509 74 68 65 72 20 69 6e 20 74 68 65 20 64 61 74 61  ther in the data
1750a 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 77 68  base file,.** wh
1750b 69 63 68 20 69 6e 20 74 75 72 6e 20 63 61 6e 20  ich in turn can 
1750c 6d 61 6b 65 20 64 61 74 61 62 61 73 65 20 61 63  make database ac
1750d 63 65 73 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a  cess faster..**.
1750e 2a 2a 20 49 66 20 74 68 65 20 22 65 78 61 63 74  ** If the "exact
1750f 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  " parameter is n
17510 6f 74 20 30 2c 20 61 6e 64 20 74 68 65 20 70 61  ot 0, and the pa
17511 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61 72 62 79  ge-number nearby
17512 20 65 78 69 73 74 73 20 0a 2a 2a 20 61 6e 79 77   exists .** anyw
17513 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65  here on the free
17514 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 74 20 69  -list, then it i
17515 73 20 67 75 61 72 65 6e 74 65 65 64 20 74 6f 20  s guarenteed to 
17516 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69  be returned. Thi
17517 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 75 73 65  s.** is only use
17518 64 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d  d by auto-vacuum
17519 20 64 61 74 61 62 61 73 65 73 20 77 68 65 6e 20   databases when 
1751a 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77  allocating a new
1751b 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
1751c 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74  c int allocateBt
1751d 72 65 65 50 61 67 65 28 0a 20 20 42 74 53 68 61  reePage(.  BtSha
1751e 72 65 64 20 2a 70 42 74 2c 20 0a 20 20 4d 65 6d  red *pBt, .  Mem
1751f 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 0a  Page **ppPage, .
17520 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c 20 0a    Pgno *pPgno, .
17521 20 20 50 67 6e 6f 20 6e 65 61 72 62 79 2c 0a 20    Pgno nearby,. 
17522 20 75 38 20 65 78 61 63 74 0a 29 7b 0a 20 20 4d   u8 exact.){.  M
17523 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a  emPage *pPage1;.
17524 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 20    int rc;.  u32 
17525 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  n;     /* Number
17526 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65   of pages on the
17527 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 75   freelist */.  u
17528 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20 4e 75 6d  32 k;     /* Num
17529 62 65 72 20 6f 66 20 6c 65 61 76 65 73 20 6f 6e  ber of leaves on
1752a 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 20 74 68   the trunk of th
1752b 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20  e freelist */.  
1752c 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e 6b 20  MemPage *pTrunk 
1752d 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  = 0;.  MemPage *
1752e 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a  pPrevTrunk = 0;.
1752f 20 20 50 67 6e 6f 20 6d 78 50 61 67 65 3b 20 20    Pgno mxPage;  
17530 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65     /* Total size
17531 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
17532 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65   file */..  asse
17533 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
17534 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
17535 78 29 20 29 3b 0a 20 20 70 50 61 67 65 31 20 3d  x) );.  pPage1 =
17536 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20   pBt->pPage1;.  
17537 6d 78 50 61 67 65 20 3d 20 70 61 67 65 72 50 61  mxPage = pagerPa
17538 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 20  gecount(pBt);.  
17539 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  n = get4byte(&pP
1753a 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29  age1->aData[36])
1753b 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d  ;.  testcase( n=
1753c 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a 20 20 69  =mxPage-1 );.  i
1753d 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20 29 7b 0a  f( n>=mxPage ){.
1753e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1753f 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
17540 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b    }.  if( n>0 ){
17541 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72  .    /* There ar
17542 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66  e pages on the f
17543 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65 20  reelist.  Reuse 
17544 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 67  one of those pag
17545 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20  es. */.    Pgno 
17546 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 73  iTrunk;.    u8 s
17547 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 2f  earchList = 0; /
17548 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c 69  * If the free-li
17549 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72 63  st must be searc
1754a 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79 27  hed for 'nearby'
1754b 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20   */.    .    /* 
1754c 49 66 20 74 68 65 20 27 65 78 61 63 74 27 20 70  If the 'exact' p
1754d 61 72 61 6d 65 74 65 72 20 77 61 73 20 74 72 75  arameter was tru
1754e 65 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f 66  e and a query of
1754f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
17550 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74 68  .    ** shows th
17551 61 74 20 74 68 65 20 70 61 67 65 20 27 6e 65 61  at the page 'nea
17552 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65 72  rby' is somewher
17553 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69  e on the free-li
17554 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20  st, then.    ** 
17555 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74 20  the entire-list 
17556 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65 64  will be searched
17557 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e 0a   for that page..
17558 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
17559 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
1755a 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65 78  ACUUM.    if( ex
1755b 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d 6d  act && nearby<=m
1755c 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 75  xPage ){.      u
1755d 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 61  8 eType;.      a
1755e 73 73 65 72 74 28 20 6e 65 61 72 62 79 3e 30 20  ssert( nearby>0 
1755f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
17560 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
17561 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   );.      rc = p
17562 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 6e 65  trmapGet(pBt, ne
17563 61 72 62 79 2c 20 26 65 54 79 70 65 2c 20 30 29  arby, &eType, 0)
17564 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
17565 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
17566 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52    if( eType==PTR
17567 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a  MAP_FREEPAGE ){.
17568 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69          searchLi
17569 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  st = 1;.      }.
1756a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e        *pPgno = n
1756b 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65 6e  earby;.    }.#en
1756c 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72  dif..    /* Decr
1756d 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c  ement the free-l
1756e 69 73 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20  ist count by 1. 
1756f 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68  Set iTrunk to th
17570 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20  e index of the. 
17571 20 20 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65     ** first free
17572 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
17573 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20  . iPrevTrunk is 
17574 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20  initially 1..   
17575 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
17576 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
17577 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b  Page1->pDbPage);
17578 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
17579 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74  turn rc;.    put
1757a 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
1757b 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a  Data[36], n-1);.
1757c 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65  .    /* The code
1757d 20 77 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f   within this loo
1757e 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e  p is run only on
1757f 63 65 20 69 66 20 74 68 65 20 27 73 65 61 72 63  ce if the 'searc
17580 68 4c 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a  hList' variable.
17581 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72      ** is not tr
17582 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ue. Otherwise, i
17583 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20  t runs once for 
17584 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20  each trunk-page 
17585 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72  on the.    ** fr
17586 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68  ee-list until th
17587 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20  e page 'nearby' 
17588 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20 20  is located..    
17589 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  */.    do {.    
1758a 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 70    pPrevTrunk = p
1758b 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28  Trunk;.      if(
1758c 20 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20   pPrevTrunk ){. 
1758d 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20         iTrunk = 
1758e 67 65 74 34 62 79 74 65 28 26 70 50 72 65 76 54  get4byte(&pPrevT
1758f 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b  runk->aData[0]);
17590 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
17591 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67        iTrunk = g
17592 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d  et4byte(&pPage1-
17593 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20  >aData[32]);.   
17594 20 20 20 7d 0a 20 20 20 20 20 20 74 65 73 74 63     }.      testc
17595 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d 6d 78 50  ase( iTrunk==mxP
17596 61 67 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28  age );.      if(
17597 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29   iTrunk>mxPage )
17598 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
17599 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1759a 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  PT;.      }else{
1759b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74  .        rc = bt
1759c 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
1759d 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c  iTrunk, &pTrunk,
1759e 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
1759f 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
175a0 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b       pTrunk = 0;
175a1 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  .        goto en
175a2 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
175a3 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
175a4 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 54  k = get4byte(&pT
175a5 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b  runk->aData[4]);
175a6 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20  .      if( k==0 
175a7 26 26 20 21 73 65 61 72 63 68 4c 69 73 74 20 29  && !searchList )
175a8 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  {.        /* The
175a9 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c 65   trunk has no le
175aa 61 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69 73  aves and the lis
175ab 74 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 73  t is not being s
175ac 65 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20 20  earched. .      
175ad 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74 20    ** So extract 
175ae 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69  the trunk page i
175af 74 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69 74  tself and use it
175b0 20 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a 20   as the newly . 
175b1 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61         ** alloca
175b2 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 20  ted page */.    
175b3 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72 65      assert( pPre
175b4 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20  vTrunk==0 );.   
175b5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
175b6 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75  3PagerWrite(pTru
175b7 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  nk->pDbPage);.  
175b8 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
175b9 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
175ba 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
175bb 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
175bc 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72      *pPgno = iTr
175bd 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  unk;.        mem
175be 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61  cpy(&pPage1->aDa
175bf 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d  ta[32], &pTrunk-
175c0 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20  >aData[0], 4);. 
175c1 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d         *ppPage =
175c2 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20   pTrunk;.       
175c3 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20   pTrunk = 0;.   
175c4 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c       TRACE(("ALL
175c5 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20  OCATE: %d trunk 
175c6 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20  - %d free pages 
175c7 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c  left\n", *pPgno,
175c8 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d 65   n-1));.      }e
175c9 6c 73 65 20 69 66 28 20 6b 3e 28 75 33 32 29 28  lse if( k>(u32)(
175ca 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f  pBt->usableSize/
175cb 34 20 2d 20 32 29 20 29 7b 0a 20 20 20 20 20 20  4 - 2) ){.      
175cc 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b 20    /* Value of k 
175cd 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e  is out of range.
175ce 20 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 75    Database corru
175cf 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20  ption */.       
175d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
175d1 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
175d2 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
175d3 63 61 74 65 5f 70 61 67 65 3b 0a 23 69 66 6e 64  cate_page;.#ifnd
175d4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
175d5 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
175d6 7d 65 6c 73 65 20 69 66 28 20 73 65 61 72 63 68  }else if( search
175d7 4c 69 73 74 20 26 26 20 6e 65 61 72 62 79 3d 3d  List && nearby==
175d8 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20  iTrunk ){.      
175d9 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 69 73    /* The list is
175da 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 20   being searched 
175db 61 6e 64 20 74 68 69 73 20 74 72 75 6e 6b 20 70  and this trunk p
175dc 61 67 65 20 69 73 20 74 68 65 20 70 61 67 65 0a  age is the page.
175dd 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 6c          ** to al
175de 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 64 6c 65  locate, regardle
175df 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 69 74  ss of whether it
175e0 20 68 61 73 20 6c 65 61 76 65 73 2e 0a 20 20 20   has leaves..   
175e1 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
175e2 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 3d 3d  assert( *pPgno==
175e3 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20 20 20 20  iTrunk );.      
175e4 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75    *ppPage = pTru
175e5 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 65 61 72  nk;.        sear
175e6 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20  chList = 0;.    
175e7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
175e8 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e  PagerWrite(pTrun
175e9 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
175ea 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
175eb 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
175ec 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
175ed 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
175ee 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 7b 0a 20     if( k==0 ){. 
175ef 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50           if( !pP
175f0 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20  revTrunk ){.    
175f1 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
175f2 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
175f3 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  ], &pTrunk->aDat
175f4 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20  a[0], 4);.      
175f5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
175f6 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70         memcpy(&p
175f7 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61  PrevTrunk->aData
175f8 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44  [0], &pTrunk->aD
175f9 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20  ata[0], 4);.    
175fa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
175fb 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
175fc 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 70 61   /* The trunk pa
175fd 67 65 20 69 73 20 72 65 71 75 69 72 65 64 20 62  ge is required b
175fe 79 20 74 68 65 20 63 61 6c 6c 65 72 20 62 75 74  y the caller but
175ff 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 0a 20 20   it contains .  
17600 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74          ** point
17601 65 72 73 20 74 6f 20 66 72 65 65 2d 6c 69 73 74  ers to free-list
17602 20 6c 65 61 76 65 73 2e 20 54 68 65 20 66 69 72   leaves. The fir
17603 73 74 20 6c 65 61 66 20 62 65 63 6f 6d 65 73 20  st leaf becomes 
17604 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 20 20 20  a trunk.        
17605 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 69    ** page in thi
17606 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20  s case..        
17607 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 4d    */.          M
17608 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 72 75 6e  emPage *pNewTrun
17609 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 50 67 6e  k;.          Pgn
1760a 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d 20 67 65  o iNewTrunk = ge
1760b 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e  t4byte(&pTrunk->
1760c 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20  aData[8]);.     
1760d 20 20 20 20 20 69 66 28 20 69 4e 65 77 54 72 75       if( iNewTru
1760e 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20 0a 20 20  nk>mxPage ){ .  
1760f 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
17610 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
17611 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  PT;.            
17612 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74  goto end_allocat
17613 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20  e_page;.        
17614 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 74 65    }.          te
17615 73 74 63 61 73 65 28 20 69 4e 65 77 54 72 75 6e  stcase( iNewTrun
17616 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20  k==mxPage );.   
17617 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65         rc = btre
17618 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4e  eGetPage(pBt, iN
17619 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72  ewTrunk, &pNewTr
1761a 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  unk, 0);.       
1761b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1761c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1761d 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
1761e 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
1761f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
17620 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
17621 67 65 72 57 72 69 74 65 28 70 4e 65 77 54 72 75  gerWrite(pNewTru
17622 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  nk->pDbPage);.  
17623 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
17624 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
17625 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65           release
17626 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b  Page(pNewTrunk);
17627 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74  .            got
17628 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
17629 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  age;.          }
1762a 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
1762b 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44  y(&pNewTrunk->aD
1762c 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d  ata[0], &pTrunk-
1762d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20  >aData[0], 4);. 
1762e 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74           put4byt
1762f 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44  e(&pNewTrunk->aD
17630 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20  ata[4], k-1);.  
17631 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
17632 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61  pNewTrunk->aData
17633 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44  [8], &pTrunk->aD
17634 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34  ata[12], (k-1)*4
17635 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c  );.          rel
17636 65 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75  easePage(pNewTru
17637 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  nk);.          i
17638 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29  f( !pPrevTrunk )
17639 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  {.            as
1763a 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
1763b 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
1763c 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 20 29  age1->pDbPage) )
1763d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75  ;.            pu
1763e 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e  t4byte(&pPage1->
1763f 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54  aData[32], iNewT
17640 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20  runk);.         
17641 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
17642 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17643 50 61 67 65 72 57 72 69 74 65 28 70 50 72 65 76  PagerWrite(pPrev
17644 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b  Trunk->pDbPage);
17645 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
17646 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20   rc ){.         
17647 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c       goto end_al
17648 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20  locate_page;.   
17649 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1764a 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28         put4byte(
1764b 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61  &pPrevTrunk->aDa
1764c 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b  ta[0], iNewTrunk
1764d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
1764e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1764f 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20   pTrunk = 0;.   
17650 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c       TRACE(("ALL
17651 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20  OCATE: %d trunk 
17652 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20  - %d free pages 
17653 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c  left\n", *pPgno,
17654 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20   n-1));.#endif. 
17655 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b       }else if( k
17656 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  >0 ){.        /*
17657 20 45 78 74 72 61 63 74 20 61 20 6c 65 61 66 20   Extract a leaf 
17658 66 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b 20 2a  from the trunk *
17659 2f 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6c  /.        u32 cl
1765a 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 20 20 50  osest;.        P
1765b 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20 20 20  gno iPage;.     
1765c 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
1765d 20 2a 61 44 61 74 61 20 3d 20 70 54 72 75 6e 6b   *aData = pTrunk
1765e 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20 20  ->aData;.       
1765f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
17660 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e  erWrite(pTrunk->
17661 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
17662 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
17663 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
17664 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20  llocate_page;.  
17665 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
17666 69 66 28 20 6e 65 61 72 62 79 3e 30 20 29 7b 0a  if( nearby>0 ){.
17667 20 20 20 20 20 20 20 20 20 20 75 33 32 20 69 3b            u32 i;
17668 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 64  .          int d
17669 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 63  ist;.          c
1766a 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20  losest = 0;.    
1766b 20 20 20 20 20 20 64 69 73 74 20 3d 20 67 65 74        dist = get
1766c 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 5d 29  4byte(&aData[8])
1766d 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 20   - nearby;.     
1766e 20 20 20 20 20 69 66 28 20 64 69 73 74 3c 30 20       if( dist<0 
1766f 29 20 64 69 73 74 20 3d 20 2d 64 69 73 74 3b 0a  ) dist = -dist;.
17670 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
17671 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20  1; i<k; i++){.  
17672 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 64 32            int d2
17673 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61   = get4byte(&aDa
17674 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e 65 61  ta[8+i*4]) - nea
17675 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20  rby;.           
17676 20 69 66 28 20 64 32 3c 30 20 29 20 64 32 20 3d   if( d2<0 ) d2 =
17677 20 2d 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20   -d2;.          
17678 20 20 69 66 28 20 64 32 3c 64 69 73 74 20 29 7b    if( d2<dist ){
17679 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63  .              c
1767a 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 20 20  losest = i;.    
1767b 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d            dist =
1767c 20 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20   d2;.           
1767d 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
1767e 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1767f 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20          closest 
17680 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  = 0;.        }..
17681 20 20 20 20 20 20 20 20 69 50 61 67 65 20 3d 20          iPage = 
17682 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b  get4byte(&aData[
17683 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 3b 0a 20  8+closest*4]);. 
17684 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
17685 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 29   iPage==mxPage )
17686 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 50  ;.        if( iP
17687 61 67 65 3e 6d 78 50 61 67 65 20 29 7b 0a 20 20  age>mxPage ){.  
17688 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
17689 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1768a 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
1768b 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61   end_allocate_pa
1768c 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ge;.        }.  
1768d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1768e 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 29 3b  iPage==mxPage );
1768f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73 65  .        if( !se
17690 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 50 61 67  archList || iPag
17691 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a 20 20 20  e==nearby ){.   
17692 20 20 20 20 20 20 20 69 6e 74 20 6e 6f 43 6f 6e         int noCon
17693 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 20 20  tent;.          
17694 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67 65 3b 0a  *pPgno = iPage;.
17695 20 20 20 20 20 20 20 20 20 20 54 52 41 43 45 28            TRACE(
17696 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 77  ("ALLOCATE: %d w
17697 61 73 20 6c 65 61 66 20 25 64 20 6f 66 20 25 64  as leaf %d of %d
17698 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22 0a 20 20   on trunk %d".  
17699 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
1769a 3a 20 25 64 20 6d 6f 72 65 20 66 72 65 65 20 70  : %d more free p
1769b 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20  ages\n",.       
1769c 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f            *pPgno
1769d 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20 6b 2c 20  , closest+1, k, 
1769e 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d  pTrunk->pgno, n-
1769f 31 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  1));.          i
176a0 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d 31 20 29  f( closest<k-1 )
176a1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65  {.            me
176a2 6d 63 70 79 28 26 61 44 61 74 61 5b 38 2b 63 6c  mcpy(&aData[8+cl
176a3 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44 61 74 61  osest*4], &aData
176a4 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a 20 20 20  [4+k*4], 4);.   
176a5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
176a6 20 20 20 70 75 74 34 62 79 74 65 28 26 61 44 61     put4byte(&aDa
176a7 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20  ta[4], k-1);.   
176a8 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73         assert( s
176a9 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
176aa 74 65 61 62 6c 65 28 70 54 72 75 6e 6b 2d 3e 70  teable(pTrunk->p
176ab 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 20  DbPage) );.     
176ac 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e 74 20 3d       noContent =
176ad 20 21 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e   !btreeGetHasCon
176ae 74 65 6e 74 28 70 42 74 2c 20 2a 70 50 67 6e 6f  tent(pBt, *pPgno
176af 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
176b0 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
176b1 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61  Bt, *pPgno, ppPa
176b2 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a  ge, noContent);.
176b3 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
176b4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
176b5 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
176b6 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
176b7 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62  e((*ppPage)->pDb
176b8 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
176b9 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
176ba 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
176bb 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
176bc 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20  e(*ppPage);.    
176bd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
176be 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
176bf 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a  searchList = 0;.
176c0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
176c1 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  }.      releaseP
176c2 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b  age(pPrevTrunk);
176c3 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e  .      pPrevTrun
176c4 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c  k = 0;.    }whil
176c5 65 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b  e( searchList );
176c6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
176c7 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61   There are no pa
176c8 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c  ges on the freel
176c9 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61  ist, so create a
176ca 20 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 65   new page at the
176cb 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74  .    ** end of t
176cc 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 69  he file */.    i
176cd 6e 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72  nt nPage = pager
176ce 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a  Pagecount(pBt);.
176cf 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50 61      *pPgno = nPa
176d0 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 66 28  ge + 1;..    if(
176d1 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47   *pPgno==PENDING
176d2 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
176d3 29 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f  ){.      (*pPgno
176d4 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e  )++;.    }..#ifn
176d5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
176d6 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69  AUTOVACUUM.    i
176d7 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
176d8 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 50  um && PTRMAP_ISP
176d9 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f 29  AGE(pBt, *pPgno)
176da 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
176db 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 6f  *pPgno refers to
176dc 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70   a pointer-map p
176dd 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 77  age, allocate tw
176de 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 20  o new pages.    
176df 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20    ** at the end 
176e0 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 74  of the file inst
176e1 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 20  ead of one. The 
176e2 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 20  first allocated 
176e3 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65  page.      ** be
176e4 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 6e  comes a new poin
176e5 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 68  ter-map page, th
176e6 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 64  e second is used
176e7 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   by the caller..
176e8 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4d        */.      M
176e9 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30 3b  emPage *pPg = 0;
176ea 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41  .      TRACE(("A
176eb 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d  LLOCATE: %d from
176ec 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 6f   end of file (po
176ed 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29 5c  inter-map page)\
176ee 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20  n", *pPgno));.  
176ef 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67      assert( *pPg
176f0 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  no!=PENDING_BYTE
176f1 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20  _PAGE(pBt) );.  
176f2 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65      rc = btreeGe
176f3 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e  tPage(pBt, *pPgn
176f4 6f 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 20 20  o, &pPg, 0);.   
176f5 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
176f6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
176f7 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
176f8 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50  rWrite(pPg->pDbP
176f9 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65  age);.        re
176fa 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a  leasePage(pPg);.
176fb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
176fc 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
176fd 3b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f 29  ;.      (*pPgno)
176fe 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 2a 70  ++;.      if( *p
176ff 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 59  Pgno==PENDING_BY
17700 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 20  TE_PAGE(pBt) ){ 
17701 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d 0a 20 20  (*pPgno)++; }.  
17702 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
17703 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d  assert( *pPgno!=
17704 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
17705 45 28 70 42 74 29 20 29 3b 0a 20 20 20 20 72 63  E(pBt) );.    rc
17706 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
17707 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50  pBt, *pPgno, ppP
17708 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  age, 0);.    if(
17709 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
1770a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1770b 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 70  3PagerWrite((*pp
1770c 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b  Page)->pDbPage);
1770d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1770e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1770f 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50  releasePage(*ppP
17710 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  age);.    }.    
17711 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45  TRACE(("ALLOCATE
17712 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66  : %d from end of
17713 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f   file\n", *pPgno
17714 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  ));.  }..  asser
17715 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49  t( *pPgno!=PENDI
17716 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
17717 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61  ) );..end_alloca
17718 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 61  te_page:.  relea
17719 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a  sePage(pTrunk);.
1771a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
1771b 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28  revTrunk);.  if(
1771c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1771d 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
1771e 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75  3PagerPageRefcou
1771f 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44  nt((*ppPage)->pD
17720 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 20 20  bPage)>1 ){.    
17721 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70    releasePage(*p
17722 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65  pPage);.      re
17723 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
17724 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a  UPT_BKPT;.    }.
17725 20 20 20 20 28 2a 70 70 50 61 67 65 29 2d 3e 69      (*ppPage)->i
17726 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 65 6c  sInit = 0;.  }el
17727 73 65 7b 0a 20 20 20 20 2a 70 70 50 61 67 65 20  se{.    *ppPage 
17728 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
17729 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
1772a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1772b 75 73 65 64 20 74 6f 20 61 64 64 20 70 61 67 65  used to add page
1772c 20 69 50 61 67 65 20 74 6f 20 74 68 65 20 64 61   iPage to the da
1772d 74 61 62 61 73 65 20 66 69 6c 65 20 66 72 65 65  tabase file free
1772e 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 20 69 73  -list. .** It is
1772f 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
17730 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c  e page is not al
17731 72 65 61 64 79 20 61 20 70 61 72 74 20 6f 66 20  ready a part of 
17732 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 2a  the free-list..*
17733 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 70  *.** The value p
17734 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
17735 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
17736 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
17737 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 49 66   optional..** If
17738 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 70 70   the caller happ
17739 65 6e 73 20 74 6f 20 68 61 76 65 20 61 20 70 6f  ens to have a po
1773a 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d  inter to the Mem
1773b 50 61 67 65 20 6f 62 6a 65 63 74 20 0a 2a 2a 20  Page object .** 
1773c 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
1773d 20 70 61 67 65 20 69 50 61 67 65 20 68 61 6e 64   page iPage hand
1773e 79 2c 20 69 74 20 6d 61 79 20 70 61 73 73 20 69  y, it may pass i
1773f 74 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  t as the second 
17740 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 68 65 72  value. .** Other
17741 77 69 73 65 2c 20 69 74 20 6d 61 79 20 70 61 73  wise, it may pas
17742 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66  s NULL..**.** If
17743 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
17744 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 69  MemPage object i
17745 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
17746 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
17747 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 65 6e 63  .** its referenc
17748 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f 74 20 61  e count is not a
17749 6c 74 65 72 65 64 20 62 79 20 74 68 69 73 20 66  ltered by this f
1774a 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
1774b 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65 32  ic int freePage2
1774c 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20  (BtShared *pBt, 
1774d 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d 50 61 67  MemPage *pMemPag
1774e 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 29 7b 0a  e, Pgno iPage){.
1774f 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e    MemPage *pTrun
17750 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  k = 0;          
17751 20 20 20 20 20 20 2f 2a 20 46 72 65 65 2d 6c 69        /* Free-li
17752 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f  st trunk page */
17753 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b 20 3d  .  Pgno iTrunk =
17754 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
17755 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
17756 75 6d 62 65 72 20 6f 66 20 66 72 65 65 2d 6c 69  umber of free-li
17757 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f  st trunk page */
17758 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61   .  MemPage *pPa
17759 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65  ge1 = pBt->pPage
1775a 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c  1;      /* Local
1775b 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61   reference to pa
1775c 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  ge 1 */.  MemPag
1775d 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20  e *pPage;       
1775e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1775f 20 50 61 67 65 20 62 65 69 6e 67 20 66 72 65 65   Page being free
17760 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 2e 20  d. May be NULL. 
17761 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
17762 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17763 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
17764 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  rn Code */.  int
17765 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20   nFree;         
17766 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17767 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 6d 62   /* Initial numb
17768 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 66  er of pages on f
17769 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61  ree-list */..  a
1776a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1776b 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
1776c 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
1776d 74 28 20 69 50 61 67 65 3e 31 20 29 3b 0a 20 20  t( iPage>1 );.  
1776e 61 73 73 65 72 74 28 20 21 70 4d 65 6d 50 61 67  assert( !pMemPag
1776f 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 2d 3e 70  e || pMemPage->p
17770 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b 0a 0a 20  gno==iPage );.. 
17771 20 69 66 28 20 70 4d 65 6d 50 61 67 65 20 29 7b   if( pMemPage ){
17772 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 4d 65  .    pPage = pMe
17773 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 6c 69 74  mPage;.    sqlit
17774 65 33 50 61 67 65 72 52 65 66 28 70 50 61 67 65  e3PagerRef(pPage
17775 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 65  ->pDbPage);.  }e
17776 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 20 3d  lse{.    pPage =
17777 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70   btreePageLookup
17778 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a 20 20  (pBt, iPage);.  
17779 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e  }..  /* Incremen
1777a 74 20 74 68 65 20 66 72 65 65 20 70 61 67 65 20  t the free page 
1777b 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 65 31 20  count on pPage1 
1777c 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
1777d 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
1777e 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  e1->pDbPage);.  
1777f 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66 72  if( rc ) goto fr
17780 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 6e 46  eepage_out;.  nF
17781 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 28 26  ree = get4byte(&
17782 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
17783 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26  ]);.  put4byte(&
17784 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
17785 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a 0a 23 69  ], nFree+1);..#i
17786 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55  fdef SQLITE_SECU
17787 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 49  RE_DELETE.  /* I
17788 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53 45 43  f the SQLITE_SEC
17789 55 52 45 5f 44 45 4c 45 54 45 20 63 6f 6d 70 69  URE_DELETE compi
1778a 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 69  le-time option i
1778b 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 0a  s enabled, then.
1778c 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75 6c 6c    ** always full
1778d 79 20 6f 76 65 72 77 72 69 74 65 20 64 65 6c 65  y overwrite dele
1778e 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ted information 
1778f 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 2a 2f  with zeros..  */
17790 0a 20 20 69 66 28 20 28 21 70 50 61 67 65 20 26  .  if( (!pPage &
17791 26 20 28 72 63 20 3d 20 62 74 72 65 65 47 65 74  & (rc = btreeGet
17792 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 65 2c  Page(pBt, iPage,
17793 20 26 70 50 61 67 65 2c 20 30 29 29 29 0a 20 20   &pPage, 0))).  
17794 20 7c 7c 20 20 20 20 20 20 20 20 20 20 20 20 28   ||            (
17795 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
17796 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44  rWrite(pPage->pD
17797 62 50 61 67 65 29 29 0a 20 20 29 7b 0a 20 20 20  bPage)).  ){.   
17798 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f   goto freepage_o
17799 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  ut;.  }.  memset
1779a 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 30  (pPage->aData, 0
1779b 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 61  , pPage->pBt->pa
1779c 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a  geSize);.#endif.
1779d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74  .  /* If the dat
1779e 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61  abase supports a
1779f 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 69 74  uto-vacuum, writ
177a0 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68  e an entry in th
177a1 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20  e pointer-map.  
177a2 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  ** to indicate t
177a3 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20  hat the page is 
177a4 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  free..  */.  if(
177a5 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b   ISAUTOVACUUM ){
177a6 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70  .    ptrmapPut(p
177a7 42 74 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41  Bt, iPage, PTRMA
177a8 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 26  P_FREEPAGE, 0, &
177a9 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  rc);.    if( rc 
177aa 29 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f  ) goto freepage_
177ab 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e  out;.  }..  /* N
177ac 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 20 74 68  ow manipulate th
177ad 65 20 61 63 74 75 61 6c 20 64 61 74 61 62 61 73  e actual databas
177ae 65 20 66 72 65 65 2d 6c 69 73 74 20 73 74 72 75  e free-list stru
177af 63 74 75 72 65 2e 20 54 68 65 72 65 20 61 72 65  cture. There are
177b0 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73 73 69 62   two.  ** possib
177b1 69 6c 69 74 69 65 73 2e 20 49 66 20 74 68 65 20  ilities. If the 
177b2 66 72 65 65 2d 6c 69 73 74 20 69 73 20 63 75 72  free-list is cur
177b3 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c 20 6f 72  rently empty, or
177b4 20 69 66 20 74 68 65 20 66 69 72 73 74 0a 20 20   if the first.  
177b5 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e  ** trunk page in
177b6 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69   the free-list i
177b7 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20 74 68 69  s full, then thi
177b8 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63 6f  s page will beco
177b9 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77 20 66 72  me a.  ** new fr
177ba 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61  ee-list trunk pa
177bb 67 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ge. Otherwise, i
177bc 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 20  t will become a 
177bd 6c 65 61 66 20 6f 66 20 74 68 65 0a 20 20 2a 2a  leaf of the.  **
177be 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61 67   first trunk pag
177bf 65 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74  e in the current
177c0 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73   free-list. This
177c1 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 69 66 20   block tests if 
177c2 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 73 69  it.  ** is possi
177c3 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 20 70  ble to add the p
177c4 61 67 65 20 61 73 20 61 20 6e 65 77 20 66 72 65  age as a new fre
177c5 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a 20 20 2a  e-list leaf..  *
177c6 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 21 3d 30  /.  if( nFree!=0
177c7 20 29 7b 0a 20 20 20 20 75 33 32 20 6e 4c 65 61   ){.    u32 nLea
177c8 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
177c9 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 6d    /* Initial num
177ca 62 65 72 20 6f 66 20 6c 65 61 66 20 63 65 6c 6c  ber of leaf cell
177cb 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 20  s on trunk page 
177cc 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e 6b 20 3d  */..    iTrunk =
177cd 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
177ce 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20  1->aData[32]);. 
177cf 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74     rc = btreeGet
177d0 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e 6b  Page(pBt, iTrunk
177d1 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20  , &pTrunk, 0);. 
177d2 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
177d3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
177d4 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b  to freepage_out;
177d5 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c 65 61  .    }..    nLea
177d6 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 54  f = get4byte(&pT
177d7 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b  runk->aData[4]);
177d8 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74  .    assert( pBt
177d9 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e 33 32 20  ->usableSize>32 
177da 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66  );.    if( nLeaf
177db 20 3e 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61   > (u32)pBt->usa
177dc 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 29 7b  bleSize/4 - 2 ){
177dd 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
177de 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
177df 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65  .      goto free
177e0 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  page_out;.    }.
177e1 20 20 20 20 69 66 28 20 6e 4c 65 61 66 20 3c 20      if( nLeaf < 
177e2 28 75 33 32 29 70 42 74 2d 3e 75 73 61 62 6c 65  (u32)pBt->usable
177e3 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a 20 20  Size/4 - 8 ){.  
177e4 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63      /* In this c
177e5 61 73 65 20 74 68 65 72 65 20 69 73 20 72 6f 6f  ase there is roo
177e6 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 70  m on the trunk p
177e7 61 67 65 20 74 6f 20 69 6e 73 65 72 74 20 74 68  age to insert th
177e8 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20  e page.      ** 
177e9 62 65 69 6e 67 20 66 72 65 65 64 20 61 73 20 61  being freed as a
177ea 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20 20 20 20   new leaf..     
177eb 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74   **.      ** Not
177ec 65 20 74 68 61 74 20 74 68 65 20 74 72 75 6e 6b  e that the trunk
177ed 20 70 61 67 65 20 69 73 20 6e 6f 74 20 72 65 61   page is not rea
177ee 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69  lly full until i
177ef 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20  t contains.     
177f0 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34   ** usableSize/4
177f1 20 2d 20 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f   - 2 entries, no
177f2 74 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d  t usableSize/4 -
177f3 20 38 20 65 6e 74 72 69 65 73 20 61 73 20 77 65   8 entries as we
177f4 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 63   have.      ** c
177f5 6f 64 65 64 2e 20 20 42 75 74 20 64 75 65 20 74  oded.  But due t
177f6 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72  o a coding error
177f7 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66 20   in versions of 
177f8 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74 6f 0a  SQLite prior to.
177f9 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20        ** 3.6.0, 
177fa 64 61 74 61 62 61 73 65 73 20 77 69 74 68 20 66  databases with f
177fb 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61  reelist trunk pa
177fc 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65  ges holding more
177fd 20 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75   than.      ** u
177fe 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20  sableSize/4 - 8 
177ff 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 65 20  entries will be 
17800 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f 72 72  reported as corr
17801 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20  upt.  In order. 
17802 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74       ** to maint
17803 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f  ain backwards co
17804 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68  mpatibility with
17805 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20   older versions 
17806 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 20 20 20  of SQLite,.     
17807 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74   ** we will cont
17808 69 6e 75 65 20 74 6f 20 72 65 73 74 72 69 63 74  inue to restrict
17809 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
1780a 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62 6c 65  ntries to usable
1780b 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 20 20 20  Size/4 - 8.     
1780c 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 41 74   ** for now.  At
1780d 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74   some point in t
1780e 68 65 20 66 75 74 75 72 65 20 28 6f 6e 63 65 20  he future (once 
1780f 65 76 65 72 79 6f 6e 65 20 68 61 73 20 75 70 67  everyone has upg
17810 72 61 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74  raded.      ** t
17811 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 74 65 72  o 3.6.0 or later
17812 29 20 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e 73  ) we should cons
17813 69 64 65 72 20 66 69 78 69 6e 67 20 74 68 65 20  ider fixing the 
17814 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 62 6f 76  conditional abov
17815 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 72 65  e.      ** to re
17816 61 64 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34  ad "usableSize/4
17817 2d 32 22 20 69 6e 73 74 65 61 64 20 6f 66 20 22  -2" instead of "
17818 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38 22 2e  usableSize/4-8".
17819 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1781a 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1781b 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70  rWrite(pTrunk->p
1781c 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  DbPage);.      i
1781d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1781e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34   ){.        put4
1781f 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44  byte(&pTrunk->aD
17820 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 2b 31 29  ata[4], nLeaf+1)
17821 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79  ;.        put4by
17822 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  te(&pTrunk->aDat
17823 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c 20 69 50  a[8+nLeaf*4], iP
17824 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  age);.#ifndef SQ
17825 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45  LITE_SECURE_DELE
17826 54 45 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  TE.        if( p
17827 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Page ){.        
17828 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f    sqlite3PagerDo
17829 6e 74 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70  ntWrite(pPage->p
1782a 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
1782b 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
1782c 20 20 72 63 20 3d 20 62 74 72 65 65 53 65 74 48    rc = btreeSetH
1782d 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 69  asContent(pBt, i
1782e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Page);.      }. 
1782f 20 20 20 20 20 54 52 41 43 45 28 28 22 46 52 45       TRACE(("FRE
17830 45 2d 50 41 47 45 3a 20 25 64 20 6c 65 61 66 20  E-PAGE: %d leaf 
17831 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 20 25 64  on trunk page %d
17832 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 67 6e 6f 2c  \n",pPage->pgno,
17833 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a  pTrunk->pgno));.
17834 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70        goto freep
17835 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  age_out;.    }. 
17836 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74   }..  /* If cont
17837 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74 68 69  rol flows to thi
17838 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 69 74  s point, then it
17839 20 77 61 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c   was not possibl
1783a 65 20 74 6f 20 61 64 64 20 74 68 65 0a 20 20 2a  e to add the.  *
1783b 2a 20 74 68 65 20 70 61 67 65 20 62 65 69 6e 67  * the page being
1783c 20 66 72 65 65 64 20 61 73 20 61 20 6c 65 61 66   freed as a leaf
1783d 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 72   page of the fir
1783e 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 68 65 20  st trunk in the 
1783f 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20  free-list..  ** 
17840 50 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73 65  Possibly because
17841 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69   the free-list i
17842 73 20 65 6d 70 74 79 2c 20 6f 72 20 70 6f 73 73  s empty, or poss
17843 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65  ibly because the
17844 20 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75   .  ** first tru
17845 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c  nk in the free-l
17846 69 73 74 20 69 73 20 66 75 6c 6c 2e 20 45 69 74  ist is full. Eit
17847 68 65 72 20 77 61 79 2c 20 74 68 65 20 70 61 67  her way, the pag
17848 65 20 62 65 69 6e 67 20 66 72 65 65 64 0a 20 20  e being freed.  
17849 2a 2a 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74  ** will become t
1784a 68 65 20 6e 65 77 20 66 69 72 73 74 20 74 72 75  he new first tru
1784b 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 66  nk page in the f
1784c 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20  ree-list..  */. 
1784d 20 69 66 28 20 70 50 61 67 65 3d 3d 30 20 26 26   if( pPage==0 &&
1784e 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
1784f 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
17850 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 67  Bt, iPage, &pPag
17851 65 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 67 6f  e, 0)) ){.    go
17852 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b  to freepage_out;
17853 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69  .  }.  rc = sqli
17854 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
17855 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
17856 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
17857 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66  OK ){.    goto f
17858 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d  reepage_out;.  }
17859 0a 20 20 70 75 74 34 62 79 74 65 28 70 50 61 67  .  put4byte(pPag
1785a 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 75 6e 6b  e->aData, iTrunk
1785b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 70  );.  put4byte(&p
1785c 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c 20  Page->aData[4], 
1785d 30 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26  0);.  put4byte(&
1785e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
1785f 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 54 52 41  ], iPage);.  TRA
17860 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20  CE(("FREE-PAGE: 
17861 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 61 67  %d new trunk pag
17862 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64 5c 6e  e replacing %d\n
17863 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20  ", pPage->pgno, 
17864 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65 65 70  iTrunk));..freep
17865 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70  age_out:.  if( p
17866 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67  Page ){.    pPag
17867 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20  e->isInit = 0;. 
17868 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65   }.  releasePage
17869 28 70 50 61 67 65 29 3b 0a 20 20 72 65 6c 65 61  (pPage);.  relea
1786a 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a  sePage(pTrunk);.
1786b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 73    return rc;.}.s
1786c 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 50  tatic void freeP
1786d 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  age(MemPage *pPa
1786e 67 65 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20  ge, int *pRC){. 
1786f 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 51 4c   if( (*pRC)==SQL
17870 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70  ITE_OK ){.    *p
17871 52 43 20 3d 20 66 72 65 65 50 61 67 65 32 28 70  RC = freePage2(p
17872 50 61 67 65 2d 3e 70 42 74 2c 20 70 50 61 67 65  Page->pBt, pPage
17873 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a  , pPage->pgno);.
17874 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65    }.}../*.** Fre
17875 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70  e any overflow p
17876 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ages associated 
17877 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 43  with the given C
17878 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ell..*/.static i
17879 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 4d 65 6d  nt clearCell(Mem
1787a 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 6e 73  Page *pPage, uns
1787b 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c  igned char *pCel
1787c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  l){.  BtShared *
1787d 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74  pBt = pPage->pBt
1787e 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66  ;.  CellInfo inf
1787f 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 50 67  o;.  Pgno ovflPg
17880 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  no;.  int rc;.  
17881 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 75 31 36  int nOvfl;.  u16
17882 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a 0a   ovflPageSize;..
17883 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17884 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
17885 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
17886 29 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43  );.  btreeParseC
17887 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
17888 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 69  ell, &info);.  i
17889 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  f( info.iOverflo
1788a 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  w==0 ){.    retu
1788b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f  rn SQLITE_OK;  /
1788c 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61  * No overflow pa
1788d 67 65 73 2e 20 52 65 74 75 72 6e 20 77 69 74 68  ges. Return with
1788e 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69  out doing anythi
1788f 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c  ng */.  }.  ovfl
17890 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  Pgno = get4byte(
17891 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65  &pCell[info.iOve
17892 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73 73 65 72  rflow]);.  asser
17893 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  t( pBt->usableSi
17894 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f 76 66 6c  ze > 4 );.  ovfl
17895 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e  PageSize = pBt->
17896 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a  usableSize - 4;.
17897 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e 66 6f 2e    nOvfl = (info.
17898 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e  nPayload - info.
17899 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 61 67  nLocal + ovflPag
1789a 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76 66 6c 50  eSize - 1)/ovflP
1789b 61 67 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72  ageSize;.  asser
1789c 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c  t( ovflPgno==0 |
1789d 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20 20 77  | nOvfl>0 );.  w
1789e 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b  hile( nOvfl-- ){
1789f 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 78 74 20  .    Pgno iNext 
178a0 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65  = 0;.    MemPage
178a1 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 20   *pOvfl = 0;.   
178a2 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3c 32 20   if( ovflPgno<2 
178a3 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 65  || ovflPgno>page
178a4 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20  rPagecount(pBt) 
178a5 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30 20 69 73  ){.      /* 0 is
178a6 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20 70 61 67   not a legal pag
178a7 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67  e number and pag
178a8 65 20 31 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e  e 1 cannot be an
178a9 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66   .      ** overf
178aa 6c 6f 77 20 70 61 67 65 2e 20 54 68 65 72 65 66  low page. Theref
178ab 6f 72 65 20 69 66 20 6f 76 66 6c 50 67 6e 6f 3c  ore if ovflPgno<
178ac 32 20 6f 72 20 70 61 73 74 20 74 68 65 20 65 6e  2 or past the en
178ad 64 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20 20  d of the .      
178ae 2a 2a 20 66 69 6c 65 20 74 68 65 20 64 61 74 61  ** file the data
178af 62 61 73 65 20 6d 75 73 74 20 62 65 20 63 6f 72  base must be cor
178b0 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20 20 20 72  rupt. */.      r
178b1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
178b2 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
178b3 0a 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 29  .    if( nOvfl )
178b4 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 67 65 74  {.      rc = get
178b5 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74  OverflowPage(pBt
178b6 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76  , ovflPgno, &pOv
178b7 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a 20 20 20  fl, &iNext);.   
178b8 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
178b9 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
178ba 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 32 28   rc = freePage2(
178bb 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f 76 66 6c  pBt, pOvfl, ovfl
178bc 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 70  Pgno);.    if( p
178bd 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 73 71  Ovfl ){.      sq
178be 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
178bf 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 3b  pOvfl->pDbPage);
178c0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
178c1 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
178c2 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 69 4e     ovflPgno = iN
178c3 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ext;.  }.  retur
178c4 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
178c5 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65  /*.** Create the
178c6 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 20 75   byte sequence u
178c7 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74  sed to represent
178c8 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65 20   a cell on page 
178c9 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77 72 69  pPage.** and wri
178ca 74 65 20 74 68 61 74 20 62 79 74 65 20 73 65 71  te that byte seq
178cb 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c 6c  uence into pCell
178cc 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70 61  [].  Overflow pa
178cd 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 63  ges are.** alloc
178ce 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65 64 20  ated and filled 
178cf 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e  in as necessary.
178d0 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72    The calling pr
178d1 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20 72 65  ocedure.** is re
178d2 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d 61  sponsible for ma
178d3 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66 69 63  king sure suffic
178d4 69 65 6e 74 20 73 70 61 63 65 20 68 61 73 20 62  ient space has b
178d5 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a  een allocated.**
178d6 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a   for pCell[]..**
178d7 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 70 43  .** Note that pC
178d8 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e 65 63  ell does not nec
178d9 65 73 73 61 72 79 20 6e 65 65 64 20 74 6f 20 70  essary need to p
178da 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 50 61 67  oint to the pPag
178db 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72 65 61  e->aData.** area
178dc 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 70  .  pCell might p
178dd 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74 65 6d  oint to some tem
178de 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 2e 20  porary storage. 
178df 20 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c 0a 2a   The cell will.*
178e0 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65 64  * be constructed
178e1 20 69 6e 20 74 68 69 73 20 74 65 6d 70 6f 72 61   in this tempora
178e2 72 79 20 61 72 65 61 20 74 68 65 6e 20 63 6f 70  ry area then cop
178e3 69 65 64 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e  ied into pPage->
178e4 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72 2e 0a  aData.** later..
178e5 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  */.static int fi
178e6 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 6d 50  llInCell(.  MemP
178e7 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20  age *pPage,     
178e8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
178e9 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74  e page that cont
178ea 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 2a 2f  ains the cell */
178eb 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
178ec 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20   *pCell,        
178ed 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65    /* Complete te
178ee 78 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a  xt of the cell *
178ef 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
178f0 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20  pKey, i64 nKey, 
178f1 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f     /* The key */
178f2 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
178f3 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 61 2c 20  Data,int nData, 
178f4 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f    /* The data */
178f5 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20  .  int nZero,   
178f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
178f7 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 72 6f 20    /* Extra zero 
178f8 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20  bytes to append 
178f9 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 20 69 6e  to pData */.  in
178fa 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 20  t *pnSize       
178fb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
178fc 57 72 69 74 65 20 63 65 6c 6c 20 73 69 7a 65 20  Write cell size 
178fd 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
178fe 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 63 6f 6e   nPayload;.  con
178ff 73 74 20 75 38 20 2a 70 53 72 63 3b 0a 20 20 69  st u8 *pSrc;.  i
17900 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 63 3b 0a  nt nSrc, n, rc;.
17901 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66 74 3b    int spaceLeft;
17902 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66  .  MemPage *pOvf
17903 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65  l = 0;.  MemPage
17904 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 30   *pToRelease = 0
17905 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
17906 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75 6e 73  r *pPrior;.  uns
17907 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 61 79  igned char *pPay
17908 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 72 65 64  load;.  BtShared
17909 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70   *pBt = pPage->p
1790a 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4f  Bt;.  Pgno pgnoO
1790b 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  vfl = 0;.  int n
1790c 48 65 61 64 65 72 3b 0a 20 20 43 65 6c 6c 49 6e  Header;.  CellIn
1790d 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 73 65  fo info;..  asse
1790e 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1790f 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
17910 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  t->mutex) );..  
17911 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20  /* pPage is not 
17912 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72 69 74  necessarily writ
17913 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43 65 6c  eable since pCel
17914 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78 69 6c  l might be auxil
17915 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 66 65 72  iary.  ** buffer
17916 20 73 70 61 63 65 20 74 68 61 74 20 69 73 20 73   space that is s
17917 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65  eparate from the
17918 20 70 50 61 67 65 20 62 75 66 66 65 72 20 61 72   pPage buffer ar
17919 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  ea */.  assert( 
1791a 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 44 61  pCell<pPage->aDa
1791b 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 70 50  ta || pCell>=&pP
1791c 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e  age->aData[pBt->
1791d 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20  pageSize].      
1791e 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
1791f 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
17920 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
17921 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69   );..  /* Fill i
17922 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 2a 2f  n the header. */
17923 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30 3b 0a  .  nHeader = 0;.
17924 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
17925 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65  af ){.    nHeade
17926 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69 66  r += 4;.  }.  if
17927 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61  ( pPage->hasData
17928 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20   ){.    nHeader 
17929 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70 43  += putVarint(&pC
1792a 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e 44  ell[nHeader], nD
1792b 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65  ata+nZero);.  }e
1792c 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 20 3d  lse{.    nData =
1792d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a   nZero = 0;.  }.
1792e 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 74    nHeader += put
1792f 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48  Varint(&pCell[nH
17930 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29 26  eader], *(u64*)&
17931 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 65 50 61  nKey);.  btreePa
17932 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65  rseCellPtr(pPage
17933 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b  , pCell, &info);
17934 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e  .  assert( info.
17935 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 64 65 72  nHeader==nHeader
17936 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e   );.  assert( in
17937 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 29 3b  fo.nKey==nKey );
17938 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e  .  assert( info.
17939 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 6e 44 61  nData==(u32)(nDa
1793a 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20 20 0a  ta+nZero) );.  .
1793b 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65    /* Fill in the
1793c 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 6e 50   payload */.  nP
1793d 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 20 2b  ayload = nData +
1793e 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 70 50   nZero;.  if( pP
1793f 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  age->intKey ){. 
17940 20 20 20 70 53 72 63 20 3d 20 70 44 61 74 61 3b     pSrc = pData;
17941 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74  .    nSrc = nDat
17942 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 30  a;.    nData = 0
17943 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20 20 20  ;.  }else{ .    
17944 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 79 3e 30  if( NEVER(nKey>0
17945 78 37 66 66 66 66 66 66 66 20 7c 7c 20 70 4b 65  x7fffffff || pKe
17946 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 72  y==0) ){.      r
17947 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
17948 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
17949 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2b 3d  .    nPayload +=
1794a 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 20 20   (int)nKey;.    
1794b 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 20 20  pSrc = pKey;.   
1794c 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e 4b 65   nSrc = (int)nKe
1794d 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 7a 65  y;.  }.  *pnSize
1794e 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 20   = info.nSize;. 
1794f 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 6e 66   spaceLeft = inf
17950 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 61 79  o.nLocal;.  pPay
17951 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b 6e 48  load = &pCell[nH
17952 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 6f 72  eader];.  pPrior
17953 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69   = &pCell[info.i
17954 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 77 68  Overflow];..  wh
17955 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e 30 20  ile( nPayload>0 
17956 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 63 65  ){.    if( space
17957 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66 6e 64  Left==0 ){.#ifnd
17958 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
17959 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
1795a 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61 70 20  Pgno pgnoPtrmap 
1795b 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a 20 4f  = pgnoOvfl; /* O
1795c 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69  verflow page poi
1795d 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 70  nter-map entry p
1795e 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  age */.      if(
1795f 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
17960 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 7b 0a   ){.        do{.
17961 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f 4f 76            pgnoOv
17962 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 20  fl++;.        } 
17963 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20 20 20  while( .        
17964 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28    PTRMAP_ISPAGE(
17965 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 20 7c  pBt, pgnoOvfl) |
17966 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 4e 44  | pgnoOvfl==PEND
17967 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
17968 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  t) .        );. 
17969 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
1796a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74      rc = allocat
1796b 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
1796c 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f 76 66  &pOvfl, &pgnoOvf
1796d 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30 29 3b  l, pgnoOvfl, 0);
1796e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1796f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
17970 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
17971 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74  database support
17972 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 61  s auto-vacuum, a
17973 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72  nd the second or
17974 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 20 20   subsequent.    
17975 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61    ** overflow pa
17976 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c 6c 6f  ge is being allo
17977 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 65 6e  cated, add an en
17978 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 6e 74  try to the point
17979 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a 2a 20  er-map.      ** 
1797a 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 6e 6f  for that page no
1797b 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  w. .      **.   
1797c 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73     ** If this is
1797d 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66   the first overf
1797e 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 77  low page, then w
1797f 72 69 74 65 20 61 20 70 61 72 74 69 61 6c 20 65  rite a partial e
17980 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 74  ntry .      ** t
17981 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  o the pointer-ma
17982 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 20 6e  p. If we write n
17983 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 20 70  othing to this p
17984 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f 74 2c  ointer-map slot,
17985 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  .      ** then t
17986 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 6f 76  he optimistic ov
17987 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 72 6f  erflow chain pro
17988 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 61 72  cessing in clear
17989 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a 2a 20  Cell().      ** 
1798a 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 65 74  may misinterpret
1798b 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c 69 73   the uninitialis
1798c 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 64 65  ed values and de
1798d 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a  lete the.      *
1798e 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 66 72  * wrong pages fr
1798f 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  om the database.
17990 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
17991 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
17992 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  uum && rc==SQLIT
17993 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
17994 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e 6f  u8 eType = (pgno
17995 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f 56  Ptrmap?PTRMAP_OV
17996 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f 4f  ERFLOW2:PTRMAP_O
17997 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 20 20  VERFLOW1);.     
17998 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
17999 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 79 70  , pgnoOvfl, eTyp
1799a 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 2c 20 26  e, pgnoPtrmap, &
1799b 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  rc);.        if(
1799c 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20   rc ){.         
1799d 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f 76   releasePage(pOv
1799e 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  fl);.        }. 
1799f 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
179a0 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
179a1 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
179a2 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20  e(pToRelease);. 
179a3 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
179a4 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
179a5 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 73   /* If pToReleas
179a6 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68  e is not zero th
179a7 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e 74 73  an pPrior points
179a8 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 61   into the data a
179a9 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  rea.      ** of 
179aa 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b  pToRelease.  Mak
179ab 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 73  e sure pToReleas
179ac 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65  e is still write
179ad 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61  able. */.      a
179ae 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65 61 73  ssert( pToReleas
179af 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50  e==0 || sqlite3P
179b0 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
179b1 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62 50  pToRelease->pDbP
179b2 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 2f  age) );..      /
179b3 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 20 70  * If pPrior is p
179b4 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20  art of the data 
179b5 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c 20 74  area of pPage, t
179b6 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 70 50  hen make sure pP
179b7 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20  age.      ** is 
179b8 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 20  still writeable 
179b9 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
179ba 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d 3e 61   pPrior<pPage->a
179bb 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72 3e 3d  Data || pPrior>=
179bc 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42  &pPage->aData[pB
179bd 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20  t->pageSize].   
179be 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69           || sqli
179bf 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
179c0 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
179c1 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 70 75  ge) );..      pu
179c2 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 70  t4byte(pPrior, p
179c3 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20  gnoOvfl);.      
179c4 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52  releasePage(pToR
179c5 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 70  elease);.      p
179c6 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f 76 66  ToRelease = pOvf
179c7 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 20  l;.      pPrior 
179c8 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 3b 0a  = pOvfl->aData;.
179c9 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70        put4byte(p
179ca 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 20 20  Prior, 0);.     
179cb 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 4f 76   pPayload = &pOv
179cc 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a 20 20  fl->aData[4];.  
179cd 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20      spaceLeft = 
179ce 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
179cf 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  - 4;.    }.    n
179d0 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20   = nPayload;.   
179d1 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 66 74   if( n>spaceLeft
179d2 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 66 74   ) n = spaceLeft
179d3 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 54 6f  ;..    /* If pTo
179d4 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 7a  Release is not z
179d5 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c 6f 61  ero than pPayloa
179d6 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 68  d points into th
179d7 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 20  e data area.    
179d8 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 65  ** of pToRelease
179d9 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54 6f  .  Make sure pTo
179da 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c 6c  Release is still
179db 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20   writeable. */. 
179dc 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65     assert( pToRe
179dd 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69  lease==0 || sqli
179de 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
179df 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e  ble(pToRelease->
179e0 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20  pDbPage) );..   
179e1 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61 64 20   /* If pPayload 
179e2 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64  is part of the d
179e3 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67  ata area of pPag
179e4 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72  e, then make sur
179e5 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a 20 69  e pPage.    ** i
179e6 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c  s still writeabl
179e7 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  e */.    assert(
179e8 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67 65 2d   pPayload<pPage-
179e9 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79 6c 6f  >aData || pPaylo
179ea 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74  ad>=&pPage->aDat
179eb 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d  a[pBt->pageSize]
179ec 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
179ed 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
179ee 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
179ef 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20  DbPage) );..    
179f0 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a 20 20  if( nSrc>0 ){.  
179f1 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63 20 29      if( n>nSrc )
179f2 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 20 20   n = nSrc;.     
179f3 20 61 73 73 65 72 74 28 20 70 53 72 63 20 29 3b   assert( pSrc );
179f4 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 50  .      memcpy(pP
179f5 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 6e 29  ayload, pSrc, n)
179f6 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
179f7 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 6c 6f     memset(pPaylo
179f8 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20 20 7d  ad, 0, n);.    }
179f9 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2d 3d  .    nPayload -=
179fa 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f 61 64   n;.    pPayload
179fb 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72 63 20   += n;.    pSrc 
179fc 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63 20 2d  += n;.    nSrc -
179fd 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65 4c 65  = n;.    spaceLe
179fe 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69 66 28  ft -= n;.    if(
179ff 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20   nSrc==0 ){.    
17a00 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b 0a    nSrc = nData;.
17a01 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 44 61        pSrc = pDa
17a02 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ta;.    }.  }.  
17a03 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52  releasePage(pToR
17a04 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74 75 72  elease);.  retur
17a05 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
17a06 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  /*.** Remove the
17a07 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f 6d 20   i-th cell from 
17a08 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 6f 75  pPage.  This rou
17a09 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 50 61  tine effects pPa
17a0a 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 65 20  ge only..** The 
17a0b 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20  cell content is 
17a0c 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 65 61  not freed or dea
17a0d 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 69 73  llocated.  It is
17a0e 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a   assumed that.**
17a0f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
17a10 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 69 65  t has been copie
17a11 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c 73 65  d someplace else
17a12 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
17a13 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 73 20  just.** removes 
17a14 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f  the reference to
17a15 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 70   the cell from p
17a16 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 7a 22  Page..**.** "sz"
17a17 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e 75 6d   must be the num
17a18 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
17a19 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 61  the cell..*/.sta
17a1a 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43 65 6c  tic void dropCel
17a1b 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  l(MemPage *pPage
17a1c 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20 73  , int idx, int s
17a1d 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20  z, int *pRC){.  
17a1e 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
17a1f 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
17a20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20  */.  int pc;    
17a21 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
17a22 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f  o cell content o
17a23 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c  f cell being del
17a24 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 2a 64 61  eted */.  u8 *da
17a25 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 70 50 61  ta;       /* pPa
17a26 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 75  ge->aData */.  u
17a27 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 2f  8 *ptr;        /
17a28 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 65 20 62  * Used to move b
17a29 79 74 65 73 20 61 72 6f 75 6e 64 20 77 69 74 68  ytes around with
17a2a 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69  in data[] */.  i
17a2b 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f  nt rc;         /
17a2c 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64  * The return cod
17a2d 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20  e */.  int hdr; 
17a2e 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e         /* Beginn
17a2f 69 6e 67 20 6f 66 20 74 68 65 20 68 65 61 64 65  ing of the heade
17a30 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 67 65 73  r.  0 most pages
17a31 2e 20 20 31 30 30 20 70 61 67 65 20 31 20 2a 2f  .  100 page 1 */
17a32 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72  ..  if( *pRC ) r
17a33 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74  eturn;..  assert
17a34 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 78 3c  ( idx>=0 && idx<
17a35 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  pPage->nCell );.
17a36 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 65    assert( sz==ce
17a37 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20 69 64  llSize(pPage, id
17a38 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
17a39 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
17a3a 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
17a3b 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73  DbPage) );.  ass
17a3c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
17a3d 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
17a3e 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
17a3f 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
17a40 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 26 64 61  ata;.  ptr = &da
17a41 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66  ta[pPage->cellOf
17a42 66 73 65 74 20 2b 20 32 2a 69 64 78 5d 3b 0a 20  fset + 2*idx];. 
17a43 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 70   pc = get2byte(p
17a44 74 72 29 3b 0a 20 20 68 64 72 20 3d 20 70 50 61  tr);.  hdr = pPa
17a45 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20  ge->hdrOffset;. 
17a46 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 67   testcase( pc==g
17a47 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
17a48 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65 73 74 63  r+5]) );.  testc
17a49 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70 50 61 67  ase( pc+sz==pPag
17a4a 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
17a4b 7a 65 20 29 3b 0a 20 20 69 66 28 20 70 63 20 3c  ze );.  if( pc <
17a4c 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
17a4d 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63 2b 73 7a  hdr+5]) || pc+sz
17a4e 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75   > pPage->pBt->u
17a4f 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20  sableSize ){.   
17a50 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43   *pRC = SQLITE_C
17a51 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
17a52 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 72   return;.  }.  r
17a53 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 70 50  c = freeSpace(pP
17a54 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a 20 20  age, pc, sz);.  
17a55 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70  if( rc ){.    *p
17a56 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 65 74  RC = rc;.    ret
17a57 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  urn;.  }.  for(i
17a58 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65 2d  =idx+1; i<pPage-
17a59 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74 72  >nCell; i++, ptr
17a5a 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30 5d  +=2){.    ptr[0]
17a5b 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20 70   = ptr[2];.    p
17a5c 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b 0a  tr[1] = ptr[3];.
17a5d 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65    }.  pPage->nCe
17a5e 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74 65  ll--;.  put2byte
17a5f 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 70  (&data[hdr+3], p
17a60 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20  Page->nCell);.  
17a61 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20  pPage->nFree += 
17a62 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65  2;.}../*.** Inse
17a63 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20 6f 6e  rt a new cell on
17a64 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c 20 69   pPage at cell i
17a65 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65 6c 6c  ndex "i".  pCell
17a66 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a   points to the.*
17a67 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  * content of the
17a68 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   cell..**.** If 
17a69 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
17a6a 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65   will fit on the
17a6b 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75 74 20   page, then put 
17a6c 69 74 20 74 68 65 72 65 2e 20 20 49 66 20 69 74  it there.  If it
17a6d 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74  .** will not fit
17a6e 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20 63 6f  , then make a co
17a6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  py of the cell c
17a70 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54 65 6d  ontent into pTem
17a71 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20 69 73  p if.** pTemp is
17a72 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 67 61   not null.  Rega
17a73 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d 70 2c  rdless of pTemp,
17a74 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
17a75 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50 61 67  entry.** in pPag
17a76 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d  e->aOvfl[] and m
17a77 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 6f 20  ake it point to 
17a78 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
17a79 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e 20 70   (either.** in p
17a7a 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 69 67  Temp or the orig
17a7b 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e 64 20  inal pCell) and 
17a7c 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 73 20  also record its 
17a7d 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f 63  index. .** Alloc
17a7e 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 72  ating a new entr
17a7f 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43 65 6c  y in pPage->aCel
17a80 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68 61 74  l[] implies that
17a81 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 65   .** pPage->nOve
17a82 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65 6d 65  rflow is increme
17a83 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  nted..**.** If n
17a84 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  Skip is non-zero
17a85 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 6f  , then do not co
17a86 70 79 20 74 68 65 20 66 69 72 73 74 20 6e 53 6b  py the first nSk
17a87 69 70 20 62 79 74 65 73 20 6f 66 20 74 68 65 0a  ip bytes of the.
17a88 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 61 6c  ** cell. The cal
17a89 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77 72 69  ler will overwri
17a8a 74 65 20 74 68 65 6d 20 61 66 74 65 72 20 74 68  te them after th
17a8b 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
17a8c 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 70  rns. If.** nSkip
17a8d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
17a8e 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e 6f 74  en pCell may not
17a8f 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69 6e 76   point to an inv
17a90 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 61  alid memory loca
17a91 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20 70 43  tion .** (but pC
17a92 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61 6c 77  ell+nSkip is alw
17a93 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a 73  ays valid)..*/.s
17a94 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73 65 72  tatic void inser
17a95 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65  tCell(.  MemPage
17a96 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50 61   *pPage,   /* Pa
17a97 67 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65  ge into which we
17a98 20 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f 0a   are copying */.
17a99 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20    int i,        
17a9a 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c 20      /* New cell 
17a9b 62 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74 68  becomes the i-th
17a9c 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61 67   cell of the pag
17a9d 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c  e */.  u8 *pCell
17a9e 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74  ,        /* Cont
17a9f 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 63  ent of the new c
17aa0 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c  ell */.  int sz,
17aa1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
17aa2 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69  tes of content i
17aa3 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20  n pCell */.  u8 
17aa4 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f  *pTemp,        /
17aa5 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 73  * Temp storage s
17aa6 70 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c 20  pace for pCell, 
17aa7 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 50  if needed */.  P
17aa8 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20  gno iChild,     
17aa9 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 2c   /* If non-zero,
17aaa 20 72 65 70 6c 61 63 65 20 66 69 72 73 74 20 34   replace first 4
17aab 20 62 79 74 65 73 20 77 69 74 68 20 74 68 69 73   bytes with this
17aac 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20   value */.  int 
17aad 2a 70 52 43 20 20 20 20 20 20 20 20 20 20 2f 2a  *pRC          /*
17aae 20 52 65 61 64 20 61 6e 64 20 77 72 69 74 65 20   Read and write 
17aaf 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  return code from
17ab0 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
17ab1 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  t idx;          
17ab2 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 74  /* Where to writ
17ab3 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f 6e 74 65  e new cell conte
17ab4 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a  nt in data[] */.
17ab5 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20    int j;        
17ab6 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
17ab7 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64  ter */.  int end
17ab8 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ;          /* Fi
17ab9 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68  rst byte past th
17aba 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e  e last cell poin
17abb 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f  ter in data[] */
17abc 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 20  .  int ins;     
17abd 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
17abe 20 64 61 74 61 5b 5d 20 77 68 65 72 65 20 6e 65   data[] where ne
17abf 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69  w cell pointer i
17ac0 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  s inserted */.  
17ac1 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20  int cellOffset; 
17ac2 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
17ac3 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74  first cell point
17ac4 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a  er in data[] */.
17ac5 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20    u8 *data;     
17ac6 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65      /* The conte
17ac7 6e 74 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 20  nt of the whole 
17ac8 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74  page */.  u8 *pt
17ac9 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  r;          /* U
17aca 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69  sed for moving i
17acb 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e  nformation aroun
17acc 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a  d in data[] */..
17acd 20 20 69 6e 74 20 6e 53 6b 69 70 20 3d 20 28 69    int nSkip = (i
17ace 43 68 69 6c 64 20 3f 20 34 20 3a 20 30 29 3b 0a  Child ? 4 : 0);.
17acf 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65  .  if( *pRC ) re
17ad0 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28  turn;..  assert(
17ad1 20 69 3e 3d 30 20 26 26 20 69 3c 3d 70 50 61 67   i>=0 && i<=pPag
17ad2 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 67 65 2d 3e  e->nCell+pPage->
17ad3 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 61  nOverflow );.  a
17ad4 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43  ssert( pPage->nC
17ad5 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 50 61  ell<=MX_CELL(pPa
17ad6 67 65 2d 3e 70 42 74 29 20 26 26 20 4d 58 5f 43  ge->pBt) && MX_C
17ad7 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 3c  ELL(pPage->pBt)<
17ad8 3d 35 34 36 30 20 29 3b 0a 20 20 61 73 73 65 72  =5460 );.  asser
17ad9 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
17ada 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a 65 28 70  low<=ArraySize(p
17adb 50 61 67 65 2d 3e 61 4f 76 66 6c 29 20 29 3b 0a  Page->aOvfl) );.
17adc 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 65    assert( sz==ce
17add 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c  llSizePtr(pPage,
17ade 20 70 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73   pCell) );.  ass
17adf 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
17ae0 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
17ae1 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
17ae2 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  if( pPage->nOver
17ae3 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e 70 50 61  flow || sz+2>pPa
17ae4 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20  ge->nFree ){.   
17ae5 20 69 66 28 20 70 54 65 6d 70 20 29 7b 0a 20 20   if( pTemp ){.  
17ae6 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70      memcpy(pTemp
17ae7 2b 6e 53 6b 69 70 2c 20 70 43 65 6c 6c 2b 6e 53  +nSkip, pCell+nS
17ae8 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a  kip, sz-nSkip);.
17ae9 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54        pCell = pT
17aea 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  emp;.    }.    i
17aeb 66 28 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 20  f( iChild ){.   
17aec 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65 6c     put4byte(pCel
17aed 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20  l, iChild);.    
17aee 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d  }.    j = pPage-
17aef 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20  >nOverflow++;.  
17af0 20 20 61 73 73 65 72 74 28 20 6a 3c 28 69 6e 74    assert( j<(int
17af1 29 28 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e  )(sizeof(pPage->
17af2 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70 50  aOvfl)/sizeof(pP
17af3 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 20  age->aOvfl[0])) 
17af4 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f  );.    pPage->aO
17af5 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20 70  vfl[j].pCell = p
17af6 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d  Cell;.    pPage-
17af7 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d 20  >aOvfl[j].idx = 
17af8 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c 73 65 7b  (u16)i;.  }else{
17af9 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71  .    int rc = sq
17afa 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
17afb 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
17afc 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
17afd 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
17afe 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 20  *pRC = rc;.     
17aff 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
17b00 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
17b01 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
17b02 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
17b03 65 29 20 29 3b 0a 20 20 20 20 64 61 74 61 20 3d  e) );.    data =
17b04 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20   pPage->aData;. 
17b05 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20     cellOffset = 
17b06 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65  pPage->cellOffse
17b07 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 63 65 6c  t;.    end = cel
17b08 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67  lOffset + 2*pPag
17b09 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 6e  e->nCell;.    in
17b0a 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b  s = cellOffset +
17b0b 20 32 2a 69 3b 0a 20 20 20 20 72 63 20 3d 20 61   2*i;.    rc = a
17b0c 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 61  llocateSpace(pPa
17b0d 67 65 2c 20 73 7a 2c 20 26 69 64 78 29 3b 0a 20  ge, sz, &idx);. 
17b0e 20 20 20 69 66 28 20 72 63 20 29 7b 20 2a 70 52     if( rc ){ *pR
17b0f 43 20 3d 20 72 63 3b 20 72 65 74 75 72 6e 3b 20  C = rc; return; 
17b10 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 6c 6c  }.    /* The all
17b11 6f 63 61 74 65 53 70 61 63 65 28 29 20 72 6f 75  ocateSpace() rou
17b12 74 69 6e 65 20 67 75 61 72 61 6e 74 65 65 73 20  tine guarantees 
17b13 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  the following tw
17b14 6f 20 70 72 6f 70 65 72 74 69 65 73 0a 20 20 20  o properties.   
17b15 20 2a 2a 20 69 66 20 69 74 20 72 65 74 75 72 6e   ** if it return
17b16 73 20 73 75 63 63 65 73 73 20 2a 2f 0a 20 20 20  s success */.   
17b17 20 61 73 73 65 72 74 28 20 69 64 78 20 3e 3d 20   assert( idx >= 
17b18 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20 61 73 73  end+2 );.    ass
17b19 65 72 74 28 20 69 64 78 2b 73 7a 20 3c 3d 20 70  ert( idx+sz <= p
17b1a 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
17b1b 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 70 50 61  eSize );.    pPa
17b1c 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20  ge->nCell++;.   
17b1d 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d   pPage->nFree -=
17b1e 20 28 75 31 36 29 28 32 20 2b 20 73 7a 29 3b 0a   (u16)(2 + sz);.
17b1f 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61      memcpy(&data
17b20 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65  [idx+nSkip], pCe
17b21 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b  ll+nSkip, sz-nSk
17b22 69 70 29 3b 0a 20 20 20 20 69 66 28 20 69 43 68  ip);.    if( iCh
17b23 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 75 74  ild ){.      put
17b24 34 62 79 74 65 28 26 64 61 74 61 5b 69 64 78 5d  4byte(&data[idx]
17b25 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 7d  , iChild);.    }
17b26 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e 64 2c 20  .    for(j=end, 
17b27 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b 20 6a 3e  ptr=&data[j]; j>
17b28 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74 72 2d 3d  ins; j-=2, ptr-=
17b29 32 29 7b 0a 20 20 20 20 20 20 70 74 72 5b 30 5d  2){.      ptr[0]
17b2a 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20 20 20 20   = ptr[-2];.    
17b2b 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 2d    ptr[1] = ptr[-
17b2c 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75  1];.    }.    pu
17b2d 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 6e 73  t2byte(&data[ins
17b2e 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 70 75 74  ], idx);.    put
17b2f 32 62 79 74 65 28 26 64 61 74 61 5b 70 50 61 67  2byte(&data[pPag
17b30 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33 5d 2c  e->hdrOffset+3],
17b31 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a   pPage->nCell);.
17b32 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
17b33 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
17b34 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42     if( pPage->pB
17b35 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
17b36 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65  .      /* The ce
17b37 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61  ll may contain a
17b38 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f   pointer to an o
17b39 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66  verflow page. If
17b3a 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 20   so, write.     
17b3b 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 6f   ** the entry fo
17b3c 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  r the overflow p
17b3d 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f 69  age into the poi
17b3e 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 20  nter map..      
17b3f 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50  */.      ptrmapP
17b40 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c  utOvflPtr(pPage,
17b41 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b 0a 20 20   pCell, pRC);.  
17b42 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
17b43 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69  ../*.** Add a li
17b44 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61  st of cells to a
17b45 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65   page.  The page
17b46 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69   should be initi
17b47 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54  ally empty..** T
17b48 68 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75 61  he cells are gua
17b49 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 6f  ranteed to fit o
17b4a 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73  n the page..*/.s
17b4b 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d  tatic void assem
17b4c 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 61  blePage(.  MemPa
17b4d 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20  ge *pPage,   /* 
17b4e 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 61  The page to be a
17b4f 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69  ssemblied */.  i
17b50 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20  nt nCell,       
17b51 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f   /* The number o
17b52 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74  f cells to add t
17b53 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20  o this page */. 
17b54 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20   u8 **apCell,   
17b55 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74     /* Pointers t
17b56 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f  o cell bodies */
17b57 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20 20  .  u16 *aSize   
17b58 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66       /* Sizes of
17b59 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b   the cells */.){
17b5a 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
17b5b 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
17b5c 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70 43  nter */.  u8 *pC
17b5d 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20 41  ellptr;     /* A
17b5e 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63  ddress of next c
17b5f 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ell pointer */. 
17b60 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20   int cellbody;  
17b61 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
17b62 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20   next cell body 
17b63 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20  */.  u8 * const 
17b64 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
17b65 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
17b66 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64   /* Pointer to d
17b67 61 74 61 20 66 6f 72 20 70 50 61 67 65 20 2a 2f  ata for pPage */
17b68 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72  .  const int hdr
17b69 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
17b6a 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  set;           /
17b6b 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64  * Offset of head
17b6c 65 72 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20  er on pPage */. 
17b6d 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61 62   const int nUsab
17b6e 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d  le = pPage->pBt-
17b6f 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20  >usableSize; /* 
17b70 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 70  Usable size of p
17b71 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  age */..  assert
17b72 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
17b73 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ow==0 );.  asser
17b74 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
17b75 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
17b76 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
17b77 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 26  sert( nCell>=0 &
17b78 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c  & nCell<=MX_CELL
17b79 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20  (pPage->pBt) && 
17b7a 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70  MX_CELL(pPage->p
17b7b 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61  Bt)<=5460 );.  a
17b7c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
17b7d 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
17b7e 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
17b7f 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  ;..  /* Check th
17b80 61 74 20 74 68 65 20 70 61 67 65 20 68 61 73 20  at the page has 
17b81 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 6f 65 64  just been zeroed
17b82 20 62 79 20 7a 65 72 6f 50 61 67 65 28 29 20 2a   by zeroPage() *
17b83 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  /.  assert( pPag
17b84 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20  e->nCell==0 );. 
17b85 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79 74   assert( get2byt
17b86 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d  e(&data[hdr+5])=
17b87 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 70  =nUsable );..  p
17b88 43 65 6c 6c 70 74 72 20 3d 20 26 64 61 74 61 5b  Cellptr = &data[
17b89 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65  pPage->cellOffse
17b8a 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20  t + nCell*2];.  
17b8b 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61 62  cellbody = nUsab
17b8c 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c  le;.  for(i=nCel
17b8d 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b  l-1; i>=0; i--){
17b8e 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 20 2d 3d  .    pCellptr -=
17b8f 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62 6f 64 79   2;.    cellbody
17b90 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20   -= aSize[i];.  
17b91 20 20 70 75 74 32 62 79 74 65 28 70 43 65 6c 6c    put2byte(pCell
17b92 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a  ptr, cellbody);.
17b93 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61      memcpy(&data
17b94 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43 65  [cellbody], apCe
17b95 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 5d 29  ll[i], aSize[i])
17b96 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 79 74 65  ;.  }.  put2byte
17b97 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e  (&data[hdr+3], n
17b98 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 74  Cell);.  put2byt
17b99 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20  e(&data[hdr+5], 
17b9a 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61  cellbody);.  pPa
17b9b 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43  ge->nFree -= (nC
17b9c 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20  ell*2 + nUsable 
17b9d 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70  - cellbody);.  p
17b9e 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75  Page->nCell = (u
17b9f 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  16)nCell;.}../*.
17ba0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
17ba1 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65   parameters dete
17ba2 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61  rmine how many a
17ba3 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65  djacent pages ge
17ba4 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e  t involved.** in
17ba5 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65   a balancing ope
17ba6 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74  ration.  NN is t
17ba7 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69  he number of nei
17ba8 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72  ghbors on either
17ba9 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20   side.** of the 
17baa 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63  page that partic
17bab 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c  ipate in the bal
17bac 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ancing operation
17bad 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20  .  NB is the.** 
17bae 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
17baf 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69  pages that parti
17bb0 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e  cipate, includin
17bb1 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67  g the target pag
17bb2 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67  e and.** NN neig
17bb3 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20  hbors on either 
17bb4 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  side..**.** The 
17bb5 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  minimum value of
17bb6 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75   NN is 1 (of cou
17bb7 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e  rse).  Increasin
17bb8 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20  g NN above 1.** 
17bb9 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65  (to 2 or 3) give
17bba 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f  s a modest impro
17bbb 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54  vement in SELECT
17bbc 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66   and DELETE perf
17bbd 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78  ormance.** in ex
17bbe 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72  change for a lar
17bbf 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20  ger degradation 
17bc0 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50  in INSERT and UP
17bc1 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65  DATE performance
17bc2 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f  ..** The value o
17bc3 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20  f NN appears to 
17bc4 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65  give the best re
17bc5 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a  sults overall..*
17bc6 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20  /.#define NN 1  
17bc7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
17bc8 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72  mber of neighbor
17bc9 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65  s on either side
17bca 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65   of pPage */.#de
17bcb 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29  fine NB (NN*2+1)
17bcc 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70        /* Total p
17bcd 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e  ages involved in
17bce 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a   the balance */.
17bcf 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
17bd0 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e  _OMIT_QUICKBALAN
17bd1 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65  CE./*.** This ve
17bd2 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65  rsion of balance
17bd3 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 63  () handles the c
17bd4 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61  ommon special ca
17bd5 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65  se where.** a ne
17bd6 77 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e 67  w entry is being
17bd7 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 65   inserted on the
17bd8 20 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d 65   extreme right-e
17bd9 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65  nd of the.** tre
17bda 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64  e, in other word
17bdb 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 20  s, when the new 
17bdc 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d  entry will becom
17bdd 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a  e the largest.**
17bde 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72   entry in the tr
17bdf 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61  ee..**.** Instea
17be0 64 20 6f 66 20 74 72 79 69 6e 67 20 74 6f 20 62  d of trying to b
17be1 61 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 67  alance the 3 rig
17be2 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 67  ht-most leaf pag
17be3 65 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a 20  es, just add.** 
17be4 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68  a new page to th
17be5 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
17be6 65 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f 6e  e and put the on
17be7 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a 2a  e new entry in.*
17be8 2a 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68  * that page.  Th
17be9 69 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 69  is leaves the ri
17bea 67 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 20  ght side of the 
17beb 74 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a 2a  tree somewhat.**
17bec 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75   unbalanced.  Bu
17bed 74 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 20  t odds are that 
17bee 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72  we will be inser
17bef 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 73  ting new entries
17bf0 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73  .** at the end s
17bf1 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 73  oon afterwards s
17bf2 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70  o the nearly emp
17bf3 74 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69  ty page will qui
17bf4 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e  ckly.** fill up.
17bf5 20 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a 2a    On average..**
17bf6 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 65  .** pPage is the
17bf7 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68   leaf page which
17bf8 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f   is the right-mo
17bf9 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 74  st page in the t
17bfa 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 20  ree..** pParent 
17bfb 69 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20  is its parent.  
17bfc 70 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 20  pPage must have 
17bfd 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c 6f  a single overflo
17bfe 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 68  w entry.** which
17bff 20 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 67   is also the rig
17c00 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f 6e  ht-most entry on
17c01 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
17c02 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66   The pSpace buff
17c03 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  er is used to st
17c04 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ore a temporary 
17c05 63 6f 70 79 20 6f 66 20 74 68 65 20 64 69 76 69  copy of the divi
17c06 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 68 61 74  der.** cell that
17c07 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 65   will be inserte
17c08 64 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20  d into pParent. 
17c09 53 75 63 68 20 61 20 63 65 6c 6c 20 63 6f 6e 73  Such a cell cons
17c0a 69 73 74 73 20 6f 66 20 61 20 34 0a 2a 2a 20 62  ists of a 4.** b
17c0b 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  yte page number 
17c0c 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 61  followed by a va
17c0d 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e  riable length in
17c0e 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 65 72 0a  teger. In other.
17c0f 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 6d 6f 73  ** words, at mos
17c10 74 20 31 33 20 62 79 74 65 73 2e 20 48 65 6e 63  t 13 bytes. Henc
17c11 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66  e the pSpace buf
17c12 66 65 72 20 6d 75 73 74 20 62 65 20 61 74 0a 2a  fer must be at.*
17c13 2a 20 6c 65 61 73 74 20 31 33 20 62 79 74 65 73  * least 13 bytes
17c14 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61   in size..*/.sta
17c15 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f  tic int balance_
17c16 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 20 2a 70  quick(MemPage *p
17c17 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 67 65 20  Parent, MemPage 
17c18 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 53 70 61  *pPage, u8 *pSpa
17c19 63 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ce){.  BtShared 
17c1a 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 50 61  *const pBt = pPa
17c1b 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 42  ge->pBt;    /* B
17c1c 2d 54 72 65 65 20 44 61 74 61 62 61 73 65 20 2a  -Tree Database *
17c1d 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65  /.  MemPage *pNe
17c1e 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
17c1f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 6c           /* Newl
17c20 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65  y allocated page
17c21 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
17c22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c23 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
17c24 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 50  turn Code */.  P
17c25 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20 20  gno pgnoNew;    
17c26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c27 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
17c28 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0a 0a 20  er of pNew */.. 
17c29 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17c2a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
17c2b 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
17c2c 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
17c2d 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
17c2e 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62  ble(pParent->pDb
17c2f 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72  Page) );.  asser
17c30 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
17c31 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 69 66 28  low==1 );..  if(
17c32 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d 30   pPage->nCell<=0
17c33 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
17c34 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a  _CORRUPT_BKPT;..
17c35 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20    /* Allocate a 
17c36 6e 65 77 20 70 61 67 65 2e 20 54 68 69 73 20 70  new page. This p
17c37 61 67 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20  age will become 
17c38 74 68 65 20 72 69 67 68 74 2d 73 69 62 6c 69 6e  the right-siblin
17c39 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50 61 67 65  g of .  ** pPage
17c3a 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 72 65 6e  . Make the paren
17c3b 74 20 70 61 67 65 20 77 72 69 74 61 62 6c 65 2c  t page writable,
17c3c 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 77   so that the new
17c3d 20 64 69 76 69 64 65 72 20 63 65 6c 6c 0a 20 20   divider cell.  
17c3e 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73 65 72 74  ** may be insert
17c3f 65 64 2e 20 49 66 20 62 6f 74 68 20 74 68 65 73  ed. If both thes
17c40 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65  e operations are
17c41 20 73 75 63 63 65 73 73 66 75 6c 2c 20 70 72 6f   successful, pro
17c42 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  ceed..  */.  rc 
17c43 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  = allocateBtreeP
17c44 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20  age(pBt, &pNew, 
17c45 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 29 3b  &pgnoNew, 0, 0);
17c46 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
17c47 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20 20 75 38  TE_OK ){..    u8
17c48 20 2a 70 4f 75 74 20 3d 20 26 70 53 70 61 63 65   *pOut = &pSpace
17c49 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a 70 43 65  [4];.    u8 *pCe
17c4a 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76 66  ll = pPage->aOvf
17c4b 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 20  l[0].pCell;.    
17c4c 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c  u16 szCell = cel
17c4d 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20  lSizePtr(pPage, 
17c4e 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75 38 20 2a  pCell);.    u8 *
17c4f 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61 73 73 65  pStop;..    asse
17c50 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
17c51 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 77  Iswriteable(pNew
17c52 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
17c53 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
17c54 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50 54 46 5f  >aData[0]==(PTF_
17c55 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44  INTKEY|PTF_LEAFD
17c56 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 20 29 3b  ATA|PTF_LEAF) );
17c57 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e  .    zeroPage(pN
17c58 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50  ew, PTF_INTKEY|P
17c59 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 5f  TF_LEAFDATA|PTF_
17c5a 4c 45 41 46 29 3b 0a 20 20 20 20 61 73 73 65 6d  LEAF);.    assem
17c5b 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 31 2c  blePage(pNew, 1,
17c5c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c   &pCell, &szCell
17c5d 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  );..    /* If th
17c5e 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61  is is an auto-va
17c5f 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75  cuum database, u
17c60 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65  pdate the pointe
17c61 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 77 69 74  r map.    ** wit
17c62 68 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68  h entries for th
17c63 65 20 6e 65 77 20 70 61 67 65 2c 20 61 6e 64 20  e new page, and 
17c64 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 6f 6d  any pointer from
17c65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 65 6c   the .    ** cel
17c66 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 74 6f  l on the page to
17c67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
17c68 65 2e 20 49 66 20 65 69 74 68 65 72 20 6f 66 20  e. If either of 
17c69 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 6f 70 65  these.    ** ope
17c6a 72 61 74 69 6f 6e 73 20 66 61 69 6c 73 2c 20 74  rations fails, t
17c6b 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69  he return code i
17c6c 73 20 73 65 74 2c 20 62 75 74 20 74 68 65 20 63  s set, but the c
17c6d 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f  ontents.    ** o
17c6e 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  f the parent pag
17c6f 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 6e 69  e are still mani
17c70 70 75 6c 61 74 65 64 20 62 79 20 74 68 68 20 63  pulated by thh c
17c71 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a  ode below..    *
17c72 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c 20 61 74  * That is Ok, at
17c73 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20   this point the 
17c74 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 67  parent page is g
17c75 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 20 20 20  uaranteed to.   
17c76 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64 20 61 73   ** be marked as
17c77 20 64 69 72 74 79 2e 20 52 65 74 75 72 6e 69 6e   dirty. Returnin
17c78 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  g an error code 
17c79 77 69 6c 6c 20 63 61 75 73 65 20 61 0a 20 20 20  will cause a.   
17c7a 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 75 6e   ** rollback, un
17c7b 64 6f 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 65  doing any change
17c7c 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20 70 61  s made to the pa
17c7d 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a  rent page..    *
17c7e 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f  /.    if( ISAUTO
17c7f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20  VACUUM ){.      
17c80 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70  ptrmapPut(pBt, p
17c81 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f 42  gnoNew, PTRMAP_B
17c82 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70  TREE, pParent->p
17c83 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20  gno, &rc);.     
17c84 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70 4e 65 77   if( szCell>pNew
17c85 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20  ->minLocal ){.  
17c86 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f        ptrmapPutO
17c87 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 70 43 65  vflPtr(pNew, pCe
17c88 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20  ll, &rc);.      
17c89 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
17c8a 2a 20 43 72 65 61 74 65 20 61 20 64 69 76 69 64  * Create a divid
17c8b 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e 73 65 72  er cell to inser
17c8c 74 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20  t into pParent. 
17c8d 54 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c  The divider cell
17c8e 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 73 74 73  .    ** consists
17c8f 20 6f 66 20 61 20 34 2d 62 79 74 65 20 70 61 67   of a 4-byte pag
17c90 65 20 6e 75 6d 62 65 72 20 28 74 68 65 20 70 61  e number (the pa
17c91 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 50 61  ge number of pPa
17c92 67 65 29 20 61 6e 64 0a 20 20 20 20 2a 2a 20 61  ge) and.    ** a
17c93 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   variable length
17c94 20 6b 65 79 20 76 61 6c 75 65 20 28 77 68 69 63   key value (whic
17c95 68 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61  h must be the sa
17c96 6d 65 20 76 61 6c 75 65 20 61 73 20 74 68 65 0a  me value as the.
17c97 20 20 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b      ** largest k
17c98 65 79 20 6f 6e 20 70 50 61 67 65 29 2e 0a 20 20  ey on pPage)..  
17c99 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 66    **.    ** To f
17c9a 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74 20  ind the largest 
17c9b 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20 70 50 61  key value on pPa
17c9c 67 65 2c 20 66 69 72 73 74 20 66 69 6e 64 20 74  ge, first find t
17c9d 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 0a 20  he right-most . 
17c9e 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50     ** cell on pP
17c9f 61 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 74  age. The first t
17ca0 77 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69  wo fields of thi
17ca1 73 20 63 65 6c 6c 20 61 72 65 20 74 68 65 20 0a  s cell are the .
17ca2 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2d 6c 65      ** record-le
17ca3 6e 67 74 68 20 28 61 20 76 61 72 69 61 62 6c 65  ngth (a variable
17ca4 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20   length integer 
17ca5 61 74 20 6d 6f 73 74 20 33 32 2d 62 69 74 73 20  at most 32-bits 
17ca6 69 6e 20 73 69 7a 65 29 0a 20 20 20 20 2a 2a 20  in size).    ** 
17ca7 61 6e 64 20 74 68 65 20 6b 65 79 20 76 61 6c 75  and the key valu
17ca8 65 20 28 61 20 76 61 72 69 61 62 6c 65 20 6c 65  e (a variable le
17ca9 6e 67 74 68 20 69 6e 74 65 67 65 72 2c 20 6d 61  ngth integer, ma
17caa 79 20 68 61 76 65 20 61 6e 79 20 76 61 6c 75 65  y have any value
17cab 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69  )..    ** The fi
17cac 72 73 74 20 6f 66 20 74 68 65 20 77 68 69 6c 65  rst of the while
17cad 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62 65 6c 6f  (...) loops belo
17cae 77 20 73 6b 69 70 73 20 6f 76 65 72 20 74 68 65  w skips over the
17caf 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68 0a 20   record-length. 
17cb0 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20 54 68 65     ** field. The
17cb1 20 73 65 63 6f 6e 64 20 77 68 69 6c 65 28 2e 2e   second while(..
17cb2 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65 73 20 74  .) loop copies t
17cb3 68 65 20 6b 65 79 20 76 61 6c 75 65 20 66 72 6f  he key value fro
17cb4 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 65 6c  m the.    ** cel
17cb5 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e 74 6f 20  l on pPage into 
17cb6 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65  the pSpace buffe
17cb7 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43  r..    */.    pC
17cb8 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
17cb9 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43 65  Page, pPage->nCe
17cba 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53 74 6f 70  ll-1);.    pStop
17cbb 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20   = &pCell[9];.  
17cbc 20 20 77 68 69 6c 65 28 20 28 2a 28 70 43 65 6c    while( (*(pCel
17cbd 6c 2b 2b 29 26 30 78 38 30 29 20 26 26 20 70 43  l++)&0x80) && pC
17cbe 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a 20 20 20  ell<pStop );.   
17cbf 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b   pStop = &pCell[
17cc0 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28  9];.    while( (
17cc1 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20 2a 28 70  (*(pOut++) = *(p
17cc2 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30 29 20 26  Cell++))&0x80) &
17cc3 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b  & pCell<pStop );
17cc4 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20  ..    /* Insert 
17cc5 74 68 65 20 6e 65 77 20 64 69 76 69 64 65 72 20  the new divider 
17cc6 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61 72 65 6e  cell into pParen
17cc7 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73 65 72 74  t. */.    insert
17cc8 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 50  Cell(pParent, pP
17cc9 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c 20 70 53  arent->nCell, pS
17cca 70 61 63 65 2c 20 28 69 6e 74 29 28 70 4f 75 74  pace, (int)(pOut
17ccb 2d 70 53 70 61 63 65 29 2c 0a 20 20 20 20 20 20  -pSpace),.      
17ccc 20 20 20 20 20 20 20 20 20 30 2c 20 70 50 61 67           0, pPag
17ccd 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 0a  e->pgno, &rc);..
17cce 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 72      /* Set the r
17ccf 69 67 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e 74  ight-child point
17cd0 65 72 20 6f 66 20 70 50 61 72 65 6e 74 20 74 6f  er of pParent to
17cd1 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65   point to the ne
17cd2 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 70  w page. */.    p
17cd3 75 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74  ut4byte(&pParent
17cd4 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d  ->aData[pParent-
17cd5 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70  >hdrOffset+8], p
17cd6 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 20 20 20  gnoNew);.  .    
17cd7 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 72  /* Release the r
17cd8 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
17cd9 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20  new page. */.   
17cda 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65   releasePage(pNe
17cdb 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  w);.  }..  retur
17cdc 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  n rc;.}.#endif /
17cdd 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55  * SQLITE_OMIT_QU
17cde 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 23  ICKBALANCE */..#
17cdf 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  if 0./*.** This 
17ce0 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  function does no
17ce1 74 20 63 6f 6e 74 72 69 62 75 74 65 20 61 6e 79  t contribute any
17ce2 74 68 69 6e 67 20 74 6f 20 74 68 65 20 6f 70 65  thing to the ope
17ce3 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65  ration of SQLite
17ce4 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f 6d 65 74  ..** it is somet
17ce5 69 6d 65 73 20 61 63 74 69 76 61 74 65 64 20 74  imes activated t
17ce6 65 6d 70 6f 72 61 72 69 6c 79 20 77 68 69 6c 65  emporarily while
17ce7 20 64 65 62 75 67 67 69 6e 67 20 63 6f 64 65 20   debugging code 
17ce8 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a 2a 2a 20  responsible .** 
17ce9 66 6f 72 20 73 65 74 74 69 6e 67 20 70 6f 69 6e  for setting poin
17cea 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 2e  ter-map entries.
17ceb 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
17cec 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28  trmapCheckPages(
17ced 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50 61 67 65  MemPage **apPage
17cee 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20  , int nPage){.  
17cef 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 6f 72 28  int i, j;.  for(
17cf0 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b 20 69 2b  i=0; i<nPage; i+
17cf1 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 3b 0a  +){.    Pgno n;.
17cf2 20 20 20 20 75 38 20 65 3b 0a 20 20 20 20 4d 65      u8 e;.    Me
17cf3 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 61  mPage *pPage = a
17cf4 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20 20 42 74  pPage[i];.    Bt
17cf5 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50  Shared *pBt = pP
17cf6 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73  age->pBt;.    as
17cf7 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49  sert( pPage->isI
17cf8 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28  nit );..    for(
17cf9 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d 3e 6e 43  j=0; j<pPage->nC
17cfa 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  ell; j++){.     
17cfb 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
17cfc 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a 20 20 20        u8 *z;.   
17cfd 20 20 0a 20 20 20 20 20 20 7a 20 3d 20 66 69 6e    .      z = fin
17cfe 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6a 29 3b  dCell(pPage, j);
17cff 0a 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73  .      btreePars
17d00 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20  eCellPtr(pPage, 
17d01 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20  z, &info);.     
17d02 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66   if( info.iOverf
17d03 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 50  low ){.        P
17d04 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62  gno ovfl = get4b
17d05 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69 4f 76 65  yte(&z[info.iOve
17d06 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20 20  rflow]);.       
17d07 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20   ptrmapGet(pBt, 
17d08 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29 3b 0a 20  ovfl, &e, &n);. 
17d09 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e         assert( n
17d0a 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26  ==pPage->pgno &&
17d0b 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46   e==PTRMAP_OVERF
17d0c 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a  LOW1 );.      }.
17d0d 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65        if( !pPage
17d0e 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
17d0f 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d 20 67    Pgno child = g
17d10 65 74 34 62 79 74 65 28 7a 29 3b 0a 20 20 20 20  et4byte(z);.    
17d11 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42      ptrmapGet(pB
17d12 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e  t, child, &e, &n
17d13 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
17d14 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e  t( n==pPage->pgn
17d15 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42  o && e==PTRMAP_B
17d16 54 52 45 45 20 29 3b 0a 20 20 20 20 20 20 7d 0a  TREE );.      }.
17d17 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70      }.    if( !p
17d18 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
17d19 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d      Pgno child =
17d1a 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
17d1b 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
17d1c 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
17d1d 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42      ptrmapGet(pB
17d1e 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e  t, child, &e, &n
17d1f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
17d20 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20   n==pPage->pgno 
17d21 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52  && e==PTRMAP_BTR
17d22 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  EE );.    }.  }.
17d23 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
17d24 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
17d25 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
17d26 64 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 6f  d to copy the co
17d27 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 2d  ntents of the b-
17d28 74 72 65 65 20 6e 6f 64 65 20 73 74 6f 72 65 64  tree node stored
17d29 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20 70 46 72   .** on page pFr
17d2a 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e 20  om to page pTo. 
17d2b 49 66 20 70 61 67 65 20 70 46 72 6f 6d 20 77 61  If page pFrom wa
17d2c 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 67  s not a leaf pag
17d2d 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 70  e, then.** the p
17d2e 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69  ointer-map entri
17d2f 65 73 20 66 6f 72 20 65 61 63 68 20 63 68 69 6c  es for each chil
17d30 64 20 70 61 67 65 20 61 72 65 20 75 70 64 61 74  d page are updat
17d31 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a  ed so that the.*
17d32 2a 20 70 61 72 65 6e 74 20 70 61 67 65 20 73 74  * parent page st
17d33 6f 72 65 64 20 69 6e 20 74 68 65 20 70 6f 69 6e  ored in the poin
17d34 74 65 72 20 6d 61 70 20 69 73 20 70 61 67 65 20  ter map is page 
17d35 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f  pTo. If pFrom co
17d36 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 79 20 63  ntained.** any c
17d37 65 6c 6c 73 20 77 69 74 68 20 6f 76 65 72 66 6c  ells with overfl
17d38 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 73  ow page pointers
17d39 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 72 72 65  , then the corre
17d3a 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e 74 65 72  sponding pointer
17d3b 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20  .** map entries 
17d3c 61 72 65 20 61 6c 73 6f 20 75 70 64 61 74 65 64  are also updated
17d3d 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 72   so that the par
17d3e 65 6e 74 20 70 61 67 65 20 69 73 20 70 61 67 65  ent page is page
17d3f 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70   pTo..**.** If p
17d40 46 72 6f 6d 20 69 73 20 63 75 72 72 65 6e 74 6c  From is currentl
17d41 79 20 63 61 72 72 79 69 6e 67 20 61 6e 79 20 6f  y carrying any o
17d42 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 28 65  verflow cells (e
17d43 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a  ntries in the.**
17d44 20 4d 65 6d 50 61 67 65 2e 61 4f 76 66 6c 5b 5d   MemPage.aOvfl[]
17d45 20 61 72 72 61 79 29 2c 20 74 68 65 79 20 61 72   array), they ar
17d46 65 20 6e 6f 74 20 63 6f 70 69 65 64 20 74 6f 20  e not copied to 
17d47 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42 65 66 6f  pTo. .**.** Befo
17d48 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 70 61  re returning, pa
17d49 67 65 20 70 54 6f 20 69 73 20 72 65 69 6e 69 74  ge pTo is reinit
17d4a 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 62 74  ialized using bt
17d4b 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a  reeInitPage()..*
17d4c 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66 6f 72 6d  *.** The perform
17d4d 61 6e 63 65 20 6f 66 20 74 68 69 73 20 66 75 6e  ance of this fun
17d4e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 72 69  ction is not cri
17d4f 74 69 63 61 6c 2e 20 49 74 20 69 73 20 6f 6e 6c  tical. It is onl
17d50 79 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 74 68  y used by .** th
17d51 65 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f  e balance_shallo
17d52 77 65 72 28 29 20 61 6e 64 20 62 61 6c 61 6e 63  wer() and balanc
17d53 65 5f 64 65 65 70 65 72 28 29 20 70 72 6f 63 65  e_deeper() proce
17d54 64 75 72 65 73 2c 20 6e 65 69 74 68 65 72 20 6f  dures, neither o
17d55 66 0a 2a 2a 20 77 68 69 63 68 20 61 72 65 20 63  f.** which are c
17d56 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 6e 64 65  alled often unde
17d57 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 75 6d 73  r normal circums
17d58 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  tances..*/.stati
17d59 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f 64 65 43  c void copyNodeC
17d5a 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 65 20 2a  ontent(MemPage *
17d5b 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 65 20 2a  pFrom, MemPage *
17d5c 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a  pTo, int *pRC){.
17d5d 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 51    if( (*pRC)==SQ
17d5e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 42  LITE_OK ){.    B
17d5f 74 53 68 61 72 65 64 20 2a 20 63 6f 6e 73 74 20  tShared * const 
17d60 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e 70 42 74  pBt = pFrom->pBt
17d61 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74  ;.    u8 * const
17d62 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 2d 3e   aFrom = pFrom->
17d63 61 44 61 74 61 3b 0a 20 20 20 20 75 38 20 2a 20  aData;.    u8 * 
17d64 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70 54 6f 2d  const aTo = pTo-
17d65 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 6e 74 20  >aData;.    int 
17d66 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64 72 20 3d  const iFromHdr =
17d67 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66 66 73 65   pFrom->hdrOffse
17d68 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6e 73 74  t;.    int const
17d69 20 69 54 6f 48 64 72 20 3d 20 28 28 70 54 6f 2d   iToHdr = ((pTo-
17d6a 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31 30 30 20  >pgno==1) ? 100 
17d6b 3a 20 30 29 3b 0a 20 20 20 20 54 45 53 54 4f 4e  : 0);.    TESTON
17d6c 4c 59 28 69 6e 74 20 72 63 3b 29 0a 20 20 20 20  LY(int rc;).    
17d6d 69 6e 74 20 69 44 61 74 61 3b 0a 20 20 0a 20 20  int iData;.  .  
17d6e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72  .    assert( pFr
17d6f 6f 6d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20  om->isInit );.  
17d70 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d    assert( pFrom-
17d71 3e 6e 46 72 65 65 3e 3d 69 54 6f 48 64 72 20 29  >nFree>=iToHdr )
17d72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65  ;.    assert( ge
17d73 74 32 62 79 74 65 28 26 61 46 72 6f 6d 5b 69 46  t2byte(&aFrom[iF
17d74 72 6f 6d 48 64 72 2b 35 5d 29 3c 3d 70 42 74 2d  romHdr+5])<=pBt-
17d75 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20  >usableSize );. 
17d76 20 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68   .    /* Copy th
17d77 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 63 6f  e b-tree node co
17d78 6e 74 65 6e 74 20 66 72 6f 6d 20 70 61 67 65 20  ntent from page 
17d79 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54  pFrom to page pT
17d7a 6f 2e 20 2a 2f 0a 20 20 20 20 69 44 61 74 61 20  o. */.    iData 
17d7b 3d 20 67 65 74 32 62 79 74 65 28 26 61 46 72 6f  = get2byte(&aFro
17d7c 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3b 0a  m[iFromHdr+5]);.
17d7d 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b      memcpy(&aTo[
17d7e 69 44 61 74 61 5d 2c 20 26 61 46 72 6f 6d 5b 69  iData], &aFrom[i
17d7f 44 61 74 61 5d 2c 20 70 42 74 2d 3e 75 73 61 62  Data], pBt->usab
17d80 6c 65 53 69 7a 65 2d 69 44 61 74 61 29 3b 0a 20  leSize-iData);. 
17d81 20 20 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b 69     memcpy(&aTo[i
17d82 54 6f 48 64 72 5d 2c 20 26 61 46 72 6f 6d 5b 69  ToHdr], &aFrom[i
17d83 46 72 6f 6d 48 64 72 5d 2c 20 70 46 72 6f 6d 2d  FromHdr], pFrom-
17d84 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a  >cellOffset + 2*
17d85 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c 29 3b 0a 20  pFrom->nCell);. 
17d86 20 0a 20 20 20 20 2f 2a 20 52 65 69 6e 69 74 69   .    /* Reiniti
17d87 61 6c 69 7a 65 20 70 61 67 65 20 70 54 6f 20 73  alize page pTo s
17d88 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65  o that the conte
17d89 6e 74 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61  nts of the MemPa
17d8a 67 65 20 73 74 72 75 63 74 75 72 65 0a 20 20 20  ge structure.   
17d8b 20 2a 2a 20 6d 61 74 63 68 20 74 68 65 20 6e 65   ** match the ne
17d8c 77 20 64 61 74 61 2e 20 54 68 65 20 69 6e 69 74  w data. The init
17d8d 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 70 54  ialization of pT
17d8e 6f 20 22 63 61 6e 6e 6f 74 22 20 66 61 69 6c 2c  o "cannot" fail,
17d8f 20 61 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 64   as the.    ** d
17d90 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f 6d 20  ata copied from 
17d91 70 46 72 6f 6d 20 69 73 20 6b 6e 6f 77 6e 20 74  pFrom is known t
17d92 6f 20 62 65 20 76 61 6c 69 64 2e 20 20 2a 2f 0a  o be valid.  */.
17d93 20 20 20 20 70 54 6f 2d 3e 69 73 49 6e 69 74 20      pTo->isInit 
17d94 3d 20 30 3b 0a 20 20 20 20 54 45 53 54 4f 4e 4c  = 0;.    TESTONL
17d95 59 28 72 63 20 3d 20 29 20 62 74 72 65 65 49 6e  Y(rc = ) btreeIn
17d96 69 74 50 61 67 65 28 70 54 6f 29 3b 0a 20 20 20  itPage(pTo);.   
17d97 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
17d98 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 0a 20 20 20  ITE_OK );.  .   
17d99 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
17d9a 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61  n auto-vacuum da
17d9b 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 20 74  tabase, update t
17d9c 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
17d9d 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 66 6f  ntries.    ** fo
17d9e 72 20 61 6e 79 20 62 2d 74 72 65 65 20 6f 72 20  r any b-tree or 
17d9f 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 74  overflow pages t
17da0 68 61 74 20 70 54 6f 20 6e 6f 77 20 63 6f 6e 74  hat pTo now cont
17da1 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74 65 72  ains the pointer
17da2 73 20 74 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  s to..    */.   
17da3 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55   if( ISAUTOVACUU
17da4 4d 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 43 20  M ){.      *pRC 
17da5 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70  = setChildPtrmap
17da6 73 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20  s(pTo);.    }.  
17da7 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
17da8 72 6f 75 74 69 6e 65 20 72 65 64 69 73 74 72 69  routine redistri
17da9 62 75 74 65 73 20 63 65 6c 6c 73 20 6f 6e 20 74  butes cells on t
17daa 68 65 20 69 50 61 72 65 6e 74 49 64 78 27 74 68  he iParentIdx'th
17dab 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e   child of pParen
17dac 74 0a 2a 2a 20 28 68 65 72 65 61 66 74 65 72 20  t.** (hereafter 
17dad 22 74 68 65 20 70 61 67 65 22 29 20 61 6e 64 20  "the page") and 
17dae 75 70 20 74 6f 20 32 20 73 69 62 6c 69 6e 67 73  up to 2 siblings
17daf 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 70 61 67   so that all pag
17db0 65 73 20 68 61 76 65 20 61 62 6f 75 74 20 74 68  es have about th
17db1 65 0a 2a 2a 20 73 61 6d 65 20 61 6d 6f 75 6e 74  e.** same amount
17db2 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 2e 20   of free space. 
17db3 55 73 75 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65  Usually a single
17db4 20 73 69 62 6c 69 6e 67 20 6f 6e 20 65 69 74 68   sibling on eith
17db5 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 0a 2a  er side of the.*
17db6 2a 20 70 61 67 65 20 61 72 65 20 75 73 65 64 20  * page are used 
17db7 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67  in the balancing
17db8 2c 20 74 68 6f 75 67 68 20 62 6f 74 68 20 73 69  , though both si
17db9 62 6c 69 6e 67 73 20 6d 69 67 68 74 20 63 6f 6d  blings might com
17dba 65 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 73 69  e from one.** si
17dbb 64 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69  de if the page i
17dbc 73 20 74 68 65 20 66 69 72 73 74 20 6f 72 20 6c  s the first or l
17dbd 61 73 74 20 63 68 69 6c 64 20 6f 66 20 69 74 73  ast child of its
17dbe 20 70 61 72 65 6e 74 2e 20 49 66 20 74 68 65 20   parent. If the 
17dbf 70 61 67 65 20 0a 2a 2a 20 68 61 73 20 66 65 77  page .** has few
17dc0 65 72 20 74 68 61 6e 20 32 20 73 69 62 6c 69 6e  er than 2 siblin
17dc1 67 73 20 28 73 6f 6d 65 74 68 69 6e 67 20 77 68  gs (something wh
17dc2 69 63 68 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70  ich can only hap
17dc3 70 65 6e 20 69 66 20 74 68 65 20 70 61 67 65 0a  pen if the page.
17dc4 2a 2a 20 69 73 20 61 20 72 6f 6f 74 20 70 61 67  ** is a root pag
17dc5 65 20 6f 72 20 61 20 63 68 69 6c 64 20 6f 66 20  e or a child of 
17dc6 61 20 72 6f 6f 74 20 70 61 67 65 29 20 74 68 65  a root page) the
17dc7 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20  n all available 
17dc8 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 70 61 72 74  siblings.** part
17dc9 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 62  icipate in the b
17dca 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  alancing..**.** 
17dcb 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69  The number of si
17dcc 62 6c 69 6e 67 73 20 6f 66 20 74 68 65 20 70 61  blings of the pa
17dcd 67 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72  ge might be incr
17dce 65 61 73 65 64 20 6f 72 20 64 65 63 72 65 61 73  eased or decreas
17dcf 65 64 20 62 79 20 0a 2a 2a 20 6f 6e 65 20 6f 72  ed by .** one or
17dd0 20 74 77 6f 20 69 6e 20 61 6e 20 65 66 66 6f 72   two in an effor
17dd1 74 20 74 6f 20 6b 65 65 70 20 70 61 67 65 73 20  t to keep pages 
17dd2 6e 65 61 72 6c 79 20 66 75 6c 6c 20 62 75 74 20  nearly full but 
17dd3 6e 6f 74 20 6f 76 65 72 20 66 75 6c 6c 2e 20 0a  not over full. .
17dd4 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
17dd5 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
17dd6 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d  e is called, som
17dd7 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f  e of the cells o
17dd8 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6d 69  n the page.** mi
17dd9 67 68 74 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79  ght not actually
17dda 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 4d 65   be stored in Me
17ddb 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d 2e 20 54  mPage.aData[]. T
17ddc 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 2a  his can happen.*
17ddd 2a 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73  * if the page is
17dde 20 6f 76 65 72 66 75 6c 6c 2e 20 54 68 69 73 20   overfull. This 
17ddf 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20  routine ensures 
17de0 74 68 61 74 20 61 6c 6c 20 63 65 6c 6c 73 20 61  that all cells a
17de1 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 74 6f 20 74  llocated.** to t
17de2 68 65 20 70 61 67 65 20 61 6e 64 20 69 74 73 20  he page and its 
17de3 73 69 62 6c 69 6e 67 73 20 66 69 74 20 69 6e 74  siblings fit int
17de4 6f 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b  o MemPage.aData[
17de5 5d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  ] before returni
17de6 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  ng..**.** In the
17de7 20 63 6f 75 72 73 65 20 6f 66 20 62 61 6c 61 6e   course of balan
17de8 63 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 6e  cing the page an
17de9 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 2c 20  d its siblings, 
17dea 63 65 6c 6c 73 20 6d 61 79 20 62 65 0a 2a 2a 20  cells may be.** 
17deb 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 6f 72  inserted into or
17dec 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
17ded 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 28 70  e parent page (p
17dee 50 61 72 65 6e 74 29 2e 20 44 6f 69 6e 67 20 73  Parent). Doing s
17def 6f 0a 2a 2a 20 6d 61 79 20 63 61 75 73 65 20 74  o.** may cause t
17df0 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 74  he parent page t
17df1 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c  o become overful
17df2 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20  l or underfull. 
17df3 49 66 20 74 68 69 73 0a 2a 2a 20 68 61 70 70 65  If this.** happe
17df4 6e 73 2c 20 69 74 20 69 73 20 74 68 65 20 72 65  ns, it is the re
17df5 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
17df6 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 69 6e  the caller to in
17df7 76 6f 6b 65 20 74 68 65 20 63 6f 72 72 65 63 74  voke the correct
17df8 0a 2a 2a 20 62 61 6c 61 6e 63 69 6e 67 20 72 6f  .** balancing ro
17df9 75 74 69 6e 65 20 74 6f 20 66 69 78 20 74 68 69  utine to fix thi
17dfa 73 20 70 72 6f 62 6c 65 6d 20 28 73 65 65 20 74  s problem (see t
17dfb 68 65 20 62 61 6c 61 6e 63 65 28 29 20 72 6f 75  he balance() rou
17dfc 74 69 6e 65 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  tine). .**.** If
17dfd 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61   this routine fa
17dfe 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65 61 73  ils for any reas
17dff 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20 6c 65 61  on, it might lea
17e00 76 65 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  ve the database.
17e01 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75 70 74 65  ** in a corrupte
17e02 64 20 73 74 61 74 65 2e 20 53 6f 20 69 66 20 74  d state. So if t
17e03 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c  his routine fail
17e04 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  s, the database 
17e05 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 72 6f 6c  should.** be rol
17e06 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  led back..**.** 
17e07 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  The third argume
17e08 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
17e09 69 6f 6e 2c 20 61 4f 76 66 6c 53 70 61 63 65 2c  ion, aOvflSpace,
17e0a 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
17e0b 20 61 0a 2a 2a 20 62 75 66 66 65 72 20 62 69 67   a.** buffer big
17e0c 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20   enough to hold 
17e0d 6f 6e 65 20 70 61 67 65 2e 20 49 66 20 77 68 69  one page. If whi
17e0e 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 63 65 6c  le inserting cel
17e0f 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65  ls into the pare
17e10 6e 74 0a 2a 2a 20 70 61 67 65 20 28 70 50 61 72  nt.** page (pPar
17e11 65 6e 74 29 20 74 68 65 20 70 61 72 65 6e 74 20  ent) the parent 
17e12 70 61 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 65  page becomes ove
17e13 72 66 75 6c 6c 2c 20 74 68 69 73 20 62 75 66 66  rfull, this buff
17e14 65 72 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f  er is.** used to
17e15 20 73 74 6f 72 65 20 74 68 65 20 70 61 72 65 6e   store the paren
17e16 74 27 73 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  t's overflow cel
17e17 6c 73 2e 20 42 65 63 61 75 73 65 20 74 68 69 73  ls. Because this
17e18 20 66 75 6e 63 74 69 6f 6e 20 69 6e 73 65 72 74   function insert
17e19 73 0a 2a 2a 20 61 20 6d 61 78 69 6d 75 6d 20 6f  s.** a maximum o
17e1a 66 20 66 6f 75 72 20 64 69 76 69 64 65 72 20 63  f four divider c
17e1b 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 61  ells into the pa
17e1c 72 65 6e 74 20 70 61 67 65 2c 20 61 6e 64 20 74  rent page, and t
17e1d 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 73 69  he maximum.** si
17e1e 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 73 74 6f  ze of a cell sto
17e1f 72 65 64 20 77 69 74 68 69 6e 20 61 6e 20 69 6e  red within an in
17e20 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 73 20 61  ternal node is a
17e21 6c 77 61 79 73 20 6c 65 73 73 20 74 68 61 6e 20  lways less than 
17e22 31 2f 34 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61  1/4.** of the pa
17e23 67 65 2d 73 69 7a 65 2c 20 74 68 65 20 61 4f 76  ge-size, the aOv
17e24 66 6c 53 70 61 63 65 5b 5d 20 62 75 66 66 65 72  flSpace[] buffer
17e25 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
17e26 6f 20 62 65 20 6c 61 72 67 65 0a 2a 2a 20 65 6e  o be large.** en
17e27 6f 75 67 68 20 66 6f 72 20 61 6c 6c 20 6f 76 65  ough for all ove
17e28 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2a 0a  rflow cells..**.
17e29 2a 2a 20 49 66 20 61 4f 76 66 6c 53 70 61 63 65  ** If aOvflSpace
17e2a 20 69 73 20 73 65 74 20 74 6f 20 61 20 6e 75 6c   is set to a nul
17e2b 6c 20 70 6f 69 6e 74 65 72 2c 20 74 68 69 73 20  l pointer, this 
17e2c 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
17e2d 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45   .** SQLITE_NOME
17e2e 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  M..*/.static int
17e2f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74   balance_nonroot
17e30 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  (.  MemPage *pPa
17e31 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20  rent,           
17e32 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 70 61      /* Parent pa
17e33 67 65 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 62  ge of siblings b
17e34 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f  eing balanced */
17e35 0a 20 20 69 6e 74 20 69 50 61 72 65 6e 74 49 64  .  int iParentId
17e36 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
17e37 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 22     /* Index of "
17e38 74 68 65 20 70 61 67 65 22 20 69 6e 20 70 50 61  the page" in pPa
17e39 72 65 6e 74 20 2a 2f 0a 20 20 75 38 20 2a 61 4f  rent */.  u8 *aO
17e3a 76 66 6c 53 70 61 63 65 2c 20 20 20 20 20 20 20  vflSpace,       
17e3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 61 67            /* pag
17e3c 65 2d 73 69 7a 65 20 62 79 74 65 73 20 6f 66 20  e-size bytes of 
17e3d 73 70 61 63 65 20 66 6f 72 20 70 61 72 65 6e 74  space for parent
17e3e 20 6f 76 66 6c 20 2a 2f 0a 20 20 69 6e 74 20 69   ovfl */.  int i
17e3f 73 52 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20  sRoot           
17e40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
17e41 75 65 20 69 66 20 70 50 61 72 65 6e 74 20 69 73  ue if pParent is
17e42 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 2a 2f 0a   a root-page */.
17e43 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
17e44 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Bt;             
17e45 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64    /* The whole d
17e46 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74  atabase */.  int
17e47 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20   nCell = 0;     
17e48 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
17e49 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20  ber of cells in 
17e4a 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e  apCell[] */.  in
17e4b 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b  t nMaxCells = 0;
17e4c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c             /* Al
17e4d 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20  located size of 
17e4e 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20  apCell, szCell, 
17e4f 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20  aFrom. */.  int 
17e50 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20 20  nNew = 0;       
17e51 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
17e52 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61  er of pages in a
17e53 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  pNew[] */.  int 
17e54 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20  nOld;           
17e55 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
17e56 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61  er of pages in a
17e57 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  pOld[] */.  int 
17e58 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20  i, j, k;        
17e59 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
17e5a 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69   counters */.  i
17e5b 6e 74 20 6e 78 44 69 76 3b 20 20 20 20 20 20 20  nt nxDiv;       
17e5c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
17e5d 65 78 74 20 64 69 76 69 64 65 72 20 73 6c 6f 74  ext divider slot
17e5e 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65   in pParent->aCe
17e5f 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ll[] */.  int rc
17e60 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
17e61 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65         /* The re
17e62 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75  turn code */.  u
17e63 31 36 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f  16 leafCorrectio
17e64 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 34  n;          /* 4
17e65 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c   if pPage is a l
17e66 65 61 66 2e 20 20 30 20 69 66 20 6e 6f 74 20 2a  eaf.  0 if not *
17e67 2f 0a 20 20 69 6e 74 20 6c 65 61 66 44 61 74 61  /.  int leafData
17e68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
17e69 20 2f 2a 20 54 72 75 65 20 69 66 20 70 50 61 67   /* True if pPag
17e6a 65 20 69 73 20 61 20 6c 65 61 66 20 6f 66 20 61  e is a leaf of a
17e6b 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20 2a   LEAFDATA tree *
17e6c 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 70  /.  int usableSp
17e6d 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ace;            
17e6e 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 70 50 61   /* Bytes in pPa
17e6f 67 65 20 62 65 79 6f 6e 64 20 74 68 65 20 68 65  ge beyond the he
17e70 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61  ader */.  int pa
17e71 67 65 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  geFlags;        
17e72 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
17e73 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  of pPage->aData[
17e74 30 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62 74  0] */.  int subt
17e75 6f 74 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20  otal;           
17e76 20 20 20 20 20 2f 2a 20 53 75 62 74 6f 74 61 6c       /* Subtotal
17e77 20 6f 66 20 62 79 74 65 73 20 69 6e 20 63 65 6c   of bytes in cel
17e78 6c 73 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a  ls on one page *
17e79 2f 0a 20 20 69 6e 74 20 69 53 70 61 63 65 31 20  /.  int iSpace1 
17e7a 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
17e7b 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64   /* First unused
17e7c 20 62 79 74 65 20 6f 66 20 61 53 70 61 63 65 31   byte of aSpace1
17e7d 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 76 66  [] */.  int iOvf
17e7e 6c 53 70 61 63 65 20 3d 20 30 3b 20 20 20 20 20  lSpace = 0;     
17e7f 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e       /* First un
17e80 75 73 65 64 20 62 79 74 65 20 6f 66 20 61 4f 76  used byte of aOv
17e81 66 6c 53 70 61 63 65 5b 5d 20 2a 2f 0a 20 20 69  flSpace[] */.  i
17e82 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20 20 20  nt szScratch;   
17e83 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
17e84 69 7a 65 20 6f 66 20 73 63 72 61 74 63 68 20 6d  ize of scratch m
17e85 65 6d 6f 72 79 20 72 65 71 75 65 73 74 65 64 20  emory requested 
17e86 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70  */.  MemPage *ap
17e87 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20  Old[NB];        
17e88 20 20 2f 2a 20 70 50 61 67 65 20 61 6e 64 20 75    /* pPage and u
17e89 70 20 74 6f 20 74 77 6f 20 73 69 62 6c 69 6e 67  p to two sibling
17e8a 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  s */.  MemPage *
17e8b 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20  apCopy[NB];     
17e8c 20 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63      /* Private c
17e8d 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d  opies of apOld[]
17e8e 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50   pages */.  MemP
17e8f 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d  age *apNew[NB+2]
17e90 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67  ;        /* pPag
17e91 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73  e and up to NB s
17e92 69 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61  iblings after ba
17e93 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 75 38 20  lancing */.  u8 
17e94 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 20  *pRight;        
17e95 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63            /* Loc
17e96 61 74 69 6f 6e 20 69 6e 20 70 61 72 65 6e 74 20  ation in parent 
17e97 6f 66 20 72 69 67 68 74 2d 73 69 62 6c 69 6e 67  of right-sibling
17e98 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 38   pointer */.  u8
17e99 20 2a 61 70 44 69 76 5b 4e 42 2d 31 5d 3b 20 20   *apDiv[NB-1];  
17e9a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69             /* Di
17e9b 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 20 70  vider cells in p
17e9c 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  Parent */.  int 
17e9d 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20  cntNew[NB+2];   
17e9e 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
17e9f 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 6f 66 20  x in aCell[] of 
17ea0 63 65 6c 6c 20 61 66 74 65 72 20 69 2d 74 68 20  cell after i-th 
17ea1 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  page */.  int sz
17ea2 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20  New[NB+2];      
17ea3 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e         /* Combin
17ea4 65 64 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 73  ed size of cells
17ea5 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74 68 20 70   place on i-th p
17ea6 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70  age */.  u8 **ap
17ea7 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20  Cell = 0;       
17ea8 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c        /* All cel
17ea9 6c 73 20 62 65 67 69 6e 20 62 61 6c 61 6e 63 65  ls begin balance
17eaa 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65  d */.  u16 *szCe
17eab 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
17eac 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a      /* Local siz
17ead 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 69  e of all cells i
17eae 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20  n apCell[] */.  
17eaf 75 38 20 2a 61 53 70 61 63 65 31 3b 20 20 20 20  u8 *aSpace1;    
17eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17eb1 53 70 61 63 65 20 66 6f 72 20 63 6f 70 69 65 73  Space for copies
17eb2 20 6f 66 20 64 69 76 69 64 65 72 73 20 63 65 6c   of dividers cel
17eb3 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  ls */.  Pgno pgn
17eb4 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o;              
17eb5 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 76 61 72       /* Temp var
17eb6 20 74 6f 20 73 74 6f 72 65 20 61 20 70 61 67 65   to store a page
17eb7 20 6e 75 6d 62 65 72 20 69 6e 20 2a 2f 0a 0a 20   number in */.. 
17eb8 20 70 42 74 20 3d 20 70 50 61 72 65 6e 74 2d 3e   pBt = pParent->
17eb9 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73  pBt;.  assert( s
17eba 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
17ebb 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
17ebc 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
17ebd 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
17ebe 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50  le(pParent->pDbP
17ebf 61 67 65 29 20 29 3b 0a 0a 23 69 66 20 30 0a 20  age) );..#if 0. 
17ec0 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45   TRACE(("BALANCE
17ec1 3a 20 62 65 67 69 6e 20 70 61 67 65 20 25 64 20  : begin page %d 
17ec2 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e 22 2c 20  child of %d\n", 
17ec3 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 50 61  pPage->pgno, pPa
17ec4 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b 0a 23 65  rent->pgno));.#e
17ec5 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 74 20 74 68  ndif..  /* At th
17ec6 69 73 20 70 6f 69 6e 74 20 70 50 61 72 65 6e 74  is point pParent
17ec7 20 6d 61 79 20 68 61 76 65 20 61 74 20 6d 6f 73   may have at mos
17ec8 74 20 6f 6e 65 20 6f 76 65 72 66 6c 6f 77 20 63  t one overflow c
17ec9 65 6c 6c 2e 20 41 6e 64 20 69 66 0a 20 20 2a 2a  ell. And if.  **
17eca 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 63   this overflow c
17ecb 65 6c 6c 20 69 73 20 70 72 65 73 65 6e 74 2c 20  ell is present, 
17ecc 69 74 20 6d 75 73 74 20 62 65 20 74 68 65 20 63  it must be the c
17ecd 65 6c 6c 20 77 69 74 68 20 0a 20 20 2a 2a 20 69  ell with .  ** i
17ece 6e 64 65 78 20 69 50 61 72 65 6e 74 49 64 78 2e  ndex iParentIdx.
17ecf 20 54 68 69 73 20 73 63 65 6e 61 72 69 6f 20 63   This scenario c
17ed0 6f 6d 65 73 20 61 62 6f 75 74 20 77 68 65 6e 20  omes about when 
17ed1 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20  this function.  
17ed2 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 28 69 6e  ** is called (in
17ed3 64 69 72 65 63 74 6c 79 29 20 66 72 6f 6d 20 73  directly) from s
17ed4 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74  qlite3BtreeDelet
17ed5 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  e()..  */.  asse
17ed6 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76  rt( pParent->nOv
17ed7 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61  erflow==0 || pPa
17ed8 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d  rent->nOverflow=
17ed9 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =1 );.  assert( 
17eda 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c  pParent->nOverfl
17edb 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e 74  ow==0 || pParent
17edc 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d  ->aOvfl[0].idx==
17edd 69 50 61 72 65 6e 74 49 64 78 20 29 3b 0a 0a 20  iParentIdx );.. 
17ede 20 69 66 28 20 21 61 4f 76 66 6c 53 70 61 63 65   if( !aOvflSpace
17edf 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
17ee0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
17ee1 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20  ..  /* Find the 
17ee2 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f  sibling pages to
17ee3 20 62 61 6c 61 6e 63 65 2e 20 41 6c 73 6f 20 6c   balance. Also l
17ee4 6f 63 61 74 65 20 74 68 65 20 63 65 6c 6c 73 20  ocate the cells 
17ee5 69 6e 20 70 50 61 72 65 6e 74 20 0a 20 20 2a 2a  in pParent .  **
17ee6 20 74 68 61 74 20 64 69 76 69 64 65 20 74 68 65   that divide the
17ee7 20 73 69 62 6c 69 6e 67 73 2e 20 41 6e 20 61 74   siblings. An at
17ee8 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f  tempt is made to
17ee9 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c 69 6e 67   find NN sibling
17eea 73 20 6f 6e 20 0a 20 20 2a 2a 20 65 69 74 68 65  s on .  ** eithe
17eeb 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 2e  r side of pPage.
17eec 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 61   More siblings a
17eed 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e  re taken from on
17eee 65 20 73 69 64 65 2c 20 68 6f 77 65 76 65 72 2c  e side, however,
17eef 20 0a 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20   .  ** if there 
17ef0 61 72 65 20 66 65 77 65 72 20 74 68 61 6e 20 4e  are fewer than N
17ef1 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74 68  N siblings on th
17ef2 65 20 6f 74 68 65 72 20 73 69 64 65 2e 20 49 66  e other side. If
17ef3 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a 20 68 61   pParent.  ** ha
17ef4 73 20 4e 42 20 6f 72 20 66 65 77 65 72 20 63 68  s NB or fewer ch
17ef5 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 6c 6c 20  ildren then all 
17ef6 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 50 61 72  children of pPar
17ef7 65 6e 74 20 61 72 65 20 74 61 6b 65 6e 2e 20 20  ent are taken.  
17ef8 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
17ef9 6c 6f 6f 70 20 61 6c 73 6f 20 64 72 6f 70 73 20  loop also drops 
17efa 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c  the divider cell
17efb 73 20 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e  s from the paren
17efc 74 20 70 61 67 65 2e 20 54 68 69 73 0a 20 20 2a  t page. This.  *
17efd 2a 20 77 61 79 2c 20 74 68 65 20 72 65 6d 61 69  * way, the remai
17efe 6e 64 65 72 20 6f 66 20 74 68 65 20 66 75 6e 63  nder of the func
17eff 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 68 61  tion does not ha
17f00 76 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20  ve to deal with 
17f01 61 6e 79 0a 20 20 2a 2a 20 6f 76 65 72 66 6c 6f  any.  ** overflo
17f02 77 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70  w cells in the p
17f03 61 72 65 6e 74 20 70 61 67 65 2c 20 73 69 6e 63  arent page, sinc
17f04 65 20 69 66 20 61 6e 79 20 65 78 69 73 74 65 64  e if any existed
17f05 20 74 68 65 79 20 77 69 6c 6c 0a 20 20 2a 2a 20   they will.  ** 
17f06 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
17f07 6e 20 72 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f 0a  n removed..  */.
17f08 20 20 69 20 3d 20 70 50 61 72 65 6e 74 2d 3e 6e    i = pParent->n
17f09 4f 76 65 72 66 6c 6f 77 20 2b 20 70 50 61 72 65  Overflow + pPare
17f0a 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 69 66 28  nt->nCell;.  if(
17f0b 20 69 3c 32 20 29 7b 0a 20 20 20 20 6e 78 44 69   i<2 ){.    nxDi
17f0c 76 20 3d 20 30 3b 0a 20 20 20 20 6e 4f 6c 64 20  v = 0;.    nOld 
17f0d 3d 20 69 2b 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  = i+1;.  }else{.
17f0e 20 20 20 20 6e 4f 6c 64 20 3d 20 33 3b 0a 20 20      nOld = 3;.  
17f0f 20 20 69 66 28 20 69 50 61 72 65 6e 74 49 64 78    if( iParentIdx
17f10 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  ==0 ){          
17f11 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 6e 78         .      nx
17f12 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  Div = 0;.    }el
17f13 73 65 20 69 66 28 20 69 50 61 72 65 6e 74 49 64  se if( iParentId
17f14 78 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 6e 78  x==i ){.      nx
17f15 44 69 76 20 3d 20 69 2d 32 3b 0a 20 20 20 20 7d  Div = i-2;.    }
17f16 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 78 44 69  else{.      nxDi
17f17 76 20 3d 20 69 50 61 72 65 6e 74 49 64 78 2d 31  v = iParentIdx-1
17f18 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20  ;.    }.    i = 
17f19 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 69 2b  2;.  }.  if( (i+
17f1a 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e  nxDiv-pParent->n
17f1b 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 50 61 72 65  Overflow)==pPare
17f1c 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20  nt->nCell ){.   
17f1d 20 70 52 69 67 68 74 20 3d 20 26 70 50 61 72 65   pRight = &pPare
17f1e 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e  nt->aData[pParen
17f1f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 3b  t->hdrOffset+8];
17f20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 52  .  }else{.    pR
17f21 69 67 68 74 20 3d 20 66 69 6e 64 43 65 6c 6c 28  ight = findCell(
17f22 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76  pParent, i+nxDiv
17f23 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66  -pParent->nOverf
17f24 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 70 67 6e 6f  low);.  }.  pgno
17f25 20 3d 20 67 65 74 34 62 79 74 65 28 70 52 69 67   = get4byte(pRig
17f26 68 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 31 20  ht);.  while( 1 
17f27 29 7b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41  ){.    rc = getA
17f28 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20  ndInitPage(pBt, 
17f29 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b 69 5d 29  pgno, &apOld[i])
17f2a 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
17f2b 20 20 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4f        memset(apO
17f2c 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a 73 69 7a  ld, 0, (i+1)*siz
17f2d 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29 29 3b 0a  eof(MemPage*));.
17f2e 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e        goto balan
17f2f 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  ce_cleanup;.    
17f30 7d 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20  }.    nMaxCells 
17f31 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e  += 1+apOld[i]->n
17f32 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e  Cell+apOld[i]->n
17f33 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66  Overflow;.    if
17f34 28 20 28 69 2d 2d 29 3d 3d 30 20 29 20 62 72 65  ( (i--)==0 ) bre
17f35 61 6b 3b 0a 0a 20 20 20 20 69 66 28 20 69 2b 6e  ak;..    if( i+n
17f36 78 44 69 76 3d 3d 70 50 61 72 65 6e 74 2d 3e 61  xDiv==pParent->a
17f37 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 26 26 20 70  Ovfl[0].idx && p
17f38 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f  Parent->nOverflo
17f39 77 20 29 7b 0a 20 20 20 20 20 20 61 70 44 69 76  w ){.      apDiv
17f3a 5b 69 5d 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61  [i] = pParent->a
17f3b 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20  Ovfl[0].pCell;. 
17f3c 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34       pgno = get4
17f3d 62 79 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a  byte(apDiv[i]);.
17f3e 20 20 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d        szNew[i] =
17f3f 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
17f40 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b  rent, apDiv[i]);
17f41 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
17f42 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20  nOverflow = 0;. 
17f43 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17f44 61 70 44 69 76 5b 69 5d 20 3d 20 66 69 6e 64 43  apDiv[i] = findC
17f45 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e  ell(pParent, i+n
17f46 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f  xDiv-pParent->nO
17f47 76 65 72 66 6c 6f 77 29 3b 0a 20 20 20 20 20 20  verflow);.      
17f48 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
17f49 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20  apDiv[i]);.     
17f4a 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c 6c   szNew[i] = cell
17f4b 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c  SizePtr(pParent,
17f4c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 0a 20 20 20   apDiv[i]);..   
17f4d 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 63     /* Drop the c
17f4e 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 70 61 72  ell from the par
17f4f 65 6e 74 20 70 61 67 65 2e 20 61 70 44 69 76 5b  ent page. apDiv[
17f50 69 5d 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73 20  i] still points 
17f51 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  to.      ** the 
17f52 63 65 6c 6c 20 77 69 74 68 69 6e 20 74 68 65 20  cell within the 
17f53 70 61 72 65 6e 74 2c 20 65 76 65 6e 20 74 68 6f  parent, even tho
17f54 75 67 68 20 69 74 20 68 61 73 20 62 65 65 6e 20  ugh it has been 
17f55 64 72 6f 70 70 65 64 2e 0a 20 20 20 20 20 20 2a  dropped..      *
17f56 2a 20 54 68 69 73 20 69 73 20 73 61 66 65 20 62  * This is safe b
17f57 65 63 61 75 73 65 20 64 72 6f 70 70 69 6e 67 20  ecause dropping 
17f58 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 6f 76 65 72  a cell only over
17f59 77 72 69 74 65 73 20 74 68 65 20 66 69 72 73 74  writes the first
17f5a 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75 72 20 62  .      ** four b
17f5b 79 74 65 73 20 6f 66 20 69 74 2c 20 61 6e 64 20  ytes of it, and 
17f5c 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f  this function do
17f5d 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 20  es not need the 
17f5e 66 69 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 66  first.      ** f
17f5f 6f 75 72 20 62 79 74 65 73 20 6f 66 20 74 68 65  our bytes of the
17f60 20 64 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 53   divider cell. S
17f61 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73  o the pointer is
17f62 20 73 61 66 65 20 74 6f 20 75 73 65 0a 20 20 20   safe to use.   
17f63 20 20 20 2a 2a 20 6c 61 74 65 72 20 6f 6e 2e 20     ** later on. 
17f64 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20   .      **.     
17f65 20 2a 2a 20 55 6e 6c 65 73 73 20 53 51 4c 69 74   ** Unless SQLit
17f66 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 69 6e  e is compiled in
17f67 20 73 65 63 75 72 65 2d 64 65 6c 65 74 65 20 6d   secure-delete m
17f68 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ode. In this cas
17f69 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  e,.      ** the 
17f6a 64 72 6f 70 43 65 6c 6c 28 29 20 72 6f 75 74 69  dropCell() routi
17f6b 6e 65 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74  ne will overwrit
17f6c 65 20 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c  e the entire cel
17f6d 6c 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 0a 20  l with zeroes.. 
17f6e 20 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20       ** In this 
17f6f 63 61 73 65 2c 20 74 65 6d 70 6f 72 61 72 69 6c  case, temporaril
17f70 79 20 63 6f 70 79 20 74 68 65 20 63 65 6c 6c 20  y copy the cell 
17f71 69 6e 74 6f 20 74 68 65 20 61 4f 76 66 6c 53 70  into the aOvflSp
17f72 61 63 65 5b 5d 0a 20 20 20 20 20 20 2a 2a 20 62  ace[].      ** b
17f73 75 66 66 65 72 2e 20 49 74 20 77 69 6c 6c 20 62  uffer. It will b
17f74 65 20 63 6f 70 69 65 64 20 6f 75 74 20 61 67 61  e copied out aga
17f75 69 6e 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68  in as soon as th
17f76 65 20 61 53 70 61 63 65 5b 5d 20 62 75 66 66 65  e aSpace[] buffe
17f77 72 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61 6c  r.      ** is al
17f78 6c 6f 63 61 74 65 64 2e 20 20 2a 2f 0a 23 69 66  located.  */.#if
17f79 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52  def SQLITE_SECUR
17f7a 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20 6d  E_DELETE.      m
17f7b 65 6d 63 70 79 28 26 61 4f 76 66 6c 53 70 61 63  emcpy(&aOvflSpac
17f7c 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 65  e[apDiv[i]-pPare
17f7d 6e 74 2d 3e 61 44 61 74 61 5d 2c 20 61 70 44 69  nt->aData], apDi
17f7e 76 5b 69 5d 2c 20 73 7a 4e 65 77 5b 69 5d 29 3b  v[i], szNew[i]);
17f7f 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20  .      apDiv[i] 
17f80 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 61 70  = &aOvflSpace[ap
17f81 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d 3e  Div[i]-pParent->
17f82 61 44 61 74 61 5d 3b 0a 23 65 6e 64 69 66 0a 20  aData];.#endif. 
17f83 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50       dropCell(pP
17f84 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70  arent, i+nxDiv-p
17f85 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f  Parent->nOverflo
17f86 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c 20 26 72 63  w, szNew[i], &rc
17f87 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
17f88 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c  /* Make nMaxCell
17f89 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20  s a multiple of 
17f8a 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72  4 in order to pr
17f8b 65 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20 20  eserve 8-byte.  
17f8c 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a  ** alignment */.
17f8d 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e    nMaxCells = (n
17f8e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e 33  MaxCells + 3)&~3
17f8f 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c  ;..  /*.  ** All
17f90 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20  ocate space for 
17f91 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
17f92 73 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20 70 42 74  s.  */.  k = pBt
17f93 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 52 4f 55  ->pageSize + ROU
17f94 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61  ND8(sizeof(MemPa
17f95 67 65 29 29 3b 0a 20 20 73 7a 53 63 72 61 74 63  ge));.  szScratc
17f96 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61 78 43  h =.       nMaxC
17f97 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a 29  ells*sizeof(u8*)
17f98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f99 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65 6c 6c         /* apCell
17f9a 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78 43   */.     + nMaxC
17f9b 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36 29  ells*sizeof(u16)
17f9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f9d 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c 6c         /* szCell
17f9e 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d 3e   */.     + pBt->
17f9f 70 61 67 65 53 69 7a 65 20 20 20 20 20 20 20 20  pageSize        
17fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17fa1 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 63 65         /* aSpace
17fa2 31 20 2a 2f 0a 20 20 20 20 20 2b 20 6b 2a 6e 4f  1 */.     + k*nO
17fa3 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ld;             
17fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17fa5 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
17fa6 63 6f 70 69 65 73 20 28 61 70 43 6f 70 79 29 20  copies (apCopy) 
17fa7 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73 71  */.  apCell = sq
17fa8 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c  lite3ScratchMall
17fa9 6f 63 28 20 73 7a 53 63 72 61 74 63 68 20 29 3b  oc( szScratch );
17faa 20 0a 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d   .  if( apCell==
17fab 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  0 ){.    rc = SQ
17fac 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
17fad 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
17fae 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a 43 65  anup;.  }.  szCe
17faf 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 43 65  ll = (u16*)&apCe
17fb0 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20  ll[nMaxCells];. 
17fb1 20 61 53 70 61 63 65 31 20 3d 20 28 75 38 2a 29   aSpace1 = (u8*)
17fb2 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c  &szCell[nMaxCell
17fb3 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49  s];.  assert( EI
17fb4 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
17fb5 4e 54 28 61 53 70 61 63 65 31 29 20 29 3b 0a 0a  NT(aSpace1) );..
17fb6 20 20 2f 2a 0a 20 20 2a 2a 20 4c 6f 61 64 20 70    /*.  ** Load p
17fb7 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 63  ointers to all c
17fb8 65 6c 6c 73 20 6f 6e 20 73 69 62 6c 69 6e 67 20  ells on sibling 
17fb9 70 61 67 65 73 20 61 6e 64 20 74 68 65 20 64 69  pages and the di
17fba 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a  vider cells.  **
17fbb 20 69 6e 74 6f 20 74 68 65 20 6c 6f 63 61 6c 20   into the local 
17fbc 61 70 43 65 6c 6c 5b 5d 20 61 72 72 61 79 2e 20  apCell[] array. 
17fbd 20 4d 61 6b 65 20 63 6f 70 69 65 73 20 6f 66 20   Make copies of 
17fbe 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c  the divider cell
17fbf 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 73 70 61 63  s.  ** into spac
17fc0 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  e obtained from 
17fc1 61 53 70 61 63 65 31 5b 5d 20 61 6e 64 20 72 65  aSpace1[] and re
17fc2 6d 6f 76 65 20 74 68 65 20 74 68 65 20 64 69 76  move the the div
17fc3 69 64 65 72 20 43 65 6c 6c 73 0a 20 20 2a 2a 20  ider Cells.  ** 
17fc4 66 72 6f 6d 20 70 50 61 72 65 6e 74 2e 0a 20 20  from pParent..  
17fc5 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 73  **.  ** If the s
17fc6 69 62 6c 69 6e 67 73 20 61 72 65 20 6f 6e 20 6c  iblings are on l
17fc7 65 61 66 20 70 61 67 65 73 2c 20 74 68 65 6e 20  eaf pages, then 
17fc8 74 68 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65  the child pointe
17fc9 72 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 64  rs of the.  ** d
17fca 69 76 69 64 65 72 20 63 65 6c 6c 73 20 61 72 65  ivider cells are
17fcb 20 73 74 72 69 70 70 65 64 20 66 72 6f 6d 20 74   stripped from t
17fcc 68 65 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 20  he cells before 
17fcd 74 68 65 79 20 61 72 65 20 63 6f 70 69 65 64 0a  they are copied.
17fce 20 20 2a 2a 20 69 6e 74 6f 20 61 53 70 61 63 65    ** into aSpace
17fcf 31 5b 5d 2e 20 20 49 6e 20 74 68 69 73 20 77 61  1[].  In this wa
17fd0 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20  y, all cells in 
17fd1 61 70 43 65 6c 6c 5b 5d 20 61 72 65 20 77 69 74  apCell[] are wit
17fd2 68 6f 75 74 0a 20 20 2a 2a 20 63 68 69 6c 64 20  hout.  ** child 
17fd3 70 6f 69 6e 74 65 72 73 2e 20 20 49 66 20 73 69  pointers.  If si
17fd4 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c  blings are not l
17fd5 65 61 76 65 73 2c 20 74 68 65 6e 20 61 6c 6c 20  eaves, then all 
17fd6 63 65 6c 6c 20 69 6e 0a 20 20 2a 2a 20 61 70 43  cell in.  ** apC
17fd7 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64 65 20 63 68  ell[] include ch
17fd8 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 45  ild pointers.  E
17fd9 69 74 68 65 72 20 77 61 79 2c 20 61 6c 6c 20 63  ither way, all c
17fda 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d  ells in apCell[]
17fdb 0a 20 20 2a 2a 20 61 72 65 20 61 6c 69 6b 65 2e  .  ** are alike.
17fdc 0a 20 20 2a 2a 0a 20 20 2a 2a 20 6c 65 61 66 43  .  **.  ** leafC
17fdd 6f 72 72 65 63 74 69 6f 6e 3a 20 20 34 20 69 66  orrection:  4 if
17fde 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 66   pPage is a leaf
17fdf 2e 20 20 30 20 69 66 20 70 50 61 67 65 20 69 73  .  0 if pPage is
17fe0 20 6e 6f 74 20 61 20 6c 65 61 66 2e 0a 20 20 2a   not a leaf..  *
17fe1 2a 20 20 20 20 20 20 20 6c 65 61 66 44 61 74 61  *       leafData
17fe2 3a 20 20 31 20 69 66 20 70 50 61 67 65 20 68 6f  :  1 if pPage ho
17fe3 6c 64 73 20 6b 65 79 2b 64 61 74 61 20 61 6e 64  lds key+data and
17fe4 20 70 50 61 72 65 6e 74 20 68 6f 6c 64 73 20 6f   pParent holds o
17fe5 6e 6c 79 20 6b 65 79 73 2e 0a 20 20 2a 2f 0a 20  nly keys..  */. 
17fe6 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 20   leafCorrection 
17fe7 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 6c 65 61 66  = apOld[0]->leaf
17fe8 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 20 3d  *4;.  leafData =
17fe9 20 61 70 4f 6c 64 5b 30 5d 2d 3e 68 61 73 44 61   apOld[0]->hasDa
17fea 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ta;.  for(i=0; i
17feb 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nOld; i++){.   
17fec 20 69 6e 74 20 6c 69 6d 69 74 3b 0a 20 20 20 20   int limit;.    
17fed 0a 20 20 20 20 2f 2a 20 42 65 66 6f 72 65 20 64  .    /* Before d
17fee 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 65 6c  oing anything el
17fef 73 65 2c 20 74 61 6b 65 20 61 20 63 6f 70 79 20  se, take a copy 
17ff0 6f 66 20 74 68 65 20 69 27 74 68 20 6f 72 69 67  of the i'th orig
17ff1 69 6e 61 6c 20 73 69 62 6c 69 6e 67 0a 20 20 20  inal sibling.   
17ff2 20 2a 2a 20 54 68 65 20 72 65 73 74 20 6f 66 20   ** The rest of 
17ff3 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69  this function wi
17ff4 6c 6c 20 75 73 65 20 64 61 74 61 20 66 72 6f 6d  ll use data from
17ff5 20 74 68 65 20 63 6f 70 69 65 73 20 72 61 74 68   the copies rath
17ff6 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74  er.    ** that t
17ff7 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65  he original page
17ff8 73 20 73 69 6e 63 65 20 74 68 65 20 6f 72 69 67  s since the orig
17ff9 69 6e 61 6c 20 70 61 67 65 73 20 77 69 6c 6c 20  inal pages will 
17ffa 62 65 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a  be in the.    **
17ffb 20 70 72 6f 63 65 73 73 20 6f 66 20 62 65 69 6e   process of bein
17ffc 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20  g overwritten.  
17ffd 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  */.    MemPage *
17ffe 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 69 5d  pOld = apCopy[i]
17fff 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 26 61 53   = (MemPage*)&aS
18000 70 61 63 65 31 5b 70 42 74 2d 3e 70 61 67 65 53  pace1[pBt->pageS
18001 69 7a 65 20 2b 20 6b 2a 69 5d 3b 0a 20 20 20 20  ize + k*i];.    
18002 6d 65 6d 63 70 79 28 70 4f 6c 64 2c 20 61 70 4f  memcpy(pOld, apO
18003 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f 66 28 4d 65  ld[i], sizeof(Me
18004 6d 50 61 67 65 29 29 3b 0a 20 20 20 20 70 4f 6c  mPage));.    pOl
18005 64 2d 3e 61 44 61 74 61 20 3d 20 28 76 6f 69 64  d->aData = (void
18006 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0a 20 20 20 20  *)&pOld[1];.    
18007 6d 65 6d 63 70 79 28 70 4f 6c 64 2d 3e 61 44 61  memcpy(pOld->aDa
18008 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44  ta, apOld[i]->aD
18009 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69  ata, pBt->pageSi
1800a 7a 65 29 3b 0a 0a 20 20 20 20 6c 69 6d 69 74 20  ze);..    limit 
1800b 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 2b 70 4f  = pOld->nCell+pO
1800c 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20  ld->nOverflow;. 
1800d 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6c 69     for(j=0; j<li
1800e 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  mit; j++){.     
1800f 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e   assert( nCell<n
18010 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20  MaxCells );.    
18011 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20    apCell[nCell] 
18012 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65  = findOverflowCe
18013 6c 6c 28 70 4f 6c 64 2c 20 6a 29 3b 0a 20 20 20  ll(pOld, j);.   
18014 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d     szCell[nCell]
18015 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
18016 4f 6c 64 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c  Old, apCell[nCel
18017 6c 5d 29 3b 0a 20 20 20 20 20 20 6e 43 65 6c 6c  l]);.      nCell
18018 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ++;.    }.    if
18019 28 20 69 3c 6e 4f 6c 64 2d 31 20 26 26 20 21 6c  ( i<nOld-1 && !l
1801a 65 61 66 44 61 74 61 29 7b 0a 20 20 20 20 20 20  eafData){.      
1801b 75 31 36 20 73 7a 20 3d 20 28 75 31 36 29 73 7a  u16 sz = (u16)sz
1801c 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 75 38  New[i];.      u8
1801d 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 61   *pTemp;.      a
1801e 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61  ssert( nCell<nMa
1801f 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20  xCells );.      
18020 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20  szCell[nCell] = 
18021 73 7a 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20  sz;.      pTemp 
18022 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70 61 63  = &aSpace1[iSpac
18023 65 31 5d 3b 0a 20 20 20 20 20 20 69 53 70 61 63  e1];.      iSpac
18024 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20  e1 += sz;.      
18025 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d  assert( sz<=pBt-
18026 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20  >pageSize/4 );. 
18027 20 20 20 20 20 61 73 73 65 72 74 28 20 69 53 70       assert( iSp
18028 61 63 65 31 3c 3d 70 42 74 2d 3e 70 61 67 65 53  ace1<=pBt->pageS
18029 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d  ize );.      mem
1802a 63 70 79 28 70 54 65 6d 70 2c 20 61 70 44 69 76  cpy(pTemp, apDiv
1802b 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20  [i], sz);.      
1802c 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20  apCell[nCell] = 
1802d 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72 72 65 63  pTemp+leafCorrec
1802e 74 69 6f 6e 3b 0a 20 20 20 20 20 20 61 73 73 65  tion;.      asse
1802f 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69  rt( leafCorrecti
18030 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 66 43 6f 72  on==0 || leafCor
18031 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20  rection==4 );.  
18032 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c      szCell[nCell
18033 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c  ] = szCell[nCell
18034 5d 20 2d 20 6c 65 61 66 43 6f 72 72 65 63 74 69  ] - leafCorrecti
18035 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  on;.      if( !p
18036 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  Old->leaf ){.   
18037 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61       assert( lea
18038 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 29  fCorrection==0 )
18039 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1803a 28 20 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73 65  ( pOld->hdrOffse
1803b 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  t==0 );.        
1803c 2f 2a 20 54 68 65 20 72 69 67 68 74 20 70 6f 69  /* The right poi
1803d 6e 74 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c  nter of the chil
1803e 64 20 70 61 67 65 20 70 4f 6c 64 20 62 65 63 6f  d page pOld beco
1803f 6d 65 73 20 74 68 65 20 6c 65 66 74 0a 20 20 20  mes the left.   
18040 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20       ** pointer 
18041 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63  of the divider c
18042 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 6d  ell */.        m
18043 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43 65  emcpy(apCell[nCe
18044 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61 74  ll], &pOld->aDat
18045 61 5b 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20  a[8], 4);.      
18046 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
18047 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65  ssert( leafCorre
18048 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20  ction==4 );.    
18049 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6e      if( szCell[n
1804a 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 20 20 20  Cell]<4 ){.     
1804b 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61       /* Do not a
1804c 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c 73 20 73  llow any cells s
1804d 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34 20 62 79  maller than 4 by
1804e 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tes. */.        
1804f 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20    szCell[nCell] 
18050 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 4;.        }. 
18051 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 65       }.      nCe
18052 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ll++;.    }.  }.
18053 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 67 75 72  .  /*.  ** Figur
18054 65 20 6f 75 74 20 74 68 65 20 6e 75 6d 62 65 72  e out the number
18055 20 6f 66 20 70 61 67 65 73 20 6e 65 65 64 65 64   of pages needed
18056 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6e 43 65   to hold all nCe
18057 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a 2a 20 53  ll cells..  ** S
18058 74 6f 72 65 20 74 68 69 73 20 6e 75 6d 62 65 72  tore this number
18059 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73 6f 20 63   in "k".  Also c
1805a 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b 5d 20 77  ompute szNew[] w
1805b 68 69 63 68 20 69 73 20 74 68 65 20 74 6f 74 61  hich is the tota
1805c 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 61  l.  ** size of a
1805d 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20  ll cells on the 
1805e 69 2d 74 68 20 70 61 67 65 20 61 6e 64 20 63 6e  i-th page and cn
1805f 74 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20  tNew[] which is 
18060 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 69  the index.  ** i
18061 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66 20 74 68  n apCell[] of th
18062 65 20 63 65 6c 6c 20 74 68 61 74 20 64 69 76 69  e cell that divi
18063 64 65 73 20 70 61 67 65 20 69 20 66 72 6f 6d 20  des page i from 
18064 70 61 67 65 20 69 2b 31 2e 20 20 0a 20 20 2a 2a  page i+1.  .  **
18065 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68 6f 75 6c   cntNew[k] shoul
18066 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c 2e 0a 20  d equal nCell.. 
18067 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75 65 73 20   **.  ** Values 
18068 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68 69 73  computed by this
18069 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a 20 20 2a   block:.  **.  *
1806a 2a 20 20 20 20 20 20 20 20 20 20 20 6b 3a 20 54  *           k: T
1806b 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
1806c 6f 66 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73  of sibling pages
1806d 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65 77 5b 69  .  **    szNew[i
1806e 5d 3a 20 53 70 61 63 65 64 20 75 73 65 64 20 6f  ]: Spaced used o
1806f 6e 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69  n the i-th sibli
18070 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 20 20  ng page..  **   
18071 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e 64 65 78  cntNew[i]: Index
18072 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 6e 64   in apCell[] and
18073 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72 20 74 68   szCell[] for th
18074 65 20 66 69 72 73 74 20 63 65 6c 6c 20 74 6f 0a  e first cell to.
18075 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
18076 20 20 74 68 65 20 72 69 67 68 74 20 6f 66 20 74    the right of t
18077 68 65 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20  he i-th sibling 
18078 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73 61 62 6c  page..  ** usabl
18079 65 53 70 61 63 65 3a 20 4e 75 6d 62 65 72 20 6f  eSpace: Number o
1807a 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  f bytes of space
1807b 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 65 61   available on ea
1807c 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20 20 2a 2a  ch sibling..  **
1807d 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62 6c 65 53   .  */.  usableS
1807e 70 61 63 65 20 3d 20 70 42 74 2d 3e 75 73 61 62  pace = pBt->usab
1807f 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b 20 6c 65  leSize - 12 + le
18080 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20  afCorrection;.  
18081 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d 6b 3d 69  for(subtotal=k=i
18082 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b  =0; i<nCell; i++
18083 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ){.    assert( i
18084 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20  <nMaxCells );.  
18085 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d 20 73 7a    subtotal += sz
18086 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a 20 20 20  Cell[i] + 2;.   
18087 20 69 66 28 20 73 75 62 74 6f 74 61 6c 20 3e 20   if( subtotal > 
18088 75 73 61 62 6c 65 53 70 61 63 65 20 29 7b 0a 20  usableSpace ){. 
18089 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20       szNew[k] = 
1808a 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a 43 65 6c  subtotal - szCel
1808b 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63 6e 74 4e  l[i];.      cntN
1808c 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20 20 20 20  ew[k] = i;.     
1808d 20 69 66 28 20 6c 65 61 66 44 61 74 61 20 29 7b   if( leafData ){
1808e 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 73 75   i--; }.      su
1808f 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 20 20  btotal = 0;.    
18090 20 20 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28    k++;.      if(
18091 20 6b 3e 4e 42 2b 31 20 29 7b 20 72 63 20 3d 20   k>NB+1 ){ rc = 
18092 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 20  SQLITE_CORRUPT; 
18093 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
18094 61 6e 75 70 3b 20 7d 0a 20 20 20 20 7d 0a 20 20  anup; }.    }.  
18095 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73  }.  szNew[k] = s
18096 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e 74 4e 65  ubtotal;.  cntNe
18097 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b 0a 20 20  w[k] = nCell;.  
18098 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  k++;..  /*.  ** 
18099 54 68 65 20 70 61 63 6b 69 6e 67 20 63 6f 6d 70  The packing comp
1809a 75 74 65 64 20 62 79 20 74 68 65 20 70 72 65 76  uted by the prev
1809b 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73 20 62 69  ious block is bi
1809c 61 73 65 64 20 74 6f 77 61 72 64 20 74 68 65 20  ased toward the 
1809d 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a 20 6f 6e  siblings.  ** on
1809e 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 2e 20   the left side. 
1809f 20 54 68 65 20 6c 65 66 74 20 73 69 62 6c 69 6e   The left siblin
180a0 67 73 20 61 72 65 20 61 6c 77 61 79 73 20 6e 65  gs are always ne
180a1 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68 69 6c 65  arly full, while
180a2 20 74 68 65 0a 20 20 2a 2a 20 72 69 67 68 74 2d   the.  ** right-
180a3 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20 6d 69 67  most sibling mig
180a4 68 74 20 62 65 20 6e 65 61 72 6c 79 20 65 6d 70  ht be nearly emp
180a5 74 79 2e 20 20 54 68 69 73 20 62 6c 6f 63 6b 20  ty.  This block 
180a6 6f 66 20 63 6f 64 65 20 61 74 74 65 6d 70 74 73  of code attempts
180a7 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75 73 74 20  .  ** to adjust 
180a8 74 68 65 20 70 61 63 6b 69 6e 67 20 6f 66 20 73  the packing of s
180a9 69 62 6c 69 6e 67 73 20 74 6f 20 67 65 74 20 61  iblings to get a
180aa 20 62 65 74 74 65 72 20 62 61 6c 61 6e 63 65 2e   better balance.
180ab 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
180ac 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6d 6f  adjustment is mo
180ad 72 65 20 74 68 61 6e 20 61 6e 20 6f 70 74 69 6d  re than an optim
180ae 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 61  ization.  The pa
180af 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d 69 67 68  cking above migh
180b0 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20 6f 75 74  t.  ** be so out
180b1 20 6f 66 20 62 61 6c 61 6e 63 65 20 61 73 20 74   of balance as t
180b2 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e 20 20 46  o be illegal.  F
180b3 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  or example, the 
180b4 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a 20  right-most.  ** 
180b5 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65  sibling might be
180b6 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74   completely empt
180b7 79 2e 20 20 54 68 69 73 20 61 64 6a 75 73 74 6d  y.  This adjustm
180b8 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70 74 69 6f  ent is not optio
180b9 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  nal..  */.  for(
180ba 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d 29  i=k-1; i>0; i--)
180bb 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52 69 67 68  {.    int szRigh
180bc 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b 20 20 2f  t = szNew[i];  /
180bd 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 6e  * Size of siblin
180be 67 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20 2a  g on the right *
180bf 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c 65 66 74  /.    int szLeft
180c0 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d 3b 20 2f   = szNew[i-1]; /
180c1 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 6e  * Size of siblin
180c2 67 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 2a 2f  g on the left */
180c3 0a 20 20 20 20 69 6e 74 20 72 3b 20 20 20 20 20  .    int r;     
180c4 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
180c5 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 20  x of right-most 
180c6 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20 73 69 62  cell in left sib
180c7 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ling */.    int 
180c8 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
180c9 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72 73  /* Index of firs
180ca 74 20 63 65 6c 6c 20 74 6f 20 74 68 65 20 6c 65  t cell to the le
180cb 66 74 20 6f 66 20 72 69 67 68 74 20 73 69 62 6c  ft of right sibl
180cc 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72 20 3d 20  ing */..    r = 
180cd 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b  cntNew[i-1] - 1;
180ce 0a 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d  .    d = r + 1 -
180cf 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 61   leafData;.    a
180d0 73 73 65 72 74 28 20 64 3c 6e 4d 61 78 43 65 6c  ssert( d<nMaxCel
180d1 6c 73 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ls );.    assert
180d2 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ( r<nMaxCells );
180d3 0a 20 20 20 20 77 68 69 6c 65 28 20 73 7a 52 69  .    while( szRi
180d4 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52 69 67 68  ght==0 || szRigh
180d5 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d 73  t+szCell[d]+2<=s
180d6 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c 5b 72 5d  zLeft-(szCell[r]
180d7 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 73 7a 52  +2) ){.      szR
180d8 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c 6c 5b 64  ight += szCell[d
180d9 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 73 7a 4c  ] + 2;.      szL
180da 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c 5b 72 5d  eft -= szCell[r]
180db 20 2b 20 32 3b 0a 20 20 20 20 20 20 63 6e 74 4e   + 2;.      cntN
180dc 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 20 20 20  ew[i-1]--;.     
180dd 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d   r = cntNew[i-1]
180de 20 2d 20 31 3b 0a 20 20 20 20 20 20 64 20 3d 20   - 1;.      d = 
180df 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 61  r + 1 - leafData
180e0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4e 65  ;.    }.    szNe
180e1 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68 74 3b 0a  w[i] = szRight;.
180e2 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31 5d 20 3d      szNew[i-1] =
180e3 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a 0a 20 20   szLeft;.  }..  
180e4 2f 2a 20 45 69 74 68 65 72 20 77 65 20 66 6f 75  /* Either we fou
180e5 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63  nd one or more c
180e6 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b 30 5d 29  ells (cntnew[0])
180e7 3e 30 29 20 6f 72 20 70 50 61 67 65 20 69 73 0a  >0) or pPage is.
180e8 20 20 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 72    ** a virtual r
180e9 6f 6f 74 20 70 61 67 65 2e 20 20 41 20 76 69 72  oot page.  A vir
180ea 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69  tual root page i
180eb 73 20 77 68 65 6e 20 74 68 65 20 72 65 61 6c 20  s when the real 
180ec 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67 65 20 69  root.  ** page i
180ed 73 20 70 61 67 65 20 31 20 61 6e 64 20 77 65 20  s page 1 and we 
180ee 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 63 68 69  are the only chi
180ef 6c 64 20 6f 66 20 74 68 61 74 20 70 61 67 65 2e  ld of that page.
180f0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
180f1 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c 7c 20 28  cntNew[0]>0 || (
180f2 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 3d 3d 31  pParent->pgno==1
180f3 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65   && pParent->nCe
180f4 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 20 54 52 41  ll==0) );..  TRA
180f5 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f 6c  CE(("BALANCE: ol
180f6 64 3a 20 25 64 20 25 64 20 25 64 20 20 22 2c 0a  d: %d %d %d  ",.
180f7 20 20 20 20 61 70 4f 6c 64 5b 30 5d 2d 3e 70 67      apOld[0]->pg
180f8 6e 6f 2c 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d 32  no, .    nOld>=2
180f9 20 3f 20 61 70 4f 6c 64 5b 31 5d 2d 3e 70 67 6e   ? apOld[1]->pgn
180fa 6f 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64 3e  o : 0,.    nOld>
180fb 3d 33 20 3f 20 61 70 4f 6c 64 5b 32 5d 2d 3e 70  =3 ? apOld[2]->p
180fc 67 6e 6f 20 3a 20 30 0a 20 20 29 29 3b 0a 0a 20  gno : 0.  ));.. 
180fd 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74   /*.  ** Allocat
180fe 65 20 6b 20 6e 65 77 20 70 61 67 65 73 2e 20 20  e k new pages.  
180ff 52 65 75 73 65 20 6f 6c 64 20 70 61 67 65 73 20  Reuse old pages 
18100 77 68 65 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a  where possible..
18101 20 20 2a 2f 0a 20 20 69 66 28 20 61 70 4f 6c 64    */.  if( apOld
18102 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31 20 29 7b 0a  [0]->pgno<=1 ){.
18103 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
18104 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 67 6f 74  CORRUPT;.    got
18105 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  o balance_cleanu
18106 70 3b 0a 20 20 7d 0a 20 20 70 61 67 65 46 6c 61  p;.  }.  pageFla
18107 67 73 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 61  gs = apOld[0]->a
18108 44 61 74 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69  Data[0];.  for(i
18109 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20  =0; i<k; i++){. 
1810a 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77     MemPage *pNew
1810b 3b 0a 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64  ;.    if( i<nOld
1810c 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d   ){.      pNew =
1810d 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c   apNew[i] = apOl
1810e 64 5b 69 5d 3b 0a 20 20 20 20 20 20 61 70 4f 6c  d[i];.      apOl
1810f 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  d[i] = 0;.      
18110 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
18111 72 57 72 69 74 65 28 70 4e 65 77 2d 3e 70 44 62  rWrite(pNew->pDb
18112 50 61 67 65 29 3b 0a 20 20 20 20 20 20 6e 4e 65  Page);.      nNe
18113 77 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 72  w++;.      if( r
18114 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65  c ) goto balance
18115 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 65  _cleanup;.    }e
18116 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
18117 74 28 20 69 3e 30 20 29 3b 0a 20 20 20 20 20 20  t( i>0 );.      
18118 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72  rc = allocateBtr
18119 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65  eePage(pBt, &pNe
1811a 77 2c 20 26 70 67 6e 6f 2c 20 70 67 6e 6f 2c 20  w, &pgno, pgno, 
1811b 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
1811c 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f   ) goto balance_
1811d 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 61  cleanup;.      a
1811e 70 4e 65 77 5b 69 5d 20 3d 20 70 4e 65 77 3b 0a  pNew[i] = pNew;.
1811f 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 0a 20        nNew++;.. 
18120 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20       /* Set the 
18121 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
18122 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69  y for the new si
18123 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20  bling page. */. 
18124 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56       if( ISAUTOV
18125 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 20  ACUUM ){.       
18126 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
18127 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d  pNew->pgno, PTRM
18128 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e  AP_BTREE, pParen
18129 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20  t->pgno, &rc);. 
1812a 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
1812b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1812c 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e        goto balan
1812d 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  ce_cleanup;.    
1812e 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1812f 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72    }.  }..  /* Fr
18130 65 65 20 61 6e 79 20 6f 6c 64 20 70 61 67 65 73  ee any old pages
18131 20 74 68 61 74 20 77 65 72 65 20 6e 6f 74 20 72   that were not r
18132 65 75 73 65 64 20 61 73 20 6e 65 77 20 70 61 67  eused as new pag
18133 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65  es..  */.  while
18134 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 20  ( i<nOld ){.    
18135 66 72 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69  freePage(apOld[i
18136 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28  ], &rc);.    if(
18137 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e   rc ) goto balan
18138 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  ce_cleanup;.    
18139 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4f 6c  releasePage(apOl
1813a 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70 4f 6c 64  d[i]);.    apOld
1813b 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 69 2b 2b  [i] = 0;.    i++
1813c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ;.  }..  /*.  **
1813d 20 50 75 74 20 74 68 65 20 6e 65 77 20 70 61 67   Put the new pag
1813e 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20  es in accending 
1813f 6f 72 64 65 72 2e 20 20 54 68 69 73 20 68 65 6c  order.  This hel
18140 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65 65 70 20  ps to.  ** keep 
18141 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 64  entries in the d
18142 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f 72 64 65  isk file in orde
18143 72 20 73 6f 20 74 68 61 74 20 61 20 73 63 61 6e  r so that a scan
18144 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62  .  ** of the tab
18145 6c 65 20 69 73 20 61 20 6c 69 6e 65 61 72 20 73  le is a linear s
18146 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20  can through the 
18147 66 69 6c 65 2e 20 20 54 68 61 74 0a 20 20 2a 2a  file.  That.  **
18148 20 69 6e 20 74 75 72 6e 20 68 65 6c 70 73 20 74   in turn helps t
18149 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  he operating sys
1814a 74 65 6d 20 74 6f 20 64 65 6c 69 76 65 72 20 70  tem to deliver p
1814b 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 74  ages.  ** from t
1814c 68 65 20 64 69 73 6b 20 6d 6f 72 65 20 72 61 70  he disk more rap
1814d 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  idly..  **.  ** 
1814e 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73 65 72 74  An O(n^2) insert
1814f 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f 72 69 74  ion sort algorit
18150 68 6d 20 69 73 20 75 73 65 64 2c 20 62 75 74 20  hm is used, but 
18151 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20 69 73 20  since.  ** n is 
18152 6e 65 76 65 72 20 6d 6f 72 65 20 74 68 61 6e 20  never more than 
18153 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63 6f 6e 73  NB (a small cons
18154 74 61 6e 74 29 2c 20 74 68 61 74 20 73 68 6f 75  tant), that shou
18155 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 61  ld.  ** not be a
18156 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a 2a 0a 20   problem..  **. 
18157 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d 33 2c 20   ** When NB==3, 
18158 74 68 69 73 20 6f 6e 65 20 6f 70 74 69 6d 69 7a  this one optimiz
18159 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74 68 65 20  ation makes the 
1815a 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 61 62  database.  ** ab
1815b 6f 75 74 20 32 35 25 20 66 61 73 74 65 72 20 66  out 25% faster f
1815c 6f 72 20 6c 61 72 67 65 20 69 6e 73 65 72 74 69  or large inserti
1815d 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74 69 6f 6e  ons and deletion
1815e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  s..  */.  for(i=
1815f 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b 29 7b 0a  0; i<k-1; i++){.
18160 20 20 20 20 69 6e 74 20 6d 69 6e 56 20 3d 20 61      int minV = a
18161 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0a 20  pNew[i]->pgno;. 
18162 20 20 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69 3b     int minI = i;
18163 0a 20 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b 20  .    for(j=i+1; 
18164 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  j<k; j++){.     
18165 20 69 66 28 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70   if( apNew[j]->p
18166 67 6e 6f 3c 28 75 6e 73 69 67 6e 65 64 29 6d 69  gno<(unsigned)mi
18167 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 69  nV ){.        mi
18168 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20  nI = j;.        
18169 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b 6a 5d 2d  minV = apNew[j]-
1816a 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  >pgno;.      }. 
1816b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e     }.    if( min
1816c 49 3e 69 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  I>i ){.      int
1816d 20 74 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67   t;.      MemPag
1816e 65 20 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 3d  e *pT;.      t =
1816f 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 3b   apNew[i]->pgno;
18170 0a 20 20 20 20 20 20 70 54 20 3d 20 61 70 4e 65  .      pT = apNe
18171 77 5b 69 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65  w[i];.      apNe
18172 77 5b 69 5d 20 3d 20 61 70 4e 65 77 5b 6d 69 6e  w[i] = apNew[min
18173 49 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b  I];.      apNew[
18174 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20 20 20 20  minI] = pT;.    
18175 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45 28 28 22  }.  }.  TRACE(("
18176 6e 65 77 3a 20 25 64 28 25 64 29 20 25 64 28 25  new: %d(%d) %d(%
18177 64 29 20 25 64 28 25 64 29 20 25 64 28 25 64 29  d) %d(%d) %d(%d)
18178 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20   %d(%d)\n",.    
18179 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 6e 6f 2c 20  apNew[0]->pgno, 
1817a 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e 4e  szNew[0],.    nN
1817b 65 77 3e 3d 32 20 3f 20 61 70 4e 65 77 5b 31 5d  ew>=2 ? apNew[1]
1817c 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77  ->pgno : 0, nNew
1817d 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20 3a  >=2 ? szNew[1] :
1817e 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33 20   0,.    nNew>=3 
1817f 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e 70 67 6e 6f  ? apNew[2]->pgno
18180 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20   : 0, nNew>=3 ? 
18181 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20  szNew[2] : 0,.  
18182 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 61 70 4e 65    nNew>=4 ? apNe
18183 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20  w[3]->pgno : 0, 
18184 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e 65 77 5b  nNew>=4 ? szNew[
18185 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77  3] : 0,.    nNew
18186 3e 3d 35 20 3f 20 61 70 4e 65 77 5b 34 5d 2d 3e  >=5 ? apNew[4]->
18187 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d  pgno : 0, nNew>=
18188 35 20 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20 30  5 ? szNew[4] : 0
18189 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ));..  assert( s
1818a 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1818b 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e  teable(pParent->
1818c 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 70 75  pDbPage) );.  pu
1818d 74 34 62 79 74 65 28 70 52 69 67 68 74 2c 20 61  t4byte(pRight, a
1818e 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 70 67  pNew[nNew-1]->pg
1818f 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  no);..  /*.  ** 
18190 45 76 65 6e 6c 79 20 64 69 73 74 72 69 62 75 74  Evenly distribut
18191 65 20 74 68 65 20 64 61 74 61 20 69 6e 20 61 70  e the data in ap
18192 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74 68  Cell[] across th
18193 65 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a  e new pages..  *
18194 2a 20 49 6e 73 65 72 74 20 64 69 76 69 64 65 72  * Insert divider
18195 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61 72   cells into pPar
18196 65 6e 74 20 61 73 20 6e 65 63 65 73 73 61 72 79  ent as necessary
18197 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b 0a  ..  */.  j = 0;.
18198 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65    for(i=0; i<nNe
18199 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20  w; i++){.    /* 
1819a 41 73 73 65 6d 62 6c 65 20 74 68 65 20 6e 65 77  Assemble the new
1819b 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a   sibling page. *
1819c 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  /.    MemPage *p
1819d 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a  New = apNew[i];.
1819e 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d      assert( j<nM
1819f 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 7a  axCells );.    z
181a0 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 70 61  eroPage(pNew, pa
181a1 67 65 46 6c 61 67 73 29 3b 0a 20 20 20 20 61 73  geFlags);.    as
181a2 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c  semblePage(pNew,
181a3 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c 20 26 61   cntNew[i]-j, &a
181a4 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a 43 65 6c  pCell[j], &szCel
181a5 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73 73 65 72  l[j]);.    asser
181a6 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c 6c 3e 30  t( pNew->nCell>0
181a7 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20 26 26 20   || (nNew==1 && 
181a8 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29 20 29 3b  cntNew[0]==0) );
181a9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65  .    assert( pNe
181aa 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20  w->nOverflow==0 
181ab 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 63 6e 74 4e  );..    j = cntN
181ac 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f 2a 20 49  ew[i];..    /* I
181ad 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61  f the sibling pa
181ae 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f  ge assembled abo
181af 76 65 20 77 61 73 20 6e 6f 74 20 74 68 65 20 72  ve was not the r
181b0 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e  ight-most siblin
181b1 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 74  g,.    ** insert
181b2 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20   a divider cell 
181b3 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20  into the parent 
181b4 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  page..    */.   
181b5 20 61 73 73 65 72 74 28 20 69 3c 6e 4e 65 77 2d   assert( i<nNew-
181b6 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b  1 || j==nCell );
181b7 0a 20 20 20 20 69 66 28 20 6a 3c 6e 43 65 6c 6c  .    if( j<nCell
181b8 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43   ){.      u8 *pC
181b9 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a 70  ell;.      u8 *p
181ba 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20  Temp;.      int 
181bb 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72  sz;..      asser
181bc 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29  t( j<nMaxCells )
181bd 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20  ;.      pCell = 
181be 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20  apCell[j];.     
181bf 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d 20   sz = szCell[j] 
181c0 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  + leafCorrection
181c1 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20  ;.      pTemp = 
181c2 26 61 4f 76 66 6c 53 70 61 63 65 5b 69 4f 76 66  &aOvflSpace[iOvf
181c3 6c 53 70 61 63 65 5d 3b 0a 20 20 20 20 20 20 69  lSpace];.      i
181c4 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61 66 20 29  f( !pNew->leaf )
181c5 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
181c6 28 26 70 4e 65 77 2d 3e 61 44 61 74 61 5b 38 5d  (&pNew->aData[8]
181c7 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 20  , pCell, 4);.   
181c8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 61     }else if( lea
181c9 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20  fData ){.       
181ca 20 2f 2a 20 49 66 20 74 68 65 20 74 72 65 65 20   /* If the tree 
181cb 69 73 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74  is a leaf-data t
181cc 72 65 65 2c 20 61 6e 64 20 74 68 65 20 73 69 62  ree, and the sib
181cd 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 76 65 73  lings are leaves
181ce 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  , .        ** th
181cf 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64  en there is no d
181d0 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 20 61  ivider cell in a
181d1 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61 64  pCell[]. Instead
181d2 2c 20 74 68 65 20 64 69 76 69 64 65 72 20 0a 20  , the divider . 
181d3 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20 63         ** cell c
181d4 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 69  onsists of the i
181d5 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20 74  nteger key for t
181d6 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 65  he right-most ce
181d7 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 20 20 2a  ll of .        *
181d8 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d 70 61  * the sibling-pa
181d9 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f  ge assembled abo
181da 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20 20  ve only..       
181db 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 65 6c 6c   */.        Cell
181dc 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20  Info info;.     
181dd 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20 20     j--;.        
181de 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
181df 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 6a  r(pNew, apCell[j
181e0 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20  ], &info);.     
181e1 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d 70     pCell = pTemp
181e2 3b 0a 20 20 20 20 20 20 20 20 73 7a 20 3d 20 34  ;.        sz = 4
181e3 20 2b 20 70 75 74 56 61 72 69 6e 74 28 26 70 43   + putVarint(&pC
181e4 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f 2e 6e 4b 65  ell[4], info.nKe
181e5 79 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d  y);.        pTem
181e6 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  p = 0;.      }el
181e7 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 65 6c  se{.        pCel
181e8 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20 20  l -= 4;.        
181e9 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 73 65 20  /* Obscure case 
181ea 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74  for non-leaf-dat
181eb 61 20 74 72 65 65 73 3a 20 49 66 20 74 68 65 20  a trees: If the 
181ec 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c 20 77 61  cell at pCell wa
181ed 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65  s.        ** pre
181ee 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 64 20 6f  viously stored o
181ef 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20 61  n a leaf node, a
181f0 6e 64 20 69 74 73 20 72 65 70 6f 72 74 65 64 20  nd its reported 
181f1 73 69 7a 65 20 77 61 73 20 34 0a 20 20 20 20 20  size was 4.     
181f2 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 74 68 65     ** bytes, the
181f3 6e 20 69 74 20 6d 61 79 20 61 63 74 75 61 6c 6c  n it may actuall
181f4 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61  y be smaller tha
181f5 6e 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 20  n this .        
181f6 2a 2a 20 28 73 65 65 20 62 74 72 65 65 50 61 72  ** (see btreePar
181f7 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 34 20 62  seCellPtr(), 4 b
181f8 79 74 65 73 20 69 73 20 74 68 65 20 6d 69 6e 69  ytes is the mini
181f9 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20 20 20 20  mum size of.    
181fa 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 6c 6c 29      ** any cell)
181fb 2e 20 42 75 74 20 69 74 20 69 73 20 69 6d 70 6f  . But it is impo
181fc 72 74 61 6e 74 20 74 6f 20 70 61 73 73 20 74 68  rtant to pass th
181fd 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 74  e correct size t
181fe 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  o .        ** in
181ff 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20 72  sertCell(), so r
18200 65 70 61 72 73 65 20 74 68 65 20 63 65 6c 6c 20  eparse the cell 
18201 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  now..        **.
18202 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20          ** Note 
18203 74 68 61 74 20 74 68 69 73 20 63 61 6e 20 6e 65  that this can ne
18204 76 65 72 20 68 61 70 70 65 6e 20 69 6e 20 61 6e  ver happen in an
18205 20 53 51 4c 69 74 65 20 64 61 74 61 20 66 69 6c   SQLite data fil
18206 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20 20 20 20  e, as all.      
18207 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 61    ** cells are a
18208 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 2e  t least 4 bytes.
18209 20 49 74 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73   It only happens
1820a 20 69 6e 20 62 2d 74 72 65 65 73 20 75 73 65 64   in b-trees used
1820b 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 65  .        ** to e
1820c 76 61 6c 75 61 74 65 20 22 49 4e 20 28 53 45 4c  valuate "IN (SEL
1820d 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64 20 73 69  ECT ...)" and si
1820e 6d 69 6c 61 72 20 63 6c 61 75 73 65 73 2e 0a 20  milar clauses.. 
1820f 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
18210 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d 3d    if( szCell[j]=
18211 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =4 ){.          
18212 61 73 73 65 72 74 28 6c 65 61 66 43 6f 72 72 65  assert(leafCorre
18213 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20 20  ction==4);.     
18214 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69       sz = cellSi
18215 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 70  zePtr(pParent, p
18216 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d  Cell);.        }
18217 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
18218 4f 76 66 6c 53 70 61 63 65 20 2b 3d 20 73 7a 3b  OvflSpace += sz;
18219 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
1821a 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  z<=pBt->pageSize
1821b 2f 34 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  /4 );.      asse
1821c 72 74 28 20 69 4f 76 66 6c 53 70 61 63 65 3c 3d  rt( iOvflSpace<=
1821d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b  pBt->pageSize );
1821e 0a 20 20 20 20 20 20 69 6e 73 65 72 74 43 65 6c  .      insertCel
1821f 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76  l(pParent, nxDiv
18220 2c 20 70 43 65 6c 6c 2c 20 73 7a 2c 20 70 54 65  , pCell, sz, pTe
18221 6d 70 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20  mp, pNew->pgno, 
18222 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20  &rc);.      if( 
18223 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
18224 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
18225 61 6e 75 70 3b 0a 20 20 20 20 20 20 61 73 73 65  anup;.      asse
18226 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
18227 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 72  Iswriteable(pPar
18228 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  ent->pDbPage) );
18229 0a 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20  ..      j++;.   
1822a 20 20 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20     nxDiv++;.    
1822b 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  }.  }.  assert( 
1822c 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73  j==nCell );.  as
1822d 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a  sert( nOld>0 );.
1822e 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30    assert( nNew>0
1822f 20 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46   );.  if( (pageF
18230 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29  lags & PTF_LEAF)
18231 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a  ==0 ){.    u8 *z
18232 43 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b  Child = &apCopy[
18233 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38  nOld-1]->aData[8
18234 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61  ];.    memcpy(&a
18235 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44  pNew[nNew-1]->aD
18236 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20  ata[8], zChild, 
18237 34 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69  4);.  }..  if( i
18238 73 52 6f 6f 74 20 26 26 20 70 50 61 72 65 6e 74  sRoot && pParent
18239 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 70 50  ->nCell==0 && pP
1823a 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74  arent->hdrOffset
1823b 3c 3d 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65  <=apNew[0]->nFre
1823c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  e ){.    /* The 
1823d 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65  root page of the
1823e 20 62 2d 74 72 65 65 20 6e 6f 77 20 63 6f 6e 74   b-tree now cont
1823f 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 54  ains no cells. T
18240 68 65 20 6f 6e 6c 79 20 73 69 62 6c 69 6e 67 0a  he only sibling.
18241 20 20 20 20 2a 2a 20 70 61 67 65 20 69 73 20 74      ** page is t
18242 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f  he right-child o
18243 66 20 74 68 65 20 70 61 72 65 6e 74 2e 20 43 6f  f the parent. Co
18244 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  py the contents 
18245 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 68  of the.    ** ch
18246 69 6c 64 20 70 61 67 65 20 69 6e 74 6f 20 74 68  ild page into th
18247 65 20 70 61 72 65 6e 74 2c 20 64 65 63 72 65 61  e parent, decrea
18248 73 69 6e 67 20 74 68 65 20 6f 76 65 72 61 6c 6c  sing the overall
18249 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 0a 20   height of the. 
1824a 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 73 74 72     ** b-tree str
1824b 75 63 74 75 72 65 20 62 79 20 6f 6e 65 2e 20 54  ucture by one. T
1824c 68 69 73 20 69 73 20 64 65 73 63 72 69 62 65 64  his is described
1824d 20 61 73 20 74 68 65 20 22 62 61 6c 61 6e 63 65   as the "balance
1824e 2d 73 68 61 6c 6c 6f 77 65 72 22 0a 20 20 20 20  -shallower".    
1824f 2a 2a 20 73 75 62 2d 61 6c 67 6f 72 69 74 68 6d  ** sub-algorithm
18250 20 69 6e 20 73 6f 6d 65 20 64 6f 63 75 6d 65 6e   in some documen
18251 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a 20  tation..    **. 
18252 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73     ** If this is
18253 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   an auto-vacuum 
18254 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63 61  database, the ca
18255 6c 6c 20 74 6f 20 63 6f 70 79 4e 6f 64 65 43 6f  ll to copyNodeCo
18256 6e 74 65 6e 74 28 29 20 0a 20 20 20 20 2a 2a 20  ntent() .    ** 
18257 73 65 74 73 20 61 6c 6c 20 70 6f 69 6e 74 65 72  sets all pointer
18258 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 63 6f 72  -map entries cor
18259 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 64 61  responding to da
1825a 74 61 62 61 73 65 20 69 6d 61 67 65 20 70 61 67  tabase image pag
1825b 65 73 20 0a 20 20 20 20 2a 2a 20 66 6f 72 20 77  es .    ** for w
1825c 68 69 63 68 20 74 68 65 20 70 6f 69 6e 74 65 72  hich the pointer
1825d 20 69 73 20 73 74 6f 72 65 64 20 77 69 74 68 69   is stored withi
1825e 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 65  n the content be
1825f 69 6e 67 20 63 6f 70 69 65 64 2e 0a 20 20 20 20  ing copied..    
18260 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65  **.    ** The se
18261 63 6f 6e 64 20 61 73 73 65 72 74 20 62 65 6c 6f  cond assert belo
18262 77 20 76 65 72 69 66 69 65 73 20 74 68 61 74 20  w verifies that 
18263 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 69  the child page i
18264 73 20 64 65 66 72 61 67 6d 65 6e 74 65 64 0a 20  s defragmented. 
18265 20 20 20 2a 2a 20 28 69 74 20 6d 75 73 74 20 62     ** (it must b
18266 65 2c 20 61 73 20 69 74 20 77 61 73 20 6a 75 73  e, as it was jus
18267 74 20 72 65 63 6f 6e 73 74 72 75 63 74 65 64 20  t reconstructed 
18268 75 73 69 6e 67 20 61 73 73 65 6d 62 6c 65 50 61  using assemblePa
18269 67 65 28 29 29 2e 20 54 68 69 73 0a 20 20 20 20  ge()). This.    
1826a 2a 2a 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  ** is important 
1826b 69 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  if the parent pa
1826c 67 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65  ge happens to be
1826d 20 70 61 67 65 20 31 20 6f 66 20 74 68 65 20 64   page 1 of the d
1826e 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 69  atabase.    ** i
1826f 6d 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 61 73  mage.  */.    as
18270 73 65 72 74 28 20 6e 4e 65 77 3d 3d 31 20 29 3b  sert( nNew==1 );
18271 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 70 4e  .    assert( apN
18272 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 3d 3d 20  ew[0]->nFree == 
18273 0a 20 20 20 20 20 20 20 20 28 67 65 74 32 62 79  .        (get2by
18274 74 65 28 26 61 70 4e 65 77 5b 30 5d 2d 3e 61 44  te(&apNew[0]->aD
18275 61 74 61 5b 35 5d 29 2d 61 70 4e 65 77 5b 30 5d  ata[5])-apNew[0]
18276 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2d 61 70 4e  ->cellOffset-apN
18277 65 77 5b 30 5d 2d 3e 6e 43 65 6c 6c 2a 32 29 20  ew[0]->nCell*2) 
18278 0a 20 20 20 20 29 3b 0a 20 20 20 20 63 6f 70 79  .    );.    copy
18279 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 61 70 4e 65  NodeContent(apNe
1827a 77 5b 30 5d 2c 20 70 50 61 72 65 6e 74 2c 20 26  w[0], pParent, &
1827b 72 63 29 3b 0a 20 20 20 20 66 72 65 65 50 61 67  rc);.    freePag
1827c 65 28 61 70 4e 65 77 5b 30 5d 2c 20 26 72 63 29  e(apNew[0], &rc)
1827d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 49 53  ;.  }else if( IS
1827e 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20  AUTOVACUUM ){.  
1827f 20 20 2f 2a 20 46 69 78 20 74 68 65 20 70 6f 69    /* Fix the poi
18280 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73  nter-map entries
18281 20 66 6f 72 20 61 6c 6c 20 74 68 65 20 63 65 6c   for all the cel
18282 6c 73 20 74 68 61 74 20 77 65 72 65 20 73 68 69  ls that were shi
18283 66 74 65 64 20 61 72 6f 75 6e 64 2e 20 0a 20 20  fted around. .  
18284 20 20 2a 2a 20 54 68 65 72 65 20 61 72 65 20 73    ** There are s
18285 65 76 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74  everal different
18286 20 74 79 70 65 73 20 6f 66 20 70 6f 69 6e 74 65   types of pointe
18287 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 74 68  r-map entries th
18288 61 74 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 2a  at need to.    *
18289 2a 20 62 65 20 64 65 61 6c 74 20 77 69 74 68 20  * be dealt with 
1828a 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  by this routine.
1828b 20 53 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 68   Some of these h
1828c 61 76 65 20 62 65 65 6e 20 73 65 74 20 61 6c 72  ave been set alr
1828d 65 61 64 79 2c 20 62 75 74 0a 20 20 20 20 2a 2a  eady, but.    **
1828e 20 6d 61 6e 79 20 68 61 76 65 20 6e 6f 74 2e 20   many have not. 
1828f 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73  The following is
18290 20 61 20 73 75 6d 6d 61 72 79 3a 0a 20 20 20 20   a summary:.    
18291 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 31 29 20 54  **.    **   1) T
18292 68 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63  he entries assoc
18293 69 61 74 65 64 20 77 69 74 68 20 6e 65 77 20 73  iated with new s
18294 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74 68 61  ibling pages tha
18295 74 20 77 65 72 65 20 6e 6f 74 0a 20 20 20 20 2a  t were not.    *
18296 2a 20 20 20 20 20 20 73 69 62 6c 69 6e 67 73 20  *      siblings 
18297 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
18298 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 54  on was called. T
18299 68 65 73 65 20 68 61 76 65 20 61 6c 72 65 61 64  hese have alread
1829a 79 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 65  y.    **      be
1829b 65 6e 20 73 65 74 2e 20 57 65 20 64 6f 6e 27 74  en set. We don't
1829c 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61   need to worry a
1829d 62 6f 75 74 20 6f 6c 64 20 73 69 62 6c 69 6e 67  bout old sibling
1829e 73 20 74 68 61 74 20 77 65 72 65 0a 20 20 20 20  s that were.    
1829f 2a 2a 20 20 20 20 20 20 6d 6f 76 65 64 20 74 6f  **      moved to
182a0 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2d   the free-list -
182a1 20 74 68 65 20 66 72 65 65 50 61 67 65 28 29 20   the freePage() 
182a2 63 6f 64 65 20 68 61 73 20 74 61 6b 65 6e 20 63  code has taken c
182a3 61 72 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  are.    **      
182a4 6f 66 20 74 68 6f 73 65 2e 0a 20 20 20 20 2a 2a  of those..    **
182a5 0a 20 20 20 20 2a 2a 20 20 20 32 29 20 54 68 65  .    **   2) The
182a6 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74   pointer-map ent
182a7 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ries associated 
182a8 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20 6f  with the first o
182a9 76 65 72 66 6c 6f 77 0a 20 20 20 20 2a 2a 20 20  verflow.    **  
182aa 20 20 20 20 70 61 67 65 20 69 6e 20 61 6e 79 20      page in any 
182ab 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 73 20  overflow chains 
182ac 75 73 65 64 20 62 79 20 6e 65 77 20 64 69 76 69  used by new divi
182ad 64 65 72 20 63 65 6c 6c 73 2e 20 54 68 65 73 65  der cells. These
182ae 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 68 61   .    **      ha
182af 76 65 20 61 6c 73 6f 20 61 6c 72 65 61 64 79 20  ve also already 
182b0 62 65 65 6e 20 74 61 6b 65 6e 20 63 61 72 65 20  been taken care 
182b1 6f 66 20 62 79 20 74 68 65 20 69 6e 73 65 72 74  of by the insert
182b2 43 65 6c 6c 28 29 20 63 6f 64 65 2e 0a 20 20 20  Cell() code..   
182b3 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 33 29 20   **.    **   3) 
182b4 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70  If the sibling p
182b5 61 67 65 73 20 61 72 65 20 6e 6f 74 20 6c 65 61  ages are not lea
182b6 76 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68  ves, then the ch
182b7 69 6c 64 20 70 61 67 65 73 20 6f 66 0a 20 20 20  ild pages of.   
182b8 20 2a 2a 20 20 20 20 20 20 63 65 6c 6c 73 20 73   **      cells s
182b9 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 73 69 62  tored on the sib
182ba 6c 69 6e 67 20 70 61 67 65 73 20 6d 61 79 20 6e  ling pages may n
182bb 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65  eed to be update
182bc 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  d..    **.    **
182bd 20 20 20 34 29 20 49 66 20 74 68 65 20 73 69 62     4) If the sib
182be 6c 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e  ling pages are n
182bf 6f 74 20 69 6e 74 65 72 6e 61 6c 20 69 6e 74 6b  ot internal intk
182c0 65 79 20 6e 6f 64 65 73 2c 20 74 68 65 6e 20 61  ey nodes, then a
182c1 6e 79 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6f  ny.    **      o
182c2 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 75 73  verflow pages us
182c3 65 64 20 62 79 20 74 68 65 73 65 20 63 65 6c 6c  ed by these cell
182c4 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65  s may need to be
182c5 20 75 70 64 61 74 65 64 0a 20 20 20 20 2a 2a 20   updated.    ** 
182c6 20 20 20 20 20 28 69 6e 74 65 72 6e 61 6c 20 69       (internal i
182c7 6e 74 6b 65 79 20 6e 6f 64 65 73 20 6e 65 76 65  ntkey nodes neve
182c8 72 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 65  r contain pointe
182c9 72 73 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70  rs to overflow p
182ca 61 67 65 73 29 2e 0a 20 20 20 20 2a 2a 0a 20 20  ages)..    **.  
182cb 20 20 2a 2a 20 20 20 35 29 20 49 66 20 74 68 65    **   5) If the
182cc 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61   sibling pages a
182cd 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74  re not leaves, t
182ce 68 65 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d  hen the pointer-
182cf 6d 61 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  map.    **      
182d0 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65 20  entries for the 
182d1 72 69 67 68 74 2d 63 68 69 6c 64 20 70 61 67 65  right-child page
182d2 73 20 6f 66 20 65 61 63 68 20 73 69 62 6c 69 6e  s of each siblin
182d3 67 20 6d 61 79 20 6e 65 65 64 0a 20 20 20 20 2a  g may need.    *
182d4 2a 20 20 20 20 20 20 74 6f 20 62 65 20 75 70 64  *      to be upd
182d5 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  ated..    **.   
182d6 20 2a 2a 20 43 61 73 65 73 20 31 20 61 6e 64 20   ** Cases 1 and 
182d7 32 20 61 72 65 20 64 65 61 6c 74 20 77 69 74 68  2 are dealt with
182d8 20 61 62 6f 76 65 20 62 79 20 6f 74 68 65 72 20   above by other 
182d9 63 6f 64 65 2e 20 54 68 65 20 6e 65 78 74 0a 20  code. The next. 
182da 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 64 65 61 6c     ** block deal
182db 73 20 77 69 74 68 20 63 61 73 65 73 20 33 20 61  s with cases 3 a
182dc 6e 64 20 34 20 61 6e 64 20 74 68 65 20 6f 6e 65  nd 4 and the one
182dd 20 61 66 74 65 72 20 74 68 61 74 2c 20 63 61 73   after that, cas
182de 65 20 35 2e 20 53 69 6e 63 65 0a 20 20 20 20 2a  e 5. Since.    *
182df 2a 20 73 65 74 74 69 6e 67 20 61 20 70 6f 69 6e  * setting a poin
182e0 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 69 73  ter map entry is
182e1 20 61 20 72 65 6c 61 74 69 76 65 6c 79 20 65 78   a relatively ex
182e2 70 65 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f  pensive operatio
182e3 6e 2c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 63  n, this.    ** c
182e4 6f 64 65 20 6f 6e 6c 79 20 73 65 74 73 20 70 6f  ode only sets po
182e5 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65  inter map entrie
182e6 73 20 66 6f 72 20 63 68 69 6c 64 20 6f 72 20 6f  s for child or o
182e7 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 74 68  verflow pages th
182e8 61 74 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 61  at have.    ** a
182e9 63 74 75 61 6c 6c 79 20 6d 6f 76 65 64 20 62 65  ctually moved be
182ea 74 77 65 65 6e 20 70 61 67 65 73 2e 20 20 2a 2f  tween pages.  */
182eb 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e  .    MemPage *pN
182ec 65 77 20 3d 20 61 70 4e 65 77 5b 30 5d 3b 0a 20  ew = apNew[0];. 
182ed 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64     MemPage *pOld
182ee 20 3d 20 61 70 43 6f 70 79 5b 30 5d 3b 0a 20 20   = apCopy[0];.  
182ef 20 20 69 6e 74 20 6e 4f 76 65 72 66 6c 6f 77 20    int nOverflow 
182f0 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  = pOld->nOverflo
182f1 77 3b 0a 20 20 20 20 69 6e 74 20 69 4e 65 78 74  w;.    int iNext
182f2 4f 6c 64 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c  Old = pOld->nCel
182f3 6c 20 2b 20 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20  l + nOverflow;. 
182f4 20 20 20 69 6e 74 20 69 4f 76 65 72 66 6c 6f 77     int iOverflow
182f5 20 3d 20 28 6e 4f 76 65 72 66 6c 6f 77 20 3f 20   = (nOverflow ? 
182f6 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69  pOld->aOvfl[0].i
182f7 64 78 20 3a 20 2d 31 29 3b 0a 20 20 20 20 6a 20  dx : -1);.    j 
182f8 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
182f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
182fa 20 2f 2a 20 43 75 72 72 65 6e 74 20 27 6f 6c 64   /* Current 'old
182fb 27 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 2a  ' sibling page *
182fc 2f 0a 20 20 20 20 6b 20 3d 20 30 3b 20 20 20 20  /.    k = 0;    
182fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
182fe 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
182ff 65 6e 74 20 27 6e 65 77 27 20 73 69 62 6c 69 6e  ent 'new' siblin
18300 67 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 66 6f  g page */.    fo
18301 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  r(i=0; i<nCell; 
18302 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  i++){.      int 
18303 69 73 44 69 76 69 64 65 72 20 3d 20 30 3b 0a 20  isDivider = 0;. 
18304 20 20 20 20 20 77 68 69 6c 65 28 20 69 3d 3d 69       while( i==i
18305 4e 65 78 74 4f 6c 64 20 29 7b 0a 20 20 20 20 20  NextOld ){.     
18306 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20 69 73 20     /* Cell i is 
18307 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65 64 69 61  the cell immedia
18308 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  tely following t
18309 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 6f 6e 20  he last cell on 
1830a 6f 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  old.        ** s
1830b 69 62 6c 69 6e 67 20 70 61 67 65 20 6a 2e 20 49  ibling page j. I
1830c 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61  f the siblings a
1830d 72 65 20 6e 6f 74 20 6c 65 61 66 20 70 61 67 65  re not leaf page
1830e 73 20 6f 66 20 61 6e 0a 20 20 20 20 20 20 20 20  s of an.        
1830f 2a 2a 20 69 6e 74 6b 65 79 20 62 2d 74 72 65 65  ** intkey b-tree
18310 2c 20 74 68 65 6e 20 63 65 6c 6c 20 69 20 77 61  , then cell i wa
18311 73 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c  s a divider cell
18312 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f 6c  . */.        pOl
18313 64 20 3d 20 61 70 43 6f 70 79 5b 2b 2b 6a 5d 3b  d = apCopy[++j];
18314 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74 4f 6c  .        iNextOl
18315 64 20 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 74  d = i + !leafDat
18316 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 20  a + pOld->nCell 
18317 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  + pOld->nOverflo
18318 77 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  w;.        if( p
18319 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29  Old->nOverflow )
1831a 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 4f 76 65  {.          nOve
1831b 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e 4f  rflow = pOld->nO
1831c 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 20 20 20  verflow;.       
1831d 20 20 20 69 4f 76 65 72 66 6c 6f 77 20 3d 20 69     iOverflow = i
1831e 20 2b 20 21 6c 65 61 66 44 61 74 61 20 2b 20 70   + !leafData + p
1831f 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64  Old->aOvfl[0].id
18320 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  x;.        }.   
18321 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d       isDivider =
18322 20 21 6c 65 61 66 44 61 74 61 3b 20 20 0a 20 20   !leafData;  .  
18323 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73      }..      ass
18324 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3e 30 20  ert(nOverflow>0 
18325 7c 7c 20 69 4f 76 65 72 66 6c 6f 77 3c 69 20 29  || iOverflow<i )
18326 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e  ;.      assert(n
18327 4f 76 65 72 66 6c 6f 77 3c 32 20 7c 7c 20 70 4f  Overflow<2 || pO
18328 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78  ld->aOvfl[0].idx
18329 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31 5d  ==pOld->aOvfl[1]
1832a 2e 69 64 78 2d 31 29 3b 0a 20 20 20 20 20 20 61  .idx-1);.      a
1832b 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3c  ssert(nOverflow<
1832c 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c  3 || pOld->aOvfl
1832d 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61  [1].idx==pOld->a
1832e 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d 31 29 3b 0a  Ovfl[2].idx-1);.
1832f 20 20 20 20 20 20 69 66 28 20 69 3d 3d 69 4f 76        if( i==iOv
18330 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20  erflow ){.      
18331 20 20 69 73 44 69 76 69 64 65 72 20 3d 20 31 3b    isDivider = 1;
18332 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 2d 2d  .        if( (--
18333 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20 29 7b 0a  nOverflow)>0 ){.
18334 20 20 20 20 20 20 20 20 20 20 69 4f 76 65 72 66            iOverf
18335 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  low++;.        }
18336 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
18337 69 66 28 20 69 3d 3d 63 6e 74 4e 65 77 5b 6b 5d  if( i==cntNew[k]
18338 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
18339 65 6c 6c 20 69 20 69 73 20 74 68 65 20 63 65 6c  ell i is the cel
1833a 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f  l immediately fo
1833b 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74  llowing the last
1833c 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 0a 20 20 20   cell on new.   
1833d 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20       ** sibling 
1833e 70 61 67 65 20 6b 2e 20 49 66 20 74 68 65 20 73  page k. If the s
1833f 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20  iblings are not 
18340 6c 65 61 66 20 70 61 67 65 73 20 6f 66 20 61 6e  leaf pages of an
18341 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6b  .        ** intk
18342 65 79 20 62 2d 74 72 65 65 2c 20 74 68 65 6e 20  ey b-tree, then 
18343 63 65 6c 6c 20 69 20 69 73 20 61 20 64 69 76 69  cell i is a divi
18344 64 65 72 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20  der cell.  */.  
18345 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e        pNew = apN
18346 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 20 20 20 20  ew[++k];.       
18347 20 69 66 28 20 21 6c 65 61 66 44 61 74 61 20 29   if( !leafData )
18348 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
18349 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
1834a 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 20 20 20 20   j<nOld );.     
1834b 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e 65 77 20   assert( k<nNew 
1834c 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  );..      /* If 
1834d 74 68 65 20 63 65 6c 6c 20 77 61 73 20 6f 72 69  the cell was ori
1834e 67 69 6e 61 6c 6c 79 20 64 69 76 69 64 65 72 20  ginally divider 
1834f 63 65 6c 6c 20 28 61 6e 64 20 69 73 20 6e 6f 74  cell (and is not
18350 20 6e 6f 77 29 20 6f 72 0a 20 20 20 20 20 20 2a   now) or.      *
18351 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65  * an overflow ce
18352 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65 20 63 65  ll, or if the ce
18353 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65 64 20 6f  ll was located o
18354 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 73 69  n a different si
18355 62 6c 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 70  bling.      ** p
18356 61 67 65 20 62 65 66 6f 72 65 20 74 68 65 20 62  age before the b
18357 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 6e 20 74  alancing, then t
18358 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65  he pointer map e
18359 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65  ntries associate
1835a 64 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20  d.      ** with 
1835b 61 6e 79 20 63 68 69 6c 64 20 6f 72 20 6f 76 65  any child or ove
1835c 72 66 6c 6f 77 20 70 61 67 65 73 20 6e 65 65 64  rflow pages need
1835d 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 20   to be updated. 
1835e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73   */.      if( is
1835f 44 69 76 69 64 65 72 20 7c 7c 20 70 4f 6c 64 2d  Divider || pOld-
18360 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 6e  >pgno!=pNew->pgn
18361 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  o ){.        if(
18362 20 21 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e   !leafCorrection
18363 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 74   ){.          pt
18364 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65 74  rmapPut(pBt, get
18365 34 62 79 74 65 28 61 70 43 65 6c 6c 5b 69 5d 29  4byte(apCell[i])
18366 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20  , PTRMAP_BTREE, 
18367 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 63 29  pNew->pgno, &rc)
18368 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
18369 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 69      if( szCell[i
1836a 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c  ]>pNew->minLocal
1836b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 74   ){.          pt
1836c 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70  rmapPutOvflPtr(p
1836d 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c 20  New, apCell[i], 
1836e 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  &rc);.        }.
1836f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
18370 20 20 20 69 66 28 20 21 6c 65 61 66 43 6f 72 72     if( !leafCorr
18371 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  ection ){.      
18372 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b  for(i=0; i<nNew;
18373 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 75   i++){.        u
18374 33 32 20 6b 65 79 20 3d 20 67 65 74 34 62 79 74  32 key = get4byt
18375 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e 61 44 61  e(&apNew[i]->aDa
18376 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20 20  ta[8]);.        
18377 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6b  ptrmapPut(pBt, k
18378 65 79 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45  ey, PTRMAP_BTREE
18379 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f  , apNew[i]->pgno
1837a 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a  , &rc);.      }.
1837b 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20      }..#if 0.   
1837c 20 2f 2a 20 54 68 65 20 70 74 72 6d 61 70 43 68   /* The ptrmapCh
1837d 65 63 6b 50 61 67 65 73 28 29 20 63 6f 6e 74 61  eckPages() conta
1837e 69 6e 73 20 61 73 73 65 72 74 28 29 20 73 74 61  ins assert() sta
1837f 74 65 6d 65 6e 74 73 20 74 68 61 74 20 76 65 72  tements that ver
18380 69 66 79 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  ify that.    ** 
18381 61 6c 6c 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  all pointer map 
18382 70 61 67 65 73 20 61 72 65 20 73 65 74 20 63 6f  pages are set co
18383 72 72 65 63 74 6c 79 2e 20 54 68 69 73 20 69 73  rrectly. This is
18384 20 68 65 6c 70 66 75 6c 20 77 68 69 6c 65 20 0a   helpful while .
18385 20 20 20 20 2a 2a 20 64 65 62 75 67 67 69 6e 67      ** debugging
18386 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c 6c  . This is usuall
18387 79 20 64 69 73 61 62 6c 65 64 20 62 65 63 61 75  y disabled becau
18388 73 65 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  se a corrupt dat
18389 61 62 61 73 65 20 6d 61 79 0a 20 20 20 20 2a 2a  abase may.    **
1838a 20 63 61 75 73 65 20 61 6e 20 61 73 73 65 72 74   cause an assert
1838b 28 29 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  () statement to 
1838c 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 20 20 70 74  fail.  */.    pt
1838d 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28 61  rmapCheckPages(a
1838e 70 4e 65 77 2c 20 6e 4e 65 77 29 3b 0a 20 20 20  pNew, nNew);.   
1838f 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65   ptrmapCheckPage
18390 73 28 26 70 50 61 72 65 6e 74 2c 20 31 29 3b 0a  s(&pParent, 1);.
18391 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 61 73  #endif.  }..  as
18392 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 69  sert( pParent->i
18393 73 49 6e 69 74 20 29 3b 0a 20 20 54 52 41 43 45  sInit );.  TRACE
18394 28 28 22 42 41 4c 41 4e 43 45 3a 20 66 69 6e 69  (("BALANCE: fini
18395 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20 6e 65 77  shed: old=%d new
18396 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c 6e 22 2c  =%d cells=%d\n",
18397 0a 20 20 20 20 20 20 20 20 20 20 6e 4f 6c 64 2c  .          nOld,
18398 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b 0a   nNew, nCell));.
18399 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61 6e  .  /*.  ** Clean
1839a 75 70 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  up before return
1839b 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e 63  ing..  */.balanc
1839c 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c  e_cleanup:.  sql
1839d 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28  ite3ScratchFree(
1839e 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69  apCell);.  for(i
1839f 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29  =0; i<nOld; i++)
183a0 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67  {.    releasePag
183a1 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 7d  e(apOld[i]);.  }
183a2 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e  .  for(i=0; i<nN
183a3 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65  ew; i++){.    re
183a4 6c 65 61 73 65 50 61 67 65 28 61 70 4e 65 77 5b  leasePage(apNew[
183a5 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  i]);.  }..  retu
183a6 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
183a7 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
183a8 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  s called when th
183a9 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61  e root page of a
183aa 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72   b-tree structur
183ab 65 20 69 73 0a 2a 2a 20 6f 76 65 72 66 75 6c 6c  e is.** overfull
183ac 20 28 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f 72   (has one or mor
183ad 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  e overflow pages
183ae 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 63  )..**.** A new c
183af 68 69 6c 64 20 70 61 67 65 20 69 73 20 61 6c 6c  hild page is all
183b0 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 63  ocated and the c
183b1 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63  ontents of the c
183b2 75 72 72 65 6e 74 20 72 6f 6f 74 0a 2a 2a 20 70  urrent root.** p
183b3 61 67 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6f  age, including o
183b4 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2c 20 61  verflow cells, a
183b5 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74  re copied into t
183b6 68 65 20 63 68 69 6c 64 2e 20 54 68 65 20 72 6f  he child. The ro
183b7 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 74 68  ot.** page is th
183b8 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e 20 74  en overwritten t
183b9 6f 20 6d 61 6b 65 20 69 74 20 61 6e 20 65 6d 70  o make it an emp
183ba 74 79 20 70 61 67 65 20 77 69 74 68 20 74 68 65  ty page with the
183bb 20 72 69 67 68 74 2d 63 68 69 6c 64 20 0a 2a 2a   right-child .**
183bc 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 69 6e   pointer pointin
183bd 67 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67  g to the new pag
183be 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 20  e..**.** Before 
183bf 72 65 74 75 72 6e 69 6e 67 2c 20 61 6c 6c 20 70  returning, all p
183c0 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69  ointer-map entri
183c1 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  es corresponding
183c2 20 74 6f 20 70 61 67 65 73 20 0a 2a 2a 20 74 68   to pages .** th
183c3 61 74 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64  at the new child
183c4 2d 70 61 67 65 20 6e 6f 77 20 63 6f 6e 74 61 69  -page now contai
183c5 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61  ns pointers to a
183c6 72 65 20 75 70 64 61 74 65 64 2e 20 54 68 65 0a  re updated. The.
183c7 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72 65 73 70  ** entry corresp
183c8 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 6e 65  onding to the ne
183c9 77 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f  w right-child po
183ca 69 6e 74 65 72 20 6f 66 20 74 68 65 20 72 6f 6f  inter of the roo
183cb 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 6c 73  t.** page is als
183cc 6f 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  o updated..**.**
183cd 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
183ce 2a 70 70 43 68 69 6c 64 20 69 73 20 73 65 74 20  *ppChild is set 
183cf 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66  to contain a ref
183d0 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 68  erence to the ch
183d1 69 6c 64 20 0a 2a 2a 20 70 61 67 65 20 61 6e 64  ild .** page and
183d2 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
183d3 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20  turned. In this 
183d4 63 61 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20  case the caller 
183d5 69 73 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 74  is required.** t
183d6 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61  o call releasePa
183d7 67 65 28 29 20 6f 6e 20 2a 70 70 43 68 69 6c 64  ge() on *ppChild
183d8 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 2e 20 49   exactly once. I
183d9 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
183da 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63  s,.** an error c
183db 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
183dc 61 6e 64 20 2a 70 70 43 68 69 6c 64 20 69 73 20  and *ppChild is 
183dd 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61  set to 0..*/.sta
183de 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f  tic int balance_
183df 64 65 65 70 65 72 28 4d 65 6d 50 61 67 65 20 2a  deeper(MemPage *
183e0 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 67 65 20 2a  pRoot, MemPage *
183e1 2a 70 70 43 68 69 6c 64 29 7b 0a 20 20 69 6e 74  *ppChild){.  int
183e2 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
183e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
183e4 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d  eturn value from
183e5 20 73 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a   subprocedures *
183e6 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 68  /.  MemPage *pCh
183e7 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ild = 0;        
183e8 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
183e9 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67   a new child pag
183ea 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
183eb 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20  Child = 0;      
183ec 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
183ed 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20  mber of the new 
183ee 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20  child page */.  
183ef 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
183f0 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 20 20 20 2f  pRoot->pBt;    /
183f1 2a 20 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 0a  * The BTree */..
183f2 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d    assert( pRoot-
183f3 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a  >nOverflow>0 );.
183f4 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
183f5 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
183f6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f  ->mutex) );..  /
183f7 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c 20 74 68  * Make pRoot, th
183f8 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  e root page of t
183f9 68 65 20 62 2d 74 72 65 65 2c 20 77 72 69 74 61  he b-tree, writa
183fa 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65 20 61 20  ble. Allocate a 
183fb 6e 65 77 20 0a 20 20 2a 2a 20 70 61 67 65 20 74  new .  ** page t
183fc 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20  hat will become 
183fd 74 68 65 20 6e 65 77 20 72 69 67 68 74 2d 63 68  the new right-ch
183fe 69 6c 64 20 6f 66 20 70 50 61 67 65 2e 20 43 6f  ild of pPage. Co
183ff 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a  py the contents.
18400 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 6f 64 65    ** of the node
18401 20 73 74 6f 72 65 64 20 6f 6e 20 70 52 6f 6f 74   stored on pRoot
18402 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 63 68   into the new ch
18403 69 6c 64 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20  ild page..  */. 
18404 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
18405 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70  erWrite(pRoot->p
18406 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  DbPage);.  if( r
18407 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
18408 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74      rc = allocat
18409 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 26  eBtreePage(pBt,&
1840a 70 43 68 69 6c 64 2c 26 70 67 6e 6f 43 68 69 6c  pChild,&pgnoChil
1840b 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 30 29  d,pRoot->pgno,0)
1840c 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 43 6f  ;.    copyNodeCo
1840d 6e 74 65 6e 74 28 70 52 6f 6f 74 2c 20 70 43 68  ntent(pRoot, pCh
1840e 69 6c 64 2c 20 26 72 63 29 3b 0a 20 20 20 20 69  ild, &rc);.    i
1840f 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  f( ISAUTOVACUUM 
18410 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50  ){.      ptrmapP
18411 75 74 28 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c  ut(pBt, pgnoChil
18412 64 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  d, PTRMAP_BTREE,
18413 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 26 72   pRoot->pgno, &r
18414 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  c);.    }.  }.  
18415 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70  if( rc ){.    *p
18416 70 43 68 69 6c 64 20 3d 20 30 3b 0a 20 20 20 20  pChild = 0;.    
18417 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 68 69  releasePage(pChi
18418 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ld);.    return 
18419 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rc;.  }.  assert
1841a 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
1841b 77 72 69 74 65 61 62 6c 65 28 70 43 68 69 6c 64  writeable(pChild
1841c 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
1841d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
1841e 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
1841f 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20  pRoot->pDbPage) 
18420 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 68  );.  assert( pCh
18421 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 52 6f 6f  ild->nCell==pRoo
18422 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 54  t->nCell );..  T
18423 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20  RACE(("BALANCE: 
18424 63 6f 70 79 20 72 6f 6f 74 20 25 64 20 69 6e 74  copy root %d int
18425 6f 20 25 64 5c 6e 22 2c 20 70 52 6f 6f 74 2d 3e  o %d\n", pRoot->
18426 70 67 6e 6f 2c 20 70 43 68 69 6c 64 2d 3e 70 67  pgno, pChild->pg
18427 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79  no));..  /* Copy
18428 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65   the overflow ce
18429 6c 6c 73 20 66 72 6f 6d 20 70 52 6f 6f 74 20 74  lls from pRoot t
1842a 6f 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 6d 65  o pChild */.  me
1842b 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61 4f 76  mcpy(pChild->aOv
1842c 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 4f 76 66 6c  fl, pRoot->aOvfl
1842d 2c 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c  , pRoot->nOverfl
1842e 6f 77 2a 73 69 7a 65 6f 66 28 70 52 6f 6f 74 2d  ow*sizeof(pRoot-
1842f 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b 0a 20 20 70  >aOvfl[0]));.  p
18430 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77  Child->nOverflow
18431 20 3d 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66   = pRoot->nOverf
18432 6c 6f 77 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20  low;..  /* Zero 
18433 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
18434 70 52 6f 6f 74 2e 20 54 68 65 6e 20 69 6e 73 74  pRoot. Then inst
18435 61 6c 6c 20 70 43 68 69 6c 64 20 61 73 20 74 68  all pChild as th
18436 65 20 72 69 67 68 74 2d 63 68 69 6c 64 2e 20 2a  e right-child. *
18437 2f 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 52 6f  /.  zeroPage(pRo
18438 6f 74 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74  ot, pChild->aDat
18439 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c 45 41 46  a[0] & ~PTF_LEAF
1843a 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 70  );.  put4byte(&p
1843b 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f  Root->aData[pRoo
1843c 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c  t->hdrOffset+8],
1843d 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a 0a 20 20   pgnoChild);..  
1843e 2a 70 70 43 68 69 6c 64 20 3d 20 70 43 68 69 6c  *ppChild = pChil
1843f 64 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  d;.  return SQLI
18440 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
18441 54 68 65 20 70 61 67 65 20 74 68 61 74 20 70 43  The page that pC
18442 75 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  ur currently poi
18443 6e 74 73 20 74 6f 20 68 61 73 20 6a 75 73 74 20  nts to has just 
18444 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 69 6e  been modified in
18445 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 2e 20 54 68  .** some way. Th
18446 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 67 75  is function figu
18447 72 65 73 20 6f 75 74 20 69 66 20 74 68 69 73 20  res out if this 
18448 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 65 61  modification mea
18449 6e 73 20 74 68 65 0a 2a 2a 20 74 72 65 65 20 6e  ns the.** tree n
1844a 65 65 64 73 20 74 6f 20 62 65 20 62 61 6c 61 6e  eeds to be balan
1844b 63 65 64 2c 20 61 6e 64 20 69 66 20 73 6f 20 63  ced, and if so c
1844c 61 6c 6c 73 20 74 68 65 20 61 70 70 72 6f 70 72  alls the appropr
1844d 69 61 74 65 20 62 61 6c 61 6e 63 69 6e 67 20 0a  iate balancing .
1844e 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 42 61 6c 61  ** routine. Bala
1844f 6e 63 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61  ncing routines a
18450 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 62 61 6c 61  re:.**.**   bala
18451 6e 63 65 5f 71 75 69 63 6b 28 29 0a 2a 2a 20 20  nce_quick().**  
18452 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28   balance_deeper(
18453 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 6e  ).**   balance_n
18454 6f 6e 72 6f 6f 74 28 29 0a 2a 2f 0a 73 74 61 74  onroot().*/.stat
18455 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 28 42  ic int balance(B
18456 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
18457 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
18458 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 69 6e  E_OK;.  const in
18459 74 20 6e 4d 69 6e 20 3d 20 70 43 75 72 2d 3e 70  t nMin = pCur->p
1845a 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2a  Bt->usableSize *
1845b 20 32 20 2f 20 33 3b 0a 20 20 75 38 20 61 42 61   2 / 3;.  u8 aBa
1845c 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b  lanceQuickSpace[
1845d 31 33 5d 3b 0a 20 20 75 38 20 2a 70 46 72 65 65  13];.  u8 *pFree
1845e 20 3d 20 30 3b 0a 0a 20 20 54 45 53 54 4f 4e 4c   = 0;..  TESTONL
1845f 59 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 71  Y( int balance_q
18460 75 69 63 6b 5f 63 61 6c 6c 65 64 20 3d 20 30 20  uick_called = 0 
18461 29 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 69  );.  TESTONLY( i
18462 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65  nt balance_deepe
18463 72 5f 63 61 6c 6c 65 64 20 3d 20 30 20 29 3b 0a  r_called = 0 );.
18464 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e 74 20  .  do {.    int 
18465 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50  iPage = pCur->iP
18466 61 67 65 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65  age;.    MemPage
18467 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e   *pPage = pCur->
18468 61 70 50 61 67 65 5b 69 50 61 67 65 5d 3b 0a 0a  apPage[iPage];..
18469 20 20 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30      if( iPage==0
1846a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50   ){.      if( pP
1846b 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29  age->nOverflow )
1846c 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  {.        /* The
1846d 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68   root page of th
1846e 65 20 62 2d 74 72 65 65 20 69 73 20 6f 76 65 72  e b-tree is over
1846f 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61  full. In this ca
18470 73 65 20 63 61 6c 6c 20 74 68 65 0a 20 20 20 20  se call the.    
18471 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 64      ** balance_d
18472 65 65 70 65 72 28 29 20 66 75 6e 63 74 69 6f 6e  eeper() function
18473 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
18474 20 63 68 69 6c 64 20 66 6f 72 20 74 68 65 20 72   child for the r
18475 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20  oot-page.       
18476 20 2a 2a 20 61 6e 64 20 63 6f 70 79 20 74 68 65   ** and copy the
18477 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74   current content
18478 73 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61  s of the root-pa
18479 67 65 20 74 6f 20 69 74 2e 20 54 68 65 0a 20 20  ge to it. The.  
1847a 20 20 20 20 20 20 2a 2a 20 6e 65 78 74 20 69 74        ** next it
1847b 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  eration of the d
1847c 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61  o-loop will bala
1847d 6e 63 65 20 74 68 65 20 63 68 69 6c 64 20 70 61  nce the child pa
1847e 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 20 0a  ge..        */ .
1847f 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
18480 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 5f  (balance_deeper_
18481 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b 0a  called++)==0 );.
18482 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c          rc = bal
18483 61 6e 63 65 5f 64 65 65 70 65 72 28 70 50 61 67  ance_deeper(pPag
18484 65 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65  e, &pCur->apPage
18485 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66  [1]);.        if
18486 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
18487 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75  ){.          pCu
18488 72 2d 3e 69 50 61 67 65 20 3d 20 31 3b 0a 20 20  r->iPage = 1;.  
18489 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69          pCur->ai
1848a 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20  Idx[0] = 0;.    
1848b 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64        pCur->aiId
1848c 78 5b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  x[1] = 0;.      
1848d 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
1848e 2d 3e 61 70 50 61 67 65 5b 31 5d 2d 3e 6e 4f 76  ->apPage[1]->nOv
1848f 65 72 66 6c 6f 77 20 29 3b 0a 20 20 20 20 20 20  erflow );.      
18490 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
18491 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
18492 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
18493 65 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76  e if( pPage->nOv
18494 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61  erflow==0 && pPa
18495 67 65 2d 3e 6e 46 72 65 65 3c 3d 6e 4d 69 6e 20  ge->nFree<=nMin 
18496 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ){.      break;.
18497 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
18498 20 4d 65 6d 50 61 67 65 20 2a 20 63 6f 6e 73 74   MemPage * const
18499 20 70 50 61 72 65 6e 74 20 3d 20 70 43 75 72 2d   pParent = pCur-
1849a 3e 61 70 50 61 67 65 5b 69 50 61 67 65 2d 31 5d  >apPage[iPage-1]
1849b 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73  ;.      int cons
1849c 74 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e 61  t iIdx = pCur->a
1849d 69 49 64 78 5b 69 50 61 67 65 2d 31 5d 3b 0a 0a  iIdx[iPage-1];..
1849e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1849f 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
184a0 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a  rent->pDbPage);.
184a1 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
184a2 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 6e 64  LITE_OK ){.#ifnd
184a3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51  ef SQLITE_OMIT_Q
184a4 55 49 43 4b 42 41 4c 41 4e 43 45 0a 20 20 20 20  UICKBALANCE.    
184a5 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68      if( pPage->h
184a6 61 73 44 61 74 61 0a 20 20 20 20 20 20 20 20 20  asData.         
184a7 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  && pPage->nOverf
184a8 6c 6f 77 3d 3d 31 0a 20 20 20 20 20 20 20 20 20  low==1.         
184a9 26 26 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b  && pPage->aOvfl[
184aa 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65 2d 3e 6e  0].idx==pPage->n
184ab 43 65 6c 6c 0a 20 20 20 20 20 20 20 20 20 26 26  Cell.         &&
184ac 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 21 3d   pParent->pgno!=
184ad 31 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 50  1.         && pP
184ae 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 69 49  arent->nCell==iI
184af 64 78 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20  dx.        ){.  
184b0 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20          /* Call 
184b1 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20  balance_quick() 
184b2 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  to create a new 
184b3 73 69 62 6c 69 6e 67 20 6f 66 20 70 50 61 67 65  sibling of pPage
184b4 20 6f 6e 20 77 68 69 63 68 0a 20 20 20 20 20 20   on which.      
184b5 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f 72 65 20      ** to store 
184b6 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  the overflow cel
184b7 6c 2e 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b  l. balance_quick
184b8 28 29 20 69 6e 73 65 72 74 73 20 61 20 6e 65 77  () inserts a new
184b9 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 20 20   cell.          
184ba 2a 2a 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2c  ** into pParent,
184bb 20 77 68 69 63 68 20 6d 61 79 20 63 61 75 73 65   which may cause
184bc 20 70 50 61 72 65 6e 74 20 6f 76 65 72 66 6c 6f   pParent overflo
184bd 77 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 20  w. If this.     
184be 20 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c       ** happens,
184bf 20 74 68 65 20 6e 65 78 74 20 69 6e 74 65 72 61   the next intera
184c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c  tion of the do-l
184c1 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63 65  oop will balance
184c2 20 70 50 61 72 65 6e 74 20 0a 20 20 20 20 20 20   pParent .      
184c3 20 20 20 20 2a 2a 20 75 73 65 20 65 69 74 68 65      ** use eithe
184c4 72 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  r balance_nonroo
184c5 74 28 29 20 6f 72 20 62 61 6c 61 6e 63 65 5f 64  t() or balance_d
184c6 65 65 70 65 72 28 29 2e 20 55 6e 74 69 6c 20 74  eeper(). Until t
184c7 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  his.          **
184c8 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 6f 76   happens, the ov
184c9 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69 73 20 73  erflow cell is s
184ca 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61 42 61  tored in the aBa
184cb 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b  lanceQuickSpace[
184cc 5d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62  ].          ** b
184cd 75 66 66 65 72 2e 20 0a 20 20 20 20 20 20 20 20  uffer. .        
184ce 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a    **.          *
184cf 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66  * The purpose of
184d0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
184d1 73 73 65 72 74 28 29 20 69 73 20 74 6f 20 63 68  ssert() is to ch
184d2 65 63 6b 20 74 68 61 74 20 6f 6e 6c 79 20 61 0a  eck that only a.
184d3 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69 6e            ** sin
184d4 67 6c 65 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61  gle call to bala
184d5 6e 63 65 5f 71 75 69 63 6b 28 29 20 69 73 20 6d  nce_quick() is m
184d6 61 64 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c  ade for each cal
184d7 6c 20 74 6f 20 74 68 69 73 0a 20 20 20 20 20 20  l to this.      
184d8 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e      ** function.
184d9 20 49 66 20 74 68 69 73 20 77 65 72 65 20 6e 6f   If this were no
184da 74 20 76 65 72 69 66 69 65 64 2c 20 61 20 73 75  t verified, a su
184db 62 74 6c 65 20 62 75 67 20 69 6e 76 6f 6c 76 69  btle bug involvi
184dc 6e 67 20 72 65 75 73 65 0a 20 20 20 20 20 20 20  ng reuse.       
184dd 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 61 42 61     ** of the aBa
184de 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b  lanceQuickSpace[
184df 5d 20 6d 69 67 68 74 20 73 6e 65 61 6b 20 69 6e  ] might sneak in
184e0 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
184e1 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
184e2 20 28 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 5f   (balance_quick_
184e3 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b 0a  called++)==0 );.
184e4 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62            rc = b
184e5 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 70 50 61  alance_quick(pPa
184e6 72 65 6e 74 2c 20 70 50 61 67 65 2c 20 61 42 61  rent, pPage, aBa
184e7 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 29  lanceQuickSpace)
184e8 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a  ;.        }else.
184e9 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7b  #endif.        {
184ea 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  .          /* In
184eb 20 74 68 69 73 20 63 61 73 65 2c 20 63 61 6c 6c   this case, call
184ec 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74   balance_nonroot
184ed 28 29 20 74 6f 20 72 65 64 69 73 74 72 69 62 75  () to redistribu
184ee 74 65 20 63 65 6c 6c 73 0a 20 20 20 20 20 20 20  te cells.       
184ef 20 20 20 2a 2a 20 62 65 74 77 65 65 6e 20 70 50     ** between pP
184f0 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 32 20  age and up to 2 
184f1 6f 66 20 69 74 73 20 73 69 62 6c 69 6e 67 20 70  of its sibling p
184f2 61 67 65 73 2e 20 54 68 69 73 20 69 6e 76 6f 6c  ages. This invol
184f3 76 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ves.          **
184f4 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63   modifying the c
184f5 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 61 72 65  ontents of pPare
184f6 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61  nt, which may ca
184f7 75 73 65 20 70 50 61 72 65 6e 74 20 74 6f 0a 20  use pParent to. 
184f8 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63 6f           ** beco
184f9 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20 75  me overfull or u
184fa 6e 64 65 72 66 75 6c 6c 2e 20 54 68 65 20 6e 65  nderfull. The ne
184fb 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  xt iteration of 
184fc 74 68 65 20 64 6f 2d 6c 6f 6f 70 0a 20 20 20 20  the do-loop.    
184fd 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 61        ** will ba
184fe 6c 61 6e 63 65 20 74 68 65 20 70 61 72 65 6e 74  lance the parent
184ff 20 70 61 67 65 20 74 6f 20 63 6f 72 72 65 63 74   page to correct
18500 20 74 68 69 73 2e 0a 20 20 20 20 20 20 20 20 20   this..         
18501 20 2a 2a 20 0a 20 20 20 20 20 20 20 20 20 20 2a   ** .          *
18502 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20  * If the parent 
18503 70 61 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 65  page becomes ove
18504 72 66 75 6c 6c 2c 20 74 68 65 20 6f 76 65 72 66  rfull, the overf
18505 6c 6f 77 20 63 65 6c 6c 20 6f 72 20 63 65 6c 6c  low cell or cell
18506 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  s.          ** a
18507 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  re stored in the
18508 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 61   pSpace buffer a
18509 6c 6c 6f 63 61 74 65 64 20 69 6d 6d 65 64 69 61  llocated immedia
1850a 74 65 6c 79 20 62 65 6c 6f 77 2e 20 0a 20 20 20  tely below. .   
1850b 20 20 20 20 20 20 20 2a 2a 20 41 20 73 75 62 73         ** A subs
1850c 65 71 75 65 6e 74 20 69 74 65 72 61 74 69 6f 6e  equent iteration
1850d 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20   of the do-loop 
1850e 77 69 6c 6c 20 64 65 61 6c 20 77 69 74 68 20 74  will deal with t
1850f 68 69 73 20 62 79 0a 20 20 20 20 20 20 20 20 20  his by.         
18510 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 62 61 6c 61   ** calling bala
18511 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 28 62  nce_nonroot() (b
18512 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20  alance_deeper() 
18513 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 66 69  may be called fi
18514 72 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 2a  rst,.          *
18515 2a 20 62 75 74 20 69 74 20 64 6f 65 73 6e 27 74  * but it doesn't
18516 20 64 65 61 6c 20 77 69 74 68 20 6f 76 65 72 66   deal with overf
18517 6c 6f 77 20 63 65 6c 6c 73 20 2d 20 6a 75 73 74  low cells - just
18518 20 6d 6f 76 65 73 20 74 68 65 6d 20 74 6f 20 61   moves them to a
18519 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69  .          ** di
1851a 66 66 65 72 65 6e 74 20 70 61 67 65 29 2e 20 4f  fferent page). O
1851b 6e 63 65 20 74 68 69 73 20 73 75 62 73 65 71 75  nce this subsequ
1851c 65 6e 74 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61  ent call to bala
1851d 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 0a 20  nce_nonroot() . 
1851e 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 73 20           ** has 
1851f 63 6f 6d 70 6c 65 74 65 64 2c 20 69 74 20 69 73  completed, it is
18520 20 73 61 66 65 20 74 6f 20 72 65 6c 65 61 73 65   safe to release
18521 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66   the pSpace buff
18522 65 72 20 75 73 65 64 20 62 79 0a 20 20 20 20 20  er used by.     
18523 20 20 20 20 20 2a 2a 20 74 68 65 20 70 72 65 76       ** the prev
18524 69 6f 75 73 20 63 61 6c 6c 2c 20 61 73 20 74 68  ious call, as th
18525 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20  e overflow cell 
18526 64 61 74 61 20 77 69 6c 6c 20 68 61 76 65 20 62  data will have b
18527 65 65 6e 20 0a 20 20 20 20 20 20 20 20 20 20 2a  een .          *
18528 2a 20 63 6f 70 69 65 64 20 65 69 74 68 65 72 20  * copied either 
18529 69 6e 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66  into the body of
1852a 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65   a database page
1852b 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6e 65 77   or into the new
1852c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 53  .          ** pS
1852d 70 61 63 65 20 62 75 66 66 65 72 20 70 61 73 73  pace buffer pass
1852e 65 64 20 74 6f 20 74 68 65 20 6c 61 74 74 65 72  ed to the latter
1852f 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65   call to balance
18530 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 0a 20 20 20 20  _nonroot()..    
18531 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
18532 20 20 20 75 38 20 2a 70 53 70 61 63 65 20 3d 20     u8 *pSpace = 
18533 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f  sqlite3PageMallo
18534 63 28 70 43 75 72 2d 3e 70 42 74 2d 3e 70 61 67  c(pCur->pBt->pag
18535 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20  eSize);.        
18536 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e    rc = balance_n
18537 6f 6e 72 6f 6f 74 28 70 50 61 72 65 6e 74 2c 20  onroot(pParent, 
18538 69 49 64 78 2c 20 70 53 70 61 63 65 2c 20 69 50  iIdx, pSpace, iP
18539 61 67 65 3d 3d 31 29 3b 0a 20 20 20 20 20 20 20  age==1);.       
1853a 20 20 20 69 66 28 20 70 46 72 65 65 20 29 7b 0a     if( pFree ){.
1853b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1853c 66 20 70 46 72 65 65 20 69 73 20 6e 6f 74 20 4e  f pFree is not N
1853d 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20 74  ULL, it points t
1853e 6f 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66  o the pSpace buf
1853f 66 65 72 20 75 73 65 64 20 0a 20 20 20 20 20 20  fer used .      
18540 20 20 20 20 20 20 2a 2a 20 62 79 20 61 20 70 72        ** by a pr
18541 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 62  evious call to b
18542 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
18543 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 73 20 61  . Its contents a
18544 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  re.            *
18545 2a 20 6e 6f 77 20 73 74 6f 72 65 64 20 65 69 74  * now stored eit
18546 68 65 72 20 6f 6e 20 72 65 61 6c 20 64 61 74 61  her on real data
18547 62 61 73 65 20 70 61 67 65 73 20 6f 72 20 77 69  base pages or wi
18548 74 68 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20  thin the .      
18549 20 20 20 20 20 20 2a 2a 20 6e 65 77 20 70 53 70        ** new pSp
1854a 61 63 65 20 62 75 66 66 65 72 2c 20 73 6f 20 69  ace buffer, so i
1854b 74 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20  t may be safely 
1854c 66 72 65 65 64 20 68 65 72 65 2e 20 2a 2f 0a 20  freed here. */. 
1854d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1854e 65 33 50 61 67 65 46 72 65 65 28 70 46 72 65 65  e3PageFree(pFree
1854f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a  );.          }..
18550 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
18551 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 77   pSpace buffer w
18552 69 6c 6c 20 62 65 20 66 72 65 65 64 20 61 66 74  ill be freed aft
18553 65 72 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  er the next call
18554 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a   to.          **
18555 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74   balance_nonroot
18556 28 29 2c 20 6f 72 20 6a 75 73 74 20 62 65 66 6f  (), or just befo
18557 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  re this function
18558 20 72 65 74 75 72 6e 73 2c 20 77 68 69 63 68 65   returns, whiche
18559 76 65 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ver.          **
1855a 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 20 2a 2f   comes first. */
1855b 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 65 65  .          pFree
1855c 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 20 20 20   = pSpace;.     
1855d 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
1855e 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72      pPage->nOver
1855f 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 20 20 20  flow = 0;..     
18560 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 74 65   /* The next ite
18561 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f  ration of the do
18562 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65 73 20 74  -loop balances t
18563 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20  he parent page. 
18564 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  */.      release
18565 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
18566 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d     pCur->iPage--
18567 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65  ;.    }.  }while
18568 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
18569 29 3b 0a 0a 20 20 69 66 28 20 70 46 72 65 65 20  );..  if( pFree 
1856a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  ){.    sqlite3Pa
1856b 67 65 46 72 65 65 28 70 46 72 65 65 29 3b 0a 20  geFree(pFree);. 
1856c 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1856d 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74  }.../*.** Insert
1856e 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e   a new record in
1856f 74 6f 20 74 68 65 20 42 54 72 65 65 2e 20 20 54  to the BTree.  T
18570 68 65 20 6b 65 79 20 69 73 20 67 69 76 65 6e 20  he key is given 
18571 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0a 2a  by (pKey,nKey).*
18572 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 69  * and the data i
18573 73 20 67 69 76 65 6e 20 62 79 20 28 70 44 61 74  s given by (pDat
18574 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 65 20 63  a,nData).  The c
18575 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 6f 6e  ursor is used on
18576 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e 65 20  ly to.** define 
18577 77 68 61 74 20 74 61 62 6c 65 20 74 68 65 20 72  what table the r
18578 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 62 65 20  ecord should be 
18579 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 20 20  inserted into.  
1857a 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73  The cursor.** is
1857b 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
1857c 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61 74  t a random locat
1857d 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61  ion..**.** For a
1857e 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 2c 20  n INTKEY table, 
1857f 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20 76 61  only the nKey va
18580 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79 20 69  lue of the key i
18581 73 20 75 73 65 64 2e 20 20 70 4b 65 79 20 69 73  s used.  pKey is
18582 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 46 6f  .** ignored.  Fo
18583 72 20 61 20 5a 45 52 4f 44 41 54 41 20 74 61 62  r a ZERODATA tab
18584 6c 65 2c 20 74 68 65 20 70 44 61 74 61 20 61 6e  le, the pData an
18585 64 20 6e 44 61 74 61 20 61 72 65 20 62 6f 74 68  d nData are both
18586 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   ignored..**.** 
18587 49 66 20 74 68 65 20 73 65 65 6b 52 65 73 75 6c  If the seekResul
18588 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  t parameter is n
18589 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20  on-zero, then a 
1858a 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20  successful call 
1858b 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f 55 6e 70 61  to.** MovetoUnpa
1858c 63 6b 65 64 28 29 20 74 6f 20 73 65 65 6b 20 63  cked() to seek c
1858d 75 72 73 6f 72 20 70 43 75 72 20 74 6f 20 28 70  ursor pCur to (p
1858e 4b 65 79 2c 20 6e 4b 65 79 29 20 68 61 73 20 61  Key, nKey) has a
1858f 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70  lready.** been p
18590 65 72 66 6f 72 6d 65 64 2e 20 73 65 65 6b 52 65  erformed. seekRe
18591 73 75 6c 74 20 69 73 20 74 68 65 20 73 65 61 72  sult is the sear
18592 63 68 20 72 65 73 75 6c 74 20 72 65 74 75 72 6e  ch result return
18593 65 64 20 28 61 20 6e 65 67 61 74 69 76 65 0a 2a  ed (a negative.*
18594 2a 20 6e 75 6d 62 65 72 20 69 66 20 70 43 75 72  * number if pCur
18595 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 65 6e   points at an en
18596 74 72 79 20 74 68 61 74 20 69 73 20 73 6d 61 6c  try that is smal
18597 6c 65 72 20 74 68 61 6e 20 28 70 4b 65 79 2c 20  ler than (pKey, 
18598 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a 20 61 20 70  nKey), or.** a p
18599 6f 73 69 74 69 76 65 20 76 61 6c 75 65 20 69 66  ositive value if
1859a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20   pCur points at 
1859b 61 6e 20 65 74 72 79 20 74 68 61 74 20 69 73 20  an etry that is 
1859c 6c 61 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a 20  larger than .** 
1859d 28 70 4b 65 79 2c 20 6e 4b 65 79 29 29 2e 20 0a  (pKey, nKey)). .
1859e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 65  **.** If the see
1859f 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65  kResult paramete
185a0 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  r is non-zero, t
185a1 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 67  hen the caller g
185a2 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a  uarantees that.*
185a3 2a 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73  * cursor pCur is
185a4 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65   pointing at the
185a5 20 65 78 69 73 74 69 6e 67 20 63 6f 70 79 20 6f   existing copy o
185a6 66 20 61 20 72 6f 77 20 74 68 61 74 20 69 73 20  f a row that is 
185a7 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69  to be.** overwri
185a8 74 74 65 6e 2e 20 20 49 66 20 74 68 65 20 73 65  tten.  If the se
185a9 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74  ekResult paramet
185aa 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 63 75  er is 0, then cu
185ab 72 73 6f 72 20 70 43 75 72 20 6d 61 79 0a 2a 2a  rsor pCur may.**
185ac 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79 20 65 6e   point to any en
185ad 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20 65 6e 74  try or to no ent
185ae 72 79 20 61 74 20 61 6c 6c 20 61 6e 64 20 73 6f  ry at all and so
185af 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68   this function h
185b0 61 73 20 74 6f 20 73 65 65 6b 0a 2a 2a 20 74 68  as to seek.** th
185b1 65 20 63 75 72 73 6f 72 20 62 65 66 6f 72 65 20  e cursor before 
185b2 74 68 65 20 6e 65 77 20 6b 65 79 20 63 61 6e 20  the new key can 
185b3 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a  be inserted..*/.
185b4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
185b5 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
185b6 6e 73 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f  nsert(.  BtCurso
185b7 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20  r *pCur,        
185b8 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72          /* Inser
185b9 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20  t data into the 
185ba 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75  table of this cu
185bb 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rsor */.  const 
185bc 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20  void *pKey, i64 
185bd 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20  nKey,    /* The 
185be 6b 65 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72  key of the new r
185bf 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  ecord */.  const
185c0 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e   void *pData, in
185c1 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65  t nData,  /* The
185c2 20 64 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77   data of the new
185c3 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
185c4 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20   nZero,         
185c5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
185c6 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 30  umber of extra 0
185c7 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64   bytes to append
185c8 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e   to data */.  in
185c9 74 20 61 70 70 65 6e 64 42 69 61 73 2c 20 20 20  t appendBias,   
185ca 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
185cb 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
185cc 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64  likely an append
185cd 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65   */.  int seekRe
185ce 73 75 6c 74 20 20 20 20 20 20 20 20 20 20 20 20  sult            
185cf 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f       /* Result o
185d0 66 20 70 72 69 6f 72 20 4d 6f 76 65 74 6f 55 6e  f prior MovetoUn
185d1 70 61 63 6b 65 64 28 29 20 63 61 6c 6c 20 2a 2f  packed() call */
185d2 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
185d3 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65 6b 52 65  int loc = seekRe
185d4 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 2f  sult;          /
185d5 2a 20 2d 31 3a 20 62 65 66 6f 72 65 20 64 65 73  * -1: before des
185d6 69 72 65 64 20 6c 6f 63 61 74 69 6f 6e 20 20 2b  ired location  +
185d7 31 3a 20 61 66 74 65 72 20 2a 2f 0a 20 20 69 6e  1: after */.  in
185d8 74 20 73 7a 4e 65 77 20 3d 20 30 3b 0a 20 20 69  t szNew = 0;.  i
185d9 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67  nt idx;.  MemPag
185da 65 20 2a 70 50 61 67 65 3b 0a 20 20 42 74 72 65  e *pPage;.  Btre
185db 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74  e *p = pCur->pBt
185dc 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20  ree;.  BtShared 
185dd 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
185de 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
185df 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69 67  oldCell;.  unsig
185e0 6e 65 64 20 63 68 61 72 20 2a 6e 65 77 43 65 6c  ned char *newCel
185e1 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 43  l = 0;..  if( pC
185e2 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
185e3 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20  OR_FAULT ){.    
185e4 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b  assert( pCur->sk
185e5 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f  ipNext!=SQLITE_O
185e6 4b 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  K );.    return 
185e7 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a  pCur->skipNext;.
185e8 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 63    }..  assert( c
185e9 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
185ea 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
185eb 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20  t( pCur->wrFlag 
185ec 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  && pBt->inTransa
185ed 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
185ee 54 45 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64  TE && !pBt->read
185ef 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 74  Only );.  assert
185f0 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65  ( hasSharedCache
185f1 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75  TableLock(p, pCu
185f2 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75  r->pgnoRoot, pCu
185f3 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20  r->pKeyInfo!=0, 
185f4 32 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65  2) );..  /* Asse
185f5 72 74 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c  rt that the call
185f6 65 72 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73  er has been cons
185f7 69 73 74 65 6e 74 2e 20 49 66 20 74 68 69 73 20  istent. If this 
185f8 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65  cursor was opene
185f9 64 0a 20 20 2a 2a 20 65 78 70 65 63 74 69 6e 67  d.  ** expecting
185fa 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65   an index b-tree
185fb 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65  , then the calle
185fc 72 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65  r should be inse
185fd 72 74 69 6e 67 20 62 6c 6f 62 0a 20 20 2a 2a 20  rting blob.  ** 
185fe 6b 65 79 73 20 77 69 74 68 20 6e 6f 20 61 73 73  keys with no ass
185ff 6f 63 69 61 74 65 64 20 64 61 74 61 2e 20 49 66  ociated data. If
18600 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 20   the cursor was 
18601 6f 70 65 6e 65 64 20 65 78 70 65 63 74 69 6e 67  opened expecting
18602 20 61 6e 0a 20 20 2a 2a 20 69 6e 74 6b 65 79 20   an.  ** intkey 
18603 74 61 62 6c 65 2c 20 74 68 65 20 63 61 6c 6c 65  table, the calle
18604 72 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65  r should be inse
18605 72 74 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65  rting integer ke
18606 79 73 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 62  ys with a.  ** b
18607 6c 6f 62 20 6f 66 20 61 73 73 6f 63 69 61 74 65  lob of associate
18608 64 20 64 61 74 61 2e 20 20 2a 2f 0a 20 20 61 73  d data.  */.  as
18609 73 65 72 74 28 20 28 70 4b 65 79 3d 3d 30 29 3d  sert( (pKey==0)=
1860a 3d 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f  =(pCur->pKeyInfo
1860b 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  ==0) );..  /* If
1860c 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 73 65   this is an inse
1860d 72 74 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20  rt into a table 
1860e 62 2d 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 61  b-tree, invalida
1860f 74 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20  te any incrblob 
18610 0a 20 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70  .  ** cursors op
18611 65 6e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65  en on the row be
18612 69 6e 67 20 72 65 70 6c 61 63 65 64 20 28 61 73  ing replaced (as
18613 73 75 6d 69 6e 67 20 74 68 69 73 20 69 73 20 61  suming this is a
18614 20 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f 70   replace.  ** op
18615 65 72 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 20  eration - if it 
18616 69 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c  is not, the foll
18617 6f 77 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f 70  owing is a no-op
18618 29 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75  ).  */.  if( pCu
18619 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29  r->pKeyInfo==0 )
1861a 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65  {.    invalidate
1861b 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28  IncrblobCursors(
1861c 70 2c 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d  p, nKey, 0);.  }
1861d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20  ..  /* Save the 
1861e 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79  positions of any
1861f 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f   other cursors o
18620 70 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c  pen on this tabl
18621 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20  e..  **.  ** In 
18622 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20  some cases, the 
18623 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 4d 6f 76  call to btreeMov
18624 65 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 61  eto() below is a
18625 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a   no-op. For.  **
18626 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 69   example, when i
18627 6e 73 65 72 74 69 6e 67 20 64 61 74 61 20 69 6e  nserting data in
18628 74 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 20  to a table with 
18629 61 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 69  auto-generated i
1862a 6e 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 73  nteger.  ** keys
1862b 2c 20 74 68 65 20 56 44 42 45 20 6c 61 79 65 72  , the VDBE layer
1862c 20 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33   invokes sqlite3
1862d 42 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20 66  BtreeLast() to f
1862e 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a 20  igure out the . 
1862f 20 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20   ** integer key 
18630 74 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e 20  to use. It then 
18631 63 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74  calls this funct
18632 69 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 20  ion to actually 
18633 69 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a 2a  insert the .  **
18634 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 69   data into the i
18635 6e 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 6e  ntkey B-Tree. In
18636 20 74 68 69 73 20 63 61 73 65 20 62 74 72 65 65   this case btree
18637 4d 6f 76 65 74 6f 28 29 20 72 65 63 6f 67 6e 69  Moveto() recogni
18638 7a 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68  zes.  ** that th
18639 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65  e cursor is alre
1863a 61 64 79 20 77 68 65 72 65 20 69 74 20 6e 65 65  ady where it nee
1863b 64 73 20 74 6f 20 62 65 20 61 6e 64 20 72 65 74  ds to be and ret
1863c 75 72 6e 73 20 77 69 74 68 6f 75 74 0a 20 20 2a  urns without.  *
1863d 2a 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b  * doing any work
1863e 2e 20 54 6f 20 61 76 6f 69 64 20 74 68 77 61 72  . To avoid thwar
1863f 74 69 6e 67 20 74 68 65 73 65 20 6f 70 74 69 6d  ting these optim
18640 69 7a 61 74 69 6f 6e 73 2c 20 69 74 20 69 73 20  izations, it is 
18641 69 6d 70 6f 72 74 61 6e 74 0a 20 20 2a 2a 20 6e  important.  ** n
18642 6f 74 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20  ot to clear the 
18643 63 75 72 73 6f 72 20 68 65 72 65 2e 0a 20 20 2a  cursor here..  *
18644 2f 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c  /.  rc = saveAll
18645 43 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75  Cursors(pBt, pCu
18646 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75  r->pgnoRoot, pCu
18647 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  r);.  if( rc ) r
18648 65 74 75 72 6e 20 72 63 3b 0a 20 20 69 66 28 20  eturn rc;.  if( 
18649 21 6c 6f 63 20 29 7b 0a 20 20 20 20 72 63 20 3d  !loc ){.    rc =
1864a 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75   btreeMoveto(pCu
1864b 72 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61  r, pKey, nKey, a
1864c 70 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29  ppendBias, &loc)
1864d 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
1864e 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
1864f 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53  assert( pCur->eS
18650 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
18651 49 44 20 7c 7c 20 28 70 43 75 72 2d 3e 65 53 74  ID || (pCur->eSt
18652 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41  ate==CURSOR_INVA
18653 4c 49 44 20 26 26 20 6c 6f 63 29 20 29 3b 0a 0a  LID && loc) );..
18654 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e    pPage = pCur->
18655 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
18656 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ge];.  assert( p
18657 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20  Page->intKey || 
18658 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 73  nKey>=0 );.  ass
18659 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66  ert( pPage->leaf
1865a 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b   || !pPage->intK
1865b 65 79 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 28  ey );..  TRACE((
1865c 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d 25  "INSERT: table=%
1865d 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 74  d nkey=%lld ndat
1865e 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73 5c  a=%d page=%d %s\
1865f 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43  n",.          pC
18660 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e 4b  ur->pgnoRoot, nK
18661 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 65  ey, nData, pPage
18662 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20  ->pgno,.        
18663 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 72    loc==0 ? "over
18664 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65 6e  write" : "new en
18665 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72 74  try"));.  assert
18666 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20  ( pPage->isInit 
18667 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d  );.  allocateTem
18668 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 6e  pSpace(pBt);.  n
18669 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54  ewCell = pBt->pT
1866a 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20 6e  mpSpace;.  if( n
1866b 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75  ewCell==0 ) retu
1866c 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1866d 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65  .  rc = fillInCe
1866e 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c  ll(pPage, newCel
1866f 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70  l, pKey, nKey, p
18670 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65  Data, nData, nZe
18671 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 69  ro, &szNew);.  i
18672 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64  f( rc ) goto end
18673 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 72  _insert;.  asser
18674 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 69  t( szNew==cellSi
18675 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65 77  zePtr(pPage, new
18676 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72  Cell) );.  asser
18677 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 4c  t( szNew<=MX_CEL
18678 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20  L_SIZE(pBt) );. 
18679 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49   idx = pCur->aiI
1867a 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  dx[pCur->iPage];
1867b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 29 7b  .  if( loc==0 ){
1867c 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a  .    u16 szOld;.
1867d 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c      assert( idx<
1867e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  pPage->nCell );.
1867f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
18680 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
18681 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
18682 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
18683 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b  goto end_insert;
18684 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65  .    }.    oldCe
18685 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
18686 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69  age, idx);.    i
18687 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  f( !pPage->leaf 
18688 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
18689 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c  newCell, oldCell
1868a 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 4);.    }.    
1868b 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65  szOld = cellSize
1868c 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65  Ptr(pPage, oldCe
1868d 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c  ll);.    rc = cl
1868e 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f  earCell(pPage, o
1868f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 64 72 6f  ldCell);.    dro
18690 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78  pCell(pPage, idx
18691 2c 20 73 7a 4f 6c 64 2c 20 26 72 63 29 3b 0a 20  , szOld, &rc);. 
18692 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
18693 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 7d   end_insert;.  }
18694 65 6c 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26  else if( loc<0 &
18695 26 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30  & pPage->nCell>0
18696 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
18697 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20  pPage->leaf );. 
18698 20 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d     idx = ++pCur-
18699 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
1869a 67 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ge];.  }else{.  
1869b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1869c 3e 6c 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20 69  >leaf );.  }.  i
1869d 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c  nsertCell(pPage,
1869e 20 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73   idx, newCell, s
1869f 7a 4e 65 77 2c 20 30 2c 20 30 2c 20 26 72 63 29  zNew, 0, 0, &rc)
186a0 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d  ;.  assert( rc!=
186a1 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61  SQLITE_OK || pPa
186a2 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70  ge->nCell>0 || p
186a3 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e  Page->nOverflow>
186a4 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f  0 );..  /* If no
186a5 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
186a6 65 64 20 61 6e 64 20 70 50 61 67 65 20 68 61 73  ed and pPage has
186a7 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c   an overflow cel
186a8 6c 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 28  l, call balance(
186a9 29 20 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69 73  ) .  ** to redis
186aa 74 72 69 62 75 74 65 20 74 68 65 20 63 65 6c 6c  tribute the cell
186ab 73 20 77 69 74 68 69 6e 20 74 68 65 20 74 72 65  s within the tre
186ac 65 2e 20 53 69 6e 63 65 20 62 61 6c 61 6e 63 65  e. Since balance
186ad 28 29 20 6d 61 79 20 6d 6f 76 65 0a 20 20 2a 2a  () may move.  **
186ae 20 74 68 65 20 63 75 72 73 6f 72 2c 20 7a 65 72   the cursor, zer
186af 6f 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69  o the BtCursor.i
186b0 6e 66 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42 74  nfo.nSize and Bt
186b1 43 75 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65 79  Cursor.validNKey
186b2 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 2e  .  ** variables.
186b3 0a 20 20 2a 2a 0a 20 20 2a 2a 20 50 72 65 76 69  .  **.  ** Previ
186b4 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ous versions of 
186b5 53 51 4c 69 74 65 20 63 61 6c 6c 65 64 20 6d 6f  SQLite called mo
186b6 76 65 54 6f 52 6f 6f 74 28 29 20 74 6f 20 6d 6f  veToRoot() to mo
186b7 76 65 20 74 68 65 20 63 75 72 73 6f 72 0a 20 20  ve the cursor.  
186b8 2a 2a 20 62 61 63 6b 20 74 6f 20 74 68 65 20 72  ** back to the r
186b9 6f 6f 74 20 70 61 67 65 20 61 73 20 62 61 6c 61  oot page as bala
186ba 6e 63 65 28 29 20 75 73 65 64 20 74 6f 20 69 6e  nce() used to in
186bb 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 6f 6e  validate the con
186bc 74 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 42 74  tents.  ** of Bt
186bd 43 75 72 73 6f 72 2e 61 70 50 61 67 65 5b 5d 20  Cursor.apPage[] 
186be 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 61 69 49  and BtCursor.aiI
186bf 64 78 5b 5d 2e 20 49 6e 73 74 65 61 64 20 6f 66  dx[]. Instead of
186c0 20 64 6f 69 6e 67 20 74 68 61 74 2c 0a 20 20 2a   doing that,.  *
186c1 2a 20 73 65 74 20 74 68 65 20 63 75 72 73 6f 72  * set the cursor
186c2 20 73 74 61 74 65 20 74 6f 20 22 69 6e 76 61 6c   state to "inval
186c3 69 64 22 2e 20 54 68 69 73 20 6d 61 6b 65 73 20  id". This makes 
186c4 63 6f 6d 6d 6f 6e 20 69 6e 73 65 72 74 20 6f 70  common insert op
186c5 65 72 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 73 6c  erations.  ** sl
186c6 69 67 68 74 6c 79 20 66 61 73 74 65 72 2e 0a 20  ightly faster.. 
186c7 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 69   **.  ** There i
186c8 73 20 61 20 73 75 62 74 6c 65 20 62 75 74 20 69  s a subtle but i
186c9 6d 70 6f 72 74 61 6e 74 20 6f 70 74 69 6d 69 7a  mportant optimiz
186ca 61 74 69 6f 6e 20 68 65 72 65 20 74 6f 6f 2e 20  ation here too. 
186cb 57 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 0a 20  When inserting. 
186cc 20 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 65 63   ** multiple rec
186cd 6f 72 64 73 20 69 6e 74 6f 20 61 6e 20 69 6e 74  ords into an int
186ce 6b 65 79 20 62 2d 74 72 65 65 20 75 73 69 6e 67  key b-tree using
186cf 20 61 20 73 69 6e 67 6c 65 20 63 75 72 73 6f 72   a single cursor
186d0 20 28 61 73 20 63 61 6e 0a 20 20 2a 2a 20 68 61   (as can.  ** ha
186d1 70 70 65 6e 20 77 68 69 6c 65 20 70 72 6f 63 65  ppen while proce
186d2 73 73 69 6e 67 20 61 6e 20 22 49 4e 53 45 52 54  ssing an "INSERT
186d3 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54   INTO ... SELECT
186d4 22 20 73 74 61 74 65 6d 65 6e 74 29 2c 20 69 74  " statement), it
186d5 0a 20 20 2a 2a 20 69 73 20 61 64 76 61 6e 74 61  .  ** is advanta
186d6 67 65 6f 75 73 20 74 6f 20 6c 65 61 76 65 20 74  geous to leave t
186d7 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69  he cursor pointi
186d8 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65  ng to the last e
186d9 6e 74 72 79 20 69 6e 0a 20 20 2a 2a 20 74 68 65  ntry in.  ** the
186da 20 62 2d 74 72 65 65 20 69 66 20 70 6f 73 73 69   b-tree if possi
186db 62 6c 65 2e 20 49 66 20 74 68 65 20 63 75 72 73  ble. If the curs
186dc 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74  or is left point
186dd 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 0a  ing to the last.
186de 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68    ** entry in th
186df 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 65  e table, and the
186e0 20 6e 65 78 74 20 72 6f 77 20 69 6e 73 65 72 74   next row insert
186e1 65 64 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65  ed has an intege
186e2 72 20 6b 65 79 0a 20 20 2a 2a 20 6c 61 72 67 65  r key.  ** large
186e3 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65  r than the large
186e4 73 74 20 65 78 69 73 74 69 6e 67 20 6b 65 79 2c  st existing key,
186e5 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
186e6 74 6f 20 69 6e 73 65 72 74 20 74 68 65 0a 20 20  to insert the.  
186e7 2a 2a 20 72 6f 77 20 77 69 74 68 6f 75 74 20 73  ** row without s
186e8 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f  eeking the curso
186e9 72 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 61  r. This can be a
186ea 20 62 69 67 20 70 65 72 66 6f 72 6d 61 6e 63 65   big performance
186eb 20 62 6f 6f 73 74 2e 0a 20 20 2a 2f 0a 20 20 70   boost..  */.  p
186ec 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
186ed 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c  = 0;.  pCur->val
186ee 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66  idNKey = 0;.  if
186ef 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
186f0 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  && pPage->nOverf
186f1 6c 6f 77 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  low ){.    rc = 
186f2 62 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a 0a  balance(pCur);..
186f3 20 20 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b 65      /* Must make
186f4 20 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 20   sure nOverflow 
186f5 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f  is reset to zero
186f6 20 65 76 65 6e 20 69 66 20 74 68 65 20 62 61 6c   even if the bal
186f7 61 6e 63 65 28 29 0a 20 20 20 20 2a 2a 20 66 61  ance().    ** fa
186f8 69 6c 73 2e 20 49 6e 74 65 72 6e 61 6c 20 64 61  ils. Internal da
186f9 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 72  ta structure cor
186fa 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73  ruption will res
186fb 75 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 0a  ult otherwise. .
186fc 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 65 74      ** Also, set
186fd 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74   the cursor stat
186fe 65 20 74 6f 20 69 6e 76 61 6c 69 64 2e 20 54 68  e to invalid. Th
186ff 69 73 20 73 74 6f 70 73 20 73 61 76 65 43 75 72  is stops saveCur
18700 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 0a 20 20  sorPosition().  
18701 20 20 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e 67    ** from trying
18702 20 74 6f 20 73 61 76 65 20 74 68 65 20 63 75 72   to save the cur
18703 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f 66  rent position of
18704 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 2a 2f   the cursor.  */
18705 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67  .    pCur->apPag
18706 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
18707 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20  nOverflow = 0;. 
18708 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
18709 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
1870a 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1870b 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
1870c 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72  r->iPage]->nOver
1870d 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 65 6e 64 5f  flow==0 );..end_
1870e 69 6e 73 65 72 74 3a 0a 20 20 72 65 74 75 72 6e  insert:.  return
1870f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65   rc;.}../*.** De
18710 6c 65 74 65 20 74 68 65 20 65 6e 74 72 79 20 74  lete the entry t
18711 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69  hat the cursor i
18712 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20  s pointing to.  
18713 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73  The cursor.** is
18714 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
18715 74 20 61 20 61 72 62 69 74 72 61 72 79 20 6c 6f  t a arbitrary lo
18716 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  cation..*/.SQLIT
18717 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
18718 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65  lite3BtreeDelete
18719 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
1871a 7b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70  {.  Btree *p = p
1871b 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42  Cur->pBtree;.  B
1871c 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
1871d 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20  ->pBt;          
1871e 20 20 20 20 0a 20 20 69 6e 74 20 72 63 3b 20 20      .  int rc;  
1871f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18720 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
18721 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
18722 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20  MemPage *pPage; 
18723 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18724 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20       /* Page to 
18725 64 65 6c 65 74 65 20 63 65 6c 6c 20 66 72 6f 6d  delete cell from
18726 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
18727 68 61 72 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20  har *pCell;     
18728 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
18729 69 6e 74 65 72 20 74 6f 20 63 65 6c 6c 20 74 6f  inter to cell to
1872a 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74   delete */.  int
1872b 20 69 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 20   iCellIdx;      
1872c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1872d 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 65    /* Index of ce
1872e 6c 6c 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a  ll to delete */.
1872f 20 20 69 6e 74 20 69 43 65 6c 6c 44 65 70 74 68    int iCellDepth
18730 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
18731 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20         /* Depth 
18732 6f 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69  of node containi
18733 6e 67 20 70 43 65 6c 6c 20 2a 2f 20 0a 0a 20 20  ng pCell */ ..  
18734 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
18735 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
18736 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d  ;.  assert( pBt-
18737 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
18738 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
18739 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72   assert( !pBt->r
1873a 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73  eadOnly );.  ass
1873b 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61  ert( pCur->wrFla
1873c 67 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68  g );.  assert( h
1873d 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62  asSharedCacheTab
1873e 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d 3e  leLock(p, pCur->
1873f 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d 3e  pgnoRoot, pCur->
18740 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 20  pKeyInfo!=0, 2) 
18741 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 68 61  );.  assert( !ha
18742 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70  sReadConflicts(p
18743 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  , pCur->pgnoRoot
18744 29 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45  ) );..  if( NEVE
18745 52 28 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  R(pCur->aiIdx[pC
18746 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72  ur->iPage]>=pCur
18747 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
18748 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 29 20 0a 20  Page]->nCell) . 
18749 20 20 7c 7c 20 4e 45 56 45 52 28 70 43 75 72 2d    || NEVER(pCur-
1874a 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f  >eState!=CURSOR_
1874b 56 41 4c 49 44 29 0a 20 20 29 7b 0a 20 20 20 20  VALID).  ){.    
1874c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1874d 52 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68 69  ROR;  /* Somethi
1874e 6e 67 20 68 61 73 20 67 6f 6e 65 20 61 77 72 79  ng has gone awry
1874f 2e 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  . */.  }..  /* I
18750 66 20 74 68 69 73 20 69 73 20 61 20 64 65 6c 65  f this is a dele
18751 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  te operation to 
18752 72 65 6d 6f 76 65 20 61 20 72 6f 77 20 66 72 6f  remove a row fro
18753 6d 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65  m a table b-tree
18754 2c 0a 20 20 2a 2a 20 69 6e 76 61 6c 69 64 61 74  ,.  ** invalidat
18755 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63  e any incrblob c
18756 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74  ursors open on t
18757 68 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c  he row being del
18758 65 74 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20  eted.  */.  if( 
18759 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d  pCur->pKeyInfo==
1875a 30 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64  0 ){.    invalid
1875b 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f  ateIncrblobCurso
1875c 72 73 28 70 2c 20 70 43 75 72 2d 3e 69 6e 66 6f  rs(p, pCur->info
1875d 2e 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a  .nKey, 0);.  }..
1875e 20 20 69 43 65 6c 6c 44 65 70 74 68 20 3d 20 70    iCellDepth = p
1875f 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 69 43  Cur->iPage;.  iC
18760 65 6c 6c 49 64 78 20 3d 20 70 43 75 72 2d 3e 61  ellIdx = pCur->a
18761 69 49 64 78 5b 69 43 65 6c 6c 44 65 70 74 68 5d  iIdx[iCellDepth]
18762 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72  ;.  pPage = pCur
18763 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65  ->apPage[iCellDe
18764 70 74 68 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d 20  pth];.  pCell = 
18765 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
18766 69 43 65 6c 6c 49 64 78 29 3b 0a 0a 20 20 2f 2a  iCellIdx);..  /*
18767 20 49 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e   If the page con
18768 74 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72  taining the entr
18769 79 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e  y to delete is n
1876a 6f 74 20 61 20 6c 65 61 66 20 70 61 67 65 2c 20  ot a leaf page, 
1876b 6d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75  move.  ** the cu
1876c 72 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 72 67  rsor to the larg
1876d 65 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  est entry in the
1876e 20 74 72 65 65 20 74 68 61 74 20 69 73 20 73 6d   tree that is sm
1876f 61 6c 6c 65 72 20 74 68 61 6e 0a 20 20 2a 2a 20  aller than.  ** 
18770 74 68 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20  the entry being 
18771 64 65 6c 65 74 65 64 2e 20 54 68 69 73 20 63 65  deleted. This ce
18772 6c 6c 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 20  ll will replace 
18773 74 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64  the cell being d
18774 65 6c 65 74 65 64 0a 20 20 2a 2a 20 66 72 6f 6d  eleted.  ** from
18775 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   the internal no
18776 64 65 2e 20 54 68 65 20 27 70 72 65 76 69 6f 75  de. The 'previou
18777 73 27 20 65 6e 74 72 79 20 69 73 20 75 73 65 64  s' entry is used
18778 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 65 61   for this instea
18779 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 27 6e  d.  ** of the 'n
1877a 65 78 74 27 20 65 6e 74 72 79 2c 20 61 73 20 74  ext' entry, as t
1877b 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72  he previous entr
1877c 79 20 69 73 20 61 6c 77 61 79 73 20 61 20 70 61  y is always a pa
1877d 72 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73  rt of the.  ** s
1877e 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 62  ub-tree headed b
1877f 79 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65  y the child page
18780 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 62 65 69   of the cell bei
18781 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73  ng deleted. This
18782 20 6d 61 6b 65 73 0a 20 20 2a 2a 20 62 61 6c 61   makes.  ** bala
18783 6e 63 69 6e 67 20 74 68 65 20 74 72 65 65 20 66  ncing the tree f
18784 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 64 65 6c  ollowing the del
18785 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 65 61  ete operation ea
18786 73 69 65 72 2e 20 20 2a 2f 0a 20 20 69 66 28 20  sier.  */.  if( 
18787 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
18788 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b      int notUsed;
18789 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1878a 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70  3BtreePrevious(p
1878b 43 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a  Cur, &notUsed);.
1878c 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1878d 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
1878e 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74  * Save the posit
1878f 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65  ions of any othe
18790 72 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f  r cursors open o
18791 6e 20 74 68 69 73 20 74 61 62 6c 65 20 62 65 66  n this table bef
18792 6f 72 65 0a 20 20 2a 2a 20 6d 61 6b 69 6e 67 20  ore.  ** making 
18793 61 6e 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e  any modification
18794 73 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 67 65  s. Make the page
18795 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
18796 65 6e 74 72 79 20 74 6f 20 62 65 20 0a 20 20 2a  entry to be .  *
18797 2a 20 64 65 6c 65 74 65 64 20 77 72 69 74 61 62  * deleted writab
18798 6c 65 2e 20 54 68 65 6e 20 66 72 65 65 20 61 6e  le. Then free an
18799 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  y overflow pages
1879a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1879b 20 74 68 65 20 0a 20 20 2a 2a 20 65 6e 74 72 79   the .  ** entry
1879c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 72 65 6d   and finally rem
1879d 6f 76 65 20 74 68 65 20 63 65 6c 6c 20 69 74 73  ove the cell its
1879e 65 6c 66 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  elf from within 
1879f 74 68 65 20 70 61 67 65 2e 20 20 0a 20 20 2a 2f  the page.  .  */
187a0 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43  .  rc = saveAllC
187a1 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72  ursors(pBt, pCur
187a2 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72  ->pgnoRoot, pCur
187a3 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
187a4 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20  turn rc;.  rc = 
187a5 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
187a6 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
187a7 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
187a8 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20  turn rc;.  rc = 
187a9 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c  clearCell(pPage,
187aa 20 70 43 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 43   pCell);.  dropC
187ab 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c  ell(pPage, iCell
187ac 49 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72  Idx, cellSizePtr
187ad 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 2c 20  (pPage, pCell), 
187ae 26 72 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29  &rc);.  if( rc )
187af 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
187b0 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 64 65  * If the cell de
187b1 6c 65 74 65 64 20 77 61 73 20 6e 6f 74 20 6c 6f  leted was not lo
187b2 63 61 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20  cated on a leaf 
187b3 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63  page, then the c
187b4 75 72 73 6f 72 0a 20 20 2a 2a 20 69 73 20 63 75  ursor.  ** is cu
187b5 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
187b6 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20   to the largest 
187b7 65 6e 74 72 79 20 69 6e 20 74 68 65 20 73 75 62  entry in the sub
187b8 2d 74 72 65 65 20 68 65 61 64 65 64 0a 20 20 2a  -tree headed.  *
187b9 2a 20 62 79 20 74 68 65 20 63 68 69 6c 64 2d 70  * by the child-p
187ba 61 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  age of the cell 
187bb 74 68 61 74 20 77 61 73 20 6a 75 73 74 20 64 65  that was just de
187bc 6c 65 74 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e  leted from an in
187bd 74 65 72 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65  ternal.  ** node
187be 2e 20 54 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20  . The cell from 
187bf 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 6e 65  the leaf node ne
187c0 65 64 73 20 74 6f 20 62 65 20 6d 6f 76 65 64 20  eds to be moved 
187c1 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a  to the internal.
187c2 20 20 2a 2a 20 6e 6f 64 65 20 74 6f 20 72 65 70    ** node to rep
187c3 6c 61 63 65 20 74 68 65 20 64 65 6c 65 74 65 64  lace the deleted
187c4 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 28   cell.  */.  if(
187c5 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
187c6 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c  .    MemPage *pL
187c7 65 61 66 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  eaf = pCur->apPa
187c8 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  ge[pCur->iPage];
187c9 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a  .    int nCell;.
187ca 20 20 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75      Pgno n = pCu
187cb 72 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44  r->apPage[iCellD
187cc 65 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a 20  epth+1]->pgno;. 
187cd 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
187ce 20 2a 70 54 6d 70 3b 0a 0a 20 20 20 20 70 43 65   *pTmp;..    pCe
187cf 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 4c  ll = findCell(pL
187d0 65 61 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c  eaf, pLeaf->nCel
187d1 6c 2d 31 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20  l-1);.    nCell 
187d2 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c  = cellSizePtr(pL
187d3 65 61 66 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20  eaf, pCell);.   
187d4 20 61 73 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c   assert( MX_CELL
187d5 5f 53 49 5a 45 28 70 42 74 29 3e 3d 6e 43 65 6c  _SIZE(pBt)>=nCel
187d6 6c 20 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 61  l );..    alloca
187d7 74 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29  teTempSpace(pBt)
187d8 3b 0a 20 20 20 20 70 54 6d 70 20 3d 20 70 42 74  ;.    pTmp = pBt
187d9 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20  ->pTmpSpace;..  
187da 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
187db 67 65 72 57 72 69 74 65 28 70 4c 65 61 66 2d 3e  gerWrite(pLeaf->
187dc 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 6e  pDbPage);.    in
187dd 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20  sertCell(pPage, 
187de 69 43 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c 2d  iCellIdx, pCell-
187df 34 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d 70  4, nCell+4, pTmp
187e0 2c 20 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 64  , n, &rc);.    d
187e1 72 6f 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70  ropCell(pLeaf, p
187e2 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e  Leaf->nCell-1, n
187e3 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20  Cell, &rc);.    
187e4 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
187e5 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61  rc;.  }..  /* Ba
187e6 6c 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e 20  lance the tree. 
187e7 49 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c  If the entry del
187e8 65 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65 64  eted was located
187e9 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c   on a leaf page,
187ea 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63  .  ** then the c
187eb 75 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 6e  ursor still poin
187ec 74 73 20 74 6f 20 74 68 61 74 20 70 61 67 65 2e  ts to that page.
187ed 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
187ee 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 6c  e first.  ** cal
187ef 6c 20 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 72  l to balance() r
187f0 65 70 61 69 72 73 20 74 68 65 20 74 72 65 65 2c  epairs the tree,
187f1 20 61 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e 29   and the if(...)
187f2 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20   condition is.  
187f3 2a 2a 20 6e 65 76 65 72 20 74 72 75 65 2e 0a 20  ** never true.. 
187f4 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69   **.  ** Otherwi
187f5 73 65 2c 20 69 66 20 74 68 65 20 65 6e 74 72 79  se, if the entry
187f6 20 64 65 6c 65 74 65 64 20 77 61 73 20 6f 6e 20   deleted was on 
187f7 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  an internal node
187f8 20 70 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a 2a   page, then.  **
187f9 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e   pCur is pointin
187fa 67 20 74 6f 20 74 68 65 20 6c 65 61 66 20 70 61  g to the leaf pa
187fb 67 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20  ge from which a 
187fc 63 65 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 64  cell was removed
187fd 20 74 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 65   to.  ** replace
187fe 20 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65   the cell delete
187ff 64 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72  d from the inter
18800 6e 61 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 69  nal node. This i
18801 73 20 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a 20  s slightly.  ** 
18802 74 72 69 63 6b 79 20 61 73 20 74 68 65 20 6c 65  tricky as the le
18803 61 66 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 75  af node may be u
18804 6e 64 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 68  nderfull, and th
18805 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20  e internal node 
18806 6d 61 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 68  may.  ** be eith
18807 65 72 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 72  er under or over
18808 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61  full. In this ca
18809 73 65 20 72 75 6e 20 74 68 65 20 62 61 6c 61 6e  se run the balan
1880a 63 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a 20  cing algorithm. 
1880b 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 20   ** on the leaf 
1880c 6e 6f 64 65 20 66 69 72 73 74 2e 20 49 66 20 74  node first. If t
1880d 68 65 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 65  he balance proce
1880e 65 64 73 20 66 61 72 20 65 6e 6f 75 67 68 20 75  eds far enough u
1880f 70 20 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 20  p the.  ** tree 
18810 74 68 61 74 20 77 65 20 63 61 6e 20 62 65 20 73  that we can be s
18811 75 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 6f  ure that any pro
18812 62 6c 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 65  blem in the inte
18813 72 6e 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 20  rnal node has.  
18814 2a 2a 20 62 65 65 6e 20 63 6f 72 72 65 63 74 65  ** been correcte
18815 64 2c 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 68  d, so be it. Oth
18816 65 72 77 69 73 65 2c 20 61 66 74 65 72 20 62 61  erwise, after ba
18817 6c 61 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 66  lancing the leaf
18818 20 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c 6b   node,.  ** walk
18819 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74   the cursor up t
1881a 68 65 20 74 72 65 65 20 74 6f 20 74 68 65 20 69  he tree to the i
1881b 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64  nternal node and
1881c 20 62 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0a   balance it as .
1881d 20 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a 20    ** well.  */. 
1881e 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43   rc = balance(pC
1881f 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ur);.  if( rc==S
18820 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 72  QLITE_OK && pCur
18821 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70  ->iPage>iCellDep
18822 74 68 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28  th ){.    while(
18823 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65   pCur->iPage>iCe
18824 6c 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20  llDepth ){.     
18825 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75   releasePage(pCu
18826 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
18827 69 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 7d  iPage--]);.    }
18828 0a 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63  .    rc = balanc
18829 65 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20  e(pCur);.  }..  
1882a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1882b 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52  K ){.    moveToR
1882c 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20  oot(pCur);.  }. 
1882d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1882e 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
1882f 77 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20  w BTree table.  
18830 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61  Write into *piTa
18831 62 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20  ble the page.** 
18832 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72  number for the r
18833 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  oot page of the 
18834 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  new table..**.**
18835 20 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70   The type of typ
18836 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  e is determined 
18837 62 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72  by the flags par
18838 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68  ameter.  Only th
18839 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76  e.** following v
1883a 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61  alues of flags a
1883b 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  re currently in 
1883c 75 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75  use.  Other valu
1883d 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20  es for.** flags 
1883e 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a  might not work:.
1883f 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f  **.**     BTREE_
18840 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41  INTKEY|BTREE_LEA
18841 46 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66  FDATA     Used f
18842 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69  or SQL tables wi
18843 74 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a  th rowid keys.**
18844 20 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44       BTREE_ZEROD
18845 41 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20  ATA             
18846 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51       Used for SQ
18847 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61  L indices.*/.sta
18848 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 65  tic int btreeCre
18849 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a  ateTable(Btree *
1884a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c  p, int *piTable,
1884b 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42   int flags){.  B
1884c 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
1884d 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65  ->pBt;.  MemPage
1884e 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20   *pRoot;.  Pgno 
1884f 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20  pgnoRoot;.  int 
18850 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  rc;..  assert( s
18851 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
18852 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73  Mutex(p) );.  as
18853 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61  sert( pBt->inTra
18854 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
18855 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72  WRITE );.  asser
18856 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c  t( !pBt->readOnl
18857 79 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  y );..#ifdef SQL
18858 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
18859 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63  UUM.  rc = alloc
1885a 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
1885b 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52  , &pRoot, &pgnoR
1885c 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66  oot, 1, 0);.  if
1885d 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75  ( rc ){.    retu
1885e 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65  rn rc;.  }.#else
1885f 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f  .  if( pBt->auto
18860 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67  Vacuum ){.    Pg
18861 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20  no pgnoMove;    
18862 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65    /* Move a page
18863 20 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f   here to make ro
18864 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d  om for the root-
18865 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50  page */.    MemP
18866 61 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20  age *pPageMove; 
18867 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d  /* The page to m
18868 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20  ove to. */..    
18869 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65  /* Creating a ne
1886a 77 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62  w table may prob
1886b 61 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76  ably require mov
1886c 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20  ing an existing 
1886d 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20  database.    ** 
1886e 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72  to make room for
1886f 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20   the new tables 
18870 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61  root page. In ca
18871 73 65 20 74 68 69 73 20 70 61 67 65 20 74 75 72  se this page tur
18872 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f  ns.    ** out to
18873 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20   be an overflow 
18874 70 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c  page, delete all
18875 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d   overflow page-m
18876 61 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a  ap caches.    **
18877 20 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75   held by open cu
18878 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  rsors..    */.  
18879 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f    invalidateAllO
1887a 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74  verflowCache(pBt
1887b 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20  );..    /* Read 
1887c 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74  the value of met
1887d 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61  a[3] from the da
1887e 74 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d  tabase to determ
1887f 69 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20 20  ine where the.  
18880 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f    ** root page o
18881 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  f the new table 
18882 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b  should go. meta[
18883 33 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65 73  3] is the larges
18884 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20  t root-page.    
18885 2a 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66 61  ** created so fa
18886 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f  r, so the new ro
18887 6f 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61  ot-page is (meta
18888 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20  [3]+1)..    */. 
18889 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47     sqlite3BtreeG
1888a 65 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f  etMeta(p, BTREE_
1888b 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47  LARGEST_ROOT_PAG
1888c 45 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20  E, &pgnoRoot);. 
1888d 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a     pgnoRoot++;..
1888e 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72      /* The new r
1888f 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74  oot-page may not
18890 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e   be allocated on
18891 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70   a pointer-map p
18892 61 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20  age, or the.    
18893 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  ** PENDING_BYTE 
18894 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  page..    */.   
18895 20 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74   while( pgnoRoot
18896 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  ==PTRMAP_PAGENO(
18897 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c  pBt, pgnoRoot) |
18898 7c 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f  |.        pgnoRo
18899 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  ot==PENDING_BYTE
1889a 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20  _PAGE(pBt) ){.  
1889b 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a      pgnoRoot++;.
1889c 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
1889d 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b  ( pgnoRoot>=3 );
1889e 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  ..    /* Allocat
1889f 65 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 61  e a page. The pa
188a0 67 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c  ge that currentl
188a1 79 20 72 65 73 69 64 65 73 20 61 74 20 70 67 6e  y resides at pgn
188a2 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a  oRoot will.    *
188a3 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68  * be moved to th
188a4 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65  e allocated page
188a5 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c   (unless the all
188a6 6f 63 61 74 65 64 20 70 61 67 65 20 68 61 70 70  ocated page happ
188a7 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65  ens.    ** to re
188a8 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74  side at pgnoRoot
188a9 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  )..    */.    rc
188aa 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
188ab 50 61 67 65 28 70 42 74 2c 20 26 70 50 61 67 65  Page(pBt, &pPage
188ac 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c  Move, &pgnoMove,
188ad 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20   pgnoRoot, 1);. 
188ae 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
188af 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
188b0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a  turn rc;.    }..
188b1 20 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65      if( pgnoMove
188b2 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20  !=pgnoRoot ){.  
188b3 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20      /* pgnoRoot 
188b4 69 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74  is the page that
188b5 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f   will be used fo
188b6 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20  r the root-page 
188b7 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  of.      ** the 
188b8 6e 65 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d  new table (assum
188b9 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64  ing an error did
188ba 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74   not occur). But
188bb 20 77 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a   we were.      *
188bc 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f  * allocated pgno
188bd 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72 65  Move. If require
188be 64 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61  d (i.e. if it wa
188bf 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a  s not allocated.
188c0 20 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65        ** by exte
188c1 6e 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c  nding the file),
188c2 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67   the current pag
188c3 65 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67  e at position pg
188c4 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20  noMove.      ** 
188c5 69 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e  is already journ
188c6 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  aled..      */. 
188c7 20 20 20 20 20 75 38 20 65 54 79 70 65 20 3d 20       u8 eType = 
188c8 30 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 50  0;.      Pgno iP
188c9 74 72 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20  trPage = 0;..   
188ca 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
188cb 50 61 67 65 4d 6f 76 65 29 3b 0a 0a 20 20 20 20  PageMove);..    
188cc 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61    /* Move the pa
188cd 67 65 20 63 75 72 72 65 6e 74 6c 79 20 61 74 20  ge currently at 
188ce 70 67 6e 6f 52 6f 6f 74 20 74 6f 20 70 67 6e 6f  pgnoRoot to pgno
188cf 4d 6f 76 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72  Move. */.      r
188d0 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
188d1 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20  (pBt, pgnoRoot, 
188d2 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20  &pRoot, 0);.    
188d3 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
188d4 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
188d5 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
188d6 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72  }.      rc = ptr
188d7 6d 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f  mapGet(pBt, pgno
188d8 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69  Root, &eType, &i
188d9 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20  PtrPage);.      
188da 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41  if( eType==PTRMA
188db 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54  P_ROOTPAGE || eT
188dc 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45  ype==PTRMAP_FREE
188dd 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20  PAGE ){.        
188de 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  rc = SQLITE_CORR
188df 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
188e0 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  }.      if( rc!=
188e1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
188e2 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
188e3 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20  (pRoot);.       
188e4 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
188e5 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
188e6 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f  ( eType!=PTRMAP_
188e7 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 20 20  ROOTPAGE );.    
188e8 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21    assert( eType!
188e9 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45  =PTRMAP_FREEPAGE
188ea 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72   );.      rc = r
188eb 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c  elocatePage(pBt,
188ec 20 70 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20 69   pRoot, eType, i
188ed 50 74 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f 76  PtrPage, pgnoMov
188ee 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 6c  e, 0);.      rel
188ef 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
188f0 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 69  ..      /* Obtai
188f1 6e 20 74 68 65 20 70 61 67 65 20 61 74 20 70 67  n the page at pg
188f2 6e 6f 52 6f 6f 74 20 2a 2f 0a 20 20 20 20 20 20  noRoot */.      
188f3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
188f4 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  K ){.        ret
188f5 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
188f6 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65        rc = btree
188f7 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e  GetPage(pBt, pgn
188f8 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30  oRoot, &pRoot, 0
188f9 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
188fa 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
188fb 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
188fc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
188fd 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
188fe 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62  Write(pRoot->pDb
188ff 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Page);.      if(
18900 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
18901 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  {.        releas
18902 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20  ePage(pRoot);.  
18903 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
18904 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
18905 73 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20  se{.      pRoot 
18906 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20  = pPageMove;.   
18907 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61   } ..    /* Upda
18908 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  te the pointer-m
18909 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61  ap and meta-data
1890a 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f   with the new ro
1890b 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20  ot-page number. 
1890c 2a 2f 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74  */.    ptrmapPut
1890d 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20  (pBt, pgnoRoot, 
1890e 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c  PTRMAP_ROOTPAGE,
1890f 20 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66   0, &rc);.    if
18910 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
18911 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29  leasePage(pRoot)
18912 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
18913 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  c;.    }.    rc 
18914 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70  = sqlite3BtreeUp
18915 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70  dateMeta(p, 4, p
18916 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66  gnoRoot);.    if
18917 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
18918 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29  leasePage(pRoot)
18919 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
1891a 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73  c;.    }..  }els
1891b 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f  e{.    rc = allo
1891c 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
1891d 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f  t, &pRoot, &pgno
1891e 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20  Root, 1, 0);.   
1891f 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
18920 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a   rc;.  }.#endif.
18921 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
18922 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
18923 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65  e(pRoot->pDbPage
18924 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28  ) );.  zeroPage(
18925 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50  pRoot, flags | P
18926 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69  TF_LEAF);.  sqli
18927 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52  te3PagerUnref(pR
18928 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  oot->pDbPage);. 
18929 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74   *piTable = (int
1892a 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74  )pgnoRoot;.  ret
1892b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1892c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1892d 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
1892e 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65  CreateTable(Btre
1892f 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62  e *p, int *piTab
18930 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  le, int flags){.
18931 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
18932 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
18933 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 72  ;.  rc = btreeCr
18934 65 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54  eateTable(p, piT
18935 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20  able, flags);.  
18936 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
18937 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
18938 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73  c;.}../*.** Eras
18939 65 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61  e the given data
1893a 62 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c  base page and al
1893b 6c 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20  l its children. 
1893c 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70   Return.** the p
1893d 61 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c  age to the freel
1893e 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ist..*/.static i
1893f 6e 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 65  nt clearDatabase
18940 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64  Page(.  BtShared
18941 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20   *pBt,          
18942 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68   /* The BTree th
18943 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
18944 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  table */.  Pgno 
18945 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  pgno,           
18946 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
18947 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 69 6e  to clear */.  in
18948 74 20 66 72 65 65 50 61 67 65 46 6c 61 67 2c 20  t freePageFlag, 
18949 20 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74      /* Deallocat
1894a 65 20 70 61 67 65 20 69 66 20 74 72 75 65 20 2a  e page if true *
1894b 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67  /.  int *pnChang
1894c 65 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  e.){.  MemPage *
1894d 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b  pPage;.  int rc;
1894e 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
1894f 20 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 69   *pCell;.  int i
18950 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
18951 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
18952 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
18953 20 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 72 50   if( pgno>pagerP
18954 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b  agecount(pBt) ){
18955 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
18956 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
18957 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74  .  }..  rc = get
18958 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c  AndInitPage(pBt,
18959 20 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b 0a   pgno, &pPage);.
1895a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
1895b 6e 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  n rc;.  for(i=0;
1895c 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   i<pPage->nCell;
1895d 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c 6c   i++){.    pCell
1895e 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67   = findCell(pPag
1895f 65 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 20 21  e, i);.    if( !
18960 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
18961 20 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44       rc = clearD
18962 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c  atabasePage(pBt,
18963 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29   get4byte(pCell)
18964 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a  , 1, pnChange);.
18965 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67        if( rc ) g
18966 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73  oto cleardatabas
18967 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d  epage_out;.    }
18968 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 43  .    rc = clearC
18969 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  ell(pPage, pCell
1896a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
1896b 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61  goto cleardataba
1896c 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a  sepage_out;.  }.
1896d 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
1896e 61 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63  af ){.    rc = c
1896f 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65  learDatabasePage
18970 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 26  (pBt, get4byte(&
18971 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d 29  pPage->aData[8])
18972 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a  , 1, pnChange);.
18973 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
18974 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 70  o cleardatabasep
18975 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65  age_out;.  }else
18976 20 69 66 28 20 70 6e 43 68 61 6e 67 65 20 29 7b   if( pnChange ){
18977 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
18978 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20  ge->intKey );.  
18979 20 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20 70    *pnChange += p
1897a 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 7d  Page->nCell;.  }
1897b 0a 20 20 69 66 28 20 66 72 65 65 50 61 67 65 46  .  if( freePageF
1897c 6c 61 67 20 29 7b 0a 20 20 20 20 66 72 65 65 50  lag ){.    freeP
1897d 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b  age(pPage, &rc);
1897e 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63  .  }else if( (rc
1897f 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
18980 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50  rite(pPage->pDbP
18981 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  age))==0 ){.    
18982 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20  zeroPage(pPage, 
18983 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20  pPage->aData[0] 
18984 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 7d  | PTF_LEAF);.  }
18985 0a 0a 63 6c 65 61 72 64 61 74 61 62 61 73 65 70  ..cleardatabasep
18986 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c 65 61  age_out:.  relea
18987 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
18988 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
18989 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20  *.** Delete all 
1898a 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
1898b 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20   a single table 
1898c 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
1898d 20 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74    iTable is.** t
1898e 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  he page number o
1898f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68  f the root of th
18990 65 20 74 61 62 6c 65 2e 20 20 41 66 74 65 72 20  e table.  After 
18991 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
18992 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 6f 6f  urns,.** the roo
18993 74 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 2c  t page is empty,
18994 20 62 75 74 20 73 74 69 6c 6c 20 65 78 69 73 74   but still exist
18995 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
18996 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20  utine will fail 
18997 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  with SQLITE_LOCK
18998 45 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ED if there are 
18999 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64  any open.** read
1899a 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20   cursors on the 
1899b 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72 69  table.  Open wri
1899c 74 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 6d  te cursors are m
1899d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 72  oved to the.** r
1899e 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65  oot of the table
1899f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 68 61  ..**.** If pnCha
189a0 6e 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c  nge is not NULL,
189a1 20 74 68 65 6e 20 74 61 62 6c 65 20 69 54 61 62   then table iTab
189a2 6c 65 20 6d 75 73 74 20 62 65 20 61 6e 20 69 6e  le must be an in
189a3 74 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 65 0a  tkey table. The.
189a4 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  ** integer value
189a5 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70   pointed to by p
189a6 6e 43 68 61 6e 67 65 20 69 73 20 69 6e 63 72 65  nChange is incre
189a7 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75  mented by the nu
189a8 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 69  mber of.** entri
189a9 65 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  es in the table.
189aa 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
189ab 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
189ac 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74  reeClearTable(Bt
189ad 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62  ree *p, int iTab
189ae 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67  le, int *pnChang
189af 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e){.  int rc;.  
189b0 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
189b1 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65  p->pBt;.  sqlite
189b2 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
189b3 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54    assert( p->inT
189b4 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
189b5 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c  E );..  /* Inval
189b6 69 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c  idate all incrbl
189b7 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20  ob cursors open 
189b8 6f 6e 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20  on table iTable 
189b9 28 61 73 73 75 6d 69 6e 67 20 69 54 61 62 6c 65  (assuming iTable
189ba 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 72 6f 6f  .  ** is the roo
189bb 74 20 6f 66 20 61 20 74 61 62 6c 65 20 62 2d 74  t of a table b-t
189bc 72 65 65 20 2d 20 69 66 20 69 74 20 69 73 20 6e  ree - if it is n
189bd 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ot, the followin
189be 67 20 63 61 6c 6c 20 69 73 0a 20 20 2a 2a 20 61  g call is.  ** a
189bf 20 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69   no-op).  */.  i
189c0 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f  nvalidateIncrblo
189c1 62 43 75 72 73 6f 72 73 28 70 2c 20 30 2c 20 31  bCursors(p, 0, 1
189c2 29 3b 0a 0a 20 20 72 63 20 3d 20 73 61 76 65 41  );..  rc = saveA
189c3 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 28  llCursors(pBt, (
189c4 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 29 3b  Pgno)iTable, 0);
189c5 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
189c6 3d 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d  ==rc ){.    rc =
189c7 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61   clearDatabasePa
189c8 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54  ge(pBt, (Pgno)iT
189c9 61 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67  able, 0, pnChang
189ca 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  e);.  }.  sqlite
189cb 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
189cc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
189cd 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20  /*.** Erase all 
189ce 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61  information in a
189cf 20 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20 74   table and add t
189d0 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74  he root of the t
189d1 61 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66  able to.** the f
189d2 72 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70 74  reelist.  Except
189d3 2c 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68  , the root of th
189d4 65 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62 6c  e principle tabl
189d5 65 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a  e (the one on.**
189d6 20 70 61 67 65 20 31 29 20 69 73 20 6e 65 76 65   page 1) is neve
189d7 72 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66  r added to the f
189d8 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54  reelist..**.** T
189d9 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
189da 20 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54   fail with SQLIT
189db 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72  E_LOCKED if ther
189dc 65 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a  e are any open.*
189dd 2a 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65  * cursors on the
189de 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   table..**.** If
189df 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65   AUTOVACUUM is e
189e0 6e 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 70  nabled and the p
189e1 61 67 65 20 61 74 20 69 54 61 62 6c 65 20 69 73  age at iTable is
189e2 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a   not the last.**
189e3 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68   root page in th
189e4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
189e5 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 72   then the last r
189e6 6f 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20  oot page .** in 
189e7 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
189e8 65 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  e is moved into 
189e9 74 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c  the slot formerl
189ea 79 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a  y occupied by.**
189eb 20 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61 74   iTable and that
189ec 20 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65   last slot forme
189ed 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 20  rly occupied by 
189ee 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61  the last root pa
189ef 67 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74  ge.** is added t
189f0 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69  o the freelist i
189f1 6e 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c 65  nstead of iTable
189f2 2e 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c 20  .  In this say, 
189f3 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65  all.** root page
189f4 73 20 61 72 65 20 6b 65 70 74 20 61 74 20 74 68  s are kept at th
189f5 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
189f6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
189f7 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65  , which.** is ne
189f8 63 65 73 73 61 72 79 20 66 6f 72 20 41 55 54 4f  cessary for AUTO
189f9 56 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72  VACUUM to work r
189fa 69 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20  ight.  *piMoved 
189fb 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a 2a  is set to the .*
189fc 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 68  * page number th
189fd 61 74 20 75 73 65 64 20 74 6f 20 62 65 20 74 68  at used to be th
189fe 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65  e last root page
189ff 20 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65 66   in the file bef
18a00 6f 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e  ore.** the move.
18a01 20 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65 74    If no page get
18a02 73 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65  s moved, *piMove
18a03 64 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a  d is set to 0..*
18a04 2a 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74 20  * The last root 
18a05 70 61 67 65 20 69 73 20 72 65 63 6f 72 64 65 64  page is recorded
18a06 20 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20   in meta[3] and 
18a07 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20  the value of.** 
18a08 6d 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61 74  meta[3] is updat
18a09 65 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65  ed by this proce
18a0a 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dure..*/.static 
18a0b 69 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62  int btreeDropTab
18a0c 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e  le(Btree *p, Pgn
18a0d 6f 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70  o iTable, int *p
18a0e 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72  iMoved){.  int r
18a0f 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  c;.  MemPage *pP
18a10 61 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61  age = 0;.  BtSha
18a11 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
18a12 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  t;..  assert( sq
18a13 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
18a14 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73  utex(p) );.  ass
18a15 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d  ert( p->inTrans=
18a16 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a  =TRANS_WRITE );.
18a17 0a 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65  .  /* It is ille
18a18 67 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61  gal to drop a ta
18a19 62 6c 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f  ble if any curso
18a1a 72 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74  rs are open on t
18a1b 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  he.  ** database
18a1c 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73  . This is becaus
18a1d 65 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  e in auto-vacuum
18a1e 20 6d 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e   mode the backen
18a1f 64 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20  d may.  ** need 
18a20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20  to move another 
18a21 72 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c  root-page to fil
18a22 6c 20 61 20 67 61 70 20 6c 65 66 74 20 62 79 20  l a gap left by 
18a23 74 68 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a  the deleted.  **
18a24 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61   root page. If a
18a25 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61  n open cursor wa
18a26 73 20 75 73 69 6e 67 20 74 68 69 73 20 70 61 67  s using this pag
18a27 65 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c  e a problem woul
18a28 64 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20  d .  ** occur.. 
18a29 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 65 72   **.  ** This er
18a2a 72 6f 72 20 69 73 20 63 61 75 67 68 74 20 6c 6f  ror is caught lo
18a2b 6e 67 20 62 65 66 6f 72 65 20 63 6f 6e 74 72 6f  ng before contro
18a2c 6c 20 72 65 61 63 68 65 73 20 74 68 69 73 20 70  l reaches this p
18a2d 6f 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  oint..  */.  if(
18a2e 20 4e 45 56 45 52 28 70 42 74 2d 3e 70 43 75 72   NEVER(pBt->pCur
18a2f 73 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  sor) ){.    sqli
18a30 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
18a31 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d  cked(p->db, pBt-
18a32 3e 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65  >pCursor->pBtree
18a33 2d 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72  ->db);.    retur
18a34 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f  n SQLITE_LOCKED_
18a35 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d  SHAREDCACHE;.  }
18a36 0a 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65  ..  rc = btreeGe
18a37 74 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f  tPage(pBt, (Pgno
18a38 29 69 54 61 62 6c 65 2c 20 26 70 50 61 67 65 2c  )iTable, &pPage,
18a39 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20   0);.  if( rc ) 
18a3a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20  return rc;.  rc 
18a3b 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  = sqlite3BtreeCl
18a3c 65 61 72 54 61 62 6c 65 28 70 2c 20 69 54 61 62  earTable(p, iTab
18a3d 6c 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  le, 0);.  if( rc
18a3e 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50   ){.    releaseP
18a3f 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
18a40 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
18a41 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a    *piMoved = 0;.
18a42 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20  .  if( iTable>1 
18a43 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
18a44 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
18a45 0a 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50  .    freePage(pP
18a46 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 72  age, &rc);.    r
18a47 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
18a48 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28  );.#else.    if(
18a49 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
18a4a 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d   ){.      Pgno m
18a4b 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20  axRootPgno;.    
18a4c 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
18a4d 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c  tMeta(p, BTREE_L
18a4e 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45  ARGEST_ROOT_PAGE
18a4f 2c 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b  , &maxRootPgno);
18a50 0a 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62  ..      if( iTab
18a51 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20  le==maxRootPgno 
18a52 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
18a53 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
18a54 20 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20   dropped is the 
18a55 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c  table with the l
18a56 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65  argest root-page
18a57 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62  .        ** numb
18a58 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  er in the databa
18a59 73 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74  se, put the root
18a5a 20 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65   page on the fre
18a5b 65 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20  e list. .       
18a5c 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 72 65 65   */.        free
18a5d 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29  Page(pPage, &rc)
18a5e 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ;.        releas
18a5f 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
18a60 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
18a61 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
18a62 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
18a63 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
18a64 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
18a65 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e  * The table bein
18a66 67 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e  g dropped does n
18a67 6f 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67  ot have the larg
18a68 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20  est root-page.  
18a69 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
18a6a 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
18a6b 20 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67   So move the pag
18a6c 65 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f  e that does into
18a6d 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a   the .        **
18a6e 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65   gap left by the
18a6f 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61   deleted root-pa
18a70 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ge..        */. 
18a71 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a         MemPage *
18a72 70 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72  pMove;.        r
18a73 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
18a74 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
18a75 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  btreeGetPage(pBt
18a76 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26  , maxRootPgno, &
18a77 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20  pMove, 0);.     
18a78 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
18a79 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
18a7a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
18a7b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72       }.        r
18a7c 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65  c = relocatePage
18a7d 28 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52  (pBt, pMove, PTR
18a7e 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c  MAP_ROOTPAGE, 0,
18a7f 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20   iTable, 0);.   
18a80 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
18a81 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20  (pMove);.       
18a82 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
18a83 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
18a84 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
18a85 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 6f     }.        pMo
18a86 76 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ve = 0;.        
18a87 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
18a88 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67  e(pBt, maxRootPg
18a89 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a  no, &pMove, 0);.
18a8a 20 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65          freePage
18a8b 28 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0a 20 20  (pMove, &rc);.  
18a8c 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
18a8d 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20  e(pMove);.      
18a8e 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
18a8f 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
18a90 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
18a91 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70      }.        *p
18a92 69 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74  iMoved = maxRoot
18a93 50 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  Pgno;.      }.. 
18a94 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20       /* Set the 
18a95 6e 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61  new 'max-root-pa
18a96 67 65 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65  ge' value in the
18a97 20 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72   database header
18a98 2e 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20  . This.      ** 
18a99 69 73 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65  is the old value
18a9a 20 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20   less one, less 
18a9b 6f 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74  one more if that
18a9c 20 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20   happens to.    
18a9d 20 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70    ** be a root-p
18a9e 61 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73  age number, less
18a9f 20 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68   one again if th
18aa0 61 74 20 69 73 20 74 68 65 0a 20 20 20 20 20 20  at is the.      
18aa1 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  ** PENDING_BYTE_
18aa2 50 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  PAGE..      */. 
18aa3 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f       maxRootPgno
18aa4 2d 2d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28  --;.      while(
18aa5 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45   maxRootPgno==PE
18aa6 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
18aa7 70 42 74 29 0a 20 20 20 20 20 20 20 20 20 20 20  pBt).           
18aa8 20 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 50 41    || PTRMAP_ISPA
18aa9 47 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50  GE(pBt, maxRootP
18aaa 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 20  gno) ){.        
18aab 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20  maxRootPgno--;. 
18aac 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
18aad 65 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f  ert( maxRootPgno
18aae 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
18aaf 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20 20  AGE(pBt) );..   
18ab0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
18ab1 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70  treeUpdateMeta(p
18ab2 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f  , 4, maxRootPgno
18ab3 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
18ab4 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61      freePage(pPa
18ab5 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20  ge, &rc);.      
18ab6 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
18ab7 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  e);.    }.#endif
18ab8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
18ab9 20 49 66 20 73 71 6c 69 74 65 33 42 74 72 65 65   If sqlite3Btree
18aba 44 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63 61  DropTable was ca
18abb 6c 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e 0a  lled on page 1..
18abc 20 20 20 20 2a 2a 20 54 68 69 73 20 72 65 61 6c      ** This real
18abd 6c 79 20 6e 65 76 65 72 20 73 68 6f 75 6c 64 20  ly never should 
18abe 68 61 70 70 65 6e 20 65 78 63 65 70 74 20 69 6e  happen except in
18abf 20 61 20 63 6f 72 72 75 70 74 0a 20 20 20 20 2a   a corrupt.    *
18ac0 2a 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 20  * database. .   
18ac1 20 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65   */.    zeroPage
18ac2 28 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b  (pPage, PTF_INTK
18ac3 45 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20  EY|PTF_LEAF );. 
18ac4 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
18ac5 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Page);.  }.  ret
18ac6 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49  urn rc;  .}.SQLI
18ac7 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18ac8 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54  qlite3BtreeDropT
18ac9 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69  able(Btree *p, i
18aca 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a  nt iTable, int *
18acb 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20  piMoved){.  int 
18acc 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  rc;.  sqlite3Btr
18acd 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63  eeEnter(p);.  rc
18ace 20 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c   = btreeDropTabl
18acf 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d  e(p, iTable, piM
18ad0 6f 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  oved);.  sqlite3
18ad1 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
18ad2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
18ad3 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
18ad4 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20  ion may only be 
18ad5 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20 62 2d  called if the b-
18ad6 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  tree connection 
18ad7 61 6c 72 65 61 64 79 0a 2a 2a 20 68 61 73 20 61  already.** has a
18ad8 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74   read or write t
18ad9 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20  ransaction open 
18ada 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  on the database.
18adb 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20  .**.** Read the 
18adc 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta-information
18add 20 6f 75 74 20 6f 66 20 61 20 64 61 74 61 62 61   out of a databa
18ade 73 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30  se file.  Meta[0
18adf 5d 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62  ].** is the numb
18ae0 65 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73  er of free pages
18ae1 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68   currently in th
18ae2 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74  e database.  Met
18ae3 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20  a[1].** through 
18ae4 6d 65 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61  meta[15] are ava
18ae5 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62  ilable for use b
18ae6 79 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e  y higher layers.
18ae7 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20    Meta[0].** is 
18ae8 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f  read-only, the o
18ae9 74 68 65 72 73 20 61 72 65 20 72 65 61 64 2f 77  thers are read/w
18aea 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65  rite..** .** The
18aeb 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75   schema layer nu
18aec 6d 62 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65  mbers meta value
18aed 73 20 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20  s differently.  
18aee 41 74 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a  At the schema.**
18aef 20 6c 61 79 65 72 20 28 61 6e 64 20 74 68 65 20   layer (and the 
18af0 53 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65  SetCookie and Re
18af1 61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73  adCookie opcodes
18af2 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  ) the number of.
18af3 2a 2a 20 66 72 65 65 20 70 61 67 65 73 20 69 73  ** free pages is
18af4 20 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53   not visible.  S
18af5 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74  o Cookie[0] is t
18af6 68 65 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b  he same as Meta[
18af7 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  1]..*/.SQLITE_PR
18af8 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
18af9 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42  e3BtreeGetMeta(B
18afa 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78  tree *p, int idx
18afb 2c 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20  , u32 *pMeta){. 
18afc 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
18afd 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69   p->pBt;..  sqli
18afe 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
18aff 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
18b00 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e  nTrans>TRANS_NON
18b01 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53  E );.  assert( S
18b02 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53  QLITE_OK==queryS
18b03 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
18b04 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f  ock(p, MASTER_RO
18b05 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 20 29  OT, READ_LOCK) )
18b06 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d  ;.  assert( pBt-
18b07 3e 70 50 61 67 65 31 20 29 3b 0a 20 20 61 73 73  >pPage1 );.  ass
18b08 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69  ert( idx>=0 && i
18b09 64 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 2a 70 4d  dx<=15 );..  *pM
18b0a 65 74 61 20 3d 20 67 65 74 34 62 79 74 65 28 26  eta = get4byte(&
18b0b 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
18b0c 74 61 5b 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b  ta[36 + idx*4]);
18b0d 0a 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f 2d 76  ..  /* If auto-v
18b0e 61 63 75 75 6d 20 69 73 20 64 69 73 61 62 6c 65  acuum is disable
18b0f 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 20  d in this build 
18b10 61 6e 64 20 74 68 69 73 20 69 73 20 61 6e 20 61  and this is an a
18b11 75 74 6f 2d 76 61 63 75 75 6d 0a 20 20 2a 2a 20  uto-vacuum.  ** 
18b12 64 61 74 61 62 61 73 65 2c 20 6d 61 72 6b 20 74  database, mark t
18b13 68 65 20 64 61 74 61 62 61 73 65 20 61 73 20 72  he database as r
18b14 65 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f 0a 23 69  ead-only.  */.#i
18b15 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
18b16 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66  _AUTOVACUUM.  if
18b17 28 20 69 64 78 3d 3d 42 54 52 45 45 5f 4c 41 52  ( idx==BTREE_LAR
18b18 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 26  GEST_ROOT_PAGE &
18b19 26 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 42 74  & *pMeta>0 ) pBt
18b1a 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a  ->readOnly = 1;.
18b1b 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65  #endif..  sqlite
18b1c 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
18b1d 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d  }../*.** Write m
18b1e 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  eta-information 
18b1f 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61  back into the da
18b20 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d  tabase.  Meta[0]
18b21 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79   is.** read-only
18b22 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20   and may not be 
18b23 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49  written..*/.SQLI
18b24 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18b25 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74  qlite3BtreeUpdat
18b26 65 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20  eMeta(Btree *p, 
18b27 69 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65  int idx, u32 iMe
18b28 74 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ta){.  BtShared 
18b29 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
18b2a 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
18b2b 70 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  pP1;.  int rc;. 
18b2c 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20   assert( idx>=1 
18b2d 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20  && idx<=15 );.  
18b2e 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
18b2f 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r(p);.  assert( 
18b30 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
18b31 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73  S_WRITE );.  ass
18b32 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ert( pBt->pPage1
18b33 21 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d 20 70  !=0 );.  pP1 = p
18b34 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
18b35 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  a;.  rc = sqlite
18b36 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d  3PagerWrite(pBt-
18b37 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65  >pPage1->pDbPage
18b38 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
18b39 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 75  ITE_OK ){.    pu
18b3a 74 34 62 79 74 65 28 26 70 50 31 5b 33 36 20 2b  t4byte(&pP1[36 +
18b3b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b   idx*4], iMeta);
18b3c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18b3d 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
18b3e 20 20 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52      if( idx==BTR
18b3f 45 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 29  EE_INCR_VACUUM )
18b40 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
18b41 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
18b42 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20  || iMeta==0 );. 
18b43 20 20 20 20 20 61 73 73 65 72 74 28 20 69 4d 65       assert( iMe
18b44 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d  ta==0 || iMeta==
18b45 31 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e  1 );.      pBt->
18b46 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 75 38  incrVacuum = (u8
18b47 29 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a 23 65  )iMeta;.    }.#e
18b48 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ndif.  }.  sqlit
18b49 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
18b4a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
18b4b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18b4c 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a  OMIT_BTREECOUNT.
18b4d 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  /*.** The first 
18b4e 61 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20  argument, pCur, 
18b4f 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e  is a cursor open
18b50 65 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65  ed on some b-tre
18b51 65 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20  e. Count the.** 
18b52 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
18b53 73 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20  s in the b-tree 
18b54 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65  and write the re
18b55 73 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79  sult to *pnEntry
18b56 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  ..**.** SQLITE_O
18b57 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  K is returned if
18b58 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69   the operation i
18b59 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65  s successfully e
18b5a 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68  xecuted. .** Oth
18b5b 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72  erwise, if an er
18b5c 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
18b5d 65 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65  ed (i.e. an IO e
18b5e 72 72 6f 72 20 6f 72 20 64 61 74 61 62 61 73 65  rror or database
18b5f 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 29 20  .** corruption) 
18b60 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
18b61 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
18b62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18b63 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
18b64 74 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73  treeCount(BtCurs
18b65 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70  or *pCur, i64 *p
18b66 6e 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e  nEntry){.  i64 n
18b67 45 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20  Entry = 0;      
18b68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b69 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75  /* Value to retu
18b6a 72 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a  rn in *pnEntry *
18b6b 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
18b6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b6d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
18b6e 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72 63 20  rn code */.  rc 
18b6f 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75  = moveToRoot(pCu
18b70 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73  r);..  /* Unless
18b71 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
18b72 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  , the following 
18b73 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 69 74  loop runs one it
18b74 65 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 63 68  eration for each
18b75 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68  .  ** page in th
18b76 65 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75  e B-Tree structu
18b77 72 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e  re (not includin
18b78 67 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  g overflow pages
18b79 29 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65  ). .  */.  while
18b7a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
18b7b 29 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 78 3b  ){.    int iIdx;
18b7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b7d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
18b7e 65 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65  ex of child node
18b7f 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20   in parent */.  
18b80 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
18b81 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
18b82 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
18b83 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72  page of the b-tr
18b84 65 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66  ee */..    /* If
18b85 20 74 68 69 73 20 69 73 20 61 20 6c 65 61 66 20   this is a leaf 
18b86 70 61 67 65 20 6f 72 20 74 68 65 20 74 72 65 65  page or the tree
18b87 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 2d 6b   is not an int-k
18b88 65 79 20 74 72 65 65 2c 20 74 68 65 6e 20 0a 20  ey tree, then . 
18b89 20 20 20 2a 2a 20 74 68 69 73 20 70 61 67 65 20     ** this page 
18b8a 63 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 61 62  contains countab
18b8b 6c 65 20 65 6e 74 72 69 65 73 2e 20 49 6e 63 72  le entries. Incr
18b8c 65 6d 65 6e 74 20 74 68 65 20 65 6e 74 72 79 20  ement the entry 
18b8d 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 61  counter.    ** a
18b8e 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20 20 20  ccordingly..    
18b8f 2a 2f 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70  */.    pPage = p
18b90 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
18b91 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66  ->iPage];.    if
18b92 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c  ( pPage->leaf ||
18b93 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20   !pPage->intKey 
18b94 29 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72 79 20  ){.      nEntry 
18b95 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b  += pPage->nCell;
18b96 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70  .    }..    /* p
18b97 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6e  Page is a leaf n
18b98 6f 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e  ode. This loop n
18b99 61 76 69 67 61 74 65 73 20 74 68 65 20 63 75 72  avigates the cur
18b9a 73 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 0a  sor so that it .
18b9b 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f      ** points to
18b9c 20 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 72   the first inter
18b9d 69 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 69 74  ior cell that it
18b9e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70   points to the p
18b9f 61 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a 2a 20  arent of.    ** 
18ba0 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e  the next page in
18ba1 20 74 68 65 20 74 72 65 65 20 74 68 61 74 20 68   the tree that h
18ba2 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20  as not yet been 
18ba3 76 69 73 69 74 65 64 2e 20 54 68 65 0a 20 20 20  visited. The.   
18ba4 20 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b   ** pCur->aiIdx[
18ba5 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 76 61 6c  pCur->iPage] val
18ba6 75 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ue is set to the
18ba7 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61   index of the pa
18ba8 72 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20 2a 2a  rent cell.    **
18ba9 20 6f 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72   of the page, or
18baa 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
18bab 66 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70  f cells in the p
18bac 61 67 65 20 69 66 20 74 68 65 20 6e 65 78 74 20  age if the next 
18bad 70 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f 20 76  page.    ** to v
18bae 69 73 69 74 20 69 73 20 74 68 65 20 72 69 67 68  isit is the righ
18baf 74 2d 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70  t-child of its p
18bb0 61 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a 20 20  arent..    **.  
18bb1 20 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61 67 65    ** If all page
18bb2 73 20 69 6e 20 74 68 65 20 74 72 65 65 20 68 61  s in the tree ha
18bb3 76 65 20 62 65 65 6e 20 76 69 73 69 74 65 64 2c  ve been visited,
18bb4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
18bb5 4b 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20  K to the.    ** 
18bb6 63 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20  caller..    */. 
18bb7 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65     if( pPage->le
18bb8 61 66 20 29 7b 0a 20 20 20 20 20 20 64 6f 20 7b  af ){.      do {
18bb9 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75  .        if( pCu
18bba 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20  r->iPage==0 ){. 
18bbb 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20           /* All 
18bbc 70 61 67 65 73 20 6f 66 20 74 68 65 20 62 2d 74  pages of the b-t
18bbd 72 65 65 20 68 61 76 65 20 62 65 65 6e 20 76 69  ree have been vi
18bbe 73 69 74 65 64 2e 20 52 65 74 75 72 6e 20 73 75  sited. Return su
18bbf 63 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f 0a 20  ccessfully. */. 
18bc0 20 20 20 20 20 20 20 20 20 2a 70 6e 45 6e 74 72           *pnEntr
18bc1 79 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20  y = nEntry;.    
18bc2 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
18bc3 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20  ITE_OK;.        
18bc4 7d 0a 20 20 20 20 20 20 20 20 6d 6f 76 65 54 6f  }.        moveTo
18bc5 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20  Parent(pCur);.  
18bc6 20 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75      }while ( pCu
18bc7 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
18bc8 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50  Page]>=pCur->apP
18bc9 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
18bca 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20  ->nCell );..    
18bcb 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43    pCur->aiIdx[pC
18bcc 75 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20  ur->iPage]++;.  
18bcd 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72      pPage = pCur
18bce 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
18bcf 50 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20  Page];.    }..  
18bd0 20 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20    /* Descend to 
18bd1 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f  the child node o
18bd2 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20  f the cell that 
18bd3 74 68 65 20 63 75 72 73 6f 72 20 63 75 72 72 65  the cursor curre
18bd4 6e 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69  ntly .    ** poi
18bd5 6e 74 73 20 61 74 2e 20 54 68 69 73 20 69 73 20  nts at. This is 
18bd6 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20  the right-child 
18bd7 69 66 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d  if (iIdx==pPage-
18bd8 3e 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a  >nCell)..    */.
18bd9 20 20 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d      iIdx = pCur-
18bda 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
18bdb 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64  ge];.    if( iId
18bdc 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  x==pPage->nCell 
18bdd 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f  ){.      rc = mo
18bde 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
18bdf 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
18be0 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
18be1 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20  rOffset+8]));.  
18be2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
18be3 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28  c = moveToChild(
18be4 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 66  pCur, get4byte(f
18be5 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  indCell(pPage, i
18be6 49 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20  Idx)));.    }.  
18be7 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72  }..  /* An error
18be8 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52   has occurred. R
18be9 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
18bea 6f 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  ode. */.  return
18beb 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f   rc;.}.#endif../
18bec 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
18bed 70 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64  pager associated
18bee 20 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20   with a BTree.  
18bef 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
18bf0 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74  used for.** test
18bf1 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
18bf2 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  g only..*/.SQLIT
18bf3 45 5f 50 52 49 56 41 54 45 20 50 61 67 65 72 20  E_PRIVATE Pager 
18bf4 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67  *sqlite3BtreePag
18bf5 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  er(Btree *p){.  
18bf6 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70  return p->pBt->p
18bf7 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65  Pager;.}..#ifnde
18bf8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
18bf9 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a  TEGRITY_CHECK./*
18bfa 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73  .** Append a mes
18bfb 73 61 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f  sage to the erro
18bfc 72 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67  r message string
18bfd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
18bfe 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18bff 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a  .  IntegrityCk *
18c00 70 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a  pCheck,.  char *
18c01 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63  zMsg1,.  const c
18c02 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20  har *zFormat,.  
18c03 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74  ....){.  va_list
18c04 20 61 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65   ap;.  if( !pChe
18c05 63 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75  ck->mxErr ) retu
18c06 72 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78  rn;.  pCheck->mx
18c07 45 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d  Err--;.  pCheck-
18c08 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74  >nErr++;.  va_st
18c09 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
18c0a 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e  ;.  if( pCheck->
18c0b 65 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a  errMsg.nChar ){.
18c0c 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
18c0d 63 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63  cumAppend(&pChec
18c0e 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c  k->errMsg, "\n",
18c0f 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a   1);.  }.  if( z
18c10 4d 73 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69  Msg1 ){.    sqli
18c11 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
18c12 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73  d(&pCheck->errMs
18c13 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20  g, zMsg1, -1);. 
18c14 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72   }.  sqlite3VXPr
18c15 69 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72  intf(&pCheck->er
18c16 72 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74  rMsg, 1, zFormat
18c17 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
18c18 61 70 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63  ap);.  if( pChec
18c19 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63  k->errMsg.malloc
18c1a 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43  Failed ){.    pC
18c1b 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  heck->mallocFail
18c1c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65  ed = 1;.  }.}.#e
18c1d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
18c1e 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
18c1f 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ECK */..#ifndef 
18c20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
18c21 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a  GRITY_CHECK./*.*
18c22 2a 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72  * Add 1 to the r
18c23 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66  eference count f
18c24 6f 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20  or page iPage.  
18c25 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73  If this is the s
18c26 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e  econd.** referen
18c27 63 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20  ce to the page, 
18c28 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  add an error mes
18c29 73 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e  sage to pCheck->
18c2a 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75  zErrMsg..** Retu
18c2b 72 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72  rn 1 if there ar
18c2c 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66  e 2 ore more ref
18c2d 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70  erences to the p
18c2e 61 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20  age and 0 if.** 
18c2f 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  if this is the f
18c30 69 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74  irst reference t
18c31 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a  o the page..**.*
18c32 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61  * Also check tha
18c33 74 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  t the page numbe
18c34 72 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a  r is in bounds..
18c35 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68  */.static int ch
18c36 65 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79  eckRef(Integrity
18c37 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f  Ck *pCheck, Pgno
18c38 20 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43   iPage, char *zC
18c39 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69  ontext){.  if( i
18c3a 50 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e  Page==0 ) return
18c3b 20 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e   1;.  if( iPage>
18c3c 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b  pCheck->nPage ){
18c3d 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64  .    checkAppend
18c3e 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
18c3f 74 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70  text, "invalid p
18c40 61 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20  age number %d", 
18c41 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75  iPage);.    retu
18c42 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 1;.  }.  if( 
18c43 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50  pCheck->anRef[iP
18c44 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63  age]==1 ){.    c
18c45 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
18c46 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
18c47 22 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74  "2nd reference t
18c48 6f 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67  o page %d", iPag
18c49 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e);.    return 1
18c4a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20  ;.  }.  return  
18c4b 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69  (pCheck->anRef[i
18c4c 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23  Page]++)>1;.}..#
18c4d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18c4e 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a  IT_AUTOVACUUM./*
18c4f 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74  .** Check that t
18c50 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  he entry in the 
18c51 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20  pointer-map for 
18c52 70 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73  page iChild maps
18c53 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61   to .** page iPa
18c54 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79  rent, pointer ty
18c55 70 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e  pe ptrType. If n
18c56 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72  ot, append an er
18c57 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74  ror message.** t
18c58 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61  o pCheck..*/.sta
18c59 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74  tic void checkPt
18c5a 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74  rmap(.  Integrit
18c5b 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f  yCk *pCheck,   /
18c5c 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63  * Integrity chec
18c5d 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50  k context */.  P
18c5e 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20  gno iChild,     
18c5f 20 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70        /* Child p
18c60 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  age number */.  
18c61 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20  u8 eType,       
18c62 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74         /* Expect
18c63 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74  ed pointer map t
18c64 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50  ype */.  Pgno iP
18c65 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20  arent,          
18c66 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e  /* Expected poin
18c67 74 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70  ter map parent p
18c68 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  age number */.  
18c69 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20  char *zContext  
18c6a 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
18c6b 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75  t description (u
18c6c 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73  sed for error ms
18c6d 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  g) */.){.  int r
18c6e 63 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54  c;.  u8 ePtrmapT
18c6f 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72  ype;.  Pgno iPtr
18c70 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63  mapParent;..  rc
18c71 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68   = ptrmapGet(pCh
18c72 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64  eck->pBt, iChild
18c73 2c 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20  , &ePtrmapType, 
18c74 26 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b  &iPtrmapParent);
18c75 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
18c76 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
18c77 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
18c78 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49   || rc==SQLITE_I
18c79 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68  OERR_NOMEM ) pCh
18c7a 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  eck->mallocFaile
18c7b 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b  d = 1;.    check
18c7c 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
18c7d 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69  , zContext, "Fai
18c7e 6c 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d  led to read ptrm
18c7f 61 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69  ap key=%d", iChi
18c80 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ld);.    return;
18c81 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72  .  }..  if( ePtr
18c82 6d 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c  mapType!=eType |
18c83 7c 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21  | iPtrmapParent!
18c84 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  =iParent ){.    
18c85 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
18c86 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
18c87 20 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72   .      "Bad ptr
18c88 20 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25   map entry key=%
18c89 64 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25  d expected=(%d,%
18c8a 64 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c  d) got=(%d,%d)",
18c8b 20 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20   .      iChild, 
18c8c 65 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20  eType, iParent, 
18c8d 65 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74  ePtrmapType, iPt
18c8e 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d  rmapParent);.  }
18c8f 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
18c90 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67   Check the integ
18c91 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65  rity of the free
18c92 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76  list or of an ov
18c93 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74  erflow page list
18c94 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74  ..** Verify that
18c95 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
18c96 61 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74  ages on the list
18c97 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63   is N..*/.static
18c98 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28   void checkList(
18c99 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a  .  IntegrityCk *
18c9a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65  pCheck,  /* Inte
18c9b 67 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63  grity checking c
18c9c 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
18c9d 69 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20  isFreeList,     
18c9e 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
18c9f 66 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65  freelist.  False
18ca0 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61   for overflow pa
18ca1 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  ge list */.  int
18ca2 20 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20   iPage,         
18ca3 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
18ca4 72 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65  r for first page
18ca5 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a   in the list */.
18ca6 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20    int N,        
18ca7 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63          /* Expec
18ca8 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  ted number of pa
18ca9 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20  ges in the list 
18caa 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74  */.  char *zCont
18cab 65 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ext        /* Co
18cac 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20  ntext for error 
18cad 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20  messages */.){. 
18cae 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78   int i;.  int ex
18caf 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e  pected = N;.  in
18cb0 74 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65  t iFirst = iPage
18cb1 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e  ;.  while( N-- >
18cb2 20 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78   0 && pCheck->mx
18cb3 45 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67  Err ){.    DbPag
18cb4 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20  e *pOvflPage;.  
18cb5 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
18cb6 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20  *pOvflData;.    
18cb7 69 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20  if( iPage<1 ){. 
18cb8 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
18cb9 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
18cba 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22  text,.         "
18cbb 25 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d  %d of %d pages m
18cbc 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72  issing from over
18cbd 66 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69  flow list starti
18cbe 6e 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20  ng at %d",.     
18cbf 20 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74       N+1, expect
18cc0 65 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20  ed, iFirst);.   
18cc1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
18cc2 20 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66      if( checkRef
18cc3 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20  (pCheck, iPage, 
18cc4 7a 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61  zContext) ) brea
18cc5 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  k;.    if( sqlit
18cc6 65 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63  e3PagerGet(pChec
18cc7 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f  k->pPager, (Pgno
18cc8 29 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61  )iPage, &pOvflPa
18cc9 67 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65  ge) ){.      che
18cca 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
18ccb 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66  ck, zContext, "f
18ccc 61 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67  ailed to get pag
18ccd 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20  e %d", iPage);. 
18cce 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
18ccf 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20  }.    pOvflData 
18cd0 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
18cd1 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47   *)sqlite3PagerG
18cd2 65 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65  etData(pOvflPage
18cd3 29 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65  );.    if( isFre
18cd4 65 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69  eList ){.      i
18cd5 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28  nt n = get4byte(
18cd6 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a  &pOvflData[4]);.
18cd7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18cd8 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
18cd9 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d       if( pCheck-
18cda 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  >pBt->autoVacuum
18cdb 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
18cdc 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
18cdd 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52  iPage, PTRMAP_FR
18cde 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74  EEPAGE, 0, zCont
18cdf 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  ext);.      }.#e
18ce0 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e  ndif.      if( n
18ce1 3e 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73  >pCheck->pBt->us
18ce2 61 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a  ableSize/4-2 ){.
18ce3 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70          checkApp
18ce4 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
18ce5 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20  Context,.       
18ce6 20 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65      "freelist le
18ce7 61 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67  af count too big
18ce8 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50   on page %d", iP
18ce9 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d  age);.        N-
18cea 2d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  -;.      }else{.
18ceb 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
18cec 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<n; i++){.    
18ced 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65        Pgno iFree
18cee 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28  Page = get4byte(
18cef 26 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34  &pOvflData[8+i*4
18cf0 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ]);.#ifndef SQLI
18cf1 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
18cf2 55 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  UM.          if(
18cf3 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75   pCheck->pBt->au
18cf4 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
18cf5 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72          checkPtr
18cf6 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65  map(pCheck, iFre
18cf7 65 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52  ePage, PTRMAP_FR
18cf8 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74  EEPAGE, 0, zCont
18cf9 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ext);.          
18cfa 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  }.#endif.       
18cfb 20 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65     checkRef(pChe
18cfc 63 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a  ck, iFreePage, z
18cfd 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  Context);.      
18cfe 20 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d    }.        N -=
18cff 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   n;.      }.    
18d00 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
18d01 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
18d02 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20  .    else{.     
18d03 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61   /* If this data
18d04 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75  base supports au
18d05 74 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50  to-vacuum and iP
18d06 61 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c  age is not the l
18d07 61 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67  ast.      ** pag
18d08 65 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c  e in this overfl
18d09 6f 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74  ow list, check t
18d0a 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d  hat the pointer-
18d0b 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20  map entry for.  
18d0c 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f      ** the follo
18d0d 77 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65  wing page matche
18d0e 73 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a  s iPage..      *
18d0f 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65  /.      if( pChe
18d10 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63  ck->pBt->autoVac
18d11 75 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20  uum && N>0 ){.  
18d12 20 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79        i = get4by
18d13 74 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20  te(pOvflData);. 
18d14 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d         checkPtrm
18d15 61 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54  ap(pCheck, i, PT
18d16 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20  RMAP_OVERFLOW2, 
18d17 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29  iPage, zContext)
18d18 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
18d19 23 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65  #endif.    iPage
18d1a 20 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66   = get4byte(pOvf
18d1b 6c 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69  lData);.    sqli
18d1c 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f  te3PagerUnref(pO
18d1d 76 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a  vflPage);.  }.}.
18d1e 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
18d1f 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
18d20 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65  CHECK */..#ifnde
18d21 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
18d22 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a  TEGRITY_CHECK./*
18d23 0a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73  .** Do various s
18d24 61 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20  anity checks on 
18d25 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66  a single page of
18d26 20 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e   a tree.  Return
18d27 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70  .** the tree dep
18d28 74 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20  th.  Root pages 
18d29 72 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e  return 0.  Paren
18d2a 74 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73  ts of root pages
18d2b 0a 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e  .** return 1, an
18d2c 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a  d so forth..** .
18d2d 2a 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20  ** These checks 
18d2e 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20  are done:.**.** 
18d2f 20 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75       1.  Make su
18d30 72 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e  re that cells an
18d31 64 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20  d freeblocks do 
18d32 6e 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20  not overlap.**  
18d33 20 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62          but comb
18d34 69 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c  ine to completel
18d35 79 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65  y cover the page
18d36 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61  ..**  NO  2.  Ma
18d37 6b 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79  ke sure cell key
18d38 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a  s are in order..
18d39 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65  **  NO  3.  Make
18d3a 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20   sure no key is 
18d3b 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75  less than or equ
18d3c 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e  al to zLowerBoun
18d3d 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d  d..**  NO  4.  M
18d3e 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20  ake sure no key 
18d3f 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
18d40 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70  or equal to zUpp
18d41 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20  erBound..**     
18d42 20 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69   5.  Check the i
18d43 6e 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72  ntegrity of over
18d44 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20  flow pages..**  
18d45 20 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76      6.  Recursiv
18d46 65 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72  ely call checkTr
18d47 65 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68  eePage on all ch
18d48 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20  ildren..**      
18d49 37 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20  7.  Verify that 
18d4a 74 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c  the depth of all
18d4b 20 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65   children is the
18d4c 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38   same..**      8
18d4d 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69  .  Make sure thi
18d4e 73 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61  s page is at lea
18d4f 73 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65  st 33% full or e
18d50 6c 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20  lse it is.**    
18d51 20 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f        the root o
18d52 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73  f the tree..*/.s
18d53 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54  tatic int checkT
18d54 72 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67  reePage(.  Integ
18d55 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20  rityCk *pCheck, 
18d56 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20   /* Context for 
18d57 74 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b  the sanity check
18d58 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c   */.  int iPage,
18d59 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
18d5a 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
18d5b 65 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20  e page to check 
18d5c 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65  */.  char *zPare
18d5d 6e 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61  ntContext  /* Pa
18d5e 72 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rent context */.
18d5f 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  ){.  MemPage *pP
18d60 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63  age;.  int i, rc
18d61 2c 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e  , depth, d2, pgn
18d62 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64  o, cnt;.  int hd
18d63 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20  r, cellStart;.  
18d64 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20  int nCell;.  u8 
18d65 2a 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65  *data;.  BtShare
18d66 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73  d *pBt;.  int us
18d67 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72  ableSize;.  char
18d68 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a   zContext[100];.
18d69 20 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b    char *hit = 0;
18d6a 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ..  sqlite3_snpr
18d6b 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e  intf(sizeof(zCon
18d6c 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c  text), zContext,
18d6d 20 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50   "Page %d: ", iP
18d6e 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  age);..  /* Chec
18d6f 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  k that the page 
18d70 65 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42  exists.  */.  pB
18d71 74 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b  t = pCheck->pBt;
18d72 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20  .  usableSize = 
18d73 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b  pBt->usableSize;
18d74 0a 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20  .  if( iPage==0 
18d75 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
18d76 28 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63  ( checkRef(pChec
18d77 6b 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e  k, iPage, zParen
18d78 74 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75  tContext) ) retu
18d79 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20  rn 0;.  if( (rc 
18d7a 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
18d7b 42 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c  Bt, (Pgno)iPage,
18d7c 20 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 20   &pPage, 0))!=0 
18d7d 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65  ){.    checkAppe
18d7e 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
18d7f 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 22  ontext,.       "
18d80 75 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68  unable to get th
18d81 65 20 70 61 67 65 2e 20 65 72 72 6f 72 20 63 6f  e page. error co
18d82 64 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20 20 20  de=%d", rc);.   
18d83 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
18d84 20 20 2f 2a 20 43 6c 65 61 72 20 4d 65 6d 50 61    /* Clear MemPa
18d85 67 65 2e 69 73 49 6e 69 74 20 74 6f 20 6d 61 6b  ge.isInit to mak
18d86 65 20 73 75 72 65 20 74 68 65 20 63 6f 72 72 75  e sure the corru
18d87 70 74 69 6f 6e 20 64 65 74 65 63 74 69 6f 6e 20  ption detection 
18d88 63 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 62 74 72  code in.  ** btr
18d89 65 65 49 6e 69 74 50 61 67 65 28 29 20 69 73 20  eeInitPage() is 
18d8a 65 78 65 63 75 74 65 64 2e 20 20 2a 2f 0a 20 20  executed.  */.  
18d8b 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
18d8c 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 62  0;.  if( (rc = b
18d8d 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61  treeInitPage(pPa
18d8e 67 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61  ge))!=0 ){.    a
18d8f 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
18d90 45 5f 43 4f 52 52 55 50 54 20 29 3b 20 20 2f 2a  E_CORRUPT );  /*
18d91 20 54 68 65 20 6f 6e 6c 79 20 70 6f 73 73 69 62   The only possib
18d92 6c 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 49 6e  le error from In
18d93 69 74 50 61 67 65 20 2a 2f 0a 20 20 20 20 63 68  itPage */.    ch
18d94 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
18d95 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a  eck, zContext, .
18d96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18d97 20 20 20 22 62 74 72 65 65 49 6e 69 74 50 61 67     "btreeInitPag
18d98 65 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f  e() returns erro
18d99 72 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b  r code %d", rc);
18d9a 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
18d9b 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74  (pPage);.    ret
18d9c 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 0;.  }..  /*
18d9d 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74   Check out all t
18d9e 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20  he cells..  */. 
18d9f 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f   depth = 0;.  fo
18da0 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e  r(i=0; i<pPage->
18da1 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d  nCell && pCheck-
18da2 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20  >mxErr; i++){.  
18da3 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20    u8 *pCell;.   
18da4 20 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c   u32 sz;.    Cel
18da5 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20  lInfo info;..   
18da6 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61   /* Check payloa
18da7 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  d overflow pages
18da8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
18da9 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
18daa 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a  eof(zContext), z
18dab 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20  Context,.       
18dac 20 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70        "On tree p
18dad 61 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20  age %d cell %d: 
18dae 22 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20  ", iPage, i);.  
18daf 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65    pCell = findCe
18db0 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20  ll(pPage,i);.   
18db1 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50   btreeParseCellP
18db2 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
18db3 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 73 7a 20   &info);.    sz 
18db4 3d 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20  = info.nData;.  
18db5 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 6e    if( !pPage->in
18db6 74 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28 69 6e  tKey ) sz += (in
18db7 74 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20  t)info.nKey;.   
18db8 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66   assert( sz==inf
18db9 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20  o.nPayload );.  
18dba 20 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e    if( (sz>info.n
18dbb 4c 6f 63 61 6c 29 20 0a 20 20 20 20 20 26 26 20  Local) .     && 
18dbc 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76  (&pCell[info.iOv
18dbd 65 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67 65 2d  erflow]<=&pPage-
18dbe 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62  >aData[pBt->usab
18dbf 6c 65 53 69 7a 65 5d 29 0a 20 20 20 20 29 7b 0a  leSize]).    ){.
18dc0 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20        int nPage 
18dc1 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f  = (sz - info.nLo
18dc2 63 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a 65  cal + usableSize
18dc3 20 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 7a   - 5)/(usableSiz
18dc4 65 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 67  e - 4);.      Pg
18dc5 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65  no pgnoOvfl = ge
18dc6 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e  t4byte(&pCell[in
18dc7 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a  fo.iOverflow]);.
18dc8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18dc9 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
18dca 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75       if( pBt->au
18dcb 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
18dcc 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28      checkPtrmap(
18dcd 70 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c  pCheck, pgnoOvfl
18dce 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  , PTRMAP_OVERFLO
18dcf 57 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74  W1, iPage, zCont
18dd0 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  ext);.      }.#e
18dd1 6e 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 6b  ndif.      check
18dd2 4c 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c 20  List(pCheck, 0, 
18dd3 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c  pgnoOvfl, nPage,
18dd4 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   zContext);.    
18dd5 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  }..    /* Check 
18dd6 73 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 63  sanity of left c
18dd7 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 2a  hild page..    *
18dd8 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65  /.    if( !pPage
18dd9 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
18dda 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
18ddb 70 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20  pCell);.#ifndef 
18ddc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
18ddd 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28  VACUUM.      if(
18dde 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
18ddf 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
18de0 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
18de1 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52  pgno, PTRMAP_BTR
18de2 45 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74  EE, iPage, zCont
18de3 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  ext);.      }.#e
18de4 6e 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d 20  ndif.      d2 = 
18de5 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43  checkTreePage(pC
18de6 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e  heck, pgno, zCon
18de7 74 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28  text);.      if(
18de8 20 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74   i>0 && d2!=dept
18de9 68 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65  h ){.        che
18dea 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
18deb 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43  ck, zContext, "C
18dec 68 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20  hild page depth 
18ded 64 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20  differs");.     
18dee 20 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d   }.      depth =
18def 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   d2;.    }.  }. 
18df0 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
18df1 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20  f ){.    pgno = 
18df2 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
18df3 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
18df4 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20  rOffset+8]);.   
18df5 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
18df6 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78  f(sizeof(zContex
18df7 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20  t), zContext, . 
18df8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18df9 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20      "On page %d 
18dfa 61 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20  at right child: 
18dfb 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64  ", iPage);.#ifnd
18dfc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
18dfd 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
18dfe 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
18dff 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b  m ){.      check
18e00 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70  Ptrmap(pCheck, p
18e01 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
18e02 45 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20  E, iPage, 0);.  
18e03 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63    }.#endif.    c
18e04 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68  heckTreePage(pCh
18e05 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74  eck, pgno, zCont
18e06 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a  ext);.  }. .  /*
18e07 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c   Check for compl
18e08 65 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 20  ete coverage of 
18e09 74 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 20  the page.  */.  
18e0a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
18e0b 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61  ata;.  hdr = pPa
18e0c 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20  ge->hdrOffset;. 
18e0d 20 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61   hit = sqlite3Pa
18e0e 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70  geMalloc( pBt->p
18e0f 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28  ageSize );.  if(
18e10 20 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70   hit==0 ){.    p
18e11 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69  Check->mallocFai
18e12 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  led = 1;.  }else
18e13 7b 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 6e  {.    u16 conten
18e14 74 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 79  tOffset = get2by
18e15 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
18e16 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f  ;.    assert( co
18e17 6e 74 65 6e 74 4f 66 66 73 65 74 3c 3d 75 73 61  ntentOffset<=usa
18e18 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45  bleSize );  /* E
18e19 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65  nforced by btree
18e1a 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20  InitPage() */.  
18e1b 20 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e    memset(hit+con
18e1c 74 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75  tentOffset, 0, u
18e1d 73 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e  sableSize-conten
18e1e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6d 65  tOffset);.    me
18e1f 6d 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e  mset(hit, 1, con
18e20 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20  tentOffset);.   
18e21 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74   nCell = get2byt
18e22 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b  e(&data[hdr+3]);
18e23 0a 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20 3d  .    cellStart =
18e24 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50   hdr + 12 - 4*pP
18e25 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66  age->leaf;.    f
18e26 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  or(i=0; i<nCell;
18e27 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74   i++){.      int
18e28 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26   pc = get2byte(&
18e29 64 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69  data[cellStart+i
18e2a 2a 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 36 20  *2]);.      u16 
18e2b 73 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20  size = 1024;.   
18e2c 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
18e2d 69 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 53 69  if( pc<=usableSi
18e2e 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20  ze-4 ){.        
18e2f 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50  size = cellSizeP
18e30 74 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b  tr(pPage, &data[
18e31 70 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  pc]);.      }.  
18e32 20 20 20 20 69 66 28 20 28 70 63 2b 73 69 7a 65      if( (pc+size
18e33 2d 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20  -1)>=usableSize 
18e34 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
18e35 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
18e36 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20  , 0, .          
18e37 20 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65    "Corruption de
18e38 74 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25  tected in cell %
18e39 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c  d on page %d",i,
18e3a 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20  iPage,0);.      
18e3b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66  }else{.        f
18e3c 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20  or(j=pc+size-1; 
18e3d 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b  j>=pc; j--) hit[
18e3e 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  j]++;.      }.  
18e3f 20 20 7d 0a 20 20 20 20 69 20 3d 20 67 65 74 32    }.    i = get2
18e40 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31  byte(&data[hdr+1
18e41 5d 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69  ]);.    while( i
18e42 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  >0 ){.      int 
18e43 73 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 20 20 61  size, j;.      a
18e44 73 73 65 72 74 28 20 69 3c 3d 75 73 61 62 6c 65  ssert( i<=usable
18e45 53 69 7a 65 2d 34 20 29 3b 20 20 20 20 20 2f 2a  Size-4 );     /*
18e46 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72   Enforced by btr
18e47 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a  eeInitPage() */.
18e48 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74        size = get
18e49 32 62 79 74 65 28 26 64 61 74 61 5b 69 2b 32 5d  2byte(&data[i+2]
18e4a 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
18e4b 20 69 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53   i+size<=usableS
18e4c 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72  ize );  /* Enfor
18e4d 63 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74  ced by btreeInit
18e4e 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20  Page() */.      
18e4f 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20  for(j=i+size-1; 
18e50 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a  j>=i; j--) hit[j
18e51 5d 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 3d 20 67  ]++;.      j = g
18e52 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 5d  et2byte(&data[i]
18e53 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
18e54 20 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b 73 69 7a   j==0 || j>i+siz
18e55 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 65  e );  /* Enforce
18e56 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61  d by btreeInitPa
18e57 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 61 73  ge() */.      as
18e58 73 65 72 74 28 20 6a 3c 3d 75 73 61 62 6c 65 53  sert( j<=usableS
18e59 69 7a 65 2d 34 20 29 3b 20 20 20 2f 2a 20 45 6e  ize-4 );   /* En
18e5a 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49  forced by btreeI
18e5b 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20  nitPage() */.   
18e5c 20 20 20 69 20 3d 20 6a 3b 0a 20 20 20 20 7d 0a     i = j;.    }.
18e5d 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b      for(i=cnt=0;
18e5e 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69   i<usableSize; i
18e5f 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68  ++){.      if( h
18e60 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  it[i]==0 ){.    
18e61 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20      cnt++;.     
18e62 20 7d 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69   }else if( hit[i
18e63 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ]>1 ){.        c
18e64 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
18e65 68 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20  heck, 0,.       
18e66 20 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65     "Multiple use
18e67 73 20 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66  s for byte %d of
18e68 20 70 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50   page %d", i, iP
18e69 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72  age);.        br
18e6a 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
18e6b 20 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d   }.    if( cnt!=
18e6c 64 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20  data[hdr+7] ){. 
18e6d 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
18e6e 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a  Msg(pCheck, 0, .
18e6f 20 20 20 20 20 20 20 20 20 20 22 46 72 61 67 6d            "Fragm
18e70 65 6e 74 61 74 69 6f 6e 20 6f 66 20 25 64 20 62  entation of %d b
18e71 79 74 65 73 20 72 65 70 6f 72 74 65 64 20 61 73  ytes reported as
18e72 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c   %d on page %d",
18e73 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20  .          cnt, 
18e74 64 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61  data[hdr+7], iPa
18e75 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ge);.    }.  }. 
18e76 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65   sqlite3PageFree
18e77 28 68 69 74 29 3b 0a 20 20 72 65 6c 65 61 73 65  (hit);.  release
18e78 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72  Page(pPage);.  r
18e79 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d  eturn depth+1;.}
18e7a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
18e7b 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59  E_OMIT_INTEGRITY
18e7c 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64  _CHECK */..#ifnd
18e7d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
18e7e 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f  NTEGRITY_CHECK./
18e7f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
18e80 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74  e does a complet
18e81 65 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 67  e check of the g
18e82 69 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 2e  iven BTree file.
18e83 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20    aRoot[] is.** 
18e84 61 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 65  an array of page
18e85 73 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 65  s numbers were e
18e86 61 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ach page number 
18e87 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  is the root page
18e88 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20   of.** a table. 
18e89 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e 75   nRoot is the nu
18e8a 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
18e8b 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20  in aRoot..**.** 
18e8c 41 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72  A read-only or r
18e8d 65 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61  ead-write transa
18e8e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70  ction must be op
18e8f 65 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c  ened before call
18e90 69 6e 67 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63  ing.** this func
18e91 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74  tion..**.** Writ
18e92 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
18e93 65 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70  error seen in *p
18e94 6e 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f  nErr.  Except fo
18e95 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a  r some memory.**
18e96 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
18e97 72 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d 65  rs,  an error me
18e98 73 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d 65  ssage held in me
18e99 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
18e9a 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73 20  om.** malloc is 
18e9b 72 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e 45  returned if *pnE
18e9c 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  rr is non-zero. 
18e9d 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74 68   If *pnErr==0 th
18e9e 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 72 65  en NULL is.** re
18e9f 74 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d 65  turned.  If a me
18ea0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
18ea1 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55  error occurs, NU
18ea2 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  LL is returned..
18ea3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18ea4 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42  E char *sqlite3B
18ea5 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65  treeIntegrityChe
18ea6 63 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20  ck(.  Btree *p, 
18ea7 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65      /* The btree
18ea8 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a   to be checked *
18ea9 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20  /.  int *aRoot, 
18eaa 20 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66    /* An array of
18eab 20 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62   root pages numb
18eac 65 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75  ers for individu
18ead 61 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e  al trees */.  in
18eae 74 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e  t nRoot,    /* N
18eaf 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
18eb0 20 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20   in aRoot[] */. 
18eb1 20 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f   int mxErr,    /
18eb2 2a 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67  * Stop reporting
18eb3 20 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 68   errors after th
18eb4 69 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74  is many */.  int
18eb5 20 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72   *pnErr    /* Wr
18eb6 69 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72  ite number of er
18eb7 72 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69  rors seen to thi
18eb8 73 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b  s variable */.){
18eb9 0a 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 6e 74  .  Pgno i;.  int
18eba 20 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 69   nRef;.  Integri
18ebb 74 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42  tyCk sCheck;.  B
18ebc 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
18ebd 2d 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a 45  ->pBt;.  char zE
18ebe 72 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c 69  rr[100];..  sqli
18ebf 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
18ec0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
18ec1 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e  nTrans>TRANS_NON
18ec2 45 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e  E && pBt->inTran
18ec3 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f  saction>TRANS_NO
18ec4 4e 45 20 29 3b 0a 20 20 6e 52 65 66 20 3d 20 73  NE );.  nRef = s
18ec5 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
18ec6 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29  unt(pBt->pPager)
18ec7 3b 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d  ;.  sCheck.pBt =
18ec8 20 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70   pBt;.  sCheck.p
18ec9 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61  Pager = pBt->pPa
18eca 67 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50  ger;.  sCheck.nP
18ecb 61 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63  age = pagerPagec
18ecc 6f 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29  ount(sCheck.pBt)
18ecd 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72  ;.  sCheck.mxErr
18ece 20 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65   = mxErr;.  sChe
18ecf 63 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73  ck.nErr = 0;.  s
18ed0 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c  Check.mallocFail
18ed1 65 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72  ed = 0;.  *pnErr
18ed2 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 68 65   = 0;.  if( sChe
18ed3 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20  ck.nPage==0 ){. 
18ed4 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
18ed5 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74  eave(p);.    ret
18ed6 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68  urn 0;.  }.  sCh
18ed7 65 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69  eck.anRef = sqli
18ed8 74 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65  te3Malloc( (sChe
18ed9 63 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65  ck.nPage+1)*size
18eda 6f 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b  of(sCheck.anRef[
18edb 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43  0]) );.  if( !sC
18edc 68 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20  heck.anRef ){.  
18edd 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20    *pnErr = 1;.  
18ede 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
18edf 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75  ave(p);.    retu
18ee0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  rn 0;.  }.  for(
18ee1 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e  i=0; i<=sCheck.n
18ee2 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65  Page; i++){ sChe
18ee3 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b  ck.anRef[i] = 0;
18ee4 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47   }.  i = PENDING
18ee5 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b  _BYTE_PAGE(pBt);
18ee6 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b  .  if( i<=sCheck
18ee7 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 43  .nPage ){.    sC
18ee8 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20  heck.anRef[i] = 
18ee9 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  1;.  }.  sqlite3
18eea 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 43  StrAccumInit(&sC
18eeb 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 72  heck.errMsg, zEr
18eec 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c  r, sizeof(zErr),
18eed 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 43   20000);..  /* C
18eee 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69  heck the integri
18eef 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69  ty of the freeli
18ef0 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c  st.  */.  checkL
18ef1 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c 20  ist(&sCheck, 1, 
18ef2 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70  get4byte(&pBt->p
18ef3 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d  Page1->aData[32]
18ef4 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  ),.            g
18ef5 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50  et4byte(&pBt->pP
18ef6 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29  age1->aData[36])
18ef7 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74  , "Main freelist
18ef8 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  : ");..  /* Chec
18ef9 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73  k all the tables
18efa 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
18efb 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26  ; (int)i<nRoot &
18efc 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20  & sCheck.mxErr; 
18efd 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52  i++){.    if( aR
18efe 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  oot[i]==0 ) cont
18eff 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51  inue;.#ifndef SQ
18f00 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
18f01 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74  CUUM.    if( pBt
18f02 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20  ->autoVacuum && 
18f03 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20  aRoot[i]>1 ){.  
18f04 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28      checkPtrmap(
18f05 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69  &sCheck, aRoot[i
18f06 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  ], PTRMAP_ROOTPA
18f07 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d  GE, 0, 0);.    }
18f08 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63  .#endif.    chec
18f09 6b 54 72 65 65 50 61 67 65 28 26 73 43 68 65 63  kTreePage(&sChec
18f0a 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69  k, aRoot[i], "Li
18f0b 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73  st of tree roots
18f0c 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  : ");.  }..  /* 
18f0d 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 20  Make sure every 
18f0e 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65  page in the file
18f0f 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a 20   is referenced. 
18f10 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69   */.  for(i=1; i
18f11 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 26  <=sCheck.nPage &
18f12 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20  & sCheck.mxErr; 
18f13 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  i++){.#ifdef SQL
18f14 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
18f15 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 65  UUM.    if( sChe
18f16 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29  ck.anRef[i]==0 )
18f17 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70  {.      checkApp
18f18 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20  endMsg(&sCheck, 
18f19 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 6e  0, "Page %d is n
18f1a 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a  ever used", i);.
18f1b 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20      }.#else.    
18f1c 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  /* If the databa
18f1d 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f  se supports auto
18f1e 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75  -vacuum, make su
18f1f 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e  re no tables con
18f20 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65  tain.    ** refe
18f21 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 65  rences to pointe
18f22 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 20  r-map pages..   
18f23 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 65   */.    if( sChe
18f24 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26  ck.anRef[i]==0 &
18f25 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41  & .       (PTRMA
18f26 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29  P_PAGENO(pBt, i)
18f27 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74  !=i || !pBt->aut
18f28 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20  oVacuum) ){.    
18f29 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
18f2a 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61  (&sCheck, 0, "Pa
18f2b 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75  ge %d is never u
18f2c 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a  sed", i);.    }.
18f2d 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61      if( sCheck.a
18f2e 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20  nRef[i]!=0 && . 
18f2f 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41        (PTRMAP_PA
18f30 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20  GENO(pBt, i)==i 
18f31 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  && pBt->autoVacu
18f32 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65  um) ){.      che
18f33 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68  ckAppendMsg(&sCh
18f34 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72  eck, 0, "Pointer
18f35 20 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 20   map page %d is 
18f36 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 3b  referenced", i);
18f37 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
18f38 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  }..  /* Make sur
18f39 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 20  e this analysis 
18f3a 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e  did not leave an
18f3b 79 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 2e  y unref() pages.
18f3c 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 6e  .  ** This is an
18f3d 20 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69 73   internal consis
18f3e 74 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e 20  tency check; an 
18f3f 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 0a  integrity check.
18f40 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74 65    ** of the inte
18f41 67 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 20 2a  grity check..  *
18f42 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52  /.  if( NEVER(nR
18f43 65 66 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67  ef != sqlite3Pag
18f44 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e  erRefcount(pBt->
18f45 70 50 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20  pPager)) ){.    
18f46 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26  checkAppendMsg(&
18f47 73 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20  sCheck, 0, .    
18f48 20 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70    "Outstanding p
18f49 61 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66  age count goes f
18f4a 72 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72  rom %d to %d dur
18f4b 69 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69  ing this analysi
18f4c 73 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20  s",.      nRef, 
18f4d 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
18f4e 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
18f4f 29 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20  ).    );.  }..  
18f50 2f 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64  /* Clean  up and
18f51 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a   report errors..
18f52 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74    */.  sqlite3Bt
18f53 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73  reeLeave(p);.  s
18f54 71 6c 69 74 65 33 5f 66 72 65 65 28 73 43 68 65  qlite3_free(sChe
18f55 63 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28  ck.anRef);.  if(
18f56 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61   sCheck.mallocFa
18f57 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  iled ){.    sqli
18f58 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74  te3StrAccumReset
18f59 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29  (&sCheck.errMsg)
18f5a 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73  ;.    *pnErr = s
18f5b 43 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20  Check.nErr+1;.  
18f5c 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
18f5d 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63    *pnErr = sChec
18f5e 6b 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43  k.nErr;.  if( sC
18f5f 68 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73  heck.nErr==0 ) s
18f60 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65  qlite3StrAccumRe
18f61 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d  set(&sCheck.errM
18f62 73 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  sg);.  return sq
18f63 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e  lite3StrAccumFin
18f64 69 73 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d  ish(&sCheck.errM
18f65 73 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  sg);.}.#endif /*
18f66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
18f67 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a  EGRITY_CHECK */.
18f68 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
18f69 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20  e full pathname 
18f6a 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  of the underlyin
18f6b 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  g database file.
18f6c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  .**.** The pager
18f6d 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76   filename is inv
18f6e 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61  ariant as long a
18f6f 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a  s the pager is.*
18f70 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20  * open so it is 
18f71 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77  safe to access w
18f72 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61  ithout the BtSha
18f73 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51  red mutex..*/.SQ
18f74 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
18f75 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
18f76 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
18f77 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73  (Btree *p){.  as
18f78 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50  sert( p->pBt->pP
18f79 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74  ager!=0 );.  ret
18f7a 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72  urn sqlite3Pager
18f7b 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d  Filename(p->pBt-
18f7c 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a  >pPager);.}../*.
18f7d 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61  ** Return the pa
18f7e 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f  thname of the jo
18f7f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74  urnal file for t
18f80 68 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 68  his database. Th
18f81 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75  e return.** valu
18f82 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  e of this routin
18f83 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65  e is the same re
18f84 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
18f85 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  her the journal 
18f86 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e  file.** has been
18f87 20 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e   created or not.
18f88 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  .**.** The pager
18f89 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d   journal filenam
18f8a 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61  e is invariant a
18f8b 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61  s long as the pa
18f8c 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73  ger is.** open s
18f8d 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20  o it is safe to 
18f8e 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74  access without t
18f8f 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65  he BtShared mute
18f90 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  x..*/.SQLITE_PRI
18f91 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
18f92 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74  *sqlite3BtreeGet
18f93 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65  Journalname(Btre
18f94 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  e *p){.  assert(
18f95 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21   p->pBt->pPager!
18f96 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  =0 );.  return s
18f97 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
18f98 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70  alname(p->pBt->p
18f99 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Pager);.}../*.**
18f9a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f   Return non-zero
18f9b 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   if a transactio
18f9c 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a  n is active..*/.
18f9d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
18f9e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
18f9f 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a  sInTrans(Btree *
18fa0 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d  p){.  assert( p=
18fa1 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
18fa2 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
18fa3 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75  mutex) );.  retu
18fa4 72 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54  rn (p && (p->inT
18fa5 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
18fa6 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  E));.}../*.** Re
18fa7 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66  turn non-zero if
18fa8 20 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74   a read (or writ
18fa9 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  e) transaction i
18faa 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c  s active..*/.SQL
18fab 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
18fac 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
18fad 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20  ReadTrans(Btree 
18fae 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
18faf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
18fb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
18fb1 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
18fb2 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e  ;.  return p->in
18fb3 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e  Trans!=TRANS_NON
18fb4 45 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  E;.}..SQLITE_PRI
18fb5 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
18fb6 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28  BtreeIsInBackup(
18fb7 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73  Btree *p){.  ass
18fb8 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65  ert( p );.  asse
18fb9 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
18fba 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
18fbb 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  tex) );.  return
18fbc 20 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a   p->nBackup!=0;.
18fbd 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
18fbe 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61  nction returns a
18fbf 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c   pointer to a bl
18fc0 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73  ob of memory ass
18fc1 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20  ociated with.** 
18fc2 61 20 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d  a single shared-
18fc3 62 74 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72  btree. The memor
18fc4 79 20 69 73 20 75 73 65 64 20 62 79 20 63 6c 69  y is used by cli
18fc5 65 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73  ent code for its
18fc6 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73   own.** purposes
18fc7 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74   (for example, t
18fc8 6f 20 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c  o store a high-l
18fc9 65 76 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f  evel schema asso
18fca 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20  ciated with .** 
18fcb 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65  the shared-btree
18fcc 29 2e 20 54 68 65 20 62 74 72 65 65 20 6c 61 79  ). The btree lay
18fcd 65 72 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72  er manages refer
18fce 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73  ence counting is
18fcf 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sues..**.** The 
18fd0 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20  first time this 
18fd1 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73  is called on a s
18fd2 68 61 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79  hared-btree, nBy
18fd3 74 65 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  tes bytes of mem
18fd4 6f 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63  ory.** are alloc
18fd5 61 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e  ated, zeroed, an
18fd6 64 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  d returned to th
18fd7 65 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61  e caller. For ea
18fd8 63 68 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a  ch subsequent .*
18fd9 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65  * call the nByte
18fda 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69  s parameter is i
18fdb 67 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69  gnored and a poi
18fdc 6e 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65  nter to the same
18fdd 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f   blob.** of memo
18fde 72 79 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a  ry returned. .**
18fdf 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65  .** If the nByte
18fe0 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30  s parameter is 0
18fe1 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66   and the blob of
18fe2 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20   memory has not 
18fe3 79 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f  yet been.** allo
18fe4 63 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f  cated, a null po
18fe5 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65  inter is returne
18fe6 64 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68  d. If the blob h
18fe7 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a  as already been.
18fe8 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74  ** allocated, it
18fe9 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20   is returned as 
18fea 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75  normal..**.** Ju
18feb 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 73 68  st before the sh
18fec 61 72 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c  ared-btree is cl
18fed 6f 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69  osed, the functi
18fee 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65  on passed as the
18fef 20 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d   .** xFree argum
18ff0 65 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d  ent when the mem
18ff1 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77  ory allocation w
18ff2 61 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b  as made is invok
18ff3 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c  ed on the .** bl
18ff4 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20  ob of allocated 
18ff5 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e  memory. This fun
18ff6 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74  ction should not
18ff7 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72   call sqlite3_fr
18ff8 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d  ee().** on the m
18ff9 65 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65  emory, the btree
18ffa 20 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74   layer does that
18ffb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18ffc 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
18ffd 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72  3BtreeSchema(Btr
18ffe 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65  ee *p, int nByte
18fff 73 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28  s, void(*xFree)(
19000 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68  void *)){.  BtSh
19001 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
19002 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  Bt;.  sqlite3Btr
19003 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66  eeEnter(p);.  if
19004 28 20 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20  ( !pBt->pSchema 
19005 26 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20  && nBytes ){.   
19006 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20   pBt->pSchema = 
19007 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
19008 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70  o(nBytes);.    p
19009 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20  Bt->xFreeSchema 
1900a 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73  = xFree;.  }.  s
1900b 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1900c 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42  (p);.  return pB
1900d 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f  t->pSchema;.}../
1900e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
1900f 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
19010 43 41 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72  CACHE if another
19011 20 75 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d   user of the sam
19012 65 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72  e shared .** btr
19013 65 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65  ee as the argume
19014 6e 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20  nt handle holds 
19015 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
19016 6b 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c  k on the .** sql
19017 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
19018 2e 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49  . Otherwise SQLI
19019 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45  TE_OK..*/.SQLITE
1901a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1901b 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c  ite3BtreeSchemaL
1901c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b  ocked(Btree *p){
1901d 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73  .  int rc;.  ass
1901e 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1901f 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
19020 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
19021 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
19022 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61  .  rc = querySha
19023 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
19024 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54  k(p, MASTER_ROOT
19025 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20  , READ_LOCK);.  
19026 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
19027 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c  TE_OK || rc==SQL
19028 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45  ITE_LOCKED_SHARE
19029 44 43 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69  DCACHE );.  sqli
1902a 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
1902b 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1902c 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1902d 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
1902e 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e  CHE./*.** Obtain
1902f 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74   a lock on the t
19030 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20  able whose root 
19031 70 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54  page is iTab.  T
19032 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20  he.** lock is a 
19033 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73  write lock if is
19034 57 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75  Writelock is tru
19035 65 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b  e or a read lock
19036 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c  .** if it is fal
19037 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
19038 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19039 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28  3BtreeLockTable(
1903a 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54  Btree *p, int iT
1903b 61 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f  ab, u8 isWriteLo
1903c 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ck){.  int rc = 
1903d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73  SQLITE_OK;.  ass
1903e 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21  ert( p->inTrans!
1903f 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20  =TRANS_NONE );. 
19040 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65   if( p->sharable
19041 20 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 6b 54   ){.    u8 lockT
19042 79 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 20  ype = READ_LOCK 
19043 2b 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20  + isWriteLock;. 
19044 20 20 20 61 73 73 65 72 74 28 20 52 45 41 44 5f     assert( READ_
19045 4c 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f  LOCK+1==WRITE_LO
19046 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  CK );.    assert
19047 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30  ( isWriteLock==0
19048 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d   || isWriteLock=
19049 3d 31 20 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74  =1 );..    sqlit
1904a 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
1904b 0a 20 20 20 20 72 63 20 3d 20 71 75 65 72 79 53  .    rc = queryS
1904c 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
1904d 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63  ock(p, iTab, loc
1904e 6b 54 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20  kType);.    if( 
1904f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
19050 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 74 53  .      rc = setS
19051 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
19052 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63  ock(p, iTab, loc
19053 6b 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20  kType);.    }.  
19054 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
19055 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65  ave(p);.  }.  re
19056 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
19057 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
19058 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
19059 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70  /*.** Argument p
1905a 43 73 72 20 6d 75 73 74 20 62 65 20 61 20 63 75  Csr must be a cu
1905b 72 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f 72 20  rsor opened for 
1905c 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a  writing on an .*
1905d 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 20 63  * INTKEY table c
1905e 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
1905f 67 20 61 74 20 61 20 76 61 6c 69 64 20 74 61 62  g at a valid tab
19060 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 54 68  le entry. .** Th
19061 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64 69  is function modi
19062 66 69 65 73 20 74 68 65 20 64 61 74 61 20 73 74  fies the data st
19063 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  ored as part of 
19064 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a  that entry..**.*
19065 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20  * Only the data 
19066 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79  content may only
19067 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74   be modified, it
19068 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
19069 20 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 65 20 74   to .** change t
1906a 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
1906b 20 64 61 74 61 20 73 74 6f 72 65 64 2e 20 49 66   data stored. If
1906c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
1906d 73 20 63 61 6c 6c 65 64 20 77 69 74 68 0a 2a 2a  s called with.**
1906e 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74   parameters that
1906f 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74   attempt to writ
19070 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
19071 66 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 64  f the existing d
19072 61 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f 64 69 66  ata,.** no modif
19073 69 63 61 74 69 6f 6e 73 20 61 72 65 20 6d 61 64  ications are mad
19074 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 43 4f 52  e and SQLITE_COR
19075 52 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64  RUPT is returned
19076 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19077 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
19078 74 72 65 65 50 75 74 44 61 74 61 28 42 74 43 75  treePutData(BtCu
19079 72 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 32 20  rsor *pCsr, u32 
1907a 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c  offset, u32 amt,
1907b 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 6e 74   void *z){.  int
1907c 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63   rc;.  assert( c
1907d 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
1907e 70 43 73 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCsr) );.  asser
1907f 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
19080 5f 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 74 72  _held(pCsr->pBtr
19081 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ee->db->mutex) )
19082 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72  ;.  assert( pCsr
19083 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64  ->isIncrblobHand
19084 6c 65 20 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65  le );..  rc = re
19085 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
19086 69 6f 6e 28 70 43 73 72 29 3b 0a 20 20 69 66 28  ion(pCsr);.  if(
19087 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
19088 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
19089 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
1908a 43 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52  Csr->eState!=CUR
1908b 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20  SOR_REQUIRESEEK 
1908c 29 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65  );.  if( pCsr->e
1908d 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41  State!=CURSOR_VA
1908e 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72  LID ){.    retur
1908f 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a  n SQLITE_ABORT;.
19090 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
19091 73 6f 6d 65 20 61 73 73 75 6d 70 74 69 6f 6e 73  some assumptions
19092 3a 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68  : .  **   (a) th
19093 65 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e  e cursor is open
19094 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20   for writing,.  
19095 2a 2a 20 20 20 28 62 29 20 74 68 65 72 65 20 69  **   (b) there i
19096 73 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 74  s a read/write t
19097 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c  ransaction open,
19098 0a 20 20 2a 2a 20 20 20 28 63 29 20 74 68 65 20  .  **   (c) the 
19099 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 73  connection holds
1909a 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e   a write-lock on
1909b 20 74 68 65 20 74 61 62 6c 65 20 28 69 66 20 72   the table (if r
1909c 65 71 75 69 72 65 64 29 2c 0a 20 20 2a 2a 20 20  equired),.  **  
1909d 20 28 64 29 20 74 68 65 72 65 20 61 72 65 20 6e   (d) there are n
1909e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65  o conflicting re
1909f 61 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 0a 20 20  ad-locks, and.  
190a0 2a 2a 20 20 20 28 65 29 20 74 68 65 20 63 75 72  **   (e) the cur
190a1 73 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20  sor points at a 
190a2 76 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20  valid row of an 
190a3 69 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20  intKey table..  
190a4 2a 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e  */.  if( !pCsr->
190a5 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65  wrFlag ){.    re
190a6 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44  turn SQLITE_READ
190a7 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65  ONLY;.  }.  asse
190a8 72 74 28 20 21 70 43 73 72 2d 3e 70 42 74 2d 3e  rt( !pCsr->pBt->
190a9 72 65 61 64 4f 6e 6c 79 20 26 26 20 70 43 73 72  readOnly && pCsr
190aa 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ->pBt->inTransac
190ab 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54  tion==TRANS_WRIT
190ac 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68  E );.  assert( h
190ad 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62  asSharedCacheTab
190ae 6c 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70 42 74  leLock(pCsr->pBt
190af 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52  ree, pCsr->pgnoR
190b0 6f 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0a 20 20  oot, 0, 2) );.  
190b1 61 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64  assert( !hasRead
190b2 43 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 2d 3e  Conflicts(pCsr->
190b3 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67  pBtree, pCsr->pg
190b4 6e 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 61 73 73  noRoot) );.  ass
190b5 65 72 74 28 20 70 43 73 72 2d 3e 61 70 50 61 67  ert( pCsr->apPag
190b6 65 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCsr->iPage]->
190b7 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 72 65 74  intKey );..  ret
190b8 75 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61  urn accessPayloa
190b9 64 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20  d(pCsr, offset, 
190ba 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63  amt, (unsigned c
190bb 68 61 72 20 2a 29 7a 2c 20 31 29 3b 0a 7d 0a 0a  har *)z, 1);.}..
190bc 2f 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61  /* .** Set a fla
190bd 67 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72  g on this cursor
190be 20 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f   to cache the lo
190bf 63 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73  cations of pages
190c0 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76   from the .** ov
190c1 65 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20  erflow list for 
190c2 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e  the current row.
190c3 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
190c4 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a   cursors opened.
190c5 2a 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74  ** for increment
190c6 61 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e  al blob IO only.
190c7 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
190c8 74 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67  tion sets a flag
190c9 20 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61   only. The actua
190ca 6c 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20  l page location 
190cb 63 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64  cache.** (stored
190cc 20 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76   in BtCursor.aOv
190cd 65 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c  erflow[]) is all
190ce 6f 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20  ocated and used 
190cf 62 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61  by function.** a
190d0 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28  ccessPayload() (
190d1 74 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74  the worker funct
190d2 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42  ion for sqlite3B
190d3 74 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a  treeData() and.*
190d4 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75  * sqlite3BtreePu
190d5 74 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c  tData())..*/.SQL
190d6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
190d7 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63   sqlite3BtreeCac
190d8 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72  heOverflow(BtCur
190d9 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73  sor *pCur){.  as
190da 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
190db 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
190dc 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
190dd 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75  3_mutex_held(pCu
190de 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d  r->pBtree->db->m
190df 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
190e0 74 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62  t(!pCur->isIncrb
190e1 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73  lobHandle);.  as
190e2 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65  sert(!pCur->aOve
190e3 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e  rflow);.  pCur->
190e4 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65  isIncrblobHandle
190e5 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a   = 1;.}.#endif..
190e6 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
190e7 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a  End of btree.c *
190e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
190eb 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
190ec 42 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75  Begin file backu
190ed 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  p.c ************
190ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
190f0 2f 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61  /*.** 2009 Janua
190f1 72 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ry 28.**.** The 
190f2 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
190f3 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
190f4 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
190f5 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
190f6 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
190f7 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
190f8 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
190f9 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
190fa 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
190fb 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
190fc 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
190fd 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
190fe 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
190ff 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
19100 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
19101 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
19102 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
19103 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19104 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19105 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19106 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19107 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
19108 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69  e contains the i
19109 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
1910a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63   the sqlite3_bac
1910b 6b 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50  kup_XXX() .** AP
1910c 49 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20  I functions and 
1910d 74 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74  the related feat
1910e 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  ures..**.** $Id:
1910f 20 62 61 63 6b 75 70 2e 63 2c 76 20 31 2e 31 39   backup.c,v 1.19
19110 20 32 30 30 39 2f 30 37 2f 30 36 20 31 39 3a 30   2009/07/06 19:0
19111 33 3a 31 33 20 64 72 68 20 45 78 70 20 24 0a 2a  3:13 drh Exp $.*
19112 2f 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66  /../* Macro to f
19113 69 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20  ind the minimum 
19114 6f 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76  of two numeric v
19115 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65  alues..*/.#ifnde
19116 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d  f MIN.# define M
19117 49 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29  IN(x,y) ((x)<(y)
19118 3f 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66  ?(x):(y)).#endif
19119 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72  ../*.** Structur
1911a 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  e allocated for 
1911b 65 61 63 68 20 62 61 63 6b 75 70 20 6f 70 65 72  each backup oper
1911c 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ation..*/.struct
1911d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
1911e 7b 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65  {.  sqlite3* pDe
1911f 73 74 44 62 3b 20 20 20 20 20 20 20 20 2f 2a 20  stDb;        /* 
19120 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  Destination data
19121 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
19122 20 42 74 72 65 65 20 2a 70 44 65 73 74 3b 20 20   Btree *pDest;  
19123 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73            /* Des
19124 74 69 6e 61 74 69 6f 6e 20 62 2d 74 72 65 65 20  tination b-tree 
19125 66 69 6c 65 20 2a 2f 0a 20 20 75 33 32 20 69 44  file */.  u32 iD
19126 65 73 74 53 63 68 65 6d 61 3b 20 20 20 20 20 20  estSchema;      
19127 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 73     /* Original s
19128 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 6e 20  chema cookie in 
19129 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20  destination */. 
1912a 20 69 6e 74 20 62 44 65 73 74 4c 6f 63 6b 65 64   int bDestLocked
1912b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
1912c 65 20 6f 6e 63 65 20 61 20 77 72 69 74 65 2d 74  e once a write-t
1912d 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70  ransaction is op
1912e 65 6e 20 6f 6e 20 70 44 65 73 74 20 2a 2f 0a 0a  en on pDest */..
1912f 20 20 50 67 6e 6f 20 69 4e 65 78 74 3b 20 20 20    Pgno iNext;   
19130 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
19131 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
19132 20 6e 65 78 74 20 73 6f 75 72 63 65 20 70 61 67   next source pag
19133 65 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 73  e to copy */.  s
19134 71 6c 69 74 65 33 2a 20 70 53 72 63 44 62 3b 20  qlite3* pSrcDb; 
19135 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63          /* Sourc
19136 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
19137 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 53  e */.  Btree *pS
19138 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
19139 2f 2a 20 53 6f 75 72 63 65 20 62 2d 74 72 65 65  /* Source b-tree
1913a 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 69 6e 74 20   file */..  int 
1913b 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
1913c 20 20 20 20 20 2f 2a 20 42 61 63 6b 75 70 20 70       /* Backup p
1913d 72 6f 63 65 73 73 20 65 72 72 6f 72 20 63 6f 64  rocess error cod
1913e 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 73 65  e */..  /* These
1913f 20 74 77 6f 20 76 61 72 69 61 62 6c 65 73 20 61   two variables a
19140 72 65 20 73 65 74 20 62 79 20 65 76 65 72 79 20  re set by every 
19141 63 61 6c 6c 20 74 6f 20 62 61 63 6b 75 70 5f 73  call to backup_s
19142 74 65 70 28 29 2e 20 54 68 65 79 20 61 72 65 0a  tep(). They are.
19143 20 20 2a 2a 20 72 65 61 64 20 62 79 20 63 61 6c    ** read by cal
19144 6c 73 20 74 6f 20 62 61 63 6b 75 70 5f 72 65 6d  ls to backup_rem
19145 61 69 6e 69 6e 67 28 29 20 61 6e 64 20 62 61 63  aining() and bac
19146 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e  kup_pagecount().
19147 0a 20 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 52 65  .  */.  Pgno nRe
19148 6d 61 69 6e 69 6e 67 3b 20 20 20 20 20 20 20 20  maining;        
19149 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
1914a 67 65 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79  ges left to copy
1914b 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65   */.  Pgno nPage
1914c 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 2f  count;         /
1914d 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
1914e 66 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20  f pages to copy 
1914f 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 41 74 74 61  */..  int isAtta
19150 63 68 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f  ched;          /
19151 2a 20 54 72 75 65 20 6f 6e 63 65 20 62 61 63 6b  * True once back
19152 75 70 20 68 61 73 20 62 65 65 6e 20 72 65 67 69  up has been regi
19153 73 74 65 72 65 64 20 77 69 74 68 20 70 61 67 65  stered with page
19154 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62  r */.  sqlite3_b
19155 61 63 6b 75 70 20 2a 70 4e 65 78 74 3b 20 20 20  ackup *pNext;   
19156 2f 2a 20 4e 65 78 74 20 62 61 63 6b 75 70 20 61  /* Next backup a
19157 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73  ssociated with s
19158 6f 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0a 7d  ource pager */.}
19159 3b 0a 0a 2f 2a 0a 2a 2a 20 54 48 52 45 41 44 20  ;../*.** THREAD 
1915a 53 41 46 45 54 59 20 4e 4f 54 45 53 3a 0a 2a 2a  SAFETY NOTES:.**
1915b 0a 2a 2a 20 20 20 4f 6e 63 65 20 69 74 20 68 61  .**   Once it ha
1915c 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 75  s been created u
1915d 73 69 6e 67 20 62 61 63 6b 75 70 5f 69 6e 69 74  sing backup_init
1915e 28 29 2c 20 61 20 73 69 6e 67 6c 65 20 73 71 6c  (), a single sql
1915f 69 74 65 33 5f 62 61 63 6b 75 70 0a 2a 2a 20 20  ite3_backup.**  
19160 20 73 74 72 75 63 74 75 72 65 20 6d 61 79 20 62   structure may b
19161 65 20 61 63 63 65 73 73 65 64 20 76 69 61 20 74  e accessed via t
19162 77 6f 20 67 72 6f 75 70 73 20 6f 66 20 74 68 72  wo groups of thr
19163 65 61 64 2d 73 61 66 65 20 65 6e 74 72 79 20 70  ead-safe entry p
19164 6f 69 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  oints:.**.**    
19165 20 2a 20 56 69 61 20 74 68 65 20 73 71 6c 69 74   * Via the sqlit
19166 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20  e3_backup_XXX() 
19167 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 62 61 63  API function bac
19168 6b 75 70 5f 73 74 65 70 28 29 20 61 6e 64 20 0a  kup_step() and .
19169 2a 2a 20 20 20 20 20 20 20 62 61 63 6b 75 70 5f  **       backup_
1916a 66 69 6e 69 73 68 28 29 2e 20 42 6f 74 68 20 74  finish(). Both t
1916b 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 6f  hese functions o
1916c 62 74 61 69 6e 20 74 68 65 20 73 6f 75 72 63 65  btain the source
1916d 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20   database.**    
1916e 20 20 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 20     handle mutex 
1916f 61 6e 64 20 74 68 65 20 6d 75 74 65 78 20 61 73  and the mutex as
19170 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
19171 65 20 73 6f 75 72 63 65 20 42 74 53 68 61 72 65  e source BtShare
19172 64 20 0a 2a 2a 20 20 20 20 20 20 20 73 74 72 75  d .**       stru
19173 63 74 75 72 65 2c 20 69 6e 20 74 68 61 74 20 6f  cture, in that o
19174 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  rder..**.**     
19175 2a 20 56 69 61 20 74 68 65 20 42 61 63 6b 75 70  * Via the Backup
19176 55 70 64 61 74 65 28 29 20 61 6e 64 20 42 61 63  Update() and Bac
19177 6b 75 70 52 65 73 74 61 72 74 28 29 20 66 75 6e  kupRestart() fun
19178 63 74 69 6f 6e 73 2c 20 77 68 69 63 68 20 61 72  ctions, which ar
19179 65 0a 2a 2a 20 20 20 20 20 20 20 69 6e 76 6f 6b  e.**       invok
1917a 65 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20  ed by the pager 
1917b 6c 61 79 65 72 20 74 6f 20 72 65 70 6f 72 74 20  layer to report 
1917c 76 61 72 69 6f 75 73 20 73 74 61 74 65 20 63 68  various state ch
1917d 61 6e 67 65 73 20 69 6e 0a 2a 2a 20 20 20 20 20  anges in.**     
1917e 20 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65    the page cache
1917f 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
19180 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
19181 62 61 73 65 2e 20 54 68 65 20 6d 75 74 65 78 0a  base. The mutex.
19182 2a 2a 20 20 20 20 20 20 20 61 73 73 6f 63 69 61  **       associa
19183 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75  ted with the sou
19184 72 63 65 20 64 61 74 61 62 61 73 65 20 42 74 53  rce database BtS
19185 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20  hared structure 
19186 77 69 6c 6c 20 61 6c 77 61 79 73 20 0a 2a 2a 20  will always .** 
19187 20 20 20 20 20 20 62 65 20 68 65 6c 64 20 77 68        be held wh
19188 65 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65  en either of the
19189 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  se functions are
1918a 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20   invoked..**.** 
1918b 20 20 54 68 65 20 6f 74 68 65 72 20 73 71 6c 69    The other sqli
1918c 74 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29  te3_backup_XXX()
1918d 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 2c 20   API functions, 
1918e 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67  backup_remaining
1918f 28 29 20 61 6e 64 0a 2a 2a 20 20 20 62 61 63 6b  () and.**   back
19190 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 20 61  up_pagecount() a
19191 72 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61  re not thread-sa
19192 66 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49 66  fe functions. If
19193 20 74 68 65 79 20 61 72 65 20 63 61 6c 6c 65 64   they are called
19194 0a 2a 2a 20 20 20 77 68 69 6c 65 20 73 6f 6d 65  .**   while some
19195 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73   other thread is
19196 20 63 61 6c 6c 69 6e 67 20 62 61 63 6b 75 70 5f   calling backup_
19197 73 74 65 70 28 29 20 6f 72 20 62 61 63 6b 75 70  step() or backup
19198 5f 66 69 6e 69 73 68 28 29 2c 0a 2a 2a 20 20 20  _finish(),.**   
19199 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72  the values retur
1919a 6e 65 64 20 6d 61 79 20 62 65 20 69 6e 76 61 6c  ned may be inval
1919b 69 64 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20  id. There is no 
1919c 77 61 79 20 66 6f 72 20 61 20 63 61 6c 6c 20 74  way for a call t
1919d 6f 0a 2a 2a 20 20 20 42 61 63 6b 75 70 55 70 64  o.**   BackupUpd
1919e 61 74 65 28 29 20 6f 72 20 42 61 63 6b 75 70 52  ate() or BackupR
1919f 65 73 74 61 72 74 28 29 20 74 6f 20 69 6e 74 65  estart() to inte
191a0 72 66 65 72 65 20 77 69 74 68 20 62 61 63 6b 75  rfere with backu
191a1 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 0a 2a 2a  p_remaining().**
191a2 20 20 20 6f 72 20 62 61 63 6b 75 70 5f 70 61 67     or backup_pag
191a3 65 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20  ecount()..**.** 
191a4 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74    Depending on t
191a5 68 65 20 53 51 4c 69 74 65 20 63 6f 6e 66 69 67  he SQLite config
191a6 75 72 61 74 69 6f 6e 2c 20 74 68 65 20 64 61 74  uration, the dat
191a7 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 61 6e  abase handles an
191a8 64 2f 6f 72 0a 2a 2a 20 20 20 74 68 65 20 42 74  d/or.**   the Bt
191a9 72 65 65 20 6f 62 6a 65 63 74 73 20 6d 61 79 20  ree objects may 
191aa 68 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 6d  have their own m
191ab 75 74 65 78 65 73 20 74 68 61 74 20 72 65 71 75  utexes that requ
191ac 69 72 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20  ire locking..** 
191ad 20 20 4e 6f 6e 2d 73 68 61 72 61 62 6c 65 20 42    Non-sharable B
191ae 74 72 65 65 73 20 28 69 6e 2d 6d 65 6d 6f 72 79  trees (in-memory
191af 20 64 61 74 61 62 61 73 65 73 20 66 6f 72 20 65   databases for e
191b0 78 61 6d 70 6c 65 29 2c 20 64 6f 20 6e 6f 74 20  xample), do not 
191b1 68 61 76 65 0a 2a 2a 20 20 20 61 73 73 6f 63 69  have.**   associ
191b2 61 74 65 64 20 6d 75 74 65 78 65 73 2e 0a 2a 2f  ated mutexes..*/
191b3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
191b4 20 70 6f 69 6e 74 65 72 20 63 6f 72 72 65 73 70   pointer corresp
191b5 6f 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61  onding to databa
191b6 73 65 20 7a 44 62 20 28 69 2e 65 2e 20 22 6d 61  se zDb (i.e. "ma
191b7 69 6e 22 2c 20 22 74 65 6d 70 22 29 0a 2a 2a 20  in", "temp").** 
191b8 69 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  in connection ha
191b9 6e 64 6c 65 20 70 44 62 2e 20 49 66 20 73 75 63  ndle pDb. If suc
191ba 68 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e  h a database can
191bb 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 72 65  not be found, re
191bc 74 75 72 6e 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70  turn.** a NULL p
191bd 6f 69 6e 74 65 72 20 61 6e 64 20 77 72 69 74 65  ointer and write
191be 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
191bf 65 20 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a  e to pErrorDb..*
191c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 74 65 6d  *.** If the "tem
191c1 70 22 20 64 61 74 61 62 61 73 65 20 69 73 20 72  p" database is r
191c2 65 71 75 65 73 74 65 64 2c 20 69 74 20 6d 61 79  equested, it may
191c3 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65 6e   need to be open
191c4 65 64 20 62 79 20 74 68 69 73 20 0a 2a 2a 20 66  ed by this .** f
191c5 75 6e 63 74 69 6f 6e 2e 20 49 66 20 61 6e 20 65  unction. If an e
191c6 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
191c7 65 20 64 6f 69 6e 67 20 73 6f 2c 20 72 65 74 75  e doing so, retu
191c8 72 6e 20 30 20 61 6e 64 20 77 72 69 74 65 20 61  rn 0 and write a
191c9 6e 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73  n .** error mess
191ca 61 67 65 20 74 6f 20 70 45 72 72 6f 72 44 62 2e  age to pErrorDb.
191cb 0a 2a 2f 0a 73 74 61 74 69 63 20 42 74 72 65 65  .*/.static Btree
191cc 20 2a 66 69 6e 64 42 74 72 65 65 28 73 71 6c 69   *findBtree(sqli
191cd 74 65 33 20 2a 70 45 72 72 6f 72 44 62 2c 20 73  te3 *pErrorDb, s
191ce 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 63 6f 6e  qlite3 *pDb, con
191cf 73 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20  st char *zDb){. 
191d0 20 69 6e 74 20 69 20 3d 20 73 71 6c 69 74 65 33   int i = sqlite3
191d1 46 69 6e 64 44 62 4e 61 6d 65 28 70 44 62 2c 20  FindDbName(pDb, 
191d2 7a 44 62 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d  zDb);..  if( i==
191d3 31 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20 2a  1 ){.    Parse *
191d4 70 50 61 72 73 65 3b 0a 20 20 20 20 69 6e 74 20  pParse;.    int 
191d5 72 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72  rc = 0;.    pPar
191d6 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63  se = sqlite3Stac
191d7 6b 41 6c 6c 6f 63 5a 65 72 6f 28 70 45 72 72 6f  kAllocZero(pErro
191d8 72 44 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61  rDb, sizeof(*pPa
191d9 72 73 65 29 29 3b 0a 20 20 20 20 69 66 28 20 70  rse));.    if( p
191da 50 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20  Parse==0 ){.    
191db 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70    sqlite3Error(p
191dc 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 54 45 5f  ErrorDb, SQLITE_
191dd 4e 4f 4d 45 4d 2c 20 22 6f 75 74 20 6f 66 20 6d  NOMEM, "out of m
191de 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 72  emory");.      r
191df 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
191e0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
191e1 20 20 20 70 50 61 72 73 65 2d 3e 64 62 20 3d 20     pParse->db = 
191e2 70 44 62 3b 0a 20 20 20 20 20 20 69 66 28 20 73  pDb;.      if( s
191e3 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61  qlite3OpenTempDa
191e4 74 61 62 61 73 65 28 70 50 61 72 73 65 29 20 29  tabase(pParse) )
191e5 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
191e6 33 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72  3ErrorClear(pPar
191e7 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  se);.        sql
191e8 69 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72  ite3Error(pError
191e9 44 62 2c 20 70 50 61 72 73 65 2d 3e 72 63 2c 20  Db, pParse->rc, 
191ea 22 25 73 22 2c 20 70 50 61 72 73 65 2d 3e 7a 45  "%s", pParse->zE
191eb 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20  rrMsg);.        
191ec 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
191ed 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  R;.      }.     
191ee 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
191ef 65 28 70 45 72 72 6f 72 44 62 2c 20 70 50 61 72  e(pErrorDb, pPar
191f0 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  se);.    }.    i
191f1 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
191f2 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
191f3 20 7d 0a 0a 20 20 69 66 28 20 69 3c 30 20 29 7b   }..  if( i<0 ){
191f4 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
191f5 72 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49  r(pErrorDb, SQLI
191f6 54 45 5f 45 52 52 4f 52 2c 20 22 75 6e 6b 6e 6f  TE_ERROR, "unkno
191f7 77 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c  wn database %s",
191f8 20 7a 44 62 29 3b 0a 20 20 20 20 72 65 74 75 72   zDb);.    retur
191f9 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  n 0;.  }..  retu
191fa 72 6e 20 70 44 62 2d 3e 61 44 62 5b 69 5d 2e 70  rn pDb->aDb[i].p
191fb 42 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  Bt;.}../*.** Cre
191fc 61 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 62  ate an sqlite3_b
191fd 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20 74 6f  ackup process to
191fe 20 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e   copy the conten
191ff 74 73 20 6f 66 20 7a 53 72 63 44 62 20 66 72 6f  ts of zSrcDb fro
19200 6d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  m.** connection 
19201 68 61 6e 64 6c 65 20 70 53 72 63 44 62 20 74 6f  handle pSrcDb to
19202 20 7a 44 65 73 74 44 62 20 69 6e 20 70 44 65 73   zDestDb in pDes
19203 74 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66  tDb. If successf
19204 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  ul, return.** a 
19205 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
19206 65 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ew sqlite3_backu
19207 70 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  p object..**.** 
19208 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
19209 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  rs, NULL is retu
1920a 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f  rned and an erro
1920b 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72  r code and error
1920c 20 6d 65 73 73 61 67 65 0a 2a 2a 20 73 74 6f 72   message.** stor
1920d 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 68  ed in database h
1920e 61 6e 64 6c 65 20 70 44 65 73 74 44 62 2e 0a 2a  andle pDestDb..*
1920f 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
19210 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c  ite3_backup *sql
19211 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74  ite3_backup_init
19212 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65  (.  sqlite3* pDe
19213 73 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20  stDb,           
19214 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
19215 61 62 61 73 65 20 74 6f 20 77 72 69 74 65 20 74  abase to write t
19216 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  o */.  const cha
19217 72 20 2a 7a 44 65 73 74 44 62 2c 20 20 20 20 20  r *zDestDb,     
19218 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19219 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65  Name of database
1921a 20 77 69 74 68 69 6e 20 70 44 65 73 74 44 62 20   within pDestDb 
1921b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53  */.  sqlite3* pS
1921c 72 63 44 62 2c 20 20 20 20 20 20 20 20 20 20 20  rcDb,           
1921d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
1921e 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1921f 6e 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a  n to read from *
19220 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
19221 7a 53 72 63 44 62 20 20 20 20 20 20 20 20 20 20  zSrcDb          
19222 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
19223 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 77 69  e of database wi
19224 74 68 69 6e 20 70 53 72 63 44 62 20 2a 2f 0a 29  thin pSrcDb */.)
19225 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  {.  sqlite3_back
19226 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  up *p;          
19227 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
19228 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  ue to return */.
19229 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 73  .  /* Lock the s
1922a 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68  ource database h
1922b 61 6e 64 6c 65 2e 20 54 68 65 20 64 65 73 74 69  andle. The desti
1922c 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 0a  nation database.
1922d 20 20 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 6e    ** handle is n
1922e 6f 74 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68 69  ot locked in thi
1922f 73 20 72 6f 75 74 69 6e 65 2c 20 62 75 74 20 69  s routine, but i
19230 74 20 69 73 20 6c 6f 63 6b 65 64 20 69 6e 0a 20  t is locked in. 
19231 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   ** sqlite3_back
19232 75 70 5f 73 74 65 70 28 29 2e 20 54 68 65 20 75  up_step(). The u
19233 73 65 72 20 69 73 20 72 65 71 75 69 72 65 64 20  ser is required 
19234 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 6e  to ensure that n
19235 6f 0a 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 72  o.  ** other thr
19236 65 61 64 20 61 63 63 65 73 73 65 73 20 74 68 65  ead accesses the
19237 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61 6e   destination han
19238 64 6c 65 20 66 6f 72 20 74 68 65 20 64 75 72 61  dle for the dura
19239 74 69 6f 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65  tion.  ** of the
1923a 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
1923b 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20  n.  Any attempt 
1923c 74 6f 20 75 73 65 20 74 68 65 20 64 65 73 74 69  to use the desti
1923d 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 61 74 61  nation.  ** data
1923e 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1923f 77 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69  while a backup i
19240 73 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 61  s in progress ma
19241 79 20 63 61 75 73 65 0a 20 20 2a 2a 20 61 20 6d  y cause.  ** a m
19242 61 6c 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 20  alfunction or a 
19243 64 65 61 64 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20  deadlock..  */. 
19244 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
19245 6e 74 65 72 28 70 53 72 63 44 62 2d 3e 6d 75 74  nter(pSrcDb->mut
19246 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ex);.  sqlite3_m
19247 75 74 65 78 5f 65 6e 74 65 72 28 70 44 65 73 74  utex_enter(pDest
19248 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 69  Db->mutex);..  i
19249 66 28 20 70 53 72 63 44 62 3d 3d 70 44 65 73 74  f( pSrcDb==pDest
1924a 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Db ){.    sqlite
1924b 33 45 72 72 6f 72 28 0a 20 20 20 20 20 20 20 20  3Error(.        
1924c 70 44 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f  pDestDb, SQLITE_
1924d 45 52 52 4f 52 2c 20 22 73 6f 75 72 63 65 20 61  ERROR, "source a
1924e 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d  nd destination m
1924f 75 73 74 20 62 65 20 64 69 73 74 69 6e 63 74 22  ust be distinct"
19250 0a 20 20 20 20 29 3b 0a 20 20 20 20 70 20 3d 20  .    );.    p = 
19251 30 3b 0a 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20  0;.  }else {.   
19252 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61   /* Allocate spa
19253 63 65 20 66 6f 72 20 61 20 6e 65 77 20 73 71 6c  ce for a new sql
19254 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65  ite3_backup obje
19255 63 74 20 2a 2f 0a 20 20 20 20 70 20 3d 20 28 73  ct */.    p = (s
19256 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29  qlite3_backup *)
19257 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
19258 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61  izeof(sqlite3_ba
19259 63 6b 75 70 29 29 3b 0a 20 20 20 20 69 66 28 20  ckup));.    if( 
1925a 21 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  !p ){.      sqli
1925b 74 65 33 45 72 72 6f 72 28 70 44 65 73 74 44 62  te3Error(pDestDb
1925c 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20  , SQLITE_NOMEM, 
1925d 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  0);.    }.  }.. 
1925e 20 2f 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63   /* If the alloc
1925f 61 74 69 6f 6e 20 73 75 63 63 65 65 64 65 64 2c  ation succeeded,
19260 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65   populate the ne
19261 77 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 69  w object. */.  i
19262 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73  f( p ){.    mems
19263 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28  et(p, 0, sizeof(
19264 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 29  sqlite3_backup))
19265 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20  ;.    p->pSrc = 
19266 66 69 6e 64 42 74 72 65 65 28 70 44 65 73 74 44  findBtree(pDestD
19267 62 2c 20 70 53 72 63 44 62 2c 20 7a 53 72 63 44  b, pSrcDb, zSrcD
19268 62 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74  b);.    p->pDest
19269 20 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 65   = findBtree(pDe
1926a 73 74 44 62 2c 20 70 44 65 73 74 44 62 2c 20 7a  stDb, pDestDb, z
1926b 44 65 73 74 44 62 29 3b 0a 20 20 20 20 70 2d 3e  DestDb);.    p->
1926c 70 44 65 73 74 44 62 20 3d 20 70 44 65 73 74 44  pDestDb = pDestD
1926d 62 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 44 62  b;.    p->pSrcDb
1926e 20 3d 20 70 53 72 63 44 62 3b 0a 20 20 20 20 70   = pSrcDb;.    p
1926f 2d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 20  ->iNext = 1;.   
19270 20 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 3d   p->isAttached =
19271 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d   0;..    if( 0==
19272 70 2d 3e 70 53 72 63 20 7c 7c 20 30 3d 3d 70 2d  p->pSrc || 0==p-
19273 3e 70 44 65 73 74 20 29 7b 0a 20 20 20 20 20 20  >pDest ){.      
19274 2f 2a 20 4f 6e 65 20 28 6f 72 20 62 6f 74 68 29  /* One (or both)
19275 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61   of the named da
19276 74 61 62 61 73 65 73 20 64 69 64 20 6e 6f 74 20  tabases did not 
19277 65 78 69 73 74 2e 20 41 6e 20 65 72 72 6f 72 20  exist. An error 
19278 68 61 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72  has.      ** alr
19279 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65  eady been writte
1927a 6e 20 69 6e 74 6f 20 74 68 65 20 70 44 65 73 74  n into the pDest
1927b 44 62 20 68 61 6e 64 6c 65 2e 20 41 6c 6c 20 74  Db handle. All t
1927c 68 61 74 20 69 73 20 6c 65 66 74 0a 20 20 20 20  hat is left.    
1927d 20 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20    ** to do here 
1927e 69 73 20 66 72 65 65 20 74 68 65 20 73 71 6c 69  is free the sqli
1927f 74 65 33 5f 62 61 63 6b 75 70 20 73 74 72 75 63  te3_backup struc
19280 74 75 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ture..      */. 
19281 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
19282 65 28 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20  e(p);.      p = 
19283 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  0;.    }.  }.  i
19284 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 70  f( p ){.    p->p
19285 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2b 2b 3b 0a  Src->nBackup++;.
19286 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d    }..  sqlite3_m
19287 75 74 65 78 5f 6c 65 61 76 65 28 70 44 65 73 74  utex_leave(pDest
19288 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71  Db->mutex);.  sq
19289 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1928a 65 28 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29  e(pSrcDb->mutex)
1928b 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
1928c 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  ./*.** Argument 
1928d 72 63 20 69 73 20 61 6e 20 53 51 4c 69 74 65 20  rc is an SQLite 
1928e 65 72 72 6f 72 20 63 6f 64 65 2e 20 52 65 74 75  error code. Retu
1928f 72 6e 20 74 72 75 65 20 69 66 20 74 68 69 73 20  rn true if this 
19290 65 72 72 6f 72 20 69 73 20 0a 2a 2a 20 63 6f 6e  error is .** con
19291 73 69 64 65 72 65 64 20 66 61 74 61 6c 20 69 66  sidered fatal if
19292 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 64 75 72   encountered dur
19293 69 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65  ing a backup ope
19294 72 61 74 69 6f 6e 2e 20 41 6c 6c 20 65 72 72 6f  ration. All erro
19295 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64  rs.** are consid
19296 65 72 65 64 20 66 61 74 61 6c 20 65 78 63 65 70  ered fatal excep
19297 74 20 66 6f 72 20 53 51 4c 49 54 45 5f 42 55 53  t for SQLITE_BUS
19298 59 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c 4f 43  Y and SQLITE_LOC
19299 4b 45 44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  KED..*/.static i
1929a 6e 74 20 69 73 46 61 74 61 6c 45 72 72 6f 72 28  nt isFatalError(
1929b 69 6e 74 20 72 63 29 7b 0a 20 20 72 65 74 75 72  int rc){.  retur
1929c 6e 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  n (rc!=SQLITE_OK
1929d 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42   && rc!=SQLITE_B
1929e 55 53 59 20 26 26 20 41 4c 57 41 59 53 28 72 63  USY && ALWAYS(rc
1929f 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29  !=SQLITE_LOCKED)
192a0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61  );.}../*.** Para
192a1 6d 65 74 65 72 20 7a 53 72 63 44 61 74 61 20 70  meter zSrcData p
192a2 6f 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65  oints to a buffe
192a3 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  r containing the
192a4 20 64 61 74 61 20 66 6f 72 20 0a 2a 2a 20 70 61   data for .** pa
192a5 67 65 20 69 53 72 63 50 67 20 66 72 6f 6d 20 74  ge iSrcPg from t
192a6 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
192a7 73 65 2e 20 43 6f 70 79 20 74 68 69 73 20 64 61  se. Copy this da
192a8 74 61 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20  ta into the .** 
192a9 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
192aa 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
192ab 69 6e 74 20 62 61 63 6b 75 70 4f 6e 65 50 61 67  int backupOnePag
192ac 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  e(sqlite3_backup
192ad 20 2a 70 2c 20 50 67 6e 6f 20 69 53 72 63 50 67   *p, Pgno iSrcPg
192ae 2c 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 72 63  , const u8 *zSrc
192af 44 61 74 61 29 7b 0a 20 20 50 61 67 65 72 20 2a  Data){.  Pager *
192b0 20 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65   const pDestPage
192b1 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
192b2 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b  Pager(p->pDest);
192b3 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72  .  const int nSr
192b4 63 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42  cPgsz = sqlite3B
192b5 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28  treeGetPageSize(
192b6 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 6e 74 20  p->pSrc);.  int 
192b7 6e 44 65 73 74 50 67 73 7a 20 3d 20 73 71 6c 69  nDestPgsz = sqli
192b8 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53  te3BtreeGetPageS
192b9 69 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20  ize(p->pDest);. 
192ba 20 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6f 70 79   const int nCopy
192bb 20 3d 20 4d 49 4e 28 6e 53 72 63 50 67 73 7a 2c   = MIN(nSrcPgsz,
192bc 20 6e 44 65 73 74 50 67 73 7a 29 3b 0a 20 20 63   nDestPgsz);.  c
192bd 6f 6e 73 74 20 69 36 34 20 69 45 6e 64 20 3d 20  onst i64 iEnd = 
192be 28 69 36 34 29 69 53 72 63 50 67 2a 28 69 36 34  (i64)iSrcPg*(i64
192bf 29 6e 53 72 63 50 67 73 7a 3b 0a 0a 20 20 69 6e  )nSrcPgsz;..  in
192c0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
192c1 3b 0a 20 20 69 36 34 20 69 4f 66 66 3b 0a 0a 20  ;.  i64 iOff;.. 
192c2 20 61 73 73 65 72 74 28 20 70 2d 3e 62 44 65 73   assert( p->bDes
192c3 74 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 61 73 73  tLocked );.  ass
192c4 65 72 74 28 20 21 69 73 46 61 74 61 6c 45 72 72  ert( !isFatalErr
192c5 6f 72 28 70 2d 3e 72 63 29 20 29 3b 0a 20 20 61  or(p->rc) );.  a
192c6 73 73 65 72 74 28 20 69 53 72 63 50 67 21 3d 50  ssert( iSrcPg!=P
192c7 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
192c8 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29  (p->pSrc->pBt) )
192c9 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72 63  ;.  assert( zSrc
192ca 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 43 61  Data );..  /* Ca
192cb 74 63 68 20 74 68 65 20 63 61 73 65 20 77 68 65  tch the case whe
192cc 72 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  re the destinati
192cd 6f 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f  on is an in-memo
192ce 72 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  ry database and 
192cf 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 73 69  the.  ** page si
192d0 7a 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72 63  zes of the sourc
192d1 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f  e and destinatio
192d2 6e 20 64 69 66 66 65 72 2e 20 0a 20 20 2a 2f 0a  n differ. .  */.
192d3 20 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21 3d    if( nSrcPgsz!=
192d4 6e 44 65 73 74 50 67 73 7a 20 26 26 20 73 71 6c  nDestPgsz && sql
192d5 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62  ite3PagerIsMemdb
192d6 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67  (sqlite3BtreePag
192d7 65 72 28 70 2d 3e 70 44 65 73 74 29 29 20 29 7b  er(p->pDest)) ){
192d8 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
192d9 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a  _READONLY;.  }..
192da 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72    /* This loop r
192db 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63  uns once for eac
192dc 68 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61  h destination pa
192dd 67 65 20 73 70 61 6e 6e 65 64 20 62 79 20 74 68  ge spanned by th
192de 65 20 73 6f 75 72 63 65 20 0a 20 20 2a 2a 20 70  e source .  ** p
192df 61 67 65 2e 20 46 6f 72 20 65 61 63 68 20 69 74  age. For each it
192e0 65 72 61 74 69 6f 6e 2c 20 76 61 72 69 61 62 6c  eration, variabl
192e1 65 20 69 4f 66 66 20 69 73 20 73 65 74 20 74 6f  e iOff is set to
192e2 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74   the byte offset
192e3 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 73  .  ** of the des
192e4 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20  tination page.. 
192e5 20 2a 2f 0a 20 20 66 6f 72 28 69 4f 66 66 3d 69   */.  for(iOff=i
192e6 45 6e 64 2d 28 69 36 34 29 6e 53 72 63 50 67 73  End-(i64)nSrcPgs
192e7 7a 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  z; rc==SQLITE_OK
192e8 20 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 69   && iOff<iEnd; i
192e9 4f 66 66 2b 3d 6e 44 65 73 74 50 67 73 7a 29 7b  Off+=nDestPgsz){
192ea 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 65  .    DbPage *pDe
192eb 73 74 50 67 20 3d 20 30 3b 0a 20 20 20 20 50 67  stPg = 0;.    Pg
192ec 6e 6f 20 69 44 65 73 74 20 3d 20 28 50 67 6e 6f  no iDest = (Pgno
192ed 29 28 69 4f 66 66 2f 6e 44 65 73 74 50 67 73 7a  )(iOff/nDestPgsz
192ee 29 2b 31 3b 0a 20 20 20 20 69 66 28 20 69 44 65  )+1;.    if( iDe
192ef 73 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  st==PENDING_BYTE
192f0 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e  _PAGE(p->pDest->
192f1 70 42 74 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  pBt) ) continue;
192f2 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
192f3 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK==(rc = sqlite
192f4 33 50 61 67 65 72 47 65 74 28 70 44 65 73 74 50  3PagerGet(pDestP
192f5 61 67 65 72 2c 20 69 44 65 73 74 2c 20 26 70 44  ager, iDest, &pD
192f6 65 73 74 50 67 29 29 0a 20 20 20 20 20 26 26 20  estPg)).     && 
192f7 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
192f8 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
192f9 74 65 28 70 44 65 73 74 50 67 29 29 0a 20 20 20  te(pDestPg)).   
192fa 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   ){.      const 
192fb 75 38 20 2a 7a 49 6e 20 3d 20 26 7a 53 72 63 44  u8 *zIn = &zSrcD
192fc 61 74 61 5b 69 4f 66 66 25 6e 53 72 63 50 67 73  ata[iOff%nSrcPgs
192fd 7a 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 44  z];.      u8 *zD
192fe 65 73 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65  estData = sqlite
192ff 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44  3PagerGetData(pD
19300 65 73 74 50 67 29 3b 0a 20 20 20 20 20 20 75 38  estPg);.      u8
19301 20 2a 7a 4f 75 74 20 3d 20 26 7a 44 65 73 74 44   *zOut = &zDestD
19302 61 74 61 5b 69 4f 66 66 25 6e 44 65 73 74 50 67  ata[iOff%nDestPg
19303 73 7a 5d 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43  sz];..      /* C
19304 6f 70 79 20 74 68 65 20 64 61 74 61 20 66 72 6f  opy the data fro
19305 6d 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67  m the source pag
19306 65 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69  e into the desti
19307 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 20  nation page..   
19308 20 20 20 2a 2a 20 54 68 65 6e 20 63 6c 65 61 72     ** Then clear
19309 20 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72   the Btree layer
1930a 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20   MemPage.isInit 
1930b 66 6c 61 67 2e 20 42 6f 74 68 20 74 68 69 73 20  flag. Both this 
1930c 6d 6f 64 75 6c 65 0a 20 20 20 20 20 20 2a 2a 20  module.      ** 
1930d 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 63 6f  and the pager co
1930e 64 65 20 75 73 65 20 74 68 69 73 20 74 72 69 63  de use this tric
1930f 6b 20 28 63 6c 65 61 72 69 6e 67 20 74 68 65 20  k (clearing the 
19310 66 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 20  first byte.     
19311 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20   ** of the page 
19312 27 65 78 74 72 61 27 20 73 70 61 63 65 20 74 6f  'extra' space to
19313 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20   invalidate the 
19314 42 74 72 65 65 20 6c 61 79 65 72 73 0a 20 20 20  Btree layers.   
19315 20 20 20 2a 2a 20 63 61 63 68 65 64 20 70 61 72     ** cached par
19316 73 65 20 6f 66 20 74 68 65 20 70 61 67 65 29 2e  se of the page).
19317 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20   MemPage.isInit 
19318 69 73 20 6d 61 72 6b 65 64 20 0a 20 20 20 20 20  is marked .     
19319 20 2a 2a 20 22 4d 55 53 54 20 42 45 20 46 49 52   ** "MUST BE FIR
1931a 53 54 22 20 66 6f 72 20 74 68 69 73 20 70 75 72  ST" for this pur
1931b 70 6f 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  pose..      */. 
1931c 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74       memcpy(zOut
1931d 2c 20 7a 49 6e 2c 20 6e 43 6f 70 79 29 3b 0a 20  , zIn, nCopy);. 
1931e 20 20 20 20 20 28 28 75 38 20 2a 29 73 71 6c 69       ((u8 *)sqli
1931f 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61  te3PagerGetExtra
19320 28 70 44 65 73 74 50 67 29 29 5b 30 5d 20 3d 20  (pDestPg))[0] = 
19321 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  0;.    }.    sql
19322 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
19323 44 65 73 74 50 67 29 3b 0a 20 20 7d 0a 0a 20 20  DestPg);.  }..  
19324 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
19325 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20 69 73 20  .** If pFile is 
19326 63 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72  currently larger
19327 20 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65   than iSize byte
19328 73 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65  s, then truncate
19329 20 69 74 20 74 6f 0a 2a 2a 20 65 78 61 63 74 6c   it to.** exactl
1932a 79 20 69 53 69 7a 65 20 62 79 74 65 73 2e 20 49  y iSize bytes. I
1932b 66 20 70 46 69 6c 65 20 69 73 20 6e 6f 74 20 6c  f pFile is not l
1932c 61 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65  arger than iSize
1932d 20 62 79 74 65 73 2c 20 74 68 65 6e 0a 2a 2a 20   bytes, then.** 
1932e 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
1932f 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
19330 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
19331 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69   if everything i
19332 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72  s successful, or
19333 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
19334 20 0a 2a 2a 20 63 6f 64 65 20 69 66 20 61 6e 20   .** code if an 
19335 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f  error occurs..*/
19336 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 63 6b  .static int back
19337 75 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 73  upTruncateFile(s
19338 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
19339 6c 65 2c 20 69 36 34 20 69 53 69 7a 65 29 7b 0a  le, i64 iSize){.
1933a 20 20 69 36 34 20 69 43 75 72 72 65 6e 74 3b 0a    i64 iCurrent;.
1933b 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
1933c 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 46 69  e3OsFileSize(pFi
1933d 6c 65 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a  le, &iCurrent);.
1933e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1933f 5f 4f 4b 20 26 26 20 69 43 75 72 72 65 6e 74 3e  _OK && iCurrent>
19340 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 20  iSize ){.    rc 
19341 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
19342 61 74 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65  ate(pFile, iSize
19343 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
19344 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  rc;.}../*.** Reg
19345 69 73 74 65 72 20 74 68 69 73 20 62 61 63 6b 75  ister this backu
19346 70 20 6f 62 6a 65 63 74 20 77 69 74 68 20 74 68  p object with th
19347 65 20 61 73 73 6f 63 69 61 74 65 64 20 73 6f 75  e associated sou
19348 72 63 65 20 70 61 67 65 72 20 66 6f 72 0a 2a 2a  rce pager for.**
19349 20 63 61 6c 6c 62 61 63 6b 73 20 77 68 65 6e 20   callbacks when 
1934a 70 61 67 65 73 20 61 72 65 20 63 68 61 6e 67 65  pages are change
1934b 64 20 6f 72 20 74 68 65 20 63 61 63 68 65 20 69  d or the cache i
1934c 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 73  nvalidated..*/.s
1934d 74 61 74 69 63 20 76 6f 69 64 20 61 74 74 61 63  tatic void attac
1934e 68 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 73 71  hBackupObject(sq
1934f 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29  lite3_backup *p)
19350 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  {.  sqlite3_back
19351 75 70 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72  up **pp;.  asser
19352 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
19353 6f 6c 64 73 4d 75 74 65 78 28 70 2d 3e 70 53 72  oldsMutex(p->pSr
19354 63 29 20 29 3b 0a 20 20 70 70 20 3d 20 73 71 6c  c) );.  pp = sql
19355 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50  ite3PagerBackupP
19356 74 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 50  tr(sqlite3BtreeP
19357 61 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a  ager(p->pSrc));.
19358 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 2a 70 70    p->pNext = *pp
19359 3b 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 70  ;.  *pp = p;.  p
1935a 2d 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20 31  ->isAttached = 1
1935b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20  ;.}../*.** Copy 
1935c 6e 50 61 67 65 20 70 61 67 65 73 20 66 72 6f 6d  nPage pages from
1935d 20 74 68 65 20 73 6f 75 72 63 65 20 62 2d 74 72   the source b-tr
1935e 65 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e  ee to the destin
1935f 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
19360 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
19361 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c  _backup_step(sql
19362 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20  ite3_backup *p, 
19363 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e  int nPage){.  in
19364 74 20 72 63 3b 0a 0a 20 20 73 71 6c 69 74 65 33  t rc;..  sqlite3
19365 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e  _mutex_enter(p->
19366 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a  pSrcDb->mutex);.
19367 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
19368 74 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20  ter(p->pSrc);.  
19369 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29  if( p->pDestDb )
1936a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
1936b 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65  tex_enter(p->pDe
1936c 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  stDb->mutex);.  
1936d 7d 0a 0a 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b  }..  rc = p->rc;
1936e 0a 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45  .  if( !isFatalE
1936f 72 72 6f 72 28 72 63 29 20 29 7b 0a 20 20 20 20  rror(rc) ){.    
19370 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 53  Pager * const pS
19371 72 63 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65  rcPager = sqlite
19372 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70  3BtreePager(p->p
19373 53 72 63 29 3b 20 20 20 20 20 2f 2a 20 53 6f 75  Src);     /* Sou
19374 72 63 65 20 70 61 67 65 72 20 2a 2f 0a 20 20 20  rce pager */.   
19375 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70   Pager * const p
19376 44 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c 69  DestPager = sqli
19377 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d  te3BtreePager(p-
19378 3e 70 44 65 73 74 29 3b 20 20 20 2f 2a 20 44 65  >pDest);   /* De
19379 73 74 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 20  st pager */.    
1937a 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20  int ii;         
1937b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1937c 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
1937d 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69  ariable */.    i
1937e 6e 74 20 6e 53 72 63 50 61 67 65 20 3d 20 2d 31  nt nSrcPage = -1
1937f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
19380 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 6f 75    /* Size of sou
19381 72 63 65 20 64 62 20 69 6e 20 70 61 67 65 73 20  rce db in pages 
19382 2a 2f 0a 20 20 20 20 69 6e 74 20 62 43 6c 6f 73  */.    int bClos
19383 65 54 72 61 6e 73 20 3d 20 30 3b 20 20 20 20 20  eTrans = 0;     
19384 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
19385 65 20 69 66 20 73 72 63 20 64 62 20 72 65 71 75  e if src db requ
19386 69 72 65 73 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a  ires unlocking *
19387 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  /..    /* If the
19388 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 69 73   source pager is
19389 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20   currently in a 
1938a 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1938b 6e 2c 20 72 65 74 75 72 6e 0a 20 20 20 20 2a 2a  n, return.    **
1938c 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 6d 6d   SQLITE_BUSY imm
1938d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2f  ediately..    */
1938e 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 44 65 73  .    if( p->pDes
1938f 74 44 62 20 26 26 20 70 2d 3e 70 53 72 63 2d 3e  tDb && p->pSrc->
19390 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
19391 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  on==TRANS_WRITE 
19392 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
19393 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
19394 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
19395 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
19396 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 74  }..    /* Lock t
19397 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  he destination d
19398 61 74 61 62 61 73 65 2c 20 69 66 20 69 74 20 69  atabase, if it i
19399 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 61 6c 72  s not locked alr
1939a 65 61 64 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28  eady. */.    if(
1939b 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 26   SQLITE_OK==rc &
1939c 26 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64  & p->bDestLocked
1939d 3d 3d 30 0a 20 20 20 20 20 26 26 20 53 51 4c 49  ==0.     && SQLI
1939e 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
1939f 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72  ite3BtreeBeginTr
193a0 61 6e 73 28 70 2d 3e 70 44 65 73 74 2c 20 32 29  ans(p->pDest, 2)
193a1 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  ) .    ){.      
193a2 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 3d  p->bDestLocked =
193a3 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   1;.      sqlite
193a4 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2d  3BtreeGetMeta(p-
193a5 3e 70 44 65 73 74 2c 20 42 54 52 45 45 5f 53 43  >pDest, BTREE_SC
193a6 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 26 70  HEMA_VERSION, &p
193a7 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 29 3b 0a  ->iDestSchema);.
193a8 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
193a9 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65   there is no ope
193aa 6e 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69  n read-transacti
193ab 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65  on on the source
193ac 20 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 0a   database, open.
193ad 20 20 20 20 2a 2a 20 6f 6e 65 20 6e 6f 77 2e 20      ** one now. 
193ae 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  If a transaction
193af 20 69 73 20 6f 70 65 6e 65 64 20 68 65 72 65 2c   is opened here,
193b0 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65   then it will be
193b1 20 63 6c 6f 73 65 64 0a 20 20 20 20 2a 2a 20 62   closed.    ** b
193b2 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
193b3 69 6f 6e 20 65 78 69 74 73 2e 0a 20 20 20 20 2a  ion exits..    *
193b4 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
193b5 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71  LITE_OK && 0==sq
193b6 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65  lite3BtreeIsInRe
193b7 61 64 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 29  adTrans(p->pSrc)
193b8 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
193b9 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e  qlite3BtreeBegin
193ba 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 2c 20 30  Trans(p->pSrc, 0
193bb 29 3b 0a 20 20 20 20 20 20 62 43 6c 6f 73 65 54  );.      bCloseT
193bc 72 61 6e 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  rans = 1;.    }.
193bd 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 74 68    .    /* Now th
193be 61 74 20 74 68 65 72 65 20 69 73 20 61 20 72 65  at there is a re
193bf 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73  ad-lock on the s
193c0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2c 20  ource database, 
193c1 71 75 65 72 79 20 74 68 65 0a 20 20 20 20 2a 2a  query the.    **
193c2 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f   source pager fo
193c3 72 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  r the number of 
193c4 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
193c5 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  abase..    */.  
193c6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
193c7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
193c8 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  = sqlite3PagerPa
193c9 67 65 63 6f 75 6e 74 28 70 53 72 63 50 61 67 65  gecount(pSrcPage
193ca 72 2c 20 26 6e 53 72 63 50 61 67 65 29 3b 0a 20  r, &nSrcPage);. 
193cb 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d     }.    for(ii=
193cc 30 3b 20 28 6e 50 61 67 65 3c 30 20 7c 7c 20 69  0; (nPage<0 || i
193cd 69 3c 6e 50 61 67 65 29 20 26 26 20 70 2d 3e 69  i<nPage) && p->i
193ce 4e 65 78 74 3c 3d 28 50 67 6e 6f 29 6e 53 72 63  Next<=(Pgno)nSrc
193cf 50 61 67 65 20 26 26 20 21 72 63 3b 20 69 69 2b  Page && !rc; ii+
193d0 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  +){.      const 
193d1 50 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 70 2d  Pgno iSrcPg = p-
193d2 3e 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  >iNext;         
193d3 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63          /* Sourc
193d4 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f  e page number */
193d5 0a 20 20 20 20 20 20 69 66 28 20 69 53 72 63 50  .      if( iSrcP
193d6 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  g!=PENDING_BYTE_
193d7 50 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42  PAGE(p->pSrc->pB
193d8 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 44 62  t) ){.        Db
193d9 50 61 67 65 20 2a 70 53 72 63 50 67 3b 20 20 20  Page *pSrcPg;   
193da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
193db 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75            /* Sou
193dc 72 63 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20  rce page object 
193dd 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  */.        rc = 
193de 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
193df 70 53 72 63 50 61 67 65 72 2c 20 69 53 72 63 50  pSrcPager, iSrcP
193e0 67 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20  g, &pSrcPg);.   
193e1 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
193e2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
193e3 20 20 20 20 72 63 20 3d 20 62 61 63 6b 75 70 4f      rc = backupO
193e4 6e 65 50 61 67 65 28 70 2c 20 69 53 72 63 50 67  nePage(p, iSrcPg
193e5 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  , sqlite3PagerGe
193e6 74 44 61 74 61 28 70 53 72 63 50 67 29 29 3b 0a  tData(pSrcPg));.
193e7 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
193e8 33 50 61 67 65 72 55 6e 72 65 66 28 70 53 72 63  3PagerUnref(pSrc
193e9 50 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  Pg);.        }. 
193ea 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e       }.      p->
193eb 69 4e 65 78 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20  iNext++;.    }. 
193ec 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
193ed 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d  E_OK ){.      p-
193ee 3e 6e 50 61 67 65 63 6f 75 6e 74 20 3d 20 6e 53  >nPagecount = nS
193ef 72 63 50 61 67 65 3b 0a 20 20 20 20 20 20 70 2d  rcPage;.      p-
193f0 3e 6e 52 65 6d 61 69 6e 69 6e 67 20 3d 20 6e 53  >nRemaining = nS
193f1 72 63 50 61 67 65 2b 31 2d 70 2d 3e 69 4e 65 78  rcPage+1-p->iNex
193f2 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  t;.      if( p->
193f3 69 4e 65 78 74 3e 28 50 67 6e 6f 29 6e 53 72 63  iNext>(Pgno)nSrc
193f4 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Page ){.        
193f5 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45  rc = SQLITE_DONE
193f6 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
193f7 28 20 21 70 2d 3e 69 73 41 74 74 61 63 68 65 64  ( !p->isAttached
193f8 20 29 7b 0a 20 20 20 20 20 20 20 20 61 74 74 61   ){.        atta
193f9 63 68 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 70  chBackupObject(p
193fa 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
193fb 0a 20 20 0a 20 20 20 20 2f 2a 20 55 70 64 61 74  .  .    /* Updat
193fc 65 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72  e the schema ver
193fd 73 69 6f 6e 20 66 69 65 6c 64 20 69 6e 20 74 68  sion field in th
193fe 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
193ff 74 61 62 61 73 65 2e 20 54 68 69 73 0a 20 20 20  tabase. This.   
19400 20 2a 2a 20 69 73 20 74 6f 20 6d 61 6b 65 20 73   ** is to make s
19401 75 72 65 20 74 68 61 74 20 74 68 65 20 73 63 68  ure that the sch
19402 65 6d 61 2d 76 65 72 73 69 6f 6e 20 72 65 61 6c  ema-version real
19403 6c 79 20 64 6f 65 73 20 63 68 61 6e 67 65 20 69  ly does change i
19404 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 73  n.    ** the cas
19405 65 20 77 68 65 72 65 20 74 68 65 20 73 6f 75 72  e where the sour
19406 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69  ce and destinati
19407 6f 6e 20 64 61 74 61 62 61 73 65 73 20 68 61 76  on databases hav
19408 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 61 6d  e the.    ** sam
19409 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e  e schema version
1940a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1940b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
1940c 20 0a 20 20 20 20 20 26 26 20 28 72 63 20 3d 20   .     && (rc = 
1940d 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61  sqlite3BtreeUpda
1940e 74 65 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c  teMeta(p->pDest,
1940f 31 2c 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61  1,p->iDestSchema
19410 2b 31 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a  +1))==SQLITE_OK.
19411 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e      ){.      con
19412 73 74 20 69 6e 74 20 6e 53 72 63 50 61 67 65 73  st int nSrcPages
19413 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ize = sqlite3Btr
19414 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d  eeGetPageSize(p-
19415 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 63 6f  >pSrc);.      co
19416 6e 73 74 20 69 6e 74 20 6e 44 65 73 74 50 61 67  nst int nDestPag
19417 65 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42  esize = sqlite3B
19418 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28  treeGetPageSize(
19419 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 20 20 20  p->pDest);.     
1941a 20 69 6e 74 20 6e 44 65 73 74 54 72 75 6e 63 61   int nDestTrunca
1941b 74 65 3b 0a 20 20 0a 20 20 20 20 20 20 69 66 28  te;.  .      if(
1941c 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20   p->pDestDb ){. 
1941d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
1941e 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
1941f 61 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 30 29  a(p->pDestDb, 0)
19420 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
19421 20 2f 2a 20 53 65 74 20 6e 44 65 73 74 54 72 75   /* Set nDestTru
19422 6e 63 61 74 65 20 74 6f 20 74 68 65 20 66 69 6e  ncate to the fin
19423 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
19424 65 73 20 69 6e 20 74 68 65 20 64 65 73 74 69 6e  es in the destin
19425 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 64  ation.      ** d
19426 61 74 61 62 61 73 65 2e 20 54 68 65 20 63 6f 6d  atabase. The com
19427 70 6c 69 63 61 74 69 6f 6e 20 68 65 72 65 20 69  plication here i
19428 73 20 74 68 61 74 20 74 68 65 20 64 65 73 74 69  s that the desti
19429 6e 61 74 69 6f 6e 20 70 61 67 65 0a 20 20 20 20  nation page.    
1942a 20 20 2a 2a 20 73 69 7a 65 20 6d 61 79 20 62 65    ** size may be
1942b 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20 74 68   different to th
1942c 65 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69  e source page si
1942d 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ze. .      **.  
1942e 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 73 6f      ** If the so
1942f 75 72 63 65 20 70 61 67 65 20 73 69 7a 65 20 69  urce page size i
19430 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74  s smaller than t
19431 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70  he destination p
19432 61 67 65 20 73 69 7a 65 2c 20 0a 20 20 20 20 20  age size, .     
19433 20 2a 2a 20 72 6f 75 6e 64 20 75 70 2e 20 49 6e   ** round up. In
19434 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63   this case the c
19435 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73  all to sqlite3Os
19436 54 72 75 6e 63 61 74 65 28 29 20 62 65 6c 6f 77  Truncate() below
19437 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 66   will.      ** f
19438 69 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ix the size of t
19439 68 65 20 66 69 6c 65 2e 20 48 6f 77 65 76 65 72  he file. However
1943a 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   it is important
1943b 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 20 20 2a   to call.      *
1943c 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72  * sqlite3PagerTr
1943d 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20 68 65  uncateImage() he
1943e 72 65 20 73 6f 20 74 68 61 74 20 61 6e 79 20 70  re so that any p
1943f 61 67 65 73 20 69 6e 20 74 68 65 20 0a 20 20 20  ages in the .   
19440 20 20 20 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f     ** destinatio
19441 6e 20 66 69 6c 65 20 74 68 61 74 20 6c 69 65 20  n file that lie 
19442 62 65 79 6f 6e 64 20 74 68 65 20 6e 44 65 73 74  beyond the nDest
19443 54 72 75 6e 63 61 74 65 20 70 61 67 65 20 6d 61  Truncate page ma
19444 72 6b 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20  rk are.      ** 
19445 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 79 20 50 61  journalled by Pa
19446 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  gerCommitPhaseOn
19447 65 28 29 20 62 65 66 6f 72 65 20 74 68 65 79 20  e() before they 
19448 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 20 20  are destroyed.  
19449 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20 66 69      ** by the fi
1944a 6c 65 20 74 72 75 6e 63 61 74 69 6f 6e 2e 0a 20  le truncation.. 
1944b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
1944c 28 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c 6e  ( nSrcPagesize<n
1944d 44 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b 0a  DestPagesize ){.
1944e 20 20 20 20 20 20 20 20 69 6e 74 20 72 61 74 69          int rati
1944f 6f 20 3d 20 6e 44 65 73 74 50 61 67 65 73 69 7a  o = nDestPagesiz
19450 65 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 3b 0a  e/nSrcPagesize;.
19451 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75          nDestTru
19452 6e 63 61 74 65 20 3d 20 28 6e 53 72 63 50 61 67  ncate = (nSrcPag
19453 65 2b 72 61 74 69 6f 2d 31 29 2f 72 61 74 69 6f  e+ratio-1)/ratio
19454 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 44  ;.        if( nD
19455 65 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e  estTruncate==(in
19456 74 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  t)PENDING_BYTE_P
19457 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42  AGE(p->pDest->pB
19458 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  t) ){.          
19459 6e 44 65 73 74 54 72 75 6e 63 61 74 65 2d 2d 3b  nDestTruncate--;
1945a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1945b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1945c 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 3d 20  nDestTruncate = 
1945d 6e 53 72 63 50 61 67 65 20 2a 20 28 6e 53 72 63  nSrcPage * (nSrc
1945e 50 61 67 65 73 69 7a 65 2f 6e 44 65 73 74 50 61  Pagesize/nDestPa
1945f 67 65 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d  gesize);.      }
19460 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61  .      sqlite3Pa
19461 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65  gerTruncateImage
19462 28 70 44 65 73 74 50 61 67 65 72 2c 20 6e 44 65  (pDestPager, nDe
19463 73 74 54 72 75 6e 63 61 74 65 29 3b 0a 0a 20 20  stTruncate);..  
19464 20 20 20 20 69 66 28 20 6e 53 72 63 50 61 67 65      if( nSrcPage
19465 73 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 73 69  size<nDestPagesi
19466 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ze ){.        /*
19467 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70   If the source p
19468 61 67 65 2d 73 69 7a 65 20 69 73 20 73 6d 61 6c  age-size is smal
19469 6c 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 73  ler than the des
1946a 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2d 73 69  tination page-si
1946b 7a 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ze,.        ** t
1946c 77 6f 20 65 78 74 72 61 20 74 68 69 6e 67 73 20  wo extra things 
1946d 6d 61 79 20 6e 65 65 64 20 74 6f 20 68 61 70 70  may need to happ
1946e 65 6e 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  en:.        **. 
1946f 20 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 54 68         **   * Th
19470 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 61  e destination ma
19471 79 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72 75  y need to be tru
19472 6e 63 61 74 65 64 2c 20 61 6e 64 0a 20 20 20 20  ncated, and.    
19473 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
19474 2a 20 20 20 2a 20 44 61 74 61 20 73 74 6f 72 65  *   * Data store
19475 64 20 6f 6e 20 74 68 65 20 70 61 67 65 73 20 69  d on the pages i
19476 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f  mmediately follo
19477 77 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20  wing the .      
19478 20 20 2a 2a 20 20 20 20 20 70 65 6e 64 69 6e 67    **     pending
19479 2d 62 79 74 65 20 70 61 67 65 20 69 6e 20 74 68  -byte page in th
1947a 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
1947b 65 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65  e may need to be
1947c 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20  .        **     
1947d 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20  copied into the 
1947e 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
1947f 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  base..        */
19480 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69  .        const i
19481 36 34 20 69 53 69 7a 65 20 3d 20 28 69 36 34 29  64 iSize = (i64)
19482 6e 53 72 63 50 61 67 65 73 69 7a 65 20 2a 20 28  nSrcPagesize * (
19483 69 36 34 29 6e 53 72 63 50 61 67 65 3b 0a 20 20  i64)nSrcPage;.  
19484 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69        sqlite3_fi
19485 6c 65 20 2a 20 63 6f 6e 73 74 20 70 46 69 6c 65  le * const pFile
19486 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46   = sqlite3PagerF
19487 69 6c 65 28 70 44 65 73 74 50 61 67 65 72 29 3b  ile(pDestPager);
19488 0a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
19489 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20  ( pFile );.     
1948a 20 20 20 61 73 73 65 72 74 28 20 28 69 36 34 29     assert( (i64)
1948b 6e 44 65 73 74 54 72 75 6e 63 61 74 65 2a 28 69  nDestTruncate*(i
1948c 36 34 29 6e 44 65 73 74 50 61 67 65 73 69 7a 65  64)nDestPagesize
1948d 20 3e 3d 20 69 53 69 7a 65 20 7c 7c 20 28 0a 20   >= iSize || (. 
1948e 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65               nDe
1948f 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74  stTruncate==(int
19490 29 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  )(PENDING_BYTE_P
19491 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42  AGE(p->pDest->pB
19492 74 29 2d 31 29 0a 20 20 20 20 20 20 20 20 20 20  t)-1).          
19493 20 26 26 20 69 53 69 7a 65 3e 3d 50 45 4e 44 49   && iSize>=PENDI
19494 4e 47 5f 42 59 54 45 20 26 26 20 69 53 69 7a 65  NG_BYTE && iSize
19495 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e  <=PENDING_BYTE+n
19496 44 65 73 74 50 61 67 65 73 69 7a 65 0a 20 20 20  DestPagesize.   
19497 20 20 20 20 20 29 29 3b 0a 20 20 20 20 20 20 20       ));.       
19498 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
19499 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67  (rc = sqlite3Pag
1949a 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  erCommitPhaseOne
1949b 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20  (pDestPager, 0, 
1949c 31 29 29 0a 20 20 20 20 20 20 20 20 20 26 26 20  1)).         && 
1949d 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
1949e 20 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46   backupTruncateF
1949f 69 6c 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65  ile(pFile, iSize
194a0 29 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 53  )).         && S
194a1 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
194a2 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63  sqlite3PagerSync
194a3 28 70 44 65 73 74 50 61 67 65 72 29 29 0a 20 20  (pDestPager)).  
194a4 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
194a5 20 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 20 20     i64 iOff;.   
194a6 20 20 20 20 20 20 20 69 36 34 20 69 45 6e 64 20         i64 iEnd 
194a7 3d 20 4d 49 4e 28 50 45 4e 44 49 4e 47 5f 42 59  = MIN(PENDING_BY
194a8 54 45 20 2b 20 6e 44 65 73 74 50 61 67 65 73 69  TE + nDestPagesi
194a9 7a 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 20 20  ze, iSize);.    
194aa 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20        for(.     
194ab 20 20 20 20 20 20 20 69 4f 66 66 3d 50 45 4e 44         iOff=PEND
194ac 49 4e 47 5f 42 59 54 45 2b 6e 53 72 63 50 61 67  ING_BYTE+nSrcPag
194ad 65 73 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20  esize; .        
194ae 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f      rc==SQLITE_O
194af 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20  K && iOff<iEnd; 
194b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 4f 66  .            iOf
194b1 66 2b 3d 6e 53 72 63 50 61 67 65 73 69 7a 65 0a  f+=nSrcPagesize.
194b2 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20            ){.   
194b3 20 20 20 20 20 20 20 20 20 50 67 48 64 72 20 2a           PgHdr *
194b4 70 53 72 63 50 67 20 3d 20 30 3b 0a 20 20 20 20  pSrcPg = 0;.    
194b5 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67          const Pg
194b6 6e 6f 20 69 53 72 63 50 67 20 3d 20 28 50 67 6e  no iSrcPg = (Pgn
194b7 6f 29 28 28 69 4f 66 66 2f 6e 53 72 63 50 61 67  o)((iOff/nSrcPag
194b8 65 73 69 7a 65 29 2b 31 29 3b 0a 20 20 20 20 20  esize)+1);.     
194b9 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
194ba 74 65 33 50 61 67 65 72 47 65 74 28 70 53 72 63  te3PagerGet(pSrc
194bb 50 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26  Pager, iSrcPg, &
194bc 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20  pSrcPg);.       
194bd 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
194be 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
194bf 20 20 20 20 20 20 20 20 75 38 20 2a 7a 44 61 74          u8 *zDat
194c0 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  a = sqlite3Pager
194c1 47 65 74 44 61 74 61 28 70 53 72 63 50 67 29 3b  GetData(pSrcPg);
194c2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
194c3 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
194c4 74 65 28 70 46 69 6c 65 2c 20 7a 44 61 74 61 2c  te(pFile, zData,
194c5 20 6e 53 72 63 50 61 67 65 73 69 7a 65 2c 20 69   nSrcPagesize, i
194c6 4f 66 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Off);.          
194c7 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
194c8 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
194c9 66 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20  f(pSrcPg);.     
194ca 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
194cb 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
194cc 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
194cd 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
194ce 73 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65 72  seOne(pDestPager
194cf 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  , 0, 0);.      }
194d0 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e  .  .      /* Fin
194d1 69 73 68 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74  ish committing t
194d2 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74  he transaction t
194d3 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  o the destinatio
194d4 6e 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 20  n database. */. 
194d5 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f       if( SQLITE_
194d6 4f 4b 3d 3d 72 63 0a 20 20 20 20 20 20 20 26 26  OK==rc.       &&
194d7 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
194d8 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
194d9 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e  mmitPhaseTwo(p->
194da 70 44 65 73 74 29 29 0a 20 20 20 20 20 20 29 7b  pDest)).      ){
194db 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
194dc 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20  LITE_DONE;.     
194dd 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
194de 2f 2a 20 49 66 20 62 43 6c 6f 73 65 54 72 61 6e  /* If bCloseTran
194df 73 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  s is true, then 
194e0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 70  this function op
194e1 65 6e 65 64 20 61 20 72 65 61 64 20 74 72 61 6e  ened a read tran
194e2 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f  saction.    ** o
194e3 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74  n the source dat
194e4 61 62 61 73 65 2e 20 43 6c 6f 73 65 20 74 68 65  abase. Close the
194e5 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f   read transactio
194e6 6e 20 68 65 72 65 2e 20 54 68 65 72 65 20 69 73  n here. There is
194e7 0a 20 20 20 20 2a 2a 20 6e 6f 20 6e 65 65 64 20  .    ** no need 
194e8 74 6f 20 63 68 65 63 6b 20 74 68 65 20 72 65 74  to check the ret
194e9 75 72 6e 20 76 61 6c 75 65 73 20 6f 66 20 74 68  urn values of th
194ea 65 20 62 74 72 65 65 20 6d 65 74 68 6f 64 73 20  e btree methods 
194eb 68 65 72 65 2c 20 61 73 0a 20 20 20 20 2a 2a 20  here, as.    ** 
194ec 22 63 6f 6d 6d 69 74 74 69 6e 67 22 20 61 20 72  "committing" a r
194ed 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63  ead-only transac
194ee 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c  tion cannot fail
194ef 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
194f0 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 29 7b 0a   bCloseTrans ){.
194f1 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20        TESTONLY( 
194f2 69 6e 74 20 72 63 32 20 29 3b 0a 20 20 20 20 20  int rc2 );.     
194f3 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 20   TESTONLY( rc2  
194f4 3d 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 65  = ) sqlite3Btree
194f5 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70  CommitPhaseOne(p
194f6 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20  ->pSrc, 0);.    
194f7 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20    TESTONLY( rc2 
194f8 7c 3d 20 29 20 73 71 6c 69 74 65 33 42 74 72 65  |= ) sqlite3Btre
194f9 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  eCommitPhaseTwo(
194fa 70 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20  p->pSrc);.      
194fb 61 73 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c  assert( rc2==SQL
194fc 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a  ITE_OK );.    }.
194fd 20 20 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72    .    p->rc = r
194fe 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  c;.  }.  if( p->
194ff 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73  pDestDb ){.    s
19500 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
19501 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d  ve(p->pDestDb->m
19502 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  utex);.  }.  sql
19503 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
19504 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74  ->pSrc);.  sqlit
19505 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
19506 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29  ->pSrcDb->mutex)
19507 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
19508 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
19509 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61 73  all resources as
1950a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6e  sociated with an
1950b 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a   sqlite3_backup*
1950c 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   handle..*/.SQLI
1950d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1950e 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
1950f 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
19510 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62  *p){.  sqlite3_b
19511 61 63 6b 75 70 20 2a 2a 70 70 3b 20 20 20 20 20  ackup **pp;     
19512 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
19513 74 72 20 74 6f 20 68 65 61 64 20 6f 66 20 70 61  tr to head of pa
19514 67 65 72 73 20 62 61 63 6b 75 70 20 6c 69 73 74  gers backup list
19515 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
19516 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20  tex *mutex;     
19517 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75             /* Mu
19518 74 65 78 20 74 6f 20 70 72 6f 74 65 63 74 20 73  tex to protect s
19519 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 2a  ource database *
1951a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
1951b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1951c 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
1951d 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a  e to return */..
1951e 20 20 2f 2a 20 45 6e 74 65 72 20 74 68 65 20 6d    /* Enter the m
1951f 75 74 65 78 65 73 20 2a 2f 0a 20 20 69 66 28 20  utexes */.  if( 
19520 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  p==0 ) return SQ
19521 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74  LITE_OK;.  sqlit
19522 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
19523 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29  ->pSrcDb->mutex)
19524 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
19525 45 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a  Enter(p->pSrc);.
19526 20 20 6d 75 74 65 78 20 3d 20 70 2d 3e 70 53 72    mutex = p->pSr
19527 63 44 62 2d 3e 6d 75 74 65 78 3b 0a 20 20 69 66  cDb->mutex;.  if
19528 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a  ( p->pDestDb ){.
19529 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1952a 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 74  x_enter(p->pDest
1952b 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  Db->mutex);.  }.
1952c 0a 20 20 2f 2a 20 44 65 74 61 63 68 20 74 68 69  .  /* Detach thi
1952d 73 20 62 61 63 6b 75 70 20 66 72 6f 6d 20 74 68  s backup from th
1952e 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e 20  e source pager. 
1952f 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73  */.  if( p->pDes
19530 74 44 62 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53  tDb ){.    p->pS
19531 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b 0a 20  rc->nBackup--;. 
19532 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 41 74   }.  if( p->isAt
19533 74 61 63 68 65 64 20 29 7b 0a 20 20 20 20 70 70  tached ){.    pp
19534 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42   = sqlite3PagerB
19535 61 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33  ackupPtr(sqlite3
19536 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53  BtreePager(p->pS
19537 72 63 29 29 3b 0a 20 20 20 20 77 68 69 6c 65 28  rc));.    while(
19538 20 2a 70 70 21 3d 70 20 29 7b 0a 20 20 20 20 20   *pp!=p ){.     
19539 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e   pp = &(*pp)->pN
1953a 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  ext;.    }.    *
1953b 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20  pp = p->pNext;. 
1953c 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72   }..  /* If a tr
1953d 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 69  ansaction is sti
1953e 6c 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42  ll open on the B
1953f 74 72 65 65 2c 20 72 6f 6c 6c 20 69 74 20 62 61  tree, roll it ba
19540 63 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ck. */.  sqlite3
19541 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 2d  BtreeRollback(p-
19542 3e 70 44 65 73 74 29 3b 0a 0a 20 20 2f 2a 20 53  >pDest);..  /* S
19543 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  et the error cod
19544 65 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61  e of the destina
19545 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61  tion database ha
19546 6e 64 6c 65 2e 20 2a 2f 0a 20 20 72 63 20 3d 20  ndle. */.  rc = 
19547 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 44  (p->rc==SQLITE_D
19548 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  ONE) ? SQLITE_OK
19549 20 3a 20 70 2d 3e 72 63 3b 0a 20 20 73 71 6c 69   : p->rc;.  sqli
1954a 74 65 33 45 72 72 6f 72 28 70 2d 3e 70 44 65 73  te3Error(p->pDes
1954b 74 44 62 2c 20 72 63 2c 20 30 29 3b 0a 0a 20 20  tDb, rc, 0);..  
1954c 2f 2a 20 45 78 69 74 20 74 68 65 20 6d 75 74 65  /* Exit the mute
1954d 78 65 73 20 61 6e 64 20 66 72 65 65 20 74 68 65  xes and free the
1954e 20 62 61 63 6b 75 70 20 63 6f 6e 74 65 78 74 20   backup context 
1954f 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20  structure. */.  
19550 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29  if( p->pDestDb )
19551 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
19552 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65  tex_leave(p->pDe
19553 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  stDb->mutex);.  
19554 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
19555 4c 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a  Leave(p->pSrc);.
19556 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62    if( p->pDestDb
19557 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
19558 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 20 20 73  free(p);.  }.  s
19559 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1955a 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  ve(mutex);.  ret
1955b 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1955c 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1955d 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c  er of pages stil
1955e 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64 20 75  l to be backed u
1955f 70 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74  p as of the most
19560 20 72 65 63 65 6e 74 0a 2a 2a 20 63 61 6c 6c 20   recent.** call 
19561 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
19562 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c  p_step()..*/.SQL
19563 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
19564 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69  te3_backup_remai
19565 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63  ning(sqlite3_bac
19566 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  kup *p){.  retur
19567 6e 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 3b  n p->nRemaining;
19568 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
19569 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
1956a 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
1956b 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
1956c 65 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74  e as of the most
1956d 20 0a 2a 2a 20 72 65 63 65 6e 74 20 63 61 6c 6c   .** recent call
1956e 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
1956f 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51  up_step()..*/.SQ
19570 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
19571 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65  ite3_backup_page
19572 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61  count(sqlite3_ba
19573 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75  ckup *p){.  retu
19574 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74  rn p->nPagecount
19575 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
19576 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
19577 65 64 20 61 66 74 65 72 20 74 68 65 20 63 6f 6e  ed after the con
19578 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20 69 50  tents of page iP
19579 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 6f  age of the.** so
1957a 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61  urce database ha
1957b 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  ve been modified
1957c 2e 20 49 66 20 70 61 67 65 20 69 50 61 67 65 20  . If page iPage 
1957d 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
1957e 20 0a 2a 2a 20 63 6f 70 69 65 64 20 69 6e 74 6f   .** copied into
1957f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
19580 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20   database, then 
19581 74 68 65 20 64 61 74 61 20 77 72 69 74 74 65 6e  the data written
19582 20 74 6f 20 74 68 65 0a 2a 2a 20 64 65 73 74 69   to the.** desti
19583 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 77 20 69 6e  nation is now in
19584 76 61 6c 69 64 61 74 65 64 2e 20 54 68 65 20 64  validated. The d
19585 65 73 74 69 6e 61 74 69 6f 6e 20 63 6f 70 79 20  estination copy 
19586 6f 66 20 69 50 61 67 65 20 6e 65 65 64 73 0a 2a  of iPage needs.*
19587 2a 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20  * to be updated 
19588 77 69 74 68 20 74 68 65 20 6e 65 77 20 64 61 74  with the new dat
19589 61 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 63  a before the bac
1958a 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  kup operation is
1958b 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a  .** complete..**
1958c 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65  .** It is assume
1958d 64 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78  d that the mutex
1958e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1958f 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62   the BtShared ob
19590 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f  ject.** correspo
19591 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75  nding to the sou
19592 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20  rce database is 
19593 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
19594 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61  unction is.** ca
19595 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lled..*/.SQLITE_
19596 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19597 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65  ite3BackupUpdate
19598 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
19599 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f 20 69  *pBackup, Pgno i
1959a 50 61 67 65 2c 20 63 6f 6e 73 74 20 75 38 20 2a  Page, const u8 *
1959b 61 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65  aData){.  sqlite
1959c 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20  3_backup *p;    
1959d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1959e 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
1959f 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70  ble */.  for(p=p
195a0 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e  Backup; p; p=p->
195a1 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65  pNext){.    asse
195a2 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
195a3 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e  x_held(p->pSrc->
195a4 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
195a5 20 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45     if( !isFatalE
195a6 72 72 6f 72 28 70 2d 3e 72 63 29 20 26 26 20 69  rror(p->rc) && i
195a7 50 61 67 65 3c 70 2d 3e 69 4e 65 78 74 20 29 7b  Page<p->iNext ){
195a8 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61  .      /* The ba
195a9 63 6b 75 70 20 70 72 6f 63 65 73 73 20 70 20 68  ckup process p h
195aa 61 73 20 61 6c 72 65 61 64 79 20 63 6f 70 69 65  as already copie
195ab 64 20 70 61 67 65 20 69 50 61 67 65 2e 20 42 75  d page iPage. Bu
195ac 74 20 6e 6f 77 20 69 74 0a 20 20 20 20 20 20 2a  t now it.      *
195ad 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66  * has been modif
195ae 69 65 64 20 62 79 20 61 20 74 72 61 6e 73 61 63  ied by a transac
195af 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72  tion on the sour
195b0 63 65 20 70 61 67 65 72 2e 20 43 6f 70 79 0a 20  ce pager. Copy. 
195b1 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20       ** the new 
195b2 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 62 61  data into the ba
195b3 63 6b 75 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ckup..      */. 
195b4 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 61       int rc = ba
195b5 63 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69  ckupOnePage(p, i
195b6 50 61 67 65 2c 20 61 44 61 74 61 29 3b 0a 20 20  Page, aData);.  
195b7 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d      assert( rc!=
195b8 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 72  SQLITE_BUSY && r
195b9 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  c!=SQLITE_LOCKED
195ba 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63   );.      if( rc
195bb 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
195bc 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72         p->rc = r
195bd 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
195be 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
195bf 73 74 61 72 74 20 74 68 65 20 62 61 63 6b 75 70  start the backup
195c0 20 70 72 6f 63 65 73 73 2e 20 54 68 69 73 20 69   process. This i
195c1 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  s called when th
195c2 65 20 70 61 67 65 72 20 6c 61 79 65 72 0a 2a 2a  e pager layer.**
195c3 20 64 65 74 65 63 74 73 20 74 68 61 74 20 74 68   detects that th
195c4 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62  e database has b
195c5 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 79 20  een modified by 
195c6 61 6e 20 65 78 74 65 72 6e 61 6c 20 64 61 74 61  an external data
195c7 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69  base.** connecti
195c8 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  on. In this case
195c9 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79   there is no way
195ca 20 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 68 69 63   of knowing whic
195cb 68 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65  h of the.** page
195cc 73 20 74 68 61 74 20 68 61 76 65 20 62 65 65 6e  s that have been
195cd 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
195ce 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
195cf 61 62 61 73 65 20 61 72 65 20 73 74 69 6c 6c 20  abase are still 
195d0 0a 2a 2a 20 76 61 6c 69 64 20 61 6e 64 20 77 68  .** valid and wh
195d1 69 63 68 20 61 72 65 20 6e 6f 74 2c 20 73 6f 20  ich are not, so 
195d2 74 68 65 20 65 6e 74 69 72 65 20 70 72 6f 63 65  the entire proce
195d3 73 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  ss needs to be r
195d4 65 73 74 61 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  estarted..**.** 
195d5 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68  It is assumed th
195d6 61 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73  at the mutex ass
195d7 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
195d8 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
195d9 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  .** correspondin
195da 67 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20  g to the source 
195db 64 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64  database is held
195dc 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
195dd 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64  ion is.** called
195de 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
195df 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
195e0 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71  BackupRestart(sq
195e1 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42  lite3_backup *pB
195e2 61 63 6b 75 70 29 7b 0a 20 20 73 71 6c 69 74 65  ackup){.  sqlite
195e3 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20  3_backup *p;    
195e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
195e5 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
195e6 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70  ble */.  for(p=p
195e7 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e  Backup; p; p=p->
195e8 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65  pNext){.    asse
195e9 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
195ea 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e  x_held(p->pSrc->
195eb 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
195ec 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 3b     p->iNext = 1;
195ed 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  .  }.}..#ifndef 
195ee 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55  SQLITE_OMIT_VACU
195ef 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68  UM./*.** Copy th
195f0 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65  e complete conte
195f1 6e 74 20 6f 66 20 70 42 74 46 72 6f 6d 20 69 6e  nt of pBtFrom in
195f2 74 6f 20 70 42 74 54 6f 2e 20 20 41 20 74 72 61  to pBtTo.  A tra
195f3 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74  nsaction.** must
195f4 20 62 65 20 61 63 74 69 76 65 20 66 6f 72 20 62   be active for b
195f5 6f 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a  oth files..**.**
195f6 20 54 68 65 20 73 69 7a 65 20 6f 66 20 66 69 6c   The size of fil
195f7 65 20 70 54 6f 20 6d 61 79 20 62 65 20 72 65 64  e pTo may be red
195f8 75 63 65 64 20 62 79 20 74 68 69 73 20 6f 70 65  uced by this ope
195f9 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79 74 68  ration. If anyth
195fa 69 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 77 72 6f  ing .** goes wro
195fb 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74  ng, the transact
195fc 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20 72 6f  ion on pTo is ro
195fd 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 73 75  lled back. If su
195fe 63 63 65 73 73 66 75 6c 2c 20 74 68 65 20 0a 2a  ccessful, the .*
195ff 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  * transaction is
19600 20 63 6f 6d 6d 69 74 74 65 64 20 62 65 66 6f 72   committed befor
19601 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a  e returning..*/.
19602 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19603 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
19604 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a 70  opyFile(Btree *p
19605 54 6f 2c 20 42 74 72 65 65 20 2a 70 46 72 6f 6d  To, Btree *pFrom
19606 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
19607 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 62 3b  qlite3_backup b;
19608 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
19609 6e 74 65 72 28 70 54 6f 29 3b 0a 20 20 73 71 6c  nter(pTo);.  sql
1960a 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
1960b 46 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20 53 65 74  From);..  /* Set
1960c 20 75 70 20 61 6e 20 73 71 6c 69 74 65 33 5f 62   up an sqlite3_b
1960d 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 20 73 71  ackup object. sq
1960e 6c 69 74 65 33 5f 62 61 63 6b 75 70 2e 70 44 65  lite3_backup.pDe
1960f 73 74 44 62 20 6d 75 73 74 20 62 65 20 73 65 74  stDb must be set
19610 0a 20 20 2a 2a 20 74 6f 20 30 2e 20 54 68 69 73  .  ** to 0. This
19611 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
19612 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
19613 6f 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  of sqlite3_backu
19614 70 5f 73 74 65 70 28 29 0a 20 20 2a 2a 20 61 6e  p_step().  ** an
19615 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  d sqlite3_backup
19616 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 64 65 74  _finish() to det
19617 65 63 74 20 74 68 61 74 20 74 68 65 79 20 61 72  ect that they ar
19618 65 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 0a 20  e being called. 
19619 20 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 66 75   ** from this fu
1961a 6e 63 74 69 6f 6e 2c 20 6e 6f 74 20 64 69 72 65  nction, not dire
1961b 63 74 6c 79 20 62 79 20 74 68 65 20 75 73 65 72  ctly by the user
1961c 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  ..  */.  memset(
1961d 26 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 62 29  &b, 0, sizeof(b)
1961e 29 3b 0a 20 20 62 2e 70 53 72 63 44 62 20 3d 20  );.  b.pSrcDb = 
1961f 70 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20 62 2e 70  pFrom->db;.  b.p
19620 53 72 63 20 3d 20 70 46 72 6f 6d 3b 0a 20 20 62  Src = pFrom;.  b
19621 2e 70 44 65 73 74 20 3d 20 70 54 6f 3b 0a 20 20  .pDest = pTo;.  
19622 62 2e 69 4e 65 78 74 20 3d 20 31 3b 0a 0a 20 20  b.iNext = 1;..  
19623 2f 2a 20 30 78 37 46 46 46 46 46 46 46 20 69 73  /* 0x7FFFFFFF is
19624 20 74 68 65 20 68 61 72 64 20 6c 69 6d 69 74 20   the hard limit 
19625 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  for the number o
19626 66 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74  f pages in a dat
19627 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e  abase.  ** file.
19628 20 42 79 20 70 61 73 73 69 6e 67 20 74 68 69 73   By passing this
19629 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   as the number o
1962a 66 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20  f pages to copy 
1962b 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  to.  ** sqlite3_
1962c 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 77  backup_step(), w
1962d 65 20 63 61 6e 20 67 75 61 72 61 6e 74 65 65 20  e can guarantee 
1962e 74 68 61 74 20 74 68 65 20 63 6f 70 79 20 66 69  that the copy fi
1962f 6e 69 73 68 65 73 20 0a 20 20 2a 2a 20 77 69 74  nishes .  ** wit
19630 68 69 6e 20 61 20 73 69 6e 67 6c 65 20 63 61 6c  hin a single cal
19631 6c 20 28 75 6e 6c 65 73 73 20 61 6e 20 65 72 72  l (unless an err
19632 6f 72 20 6f 63 63 75 72 73 29 2e 20 54 68 65 20  or occurs). The 
19633 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
19634 6e 74 0a 20 20 2a 2a 20 63 68 65 63 6b 73 20 74  nt.  ** checks t
19635 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 20 2d  his assumption -
19636 20 28 70 2d 3e 72 63 29 20 73 68 6f 75 6c 64 20   (p->rc) should 
19637 62 65 20 73 65 74 20 74 6f 20 65 69 74 68 65 72  be set to either
19638 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20   SQLITE_DONE .  
19639 2a 2a 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63  ** or an error c
1963a 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ode..  */.  sqli
1963b 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
1963c 26 62 2c 20 30 78 37 46 46 46 46 46 46 46 29 3b  &b, 0x7FFFFFFF);
1963d 0a 20 20 61 73 73 65 72 74 28 20 62 2e 72 63 21  .  assert( b.rc!
1963e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
1963f 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 61 63  rc = sqlite3_bac
19640 6b 75 70 5f 66 69 6e 69 73 68 28 26 62 29 3b 0a  kup_finish(&b);.
19641 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
19642 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e  _OK ){.    pTo->
19643 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78  pBt->pageSizeFix
19644 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73  ed = 0;.  }..  s
19645 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
19646 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74  (pFrom);.  sqlit
19647 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f  e3BtreeLeave(pTo
19648 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
19649 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1964a 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 2a  TE_OMIT_VACUUM *
1964b 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
1964c 2a 2a 20 45 6e 64 20 6f 66 20 62 61 63 6b 75 70  ** End of backup
1964d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1964e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1964f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19650 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
19651 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
19652 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  bemem.c ********
19653 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19654 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19655 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61  */./*.** 2004 Ma
19656 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 26.**.** The a
19657 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
19658 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
19659 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
1965a 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
1965b 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
1965c 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
1965d 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
1965e 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
1965f 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
19660 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
19661 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
19662 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
19663 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
19664 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
19665 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
19666 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
19667 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
19668 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19669 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1966a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1966b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1966c 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
1966d 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
1966e 65 20 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c  e use to manipul
1966f 61 74 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74  ate "Mem" struct
19670 75 72 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a  ure.  A "Mem".**
19671 20 73 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65   stores a single
19672 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44   value in the VD
19673 42 45 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f  BE.  Mem is an o
19674 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20  paque structure 
19675 76 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20  visible.** only 
19676 77 69 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e  within the VDBE.
19677 20 20 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74    Interface rout
19678 69 6e 65 73 20 72 65 66 65 72 20 74 6f 20 61 20  ines refer to a 
19679 4d 65 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  Mem using the.**
1967a 20 6e 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c   name sqlite_val
1967b 75 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64  ue.**.** $Id: vd
1967c 62 65 6d 65 6d 2e 63 2c 76 20 31 2e 31 35 32 20  bemem.c,v 1.152 
1967d 32 30 30 39 2f 30 37 2f 32 32 20 31 38 3a 30 37  2009/07/22 18:07
1967e 3a 34 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :41 drh Exp $.*/
1967f 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c  ../*.** Call sql
19680 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e  ite3VdbeMemExpan
19681 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73  dBlob() on the s
19682 75 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74  upplied value (t
19683 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69  ype Mem*).** P i
19684 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23  f required..*/.#
19685 64 65 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f  define expandBlo
19686 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67  b(P) (((P)->flag
19687 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69  s&MEM_Zero)?sqli
19688 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
19689 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a  Blob(P):0)../*.*
1968a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20  * If pMem is an 
1968b 6f 62 6a 65 63 74 20 77 69 74 68 20 61 20 76 61  object with a va
1968c 6c 69 64 20 73 74 72 69 6e 67 20 72 65 70 72 65  lid string repre
1968d 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20  sentation, this 
1968e 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72  routine.** ensur
1968f 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  es the internal 
19690 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65  encoding for the
19691 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e   string represen
19692 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65  tation is.** 'de
19693 73 69 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f  siredEnc', one o
19694 66 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  f SQLITE_UTF8, S
19695 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72  QLITE_UTF16LE or
19696 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e   SQLITE_UTF16BE.
19697 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69  .**.** If pMem i
19698 73 20 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f  s not a string o
19699 62 6a 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e  bject, or the en
1969a 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74  coding of the st
1969b 72 69 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e  ring.** represen
1969c 74 61 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64  tation is alread
1969d 79 20 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74  y stored using t
1969e 68 65 20 72 65 71 75 65 73 74 65 64 20 65 6e 63  he requested enc
1969f 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73  oding, then this
196a0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61  .** routine is a
196a1 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51   no-op..**.** SQ
196a2 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
196a3 6e 65 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65  ned if the conve
196a4 72 73 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73  rsion is success
196a5 66 75 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75  ful (or not requ
196a6 69 72 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45  ired)..** SQLITE
196a7 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65  _NOMEM may be re
196a8 74 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c  turned if a mall
196a9 6f 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e  oc() fails durin
196aa 67 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20  g conversion.** 
196ab 62 65 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e  between formats.
196ac 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
196ad 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
196ae 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
196af 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20  (Mem *pMem, int 
196b0 64 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69  desiredEnc){.  i
196b1 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
196b2 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45   (pMem->flags&ME
196b3 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a  M_RowSet)==0 );.
196b4 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65    assert( desire
196b5 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc==SQLITE_UTF
196b6 38 20 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d  8 || desiredEnc=
196b7 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a  =SQLITE_UTF16LE.
196b8 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65             || de
196b9 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
196ba 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66  _UTF16BE );.  if
196bb 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  ( !(pMem->flags&
196bc 4d 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d  MEM_Str) || pMem
196bd 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e  ->enc==desiredEn
196be 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
196bf 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
196c0 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
196c1 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
196c2 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
196c3 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23  >db->mutex) );.#
196c4 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
196c5 54 5f 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e  T_UTF16.  return
196c6 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23   SQLITE_ERROR;.#
196c7 65 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72  else..  /* MemTr
196c8 61 6e 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65  anslate() may re
196c9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
196ca 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20  r SQLITE_NOMEM. 
196cb 49 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75  If NOMEM is retu
196cc 72 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20  rned,.  ** then 
196cd 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  the encoding of 
196ce 74 68 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f  the value may no
196cf 74 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a  t have changed..
196d0 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69    */.  rc = sqli
196d1 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c  te3VdbeMemTransl
196d2 61 74 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65  ate(pMem, (u8)de
196d3 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73  siredEnc);.  ass
196d4 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  ert(rc==SQLITE_O
196d5 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  K    || rc==SQLI
196d6 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 73 73  TE_NOMEM);.  ass
196d7 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  ert(rc==SQLITE_O
196d8 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e  K    || pMem->en
196d9 63 21 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a  c!=desiredEnc);.
196da 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c    assert(rc==SQL
196db 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65  ITE_NOMEM || pMe
196dc 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45  m->enc==desiredE
196dd 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  nc);.  return rc
196de 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
196df 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 4d 65 6d  * Make sure pMem
196e0 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  ->z points to a 
196e1 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74  writable allocat
196e2 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20  ion of at least 
196e3 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a  .** n bytes..**.
196e4 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79  ** If the memory
196e5 20 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20   cell currently 
196e6 63 6f 6e 74 61 69 6e 73 20 73 74 72 69 6e 67 20  contains string 
196e7 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a 2a 2a 20  or blob data.** 
196e8 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72  and the third ar
196e9 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f  gument passed to
196ea 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
196eb 73 20 74 72 75 65 2c 20 74 68 65 20 0a 2a 2a 20  s true, the .** 
196ec 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20  current content 
196ed 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 70  of the cell is p
196ee 72 65 73 65 72 76 65 64 2e 20 4f 74 68 65 72 77  reserved. Otherw
196ef 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a 2a 20 62  ise, it may.** b
196f0 65 20 64 69 73 63 61 72 64 65 64 2e 20 20 0a 2a  e discarded.  .*
196f1 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
196f2 6f 6e 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f  on sets the MEM_
196f3 44 79 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65  Dyn flag and cle
196f4 61 72 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c  ars any xDel cal
196f5 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73  lback..** It als
196f6 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70 68  o clears MEM_Eph
196f7 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74 69  em and MEM_Stati
196f8 63 2e 20 49 66 20 74 68 65 20 70 72 65 73 65 72  c. If the preser
196f9 76 65 20 66 6c 61 67 20 69 73 20 0a 2a 2a 20 6e  ve flag is .** n
196fa 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 69 73  ot set, Mem.n is
196fb 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49   zeroed..*/.SQLI
196fc 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
196fd 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
196fe 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74  w(Mem *pMem, int
196ff 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65   n, int preserve
19700 29 7b 0a 20 20 61 73 73 65 72 74 28 20 31 20 3e  ){.  assert( 1 >
19701 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d  =.    ((pMem->zM
19702 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a  alloc && pMem->z
19703 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29  Malloc==pMem->z)
19704 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20 20 20 20   ? 1 : 0) +.    
19705 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  (((pMem->flags&M
19706 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d 2d 3e 78  EM_Dyn)&&pMem->x
19707 44 65 6c 29 20 3f 20 31 20 3a 20 30 29 20 2b 20  Del) ? 1 : 0) + 
19708 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61  .    ((pMem->fla
19709 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 20 3f 20  gs&MEM_Ephem) ? 
1970a 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28  1 : 0) + .    ((
1970b 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
1970c 53 74 61 74 69 63 29 20 3f 20 31 20 3a 20 30 29  Static) ? 1 : 0)
1970d 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  .  );.  assert( 
1970e 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pMem->flags&MEM
1970f 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a  _RowSet)==0 );..
19710 20 20 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d    if( n<32 ) n =
19711 20 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74   32;.  if( sqlit
19712 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  e3DbMallocSize(p
19713 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a  Mem->db, pMem->z
19714 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20  Malloc)<n ){.   
19715 20 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26   if( preserve &&
19716 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e   pMem->z==pMem->
19717 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20  zMalloc ){.     
19718 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d   pMem->z = pMem-
19719 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74  >zMalloc = sqlit
1971a 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65  e3DbReallocOrFre
1971b 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d  e(pMem->db, pMem
1971c 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70  ->z, n);.      p
1971d 72 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20  reserve = 0;.   
1971e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
1971f 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d  lite3DbFree(pMem
19720 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c  ->db, pMem->zMal
19721 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d  loc);.      pMem
19722 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69  ->zMalloc = sqli
19723 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
19724 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20  Mem->db, n);.   
19725 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d   }.  }..  if( pM
19726 65 6d 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76  em->z && preserv
19727 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  e && pMem->zMall
19728 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70  oc && pMem->z!=p
19729 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a  Mem->zMalloc ){.
1972a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d      memcpy(pMem-
1972b 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e  >zMalloc, pMem->
1972c 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d  z, pMem->n);.  }
1972d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  .  if( pMem->fla
1972e 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d  gs&MEM_Dyn && pM
1972f 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20  em->xDel ){.    
19730 70 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64  pMem->xDel((void
19731 20 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20   *)(pMem->z));. 
19732 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20   }..  pMem->z = 
19733 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20  pMem->zMalloc;. 
19734 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20   if( pMem->z==0 
19735 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ){.    pMem->fla
19736 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
19737 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d   }else{.    pMem
19738 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
19739 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69  _Ephem|MEM_Stati
1973a 63 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e  c);.  }.  pMem->
1973b 78 44 65 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75  xDel = 0;.  retu
1973c 72 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51  rn (pMem->z ? SQ
1973d 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
1973e 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  _NOMEM);.}../*.*
1973f 2a 20 4d 61 6b 65 20 74 68 65 20 67 69 76 65 6e  * Make the given
19740 20 4d 65 6d 20 6f 62 6a 65 63 74 20 4d 45 4d 5f   Mem object MEM_
19741 44 79 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  Dyn.  In other w
19742 6f 72 64 73 2c 20 6d 61 6b 65 20 69 74 20 73 6f  ords, make it so
19743 0a 2a 2a 20 74 68 61 74 20 61 6e 79 20 54 45 58  .** that any TEX
19744 54 20 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 65 6e  T or BLOB conten
19745 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d  t is stored in m
19746 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
19747 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e  rom.** malloc().
19748 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77    In this way, w
19749 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20  e know that the 
1974a 6d 65 6d 6f 72 79 20 69 73 20 73 61 66 65 20 74  memory is safe t
1974b 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74  o be.** overwrit
1974c 74 65 6e 20 6f 72 20 61 6c 74 65 72 65 64 2e 0a  ten or altered..
1974d 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
1974e 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
1974f 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  s or SQLITE_NOME
19750 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  M if malloc fail
19751 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
19752 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
19753 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65  VdbeMemMakeWrite
19754 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  able(Mem *pMem){
19755 0a 20 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65  .  int f;.  asse
19756 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
19757 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
19758 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
19759 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
1975a 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
1975b 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20  &MEM_RowSet)==0 
1975c 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28  );.  expandBlob(
1975d 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d 65  pMem);.  f = pMe
1975e 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20  m->flags;.  if( 
1975f 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  (f&(MEM_Str|MEM_
19760 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d 3e  Blob)) && pMem->
19761 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63  z!=pMem->zMalloc
19762 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
19763 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
19764 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32  Mem, pMem->n + 2
19765 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65  , 1) ){.      re
19766 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
19767 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65  M;.    }.    pMe
19768 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20  m->z[pMem->n] = 
19769 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70  0;.    pMem->z[p
1976a 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20  Mem->n+1] = 0;. 
1976b 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c     pMem->flags |
1976c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a  = MEM_Term;.  }.
1976d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1976e 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  _OK;.}../*.** If
1976f 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20   the given Mem* 
19770 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65  has a zero-fille
19771 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20  d tail, turn it 
19772 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79  into an ordinary
19773 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20  .** blob stored 
19774 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61  in dynamically a
19775 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a  llocated space..
19776 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
19777 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
19778 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19779 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
1977a 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20  mExpandBlob(Mem 
1977b 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d  *pMem){.  if( pM
1977c 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
1977d 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20  Zero ){.    int 
1977e 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72  nByte;.    asser
1977f 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  t( pMem->flags&M
19780 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61  EM_Blob );.    a
19781 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
19782 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  ags&MEM_RowSet)=
19783 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
19784 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
19785 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
19786 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
19787 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  tex) );..    /* 
19788 53 65 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65  Set nByte to the
19789 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1978a 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
1978b 72 65 20 74 68 65 20 65 78 70 61 6e 64 65 64 20  re the expanded 
1978c 62 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79  blob. */.    nBy
1978d 74 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70  te = pMem->n + p
1978e 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20  Mem->u.nZero;.  
1978f 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29    if( nByte<=0 )
19790 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20  {.      nByte = 
19791 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
19792 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
19793 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c  row(pMem, nByte,
19794 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74   1) ){.      ret
19795 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
19796 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d  ;.    }..    mem
19797 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65  set(&pMem->z[pMe
19798 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e  m->n], 0, pMem->
19799 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d  u.nZero);.    pM
1979a 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75  em->n += pMem->u
1979b 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d  .nZero;.    pMem
1979c 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
1979d 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b  _Zero|MEM_Term);
1979e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
1979f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
197a0 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  f.../*.** Make s
197a1 75 72 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65  ure the given Me
197a2 6d 20 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d  m is \u0000 term
197a3 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  inated..*/.SQLIT
197a4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
197a5 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54  lite3VdbeMemNulT
197a6 65 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d  erminate(Mem *pM
197a7 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  em){.  assert( p
197a8 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
197a9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
197aa 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
197ab 29 20 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d  ) );.  if( (pMem
197ac 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65  ->flags & MEM_Te
197ad 72 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d  rm)!=0 || (pMem-
197ae 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72  >flags & MEM_Str
197af 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
197b0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20  rn SQLITE_OK;   
197b1 2f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f  /* Nothing to do
197b2 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71   */.  }.  if( sq
197b3 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
197b4 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32  (pMem, pMem->n+2
197b5 2c 20 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , 1) ){.    retu
197b6 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
197b7 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70  .  }.  pMem->z[p
197b8 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70  Mem->n] = 0;.  p
197b9 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31  Mem->z[pMem->n+1
197ba 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66  ] = 0;.  pMem->f
197bb 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d  lags |= MEM_Term
197bc 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
197bd 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
197be 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68  dd MEM_Str to th
197bf 65 20 73 65 74 20 6f 66 20 72 65 70 72 65 73 65  e set of represe
197c0 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65  ntations for the
197c1 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d   given Mem.  Num
197c2 62 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76  bers.** are conv
197c3 65 72 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69  erted using sqli
197c4 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20  te3_snprintf(). 
197c5 20 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c   Converting a BL
197c6 4f 42 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a  OB to a string.*
197c7 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  * is a no-op..**
197c8 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70  .** Existing rep
197c9 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d  resentations MEM
197ca 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61  _Int and MEM_Rea
197cb 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61  l are *not* inva
197cc 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41  lidated..**.** A
197cd 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20   MEM_Null value 
197ce 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61  will never be pa
197cf 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e  ssed to this fun
197d0 63 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63  ction. This func
197d1 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20  tion is.** used 
197d2 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76  for converting v
197d3 61 6c 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f  alues to text fo
197d4 72 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74  r returning to t
197d5 68 65 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69  he user (i.e. vi
197d6 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c  a.** sqlite3_val
197d7 75 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66  ue_text()), or f
197d8 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74  or ensuring that
197d9 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73   values to be us
197da 65 64 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b  ed as btree.** k
197db 65 79 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e  eys are strings.
197dc 20 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63   In the former c
197dd 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ase a NULL point
197de 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  er is returned t
197df 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74  he.** user and t
197e0 68 65 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69  he later is an i
197e1 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d  nternal programm
197e2 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51  ing error..*/.SQ
197e3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
197e4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
197e5 74 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d  tringify(Mem *pM
197e6 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20  em, int enc){.  
197e7 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
197e8 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70  OK;.  int fg = p
197e9 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f  Mem->flags;.  co
197ea 6e 73 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20  nst int nByte = 
197eb 33 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  32;..  assert( p
197ec 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
197ed 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
197ee 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
197ef 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ) );.  assert( !
197f0 28 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b  (fg&MEM_Zero) );
197f1 0a 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26  .  assert( !(fg&
197f2 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
197f3 62 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  b)) );.  assert(
197f4 20 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d   fg&(MEM_Int|MEM
197f5 5f 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65  _Real) );.  asse
197f6 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
197f7 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20  &MEM_RowSet)==0 
197f8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  );.  assert( EIG
197f9 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
197fa 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69  T(pMem) );...  i
197fb 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
197fc 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74  mGrow(pMem, nByt
197fd 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74  e, 0) ){.    ret
197fe 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
197ff 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20  ;.  }..  /* For 
19800 61 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65  a Real or Intege
19801 72 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d  r, use sqlite3_m
19802 70 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64  printf() to prod
19803 75 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20  uce the UTF-8.  
19804 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73  ** string repres
19805 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
19806 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20  value. Then, if 
19807 74 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63  the required enc
19808 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54  oding.  ** is UT
19809 46 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36  F-16le or UTF-16
1980a 62 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74  be do a translat
1980b 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20  ion..  ** .  ** 
1980c 46 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64  FIX ME: It would
1980d 20 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71   be better if sq
1980e 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
1980f 20 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36   could do UTF-16
19810 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20  ..  */.  if( fg 
19811 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
19812 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
19813 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a  f(nByte, pMem->z
19814 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e  , "%lld", pMem->
19815 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  u.i);.  }else{. 
19816 20 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20     assert( fg & 
19817 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20  MEM_Real );.    
19818 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
19819 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c  (nByte, pMem->z,
1981a 20 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d   "%!.15g", pMem-
1981b 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d  >r);.  }.  pMem-
1981c 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  >n = sqlite3Strl
1981d 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20  en30(pMem->z);. 
1981e 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c   pMem->enc = SQL
1981f 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d  ITE_UTF8;.  pMem
19820 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53  ->flags |= MEM_S
19821 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73  tr|MEM_Term;.  s
19822 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
19823 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65  Encoding(pMem, e
19824 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  nc);.  return rc
19825 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72  ;.}../*.** Memor
19826 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74  y cell pMem cont
19827 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74  ains the context
19828 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65   of an aggregate
19829 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68   function..** Th
1982a 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73  is routine calls
1982b 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65   the finalize me
1982c 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75  thod for that fu
1982d 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20  nction.  The.** 
1982e 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67  result of the ag
1982f 67 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65  gregate is store
19830 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d  d back into pMem
19831 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
19832 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74  QLITE_ERROR if t
19833 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70  he finalizer rep
19834 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20  orts an error.  
19835 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68  SQLITE_OK.** oth
19836 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erwise..*/.SQLIT
19837 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19838 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61  lite3VdbeMemFina
19839 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  lize(Mem *pMem, 
1983a 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b  FuncDef *pFunc){
1983b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1983c 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57  TE_OK;.  if( ALW
1983d 41 59 53 28 70 46 75 6e 63 20 26 26 20 70 46 75  AYS(pFunc && pFu
1983e 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29  nc->xFinalize) )
1983f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  {.    sqlite3_co
19840 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61  ntext ctx;.    a
19841 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
19842 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21  ags & MEM_Null)!
19843 3d 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65  =0 || pFunc==pMe
19844 6d 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20  m->u.pDef );.   
19845 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
19846 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
19847 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
19848 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
19849 20 20 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20     memset(&ctx, 
1984a 30 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b  0, sizeof(ctx));
1984b 0a 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73  .    ctx.s.flags
1984c 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
1984d 20 63 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d   ctx.s.db = pMem
1984e 2d 3e 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d  ->db;.    ctx.pM
1984f 65 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63  em = pMem;.    c
19850 74 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63  tx.pFunc = pFunc
19851 3b 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69  ;.    pFunc->xFi
19852 6e 61 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20  nalize(&ctx);.  
19853 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 4d    assert( 0==(pM
19854 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79  em->flags&MEM_Dy
19855 6e 29 20 26 26 20 21 70 4d 65 6d 2d 3e 78 44 65  n) && !pMem->xDe
19856 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  l );.    sqlite3
19857 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c  DbFree(pMem->db,
19858 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b   pMem->zMalloc);
19859 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d  .    memcpy(pMem
1985a 2c 20 26 63 74 78 2e 73 2c 20 73 69 7a 65 6f 66  , &ctx.s, sizeof
1985b 28 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63  (ctx.s));.    rc
1985c 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a   = ctx.isError;.
1985d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1985e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
1985f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e   memory cell con
19860 74 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76  tains a string v
19861 61 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20 62  alue that must b
19862 65 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e  e freed by.** in
19863 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e  voking an extern
19864 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65  al callback, fre
19865 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e  e it now. Callin
19866 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  g this function.
19867 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65  ** does not free
19868 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63   any Mem.zMalloc
19869 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49   buffer..*/.SQLI
1986a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1986b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1986c 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65  leaseExternal(Me
1986d 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  m *p){.  assert(
1986e 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c   p->db==0 || sql
1986f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19870 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
19871 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e  .  testcase( p->
19872 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 20  flags & MEM_Agg 
19873 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  );.  testcase( p
19874 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79  ->flags & MEM_Dy
19875 6e 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  n );.  testcase(
19876 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   p->flags & MEM_
19877 52 6f 77 53 65 74 20 29 3b 0a 20 20 74 65 73 74  RowSet );.  test
19878 63 61 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26  case( p->flags &
19879 20 4d 45 4d 5f 46 72 61 6d 65 20 29 3b 0a 20 20   MEM_Frame );.  
1987a 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45  if( p->flags&(ME
1987b 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45  M_Agg|MEM_Dyn|ME
1987c 4d 5f 52 6f 77 53 65 74 7c 4d 45 4d 5f 46 72 61  M_RowSet|MEM_Fra
1987d 6d 65 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70  me) ){.    if( p
1987e 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20  ->flags&MEM_Agg 
1987f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
19880 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28  VdbeMemFinalize(
19881 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20  p, p->u.pDef);. 
19882 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d       assert( (p-
19883 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67  >flags & MEM_Agg
19884 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  )==0 );.      sq
19885 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19886 61 73 65 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73  ase(p);.    }els
19887 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d  e if( p->flags&M
19888 45 4d 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65  EM_Dyn && p->xDe
19889 6c 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  l ){.      asser
1988a 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d  t( (p->flags&MEM
1988b 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
1988c 20 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f       p->xDel((vo
1988d 69 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20  id *)p->z);.    
1988e 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20    p->xDel = 0;. 
1988f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
19890 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74  flags&MEM_RowSet
19891 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
19892 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 2d 3e  3RowSetClear(p->
19893 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20  u.pRowSet);.    
19894 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61  }else if( p->fla
19895 67 73 26 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a  gs&MEM_Frame ){.
19896 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19897 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b 0a  eMemSetNull(p);.
19898 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
19899 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d  ** Release any m
1989a 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68  emory held by th
1989b 65 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20  e Mem. This may 
1989c 6c 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e  leave the Mem in
1989d 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74   an.** inconsist
1989e 65 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65  ent state, for e
1989f 78 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d  xample with (Mem
198a0 2e 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d  .z==0) and.** (M
198a1 65 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f  em.type==SQLITE_
198a2 54 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  TEXT)..*/.SQLITE
198a3 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
198a4 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
198a5 61 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 73  ase(Mem *p){.  s
198a6 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
198a7 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 29 3b  easeExternal(p);
198a8 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
198a9 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c  (p->db, p->zMall
198aa 6f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b  oc);.  p->z = 0;
198ab 0a 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  .  p->zMalloc = 
198ac 30 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30  0;.  p->xDel = 0
198ad 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
198ae 72 74 20 61 20 36 34 2d 62 69 74 20 49 45 45 45  rt a 64-bit IEEE
198af 20 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36   double into a 6
198b0 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  4-bit signed int
198b1 65 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20  eger..** If the 
198b2 64 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61  double is too la
198b3 72 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30  rge, return 0x80
198b4 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a  00000000000000..
198b5 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65  **.** Most syste
198b6 6d 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20  ms appear to do 
198b7 74 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61  this simply by a
198b8 73 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69  ssigning.** vari
198b9 61 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75  ables and withou
198ba 74 20 74 68 65 20 65 78 74 72 61 20 72 61 6e 67  t the extra rang
198bb 65 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a  e tests.  But.**
198bc 20 74 68 65 72 65 20 61 72 65 20 72 65 70 6f 72   there are repor
198bd 74 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20  ts that windows 
198be 74 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74  throws an expect
198bf 69 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c  ion.** if the fl
198c0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
198c1 75 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e  ue is out of ran
198c2 67 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20  ge. (See ticket 
198c3 23 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75  #2880.).** Becau
198c4 73 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d  se we do not com
198c5 70 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61  pletely understa
198c6 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20  nd the problem, 
198c7 77 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20  we will.** take 
198c8 74 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65  the conservative
198c9 20 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c   approach and al
198ca 77 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65  ways do range te
198cb 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74  sts.** before at
198cc 74 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e  tempting the con
198cd 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  version..*/.stat
198ce 69 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49  ic i64 doubleToI
198cf 6e 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a  nt64(double r){.
198d0 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63    /*.  ** Many c
198d1 6f 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f  ompilers we enco
198d2 75 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66  unter do not def
198d3 69 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f  ine constants fo
198d4 72 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d  r the.  ** minim
198d5 75 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36  um and maximum 6
198d6 34 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20  4-bit integers, 
198d7 6f 72 20 74 68 65 79 20 64 65 66 69 6e 65 20 74  or they define t
198d8 68 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69  hem.  ** inconsi
198d9 73 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61  stently.  And ma
198da 6e 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73  ny do not unders
198db 74 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f  tand the "LL" no
198dc 74 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20  tation..  ** So 
198dd 77 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77  we define our ow
198de 6e 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e  n static constan
198df 74 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f  ts here using no
198e0 74 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65  thing.  ** large
198e1 72 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20  r than a 32-bit 
198e2 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
198e3 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  ..  */.  static 
198e4 63 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74  const i64 maxInt
198e5 20 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34   = LARGEST_INT64
198e6 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
198e7 20 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d   i64 minInt = SM
198e8 41 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20  ALLEST_INT64;.. 
198e9 20 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d   if( r<(double)m
198ea 69 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74  inInt ){.    ret
198eb 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65  urn minInt;.  }e
198ec 6c 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c  lse if( r>(doubl
198ed 65 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20  e)maxInt ){.    
198ee 2f 2a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72  /* minInt is cor
198ef 72 65 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20  rect here - not 
198f0 6d 61 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e  maxInt.  It turn
198f1 73 20 6f 75 74 20 74 68 61 74 20 61 73 73 69 67  s out that assig
198f2 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 65  ning.    ** a ve
198f3 72 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76  ry large positiv
198f4 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69  e number to an i
198f5 6e 74 65 67 65 72 20 72 65 73 75 6c 74 73 20 69  nteger results i
198f6 6e 20 61 20 76 65 72 79 20 6c 61 72 67 65 0a 20  n a very large. 
198f7 20 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69     ** negative i
198f8 6e 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61  nteger.  This ma
198f9 6b 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75  kes no sense, bu
198fa 74 20 69 74 20 69 73 20 77 68 61 74 20 78 38 36  t it is what x86
198fb 20 68 61 72 64 77 61 72 65 0a 20 20 20 20 2a 2a   hardware.    **
198fc 20 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f 6d   does so for com
198fd 70 61 74 69 62 69 6c 69 74 79 20 77 65 20 77 69  patibility we wi
198fe 6c 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20 69  ll do the same i
198ff 6e 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a 20  n software. */. 
19900 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74     return minInt
19901 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
19902 65 74 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 20  eturn (i64)r;.  
19903 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
19904 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69  n some kind of i
19905 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 68 69  nteger value whi
19906 63 68 20 69 73 20 74 68 65 20 62 65 73 74 20 77  ch is the best w
19907 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72  e can do.** at r
19908 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
19909 76 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 6d  value that *pMem
1990a 20 64 65 73 63 72 69 62 65 73 20 61 73 20 61 6e   describes as an
1990b 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20   integer..** If 
1990c 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 67  pMem is an integ
1990d 65 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  er, then the val
1990e 75 65 20 69 73 20 65 78 61 63 74 2e 20 20 49 66  ue is exact.  If
1990f 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c   pMem is.** a fl
19910 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65  oating-point the
19911 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  n the value retu
19912 72 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74 65  rned is the inte
19913 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20  ger part..** If 
19914 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e 67  pMem is a string
19915 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77   or blob, then w
19916 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d 70  e make an attemp
19917 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20  t to convert.** 
19918 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 65  it into a intege
19919 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 61  r and return tha
1991a 74 2e 20 20 49 66 20 70 4d 65 6d 20 72 65 70 72  t.  If pMem repr
1991b 65 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e 20  esents an.** an 
1991c 53 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c 20  SQL-NULL value, 
1991d 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20  return 0..**.** 
1991e 49 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e  If pMem represen
1991f 74 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75  ts a string valu
19920 65 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20  e, its encoding 
19921 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 64  might be changed
19922 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19923 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56  ATE i64 sqlite3V
19924 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20  dbeIntValue(Mem 
19925 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c  *pMem){.  int fl
19926 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ags;.  assert( p
19927 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
19928 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
19929 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
1992a 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45  ) );.  assert( E
1992b 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d  IGHT_BYTE_ALIGNM
1992c 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 66  ENT(pMem) );.  f
1992d 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61  lags = pMem->fla
1992e 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20  gs;.  if( flags 
1992f 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
19930 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e   return pMem->u.
19931 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  i;.  }else if( f
19932 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
19933 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f  ){.    return do
19934 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d  ubleToInt64(pMem
19935 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ->r);.  }else if
19936 28 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53  ( flags & (MEM_S
19937 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a  tr|MEM_Blob) ){.
19938 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20      i64 value;. 
19939 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c     pMem->flags |
1993a 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69  = MEM_Str;.    i
1993b 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  f( sqlite3VdbeCh
1993c 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65  angeEncoding(pMe
1993d 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a  m, SQLITE_UTF8).
1993e 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
1993f 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
19940 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20  nate(pMem) ){.  
19941 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
19942 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
19943 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73  pMem->z );.    s
19944 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65  qlite3Atoi64(pMe
19945 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20  m->z, &value);. 
19946 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b     return value;
19947 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
19948 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 0;.  }.}../
19949 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1994a 62 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74  best representat
1994b 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74  ion of pMem that
1994c 20 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f   we can get into
1994d 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49   a.** double.  I
1994e 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64  f pMem is alread
1994f 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e  y a double or an
19950 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e   integer, return
19951 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20   its.** value.  
19952 49 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e  If it is a strin
19953 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74  g or blob, try t
19954 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20  o convert it to 
19955 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20  a double..** If 
19956 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65  it is a NULL, re
19957 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c  turn 0.0..*/.SQL
19958 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62  ITE_PRIVATE doub
19959 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  le sqlite3VdbeRe
1995a 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65  alValue(Mem *pMe
1995b 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  m){.  assert( pM
1995c 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
1995d 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1995e 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
1995f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49   );.  assert( EI
19960 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
19961 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66  NT(pMem) );.  if
19962 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
19963 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
19964 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a  return pMem->r;.
19965 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d    }else if( pMem
19966 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
19967 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
19968 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e  (double)pMem->u.
19969 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  i;.  }else if( p
1996a 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Mem->flags & (ME
1996b 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
1996c 29 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c  ){.    /* (doubl
1996d 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53  e)0 In case of S
1996e 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
1996f 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a  ING_POINT... */.
19970 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d      double val =
19971 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20   (double)0;.    
19972 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
19973 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20  EM_Str;.    if( 
19974 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
19975 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20  eEncoding(pMem, 
19976 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20  SQLITE_UTF8).   
19977 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64      || sqlite3Vd
19978 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74  beMemNulTerminat
19979 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20  e(pMem) ){.     
1997a 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e   /* (double)0 In
1997b 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f   case of SQLITE_
1997c 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
1997d 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20  INT... */.      
1997e 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 30  return (double)0
1997f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
19980 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20  rt( pMem->z );. 
19981 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70     sqlite3AtoF(p
19982 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20  Mem->z, &val);. 
19983 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20     return val;. 
19984 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28   }else{.    /* (
19985 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65  double)0 In case
19986 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   of SQLITE_OMIT_
19987 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e  FLOATING_POINT..
19988 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
19989 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a 7d  (double)0;.  }.}
1998a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20  ../*.** The MEM 
1998b 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 72  structure is alr
1998c 65 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e  eady a MEM_Real.
1998d 20 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61    Try to also ma
1998e 6b 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49  ke it a.** MEM_I
1998f 6e 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f  nt if we can..*/
19990 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19991 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
19992 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28  IntegerAffinity(
19993 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73  Mem *pMem){.  as
19994 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  sert( pMem->flag
19995 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a  s & MEM_Real );.
19996 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
19997 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
19998 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Set)==0 );.  ass
19999 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30  ert( pMem->db==0
1999a 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
1999b 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d  x_held(pMem->db-
1999c 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
1999d 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
1999e 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20  ALIGNMENT(pMem) 
1999f 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20  );..  pMem->u.i 
199a0 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28  = doubleToInt64(
199a1 70 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a 20  pMem->r);..  /* 
199a2 4f 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 61  Only mark the va
199a3 6c 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67 65  lue as an intege
199a4 72 20 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  r if.  **.  **  
199a5 20 20 28 31 29 20 74 68 65 20 72 6f 75 6e 64 2d    (1) the round-
199a6 74 72 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20  trip conversion 
199a7 72 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c 20  real->int->real 
199a8 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 0a  is a no-op, and.
199a9 20 20 2a 2a 20 20 20 20 28 32 29 20 54 68 65 20    **    (2) The 
199aa 69 6e 74 65 67 65 72 20 69 73 20 6e 65 69 74 68  integer is neith
199ab 65 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 6e  er the largest n
199ac 6f 72 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 0a  or the smallest.
199ad 20 20 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 73    **        poss
199ae 69 62 6c 65 20 69 6e 74 65 67 65 72 20 28 74 69  ible integer (ti
199af 63 6b 65 74 20 23 33 39 32 32 29 0a 20 20 2a 2a  cket #3922).  **
199b0 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  .  ** The second
199b1 20 61 6e 64 20 74 68 69 72 64 20 74 65 72 6d 73   and third terms
199b2 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   in the followin
199b3 67 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 6e  g conditional en
199b4 66 6f 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 20  forces.  ** the 
199b5 73 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e  second condition
199b6 20 75 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d   under the assum
199b7 70 74 69 6f 6e 20 74 68 61 74 20 61 64 64 69 74  ption that addit
199b8 69 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 75  ion overflow cau
199b9 73 65 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 20  ses.  ** values 
199ba 74 6f 20 77 72 61 70 20 61 72 6f 75 6e 64 2e 20  to wrap around. 
199bb 20 4f 6e 20 78 38 36 20 68 61 72 64 77 61 72 65   On x86 hardware
199bc 2c 20 74 68 65 20 74 68 69 72 64 20 74 65 72 6d  , the third term
199bd 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20   is always.  ** 
199be 74 72 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 62  true and could b
199bf 65 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20  e omitted.  But 
199c0 77 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20 62  we leave it in b
199c1 65 63 61 75 73 65 20 6f 74 68 65 72 0a 20 20 2a  ecause other.  *
199c2 2a 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20  * architectures 
199c3 6d 69 67 68 74 20 62 65 68 61 76 65 20 64 69 66  might behave dif
199c4 66 65 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a 20  ferently..  */. 
199c5 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64   if( pMem->r==(d
199c6 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20  ouble)pMem->u.i 
199c7 26 26 20 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41  && pMem->u.i>SMA
199c8 4c 4c 45 53 54 5f 49 4e 54 36 34 0a 20 20 20 20  LLEST_INT64.    
199c9 20 20 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d    && ALWAYS(pMem
199ca 2d 3e 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e  ->u.i<LARGEST_IN
199cb 54 36 34 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d  T64) ){.    pMem
199cc 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49  ->flags |= MEM_I
199cd 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  nt;.  }.}../*.**
199ce 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f   Convert pMem to
199cf 20 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20   type integer.  
199d0 49 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70  Invalidate any p
199d1 72 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74  rior representat
199d2 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
199d3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
199d4 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
199d5 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  rify(Mem *pMem){
199d6 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
199d7 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
199d8 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65  3_mutex_held(pMe
199d9 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  m->db->mutex) );
199da 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
199db 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
199dc 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73  wSet)==0 );.  as
199dd 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45  sert( EIGHT_BYTE
199de 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29  _ALIGNMENT(pMem)
199df 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69   );..  pMem->u.i
199e0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e   = sqlite3VdbeIn
199e1 74 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20  tValue(pMem);.  
199e2 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
199e3 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Mem, MEM_Int);. 
199e4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
199e5 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  K;.}../*.** Conv
199e6 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74  ert pMem so that
199e7 20 69 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d   it is of type M
199e8 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61  EM_Real..** Inva
199e9 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72  lidate any prior
199ea 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73   representations
199eb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
199ec 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
199ed 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65  dbeMemRealify(Me
199ee 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65  m *pMem){.  asse
199ef 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
199f0 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
199f1 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
199f2 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
199f3 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
199f4 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29  LIGNMENT(pMem) )
199f5 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73  ;..  pMem->r = s
199f6 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
199f7 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d  lue(pMem);.  Mem
199f8 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d  SetTypeFlag(pMem
199f9 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72  , MEM_Real);.  r
199fa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
199fb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
199fc 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69  t pMem so that i
199fd 74 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f  t has types MEM_
199fe 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20  Real or MEM_Int 
199ff 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61  or both..** Inva
19a00 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72  lidate any prior
19a01 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73   representations
19a02 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19a03 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
19a04 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d  dbeMemNumerify(M
19a05 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75  em *pMem){.  dou
19a06 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36  ble r1, r2;.  i6
19a07 34 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28  4 i;.  assert( (
19a08 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d  pMem->flags & (M
19a09 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c  EM_Int|MEM_Real|
19a0a 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b  MEM_Null))==0 );
19a0b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
19a0c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42  ->flags & (MEM_B
19a0d 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30  lob|MEM_Str))!=0
19a0e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   );.  assert( pM
19a0f 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
19a10 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19a11 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
19a12 20 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74   );.  r1 = sqlit
19a13 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
19a14 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75  pMem);.  i = dou
19a15 62 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a  bleToInt64(r1);.
19a16 20 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69    r2 = (double)i
19a17 3b 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29  ;.  if( r1==r2 )
19a18 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
19a19 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70  eMemIntegerify(p
19a1a 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Mem);.  }else{. 
19a1b 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b     pMem->r = r1;
19a1c 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46  .    MemSetTypeF
19a1d 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65  lag(pMem, MEM_Re
19a1e 61 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  al);.  }.  retur
19a1f 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
19a20 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79  /*.** Delete any
19a21 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20   previous value 
19a22 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75  and set the valu
19a23 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65  e stored in *pMe
19a24 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51  m to NULL..*/.SQ
19a25 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19a26 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
19a27 53 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65  SetNull(Mem *pMe
19a28 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e  m){.  if( pMem->
19a29 66 6c 61 67 73 20 26 20 4d 45 4d 5f 46 72 61 6d  flags & MEM_Fram
19a2a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
19a2b 56 64 62 65 46 72 61 6d 65 44 65 6c 65 74 65 28  VdbeFrameDelete(
19a2c 70 4d 65 6d 2d 3e 75 2e 70 46 72 61 6d 65 29 3b  pMem->u.pFrame);
19a2d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d  .  }.  if( pMem-
19a2e 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
19a2f 53 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Set ){.    sqlit
19a30 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d  e3RowSetClear(pM
19a31 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a  em->u.pRowSet);.
19a32 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65    }.  MemSetType
19a33 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e  Flag(pMem, MEM_N
19a34 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79  ull);.  pMem->ty
19a35 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  pe = SQLITE_NULL
19a36 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
19a37 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76  e any previous v
19a38 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65  alue and set the
19a39 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42   value to be a B
19a3a 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a  LOB of length.**
19a3b 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c   n containing al
19a3c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49  l zeros..*/.SQLI
19a3d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19a3e 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
19a3f 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70  tZeroBlob(Mem *p
19a40 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73  Mem, int n){.  s
19a41 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
19a42 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d  ease(pMem);.  pM
19a43 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
19a44 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20  Blob|MEM_Zero;. 
19a45 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
19a46 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65  LITE_BLOB;.  pMe
19a47 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20  m->n = 0;.  if( 
19a48 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70  n<0 ) n = 0;.  p
19a49 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e  Mem->u.nZero = n
19a4a 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20  ;.  pMem->enc = 
19a4b 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 23 69  SQLITE_UTF8;..#i
19a4c 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
19a4d 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 73 71 6c 69  _INCRBLOB.  sqli
19a4e 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
19a4f 4d 65 6d 2c 20 6e 2c 20 30 29 3b 0a 20 20 69 66  Mem, n, 0);.  if
19a50 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20  ( pMem->z ){.   
19a51 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 20 20   pMem->n = n;.  
19a52 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a    memset(pMem->z
19a53 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 23 65 6e  , 0, n);.  }.#en
19a54 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  dif.}../*.** Del
19a55 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ete any previous
19a56 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74   value and set t
19a57 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  he value stored 
19a58 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c  in *pMem to val,
19a59 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70  .** manifest typ
19a5a 65 20 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51  e INTEGER..*/.SQ
19a5b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19a5c 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
19a5d 53 65 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d  SetInt64(Mem *pM
19a5e 65 6d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20  em, i64 val){.  
19a5f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
19a60 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70  lease(pMem);.  p
19a61 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a  Mem->u.i = val;.
19a62 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
19a63 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d  MEM_Int;.  pMem-
19a64 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49  >type = SQLITE_I
19a65 4e 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  NTEGER;.}../*.**
19a66 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76   Delete any prev
19a67 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73  ious value and s
19a68 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  et the value sto
19a69 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20  red in *pMem to 
19a6a 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74  val,.** manifest
19a6b 20 74 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53   type REAL..*/.S
19a6c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
19a6d 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
19a6e 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a  mSetDouble(Mem *
19a6f 70 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c  pMem, double val
19a70 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ){.  if( sqlite3
19a71 49 73 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20  IsNaN(val) ){.  
19a72 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19a73 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20  SetNull(pMem);. 
19a74 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
19a75 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
19a76 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65  e(pMem);.    pMe
19a77 6d 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20  m->r = val;.    
19a78 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
19a79 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d  M_Real;.    pMem
19a7a 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
19a7b 46 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  FLOAT;.  }.}../*
19a7c 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70  .** Delete any p
19a7d 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e  revious value an
19a7e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  d set the value 
19a7f 6f 66 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e  of pMem to be an
19a80 0a 2a 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61  .** empty boolea
19a81 6e 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49  n index..*/.SQLI
19a82 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19a83 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
19a84 74 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65  tRowSet(Mem *pMe
19a85 6d 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  m){.  sqlite3 *d
19a86 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20  b = pMem->db;.  
19a87 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b  assert( db!=0 );
19a88 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
19a89 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
19a8a 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71  wSet)==0 );.  sq
19a8b 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19a8c 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65  ase(pMem);.  pMe
19a8d 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c  m->zMalloc = sql
19a8e 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
19a8f 64 62 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64  db, 64);.  if( d
19a90 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
19a91 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ){.    pMem->fla
19a92 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
19a93 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
19a94 72 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  rt( pMem->zMallo
19a95 63 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75  c );.    pMem->u
19a96 2e 70 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74  .pRowSet = sqlit
19a97 65 33 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c  e3RowSetInit(db,
19a98 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20   pMem->zMalloc, 
19a99 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
19a9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19a9b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
19a9c 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
19a9d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b  pMem->zMalloc));
19a9e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
19a9f 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20  m->u.pRowSet!=0 
19aa0 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  );.    pMem->fla
19aa1 67 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b  gs = MEM_RowSet;
19aa2 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
19aa3 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
19aa4 20 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74   Mem object cont
19aa5 61 69 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42  ains a TEXT or B
19aa6 4c 4f 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74  LOB that is.** t
19aa7 6f 6f 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65  oo large - whose
19aa8 20 73 69 7a 65 20 65 78 63 65 65 64 73 20 53 51   size exceeds SQ
19aa9 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e  LITE_MAX_LENGTH.
19aaa 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19aab 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19aac 62 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20  beMemTooBig(Mem 
19aad 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
19aae 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28  ->db!=0 );.  if(
19aaf 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d   p->flags & (MEM
19ab0 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29  _Str|MEM_Blob) )
19ab1 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d  {.    int n = p-
19ab2 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66  >n;.    if( p->f
19ab3 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
19ab4 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d  ){.      n += p-
19ab5 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a  >u.nZero;.    }.
19ab6 20 20 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e      return n>p->
19ab7 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
19ab8 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b  E_LIMIT_LENGTH];
19ab9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
19aba 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20   .}../*.** Size 
19abb 6f 66 20 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f  of struct Mem no
19abc 74 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20  t including the 
19abd 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62  Mem.zMalloc memb
19abe 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  er..*/.#define M
19abf 45 4d 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65  EMCELLSIZE (size
19ac0 5f 74 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29  _t)(&(((Mem *)0)
19ac1 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a  ->zMalloc))../*.
19ac2 2a 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61 6c 6c  ** Make an shall
19ac3 6f 77 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d  ow copy of pFrom
19ac4 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f   into pTo.  Prio
19ac5 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a  r contents of.**
19ac6 20 70 54 6f 20 61 72 65 20 66 72 65 65 64 2e 20   pTo are freed. 
19ac7 20 54 68 65 20 70 46 72 6f 6d 2d 3e 7a 20 66 69   The pFrom->z fi
19ac8 65 6c 64 20 69 73 20 6e 6f 74 20 64 75 70 6c 69  eld is not dupli
19ac9 63 61 74 65 64 2e 20 20 49 66 0a 2a 2a 20 70 46  cated.  If.** pF
19aca 72 6f 6d 2d 3e 7a 20 69 73 20 75 73 65 64 2c 20  rom->z is used, 
19acb 74 68 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f 69 6e  then pTo->z poin
19acc 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74  ts to the same t
19acd 68 69 6e 67 20 61 73 20 70 46 72 6f 6d 2d 3e 7a  hing as pFrom->z
19ace 0a 2a 2a 20 61 6e 64 20 66 6c 61 67 73 20 67 65  .** and flags ge
19acf 74 73 20 73 72 63 54 79 70 65 20 28 65 69 74 68  ts srcType (eith
19ad0 65 72 20 4d 45 4d 5f 45 70 68 65 6d 20 6f 72 20  er MEM_Ephem or 
19ad1 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a 2a 2f 0a  MEM_Static)..*/.
19ad2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19ad3 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
19ad4 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65  emShallowCopy(Me
19ad5 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65  m *pTo, const Me
19ad6 6d 20 2a 70 46 72 6f 6d 2c 20 69 6e 74 20 73 72  m *pFrom, int sr
19ad7 63 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74  cType){.  assert
19ad8 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20  ( (pFrom->flags 
19ad9 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
19ada 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
19adb 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72  eMemReleaseExter
19adc 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63  nal(pTo);.  memc
19add 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d  py(pTo, pFrom, M
19ade 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70  EMCELLSIZE);.  p
19adf 54 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20  To->xDel = 0;.  
19ae0 69 66 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67  if( (pFrom->flag
19ae1 73 26 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c  s&MEM_Dyn)!=0 ||
19ae2 20 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d   pFrom->z==pFrom
19ae3 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20  ->zMalloc ){.   
19ae4 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e   pTo->flags &= ~
19ae5 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61  (MEM_Dyn|MEM_Sta
19ae6 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a  tic|MEM_Ephem);.
19ae7 20 20 20 20 61 73 73 65 72 74 28 20 73 72 63 54      assert( srcT
19ae8 79 70 65 3d 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c  ype==MEM_Ephem |
19ae9 7c 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 53  | srcType==MEM_S
19aea 74 61 74 69 63 20 29 3b 0a 20 20 20 20 70 54 6f  tatic );.    pTo
19aeb 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 72 63 54 79  ->flags |= srcTy
19aec 70 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  pe;.  }.}../*.**
19aed 20 4d 61 6b 65 20 61 20 66 75 6c 6c 20 63 6f 70   Make a full cop
19aee 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20  y of pFrom into 
19aef 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74  pTo.  Prior cont
19af0 65 6e 74 73 20 6f 66 20 70 54 6f 20 61 72 65 0a  ents of pTo are.
19af1 2a 2a 20 66 72 65 65 64 20 62 65 66 6f 72 65 20  ** freed before 
19af2 74 68 65 20 63 6f 70 79 20 69 73 20 6d 61 64 65  the copy is made
19af3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19af4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
19af5 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a  dbeMemCopy(Mem *
19af6 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a  pTo, const Mem *
19af7 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63  pFrom){.  int rc
19af8 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
19af9 20 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d   assert( (pFrom-
19afa 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
19afb 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c  Set)==0 );.  sql
19afc 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19afd 73 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b  seExternal(pTo);
19afe 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70  .  memcpy(pTo, p
19aff 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a  From, MEMCELLSIZ
19b00 45 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73  E);.  pTo->flags
19b01 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20   &= ~MEM_Dyn;.. 
19b02 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26   if( pTo->flags&
19b03 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
19b04 62 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d  b) ){.    if( 0=
19b05 3d 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d  =(pFrom->flags&M
19b06 45 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20  EM_Static) ){.  
19b07 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c      pTo->flags |
19b08 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20  = MEM_Ephem;.   
19b09 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
19b0a 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
19b0b 62 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a  ble(pTo);.    }.
19b0c 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
19b0d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  ;.}../*.** Trans
19b0e 66 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  fer the contents
19b0f 20 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f   of pFrom to pTo
19b10 2e 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76  . Any existing v
19b11 61 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a  alue in pTo is.*
19b12 2a 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f  * freed. If pFro
19b13 6d 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d  m contains ephem
19b14 65 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70  eral data, a cop
19b15 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a  y is made..**.**
19b16 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20   pFrom contains 
19b17 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e  an SQL NULL when
19b18 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
19b19 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  turns..*/.SQLITE
19b1a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
19b1b 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
19b1c 28 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a  (Mem *pTo, Mem *
19b1d 70 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74  pFrom){.  assert
19b1e 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c  ( pFrom->db==0 |
19b1f 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
19b20 68 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e  held(pFrom->db->
19b21 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
19b22 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c  rt( pTo->db==0 |
19b23 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
19b24 68 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75  held(pTo->db->mu
19b25 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
19b26 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c  ( pFrom->db==0 |
19b27 7c 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20  | pTo->db==0 || 
19b28 70 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e  pFrom->db==pTo->
19b29 64 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  db );..  sqlite3
19b2a 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
19b2b 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54  To);.  memcpy(pT
19b2c 6f 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66  o, pFrom, sizeof
19b2d 28 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d  (Mem));.  pFrom-
19b2e 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
19b2f 6c 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c  l;.  pFrom->xDel
19b30 20 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a   = 0;.  pFrom->z
19b31 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f  Malloc = 0;.}../
19b32 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
19b33 76 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74  value of a Mem t
19b34 6f 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72  o be a string or
19b35 20 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54   a BLOB..**.** T
19b36 68 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65  he memory manage
19b37 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65  ment strategy de
19b38 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c  pends on the val
19b39 75 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a  ue of the xDel.*
19b3a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20  * parameter. If 
19b3b 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64  the value passed
19b3c 20 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53   is SQLITE_TRANS
19b3d 49 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a  IENT, then the .
19b3e 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70  ** string is cop
19b3f 69 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73  ied into a (poss
19b40 69 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62  ibly existing) b
19b41 75 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79  uffer managed by
19b42 20 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72   the .** Mem str
19b43 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73  ucture. Otherwis
19b44 65 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20  e, any existing 
19b45 62 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20  buffer is freed 
19b46 61 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74  and the.** point
19b47 65 72 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a  er copied..**.**
19b48 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69   If the string i
19b49 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 69 66 20  s too large (if 
19b4a 69 74 20 65 78 63 65 65 64 73 20 74 68 65 20 53  it exceeds the S
19b4b 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
19b4c 54 48 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74  TH.** size limit
19b4d 29 20 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79  ) then no memory
19b4e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75   allocation occu
19b4f 72 73 2e 20 20 49 66 20 74 68 65 20 73 74 72 69  rs.  If the stri
19b50 6e 67 20 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f  ng can be.** sto
19b51 72 65 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f  red without allo
19b52 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74  cating memory, t
19b53 68 65 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61  hen it is.  If a
19b54 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
19b55 6f 6e 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65  on.** is require
19b56 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73  d to store the s
19b57 74 72 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75  tring, then valu
19b58 65 20 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63  e of pMem is unc
19b59 68 61 6e 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65  hanged.  In.** e
19b5a 69 74 68 65 72 20 63 61 73 65 2c 20 53 51 4c 49  ither case, SQLI
19b5b 54 45 5f 54 4f 4f 42 49 47 20 69 73 20 72 65 74  TE_TOOBIG is ret
19b5c 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
19b5d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19b5e 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
19b5f 72 28 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20  r(.  Mem *pMem, 
19b60 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f           /* Memo
19b61 72 79 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74  ry cell to set t
19b62 6f 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a  o string value *
19b63 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
19b64 7a 2c 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e  z,      /* Strin
19b65 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69  g pointer */.  i
19b66 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20  nt n,           
19b67 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73     /* Bytes in s
19b68 74 72 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69  tring, or negati
19b69 76 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20  ve */.  u8 enc, 
19b6a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
19b6b 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30  ncoding of z.  0
19b6c 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20   for BLOBs */.  
19b6d 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
19b6e 64 2a 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f  d*) /* Destructo
19b6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  r function */.){
19b70 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e  .  int nByte = n
19b71 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61  ;      /* New va
19b72 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20  lue for pMem->n 
19b73 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b  */.  int iLimit;
19b74 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
19b75 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69  mum allowed stri
19b76 6e 67 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20  ng or blob size 
19b77 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d  */.  u16 flags =
19b78 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20   0;      /* New 
19b79 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e  value for pMem->
19b7a 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65  flags */..  asse
19b7b 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
19b7c 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
19b7d 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
19b7e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
19b7f 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
19b80 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
19b81 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20  0 );..  /* If z 
19b82 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  is a NULL pointe
19b83 72 2c 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63  r, set pMem to c
19b84 6f 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55  ontain an SQL NU
19b85 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20  LL. */.  if( !z 
19b86 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
19b87 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65  beMemSetNull(pMe
19b88 6d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  m);.    return S
19b89 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
19b8a 20 69 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b   if( pMem->db ){
19b8b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d  .    iLimit = pM
19b8c 65 6d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  em->db->aLimit[S
19b8d 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
19b8e 54 48 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  TH];.  }else{.  
19b8f 20 20 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54    iLimit = SQLIT
19b90 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20  E_MAX_LENGTH;.  
19b91 7d 0a 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63  }.  flags = (enc
19b92 3d 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d  ==0?MEM_Blob:MEM
19b93 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79  _Str);.  if( nBy
19b94 74 65 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65  te<0 ){.    asse
19b95 72 74 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20  rt( enc!=0 );.  
19b96 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54    if( enc==SQLIT
19b97 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20  E_UTF8 ){.      
19b98 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79  for(nByte=0; nBy
19b99 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b  te<=iLimit && z[
19b9a 6e 42 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29  nByte]; nByte++)
19b9b 7b 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  {}.    }else{.  
19b9c 20 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b      for(nByte=0;
19b9d 20 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26   nByte<=iLimit &
19b9e 26 20 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b  & (z[nByte] | z[
19b9f 6e 42 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65  nByte+1]); nByte
19ba0 2b 3d 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20  +=2){}.    }.   
19ba1 20 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65   flags |= MEM_Te
19ba2 72 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  rm;.  }..  /* Th
19ba3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
19ba4 6b 20 73 65 74 73 20 74 68 65 20 6e 65 77 20 76  k sets the new v
19ba5 61 6c 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61  alues of Mem.z a
19ba6 6e 64 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a  nd Mem.xDel. It.
19ba7 20 20 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61    ** also sets a
19ba8 20 66 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76   flag in local v
19ba9 61 72 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20  ariable "flags" 
19baa 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20  to indicate the 
19bab 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61  memory.  ** mana
19bac 67 65 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d  gement (one of M
19bad 45 4d 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74  EM_Dyn or MEM_St
19bae 61 74 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66  atic)..  */.  if
19baf 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54  ( xDel==SQLITE_T
19bb0 52 41 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20  RANSIENT ){.    
19bb1 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79  int nAlloc = nBy
19bb2 74 65 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67  te;.    if( flag
19bb3 73 26 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20  s&MEM_Term ){.  
19bb4 20 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65      nAlloc += (e
19bb5 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f  nc==SQLITE_UTF8?
19bb6 31 3a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  1:2);.    }.    
19bb7 69 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74  if( nByte>iLimit
19bb8 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19bb9 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a   SQLITE_TOOBIG;.
19bba 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
19bbb 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
19bbc 28 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30  (pMem, nAlloc, 0
19bbd 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
19bbe 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
19bbf 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79      }.    memcpy
19bc0 28 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c  (pMem->z, z, nAl
19bc1 6c 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  loc);.  }else if
19bc2 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44  ( xDel==SQLITE_D
19bc3 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71  YNAMIC ){.    sq
19bc4 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19bc5 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70  ase(pMem);.    p
19bc6 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70  Mem->zMalloc = p
19bc7 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a  Mem->z = (char *
19bc8 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44  )z;.    pMem->xD
19bc9 65 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  el = 0;.  }else{
19bca 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19bcb 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
19bcc 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20  ;.    pMem->z = 
19bcd 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70  (char *)z;.    p
19bce 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c  Mem->xDel = xDel
19bcf 3b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28  ;.    flags |= (
19bd0 28 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54  (xDel==SQLITE_ST
19bd1 41 54 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63  ATIC)?MEM_Static
19bd2 3a 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a  :MEM_Dyn);.  }..
19bd3 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74    pMem->n = nByt
19bd4 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  e;.  pMem->flags
19bd5 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d   = flags;.  pMem
19bd6 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20  ->enc = (enc==0 
19bd7 3f 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20  ? SQLITE_UTF8 : 
19bd8 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79  enc);.  pMem->ty
19bd9 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53  pe = (enc==0 ? S
19bda 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c  QLITE_BLOB : SQL
19bdb 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e  ITE_TEXT);..#ifn
19bdc 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19bdd 55 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d  UTF16.  if( pMem
19bde 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54  ->enc!=SQLITE_UT
19bdf 46 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62  F8 && sqlite3Vdb
19be0 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d  eMemHandleBom(pM
19be1 65 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  em) ){.    retur
19be2 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
19be3 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
19be4 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29  ( nByte>iLimit )
19be5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
19be6 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a  ITE_TOOBIG;.  }.
19be7 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
19be8 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  _OK;.}../*.** Co
19be9 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73  mpare the values
19bea 20 63 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68   contained by th
19beb 65 20 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c  e two memory cel
19bec 6c 73 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a  ls, returning.**
19bed 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20   negative, zero 
19bee 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70  or positive if p
19bef 4d 65 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61  Mem1 is less tha
19bf0 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20  n, equal to, or 
19bf1 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20  greater.** than 
19bf2 70 4d 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f  pMem2. Sorting o
19bf3 72 64 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66  rder is NULL's f
19bf4 69 72 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62  irst, followed b
19bf5 79 20 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67  y numbers (integ
19bf6 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73  ers.** and reals
19bf7 29 20 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63  ) sorted numeric
19bf8 61 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62  ally, followed b
19bf9 79 20 74 65 78 74 20 6f 72 64 65 72 65 64 20 62  y text ordered b
19bfa 79 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a  y the collating.
19bfb 2a 2a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c  ** sequence pCol
19bfc 6c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c  l and finally bl
19bfd 6f 62 27 73 20 6f 72 64 65 72 65 64 20 62 79 20  ob's ordered by 
19bfe 6d 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20  memcmp()..**.** 
19bff 54 77 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20  Two NULL values 
19c00 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65  are considered e
19c01 71 75 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e  qual by this fun
19c02 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
19c03 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19c04 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63  ite3MemCompare(c
19c05 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c  onst Mem *pMem1,
19c06 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d   const Mem *pMem
19c07 32 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71  2, const CollSeq
19c08 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20   *pColl){.  int 
19c09 72 63 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32  rc;.  int f1, f2
19c0a 3b 0a 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64  ;.  int combined
19c0b 5f 66 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 6e  _flags;..  /* In
19c0c 74 65 72 63 68 61 6e 67 65 20 70 4d 65 6d 31 20  terchange pMem1 
19c0d 61 6e 64 20 70 4d 65 6d 32 20 69 66 20 74 68 65  and pMem2 if the
19c0e 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
19c0f 6e 63 65 20 73 70 65 63 69 66 69 65 73 0a 20 20  nce specifies.  
19c10 2a 2a 20 44 45 53 43 20 6f 72 64 65 72 2e 0a 20  ** DESC order.. 
19c11 20 2a 2f 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31   */.  f1 = pMem1
19c12 2d 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20  ->flags;.  f2 = 
19c13 70 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20  pMem2->flags;.  
19c14 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d  combined_flags =
19c15 20 66 31 7c 66 32 3b 0a 20 20 61 73 73 65 72 74   f1|f2;.  assert
19c16 28 20 28 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  ( (combined_flag
19c17 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
19c18 3d 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20  =0 );. .  /* If 
19c19 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 4e 55 4c  one value is NUL
19c1a 4c 2c 20 69 74 20 69 73 20 6c 65 73 73 20 74 68  L, it is less th
19c1b 61 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 49 66  an the other. If
19c1c 20 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 20 2a   both values.  *
19c1d 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75  * are NULL, retu
19c1e 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rn 0..  */.  if(
19c1f 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26   combined_flags&
19c20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
19c21 72 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e  return (f2&MEM_N
19c22 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e  ull) - (f1&MEM_N
19c23 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ull);.  }..  /* 
19c24 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20  If one value is 
19c25 61 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65  a number and the
19c26 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c 20 74   other is not, t
19c27 68 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73  he number is les
19c28 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20  s..  ** If both 
19c29 61 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d  are numbers, com
19c2a 70 61 72 65 20 61 73 20 72 65 61 6c 73 20 69 66  pare as reals if
19c2b 20 6f 6e 65 20 69 73 20 61 20 72 65 61 6c 2c 20   one is a real, 
19c2c 6f 72 20 61 73 20 69 6e 74 65 67 65 72 73 0a 20  or as integers. 
19c2d 20 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 6c 75   ** if both valu
19c2e 65 73 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e  es are integers.
19c2f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62  .  */.  if( comb
19c30 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f  ined_flags&(MEM_
19c31 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b  Int|MEM_Real) ){
19c32 0a 20 20 20 20 69 66 28 20 21 28 66 31 26 28 4d  .    if( !(f1&(M
19c33 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29  EM_Int|MEM_Real)
19c34 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
19c35 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
19c36 66 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74  f( !(f2&(MEM_Int
19c37 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20  |MEM_Real)) ){. 
19c38 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a       return -1;.
19c39 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66      }.    if( (f
19c3a 31 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74  1 & f2 & MEM_Int
19c3b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f  )==0 ){.      do
19c3c 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 20  uble r1, r2;.   
19c3d 20 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f 52     if( (f1&MEM_R
19c3e 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  eal)==0 ){.     
19c3f 20 20 20 72 31 20 3d 20 28 64 6f 75 62 6c 65 29     r1 = (double)
19c40 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 20  pMem1->u.i;.    
19c41 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
19c42 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b 0a   r1 = pMem1->r;.
19c43 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
19c44 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c 29 3d  ( (f2&MEM_Real)=
19c45 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 32  =0 ){.        r2
19c46 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 32   = (double)pMem2
19c47 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c  ->u.i;.      }el
19c48 73 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d  se{.        r2 =
19c49 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20   pMem2->r;.     
19c4a 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 31 3c   }.      if( r1<
19c4b 72 32 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a  r2 ) return -1;.
19c4c 20 20 20 20 20 20 69 66 28 20 72 31 3e 72 32 20        if( r1>r2 
19c4d 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
19c4e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
19c4f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
19c50 65 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20  ert( f1&MEM_Int 
19c51 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
19c52 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20   f2&MEM_Int );. 
19c53 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e       if( pMem1->
19c54 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69  u.i < pMem2->u.i
19c55 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
19c56 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75      if( pMem1->u
19c57 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20  .i > pMem2->u.i 
19c58 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
19c59 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
19c5a 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f  }.  }..  /* If o
19c5b 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74  ne value is a st
19c5c 72 69 6e 67 20 61 6e 64 20 74 68 65 20 6f 74 68  ring and the oth
19c5d 65 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68  er is a blob, th
19c5e 65 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73  e string is less
19c5f 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61  ..  ** If both a
19c60 72 65 20 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70  re strings, comp
19c61 61 72 65 20 75 73 69 6e 67 20 74 68 65 20 63 6f  are using the co
19c62 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
19c63 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f  s..  */.  if( co
19c64 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d  mbined_flags&MEM
19c65 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 66 28 20  _Str ){.    if( 
19c66 28 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d  (f1 & MEM_Str)==
19c67 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
19c68 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
19c69 66 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 74 72  f( (f2 & MEM_Str
19c6a 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  )==0 ){.      re
19c6b 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a  turn -1;.    }..
19c6c 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
19c6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65  1->enc==pMem2->e
19c6e 6e 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  nc );.    assert
19c6f 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51  ( pMem1->enc==SQ
19c70 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20  LITE_UTF8 || .  
19c71 20 20 20 20 20 20 20 20 20 20 70 4d 65 6d 31 2d            pMem1-
19c72 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  >enc==SQLITE_UTF
19c73 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65  16LE || pMem1->e
19c74 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
19c75 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68  BE );..    /* Th
19c76 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
19c77 65 6e 63 65 20 6d 75 73 74 20 62 65 20 64 65 66  ence must be def
19c78 69 6e 65 64 20 61 74 20 74 68 69 73 20 70 6f 69  ined at this poi
19c79 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20  nt, even if.    
19c7a 2a 2a 20 74 68 65 20 75 73 65 72 20 64 65 6c 65  ** the user dele
19c7b 74 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  tes the collatio
19c7c 6e 20 73 65 71 75 65 6e 63 65 20 61 66 74 65 72  n sequence after
19c7d 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61   the vdbe progra
19c7e 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70  m is.    ** comp
19c7f 69 6c 65 64 20 28 74 68 69 73 20 77 61 73 20 6e  iled (this was n
19c80 6f 74 20 61 6c 77 61 79 73 20 74 68 65 20 63 61  ot always the ca
19c81 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  se)..    */.    
19c82 61 73 73 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c  assert( !pColl |
19c83 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b  | pColl->xCmp );
19c84 0a 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20  ..    if( pColl 
19c85 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65  ){.      if( pMe
19c86 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e  m1->enc==pColl->
19c87 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  enc ){.        /
19c88 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 61 72  * The strings ar
19c89 65 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  e already in the
19c8a 20 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 69 6e   correct encodin
19c8b 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 20 20  g.  Call the.   
19c8c 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73       ** comparis
19c8d 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72 65  on function dire
19c8e 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20 20  ctly */.        
19c8f 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43  return pColl->xC
19c90 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c  mp(pColl->pUser,
19c91 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e  pMem1->n,pMem1->
19c92 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32  z,pMem2->n,pMem2
19c93 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ->z);.      }els
19c94 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  e{.        const
19c95 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a   void *v1, *v2;.
19c96 20 20 20 20 20 20 20 20 69 6e 74 20 6e 31 2c 20          int n1, 
19c97 6e 32 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20  n2;.        Mem 
19c98 63 31 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20  c1;.        Mem 
19c99 63 32 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  c2;.        mems
19c9a 65 74 28 26 63 31 2c 20 30 2c 20 73 69 7a 65 6f  et(&c1, 0, sizeo
19c9b 66 28 63 31 29 29 3b 0a 20 20 20 20 20 20 20 20  f(c1));.        
19c9c 6d 65 6d 73 65 74 28 26 63 32 2c 20 30 2c 20 73  memset(&c2, 0, s
19c9d 69 7a 65 6f 66 28 63 32 29 29 3b 0a 20 20 20 20  izeof(c2));.    
19c9e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19c9f 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63  emShallowCopy(&c
19ca0 31 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d 5f 45 70  1, pMem1, MEM_Ep
19ca1 68 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71  hem);.        sq
19ca2 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c  lite3VdbeMemShal
19ca3 6c 6f 77 43 6f 70 79 28 26 63 32 2c 20 70 4d 65  lowCopy(&c2, pMe
19ca4 6d 32 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a  m2, MEM_Ephem);.
19ca5 20 20 20 20 20 20 20 20 76 31 20 3d 20 73 71 6c          v1 = sql
19ca6 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73  ite3ValueText((s
19ca7 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63  qlite3_value*)&c
19ca8 31 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a  1, pColl->enc);.
19ca9 20 20 20 20 20 20 20 20 6e 31 20 3d 20 76 31 3d          n1 = v1=
19caa 3d 30 20 3f 20 30 20 3a 20 63 31 2e 6e 3b 0a 20  =0 ? 0 : c1.n;. 
19cab 20 20 20 20 20 20 20 76 32 20 3d 20 73 71 6c 69         v2 = sqli
19cac 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71  te3ValueText((sq
19cad 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 32  lite3_value*)&c2
19cae 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20  , pColl->enc);. 
19caf 20 20 20 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d         n2 = v2==
19cb0 30 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 0a 20 20  0 ? 0 : c2.n;.  
19cb1 20 20 20 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c        rc = pColl
19cb2 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55  ->xCmp(pColl->pU
19cb3 73 65 72 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c  ser, n1, v1, n2,
19cb4 20 76 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71   v2);.        sq
19cb5 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19cb6 61 73 65 28 26 63 31 29 3b 0a 20 20 20 20 20 20  ase(&c1);.      
19cb7 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19cb8 52 65 6c 65 61 73 65 28 26 63 32 29 3b 0a 20 20  Release(&c2);.  
19cb9 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
19cba 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
19cbb 20 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c 4c 20     /* If a NULL 
19cbc 70 6f 69 6e 74 65 72 20 77 61 73 20 70 61 73 73  pointer was pass
19cbd 65 64 20 61 73 20 74 68 65 20 63 6f 6c 6c 61 74  ed as the collat
19cbe 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c  e function, fall
19cbf 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20   through.    ** 
19cc0 74 6f 20 74 68 65 20 62 6c 6f 62 20 63 61 73 65  to the blob case
19cc1 20 61 6e 64 20 75 73 65 20 6d 65 6d 63 6d 70 28   and use memcmp(
19cc2 29 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f  ).  */.  }. .  /
19cc3 2a 20 42 6f 74 68 20 76 61 6c 75 65 73 20 6d 75  * Both values mu
19cc4 73 74 20 62 65 20 62 6c 6f 62 73 2e 20 20 43 6f  st be blobs.  Co
19cc5 6d 70 61 72 65 20 75 73 69 6e 67 20 6d 65 6d 63  mpare using memc
19cc6 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 72 63 20 3d  mp().  */.  rc =
19cc7 20 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a   memcmp(pMem1->z
19cc8 2c 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65  , pMem2->z, (pMe
19cc9 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f  m1->n>pMem2->n)?
19cca 70 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e  pMem2->n:pMem1->
19ccb 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20  n);.  if( rc==0 
19ccc 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d 65 6d  ){.    rc = pMem
19ccd 31 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b  1->n - pMem2->n;
19cce 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
19ccf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  ;.}../*.** Move 
19cd0 64 61 74 61 20 6f 75 74 20 6f 66 20 61 20 62 74  data out of a bt
19cd1 72 65 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20  ree key or data 
19cd2 66 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61  field and into a
19cd3 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 0a   Mem structure..
19cd4 2a 2a 20 54 68 65 20 64 61 74 61 20 6f 72 20 6b  ** The data or k
19cd5 65 79 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d  ey is taken from
19cd6 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20   the entry that 
19cd7 70 43 75 72 20 69 73 20 63 75 72 72 65 6e 74 6c  pCur is currentl
19cd8 79 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f  y pointing.** to
19cd9 2e 20 20 6f 66 66 73 65 74 20 61 6e 64 20 61 6d  .  offset and am
19cda 74 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74  t determine what
19cdb 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20   portion of the 
19cdc 64 61 74 61 20 6f 72 20 6b 65 79 20 74 6f 20 72  data or key to r
19cdd 65 74 72 69 65 76 65 2e 0a 2a 2a 20 6b 65 79 20  etrieve..** key 
19cde 69 73 20 74 72 75 65 20 74 6f 20 67 65 74 20 74  is true to get t
19cdf 68 65 20 6b 65 79 20 6f 72 20 66 61 6c 73 65 20  he key or false 
19ce0 74 6f 20 67 65 74 20 64 61 74 61 2e 20 20 54 68  to get data.  Th
19ce1 65 20 72 65 73 75 6c 74 20 69 73 20 77 72 69 74  e result is writ
19ce2 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20  ten.** into the 
19ce3 70 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a  pMem element..**
19ce4 0a 2a 2a 20 54 68 65 20 70 4d 65 6d 20 73 74 72  .** The pMem str
19ce5 75 63 74 75 72 65 20 69 73 20 61 73 73 75 6d 65  ucture is assume
19ce6 64 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69 61  d to be uninitia
19ce7 6c 69 7a 65 64 2e 20 20 41 6e 79 20 70 72 69 6f  lized.  Any prio
19ce8 72 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20  r content.** is 
19ce9 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68  overwritten with
19cea 6f 75 74 20 62 65 69 6e 67 20 66 72 65 65 64 2e  out being freed.
19ceb 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72  .**.** If this r
19cec 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72  outine fails for
19ced 20 61 6e 79 20 72 65 61 73 6f 6e 20 28 6d 61 6c   any reason (mal
19cee 6c 6f 63 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  loc returns NULL
19cef 20 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f   or unable.** to
19cf0 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64   read from the d
19cf1 69 73 6b 29 20 74 68 65 6e 20 74 68 65 20 70 4d  isk) then the pM
19cf2 65 6d 20 69 73 20 6c 65 66 74 20 69 6e 20 61 6e  em is left in an
19cf3 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74   inconsistent st
19cf4 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ate..*/.SQLITE_P
19cf5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19cf6 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72  e3VdbeMemFromBtr
19cf7 65 65 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  ee(.  BtCursor *
19cf8 70 43 75 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f  pCur,   /* Curso
19cf9 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 72 65  r pointing at re
19cfa 63 6f 72 64 20 74 6f 20 72 65 74 72 69 65 76 65  cord to retrieve
19cfb 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65  . */.  int offse
19cfc 74 2c 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73  t,       /* Offs
19cfd 65 74 20 66 72 6f 6d 20 74 68 65 20 73 74 61 72  et from the star
19cfe 74 20 6f 66 20 64 61 74 61 20 74 6f 20 72 65 74  t of data to ret
19cff 75 72 6e 20 62 79 74 65 73 20 66 72 6f 6d 2e 20  urn bytes from. 
19d00 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20  */.  int amt,   
19d01 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
19d02 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 74   of bytes to ret
19d03 75 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 6b 65  urn. */.  int ke
19d04 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  y,          /* I
19d05 66 20 74 72 75 65 2c 20 72 65 74 72 69 65 76 65  f true, retrieve
19d06 20 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20   from the btree 
19d07 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a  key, not data. *
19d08 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20  /.  Mem *pMem   
19d09 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65        /* OUT: Re
19d0a 74 75 72 6e 20 64 61 74 61 20 69 6e 20 74 68 69  turn data in thi
19d0b 73 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e  s Mem structure.
19d0c 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a   */.){.  char *z
19d0d 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20  Data;        /* 
19d0e 44 61 74 61 20 66 72 6f 6d 20 74 68 65 20 62 74  Data from the bt
19d0f 72 65 65 20 6c 61 79 65 72 20 2a 2f 0a 20 20 69  ree layer */.  i
19d10 6e 74 20 61 76 61 69 6c 61 62 6c 65 20 3d 20 30  nt available = 0
19d11 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  ;  /* Number of 
19d12 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20  bytes available 
19d13 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 62 74 72  on the local btr
19d14 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ee page */.  int
19d15 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
19d16 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
19d17 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  */..  assert( sq
19d18 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
19d19 49 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b  IsValid(pCur) );
19d1a 0a 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 65  ..  /* Note: the
19d1b 20 63 61 6c 6c 73 20 74 6f 20 42 74 72 65 65 4b   calls to BtreeK
19d1c 65 79 46 65 74 63 68 28 29 20 61 6e 64 20 44 61  eyFetch() and Da
19d1d 74 61 46 65 74 63 68 28 29 20 62 65 6c 6f 77 20  taFetch() below 
19d1e 61 73 73 65 72 74 28 29 20 0a 20 20 2a 2a 20 74  assert() .  ** t
19d1f 68 61 74 20 62 6f 74 68 20 74 68 65 20 42 74 53  hat both the BtS
19d20 68 61 72 65 64 20 61 6e 64 20 64 61 74 61 62 61  hared and databa
19d21 73 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 65  se handle mutexe
19d22 73 20 61 72 65 20 68 65 6c 64 2e 20 2a 2f 0a 20  s are held. */. 
19d23 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
19d24 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
19d25 65 74 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20  et)==0 );.  if( 
19d26 6b 65 79 20 29 7b 0a 20 20 20 20 7a 44 61 74 61  key ){.    zData
19d27 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
19d28 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28  e3BtreeKeyFetch(
19d29 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65  pCur, &available
19d2a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
19d2b 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29  zData = (char *)
19d2c 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
19d2d 46 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61  Fetch(pCur, &ava
19d2e 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 61  ilable);.  }.  a
19d2f 73 73 65 72 74 28 20 7a 44 61 74 61 21 3d 30 20  ssert( zData!=0 
19d30 29 3b 0a 0a 20 20 69 66 28 20 6f 66 66 73 65 74  );..  if( offset
19d31 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20  +amt<=available 
19d32 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  && (pMem->flags&
19d33 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 7b 0a 20  MEM_Dyn)==0 ){. 
19d34 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19d35 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
19d36 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a      pMem->z = &z
19d37 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20  Data[offset];.  
19d38 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
19d39 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68  MEM_Blob|MEM_Eph
19d3a 65 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  em;.  }else if( 
19d3b 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
19d3c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
19d3d 72 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c  row(pMem, amt+2,
19d3e 20 30 29 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d   0)) ){.    pMem
19d3f 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c  ->flags = MEM_Bl
19d40 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54  ob|MEM_Dyn|MEM_T
19d41 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65  erm;.    pMem->e
19d42 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d  nc = 0;.    pMem
19d43 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
19d44 42 4c 4f 42 3b 0a 20 20 20 20 69 66 28 20 6b 65  BLOB;.    if( ke
19d45 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  y ){.      rc = 
19d46 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28  sqlite3BtreeKey(
19d47 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d  pCur, offset, am
19d48 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20  t, pMem->z);.   
19d49 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
19d4a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44   = sqlite3BtreeD
19d4b 61 74 61 28 70 43 75 72 2c 20 6f 66 66 73 65 74  ata(pCur, offset
19d4c 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b  , amt, pMem->z);
19d4d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
19d4e 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20  >z[amt] = 0;.   
19d4f 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20   pMem->z[amt+1] 
19d50 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 21  = 0;.    if( rc!
19d51 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
19d52 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19d53 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
19d54 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4d 65  .    }.  }.  pMe
19d55 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a 20 20 72  m->n = amt;..  r
19d56 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
19d57 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
19d58 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
19d59 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69  internally, it i
19d5a 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68  s not part of th
19d5b 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50  e.** external AP
19d5c 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61  I. It works in a
19d5d 20 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20   similar way to 
19d5e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
19d5f 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20  xt(),.** except 
19d60 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65  the data returne
19d61 64 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f  d is in the enco
19d62 64 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62  ding specified b
19d63 79 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  y the second.** 
19d64 70 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68  parameter, which
19d65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20   must be one of 
19d66 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20  SQLITE_UTF16BE, 
19d67 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f  SQLITE_UTF16LE o
19d68 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38  r.** SQLITE_UTF8
19d69 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32  ..**.** (2006-02
19d6a 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76  -16:)  The enc v
19d6b 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65  alue can be or-e
19d6c 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54  d with SQLITE_UT
19d6d 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20  F16_ALIGNED..** 
19d6e 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63  If that is the c
19d6f 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ase, then the re
19d70 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69  sult must be ali
19d71 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20  gned on an even 
19d72 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79  byte.** boundary
19d73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19d74 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ATE const void *
19d75 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
19d76 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20  (sqlite3_value* 
19d77 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20  pVal, u8 enc){. 
19d78 20 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74   if( !pVal ) ret
19d79 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  urn 0;..  assert
19d7a 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pVal->db==0 ||
19d7b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
19d7c 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75  eld(pVal->db->mu
19d7d 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
19d7e 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26  ( (enc&3)==(enc&
19d7f 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  ~SQLITE_UTF16_AL
19d80 49 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 73 65  IGNED) );.  asse
19d81 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73  rt( (pVal->flags
19d82 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
19d83 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c  0 );..  if( pVal
19d84 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c  ->flags&MEM_Null
19d85 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
19d86 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
19d87 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d  (MEM_Blob>>3) ==
19d88 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56   MEM_Str );.  pV
19d89 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56  al->flags |= (pV
19d8a 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  al->flags & MEM_
19d8b 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61  Blob)>>3;.  expa
19d8c 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20  ndBlob(pVal);.  
19d8d 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26  if( pVal->flags&
19d8e 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73  MEM_Str ){.    s
19d8f 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
19d90 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65  Encoding(pVal, e
19d91 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46  nc & ~SQLITE_UTF
19d92 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20  16_ALIGNED);.   
19d93 20 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49   if( (enc & SQLI
19d94 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
19d95 29 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 53 51  )!=0 && 1==(1&SQ
19d96 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28  LITE_PTR_TO_INT(
19d97 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 20 20  pVal->z)) ){.   
19d98 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c     assert( (pVal
19d99 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 45  ->flags & (MEM_E
19d9a 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29  phem|MEM_Static)
19d9b 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  )!=0 );.      if
19d9c 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
19d9d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 56  MakeWriteable(pV
19d9e 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  al)!=SQLITE_OK )
19d9f 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
19da0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
19da1 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
19da2 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65  eMemNulTerminate
19da3 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b  (pVal);.  }else{
19da4 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56  .    assert( (pV
19da5 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c  al->flags&MEM_Bl
19da6 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71  ob)==0 );.    sq
19da7 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69  lite3VdbeMemStri
19da8 6e 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63 29  ngify(pVal, enc)
19da9 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d  ;.    assert( 0=
19daa 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54  =(1&SQLITE_PTR_T
19dab 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20  O_INT(pVal->z)) 
19dac 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  );.  }.  assert(
19dad 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20  pVal->enc==(enc 
19dae 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  & ~SQLITE_UTF16_
19daf 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70 56 61 6c  ALIGNED) || pVal
19db0 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20 20 20 20  ->db==0.        
19db1 20 20 20 20 20 20 7c 7c 20 70 56 61 6c 2d 3e 64        || pVal->d
19db2 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
19db3 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65  );.  if( pVal->e
19db4 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49  nc==(enc & ~SQLI
19db5 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
19db6 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
19db7 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65  pVal->z;.  }else
19db8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
19db9 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65    }.}../*.** Cre
19dba 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65  ate a new sqlite
19dbb 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a  3_value object..
19dbc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19dbd 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  E sqlite3_value 
19dbe 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77  *sqlite3ValueNew
19dbf 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
19dc0 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c 69 74 65   Mem *p = sqlite
19dc1 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
19dc2 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20  , sizeof(*p));. 
19dc3 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d   if( p ){.    p-
19dc4 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
19dc5 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70 65 20 3d  l;.    p->type =
19dc6 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20   SQLITE_NULL;.  
19dc7 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20    p->db = db;.  
19dc8 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
19dc9 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
19dca 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  new sqlite3_valu
19dcb 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69  e object, contai
19dcc 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f  ning the value o
19dcd 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54  f pExpr..**.** T
19dce 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66  his only works f
19dcf 6f 72 20 76 65 72 79 20 73 69 6d 70 6c 65 20 65  or very simple e
19dd0 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20  xpressions that 
19dd1 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 63  consist of one c
19dd2 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e  onstant.** token
19dd3 20 28 69 2e 65 2e 20 22 35 22 2c 20 22 35 2e 31   (i.e. "5", "5.1
19dd4 22 2c 20 22 27 61 20 73 74 72 69 6e 67 27 22 29  ", "'a string'")
19dd5 2e 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  . If the express
19dd6 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f  ion can.** be co
19dd7 6e 76 65 72 74 65 64 20 64 69 72 65 63 74 6c 79  nverted directly
19dd8 20 69 6e 74 6f 20 61 20 76 61 6c 75 65 2c 20 74   into a value, t
19dd9 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73  hen the value is
19dda 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a   allocated and.*
19ddb 2a 20 61 20 70 6f 69 6e 74 65 72 20 77 72 69 74  * a pointer writ
19ddc 74 65 6e 20 74 6f 20 2a 70 70 56 61 6c 2e 20 54  ten to *ppVal. T
19ddd 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73  he caller is res
19dde 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61  ponsible for dea
19ddf 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 74 68 65  llocating.** the
19de0 20 76 61 6c 75 65 20 62 79 20 70 61 73 73 69 6e   value by passin
19de1 67 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 56  g it to sqlite3V
19de2 61 6c 75 65 46 72 65 65 28 29 20 6c 61 74 65 72  alueFree() later
19de3 20 6f 6e 2e 20 49 66 20 74 68 65 20 65 78 70 72   on. If the expr
19de4 65 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74  ession.** cannot
19de5 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f   be converted to
19de6 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a   a value, then *
19de7 70 70 56 61 6c 20 69 73 20 73 65 74 20 74 6f 20  ppVal is set to 
19de8 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NULL..*/.SQLITE_
19de9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19dea 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
19deb 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
19dec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19ded 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
19dee 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  nnection */.  Ex
19def 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20  pr *pExpr,      
19df0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
19df1 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 65 76 61  xpression to eva
19df2 6c 75 61 74 65 20 2a 2f 0a 20 20 75 38 20 65 6e  luate */.  u8 en
19df3 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
19df4 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67       /* Encoding
19df5 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 75 38 20   to use */.  u8 
19df6 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 20  affinity,       
19df7 20 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69         /* Affini
19df8 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 73  ty to use */.  s
19df9 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70  qlite3_value **p
19dfa 70 56 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74  pVal     /* Writ
19dfb 65 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20  e the new value 
19dfc 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
19dfd 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61   op;.  char *zVa
19dfe 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  l = 0;.  sqlite3
19dff 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30  _value *pVal = 0
19e00 3b 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20  ;..  if( !pExpr 
19e01 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20  ){.    *ppVal = 
19e02 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
19e03 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f  LITE_OK;.  }.  o
19e04 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20  p = pExpr->op;. 
19e05 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49   if( op==TK_REGI
19e06 53 54 45 52 20 29 7b 0a 20 20 20 20 6f 70 20 3d  STER ){.    op =
19e07 20 70 45 78 70 72 2d 3e 6f 70 32 3b 0a 20 20 7d   pExpr->op2;.  }
19e08 0a 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53  ..  if( op==TK_S
19e09 54 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f  TRING || op==TK_
19e0a 46 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f  FLOAT || op==TK_
19e0b 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70  INTEGER ){.    p
19e0c 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  Val = sqlite3Val
19e0d 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69  ueNew(db);.    i
19e0e 66 28 20 70 56 61 6c 3d 3d 30 20 29 20 67 6f 74  f( pVal==0 ) got
19e0f 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66  o no_mem;.    if
19e10 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
19e11 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56  y(pExpr, EP_IntV
19e12 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 73  alue) ){.      s
19e13 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
19e14 49 6e 74 36 34 28 70 56 61 6c 2c 20 28 69 36 34  Int64(pVal, (i64
19e15 29 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c 75 65  )pExpr->u.iValue
19e16 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
19e17 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74      zVal = sqlit
19e18 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
19e19 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b  Expr->u.zToken);
19e1a 0a 20 20 20 20 20 20 69 66 28 20 7a 56 61 6c 3d  .      if( zVal=
19e1b 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  =0 ) goto no_mem
19e1c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
19e1d 61 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c  alueSetStr(pVal,
19e1e 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 54   -1, zVal, SQLIT
19e1f 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44  E_UTF8, SQLITE_D
19e20 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 69  YNAMIC);.      i
19e21 66 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20  f( op==TK_FLOAT 
19e22 29 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d 20 53  ) pVal->type = S
19e23 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 20  QLITE_FLOAT;.   
19e24 20 7d 0a 20 20 20 20 69 66 28 20 28 6f 70 3d 3d   }.    if( (op==
19e25 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 6f 70  TK_INTEGER || op
19e26 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 26 20  ==TK_FLOAT ) && 
19e27 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
19e28 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20  _AFF_NONE ){.   
19e29 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41     sqlite3ValueA
19e2a 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56 61  pplyAffinity(pVa
19e2b 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  l, SQLITE_AFF_NU
19e2c 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 5f 55 54  MERIC, SQLITE_UT
19e2d 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  F8);.    }else{.
19e2e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
19e2f 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28  ueApplyAffinity(
19e30 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20  pVal, affinity, 
19e31 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20  SQLITE_UTF8);.  
19e32 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 21    }.    if( enc!
19e33 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a  =SQLITE_UTF8 ){.
19e34 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19e35 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
19e36 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20  pVal, enc);.    
19e37 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70  }.  }else if( op
19e38 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 7b 0a  ==TK_UMINUS ) {.
19e39 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
19e3a 4b 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65 46  K==sqlite3ValueF
19e3b 72 6f 6d 45 78 70 72 28 64 62 2c 70 45 78 70 72  romExpr(db,pExpr
19e3c 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66 66 69  ->pLeft,enc,affi
19e3d 6e 69 74 79 2c 26 70 56 61 6c 29 20 29 7b 0a 20  nity,&pVal) ){. 
19e3e 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 20 3d       pVal->u.i =
19e3f 20 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e 69 3b   -1 * pVal->u.i;
19e40 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c  .      /* (doubl
19e41 65 29 2d 31 20 49 6e 20 63 61 73 65 20 6f 66 20  e)-1 In case of 
19e42 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
19e43 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f  TING_POINT... */
19e44 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 72 20 3d  .      pVal->r =
19e45 20 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20 70 56   (double)-1 * pV
19e46 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  al->r;.    }.  }
19e47 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
19e48 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
19e49 4c 0a 20 20 65 6c 73 65 20 69 66 28 20 6f 70 3d  L.  else if( op=
19e4a 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20  =TK_BLOB ){.    
19e4b 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61 73  int nVal;.    as
19e4c 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a  sert( pExpr->u.z
19e4d 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 7c 7c  Token[0]=='x' ||
19e4e 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
19e4f 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20  [0]=='X' );.    
19e50 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75  assert( pExpr->u
19e51 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27  .zToken[1]=='\''
19e52 20 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73   );.    pVal = s
19e53 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64  qlite3ValueNew(d
19e54 62 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 61  b);.    if( !pVa
19e55 6c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  l ) goto no_mem;
19e56 0a 20 20 20 20 7a 56 61 6c 20 3d 20 26 70 45 78  .    zVal = &pEx
19e57 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b  pr->u.zToken[2];
19e58 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69  .    nVal = sqli
19e59 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 56 61 6c  te3Strlen30(zVal
19e5a 29 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28  )-1;.    assert(
19e5b 20 7a 56 61 6c 5b 6e 56 61 6c 5d 3d 3d 27 5c 27   zVal[nVal]=='\'
19e5c 27 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ' );.    sqlite3
19e5d 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 56  VdbeMemSetStr(pV
19e5e 61 6c 2c 20 73 71 6c 69 74 65 33 48 65 78 54 6f  al, sqlite3HexTo
19e5f 42 6c 6f 62 28 64 62 2c 20 7a 56 61 6c 2c 20 6e  Blob(db, zVal, n
19e60 56 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c 0a 20 20  Val), nVal/2,.  
19e61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e62 20 20 20 20 20 20 20 30 2c 20 53 51 4c 49 54 45         0, SQLITE
19e63 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23  _DYNAMIC);.  }.#
19e64 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 56 61  endif..  if( pVa
19e65 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  l ){.    sqlite3
19e66 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65  VdbeMemStoreType
19e67 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70  (pVal);.  }.  *p
19e68 70 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 20 72  pVal = pVal;.  r
19e69 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
19e6a 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e  ..no_mem:.  db->
19e6b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
19e6c 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
19e6d 65 28 64 62 2c 20 7a 56 61 6c 29 3b 0a 20 20 73  e(db, zVal);.  s
19e6e 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
19e6f 70 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61 6c 20  pVal);.  *ppVal 
19e70 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  = 0;.  return SQ
19e71 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f  LITE_NOMEM;.}../
19e72 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
19e73 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f 66 20  string value of 
19e74 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  an sqlite3_value
19e75 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54   object.*/.SQLIT
19e76 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19e77 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74  qlite3ValueSetSt
19e78 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  r(.  sqlite3_val
19e79 75 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20 56 61  ue *v,     /* Va
19e7a 6c 75 65 20 74 6f 20 62 65 20 73 65 74 20 2a 2f  lue to be set */
19e7b 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20  .  int n,       
19e7c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
19e7d 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a 20 2a  th of string z *
19e7e 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
19e7f 7a 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78  z,        /* Tex
19e80 74 20 6f 66 20 74 68 65 20 6e 65 77 20 73 74 72  t of the new str
19e81 69 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c  ing */.  u8 enc,
19e82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19e83 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73  * Encoding to us
19e84 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  e */.  void (*xD
19e85 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f 2a 20  el)(void*)   /* 
19e86 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74  Destructor for t
19e87 68 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  he string */.){.
19e88 20 20 69 66 28 20 76 20 29 20 73 71 6c 69 74 65    if( v ) sqlite
19e89 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 28  3VdbeMemSetStr((
19e8a 4d 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c 20 65  Mem *)v, z, n, e
19e8b 6e 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a 2f 2a  nc, xDel);.}../*
19e8c 0a 2a 2a 20 46 72 65 65 20 61 6e 20 73 71 6c 69  .** Free an sqli
19e8d 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
19e8e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19e8f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
19e90 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33  alueFree(sqlite3
19e91 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20 69 66  _value *v){.  if
19e92 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20  ( !v ) return;. 
19e93 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19e94 65 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29 76 29  elease((Mem *)v)
19e95 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
19e96 65 28 28 28 4d 65 6d 2a 29 76 29 2d 3e 64 62 2c  e(((Mem*)v)->db,
19e97 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   v);.}../*.** Re
19e98 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
19e99 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
19e9a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
19e9b 6a 65 63 74 20 61 73 73 75 6d 69 6e 67 0a 2a 2a  ject assuming.**
19e9c 20 74 68 61 74 20 69 74 20 75 73 65 73 20 74 68   that it uses th
19e9d 65 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e 63 22  e encoding "enc"
19e9e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19e9f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61  TE int sqlite3Va
19ea0 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 65 33  lueBytes(sqlite3
19ea1 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 75 38  _value *pVal, u8
19ea2 20 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a 70 20   enc){.  Mem *p 
19ea3 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20  = (Mem*)pVal;.  
19ea4 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20  if( (p->flags & 
19ea5 4d 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c 7c 20  MEM_Blob)!=0 || 
19ea6 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
19ea7 28 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b 0a 20  (pVal, enc) ){. 
19ea8 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20     if( p->flags 
19ea9 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
19eaa 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 20      return p->n 
19eab 2b 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20  + p->u.nZero;.  
19eac 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
19ead 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20  eturn p->n;.    
19eae 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
19eaf 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
19eb0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65  **** End of vdbe
19eb1 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem.c **********
19eb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eb4 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
19eb5 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
19eb6 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a  vdbeaux.c ******
19eb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eb9 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20  ***/./*.** 2003 
19eba 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a  September 6.**.*
19ebb 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
19ebc 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
19ebd 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
19ebe 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
19ebf 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
19ec0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
19ec1 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
19ec2 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
19ec3 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
19ec4 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
19ec5 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
19ec6 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
19ec7 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
19ec8 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
19ec9 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
19eca 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
19ecb 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
19ecc 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
19ecd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ece 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ecf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
19ed1 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
19ed2 20 63 6f 64 65 20 75 73 65 64 20 66 6f 72 20 63   code used for c
19ed3 72 65 61 74 69 6e 67 2c 20 64 65 73 74 72 6f 79  reating, destroy
19ed4 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 74  ing, and populat
19ed5 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 6f  ing.** a VDBE (o
19ed6 72 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74  r an "sqlite3_st
19ed7 6d 74 22 20 61 73 20 69 74 20 69 73 20 6b 6e 6f  mt" as it is kno
19ed8 77 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 69 64  wn to the outsid
19ed9 65 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f 72  e world.)  Prior
19eda 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 32  .** to version 2
19edb 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 63  .8.7, all this c
19edc 6f 64 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 64  ode was combined
19edd 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 63   into the vdbe.c
19ede 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a   source file..**
19edf 20 42 75 74 20 74 68 61 74 20 66 69 6c 65 20 77   But that file w
19ee0 61 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 62  as getting too b
19ee1 69 67 20 73 6f 20 74 68 69 73 20 73 75 62 72 6f  ig so this subro
19ee2 75 74 69 6e 65 73 20 77 65 72 65 20 73 70 6c 69  utines were spli
19ee3 74 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  t out..**.** $Id
19ee4 3a 20 76 64 62 65 61 75 78 2e 63 2c 76 20 31 2e  : vdbeaux.c,v 1.
19ee5 34 38 30 20 32 30 30 39 2f 30 38 2f 30 38 20 31  480 2009/08/08 1
19ee6 38 3a 30 31 3a 30 38 20 64 72 68 20 45 78 70 20  8:01:08 drh Exp 
19ee7 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68  $.*/..../*.** Wh
19ee8 65 6e 20 64 65 62 75 67 67 69 6e 67 20 74 68 65  en debugging the
19ee9 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
19eea 69 6e 20 61 20 73 79 6d 62 6f 6c 69 63 20 64 65  in a symbolic de
19eeb 62 75 67 67 65 72 2c 20 6f 6e 65 20 63 61 6e 0a  bugger, one can.
19eec 2a 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74  ** set the sqlit
19eed 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65  e3VdbeAddopTrace
19eee 20 74 6f 20 31 20 61 6e 64 20 61 6c 6c 20 6f 70   to 1 and all op
19eef 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20 70 72  codes will be pr
19ef0 69 6e 74 65 64 0a 2a 2a 20 61 73 20 74 68 65 79  inted.** as they
19ef1 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68   are added to th
19ef2 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 73 74  e instruction st
19ef3 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ream..*/.#ifdef 
19ef4 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
19ef5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19ef6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70  sqlite3VdbeAddop
19ef7 54 72 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69  Trace = 0;.#endi
19ef8 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  f.../*.** Create
19ef9 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 64   a new virtual d
19efa 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 2e 0a  atabase engine..
19efb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19efc 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65 33 56  E Vdbe *sqlite3V
19efd 64 62 65 43 72 65 61 74 65 28 73 71 6c 69 74 65  dbeCreate(sqlite
19efe 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a  3 *db){.  Vdbe *
19eff 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  p;.  p = sqlite3
19f00 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
19f01 20 73 69 7a 65 6f 66 28 56 64 62 65 29 20 29 3b   sizeof(Vdbe) );
19f02 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
19f03 74 75 72 6e 20 30 3b 0a 20 20 70 2d 3e 64 62 20  turn 0;.  p->db 
19f04 3d 20 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e  = db;.  if( db->
19f05 70 56 64 62 65 20 29 7b 0a 20 20 20 20 64 62 2d  pVdbe ){.    db-
19f06 3e 70 56 64 62 65 2d 3e 70 50 72 65 76 20 3d 20  >pVdbe->pPrev = 
19f07 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4e 65 78  p;.  }.  p->pNex
19f08 74 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a 20  t = db->pVdbe;. 
19f09 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20   p->pPrev = 0;. 
19f0a 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 3b 0a   db->pVdbe = p;.
19f0b 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42    p->magic = VDB
19f0c 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20  E_MAGIC_INIT;.  
19f0d 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
19f0e 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20  ** Remember the 
19f0f 53 51 4c 20 73 74 72 69 6e 67 20 66 6f 72 20 61  SQL string for a
19f10 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
19f11 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
19f12 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
19f13 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64  te3VdbeSetSql(Vd
19f14 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  be *p, const cha
19f15 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74  r *z, int n, int
19f16 20 69 73 50 72 65 70 61 72 65 56 32 29 7b 0a 20   isPrepareV2){. 
19f17 20 61 73 73 65 72 74 28 20 69 73 50 72 65 70 61   assert( isPrepa
19f18 72 65 56 32 3d 3d 31 20 7c 7c 20 69 73 50 72 65  reV2==1 || isPre
19f19 70 61 72 65 56 32 3d 3d 30 20 29 3b 0a 20 20 69  pareV2==0 );.  i
19f1a 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
19f1b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
19f1c 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 69 66 28  OMIT_TRACE.  if(
19f1d 20 21 69 73 50 72 65 70 61 72 65 56 32 20 29 20   !isPrepareV2 ) 
19f1e 72 65 74 75 72 6e 3b 0a 23 65 6e 64 69 66 0a 20  return;.#endif. 
19f1f 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 53 71 6c   assert( p->zSql
19f20 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c  ==0 );.  p->zSql
19f21 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
19f22 44 75 70 28 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29  Dup(p->db, z, n)
19f23 3b 0a 20 20 70 2d 3e 69 73 50 72 65 70 61 72 65  ;.  p->isPrepare
19f24 56 32 20 3d 20 28 75 38 29 69 73 50 72 65 70 61  V2 = (u8)isPrepa
19f25 72 65 56 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  reV2;.}../*.** R
19f26 65 74 75 72 6e 20 74 68 65 20 53 51 4c 20 61 73  eturn the SQL as
19f27 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
19f28 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
19f29 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  nt.*/.SQLITE_API
19f2a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
19f2b 69 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 33  ite3_sql(sqlite3
19f2c 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
19f2d 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
19f2e 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75   *)pStmt;.  retu
19f2f 72 6e 20 28 70 2d 3e 69 73 50 72 65 70 61 72 65  rn (p->isPrepare
19f30 56 32 20 3f 20 70 2d 3e 7a 53 71 6c 20 3a 20 30  V2 ? p->zSql : 0
19f31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70  );.}../*.** Swap
19f32 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 65 74   all content bet
19f33 77 65 65 6e 20 74 77 6f 20 56 44 42 45 20 73 74  ween two VDBE st
19f34 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c  ructures..*/.SQL
19f35 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19f36 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70   sqlite3VdbeSwap
19f37 28 56 64 62 65 20 2a 70 41 2c 20 56 64 62 65 20  (Vdbe *pA, Vdbe 
19f38 2a 70 42 29 7b 0a 20 20 56 64 62 65 20 74 6d 70  *pB){.  Vdbe tmp
19f39 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68 61 72 20  , *pTmp;.  char 
19f3a 2a 7a 54 6d 70 3b 0a 20 20 74 6d 70 20 3d 20 2a  *zTmp;.  tmp = *
19f3b 70 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b  pA;.  *pA = *pB;
19f3c 0a 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a 20 20  .  *pB = tmp;.  
19f3d 70 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74  pTmp = pA->pNext
19f3e 3b 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d 20  ;.  pA->pNext = 
19f3f 70 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 42 2d  pB->pNext;.  pB-
19f40 3e 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b 0a 20  >pNext = pTmp;. 
19f41 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50 72 65   pTmp = pA->pPre
19f42 76 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76 20 3d  v;.  pA->pPrev =
19f43 20 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20 70 42   pB->pPrev;.  pB
19f44 2d 3e 70 50 72 65 76 20 3d 20 70 54 6d 70 3b 0a  ->pPrev = pTmp;.
19f45 20 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a 53 71    zTmp = pA->zSq
19f46 6c 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20 3d 20  l;.  pA->zSql = 
19f47 70 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e  pB->zSql;.  pB->
19f48 7a 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20 20 70  zSql = zTmp;.  p
19f49 42 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d  B->isPrepareV2 =
19f4a 20 70 41 2d 3e 69 73 50 72 65 70 61 72 65 56 32   pA->isPrepareV2
19f4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
19f4c 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54  TE_DEBUG./*.** T
19f4d 75 72 6e 20 74 72 61 63 69 6e 67 20 6f 6e 20 6f  urn tracing on o
19f4e 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c 49 54 45 5f  r off.*/.SQLITE_
19f4f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19f50 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64  ite3VdbeTrace(Vd
19f51 62 65 20 2a 70 2c 20 46 49 4c 45 20 2a 74 72 61  be *p, FILE *tra
19f52 63 65 29 7b 0a 20 20 70 2d 3e 74 72 61 63 65 20  ce){.  p->trace 
19f53 3d 20 74 72 61 63 65 3b 0a 7d 0a 23 65 6e 64 69  = trace;.}.#endi
19f54 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20  f../*.** Resize 
19f55 74 68 65 20 56 64 62 65 2e 61 4f 70 20 61 72 72  the Vdbe.aOp arr
19f56 61 79 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  ay so that it is
19f57 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70   at least one op
19f58 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a   larger than .**
19f59 20 69 74 20 77 61 73 2e 0a 2a 2a 0a 2a 2a 20 49   it was..**.** I
19f5a 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f  f an out-of-memo
19f5b 72 79 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  ry error occurs 
19f5c 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20 74  while resizing t
19f5d 68 65 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e  he array, return
19f5e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  .** SQLITE_NOMEM
19f5f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 56  . In this case V
19f60 64 62 65 2e 61 4f 70 20 61 6e 64 20 56 64 62 65  dbe.aOp and Vdbe
19f61 2e 6e 4f 70 41 6c 6c 6f 63 20 72 65 6d 61 69 6e  .nOpAlloc remain
19f62 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 28   .** unchanged (
19f63 74 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 20  this is so that 
19f64 61 6e 79 20 6f 70 63 6f 64 65 73 20 61 6c 72 65  any opcodes alre
19f65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 63 61  ady allocated ca
19f66 6e 20 62 65 20 0a 2a 2a 20 63 6f 72 72 65 63 74  n be .** correct
19f67 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 61  ly deallocated a
19f68 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72 65  long with the re
19f69 73 74 20 6f 66 20 74 68 65 20 56 64 62 65 29 2e  st of the Vdbe).
19f6a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
19f6b 72 6f 77 4f 70 41 72 72 61 79 28 56 64 62 65 20  rowOpArray(Vdbe 
19f6c 2a 70 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 70  *p){.  VdbeOp *p
19f6d 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 4e 65 77 20  New;.  int nNew 
19f6e 3d 20 28 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f  = (p->nOpAlloc ?
19f6f 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a   p->nOpAlloc*2 :
19f70 20 28 69 6e 74 29 28 31 30 32 34 2f 73 69 7a 65   (int)(1024/size
19f71 6f 66 28 4f 70 29 29 29 3b 0a 20 20 70 4e 65 77  of(Op)));.  pNew
19f72 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
19f73 6c 6f 63 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f  loc(p->db, p->aO
19f74 70 2c 20 6e 4e 65 77 2a 73 69 7a 65 6f 66 28 4f  p, nNew*sizeof(O
19f75 70 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20  p));.  if( pNew 
19f76 29 7b 0a 20 20 20 20 70 2d 3e 6e 4f 70 41 6c 6c  ){.    p->nOpAll
19f77 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  oc = sqlite3DbMa
19f78 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20  llocSize(p->db, 
19f79 70 4e 65 77 29 2f 73 69 7a 65 6f 66 28 4f 70 29  pNew)/sizeof(Op)
19f7a 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70  ;.    p->aOp = p
19f7b 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  New;.  }.  retur
19f7c 6e 20 28 70 4e 65 77 20 3f 20 53 51 4c 49 54 45  n (pNew ? SQLITE
19f7d 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d  _OK : SQLITE_NOM
19f7e 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  EM);.}../*.** Ad
19f7f 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74  d a new instruct
19f80 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20  ion to the list 
19f81 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  of instructions 
19f82 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a  current in the.*
19f83 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20  * VDBE.  Return 
19f84 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
19f85 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69  he new instructi
19f86 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65  on..**.** Parame
19f87 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70  ters:.**.**    p
19f88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
19f89 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44  ointer to the VD
19f8a 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20  BE.**.**    op  
19f8b 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20              The 
19f8c 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20  opcode for this 
19f8d 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a  instruction.**.*
19f8e 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20  *    p1, p2, p3 
19f8f 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a       Operands.**
19f90 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69  .** Use the sqli
19f91 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
19f92 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74  bel() function t
19f93 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73  o fix an address
19f94 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69   and.** the sqli
19f95 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
19f96 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68  ) function to ch
19f97 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
19f98 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72  f the P4.** oper
19f99 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  and..*/.SQLITE_P
19f9a 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19f9b 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62  e3VdbeAddOp3(Vdb
19f9c 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  e *p, int op, in
19f9d 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e  t p1, int p2, in
19f9e 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  t p3){.  int i;.
19f9f 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a    VdbeOp *pOp;..
19fa0 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20    i = p->nOp;.  
19fa1 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
19fa2 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
19fa3 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  T );.  assert( o
19fa4 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29  p>0 && op<0xff )
19fa5 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41 6c  ;.  if( p->nOpAl
19fa6 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 20 69 66  loc<=i ){.    if
19fa7 28 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29  ( growOpArray(p)
19fa8 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19fa9 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
19faa 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20  p->nOp++;.  pOp 
19fab 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20  = &p->aOp[i];.  
19fac 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 28 75  pOp->opcode = (u
19fad 38 29 6f 70 3b 0a 20 20 70 4f 70 2d 3e 70 35 20  8)op;.  pOp->p5 
19fae 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d  = 0;.  pOp->p1 =
19faf 20 70 31 3b 0a 20 20 70 4f 70 2d 3e 70 32 20 3d   p1;.  pOp->p2 =
19fb0 20 70 32 3b 0a 20 20 70 4f 70 2d 3e 70 33 20 3d   p2;.  pOp->p3 =
19fb1 20 70 33 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70   p3;.  pOp->p4.p
19fb2 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 34 74   = 0;.  pOp->p4t
19fb3 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44  ype = P4_NOTUSED
19fb4 3b 0a 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d  ;.  p->expired =
19fb5 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   0;.#ifdef SQLIT
19fb6 45 5f 44 45 42 55 47 0a 20 20 70 4f 70 2d 3e 7a  E_DEBUG.  pOp->z
19fb7 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 69  Comment = 0;.  i
19fb8 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  f( sqlite3VdbeAd
19fb9 64 6f 70 54 72 61 63 65 20 29 20 73 71 6c 69 74  dopTrace ) sqlit
19fba 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c  e3VdbePrintOp(0,
19fbb 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b   i, &p->aOp[i]);
19fbc 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56  .#endif.#ifdef V
19fbd 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 70 4f  DBE_PROFILE.  pO
19fbe 70 2d 3e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20  p->cycles = 0;. 
19fbf 20 70 4f 70 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23   pOp->cnt = 0;.#
19fc0 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 69  endif.  return i
19fc1 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
19fc2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19fc3 62 65 41 64 64 4f 70 30 28 56 64 62 65 20 2a 70  beAddOp0(Vdbe *p
19fc4 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 72 65 74  , int op){.  ret
19fc5 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41  urn sqlite3VdbeA
19fc6 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20  ddOp3(p, op, 0, 
19fc7 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  0, 0);.}.SQLITE_
19fc8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19fc9 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64  te3VdbeAddOp1(Vd
19fca 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69  be *p, int op, i
19fcb 6e 74 20 70 31 29 7b 0a 20 20 72 65 74 75 72 6e  nt p1){.  return
19fcc 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19fcd 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c  p3(p, op, p1, 0,
19fce 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52   0);.}.SQLITE_PR
19fcf 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19fd0 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65  3VdbeAddOp2(Vdbe
19fd1 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74   *p, int op, int
19fd2 20 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20   p1, int p2){.  
19fd3 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64  return sqlite3Vd
19fd4 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20  beAddOp3(p, op, 
19fd5 70 31 2c 20 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a  p1, p2, 0);.}...
19fd6 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63  /*.** Add an opc
19fd7 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65  ode that include
19fd8 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61  s the p4 value a
19fd9 73 20 61 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  s a pointer..*/.
19fda 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19fdb 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
19fdc 64 4f 70 34 28 0a 20 20 56 64 62 65 20 2a 70 2c  dOp4(.  Vdbe *p,
19fdd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
19fde 64 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f  dd the opcode to
19fdf 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e   this VM */.  in
19fe0 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
19fe1 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63    /* The new opc
19fe2 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c  ode */.  int p1,
19fe3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19fe4 54 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a  The P1 operand *
19fe5 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20  /.  int p2,     
19fe6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50          /* The P
19fe7 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  2 operand */.  i
19fe8 6e 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20  nt p3,          
19fe9 20 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65     /* The P3 ope
19fea 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rand */.  const 
19feb 63 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a  char *zP4,    /*
19fec 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20   The P4 operand 
19fed 2a 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 20  */.  int p4type 
19fee 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 6f           /* P4 o
19fef 70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0a 29  perand type */.)
19ff0 7b 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73  {.  int addr = s
19ff1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
19ff2 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20  (p, op, p1, p2, 
19ff3 70 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  p3);.  sqlite3Vd
19ff4 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 64  beChangeP4(p, ad
19ff5 64 72 2c 20 7a 50 34 2c 20 70 34 74 79 70 65 29  dr, zP4, p4type)
19ff6 3b 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b  ;.  return addr;
19ff7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
19ff8 20 61 20 6e 65 77 20 73 79 6d 62 6f 6c 69 63 20   a new symbolic 
19ff9 6c 61 62 65 6c 20 66 6f 72 20 61 6e 20 69 6e 73  label for an ins
19ffa 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 68 61  truction that ha
19ffb 73 20 79 65 74 20 74 6f 20 62 65 0a 2a 2a 20 63  s yet to be.** c
19ffc 6f 64 65 64 2e 20 20 54 68 65 20 73 79 6d 62 6f  oded.  The symbo
19ffd 6c 69 63 20 6c 61 62 65 6c 20 69 73 20 72 65 61  lic label is rea
19ffe 6c 6c 79 20 6a 75 73 74 20 61 20 6e 65 67 61 74  lly just a negat
19fff 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65  ive number.  The
1a000 0a 2a 2a 20 6c 61 62 65 6c 20 63 61 6e 20 62 65  .** label can be
1a001 20 75 73 65 64 20 61 73 20 74 68 65 20 50 32 20   used as the P2 
1a002 76 61 6c 75 65 20 6f 66 20 61 6e 20 6f 70 65 72  value of an oper
1a003 61 74 69 6f 6e 2e 20 20 4c 61 74 65 72 2c 20 77  ation.  Later, w
1a004 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c  hen.** the label
1a005 20 69 73 20 72 65 73 6f 6c 76 65 64 20 74 6f 20   is resolved to 
1a006 61 20 73 70 65 63 69 66 69 63 20 61 64 64 72 65  a specific addre
1a007 73 73 2c 20 74 68 65 20 56 44 42 45 20 77 69 6c  ss, the VDBE wil
1a008 6c 20 73 63 61 6e 0a 2a 2a 20 74 68 72 6f 75 67  l scan.** throug
1a009 68 20 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20  h its operation 
1a00a 6c 69 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20  list and change 
1a00b 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32  all values of P2
1a00c 20 77 68 69 63 68 20 6d 61 74 63 68 0a 2a 2a 20   which match.** 
1a00d 74 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74  the label into t
1a00e 68 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72  he resolved addr
1a00f 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56  ess..**.** The V
1a010 44 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20 61  DBE knows that a
1a011 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c   P2 value is a l
1a012 61 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61 62  abel because lab
1a013 65 6c 73 20 61 72 65 0a 2a 2a 20 61 6c 77 61 79  els are.** alway
1a014 73 20 6e 65 67 61 74 69 76 65 20 61 6e 64 20 50  s negative and P
1a015 32 20 76 61 6c 75 65 73 20 61 72 65 20 73 75 70  2 values are sup
1a016 70 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e 2d 6e  pose to be non-n
1a017 65 67 61 74 69 76 65 2e 0a 2a 2a 20 48 65 6e 63  egative..** Henc
1a018 65 2c 20 61 20 6e 65 67 61 74 69 76 65 20 50 32  e, a negative P2
1a019 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65   value is a labe
1a01a 6c 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74  l that has yet t
1a01b 6f 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a  o be resolved..*
1a01c 2a 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 72 65 74  *.** Zero is ret
1a01d 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f  urned if a mallo
1a01e 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51  c() fails..*/.SQ
1a01f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1a020 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1a021 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 29 7b 0a  Label(Vdbe *p){.
1a022 20 20 69 6e 74 20 69 3b 0a 20 20 69 20 3d 20 70    int i;.  i = p
1a023 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a 20 20 61 73  ->nLabel++;.  as
1a024 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
1a025 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
1a026 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e  );.  if( i>=p->n
1a027 4c 61 62 65 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20  LabelAlloc ){.  
1a028 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 4c 61    int n = p->nLa
1a029 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 35 3b 0a  belAlloc*2 + 5;.
1a02a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20      p->aLabel = 
1a02b 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
1a02c 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d  OrFree(p->db, p-
1a02d 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20 20  >aLabel,.       
1a02e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a02f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a030 6e 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62  n*sizeof(p->aLab
1a031 65 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 70 2d 3e  el[0]));.    p->
1a032 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 3d 20 73 71  nLabelAlloc = sq
1a033 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
1a034 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62  e(p->db, p->aLab
1a035 65 6c 29 2f 73 69 7a 65 6f 66 28 70 2d 3e 61 4c  el)/sizeof(p->aL
1a036 61 62 65 6c 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20  abel[0]);.  }.  
1a037 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b  if( p->aLabel ){
1a038 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 69  .    p->aLabel[i
1a039 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65  ] = -1;.  }.  re
1a03a 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a 2f 2a  turn -1-i;.}../*
1a03b 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6c 61 62 65  .** Resolve labe
1a03c 6c 20 22 78 22 20 74 6f 20 62 65 20 74 68 65 20  l "x" to be the 
1a03d 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e  address of the n
1a03e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
1a03f 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 65 72 74 65  to.** be inserte
1a040 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65  d.  The paramete
1a041 72 20 22 78 22 20 6d 75 73 74 20 68 61 76 65 20  r "x" must have 
1a042 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72  been obtained fr
1a043 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f 72 20 63 61  om.** a prior ca
1a044 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62  ll to sqlite3Vdb
1a045 65 4d 61 6b 65 4c 61 62 65 6c 28 29 2e 0a 2a 2f  eMakeLabel()..*/
1a046 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a047 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1a048 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62  ResolveLabel(Vdb
1a049 65 20 2a 70 2c 20 69 6e 74 20 78 29 7b 0a 20 20  e *p, int x){.  
1a04a 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a 20 20  int j = -1-x;.  
1a04b 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
1a04c 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
1a04d 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a  T );.  assert( j
1a04e 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62  >=0 && j<p->nLab
1a04f 65 6c 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  el );.  if( p->a
1a050 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e  Label ){.    p->
1a051 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e 6e  aLabel[j] = p->n
1a052 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65  Op;.  }.}..#ifde
1a053 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2f  f SQLITE_DEBUG /
1a054 2a 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d  * sqlite3AssertM
1a055 61 79 41 62 6f 72 74 28 29 20 6c 6f 67 69 63 20  ayAbort() logic 
1a056 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  */../*.** The fo
1a057 6c 6c 6f 77 69 6e 67 20 74 79 70 65 20 61 6e 64  llowing type and
1a058 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 75 73   function are us
1a059 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  ed to iterate th
1a05a 72 6f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64 65  rough all opcode
1a05b 73 0a 2a 2a 20 69 6e 20 61 20 56 64 62 65 20 6d  s.** in a Vdbe m
1a05c 61 69 6e 20 70 72 6f 67 72 61 6d 20 61 6e 64 20  ain program and 
1a05d 65 61 63 68 20 6f 66 20 74 68 65 20 73 75 62 2d  each of the sub-
1a05e 70 72 6f 67 72 61 6d 73 20 28 74 72 69 67 67 65  programs (trigge
1a05f 72 73 29 20 69 74 20 6d 61 79 20 0a 2a 2a 20 69  rs) it may .** i
1a060 6e 76 6f 6b 65 20 64 69 72 65 63 74 6c 79 20 6f  nvoke directly o
1a061 72 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 49 74  r indirectly. It
1a062 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20   should be used 
1a063 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
1a064 2a 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 2a 2a 20  *   Op *pOp;.** 
1a065 20 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74    VdbeOpIter sIt
1a066 65 72 3b 0a 2a 2a 0a 2a 2a 20 20 20 6d 65 6d 73  er;.**.**   mems
1a067 65 74 28 26 73 49 74 65 72 2c 20 30 2c 20 73 69  et(&sIter, 0, si
1a068 7a 65 6f 66 28 73 49 74 65 72 29 29 3b 0a 2a 2a  zeof(sIter));.**
1a069 20 20 20 73 49 74 65 72 2e 76 20 3d 20 76 3b 20     sIter.v = v; 
1a06a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a06b 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 76 20             // v 
1a06c 69 73 20 6f 66 20 74 79 70 65 20 56 64 62 65 2a  is of type Vdbe*
1a06d 20 0a 2a 2a 20 20 20 77 68 69 6c 65 28 20 28 70   .**   while( (p
1a06e 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 78 74 28  Op = opIterNext(
1a06f 26 73 49 74 65 72 29 29 20 29 7b 0a 2a 2a 20 20  &sIter)) ){.**  
1a070 20 20 20 2f 2f 20 44 6f 20 73 6f 6d 65 74 68 69     // Do somethi
1a071 6e 67 20 77 69 74 68 20 70 4f 70 0a 2a 2a 20 20  ng with pOp.**  
1a072 20 7d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 44   }.**   sqlite3D
1a073 62 46 72 65 65 28 76 2d 3e 64 62 2c 20 73 49 74  bFree(v->db, sIt
1a074 65 72 2e 61 70 53 75 62 29 3b 0a 2a 2a 20 0a 2a  er.apSub);.** .*
1a075 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
1a076 20 56 64 62 65 4f 70 49 74 65 72 20 56 64 62 65   VdbeOpIter Vdbe
1a077 4f 70 49 74 65 72 3b 0a 73 74 72 75 63 74 20 56  OpIter;.struct V
1a078 64 62 65 4f 70 49 74 65 72 20 7b 0a 20 20 56 64  dbeOpIter {.  Vd
1a079 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
1a07a 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65           /* Vdbe
1a07b 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
1a07c 75 67 68 20 74 68 65 20 6f 70 63 6f 64 65 73 20  ugh the opcodes 
1a07d 6f 66 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72  of */.  SubProgr
1a07e 61 6d 20 2a 2a 61 70 53 75 62 3b 20 20 20 20 20  am **apSub;     
1a07f 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73     /* Array of s
1a080 75 62 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 20 20  ubprograms */.  
1a081 69 6e 74 20 6e 53 75 62 3b 20 20 20 20 20 20 20  int nSub;       
1a082 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1a083 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
1a084 69 6e 20 61 70 53 75 62 20 2a 2f 0a 20 20 69 6e  in apSub */.  in
1a085 74 20 69 41 64 64 72 3b 20 20 20 20 20 20 20 20  t iAddr;        
1a086 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
1a087 65 73 73 20 6f 66 20 6e 65 78 74 20 69 6e 73 74  ess of next inst
1a088 72 75 63 74 69 6f 6e 20 74 6f 20 72 65 74 75 72  ruction to retur
1a089 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 75 62 3b  n */.  int iSub;
1a08a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a08b 20 20 2f 2a 20 30 20 3d 20 6d 61 69 6e 20 70 72    /* 0 = main pr
1a08c 6f 67 72 61 6d 2c 20 31 20 3d 20 66 69 72 73 74  ogram, 1 = first
1a08d 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 65 74 63   sub-program etc
1a08e 2e 20 2a 2f 0a 7d 3b 0a 73 74 61 74 69 63 20 4f  . */.};.static O
1a08f 70 20 2a 6f 70 49 74 65 72 4e 65 78 74 28 56 64  p *opIterNext(Vd
1a090 62 65 4f 70 49 74 65 72 20 2a 70 29 7b 0a 20 20  beOpIter *p){.  
1a091 56 64 62 65 20 2a 76 20 3d 20 70 2d 3e 76 3b 0a  Vdbe *v = p->v;.
1a092 20 20 4f 70 20 2a 70 52 65 74 20 3d 20 30 3b 0a    Op *pRet = 0;.
1a093 20 20 4f 70 20 2a 61 4f 70 3b 0a 20 20 69 6e 74    Op *aOp;.  int
1a094 20 6e 4f 70 3b 0a 0a 20 20 69 66 28 20 70 2d 3e   nOp;..  if( p->
1a095 69 53 75 62 3c 3d 70 2d 3e 6e 53 75 62 20 29 7b  iSub<=p->nSub ){
1a096 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 53 75  ..    if( p->iSu
1a097 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 4f  b==0 ){.      aO
1a098 70 20 3d 20 76 2d 3e 61 4f 70 3b 0a 20 20 20 20  p = v->aOp;.    
1a099 20 20 6e 4f 70 20 3d 20 76 2d 3e 6e 4f 70 3b 0a    nOp = v->nOp;.
1a09a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1a09b 20 61 4f 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b   aOp = p->apSub[
1a09c 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e 61 4f 70 3b  p->iSub-1]->aOp;
1a09d 0a 20 20 20 20 20 20 6e 4f 70 20 3d 20 70 2d 3e  .      nOp = p->
1a09e 61 70 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31 5d  apSub[p->iSub-1]
1a09f 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a 20 20 20  ->nOp;.    }.   
1a0a0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 41 64 64   assert( p->iAdd
1a0a1 72 3c 6e 4f 70 20 29 3b 0a 0a 20 20 20 20 70 52  r<nOp );..    pR
1a0a2 65 74 20 3d 20 26 61 4f 70 5b 70 2d 3e 69 41 64  et = &aOp[p->iAd
1a0a3 64 72 5d 3b 0a 20 20 20 20 70 2d 3e 69 41 64 64  dr];.    p->iAdd
1a0a4 72 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  r++;.    if( p->
1a0a5 69 41 64 64 72 3d 3d 6e 4f 70 20 29 7b 0a 20 20  iAddr==nOp ){.  
1a0a6 20 20 20 20 70 2d 3e 69 53 75 62 2b 2b 3b 0a 20      p->iSub++;. 
1a0a7 20 20 20 20 20 70 2d 3e 69 41 64 64 72 20 3d 20       p->iAddr = 
1a0a8 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  0;.    }.  .    
1a0a9 69 66 28 20 70 52 65 74 2d 3e 70 34 74 79 70 65  if( pRet->p4type
1a0aa 3d 3d 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20  ==P4_SUBPROGRAM 
1a0ab 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79  ){.      int nBy
1a0ac 74 65 20 3d 20 28 70 2d 3e 6e 53 75 62 2b 31 29  te = (p->nSub+1)
1a0ad 2a 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72  *sizeof(SubProgr
1a0ae 61 6d 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  am*);.      int 
1a0af 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  j;.      for(j=0
1a0b0 3b 20 6a 3c 70 2d 3e 6e 53 75 62 3b 20 6a 2b 2b  ; j<p->nSub; j++
1a0b1 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
1a0b2 2d 3e 61 70 53 75 62 5b 6a 5d 3d 3d 70 52 65 74  ->apSub[j]==pRet
1a0b3 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d 20 29 20  ->p4.pProgram ) 
1a0b4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1a0b5 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 2d 3e 6e       if( j==p->n
1a0b6 53 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70  Sub ){.        p
1a0b7 2d 3e 61 70 53 75 62 20 3d 20 73 71 6c 69 74 65  ->apSub = sqlite
1a0b8 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
1a0b9 28 76 2d 3e 64 62 2c 20 70 2d 3e 61 70 53 75 62  (v->db, p->apSub
1a0ba 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  , nByte);.      
1a0bb 20 20 69 66 28 20 21 70 2d 3e 61 70 53 75 62 20    if( !p->apSub 
1a0bc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52 65  ){.          pRe
1a0bd 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  t = 0;.        }
1a0be 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1a0bf 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 6e 53 75 62  p->apSub[p->nSub
1a0c0 2b 2b 5d 20 3d 20 70 52 65 74 2d 3e 70 34 2e 70  ++] = pRet->p4.p
1a0c1 50 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20 20 20  Program;.       
1a0c2 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1a0c3 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
1a0c4 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  Ret;.}../*.** Ch
1a0c5 65 63 6b 20 69 66 20 74 68 65 20 70 72 6f 67 72  eck if the progr
1a0c6 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  am stored in the
1a0c7 20 56 4d 20 61 73 73 6f 63 69 61 74 65 64 20 77   VM associated w
1a0c8 69 74 68 20 70 50 61 72 73 65 20 6d 61 79 0a 2a  ith pParse may.*
1a0c9 2a 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54  * throw an ABORT
1a0ca 20 65 78 63 65 70 74 69 6f 6e 20 28 63 61 75 73   exception (caus
1a0cb 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ing the statemen
1a0cc 74 2c 20 62 75 74 20 6e 6f 74 20 65 6e 74 69 72  t, but not entir
1a0cd 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  e transaction.**
1a0ce 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61   to be rolled ba
1a0cf 63 6b 29 2e 20 54 68 69 73 20 63 6f 6e 64 69 74  ck). This condit
1a0d0 69 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74  ion is true if t
1a0d1 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20  he main program 
1a0d2 6f 72 20 61 6e 79 0a 2a 2a 20 73 75 62 2d 70 72  or any.** sub-pr
1a0d3 6f 67 72 61 6d 73 20 63 6f 6e 74 61 69 6e 73 20  ograms contains 
1a0d4 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  any of the follo
1a0d5 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  wing:.**.**   * 
1a0d6 20 4f 50 5f 48 61 6c 74 20 77 69 74 68 20 50 31   OP_Halt with P1
1a0d7 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49  =SQLITE_CONSTRAI
1a0d8 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62 6f  NT and P2=OE_Abo
1a0d9 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48  rt..**   *  OP_H
1a0da 61 6c 74 49 66 4e 75 6c 6c 20 77 69 74 68 20 50  altIfNull with P
1a0db 31 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  1=SQLITE_CONSTRA
1a0dc 49 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62  INT and P2=OE_Ab
1a0dd 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f  ort..**   *  OP_
1a0de 44 65 73 74 72 6f 79 0a 2a 2a 20 20 20 2a 20 20  Destroy.**   *  
1a0df 4f 50 5f 56 55 70 64 61 74 65 0a 2a 2a 20 20 20  OP_VUpdate.**   
1a0e0 2a 20 20 4f 50 5f 56 52 65 6e 61 6d 65 0a 2a 2a  *  OP_VRename.**
1a0e1 20 20 20 2a 20 20 4f 50 5f 46 6b 43 6f 75 6e 74     *  OP_FkCount
1a0e2 65 72 20 77 69 74 68 20 50 32 3d 3d 30 20 28 69  er with P2==0 (i
1a0e3 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e  mmediate foreign
1a0e4 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 29   key constraint)
1a0e5 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 63 68 65 63  .**.** Then chec
1a0e6 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65  k that the value
1a0e7 20 6f 66 20 50 61 72 73 65 2e 6d 61 79 41 62 6f   of Parse.mayAbo
1a0e8 72 74 20 69 73 20 74 72 75 65 20 69 66 20 61 6e  rt is true if an
1a0e9 0a 2a 2a 20 41 42 4f 52 54 20 6d 61 79 20 62 65  .** ABORT may be
1a0ea 20 74 68 72 6f 77 6e 2c 20 6f 72 20 66 61 6c 73   thrown, or fals
1a0eb 65 20 6f 74 68 65 72 77 69 73 65 2e 20 52 65 74  e otherwise. Ret
1a0ec 75 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 64  urn true if it d
1a0ed 6f 65 73 0a 2a 2a 20 6d 61 74 63 68 2c 20 6f 72  oes.** match, or
1a0ee 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65   false otherwise
1a0ef 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
1a0f0 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62  is intended to b
1a0f1 65 20 75 73 65 64 20 61 73 0a 2a 2a 20 70 61 72  e used as.** par
1a0f2 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 20 73  t of an assert s
1a0f3 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  tatement in the 
1a0f4 63 6f 6d 70 69 6c 65 72 2e 20 53 69 6d 69 6c 61  compiler. Simila
1a0f5 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73  r to:.**.**   as
1a0f6 73 65 72 74 28 20 73 71 6c 69 74 65 33 56 64 62  sert( sqlite3Vdb
1a0f7 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28  eAssertMayAbort(
1a0f8 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 70  pParse->pVdbe, p
1a0f9 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29  Parse->mayAbort)
1a0fa 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   );.*/.SQLITE_PR
1a0fb 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1a0fc 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 62  3VdbeAssertMayAb
1a0fd 6f 72 74 28 56 64 62 65 20 2a 76 2c 20 69 6e 74  ort(Vdbe *v, int
1a0fe 20 6d 61 79 41 62 6f 72 74 29 7b 0a 20 20 69 6e   mayAbort){.  in
1a0ff 74 20 68 61 73 41 62 6f 72 74 20 3d 20 30 3b 0a  t hasAbort = 0;.
1a100 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62    Op *pOp;.  Vdb
1a101 65 4f 70 49 74 65 72 20 73 49 74 65 72 3b 0a 20  eOpIter sIter;. 
1a102 20 6d 65 6d 73 65 74 28 26 73 49 74 65 72 2c 20   memset(&sIter, 
1a103 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 65 72 29  0, sizeof(sIter)
1a104 29 3b 0a 20 20 73 49 74 65 72 2e 76 20 3d 20 76  );.  sIter.v = v
1a105 3b 0a 0a 20 20 77 68 69 6c 65 28 20 28 70 4f 70  ;..  while( (pOp
1a106 20 3d 20 6f 70 49 74 65 72 4e 65 78 74 28 26 73   = opIterNext(&s
1a107 49 74 65 72 29 29 21 3d 30 20 29 7b 0a 20 20 20  Iter))!=0 ){.   
1a108 20 69 6e 74 20 6f 70 63 6f 64 65 20 3d 20 70 4f   int opcode = pO
1a109 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 69  p->opcode;.    i
1a10a 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 44 65  f( opcode==OP_De
1a10b 73 74 72 6f 79 20 7c 7c 20 6f 70 63 6f 64 65 3d  stroy || opcode=
1a10c 3d 4f 50 5f 56 55 70 64 61 74 65 20 7c 7c 20 6f  =OP_VUpdate || o
1a10d 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 65 6e 61 6d  pcode==OP_VRenam
1a10e 65 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e .#ifndef SQLIT
1a10f 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
1a110 45 59 0a 20 20 20 20 20 7c 7c 20 28 6f 70 63 6f  EY.     || (opco
1a111 64 65 3d 3d 4f 50 5f 46 6b 43 6f 75 6e 74 65 72  de==OP_FkCounter
1a112 20 26 26 20 70 4f 70 2d 3e 70 31 3d 3d 30 20 26   && pOp->p1==0 &
1a113 26 20 70 4f 70 2d 3e 70 32 3d 3d 31 29 20 0a 23  & pOp->p2==1) .#
1a114 65 6e 64 69 66 0a 20 20 20 20 20 7c 7c 20 28 28  endif.     || ((
1a115 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20  opcode==OP_Halt 
1a116 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61  || opcode==OP_Ha
1a117 6c 74 49 66 4e 75 6c 6c 29 20 0a 20 20 20 20 20  ltIfNull) .     
1a118 20 26 26 20 28 70 4f 70 2d 3e 70 31 3d 3d 53 51   && (pOp->p1==SQ
1a119 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20  LITE_CONSTRAINT 
1a11a 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41  && pOp->p2==OE_A
1a11b 62 6f 72 74 29 29 0a 20 20 20 20 29 7b 0a 20 20  bort)).    ){.  
1a11c 20 20 20 20 68 61 73 41 62 6f 72 74 20 3d 20 31      hasAbort = 1
1a11d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1a11e 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
1a11f 65 33 44 62 46 72 65 65 28 76 2d 3e 64 62 2c 20  e3DbFree(v->db, 
1a120 73 49 74 65 72 2e 61 70 53 75 62 29 3b 0a 0a 20  sIter.apSub);.. 
1a121 20 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20   /* Return true 
1a122 69 66 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79  if hasAbort==may
1a123 41 62 6f 72 74 2e 20 4f 72 20 69 66 20 61 20 6d  Abort. Or if a m
1a124 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63  alloc failure oc
1a125 63 75 72 65 64 2e 0a 20 20 2a 2a 20 49 66 20 6d  cured..  ** If m
1a126 61 6c 6c 6f 63 20 66 61 69 6c 65 64 2c 20 74 68  alloc failed, th
1a127 65 6e 20 74 68 65 20 77 68 69 6c 65 28 29 20 6c  en the while() l
1a128 6f 6f 70 20 61 62 6f 76 65 20 6d 61 79 20 6e 6f  oop above may no
1a129 74 20 68 61 76 65 20 69 74 65 72 61 74 65 64 0a  t have iterated.
1a12a 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 61 6c 6c    ** through all
1a12b 20 6f 70 63 6f 64 65 73 20 61 6e 64 20 68 61 73   opcodes and has
1a12c 41 62 6f 72 74 20 6d 61 79 20 62 65 20 73 65 74  Abort may be set
1a12d 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 20 52 65   incorrectly. Re
1a12e 74 75 72 6e 0a 20 20 2a 2a 20 74 72 75 65 20 66  turn.  ** true f
1a12f 6f 72 20 74 68 69 73 20 63 61 73 65 20 74 6f 20  or this case to 
1a130 70 72 65 76 65 6e 74 20 74 68 65 20 61 73 73 65  prevent the asse
1a131 72 74 28 29 20 69 6e 20 74 68 65 20 63 61 6c 6c  rt() in the call
1a132 65 72 73 20 66 72 61 6d 65 0a 20 20 2a 2a 20 66  ers frame.  ** f
1a133 72 6f 6d 20 66 61 69 6c 69 6e 67 2e 20 20 2a 2f  rom failing.  */
1a134 0a 20 20 72 65 74 75 72 6e 20 28 20 76 2d 3e 64  .  return ( v->d
1a135 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a136 7c 7c 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79  || hasAbort==may
1a137 41 62 6f 72 74 20 29 3b 0a 7d 0a 23 65 6e 64 69  Abort );.}.#endi
1a138 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55  f /* SQLITE_DEBU
1a139 47 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33 41  G - the sqlite3A
1a13a 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 29 20  ssertMayAbort() 
1a13b 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a  function */../*.
1a13c 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  ** Loop through 
1a13d 74 68 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b  the program look
1a13e 69 6e 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65  ing for P2 value
1a13f 73 20 74 68 61 74 20 61 72 65 20 6e 65 67 61 74  s that are negat
1a140 69 76 65 0a 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69  ive.** on jump i
1a141 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 45 61  nstructions.  Ea
1a142 63 68 20 73 75 63 68 20 76 61 6c 75 65 20 69 73  ch such value is
1a143 20 61 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c   a label.  Resol
1a144 76 65 20 74 68 65 0a 2a 2a 20 6c 61 62 65 6c 20  ve the.** label 
1a145 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 50  by setting the P
1a146 32 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63  2 value to its c
1a147 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20  orrect non-zero 
1a148 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  value..**.** Thi
1a149 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
1a14a 6c 65 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61  led once after a
1a14b 6c 6c 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20  ll opcodes have 
1a14c 62 65 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a  been inserted..*
1a14d 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70  *.** Variable *p
1a14e 4d 61 78 46 75 6e 63 41 72 67 73 20 69 73 20 73  MaxFuncArgs is s
1a14f 65 74 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  et to the maximu
1a150 6d 20 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50  m value of any P
1a151 32 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74  2 argument .** t
1a152 6f 20 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  o an OP_Function
1a153 2c 20 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20  , OP_AggStep or 
1a154 4f 50 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64  OP_VFilter opcod
1a155 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  e. This is used 
1a156 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  by .** sqlite3Vd
1a157 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f  beMakeReady() to
1a158 20 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61   size the Vdbe.a
1a159 70 41 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f  pArg[] array..*/
1a15a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73  .static void res
1a15b 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 56 64 62  olveP2Values(Vdb
1a15c 65 20 2a 70 2c 20 69 6e 74 20 2a 70 4d 61 78 46  e *p, int *pMaxF
1a15d 75 6e 63 41 72 67 73 29 7b 0a 20 20 69 6e 74 20  uncArgs){.  int 
1a15e 69 3b 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67  i;.  int nMaxArg
1a15f 73 20 3d 20 2a 70 4d 61 78 46 75 6e 63 41 72 67  s = *pMaxFuncArg
1a160 73 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20  s;.  Op *pOp;.  
1a161 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d 20 70 2d  int *aLabel = p-
1a162 3e 61 4c 61 62 65 6c 3b 0a 20 20 70 2d 3e 72 65  >aLabel;.  p->re
1a163 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 66 6f  adOnly = 1;.  fo
1a164 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d  r(pOp=p->aOp, i=
1a165 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20  p->nOp-1; i>=0; 
1a166 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20  i--, pOp++){.   
1a167 20 75 38 20 6f 70 63 6f 64 65 20 3d 20 70 4f 70   u8 opcode = pOp
1a168 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 20 20 20 69  ->opcode;..    i
1a169 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 75  f( opcode==OP_Fu
1a16a 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65  nction || opcode
1a16b 3d 3d 4f 50 5f 41 67 67 53 74 65 70 20 29 7b 0a  ==OP_AggStep ){.
1a16c 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70        if( pOp->p
1a16d 35 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61  5>nMaxArgs ) nMa
1a16e 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 35 3b  xArgs = pOp->p5;
1a16f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a170 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1a171 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  E.    }else if( 
1a172 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61  opcode==OP_VUpda
1a173 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  te ){.      if( 
1a174 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 41 72 67 73  pOp->p2>nMaxArgs
1a175 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f   ) nMaxArgs = pO
1a176 70 2d 3e 70 32 3b 0a 23 65 6e 64 69 66 0a 20 20  p->p2;.#endif.  
1a177 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f    }else if( opco
1a178 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63 74 69  de==OP_Transacti
1a179 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21 3d 30  on && pOp->p2!=0
1a17a 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 65 61   ){.      p->rea
1a17b 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 6e 64  dOnly = 0;.#ifnd
1a17c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1a17d 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
1a17e 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65  }else if( opcode
1a17f 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a  ==OP_VFilter ){.
1a180 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20        int n;.   
1a181 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f     assert( p->nO
1a182 70 20 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20  p - i >= 3 );.  
1a183 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b      assert( pOp[
1a184 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49  -1].opcode==OP_I
1a185 6e 74 65 67 65 72 20 29 3b 0a 20 20 20 20 20 20  nteger );.      
1a186 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a  n = pOp[-1].p1;.
1a187 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78        if( n>nMax
1a188 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20  Args ) nMaxArgs 
1a189 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  = n;.#endif.    
1a18a 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  }..    if( sqlit
1a18b 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50  e3VdbeOpcodeHasP
1a18c 72 6f 70 65 72 74 79 28 6f 70 63 6f 64 65 2c 20  roperty(opcode, 
1a18d 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70  OPFLG_JUMP) && p
1a18e 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20  Op->p2<0 ){.    
1a18f 20 20 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70    assert( -1-pOp
1a190 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29  ->p2<p->nLabel )
1a191 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20  ;.      pOp->p2 
1a192 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d  = aLabel[-1-pOp-
1a193 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  >p2];.    }.  }.
1a194 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1a195 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c  p->db, p->aLabel
1a196 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d  );.  p->aLabel =
1a197 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63   0;..  *pMaxFunc
1a198 41 72 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b  Args = nMaxArgs;
1a199 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1a19a 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
1a19b 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
1a19c 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73 65 72  tion to be inser
1a19d 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
1a19e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1a19f 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
1a1a0 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 61 73  r(Vdbe *p){.  as
1a1a1 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
1a1a2 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
1a1a3 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e  );.  return p->n
1a1a4 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  Op;.}../*.** Thi
1a1a5 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
1a1a6 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
1a1a7 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70 63  the array of opc
1a1a8 6f 64 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  odes associated 
1a1a9 77 69 74 68 0a 2a 2a 20 74 68 65 20 56 64 62 65  with.** the Vdbe
1a1aa 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
1a1ab 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 49  irst argument. I
1a1ac 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72 73  t is the callers
1a1ad 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a   responsibility.
1a1ae 2a 2a 20 74 6f 20 61 72 72 61 6e 67 65 20 66 6f  ** to arrange fo
1a1af 72 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61  r the returned a
1a1b0 72 72 61 79 20 74 6f 20 62 65 20 65 76 65 6e 74  rray to be event
1a1b1 75 61 6c 6c 79 20 66 72 65 65 64 20 75 73 69 6e  ually freed usin
1a1b2 67 20 74 68 65 20 0a 2a 2a 20 76 64 62 65 46 72  g the .** vdbeFr
1a1b3 65 65 4f 70 41 72 72 61 79 28 29 20 66 75 6e 63  eeOpArray() func
1a1b4 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f  tion..**.** Befo
1a1b5 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70  re returning, *p
1a1b6 6e 4f 70 20 69 73 20 73 65 74 20 74 6f 20 74 68  nOp is set to th
1a1b7 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
1a1b8 69 65 73 20 69 6e 20 74 68 65 20 72 65 74 75 72  ies in the retur
1a1b9 6e 65 64 0a 2a 2a 20 61 72 72 61 79 2e 20 41 6c  ned.** array. Al
1a1ba 73 6f 2c 20 2a 70 6e 4d 61 78 41 72 67 20 69 73  so, *pnMaxArg is
1a1bb 20 73 65 74 20 74 6f 20 74 68 65 20 6c 61 72 67   set to the larg
1a1bc 65 72 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e  er of its curren
1a1bd 74 20 76 61 6c 75 65 20 61 6e 64 20 0a 2a 2a 20  t value and .** 
1a1be 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e  the number of en
1a1bf 74 72 69 65 73 20 69 6e 20 74 68 65 20 56 64 62  tries in the Vdb
1a1c0 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 79 20  e.apArg[] array 
1a1c1 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 65 63  required to exec
1a1c2 75 74 65 20 74 68 65 20 0a 2a 2a 20 72 65 74 75  ute the .** retu
1a1c3 72 6e 65 64 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f  rned program..*/
1a1c4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a1c5 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56  VdbeOp *sqlite3V
1a1c6 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 56  dbeTakeOpArray(V
1a1c7 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 4f  dbe *p, int *pnO
1a1c8 70 2c 20 69 6e 74 20 2a 70 6e 4d 61 78 41 72 67  p, int *pnMaxArg
1a1c9 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f 70  ){.  VdbeOp *aOp
1a1ca 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 61 73 73   = p->aOp;.  ass
1a1cb 65 72 74 28 20 61 4f 70 20 26 26 20 21 70 2d 3e  ert( aOp && !p->
1a1cc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1a1cd 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20   );..  /* Check 
1a1ce 74 68 61 74 20 73 71 6c 69 74 65 33 56 64 62 65  that sqlite3Vdbe
1a1cf 55 73 65 73 42 74 72 65 65 28 29 20 77 61 73 20  UsesBtree() was 
1a1d0 6e 6f 74 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68  not called on th
1a1d1 69 73 20 56 4d 20 2a 2f 0a 20 20 61 73 73 65 72  is VM */.  asser
1a1d2 74 28 20 70 2d 3e 61 4d 75 74 65 78 2e 6e 4d 75  t( p->aMutex.nMu
1a1d3 74 65 78 3d 3d 30 20 29 3b 0a 0a 20 20 72 65 73  tex==0 );..  res
1a1d4 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 70 2c 20  olveP2Values(p, 
1a1d5 70 6e 4d 61 78 41 72 67 29 3b 0a 20 20 2a 70 6e  pnMaxArg);.  *pn
1a1d6 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 70  Op = p->nOp;.  p
1a1d7 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 72 65 74  ->aOp = 0;.  ret
1a1d8 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn aOp;.}../*.*
1a1d9 2a 20 41 64 64 20 61 20 77 68 6f 6c 65 20 6c 69  * Add a whole li
1a1da 73 74 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73  st of operations
1a1db 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f   to the operatio
1a1dc 6e 20 73 74 61 63 6b 2e 20 20 52 65 74 75 72 6e  n stack.  Return
1a1dd 20 74 68 65 0a 2a 2a 20 61 64 64 72 65 73 73 20   the.** address 
1a1de 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f 70 65  of the first ope
1a1df 72 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f  ration added..*/
1a1e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a1e1 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
1a1e2 64 64 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70  ddOpList(Vdbe *p
1a1e3 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f  , int nOp, VdbeO
1a1e4 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70  pList const *aOp
1a1e5 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20  ){.  int addr;. 
1a1e6 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
1a1e7 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  c==VDBE_MAGIC_IN
1a1e8 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  IT );.  if( p->n
1a1e9 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f  Op + nOp > p->nO
1a1ea 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f 70  pAlloc && growOp
1a1eb 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20  Array(p) ){.    
1a1ec 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1a1ed 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20  addr = p->nOp;. 
1a1ee 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f 70 3e   if( ALWAYS(nOp>
1a1ef 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  0) ){.    int i;
1a1f0 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20  .    VdbeOpList 
1a1f1 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70  const *pIn = aOp
1a1f2 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
1a1f3 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b  <nOp; i++, pIn++
1a1f4 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20  ){.      int p2 
1a1f5 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20  = pIn->p2;.     
1a1f6 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20   VdbeOp *pOut = 
1a1f7 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b  &p->aOp[i+addr];
1a1f8 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63  .      pOut->opc
1a1f9 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64  ode = pIn->opcod
1a1fa 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  e;.      pOut->p
1a1fb 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20  1 = pIn->p1;.   
1a1fc 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 73     if( p2<0 && s
1a1fd 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65  qlite3VdbeOpcode
1a1fe 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 75 74  HasProperty(pOut
1a1ff 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f  ->opcode, OPFLG_
1a200 4a 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 20 20  JUMP) ){.       
1a201 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72   pOut->p2 = addr
1a202 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20   + ADDR(p2);.   
1a203 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1a204 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b    pOut->p2 = p2;
1a205 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1a206 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70  Out->p3 = pIn->p
1a207 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  3;.      pOut->p
1a208 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53  4type = P4_NOTUS
1a209 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  ED;.      pOut->
1a20a 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20  p4.p = 0;.      
1a20b 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69  pOut->p5 = 0;.#i
1a20c 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1a20d 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43  G.      pOut->zC
1a20e 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20  omment = 0;.    
1a20f 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1a210 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20  eAddopTrace ){. 
1a211 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1a212 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61  bePrintOp(0, i+a
1a213 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61  ddr, &p->aOp[i+a
1a214 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23  ddr]);.      }.#
1a215 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20  endif.    }.    
1a216 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20  p->nOp += nOp;. 
1a217 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72   }.  return addr
1a218 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
1a219 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
1a21a 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f  he P1 operand fo
1a21b 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73  r a specific ins
1a21c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  truction..** Thi
1a21d 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
1a21e 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65  ful when a large
1a21f 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64   program is load
1a220 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61  ed from a.** sta
1a221 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20  tic array using 
1a222 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1a223 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74  List but we want
1a224 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65   to make a.** fe
1a225 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20  w minor changes 
1a226 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a  to the program..
1a227 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a228 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a229 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20  beChangeP1(Vdbe 
1a22a 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e  *p, int addr, in
1a22b 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74  t val){.  assert
1a22c 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( p!=0 );.  asse
1a22d 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20  rt( addr>=0 );. 
1a22e 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72   if( p->nOp>addr
1a22f 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61   ){.    p->aOp[a
1a230 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b 0a 20  ddr].p1 = val;. 
1a231 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
1a232 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
1a233 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 66  the P2 operand f
1a234 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e  or a specific in
1a235 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68  struction..** Th
1a236 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
1a237 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69 6e 67  eful for setting
1a238 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74   a jump destinat
1a239 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
1a23a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a23b 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1a23c 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64  Vdbe *p, int add
1a23d 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61  r, int val){.  a
1a23e 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
1a23f 20 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30   assert( addr>=0
1a240 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70   );.  if( p->nOp
1a241 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e  >addr ){.    p->
1a242 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20 3d 20 76  aOp[addr].p2 = v
1a243 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  al;.  }.}../*.**
1a244 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   Change the valu
1a245 65 20 6f 66 20 74 68 65 20 50 33 20 6f 70 65 72  e of the P3 oper
1a246 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66  and for a specif
1a247 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ic instruction..
1a248 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a249 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a24a 62 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 20  beChangeP3(Vdbe 
1a24b 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e  *p, int addr, in
1a24c 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74  t val){.  assert
1a24d 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( p!=0 );.  asse
1a24e 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20  rt( addr>=0 );. 
1a24f 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72   if( p->nOp>addr
1a250 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61   ){.    p->aOp[a
1a251 64 64 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a 20  ddr].p3 = val;. 
1a252 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
1a253 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
1a254 74 68 65 20 50 35 20 6f 70 65 72 61 6e 64 20 66  the P5 operand f
1a255 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  or the most rece
1a256 6e 74 6c 79 0a 2a 2a 20 61 64 64 65 64 20 6f 70  ntly.** added op
1a257 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  eration..*/.SQLI
1a258 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a259 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1a25a 65 50 35 28 56 64 62 65 20 2a 70 2c 20 75 38 20  eP5(Vdbe *p, u8 
1a25b 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  val){.  assert( 
1a25c 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d  p!=0 );.  if( p-
1a25d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65  >aOp ){.    asse
1a25e 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a  rt( p->nOp>0 );.
1a25f 20 20 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f      p->aOp[p->nO
1a260 70 2d 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20  p-1].p5 = val;. 
1a261 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
1a262 67 65 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e  ge the P2 operan
1a263 64 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e  d of instruction
1a264 20 61 64 64 72 20 73 6f 20 74 68 61 74 20 69 74   addr so that it
1a265 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68   points to.** th
1a266 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
1a267 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1a268 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a  n to be coded..*
1a269 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a26a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1a26b 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a  eJumpHere(Vdbe *
1a26c 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20  p, int addr){.  
1a26d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1a26e 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e  eP2(p, addr, p->
1a26f 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  nOp);.}.../*.** 
1a270 49 66 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e  If the input Fun
1a271 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69  cDef structure i
1a272 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65  s ephemeral, the
1a273 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a  n free it.  If.*
1a274 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20 69 73  * the FuncDef is
1a275 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74   not ephermal, t
1a276 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a  hen do nothing..
1a277 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
1a278 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63  reeEphemeralFunc
1a279 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62  tion(sqlite3 *db
1a27a 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29  , FuncDef *pDef)
1a27b 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  {.  if( ALWAYS(p
1a27c 44 65 66 29 20 26 26 20 28 70 44 65 66 2d 3e 66  Def) && (pDef->f
1a27d 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55  lags & SQLITE_FU
1a27e 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a  NC_EPHEM)!=0 ){.
1a27f 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1a280 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d  e(db, pDef);.  }
1a281 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
1a282 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e   a P4 value if n
1a283 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61  ecessary..*/.sta
1a284 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 28  tic void freeP4(
1a285 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
1a286 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70   p4type, void *p
1a287 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a  4){.  if( p4 ){.
1a288 20 20 20 20 73 77 69 74 63 68 28 20 70 34 74 79      switch( p4ty
1a289 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  pe ){.      case
1a28a 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20   P4_REAL:.      
1a28b 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20  case P4_INT64:. 
1a28c 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52       case P4_MPR
1a28d 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65  INTF:.      case
1a28e 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20   P4_DYNAMIC:.   
1a28f 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e     case P4_KEYIN
1a290 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50  FO:.      case P
1a291 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20  4_INTARRAY:.    
1a292 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46    case P4_KEYINF
1a293 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20  O_HANDOFF: {.   
1a294 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1a295 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20  ee(db, p4);.    
1a296 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1a297 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34   }.      case P4
1a298 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20  _VDBEFUNC: {.   
1a299 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70       VdbeFunc *p
1a29a 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65  VdbeFunc = (Vdbe
1a29b 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20  Func *)p4;.     
1a29c 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c     freeEphemeral
1a29d 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64  Function(db, pVd
1a29e 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a  beFunc->pFunc);.
1a29f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1a2a0 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61  dbeDeleteAuxData
1a2a1 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a  (pVdbeFunc, 0);.
1a2a2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1a2a3 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 46  bFree(db, pVdbeF
1a2a4 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72  unc);.        br
1a2a5 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1a2a6 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44     case P4_FUNCD
1a2a7 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72  EF: {.        fr
1a2a8 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74  eeEphemeralFunct
1a2a9 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66  ion(db, (FuncDef
1a2aa 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62  *)p4);.        b
1a2ab 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1a2ac 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a      case P4_MEM:
1a2ad 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
1a2ae 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c  e3ValueFree((sql
1a2af 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b  ite3_value*)p4);
1a2b0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1a2b1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
1a2b2 73 65 20 50 34 5f 56 54 41 42 20 3a 20 7b 0a 20  se P4_VTAB : {. 
1a2b3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74         sqlite3Vt
1a2b4 61 62 55 6e 6c 6f 63 6b 28 28 56 54 61 62 6c 65  abUnlock((VTable
1a2b5 20 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20   *)p4);.        
1a2b6 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1a2b7 20 20 20 20 20 63 61 73 65 20 50 34 5f 53 55 42       case P4_SUB
1a2b8 50 52 4f 47 52 41 4d 20 3a 20 7b 0a 20 20 20 20  PROGRAM : {.    
1a2b9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50      sqlite3VdbeP
1a2ba 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 64 62 2c  rogramDelete(db,
1a2bb 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 29 70   (SubProgram *)p
1a2bc 34 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 62  4, 1);.        b
1a2bd 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1a2be 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1a2bf 20 46 72 65 65 20 74 68 65 20 73 70 61 63 65 20   Free the space 
1a2c0 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 4f  allocated for aO
1a2c1 70 20 61 6e 64 20 61 6e 79 20 70 34 20 76 61 6c  p and any p4 val
1a2c2 75 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ues allocated fo
1a2c3 72 20 74 68 65 0a 2a 2a 20 6f 70 63 6f 64 65 73  r the.** opcodes
1a2c4 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
1a2c5 6e 2e 20 49 66 20 61 4f 70 20 69 73 20 6e 6f 74  n. If aOp is not
1a2c6 20 4e 55 4c 4c 20 69 74 20 69 73 20 61 73 73 75   NULL it is assu
1a2c7 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 0a  med to contain .
1a2c8 2a 2a 20 6e 4f 70 20 65 6e 74 72 69 65 73 2e 20  ** nOp entries. 
1a2c9 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1a2ca 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28  vdbeFreeOpArray(
1a2cb 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 4f 70 20  sqlite3 *db, Op 
1a2cc 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f 70 29 7b 0a  *aOp, int nOp){.
1a2cd 20 20 69 66 28 20 61 4f 70 20 29 7b 0a 20 20 20    if( aOp ){.   
1a2ce 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 66 6f   Op *pOp;.    fo
1a2cf 72 28 70 4f 70 3d 61 4f 70 3b 20 70 4f 70 3c 26  r(pOp=aOp; pOp<&
1a2d0 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f 70 2b 2b 29  aOp[nOp]; pOp++)
1a2d1 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 64  {.      freeP4(d
1a2d2 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20  b, pOp->p4type, 
1a2d3 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64  pOp->p4.p);.#ifd
1a2d4 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1a2d5 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1a2d6 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f  ree(db, pOp->zCo
1a2d7 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20  mment);.#endif  
1a2d8 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20     .    }.  }.  
1a2d9 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1a2da 2c 20 61 4f 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  , aOp);.}../*.**
1a2db 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72   Decrement the r
1a2dc 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 74 68 65 20  ef-count on the 
1a2dd 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63  SubProgram struc
1a2de 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74  ture passed as t
1a2df 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67  he.** second arg
1a2e0 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 72 65  ument. If the re
1a2e1 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20  f-count reaches 
1a2e2 7a 65 72 6f 2c 20 66 72 65 65 20 74 68 65 20 73  zero, free the s
1a2e3 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
1a2e4 54 68 65 20 61 72 72 61 79 20 6f 66 20 56 44 42  The array of VDB
1a2e5 45 20 6f 70 63 6f 64 65 73 20 73 74 6f 72 65 64  E opcodes stored
1a2e6 20 61 73 20 53 75 62 50 72 6f 67 72 61 6d 2e 61   as SubProgram.a
1a2e7 4f 70 20 69 73 20 66 72 65 65 64 20 69 66 0a 2a  Op is freed if.*
1a2e8 2a 20 65 69 74 68 65 72 20 74 68 65 20 72 65 66  * either the ref
1a2e9 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a  -count reaches z
1a2ea 65 72 6f 20 6f 72 20 70 61 72 61 6d 65 74 65 72  ero or parameter
1a2eb 20 66 72 65 65 6f 70 20 69 73 20 6e 6f 6e 2d 7a   freeop is non-z
1a2ec 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65  ero..**.** Since
1a2ed 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70   the array of op
1a2ee 63 6f 64 65 73 20 70 6f 69 6e 74 65 64 20 74 6f  codes pointed to
1a2ef 20 62 79 20 53 75 62 50 72 6f 67 72 61 6d 2e 61   by SubProgram.a
1a2f0 4f 70 20 6d 61 79 20 64 69 72 65 63 74 6c 79 0a  Op may directly.
1a2f1 2a 2a 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79  ** or indirectly
1a2f2 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66 65 72   contain a refer
1a2f3 65 6e 63 65 20 74 6f 20 74 68 65 20 53 75 62 50  ence to the SubP
1a2f4 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72 65  rogram structure
1a2f5 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 42 79 20 70   itself..** By p
1a2f6 61 73 73 69 6e 67 20 61 20 6e 6f 6e 2d 7a 65 72  assing a non-zer
1a2f7 6f 20 66 72 65 65 6f 70 20 70 61 72 61 6d 65 74  o freeop paramet
1a2f8 65 72 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d  er, the caller m
1a2f9 61 79 20 65 6e 73 75 72 65 20 74 68 61 74 20 61  ay ensure that a
1a2fa 6c 6c 0a 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d  ll.** SubProgram
1a2fb 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64 20   structures and 
1a2fc 74 68 65 69 72 20 61 4f 70 20 61 72 72 61 79 73  their aOp arrays
1a2fd 20 61 72 65 20 66 72 65 65 64 2c 20 65 76 65 6e   are freed, even
1a2fe 20 77 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 61   when there.** a
1a2ff 72 65 20 73 75 63 68 20 63 69 72 63 75 6c 61 72  re such circular
1a300 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2f 0a   references..*/.
1a301 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a302 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50  oid sqlite3VdbeP
1a303 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 73 71 6c  rogramDelete(sql
1a304 69 74 65 33 20 2a 64 62 2c 20 53 75 62 50 72 6f  ite3 *db, SubPro
1a305 67 72 61 6d 20 2a 70 2c 20 69 6e 74 20 66 72 65  gram *p, int fre
1a306 65 6f 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b  eop){.  if( p ){
1a307 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1a308 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 69 66  nRef>0 );.    if
1a309 28 20 66 72 65 65 6f 70 20 7c 7c 20 70 2d 3e 6e  ( freeop || p->n
1a30a 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  Ref==1 ){.      
1a30b 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70  Op *aOp = p->aOp
1a30c 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 20 3d  ;.      p->aOp =
1a30d 20 30 3b 0a 20 20 20 20 20 20 76 64 62 65 46 72   0;.      vdbeFr
1a30e 65 65 4f 70 41 72 72 61 79 28 64 62 2c 20 61 4f  eeOpArray(db, aO
1a30f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 20 20  p, p->nOp);.    
1a310 20 20 70 2d 3e 6e 4f 70 20 3d 20 30 3b 0a 20 20    p->nOp = 0;.  
1a311 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2d    }.    p->nRef-
1a312 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52  -;.    if( p->nR
1a313 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ef==0 ){.      s
1a314 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1a315 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d   p);.    }.  }.}
1a316 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  .../*.** Change 
1a317 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 72 74 69  N opcodes starti
1a318 6e 67 20 61 74 20 61 64 64 72 20 74 6f 20 4e 6f  ng at addr to No
1a319 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  -ops..*/.SQLITE_
1a31a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a31b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f  ite3VdbeChangeTo
1a31c 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c 20 69 6e  Noop(Vdbe *p, in
1a31d 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 7b 0a  t addr, int N){.
1a31e 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a    if( p->aOp ){.
1a31f 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20      VdbeOp *pOp 
1a320 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b  = &p->aOp[addr];
1a321 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
1a322 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 68   = p->db;.    wh
1a323 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20  ile( N-- ){.    
1a324 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70    freeP4(db, pOp
1a325 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70  ->p4type, pOp->p
1a326 34 2e 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73  4.p);.      mems
1a327 65 74 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f  et(pOp, 0, sizeo
1a328 66 28 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20  f(pOp[0]));.    
1a329 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20    pOp->opcode = 
1a32a 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70  OP_Noop;.      p
1a32b 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Op++;.    }.  }.
1a32c 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
1a32d 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1a32e 20 50 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 20   P4 operand for 
1a32f 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72  a specific instr
1a330 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20  uction..** This 
1a331 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75  routine is usefu
1a332 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70  l when a large p
1a333 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64  rogram is loaded
1a334 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69   from a.** stati
1a335 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 71  c array using sq
1a336 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
1a337 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 74  st but we want t
1a338 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20  o make a.** few 
1a339 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f  minor changes to
1a33a 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a   the program..**
1a33b 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e  .** If n>=0 then
1a33c 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20   the P4 operand 
1a33d 69 73 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e  is dynamic, mean
1a33e 69 6e 67 20 74 68 61 74 20 61 20 63 6f 70 79 20  ing that a copy 
1a33f 6f 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  of.** the string
1a340 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65   is made into me
1a341 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
1a342 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
1a343 63 28 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20  c()..** A value 
1a344 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f  of n==0 means co
1a345 70 79 20 62 79 74 65 73 20 6f 66 20 7a 50 34 20  py bytes of zP4 
1a346 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64  up to and includ
1a347 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  ing the.** first
1a348 20 6e 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20   null byte.  If 
1a349 6e 3e 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b  n>0 then copy n+
1a34a 31 20 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0a  1 bytes of zP4..
1a34b 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b  **.** If n==P4_K
1a34c 45 59 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20  EYINFO it means 
1a34d 74 68 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f  that zP4 is a po
1a34e 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e  inter to a KeyIn
1a34f 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  fo structure..**
1a350 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20   A copy is made 
1a351 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  of the KeyInfo s
1a352 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65  tructure into me
1a353 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
1a354 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61  om.** sqlite3_ma
1a355 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65  lloc, to be free
1a356 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20  d when the Vdbe 
1a357 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a  is finalized..**
1a358 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48   n==P4_KEYINFO_H
1a359 41 4e 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73  ANDOFF indicates
1a35a 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73   that zP4 points
1a35b 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74   to a KeyInfo st
1a35c 72 75 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65  ructure.** store
1a35d 64 20 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74  d in memory that
1a35e 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20   the caller has 
1a35f 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
1a360 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68  lite3_malloc. Th
1a361 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f  e .** caller sho
1a362 75 6c 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65  uld not free the
1a363 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20   allocation, it 
1a364 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68  will be freed wh
1a365 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a  en the Vdbe is.*
1a366 2a 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20  * finalized..** 
1a367 0a 2a 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73  .** Other values
1a368 20 6f 66 20 6e 20 28 50 34 5f 53 54 41 54 49 43   of n (P4_STATIC
1a369 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 63  , P4_COLLSEQ etc
1a36a 2e 29 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  .) indicate that
1a36b 20 7a 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 74   zP4 points.** t
1a36c 6f 20 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74  o a string or st
1a36d 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20  ructure that is 
1a36e 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78  guaranteed to ex
1a36f 69 73 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65  ist for the life
1a370 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56  time of.** the V
1a371 64 62 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61  dbe. In these ca
1a372 73 65 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20  ses we can just 
1a373 63 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72  copy the pointer
1a374 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c  ..**.** If addr<
1a375 30 20 74 68 65 6e 20 63 68 61 6e 67 65 20 50 34  0 then change P4
1a376 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63   on the most rec
1a377 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69  ently inserted i
1a378 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  nstruction..*/.S
1a379 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1a37a 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
1a37b 61 6e 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20  angeP4(Vdbe *p, 
1a37c 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20  int addr, const 
1a37d 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e  char *zP4, int n
1a37e 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20  ){.  Op *pOp;.  
1a37f 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 61  sqlite3 *db;.  a
1a380 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
1a381 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61   db = p->db;.  a
1a382 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d  ssert( p->magic=
1a383 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54  =VDBE_MAGIC_INIT
1a384 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70   );.  if( p->aOp
1a385 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ==0 || db->mallo
1a386 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69  cFailed ){.    i
1a387 66 20 28 20 6e 21 3d 50 34 5f 4b 45 59 49 4e 46  f ( n!=P4_KEYINF
1a388 4f 20 26 26 20 6e 21 3d 50 34 5f 56 54 41 42 20  O && n!=P4_VTAB 
1a389 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34  ) {.      freeP4
1a38a 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 2a  (db, n, (void*)*
1a38b 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 3b 0a 20  (char**)&zP4);. 
1a38c 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 3b     }.    return;
1a38d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
1a38e 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 61 73 73  ->nOp>0 );.  ass
1a38f 65 72 74 28 20 61 64 64 72 3c 70 2d 3e 6e 4f 70  ert( addr<p->nOp
1a390 20 29 3b 0a 20 20 69 66 28 20 61 64 64 72 3c 30   );.  if( addr<0
1a391 20 29 7b 0a 20 20 20 20 61 64 64 72 20 3d 20 70   ){.    addr = p
1a392 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20  ->nOp - 1;.  }. 
1a393 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61   pOp = &p->aOp[a
1a394 64 64 72 5d 3b 0a 20 20 66 72 65 65 50 34 28 64  ddr];.  freeP4(d
1a395 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20  b, pOp->p4type, 
1a396 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f  pOp->p4.p);.  pO
1a397 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69  p->p4.p = 0;.  i
1a398 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29  f( n==P4_INT32 )
1a399 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74  {.    /* Note: t
1a39a 68 69 73 20 63 61 73 74 20 69 73 20 73 61 66 65  his cast is safe
1a39b 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 6f 72  , because the or
1a39c 69 67 69 6e 20 64 61 74 61 20 70 6f 69 6e 74 20  igin data point 
1a39d 77 61 73 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a  was an int.    *
1a39e 2a 20 74 68 61 74 20 77 61 73 20 63 61 73 74 20  * that was cast 
1a39f 74 6f 20 61 20 28 63 6f 6e 73 74 20 63 68 61 72  to a (const char
1a3a0 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d   *). */.    pOp-
1a3a1 3e 70 34 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50  >p4.i = SQLITE_P
1a3a2 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a  TR_TO_INT(zP4);.
1a3a3 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
1a3a4 3d 20 50 34 5f 49 4e 54 33 32 3b 0a 20 20 7d 65  = P4_INT32;.  }e
1a3a5 6c 73 65 20 69 66 28 20 7a 50 34 3d 3d 30 20 29  lse if( zP4==0 )
1a3a6 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20  {.    pOp->p4.p 
1a3a7 3d 20 30 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  = 0;.    pOp->p4
1a3a8 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45  type = P4_NOTUSE
1a3a9 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e  D;.  }else if( n
1a3aa 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a  ==P4_KEYINFO ){.
1a3ab 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
1a3ac 79 49 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e  yInfo;.    int n
1a3ad 46 69 65 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20  Field, nByte;.. 
1a3ae 20 20 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65     nField = ((Ke
1a3af 79 49 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69  yInfo*)zP4)->nFi
1a3b0 65 6c 64 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d  eld;.    nByte =
1a3b1 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66   sizeof(*pKeyInf
1a3b2 6f 29 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a  o) + (nField-1)*
1a3b3 73 69 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d  sizeof(pKeyInfo-
1a3b4 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69  >aColl[0]) + nFi
1a3b5 65 6c 64 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66  eld;.    pKeyInf
1a3b6 6f 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  o = sqlite3Mallo
1a3b7 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 20 20  c( nByte );.    
1a3b8 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f  pOp->p4.pKeyInfo
1a3b9 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20   = pKeyInfo;.   
1a3ba 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b   if( pKeyInfo ){
1a3bb 0a 20 20 20 20 20 20 75 38 20 2a 61 53 6f 72 74  .      u8 *aSort
1a3bc 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d  Order;.      mem
1a3bd 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50  cpy(pKeyInfo, zP
1a3be 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  4, nByte);.     
1a3bf 20 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b   aSortOrder = pK
1a3c0 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64  eyInfo->aSortOrd
1a3c1 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 61 53  er;.      if( aS
1a3c2 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20  ortOrder ){.    
1a3c3 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53      pKeyInfo->aS
1a3c4 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 6e 73 69  ortOrder = (unsi
1a3c5 67 6e 65 64 20 63 68 61 72 2a 29 26 70 4b 65 79  gned char*)&pKey
1a3c6 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65  Info->aColl[nFie
1a3c7 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  ld];.        mem
1a3c8 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53  cpy(pKeyInfo->aS
1a3c9 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f  ortOrder, aSortO
1a3ca 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20  rder, nField);. 
1a3cb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70       }.      pOp
1a3cc 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45  ->p4type = P4_KE
1a3cd 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65  YINFO;.    }else
1a3ce 7b 0a 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d  {.      p->db->m
1a3cf 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1a3d0 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  .      pOp->p4ty
1a3d1 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b  pe = P4_NOTUSED;
1a3d2 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
1a3d3 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f  f( n==P4_KEYINFO
1a3d4 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20  _HANDOFF ){.    
1a3d5 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69  pOp->p4.p = (voi
1a3d6 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d  d*)zP4;.    pOp-
1a3d7 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59  >p4type = P4_KEY
1a3d8 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66  INFO;.  }else if
1a3d9 28 20 6e 3d 3d 50 34 5f 56 54 41 42 20 29 7b 0a  ( n==P4_VTAB ){.
1a3da 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20      pOp->p4.p = 
1a3db 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20  (void*)zP4;.    
1a3dc 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
1a3dd 5f 56 54 41 42 3b 0a 20 20 20 20 73 71 6c 69 74  _VTAB;.    sqlit
1a3de 65 33 56 74 61 62 4c 6f 63 6b 28 28 56 54 61 62  e3VtabLock((VTab
1a3df 6c 65 20 2a 29 7a 50 34 29 3b 0a 20 20 20 20 61  le *)zP4);.    a
1a3e0 73 73 65 72 74 28 20 28 28 56 54 61 62 6c 65 20  ssert( ((VTable 
1a3e1 2a 29 7a 50 34 29 2d 3e 64 62 3d 3d 70 2d 3e 64  *)zP4)->db==p->d
1a3e2 62 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  b );.  }else if(
1a3e3 20 6e 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d   n<0 ){.    pOp-
1a3e4 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a  >p4.p = (void*)z
1a3e5 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74  P4;.    pOp->p4t
1a3e6 79 70 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68  ype = (signed ch
1a3e7 61 72 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ar)n;.  }else{. 
1a3e8 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20     if( n==0 ) n 
1a3e9 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1a3ea 30 28 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d  0(zP4);.    pOp-
1a3eb 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44  >p4.z = sqlite3D
1a3ec 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20  bStrNDup(p->db, 
1a3ed 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70  zP4, n);.    pOp
1a3ee 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59  ->p4type = P4_DY
1a3ef 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69  NAMIC;.  }.}..#i
1a3f0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a  fndef NDEBUG./*.
1a3f1 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 63 6f  ** Change the co
1a3f2 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 65  mment on the the
1a3f3 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63   most recently c
1a3f4 6f 64 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e  oded instruction
1a3f5 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20  .  Or.** insert 
1a3f6 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20  a No-op and add 
1a3f7 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74  the comment to t
1a3f8 68 61 74 20 6e 65 77 20 69 6e 73 74 72 75 63 74  hat new instruct
1a3f9 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61  ion.  This.** ma
1a3fa 6b 65 73 20 74 68 65 20 63 6f 64 65 20 65 61 73  kes the code eas
1a3fb 69 65 72 20 74 6f 20 72 65 61 64 20 64 75 72 69  ier to read duri
1a3fc 6e 67 20 64 65 62 75 67 67 69 6e 67 2e 20 20 4e  ng debugging.  N
1a3fd 6f 6e 65 20 6f 66 20 74 68 69 73 20 68 61 70 70  one of this happ
1a3fe 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64  ens.** in a prod
1a3ff 75 63 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f  uction build..*/
1a400 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a401 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1a402 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c  Comment(Vdbe *p,
1a403 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
1a404 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
1a405 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20  _list ap;.  if( 
1a406 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  !p ) return;.  a
1a407 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20  ssert( p->nOp>0 
1a408 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a  || p->aOp==0 );.
1a409 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70    assert( p->aOp
1a40a 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d  ==0 || p->aOp[p-
1a40b 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74  >nOp-1].zComment
1a40c 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61  ==0 || p->db->ma
1a40d 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
1a40e 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20  if( p->nOp ){.  
1a40f 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70    char **pz = &p
1a410 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e  ->aOp[p->nOp-1].
1a411 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61  zComment;.    va
1a412 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
1a413 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  at);.    sqlite3
1a414 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70  DbFree(p->db, *p
1a415 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71  z);.    *pz = sq
1a416 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d  lite3VMPrintf(p-
1a417 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  >db, zFormat, ap
1a418 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70  );.    va_end(ap
1a419 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f  );.  }.}.SQLITE_
1a41a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a41b 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ite3VdbeNoopComm
1a41c 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e  ent(Vdbe *p, con
1a41d 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
1a41e 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
1a41f 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 20 29  t ap;.  if( !p )
1a420 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
1a421 65 33 56 64 62 65 41 64 64 4f 70 30 28 70 2c 20  e3VdbeAddOp0(p, 
1a422 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 65  OP_Noop);.  asse
1a423 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20  rt( p->nOp>0 || 
1a424 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61  p->aOp==0 );.  a
1a425 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30  ssert( p->aOp==0
1a426 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f   || p->aOp[p->nO
1a427 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30  p-1].zComment==0
1a428 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f   || p->db->mallo
1a429 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28  cFailed );.  if(
1a42a 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63   p->nOp ){.    c
1a42b 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61  har **pz = &p->a
1a42c 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f  Op[p->nOp-1].zCo
1a42d 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74  mment;.    va_st
1a42e 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
1a42f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
1a430 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b  ree(p->db, *pz);
1a431 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74  .    *pz = sqlit
1a432 65 33 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62  e3VMPrintf(p->db
1a433 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
1a434 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a      va_end(ap);.
1a435 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a    }.}.#endif  /*
1a436 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a   NDEBUG */../*.*
1a437 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 70 63  * Return the opc
1a438 6f 64 65 20 66 6f 72 20 61 20 67 69 76 65 6e 20  ode for a given 
1a439 61 64 64 72 65 73 73 2e 20 20 49 66 20 74 68 65  address.  If the
1a43a 20 61 64 64 72 65 73 73 20 69 73 20 2d 31 2c 20   address is -1, 
1a43b 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 74  then.** return t
1a43c 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
1a43d 20 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65   inserted opcode
1a43e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d  ..**.** If a mem
1a43f 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
1a440 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
1a441 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63  d prior to the c
1a442 61 6c 6c 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a  alling of this.*
1a443 2a 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20  * routine, then 
1a444 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64  a pointer to a d
1a445 75 6d 6d 79 20 56 64 62 65 4f 70 20 77 69 6c 6c  ummy VdbeOp will
1a446 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54   be returned.  T
1a447 68 61 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73  hat opcode.** is
1a448 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72   readable and wr
1a449 69 74 61 62 6c 65 2c 20 62 75 74 20 69 74 20 68  itable, but it h
1a44a 61 73 20 6e 6f 20 65 66 66 65 63 74 2e 20 20 54  as no effect.  T
1a44b 68 65 20 72 65 74 75 72 6e 20 6f 66 20 61 20 64  he return of a d
1a44c 75 6d 6d 79 0a 2a 2a 20 6f 70 63 6f 64 65 20 61  ummy.** opcode a
1a44d 6c 6c 6f 77 73 20 74 68 65 20 63 61 6c 6c 20 74  llows the call t
1a44e 6f 20 63 6f 6e 74 69 6e 75 65 20 66 75 6e 63 74  o continue funct
1a44f 69 6f 6e 69 6e 67 20 61 66 74 65 72 20 61 20 4f  ioning after a O
1a450 4f 4d 20 66 61 75 6c 74 20 77 69 74 68 6f 75 74  OM fault without
1a451 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 63 68  .** having to ch
1a452 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
1a453 65 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68  e return from th
1a454 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
1a455 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a  valid pointer..*
1a456 2a 0a 2a 2a 20 41 62 6f 75 74 20 74 68 65 20 23  *.** About the #
1a457 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
1a458 54 5f 54 52 41 43 45 3a 20 20 4e 6f 72 6d 61 6c  T_TRACE:  Normal
1a459 6c 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ly, this routine
1a45a 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64   is never called
1a45b 0a 2a 2a 20 75 6e 6c 65 73 73 20 70 2d 3e 6e 4f  .** unless p->nO
1a45c 70 3e 30 2e 20 20 54 68 69 73 20 69 73 20 62 65  p>0.  This is be
1a45d 63 61 75 73 65 20 69 6e 20 74 68 65 20 61 62 73  cause in the abs
1a45e 65 6e 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f  ense of SQLITE_O
1a45f 4d 49 54 5f 54 52 41 43 45 2c 0a 2a 2a 20 61 6e  MIT_TRACE,.** an
1a460 20 4f 50 5f 54 72 61 63 65 20 69 6e 73 74 72 75   OP_Trace instru
1a461 63 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 20  ction is always 
1a462 69 6e 73 65 72 74 65 64 20 62 79 20 73 71 6c 69  inserted by sqli
1a463 74 65 33 56 64 62 65 47 65 74 28 29 20 61 73 20  te3VdbeGet() as 
1a464 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61 20 6e 65 77  soon as.** a new
1a465 20 56 44 42 45 20 69 73 20 63 72 65 61 74 65 64   VDBE is created
1a466 2e 20 20 53 6f 20 77 65 20 61 72 65 20 66 72 65  .  So we are fre
1a467 65 20 74 6f 20 73 65 74 20 61 64 64 72 20 74 6f  e to set addr to
1a468 20 70 2d 3e 6e 4f 70 2d 31 20 77 69 74 68 6f 75   p->nOp-1 withou
1a469 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 64  t.** having to d
1a46a 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d  ouble-check to m
1a46b 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
1a46c 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 6e 2d  e result is non-
1a46d 6e 65 67 61 74 69 76 65 2e 20 42 75 74 0a 2a 2a  negative. But.**
1a46e 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   if SQLITE_OMIT_
1a46f 54 52 41 43 45 20 69 73 20 64 65 66 69 6e 65 64  TRACE is defined
1a470 2c 20 74 68 65 20 4f 50 5f 54 72 61 63 65 20 69  , the OP_Trace i
1a471 73 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 77 65  s omitted and we
1a472 20 64 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 63   do need to.** c
1a473 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f  heck the value o
1a474 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 65 66 6f 72  f p->nOp-1 befor
1a475 65 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2f  e continuing..*/
1a476 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a477 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56  VdbeOp *sqlite3V
1a478 64 62 65 47 65 74 4f 70 28 56 64 62 65 20 2a 70  dbeGetOp(Vdbe *p
1a479 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73  , int addr){.  s
1a47a 74 61 74 69 63 20 56 64 62 65 4f 70 20 64 75 6d  tatic VdbeOp dum
1a47b 6d 79 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  my;.  assert( p-
1a47c 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
1a47d 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28  IC_INIT );.  if(
1a47e 20 61 64 64 72 3c 30 20 29 7b 0a 23 69 66 64 65   addr<0 ){.#ifde
1a47f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
1a480 41 43 45 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e  ACE.    if( p->n
1a481 4f 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 26  Op==0 ) return &
1a482 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 66 0a 20 20  dummy;.#endif.  
1a483 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20    addr = p->nOp 
1a484 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  - 1;.  }.  asser
1a485 74 28 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61  t( (addr>=0 && a
1a486 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70  ddr<p->nOp) || p
1a487 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1a488 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64  ed );.  if( p->d
1a489 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a48a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64  ){.    return &d
1a48b 75 6d 6d 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ummy;.  }else{. 
1a48c 20 20 20 72 65 74 75 72 6e 20 26 70 2d 3e 61 4f     return &p->aO
1a48d 70 5b 61 64 64 72 5d 3b 0a 20 20 7d 0a 7d 0a 0a  p[addr];.  }.}..
1a48e 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
1a48f 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
1a490 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44  ) || !defined(ND
1a491 45 42 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20  EBUG) \.     || 
1a492 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f  defined(VDBE_PRO
1a493 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  FILE) || defined
1a494 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f  (SQLITE_DEBUG)./
1a495 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73  *.** Compute a s
1a496 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63 72  tring that descr
1a497 69 62 65 73 20 74 68 65 20 50 34 20 70 61 72 61  ibes the P4 para
1a498 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 63  meter for an opc
1a499 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d  ode..** Use zTem
1a49a 70 20 66 6f 72 20 61 6e 79 20 72 65 71 75 69 72  p for any requir
1a49b 65 64 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66  ed temporary buf
1a49c 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74  fer space..*/.st
1a49d 61 74 69 63 20 63 68 61 72 20 2a 64 69 73 70 6c  atic char *displ
1a49e 61 79 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68  ayP4(Op *pOp, ch
1a49f 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e  ar *zTemp, int n
1a4a0 54 65 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a  Temp){.  char *z
1a4a1 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73  P4 = zTemp;.  as
1a4a2 73 65 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20  sert( nTemp>=20 
1a4a3 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70  );.  switch( pOp
1a4a4 2d 3e 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20  ->p4type ){.    
1a4a5 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  case P4_KEYINFO_
1a4a6 53 54 41 54 49 43 3a 0a 20 20 20 20 63 61 73 65  STATIC:.    case
1a4a7 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20   P4_KEYINFO: {. 
1a4a8 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20       int i, j;. 
1a4a9 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b       KeyInfo *pK
1a4aa 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34  eyInfo = pOp->p4
1a4ab 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20  .pKeyInfo;.     
1a4ac 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1a4ad 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
1a4ae 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b  "keyinfo(%d", pK
1a4af 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b  eyInfo->nField);
1a4b0 0a 20 20 20 20 20 20 69 20 3d 20 73 71 6c 69 74  .      i = sqlit
1a4b1 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70  e3Strlen30(zTemp
1a4b2 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  );.      for(j=0
1a4b3 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  ; j<pKeyInfo->nF
1a4b4 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  ield; j++){.    
1a4b5 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
1a4b6 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  ll = pKeyInfo->a
1a4b7 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20  Coll[j];.       
1a4b8 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20   if( pColl ){.  
1a4b9 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20          int n = 
1a4ba 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1a4bb 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pColl->zName);. 
1a4bc 20 20 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e           if( i+n
1a4bd 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20  >nTemp-6 ){.    
1a4be 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
1a4bf 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c  zTemp[i],",...",
1a4c0 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  4);.            
1a4c1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
1a4c2 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65   }.          zTe
1a4c3 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20  mp[i++] = ',';. 
1a4c4 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4b 65           if( pKe
1a4c5 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65  yInfo->aSortOrde
1a4c6 72 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  r && pKeyInfo->a
1a4c7 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a  SortOrder[j] ){.
1a4c8 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d              zTem
1a4c9 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20  p[i++] = '-';.  
1a4ca 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1a4cb 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d      memcpy(&zTem
1a4cc 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61  p[i], pColl->zNa
1a4cd 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20  me,n+1);.       
1a4ce 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20     i += n;.     
1a4cf 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34     }else if( i+4
1a4d0 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20  <nTemp-6 ){.    
1a4d1 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54        memcpy(&zT
1a4d2 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29  emp[i],",nil",4)
1a4d3 3b 0a 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d  ;.          i +=
1a4d4 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   4;.        }.  
1a4d5 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d      }.      zTem
1a4d6 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20  p[i++] = ')';.  
1a4d7 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30      zTemp[i] = 0
1a4d8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1a4d9 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20  i<nTemp );.     
1a4da 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1a4db 20 20 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45    case P4_COLLSE
1a4dc 51 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53  Q: {.      CollS
1a4dd 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d  eq *pColl = pOp-
1a4de 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  >p4.pColl;.     
1a4df 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1a4e0 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
1a4e1 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22  "collseq(%.20s)"
1a4e2 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b  , pColl->zName);
1a4e3 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1a4e4 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f    }.    case P4_
1a4e5 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20  FUNCDEF: {.     
1a4e6 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d   FuncDef *pDef =
1a4e7 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a   pOp->p4.pFunc;.
1a4e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1a4e9 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
1a4ea 65 6d 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70  emp, "%s(%d)", p
1a4eb 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66  Def->zName, pDef
1a4ec 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62  ->nArg);.      b
1a4ed 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1a4ee 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b  case P4_INT64: {
1a4ef 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1a4f0 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
1a4f1 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70  Temp, "%lld", *p
1a4f2 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20  Op->p4.pI64);.  
1a4f3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1a4f4 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54  .    case P4_INT
1a4f5 33 32 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  32: {.      sqli
1a4f6 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65  te3_snprintf(nTe
1a4f7 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c  mp, zTemp, "%d",
1a4f8 20 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20   pOp->p4.i);.   
1a4f9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1a4fa 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c      case P4_REAL
1a4fb 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
1a4fc 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
1a4fd 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22  , zTemp, "%.16g"
1a4fe 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c  , *pOp->p4.pReal
1a4ff 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1a500 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50      }.    case P
1a501 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d  4_MEM: {.      M
1a502 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e  em *pMem = pOp->
1a503 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61  p4.pMem;.      a
1a504 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
1a505 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d  ags & MEM_Null)=
1a506 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =0 );.      if( 
1a507 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
1a508 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20  M_Str ){.       
1a509 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a   zP4 = pMem->z;.
1a50a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1a50b 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
1a50c 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20  M_Int ){.       
1a50d 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1a50e 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
1a50f 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e  "%lld", pMem->u.
1a510 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  i);.      }else 
1a511 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
1a512 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20  & MEM_Real ){.  
1a513 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1a514 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
1a515 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d  emp, "%.16g", pM
1a516 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 65  em->r);.      }e
1a517 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
1a518 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
1a519 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20   & MEM_Blob );. 
1a51a 20 20 20 20 20 20 20 7a 50 34 20 3d 20 22 28 62         zP4 = "(b
1a51b 6c 6f 62 29 22 3b 0a 20 20 20 20 20 20 7d 0a 20  lob)";.      }. 
1a51c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a51d 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1a51e 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1a51f 4c 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56  LE.    case P4_V
1a520 54 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  TAB: {.      sql
1a521 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1a522 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62   = pOp->p4.pVtab
1a523 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 73  ->pVtab;.      s
1a524 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a525 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 76  nTemp, zTemp, "v
1a526 74 61 62 3a 25 70 3a 25 70 22 2c 20 70 56 74 61  tab:%p:%p", pVta
1a527 62 2c 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c  b, pVtab->pModul
1a528 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  e);.      break;
1a529 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1a52a 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52    case P4_INTARR
1a52b 41 59 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  AY: {.      sqli
1a52c 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65  te3_snprintf(nTe
1a52d 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74 61  mp, zTemp, "inta
1a52e 72 72 61 79 22 29 3b 0a 20 20 20 20 20 20 62 72  rray");.      br
1a52f 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1a530 61 73 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41  ase P4_SUBPROGRA
1a531 4d 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  M: {.      sqlit
1a532 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d  e3_snprintf(nTem
1a533 70 2c 20 7a 54 65 6d 70 2c 20 22 70 72 6f 67 72  p, zTemp, "progr
1a534 61 6d 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61  am");.      brea
1a535 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
1a536 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50  ault: {.      zP
1a537 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  4 = pOp->p4.z;. 
1a538 20 20 20 20 20 69 66 28 20 7a 50 34 3d 3d 30 20       if( zP4==0 
1a539 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d  ){.        zP4 =
1a53a 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20   zTemp;.        
1a53b 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20  zTemp[0] = 0;.  
1a53c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1a53d 20 20 61 73 73 65 72 74 28 20 7a 50 34 21 3d 30    assert( zP4!=0
1a53e 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 34   );.  return zP4
1a53f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1a540 2a 20 44 65 63 6c 61 72 65 20 74 6f 20 74 68 65  * Declare to the
1a541 20 56 64 62 65 20 74 68 61 74 20 74 68 65 20 42   Vdbe that the B
1a542 54 72 65 65 20 6f 62 6a 65 63 74 20 61 74 20 64  Tree object at d
1a543 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 65  b->aDb[i] is use
1a544 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1a545 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a546 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 56  3VdbeUsesBtree(V
1a547 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a  dbe *p, int i){.
1a548 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73    int mask;.  as
1a549 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1a54a 70 2d 3e 64 62 2d 3e 6e 44 62 20 26 26 20 69 3c  p->db->nDb && i<
1a54b 73 69 7a 65 6f 66 28 75 33 32 29 2a 38 20 29 3b  sizeof(u32)*8 );
1a54c 0a 20 20 61 73 73 65 72 74 28 20 69 3c 28 69 6e  .  assert( i<(in
1a54d 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 74 72 65  t)sizeof(p->btre
1a54e 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 20 6d 61  eMask)*8 );.  ma
1a54f 73 6b 20 3d 20 28 28 75 33 32 29 31 29 3c 3c 69  sk = ((u32)1)<<i
1a550 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 74 72 65  ;.  if( (p->btre
1a551 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30  eMask & mask)==0
1a552 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 72 65 65   ){.    p->btree
1a553 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20  Mask |= mask;.  
1a554 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75    sqlite3BtreeMu
1a555 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 26  texArrayInsert(&
1a556 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d 3e 64 62  p->aMutex, p->db
1a557 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a 20  ->aDb[i].pBt);. 
1a558 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69 6e   }.}...#if defin
1a559 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29  ed(VDBE_PROFILE)
1a55a 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
1a55b 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20  TE_DEBUG)./*.** 
1a55c 50 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f  Print a single o
1a55d 70 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f 75  pcode.  This rou
1a55e 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72  tine is used for
1a55f 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
1a560 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a561 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a562 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 20  dbePrintOp(FILE 
1a563 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20 4f  *pOut, int pc, O
1a564 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72 20  p *pOp){.  char 
1a565 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a 50 74  *zP4;.  char zPt
1a566 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69 63 20  r[50];.  static 
1a567 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
1a568 6d 61 74 31 20 3d 20 22 25 34 64 20 25 2d 31 33  mat1 = "%4d %-13
1a569 73 20 25 34 64 20 25 34 64 20 25 34 64 20 25 2d  s %4d %4d %4d %-
1a56a 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a 20  4s %.2X %s\n";. 
1a56b 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20 70   if( pOut==0 ) p
1a56c 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20  Out = stdout;.  
1a56d 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 50 34 28  zP4 = displayP4(
1a56e 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65 6f  pOp, zPtr, sizeo
1a56f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 70 72 69  f(zPtr));.  fpri
1a570 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d 61  ntf(pOut, zForma
1a571 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 20 20 73  t1, pc, .      s
1a572 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65  qlite3OpcodeName
1a573 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 70  (pOp->opcode), p
1a574 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c  Op->p1, pOp->p2,
1a575 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 70   pOp->p3, zP4, p
1a576 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20 53  Op->p5,.#ifdef S
1a577 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
1a578 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20    pOp->zComment 
1a579 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20  ? pOp->zComment 
1a57a 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20 20 20  : "".#else.     
1a57b 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a   "".#endif.  );.
1a57c 20 20 66 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a    fflush(pOut);.
1a57d 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1a57e 52 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61 79  Release an array
1a57f 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e   of N Mem elemen
1a580 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ts.*/.static voi
1a581 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61  d releaseMemArra
1a582 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29  y(Mem *p, int N)
1a583 7b 0a 20 20 69 66 28 20 70 20 26 26 20 4e 20 29  {.  if( p && N )
1a584 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b  {.    Mem *pEnd;
1a585 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
1a586 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 75 38   = p->db;.    u8
1a587 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d   malloc_failed =
1a588 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1a589 64 3b 0a 20 20 20 20 66 6f 72 28 70 45 6e 64 3d  d;.    for(pEnd=
1a58a 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70  &p[N]; p<pEnd; p
1a58b 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
1a58c 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64  t( (&p[1])==pEnd
1a58d 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31   || p[0].db==p[1
1a58e 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 20 20 2f  ].db );..      /
1a58f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69 73 20  * This block is 
1a590 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e 65  really an inline
1a591 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c  d version of sql
1a592 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1a593 73 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 74 68  se().      ** th
1a594 61 74 20 74 61 6b 65 73 20 61 64 76 61 6e 74 61  at takes advanta
1a595 67 65 20 6f 66 20 74 68 65 20 66 61 63 74 20 74  ge of the fact t
1a596 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 63  hat the memory c
1a597 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 0a 20 20  ell value is .  
1a598 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 73 65 74      ** being set
1a599 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 72 20 72   to NULL after r
1a59a 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 64 79 6e  eleasing any dyn
1a59b 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73 2e 0a  amic resources..
1a59c 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
1a59d 2a 20 54 68 65 20 6a 75 73 74 69 66 69 63 61 74  * The justificat
1a59e 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 61 74  ion for duplicat
1a59f 69 6e 67 20 63 6f 64 65 20 69 73 20 74 68 61 74  ing code is that
1a5a0 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 0a 20   according to . 
1a5a1 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 72 69 6e       ** callgrin
1a5a2 64 2c 20 74 68 69 73 20 63 61 75 73 65 73 20 61  d, this causes a
1a5a3 20 63 65 72 74 61 69 6e 20 74 65 73 74 20 63 61   certain test ca
1a5a4 73 65 20 74 6f 20 68 69 74 20 74 68 65 20 43 50  se to hit the CP
1a5a5 55 20 34 2e 37 20 0a 20 20 20 20 20 20 2a 2a 20  U 4.7 .      ** 
1a5a6 70 65 72 63 65 6e 74 20 6c 65 73 73 20 28 78 38  percent less (x8
1a5a7 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 76 65 72  6 linux, gcc ver
1a5a8 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 29  sion 4.1.2, -O6)
1a5a9 20 74 68 61 6e 20 69 66 20 0a 20 20 20 20 20 20   than if .      
1a5aa 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 6c  ** sqlite3MemRel
1a5ab 65 61 73 65 28 29 20 77 65 72 65 20 63 61 6c 6c  ease() were call
1a5ac 65 64 20 66 72 6f 6d 20 68 65 72 65 2e 20 57 69  ed from here. Wi
1a5ad 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 6a 75 6d  th -O2, this jum
1a5ae 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 36  ps.      ** to 6
1a5af 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 68 65 20  .6 percent. The 
1a5b0 74 65 73 74 20 63 61 73 65 20 69 73 20 69 6e 73  test case is ins
1a5b1 65 72 74 69 6e 67 20 31 30 30 30 20 72 6f 77 73  erting 1000 rows
1a5b2 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 0a 20   into a table . 
1a5b3 20 20 20 20 20 2a 2a 20 77 69 74 68 20 6e 6f 20       ** with no 
1a5b4 69 6e 64 65 78 65 73 20 75 73 69 6e 67 20 61 20  indexes using a 
1a5b5 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65 64 20  single prepared 
1a5b6 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
1a5b7 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 20 20 20  , bind() .      
1a5b8 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 29 2e 20  ** and reset(). 
1a5b9 49 6e 73 65 72 74 73 20 61 72 65 20 67 72 6f 75  Inserts are grou
1a5ba 70 65 64 20 69 6e 74 6f 20 61 20 74 72 61 6e 73  ped into a trans
1a5bb 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f  action..      */
1a5bc 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 66 6c  .      if( p->fl
1a5bd 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d  ags&(MEM_Agg|MEM
1a5be 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65 7c 4d  _Dyn|MEM_Frame|M
1a5bf 45 4d 5f 52 6f 77 53 65 74 29 20 29 7b 0a 20 20  EM_RowSet) ){.  
1a5c0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1a5c1 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a  eMemRelease(p);.
1a5c2 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1a5c3 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20  p->zMalloc ){.  
1a5c4 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1a5c5 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c  ree(db, p->zMall
1a5c6 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  oc);.        p->
1a5c7 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20  zMalloc = 0;.   
1a5c8 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 2d 3e 66     }..      p->f
1a5c9 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
1a5ca 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d  .    }.    db->m
1a5cb 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61  allocFailed = ma
1a5cc 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 7d  lloc_failed;.  }
1a5cd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
1a5ce 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a   a VdbeFrame obj
1a5cf 65 63 74 20 61 6e 64 20 69 74 73 20 63 6f 6e 74  ect and its cont
1a5d0 65 6e 74 73 2e 20 56 64 62 65 46 72 61 6d 65 20  ents. VdbeFrame 
1a5d1 6f 62 6a 65 63 74 73 20 61 72 65 0a 2a 2a 20 61  objects are.** a
1a5d2 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20  llocated by the 
1a5d3 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70 63 6f 64  OP_Program opcod
1a5d4 65 20 69 6e 20 73 71 6c 69 74 65 33 56 64 62 65  e in sqlite3Vdbe
1a5d5 45 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  Exec()..*/.SQLIT
1a5d6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1a5d7 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 44  qlite3VdbeFrameD
1a5d8 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 20  elete(VdbeFrame 
1a5d9 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  *p){.  int i;.  
1a5da 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20 56 64 62 65  Mem *aMem = Vdbe
1a5db 46 72 61 6d 65 4d 65 6d 28 70 29 3b 0a 20 20 56  FrameMem(p);.  V
1a5dc 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 73  dbeCursor **apCs
1a5dd 72 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 20  r = (VdbeCursor 
1a5de 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e 6e 43 68 69  **)&aMem[p->nChi
1a5df 6c 64 4d 65 6d 5d 3b 0a 20 20 66 6f 72 28 69 3d  ldMem];.  for(i=
1a5e0 30 3b 20 69 3c 70 2d 3e 6e 43 68 69 6c 64 43 73  0; i<p->nChildCs
1a5e1 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  r; i++){.    sql
1a5e2 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73  ite3VdbeFreeCurs
1a5e3 6f 72 28 70 2d 3e 76 2c 20 61 70 43 73 72 5b 69  or(p->v, apCsr[i
1a5e4 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73  ]);.  }.  releas
1a5e5 65 4d 65 6d 41 72 72 61 79 28 61 4d 65 6d 2c 20  eMemArray(aMem, 
1a5e6 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d 29 3b 0a 20  p->nChildMem);. 
1a5e7 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
1a5e8 2d 3e 76 2d 3e 64 62 2c 20 70 29 3b 0a 7d 0a 0a  ->v->db, p);.}..
1a5e9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a5ea 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a 2a  MIT_EXPLAIN./*.*
1a5eb 2a 20 47 69 76 65 20 61 20 6c 69 73 74 69 6e 67  * Give a listing
1a5ec 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20   of the program 
1a5ed 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  in the virtual m
1a5ee 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  achine..**.** Th
1a5ef 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74  e interface is t
1a5f0 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74  he same as sqlit
1a5f1 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 42  e3VdbeExec().  B
1a5f2 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a  ut instead of.**
1a5f3 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 63 6f 64   running the cod
1a5f4 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73 20 74 68  e, it invokes th
1a5f5 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20  e callback once 
1a5f6 66 6f 72 20 65 61 63 68 20 69 6e 73 74 72 75 63  for each instruc
1a5f7 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 65  tion..** This fe
1a5f8 61 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  ature is used to
1a5f9 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45 58 50 4c   implement "EXPL
1a5fa 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  AIN"..**.** When
1a5fb 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 2c 20   p->explain==1, 
1a5fc 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e  each instruction
1a5fd 20 69 73 20 6c 69 73 74 65 64 2e 20 20 57 68 65   is listed.  Whe
1a5fe 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c 61 69 6e 3d  n.** p->explain=
1a5ff 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f 45 78 70 6c  =2, only OP_Expl
1a600 61 69 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ain instructions
1a601 20 61 72 65 20 6c 69 73 74 65 64 20 61 6e 64 20   are listed and 
1a602 74 68 65 73 65 0a 2a 2a 20 61 72 65 20 73 68 6f  these.** are sho
1a603 77 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e  wn in a differen
1a604 74 20 66 6f 72 6d 61 74 2e 20 20 70 2d 3e 65 78  t format.  p->ex
1a605 70 6c 61 69 6e 3d 3d 32 20 69 73 20 75 73 65 64  plain==2 is used
1a606 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a   to implement.**
1a607 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50   EXPLAIN QUERY P
1a608 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  LAN..*/.SQLITE_P
1a609 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1a60a 65 33 56 64 62 65 4c 69 73 74 28 0a 20 20 56 64  e3VdbeList(.  Vd
1a60b 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20  be *p           
1a60c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
1a60d 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  DBE */.){.  int 
1a60e 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20  nRow;           
1a60f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a610 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
1a611 20 6f 66 20 72 6f 77 73 20 74 6f 20 72 65 74 75   of rows to retu
1a612 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62  rn */.  int nSub
1a613 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1a614 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a615 4e 75 6d 62 65 72 20 6f 66 20 73 75 62 2d 76 64  Number of sub-vd
1a616 62 65 73 20 73 65 65 6e 20 73 6f 20 66 61 72 20  bes seen so far 
1a617 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20  */.  SubProgram 
1a618 2a 2a 61 70 53 75 62 20 3d 20 30 3b 20 20 20 20  **apSub = 0;    
1a619 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72            /* Arr
1a61a 61 79 20 6f 66 20 73 75 62 2d 76 64 62 65 73 20  ay of sub-vdbes 
1a61b 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 75 62 20 3d  */.  Mem *pSub =
1a61c 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   0;.  sqlite3 *d
1a61d 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74  b = p->db;.  int
1a61e 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   i;.  int rc = S
1a61f 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20  QLITE_OK;.  Mem 
1a620 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75  *pMem = p->pResu
1a621 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d  ltSet = &p->aMem
1a622 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  [1];..  assert( 
1a623 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20  p->explain );.  
1a624 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
1a625 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  ==VDBE_MAGIC_RUN
1a626 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62   );.  assert( db
1a627 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f  ->magic==SQLITE_
1a628 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20  MAGIC_BUSY );.  
1a629 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53  assert( p->rc==S
1a62a 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72  QLITE_OK || p->r
1a62b 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c  c==SQLITE_BUSY |
1a62c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  | p->rc==SQLITE_
1a62d 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20 45  NOMEM );..  /* E
1a62e 76 65 6e 20 74 68 6f 75 67 68 20 74 68 69 73 20  ven though this 
1a62f 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20  opcode does not 
1a630 75 73 65 20 64 79 6e 61 6d 69 63 20 73 74 72 69  use dynamic stri
1a631 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65  ngs for.  ** the
1a632 20 72 65 73 75 6c 74 2c 20 72 65 73 75 6c 74 20   result, result 
1a633 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63 6f  columns may beco
1a634 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 20 74 68  me dynamic if th
1a635 65 20 75 73 65 72 20 63 61 6c 6c 73 0a 20 20 2a  e user calls.  *
1a636 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
1a637 5f 74 65 78 74 31 36 28 29 2c 20 63 61 75 73 69  _text16(), causi
1a638 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e  ng a translation
1a639 20 74 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f 64   to UTF-16 encod
1a63a 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c 65  ing..  */.  rele
1a63b 61 73 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 6d  aseMemArray(pMem
1a63c 2c 20 38 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e  , 8);..  if( p->
1a63d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
1a63e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
1a63f 68 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c  happens if a mal
1a640 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 63  loc() inside a c
1a641 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  all to sqlite3_c
1a642 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a  olumn_text() or.
1a643 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63      ** sqlite3_c
1a644 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66  olumn_text16() f
1a645 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 64  ailed.  */.    d
1a646 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a647 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
1a648 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1a649 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  }..  /* Figure o
1a64a 75 74 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  ut total number 
1a64b 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c  of rows that wil
1a64c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79  l be returned by
1a64d 20 74 68 69 73 20 0a 20 20 2a 2a 20 45 58 50 4c   this .  ** EXPL
1a64e 41 49 4e 20 70 72 6f 67 72 61 6d 2e 20 20 2a 2f  AIN program.  */
1a64f 0a 20 20 6e 52 6f 77 20 3d 20 70 2d 3e 6e 4f 70  .  nRow = p->nOp
1a650 3b 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61  ;.  if( p->expla
1a651 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 70 53 75  in==1 ){.    pSu
1a652 62 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 39 5d 3b  b = &p->aMem[9];
1a653 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e 66  .    if( pSub->f
1a654 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b  lags&MEM_Blob ){
1a655 0a 20 20 20 20 20 20 6e 53 75 62 20 3d 20 70 53  .      nSub = pS
1a656 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 28 56 64 62  ub->n/sizeof(Vdb
1a657 65 2a 29 3b 0a 20 20 20 20 20 20 61 70 53 75 62  e*);.      apSub
1a658 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a   = (SubProgram *
1a659 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 7d  *)pSub->z;.    }
1a65a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1a65b 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nSub; i++){.    
1a65c 20 20 6e 52 6f 77 20 2b 3d 20 61 70 53 75 62 5b    nRow += apSub[
1a65d 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a 20  i]->nOp;.    }. 
1a65e 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20   }..  do{.    i 
1a65f 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68  = p->pc++;.  }wh
1a660 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26 26 20 70  ile( i<nRow && p
1a661 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 20  ->explain==2 && 
1a662 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65  p->aOp[i].opcode
1a663 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b 0a  !=OP_Explain );.
1a664 20 20 69 66 28 20 69 3e 3d 6e 52 6f 77 20 29 7b    if( i>=nRow ){
1a665 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  .    p->rc = SQL
1a666 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d  ITE_OK;.    rc =
1a667 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
1a668 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 75 31  }else if( db->u1
1a669 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29  .isInterrupted )
1a66a 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  {.    p->rc = SQ
1a66b 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a  LITE_INTERRUPT;.
1a66c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1a66d 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74  ERROR;.    sqlit
1a66e 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1a66f 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
1a670 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  ", sqlite3ErrStr
1a671 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c 73  (p->rc));.  }els
1a672 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a  e{.    char *z;.
1a673 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20      Op *pOp;.   
1a674 20 69 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29 7b   if( i<p->nOp ){
1a675 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26 70 2d  .      pOp = &p-
1a676 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c  >aOp[i];.    }el
1a677 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b  se{.      int j;
1a678 0a 20 20 20 20 20 20 69 20 2d 3d 20 70 2d 3e 6e  .      i -= p->n
1a679 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  Op;.      for(j=
1a67a 30 3b 20 69 3e 3d 61 70 53 75 62 5b 6a 5d 2d 3e  0; i>=apSub[j]->
1a67b 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  nOp; j++){.     
1a67c 20 20 20 69 20 2d 3d 20 61 70 53 75 62 5b 6a 5d     i -= apSub[j]
1a67d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 7d 0a 20  ->nOp;.      }. 
1a67e 20 20 20 20 20 70 4f 70 20 3d 20 26 61 70 53 75       pOp = &apSu
1a67f 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20  b[j]->aOp[i];.  
1a680 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 65    }.    if( p->e
1a681 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20  xplain==1 ){.   
1a682 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1a683 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
1a684 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1a685 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  ITE_INTEGER;.   
1a686 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69     pMem->u.i = i
1a687 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1a688 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a689 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e   /* Program coun
1a68a 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65  ter */.      pMe
1a68b 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d  m++;.  .      pM
1a68c 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1a68d 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d  Static|MEM_Str|M
1a68e 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70  EM_Term;.      p
1a68f 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29  Mem->z = (char*)
1a690 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d  sqlite3OpcodeNam
1a691 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20  e(pOp->opcode); 
1a692 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20   /* Opcode */.  
1a693 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
1a694 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  ->z!=0 );.      
1a695 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65  pMem->n = sqlite
1a696 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e  3Strlen30(pMem->
1a697 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  z);.      pMem->
1a698 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45  type = SQLITE_TE
1a699 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  XT;.      pMem->
1a69a 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
1a69b 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b  8;.      pMem++;
1a69c 0a 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d  ..      if( pOp-
1a69d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50  >p4type==P4_SUBP
1a69e 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 20  ROGRAM ){.      
1a69f 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 6e    int nByte = (n
1a6a0 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75  Sub+1)*sizeof(Su
1a6a1 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20  bProgram*);.    
1a6a2 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20      int j;.     
1a6a3 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 53     for(j=0; j<nS
1a6a4 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ub; j++){.      
1a6a5 20 20 20 20 69 66 28 20 61 70 53 75 62 5b 6a 5d      if( apSub[j]
1a6a6 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72  ==pOp->p4.pProgr
1a6a7 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  am ) break;.    
1a6a8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
1a6a9 28 20 6a 3d 3d 6e 53 75 62 20 26 26 20 53 51 4c  ( j==nSub && SQL
1a6aa 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56  ITE_OK==sqlite3V
1a6ab 64 62 65 4d 65 6d 47 72 6f 77 28 70 53 75 62 2c  dbeMemGrow(pSub,
1a6ac 20 6e 42 79 74 65 2c 20 31 29 20 29 7b 0a 20 20   nByte, 1) ){.  
1a6ad 20 20 20 20 20 20 20 20 61 70 53 75 62 20 3d 20          apSub = 
1a6ae 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 29 70  (SubProgram **)p
1a6af 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 20 20 20 20  Sub->z;.        
1a6b0 20 20 61 70 53 75 62 5b 6e 53 75 62 2b 2b 5d 20    apSub[nSub++] 
1a6b1 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72  = pOp->p4.pProgr
1a6b2 61 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53  am;.          pS
1a6b3 75 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  ub->flags |= MEM
1a6b4 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 20 20 20 20  _Blob;.         
1a6b5 20 70 53 75 62 2d 3e 6e 20 3d 20 6e 53 75 62 2a   pSub->n = nSub*
1a6b6 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61  sizeof(SubProgra
1a6b7 6d 2a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  m*);.        }. 
1a6b8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
1a6b9 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1a6ba 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65  MEM_Int;.    pMe
1a6bb 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31  m->u.i = pOp->p1
1a6bc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1a6bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31             /* P1
1a6be 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79   */.    pMem->ty
1a6bf 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  pe = SQLITE_INTE
1a6c0 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b  GER;.    pMem++;
1a6c1 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ..    pMem->flag
1a6c2 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1a6c3 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70   pMem->u.i = pOp
1a6c4 2d 3e 70 32 3b 20 20 20 20 20 20 20 20 20 20 20  ->p2;           
1a6c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a6c6 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65 6d  * P2 */.    pMem
1a6c7 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
1a6c8 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65  INTEGER;.    pMe
1a6c9 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d  m++;..    if( p-
1a6ca 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20  >explain==1 ){. 
1a6cb 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1a6cc 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1a6cd 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f    pMem->u.i = pO
1a6ce 70 2d 3e 70 33 3b 20 20 20 20 20 20 20 20 20 20  p->p3;          
1a6cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6d0 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 20 70  /* P3 */.      p
1a6d1 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1a6d2 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  TE_INTEGER;.    
1a6d3 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a    pMem++;.    }.
1a6d4 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1a6d5 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
1a6d6 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20 20  , 32, 0) ){     
1a6d7 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a         /* P4 */.
1a6d8 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
1a6d9 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1a6da 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  d );.      retur
1a6db 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1a6dc 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e      }.    pMem->
1a6dd 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c  flags = MEM_Dyn|
1a6de 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d  MEM_Str|MEM_Term
1a6df 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c 61  ;.    z = displa
1a6e0 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e 7a  yP4(pOp, pMem->z
1a6e1 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20 7a  , 32);.    if( z
1a6e2 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20  !=pMem->z ){.   
1a6e3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1a6e4 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a 2c  mSetStr(pMem, z,
1a6e5 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38   -1, SQLITE_UTF8
1a6e6 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 0);.    }else{
1a6e7 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1a6e8 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20  Mem->z!=0 );.   
1a6e9 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c     pMem->n = sql
1a6ea 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65  ite3Strlen30(pMe
1a6eb 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65  m->z);.      pMe
1a6ec 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  m->enc = SQLITE_
1a6ed 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20  UTF8;.    }.    
1a6ee 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1a6ef 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d  ITE_TEXT;.    pM
1a6f0 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70  em++;..    if( p
1a6f1 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a  ->explain==1 ){.
1a6f2 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1a6f3 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
1a6f4 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20  m, 4, 0) ){.    
1a6f5 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64      assert( p->d
1a6f6 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a6f7 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1a6f8 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1a6f9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4d        }.      pM
1a6fa 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1a6fb 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  Dyn|MEM_Str|MEM_
1a6fc 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d  Term;.      pMem
1a6fd 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20 73  ->n = 2;.      s
1a6fe 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a6ff 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e 32  3, pMem->z, "%.2
1a700 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20 20  x", pOp->p5);   
1a701 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 20 20 70  /* P5 */.      p
1a702 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1a703 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70  TE_TEXT;.      p
1a704 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
1a705 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d  E_UTF8;.      pM
1a706 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66 20  em++;.  .#ifdef 
1a707 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
1a708 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f 6d     if( pOp->zCom
1a709 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  ment ){.        
1a70a 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1a70b 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
1a70c 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20          pMem->z 
1a70d 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b  = pOp->zComment;
1a70e 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e  .        pMem->n
1a70f 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
1a710 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20  30(pMem->z);.   
1a711 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d       pMem->enc =
1a712 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20   SQLITE_UTF8;.  
1a713 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65        pMem->type
1a714 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a   = SQLITE_TEXT;.
1a715 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64        }else.#end
1a716 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  if.      {.     
1a717 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1a718 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 20   MEM_Null;      
1a719 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a71a 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20   /* Comment */. 
1a71b 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70         pMem->typ
1a71c 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b  e = SQLITE_NULL;
1a71d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1a71e 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d      p->nResColum
1a71f 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65 78  n = 8 - 5*(p->ex
1a720 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 2d  plain-1);.    p-
1a721 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
1a722 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1a723 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74 75  _ROW;.  }.  retu
1a724 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20  rn rc;.}.#endif 
1a725 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  /* SQLITE_OMIT_E
1a726 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 65  XPLAIN */..#ifde
1a727 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f  f SQLITE_DEBUG./
1a728 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 53  *.** Print the S
1a729 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65 64  QL that was used
1a72a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 56   to generate a V
1a72b 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a  DBE program..*/.
1a72c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a72d 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50  oid sqlite3VdbeP
1a72e 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70 29  rintSql(Vdbe *p)
1a72f 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d  {.  int nOp = p-
1a730 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a  >nOp;.  VdbeOp *
1a731 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31  pOp;.  if( nOp<1
1a732 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70   ) return;.  pOp
1a733 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20   = &p->aOp[0];. 
1a734 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1a735 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f  ==OP_Trace && pO
1a736 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20  p->p4.z!=0 ){.  
1a737 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
1a738 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20  = pOp->p4.z;.   
1a739 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
1a73a 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b  sspace(*z) ) z++
1a73b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 51  ;.    printf("SQ
1a73c 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b 0a  L: [%s]\n", z);.
1a73d 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69    }.}.#endif..#i
1a73e 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
1a73f 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26  E_OMIT_TRACE) &&
1a740 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
1a741 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a  ENABLE_IOTRACE).
1a742 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 49  /*.** Print an I
1a743 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20 73  OTRACE message s
1a744 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 65  howing SQL conte
1a745 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
1a746 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a747 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c  e3VdbeIOTraceSql
1a748 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74  (Vdbe *p){.  int
1a749 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20   nOp = p->nOp;. 
1a74a 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20   VdbeOp *pOp;.  
1a74b 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61  if( sqlite3IoTra
1a74c 63 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ce==0 ) return;.
1a74d 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65    if( nOp<1 ) re
1a74e 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70  turn;.  pOp = &p
1a74f 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20  ->aOp[0];.  if( 
1a750 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
1a751 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34  Trace && pOp->p4
1a752 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  .z!=0 ){.    int
1a753 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 20   i, j;.    char 
1a754 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 6c  z[1000];.    sql
1a755 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1a756 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 22  zeof(z), z, "%s"
1a757 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20  , pOp->p4.z);.  
1a758 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69 74    for(i=0; sqlit
1a759 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b  e3Isspace(z[i]);
1a75a 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 28   i++){}.    for(
1a75b 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b  j=0; z[i]; i++){
1a75c 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1a75d 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 20  e3Isspace(z[i]) 
1a75e 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  ){.        if( z
1a75f 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 20  [i-1]!=' ' ){.  
1a760 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
1a761 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a   ' ';.        }.
1a762 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1a763 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b       z[j++] = z[
1a764 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  i];.      }.    
1a765 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a  }.    z[j] = 0;.
1a766 20 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72 61      sqlite3IoTra
1a767 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 7a  ce("SQL %s\n", z
1a768 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  );.  }.}.#endif 
1a769 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  /* !SQLITE_OMIT_
1a76a 54 52 41 43 45 20 26 26 20 53 51 4c 49 54 45 5f  TRACE && SQLITE_
1a76b 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 2a  ENABLE_IOTRACE *
1a76c 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  /../*.** Allocat
1a76d 65 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20 66  e space from a f
1a76e 69 78 65 64 20 73 69 7a 65 20 62 75 66 66 65 72  ixed size buffer
1a76f 2e 20 20 4d 61 6b 65 20 2a 70 70 20 70 6f 69 6e  .  Make *pp poin
1a770 74 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f  t to the.** allo
1a771 63 61 74 65 64 20 73 70 61 63 65 2e 20 20 28 4e  cated space.  (N
1a772 6f 74 65 3a 20 20 70 70 20 69 73 20 61 20 63 68  ote:  pp is a ch
1a773 61 72 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20  ar* rather than 
1a774 61 20 76 6f 69 64 2a 2a 20 74 6f 0a 2a 2a 20 77  a void** to.** w
1a775 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70  ork around the p
1a776 6f 69 6e 74 65 72 20 61 6c 69 61 73 69 6e 67 20  ointer aliasing 
1a777 72 75 6c 65 73 20 6f 66 20 43 2e 29 20 20 2a 70  rules of C.)  *p
1a778 70 20 73 68 6f 75 6c 64 20 69 6e 69 74 69 61 6c  p should initial
1a779 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 6f 2e 20 20  ly.** be zero.  
1a77a 49 66 20 2a 70 70 20 69 73 20 6e 6f 74 20 7a 65  If *pp is not ze
1a77b 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  ro, that means t
1a77c 68 61 74 20 74 68 65 20 73 70 61 63 65 20 68 61  hat the space ha
1a77d 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65  s already.** bee
1a77e 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  n allocated and 
1a77f 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
1a780 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 6e 42  a noop..**.** nB
1a781 79 74 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65  yte is the numbe
1a782 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70  r of bytes of sp
1a783 61 63 65 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a  ace needed..**.*
1a784 2a 20 2a 70 70 46 72 6f 6d 20 70 6f 69 6e 74 20  * *ppFrom point 
1a785 74 6f 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61  to available spa
1a786 63 65 20 61 6e 64 20 70 45 6e 64 20 70 6f 69 6e  ce and pEnd poin
1a787 74 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  ts to the end of
1a788 20 74 68 65 0a 2a 2a 20 61 76 61 69 6c 61 62 6c   the.** availabl
1a789 65 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 2a  e space..**.** *
1a78a 70 6e 42 79 74 65 20 69 73 20 61 20 63 6f 75 6e  pnByte is a coun
1a78b 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ter of the numbe
1a78c 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70  r of bytes of sp
1a78d 61 63 65 20 74 68 61 74 20 68 61 76 65 20 66 61  ace that have fa
1a78e 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c 6c 6f 63  iled.** to alloc
1a78f 61 74 65 2e 20 20 49 66 20 74 68 65 72 65 20 69  ate.  If there i
1a790 73 20 69 6e 73 75 66 66 69 63 69 65 6e 74 20 73  s insufficient s
1a791 70 61 63 65 20 69 6e 20 2a 70 70 46 72 6f 6d 20  pace in *ppFrom 
1a792 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a 2a  to satisfy the.*
1a793 2a 20 72 65 71 75 65 73 74 2c 20 74 68 65 6e 20  * request, then 
1a794 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74  increment *pnByt
1a795 65 20 62 79 20 74 68 65 20 61 6d 6f 75 6e 74 20  e by the amount 
1a796 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 2e 0a  of the request..
1a797 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
1a798 6c 6c 6f 63 53 70 61 63 65 28 0a 20 20 63 68 61  llocSpace(.  cha
1a799 72 20 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20  r *pp,          
1a79a 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65 74    /* IN/OUT: Set
1a79b 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 6f   *pp to point to
1a79c 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65   allocated buffe
1a79d 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  r */.  int nByte
1a79e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
1a79f 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
1a7a0 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20  o allocate */.  
1a7a1 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20 20  u8 **ppFrom,    
1a7a2 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
1a7a3 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a 70  Allocate from *p
1a7a4 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 38 20 2a 70  pFrom */.  u8 *p
1a7a5 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  End,            
1a7a6 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 31 20  /* Pointer to 1 
1a7a7 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e  byte past the en
1a7a8 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75 66  d of *ppFrom buf
1a7a9 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  fer */.  int *pn
1a7aa 42 79 74 65 20 20 20 20 20 20 20 20 20 20 2f 2a  Byte          /*
1a7ab 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63   If allocation c
1a7ac 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65 2c 20 69  annot be made, i
1a7ad 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 65  ncrement *pnByte
1a7ae 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28   */.){.  assert(
1a7af 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
1a7b0 4e 4d 45 4e 54 28 2a 70 70 46 72 6f 6d 29 20 29  NMENT(*ppFrom) )
1a7b1 3b 0a 20 20 69 66 28 20 28 2a 28 76 6f 69 64 2a  ;.  if( (*(void*
1a7b2 2a 29 70 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20  *)pp)==0 ){.    
1a7b3 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e  nByte = ROUND8(n
1a7b4 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 26  Byte);.    if( &
1a7b5 28 2a 70 70 46 72 6f 6d 29 5b 6e 42 79 74 65 5d  (*ppFrom)[nByte]
1a7b6 20 3c 3d 20 70 45 6e 64 20 29 7b 0a 20 20 20 20   <= pEnd ){.    
1a7b7 20 20 2a 28 76 6f 69 64 2a 2a 29 70 70 20 3d 20    *(void**)pp = 
1a7b8 28 76 6f 69 64 20 2a 29 2a 70 70 46 72 6f 6d 3b  (void *)*ppFrom;
1a7b9 0a 20 20 20 20 20 20 2a 70 70 46 72 6f 6d 20 2b  .      *ppFrom +
1a7ba 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 65 6c  = nByte;.    }el
1a7bb 73 65 7b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74  se{.      *pnByt
1a7bc 65 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20  e += nByte;.    
1a7bd 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  }.  }.}../*.** P
1a7be 72 65 70 61 72 65 20 61 20 76 69 72 74 75 61 6c  repare a virtual
1a7bf 20 6d 61 63 68 69 6e 65 20 66 6f 72 20 65 78 65   machine for exe
1a7c0 63 75 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e  cution.  This in
1a7c1 76 6f 6c 76 65 73 20 74 68 69 6e 67 73 20 73 75  volves things su
1a7c2 63 68 0a 2a 2a 20 61 73 20 61 6c 6c 6f 63 61 74  ch.** as allocat
1a7c3 69 6e 67 20 73 74 61 63 6b 20 73 70 61 63 65 20  ing stack space 
1a7c4 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  and initializing
1a7c5 20 74 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75   the program cou
1a7c6 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 65 72 20 74  nter..** After t
1a7c7 68 65 20 56 44 42 45 20 68 61 73 20 62 65 20 70  he VDBE has be p
1a7c8 72 65 70 70 65 64 2c 20 69 74 20 63 61 6e 20 62  repped, it can b
1a7c9 65 20 65 78 65 63 75 74 65 64 20 62 79 20 6f 6e  e executed by on
1a7ca 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 63 61 6c  e or more.** cal
1a7cb 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62  ls to sqlite3Vdb
1a7cc 65 45 78 65 63 28 29 2e 20 20 0a 2a 2a 0a 2a 2a  eExec().  .**.**
1a7cd 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c   This is the onl
1a7ce 79 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 61 20  y way to move a 
1a7cf 56 44 42 45 20 66 72 6f 6d 20 56 44 42 45 5f 4d  VDBE from VDBE_M
1a7d0 41 47 49 43 5f 49 4e 49 54 20 74 6f 0a 2a 2a 20  AGIC_INIT to.** 
1a7d1 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 2e 0a  VDBE_MAGIC_RUN..
1a7d2 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
1a7d3 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  ion may be calle
1a7d4 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  d more than once
1a7d5 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 76 69 72   on a single vir
1a7d6 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a  tual machine..**
1a7d7 20 54 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   The first call 
1a7d8 69 73 20 6d 61 64 65 20 77 68 69 6c 65 20 63 6f  is made while co
1a7d9 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 51 4c 20  mpiling the SQL 
1a7da 73 74 61 74 65 6d 65 6e 74 2e 20 53 75 62 73 65  statement. Subse
1a7db 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 61  quent.** calls a
1a7dc 72 65 20 6d 61 64 65 20 61 73 20 70 61 72 74 20  re made as part 
1a7dd 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f  of the process o
1a7de 66 20 72 65 73 65 74 74 69 6e 67 20 61 20 73 74  f resetting a st
1a7df 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a  atement to be.**
1a7e0 20 72 65 2d 65 78 65 63 75 74 65 64 20 28 66 72   re-executed (fr
1a7e1 6f 6d 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  om a call to sql
1a7e2 69 74 65 33 5f 72 65 73 65 74 28 29 29 2e 20 54  ite3_reset()). T
1a7e3 68 65 20 6e 56 61 72 2c 20 6e 4d 65 6d 2c 20 6e  he nVar, nMem, n
1a7e4 43 75 72 73 6f 72 20 0a 2a 2a 20 61 6e 64 20 69  Cursor .** and i
1a7e5 73 45 78 70 6c 61 69 6e 20 70 61 72 61 6d 65 74  sExplain paramet
1a7e6 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 70 61 73  ers are only pas
1a7e7 73 65 64 20 63 6f 72 72 65 63 74 20 76 61 6c 75  sed correct valu
1a7e8 65 73 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  es the first tim
1a7e9 65 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f  e.** the functio
1a7ea 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 4f 6e 20  n is called. On 
1a7eb 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1a7ec 2c 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72  , from sqlite3_r
1a7ed 65 73 65 74 28 29 2c 20 6e 56 61 72 0a 2a 2a 20  eset(), nVar.** 
1a7ee 69 73 20 70 61 73 73 65 64 20 2d 31 20 61 6e 64  is passed -1 and
1a7ef 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 61   nMem, nCursor a
1a7f0 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 61 72 65  nd isExplain are
1a7f1 20 61 6c 6c 20 70 61 73 73 65 64 20 7a 65 72 6f   all passed zero
1a7f2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a7f3 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1a7f4 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 0a 20  VdbeMakeReady(. 
1a7f5 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20   Vdbe *p,       
1a7f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a7f7 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 20  /* The VDBE */. 
1a7f8 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 20 20 20   int nVar,      
1a7f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a7fa 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27  /* Number of '?'
1a7fb 20 73 65 65 20 69 6e 20 74 68 65 20 53 51 4c 20   see in the SQL 
1a7fc 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69  statement */.  i
1a7fd 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 20 20 20  nt nMem,        
1a7fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a7ff 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72   Number of memor
1a800 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c 6c 6f 63  y cells to alloc
1a801 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75  ate */.  int nCu
1a802 72 73 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20  rsor,           
1a803 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1a804 72 20 6f 66 20 63 75 72 73 6f 72 73 20 74 6f 20  r of cursors to 
1a805 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e  allocate */.  in
1a806 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20  t nArg,         
1a807 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a808 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  Maximum number o
1a809 66 20 61 72 67 73 20 69 6e 20 53 75 62 50 72 6f  f args in SubPro
1a80a 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 69  grams */.  int i
1a80b 73 45 78 70 6c 61 69 6e 2c 20 20 20 20 20 20 20  sExplain,       
1a80c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1a80d 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e  e if the EXPLAIN
1a80e 20 6b 65 79 77 6f 72 64 73 20 69 73 20 70 72 65   keywords is pre
1a80f 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 75 73  sent */.  int us
1a810 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 20 20  esStmtJournal   
1a811 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
1a812 20 74 6f 20 73 65 74 20 56 64 62 65 2e 75 73 65   to set Vdbe.use
1a813 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 2a 2f 0a  sStmtJournal */.
1a814 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71  ){.  int n;.  sq
1a815 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
1a816 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21  b;..  assert( p!
1a817 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1a818 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
1a819 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20  AGIC_INIT );..  
1a81a 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20  /* There should 
1a81b 62 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  be at least one 
1a81c 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61  opcode..  */.  a
1a81d 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20  ssert( p->nOp>0 
1a81e 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65  );..  /* Set the
1a81f 20 6d 61 67 69 63 20 74 6f 20 56 44 42 45 5f 4d   magic to VDBE_M
1a820 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20  AGIC_RUN sooner 
1a821 72 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65  rather than late
1a822 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63  r. */.  p->magic
1a823 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55   = VDBE_MAGIC_RU
1a824 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63  N;..  /* For eac
1a825 68 20 63 75 72 73 6f 72 20 72 65 71 75 69 72 65  h cursor require
1a826 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65  d, also allocate
1a827 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20   a memory cell. 
1a828 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c  Memory.  ** cell
1a829 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f  s (nMem+1-nCurso
1a82a 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73  r)..nMem, inclus
1a82b 69 76 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20  ive, will never 
1a82c 62 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20  be used by.  ** 
1a82d 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d  the vdbe program
1a82e 2e 20 49 6e 73 74 65 61 64 20 74 68 65 79 20 61  . Instead they a
1a82f 72 65 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63  re used to alloc
1a830 61 74 65 20 73 70 61 63 65 20 66 6f 72 0a 20 20  ate space for.  
1a831 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 2f 42 74  ** VdbeCursor/Bt
1a832 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  Cursor structure
1a833 73 2e 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d  s. The blob of m
1a834 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64  emory associated
1a835 20 77 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 73   with .  ** curs
1a836 6f 72 20 30 20 69 73 20 73 74 6f 72 65 64 20 69  or 0 is stored i
1a837 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d  n memory cell nM
1a838 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20  em. Memory cell 
1a839 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74  (nMem-1).  ** st
1a83a 6f 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66  ores the blob of
1a83b 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74   memory associat
1a83c 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 31  ed with cursor 1
1a83d 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  , etc..  **.  **
1a83e 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63   See also: alloc
1a83f 61 74 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a  ateCursor()..  *
1a840 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72  /.  nMem += nCur
1a841 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  sor;..  /* Alloc
1a842 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65  ate space for me
1a843 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 2c 20  mory registers, 
1a844 53 51 4c 20 76 61 72 69 61 62 6c 65 73 2c 20 56  SQL variables, V
1a845 44 42 45 20 63 75 72 73 6f 72 73 20 61 6e 64 20  DBE cursors and 
1a846 0a 20 20 2a 2a 20 61 6e 20 61 72 72 61 79 20 74  .  ** an array t
1a847 6f 20 6d 61 72 73 68 61 6c 20 53 51 4c 20 66 75  o marshal SQL fu
1a848 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73  nction arguments
1a849 20 69 6e 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c   in. This is onl
1a84a 79 20 64 6f 6e 65 20 74 68 65 0a 20 20 2a 2a 20  y done the.  ** 
1a84b 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20  first time this 
1a84c 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1a84d 65 64 20 66 6f 72 20 61 20 67 69 76 65 6e 20 56  ed for a given V
1a84e 44 42 45 2c 20 6e 6f 74 20 77 68 65 6e 20 69 74  DBE, not when it
1a84f 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20 63   is.  ** being c
1a850 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  alled from sqlit
1a851 65 33 5f 72 65 73 65 74 28 29 20 74 6f 20 72 65  e3_reset() to re
1a852 73 65 74 20 74 68 65 20 76 69 72 74 75 61 6c 20  set the virtual 
1a853 6d 61 63 68 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20  machine..  */.  
1a854 69 66 28 20 6e 56 61 72 3e 3d 30 20 26 26 20 41  if( nVar>=0 && A
1a855 4c 57 41 59 53 28 64 62 2d 3e 6d 61 6c 6c 6f 63  LWAYS(db->malloc
1a856 46 61 69 6c 65 64 3d 3d 30 29 20 29 7b 0a 20 20  Failed==0) ){.  
1a857 20 20 75 38 20 2a 7a 43 73 72 20 3d 20 28 75 38    u8 *zCsr = (u8
1a858 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f   *)&p->aOp[p->nO
1a859 70 5d 3b 0a 20 20 20 20 75 38 20 2a 7a 45 6e 64  p];.    u8 *zEnd
1a85a 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70   = (u8 *)&p->aOp
1a85b 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d 3b 0a 20  [p->nOpAlloc];. 
1a85c 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20     int nByte;.  
1a85d 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65    resolveP2Value
1a85e 73 28 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20 20  s(p, &nArg);.   
1a85f 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72   p->usesStmtJour
1a860 6e 61 6c 20 3d 20 28 75 38 29 75 73 65 73 53 74  nal = (u8)usesSt
1a861 6d 74 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 69  mtJournal;.    i
1a862 66 28 20 69 73 45 78 70 6c 61 69 6e 20 26 26 20  f( isExplain && 
1a863 6e 4d 65 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20  nMem<10 ){.     
1a864 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20   nMem = 10;.    
1a865 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a 43 73  }.    memset(zCs
1a866 72 2c 20 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29  r, 0, zEnd-zCsr)
1a867 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 28 7a  ;.    zCsr += (z
1a868 43 73 72 20 2d 20 28 75 38 2a 29 30 29 26 37 3b  Csr - (u8*)0)&7;
1a869 0a 20 20 20 20 61 73 73 65 72 74 28 20 45 49 47  .    assert( EIG
1a86a 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
1a86b 54 28 7a 43 73 72 29 20 29 3b 0a 0a 20 20 20 20  T(zCsr) );..    
1a86c 64 6f 20 7b 0a 20 20 20 20 20 20 6e 42 79 74 65  do {.      nByte
1a86d 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 6c 6c 6f   = 0;.      allo
1a86e 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70  cSpace((char*)&p
1a86f 2d 3e 61 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a  ->aMem, nMem*siz
1a870 65 6f 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c  eof(Mem), &zCsr,
1a871 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a   zEnd, &nByte);.
1a872 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65        allocSpace
1a873 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 56 61 72  ((char*)&p->aVar
1a874 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65  , nVar*sizeof(Me
1a875 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c  m), &zCsr, zEnd,
1a876 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20   &nByte);.      
1a877 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72  allocSpace((char
1a878 2a 29 26 70 2d 3e 61 70 41 72 67 2c 20 6e 41 72  *)&p->apArg, nAr
1a879 67 2a 73 69 7a 65 6f 66 28 4d 65 6d 2a 29 2c 20  g*sizeof(Mem*), 
1a87a 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42  &zCsr, zEnd, &nB
1a87b 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f  yte);.      allo
1a87c 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70  cSpace((char*)&p
1a87d 2d 3e 61 7a 56 61 72 2c 20 6e 56 61 72 2a 73 69  ->azVar, nVar*si
1a87e 7a 65 6f 66 28 63 68 61 72 2a 29 2c 20 26 7a 43  zeof(char*), &zC
1a87f 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65  sr, zEnd, &nByte
1a880 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70  );.      allocSp
1a881 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61  ace((char*)&p->a
1a882 70 43 73 72 2c 20 0a 20 20 20 20 20 20 20 20 20  pCsr, .         
1a883 20 20 20 20 20 20 20 20 6e 43 75 72 73 6f 72 2a          nCursor*
1a884 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f  sizeof(VdbeCurso
1a885 72 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64  r*), &zCsr, zEnd
1a886 2c 20 26 6e 42 79 74 65 0a 20 20 20 20 20 20 29  , &nByte.      )
1a887 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 42 79 74  ;.      if( nByt
1a888 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  e ){.        p->
1a889 70 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33 44  pFree = sqlite3D
1a88a 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
1a88b 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a  nByte);.      }.
1a88c 20 20 20 20 20 20 7a 43 73 72 20 3d 20 70 2d 3e        zCsr = p->
1a88d 70 46 72 65 65 3b 0a 20 20 20 20 20 20 7a 45 6e  pFree;.      zEn
1a88e 64 20 3d 20 26 7a 43 73 72 5b 6e 42 79 74 65 5d  d = &zCsr[nByte]
1a88f 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 6e 42  ;.    }while( nB
1a890 79 74 65 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c  yte && !db->mall
1a891 6f 63 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 20  ocFailed );..   
1a892 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75   p->nCursor = (u
1a893 31 36 29 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20  16)nCursor;.    
1a894 69 66 28 20 70 2d 3e 61 56 61 72 20 29 7b 0a 20  if( p->aVar ){. 
1a895 20 20 20 20 20 70 2d 3e 6e 56 61 72 20 3d 20 28       p->nVar = (
1a896 79 6e 56 61 72 29 6e 56 61 72 3b 0a 20 20 20 20  ynVar)nVar;.    
1a897 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61    for(n=0; n<nVa
1a898 72 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20  r; n++){.       
1a899 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67   p->aVar[n].flag
1a89a 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1a89b 20 20 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d        p->aVar[n]
1a89c 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20  .db = db;.      
1a89d 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
1a89e 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 20  p->aMem ){.     
1a89f 20 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20   p->aMem--;     
1a8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a8a1 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20   /* aMem[] goes 
1a8a2 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a  from 1..nMem */.
1a8a3 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20        p->nMem = 
1a8a4 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  nMem;           
1a8a5 20 20 20 20 20 20 2f 2a 20 20 20 20 20 20 20 6e        /*       n
1a8a6 6f 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d  ot from 0..nMem-
1a8a7 31 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6e  1 */.      for(n
1a8a8 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b  =1; n<=nMem; n++
1a8a9 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d  ){.        p->aM
1a8aa 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d 45  em[n].flags = ME
1a8ab 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20  M_Null;.        
1a8ac 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20  p->aMem[n].db = 
1a8ad 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  db;.      }.    
1a8ae 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c  }.  }.#ifdef SQL
1a8af 49 54 45 5f 44 45 42 55 47 0a 20 20 66 6f 72 28  ITE_DEBUG.  for(
1a8b0 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20  n=1; n<p->nMem; 
1a8b1 6e 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74  n++){.    assert
1a8b2 28 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d  ( p->aMem[n].db=
1a8b3 3d 64 62 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  =db );.  }.#endi
1a8b4 66 0a 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b  f..  p->pc = -1;
1a8b5 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54  .  p->rc = SQLIT
1a8b6 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f 72  E_OK;.  p->error
1a8b7 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72  Action = OE_Abor
1a8b8 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20  t;.  p->explain 
1a8b9 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20  |= isExplain;.  
1a8ba 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f  p->magic = VDBE_
1a8bb 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e  MAGIC_RUN;.  p->
1a8bc 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70  nChange = 0;.  p
1a8bd 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a  ->cacheCtr = 1;.
1a8be 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c    p->minWriteFil
1a8bf 65 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20  eFormat = 255;. 
1a8c0 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d   p->iStatement =
1a8c1 20 30 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f   0;.#ifdef VDBE_
1a8c2 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20  PROFILE.  {.    
1a8c3 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
1a8c4 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b  =0; i<p->nOp; i+
1a8c5 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70  +){.      p->aOp
1a8c6 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20  [i].cnt = 0;.   
1a8c7 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63     p->aOp[i].cyc
1a8c8 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  les = 0;.    }. 
1a8c9 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
1a8ca 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 42 45 20  ** Close a VDBE 
1a8cb 63 75 72 73 6f 72 20 61 6e 64 20 72 65 6c 65 61  cursor and relea
1a8cc 73 65 20 61 6c 6c 20 74 68 65 20 72 65 73 6f 75  se all the resou
1a8cd 72 63 65 73 20 74 68 61 74 20 63 75 72 73 6f 72  rces that cursor
1a8ce 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74 6f 20   .** happens to 
1a8cf 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  hold..*/.SQLITE_
1a8d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a8d1 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73  ite3VdbeFreeCurs
1a8d2 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 64 62 65  or(Vdbe *p, Vdbe
1a8d3 43 75 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20  Cursor *pCx){.  
1a8d4 69 66 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20  if( pCx==0 ){.  
1a8d5 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
1a8d6 69 66 28 20 70 43 78 2d 3e 70 42 74 20 29 7b 0a  if( pCx->pBt ){.
1a8d7 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1a8d8 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74 29 3b  Close(pCx->pBt);
1a8d9 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 43 78 2d  .    /* The pCx-
1a8da 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c 20 62 65  >pCursor will be
1a8db 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69 63   close automatic
1a8dc 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 78 69 73  ally, if it exis
1a8dd 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a 20 74 68  ts, by.    ** th
1a8de 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f  e call above. */
1a8df 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 78  .  }else if( pCx
1a8e0 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20  ->pCursor ){.   
1a8e1 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
1a8e2 73 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 43  seCursor(pCx->pC
1a8e3 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e  ursor);.  }.#ifn
1a8e4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1a8e5 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69  VIRTUALTABLE.  i
1a8e6 66 28 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72  f( pCx->pVtabCur
1a8e7 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  sor ){.    sqlit
1a8e8 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
1a8e9 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 43  pVtabCursor = pC
1a8ea 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b 0a  x->pVtabCursor;.
1a8eb 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65      const sqlite
1a8ec 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
1a8ed 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c 65  e = pCx->pModule
1a8ee 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d  ;.    p->inVtabM
1a8ef 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 28  ethod = 1;.    (
1a8f0 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
1a8f1 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a 20 20  tyOff(p->db);.  
1a8f2 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73    pModule->xClos
1a8f3 65 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a  e(pVtabCursor);.
1a8f4 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
1a8f5 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29  3SafetyOn(p->db)
1a8f6 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d  ;.    p->inVtabM
1a8f7 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23  ethod = 0;.  }.#
1a8f8 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  endif.}../*.** C
1a8f9 6f 70 79 20 74 68 65 20 76 61 6c 75 65 73 20 73  opy the values s
1a8fa 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64 62  tored in the Vdb
1a8fb 65 46 72 61 6d 65 20 73 74 72 75 63 74 75 72 65  eFrame structure
1a8fc 20 74 6f 20 69 74 73 20 56 64 62 65 2e 20 54 68   to its Vdbe. Th
1a8fd 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20 66  is.** is used, f
1a8fe 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e  or example, when
1a8ff 20 61 20 74 72 69 67 67 65 72 20 73 75 62 2d 70   a trigger sub-p
1a900 72 6f 67 72 61 6d 20 69 73 20 68 61 6c 74 65 64  rogram is halted
1a901 20 74 6f 20 72 65 73 74 6f 72 65 0a 2a 2a 20 63   to restore.** c
1a902 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 6d 61  ontrol to the ma
1a903 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53  in program..*/.S
1a904 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1a905 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 61  t sqlite3VdbeFra
1a906 6d 65 52 65 73 74 6f 72 65 28 56 64 62 65 46 72  meRestore(VdbeFr
1a907 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b 0a 20 20  ame *pFrame){.  
1a908 56 64 62 65 20 2a 76 20 3d 20 70 46 72 61 6d 65  Vdbe *v = pFrame
1a909 2d 3e 76 3b 0a 20 20 76 2d 3e 61 4f 70 20 3d 20  ->v;.  v->aOp = 
1a90a 70 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a 20 20 76  pFrame->aOp;.  v
1a90b 2d 3e 6e 4f 70 20 3d 20 70 46 72 61 6d 65 2d 3e  ->nOp = pFrame->
1a90c 6e 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65 6d 20 3d  nOp;.  v->aMem =
1a90d 20 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b 0a 20   pFrame->aMem;. 
1a90e 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72 61 6d   v->nMem = pFram
1a90f 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d 3e 61 70  e->nMem;.  v->ap
1a910 43 73 72 20 3d 20 70 46 72 61 6d 65 2d 3e 61 70  Csr = pFrame->ap
1a911 43 73 72 3b 0a 20 20 76 2d 3e 6e 43 75 72 73 6f  Csr;.  v->nCurso
1a912 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 43 75 72  r = pFrame->nCur
1a913 73 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d 3e 6c 61  sor;.  v->db->la
1a914 73 74 52 6f 77 69 64 20 3d 20 70 46 72 61 6d 65  stRowid = pFrame
1a915 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 76  ->lastRowid;.  v
1a916 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 46 72 61  ->nChange = pFra
1a917 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 72  me->nChange;.  r
1a918 65 74 75 72 6e 20 70 46 72 61 6d 65 2d 3e 70 63  eturn pFrame->pc
1a919 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
1a91a 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2a   all cursors..**
1a91b 0a 2a 2a 20 41 6c 73 6f 20 72 65 6c 65 61 73 65  .** Also release
1a91c 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 6d 65 6d   any dynamic mem
1a91d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 20  ory held by the 
1a91e 56 4d 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61  VM in the Vdbe.a
1a91f 4d 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63  Mem memory .** c
1a920 65 6c 6c 20 61 72 72 61 79 2e 20 54 68 69 73 20  ell array. This 
1a921 69 73 20 6e 65 63 65 73 73 61 72 79 20 61 73 20  is necessary as 
1a922 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  the memory cell 
1a923 61 72 72 61 79 20 6d 61 79 20 63 6f 6e 74 61 69  array may contai
1a924 6e 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f  n.** pointers to
1a925 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63   VdbeFrame objec
1a926 74 73 2c 20 77 68 69 63 68 20 6d 61 79 20 69 6e  ts, which may in
1a927 20 74 75 72 6e 20 63 6f 6e 74 61 69 6e 20 70 6f   turn contain po
1a928 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 6f 70 65  inters to.** ope
1a929 6e 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 73 74  n cursors..*/.st
1a92a 61 74 69 63 20 76 6f 69 64 20 63 6c 6f 73 65 41  atic void closeA
1a92b 6c 6c 43 75 72 73 6f 72 73 28 56 64 62 65 20 2a  llCursors(Vdbe *
1a92c 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 46 72  p){.  if( p->pFr
1a92d 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 65 46  ame ){.    VdbeF
1a92e 72 61 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20 70  rame *pFrame = p
1a92f 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20 20 66 6f  ->pFrame;.    fo
1a930 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61  r(pFrame=p->pFra
1a931 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70 50 61 72  me; pFrame->pPar
1a932 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70 46 72 61  ent; pFrame=pFra
1a933 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20  me->pParent);.  
1a934 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 61    sqlite3VdbeFra
1a935 6d 65 52 65 73 74 6f 72 65 28 70 46 72 61 6d 65  meRestore(pFrame
1a936 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 46 72 61  );.  }.  p->pFra
1a937 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 72  me = 0;.  p->nFr
1a938 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  ame = 0;..  if( 
1a939 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20 20 20 20  p->apCsr ){.    
1a93a 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
1a93b 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72  =0; i<p->nCursor
1a93c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 56 64  ; i++){.      Vd
1a93d 62 65 43 75 72 73 6f 72 20 2a 70 43 20 3d 20 70  beCursor *pC = p
1a93e 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 20 20  ->apCsr[i];.    
1a93f 20 20 69 66 28 20 70 43 20 29 7b 0a 20 20 20 20    if( pC ){.    
1a940 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
1a941 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 43 29  reeCursor(p, pC)
1a942 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70 43  ;.        p->apC
1a943 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20  sr[i] = 0;.     
1a944 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69   }.    }.  }.  i
1a945 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20  f( p->aMem ){.  
1a946 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61    releaseMemArra
1a947 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 70  y(&p->aMem[1], p
1a948 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a  ->nMem);.  }.}..
1a949 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74  /*.** Clean up t
1a94a 68 65 20 56 4d 20 61 66 74 65 72 20 65 78 65 63  he VM after exec
1a94b 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ution..**.** Thi
1a94c 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61  s routine will a
1a94d 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f  utomatically clo
1a94e 73 65 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20  se any cursors, 
1a94f 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a  lists, and/or.**
1a950 20 73 6f 72 74 65 72 73 20 74 68 61 74 20 77 65   sorters that we
1a951 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49  re left open.  I
1a952 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74  t also deletes t
1a953 68 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20  he values of.** 
1a954 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65  variables in the
1a955 20 61 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a   aVar[] array..*
1a956 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c  /.static void Cl
1a957 65 61 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a  eanup(Vdbe *p){.
1a958 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1a959 70 2d 3e 64 62 3b 0a 0a 23 69 66 64 65 66 20 53  p->db;..#ifdef S
1a95a 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a  QLITE_DEBUG.  /*
1a95b 20 45 78 65 63 75 74 65 20 61 73 73 65 72 74 28   Execute assert(
1a95c 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20  ) statements to 
1a95d 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20  ensure that the 
1a95e 56 64 62 65 2e 61 70 43 73 72 5b 5d 20 61 6e 64  Vdbe.apCsr[] and
1a95f 20 0a 20 20 2a 2a 20 56 64 62 65 2e 61 4d 65 6d   .  ** Vdbe.aMem
1a960 5b 5d 20 61 72 72 61 79 73 20 68 61 76 65 20 61  [] arrays have a
1a961 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6c 65 61  lready been clea
1a962 6e 65 64 20 75 70 2e 20 20 2a 2f 0a 20 20 69 6e  ned up.  */.  in
1a963 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
1a964 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b  i<p->nCursor; i+
1a965 2b 29 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70  +) assert( p->ap
1a966 43 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e 61 70 43  Csr==0 || p->apC
1a967 73 72 5b 69 5d 3d 3d 30 20 29 3b 0a 20 20 66 6f  sr[i]==0 );.  fo
1a968 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d 65  r(i=1; i<=p->nMe
1a969 6d 3b 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20  m; i++) assert( 
1a96a 70 2d 3e 61 4d 65 6d 3d 3d 30 20 7c 7c 20 70 2d  p->aMem==0 || p-
1a96b 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67 73 3d 3d  >aMem[i].flags==
1a96c 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 23 65 6e 64  MEM_Null );.#end
1a96d 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 44 62 46  if..  sqlite3DbF
1a96e 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1a96f 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73  sg);.  p->zErrMs
1a970 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 65 73  g = 0;.  p->pRes
1a971 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f  ultSet = 0;.}../
1a972 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d  *.** Set the num
1a973 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f  ber of result co
1a974 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 6c 6c 20  lumns that will 
1a975 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  be returned by t
1a976 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65  his SQL.** state
1a977 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 6e 6f  ment. This is no
1a978 77 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65  w set at compile
1a979 20 74 69 6d 65 2c 20 72 61 74 68 65 72 20 74 68   time, rather th
1a97a 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65 78 65  an during.** exe
1a97b 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 64  cution of the vd
1a97c 62 65 20 70 72 6f 67 72 61 6d 20 73 6f 20 74 68  be program so th
1a97d 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  at sqlite3_colum
1a97e 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a 2a 2a  n_count() can.**
1a97f 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e   be called on an
1a980 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62   SQL statement b
1a981 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 73 74  efore sqlite3_st
1a982 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ep()..*/.SQLITE_
1a983 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a984 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
1a985 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  ls(Vdbe *p, int 
1a986 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d  nResColumn){.  M
1a987 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20  em *pColName;.  
1a988 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33  int n;.  sqlite3
1a989 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20   *db = p->db;.. 
1a98a 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79   releaseMemArray
1a98b 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d  (p->aColName, p-
1a98c 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e  >nResColumn*COLN
1a98d 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65  AME_N);.  sqlite
1a98e 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
1a98f 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d 20  ColName);.  n = 
1a990 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41  nResColumn*COLNA
1a991 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 43  ME_N;.  p->nResC
1a992 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 29 6e 52 65  olumn = (u16)nRe
1a993 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61 43  sColumn;.  p->aC
1a994 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e 61 6d  olName = pColNam
1a995 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 74 65  e = (Mem*)sqlite
1a996 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
1a997 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 6e 20  , sizeof(Mem)*n 
1a998 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 43 6f 6c  );.  if( p->aCol
1a999 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 72 6e  Name==0 ) return
1a99a 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 3e  ;.  while( n-- >
1a99b 20 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 4e 61   0 ){.    pColNa
1a99c 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  me->flags = MEM_
1a99d 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c 4e 61  Null;.    pColNa
1a99e 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  me->db = p->db;.
1a99f 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b 3b 0a      pColName++;.
1a9a0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74    }.}../*.** Set
1a9a1 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1a9a2 20 69 64 78 27 74 68 20 63 6f 6c 75 6d 6e 20 74   idx'th column t
1a9a3 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79  o be returned by
1a9a4 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
1a9a5 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d 75 73  nt..** zName mus
1a9a6 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74  t be a pointer t
1a9a7 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74  o a nul terminat
1a9a8 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  ed string..**.**
1a9a9 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 73 74 20   This call must 
1a9aa 62 65 20 6d 61 64 65 20 61 66 74 65 72 20 61 20  be made after a 
1a9ab 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56  call to sqlite3V
1a9ac 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 29 2e  dbeSetNumCols().
1a9ad 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6e 61 6c  .**.** The final
1a9ae 20 70 61 72 61 6d 65 74 65 72 2c 20 78 44 65 6c   parameter, xDel
1a9af 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66  , must be one of
1a9b0 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c   SQLITE_DYNAMIC,
1a9b1 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 0a 2a   SQLITE_STATIC.*
1a9b2 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 52 41 4e  * or SQLITE_TRAN
1a9b3 53 49 45 4e 54 2e 20 49 66 20 69 74 20 69 73 20  SIENT. If it is 
1a9b4 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c 20  SQLITE_DYNAMIC, 
1a9b5 74 68 65 6e 20 74 68 65 20 62 75 66 66 65 72 20  then the buffer 
1a9b6 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79  pointed.** to by
1a9b7 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 65 20 66   zName will be f
1a9b8 72 65 65 64 20 62 79 20 73 71 6c 69 74 65 33 44  reed by sqlite3D
1a9b9 62 46 72 65 65 28 29 20 77 68 65 6e 20 74 68 65  bFree() when the
1a9ba 20 76 64 62 65 20 69 73 20 64 65 73 74 72 6f 79   vdbe is destroy
1a9bb 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1a9bc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1a9bd 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
1a9be 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20  .  Vdbe *p,     
1a9bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9c0 20 20 20 20 2f 2a 20 56 64 62 65 20 62 65 69 6e      /* Vdbe bein
1a9c1 67 20 63 6f 6e 66 69 67 75 72 65 64 20 2a 2f 0a  g configured */.
1a9c2 20 20 69 6e 74 20 69 64 78 2c 20 20 20 20 20 20    int idx,      
1a9c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9c4 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
1a9c5 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 70 70 6c  olumn zName appl
1a9c6 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  ies to */.  int 
1a9c7 76 61 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  var,            
1a9c8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a9c9 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f 4c 4e 41  One of the COLNA
1a9ca 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a  ME_* constants *
1a9cb 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1a9cc 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20  zName,          
1a9cd 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
1a9ce 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69  to buffer contai
1a9cf 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a 20 20 76  ning name */.  v
1a9d0 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
1a9d1 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *)              
1a9d2 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e 61 67 65  /* Memory manage
1a9d3 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 66 6f  ment strategy fo
1a9d4 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20  r zName */.){.  
1a9d5 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 20 2a 70  int rc;.  Mem *p
1a9d6 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72  ColName;.  asser
1a9d7 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 73 43 6f  t( idx<p->nResCo
1a9d8 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 65 72 74  lumn );.  assert
1a9d9 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f 4e 20  ( var<COLNAME_N 
1a9da 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e  );.  if( p->db->
1a9db 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1a9dc 20 20 20 20 61 73 73 65 72 74 28 20 21 7a 4e 61      assert( !zNa
1a9dd 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 51 4c 49  me || xDel!=SQLI
1a9de 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b 0a 20 20  TE_DYNAMIC );.  
1a9df 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1a9e0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 73  NOMEM;.  }.  ass
1a9e1 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65  ert( p->aColName
1a9e2 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c 4e 61 6d  !=0 );.  pColNam
1a9e3 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e 61 6d  e = &(p->aColNam
1a9e4 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e 6e 52 65  e[idx+var*p->nRe
1a9e5 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 72 63 20  sColumn]);.  rc 
1a9e6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
1a9e7 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 6d 65 2c  SetStr(pColName,
1a9e8 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49   zName, -1, SQLI
1a9e9 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a  TE_UTF8, xDel);.
1a9ea 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 30 20    assert( rc!=0 
1a9eb 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 28 70 43  || !zName || (pC
1a9ec 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 26 4d 45  olName->flags&ME
1a9ed 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b 0a 20 20  M_Term)!=0 );.  
1a9ee 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1a9ef 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 77 72  .** A read or wr
1a9f0 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite transaction 
1a9f1 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62  may or may not b
1a9f2 65 20 61 63 74 69 76 65 20 6f 6e 20 64 61 74 61  e active on data
1a9f3 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64  base handle.** d
1a9f4 62 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74  b. If a transact
1a9f5 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c 20 63  ion is active, c
1a9f6 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 74 68 65  ommit it. If the
1a9f7 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 69 74 65  re is a.** write
1a9f8 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 70 61  -transaction spa
1a9f9 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  nning more than 
1a9fa 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  one database fil
1a9fb 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  e, this routine.
1a9fc 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 20 6f 66  ** takes care of
1a9fd 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
1a9fe 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e 0a 2a 2f  nal trickery..*/
1a9ff 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62 65  .static int vdbe
1aa00 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a  Commit(sqlite3 *
1aa01 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 0a 20 20  db, Vdbe *p){.  
1aa02 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 72  int i;.  int nTr
1aa03 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d  ans = 0;  /* Num
1aa04 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 65 73  ber of databases
1aa05 20 77 69 74 68 20 61 6e 20 61 63 74 69 76 65 20   with an active 
1aa06 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1aa07 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  n */.  int rc = 
1aa08 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
1aa09 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 30   needXcommit = 0
1aa0a 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
1aa0b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1aa0c 4c 45 0a 20 20 2f 2a 20 57 69 74 68 20 74 68 69  LE.  /* With thi
1aa0d 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c 69 74 65  s option, sqlite
1aa0e 33 56 74 61 62 53 79 6e 63 28 29 20 69 73 20 64  3VtabSync() is d
1aa0f 65 66 69 6e 65 64 20 74 6f 20 62 65 20 73 69 6d  efined to be sim
1aa10 70 6c 79 20 0a 20 20 2a 2a 20 53 51 4c 49 54 45  ply .  ** SQLITE
1aa11 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e 6f 74 20  _OK so p is not 
1aa12 75 73 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 55 4e  used. .  */.  UN
1aa13 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
1aa14 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20  );.#endif..  /* 
1aa15 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79  Before doing any
1aa16 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c 6c  thing else, call
1aa17 20 74 68 65 20 78 53 79 6e 63 28 29 20 63 61 6c   the xSync() cal
1aa18 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20 20  lback for any.  
1aa19 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c  ** virtual modul
1aa1a 65 20 74 61 62 6c 65 73 20 77 72 69 74 74 65 6e  e tables written
1aa1b 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63   in this transac
1aa1c 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73 20 74  tion. This has t
1aa1d 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20 62  o.  ** be done b
1aa1e 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e 69 6e  efore determinin
1aa1f 67 20 77 68 65 74 68 65 72 20 61 20 6d 61 73 74  g whether a mast
1aa20 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
1aa21 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69 72 65  is .  ** require
1aa22 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63 28 29  d, as an xSync()
1aa23 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 64   callback may ad
1aa24 64 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  d an attached da
1aa25 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20 74  tabase.  ** to t
1aa26 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  he transaction..
1aa27 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69    */.  rc = sqli
1aa28 74 65 33 56 74 61 62 53 79 6e 63 28 64 62 2c 20  te3VtabSync(db, 
1aa29 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  &p->zErrMsg);.  
1aa2a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1aa2b 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
1aa2c 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  rc;.  }..  /* Th
1aa2d 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d 69 6e  is loop determin
1aa2e 65 73 20 28 61 29 20 69 66 20 74 68 65 20 63 6f  es (a) if the co
1aa2f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75 6c 64  mmit hook should
1aa30 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e 64 0a   be invoked and.
1aa31 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d 61 6e    ** (b) how man
1aa32 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  y database files
1aa33 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69 74 65   have open write
1aa34 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c 20 6e   transactions, n
1aa35 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 64 69  ot .  ** includi
1aa36 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61 74 61  ng the temp data
1aa37 62 61 73 65 2e 20 28 62 29 20 69 73 20 69 6d 70  base. (b) is imp
1aa38 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20 69  ortant because i
1aa39 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20 20 2a  f more than .  *
1aa3a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66  * one database f
1aa3b 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65 6e 20  ile has an open 
1aa3c 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
1aa3d 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  n, a master jour
1aa3e 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 73  nal.  ** file is
1aa3f 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 6e   required for an
1aa40 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e 0a   atomic commit..
1aa41 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d 30 3b    */ .  for(i=0;
1aa42 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
1aa43 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42  { .    Btree *pB
1aa44 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
1aa45 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  Bt;.    if( sqli
1aa46 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e  te3BtreeIsInTran
1aa47 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  s(pBt) ){.      
1aa48 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31 3b  needXcommit = 1;
1aa49 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d 31 20  .      if( i!=1 
1aa4a 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 20 20  ) nTrans++;.    
1aa4b 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
1aa4c 68 65 72 65 20 61 72 65 20 61 6e 79 20 77 72 69  here are any wri
1aa4d 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  te-transactions 
1aa4e 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74  at all, invoke t
1aa4f 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a  he commit hook *
1aa50 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f 6d  /.  if( needXcom
1aa51 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d 6d  mit && db->xComm
1aa52 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20  itCallback ){.  
1aa53 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
1aa54 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
1aa55 20 20 72 63 20 3d 20 64 62 2d 3e 78 43 6f 6d 6d    rc = db->xComm
1aa56 69 74 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70  itCallback(db->p
1aa57 43 6f 6d 6d 69 74 41 72 67 29 3b 0a 20 20 20 20  CommitArg);.    
1aa58 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1aa59 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 69  etyOn(db);.    i
1aa5a 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
1aa5b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e  eturn SQLITE_CON
1aa5c 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20  STRAINT;.    }. 
1aa5d 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d   }..  /* The sim
1aa5e 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f  ple case - no mo
1aa5f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61  re than one data
1aa60 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 63  base file (not c
1aa61 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a  ounting the.  **
1aa62 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 29 20   TEMP database) 
1aa63 68 61 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f  has a transactio
1aa64 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68 65 72  n active.   Ther
1aa65 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72  e is no need for
1aa66 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72   the.  ** master
1aa67 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20  -journal..  **. 
1aa68 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74 75 72   ** If the retur
1aa69 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74  n value of sqlit
1aa6a 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61  e3BtreeGetFilena
1aa6b 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f 20 6c  me() is a zero l
1aa6c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e  ength.  ** strin
1aa6d 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20  g, it means the 
1aa6e 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73  main database is
1aa6f 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74   :memory: or a t
1aa70 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e 20 0a 20  emp file.  In . 
1aa71 20 2a 2a 20 74 68 61 74 20 63 61 73 65 20 77 65   ** that case we
1aa72 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20   do not support 
1aa73 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c  atomic multi-fil
1aa74 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73  e commits, so us
1aa75 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70  e the .  ** simp
1aa76 6c 65 20 63 61 73 65 20 74 68 65 6e 20 74 6f 6f  le case then too
1aa77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d  ..  */.  if( 0==
1aa78 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1aa79 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46  sqlite3BtreeGetF
1aa7a 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b  ilename(db->aDb[
1aa7b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c 7c 20 6e  0].pBt)).   || n
1aa7c 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 20  Trans<=1.  ){.  
1aa7d 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
1aa7e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
1aa7f 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
1aa80 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
1aa81 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
1aa82 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b        if( pBt ){
1aa83 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1aa84 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
1aa85 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 29  PhaseOne(pBt, 0)
1aa86 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1aa87 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63  .    /* Do the c
1aa88 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c  ommit only if al
1aa89 6c 20 64 61 74 61 62 61 73 65 73 20 73 75 63 63  l databases succ
1aa8a 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74  essfully complet
1aa8b 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20 20 20  e phase 1. .    
1aa8c 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65  ** If one of the
1aa8d 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73   BtreeCommitPhas
1aa8e 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 69  eOne() calls fai
1aa8f 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74  ls, this indicat
1aa90 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20  es an.    ** IO 
1aa91 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65 6c 65  error while dele
1aa92 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69  ting or truncati
1aa93 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ng a journal fil
1aa94 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c  e. It is unlikel
1aa95 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f  y,.    ** but co
1aa96 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 74  uld happen. In t
1aa97 68 69 73 20 63 61 73 65 20 61 62 61 6e 64 6f 6e  his case abandon
1aa98 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20   processing and 
1aa99 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
1aa9a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
1aa9b 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=0; rc==SQLITE
1aa9c 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62  _OK && i<db->nDb
1aa9d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74  ; i++){.      Bt
1aa9e 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
1aa9f 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20  Db[i].pBt;.     
1aaa0 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20   if( pBt ){.    
1aaa1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1aaa2 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
1aaa3 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20  Two(pBt);.      
1aaa4 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
1aaa5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1aaa6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74  .      sqlite3Vt
1aaa7 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20  abCommit(db);.  
1aaa8 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68    }.  }..  /* Th
1aaa9 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d  e complex case -
1aaaa 20 54 68 65 72 65 20 69 73 20 61 20 6d 75 6c 74   There is a mult
1aaab 69 2d 66 69 6c 65 20 77 72 69 74 65 2d 74 72 61  i-file write-tra
1aaac 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76 65 2e  nsaction active.
1aaad 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 71 75 69  .  ** This requi
1aaae 72 65 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75  res a master jou
1aaaf 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73  rnal file to ens
1aab0 75 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74  ure the transact
1aab1 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d  ion is.  ** comm
1aab2 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a  itted atomicly..
1aab3 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
1aab4 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a  ITE_OMIT_DISKIO.
1aab5 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69    else{.    sqli
1aab6 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20  te3_vfs *pVfs = 
1aab7 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e  db->pVfs;.    in
1aab8 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a  t needSync = 0;.
1aab9 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65      char *zMaste
1aaba 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65  r = 0;   /* File
1aabb 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 6d 61  -name for the ma
1aabc 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  ster journal */.
1aabd 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a      char const *
1aabe 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69  zMainFile = sqli
1aabf 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e  te3BtreeGetFilen
1aac0 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  ame(db->aDb[0].p
1aac1 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Bt);.    sqlite3
1aac2 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d  _file *pMaster =
1aac3 20 30 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 73   0;.    i64 offs
1aac4 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  et = 0;.    int 
1aac5 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c  res;..    /* Sel
1aac6 65 63 74 20 61 20 6d 61 73 74 65 72 20 6a 6f 75  ect a master jou
1aac7 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a  rnal file name *
1aac8 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20  /.    do {.     
1aac9 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b 0a 20 20   u32 iRandom;.  
1aaca 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1aacb 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a  e(db, zMaster);.
1aacc 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61        sqlite3_ra
1aacd 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28  ndomness(sizeof(
1aace 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52 61 6e 64  iRandom), &iRand
1aacf 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d 61 73 74  om);.      zMast
1aad0 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  er = sqlite3MPri
1aad1 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d 6a 25 30  ntf(db, "%s-mj%0
1aad2 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c 65 2c 20  8X", zMainFile, 
1aad3 69 52 61 6e 64 6f 6d 26 30 78 37 66 66 66 66 66  iRandom&0x7fffff
1aad4 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ff);.      if( !
1aad5 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20  zMaster ){.     
1aad6 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1aad7 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a  _NOMEM;.      }.
1aad8 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1aad9 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c  e3OsAccess(pVfs,
1aada 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45   zMaster, SQLITE
1aadb 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20  _ACCESS_EXISTS, 
1aadc 26 72 65 73 29 3b 0a 20 20 20 20 7d 77 68 69 6c  &res);.    }whil
1aadd 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
1aade 20 26 26 20 72 65 73 20 29 3b 0a 20 20 20 20 69   && res );.    i
1aadf 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1aae0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65   ){.      /* Ope
1aae1 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  n the master jou
1aae2 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 72  rnal. */.      r
1aae3 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
1aae4 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c 20 7a 4d  nMalloc(pVfs, zM
1aae5 61 73 74 65 72 2c 20 26 70 4d 61 73 74 65 72 2c  aster, &pMaster,
1aae6 20 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49   .          SQLI
1aae7 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
1aae8 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  E|SQLITE_OPEN_CR
1aae9 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20 20 20  EATE|.          
1aaea 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
1aaeb 55 53 49 56 45 7c 53 51 4c 49 54 45 5f 4f 50 45  USIVE|SQLITE_OPE
1aaec 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c  N_MASTER_JOURNAL
1aaed 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  , 0.      );.   
1aaee 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53   }.    if( rc!=S
1aaef 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1aaf0 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1aaf1 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20  db, zMaster);.  
1aaf2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1aaf3 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a 20 57 72     }. .    /* Wr
1aaf4 69 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ite the name of 
1aaf5 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69  each database fi
1aaf6 6c 65 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61  le in the transa
1aaf7 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 6e  ction into the n
1aaf8 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72  ew.    ** master
1aaf9 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49   journal file. I
1aafa 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
1aafb 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20  s at this point 
1aafc 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64  close.    ** and
1aafd 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74   delete the mast
1aafe 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
1aaff 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 64   All the individ
1ab00 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ual journal file
1ab01 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 68  s.    ** still h
1ab02 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 20 74 68  ave 'null' as th
1ab03 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
1ab04 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 74 68 65   pointer, so the
1ab05 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20 20  y will roll.    
1ab06 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64  ** back independ
1ab07 65 6e 74 6c 79 20 69 66 20 61 20 66 61 69 6c 75  ently if a failu
1ab08 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 20 20 2a  re occurs..    *
1ab09 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
1ab0a 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
1ab0b 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74        Btree *pBt
1ab0c 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
1ab0d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d  t;.      if( i==
1ab0e 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 20  1 ) continue;   
1ab0f 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 54 45  /* Ignore the TE
1ab10 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  MP database */. 
1ab11 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1ab12 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70  BtreeIsInTrans(p
1ab13 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63  Bt) ){.        c
1ab14 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65  har const *zFile
1ab15 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
1ab16 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 42  etJournalname(pB
1ab17 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t);.        if( 
1ab18 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 20 63 6f  zFile[0]==0 ) co
1ab19 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 67 6e 6f  ntinue;  /* Igno
1ab1a 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61  re :memory: data
1ab1b 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20 20 20  bases */.       
1ab1c 20 69 66 28 20 21 6e 65 65 64 53 79 6e 63 20 26   if( !needSync &
1ab1d 26 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 53  & !sqlite3BtreeS
1ab1e 79 6e 63 44 69 73 61 62 6c 65 64 28 70 42 74 29  yncDisabled(pBt)
1ab1f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65   ){.          ne
1ab20 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20  edSync = 1;.    
1ab21 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63      }.        rc
1ab22 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
1ab23 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 69 6c 65  e(pMaster, zFile
1ab24 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
1ab25 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66 73  0(zFile)+1, offs
1ab26 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66  et);.        off
1ab27 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  set += sqlite3St
1ab28 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 3b  rlen30(zFile)+1;
1ab29 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
1ab2a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1ab2b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
1ab2c 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74  sCloseFree(pMast
1ab2d 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  er);.          s
1ab2e 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70  qlite3OsDelete(p
1ab2f 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29  Vfs, zMaster, 0)
1ab30 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1ab31 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d  te3DbFree(db, zM
1ab32 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20  aster);.        
1ab33 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1ab34 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1ab35 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e     }..    /* Syn
1ab36 63 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  c the master jou
1ab37 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68  rnal file. If th
1ab38 65 20 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49  e IOCAP_SEQUENTI
1ab39 41 4c 20 64 65 76 69 63 65 0a 20 20 20 20 2a 2a  AL device.    **
1ab3a 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68 69   flag is set thi
1ab3b 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  s is not require
1ab3c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d..    */.    if
1ab3d 28 20 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 20  ( needSync .    
1ab3e 20 26 26 20 30 3d 3d 28 73 71 6c 69 74 65 33 4f   && 0==(sqlite3O
1ab3f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
1ab40 69 73 74 69 63 73 28 70 4d 61 73 74 65 72 29 26  istics(pMaster)&
1ab41 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
1ab42 55 45 4e 54 49 41 4c 29 0a 20 20 20 20 20 26 26  UENTIAL).     &&
1ab43 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
1ab44 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28  = sqlite3OsSync(
1ab45 70 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f  pMaster, SQLITE_
1ab46 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29 0a 20 20  SYNC_NORMAL)).  
1ab47 20 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74    ){.      sqlit
1ab48 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d  e3OsCloseFree(pM
1ab49 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 73 71  aster);.      sq
1ab4a 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56  lite3OsDelete(pV
1ab4b 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b  fs, zMaster, 0);
1ab4c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
1ab4d 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72  Free(db, zMaster
1ab4e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1ab4f 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  rc;.    }..    /
1ab50 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68 65 20 64  * Sync all the d
1ab51 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64  b files involved
1ab52 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74   in the transact
1ab53 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65 20 63 61  ion. The same ca
1ab54 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74 73 20 74  ll.    ** sets t
1ab55 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
1ab56 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 65 61 63  l pointer in eac
1ab57 68 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75  h individual jou
1ab58 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20  rnal. If.    ** 
1ab59 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
1ab5a 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c  here, do not del
1ab5b 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ete the master j
1ab5c 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20  ournal file..   
1ab5d 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68   **.    ** If th
1ab5e 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  e error occurs d
1ab5f 75 72 69 6e 67 20 74 68 65 20 66 69 72 73 74 20  uring the first 
1ab60 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73  call to.    ** s
1ab61 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
1ab62 74 50 68 61 73 65 4f 6e 65 28 29 2c 20 74 68 65  tPhaseOne(), the
1ab63 6e 20 74 68 65 72 65 20 69 73 20 61 20 63 68 61  n there is a cha
1ab64 6e 63 65 20 74 68 61 74 20 74 68 65 0a 20 20 20  nce that the.   
1ab65 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   ** master journ
1ab66 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20  al file will be 
1ab67 6f 72 70 68 61 6e 65 64 2e 20 42 75 74 20 77 65  orphaned. But we
1ab68 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69   cannot delete i
1ab69 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20 63 61 73  t,.    ** in cas
1ab6a 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
1ab6b 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77  rnal file name w
1ab6c 61 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  as written into 
1ab6d 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20  the journal.    
1ab6e 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74  ** file before t
1ab6f 68 65 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72  he failure occur
1ab70 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  red..    */.    
1ab71 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  for(i=0; rc==SQL
1ab72 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e  ITE_OK && i<db->
1ab73 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20  nDb; i++){ .    
1ab74 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
1ab75 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
1ab76 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a       if( pBt ){.
1ab77 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1ab78 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
1ab79 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 7a 4d 61  haseOne(pBt, zMa
1ab7a 73 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ster);.      }. 
1ab7b 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1ab7c 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73  OsCloseFree(pMas
1ab7d 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ter);.    if( rc
1ab7e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1ab7f 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1ab80 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b  ee(db, zMaster);
1ab81 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
1ab82 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1ab83 44 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65  Delete the maste
1ab84 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  r journal file. 
1ab85 54 68 69 73 20 63 6f 6d 6d 69 74 73 20 74 68 65  This commits the
1ab86 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 66   transaction. Af
1ab87 74 65 72 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67  ter.    ** doing
1ab88 20 74 68 69 73 20 74 68 65 20 64 69 72 65 63 74   this the direct
1ab89 6f 72 79 20 69 73 20 73 79 6e 63 65 64 20 61 67  ory is synced ag
1ab8a 61 69 6e 20 62 65 66 6f 72 65 20 61 6e 79 20 69  ain before any i
1ab8b 6e 64 69 76 69 64 75 61 6c 0a 20 20 20 20 2a 2a  ndividual.    **
1ab8c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 69 6c   transaction fil
1ab8d 65 73 20 61 72 65 20 64 65 6c 65 74 65 64 2e 0a  es are deleted..
1ab8e 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
1ab8f 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
1ab90 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 31  pVfs, zMaster, 1
1ab91 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
1ab92 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72  Free(db, zMaster
1ab93 29 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d  );.    zMaster =
1ab94 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29   0;.    if( rc )
1ab95 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
1ab96 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  c;.    }..    /*
1ab97 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e 64 20 64   All files and d
1ab98 69 72 65 63 74 6f 72 69 65 73 20 68 61 76 65 20  irectories have 
1ab99 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e  already been syn
1ab9a 63 65 64 2c 20 73 6f 20 74 68 65 20 66 6f 6c 6c  ced, so the foll
1ab9b 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 61 6c  owing.    ** cal
1ab9c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  ls to sqlite3Btr
1ab9d 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
1ab9e 28 29 20 61 72 65 20 6f 6e 6c 79 20 63 6c 6f 73  () are only clos
1ab9f 69 6e 67 20 66 69 6c 65 73 20 61 6e 64 0a 20 20  ing files and.  
1aba0 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67 20 6f 72    ** deleting or
1aba1 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72   truncating jour
1aba2 6e 61 6c 73 2e 20 49 66 20 73 6f 6d 65 74 68 69  nals. If somethi
1aba3 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 20 77 68  ng goes wrong wh
1aba4 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20  ile.    ** this 
1aba5 69 73 20 68 61 70 70 65 6e 69 6e 67 20 77 65 20  is happening we 
1aba6 64 6f 6e 27 74 20 72 65 61 6c 6c 79 20 63 61 72  don't really car
1aba7 65 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74 79  e. The integrity
1aba8 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 74   of the.    ** t
1aba9 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 6c  ransaction is al
1abaa 72 65 61 64 79 20 67 75 61 72 61 6e 74 65 65 64  ready guaranteed
1abab 2c 20 62 75 74 20 73 6f 6d 65 20 73 74 72 61 79  , but some stray
1abac 20 27 63 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c 73   'cold' journals
1abad 0a 20 20 20 20 2a 2a 20 6d 61 79 20 62 65 20 6c  .    ** may be l
1abae 79 69 6e 67 20 61 72 6f 75 6e 64 2e 20 52 65 74  ying around. Ret
1abaf 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20  urning an error 
1abb0 63 6f 64 65 20 77 6f 6e 27 74 20 68 65 6c 70 20  code won't help 
1abb1 6d 61 74 74 65 72 73 2e 0a 20 20 20 20 2a 2f 0a  matters..    */.
1abb2 20 20 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75      disable_simu
1abb3 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
1abb4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  );.    sqlite3Be
1abb5 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
1abb6 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
1abb7 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
1abb8 20 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70   .      Btree *p
1abb9 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  Bt = db->aDb[i].
1abba 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  pBt;.      if( p
1abbb 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  Bt ){.        sq
1abbc 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
1abbd 50 68 61 73 65 54 77 6f 28 70 42 74 29 3b 0a 20  PhaseTwo(pBt);. 
1abbe 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1abbf 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
1abc0 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 65  nMalloc();.    e
1abc1 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
1abc2 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 0a 20 20  io_errors();..  
1abc3 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d    sqlite3VtabCom
1abc4 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e  mit(db);.  }.#en
1abc5 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 72 63  dif..  return rc
1abc6 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73  ;.}../* .** This
1abc7 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20   routine checks 
1abc8 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33  that the sqlite3
1abc9 2e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 63  .activeVdbeCnt c
1abca 6f 75 6e 74 20 76 61 72 69 61 62 6c 65 0a 2a 2a  ount variable.**
1abcb 20 6d 61 74 63 68 65 73 20 74 68 65 20 6e 75 6d   matches the num
1abcc 62 65 72 20 6f 66 20 76 64 62 65 27 73 20 69 6e  ber of vdbe's in
1abcd 20 74 68 65 20 6c 69 73 74 20 73 71 6c 69 74 65   the list sqlite
1abce 33 2e 70 56 64 62 65 20 74 68 61 74 20 61 72 65  3.pVdbe that are
1abcf 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 61 63  .** currently ac
1abd0 74 69 76 65 2e 20 41 6e 20 61 73 73 65 72 74 69  tive. An asserti
1abd1 6f 6e 20 66 61 69 6c 73 20 69 66 20 74 68 65 20  on fails if the 
1abd2 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f 20 6e 6f  two counts do no
1abd3 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54 68 69 73  t match..** This
1abd4 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20   is an internal 
1abd5 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e 6c 79 20  self-check only 
1abd6 2d 20 69 74 20 69 73 20 6e 6f 74 20 61 6e 20 65  - it is not an e
1abd7 73 73 65 6e 74 69 61 6c 20 70 72 6f 63 65 73 73  ssential process
1abd8 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a  ing.** step..**.
1abd9 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d  ** This is a no-
1abda 6f 70 20 69 66 20 4e 44 45 42 55 47 20 69 73 20  op if NDEBUG is 
1abdb 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e  defined..*/.#ifn
1abdc 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69  def NDEBUG.stati
1abdd 63 20 76 6f 69 64 20 63 68 65 63 6b 41 63 74 69  c void checkActi
1abde 76 65 56 64 62 65 43 6e 74 28 73 71 6c 69 74 65  veVdbeCnt(sqlite
1abdf 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a  3 *db){.  Vdbe *
1abe0 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30  p;.  int cnt = 0
1abe1 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20 3d  ;.  int nWrite =
1abe2 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d 3e 70 56   0;.  p = db->pV
1abe3 64 62 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20  dbe;.  while( p 
1abe4 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6d 61  ){.    if( p->ma
1abe5 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
1abe6 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20  RUN && p->pc>=0 
1abe7 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a  ){.      cnt++;.
1abe8 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 65 61        if( p->rea
1abe9 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57 72 69 74  dOnly==0 ) nWrit
1abea 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  e++;.    }.    p
1abeb 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d   = p->pNext;.  }
1abec 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 3d 3d  .  assert( cnt==
1abed 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
1abee 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  t );.  assert( n
1abef 57 72 69 74 65 3d 3d 64 62 2d 3e 77 72 69 74 65  Write==db->write
1abf0 56 64 62 65 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c  VdbeCnt );.}.#el
1abf1 73 65 0a 23 64 65 66 69 6e 65 20 63 68 65 63 6b  se.#define check
1abf2 41 63 74 69 76 65 56 64 62 65 43 6e 74 28 78 29  ActiveVdbeCnt(x)
1abf3 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46  .#endif../*.** F
1abf4 6f 72 20 65 76 65 72 79 20 42 74 72 65 65 20 74  or every Btree t
1abf5 68 61 74 20 69 6e 20 64 61 74 61 62 61 73 65 20  hat in database 
1abf6 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 77 68  connection db wh
1abf7 69 63 68 20 0a 2a 2a 20 68 61 73 20 62 65 65 6e  ich .** has been
1abf8 20 6d 6f 64 69 66 69 65 64 2c 20 22 74 72 69 70   modified, "trip
1abf9 22 20 6f 72 20 69 6e 76 61 6c 69 64 61 74 65 20  " or invalidate 
1abfa 65 61 63 68 20 63 75 72 73 6f 72 20 69 6e 0a 2a  each cursor in.*
1abfb 2a 20 74 68 61 74 20 42 74 72 65 65 20 6d 69 67  * that Btree mig
1abfc 68 74 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64  ht have been mod
1abfd 69 66 69 65 64 20 73 6f 20 74 68 61 74 20 74 68  ified so that th
1abfe 65 20 63 75 72 73 6f 72 0a 2a 2a 20 63 61 6e 20  e cursor.** can 
1abff 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 61 67  never be used ag
1ac00 61 69 6e 2e 20 20 54 68 69 73 20 68 61 70 70 65  ain.  This happe
1ac01 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61  ns when a rollba
1ac02 63 6b 0a 2a 2a 2a 20 6f 63 63 75 72 73 2e 20 20  ck.*** occurs.  
1ac03 57 65 20 68 61 76 65 20 74 6f 20 74 72 69 70 20  We have to trip 
1ac04 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 63 75  all the other cu
1ac05 72 73 6f 72 73 2c 20 65 76 65 6e 0a 2a 2a 20 63  rsors, even.** c
1ac06 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74 68 65 72  ursor from other
1ac07 20 56 4d 73 20 69 6e 20 64 69 66 66 65 72 65 6e   VMs in differen
1ac08 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  t database conne
1ac09 63 74 69 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68  ctions,.** so th
1ac0a 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 6d 20  at none of them 
1ac0b 74 72 79 20 74 6f 20 75 73 65 20 74 68 65 20 64  try to use the d
1ac0c 61 74 61 20 61 74 20 77 68 69 63 68 20 74 68 65  ata at which the
1ac0d 79 0a 2a 2a 20 77 65 72 65 20 70 6f 69 6e 74 69  y.** were pointi
1ac0e 6e 67 20 61 6e 64 20 77 68 69 63 68 20 6e 6f 77  ng and which now
1ac0f 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 63   may have been c
1ac10 68 61 6e 67 65 64 20 64 75 65 0a 2a 2a 20 74 6f  hanged due.** to
1ac11 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a   the rollback..*
1ac12 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68  *.** Remember th
1ac13 61 74 20 61 20 72 6f 6c 6c 62 61 63 6b 20 63 61  at a rollback ca
1ac14 6e 20 64 65 6c 65 74 65 20 74 61 62 6c 65 73 20  n delete tables 
1ac15 63 6f 6d 70 6c 65 74 65 20 61 6e 64 0a 2a 2a 20  complete and.** 
1ac16 72 65 6f 72 64 65 72 20 72 6f 6f 74 70 61 67 65  reorder rootpage
1ac17 73 2e 20 20 53 6f 20 69 74 20 69 73 20 6e 6f 74  s.  So it is not
1ac18 20 73 75 66 66 69 63 69 65 6e 74 20 6a 75 73 74   sufficient just
1ac19 20 74 6f 20 73 61 76 65 0a 2a 2a 20 74 68 65 20   to save.** the 
1ac1a 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 75 72  state of the cur
1ac1b 73 6f 72 2e 20 20 57 65 20 68 61 76 65 20 74 6f  sor.  We have to
1ac1c 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20   invalidate the 
1ac1d 63 75 72 73 6f 72 0a 2a 2a 20 73 6f 20 74 68 61  cursor.** so tha
1ac1e 74 20 69 74 20 69 73 20 6e 65 76 65 72 20 75 73  t it is never us
1ac1f 65 64 20 61 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61  ed again..*/.sta
1ac20 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64  tic void invalid
1ac21 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69  ateCursorsOnModi
1ac22 66 69 65 64 42 74 72 65 65 73 28 73 71 6c 69 74  fiedBtrees(sqlit
1ac23 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69  e3 *db){.  int i
1ac24 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64  ;.  for(i=0; i<d
1ac25 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
1ac26 20 20 42 74 72 65 65 20 2a 70 20 3d 20 64 62 2d    Btree *p = db-
1ac27 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
1ac28 20 69 66 28 20 70 20 26 26 20 73 71 6c 69 74 65   if( p && sqlite
1ac29 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
1ac2a 70 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  p) ){.      sqli
1ac2b 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43  te3BtreeTripAllC
1ac2c 75 72 73 6f 72 73 28 70 2c 20 53 51 4c 49 54 45  ursors(p, SQLITE
1ac2d 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20 7d 0a 20  _ABORT);.    }. 
1ac2e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74   }.}../*.** If t
1ac2f 68 65 20 56 64 62 65 20 70 61 73 73 65 64 20 61  he Vdbe passed a
1ac30 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
1ac31 6d 65 6e 74 20 6f 70 65 6e 65 64 20 61 20 73 74  ment opened a st
1ac32 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74  atement-transact
1ac33 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 20 69 74  ion,.** close it
1ac34 20 6e 6f 77 2e 20 41 72 67 75 6d 65 6e 74 20 65   now. Argument e
1ac35 4f 70 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  Op must be eithe
1ac36 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  r SAVEPOINT_ROLL
1ac37 42 41 43 4b 20 6f 72 0a 2a 2a 20 53 41 56 45 50  BACK or.** SAVEP
1ac38 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 49 66  OINT_RELEASE. If
1ac39 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54   it is SAVEPOINT
1ac3a 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20  _ROLLBACK, then 
1ac3b 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  the statement.**
1ac3c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1ac3d 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20  rolled back. If 
1ac3e 65 4f 70 20 69 73 20 53 41 56 45 50 4f 49 4e 54  eOp is SAVEPOINT
1ac3f 5f 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 74  _RELEASE, then t
1ac40 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  he .** statement
1ac41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1ac42 63 6f 6d 6d 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  commtted..**.** 
1ac43 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f  If an IO error o
1ac44 63 63 75 72 73 2c 20 61 6e 20 53 51 4c 49 54 45  ccurs, an SQLITE
1ac45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72  _IOERR_XXX error
1ac46 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
1ac47 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  d. .** Otherwise
1ac48 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53   SQLITE_OK..*/.S
1ac49 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1ac4a 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f  t sqlite3VdbeClo
1ac4b 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 62 65  seStatement(Vdbe
1ac4c 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29 7b 0a 20   *p, int eOp){. 
1ac4d 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e 73 74 20   sqlite3 *const 
1ac4e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e  db = p->db;.  in
1ac4f 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
1ac50 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 2d 3e 69 53  ;..  /* If p->iS
1ac51 74 61 74 65 6d 65 6e 74 20 69 73 20 67 72 65 61  tatement is grea
1ac52 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74  ter than zero, t
1ac53 68 65 6e 20 74 68 69 73 20 56 64 62 65 20 6f 70  hen this Vdbe op
1ac54 65 6e 65 64 20 61 20 0a 20 20 2a 2a 20 73 74 61  ened a .  ** sta
1ac55 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1ac56 6f 6e 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  on that should b
1ac57 65 20 63 6c 6f 73 65 64 20 68 65 72 65 2e 20 54  e closed here. T
1ac58 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f  he only exceptio
1ac59 6e 0a 20 20 2a 2a 20 69 73 20 74 68 61 74 20 61  n.  ** is that a
1ac5a 6e 20 49 4f 20 65 72 72 6f 72 20 6d 61 79 20 68  n IO error may h
1ac5b 61 76 65 20 6f 63 63 75 72 65 64 2c 20 63 61 75  ave occured, cau
1ac5c 73 69 6e 67 20 61 6e 20 65 6d 65 72 67 65 6e 63  sing an emergenc
1ac5d 79 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2a  y rollback..  **
1ac5e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 28 64   In this case (d
1ac5f 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30  b->nStatement==0
1ac60 29 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 20  ), and there is 
1ac61 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 2e 0a 20  nothing to do.. 
1ac62 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e 53   */.  if( db->nS
1ac63 74 61 74 65 6d 65 6e 74 20 26 26 20 70 2d 3e 69  tatement && p->i
1ac64 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20  Statement ){.   
1ac65 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 6f 6e 73   int i;.    cons
1ac66 74 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74  t int iSavepoint
1ac67 20 3d 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74   = p->iStatement
1ac68 2d 31 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  -1;..    assert(
1ac69 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f   eOp==SAVEPOINT_
1ac6a 52 4f 4c 4c 42 41 43 4b 20 7c 7c 20 65 4f 70 3d  ROLLBACK || eOp=
1ac6b 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41  =SAVEPOINT_RELEA
1ac6c 53 45 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  SE);.    assert(
1ac6d 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3e   db->nStatement>
1ac6e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
1ac6f 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d   p->iStatement==
1ac70 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b  (db->nStatement+
1ac71 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 20  db->nSavepoint) 
1ac72 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  );..    for(i=0;
1ac73 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
1ac74 7b 20 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32  { .      int rc2
1ac75 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1ac76 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
1ac77 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
1ac78 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29  .      if( pBt )
1ac79 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 4f  {.        if( eO
1ac7a 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  p==SAVEPOINT_ROL
1ac7b 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 20  LBACK ){.       
1ac7c 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33     rc2 = sqlite3
1ac7d 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70  BtreeSavepoint(p
1ac7e 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f  Bt, SAVEPOINT_RO
1ac7f 4c 4c 42 41 43 4b 2c 20 69 53 61 76 65 70 6f 69  LLBACK, iSavepoi
1ac80 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nt);.        }. 
1ac81 20 20 20 20 20 20 20 69 66 28 20 72 63 32 3d 3d         if( rc2==
1ac82 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1ac83 20 20 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c         rc2 = sql
1ac84 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69  ite3BtreeSavepoi
1ac85 6e 74 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e  nt(pBt, SAVEPOIN
1ac86 54 5f 52 45 4c 45 41 53 45 2c 20 69 53 61 76 65  T_RELEASE, iSave
1ac87 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  point);.        
1ac88 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  }.        if( rc
1ac89 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1ac8a 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63           rc = rc
1ac8b 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  2;.        }.   
1ac8c 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 64     }.    }.    d
1ac8d 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2d 2d 3b  b->nStatement--;
1ac8e 0a 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65  .    p->iStateme
1ac8f 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20  nt = 0;..    /* 
1ac90 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  If the statement
1ac91 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1ac92 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63  being rolled bac
1ac93 6b 2c 20 61 6c 73 6f 20 72 65 73 74 6f 72 65 20  k, also restore 
1ac94 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 61  the .    ** data
1ac95 62 61 73 65 20 68 61 6e 64 6c 65 73 20 64 65 66  base handles def
1ac96 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74  erred constraint
1ac97 20 63 6f 75 6e 74 65 72 20 74 6f 20 74 68 65 20   counter to the 
1ac98 76 61 6c 75 65 20 69 74 20 68 61 64 20 77 68 65  value it had whe
1ac99 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74  n .    ** the st
1ac9a 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1ac9b 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20  ion was opened. 
1ac9c 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 4f 70 3d   */.    if( eOp=
1ac9d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
1ac9e 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  ACK ){.      db-
1ac9f 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d  >nDeferredCons =
1aca0 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 6f 6e 73   p->nStmtDefCons
1aca1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1aca2 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1aca3 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63  * If SQLite is c
1aca4 6f 6d 70 69 6c 65 64 20 74 6f 20 73 75 70 70 6f  ompiled to suppo
1aca5 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  rt shared-cache 
1aca6 6d 6f 64 65 20 61 6e 64 20 74 6f 20 62 65 20 74  mode and to be t
1aca7 68 72 65 61 64 73 61 66 65 2c 0a 2a 2a 20 74 68  hreadsafe,.** th
1aca8 69 73 20 72 6f 75 74 69 6e 65 20 6f 62 74 61 69  is routine obtai
1aca9 6e 73 20 74 68 65 20 6d 75 74 65 78 20 61 73 73  ns the mutex ass
1acaa 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63  ociated with eac
1acab 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63  h BtShared struc
1acac 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 6d 61 79  ture.** that may
1acad 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20   be accessed by 
1acae 74 68 65 20 56 4d 20 70 61 73 73 65 64 20 61 73  the VM passed as
1acaf 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 6e   an argument. In
1acb0 20 64 6f 69 6e 67 20 73 6f 20 69 74 0a 2a 2a 20   doing so it.** 
1acb1 73 65 74 73 20 74 68 65 20 42 74 53 68 61 72 65  sets the BtShare
1acb2 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f 66 20 65  d.db member of e
1acb3 61 63 68 20 6f 66 20 74 68 65 20 42 74 53 68 61  ach of the BtSha
1acb4 72 65 64 20 73 74 72 75 63 74 75 72 65 73 2c 20  red structures, 
1acb5 65 6e 73 75 72 69 6e 67 0a 2a 2a 20 74 68 61 74  ensuring.** that
1acb6 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 75 73   the correct bus
1acb7 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61  y-handler callba
1acb8 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 66  ck is invoked if
1acb9 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a   required..**.**
1acba 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 6e 6f   If SQLite is no
1acbb 74 20 74 68 72 65 61 64 73 61 66 65 20 62 75 74  t threadsafe but
1acbc 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20 73 68   does support sh
1acbd 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c  ared-cache mode,
1acbe 20 74 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33   then.** sqlite3
1acbf 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 20  BtreeEnterAll() 
1acc0 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73 65  is invoked to se
1acc1 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 64  t the BtShared.d
1acc2 62 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f  b variables.** o
1acc3 66 20 61 6c 6c 20 6f 66 20 42 74 53 68 61 72 65  f all of BtShare
1acc4 64 20 73 74 72 75 63 74 75 72 65 73 20 61 63 63  d structures acc
1acc5 65 73 73 69 62 6c 65 20 76 69 61 20 74 68 65 20  essible via the 
1acc6 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
1acc7 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77  .** associated w
1acc8 69 74 68 20 74 68 65 20 56 4d 2e 20 4f 66 20 63  ith the VM. Of c
1acc9 6f 75 72 73 65 20 6f 6e 6c 79 20 61 20 73 75 62  ourse only a sub
1acca 73 65 74 20 6f 66 20 74 68 65 73 65 20 73 74 72  set of these str
1accb 75 63 74 75 72 65 73 0a 2a 2a 20 77 69 6c 6c 20  uctures.** will 
1accc 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74  be accessed by t
1accd 68 65 20 56 4d 2c 20 61 6e 64 20 77 65 20 63 6f  he VM, and we co
1acce 75 6c 64 20 75 73 65 20 56 64 62 65 2e 62 74 72  uld use Vdbe.btr
1accf 65 65 4d 61 73 6b 20 74 6f 20 66 69 67 75 72 65  eeMask to figure
1acd0 0a 2a 2a 20 74 68 61 74 20 73 75 62 73 65 74 20  .** that subset 
1acd1 6f 75 74 2c 20 62 75 74 20 74 68 65 72 65 20 69  out, but there i
1acd2 73 20 6e 6f 20 61 64 76 61 6e 74 61 67 65 20 74  s no advantage t
1acd3 6f 20 64 6f 69 6e 67 20 73 6f 2e 0a 2a 2a 0a 2a  o doing so..**.*
1acd4 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 6e  * If SQLite is n
1acd5 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 61 6e  ot threadsafe an
1acd6 64 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f  d does not suppo
1acd7 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  rt shared-cache 
1acd8 6d 6f 64 65 2c 20 74 68 69 73 0a 2a 2a 20 66 75  mode, this.** fu
1acd9 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
1acda 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  p..*/.#ifndef SQ
1acdb 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
1acdc 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52  _CACHE.SQLITE_PR
1acdd 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1acde 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79  e3VdbeMutexArray
1acdf 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29 7b 0a  Enter(Vdbe *p){.
1ace0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
1ace1 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 42  DSAFE.  sqlite3B
1ace2 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e  treeMutexArrayEn
1ace3 74 65 72 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b  ter(&p->aMutex);
1ace4 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33  .#else.  sqlite3
1ace5 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 70 2d  BtreeEnterAll(p-
1ace6 3e 64 62 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 23  >db);.#endif.}.#
1ace7 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
1ace8 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
1ace9 6c 6c 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e  lled when a tran
1acea 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 62  saction opened b
1aceb 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a  y the database .
1acec 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69  ** handle associ
1aced 61 74 65 64 20 77 69 74 68 20 74 68 65 20 56 4d  ated with the VM
1acee 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72   passed as an ar
1acef 67 75 6d 65 6e 74 20 69 73 20 61 62 6f 75 74 20  gument is about 
1acf0 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 6d 6d 69 74  to be .** commit
1acf1 74 65 64 2e 20 49 66 20 74 68 65 72 65 20 61 72  ted. If there ar
1acf2 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65  e outstanding de
1acf3 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b  ferred foreign k
1acf4 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a  ey constraint.**
1acf5 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 72 65 74   violations, ret
1acf6 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
1acf7 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c  . Otherwise, SQL
1acf8 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  ITE_OK..**.** If
1acf9 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74   there are outst
1acfa 61 6e 64 69 6e 67 20 46 4b 20 76 69 6f 6c 61 74  anding FK violat
1acfb 69 6f 6e 73 20 61 6e 64 20 74 68 69 73 20 66 75  ions and this fu
1acfc 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a  nction returns .
1acfd 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c  ** SQLITE_ERROR,
1acfe 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20   set the result 
1acff 6f 66 20 74 68 65 20 56 4d 20 74 6f 20 53 51 4c  of the VM to SQL
1ad00 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61  ITE_CONSTRAINT a
1ad01 6e 64 20 77 72 69 74 65 0a 2a 2a 20 61 6e 20 65  nd write.** an e
1ad02 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20  rror message to 
1ad03 69 74 2e 20 54 68 65 6e 20 72 65 74 75 72 6e 20  it. Then return 
1ad04 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2f  SQLITE_ERROR..*/
1ad05 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ad06 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
1ad07 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1ad08 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
1ad09 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 70 2c 20  heckFk(Vdbe *p, 
1ad0a 69 6e 74 20 64 65 66 65 72 72 65 64 29 7b 0a 20  int deferred){. 
1ad0b 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1ad0c 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 64 65 66  ->db;.  if( (def
1ad0d 65 72 72 65 64 20 26 26 20 64 62 2d 3e 6e 44 65  erred && db->nDe
1ad0e 66 65 72 72 65 64 43 6f 6e 73 3e 30 29 20 7c 7c  ferredCons>0) ||
1ad0f 20 28 21 64 65 66 65 72 72 65 64 20 26 26 20 70   (!deferred && p
1ad10 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3e  ->nFkConstraint>
1ad11 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  0) ){.    p->rc 
1ad12 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  = SQLITE_CONSTRA
1ad13 49 4e 54 3b 0a 20 20 20 20 70 2d 3e 65 72 72 6f  INT;.    p->erro
1ad14 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f  rAction = OE_Abo
1ad15 72 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  rt;.    sqlite3S
1ad16 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1ad17 72 4d 73 67 2c 20 64 62 2c 20 22 66 6f 72 65 69  rMsg, db, "forei
1ad18 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
1ad19 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20  t failed");.    
1ad1a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1ad1b 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ROR;.  }.  retur
1ad1c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
1ad1d 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
1ad1e 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
1ad1f 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61 20 56  led the when a V
1ad20 44 42 45 20 74 72 69 65 73 20 74 6f 20 68 61 6c  DBE tries to hal
1ad21 74 2e 20 20 49 66 20 74 68 65 20 56 44 42 45 0a  t.  If the VDBE.
1ad22 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68 61 6e  ** has made chan
1ad23 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 75  ges and is in au
1ad24 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 74  tocommit mode, t
1ad25 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73 65  hen commit those
1ad26 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 49 66  .** changes.  If
1ad27 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6e   a rollback is n
1ad28 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f 20 74  eeded, then do t
1ad29 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  he rollback..**.
1ad2a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1ad2b 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20  is the only way 
1ad2c 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74 61 74  to move the stat
1ad2d 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d 0a 2a  e of a VM from.*
1ad2e 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 52  * SQLITE_MAGIC_R
1ad2f 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47  UN to SQLITE_MAG
1ad30 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69 73 20  IC_HALT.  It is 
1ad31 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 63  harmless to.** c
1ad32 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20 56 4d  all this on a VM
1ad33 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20   that is in the 
1ad34 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c  SQLITE_MAGIC_HAL
1ad35 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 52  T state..**.** R
1ad36 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
1ad37 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63 6f 6d  ode.  If the com
1ad38 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20 63 6f  mit could not co
1ad39 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65 20 6f  mplete because o
1ad3a 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e  f.** lock conten
1ad3b 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53 51 4c  tion, return SQL
1ad3c 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20 53 51  ITE_BUSY.  If SQ
1ad3d 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74  LITE_BUSY is ret
1ad3e 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d 65 61  urned, it.** mea
1ad3f 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64 69 64  ns the close did
1ad40 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e 64 20   not happen and 
1ad41 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 70 65  needs to be repe
1ad42 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ated..*/.SQLITE_
1ad43 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1ad44 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 65  te3VdbeHalt(Vdbe
1ad45 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20   *p){.  int rc; 
1ad46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad47 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
1ad48 74 6f 20 73 74 6f 72 65 20 74 72 61 6e 73 69 65  to store transie
1ad49 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 65 73 20  nt return codes 
1ad4a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
1ad4b 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20   = p->db;..  /* 
1ad4c 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f  This function co
1ad4d 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 63  ntains the logic
1ad4e 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73   that determines
1ad4f 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   if a statement 
1ad50 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  or.  ** transact
1ad51 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d  ion will be comm
1ad52 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20  itted or rolled 
1ad53 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74  back as a result
1ad54 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 78 65   of the.  ** exe
1ad55 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 76  cution of this v
1ad56 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20  irtual machine. 
1ad57 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6e  .  **.  ** If an
1ad58 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  y of the followi
1ad59 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 72 3a  ng errors occur:
1ad5a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53  .  **.  **     S
1ad5b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a  QLITE_NOMEM.  **
1ad5c 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52       SQLITE_IOER
1ad5d 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54  R.  **     SQLIT
1ad5e 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20 20  E_FULL.  **     
1ad5f 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
1ad60 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20  .  **.  ** Then 
1ad61 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63  the internal cac
1ad62 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65  he might have be
1ad63 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e  en left in an in
1ad64 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a 20  consistent.  ** 
1ad65 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 64 20  state.  We need 
1ad66 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  to rollback the 
1ad67 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1ad68 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 65 20  ction, if there 
1ad69 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 20  is.  ** one, or 
1ad6a 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 72 61  the complete tra
1ad6b 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 72  nsaction if ther
1ad6c 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e  e is no statemen
1ad6d 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20  t transaction.. 
1ad6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64 62   */..  if( p->db
1ad6f 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1ad70 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  {.    p->rc = SQ
1ad71 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
1ad72 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72    closeAllCursor
1ad73 73 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d  s(p);.  if( p->m
1ad74 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43  agic!=VDBE_MAGIC
1ad75 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75  _RUN ){.    retu
1ad76 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1ad77 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 56  }.  checkActiveV
1ad78 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20 20 2f  dbeCnt(db);..  /
1ad79 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72 20 72  * No commit or r
1ad7a 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 64 20 69  ollback needed i
1ad7b 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 6e 65  f the program ne
1ad7c 76 65 72 20 73 74 61 72 74 65 64 20 2a 2f 0a 20  ver started */. 
1ad7d 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b   if( p->pc>=0 ){
1ad7e 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b 20 20 20  .    int mrc;   
1ad7f 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 72 6f 72  /* Primary error
1ad80 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d 3e 72 63   code from p->rc
1ad81 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 53 74 61   */.    int eSta
1ad82 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b 0a 20 20  tementOp = 0;.  
1ad83 20 20 69 6e 74 20 69 73 53 70 65 63 69 61 6c 45    int isSpecialE
1ad84 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20  rror;           
1ad85 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20   /* Set to true 
1ad86 69 66 20 61 20 27 73 70 65 63 69 61 6c 27 20 65  if a 'special' e
1ad87 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  rror */..    /* 
1ad88 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65 73 20  Lock all btrees 
1ad89 75 73 65 64 20 62 79 20 74 68 65 20 73 74 61 74  used by the stat
1ad8a 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71 6c  ement */.    sql
1ad8b 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72  ite3VdbeMutexArr
1ad8c 61 79 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20 20  ayEnter(p);..   
1ad8d 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6f 6e   /* Check for on
1ad8e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c  e of the special
1ad8f 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 6d   errors */.    m
1ad90 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30 78 66  rc = p->rc & 0xf
1ad91 66 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  f;.    assert( p
1ad92 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45  ->rc!=SQLITE_IOE
1ad93 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 20 20 2f  RR_BLOCKED );  /
1ad94 2a 20 54 68 69 73 20 65 72 72 6f 72 20 6e 6f 20  * This error no 
1ad95 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 20 2a 2f  longer exists */
1ad96 0a 20 20 20 20 69 73 53 70 65 63 69 61 6c 45 72  .    isSpecialEr
1ad97 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51 4c 49 54  ror = mrc==SQLIT
1ad98 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d  E_NOMEM || mrc==
1ad99 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 20  SQLITE_IOERR.   
1ad9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad9b 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45    || mrc==SQLITE
1ad9c 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c 20 6d 72  _INTERRUPT || mr
1ad9d 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a  c==SQLITE_FULL;.
1ad9e 20 20 20 20 69 66 28 20 69 73 53 70 65 63 69 61      if( isSpecia
1ad9f 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20  lError ){.      
1ada0 2f 2a 20 49 66 20 74 68 65 20 71 75 65 72 79 20  /* If the query 
1ada1 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 77  was read-only, w
1ada2 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20 72 6f 6c  e need do no rol
1ada3 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e 20 4f 74  lback at all. Ot
1ada4 68 65 72 77 69 73 65 2c 0a 20 20 20 20 20 20 2a  herwise,.      *
1ada5 2a 20 70 72 6f 63 65 65 64 20 77 69 74 68 20 74  * proceed with t
1ada6 68 65 20 73 70 65 63 69 61 6c 20 68 61 6e 64 6c  he special handl
1ada7 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ing..      */.  
1ada8 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61 64      if( !p->read
1ada9 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d 53 51 4c  Only || mrc!=SQL
1adaa 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 29 7b  ITE_INTERRUPT ){
1adab 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6d 72  .        if( (mr
1adac 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
1adad 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46  || mrc==SQLITE_F
1adae 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73 65 73 53  ULL) && p->usesS
1adaf 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20  tmtJournal ){.  
1adb0 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65          eStateme
1adb1 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54  ntOp = SAVEPOINT
1adb2 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 20  _ROLLBACK;.     
1adb3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1adb4 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 66 6f      /* We are fo
1adb5 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 62 61 63  rced to roll bac
1adb6 6b 20 74 68 65 20 61 63 74 69 76 65 20 74 72 61  k the active tra
1adb7 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 6f 72 65  nsaction. Before
1adb8 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 20 20 20   doing.         
1adb9 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 20 61 6e   ** so, abort an
1adba 79 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e  y other statemen
1adbb 74 73 20 74 68 69 73 20 68 61 6e 64 6c 65 20 63  ts this handle c
1adbc 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61 63 74  urrently has act
1adbd 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ive..          *
1adbe 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e 76 61  /.          inva
1adbf 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d  lidateCursorsOnM
1adc0 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 64 62  odifiedBtrees(db
1adc1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1adc2 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28  ite3RollbackAll(
1adc3 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  db);.          s
1adc4 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70  qlite3CloseSavep
1adc5 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 20  oints(db);.     
1adc6 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d       db->autoCom
1adc7 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  mit = 1;.       
1adc8 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1adc9 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66  ..    /* Check f
1adca 6f 72 20 69 6d 6d 65 64 69 61 74 65 20 66 6f 72  or immediate for
1adcb 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69  eign key violati
1adcc 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ons. */.    if( 
1adcd 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  p->rc==SQLITE_OK
1adce 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1adcf 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 20  3VdbeCheckFk(p, 
1add0 30 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20  0);.    }.  .   
1add1 20 2f 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d   /* If the auto-
1add2 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73  commit flag is s
1add3 65 74 20 61 6e 64 20 74 68 69 73 20 69 73 20 74  et and this is t
1add4 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 77  he only active w
1add5 72 69 74 65 72 20 0a 20 20 20 20 2a 2a 20 56 4d  riter .    ** VM
1add6 2c 20 74 68 65 6e 20 77 65 20 64 6f 20 65 69 74  , then we do eit
1add7 68 65 72 20 61 20 63 6f 6d 6d 69 74 20 6f 72 20  her a commit or 
1add8 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 20  rollback of the 
1add9 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
1adda 69 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20  ion. .    **.   
1addb 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 73 20 62   ** Note: This b
1addc 6c 6f 63 6b 20 61 6c 73 6f 20 72 75 6e 73 20 69  lock also runs i
1addd 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65  f one of the spe
1adde 63 69 61 6c 20 65 72 72 6f 72 73 20 68 61 6e 64  cial errors hand
1addf 6c 65 64 20 0a 20 20 20 20 2a 2a 20 61 62 6f 76  led .    ** abov
1ade0 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20  e has occurred. 
1ade1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1ade2 21 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79  !sqlite3VtabInSy
1ade3 6e 63 28 64 62 29 20 0a 20 20 20 20 20 26 26 20  nc(db) .     && 
1ade4 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 0a  db->autoCommit .
1ade5 20 20 20 20 20 26 26 20 64 62 2d 3e 77 72 69 74       && db->writ
1ade6 65 56 64 62 65 43 6e 74 3d 3d 28 70 2d 3e 72 65  eVdbeCnt==(p->re
1ade7 61 64 4f 6e 6c 79 3d 3d 30 29 20 0a 20 20 20 20  adOnly==0) .    
1ade8 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  ){.      if( p->
1ade9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
1adea 20 28 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e   (p->errorAction
1adeb 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 20 21 69 73  ==OE_Fail && !is
1adec 53 70 65 63 69 61 6c 45 72 72 6f 72 29 20 29 7b  SpecialError) ){
1aded 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
1adee 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28  ite3VdbeCheckFk(
1adef 70 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 20  p, 1) ){.       
1adf0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d     sqlite3BtreeM
1adf1 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26  utexArrayLeave(&
1adf2 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20 20  p->aMutex);.    
1adf3 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1adf4 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
1adf5 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
1adf6 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  The auto-commit 
1adf7 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68  flag is true, th
1adf8 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77  e vdbe program w
1adf9 61 73 20 73 75 63 63 65 73 73 66 75 6c 20 0a 20  as successful . 
1adfa 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 68 69 74         ** or hit
1adfb 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 20 63 6f   an 'OR FAIL' co
1adfc 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74 68 65  nstraint and the
1adfd 72 65 20 61 72 65 20 6e 6f 20 64 65 66 65 72 72  re are no deferr
1adfe 65 64 20 66 6f 72 65 69 67 6e 0a 20 20 20 20 20  ed foreign.     
1adff 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6e 73 74 72     ** key constr
1ae00 61 69 6e 74 73 20 74 6f 20 68 6f 6c 64 20 75 70  aints to hold up
1ae01 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1ae02 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 61 20 63  . This means a c
1ae03 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 20 20 2a  ommit .        *
1ae04 2a 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 20  * is required.  
1ae05 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  */.        rc = 
1ae06 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c 20 70  vdbeCommit(db, p
1ae07 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1ae08 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29  c==SQLITE_BUSY )
1ae09 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
1ae0a 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72  te3BtreeMutexArr
1ae0b 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 74  ayLeave(&p->aMut
1ae0c 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  ex);.          r
1ae0d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
1ae0e 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  Y;.        }else
1ae0f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1ae10 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1ae11 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20  p->rc = rc;.    
1ae12 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c        sqlite3Rol
1ae13 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20  lbackAll(db);.  
1ae14 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1ae15 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65         db->nDefe
1ae16 72 72 65 64 43 6f 6e 73 20 3d 20 30 3b 0a 20 20  rredCons = 0;.  
1ae17 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
1ae18 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61  ommitInternalCha
1ae19 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20  nges(db);.      
1ae1a 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
1ae1b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ae1c 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b  RollbackAll(db);
1ae1d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64  .      }.      d
1ae1e 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d 20  b->nStatement = 
1ae1f 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  0;.    }else if(
1ae20 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d 3d 30   eStatementOp==0
1ae21 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d   ){.      if( p-
1ae22 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  >rc==SQLITE_OK |
1ae23 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e  | p->errorAction
1ae24 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 20  ==OE_Fail ){.   
1ae25 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f       eStatementO
1ae26 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 45  p = SAVEPOINT_RE
1ae27 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c  LEASE;.      }el
1ae28 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 41  se if( p->errorA
1ae29 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 20  ction==OE_Abort 
1ae2a 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 61 74  ){.        eStat
1ae2b 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f  ementOp = SAVEPO
1ae2c 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20  INT_ROLLBACK;.  
1ae2d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ae2e 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72     invalidateCur
1ae2f 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74  sorsOnModifiedBt
1ae30 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20  rees(db);.      
1ae31 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63    sqlite3Rollbac
1ae32 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20  kAll(db);.      
1ae33 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61    sqlite3CloseSa
1ae34 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20  vepoints(db);.  
1ae35 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f        db->autoCo
1ae36 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  mmit = 1;.      
1ae37 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
1ae38 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e 74 4f  * If eStatementO
1ae39 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  p is non-zero, t
1ae3a 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20  hen a statement 
1ae3b 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64  transaction need
1ae3c 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 63  s to.    ** be c
1ae3d 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c  ommitted or roll
1ae3e 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c 20 73 71  ed back. Call sq
1ae3f 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74  lite3VdbeCloseSt
1ae40 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a 20 20 20  atement() to.   
1ae41 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 20 74 68   ** do so. If th
1ae42 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 72 65 74  is operation ret
1ae43 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2c 20 61  urns an error, a
1ae44 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  nd the current s
1ae45 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20  tatement.    ** 
1ae46 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 53 51  error code is SQ
1ae47 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54  LITE_OK or SQLIT
1ae48 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 74 68  E_CONSTRAINT, th
1ae49 65 6e 20 73 65 74 20 74 68 65 20 65 72 72 6f 72  en set the error
1ae4a 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f 20  .    ** code to 
1ae4b 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e 0a 20  the new value.. 
1ae4c 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 53     */.    if( eS
1ae4d 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b 0a 20 20  tatementOp ){.  
1ae4e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ae4f 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65  VdbeCloseStateme
1ae50 6e 74 28 70 2c 20 65 53 74 61 74 65 6d 65 6e 74  nt(p, eStatement
1ae51 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  Op);.      if( r
1ae52 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51 4c  c && (p->rc==SQL
1ae53 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d  ITE_OK || p->rc=
1ae54 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49  =SQLITE_CONSTRAI
1ae55 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  NT) ){.        p
1ae56 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20  ->rc = rc;.     
1ae57 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1ae58 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1ae59 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 72  ;.        p->zEr
1ae5a 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  rMsg = 0;.      
1ae5b 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
1ae5c 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 61 6e  * If this was an
1ae5d 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20   INSERT, UPDATE 
1ae5e 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20 6e 6f  or DELETE and no
1ae5f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
1ae60 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 61  action.    ** ha
1ae61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61  s been rolled ba
1ae62 63 6b 2c 20 75 70 64 61 74 65 20 74 68 65 20 64  ck, update the d
1ae63 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1ae64 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  on change-counte
1ae65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  r. .    */.    i
1ae66 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f  f( p->changeCntO
1ae67 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65  n ){.      if( e
1ae68 53 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56  StatementOp!=SAV
1ae69 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1ae6a 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1ae6b 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73  e3VdbeSetChanges
1ae6c 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29  (db, p->nChange)
1ae6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1ae6e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1ae6f 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c  beSetChanges(db,
1ae70 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
1ae71 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20     p->nChange = 
1ae72 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  0;.    }.  .    
1ae73 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63  /* Rollback or c
1ae74 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61  ommit any schema
1ae75 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63   changes that oc
1ae76 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69  curred. */.    i
1ae77 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45  f( p->rc!=SQLITE
1ae78 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73  _OK && db->flags
1ae79 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  &SQLITE_InternCh
1ae7a 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73  anges ){.      s
1ae7b 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
1ae7c 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
1ae7d 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67  ;.      db->flag
1ae7e 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c  s = (db->flags |
1ae7f 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
1ae80 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20  anges);.    }.. 
1ae81 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68     /* Release th
1ae82 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73  e locks */.    s
1ae83 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
1ae84 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61  ArrayLeave(&p->a
1ae85 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f  Mutex);.  }..  /
1ae86 2a 20 57 65 20 68 61 76 65 20 73 75 63 63 65 73  * We have succes
1ae87 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e  sfully halted an
1ae88 64 20 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e  d closed the VM.
1ae89 20 20 52 65 63 6f 72 64 20 74 68 69 73 20 66 61    Record this fa
1ae8a 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ct. */.  if( p->
1ae8b 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d  pc>=0 ){.    db-
1ae8c 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d  >activeVdbeCnt--
1ae8d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65  ;.    if( !p->re
1ae8e 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20  adOnly ){.      
1ae8f 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74  db->writeVdbeCnt
1ae90 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  --;.    }.    as
1ae91 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65  sert( db->active
1ae92 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69  VdbeCnt>=db->wri
1ae93 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d  teVdbeCnt );.  }
1ae94 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44  .  p->magic = VD
1ae95 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20  BE_MAGIC_HALT;. 
1ae96 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65   checkActiveVdbe
1ae97 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70  Cnt(db);.  if( p
1ae98 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1ae99 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  ed ){.    p->rc 
1ae9a 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1ae9b 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
1ae9c 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61   auto-commit fla
1ae9d 67 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65  g is set to true
1ae9e 2c 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73  , then any locks
1ae9f 20 74 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a   that were held.
1aea0 20 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69    ** by connecti
1aea1 6f 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62  on db have now b
1aea2 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61  een released. Ca
1aea3 6c 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63  ll sqlite3Connec
1aea4 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a  tionUnlocked() .
1aea5 20 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61    ** to invoke a
1aea6 6e 79 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f  ny required unlo
1aea7 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
1aea8 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  cks..  */.  if( 
1aea9 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
1aeaa 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e  {.    sqlite3Con
1aeab 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28  nectionUnlocked(
1aeac 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  db);.  }..  asse
1aead 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64  rt( db->activeVd
1aeae 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61  beCnt>0 || db->a
1aeaf 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20  utoCommit==0 || 
1aeb0 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d  db->nStatement==
1aeb1 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  0 );.  return SQ
1aeb2 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
1aeb3 2a 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c  ** Each VDBE hol
1aeb4 64 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  ds the result of
1aeb5 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
1aeb6 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
1aeb7 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63  call.** in p->rc
1aeb8 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
1aeb9 73 65 74 73 20 74 68 61 74 20 72 65 73 75 6c 74  sets that result
1aeba 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f   back to SQLITE_
1aebb 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  OK..*/.SQLITE_PR
1aebc 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1aebd 65 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52  e3VdbeResetStepR
1aebe 65 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a  esult(Vdbe *p){.
1aebf 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45    p->rc = SQLITE
1aec0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
1aec1 65 61 6e 20 75 70 20 61 20 56 44 42 45 20 61 66  ean up a VDBE af
1aec2 74 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75  ter execution bu
1aec3 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20  t do not delete 
1aec4 74 68 65 20 56 44 42 45 20 6a 75 73 74 20 79 65  the VDBE just ye
1aec5 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20  t..** Write any 
1aec6 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69  error messages i
1aec7 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20  nto *pzErrMsg.  
1aec8 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c  Return the resul
1aec9 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66  t code..**.** Af
1aeca 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
1aecb 20 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42   is run, the VDB
1aecc 45 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64  E should be read
1aecd 79 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64  y to be executed
1aece 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a  .** again..**.**
1aecf 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61   To look at it a
1aed0 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 73  nother way, this
1aed1 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20   routine resets 
1aed2 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65  the state of the
1aed3 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68  .** virtual mach
1aed4 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41  ine from VDBE_MA
1aed5 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f  GIC_RUN or VDBE_
1aed6 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20  MAGIC_HALT back 
1aed7 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43  to.** VDBE_MAGIC
1aed8 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45  _INIT..*/.SQLITE
1aed9 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1aeda 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64  ite3VdbeReset(Vd
1aedb 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  be *p){.  sqlite
1aedc 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d  3 *db;.  db = p-
1aedd 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  >db;..  /* If th
1aede 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e  e VM did not run
1aedf 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f   to completion o
1aee0 72 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65  r if it encounte
1aee1 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f  red an.  ** erro
1aee2 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74  r, then it might
1aee3 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68   not have been h
1aee4 61 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20  alted properly. 
1aee5 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74   So halt.  ** it
1aee6 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f   now..  */.  (vo
1aee7 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
1aee8 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65  On(db);.  sqlite
1aee9 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20  3VdbeHalt(p);.  
1aeea 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1aeeb 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f  etyOff(db);..  /
1aeec 2a 20 49 66 20 74 68 65 20 56 44 42 45 20 68 61  * If the VDBE ha
1aeed 73 20 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61  s be run even pa
1aeee 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72  rtially, then tr
1aeef 61 6e 73 66 65 72 20 74 68 65 20 65 72 72 6f 72  ansfer the error
1aef0 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65   code.  ** and e
1aef1 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f  rror message fro
1aef2 6d 20 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20  m the VDBE into 
1aef3 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1aef4 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 42 75  e structure.  Bu
1aef5 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44  t.  ** if the VD
1aef6 42 45 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  BE has just been
1aef7 20 73 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20   set to run but 
1aef8 68 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79  has not actually
1aef9 20 65 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20   executed any.  
1aefa 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  ** instructions 
1aefb 79 65 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d  yet, leave the m
1aefc 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 72 72  ain database err
1aefd 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75  or information u
1aefe 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20  nchanged..  */. 
1aeff 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b   if( p->pc>=0 ){
1af00 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72  .    if( p->zErr
1af01 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Msg ){.      sql
1af02 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
1af03 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73  alloc();.      s
1af04 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74  qlite3ValueSetSt
1af05 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d  r(db->pErr,-1,p-
1af06 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f  >zErrMsg,SQLITE_
1af07 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e  UTF8,SQLITE_TRAN
1af08 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71  SIENT);.      sq
1af09 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
1af0a 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62  lloc();.      db
1af0b 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72  ->errCode = p->r
1af0c 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
1af0d 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1af0e 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d  rrMsg);.      p-
1af0f 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1af10 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72    }else if( p->r
1af11 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  c ){.      sqlit
1af12 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72  e3Error(db, p->r
1af13 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  c, 0);.    }else
1af14 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1af15 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
1af16 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  OK, 0);.    }.  
1af17 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20  }else if( p->rc 
1af18 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b  && p->expired ){
1af19 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69  .    /* The expi
1af1a 72 65 64 20 66 6c 61 67 20 77 61 73 20 73 65 74  red flag was set
1af1b 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65 66   on the VDBE bef
1af1c 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 63 61  ore the first ca
1af1d 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c  ll.    ** to sql
1af1e 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72  ite3_step(). For
1af1f 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69   consistency (si
1af20 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  nce sqlite3_step
1af21 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61  () was.    ** ca
1af22 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20 64  lled), set the d
1af23 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e  atabase error in
1af24 20 74 68 69 73 20 63 61 73 65 20 61 73 20 77 65   this case as we
1af25 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ll..    */.    s
1af26 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
1af27 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73  p->rc, 0);.    s
1af28 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74  qlite3ValueSetSt
1af29 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20  r(db->pErr, -1, 
1af2a 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49  p->zErrMsg, SQLI
1af2b 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
1af2c 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
1af2d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1af2e 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , p->zErrMsg);. 
1af2f 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20     p->zErrMsg = 
1af30 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63  0;.  }..  /* Rec
1af31 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  laim all memory 
1af32 75 73 65 64 20 62 79 20 74 68 65 20 56 44 42 45  used by the VDBE
1af33 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28  .  */.  Cleanup(
1af34 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70  p);..  /* Save p
1af35 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61  rofiling informa
1af36 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56  tion from this V
1af37 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69  DBE run..  */.#i
1af38 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c  fdef VDBE_PROFIL
1af39 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a  E.  {.    FILE *
1af3a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62  out = fopen("vdb
1af3b 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20  e_profile.out", 
1af3c 22 61 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75  "a");.    if( ou
1af3d 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  t ){.      int i
1af3e 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  ;.      fprintf(
1af3f 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20  out, "---- ");. 
1af40 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
1af41 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20  p->nOp; i++){.  
1af42 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
1af43 74 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f  t, "%02x", p->aO
1af44 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20  p[i].opcode);.  
1af45 20 20 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69      }.      fpri
1af46 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a  ntf(out, "\n");.
1af47 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
1af48 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20  <p->nOp; i++){. 
1af49 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f         fprintf(o
1af4a 75 74 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20  ut, "%6d %10lld 
1af4b 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20  %8lld ",.       
1af4c 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e      p->aOp[i].cn
1af4d 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d  t,.           p-
1af4e 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a  >aOp[i].cycles,.
1af4f 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f             p->aO
1af50 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e  p[i].cnt>0 ? p->
1af51 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d  aOp[i].cycles/p-
1af52 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a  >aOp[i].cnt : 0.
1af53 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
1af54 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72     sqlite3VdbePr
1af55 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70  intOp(out, i, &p
1af56 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20  ->aOp[i]);.     
1af57 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28   }.      fclose(
1af58 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  out);.    }.  }.
1af59 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69  #endif.  p->magi
1af5a 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49  c = VDBE_MAGIC_I
1af5b 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d  NIT;.  return p-
1af5c 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73  >rc & db->errMas
1af5d 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65  k;.}. ./*.** Cle
1af5e 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65  an up and delete
1af5f 20 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78   a VDBE after ex
1af60 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e  ecution.  Return
1af61 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63   an integer whic
1af62 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75  h is.** the resu
1af63 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20  lt code.  Write 
1af64 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67  any error messag
1af65 65 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45  e text into *pzE
1af66 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rrMsg..*/.SQLITE
1af67 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1af68 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
1af69 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74  (Vdbe *p){.  int
1af6a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1af6b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d  .  if( p->magic=
1af6c 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20  =VDBE_MAGIC_RUN 
1af6d 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  || p->magic==VDB
1af6e 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a  E_MAGIC_HALT ){.
1af6f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1af70 56 64 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20  VdbeReset(p);.  
1af71 20 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20    assert( (rc & 
1af72 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d  p->db->errMask)=
1af73 3d 72 63 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  =rc );.  }.  sql
1af74 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 70  ite3VdbeDelete(p
1af75 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1af76 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68  }../*.** Call th
1af77 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72  e destructor for
1af78 20 65 61 63 68 20 61 75 78 64 61 74 61 20 65 6e   each auxdata en
1af79 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e 63  try in pVdbeFunc
1af7a 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74 68   for which.** th
1af7b 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
1af7c 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20 63  bit in mask is c
1af7d 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 20 65  lear.  Auxdata e
1af7e 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33 31  ntries beyond 31
1af7f 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 20 64  .** are always d
1af80 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 64 65  estroyed.  To de
1af81 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 61 74  stroy all auxdat
1af82 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c 20  a entries, call 
1af83 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
1af84 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a 2f  with mask==0..*/
1af85 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1af86 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1af87 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64  DeleteAuxData(Vd
1af88 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e  beFunc *pVdbeFun
1af89 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20  c, int mask){.  
1af8a 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30  int i;.  for(i=0
1af8b 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e 6e  ; i<pVdbeFunc->n
1af8c 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  Aux; i++){.    s
1af8d 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a 70  truct AuxData *p
1af8e 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 6e 63  Aux = &pVdbeFunc
1af8f 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20 20  ->apAux[i];.    
1af90 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 28 6d  if( (i>31 || !(m
1af91 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c 3c 69  ask&(((u32)1)<<i
1af92 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70 41 75  ))) && pAux->pAu
1af93 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  x ){.      if( p
1af94 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a  Aux->xDelete ){.
1af95 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e 78 44          pAux->xD
1af96 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 75 78  elete(pAux->pAux
1af97 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1af98 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b   pAux->pAux = 0;
1af99 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
1af9a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e  .** Delete an en
1af9b 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51  tire VDBE..*/.SQ
1af9c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1af9d 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c  d sqlite3VdbeDel
1af9e 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ete(Vdbe *p){.  
1af9f 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20  sqlite3 *db;..  
1afa0 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20  if( NEVER(p==0) 
1afa1 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d  ) return;.  db =
1afa2 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d   p->db;.  if( p-
1afa3 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70 2d  >pPrev ){.    p-
1afa4 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
1afa5 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73  p->pNext;.  }els
1afa6 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  e{.    assert( d
1afa7 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20  b->pVdbe==p );. 
1afa8 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70     db->pVdbe = p
1afa9 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  ->pNext;.  }.  i
1afaa 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  f( p->pNext ){. 
1afab 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72     p->pNext->pPr
1afac 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20  ev = p->pPrev;. 
1afad 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41   }.  releaseMemA
1afae 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20 70 2d  rray(p->aVar, p-
1afaf 3e 6e 56 61 72 29 3b 0a 20 20 72 65 6c 65 61 73  >nVar);.  releas
1afb0 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f  eMemArray(p->aCo
1afb1 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f  lName, p->nResCo
1afb2 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b  lumn*COLNAME_N);
1afb3 0a 20 20 76 64 62 65 46 72 65 65 4f 70 41 72 72  .  vdbeFreeOpArr
1afb4 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 70  ay(db, p->aOp, p
1afb5 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c 69 74 65  ->nOp);.  sqlite
1afb6 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
1afb7 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c 69 74 65  Label);.  sqlite
1afb8 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
1afb9 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  ColName);.  sqli
1afba 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1afbb 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67  >zSql);.  p->mag
1afbc 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f  ic = VDBE_MAGIC_
1afbd 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 65 33 44  DEAD;.  sqlite3D
1afbe 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 72  bFree(db, p->pFr
1afbf 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  ee);.  sqlite3Db
1afc0 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a  Free(db, p);.}..
1afc1 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
1afc2 74 68 65 20 63 75 72 73 6f 72 20 70 20 69 73 20  the cursor p is 
1afc3 72 65 61 64 79 20 74 6f 20 72 65 61 64 20 6f 72  ready to read or
1afc4 20 77 72 69 74 65 20 74 68 65 20 72 6f 77 20 74   write the row t
1afc5 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a 20 77 61  o which it.** wa
1afc6 73 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 65  s last positione
1afc7 64 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72  d.  Return an er
1afc8 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 4f  ror code if an O
1afc9 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49 2f 4f 20  OM fault or I/O 
1afca 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76 65 6e 74  error.** prevent
1afcb 73 20 75 73 20 66 72 6f 6d 20 70 6f 73 69 74 69  s us from positi
1afcc 6f 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72  oning the cursor
1afcd 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20   to its correct 
1afce 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  position..**.** 
1afcf 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72  If a MoveTo oper
1afd0 61 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67  ation is pending
1afd1 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75   on the given cu
1afd2 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68  rsor, then do th
1afd3 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77  at.** MoveTo now
1afd4 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65 20 69 73  .  If no move is
1afd5 20 70 65 6e 64 69 6e 67 2c 20 63 68 65 63 6b 20   pending, check 
1afd6 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 72 6f  to see if the ro
1afd7 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 64 65  w has been.** de
1afd8 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75  leted out from u
1afd9 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f 72 20  nder the cursor 
1afda 61 6e 64 20 69 66 20 69 74 20 68 61 73 2c 20 6d  and if it has, m
1afdb 61 72 6b 20 74 68 65 20 72 6f 77 20 61 73 0a 2a  ark the row as.*
1afdc 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a 2a 2a  * a NULL row..**
1afdd 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f  .** If the curso
1afde 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 69  r is already poi
1afdf 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 72  nting to the cor
1afe0 72 65 63 74 20 72 6f 77 20 61 6e 64 20 74 68 61  rect row and tha
1afe1 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20 6e 6f 74  t row has.** not
1afe2 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 6f 75   been deleted ou
1afe3 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65  t from under the
1afe4 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 74 68   cursor, then th
1afe5 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
1afe6 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45  no-op..*/.SQLITE
1afe7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1afe8 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f  ite3VdbeCursorMo
1afe9 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20  veto(VdbeCursor 
1afea 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65  *p){.  if( p->de
1afeb 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a  ferredMoveto ){.
1afec 20 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b      int res, rc;
1afed 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1afee 45 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69  EST.    extern i
1afef 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63  nt sqlite3_searc
1aff0 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a  h_count;.#endif.
1aff1 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69      assert( p->i
1aff2 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63  sTable );.    rc
1aff3 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
1aff4 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d  ovetoUnpacked(p-
1aff5 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e  >pCursor, 0, p->
1aff6 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c  movetoTarget, 0,
1aff7 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20   &res);.    if( 
1aff8 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1aff9 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64      p->lastRowid
1affa 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67   = p->movetoTarg
1affb 65 74 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64  et;.    p->rowid
1affc 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 53  IsValid = ALWAYS
1affd 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20  (res==0) ?1:0;. 
1affe 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 65 73     if( NEVER(res
1afff 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  <0) ){.      rc 
1b000 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  = sqlite3BtreeNe
1b001 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26  xt(p->pCursor, &
1b002 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20  res);.      if( 
1b003 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1b004 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c      }.#ifdef SQL
1b005 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c  ITE_TEST.    sql
1b006 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e  ite3_search_coun
1b007 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  t++;.#endif.    
1b008 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  p->deferredMovet
1b009 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61  o = 0;.    p->ca
1b00a 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1b00b 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65  E_STALE;.  }else
1b00c 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d 3e 70   if( ALWAYS(p->p
1b00d 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 69  Cursor) ){.    i
1b00e 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20 20 20  nt hasMoved;.   
1b00f 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
1b010 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d  3BtreeCursorHasM
1b011 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c  oved(p->pCursor,
1b012 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20 20 20   &hasMoved);.   
1b013 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
1b014 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68 61 73   rc;.    if( has
1b015 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20 20 70  Moved ){.      p
1b016 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1b017 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20  CACHE_STALE;.   
1b018 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20     p->nullRow = 
1b019 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
1b01a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1b01b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
1b01c 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llowing function
1b01d 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33  s:.**.** sqlite3
1b01e 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 29  VdbeSerialType()
1b01f 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53  .** sqlite3VdbeS
1b020 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a 2a  erialTypeLen().*
1b021 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  * sqlite3VdbeSer
1b022 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69  ialLen().** sqli
1b023 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74  te3VdbeSerialPut
1b024 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  ().** sqlite3Vdb
1b025 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a 2a 0a  eSerialGet().**.
1b026 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20 74  ** encapsulate t
1b027 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65 72  he code that ser
1b028 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20 66  ializes values f
1b029 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53 51  or storage in SQ
1b02a 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e 64  Lite.** data and
1b02b 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e 20   index records. 
1b02c 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64 20  Each serialized 
1b02d 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20 6f  value consists o
1b02e 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d 74  f a.** 'serial-t
1b02f 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f 62 20  ype' and a blob 
1b030 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 65 72  of data. The ser
1b031 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e 20 38  ial type is an 8
1b032 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 0a 2a  -byte unsigned.*
1b033 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f 72 65  * integer, store
1b034 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e 0a 2a  d as a varint..*
1b035 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69 74  *.** In an SQLit
1b036 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2c 20  e index record, 
1b037 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20  the serial type 
1b038 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 63 74  is stored direct
1b039 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65  ly before.** the
1b03a 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 74 68   blob of data th
1b03b 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f 6e 64  at it correspond
1b03c 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c 65  s to. In a table
1b03d 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65 72   record, all ser
1b03e 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72 65  ial.** types are
1b03f 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 73   stored at the s
1b040 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f  tart of the reco
1b041 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f 62  rd, and the blob
1b042 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a 20  s of data at.** 
1b043 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 20 74  the end. Hence t
1b044 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61  hese functions a
1b045 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72 20  llow the caller 
1b046 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a  to handle the.**
1b047 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 6e 64   serial-type and
1b048 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 65 72   data blob seper
1b049 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ately..**.** The
1b04a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65   following table
1b04b 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 76   describes the v
1b04c 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 20 63  arious storage c
1b04d 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 61 3a  lasses for data:
1b04e 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c 20  .**.**   serial 
1b04f 74 79 70 65 20 20 20 20 20 20 20 20 62 79 74 65  type        byte
1b050 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 20 74  s of data      t
1b051 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ype.**   -------
1b052 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d  -------     ----
1b053 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d  -----------    -
1b054 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
1b055 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20  *      0        
1b056 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20               0  
1b057 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a 2a            NULL.*
1b058 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20  *      1        
1b059 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20 20               1  
1b05a 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64            signed
1b05b 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20   integer.**     
1b05c 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20   2              
1b05d 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20 20         2        
1b05e 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67      signed integ
1b05f 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 20  er.**      3    
1b060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b061 20 33 20 20 20 20 20 20 20 20 20 20 20 20 73 69   3            si
1b062 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20  gned integer.** 
1b063 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20 20       4          
1b064 20 20 20 20 20 20 20 20 20 20 20 34 20 20 20 20             4    
1b065 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69          signed i
1b066 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 35  nteger.**      5
1b067 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b068 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20 20       6          
1b069 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72    signed integer
1b06a 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 20 20  .**      6      
1b06b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
1b06c 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e              sign
1b06d 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  ed integer.**   
1b06e 20 20 20 37 20 20 20 20 20 20 20 20 20 20 20 20     7            
1b06f 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20 20           8      
1b070 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f 61 74        IEEE float
1b071 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 20  .**      8      
1b072 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
1b073 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65              Inte
1b074 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a 2a  ger constant 0.*
1b075 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 20 20  *      9        
1b076 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20               0  
1b077 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67 65            Intege
1b078 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a 20  r constant 1.** 
1b079 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 20 20      10,11       
1b07a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b07b 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65 64          reserved
1b07c 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a 2a   for expansion.*
1b07d 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20 65  *    N>=12 and e
1b07e 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 32 29  ven       (N-12)
1b07f 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 0a 2a  /2        BLOB.*
1b080 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20 6f  *    N>=13 and o
1b081 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 33 29  dd        (N-13)
1b082 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 0a 2a  /2        text.*
1b083 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 20 39  *.** The 8 and 9
1b084 20 74 79 70 65 73 20 77 65 72 65 20 61 64 64 65   types were adde
1b085 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c 65  d in 3.3.0, file
1b086 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 69 6f   format 4.  Prio
1b087 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66  r versions.** of
1b088 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74   SQLite will not
1b089 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 6f 73   understand thos
1b08a 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e 0a  e serial types..
1b08b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
1b08c 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 70 65   the serial-type
1b08d 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 73   for the value s
1b08e 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a 2a  tored in pMem..*
1b08f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b090 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65   u32 sqlite3Vdbe
1b091 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 20 2a  SerialType(Mem *
1b092 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66  pMem, int file_f
1b093 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66 6c  ormat){.  int fl
1b094 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67  ags = pMem->flag
1b095 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69  s;.  int n;..  i
1b096 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c  f( flags&MEM_Nul
1b097 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
1b098 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61  0;.  }.  if( fla
1b099 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20  gs&MEM_Int ){.  
1b09a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
1b09b 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 20 31  whether to use 1
1b09c 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 20 62  , 2, 4, 6 or 8 b
1b09d 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65 66  ytes. */.#   def
1b09e 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 28 28  ine MAX_6BYTE ((
1b09f 28 28 69 36 34 29 30 78 30 30 30 30 38 30 30 30  ((i64)0x00008000
1b0a0 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 69 36  )<<32)-1).    i6
1b0a1 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b  4 i = pMem->u.i;
1b0a2 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 20 20  .    u64 u;.    
1b0a3 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3e  if( file_format>
1b0a4 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20 29  =4 && (i&1)==i )
1b0a5 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 38  {.      return 8
1b0a6 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d 0a 20  +(u32)i;.    }. 
1b0a7 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69 20     u = i<0 ? -i 
1b0a8 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d  : i;.    if( u<=
1b0a9 31 32 37 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  127 ) return 1;.
1b0aa 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37 36 37      if( u<=32767
1b0ab 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20   ) return 2;.   
1b0ac 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30 37 20   if( u<=8388607 
1b0ad 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20  ) return 3;.    
1b0ae 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33 36 34  if( u<=214748364
1b0af 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a 20 20  7 ) return 4;.  
1b0b0 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42 59    if( u<=MAX_6BY
1b0b1 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b 0a 20  TE ) return 5;. 
1b0b2 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d     return 6;.  }
1b0b3 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d  .  if( flags&MEM
1b0b4 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74  _Real ){.    ret
1b0b5 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 7;.  }.  ass
1b0b6 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  ert( pMem->db->m
1b0b7 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 66  allocFailed || f
1b0b8 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45  lags&(MEM_Str|ME
1b0b9 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20 3d  M_Blob) );.  n =
1b0ba 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28 20   pMem->n;.  if( 
1b0bb 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
1b0bc 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d 65   ){.    n += pMe
1b0bd 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 0a  m->u.nZero;.  }.
1b0be 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29    assert( n>=0 )
1b0bf 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e 2a 32  ;.  return ((n*2
1b0c0 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61 67 73  ) + 12 + ((flags
1b0c1 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b 0a  &MEM_Str)!=0));.
1b0c2 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1b0c3 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
1b0c4 65 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e  e data correspon
1b0c5 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70  ding to the supp
1b0c6 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79 70 65  lied serial-type
1b0c7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1b0c8 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56  ATE u32 sqlite3V
1b0c9 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
1b0ca 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65  (u32 serial_type
1b0cb 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f  ){.  if( serial_
1b0cc 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20  type>=12 ){.    
1b0cd 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c 5f 74  return (serial_t
1b0ce 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65 6c  ype-12)/2;.  }el
1b0cf 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63  se{.    static c
1b0d0 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b 5d 20  onst u8 aSize[] 
1b0d1 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20  = { 0, 1, 2, 3, 
1b0d2 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 30  4, 6, 8, 8, 0, 0
1b0d3 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72 65  , 0, 0 };.    re
1b0d4 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 61  turn aSize[seria
1b0d5 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a 0a  l_type];.  }.}..
1b0d6 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20  /*.** If we are 
1b0d7 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63 74 75  on an architectu
1b0d8 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d 65 6e  re with mixed-en
1b0d9 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a 2a  dian floating .*
1b0da 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20 41 52  * points (ex: AR
1b0db 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20 74 68  M7) then swap th
1b0dc 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65 73 20  e lower 4 bytes 
1b0dd 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75 70 70  with the .** upp
1b0de 65 72 20 34 20 62 79 74 65 73 2e 20 20 52 65 74  er 4 bytes.  Ret
1b0df 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 2e 0a  urn the result..
1b0e0 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20 61  **.** For most a
1b0e1 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 68  rchitectures, th
1b0e2 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  is is a no-op..*
1b0e3 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20 49  *.** (later):  I
1b0e4 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74 6f  t is reported to
1b0e5 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d 69 78   me that the mix
1b0e6 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c 65  ed-endian proble
1b0e7 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73 20  m.** on ARM7 is 
1b0e8 61 6e 20 69 73 73 75 65 20 77 69 74 68 20 47 43  an issue with GC
1b0e9 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20  C, not with the 
1b0ea 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74 20 73  ARM7 chip.  It s
1b0eb 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61 72  eems.** that ear
1b0ec 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47  ly versions of G
1b0ed 43 43 20 73 74 6f 72 65 64 20 74 68 65 20 74 77  CC stored the tw
1b0ee 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36 34 2d  o words of a 64-
1b0ef 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e 20  bit.** float in 
1b0f0 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 2e  the wrong order.
1b0f1 20 20 41 6e 64 20 74 68 61 74 20 65 72 72 6f 72    And that error
1b0f2 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70 61 67   has been propag
1b0f3 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73 69 6e  ated.** ever sin
1b0f4 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 69  ce.  The blame i
1b0f5 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  s not necessaril
1b0f6 79 20 77 69 74 68 20 47 43 43 2c 20 74 68 6f 75  y with GCC, thou
1b0f7 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68 74  gh..** GCC might
1b0f8 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 79 69   have just copyi
1b0f9 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 66  ng the problem f
1b0fa 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d 70  rom a prior comp
1b0fb 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61 6c  iler..** I am al
1b0fc 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e 65 77  so told that new
1b0fd 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47  er versions of G
1b0fe 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61  CC that follow a
1b0ff 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41 42   different.** AB
1b100 49 20 67 65 74 20 74 68 65 20 62 79 74 65 20 6f  I get the byte o
1b101 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a 2a  rder right..**.*
1b102 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75 73 69  * Developers usi
1b103 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e 20  ng SQLite on an 
1b104 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d 70  ARM7 should comp
1b105 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68 65 69  ile and run thei
1b106 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  r.** application
1b107 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f   using -DSQLITE_
1b108 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61 73 74  DEBUG=1 at least
1b109 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44 45 42   once.  With DEB
1b10a 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 73  UG.** enabled, s
1b10b 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65 6c 6f  ome asserts belo
1b10c 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20 74 68  w will ensure th
1b10d 61 74 20 74 68 65 20 62 79 74 65 20 6f 72 64 65  at the byte orde
1b10e 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e 67  r of.** floating
1b10f 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 69 73   point values is
1b110 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a 20   correct..**.** 
1b111 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20 46 72  (2007-08-30)  Fr
1b112 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68 61 73  ank van Vugt has
1b113 20 73 74 75 64 69 65 64 20 74 68 69 73 20 70 72   studied this pr
1b114 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a 2a  oblem closely.**
1b115 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20 68 69   and has send hi
1b116 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20 74 68  s findings to th
1b117 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c 6f 70  e SQLite develop
1b118 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20 77  ers.  Frank.** w
1b119 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d 65 20  rites that some 
1b11a 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f 66  Linux kernels of
1b11b 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  fer floating poi
1b11c 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a 20 65  nt hardware.** e
1b11d 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20 75 73  mulation that us
1b11e 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20 6d  es only 32-bit m
1b11f 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65 61 64  antissas instead
1b120 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20 34   of a full .** 4
1b121 38 2d 62 69 74 73 20 61 73 20 72 65 71 75 69 72  8-bits as requir
1b122 65 64 20 62 79 20 74 68 65 20 49 45 45 45 20 73  ed by the IEEE s
1b123 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69 73 20  tandard.  (This 
1b124 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49 47  is the.** CONFIG
1b125 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f 70 74  _FPE_FASTFPE opt
1b126 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20 73  ion.)  On such s
1b127 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e 67  ystems, floating
1b128 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20 73   point.** byte s
1b129 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 73 20  wapping becomes 
1b12a 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 65 64  very complicated
1b12b 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72 6f 62  .  To avoid prob
1b12c 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65 63  lems,.** the nec
1b12d 65 73 73 61 72 79 20 62 79 74 65 20 73 77 61 70  essary byte swap
1b12e 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 64 20  ping is carried 
1b12f 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 2d 62  out using a 64-b
1b130 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 61  it integer.** ra
1b131 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 2d 62  ther than a 64-b
1b132 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e 6b  it float.  Frank
1b133 20 61 73 73 75 72 65 73 20 75 73 20 74 68 61 74   assures us that
1b134 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 0a 2a   the code here.*
1b135 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d 2e  * works for him.
1b136 20 20 57 65 2c 20 74 68 65 20 64 65 76 65 6c 6f    We, the develo
1b137 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20 77 61  pers, have no wa
1b138 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e 74  y to independent
1b139 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 69  ly.** verify thi
1b13a 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73 65 65  s, but Frank see
1b13b 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74 20  ms to know what 
1b13c 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20 61 62  he is talking ab
1b13d 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72 75  out.** so we tru
1b13e 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64 65  st him..*/.#ifde
1b13f 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45  f SQLITE_MIXED_E
1b140 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41  NDIAN_64BIT_FLOA
1b141 54 0a 73 74 61 74 69 63 20 75 36 34 20 66 6c 6f  T.static u64 flo
1b142 61 74 53 77 61 70 28 75 36 34 20 69 6e 29 7b 0a  atSwap(u64 in){.
1b143 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75 36    union {.    u6
1b144 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69 5b 32  4 r;.    u32 i[2
1b145 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32 20  ];.  } u;.  u32 
1b146 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b 0a  t;..  u.r = in;.
1b147 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20 20    t = u.i[0];.  
1b148 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d 3b  u.i[0] = u.i[1];
1b149 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a 20  .  u.i[1] = t;. 
1b14a 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a 23   return u.r;.}.#
1b14b 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65   define swapMixe
1b14c 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 20  dEndianFloat(X) 
1b14d 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70 28 58   X = floatSwap(X
1b14e 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ).#else.# define
1b14f 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e   swapMixedEndian
1b150 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69 66 0a  Float(X).#endif.
1b151 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  ./*.** Write the
1b152 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61   serialized data
1b153 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20 76 61   blob for the va
1b154 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d  lue stored in pM
1b155 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66 2e  em into .** buf.
1b156 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   It is assumed t
1b157 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68  hat the caller h
1b158 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 75 66  as allocated suf
1b159 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e 0a 2a  ficient space..*
1b15a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
1b15b 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 69  ber of bytes wri
1b15c 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75 66  tten..**.** nBuf
1b15d 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f   is the amount o
1b15e 66 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20  f space left in 
1b15f 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75 73  buf[].  nBuf mus
1b160 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20 6c  t always be.** l
1b161 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68  arge enough to h
1b162 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65 20 66  old the entire f
1b163 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c 20 69  ield.  Except, i
1b164 66 20 74 68 65 20 66 69 65 6c 64 20 69 73 0a 2a  f the field is.*
1b165 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 61 20  * a blob with a 
1b166 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c  zero-filled tail
1b167 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69 67  , then buf[] mig
1b168 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 20 72  ht be just the r
1b169 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f 20  ight.** size to 
1b16a 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67 20  hold everything 
1b16b 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 20 7a  except for the z
1b16c 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e  ero-filled tail.
1b16d 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69 73    If buf[].** is
1b16e 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67 68   only big enough
1b16f 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f 6e   to hold the non
1b170 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20 74 68  -zero prefix, th
1b171 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 68  en only write th
1b172 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e 74  at.** prefix int
1b173 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20 69 66  o buf[].  But if
1b174 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67 65 20   buf[] is large 
1b175 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 62  enough to hold b
1b176 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65 66 69  oth the.** prefi
1b177 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c 20 74  x and the tail t
1b178 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 70 72  hen write the pr
1b179 65 66 69 78 20 61 6e 64 20 73 65 74 20 74 68 65  efix and set the
1b17a 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a 20   tail to all.** 
1b17b 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  zeros..**.** Ret
1b17c 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1b17d 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79  f bytes actually
1b17e 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 62 75   written into bu
1b17f 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  f[].  The number
1b180 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69 6e 20  .** of bytes in 
1b181 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20  the zero-filled 
1b182 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64 65 64  tail is included
1b183 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76   in the return v
1b184 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20  alue only.** if 
1b185 74 68 6f 73 65 20 62 79 74 65 73 20 77 65 72 65  those bytes were
1b186 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66 5b 5d   zeroed in buf[]
1b187 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/ .SQLITE_PRI
1b188 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
1b189 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 38  VdbeSerialPut(u8
1b18a 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75 66 2c   *buf, int nBuf,
1b18b 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20   Mem *pMem, int 
1b18c 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20  file_format){.  
1b18d 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 20  u32 serial_type 
1b18e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
1b18f 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66 69  ialType(pMem, fi
1b190 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 75 33  le_format);.  u3
1b191 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 74  2 len;..  /* Int
1b192 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20 2a 2f  eger and Real */
1b193 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79  .  if( serial_ty
1b194 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 6c 5f  pe<=7 && serial_
1b195 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20 75 36  type>0 ){.    u6
1b196 34 20 76 3b 0a 20 20 20 20 75 33 32 20 69 3b 0a  4 v;.    u32 i;.
1b197 20 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74      if( serial_t
1b198 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20  ype==7 ){.      
1b199 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 76  assert( sizeof(v
1b19a 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e  )==sizeof(pMem->
1b19b 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63  r) );.      memc
1b19c 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72 2c  py(&v, &pMem->r,
1b19d 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20 20   sizeof(v));.   
1b19e 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69     swapMixedEndi
1b19f 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20 20  anFloat(v);.    
1b1a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 20 3d  }else{.      v =
1b1a1 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20   pMem->u.i;.    
1b1a2 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d 20  }.    len = i = 
1b1a3 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1b1a4 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f  lTypeLen(serial_
1b1a5 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65 72  type);.    asser
1b1a6 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29 6e 42 75  t( len<=(u32)nBu
1b1a7 66 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  f );.    while( 
1b1a8 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62 75 66  i-- ){.      buf
1b1a9 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 30 78 46  [i] = (u8)(v&0xF
1b1aa 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20  F);.      v >>= 
1b1ab 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  8;.    }.    ret
1b1ac 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20  urn len;.  }..  
1b1ad 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 6c 6f  /* String or blo
1b1ae 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61  b */.  if( seria
1b1af 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20  l_type>=12 ){.  
1b1b0 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1b1b1 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67  n + ((pMem->flag
1b1b2 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d  s & MEM_Zero)?pM
1b1b3 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29 0a 20  em->u.nZero:0). 
1b1b4 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d 20 28              == (
1b1b5 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65 53  int)sqlite3VdbeS
1b1b6 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72  erialTypeLen(ser
1b1b7 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a 20 20 20  ial_type) );.   
1b1b8 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e   assert( pMem->n
1b1b9 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20 6c 65  <=nBuf );.    le
1b1ba 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20  n = pMem->n;.   
1b1bb 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 70 4d 65   memcpy(buf, pMe
1b1bc 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20  m->z, len);.    
1b1bd 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
1b1be 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
1b1bf 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d      len += pMem-
1b1c0 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 20  >u.nZero;.      
1b1c1 61 73 73 65 72 74 28 20 6e 42 75 66 3e 3d 30 20  assert( nBuf>=0 
1b1c2 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e  );.      if( len
1b1c3 20 3e 20 28 75 33 32 29 6e 42 75 66 20 29 7b 0a   > (u32)nBuf ){.
1b1c4 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 28 75          len = (u
1b1c5 33 32 29 6e 42 75 66 3b 0a 20 20 20 20 20 20 7d  32)nBuf;.      }
1b1c6 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 62  .      memset(&b
1b1c7 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20  uf[pMem->n], 0, 
1b1c8 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20  len-pMem->n);.  
1b1c9 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6c    }.    return l
1b1ca 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 55  en;.  }..  /* NU
1b1cb 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74 73 20  LL or constants 
1b1cc 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65 74 75  0 or 1 */.  retu
1b1cd 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  rn 0;.}../*.** D
1b1ce 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 64  eserialize the d
1b1cf 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74 65 64  ata blob pointed
1b1d0 20 74 6f 20 62 79 20 62 75 66 20 61 73 20 73 65   to by buf as se
1b1d1 72 69 61 6c 20 74 79 70 65 20 73 65 72 69 61 6c  rial type serial
1b1d2 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73 74 6f  _type.** and sto
1b1d3 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
1b1d4 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e 20 74   pMem.  Return t
1b1d5 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
1b1d6 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 51 4c  es read..*/ .SQL
1b1d7 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
1b1d8 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1b1d9 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 20 75 6e  lGet(.  const un
1b1da 73 69 67 6e 65 64 20 63 68 61 72 20 2a 62 75 66  signed char *buf
1b1db 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20  ,     /* Buffer 
1b1dc 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 66  to deserialize f
1b1dd 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73 65 72  rom */.  u32 ser
1b1de 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 20 20  ial_type,       
1b1df 20 20 20 20 20 20 20 2f 2a 20 53 65 72 69 61 6c         /* Serial
1b1e0 20 74 79 70 65 20 74 6f 20 64 65 73 65 72 69 61   type to deseria
1b1e1 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  lize */.  Mem *p
1b1e2 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  Mem             
1b1e3 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
1b1e4 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 65 20  y cell to write 
1b1e5 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a 29 7b  value into */.){
1b1e6 0a 20 20 73 77 69 74 63 68 28 20 73 65 72 69 61  .  switch( seria
1b1e7 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61  l_type ){.    ca
1b1e8 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 65 73 65  se 10:   /* Rese
1b1e9 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20  rved for future 
1b1ea 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  use */.    case 
1b1eb 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 65  11:   /* Reserve
1b1ec 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65  d for future use
1b1ed 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 30 3a 20   */.    case 0: 
1b1ee 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20  {  /* NULL */.  
1b1ef 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1b1f0 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
1b1f1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1b1f2 20 20 20 63 61 73 65 20 31 3a 20 7b 20 2f 2a 20     case 1: { /* 
1b1f3 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  1-byte signed in
1b1f4 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70  teger */.      p
1b1f5 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 67 6e  Mem->u.i = (sign
1b1f6 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d 3b 0a  ed char)buf[0];.
1b1f7 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1b1f8 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1b1f9 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1b1fa 20 7d 0a 20 20 20 20 63 61 73 65 20 32 3a 20 7b   }.    case 2: {
1b1fb 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65   /* 2-byte signe
1b1fc 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20  d integer */.   
1b1fd 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28     pMem->u.i = (
1b1fe 28 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75  ((signed char)bu
1b1ff 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b  f[0])<<8) | buf[
1b200 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  1];.      pMem->
1b201 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1b202 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b  .      return 2;
1b203 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1b204 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 20 73  3: { /* 3-byte s
1b205 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
1b206 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  .      pMem->u.i
1b207 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61   = (((signed cha
1b208 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29 20 7c  r)buf[0])<<16) |
1b209 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c 20 62   (buf[1]<<8) | b
1b20a 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70 4d 65  uf[2];.      pMe
1b20b 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
1b20c 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  nt;.      return
1b20d 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   3;.    }.    ca
1b20e 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 79 74  se 4: { /* 4-byt
1b20f 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
1b210 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
1b211 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32  u.i = (buf[0]<<2
1b212 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36  4) | (buf[1]<<16
1b213 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20  ) | (buf[2]<<8) 
1b214 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20  | buf[3];.      
1b215 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1b216 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74  M_Int;.      ret
1b217 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 4;.    }.   
1b218 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20 36 2d   case 5: { /* 6-
1b219 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65  byte signed inte
1b21a 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 36 34  ger */.      u64
1b21b 20 78 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63   x = (((signed c
1b21c 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20  har)buf[0])<<8) 
1b21d 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20  | buf[1];.      
1b21e 75 33 32 20 79 20 3d 20 28 62 75 66 5b 32 5d 3c  u32 y = (buf[2]<
1b21f 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c 3c  <24) | (buf[3]<<
1b220 31 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c 38  16) | (buf[4]<<8
1b221 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 20 20  ) | buf[5];.    
1b222 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20    x = (x<<32) | 
1b223 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  y;.      pMem->u
1b224 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a  .i = *(i64*)&x;.
1b225 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1b226 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1b227 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 20     return 6;.   
1b228 20 7d 0a 20 20 20 20 63 61 73 65 20 36 3a 20 20   }.    case 6:  
1b229 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65   /* 8-byte signe
1b22a 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20  d integer */.   
1b22b 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20 49 45   case 7: { /* IE
1b22c 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  EE floating poin
1b22d 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78  t */.      u64 x
1b22e 3b 0a 20 20 20 20 20 20 75 33 32 20 79 3b 0a 23  ;.      u32 y;.#
1b22f 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  if !defined(NDEB
1b230 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  UG) && !defined(
1b231 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
1b232 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 20 20  TING_POINT).    
1b233 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74    /* Verify that
1b234 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 66 6c   integers and fl
1b235 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
1b236 75 65 73 20 75 73 65 20 74 68 65 20 73 61 6d 65  ues use the same
1b237 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 6f  .      ** byte o
1b238 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 61 74 20  rder.  Or, that 
1b239 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f  if SQLITE_MIXED_
1b23a 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f  ENDIAN_64BIT_FLO
1b23b 41 54 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64  AT is.      ** d
1b23c 65 66 69 6e 65 64 20 74 68 61 74 20 36 34 2d 62  efined that 64-b
1b23d 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  it floating poin
1b23e 74 20 76 61 6c 75 65 73 20 72 65 61 6c 6c 79 20  t values really 
1b23f 61 72 65 20 6d 69 78 65 64 0a 20 20 20 20 20 20  are mixed.      
1b240 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20 20 20  ** endian..     
1b241 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63   */.      static
1b242 20 63 6f 6e 73 74 20 75 36 34 20 74 31 20 3d 20   const u64 t1 = 
1b243 28 28 75 36 34 29 30 78 33 66 66 30 30 30 30 30  ((u64)0x3ff00000
1b244 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73 74 61  )<<32;.      sta
1b245 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c 65  tic const double
1b246 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20   r1 = 1.0;.     
1b247 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0a 20 20   u64 t2 = t1;.  
1b248 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64      swapMixedEnd
1b249 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a 20 20  ianFloat(t2);.  
1b24a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65      assert( size
1b24b 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66 28 74  of(r1)==sizeof(t
1b24c 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 26 72 31  2) && memcmp(&r1
1b24d 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 28 72 31  , &t2, sizeof(r1
1b24e 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a  ))==0 );.#endif.
1b24f 0a 20 20 20 20 20 20 78 20 3d 20 28 62 75 66 5b  .      x = (buf[
1b250 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31  0]<<24) | (buf[1
1b251 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d  ]<<16) | (buf[2]
1b252 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20  <<8) | buf[3];. 
1b253 20 20 20 20 20 79 20 3d 20 28 62 75 66 5b 34 5d       y = (buf[4]
1b254 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 35 5d 3c  <<24) | (buf[5]<
1b255 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5d 3c 3c  <16) | (buf[6]<<
1b256 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 20 20  8) | buf[7];.   
1b257 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c     x = (x<<32) |
1b258 20 79 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65   y;.      if( se
1b259 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29 7b 0a  rial_type==6 ){.
1b25a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e          pMem->u.
1b25b 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20  i = *(i64*)&x;. 
1b25c 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61         pMem->fla
1b25d 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
1b25e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1b25f 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
1b260 66 28 78 29 3d 3d 38 20 26 26 20 73 69 7a 65 6f  f(x)==8 && sizeo
1b261 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 3b  f(pMem->r)==8 );
1b262 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d 69 78  .        swapMix
1b263 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78 29  edEndianFloat(x)
1b264 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  ;.        memcpy
1b265 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 73  (&pMem->r, &x, s
1b266 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 20  izeof(x));.     
1b267 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1b268 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 70 4d   sqlite3IsNaN(pM
1b269 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75 6c  em->r) ? MEM_Nul
1b26a 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20  l : MEM_Real;.  
1b26b 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
1b26c 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 8;.    }.    
1b26d 63 61 73 65 20 38 3a 20 20 20 20 2f 2a 20 49 6e  case 8:    /* In
1b26e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 20 20 63  teger 0 */.    c
1b26f 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49 6e 74  ase 9: {  /* Int
1b270 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 20 20 20  eger 1 */.      
1b271 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 72 69  pMem->u.i = seri
1b272 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20 20 20  al_type-8;.     
1b273 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1b274 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65  EM_Int;.      re
1b275 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
1b276 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
1b277 20 20 20 75 33 32 20 6c 65 6e 20 3d 20 28 73 65     u32 len = (se
1b278 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b  rial_type-12)/2;
1b279 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d  .      pMem->z =
1b27a 20 28 63 68 61 72 20 2a 29 62 75 66 3b 0a 20 20   (char *)buf;.  
1b27b 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c 65      pMem->n = le
1b27c 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78  n;.      pMem->x
1b27d 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  Del = 0;.      i
1b27e 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 26 30  f( serial_type&0
1b27f 78 30 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70  x01 ){.        p
1b280 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1b281 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65 6d  _Str | MEM_Ephem
1b282 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1b283 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61         pMem->fla
1b284 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20  gs = MEM_Blob | 
1b285 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20  MEM_Ephem;.     
1b286 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   }.      return 
1b287 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  len;.    }.  }. 
1b288 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f   return 0;.}.../
1b289 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e  *.** Given the n
1b28a 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f 64 69 6e  Key-byte encodin
1b28b 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 6e  g of a record in
1b28c 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65 20 74   pKey[], parse t
1b28d 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 74  he.** record int
1b28e 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  o a UnpackedReco
1b28f 72 64 20 73 74 72 75 63 74 75 72 65 2e 20 20 52  rd structure.  R
1b290 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1b291 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72 75 63  to.** that struc
1b292 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ture..**.** The 
1b293 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
1b294 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65 20 73   might provide s
1b295 7a 53 70 61 63 65 20 62 79 74 65 73 20 6f 66 20  zSpace bytes of 
1b296 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20  memory.** space 
1b297 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 69 73  at pSpace.  This
1b298 20 73 70 61 63 65 20 63 61 6e 20 62 65 20 75 73   space can be us
1b299 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ed to hold the r
1b29a 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62 65 50  eturned.** VDbeP
1b29b 61 72 73 65 64 52 65 63 6f 72 64 20 73 74 72 75  arsedRecord stru
1b29c 63 74 75 72 65 20 69 66 20 69 74 20 69 73 20 6c  cture if it is l
1b29d 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 20 49 66  arge enough.  If
1b29e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 62 69   it is.** not bi
1b29f 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 63 65 20  g enough, space 
1b2a0 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
1b2a1 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
1b2a2 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  )..**.** The ret
1b2a3 75 72 6e 65 64 20 73 74 72 75 63 74 75 72 65 20  urned structure 
1b2a4 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64  should be closed
1b2a5 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a   by a call to.**
1b2a6 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
1b2a7 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  teUnpackedRecord
1b2a8 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50  ()..*/ .SQLITE_P
1b2a9 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 64 52  RIVATE UnpackedR
1b2aa 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 64  ecord *sqlite3Vd
1b2ab 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 0a  beRecordUnpack(.
1b2ac 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
1b2ad 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e 66 6f  nfo,     /* Info
1b2ae 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
1b2af 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20  e record format 
1b2b0 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20  */.  int nKey,  
1b2b1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1b2b2 69 7a 65 20 6f 66 20 74 68 65 20 62 69 6e 61 72  ize of the binar
1b2b3 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f  y record */.  co
1b2b4 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
1b2b5 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e 61       /* The bina
1b2b6 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63  ry record */.  c
1b2b7 68 61 72 20 2a 70 53 70 61 63 65 2c 20 20 20 20  har *pSpace,    
1b2b8 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c 69 67 6e        /* Unalign
1b2b9 65 64 20 73 70 61 63 65 20 61 76 61 69 6c 61 62  ed space availab
1b2ba 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f  le to hold the o
1b2bb 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73  bject */.  int s
1b2bc 7a 53 70 61 63 65 20 20 20 20 20 20 20 20 20 20  zSpace          
1b2bd 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70    /* Size of pSp
1b2be 61 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a  ace[] in bytes *
1b2bf 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  /.){.  const uns
1b2c0 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79  igned char *aKey
1b2c1 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e   = (const unsign
1b2c2 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a  ed char *)pKey;.
1b2c3 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1b2c4 20 2a 70 3b 20 20 2f 2a 20 54 68 65 20 75 6e 70   *p;  /* The unp
1b2c5 61 63 6b 65 64 20 72 65 63 6f 72 64 20 74 68 61  acked record tha
1b2c6 74 20 77 65 20 77 69 6c 6c 20 72 65 74 75 72 6e  t we will return
1b2c7 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b   */.  int nByte;
1b2c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
1b2c9 6f 72 79 20 73 70 61 63 65 20 6e 65 65 64 65 64  ory space needed
1b2ca 20 74 6f 20 68 6f 6c 64 20 70 2c 20 69 6e 20 62   to hold p, in b
1b2cb 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 64 3b  ytes */.  int d;
1b2cc 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 20 75 31  .  u32 idx;.  u1
1b2cd 36 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20  6 u;            
1b2ce 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f    /* Unsigned lo
1b2cf 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
1b2d0 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 4d 65 6d  u32 szHdr;.  Mem
1b2d1 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 6e 4f   *pMem;.  int nO
1b2d2 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ff;           /*
1b2d3 20 49 6e 63 72 65 61 73 65 20 70 53 70 61 63 65   Increase pSpace
1b2d4 20 62 79 20 74 68 69 73 20 6d 75 63 68 20 74 6f   by this much to
1b2d5 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 20 69 74   8-byte align it
1b2d6 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a   */.  .  /*.  **
1b2d7 20 57 65 20 77 61 6e 74 20 74 6f 20 73 68 69 66   We want to shif
1b2d8 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 53  t the pointer pS
1b2d9 70 61 63 65 20 75 70 20 73 75 63 68 20 74 68 61  pace up such tha
1b2da 74 20 69 74 20 69 73 20 38 2d 62 79 74 65 20 61  t it is 8-byte a
1b2db 6c 69 67 6e 65 64 2e 0a 20 20 2a 2a 20 54 68 75  ligned..  ** Thu
1b2dc 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 63 61  s, we need to ca
1b2dd 6c 63 75 6c 61 74 65 20 61 20 76 61 6c 75 65 2c  lculate a value,
1b2de 20 6e 4f 66 66 2c 20 62 65 74 77 65 65 6e 20 30   nOff, between 0
1b2df 20 61 6e 64 20 37 2c 20 74 6f 20 73 68 69 66 74   and 7, to shift
1b2e0 20 0a 20 20 2a 2a 20 69 74 20 62 79 2e 20 20 49   .  ** it by.  I
1b2e1 66 20 70 53 70 61 63 65 20 69 73 20 61 6c 72 65  f pSpace is alre
1b2e2 61 64 79 20 38 2d 62 79 74 65 20 61 6c 69 67 6e  ady 8-byte align
1b2e3 65 64 2c 20 6e 4f 66 66 20 73 68 6f 75 6c 64 20  ed, nOff should 
1b2e4 62 65 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20  be zero..  */.  
1b2e5 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28 53 51 4c  nOff = (8 - (SQL
1b2e6 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70  ITE_PTR_TO_INT(p
1b2e7 53 70 61 63 65 29 20 26 20 37 29 29 20 26 20 37  Space) & 7)) & 7
1b2e8 3b 0a 20 20 70 53 70 61 63 65 20 2b 3d 20 6e 4f  ;.  pSpace += nO
1b2e9 66 66 3b 0a 20 20 73 7a 53 70 61 63 65 20 2d 3d  ff;.  szSpace -=
1b2ea 20 6e 4f 66 66 3b 0a 20 20 6e 42 79 74 65 20 3d   nOff;.  nByte =
1b2eb 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55   ROUND8(sizeof(U
1b2ec 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 20  npackedRecord)) 
1b2ed 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 28 70  + sizeof(Mem)*(p
1b2ee 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b  KeyInfo->nField+
1b2ef 31 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e  1);.  if( nByte>
1b2f0 73 7a 53 70 61 63 65 20 29 7b 0a 20 20 20 20 70  szSpace ){.    p
1b2f1 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1b2f2 6f 63 52 61 77 28 70 4b 65 79 49 6e 66 6f 2d 3e  ocRaw(pKeyInfo->
1b2f3 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20  db, nByte);.    
1b2f4 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1b2f5 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67  n 0;.    p->flag
1b2f6 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45  s = UNPACKED_NEE
1b2f7 44 5f 46 52 45 45 20 7c 20 55 4e 50 41 43 4b 45  D_FREE | UNPACKE
1b2f8 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a  D_NEED_DESTROY;.
1b2f9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d    }else{.    p =
1b2fa 20 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64   (UnpackedRecord
1b2fb 2a 29 70 53 70 61 63 65 3b 0a 20 20 20 20 70 2d  *)pSpace;.    p-
1b2fc 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45  >flags = UNPACKE
1b2fd 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a  D_NEED_DESTROY;.
1b2fe 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 49 6e 66    }.  p->pKeyInf
1b2ff 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  o = pKeyInfo;.  
1b300 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79  p->nField = pKey
1b301 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31  Info->nField + 1
1b302 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 70 4d  ;.  p->aMem = pM
1b303 65 6d 20 3d 20 28 4d 65 6d 2a 29 26 28 28 63 68  em = (Mem*)&((ch
1b304 61 72 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 69  ar*)p)[ROUND8(si
1b305 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63  zeof(UnpackedRec
1b306 6f 72 64 29 29 5d 3b 0a 20 20 61 73 73 65 72 74  ord))];.  assert
1b307 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
1b308 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a  GNMENT(pMem) );.
1b309 20 20 69 64 78 20 3d 20 67 65 74 56 61 72 69 6e    idx = getVarin
1b30a 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29  t32(aKey, szHdr)
1b30b 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20  ;.  d = szHdr;. 
1b30c 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28   u = 0;.  while(
1b30d 20 69 64 78 3c 73 7a 48 64 72 20 26 26 20 75 3c   idx<szHdr && u<
1b30e 70 2d 3e 6e 46 69 65 6c 64 20 26 26 20 64 3c 3d  p->nField && d<=
1b30f 6e 4b 65 79 20 29 7b 0a 20 20 20 20 75 33 32 20  nKey ){.    u32 
1b310 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 20  serial_type;..  
1b311 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61 72 69    idx += getVari
1b312 6e 74 33 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c  nt32(&aKey[idx],
1b313 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20   serial_type);. 
1b314 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 70     pMem->enc = p
1b315 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20  KeyInfo->enc;.  
1b316 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b 65    pMem->db = pKe
1b317 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20 70  yInfo->db;.    p
1b318 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a  Mem->flags = 0;.
1b319 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f      pMem->zMallo
1b31a 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d 20  c = 0;.    d += 
1b31b 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1b31c 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20 73  lGet(&aKey[d], s
1b31d 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65 6d  erial_type, pMem
1b31e 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20  );.    pMem++;. 
1b31f 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61 73     u++;.  }.  as
1b320 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e 66  sert( u<=pKeyInf
1b321 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29 3b  o->nField + 1 );
1b322 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 75  .  p->nField = u
1b323 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  ;.  return (void
1b324 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  *)p;.}../*.** Th
1b325 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72  is routine destr
1b326 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52 65  oys a UnpackedRe
1b327 63 6f 72 64 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  cord object..*/.
1b328 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1b329 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
1b32a 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63  eleteUnpackedRec
1b32b 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f  ord(UnpackedReco
1b32c 72 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  rd *p){.  int i;
1b32d 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 0a 20  .  Mem *pMem;.. 
1b32e 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b   assert( p!=0 );
1b32f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c  .  assert( p->fl
1b330 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e  ags & UNPACKED_N
1b331 45 45 44 5f 44 45 53 54 52 4f 59 20 29 3b 0a 20  EED_DESTROY );. 
1b332 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d 70   for(i=0, pMem=p
1b333 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46 69  ->aMem; i<p->nFi
1b334 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b 2b  eld; i++, pMem++
1b335 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 6e  ){.    /* The un
1b336 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 69 73  packed record is
1b337 20 61 6c 77 61 79 73 20 63 6f 6e 73 74 72 75 63   always construc
1b338 74 65 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a  ted by the.    *
1b339 2a 20 73 71 6c 69 74 65 33 56 64 62 65 55 6e 70  * sqlite3VdbeUnp
1b33a 61 63 6b 52 65 63 6f 72 64 28 29 20 66 75 6e 63  ackRecord() func
1b33b 74 69 6f 6e 20 61 62 6f 76 65 2c 20 77 68 69 63  tion above, whic
1b33c 68 20 6d 61 6b 65 73 20 61 6c 6c 0a 20 20 20 20  h makes all.    
1b33d 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 20 62  ** strings and b
1b33e 6c 6f 62 73 20 73 74 61 74 69 63 2e 20 20 41 6e  lobs static.  An
1b33f 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65 6c  d none of the el
1b340 65 6d 65 6e 74 73 20 61 72 65 0a 20 20 20 20 2a  ements are.    *
1b341 2a 20 65 76 65 72 20 74 72 61 6e 73 66 6f 72 6d  * ever transform
1b342 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20  ed, so there is 
1b343 6e 65 76 65 72 20 61 6e 79 74 68 69 6e 67 20 74  never anything t
1b344 6f 20 64 65 6c 65 74 65 2e 0a 20 20 20 20 2a 2f  o delete..    */
1b345 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 70  .    if( NEVER(p
1b346 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 20 29 20  Mem->zMalloc) ) 
1b347 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1b348 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 7d  lease(pMem);.  }
1b349 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  .  if( p->flags 
1b34a 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f  & UNPACKED_NEED_
1b34b 46 52 45 45 20 29 7b 0a 20 20 20 20 73 71 6c 69  FREE ){.    sqli
1b34c 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70 4b 65  te3DbFree(p->pKe
1b34d 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b 0a 20  yInfo->db, p);. 
1b34e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
1b34f 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72   function compar
1b350 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65  es the two table
1b351 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 72   rows or index r
1b352 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63 69 66  ecords.** specif
1b353 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20 70  ied by {nKey1, p
1b354 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 79 32  Key1} and pPKey2
1b355 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20  .  It returns a 
1b356 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 0a 2a  negative, zero.*
1b357 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 6e  * or positive in
1b358 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 69 73  teger if key1 is
1b359 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61   less than, equa
1b35a 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 61  l to or .** grea
1b35b 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e 20 20  ter than key2.  
1b35c 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79  The {nKey1, pKey
1b35d 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61  1} key must be a
1b35e 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65 64   blob.** created
1b35f 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52 65   by th OP_MakeRe
1b360 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 74  cord opcode of t
1b361 68 65 20 56 44 42 45 2e 20 20 54 68 65 20 70 50  he VDBE.  The pP
1b362 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 74  Key2.** key must
1b363 20 62 65 20 61 20 70 61 72 73 65 64 20 6b 65 79   be a parsed key
1b364 20 73 75 63 68 20 61 73 20 6f 62 74 61 69 6e 65   such as obtaine
1b365 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65  d from.** sqlite
1b366 33 56 64 62 65 50 61 72 73 65 52 65 63 6f 72 64  3VdbeParseRecord
1b367 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e 64  ..**.** Key1 and
1b368 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 61 76   Key2 do not hav
1b369 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65  e to contain the
1b36a 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
1b36b 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 20 6b  fields..** The k
1b36c 65 79 20 77 69 74 68 20 66 65 77 65 72 20 66 69  ey with fewer fi
1b36d 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c 79 20  elds is usually 
1b36e 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20 74 68  compares less th
1b36f 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67 65  an the .** longe
1b370 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65 72 20  r key.  However 
1b371 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f  if the UNPACKED_
1b372 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20 69 6e  INCRKEY flags in
1b373 20 70 50 4b 65 79 32 20 69 73 20 73 65 74 0a 2a   pPKey2 is set.*
1b374 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e  * and the common
1b375 20 70 72 65 66 69 78 65 73 20 61 72 65 20 65 71   prefixes are eq
1b376 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31 20 69  ual, then key1 i
1b377 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65 79 32  s less than key2
1b378 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65 20 55  ..** Or if the U
1b379 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f 50 52  NPACKED_MATCH_PR
1b37a 45 46 49 58 20 66 6c 61 67 20 69 73 20 73 65 74  EFIX flag is set
1b37b 20 61 6e 64 20 74 68 65 20 70 72 65 66 69 78 65   and the prefixe
1b37c 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c 2c 20  s are.** equal, 
1b37d 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20 61 72  then the keys ar
1b37e 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20  e considered to 
1b37f 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a 2a 20  be equal and.** 
1b380 74 68 65 20 70 61 72 74 73 20 62 65 79 6f 6e 64  the parts beyond
1b381 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66   the common pref
1b382 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a  ix are ignored..
1b383 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55 4e 50  **.** If the UNP
1b384 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57  ACKED_IGNORE_ROW
1b385 49 44 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  ID flag is set, 
1b386 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 62 79  then the last by
1b387 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61  te of.** the hea
1b388 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69 73 20  der of pKey1 is 
1b389 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69 73 20  ignored.  It is 
1b38a 61 73 73 75 6d 65 64 20 74 68 61 74 20 70 4b 65  assumed that pKe
1b38b 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65  y1 is.** an inde
1b38c 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75 73 20  x key, and thus 
1b38d 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f 77 69  ends with a rowi
1b38e 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20 6c 61  d value.  The la
1b38f 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68  st byte.** of th
1b390 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20 74 68  e header will th
1b391 65 72 65 66 6f 72 65 20 62 65 20 74 68 65 20 73  erefore be the s
1b392 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68  erial type of th
1b393 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65 20  e rowid:.** one 
1b394 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35  of 1, 2, 3, 4, 5
1b395 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d 20 74  , 6, 8, or 9 - t
1b396 68 65 20 69 6e 74 65 67 65 72 20 73 65 72 69 61  he integer seria
1b397 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68 65 20  l types..** The 
1b398 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74  serial type of t
1b399 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 77  he final rowid w
1b39a 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 61 20  ill always be a 
1b39b 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20  single byte..** 
1b39c 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68 69 73  By ignoring this
1b39d 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20 74 68   last byte of th
1b39e 65 20 68 65 61 64 65 72 2c 20 77 65 20 66 6f 72  e header, we for
1b39f 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  ce the compariso
1b3a0 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74  n.** to ignore t
1b3a1 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20  he rowid at the 
1b3a2 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f 0a  end of key1..*/.
1b3a3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1b3a4 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  nt sqlite3VdbeRe
1b3a5 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20 20 69  cordCompare(.  i
1b3a6 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20  nt nKey1, const 
1b3a7 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f 2a 20  void *pKey1, /* 
1b3a8 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20 55 6e  Left key */.  Un
1b3a9 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 50  packedRecord *pP
1b3aa 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a 20 52  Key2        /* R
1b3ab 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a 20  ight key */.){. 
1b3ac 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20 20 20   int d1;        
1b3ad 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
1b3ae 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78  to aKey[] of nex
1b3af 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 2a  t data element *
1b3b0 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20 20 20  /.  u32 idx1;   
1b3b1 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
1b3b2 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20   into aKey[] of 
1b3b3 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c 65 6d  next header elem
1b3b4 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48  ent */.  u32 szH
1b3b5 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  dr1;        /* N
1b3b6 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
1b3b7 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e  n header */.  in
1b3b8 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  t i = 0;.  int n
1b3b9 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20  Field;.  int rc 
1b3ba 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  = 0;.  const uns
1b3bb 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79  igned char *aKey
1b3bc 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67  1 = (const unsig
1b3bd 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 31  ned char *)pKey1
1b3be 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  ;.  KeyInfo *pKe
1b3bf 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 6d  yInfo;.  Mem mem
1b3c0 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f 20 3d  1;..  pKeyInfo =
1b3c1 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e 66   pPKey2->pKeyInf
1b3c2 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d 20  o;.  mem1.enc = 
1b3c3 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20  pKeyInfo->enc;. 
1b3c4 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65 79 49   mem1.db = pKeyI
1b3c5 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65 6d 31 2e  nfo->db;.  mem1.
1b3c6 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d 65 6d  flags = 0;.  mem
1b3c7 31 2e 75 2e 69 20 3d 20 30 3b 20 20 2f 2a 20 6e  1.u.i = 0;  /* n
1b3c8 6f 74 20 6e 65 65 64 65 64 2c 20 68 65 72 65 20  ot needed, here 
1b3c9 74 6f 20 73 69 6c 65 6e 63 65 20 63 6f 6d 70 69  to silence compi
1b3ca 6c 65 72 20 77 61 72 6e 69 6e 67 20 2a 2f 0a 20  ler warning */. 
1b3cb 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20   mem1.zMalloc = 
1b3cc 30 3b 0a 20 20 0a 20 20 69 64 78 31 20 3d 20 67  0;.  .  idx1 = g
1b3cd 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79 31  etVarint32(aKey1
1b3ce 2c 20 73 7a 48 64 72 31 29 3b 0a 20 20 64 31 20  , szHdr1);.  d1 
1b3cf 3d 20 73 7a 48 64 72 31 3b 0a 20 20 69 66 28 20  = szHdr1;.  if( 
1b3d0 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20  pPKey2->flags & 
1b3d1 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f  UNPACKED_IGNORE_
1b3d2 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 73 7a 48  ROWID ){.    szH
1b3d3 64 72 31 2d 2d 3b 0a 20 20 7d 0a 20 20 6e 46 69  dr1--;.  }.  nFi
1b3d4 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e  eld = pKeyInfo->
1b3d5 6e 46 69 65 6c 64 3b 0a 20 20 77 68 69 6c 65 28  nField;.  while(
1b3d6 20 69 64 78 31 3c 73 7a 48 64 72 31 20 26 26 20   idx1<szHdr1 && 
1b3d7 69 3c 70 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64  i<pPKey2->nField
1b3d8 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65 72 69   ){.    u32 seri
1b3d9 61 6c 5f 74 79 70 65 31 3b 0a 0a 20 20 20 20 2f  al_type1;..    /
1b3da 2a 20 52 65 61 64 20 74 68 65 20 73 65 72 69 61  * Read the seria
1b3db 6c 20 74 79 70 65 73 20 66 6f 72 20 74 68 65 20  l types for the 
1b3dc 6e 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20  next element in 
1b3dd 65 61 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20  each key. */.   
1b3de 20 69 64 78 31 20 2b 3d 20 67 65 74 56 61 72 69   idx1 += getVari
1b3df 6e 74 33 32 28 20 61 4b 65 79 31 2b 69 64 78 31  nt32( aKey1+idx1
1b3e0 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 31 20 29  , serial_type1 )
1b3e1 3b 0a 20 20 20 20 69 66 28 20 64 31 3e 3d 6e 4b  ;.    if( d1>=nK
1b3e2 65 79 31 20 26 26 20 73 71 6c 69 74 65 33 56 64  ey1 && sqlite3Vd
1b3e3 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
1b3e4 73 65 72 69 61 6c 5f 74 79 70 65 31 29 3e 30 20  serial_type1)>0 
1b3e5 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a  ) break;..    /*
1b3e6 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61 6c   Extract the val
1b3e7 75 65 73 20 74 6f 20 62 65 20 63 6f 6d 70 61 72  ues to be compar
1b3e8 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64  ed..    */.    d
1b3e9 31 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65  1 += sqlite3Vdbe
1b3ea 53 65 72 69 61 6c 47 65 74 28 26 61 4b 65 79 31  SerialGet(&aKey1
1b3eb 5b 64 31 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70  [d1], serial_typ
1b3ec 65 31 2c 20 26 6d 65 6d 31 29 3b 0a 0a 20 20 20  e1, &mem1);..   
1b3ed 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 70 61   /* Do the compa
1b3ee 72 69 73 6f 6e 0a 20 20 20 20 2a 2f 0a 20 20 20  rison.    */.   
1b3ef 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d   rc = sqlite3Mem
1b3f0 43 6f 6d 70 61 72 65 28 26 6d 65 6d 31 2c 20 26  Compare(&mem1, &
1b3f1 70 50 4b 65 79 32 2d 3e 61 4d 65 6d 5b 69 5d 2c  pPKey2->aMem[i],
1b3f2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1b3f3 20 20 20 20 20 20 20 20 20 20 20 20 69 3c 6e 46              i<nF
1b3f4 69 65 6c 64 20 3f 20 70 4b 65 79 49 6e 66 6f 2d  ield ? pKeyInfo-
1b3f5 3e 61 43 6f 6c 6c 5b 69 5d 20 3a 20 30 29 3b 0a  >aColl[i] : 0);.
1b3f6 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b      if( rc!=0 ){
1b3f7 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1b3f8 20 20 7d 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d    }.    i++;.  }
1b3f9 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 65 6d 6f 72 79  ..  /* No memory
1b3fa 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 65   allocation is e
1b3fb 76 65 72 20 75 73 65 64 20 6f 6e 20 6d 65 6d 31  ver used on mem1
1b3fc 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52  . */.  if( NEVER
1b3fd 28 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 29 20 29  (mem1.zMalloc) )
1b3fe 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1b3ff 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a  elease(&mem1);..
1b400 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 46    /* If the PREF
1b401 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 69  IX_SEARCH flag i
1b402 73 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 69  s set and all fi
1b403 65 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 20  elds except the 
1b404 66 69 6e 61 6c 0a 20 20 2a 2a 20 72 6f 77 69 64  final.  ** rowid
1b405 20 66 69 65 6c 64 20 77 65 72 65 20 65 71 75 61   field were equa
1b406 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68  l, then clear th
1b407 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20  e PREFIX_SEARCH 
1b408 66 6c 61 67 20 61 6e 64 20 73 65 74 20 0a 20 20  flag and set .  
1b409 2a 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64  ** pPKey2->rowid
1b40a 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
1b40b 20 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64   the rowid field
1b40c 20 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b 65 79   in (pKey1, nKey
1b40d 31 29 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73  1)..  ** This is
1b40e 20 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f   used by the OP_
1b40f 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 2e  IsUnique opcode.
1b410 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 4b  .  */.  if( (pPK
1b411 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50  ey2->flags & UNP
1b412 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1b413 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b 65  RCH) && i==(pPKe
1b414 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29 7b  y2->nField-1) ){
1b415 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78  .    assert( idx
1b416 31 3d 3d 73 7a 48 64 72 31 20 26 26 20 72 63 20  1==szHdr1 && rc 
1b417 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  );.    assert( m
1b418 65 6d 31 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em1.flags & MEM_
1b419 49 6e 74 20 29 3b 0a 20 20 20 20 70 50 4b 65 79  Int );.    pPKey
1b41a 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50  2->flags &= ~UNP
1b41b 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1b41c 52 43 48 3b 0a 20 20 20 20 70 50 4b 65 79 32 2d  RCH;.    pPKey2-
1b41d 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 75 2e  >rowid = mem1.u.
1b41e 69 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  i;.  }..  if( rc
1b41f 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 72 63  ==0 ){.    /* rc
1b420 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 20 74  ==0 here means t
1b421 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 6b  hat one of the k
1b422 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 66  eys ran out of f
1b423 69 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 2a 2a  ields and.    **
1b424 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73 20   all the fields 
1b425 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e 74  up to that point
1b426 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66 20   were equal. If 
1b427 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43  the UNPACKED_INC
1b428 52 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c 61 67  RKEY.    ** flag
1b429 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 62 72   is set, then br
1b42a 65 61 6b 20 74 68 65 20 74 69 65 20 62 79 20 74  eak the tie by t
1b42b 72 65 61 74 69 6e 67 20 6b 65 79 32 20 61 73 20  reating key2 as 
1b42c 6c 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a 20 49  larger..    ** I
1b42d 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50 52  f the UPACKED_PR
1b42e 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67 20  EFIX_MATCH flag 
1b42f 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65 79  is set, then key
1b430 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70 72  s with common pr
1b431 65 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 61 72  efixes.    ** ar
1b432 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20  e considered to 
1b433 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 72  be equal.  Other
1b434 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 72  wise, the longer
1b435 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 20   key is the .   
1b436 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 20   ** larger.  As 
1b437 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20  it happens, the 
1b438 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 61  pPKey2 will alwa
1b439 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 72  ys be the longer
1b43a 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 65  .    ** if there
1b43b 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 65   is a difference
1b43c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1b43d 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26   pPKey2->flags &
1b43e 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45   UNPACKED_INCRKE
1b43f 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  Y ){.      rc = 
1b440 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  -1;.    }else if
1b441 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20  ( pPKey2->flags 
1b442 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  & UNPACKED_PREFI
1b443 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20 20  X_MATCH ){.     
1b444 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30 20   /* Leave rc==0 
1b445 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  */.    }else if(
1b446 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29 7b 0a   idx1<szHdr1 ){.
1b447 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20        rc = 1;.  
1b448 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
1b449 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f  pKeyInfo->aSortO
1b44a 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49 6e  rder && i<pKeyIn
1b44b 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20 20  fo->nField.     
1b44c 20 20 20 20 20 20 20 20 20 20 26 26 20 70 4b 65            && pKe
1b44d 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65  yInfo->aSortOrde
1b44e 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d  r[i] ){.    rc =
1b44f 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74   -rc;.  }..  ret
1b450 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a  urn rc;.}. ../*.
1b451 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61  ** pCur points a
1b452 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79  t an index entry
1b453 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74   created using t
1b454 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  he OP_MakeRecord
1b455 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 61 64   opcode..** Read
1b456 20 74 68 65 20 72 6f 77 69 64 20 28 74 68 65 20   the rowid (the 
1b457 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74 68  last field in th
1b458 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73 74  e record) and st
1b459 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69 64  ore it in *rowid
1b45a 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  ..** Return SQLI
1b45b 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68  TE_OK if everyth
1b45c 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61 6e  ing works, or an
1b45d 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 65   error code othe
1b45e 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75  rwise..**.** pCu
1b45f 72 20 6d 69 67 68 74 20 62 65 20 70 6f 69 6e 74  r might be point
1b460 69 6e 67 20 74 6f 20 74 65 78 74 20 6f 62 74 61  ing to text obta
1b461 69 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f 72 72  ined from a corr
1b462 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c  upt database fil
1b463 65 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63 6f 6e  e..** So the con
1b464 74 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 74  tent cannot be t
1b465 72 75 73 74 65 64 2e 20 20 44 6f 20 61 70 70 72  rusted.  Do appr
1b466 6f 70 72 69 61 74 65 20 63 68 65 63 6b 73 20 6f  opriate checks o
1b467 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a  n the content..*
1b468 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b469 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1b46a 49 64 78 52 6f 77 69 64 28 73 71 6c 69 74 65 33  IdxRowid(sqlite3
1b46b 20 2a 64 62 2c 20 42 74 43 75 72 73 6f 72 20 2a   *db, BtCursor *
1b46c 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77 69 64  pCur, i64 *rowid
1b46d 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65  ){.  i64 nCellKe
1b46e 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  y = 0;.  int rc;
1b46f 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20 20 20  .  u32 szHdr;   
1b470 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1b471 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  the header */.  
1b472 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b 20 20  u32 typeRowid;  
1b473 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65    /* Serial type
1b474 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f   of the rowid */
1b475 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69 64 3b  .  u32 lenRowid;
1b476 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1b477 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 4d  the rowid */.  M
1b478 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 55 4e 55 53  em m, v;..  UNUS
1b479 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 62 29  ED_PARAMETER(db)
1b47a 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20  ;..  /* Get the 
1b47b 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 64 65  size of the inde
1b47c 78 20 65 6e 74 72 79 2e 20 20 4f 6e 6c 79 20 69  x entry.  Only i
1b47d 6e 64 69 63 65 73 20 65 6e 74 72 69 65 73 20 6f  ndices entries o
1b47e 66 20 6c 65 73 73 0a 20 20 2a 2a 20 74 68 61 6e  f less.  ** than
1b47f 20 32 47 69 42 20 61 72 65 20 73 75 70 70 6f 72   2GiB are suppor
1b480 74 20 2d 20 61 6e 79 74 68 69 6e 67 20 6c 61 72  t - anything lar
1b481 67 65 20 6d 75 73 74 20 62 65 20 64 61 74 61 62  ge must be datab
1b482 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a  ase corruption..
1b483 20 20 2a 2a 20 41 6e 79 20 63 6f 72 72 75 70 74    ** Any corrupt
1b484 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 20  ion is detected 
1b485 69 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  in sqlite3BtreeP
1b486 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 74  arseCellPtr(), t
1b487 68 6f 75 67 68 2c 20 73 6f 0a 20 20 2a 2a 20 74  hough, so.  ** t
1b488 68 69 73 20 63 6f 64 65 20 63 61 6e 20 73 61 66  his code can saf
1b489 65 6c 79 20 61 73 73 75 6d 65 20 74 68 61 74 20  ely assume that 
1b48a 6e 43 65 6c 6c 4b 65 79 20 69 73 20 33 32 2d 62  nCellKey is 32-b
1b48b 69 74 73 20 20 0a 20 20 2a 2f 0a 20 20 61 73 73  its  .  */.  ass
1b48c 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1b48d 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70  eCursorIsValid(p
1b48e 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 73  Cur) );.  rc = s
1b48f 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69  qlite3BtreeKeySi
1b490 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b  ze(pCur, &nCellK
1b491 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  ey);.  assert( r
1b492 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20  c==SQLITE_OK ); 
1b493 20 20 20 20 2f 2a 20 70 43 75 72 20 69 73 20 61      /* pCur is a
1b494 6c 77 61 79 73 20 76 61 6c 69 64 20 73 6f 20 4b  lways valid so K
1b495 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66 61  eySize cannot fa
1b496 69 6c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  il */.  assert( 
1b497 28 6e 43 65 6c 6c 4b 65 79 20 26 20 53 51 4c 49  (nCellKey & SQLI
1b498 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28 75 36  TE_MAX_U32)==(u6
1b499 34 29 6e 43 65 6c 6c 4b 65 79 20 29 3b 0a 0a 20  4)nCellKey );.. 
1b49a 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20   /* Read in the 
1b49b 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74  complete content
1b49c 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e   of the index en
1b49d 74 72 79 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  try */.  memset(
1b49e 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29  &m, 0, sizeof(m)
1b49f 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1b4a0 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65  3VdbeMemFromBtre
1b4a1 65 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29  e(pCur, 0, (int)
1b4a2 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29  nCellKey, 1, &m)
1b4a3 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
1b4a4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
1b4a5 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78  ..  /* The index
1b4a6 20 65 6e 74 72 79 20 6d 75 73 74 20 62 65 67 69   entry must begi
1b4a7 6e 20 77 69 74 68 20 61 20 68 65 61 64 65 72 20  n with a header 
1b4a8 73 69 7a 65 20 2a 2f 0a 20 20 28 76 6f 69 64 29  size */.  (void)
1b4a9 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a  getVarint32((u8*
1b4aa 29 6d 2e 7a 2c 20 73 7a 48 64 72 29 3b 0a 20 20  )m.z, szHdr);.  
1b4ab 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d  testcase( szHdr=
1b4ac 3d 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  =3 );.  testcase
1b4ad 28 20 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a  ( szHdr==m.n );.
1b4ae 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 73    if( unlikely(s
1b4af 7a 48 64 72 3c 33 20 7c 7c 20 28 69 6e 74 29 73  zHdr<3 || (int)s
1b4b0 7a 48 64 72 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20  zHdr>m.n) ){.   
1b4b1 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f   goto idx_rowid_
1b4b2 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a  corruption;.  }.
1b4b3 0a 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 66  .  /* The last f
1b4b4 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65  ield of the inde
1b4b5 78 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 69  x should be an i
1b4b6 6e 74 65 67 65 72 20 2d 20 74 68 65 20 52 4f 57  nteger - the ROW
1b4b7 49 44 2e 0a 20 20 2a 2a 20 56 65 72 69 66 79 20  ID..  ** Verify 
1b4b8 74 68 61 74 20 74 68 65 20 6c 61 73 74 20 65 6e  that the last en
1b4b9 74 72 79 20 72 65 61 6c 6c 79 20 69 73 20 61 6e  try really is an
1b4ba 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 28   integer. */.  (
1b4bb 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32  void)getVarint32
1b4bc 28 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72  ((u8*)&m.z[szHdr
1b4bd 2d 31 5d 2c 20 74 79 70 65 52 6f 77 69 64 29 3b  -1], typeRowid);
1b4be 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
1b4bf 65 52 6f 77 69 64 3d 3d 31 20 29 3b 0a 20 20 74  eRowid==1 );.  t
1b4c0 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
1b4c1 69 64 3d 3d 32 20 29 3b 0a 20 20 74 65 73 74 63  id==2 );.  testc
1b4c2 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
1b4c3 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  3 );.  testcase(
1b4c4 20 74 79 70 65 52 6f 77 69 64 3d 3d 34 20 29 3b   typeRowid==4 );
1b4c5 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
1b4c6 65 52 6f 77 69 64 3d 3d 35 20 29 3b 0a 20 20 74  eRowid==5 );.  t
1b4c7 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
1b4c8 69 64 3d 3d 36 20 29 3b 0a 20 20 74 65 73 74 63  id==6 );.  testc
1b4c9 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
1b4ca 38 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  8 );.  testcase(
1b4cb 20 74 79 70 65 52 6f 77 69 64 3d 3d 39 20 29 3b   typeRowid==9 );
1b4cc 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28  .  if( unlikely(
1b4cd 74 79 70 65 52 6f 77 69 64 3c 31 20 7c 7c 20 74  typeRowid<1 || t
1b4ce 79 70 65 52 6f 77 69 64 3e 39 20 7c 7c 20 74 79  ypeRowid>9 || ty
1b4cf 70 65 52 6f 77 69 64 3d 3d 37 29 20 29 7b 0a 20  peRowid==7) ){. 
1b4d0 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69     goto idx_rowi
1b4d1 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20  d_corruption;.  
1b4d2 7d 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73  }.  lenRowid = s
1b4d3 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1b4d4 54 79 70 65 4c 65 6e 28 74 79 70 65 52 6f 77 69  TypeLen(typeRowi
1b4d5 64 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20  d);.  testcase( 
1b4d6 28 75 33 32 29 6d 2e 6e 3d 3d 73 7a 48 64 72 2b  (u32)m.n==szHdr+
1b4d7 6c 65 6e 52 6f 77 69 64 20 29 3b 0a 20 20 69 66  lenRowid );.  if
1b4d8 28 20 75 6e 6c 69 6b 65 6c 79 28 28 75 33 32 29  ( unlikely((u32)
1b4d9 6d 2e 6e 3c 73 7a 48 64 72 2b 6c 65 6e 52 6f 77  m.n<szHdr+lenRow
1b4da 69 64 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  id) ){.    goto 
1b4db 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70  idx_rowid_corrup
1b4dc 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tion;.  }..  /* 
1b4dd 46 65 74 63 68 20 74 68 65 20 69 6e 74 65 67 65  Fetch the intege
1b4de 72 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66  r off the end of
1b4df 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f 72   the index recor
1b4e0 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64  d */.  sqlite3Vd
1b4e1 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a  beSerialGet((u8*
1b4e2 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77  )&m.z[m.n-lenRow
1b4e3 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64 2c 20  id], typeRowid, 
1b4e4 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20 3d 20  &v);.  *rowid = 
1b4e5 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74 65 33  v.u.i;.  sqlite3
1b4e6 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
1b4e7 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  m);.  return SQL
1b4e8 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75  ITE_OK;..  /* Ju
1b4e9 6d 70 20 68 65 72 65 20 69 66 20 64 61 74 61 62  mp here if datab
1b4ea 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69  ase corruption i
1b4eb 73 20 64 65 74 65 63 74 65 64 20 61 66 74 65 72  s detected after
1b4ec 20 6d 20 68 61 73 20 62 65 65 6e 0a 20 20 2a 2a   m has been.  **
1b4ed 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 72 65   allocated.  Fre
1b4ee 65 20 74 68 65 20 6d 20 6f 62 6a 65 63 74 20 61  e the m object a
1b4ef 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
1b4f0 5f 43 4f 52 52 55 50 54 2e 20 2a 2f 0a 69 64 78  _CORRUPT. */.idx
1b4f1 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f  _rowid_corruptio
1b4f2 6e 3a 0a 20 20 74 65 73 74 63 61 73 65 28 20 6d  n:.  testcase( m
1b4f3 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b 0a 20  .zMalloc!=0 );. 
1b4f4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1b4f5 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65  elease(&m);.  re
1b4f6 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1b4f7 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a 0a  UPT_BKPT;.}../*.
1b4f8 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 6b  ** Compare the k
1b4f9 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ey of the index 
1b4fa 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 6f  entry that curso
1b4fb 72 20 70 43 20 69 73 20 70 6f 69 6e 74 69 6e 67  r pC is pointing
1b4fc 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74   to against.** t
1b4fd 68 65 20 6b 65 79 20 73 74 72 69 6e 67 20 69 6e  he key string in
1b4fe 20 70 55 6e 70 61 63 6b 65 64 2e 20 20 57 72 69   pUnpacked.  Wri
1b4ff 74 65 20 69 6e 74 6f 20 2a 70 52 65 73 20 61 20  te into *pRes a 
1b500 6e 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74 20 69  number.** that i
1b501 73 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f  s negative, zero
1b502 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66  , or positive if
1b503 20 70 43 20 69 73 20 6c 65 73 73 20 74 68 61 6e   pC is less than
1b504 2c 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f  , equal to,.** o
1b505 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70  r greater than p
1b506 55 6e 70 61 63 6b 65 64 2e 20 20 52 65 74 75 72  Unpacked.  Retur
1b507 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
1b508 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 70 55  uccess..**.** pU
1b509 6e 70 61 63 6b 65 64 20 69 73 20 65 69 74 68 65  npacked is eithe
1b50a 72 20 63 72 65 61 74 65 64 20 77 69 74 68 6f 75  r created withou
1b50b 74 20 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20  t a rowid or is 
1b50c 74 72 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61  truncated so tha
1b50d 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68  t it.** omits th
1b50e 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65  e rowid at the e
1b50f 6e 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61  nd.  The rowid a
1b510 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
1b511 20 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20   index entry.** 
1b512 69 73 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65  is ignored as we
1b513 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73  ll.  Hence, this
1b514 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f   routine only co
1b515 6d 70 61 72 65 73 20 74 68 65 20 70 72 65 66 69  mpares the prefi
1b516 78 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b  xes .** of the k
1b517 65 79 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65  eys prior to the
1b518 20 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f   final rowid, no
1b519 74 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79  t the entire key
1b51a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1b51b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1b51c 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65  dbeIdxKeyCompare
1b51d 28 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  (.  VdbeCursor *
1b51e 70 43 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pC,             
1b51f 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f  /* The cursor to
1b520 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74   compare against
1b521 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65   */.  UnpackedRe
1b522 63 6f 72 64 20 2a 70 55 6e 70 61 63 6b 65 64 2c  cord *pUnpacked,
1b523 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 76 65    /* Unpacked ve
1b524 72 73 69 6f 6e 20 6f 66 20 6b 65 79 20 74 6f 20  rsion of key to 
1b525 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20  compare against 
1b526 2a 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20  */.  int *res   
1b527 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b528 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f   /* Write the co
1b529 6d 70 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20  mparison result 
1b52a 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34  here */.){.  i64
1b52b 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20   nCellKey = 0;. 
1b52c 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72   int rc;.  BtCur
1b52d 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e  sor *pCur = pC->
1b52e 70 43 75 72 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d  pCursor;.  Mem m
1b52f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
1b530 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
1b531 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a  sValid(pCur) );.
1b532 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1b533 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c  reeKeySize(pCur,
1b534 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 61   &nCellKey);.  a
1b535 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1b536 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 70 43  E_OK );    /* pC
1b537 75 72 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c  ur is always val
1b538 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 20 63 61  id so KeySize ca
1b539 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 2f  nnot fail */.  /
1b53a 2a 20 6e 43 65 6c 6c 4b 65 79 20 77 69 6c 6c 20  * nCellKey will 
1b53b 61 6c 77 61 79 73 20 62 65 20 62 65 74 77 65 65  always be betwee
1b53c 6e 20 30 20 61 6e 64 20 30 78 66 66 66 66 66 66  n 0 and 0xffffff
1b53d 66 66 20 62 65 63 61 75 73 65 20 6f 66 20 74 68  ff because of th
1b53e 65 20 73 61 79 0a 20 20 2a 2a 20 74 68 61 74 20  e say.  ** that 
1b53f 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
1b540 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 47  r() and sqlite3G
1b541 65 74 56 61 72 69 6e 74 33 32 28 29 20 61 72 65  etVarint32() are
1b542 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 2a 2f 0a   implemented */.
1b543 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d    if( nCellKey<=
1b544 30 20 7c 7c 20 6e 43 65 6c 6c 4b 65 79 3e 30 78  0 || nCellKey>0x
1b545 37 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20  7fffffff ){.    
1b546 2a 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65  *res = 0;.    re
1b547 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1b548 55 50 54 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  UPT;.  }.  memse
1b549 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  t(&m, 0, sizeof(
1b54a 6d 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  m));.  rc = sqli
1b54b 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
1b54c 72 65 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c  ree(pC->pCursor,
1b54d 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65   0, (int)nCellKe
1b54e 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 28  y, 1, &m);.  if(
1b54f 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72   rc ){.    retur
1b550 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65  n rc;.  }.  asse
1b551 72 74 28 20 70 55 6e 70 61 63 6b 65 64 2d 3e 66  rt( pUnpacked->f
1b552 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f  lags & UNPACKED_
1b553 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29 3b 0a  IGNORE_ROWID );.
1b554 20 20 2a 72 65 73 20 3d 20 73 71 6c 69 74 65 33    *res = sqlite3
1b555 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72  VdbeRecordCompar
1b556 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c 20 70 55 6e 70  e(m.n, m.z, pUnp
1b557 61 63 6b 65 64 29 3b 0a 20 20 73 71 6c 69 74 65  acked);.  sqlite
1b558 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1b559 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  &m);.  return SQ
1b55a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1b55b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
1b55c 65 74 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f  ets the value to
1b55d 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20   be returned by 
1b55e 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1b55f 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63   to.** sqlite3_c
1b560 68 61 6e 67 65 73 28 29 20 6f 6e 20 74 68 65 20  hanges() on the 
1b561 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
1b562 27 64 62 27 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45  'db'. .*/.SQLITE
1b563 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1b564 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
1b565 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ges(sqlite3 *db,
1b566 20 69 6e 74 20 6e 43 68 61 6e 67 65 29 7b 0a 20   int nChange){. 
1b567 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b568 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
1b569 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 62 2d 3e  mutex) );.  db->
1b56a 6e 43 68 61 6e 67 65 20 3d 20 6e 43 68 61 6e 67  nChange = nChang
1b56b 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43  e;.  db->nTotalC
1b56c 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65  hange += nChange
1b56d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61  ;.}../*.** Set a
1b56e 20 66 6c 61 67 20 69 6e 20 74 68 65 20 76 64 62   flag in the vdb
1b56f 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20  e to update the 
1b570 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 77  change counter w
1b571 68 65 6e 20 69 74 20 69 73 20 66 69 6e 61 6c 69  hen it is finali
1b572 73 65 64 0a 2a 2a 20 6f 72 20 72 65 73 65 74 2e  sed.** or reset.
1b573 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b574 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1b575 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28  dbeCountChanges(
1b576 56 64 62 65 20 2a 76 29 7b 0a 20 20 76 2d 3e 63  Vdbe *v){.  v->c
1b577 68 61 6e 67 65 43 6e 74 4f 6e 20 3d 20 31 3b 0a  hangeCntOn = 1;.
1b578 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 65 76  }../*.** Mark ev
1b579 65 72 79 20 70 72 65 70 61 72 65 64 20 73 74 61  ery prepared sta
1b57a 74 65 6d 65 6e 74 20 61 73 73 6f 63 69 61 74 65  tement associate
1b57b 64 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73  d with a databas
1b57c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
1b57d 61 73 20 65 78 70 69 72 65 64 2e 0a 2a 2a 0a 2a  as expired..**.*
1b57e 2a 20 41 6e 20 65 78 70 69 72 65 64 20 73 74 61  * An expired sta
1b57f 74 65 6d 65 6e 74 20 6d 65 61 6e 73 20 74 68 61  tement means tha
1b580 74 20 72 65 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  t recompilation 
1b581 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  of the statement
1b582 20 69 73 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64   is.** recommend
1b583 2e 20 20 53 74 61 74 65 6d 65 6e 74 73 20 65 78  .  Statements ex
1b584 70 69 72 65 20 77 68 65 6e 20 74 68 69 6e 67 73  pire when things
1b585 20 68 61 70 70 65 6e 20 74 68 61 74 20 6d 61 6b   happen that mak
1b586 65 20 74 68 65 69 72 0a 2a 2a 20 70 72 6f 67 72  e their.** progr
1b587 61 6d 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 52  ams obsolete.  R
1b588 65 6d 6f 76 69 6e 67 20 75 73 65 72 2d 64 65 66  emoving user-def
1b589 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 6f  ined functions o
1b58a 72 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73  r collating.** s
1b58b 65 71 75 65 6e 63 65 73 2c 20 6f 72 20 63 68 61  equences, or cha
1b58c 6e 67 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69  nging an authori
1b58d 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  zation function 
1b58e 61 72 65 20 74 68 65 20 74 79 70 65 73 20 6f 66  are the types of
1b58f 0a 2a 2a 20 74 68 69 6e 67 73 20 74 68 61 74 20  .** things that 
1b590 6d 61 6b 65 20 70 72 65 70 61 72 65 64 20 73 74  make prepared st
1b591 61 74 65 6d 65 6e 74 73 20 6f 62 73 6f 6c 65 74  atements obsolet
1b592 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1b593 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1b594 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
1b595 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65  tatements(sqlite
1b596 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a  3 *db){.  Vdbe *
1b597 70 3b 0a 20 20 66 6f 72 28 70 20 3d 20 64 62 2d  p;.  for(p = db-
1b598 3e 70 56 64 62 65 3b 20 70 3b 20 70 3d 70 2d 3e  >pVdbe; p; p=p->
1b599 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 65  pNext){.    p->e
1b59a 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  xpired = 1;.  }.
1b59b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1b59c 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73  the database ass
1b59d 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
1b59e 20 56 64 62 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   Vdbe..*/.SQLITE
1b59f 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
1b5a0 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28   *sqlite3VdbeDb(
1b5a1 56 64 62 65 20 2a 76 29 7b 0a 20 20 72 65 74 75  Vdbe *v){.  retu
1b5a2 72 6e 20 76 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a  rn v->db;.}../*.
1b5a3 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
1b5a4 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65  ter to an sqlite
1b5a5 33 5f 76 61 6c 75 65 20 73 74 72 75 63 74 75 72  3_value structur
1b5a6 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
1b5a7 20 76 61 6c 75 65 20 62 6f 75 6e 64 0a 2a 2a 20   value bound.** 
1b5a8 70 61 72 61 6d 65 74 65 72 20 69 56 61 72 20 6f  parameter iVar o
1b5a9 66 20 56 4d 20 76 2e 20 45 78 63 65 70 74 2c 20  f VM v. Except, 
1b5aa 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  if the value is 
1b5ab 61 6e 20 53 51 4c 20 4e 55 4c 4c 2c 20 72 65 74  an SQL NULL, ret
1b5ac 75 72 6e 20 0a 2a 2a 20 30 20 69 6e 73 74 65 61  urn .** 0 instea
1b5ad 64 2e 20 55 6e 6c 65 73 73 20 69 74 20 69 73 20  d. Unless it is 
1b5ae 4e 55 4c 4c 2c 20 61 70 70 6c 79 20 61 66 66 69  NULL, apply affi
1b5af 6e 69 74 79 20 61 66 66 20 28 6f 6e 65 20 6f 66  nity aff (one of
1b5b0 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f   the SQLITE_AFF_
1b5b1 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74 73 29 20  *.** constants) 
1b5b2 74 6f 20 74 68 65 20 76 61 6c 75 65 20 62 65 66  to the value bef
1b5b3 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 69 74  ore returning it
1b5b4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
1b5b5 72 6e 65 64 20 76 61 6c 75 65 20 6d 75 73 74 20  rned value must 
1b5b6 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20  be freed by the 
1b5b7 63 61 6c 6c 65 72 20 75 73 69 6e 67 20 73 71 6c  caller using sql
1b5b8 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 29 2e  ite3ValueFree().
1b5b9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b5ba 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  TE sqlite3_value
1b5bb 20 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74   *sqlite3VdbeGet
1b5bc 56 61 6c 75 65 28 56 64 62 65 20 2a 76 2c 20 69  Value(Vdbe *v, i
1b5bd 6e 74 20 69 56 61 72 2c 20 75 38 20 61 66 66 29  nt iVar, u8 aff)
1b5be 7b 0a 20 20 61 73 73 65 72 74 28 20 69 56 61 72  {.  assert( iVar
1b5bf 3e 30 20 29 3b 0a 20 20 69 66 28 20 76 20 29 7b  >0 );.  if( v ){
1b5c0 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d  .    Mem *pMem =
1b5c1 20 26 76 2d 3e 61 56 61 72 5b 69 56 61 72 2d 31   &v->aVar[iVar-1
1b5c2 5d 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70  ];.    if( 0==(p
1b5c3 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
1b5c4 5f 4e 75 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20  _Null) ){.      
1b5c5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1b5c6 52 65 74 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  Ret = sqlite3Val
1b5c7 75 65 4e 65 77 28 76 2d 3e 64 62 29 3b 0a 20 20  ueNew(v->db);.  
1b5c8 20 20 20 20 69 66 28 20 70 52 65 74 20 29 7b 0a      if( pRet ){.
1b5c9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1b5ca 64 62 65 4d 65 6d 43 6f 70 79 28 28 4d 65 6d 20  dbeMemCopy((Mem 
1b5cb 2a 29 70 52 65 74 2c 20 70 4d 65 6d 29 3b 0a 20  *)pRet, pMem);. 
1b5cc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61         sqlite3Va
1b5cd 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79  lueApplyAffinity
1b5ce 28 70 52 65 74 2c 20 61 66 66 2c 20 53 51 4c 49  (pRet, aff, SQLI
1b5cf 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20  TE_UTF8);.      
1b5d0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b5d1 53 74 6f 72 65 54 79 70 65 28 28 4d 65 6d 20 2a  StoreType((Mem *
1b5d2 29 70 52 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a  )pRet);.      }.
1b5d3 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 52 65        return pRe
1b5d4 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  t;.    }.  }.  r
1b5d5 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
1b5d6 2a 20 43 6f 6e 66 69 67 75 72 65 20 53 51 4c 20  * Configure SQL 
1b5d7 76 61 72 69 61 62 6c 65 20 69 56 61 72 20 73 6f  variable iVar so
1b5d8 20 74 68 61 74 20 62 69 6e 64 69 6e 67 20 61 20   that binding a 
1b5d9 6e 65 77 20 76 61 6c 75 65 20 74 6f 20 69 74 20  new value to it 
1b5da 73 69 67 6e 61 6c 73 0a 2a 2a 20 74 6f 20 73 71  signals.** to sq
1b5db 6c 69 74 65 33 5f 72 65 6f 70 74 69 6d 69 7a 65  lite3_reoptimize
1b5dc 28 29 20 74 68 61 74 20 72 65 2d 70 72 65 70 61  () that re-prepa
1b5dd 72 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65  ring the stateme
1b5de 6e 74 20 6d 61 79 20 72 65 73 75 6c 74 0a 2a 2a  nt may result.**
1b5df 20 69 6e 20 61 20 62 65 74 74 65 72 20 71 75 65   in a better que
1b5e0 72 79 20 70 6c 61 6e 2e 0a 2a 2f 0a 53 51 4c 49  ry plan..*/.SQLI
1b5e1 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1b5e2 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61  sqlite3VdbeSetVa
1b5e3 72 6d 61 73 6b 28 56 64 62 65 20 2a 76 2c 20 69  rmask(Vdbe *v, i
1b5e4 6e 74 20 69 56 61 72 29 7b 0a 20 20 61 73 73 65  nt iVar){.  asse
1b5e5 72 74 28 20 69 56 61 72 3e 30 20 29 3b 0a 20 20  rt( iVar>0 );.  
1b5e6 69 66 28 20 69 56 61 72 3e 33 32 20 29 7b 0a 20  if( iVar>32 ){. 
1b5e7 20 20 20 76 2d 3e 65 78 70 6d 61 73 6b 20 3d 20     v->expmask = 
1b5e8 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 7d 65  0xffffffff;.  }e
1b5e9 6c 73 65 7b 0a 20 20 20 20 76 2d 3e 65 78 70 6d  lse{.    v->expm
1b5ea 61 73 6b 20 7c 3d 20 28 28 75 33 32 29 31 20 3c  ask |= ((u32)1 <
1b5eb 3c 20 28 69 56 61 72 2d 31 29 29 3b 0a 20 20 7d  < (iVar-1));.  }
1b5ec 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
1b5ed 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61  *** End of vdbea
1b5ee 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ux.c ***********
1b5ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5f1 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1b5f2 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76  *** Begin file v
1b5f3 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a  dbeapi.c *******
1b5f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5f6 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d  **/./*.** 2004 M
1b5f7 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 26.**.** The 
1b5f8 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1b5f9 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1b5fa 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1b5fb 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1b5fc 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1b5fd 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1b5fe 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1b5ff 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1b600 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1b601 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1b602 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1b603 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1b604 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1b605 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1b606 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1b607 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1b608 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1b609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b60a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b60b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b60c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b60d 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
1b60e 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
1b60f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 6d  de use to implem
1b610 65 6e 74 20 41 50 49 73 20 74 68 61 74 20 61 72  ent APIs that ar
1b611 65 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a  e part of the.**
1b612 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64   VDBE..**.** $Id
1b613 3a 20 76 64 62 65 61 70 69 2e 63 2c 76 20 31 2e  : vdbeapi.c,v 1.
1b614 31 36 37 20 32 30 30 39 2f 30 36 2f 32 35 20 30  167 2009/06/25 0
1b615 31 3a 34 37 3a 31 32 20 64 72 68 20 45 78 70 20  1:47:12 drh Exp 
1b616 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  $.*/..#ifndef SQ
1b617 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
1b618 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ATED./*.** Retur
1b619 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f  n TRUE (non-zero
1b61a 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65  ) of the stateme
1b61b 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 61  nt supplied as a
1b61c 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64 73  n argument needs
1b61d 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d 70  .** to be recomp
1b61e 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d 65  iled.  A stateme
1b61f 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  nt needs to be r
1b620 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65 76  ecompiled whenev
1b621 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75 74  er the.** execut
1b622 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20  ion environment 
1b623 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61 79  changes in a way
1b624 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74 65   that would alte
1b625 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a  r the program.**
1b626 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70 72   that sqlite3_pr
1b627 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 74 65  epare() generate
1b628 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  s.  For example,
1b629 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e   if new function
1b62a 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e  s or.** collatin
1b62b 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 20  g sequences are 
1b62c 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 69 66  registered or if
1b62d 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 66   an authorizer f
1b62e 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 64  unction is.** ad
1b62f 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e 0a  ded or changed..
1b630 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1b631 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65  t sqlite3_expire
1b632 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  d(sqlite3_stmt *
1b633 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a  pStmt){.  Vdbe *
1b634 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  p = (Vdbe*)pStmt
1b635 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20  ;.  return p==0 
1b636 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a 7d  || p->expired;.}
1b637 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
1b638 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
1b639 74 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20  tine destroys a 
1b63a 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
1b63b 74 68 61 74 20 69 73 20 63 72 65 61 74 65 64 20  that is created 
1b63c 62 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65  by.** the sqlite
1b63d 33 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75 74  3_compile() rout
1b63e 69 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65 72  ine. The integer
1b63f 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e 20   returned is an 
1b640 53 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63 65  SQLITE_.** succe
1b641 73 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65 20  ss/failure code 
1b642 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
1b643 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78 65  he result of exe
1b644 63 75 74 69 6e 67 20 74 68 65 20 76 69 72 74 75  cuting the virtu
1b645 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a 2a  al.** machine..*
1b646 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1b647 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72  e sets the error
1b648 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67   code and string
1b649 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20   returned by.** 
1b64a 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
1b64b 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  ), sqlite3_errms
1b64c 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  g() and sqlite3_
1b64d 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53  errmsg16()..*/.S
1b64e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1b64f 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73  lite3_finalize(s
1b650 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1b651 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  mt){.  int rc;. 
1b652 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b   if( pStmt==0 ){
1b653 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1b654 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
1b655 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62    Vdbe *v = (Vdb
1b656 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71  e*)pStmt;.    sq
1b657 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64  lite3 *db = v->d
1b658 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  b;.#if SQLITE_TH
1b659 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c  READSAFE.    sql
1b65a 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
1b65b 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 78  x = v->db->mutex
1b65c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c  ;.#endif.    sql
1b65d 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1b65e 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20  (mutex);.    rc 
1b65f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e  = sqlite3VdbeFin
1b660 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 72 63  alize(v);.    rc
1b661 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
1b662 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 73  t(db, rc);.    s
1b663 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1b664 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20  ve(mutex);.  }. 
1b665 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1b666 2a 0a 2a 2a 20 54 65 72 6d 69 6e 61 74 65 20 74  *.** Terminate t
1b667 68 65 20 63 75 72 72 65 6e 74 20 65 78 65 63 75  he current execu
1b668 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 73  tion of an SQL s
1b669 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 73  tatement and res
1b66a 65 74 20 69 74 0a 2a 2a 20 62 61 63 6b 20 74 6f  et it.** back to
1b66b 20 69 74 73 20 73 74 61 72 74 69 6e 67 20 73 74   its starting st
1b66c 61 74 65 20 73 6f 20 74 68 61 74 20 69 74 20 63  ate so that it c
1b66d 61 6e 20 62 65 20 72 65 75 73 65 64 2e 20 41 20  an be reused. A 
1b66e 73 75 63 63 65 73 73 20 63 6f 64 65 20 66 72 6f  success code fro
1b66f 6d 0a 2a 2a 20 74 68 65 20 70 72 69 6f 72 20 65  m.** the prior e
1b670 78 65 63 75 74 69 6f 6e 20 69 73 20 72 65 74 75  xecution is retu
1b671 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rned..**.** This
1b672 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
1b673 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  e error code and
1b674 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64   string returned
1b675 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65   by.** sqlite3_e
1b676 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 65  rrcode(), sqlite
1b677 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73  3_errmsg() and s
1b678 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
1b679 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
1b67a 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 73   int sqlite3_res
1b67b 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  et(sqlite3_stmt 
1b67c 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72  *pStmt){.  int r
1b67d 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d  c;.  if( pStmt==
1b67e 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  0 ){.    rc = SQ
1b67f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
1b680 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20  {.    Vdbe *v = 
1b681 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20  (Vdbe*)pStmt;.  
1b682 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1b683 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75 74  enter(v->db->mut
1b684 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  ex);.    rc = sq
1b685 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 76  lite3VdbeReset(v
1b686 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1b687 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 2d  beMakeReady(v, -
1b688 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
1b689 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
1b68a 72 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72 72  rc & (v->db->err
1b68b 4d 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a 20 20  Mask))==rc );.  
1b68c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
1b68d 69 45 78 69 74 28 76 2d 3e 64 62 2c 20 72 63 29  iExit(v->db, rc)
1b68e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1b68f 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d  tex_leave(v->db-
1b690 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72  >mutex);.  }.  r
1b691 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1b692 2a 2a 20 53 65 74 20 61 6c 6c 20 74 68 65 20 70  ** Set all the p
1b693 61 72 61 6d 65 74 65 72 73 20 69 6e 20 74 68 65  arameters in the
1b694 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74   compiled SQL st
1b695 61 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e  atement to NULL.
1b696 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1b697 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72  nt sqlite3_clear
1b698 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65  _bindings(sqlite
1b699 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  3_stmt *pStmt){.
1b69a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72    int i;.  int r
1b69b 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1b69c 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
1b69d 2a 29 70 53 74 6d 74 3b 0a 23 69 66 20 53 51 4c  *)pStmt;.#if SQL
1b69e 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20  ITE_THREADSAFE. 
1b69f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
1b6a0 6d 75 74 65 78 20 3d 20 28 28 56 64 62 65 2a 29  mutex = ((Vdbe*)
1b6a1 70 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74 65  pStmt)->db->mute
1b6a2 78 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  x;.#endif.  sqli
1b6a3 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1b6a4 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d  mutex);.  for(i=
1b6a5 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b  0; i<p->nVar; i+
1b6a6 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  +){.    sqlite3V
1b6a7 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 70  dbeMemRelease(&p
1b6a8 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20 20  ->aVar[i]);.    
1b6a9 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67 73  p->aVar[i].flags
1b6aa 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d   = MEM_Null;.  }
1b6ab 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70  .  if( p->isPrep
1b6ac 61 72 65 56 32 20 26 26 20 70 2d 3e 65 78 70 6d  areV2 && p->expm
1b6ad 61 73 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 65 78  ask ){.    p->ex
1b6ae 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  pired = 1;.  }. 
1b6af 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1b6b0 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72  eave(mutex);.  r
1b6b1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
1b6b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69  *********** sqli
1b6b4 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a  te3_value_  ****
1b6b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
1b6b7 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
1b6b8 74 69 6e 65 73 20 65 78 74 72 61 63 74 20 69 6e  tines extract in
1b6b9 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61  formation from a
1b6ba 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f   Mem or sqlite3_
1b6bb 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75 63 74 75  value.** structu
1b6bc 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  re..*/.SQLITE_AP
1b6bd 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
1b6be 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
1b6bf 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
1b6c0 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20  pVal){.  Mem *p 
1b6c1 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20  = (Mem*)pVal;.  
1b6c2 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 28  if( p->flags & (
1b6c3 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72  MEM_Blob|MEM_Str
1b6c4 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
1b6c5 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
1b6c6 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61  b(p);.    p->fla
1b6c7 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a  gs &= ~MEM_Str;.
1b6c8 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20      p->flags |= 
1b6c9 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65  MEM_Blob;.    re
1b6ca 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c  turn p->z;.  }el
1b6cb 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73  se{.    return s
1b6cc 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1b6cd 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53  t(pVal);.  }.}.S
1b6ce 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1b6cf 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1b6d0 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  s(sqlite3_value 
1b6d1 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e  *pVal){.  return
1b6d2 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74   sqlite3ValueByt
1b6d3 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  es(pVal, SQLITE_
1b6d4 55 54 46 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  UTF8);.}.SQLITE_
1b6d5 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1b6d6 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71  value_bytes16(sq
1b6d7 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1b6d8 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  l){.  return sql
1b6d9 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70  ite3ValueBytes(p
1b6da 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Val, SQLITE_UTF1
1b6db 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49  6NATIVE);.}.SQLI
1b6dc 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71  TE_API double sq
1b6dd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
1b6de 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le(sqlite3_value
1b6df 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72   *pVal){.  retur
1b6e0 6e 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  n sqlite3VdbeRea
1b6e1 6c 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61  lValue((Mem*)pVa
1b6e2 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  l);.}.SQLITE_API
1b6e3 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
1b6e4 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76  ue_int(sqlite3_v
1b6e5 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
1b6e6 65 74 75 72 6e 20 28 69 6e 74 29 73 71 6c 69 74  eturn (int)sqlit
1b6e7 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 28  e3VdbeIntValue((
1b6e8 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51  Mem*)pVal);.}.SQ
1b6e9 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f  LITE_API sqlite_
1b6ea 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 76 61  int64 sqlite3_va
1b6eb 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c 69 74 65  lue_int64(sqlite
1b6ec 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
1b6ed 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1b6ee 56 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 65  VdbeIntValue((Me
1b6ef 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49  m*)pVal);.}.SQLI
1b6f0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e 73  TE_API const uns
1b6f1 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c 69  igned char *sqli
1b6f2 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 73  te3_value_text(s
1b6f3 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
1b6f4 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63  al){.  return (c
1b6f5 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1b6f6 61 72 20 2a 29 73 71 6c 69 74 65 33 56 61 6c 75  ar *)sqlite3Valu
1b6f7 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49  eText(pVal, SQLI
1b6f8 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e  TE_UTF8);.}.#ifn
1b6f9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1b6fa 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
1b6fb 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
1b6fc 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
1b6fd 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  6(sqlite3_value*
1b6fe 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e   pVal){.  return
1b6ff 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78   sqlite3ValueTex
1b700 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55  t(pVal, SQLITE_U
1b701 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53  TF16NATIVE);.}.S
1b702 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
1b703 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61  void *sqlite3_va
1b704 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c  lue_text16be(sql
1b705 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
1b706 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
1b707 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
1b708 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  l, SQLITE_UTF16B
1b709 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  E);.}.SQLITE_API
1b70a 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
1b70b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
1b70c 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  6le(sqlite3_valu
1b70d 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e *pVal){.  retu
1b70e 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54  rn sqlite3ValueT
1b70f 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45  ext(pVal, SQLITE
1b710 5f 55 54 46 31 36 4c 45 29 3b 0a 7d 0a 23 65 6e  _UTF16LE);.}.#en
1b711 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1b712 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49  IT_UTF16 */.SQLI
1b713 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1b714 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 71  e3_value_type(sq
1b715 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61  lite3_value* pVa
1b716 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 61  l){.  return pVa
1b717 6c 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a  l->type;.}../***
1b718 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b719 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65  ********* sqlite
1b71a 33 5f 72 65 73 75 6c 74 5f 20 20 2a 2a 2a 2a 2a  3_result_  *****
1b71b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b71c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1b71d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
1b71e 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 79  ines are used by
1b71f 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75   user-defined fu
1b720 6e 63 74 69 6f 6e 73 20 74 6f 20 73 70 65 63 69  nctions to speci
1b721 66 79 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69  fy.** the functi
1b722 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a  on result..**.**
1b723 20 54 68 65 20 73 65 74 53 74 72 4f 72 45 72 72   The setStrOrErr
1b724 6f 72 28 29 20 66 75 6e 74 69 6f 6e 20 63 61 6c  or() funtion cal
1b725 6c 73 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  ls sqlite3VdbeMe
1b726 6d 53 65 74 53 74 72 28 29 20 74 6f 20 73 74 6f  mSetStr() to sto
1b727 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74  re the.** result
1b728 20 61 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20   as a string or 
1b729 62 6c 6f 62 20 62 75 74 20 69 66 20 74 68 65 20  blob but if the 
1b72a 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 69  string or blob i
1b72b 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 69 74 0a  s too large, it.
1b72c 2a 2a 20 74 68 65 6e 20 73 65 74 73 20 74 68 65  ** then sets the
1b72d 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 53   error code to S
1b72e 51 4c 49 54 45 5f 54 4f 4f 42 49 47 0a 2a 2f 0a  QLITE_TOOBIG.*/.
1b72f 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 52  static void setR
1b730 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28  esultStrOrError(
1b731 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1b732 78 74 20 2a 70 43 74 78 2c 20 20 2f 2a 20 46 75  xt *pCtx,  /* Fu
1b733 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a  nction context *
1b734 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1b735 7a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  z,          /* S
1b736 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f  tring pointer */
1b737 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20  .  int n,       
1b738 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
1b739 74 65 73 20 69 6e 20 73 74 72 69 6e 67 2c 20 6f  tes in string, o
1b73a 72 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20  r negative */.  
1b73b 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20  u8 enc,         
1b73c 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64          /* Encod
1b73d 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72  ing of z.  0 for
1b73e 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64   BLOBs */.  void
1b73f 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20   (*xDel)(void*) 
1b740 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f      /* Destructo
1b741 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  r function */.){
1b742 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
1b743 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74  beMemSetStr(&pCt
1b744 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c  x->s, z, n, enc,
1b745 20 78 44 65 6c 29 3d 3d 53 51 4c 49 54 45 5f 54   xDel)==SQLITE_T
1b746 4f 4f 42 49 47 20 29 7b 0a 20 20 20 20 73 71 6c  OOBIG ){.    sql
1b747 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
1b748 72 5f 74 6f 6f 62 69 67 28 70 43 74 78 29 3b 0a  r_toobig(pCtx);.
1b749 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49    }.}.SQLITE_API
1b74a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1b74b 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c  sult_blob(.  sql
1b74c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
1b74d 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69  tx, .  const voi
1b74e 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20  d *z, .  int n, 
1b74f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
1b750 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73  void *).){.  ass
1b751 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61  ert( n>=0 );.  a
1b752 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1b753 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1b754 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1b755 20 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72    setResultStrOr
1b756 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e  Error(pCtx, z, n
1b757 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51  , 0, xDel);.}.SQ
1b758 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1b759 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
1b75a 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ble(sqlite3_cont
1b75b 65 78 74 20 2a 70 43 74 78 2c 20 64 6f 75 62 6c  ext *pCtx, doubl
1b75c 65 20 72 56 61 6c 29 7b 0a 20 20 61 73 73 65 72  e rVal){.  asser
1b75d 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1b75e 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1b75f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
1b760 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44  lite3VdbeMemSetD
1b761 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e 73 2c 20  ouble(&pCtx->s, 
1b762 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rVal);.}.SQLITE_
1b763 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1b764 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71  _result_error(sq
1b765 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1b766 43 74 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Ctx, const char 
1b767 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73  *z, int n){.  as
1b768 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b769 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1b76a 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b76b 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d   pCtx->isError =
1b76c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1b76d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1b76e 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
1b76f 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
1b770 38 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  8, SQLITE_TRANSI
1b771 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  ENT);.}.#ifndef 
1b772 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1b773 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  6.SQLITE_API voi
1b774 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1b775 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33  _error16(sqlite3
1b776 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1b777 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69  const void *z, i
1b778 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28  nt n){.  assert(
1b779 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b77a 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b77b 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78  mutex) );.  pCtx
1b77c 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49  ->isError = SQLI
1b77d 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69  TE_ERROR;.  sqli
1b77e 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
1b77f 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c  (&pCtx->s, z, n,
1b780 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
1b781 49 56 45 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  IVE, SQLITE_TRAN
1b782 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66  SIENT);.}.#endif
1b783 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1b784 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1b785 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  int(sqlite3_cont
1b786 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69  ext *pCtx, int i
1b787 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Val){.  assert( 
1b788 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b789 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1b78a 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
1b78b 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
1b78c 34 28 26 70 43 74 78 2d 3e 73 2c 20 28 69 36 34  4(&pCtx->s, (i64
1b78d 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45  )iVal);.}.SQLITE
1b78e 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1b78f 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73  3_result_int64(s
1b790 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b791 70 43 74 78 2c 20 69 36 34 20 69 56 61 6c 29 7b  pCtx, i64 iVal){
1b792 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1b793 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1b794 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
1b795 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
1b796 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 43  eMemSetInt64(&pC
1b797 74 78 2d 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a  tx->s, iVal);.}.
1b798 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1b799 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
1b79a 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ull(sqlite3_cont
1b79b 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73  ext *pCtx){.  as
1b79c 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b79d 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1b79e 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b79f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1b7a0 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29  etNull(&pCtx->s)
1b7a1 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ;.}.SQLITE_API v
1b7a2 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1b7a3 6c 74 5f 74 65 78 74 28 0a 20 20 73 71 6c 69 74  lt_text(.  sqlit
1b7a4 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1b7a5 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
1b7a6 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 0a 20 20  *z, .  int n,.  
1b7a7 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
1b7a8 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74  d *).){.  assert
1b7a9 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1b7aa 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
1b7ab 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74  >mutex) );.  set
1b7ac 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72  ResultStrOrError
1b7ad 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c  (pCtx, z, n, SQL
1b7ae 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b  ITE_UTF8, xDel);
1b7af 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
1b7b0 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1b7b1 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1b7b2 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
1b7b3 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16(.  sqlite3_co
1b7b4 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20  ntext *pCtx, .  
1b7b5 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a  const void *z, .
1b7b6 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64    int n, .  void
1b7b7 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29   (*xDel)(void *)
1b7b8 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  .){.  assert( sq
1b7b9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1b7ba 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1b7bb 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75  ex) );.  setResu
1b7bc 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74  ltStrOrError(pCt
1b7bd 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f  x, z, n, SQLITE_
1b7be 55 54 46 31 36 4e 41 54 49 56 45 2c 20 78 44 65  UTF16NATIVE, xDe
1b7bf 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  l);.}.SQLITE_API
1b7c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1b7c1 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 0a 20  sult_text16be(. 
1b7c2 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1b7c3 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74   *pCtx, .  const
1b7c4 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74   void *z, .  int
1b7c5 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44   n, .  void (*xD
1b7c6 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20  el)(void *).){. 
1b7c7 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b7c8 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
1b7c9 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
1b7ca 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 72  ;.  setResultStr
1b7cb 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c  OrError(pCtx, z,
1b7cc 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36   n, SQLITE_UTF16
1b7cd 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c  BE, xDel);.}.SQL
1b7ce 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1b7cf 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
1b7d0 31 36 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  16le(.  sqlite3_
1b7d1 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a  context *pCtx, .
1b7d2 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c    const void *z,
1b7d3 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f   .  int n, .  vo
1b7d4 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20  id (*xDel)(void 
1b7d5 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20  *).){.  assert( 
1b7d6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b7d7 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1b7d8 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65  utex) );.  setRe
1b7d9 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70  sultStrOrError(p
1b7da 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54  Ctx, z, n, SQLIT
1b7db 45 5f 55 54 46 31 36 4c 45 2c 20 78 44 65 6c 29  E_UTF16LE, xDel)
1b7dc 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1b7dd 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
1b7de 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
1b7df 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1b7e0 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  t_value(sqlite3_
1b7e1 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 73  context *pCtx, s
1b7e2 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
1b7e3 61 6c 75 65 29 7b 0a 20 20 61 73 73 65 72 74 28  alue){.  assert(
1b7e4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b7e5 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b7e6 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69  mutex) );.  sqli
1b7e7 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 26  te3VdbeMemCopy(&
1b7e8 70 43 74 78 2d 3e 73 2c 20 70 56 61 6c 75 65 29  pCtx->s, pValue)
1b7e9 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ;.}.SQLITE_API v
1b7ea 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1b7eb 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69  lt_zeroblob(sqli
1b7ec 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
1b7ed 78 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73  x, int n){.  ass
1b7ee 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b7ef 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1b7f0 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1b7f1 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1b7f2 74 5a 65 72 6f 42 6c 6f 62 28 26 70 43 74 78 2d  tZeroBlob(&pCtx-
1b7f3 3e 73 2c 20 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45  >s, n);.}.SQLITE
1b7f4 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1b7f5 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
1b7f6 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ode(sqlite3_cont
1b7f7 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 65  ext *pCtx, int e
1b7f8 72 72 43 6f 64 65 29 7b 0a 20 20 70 43 74 78 2d  rrCode){.  pCtx-
1b7f9 3e 69 73 45 72 72 6f 72 20 3d 20 65 72 72 43 6f  >isError = errCo
1b7fa 64 65 3b 0a 20 20 69 66 28 20 70 43 74 78 2d 3e  de;.  if( pCtx->
1b7fb 73 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  s.flags & MEM_Nu
1b7fc 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ll ){.    sqlite
1b7fd 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26  3VdbeMemSetStr(&
1b7fe 70 43 74 78 2d 3e 73 2c 20 73 71 6c 69 74 65 33  pCtx->s, sqlite3
1b7ff 45 72 72 53 74 72 28 65 72 72 43 6f 64 65 29 2c  ErrStr(errCode),
1b800 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20   -1, .          
1b801 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
1b802 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
1b803 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a  TE_STATIC);.  }.
1b804 7d 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53  }../* Force an S
1b805 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72 72  QLITE_TOOBIG err
1b806 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  or. */.SQLITE_AP
1b807 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1b808 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
1b809 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ig(sqlite3_conte
1b80a 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73  xt *pCtx){.  ass
1b80b 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b80c 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1b80d 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1b80e 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20  pCtx->isError = 
1b80f 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20  SQLITE_TOOBIG;. 
1b810 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1b811 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
1b812 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20  "string or blob 
1b813 74 6f 6f 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20  too big", -1, . 
1b814 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b815 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46        SQLITE_UTF
1b816 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  8, SQLITE_STATIC
1b817 29 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49  );.}../* An SQLI
1b818 54 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20  TE_NOMEM error. 
1b819 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
1b81a 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1b81b 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71  t_error_nomem(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 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Ctx){.  assert( 
1b81e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b81f 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1b820 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
1b821 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
1b822 28 26 70 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43  (&pCtx->s);.  pC
1b823 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51  tx->isError = SQ
1b824 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43  LITE_NOMEM;.  pC
1b825 74 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63  tx->s.db->malloc
1b826 46 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f  Failed = 1;.}../
1b827 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65  *.** Execute the
1b828 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74   statement pStmt
1b829 2c 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20 61  , either until a
1b82a 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20   row of data is 
1b82b 72 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74  ready, the.** st
1b82c 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c  atement is compl
1b82d 65 74 65 6c 79 20 65 78 65 63 75 74 65 64 20 6f  etely executed o
1b82e 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  r an error occur
1b82f 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
1b830 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73  utine implements
1b831 20 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 65   the bulk of the
1b832 20 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 68   logic behind th
1b833 65 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a  e sqlite_step().
1b834 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c  ** API.  The onl
1b835 79 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 20  y thing omitted 
1b836 69 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63  is the automatic
1b837 20 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 20   recompile if a 
1b838 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67  .** schema chang
1b839 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20  e has occurred. 
1b83a 20 54 68 61 74 20 64 65 74 61 69 6c 20 69 73 20   That detail is 
1b83b 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a  handled by the.*
1b83c 2a 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33 5f  * outer sqlite3_
1b83d 73 74 65 70 28 29 20 77 72 61 70 70 65 72 20 70  step() wrapper p
1b83e 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61  rocedure..*/.sta
1b83f 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 53  tic int sqlite3S
1b840 74 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  tep(Vdbe *p){.  
1b841 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69  sqlite3 *db;.  i
1b842 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
1b843 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61  (p);.  if( p->ma
1b844 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic!=VDBE_MAGIC_
1b845 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  RUN ){.    retur
1b846 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
1b847 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72  .  }..  /* Asser
1b848 74 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20  t that malloc() 
1b849 68 61 73 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a  has not failed *
1b84a 2f 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  /.  db = p->db;.
1b84b 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
1b84c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
1b84d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
1b84e 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d  M;.  }..  if( p-
1b84f 3e 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70  >pc<=0 && p->exp
1b850 69 72 65 64 20 29 7b 0a 20 20 20 20 69 66 28 20  ired ){.    if( 
1b851 41 4c 57 41 59 53 28 70 2d 3e 72 63 3d 3d 53 51  ALWAYS(p->rc==SQ
1b852 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63  LITE_OK || p->rc
1b853 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 29  ==SQLITE_SCHEMA)
1b854 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20   ){.      p->rc 
1b855 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b  = SQLITE_SCHEMA;
1b856 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
1b857 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1b858 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74    goto end_of_st
1b859 65 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  ep;.  }.  if( sq
1b85a 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1b85b 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  ) ){.    p->rc =
1b85c 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
1b85d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1b85e 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
1b85f 69 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20  if( p->pc<0 ){. 
1b860 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61     /* If there a
1b861 72 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74  re no other stat
1b862 65 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79  ements currently
1b863 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20   running, then. 
1b864 20 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20     ** reset the 
1b865 69 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20  interrupt flag. 
1b866 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61   This prevents a
1b867 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
1b868 5f 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a  _interrupt.    *
1b869 2a 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74  * from interrupt
1b86a 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20  ing a statement 
1b86b 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74  that has not yet
1b86c 20 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f   started..    */
1b86d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74  .    if( db->act
1b86e 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b  iveVdbeCnt==0 ){
1b86f 0a 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73  .      db->u1.is
1b870 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b  Interrupted = 0;
1b871 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65  .    }..    asse
1b872 72 74 28 20 64 62 2d 3e 77 72 69 74 65 56 64 62  rt( db->writeVdb
1b873 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75  eCnt>0 || db->au
1b874 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64  toCommit==0 || d
1b875 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73  b->nDeferredCons
1b876 3d 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20  ==0 );..#ifndef 
1b877 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1b878 45 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50  E.    if( db->xP
1b879 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69  rofile && !db->i
1b87a 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
1b87b 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20    double rNow;. 
1b87c 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75       sqlite3OsCu
1b87d 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56  rrentTime(db->pV
1b87e 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20  fs, &rNow);.    
1b87f 20 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d    p->startTime =
1b880 20 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28   (u64)((rNow - (
1b881 69 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30  int)rNow)*3600.0
1b882 2a 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30  *24.0*1000000000
1b883 2e 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  .0);.    }.#endi
1b884 66 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76  f..    db->activ
1b885 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20  eVdbeCnt++;.    
1b886 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d  if( p->readOnly=
1b887 3d 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64  =0 ) db->writeVd
1b888 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e  beCnt++;.    p->
1b889 70 63 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e  pc = 0;.  }.#ifn
1b88a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1b88b 45 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 70 2d  EXPLAIN.  if( p-
1b88c 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20  >explain ){.    
1b88d 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1b88e 4c 69 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73 65  List(p);.  }else
1b88f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1b890 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a  E_OMIT_EXPLAIN *
1b891 2f 0a 20 20 7b 0a 20 20 20 20 72 63 20 3d 20 73  /.  {.    rc = s
1b892 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 70  qlite3VdbeExec(p
1b893 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71  );.  }..  if( sq
1b894 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1b895 62 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  b) ){.    rc = S
1b896 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
1b897 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1b898 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 2f  E_OMIT_TRACE.  /
1b899 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 6f  * Invoke the pro
1b89a 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 66  file callback if
1b89b 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 20 20   there is one.  
1b89c 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  */.  if( rc!=SQL
1b89d 49 54 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e 78  ITE_ROW && db->x
1b89e 50 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e  Profile && !db->
1b89f 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70 2d 3e  init.busy && p->
1b8a0 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 75 62  zSql ){.    doub
1b8a1 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 75 36 34  le rNow;.    u64
1b8a2 20 65 6c 61 70 73 65 54 69 6d 65 3b 0a 0a 20 20   elapseTime;..  
1b8a3 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65    sqlite3OsCurre
1b8a4 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c  ntTime(db->pVfs,
1b8a5 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 65 6c 61   &rNow);.    ela
1b8a6 70 73 65 54 69 6d 65 20 3d 20 28 75 36 34 29 28  pseTime = (u64)(
1b8a7 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f  (rNow - (int)rNo
1b8a8 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31  w)*3600.0*24.0*1
1b8a9 30 30 30 30 30 30 30 30 30 2e 30 29 3b 0a 20 20  000000000.0);.  
1b8aa 20 20 65 6c 61 70 73 65 54 69 6d 65 20 2d 3d 20    elapseTime -= 
1b8ab 70 2d 3e 73 74 61 72 74 54 69 6d 65 3b 0a 20 20  p->startTime;.  
1b8ac 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 28 64    db->xProfile(d
1b8ad 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 2c 20  b->pProfileArg, 
1b8ae 70 2d 3e 7a 53 71 6c 2c 20 65 6c 61 70 73 65 54  p->zSql, elapseT
1b8af 69 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ime);.  }.#endif
1b8b0 0a 0a 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20  ..  db->errCode 
1b8b1 3d 20 72 63 3b 0a 20 20 69 66 28 20 53 51 4c 49  = rc;.  if( SQLI
1b8b2 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65  TE_NOMEM==sqlite
1b8b3 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20  3ApiExit(p->db, 
1b8b4 70 2d 3e 72 63 29 20 29 7b 0a 20 20 20 20 70 2d  p->rc) ){.    p-
1b8b5 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  >rc = SQLITE_NOM
1b8b6 45 4d 3b 0a 20 20 7d 0a 65 6e 64 5f 6f 66 5f 73  EM;.  }.end_of_s
1b8b7 74 65 70 3a 0a 20 20 2f 2a 20 41 74 20 74 68 69  tep:.  /* At thi
1b8b8 73 20 70 6f 69 6e 74 20 6c 6f 63 61 6c 20 76 61  s point local va
1b8b9 72 69 61 62 6c 65 20 72 63 20 68 6f 6c 64 73 20  riable rc holds 
1b8ba 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 73  the value that s
1b8bb 68 6f 75 6c 64 20 62 65 20 0a 20 20 2a 2a 20 72  hould be .  ** r
1b8bc 65 74 75 72 6e 65 64 20 69 66 20 74 68 69 73 20  eturned if this 
1b8bd 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 63 6f  statement was co
1b8be 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 74 68 65  mpiled using the
1b8bf 20 6c 65 67 61 63 79 20 0a 20 20 2a 2a 20 73 71   legacy .  ** sq
1b8c0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20  lite3_prepare() 
1b8c1 69 6e 74 65 72 66 61 63 65 2e 20 41 63 63 6f 72  interface. Accor
1b8c2 64 69 6e 67 20 74 6f 20 74 68 65 20 64 6f 63 73  ding to the docs
1b8c3 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 0a  , this can only.
1b8c4 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74    ** be one of t
1b8c5 68 65 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65  he values in the
1b8c6 20 66 69 72 73 74 20 61 73 73 65 72 74 28 29 20   first assert() 
1b8c7 62 65 6c 6f 77 2e 20 56 61 72 69 61 62 6c 65 20  below. Variable 
1b8c8 70 2d 3e 72 63 20 0a 20 20 2a 2a 20 63 6f 6e 74  p->rc .  ** cont
1b8c9 61 69 6e 73 20 74 68 65 20 76 61 6c 75 65 20 74  ains the value t
1b8ca 68 61 74 20 77 6f 75 6c 64 20 62 65 20 72 65 74  hat would be ret
1b8cb 75 72 6e 65 64 20 69 66 20 73 71 6c 69 74 65 33  urned if sqlite3
1b8cc 5f 66 69 6e 61 6c 69 7a 65 28 29 20 0a 20 20 2a  _finalize() .  *
1b8cd 2a 20 77 65 72 65 20 63 61 6c 6c 65 64 20 6f 6e  * were called on
1b8ce 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 20 20   statement p..  
1b8cf 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  */.  assert( rc=
1b8d0 3d 53 51 4c 49 54 45 5f 52 4f 57 20 20 7c 7c 20  =SQLITE_ROW  || 
1b8d1 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
1b8d2 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f    || rc==SQLITE_
1b8d3 45 52 52 4f 52 20 0a 20 20 20 20 20 20 20 7c 7c  ERROR .       ||
1b8d4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc==SQLITE_BUSY
1b8d5 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4d   || rc==SQLITE_M
1b8d6 49 53 55 53 45 0a 20 20 29 3b 0a 20 20 61 73 73  ISUSE.  );.  ass
1b8d7 65 72 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49  ert( p->rc!=SQLI
1b8d8 54 45 5f 52 4f 57 20 26 26 20 70 2d 3e 72 63 21  TE_ROW && p->rc!
1b8d9 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a  =SQLITE_DONE );.
1b8da 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70 61    if( p->isPrepa
1b8db 72 65 56 32 20 26 26 20 72 63 21 3d 53 51 4c 49  reV2 && rc!=SQLI
1b8dc 54 45 5f 52 4f 57 20 26 26 20 72 63 21 3d 53 51  TE_ROW && rc!=SQ
1b8dd 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
1b8de 20 2f 2a 20 49 66 20 74 68 69 73 20 73 74 61 74   /* If this stat
1b8df 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 72  ement was prepar
1b8e0 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
1b8e1 5f 70 72 65 70 61 72 65 5f 76 32 28 29 2c 20 61  _prepare_v2(), a
1b8e2 6e 64 20 61 6e 0a 20 20 20 20 2a 2a 20 65 72 72  nd an.    ** err
1b8e3 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64 2c 20  or has occured, 
1b8e4 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 65 20  then return the 
1b8e5 65 72 72 6f 72 20 63 6f 64 65 20 69 6e 20 70 2d  error code in p-
1b8e6 3e 72 63 20 74 6f 20 74 68 65 0a 20 20 20 20 2a  >rc to the.    *
1b8e7 2a 20 63 61 6c 6c 65 72 2e 20 53 65 74 20 74 68  * caller. Set th
1b8e8 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e 20  e error code in 
1b8e9 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
1b8ea 64 6c 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20  dle to the same 
1b8eb 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 20 0a 20  value..    */ . 
1b8ec 20 20 20 72 63 20 3d 20 64 62 2d 3e 65 72 72 43     rc = db->errC
1b8ed 6f 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 7d  ode = p->rc;.  }
1b8ee 0a 20 20 72 65 74 75 72 6e 20 28 72 63 26 64 62  .  return (rc&db
1b8ef 2d 3e 65 72 72 4d 61 73 6b 29 3b 0a 7d 0a 0a 2f  ->errMask);.}../
1b8f0 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
1b8f1 20 74 6f 70 2d 6c 65 76 65 6c 20 69 6d 70 6c 65   top-level imple
1b8f2 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c  mentation of sql
1b8f3 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 43 61  ite3_step().  Ca
1b8f4 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 53 74 65  ll.** sqlite3Ste
1b8f5 70 28 29 20 74 6f 20 64 6f 20 6d 6f 73 74 20 6f  p() to do most o
1b8f6 66 20 74 68 65 20 77 6f 72 6b 2e 20 20 49 66 20  f the work.  If 
1b8f7 61 20 73 63 68 65 6d 61 20 65 72 72 6f 72 20 6f  a schema error o
1b8f8 63 63 75 72 73 2c 0a 2a 2a 20 63 61 6c 6c 20 73  ccurs,.** call s
1b8f9 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28  qlite3Reprepare(
1b8fa 29 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e  ) and try again.
1b8fb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1b8fc 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  nt sqlite3_step(
1b8fd 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1b8fe 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  tmt){.  int rc =
1b8ff 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
1b900 20 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20    if( pStmt ){. 
1b901 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a     int cnt = 0;.
1b902 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56      Vdbe *v = (V
1b903 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20  dbe*)pStmt;.    
1b904 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d  sqlite3 *db = v-
1b905 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  >db;.    sqlite3
1b906 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
1b907 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 77 68 69  >mutex);.    whi
1b908 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65  le( (rc = sqlite
1b909 33 53 74 65 70 28 76 29 29 3d 3d 53 51 4c 49 54  3Step(v))==SQLIT
1b90a 45 5f 53 43 48 45 4d 41 0a 20 20 20 20 20 20 20  E_SCHEMA.       
1b90b 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35      && cnt++ < 5
1b90c 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28  .           && (
1b90d 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 70 72  rc = sqlite3Repr
1b90e 65 70 61 72 65 28 76 29 29 3d 3d 53 51 4c 49 54  epare(v))==SQLIT
1b90f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
1b910 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d  lite3_reset(pStm
1b911 74 29 3b 0a 20 20 20 20 20 20 76 2d 3e 65 78 70  t);.      v->exp
1b912 69 72 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  ired = 0;.    }.
1b913 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1b914 54 45 5f 53 43 48 45 4d 41 20 26 26 20 41 4c 57  TE_SCHEMA && ALW
1b915 41 59 53 28 76 2d 3e 69 73 50 72 65 70 61 72 65  AYS(v->isPrepare
1b916 56 32 29 20 26 26 20 41 4c 57 41 59 53 28 64 62  V2) && ALWAYS(db
1b917 2d 3e 70 45 72 72 29 20 29 7b 0a 20 20 20 20 20  ->pErr) ){.     
1b918 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f 63   /* This case oc
1b919 63 75 72 73 20 61 66 74 65 72 20 66 61 69 6c 69  curs after faili
1b91a 6e 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c 65 20  ng to recompile 
1b91b 61 6e 20 73 71 6c 20 73 74 61 74 65 6d 65 6e 74  an sql statement
1b91c 2e 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20  . .      ** The 
1b91d 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72  error message fr
1b91e 6f 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d 70 69  om the SQL compi
1b91f 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20  ler has already 
1b920 62 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20 20 20  been loaded .   
1b921 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64     ** into the d
1b922 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
1b923 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65  This block copie
1b924 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  s the error mess
1b925 61 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 72  age .      ** fr
1b926 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
1b927 68 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 65 20  handle into the 
1b928 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 73 65  statement and se
1b929 74 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ts the statement
1b92a 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 67 72 61  .      ** progra
1b92b 6d 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 20 74  m counter to 0 t
1b92c 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 77 68  o ensure that wh
1b92d 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  en the statement
1b92e 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69   is .      ** fi
1b92f 6e 61 6c 69 7a 65 64 20 6f 72 20 72 65 73 65 74  nalized or reset
1b930 20 74 68 65 20 70 61 72 73 65 72 20 65 72 72 6f   the parser erro
1b931 72 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61  r message is ava
1b932 69 6c 61 62 6c 65 20 76 69 61 0a 20 20 20 20 20  ilable via.     
1b933 20 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d   ** sqlite3_errm
1b934 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  sg() and sqlite3
1b935 5f 65 72 72 63 6f 64 65 28 29 2e 0a 20 20 20 20  _errcode()..    
1b936 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74    */.      const
1b937 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 28 63   char *zErr = (c
1b938 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
1b939 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 64  te3_value_text(d
1b93a 62 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20 20 20  b->pErr); .     
1b93b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1b93c 62 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  b, v->zErrMsg);.
1b93d 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d        if( !db->m
1b93e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1b93f 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73         v->zErrMs
1b940 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  g = sqlite3DbStr
1b941 44 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  Dup(db, zErr);. 
1b942 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20       } else {.  
1b943 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67        v->zErrMsg
1b944 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 76 2d   = 0;.        v-
1b945 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  >rc = SQLITE_NOM
1b946 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EM;.      }.    
1b947 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
1b948 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
1b949 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
1b94a 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
1b94b 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74  utex);.  }.  ret
1b94c 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1b94d 20 45 78 74 72 61 63 74 20 74 68 65 20 75 73 65   Extract the use
1b94e 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71  r data from a sq
1b94f 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74  lite3_context st
1b950 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75  ructure and retu
1b951 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  rn a.** pointer 
1b952 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  to it..*/.SQLITE
1b953 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
1b954 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c  e3_user_data(sql
1b955 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29  ite3_context *p)
1b956 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26  {.  assert( p &&
1b957 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72   p->pFunc );.  r
1b958 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d 3e  eturn p->pFunc->
1b959 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f 2a  pUserData;.}../*
1b95a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
1b95b 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61  user data from a
1b95c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1b95d 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72   structure and r
1b95e 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74  eturn a.** point
1b95f 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c  er to it..*/.SQL
1b960 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20  ITE_API sqlite3 
1b961 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  *sqlite3_context
1b962 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74  _db_handle(sqlit
1b963 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a  e3_context *p){.
1b964 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70    assert( p && p
1b965 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 74  ->pFunc );.  ret
1b966 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a 0a  urn p->s.db;.}..
1b967 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
1b968 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c 65  ing is the imple
1b969 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20  mentation of an 
1b96a 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  SQL function tha
1b96b 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69 6c  t always.** fail
1b96c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  s with an error 
1b96d 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e 67 20  message stating 
1b96e 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f  that the functio
1b96f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 65  n is used in the
1b970 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 78  .** wrong contex
1b971 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  t.  The sqlite3_
1b972 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f  overload_functio
1b973 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63 6f  n() API might co
1b974 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20 66  nstruct.** SQL f
1b975 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65  unction that use
1b976 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 6f   this routine so
1b977 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69   that the functi
1b978 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a 2a  ons will exist.*
1b979 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c  * for name resol
1b97a 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 63  ution but are ac
1b97b 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 65  tually overloade
1b97c 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 75  d by the xFindFu
1b97d 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f 64  nction.** method
1b97e 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c   of virtual tabl
1b97f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
1b980 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1b981 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f  e3InvalidFunctio
1b982 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  n(.  sqlite3_con
1b983 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 20  text *context,  
1b984 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  /* The function 
1b985 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74 20  calling context 
1b986 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  */.  int NotUsed
1b987 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1b988 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67  /* Number of arg
1b989 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75  uments to the fu
1b98a 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69  nction */.  sqli
1b98b 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
1b98c 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65 20  sed2   /* Value 
1b98d 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74  of each argument
1b98e 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63   */.){.  const c
1b98f 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f 6e  har *zName = con
1b990 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e 61  text->pFunc->zNa
1b991 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  me;.  char *zErr
1b992 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
1b993 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
1b994 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72 72  otUsed2);.  zErr
1b995 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
1b996 74 66 28 0a 20 20 20 20 20 20 22 75 6e 61 62 6c  tf(.      "unabl
1b997 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f  e to use functio
1b998 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 71 75  n %s in the requ
1b999 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c 20  ested context", 
1b99a 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  zName);.  sqlite
1b99b 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
1b99c 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31  ontext, zErr, -1
1b99d 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
1b99e 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  e(zErr);.}../*.*
1b99f 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72 65  * Allocate or re
1b9a0 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 67 61  turn the aggrega
1b9a1 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 61  te context for a
1b9a2 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20   user function. 
1b9a3 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65 78   A new.** contex
1b9a4 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f  t is allocated o
1b9a5 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c  n the first call
1b9a6 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 61  .  Subsequent ca
1b9a7 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a 2a  lls return the.*
1b9a8 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20 74  * same context t
1b9a9 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64  hat was returned
1b9aa 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73 2e   on prior calls.
1b9ab 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
1b9ac 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67  oid *sqlite3_agg
1b9ad 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73  regate_context(s
1b9ae 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b9af 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  p, int nByte){. 
1b9b0 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73   Mem *pMem;.  as
1b9b1 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46  sert( p && p->pF
1b9b2 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d  unc && p->pFunc-
1b9b3 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73 65  >xStep );.  asse
1b9b4 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b9b5 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d 3e  x_held(p->s.db->
1b9b6 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 6d  mutex) );.  pMem
1b9b7 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69 66   = p->pMem;.  if
1b9b8 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
1b9b9 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b 0a   MEM_Agg)==0 ){.
1b9ba 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30      if( nByte==0
1b9bb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1b9bc 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45  3VdbeMemReleaseE
1b9bd 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a 20  xternal(pMem);. 
1b9be 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1b9bf 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
1b9c0 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a     pMem->z = 0;.
1b9c1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1b9c2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
1b9c3 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c  row(pMem, nByte,
1b9c4 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d   0);.      pMem-
1b9c5 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67 67  >flags = MEM_Agg
1b9c6 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  ;.      pMem->u.
1b9c7 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63 3b  pDef = p->pFunc;
1b9c8 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d  .      if( pMem-
1b9c9 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  >z ){.        me
1b9ca 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c  mset(pMem->z, 0,
1b9cb 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d   nByte);.      }
1b9cc 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1b9cd 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d 2d  urn (void*)pMem-
1b9ce 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  >z;.}../*.** Ret
1b9cf 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72 79  urn the auxilary
1b9d0 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 69   data pointer, i
1b9d1 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 69  f any, for the i
1b9d2 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74 20  Arg'th argument 
1b9d3 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d 66  to.** the user-f
1b9d4 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  unction defined 
1b9d5 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c 49  by pCtx..*/.SQLI
1b9d6 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
1b9d7 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
1b9d8 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1b9d9 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72 67   *pCtx, int iArg
1b9da 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a 70  ){.  VdbeFunc *p
1b9db 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 73  VdbeFunc;..  ass
1b9dc 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b9dd 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1b9de 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1b9df 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78  pVdbeFunc = pCtx
1b9e0 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69  ->pVdbeFunc;.  i
1b9e1 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c  f( !pVdbeFunc ||
1b9e2 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e 63   iArg>=pVdbeFunc
1b9e3 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c 30  ->nAux || iArg<0
1b9e4 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
1b9e5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
1b9e6 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b  VdbeFunc->apAux[
1b9e7 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a 2f  iArg].pAux;.}../
1b9e8 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75 78  *.** Set the aux
1b9e9 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74  ilary data point
1b9ea 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66 75  er and delete fu
1b9eb 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 20  nction, for the 
1b9ec 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 6d  iArg'th.** argum
1b9ed 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72 2d  ent to the user-
1b9ee 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64  function defined
1b9ef 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70 72   by pCtx. Any pr
1b9f0 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73 0a  evious value is.
1b9f1 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63 61  ** deleted by ca
1b9f2 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74 65  lling the delete
1b9f3 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66   function specif
1b9f4 69 65 64 20 77 68 65 6e 20 69 74 20 77 61 73 20  ied when it was 
1b9f5 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  set..*/.SQLITE_A
1b9f6 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1b9f7 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20 73  set_auxdata(.  s
1b9f8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b9f9 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 72  pCtx, .  int iAr
1b9fa 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 78  g, .  void *pAux
1b9fb 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  , .  void (*xDel
1b9fc 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20  ete)(void*).){. 
1b9fd 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20   struct AuxData 
1b9fe 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64 62  *pAuxData;.  Vdb
1b9ff 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63  eFunc *pVdbeFunc
1ba00 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20 29  ;.  if( iArg<0 )
1ba01 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a 20   goto failed;.. 
1ba02 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1ba03 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
1ba04 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
1ba05 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20  ;.  pVdbeFunc = 
1ba06 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b  pCtx->pVdbeFunc;
1ba07 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e  .  if( !pVdbeFun
1ba08 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d 3e  c || pVdbeFunc->
1ba09 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 20  nAux<=iArg ){.  
1ba0a 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 56    int nAux = (pV
1ba0b 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65 46  dbeFunc ? pVdbeF
1ba0c 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b 0a  unc->nAux : 0);.
1ba0d 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20      int nMalloc 
1ba0e 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75 6e  = sizeof(VdbeFun
1ba0f 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 75  c) + sizeof(stru
1ba10 63 74 20 41 75 78 44 61 74 61 29 2a 69 41 72 67  ct AuxData)*iArg
1ba11 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 20  ;.    pVdbeFunc 
1ba12 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
1ba13 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 70  oc(pCtx->s.db, p
1ba14 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f  VdbeFunc, nMallo
1ba15 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 64  c);.    if( !pVd
1ba16 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 20  beFunc ){.      
1ba17 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20  goto failed;.   
1ba18 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 64   }.    pCtx->pVd
1ba19 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46 75  beFunc = pVdbeFu
1ba1a 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  nc;.    memset(&
1ba1b 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78  pVdbeFunc->apAux
1ba1c 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 6f  [nAux], 0, sizeo
1ba1d 66 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61  f(struct AuxData
1ba1e 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 29  )*(iArg+1-nAux))
1ba1f 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d  ;.    pVdbeFunc-
1ba20 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b 0a  >nAux = iArg+1;.
1ba21 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 70      pVdbeFunc->p
1ba22 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 75  Func = pCtx->pFu
1ba23 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 44  nc;.  }..  pAuxD
1ba24 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e 63  ata = &pVdbeFunc
1ba25 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a 20  ->apAux[iArg];. 
1ba26 20 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e 70   if( pAuxData->p
1ba27 41 75 78 20 26 26 20 70 41 75 78 44 61 74 61 2d  Aux && pAuxData-
1ba28 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20  >xDelete ){.    
1ba29 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74  pAuxData->xDelet
1ba2a 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78  e(pAuxData->pAux
1ba2b 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 74  );.  }.  pAuxDat
1ba2c 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a  a->pAux = pAux;.
1ba2d 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c    pAuxData->xDel
1ba2e 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a 20  ete = xDelete;. 
1ba2f 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 64   return;..failed
1ba30 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65 20  :.  if( xDelete 
1ba31 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28 70  ){.    xDelete(p
1ba32 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66  Aux);.  }.}..#if
1ba33 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1ba34 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a  _DEPRECATED./*.*
1ba35 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
1ba36 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65  ber of times the
1ba37 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 6f   Step function o
1ba38 66 20 61 20 61 67 67 72 65 67 61 74 65 20 68 61  f a aggregate ha
1ba39 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65  s been .** calle
1ba3a 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  d..**.** This fu
1ba3b 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63  nction is deprec
1ba3c 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 73  ated.  Do not us
1ba3d 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f 64  e it for new cod
1ba3e 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f  e.  It is.** pro
1ba3f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 6f  vide only to avo
1ba40 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 61  id breaking lega
1ba41 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 67  cy code.  New ag
1ba42 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
1ba43 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
1ba44 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 20  ons should keep 
1ba45 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 73  their own counts
1ba46 20 77 69 74 68 69 6e 20 74 68 65 69 72 20 61 67   within their ag
1ba47 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 65  gregate.** conte
1ba48 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  xt..*/.SQLITE_AP
1ba49 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67  I int sqlite3_ag
1ba4a 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71  gregate_count(sq
1ba4b 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1ba4c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26  ){.  assert( p &
1ba4d 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70 2d 3e  & p->pMem && p->
1ba4e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e  pFunc && p->pFun
1ba4f 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72 65  c->xStep );.  re
1ba50 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b  turn p->pMem->n;
1ba51 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1ba52 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1ba53 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
1ba54 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1ba55 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e  for the statemen
1ba56 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49  t pStmt..*/.SQLI
1ba57 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1ba58 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
1ba59 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1ba5a 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56  tmt){.  Vdbe *pV
1ba5b 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  m = (Vdbe *)pStm
1ba5c 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 20  t;.  return pVm 
1ba5d 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d  ? pVm->nResColum
1ba5e 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n : 0;.}../*.** 
1ba5f 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1ba60 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61 69  r of values avai
1ba61 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 63  lable from the c
1ba62 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68  urrent row of th
1ba63 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65  e.** currently e
1ba64 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65  xecuting stateme
1ba65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c  nt pStmt..*/.SQL
1ba66 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1ba67 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73  te3_data_count(s
1ba68 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1ba69 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d  mt){.  Vdbe *pVm
1ba6a 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
1ba6b 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 20 7c  ;.  if( pVm==0 |
1ba6c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65  | pVm->pResultSe
1ba6d 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  t==0 ) return 0;
1ba6e 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e 6e  .  return pVm->n
1ba6f 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f  ResColumn;.}.../
1ba70 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65  *.** Check to se
1ba71 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c  e if column iCol
1ba72 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 73 74   of the given st
1ba73 61 74 65 6d 65 6e 74 20 69 73 20 76 61 6c 69 64  atement is valid
1ba74 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c 20  .  If.** it is, 
1ba75 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
1ba76 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72 20   to the Mem for 
1ba77 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 61  the value of tha
1ba78 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 20  t column..** If 
1ba79 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c 69  iCol is not vali
1ba7a 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  d, return a poin
1ba7b 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 68 69  ter to a Mem whi
1ba7c 63 68 20 68 61 73 20 61 20 76 61 6c 75 65 0a 2a  ch has a value.*
1ba7d 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74  * of NULL..*/.st
1ba7e 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d 6e  atic Mem *column
1ba7f 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  Mem(sqlite3_stmt
1ba80 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b   *pStmt, int i){
1ba81 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20 20  .  Vdbe *pVm;.  
1ba82 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 6d 20  int vals;.  Mem 
1ba83 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 3d 20  *pOut;..  pVm = 
1ba84 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
1ba85 20 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d 2d   if( pVm && pVm-
1ba86 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20 26  >pResultSet!=0 &
1ba87 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c  & i<pVm->nResCol
1ba88 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0a 20  umn && i>=0 ){. 
1ba89 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1ba8a 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e  _enter(pVm->db->
1ba8b 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 61 6c 73  mutex);.    vals
1ba8c 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f   = sqlite3_data_
1ba8d 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20  count(pStmt);.  
1ba8e 20 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d 3e 70    pOut = &pVm->p
1ba8f 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a 20 20  ResultSet[i];.  
1ba90 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66  }else{.    /* If
1ba91 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65   the value passe
1ba92 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
1ba93 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 75 74 20  argument is out 
1ba94 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 6e  of range, return
1ba95 0a 20 20 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65  .    ** a pointe
1ba96 72 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  r to the followi
1ba97 6e 67 20 73 74 61 74 69 63 20 4d 65 6d 20 6f 62  ng static Mem ob
1ba98 6a 65 63 74 20 77 68 69 63 68 20 63 6f 6e 74 61  ject which conta
1ba99 69 6e 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 76  ins the.    ** v
1ba9a 61 6c 75 65 20 53 51 4c 20 4e 55 4c 4c 2e 20 45  alue SQL NULL. E
1ba9b 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20 4d  ven though the M
1ba9c 65 6d 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  em structure con
1ba9d 74 61 69 6e 73 20 61 6e 20 65 6c 65 6d 65 6e 74  tains an element
1ba9e 0a 20 20 20 20 2a 2a 20 6f 66 20 74 79 70 65 20  .    ** of type 
1ba9f 69 36 34 2c 20 6f 6e 20 63 65 72 74 61 69 6e 20  i64, on certain 
1baa0 61 72 63 68 69 74 65 63 74 75 72 65 20 28 78 38  architecture (x8
1baa1 36 29 20 77 69 74 68 20 63 65 72 74 61 69 6e 20  6) with certain 
1baa2 63 6f 6d 70 69 6c 65 72 0a 20 20 20 20 2a 2a 20  compiler.    ** 
1baa3 73 77 69 74 63 68 65 73 20 28 2d 4f 73 29 2c 20  switches (-Os), 
1baa4 67 63 63 20 6d 61 79 20 61 6c 69 67 6e 20 74 68  gcc may align th
1baa5 69 73 20 4d 65 6d 20 6f 62 6a 65 63 74 20 6f 6e  is Mem object on
1baa6 20 61 20 34 2d 62 79 74 65 20 62 6f 75 6e 64 61   a 4-byte bounda
1baa7 72 79 0a 20 20 20 20 2a 2a 20 69 6e 73 74 65 61  ry.    ** instea
1baa8 64 20 6f 66 20 61 6e 20 38 2d 62 79 74 65 20 6f  d of an 8-byte o
1baa9 6e 65 2e 20 54 68 69 73 20 61 6c 6c 20 77 6f 72  ne. This all wor
1baaa 6b 73 20 66 69 6e 65 2c 20 65 78 63 65 70 74 20  ks fine, except 
1baab 74 68 61 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a  that when.    **
1baac 20 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 53 51   running with SQ
1baad 4c 49 54 45 5f 44 45 42 55 47 20 64 65 66 69 6e  LITE_DEBUG defin
1baae 65 64 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f  ed the SQLite co
1baaf 64 65 20 73 6f 6d 65 74 69 6d 65 73 20 61 73 73  de sometimes ass
1bab0 65 72 74 28 29 73 0a 20 20 20 20 2a 2a 20 74 68  ert()s.    ** th
1bab1 61 74 20 61 20 4d 65 6d 20 73 74 72 75 63 74 75  at a Mem structu
1bab2 72 65 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e  re is located on
1bab3 20 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64   an 8-byte bound
1bab4 61 72 79 2e 20 54 6f 20 70 72 65 76 65 6e 74 0a  ary. To prevent.
1bab5 20 20 20 20 2a 2a 20 74 68 69 73 20 61 73 73 65      ** this asse
1bab6 72 74 28 29 20 66 72 6f 6d 20 66 61 69 6c 69 6e  rt() from failin
1bab7 67 2c 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67  g, when building
1bab8 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42   with SQLITE_DEB
1bab9 55 47 20 64 65 66 69 6e 65 64 0a 20 20 20 20 2a  UG defined.    *
1baba 2a 20 75 73 69 6e 67 20 67 63 63 2c 20 66 6f 72  * using gcc, for
1babb 63 65 20 6e 75 6c 6c 4d 65 6d 20 74 6f 20 62 65  ce nullMem to be
1babc 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20   8-byte aligned 
1babd 75 73 69 6e 67 20 74 68 65 20 6d 61 67 69 63 61  using the magica
1babe 6c 0a 20 20 20 20 2a 2a 20 5f 5f 61 74 74 72 69  l.    ** __attri
1babf 62 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28  bute__((aligned(
1bac0 38 29 29 29 20 6d 61 63 72 6f 2e 20 20 2a 2f 0a  8))) macro.  */.
1bac1 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
1bac2 20 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 0a 23 69   Mem nullMem .#i
1bac3 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
1bac4 5f 44 45 42 55 47 29 20 26 26 20 64 65 66 69 6e  _DEBUG) && defin
1bac5 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 20 20 20  ed(__GNUC__).   
1bac6 20 20 20 5f 5f 61 74 74 72 69 62 75 74 65 5f 5f     __attribute__
1bac7 28 28 61 6c 69 67 6e 65 64 28 38 29 29 29 20 0a  ((aligned(8))) .
1bac8 23 65 6e 64 69 66 0a 20 20 20 20 20 20 3d 20 7b  #endif.      = {
1bac9 7b 30 7d 2c 20 28 64 6f 75 62 6c 65 29 30 2c 20  {0}, (double)0, 
1baca 30 2c 20 22 22 2c 20 30 2c 20 4d 45 4d 5f 4e 75  0, "", 0, MEM_Nu
1bacb 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c  ll, SQLITE_NULL,
1bacc 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 20 20 20   0, 0, 0 };..   
1bacd 20 69 66 28 20 70 56 6d 20 26 26 20 41 4c 57 41   if( pVm && ALWA
1bace 59 53 28 70 56 6d 2d 3e 64 62 29 20 29 7b 0a 20  YS(pVm->db) ){. 
1bacf 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
1bad0 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62  ex_enter(pVm->db
1bad1 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
1bad2 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56 6d  sqlite3Error(pVm
1bad3 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e  ->db, SQLITE_RAN
1bad4 47 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  GE, 0);.    }.  
1bad5 20 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29 26    pOut = (Mem*)&
1bad6 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72  nullMem;.  }.  r
1bad7 65 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f  eturn pOut;.}../
1bad8 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1bad9 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74  on is called aft
1bada 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 73  er invoking an s
1badb 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58 58  qlite3_value_XXX
1badc 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20 0a   function on a .
1badd 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20  ** column value 
1bade 28 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72 65  (i.e. a value re
1badf 74 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75 61  turned by evalua
1bae0 74 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 72  ting an SQL expr
1bae1 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a  ession in the.**
1bae2 20 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66 20   select list of 
1bae3 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
1bae4 6e 74 29 20 74 68 61 74 20 6d 61 79 20 63 61 75  nt) that may cau
1bae5 73 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  se a malloc() fa
1bae6 69 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d 61  ilure. If .** ma
1bae7 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65  lloc() has faile
1bae8 64 2c 20 74 68 65 20 74 68 72 65 61 64 73 20 6d  d, the threads m
1bae9 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
1baea 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 20   is cleared and 
1baeb 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f  the result.** co
1baec 64 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20  de of statement 
1baed 70 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51 4c  pStmt set to SQL
1baee 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a  ITE_NOMEM..**.**
1baef 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74   Specifically, t
1baf0 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66 72  his is called fr
1baf1 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a  om within:.**.**
1baf2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
1baf3 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20 20  umn_int().**    
1baf4 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1baf5 69 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20 73  int64().**     s
1baf6 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1baf7 78 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  xt().**     sqli
1baf8 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
1baf9 36 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  6().**     sqlit
1bafa 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29  e3_column_real()
1bafb 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .**     sqlite3_
1bafc 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a  column_bytes().*
1bafd 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
1bafe 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a 2a  lumn_bytes16().*
1baff 2a 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 66 6f 72  *.** But not for
1bb00 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1bb01 62 6c 6f 62 28 29 2c 20 77 68 69 63 68 20 6e 65  blob(), which ne
1bb02 76 65 72 20 63 61 6c 6c 73 20 6d 61 6c 6c 6f 63  ver calls malloc
1bb03 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ()..*/.static vo
1bb04 69 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  id columnMallocF
1bb05 61 69 6c 75 72 65 28 73 71 6c 69 74 65 33 5f 73  ailure(sqlite3_s
1bb06 74 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 0a 20 20  tmt *pStmt).{.  
1bb07 2f 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 29 20 66  /* If malloc() f
1bb08 61 69 6c 65 64 20 64 75 72 69 6e 67 20 61 6e 20  ailed during an 
1bb09 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73  encoding convers
1bb0a 69 6f 6e 20 77 69 74 68 69 6e 20 61 6e 0a 20 20  ion within an.  
1bb0b 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
1bb0c 6e 5f 58 58 58 20 41 50 49 2c 20 74 68 65 6e 20  n_XXX API, then 
1bb0d 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63  set the return c
1bb0e 6f 64 65 20 6f 66 20 74 68 65 20 73 74 61 74 65  ode of the state
1bb0f 6d 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 53 51 4c  ment to.  ** SQL
1bb10 49 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e  ITE_NOMEM. The n
1bb11 65 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74 65  ext call to _ste
1bb12 70 28 29 20 28 69 66 20 61 6e 79 29 20 77 69 6c  p() (if any) wil
1bb13 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  l return SQLITE_
1bb14 45 52 52 4f 52 0a 20 20 2a 2a 20 61 6e 64 20 5f  ERROR.  ** and _
1bb15 66 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20  finalize() will 
1bb16 72 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 20  return NOMEM..  
1bb17 2a 2f 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  */.  Vdbe *p = (
1bb18 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
1bb19 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e  if( p ){.    p->
1bb1a 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
1bb1b 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63  xit(p->db, p->rc
1bb1c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
1bb1d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62  utex_leave(p->db
1bb1e 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a  ->mutex);.  }.}.
1bb1f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1bb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73  ************** s
1bb21 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 20 20  qlite3_column_  
1bb22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bb23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1bb24 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
1bb25 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73   routines are us
1bb26 65 64 20 74 6f 20 61 63 63 65 73 73 20 65 6c 65  ed to access ele
1bb27 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 72  ments of the cur
1bb28 72 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 6e 20 74  rent row.** in t
1bb29 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a  he result set..*
1bb2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
1bb2b 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1bb2c 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c  _column_blob(sql
1bb2d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bb2e 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73  , int i){.  cons
1bb2f 74 20 76 6f 69 64 20 2a 76 61 6c 3b 0a 20 20 76  t void *val;.  v
1bb30 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
1bb31 75 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d  ue_blob( columnM
1bb32 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
1bb33 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20   /* Even though 
1bb34 74 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63 6f  there is no enco
1bb35 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 2c  ding conversion,
1bb36 20 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d 69   value_blob() mi
1bb37 67 68 74 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f  ght.  ** need to
1bb38 20 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20 74   call malloc() t
1bb39 6f 20 65 78 70 61 6e 64 20 74 68 65 20 72 65 73  o expand the res
1bb3a 75 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c 6f  ult of a zeroblo
1bb3b 62 28 29 20 0a 20 20 2a 2a 20 65 78 70 72 65 73  b() .  ** expres
1bb3c 73 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f  sion. .  */.  co
1bb3d 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
1bb3e 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
1bb3f 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45  rn val;.}.SQLITE
1bb40 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1bb41 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71  _column_bytes(sq
1bb42 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bb43 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74  t, int i){.  int
1bb44 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76   val = sqlite3_v
1bb45 61 6c 75 65 5f 62 79 74 65 73 28 20 63 6f 6c 75  alue_bytes( colu
1bb46 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
1bb47 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
1bb48 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
1bb49 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a    return val;.}.
1bb4a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1bb4b 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
1bb4c 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74  tes16(sqlite3_st
1bb4d 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1bb4e 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73  ){.  int val = s
1bb4f 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
1bb50 65 73 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  es16( columnMem(
1bb51 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f  pStmt,i) );.  co
1bb52 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
1bb53 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
1bb54 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45  rn val;.}.SQLITE
1bb55 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69  _API double sqli
1bb56 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c  te3_column_doubl
1bb57 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1bb58 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
1bb59 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 73 71   double val = sq
1bb5a 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
1bb5b 6c 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  le( columnMem(pS
1bb5c 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75  tmt,i) );.  colu
1bb5d 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28  mnMallocFailure(
1bb5e 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
1bb5f 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41   val;.}.SQLITE_A
1bb60 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
1bb61 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 65  olumn_int(sqlite
1bb62 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1bb63 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c  nt i){.  int val
1bb64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1bb65 5f 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  _int( columnMem(
1bb66 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f  pStmt,i) );.  co
1bb67 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
1bb68 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
1bb69 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45  rn val;.}.SQLITE
1bb6a 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36  _API sqlite_int6
1bb6b 34 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  4 sqlite3_column
1bb6c 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73  _int64(sqlite3_s
1bb6d 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bb6e 69 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  i){.  sqlite_int
1bb6f 36 34 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33  64 val = sqlite3
1bb70 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 20 63 6f  _value_int64( co
1bb71 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
1bb72 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   );.  columnMall
1bb73 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
1bb74 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a  ;.  return val;.
1bb75 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  }.SQLITE_API con
1bb76 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
1bb77 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
1bb78 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74  _text(sqlite3_st
1bb79 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1bb7a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
1bb7b 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d 20  ned char *val = 
1bb7c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bb7d 78 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  xt( columnMem(pS
1bb7e 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75  tmt,i) );.  colu
1bb7f 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28  mnMallocFailure(
1bb80 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
1bb81 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41   val;.}.SQLITE_A
1bb82 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  PI sqlite3_value
1bb83 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
1bb84 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73  _value(sqlite3_s
1bb85 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bb86 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20  i){.  Mem *pOut 
1bb87 3d 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d  = columnMem(pStm
1bb88 74 2c 20 69 29 3b 0a 20 20 69 66 28 20 70 4f 75  t, i);.  if( pOu
1bb89 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61  t->flags&MEM_Sta
1bb8a 74 69 63 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d  tic ){.    pOut-
1bb8b 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53  >flags &= ~MEM_S
1bb8c 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d  tatic;.    pOut-
1bb8d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70  >flags |= MEM_Ep
1bb8e 68 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f 6c 75 6d  hem;.  }.  colum
1bb8f 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
1bb90 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
1bb91 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
1bb92 29 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64 65 66  )pOut;.}.#ifndef
1bb93 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1bb94 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  16.SQLITE_API co
1bb95 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
1bb96 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
1bb97 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1bb98 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63  tmt, int i){.  c
1bb99 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 3d  onst void *val =
1bb9a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1bb9b 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d  ext16( columnMem
1bb9c 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
1bb9d 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
1bb9e 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
1bb9f 75 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69  urn val;.}.#endi
1bba0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1bba1 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45  _UTF16 */.SQLITE
1bba2 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1bba3 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c  _column_type(sql
1bba4 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bba5 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20  , int i){.  int 
1bba6 69 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f  iType = sqlite3_
1bba7 76 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c 75  value_type( colu
1bba8 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
1bba9 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
1bbaa 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
1bbab 20 20 72 65 74 75 72 6e 20 69 54 79 70 65 3b 0a    return iType;.
1bbac 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  }../* The follow
1bbad 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ing function is 
1bbae 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64  experimental and
1bbaf 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
1bbb0 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c  ge or.** removal
1bbb1 20 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69 74 65   */./*int sqlite
1bbb2 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63  3_column_numeric
1bbb3 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74  _type(sqlite3_st
1bbb4 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1bbb5 29 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73 71  ){.**  return sq
1bbb6 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65  lite3_value_nume
1bbb7 72 69 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e  ric_type( column
1bbb8 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a  Mem(pStmt,i) );.
1bbb9 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f  **}.*/../*.** Co
1bbba 6e 76 65 72 74 20 74 68 65 20 4e 2d 74 68 20 65  nvert the N-th e
1bbbb 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d 74 2d  lement of pStmt-
1bbbc 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e 74 6f  >pColName[] into
1bbbd 20 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 0a   a string using.
1bbbe 2a 2a 20 78 46 75 6e 63 28 29 20 74 68 65 6e 20  ** xFunc() then 
1bbbf 72 65 74 75 72 6e 20 74 68 61 74 20 73 74 72 69  return that stri
1bbc0 6e 67 2e 20 20 49 66 20 4e 20 69 73 20 6f 75 74  ng.  If N is out
1bbc1 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72   of range, retur
1bbc2 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  n 0..**.** There
1bbc3 20 61 72 65 20 75 70 20 74 6f 20 35 20 6e 61 6d   are up to 5 nam
1bbc4 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  es for each colu
1bbc5 6d 6e 2e 20 20 75 73 65 54 79 70 65 20 64 65 74  mn.  useType det
1bbc6 65 72 6d 69 6e 65 73 20 77 68 69 63 68 0a 2a 2a  ermines which.**
1bbc7 20 6e 61 6d 65 20 69 73 20 72 65 74 75 72 6e 65   name is returne
1bbc8 64 2e 20 20 48 65 72 65 20 61 72 65 20 74 68 65  d.  Here are the
1bbc9 20 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20   names:.**.**   
1bbca 20 30 20 20 20 20 20 20 54 68 65 20 63 6f 6c 75   0      The colu
1bbcb 6d 6e 20 6e 61 6d 65 20 61 73 20 69 74 20 73 68  mn name as it sh
1bbcc 6f 75 6c 64 20 62 65 20 64 69 73 70 6c 61 79 65  ould be displaye
1bbcd 64 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a 2a 20  d for output.** 
1bbce 20 20 20 31 20 20 20 20 20 20 54 68 65 20 64 61     1      The da
1bbcf 74 61 74 79 70 65 20 6e 61 6d 65 20 66 6f 72 20  tatype name for 
1bbd0 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20  the column.**   
1bbd1 20 32 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65   2      The name
1bbd2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1bbd3 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e   that the column
1bbd4 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a   derives from.**
1bbd5 20 20 20 20 33 20 20 20 20 20 20 54 68 65 20 6e      3      The n
1bbd6 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1bbd7 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e   that the column
1bbd8 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a   derives from.**
1bbd9 20 20 20 20 34 20 20 20 20 20 20 54 68 65 20 6e      4      The n
1bbda 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1bbdb 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65   column that the
1bbdc 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64   result column d
1bbdd 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 0a 2a  erives from.**.*
1bbde 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20  * If the result 
1bbdf 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20  is not a simple 
1bbe0 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65  column reference
1bbe1 20 28 69 66 20 69 74 20 69 73 20 61 6e 20 65 78   (if it is an ex
1bbe2 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20 61  pression.** or a
1bbe3 20 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e 20   constant) then 
1bbe4 75 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20 61  useTypes 2, 3, a
1bbe5 6e 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c 4c  nd 4 return NULL
1bbe6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
1bbe7 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e 4e 61  t void *columnNa
1bbe8 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  me(.  sqlite3_st
1bbe9 6d 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69 6e 74  mt *pStmt,.  int
1bbea 20 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   N,.  const void
1bbeb 20 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d 2a 29   *(*xFunc)(Mem*)
1bbec 2c 0a 20 20 69 6e 74 20 75 73 65 54 79 70 65 0a  ,.  int useType.
1bbed 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  ){.  const void 
1bbee 2a 72 65 74 20 3d 20 30 3b 0a 20 20 56 64 62 65  *ret = 0;.  Vdbe
1bbef 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53   *p = (Vdbe *)pS
1bbf0 74 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  tmt;.  int n;.  
1bbf1 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d  sqlite3 *db = p-
1bbf2 3e 64 62 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  >db;.  .  assert
1bbf3 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 6e 20 3d  ( db!=0 );.  n =
1bbf4 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1bbf5 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20  count(pStmt);.  
1bbf6 69 66 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20  if( N<n && N>=0 
1bbf7 29 7b 0a 20 20 20 20 4e 20 2b 3d 20 75 73 65 54  ){.    N += useT
1bbf8 79 70 65 2a 6e 3b 0a 20 20 20 20 73 71 6c 69 74  ype*n;.    sqlit
1bbf9 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
1bbfa 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 61  b->mutex);.    a
1bbfb 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f  ssert( db->mallo
1bbfc 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20  cFailed==0 );.  
1bbfd 20 20 72 65 74 20 3d 20 78 46 75 6e 63 28 26 70    ret = xFunc(&p
1bbfe 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a  ->aColName[N]);.
1bbff 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63       /* A malloc
1bc00 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65 64   may have failed
1bc01 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 78   inside of the x
1bc02 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20 49 66 20  Func() call. If 
1bc03 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 74  this.    ** is t
1bc04 68 65 20 63 61 73 65 2c 20 63 6c 65 61 72 20 74  he case, clear t
1bc05 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
1bc06 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e 20  flag and return 
1bc07 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  NULL..    */.   
1bc08 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1bc09 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 64  ailed ){.      d
1bc0a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1bc0b 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 20 3d  = 0;.      ret =
1bc0c 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71   0;.    }.    sq
1bc0d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1bc0e 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
1bc0f 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a  }.  return ret;.
1bc10 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1bc11 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1bc12 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  Nth column of th
1bc13 65 20 72 65 73 75 6c 74 20 73 65 74 20 72 65 74  e result set ret
1bc14 75 72 6e 65 64 20 62 79 20 53 51 4c 0a 2a 2a 20  urned by SQL.** 
1bc15 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e  statement pStmt.
1bc16 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
1bc17 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
1bc18 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73  e3_column_name(s
1bc19 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bc1a 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
1bc1b 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
1bc1c 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
1bc1d 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
1bc1e 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1bc1f 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41  alue_text, COLNA
1bc20 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e  ME_NAME);.}.#ifn
1bc21 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1bc22 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
1bc23 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
1bc24 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
1bc25 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  16(sqlite3_stmt 
1bc26 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
1bc27 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
1bc28 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
1bc29 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
1bc2a 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
1bc2b 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c  e3_value_text16,
1bc2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a   COLNAME_NAME);.
1bc2d 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1bc2e 43 6f 6e 73 74 72 61 69 6e 74 3a 20 20 49 66 20  Constraint:  If 
1bc2f 79 6f 75 20 68 61 76 65 20 45 4e 41 42 4c 45 5f  you have ENABLE_
1bc30 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20  COLUMN_METADATA 
1bc31 74 68 65 6e 20 79 6f 75 20 6d 75 73 74 0a 2a 2a  then you must.**
1bc32 20 6e 6f 74 20 64 65 66 69 6e 65 20 4f 4d 49 54   not define OMIT
1bc33 5f 44 45 43 4c 54 59 50 45 2e 0a 2a 2f 0a 23 69  _DECLTYPE..*/.#i
1bc34 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
1bc35 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 29 20  _OMIT_DECLTYPE) 
1bc36 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
1bc37 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f  E_ENABLE_COLUMN_
1bc38 4d 45 54 41 44 41 54 41 29 0a 23 20 65 72 72 6f  METADATA).# erro
1bc39 72 20 22 4d 75 73 74 20 6e 6f 74 20 64 65 66 69  r "Must not defi
1bc3a 6e 65 20 62 6f 74 68 20 53 51 4c 49 54 45 5f 4f  ne both SQLITE_O
1bc3b 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 5c 0a 20  MIT_DECLTYPE \. 
1bc3c 20 20 20 20 20 20 20 20 61 6e 64 20 53 51 4c 49          and SQLI
1bc3d 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
1bc3e 5f 4d 45 54 41 44 41 54 41 22 0a 23 65 6e 64 69  _METADATA".#endi
1bc3f 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
1bc40 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a  E_OMIT_DECLTYPE.
1bc41 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1bc42 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74   column declarat
1bc43 69 6f 6e 20 74 79 70 65 20 28 69 66 20 61 70 70  ion type (if app
1bc44 6c 69 63 61 62 6c 65 29 20 6f 66 20 74 68 65 20  licable) of the 
1bc45 27 69 27 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  'i'th column.** 
1bc46 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
1bc47 74 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65  t of SQL stateme
1bc48 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c  nt pStmt..*/.SQL
1bc49 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
1bc4a 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
1bc4b 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69  mn_decltype(sqli
1bc4c 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bc4d 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72   int N){.  retur
1bc4e 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20  n columnName(.  
1bc4f 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63      pStmt, N, (c
1bc50 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65  onst void*(*)(Me
1bc51 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  m*))sqlite3_valu
1bc52 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f  e_text, COLNAME_
1bc53 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 69 66  DECLTYPE);.}.#if
1bc54 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1bc55 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
1bc56 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
1bc57 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63  lite3_column_dec
1bc58 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f  ltype16(sqlite3_
1bc59 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bc5a 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
1bc5b 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
1bc5c 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
1bc5d 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
1bc5e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bc5f 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45  xt16, COLNAME_DE
1bc60 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 65 6e 64 69  CLTYPE);.}.#endi
1bc61 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1bc62 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66  _UTF16 */.#endif
1bc63 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1bc64 44 45 43 4c 54 59 50 45 20 2a 2f 0a 0a 23 69 66  DECLTYPE */..#if
1bc65 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
1bc66 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
1bc67 41 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  A./*.** Return t
1bc68 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  he name of the d
1bc69 61 74 61 62 61 73 65 20 66 72 6f 6d 20 77 68 69  atabase from whi
1bc6a 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75  ch a result colu
1bc6b 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e  mn derives..** N
1bc6c 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
1bc6d 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  if the result co
1bc6e 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65  lumn is an expre
1bc6f 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e  ssion or constan
1bc70 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67  t or.** anything
1bc71 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e   else which is n
1bc72 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73  ot an unabiguous
1bc73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20   reference to a 
1bc74 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e  database column.
1bc75 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
1bc76 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
1bc77 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
1bc78 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  se_name(sqlite3_
1bc79 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bc7a 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
1bc7b 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
1bc7c 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
1bc7d 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
1bc7e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bc7f 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41  xt, COLNAME_DATA
1bc80 42 41 53 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66  BASE);.}.#ifndef
1bc81 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1bc82 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  16.SQLITE_API co
1bc83 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
1bc84 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  3_column_databas
1bc85 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33  e_name16(sqlite3
1bc86 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1bc87 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  t N){.  return c
1bc88 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20  olumnName(.     
1bc89 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73   pStmt, N, (cons
1bc8a 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29  t void*(*)(Mem*)
1bc8b 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
1bc8c 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44  ext16, COLNAME_D
1bc8d 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 65 6e 64  ATABASE);.}.#end
1bc8e 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1bc8f 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a  T_UTF16 */../*.*
1bc90 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d  * Return the nam
1bc91 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 66  e of the table f
1bc92 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75  rom which a resu
1bc93 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65  lt column derive
1bc94 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65  s..** NULL is re
1bc95 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65  turned if the re
1bc96 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61  sult column is a
1bc97 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20  n expression or 
1bc98 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61  constant or.** a
1bc99 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69  nything else whi
1bc9a 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61  ch is not an una
1bc9b 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63  biguous referenc
1bc9c 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  e to a database 
1bc9d 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  column..*/.SQLIT
1bc9e 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
1bc9f 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
1bca0 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69  _table_name(sqli
1bca1 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bca2 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72   int N){.  retur
1bca3 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20  n columnName(.  
1bca4 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63      pStmt, N, (c
1bca5 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65  onst void*(*)(Me
1bca6 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  m*))sqlite3_valu
1bca7 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f  e_text, COLNAME_
1bca8 54 41 42 4c 45 29 3b 0a 7d 0a 23 69 66 6e 64 65  TABLE);.}.#ifnde
1bca9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
1bcaa 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  F16.SQLITE_API c
1bcab 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
1bcac 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
1bcad 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73  name16(sqlite3_s
1bcae 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bcaf 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  N){.  return col
1bcb0 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70  umnName(.      p
1bcb1 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20  Stmt, N, (const 
1bcb2 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73  void*(*)(Mem*))s
1bcb3 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1bcb4 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42  t16, COLNAME_TAB
1bcb5 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  LE);.}.#endif /*
1bcb6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1bcb7 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  16 */../*.** Ret
1bcb8 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  urn the name of 
1bcb9 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  the table column
1bcba 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65   from which a re
1bcbb 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69  sult column deri
1bcbc 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20  ves..** NULL is 
1bcbd 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20  returned if the 
1bcbe 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73  result column is
1bcbf 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f   an expression o
1bcc0 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a  r constant or.**
1bcc1 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77   anything else w
1bcc2 68 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75  hich is not an u
1bcc3 6e 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 65  nabiguous refere
1bcc4 6e 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 73  nce to a databas
1bcc5 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c  e column..*/.SQL
1bcc6 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
1bcc7 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
1bcc8 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73  mn_origin_name(s
1bcc9 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bcca 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
1bccb 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
1bccc 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
1bccd 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
1bcce 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1bccf 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41  alue_text, COLNA
1bcd0 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69  ME_COLUMN);.}.#i
1bcd1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1bcd2 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41  T_UTF16.SQLITE_A
1bcd3 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
1bcd4 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
1bcd5 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69  igin_name16(sqli
1bcd6 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bcd7 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72   int N){.  retur
1bcd8 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20  n columnName(.  
1bcd9 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63      pStmt, N, (c
1bcda 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65  onst void*(*)(Me
1bcdb 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  m*))sqlite3_valu
1bcdc 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d  e_text16, COLNAM
1bcdd 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 6e  E_COLUMN);.}.#en
1bcde 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1bcdf 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64  IT_UTF16 */.#end
1bce0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41  if /* SQLITE_ENA
1bce1 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44  BLE_COLUMN_METAD
1bce2 41 54 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a  ATA */.../******
1bce3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bce4 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65  ********* sqlite
1bce5 33 5f 62 69 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a  3_bind_  *******
1bce6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bce7 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 74  ****.** .** Rout
1bce8 69 6e 65 73 20 75 73 65 64 20 74 6f 20 61 74 74  ines used to att
1bce9 61 63 68 20 76 61 6c 75 65 73 20 74 6f 20 77 69  ach values to wi
1bcea 6c 64 63 61 72 64 73 20 69 6e 20 61 20 63 6f 6d  ldcards in a com
1bceb 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d  piled SQL statem
1bcec 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e  ent..*/./*.** Un
1bced 62 69 6e 64 20 74 68 65 20 76 61 6c 75 65 20 62  bind the value b
1bcee 6f 75 6e 64 20 74 6f 20 76 61 72 69 61 62 6c 65  ound to variable
1bcef 20 69 20 69 6e 20 76 69 72 74 75 61 6c 20 6d 61   i in virtual ma
1bcf0 63 68 69 6e 65 20 70 2e 20 54 68 69 73 20 69 73  chine p. This is
1bcf1 20 74 68 65 20 0a 2a 2a 20 74 68 65 20 73 61 6d   the .** the sam
1bcf2 65 20 61 73 20 62 69 6e 64 69 6e 67 20 61 20 4e  e as binding a N
1bcf3 55 4c 4c 20 76 61 6c 75 65 20 74 6f 20 74 68 65  ULL value to the
1bcf4 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 20   column. If the 
1bcf5 22 69 22 20 70 61 72 61 6d 65 74 65 72 20 69 73  "i" parameter is
1bcf6 0a 2a 2a 20 6f 75 74 20 6f 66 20 72 61 6e 67 65  .** out of range
1bcf7 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 52 41  , then SQLITE_RA
1bcf8 4e 47 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e  NGE is returned.
1bcf9 20 4f 74 68 65 77 69 73 65 20 53 51 4c 49 54 45   Othewise SQLITE
1bcfa 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 75 63  _OK..**.** A suc
1bcfb 63 65 73 73 66 75 6c 20 65 76 61 6c 75 61 74 69  cessful evaluati
1bcfc 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  on of this routi
1bcfd 6e 65 20 61 63 71 75 69 72 65 73 20 74 68 65 20  ne acquires the 
1bcfe 6d 75 74 65 78 20 6f 6e 20 70 2e 0a 2a 2a 20 74  mutex on p..** t
1bcff 68 65 20 6d 75 74 65 78 20 69 73 20 72 65 6c 65  he mutex is rele
1bd00 61 73 65 64 20 69 66 20 61 6e 79 20 6b 69 6e 64  ased if any kind
1bd01 20 6f 66 20 65 72 72 6f 72 20 6f 63 63 75 72 73   of error occurs
1bd02 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 72 72 6f  ..**.** The erro
1bd03 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e  r code stored in
1bd04 20 64 61 74 61 62 61 73 65 20 70 2d 3e 64 62 20   database p->db 
1bd05 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77  is overwritten w
1bd06 69 74 68 20 74 68 65 20 72 65 74 75 72 6e 0a 2a  ith the return.*
1bd07 2a 20 76 61 6c 75 65 20 69 6e 20 61 6e 79 20 63  * value in any c
1bd08 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ase..*/.static i
1bd09 6e 74 20 76 64 62 65 55 6e 62 69 6e 64 28 56 64  nt vdbeUnbind(Vd
1bd0a 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20  be *p, int i){. 
1bd0b 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 66   Mem *pVar;.  if
1bd0c 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
1bd0d 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1bd0e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1bd0f 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  nter(p->db->mute
1bd10 78 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67  x);.  if( p->mag
1bd11 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic!=VDBE_MAGIC_R
1bd12 55 4e 20 7c 7c 20 70 2d 3e 70 63 3e 3d 30 20 29  UN || p->pc>=0 )
1bd13 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1bd14 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45  or(p->db, SQLITE
1bd15 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20  _MISUSE, 0);.   
1bd16 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1bd17 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  eave(p->db->mute
1bd18 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  x);.    return S
1bd19 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
1bd1a 7d 0a 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69  }.  if( i<1 || i
1bd1b 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20  >p->nVar ){.    
1bd1c 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e  sqlite3Error(p->
1bd1d 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 45  db, SQLITE_RANGE
1bd1e 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1bd1f 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
1bd20 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
1bd21 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52   return SQLITE_R
1bd22 41 4e 47 45 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b  ANGE;.  }.  i--;
1bd23 0a 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56  .  pVar = &p->aV
1bd24 61 72 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65 33  ar[i];.  sqlite3
1bd25 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
1bd26 56 61 72 29 3b 0a 20 20 70 56 61 72 2d 3e 66 6c  Var);.  pVar->fl
1bd27 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
1bd28 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70    sqlite3Error(p
1bd29 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c  ->db, SQLITE_OK,
1bd2a 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68   0);..  /* If th
1bd2b 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64  e bit correspond
1bd2c 69 6e 67 20 74 6f 20 74 68 69 73 20 76 61 72 69  ing to this vari
1bd2d 61 62 6c 65 20 69 6e 20 56 64 62 65 2e 65 78 70  able in Vdbe.exp
1bd2e 6d 61 73 6b 20 69 73 20 73 65 74 2c 20 74 68 65  mask is set, the
1bd2f 6e 20 0a 20 20 2a 2a 20 62 69 6e 64 69 6e 67 20  n .  ** binding 
1bd30 61 20 6e 65 77 20 76 61 6c 75 65 20 74 6f 20 74  a new value to t
1bd31 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e 76  his variable inv
1bd32 61 6c 69 64 61 74 65 73 20 74 68 65 20 63 75 72  alidates the cur
1bd33 72 65 6e 74 20 71 75 65 72 79 20 70 6c 61 6e 2e  rent query plan.
1bd34 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69  .  */.  if( p->i
1bd35 73 50 72 65 70 61 72 65 56 32 20 26 26 0a 20 20  sPrepareV2 &&.  
1bd36 20 20 20 28 28 69 3c 33 32 20 26 26 20 70 2d 3e     ((i<32 && p->
1bd37 65 78 70 6d 61 73 6b 20 26 20 28 28 75 33 32 29  expmask & ((u32)
1bd38 31 20 3c 3c 20 69 29 29 20 7c 7c 20 70 2d 3e 65  1 << i)) || p->e
1bd39 78 70 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66 66  xpmask==0xffffff
1bd3a 66 66 29 0a 20 20 29 7b 0a 20 20 20 20 70 2d 3e  ff).  ){.    p->
1bd3b 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d  expired = 1;.  }
1bd3c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1bd3d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69  _OK;.}../*.** Bi
1bd3e 6e 64 20 61 20 74 65 78 74 20 6f 72 20 42 4c 4f  nd a text or BLO
1bd3f 42 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  B value..*/.stat
1bd40 69 63 20 69 6e 74 20 62 69 6e 64 54 65 78 74 28  ic int bindText(
1bd41 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
1bd42 2a 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 68 65  *pStmt,   /* The
1bd43 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 69   statement to bi
1bd44 6e 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20  nd against */.  
1bd45 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20  int i,          
1bd46 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
1bd47 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  of the parameter
1bd48 20 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 63 6f   to bind */.  co
1bd49 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c  nst void *zData,
1bd4a 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
1bd4b 74 6f 20 74 68 65 20 64 61 74 61 20 74 6f 20 62  to the data to b
1bd4c 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e 74  e bound */.  int
1bd4d 20 6e 44 61 74 61 2c 20 20 20 20 20 20 20 20 20   nData,         
1bd4e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1bd4f 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 74   bytes of data t
1bd50 6f 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20  o be bound */.  
1bd51 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
1bd52 64 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 72 75  d*),   /* Destru
1bd53 63 74 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74  ctor for the dat
1bd54 61 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69  a */.  u8 encodi
1bd55 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ng            /*
1bd56 20 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68   Encoding for th
1bd57 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 56  e data */.){.  V
1bd58 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a  dbe *p = (Vdbe *
1bd59 29 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 2a 70  )pStmt;.  Mem *p
1bd5a 56 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  Var;.  int rc;..
1bd5b 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e    rc = vdbeUnbin
1bd5c 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72  d(p, i);.  if( r
1bd5d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1bd5e 20 20 20 20 69 66 28 20 7a 44 61 74 61 21 3d 30      if( zData!=0
1bd5f 20 29 7b 0a 20 20 20 20 20 20 70 56 61 72 20 3d   ){.      pVar =
1bd60 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b 0a   &p->aVar[i-1];.
1bd61 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1bd62 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
1bd63 70 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61  pVar, zData, nDa
1bd64 74 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44  ta, encoding, xD
1bd65 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  el);.      if( r
1bd66 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1bd67 65 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b 0a 20  encoding!=0 ){. 
1bd68 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1bd69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
1bd6a 6f 64 69 6e 67 28 70 56 61 72 2c 20 45 4e 43 28  oding(pVar, ENC(
1bd6b 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 7d  p->db));.      }
1bd6c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1bd6d 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30  ror(p->db, rc, 0
1bd6e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
1bd6f 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e  lite3ApiExit(p->
1bd70 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20  db, rc);.    }. 
1bd71 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1bd72 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75  _leave(p->db->mu
1bd73 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
1bd74 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
1bd75 20 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 61 6c   Bind a blob val
1bd76 75 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 74 61  ue to an SQL sta
1bd77 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 2e  tement variable.
1bd78 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1bd79 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
1bd7a 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f  blob(.  sqlite3_
1bd7b 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20  stmt *pStmt, .  
1bd7c 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20  int i, .  const 
1bd7d 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20  void *zData, .  
1bd7e 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f  int nData, .  vo
1bd7f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
1bd80 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69  ).){.  return bi
1bd81 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c  ndText(pStmt, i,
1bd82 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78   zData, nData, x
1bd83 44 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54  Del, 0);.}.SQLIT
1bd84 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bd85 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71  3_bind_double(sq
1bd86 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bd87 74 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 6c 65  t, int i, double
1bd88 20 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20   rValue){.  int 
1bd89 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  rc;.  Vdbe *p = 
1bd8a 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
1bd8b 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64   rc = vdbeUnbind
1bd8c 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63  (p, i);.  if( rc
1bd8d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1bd8e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1bd8f 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 2d 3e 61  mSetDouble(&p->a
1bd90 56 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c 75 65  Var[i-1], rValue
1bd91 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
1bd92 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62  utex_leave(p->db
1bd93 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  ->mutex);.  }.  
1bd94 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
1bd95 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1bd96 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c  te3_bind_int(sql
1bd97 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e  ite3_stmt *p, in
1bd98 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29  t i, int iValue)
1bd99 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
1bd9a 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c  e3_bind_int64(p,
1bd9b 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 29   i, (i64)iValue)
1bd9c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
1bd9d 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
1bd9e 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74  int64(sqlite3_st
1bd9f 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1bda0 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
1bda1 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63  Value){.  int rc
1bda2 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  ;.  Vdbe *p = (V
1bda3 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72  dbe *)pStmt;.  r
1bda4 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70  c = vdbeUnbind(p
1bda5 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , i);.  if( rc==
1bda6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1bda7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1bda8 65 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 61 72  etInt64(&p->aVar
1bda9 5b 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 3b 0a  [i-1], iValue);.
1bdaa 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1bdab 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
1bdac 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74  utex);.  }.  ret
1bdad 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45  urn rc;.}.SQLITE
1bdae 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1bdaf 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74  _bind_null(sqlit
1bdb0 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bdb1 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 72 63  int i){.  int rc
1bdb2 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  ;.  Vdbe *p = (V
1bdb3 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63  dbe*)pStmt;.  rc
1bdb4 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c   = vdbeUnbind(p,
1bdb5 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   i);.  if( rc==S
1bdb6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1bdb7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1bdb8 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
1bdb9 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1bdba 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  rc;.}.SQLITE_API
1bdbb 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
1bdbc 64 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69 74  d_text( .  sqlit
1bdbd 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bdbe 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e  .  int i, .  con
1bdbf 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c 20  st char *zData, 
1bdc0 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20  .  int nData, . 
1bdc1 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
1bdc2 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e  id*).){.  return
1bdc3 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c   bindText(pStmt,
1bdc4 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61   i, zData, nData
1bdc5 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55  , xDel, SQLITE_U
1bdc6 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  TF8);.}.#ifndef 
1bdc7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1bdc8 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  6.SQLITE_API int
1bdc9 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
1bdca 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f  xt16(.  sqlite3_
1bdcb 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20  stmt *pStmt, .  
1bdcc 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20  int i, .  const 
1bdcd 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20  void *zData, .  
1bdce 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f  int nData, .  vo
1bdcf 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
1bdd0 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69  ).){.  return bi
1bdd1 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c  ndText(pStmt, i,
1bdd2 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78   zData, nData, x
1bdd3 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Del, SQLITE_UTF1
1bdd4 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 6e 64  6NATIVE);.}.#end
1bdd5 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1bdd6 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54  T_UTF16 */.SQLIT
1bdd7 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bdd8 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c  3_bind_value(sql
1bdd9 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bdda 2c 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 20 73  , int i, const s
1bddb 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
1bddc 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  alue){.  int rc;
1bddd 0a 20 20 73 77 69 74 63 68 28 20 70 56 61 6c 75  .  switch( pValu
1bdde 65 2d 3e 74 79 70 65 20 29 7b 0a 20 20 20 20 63  e->type ){.    c
1bddf 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  ase SQLITE_INTEG
1bde0 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d  ER: {.      rc =
1bde1 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
1bde2 74 36 34 28 70 53 74 6d 74 2c 20 69 2c 20 70 56  t64(pStmt, i, pV
1bde3 61 6c 75 65 2d 3e 75 2e 69 29 3b 0a 20 20 20 20  alue->u.i);.    
1bde4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1bde5 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
1bde6 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 72 63  LOAT: {.      rc
1bde7 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
1bde8 64 6f 75 62 6c 65 28 70 53 74 6d 74 2c 20 69 2c  double(pStmt, i,
1bde9 20 70 56 61 6c 75 65 2d 3e 72 29 3b 0a 20 20 20   pValue->r);.   
1bdea 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1bdeb 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
1bdec 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 66  BLOB: {.      if
1bded 28 20 70 56 61 6c 75 65 2d 3e 66 6c 61 67 73 20  ( pValue->flags 
1bdee 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
1bdef 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1bdf0 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  e3_bind_zeroblob
1bdf1 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75  (pStmt, i, pValu
1bdf2 65 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20  e->u.nZero);.   
1bdf3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1bdf4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
1bdf5 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20  ind_blob(pStmt, 
1bdf6 69 2c 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 56  i, pValue->z, pV
1bdf7 61 6c 75 65 2d 3e 6e 2c 53 51 4c 49 54 45 5f 54  alue->n,SQLITE_T
1bdf8 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20  RANSIENT);.     
1bdf9 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1bdfa 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
1bdfb 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20  QLITE_TEXT: {.  
1bdfc 20 20 20 20 72 63 20 3d 20 62 69 6e 64 54 65 78      rc = bindTex
1bdfd 74 28 70 53 74 6d 74 2c 69 2c 20 20 70 56 61 6c  t(pStmt,i,  pVal
1bdfe 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e  ue->z, pValue->n
1bdff 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
1be00 4e 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  NT,.            
1be01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1be02 20 20 70 56 61 6c 75 65 2d 3e 65 6e 63 29 3b 0a    pValue->enc);.
1be03 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1be04 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
1be05 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1be06 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70  ite3_bind_null(p
1be07 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20  Stmt, i);.      
1be08 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
1be09 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1be0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1be0b 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f  qlite3_bind_zero
1be0c 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d  blob(sqlite3_stm
1be0d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c  t *pStmt, int i,
1be0e 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 72   int n){.  int r
1be0f 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  c;.  Vdbe *p = (
1be10 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
1be11 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28  rc = vdbeUnbind(
1be12 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d  p, i);.  if( rc=
1be13 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1be14 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1be15 53 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e  SetZeroBlob(&p->
1be16 61 56 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20  aVar[i-1], n);. 
1be17 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1be18 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75  _leave(p->db->mu
1be19 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
1be1a 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1be1b 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1be1c 72 20 6f 66 20 77 69 6c 64 63 61 72 64 73 20 74  r of wildcards t
1be1d 68 61 74 20 63 61 6e 20 62 65 20 70 6f 74 65 6e  hat can be poten
1be1e 74 69 61 6c 6c 79 20 62 6f 75 6e 64 20 74 6f 2e  tially bound to.
1be1f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1be20 20 69 73 20 61 64 64 65 64 20 74 6f 20 73 75 70   is added to sup
1be21 70 6f 72 74 20 44 42 44 3a 3a 53 51 4c 69 74 65  port DBD::SQLite
1be22 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  .  .*/.SQLITE_AP
1be23 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
1be24 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75  nd_parameter_cou
1be25 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  nt(sqlite3_stmt 
1be26 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
1be27 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *p = (Vdbe*)pStm
1be28 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20  t;.  return p ? 
1be29 70 2d 3e 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a  p->nVar : 0;.}..
1be2a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6d  /*.** Create a m
1be2b 61 70 70 69 6e 67 20 66 72 6f 6d 20 76 61 72 69  apping from vari
1be2c 61 62 6c 65 20 6e 75 6d 62 65 72 73 20 74 6f 20  able numbers to 
1be2d 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 73 0a 2a  variable names.*
1be2e 2a 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 7a  * in the Vdbe.az
1be2f 56 61 72 5b 5d 20 61 72 72 61 79 2c 20 69 66 20  Var[] array, if 
1be30 73 75 63 68 20 61 20 6d 61 70 70 69 6e 67 20 64  such a mapping d
1be31 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a  oes not already.
1be32 2a 2a 20 65 78 69 73 74 2e 0a 2a 2f 0a 73 74 61  ** exist..*/.sta
1be33 74 69 63 20 76 6f 69 64 20 63 72 65 61 74 65 56  tic void createV
1be34 61 72 4d 61 70 28 56 64 62 65 20 2a 70 29 7b 0a  arMap(Vdbe *p){.
1be35 20 20 69 66 28 20 21 70 2d 3e 6f 6b 56 61 72 20    if( !p->okVar 
1be36 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20  ){.    int j;.  
1be37 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 73    Op *pOp;.    s
1be38 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
1be39 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  er(p->db->mutex)
1be3a 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 61 63  ;.    /* The rac
1be3b 65 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65  e condition here
1be3c 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 49   is harmless.  I
1be3d 66 20 74 77 6f 20 74 68 72 65 61 64 73 20 63 61  f two threads ca
1be3e 6c 6c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 72  ll this.    ** r
1be3f 6f 75 74 69 6e 65 20 6f 6e 20 74 68 65 20 73 61  outine on the sa
1be40 6d 65 20 56 64 62 65 20 61 74 20 74 68 65 20 73  me Vdbe at the s
1be41 61 6d 65 20 74 69 6d 65 2c 20 74 68 65 79 20 62  ame time, they b
1be42 6f 74 68 20 6d 69 67 68 74 20 65 6e 64 0a 20 20  oth might end.  
1be43 20 20 2a 2a 20 75 70 20 69 6e 69 74 69 61 6c 69    ** up initiali
1be44 7a 69 6e 67 20 74 68 65 20 56 64 62 65 2e 61 7a  zing the Vdbe.az
1be45 56 61 72 5b 5d 20 61 72 72 61 79 2e 20 20 54 68  Var[] array.  Th
1be46 61 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 65  at is a little e
1be47 78 74 72 61 0a 20 20 20 20 2a 2a 20 77 6f 72 6b  xtra.    ** work
1be48 20 62 75 74 20 69 74 20 72 65 73 75 6c 74 73 20   but it results 
1be49 69 6e 20 74 68 65 20 73 61 6d 65 20 61 6e 73 77  in the same answ
1be4a 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  er..    */.    f
1be4b 6f 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61  or(j=0, pOp=p->a
1be4c 4f 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b  Op; j<p->nOp; j+
1be4d 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20  +, pOp++){.     
1be4e 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1be4f 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29 7b  ==OP_Variable ){
1be50 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1be51 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f   pOp->p1>0 && pO
1be52 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72 20 29  p->p1<=p->nVar )
1be53 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 7a 56  ;.        p->azV
1be54 61 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20  ar[pOp->p1-1] = 
1be55 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20  pOp->p4.z;.     
1be56 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e   }.    }.    p->
1be57 6f 6b 56 61 72 20 3d 20 31 3b 0a 20 20 20 20 73  okVar = 1;.    s
1be58 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1be59 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
1be5a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
1be5b 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f  eturn the name o
1be5c 66 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72  f a wildcard par
1be5d 61 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e 20  ameter.  Return 
1be5e 4e 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64 65  NULL if the inde
1be5f 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20 72  x.** is out of r
1be60 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 77  ange or if the w
1be61 69 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61 6d  ildcard is unnam
1be62 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ed..**.** The re
1be63 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 55  sult is always U
1be64 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  TF-8..*/.SQLITE_
1be65 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
1be66 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
1be67 61 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69  ameter_name(sqli
1be68 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1be69 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 20   int i){.  Vdbe 
1be6a 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *p = (Vdbe*)pStm
1be6b 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c  t;.  if( p==0 ||
1be6c 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61   i<1 || i>p->nVa
1be6d 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  r ){.    return 
1be6e 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56  0;.  }.  createV
1be6f 61 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74 75  arMap(p);.  retu
1be70 72 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d  rn p->azVar[i-1]
1be71 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  ;.}../*.** Given
1be72 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61   a wildcard para
1be73 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74 75  meter name, retu
1be74 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
1be75 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20  the variable.** 
1be76 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e 20  with that name. 
1be77 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
1be78 76 61 72 69 61 62 6c 65 20 77 69 74 68 20 74 68  variable with th
1be79 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a  e given name,.**
1be7a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51   return 0..*/.SQ
1be7b 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1be7c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
1be7d 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65  ter_index(sqlite
1be7e 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63  3_stmt *pStmt, c
1be7f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1be80 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  ){.  Vdbe *p = (
1be81 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69  Vdbe*)pStmt;.  i
1be82 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30  nt i;.  if( p==0
1be83 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
1be84 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61  ;.  }.  createVa
1be85 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 28 20  rMap(p); .  if( 
1be86 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72  zName ){.    for
1be87 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b  (i=0; i<p->nVar;
1be88 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e   i++){.      con
1be89 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 2d 3e  st char *z = p->
1be8a 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 20 20  azVar[i];.      
1be8b 69 66 28 20 7a 20 26 26 20 73 74 72 63 6d 70 28  if( z && strcmp(
1be8c 7a 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20  z,zName)==0 ){. 
1be8d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b         return i+
1be8e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
1be8f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
1be90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66  .}../*.** Transf
1be91 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73 20  er all bindings 
1be92 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 73  from the first s
1be93 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74 6f  tatement over to
1be94 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2f 0a   the second..*/.
1be95 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1be96 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66  nt sqlite3Transf
1be97 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  erBindings(sqlit
1be98 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74  e3_stmt *pFromSt
1be99 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  mt, sqlite3_stmt
1be9a 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64   *pToStmt){.  Vd
1be9b 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62  be *pFrom = (Vdb
1be9c 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20  e*)pFromStmt;.  
1be9d 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62  Vdbe *pTo = (Vdb
1be9e 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 6e  e*)pToStmt;.  in
1be9f 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t i;.  assert( p
1bea0 54 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d 3e 64  To->db==pFrom->d
1bea1 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  b );.  assert( p
1bea2 54 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f 6d 2d  To->nVar==pFrom-
1bea3 3e 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c 69 74  >nVar );.  sqlit
1bea4 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
1bea5 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  To->db->mutex);.
1bea6 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 72    for(i=0; i<pFr
1bea7 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a  om->nVar; i++){.
1bea8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1bea9 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 61  emMove(&pTo->aVa
1beaa 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 56  r[i], &pFrom->aV
1beab 61 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71  ar[i]);.  }.  sq
1beac 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1bead 65 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78  e(pTo->db->mutex
1beae 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
1beaf 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  TE_OK;.}..#ifnde
1beb0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
1beb1 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44  PRECATED./*.** D
1beb2 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72 6e  eprecated extern
1beb3 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49  al interface.  I
1beb4 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c  nternal/core SQL
1beb5 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75  ite code.** shou
1beb6 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 54  ld call sqlite3T
1beb7 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 2e  ransferBindings.
1beb8 0a 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d 69 73  .**.** Is is mis
1beb9 75 73 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73  use to call this
1beba 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 73 74   routine with st
1bebb 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 64 69  atements from di
1bebc 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62  fferent.** datab
1bebd 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ase connections.
1bebe 20 20 42 75 74 20 61 73 20 74 68 69 73 20 69 73    But as this is
1bebf 20 61 20 64 65 70 72 65 63 61 74 65 64 20 69 6e   a deprecated in
1bec0 74 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a 20 77  terface, we.** w
1bec1 69 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 20 74  ill not bother t
1bec2 6f 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61 74  o check for that
1bec3 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a   condition..**.*
1bec4 2a 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 61  * If the two sta
1bec5 74 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20  tements contain 
1bec6 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62  a different numb
1bec7 65 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20  er of bindings, 
1bec8 74 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54  then.** an SQLIT
1bec9 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72  E_ERROR is retur
1beca 6e 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20 65 6c  ned.  Nothing el
1becb 73 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 2c  se can go wrong,
1becc 20 73 6f 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a   so otherwise.**
1becd 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
1bece 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
1becf 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bed0 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69  3_transfer_bindi
1bed1 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ngs(sqlite3_stmt
1bed2 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c   *pFromStmt, sql
1bed3 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74  ite3_stmt *pToSt
1bed4 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72  mt){.  Vdbe *pFr
1bed5 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f  om = (Vdbe*)pFro
1bed6 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70  mStmt;.  Vdbe *p
1bed7 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53  To = (Vdbe*)pToS
1bed8 74 6d 74 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d  tmt;.  if( pFrom
1bed9 2d 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e 56 61  ->nVar!=pTo->nVa
1beda 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  r ){.    return 
1bedb 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1bedc 7d 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69 73 50  }.  if( pTo->isP
1bedd 72 65 70 61 72 65 56 32 20 26 26 20 70 54 6f 2d  repareV2 && pTo-
1bede 3e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20 20 20  >expmask ){.    
1bedf 70 54 6f 2d 3e 65 78 70 69 72 65 64 20 3d 20 31  pTo->expired = 1
1bee0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 72 6f  ;.  }.  if( pFro
1bee1 6d 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 26  m->isPrepareV2 &
1bee2 26 20 70 46 72 6f 6d 2d 3e 65 78 70 6d 61 73 6b  & pFrom->expmask
1bee3 20 29 7b 0a 20 20 20 20 70 46 72 6f 6d 2d 3e 65   ){.    pFrom->e
1bee4 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  xpired = 1;.  }.
1bee5 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1bee6 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73  TransferBindings
1bee7 28 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54 6f 53  (pFromStmt, pToS
1bee8 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  tmt);.}.#endif..
1bee9 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1beea 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61   sqlite3* databa
1beeb 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 68 69  se handle to whi
1beec 63 68 20 74 68 65 20 70 72 65 70 61 72 65 64 20  ch the prepared 
1beed 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 0a  statement given.
1beee 2a 2a 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65  ** in the argume
1beef 6e 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 69  nt belongs.  Thi
1bef0 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 64 61  s is the same da
1bef1 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68  tabase handle th
1bef2 61 74 20 77 61 73 0a 2a 2a 20 74 68 65 20 66 69  at was.** the fi
1bef3 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
1bef4 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  the sqlite3_prep
1bef5 61 72 65 28 29 20 74 68 61 74 20 77 61 73 20 75  are() that was u
1bef6 73 65 64 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a  sed to create.**
1bef7 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
1bef8 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63  n the first plac
1bef9 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
1befa 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65   sqlite3 *sqlite
1befb 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69  3_db_handle(sqli
1befc 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
1befd 7b 0a 20 20 72 65 74 75 72 6e 20 70 53 74 6d 74  {.  return pStmt
1befe 20 3f 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74   ? ((Vdbe*)pStmt
1beff 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a  )->db : 0;.}../*
1bf00 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
1bf01 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74  nter to the next
1bf02 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1bf03 65 6e 74 20 61 66 74 65 72 20 70 53 74 6d 74 20  ent after pStmt 
1bf04 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
1bf05 74 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  th database conn
1bf06 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20  ection pDb.  If 
1bf07 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c 20 72  pStmt is NULL, r
1bf08 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 0a  eturn the first.
1bf09 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74  ** prepared stat
1bf0a 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 64 61  ement for the da
1bf0b 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1bf0c 6e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  n.  Return NULL 
1bf0d 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20  if there.** are 
1bf0e 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49  no more..*/.SQLI
1bf0f 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 73  TE_API sqlite3_s
1bf10 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78  tmt *sqlite3_nex
1bf11 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 2a  t_stmt(sqlite3 *
1bf12 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d  pDb, sqlite3_stm
1bf13 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 73 71 6c  t *pStmt){.  sql
1bf14 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 78 74  ite3_stmt *pNext
1bf15 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
1bf16 78 5f 65 6e 74 65 72 28 70 44 62 2d 3e 6d 75 74  x_enter(pDb->mut
1bf17 65 78 29 3b 0a 20 20 69 66 28 20 70 53 74 6d 74  ex);.  if( pStmt
1bf18 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 78 74  ==0 ){.    pNext
1bf19 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74   = (sqlite3_stmt
1bf1a 2a 29 70 44 62 2d 3e 70 56 64 62 65 3b 0a 20 20  *)pDb->pVdbe;.  
1bf1b 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 78 74  }else{.    pNext
1bf1c 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74   = (sqlite3_stmt
1bf1d 2a 29 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29  *)((Vdbe*)pStmt)
1bf1e 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 73  ->pNext;.  }.  s
1bf1f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1bf20 76 65 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a  ve(pDb->mutex);.
1bf21 20 20 72 65 74 75 72 6e 20 70 4e 65 78 74 3b 0a    return pNext;.
1bf22 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1bf23 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73  the value of a s
1bf24 74 61 74 75 73 20 63 6f 75 6e 74 65 72 20 66 6f  tatus counter fo
1bf25 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61  r a prepared sta
1bf26 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45  tement.*/.SQLITE
1bf27 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1bf28 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c  _stmt_status(sql
1bf29 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bf2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 72 65  , int op, int re
1bf2b 73 65 74 46 6c 61 67 29 7b 0a 20 20 56 64 62 65  setFlag){.  Vdbe
1bf2c 20 2a 70 56 64 62 65 20 3d 20 28 56 64 62 65 2a   *pVdbe = (Vdbe*
1bf2d 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 76 20  )pStmt;.  int v 
1bf2e 3d 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65  = pVdbe->aCounte
1bf2f 72 5b 6f 70 2d 31 5d 3b 0a 20 20 69 66 28 20 72  r[op-1];.  if( r
1bf30 65 73 65 74 46 6c 61 67 20 29 20 70 56 64 62 65  esetFlag ) pVdbe
1bf31 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d  ->aCounter[op-1]
1bf32 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 76   = 0;.  return v
1bf33 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
1bf34 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65  **** End of vdbe
1bf35 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  api.c **********
1bf36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf38 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1bf39 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1bf3a 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vdbe.c *********
1bf3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf3d 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
1bf3e 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
1bf3f 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1bf40 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1bf41 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1bf42 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1bf43 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1bf44 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1bf45 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1bf46 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1bf47 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1bf48 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1bf49 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1bf4a 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1bf4b 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1bf4c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1bf4d 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1bf4e 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1bf4f 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1bf50 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1bf51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
1bf55 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
1bf56 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  file implements 
1bf57 65 78 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f 64  execution method
1bf58 20 6f 66 20 74 68 65 20 0a 2a 2a 20 56 69 72 74   of the .** Virt
1bf59 75 61 6c 20 44 61 74 61 62 61 73 65 20 45 6e 67  ual Database Eng
1bf5a 69 6e 65 20 28 56 44 42 45 29 2e 20 20 41 20 73  ine (VDBE).  A s
1bf5b 65 70 61 72 61 74 65 20 66 69 6c 65 20 28 22 76  eparate file ("v
1bf5c 64 62 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68 61  dbeaux.c").** ha
1bf5d 6e 64 6c 65 73 20 68 6f 75 73 65 6b 65 65 70 69  ndles housekeepi
1bf5e 6e 67 20 64 65 74 61 69 6c 73 20 73 75 63 68 20  ng details such 
1bf5f 61 73 20 63 72 65 61 74 69 6e 67 20 61 6e 64 20  as creating and 
1bf60 64 65 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42 45  deleting.** VDBE
1bf61 20 69 6e 73 74 61 6e 63 65 73 2e 20 20 54 68 69   instances.  Thi
1bf62 73 20 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c 79  s file is solely
1bf63 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 20 65   interested in e
1bf64 78 65 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65 20  xecuting.** the 
1bf65 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a  VDBE program..**
1bf66 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 74 65 72  .** In the exter
1bf67 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 61  nal interface, a
1bf68 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  n "sqlite3_stmt*
1bf69 22 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 70  " is an opaque p
1bf6a 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 56  ointer.** to a V
1bf6b 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53  DBE..**.** The S
1bf6c 51 4c 20 70 61 72 73 65 72 20 67 65 6e 65 72 61  QL parser genera
1bf6d 74 65 73 20 61 20 70 72 6f 67 72 61 6d 20 77 68  tes a program wh
1bf6e 69 63 68 20 69 73 20 74 68 65 6e 20 65 78 65 63  ich is then exec
1bf6f 75 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 56  uted by.** the V
1bf70 44 42 45 20 74 6f 20 64 6f 20 74 68 65 20 77 6f  DBE to do the wo
1bf71 72 6b 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74  rk of the SQL st
1bf72 61 74 65 6d 65 6e 74 2e 20 20 56 44 42 45 20 70  atement.  VDBE p
1bf73 72 6f 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a 20  rograms are .** 
1bf74 73 69 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d 20  similar in form 
1bf75 74 6f 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e 67  to assembly lang
1bf76 75 61 67 65 2e 20 20 54 68 65 20 70 72 6f 67 72  uage.  The progr
1bf77 61 6d 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a 2a  am consists of.*
1bf78 2a 20 61 20 6c 69 6e 65 61 72 20 73 65 71 75 65  * a linear seque
1bf79 6e 63 65 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e  nce of operation
1bf7a 73 2e 20 20 45 61 63 68 20 6f 70 65 72 61 74 69  s.  Each operati
1bf7b 6f 6e 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65  on has an opcode
1bf7c 20 0a 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72 61   .** and 5 opera
1bf7d 6e 64 73 2e 20 20 4f 70 65 72 61 6e 64 73 20 50  nds.  Operands P
1bf7e 31 2c 20 50 32 2c 20 61 6e 64 20 50 33 20 61 72  1, P2, and P3 ar
1bf7f 65 20 69 6e 74 65 67 65 72 73 2e 20 20 4f 70 65  e integers.  Ope
1bf80 72 61 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20 61  rand P4 .** is a
1bf81 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64   null-terminated
1bf82 20 73 74 72 69 6e 67 2e 20 20 4f 70 65 72 61 6e   string.  Operan
1bf83 64 20 50 35 20 69 73 20 61 6e 20 75 6e 73 69 67  d P5 is an unsig
1bf84 6e 65 64 20 63 68 61 72 61 63 74 65 72 2e 0a 2a  ned character..*
1bf85 2a 20 46 65 77 20 6f 70 63 6f 64 65 73 20 75 73  * Few opcodes us
1bf86 65 20 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64 73  e all 5 operands
1bf87 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61 74  ..**.** Computat
1bf88 69 6f 6e 20 72 65 73 75 6c 74 73 20 61 72 65 20  ion results are 
1bf89 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 65 74 20  stored on a set 
1bf8a 6f 66 20 72 65 67 69 73 74 65 72 73 20 6e 75 6d  of registers num
1bf8b 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 0a  bered beginning.
1bf8c 2a 2a 20 77 69 74 68 20 31 20 61 6e 64 20 67 6f  ** with 1 and go
1bf8d 69 6e 67 20 75 70 20 74 6f 20 56 64 62 65 2e 6e  ing up to Vdbe.n
1bf8e 4d 65 6d 2e 20 20 45 61 63 68 20 72 65 67 69 73  Mem.  Each regis
1bf8f 74 65 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a 2a  ter can store.**
1bf90 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67   either an integ
1bf91 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69  er, a null-termi
1bf92 6e 61 74 65 64 20 73 74 72 69 6e 67 2c 20 61 20  nated string, a 
1bf93 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a  floating point.*
1bf94 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68 65  * number, or the
1bf95 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c 75   SQL "NULL" valu
1bf96 65 2e 20 20 41 6e 20 69 6d 70 6c 69 63 69 74 20  e.  An implicit 
1bf97 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20  conversion from 
1bf98 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20 74  one.** type to t
1bf99 68 65 20 6f 74 68 65 72 20 6f 63 63 75 72 73 20  he other occurs 
1bf9a 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a  as necessary..**
1bf9b 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65   .** Most of the
1bf9c 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
1bf9d 6c 65 20 69 73 20 74 61 6b 65 6e 20 75 70 20 62  le is taken up b
1bf9e 79 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62  y the sqlite3Vdb
1bf9f 65 45 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63 74  eExec().** funct
1bfa0 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20 74  ion which does t
1bfa1 68 65 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65 72  he work of inter
1bfa2 70 72 65 74 69 6e 67 20 61 20 56 44 42 45 20 70  preting a VDBE p
1bfa3 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f  rogram..** But o
1bfa4 74 68 65 72 20 72 6f 75 74 69 6e 65 73 20 61 72  ther routines ar
1bfa5 65 20 61 6c 73 6f 20 70 72 6f 76 69 64 65 64 20  e also provided 
1bfa6 74 6f 20 68 65 6c 70 20 69 6e 20 62 75 69 6c 64  to help in build
1bfa7 69 6e 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f 67  ing up.** a prog
1bfa8 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ram instruction 
1bfa9 62 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  by instruction..
1bfaa 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73 63  **.** Various sc
1bfab 72 69 70 74 73 20 73 63 61 6e 20 74 68 69 73 20  ripts scan this 
1bfac 73 6f 75 72 63 65 20 66 69 6c 65 20 69 6e 20 6f  source file in o
1bfad 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65  rder to generate
1bfae 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e   HTML.** documen
1bfaf 74 61 74 69 6f 6e 2c 20 68 65 61 64 65 72 73 20  tation, headers 
1bfb0 66 69 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72 20  files, or other 
1bfb1 64 65 72 69 76 65 64 20 66 69 6c 65 73 2e 20 20  derived files.  
1bfb2 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a  The formatting.*
1bfb3 2a 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e  * of the code in
1bfb4 20 74 68 69 73 20 66 69 6c 65 20 69 73 2c 20 74   this file is, t
1bfb5 68 65 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72 74  herefore, import
1bfb6 61 6e 74 2e 20 20 53 65 65 20 6f 74 68 65 72 20  ant.  See other 
1bfb7 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74  comments.** in t
1bfb8 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65 74  his file for det
1bfb9 61 69 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f 75  ails.  If in dou
1bfba 62 74 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69 61  bt, do not devia
1bfbb 74 65 20 66 72 6f 6d 20 65 78 69 73 74 69 6e 67  te from existing
1bfbc 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 61  .** commenting a
1bfbd 6e 64 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 70  nd indentation p
1bfbe 72 61 63 74 69 63 65 73 20 77 68 65 6e 20 63 68  ractices when ch
1bfbf 61 6e 67 69 6e 67 20 6f 72 20 61 64 64 69 6e 67  anging or adding
1bfc0 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64   code..**.** $Id
1bfc1 3a 20 76 64 62 65 2e 63 2c 76 20 31 2e 38 37 34  : vdbe.c,v 1.874
1bfc2 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a 35   2009/07/24 17:5
1bfc3 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 37  8:53 danielk1977
1bfc4 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
1bfc5 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67   The following g
1bfc6 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69  lobal variable i
1bfc7 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 76  s incremented ev
1bfc8 65 72 79 20 74 69 6d 65 20 61 20 63 75 72 73 6f  ery time a curso
1bfc9 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 74 68  r.** moves, eith
1bfca 65 72 20 62 79 20 74 68 65 20 4f 50 5f 53 65 65  er by the OP_See
1bfcb 6b 58 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 6f 72  kXX, OP_Next, or
1bfcc 20 4f 50 5f 50 72 65 76 20 6f 70 63 6f 64 65 73   OP_Prev opcodes
1bfcd 2e 20 20 54 68 65 20 74 65 73 74 0a 2a 2a 20 70  .  The test.** p
1bfce 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74 68  rocedures use th
1bfcf 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74  is information t
1bfd0 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
1bfd1 20 69 6e 64 69 63 65 73 20 61 72 65 0a 2a 2a 20   indices are.** 
1bfd2 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c  working correctl
1bfd3 79 2e 20 20 54 68 69 73 20 76 61 72 69 61 62 6c  y.  This variabl
1bfd4 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f  e has no functio
1bfd5 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a  n other than to.
1bfd6 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 74  ** help verify t
1bfd7 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61  he correct opera
1bfd8 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72  tion of the libr
1bfd9 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ary..*/.#ifdef S
1bfda 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
1bfdb 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bfdc 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 20 3d  3_search_count =
1bfdd 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   0;.#endif../*.*
1bfde 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c 6f 62  * When this glob
1bfdf 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 70  al variable is p
1bfe0 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65 74 73  ositive, it gets
1bfe1 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e 63   decremented onc
1bfe2 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63 68  e before.** each
1bfe3 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20   instruction in 
1bfe4 74 68 65 20 56 44 42 45 2e 20 20 57 68 65 6e 20  the VDBE.  When 
1bfe5 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 74 68  reaches zero, th
1bfe6 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  e u1.isInterrupt
1bfe7 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74  ed.** field of t
1bfe8 68 65 20 73 71 6c 69 74 65 33 20 73 74 72 75 63  he sqlite3 struc
1bfe9 74 75 72 65 20 69 73 20 73 65 74 20 69 6e 20 6f  ture is set in o
1bfea 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61 74 65  rder to simulate
1bfeb 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74 2e 0a   and interrupt..
1bfec 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63 69 6c  **.** This facil
1bfed 69 74 79 20 69 73 20 75 73 65 64 20 66 6f 72 20  ity is used for 
1bfee 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73  testing purposes
1bfef 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73 20   only.  It does 
1bff0 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  not function.** 
1bff1 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 62  in an ordinary b
1bff2 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  uild..*/.#ifdef 
1bff3 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
1bff4 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1bff5 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75  e3_interrupt_cou
1bff6 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
1bff7 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67  /*.** The next g
1bff8 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69  lobal variable i
1bff9 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61  s incremented ea
1bffa 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f 53  ch type the OP_S
1bffb 6f 72 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73  ort opcode.** is
1bffc 20 65 78 65 63 75 74 65 64 2e 20 20 54 68 65 20   executed.  The 
1bffd 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 20  test procedures 
1bffe 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61  use this informa
1bfff 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72  tion to make sur
1c000 65 20 74 68 61 74 0a 2a 2a 20 73 6f 72 74 69 6e  e that.** sortin
1c001 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67 20 6f  g is occurring o
1c002 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e 67 20  r not occurring 
1c003 61 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 74  at appropriate t
1c004 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76 61 72  imes.   This var
1c005 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20  iable.** has no 
1c006 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74  function other t
1c007 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 69  han to help veri
1c008 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  fy the correct o
1c009 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  peration of the.
1c00a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23  ** library..*/.#
1c00b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1c00c 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
1c00d 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f   sqlite3_sort_co
1c00e 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
1c00f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  ./*.** The next 
1c010 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1c011 72 65 63 6f 72 64 73 20 74 68 65 20 73 69 7a 65  records the size
1c012 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20   of the largest 
1c013 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 20 4d  MEM_Blob.** or M
1c014 45 4d 5f 53 74 72 20 74 68 61 74 20 68 61 73 20  EM_Str that has 
1c015 62 65 65 6e 20 75 73 65 64 20 62 79 20 61 20 56  been used by a V
1c016 44 42 45 20 6f 70 63 6f 64 65 2e 20 20 54 68 65  DBE opcode.  The
1c017 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73   test procedures
1c018 0a 2a 2a 20 75 73 65 20 74 68 69 73 20 69 6e 66  .** use this inf
1c019 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65  ormation to make
1c01a 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 7a   sure that the z
1c01b 65 72 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 69 6f  ero-blob functio
1c01c 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 20 77 6f 72  nality.** is wor
1c01d 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20  king correctly. 
1c01e 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20    This variable 
1c01f 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20  has no function 
1c020 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a 2a  other than to.**
1c021 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 65   help verify the
1c022 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69   correct operati
1c023 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  on of the librar
1c024 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  y..*/.#ifdef SQL
1c025 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
1c026 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1c027 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 30  max_blobsize = 0
1c028 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70  ;.static void up
1c029 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65 28  dateMaxBlobsize(
1c02a 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 28  Mem *p){.  if( (
1c02b 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  p->flags & (MEM_
1c02c 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 21 3d  Str|MEM_Blob))!=
1c02d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 74 65  0 && p->n>sqlite
1c02e 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 29  3_max_blobsize )
1c02f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 61  {.    sqlite3_ma
1c030 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70 2d 3e  x_blobsize = p->
1c031 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  n;.  }.}.#endif.
1c032 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  ./*.** The next 
1c033 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1c034 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65  is incremented e
1c035 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f  ach type the OP_
1c036 46 6f 75 6e 64 20 6f 70 63 6f 64 65 0a 2a 2a 20  Found opcode.** 
1c037 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68 69  is executed. Thi
1c038 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73  s is used to tes
1c039 74 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  t whether or not
1c03a 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
1c03b 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 69 6d  .** operation im
1c03c 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20  plemented using 
1c03d 4f 50 5f 46 6b 49 73 5a 65 72 6f 20 69 73 20 77  OP_FkIsZero is w
1c03e 6f 72 6b 69 6e 67 2e 20 54 68 69 73 20 76 61 72  orking. This var
1c03f 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20  iable.** has no 
1c040 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74  function other t
1c041 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 69  han to help veri
1c042 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  fy the correct o
1c043 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  peration of the.
1c044 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23  ** library..*/.#
1c045 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1c046 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
1c047 20 73 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f 63   sqlite3_found_c
1c048 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
1c049 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20 72  ../*.** Test a r
1c04a 65 67 69 73 74 65 72 20 74 6f 20 73 65 65 20 69  egister to see i
1c04b 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68 65  f it exceeds the
1c04c 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d   current maximum
1c04d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20 49   blob size..** I
1c04e 66 20 69 74 20 64 6f 65 73 2c 20 72 65 63 6f 72  f it does, recor
1c04f 64 20 74 68 65 20 6e 65 77 20 6d 61 78 69 6d 75  d the new maximu
1c050 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f 0a  m blob size..*/.
1c051 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
1c052 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65 66  TE_TEST) && !def
1c053 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1c054 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a 23  _BUILTIN_TEST).#
1c055 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d   define UPDATE_M
1c056 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20 20  AX_BLOBSIZE(P)  
1c057 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a  updateMaxBlobsiz
1c058 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  e(P).#else.# def
1c059 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  ine UPDATE_MAX_B
1c05a 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 6e 64 69  LOBSIZE(P).#endi
1c05b 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  f../*.** Convert
1c05c 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69 73   the given regis
1c05d 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 6e  ter into a strin
1c05e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f 6e  g if it isn't on
1c05f 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52 65  e.** already. Re
1c060 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66  turn non-zero if
1c061 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
1c062 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 74  s..*/.#define St
1c063 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63 29 20  ringify(P, enc) 
1c064 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e 66 6c  \.   if(((P)->fl
1c065 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  ags&(MEM_Str|MEM
1c066 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73 71  _Blob))==0 && sq
1c067 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69  lite3VdbeMemStri
1c068 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20 5c 0a  ngify(P,enc)) \.
1c069 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f 6d       { goto no_m
1c06a 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  em; }../*.** An 
1c06b 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67  ephemeral string
1c06c 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66 69 65   value (signifie
1c06d 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45 70 68  d by the MEM_Eph
1c06e 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61 69 6e  em flag) contain
1c06f 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  s.** a pointer t
1c070 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20  o a dynamically 
1c071 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67  allocated string
1c072 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74 68 65   where some othe
1c073 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20 72  r entity.** is r
1c074 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64  esponsible for d
1c075 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61 74  eallocating that
1c076 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61 75 73   string.  Becaus
1c077 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a  e the register.*
1c078 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72  * does not contr
1c079 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20 69  ol the string, i
1c07a 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65 74  t might be delet
1c07b 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 72  ed without the r
1c07c 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77 69  egister.** knowi
1c07d 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ng it..**.** Thi
1c07e 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65 72  s routine conver
1c07f 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20  ts an ephemeral 
1c080 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 64 79  string into a dy
1c081 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
1c082 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74 68  ted.** string th
1c083 61 74 20 74 68 65 20 72 65 67 69 73 74 65 72 20  at the register 
1c084 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73 2e  itself controls.
1c085 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
1c086 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74 73  , it.** converts
1c087 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73 74   an MEM_Ephem st
1c088 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45 4d  ring into an MEM
1c089 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a  _Dyn string..*/.
1c08a 23 64 65 66 69 6e 65 20 44 65 65 70 68 65 6d 65  #define Deepheme
1c08b 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20 69  ralize(P) \.   i
1c08c 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d  f( ((P)->flags&M
1c08d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a 20  EM_Ephem)!=0 \. 
1c08e 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33        && sqlite3
1c08f 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65  VdbeMemMakeWrite
1c090 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f 20  able(P) ){ goto 
1c091 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a 20  no_mem;}../*.** 
1c092 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65  Call sqlite3Vdbe
1c093 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 20  MemExpandBlob() 
1c094 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  on the supplied 
1c095 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d 2a  value (type Mem*
1c096 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69 72  ).** P if requir
1c097 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ed..*/.#define E
1c098 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 28  xpandBlob(P) (((
1c099 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65  P)->flags&MEM_Ze
1c09a 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65 4d  ro)?sqlite3VdbeM
1c09b 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 3a  emExpandBlob(P):
1c09c 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65  0)../*.** Argume
1c09d 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20 61  nt pMem points a
1c09e 74 20 61 20 72 65 67 69 73 74 65 72 20 74 68 61  t a register tha
1c09f 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64  t will be passed
1c0a0 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64 65   to a.** user-de
1c0a1 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 6f  fined function o
1c0a2 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  r returned to th
1c0a3 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72 65  e user as the re
1c0a4 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79 2e  sult of a query.
1c0a5 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1c0a6 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d 3e   sets the pMem->
1c0a7 74 79 70 65 20 76 61 72 69 61 62 6c 65 20 75 73  type variable us
1c0a8 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  ed by the sqlite
1c0a9 33 5f 76 61 6c 75 65 5f 2a 28 29 20 0a 2a 2a 20  3_value_*() .** 
1c0aa 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 53 51 4c  routines..*/.SQL
1c0ab 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1c0ac 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1c0ad 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d  toreType(Mem *pM
1c0ae 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73  em){.  int flags
1c0af 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a   = pMem->flags;.
1c0b0 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45    if( flags & ME
1c0b1 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d  M_Null ){.    pM
1c0b2 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
1c0b3 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c  E_NULL;.  }.  el
1c0b4 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  se if( flags & M
1c0b5 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d  EM_Int ){.    pM
1c0b6 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
1c0b7 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20  E_INTEGER;.  }. 
1c0b8 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20   else if( flags 
1c0b9 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20  & MEM_Real ){.  
1c0ba 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
1c0bb 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d  QLITE_FLOAT;.  }
1c0bc 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67  .  else if( flag
1c0bd 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20  s & MEM_Str ){. 
1c0be 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1c0bf 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d  SQLITE_TEXT;.  }
1c0c0 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  else{.    pMem->
1c0c1 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c  type = SQLITE_BL
1c0c2 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  OB;.  }.}../*.**
1c0c3 20 50 72 6f 70 65 72 74 69 65 73 20 6f 66 20 6f   Properties of o
1c0c4 70 63 6f 64 65 73 2e 20 20 54 68 65 20 4f 50 46  pcodes.  The OPF
1c0c5 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d  LG_INITIALIZER m
1c0c6 61 63 72 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74  acro is.** creat
1c0c7 65 64 20 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e  ed by mkopcodeh.
1c0c8 61 77 6b 20 64 75 72 69 6e 67 20 63 6f 6d 70 69  awk during compi
1c0c9 6c 61 74 69 6f 6e 2e 20 20 44 61 74 61 20 69 73  lation.  Data is
1c0ca 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
1c0cb 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66  m the comments f
1c0cc 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61  ollowing the "ca
1c0cd 73 65 20 4f 50 5f 78 78 78 78 3a 22 20 73 74 61  se OP_xxxx:" sta
1c0ce 74 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68  tements in.** th
1c0cf 69 73 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a 73 74  is file.  .*/.st
1c0d0 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
1c0d1 6e 65 64 20 63 68 61 72 20 6f 70 63 6f 64 65 50  ned char opcodeP
1c0d2 72 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c  roperty[] = OPFL
1c0d3 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a  G_INITIALIZER;..
1c0d4 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
1c0d5 65 20 69 66 20 61 6e 20 6f 70 63 6f 64 65 20 68  e if an opcode h
1c0d6 61 73 20 61 6e 79 20 6f 66 20 74 68 65 20 4f 50  as any of the OP
1c0d7 46 4c 47 5f 78 78 78 20 70 72 6f 70 65 72 74 69  FLG_xxx properti
1c0d8 65 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  es.** specified 
1c0d9 62 79 20 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 4c 49  by mask..*/.SQLI
1c0da 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1c0db 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65  qlite3VdbeOpcode
1c0dc 48 61 73 50 72 6f 70 65 72 74 79 28 69 6e 74 20  HasProperty(int 
1c0dd 6f 70 63 6f 64 65 2c 20 69 6e 74 20 6d 61 73 6b  opcode, int mask
1c0de 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63  ){.  assert( opc
1c0df 6f 64 65 3e 30 20 26 26 20 6f 70 63 6f 64 65 3c  ode>0 && opcode<
1c0e0 28 69 6e 74 29 73 69 7a 65 6f 66 28 6f 70 63 6f  (int)sizeof(opco
1c0e1 64 65 50 72 6f 70 65 72 74 79 29 20 29 3b 0a 20  deProperty) );. 
1c0e2 20 72 65 74 75 72 6e 20 28 6f 70 63 6f 64 65 50   return (opcodeP
1c0e3 72 6f 70 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26  roperty[opcode]&
1c0e4 6d 61 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  mask)!=0;.}../*.
1c0e5 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65  ** Allocate Vdbe
1c0e6 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43  Cursor number iC
1c0e7 75 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  ur.  Return a po
1c0e8 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65  inter to it.  Re
1c0e9 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20  turn NULL.** if 
1c0ea 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65  we run out of me
1c0eb 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mory..*/.static 
1c0ec 56 64 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f  VdbeCursor *allo
1c0ed 63 61 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64  cateCursor(.  Vd
1c0ee 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  be *p,          
1c0ef 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75      /* The virtu
1c0f0 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20  al machine */.  
1c0f1 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20  int iCur,       
1c0f2 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1c0f3 66 20 74 68 65 20 6e 65 77 20 56 64 62 65 43 75  f the new VdbeCu
1c0f4 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  rsor */.  int nF
1c0f5 69 65 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20  ield,           
1c0f6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65  /* Number of fie
1c0f7 6c 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  lds in the table
1c0f8 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69   or index */.  i
1c0f9 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20  nt iDb,         
1c0fa 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 64 61 74       /* When dat
1c0fb 61 62 61 73 65 20 74 68 65 20 63 75 72 73 6f 72  abase the cursor
1c0fc 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72 20   belongs to, or 
1c0fd 2d 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73 42 74  -1 */.  int isBt
1c0fe 72 65 65 43 75 72 73 6f 72 20 20 20 20 20 2f 2a  reeCursor     /*
1c0ff 20 54 72 75 65 20 66 6f 72 20 42 2d 54 72 65 65   True for B-Tree
1c100 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 70 73 65  .  False for pse
1c101 75 64 6f 2d 74 61 62 6c 65 20 6f 72 20 76 74 61  udo-table or vta
1c102 62 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e  b */.){.  /* Fin
1c103 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c  d the memory cel
1c104 6c 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75  l that will be u
1c105 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
1c106 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 0a   blob of memory.
1c107 20 20 2a 2a 20 72 65 71 75 69 72 65 64 20 66 6f    ** required fo
1c108 72 20 74 68 69 73 20 56 64 62 65 43 75 72 73 6f  r this VdbeCurso
1c109 72 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 20  r structure. It 
1c10a 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f  is convenient to
1c10b 20 75 73 65 20 61 20 0a 20 20 2a 2a 20 76 64 62   use a .  ** vdb
1c10c 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f  e memory cell to
1c10d 20 6d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d 6f   manage the memo
1c10e 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65  ry allocation re
1c10f 71 75 69 72 65 64 20 66 6f 72 20 61 0a 20 20 2a  quired for a.  *
1c110 2a 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72  * VdbeCursor str
1c111 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 66  ucture for the f
1c112 6f 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73  ollowing reasons
1c113 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20  :.  **.  **   * 
1c114 53 6f 6d 65 74 69 6d 65 73 20 63 75 72 73 6f 72  Sometimes cursor
1c115 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75 73 65   numbers are use
1c116 64 20 66 6f 72 20 61 20 63 6f 75 70 6c 65 20 6f  d for a couple o
1c117 66 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a  f different.  **
1c118 20 20 20 20 20 70 75 72 70 6f 73 65 73 20 69 6e       purposes in
1c119 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e   a vdbe program.
1c11a 20 54 68 65 20 64 69 66 66 65 72 65 6e 74 20 75   The different u
1c11b 73 65 73 20 6d 69 67 68 74 20 72 65 71 75 69 72  ses might requir
1c11c 65 0a 20 20 2a 2a 20 20 20 20 20 64 69 66 66 65  e.  **     diffe
1c11d 72 65 6e 74 20 73 69 7a 65 64 20 61 6c 6c 6f 63  rent sized alloc
1c11e 61 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 20 63  ations. Memory c
1c11f 65 6c 6c 73 20 70 72 6f 76 69 64 65 20 67 72 6f  ells provide gro
1c120 77 61 62 6c 65 0a 20 20 2a 2a 20 20 20 20 20 61  wable.  **     a
1c121 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a  llocations..  **
1c122 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 75  .  **   * When u
1c123 73 69 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f  sing ENABLE_MEMO
1c124 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20 6d  RY_MANAGEMENT, m
1c125 65 6d 6f 72 79 20 63 65 6c 6c 20 62 75 66 66 65  emory cell buffe
1c126 72 73 20 63 61 6e 0a 20 20 2a 2a 20 20 20 20 20  rs can.  **     
1c127 62 65 20 66 72 65 65 64 20 6c 61 7a 69 6c 79 20  be freed lazily 
1c128 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f  via the sqlite3_
1c129 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29  release_memory()
1c12a 20 41 50 49 2e 20 54 68 69 73 0a 20 20 2a 2a 20   API. This.  ** 
1c12b 20 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74 68      minimizes th
1c12c 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c  e number of mall
1c12d 6f 63 20 63 61 6c 6c 73 20 6d 61 64 65 20 62 79  oc calls made by
1c12e 20 74 68 65 20 73 79 73 74 65 6d 2e 0a 20 20 2a   the system..  *
1c12f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 63 65  *.  ** Memory ce
1c130 6c 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 73 20  lls for cursors 
1c131 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 74  are allocated at
1c132 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
1c133 61 64 64 72 65 73 73 0a 20 20 2a 2a 20 73 70 61  address.  ** spa
1c134 63 65 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20  ce. Memory cell 
1c135 28 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 65 73  (p->nMem) corres
1c136 70 6f 6e 64 73 20 74 6f 20 63 75 72 73 6f 72 20  ponds to cursor 
1c137 30 2e 20 53 70 61 63 65 20 66 6f 72 0a 20 20 2a  0. Space for.  *
1c138 2a 20 63 75 72 73 6f 72 20 31 20 69 73 20 6d 61  * cursor 1 is ma
1c139 6e 61 67 65 64 20 62 79 20 6d 65 6d 6f 72 79 20  naged by memory 
1c13a 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29  cell (p->nMem-1)
1c13b 2c 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65  , etc..  */.  Me
1c13c 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d  m *pMem = &p->aM
1c13d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d  em[p->nMem-iCur]
1c13e 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a  ;..  int nByte;.
1c13f 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c140 78 20 3d 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d  x = 0;.  nByte =
1c141 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 56   .      sizeof(V
1c142 64 62 65 43 75 72 73 6f 72 29 20 2b 20 0a 20 20  dbeCursor) + .  
1c143 20 20 20 20 28 69 73 42 74 72 65 65 43 75 72 73      (isBtreeCurs
1c144 6f 72 3f 73 71 6c 69 74 65 33 42 74 72 65 65 43  or?sqlite3BtreeC
1c145 75 72 73 6f 72 53 69 7a 65 28 29 3a 30 29 20 2b  ursorSize():0) +
1c146 20 0a 20 20 20 20 20 20 32 2a 6e 46 69 65 6c 64   .      2*nField
1c147 2a 73 69 7a 65 6f 66 28 75 33 32 29 3b 0a 0a 20  *sizeof(u32);.. 
1c148 20 61 73 73 65 72 74 28 20 69 43 75 72 3c 70 2d   assert( iCur<p-
1c149 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66  >nCursor );.  if
1c14a 28 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d  ( p->apCsr[iCur]
1c14b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1c14c 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c  dbeFreeCursor(p,
1c14d 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 29   p->apCsr[iCur])
1c14e 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69  ;.    p->apCsr[i
1c14f 43 75 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Cur] = 0;.  }.  
1c150 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73  if( SQLITE_OK==s
1c151 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
1c152 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30  w(pMem, nByte, 0
1c153 29 20 29 7b 0a 20 20 20 20 70 2d 3e 61 70 43 73  ) ){.    p->apCs
1c154 72 5b 69 43 75 72 5d 20 3d 20 70 43 78 20 3d 20  r[iCur] = pCx = 
1c155 28 56 64 62 65 43 75 72 73 6f 72 2a 29 70 4d 65  (VdbeCursor*)pMe
1c156 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 65 74  m->z;.    memset
1c157 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79  (pMem->z, 0, nBy
1c158 74 65 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44  te);.    pCx->iD
1c159 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 78  b = iDb;.    pCx
1c15a 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c  ->nField = nFiel
1c15b 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65 6c  d;.    if( nFiel
1c15c 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e  d ){.      pCx->
1c15d 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29 26  aType = (u32 *)&
1c15e 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56  pMem->z[sizeof(V
1c15f 64 62 65 43 75 72 73 6f 72 29 5d 3b 0a 20 20 20  dbeCursor)];.   
1c160 20 7d 0a 20 20 20 20 69 66 28 20 69 73 42 74 72   }.    if( isBtr
1c161 65 65 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20  eeCursor ){.    
1c162 20 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 3d    pCx->pCursor =
1c163 20 28 42 74 43 75 72 73 6f 72 2a 29 0a 20 20 20   (BtCursor*).   
1c164 20 20 20 20 20 20 20 26 70 4d 65 6d 2d 3e 7a 5b         &pMem->z[
1c165 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f  sizeof(VdbeCurso
1c166 72 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65  r)+2*nField*size
1c167 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 7d 0a  of(u32)];.    }.
1c168 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 78    }.  return pCx
1c169 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74  ;.}../*.** Try t
1c16a 6f 20 63 6f 6e 76 65 72 74 20 61 20 76 61 6c 75  o convert a valu
1c16b 65 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63  e into a numeric
1c16c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1c16d 69 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f 20  if we can.** do 
1c16e 73 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20  so without loss 
1c16f 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20  of information. 
1c170 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
1c171 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a   if the string.*
1c172 2a 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e  * looks like a n
1c173 75 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74 20 69  umber, convert i
1c174 74 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 2e  t into a number.
1c175 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74    If it does not
1c176 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20  .** look like a 
1c177 6e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20 69 74  number, leave it
1c178 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69   alone..*/.stati
1c179 63 20 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d 65  c void applyNume
1c17a 72 69 63 41 66 66 69 6e 69 74 79 28 4d 65 6d 20  ricAffinity(Mem 
1c17b 2a 70 52 65 63 29 7b 0a 20 20 69 66 28 20 28 70  *pRec){.  if( (p
1c17c 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Rec->flags & (ME
1c17d 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29  M_Real|MEM_Int))
1c17e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ==0 ){.    int r
1c17f 65 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69  ealnum;.    sqli
1c180 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72  te3VdbeMemNulTer
1c181 6d 69 6e 61 74 65 28 70 52 65 63 29 3b 0a 20 20  minate(pRec);.  
1c182 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61    if( (pRec->fla
1c183 67 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20 20  gs&MEM_Str).    
1c184 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 49       && sqlite3I
1c185 73 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a 2c  sNumber(pRec->z,
1c186 20 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63 2d   &realnum, pRec-
1c187 3e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 69  >enc) ){.      i
1c188 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  64 value;.      
1c189 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1c18a 65 45 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c 20  eEncoding(pRec, 
1c18b 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20  SQLITE_UTF8);.  
1c18c 20 20 20 20 69 66 28 20 21 72 65 61 6c 6e 75 6d      if( !realnum
1c18d 20 26 26 20 73 71 6c 69 74 65 33 41 74 6f 69 36   && sqlite3Atoi6
1c18e 34 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c 75  4(pRec->z, &valu
1c18f 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52  e) ){.        pR
1c190 65 63 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 65 3b  ec->u.i = value;
1c191 0a 20 20 20 20 20 20 20 20 4d 65 6d 53 65 74 54  .        MemSetT
1c192 79 70 65 46 6c 61 67 28 70 52 65 63 2c 20 4d 45  ypeFlag(pRec, ME
1c193 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 7d 65  M_Int);.      }e
1c194 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
1c195 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69  ite3VdbeMemReali
1c196 66 79 28 70 52 65 63 29 3b 0a 20 20 20 20 20 20  fy(pRec);.      
1c197 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  }.    }.  }.}../
1c198 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67 20  *.** Processing 
1c199 69 73 20 64 65 74 65 72 6d 69 6e 65 20 62 79 20  is determine by 
1c19a 74 68 65 20 61 66 66 69 6e 69 74 79 20 70 61 72  the affinity par
1c19b 61 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51  ameter:.**.** SQ
1c19c 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
1c19d 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  :.** SQLITE_AFF_
1c19e 52 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f  REAL:.** SQLITE_
1c19f 41 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20  AFF_NUMERIC:.** 
1c1a0 20 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72     Try to conver
1c1a1 74 20 70 52 65 63 20 74 6f 20 61 6e 20 69 6e 74  t pRec to an int
1c1a2 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74  eger representat
1c1a3 69 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20 20  ion or a .**    
1c1a4 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 72  floating-point r
1c1a5 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66  epresentation if
1c1a6 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72   an integer repr
1c1a7 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20  esentation.**   
1c1a8 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
1c1a9 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  .  Note that the
1c1aa 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65   integer represe
1c1ab 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20  ntation is.**   
1c1ac 20 61 6c 77 61 79 73 20 70 72 65 66 65 72 72 65   always preferre
1c1ad 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20 61  d, even if the a
1c1ae 66 66 69 6e 69 74 79 20 69 73 20 52 45 41 4c 2c  ffinity is REAL,
1c1af 20 62 65 63 61 75 73 65 0a 2a 2a 20 20 20 20 61   because.**    a
1c1b0 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73  n integer repres
1c1b1 65 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65  entation is more
1c1b2 20 73 70 61 63 65 20 65 66 66 69 63 69 65 6e 74   space efficient
1c1b3 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20   on disk..**.** 
1c1b4 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a  SQLITE_AFF_TEXT:
1c1b5 0a 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74 20 70  .**    Convert p
1c1b6 52 65 63 20 74 6f 20 61 20 74 65 78 74 20 72 65  Rec to a text re
1c1b7 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a  presentation..**
1c1b8 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  .** SQLITE_AFF_N
1c1b9 4f 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70  ONE:.**    No-op
1c1ba 2e 20 20 70 52 65 63 20 69 73 20 75 6e 63 68 61  .  pRec is uncha
1c1bb 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nged..*/.static 
1c1bc 76 6f 69 64 20 61 70 70 6c 79 41 66 66 69 6e 69  void applyAffini
1c1bd 74 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63 2c  ty(.  Mem *pRec,
1c1be 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1c1bf 20 76 61 6c 75 65 20 74 6f 20 61 70 70 6c 79 20   value to apply 
1c1c0 61 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a 20  affinity to */. 
1c1c1 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 2c 20   char affinity, 
1c1c2 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69       /* The affi
1c1c3 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69  nity to be appli
1c1c4 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20 20  ed */.  u8 enc  
1c1c5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
1c1c6 73 65 20 74 68 69 73 20 74 65 78 74 20 65 6e 63  se this text enc
1c1c7 6f 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66  oding */.){.  if
1c1c8 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49  ( affinity==SQLI
1c1c9 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a 20  TE_AFF_TEXT ){. 
1c1ca 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65 6d     /* Only attem
1c1cb 70 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  pt the conversio
1c1cc 6e 20 74 6f 20 54 45 58 54 20 69 66 20 74 68 65  n to TEXT if the
1c1cd 72 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  re is an integer
1c1ce 20 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a 20   or real.    ** 
1c1cf 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 28  representation (
1c1d0 62 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64 6f  blob and NULL do
1c1d1 20 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65 72 74   not get convert
1c1d2 65 64 29 20 62 75 74 20 6e 6f 20 73 74 72 69 6e  ed) but no strin
1c1d3 67 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65  g.    ** represe
1c1d4 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  ntation..    */.
1c1d5 20 20 20 20 69 66 28 20 30 3d 3d 28 70 52 65 63      if( 0==(pRec
1c1d6 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29  ->flags&MEM_Str)
1c1d7 20 26 26 20 28 70 52 65 63 2d 3e 66 6c 61 67 73   && (pRec->flags
1c1d8 26 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49  &(MEM_Real|MEM_I
1c1d9 6e 74 29 29 20 29 7b 0a 20 20 20 20 20 20 73 71  nt)) ){.      sq
1c1da 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69  lite3VdbeMemStri
1c1db 6e 67 69 66 79 28 70 52 65 63 2c 20 65 6e 63 29  ngify(pRec, enc)
1c1dc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 63  ;.    }.    pRec
1c1dd 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
1c1de 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a  _Real|MEM_Int);.
1c1df 20 20 7d 65 6c 73 65 20 69 66 28 20 61 66 66 69    }else if( affi
1c1e0 6e 69 74 79 21 3d 53 51 4c 49 54 45 5f 41 46 46  nity!=SQLITE_AFF
1c1e1 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 73  _NONE ){.    ass
1c1e2 65 72 74 28 20 61 66 66 69 6e 69 74 79 3d 3d 53  ert( affinity==S
1c1e3 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
1c1e4 52 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53  R || affinity==S
1c1e5 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20  QLITE_AFF_REAL. 
1c1e6 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 61              || a
1c1e7 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
1c1e8 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20  AFF_NUMERIC );. 
1c1e9 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41     applyNumericA
1c1ea 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20  ffinity(pRec);. 
1c1eb 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61     if( pRec->fla
1c1ec 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b  gs & MEM_Real ){
1c1ed 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1c1ee 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74  beIntegerAffinit
1c1ef 79 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20  y(pRec);.    }. 
1c1f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20   }.}../*.** Try 
1c1f1 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 74  to convert the t
1c1f2 79 70 65 20 6f 66 20 61 20 66 75 6e 63 74 69 6f  ype of a functio
1c1f3 6e 20 61 72 67 75 6d 65 6e 74 20 6f 72 20 61 20  n argument or a 
1c1f4 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a  result column.**
1c1f5 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20   into a numeric 
1c1f6 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20  representation. 
1c1f7 20 55 73 65 20 65 69 74 68 65 72 20 49 4e 54 45   Use either INTE
1c1f8 47 45 52 20 6f 72 20 52 45 41 4c 20 77 68 69 63  GER or REAL whic
1c1f9 68 65 76 65 72 0a 2a 2a 20 69 73 20 61 70 70 72  hever.** is appr
1c1fa 6f 70 72 69 61 74 65 2e 20 20 42 75 74 20 6f 6e  opriate.  But on
1c1fb 6c 79 20 64 6f 20 74 68 65 20 63 6f 6e 76 65 72  ly do the conver
1c1fc 73 69 6f 6e 20 69 66 20 69 74 20 69 73 20 70 6f  sion if it is po
1c1fd 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74 0a 2a  ssible without.*
1c1fe 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d  * loss of inform
1c1ff 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e  ation and return
1c200 20 74 68 65 20 72 65 76 69 73 65 64 20 74 79 70   the revised typ
1c201 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e  e of the argumen
1c202 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  t..**.** This is
1c203 20 61 6e 20 45 58 50 45 52 49 4d 45 4e 54 41 4c   an EXPERIMENTAL
1c204 20 61 70 69 20 61 6e 64 20 69 73 20 73 75 62 6a   api and is subj
1c205 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72  ect to change or
1c206 20 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c   removal..*/.SQL
1c207 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1c208 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69  te3_value_numeri
1c209 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76  c_type(sqlite3_v
1c20a 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d  alue *pVal){.  M
1c20b 65 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a  em *pMem = (Mem*
1c20c 29 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e 75  )pVal;.  applyNu
1c20d 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 4d  mericAffinity(pM
1c20e 65 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  em);.  sqlite3Vd
1c20f 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 70  beMemStoreType(p
1c210 4d 65 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  Mem);.  return p
1c211 4d 65 6d 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a  Mem->type;.}../*
1c212 0a 2a 2a 20 45 78 70 6f 72 74 65 64 20 76 65 72  .** Exported ver
1c213 73 69 6f 6e 20 6f 66 20 61 70 70 6c 79 41 66 66  sion of applyAff
1c214 69 6e 69 74 79 28 29 2e 20 54 68 69 73 20 6f 6e  inity(). This on
1c215 65 20 77 6f 72 6b 73 20 6f 6e 20 73 71 6c 69 74  e works on sqlit
1c216 65 33 5f 76 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e  e3_value*, .** n
1c217 6f 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ot the internal 
1c218 4d 65 6d 2a 20 74 79 70 65 2e 0a 2a 2f 0a 53 51  Mem* type..*/.SQ
1c219 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1c21a 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70  d sqlite3ValueAp
1c21b 70 6c 79 41 66 66 69 6e 69 74 79 28 0a 20 20 73  plyAffinity(.  s
1c21c 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
1c21d 61 6c 2c 20 0a 20 20 75 38 20 61 66 66 69 6e 69  al, .  u8 affini
1c21e 74 79 2c 20 0a 20 20 75 38 20 65 6e 63 0a 29 7b  ty, .  u8 enc.){
1c21f 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79  .  applyAffinity
1c220 28 28 4d 65 6d 20 2a 29 70 56 61 6c 2c 20 61 66  ((Mem *)pVal, af
1c221 66 69 6e 69 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a  finity, enc);.}.
1c222 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1c223 45 42 55 47 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  EBUG./*.** Write
1c224 20 61 20 6e 69 63 65 20 73 74 72 69 6e 67 20 72   a nice string r
1c225 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
1c226 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
1c227 20 63 65 6c 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e   cell pMem.** in
1c228 74 6f 20 62 75 66 66 65 72 20 7a 42 75 66 2c 20  to buffer zBuf, 
1c229 6c 65 6e 67 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a  length nBuf..*/.
1c22a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1c22b 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
1c22c 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 4d 65  emPrettyPrint(Me
1c22d 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a  m *pMem, char *z
1c22e 42 75 66 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43  Buf){.  char *zC
1c22f 73 72 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74  sr = zBuf;.  int
1c230 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   f = pMem->flags
1c231 3b 0a 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ;..  static cons
1c232 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 65 6e  t char *const en
1c233 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 22 28 58 29  cnames[] = {"(X)
1c234 22 2c 20 22 28 38 29 22 2c 20 22 28 31 36 4c 45  ", "(8)", "(16LE
1c235 29 22 2c 20 22 28 31 36 42 45 29 22 7d 3b 0a 0a  )", "(16BE)"};..
1c236 20 20 69 66 28 20 66 26 4d 45 4d 5f 42 6c 6f 62    if( f&MEM_Blob
1c237 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
1c238 20 20 20 63 68 61 72 20 63 3b 0a 20 20 20 20 69     char c;.    i
1c239 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29  f( f & MEM_Dyn )
1c23a 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 7a 27 3b  {.      c = 'z';
1c23b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
1c23c 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c  f & (MEM_Static|
1c23d 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29  MEM_Ephem))==0 )
1c23e 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1c23f 66 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 20 29  f & MEM_Static )
1c240 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 74 27 3b  {.      c = 't';
1c241 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
1c242 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d  f & (MEM_Dyn|MEM
1c243 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20  _Ephem))==0 );. 
1c244 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26     }else if( f &
1c245 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20   MEM_Ephem ){.  
1c246 20 20 20 20 63 20 3d 20 27 65 27 3b 0a 20 20 20      c = 'e';.   
1c247 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
1c248 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
1c249 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  Dyn))==0 );.    
1c24a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d  }else{.      c =
1c24b 20 27 73 27 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   's';.    }..   
1c24c 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1c24d 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 63  f(100, zCsr, "%c
1c24e 22 2c 20 63 29 3b 0a 20 20 20 20 7a 43 73 72 20  ", c);.    zCsr 
1c24f 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
1c250 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 73 71  30(zCsr);.    sq
1c251 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
1c252 30 30 2c 20 7a 43 73 72 2c 20 22 25 64 5b 22 2c  00, zCsr, "%d[",
1c253 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a   pMem->n);.    z
1c254 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  Csr += sqlite3St
1c255 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20  rlen30(zCsr);.  
1c256 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20    for(i=0; i<16 
1c257 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b  && i<pMem->n; i+
1c258 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
1c259 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20  3_snprintf(100, 
1c25a 7a 43 73 72 2c 20 22 25 30 32 58 22 2c 20 28 28  zCsr, "%02X", ((
1c25b 69 6e 74 29 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26  int)pMem->z[i] &
1c25c 20 30 78 46 46 29 29 3b 0a 20 20 20 20 20 20 7a   0xFF));.      z
1c25d 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  Csr += sqlite3St
1c25e 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20  rlen30(zCsr);.  
1c25f 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
1c260 20 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d   i<16 && i<pMem-
1c261 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  >n; i++){.      
1c262 63 68 61 72 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a  char z = pMem->z
1c263 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  [i];.      if( z
1c264 3c 33 32 20 7c 7c 20 7a 3e 31 32 36 20 29 20 2a  <32 || z>126 ) *
1c265 7a 43 73 72 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20  zCsr++ = '.';.  
1c266 20 20 20 20 65 6c 73 65 20 2a 7a 43 73 72 2b 2b      else *zCsr++
1c267 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = z;.    }..   
1c268 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1c269 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 5d 25  f(100, zCsr, "]%
1c26a 73 22 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65  s", encnames[pMe
1c26b 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 7a 43  m->enc]);.    zC
1c26c 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72  sr += sqlite3Str
1c26d 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20  len30(zCsr);.   
1c26e 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 5a 65 72   if( f & MEM_Zer
1c26f 6f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  o ){.      sqlit
1c270 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c  e3_snprintf(100,
1c271 20 7a 43 73 72 2c 22 2b 25 64 7a 22 2c 70 4d 65   zCsr,"+%dz",pMe
1c272 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20  m->u.nZero);.   
1c273 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74     zCsr += sqlit
1c274 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29  e3Strlen30(zCsr)
1c275 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 7a 43 73  ;.    }.    *zCs
1c276 72 20 3d 20 27 5c 30 27 3b 0a 20 20 7d 65 6c 73  r = '\0';.  }els
1c277 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74  e if( f & MEM_St
1c278 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 2c 20  r ){.    int j, 
1c279 6b 3b 0a 20 20 20 20 7a 42 75 66 5b 30 5d 20 3d  k;.    zBuf[0] =
1c27a 20 27 20 27 3b 0a 20 20 20 20 69 66 28 20 66 20   ' ';.    if( f 
1c27b 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20  & MEM_Dyn ){.   
1c27c 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 7a 27     zBuf[1] = 'z'
1c27d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1c27e 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63  (f & (MEM_Static
1c27f 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20  |MEM_Ephem))==0 
1c280 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
1c281 20 66 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 20   f & MEM_Static 
1c282 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d  ){.      zBuf[1]
1c283 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73   = 't';.      as
1c284 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f  sert( (f & (MEM_
1c285 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d  Dyn|MEM_Ephem))=
1c286 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  =0 );.    }else 
1c287 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65  if( f & MEM_Ephe
1c288 6d 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b  m ){.      zBuf[
1c289 31 5d 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20  1] = 'e';.      
1c28a 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45  assert( (f & (ME
1c28b 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e  M_Static|MEM_Dyn
1c28c 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c  ))==0 );.    }el
1c28d 73 65 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31  se{.      zBuf[1
1c28e 5d 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d 0a 20  ] = 's';.    }. 
1c28f 20 20 20 6b 20 3d 20 32 3b 0a 20 20 20 20 73 71     k = 2;.    sq
1c290 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
1c291 30 30 2c 20 26 7a 42 75 66 5b 6b 5d 2c 20 22 25  00, &zBuf[k], "%
1c292 64 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20  d", pMem->n);.  
1c293 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74    k += sqlite3St
1c294 72 6c 65 6e 33 30 28 26 7a 42 75 66 5b 6b 5d 29  rlen30(&zBuf[k])
1c295 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20  ;.    zBuf[k++] 
1c296 3d 20 27 5b 27 3b 0a 20 20 20 20 66 6f 72 28 6a  = '[';.    for(j
1c297 3d 30 3b 20 6a 3c 31 35 20 26 26 20 6a 3c 70 4d  =0; j<15 && j<pM
1c298 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20  em->n; j++){.   
1c299 20 20 20 75 38 20 63 20 3d 20 70 4d 65 6d 2d 3e     u8 c = pMem->
1c29a 7a 5b 6a 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  z[j];.      if( 
1c29b 63 3e 3d 30 78 32 30 20 26 26 20 63 3c 30 78 37  c>=0x20 && c<0x7
1c29c 66 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75  f ){.        zBu
1c29d 66 5b 6b 2b 2b 5d 20 3d 20 63 3b 0a 20 20 20 20  f[k++] = c;.    
1c29e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1c29f 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27   zBuf[k++] = '.'
1c2a0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1c2a1 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20      zBuf[k++] = 
1c2a2 27 5d 27 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ']';.    sqlite3
1c2a3 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 26 7a  _snprintf(100,&z
1c2a4 42 75 66 5b 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73  Buf[k], encnames
1c2a5 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20  [pMem->enc]);.  
1c2a6 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74    k += sqlite3St
1c2a7 72 6c 65 6e 33 30 28 26 7a 42 75 66 5b 6b 5d 29  rlen30(&zBuf[k])
1c2a8 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20  ;.    zBuf[k++] 
1c2a9 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  = 0;.  }.}.#endi
1c2aa 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
1c2ab 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69  _DEBUG./*.** Pri
1c2ac 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  nt the value of 
1c2ad 61 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74  a register for t
1c2ae 72 61 63 69 6e 67 20 70 75 72 70 6f 73 65 73 3a  racing purposes:
1c2af 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1c2b0 6d 65 6d 54 72 61 63 65 50 72 69 6e 74 28 46 49  memTracePrint(FI
1c2b1 4c 45 20 2a 6f 75 74 2c 20 4d 65 6d 20 2a 70 29  LE *out, Mem *p)
1c2b2 7b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  {.  if( p->flags
1c2b3 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
1c2b4 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
1c2b5 22 20 4e 55 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73  " NULL");.  }els
1c2b6 65 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20  e if( (p->flags 
1c2b7 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53  & (MEM_Int|MEM_S
1c2b8 74 72 29 29 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d  tr))==(MEM_Int|M
1c2b9 45 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 66  EM_Str) ){.    f
1c2ba 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 73 69  printf(out, " si
1c2bb 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b  :%lld", p->u.i);
1c2bc 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
1c2bd 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20  flags & MEM_Int 
1c2be 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f  ){.    fprintf(o
1c2bf 75 74 2c 20 22 20 69 3a 25 6c 6c 64 22 2c 20 70  ut, " i:%lld", p
1c2c0 2d 3e 75 2e 69 29 3b 0a 23 69 66 6e 64 65 66 20  ->u.i);.#ifndef 
1c2c1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
1c2c2 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7d 65 6c  TING_POINT.  }el
1c2c3 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  se if( p->flags 
1c2c4 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20  & MEM_Real ){.  
1c2c5 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
1c2c6 20 72 3a 25 67 22 2c 20 70 2d 3e 72 29 3b 0a 23   r:%g", p->r);.#
1c2c7 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 20 69 66  endif.  }else if
1c2c8 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
1c2c9 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 66  _RowSet ){.    f
1c2ca 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 28 72  printf(out, " (r
1c2cb 6f 77 73 65 74 29 22 29 3b 0a 20 20 7d 65 6c 73  owset)");.  }els
1c2cc 65 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66  e{.    char zBuf
1c2cd 5b 32 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74  [200];.    sqlit
1c2ce 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50  e3VdbeMemPrettyP
1c2cf 72 69 6e 74 28 70 2c 20 7a 42 75 66 29 3b 0a 20  rint(p, zBuf);. 
1c2d0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
1c2d1 22 20 22 29 3b 0a 20 20 20 20 66 70 72 69 6e 74  " ");.    fprint
1c2d2 66 28 6f 75 74 2c 20 22 25 73 22 2c 20 7a 42 75  f(out, "%s", zBu
1c2d3 66 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  f);.  }.}.static
1c2d4 20 76 6f 69 64 20 72 65 67 69 73 74 65 72 54 72   void registerTr
1c2d5 61 63 65 28 46 49 4c 45 20 2a 6f 75 74 2c 20 69  ace(FILE *out, i
1c2d6 6e 74 20 69 52 65 67 2c 20 4d 65 6d 20 2a 70 29  nt iReg, Mem *p)
1c2d7 7b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  {.  fprintf(out,
1c2d8 20 22 52 45 47 5b 25 64 5d 20 3d 20 22 2c 20 69   "REG[%d] = ", i
1c2d9 52 65 67 29 3b 0a 20 20 6d 65 6d 54 72 61 63 65  Reg);.  memTrace
1c2da 50 72 69 6e 74 28 6f 75 74 2c 20 70 29 3b 0a 20  Print(out, p);. 
1c2db 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c   fprintf(out, "\
1c2dc 6e 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  n");.}.#endif..#
1c2dd 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
1c2de 55 47 0a 23 20 20 64 65 66 69 6e 65 20 52 45 47  UG.#  define REG
1c2df 49 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d 29  ISTER_TRACE(R,M)
1c2e0 20 69 66 28 70 2d 3e 74 72 61 63 65 29 72 65 67   if(p->trace)reg
1c2e1 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72  isterTrace(p->tr
1c2e2 61 63 65 2c 52 2c 4d 29 0a 23 65 6c 73 65 0a 23  ace,R,M).#else.#
1c2e3 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45    define REGISTE
1c2e4 52 5f 54 52 41 43 45 28 52 2c 4d 29 0a 23 65 6e  R_TRACE(R,M).#en
1c2e5 64 69 66 0a 0a 0a 23 69 66 64 65 66 20 56 44 42  dif...#ifdef VDB
1c2e6 45 5f 50 52 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a  E_PROFILE../* .*
1c2e7 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61  * hwtime.h conta
1c2e8 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d  ins inline assem
1c2e9 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d  bler code for im
1c2ea 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68  plementing .** h
1c2eb 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20  igh-performance 
1c2ec 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e  timing routines.
1c2ed 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
1c2ee 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69  *** Include hwti
1c2ef 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  me.h in the midd
1c2f0 6c 65 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a  le of vdbe.c ***
1c2f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c2f2 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1c2f3 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
1c2f4 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
1c2f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c2f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c2f7 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d  **/./*.** 2008 M
1c2f8 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 27.**.** The 
1c2f9 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1c2fa 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1c2fb 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1c2fc 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1c2fd 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1c2fe 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1c2ff 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1c300 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1c301 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1c302 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1c303 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1c304 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1c305 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1c306 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1c307 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1c308 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1c309 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1c30a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c30b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c30c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c30d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c30e 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1c30f 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1c310 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f  ns inline asm co
1c311 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e  de for retrievin
1c312 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61  g "high-performa
1c313 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73  nce".** counters
1c314 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43   for x86 class C
1c315 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  PUs..**.** $Id: 
1c316 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32  hwtime.h,v 1.3 2
1c317 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a  008/08/01 14:33:
1c318 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  15 shane Exp $.*
1c319 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d  /.#ifndef _HWTIM
1c31a 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57  E_H_.#define _HW
1c31b 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54  TIME_H_../*.** T
1c31c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
1c31d 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  tine only works 
1c31e 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73  on pentium-class
1c31f 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63   (or newer) proc
1c320 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73  essors..** It us
1c321 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63  es the RDTSC opc
1c322 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20  ode to read the 
1c323 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75  cycle count valu
1c324 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20  e out of the.** 
1c325 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65  processor and re
1c326 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65  turns that value
1c327 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  .  This can be u
1c328 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73  sed for high-res
1c329 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a  .** profiling..*
1c32a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f  /.#if (defined(_
1c32b 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69  _GNUC__) || defi
1c32c 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26  ned(_MSC_VER)) &
1c32d 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e  & \.      (defin
1c32e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69  ed(i386) || defi
1c32f 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c  ned(__i386__) ||
1c330 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36   defined(_M_IX86
1c331 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65  ))..  #if define
1c332 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f  d(__GNUC__)..  _
1c333 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
1c334 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
1c335 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
1c336 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c    unsigned int l
1c337 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73  o, hi;.     __as
1c338 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
1c339 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22   ("rdtsc" : "=a"
1c33a 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29   (lo), "=d" (hi)
1c33b 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28  );.     return (
1c33c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69  sqlite_uint64)hi
1c33d 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d   << 32 | lo;.  }
1c33e 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65  ..  #elif define
1c33f 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f  d(_MSC_VER)..  _
1c340 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29  _declspec(naked)
1c341 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65   __inline sqlite
1c342 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20  _uint64 __cdecl 
1c343 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
1c344 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20  id){.     __asm 
1c345 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a  {.        rdtsc.
1c346 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20          ret     
1c347 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65    ; return value
1c348 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20   at EDX:EAX.    
1c349 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66   }.  }..  #endif
1c34a 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64  ..#elif (defined
1c34b 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65  (__GNUC__) && de
1c34c 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f  fined(__x86_64__
1c34d 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  ))..  __inline__
1c34e 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
1c34f 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
1c350 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  d){.      unsign
1c351 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20  ed long val;.   
1c352 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
1c353 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
1c354 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a   : "=A" (val));.
1c355 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c        return val
1c356 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64  ;.  }. .#elif (d
1c357 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
1c358 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70   && defined(__pp
1c359 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  c__))..  __inlin
1c35a 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
1c35b 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
1c35c 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73  void){.      uns
1c35d 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
1c35e 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e  retval;.      un
1c35f 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b  signed long junk
1c360 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ;.      __asm__ 
1c361 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c  __volatile__ ("\
1c362 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20  n\.          1: 
1c363 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c       mftbu   %1\
1c364 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
1c365 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30       mftb    %L0
1c366 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
1c367 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30        mftbu   %0
1c368 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
1c369 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30        cmpw    %0
1c36a 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20  ,%1\n\.         
1c36b 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20           bne    
1c36c 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20   1b".           
1c36d 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72         : "=r" (r
1c36e 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75  etval), "=r" (ju
1c36f 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75  nk));.      retu
1c370 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a  rn retval;.  }..
1c371 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20  #else..  #error 
1c372 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74  Need implementat
1c373 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77  ion of sqlite3Hw
1c374 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
1c375 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a  platform...  /*.
1c376 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20    ** To compile 
1c377 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e  without implemen
1c378 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69  ting sqlite3Hwti
1c379 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
1c37a 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75  atform,.  ** you
1c37b 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20   can remove the 
1c37c 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64  above #error and
1c37d 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69   use the followi
1c37e 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e  ng.  ** stub fun
1c37f 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c  ction.  You will
1c380 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70   lose timing sup
1c381 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20  port for many.  
1c382 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67  ** of the debugg
1c383 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20  ing and testing 
1c384 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69  utilities, but i
1c385 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a  t should at.  **
1c386 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61   least compile a
1c387 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c  nd run..  */.SQL
1c388 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71  ITE_PRIVATE   sq
1c389 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
1c38a 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
1c38b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65   return ((sqlite
1c38c 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23  _uint64)0); }..#
1c38d 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
1c38e 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d   !defined(_HWTIM
1c38f 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  E_H_) */../*****
1c390 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1c391 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a   hwtime.h ******
1c392 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c393 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c394 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1c395 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
1c396 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
1c397 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 63  ft off in vdbe.c
1c398 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1c399 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69  ********/..#endi
1c39a 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45  f../*.** The CHE
1c39b 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54  CK_FOR_INTERRUPT
1c39c 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 68   macro defined h
1c39d 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 65  ere looks to see
1c39e 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74   if the.** sqlit
1c39f 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72  e3_interrupt() r
1c3a0 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20  outine has been 
1c3a1 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74 20 68  called.  If it h
1c3a2 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a  as been, then.**
1c3a3 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74   processing of t
1c3a4 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20  he VDBE program 
1c3a5 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 2e 0a  is interrupted..
1c3a6 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f  **.** This macro
1c3a7 20 61 64 64 65 64 20 74 6f 20 65 76 65 72 79 20   added to every 
1c3a8 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74  instruction that
1c3a9 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69 6e 20   does a jump in 
1c3aa 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c  order to.** impl
1c3ab 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 54  ement a loop.  T
1c3ac 68 69 73 20 74 65 73 74 20 75 73 65 64 20 74 6f  his test used to
1c3ad 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73 69 6e   be on every sin
1c3ae 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c  gle instruction,
1c3af 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d 65 61  .** but that mea
1c3b0 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73 74 69  nt we more testi
1c3b1 6e 67 20 74 68 61 74 20 77 65 20 6e 65 65 64 65  ng that we neede
1c3b2 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65 73 74  d.  By only test
1c3b3 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61 67 20  ing the.** flag 
1c3b4 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74  on jump instruct
1c3b5 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61 20 28  ions, we get a (
1c3b6 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69 6d 70  small) speed imp
1c3b7 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65  rovement..*/.#de
1c3b8 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49  fine CHECK_FOR_I
1c3b9 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20 69 66  NTERRUPT \.   if
1c3ba 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72  ( db->u1.isInter
1c3bb 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20 61 62  rupted ) goto ab
1c3bc 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72  ort_due_to_inter
1c3bd 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 20 53 51  rupt;..#ifdef SQ
1c3be 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69  LITE_DEBUG.stati
1c3bf 63 20 69 6e 74 20 66 69 6c 65 45 78 69 73 74 73  c int fileExists
1c3c0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
1c3c1 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 29  nst char *zFile)
1c3c2 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b  {.  int res = 0;
1c3c3 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1c3c4 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 53 51  TE_OK;.#ifdef SQ
1c3c5 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49  LITE_TEST.  /* I
1c3c6 66 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74  f we are current
1c3c7 6c 79 20 74 65 73 74 69 6e 67 20 49 4f 20 65 72  ly testing IO er
1c3c8 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f  rors, then do no
1c3c9 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 73 73 28  t call OsAccess(
1c3ca 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74 20 66  ) to.  ** test f
1c3cb 6f 72 20 74 68 65 20 70 72 65 73 65 6e 63 65 20  or the presence 
1c3cc 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 73 20 69  of zFile. This i
1c3cd 73 20 62 65 63 61 75 73 65 20 61 6e 79 20 49 4f  s because any IO
1c3ce 20 65 72 72 6f 72 20 74 68 61 74 0a 20 20 2a 2a   error that.  **
1c3cf 20 6f 63 63 75 72 73 20 68 65 72 65 20 77 69 6c   occurs here wil
1c3d0 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f 72 74 65  l not be reporte
1c3d1 64 2c 20 63 61 75 73 69 6e 67 20 74 68 65 20 74  d, causing the t
1c3d2 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20 20 2a  est to fail..  *
1c3d3 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73  /.  extern int s
1c3d4 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
1c3d5 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28 20 73  pending;.  if( s
1c3d6 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
1c3d7 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 65 6e  pending<=0 ).#en
1c3d8 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  dif.    rc = sql
1c3d9 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d  ite3OsAccess(db-
1c3da 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 53 51  >pVfs, zFile, SQ
1c3db 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
1c3dc 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 72 65 74  TS, &res);.  ret
1c3dd 75 72 6e 20 28 72 65 73 20 26 26 20 72 63 3d 3d  urn (res && rc==
1c3de 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65  SQLITE_OK);.}.#e
1c3df 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44  ndif..#ifndef ND
1c3e0 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EBUG./*.** This 
1c3e1 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  function is only
1c3e2 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
1c3e3 68 69 6e 20 61 6e 20 61 73 73 65 72 74 28 29 20  hin an assert() 
1c3e4 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 74 0a 2a  expression. It.*
1c3e5 2a 20 63 68 65 63 6b 73 20 74 68 61 74 20 74 68  * checks that th
1c3e6 65 20 73 71 6c 69 74 65 33 2e 6e 54 72 61 6e 73  e sqlite3.nTrans
1c3e7 61 63 74 69 6f 6e 20 76 61 72 69 61 62 6c 65 20  action variable 
1c3e8 69 73 20 63 6f 72 72 65 63 74 6c 79 20 73 65 74  is correctly set
1c3e9 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65   to.** the numbe
1c3ea 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 63  r of non-transac
1c3eb 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 73 20  tion savepoints 
1c3ec 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65  currently in the
1c3ed 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74   .** linked list
1c3ee 20 73 74 61 72 74 69 6e 67 20 61 74 20 73 71 6c   starting at sql
1c3ef 69 74 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e  ite3.pSavepoint.
1c3f0 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a  .** .** Usage:.*
1c3f1 2a 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28  *.**     assert(
1c3f2 20 63 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43   checkSavepointC
1c3f3 6f 75 6e 74 28 64 62 29 20 29 3b 0a 2a 2f 0a 73  ount(db) );.*/.s
1c3f4 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 53  tatic int checkS
1c3f5 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71  avepointCount(sq
1c3f6 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e  lite3 *db){.  in
1c3f7 74 20 6e 20 3d 20 30 3b 0a 20 20 53 61 76 65 70  t n = 0;.  Savep
1c3f8 6f 69 6e 74 20 2a 70 3b 0a 20 20 66 6f 72 28 70  oint *p;.  for(p
1c3f9 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b  =db->pSavepoint;
1c3fa 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 20   p; p=p->pNext) 
1c3fb 6e 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  n++;.  assert( n
1c3fc 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e  ==(db->nSavepoin
1c3fd 74 20 2b 20 64 62 2d 3e 69 73 54 72 61 6e 73 61  t + db->isTransa
1c3fe 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 29 20  ctionSavepoint) 
1c3ff 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  );.  return 1;.}
1c400 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45  .#endif../*.** E
1c401 78 65 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f  xecute as much o
1c402 66 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d  f a VDBE program
1c403 20 61 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20   as we can then 
1c404 72 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71  return..**.** sq
1c405 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61  lite3VdbeMakeRea
1c406 64 79 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c  dy() must be cal
1c407 6c 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20  led before this 
1c408 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72  routine in order
1c409 20 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65   to.** close the
1c40a 20 70 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20   program with a 
1c40b 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e  final OP_Halt an
1c40c 64 20 74 6f 20 73 65 74 20 75 70 20 74 68 65 20  d to set up the 
1c40d 63 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64  callbacks.** and
1c40e 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
1c40f 67 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  ge pointer..**.*
1c410 2a 20 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77  * Whenever a row
1c411 20 6f 72 20 72 65 73 75 6c 74 20 64 61 74 61 20   or result data 
1c412 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68  is available, th
1c413 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
1c414 65 69 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65  either.** invoke
1c415 20 74 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c   the result call
1c416 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69  back (if there i
1c417 73 20 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e  s one) or return
1c418 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f   with.** SQLITE_
1c419 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ROW..**.** If an
1c41a 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
1c41b 20 74 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65   to open a locke
1c41c 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e  d database, then
1c41d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
1c41e 20 77 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76   will either inv
1c41f 6f 6b 65 20 74 68 65 20 62 75 73 79 20 63 61 6c  oke the busy cal
1c420 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 20  lback (if there 
1c421 69 73 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69  is one) or it wi
1c422 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c  ll.** return SQL
1c423 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20  ITE_BUSY..**.** 
1c424 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
1c425 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  rs, an error mes
1c426 73 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20  sage is written 
1c427 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
1c428 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  ed.** from sqlit
1c429 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  e3_malloc() and 
1c42a 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61  p->zErrMsg is ma
1c42b 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  de to point to t
1c42c 68 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54  hat memory..** T
1c42d 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  he error code is
1c42e 20 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63   stored in p->rc
1c42f 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
1c430 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
1c431 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66  _ERROR..**.** If
1c432 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76   the callback ev
1c433 65 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  er returns non-z
1c434 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72  ero, then the pr
1c435 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69  ogram exits.** i
1c436 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65  mmediately.  The
1c437 72 65 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72  re will be no er
1c438 72 6f 72 20 6d 65 73 73 61 67 65 20 62 75 74 20  ror message but 
1c439 74 68 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20  the p->rc field 
1c43a 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c  is.** set to SQL
1c43b 49 54 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68  ITE_ABORT and th
1c43c 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
1c43d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1c43e 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d  ROR..**.** A mem
1c43f 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
1c440 72 72 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72  rror causes p->r
1c441 63 20 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53  c to be set to S
1c442 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20  QLITE_NOMEM and 
1c443 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
1c444 74 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  to return SQLITE
1c445 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74  _ERROR..**.** Ot
1c446 68 65 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73  her fatal errors
1c447 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1c448 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  RROR..**.** Afte
1c449 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68  r this routine h
1c44a 61 73 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c  as finished, sql
1c44b 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
1c44c 28 29 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20  () should be.** 
1c44d 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70  used to clean up
1c44e 20 74 68 65 20 6d 65 73 73 20 74 68 61 74 20 77   the mess that w
1c44f 61 73 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a  as left behind..
1c450 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1c451 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
1c452 65 45 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70  eExec(.  Vdbe *p
1c453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c454 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
1c455 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20  */.){.  int pc; 
1c456 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c457 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61     /* The progra
1c458 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f  m counter */.  O
1c459 70 20 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 20  p *pOp;         
1c45a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
1c45b 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a  rent operation *
1c45c 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
1c45d 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f  ITE_OK;        /
1c45e 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
1c45f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  n */.  sqlite3 *
1c460 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20  db = p->db;     
1c461 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1c462 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69  e */.  u8 encodi
1c463 6e 67 20 3d 20 45 4e 43 28 64 62 29 3b 20 20 20  ng = ENC(db);   
1c464 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1c465 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  e encoding */.  
1c466 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b 20 20  Mem *pIn1 = 0;  
1c467 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73             /* 1s
1c468 74 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20  t input operand 
1c469 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32 20 3d  */.  Mem *pIn2 =
1c46a 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1c46b 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f 70 65  /* 2nd input ope
1c46c 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  rand */.  Mem *p
1c46d 49 6e 33 20 3d 20 30 3b 20 20 20 20 20 20 20 20  In3 = 0;        
1c46e 20 20 20 20 20 2f 2a 20 33 72 64 20 69 6e 70 75       /* 3rd inpu
1c46f 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d  t operand */.  M
1c470 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20 20 20  em *pOut = 0;   
1c471 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74            /* Out
1c472 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  put operand */. 
1c473 20 75 38 20 6f 70 50 72 6f 70 65 72 74 79 3b 0a   u8 opProperty;.
1c474 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d    int iCompare =
1c475 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
1c476 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 4f  Result of last O
1c477 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 74  P_Compare operat
1c478 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50  ion */.  int *aP
1c479 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 20  ermute = 0;     
1c47a 20 20 20 20 2f 2a 20 50 65 72 6d 75 74 61 74 69      /* Permutati
1c47b 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f  on of columns fo
1c47c 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a  r OP_Compare */.
1c47d 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
1c47e 49 4c 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b  ILE.  u64 start;
1c47f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c480 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f   /* CPU clock co
1c481 75 6e 74 20 61 74 20 73 74 61 72 74 20 6f 66 20  unt at start of 
1c482 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  opcode */.  int 
1c483 6f 72 69 67 50 63 3b 20 20 20 20 20 20 20 20 20  origPc;         
1c484 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61         /* Progra
1c485 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61  m counter at sta
1c486 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a  rt of opcode */.
1c487 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
1c488 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52  QLITE_OMIT_PROGR
1c489 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69  ESS_CALLBACK.  i
1c48a 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20  nt nProgressOps 
1c48b 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63  = 0;      /* Opc
1c48c 6f 64 65 73 20 65 78 65 63 75 74 65 64 20 73 69  odes executed si
1c48d 6e 63 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  nce progress cal
1c48e 6c 62 61 63 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66  lback. */.#endif
1c48f 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .  /************
1c490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c491 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c492 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c493 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 41 75  ********.  ** Au
1c494 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65  tomatically gene
1c495 72 61 74 65 64 20 63 6f 64 65 0a 20 20 2a 2a 0a  rated code.  **.
1c496 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    ** The followi
1c497 6e 67 20 75 6e 69 6f 6e 20 69 73 20 61 75 74 6f  ng union is auto
1c498 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61  matically genera
1c499 74 65 64 20 62 79 20 74 68 65 0a 20 20 2a 2a 20  ted by the.  ** 
1c49a 76 64 62 65 2d 63 6f 6d 70 72 65 73 73 2e 74 63  vdbe-compress.tc
1c49b 6c 20 73 63 72 69 70 74 2e 20 20 54 68 65 20 70  l script.  The p
1c49c 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 75  urpose of this u
1c49d 6e 69 6f 6e 20 69 73 20 74 6f 0a 20 20 2a 2a 20  nion is to.  ** 
1c49e 72 65 64 75 63 65 20 74 68 65 20 61 6d 6f 75 6e  reduce the amoun
1c49f 74 20 6f 66 20 73 74 61 63 6b 20 73 70 61 63 65  t of stack space
1c4a0 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 69   required by thi
1c4a1 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a  s function..  **
1c4a2 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 69 6e   See comments in
1c4a3 20 74 68 65 20 76 64 62 65 2d 63 6f 6d 70 72 65   the vdbe-compre
1c4a4 73 73 2e 74 63 6c 20 73 63 72 69 70 74 20 66 6f  ss.tcl script fo
1c4a5 72 20 64 65 74 61 69 6c 73 2e 0a 20 20 2a 2f 0a  r details..  */.
1c4a6 20 20 75 6e 69 6f 6e 20 76 64 62 65 45 78 65 63    union vdbeExec
1c4a7 55 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75  Union {.    stru
1c4a8 63 74 20 4f 50 5f 59 69 65 6c 64 5f 73 74 61 63  ct OP_Yield_stac
1c4a9 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c4aa 6e 74 20 70 63 44 65 73 74 3b 0a 20 20 20 20 7d  nt pcDest;.    }
1c4ab 20 61 61 3b 0a 20 20 20 20 73 74 72 75 63 74 20   aa;.    struct 
1c4ac 4f 50 5f 56 61 72 69 61 62 6c 65 5f 73 74 61 63  OP_Variable_stac
1c4ad 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c4ae 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20  nt p1;          
1c4af 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6f 20 63  /* Variable to c
1c4b0 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20  opy from */.    
1c4b1 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20    int p2;       
1c4b2 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74     /* Register t
1c4b3 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 20  o copy to */.   
1c4b4 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20     int n;       
1c4b5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1c4b6 20 76 61 6c 75 65 73 20 6c 65 66 74 20 74 6f 20   values left to 
1c4b7 63 6f 70 79 20 2a 2f 0a 20 20 20 20 20 20 4d 65  copy */.      Me
1c4b8 6d 20 2a 70 56 61 72 3b 20 20 20 20 20 20 20 2f  m *pVar;       /
1c4b9 2a 20 56 61 6c 75 65 20 62 65 69 6e 67 20 74 72  * Value being tr
1c4ba 61 6e 73 66 65 72 72 65 64 20 2a 2f 0a 20 20 20  ansferred */.   
1c4bb 20 7d 20 61 62 3b 0a 20 20 20 20 73 74 72 75 63   } ab;.    struc
1c4bc 74 20 4f 50 5f 4d 6f 76 65 5f 73 74 61 63 6b 5f  t OP_Move_stack_
1c4bd 76 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61  vars {.      cha
1c4be 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a  r *zMalloc;   /*
1c4bf 20 48 6f 6c 64 69 6e 67 20 76 61 72 69 61 62 6c   Holding variabl
1c4c0 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 65 64 20  e for allocated 
1c4c1 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 20 20  memory */.      
1c4c2 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20  int n;          
1c4c3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
1c4c4 67 69 73 74 65 72 73 20 6c 65 66 74 20 74 6f 20  gisters left to 
1c4c5 63 6f 70 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e  copy */.      in
1c4c6 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f  t p1;          /
1c4c7 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f  * Register to co
1c4c8 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20  py from */.     
1c4c9 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20   int p2;        
1c4ca 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f    /* Register to
1c4cb 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 20 20   copy to */.    
1c4cc 7d 20 61 63 3b 0a 20 20 20 20 73 74 72 75 63 74  } ac;.    struct
1c4cd 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 5f 73 74   OP_ResultRow_st
1c4ce 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c4cf 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20   Mem *pMem;.    
1c4d0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 7d 20 61    int i;.    } a
1c4d1 64 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  d;.    struct OP
1c4d2 5f 43 6f 6e 63 61 74 5f 73 74 61 63 6b 5f 76 61  _Concat_stack_va
1c4d3 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 6e  rs {.      i64 n
1c4d4 42 79 74 65 3b 0a 20 20 20 20 7d 20 61 65 3b 0a  Byte;.    } ae;.
1c4d5 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65      struct OP_Re
1c4d6 6d 61 69 6e 64 65 72 5f 73 74 61 63 6b 5f 76 61  mainder_stack_va
1c4d7 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 66  rs {.      int f
1c4d8 6c 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f  lags;      /* Co
1c4d9 6d 62 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61  mbined MEM_* fla
1c4da 67 73 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70  gs from both inp
1c4db 75 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 36 34  uts */.      i64
1c4dc 20 69 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   iA;         /* 
1c4dd 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66  Integer value of
1c4de 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f   left operand */
1c4df 0a 20 20 20 20 20 20 69 36 34 20 69 42 3b 20 20  .      i64 iB;  
1c4e0 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65         /* Intege
1c4e1 72 20 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74  r value of right
1c4e2 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20   operand */.    
1c4e3 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 20 20 20    double rA;    
1c4e4 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20    /* Real value 
1c4e5 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  of left operand 
1c4e6 2a 2f 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  */.      double 
1c4e7 72 42 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c  rB;      /* Real
1c4e8 20 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20   value of right 
1c4e9 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 7d  operand */.    }
1c4ea 20 61 66 3b 0a 20 20 20 20 73 74 72 75 63 74 20   af;.    struct 
1c4eb 4f 50 5f 46 75 6e 63 74 69 6f 6e 5f 73 74 61 63  OP_Function_stac
1c4ec 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c4ed 6e 74 20 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20  nt i;.      Mem 
1c4ee 2a 70 41 72 67 3b 0a 20 20 20 20 20 20 73 71 6c  *pArg;.      sql
1c4ef 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78  ite3_context ctx
1c4f0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
1c4f1 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20  value **apVal;. 
1c4f2 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20       int n;.    
1c4f3 7d 20 61 67 3b 0a 20 20 20 20 73 74 72 75 63 74  } ag;.    struct
1c4f4 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74 5f 73   OP_ShiftRight_s
1c4f5 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c4f6 20 20 69 36 34 20 61 3b 0a 20 20 20 20 20 20 69    i64 a;.      i
1c4f7 36 34 20 62 3b 0a 20 20 20 20 7d 20 61 68 3b 0a  64 b;.    } ah;.
1c4f8 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 47 65      struct OP_Ge
1c4f9 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c4fa 20 20 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20      int res;    
1c4fb 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
1c4fc 74 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69  t of the compari
1c4fd 73 6f 6e 20 6f 66 20 70 49 6e 31 20 61 67 61 69  son of pIn1 agai
1c4fe 6e 73 74 20 70 49 6e 33 20 2a 2f 0a 20 20 20 20  nst pIn3 */.    
1c4ff 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b    char affinity;
1c500 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74        /* Affinit
1c501 79 20 74 6f 20 75 73 65 20 66 6f 72 20 63 6f 6d  y to use for com
1c502 70 61 72 69 73 6f 6e 20 2a 2f 0a 20 20 20 20 7d  parison */.    }
1c503 20 61 69 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ai;.    struct 
1c504 4f 50 5f 43 6f 6d 70 61 72 65 5f 73 74 61 63 6b  OP_Compare_stack
1c505 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
1c506 74 20 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  t n;.      int i
1c507 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 0a  ;.      int p1;.
1c508 20 20 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20        int p2;.  
1c509 20 20 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66      const KeyInf
1c50a 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  o *pKeyInfo;.   
1c50b 20 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 20 20     int idx;.    
1c50c 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
1c50d 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e  ;    /* Collatin
1c50e 67 20 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73  g sequence to us
1c50f 65 20 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a  e on this term *
1c510 2f 0a 20 20 20 20 20 20 69 6e 74 20 62 52 65 76  /.      int bRev
1c511 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ;          /* Tr
1c512 75 65 20 66 6f 72 20 44 45 53 43 45 4e 44 49 4e  ue for DESCENDIN
1c513 47 20 73 6f 72 74 20 6f 72 64 65 72 20 2a 2f 0a  G sort order */.
1c514 20 20 20 20 7d 20 61 6a 3b 0a 20 20 20 20 73 74      } aj;.    st
1c515 72 75 63 74 20 4f 50 5f 4f 72 5f 73 74 61 63 6b  ruct OP_Or_stack
1c516 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
1c517 74 20 76 31 3b 20 20 20 20 2f 2a 20 4c 65 66 74  t v1;    /* Left
1c518 20 6f 70 65 72 61 6e 64 3a 20 20 30 3d 3d 46 41   operand:  0==FA
1c519 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d  LSE, 1==TRUE, 2=
1c51a 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c  =UNKNOWN or NULL
1c51b 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 76 32   */.      int v2
1c51c 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70  ;    /* Right op
1c51d 65 72 61 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c  erand: 0==FALSE,
1c51e 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b   1==TRUE, 2==UNK
1c51f 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  NOWN or NULL */.
1c520 20 20 20 20 7d 20 61 6b 3b 0a 20 20 20 20 73 74      } ak;.    st
1c521 72 75 63 74 20 4f 50 5f 49 66 4e 6f 74 5f 73 74  ruct OP_IfNot_st
1c522 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c523 20 69 6e 74 20 63 3b 0a 20 20 20 20 7d 20 61 6c   int c;.    } al
1c524 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c525 43 6f 6c 75 6d 6e 5f 73 74 61 63 6b 5f 76 61 72  Column_stack_var
1c526 73 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 61  s {.      u32 pa
1c527 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20  yloadSize;   /* 
1c528 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1c529 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  in the record */
1c52a 0a 20 20 20 20 20 20 69 36 34 20 70 61 79 6c 6f  .      i64 paylo
1c52b 61 64 53 69 7a 65 36 34 3b 20 2f 2a 20 4e 75 6d  adSize64; /* Num
1c52c 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1c52d 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1c52e 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20      int p1;     
1c52f 20 20 20 20 20 20 20 2f 2a 20 50 31 20 76 61 6c         /* P1 val
1c530 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65  ue of the opcode
1c531 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32   */.      int p2
1c532 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1c533 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f  column number to
1c534 20 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 20   retrieve */.   
1c535 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
1c536 43 3b 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42  C;    /* The VDB
1c537 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20  E cursor */.    
1c538 20 20 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 20    char *zRec;   
1c539 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
1c53a 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f  to complete reco
1c53b 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20 20 20 20  rd-data */.     
1c53c 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1c53d 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65  ;   /* The BTree
1c53e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20   cursor */.     
1c53f 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20   u32 *aType;    
1c540 20 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20      /* aType[i] 
1c541 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69  holds the numeri
1c542 63 20 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d  c type of the i-
1c543 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20  th column */.   
1c544 20 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b     u32 *aOffset;
1c545 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 73 65 74        /* aOffset
1c546 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 20 74 6f  [i] is offset to
1c547 20 73 74 61 72 74 20 6f 66 20 64 61 74 61 20 66   start of data f
1c548 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a  or i-th column *
1c549 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65  /.      int nFie
1c54a 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 75  ld;        /* nu
1c54b 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69  mber of fields i
1c54c 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  n the record */.
1c54d 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20        int len;  
1c54e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1c54f 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65  length of the se
1c550 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f  rialized data fo
1c551 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  r the column */.
1c552 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20        int i;    
1c553 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1c554 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
1c555 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20    char *zData;  
1c556 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f 66 20       /* Part of 
1c557 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67  the record being
1c558 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20   decoded */.    
1c559 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20 20    Mem *pDest;   
1c55a 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f       /* Where to
1c55b 20 77 72 69 74 65 20 74 68 65 20 65 78 74 72 61   write the extra
1c55c 63 74 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20 20  cted value */.  
1c55d 20 20 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20      Mem sMem;   
1c55e 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74         /* For st
1c55f 6f 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  oring the record
1c560 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a   being decoded *
1c561 2f 0a 20 20 20 20 20 20 75 38 20 2a 7a 49 64 78  /.      u8 *zIdx
1c562 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ;          /* In
1c563 64 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72 20  dex into header 
1c564 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a 7a 45 6e  */.      u8 *zEn
1c565 64 48 64 72 3b 20 20 20 20 20 20 20 2f 2a 20 50  dHdr;       /* P
1c566 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20  ointer to first 
1c567 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20 68  byte after the h
1c568 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 75  eader */.      u
1c569 33 32 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20  32 offset;      
1c56a 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
1c56b 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 20   the data */.   
1c56c 20 20 20 75 36 34 20 6f 66 66 73 65 74 36 34 3b     u64 offset64;
1c56d 20 20 20 20 20 20 2f 2a 20 36 34 2d 62 69 74 20        /* 64-bit 
1c56e 6f 66 66 73 65 74 2e 20 20 36 34 20 62 69 74 73  offset.  64 bits
1c56f 20 6e 65 65 64 65 64 20 74 6f 20 63 61 74 63 68   needed to catch
1c570 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 20   overflow */.   
1c571 20 20 20 69 6e 74 20 73 7a 48 64 72 3b 20 20 20     int szHdr;   
1c572 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
1c573 20 74 68 65 20 68 65 61 64 65 72 20 73 69 7a 65   the header size
1c574 20 66 69 65 6c 64 20 61 74 20 73 74 61 72 74 20   field at start 
1c575 6f 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20  of record */.   
1c576 20 20 20 69 6e 74 20 61 76 61 69 6c 3b 20 20 20     int avail;   
1c577 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1c578 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76 61 69  of bytes of avai
1c579 6c 61 62 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20  lable data */.  
1c57a 20 20 20 20 4d 65 6d 20 2a 70 52 65 67 3b 20 20      Mem *pReg;  
1c57b 20 20 20 20 20 20 20 2f 2a 20 50 73 65 75 64 6f         /* Pseudo
1c57c 54 61 62 6c 65 20 69 6e 70 75 74 20 72 65 67 69  Table input regi
1c57d 73 74 65 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6d  ster */.    } am
1c57e 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c57f 41 66 66 69 6e 69 74 79 5f 73 74 61 63 6b 5f 76  Affinity_stack_v
1c580 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72  ars {.      char
1c581 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 2f   *zAffinity;   /
1c582 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 74  * The affinity t
1c583 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a  o be applied */.
1c584 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61        Mem *pData
1c585 30 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  0;       /* Firs
1c586 74 20 72 65 67 69 73 74 65 72 20 74 6f 20 77 68  t register to wh
1c587 69 63 68 20 74 6f 20 61 70 70 6c 79 20 61 66 66  ich to apply aff
1c588 69 6e 69 74 79 20 2a 2f 0a 20 20 20 20 20 20 4d  inity */.      M
1c589 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20  em *pLast;      
1c58a 20 20 2f 2a 20 4c 61 73 74 20 72 65 67 69 73 74    /* Last regist
1c58b 65 72 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61  er to which to a
1c58c 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f  pply affinity */
1c58d 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63  .      Mem *pRec
1c58e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  ;         /* Cur
1c58f 72 65 6e 74 20 72 65 67 69 73 74 65 72 20 2a 2f  rent register */
1c590 0a 20 20 20 20 7d 20 61 6e 3b 0a 20 20 20 20 73  .    } an;.    s
1c591 74 72 75 63 74 20 4f 50 5f 4d 61 6b 65 52 65 63  truct OP_MakeRec
1c592 6f 72 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ord_stack_vars {
1c593 0a 20 20 20 20 20 20 75 38 20 2a 7a 4e 65 77 52  .      u8 *zNewR
1c594 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a  ecord;        /*
1c595 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c   A buffer to hol
1c596 64 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  d the data for t
1c597 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f  he new record */
1c598 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63  .      Mem *pRec
1c599 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1c59a 20 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   The new record 
1c59b 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 6e 44 61  */.      u64 nDa
1c59c 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
1c59d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1c59e 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65  es of data space
1c59f 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 48   */.      int nH
1c5a0 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
1c5a1 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1c5a2 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70  tes of header sp
1c5a3 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69 36 34  ace */.      i64
1c5a4 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
1c5a5 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63      /* Data spac
1c5a6 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74  e required for t
1c5a7 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  his record */.  
1c5a8 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20      int nZero;  
1c5a9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1c5aa 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74  mber of zero byt
1c5ab 65 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  es at the end of
1c5ac 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1c5ad 20 20 20 20 20 69 6e 74 20 6e 56 61 72 69 6e 74       int nVarint
1c5ae 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1c5af 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
1c5b0 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20  n a varint */.  
1c5b1 20 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74      u32 serial_t
1c5b2 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79  ype;       /* Ty
1c5b3 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20  pe field */.    
1c5b4 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20    Mem *pData0;  
1c5b5 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
1c5b6 74 20 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f  t field to be co
1c5b7 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20  mbined into the 
1c5b8 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20  record */.      
1c5b9 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20  Mem *pLast;     
1c5ba 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66         /* Last f
1c5bb 69 65 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f  ield of the reco
1c5bc 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  rd */.      int 
1c5bd 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20  nField;         
1c5be 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1c5bf 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65  fields in the re
1c5c0 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 63 68  cord */.      ch
1c5c1 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20  ar *zAffinity;  
1c5c2 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69       /* The affi
1c5c3 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20  nity string for 
1c5c4 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1c5c5 20 20 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72      int file_for
1c5c6 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69  mat;       /* Fi
1c5c7 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65  le format to use
1c5c8 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f   for encoding */
1c5c9 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20  .      int i;   
1c5ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c5cb 20 53 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a   Space used in z
1c5cc 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20  NewRecord[] */. 
1c5cd 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20       int len;   
1c5ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1c5cf 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65 6c 64  ength of a field
1c5d0 20 2a 2f 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20   */.    } ao;.  
1c5d1 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 75 6e    struct OP_Coun
1c5d2 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c5d3 20 20 20 20 20 69 36 34 20 6e 45 6e 74 72 79 3b       i64 nEntry;
1c5d4 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20  .      BtCursor 
1c5d5 2a 70 43 72 73 72 3b 0a 20 20 20 20 7d 20 61 70  *pCrsr;.    } ap
1c5d6 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c5d7 53 61 76 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f  Savepoint_stack_
1c5d8 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c5d9 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   p1;            
1c5da 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c5db 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72  Value of P1 oper
1c5dc 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61  and */.      cha
1c5dd 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
1c5de 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c5df 4e 61 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e  Name of savepoin
1c5e0 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  t */.      int n
1c5e1 4e 61 6d 65 3b 0a 20 20 20 20 20 20 53 61 76 65  Name;.      Save
1c5e2 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 20  point *pNew;.   
1c5e3 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53     Savepoint *pS
1c5e4 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20  avepoint;.      
1c5e5 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b  Savepoint *pTmp;
1c5e6 0a 20 20 20 20 20 20 69 6e 74 20 69 53 61 76 65  .      int iSave
1c5e7 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 69 6e 74  point;.      int
1c5e8 20 69 69 3b 0a 20 20 20 20 7d 20 61 71 3b 0a 20   ii;.    } aq;. 
1c5e9 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 75 74     struct OP_Aut
1c5ea 6f 43 6f 6d 6d 69 74 5f 73 74 61 63 6b 5f 76 61  oCommit_stack_va
1c5eb 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64  rs {.      int d
1c5ec 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
1c5ed 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6c  ;.      int iRol
1c5ee 6c 62 61 63 6b 3b 0a 20 20 20 20 20 20 69 6e 74  lback;.      int
1c5ef 20 74 75 72 6e 4f 6e 41 43 3b 0a 20 20 20 20 7d   turnOnAC;.    }
1c5f0 20 61 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ar;.    struct 
1c5f1 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73  OP_Transaction_s
1c5f2 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c5f3 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20    Btree *pBt;.  
1c5f4 20 20 7d 20 61 73 3b 0a 20 20 20 20 73 74 72 75    } as;.    stru
1c5f5 63 74 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  ct OP_ReadCookie
1c5f6 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c5f7 20 20 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20      int iMeta;. 
1c5f8 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20       int iDb;.  
1c5f9 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b      int iCookie;
1c5fa 0a 20 20 20 20 7d 20 61 74 3b 0a 20 20 20 20 73  .    } at;.    s
1c5fb 74 72 75 63 74 20 4f 50 5f 53 65 74 43 6f 6f 6b  truct OP_SetCook
1c5fc 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ie_stack_vars {.
1c5fd 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20        Db *pDb;. 
1c5fe 20 20 20 7d 20 61 75 3b 0a 20 20 20 20 73 74 72     } au;.    str
1c5ff 75 63 74 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f  uct OP_VerifyCoo
1c600 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  kie_stack_vars {
1c601 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61  .      int iMeta
1c602 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  ;.      Btree *p
1c603 42 74 3b 0a 20 20 20 20 7d 20 61 76 3b 0a 20 20  Bt;.    } av;.  
1c604 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e    struct OP_Open
1c605 57 72 69 74 65 5f 73 74 61 63 6b 5f 76 61 72 73  Write_stack_vars
1c606 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69   {.      int nFi
1c607 65 6c 64 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e  eld;.      KeyIn
1c608 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  fo *pKeyInfo;.  
1c609 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20 20 20      int p2;.    
1c60a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20    int iDb;.     
1c60b 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 20   int wrFlag;.   
1c60c 20 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20     Btree *pX;.  
1c60d 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c60e 70 43 75 72 3b 0a 20 20 20 20 20 20 44 62 20 2a  pCur;.      Db *
1c60f 70 44 62 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20  pDb;.    } aw;. 
1c610 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65     struct OP_Ope
1c611 6e 45 70 68 65 6d 65 72 61 6c 5f 73 74 61 63 6b  nEphemeral_stack
1c612 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c613 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20  beCursor *pCx;. 
1c614 20 20 20 7d 20 61 78 3b 0a 20 20 20 20 73 74 72     } ax;.    str
1c615 75 63 74 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64  uct OP_OpenPseud
1c616 6f 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  o_stack_vars {. 
1c617 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c618 2a 70 43 78 3b 0a 20 20 20 20 7d 20 61 79 3b 0a  *pCx;.    } ay;.
1c619 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65      struct OP_Se
1c61a 65 6b 47 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  ekGt_stack_vars 
1c61b 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b  {.      int res;
1c61c 0a 20 20 20 20 20 20 69 6e 74 20 6f 63 3b 0a 20  .      int oc;. 
1c61d 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c61e 2a 70 43 3b 0a 20 20 20 20 20 20 55 6e 70 61 63  *pC;.      Unpac
1c61f 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20  kedRecord r;.   
1c620 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20     int nField;. 
1c621 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20       i64 iKey;  
1c622 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64      /* The rowid
1c623 20 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20   we are to seek 
1c624 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 61 7a 3b 0a  to */.    } az;.
1c625 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65      struct OP_Se
1c626 65 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ek_stack_vars {.
1c627 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c628 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 61 3b 0a   *pC;.    } ba;.
1c629 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 46 6f      struct OP_Fo
1c62a 75 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  und_stack_vars {
1c62b 0a 20 20 20 20 20 20 69 6e 74 20 61 6c 72 65 61  .      int alrea
1c62c 64 79 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20  dyExists;.      
1c62d 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c62e 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
1c62f 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63       UnpackedRec
1c630 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20  ord *pIdxKey;.  
1c631 20 20 20 20 63 68 61 72 20 61 54 65 6d 70 52 65      char aTempRe
1c632 63 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28  c[ROUND8(sizeof(
1c633 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29  UnpackedRecord))
1c634 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 33   + sizeof(Mem)*3
1c635 20 2b 20 37 5d 3b 0a 20 20 20 20 7d 20 62 62 3b   + 7];.    } bb;
1c636 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
1c637 73 55 6e 69 71 75 65 5f 73 74 61 63 6b 5f 76 61  sUnique_stack_va
1c638 72 73 20 7b 0a 20 20 20 20 20 20 75 31 36 20 69  rs {.      u16 i
1c639 69 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72  i;.      VdbeCur
1c63a 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20 20 20  sor *pCx;.      
1c63b 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1c63c 0a 20 20 20 20 20 20 75 31 36 20 6e 46 69 65 6c  .      u16 nFiel
1c63d 64 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 61 4d  d;.      Mem *aM
1c63e 65 6d 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b  em;.      Unpack
1c63f 65 64 52 65 63 6f 72 64 20 72 3b 20 20 20 20 20  edRecord r;     
1c640 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c641 42 2d 54 72 65 65 20 69 6e 64 65 78 20 73 65 61  B-Tree index sea
1c642 72 63 68 20 6b 65 79 20 2a 2f 0a 20 20 20 20 20  rch key */.     
1c643 20 69 36 34 20 52 3b 20 20 20 20 20 20 20 20 20   i64 R;         
1c644 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c645 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f      /* Rowid sto
1c646 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
1c647 50 33 20 2a 2f 0a 20 20 20 20 7d 20 62 63 3b 0a  P3 */.    } bc;.
1c648 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 6f      struct OP_No
1c649 74 45 78 69 73 74 73 5f 73 74 61 63 6b 5f 76 61  tExists_stack_va
1c64a 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43  rs {.      VdbeC
1c64b 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20  ursor *pC;.     
1c64c 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1c64d 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b  ;.      int res;
1c64e 0a 20 20 20 20 20 20 75 36 34 20 69 4b 65 79 3b  .      u64 iKey;
1c64f 0a 20 20 20 20 7d 20 62 64 3b 0a 20 20 20 20 73  .    } bd;.    s
1c650 74 72 75 63 74 20 4f 50 5f 4e 65 77 52 6f 77 69  truct OP_NewRowi
1c651 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  d_stack_vars {. 
1c652 20 20 20 20 20 69 36 34 20 76 3b 20 20 20 20 20       i64 v;     
1c653 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1c654 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a  he new rowid */.
1c655 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c656 20 2a 70 43 3b 20 20 20 20 20 20 20 20 2f 2a 20   *pC;        /* 
1c657 43 75 72 73 6f 72 20 6f 66 20 74 61 62 6c 65 20  Cursor of table 
1c658 74 6f 20 67 65 74 20 74 68 65 20 6e 65 77 20 72  to get the new r
1c659 6f 77 69 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e  owid */.      in
1c65a 74 20 72 65 73 3b 20 20 20 20 20 20 20 20 20 20  t res;          
1c65b 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f       /* Result o
1c65c 66 20 61 6e 20 73 71 6c 69 74 65 33 42 74 72 65  f an sqlite3Btre
1c65d 65 4c 61 73 74 28 29 20 2a 2f 0a 20 20 20 20 20  eLast() */.     
1c65e 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20   int cnt;       
1c65f 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74          /* Count
1c660 65 72 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20  er to limit the 
1c661 6e 75 6d 62 65 72 20 6f 66 20 73 65 61 72 63 68  number of search
1c662 65 73 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20  es */.      Mem 
1c663 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *pMem;          
1c664 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
1c665 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72  olding largest r
1c666 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43  owid for AUTOINC
1c667 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 20 20 20 20  REMENT */.      
1c668 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d  VdbeFrame *pFram
1c669 65 3b 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 66  e;     /* Root f
1c66a 72 61 6d 65 20 6f 66 20 56 44 42 45 20 2a 2f 0a  rame of VDBE */.
1c66b 20 20 20 20 7d 20 62 65 3b 0a 20 20 20 20 73 74      } be;.    st
1c66c 72 75 63 74 20 4f 50 5f 49 6e 73 65 72 74 49 6e  ruct OP_InsertIn
1c66d 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c66e 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b       Mem *pData;
1c66f 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65         /* MEM ce
1c670 6c 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20  ll holding data 
1c671 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74  for the record t
1c672 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f  o be inserted */
1c673 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4b 65 79  .      Mem *pKey
1c674 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20  ;        /* MEM 
1c675 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 6b 65 79  cell holding key
1c676 20 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64    for the record
1c677 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 69 4b   */.      i64 iK
1c678 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ey;         /* T
1c679 68 65 20 69 6e 74 65 67 65 72 20 52 4f 57 49 44  he integer ROWID
1c67a 20 6f 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20   or key for the 
1c67b 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73  record to be ins
1c67c 65 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 56  erted */.      V
1c67d 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20  dbeCursor *pC;  
1c67e 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 74 61   /* Cursor to ta
1c67f 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 69  ble into which i
1c680 6e 73 65 72 74 20 69 73 20 77 72 69 74 74 65 6e  nsert is written
1c681 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 5a   */.      int nZ
1c682 65 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  ero;        /* N
1c683 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79  umber of zero-by
1c684 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f  tes to append */
1c685 0a 20 20 20 20 20 20 69 6e 74 20 73 65 65 6b 52  .      int seekR
1c686 65 73 75 6c 74 3b 20 20 20 2f 2a 20 52 65 73 75  esult;   /* Resu
1c687 6c 74 20 6f 66 20 70 72 69 6f 72 20 73 65 65 6b  lt of prior seek
1c688 20 6f 72 20 30 20 69 66 20 6e 6f 20 55 53 45 53   or 0 if no USES
1c689 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 67 20 2a  EEKRESULT flag *
1c68a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  /.      const ch
1c68b 61 72 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74  ar *zDb;  /* dat
1c68c 61 62 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65  abase name - use
1c68d 64 20 62 79 20 74 68 65 20 75 70 64 61 74 65 20  d by the update 
1c68e 68 6f 6f 6b 20 2a 2f 0a 20 20 20 20 20 20 63 6f  hook */.      co
1c68f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 3b 20  nst char *zTbl; 
1c690 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2d 20  /* Table name - 
1c691 75 73 65 64 20 62 79 20 74 68 65 20 6f 70 64 61  used by the opda
1c692 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 20 20 20  te hook */.     
1c693 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20   int op;        
1c694 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72     /* Opcode for
1c695 20 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51   update hook: SQ
1c696 4c 49 54 45 5f 55 50 44 41 54 45 20 6f 72 20 53  LITE_UPDATE or S
1c697 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a  QLITE_INSERT */.
1c698 20 20 20 20 7d 20 62 66 3b 0a 20 20 20 20 73 74      } bf;.    st
1c699 72 75 63 74 20 4f 50 5f 44 65 6c 65 74 65 5f 73  ruct OP_Delete_s
1c69a 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c69b 20 20 69 36 34 20 69 4b 65 79 3b 0a 20 20 20 20    i64 iKey;.    
1c69c 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c69d 3b 0a 20 20 20 20 7d 20 62 67 3b 0a 20 20 20 20  ;.    } bg;.    
1c69e 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 44 61 74  struct OP_RowDat
1c69f 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  a_stack_vars {. 
1c6a0 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c6a1 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 72  *pC;.      BtCur
1c6a2 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20  sor *pCrsr;.    
1c6a3 20 20 75 33 32 20 6e 3b 0a 20 20 20 20 20 20 69    u32 n;.      i
1c6a4 36 34 20 6e 36 34 3b 0a 20 20 20 20 7d 20 62 68  64 n64;.    } bh
1c6a5 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c6a6 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73  Rowid_stack_vars
1c6a7 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c6a8 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69  sor *pC;.      i
1c6a9 36 34 20 76 3b 0a 20 20 20 20 20 20 73 71 6c 69  64 v;.      sqli
1c6aa 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
1c6ab 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c  .      const sql
1c6ac 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
1c6ad 64 75 6c 65 3b 0a 20 20 20 20 7d 20 62 69 3b 0a  dule;.    } bi;.
1c6ae 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 75      struct OP_Nu
1c6af 6c 6c 52 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73  llRow_stack_vars
1c6b0 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c6b1 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62  sor *pC;.    } b
1c6b2 6a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  j;.    struct OP
1c6b3 5f 4c 61 73 74 5f 73 74 61 63 6b 5f 76 61 72 73  _Last_stack_vars
1c6b4 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c6b5 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42  sor *pC;.      B
1c6b6 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c6b7 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
1c6b8 20 20 20 7d 20 62 6b 3b 0a 20 20 20 20 73 74 72     } bk;.    str
1c6b9 75 63 74 20 4f 50 5f 52 65 77 69 6e 64 5f 73 74  uct OP_Rewind_st
1c6ba 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c6bb 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c6bc 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20  .      BtCursor 
1c6bd 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e  *pCrsr;.      in
1c6be 74 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6c 3b  t res;.    } bl;
1c6bf 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e  .    struct OP_N
1c6c0 65 78 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ext_stack_vars {
1c6c1 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c6c2 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43  r *pC;.      BtC
1c6c3 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1c6c4 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1c6c5 20 7d 20 62 6d 3b 0a 20 20 20 20 73 74 72 75 63   } bm;.    struc
1c6c6 74 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 5f 73  t OP_IdxInsert_s
1c6c7 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c6c8 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c6c9 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  ;.      BtCursor
1c6ca 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69   *pCrsr;.      i
1c6cb 6e 74 20 6e 4b 65 79 3b 0a 20 20 20 20 20 20 63  nt nKey;.      c
1c6cc 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b  onst char *zKey;
1c6cd 0a 20 20 20 20 7d 20 62 6e 3b 0a 20 20 20 20 73  .    } bn;.    s
1c6ce 74 72 75 63 74 20 4f 50 5f 49 64 78 44 65 6c 65  truct OP_IdxDele
1c6cf 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  te_stack_vars {.
1c6d0 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c6d1 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75   *pC;.      BtCu
1c6d2 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20  rsor *pCrsr;.   
1c6d3 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
1c6d4 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1c6d5 20 72 3b 0a 20 20 20 20 7d 20 62 6f 3b 0a 20 20   r;.    } bo;.  
1c6d6 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 52    struct OP_IdxR
1c6d7 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73 20  owid_stack_vars 
1c6d8 7b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  {.      BtCursor
1c6d9 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 56   *pCrsr;.      V
1c6da 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  dbeCursor *pC;. 
1c6db 20 20 20 20 20 69 36 34 20 72 6f 77 69 64 3b 0a       i64 rowid;.
1c6dc 20 20 20 20 7d 20 62 70 3b 0a 20 20 20 20 73 74      } bp;.    st
1c6dd 72 75 63 74 20 4f 50 5f 49 64 78 47 45 5f 73 74  ruct OP_IdxGE_st
1c6de 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c6df 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c6e0 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a  .      int res;.
1c6e1 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65        UnpackedRe
1c6e2 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d 20 62 71  cord r;.    } bq
1c6e3 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c6e4 44 65 73 74 72 6f 79 5f 73 74 61 63 6b 5f 76 61  Destroy_stack_va
1c6e5 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  rs {.      int i
1c6e6 4d 6f 76 65 64 3b 0a 20 20 20 20 20 20 69 6e 74  Moved;.      int
1c6e7 20 69 43 6e 74 3b 0a 20 20 20 20 20 20 56 64 62   iCnt;.      Vdb
1c6e8 65 20 2a 70 56 64 62 65 3b 0a 20 20 20 20 20 20  e *pVdbe;.      
1c6e9 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 7d 20 62  int iDb;.    } b
1c6ea 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  r;.    struct OP
1c6eb 5f 43 6c 65 61 72 5f 73 74 61 63 6b 5f 76 61 72  _Clear_stack_var
1c6ec 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43  s {.      int nC
1c6ed 68 61 6e 67 65 3b 0a 20 20 20 20 7d 20 62 73 3b  hange;.    } bs;
1c6ee 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43  .    struct OP_C
1c6ef 72 65 61 74 65 54 61 62 6c 65 5f 73 74 61 63 6b  reateTable_stack
1c6f0 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
1c6f1 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 69 6e  t pgno;.      in
1c6f2 74 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 44  t flags;.      D
1c6f3 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20 62 74  b *pDb;.    } bt
1c6f4 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c6f5 50 61 72 73 65 53 63 68 65 6d 61 5f 73 74 61 63  ParseSchema_stac
1c6f6 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c6f7 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 63 6f  nt iDb;.      co
1c6f8 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nst char *zMaste
1c6f9 72 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  r;.      char *z
1c6fa 53 71 6c 3b 0a 20 20 20 20 20 20 49 6e 69 74 44  Sql;.      InitD
1c6fb 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20  ata initData;.  
1c6fc 20 20 7d 20 62 75 3b 0a 20 20 20 20 73 74 72 75    } bu;.    stru
1c6fd 63 74 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43  ct OP_IntegrityC
1c6fe 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  k_stack_vars {. 
1c6ff 20 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20       int nRoot; 
1c700 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c701 66 20 74 61 62 6c 65 73 20 74 6f 20 63 68 65 63  f tables to chec
1c702 6b 2e 20 20 28 4e 75 6d 62 65 72 20 6f 66 20 72  k.  (Number of r
1c703 6f 6f 74 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20  oot pages.) */. 
1c704 20 20 20 20 20 69 6e 74 20 2a 61 52 6f 6f 74 3b       int *aRoot;
1c705 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
1c706 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72   rootpage number
1c707 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20  s for tables to 
1c708 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20  be checked */.  
1c709 20 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20      int j;      
1c70a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1c70b 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ter */.      int
1c70c 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a 20   nErr;       /* 
1c70d 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  Number of errors
1c70e 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20   reported */.   
1c70f 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20     char *z;     
1c710 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68     /* Text of th
1c711 65 20 65 72 72 6f 72 20 72 65 70 6f 72 74 20 2a  e error report *
1c712 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 6e 45  /.      Mem *pnE
1c713 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67 69 73  rr;     /* Regis
1c714 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72 61 63  ter keeping trac
1c715 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61  k of errors rema
1c716 69 6e 69 6e 67 20 2a 2f 0a 20 20 20 20 7d 20 62  ining */.    } b
1c717 76 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  v;.    struct OP
1c718 5f 52 6f 77 53 65 74 41 64 64 5f 73 74 61 63 6b  _RowSetAdd_stack
1c719 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65  _vars {.      Me
1c71a 6d 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 20 4d  m *pIdx;.      M
1c71b 65 6d 20 2a 70 56 61 6c 3b 0a 20 20 20 20 7d 20  em *pVal;.    } 
1c71c 62 77 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bw;.    struct O
1c71d 50 5f 52 6f 77 53 65 74 52 65 61 64 5f 73 74 61  P_RowSetRead_sta
1c71e 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c71f 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 20 20 20  Mem *pIdx;.     
1c720 20 69 36 34 20 76 61 6c 3b 0a 20 20 20 20 7d 20   i64 val;.    } 
1c721 62 78 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bx;.    struct O
1c722 50 5f 52 6f 77 53 65 74 54 65 73 74 5f 73 74 61  P_RowSetTest_sta
1c723 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c724 69 6e 74 20 69 53 65 74 3b 0a 20 20 20 20 20 20  int iSet;.      
1c725 69 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20  int exists;.    
1c726 7d 20 62 79 3b 0a 20 20 20 20 73 74 72 75 63 74  } by;.    struct
1c727 20 4f 50 5f 50 72 6f 67 72 61 6d 5f 73 74 61 63   OP_Program_stac
1c728 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c729 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20  nt nMem;        
1c72a 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1c72b 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 67 69 73   of memory regis
1c72c 74 65 72 73 20 66 6f 72 20 73 75 62 2d 70 72 6f  ters for sub-pro
1c72d 67 72 61 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e  gram */.      in
1c72e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  t nByte;        
1c72f 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f        /* Bytes o
1c730 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20  f runtime space 
1c731 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 75 62  required for sub
1c732 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20  -program */.    
1c733 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20 20    Mem *pRt;     
1c734 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1c735 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61 74  ister to allocat
1c736 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20  e runtime space 
1c737 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d  */.      Mem *pM
1c738 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
1c739 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72   /* Used to iter
1c73a 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 6d 6f  ate through memo
1c73b 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 20 20  ry cells */.    
1c73c 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20    Mem *pEnd;    
1c73d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73            /* Las
1c73e 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e  t memory cell in
1c73f 20 6e 65 77 20 61 72 72 61 79 20 2a 2f 0a 20 20   new array */.  
1c740 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70      VdbeFrame *p
1c741 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e  Frame;      /* N
1c742 65 77 20 76 64 62 65 20 66 72 61 6d 65 20 74 6f  ew vdbe frame to
1c743 20 65 78 65 63 75 74 65 20 69 6e 20 2a 2f 0a 20   execute in */. 
1c744 20 20 20 20 20 53 75 62 50 72 6f 67 72 61 6d 20       SubProgram 
1c745 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a 20  *pProgram;   /* 
1c746 53 75 62 2d 70 72 6f 67 72 61 6d 20 74 6f 20 65  Sub-program to e
1c747 78 65 63 75 74 65 20 2a 2f 0a 20 20 20 20 20 20  xecute */.      
1c748 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20 20  void *t;        
1c749 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e          /* Token
1c74a 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72 69   identifying tri
1c74b 67 67 65 72 20 2a 2f 0a 20 20 20 20 7d 20 62 7a  gger */.    } bz
1c74c 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c74d 50 61 72 61 6d 5f 73 74 61 63 6b 5f 76 61 72 73  Param_stack_vars
1c74e 20 7b 0a 20 20 20 20 20 20 56 64 62 65 46 72 61   {.      VdbeFra
1c74f 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20 20  me *pFrame;.    
1c750 20 20 4d 65 6d 20 2a 70 49 6e 3b 0a 20 20 20 20    Mem *pIn;.    
1c751 7d 20 63 61 3b 0a 20 20 20 20 73 74 72 75 63 74  } ca;.    struct
1c752 20 4f 50 5f 4d 65 6d 4d 61 78 5f 73 74 61 63 6b   OP_MemMax_stack
1c753 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65  _vars {.      Me
1c754 6d 20 2a 70 49 6e 31 3b 0a 20 20 20 20 20 20 56  m *pIn1;.      V
1c755 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
1c756 3b 0a 20 20 20 20 7d 20 63 62 3b 0a 20 20 20 20  ;.    } cb;.    
1c757 73 74 72 75 63 74 20 4f 50 5f 41 67 67 53 74 65  struct OP_AggSte
1c758 70 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  p_stack_vars {. 
1c759 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20       int n;.    
1c75a 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 4d    int i;.      M
1c75b 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20  em *pMem;.      
1c75c 4d 65 6d 20 2a 70 52 65 63 3b 0a 20 20 20 20 20  Mem *pRec;.     
1c75d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1c75e 20 63 74 78 3b 0a 20 20 20 20 20 20 73 71 6c 69   ctx;.      sqli
1c75f 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61  te3_value **apVa
1c760 6c 3b 0a 20 20 20 20 7d 20 63 63 3b 0a 20 20 20  l;.    } cc;.   
1c761 20 73 74 72 75 63 74 20 4f 50 5f 41 67 67 46 69   struct OP_AggFi
1c762 6e 61 6c 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  nal_stack_vars {
1c763 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d  .      Mem *pMem
1c764 3b 0a 20 20 20 20 7d 20 63 64 3b 0a 20 20 20 20  ;.    } cd;.    
1c765 73 74 72 75 63 74 20 4f 50 5f 49 6e 63 72 56 61  struct OP_IncrVa
1c766 63 75 75 6d 5f 73 74 61 63 6b 5f 76 61 72 73 20  cuum_stack_vars 
1c767 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  {.      Btree *p
1c768 42 74 3b 0a 20 20 20 20 7d 20 63 65 3b 0a 20 20  Bt;.    } ce;.  
1c769 20 20 73 74 72 75 63 74 20 4f 50 5f 56 42 65 67    struct OP_VBeg
1c76a 69 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  in_stack_vars {.
1c76b 20 20 20 20 20 20 56 54 61 62 6c 65 20 2a 70 56        VTable *pV
1c76c 54 61 62 3b 0a 20 20 20 20 7d 20 63 66 3b 0a 20  Tab;.    } cf;. 
1c76d 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 4f 70     struct OP_VOp
1c76e 65 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  en_stack_vars {.
1c76f 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c770 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20 73 71   *pCur;.      sq
1c771 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1c772 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a  r *pVtabCursor;.
1c773 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
1c774 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20  ab *pVtab;.     
1c775 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
1c776 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 20  *pModule;.    } 
1c777 63 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  cg;.    struct O
1c778 50 5f 56 46 69 6c 74 65 72 5f 73 74 61 63 6b 5f  P_VFilter_stack_
1c779 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c77a 20 6e 41 72 67 3b 0a 20 20 20 20 20 20 69 6e 74   nArg;.      int
1c77b 20 69 51 75 65 72 79 3b 0a 20 20 20 20 20 20 63   iQuery;.      c
1c77c 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
1c77d 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20  ule *pModule;.  
1c77e 20 20 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b      Mem *pQuery;
1c77f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 41 72 67  .      Mem *pArg
1c780 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
1c781 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56  _vtab_cursor *pV
1c782 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  tabCursor;.     
1c783 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1c784 56 74 61 62 3b 0a 20 20 20 20 20 20 56 64 62 65  Vtab;.      Vdbe
1c785 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20  Cursor *pCur;.  
1c786 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1c787 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
1c788 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 20  Mem **apArg;.   
1c789 20 7d 20 63 68 3b 0a 20 20 20 20 73 74 72 75 63   } ch;.    struc
1c78a 74 20 4f 50 5f 56 43 6f 6c 75 6d 6e 5f 73 74 61  t OP_VColumn_sta
1c78b 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c78c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1c78d 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  tab;.      const
1c78e 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
1c78f 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20  *pModule;.      
1c790 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 20 20  Mem *pDest;.    
1c791 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1c792 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  t sContext;.    
1c793 7d 20 63 69 3b 0a 20 20 20 20 73 74 72 75 63 74  } ci;.    struct
1c794 20 4f 50 5f 56 4e 65 78 74 5f 73 74 61 63 6b 5f   OP_VNext_stack_
1c795 76 61 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c  vars {.      sql
1c796 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1c797 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71  ;.      const sq
1c798 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1c799 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74  odule;.      int
1c79a 20 72 65 73 3b 0a 20 20 20 20 20 20 56 64 62 65   res;.      Vdbe
1c79b 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20  Cursor *pCur;.  
1c79c 20 20 7d 20 63 6a 3b 0a 20 20 20 20 73 74 72 75    } cj;.    stru
1c79d 63 74 20 4f 50 5f 56 52 65 6e 61 6d 65 5f 73 74  ct OP_VRename_st
1c79e 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c79f 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1c7a0 56 74 61 62 3b 0a 20 20 20 20 20 20 4d 65 6d 20  Vtab;.      Mem 
1c7a1 2a 70 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 63 6b  *pName;.    } ck
1c7a2 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c7a3 56 55 70 64 61 74 65 5f 73 74 61 63 6b 5f 76 61  VUpdate_stack_va
1c7a4 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  rs {.      sqlit
1c7a5 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1c7a6 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f        sqlite3_mo
1c7a7 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1c7a8 20 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20       int nArg;. 
1c7a9 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
1c7aa 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72    sqlite_int64 r
1c7ab 6f 77 69 64 3b 0a 20 20 20 20 20 20 4d 65 6d 20  owid;.      Mem 
1c7ac 2a 2a 61 70 41 72 67 3b 0a 20 20 20 20 20 20 4d  **apArg;.      M
1c7ad 65 6d 20 2a 70 58 3b 0a 20 20 20 20 7d 20 63 6c  em *pX;.    } cl
1c7ae 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c7af 50 61 67 65 63 6f 75 6e 74 5f 73 74 61 63 6b 5f  Pagecount_stack_
1c7b0 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c7b1 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e   p1;.      int n
1c7b2 50 61 67 65 3b 0a 20 20 20 20 20 20 50 61 67 65  Page;.      Page
1c7b3 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 7d  r *pPager;.    }
1c7b4 20 63 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20   cm;.    struct 
1c7b5 4f 50 5f 54 72 61 63 65 5f 73 74 61 63 6b 5f 76  OP_Trace_stack_v
1c7b6 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72  ars {.      char
1c7b7 20 2a 7a 54 72 61 63 65 3b 0a 20 20 20 20 7d 20   *zTrace;.    } 
1c7b8 63 6e 3b 0a 20 20 7d 20 75 3b 0a 20 20 2f 2a 20  cn;.  } u;.  /* 
1c7b9 45 6e 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  End automaticall
1c7ba 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65  y generated code
1c7bb 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .  *************
1c7bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c7bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c7be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c7bf 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 61 73 73 65  *******/..  asse
1c7c0 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44  rt( p->magic==VD
1c7c1 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b 20  BE_MAGIC_RUN ); 
1c7c2 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70   /* sqlite3_step
1c7c3 28 29 20 76 65 72 69 66 69 65 73 20 74 68 69 73  () verifies this
1c7c4 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62   */.  assert( db
1c7c5 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f  ->magic==SQLITE_
1c7c6 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20  MAGIC_BUSY );.  
1c7c7 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78  sqlite3VdbeMutex
1c7c8 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 20  ArrayEnter(p);. 
1c7c9 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
1c7ca 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
1c7cb 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
1c7cc 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e  if a malloc() in
1c7cd 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73  side a call to s
1c7ce 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1c7cf 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73  xt() or.    ** s
1c7d0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1c7d1 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20  xt16() failed.  
1c7d2 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d  */.    goto no_m
1c7d3 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  em;.  }.  assert
1c7d4 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  ( p->rc==SQLITE_
1c7d5 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c  OK || p->rc==SQL
1c7d6 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20 70 2d  ITE_BUSY );.  p-
1c7d7 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
1c7d8 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 78  .  assert( p->ex
1c7d9 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 2d  plain==0 );.  p-
1c7da 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b  >pResultSet = 0;
1c7db 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c  .  db->busyHandl
1c7dc 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20  er.nBusy = 0;.  
1c7dd 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52  CHECK_FOR_INTERR
1c7de 55 50 54 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  UPT;.  sqlite3Vd
1c7df 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70 29 3b  beIOTraceSql(p);
1c7e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1c7e1 45 42 55 47 0a 20 20 73 71 6c 69 74 65 33 42 65  EBUG.  sqlite3Be
1c7e2 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
1c7e3 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 63 3d 3d  );.  if( p->pc==
1c7e4 30 20 0a 20 20 20 26 26 20 28 28 70 2d 3e 64 62  0 .   && ((p->db
1c7e5 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
1c7e6 5f 56 64 62 65 4c 69 73 74 69 6e 67 29 20 7c 7c  _VdbeListing) ||
1c7e7 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c 20   fileExists(db, 
1c7e8 22 76 64 62 65 5f 65 78 70 6c 61 69 6e 22 29 29  "vdbe_explain"))
1c7e9 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  .  ){.    int i;
1c7ea 0a 20 20 20 20 70 72 69 6e 74 66 28 22 56 44 42  .    printf("VDB
1c7eb 45 20 50 72 6f 67 72 61 6d 20 4c 69 73 74 69 6e  E Program Listin
1c7ec 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73 71 6c 69  g:\n");.    sqli
1c7ed 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28  te3VdbePrintSql(
1c7ee 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  p);.    for(i=0;
1c7ef 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b   i<p->nOp; i++){
1c7f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1c7f1 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f 75 74  bePrintOp(stdout
1c7f2 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29  , i, &p->aOp[i])
1c7f3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
1c7f4 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c  ( fileExists(db,
1c7f5 20 22 76 64 62 65 5f 74 72 61 63 65 22 29 20 29   "vdbe_trace") )
1c7f6 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 65 20 3d  {.    p->trace =
1c7f7 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a 20 20 73   stdout;.  }.  s
1c7f8 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
1c7f9 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69 66 0a  alloc();.#endif.
1c7fa 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63 3b 20    for(pc=p->pc; 
1c7fb 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70  rc==SQLITE_OK; p
1c7fc 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74  c++){.    assert
1c7fd 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c 70 2d  ( pc>=0 && pc<p-
1c7fe 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66 28 20  >nOp );.    if( 
1c7ff 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1c800 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1c801 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
1c802 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63 20 3d  ILE.    origPc =
1c803 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74 20 3d   pc;.    start =
1c804 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
1c805 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4f 70  ;.#endif.    pOp
1c806 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 63 5d 3b 0a   = &p->aOp[pc];.
1c807 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c  .    /* Only all
1c808 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51  ow tracing if SQ
1c809 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65  LITE_DEBUG is de
1c80a 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69  fined..    */.#i
1c80b 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1c80c 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61  G.    if( p->tra
1c80d 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ce ){.      if( 
1c80e 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  pc==0 ){.       
1c80f 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78   printf("VDBE Ex
1c810 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e  ecution Trace:\n
1c811 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ");.        sqli
1c812 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28  te3VdbePrintSql(
1c813 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
1c814 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1c815 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70  ntOp(p->trace, p
1c816 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20  c, pOp);.    }. 
1c817 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d     if( p->trace=
1c818 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20  =0 && pc==0 ){. 
1c819 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69       sqlite3Begi
1c81a 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
1c81b 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c 65 45  .      if( fileE
1c81c 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f  xists(db, "vdbe_
1c81d 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a 20 20  sqltrace") ){.  
1c81e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c81f 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20  ePrintSql(p);.  
1c820 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1c821 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
1c822 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  oc();.    }.#end
1c823 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f  if.      ..    /
1c824 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
1c825 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d  f we need to sim
1c826 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75  ulate an interru
1c827 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68  pt.  This only h
1c828 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66  appens.    ** if
1c829 20 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69   we have a speci
1c82a 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20  al test build.. 
1c82b 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c     */.#ifdef SQL
1c82c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28  ITE_TEST.    if(
1c82d 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
1c82e 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20  pt_count>0 ){.  
1c82f 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65      sqlite3_inte
1c830 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20  rrupt_count--;. 
1c831 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1c832 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74  _interrupt_count
1c833 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
1c834 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1c835 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  (db);.      }.  
1c836 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e    }.#endif..#ifn
1c837 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1c838 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
1c839 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68  K.    /* Call th
1c83a 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
1c83b 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e  ack if it is con
1c83c 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 65 20  figured and the 
1c83d 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a  required number.
1c83e 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f      ** of VDBE o
1c83f 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 78 65  ps have been exe
1c840 63 75 74 65 64 20 28 65 69 74 68 65 72 20 73 69  cuted (either si
1c841 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74  nce this invocat
1c842 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71  ion of.    ** sq
1c843 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20  lite3VdbeExec() 
1c844 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69  or since last ti
1c845 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  me the progress 
1c846 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c  callback was cal
1c847 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20  led)..    ** If 
1c848 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
1c849 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f  lback returns no
1c84a 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65  n-zero, exit the
1c84b 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1c84c 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72   with.    ** a r
1c84d 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54  eturn code SQLIT
1c84e 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a  E_ABORT..    */.
1c84f 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f      if( db->xPro
1c850 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69  gress ){.      i
1c851 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73  f( db->nProgress
1c852 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70  Ops==nProgressOp
1c853 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  s ){.        int
1c854 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66   prc;.        if
1c855 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1c856 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
1c857 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1c858 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d  e;.        prc =
1c859 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62  db->xProgress(db
1c85a 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b  ->pProgressArg);
1c85b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
1c85c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1c85d 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1c85e 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20  e_to_misuse;.   
1c85f 20 20 20 20 20 69 66 28 20 70 72 63 21 3d 30 20       if( prc!=0 
1c860 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
1c861 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
1c862 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  PT;.          go
1c863 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61  to vdbe_error_ha
1c864 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  lt;.        }.  
1c865 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f        nProgressO
1c866 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  ps = 0;.      }.
1c867 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f        nProgressO
1c868 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ps++;.    }.#end
1c869 69 66 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f  if..    /* Do co
1c86a 6d 6d 6f 6e 20 73 65 74 75 70 20 70 72 6f 63 65  mmon setup proce
1c86b 73 73 69 6e 67 20 66 6f 72 20 61 6e 79 20 6f 70  ssing for any op
1c86c 63 6f 64 65 20 74 68 61 74 20 69 73 20 6d 61 72  code that is mar
1c86d 6b 65 64 0a 20 20 20 20 2a 2a 20 77 69 74 68 20  ked.    ** with 
1c86e 74 68 65 20 22 6f 75 74 32 2d 70 72 65 72 65 6c  the "out2-prerel
1c86f 65 61 73 65 22 20 74 61 67 2e 20 20 53 75 63 68  ease" tag.  Such
1c870 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 61 20   opcodes have a 
1c871 73 69 6e 67 6c 65 0a 20 20 20 20 2a 2a 20 6f 75  single.    ** ou
1c872 74 70 75 74 20 77 68 69 63 68 20 69 73 20 73 70  tput which is sp
1c873 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 50  ecified by the P
1c874 32 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68  2 parameter.  Th
1c875 65 20 50 32 20 72 65 67 69 73 74 65 72 0a 20 20  e P2 register.  
1c876 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69    ** is initiali
1c877 7a 65 64 20 74 6f 20 61 20 4e 55 4c 4c 2e 0a 20  zed to a NULL.. 
1c878 20 20 20 2a 2f 0a 20 20 20 20 6f 70 50 72 6f 70     */.    opProp
1c879 65 72 74 79 20 3d 20 6f 70 63 6f 64 65 50 72 6f  erty = opcodePro
1c87a 70 65 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 6f 64  perty[pOp->opcod
1c87b 65 5d 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 50  e];.    if( (opP
1c87c 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f  roperty & OPFLG_
1c87d 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 29  OUT2_PRERELEASE)
1c87e 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  !=0 ){.      ass
1c87f 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29  ert( pOp->p2>0 )
1c880 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1c881 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p2<=p->nMem
1c882 20 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 20 3d   );.      pOut =
1c883 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1c884 32 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2];.      sqlite
1c885 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45  3VdbeMemReleaseE
1c886 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 3b 0a 20  xternal(pOut);. 
1c887 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73       pOut->flags
1c888 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
1c889 20 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 30 3b 0a     pOut->n = 0;.
1c88a 20 20 20 20 7d 65 6c 73 65 0a 20 0a 20 20 20 20      }else. .    
1c88b 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74  /* Do common set
1c88c 75 70 20 66 6f 72 20 6f 70 63 6f 64 65 73 20 6d  up for opcodes m
1c88d 61 72 6b 65 64 20 77 69 74 68 20 6f 6e 65 20 6f  arked with one o
1c88e 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  f the following.
1c88f 20 20 20 20 2a 2a 20 63 6f 6d 62 69 6e 61 74 69      ** combinati
1c890 6f 6e 73 20 6f 66 20 70 72 6f 70 65 72 74 69 65  ons of propertie
1c891 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s..    **.    **
1c892 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 0a 20             in1. 
1c893 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
1c894 69 6e 31 20 69 6e 32 0a 20 20 20 20 2a 2a 20 20  in1 in2.    **  
1c895 20 20 20 20 20 20 20 20 20 69 6e 31 20 69 6e 32           in1 in2
1c896 20 6f 75 74 33 0a 20 20 20 20 2a 2a 20 20 20 20   out3.    **    
1c897 20 20 20 20 20 20 20 69 6e 31 20 69 6e 33 0a 20         in1 in3. 
1c898 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 56 61 72     **.    ** Var
1c899 69 61 62 6c 65 73 20 70 49 6e 31 2c 20 70 49 6e  iables pIn1, pIn
1c89a 32 2c 20 61 6e 64 20 70 49 6e 33 20 61 72 65 20  2, and pIn3 are 
1c89b 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f  made to point to
1c89c 20 61 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20   appropriate.   
1c89d 20 2a 2a 20 72 65 67 69 73 74 65 72 73 20 66 6f   ** registers fo
1c89e 72 20 69 6e 70 75 74 73 2e 20 20 56 61 72 69 61  r inputs.  Varia
1c89f 62 6c 65 20 70 4f 75 74 20 70 6f 69 6e 74 73 20  ble pOut points 
1c8a0 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 72 65  to the output re
1c8a1 67 69 73 74 65 72 2e 0a 20 20 20 20 2a 2f 0a 20  gister..    */. 
1c8a2 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72     if( (opProper
1c8a3 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21  ty & OPFLG_IN1)!
1c8a4 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
1c8a5 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b  rt( pOp->p1>0 );
1c8a6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1c8a7 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p1<=p->nMem 
1c8a8 29 3b 0a 20 20 20 20 20 20 70 49 6e 31 20 3d 20  );.      pIn1 = 
1c8a9 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
1c8aa 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45  ];.      REGISTE
1c8ab 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c  R_TRACE(pOp->p1,
1c8ac 20 70 49 6e 31 29 3b 0a 20 20 20 20 20 20 69 66   pIn1);.      if
1c8ad 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20  ( (opProperty & 
1c8ae 4f 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b  OPFLG_IN2)!=0 ){
1c8af 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1c8b0 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20   pOp->p2>0 );.  
1c8b1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1c8b2 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p2<=p->nMem )
1c8b3 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 32 20 3d  ;.        pIn2 =
1c8b4 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1c8b5 32 5d 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49  2];.        REGI
1c8b6 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1c8b7 70 32 2c 20 70 49 6e 32 29 3b 0a 20 20 20 20 20  p2, pIn2);.     
1c8b8 20 20 20 2f 2a 20 41 73 20 63 75 72 72 65 6e 74     /* As current
1c8b9 6c 79 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2c 20  ly implemented, 
1c8ba 69 6e 32 20 69 6d 70 6c 69 65 73 20 6f 75 74 33  in2 implies out3
1c8bb 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 72  .  There is no r
1c8bc 65 61 73 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a  eason.        **
1c8bd 20 77 68 79 20 74 68 69 73 20 68 61 73 20 74 6f   why this has to
1c8be 20 62 65 2c 20 69 74 20 6a 75 73 74 20 77 6f 72   be, it just wor
1c8bf 6b 65 64 20 6f 75 74 20 74 68 61 74 20 77 61 79  ked out that way
1c8c0 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73  . */.        ass
1c8c1 65 72 74 28 20 28 6f 70 50 72 6f 70 65 72 74 79  ert( (opProperty
1c8c2 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d   & OPFLG_OUT3)!=
1c8c3 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  0 );.        ass
1c8c4 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29  ert( pOp->p3>0 )
1c8c5 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1c8c6 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  ( pOp->p3<=p->nM
1c8c7 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  em );.        pO
1c8c8 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1c8c9 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 7d 65  p->p3];.      }e
1c8ca 6c 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65  lse if( (opPrope
1c8cb 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29  rty & OPFLG_IN3)
1c8cc 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  !=0 ){.        a
1c8cd 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30  ssert( pOp->p3>0
1c8ce 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
1c8cf 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e  rt( pOp->p3<=p->
1c8d0 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20  nMem );.        
1c8d1 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn3 = &p->aMem[
1c8d2 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20  pOp->p3];.      
1c8d3 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1c8d4 28 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b  (pOp->p3, pIn3);
1c8d5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
1c8d6 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72  se if( (opProper
1c8d7 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21  ty & OPFLG_IN2)!
1c8d8 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
1c8d9 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b  rt( pOp->p2>0 );
1c8da 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1c8db 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p2<=p->nMem 
1c8dc 29 3b 0a 20 20 20 20 20 20 70 49 6e 32 20 3d 20  );.      pIn2 = 
1c8dd 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1c8de 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45  ];.      REGISTE
1c8df 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c  R_TRACE(pOp->p2,
1c8e0 20 70 49 6e 32 29 3b 0a 20 20 20 20 7d 65 6c 73   pIn2);.    }els
1c8e1 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74  e if( (opPropert
1c8e2 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 21 3d  y & OPFLG_IN3)!=
1c8e3 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
1c8e4 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a  t( pOp->p3>0 );.
1c8e5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1c8e6 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
1c8e7 3b 0a 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26  ;.      pIn3 = &
1c8e8 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1c8e9 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 52  ;.      REGISTER
1c8ea 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20  _TRACE(pOp->p3, 
1c8eb 70 49 6e 33 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  pIn3);.    }..  
1c8ec 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f    switch( pOp->o
1c8ed 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a  pcode ){../*****
1c8ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8f2 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61 74  ********.** What
1c8f3 20 66 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d 61   follows is a ma
1c8f4 73 73 69 76 65 20 73 77 69 74 63 68 20 73 74 61  ssive switch sta
1c8f5 74 65 6d 65 6e 74 20 77 68 65 72 65 20 65 61 63  tement where eac
1c8f6 68 20 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e 74  h case implement
1c8f7 73 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65 20  s a.** separate 
1c8f8 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74  instruction in t
1c8f9 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
1c8fa 6e 65 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c 6f  ne.  If we follo
1c8fb 77 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 69  w the usual.** i
1c8fc 6e 64 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76 65  ndentation conve
1c8fd 6e 74 69 6f 6e 73 2c 20 65 61 63 68 20 63 61 73  ntions, each cas
1c8fe 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 64 65  e should be inde
1c8ff 6e 74 65 64 20 62 79 20 36 20 73 70 61 63 65 73  nted by 6 spaces
1c900 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 69  .  But.** that i
1c901 73 20 61 20 6c 6f 74 20 6f 66 20 77 61 73 74 65  s a lot of waste
1c902 64 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 6c  d space on the l
1c903 65 66 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f 20  eft margin.  So 
1c904 74 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 0a  the code within.
1c905 2a 2a 20 74 68 65 20 73 77 69 74 63 68 20 73 74  ** the switch st
1c906 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72 65  atement will bre
1c907 61 6b 20 77 69 74 68 20 63 6f 6e 76 65 6e 74 69  ak with conventi
1c908 6f 6e 20 61 6e 64 20 62 65 20 66 6c 75 73 68 2d  on and be flush-
1c909 6c 65 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a 2a  left. Another.**
1c90a 20 62 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73 69   big comment (si
1c90b 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e  milar to this on
1c90c 65 29 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68 65  e) will mark the
1c90d 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f   point in the co
1c90e 64 65 20 77 68 65 72 65 0a 2a 2a 20 77 65 20 74  de where.** we t
1c90f 72 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20 74  ransition back t
1c910 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61  o normal indenta
1c911 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
1c912 66 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65 61  formatting of ea
1c913 63 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f 72  ch case is impor
1c914 74 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65 66  tant.  The makef
1c915 69 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0a 2a  ile for SQLite.*
1c916 2a 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20  * generates two 
1c917 43 20 66 69 6c 65 73 20 22 6f 70 63 6f 64 65 73  C files "opcodes
1c918 2e 68 22 20 61 6e 64 20 22 6f 70 63 6f 64 65 73  .h" and "opcodes
1c919 2e 63 22 20 62 79 20 73 63 61 6e 6e 69 6e 67 20  .c" by scanning 
1c91a 74 68 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f 6f  this.** file loo
1c91b 6b 69 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20 74  king for lines t
1c91c 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 22  hat begin with "
1c91d 63 61 73 65 20 4f 50 5f 22 2e 20 20 54 68 65 20  case OP_".  The 
1c91e 6f 70 63 6f 64 65 73 2e 68 20 66 69 6c 65 73 0a  opcodes.h files.
1c91f 2a 2a 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c 65  ** will be fille
1c920 64 20 77 69 74 68 20 23 64 65 66 69 6e 65 73 20  d with #defines 
1c921 74 68 61 74 20 67 69 76 65 20 75 6e 69 71 75 65  that give unique
1c922 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20   integer values 
1c923 74 6f 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f 64  to each.** opcod
1c924 65 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65  e and the opcode
1c925 73 2e 63 20 66 69 6c 65 20 69 73 20 66 69 6c 6c  s.c file is fill
1c926 65 64 20 77 69 74 68 20 61 6e 20 61 72 72 61 79  ed with an array
1c927 20 6f 66 20 73 74 72 69 6e 67 73 20 77 68 65 72   of strings wher
1c928 65 0a 2a 2a 20 65 61 63 68 20 73 74 72 69 6e 67  e.** each string
1c929 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 69 63   is the symbolic
1c92a 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f   name for the co
1c92b 72 72 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63 6f  rresponding opco
1c92c 64 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 63  de.  If the.** c
1c92d 61 73 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ase statement is
1c92e 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 63   followed by a c
1c92f 6f 6d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f  omment of the fo
1c930 72 6d 20 22 2f 23 20 73 61 6d 65 20 61 73 20 2e  rm "/# same as .
1c931 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20 63  .. #/".** that c
1c932 6f 6d 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74  omment is used t
1c933 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
1c934 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75 65  particular value
1c935 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 0a   of the opcode..
1c936 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 6b 65 79 77  **.** Other keyw
1c937 6f 72 64 73 20 69 6e 20 74 68 65 20 63 6f 6d 6d  ords in the comm
1c938 65 6e 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ent that follows
1c939 20 65 61 63 68 20 63 61 73 65 20 61 72 65 20 75   each case are u
1c93a 73 65 64 20 74 6f 0a 2a 2a 20 63 6f 6e 73 74 72  sed to.** constr
1c93b 75 63 74 20 74 68 65 20 4f 50 46 4c 47 5f 49 4e  uct the OPFLG_IN
1c93c 49 54 49 41 4c 49 5a 45 52 20 76 61 6c 75 65 20  ITIALIZER value 
1c93d 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73  that initializes
1c93e 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b   opcodeProperty[
1c93f 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72 64 73 20 69  ]..** Keywords i
1c940 6e 63 6c 75 64 65 3a 20 69 6e 31 2c 20 69 6e 32  nclude: in1, in2
1c941 2c 20 69 6e 33 2c 20 6f 75 74 32 5f 70 72 65 72  , in3, out2_prer
1c942 65 6c 65 61 73 65 2c 20 6f 75 74 32 2c 20 6f 75  elease, out2, ou
1c943 74 33 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20  t3.  See.** the 
1c944 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63  mkopcodeh.awk sc
1c945 72 69 70 74 20 66 6f 72 20 61 64 64 69 74 69 6f  ript for additio
1c946 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
1c947 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74 61  .**.** Documenta
1c948 74 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45 20  tion about VDBE 
1c949 6f 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65 72  opcodes is gener
1c94a 61 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e 67  ated by scanning
1c94b 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 66 6f   this file.** fo
1c94c 72 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74 20  r lines of that 
1c94d 63 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65 3a  contain "Opcode:
1c94e 22 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61 6e  ".  That line an
1c94f 64 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74  d all subsequent
1c950 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e 65  .** comment line
1c951 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68  s are used in th
1c952 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20  e generation of 
1c953 74 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c 20  the opcode.html 
1c954 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a  documentation.**
1c955 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 55 4d   file..**.** SUM
1c956 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  MARY:.**.**     
1c957 46 6f 72 6d 61 74 74 69 6e 67 20 69 73 20 69 6d  Formatting is im
1c958 70 6f 72 74 61 6e 74 20 74 6f 20 73 63 72 69 70  portant to scrip
1c959 74 73 20 74 68 61 74 20 73 63 61 6e 20 74 68 69  ts that scan thi
1c95a 73 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20 44  s file..**     D
1c95b 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72  o not deviate fr
1c95c 6f 6d 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e  om the formattin
1c95d 67 20 73 74 79 6c 65 20 63 75 72 72 65 6e 74 6c  g style currentl
1c95e 79 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 2a  y in use..**.***
1c95f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c961 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c962 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c963 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20  **********/../* 
1c964 4f 70 63 6f 64 65 3a 20 20 47 6f 74 6f 20 2a 20  Opcode:  Goto * 
1c965 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41  P2 * * *.**.** A
1c966 6e 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 20  n unconditional 
1c967 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20  jump to address 
1c968 50 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  P2..** The next 
1c969 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65 63  instruction exec
1c96a 75 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a 2a  uted will be .**
1c96b 20 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64 65   the one at inde
1c96c 78 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62 65  x P2 from the be
1c96d 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68  ginning of.** th
1c96e 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 61  e program..*/.ca
1c96f 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20 20  se OP_Goto: {   
1c970 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d            /* jum
1c971 70 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f 52  p */.  CHECK_FOR
1c972 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 63  _INTERRUPT;.  pc
1c973 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1c974 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c975 70 63 6f 64 65 3a 20 20 47 6f 73 75 62 20 50 31  pcode:  Gosub P1
1c976 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1c977 57 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e  Write the curren
1c978 74 20 61 64 64 72 65 73 73 20 6f 6e 74 6f 20 72  t address onto r
1c979 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 61 6e  egister P1.** an
1c97a 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 61  d then jump to a
1c97b 64 64 72 65 73 73 20 50 32 2e 0a 2a 2f 0a 63 61  ddress P2..*/.ca
1c97c 73 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 20  se OP_Gosub: {  
1c97d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d            /* jum
1c97e 70 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  p */.  assert( p
1c97f 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73  Op->p1>0 );.  as
1c980 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70  sert( pOp->p1<=p
1c981 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31  ->nMem );.  pIn1
1c982 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1c983 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1c984 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn1->flags & M
1c985 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20  EM_Dyn)==0 );.  
1c986 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pIn1->flags = ME
1c987 4d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e 75  M_Int;.  pIn1->u
1c988 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 53  .i = pc;.  REGIS
1c989 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1c98a 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d  1, pIn1);.  pc =
1c98b 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1c98c 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1c98d 6f 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31 20  ode:  Return P1 
1c98e 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75  * * * *.**.** Ju
1c98f 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69  mp to the next i
1c990 6e 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65 72  nstruction after
1c991 20 74 68 65 20 61 64 64 72 65 73 73 20 69 6e 20   the address in 
1c992 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a  register P1..*/.
1c993 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20  case OP_Return: 
1c994 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69  {           /* i
1c995 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  n1 */.  assert( 
1c996 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1c997 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 63 20 3d 20  M_Int );.  pc = 
1c998 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a  (int)pIn1->u.i;.
1c999 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c99a 70 63 6f 64 65 3a 20 20 59 69 65 6c 64 20 50 31  pcode:  Yield P1
1c99b 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53   * * * *.**.** S
1c99c 77 61 70 20 74 68 65 20 70 72 6f 67 72 61 6d 20  wap the program 
1c99d 63 6f 75 6e 74 65 72 20 77 69 74 68 20 74 68 65  counter with the
1c99e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1c99f 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f  er P1..*/.case O
1c9a0 50 5f 59 69 65 6c 64 3a 20 7b 20 20 20 20 20 20  P_Yield: {      
1c9a1 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a        /* in1 */.
1c9a2 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1c9a3 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1c9a4 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a 20 20 69  into u.aa */.  i
1c9a5 6e 74 20 70 63 44 65 73 74 3b 0a 23 65 6e 64 69  nt pcDest;.#endi
1c9a6 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1c9a7 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1c9a8 75 2e 61 61 20 2a 2f 0a 20 20 61 73 73 65 72 74  u.aa */.  assert
1c9a9 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1c9aa 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a   MEM_Dyn)==0 );.
1c9ab 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20    pIn1->flags = 
1c9ac 4d 45 4d 5f 49 6e 74 3b 0a 20 20 75 2e 61 61 2e  MEM_Int;.  u.aa.
1c9ad 70 63 44 65 73 74 20 3d 20 28 69 6e 74 29 70 49  pcDest = (int)pI
1c9ae 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 6e 31 2d  n1->u.i;.  pIn1-
1c9af 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47  >u.i = pc;.  REG
1c9b0 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1c9b1 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63  >p1, pIn1);.  pc
1c9b2 20 3d 20 75 2e 61 61 2e 70 63 44 65 73 74 3b 0a   = u.aa.pcDest;.
1c9b3 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c9b4 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 66 4e 75  pcode:  HaltIfNu
1c9b5 6c 6c 20 20 50 31 20 50 32 20 50 33 20 50 34 20  ll  P1 P2 P3 P4 
1c9b6 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  *.**.** Check th
1c9b7 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1c9b8 74 65 72 20 50 33 2e 20 20 49 66 20 69 73 20 69  ter P3.  If is i
1c9b9 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 61 6c 74  s NULL then Halt
1c9ba 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65   using.** parame
1c9bb 74 65 72 20 50 31 2c 20 50 32 2c 20 61 6e 64 20  ter P1, P2, and 
1c9bc 50 34 20 61 73 20 69 66 20 74 68 69 73 20 77 65  P4 as if this we
1c9bd 72 65 20 61 20 48 61 6c 74 20 69 6e 73 74 72 75  re a Halt instru
1c9be 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 0a 2a  ction.  If the.*
1c9bf 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  * value in regis
1c9c0 74 65 72 20 50 33 20 69 73 20 6e 6f 74 20 4e 55  ter P3 is not NU
1c9c1 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  LL, then this ro
1c9c2 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
1c9c3 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c  ..*/.case OP_Hal
1c9c4 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20  tIfNull: {      
1c9c5 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 69 66 28 20  /* in3 */.  if( 
1c9c6 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn3->flags & M
1c9c7 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 20 62 72  EM_Null)==0 ) br
1c9c8 65 61 6b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74  eak;.  /* Fall t
1c9c9 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f 48  hrough into OP_H
1c9ca 61 6c 74 20 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63  alt */.}../* Opc
1c9cb 6f 64 65 3a 20 20 48 61 6c 74 20 50 31 20 50 32  ode:  Halt P1 P2
1c9cc 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78   * P4 *.**.** Ex
1c9cd 69 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20  it immediately. 
1c9ce 20 41 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72   All open cursor
1c9cf 73 2c 20 65 74 63 20 61 72 65 20 63 6c 6f 73 65  s, etc are close
1c9d0 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c  d.** automatical
1c9d1 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  ly..**.** P1 is 
1c9d2 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 20  the result code 
1c9d3 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
1c9d4 74 65 33 5f 65 78 65 63 28 29 2c 20 73 71 6c 69  te3_exec(), sqli
1c9d5 74 65 33 5f 72 65 73 65 74 28 29 2c 0a 2a 2a 20  te3_reset(),.** 
1c9d6 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  or sqlite3_final
1c9d7 69 7a 65 28 29 2e 20 20 46 6f 72 20 61 20 6e 6f  ize().  For a no
1c9d8 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68 69 73 20  rmal halt, this 
1c9d9 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49 54 45  should be SQLITE
1c9da 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46 6f 72 20  _OK (0)..** For 
1c9db 65 72 72 6f 72 73 2c 20 69 74 20 63 61 6e 20 62  errors, it can b
1c9dc 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 76 61 6c  e some other val
1c9dd 75 65 2e 20 20 49 66 20 50 31 21 3d 30 20 74 68  ue.  If P1!=0 th
1c9de 65 6e 20 50 32 20 77 69 6c 6c 20 64 65 74 65 72  en P2 will deter
1c9df 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20  mine.** whether 
1c9e0 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c 6c 62 61  or not to rollba
1c9e1 63 6b 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  ck the current t
1c9e2 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 44 6f 20  ransaction.  Do 
1c9e3 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20  not rollback.** 
1c9e4 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20  if P2==OE_Fail. 
1c9e5 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  Do the rollback 
1c9e6 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61  if P2==OE_Rollba
1c9e7 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f 45 5f 41  ck.  If P2==OE_A
1c9e8 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e 20 62 61  bort,.** then ba
1c9e9 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68 61 6e 67  ck out all chang
1c9ea 65 73 20 74 68 61 74 20 68 61 76 65 20 6f 63 63  es that have occ
1c9eb 75 72 72 65 64 20 64 75 72 69 6e 67 20 74 68 69  urred during thi
1c9ec 73 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74  s execution of t
1c9ed 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62 75 74 20  he.** VDBE, but 
1c9ee 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20  do not rollback 
1c9ef 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
1c9f0 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73   .**.** If P4 is
1c9f1 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20 69   not null then i
1c9f2 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 6d 65  t is an error me
1c9f3 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2a  ssage string..**
1c9f4 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20  .** There is an 
1c9f5 69 6d 70 6c 69 65 64 20 22 48 61 6c 74 20 30 20  implied "Halt 0 
1c9f6 30 20 30 22 20 69 6e 73 74 72 75 63 74 69 6f 6e  0 0" instruction
1c9f7 20 69 6e 73 65 72 74 65 64 20 61 74 20 74 68 65   inserted at the
1c9f8 20 76 65 72 79 20 65 6e 64 20 6f 66 0a 2a 2a 20   very end of.** 
1c9f9 65 76 65 72 79 20 70 72 6f 67 72 61 6d 2e 20 20  every program.  
1c9fa 53 6f 20 61 20 6a 75 6d 70 20 70 61 73 74 20 74  So a jump past t
1c9fb 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74  he last instruct
1c9fc 69 6f 6e 20 6f 66 20 74 68 65 20 70 72 6f 67 72  ion of the progr
1c9fd 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d  am.** is the sam
1c9fe 65 20 61 73 20 65 78 65 63 75 74 69 6e 67 20 48  e as executing H
1c9ff 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  alt..*/.case OP_
1ca00 48 61 6c 74 3a 20 7b 0a 20 20 69 66 28 20 70 4f  Halt: {.  if( pO
1ca01 70 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 4f 4b  p->p1==SQLITE_OK
1ca02 20 26 26 20 70 2d 3e 70 46 72 61 6d 65 20 29 7b   && p->pFrame ){
1ca03 0a 20 20 20 20 2f 2a 20 48 61 6c 74 20 74 68 65  .    /* Halt the
1ca04 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 52 65   sub-program. Re
1ca05 74 75 72 6e 20 63 6f 6e 74 72 6f 6c 20 74 6f 20  turn control to 
1ca06 74 68 65 20 70 61 72 65 6e 74 20 66 72 61 6d 65  the parent frame
1ca07 2e 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61  . */.    VdbeFra
1ca08 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20 70 2d 3e  me *pFrame = p->
1ca09 70 46 72 61 6d 65 3b 0a 20 20 20 20 70 2d 3e 70  pFrame;.    p->p
1ca0a 46 72 61 6d 65 20 3d 20 70 46 72 61 6d 65 2d 3e  Frame = pFrame->
1ca0b 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 70 2d 3e  pParent;.    p->
1ca0c 6e 46 72 61 6d 65 2d 2d 3b 0a 20 20 20 20 73 71  nFrame--;.    sq
1ca0d 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
1ca0e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e  ges(db, p->nChan
1ca0f 67 65 29 3b 0a 20 20 20 20 70 63 20 3d 20 73 71  ge);.    pc = sq
1ca10 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 52 65  lite3VdbeFrameRe
1ca11 73 74 6f 72 65 28 70 46 72 61 6d 65 29 3b 0a 20  store(pFrame);. 
1ca12 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d     if( pOp->p2==
1ca13 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20  OE_Ignore ){.   
1ca14 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f     /* Instructio
1ca15 6e 20 70 63 20 69 73 20 74 68 65 20 4f 50 5f 50  n pc is the OP_P
1ca16 72 6f 67 72 61 6d 20 74 68 61 74 20 69 6e 76 6f  rogram that invo
1ca17 6b 65 64 20 74 68 65 20 73 75 62 2d 70 72 6f 67  ked the sub-prog
1ca18 72 61 6d 20 0a 20 20 20 20 20 20 2a 2a 20 63 75  ram .      ** cu
1ca19 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 68 61  rrently being ha
1ca1a 6c 74 65 64 2e 20 49 66 20 74 68 65 20 70 32 20  lted. If the p2 
1ca1b 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74  instruction of t
1ca1c 68 69 73 20 4f 50 5f 48 61 6c 74 0a 20 20 20 20  his OP_Halt.    
1ca1d 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e    ** instruction
1ca1e 20 69 73 20 73 65 74 20 74 6f 20 4f 45 5f 49 67   is set to OE_Ig
1ca1f 6e 6f 72 65 2c 20 74 68 65 6e 20 74 68 65 20 73  nore, then the s
1ca20 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 74 68  ub-program is th
1ca21 72 6f 77 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20  rowing.      ** 
1ca22 61 6e 20 49 47 4e 4f 52 45 20 65 78 63 65 70 74  an IGNORE except
1ca23 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ion. In this cas
1ca24 65 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 61 64  e jump to the ad
1ca25 64 72 65 73 73 20 73 70 65 63 69 66 69 65 64 0a  dress specified.
1ca26 20 20 20 20 20 20 2a 2a 20 61 73 20 74 68 65 20        ** as the 
1ca27 70 32 20 6f 66 20 74 68 65 20 63 61 6c 6c 69 6e  p2 of the callin
1ca28 67 20 4f 50 5f 50 72 6f 67 72 61 6d 2e 20 20 2a  g OP_Program.  *
1ca29 2f 0a 20 20 20 20 20 20 70 63 20 3d 20 70 2d 3e  /.      pc = p->
1ca2a 61 4f 70 5b 70 63 5d 2e 70 32 2d 31 3b 0a 20 20  aOp[pc].p2-1;.  
1ca2b 20 20 7d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20    }.    break;. 
1ca2c 20 7d 0a 0a 20 20 70 2d 3e 72 63 20 3d 20 70 4f   }..  p->rc = pO
1ca2d 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 65 72 72 6f  p->p1;.  p->erro
1ca2e 72 41 63 74 69 6f 6e 20 3d 20 28 75 38 29 70 4f  rAction = (u8)pO
1ca2f 70 2d 3e 70 32 3b 0a 20 20 70 2d 3e 70 63 20 3d  p->p2;.  p->pc =
1ca30 20 70 63 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e   pc;.  if( pOp->
1ca31 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69  p4.z ){.    sqli
1ca32 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1ca33 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  >zErrMsg, db, "%
1ca34 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a  s", pOp->p4.z);.
1ca35 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
1ca36 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20  e3VdbeHalt(p);. 
1ca37 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1ca38 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d  ITE_BUSY || rc==
1ca39 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d  SQLITE_OK || rc=
1ca3a 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b  =SQLITE_ERROR );
1ca3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1ca3c 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 70 2d  E_BUSY ){.    p-
1ca3d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54  >rc = rc = SQLIT
1ca3e 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b  E_BUSY;.  }else{
1ca3f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
1ca40 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d  =SQLITE_OK || p-
1ca41 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53  >rc==SQLITE_CONS
1ca42 54 52 41 49 4e 54 20 29 3b 0a 20 20 20 20 61 73  TRAINT );.    as
1ca43 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
1ca44 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6e 44 65 66 65  _OK || db->nDefe
1ca45 72 72 65 64 43 6f 6e 73 3e 30 20 29 3b 0a 20 20  rredCons>0 );.  
1ca46 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20 53    rc = p->rc ? S
1ca47 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 51  QLITE_ERROR : SQ
1ca48 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20  LITE_DONE;.  }. 
1ca49 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72   goto vdbe_retur
1ca4a 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  n;.}../* Opcode:
1ca4b 20 49 6e 74 65 67 65 72 20 50 31 20 50 32 20 2a   Integer P1 P2 *
1ca4c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33   * *.**.** The 3
1ca4d 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61  2-bit integer va
1ca4e 6c 75 65 20 50 31 20 69 73 20 77 72 69 74 74 65  lue P1 is writte
1ca4f 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  n into register 
1ca50 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  P2..*/.case OP_I
1ca51 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20 20  nteger: {       
1ca52 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1ca53 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e  ease */.  pOut->
1ca54 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1ca55 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70  .  pOut->u.i = p
1ca56 4f 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61 6b 3b  Op->p1;.  break;
1ca57 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
1ca58 6e 74 36 34 20 2a 20 50 32 20 2a 20 50 34 20 2a  nt64 * P2 * P4 *
1ca59 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  .**.** P4 is a p
1ca5a 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d 62  ointer to a 64-b
1ca5b 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  it integer value
1ca5c 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20  ..** Write that 
1ca5d 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73  value into regis
1ca5e 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20  ter P2..*/.case 
1ca5f 4f 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20 20 20  OP_Int64: {     
1ca60 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1ca61 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73  erelease */.  as
1ca62 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 49  sert( pOp->p4.pI
1ca63 36 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d  64!=0 );.  pOut-
1ca64 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1ca65 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  ;.  pOut->u.i = 
1ca66 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20  *pOp->p4.pI64;. 
1ca67 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1ca68 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20  code: Real * P2 
1ca69 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1ca6a 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1ca6b 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e  a 64-bit floatin
1ca6c 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a  g point value..*
1ca6d 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c  * Write that val
1ca6e 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  ue into register
1ca6f 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P2..*/.case OP_
1ca70 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20  Real: {         
1ca71 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1ca72 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65  _FLOAT, out2-pre
1ca73 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75  release */.  pOu
1ca74 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52  t->flags = MEM_R
1ca75 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21  eal;.  assert( !
1ca76 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f  sqlite3IsNaN(*pO
1ca77 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a  p->p4.pReal) );.
1ca78 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70    pOut->r = *pOp
1ca79 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72  ->p4.pReal;.  br
1ca7a 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1ca7b 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20  e: String8 * P2 
1ca7c 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1ca7d 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20  points to a nul 
1ca7e 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
1ca7f 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70   string. This op
1ca80 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72  code is transfor
1ca81 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20  med .** into an 
1ca82 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65  OP_String before
1ca83 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20   it is executed 
1ca84 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69  for the first ti
1ca85 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  me..*/.case OP_S
1ca86 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20  tring8: {       
1ca87 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1ca88 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65  STRING, out2-pre
1ca89 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73  release */.  ass
1ca8a 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d  ert( pOp->p4.z!=
1ca8b 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f  0 );.  pOp->opco
1ca8c 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a  de = OP_String;.
1ca8d 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 6c 69    pOp->p1 = sqli
1ca8e 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4f 70 2d  te3Strlen30(pOp-
1ca8f 3e 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66  >p4.z);..#ifndef
1ca90 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1ca91 31 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 6e  16.  if( encodin
1ca92 67 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  g!=SQLITE_UTF8 )
1ca93 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1ca94 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
1ca95 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c  pOut, pOp->p4.z,
1ca96 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38   -1, SQLITE_UTF8
1ca97 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
1ca98 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1ca99 4c 49 54 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f  LITE_TOOBIG ) go
1ca9a 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20  to too_big;.    
1ca9b 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
1ca9c 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1ca9d 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65  Encoding(pOut, e
1ca9e 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f 20  ncoding) ) goto 
1ca9f 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 61 73 73 65  no_mem;.    asse
1caa0 72 74 28 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f  rt( pOut->zMallo
1caa1 63 3d 3d 70 4f 75 74 2d 3e 7a 20 29 3b 0a 20 20  c==pOut->z );.  
1caa2 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e    assert( pOut->
1caa3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20  flags & MEM_Dyn 
1caa4 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61  );.    pOut->zMa
1caa5 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f  lloc = 0;.    pO
1caa6 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  ut->flags |= MEM
1caa7 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75  _Static;.    pOu
1caa8 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d  t->flags &= ~MEM
1caa9 5f 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f  _Dyn;.    if( pO
1caaa 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59  p->p4type==P4_DY
1caab 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73  NAMIC ){.      s
1caac 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1caad 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20   pOp->p4.z);.   
1caae 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79   }.    pOp->p4ty
1caaf 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b  pe = P4_DYNAMIC;
1cab0 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d  .    pOp->p4.z =
1cab1 20 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f   pOut->z;.    pO
1cab2 70 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b  p->p1 = pOut->n;
1cab3 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66  .  }.#endif.  if
1cab4 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c  ( pOp->p1>db->aL
1cab5 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1cab6 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
1cab7 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
1cab8 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72   }.  /* Fall thr
1cab9 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74  ough to the next
1caba 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67   case, OP_String
1cabb 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f   */.}.  ./* Opco
1cabc 64 65 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32  de: String P1 P2
1cabd 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68   * P4 *.**.** Th
1cabe 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50  e string value P
1cabf 34 20 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28  4 of length P1 (
1cac0 62 79 74 65 73 29 20 69 73 20 73 74 6f 72 65 64  bytes) is stored
1cac1 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
1cac2 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69  .*/.case OP_Stri
1cac3 6e 67 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f  ng: {          /
1cac4 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1cac5 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  e */.  assert( p
1cac6 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20  Op->p4.z!=0 );. 
1cac7 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
1cac8 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69  EM_Str|MEM_Stati
1cac9 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f  c|MEM_Term;.  pO
1caca 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e  ut->z = pOp->p4.
1cacb 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70  z;.  pOut->n = p
1cacc 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e  Op->p1;.  pOut->
1cacd 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a  enc = encoding;.
1cace 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1cacf 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62  BSIZE(pOut);.  b
1cad0 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cad1 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20  de: Null * P2 * 
1cad2 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  * *.**.** Write 
1cad3 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69  a NULL into regi
1cad4 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  ster P2..*/.case
1cad5 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20   OP_Null: {     
1cad6 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1cad7 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 62 72  erelease */.  br
1cad8 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f  eak;.}.../* Opco
1cad9 64 65 3a 20 42 6c 6f 62 20 50 31 20 50 32 20 2a  de: Blob P1 P2 *
1cada 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69   P4.**.** P4 poi
1cadb 6e 74 73 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66  nts to a blob of
1cadc 20 64 61 74 61 20 50 31 20 62 79 74 65 73 20 6c   data P1 bytes l
1cadd 6f 6e 67 2e 20 20 53 74 6f 72 65 20 74 68 69 73  ong.  Store this
1cade 0a 2a 2a 20 62 6c 6f 62 20 69 6e 20 72 65 67 69  .** blob in regi
1cadf 73 74 65 72 20 50 32 2e 20 54 68 69 73 20 69 6e  ster P2. This in
1cae0 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6e 6f 74  struction is not
1cae1 20 63 6f 64 65 64 20 64 69 72 65 63 74 6c 79 0a   coded directly.
1cae2 2a 2a 20 62 79 20 74 68 65 20 63 6f 6d 70 69 6c  ** by the compil
1cae3 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65  er. Instead, the
1cae4 20 63 6f 6d 70 69 6c 65 72 20 6c 61 79 65 72 20   compiler layer 
1cae5 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6e 20  specifies.** an 
1cae6 4f 50 5f 48 65 78 42 6c 6f 62 20 6f 70 63 6f 64  OP_HexBlob opcod
1cae7 65 2c 20 77 69 74 68 20 74 68 65 20 68 65 78 20  e, with the hex 
1cae8 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74  string represent
1cae9 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20  ation of.** the 
1caea 62 6c 6f 62 20 61 73 20 50 34 2e 20 54 68 69 73  blob as P4. This
1caeb 20 6f 70 63 6f 64 65 20 69 73 20 74 72 61 6e 73   opcode is trans
1caec 66 6f 72 6d 65 64 20 74 6f 20 61 6e 20 4f 50 5f  formed to an OP_
1caed 42 6c 6f 62 0a 2a 2a 20 74 68 65 20 66 69 72 73  Blob.** the firs
1caee 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 78 65  t time it is exe
1caef 63 75 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f  cuted..*/.case O
1caf0 50 5f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20  P_Blob: {       
1caf1 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
1caf2 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20  -prerelease */. 
1caf3 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1caf4 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c   <= SQLITE_MAX_L
1caf5 45 4e 47 54 48 20 29 3b 0a 20 20 73 71 6c 69 74  ENGTH );.  sqlit
1caf6 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
1caf7 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c  pOut, pOp->p4.z,
1caf8 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30 29 3b   pOp->p1, 0, 0);
1caf9 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65  .  pOut->enc = e
1cafa 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54  ncoding;.  UPDAT
1cafb 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
1cafc 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
1cafd 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 72  ../* Opcode: Var
1cafe 69 61 62 6c 65 20 50 31 20 50 32 20 50 33 20 50  iable P1 P2 P3 P
1caff 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 66  4 *.**.** Transf
1cb00 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  er the values of
1cb01 20 62 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72   bound parameter
1cb02 73 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 69 6e  s P1..P1+P3-1 in
1cb03 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20  to registers.** 
1cb04 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 0a 2a 2a 0a  P2..P2+P3-1..**.
1cb05 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65  ** If the parame
1cb06 74 65 72 20 69 73 20 6e 61 6d 65 64 2c 20 74 68  ter is named, th
1cb07 65 6e 20 69 74 73 20 6e 61 6d 65 20 61 70 70 65  en its name appe
1cb08 61 72 73 20 69 6e 20 50 34 20 61 6e 64 20 50 33  ars in P4 and P3
1cb09 3d 3d 31 2e 0a 2a 2a 20 54 68 65 20 50 34 20 76  ==1..** The P4 v
1cb0a 61 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20  alue is used by 
1cb0b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
1cb0c 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 2e 0a 2a  ameter_name()..*
1cb0d 2f 0a 63 61 73 65 20 4f 50 5f 56 61 72 69 61 62  /.case OP_Variab
1cb0e 6c 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  le: {.#if 0  /* 
1cb0f 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1cb10 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 62 20  moved into u.ab 
1cb11 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20  */.  int p1;    
1cb12 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c        /* Variabl
1cb13 65 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a  e to copy from *
1cb14 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20  /.  int p2;     
1cb15 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
1cb16 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20   to copy to */. 
1cb17 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
1cb18 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76    /* Number of v
1cb19 61 6c 75 65 73 20 6c 65 66 74 20 74 6f 20 63 6f  alues left to co
1cb1a 70 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 56 61  py */.  Mem *pVa
1cb1b 72 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75  r;       /* Valu
1cb1c 65 20 62 65 69 6e 67 20 74 72 61 6e 73 66 65 72  e being transfer
1cb1d 72 65 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  red */.#endif /*
1cb1e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1cb1f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 62   moved into u.ab
1cb20 20 2a 2f 0a 0a 20 20 75 2e 61 62 2e 70 31 20 3d   */..  u.ab.p1 =
1cb21 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0a 20 20   pOp->p1 - 1;.  
1cb22 75 2e 61 62 2e 70 32 20 3d 20 70 4f 70 2d 3e 70  u.ab.p2 = pOp->p
1cb23 32 3b 0a 20 20 75 2e 61 62 2e 6e 20 3d 20 70 4f  2;.  u.ab.n = pO
1cb24 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28  p->p3;.  assert(
1cb25 20 75 2e 61 62 2e 70 31 3e 3d 30 20 26 26 20 75   u.ab.p1>=0 && u
1cb26 2e 61 62 2e 70 31 2b 75 2e 61 62 2e 6e 3c 3d 70  .ab.p1+u.ab.n<=p
1cb27 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 61 73 73 65  ->nVar );.  asse
1cb28 72 74 28 20 75 2e 61 62 2e 70 32 3e 3d 31 20 26  rt( u.ab.p2>=1 &
1cb29 26 20 75 2e 61 62 2e 70 32 2b 75 2e 61 62 2e 6e  & u.ab.p2+u.ab.n
1cb2a 2d 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  -1<=p->nMem );. 
1cb2b 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1cb2c 2e 7a 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 33  .z==0 || pOp->p3
1cb2d 3d 3d 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d  ==1 || pOp->p3==
1cb2e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 75  0 );..  while( u
1cb2f 2e 61 62 2e 6e 2d 2d 20 3e 20 30 20 29 7b 0a 20  .ab.n-- > 0 ){. 
1cb30 20 20 20 75 2e 61 62 2e 70 56 61 72 20 3d 20 26     u.ab.pVar = &
1cb31 70 2d 3e 61 56 61 72 5b 75 2e 61 62 2e 70 31 2b  p->aVar[u.ab.p1+
1cb32 2b 5d 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  +];.    if( sqli
1cb33 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
1cb34 28 75 2e 61 62 2e 70 56 61 72 29 20 29 7b 0a 20  (u.ab.pVar) ){. 
1cb35 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69       goto too_bi
1cb36 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75  g;.    }.    pOu
1cb37 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61  t = &p->aMem[u.a
1cb38 62 2e 70 32 2b 2b 5d 3b 0a 20 20 20 20 73 71 6c  b.p2++];.    sql
1cb39 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1cb3a 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 74 29  seExternal(pOut)
1cb3b 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67  ;.    pOut->flag
1cb3c 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1cb3d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cb3e 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74  ShallowCopy(pOut
1cb3f 2c 20 75 2e 61 62 2e 70 56 61 72 2c 20 4d 45 4d  , u.ab.pVar, MEM
1cb40 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 55 50  _Static);.    UP
1cb41 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1cb42 45 28 70 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 62  E(pOut);.  }.  b
1cb43 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cb44 64 65 3a 20 4d 6f 76 65 20 50 31 20 50 32 20 50  de: Move P1 P2 P
1cb45 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65  3 * *.**.** Move
1cb46 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72   the values in r
1cb47 65 67 69 73 74 65 72 20 50 31 2e 2e 50 31 2b 50  egister P1..P1+P
1cb48 33 2d 31 20 6f 76 65 72 20 69 6e 74 6f 0a 2a 2a  3-1 over into.**
1cb49 20 72 65 67 69 73 74 65 72 73 20 50 32 2e 2e 50   registers P2..P
1cb4a 32 2b 50 33 2d 31 2e 20 20 52 65 67 69 73 74 65  2+P3-1.  Registe
1cb4b 72 73 20 50 31 2e 2e 50 31 2b 50 31 2d 31 20 61  rs P1..P1+P1-1 a
1cb4c 72 65 0a 2a 2a 20 6c 65 66 74 20 68 6f 6c 64 69  re.** left holdi
1cb4d 6e 67 20 61 20 4e 55 4c 4c 2e 20 20 49 74 20 69  ng a NULL.  It i
1cb4e 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20 72  s an error for r
1cb4f 65 67 69 73 74 65 72 20 72 61 6e 67 65 73 0a 2a  egister ranges.*
1cb50 2a 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 61 6e  * P1..P1+P3-1 an
1cb51 64 20 50 32 2e 2e 50 32 2b 50 33 2d 31 20 74 6f  d P2..P2+P3-1 to
1cb52 20 6f 76 65 72 6c 61 70 2e 0a 2a 2f 0a 63 61 73   overlap..*/.cas
1cb53 65 20 4f 50 5f 4d 6f 76 65 3a 20 7b 0a 23 69 66  e OP_Move: {.#if
1cb54 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1cb55 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1cb56 6f 20 75 2e 61 63 20 2a 2f 0a 20 20 63 68 61 72  o u.ac */.  char
1cb57 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a 20   *zMalloc;   /* 
1cb58 48 6f 6c 64 69 6e 67 20 76 61 72 69 61 62 6c 65  Holding variable
1cb59 20 66 6f 72 20 61 6c 6c 6f 63 61 74 65 64 20 6d   for allocated m
1cb5a 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6e  emory */.  int n
1cb5b 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1cb5c 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65  umber of registe
1cb5d 72 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20  rs left to copy 
1cb5e 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20  */.  int p1;    
1cb5f 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
1cb60 72 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a  r to copy from *
1cb61 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20  /.  int p2;     
1cb62 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
1cb63 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 23   to copy to */.#
1cb64 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1cb65 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1cb66 6e 74 6f 20 75 2e 61 63 20 2a 2f 0a 0a 20 20 75  nto u.ac */..  u
1cb67 2e 61 63 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b  .ac.n = pOp->p3;
1cb68 0a 20 20 75 2e 61 63 2e 70 31 20 3d 20 70 4f 70  .  u.ac.p1 = pOp
1cb69 2d 3e 70 31 3b 0a 20 20 75 2e 61 63 2e 70 32 20  ->p1;.  u.ac.p2 
1cb6a 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73  = pOp->p2;.  ass
1cb6b 65 72 74 28 20 75 2e 61 63 2e 6e 3e 30 20 26 26  ert( u.ac.n>0 &&
1cb6c 20 75 2e 61 63 2e 70 31 3e 30 20 26 26 20 75 2e   u.ac.p1>0 && u.
1cb6d 61 63 2e 70 32 3e 30 20 29 3b 0a 20 20 61 73 73  ac.p2>0 );.  ass
1cb6e 65 72 74 28 20 75 2e 61 63 2e 70 31 2b 75 2e 61  ert( u.ac.p1+u.a
1cb6f 63 2e 6e 3c 3d 75 2e 61 63 2e 70 32 20 7c 7c 20  c.n<=u.ac.p2 || 
1cb70 75 2e 61 63 2e 70 32 2b 75 2e 61 63 2e 6e 3c 3d  u.ac.p2+u.ac.n<=
1cb71 75 2e 61 63 2e 70 31 20 29 3b 0a 0a 20 20 70 49  u.ac.p1 );..  pI
1cb72 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e  n1 = &p->aMem[u.
1cb73 61 63 2e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d  ac.p1];.  pOut =
1cb74 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 63 2e 70   &p->aMem[u.ac.p
1cb75 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 75 2e 61  2];.  while( u.a
1cb76 63 2e 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73 73  c.n-- ){.    ass
1cb77 65 72 74 28 20 70 4f 75 74 3c 3d 26 70 2d 3e 61  ert( pOut<=&p->a
1cb78 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a  Mem[p->nMem] );.
1cb79 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 31      assert( pIn1
1cb7a 3c 3d 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d  <=&p->aMem[p->nM
1cb7b 65 6d 5d 20 29 3b 0a 20 20 20 20 75 2e 61 63 2e  em] );.    u.ac.
1cb7c 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4f 75 74 2d 3e  zMalloc = pOut->
1cb7d 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 70 4f 75  zMalloc;.    pOu
1cb7e 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a  t->zMalloc = 0;.
1cb7f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1cb80 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 70 49 6e  emMove(pOut, pIn
1cb81 31 29 3b 0a 20 20 20 20 70 49 6e 31 2d 3e 7a 4d  1);.    pIn1->zM
1cb82 61 6c 6c 6f 63 20 3d 20 75 2e 61 63 2e 7a 4d 61  alloc = u.ac.zMa
1cb83 6c 6c 6f 63 3b 0a 20 20 20 20 52 45 47 49 53 54  lloc;.    REGIST
1cb84 45 52 5f 54 52 41 43 45 28 75 2e 61 63 2e 70 32  ER_TRACE(u.ac.p2
1cb85 2b 2b 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 70  ++, pOut);.    p
1cb86 49 6e 31 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 2b  In1++;.    pOut+
1cb87 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  +;.  }.  break;.
1cb88 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f  }../* Opcode: Co
1cb89 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  py P1 P2 * * *.*
1cb8a 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79  *.** Make a copy
1cb8b 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20   of register P1 
1cb8c 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  into register P2
1cb8d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73  ..**.** This ins
1cb8e 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61  truction makes a
1cb8f 20 64 65 65 70 20 63 6f 70 79 20 6f 66 20 74 68   deep copy of th
1cb90 65 20 76 61 6c 75 65 2e 20 20 41 20 64 75 70 6c  e value.  A dupl
1cb91 69 63 61 74 65 0a 2a 2a 20 69 73 20 6d 61 64 65  icate.** is made
1cb92 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f   of any string o
1cb93 72 20 62 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 2e  r blob constant.
1cb94 20 20 53 65 65 20 61 6c 73 6f 20 4f 50 5f 53 43    See also OP_SC
1cb95 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  opy..*/.case OP_
1cb96 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20  Copy: {         
1cb97 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20      /* in1 */.  
1cb98 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e  assert( pOp->p2>
1cb99 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1cb9a 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p2<=p->nMem 
1cb9b 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e  );.  pOut = &p->
1cb9c 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1cb9d 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70   assert( pOut!=p
1cb9e 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  In1 );.  sqlite3
1cb9f 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
1cba0 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d  py(pOut, pIn1, M
1cba1 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65 65  EM_Ephem);.  Dee
1cba2 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74  phemeralize(pOut
1cba3 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  );.  REGISTER_TR
1cba4 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75  ACE(pOp->p2, pOu
1cba5 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
1cba6 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79  /* Opcode: SCopy
1cba7 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1cba8 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f  ** Make a shallo
1cba9 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74  w copy of regist
1cbaa 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73  er P1 into regis
1cbab 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68  ter P2..**.** Th
1cbac 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d  is instruction m
1cbad 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 20 63  akes a shallow c
1cbae 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65  opy of the value
1cbaf 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a  .  If the value.
1cbb0 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f  ** is a string o
1cbb1 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65  r blob, then the
1cbb2 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61 20   copy is only a 
1cbb3 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a  pointer to the.*
1cbb4 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 68  * original and h
1cbb5 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72 69 67  ence if the orig
1cbb6 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 6f 20  inal changes so 
1cbb7 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a  will the copy..*
1cbb8 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68 65 20  * Worse, if the 
1cbb9 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65 61 6c  original is deal
1cbba 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63 6f 70  located, the cop
1cbbb 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69  y becomes invali
1cbbc 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 70  d..** Thus the p
1cbbd 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 61 72  rogram must guar
1cbbe 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 6f  antee that the o
1cbbf 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74  riginal will not
1cbc0 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e   change.** durin
1cbc1 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f  g the lifetime o
1cbc2 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 73 65  f the copy.  Use
1cbc3 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65   OP_Copy to make
1cbc4 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63   a complete.** c
1cbc5 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  opy..*/.case OP_
1cbc6 53 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20  SCopy: {        
1cbc7 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20      /* in1 */.  
1cbc8 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1cbc9 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20  Op->p1, pIn1);. 
1cbca 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1cbcb 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
1cbcc 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p2<=p->nMem
1cbcd 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d   );.  pOut = &p-
1cbce 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  >aMem[pOp->p2];.
1cbcf 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d    assert( pOut!=
1cbd0 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65  pIn1 );.  sqlite
1cbd1 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43  3VdbeMemShallowC
1cbd2 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20  opy(pOut, pIn1, 
1cbd3 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 52 45  MEM_Ephem);.  RE
1cbd4 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1cbd5 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62  ->p2, pOut);.  b
1cbd6 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cbd7 64 65 3a 20 52 65 73 75 6c 74 52 6f 77 20 50 31  de: ResultRow P1
1cbd8 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1cbd9 54 68 65 20 72 65 67 69 73 74 65 72 73 20 50 31  The registers P1
1cbda 20 74 68 72 6f 75 67 68 20 50 31 2b 50 32 2d 31   through P1+P2-1
1cbdb 20 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c   contain a singl
1cbdc 65 20 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75  e row of.** resu
1cbdd 6c 74 73 2e 20 54 68 69 73 20 6f 70 63 6f 64 65  lts. This opcode
1cbde 20 63 61 75 73 65 73 20 74 68 65 20 73 71 6c 69   causes the sqli
1cbdf 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 20  te3_step() call 
1cbe0 74 6f 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20  to terminate.** 
1cbe1 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 52  with an SQLITE_R
1cbe2 4f 57 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61  OW return code a
1cbe3 6e 64 20 69 74 20 73 65 74 73 20 75 70 20 74 68  nd it sets up th
1cbe4 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a  e sqlite3_stmt.*
1cbe5 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 70  * structure to p
1cbe6 72 6f 76 69 64 65 20 61 63 63 65 73 73 20 74 6f  rovide access to
1cbe7 20 74 68 65 20 74 6f 70 20 50 31 20 76 61 6c 75   the top P1 valu
1cbe8 65 73 20 61 73 20 74 68 65 20 72 65 73 75 6c 74  es as the result
1cbe9 0a 2a 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65  .** row..*/.case
1cbea 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b   OP_ResultRow: {
1cbeb 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1cbec 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1cbed 20 69 6e 74 6f 20 75 2e 61 64 20 2a 2f 0a 20 20   into u.ad */.  
1cbee 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74  Mem *pMem;.  int
1cbef 20 69 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f   i;.#endif /* lo
1cbf0 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1cbf1 76 65 64 20 69 6e 74 6f 20 75 2e 61 64 20 2a 2f  ved into u.ad */
1cbf2 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52  .  assert( p->nR
1cbf3 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e 70  esColumn==pOp->p
1cbf4 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  2 );.  assert( p
1cbf5 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73  Op->p1>0 );.  as
1cbf6 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 2b 70 4f  sert( pOp->p1+pO
1cbf7 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31  p->p2<=p->nMem+1
1cbf8 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69   );..  /* If thi
1cbf9 73 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  s statement has 
1cbfa 76 69 6f 6c 61 74 65 64 20 69 6d 6d 65 64 69 61  violated immedia
1cbfb 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  te foreign key c
1cbfc 6f 6e 73 74 72 61 69 6e 74 73 2c 20 64 6f 0a 20  onstraints, do. 
1cbfd 20 2a 2a 20 6e 6f 74 20 72 65 74 75 72 6e 20 74   ** not return t
1cbfe 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
1cbff 73 20 6d 6f 64 69 66 69 65 64 2e 20 41 6e 64 20  s modified. And 
1cc00 64 6f 20 6e 6f 74 20 52 45 4c 45 41 53 45 20 74  do not RELEASE t
1cc01 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a  he statement.  *
1cc02 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49  * transaction. I
1cc03 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f  t needs to be ro
1cc04 6c 6c 65 64 20 62 61 63 6b 2e 20 20 2a 2f 0a 20  lled back.  */. 
1cc05 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
1cc06 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62  (rc = sqlite3Vdb
1cc07 65 43 68 65 63 6b 46 6b 28 70 2c 20 30 29 29 20  eCheckFk(p, 0)) 
1cc08 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  ){.    assert( d
1cc09 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
1cc0a 43 6f 75 6e 74 52 6f 77 73 20 29 3b 0a 20 20 20  CountRows );.   
1cc0b 20 61 73 73 65 72 74 28 20 70 2d 3e 75 73 65 73   assert( p->uses
1cc0c 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20  StmtJournal );. 
1cc0d 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20     break;.  }.. 
1cc0e 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54   /* If the SQLIT
1cc0f 45 5f 43 6f 75 6e 74 52 6f 77 73 20 66 6c 61 67  E_CountRows flag
1cc10 20 69 73 20 73 65 74 20 69 6e 20 73 71 6c 69 74   is set in sqlit
1cc11 65 33 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74  e3.flags mask, t
1cc12 68 65 6e 0a 20 20 2a 2a 20 44 4d 4c 20 73 74 61  hen.  ** DML sta
1cc13 74 65 6d 65 6e 74 73 20 69 6e 76 6f 6b 65 20 74  tements invoke t
1cc14 68 69 73 20 6f 70 63 6f 64 65 20 74 6f 20 72 65  his opcode to re
1cc15 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
1cc16 6f 66 20 72 6f 77 73 0a 20 20 2a 2a 20 6d 6f 64  of rows.  ** mod
1cc17 69 66 69 65 64 20 74 6f 20 74 68 65 20 75 73 65  ified to the use
1cc18 72 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 6f  r. This is the o
1cc19 6e 6c 79 20 77 61 79 20 74 68 61 74 20 61 20 56  nly way that a V
1cc1a 4d 20 74 68 61 74 0a 20 20 2a 2a 20 6f 70 65 6e  M that.  ** open
1cc1b 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72  s a statement tr
1cc1c 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 69 6e  ansaction may in
1cc1d 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 65  voke this opcode
1cc1e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 63  ..  **.  ** In c
1cc1f 61 73 65 20 74 68 69 73 20 69 73 20 73 75 63 68  ase this is such
1cc20 20 61 20 73 74 61 74 65 6d 65 6e 74 2c 20 63 6c   a statement, cl
1cc21 6f 73 65 20 61 6e 79 20 73 74 61 74 65 6d 65 6e  ose any statemen
1cc22 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  t transaction.  
1cc23 2a 2a 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69  ** opened by thi
1cc24 73 20 56 4d 20 62 65 66 6f 72 65 20 72 65 74 75  s VM before retu
1cc25 72 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f  rning control to
1cc26 20 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20   the user. This 
1cc27 69 73 20 74 6f 0a 20 20 2a 2a 20 65 6e 73 75 72  is to.  ** ensur
1cc28 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74  e that statement
1cc29 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72  -transactions ar
1cc2a 65 20 61 6c 77 61 79 73 20 6e 65 73 74 65 64 2c  e always nested,
1cc2b 20 6e 6f 74 20 6f 76 65 72 6c 61 70 70 69 6e 67   not overlapping
1cc2c 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70  ..  ** If the op
1cc2d 65 6e 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61  en statement-tra
1cc2e 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  nsaction is not 
1cc2f 63 6c 6f 73 65 64 20 68 65 72 65 2c 20 74 68 65  closed here, the
1cc30 6e 20 74 68 65 20 75 73 65 72 0a 20 20 2a 2a 20  n the user.  ** 
1cc31 6d 61 79 20 73 74 65 70 20 61 6e 6f 74 68 65 72  may step another
1cc32 20 56 4d 20 74 68 61 74 20 6f 70 65 6e 73 20 69   VM that opens i
1cc33 74 73 20 6f 77 6e 20 73 74 61 74 65 6d 65 6e 74  ts own statement
1cc34 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68   transaction. Th
1cc35 69 73 0a 20 20 2a 2a 20 6d 61 79 20 6c 65 61 64  is.  ** may lead
1cc36 20 74 6f 20 6f 76 65 72 6c 61 70 70 69 6e 67 20   to overlapping 
1cc37 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1cc38 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a  ctions..  **.  *
1cc39 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * The statement 
1cc3a 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e  transaction is n
1cc3b 65 76 65 72 20 61 20 74 6f 70 2d 6c 65 76 65 6c  ever a top-level
1cc3c 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 48   transaction.  H
1cc3d 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 52 45  ence.  ** the RE
1cc3e 4c 45 41 53 45 20 63 61 6c 6c 20 62 65 6c 6f 77  LEASE call below
1cc3f 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e   can never fail.
1cc40 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
1cc41 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30  p->iStatement==0
1cc42 20 7c 7c 20 64 62 2d 3e 66 6c 61 67 73 26 53 51   || db->flags&SQ
1cc43 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29  LITE_CountRows )
1cc44 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1cc45 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65  VdbeCloseStateme
1cc46 6e 74 28 70 2c 20 53 41 56 45 50 4f 49 4e 54 5f  nt(p, SAVEPOINT_
1cc47 52 45 4c 45 41 53 45 29 3b 0a 20 20 69 66 28 20  RELEASE);.  if( 
1cc48 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45  NEVER(rc!=SQLITE
1cc49 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 62 72 65 61  _OK) ){.    brea
1cc4a 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 76  k;.  }..  /* Inv
1cc4b 61 6c 69 64 61 74 65 20 61 6c 6c 20 65 70 68 65  alidate all ephe
1cc4c 6d 65 72 61 6c 20 63 75 72 73 6f 72 20 72 6f 77  meral cursor row
1cc4d 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 70 2d 3e   caches */.  p->
1cc4e 63 61 63 68 65 43 74 72 20 3d 20 28 70 2d 3e 63  cacheCtr = (p->c
1cc4f 61 63 68 65 43 74 72 20 2b 20 32 29 7c 31 3b 0a  acheCtr + 2)|1;.
1cc50 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
1cc51 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74  the results of t
1cc52 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 61  he current row a
1cc53 72 65 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74  re \000 terminat
1cc54 65 64 0a 20 20 2a 2a 20 61 6e 64 20 68 61 76 65  ed.  ** and have
1cc55 20 61 6e 20 61 73 73 69 67 6e 65 64 20 74 79 70   an assigned typ
1cc56 65 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20  e.  The results 
1cc57 61 72 65 20 64 65 2d 65 70 68 65 6d 65 72 61 6c  are de-ephemeral
1cc58 69 7a 65 64 20 61 73 0a 20 20 2a 2a 20 61 73 20  ized as.  ** as 
1cc59 73 69 64 65 20 65 66 66 65 63 74 2e 0a 20 20 2a  side effect..  *
1cc5a 2f 0a 20 20 75 2e 61 64 2e 70 4d 65 6d 20 3d 20  /.  u.ad.pMem = 
1cc5b 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20  p->pResultSet = 
1cc5c 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
1cc5d 5d 3b 0a 20 20 66 6f 72 28 75 2e 61 64 2e 69 3d  ];.  for(u.ad.i=
1cc5e 30 3b 20 75 2e 61 64 2e 69 3c 70 4f 70 2d 3e 70  0; u.ad.i<pOp->p
1cc5f 32 3b 20 75 2e 61 64 2e 69 2b 2b 29 7b 0a 20 20  2; u.ad.i++){.  
1cc60 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cc61 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 26 75 2e  NulTerminate(&u.
1cc62 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29  ad.pMem[u.ad.i])
1cc63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1cc64 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 26 75  eMemStoreType(&u
1cc65 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d  .ad.pMem[u.ad.i]
1cc66 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f  );.    REGISTER_
1cc67 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 75 2e  TRACE(pOp->p1+u.
1cc68 61 64 2e 69 2c 20 26 75 2e 61 64 2e 70 4d 65 6d  ad.i, &u.ad.pMem
1cc69 5b 75 2e 61 64 2e 69 5d 29 3b 0a 20 20 7d 0a 20  [u.ad.i]);.  }. 
1cc6a 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1cc6b 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f  ailed ) goto no_
1cc6c 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72  mem;..  /* Retur
1cc6d 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20 20 2a  n SQLITE_ROW.  *
1cc6e 2f 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 20 2b  /.  p->pc = pc +
1cc6f 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54   1;.  rc = SQLIT
1cc70 45 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20 76 64  E_ROW;.  goto vd
1cc71 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  be_return;.}../*
1cc72 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61 74 20   Opcode: Concat 
1cc73 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1cc74 2a 2a 20 41 64 64 20 74 68 65 20 74 65 78 74 20  ** Add the text 
1cc75 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 6f  in register P1 o
1cc76 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74  nto the end of t
1cc77 68 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20 72 65  he text in.** re
1cc78 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 74  gister P2 and st
1cc79 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1cc7a 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1cc7b 2a 20 49 66 20 65 69 74 68 65 72 20 74 68 65 20  * If either the 
1cc7c 50 31 20 6f 72 20 50 32 20 74 65 78 74 20 61 72  P1 or P2 text ar
1cc7d 65 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74 6f 72  e NULL then stor
1cc7e 65 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a 2a 2a  e NULL in P3..**
1cc7f 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32 20 7c 7c  .**   P3 = P2 ||
1cc80 20 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20   P1.**.** It is 
1cc81 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31 20 61  illegal for P1 a
1cc82 6e 64 20 50 33 20 74 6f 20 62 65 20 74 68 65 20  nd P3 to be the 
1cc83 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e 20 53  same register. S
1cc84 6f 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69 66 20  ometimes,.** if 
1cc85 50 33 20 69 73 20 74 68 65 20 73 61 6d 65 20 72  P3 is the same r
1cc86 65 67 69 73 74 65 72 20 61 73 20 50 32 2c 20 74  egister as P2, t
1cc87 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
1cc88 6e 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74 6f 20  n is able.** to 
1cc89 61 76 6f 69 64 20 61 20 6d 65 6d 63 70 79 28 29  avoid a memcpy()
1cc8a 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e  ..*/.case OP_Con
1cc8b 63 61 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  cat: {          
1cc8c 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43   /* same as TK_C
1cc8d 4f 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e 32 2c  ONCAT, in1, in2,
1cc8e 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20   out3 */.#if 0  
1cc8f 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1cc90 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1cc91 61 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74  ae */.  i64 nByt
1cc92 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  e;.#endif /* loc
1cc93 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1cc94 65 64 20 69 6e 74 6f 20 75 2e 61 65 20 2a 2f 0a  ed into u.ae */.
1cc95 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 21  .  assert( pIn1!
1cc96 3d 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 20 28  =pOut );.  if( (
1cc97 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49  pIn1->flags | pI
1cc98 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d  n2->flags) & MEM
1cc99 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c  _Null ){.    sql
1cc9a 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
1cc9b 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72  ll(pOut);.    br
1cc9c 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45  eak;.  }.  if( E
1cc9d 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20  xpandBlob(pIn1) 
1cc9e 7c 7c 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49  || ExpandBlob(pI
1cc9f 6e 32 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  n2) ) goto no_me
1cca0 6d 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70  m;.  Stringify(p
1cca1 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  In1, encoding);.
1cca2 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32    Stringify(pIn2
1cca3 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75  , encoding);.  u
1cca4 2e 61 65 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31  .ae.nByte = pIn1
1cca5 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20  ->n + pIn2->n;. 
1cca6 20 69 66 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e   if( u.ae.nByte>
1cca7 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1cca8 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
1cca9 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f  ){.    goto too_
1ccaa 62 69 67 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65  big;.  }.  MemSe
1ccab 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1ccac 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 20  MEM_Str);.  if( 
1ccad 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1ccae 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e  ow(pOut, (int)u.
1ccaf 61 65 2e 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74  ae.nByte+2, pOut
1ccb0 3d 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20 67  ==pIn2) ){.    g
1ccb1 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1ccb2 20 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e 32    if( pOut!=pIn2
1ccb3 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70   ){.    memcpy(p
1ccb4 4f 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c  Out->z, pIn2->z,
1ccb5 20 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20   pIn2->n);.  }. 
1ccb6 20 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a   memcpy(&pOut->z
1ccb7 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d  [pIn2->n], pIn1-
1ccb8 3e 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20  >z, pIn1->n);.  
1ccb9 70 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79  pOut->z[u.ae.nBy
1ccba 74 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d  te] = 0;.  pOut-
1ccbb 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d  >z[u.ae.nByte+1]
1ccbc 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c   = 0;.  pOut->fl
1ccbd 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
1ccbe 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e  .  pOut->n = (in
1ccbf 74 29 75 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20  t)u.ae.nByte;.  
1ccc0 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f  pOut->enc = enco
1ccc1 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d  ding;.  UPDATE_M
1ccc2 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74  AX_BLOBSIZE(pOut
1ccc3 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1ccc4 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 50 31  * Opcode: Add P1
1ccc5 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1ccc6 20 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 69   Add the value i
1ccc7 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f  n register P1 to
1ccc8 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1ccc9 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64  gister P2.** and
1ccca 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
1cccb 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
1cccc 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
1cccd 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
1ccce 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1cccf 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1ccd0 4d 75 6c 74 69 70 6c 79 20 50 31 20 50 32 20 50  Multiply P1 P2 P
1ccd1 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d  3 * *.**.**.** M
1ccd2 75 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75  ultiply the valu
1ccd3 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1ccd4 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e   by the value in
1ccd5 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20   register P2.** 
1ccd6 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65  and store the re
1ccd7 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72  sult in register
1ccd8 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65   P3..** If eithe
1ccd9 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c  r input is NULL,
1ccda 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
1ccdb 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ULL..*/./* Opcod
1ccdc 65 3a 20 53 75 62 74 72 61 63 74 20 50 31 20 50  e: Subtract P1 P
1ccdd 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  2 P3 * *.**.** S
1ccde 75 62 74 72 61 63 74 20 74 68 65 20 76 61 6c 75  ubtract the valu
1ccdf 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1cce0 20 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 65 20   from the value 
1cce1 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a  in register P2.*
1cce2 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  * and store the 
1cce3 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
1cce4 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
1cce5 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1cce6 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1cce7 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
1cce8 6f 64 65 3a 20 44 69 76 69 64 65 20 50 31 20 50  ode: Divide P1 P
1cce9 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44  2 P3 * *.**.** D
1ccea 69 76 69 64 65 20 74 68 65 20 76 61 6c 75 65 20  ivide the value 
1cceb 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62  in register P1 b
1ccec 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  y the value in r
1cced 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e  egister P2.** an
1ccee 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  d store the resu
1ccef 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  lt in register P
1ccf0 33 20 28 50 33 3d 50 32 2f 50 31 29 2e 20 49 66  3 (P3=P2/P1). If
1ccf1 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a   the value in .*
1ccf2 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  * register P1 is
1ccf3 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
1ccf4 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20  result is NULL. 
1ccf5 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20  If either input 
1ccf6 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65  is .** NULL, the
1ccf7 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
1ccf8 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .*/./* Opcode: R
1ccf9 65 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20 50  emainder P1 P2 P
1ccfa 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70  3 * *.**.** Comp
1ccfb 75 74 65 20 74 68 65 20 72 65 6d 61 69 6e 64 65  ute the remainde
1ccfc 72 20 61 66 74 65 72 20 69 6e 74 65 67 65 72 20  r after integer 
1ccfd 64 69 76 69 73 69 6f 6e 20 6f 66 20 74 68 65 20  division of the 
1ccfe 76 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69  value in.** regi
1ccff 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76  ster P1 by the v
1cd00 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1cd01 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 68   P2 and store th
1cd02 65 20 72 65 73 75 6c 74 20 69 6e 20 50 33 2e 20  e result in P3. 
1cd03 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1cd04 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20   in register P2 
1cd05 69 73 20 7a 65 72 6f 20 74 68 65 20 72 65 73 75  is zero the resu
1cd06 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49  lt is NULL..** I
1cd07 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  f either operand
1cd08 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
1cd09 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
1cd0a 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20  .case OP_Add:   
1cd0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd0c 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c  /* same as TK_PL
1cd0d 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  US, in1, in2, ou
1cd0e 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75  t3 */.case OP_Su
1cd0f 62 74 72 61 63 74 3a 20 20 20 20 20 20 20 20 20  btract:         
1cd10 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1cd11 54 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69  TK_MINUS, in1, i
1cd12 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65  n2, out3 */.case
1cd13 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20   OP_Multiply:   
1cd14 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1cd15 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 2c 20 69  me as TK_STAR, i
1cd16 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1cd17 0a 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a  .case OP_Divide:
1cd18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd19 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c  /* same as TK_SL
1cd1a 41 53 48 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  ASH, in1, in2, o
1cd1b 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  ut3 */.case OP_R
1cd1c 65 6d 61 69 6e 64 65 72 3a 20 7b 20 20 20 20 20  emainder: {     
1cd1d 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1cd1e 20 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e   TK_REM, in1, in
1cd1f 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30  2, out3 */.#if 0
1cd20 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1cd21 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1cd22 75 2e 61 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  u.af */.  int fl
1cd23 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d  ags;      /* Com
1cd24 62 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67  bined MEM_* flag
1cd25 73 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75  s from both inpu
1cd26 74 73 20 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20  ts */.  i64 iA; 
1cd27 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67          /* Integ
1cd28 65 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74  er value of left
1cd29 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36   operand */.  i6
1cd2a 34 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  4 iB;         /*
1cd2b 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f   Integer value o
1cd2c 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  f right operand 
1cd2d 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 41 3b 20  */.  double rA; 
1cd2e 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c       /* Real val
1cd2f 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61  ue of left opera
1cd30 6e 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72  nd */.  double r
1cd31 42 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20  B;      /* Real 
1cd32 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f  value of right o
1cd33 70 65 72 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66  perand */.#endif
1cd34 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1cd35 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1cd36 2e 61 66 20 2a 2f 0a 0a 20 20 61 70 70 6c 79 4e  .af */..  applyN
1cd37 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70  umericAffinity(p
1cd38 49 6e 31 29 3b 0a 20 20 61 70 70 6c 79 4e 75 6d  In1);.  applyNum
1cd39 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e  ericAffinity(pIn
1cd3a 32 29 3b 0a 20 20 75 2e 61 66 2e 66 6c 61 67 73  2);.  u.af.flags
1cd3b 20 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c   = pIn1->flags |
1cd3c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 3b 0a 20 20   pIn2->flags;.  
1cd3d 69 66 28 20 28 75 2e 61 66 2e 66 6c 61 67 73 20  if( (u.af.flags 
1cd3e 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29  & MEM_Null)!=0 )
1cd3f 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63   goto arithmetic
1cd40 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b  _result_is_null;
1cd41 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c  .  if( (pIn1->fl
1cd42 61 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c 61 67  ags & pIn2->flag
1cd43 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 4d 45  s & MEM_Int)==ME
1cd44 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 75 2e 61  M_Int ){.    u.a
1cd45 66 2e 69 41 20 3d 20 70 49 6e 31 2d 3e 75 2e 69  f.iA = pIn1->u.i
1cd46 3b 0a 20 20 20 20 75 2e 61 66 2e 69 42 20 3d 20  ;.    u.af.iB = 
1cd47 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 73  pIn2->u.i;.    s
1cd48 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f  witch( pOp->opco
1cd49 64 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  de ){.      case
1cd4a 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20   OP_Add:        
1cd4b 20 75 2e 61 66 2e 69 42 20 2b 3d 20 75 2e 61 66   u.af.iB += u.af
1cd4c 2e 69 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b  .iA;       break
1cd4d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f  ;.      case OP_
1cd4e 53 75 62 74 72 61 63 74 3a 20 20 20 20 75 2e 61  Subtract:    u.a
1cd4f 66 2e 69 42 20 2d 3d 20 75 2e 61 66 2e 69 41 3b  f.iB -= u.af.iA;
1cd50 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1cd51 20 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74      case OP_Mult
1cd52 69 70 6c 79 3a 20 20 20 20 75 2e 61 66 2e 69 42  iply:    u.af.iB
1cd53 20 2a 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20   *= u.af.iA;    
1cd54 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1cd55 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20  case OP_Divide: 
1cd56 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  {.        if( u.
1cd57 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20  af.iA==0 ) goto 
1cd58 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c  arithmetic_resul
1cd59 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20  t_is_null;.     
1cd5a 20 20 20 2f 2a 20 44 69 76 69 64 69 6e 67 20 74     /* Dividing t
1cd5b 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69  he largest possi
1cd5c 62 6c 65 20 6e 65 67 61 74 69 76 65 20 36 34 2d  ble negative 64-
1cd5d 62 69 74 20 69 6e 74 65 67 65 72 20 28 31 3c 3c  bit integer (1<<
1cd5e 36 33 29 20 62 79 0a 20 20 20 20 20 20 20 20 2a  63) by.        *
1cd5f 2a 20 2d 31 20 72 65 74 75 72 6e 73 20 61 6e 20  * -1 returns an 
1cd60 69 6e 74 65 67 65 72 20 74 6f 6f 20 6c 61 72 67  integer too larg
1cd61 65 20 74 6f 20 73 74 6f 72 65 20 69 6e 20 61 20  e to store in a 
1cd62 36 34 2d 62 69 74 20 64 61 74 61 2d 74 79 70 65  64-bit data-type
1cd63 2e 20 4f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  . On.        ** 
1cd64 73 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75 72  some architectur
1cd65 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 76  es, the value ov
1cd66 65 72 66 6c 6f 77 73 20 74 6f 20 28 31 3c 3c 36  erflows to (1<<6
1cd67 33 29 2e 20 4f 6e 20 6f 74 68 65 72 73 2c 0a 20  3). On others,. 
1cd68 20 20 20 20 20 20 20 2a 2a 20 61 20 53 49 47 46         ** a SIGF
1cd69 50 45 20 69 73 20 69 73 73 75 65 64 2e 20 54 68  PE is issued. Th
1cd6a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74  e following stat
1cd6b 65 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69 7a 65 73  ement normalizes
1cd6c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a   this.        **
1cd6d 20 62 65 68 61 76 69 6f 72 20 73 6f 20 74 68 61   behavior so tha
1cd6e 74 20 61 6c 6c 20 61 72 63 68 69 74 65 63 74 75  t all architectu
1cd6f 72 65 73 20 62 65 68 61 76 65 20 61 73 20 69 66  res behave as if
1cd70 20 69 6e 74 65 67 65 72 0a 20 20 20 20 20 20 20   integer.       
1cd71 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6f 63 63   ** overflow occ
1cd72 75 72 72 65 64 2e 0a 20 20 20 20 20 20 20 20 2a  urred..        *
1cd73 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  /.        if( u.
1cd74 61 66 2e 69 41 3d 3d 2d 31 20 26 26 20 75 2e 61  af.iA==-1 && u.a
1cd75 66 2e 69 42 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49  f.iB==SMALLEST_I
1cd76 4e 54 36 34 20 29 20 75 2e 61 66 2e 69 41 20 3d  NT64 ) u.af.iA =
1cd77 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66   1;.        u.af
1cd78 2e 69 42 20 2f 3d 20 75 2e 61 66 2e 69 41 3b 0a  .iB /= u.af.iA;.
1cd79 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1cd7a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66       }.      def
1cd7b 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20  ault: {.        
1cd7c 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29  if( u.af.iA==0 )
1cd7d 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63   goto arithmetic
1cd7e 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b  _result_is_null;
1cd7f 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61  .        if( u.a
1cd80 66 2e 69 41 3d 3d 2d 31 20 29 20 75 2e 61 66 2e  f.iA==-1 ) u.af.
1cd81 69 41 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  iA = 1;.        
1cd82 75 2e 61 66 2e 69 42 20 25 3d 20 75 2e 61 66 2e  u.af.iB %= u.af.
1cd83 69 41 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  iA;.        brea
1cd84 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1cd85 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  .    pOut->u.i =
1cd86 20 75 2e 61 66 2e 69 42 3b 0a 20 20 20 20 4d 65   u.af.iB;.    Me
1cd87 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
1cd88 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d  t, MEM_Int);.  }
1cd89 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 66 2e 72  else{.    u.af.r
1cd8a 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52  A = sqlite3VdbeR
1cd8b 65 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a  ealValue(pIn1);.
1cd8c 20 20 20 20 75 2e 61 66 2e 72 42 20 3d 20 73 71      u.af.rB = sq
1cd8d 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
1cd8e 75 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 73 77  ue(pIn2);.    sw
1cd8f 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  itch( pOp->opcod
1cd90 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  e ){.      case 
1cd91 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20  OP_Add:         
1cd92 75 2e 61 66 2e 72 42 20 2b 3d 20 75 2e 61 66 2e  u.af.rB += u.af.
1cd93 72 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b  rA;       break;
1cd94 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53  .      case OP_S
1cd95 75 62 74 72 61 63 74 3a 20 20 20 20 75 2e 61 66  ubtract:    u.af
1cd96 2e 72 42 20 2d 3d 20 75 2e 61 66 2e 72 41 3b 20  .rB -= u.af.rA; 
1cd97 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1cd98 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69     case OP_Multi
1cd99 70 6c 79 3a 20 20 20 20 75 2e 61 66 2e 72 42 20  ply:    u.af.rB 
1cd9a 2a 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20  *= u.af.rA;     
1cd9b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1cd9c 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b  ase OP_Divide: {
1cd9d 0a 20 20 20 20 20 20 20 20 2f 2a 20 28 64 6f 75  .        /* (dou
1cd9e 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66  ble)0 In case of
1cd9f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
1cda0 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a  ATING_POINT... *
1cda1 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  /.        if( u.
1cda2 61 66 2e 72 41 3d 3d 28 64 6f 75 62 6c 65 29 30  af.rA==(double)0
1cda3 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74   ) goto arithmet
1cda4 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
1cda5 6c 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e  l;.        u.af.
1cda6 72 42 20 2f 3d 20 75 2e 61 66 2e 72 41 3b 0a 20  rB /= u.af.rA;. 
1cda7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1cda8 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61      }.      defa
1cda9 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 75  ult: {.        u
1cdaa 2e 61 66 2e 69 41 20 3d 20 28 69 36 34 29 75 2e  .af.iA = (i64)u.
1cdab 61 66 2e 72 41 3b 0a 20 20 20 20 20 20 20 20 75  af.rA;.        u
1cdac 2e 61 66 2e 69 42 20 3d 20 28 69 36 34 29 75 2e  .af.iB = (i64)u.
1cdad 61 66 2e 72 42 3b 0a 20 20 20 20 20 20 20 20 69  af.rB;.        i
1cdae 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20  f( u.af.iA==0 ) 
1cdaf 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f  goto arithmetic_
1cdb0 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a  result_is_null;.
1cdb1 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66          if( u.af
1cdb2 2e 69 41 3d 3d 2d 31 20 29 20 75 2e 61 66 2e 69  .iA==-1 ) u.af.i
1cdb3 41 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75  A = 1;.        u
1cdb4 2e 61 66 2e 72 42 20 3d 20 28 64 6f 75 62 6c 65  .af.rB = (double
1cdb5 29 28 75 2e 61 66 2e 69 42 20 25 20 75 2e 61 66  )(u.af.iB % u.af
1cdb6 2e 69 41 29 3b 0a 20 20 20 20 20 20 20 20 62 72  .iA);.        br
1cdb7 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1cdb8 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
1cdb9 65 33 49 73 4e 61 4e 28 75 2e 61 66 2e 72 42 29  e3IsNaN(u.af.rB)
1cdba 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61   ){.      goto a
1cdbb 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74  rithmetic_result
1cdbc 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 7d 0a  _is_null;.    }.
1cdbd 20 20 20 20 70 4f 75 74 2d 3e 72 20 3d 20 75 2e      pOut->r = u.
1cdbe 61 66 2e 72 42 3b 0a 20 20 20 20 4d 65 6d 53 65  af.rB;.    MemSe
1cdbf 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1cdc0 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69  MEM_Real);.    i
1cdc1 66 28 20 28 75 2e 61 66 2e 66 6c 61 67 73 20 26  f( (u.af.flags &
1cdc2 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b   MEM_Real)==0 ){
1cdc3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1cdc4 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74  beIntegerAffinit
1cdc5 79 28 70 4f 75 74 29 3b 0a 20 20 20 20 7d 0a 20  y(pOut);.    }. 
1cdc6 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 0a 61 72 69   }.  break;..ari
1cdc7 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69  thmetic_result_i
1cdc8 73 5f 6e 75 6c 6c 3a 0a 20 20 73 71 6c 69 74 65  s_null:.  sqlite
1cdc9 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
1cdca 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pOut);.  break;.
1cdcb 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f  }../* Opcode: Co
1cdcc 6c 6c 53 65 71 20 2a 20 2a 20 50 34 0a 2a 2a 0a  llSeq * * P4.**.
1cdcd 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
1cdce 65 72 20 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20  er to a CollSeq 
1cdcf 73 74 72 75 63 74 2e 20 49 66 20 74 68 65 20 6e  struct. If the n
1cdd0 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 20 75 73  ext call to a us
1cdd1 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6f  er function.** o
1cdd2 72 20 61 67 67 72 65 67 61 74 65 20 63 61 6c 6c  r aggregate call
1cdd3 73 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63  s sqlite3GetFunc
1cdd4 43 6f 6c 6c 53 65 71 28 29 2c 20 74 68 69 73 20  CollSeq(), this 
1cdd5 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
1cdd6 63 65 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 72 65  ce will.** be re
1cdd7 74 75 72 6e 65 64 2e 20 54 68 69 73 20 69 73 20  turned. This is 
1cdd8 75 73 65 64 20 62 79 20 74 68 65 20 62 75 69 6c  used by the buil
1cdd9 74 2d 69 6e 20 6d 69 6e 28 29 2c 20 6d 61 78 28  t-in min(), max(
1cdda 29 20 61 6e 64 20 6e 75 6c 6c 69 66 28 29 0a 2a  ) and nullif().*
1cddb 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a  * functions..**.
1cddc 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65  ** The interface
1cddd 20 75 73 65 64 20 62 79 20 74 68 65 20 69 6d 70   used by the imp
1cdde 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
1cddf 68 65 20 61 66 6f 72 65 6d 65 6e 74 69 6f 6e 65  he aforementione
1cde0 64 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74  d functions.** t
1cde1 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20 63  o retrieve the c
1cde2 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
1cde3 65 20 73 65 74 20 62 79 20 74 68 69 73 20 6f 70  e set by this op
1cde4 63 6f 64 65 20 69 73 20 6e 6f 74 20 61 76 61 69  code is not avai
1cde5 6c 61 62 6c 65 0a 2a 2a 20 70 75 62 6c 69 63 6c  lable.** publicl
1cde6 79 2c 20 6f 6e 6c 79 20 74 6f 20 75 73 65 72 20  y, only to user 
1cde7 66 75 6e 63 74 69 6f 6e 73 20 64 65 66 69 6e 65  functions define
1cde8 64 20 69 6e 20 66 75 6e 63 2e 63 2e 0a 2a 2f 0a  d in func.c..*/.
1cde9 63 61 73 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 3a  case OP_CollSeq:
1cdea 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70   {.  assert( pOp
1cdeb 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c  ->p4type==P4_COL
1cdec 4c 53 45 51 20 29 3b 0a 20 20 62 72 65 61 6b 3b  LSEQ );.  break;
1cded 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46  .}../* Opcode: F
1cdee 75 6e 63 74 69 6f 6e 20 50 31 20 50 32 20 50 33  unction P1 P2 P3
1cdef 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 76   P4 P5.**.** Inv
1cdf0 6f 6b 65 20 61 20 75 73 65 72 20 66 75 6e 63 74  oke a user funct
1cdf1 69 6f 6e 20 28 50 34 20 69 73 20 61 20 70 6f 69  ion (P4 is a poi
1cdf2 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 74 69  nter to a Functi
1cdf3 6f 6e 20 73 74 72 75 63 74 75 72 65 20 74 68 61  on structure tha
1cdf4 74 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65  t.** defines the
1cdf5 20 66 75 6e 63 74 69 6f 6e 29 20 77 69 74 68 20   function) with 
1cdf6 50 35 20 61 72 67 75 6d 65 6e 74 73 20 74 61 6b  P5 arguments tak
1cdf7 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72  en from register
1cdf8 20 50 32 20 61 6e 64 0a 2a 2a 20 73 75 63 63 65   P2 and.** succe
1cdf9 73 73 6f 72 73 2e 20 20 54 68 65 20 72 65 73 75  ssors.  The resu
1cdfa 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  lt of the functi
1cdfb 6f 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  on is stored in 
1cdfc 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20  register P3..** 
1cdfd 52 65 67 69 73 74 65 72 20 50 33 20 6d 75 73 74  Register P3 must
1cdfe 20 6e 6f 74 20 62 65 20 6f 6e 65 20 6f 66 20 74   not be one of t
1cdff 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 70 75  he function inpu
1ce00 74 73 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  ts..**.** P1 is 
1ce01 61 20 33 32 2d 62 69 74 20 62 69 74 6d 61 73 6b  a 32-bit bitmask
1ce02 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68 65 74   indicating whet
1ce03 68 65 72 20 6f 72 20 6e 6f 74 20 65 61 63 68 20  her or not each 
1ce04 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
1ce05 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73  .** function was
1ce06 20 64 65 74 65 72 6d 69 6e 65 64 20 74 6f 20 62   determined to b
1ce07 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f  e constant at co
1ce08 6d 70 69 6c 65 20 74 69 6d 65 2e 20 49 66 20 74  mpile time. If t
1ce09 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75  he first.** argu
1ce0a 6d 65 6e 74 20 77 61 73 20 63 6f 6e 73 74 61 6e  ment was constan
1ce0b 74 20 74 68 65 6e 20 62 69 74 20 30 20 6f 66 20  t then bit 0 of 
1ce0c 50 31 20 69 73 20 73 65 74 2e 20 54 68 69 73 20  P1 is set. This 
1ce0d 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  is used to deter
1ce0e 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20  mine.** whether 
1ce0f 6d 65 74 61 20 64 61 74 61 20 61 73 73 6f 63 69  meta data associ
1ce10 61 74 65 64 20 77 69 74 68 20 61 20 75 73 65 72  ated with a user
1ce11 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
1ce12 6e 74 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  nt using the.** 
1ce13 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
1ce14 61 74 61 28 29 20 41 50 49 20 6d 61 79 20 62 65  ata() API may be
1ce15 20 73 61 66 65 6c 79 20 72 65 74 61 69 6e 65 64   safely retained
1ce16 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 0a   until the next.
1ce17 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  ** invocation of
1ce18 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2a   this opcode..**
1ce19 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 41 67  .** See also: Ag
1ce1a 67 53 74 65 70 20 61 6e 64 20 41 67 67 46 69 6e  gStep and AggFin
1ce1b 61 6c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 75  al.*/.case OP_Fu
1ce1c 6e 63 74 69 6f 6e 3a 20 7b 0a 23 69 66 20 30 20  nction: {.#if 0 
1ce1d 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ce1e 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ce1f 2e 61 67 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a  .ag */.  int i;.
1ce20 20 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 73    Mem *pArg;.  s
1ce21 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63  qlite3_context c
1ce22 74 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  tx;.  sqlite3_va
1ce23 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 69  lue **apVal;.  i
1ce24 6e 74 20 6e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  nt n;.#endif /* 
1ce25 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1ce26 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67 20  moved into u.ag 
1ce27 2a 2f 0a 0a 20 20 75 2e 61 67 2e 6e 20 3d 20 70  */..  u.ag.n = p
1ce28 4f 70 2d 3e 70 35 3b 0a 20 20 75 2e 61 67 2e 61  Op->p5;.  u.ag.a
1ce29 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b  pVal = p->apArg;
1ce2a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 67 2e  .  assert( u.ag.
1ce2b 61 70 56 61 6c 20 7c 7c 20 75 2e 61 67 2e 6e 3d  apVal || u.ag.n=
1ce2c 3d 30 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  =0 );..  assert(
1ce2d 20 75 2e 61 67 2e 6e 3d 3d 30 20 7c 7c 20 28 70   u.ag.n==0 || (p
1ce2e 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d  Op->p2>0 && pOp-
1ce2f 3e 70 32 2b 75 2e 61 67 2e 6e 3c 3d 70 2d 3e 6e  >p2+u.ag.n<=p->n
1ce30 4d 65 6d 2b 31 29 20 29 3b 0a 20 20 61 73 73 65  Mem+1) );.  asse
1ce31 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d  rt( pOp->p3<pOp-
1ce32 3e 70 32 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d  >p2 || pOp->p3>=
1ce33 70 4f 70 2d 3e 70 32 2b 75 2e 61 67 2e 6e 20 29  pOp->p2+u.ag.n )
1ce34 3b 0a 20 20 75 2e 61 67 2e 70 41 72 67 20 3d 20  ;.  u.ag.pArg = 
1ce35 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1ce36 5d 3b 0a 20 20 66 6f 72 28 75 2e 61 67 2e 69 3d  ];.  for(u.ag.i=
1ce37 30 3b 20 75 2e 61 67 2e 69 3c 75 2e 61 67 2e 6e  0; u.ag.i<u.ag.n
1ce38 3b 20 75 2e 61 67 2e 69 2b 2b 2c 20 75 2e 61 67  ; u.ag.i++, u.ag
1ce39 2e 70 41 72 67 2b 2b 29 7b 0a 20 20 20 20 75 2e  .pArg++){.    u.
1ce3a 61 67 2e 61 70 56 61 6c 5b 75 2e 61 67 2e 69 5d  ag.apVal[u.ag.i]
1ce3b 20 3d 20 75 2e 61 67 2e 70 41 72 67 3b 0a 20 20   = u.ag.pArg;.  
1ce3c 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1ce3d 53 74 6f 72 65 54 79 70 65 28 75 2e 61 67 2e 70  StoreType(u.ag.p
1ce3e 41 72 67 29 3b 0a 20 20 20 20 52 45 47 49 53 54  Arg);.    REGIST
1ce3f 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32  ER_TRACE(pOp->p2
1ce40 2c 20 75 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20  , u.ag.pArg);.  
1ce41 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  }..  assert( pOp
1ce42 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e  ->p4type==P4_FUN
1ce43 43 44 45 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 74  CDEF || pOp->p4t
1ce44 79 70 65 3d 3d 50 34 5f 56 44 42 45 46 55 4e 43  ype==P4_VDBEFUNC
1ce45 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70   );.  if( pOp->p
1ce46 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45  4type==P4_FUNCDE
1ce47 46 20 29 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74  F ){.    u.ag.ct
1ce48 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70  x.pFunc = pOp->p
1ce49 34 2e 70 46 75 6e 63 3b 0a 20 20 20 20 75 2e 61  4.pFunc;.    u.a
1ce4a 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20  g.ctx.pVdbeFunc 
1ce4b 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
1ce4c 20 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65    u.ag.ctx.pVdbe
1ce4d 46 75 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63  Func = (VdbeFunc
1ce4e 2a 29 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46  *)pOp->p4.pVdbeF
1ce4f 75 6e 63 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74  unc;.    u.ag.ct
1ce50 78 2e 70 46 75 6e 63 20 3d 20 75 2e 61 67 2e 63  x.pFunc = u.ag.c
1ce51 74 78 2e 70 56 64 62 65 46 75 6e 63 2d 3e 70 46  tx.pVdbeFunc->pF
1ce52 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  unc;.  }..  asse
1ce53 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26  rt( pOp->p3>0 &&
1ce54 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65   pOp->p3<=p->nMe
1ce55 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70  m );.  pOut = &p
1ce56 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
1ce57 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 66 6c  .  u.ag.ctx.s.fl
1ce58 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
1ce59 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 64 62 20    u.ag.ctx.s.db 
1ce5a 3d 20 64 62 3b 0a 20 20 75 2e 61 67 2e 63 74 78  = db;.  u.ag.ctx
1ce5b 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75  .s.xDel = 0;.  u
1ce5c 2e 61 67 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f  .ag.ctx.s.zMallo
1ce5d 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65  c = 0;..  /* The
1ce5e 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79   output cell may
1ce5f 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20   already have a 
1ce60 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64  buffer allocated
1ce61 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20  . Move.  ** the 
1ce62 70 6f 69 6e 74 65 72 20 74 6f 20 75 2e 61 67 2e  pointer to u.ag.
1ce63 63 74 78 2e 73 20 73 6f 20 69 6e 20 63 61 73 65  ctx.s so in case
1ce64 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69   the user-functi
1ce65 6f 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20  on can use.  ** 
1ce66 74 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f  the already allo
1ce67 63 61 74 65 64 20 62 75 66 66 65 72 20 69 6e 73  cated buffer ins
1ce68 74 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69  tead of allocati
1ce69 6e 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20  ng a new one..  
1ce6a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
1ce6b 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 67 2e 63 74  MemMove(&u.ag.ct
1ce6c 78 2e 73 2c 20 70 4f 75 74 29 3b 0a 20 20 4d 65  x.s, pOut);.  Me
1ce6d 6d 53 65 74 54 79 70 65 46 6c 61 67 28 26 75 2e  mSetTypeFlag(&u.
1ce6e 61 67 2e 63 74 78 2e 73 2c 20 4d 45 4d 5f 4e 75  ag.ctx.s, MEM_Nu
1ce6f 6c 6c 29 3b 0a 0a 20 20 75 2e 61 67 2e 63 74 78  ll);..  u.ag.ctx
1ce70 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20  .isError = 0;.  
1ce71 69 66 28 20 75 2e 61 67 2e 63 74 78 2e 70 46 75  if( u.ag.ctx.pFu
1ce72 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  nc->flags & SQLI
1ce73 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c  TE_FUNC_NEEDCOLL
1ce74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1ce75 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20  pOp>p->aOp );.  
1ce76 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31    assert( pOp[-1
1ce77 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c  ].p4type==P4_COL
1ce78 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65  LSEQ );.    asse
1ce79 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f  rt( pOp[-1].opco
1ce7a 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29  de==OP_CollSeq )
1ce7b 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70  ;.    u.ag.ctx.p
1ce7c 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70  Coll = pOp[-1].p
1ce7d 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69  4.pColl;.  }.  i
1ce7e 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1ce7f 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61  Off(db) ) goto a
1ce80 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1ce81 73 65 3b 0a 20 20 28 2a 75 2e 61 67 2e 63 74 78  se;.  (*u.ag.ctx
1ce82 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26  .pFunc->xFunc)(&
1ce83 75 2e 61 67 2e 63 74 78 2c 20 75 2e 61 67 2e 6e  u.ag.ctx, u.ag.n
1ce84 2c 20 75 2e 61 67 2e 61 70 56 61 6c 29 3b 0a 20  , u.ag.apVal);. 
1ce85 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1ce86 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20  tyOn(db) ){.    
1ce87 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1ce88 6c 65 61 73 65 28 26 75 2e 61 67 2e 63 74 78 2e  lease(&u.ag.ctx.
1ce89 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f  s);.    goto abo
1ce8a 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1ce8b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e  ;.  }.  if( db->
1ce8c 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1ce8d 20 20 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75      /* Even thou
1ce8e 67 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61  gh a malloc() ha
1ce8f 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20 69 6d  s failed, the im
1ce90 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
1ce91 74 68 65 0a 20 20 20 20 2a 2a 20 75 73 65 72 20  the.    ** user 
1ce92 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68 61 76  function may hav
1ce93 65 20 63 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69  e called an sqli
1ce94 74 65 33 5f 72 65 73 75 6c 74 5f 58 58 58 28 29  te3_result_XXX()
1ce95 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a   function.    **
1ce96 20 74 6f 20 72 65 74 75 72 6e 20 61 20 76 61 6c   to return a val
1ce97 75 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ue. The followin
1ce98 67 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 73 20  g call releases 
1ce99 61 6e 79 20 72 65 73 6f 75 72 63 65 73 0a 20 20  any resources.  
1ce9a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20    ** associated 
1ce9b 77 69 74 68 20 73 75 63 68 20 61 20 76 61 6c 75  with such a valu
1ce9c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e..    **.    **
1ce9d 20 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d   Note: Maybe Mem
1ce9e 52 65 6c 65 61 73 65 28 29 20 73 68 6f 75 6c 64  Release() should
1ce9f 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 73 71   be called if sq
1cea0 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 29 0a  lite3SafetyOn().
1cea1 20 20 20 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73      ** fails als
1cea2 6f 20 28 74 68 65 20 69 66 28 2e 2e 2e 29 20 73  o (the if(...) s
1cea3 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e  tatement above).
1cea4 20 42 75 74 20 69 66 20 70 65 6f 70 6c 65 20 61   But if people a
1cea5 72 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75 73 69  re.    ** misusi
1cea6 6e 67 20 73 71 6c 69 74 65 2c 20 74 68 65 79 20  ng sqlite, they 
1cea7 68 61 76 65 20 62 69 67 67 65 72 20 70 72 6f 62  have bigger prob
1cea8 6c 65 6d 73 20 74 68 61 6e 20 61 20 6c 65 61 6b  lems than a leak
1cea9 65 64 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f  ed value..    */
1ceaa 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ceab 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 61 67  MemRelease(&u.ag
1ceac 2e 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74  .ctx.s);.    got
1cead 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20  o no_mem;.  }.. 
1ceae 20 2f 2a 20 49 66 20 61 6e 79 20 61 75 78 69 6c   /* If any auxil
1ceaf 69 61 72 79 20 64 61 74 61 20 66 75 6e 63 74 69  iary data functi
1ceb0 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 63 61  ons have been ca
1ceb1 6c 6c 65 64 20 62 79 20 74 68 69 73 20 75 73 65  lled by this use
1ceb2 72 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a  r function,.  **
1ceb3 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 63 61 6c   immediately cal
1ceb4 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  l the destructor
1ceb5 20 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61   for any non-sta
1ceb6 74 69 63 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f  tic values..  */
1ceb7 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74 78 2e  .  if( u.ag.ctx.
1ceb8 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20  pVdbeFunc ){.   
1ceb9 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
1ceba 74 65 41 75 78 44 61 74 61 28 75 2e 61 67 2e 63  teAuxData(u.ag.c
1cebb 74 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f  tx.pVdbeFunc, pO
1cebc 70 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d  p->p1);.    pOp-
1cebd 3e 70 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20  >p4.pVdbeFunc = 
1cebe 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75  u.ag.ctx.pVdbeFu
1cebf 6e 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74  nc;.    pOp->p4t
1cec0 79 70 65 20 3d 20 50 34 5f 56 44 42 45 46 55 4e  ype = P4_VDBEFUN
1cec1 43 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  C;.  }..  /* If 
1cec2 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  the function ret
1cec3 75 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 2c 20  urned an error, 
1cec4 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69  throw an excepti
1cec5 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67  on */.  if( u.ag
1cec6 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a  .ctx.isError ){.
1cec7 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1cec8 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1cec9 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
1ceca 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26  te3_value_text(&
1cecb 75 2e 61 67 2e 63 74 78 2e 73 29 29 3b 0a 20 20  u.ag.ctx.s));.  
1cecc 20 20 72 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e    rc = u.ag.ctx.
1cecd 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20  isError;.  }..  
1cece 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75  /* Copy the resu
1cecf 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  lt of the functi
1ced0 6f 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  on into register
1ced1 20 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33   P3 */.  sqlite3
1ced2 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
1ced3 6e 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20  ng(&u.ag.ctx.s, 
1ced4 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c  encoding);.  sql
1ced5 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
1ced6 70 4f 75 74 2c 20 26 75 2e 61 67 2e 63 74 78 2e  pOut, &u.ag.ctx.
1ced7 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  s);.  if( sqlite
1ced8 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70  3VdbeMemTooBig(p
1ced9 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Out) ){.    goto
1ceda 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20   too_big;.  }.  
1cedb 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1cedc 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20  Op->p3, pOut);. 
1cedd 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1cede 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72  SIZE(pOut);.  br
1cedf 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1cee0 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32 20  e: BitAnd P1 P2 
1cee1 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b  P3 * *.**.** Tak
1cee2 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 41  e the bit-wise A
1cee3 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73  ND of the values
1cee4 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1cee5 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74  and P2 and.** st
1cee6 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1cee7 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1cee8 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
1cee9 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
1ceea 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
1ceeb 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74  /./* Opcode: Bit
1ceec 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  Or P1 P2 P3 * *.
1ceed 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62  **.** Take the b
1ceee 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 68  it-wise OR of th
1ceef 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
1cef0 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61  ster P1 and P2 a
1cef1 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20  nd.** store the 
1cef2 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
1cef3 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
1cef4 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1cef5 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1cef6 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
1cef7 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 50  ode: ShiftLeft P
1cef8 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1cef9 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65  * Shift the inte
1cefa 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67  ger value in reg
1cefb 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20  ister P2 to the 
1cefc 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e  left by the.** n
1cefd 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70  umber of bits sp
1cefe 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69  ecified by the i
1ceff 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 65  nteger in regise
1cf00 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74  r P1..** Store t
1cf01 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
1cf02 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20  ister P3..** If 
1cf03 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20  either input is 
1cf04 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
1cf05 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20   is NULL..*/./* 
1cf06 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69 67  Opcode: ShiftRig
1cf07 68 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  ht P1 P2 P3 * *.
1cf08 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20  **.** Shift the 
1cf09 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e  integer value in
1cf0a 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20   register P2 to 
1cf0b 74 68 65 20 72 69 67 68 74 20 62 79 20 74 68 65  the right by the
1cf0c 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69  .** number of bi
1cf0d 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ts specified by 
1cf0e 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72  the integer in r
1cf0f 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53  egister P1..** S
1cf10 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1cf11 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a  in register P3..
1cf12 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70  ** If either inp
1cf13 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20  ut is NULL, the 
1cf14 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a  result is NULL..
1cf15 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41 6e  */.case OP_BitAn
1cf16 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d:              
1cf17 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1cf18 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e  _BITAND, in1, in
1cf19 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20  2, out3 */.case 
1cf1a 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20  OP_BitOr:       
1cf1b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1cf1c 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20  me as TK_BITOR, 
1cf1d 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1cf1e 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c  /.case OP_ShiftL
1cf1f 65 66 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  eft:            
1cf20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1cf21 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32  LSHIFT, in1, in2
1cf22 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
1cf23 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b 20  P_ShiftRight: { 
1cf24 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1cf25 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c 20  e as TK_RSHIFT, 
1cf26 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1cf27 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1cf28 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1cf29 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 20  d into u.ah */. 
1cf2a 20 69 36 34 20 61 3b 0a 20 20 69 36 34 20 62 3b   i64 a;.  i64 b;
1cf2b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1cf2c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1cf2d 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 0a 20   into u.ah */.. 
1cf2e 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
1cf2f 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29  s | pIn2->flags)
1cf30 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
1cf31 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1cf32 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a  mSetNull(pOut);.
1cf33 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
1cf34 20 75 2e 61 68 2e 61 20 3d 20 73 71 6c 69 74 65   u.ah.a = sqlite
1cf35 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49  3VdbeIntValue(pI
1cf36 6e 32 29 3b 0a 20 20 75 2e 61 68 2e 62 20 3d 20  n2);.  u.ah.b = 
1cf37 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
1cf38 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 73 77 69  lue(pIn1);.  swi
1cf39 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  tch( pOp->opcode
1cf3a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f   ){.    case OP_
1cf3b 42 69 74 41 6e 64 3a 20 20 20 20 20 20 75 2e 61  BitAnd:      u.a
1cf3c 68 2e 61 20 26 3d 20 75 2e 61 68 2e 62 3b 20 20  h.a &= u.ah.b;  
1cf3d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
1cf3e 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20  se OP_BitOr:    
1cf3f 20 20 20 75 2e 61 68 2e 61 20 7c 3d 20 75 2e 61     u.ah.a |= u.a
1cf40 68 2e 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a  h.b;     break;.
1cf41 20 20 20 20 63 61 73 65 20 4f 50 5f 53 68 69 66      case OP_Shif
1cf42 74 4c 65 66 74 3a 20 20 20 75 2e 61 68 2e 61 20  tLeft:   u.ah.a 
1cf43 3c 3c 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 62  <<= u.ah.b;    b
1cf44 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c  reak;.    defaul
1cf45 74 3a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  t:  assert( pOp-
1cf46 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66  >opcode==OP_Shif
1cf47 74 52 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20  tRight );.      
1cf48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cf49 20 20 20 75 2e 61 68 2e 61 20 3e 3e 3d 20 75 2e     u.ah.a >>= u.
1cf4a 61 68 2e 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a  ah.b;    break;.
1cf4b 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20    }.  pOut->u.i 
1cf4c 3d 20 75 2e 61 68 2e 61 3b 0a 20 20 4d 65 6d 53  = u.ah.a;.  MemS
1cf4d 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1cf4e 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65   MEM_Int);.  bre
1cf4f 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1cf50 3a 20 41 64 64 49 6d 6d 20 20 50 31 20 50 32 20  : AddImm  P1 P2 
1cf51 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64  * * *.** .** Add
1cf52 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 50 32   the constant P2
1cf53 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e   to the value in
1cf54 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a   register P1..**
1cf55 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   The result is a
1cf56 6c 77 61 79 73 20 61 6e 20 69 6e 74 65 67 65 72  lways an integer
1cf57 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65  ..**.** To force
1cf58 20 61 6e 79 20 72 65 67 69 73 74 65 72 20 74 6f   any register to
1cf59 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2c 20   be an integer, 
1cf5a 6a 75 73 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63  just add 0..*/.c
1cf5b 61 73 65 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b  ase OP_AddImm: {
1cf5c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69              /* i
1cf5d 6e 31 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  n1 */.  sqlite3V
1cf5e 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
1cf5f 28 70 49 6e 31 29 3b 0a 20 20 70 49 6e 31 2d 3e  (pIn1);.  pIn1->
1cf60 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a  u.i += pOp->p2;.
1cf61 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1cf62 70 63 6f 64 65 3a 20 4d 75 73 74 42 65 49 6e 74  pcode: MustBeInt
1cf63 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20   P1 P2 * * *.** 
1cf64 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61  .** Force the va
1cf65 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1cf66 50 31 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65  P1 to be an inte
1cf67 67 65 72 2e 20 20 49 66 20 74 68 65 20 76 61 6c  ger.  If the val
1cf68 75 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 6e  ue.** in P1 is n
1cf69 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e  ot an integer an
1cf6a 64 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76  d cannot be conv
1cf6b 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e  erted into an in
1cf6c 74 65 67 65 72 0a 2a 2a 20 77 69 74 68 6f 75 74  teger.** without
1cf6d 20 64 61 74 61 20 6c 6f 73 73 2c 20 74 68 65 6e   data loss, then
1cf6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c   jump immediatel
1cf6f 79 20 74 6f 20 50 32 2c 20 6f 72 20 69 66 20 50  y to P2, or if P
1cf70 32 3d 3d 30 0a 2a 2a 20 72 61 69 73 65 20 61 6e  2==0.** raise an
1cf71 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48   SQLITE_MISMATCH
1cf72 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 63   exception..*/.c
1cf73 61 73 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74  ase OP_MustBeInt
1cf74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
1cf75 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20  * jump, in1 */. 
1cf76 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70   applyAffinity(p
1cf77 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  In1, SQLITE_AFF_
1cf78 4e 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e  NUMERIC, encodin
1cf79 67 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d  g);.  if( (pIn1-
1cf7a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1cf7b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
1cf7c 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20  pOp->p2==0 ){.  
1cf7d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1cf7e 4d 49 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20  MISMATCH;.      
1cf7f 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1cf80 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c  o_error;.    }el
1cf81 73 65 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70  se{.      pc = p
1cf82 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1cf83 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d  }.  }else{.    M
1cf84 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49  emSetTypeFlag(pI
1cf85 6e 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  n1, MEM_Int);.  
1cf86 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1cf87 20 4f 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66   Opcode: RealAff
1cf88 69 6e 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a  inity P1 * * * *
1cf89 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74  .**.** If regist
1cf8a 65 72 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69  er P1 holds an i
1cf8b 6e 74 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69  nteger convert i
1cf8c 74 20 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75  t to a real valu
1cf8d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70  e..**.** This op
1cf8e 63 6f 64 65 20 69 73 20 75 73 65 64 20 77 68 65  code is used whe
1cf8f 6e 20 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66  n extracting inf
1cf90 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20  ormation from a 
1cf91 63 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68  column that.** h
1cf92 61 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79  as REAL affinity
1cf93 2e 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76  .  Such column v
1cf94 61 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20  alues may still 
1cf95 62 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20  be stored as.** 
1cf96 69 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70  integers, for sp
1cf97 61 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20  ace efficiency, 
1cf98 62 75 74 20 61 66 74 65 72 20 65 78 74 72 61 63  but after extrac
1cf99 74 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65  tion we want the
1cf9a 6d 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c  m.** to have onl
1cf9b 79 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a  y a real value..
1cf9c 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41  */.case OP_RealA
1cf9d 66 66 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20  ffinity: {      
1cf9e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69              /* i
1cf9f 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31  n1 */.  if( pIn1
1cfa0 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1cfa1 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
1cfa2 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70  VdbeMemRealify(p
1cfa3 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61  In1);.  }.  brea
1cfa4 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  k;.}..#ifndef SQ
1cfa5 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f  LITE_OMIT_CAST./
1cfa6 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 54 65 78 74  * Opcode: ToText
1cfa7 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   P1 * * * *.**.*
1cfa8 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75  * Force the valu
1cfa9 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1cfaa 20 74 6f 20 62 65 20 74 65 78 74 2e 0a 2a 2a 20   to be text..** 
1cfab 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
1cfac 6e 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74  numeric, convert
1cfad 20 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20   it to a string 
1cfae 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75  using the.** equ
1cfaf 69 76 61 6c 65 6e 74 20 6f 66 20 70 72 69 6e 74  ivalent of print
1cfb0 66 28 29 2e 20 20 42 6c 6f 62 20 76 61 6c 75 65  f().  Blob value
1cfb1 73 20 61 72 65 20 75 6e 63 68 61 6e 67 65 64 20  s are unchanged 
1cfb2 61 6e 64 0a 2a 2a 20 61 72 65 20 61 66 74 65 72  and.** are after
1cfb3 77 61 72 64 73 20 73 69 6d 70 6c 79 20 69 6e 74  wards simply int
1cfb4 65 72 70 72 65 74 65 64 20 61 73 20 74 65 78 74  erpreted as text
1cfb5 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76  ..**.** A NULL v
1cfb6 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e  alue is not chan
1cfb7 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ged by this rout
1cfb8 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73  ine.  It remains
1cfb9 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f   NULL..*/.case O
1cfba 50 5f 54 6f 54 65 78 74 3a 20 7b 20 20 20 20 20  P_ToText: {     
1cfbb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cfbc 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45  same as TK_TO_TE
1cfbd 58 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28  XT, in1 */.  if(
1cfbe 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
1cfbf 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b  EM_Null ) break;
1cfc0 0a 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53  .  assert( MEM_S
1cfc1 74 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33  tr==(MEM_Blob>>3
1cfc2 29 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61  ) );.  pIn1->fla
1cfc3 67 73 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61  gs |= (pIn1->fla
1cfc4 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b  gs&MEM_Blob)>>3;
1cfc5 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79  .  applyAffinity
1cfc6 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46  (pIn1, SQLITE_AF
1cfc7 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67  F_TEXT, encoding
1cfc8 29 3b 0a 20 20 72 63 20 3d 20 45 78 70 61 6e 64  );.  rc = Expand
1cfc9 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 73  Blob(pIn1);.  as
1cfca 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67  sert( pIn1->flag
1cfcb 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64  s & MEM_Str || d
1cfcc 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1cfcd 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73  );.  pIn1->flags
1cfce 20 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45   &= ~(MEM_Int|ME
1cfcf 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c  M_Real|MEM_Blob|
1cfd0 4d 45 4d 5f 5a 65 72 6f 29 3b 0a 20 20 55 50 44  MEM_Zero);.  UPD
1cfd1 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
1cfd2 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b  (pIn1);.  break;
1cfd3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54  .}../* Opcode: T
1cfd4 6f 42 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20 2a  oBlob P1 * * * *
1cfd5 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65  .**.** Force the
1cfd6 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1cfd7 65 72 20 50 31 20 74 6f 20 62 65 20 61 20 42 4c  er P1 to be a BL
1cfd8 4f 42 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61  OB..** If the va
1cfd9 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20  lue is numeric, 
1cfda 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20  convert it to a 
1cfdb 73 74 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a 2a  string first..**
1cfdc 20 53 74 72 69 6e 67 73 20 61 72 65 20 73 69 6d   Strings are sim
1cfdd 70 6c 79 20 72 65 69 6e 74 65 72 70 72 65 74 65  ply reinterprete
1cfde 64 20 61 73 20 62 6c 6f 62 73 20 77 69 74 68 20  d as blobs with 
1cfdf 6e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f 20  no change.** to 
1cfe0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64  the underlying d
1cfe1 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c  ata..**.** A NUL
1cfe2 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63  L value is not c
1cfe3 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72  hanged by this r
1cfe4 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61  outine.  It rema
1cfe5 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73  ins NULL..*/.cas
1cfe6 65 20 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20  e OP_ToBlob: {  
1cfe7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cfe8 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f  /* same as TK_TO
1cfe9 5f 42 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 20  _BLOB, in1 */.  
1cfea 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  if( pIn1->flags 
1cfeb 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65  & MEM_Null ) bre
1cfec 61 6b 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d  ak;.  if( (pIn1-
1cfed 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
1cfee 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 70 70  b)==0 ){.    app
1cfef 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c  lyAffinity(pIn1,
1cff0 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
1cff1 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20  , encoding);.   
1cff2 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66   assert( pIn1->f
1cff3 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c  lags & MEM_Str |
1cff4 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
1cff5 65 64 20 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74  ed );.    MemSet
1cff6 54 79 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d  TypeFlag(pIn1, M
1cff7 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 65 6c 73  EM_Blob);.  }els
1cff8 65 7b 0a 20 20 20 20 70 49 6e 31 2d 3e 66 6c 61  e{.    pIn1->fla
1cff9 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 54 79 70 65  gs &= ~(MEM_Type
1cffa 4d 61 73 6b 26 7e 4d 45 4d 5f 42 6c 6f 62 29 3b  Mask&~MEM_Blob);
1cffb 0a 20 20 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41  .  }.  UPDATE_MA
1cffc 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29  X_BLOBSIZE(pIn1)
1cffd 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1cffe 20 4f 70 63 6f 64 65 3a 20 54 6f 4e 75 6d 65 72   Opcode: ToNumer
1cfff 69 63 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  ic P1 * * * *.**
1d000 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61  .** Force the va
1d001 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1d002 50 31 20 74 6f 20 62 65 20 6e 75 6d 65 72 69 63  P1 to be numeric
1d003 20 28 65 69 74 68 65 72 20 61 6e 0a 2a 2a 20 69   (either an.** i
1d004 6e 74 65 67 65 72 20 6f 72 20 61 20 66 6c 6f 61  nteger or a floa
1d005 74 69 6e 67 2d 70 6f 69 6e 74 20 6e 75 6d 62 65  ting-point numbe
1d006 72 2e 29 0a 2a 2a 20 49 66 20 74 68 65 20 76 61  r.).** If the va
1d007 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62  lue is text or b
1d008 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76  lob, try to conv
1d009 65 72 74 20 69 74 20 74 6f 20 61 6e 20 75 73 69  ert it to an usi
1d00a 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61  ng the.** equiva
1d00b 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 6f  lent of atoi() o
1d00c 72 20 61 74 6f 66 28 29 20 61 6e 64 20 73 74 6f  r atof() and sto
1d00d 72 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68 20  re 0 if no such 
1d00e 63 6f 6e 76 65 72 73 69 6f 6e 20 0a 2a 2a 20 69  conversion .** i
1d00f 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a  s possible..**.*
1d010 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69  * A NULL value i
1d011 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79  s not changed by
1d012 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
1d013 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e  It remains NULL.
1d014 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 4e 75  .*/.case OP_ToNu
1d015 6d 65 72 69 63 3a 20 7b 20 20 20 20 20 20 20 20  meric: {        
1d016 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1d017 65 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52  e as TK_TO_NUMER
1d018 49 43 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28  IC, in1 */.  if(
1d019 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1d01a 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e  (MEM_Null|MEM_In
1d01b 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d 3d 30 20  t|MEM_Real))==0 
1d01c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1d01d 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 70 49  beMemNumerify(pI
1d01e 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  n1);.  }.  break
1d01f 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1d020 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a  LITE_OMIT_CAST *
1d021 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f  /../* Opcode: To
1d022 49 6e 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  Int P1 * * * *.*
1d023 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76  *.** Force the v
1d024 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1d025 20 50 31 20 62 65 20 61 6e 20 69 6e 74 65 67 65   P1 be an intege
1d026 72 2e 20 20 49 66 0a 2a 2a 20 54 68 65 20 76 61  r.  If.** The va
1d027 6c 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79  lue is currently
1d028 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2c 20   a real number, 
1d029 64 72 6f 70 20 69 74 73 20 66 72 61 63 74 69 6f  drop its fractio
1d02a 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20  nal part..** If 
1d02b 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78  the value is tex
1d02c 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74  t or blob, try t
1d02d 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20  o convert it to 
1d02e 61 6e 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67  an integer using
1d02f 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65   the.** equivale
1d030 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64  nt of atoi() and
1d031 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 73   store 0 if no s
1d032 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  uch conversion i
1d033 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a  s possible..**.*
1d034 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69  * A NULL value i
1d035 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79  s not changed by
1d036 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
1d037 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e  It remains NULL.
1d038 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 49 6e  .*/.case OP_ToIn
1d039 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  t: {            
1d03a 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1d03b 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69 6e 31 20   TK_TO_INT, in1 
1d03c 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  */.  if( (pIn1->
1d03d 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1d03e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )==0 ){.    sqli
1d03f 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
1d040 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a  rify(pIn1);.  }.
1d041 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e    break;.}..#ifn
1d042 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1d043 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  CAST./* Opcode: 
1d044 54 6f 52 65 61 6c 20 50 31 20 2a 20 2a 20 2a 20  ToReal P1 * * * 
1d045 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68  *.**.** Force th
1d046 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1d047 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 20 66  ter P1 to be a f
1d048 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75  loating point nu
1d049 6d 62 65 72 2e 0a 2a 2a 20 49 66 20 54 68 65 20  mber..** If The 
1d04a 76 61 6c 75 65 20 69 73 20 63 75 72 72 65 6e 74  value is current
1d04b 6c 79 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 63  ly an integer, c
1d04c 6f 6e 76 65 72 74 20 69 74 2e 0a 2a 2a 20 49 66  onvert it..** If
1d04d 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65   the value is te
1d04e 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20  xt or blob, try 
1d04f 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f  to convert it to
1d050 20 61 6e 20 69 6e 74 65 67 65 72 20 75 73 69 6e   an integer usin
1d051 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c  g the.** equival
1d052 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 61 6e  ent of atoi() an
1d053 64 20 73 74 6f 72 65 20 30 2e 30 20 69 66 20 6e  d store 0.0 if n
1d054 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f  o such conversio
1d055 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a  n is possible..*
1d056 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75  *.** A NULL valu
1d057 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64  e is not changed
1d058 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
1d059 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55  .  It remains NU
1d05a 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54  LL..*/.case OP_T
1d05b 6f 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20  oReal: {        
1d05c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1d05d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c  e as TK_TO_REAL,
1d05e 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70   in1 */.  if( (p
1d05f 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1d060 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20  _Null)==0 ){.   
1d061 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1d062 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20  ealify(pIn1);.  
1d063 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  }.  break;.}.#en
1d064 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1d065 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f  IT_CAST */../* O
1d066 70 63 6f 64 65 3a 20 4c 74 20 50 31 20 50 32 20  pcode: Lt P1 P2 
1d067 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 43  P3 P4 P5.**.** C
1d068 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65  ompare the value
1d069 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  s in register P1
1d06a 20 61 6e 64 20 50 33 2e 20 20 49 66 20 72 65 67   and P3.  If reg
1d06b 28 50 33 29 3c 72 65 67 28 50 31 29 20 74 68 65  (P3)<reg(P1) the
1d06c 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 61 64 64  n.** jump to add
1d06d 72 65 73 73 20 50 32 2e 20 20 0a 2a 2a 0a 2a 2a  ress P2.  .**.**
1d06e 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a   If the SQLITE_J
1d06f 55 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 6f 66  UMPIFNULL bit of
1d070 20 50 35 20 69 73 20 73 65 74 20 61 6e 64 20 65   P5 is set and e
1d071 69 74 68 65 72 20 72 65 67 28 50 31 29 20 6f 72  ither reg(P1) or
1d072 0a 2a 2a 20 72 65 67 28 50 33 29 20 69 73 20 4e  .** reg(P3) is N
1d073 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68  ULL then take th
1d074 65 20 6a 75 6d 70 2e 20 20 49 66 20 74 68 65 20  e jump.  If the 
1d075 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
1d076 4c 20 0a 2a 2a 20 62 69 74 20 69 73 20 63 6c 65  L .** bit is cle
1d077 61 72 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72  ar then fall thr
1d078 75 20 69 66 20 65 69 74 68 65 72 20 6f 70 65 72  u if either oper
1d079 61 6e 64 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  and is NULL..**.
1d07a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 41 46  ** The SQLITE_AF
1d07b 46 5f 4d 41 53 4b 20 70 6f 72 74 69 6f 6e 20 6f  F_MASK portion o
1d07c 66 20 50 35 20 6d 75 73 74 20 62 65 20 61 6e 20  f P5 must be an 
1d07d 61 66 66 69 6e 69 74 79 20 63 68 61 72 61 63 74  affinity charact
1d07e 65 72 20 2d 0a 2a 2a 20 53 51 4c 49 54 45 5f 41  er -.** SQLITE_A
1d07f 46 46 5f 54 45 58 54 2c 20 53 51 4c 49 54 45 5f  FF_TEXT, SQLITE_
1d080 41 46 46 5f 49 4e 54 45 47 45 52 2c 20 61 6e 64  AFF_INTEGER, and
1d081 20 73 6f 20 66 6f 72 74 68 2e 20 41 6e 20 61 74   so forth. An at
1d082 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 0a 2a  tempt is made .*
1d083 2a 20 74 6f 20 63 6f 65 72 63 65 20 62 6f 74 68  * to coerce both
1d084 20 69 6e 70 75 74 73 20 61 63 63 6f 72 64 69 6e   inputs accordin
1d085 67 20 74 6f 20 74 68 69 73 20 61 66 66 69 6e 69  g to this affini
1d086 74 79 20 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a  ty before the.**
1d087 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d   comparison is m
1d088 61 64 65 2e 20 49 66 20 74 68 65 20 53 51 4c 49  ade. If the SQLI
1d089 54 45 5f 41 46 46 5f 4d 41 53 4b 20 69 73 20 30  TE_AFF_MASK is 0
1d08a 78 30 30 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69  x00, then numeri
1d08b 63 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 69 73  c.** affinity is
1d08c 20 75 73 65 64 2e 20 4e 6f 74 65 20 74 68 61 74   used. Note that
1d08d 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 63 6f   the affinity co
1d08e 6e 76 65 72 73 69 6f 6e 73 20 61 72 65 20 73 74  nversions are st
1d08f 6f 72 65 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 74  ored.** back int
1d090 6f 20 74 68 65 20 69 6e 70 75 74 20 72 65 67 69  o the input regi
1d091 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 2e  sters P1 and P3.
1d092 20 20 53 6f 20 74 68 69 73 20 6f 70 63 6f 64 65    So this opcode
1d093 20 63 61 6e 20 63 61 75 73 65 0a 2a 2a 20 70 65   can cause.** pe
1d094 72 73 69 73 74 65 6e 74 20 63 68 61 6e 67 65 73  rsistent changes
1d095 20 74 6f 20 72 65 67 69 73 74 65 72 73 20 50 31   to registers P1
1d096 20 61 6e 64 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 4f   and P3..**.** O
1d097 6e 63 65 20 61 6e 79 20 63 6f 6e 76 65 72 73 69  nce any conversi
1d098 6f 6e 73 20 68 61 76 65 20 74 61 6b 65 6e 20 70  ons have taken p
1d099 6c 61 63 65 2c 20 61 6e 64 20 6e 65 69 74 68 65  lace, and neithe
1d09a 72 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c  r value is NULL,
1d09b 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20   .** the values 
1d09c 61 72 65 20 63 6f 6d 70 61 72 65 64 2e 20 49 66  are compared. If
1d09d 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65   both values are
1d09e 20 62 6c 6f 62 73 20 74 68 65 6e 20 6d 65 6d 63   blobs then memc
1d09f 6d 70 28 29 20 69 73 0a 2a 2a 20 75 73 65 64 20  mp() is.** used 
1d0a0 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
1d0a1 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
1d0a2 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 49 66 20  comparison.  If 
1d0a3 62 6f 74 68 20 76 61 6c 75 65 73 0a 2a 2a 20 61  both values.** a
1d0a4 72 65 20 74 65 78 74 2c 20 74 68 65 6e 20 74 68  re text, then th
1d0a5 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 6f  e appropriate co
1d0a6 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
1d0a7 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a   specified in.**
1d0a8 20 50 34 20 69 73 20 20 75 73 65 64 20 74 6f 20   P4 is  used to 
1d0a9 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  do the compariso
1d0aa 6e 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f 74  n.  If P4 is not
1d0ab 20 73 70 65 63 69 66 69 65 64 20 74 68 65 6e 0a   specified then.
1d0ac 2a 2a 20 6d 65 6d 63 6d 70 28 29 20 69 73 20 75  ** memcmp() is u
1d0ad 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 74  sed to compare t
1d0ae 65 78 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20  ext string.  If 
1d0af 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 0a  both values are.
1d0b0 2a 2a 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 6e  ** numeric, then
1d0b1 20 61 20 6e 75 6d 65 72 69 63 20 63 6f 6d 70 61   a numeric compa
1d0b2 72 69 73 6f 6e 20 69 73 20 75 73 65 64 2e 20 49  rison is used. I
1d0b3 66 20 74 68 65 20 74 77 6f 20 76 61 6c 75 65 73  f the two values
1d0b4 0a 2a 2a 20 61 72 65 20 6f 66 20 64 69 66 66 65  .** are of diffe
1d0b5 72 65 6e 74 20 74 79 70 65 73 2c 20 74 68 65 6e  rent types, then
1d0b6 20 6e 75 6d 62 65 72 73 20 61 72 65 20 63 6f 6e   numbers are con
1d0b7 73 69 64 65 72 65 64 20 6c 65 73 73 20 74 68 61  sidered less tha
1d0b8 6e 0a 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64  n.** strings and
1d0b9 20 73 74 72 69 6e 67 73 20 61 72 65 20 63 6f 6e   strings are con
1d0ba 73 69 64 65 72 65 64 20 6c 65 73 73 20 74 68 61  sidered less tha
1d0bb 6e 20 62 6c 6f 62 73 2e 0a 2a 2a 0a 2a 2a 20 49  n blobs..**.** I
1d0bc 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53 54 4f  f the SQLITE_STO
1d0bd 52 45 50 32 20 62 69 74 20 6f 66 20 50 35 20 69  REP2 bit of P5 i
1d0be 73 20 73 65 74 2c 20 74 68 65 6e 20 64 6f 20 6e  s set, then do n
1d0bf 6f 74 20 6a 75 6d 70 2e 20 20 49 6e 73 74 65 61  ot jump.  Instea
1d0c0 64 2c 0a 2a 2a 20 73 74 6f 72 65 20 61 20 62 6f  d,.** store a bo
1d0c1 6f 6c 65 61 6e 20 72 65 73 75 6c 74 20 28 65 69  olean result (ei
1d0c2 74 68 65 72 20 30 2c 20 6f 72 20 31 2c 20 6f 72  ther 0, or 1, or
1d0c3 20 4e 55 4c 4c 29 20 69 6e 20 72 65 67 69 73 74   NULL) in regist
1d0c4 65 72 20 50 32 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63  er P2..*/./* Opc
1d0c5 6f 64 65 3a 20 4e 65 20 50 31 20 50 32 20 50 33  ode: Ne P1 P2 P3
1d0c6 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69   P4 P5.**.** Thi
1d0c7 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b  s works just lik
1d0c8 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20  e the Lt opcode 
1d0c9 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
1d0ca 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66  jump is taken if
1d0cb 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73  .** the operands
1d0cc 20 69 6e 20 72 65 67 69 73 74 65 72 73 20 50 31   in registers P1
1d0cd 20 61 6e 64 20 50 33 20 61 72 65 20 6e 6f 74 20   and P3 are not 
1d0ce 65 71 75 61 6c 2e 20 20 53 65 65 20 74 68 65 20  equal.  See the 
1d0cf 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 0a 2a 2a  Lt opcode for.**
1d0d0 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
1d0d1 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  rmation..**.** I
1d0d2 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20  f SQLITE_NULLEQ 
1d0d3 69 73 20 73 65 74 20 69 6e 20 50 35 20 74 68 65  is set in P5 the
1d0d4 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  n the result of 
1d0d5 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 6c  comparison is al
1d0d6 77 61 79 73 20 65 69 74 68 65 72 0a 2a 2a 20 74  ways either.** t
1d0d7 72 75 65 20 6f 72 20 66 61 6c 73 65 20 61 6e 64  rue or false and
1d0d8 20 69 73 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20   is never NULL. 
1d0d9 20 49 66 20 62 6f 74 68 20 6f 70 65 72 61 6e 64   If both operand
1d0da 73 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20  s are NULL then 
1d0db 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66  the result.** of
1d0dc 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 66   comparison is f
1d0dd 61 6c 73 65 2e 20 20 49 66 20 65 69 74 68 65 72  alse.  If either
1d0de 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
1d0df 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
1d0e0 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 49 66 20   is true..** If 
1d0e1 6e 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20  neither operand 
1d0e2 69 73 20 4e 55 4c 4c 20 74 68 65 20 74 68 65 20  is NULL the the 
1d0e3 72 65 73 75 6c 74 20 69 73 20 74 68 65 20 73 61  result is the sa
1d0e4 6d 65 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62  me as it would b
1d0e5 65 20 69 66 0a 2a 2a 20 74 68 65 20 53 51 4c 49  e if.** the SQLI
1d0e6 54 45 5f 4e 55 4c 4c 45 51 20 66 6c 61 67 20 77  TE_NULLEQ flag w
1d0e7 65 72 65 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d  ere omitted from
1d0e8 20 50 35 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64   P5..*/./* Opcod
1d0e9 65 3a 20 45 71 20 50 31 20 50 32 20 50 33 20 50  e: Eq P1 P2 P3 P
1d0ea 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  4 P5.**.** This 
1d0eb 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20  works just like 
1d0ec 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78  the Lt opcode ex
1d0ed 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75  cept that the ju
1d0ee 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a  mp is taken if.*
1d0ef 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69  * the operands i
1d0f0 6e 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61  n registers P1 a
1d0f1 6e 64 20 50 33 20 61 72 65 20 65 71 75 61 6c 2e  nd P3 are equal.
1d0f2 0a 2a 2a 20 53 65 65 20 74 68 65 20 4c 74 20 6f  .** See the Lt o
1d0f3 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
1d0f4 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1d0f5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ..**.** If SQLIT
1d0f6 45 5f 4e 55 4c 4c 45 51 20 69 73 20 73 65 74 20  E_NULLEQ is set 
1d0f7 69 6e 20 50 35 20 74 68 65 6e 20 74 68 65 20 72  in P5 then the r
1d0f8 65 73 75 6c 74 20 6f 66 20 63 6f 6d 70 61 72 69  esult of compari
1d0f9 73 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 65 69  son is always ei
1d0fa 74 68 65 72 0a 2a 2a 20 74 72 75 65 20 6f 72 20  ther.** true or 
1d0fb 66 61 6c 73 65 20 61 6e 64 20 69 73 20 6e 65 76  false and is nev
1d0fc 65 72 20 4e 55 4c 4c 2e 20 20 49 66 20 62 6f 74  er NULL.  If bot
1d0fd 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 4e  h operands are N
1d0fe 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 65 73  ULL then the res
1d0ff 75 6c 74 0a 2a 2a 20 6f 66 20 63 6f 6d 70 61 72  ult.** of compar
1d100 69 73 6f 6e 20 69 73 20 74 72 75 65 2e 20 20 49  ison is true.  I
1d101 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  f either operand
1d102 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68   is NULL then th
1d103 65 20 72 65 73 75 6c 74 20 69 73 20 66 61 6c 73  e result is fals
1d104 65 2e 0a 2a 2a 20 49 66 20 6e 65 69 74 68 65 72  e..** If neither
1d105 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
1d106 20 74 68 65 20 74 68 65 20 72 65 73 75 6c 74 20   the the result 
1d107 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 69  is the same as i
1d108 74 20 77 6f 75 6c 64 20 62 65 20 69 66 0a 2a 2a  t would be if.**
1d109 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c   the SQLITE_NULL
1d10a 45 51 20 66 6c 61 67 20 77 65 72 65 20 6f 6d 69  EQ flag were omi
1d10b 74 74 65 64 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f  tted from P5..*/
1d10c 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50  ./* Opcode: Le P
1d10d 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1d10e 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a  .** This works j
1d10f 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20  ust like the Lt 
1d110 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68  opcode except th
1d111 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74  at the jump is t
1d112 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63  aken if.** the c
1d113 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74  ontent of regist
1d114 65 72 20 50 33 20 69 73 20 6c 65 73 73 20 74 68  er P3 is less th
1d115 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
1d116 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a  he content of.**
1d117 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 53   register P1.  S
1d118 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65  ee the Lt opcode
1d119 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
1d11a 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
1d11b 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20 50 31  /* Opcode: Gt P1
1d11c 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
1d11d 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75  ** This works ju
1d11e 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f  st like the Lt o
1d11f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61  pcode except tha
1d120 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  t the jump is ta
1d121 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f  ken if.** the co
1d122 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65  ntent of registe
1d123 72 20 50 33 20 69 73 20 67 72 65 61 74 65 72 20  r P3 is greater 
1d124 74 68 61 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74  than the content
1d125 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20   of.** register 
1d126 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20  P1.  See the Lt 
1d127 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74  opcode for addit
1d128 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
1d129 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  n..*/./* Opcode:
1d12a 20 47 65 20 50 31 20 50 32 20 50 33 20 50 34 20   Ge P1 P2 P3 P4 
1d12b 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f  P5.**.** This wo
1d12c 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  rks just like th
1d12d 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65  e Lt opcode exce
1d12e 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70  pt that the jump
1d12f 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20   is taken if.** 
1d130 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1d131 65 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72  egister P3 is gr
1d132 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
1d133 75 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74 65  ual to the conte
1d134 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65  nt of.** registe
1d135 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c  r P1.  See the L
1d136 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64  t opcode for add
1d137 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
1d138 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ion..*/.case OP_
1d139 45 71 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  Eq:             
1d13a 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1d13b 45 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69  EQ, jump, in1, i
1d13c 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65  n3 */.case OP_Ne
1d13d 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
1d13e 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45  /* same as TK_NE
1d13f 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33  , jump, in1, in3
1d140 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20   */.case OP_Lt: 
1d141 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d142 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20   same as TK_LT, 
1d143 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
1d144 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20  /.case OP_Le:   
1d145 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1d146 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75  ame as TK_LE, ju
1d147 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a  mp, in1, in3 */.
1d148 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 20  case OP_Gt:     
1d149 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1d14a 65 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70  e as TK_GT, jump
1d14b 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61  , in1, in3 */.ca
1d14c 73 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20  se OP_Ge: {     
1d14d 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1d14e 61 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20  as TK_GE, jump, 
1d14f 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20  in1, in3 */.#if 
1d150 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1d151 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1d152 20 75 2e 61 69 20 2a 2f 0a 20 20 69 6e 74 20 72   u.ai */.  int r
1d153 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  es;            /
1d154 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20  * Result of the 
1d155 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 49  comparison of pI
1d156 6e 31 20 61 67 61 69 6e 73 74 20 70 49 6e 33 20  n1 against pIn3 
1d157 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69  */.  char affini
1d158 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 69  ty;      /* Affi
1d159 6e 69 74 79 20 74 6f 20 75 73 65 20 66 6f 72 20  nity to use for 
1d15a 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 23 65  comparison */.#e
1d15b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1d15c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d15d 74 6f 20 75 2e 61 69 20 2a 2f 0a 0a 20 20 69 66  to u.ai */..  if
1d15e 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c  ( (pIn1->flags |
1d15f 20 70 49 6e 33 2d 3e 66 6c 61 67 73 29 26 4d 45   pIn3->flags)&ME
1d160 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 2f 2a  M_Null ){.    /*
1d161 20 4f 6e 65 20 6f 72 20 62 6f 74 68 20 6f 70 65   One or both ope
1d162 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20 2a  rands are NULL *
1d163 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70  /.    if( pOp->p
1d164 35 20 26 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45  5 & SQLITE_NULLE
1d165 51 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  Q ){.      /* If
1d166 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69   SQLITE_NULLEQ i
1d167 73 20 73 65 74 20 28 77 68 69 63 68 20 77 69 6c  s set (which wil
1d168 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66  l only happen if
1d169 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 69 73   the operator is
1d16a 0a 20 20 20 20 20 20 2a 2a 20 4f 50 5f 45 71 20  .      ** OP_Eq 
1d16b 6f 72 20 4f 50 5f 4e 65 29 20 74 68 65 6e 20 74  or OP_Ne) then t
1d16c 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 6f 72 20  ake the jump or 
1d16d 6e 6f 74 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  not depending on
1d16e 20 77 68 65 74 68 65 72 0a 20 20 20 20 20 20 2a   whether.      *
1d16f 2a 20 6f 72 20 6e 6f 74 20 62 6f 74 68 20 6f 70  * or not both op
1d170 65 72 61 6e 64 73 20 61 72 65 20 6e 75 6c 6c 2e  erands are null.
1d171 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1d172 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63  assert( pOp->opc
1d173 6f 64 65 3d 3d 4f 50 5f 45 71 20 7c 7c 20 70 4f  ode==OP_Eq || pO
1d174 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65  p->opcode==OP_Ne
1d175 20 29 3b 0a 20 20 20 20 20 20 75 2e 61 69 2e 72   );.      u.ai.r
1d176 65 73 20 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67  es = (pIn1->flag
1d177 73 20 26 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20  s & pIn3->flags 
1d178 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 3b 0a  & MEM_Null)==0;.
1d179 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1d17a 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45   /* SQLITE_NULLE
1d17b 51 20 69 73 20 63 6c 65 61 72 20 61 6e 64 20 61  Q is clear and a
1d17c 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 65 72  t least one oper
1d17d 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 0a 20 20 20  and is NULL,.   
1d17e 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 72     ** then the r
1d17f 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20  esult is always 
1d180 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 2a 2a 20 54  NULL..      ** T
1d181 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e  he jump is taken
1d182 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a   if the SQLITE_J
1d183 55 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 69 73  UMPIFNULL bit is
1d184 20 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20   set..      */. 
1d185 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35       if( pOp->p5
1d186 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50   & SQLITE_STOREP
1d187 32 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4f 75  2 ){.        pOu
1d188 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  t = &p->aMem[pOp
1d189 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20 20 4d  ->p2];.        M
1d18a 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
1d18b 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20  ut, MEM_Null);. 
1d18c 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f         REGISTER_
1d18d 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70  TRACE(pOp->p2, p
1d18e 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Out);.      }els
1d18f 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20  e if( pOp->p5 & 
1d190 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
1d191 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63 20  L ){.        pc 
1d192 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20  = pOp->p2-1;.   
1d193 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
1d194 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1d195 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68 65 72 20  .    /* Neither 
1d196 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2e  operand is NULL.
1d197 20 20 44 6f 20 61 20 63 6f 6d 70 61 72 69 73 6f    Do a compariso
1d198 6e 2e 20 2a 2f 0a 20 20 20 20 75 2e 61 69 2e 61  n. */.    u.ai.a
1d199 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70  ffinity = pOp->p
1d19a 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d  5 & SQLITE_AFF_M
1d19b 41 53 4b 3b 0a 20 20 20 20 69 66 28 20 75 2e 61  ASK;.    if( u.a
1d19c 69 2e 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20  i.affinity ){.  
1d19d 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74      applyAffinit
1d19e 79 28 70 49 6e 31 2c 20 75 2e 61 69 2e 61 66 66  y(pIn1, u.ai.aff
1d19f 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29  inity, encoding)
1d1a0 3b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66 66  ;.      applyAff
1d1a1 69 6e 69 74 79 28 70 49 6e 33 2c 20 75 2e 61 69  inity(pIn3, u.ai
1d1a2 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64  .affinity, encod
1d1a3 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ing);.      if( 
1d1a4 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1d1a5 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1d1a6 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72      }..    asser
1d1a7 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1d1a8 50 34 5f 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f  P4_COLLSEQ || pO
1d1a9 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29  p->p4.pColl==0 )
1d1aa 3b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62  ;.    ExpandBlob
1d1ab 28 70 49 6e 31 29 3b 0a 20 20 20 20 45 78 70 61  (pIn1);.    Expa
1d1ac 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20  ndBlob(pIn3);.  
1d1ad 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 73 71 6c    u.ai.res = sql
1d1ae 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70  ite3MemCompare(p
1d1af 49 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d 3e  In3, pIn1, pOp->
1d1b0 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20  p4.pColl);.  }. 
1d1b1 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70   switch( pOp->op
1d1b2 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65  code ){.    case
1d1b3 20 4f 50 5f 45 71 3a 20 20 20 20 75 2e 61 69 2e   OP_Eq:    u.ai.
1d1b4 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3d 3d  res = u.ai.res==
1d1b5 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  0;     break;.  
1d1b6 20 20 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20    case OP_Ne:   
1d1b7 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69   u.ai.res = u.ai
1d1b8 2e 72 65 73 21 3d 30 3b 20 20 20 20 20 62 72 65  .res!=0;     bre
1d1b9 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f  ak;.    case OP_
1d1ba 4c 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20  Lt:    u.ai.res 
1d1bb 3d 20 75 2e 61 69 2e 72 65 73 3c 30 3b 20 20 20  = u.ai.res<0;   
1d1bc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
1d1bd 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 75 2e 61  se OP_Le:    u.a
1d1be 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73  i.res = u.ai.res
1d1bf 3c 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a  <=0;     break;.
1d1c0 20 20 20 20 63 61 73 65 20 4f 50 5f 47 74 3a 20      case OP_Gt: 
1d1c1 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e     u.ai.res = u.
1d1c2 61 69 2e 72 65 73 3e 30 3b 20 20 20 20 20 20 62  ai.res>0;      b
1d1c3 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c  reak;.    defaul
1d1c4 74 3a 20 20 20 20 20 20 20 75 2e 61 69 2e 72 65  t:       u.ai.re
1d1c5 73 20 3d 20 75 2e 61 69 2e 72 65 73 3e 3d 30 3b  s = u.ai.res>=0;
1d1c6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a       break;.  }.
1d1c7 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26  .  if( pOp->p5 &
1d1c8 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20   SQLITE_STOREP2 
1d1c9 29 7b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70  ){.    pOut = &p
1d1ca 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b  ->aMem[pOp->p2];
1d1cb 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46  .    MemSetTypeF
1d1cc 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
1d1cd 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e  t);.    pOut->u.
1d1ce 69 20 3d 20 75 2e 61 69 2e 72 65 73 3b 0a 20 20  i = u.ai.res;.  
1d1cf 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1d1d0 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b  (pOp->p2, pOut);
1d1d1 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61  .  }else if( u.a
1d1d2 69 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20  i.res ){.    pc 
1d1d3 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d  = pOp->p2-1;.  }
1d1d4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1d1d5 4f 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 74  Opcode: Permutat
1d1d6 69 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a  ion * * * P4 *.*
1d1d7 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65 72  *.** Set the per
1d1d8 6d 75 74 61 74 69 6f 6e 20 75 73 65 64 20 62 79  mutation used by
1d1d9 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20   the OP_Compare 
1d1da 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 74  operator to be t
1d1db 68 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69  he array.** of i
1d1dc 6e 74 65 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a  ntegers in P4..*
1d1dd 2a 0a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 61  *.** The permuta
1d1de 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c  tion is only val
1d1df 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78  id until the nex
1d1e0 74 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e  t OP_Permutation
1d1e1 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a  , OP_Compare,.**
1d1e2 20 4f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f   OP_Halt, or OP_
1d1e3 52 65 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 69  ResultRow.  Typi
1d1e4 63 61 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 72  cally the OP_Per
1d1e5 6d 75 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20  mutation should 
1d1e6 6f 63 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61  occur.** immedia
1d1e7 74 65 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 68  tely prior to th
1d1e8 65 20 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f  e OP_Compare..*/
1d1e9 0a 63 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 61  .case OP_Permuta
1d1ea 74 69 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74  tion: {.  assert
1d1eb 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1d1ec 34 5f 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 20  4_INTARRAY );.  
1d1ed 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
1d1ee 61 69 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 65  ai );.  aPermute
1d1ef 20 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20   = pOp->p4.ai;. 
1d1f0 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1d1f1 63 6f 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 31  code: Compare P1
1d1f2 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1d1f3 2a 20 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 63  * Compare to vec
1d1f4 74 6f 72 73 20 6f 66 20 72 65 67 69 73 74 65 72  tors of register
1d1f5 73 20 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 65  s in reg(P1)..re
1d1f6 67 28 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20  g(P1+P3-1) (all 
1d1f7 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29  this.** one "A")
1d1f8 20 61 6e 64 20 69 6e 20 72 65 67 28 50 32 29 2e   and in reg(P2).
1d1f9 2e 72 65 67 28 50 32 2b 50 33 2d 31 29 20 28 22  .reg(P2+P3-1) ("
1d1fa 42 22 29 2e 20 20 53 61 76 65 20 74 68 65 20 72  B").  Save the r
1d1fb 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20  esult of.** the 
1d1fc 63 6f 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 75  comparison for u
1d1fd 73 65 20 62 79 20 74 68 65 20 6e 65 78 74 20 4f  se by the next O
1d1fe 50 5f 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 2e  P_Jump instruct.
1d1ff 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b  .**.** P4 is a K
1d200 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
1d201 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f   that defines co
1d202 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
1d203 73 20 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72  s and sort.** or
1d204 64 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d  ders for the com
1d205 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65  parison.  The pe
1d206 72 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65  rmutation applie
1d207 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a  s to registers.*
1d208 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79  * only.  The Key
1d209 49 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72  Info elements ar
1d20a 65 20 75 73 65 64 20 73 65 71 75 65 6e 74 69 61  e used sequentia
1d20b 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  lly..**.** The c
1d20c 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 73  omparison is a s
1d20d 6f 72 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20  ort comparison, 
1d20e 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65  so NULLs compare
1d20f 20 65 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73   equal,.** NULLs
1d210 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6e   are less than n
1d211 75 6d 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 20  umbers, numbers 
1d212 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 73 74  are less than st
1d213 72 69 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74  rings,.** and st
1d214 72 69 6e 67 73 20 61 72 65 20 6c 65 73 73 20 74  rings are less t
1d215 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61  han blobs..*/.ca
1d216 73 65 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b  se OP_Compare: {
1d217 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1d218 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d219 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 20   into u.aj */.  
1d21a 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a  int n;.  int i;.
1d21b 20 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20    int p1;.  int 
1d21c 70 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49  p2;.  const KeyI
1d21d 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20  nfo *pKeyInfo;. 
1d21e 20 69 6e 74 20 69 64 78 3b 0a 20 20 43 6f 6c 6c   int idx;.  Coll
1d21f 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f  Seq *pColl;    /
1d220 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * Collating sequ
1d221 65 6e 63 65 20 74 6f 20 75 73 65 20 6f 6e 20 74  ence to use on t
1d222 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e  his term */.  in
1d223 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20  t bRev;         
1d224 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53   /* True for DES
1d225 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64  CENDING sort ord
1d226 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  er */.#endif /* 
1d227 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d228 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20  moved into u.aj 
1d229 2a 2f 0a 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20 70  */..  u.aj.n = p
1d22a 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e 70  Op->p3;.  u.aj.p
1d22b 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70  KeyInfo = pOp->p
1d22c 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73  4.pKeyInfo;.  as
1d22d 73 65 72 74 28 20 75 2e 61 6a 2e 6e 3e 30 20 29  sert( u.aj.n>0 )
1d22e 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a  ;.  assert( u.aj
1d22f 2e 70 4b 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a  .pKeyInfo!=0 );.
1d230 20 20 75 2e 61 6a 2e 70 31 20 3d 20 70 4f 70 2d    u.aj.p1 = pOp-
1d231 3e 70 31 3b 0a 20 20 75 2e 61 6a 2e 70 32 20 3d  >p1;.  u.aj.p2 =
1d232 20 70 4f 70 2d 3e 70 32 3b 0a 23 69 66 20 53 51   pOp->p2;.#if SQ
1d233 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28  LITE_DEBUG.  if(
1d234 20 61 50 65 72 6d 75 74 65 20 29 7b 0a 20 20 20   aPermute ){.   
1d235 20 69 6e 74 20 6b 2c 20 6d 78 20 3d 20 30 3b 0a   int k, mx = 0;.
1d236 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 75      for(k=0; k<u
1d237 2e 61 6a 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28 20  .aj.n; k++) if( 
1d238 61 50 65 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20 29  aPermute[k]>mx )
1d239 20 6d 78 20 3d 20 61 50 65 72 6d 75 74 65 5b 6b   mx = aPermute[k
1d23a 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  ];.    assert( u
1d23b 2e 61 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a  .aj.p1>0 && u.aj
1d23c 2e 70 31 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b  .p1+mx<=p->nMem+
1d23d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  1 );.    assert(
1d23e 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 2e   u.aj.p2>0 && u.
1d23f 61 6a 2e 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65  aj.p2+mx<=p->nMe
1d240 6d 2b 31 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  m+1 );.  }else{.
1d241 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a      assert( u.aj
1d242 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 31  .p1>0 && u.aj.p1
1d243 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d  +u.aj.n<=p->nMem
1d244 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  +1 );.    assert
1d245 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75  ( u.aj.p2>0 && u
1d246 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 6e 3c 3d 70  .aj.p2+u.aj.n<=p
1d247 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d 0a  ->nMem+1 );.  }.
1d248 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1d249 5f 44 45 42 55 47 20 2a 2f 0a 20 20 66 6f 72 28  _DEBUG */.  for(
1d24a 75 2e 61 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e 69  u.aj.i=0; u.aj.i
1d24b 3c 75 2e 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69 2b  <u.aj.n; u.aj.i+
1d24c 2b 29 7b 0a 20 20 20 20 75 2e 61 6a 2e 69 64 78  +){.    u.aj.idx
1d24d 20 3d 20 61 50 65 72 6d 75 74 65 20 3f 20 61 50   = aPermute ? aP
1d24e 65 72 6d 75 74 65 5b 75 2e 61 6a 2e 69 5d 20 3a  ermute[u.aj.i] :
1d24f 20 75 2e 61 6a 2e 69 3b 0a 20 20 20 20 52 45 47   u.aj.i;.    REG
1d250 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a  ISTER_TRACE(u.aj
1d251 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 70  .p1+u.aj.idx, &p
1d252 2d 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75  ->aMem[u.aj.p1+u
1d253 2e 61 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52  .aj.idx]);.    R
1d254 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e  EGISTER_TRACE(u.
1d255 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 2c 20  aj.p2+u.aj.idx, 
1d256 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32  &p->aMem[u.aj.p2
1d257 2b 75 2e 61 6a 2e 69 64 78 5d 29 3b 0a 20 20 20  +u.aj.idx]);.   
1d258 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 69 3c   assert( u.aj.i<
1d259 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 6e  u.aj.pKeyInfo->n
1d25a 46 69 65 6c 64 20 29 3b 0a 20 20 20 20 75 2e 61  Field );.    u.a
1d25b 6a 2e 70 43 6f 6c 6c 20 3d 20 75 2e 61 6a 2e 70  j.pColl = u.aj.p
1d25c 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 75  KeyInfo->aColl[u
1d25d 2e 61 6a 2e 69 5d 3b 0a 20 20 20 20 75 2e 61 6a  .aj.i];.    u.aj
1d25e 2e 62 52 65 76 20 3d 20 75 2e 61 6a 2e 70 4b 65  .bRev = u.aj.pKe
1d25f 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65  yInfo->aSortOrde
1d260 72 5b 75 2e 61 6a 2e 69 5d 3b 0a 20 20 20 20 69  r[u.aj.i];.    i
1d261 43 6f 6d 70 61 72 65 20 3d 20 73 71 6c 69 74 65  Compare = sqlite
1d262 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 70 2d 3e  3MemCompare(&p->
1d263 61 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61  aMem[u.aj.p1+u.a
1d264 6a 2e 69 64 78 5d 2c 20 26 70 2d 3e 61 4d 65 6d  j.idx], &p->aMem
1d265 5b 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64  [u.aj.p2+u.aj.id
1d266 78 5d 2c 20 75 2e 61 6a 2e 70 43 6f 6c 6c 29 3b  x], u.aj.pColl);
1d267 0a 20 20 20 20 69 66 28 20 69 43 6f 6d 70 61 72  .    if( iCompar
1d268 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 75  e ){.      if( u
1d269 2e 61 6a 2e 62 52 65 76 20 29 20 69 43 6f 6d 70  .aj.bRev ) iComp
1d26a 61 72 65 20 3d 20 2d 69 43 6f 6d 70 61 72 65 3b  are = -iCompare;
1d26b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1d26c 20 20 7d 0a 20 20 7d 0a 20 20 61 50 65 72 6d 75    }.  }.  aPermu
1d26d 74 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b  te = 0;.  break;
1d26e 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a  .}../* Opcode: J
1d26f 75 6d 70 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ump P1 P2 P3 * *
1d270 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74  .**.** Jump to t
1d271 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61  he instruction a
1d272 74 20 61 64 64 72 65 73 73 20 50 31 2c 20 50 32  t address P1, P2
1d273 2c 20 6f 72 20 50 33 20 64 65 70 65 6e 64 69 6e  , or P3 dependin
1d274 67 20 6f 6e 20 77 68 65 74 68 65 72 0a 2a 2a 20  g on whether.** 
1d275 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  in the most rece
1d276 6e 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20 69 6e  nt OP_Compare in
1d277 73 74 72 75 63 74 69 6f 6e 20 74 68 65 20 50 31  struction the P1
1d278 20 76 65 63 74 6f 72 20 77 61 73 20 6c 65 73 73   vector was less
1d279 20 74 68 61 6e 0a 2a 2a 20 65 71 75 61 6c 20 74   than.** equal t
1d27a 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74 68  o, or greater th
1d27b 61 6e 20 74 68 65 20 50 32 20 76 65 63 74 6f 72  an the P2 vector
1d27c 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a  , respectively..
1d27d 2a 2f 0a 63 61 73 65 20 4f 50 5f 4a 75 6d 70 3a  */.case OP_Jump:
1d27e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
1d27f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20  * jump */.  if( 
1d280 69 43 6f 6d 70 61 72 65 3c 30 20 29 7b 0a 20 20  iCompare<0 ){.  
1d281 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 31 20 2d    pc = pOp->p1 -
1d282 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   1;.  }else if( 
1d283 69 43 6f 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20  iCompare==0 ){. 
1d284 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1d285 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  - 1;.  }else{.  
1d286 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d    pc = pOp->p3 -
1d287 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1d288 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41  .}../* Opcode: A
1d289 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  nd P1 P2 P3 * *.
1d28a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c  **.** Take the l
1d28b 6f 67 69 63 61 6c 20 41 4e 44 20 6f 66 20 74 68  ogical AND of th
1d28c 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
1d28d 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 32 20  sters P1 and P2 
1d28e 61 6e 64 0a 2a 2a 20 77 72 69 74 65 20 74 68 65  and.** write the
1d28f 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 72 65 67   result into reg
1d290 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20  ister P3..**.** 
1d291 49 66 20 65 69 74 68 65 72 20 50 31 20 6f 72 20  If either P1 or 
1d292 50 32 20 69 73 20 30 20 28 66 61 6c 73 65 29 20  P2 is 0 (false) 
1d293 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20  then the result 
1d294 69 73 20 30 20 65 76 65 6e 20 69 66 0a 2a 2a 20  is 0 even if.** 
1d295 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75 74 20  the other input 
1d296 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c  is NULL.  A NULL
1d297 20 61 6e 64 20 74 72 75 65 20 6f 72 20 74 77 6f   and true or two
1d298 20 4e 55 4c 4c 73 20 67 69 76 65 0a 2a 2a 20 61   NULLs give.** a
1d299 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f   NULL output..*/
1d29a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 72 20 50  ./* Opcode: Or P
1d29b 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1d29c 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 63  * Take the logic
1d29d 61 6c 20 4f 52 20 6f 66 20 74 68 65 20 76 61 6c  al OR of the val
1d29e 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20  ues in register 
1d29f 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a  P1 and P2 and.**
1d2a0 20 73 74 6f 72 65 20 74 68 65 20 61 6e 73 77 65   store the answe
1d2a1 72 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  r in register P3
1d2a2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65  ..**.** If eithe
1d2a3 72 20 50 31 20 6f 72 20 50 32 20 69 73 20 6e 6f  r P1 or P2 is no
1d2a4 6e 7a 65 72 6f 20 28 74 72 75 65 29 20 74 68 65  nzero (true) the
1d2a5 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  n the result is 
1d2a6 31 20 28 74 72 75 65 29 0a 2a 2a 20 65 76 65 6e  1 (true).** even
1d2a7 20 69 66 20 74 68 65 20 6f 74 68 65 72 20 69 6e   if the other in
1d2a8 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20  put is NULL.  A 
1d2a9 4e 55 4c 4c 20 61 6e 64 20 66 61 6c 73 65 20 6f  NULL and false o
1d2aa 72 20 74 77 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67  r two NULLs.** g
1d2ab 69 76 65 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75  ive a NULL outpu
1d2ac 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 6e  t..*/.case OP_An
1d2ad 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d:              
1d2ae 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e  /* same as TK_AN
1d2af 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74  D, in1, in2, out
1d2b0 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a  3 */.case OP_Or:
1d2b1 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
1d2b2 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c  * same as TK_OR,
1d2b3 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
1d2b4 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1d2b5 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d2b6 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a  ed into u.ak */.
1d2b7 20 20 69 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20    int v1;    /* 
1d2b8 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a 20 20 30  Left operand:  0
1d2b9 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45  ==FALSE, 1==TRUE
1d2ba 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20  , 2==UNKNOWN or 
1d2bb 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 76 32  NULL */.  int v2
1d2bc 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70  ;    /* Right op
1d2bd 65 72 61 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c  erand: 0==FALSE,
1d2be 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b   1==TRUE, 2==UNK
1d2bf 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  NOWN or NULL */.
1d2c0 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1d2c1 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1d2c2 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a 0a 20 20  into u.ak */..  
1d2c3 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  if( pIn1->flags 
1d2c4 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
1d2c5 20 20 75 2e 61 6b 2e 76 31 20 3d 20 32 3b 0a 20    u.ak.v1 = 2;. 
1d2c6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b   }else{.    u.ak
1d2c7 2e 76 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  .v1 = sqlite3Vdb
1d2c8 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21  eIntValue(pIn1)!
1d2c9 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49  =0;.  }.  if( pI
1d2ca 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n2->flags & MEM_
1d2cb 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 61 6b  Null ){.    u.ak
1d2cc 2e 76 32 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65  .v2 = 2;.  }else
1d2cd 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 32 20 3d 20  {.    u.ak.v2 = 
1d2ce 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
1d2cf 6c 75 65 28 70 49 6e 32 29 21 3d 30 3b 0a 20 20  lue(pIn2)!=0;.  
1d2d0 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63  }.  if( pOp->opc
1d2d1 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a 20  ode==OP_And ){. 
1d2d2 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
1d2d3 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 6e  unsigned char an
1d2d4 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c  d_logic[] = { 0,
1d2d5 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20   0, 0, 0, 1, 2, 
1d2d6 30 2c 20 32 2c 20 32 20 7d 3b 0a 20 20 20 20 75  0, 2, 2 };.    u
1d2d7 2e 61 6b 2e 76 31 20 3d 20 61 6e 64 5f 6c 6f 67  .ak.v1 = and_log
1d2d8 69 63 5b 75 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61  ic[u.ak.v1*3+u.a
1d2d9 6b 2e 76 32 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  k.v2];.  }else{.
1d2da 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
1d2db 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f   unsigned char o
1d2dc 72 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c  r_logic[] = { 0,
1d2dd 20 31 2c 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 2, 1, 1, 1, 
1d2de 32 2c 20 31 2c 20 32 20 7d 3b 0a 20 20 20 20 75  2, 1, 2 };.    u
1d2df 2e 61 6b 2e 76 31 20 3d 20 6f 72 5f 6c 6f 67 69  .ak.v1 = or_logi
1d2e0 63 5b 75 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 6b  c[u.ak.v1*3+u.ak
1d2e1 2e 76 32 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  .v2];.  }.  if( 
1d2e2 75 2e 61 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20  u.ak.v1==2 ){.  
1d2e3 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1d2e4 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  (pOut, MEM_Null)
1d2e5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1d2e6 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e  Out->u.i = u.ak.
1d2e7 76 31 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79  v1;.    MemSetTy
1d2e8 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1d2e9 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65  _Int);.  }.  bre
1d2ea 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1d2eb 3a 20 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20  : Not P1 P2 * * 
1d2ec 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  *.**.** Interpre
1d2ed 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  t the value in r
1d2ee 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 20  egister P1 as a 
1d2ef 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20  boolean value.  
1d2f0 53 74 6f 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f  Store the.** boo
1d2f1 6c 65 61 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20  lean complement 
1d2f2 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20  in register P2. 
1d2f3 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   If the value in
1d2f4 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1d2f5 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61  .** NULL, then a
1d2f6 20 4e 55 4c 4c 20 69 73 20 73 74 6f 72 65 64 20   NULL is stored 
1d2f7 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  in P2..*/.case O
1d2f8 50 5f 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20  P_Not: {        
1d2f9 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1d2fa 61 73 20 54 4b 5f 4e 4f 54 2c 20 69 6e 31 20 2a  as TK_NOT, in1 *
1d2fb 2f 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  /.  pOut = &p->a
1d2fc 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1d2fd 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  if( pIn1->flags 
1d2fe 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
1d2ff 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1d300 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20  SetNull(pOut);. 
1d301 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
1d302 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
1d303 36 34 28 70 4f 75 74 2c 20 21 73 71 6c 69 74 65  64(pOut, !sqlite
1d304 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49  3VdbeIntValue(pI
1d305 6e 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61  n1));.  }.  brea
1d306 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d307 20 42 69 74 4e 6f 74 20 50 31 20 50 32 20 2a 20   BitNot P1 P2 * 
1d308 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70  * *.**.** Interp
1d309 72 65 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ret the content 
1d30a 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 61  of register P1 a
1d30b 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 53  s an integer.  S
1d30c 74 6f 72 65 20 74 68 65 0a 2a 2a 20 6f 6e 65 73  tore the.** ones
1d30d 2d 63 6f 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74  -complement of t
1d30e 68 65 20 50 31 20 76 61 6c 75 65 20 69 6e 74 6f  he P1 value into
1d30f 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49   register P2.  I
1d310 66 20 50 31 20 68 6f 6c 64 73 0a 2a 2a 20 61 20  f P1 holds.** a 
1d311 4e 55 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65 20  NULL then store 
1d312 61 20 4e 55 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f  a NULL in P2..*/
1d313 0a 63 61 73 65 20 4f 50 5f 42 69 74 4e 6f 74 3a  .case OP_BitNot:
1d314 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
1d315 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
1d316 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 4f  NOT, in1 */.  pO
1d317 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1d318 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20 70 49  p->p2];.  if( pI
1d319 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1d31a 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  Null ){.    sqli
1d31b 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
1d31c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65  l(pOut);.  }else
1d31d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1d31e 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f 75  eMemSetInt64(pOu
1d31f 74 2c 20 7e 73 71 6c 69 74 65 33 56 64 62 65 49  t, ~sqlite3VdbeI
1d320 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29 3b 0a  ntValue(pIn1));.
1d321 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1d322 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 20 50 31  /* Opcode: If P1
1d323 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1d324 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74   Jump to P2 if t
1d325 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1d326 73 74 65 72 20 50 31 20 69 73 20 74 72 75 65 2e  ster P1 is true.
1d327 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a 2a    The value is.*
1d328 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  * is considered 
1d329 74 72 75 65 20 69 66 20 69 74 20 69 73 20 6e 75  true if it is nu
1d32a 6d 65 72 69 63 20 61 6e 64 20 6e 6f 6e 2d 7a 65  meric and non-ze
1d32b 72 6f 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75  ro.  If the valu
1d32c 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 4e 55  e.** in P1 is NU
1d32d 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65  LL then take the
1d32e 20 6a 75 6d 70 20 69 66 20 50 33 20 69 73 20 74   jump if P3 is t
1d32f 72 75 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  rue..*/./* Opcod
1d330 65 3a 20 49 66 4e 6f 74 20 50 31 20 50 32 20 50  e: IfNot P1 P2 P
1d331 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  3 * *.**.** Jump
1d332 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 61   to P2 if the va
1d333 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1d334 50 31 20 69 73 20 46 61 6c 73 65 2e 20 20 54 68  P1 is False.  Th
1d335 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73  e value is.** is
1d336 20 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75 65   considered true
1d337 20 69 66 20 69 74 20 68 61 73 20 61 20 6e 75 6d   if it has a num
1d338 65 72 69 63 20 76 61 6c 75 65 20 6f 66 20 7a 65  eric value of ze
1d339 72 6f 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75  ro.  If the valu
1d33a 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 4e 55  e.** in P1 is NU
1d33b 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65  LL then take the
1d33c 20 6a 75 6d 70 20 69 66 20 50 33 20 69 73 20 74   jump if P3 is t
1d33d 72 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  rue..*/.case OP_
1d33e 49 66 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  If:             
1d33f 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31      /* jump, in1
1d340 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 6f   */.case OP_IfNo
1d341 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  t: {            
1d342 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a  /* jump, in1 */.
1d343 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1d344 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1d345 69 6e 74 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 69  into u.al */.  i
1d346 6e 74 20 63 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  nt c;.#endif /* 
1d347 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d348 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20  moved into u.al 
1d349 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  */.  if( pIn1->f
1d34a 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
1d34b 29 7b 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20  ){.    u.al.c = 
1d34c 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 65  pOp->p3;.  }else
1d34d 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
1d34e 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
1d34f 49 4e 54 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d  INT.    u.al.c =
1d350 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
1d351 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a 23  alue(pIn1)!=0;.#
1d352 65 6c 73 65 0a 20 20 20 20 75 2e 61 6c 2e 63 20  else.    u.al.c 
1d353 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  = sqlite3VdbeRea
1d354 6c 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e  lValue(pIn1)!=0.
1d355 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66  0;.#endif.    if
1d356 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
1d357 50 5f 49 66 4e 6f 74 20 29 20 75 2e 61 6c 2e 63  P_IfNot ) u.al.c
1d358 20 3d 20 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d 0a   = !u.al.c;.  }.
1d359 20 20 69 66 28 20 75 2e 61 6c 2e 63 20 29 7b 0a    if( u.al.c ){.
1d35a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1d35b 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  -1;.  }.  break;
1d35c 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
1d35d 73 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20  sNull P1 P2 * * 
1d35e 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20  *.**.** Jump to 
1d35f 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  P2 if the value 
1d360 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69  in register P1 i
1d361 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20  s NULL..*/.case 
1d362 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20  OP_IsNull: {    
1d363 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1d364 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75  as TK_ISNULL, ju
1d365 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28  mp, in1 */.  if(
1d366 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1d367 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0a  MEM_Null)!=0 ){.
1d368 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1d369 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1d36a 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d36b 20 4e 6f 74 4e 75 6c 6c 20 50 31 20 50 32 20 2a   NotNull P1 P2 *
1d36c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20   * *.**.** Jump 
1d36d 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 61 6c  to P2 if the val
1d36e 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1d36f 31 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20  1 is not NULL.  
1d370 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e  .*/.case OP_NotN
1d371 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20  ull: {          
1d372 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1d373 4e 4f 54 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69  NOTNULL, jump, i
1d374 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e  n1 */.  if( (pIn
1d375 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  1->flags & MEM_N
1d376 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70  ull)==0 ){.    p
1d377 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1d378 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1d379 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75  ./* Opcode: Colu
1d37a 6d 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50  mn P1 P2 P3 P4 P
1d37b 35 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  5.**.** Interpre
1d37c 74 20 74 68 65 20 64 61 74 61 20 74 68 61 74 20  t the data that 
1d37d 63 75 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73  cursor P1 points
1d37e 20 74 6f 20 61 73 20 61 20 73 74 72 75 63 74 75   to as a structu
1d37f 72 65 20 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a  re built using.*
1d380 2a 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64  * the MakeRecord
1d381 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 28   instruction.  (
1d382 53 65 65 20 74 68 65 20 4d 61 6b 65 52 65 63 6f  See the MakeReco
1d383 72 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64  rd opcode for ad
1d384 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f  ditional.** info
1d385 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
1d386 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20  e format of the 
1d387 64 61 74 61 2e 29 20 20 45 78 74 72 61 63 74 20  data.)  Extract 
1d388 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e  the P2-th column
1d389 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65  .** from this re
1d38a 63 6f 72 64 2e 20 20 49 66 20 74 68 65 72 65 20  cord.  If there 
1d38b 61 72 65 20 6c 65 73 73 20 74 68 61 74 20 28 50  are less that (P
1d38c 32 2b 31 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20  2+1) .** values 
1d38d 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65  in the record, e
1d38e 78 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a  xtract a NULL..*
1d38f 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 65  *.** The value e
1d390 78 74 72 61 63 74 65 64 20 69 73 20 73 74 6f 72  xtracted is stor
1d391 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
1d392 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  3..**.** If the 
1d393 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20  column contains 
1d394 66 65 77 65 72 20 74 68 61 6e 20 50 32 20 66 69  fewer than P2 fi
1d395 65 6c 64 73 2c 20 74 68 65 6e 20 65 78 74 72 61  elds, then extra
1d396 63 74 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a  ct a NULL.  Or,.
1d397 2a 2a 20 69 66 20 74 68 65 20 50 34 20 61 72 67  ** if the P4 arg
1d398 75 6d 65 6e 74 20 69 73 20 61 20 50 34 5f 4d 45  ument is a P4_ME
1d399 4d 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20  M use the value 
1d39a 6f 66 20 74 68 65 20 50 34 20 61 72 67 75 6d 65  of the P4 argume
1d39b 6e 74 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 73  nt as.** the res
1d39c 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ult..**.** If th
1d39d 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41  e OPFLAG_CLEARCA
1d39e 43 48 45 20 62 69 74 20 69 73 20 73 65 74 20 6f  CHE bit is set o
1d39f 6e 20 50 35 20 61 6e 64 20 50 31 20 69 73 20 61  n P5 and P1 is a
1d3a0 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 75   pseudo-table cu
1d3a1 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  rsor,.** then th
1d3a2 65 20 63 61 63 68 65 20 6f 66 20 74 68 65 20 63  e cache of the c
1d3a3 75 72 73 6f 72 20 69 73 20 72 65 73 65 74 20 70  ursor is reset p
1d3a4 72 69 6f 72 20 74 6f 20 65 78 74 72 61 63 74 69  rior to extracti
1d3a5 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a  ng the column..*
1d3a6 2a 20 54 68 65 20 66 69 72 73 74 20 4f 50 5f 43  * The first OP_C
1d3a7 6f 6c 75 6d 6e 20 61 67 61 69 6e 73 74 20 61 20  olumn against a 
1d3a8 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 61 66 74  pseudo-table aft
1d3a9 65 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  er the value of 
1d3aa 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 72  the content.** r
1d3ab 65 67 69 73 74 65 72 20 68 61 73 20 63 68 61 6e  egister has chan
1d3ac 67 65 64 20 73 68 6f 75 6c 64 20 68 61 76 65 20  ged should have 
1d3ad 74 68 69 73 20 62 69 74 20 73 65 74 2e 0a 2a 2f  this bit set..*/
1d3ae 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a  .case OP_Column:
1d3af 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1d3b0 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d3b1 65 64 20 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a  ed into u.am */.
1d3b2 20 20 75 33 32 20 70 61 79 6c 6f 61 64 53 69 7a    u32 payloadSiz
1d3b3 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  e;   /* Number o
1d3b4 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72  f bytes in the r
1d3b5 65 63 6f 72 64 20 2a 2f 0a 20 20 69 36 34 20 70  ecord */.  i64 p
1d3b6 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a  ayloadSize64; /*
1d3b7 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1d3b8 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a   in the record *
1d3b9 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20  /.  int p1;     
1d3ba 20 20 20 20 20 20 20 2f 2a 20 50 31 20 76 61 6c         /* P1 val
1d3bb 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65  ue of the opcode
1d3bc 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20   */.  int p2;   
1d3bd 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75           /* colu
1d3be 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74  mn number to ret
1d3bf 72 69 65 76 65 20 2a 2f 0a 20 20 56 64 62 65 43  rieve */.  VdbeC
1d3c0 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a  ursor *pC;    /*
1d3c1 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
1d3c2 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 65 63   */.  char *zRec
1d3c3 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
1d3c4 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20  ter to complete 
1d3c5 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20  record-data */. 
1d3c6 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1d3c7 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65  ;   /* The BTree
1d3c8 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32   cursor */.  u32
1d3c9 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20   *aType;        
1d3ca 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64  /* aType[i] hold
1d3cb 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79  s the numeric ty
1d3cc 70 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63  pe of the i-th c
1d3cd 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a  olumn */.  u32 *
1d3ce 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a  aOffset;      /*
1d3cf 20 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f   aOffset[i] is o
1d3d0 66 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f  ffset to start o
1d3d1 66 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20  f data for i-th 
1d3d2 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20  column */.  int 
1d3d3 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f  nField;        /
1d3d4 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c  * number of fiel
1d3d5 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ds in the record
1d3d6 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20   */.  int len;  
1d3d7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1d3d8 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65  length of the se
1d3d9 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f  rialized data fo
1d3da 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  r the column */.
1d3db 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
1d3dc 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
1d3dd 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a  nter */.  char *
1d3de 7a 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20  zData;       /* 
1d3df 50 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f  Part of the reco
1d3e0 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64  rd being decoded
1d3e1 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74   */.  Mem *pDest
1d3e2 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72  ;        /* Wher
1d3e3 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65  e to write the e
1d3e4 78 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a  xtracted value *
1d3e5 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20  /.  Mem sMem;   
1d3e6 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74         /* For st
1d3e7 6f 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  oring the record
1d3e8 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a   being decoded *
1d3e9 2f 0a 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20  /.  u8 *zIdx;   
1d3ea 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
1d3eb 69 6e 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20  into header */. 
1d3ec 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20   u8 *zEndHdr;   
1d3ed 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
1d3ee 6f 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74  o first byte aft
1d3ef 65 72 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f  er the header */
1d3f0 0a 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20  .  u32 offset;  
1d3f1 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1d3f2 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f  into the data */
1d3f3 0a 20 20 75 36 34 20 6f 66 66 73 65 74 36 34 3b  .  u64 offset64;
1d3f4 20 20 20 20 20 20 2f 2a 20 36 34 2d 62 69 74 20        /* 64-bit 
1d3f5 6f 66 66 73 65 74 2e 20 20 36 34 20 62 69 74 73  offset.  64 bits
1d3f6 20 6e 65 65 64 65 64 20 74 6f 20 63 61 74 63 68   needed to catch
1d3f7 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 69   overflow */.  i
1d3f8 6e 74 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20  nt szHdr;       
1d3f9 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
1d3fa 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 69 65   header size fie
1d3fb 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66 20 72  ld at start of r
1d3fc 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 61  ecord */.  int a
1d3fd 76 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a  vail;         /*
1d3fe 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1d3ff 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61   of available da
1d400 74 61 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65  ta */.  Mem *pRe
1d401 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 73  g;         /* Ps
1d402 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 75 74 20  eudoTable input 
1d403 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64  register */.#end
1d404 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1d405 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1d406 20 75 2e 61 6d 20 2a 2f 0a 0a 0a 20 20 75 2e 61   u.am */...  u.a
1d407 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  m.p1 = pOp->p1;.
1d408 20 20 75 2e 61 6d 2e 70 32 20 3d 20 70 4f 70 2d    u.am.p2 = pOp-
1d409 3e 70 32 3b 0a 20 20 75 2e 61 6d 2e 70 43 20 3d  >p2;.  u.am.pC =
1d40a 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e   0;.  memset(&u.
1d40b 61 6d 2e 73 4d 65 6d 2c 20 30 2c 20 73 69 7a 65  am.sMem, 0, size
1d40c 6f 66 28 75 2e 61 6d 2e 73 4d 65 6d 29 29 3b 0a  of(u.am.sMem));.
1d40d 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70    assert( u.am.p
1d40e 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1d40f 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1d410 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d  3>0 && pOp->p3<=
1d411 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 61  p->nMem );.  u.a
1d412 6d 2e 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d  m.pDest = &p->aM
1d413 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d  em[pOp->p3];.  M
1d414 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 75 2e  emSetTypeFlag(u.
1d415 61 6d 2e 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75  am.pDest, MEM_Nu
1d416 6c 6c 29 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63  ll);.  u.am.zRec
1d417 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73   = 0;..  /* This
1d418 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20   block sets the 
1d419 76 61 72 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61  variable u.am.pa
1d41a 79 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 65 20  yloadSize to be 
1d41b 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
1d41c 20 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73 20 69   of.  ** bytes i
1d41d 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20  n the record..  
1d41e 2a 2a 0a 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65  **.  ** u.am.zRe
1d41f 63 20 69 73 20 73 65 74 20 74 6f 20 62 65 20 74  c is set to be t
1d420 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74  he complete text
1d421 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 69   of the record i
1d422 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c  f it is availabl
1d423 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70  e..  ** The comp
1d424 6c 65 74 65 20 72 65 63 6f 72 64 20 74 65 78 74  lete record text
1d425 20 69 73 20 61 6c 77 61 79 73 20 61 76 61 69 6c   is always avail
1d426 61 62 6c 65 20 66 6f 72 20 70 73 65 75 64 6f 2d  able for pseudo-
1d427 74 61 62 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74  tables.  ** If t
1d428 68 65 20 72 65 63 6f 72 64 20 69 73 20 73 74 6f  he record is sto
1d429 72 65 64 20 69 6e 20 61 20 63 75 72 73 6f 72 2c  red in a cursor,
1d42a 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 65   the complete re
1d42b 63 6f 72 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d  cord text.  ** m
1d42c 69 67 68 74 20 62 65 20 61 76 61 69 6c 61 62 6c  ight be availabl
1d42d 65 20 69 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70  e in the  u.am.p
1d42e 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20  C->aRow cache.  
1d42f 4f 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20  Or it might not 
1d430 62 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20  be..  ** If the 
1d431 64 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c 61  data is unavaila
1d432 62 6c 65 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20  ble,  u.am.zRec 
1d433 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a  is set to NULL..
1d434 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73    **.  ** We als
1d435 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e 75  o compute the nu
1d436 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1d437 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 20 20  in the record.  
1d438 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a  For cursors,.  *
1d439 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
1d43a 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74 6f 72 65  columns is store
1d43b 64 20 69 6e 20 74 68 65 20 56 64 62 65 43 75 72  d in the VdbeCur
1d43c 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65  sor.nField eleme
1d43d 6e 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e  nt..  */.  u.am.
1d43e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e  pC = p->apCsr[u.
1d43f 61 6d 2e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  am.p1];.  assert
1d440 28 20 75 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a  ( u.am.pC!=0 );.
1d441 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1d442 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1d443 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e  .  assert( u.am.
1d444 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d  pC->pVtabCursor=
1d445 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75  =0 );.#endif.  u
1d446 2e 61 6d 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d  .am.pCrsr = u.am
1d447 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20  .pC->pCursor;.  
1d448 69 66 28 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d  if( u.am.pCrsr!=
1d449 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  0 ){.    /* The 
1d44a 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64  record is stored
1d44b 20 69 6e 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a   in a B-Tree */.
1d44c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1d44d 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f  VdbeCursorMoveto
1d44e 28 75 2e 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69  (u.am.pC);.    i
1d44f 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f  f( rc ) goto abo
1d450 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1d451 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 43  .    if( u.am.pC
1d452 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20  ->nullRow ){.   
1d453 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53     u.am.payloadS
1d454 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  ize = 0;.    }el
1d455 73 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e  se if( u.am.pC->
1d456 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e  cacheStatus==p->
1d457 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20  cacheCtr ){.    
1d458 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69    u.am.payloadSi
1d459 7a 65 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61  ze = u.am.pC->pa
1d45a 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 20  yloadSize;.     
1d45b 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68   u.am.zRec = (ch
1d45c 61 72 2a 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f  ar*)u.am.pC->aRo
1d45d 77 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  w;.    }else if(
1d45e 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65   u.am.pC->isInde
1d45f 78 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  x ){.      asser
1d460 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  t( sqlite3BtreeC
1d461 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 2e 61  ursorIsValid(u.a
1d462 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20 20 20  m.pCrsr) );.    
1d463 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1d464 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e  reeKeySize(u.am.
1d465 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79  pCrsr, &u.am.pay
1d466 6c 6f 61 64 53 69 7a 65 36 34 29 3b 0a 20 20 20  loadSize64);.   
1d467 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
1d468 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a  QLITE_OK );   /*
1d469 20 54 72 75 65 20 62 65 63 61 75 73 65 20 6f 66   True because of
1d46a 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20   CursorMoveto() 
1d46b 63 61 6c 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20  call above */.  
1d46c 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 42 74      /* sqlite3Bt
1d46d 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
1d46e 29 20 75 73 65 73 20 67 65 74 56 61 72 69 6e 74  ) uses getVarint
1d46f 33 32 28 29 20 74 6f 20 65 78 74 72 61 63 74 20  32() to extract 
1d470 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 79  the.      ** pay
1d471 6c 6f 61 64 20 73 69 7a 65 2c 20 73 6f 20 69 74  load size, so it
1d472 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66   is impossible f
1d473 6f 72 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  or u.am.payloadS
1d474 69 7a 65 36 34 20 74 6f 20 62 65 0a 20 20 20 20  ize64 to be.    
1d475 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e    ** larger than
1d476 20 33 32 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20   32 bits. */.   
1d477 20 20 20 61 73 73 65 72 74 28 20 28 75 2e 61 6d     assert( (u.am
1d478 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 26  .payloadSize64 &
1d479 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29   SQLITE_MAX_U32)
1d47a 3d 3d 28 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c  ==(u64)u.am.payl
1d47b 6f 61 64 53 69 7a 65 36 34 20 29 3b 0a 20 20 20  oadSize64 );.   
1d47c 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53     u.am.payloadS
1d47d 69 7a 65 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e  ize = (u32)u.am.
1d47e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20  payloadSize64;. 
1d47f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1d480 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1d481 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69  treeCursorIsVali
1d482 64 28 75 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b  d(u.am.pCrsr) );
1d483 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1d484 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65  te3BtreeDataSize
1d485 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e  (u.am.pCrsr, &u.
1d486 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b  am.payloadSize);
1d487 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
1d488 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20  c==SQLITE_OK ); 
1d489 20 20 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20    /* DataSize() 
1d48a 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20  cannot fail */. 
1d48b 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
1d48c 20 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f   u.am.pC->pseudo
1d48d 54 61 62 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20  TableReg>0 ){.  
1d48e 20 20 75 2e 61 6d 2e 70 52 65 67 20 3d 20 26 70    u.am.pReg = &p
1d48f 2d 3e 61 4d 65 6d 5b 75 2e 61 6d 2e 70 43 2d 3e  ->aMem[u.am.pC->
1d490 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b  pseudoTableReg];
1d491 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61  .    assert( u.a
1d492 6d 2e 70 52 65 67 2d 3e 66 6c 61 67 73 20 26 20  m.pReg->flags & 
1d493 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20  MEM_Blob );.    
1d494 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d495 20 3d 20 75 2e 61 6d 2e 70 52 65 67 2d 3e 6e 3b   = u.am.pReg->n;
1d496 0a 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d  .    u.am.zRec =
1d497 20 75 2e 61 6d 2e 70 52 65 67 2d 3e 7a 3b 0a 20   u.am.pReg->z;. 
1d498 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68     u.am.pC->cach
1d499 65 53 74 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e  eStatus = (pOp->
1d49a 70 35 26 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43  p5&OPFLAG_CLEARC
1d49b 41 43 48 45 29 20 3f 20 43 41 43 48 45 5f 53 54  ACHE) ? CACHE_ST
1d49c 41 4c 45 20 3a 20 70 2d 3e 63 61 63 68 65 43 74  ALE : p->cacheCt
1d49d 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  r;.    assert( u
1d49e 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d  .am.payloadSize=
1d49f 3d 30 20 7c 7c 20 75 2e 61 6d 2e 7a 52 65 63 21  =0 || u.am.zRec!
1d4a0 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  =0 );.  }else{. 
1d4a1 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74     /* Consider t
1d4a2 68 65 20 72 6f 77 20 74 6f 20 62 65 20 4e 55 4c  he row to be NUL
1d4a3 4c 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 70 61  L */.    u.am.pa
1d4a4 79 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20  yloadSize = 0;. 
1d4a5 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 75 2e 61 6d   }..  /* If u.am
1d4a6 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 69 73 20  .payloadSize is 
1d4a7 30 2c 20 74 68 65 6e 20 6a 75 73 74 20 73 74 6f  0, then just sto
1d4a8 72 65 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69  re a NULL */.  i
1d4a9 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  f( u.am.payloadS
1d4aa 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  ize==0 ){.    as
1d4ab 73 65 72 74 28 20 75 2e 61 6d 2e 70 44 65 73 74  sert( u.am.pDest
1d4ac 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c  ->flags&MEM_Null
1d4ad 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f   );.    goto op_
1d4ae 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a  column_out;.  }.
1d4af 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 4c    assert( db->aL
1d4b0 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1d4b1 54 5f 4c 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a  T_LENGTH]>=0 );.
1d4b2 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f    if( u.am.paylo
1d4b3 61 64 53 69 7a 65 20 3e 20 28 75 33 32 29 64 62  adSize > (u32)db
1d4b4 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
1d4b5 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b  LIMIT_LENGTH] ){
1d4b6 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69  .    goto too_bi
1d4b7 67 3b 0a 20 20 7d 0a 0a 20 20 75 2e 61 6d 2e 6e  g;.  }..  u.am.n
1d4b8 46 69 65 6c 64 20 3d 20 75 2e 61 6d 2e 70 43 2d  Field = u.am.pC-
1d4b9 3e 6e 46 69 65 6c 64 3b 0a 20 20 61 73 73 65 72  >nField;.  asser
1d4ba 74 28 20 75 2e 61 6d 2e 70 32 3c 75 2e 61 6d 2e  t( u.am.p2<u.am.
1d4bb 6e 46 69 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20  nField );..  /* 
1d4bc 52 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 74  Read and parse t
1d4bd 68 65 20 74 61 62 6c 65 20 68 65 61 64 65 72 2e  he table header.
1d4be 20 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75    Store the resu
1d4bf 6c 74 73 20 6f 66 20 74 68 65 20 70 61 72 73 65  lts of the parse
1d4c0 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72  .  ** into the r
1d4c1 65 63 6f 72 64 20 68 65 61 64 65 72 20 63 61 63  ecord header cac
1d4c2 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  he fields of the
1d4c3 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20   cursor..  */.  
1d4c4 75 2e 61 6d 2e 61 54 79 70 65 20 3d 20 75 2e 61  u.am.aType = u.a
1d4c5 6d 2e 70 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69  m.pC->aType;.  i
1d4c6 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68  f( u.am.pC->cach
1d4c7 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68  eStatus==p->cach
1d4c8 65 43 74 72 20 29 7b 0a 20 20 20 20 75 2e 61 6d  eCtr ){.    u.am
1d4c9 2e 61 4f 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e  .aOffset = u.am.
1d4ca 70 43 2d 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d  pC->aOffset;.  }
1d4cb 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
1d4cc 28 75 2e 61 6d 2e 61 54 79 70 65 29 3b 0a 20 20  (u.am.aType);.  
1d4cd 20 20 75 2e 61 6d 2e 61 76 61 69 6c 20 3d 20 30    u.am.avail = 0
1d4ce 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61  ;.    u.am.pC->a
1d4cf 4f 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e 61 4f  Offset = u.am.aO
1d4d0 66 66 73 65 74 20 3d 20 26 75 2e 61 6d 2e 61 54  ffset = &u.am.aT
1d4d1 79 70 65 5b 75 2e 61 6d 2e 6e 46 69 65 6c 64 5d  ype[u.am.nField]
1d4d2 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 70  ;.    u.am.pC->p
1d4d3 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61  ayloadSize = u.a
1d4d4 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20  m.payloadSize;. 
1d4d5 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68     u.am.pC->cach
1d4d6 65 53 74 61 74 75 73 20 3d 20 70 2d 3e 63 61 63  eStatus = p->cac
1d4d7 68 65 43 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46  heCtr;..    /* F
1d4d8 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61  igure out how ma
1d4d9 6e 79 20 62 79 74 65 73 20 61 72 65 20 69 6e 20  ny bytes are in 
1d4da 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  the header */.  
1d4db 20 20 69 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20    if( u.am.zRec 
1d4dc 29 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44  ){.      u.am.zD
1d4dd 61 74 61 20 3d 20 75 2e 61 6d 2e 7a 52 65 63 3b  ata = u.am.zRec;
1d4de 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1d4df 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69    if( u.am.pC->i
1d4e0 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20  sIndex ){.      
1d4e1 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28    u.am.zData = (
1d4e2 63 68 61 72 2a 29 73 71 6c 69 74 65 33 42 74 72  char*)sqlite3Btr
1d4e3 65 65 4b 65 79 46 65 74 63 68 28 75 2e 61 6d 2e  eeKeyFetch(u.am.
1d4e4 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61  pCrsr, &u.am.ava
1d4e5 69 6c 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  il);.      }else
1d4e6 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a  {.        u.am.z
1d4e7 44 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71  Data = (char*)sq
1d4e8 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65  lite3BtreeDataFe
1d4e9 74 63 68 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20  tch(u.am.pCrsr, 
1d4ea 26 75 2e 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 20  &u.am.avail);.  
1d4eb 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49      }.      /* I
1d4ec 66 20 4b 65 79 46 65 74 63 68 28 29 2f 44 61 74  f KeyFetch()/Dat
1d4ed 61 46 65 74 63 68 28 29 20 6d 61 6e 61 67 65 64  aFetch() managed
1d4ee 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e 74 69   to get the enti
1d4ef 72 65 20 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20  re payload,.    
1d4f0 20 20 2a 2a 20 73 61 76 65 20 74 68 65 20 70 61    ** save the pa
1d4f1 79 6c 6f 61 64 20 69 6e 20 74 68 65 20 75 2e 61  yload in the u.a
1d4f2 6d 2e 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65  m.pC->aRow cache
1d4f3 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 73 61 76  .  That will sav
1d4f4 65 20 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 20  e us from.      
1d4f5 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b  ** having to mak
1d4f6 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c  e additional cal
1d4f7 6c 73 20 74 6f 20 66 65 74 63 68 20 74 68 65 20  ls to fetch the 
1d4f8 63 6f 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20  content portion 
1d4f9 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  of.      ** the 
1d4fa 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f  record..      */
1d4fb 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75  .      assert( u
1d4fc 2e 61 6d 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a  .am.avail>=0 );.
1d4fd 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 70        if( u.am.p
1d4fe 61 79 6c 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75  ayloadSize <= (u
1d4ff 33 32 29 75 2e 61 6d 2e 61 76 61 69 6c 20 29 7b  32)u.am.avail ){
1d500 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52  .        u.am.zR
1d501 65 63 20 3d 20 75 2e 61 6d 2e 7a 44 61 74 61 3b  ec = u.am.zData;
1d502 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 70 43  .        u.am.pC
1d503 2d 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e  ->aRow = (u8*)u.
1d504 61 6d 2e 7a 44 61 74 61 3b 0a 20 20 20 20 20 20  am.zData;.      
1d505 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75  }else{.        u
1d506 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30  .am.pC->aRow = 0
1d507 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1d508 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f      /* The follo
1d509 77 69 6e 67 20 61 73 73 65 72 74 20 69 73 20 74  wing assert is t
1d50a 72 75 65 20 69 6e 20 61 6c 6c 20 63 61 73 65 73  rue in all cases
1d50b 20 61 63 63 65 70 74 20 77 68 65 6e 0a 20 20 20   accept when.   
1d50c 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
1d50d 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63   file has been c
1d50e 6f 72 72 75 70 74 65 64 20 65 78 74 65 72 6e 61  orrupted externa
1d50f 6c 6c 79 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61  lly..    **    a
1d510 73 73 65 72 74 28 20 75 2e 61 6d 2e 7a 52 65 63  ssert( u.am.zRec
1d511 21 3d 30 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69  !=0 || u.am.avai
1d512 6c 3e 3d 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  l>=u.am.payloadS
1d513 69 7a 65 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69  ize || u.am.avai
1d514 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75  l>=9 ); */.    u
1d515 2e 61 6d 2e 73 7a 48 64 72 20 3d 20 67 65 74 56  .am.szHdr = getV
1d516 61 72 69 6e 74 33 32 28 28 75 38 2a 29 75 2e 61  arint32((u8*)u.a
1d517 6d 2e 7a 44 61 74 61 2c 20 75 2e 61 6d 2e 6f 66  m.zData, u.am.of
1d518 66 73 65 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d  fset);..    /* M
1d519 61 6b 65 20 73 75 72 65 20 61 20 63 6f 72 72 75  ake sure a corru
1d51a 70 74 20 64 61 74 61 62 61 73 65 20 68 61 73 20  pt database has 
1d51b 6e 6f 74 20 67 69 76 65 6e 20 75 73 20 61 6e 20  not given us an 
1d51c 6f 76 65 72 73 69 7a 65 20 68 65 61 64 65 72 2e  oversize header.
1d51d 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 20  .    ** Do this 
1d51e 6e 6f 77 20 74 6f 20 61 76 6f 69 64 20 61 6e 20  now to avoid an 
1d51f 6f 76 65 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20  oversize memory 
1d520 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20  allocation..    
1d521 2a 2a 0a 20 20 20 20 2a 2a 20 54 79 70 65 20 65  **.    ** Type e
1d522 6e 74 72 69 65 73 20 63 61 6e 20 62 65 20 62 65  ntries can be be
1d523 74 77 65 65 6e 20 31 20 61 6e 64 20 35 20 62 79  tween 1 and 5 by
1d524 74 65 73 20 65 61 63 68 2e 20 20 42 75 74 20 34  tes each.  But 4
1d525 20 61 6e 64 20 35 20 62 79 74 65 0a 20 20 20 20   and 5 byte.    
1d526 2a 2a 20 74 79 70 65 73 20 75 73 65 20 73 6f 20  ** types use so 
1d527 6d 75 63 68 20 64 61 74 61 20 73 70 61 63 65 20  much data space 
1d528 74 68 61 74 20 74 68 65 72 65 20 63 61 6e 20 6f  that there can o
1d529 6e 6c 79 20 62 65 20 34 30 39 36 20 61 6e 64 20  nly be 4096 and 
1d52a 33 32 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65  32 of.    ** the
1d52b 6d 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  m, respectively.
1d52c 20 20 53 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d    So the maximum
1d52d 20 68 65 61 64 65 72 20 6c 65 6e 67 74 68 20 72   header length r
1d52e 65 73 75 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20  esults from a.  
1d52f 20 20 2a 2a 20 33 2d 62 79 74 65 20 74 79 70 65    ** 3-byte type
1d530 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65   for each of the
1d531 20 6d 61 78 69 6d 75 6d 20 6f 66 20 33 32 37 36   maximum of 3276
1d532 38 20 63 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74  8 columns plus t
1d533 68 72 65 65 0a 20 20 20 20 2a 2a 20 65 78 74 72  hree.    ** extr
1d534 61 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 20  a bytes for the 
1d535 68 65 61 64 65 72 20 6c 65 6e 67 74 68 20 69 74  header length it
1d536 73 65 6c 66 2e 20 20 33 32 37 36 38 2a 33 20 2b  self.  32768*3 +
1d537 20 33 20 3d 20 39 38 33 30 37 2e 0a 20 20 20 20   3 = 98307..    
1d538 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e  */.    if( u.am.
1d539 6f 66 66 73 65 74 20 3e 20 39 38 33 30 37 20 29  offset > 98307 )
1d53a 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1d53b 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1d53c 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f  ;.      goto op_
1d53d 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20  column_out;.    
1d53e 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74  }..    /* Comput
1d53f 65 20 69 6e 20 75 2e 61 6d 2e 6c 65 6e 20 74 68  e in u.am.len th
1d540 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
1d541 73 20 6f 66 20 64 61 74 61 20 77 65 20 6e 65 65  s of data we nee
1d542 64 20 74 6f 20 72 65 61 64 20 69 6e 20 6f 72 64  d to read in ord
1d543 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20 67 65 74  er.    ** to get
1d544 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 74 79 70   u.am.nField typ
1d545 65 20 76 61 6c 75 65 73 2e 20 20 75 2e 61 6d 2e  e values.  u.am.
1d546 6f 66 66 73 65 74 20 69 73 20 61 6e 20 75 70 70  offset is an upp
1d547 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73  er bound on this
1d548 2e 20 20 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e  .  But.    ** u.
1d549 61 6d 2e 6e 46 69 65 6c 64 20 6d 69 67 68 74 20  am.nField might 
1d54a 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74 6c 79  be significantly
1d54b 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 74   less than the t
1d54c 72 75 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  rue number of co
1d54d 6c 75 6d 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20  lumns.    ** in 
1d54e 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 69  the table, and i
1d54f 6e 20 74 68 61 74 20 63 61 73 65 2c 20 35 2a 75  n that case, 5*u
1d550 2e 61 6d 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67  .am.nField+3 mig
1d551 68 74 20 62 65 20 73 6d 61 6c 6c 65 72 20 74 68  ht be smaller th
1d552 61 6e 20 75 2e 61 6d 2e 6f 66 66 73 65 74 2e 0a  an u.am.offset..
1d553 20 20 20 20 2a 2a 20 57 65 20 77 61 6e 74 20 74      ** We want t
1d554 6f 20 6d 69 6e 69 6d 69 7a 65 20 75 2e 61 6d 2e  o minimize u.am.
1d555 6c 65 6e 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  len in order to 
1d556 6c 69 6d 69 74 20 74 68 65 20 73 69 7a 65 20 6f  limit the size o
1d557 66 20 74 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20  f the memory.   
1d558 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20   ** allocation, 
1d559 65 73 70 65 63 69 61 6c 6c 79 20 69 66 20 61 20  especially if a 
1d55a 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
1d55b 20 66 69 6c 65 20 68 61 73 20 63 61 75 73 65 64   file has caused
1d55c 20 75 2e 61 6d 2e 6f 66 66 73 65 74 0a 20 20 20   u.am.offset.   
1d55d 20 2a 2a 20 74 6f 20 62 65 20 6f 76 65 72 73 69   ** to be oversi
1d55e 7a 65 64 2e 20 4f 66 66 73 65 74 20 69 73 20 6c  zed. Offset is l
1d55f 69 6d 69 74 65 64 20 74 6f 20 39 38 33 30 37 20  imited to 98307 
1d560 61 62 6f 76 65 2e 20 20 42 75 74 20 39 38 33 30  above.  But 9830
1d561 37 20 6d 69 67 68 74 0a 20 20 20 20 2a 2a 20 73  7 might.    ** s
1d562 74 69 6c 6c 20 65 78 63 65 65 64 20 52 6f 62 73  till exceed Robs
1d563 6f 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  on memory alloca
1d564 74 69 6f 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73  tion limits on s
1d565 6f 6d 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ome configuratio
1d566 6e 73 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79  ns..    ** On sy
1d567 73 74 65 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f  stems that canno
1d568 74 20 74 6f 6c 65 72 61 74 65 20 6c 61 72 67 65  t tolerate large
1d569 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
1d56a 6f 6e 73 2c 20 75 2e 61 6d 2e 6e 46 69 65 6c 64  ons, u.am.nField
1d56b 2a 35 2b 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c  *5+3.    ** will
1d56c 20 6c 69 6b 65 6c 79 20 62 65 20 6d 75 63 68 20   likely be much 
1d56d 73 6d 61 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e  smaller since u.
1d56e 61 6d 2e 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c  am.nField will l
1d56f 69 6b 65 6c 79 20 62 65 20 6c 65 73 73 20 74 68  ikely be less th
1d570 61 6e 0a 20 20 20 20 2a 2a 20 32 30 20 6f 72 20  an.    ** 20 or 
1d571 73 6f 2e 20 20 54 68 69 73 20 69 6e 73 75 72 65  so.  This insure
1d572 73 20 74 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65  s that Robson me
1d573 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
1d574 6c 69 6d 69 74 73 20 61 72 65 0a 20 20 20 20 2a  limits are.    *
1d575 2a 20 6e 6f 74 20 65 78 63 65 65 64 65 64 20 65  * not exceeded e
1d576 76 65 6e 20 66 6f 72 20 63 6f 72 72 75 70 74 20  ven for corrupt 
1d577 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 0a  database files..
1d578 20 20 20 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e      */.    u.am.
1d579 6c 65 6e 20 3d 20 75 2e 61 6d 2e 6e 46 69 65 6c  len = u.am.nFiel
1d57a 64 2a 35 20 2b 20 33 3b 0a 20 20 20 20 69 66 28  d*5 + 3;.    if(
1d57b 20 75 2e 61 6d 2e 6c 65 6e 20 3e 20 28 69 6e 74   u.am.len > (int
1d57c 29 75 2e 61 6d 2e 6f 66 66 73 65 74 20 29 20 75  )u.am.offset ) u
1d57d 2e 61 6d 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75  .am.len = (int)u
1d57e 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20  .am.offset;..   
1d57f 20 2f 2a 20 54 68 65 20 4b 65 79 46 65 74 63 68   /* The KeyFetch
1d580 28 29 20 6f 72 20 44 61 74 61 46 65 74 63 68 28  () or DataFetch(
1d581 29 20 61 62 6f 76 65 20 61 72 65 20 66 61 73 74  ) above are fast
1d582 20 61 6e 64 20 77 69 6c 6c 20 67 65 74 20 74 68   and will get th
1d583 65 20 65 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20  e entire.    ** 
1d584 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 69 6e  record header in
1d585 20 6d 6f 73 74 20 63 61 73 65 73 2e 20 20 42 75   most cases.  Bu
1d586 74 20 74 68 65 79 20 77 69 6c 6c 20 66 61 69 6c  t they will fail
1d587 20 74 6f 20 67 65 74 20 74 68 65 20 63 6f 6d 70   to get the comp
1d588 6c 65 74 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f  lete.    ** reco
1d589 72 64 20 68 65 61 64 65 72 20 69 66 20 74 68 65  rd header if the
1d58a 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 64   record header d
1d58b 6f 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61  oes not fit on a
1d58c 20 73 69 6e 67 6c 65 20 70 61 67 65 0a 20 20 20   single page.   
1d58d 20 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54 72 65   ** in the B-Tre
1d58e 65 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 61  e.  When that ha
1d58f 70 70 65 6e 73 2c 20 75 73 65 20 73 71 6c 69 74  ppens, use sqlit
1d590 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72  e3VdbeMemFromBtr
1d591 65 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61  ee() to.    ** a
1d592 63 71 75 69 72 65 20 74 68 65 20 63 6f 6d 70 6c  cquire the compl
1d593 65 74 65 20 68 65 61 64 65 72 20 74 65 78 74 2e  ete header text.
1d594 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1d595 21 75 2e 61 6d 2e 7a 52 65 63 20 26 26 20 75 2e  !u.am.zRec && u.
1d596 61 6d 2e 61 76 61 69 6c 3c 75 2e 61 6d 2e 6c 65  am.avail<u.am.le
1d597 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e  n ){.      u.am.
1d598 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a  sMem.flags = 0;.
1d599 20 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e        u.am.sMem.
1d59a 64 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63  db = 0;.      rc
1d59b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
1d59c 6d 46 72 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e  mFromBtree(u.am.
1d59d 70 43 72 73 72 2c 20 30 2c 20 75 2e 61 6d 2e 6c  pCrsr, 0, u.am.l
1d59e 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49  en, u.am.pC->isI
1d59f 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d  ndex, &u.am.sMem
1d5a0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1d5a1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1d5a2 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f        goto op_co
1d5a3 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20  lumn_out;.      
1d5a4 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61  }.      u.am.zDa
1d5a5 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a  ta = u.am.sMem.z
1d5a6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d  ;.    }.    u.am
1d5a7 2e 7a 45 6e 64 48 64 72 20 3d 20 28 75 38 20 2a  .zEndHdr = (u8 *
1d5a8 29 26 75 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61  )&u.am.zData[u.a
1d5a9 6d 2e 6c 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6d  m.len];.    u.am
1d5aa 2e 7a 49 64 78 20 3d 20 28 75 38 20 2a 29 26 75  .zIdx = (u8 *)&u
1d5ab 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 73  .am.zData[u.am.s
1d5ac 7a 48 64 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53  zHdr];..    /* S
1d5ad 63 61 6e 20 74 68 65 20 68 65 61 64 65 72 20 61  can the header a
1d5ae 6e 64 20 75 73 65 20 69 74 20 74 6f 20 66 69 6c  nd use it to fil
1d5af 6c 20 69 6e 20 74 68 65 20 75 2e 61 6d 2e 61 54  l in the u.am.aT
1d5b0 79 70 65 5b 5d 20 61 6e 64 20 75 2e 61 6d 2e 61  ype[] and u.am.a
1d5b1 4f 66 66 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20  Offset[].    ** 
1d5b2 61 72 72 61 79 73 2e 20 20 75 2e 61 6d 2e 61 54  arrays.  u.am.aT
1d5b3 79 70 65 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c  ype[u.am.i] will
1d5b4 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74 79 70   contain the typ
1d5b5 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 74 68  e integer for th
1d5b6 65 20 75 2e 61 6d 2e 69 2d 74 68 0a 20 20 20 20  e u.am.i-th.    
1d5b7 2a 2a 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e  ** column and u.
1d5b8 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e  am.aOffset[u.am.
1d5b9 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20  i] will contain 
1d5ba 74 68 65 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20  the u.am.offset 
1d5bb 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69  from the beginni
1d5bc 6e 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  ng.    ** of the
1d5bd 20 72 65 63 6f 72 64 20 74 6f 20 74 68 65 20 73   record to the s
1d5be 74 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  tart of the data
1d5bf 20 66 6f 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d   for the u.am.i-
1d5c0 74 68 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f  th column.    */
1d5c1 0a 20 20 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74  .    u.am.offset
1d5c2 36 34 20 3d 20 75 2e 61 6d 2e 6f 66 66 73 65 74  64 = u.am.offset
1d5c3 3b 0a 20 20 20 20 66 6f 72 28 75 2e 61 6d 2e 69  ;.    for(u.am.i
1d5c4 3d 30 3b 20 75 2e 61 6d 2e 69 3c 75 2e 61 6d 2e  =0; u.am.i<u.am.
1d5c5 6e 46 69 65 6c 64 3b 20 75 2e 61 6d 2e 69 2b 2b  nField; u.am.i++
1d5c6 29 7b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61  ){.      if( u.a
1d5c7 6d 2e 7a 49 64 78 3c 75 2e 61 6d 2e 7a 45 6e 64  m.zIdx<u.am.zEnd
1d5c8 48 64 72 20 29 7b 0a 20 20 20 20 20 20 20 20 75  Hdr ){.        u
1d5c9 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d  .am.aOffset[u.am
1d5ca 2e 69 5d 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e  .i] = (u32)u.am.
1d5cb 6f 66 66 73 65 74 36 34 3b 0a 20 20 20 20 20 20  offset64;.      
1d5cc 20 20 75 2e 61 6d 2e 7a 49 64 78 20 2b 3d 20 67    u.am.zIdx += g
1d5cd 65 74 56 61 72 69 6e 74 33 32 28 75 2e 61 6d 2e  etVarint32(u.am.
1d5ce 7a 49 64 78 2c 20 75 2e 61 6d 2e 61 54 79 70 65  zIdx, u.am.aType
1d5cf 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 20  [u.am.i]);.     
1d5d0 20 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34     u.am.offset64
1d5d1 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53   += sqlite3VdbeS
1d5d2 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61  erialTypeLen(u.a
1d5d3 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29  m.aType[u.am.i])
1d5d4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1d5d5 20 20 20 20 20 20 20 2f 2a 20 49 66 20 75 2e 61         /* If u.a
1d5d6 6d 2e 69 20 69 73 20 6c 65 73 73 20 74 68 61 74  m.i is less that
1d5d7 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2c 20 74 68   u.am.nField, th
1d5d8 65 6e 20 74 68 65 72 65 20 61 72 65 20 6c 65 73  en there are les
1d5d9 73 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69 73  s fields in this
1d5da 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 63 6f  .        ** reco
1d5db 72 64 20 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f  rd than SetNumCo
1d5dc 6c 75 6d 6e 73 20 69 6e 64 69 63 61 74 65 64 20  lumns indicated 
1d5dd 74 68 65 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e  there are column
1d5de 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20  s in the.       
1d5df 20 2a 2a 20 74 61 62 6c 65 2e 20 53 65 74 20 74   ** table. Set t
1d5e0 68 65 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66  he u.am.offset f
1d5e1 6f 72 20 61 6e 79 20 65 78 74 72 61 20 63 6f 6c  or any extra col
1d5e2 75 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65 6e 74  umns not present
1d5e3 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74   in.        ** t
1d5e4 68 65 20 72 65 63 6f 72 64 20 74 6f 20 30 2e 20  he record to 0. 
1d5e5 54 68 69 73 20 74 65 6c 6c 73 20 63 6f 64 65 20  This tells code 
1d5e6 62 65 6c 6f 77 20 74 6f 20 73 74 6f 72 65 20 61  below to store a
1d5e7 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a   NULL.        **
1d5e8 20 69 6e 73 74 65 61 64 20 6f 66 20 64 65 73 65   instead of dese
1d5e9 72 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75  rializing a valu
1d5ea 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72  e from the recor
1d5eb 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  d..        */.  
1d5ec 20 20 20 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73        u.am.aOffs
1d5ed 65 74 5b 75 2e 61 6d 2e 69 5d 20 3d 20 30 3b 0a  et[u.am.i] = 0;.
1d5ee 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1d5ef 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1d5f0 52 65 6c 65 61 73 65 28 26 75 2e 61 6d 2e 73 4d  Release(&u.am.sM
1d5f1 65 6d 29 3b 0a 20 20 20 20 75 2e 61 6d 2e 73 4d  em);.    u.am.sM
1d5f2 65 6d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  em.flags = MEM_N
1d5f3 75 6c 6c 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  ull;..    /* If 
1d5f4 77 65 20 68 61 76 65 20 72 65 61 64 20 6d 6f 72  we have read mor
1d5f5 65 20 68 65 61 64 65 72 20 64 61 74 61 20 74 68  e header data th
1d5f6 61 6e 20 77 61 73 20 63 6f 6e 74 61 69 6e 65 64  an was contained
1d5f7 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2c 0a   in the header,.
1d5f8 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65      ** or if the
1d5f9 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74   end of the last
1d5fa 20 66 69 65 6c 64 20 61 70 70 65 61 72 73 20 74   field appears t
1d5fb 6f 20 62 65 20 70 61 73 74 20 74 68 65 20 65 6e  o be past the en
1d5fc 64 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  d of the.    ** 
1d5fd 72 65 63 6f 72 64 2c 20 6f 72 20 69 66 20 74 68  record, or if th
1d5fe 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73  e end of the las
1d5ff 74 20 66 69 65 6c 64 20 61 70 70 65 61 72 73 20  t field appears 
1d600 74 6f 20 62 65 20 62 65 66 6f 72 65 20 74 68 65  to be before the
1d601 20 65 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74   end.    ** of t
1d602 68 65 20 72 65 63 6f 72 64 20 28 77 68 65 6e 20  he record (when 
1d603 61 6c 6c 20 66 69 65 6c 64 73 20 70 72 65 73 65  all fields prese
1d604 6e 74 29 2c 20 74 68 65 6e 20 77 65 20 6d 75 73  nt), then we mus
1d605 74 20 62 65 20 64 65 61 6c 69 6e 67 0a 20 20 20  t be dealing.   
1d606 20 2a 2a 20 77 69 74 68 20 61 20 63 6f 72 72 75   ** with a corru
1d607 70 74 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20  pt database..   
1d608 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 75 2e 61   */.    if( (u.a
1d609 6d 2e 7a 49 64 78 20 3e 20 75 2e 61 6d 2e 7a 45  m.zIdx > u.am.zE
1d60a 6e 64 48 64 72 29 7c 7c 20 28 75 2e 61 6d 2e 6f  ndHdr)|| (u.am.o
1d60b 66 66 73 65 74 36 34 20 3e 20 75 2e 61 6d 2e 70  ffset64 > u.am.p
1d60c 61 79 6c 6f 61 64 53 69 7a 65 29 0a 20 20 20 20  ayloadSize).    
1d60d 20 7c 7c 20 28 75 2e 61 6d 2e 7a 49 64 78 3d 3d   || (u.am.zIdx==
1d60e 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 20 26 26 20  u.am.zEndHdr && 
1d60f 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 21 3d 28  u.am.offset64!=(
1d610 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64  u64)u.am.payload
1d611 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 20 20 72  Size) ){.      r
1d612 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
1d613 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67  PT_BKPT;.      g
1d614 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75  oto op_column_ou
1d615 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  t;.    }.  }..  
1d616 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6c 75 6d  /* Get the colum
1d617 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49  n information. I
1d618 66 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75  f u.am.aOffset[u
1d619 2e 61 6d 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a  .am.p2] is non-z
1d61a 65 72 6f 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 64  ero, then.  ** d
1d61b 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 76  eserialize the v
1d61c 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65  alue from the re
1d61d 63 6f 72 64 2e 20 49 66 20 75 2e 61 6d 2e 61 4f  cord. If u.am.aO
1d61e 66 66 73 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69  ffset[u.am.p2] i
1d61f 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65  s zero,.  ** the
1d620 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 74 20  n there are not 
1d621 65 6e 6f 75 67 68 20 66 69 65 6c 64 73 20 69 6e  enough fields in
1d622 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 73   the record to s
1d623 61 74 69 73 66 79 20 74 68 65 0a 20 20 2a 2a 20  atisfy the.  ** 
1d624 72 65 71 75 65 73 74 2e 20 20 49 6e 20 74 68 69  request.  In thi
1d625 73 20 63 61 73 65 2c 20 73 65 74 20 74 68 65 20  s case, set the 
1d626 76 61 6c 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f  value NULL or to
1d627 20 50 34 20 69 66 20 50 34 20 69 73 0a 20 20 2a   P4 if P4 is.  *
1d628 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
1d629 20 4d 65 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a   Mem object..  *
1d62a 2f 0a 20 20 69 66 28 20 75 2e 61 6d 2e 61 4f 66  /.  if( u.am.aOf
1d62b 66 73 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 29 7b  fset[u.am.p2] ){
1d62c 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
1d62d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
1d62e 20 20 69 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20    if( u.am.zRec 
1d62f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1d630 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78  VdbeMemReleaseEx
1d631 74 65 72 6e 61 6c 28 75 2e 61 6d 2e 70 44 65 73  ternal(u.am.pDes
1d632 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
1d633 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28  3VdbeSerialGet((
1d634 75 38 20 2a 29 26 75 2e 61 6d 2e 7a 52 65 63 5b  u8 *)&u.am.zRec[
1d635 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61  u.am.aOffset[u.a
1d636 6d 2e 70 32 5d 5d 2c 20 75 2e 61 6d 2e 61 54 79  m.p2]], u.am.aTy
1d637 70 65 5b 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61  pe[u.am.p2], u.a
1d638 6d 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 7d 65  m.pDest);.    }e
1d639 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e  lse{.      u.am.
1d63a 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62  len = sqlite3Vdb
1d63b 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75  eSerialTypeLen(u
1d63c 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70  .am.aType[u.am.p
1d63d 32 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  2]);.      sqlit
1d63e 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75  e3VdbeMemMove(&u
1d63f 2e 61 6d 2e 73 4d 65 6d 2c 20 75 2e 61 6d 2e 70  .am.sMem, u.am.p
1d640 44 65 73 74 29 3b 0a 20 20 20 20 20 20 72 63 20  Dest);.      rc 
1d641 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
1d642 46 72 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70  FromBtree(u.am.p
1d643 43 72 73 72 2c 20 75 2e 61 6d 2e 61 4f 66 66 73  Crsr, u.am.aOffs
1d644 65 74 5b 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61  et[u.am.p2], u.a
1d645 6d 2e 6c 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e  m.len, u.am.pC->
1d646 69 73 49 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73  isIndex, &u.am.s
1d647 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Mem);.      if( 
1d648 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1d649 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6f 70  .        goto op
1d64a 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20  _column_out;.   
1d64b 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e     }.      u.am.
1d64c 7a 44 61 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65  zData = u.am.sMe
1d64d 6d 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  m.z;.      sqlit
1d64e 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28  e3VdbeSerialGet(
1d64f 28 75 38 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c  (u8*)u.am.zData,
1d650 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d   u.am.aType[u.am
1d651 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 74  .p2], u.am.pDest
1d652 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 61  );.    }.    u.a
1d653 6d 2e 70 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65  m.pDest->enc = e
1d654 6e 63 6f 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65  ncoding;.  }else
1d655 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70  {.    if( pOp->p
1d656 34 74 79 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b  4type==P4_MEM ){
1d657 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d658 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
1d659 28 75 2e 61 6d 2e 70 44 65 73 74 2c 20 70 4f 70  (u.am.pDest, pOp
1d65a 2d 3e 70 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53  ->p4.pMem, MEM_S
1d65b 74 61 74 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73  tatic);.    }els
1d65c 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e{.      assert(
1d65d 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61   u.am.pDest->fla
1d65e 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20  gs&MEM_Null );. 
1d65f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
1d660 66 20 77 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79  f we dynamically
1d661 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65   allocated space
1d662 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74   to hold the dat
1d663 61 20 28 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73  a (in the.  ** s
1d664 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
1d665 6d 42 74 72 65 65 28 29 20 63 61 6c 6c 20 61 62  mBtree() call ab
1d666 6f 76 65 29 20 74 68 65 6e 20 74 72 61 6e 73 66  ove) then transf
1d667 65 72 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68  er control of th
1d668 61 74 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61  at.  ** dynamica
1d669 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70  lly allocated sp
1d66a 61 63 65 20 6f 76 65 72 20 74 6f 20 74 68 65 20  ace over to the 
1d66b 75 2e 61 6d 2e 70 44 65 73 74 20 73 74 72 75 63  u.am.pDest struc
1d66c 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20  ture..  ** This 
1d66d 70 72 65 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72  prevents a memor
1d66e 79 20 63 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69  y copy..  */.  i
1d66f 66 28 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61  f( u.am.sMem.zMa
1d670 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 61 73 73 65  lloc ){.    asse
1d671 72 74 28 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3d  rt( u.am.sMem.z=
1d672 3d 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c  =u.am.sMem.zMall
1d673 6f 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  oc );.    assert
1d674 28 20 21 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e  ( !(u.am.pDest->
1d675 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29  flags & MEM_Dyn)
1d676 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1d677 21 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c  !(u.am.pDest->fl
1d678 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c  ags & (MEM_Blob|
1d679 4d 45 4d 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61  MEM_Str)) || u.a
1d67a 6d 2e 70 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6d  m.pDest->z==u.am
1d67b 2e 73 4d 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75  .sMem.z );.    u
1d67c 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73  .am.pDest->flags
1d67d 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c   &= ~(MEM_Ephem|
1d67e 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20  MEM_Static);.   
1d67f 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61   u.am.pDest->fla
1d680 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a  gs |= MEM_Term;.
1d681 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e      u.am.pDest->
1d682 7a 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b  z = u.am.sMem.z;
1d683 0a 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d  .    u.am.pDest-
1d684 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 75 2e 61 6d 2e  >zMalloc = u.am.
1d685 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20  sMem.zMalloc;.  
1d686 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  }..  rc = sqlite
1d687 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
1d688 65 61 62 6c 65 28 75 2e 61 6d 2e 70 44 65 73 74  eable(u.am.pDest
1d689 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75  );..op_column_ou
1d68a 74 3a 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  t:.  UPDATE_MAX_
1d68b 42 4c 4f 42 53 49 5a 45 28 75 2e 61 6d 2e 70 44  BLOBSIZE(u.am.pD
1d68c 65 73 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52  est);.  REGISTER
1d68d 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20  _TRACE(pOp->p3, 
1d68e 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 62  u.am.pDest);.  b
1d68f 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d690 64 65 3a 20 41 66 66 69 6e 69 74 79 20 50 31 20  de: Affinity P1 
1d691 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  P2 * P4 *.**.** 
1d692 41 70 70 6c 79 20 61 66 66 69 6e 69 74 69 65 73  Apply affinities
1d693 20 74 6f 20 61 20 72 61 6e 67 65 20 6f 66 20 50   to a range of P
1d694 32 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  2 registers star
1d695 74 69 6e 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a  ting with P1..**
1d696 0a 2a 2a 20 50 34 20 69 73 20 61 20 73 74 72 69  .** P4 is a stri
1d697 6e 67 20 74 68 61 74 20 69 73 20 50 32 20 63 68  ng that is P2 ch
1d698 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54  aracters long. T
1d699 68 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 72  he nth character
1d69a 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e   of the.** strin
1d69b 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20  g indicates the 
1d69c 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  column affinity 
1d69d 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75  that should be u
1d69e 73 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 0a  sed for the nth.
1d69f 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69  ** memory cell i
1d6a0 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a  n the range..*/.
1d6a1 63 61 73 65 20 4f 50 5f 41 66 66 69 6e 69 74 79  case OP_Affinity
1d6a2 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
1d6a3 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1d6a4 76 65 64 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f  ved into u.an */
1d6a5 0a 20 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69  .  char *zAffini
1d6a6 74 79 3b 20 20 20 2f 2a 20 54 68 65 20 61 66 66  ty;   /* The aff
1d6a7 69 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c  inity to be appl
1d6a8 69 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44  ied */.  Mem *pD
1d6a9 61 74 61 30 3b 20 20 20 20 20 20 20 2f 2a 20 46  ata0;       /* F
1d6aa 69 72 73 74 20 72 65 67 69 73 74 65 72 20 74 6f  irst register to
1d6ab 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c 79 20   which to apply 
1d6ac 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 4d 65  affinity */.  Me
1d6ad 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20  m *pLast;       
1d6ae 20 2f 2a 20 4c 61 73 74 20 72 65 67 69 73 74 65   /* Last registe
1d6af 72 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 70  r to which to ap
1d6b0 70 6c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a  ply affinity */.
1d6b1 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20    Mem *pRec;    
1d6b2 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
1d6b3 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64  register */.#end
1d6b4 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1d6b5 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1d6b6 20 75 2e 61 6e 20 2a 2f 0a 0a 20 20 75 2e 61 6e   u.an */..  u.an
1d6b7 2e 7a 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70  .zAffinity = pOp
1d6b8 2d 3e 70 34 2e 7a 3b 0a 20 20 75 2e 61 6e 2e 70  ->p4.z;.  u.an.p
1d6b9 44 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d  Data0 = &p->aMem
1d6ba 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 75 2e 61  [pOp->p1];.  u.a
1d6bb 6e 2e 70 4c 61 73 74 20 3d 20 26 75 2e 61 6e 2e  n.pLast = &u.an.
1d6bc 70 44 61 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31  pData0[pOp->p2-1
1d6bd 5d 3b 0a 20 20 66 6f 72 28 75 2e 61 6e 2e 70 52  ];.  for(u.an.pR
1d6be 65 63 3d 75 2e 61 6e 2e 70 44 61 74 61 30 3b 20  ec=u.an.pData0; 
1d6bf 75 2e 61 6e 2e 70 52 65 63 3c 3d 75 2e 61 6e 2e  u.an.pRec<=u.an.
1d6c0 70 4c 61 73 74 3b 20 75 2e 61 6e 2e 70 52 65 63  pLast; u.an.pRec
1d6c1 2b 2b 29 7b 0a 20 20 20 20 45 78 70 61 6e 64 42  ++){.    ExpandB
1d6c2 6c 6f 62 28 75 2e 61 6e 2e 70 52 65 63 29 3b 0a  lob(u.an.pRec);.
1d6c3 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74      applyAffinit
1d6c4 79 28 75 2e 61 6e 2e 70 52 65 63 2c 20 75 2e 61  y(u.an.pRec, u.a
1d6c5 6e 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 6e  n.zAffinity[u.an
1d6c6 2e 70 52 65 63 2d 75 2e 61 6e 2e 70 44 61 74 61  .pRec-u.an.pData
1d6c7 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  0], encoding);. 
1d6c8 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1d6c9 2a 20 4f 70 63 6f 64 65 3a 20 4d 61 6b 65 52 65  * Opcode: MakeRe
1d6ca 63 6f 72 64 20 50 31 20 50 32 20 50 33 20 50 34  cord P1 P2 P3 P4
1d6cb 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74   *.**.** Convert
1d6cc 20 50 32 20 72 65 67 69 73 74 65 72 73 20 62 65   P2 registers be
1d6cd 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 31 20  ginning with P1 
1d6ce 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 65 6e  into a single en
1d6cf 74 72 79 0a 2a 2a 20 73 75 69 74 61 62 6c 65 20  try.** suitable 
1d6d0 66 6f 72 20 75 73 65 20 61 73 20 61 20 64 61 74  for use as a dat
1d6d1 61 20 72 65 63 6f 72 64 20 69 6e 20 61 20 64 61  a record in a da
1d6d2 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
1d6d3 61 73 20 61 20 6b 65 79 0a 2a 2a 20 69 6e 20 61  as a key.** in a
1d6d4 6e 20 69 6e 64 65 78 2e 20 20 54 68 65 20 64 65  n index.  The de
1d6d5 74 61 69 6c 73 20 6f 66 20 74 68 65 20 66 6f 72  tails of the for
1d6d6 6d 61 74 20 61 72 65 20 69 72 72 65 6c 65 76 61  mat are irreleva
1d6d7 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a  nt as long as.**
1d6d8 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f   the OP_Column o
1d6d9 70 63 6f 64 65 20 63 61 6e 20 64 65 63 6f 64 65  pcode can decode
1d6da 20 74 68 65 20 72 65 63 6f 72 64 20 6c 61 74 65   the record late
1d6db 72 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 73  r..** Refer to s
1d6dc 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65  ource code comme
1d6dd 6e 74 73 20 66 6f 72 20 74 68 65 20 64 65 74 61  nts for the deta
1d6de 69 6c 73 20 6f 66 20 74 68 65 20 72 65 63 6f 72  ils of the recor
1d6df 64 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a  d.** format..**.
1d6e0 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20 73  ** P4 may be a s
1d6e1 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 50 32  tring that is P2
1d6e2 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67   characters long
1d6e3 2e 20 20 54 68 65 20 6e 74 68 20 63 68 61 72 61  .  The nth chara
1d6e4 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73  cter of the.** s
1d6e5 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73 20  tring indicates 
1d6e6 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  the column affin
1d6e7 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ity that should 
1d6e8 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  be used for the 
1d6e9 6e 74 68 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20  nth.** field of 
1d6ea 74 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 0a 2a  the index key..*
1d6eb 2a 0a 2a 2a 20 54 68 65 20 6d 61 70 70 69 6e 67  *.** The mapping
1d6ec 20 66 72 6f 6d 20 63 68 61 72 61 63 74 65 72 20   from character 
1d6ed 74 6f 20 61 66 66 69 6e 69 74 79 20 69 73 20 67  to affinity is g
1d6ee 69 76 65 6e 20 62 79 20 74 68 65 20 53 51 4c 49  iven by the SQLI
1d6ef 54 45 5f 41 46 46 5f 0a 2a 2a 20 6d 61 63 72 6f  TE_AFF_.** macro
1d6f0 73 20 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c  s defined in sql
1d6f1 69 74 65 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20  iteInt.h..**.** 
1d6f2 49 66 20 50 34 20 69 73 20 4e 55 4c 4c 20 74 68  If P4 is NULL th
1d6f3 65 6e 20 61 6c 6c 20 69 6e 64 65 78 20 66 69 65  en all index fie
1d6f4 6c 64 73 20 68 61 76 65 20 74 68 65 20 61 66 66  lds have the aff
1d6f5 69 6e 69 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63  inity NONE..*/.c
1d6f6 61 73 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  ase OP_MakeRecor
1d6f7 64 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  d: {.#if 0  /* l
1d6f8 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d6f9 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20 2a  oved into u.ao *
1d6fa 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f  /.  u8 *zNewReco
1d6fb 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20  rd;        /* A 
1d6fc 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74  buffer to hold t
1d6fd 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  he data for the 
1d6fe 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  new record */.  
1d6ff 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20  Mem *pRec;      
1d700 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
1d701 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36  w record */.  u6
1d702 34 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20  4 nData;        
1d703 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d704 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  f bytes of data 
1d705 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  space */.  int n
1d706 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Hdr;            
1d707 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1d708 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73  ytes of header s
1d709 70 61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42  pace */.  i64 nB
1d70a 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  yte;            
1d70b 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72   /* Data space r
1d70c 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73  equired for this
1d70d 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
1d70e 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 20   nZero;         
1d70f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1d710 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74   zero bytes at t
1d711 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65  he end of the re
1d712 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56  cord */.  int nV
1d713 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  arint;          
1d714 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1d715 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20  tes in a varint 
1d716 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f  */.  u32 serial_
1d717 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54  type;       /* T
1d718 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d  ype field */.  M
1d719 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20  em *pData0;     
1d71a 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66        /* First f
1d71b 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69  ield to be combi
1d71c 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63  ned into the rec
1d71d 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c  ord */.  Mem *pL
1d71e 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ast;            
1d71f 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f 66  /* Last field of
1d720 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1d721 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20   int nField;    
1d722 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1d723 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74  r of fields in t
1d724 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63  he record */.  c
1d725 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20  har *zAffinity; 
1d726 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66        /* The aff
1d727 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72  inity string for
1d728 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1d729 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74   int file_format
1d72a 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20  ;       /* File 
1d72b 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f  format to use fo
1d72c 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  r encoding */.  
1d72d 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
1d72e 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
1d72f 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f  used in zNewReco
1d730 72 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c 65  rd[] */.  int le
1d731 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
1d732 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20   /* Length of a 
1d733 66 69 65 6c 64 20 2a 2f 0a 23 65 6e 64 69 66 20  field */.#endif 
1d734 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d735 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d736 61 6f 20 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 75  ao */..  /* Assu
1d737 6d 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20  ming the record 
1d738 63 6f 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c 64  contains N field
1d739 73 2c 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f  s, the record fo
1d73a 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a 20  rmat looks.  ** 
1d73b 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a  like this:.  **.
1d73c 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    ** -----------
1d73d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d73e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d73f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d740 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20  -------------.  
1d741 2a 2a 20 7c 20 68 64 72 2d 73 69 7a 65 20 7c 20  ** | hdr-size | 
1d742 74 79 70 65 20 30 20 7c 20 74 79 70 65 20 31 20  type 0 | type 1 
1d743 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 20 4e 2d 31  | ... | type N-1
1d744 20 7c 20 64 61 74 61 30 20 7c 20 2e 2e 2e 20 7c   | data0 | ... |
1d745 20 64 61 74 61 20 4e 2d 31 20 7c 0a 20 20 2a 2a   data N-1 |.  **
1d746 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
1d747 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d748 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d749 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d74a 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20  ---------.  **. 
1d74b 20 2a 2a 20 44 61 74 61 28 30 29 20 69 73 20 74   ** Data(0) is t
1d74c 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74  aken from regist
1d74d 65 72 20 50 31 2e 20 20 44 61 74 61 28 31 29 20  er P1.  Data(1) 
1d74e 63 6f 6d 65 73 20 66 72 6f 6d 20 72 65 67 69 73  comes from regis
1d74f 74 65 72 20 50 31 2b 31 0a 20 20 2a 2a 20 61 6e  ter P1+1.  ** an
1d750 64 20 73 6f 20 66 72 6f 74 68 2e 0a 20 20 2a 2a  d so froth..  **
1d751 0a 20 20 2a 2a 20 45 61 63 68 20 74 79 70 65 20  .  ** Each type 
1d752 66 69 65 6c 64 20 69 73 20 61 20 76 61 72 69 6e  field is a varin
1d753 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  t representing t
1d754 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f  he serial type o
1d755 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72 65  f the.  ** corre
1d756 73 70 6f 6e 64 69 6e 67 20 64 61 74 61 20 65 6c  sponding data el
1d757 65 6d 65 6e 74 20 28 73 65 65 20 73 71 6c 69 74  ement (see sqlit
1d758 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
1d759 28 29 29 2e 20 54 68 65 0a 20 20 2a 2a 20 68 64  ()). The.  ** hd
1d75a 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20  r-size field is 
1d75b 61 6c 73 6f 20 61 20 76 61 72 69 6e 74 20 77 68  also a varint wh
1d75c 69 63 68 20 69 73 20 74 68 65 20 6f 66 66 73 65  ich is the offse
1d75d 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e  t from the begin
1d75e 6e 69 6e 67 0a 20 20 2a 2a 20 6f 66 20 74 68 65  ning.  ** of the
1d75f 20 72 65 63 6f 72 64 20 74 6f 20 64 61 74 61 30   record to data0
1d760 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 44  ..  */.  u.ao.nD
1d761 61 74 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ata = 0;        
1d762 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1d763 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63  tes of data spac
1d764 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 72  e */.  u.ao.nHdr
1d765 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f   = 0;          /
1d766 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1d767 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63  s of header spac
1d768 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79 74  e */.  u.ao.nByt
1d769 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  e = 0;         /
1d76a 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71  * Data space req
1d76b 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72  uired for this r
1d76c 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e  ecord */.  u.ao.
1d76d 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 20  nZero = 0;      
1d76e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1d76f 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68  zero bytes at th
1d770 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63  e end of the rec
1d771 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 46  ord */.  u.ao.nF
1d772 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  ield = pOp->p1;.
1d773 20 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79    u.ao.zAffinity
1d774 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20   = pOp->p4.z;.  
1d775 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 6e 46 69  assert( u.ao.nFi
1d776 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32  eld>0 && pOp->p2
1d777 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e  >0 && pOp->p2+u.
1d778 61 6f 2e 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d  ao.nField<=p->nM
1d779 65 6d 2b 31 20 29 3b 0a 20 20 75 2e 61 6f 2e 70  em+1 );.  u.ao.p
1d77a 44 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d  Data0 = &p->aMem
1d77b 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20  [u.ao.nField];. 
1d77c 20 75 2e 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70   u.ao.nField = p
1d77d 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 6f 2e 70  Op->p2;.  u.ao.p
1d77e 4c 61 73 74 20 3d 20 26 75 2e 61 6f 2e 70 44 61  Last = &u.ao.pDa
1d77f 74 61 30 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 2d  ta0[u.ao.nField-
1d780 31 5d 3b 0a 20 20 75 2e 61 6f 2e 66 69 6c 65 5f  1];.  u.ao.file_
1d781 66 6f 72 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57  format = p->minW
1d782 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0a  riteFileFormat;.
1d783 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75  .  /* Loop throu
1d784 67 68 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20  gh the elements 
1d785 74 68 61 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75  that will make u
1d786 70 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20  p the record to 
1d787 66 69 67 75 72 65 0a 20 20 2a 2a 20 6f 75 74 20  figure.  ** out 
1d788 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20 69  how much space i
1d789 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74  s required for t
1d78a 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20  he new record.. 
1d78b 20 2a 2f 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70   */.  for(u.ao.p
1d78c 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b  Rec=u.ao.pData0;
1d78d 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f   u.ao.pRec<=u.ao
1d78e 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65  .pLast; u.ao.pRe
1d78f 63 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 2e  c++){.    if( u.
1d790 61 6f 2e 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a  ao.zAffinity ){.
1d791 20 20 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e        applyAffin
1d792 69 74 79 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75  ity(u.ao.pRec, u
1d793 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e  .ao.zAffinity[u.
1d794 61 6f 2e 70 52 65 63 2d 75 2e 61 6f 2e 70 44 61  ao.pRec-u.ao.pDa
1d795 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  ta0], encoding);
1d796 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75  .    }.    if( u
1d797 2e 61 6f 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26  .ao.pRec->flags&
1d798 4d 45 4d 5f 5a 65 72 6f 20 26 26 20 75 2e 61 6f  MEM_Zero && u.ao
1d799 2e 70 52 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20  .pRec->n>0 ){.  
1d79a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1d79b 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61  emExpandBlob(u.a
1d79c 6f 2e 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20  o.pRec);.    }. 
1d79d 20 20 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74     u.ao.serial_t
1d79e 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ype = sqlite3Vdb
1d79f 65 53 65 72 69 61 6c 54 79 70 65 28 75 2e 61 6f  eSerialType(u.ao
1d7a0 2e 70 52 65 63 2c 20 75 2e 61 6f 2e 66 69 6c 65  .pRec, u.ao.file
1d7a1 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e  _format);.    u.
1d7a2 61 6f 2e 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33  ao.len = sqlite3
1d7a3 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
1d7a4 6e 28 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79  n(u.ao.serial_ty
1d7a5 70 65 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e 44  pe);.    u.ao.nD
1d7a6 61 74 61 20 2b 3d 20 75 2e 61 6f 2e 6c 65 6e 3b  ata += u.ao.len;
1d7a7 0a 20 20 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b  .    u.ao.nHdr +
1d7a8 3d 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c  = sqlite3VarintL
1d7a9 65 6e 28 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74  en(u.ao.serial_t
1d7aa 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 75 2e  ype);.    if( u.
1d7ab 61 6f 2e 70 52 65 63 2d 3e 66 6c 61 67 73 20 26  ao.pRec->flags &
1d7ac 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
1d7ad 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20     /* Only pure 
1d7ae 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42  zero-filled BLOB
1d7af 73 20 63 61 6e 20 62 65 20 69 6e 70 75 74 20 74  s can be input t
1d7b0 6f 20 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20  o this Opcode.. 
1d7b1 20 20 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f       ** We do no
1d7b2 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69  t allow blobs wi
1d7b3 74 68 20 61 20 70 72 65 66 69 78 20 61 6e 64 20  th a prefix and 
1d7b4 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  a zero-filled ta
1d7b5 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61  il. */.      u.a
1d7b6 6f 2e 6e 5a 65 72 6f 20 2b 3d 20 75 2e 61 6f 2e  o.nZero += u.ao.
1d7b7 70 52 65 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20  pRec->u.nZero;. 
1d7b8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61     }else if( u.a
1d7b9 6f 2e 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 75  o.len ){.      u
1d7ba 2e 61 6f 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20  .ao.nZero = 0;. 
1d7bb 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
1d7bc 64 64 20 74 68 65 20 69 6e 69 74 69 61 6c 20 68  dd the initial h
1d7bd 65 61 64 65 72 20 76 61 72 69 6e 74 20 61 6e 64  eader varint and
1d7be 20 74 6f 74 61 6c 20 74 68 65 20 73 69 7a 65 20   total the size 
1d7bf 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b  */.  u.ao.nHdr +
1d7c0 3d 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 20 3d  = u.ao.nVarint =
1d7c1 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65   sqlite3VarintLe
1d7c2 6e 28 75 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20  n(u.ao.nHdr);.  
1d7c3 69 66 28 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74  if( u.ao.nVarint
1d7c4 3c 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65  <sqlite3VarintLe
1d7c5 6e 28 75 2e 61 6f 2e 6e 48 64 72 29 20 29 7b 0a  n(u.ao.nHdr) ){.
1d7c6 20 20 20 20 75 2e 61 6f 2e 6e 48 64 72 2b 2b 3b      u.ao.nHdr++;
1d7c7 0a 20 20 7d 0a 20 20 75 2e 61 6f 2e 6e 42 79 74  .  }.  u.ao.nByt
1d7c8 65 20 3d 20 75 2e 61 6f 2e 6e 48 64 72 2b 75 2e  e = u.ao.nHdr+u.
1d7c9 61 6f 2e 6e 44 61 74 61 2d 75 2e 61 6f 2e 6e 5a  ao.nData-u.ao.nZ
1d7ca 65 72 6f 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e  ero;.  if( u.ao.
1d7cb 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74  nByte>db->aLimit
1d7cc 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
1d7cd 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74  NGTH] ){.    got
1d7ce 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a  o too_big;.  }..
1d7cf 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
1d7d0 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74  he output regist
1d7d1 65 72 20 68 61 73 20 61 20 62 75 66 66 65 72 20  er has a buffer 
1d7d2 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20  large enough to 
1d7d3 73 74 6f 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e  store.  ** the n
1d7d4 65 77 20 72 65 63 6f 72 64 2e 20 54 68 65 20 6f  ew record. The o
1d7d5 75 74 70 75 74 20 72 65 67 69 73 74 65 72 20 28  utput register (
1d7d6 70 4f 70 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20  pOp->p3) is not 
1d7d7 61 6c 6c 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20  allowed to.  ** 
1d7d8 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e  be one of the in
1d7d9 70 75 74 20 72 65 67 69 73 74 65 72 73 20 28 62  put registers (b
1d7da 65 63 61 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f  ecause the follo
1d7db 77 69 6e 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a  wing call to.  *
1d7dc 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  * sqlite3VdbeMem
1d7dd 47 72 6f 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f  Grow() could clo
1d7de 62 62 65 72 20 74 68 65 20 76 61 6c 75 65 20 62  bber the value b
1d7df 65 66 6f 72 65 20 69 74 20 69 73 20 75 73 65 64  efore it is used
1d7e0 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  )..  */.  assert
1d7e1 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70  ( pOp->p3<pOp->p
1d7e2 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f  1 || pOp->p3>=pO
1d7e3 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 20 29 3b  p->p1+pOp->p2 );
1d7e4 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d  .  pOut = &p->aM
1d7e5 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69  em[pOp->p3];.  i
1d7e6 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
1d7e7 6d 47 72 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74  mGrow(pOut, (int
1d7e8 29 75 2e 61 6f 2e 6e 42 79 74 65 2c 20 30 29 20  )u.ao.nByte, 0) 
1d7e9 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d  ){.    goto no_m
1d7ea 65 6d 3b 0a 20 20 7d 0a 20 20 75 2e 61 6f 2e 7a  em;.  }.  u.ao.z
1d7eb 4e 65 77 52 65 63 6f 72 64 20 3d 20 28 75 38 20  NewRecord = (u8 
1d7ec 2a 29 70 4f 75 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a  *)pOut->z;..  /*
1d7ed 20 57 72 69 74 65 20 74 68 65 20 72 65 63 6f 72   Write the recor
1d7ee 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 69 20 3d 20  d */.  u.ao.i = 
1d7ef 70 75 74 56 61 72 69 6e 74 33 32 28 75 2e 61 6f  putVarint32(u.ao
1d7f0 2e 7a 4e 65 77 52 65 63 6f 72 64 2c 20 75 2e 61  .zNewRecord, u.a
1d7f1 6f 2e 6e 48 64 72 29 3b 0a 20 20 66 6f 72 28 75  o.nHdr);.  for(u
1d7f2 2e 61 6f 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44  .ao.pRec=u.ao.pD
1d7f3 61 74 61 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c  ata0; u.ao.pRec<
1d7f4 3d 75 2e 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61  =u.ao.pLast; u.a
1d7f5 6f 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75  o.pRec++){.    u
1d7f6 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 20  .ao.serial_type 
1d7f7 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
1d7f8 69 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70 52 65  ialType(u.ao.pRe
1d7f9 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72  c, u.ao.file_for
1d7fa 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 69  mat);.    u.ao.i
1d7fb 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 33 32 28   += putVarint32(
1d7fc 26 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64  &u.ao.zNewRecord
1d7fd 5b 75 2e 61 6f 2e 69 5d 2c 20 75 2e 61 6f 2e 73  [u.ao.i], u.ao.s
1d7fe 65 72 69 61 6c 5f 74 79 70 65 29 3b 20 20 20 20  erial_type);    
1d7ff 20 20 2f 2a 20 73 65 72 69 61 6c 20 74 79 70 65    /* serial type
1d800 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 75 2e   */.  }.  for(u.
1d801 61 6f 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44 61  ao.pRec=u.ao.pDa
1d802 74 61 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c 3d  ta0; u.ao.pRec<=
1d803 75 2e 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 6f  u.ao.pLast; u.ao
1d804 2e 70 52 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65  .pRec++){  /* se
1d805 72 69 61 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20  rial data */.   
1d806 20 75 2e 61 6f 2e 69 20 2b 3d 20 73 71 6c 69 74   u.ao.i += sqlit
1d807 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28  e3VdbeSerialPut(
1d808 26 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64  &u.ao.zNewRecord
1d809 5b 75 2e 61 6f 2e 69 5d 2c 20 28 69 6e 74 29 28  [u.ao.i], (int)(
1d80a 75 2e 61 6f 2e 6e 42 79 74 65 2d 75 2e 61 6f 2e  u.ao.nByte-u.ao.
1d80b 69 29 2c 20 75 2e 61 6f 2e 70 52 65 63 2c 75 2e  i), u.ao.pRec,u.
1d80c 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b  ao.file_format);
1d80d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 75  .  }.  assert( u
1d80e 2e 61 6f 2e 69 3d 3d 75 2e 61 6f 2e 6e 42 79 74  .ao.i==u.ao.nByt
1d80f 65 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  e );..  assert( 
1d810 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70  pOp->p3>0 && pOp
1d811 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
1d812 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e  .  pOut->n = (in
1d813 74 29 75 2e 61 6f 2e 6e 42 79 74 65 3b 0a 20 20  t)u.ao.nByte;.  
1d814 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pOut->flags = ME
1d815 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 44 79 6e  M_Blob | MEM_Dyn
1d816 3b 0a 20 20 70 4f 75 74 2d 3e 78 44 65 6c 20 3d  ;.  pOut->xDel =
1d817 20 30 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e   0;.  if( u.ao.n
1d818 5a 65 72 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74  Zero ){.    pOut
1d819 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 75 2e 61 6f  ->u.nZero = u.ao
1d81a 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4f 75 74  .nZero;.    pOut
1d81b 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 5a  ->flags |= MEM_Z
1d81c 65 72 6f 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d  ero;.  }.  pOut-
1d81d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  >enc = SQLITE_UT
1d81e 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73 65 20  F8;  /* In case 
1d81f 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76 65 72  the blob is ever
1d820 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 74 65   converted to te
1d821 78 74 20 2a 2f 0a 20 20 52 45 47 49 53 54 45 52  xt */.  REGISTER
1d822 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20  _TRACE(pOp->p3, 
1d823 70 4f 75 74 29 3b 0a 20 20 55 50 44 41 54 45 5f  pOut);.  UPDATE_
1d824 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75  MAX_BLOBSIZE(pOu
1d825 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
1d826 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 75 6e 74  /* Opcode: Count
1d827 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1d828 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 6e 75 6d  ** Store the num
1d829 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 28  ber of entries (
1d82a 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  an integer value
1d82b 29 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f  ) in the table o
1d82c 72 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 70 65 6e  r index .** open
1d82d 65 64 20 62 79 20 63 75 72 73 6f 72 20 50 31 20  ed by cursor P1 
1d82e 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a  in register P2.*
1d82f 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1d830 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54  _OMIT_BTREECOUNT
1d831 0a 63 61 73 65 20 4f 50 5f 43 6f 75 6e 74 3a 20  .case OP_Count: 
1d832 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74  {         /* out
1d833 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
1d834 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1d835 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1d836 69 6e 74 6f 20 75 2e 61 70 20 2a 2f 0a 20 20 69  into u.ap */.  i
1d837 36 34 20 6e 45 6e 74 72 79 3b 0a 20 20 42 74 43  64 nEntry;.  BtC
1d838 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 23 65  ursor *pCrsr;.#e
1d839 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1d83a 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d83b 74 6f 20 75 2e 61 70 20 2a 2f 0a 0a 20 20 75 2e  to u.ap */..  u.
1d83c 61 70 2e 70 43 72 73 72 20 3d 20 70 2d 3e 61 70  ap.pCrsr = p->ap
1d83d 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 2d 3e 70 43  Csr[pOp->p1]->pC
1d83e 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 61  ursor;.  if( u.a
1d83f 70 2e 70 43 72 73 72 20 29 7b 0a 20 20 20 20 72  p.pCrsr ){.    r
1d840 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1d841 43 6f 75 6e 74 28 75 2e 61 70 2e 70 43 72 73 72  Count(u.ap.pCrsr
1d842 2c 20 26 75 2e 61 70 2e 6e 45 6e 74 72 79 29 3b  , &u.ap.nEntry);
1d843 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e  .  }else{.    u.
1d844 61 70 2e 6e 45 6e 74 72 79 20 3d 20 30 3b 0a 20  ap.nEntry = 0;. 
1d845 20 7d 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73   }.  pOut->flags
1d846 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f   = MEM_Int;.  pO
1d847 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 70 2e 6e  ut->u.i = u.ap.n
1d848 45 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b 3b 0a  Entry;.  break;.
1d849 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63  }.#endif../* Opc
1d84a 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 20 50  ode: Savepoint P
1d84b 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  1 * * P4 *.**.**
1d84c 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f   Open, release o
1d84d 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73  r rollback the s
1d84e 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 20 62  avepoint named b
1d84f 79 20 70 61 72 61 6d 65 74 65 72 20 50 34 2c 20  y parameter P4, 
1d850 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20  depending.** on 
1d851 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 31 2e  the value of P1.
1d852 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 73   To open a new s
1d853 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 2e  avepoint, P1==0.
1d854 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63 6f 6d   To release (com
1d855 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 73 74  mit) an.** exist
1d856 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c 20 50  ing savepoint, P
1d857 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f 6c 6c  1==1, or to roll
1d858 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69 6e 67  back an existing
1d859 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d 3d 32   savepoint P1==2
1d85a 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 61 76  ..*/.case OP_Sav
1d85b 65 70 6f 69 6e 74 3a 20 7b 0a 23 69 66 20 30 20  epoint: {.#if 0 
1d85c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d85d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d85e 2e 61 71 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b  .aq */.  int p1;
1d85f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d860 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
1d861 65 20 6f 66 20 50 31 20 6f 70 65 72 61 6e 64 20  e of P1 operand 
1d862 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  */.  char *zName
1d863 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1d864 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
1d865 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 69  savepoint */.  i
1d866 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 53 61 76 65  nt nName;.  Save
1d867 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 53  point *pNew;.  S
1d868 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70  avepoint *pSavep
1d869 6f 69 6e 74 3b 0a 20 20 53 61 76 65 70 6f 69 6e  oint;.  Savepoin
1d86a 74 20 2a 70 54 6d 70 3b 0a 20 20 69 6e 74 20 69  t *pTmp;.  int i
1d86b 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 69 6e 74  Savepoint;.  int
1d86c 20 69 69 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c   ii;.#endif /* l
1d86d 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d86e 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71 20 2a  oved into u.aq *
1d86f 2f 0a 0a 20 20 75 2e 61 71 2e 70 31 20 3d 20 70  /..  u.aq.p1 = p
1d870 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 71 2e 7a  Op->p1;.  u.aq.z
1d871 4e 61 6d 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a  Name = pOp->p4.z
1d872 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74  ;..  /* Assert t
1d873 68 61 74 20 74 68 65 20 75 2e 61 71 2e 70 31 20  hat the u.aq.p1 
1d874 70 61 72 61 6d 65 74 65 72 20 69 73 20 76 61 6c  parameter is val
1d875 69 64 2e 20 41 6c 73 6f 20 74 68 61 74 20 69 66  id. Also that if
1d876 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65   there is no ope
1d877 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  n.  ** transacti
1d878 6f 6e 2c 20 74 68 65 6e 20 74 68 65 72 65 20 63  on, then there c
1d879 61 6e 6e 6f 74 20 62 65 20 61 6e 79 20 73 61 76  annot be any sav
1d87a 65 70 6f 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20  epoints..  */.  
1d87b 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61 76  assert( db->pSav
1d87c 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d  epoint==0 || db-
1d87d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29  >autoCommit==0 )
1d87e 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 71  ;.  assert( u.aq
1d87f 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42  .p1==SAVEPOINT_B
1d880 45 47 49 4e 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53  EGIN||u.aq.p1==S
1d881 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
1d882 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50  ||u.aq.p1==SAVEP
1d883 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b  OINT_ROLLBACK );
1d884 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70  .  assert( db->p
1d885 53 61 76 65 70 6f 69 6e 74 20 7c 7c 20 64 62 2d  Savepoint || db-
1d886 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61  >isTransactionSa
1d887 76 65 70 6f 69 6e 74 3d 3d 30 20 29 3b 0a 20 20  vepoint==0 );.  
1d888 61 73 73 65 72 74 28 20 63 68 65 63 6b 53 61 76  assert( checkSav
1d889 65 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62 29 20  epointCount(db) 
1d88a 29 3b 0a 0a 20 20 69 66 28 20 75 2e 61 71 2e 70  );..  if( u.aq.p
1d88b 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47  1==SAVEPOINT_BEG
1d88c 49 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  IN ){.    if( db
1d88d 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30  ->writeVdbeCnt>0
1d88e 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e   ){.      /* A n
1d88f 65 77 20 73 61 76 65 70 6f 69 6e 74 20 63 61 6e  ew savepoint can
1d890 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 20 69  not be created i
1d891 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69  f there are acti
1d892 76 65 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a  ve write.      *
1d893 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28 69 2e  * statements (i.
1d894 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 72 69  e. open read/wri
1d895 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62  te incremental b
1d896 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a 20 20  lob handles)..  
1d897 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
1d898 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1d899 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
1d89a 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 76 65  cannot open save
1d89b 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 20 20  point - ".      
1d89c 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74    "SQL statement
1d89d 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b  s in progress");
1d89e 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1d89f 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c  TE_BUSY;.    }el
1d8a0 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e 6e  se{.      u.aq.n
1d8a1 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
1d8a2 72 6c 65 6e 33 30 28 75 2e 61 71 2e 7a 4e 61 6d  rlen30(u.aq.zNam
1d8a3 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72  e);..      /* Cr
1d8a4 65 61 74 65 20 61 20 6e 65 77 20 73 61 76 65 70  eate a new savep
1d8a5 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20  oint structure. 
1d8a6 2a 2f 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 4e  */.      u.aq.pN
1d8a7 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
1d8a8 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
1d8a9 6f 66 28 53 61 76 65 70 6f 69 6e 74 29 2b 75 2e  of(Savepoint)+u.
1d8aa 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20  aq.nName+1);.   
1d8ab 20 20 20 69 66 28 20 75 2e 61 71 2e 70 4e 65 77     if( u.aq.pNew
1d8ac 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71   ){.        u.aq
1d8ad 2e 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28  .pNew->zName = (
1d8ae 63 68 61 72 20 2a 29 26 75 2e 61 71 2e 70 4e 65  char *)&u.aq.pNe
1d8af 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65  w[1];.        me
1d8b0 6d 63 70 79 28 75 2e 61 71 2e 70 4e 65 77 2d 3e  mcpy(u.aq.pNew->
1d8b1 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d  zName, u.aq.zNam
1d8b2 65 2c 20 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29  e, u.aq.nName+1)
1d8b3 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ;..        /* If
1d8b4 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65   there is no ope
1d8b5 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  n transaction, t
1d8b6 68 65 6e 20 6d 61 72 6b 20 74 68 69 73 20 61 73  hen mark this as
1d8b7 20 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20   a special.     
1d8b8 20 20 20 2a 2a 20 22 74 72 61 6e 73 61 63 74 69     ** "transacti
1d8b9 6f 6e 20 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a  on savepoint". *
1d8ba 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62  /.        if( db
1d8bb 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
1d8bc 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75            db->au
1d8bd 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20  toCommit = 0;.  
1d8be 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72          db->isTr
1d8bf 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69  ansactionSavepoi
1d8c0 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  nt = 1;.        
1d8c1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1d8c2 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b   db->nSavepoint+
1d8c3 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  +;.        }..  
1d8c4 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 20 74 68        /* Link th
1d8c5 65 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20  e new savepoint 
1d8c6 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
1d8c7 65 20 68 61 6e 64 6c 65 27 73 20 6c 69 73 74 2e  e handle's list.
1d8c8 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 71   */.        u.aq
1d8c9 2e 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 64  .pNew->pNext = d
1d8ca 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20  b->pSavepoint;. 
1d8cb 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65         db->pSave
1d8cc 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 4e 65  point = u.aq.pNe
1d8cd 77 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e  w;.        u.aq.
1d8ce 70 4e 65 77 2d 3e 6e 44 65 66 65 72 72 65 64 43  pNew->nDeferredC
1d8cf 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72  ons = db->nDefer
1d8d0 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d  redCons;.      }
1d8d1 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
1d8d2 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f      u.aq.iSavepo
1d8d3 69 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a  int = 0;..    /*
1d8d4 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 64 20   Find the named 
1d8d5 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 74 68  savepoint. If th
1d8d6 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 73  ere is no such s
1d8d7 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61  avepoint, then a
1d8d8 6e 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f  n.    ** an erro
1d8d9 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f  r is returned to
1d8da 20 74 68 65 20 75 73 65 72 2e 20 20 2a 2f 0a 20   the user.  */. 
1d8db 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20 75 2e     for(.      u.
1d8dc 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20  aq.pSavepoint = 
1d8dd 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a  db->pSavepoint;.
1d8de 20 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65        u.aq.pSave
1d8df 70 6f 69 6e 74 20 26 26 20 73 71 6c 69 74 65 33  point && sqlite3
1d8e0 53 74 72 49 43 6d 70 28 75 2e 61 71 2e 70 53 61  StrICmp(u.aq.pSa
1d8e1 76 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65 2c 20  vepoint->zName, 
1d8e2 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  u.aq.zName);.   
1d8e3 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69     u.aq.pSavepoi
1d8e4 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 70  nt = u.aq.pSavep
1d8e5 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a 20 20 20 20  oint->pNext.    
1d8e6 29 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e 69 53  ){.      u.aq.iS
1d8e7 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20  avepoint++;.    
1d8e8 7d 0a 20 20 20 20 69 66 28 20 21 75 2e 61 71 2e  }.    if( !u.aq.
1d8e9 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20  pSavepoint ){.  
1d8ea 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1d8eb 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1d8ec 2c 20 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 73  , db, "no such s
1d8ed 61 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c 20 75  avepoint: %s", u
1d8ee 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  .aq.zName);.    
1d8ef 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
1d8f0 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ROR;.    }else i
1d8f1 66 28 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 77  f(.        db->w
1d8f2 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c  riteVdbeCnt>0 ||
1d8f3 20 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50   (u.aq.p1==SAVEP
1d8f4 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26  OINT_ROLLBACK &&
1d8f5 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
1d8f6 6e 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20 20 20  nt>1).    ){.   
1d8f7 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20     /* It is not 
1d8f8 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 6c 65  possible to rele
1d8f9 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 73  ase (commit) a s
1d8fa 61 76 65 70 6f 69 6e 74 20 69 66 20 74 68 65 72  avepoint if ther
1d8fb 65 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61  e are.      ** a
1d8fc 63 74 69 76 65 20 77 72 69 74 65 20 73 74 61 74  ctive write stat
1d8fd 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 6e 6f  ements. It is no
1d8fe 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 6f  t possible to ro
1d8ff 6c 6c 62 61 63 6b 20 61 20 73 61 76 65 70 6f 69  llback a savepoi
1d900 6e 74 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 74  nt.      ** if t
1d901 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74  here are any act
1d902 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 61  ive statements a
1d903 74 20 61 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a  t all..      */.
1d904 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74        sqlite3Set
1d905 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1d906 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20  sg, db,.        
1d907 22 63 61 6e 6e 6f 74 20 25 73 20 73 61 76 65 70  "cannot %s savep
1d908 6f 69 6e 74 20 2d 20 53 51 4c 20 73 74 61 74 65  oint - SQL state
1d909 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73  ments in progres
1d90a 73 22 2c 0a 20 20 20 20 20 20 20 20 28 75 2e 61  s",.        (u.a
1d90b 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  q.p1==SAVEPOINT_
1d90c 52 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c  ROLLBACK ? "roll
1d90d 62 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22  back": "release"
1d90e 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20  ).      );.     
1d90f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
1d910 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20  Y;.    }else{.. 
1d911 20 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e       /* Determin
1d912 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
1d913 20 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73   this is a trans
1d914 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74  action savepoint
1d915 2e 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a  . If so,.      *
1d916 2a 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20  * and this is a 
1d917 52 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c  RELEASE command,
1d918 20 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e   then the curren
1d919 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  t transaction.  
1d91a 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74      ** is commit
1d91b 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ted..      */.  
1d91c 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e 73 61      int isTransa
1d91d 63 74 69 6f 6e 20 3d 20 75 2e 61 71 2e 70 53 61  ction = u.aq.pSa
1d91e 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d  vepoint->pNext==
1d91f 30 20 26 26 20 64 62 2d 3e 69 73 54 72 61 6e 73  0 && db->isTrans
1d920 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b  actionSavepoint;
1d921 0a 20 20 20 20 20 20 69 66 28 20 69 73 54 72 61  .      if( isTra
1d922 6e 73 61 63 74 69 6f 6e 20 26 26 20 75 2e 61 71  nsaction && u.aq
1d923 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  .p1==SAVEPOINT_R
1d924 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20  ELEASE ){.      
1d925 20 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69    if( (rc = sqli
1d926 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70  te3VdbeCheckFk(p
1d927 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b  , 1))!=SQLITE_OK
1d928 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
1d929 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a  to vdbe_return;.
1d92a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1d92b 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74    db->autoCommit
1d92c 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66   = 1;.        if
1d92d 28 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c  ( sqlite3VdbeHal
1d92e 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53  t(p)==SQLITE_BUS
1d92f 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  Y ){.          p
1d930 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20  ->pc = pc;.     
1d931 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d       db->autoCom
1d932 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  mit = 0;.       
1d933 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20     p->rc = rc = 
1d934 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
1d935 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65         goto vdbe
1d936 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20  _return;.       
1d937 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 69   }.        db->i
1d938 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65  sTransactionSave
1d939 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20  point = 0;.     
1d93a 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20     rc = p->rc;. 
1d93b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d93c 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f      u.aq.iSavepo
1d93d 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70  int = db->nSavep
1d93e 6f 69 6e 74 20 2d 20 75 2e 61 71 2e 69 53 61 76  oint - u.aq.iSav
1d93f 65 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20  epoint - 1;.    
1d940 20 20 20 20 66 6f 72 28 75 2e 61 71 2e 69 69 3d      for(u.aq.ii=
1d941 30 3b 20 75 2e 61 71 2e 69 69 3c 64 62 2d 3e 6e  0; u.aq.ii<db->n
1d942 44 62 3b 20 75 2e 61 71 2e 69 69 2b 2b 29 7b 0a  Db; u.aq.ii++){.
1d943 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
1d944 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70  qlite3BtreeSavep
1d945 6f 69 6e 74 28 64 62 2d 3e 61 44 62 5b 75 2e 61  oint(db->aDb[u.a
1d946 71 2e 69 69 5d 2e 70 42 74 2c 20 75 2e 61 71 2e  q.ii].pBt, u.aq.
1d947 70 31 2c 20 75 2e 61 71 2e 69 53 61 76 65 70 6f  p1, u.aq.iSavepo
1d948 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  int);.          
1d949 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1d94a 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
1d94b 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1d94c 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
1d94d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
1d94e 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 71          if( u.aq
1d94f 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  .p1==SAVEPOINT_R
1d950 4f 4c 4c 42 41 43 4b 20 26 26 20 28 64 62 2d 3e  OLLBACK && (db->
1d951 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74  flags&SQLITE_Int
1d952 65 72 6e 43 68 61 6e 67 65 73 29 21 3d 30 20 29  ernChanges)!=0 )
1d953 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
1d954 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
1d955 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b  dStatements(db);
1d956 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1d957 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
1d958 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20  chema(db, 0);.  
1d959 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1d95a 0a 20 20 20 20 20 20 2f 2a 20 52 65 67 61 72 64  .      /* Regard
1d95b 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20  less of whether 
1d95c 74 68 69 73 20 69 73 20 61 20 52 45 4c 45 41 53  this is a RELEAS
1d95d 45 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c 20 64  E or ROLLBACK, d
1d95e 65 73 74 72 6f 79 20 61 6c 6c 0a 20 20 20 20 20  estroy all.     
1d95f 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 6e   ** savepoints n
1d960 65 73 74 65 64 20 69 6e 73 69 64 65 20 6f 66 20  ested inside of 
1d961 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 62 65  the savepoint be
1d962 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e  ing operated on.
1d963 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28   */.      while(
1d964 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21   db->pSavepoint!
1d965 3d 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74  =u.aq.pSavepoint
1d966 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71   ){.        u.aq
1d967 2e 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76  .pTmp = db->pSav
1d968 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20  epoint;.        
1d969 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d  db->pSavepoint =
1d96a 20 75 2e 61 71 2e 70 54 6d 70 2d 3e 70 4e 65 78   u.aq.pTmp->pNex
1d96b 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  t;.        sqlit
1d96c 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 61  e3DbFree(db, u.a
1d96d 71 2e 70 54 6d 70 29 3b 0a 20 20 20 20 20 20 20  q.pTmp);.       
1d96e 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d   db->nSavepoint-
1d96f 2d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  -;.      }..    
1d970 20 20 2f 2a 20 49 66 20 69 74 20 69 73 20 61 20    /* If it is a 
1d971 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 64 65  RELEASE, then de
1d972 73 74 72 6f 79 20 74 68 65 20 73 61 76 65 70 6f  stroy the savepo
1d973 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74  int being operat
1d974 65 64 20 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 74  ed on.      ** t
1d975 6f 6f 2e 20 49 66 20 69 74 20 69 73 20 61 20 52  oo. If it is a R
1d976 4f 4c 4c 42 41 43 4b 20 54 4f 2c 20 74 68 65 6e  OLLBACK TO, then
1d977 20 73 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20   set the number 
1d978 6f 66 20 64 65 66 65 72 72 65 64 0a 20 20 20 20  of deferred.    
1d979 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20    ** constraint 
1d97a 76 69 6f 6c 61 74 69 6f 6e 73 20 70 72 65 73 65  violations prese
1d97b 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nt in the databa
1d97c 73 65 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  se to the value 
1d97d 73 74 6f 72 65 64 0a 20 20 20 20 20 20 2a 2a 20  stored.      ** 
1d97e 77 68 65 6e 20 74 68 65 20 73 61 76 65 70 6f 69  when the savepoi
1d97f 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 20  nt was created. 
1d980 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 75 2e   */.      if( u.
1d981 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54  aq.p1==SAVEPOINT
1d982 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20  _RELEASE ){.    
1d983 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 71      assert( u.aq
1d984 2e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64 62 2d  .pSavepoint==db-
1d985 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20  >pSavepoint );. 
1d986 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65         db->pSave
1d987 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61  point = u.aq.pSa
1d988 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a  vepoint->pNext;.
1d989 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1d98a 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71 2e 70  bFree(db, u.aq.p
1d98b 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20  Savepoint);.    
1d98c 20 20 20 20 69 66 28 20 21 69 73 54 72 61 6e 73      if( !isTrans
1d98d 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  action ){.      
1d98e 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69      db->nSavepoi
1d98f 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  nt--;.        }.
1d990 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1d991 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72       db->nDeferr
1d992 65 64 43 6f 6e 73 20 3d 20 75 2e 61 71 2e 70 53  edCons = u.aq.pS
1d993 61 76 65 70 6f 69 6e 74 2d 3e 6e 44 65 66 65 72  avepoint->nDefer
1d994 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d  redCons;.      }
1d995 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72  .    }.  }..  br
1d996 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1d997 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31  e: AutoCommit P1
1d998 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1d999 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65  Set the database
1d99a 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61   auto-commit fla
1d99b 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29  g to P1 (1 or 0)
1d99c 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c  . If P2 is true,
1d99d 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e   roll.** back an
1d99e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69  y currently acti
1d99f 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63  ve btree transac
1d9a0 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20  tions. If there 
1d9a1 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a  are any active.*
1d9a2 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f  * VMs (apart fro
1d9a3 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65  m this one), the
1d9a4 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69  n a ROLLBACK fai
1d9a5 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66 61  ls.  A COMMIT fa
1d9a6 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65 20  ils if.** there 
1d9a7 61 72 65 20 61 63 74 69 76 65 20 77 72 69 74 69  are active writi
1d9a8 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76 65  ng VMs or active
1d9a9 20 56 4d 73 20 74 68 61 74 20 75 73 65 20 73 68   VMs that use sh
1d9aa 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a  ared cache..**.*
1d9ab 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  * This instructi
1d9ac 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d  on causes the VM
1d9ad 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73   to halt..*/.cas
1d9ae 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a  e OP_AutoCommit:
1d9af 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1d9b0 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d9b1 65 64 20 69 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a  ed into u.ar */.
1d9b2 20 20 69 6e 74 20 64 65 73 69 72 65 64 41 75 74    int desiredAut
1d9b3 6f 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74 20 69  oCommit;.  int i
1d9b4 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e 74 20  Rollback;.  int 
1d9b5 74 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64 69 66  turnOnAC;.#endif
1d9b6 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d9b7 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d9b8 2e 61 72 20 2a 2f 0a 0a 20 20 75 2e 61 72 2e 64  .ar */..  u.ar.d
1d9b9 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
1d9ba 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e   = pOp->p1;.  u.
1d9bb 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d 20 70  ar.iRollback = p
1d9bc 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 72 2e 74  Op->p2;.  u.ar.t
1d9bd 75 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 72 2e 64  urnOnAC = u.ar.d
1d9be 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
1d9bf 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d   && !db->autoCom
1d9c0 6d 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75  mit;.  assert( u
1d9c1 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43  .ar.desiredAutoC
1d9c2 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e 61 72  ommit==1 || u.ar
1d9c3 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d  .desiredAutoComm
1d9c4 69 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  it==0 );.  asser
1d9c5 74 28 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41  t( u.ar.desiredA
1d9c6 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20  utoCommit==1 || 
1d9c7 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 3d 3d  u.ar.iRollback==
1d9c8 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  0 );.  assert( d
1d9c9 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
1d9ca 3e 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61  >0 );  /* At lea
1d9cb 73 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69  st this one VM i
1d9cc 73 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69  s active */..  i
1d9cd 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43  f( u.ar.turnOnAC
1d9ce 20 26 26 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61   && u.ar.iRollba
1d9cf 63 6b 20 26 26 20 64 62 2d 3e 61 63 74 69 76 65  ck && db->active
1d9d0 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20  VdbeCnt>1 ){.   
1d9d1 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74   /* If this inst
1d9d2 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  ruction implemen
1d9d3 74 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e  ts a ROLLBACK an
1d9d4 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 0a  d other VMs are.
1d9d5 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72 75 6e      ** still run
1d9d6 6e 69 6e 67 2c 20 61 6e 64 20 61 20 74 72 61 6e  ning, and a tran
1d9d7 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76  saction is activ
1d9d8 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  e, return an err
1d9d9 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 0a 20 20  or indicating.  
1d9da 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 74    ** that the ot
1d9db 68 65 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d  her VMs must com
1d9dc 70 6c 65 74 65 20 66 69 72 73 74 2e 0a 20 20 20  plete first..   
1d9dd 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53   */.    sqlite3S
1d9de 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1d9df 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f  rMsg, db, "canno
1d9e0 74 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73  t rollback trans
1d9e1 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20  action - ".     
1d9e2 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e     "SQL statemen
1d9e3 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29  ts in progress")
1d9e4 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
1d9e5 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20  E_BUSY;.  }else 
1d9e6 69 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41  if( u.ar.turnOnA
1d9e7 43 20 26 26 20 21 75 2e 61 72 2e 69 52 6f 6c 6c  C && !u.ar.iRoll
1d9e8 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 69 74  back && db->writ
1d9e9 65 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20  eVdbeCnt>0 ){.  
1d9ea 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73    /* If this ins
1d9eb 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65  truction impleme
1d9ec 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64  nts a COMMIT and
1d9ed 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 20 77   other VMs are w
1d9ee 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65  riting.    ** re
1d9ef 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e  turn an error in
1d9f0 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 68  dicating that th
1d9f1 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74  e other VMs must
1d9f2 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e   complete first.
1d9f3 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
1d9f4 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1d9f5 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63  >zErrMsg, db, "c
1d9f6 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 72 61  annot commit tra
1d9f7 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20  nsaction - ".   
1d9f8 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d       "SQL statem
1d9f9 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73  ents in progress
1d9fa 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ");.    rc = SQL
1d9fb 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73  ITE_BUSY;.  }els
1d9fc 65 20 69 66 28 20 75 2e 61 72 2e 64 65 73 69 72  e if( u.ar.desir
1d9fd 65 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62  edAutoCommit!=db
1d9fe 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
1d9ff 20 20 20 20 69 66 28 20 75 2e 61 72 2e 69 52 6f      if( u.ar.iRo
1da00 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20  llback ){.      
1da01 61 73 73 65 72 74 28 20 75 2e 61 72 2e 64 65 73  assert( u.ar.des
1da02 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d  iredAutoCommit==
1da03 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  1 );.      sqlit
1da04 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62  e3RollbackAll(db
1da05 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74  );.      db->aut
1da06 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20  oCommit = 1;.   
1da07 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20 3d   }else if( (rc =
1da08 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63   sqlite3VdbeChec
1da09 6b 46 6b 28 70 2c 20 31 29 29 21 3d 53 51 4c 49  kFk(p, 1))!=SQLI
1da0a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67  TE_OK ){.      g
1da0b 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b  oto vdbe_return;
1da0c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1da0d 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74    db->autoCommit
1da0e 20 3d 20 28 75 38 29 75 2e 61 72 2e 64 65 73 69   = (u8)u.ar.desi
1da0f 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20  redAutoCommit;. 
1da10 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1da11 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c  VdbeHalt(p)==SQL
1da12 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
1da13 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a      p->pc = pc;.
1da14 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f          db->auto
1da15 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28 31 2d  Commit = (u8)(1-
1da16 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f  u.ar.desiredAuto
1da17 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 20 20 20  Commit);.       
1da18 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51   p->rc = rc = SQ
1da19 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20  LITE_BUSY;.     
1da1a 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74     goto vdbe_ret
1da1b 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
1da1c 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64   }.    assert( d
1da1d 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30  b->nStatement==0
1da1e 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43   );.    sqlite3C
1da1f 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 64  loseSavepoints(d
1da20 62 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 72  b);.    if( p->r
1da21 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1da22 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1da23 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73  E_DONE;.    }els
1da24 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  e{.      rc = SQ
1da25 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
1da26 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f  }.    goto vdbe_
1da27 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b  return;.  }else{
1da28 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1da29 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1da2a 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 28  g, db,.        (
1da2b 21 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74  !u.ar.desiredAut
1da2c 6f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f 74  oCommit)?"cannot
1da2d 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63   start a transac
1da2e 74 69 6f 6e 20 77 69 74 68 69 6e 20 61 20 74 72  tion within a tr
1da2f 61 6e 73 61 63 74 69 6f 6e 22 3a 28 0a 20 20 20  ansaction":(.   
1da30 20 20 20 20 20 28 75 2e 61 72 2e 69 52 6f 6c 6c       (u.ar.iRoll
1da31 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20 72 6f  back)?"cannot ro
1da32 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72 61 6e  llback - no tran
1da33 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76  saction is activ
1da34 65 22 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20  e":.            
1da35 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 63         "cannot c
1da36 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61 6e 73  ommit - no trans
1da37 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
1da38 22 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 53  "));..    rc = S
1da39 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
1da3a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1da3b 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 63 74  Opcode: Transact
1da3c 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ion P1 P2 * * *.
1da3d 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72  **.** Begin a tr
1da3e 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 65 20  ansaction.  The 
1da3f 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73  transaction ends
1da40 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f   when a Commit o
1da41 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70  r Rollback.** op
1da42 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e 74 65  code is encounte
1da43 72 65 64 2e 20 20 44 65 70 65 6e 64 69 6e 67 20  red.  Depending 
1da44 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49  on the ON CONFLI
1da45 43 54 20 73 65 74 74 69 6e 67 2c 20 74 68 65 0a  CT setting, the.
1da46 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d  ** transaction m
1da47 69 67 68 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c  ight also be rol
1da48 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65  led back if an e
1da49 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
1da4a 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73  red..**.** P1 is
1da4b 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
1da4c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1da4d 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74 72 61  on which the tra
1da4e 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73  nsaction is.** s
1da4f 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78 20 30  tarted.  Index 0
1da50 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74   is the main dat
1da51 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69  abase file and i
1da52 6e 64 65 78 20 31 20 69 73 20 74 68 65 0a 2a 2a  ndex 1 is the.**
1da53 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 74   file used for t
1da54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
1da55 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32 20 6f    Indices of 2 o
1da56 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65 64 20  r more are used 
1da57 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 64 20  for.** attached 
1da58 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a  databases..**.**
1da59 20 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d 7a 65   If P2 is non-ze
1da5a 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 74 65  ro, then a write
1da5b 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20  -transaction is 
1da5c 73 74 61 72 74 65 64 2e 20 20 41 20 52 45 53 45  started.  A RESE
1da5d 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20  RVED lock is.** 
1da5e 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20  obtained on the 
1da5f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68  database file wh
1da60 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73  en a write-trans
1da61 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65  action is starte
1da62 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 20  d.  No.** other 
1da63 70 72 6f 63 65 73 73 20 63 61 6e 20 73 74 61 72  process can star
1da64 74 20 61 6e 6f 74 68 65 72 20 77 72 69 74 65 20  t another write 
1da65 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 6c  transaction whil
1da66 65 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  e this transacti
1da67 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 77 61  on is.** underwa
1da68 79 2e 20 20 53 74 61 72 74 69 6e 67 20 61 20 77  y.  Starting a w
1da69 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
1da6a 20 61 6c 73 6f 20 63 72 65 61 74 65 73 20 61 20   also creates a 
1da6b 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
1da6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 72 61  . A.** write tra
1da6d 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  nsaction must be
1da6e 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20   started before 
1da6f 61 6e 79 20 63 68 61 6e 67 65 73 20 63 61 6e 20  any changes can 
1da70 62 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a 2a  be made to the.*
1da71 2a 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20  * database.  If 
1da72 50 32 20 69 73 20 32 20 6f 72 20 67 72 65 61 74  P2 is 2 or great
1da73 65 72 20 74 68 65 6e 20 61 6e 20 45 58 43 4c 55  er then an EXCLU
1da74 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 6c 73  SIVE lock is als
1da75 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e  o obtained.** on
1da76 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a   the file..**.**
1da77 20 49 66 20 61 20 77 72 69 74 65 2d 74 72 61 6e   If a write-tran
1da78 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74  saction is start
1da79 65 64 20 61 6e 64 20 74 68 65 20 56 64 62 65 2e  ed and the Vdbe.
1da7a 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20  usesStmtJournal 
1da7b 66 6c 61 67 20 69 73 0a 2a 2a 20 74 72 75 65 20  flag is.** true 
1da7c 28 74 68 69 73 20 66 6c 61 67 20 69 73 20 73 65  (this flag is se
1da7d 74 20 69 66 20 74 68 65 20 56 64 62 65 20 6d 61  t if the Vdbe ma
1da7e 79 20 6d 6f 64 69 66 79 20 6d 6f 72 65 20 74 68  y modify more th
1da7f 61 6e 20 6f 6e 65 20 72 6f 77 20 61 6e 64 20 6d  an one row and m
1da80 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41  ay.** throw an A
1da81 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e 29 2c  BORT exception),
1da82 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61   a statement tra
1da83 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 61 6c 73  nsaction may als
1da84 6f 20 62 65 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20  o be opened..** 
1da85 4d 6f 72 65 20 73 70 65 63 69 66 69 63 61 6c 6c  More specificall
1da86 79 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  y, a statement t
1da87 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70  ransaction is op
1da88 65 6e 65 64 20 69 66 66 20 74 68 65 20 64 61 74  ened iff the dat
1da89 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
1da8a 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ion is currently
1da8b 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d   not in autocomm
1da8c 69 74 20 6d 6f 64 65 2c 20 6f 72 20 69 66 20 74  it mode, or if t
1da8d 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 0a 2a  here are other.*
1da8e 2a 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65  * active stateme
1da8f 6e 74 73 2e 20 41 20 73 74 61 74 65 6d 65 6e 74  nts. A statement
1da90 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 6c   transaction all
1da91 6f 77 73 20 74 68 65 20 61 66 66 65 63 74 73 20  ows the affects 
1da92 6f 66 20 74 68 69 73 0a 2a 2a 20 56 44 42 45 20  of this.** VDBE 
1da93 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  to be rolled bac
1da94 6b 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72  k after an error
1da95 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
1da96 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65  to roll back the
1da97 0a 2a 2a 20 65 6e 74 69 72 65 20 74 72 61 6e 73  .** entire trans
1da98 61 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 65 72  action. If no er
1da99 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
1da9a 65 64 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ed, the statemen
1da9b 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  t transaction.**
1da9c 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61   will automatica
1da9d 6c 6c 79 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20  lly commit when 
1da9e 74 68 65 20 56 44 42 45 20 68 61 6c 74 73 2e 0a  the VDBE halts..
1da9f 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 7a  **.** If P2 is z
1daa0 65 72 6f 2c 20 74 68 65 6e 20 61 20 72 65 61 64  ero, then a read
1daa1 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65  -lock is obtaine
1daa2 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  d on the databas
1daa3 65 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20  e file..*/.case 
1daa4 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 3a 20  OP_Transaction: 
1daa5 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1daa6 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1daa7 64 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 20  d into u.as */. 
1daa8 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e   Btree *pBt;.#en
1daa9 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1daaa 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1daab 6f 20 75 2e 61 73 20 2a 2f 0a 0a 20 20 61 73 73  o u.as */..  ass
1daac 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1daad 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e  && pOp->p1<db->n
1daae 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
1daaf 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20  (p->btreeMask & 
1dab0 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30  (1<<pOp->p1))!=0
1dab1 20 29 3b 0a 20 20 75 2e 61 73 2e 70 42 74 20 3d   );.  u.as.pBt =
1dab2 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31   db->aDb[pOp->p1
1dab3 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20 75 2e  ].pBt;..  if( u.
1dab4 61 73 2e 70 42 74 20 29 7b 0a 20 20 20 20 72 63  as.pBt ){.    rc
1dab5 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42   = sqlite3BtreeB
1dab6 65 67 69 6e 54 72 61 6e 73 28 75 2e 61 73 2e 70  eginTrans(u.as.p
1dab7 42 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20  Bt, pOp->p2);.  
1dab8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1dab9 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 70  _BUSY ){.      p
1daba 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20  ->pc = pc;.     
1dabb 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51   p->rc = rc = SQ
1dabc 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20  LITE_BUSY;.     
1dabd 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72   goto vdbe_retur
1dabe 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  n;.    }.    if(
1dabf 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1dac0 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f  {.      goto abo
1dac1 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1dac2 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
1dac3 70 4f 70 2d 3e 70 32 20 26 26 20 70 2d 3e 75 73  pOp->p2 && p->us
1dac4 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 0a 20 20  esStmtJournal.  
1dac5 20 20 20 26 26 20 28 64 62 2d 3e 61 75 74 6f 43     && (db->autoC
1dac6 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e  ommit==0 || db->
1dac7 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 29  activeVdbeCnt>1)
1dac8 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 61 73  .    ){.      as
1dac9 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
1daca 65 65 49 73 49 6e 54 72 61 6e 73 28 75 2e 61 73  eeIsInTrans(u.as
1dacb 2e 70 42 74 29 20 29 3b 0a 20 20 20 20 20 20 69  .pBt) );.      i
1dacc 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74  f( p->iStatement
1dacd 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ==0 ){.        a
1dace 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74  ssert( db->nStat
1dacf 65 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e  ement>=0 && db->
1dad0 6e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b  nSavepoint>=0 );
1dad1 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 74  .        db->nSt
1dad2 61 74 65 6d 65 6e 74 2b 2b 3b 0a 20 20 20 20 20  atement++;.     
1dad3 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74     p->iStatement
1dad4 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e   = db->nSavepoin
1dad5 74 20 2b 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65  t + db->nStateme
1dad6 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  nt;.      }.    
1dad7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1dad8 72 65 65 42 65 67 69 6e 53 74 6d 74 28 75 2e 61  reeBeginStmt(u.a
1dad9 73 2e 70 42 74 2c 20 70 2d 3e 69 53 74 61 74 65  s.pBt, p->iState
1dada 6d 65 6e 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ment);..      /*
1dadb 20 53 74 6f 72 65 20 74 68 65 20 63 75 72 72 65   Store the curre
1dadc 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  nt value of the 
1dadd 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73  database handles
1dade 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72   deferred constr
1dadf 61 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f  aint.      ** co
1dae0 75 6e 74 65 72 2e 20 49 66 20 74 68 65 20 73 74  unter. If the st
1dae1 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1dae2 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 62 65 20  ion needs to be 
1dae3 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 0a 20 20 20  rolled back,.   
1dae4 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20     ** the value 
1dae5 6f 66 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20  of this counter 
1dae6 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74  needs to be rest
1dae7 6f 72 65 64 20 74 6f 6f 2e 20 20 2a 2f 0a 20 20  ored too.  */.  
1dae8 20 20 20 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43      p->nStmtDefC
1dae9 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72  ons = db->nDefer
1daea 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 7d 0a 20  redCons;.    }. 
1daeb 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1daec 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 43 6f  * Opcode: ReadCo
1daed 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20  okie P1 P2 P3 * 
1daee 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 6f 6f  *.**.** Read coo
1daef 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66 72  kie number P3 fr
1daf0 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 20 61  om database P1 a
1daf1 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f  nd write it into
1daf2 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a   register P2..**
1daf3 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20 73 63   P3==1 is the sc
1daf4 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50  hema version.  P
1daf5 33 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74 61  3==2 is the data
1daf6 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20  base format..** 
1daf7 50 33 3d 3d 33 20 69 73 20 74 68 65 20 72 65 63  P3==3 is the rec
1daf8 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63  ommended pager c
1daf9 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73  ache size, and s
1dafa 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20  o forth.  P1==0 
1dafb 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64  is.** the main d
1dafc 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1dafd 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61   P1==1 is the da
1dafe 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64  tabase file used
1daff 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d   to store.** tem
1db00 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a  porary tables..*
1db01 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20  *.** There must 
1db02 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  be a read-lock o
1db03 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28  n the database (
1db04 65 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63  either a transac
1db05 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20  tion.** must be 
1db06 73 74 61 72 74 65 64 20 6f 72 20 74 68 65 72 65  started or there
1db07 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e   must be an open
1db08 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72 65 0a   cursor) before.
1db09 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74 68 69  ** executing thi
1db0a 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  s instruction..*
1db0b 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 43 6f  /.case OP_ReadCo
1db0c 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 20 20  okie: {         
1db0d 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1db0e 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20  erelease */.#if 
1db0f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1db10 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1db11 20 75 2e 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69   u.at */.  int i
1db12 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62 3b  Meta;.  int iDb;
1db13 0a 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a  .  int iCookie;.
1db14 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1db15 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1db16 69 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 0a 20 20  into u.at */..  
1db17 75 2e 61 74 2e 69 44 62 20 3d 20 70 4f 70 2d 3e  u.at.iDb = pOp->
1db18 70 31 3b 0a 20 20 75 2e 61 74 2e 69 43 6f 6f 6b  p1;.  u.at.iCook
1db19 69 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  ie = pOp->p3;.  
1db1a 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c  assert( pOp->p3<
1db1b 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d  SQLITE_N_BTREE_M
1db1c 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ETA );.  assert(
1db1d 20 75 2e 61 74 2e 69 44 62 3e 3d 30 20 26 26 20   u.at.iDb>=0 && 
1db1e 75 2e 61 74 2e 69 44 62 3c 64 62 2d 3e 6e 44 62  u.at.iDb<db->nDb
1db1f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62   );.  assert( db
1db20 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 5d 2e  ->aDb[u.at.iDb].
1db21 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  pBt!=0 );.  asse
1db22 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  rt( (p->btreeMas
1db23 6b 20 26 20 28 31 3c 3c 75 2e 61 74 2e 69 44 62  k & (1<<u.at.iDb
1db24 29 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71 6c 69  ))!=0 );..  sqli
1db25 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28  te3BtreeGetMeta(
1db26 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62  db->aDb[u.at.iDb
1db27 5d 2e 70 42 74 2c 20 75 2e 61 74 2e 69 43 6f 6f  ].pBt, u.at.iCoo
1db28 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 75 2e 61  kie, (u32 *)&u.a
1db29 74 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f 75 74  t.iMeta);.  pOut
1db2a 2d 3e 75 2e 69 20 3d 20 75 2e 61 74 2e 69 4d 65  ->u.i = u.at.iMe
1db2b 74 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65  ta;.  MemSetType
1db2c 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1db2d 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  nt);.  break;.}.
1db2e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 43  ./* Opcode: SetC
1db2f 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a  ookie P1 P2 P3 *
1db30 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74   *.**.** Write t
1db31 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
1db32 67 69 73 74 65 72 20 50 33 20 28 69 6e 74 65 72  gister P3 (inter
1db33 70 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e 74  preted as an int
1db34 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f  eger).** into co
1db35 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f  okie number P2 o
1db36 66 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20  f database P1.  
1db37 50 32 3d 3d 31 20 69 73 20 74 68 65 20 73 63 68  P2==1 is the sch
1db38 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a 2a  ema version.  .*
1db39 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 20 64  * P2==2 is the d
1db3a 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 20  atabase format. 
1db3b 50 32 3d 3d 33 20 69 73 20 74 68 65 20 72 65 63  P2==3 is the rec
1db3c 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63  ommended pager c
1db3d 61 63 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20 61  ache .** size, a
1db3e 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31  nd so forth.  P1
1db3f 3d 3d 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20  ==0 is the main 
1db40 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
1db41 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 0a  d P1==1 is the .
1db42 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
1db43 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
1db44 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
1db45 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63  .**.** A transac
1db46 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61  tion must be sta
1db47 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 65 63  rted before exec
1db48 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64  uting this opcod
1db49 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65  e..*/.case OP_Se
1db4a 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20  tCookie: {      
1db4b 20 2f 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30   /* in3 */.#if 0
1db4c 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1db4d 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1db4e 75 2e 61 75 20 2a 2f 0a 20 20 44 62 20 2a 70 44  u.au */.  Db *pD
1db4f 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  b;.#endif /* loc
1db50 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1db51 65 64 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f 0a  ed into u.au */.
1db52 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1db53 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45  2<SQLITE_N_BTREE
1db54 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72  _META );.  asser
1db55 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1db56 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62   pOp->p1<db->nDb
1db57 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
1db58 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1db59 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29  <<pOp->p1))!=0 )
1db5a 3b 0a 20 20 75 2e 61 75 2e 70 44 62 20 3d 20 26  ;.  u.au.pDb = &
1db5b 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d  db->aDb[pOp->p1]
1db5c 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 75  ;.  assert( u.au
1db5d 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a  .pDb->pBt!=0 );.
1db5e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1db5f 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33 29  Integerify(pIn3)
1db60 3b 0a 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65 20  ;.  /* See note 
1db61 61 62 6f 75 74 20 69 6e 64 65 78 20 73 68 69 66  about index shif
1db62 74 69 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64 43  ting on OP_ReadC
1db63 6f 6f 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d 20  ookie */.  rc = 
1db64 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61  sqlite3BtreeUpda
1db65 74 65 4d 65 74 61 28 75 2e 61 75 2e 70 44 62 2d  teMeta(u.au.pDb-
1db66 3e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 28  >pBt, pOp->p2, (
1db67 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a  int)pIn3->u.i);.
1db68 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42    if( pOp->p2==B
1db69 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53  TREE_SCHEMA_VERS
1db6a 49 4f 4e 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68  ION ){.    /* Wh
1db6b 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  en the schema co
1db6c 6f 6b 69 65 20 63 68 61 6e 67 65 73 2c 20 72 65  okie changes, re
1db6d 63 6f 72 64 20 74 68 65 20 6e 65 77 20 63 6f 6f  cord the new coo
1db6e 6b 69 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a  kie internally *
1db6f 2f 0a 20 20 20 20 75 2e 61 75 2e 70 44 62 2d 3e  /.    u.au.pDb->
1db70 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f  pSchema->schema_
1db71 63 6f 6f 6b 69 65 20 3d 20 28 69 6e 74 29 70 49  cookie = (int)pI
1db72 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d  n3->u.i;.    db-
1db73 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45  >flags |= SQLITE
1db74 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a  _InternChanges;.
1db75 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d    }else if( pOp-
1db76 3e 70 32 3d 3d 42 54 52 45 45 5f 46 49 4c 45 5f  >p2==BTREE_FILE_
1db77 46 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20 2f 2a  FORMAT ){.    /*
1db78 20 52 65 63 6f 72 64 20 63 68 61 6e 67 65 73 20   Record changes 
1db79 69 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d  in the file form
1db7a 61 74 20 2a 2f 0a 20 20 20 20 75 2e 61 75 2e 70  at */.    u.au.p
1db7b 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c  Db->pSchema->fil
1db7c 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38 29 70  e_format = (u8)p
1db7d 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20  In3->u.i;.  }.  
1db7e 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 31 20 29  if( pOp->p1==1 )
1db7f 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61 6c 69 64  {.    /* Invalid
1db80 61 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65 64  ate all prepared
1db81 20 73 74 61 74 65 6d 65 6e 74 73 20 77 68 65 6e   statements when
1db82 65 76 65 72 20 74 68 65 20 54 45 4d 50 20 64 61  ever the TEMP da
1db83 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 73 63  tabase.    ** sc
1db84 68 65 6d 61 20 69 73 20 63 68 61 6e 67 65 64 2e  hema is changed.
1db85 20 20 54 69 63 6b 65 74 20 23 31 36 34 34 20 2a    Ticket #1644 *
1db86 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  /.    sqlite3Exp
1db87 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
1db88 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 70  ments(db);.    p
1db89 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 20  ->expired = 0;. 
1db8a 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1db8b 2a 20 4f 70 63 6f 64 65 3a 20 56 65 72 69 66 79  * Opcode: Verify
1db8c 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 2a 0a 2a  Cookie P1 P2 *.*
1db8d 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 76  *.** Check the v
1db8e 61 6c 75 65 20 6f 66 20 67 6c 6f 62 61 6c 20 64  alue of global d
1db8f 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 65  atabase paramete
1db90 72 20 6e 75 6d 62 65 72 20 30 20 28 74 68 65 0a  r number 0 (the.
1db91 2a 2a 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f  ** schema versio
1db92 6e 29 20 61 6e 64 20 6d 61 6b 65 20 73 75 72 65  n) and make sure
1db93 20 69 74 20 69 73 20 65 71 75 61 6c 20 74 6f 20   it is equal to 
1db94 50 32 2e 20 20 0a 2a 2a 20 50 31 20 69 73 20 74  P2.  .** P1 is t
1db95 68 65 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62  he database numb
1db96 65 72 20 77 68 69 63 68 20 69 73 20 30 20 66 6f  er which is 0 fo
1db97 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
1db98 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20  ase file.** and 
1db99 31 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 68  1 for the file h
1db9a 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79  olding temporary
1db9b 20 74 61 62 6c 65 73 20 61 6e 64 20 73 6f 6d 65   tables and some
1db9c 20 68 69 67 68 65 72 20 6e 75 6d 62 65 72 0a 2a   higher number.*
1db9d 2a 20 66 6f 72 20 61 75 78 69 6c 69 61 72 79 20  * for auxiliary 
1db9e 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a  databases..**.**
1db9f 20 54 68 65 20 63 6f 6f 6b 69 65 20 63 68 61 6e   The cookie chan
1dba0 67 65 73 20 69 74 73 20 76 61 6c 75 65 20 77 68  ges its value wh
1dba1 65 6e 65 76 65 72 20 74 68 65 20 64 61 74 61 62  enever the datab
1dba2 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ase schema chang
1dba3 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 65 72  es..** This oper
1dba4 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ation is used to
1dba5 20 64 65 74 65 63 74 20 77 68 65 6e 20 74 68 61   detect when tha
1dba6 74 20 74 68 65 20 63 6f 6f 6b 69 65 20 68 61 73  t the cookie has
1dba7 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20   changed.** and 
1dba8 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74  that the current
1dba9 20 70 72 6f 63 65 73 73 20 6e 65 65 64 73 20 74   process needs t
1dbaa 6f 20 72 65 72 65 61 64 20 74 68 65 20 73 63 68  o reread the sch
1dbab 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65  ema..**.** Eithe
1dbac 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  r a transaction 
1dbad 6e 65 65 64 73 20 74 6f 20 68 61 76 65 20 62 65  needs to have be
1dbae 65 6e 20 73 74 61 72 74 65 64 20 6f 72 20 61 6e  en started or an
1dbaf 20 4f 50 5f 4f 70 65 6e 20 6e 65 65 64 73 0a 2a   OP_Open needs.*
1dbb0 2a 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64  * to be executed
1dbb1 20 28 74 6f 20 65 73 74 61 62 6c 69 73 68 20 61   (to establish a
1dbb2 20 72 65 61 64 20 6c 6f 63 6b 29 20 62 65 66 6f   read lock) befo
1dbb3 72 65 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69  re this opcode i
1dbb4 73 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f  s.** invoked..*/
1dbb5 0a 63 61 73 65 20 4f 50 5f 56 65 72 69 66 79 43  .case OP_VerifyC
1dbb6 6f 6f 6b 69 65 3a 20 7b 0a 23 69 66 20 30 20 20  ookie: {.#if 0  
1dbb7 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1dbb8 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1dbb9 61 76 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 74  av */.  int iMet
1dbba 61 3b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b  a;.  Btree *pBt;
1dbbb 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1dbbc 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1dbbd 20 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20   into u.av */.  
1dbbe 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1dbbf 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62  =0 && pOp->p1<db
1dbc0 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
1dbc1 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1dbc2 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29   & (1<<pOp->p1))
1dbc3 21 3d 30 20 29 3b 0a 20 20 75 2e 61 76 2e 70 42  !=0 );.  u.av.pB
1dbc4 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d  t = db->aDb[pOp-
1dbc5 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20  >p1].pBt;.  if( 
1dbc6 75 2e 61 76 2e 70 42 74 20 29 7b 0a 20 20 20 20  u.av.pBt ){.    
1dbc7 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
1dbc8 65 74 61 28 75 2e 61 76 2e 70 42 74 2c 20 42 54  eta(u.av.pBt, BT
1dbc9 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49  REE_SCHEMA_VERSI
1dbca 4f 4e 2c 20 28 75 33 32 20 2a 29 26 75 2e 61 76  ON, (u32 *)&u.av
1dbcb 2e 69 4d 65 74 61 29 3b 0a 20 20 7d 65 6c 73 65  .iMeta);.  }else
1dbcc 7b 0a 20 20 20 20 75 2e 61 76 2e 69 4d 65 74 61  {.    u.av.iMeta
1dbcd 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
1dbce 75 2e 61 76 2e 69 4d 65 74 61 21 3d 70 4f 70 2d  u.av.iMeta!=pOp-
1dbcf 3e 70 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >p2 ){.    sqlit
1dbd0 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1dbd1 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d  zErrMsg);.    p-
1dbd2 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  >zErrMsg = sqlit
1dbd3 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 22  e3DbStrDup(db, "
1dbd4 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
1dbd5 68 61 73 20 63 68 61 6e 67 65 64 22 29 3b 0a 20  has changed");. 
1dbd6 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 63 68     /* If the sch
1dbd7 65 6d 61 2d 63 6f 6f 6b 69 65 20 66 72 6f 6d 20  ema-cookie from 
1dbd8 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1dbd9 65 20 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f  e matches the co
1dbda 6f 6b 69 65 0a 20 20 20 20 2a 2a 20 73 74 6f 72  okie.    ** stor
1dbdb 65 64 20 77 69 74 68 20 74 68 65 20 69 6e 2d 6d  ed with the in-m
1dbdc 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61  emory representa
1dbdd 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 68 65  tion of the sche
1dbde 6d 61 2c 20 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f  ma, do.    ** no
1dbdf 74 20 72 65 6c 6f 61 64 20 74 68 65 20 73 63 68  t reload the sch
1dbe0 65 6d 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74  ema from the dat
1dbe1 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20  abase file..    
1dbe2 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 76 69 72  **.    ** If vir
1dbe3 74 75 61 6c 2d 74 61 62 6c 65 73 20 61 72 65 20  tual-tables are 
1dbe4 69 6e 20 75 73 65 2c 20 74 68 69 73 20 69 73 20  in use, this is 
1dbe5 6e 6f 74 20 6a 75 73 74 20 61 6e 20 6f 70 74 69  not just an opti
1dbe6 6d 69 7a 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a  mization..    **
1dbe7 20 4f 66 74 65 6e 2c 20 76 2d 74 61 62 6c 65 73   Often, v-tables
1dbe8 20 73 74 6f 72 65 20 74 68 65 69 72 20 64 61 74   store their dat
1dbe9 61 20 69 6e 20 6f 74 68 65 72 20 53 51 4c 69 74  a in other SQLit
1dbea 65 20 74 61 62 6c 65 73 2c 20 77 68 69 63 68 0a  e tables, which.
1dbeb 20 20 20 20 2a 2a 20 61 72 65 20 71 75 65 72 69      ** are queri
1dbec 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 78  ed from within x
1dbed 4e 65 78 74 28 29 20 61 6e 64 20 6f 74 68 65 72  Next() and other
1dbee 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64 73   v-table methods
1dbef 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 70 72   using.    ** pr
1dbf0 65 70 61 72 65 64 20 71 75 65 72 69 65 73 2e 20  epared queries. 
1dbf1 49 66 20 73 75 63 68 20 61 20 71 75 65 72 79 20  If such a query 
1dbf2 69 73 20 6f 75 74 2d 6f 66 2d 64 61 74 65 2c 20  is out-of-date, 
1dbf3 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74  we do not want t
1dbf4 6f 0a 20 20 20 20 2a 2a 20 64 69 73 63 61 72 64  o.    ** discard
1dbf5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
1dbf6 68 65 6d 61 2c 20 61 73 20 74 68 65 20 75 73 65  hema, as the use
1dbf7 72 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74  r code implement
1dbf8 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 76  ing the.    ** v
1dbf9 2d 74 61 62 6c 65 20 77 6f 75 6c 64 20 68 61 76  -table would hav
1dbfa 65 20 74 6f 20 62 65 20 72 65 61 64 79 20 66 6f  e to be ready fo
1dbfb 72 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74  r the sqlite3_vt
1dbfc 61 62 20 73 74 72 75 63 74 75 72 65 20 69 74 73  ab structure its
1dbfd 65 6c 66 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65  elf.    ** to be
1dbfe 20 69 6e 76 61 6c 69 64 61 74 65 64 20 77 68 65   invalidated whe
1dbff 6e 65 76 65 72 20 73 71 6c 69 74 65 33 5f 73 74  never sqlite3_st
1dc00 65 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20 66  ep() is called f
1dc01 72 6f 6d 20 77 69 74 68 69 6e 0a 20 20 20 20 2a  rom within.    *
1dc02 2a 20 61 20 76 2d 74 61 62 6c 65 20 6d 65 74 68  * a v-table meth
1dc03 6f 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  od..    */.    i
1dc04 66 28 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e  f( db->aDb[pOp->
1dc05 70 31 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68  p1].pSchema->sch
1dc06 65 6d 61 5f 63 6f 6f 6b 69 65 21 3d 75 2e 61 76  ema_cookie!=u.av
1dc07 2e 69 4d 65 74 61 20 29 7b 0a 20 20 20 20 20 20  .iMeta ){.      
1dc08 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
1dc09 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 70  rnalSchema(db, p
1dc0a 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 0a  Op->p1);.    }..
1dc0b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72      sqlite3Expir
1dc0c 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65  ePreparedStateme
1dc0d 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 72 63 20  nts(db);.    rc 
1dc0e 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b  = SQLITE_SCHEMA;
1dc0f 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1dc10 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e  ./* Opcode: Open
1dc11 52 65 61 64 20 50 31 20 50 32 20 50 33 20 50 34  Read P1 P2 P3 P4
1dc12 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61   P5.**.** Open a
1dc13 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f   read-only curso
1dc14 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61  r for the databa
1dc15 73 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72  se table whose r
1dc16 6f 6f 74 20 70 61 67 65 20 69 73 0a 2a 2a 20 50  oot page is.** P
1dc17 32 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20  2 in a database 
1dc18 66 69 6c 65 2e 20 20 54 68 65 20 64 61 74 61 62  file.  The datab
1dc19 61 73 65 20 66 69 6c 65 20 69 73 20 64 65 74 65  ase file is dete
1dc1a 72 6d 69 6e 65 64 20 62 79 20 50 33 2e 20 0a 2a  rmined by P3. .*
1dc1b 2a 20 50 33 3d 3d 30 20 6d 65 61 6e 73 20 74 68  * P3==0 means th
1dc1c 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c  e main database,
1dc1d 20 50 33 3d 3d 31 20 6d 65 61 6e 73 20 74 68 65   P3==1 means the
1dc1e 20 64 61 74 61 62 61 73 65 20 75 73 65 64 20 66   database used f
1dc1f 6f 72 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79  or .** temporary
1dc20 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 50 33 3e   tables, and P3>
1dc21 31 20 6d 65 61 6e 73 20 75 73 65 64 20 74 68 65  1 means used the
1dc22 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61   corresponding a
1dc23 74 74 61 63 68 65 64 0a 2a 2a 20 64 61 74 61 62  ttached.** datab
1dc24 61 73 65 2e 20 20 47 69 76 65 20 74 68 65 20 6e  ase.  Give the n
1dc25 65 77 20 63 75 72 73 6f 72 20 61 6e 20 69 64 65  ew cursor an ide
1dc26 6e 74 69 66 69 65 72 20 6f 66 20 50 31 2e 20 20  ntifier of P1.  
1dc27 54 68 65 20 50 31 0a 2a 2a 20 76 61 6c 75 65 73  The P1.** values
1dc28 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 63 6f 6e   need not be con
1dc29 74 69 67 75 6f 75 73 20 62 75 74 20 61 6c 6c 20  tiguous but all 
1dc2a 50 31 20 76 61 6c 75 65 73 20 73 68 6f 75 6c 64  P1 values should
1dc2b 20 62 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65   be small intege
1dc2c 72 73 2e 0a 2a 2a 20 49 74 20 69 73 20 61 6e 20  rs..** It is an 
1dc2d 65 72 72 6f 72 20 66 6f 72 20 50 31 20 74 6f 20  error for P1 to 
1dc2e 62 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a  be negative..**.
1dc2f 2a 2a 20 49 66 20 50 35 21 3d 30 20 74 68 65 6e  ** If P5!=0 then
1dc30 20 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74   use the content
1dc31 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20   of register P2 
1dc32 61 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  as the root page
1dc33 2c 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 76 61 6c  , not.** the val
1dc34 75 65 20 6f 66 20 50 32 20 69 74 73 65 6c 66 2e  ue of P2 itself.
1dc35 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 77 69 6c  .**.** There wil
1dc36 6c 20 62 65 20 61 20 72 65 61 64 20 6c 6f 63 6b  l be a read lock
1dc37 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
1dc38 20 77 68 65 6e 65 76 65 72 20 74 68 65 72 65 20   whenever there 
1dc39 69 73 20 61 6e 0a 2a 2a 20 6f 70 65 6e 20 63 75  is an.** open cu
1dc3a 72 73 6f 72 2e 20 20 49 66 20 74 68 65 20 64 61  rsor.  If the da
1dc3b 74 61 62 61 73 65 20 77 61 73 20 75 6e 6c 6f 63  tabase was unloc
1dc3c 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 69  ked prior to thi
1dc3d 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a  s instruction.**
1dc3e 20 74 68 65 6e 20 61 20 72 65 61 64 20 6c 6f 63   then a read loc
1dc3f 6b 20 69 73 20 61 63 71 75 69 72 65 64 20 61 73  k is acquired as
1dc40 20 70 61 72 74 20 6f 66 20 74 68 69 73 20 69 6e   part of this in
1dc41 73 74 72 75 63 74 69 6f 6e 2e 20 20 41 20 72 65  struction.  A re
1dc42 61 64 0a 2a 2a 20 6c 6f 63 6b 20 61 6c 6c 6f 77  ad.** lock allow
1dc43 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  s other processe
1dc44 73 20 74 6f 20 72 65 61 64 20 74 68 65 20 64 61  s to read the da
1dc45 74 61 62 61 73 65 20 62 75 74 20 70 72 6f 68 69  tabase but prohi
1dc46 62 69 74 73 0a 2a 2a 20 61 6e 79 20 6f 74 68 65  bits.** any othe
1dc47 72 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 6d  r process from m
1dc48 6f 64 69 66 79 69 6e 67 20 74 68 65 20 64 61 74  odifying the dat
1dc49 61 62 61 73 65 2e 20 20 54 68 65 20 72 65 61 64  abase.  The read
1dc4a 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 72 65 6c 65   lock is.** rele
1dc4b 61 73 65 64 20 77 68 65 6e 20 61 6c 6c 20 63 75  ased when all cu
1dc4c 72 73 6f 72 73 20 61 72 65 20 63 6c 6f 73 65 64  rsors are closed
1dc4d 2e 20 20 49 66 20 74 68 69 73 20 69 6e 73 74 72  .  If this instr
1dc4e 75 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 0a  uction attempts.
1dc4f 2a 2a 20 74 6f 20 67 65 74 20 61 20 72 65 61 64  ** to get a read
1dc50 20 6c 6f 63 6b 20 62 75 74 20 66 61 69 6c 73 2c   lock but fails,
1dc51 20 74 68 65 20 73 63 72 69 70 74 20 74 65 72 6d   the script term
1dc52 69 6e 61 74 65 73 20 77 69 74 68 20 61 6e 0a 2a  inates with an.*
1dc53 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72  * SQLITE_BUSY er
1dc54 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ror code..**.** 
1dc55 54 68 65 20 50 34 20 76 61 6c 75 65 20 6d 61 79  The P4 value may
1dc56 20 62 65 20 65 69 74 68 65 72 20 61 6e 20 69 6e   be either an in
1dc57 74 65 67 65 72 20 28 50 34 5f 49 4e 54 33 32 29  teger (P4_INT32)
1dc58 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   or a pointer to
1dc59 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f 20 73 74  .** a KeyInfo st
1dc5a 72 75 63 74 75 72 65 20 28 50 34 5f 4b 45 59 49  ructure (P4_KEYI
1dc5b 4e 46 4f 29 2e 20 49 66 20 69 74 20 69 73 20 61  NFO). If it is a
1dc5c 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65   pointer to a Ke
1dc5d 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74  yInfo .** struct
1dc5e 75 72 65 2c 20 74 68 65 6e 20 73 61 69 64 20 73  ure, then said s
1dc5f 74 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73  tructure defines
1dc60 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64   the content and
1dc61 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73   collating .** s
1dc62 65 71 75 65 6e 63 65 20 6f 66 20 74 68 65 20 69  equence of the i
1dc63 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65 6e 65  ndex being opene
1dc64 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  d. Otherwise, if
1dc65 20 50 34 20 69 73 20 61 6e 20 69 6e 74 65 67 65   P4 is an intege
1dc66 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20  r .** value, it 
1dc67 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
1dc68 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1dc69 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a  in the table..**
1dc6a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65  .** See also Ope
1dc6b 6e 57 72 69 74 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70  nWrite..*/./* Op
1dc6c 63 6f 64 65 3a 20 4f 70 65 6e 57 72 69 74 65 20  code: OpenWrite 
1dc6d 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1dc6e 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64  *.** Open a read
1dc6f 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 6e 61  /write cursor na
1dc70 6d 65 64 20 50 31 20 6f 6e 20 74 68 65 20 74 61  med P1 on the ta
1dc71 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f  ble or index who
1dc72 73 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20  se root.** page 
1dc73 69 73 20 50 32 2e 20 20 4f 72 20 69 66 20 50 35  is P2.  Or if P5
1dc74 21 3d 30 20 75 73 65 20 74 68 65 20 63 6f 6e 74  !=0 use the cont
1dc75 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20  ent of register 
1dc76 50 32 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a  P2 to find the.*
1dc77 2a 20 72 6f 6f 74 20 70 61 67 65 2e 0a 2a 2a 0a  * root page..**.
1dc78 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20  ** The P4 value 
1dc79 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 61 6e  may be either an
1dc7a 20 69 6e 74 65 67 65 72 20 28 50 34 5f 49 4e 54   integer (P4_INT
1dc7b 33 32 29 20 6f 72 20 61 20 70 6f 69 6e 74 65 72  32) or a pointer
1dc7c 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f   to.** a KeyInfo
1dc7d 20 73 74 72 75 63 74 75 72 65 20 28 50 34 5f 4b   structure (P4_K
1dc7e 45 59 49 4e 46 4f 29 2e 20 49 66 20 69 74 20 69  EYINFO). If it i
1dc7f 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
1dc80 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72   KeyInfo .** str
1dc81 75 63 74 75 72 65 2c 20 74 68 65 6e 20 73 61 69  ucture, then sai
1dc82 64 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69  d structure defi
1dc83 6e 65 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  nes the content 
1dc84 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a  and collating .*
1dc85 2a 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68  * sequence of th
1dc86 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 6f 70  e index being op
1dc87 65 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  ened. Otherwise,
1dc88 20 69 66 20 50 34 20 69 73 20 61 6e 20 69 6e 74   if P4 is an int
1dc89 65 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20  eger .** value, 
1dc8a 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  it is set to the
1dc8b 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1dc8c 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c  ns in the table,
1dc8d 20 6f 72 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 61   or to the.** la
1dc8e 72 67 65 73 74 20 69 6e 64 65 78 20 6f 66 20 61  rgest index of a
1dc8f 6e 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  ny column of the
1dc90 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 61   table that is a
1dc91 63 74 75 61 6c 6c 79 20 75 73 65 64 2e 0a 2a 2a  ctually used..**
1dc92 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63  .** This instruc
1dc93 74 69 6f 6e 20 77 6f 72 6b 73 20 6a 75 73 74 20  tion works just 
1dc94 6c 69 6b 65 20 4f 70 65 6e 52 65 61 64 20 65 78  like OpenRead ex
1dc95 63 65 70 74 20 74 68 61 74 20 69 74 20 6f 70 65  cept that it ope
1dc96 6e 73 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  ns the cursor.**
1dc97 20 69 6e 20 72 65 61 64 2f 77 72 69 74 65 20 6d   in read/write m
1dc98 6f 64 65 2e 20 20 46 6f 72 20 61 20 67 69 76 65  ode.  For a give
1dc99 6e 20 74 61 62 6c 65 2c 20 74 68 65 72 65 20 63  n table, there c
1dc9a 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72  an be one or mor
1dc9b 65 20 72 65 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63  e read-only.** c
1dc9c 75 72 73 6f 72 73 20 6f 72 20 61 20 73 69 6e 67  ursors or a sing
1dc9d 6c 65 20 72 65 61 64 2f 77 72 69 74 65 20 63 75  le read/write cu
1dc9e 72 73 6f 72 20 62 75 74 20 6e 6f 74 20 62 6f 74  rsor but not bot
1dc9f 68 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  h..**.** See als
1dca0 6f 20 4f 70 65 6e 52 65 61 64 2e 0a 2a 2f 0a 63  o OpenRead..*/.c
1dca1 61 73 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3a  ase OP_OpenRead:
1dca2 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 57 72 69  .case OP_OpenWri
1dca3 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  te: {.#if 0  /* 
1dca4 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1dca5 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 77 20  moved into u.aw 
1dca6 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b  */.  int nField;
1dca7 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
1dca8 49 6e 66 6f 3b 0a 20 20 69 6e 74 20 70 32 3b 0a  Info;.  int p2;.
1dca9 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74    int iDb;.  int
1dcaa 20 77 72 46 6c 61 67 3b 0a 20 20 42 74 72 65 65   wrFlag;.  Btree
1dcab 20 2a 70 58 3b 0a 20 20 56 64 62 65 43 75 72 73   *pX;.  VdbeCurs
1dcac 6f 72 20 2a 70 43 75 72 3b 0a 20 20 44 62 20 2a  or *pCur;.  Db *
1dcad 70 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  pDb;.#endif /* l
1dcae 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1dcaf 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 77 20 2a  oved into u.aw *
1dcb0 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 69  /..  if( p->expi
1dcb1 72 65 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  red ){.    rc = 
1dcb2 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20  SQLITE_ABORT;.  
1dcb3 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20    break;.  }..  
1dcb4 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d 20 30 3b  u.aw.nField = 0;
1dcb5 0a 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f  .  u.aw.pKeyInfo
1dcb6 20 3d 20 30 3b 0a 20 20 75 2e 61 77 2e 70 32 20   = 0;.  u.aw.p2 
1dcb7 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61  = pOp->p2;.  u.a
1dcb8 77 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b  w.iDb = pOp->p3;
1dcb9 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 77 2e  .  assert( u.aw.
1dcba 69 44 62 3e 3d 30 20 26 26 20 75 2e 61 77 2e 69  iDb>=0 && u.aw.i
1dcbb 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
1dcbc 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
1dcbd 65 4d 61 73 6b 20 26 20 28 31 3c 3c 75 2e 61 77  eMask & (1<<u.aw
1dcbe 2e 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 75  .iDb))!=0 );.  u
1dcbf 2e 61 77 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61  .aw.pDb = &db->a
1dcc0 44 62 5b 75 2e 61 77 2e 69 44 62 5d 3b 0a 20 20  Db[u.aw.iDb];.  
1dcc1 75 2e 61 77 2e 70 58 20 3d 20 75 2e 61 77 2e 70  u.aw.pX = u.aw.p
1dcc2 44 62 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72  Db->pBt;.  asser
1dcc3 74 28 20 75 2e 61 77 2e 70 58 21 3d 30 20 29 3b  t( u.aw.pX!=0 );
1dcc4 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  .  if( pOp->opco
1dcc5 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65  de==OP_OpenWrite
1dcc6 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 77 72 46   ){.    u.aw.wrF
1dcc7 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 69 66 28  lag = 1;.    if(
1dcc8 20 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63 68 65   u.aw.pDb->pSche
1dcc9 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20  ma->file_format 
1dcca 3c 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c  < p->minWriteFil
1dccb 65 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20  eFormat ){.     
1dccc 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65   p->minWriteFile
1dccd 46 6f 72 6d 61 74 20 3d 20 75 2e 61 77 2e 70 44  Format = u.aw.pD
1dcce 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  b->pSchema->file
1dccf 5f 66 6f 72 6d 61 74 3b 0a 20 20 20 20 7d 0a 20  _format;.    }. 
1dcd0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 77   }else{.    u.aw
1dcd1 2e 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20 20 7d  .wrFlag = 0;.  }
1dcd2 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29  .  if( pOp->p5 )
1dcd3 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  {.    assert( u.
1dcd4 61 77 2e 70 32 3e 30 20 29 3b 0a 20 20 20 20 61  aw.p2>0 );.    a
1dcd5 73 73 65 72 74 28 20 75 2e 61 77 2e 70 32 3c 3d  ssert( u.aw.p2<=
1dcd6 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 70  p->nMem );.    p
1dcd7 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75  In2 = &p->aMem[u
1dcd8 2e 61 77 2e 70 32 5d 3b 0a 20 20 20 20 73 71 6c  .aw.p2];.    sql
1dcd9 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
1dcda 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 20  erify(pIn2);.   
1dcdb 20 75 2e 61 77 2e 70 32 20 3d 20 28 69 6e 74 29   u.aw.p2 = (int)
1dcdc 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 2f  pIn2->u.i;.    /
1dcdd 2a 20 54 68 65 20 75 2e 61 77 2e 70 32 20 76 61  * The u.aw.p2 va
1dcde 6c 75 65 20 61 6c 77 61 79 73 20 63 6f 6d 65 73  lue always comes
1dcdf 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 4f 50   from a prior OP
1dce0 5f 43 72 65 61 74 65 54 61 62 6c 65 20 6f 70 63  _CreateTable opc
1dce1 6f 64 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74  ode and.    ** t
1dce2 68 61 74 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20  hat opcode will 
1dce3 61 6c 77 61 79 73 20 73 65 74 20 74 68 65 20 75  always set the u
1dce4 2e 61 77 2e 70 32 20 76 61 6c 75 65 20 74 6f 20  .aw.p2 value to 
1dce5 32 20 6f 72 20 6d 6f 72 65 20 6f 72 20 65 6c 73  2 or more or els
1dce6 65 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2a 20 49  e fail..    ** I
1dce7 66 20 74 68 65 72 65 20 77 65 72 65 20 61 20 66  f there were a f
1dce8 61 69 6c 75 72 65 2c 20 74 68 65 20 70 72 65 70  ailure, the prep
1dce9 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 77  ared statement w
1dcea 6f 75 6c 64 20 68 61 76 65 20 68 61 6c 74 65 64  ould have halted
1dceb 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 72  .    ** before r
1dcec 65 61 63 68 69 6e 67 20 74 68 69 73 20 69 6e 73  eaching this ins
1dced 74 72 75 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20  truction. */.   
1dcee 20 69 66 28 20 4e 45 56 45 52 28 75 2e 61 77 2e   if( NEVER(u.aw.
1dcef 70 32 3c 32 29 20 29 20 7b 0a 20 20 20 20 20 20  p2<2) ) {.      
1dcf0 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  rc = SQLITE_CORR
1dcf1 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
1dcf2 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1dcf3 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  o_error;.    }. 
1dcf4 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34   }.  if( pOp->p4
1dcf5 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f  type==P4_KEYINFO
1dcf6 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 70 4b 65   ){.    u.aw.pKe
1dcf7 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e  yInfo = pOp->p4.
1dcf8 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75 2e  pKeyInfo;.    u.
1dcf9 61 77 2e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63  aw.pKeyInfo->enc
1dcfa 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20   = ENC(p->db);. 
1dcfb 20 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d     u.aw.nField =
1dcfc 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2d 3e   u.aw.pKeyInfo->
1dcfd 6e 46 69 65 6c 64 2b 31 3b 0a 20 20 7d 65 6c 73  nField+1;.  }els
1dcfe 65 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70  e if( pOp->p4typ
1dcff 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20  e==P4_INT32 ){. 
1dd00 20 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d     u.aw.nField =
1dd01 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 7d 0a   pOp->p4.i;.  }.
1dd02 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1dd03 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 77 2e 70  1>=0 );.  u.aw.p
1dd04 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75  Cur = allocateCu
1dd05 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c  rsor(p, pOp->p1,
1dd06 20 75 2e 61 77 2e 6e 46 69 65 6c 64 2c 20 75 2e   u.aw.nField, u.
1dd07 61 77 2e 69 44 62 2c 20 31 29 3b 0a 20 20 69 66  aw.iDb, 1);.  if
1dd08 28 20 75 2e 61 77 2e 70 43 75 72 3d 3d 30 20 29  ( u.aw.pCur==0 )
1dd09 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1dd0a 75 2e 61 77 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52  u.aw.pCur->nullR
1dd0b 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 73  ow = 1;.  rc = s
1dd0c 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
1dd0d 72 28 75 2e 61 77 2e 70 58 2c 20 75 2e 61 77 2e  r(u.aw.pX, u.aw.
1dd0e 70 32 2c 20 75 2e 61 77 2e 77 72 46 6c 61 67 2c  p2, u.aw.wrFlag,
1dd0f 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2c 20   u.aw.pKeyInfo, 
1dd10 75 2e 61 77 2e 70 43 75 72 2d 3e 70 43 75 72 73  u.aw.pCur->pCurs
1dd11 6f 72 29 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72  or);.  u.aw.pCur
1dd12 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 61  ->pKeyInfo = u.a
1dd13 77 2e 70 4b 65 79 49 6e 66 6f 3b 0a 0a 20 20 2f  w.pKeyInfo;..  /
1dd14 2a 20 53 69 6e 63 65 20 69 74 20 70 65 72 66 6f  * Since it perfo
1dd15 72 6d 73 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 6c  rms no memory al
1dd16 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 49 4f 2c 20  location or IO, 
1dd17 74 68 65 20 6f 6e 6c 79 20 76 61 6c 75 65 73 20  the only values 
1dd18 74 68 61 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65  that.  ** sqlite
1dd19 33 42 74 72 65 65 43 75 72 73 6f 72 28 29 20 6d  3BtreeCursor() m
1dd1a 61 79 20 72 65 74 75 72 6e 20 61 72 65 20 53 51  ay return are SQ
1dd1b 4c 49 54 45 5f 45 4d 50 54 59 20 61 6e 64 20 53  LITE_EMPTY and S
1dd1c 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2a 20 53  QLITE_OK..  ** S
1dd1d 51 4c 49 54 45 5f 45 4d 50 54 59 20 69 73 20 6f  QLITE_EMPTY is o
1dd1e 6e 6c 79 20 72 65 74 75 72 6e 65 64 20 77 68 65  nly returned whe
1dd1f 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  n attempting to 
1dd20 6f 70 65 6e 20 74 68 65 20 74 61 62 6c 65 0a 20  open the table. 
1dd21 20 2a 2a 20 72 6f 6f 74 65 64 20 61 74 20 70 61   ** rooted at pa
1dd22 67 65 20 31 20 6f 66 20 61 20 7a 65 72 6f 2d 62  ge 1 of a zero-b
1dd23 79 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 2a  yte database.  *
1dd24 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  /.  assert( rc==
1dd25 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 7c 7c 20  SQLITE_EMPTY || 
1dd26 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
1dd27 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1dd28 45 5f 45 4d 50 54 59 20 29 7b 0a 20 20 20 20 75  E_EMPTY ){.    u
1dd29 2e 61 77 2e 70 43 75 72 2d 3e 70 43 75 72 73 6f  .aw.pCur->pCurso
1dd2a 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  r = 0;.    rc = 
1dd2b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
1dd2c 20 20 2f 2a 20 53 65 74 20 74 68 65 20 56 64 62    /* Set the Vdb
1dd2d 65 43 75 72 73 6f 72 2e 69 73 54 61 62 6c 65 20  eCursor.isTable 
1dd2e 61 6e 64 20 69 73 49 6e 64 65 78 20 76 61 72 69  and isIndex vari
1dd2f 61 62 6c 65 73 2e 20 50 72 65 76 69 6f 75 73 20  ables. Previous 
1dd30 76 65 72 73 69 6f 6e 73 20 6f 66 0a 20 20 2a 2a  versions of.  **
1dd31 20 53 51 4c 69 74 65 20 75 73 65 64 20 74 6f 20   SQLite used to 
1dd32 63 68 65 63 6b 20 69 66 20 74 68 65 20 72 6f 6f  check if the roo
1dd33 74 2d 70 61 67 65 20 66 6c 61 67 73 20 77 65 72  t-page flags wer
1dd34 65 20 73 61 6e 65 20 61 74 20 74 68 69 73 20 70  e sane at this p
1dd35 6f 69 6e 74 0a 20 20 2a 2a 20 61 6e 64 20 72 65  oint.  ** and re
1dd36 70 6f 72 74 20 64 61 74 61 62 61 73 65 20 63 6f  port database co
1dd37 72 72 75 70 74 69 6f 6e 20 69 66 20 74 68 65 79  rruption if they
1dd38 20 77 65 72 65 20 6e 6f 74 2c 20 62 75 74 20 74   were not, but t
1dd39 68 69 73 20 63 68 65 63 6b 20 68 61 73 0a 20 20  his check has.  
1dd3a 2a 2a 20 73 69 6e 63 65 20 6d 6f 76 65 64 20 69  ** since moved i
1dd3b 6e 74 6f 20 74 68 65 20 62 74 72 65 65 20 6c 61  nto the btree la
1dd3c 79 65 72 2e 20 20 2a 2f 0a 20 20 75 2e 61 77 2e  yer.  */.  u.aw.
1dd3d 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20  pCur->isTable = 
1dd3e 70 4f 70 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f  pOp->p4type!=P4_
1dd3f 4b 45 59 49 4e 46 4f 3b 0a 20 20 75 2e 61 77 2e  KEYINFO;.  u.aw.
1dd40 70 43 75 72 2d 3e 69 73 49 6e 64 65 78 20 3d 20  pCur->isIndex = 
1dd41 21 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54 61  !u.aw.pCur->isTa
1dd42 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ble;.  break;.}.
1dd43 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e  ./* Opcode: Open
1dd44 45 70 68 65 6d 65 72 61 6c 20 50 31 20 50 32 20  Ephemeral P1 P2 
1dd45 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65  * P4 *.**.** Ope
1dd46 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 50  n a new cursor P
1dd47 31 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74  1 to a transient
1dd48 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63   table..** The c
1dd49 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79 73 20  ursor is always 
1dd4a 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72 69 74  opened read/writ
1dd4b 65 20 65 76 65 6e 20 69 66 20 0a 2a 2a 20 74 68  e even if .** th
1dd4c 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1dd4d 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 54  is read-only.  T
1dd4e 68 65 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 20  he transient or 
1dd4f 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65  virtual.** table
1dd50 20 69 73 20 64 65 6c 65 74 65 64 20 61 75 74 6f   is deleted auto
1dd51 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74  matically when t
1dd52 68 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f  he cursor is clo
1dd53 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73  sed..**.** P2 is
1dd54 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
1dd55 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 76 69  olumns in the vi
1dd56 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 20  rtual table..** 
1dd57 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74  The cursor point
1dd58 73 20 74 6f 20 61 20 42 54 72 65 65 20 74 61 62  s to a BTree tab
1dd59 6c 65 20 69 66 20 50 34 3d 3d 30 20 61 6e 64 20  le if P4==0 and 
1dd5a 74 6f 20 61 20 42 54 72 65 65 20 69 6e 64 65 78  to a BTree index
1dd5b 0a 2a 2a 20 69 66 20 50 34 20 69 73 20 6e 6f 74  .** if P4 is not
1dd5c 20 30 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f   0.  If P4 is no
1dd5d 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74  t NULL, it point
1dd5e 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73  s to a KeyInfo s
1dd5f 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74  tructure.** that
1dd60 20 64 65 66 69 6e 65 73 20 74 68 65 20 66 6f 72   defines the for
1dd61 6d 61 74 20 6f 66 20 6b 65 79 73 20 69 6e 20 74  mat of keys in t
1dd62 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  he index..**.** 
1dd63 54 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20  This opcode was 
1dd64 6f 6e 63 65 20 63 61 6c 6c 65 64 20 4f 70 65 6e  once called Open
1dd65 54 65 6d 70 2e 20 20 42 75 74 20 74 68 61 74 20  Temp.  But that 
1dd66 63 72 65 61 74 65 64 0a 2a 2a 20 63 6f 6e 66 75  created.** confu
1dd67 73 69 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65  sion because the
1dd68 20 74 65 72 6d 20 22 74 65 6d 70 20 74 61 62 6c   term "temp tabl
1dd69 65 22 2c 20 6d 69 67 68 74 20 72 65 66 65 72 20  e", might refer 
1dd6a 65 69 74 68 65 72 0a 2a 2a 20 74 6f 20 61 20 54  either.** to a T
1dd6b 45 4d 50 20 74 61 62 6c 65 20 61 74 20 74 68 65  EMP table at the
1dd6c 20 53 51 4c 20 6c 65 76 65 6c 2c 20 6f 72 20 74   SQL level, or t
1dd6d 6f 20 61 20 74 61 62 6c 65 20 6f 70 65 6e 65 64  o a table opened
1dd6e 20 62 79 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f   by.** this opco
1dd6f 64 65 2e 20 20 54 68 65 6e 20 74 68 69 73 20 6f  de.  Then this o
1dd70 70 63 6f 64 65 20 77 61 73 20 63 61 6c 6c 20 4f  pcode was call O
1dd71 70 65 6e 56 69 72 74 75 61 6c 2e 20 20 42 75 74  penVirtual.  But
1dd72 0a 2a 2a 20 74 68 61 74 20 63 72 65 61 74 65 64  .** that created
1dd73 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20   confusion with 
1dd74 74 68 65 20 77 68 6f 6c 65 20 76 69 72 74 75 61  the whole virtua
1dd75 6c 2d 74 61 62 6c 65 20 69 64 65 61 2e 0a 2a 2f  l-table idea..*/
1dd76 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 45 70 68  .case OP_OpenEph
1dd77 65 6d 65 72 61 6c 3a 20 7b 0a 23 69 66 20 30 20  emeral: {.#if 0 
1dd78 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1dd79 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1dd7a 2e 61 78 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .ax */.  VdbeCur
1dd7b 73 6f 72 20 2a 70 43 78 3b 0a 23 65 6e 64 69 66  sor *pCx;.#endif
1dd7c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1dd7d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1dd7e 2e 61 78 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  .ax */.  static 
1dd7f 63 6f 6e 73 74 20 69 6e 74 20 6f 70 65 6e 46 6c  const int openFl
1dd80 61 67 73 20 3d 0a 20 20 20 20 20 20 53 51 4c 49  ags =.      SQLI
1dd81 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
1dd82 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45  E |.      SQLITE
1dd83 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20  _OPEN_CREATE |. 
1dd84 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
1dd85 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20 20 20  _EXCLUSIVE |.   
1dd86 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44     SQLITE_OPEN_D
1dd87 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20  ELETEONCLOSE |. 
1dd88 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
1dd89 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 3b 0a 0a  _TRANSIENT_DB;..
1dd8a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1dd8b 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 78 2e 70  1>=0 );.  u.ax.p
1dd8c 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72  Cx = allocateCur
1dd8d 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20  sor(p, pOp->p1, 
1dd8e 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c 20 31 29 3b  pOp->p2, -1, 1);
1dd8f 0a 20 20 69 66 28 20 75 2e 61 78 2e 70 43 78 3d  .  if( u.ax.pCx=
1dd90 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  =0 ) goto no_mem
1dd91 3b 0a 20 20 75 2e 61 78 2e 70 43 78 2d 3e 6e 75  ;.  u.ax.pCx->nu
1dd92 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20  llRow = 1;.  rc 
1dd93 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61  = sqlite3BtreeFa
1dd94 63 74 6f 72 79 28 64 62 2c 20 30 2c 20 31 2c 20  ctory(db, 0, 1, 
1dd95 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54  SQLITE_DEFAULT_T
1dd96 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20  EMP_CACHE_SIZE, 
1dd97 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20  openFlags,.     
1dd98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dd99 20 20 20 20 20 20 26 75 2e 61 78 2e 70 43 78 2d        &u.ax.pCx-
1dd9a 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63 3d  >pBt);.  if( rc=
1dd9b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1dd9c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1dd9d 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e  reeBeginTrans(u.
1dd9e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b  ax.pCx->pBt, 1);
1dd9f 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
1dda0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1dda1 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e  /* If a transien
1dda2 74 20 69 6e 64 65 78 20 69 73 20 72 65 71 75 69  t index is requi
1dda3 72 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 62  red, create it b
1dda4 79 20 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a  y calling.    **
1dda5 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65   sqlite3BtreeCre
1dda6 61 74 65 54 61 62 6c 65 28 29 20 77 69 74 68 20  ateTable() with 
1dda7 74 68 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41  the BTREE_ZERODA
1dda8 54 41 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20  TA flag before. 
1dda9 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74     ** opening it
1ddaa 2e 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74  . If a transient
1ddab 20 74 61 62 6c 65 20 69 73 20 72 65 71 75 69 72   table is requir
1ddac 65 64 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65  ed, just use the
1ddad 0a 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69  .    ** automati
1ddae 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61  cally created ta
1ddaf 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61  ble with root-pa
1ddb0 67 65 20 31 20 28 61 6e 20 49 4e 54 4b 45 59 20  ge 1 (an INTKEY 
1ddb1 74 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20  table)..    */. 
1ddb2 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70     if( pOp->p4.p
1ddb3 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20  KeyInfo ){.     
1ddb4 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20   int pgno;.     
1ddb5 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1ddb6 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f  type==P4_KEYINFO
1ddb7 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   );.      rc = s
1ddb8 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
1ddb9 65 54 61 62 6c 65 28 75 2e 61 78 2e 70 43 78 2d  eTable(u.ax.pCx-
1ddba 3e 70 42 74 2c 20 26 70 67 6e 6f 2c 20 42 54 52  >pBt, &pgno, BTR
1ddbb 45 45 5f 5a 45 52 4f 44 41 54 41 29 3b 0a 20 20  EE_ZERODATA);.  
1ddbc 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1ddbd 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1ddbe 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3d 3d 4d   assert( pgno==M
1ddbf 41 53 54 45 52 5f 52 4f 4f 54 2b 31 20 29 3b 0a  ASTER_ROOT+1 );.
1ddc0 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1ddc1 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28  ite3BtreeCursor(
1ddc2 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 70  u.ax.pCx->pBt, p
1ddc3 67 6e 6f 2c 20 31 2c 0a 20 20 20 20 20 20 20 20  gno, 1,.        
1ddc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ddc5 20 20 20 20 20 20 20 20 28 4b 65 79 49 6e 66 6f          (KeyInfo
1ddc6 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e 61  *)pOp->p4.z, u.a
1ddc7 78 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b  x.pCx->pCursor);
1ddc8 0a 20 20 20 20 20 20 20 20 75 2e 61 78 2e 70 43  .        u.ax.pC
1ddc9 78 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f  x->pKeyInfo = pO
1ddca 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a  p->p4.pKeyInfo;.
1ddcb 20 20 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78          u.ax.pCx
1ddcc 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20  ->pKeyInfo->enc 
1ddcd 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20  = ENC(p->db);.  
1ddce 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 78      }.      u.ax
1ddcf 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20  .pCx->isTable = 
1ddd0 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
1ddd1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ddd2 42 74 72 65 65 43 75 72 73 6f 72 28 75 2e 61 78  BtreeCursor(u.ax
1ddd3 2e 70 43 78 2d 3e 70 42 74 2c 20 4d 41 53 54 45  .pCx->pBt, MASTE
1ddd4 52 5f 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 75 2e  R_ROOT, 1, 0, u.
1ddd5 61 78 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 29  ax.pCx->pCursor)
1ddd6 3b 0a 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78  ;.      u.ax.pCx
1ddd7 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20  ->isTable = 1;. 
1ddd8 20 20 20 7d 0a 20 20 7d 0a 20 20 75 2e 61 78 2e     }.  }.  u.ax.
1ddd9 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21  pCx->isIndex = !
1ddda 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c  u.ax.pCx->isTabl
1dddb 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  e;.  break;.}../
1dddc 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 50 73  * Opcode: OpenPs
1dddd 65 75 64 6f 20 50 31 20 50 32 20 50 33 20 2a 20  eudo P1 P2 P3 * 
1ddde 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e  *.**.** Open a n
1dddf 65 77 20 63 75 72 73 6f 72 20 74 68 61 74 20 70  ew cursor that p
1dde0 6f 69 6e 74 73 20 74 6f 20 61 20 66 61 6b 65 20  oints to a fake 
1dde1 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61  table that conta
1dde2 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  ins a single.** 
1dde3 72 6f 77 20 6f 66 20 64 61 74 61 2e 20 20 54 68  row of data.  Th
1dde4 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 61  e content of tha
1dde5 74 20 6f 6e 65 20 72 6f 77 20 69 6e 20 74 68 65  t one row in the
1dde6 20 63 6f 6e 74 65 6e 74 20 6f 66 20 6d 65 6d 6f   content of memo
1dde7 72 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50  ry.** register P
1dde8 32 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  2.  In other wor
1dde9 64 73 2c 20 63 75 72 73 6f 72 20 50 31 20 62 65  ds, cursor P1 be
1ddea 63 6f 6d 65 73 20 61 6e 20 61 6c 69 61 73 20 66  comes an alias f
1ddeb 6f 72 20 74 68 65 20 0a 2a 2a 20 4d 45 4d 5f 42  or the .** MEM_B
1ddec 6c 6f 62 20 63 6f 6e 74 65 6e 74 20 63 6f 6e 74  lob content cont
1dded 61 69 6e 65 64 20 69 6e 20 72 65 67 69 73 74 65  ained in registe
1ddee 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 73  r P2..**.** A ps
1ddef 65 75 64 6f 2d 74 61 62 6c 65 20 63 72 65 61 74  eudo-table creat
1ddf0 65 64 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64  ed by this opcod
1ddf1 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c  e is used to hol
1ddf2 64 20 74 68 65 20 61 20 73 69 6e 67 6c 65 0a 2a  d the a single.*
1ddf3 2a 20 72 6f 77 20 6f 75 74 70 75 74 20 66 72 6f  * row output fro
1ddf4 6d 20 74 68 65 20 73 6f 72 74 65 72 20 73 6f 20  m the sorter so 
1ddf5 74 68 61 74 20 74 68 65 20 72 6f 77 20 63 61 6e  that the row can
1ddf6 20 62 65 20 64 65 63 6f 6d 70 6f 73 65 64 20 69   be decomposed i
1ddf7 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61  nto.** individua
1ddf8 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 69 6e 67 20  l columns using 
1ddf9 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70  the OP_Column op
1ddfa 63 6f 64 65 2e 20 20 54 68 65 20 4f 50 5f 43 6f  code.  The OP_Co
1ddfb 6c 75 6d 6e 20 6f 70 63 6f 64 65 0a 2a 2a 20 69  lumn opcode.** i
1ddfc 73 20 74 68 65 20 6f 6e 6c 79 20 63 75 72 73 6f  s the only curso
1ddfd 72 20 6f 70 63 6f 64 65 20 74 68 61 74 20 77 6f  r opcode that wo
1ddfe 72 6b 73 20 77 69 74 68 20 61 20 70 73 65 75 64  rks with a pseud
1ddff 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 50  o-table..**.** P
1de00 33 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  3 is the number 
1de01 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65  of fields in the
1de02 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 77 69   records that wi
1de03 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 62 79 0a  ll be stored by.
1de04 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61  ** the pseudo-ta
1de05 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ble..*/.case OP_
1de06 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a 23 69  OpenPseudo: {.#i
1de07 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1de08 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1de09 74 6f 20 75 2e 61 79 20 2a 2f 0a 20 20 56 64 62  to u.ay */.  Vdb
1de0a 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23 65  eCursor *pCx;.#e
1de0b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1de0c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1de0d 74 6f 20 75 2e 61 79 20 2a 2f 0a 0a 20 20 61 73  to u.ay */..  as
1de0e 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1de0f 20 29 3b 0a 20 20 75 2e 61 79 2e 70 43 78 20 3d   );.  u.ay.pCx =
1de10 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28   allocateCursor(
1de11 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d  p, pOp->p1, pOp-
1de12 3e 70 33 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 69  >p3, -1, 0);.  i
1de13 66 28 20 75 2e 61 79 2e 70 43 78 3d 3d 30 20 29  f( u.ay.pCx==0 )
1de14 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1de15 75 2e 61 79 2e 70 43 78 2d 3e 6e 75 6c 6c 52 6f  u.ay.pCx->nullRo
1de16 77 20 3d 20 31 3b 0a 20 20 75 2e 61 79 2e 70 43  w = 1;.  u.ay.pC
1de17 78 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65  x->pseudoTableRe
1de18 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75  g = pOp->p2;.  u
1de19 2e 61 79 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65  .ay.pCx->isTable
1de1a 20 3d 20 31 3b 0a 20 20 75 2e 61 79 2e 70 43 78   = 1;.  u.ay.pCx
1de1b 2d 3e 69 73 49 6e 64 65 78 20 3d 20 30 3b 0a 20  ->isIndex = 0;. 
1de1c 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1de1d 63 6f 64 65 3a 20 43 6c 6f 73 65 20 50 31 20 2a  code: Close P1 *
1de1e 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f   * * *.**.** Clo
1de1f 73 65 20 61 20 63 75 72 73 6f 72 20 70 72 65 76  se a cursor prev
1de20 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 61 73  iously opened as
1de21 20 50 31 2e 20 20 49 66 20 50 31 20 69 73 20 6e   P1.  If P1 is n
1de22 6f 74 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  ot.** currently 
1de23 6f 70 65 6e 2c 20 74 68 69 73 20 69 6e 73 74 72  open, this instr
1de24 75 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  uction is a no-o
1de25 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c  p..*/.case OP_Cl
1de26 6f 73 65 3a 20 7b 0a 20 20 61 73 73 65 72 74 28  ose: {.  assert(
1de27 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1de28 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
1de29 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  r );.  sqlite3Vd
1de2a 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20  beFreeCursor(p, 
1de2b 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1de2c 5d 29 3b 0a 20 20 70 2d 3e 61 70 43 73 72 5b 70  ]);.  p->apCsr[p
1de2d 4f 70 2d 3e 70 31 5d 20 3d 20 30 3b 0a 20 20 62  Op->p1] = 0;.  b
1de2e 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1de2f 64 65 3a 20 53 65 65 6b 47 65 20 50 31 20 50 32  de: SeekGe P1 P2
1de30 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49   P3 P4 *.**.** I
1de31 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65  f cursor P1 refe
1de32 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62  rs to an SQL tab
1de33 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20  le (B-Tree that 
1de34 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79  uses integer key
1de35 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20  s), .** use the 
1de36 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1de37 72 20 50 33 20 61 73 20 74 68 65 20 6b 65 79 2e  r P3 as the key.
1de38 20 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72    If cursor P1 r
1de39 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20  efers .** to an 
1de3a 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20  SQL index, then 
1de3b 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20  P3 is the first 
1de3c 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50  in an array of P
1de3d 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20  4 registers .** 
1de3e 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73  that are used as
1de3f 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64   an unpacked ind
1de40 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52  ex key. .**.** R
1de41 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72  eposition cursor
1de42 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 20   P1 so that  it 
1de43 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 6d  points to the sm
1de44 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 74 68 61  allest entry tha
1de45 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72  t .** is greater
1de46 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
1de47 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e  o the key value.
1de48 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
1de49 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 67 72 65   records .** gre
1de4a 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
1de4b 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e  al to the key an
1de4c 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f  d P2 is not zero
1de4d 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  , then jump to P
1de4e 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  2..**.** See als
1de4f 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75  o: Found, NotFou
1de50 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65  nd, Distinct, Se
1de51 65 6b 4c 74 2c 20 53 65 65 6b 47 74 2c 20 53 65  ekLt, SeekGt, Se
1de52 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ekLe.*/./* Opcod
1de53 65 3a 20 53 65 65 6b 47 74 20 50 31 20 50 32 20  e: SeekGt P1 P2 
1de54 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66  P3 P4 *.**.** If
1de55 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72   cursor P1 refer
1de56 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c  s to an SQL tabl
1de57 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75  e (B-Tree that u
1de58 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73  ses integer keys
1de59 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76  ), .** use the v
1de5a 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1de5b 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66   P3 as a key. If
1de5c 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72   cursor P1 refer
1de5d 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20  s .** to an SQL 
1de5e 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69  index, then P3 i
1de5f 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61  s the first in a
1de60 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65  n array of P4 re
1de61 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74  gisters .** that
1de62 20 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20   are used as an 
1de63 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b  unpacked index k
1de64 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73  ey. .**.** Repos
1de65 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20  ition cursor P1 
1de66 73 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e  so that  it poin
1de67 74 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65  ts to the smalle
1de68 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a  st entry that .*
1de69 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  * is greater tha
1de6a 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e  n the key value.
1de6b 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
1de6c 20 72 65 63 6f 72 64 73 20 67 72 65 61 74 65 72   records greater
1de6d 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65   than .** the ke
1de6e 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20  y and P2 is not 
1de6f 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20  zero, then jump 
1de70 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  to P2..**.** See
1de71 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f   also: Found, No
1de72 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74  tFound, Distinct
1de73 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 65  , SeekLt, SeekGe
1de74 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f  , SeekLe.*/./* O
1de75 70 63 6f 64 65 3a 20 53 65 65 6b 4c 74 20 50 31  pcode: SeekLt P1
1de76 20 50 32 20 50 33 20 50 34 20 2a 20 0a 2a 2a 0a   P2 P3 P4 * .**.
1de77 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20  ** If cursor P1 
1de78 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c  refers to an SQL
1de79 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74   table (B-Tree t
1de7a 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72  hat uses integer
1de7b 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20   keys), .** use 
1de7c 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1de7d 69 73 74 65 72 20 50 33 20 61 73 20 61 20 6b 65  ister P3 as a ke
1de7e 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 20  y. If cursor P1 
1de7f 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e  refers .** to an
1de80 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e   SQL index, then
1de81 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74   P3 is the first
1de82 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20   in an array of 
1de83 50 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a  P4 registers .**
1de84 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61   that are used a
1de85 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e  s an unpacked in
1de86 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20  dex key. .**.** 
1de87 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f  Reposition curso
1de88 72 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 74  r P1 so that  it
1de89 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c   points to the l
1de8a 61 72 67 65 73 74 20 65 6e 74 72 79 20 74 68 61  argest entry tha
1de8b 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68  t .** is less th
1de8c 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  an the key value
1de8d 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e  . If there are n
1de8e 6f 20 72 65 63 6f 72 64 73 20 6c 65 73 73 20 74  o records less t
1de8f 68 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20  han .** the key 
1de90 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65  and P2 is not ze
1de91 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  ro, then jump to
1de92 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61   P2..**.** See a
1de93 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46  lso: Found, NotF
1de94 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20  ound, Distinct, 
1de95 53 65 65 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20  SeekGt, SeekGe, 
1de96 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63  SeekLe.*/./* Opc
1de97 6f 64 65 3a 20 53 65 65 6b 4c 65 20 50 31 20 50  ode: SeekLe P1 P
1de98 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
1de99 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66  If cursor P1 ref
1de9a 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61  ers to an SQL ta
1de9b 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74  ble (B-Tree that
1de9c 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65   uses integer ke
1de9d 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65  ys), .** use the
1de9e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1de9f 65 72 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20  er P3 as a key. 
1dea0 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66  If cursor P1 ref
1dea1 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51  ers .** to an SQ
1dea2 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33  L index, then P3
1dea3 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e   is the first in
1dea4 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20   an array of P4 
1dea5 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68  registers .** th
1dea6 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20 61  at are used as a
1dea7 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78  n unpacked index
1dea8 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70   key. .**.** Rep
1dea9 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50  osition cursor P
1deaa 31 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69  1 so that it poi
1deab 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 72 67 65  nts to the large
1deac 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a  st entry that .*
1dead 2a 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f  * is less than o
1deae 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b  r equal to the k
1deaf 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65  ey value. If the
1deb0 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64  re are no record
1deb1 73 20 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20  s .** less than 
1deb2 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
1deb3 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f  key and P2 is no
1deb4 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d  t zero, then jum
1deb5 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53  p to P2..**.** S
1deb6 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20  ee also: Found, 
1deb7 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e  NotFound, Distin
1deb8 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b  ct, SeekGt, Seek
1deb9 47 65 2c 20 53 65 65 6b 4c 74 0a 2a 2f 0a 63 61  Ge, SeekLt.*/.ca
1deba 73 65 20 4f 50 5f 53 65 65 6b 4c 74 3a 20 20 20  se OP_SeekLt:   
1debb 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69        /* jump, i
1debc 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65  n3 */.case OP_Se
1debd 65 6b 4c 65 3a 20 20 20 20 20 20 20 20 20 2f 2a  ekLe:         /*
1debe 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61   jump, in3 */.ca
1debf 73 65 20 4f 50 5f 53 65 65 6b 47 65 3a 20 20 20  se OP_SeekGe:   
1dec0 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69        /* jump, i
1dec1 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65  n3 */.case OP_Se
1dec2 65 6b 47 74 3a 20 7b 20 20 20 20 20 20 20 2f 2a  ekGt: {       /*
1dec3 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69   jump, in3 */.#i
1dec4 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1dec5 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1dec6 74 6f 20 75 2e 61 7a 20 2a 2f 0a 20 20 69 6e 74  to u.az */.  int
1dec7 20 72 65 73 3b 0a 20 20 69 6e 74 20 6f 63 3b 0a   res;.  int oc;.
1dec8 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1dec9 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  ;.  UnpackedReco
1deca 72 64 20 72 3b 0a 20 20 69 6e 74 20 6e 46 69 65  rd r;.  int nFie
1decb 6c 64 3b 0a 20 20 69 36 34 20 69 4b 65 79 3b 20  ld;.  i64 iKey; 
1decc 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69       /* The rowi
1decd 64 20 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b  d we are to seek
1dece 20 74 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a   to */.#endif /*
1decf 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1ded0 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 7a   moved into u.az
1ded1 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
1ded2 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1ded3 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1ded4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1ded5 2d 3e 70 32 21 3d 30 20 29 3b 0a 20 20 75 2e 61  ->p2!=0 );.  u.a
1ded6 7a 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b  z.pC = p->apCsr[
1ded7 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1ded8 72 74 28 20 75 2e 61 7a 2e 70 43 21 3d 30 20 29  rt( u.az.pC!=0 )
1ded9 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a  ;.  assert( u.az
1deda 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65  .pC->pseudoTable
1dedb 52 65 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20  Reg==0 );.  if( 
1dedc 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.az.pC->pCursor
1dedd 21 3d 30 20 29 7b 0a 20 20 20 20 75 2e 61 7a 2e  !=0 ){.    u.az.
1dede 6f 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  oc = pOp->opcode
1dedf 3b 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 6e  ;.    u.az.pC->n
1dee0 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20  ullRow = 0;.    
1dee1 69 66 28 20 75 2e 61 7a 2e 70 43 2d 3e 69 73 54  if( u.az.pC->isT
1dee2 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a  able ){.      /*
1dee3 20 54 68 65 20 69 6e 70 75 74 20 76 61 6c 75 65   The input value
1dee4 20 69 6e 20 50 33 20 6d 69 67 68 74 20 62 65 20   in P3 might be 
1dee5 6f 66 20 61 6e 79 20 74 79 70 65 3a 20 69 6e 74  of any type: int
1dee6 65 67 65 72 2c 20 72 65 61 6c 2c 20 73 74 72 69  eger, real, stri
1dee7 6e 67 2c 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f  ng,.      ** blo
1dee8 62 2c 20 6f 72 20 4e 55 4c 4c 2e 20 20 42 75 74  b, or NULL.  But
1dee9 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20   it needs to be 
1deea 61 6e 20 69 6e 74 65 67 65 72 20 62 65 66 6f 72  an integer befor
1deeb 65 20 77 65 20 63 61 6e 20 64 6f 0a 20 20 20 20  e we can do.    
1deec 20 20 2a 2a 20 74 68 65 20 73 65 65 6b 2c 20 73    ** the seek, s
1deed 6f 20 63 6f 76 65 72 74 20 69 74 2e 20 2a 2f 0a  o covert it. */.
1deee 20 20 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72        applyNumer
1deef 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 33 29  icAffinity(pIn3)
1def0 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 69 4b 65  ;.      u.az.iKe
1def1 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  y = sqlite3VdbeI
1def2 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b 0a 20  ntValue(pIn3);. 
1def3 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f       u.az.pC->ro
1def4 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1def5 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
1def6 20 50 33 20 76 61 6c 75 65 20 63 6f 75 6c 64 20   P3 value could 
1def7 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64  not be converted
1def8 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72   into an integer
1def9 20 77 69 74 68 6f 75 74 0a 20 20 20 20 20 20 2a   without.      *
1defa 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d  * loss of inform
1defb 61 74 69 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63  ation, then spec
1defc 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 69  ial processing i
1defd 73 20 72 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f  s required... */
1defe 0a 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33  .      if( (pIn3
1deff 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1df00 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  t)==0 ){.       
1df01 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67   if( (pIn3->flag
1df02 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30  s & MEM_Real)==0
1df03 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
1df04 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65   If the P3 value
1df05 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65   cannot be conve
1df06 72 74 65 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69  rted into any ki
1df07 6e 64 20 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a  nd of a number,.
1df08 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65            ** the
1df09 6e 20 74 68 65 20 73 65 65 6b 20 69 73 20 6e 6f  n the seek is no
1df0a 74 20 70 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a  t possible, so j
1df0b 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20  ump to P2 */.   
1df0c 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d         pc = pOp-
1df0d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20  >p2 - 1;.       
1df0e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1df0f 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49    }.        /* I
1df10 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20  f we reach this 
1df11 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20  point, then the 
1df12 50 33 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65  P3 value must be
1df13 20 61 20 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20   a floating.    
1df14 20 20 20 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d      ** point num
1df15 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ber. */.        
1df16 61 73 73 65 72 74 28 20 28 70 49 6e 33 2d 3e 66  assert( (pIn3->f
1df17 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29  lags & MEM_Real)
1df18 21 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 20 20  !=0 );..        
1df19 69 66 28 20 75 2e 61 7a 2e 69 4b 65 79 3d 3d 53  if( u.az.iKey==S
1df1a 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 26 26  MALLEST_INT64 &&
1df1b 20 28 70 49 6e 33 2d 3e 72 3c 28 64 6f 75 62 6c   (pIn3->r<(doubl
1df1c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 7c 7c 20 70  e)u.az.iKey || p
1df1d 49 6e 33 2d 3e 72 3e 30 29 20 29 7b 0a 20 20 20  In3->r>0) ){.   
1df1e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33         /* The P3
1df1f 20 76 61 6c 75 65 20 69 73 20 74 6f 6f 20 6c 61   value is too la
1df20 72 67 65 20 69 6e 20 6d 61 67 6e 69 74 75 64 65  rge in magnitude
1df21 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 64   to be expressed
1df22 20 61 73 20 61 6e 0a 20 20 20 20 20 20 20 20 20   as an.         
1df23 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a   ** integer. */.
1df24 20 20 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72            u.az.r
1df25 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  es = 1;.        
1df26 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20    if( pIn3->r<0 
1df27 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
1df28 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53  f( u.az.oc==OP_S
1df29 65 65 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63  eekGt || u.az.oc
1df2a 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 0a 20  ==OP_SeekGe ){. 
1df2b 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20               rc 
1df2c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69  = sqlite3BtreeFi
1df2d 72 73 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75  rst(u.az.pC->pCu
1df2e 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29  rsor, &u.az.res)
1df2f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1df30 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1df31 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  K ) goto abort_d
1df32 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1df33 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1df34 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1df35 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a          if( u.az
1df36 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c  .oc==OP_SeekLt |
1df37 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65  | u.az.oc==OP_Se
1df38 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  ekLe ){.        
1df39 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1df3a 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e 61 7a  e3BtreeLast(u.az
1df3b 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75  .pC->pCursor, &u
1df3c 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20  .az.res);.      
1df3d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
1df3e 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
1df3f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1df40 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ror;.           
1df41 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
1df42 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61           if( u.a
1df43 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20  z.res ){.       
1df44 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1df45 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  2 - 1;.         
1df46 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65   }.          bre
1df47 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
1df48 65 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f  e if( u.az.oc==O
1df49 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a  P_SeekLt || u.az
1df4a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29  .oc==OP_SeekGe )
1df4b 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  {.          /* U
1df4c 73 65 20 74 68 65 20 63 65 69 6c 69 6e 67 28 29  se the ceiling()
1df4d 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e   function to con
1df4e 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20 2a  vert real->int *
1df4f 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
1df50 70 49 6e 33 2d 3e 72 20 3e 20 28 64 6f 75 62 6c  pIn3->r > (doubl
1df51 65 29 75 2e 61 7a 2e 69 4b 65 79 20 29 20 75 2e  e)u.az.iKey ) u.
1df52 61 7a 2e 69 4b 65 79 2b 2b 3b 0a 20 20 20 20 20  az.iKey++;.     
1df53 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1df54 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 66      /* Use the f
1df55 6c 6f 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e 20  loor() function 
1df56 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d  to convert real-
1df57 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20  >int */.        
1df58 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f    assert( u.az.o
1df59 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c 7c 20  c==OP_SeekLe || 
1df5a 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1df5b 47 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Gt );.          
1df5c 69 66 28 20 70 49 6e 33 2d 3e 72 20 3c 20 28 64  if( pIn3->r < (d
1df5d 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20  ouble)u.az.iKey 
1df5e 29 20 75 2e 61 7a 2e 69 4b 65 79 2d 2d 3b 0a 20  ) u.az.iKey--;. 
1df5f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1df60 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1df61 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e  te3BtreeMovetoUn
1df62 70 61 63 6b 65 64 28 75 2e 61 7a 2e 70 43 2d 3e  packed(u.az.pC->
1df63 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 34  pCursor, 0, (u64
1df64 29 75 2e 61 7a 2e 69 4b 65 79 2c 20 30 2c 20 26  )u.az.iKey, 0, &
1df65 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20  u.az.res);.     
1df66 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1df67 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
1df68 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1df69 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20  error;.      }. 
1df6a 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65       if( u.az.re
1df6b 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  s==0 ){.        
1df6c 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73  u.az.pC->rowidIs
1df6d 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Valid = 1;.     
1df6e 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 6c 61 73 74     u.az.pC->last
1df6f 52 6f 77 69 64 20 3d 20 75 2e 61 7a 2e 69 4b 65  Rowid = u.az.iKe
1df70 79 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  y;.      }.    }
1df71 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 7a  else{.      u.az
1df72 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70  .nField = pOp->p
1df73 34 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72  4.i;.      asser
1df74 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1df75 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20  P4_INT32 );.    
1df76 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6e    assert( u.az.n
1df77 46 69 65 6c 64 3e 30 20 29 3b 0a 20 20 20 20 20  Field>0 );.     
1df78 20 75 2e 61 7a 2e 72 2e 70 4b 65 79 49 6e 66 6f   u.az.r.pKeyInfo
1df79 20 3d 20 75 2e 61 7a 2e 70 43 2d 3e 70 4b 65 79   = u.az.pC->pKey
1df7a 49 6e 66 6f 3b 0a 20 20 20 20 20 20 75 2e 61 7a  Info;.      u.az
1df7b 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36  .r.nField = (u16
1df7c 29 75 2e 61 7a 2e 6e 46 69 65 6c 64 3b 0a 20 20  )u.az.nField;.  
1df7d 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d      if( u.az.oc=
1df7e 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e  =OP_SeekGt || u.
1df7f 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65  az.oc==OP_SeekLe
1df80 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a   ){.        u.az
1df81 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43  .r.flags = UNPAC
1df82 4b 45 44 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20  KED_INCRKEY;.   
1df83 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1df84 20 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 20 3d    u.az.r.flags =
1df85 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
1df86 20 20 75 2e 61 7a 2e 72 2e 61 4d 65 6d 20 3d 20    u.az.r.aMem = 
1df87 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
1df88 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ];.      rc = sq
1df89 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
1df8a 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e 70 43  Unpacked(u.az.pC
1df8b 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a  ->pCursor, &u.az
1df8c 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 61 7a 2e  .r, 0, 0, &u.az.
1df8d 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20  res);.      if( 
1df8e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1df8f 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62  .        goto ab
1df90 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1df91 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1df92 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73  u.az.pC->rowidIs
1df93 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d  Valid = 0;.    }
1df94 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 64 65  .    u.az.pC->de
1df95 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30  ferredMoveto = 0
1df96 3b 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 63  ;.    u.az.pC->c
1df97 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43  acheStatus = CAC
1df98 48 45 5f 53 54 41 4c 45 3b 0a 23 69 66 64 65 66  HE_STALE;.#ifdef
1df99 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20   SQLITE_TEST.   
1df9a 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f   sqlite3_search_
1df9b 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a  count++;.#endif.
1df9c 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d      if( u.az.oc=
1df9d 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e  =OP_SeekGe || u.
1df9e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74  az.oc==OP_SeekGt
1df9f 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 75 2e   ){.      if( u.
1dfa0 61 7a 2e 72 65 73 3c 30 20 7c 7c 20 28 75 2e 61  az.res<0 || (u.a
1dfa1 7a 2e 72 65 73 3d 3d 30 20 26 26 20 75 2e 61 7a  z.res==0 && u.az
1dfa2 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 29 20  .oc==OP_SeekGt) 
1dfa3 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
1dfa4 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
1dfa5 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.az.pC->pCurso
1dfa6 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20  r, &u.az.res);. 
1dfa7 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
1dfa8 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
1dfa9 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1dfaa 6f 72 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a  or;.        u.az
1dfab 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69  .pC->rowidIsVali
1dfac 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  d = 0;.      }el
1dfad 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a  se{.        u.az
1dfae 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  .res = 0;.      
1dfaf 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
1dfb0 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e     assert( u.az.
1dfb1 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c  oc==OP_SeekLt ||
1dfb2 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65   u.az.oc==OP_See
1dfb3 6b 4c 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28  kLe );.      if(
1dfb4 20 75 2e 61 7a 2e 72 65 73 3e 30 20 7c 7c 20 28   u.az.res>0 || (
1dfb5 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 26 20 75  u.az.res==0 && u
1dfb6 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c  .az.oc==OP_SeekL
1dfb7 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  t) ){.        rc
1dfb8 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
1dfb9 72 65 76 69 6f 75 73 28 75 2e 61 7a 2e 70 43 2d  revious(u.az.pC-
1dfba 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e  >pCursor, &u.az.
1dfbb 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66  res);.        if
1dfbc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1dfbd 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1dfbe 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20  _to_error;.     
1dfbf 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69     u.az.pC->rowi
1dfc0 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1dfc1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1dfc2 20 20 20 2f 2a 20 75 2e 61 7a 2e 72 65 73 20 6d     /* u.az.res m
1dfc3 69 67 68 74 20 62 65 20 6e 65 67 61 74 69 76 65  ight be negative
1dfc4 20 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62   because the tab
1dfc5 6c 65 20 69 73 20 65 6d 70 74 79 2e 20 20 43 68  le is empty.  Ch
1dfc6 65 63 6b 20 74 6f 0a 20 20 20 20 20 20 20 20 2a  eck to.        *
1dfc7 2a 20 73 65 65 20 69 66 20 74 68 69 73 20 69 73  * see if this is
1dfc8 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 20 20   the case..     
1dfc9 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e     */.        u.
1dfca 61 7a 2e 72 65 73 20 3d 20 73 71 6c 69 74 65 33  az.res = sqlite3
1dfcb 42 74 72 65 65 45 6f 66 28 75 2e 61 7a 2e 70 43  BtreeEof(u.az.pC
1dfcc 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->pCursor);.    
1dfcd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
1dfce 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1dfcf 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 7a 2e  );.    if( u.az.
1dfd0 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20  res ){.      pc 
1dfd1 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1dfd2 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
1dfd3 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e    /* This happen
1dfd4 73 20 77 68 65 6e 20 61 74 74 65 6d 70 74 69 6e  s when attemptin
1dfd5 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 73 71  g to open the sq
1dfd6 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 74 61 62  lite3_master tab
1dfd7 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 20 72 65  le.    ** for re
1dfd8 61 64 20 61 63 63 65 73 73 20 72 65 74 75 72 6e  ad access return
1dfd9 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20  s SQLITE_EMPTY. 
1dfda 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 77  In this case alw
1dfdb 61 79 73 0a 20 20 20 20 2a 2a 20 74 61 6b 65 20  ays.    ** take 
1dfdc 74 68 65 20 6a 75 6d 70 20 28 73 69 6e 63 65 20  the jump (since 
1dfdd 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63  there are no rec
1dfde 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c  ords in the tabl
1dfdf 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  e)..    */.    p
1dfe0 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1dfe1 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1dfe2 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b  ./* Opcode: Seek
1dfe3 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1dfe4 2a 2a 20 50 31 20 69 73 20 61 6e 20 6f 70 65 6e  ** P1 is an open
1dfe5 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 61 6e   table cursor an
1dfe6 64 20 50 32 20 69 73 20 61 20 72 6f 77 69 64 20  d P2 is a rowid 
1dfe7 69 6e 74 65 67 65 72 2e 20 20 41 72 72 61 6e 67  integer.  Arrang
1dfe8 65 0a 2a 2a 20 66 6f 72 20 50 31 20 74 6f 20 6d  e.** for P1 to m
1dfe9 6f 76 65 20 73 6f 20 74 68 61 74 20 69 74 20 70  ove so that it p
1dfea 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 72 6f 77  oints to the row
1dfeb 69 64 20 67 69 76 65 6e 20 62 79 20 50 32 2e 0a  id given by P2..
1dfec 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 63  **.** This is ac
1dfed 74 75 61 6c 6c 79 20 61 20 64 65 66 65 72 72 65  tually a deferre
1dfee 64 20 73 65 65 6b 2e 20 20 4e 6f 74 68 69 6e 67  d seek.  Nothing
1dfef 20 61 63 74 75 61 6c 6c 79 20 68 61 70 70 65 6e   actually happen
1dff0 73 20 75 6e 74 69 6c 0a 2a 2a 20 74 68 65 20 63  s until.** the c
1dff1 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 74 6f  ursor is used to
1dff2 20 72 65 61 64 20 61 20 72 65 63 6f 72 64 2e 20   read a record. 
1dff3 20 54 68 61 74 20 77 61 79 2c 20 69 66 20 6e 6f   That way, if no
1dff4 20 72 65 61 64 73 0a 2a 2a 20 6f 63 63 75 72 2c   reads.** occur,
1dff5 20 6e 6f 20 75 6e 6e 65 63 65 73 73 61 72 79 20   no unnecessary 
1dff6 49 2f 4f 20 68 61 70 70 65 6e 73 2e 0a 2a 2f 0a  I/O happens..*/.
1dff7 63 61 73 65 20 4f 50 5f 53 65 65 6b 3a 20 7b 20  case OP_Seek: { 
1dff8 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66     /* in2 */.#if
1dff9 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1dffa 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1dffb 6f 20 75 2e 62 61 20 2a 2f 0a 20 20 56 64 62 65  o u.ba */.  Vdbe
1dffc 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64  Cursor *pC;.#end
1dffd 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1dffe 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1dfff 20 75 2e 62 61 20 2a 2f 0a 0a 20 20 61 73 73 65   u.ba */..  asse
1e000 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e001 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e002 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 61 2e 70  rsor );.  u.ba.p
1e003 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e004 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1e005 20 75 2e 62 61 2e 70 43 21 3d 30 20 29 3b 0a 20   u.ba.pC!=0 );. 
1e006 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 61   if( ALWAYS(u.ba
1e007 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29  .pC->pCursor!=0)
1e008 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1e009 75 2e 62 61 2e 70 43 2d 3e 69 73 54 61 62 6c 65  u.ba.pC->isTable
1e00a 20 29 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d   );.    u.ba.pC-
1e00b 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20  >nullRow = 0;.  
1e00c 20 20 75 2e 62 61 2e 70 43 2d 3e 6d 6f 76 65 74    u.ba.pC->movet
1e00d 6f 54 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65  oTarget = sqlite
1e00e 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49  3VdbeIntValue(pI
1e00f 6e 32 29 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43  n2);.    u.ba.pC
1e010 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
1e011 20 30 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d   0;.    u.ba.pC-
1e012 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1e013 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  = 1;.  }.  break
1e014 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63 6f 64  ;.}.  ../* Opcod
1e015 65 3a 20 46 6f 75 6e 64 20 50 31 20 50 32 20 50  e: Found P1 P2 P
1e016 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69  3 * *.**.** Regi
1e017 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20  ster P3 holds a 
1e018 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64  blob constructed
1e019 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20   by MakeRecord. 
1e01a 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e   P1 is an index.
1e01b 0a 2a 2a 20 49 66 20 61 6e 20 65 6e 74 72 79 20  .** If an entry 
1e01c 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65  that matches the
1e01d 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1e01e 65 72 20 70 33 20 65 78 69 73 74 73 20 69 6e 20  er p3 exists in 
1e01f 50 31 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20  P1 then.** jump 
1e020 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65 20 50  to P2.  If the P
1e021 33 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74  3 value does not
1e022 20 6d 61 74 63 68 20 61 6e 79 20 65 6e 74 72 79   match any entry
1e023 20 69 6e 20 50 31 0a 2a 2a 20 74 68 65 6e 20 66   in P1.** then f
1e024 61 6c 6c 20 74 68 72 75 2e 20 20 54 68 65 20 50  all thru.  The P
1e025 31 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74  1 cursor is left
1e026 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65   pointing at the
1e027 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 79 0a   matching entry.
1e028 2a 2a 20 69 66 20 69 74 20 65 78 69 73 74 73 2e  ** if it exists.
1e029 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
1e02a 72 75 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  ruction is used 
1e02b 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1e02c 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 68 65   IN operator whe
1e02d 72 65 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 68  re the.** left-h
1e02e 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 53 45  and side is a SE
1e02f 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  LECT statement. 
1e030 20 50 31 20 6d 61 79 20 62 65 20 61 20 74 72 75   P1 may be a tru
1e031 65 20 69 6e 64 65 78 2c 20 6f 72 20 69 74 0a 2a  e index, or it.*
1e032 2a 20 6d 61 79 20 62 65 20 61 20 74 65 6d 70 6f  * may be a tempo
1e033 72 61 72 79 20 69 6e 64 65 78 20 74 68 61 74 20  rary index that 
1e034 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74  holds the result
1e035 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 0a  s of the SELECT.
1e036 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20  ** statement.   
1e037 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1e038 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 74 6f   is also used to
1e039 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a   implement the.*
1e03a 2a 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f  * DISTINCT keywo
1e03b 72 64 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61  rd in SELECT sta
1e03c 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  tements..**.** T
1e03d 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1e03e 63 68 65 63 6b 73 20 69 66 20 69 6e 64 65 78 20  checks if index 
1e03f 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65  P1 contains a re
1e040 63 6f 72 64 20 66 6f 72 20 77 68 69 63 68 20 0a  cord for which .
1e041 2a 2a 20 74 68 65 20 66 69 72 73 74 20 4e 20 73  ** the first N s
1e042 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73  erialized values
1e043 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 20 74   exactly match t
1e044 68 65 20 4e 20 73 65 72 69 61 6c 69 7a 65 64 20  he N serialized 
1e045 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 74 68 65  values.** in the
1e046 20 72 65 63 6f 72 64 20 69 6e 20 72 65 67 69 73   record in regis
1e047 74 65 72 20 50 33 2c 20 77 68 65 72 65 20 4e 20  ter P3, where N 
1e048 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  is the total num
1e049 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e  ber of values in
1e04a 0a 2a 2a 20 74 68 65 20 50 33 20 72 65 63 6f 72  .** the P3 recor
1e04b 64 20 28 74 68 65 20 50 33 20 72 65 63 6f 72 64  d (the P3 record
1e04c 20 69 73 20 61 20 70 72 65 66 69 78 20 6f 66 20   is a prefix of 
1e04d 74 68 65 20 50 31 20 72 65 63 6f 72 64 29 2e 20  the P1 record). 
1e04e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1e04f 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69   NotFound, IsUni
1e050 71 75 65 2c 20 4e 6f 74 45 78 69 73 74 73 0a 2a  que, NotExists.*
1e051 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74  /./* Opcode: Not
1e052 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33 20 2a  Found P1 P2 P3 *
1e053 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65   *.**.** Registe
1e054 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f  r P3 holds a blo
1e055 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79  b constructed by
1e056 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20 50 31   MakeRecord.  P1
1e057 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e   is.** an index.
1e058 20 20 49 66 20 6e 6f 20 65 6e 74 72 79 20 65 78    If no entry ex
1e059 69 73 74 73 20 69 6e 20 50 31 20 74 68 61 74 20  ists in P1 that 
1e05a 6d 61 74 63 68 65 73 20 74 68 65 20 62 6c 6f 62  matches the blob
1e05b 20 74 68 65 6e 20 6a 75 6d 70 0a 2a 2a 20 74 6f   then jump.** to
1e05c 20 50 32 2e 20 20 49 66 20 61 6e 20 65 6e 74 72   P2.  If an entr
1e05d 79 20 64 6f 65 73 20 65 78 69 73 74 69 6e 67 2c  y does existing,
1e05e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e 20 20   fall through.  
1e05f 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  The cursor is le
1e060 66 74 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74  ft.** pointing t
1e061 6f 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  o the entry that
1e062 20 6d 61 74 63 68 65 73 2e 0a 2a 2a 0a 2a 2a 20   matches..**.** 
1e063 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c  See also: Found,
1e064 20 4e 6f 74 45 78 69 73 74 73 2c 20 49 73 55 6e   NotExists, IsUn
1e065 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ique.*/.case OP_
1e066 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 20 20 20 20  NotFound:       
1e067 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a  /* jump, in3 */.
1e068 63 61 73 65 20 4f 50 5f 46 6f 75 6e 64 3a 20 7b  case OP_Found: {
1e069 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
1e06a 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f   in3 */.#if 0  /
1e06b 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e06c 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e06d 62 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 72 65 61  b */.  int alrea
1e06e 64 79 45 78 69 73 74 73 3b 0a 20 20 56 64 62 65  dyExists;.  Vdbe
1e06f 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 6e  Cursor *pC;.  in
1e070 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65  t res;.  Unpacke
1e071 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79  dRecord *pIdxKey
1e072 3b 0a 20 20 63 68 61 72 20 61 54 65 6d 70 52 65  ;.  char aTempRe
1e073 63 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28  c[ROUND8(sizeof(
1e074 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29  UnpackedRecord))
1e075 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 33   + sizeof(Mem)*3
1e076 20 2b 20 37 5d 3b 0a 23 65 6e 64 69 66 20 2f 2a   + 7];.#endif /*
1e077 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e078 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 62   moved into u.bb
1e079 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
1e07a 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65  TE_TEST.  sqlite
1e07b 33 5f 66 6f 75 6e 64 5f 63 6f 75 6e 74 2b 2b 3b  3_found_count++;
1e07c 0a 23 65 6e 64 69 66 0a 0a 20 20 75 2e 62 62 2e  .#endif..  u.bb.
1e07d 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 3d 20  alreadyExists = 
1e07e 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  0;.  assert( pOp
1e07f 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1e080 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1e081 0a 20 20 75 2e 62 62 2e 70 43 20 3d 20 70 2d 3e  .  u.bb.pC = p->
1e082 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1e083 20 20 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70    assert( u.bb.p
1e084 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c  C!=0 );.  if( AL
1e085 57 41 59 53 28 75 2e 62 62 2e 70 43 2d 3e 70 43  WAYS(u.bb.pC->pC
1e086 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a 0a 20 20  ursor!=0) ){..  
1e087 20 20 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70    assert( u.bb.p
1e088 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b  C->isTable==0 );
1e089 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e  .    assert( pIn
1e08a 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42  3->flags & MEM_B
1e08b 6c 6f 62 20 29 3b 0a 20 20 20 20 45 78 70 61 6e  lob );.    Expan
1e08c 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20  dBlob(pIn3);.   
1e08d 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 20 3d 20   u.bb.pIdxKey = 
1e08e 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
1e08f 64 55 6e 70 61 63 6b 28 75 2e 62 62 2e 70 43 2d  dUnpack(u.bb.pC-
1e090 3e 70 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d  >pKeyInfo, pIn3-
1e091 3e 6e 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20  >n, pIn3->z,.   
1e092 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e094 20 20 20 75 2e 62 62 2e 61 54 65 6d 70 52 65 63     u.bb.aTempRec
1e095 2c 20 73 69 7a 65 6f 66 28 75 2e 62 62 2e 61 54  , sizeof(u.bb.aT
1e096 65 6d 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66  empRec));.    if
1e097 28 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 3d 3d  ( u.bb.pIdxKey==
1e098 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
1e099 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20  no_mem;.    }.  
1e09a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
1e09b 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20  e==OP_Found ){. 
1e09c 20 20 20 20 20 75 2e 62 62 2e 70 49 64 78 4b 65       u.bb.pIdxKe
1e09d 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41  y->flags |= UNPA
1e09e 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43  CKED_PREFIX_MATC
1e09f 48 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  H;.    }.    rc 
1e0a0 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
1e0a1 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62  vetoUnpacked(u.b
1e0a2 62 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75  b.pC->pCursor, u
1e0a3 2e 62 62 2e 70 49 64 78 4b 65 79 2c 20 30 2c 20  .bb.pIdxKey, 0, 
1e0a4 30 2c 20 26 75 2e 62 62 2e 72 65 73 29 3b 0a 20  0, &u.bb.res);. 
1e0a5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65     sqlite3VdbeDe
1e0a6 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f  leteUnpackedReco
1e0a7 72 64 28 75 2e 62 62 2e 70 49 64 78 4b 65 79 29  rd(u.bb.pIdxKey)
1e0a8 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
1e0a9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1e0aa 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1e0ab 20 20 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78    u.bb.alreadyEx
1e0ac 69 73 74 73 20 3d 20 28 75 2e 62 62 2e 72 65 73  ists = (u.bb.res
1e0ad 3d 3d 30 29 3b 0a 20 20 20 20 75 2e 62 62 2e 70  ==0);.    u.bb.p
1e0ae 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1e0af 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 62 2e  o = 0;.    u.bb.
1e0b0 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1e0b1 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
1e0b2 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70   }.  if( pOp->op
1e0b3 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29  code==OP_Found )
1e0b4 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 62 2e 61  {.    if( u.bb.a
1e0b5 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20 70  lreadyExists ) p
1e0b6 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1e0b7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66  .  }else{.    if
1e0b8 28 20 21 75 2e 62 62 2e 61 6c 72 65 61 64 79 45  ( !u.bb.alreadyE
1e0b9 78 69 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70  xists ) pc = pOp
1e0ba 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20  ->p2 - 1;.  }.  
1e0bb 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1e0bc 6f 64 65 3a 20 49 73 55 6e 69 71 75 65 20 50 31  ode: IsUnique P1
1e0bd 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1e0be 2a 20 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f  * Cursor P1 is o
1e0bf 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 64 65 78 2e  pen on an index.
1e0c0 20 20 53 6f 20 69 74 20 68 61 73 20 6e 6f 20 64    So it has no d
1e0c1 61 74 61 20 61 6e 64 20 69 74 73 20 6b 65 79 20  ata and its key 
1e0c2 63 6f 6e 73 69 73 74 73 20 0a 2a 2a 20 6f 66 20  consists .** of 
1e0c3 61 20 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74  a record generat
1e0c4 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63  ed by OP_MakeRec
1e0c5 6f 72 64 20 77 68 65 72 65 20 74 68 65 20 6c 61  ord where the la
1e0c6 73 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20  st field is the 
1e0c7 0a 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65  .** rowid of the
1e0c8 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20   entry that the 
1e0c9 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e  index refers to.
1e0ca 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 20 72 65  .**.** The P3 re
1e0cb 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 73 20  gister contains 
1e0cc 61 6e 20 69 6e 74 65 67 65 72 20 72 65 63 6f 72  an integer recor
1e0cd 64 20 6e 75 6d 62 65 72 2e 20 43 61 6c 6c 20 74  d number. Call t
1e0ce 68 69 73 20 72 65 63 6f 72 64 20 0a 2a 2a 20 6e  his record .** n
1e0cf 75 6d 62 65 72 20 52 2e 20 52 65 67 69 73 74 65  umber R. Registe
1e0d0 72 20 50 34 20 69 73 20 74 68 65 20 66 69 72 73  r P4 is the firs
1e0d1 74 20 69 6e 20 61 20 73 65 74 20 6f 66 20 4e 20  t in a set of N 
1e0d2 63 6f 6e 74 69 67 75 6f 75 73 20 72 65 67 69 73  contiguous regis
1e0d3 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 6d 61 6b  ters.** that mak
1e0d4 65 20 75 70 20 61 6e 20 75 6e 70 61 63 6b 65 64  e up an unpacked
1e0d5 20 69 6e 64 65 78 20 6b 65 79 20 74 68 61 74 20   index key that 
1e0d6 63 61 6e 20 62 65 20 75 73 65 64 20 77 69 74 68  can be used with
1e0d7 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54   cursor P1..** T
1e0d8 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 20 63 61  he value of N ca
1e0d9 6e 20 62 65 20 69 6e 66 65 72 72 65 64 20 66 72  n be inferred fr
1e0da 6f 6d 20 74 68 65 20 63 75 72 73 6f 72 2e 20 4e  om the cursor. N
1e0db 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 72 6f   includes the ro
1e0dc 77 69 64 0a 2a 2a 20 76 61 6c 75 65 20 61 70 70  wid.** value app
1e0dd 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64  ended to the end
1e0de 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 72 65   of the index re
1e0df 63 6f 72 64 2e 20 54 68 69 73 20 72 6f 77 69 64  cord. This rowid
1e0e0 20 76 61 6c 75 65 20 6d 61 79 0a 2a 2a 20 6f 72   value may.** or
1e0e1 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20   may not be the 
1e0e2 73 61 6d 65 20 61 73 20 52 2e 0a 2a 2a 0a 2a 2a  same as R..**.**
1e0e3 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20 4e   If any of the N
1e0e4 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e   registers begin
1e0e5 6e 69 6e 67 20 77 69 74 68 20 72 65 67 69 73 74  ning with regist
1e0e6 65 72 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61  er P4 contains a
1e0e7 20 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65 2c 20   NULL.** value, 
1e0e8 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
1e0e9 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 4f 74   to P2..**.** Ot
1e0ea 68 65 72 77 69 73 65 2c 20 74 68 69 73 20 69 6e  herwise, this in
1e0eb 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73  struction checks
1e0ec 20 69 66 20 63 75 72 73 6f 72 20 50 31 20 63 6f   if cursor P1 co
1e0ed 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 0a  ntains an entry.
1e0ee 2a 2a 20 77 68 65 72 65 20 74 68 65 20 66 69 72  ** where the fir
1e0ef 73 74 20 28 4e 2d 31 29 20 66 69 65 6c 64 73 20  st (N-1) fields 
1e0f0 6d 61 74 63 68 20 62 75 74 20 74 68 65 20 72 6f  match but the ro
1e0f1 77 69 64 20 76 61 6c 75 65 20 61 74 20 74 68 65  wid value at the
1e0f2 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 69   end.** of the i
1e0f3 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 6e 6f  ndex entry is no
1e0f4 74 20 52 2e 20 49 66 20 74 68 65 72 65 20 69 73  t R. If there is
1e0f5 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20   no such entry, 
1e0f6 63 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a 2a 2a  control jumps.**
1e0f7 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   to instruction 
1e0f8 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74  P2. Otherwise, t
1e0f9 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
1e0fa 63 6f 6e 66 6c 69 63 74 69 6e 67 20 69 6e 64 65  conflicting inde
1e0fb 78 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20 63 6f  x.** entry is co
1e0fc 70 69 65 64 20 74 6f 20 72 65 67 69 73 74 65 72  pied to register
1e0fd 20 50 33 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20   P3 and control 
1e0fe 66 61 6c 6c 73 20 74 68 72 6f 75 67 68 20 74 6f  falls through to
1e0ff 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 73   the next.** ins
1e100 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  truction..**.** 
1e101 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75  See also: NotFou
1e102 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 46  nd, NotExists, F
1e103 6f 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ound.*/.case OP_
1e104 49 73 55 6e 69 71 75 65 3a 20 7b 20 20 20 20 20  IsUnique: {     
1e105 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20     /* jump, in3 
1e106 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1e107 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e108 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a  ed into u.bc */.
1e109 20 20 75 31 36 20 69 69 3b 0a 20 20 56 64 62 65    u16 ii;.  Vdbe
1e10a 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 42  Cursor *pCx;.  B
1e10b 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1e10c 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20    u16 nField;.  
1e10d 4d 65 6d 20 2a 61 4d 65 6d 3b 0a 20 20 55 6e 70  Mem *aMem;.  Unp
1e10e 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 20 20  ackedRecord r;  
1e10f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e110 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65 78 20  /* B-Tree index 
1e111 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20 20  search key */.  
1e112 69 36 34 20 52 3b 20 20 20 20 20 20 20 20 20 20  i64 R;          
1e113 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e114 20 20 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f 72     /* Rowid stor
1e115 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
1e116 33 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  3 */.#endif /* l
1e117 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e118 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 2a  oved into u.bc *
1e119 2f 0a 0a 20 20 75 2e 62 63 2e 61 4d 65 6d 20 3d  /..  u.bc.aMem =
1e11a 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1e11b 34 2e 69 5d 3b 0a 20 20 2f 2a 20 41 73 73 65 72  4.i];.  /* Asser
1e11c 74 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65  t that the value
1e11d 73 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20  s of parameters 
1e11e 50 31 20 61 6e 64 20 50 34 20 61 72 65 20 69 6e  P1 and P4 are in
1e11f 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 61 73 73   range. */.  ass
1e120 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
1e121 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20  ==P4_INT32 );.  
1e122 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
1e123 69 3e 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69  i>0 && pOp->p4.i
1e124 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61  <=p->nMem );.  a
1e125 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1e126 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e  0 && pOp->p1<p->
1e127 6e 43 75 72 73 6f 72 20 29 3b 0a 0a 20 20 2f 2a  nCursor );..  /*
1e128 20 46 69 6e 64 20 74 68 65 20 69 6e 64 65 78 20   Find the index 
1e129 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 75 2e 62  cursor. */.  u.b
1e12a 63 2e 70 43 78 20 3d 20 70 2d 3e 61 70 43 73 72  c.pCx = p->apCsr
1e12b 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1e12c 65 72 74 28 20 75 2e 62 63 2e 70 43 78 2d 3e 64  ert( u.bc.pCx->d
1e12d 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30  eferredMoveto==0
1e12e 20 29 3b 0a 20 20 75 2e 62 63 2e 70 43 78 2d 3e   );.  u.bc.pCx->
1e12f 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a  seekResult = 0;.
1e130 20 20 75 2e 62 63 2e 70 43 78 2d 3e 63 61 63 68    u.bc.pCx->cach
1e131 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1e132 53 54 41 4c 45 3b 0a 20 20 75 2e 62 63 2e 70 43  STALE;.  u.bc.pC
1e133 72 73 72 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e  rsr = u.bc.pCx->
1e134 70 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 49  pCursor;..  /* I
1e135 66 20 61 6e 79 20 6f 66 20 74 68 65 20 76 61 6c  f any of the val
1e136 75 65 73 20 61 72 65 20 4e 55 4c 4c 2c 20 74 61  ues are NULL, ta
1e137 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 2a 2f 0a  ke the jump. */.
1e138 20 20 75 2e 62 63 2e 6e 46 69 65 6c 64 20 3d 20    u.bc.nField = 
1e139 75 2e 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49 6e  u.bc.pCx->pKeyIn
1e13a 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 66 6f  fo->nField;.  fo
1e13b 72 28 75 2e 62 63 2e 69 69 3d 30 3b 20 75 2e 62  r(u.bc.ii=0; u.b
1e13c 63 2e 69 69 3c 75 2e 62 63 2e 6e 46 69 65 6c 64  c.ii<u.bc.nField
1e13d 3b 20 75 2e 62 63 2e 69 69 2b 2b 29 7b 0a 20 20  ; u.bc.ii++){.  
1e13e 20 20 69 66 28 20 75 2e 62 63 2e 61 4d 65 6d 5b    if( u.bc.aMem[
1e13f 75 2e 62 63 2e 69 69 5d 2e 66 6c 61 67 73 20 26  u.bc.ii].flags &
1e140 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1e141 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1e142 2d 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 63 2e  - 1;.      u.bc.
1e143 70 43 72 73 72 20 3d 20 30 3b 0a 20 20 20 20 20  pCrsr = 0;.     
1e144 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1e145 7d 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 62  }.  assert( (u.b
1e146 63 2e 61 4d 65 6d 5b 75 2e 62 63 2e 6e 46 69 65  c.aMem[u.bc.nFie
1e147 6c 64 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ld].flags & MEM_
1e148 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 0a 20 20 69  Null)==0 );..  i
1e149 66 28 20 75 2e 62 63 2e 70 43 72 73 72 21 3d 30  f( u.bc.pCrsr!=0
1e14a 20 29 7b 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c   ){.    /* Popul
1e14b 61 74 65 20 74 68 65 20 69 6e 64 65 78 20 73 65  ate the index se
1e14c 61 72 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20  arch key. */.   
1e14d 20 75 2e 62 63 2e 72 2e 70 4b 65 79 49 6e 66 6f   u.bc.r.pKeyInfo
1e14e 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 4b 65   = u.bc.pCx->pKe
1e14f 79 49 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 63 2e  yInfo;.    u.bc.
1e150 72 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 62 63 2e  r.nField = u.bc.
1e151 6e 46 69 65 6c 64 20 2b 20 31 3b 0a 20 20 20 20  nField + 1;.    
1e152 75 2e 62 63 2e 72 2e 66 6c 61 67 73 20 3d 20 55  u.bc.r.flags = U
1e153 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53  NPACKED_PREFIX_S
1e154 45 41 52 43 48 3b 0a 20 20 20 20 75 2e 62 63 2e  EARCH;.    u.bc.
1e155 72 2e 61 4d 65 6d 20 3d 20 75 2e 62 63 2e 61 4d  r.aMem = u.bc.aM
1e156 65 6d 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72  em;..    /* Extr
1e157 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  act the value of
1e158 20 75 2e 62 63 2e 52 20 66 72 6f 6d 20 72 65 67   u.bc.R from reg
1e159 69 73 74 65 72 20 50 33 2e 20 2a 2f 0a 20 20 20  ister P3. */.   
1e15a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
1e15b 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b  ntegerify(pIn3);
1e15c 0a 20 20 20 20 75 2e 62 63 2e 52 20 3d 20 70 49  .    u.bc.R = pI
1e15d 6e 33 2d 3e 75 2e 69 3b 0a 0a 20 20 20 20 2f 2a  n3->u.i;..    /*
1e15e 20 53 65 61 72 63 68 20 74 68 65 20 42 2d 54 72   Search the B-Tr
1e15f 65 65 20 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20  ee index. If no 
1e160 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f  conflicting reco
1e161 72 64 20 69 73 20 66 6f 75 6e 64 2c 20 6a 75 6d  rd is found, jum
1e162 70 0a 20 20 20 20 2a 2a 20 74 6f 20 50 32 2e 20  p.    ** to P2. 
1e163 4f 74 68 65 72 77 69 73 65 2c 20 63 6f 70 79 20  Otherwise, copy 
1e164 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
1e165 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63   conflicting rec
1e166 6f 72 64 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65  ord to.    ** re
1e167 67 69 73 74 65 72 20 50 33 20 61 6e 64 20 66 61  gister P3 and fa
1e168 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
1e169 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
1e16a 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  on.  */.    rc =
1e16b 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
1e16c 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 63  etoUnpacked(u.bc
1e16d 2e 70 43 72 73 72 2c 20 26 75 2e 62 63 2e 72 2c  .pCrsr, &u.bc.r,
1e16e 20 30 2c 20 30 2c 20 26 75 2e 62 63 2e 70 43 78   0, 0, &u.bc.pCx
1e16f 2d 3e 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 20  ->seekResult);. 
1e170 20 20 20 69 66 28 20 28 75 2e 62 63 2e 72 2e 66     if( (u.bc.r.f
1e171 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f  lags & UNPACKED_
1e172 50 52 45 46 49 58 5f 53 45 41 52 43 48 29 20 7c  PREFIX_SEARCH) |
1e173 7c 20 75 2e 62 63 2e 72 2e 72 6f 77 69 64 3d 3d  | u.bc.r.rowid==
1e174 75 2e 62 63 2e 52 20 29 7b 0a 20 20 20 20 20 20  u.bc.R ){.      
1e175 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1e176 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1e177 20 20 20 70 49 6e 33 2d 3e 75 2e 69 20 3d 20 75     pIn3->u.i = u
1e178 2e 62 63 2e 72 2e 72 6f 77 69 64 3b 0a 20 20 20  .bc.r.rowid;.   
1e179 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
1e17a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  }../* Opcode: No
1e17b 74 45 78 69 73 74 73 20 50 31 20 50 32 20 50 33  tExists P1 P2 P3
1e17c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74   * *.**.** Use t
1e17d 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
1e17e 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20 69  gister P3 as a i
1e17f 6e 74 65 67 65 72 20 6b 65 79 2e 20 20 49 66 20  nteger key.  If 
1e180 61 20 72 65 63 6f 72 64 20 0a 2a 2a 20 77 69 74  a record .** wit
1e181 68 20 74 68 61 74 20 6b 65 79 20 64 6f 65 73 20  h that key does 
1e182 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 61 62  not exist in tab
1e183 6c 65 20 6f 66 20 50 31 2c 20 74 68 65 6e 20 6a  le of P1, then j
1e184 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 20 49  ump to P2. .** I
1e185 66 20 74 68 65 20 72 65 63 6f 72 64 20 64 6f 65  f the record doe
1e186 73 20 65 78 69 73 74 2c 20 74 68 65 6e 20 66 61  s exist, then fa
1e187 6c 6c 20 74 68 72 75 2e 20 20 54 68 65 20 63 75  ll thru.  The cu
1e188 72 73 6f 72 20 69 73 20 6c 65 66 74 20 0a 2a 2a  rsor is left .**
1e189 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65   pointing to the
1e18a 20 72 65 63 6f 72 64 20 69 66 20 69 74 20 65 78   record if it ex
1e18b 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ists..**.** The 
1e18c 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
1e18d 65 6e 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  en this operatio
1e18e 6e 20 61 6e 64 20 4e 6f 74 46 6f 75 6e 64 20 69  n and NotFound i
1e18f 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 6f  s that this.** o
1e190 70 65 72 61 74 69 6f 6e 20 61 73 73 75 6d 65 73  peration assumes
1e191 20 74 68 65 20 6b 65 79 20 69 73 20 61 6e 20 69   the key is an i
1e192 6e 74 65 67 65 72 20 61 6e 64 20 74 68 61 74 20  nteger and that 
1e193 50 31 20 69 73 20 61 20 74 61 62 6c 65 20 77 68  P1 is a table wh
1e194 65 72 65 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e  ereas.** NotFoun
1e195 64 20 61 73 73 75 6d 65 73 20 6b 65 79 20 69 73  d assumes key is
1e196 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63   a blob construc
1e197 74 65 64 20 66 72 6f 6d 20 4d 61 6b 65 52 65 63  ted from MakeRec
1e198 6f 72 64 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73  ord and.** P1 is
1e199 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a   an index..**.**
1e19a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64   See also: Found
1e19b 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e  , NotFound, IsUn
1e19c 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ique.*/.case OP_
1e19d 4e 6f 74 45 78 69 73 74 73 3a 20 7b 20 20 20 20  NotExists: {    
1e19e 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33      /* jump, in3
1e19f 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
1e1a0 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e1a1 76 65 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f  ved into u.bd */
1e1a2 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1e1a3 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
1e1a4 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b  Crsr;.  int res;
1e1a5 0a 20 20 75 36 34 20 69 4b 65 79 3b 0a 23 65 6e  .  u64 iKey;.#en
1e1a6 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e1a7 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e1a8 6f 20 75 2e 62 64 20 2a 2f 0a 0a 20 20 61 73 73  o u.bd */..  ass
1e1a9 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73  ert( pIn3->flags
1e1aa 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20   & MEM_Int );.  
1e1ab 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1e1ac 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
1e1ad 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e  >nCursor );.  u.
1e1ae 62 64 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  bd.pC = p->apCsr
1e1af 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1e1b0 65 72 74 28 20 75 2e 62 64 2e 70 43 21 3d 30 20  ert( u.bd.pC!=0 
1e1b1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  );.  assert( u.b
1e1b2 64 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b  d.pC->isTable );
1e1b3 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e  .  assert( u.bd.
1e1b4 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52  pC->pseudoTableR
1e1b5 65 67 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 64 2e  eg==0 );.  u.bd.
1e1b6 70 43 72 73 72 20 3d 20 75 2e 62 64 2e 70 43 2d  pCrsr = u.bd.pC-
1e1b7 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20  >pCursor;.  if( 
1e1b8 75 2e 62 64 2e 70 43 72 73 72 21 3d 30 20 29 7b  u.bd.pCrsr!=0 ){
1e1b9 0a 20 20 20 20 75 2e 62 64 2e 72 65 73 20 3d 20  .    u.bd.res = 
1e1ba 30 3b 0a 20 20 20 20 75 2e 62 64 2e 69 4b 65 79  0;.    u.bd.iKey
1e1bb 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20   = pIn3->u.i;.  
1e1bc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1e1bd 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
1e1be 64 28 75 2e 62 64 2e 70 43 72 73 72 2c 20 30 2c  d(u.bd.pCrsr, 0,
1e1bf 20 75 2e 62 64 2e 69 4b 65 79 2c 20 30 2c 20 26   u.bd.iKey, 0, &
1e1c0 75 2e 62 64 2e 72 65 73 29 3b 0a 20 20 20 20 75  u.bd.res);.    u
1e1c1 2e 62 64 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69  .bd.pC->lastRowi
1e1c2 64 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20  d = pIn3->u.i;. 
1e1c3 20 20 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69     u.bd.pC->rowi
1e1c4 64 49 73 56 61 6c 69 64 20 3d 20 75 2e 62 64 2e  dIsValid = u.bd.
1e1c5 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20  res==0 ?1:0;.   
1e1c6 20 75 2e 62 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f   u.bd.pC->nullRo
1e1c7 77 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e  w = 0;.    u.bd.
1e1c8 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1e1c9 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
1e1ca 20 20 20 75 2e 62 64 2e 70 43 2d 3e 64 65 66 65     u.bd.pC->defe
1e1cb 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a  rredMoveto = 0;.
1e1cc 20 20 20 20 69 66 28 20 75 2e 62 64 2e 72 65 73      if( u.bd.res
1e1cd 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20  !=0 ){.      pc 
1e1ce 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1e1cf 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62       assert( u.b
1e1d0 64 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c  d.pC->rowidIsVal
1e1d1 69 64 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20  id==0 );.    }. 
1e1d2 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73 65 65 6b     u.bd.pC->seek
1e1d3 52 65 73 75 6c 74 20 3d 20 75 2e 62 64 2e 72 65  Result = u.bd.re
1e1d4 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  s;.  }else{.    
1e1d5 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
1e1d6 77 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20  when an attempt 
1e1d7 74 6f 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63  to open a read c
1e1d8 75 72 73 6f 72 20 6f 6e 20 74 68 65 0a 20 20 20  ursor on the.   
1e1d9 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65   ** sqlite_maste
1e1da 72 20 74 61 62 6c 65 20 72 65 74 75 72 6e 73 20  r table returns 
1e1db 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e 0a 20 20  SQLITE_EMPTY..  
1e1dc 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f    */.    pc = pO
1e1dd 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 61  p->p2 - 1;.    a
1e1de 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e  ssert( u.bd.pC->
1e1df 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20  rowidIsValid==0 
1e1e0 29 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e  );.    u.bd.pC->
1e1e1 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a  seekResult = 0;.
1e1e2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1e1e3 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 71 75 65  /* Opcode: Seque
1e1e4 6e 63 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  nce P1 P2 * * *.
1e1e5 2a 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e  **.** Find the n
1e1e6 65 78 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65  ext available se
1e1e7 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 66 6f  quence number fo
1e1e8 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20  r cursor P1..** 
1e1e9 57 72 69 74 65 20 74 68 65 20 73 65 71 75 65 6e  Write the sequen
1e1ea 63 65 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 72  ce number into r
1e1eb 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 54  egister P2..** T
1e1ec 68 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62  he sequence numb
1e1ed 65 72 20 6f 6e 20 74 68 65 20 63 75 72 73 6f 72  er on the cursor
1e1ee 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
1e1ef 61 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e  after this.** in
1e1f0 73 74 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a  struction.  .*/.
1e1f1 63 61 73 65 20 4f 50 5f 53 65 71 75 65 6e 63 65  case OP_Sequence
1e1f2 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  : {           /*
1e1f3 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
1e1f4 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
1e1f5 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1e1f6 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
1e1f7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
1e1f8 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30  pCsr[pOp->p1]!=0
1e1f9 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20   );.  pOut->u.i 
1e1fa 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1e1fb 70 31 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b  p1]->seqCount++;
1e1fc 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1e1fd 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
1e1fe 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f  ;.  break;.}.../
1e1ff 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77  * Opcode: NewRow
1e200 69 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  id P1 P2 P3 * *.
1e201 2a 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20  **.** Get a new 
1e202 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e  integer record n
1e203 75 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f  umber (a.k.a "ro
1e204 77 69 64 22 29 20 75 73 65 64 20 61 73 20 74 68  wid") used as th
1e205 65 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65  e key to a table
1e206 2e 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20  ..** The record 
1e207 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72  number is not pr
1e208 65 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73  eviously used as
1e209 20 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61   a key in the da
1e20a 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20  tabase.** table 
1e20b 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70  that cursor P1 p
1e20c 6f 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e  oints to.  The n
1e20d 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  ew record number
1e20e 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77   is written.** w
1e20f 72 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74  ritten to regist
1e210 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P2..**.** If 
1e211 50 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20  P3>0 then P3 is 
1e212 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68  a register in th
1e213 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20  e root frame of 
1e214 74 68 69 73 20 56 44 42 45 20 74 68 61 74 20 68  this VDBE that h
1e215 6f 6c 64 73 20 0a 2a 2a 20 74 68 65 20 6c 61 72  olds .** the lar
1e216 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c 79 20  gest previously 
1e217 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64  generated record
1e218 20 6e 75 6d 62 65 72 2e 20 4e 6f 20 6e 65 77 20   number. No new 
1e219 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61  record numbers a
1e21a 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f  re.** allowed to
1e21b 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68   be less than th
1e21c 69 73 20 76 61 6c 75 65 2e 20 57 68 65 6e 20 74  is value. When t
1e21d 68 69 73 20 76 61 6c 75 65 20 72 65 61 63 68 65  his value reache
1e21e 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 0a  s its maximum, .
1e21f 2a 2a 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c  ** a SQLITE_FULL
1e220 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61   error is genera
1e221 74 65 64 2e 20 54 68 65 20 50 33 20 72 65 67 69  ted. The P3 regi
1e222 73 74 65 72 20 69 73 20 75 70 64 61 74 65 64 20  ster is updated 
1e223 77 69 74 68 20 74 68 65 20 27 0a 2a 2a 20 67 65  with the '.** ge
1e224 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e  nerated record n
1e225 75 6d 62 65 72 2e 20 54 68 69 73 20 50 33 20 6d  umber. This P3 m
1e226 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64  echanism is used
1e227 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65   to help impleme
1e228 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e  nt the.** AUTOIN
1e229 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e  CREMENT feature.
1e22a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52  .*/.case OP_NewR
1e22b 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20  owid: {         
1e22c 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1e22d 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f  ease */.#if 0  /
1e22e 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e22f 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e230 65 20 2a 2f 0a 20 20 69 36 34 20 76 3b 20 20 20  e */.  i64 v;   
1e231 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e232 20 54 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a   The new rowid *
1e233 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1e234 70 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75  pC;        /* Cu
1e235 72 73 6f 72 20 6f 66 20 74 61 62 6c 65 20 74 6f  rsor of table to
1e236 20 67 65 74 20 74 68 65 20 6e 65 77 20 72 6f 77   get the new row
1e237 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b  id */.  int res;
1e238 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e239 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 73  * Result of an s
1e23a 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28  qlite3BtreeLast(
1e23b 29 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20  ) */.  int cnt; 
1e23c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e23d 20 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69   Counter to limi
1e23e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
1e23f 73 65 61 72 63 68 65 73 20 2a 2f 0a 20 20 4d 65  searches */.  Me
1e240 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *pMem;        
1e241 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
1e242 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74   holding largest
1e243 20 72 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49   rowid for AUTOI
1e244 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 56 64  NCREMENT */.  Vd
1e245 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
1e246 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61       /* Root fra
1e247 6d 65 20 6f 66 20 56 44 42 45 20 2a 2f 0a 23 65  me of VDBE */.#e
1e248 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e249 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e24a 74 6f 20 75 2e 62 65 20 2a 2f 0a 0a 20 20 75 2e  to u.be */..  u.
1e24b 62 65 2e 76 20 3d 20 30 3b 0a 20 20 75 2e 62 65  be.v = 0;.  u.be
1e24c 2e 72 65 73 20 3d 20 30 3b 0a 20 20 61 73 73 65  .res = 0;.  asse
1e24d 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e24e 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e24f 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 65 2e 70  rsor );.  u.be.p
1e250 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e251 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1e252 20 75 2e 62 65 2e 70 43 21 3d 30 20 29 3b 0a 20   u.be.pC!=0 );. 
1e253 20 69 66 28 20 4e 45 56 45 52 28 75 2e 62 65 2e   if( NEVER(u.be.
1e254 70 43 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 29 20  pC->pCursor==0) 
1e255 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65  ){.    /* The ze
1e256 72 6f 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ro initializatio
1e257 6e 20 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74  n above is all t
1e258 68 61 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f  hat is needed */
1e259 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1e25a 20 54 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20   The next rowid 
1e25b 6f 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  or record number
1e25c 20 28 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d   (different term
1e25d 73 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20  s for the same. 
1e25e 20 20 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20     ** thing) is 
1e25f 6f 62 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77  obtained in a tw
1e260 6f 2d 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d  o-step algorithm
1e261 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1e262 46 69 72 73 74 20 77 65 20 61 74 74 65 6d 70 74  First we attempt
1e263 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72   to find the lar
1e264 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f  gest existing ro
1e265 77 69 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a  wid and add one.
1e266 20 20 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20      ** to that. 
1e267 20 42 75 74 20 69 66 20 74 68 65 20 6c 61 72 67   But if the larg
1e268 65 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77  est existing row
1e269 69 64 20 69 73 20 61 6c 72 65 61 64 79 20 74 68  id is already th
1e26a 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a  e maximum.    **
1e26b 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65   positive intege
1e26c 72 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 61  r, we have to fa
1e26d 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
1e26e 65 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20  e second.    ** 
1e26f 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c  probabilistic al
1e270 67 6f 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20  gorithm.    **. 
1e271 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64     ** The second
1e272 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f   algorithm is to
1e273 20 73 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20   select a rowid 
1e274 61 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65  at random and se
1e275 65 20 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61  e if.    ** it a
1e276 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 69 6e  lready exists in
1e277 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20   the table.  If 
1e278 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  it does not exis
1e279 74 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a  t, we have.    *
1e27a 2a 20 73 75 63 63 65 65 64 65 64 2e 20 20 49 66  * succeeded.  If
1e27b 20 74 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69   the random rowi
1e27c 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65  d does exist, we
1e27d 20 73 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e   select a new on
1e27e 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79  e.    ** and try
1e27f 20 61 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30   again, up to 10
1e280 30 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2f 0a  0 times..    */.
1e281 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 65      assert( u.be
1e282 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a  .pC->isTable );.
1e283 20 20 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 30      u.be.cnt = 0
1e284 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
1e285 5f 33 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20  _32BIT_ROWID.#  
1e286 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49   define MAX_ROWI
1e287 44 20 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c  D 0x7fffffff.#el
1e288 73 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63  se.    /* Some c
1e289 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69  ompilers complai
1e28a 6e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74  n about constant
1e28b 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78  s of the form 0x
1e28c 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  7fffffffffffffff
1e28d 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20  ..    ** Others 
1e28e 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30  complain about 0
1e28f 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66  x7ffffffffffffff
1e290 66 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  ffLL.  The follo
1e291 77 69 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73  wing macro seems
1e292 0a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69  .    ** to provi
1e293 64 65 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20  de the constant 
1e294 77 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c  while making all
1e295 20 63 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79   compilers happy
1e296 2e 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66  ..    */.#   def
1e297 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28  ine MAX_ROWID  (
1e298 69 36 34 29 28 20 28 28 28 75 36 34 29 30 78 37  i64)( (((u64)0x7
1e299 66 66 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20  fffffff)<<32) | 
1e29a 28 75 36 34 29 30 78 66 66 66 66 66 66 66 66 20  (u64)0xffffffff 
1e29b 29 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66  ).#endif..    if
1e29c 28 20 21 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52  ( !u.be.pC->useR
1e29d 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20  andomRowid ){.  
1e29e 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 73 71 6c      u.be.v = sql
1e29f 69 74 65 33 42 74 72 65 65 47 65 74 43 61 63 68  ite3BtreeGetCach
1e2a0 65 64 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d  edRowid(u.be.pC-
1e2a1 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >pCursor);.     
1e2a2 20 69 66 28 20 75 2e 62 65 2e 76 3d 3d 30 20 29   if( u.be.v==0 )
1e2a3 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
1e2a4 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28  qlite3BtreeLast(
1e2a5 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.be.pC->pCursor
1e2a6 2c 20 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20  , &u.be.res);.  
1e2a7 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1e2a8 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1e2a9 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
1e2aa 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1e2ab 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e2ac 69 66 28 20 75 2e 62 65 2e 72 65 73 20 29 7b 0a  if( u.be.res ){.
1e2ad 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76            u.be.v
1e2ae 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65   = 1;.        }e
1e2af 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61  lse{.          a
1e2b0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1e2b1 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64  reeCursorIsValid
1e2b2 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.be.pC->pCurso
1e2b3 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r) );.          
1e2b4 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1e2b5 65 4b 65 79 53 69 7a 65 28 75 2e 62 65 2e 70 43  eKeySize(u.be.pC
1e2b6 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 65  ->pCursor, &u.be
1e2b7 2e 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  .v);.          a
1e2b8 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1e2b9 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 43 61 6e  E_OK );   /* Can
1e2ba 6e 6f 74 20 66 61 69 6c 20 66 6f 6c 6c 6f 77 69  not fail followi
1e2bb 6e 67 20 42 74 72 65 65 4c 61 73 74 28 29 20 2a  ng BtreeLast() *
1e2bc 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
1e2bd 75 2e 62 65 2e 76 3d 3d 4d 41 58 5f 52 4f 57 49  u.be.v==MAX_ROWI
1e2be 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  D ){.           
1e2bf 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e   u.be.pC->useRan
1e2c0 64 6f 6d 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20  domRowid = 1;.  
1e2c1 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1e2c2 20 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e             u.be.
1e2c3 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  v++;.          }
1e2c4 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1e2c5 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
1e2c6 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
1e2c7 45 4d 45 4e 54 0a 20 20 20 20 20 20 69 66 28 20  EMENT.      if( 
1e2c8 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20  pOp->p3 ){.     
1e2c9 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61     /* Assert tha
1e2ca 74 20 50 33 20 69 73 20 61 20 76 61 6c 69 64 20  t P3 is a valid 
1e2cb 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a  memory cell. */.
1e2cc 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1e2cd 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20  pOp->p3>0 );.   
1e2ce 20 20 20 20 20 69 66 28 20 70 2d 3e 70 46 72 61       if( p->pFra
1e2cf 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  me ){.          
1e2d0 66 6f 72 28 75 2e 62 65 2e 70 46 72 61 6d 65 3d  for(u.be.pFrame=
1e2d1 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 62 65 2e  p->pFrame; u.be.
1e2d2 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b  pFrame->pParent;
1e2d3 20 75 2e 62 65 2e 70 46 72 61 6d 65 3d 75 2e 62   u.be.pFrame=u.b
1e2d4 65 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e  e.pFrame->pParen
1e2d5 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  t);.          /*
1e2d6 20 41 73 73 65 72 74 20 74 68 61 74 20 50 33 20   Assert that P3 
1e2d7 69 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72  is a valid memor
1e2d8 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20  y cell. */.     
1e2d9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1e2da 2d 3e 70 33 3c 3d 75 2e 62 65 2e 70 46 72 61 6d  ->p3<=u.be.pFram
1e2db 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  e->nMem );.     
1e2dc 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d       u.be.pMem =
1e2dd 20 26 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 61   &u.be.pFrame->a
1e2de 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
1e2df 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1e2e0 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65 72 74         /* Assert
1e2e1 20 74 68 61 74 20 50 33 20 69 73 20 61 20 76 61   that P3 is a va
1e2e2 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e  lid memory cell.
1e2e3 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73   */.          as
1e2e4 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70  sert( pOp->p3<=p
1e2e5 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20  ->nMem );.      
1e2e6 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d 20      u.be.pMem = 
1e2e7 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
1e2e8 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  ];.        }..  
1e2e9 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54        REGISTER_T
1e2ea 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e  RACE(pOp->p3, u.
1e2eb 62 65 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20  be.pMem);.      
1e2ec 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1e2ed 49 6e 74 65 67 65 72 69 66 79 28 75 2e 62 65 2e  Integerify(u.be.
1e2ee 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61  pMem);.        a
1e2ef 73 73 65 72 74 28 20 28 75 2e 62 65 2e 70 4d 65  ssert( (u.be.pMe
1e2f0 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  m->flags & MEM_I
1e2f1 6e 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65  nt)!=0 );  /* me
1e2f2 6d 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 69  m(P3) holds an i
1e2f3 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
1e2f4 20 20 69 66 28 20 75 2e 62 65 2e 70 4d 65 6d 2d    if( u.be.pMem-
1e2f5 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  >u.i==MAX_ROWID 
1e2f6 7c 7c 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52  || u.be.pC->useR
1e2f7 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20  andomRowid ){.  
1e2f8 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1e2f9 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20  ITE_FULL;.      
1e2fa 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1e2fb 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1e2fc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1e2fd 66 28 20 75 2e 62 65 2e 76 3c 75 2e 62 65 2e 70  f( u.be.v<u.be.p
1e2fe 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20  Mem->u.i+1 ){.  
1e2ff 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d          u.be.v =
1e300 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20   u.be.pMem->u.i 
1e301 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  + 1;.        }. 
1e302 20 20 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d         u.be.pMem
1e303 2d 3e 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a  ->u.i = u.be.v;.
1e304 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a        }.#endif..
1e305 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1e306 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64  eeSetCachedRowid
1e307 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.be.pC->pCurso
1e308 72 2c 20 75 2e 62 65 2e 76 3c 4d 41 58 5f 52 4f  r, u.be.v<MAX_RO
1e309 57 49 44 20 3f 20 75 2e 62 65 2e 76 2b 31 20 3a  WID ? u.be.v+1 :
1e30a 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   0);.    }.    i
1e30b 66 28 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52  f( u.be.pC->useR
1e30c 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20  andomRowid ){.  
1e30d 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1e30e 3e 70 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 57 65  >p3==0 );  /* We
1e30f 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e 20 72 61   cannot be in ra
1e310 6e 64 6f 6d 20 72 6f 77 69 64 20 6d 6f 64 65 20  ndom rowid mode 
1e311 69 66 20 74 68 69 73 20 69 73 0a 20 20 20 20 20  if this is.     
1e312 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e313 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 41 55          ** an AU
1e314 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c  TOINCREMENT tabl
1e315 65 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 62 65  e. */.      u.be
1e316 2e 76 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77  .v = db->lastRow
1e317 69 64 3b 0a 20 20 20 20 20 20 75 2e 62 65 2e 63  id;.      u.be.c
1e318 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f  nt = 0;.      do
1e319 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  {.        if( u.
1e31a 62 65 2e 63 6e 74 3d 3d 30 20 26 26 20 28 75 2e  be.cnt==0 && (u.
1e31b 62 65 2e 76 26 30 78 66 66 66 66 66 66 29 3d 3d  be.v&0xffffff)==
1e31c 75 2e 62 65 2e 76 20 29 7b 0a 20 20 20 20 20 20  u.be.v ){.      
1e31d 20 20 20 20 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20      u.be.v++;.  
1e31e 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1e31f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
1e320 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66  andomness(sizeof
1e321 28 75 2e 62 65 2e 76 29 2c 20 26 75 2e 62 65 2e  (u.be.v), &u.be.
1e322 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  v);.          if
1e323 28 20 75 2e 62 65 2e 63 6e 74 3c 35 20 29 20 75  ( u.be.cnt<5 ) u
1e324 2e 62 65 2e 76 20 26 3d 20 30 78 66 66 66 66 66  .be.v &= 0xfffff
1e325 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  f;.        }.   
1e326 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1e327 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61  3BtreeMovetoUnpa
1e328 63 6b 65 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43  cked(u.be.pC->pC
1e329 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75  ursor, 0, (u64)u
1e32a 2e 62 65 2e 76 2c 20 30 2c 20 26 75 2e 62 65 2e  .be.v, 0, &u.be.
1e32b 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 75 2e  res);.        u.
1e32c 62 65 2e 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20  be.cnt++;.      
1e32d 7d 77 68 69 6c 65 28 20 75 2e 62 65 2e 63 6e 74  }while( u.be.cnt
1e32e 3c 31 30 30 20 26 26 20 72 63 3d 3d 53 51 4c 49  <100 && rc==SQLI
1e32f 54 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65  TE_OK && u.be.re
1e330 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  s==0 );.      if
1e331 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1e332 26 26 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 29  && u.be.res==0 )
1e333 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
1e334 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20  QLITE_FULL;.    
1e335 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1e336 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1e337 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75     }.    }.    u
1e338 2e 62 65 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .be.pC->rowidIsV
1e339 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e  alid = 0;.    u.
1e33a 62 65 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  be.pC->deferredM
1e33b 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75  oveto = 0;.    u
1e33c 2e 62 65 2e 70 43 2d 3e 63 61 63 68 65 53 74 61  .be.pC->cacheSta
1e33d 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
1e33e 45 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54  E;.  }.  MemSetT
1e33f 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1e340 4d 5f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e  M_Int);.  pOut->
1e341 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20  u.i = u.be.v;.  
1e342 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1e343 6f 64 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50  ode: Insert P1 P
1e344 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
1e345 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20   Write an entry 
1e346 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f  into the table o
1e347 66 20 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20  f cursor P1.  A 
1e348 6e 65 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20  new entry is.** 
1e349 63 72 65 61 74 65 64 20 69 66 20 69 74 20 64 6f  created if it do
1e34a 65 73 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78  esn't already ex
1e34b 69 73 74 20 6f 72 20 74 68 65 20 64 61 74 61 20  ist or the data 
1e34c 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  for an existing.
1e34d 2a 2a 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72  ** entry is over
1e34e 77 72 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61  written.  The da
1e34f 74 61 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  ta is the value 
1e350 4d 45 4d 5f 42 6c 6f 62 20 73 74 6f 72 65 64 20  MEM_Blob stored 
1e351 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6e  in register.** n
1e352 75 6d 62 65 72 20 50 32 2e 20 54 68 65 20 6b 65  umber P2. The ke
1e353 79 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72  y is stored in r
1e354 65 67 69 73 74 65 72 20 50 33 2e 20 54 68 65 20  egister P3. The 
1e355 6b 65 79 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61  key must.** be a
1e356 20 4d 45 4d 5f 49 6e 74 2e 0a 2a 2a 0a 2a 2a 20   MEM_Int..**.** 
1e357 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43  If the OPFLAG_NC
1e358 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 35  HANGE flag of P5
1e359 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68   is set, then th
1e35a 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75  e row change cou
1e35b 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65  nt is.** increme
1e35c 6e 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20  nted (otherwise 
1e35d 6e 6f 74 29 2e 20 20 49 66 20 74 68 65 20 4f 50  not).  If the OP
1e35e 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 66  FLAG_LASTROWID f
1e35f 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74  lag of P5 is set
1e360 2c 0a 2a 2a 20 74 68 65 6e 20 72 6f 77 69 64 20  ,.** then rowid 
1e361 69 73 20 73 74 6f 72 65 64 20 66 6f 72 20 73 75  is stored for su
1e362 62 73 65 71 75 65 6e 74 20 72 65 74 75 72 6e 20  bsequent return 
1e363 62 79 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  by the.** sqlite
1e364 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
1e365 77 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 28  wid() function (
1e366 6f 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20  otherwise it is 
1e367 75 6e 6d 6f 64 69 66 69 65 64 29 2e 0a 2a 2a 0a  unmodified)..**.
1e368 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47  ** If the OPFLAG
1e369 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66  _USESEEKRESULT f
1e36a 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74  lag of P5 is set
1e36b 20 61 6e 64 20 69 66 20 74 68 65 20 72 65 73 75   and if the resu
1e36c 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 61 73  lt of.** the las
1e36d 74 20 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e  t seek operation
1e36e 20 28 4f 50 5f 4e 6f 74 45 78 69 73 74 73 29 20   (OP_NotExists) 
1e36f 77 61 73 20 61 20 73 75 63 63 65 73 73 2c 20 74  was a success, t
1e370 68 65 6e 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72  hen this.** oper
1e371 61 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 20 61  ation will not a
1e372 74 74 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74  ttempt to find t
1e373 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72  he appropriate r
1e374 6f 77 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 0a  ow before doing.
1e375 2a 2a 20 74 68 65 20 69 6e 73 65 72 74 20 62 75  ** the insert bu
1e376 74 20 77 69 6c 6c 20 69 6e 73 74 65 61 64 20 6f  t will instead o
1e377 76 65 72 77 72 69 74 65 20 74 68 65 20 72 6f 77  verwrite the row
1e378 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72   that the cursor
1e379 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79   is.** currently
1e37a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 50   pointing to.  P
1e37b 72 65 73 75 6d 61 62 6c 79 2c 20 74 68 65 20 70  resumably, the p
1e37c 72 69 6f 72 20 4f 50 5f 4e 6f 74 45 78 69 73 74  rior OP_NotExist
1e37d 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 68 61 73 20  s opcode.** has 
1e37e 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e  already position
1e37f 65 64 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f  ed the cursor co
1e380 72 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 69  rrectly.  This i
1e381 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
1e382 6e 0a 2a 2a 20 74 68 61 74 20 62 6f 6f 73 74 73  n.** that boosts
1e383 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 79 20   performance by 
1e384 61 76 6f 69 64 69 6e 67 20 72 65 64 75 6e 64 61  avoiding redunda
1e385 6e 74 20 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20  nt seeks..**.** 
1e386 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 49 53  If the OPFLAG_IS
1e387 55 50 44 41 54 45 20 66 6c 61 67 20 69 73 20 73  UPDATE flag is s
1e388 65 74 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70  et, then this op
1e389 63 6f 64 65 20 69 73 20 70 61 72 74 20 6f 66 20  code is part of 
1e38a 61 6e 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65  an.** UPDATE ope
1e38b 72 61 74 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69  ration.  Otherwi
1e38c 73 65 20 28 69 66 20 74 68 65 20 66 6c 61 67 20  se (if the flag 
1e38d 69 73 20 63 6c 65 61 72 29 20 74 68 65 6e 20 74  is clear) then t
1e38e 68 69 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73  his opcode.** is
1e38f 20 70 61 72 74 20 6f 66 20 61 6e 20 49 4e 53 45   part of an INSE
1e390 52 54 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54  RT operation.  T
1e391 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 69 73  he difference is
1e392 20 6f 6e 6c 79 20 69 6d 70 6f 72 74 61 6e 74 20   only important 
1e393 74 6f 0a 2a 2a 20 74 68 65 20 75 70 64 61 74 65  to.** the update
1e394 20 68 6f 6f 6b 2e 0a 2a 2a 0a 2a 2a 20 50 61 72   hook..**.** Par
1e395 61 6d 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f  ameter P4 may po
1e396 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20  int to a string 
1e397 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
1e398 61 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a  able-name, or.**
1e399 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66   may be NULL. If
1e39a 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   it is not NULL,
1e39b 20 74 68 65 6e 20 74 68 65 20 75 70 64 61 74 65   then the update
1e39c 2d 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74  -hook .** (sqlit
1e39d 65 33 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61  e3.xUpdateCallba
1e39e 63 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66  ck) is invoked f
1e39f 6f 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65  ollowing a succe
1e3a0 73 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a  ssful insert..**
1e3a1 0a 2a 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44  .** (WARNING/TOD
1e3a2 4f 3a 20 49 66 20 50 31 20 69 73 20 61 20 70 73  O: If P1 is a ps
1e3a3 65 75 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20  eudo-cursor and 
1e3a4 50 32 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c  P2 is dynamicall
1e3a5 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20  y.** allocated, 
1e3a6 74 68 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f  then ownership o
1e3a7 66 20 50 32 20 69 73 20 74 72 61 6e 73 66 65 72  f P2 is transfer
1e3a8 72 65 64 20 74 6f 20 74 68 65 20 70 73 65 75 64  red to the pseud
1e3a9 6f 2d 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20  o-cursor.** and 
1e3aa 72 65 67 69 73 74 65 72 20 50 32 20 62 65 63 6f  register P2 beco
1e3ab 6d 65 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20  mes ephemeral.  
1e3ac 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  If the cursor is
1e3ad 20 63 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a   changed, the.**
1e3ae 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74   value of regist
1e3af 65 72 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20  er P2 will then 
1e3b0 63 68 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75  change.  Make su
1e3b1 72 65 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74  re this does not
1e3b2 0a 2a 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72  .** cause any pr
1e3b3 6f 62 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54  oblems.).**.** T
1e3b4 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1e3b5 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61  only works on ta
1e3b6 62 6c 65 73 2e 20 20 54 68 65 20 65 71 75 69 76  bles.  The equiv
1e3b7 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f  alent instructio
1e3b8 6e 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73  n.** for indices
1e3b9 20 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74   is OP_IdxInsert
1e3ba 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1e3bb 49 6e 73 65 72 74 49 6e 74 20 50 31 20 50 32 20  InsertInt P1 P2 
1e3bc 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54  P3 P4 P5.**.** T
1e3bd 68 69 73 20 77 6f 72 6b 73 20 65 78 61 63 74 6c  his works exactl
1e3be 79 20 6c 69 6b 65 20 4f 50 5f 49 6e 73 65 72 74  y like OP_Insert
1e3bf 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
1e3c0 20 6b 65 79 20 69 73 20 74 68 65 0a 2a 2a 20 69   key is the.** i
1e3c1 6e 74 65 67 65 72 20 76 61 6c 75 65 20 50 33 2c  nteger value P3,
1e3c2 20 6e 6f 74 20 74 68 65 20 76 61 6c 75 65 20 6f   not the value o
1e3c3 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 73 74  f the integer st
1e3c4 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
1e3c5 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P3..*/.case OP_
1e3c6 49 6e 73 65 72 74 3a 20 0a 63 61 73 65 20 4f 50  Insert: .case OP
1e3c7 5f 49 6e 73 65 72 74 49 6e 74 3a 20 7b 0a 23 69  _InsertInt: {.#i
1e3c8 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1e3c9 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e3ca 74 6f 20 75 2e 62 66 20 2a 2f 0a 20 20 4d 65 6d  to u.bf */.  Mem
1e3cb 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f   *pData;       /
1e3cc 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69  * MEM cell holdi
1e3cd 6e 67 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  ng data for the 
1e3ce 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73  record to be ins
1e3cf 65 72 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a  erted */.  Mem *
1e3d0 70 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20  pKey;        /* 
1e3d1 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67  MEM cell holding
1e3d2 20 6b 65 79 20 20 66 6f 72 20 74 68 65 20 72 65   key  for the re
1e3d3 63 6f 72 64 20 2a 2f 0a 20 20 69 36 34 20 69 4b  cord */.  i64 iK
1e3d4 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ey;         /* T
1e3d5 68 65 20 69 6e 74 65 67 65 72 20 52 4f 57 49 44  he integer ROWID
1e3d6 20 6f 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20   or key for the 
1e3d7 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73  record to be ins
1e3d8 65 72 74 65 64 20 2a 2f 0a 20 20 56 64 62 65 43  erted */.  VdbeC
1e3d9 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 2f 2a 20  ursor *pC;   /* 
1e3da 43 75 72 73 6f 72 20 74 6f 20 74 61 62 6c 65 20  Cursor to table 
1e3db 69 6e 74 6f 20 77 68 69 63 68 20 69 6e 73 65 72  into which inser
1e3dc 74 20 69 73 20 77 72 69 74 74 65 6e 20 2a 2f 0a  t is written */.
1e3dd 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20    int nZero;    
1e3de 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1e3df 20 7a 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61   zero-bytes to a
1e3e0 70 70 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73  ppend */.  int s
1e3e1 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20  eekResult;   /* 
1e3e2 52 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20  Result of prior 
1e3e3 73 65 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f 20  seek or 0 if no 
1e3e4 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c  USESEEKRESULT fl
1e3e5 61 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ag */.  const ch
1e3e6 61 72 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74  ar *zDb;  /* dat
1e3e7 61 62 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65  abase name - use
1e3e8 64 20 62 79 20 74 68 65 20 75 70 64 61 74 65 20  d by the update 
1e3e9 68 6f 6f 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  hook */.  const 
1e3ea 63 68 61 72 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54  char *zTbl; /* T
1e3eb 61 62 6c 65 20 6e 61 6d 65 20 2d 20 75 73 65 64  able name - used
1e3ec 20 62 79 20 74 68 65 20 6f 70 64 61 74 65 20 68   by the opdate h
1e3ed 6f 6f 6b 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b  ook */.  int op;
1e3ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
1e3ef 63 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20  code for update 
1e3f0 68 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44  hook: SQLITE_UPD
1e3f1 41 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e  ATE or SQLITE_IN
1e3f2 53 45 52 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  SERT */.#endif /
1e3f3 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e3f4 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e3f5 66 20 2a 2f 0a 0a 20 20 75 2e 62 66 2e 70 44 61  f */..  u.bf.pDa
1e3f6 74 61 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ta = &p->aMem[pO
1e3f7 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74  p->p2];.  assert
1e3f8 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1e3f9 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1e3fa 6f 72 20 29 3b 0a 20 20 75 2e 62 66 2e 70 43 20  or );.  u.bf.pC 
1e3fb 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1e3fc 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e3fd 2e 62 66 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61  .bf.pC!=0 );.  a
1e3fe 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e  ssert( u.bf.pC->
1e3ff 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20  pCursor!=0 );.  
1e400 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d  assert( u.bf.pC-
1e401 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d  >pseudoTableReg=
1e402 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1e403 75 2e 62 66 2e 70 43 2d 3e 69 73 54 61 62 6c 65  u.bf.pC->isTable
1e404 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54   );.  REGISTER_T
1e405 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 75 2e  RACE(pOp->p2, u.
1e406 62 66 2e 70 44 61 74 61 29 3b 0a 0a 20 20 69 66  bf.pData);..  if
1e407 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
1e408 50 5f 49 6e 73 65 72 74 20 29 7b 0a 20 20 20 20  P_Insert ){.    
1e409 75 2e 62 66 2e 70 4b 65 79 20 3d 20 26 70 2d 3e  u.bf.pKey = &p->
1e40a 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1e40b 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e     assert( u.bf.
1e40c 70 4b 65 79 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pKey->flags & ME
1e40d 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 52 45 47  M_Int );.    REG
1e40e 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1e40f 3e 70 33 2c 20 75 2e 62 66 2e 70 4b 65 79 29 3b  >p3, u.bf.pKey);
1e410 0a 20 20 20 20 75 2e 62 66 2e 69 4b 65 79 20 3d  .    u.bf.iKey =
1e411 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 2e 69 3b   u.bf.pKey->u.i;
1e412 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
1e413 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  sert( pOp->opcod
1e414 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 49 6e 74 20  e==OP_InsertInt 
1e415 29 3b 0a 20 20 20 20 75 2e 62 66 2e 69 4b 65 79  );.    u.bf.iKey
1e416 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d 0a   = pOp->p3;.  }.
1e417 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26  .  if( pOp->p5 &
1e418 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20   OPFLAG_NCHANGE 
1e419 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a  ) p->nChange++;.
1e41a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20    if( pOp->p5 & 
1e41b 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44  OPFLAG_LASTROWID
1e41c 20 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64   ) db->lastRowid
1e41d 20 3d 20 75 2e 62 66 2e 69 4b 65 79 3b 0a 20 20   = u.bf.iKey;.  
1e41e 69 66 28 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e  if( u.bf.pData->
1e41f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1e420 20 29 7b 0a 20 20 20 20 75 2e 62 66 2e 70 44 61   ){.    u.bf.pDa
1e421 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 75  ta->z = 0;.    u
1e422 2e 62 66 2e 70 44 61 74 61 2d 3e 6e 20 3d 20 30  .bf.pData->n = 0
1e423 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
1e424 73 73 65 72 74 28 20 75 2e 62 66 2e 70 44 61 74  ssert( u.bf.pDat
1e425 61 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  a->flags & (MEM_
1e426 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 3b  Blob|MEM_Str) );
1e427 0a 20 20 7d 0a 20 20 75 2e 62 66 2e 73 65 65 6b  .  }.  u.bf.seek
1e428 52 65 73 75 6c 74 20 3d 20 28 28 70 4f 70 2d 3e  Result = ((pOp->
1e429 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45 53  p5 & OPFLAG_USES
1e42a 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 75 2e 62  EEKRESULT) ? u.b
1e42b 66 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74  f.pC->seekResult
1e42c 20 3a 20 30 29 3b 0a 20 20 69 66 28 20 75 2e 62   : 0);.  if( u.b
1e42d 66 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26  f.pData->flags &
1e42e 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
1e42f 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d 20 75 2e   u.bf.nZero = u.
1e430 62 66 2e 70 44 61 74 61 2d 3e 75 2e 6e 5a 65 72  bf.pData->u.nZer
1e431 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  o;.  }else{.    
1e432 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a  u.bf.nZero = 0;.
1e433 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
1e434 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64  eeSetCachedRowid
1e435 28 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.bf.pC->pCurso
1e436 72 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71  r, 0);.  rc = sq
1e437 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74  lite3BtreeInsert
1e438 28 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.bf.pC->pCurso
1e439 72 2c 20 30 2c 20 75 2e 62 66 2e 69 4b 65 79 2c  r, 0, u.bf.iKey,
1e43a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1e43b 20 20 20 20 20 20 20 20 20 20 20 75 2e 62 66 2e             u.bf.
1e43c 70 44 61 74 61 2d 3e 7a 2c 20 75 2e 62 66 2e 70  pData->z, u.bf.p
1e43d 44 61 74 61 2d 3e 6e 2c 20 75 2e 62 66 2e 6e 5a  Data->n, u.bf.nZ
1e43e 65 72 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ero,.           
1e43f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1e440 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
1e441 41 50 50 45 4e 44 2c 20 75 2e 62 66 2e 73 65 65  APPEND, u.bf.see
1e442 6b 52 65 73 75 6c 74 0a 20 20 29 3b 0a 20 20 75  kResult.  );.  u
1e443 2e 62 66 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .bf.pC->rowidIsV
1e444 61 6c 69 64 20 3d 20 30 3b 0a 20 20 75 2e 62 66  alid = 0;.  u.bf
1e445 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
1e446 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e 62 66 2e  eto = 0;.  u.bf.
1e447 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1e448 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a  = CACHE_STALE;..
1e449 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
1e44a 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72  update-hook if r
1e44b 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66  equired. */.  if
1e44c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1e44d 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61  && db->xUpdateCa
1e44e 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70  llback && pOp->p
1e44f 34 2e 7a 20 29 7b 0a 20 20 20 20 75 2e 62 66 2e  4.z ){.    u.bf.
1e450 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 75 2e  zDb = db->aDb[u.
1e451 62 66 2e 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d  bf.pC->iDb].zNam
1e452 65 3b 0a 20 20 20 20 75 2e 62 66 2e 7a 54 62 6c  e;.    u.bf.zTbl
1e453 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20   = pOp->p4.z;.  
1e454 20 20 75 2e 62 66 2e 6f 70 20 3d 20 28 28 70 4f    u.bf.op = ((pO
1e455 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 49  p->p5 & OPFLAG_I
1e456 53 55 50 44 41 54 45 29 20 3f 20 53 51 4c 49 54  SUPDATE) ? SQLIT
1e457 45 5f 55 50 44 41 54 45 20 3a 20 53 51 4c 49 54  E_UPDATE : SQLIT
1e458 45 5f 49 4e 53 45 52 54 29 3b 0a 20 20 20 20 61  E_INSERT);.    a
1e459 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e  ssert( u.bf.pC->
1e45a 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 64  isTable );.    d
1e45b 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61  b->xUpdateCallba
1e45c 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 72  ck(db->pUpdateAr
1e45d 67 2c 20 75 2e 62 66 2e 6f 70 2c 20 75 2e 62 66  g, u.bf.op, u.bf
1e45e 2e 7a 44 62 2c 20 75 2e 62 66 2e 7a 54 62 6c 2c  .zDb, u.bf.zTbl,
1e45f 20 75 2e 62 66 2e 69 4b 65 79 29 3b 0a 20 20 20   u.bf.iKey);.   
1e460 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43   assert( u.bf.pC
1e461 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a  ->iDb>=0 );.  }.
1e462 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e463 70 63 6f 64 65 3a 20 44 65 6c 65 74 65 20 50 31  pcode: Delete P1
1e464 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a   P2 * P4 *.**.**
1e465 20 44 65 6c 65 74 65 20 74 68 65 20 72 65 63 6f   Delete the reco
1e466 72 64 20 61 74 20 77 68 69 63 68 20 74 68 65 20  rd at which the 
1e467 50 31 20 63 75 72 73 6f 72 20 69 73 20 63 75 72  P1 cursor is cur
1e468 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e  rently pointing.
1e469 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f  .**.** The curso
1e46a 72 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 70  r will be left p
1e46b 6f 69 6e 74 69 6e 67 20 61 74 20 65 69 74 68 65  ointing at eithe
1e46c 72 20 74 68 65 20 6e 65 78 74 20 6f 72 20 74 68  r the next or th
1e46d 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 72 65  e previous.** re
1e46e 63 6f 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c  cord in the tabl
1e46f 65 2e 20 49 66 20 69 74 20 69 73 20 6c 65 66 74  e. If it is left
1e470 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65   pointing at the
1e471 20 6e 65 78 74 20 72 65 63 6f 72 64 2c 20 74 68   next record, th
1e472 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20 4e  en.** the next N
1e473 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
1e474 77 69 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f 70 2e  will be a no-op.
1e475 20 20 48 65 6e 63 65 20 69 74 20 69 73 20 4f 4b    Hence it is OK
1e476 20 74 6f 20 64 65 6c 65 74 65 0a 2a 2a 20 61 20   to delete.** a 
1e477 72 65 63 6f 72 64 20 66 72 6f 6d 20 77 69 74 68  record from with
1e478 69 6e 20 61 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e  in an Next loop.
1e479 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50  .**.** If the OP
1e47a 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61  FLAG_NCHANGE fla
1e47b 67 20 6f 66 20 50 32 20 69 73 20 73 65 74 2c 20  g of P2 is set, 
1e47c 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61  then the row cha
1e47d 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20  nge count is.** 
1e47e 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68  incremented (oth
1e47f 65 72 77 69 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a  erwise not)..**.
1e480 2a 2a 20 50 31 20 6d 75 73 74 20 6e 6f 74 20 62  ** P1 must not b
1e481 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20  e pseudo-table. 
1e482 20 49 74 20 68 61 73 20 74 6f 20 62 65 20 61 20   It has to be a 
1e483 72 65 61 6c 20 74 61 62 6c 65 20 77 69 74 68 0a  real table with.
1e484 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73  ** multiple rows
1e485 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73  ..**.** If P4 is
1e486 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
1e487 69 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  it is the name o
1e488 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
1e489 20 50 31 20 69 73 0a 2a 2a 20 70 6f 69 6e 74 69   P1 is.** pointi
1e48a 6e 67 20 74 6f 2e 20 20 54 68 65 20 75 70 64 61  ng to.  The upda
1e48b 74 65 20 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20  te hook will be 
1e48c 69 6e 76 6f 6b 65 64 2c 20 69 66 20 69 74 20 65  invoked, if it e
1e48d 78 69 73 74 73 2e 0a 2a 2a 20 49 66 20 50 34 20  xists..** If P4 
1e48e 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e  is not NULL then
1e48f 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d   the P1 cursor m
1e490 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 70 6f  ust have been po
1e491 73 69 74 69 6f 6e 65 64 0a 2a 2a 20 75 73 69 6e  sitioned.** usin
1e492 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 70 72  g OP_NotFound pr
1e493 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20  ior to invoking 
1e494 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a  this opcode..*/.
1e495 63 61 73 65 20 4f 50 5f 44 65 6c 65 74 65 3a 20  case OP_Delete: 
1e496 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1e497 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e498 64 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a 20  d into u.bg */. 
1e499 20 69 36 34 20 69 4b 65 79 3b 0a 20 20 56 64 62   i64 iKey;.  Vdb
1e49a 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e  eCursor *pC;.#en
1e49b 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e49c 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e49d 6f 20 75 2e 62 67 20 2a 2f 0a 0a 20 20 75 2e 62  o u.bg */..  u.b
1e49e 67 2e 69 4b 65 79 20 3d 20 30 3b 0a 20 20 61 73  g.iKey = 0;.  as
1e49f 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e4a0 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e4a1 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 67  Cursor );.  u.bg
1e4a2 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1e4a3 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1e4a4 74 28 20 75 2e 62 67 2e 70 43 21 3d 30 20 29 3b  t( u.bg.pC!=0 );
1e4a5 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e  .  assert( u.bg.
1e4a6 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29  pC->pCursor!=0 )
1e4a7 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c 69 64  ;  /* Only valid
1e4a8 20 66 6f 72 20 72 65 61 6c 20 74 61 62 6c 65 73   for real tables
1e4a9 2c 20 6e 6f 20 70 73 65 75 64 6f 74 61 62 6c 65  , no pseudotable
1e4aa 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68  s */..  /* If th
1e4ab 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 77 69  e update-hook wi
1e4ac 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 73  ll be invoked, s
1e4ad 65 74 20 75 2e 62 67 2e 69 4b 65 79 20 74 6f 20  et u.bg.iKey to 
1e4ae 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
1e4af 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e 67 20  .  ** row being 
1e4b0 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  deleted..  */.  
1e4b1 69 66 28 20 64 62 2d 3e 78 55 70 64 61 74 65 43  if( db->xUpdateC
1e4b2 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e  allback && pOp->
1e4b3 70 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73 73 65  p4.z ){.    asse
1e4b4 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 69 73 54  rt( u.bg.pC->isT
1e4b5 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65  able );.    asse
1e4b6 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 72 6f 77  rt( u.bg.pC->row
1e4b7 69 64 49 73 56 61 6c 69 64 20 29 3b 20 20 2f 2a  idIsValid );  /*
1e4b8 20 6c 61 73 74 52 6f 77 69 64 20 73 65 74 20 62   lastRowid set b
1e4b9 79 20 70 72 65 76 69 6f 75 73 20 4f 50 5f 4e 6f  y previous OP_No
1e4ba 74 46 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 75 2e  tFound */.    u.
1e4bb 62 67 2e 69 4b 65 79 20 3d 20 75 2e 62 67 2e 70  bg.iKey = u.bg.p
1e4bc 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20  C->lastRowid;.  
1e4bd 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 4f 50 5f 44  }..  /* The OP_D
1e4be 65 6c 65 74 65 20 6f 70 63 6f 64 65 20 61 6c 77  elete opcode alw
1e4bf 61 79 73 20 66 6f 6c 6c 6f 77 73 20 61 6e 20 4f  ays follows an O
1e4c0 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 20 4f  P_NotExists or O
1e4c1 50 5f 4c 61 73 74 20 6f 72 0a 20 20 2a 2a 20 4f  P_Last or.  ** O
1e4c2 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65 20  P_Column on the 
1e4c3 73 61 6d 65 20 74 61 62 6c 65 20 77 69 74 68 6f  same table witho
1e4c4 75 74 20 61 6e 79 20 69 6e 74 65 72 76 65 6e 69  ut any interveni
1e4c5 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68  ng operations th
1e4c6 61 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 6d 6f  at.  ** might mo
1e4c7 76 65 20 6f 72 20 69 6e 76 61 6c 69 64 61 74 65  ve or invalidate
1e4c8 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 48 65   the cursor.  He
1e4c9 6e 63 65 20 63 75 72 73 6f 72 20 75 2e 62 67 2e  nce cursor u.bg.
1e4ca 70 43 20 69 73 20 61 6c 77 61 79 73 20 70 6f 69  pC is always poi
1e4cb 6e 74 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 74 68  nting.  ** to th
1e4cc 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65  e row to be dele
1e4cd 74 65 64 20 61 6e 64 20 74 68 65 20 73 71 6c 69  ted and the sqli
1e4ce 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
1e4cf 65 74 6f 28 29 20 6f 70 65 72 61 74 69 6f 6e 0a  eto() operation.
1e4d0 20 20 2a 2a 20 62 65 6c 6f 77 20 69 73 20 61 6c    ** below is al
1e4d1 77 61 79 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64  ways a no-op and
1e4d2 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 57   cannot fail.  W
1e4d3 65 20 77 69 6c 6c 20 72 75 6e 20 69 74 20 61 6e  e will run it an
1e4d4 79 68 6f 77 2c 20 74 68 6f 75 67 68 2c 0a 20 20  yhow, though,.  
1e4d5 2a 2a 20 74 6f 20 67 75 61 72 64 20 61 67 61 69  ** to guard agai
1e4d6 6e 73 74 20 66 75 74 75 72 65 20 63 68 61 6e 67  nst future chang
1e4d7 65 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67  es to the code g
1e4d8 65 6e 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 2f 0a  enerator..  **/.
1e4d9 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70    assert( u.bg.p
1e4da 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1e4db 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73  o==0 );.  rc = s
1e4dc 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
1e4dd 4d 6f 76 65 74 6f 28 75 2e 62 67 2e 70 43 29 3b  Moveto(u.bg.pC);
1e4de 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21  .  if( NEVER(rc!
1e4df 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67 6f  =SQLITE_OK) ) go
1e4e0 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1e4e1 65 72 72 6f 72 3b 0a 0a 20 20 73 71 6c 69 74 65  error;..  sqlite
1e4e2 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52  3BtreeSetCachedR
1e4e3 6f 77 69 64 28 75 2e 62 67 2e 70 43 2d 3e 70 43  owid(u.bg.pC->pC
1e4e4 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20  ursor, 0);.  rc 
1e4e5 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  = sqlite3BtreeDe
1e4e6 6c 65 74 65 28 75 2e 62 67 2e 70 43 2d 3e 70 43  lete(u.bg.pC->pC
1e4e7 75 72 73 6f 72 29 3b 0a 20 20 75 2e 62 67 2e 70  ursor);.  u.bg.p
1e4e8 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1e4e9 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20   CACHE_STALE;.. 
1e4ea 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75   /* Invoke the u
1e4eb 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65  pdate-hook if re
1e4ec 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28  quired. */.  if(
1e4ed 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1e4ee 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c  & db->xUpdateCal
1e4ef 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34  lback && pOp->p4
1e4f0 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  .z ){.    const 
1e4f1 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e  char *zDb = db->
1e4f2 61 44 62 5b 75 2e 62 67 2e 70 43 2d 3e 69 44 62  aDb[u.bg.pC->iDb
1e4f3 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e  ].zName;.    con
1e4f4 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20  st char *zTbl = 
1e4f5 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 64  pOp->p4.z;.    d
1e4f6 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61  b->xUpdateCallba
1e4f7 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 72  ck(db->pUpdateAr
1e4f8 67 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  g, SQLITE_DELETE
1e4f9 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 75 2e 62  , zDb, zTbl, u.b
1e4fa 67 2e 69 4b 65 79 29 3b 0a 20 20 20 20 61 73 73  g.iKey);.    ass
1e4fb 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 69 44  ert( u.bg.pC->iD
1e4fc 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66  b>=0 );.  }.  if
1e4fd 28 20 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c  ( pOp->p2 & OPFL
1e4fe 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e  AG_NCHANGE ) p->
1e4ff 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 62 72 65  nChange++;.  bre
1e500 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ak;.}./* Opcode:
1e501 20 52 65 73 65 74 43 6f 75 6e 74 20 2a 20 2a 20   ResetCount * * 
1e502 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  * * *.**.** The 
1e503 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 68 61  value of the cha
1e504 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 63  nge counter is c
1e505 6f 70 69 65 64 20 74 6f 20 74 68 65 20 64 61 74  opied to the dat
1e506 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20  abase handle.** 
1e507 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 28  change counter (
1e508 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62 73  returned by subs
1e509 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20  equent calls to 
1e50a 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
1e50b 29 29 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20  ))..** Then the 
1e50c 56 4d 73 20 69 6e 74 65 72 6e 61 6c 20 63 68 61  VMs internal cha
1e50d 6e 67 65 20 63 6f 75 6e 74 65 72 20 72 65 73 65  nge counter rese
1e50e 74 73 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 69 73  ts to 0..** This
1e50f 20 69 73 20 75 73 65 64 20 62 79 20 74 72 69 67   is used by trig
1e510 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 0a 2a 2f  ger programs..*/
1e511 0a 63 61 73 65 20 4f 50 5f 52 65 73 65 74 43 6f  .case OP_ResetCo
1e512 75 6e 74 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33  unt: {.  sqlite3
1e513 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64  VdbeSetChanges(d
1e514 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a  b, p->nChange);.
1e515 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30    p->nChange = 0
1e516 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1e517 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44 61 74 61   Opcode: RowData
1e518 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1e519 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65  ** Write into re
1e51a 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63 6f  gister P2 the co
1e51b 6d 70 6c 65 74 65 20 72 6f 77 20 64 61 74 61 20  mplete row data 
1e51c 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a  for cursor P1..*
1e51d 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e  * There is no in
1e51e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20  terpretation of 
1e51f 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 49  the data.  .** I
1e520 74 20 69 73 20 6a 75 73 74 20 63 6f 70 69 65 64  t is just copied
1e521 20 6f 6e 74 6f 20 74 68 65 20 50 32 20 72 65 67   onto the P2 reg
1e522 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 73  ister exactly as
1e523 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64   .** it is found
1e524 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1e525 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   file..**.** If 
1e526 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75  the P1 cursor mu
1e527 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74  st be pointing t
1e528 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e  o a valid row (n
1e529 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a  ot a NULL row).*
1e52a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c  * of a real tabl
1e52b 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d  e, not a pseudo-
1e52c 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63  table..*/./* Opc
1e52d 6f 64 65 3a 20 52 6f 77 4b 65 79 20 50 31 20 50  ode: RowKey P1 P
1e52e 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72  2 * * *.**.** Wr
1e52f 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ite into registe
1e530 72 20 50 32 20 74 68 65 20 63 6f 6d 70 6c 65 74  r P2 the complet
1e531 65 20 72 6f 77 20 6b 65 79 20 66 6f 72 20 63 75  e row key for cu
1e532 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 72  rsor P1..** Ther
1e533 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65  e is no interpre
1e534 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61  tation of the da
1e535 74 61 2e 20 20 0a 2a 2a 20 54 68 65 20 6b 65 79  ta.  .** The key
1e536 20 69 73 20 63 6f 70 69 65 64 20 6f 6e 74 6f 20   is copied onto 
1e537 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72 20  the P3 register 
1e538 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20 69  exactly as .** i
1e539 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68  t is found in th
1e53a 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1e53b 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31  .**.** If the P1
1e53c 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20   cursor must be 
1e53d 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61  pointing to a va
1e53e 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 20 4e  lid row (not a N
1e53f 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20 61  ULL row).** of a
1e540 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74   real table, not
1e541 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e   a pseudo-table.
1e542 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 4b  .*/.case OP_RowK
1e543 65 79 3a 0a 63 61 73 65 20 4f 50 5f 52 6f 77 44  ey:.case OP_RowD
1e544 61 74 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  ata: {.#if 0  /*
1e545 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e546 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 68   moved into u.bh
1e547 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72   */.  VdbeCursor
1e548 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72   *pC;.  BtCursor
1e549 20 2a 70 43 72 73 72 3b 0a 20 20 75 33 32 20 6e   *pCrsr;.  u32 n
1e54a 3b 0a 20 20 69 36 34 20 6e 36 34 3b 0a 23 65 6e  ;.  i64 n64;.#en
1e54b 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e54c 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e54d 6f 20 75 2e 62 68 20 2a 2f 0a 0a 20 20 70 4f 75  o u.bh */..  pOu
1e54e 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  t = &p->aMem[pOp
1e54f 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f 74  ->p2];..  /* Not
1e550 65 20 74 68 61 74 20 52 6f 77 4b 65 79 20 61 6e  e that RowKey an
1e551 64 20 52 6f 77 44 61 74 61 20 61 72 65 20 72 65  d RowData are re
1e552 61 6c 6c 79 20 65 78 61 63 74 6c 79 20 74 68 65  ally exactly the
1e553 20 73 61 6d 65 20 69 6e 73 74 72 75 63 74 69 6f   same instructio
1e554 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  n */.  assert( p
1e555 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1e556 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1e557 29 3b 0a 20 20 75 2e 62 68 2e 70 43 20 3d 20 70  );.  u.bh.pC = p
1e558 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1e559 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68  ;.  assert( u.bh
1e55a 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 7c 7c 20  .pC->isTable || 
1e55b 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
1e55c 52 6f 77 4b 65 79 20 29 3b 0a 20 20 61 73 73 65  RowKey );.  asse
1e55d 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 69 73 49  rt( u.bh.pC->isI
1e55e 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63  ndex || pOp->opc
1e55f 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 20  ode==OP_RowData 
1e560 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  );.  assert( u.b
1e561 68 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73  h.pC!=0 );.  ass
1e562 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 6e 75  ert( u.bh.pC->nu
1e563 6c 6c 52 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73  llRow==0 );.  as
1e564 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70  sert( u.bh.pC->p
1e565 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30  seudoTableReg==0
1e566 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1e567 62 68 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d  bh.pC->pCursor!=
1e568 30 20 29 3b 0a 20 20 75 2e 62 68 2e 70 43 72 73  0 );.  u.bh.pCrs
1e569 72 20 3d 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75  r = u.bh.pC->pCu
1e56a 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20  rsor;.  assert( 
1e56b 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
1e56c 6f 72 49 73 56 61 6c 69 64 28 75 2e 62 68 2e 70  orIsValid(u.bh.p
1e56d 43 72 73 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 54  Crsr) );..  /* T
1e56e 68 65 20 4f 50 5f 52 6f 77 4b 65 79 20 61 6e 64  he OP_RowKey and
1e56f 20 4f 50 5f 52 6f 77 44 61 74 61 20 6f 70 63 6f   OP_RowData opco
1e570 64 65 73 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f  des always follo
1e571 77 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f  w OP_NotExists o
1e572 72 0a 20 20 2a 2a 20 4f 50 5f 52 65 77 69 6e 64  r.  ** OP_Rewind
1e573 2f 4f 70 5f 4e 65 78 74 20 77 69 74 68 20 6e 6f  /Op_Next with no
1e574 20 69 6e 74 65 72 76 65 6e 69 6e 67 20 69 6e 73   intervening ins
1e575 74 72 75 63 74 69 6f 6e 73 20 74 68 61 74 20 6d  tructions that m
1e576 69 67 68 74 20 69 6e 76 61 6c 69 64 61 74 65 0a  ight invalidate.
1e577 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 2e    ** the cursor.
1e578 20 20 48 65 6e 63 65 20 74 68 65 20 66 6f 6c 6c    Hence the foll
1e579 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62  owing sqlite3Vdb
1e57a 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20  eCursorMoveto() 
1e57b 63 61 6c 6c 20 69 73 20 61 6c 77 61 79 73 0a 20  call is always. 
1e57c 20 2a 2a 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20   ** a no-op and 
1e57d 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20  can never fail. 
1e57e 20 42 75 74 20 77 65 20 6c 65 61 76 65 20 69 74   But we leave it
1e57f 20 69 6e 20 70 6c 61 63 65 20 61 73 20 61 20 73   in place as a s
1e580 61 66 65 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73  afety..  */.  as
1e581 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 64  sert( u.bh.pC->d
1e582 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30  eferredMoveto==0
1e583 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
1e584 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
1e585 74 6f 28 75 2e 62 68 2e 70 43 29 3b 0a 20 20 69  to(u.bh.pC);.  i
1e586 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c  f( NEVER(rc!=SQL
1e587 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 61  ITE_OK) ) goto a
1e588 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1e589 72 3b 0a 0a 20 20 69 66 28 20 75 2e 62 68 2e 70  r;..  if( u.bh.p
1e58a 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20  C->isIndex ){.  
1e58b 20 20 61 73 73 65 72 74 28 20 21 75 2e 62 68 2e    assert( !u.bh.
1e58c 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20  pC->isTable );. 
1e58d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1e58e 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 62 68  treeKeySize(u.bh
1e58f 2e 70 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e 36  .pCrsr, &u.bh.n6
1e590 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  4);.    assert( 
1e591 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
1e592 20 20 20 20 2f 2a 20 54 72 75 65 20 62 65 63 61      /* True beca
1e593 75 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f 76  use of CursorMov
1e594 65 74 6f 28 29 20 63 61 6c 6c 20 61 62 6f 76 65  eto() call above
1e595 20 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 62 68   */.    if( u.bh
1e596 2e 6e 36 34 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  .n64>db->aLimit[
1e597 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
1e598 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f  GTH] ){.      go
1e599 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20  to too_big;.    
1e59a 7d 0a 20 20 20 20 75 2e 62 68 2e 6e 20 3d 20 28  }.    u.bh.n = (
1e59b 75 33 32 29 75 2e 62 68 2e 6e 36 34 3b 0a 20 20  u32)u.bh.n64;.  
1e59c 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1e59d 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
1e59e 53 69 7a 65 28 75 2e 62 68 2e 70 43 72 73 72 2c  Size(u.bh.pCrsr,
1e59f 20 26 75 2e 62 68 2e 6e 29 3b 0a 20 20 20 20 61   &u.bh.n);.    a
1e5a0 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1e5a1 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 44 61  E_OK );    /* Da
1e5a2 74 61 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20  taSize() cannot 
1e5a3 66 61 69 6c 20 2a 2f 0a 20 20 20 20 69 66 28 20  fail */.    if( 
1e5a4 75 2e 62 68 2e 6e 3e 28 75 33 32 29 64 62 2d 3e  u.bh.n>(u32)db->
1e5a5 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1e5a6 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
1e5a7 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69       goto too_bi
1e5a8 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  g;.    }.  }.  i
1e5a9 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
1e5aa 6d 47 72 6f 77 28 70 4f 75 74 2c 20 75 2e 62 68  mGrow(pOut, u.bh
1e5ab 2e 6e 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f  .n, 0) ){.    go
1e5ac 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20  to no_mem;.  }. 
1e5ad 20 70 4f 75 74 2d 3e 6e 20 3d 20 75 2e 62 68 2e   pOut->n = u.bh.
1e5ae 6e 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  n;.  MemSetTypeF
1e5af 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 42 6c  lag(pOut, MEM_Bl
1e5b0 6f 62 29 3b 0a 20 20 69 66 28 20 75 2e 62 68 2e  ob);.  if( u.bh.
1e5b1 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20  pC->isIndex ){. 
1e5b2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1e5b3 74 72 65 65 4b 65 79 28 75 2e 62 68 2e 70 43 72  treeKey(u.bh.pCr
1e5b4 73 72 2c 20 30 2c 20 75 2e 62 68 2e 6e 2c 20 70  sr, 0, u.bh.n, p
1e5b5 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 65 6c 73 65  Out->z);.  }else
1e5b6 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1e5b7 65 33 42 74 72 65 65 44 61 74 61 28 75 2e 62 68  e3BtreeData(u.bh
1e5b8 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 68 2e  .pCrsr, 0, u.bh.
1e5b9 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d  n, pOut->z);.  }
1e5ba 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53  .  pOut->enc = S
1e5bb 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20  QLITE_UTF8;  /* 
1e5bc 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62  In case the blob
1e5bd 20 69 73 20 65 76 65 72 20 63 61 73 74 20 74 6f   is ever cast to
1e5be 20 74 65 78 74 20 2a 2f 0a 20 20 55 50 44 41 54   text */.  UPDAT
1e5bf 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
1e5c0 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
1e5c1 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77  ../* Opcode: Row
1e5c2 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  id P1 P2 * * *.*
1e5c3 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72 65  *.** Store in re
1e5c4 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e 74  gister P2 an int
1e5c5 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
1e5c6 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61 62  e key of the tab
1e5c7 6c 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a  le entry that.**
1e5c8 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79   P1 is currently
1e5c9 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2a 0a 2a 2a   point to..**.**
1e5ca 20 50 31 20 63 61 6e 20 62 65 20 65 69 74 68 65   P1 can be eithe
1e5cb 72 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 74 61  r an ordinary ta
1e5cc 62 6c 65 20 6f 72 20 61 20 76 69 72 74 75 61 6c  ble or a virtual
1e5cd 20 74 61 62 6c 65 2e 20 20 54 68 65 72 65 20 75   table.  There u
1e5ce 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 61 20 73  sed to.** be a s
1e5cf 65 70 61 72 61 74 65 20 4f 50 5f 56 52 6f 77 69  eparate OP_VRowi
1e5d0 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 75 73 65  d opcode for use
1e5d1 20 77 69 74 68 20 76 69 72 74 75 61 6c 20 74 61   with virtual ta
1e5d2 62 6c 65 73 2c 20 62 75 74 20 74 68 69 73 0a 2a  bles, but this.*
1e5d3 2a 20 6f 6e 65 20 6f 70 63 6f 64 65 20 6e 6f 77  * one opcode now
1e5d4 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 20   works for both 
1e5d5 74 61 62 6c 65 20 74 79 70 65 73 2e 0a 2a 2f 0a  table types..*/.
1e5d6 63 61 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b  case OP_Rowid: {
1e5d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e5d8 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
1e5d9 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  ase */.#if 0  /*
1e5da 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e5db 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 69   moved into u.bi
1e5dc 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72   */.  VdbeCursor
1e5dd 20 2a 70 43 3b 0a 20 20 69 36 34 20 76 3b 0a 20   *pC;.  i64 v;. 
1e5de 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1e5df 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71  Vtab;.  const sq
1e5e0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1e5e1 6f 64 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f 2a  odule;.#endif /*
1e5e2 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e5e3 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 69   moved into u.bi
1e5e4 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
1e5e5 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1e5e6 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1e5e7 29 3b 0a 20 20 75 2e 62 69 2e 70 43 20 3d 20 70  );.  u.bi.pC = p
1e5e8 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1e5e9 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 69  ;.  assert( u.bi
1e5ea 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  .pC!=0 );.  asse
1e5eb 72 74 28 20 75 2e 62 69 2e 70 43 2d 3e 70 73 65  rt( u.bi.pC->pse
1e5ec 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29  udoTableReg==0 )
1e5ed 3b 0a 20 20 69 66 28 20 75 2e 62 69 2e 70 43 2d  ;.  if( u.bi.pC-
1e5ee 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20  >nullRow ){.    
1e5ef 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 73 6f  /* Do nothing so
1e5f0 20 74 68 61 74 20 72 65 67 5b 50 32 5d 20 72 65   that reg[P2] re
1e5f1 6d 61 69 6e 73 20 4e 55 4c 4c 20 2a 2f 0a 20 20  mains NULL */.  
1e5f2 20 20 62 72 65 61 6b 3b 0a 20 20 7d 65 6c 73 65    break;.  }else
1e5f3 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 64 65   if( u.bi.pC->de
1e5f4 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a  ferredMoveto ){.
1e5f5 20 20 20 20 75 2e 62 69 2e 76 20 3d 20 75 2e 62      u.bi.v = u.b
1e5f6 69 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54 61 72 67  i.pC->movetoTarg
1e5f7 65 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  et;.#ifndef SQLI
1e5f8 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1e5f9 41 42 4c 45 0a 20 20 7d 65 6c 73 65 20 69 66 28  ABLE.  }else if(
1e5fa 20 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43   u.bi.pC->pVtabC
1e5fb 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 75 2e 62  ursor ){.    u.b
1e5fc 69 2e 70 56 74 61 62 20 3d 20 75 2e 62 69 2e 70  i.pVtab = u.bi.p
1e5fd 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e  C->pVtabCursor->
1e5fe 70 56 74 61 62 3b 0a 20 20 20 20 75 2e 62 69 2e  pVtab;.    u.bi.
1e5ff 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 62 69 2e 70  pModule = u.bi.p
1e600 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  Vtab->pModule;. 
1e601 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e     assert( u.bi.
1e602 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 20  pModule->xRowid 
1e603 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
1e604 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
1e605 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1e606 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20  _to_misuse;.    
1e607 72 63 20 3d 20 75 2e 62 69 2e 70 4d 6f 64 75 6c  rc = u.bi.pModul
1e608 65 2d 3e 78 52 6f 77 69 64 28 75 2e 62 69 2e 70  e->xRowid(u.bi.p
1e609 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20  C->pVtabCursor, 
1e60a 26 75 2e 62 69 2e 76 29 3b 0a 20 20 20 20 73 71  &u.bi.v);.    sq
1e60b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1e60c 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  p->zErrMsg);.   
1e60d 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e   p->zErrMsg = u.
1e60e 62 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  bi.pVtab->zErrMs
1e60f 67 3b 0a 20 20 20 20 75 2e 62 69 2e 70 56 74 61  g;.    u.bi.pVta
1e610 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  b->zErrMsg = 0;.
1e611 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1e612 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f  afetyOn(db) ) go
1e613 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1e614 6d 69 73 75 73 65 3b 0a 23 65 6e 64 69 66 20 2f  misuse;.#endif /
1e615 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1e616 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 20 20  RTUALTABLE */.  
1e617 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
1e618 74 28 20 75 2e 62 69 2e 70 43 2d 3e 70 43 75 72  t( u.bi.pC->pCur
1e619 73 6f 72 21 3d 30 20 29 3b 0a 20 20 20 20 72 63  sor!=0 );.    rc
1e61a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
1e61b 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 69 2e  rsorMoveto(u.bi.
1e61c 70 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  pC);.    if( rc 
1e61d 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1e61e 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 69  _to_error;.    i
1e61f 66 28 20 75 2e 62 69 2e 70 43 2d 3e 72 6f 77 69  f( u.bi.pC->rowi
1e620 64 49 73 56 61 6c 69 64 20 29 7b 0a 20 20 20 20  dIsValid ){.    
1e621 20 20 75 2e 62 69 2e 76 20 3d 20 75 2e 62 69 2e    u.bi.v = u.bi.
1e622 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20  pC->lastRowid;. 
1e623 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1e624 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1e625 65 4b 65 79 53 69 7a 65 28 75 2e 62 69 2e 70 43  eKeySize(u.bi.pC
1e626 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 69  ->pCursor, &u.bi
1e627 2e 76 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  .v);.      asser
1e628 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
1e629 20 29 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 73   );  /* Always s
1e62a 6f 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72  o because of Cur
1e62b 73 6f 72 4d 6f 76 65 74 6f 28 29 20 61 62 6f 76  sorMoveto() abov
1e62c 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  e */.    }.  }. 
1e62d 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62   pOut->u.i = u.b
1e62e 69 2e 76 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70  i.v;.  MemSetTyp
1e62f 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
1e630 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Int);.  break;.}
1e631 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c  ../* Opcode: Nul
1e632 6c 52 6f 77 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  lRow P1 * * * *.
1e633 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63  **.** Move the c
1e634 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20 6e 75  ursor P1 to a nu
1e635 6c 6c 20 72 6f 77 2e 20 20 41 6e 79 20 4f 50 5f  ll row.  Any OP_
1e636 43 6f 6c 75 6d 6e 20 6f 70 65 72 61 74 69 6f 6e  Column operation
1e637 73 0a 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 20  s.** that occur 
1e638 77 68 69 6c 65 20 74 68 65 20 63 75 72 73 6f 72  while the cursor
1e639 20 69 73 20 6f 6e 20 74 68 65 20 6e 75 6c 6c 20   is on the null 
1e63a 72 6f 77 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a  row will always.
1e63b 2a 2a 20 77 72 69 74 65 20 61 20 4e 55 4c 4c 2e  ** write a NULL.
1e63c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c  .*/.case OP_Null
1e63d 52 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  Row: {.#if 0  /*
1e63e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e63f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6a   moved into u.bj
1e640 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72   */.  VdbeCursor
1e641 20 2a 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   *pC;.#endif /* 
1e642 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e643 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6a 20  moved into u.bj 
1e644 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f  */..  assert( pO
1e645 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1e646 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
1e647 3b 0a 20 20 75 2e 62 6a 2e 70 43 20 3d 20 70 2d  ;.  u.bj.pC = p-
1e648 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1e649 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6a 2e  .  assert( u.bj.
1e64a 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6a 2e  pC!=0 );.  u.bj.
1e64b 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b  pC->nullRow = 1;
1e64c 0a 20 20 75 2e 62 6a 2e 70 43 2d 3e 72 6f 77 69  .  u.bj.pC->rowi
1e64d 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1e64e 69 66 28 20 75 2e 62 6a 2e 70 43 2d 3e 70 43 75  if( u.bj.pC->pCu
1e64f 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  rsor ){.    sqli
1e650 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72  te3BtreeClearCur
1e651 73 6f 72 28 75 2e 62 6a 2e 70 43 2d 3e 70 43 75  sor(u.bj.pC->pCu
1e652 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 62 72 65  rsor);.  }.  bre
1e653 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e654 3a 20 4c 61 73 74 20 50 31 20 50 32 20 2a 20 2a  : Last P1 P2 * *
1e655 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78   *.**.** The nex
1e656 74 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77  t use of the Row
1e657 69 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20  id or Column or 
1e658 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  Next instruction
1e659 20 66 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c   for P1 .** will
1e65a 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 6c 61   refer to the la
1e65b 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
1e65c 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f  database table o
1e65d 72 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74  r index..** If t
1e65e 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  he table or inde
1e65f 78 20 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50  x is empty and P
1e660 32 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69  2>0, then jump i
1e661 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32  mmediately to P2
1e662 2e 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20  ..** If P2 is 0 
1e663 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  or if the table 
1e664 6f 72 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20  or index is not 
1e665 65 6d 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f  empty, fall thro
1e666 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f  ugh.** to the fo
1e667 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74  llowing instruct
1e668 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ion..*/.case OP_
1e669 4c 61 73 74 3a 20 7b 20 20 20 20 20 20 20 20 2f  Last: {        /
1e66a 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20  * jump */.#if 0 
1e66b 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e66c 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e66d 2e 62 6b 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .bk */.  VdbeCur
1e66e 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
1e66f 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e  sor *pCrsr;.  in
1e670 74 20 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a  t res;.#endif /*
1e671 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e672 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b   moved into u.bk
1e673 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
1e674 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1e675 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1e676 29 3b 0a 20 20 75 2e 62 6b 2e 70 43 20 3d 20 70  );.  u.bk.pC = p
1e677 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1e678 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6b  ;.  assert( u.bk
1e679 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6b  .pC!=0 );.  u.bk
1e67a 2e 70 43 72 73 72 20 3d 20 75 2e 62 6b 2e 70 43  .pCrsr = u.bk.pC
1e67b 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28  ->pCursor;.  if(
1e67c 20 75 2e 62 6b 2e 70 43 72 73 72 3d 3d 30 20 29   u.bk.pCrsr==0 )
1e67d 7b 0a 20 20 20 20 75 2e 62 6b 2e 72 65 73 20 3d  {.    u.bk.res =
1e67e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
1e67f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e680 65 65 4c 61 73 74 28 75 2e 62 6b 2e 70 43 72 73  eeLast(u.bk.pCrs
1e681 72 2c 20 26 75 2e 62 6b 2e 72 65 73 29 3b 0a 20  r, &u.bk.res);. 
1e682 20 7d 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 6e 75   }.  u.bk.pC->nu
1e683 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 6b  llRow = (u8)u.bk
1e684 2e 72 65 73 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d  .res;.  u.bk.pC-
1e685 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1e686 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e  = 0;.  u.bk.pC->
1e687 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
1e688 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 63 61 63  ;.  u.bk.pC->cac
1e689 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1e68a 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 70 4f  _STALE;.  if( pO
1e68b 70 2d 3e 70 32 3e 30 20 26 26 20 75 2e 62 6b 2e  p->p2>0 && u.bk.
1e68c 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20  res ){.    pc = 
1e68d 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d  pOp->p2 - 1;.  }
1e68e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a  .  break;.}.../*
1e68f 20 4f 70 63 6f 64 65 3a 20 53 6f 72 74 20 50 31   Opcode: Sort P1
1e690 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1e691 54 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73  This opcode does
1e692 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d   exactly the sam
1e693 65 20 74 68 69 6e 67 20 61 73 20 4f 50 5f 52 65  e thing as OP_Re
1e694 77 69 6e 64 20 65 78 63 65 70 74 20 74 68 61 74  wind except that
1e695 0a 2a 2a 20 69 74 20 69 6e 63 72 65 6d 65 6e 74  .** it increment
1e696 73 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65  s an undocumente
1e697 64 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  d global variabl
1e698 65 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  e used for testi
1e699 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f 72 74 69 6e  ng..**.** Sortin
1e69a 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 73 68 65  g is accomplishe
1e69b 64 20 62 79 20 77 72 69 74 69 6e 67 20 72 65 63  d by writing rec
1e69c 6f 72 64 73 20 69 6e 74 6f 20 61 20 73 6f 72 74  ords into a sort
1e69d 69 6e 67 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68  ing index,.** th
1e69e 65 6e 20 72 65 77 69 6e 64 69 6e 67 20 74 68 61  en rewinding tha
1e69f 74 20 69 6e 64 65 78 20 61 6e 64 20 70 6c 61 79  t index and play
1e6a0 69 6e 67 20 69 74 20 62 61 63 6b 20 66 72 6f 6d  ing it back from
1e6a1 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a   beginning to.**
1e6a2 20 65 6e 64 2e 20 20 57 65 20 75 73 65 20 74 68   end.  We use th
1e6a3 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65  e OP_Sort opcode
1e6a4 20 69 6e 73 74 65 61 64 20 6f 66 20 4f 50 5f 52   instead of OP_R
1e6a5 65 77 69 6e 64 20 74 6f 20 64 6f 20 74 68 65 0a  ewind to do the.
1e6a6 2a 2a 20 72 65 77 69 6e 64 69 6e 67 20 73 6f 20  ** rewinding so 
1e6a7 74 68 61 74 20 74 68 65 20 67 6c 6f 62 61 6c 20  that the global 
1e6a8 76 61 72 69 61 62 6c 65 20 77 69 6c 6c 20 62 65  variable will be
1e6a9 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 6e 64   incremented and
1e6aa 0a 2a 2a 20 72 65 67 72 65 73 73 69 6f 6e 20 74  .** regression t
1e6ab 65 73 74 73 20 63 61 6e 20 64 65 74 65 72 6d 69  ests can determi
1e6ac 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ne whether or no
1e6ad 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 20  t the optimizer 
1e6ae 69 73 0a 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20  is.** correctly 
1e6af 6f 70 74 69 6d 69 7a 69 6e 67 20 6f 75 74 20 73  optimizing out s
1e6b0 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  orts..*/.case OP
1e6b1 5f 53 6f 72 74 3a 20 7b 20 20 20 20 20 20 20 20  _Sort: {        
1e6b2 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 64 65  /* jump */.#ifde
1e6b3 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
1e6b4 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f 75  sqlite3_sort_cou
1e6b5 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f  nt++;.  sqlite3_
1e6b6 73 65 61 72 63 68 5f 63 6f 75 6e 74 2d 2d 3b 0a  search_count--;.
1e6b7 23 65 6e 64 69 66 0a 20 20 70 2d 3e 61 43 6f 75  #endif.  p->aCou
1e6b8 6e 74 65 72 5b 53 51 4c 49 54 45 5f 53 54 4d 54  nter[SQLITE_STMT
1e6b9 53 54 41 54 55 53 5f 53 4f 52 54 2d 31 5d 2b 2b  STATUS_SORT-1]++
1e6ba 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f  ;.  /* Fall thro
1e6bb 75 67 68 20 69 6e 74 6f 20 4f 50 5f 52 65 77 69  ugh into OP_Rewi
1e6bc 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f 70 63 6f 64  nd */.}./* Opcod
1e6bd 65 3a 20 52 65 77 69 6e 64 20 50 31 20 50 32 20  e: Rewind P1 P2 
1e6be 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  * * *.**.** The 
1e6bf 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68 65 20  next use of the 
1e6c0 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d 6e 20  Rowid or Column 
1e6c1 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74  or Next instruct
1e6c2 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a 20 77  ion for P1 .** w
1e6c3 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74 68 65  ill refer to the
1e6c4 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20   first entry in 
1e6c5 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62  the database tab
1e6c6 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 20  le or index..** 
1e6c7 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  If the table or 
1e6c8 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20 61  index is empty a
1e6c9 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a 75  nd P2>0, then ju
1e6ca 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
1e6cb 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20 69  o P2..** If P2 i
1e6cc 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 74 61  s 0 or if the ta
1e6cd 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20  ble or index is 
1e6ce 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c 20  not empty, fall 
1e6cf 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68  through.** to th
1e6d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74  e following inst
1e6d1 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65  ruction..*/.case
1e6d2 20 4f 50 5f 52 65 77 69 6e 64 3a 20 7b 20 20 20   OP_Rewind: {   
1e6d3 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1e6d4 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1e6d5 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e6d6 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0a 20 20 56  into u.bl */.  V
1e6d7 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  dbeCursor *pC;. 
1e6d8 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1e6d9 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e  ;.  int res;.#en
1e6da 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e6db 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e6dc 6f 20 75 2e 62 6c 20 2a 2f 0a 0a 20 20 61 73 73  o u.bl */..  ass
1e6dd 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1e6de 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
1e6df 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6c 2e  ursor );.  u.bl.
1e6e0 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  pC = p->apCsr[pO
1e6e1 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1e6e2 28 20 75 2e 62 6c 2e 70 43 21 3d 30 20 29 3b 0a  ( u.bl.pC!=0 );.
1e6e3 20 20 69 66 28 20 28 75 2e 62 6c 2e 70 43 72 73    if( (u.bl.pCrs
1e6e4 72 20 3d 20 75 2e 62 6c 2e 70 43 2d 3e 70 43 75  r = u.bl.pC->pCu
1e6e5 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20  rsor)!=0 ){.    
1e6e6 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1e6e7 65 46 69 72 73 74 28 75 2e 62 6c 2e 70 43 72 73  eFirst(u.bl.pCrs
1e6e8 72 2c 20 26 75 2e 62 6c 2e 72 65 73 29 3b 0a 20  r, &u.bl.res);. 
1e6e9 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 61 74 46 69     u.bl.pC->atFi
1e6ea 72 73 74 20 3d 20 75 2e 62 6c 2e 72 65 73 3d 3d  rst = u.bl.res==
1e6eb 30 20 3f 31 3a 30 3b 0a 20 20 20 20 75 2e 62 6c  0 ?1:0;.    u.bl
1e6ec 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
1e6ed 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62  eto = 0;.    u.b
1e6ee 6c 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  l.pC->cacheStatu
1e6ef 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
1e6f0 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 72 6f  .    u.bl.pC->ro
1e6f1 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1e6f2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62    }else{.    u.b
1e6f3 6c 2e 72 65 73 20 3d 20 31 3b 0a 20 20 7d 0a 20  l.res = 1;.  }. 
1e6f4 20 75 2e 62 6c 2e 70 43 2d 3e 6e 75 6c 6c 52 6f   u.bl.pC->nullRo
1e6f5 77 20 3d 20 28 75 38 29 75 2e 62 6c 2e 72 65 73  w = (u8)u.bl.res
1e6f6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1e6f7 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32  >p2>0 && pOp->p2
1e6f8 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28  <p->nOp );.  if(
1e6f9 20 75 2e 62 6c 2e 72 65 73 20 29 7b 0a 20 20 20   u.bl.res ){.   
1e6fa 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1e6fb 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1e6fc 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65  }../* Opcode: Ne
1e6fd 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  xt P1 P2 * * *.*
1e6fe 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 63 75 72  *.** Advance cur
1e6ff 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69  sor P1 so that i
1e700 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
1e701 6e 65 78 74 20 6b 65 79 2f 64 61 74 61 20 70 61  next key/data pa
1e702 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62  ir in its.** tab
1e703 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 20 49 66  le or index.  If
1e704 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f   there are no mo
1e705 72 65 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69  re key/value pai
1e706 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72  rs then fall thr
1e707 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66  ough.** to the f
1e708 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63  ollowing instruc
1e709 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68  tion.  But if th
1e70a 65 20 63 75 72 73 6f 72 20 61 64 76 61 6e 63 65  e cursor advance
1e70b 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c   was successful,
1e70c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61  .** jump immedia
1e70d 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  tely to P2..**.*
1e70e 2a 20 54 68 65 20 50 31 20 63 75 72 73 6f 72 20  * The P1 cursor 
1e70f 6d 75 73 74 20 62 65 20 66 6f 72 20 61 20 72 65  must be for a re
1e710 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20  al table, not a 
1e711 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a  pseudo-table..**
1e712 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 50 72  .** See also: Pr
1e713 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ev.*/./* Opcode:
1e714 20 50 72 65 76 20 50 31 20 50 32 20 2a 20 2a 20   Prev P1 P2 * * 
1e715 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b 20 75 70 20  *.**.** Back up 
1e716 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61  cursor P1 so tha
1e717 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74  t it points to t
1e718 68 65 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f  he previous key/
1e719 64 61 74 61 20 70 61 69 72 20 69 6e 20 69 74 73  data pair in its
1e71a 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  .** table or ind
1e71b 65 78 2e 20 20 49 66 20 74 68 65 72 65 20 69 73  ex.  If there is
1e71c 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 6b 65 79   no previous key
1e71d 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74 68 65  /value pairs the
1e71e 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a  n fall through.*
1e71f 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * to the followi
1e720 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  ng instruction. 
1e721 20 42 75 74 20 69 66 20 74 68 65 20 63 75 72 73   But if the curs
1e722 6f 72 20 62 61 63 6b 75 70 20 77 61 73 20 73 75  or backup was su
1e723 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d  ccessful,.** jum
1e724 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  p immediately to
1e725 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50   P2..**.** The P
1e726 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65  1 cursor must be
1e727 20 66 6f 72 20 61 20 72 65 61 6c 20 74 61 62 6c   for a real tabl
1e728 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d  e, not a pseudo-
1e729 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  table..*/.case O
1e72a 50 5f 50 72 65 76 3a 20 20 20 20 20 20 20 20 20  P_Prev:         
1e72b 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61 73 65   /* jump */.case
1e72c 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 20 20 20 20   OP_Next: {     
1e72d 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69     /* jump */.#i
1e72e 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1e72f 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e730 74 6f 20 75 2e 62 6d 20 2a 2f 0a 20 20 56 64 62  to u.bm */.  Vdb
1e731 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1e732 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1e733 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 69    int res;.#endi
1e734 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e735 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e736 75 2e 62 6d 20 2a 2f 0a 0a 20 20 43 48 45 43 4b  u.bm */..  CHECK
1e737 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a  _FOR_INTERRUPT;.
1e738 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e739 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1e73a 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1e73b 75 2e 62 6d 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bm.pC = p->apC
1e73c 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69  sr[pOp->p1];.  i
1e73d 66 28 20 75 2e 62 6d 2e 70 43 3d 3d 30 20 29 7b  f( u.bm.pC==0 ){
1e73e 0a 20 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20  .    break;  /* 
1e73f 53 65 65 20 74 69 63 6b 65 74 20 23 32 32 37 33  See ticket #2273
1e740 20 2a 2f 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 70   */.  }.  u.bm.p
1e741 43 72 73 72 20 3d 20 75 2e 62 6d 2e 70 43 2d 3e  Crsr = u.bm.pC->
1e742 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75  pCursor;.  if( u
1e743 2e 62 6d 2e 70 43 72 73 72 3d 3d 30 20 29 7b 0a  .bm.pCrsr==0 ){.
1e744 20 20 20 20 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c      u.bm.pC->nul
1e745 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 62 72  lRow = 1;.    br
1e746 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e  eak;.  }.  u.bm.
1e747 72 65 73 20 3d 20 31 3b 0a 20 20 61 73 73 65 72  res = 1;.  asser
1e748 74 28 20 75 2e 62 6d 2e 70 43 2d 3e 64 65 66 65  t( u.bm.pC->defe
1e749 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b  rredMoveto==0 );
1e74a 0a 20 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63  .  rc = pOp->opc
1e74b 6f 64 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73  ode==OP_Next ? s
1e74c 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28  qlite3BtreeNext(
1e74d 75 2e 62 6d 2e 70 43 72 73 72 2c 20 26 75 2e 62  u.bm.pCrsr, &u.b
1e74e 6d 2e 72 65 73 29 20 3a 0a 20 20 20 20 20 20 20  m.res) :.       
1e74f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e750 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
1e751 72 65 65 50 72 65 76 69 6f 75 73 28 75 2e 62 6d  reePrevious(u.bm
1e752 2e 70 43 72 73 72 2c 20 26 75 2e 62 6d 2e 72 65  .pCrsr, &u.bm.re
1e753 73 29 3b 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 6e  s);.  u.bm.pC->n
1e754 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62  ullRow = (u8)u.b
1e755 6d 2e 72 65 73 3b 0a 20 20 75 2e 62 6d 2e 70 43  m.res;.  u.bm.pC
1e756 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1e757 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69  CACHE_STALE;.  i
1e758 66 28 20 75 2e 62 6d 2e 72 65 73 3d 3d 30 20 29  f( u.bm.res==0 )
1e759 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  {.    pc = pOp->
1e75a 70 32 20 2d 20 31 3b 0a 20 20 20 20 69 66 28 20  p2 - 1;.    if( 
1e75b 70 4f 70 2d 3e 70 35 20 29 20 70 2d 3e 61 43 6f  pOp->p5 ) p->aCo
1e75c 75 6e 74 65 72 5b 70 4f 70 2d 3e 70 35 2d 31 5d  unter[pOp->p5-1]
1e75d 2b 2b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ++;.#ifdef SQLIT
1e75e 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74  E_TEST.    sqlit
1e75f 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b  e3_search_count+
1e760 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  +;.#endif.  }.  
1e761 75 2e 62 6d 2e 70 43 2d 3e 72 6f 77 69 64 49 73  u.bm.pC->rowidIs
1e762 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 62 72 65  Valid = 0;.  bre
1e763 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e764 3a 20 49 64 78 49 6e 73 65 72 74 20 50 31 20 50  : IdxInsert P1 P
1e765 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20  2 P3 * P5.**.** 
1e766 52 65 67 69 73 74 65 72 20 50 32 20 68 6f 6c 64  Register P2 hold
1e767 73 20 61 20 53 51 4c 20 69 6e 64 65 78 20 6b 65  s a SQL index ke
1e768 79 20 6d 61 64 65 20 75 73 69 6e 67 20 74 68 65  y made using the
1e769 0a 2a 2a 20 4d 61 6b 65 52 65 63 6f 72 64 20 69  .** MakeRecord i
1e76a 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 54 68  nstructions.  Th
1e76b 69 73 20 6f 70 63 6f 64 65 20 77 72 69 74 65 73  is opcode writes
1e76c 20 74 68 61 74 20 6b 65 79 0a 2a 2a 20 69 6e 74   that key.** int
1e76d 6f 20 74 68 65 20 69 6e 64 65 78 20 50 31 2e 20  o the index P1. 
1e76e 20 44 61 74 61 20 66 6f 72 20 74 68 65 20 65 6e   Data for the en
1e76f 74 72 79 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a  try is nil..**.*
1e770 2a 20 50 33 20 69 73 20 61 20 66 6c 61 67 20 74  * P3 is a flag t
1e771 68 61 74 20 70 72 6f 76 69 64 65 73 20 61 20 68  hat provides a h
1e772 69 6e 74 20 74 6f 20 74 68 65 20 62 2d 74 72 65  int to the b-tre
1e773 65 20 6c 61 79 65 72 20 74 68 61 74 20 74 68 69  e layer that thi
1e774 73 0a 2a 2a 20 69 6e 73 65 72 74 20 69 73 20 6c  s.** insert is l
1e775 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61  ikely to be an a
1e776 70 70 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ppend..**.** Thi
1e777 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e  s instruction on
1e778 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 69 6e 64  ly works for ind
1e779 69 63 65 73 2e 20 20 54 68 65 20 65 71 75 69 76  ices.  The equiv
1e77a 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f  alent instructio
1e77b 6e 0a 2a 2a 20 66 6f 72 20 74 61 62 6c 65 73 20  n.** for tables 
1e77c 69 73 20 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f  is OP_Insert..*/
1e77d 0a 63 61 73 65 20 4f 50 5f 49 64 78 49 6e 73 65  .case OP_IdxInse
1e77e 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  rt: {        /* 
1e77f 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  in2 */.#if 0  /*
1e780 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e781 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6e   moved into u.bn
1e782 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72   */.  VdbeCursor
1e783 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72   *pC;.  BtCursor
1e784 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 6e   *pCrsr;.  int n
1e785 4b 65 79 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  Key;.  const cha
1e786 72 20 2a 7a 4b 65 79 3b 0a 23 65 6e 64 69 66 20  r *zKey;.#endif 
1e787 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e788 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e789 62 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  bn */..  assert(
1e78a 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1e78b 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
1e78c 72 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 20 3d  r );.  u.bn.pC =
1e78d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1e78e 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  1];.  assert( u.
1e78f 62 6e 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73  bn.pC!=0 );.  as
1e790 73 65 72 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67  sert( pIn2->flag
1e791 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a  s & MEM_Blob );.
1e792 20 20 75 2e 62 6e 2e 70 43 72 73 72 20 3d 20 75    u.bn.pCrsr = u
1e793 2e 62 6e 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b  .bn.pC->pCursor;
1e794 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e  .  if( ALWAYS(u.
1e795 62 6e 2e 70 43 72 73 72 21 3d 30 29 20 29 7b 0a  bn.pCrsr!=0) ){.
1e796 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 6e      assert( u.bn
1e797 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20  .pC->isTable==0 
1e798 29 3b 0a 20 20 20 20 72 63 20 3d 20 45 78 70 61  );.    rc = Expa
1e799 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20  ndBlob(pIn2);.  
1e79a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1e79b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e 62  _OK ){.      u.b
1e79c 6e 2e 6e 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 6e  n.nKey = pIn2->n
1e79d 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2e 7a 4b 65  ;.      u.bn.zKe
1e79e 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a 20 20 20  y = pIn2->z;.   
1e79f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1e7a0 74 72 65 65 49 6e 73 65 72 74 28 75 2e 62 6e 2e  treeInsert(u.bn.
1e7a1 70 43 72 73 72 2c 20 75 2e 62 6e 2e 7a 4b 65 79  pCrsr, u.bn.zKey
1e7a2 2c 20 75 2e 62 6e 2e 6e 4b 65 79 2c 20 22 22 2c  , u.bn.nKey, "",
1e7a3 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 33 2c 0a   0, 0, pOp->p3,.
1e7a4 20 20 20 20 20 20 20 20 20 20 28 28 70 4f 70 2d            ((pOp-
1e7a5 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45  >p5 & OPFLAG_USE
1e7a6 53 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 75 2e  SEEKRESULT) ? u.
1e7a7 62 6e 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c  bn.pC->seekResul
1e7a8 74 20 3a 20 30 29 0a 20 20 20 20 20 20 29 3b 0a  t : 0).      );.
1e7a9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1e7aa 62 6e 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  bn.pC->deferredM
1e7ab 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20  oveto==0 );.    
1e7ac 20 20 75 2e 62 6e 2e 70 43 2d 3e 63 61 63 68 65    u.bn.pC->cache
1e7ad 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
1e7ae 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TALE;.    }.  }.
1e7af 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e7b0 70 63 6f 64 65 3a 20 49 64 78 44 65 6c 65 74 65  pcode: IdxDelete
1e7b1 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1e7b2 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  .** The content 
1e7b3 6f 66 20 50 33 20 72 65 67 69 73 74 65 72 73 20  of P3 registers 
1e7b4 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 69  starting at regi
1e7b5 73 74 65 72 20 50 32 20 66 6f 72 6d 0a 2a 2a 20  ster P2 form.** 
1e7b6 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65  an unpacked inde
1e7b7 78 20 6b 65 79 2e 20 54 68 69 73 20 6f 70 63 6f  x key. This opco
1e7b8 64 65 20 72 65 6d 6f 76 65 73 20 74 68 61 74 20  de removes that 
1e7b9 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 0a  entry from the .
1e7ba 2a 2a 20 69 6e 64 65 78 20 6f 70 65 6e 65 64 20  ** index opened 
1e7bb 62 79 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2f  by cursor P1..*/
1e7bc 0a 63 61 73 65 20 4f 50 5f 49 64 78 44 65 6c 65  .case OP_IdxDele
1e7bd 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  te: {.#if 0  /* 
1e7be 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e7bf 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6f 20  moved into u.bo 
1e7c0 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  */.  VdbeCursor 
1e7c1 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  *pC;.  BtCursor 
1e7c2 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65  *pCrsr;.  int re
1e7c3 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63  s;.  UnpackedRec
1e7c4 6f 72 64 20 72 3b 0a 23 65 6e 64 69 66 20 2f 2a  ord r;.#endif /*
1e7c5 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e7c6 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6f   moved into u.bo
1e7c7 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
1e7c8 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 61 73  Op->p3>0 );.  as
1e7c9 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1e7ca 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d 3e  && pOp->p2+pOp->
1e7cb 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b  p3<=p->nMem+1 );
1e7cc 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e7cd 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e7ce 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e7cf 20 75 2e 62 6f 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bo.pC = p->ap
1e7d0 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e7d1 61 73 73 65 72 74 28 20 75 2e 62 6f 2e 70 43 21  assert( u.bo.pC!
1e7d2 3d 30 20 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 72  =0 );.  u.bo.pCr
1e7d3 73 72 20 3d 20 75 2e 62 6f 2e 70 43 2d 3e 70 43  sr = u.bo.pC->pC
1e7d4 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57  ursor;.  if( ALW
1e7d5 41 59 53 28 75 2e 62 6f 2e 70 43 72 73 72 21 3d  AYS(u.bo.pCrsr!=
1e7d6 30 29 20 29 7b 0a 20 20 20 20 75 2e 62 6f 2e 72  0) ){.    u.bo.r
1e7d7 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 6f  .pKeyInfo = u.bo
1e7d8 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20  .pC->pKeyInfo;. 
1e7d9 20 20 20 75 2e 62 6f 2e 72 2e 6e 46 69 65 6c 64     u.bo.r.nField
1e7da 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 33 3b   = (u16)pOp->p3;
1e7db 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 66 6c 61 67  .    u.bo.r.flag
1e7dc 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 6f 2e  s = 0;.    u.bo.
1e7dd 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65  r.aMem = &p->aMe
1e7de 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  m[pOp->p2];.    
1e7df 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1e7e0 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
1e7e1 75 2e 62 6f 2e 70 43 72 73 72 2c 20 26 75 2e 62  u.bo.pCrsr, &u.b
1e7e2 6f 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 62 6f  o.r, 0, 0, &u.bo
1e7e3 2e 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72  .res);.    if( r
1e7e4 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1e7e5 75 2e 62 6f 2e 72 65 73 3d 3d 30 20 29 7b 0a 20  u.bo.res==0 ){. 
1e7e6 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1e7e7 33 42 74 72 65 65 44 65 6c 65 74 65 28 75 2e 62  3BtreeDelete(u.b
1e7e8 6f 2e 70 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a  o.pCrsr);.    }.
1e7e9 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 6f      assert( u.bo
1e7ea 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
1e7eb 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 75 2e  eto==0 );.    u.
1e7ec 62 6f 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  bo.pC->cacheStat
1e7ed 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1e7ee 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1e7ef 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78  ../* Opcode: Idx
1e7f0 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20 2a 20  Rowid P1 P2 * * 
1e7f1 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e  *.**.** Write in
1e7f2 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 61  to register P2 a
1e7f3 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
1e7f4 69 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72  is the last entr
1e7f5 79 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  y in the record 
1e7f6 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  at.** the end of
1e7f7 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20 70   the index key p
1e7f8 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 63 75 72  ointed to by cur
1e7f9 73 6f 72 20 50 31 2e 20 20 54 68 69 73 20 69 6e  sor P1.  This in
1e7fa 74 65 67 65 72 20 73 68 6f 75 6c 64 20 62 65 0a  teger should be.
1e7fb 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20  ** the rowid of 
1e7fc 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20  the table entry 
1e7fd 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 69 6e  to which this in
1e7fe 64 65 78 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  dex entry points
1e7ff 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1e800 3a 20 52 6f 77 69 64 2c 20 4d 61 6b 65 52 65 63  : Rowid, MakeRec
1e801 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ord..*/.case OP_
1e802 49 64 78 52 6f 77 69 64 3a 20 7b 20 20 20 20 20  IdxRowid: {     
1e803 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
1e804 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23  -prerelease */.#
1e805 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1e806 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e807 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a 20 20 42 74  nto u.bp */.  Bt
1e808 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1e809 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1e80a 0a 20 20 69 36 34 20 72 6f 77 69 64 3b 0a 23 65  .  i64 rowid;.#e
1e80b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e80c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e80d 74 6f 20 75 2e 62 70 20 2a 2f 0a 0a 20 20 61 73  to u.bp */..  as
1e80e 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e80f 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e810 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 70  Cursor );.  u.bp
1e811 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1e812 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1e813 74 28 20 75 2e 62 70 2e 70 43 21 3d 30 20 29 3b  t( u.bp.pC!=0 );
1e814 0a 20 20 75 2e 62 70 2e 70 43 72 73 72 20 3d 20  .  u.bp.pCrsr = 
1e815 75 2e 62 70 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bp.pC->pCursor
1e816 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75  ;.  if( ALWAYS(u
1e817 2e 62 70 2e 70 43 72 73 72 21 3d 30 29 20 29 7b  .bp.pCrsr!=0) ){
1e818 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1e819 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74  3VdbeCursorMovet
1e81a 6f 28 75 2e 62 70 2e 70 43 29 3b 0a 20 20 20 20  o(u.bp.pC);.    
1e81b 69 66 28 20 4e 45 56 45 52 28 72 63 29 20 29 20  if( NEVER(rc) ) 
1e81c 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1e81d 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 61 73 73  o_error;.    ass
1e81e 65 72 74 28 20 75 2e 62 70 2e 70 43 2d 3e 64 65  ert( u.bp.pC->de
1e81f 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20  ferredMoveto==0 
1e820 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  );.    assert( u
1e821 2e 62 70 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d  .bp.pC->isTable=
1e822 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21 75  =0 );.    if( !u
1e823 2e 62 70 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  .bp.pC->nullRow 
1e824 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
1e825 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69  lite3VdbeIdxRowi
1e826 64 28 64 62 2c 20 75 2e 62 70 2e 70 43 72 73 72  d(db, u.bp.pCrsr
1e827 2c 20 26 75 2e 62 70 2e 72 6f 77 69 64 29 3b 0a  , &u.bp.rowid);.
1e828 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1e829 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1e82a 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1e82b 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1e82c 20 20 7d 0a 20 20 20 20 20 20 4d 65 6d 53 65 74    }.      MemSet
1e82d 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
1e82e 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 70  EM_Int);.      p
1e82f 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 70 2e  Out->u.i = u.bp.
1e830 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 20 7d  rowid;.    }.  }
1e831 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1e832 4f 70 63 6f 64 65 3a 20 49 64 78 47 45 20 50 31  Opcode: IdxGE P1
1e833 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
1e834 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 74  ** The P4 regist
1e835 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e  er values beginn
1e836 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d  ing with P3 form
1e837 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64   an unpacked ind
1e838 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20  ex .** key that 
1e839 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e  omits the ROWID.
1e83a 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b    Compare this k
1e83b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74  ey value against
1e83c 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74   the index .** t
1e83d 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65 6e  hat P1 is curren
1e83e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c  tly pointing to,
1e83f 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f   ignoring the RO
1e840 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e  WID on the P1 in
1e841 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  dex..**.** If th
1e842 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79  e P1 index entry
1e843 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
1e844 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
1e845 20 6b 65 79 20 76 61 6c 75 65 0a 2a 2a 20 74 68   key value.** th
1e846 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20  en jump to P2.  
1e847 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74  Otherwise fall t
1e848 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
1e849 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  xt instruction..
1e84a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e  **.** If P5 is n
1e84b 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65  on-zero then the
1e84c 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e   key value is in
1e84d 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 70  creased by an ep
1e84e 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 6f 72 20  silon .** prior 
1e84f 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  to the compariso
1e850 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 20 74 68  n.  This make th
1e851 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69  e opcode work li
1e852 6b 65 20 49 64 78 47 54 20 65 78 63 65 70 74 0a  ke IdxGT except.
1e853 2a 2a 20 74 68 61 74 20 69 66 20 74 68 65 20 6b  ** that if the k
1e854 65 79 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72  ey from register
1e855 20 50 33 20 69 73 20 61 20 70 72 65 66 69 78 20   P3 is a prefix 
1e856 6f 66 20 74 68 65 20 6b 65 79 20 69 6e 20 74 68  of the key in th
1e857 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 65  e cursor,.** the
1e858 20 72 65 73 75 6c 74 20 69 73 20 66 61 6c 73 65   result is false
1e859 20 77 68 65 72 65 61 73 20 69 74 20 77 6f 75 6c   whereas it woul
1e85a 64 20 62 65 20 74 72 75 65 20 77 69 74 68 20 49  d be true with I
1e85b 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  dxGT..*/./* Opco
1e85c 64 65 3a 20 49 64 78 4c 54 20 50 31 20 50 32 20  de: IdxLT P1 P2 
1e85d 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68  P3 * P5.**.** Th
1e85e 65 20 50 34 20 72 65 67 69 73 74 65 72 20 76 61  e P4 register va
1e85f 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 77  lues beginning w
1e860 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 75  ith P3 form an u
1e861 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a 2a  npacked index .*
1e862 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 73  * key that omits
1e863 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f 6d   the ROWID.  Com
1e864 70 61 72 65 20 74 68 69 73 20 6b 65 79 20 76 61  pare this key va
1e865 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68 65 20  lue against the 
1e866 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 50  index .** that P
1e867 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  1 is currently p
1e868 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e 6f  ointing to, igno
1e869 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20 6f  ring the ROWID o
1e86a 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e 0a  n the P1 index..
1e86b 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20  **.** If the P1 
1e86c 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 6c  index entry is l
1e86d 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79  ess than the key
1e86e 20 76 61 6c 75 65 20 74 68 65 6e 20 6a 75 6d 70   value then jump
1e86f 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 68 65 72   to P2..** Other
1e870 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67  wise fall throug
1e871 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e  h to the next in
1e872 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  struction..**.**
1e873 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a 65   If P5 is non-ze
1e874 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79 20  ro then the key 
1e875 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73  value is increas
1e876 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f 6e  ed by an epsilon
1e877 20 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 74 68   prior .** to th
1e878 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54  e comparison.  T
1e879 68 69 73 20 6d 61 6b 65 73 20 74 68 65 20 6f 70  his makes the op
1e87a 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49  code work like I
1e87b 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  dxLE..*/.case OP
1e87c 5f 49 64 78 4c 54 3a 20 20 20 20 20 20 20 20 20  _IdxLT:         
1e87d 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f   /* jump, in3 */
1e87e 0a 63 61 73 65 20 4f 50 5f 49 64 78 47 45 3a 20  .case OP_IdxGE: 
1e87f 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1e880 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20  , in3 */.#if 0  
1e881 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e882 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e883 62 71 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73  bq */.  VdbeCurs
1e884 6f 72 20 2a 70 43 3b 0a 20 20 69 6e 74 20 72 65  or *pC;.  int re
1e885 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63  s;.  UnpackedRec
1e886 6f 72 64 20 72 3b 0a 23 65 6e 64 69 66 20 2f 2a  ord r;.#endif /*
1e887 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e888 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 71   moved into u.bq
1e889 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
1e88a 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1e88b 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1e88c 29 3b 0a 20 20 75 2e 62 71 2e 70 43 20 3d 20 70  );.  u.bq.pC = p
1e88d 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1e88e 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 71  ;.  assert( u.bq
1e88f 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  .pC!=0 );.  if( 
1e890 41 4c 57 41 59 53 28 75 2e 62 71 2e 70 43 2d 3e  ALWAYS(u.bq.pC->
1e891 70 43 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a 20  pCursor!=0) ){. 
1e892 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 71 2e     assert( u.bq.
1e893 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1e894 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  to==0 );.    ass
1e895 65 72 74 28 20 70 4f 70 2d 3e 70 35 3d 3d 30 20  ert( pOp->p5==0 
1e896 7c 7c 20 70 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b  || pOp->p5==1 );
1e897 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
1e898 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54  ->p4type==P4_INT
1e899 33 32 20 29 3b 0a 20 20 20 20 75 2e 62 71 2e 72  32 );.    u.bq.r
1e89a 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 71  .pKeyInfo = u.bq
1e89b 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20  .pC->pKeyInfo;. 
1e89c 20 20 20 75 2e 62 71 2e 72 2e 6e 46 69 65 6c 64     u.bq.r.nField
1e89d 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 34 2e   = (u16)pOp->p4.
1e89e 69 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  i;.    if( pOp->
1e89f 70 35 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 71  p5 ){.      u.bq
1e8a0 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43  .r.flags = UNPAC
1e8a1 4b 45 44 5f 49 4e 43 52 4b 45 59 20 7c 20 55 4e  KED_INCRKEY | UN
1e8a2 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f  PACKED_IGNORE_RO
1e8a3 57 49 44 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  WID;.    }else{.
1e8a4 20 20 20 20 20 20 75 2e 62 71 2e 72 2e 66 6c 61        u.bq.r.fla
1e8a5 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 47  gs = UNPACKED_IG
1e8a6 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20  NORE_ROWID;.    
1e8a7 7d 0a 20 20 20 20 75 2e 62 71 2e 72 2e 61 4d 65  }.    u.bq.r.aMe
1e8a8 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  m = &p->aMem[pOp
1e8a9 2d 3e 70 33 5d 3b 0a 20 20 20 20 72 63 20 3d 20  ->p3];.    rc = 
1e8aa 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65  sqlite3VdbeIdxKe
1e8ab 79 43 6f 6d 70 61 72 65 28 75 2e 62 71 2e 70 43  yCompare(u.bq.pC
1e8ac 2c 20 26 75 2e 62 71 2e 72 2c 20 26 75 2e 62 71  , &u.bq.r, &u.bq
1e8ad 2e 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 70  .res);.    if( p
1e8ae 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49  Op->opcode==OP_I
1e8af 64 78 4c 54 20 29 7b 0a 20 20 20 20 20 20 75 2e  dxLT ){.      u.
1e8b0 62 71 2e 72 65 73 20 3d 20 2d 75 2e 62 71 2e 72  bq.res = -u.bq.r
1e8b1 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  es;.    }else{. 
1e8b2 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1e8b3 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78  ->opcode==OP_Idx
1e8b4 47 45 20 29 3b 0a 20 20 20 20 20 20 75 2e 62 71  GE );.      u.bq
1e8b5 2e 72 65 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  .res++;.    }.  
1e8b6 20 20 69 66 28 20 75 2e 62 71 2e 72 65 73 3e 30    if( u.bq.res>0
1e8b7 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70   ){.      pc = p
1e8b8 4f 70 2d 3e 70 32 20 2d 20 31 20 3b 0a 20 20 20  Op->p2 - 1 ;.   
1e8b9 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
1e8ba 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65  }../* Opcode: De
1e8bb 73 74 72 6f 79 20 50 31 20 50 32 20 50 33 20 2a  stroy P1 P2 P3 *
1e8bc 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20   *.**.** Delete 
1e8bd 61 6e 20 65 6e 74 69 72 65 20 64 61 74 61 62 61  an entire databa
1e8be 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  se table or inde
1e8bf 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67  x whose root pag
1e8c0 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
1e8c1 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 67 69 76  e.** file is giv
1e8c2 65 6e 20 62 79 20 50 31 2e 0a 2a 2a 0a 2a 2a 20  en by P1..**.** 
1e8c3 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  The table being 
1e8c4 64 65 73 74 72 6f 79 65 64 20 69 73 20 69 6e 20  destroyed is in 
1e8c5 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1e8c6 65 20 66 69 6c 65 20 69 66 20 50 33 3d 3d 30 2e  e file if P3==0.
1e8c7 20 20 49 66 0a 2a 2a 20 50 33 3d 3d 31 20 74 68    If.** P3==1 th
1e8c8 65 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  en the table to 
1e8c9 62 65 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74  be clear is in t
1e8ca 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  he auxiliary dat
1e8cb 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68  abase file.** th
1e8cc 61 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  at is used to st
1e8cd 6f 72 65 20 74 61 62 6c 65 73 20 63 72 65 61 74  ore tables creat
1e8ce 65 20 75 73 69 6e 67 20 43 52 45 41 54 45 20 54  e using CREATE T
1e8cf 45 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a  EMPORARY TABLE..
1e8d0 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43  **.** If AUTOVAC
1e8d1 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 64 20 74  UUM is enabled t
1e8d2 68 65 6e 20 69 74 20 69 73 20 70 6f 73 73 69 62  hen it is possib
1e8d3 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20  le that another 
1e8d4 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 6d 69 67  root page.** mig
1e8d5 68 74 20 62 65 20 6d 6f 76 65 64 20 69 6e 74 6f  ht be moved into
1e8d6 20 74 68 65 20 6e 65 77 6c 79 20 64 65 6c 65 74   the newly delet
1e8d7 65 64 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20  ed root page in 
1e8d8 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 61 6c  order to keep al
1e8d9 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20  l.** root pages 
1e8da 63 6f 6e 74 69 67 75 6f 75 73 20 61 74 20 74 68  contiguous at th
1e8db 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
1e8dc 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
1e8dd 65 20 66 6f 72 6d 65 72 0a 2a 2a 20 76 61 6c 75  e former.** valu
1e8de 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 70 61  e of the root pa
1e8df 67 65 20 74 68 61 74 20 6d 6f 76 65 64 20 2d 20  ge that moved - 
1e8e0 69 74 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65  its value before
1e8e1 20 74 68 65 20 6d 6f 76 65 20 6f 63 63 75 72 72   the move occurr
1e8e2 65 64 20 2d 0a 2a 2a 20 69 73 20 73 74 6f 72 65  ed -.** is store
1e8e3 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  d in register P2
1e8e4 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 20 0a 2a  .  If no page .*
1e8e5 2a 20 6d 6f 76 65 6d 65 6e 74 20 77 61 73 20 72  * movement was r
1e8e6 65 71 75 69 72 65 64 20 28 62 65 63 61 75 73 65  equired (because
1e8e7 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
1e8e8 20 64 72 6f 70 70 65 64 20 77 61 73 20 61 6c 72   dropped was alr
1e8e9 65 61 64 79 20 0a 2a 2a 20 74 68 65 20 6c 61 73  eady .** the las
1e8ea 74 20 6f 6e 65 20 69 6e 20 74 68 65 20 64 61 74  t one in the dat
1e8eb 61 62 61 73 65 29 20 74 68 65 6e 20 61 20 7a 65  abase) then a ze
1e8ec 72 6f 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ro is stored in 
1e8ed 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20  register P2..** 
1e8ee 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73  If AUTOVACUUM is
1e8ef 20 64 69 73 61 62 6c 65 64 20 74 68 65 6e 20 61   disabled then a
1e8f0 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20   zero is stored 
1e8f1 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  in register P2..
1e8f2 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1e8f3 43 6c 65 61 72 0a 2a 2f 0a 63 61 73 65 20 4f 50  Clear.*/.case OP
1e8f4 5f 44 65 73 74 72 6f 79 3a 20 7b 20 20 20 20 20  _Destroy: {     
1e8f5 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1e8f6 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  se */.#if 0  /* 
1e8f7 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e8f8 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 72 20  moved into u.br 
1e8f9 2a 2f 0a 20 20 69 6e 74 20 69 4d 6f 76 65 64 3b  */.  int iMoved;
1e8fa 0a 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20 56  .  int iCnt;.  V
1e8fb 64 62 65 20 2a 70 56 64 62 65 3b 0a 20 20 69 6e  dbe *pVdbe;.  in
1e8fc 74 20 69 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a  t iDb;.#endif /*
1e8fd 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e8fe 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 72   moved into u.br
1e8ff 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
1e900 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1e901 41 42 4c 45 0a 20 20 75 2e 62 72 2e 69 43 6e 74  ABLE.  u.br.iCnt
1e902 20 3d 20 30 3b 0a 20 20 66 6f 72 28 75 2e 62 72   = 0;.  for(u.br
1e903 2e 70 56 64 62 65 3d 64 62 2d 3e 70 56 64 62 65  .pVdbe=db->pVdbe
1e904 3b 20 75 2e 62 72 2e 70 56 64 62 65 3b 20 75 2e  ; u.br.pVdbe; u.
1e905 62 72 2e 70 56 64 62 65 20 3d 20 75 2e 62 72 2e  br.pVdbe = u.br.
1e906 70 56 64 62 65 2d 3e 70 4e 65 78 74 29 7b 0a 20  pVdbe->pNext){. 
1e907 20 20 20 69 66 28 20 75 2e 62 72 2e 70 56 64 62     if( u.br.pVdb
1e908 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  e->magic==VDBE_M
1e909 41 47 49 43 5f 52 55 4e 20 26 26 20 75 2e 62 72  AGIC_RUN && u.br
1e90a 2e 70 56 64 62 65 2d 3e 69 6e 56 74 61 62 4d 65  .pVdbe->inVtabMe
1e90b 74 68 6f 64 3c 32 20 26 26 20 75 2e 62 72 2e 70  thod<2 && u.br.p
1e90c 56 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  Vdbe->pc>=0 ){. 
1e90d 20 20 20 20 20 75 2e 62 72 2e 69 43 6e 74 2b 2b       u.br.iCnt++
1e90e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73  ;.    }.  }.#els
1e90f 65 0a 20 20 75 2e 62 72 2e 69 43 6e 74 20 3d 20  e.  u.br.iCnt = 
1e910 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
1e911 74 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20  t;.#endif.  if( 
1e912 75 2e 62 72 2e 69 43 6e 74 3e 31 20 29 7b 0a 20  u.br.iCnt>1 ){. 
1e913 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c     rc = SQLITE_L
1e914 4f 43 4b 45 44 3b 0a 20 20 20 20 70 2d 3e 65 72  OCKED;.    p->er
1e915 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41  rorAction = OE_A
1e916 62 6f 72 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  bort;.  }else{. 
1e917 20 20 20 75 2e 62 72 2e 69 44 62 20 3d 20 70 4f     u.br.iDb = pO
1e918 70 2d 3e 70 33 3b 0a 20 20 20 20 61 73 73 65 72  p->p3;.    asser
1e919 74 28 20 75 2e 62 72 2e 69 43 6e 74 3d 3d 31 20  t( u.br.iCnt==1 
1e91a 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
1e91b 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1e91c 31 3c 3c 75 2e 62 72 2e 69 44 62 29 29 21 3d 30  1<<u.br.iDb))!=0
1e91d 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
1e91e 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62  ite3BtreeDropTab
1e91f 6c 65 28 64 62 2d 3e 61 44 62 5b 75 2e 62 72 2e  le(db->aDb[u.br.
1e920 69 44 62 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70  iDb].pBt, pOp->p
1e921 31 2c 20 26 75 2e 62 72 2e 69 4d 6f 76 65 64 29  1, &u.br.iMoved)
1e922 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ;.    MemSetType
1e923 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1e924 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75  nt);.    pOut->u
1e925 2e 69 20 3d 20 75 2e 62 72 2e 69 4d 6f 76 65 64  .i = u.br.iMoved
1e926 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
1e927 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1e928 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1e929 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62 72 2e 69  ITE_OK && u.br.i
1e92a 4d 6f 76 65 64 21 3d 30 20 29 7b 0a 20 20 20 20  Moved!=0 ){.    
1e92b 20 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 67    sqlite3RootPag
1e92c 65 4d 6f 76 65 64 28 26 64 62 2d 3e 61 44 62 5b  eMoved(&db->aDb[
1e92d 75 2e 62 72 2e 69 44 62 5d 2c 20 75 2e 62 72 2e  u.br.iDb], u.br.
1e92e 69 4d 6f 76 65 64 2c 20 70 4f 70 2d 3e 70 31 29  iMoved, pOp->p1)
1e92f 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
1e930 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1e931 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 65 61 72 20  * Opcode: Clear 
1e932 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44  P1 P2 P3.**.** D
1e933 65 6c 65 74 65 20 61 6c 6c 20 63 6f 6e 74 65 6e  elete all conten
1e934 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ts of the databa
1e935 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  se table or inde
1e936 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67  x whose root pag
1e937 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61  e.** in the data
1e938 62 61 73 65 20 66 69 6c 65 20 69 73 20 67 69 76  base file is giv
1e939 65 6e 20 62 79 20 50 31 2e 20 20 42 75 74 2c 20  en by P1.  But, 
1e93a 75 6e 6c 69 6b 65 20 44 65 73 74 72 6f 79 2c 20  unlike Destroy, 
1e93b 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f 76 65  do not.** remove
1e93c 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e   the table or in
1e93d 64 65 78 20 66 72 6f 6d 20 74 68 65 20 64 61 74  dex from the dat
1e93e 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
1e93f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e  * The table bein
1e940 67 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68  g clear is in th
1e941 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1e942 66 69 6c 65 20 69 66 20 50 32 3d 3d 30 2e 20 20  file if P2==0.  
1e943 49 66 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 65 6e  If.** P2==1 then
1e944 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
1e945 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65   clear is in the
1e946 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
1e947 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74  ase file.** that
1e948 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72   is used to stor
1e949 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 20  e tables create 
1e94a 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d  using CREATE TEM
1e94b 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a  PORARY TABLE..**
1e94c 0a 2a 2a 20 49 66 20 74 68 65 20 50 33 20 76 61  .** If the P3 va
1e94d 6c 75 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  lue is non-zero,
1e94e 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20   then the table 
1e94f 72 65 66 65 72 72 65 64 20 74 6f 20 6d 75 73 74  referred to must
1e950 20 62 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79   be an.** intkey
1e951 20 74 61 62 6c 65 20 28 61 6e 20 53 51 4c 20 74   table (an SQL t
1e952 61 62 6c 65 2c 20 6e 6f 74 20 61 6e 20 69 6e 64  able, not an ind
1e953 65 78 29 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ex). In this cas
1e954 65 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65  e the row change
1e955 20 0a 2a 2a 20 63 6f 75 6e 74 20 69 73 20 69 6e   .** count is in
1e956 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65  cremented by the
1e957 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
1e958 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  in the table bei
1e959 6e 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2a 20  ng cleared. .** 
1e95a 49 66 20 50 33 20 69 73 20 67 72 65 61 74 65 72  If P3 is greater
1e95b 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e   than zero, then
1e95c 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65   the value store
1e95d 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  d in register P3
1e95e 20 69 73 0a 2a 2a 20 61 6c 73 6f 20 69 6e 63 72   is.** also incr
1e95f 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e  emented by the n
1e960 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e  umber of rows in
1e961 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
1e962 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   cleared..**.** 
1e963 53 65 65 20 61 6c 73 6f 3a 20 44 65 73 74 72 6f  See also: Destro
1e964 79 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 65  y.*/.case OP_Cle
1e965 61 72 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  ar: {.#if 0  /* 
1e966 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e967 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 73 20  moved into u.bs 
1e968 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65  */.  int nChange
1e969 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1e96a 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e96b 64 20 69 6e 74 6f 20 75 2e 62 73 20 2a 2f 0a 0a  d into u.bs */..
1e96c 20 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 20 3d    u.bs.nChange =
1e96d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   0;.  assert( (p
1e96e 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1e96f 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d 30 20 29  <<pOp->p2))!=0 )
1e970 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1e971 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28  BtreeClearTable(
1e972 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 70  .      db->aDb[p
1e973 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 70 4f 70  Op->p2].pBt, pOp
1e974 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e 70 33 20 3f  ->p1, (pOp->p3 ?
1e975 20 26 75 2e 62 73 2e 6e 43 68 61 6e 67 65 20 3a   &u.bs.nChange :
1e976 20 30 29 0a 20 20 29 3b 0a 20 20 69 66 28 20 70   0).  );.  if( p
1e977 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 70 2d  Op->p3 ){.    p-
1e978 3e 6e 43 68 61 6e 67 65 20 2b 3d 20 75 2e 62 73  >nChange += u.bs
1e979 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 69 66  .nChange;.    if
1e97a 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 7b 0a 20  ( pOp->p3>0 ){. 
1e97b 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 70 4f 70       p->aMem[pOp
1e97c 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20 75 2e 62  ->p3].u.i += u.b
1e97d 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 7d  s.nChange;.    }
1e97e 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1e97f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61  ./* Opcode: Crea
1e980 74 65 54 61 62 6c 65 20 50 31 20 50 32 20 2a 20  teTable P1 P2 * 
1e981 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  * *.**.** Alloca
1e982 74 65 20 61 20 6e 65 77 20 74 61 62 6c 65 20 69  te a new table i
1e983 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
1e984 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d  ase file if P1==
1e985 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61  0 or in the.** a
1e986 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
1e987 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20  e file if P1==1 
1e988 6f 72 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65  or in an attache
1e989 64 20 64 61 74 61 62 61 73 65 20 69 66 0a 2a 2a  d database if.**
1e98a 20 50 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68   P1>1.  Write th
1e98b 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  e root page numb
1e98c 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61  er of the new ta
1e98d 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69  ble into.** regi
1e98e 73 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68  ster P2.**.** Th
1e98f 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  e difference bet
1e990 77 65 65 6e 20 61 20 74 61 62 6c 65 20 61 6e 64  ween a table and
1e991 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74 68 69   an index is thi
1e992 73 3a 20 20 41 20 74 61 62 6c 65 20 6d 75 73 74  s:  A table must
1e993 0a 2a 2a 20 68 61 76 65 20 61 20 34 2d 62 79 74  .** have a 4-byt
1e994 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 61 6e  e integer key an
1e995 64 20 63 61 6e 20 68 61 76 65 20 61 72 62 69 74  d can have arbit
1e996 72 61 72 79 20 64 61 74 61 2e 20 20 41 6e 20 69  rary data.  An i
1e997 6e 64 65 78 0a 2a 2a 20 68 61 73 20 61 6e 20 61  ndex.** has an a
1e998 72 62 69 74 72 61 72 79 20 6b 65 79 20 62 75 74  rbitrary key but
1e999 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20   no data..**.** 
1e99a 53 65 65 20 61 6c 73 6f 3a 20 43 72 65 61 74 65  See also: Create
1e99b 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  Index.*/./* Opco
1e99c 64 65 3a 20 43 72 65 61 74 65 49 6e 64 65 78 20  de: CreateIndex 
1e99d 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1e99e 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
1e99f 20 69 6e 64 65 78 20 69 6e 20 74 68 65 20 6d 61   index in the ma
1e9a0 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
1e9a1 20 69 66 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20   if P1==0 or in 
1e9a2 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79  the.** auxiliary
1e9a3 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1e9a4 66 20 50 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e  f P1==1 or in an
1e9a5 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
1e9a6 73 65 20 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20  se if.** P1>1.  
1e9a7 57 72 69 74 65 20 74 68 65 20 72 6f 6f 74 20 70  Write the root p
1e9a8 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
1e9a9 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 74 6f  e new table into
1e9aa 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 2e  .** register P2.
1e9ab 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f 63 75 6d  .**.** See docum
1e9ac 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43  entation on OP_C
1e9ad 72 65 61 74 65 54 61 62 6c 65 20 66 6f 72 20 61  reateTable for a
1e9ae 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1e9af 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ation..*/.case O
1e9b0 50 5f 43 72 65 61 74 65 49 6e 64 65 78 3a 20 20  P_CreateIndex:  
1e9b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74            /* out
1e9b2 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
1e9b3 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65 54 61  case OP_CreateTa
1e9b4 62 6c 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20  ble: {          
1e9b5 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1e9b6 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  se */.#if 0  /* 
1e9b7 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e9b8 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 74 20  moved into u.bt 
1e9b9 2a 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20  */.  int pgno;. 
1e9ba 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62   int flags;.  Db
1e9bb 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a   *pDb;.#endif /*
1e9bc 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e9bd 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 74   moved into u.bt
1e9be 20 2a 2f 0a 0a 20 20 75 2e 62 74 2e 70 67 6e 6f   */..  u.bt.pgno
1e9bf 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
1e9c0 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1e9c1 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  p->p1<db->nDb );
1e9c2 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62  .  assert( (p->b
1e9c3 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70  treeMask & (1<<p
1e9c4 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20  Op->p1))!=0 );. 
1e9c5 20 75 2e 62 74 2e 70 44 62 20 3d 20 26 64 62 2d   u.bt.pDb = &db-
1e9c6 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  >aDb[pOp->p1];. 
1e9c7 20 61 73 73 65 72 74 28 20 75 2e 62 74 2e 70 44   assert( u.bt.pD
1e9c8 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 69  b->pBt!=0 );.  i
1e9c9 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
1e9ca 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 29  OP_CreateTable )
1e9cb 7b 0a 20 20 20 20 2f 2a 20 75 2e 62 74 2e 66 6c  {.    /* u.bt.fl
1e9cc 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e 54 4b  ags = BTREE_INTK
1e9cd 45 59 3b 20 2a 2f 0a 20 20 20 20 75 2e 62 74 2e  EY; */.    u.bt.
1e9ce 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 4c 45  flags = BTREE_LE
1e9cf 41 46 44 41 54 41 7c 42 54 52 45 45 5f 49 4e 54  AFDATA|BTREE_INT
1e9d0 4b 45 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  KEY;.  }else{.  
1e9d1 20 20 75 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42    u.bt.flags = B
1e9d2 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b 0a 20  TREE_ZERODATA;. 
1e9d3 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
1e9d4 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c  3BtreeCreateTabl
1e9d5 65 28 75 2e 62 74 2e 70 44 62 2d 3e 70 42 74 2c  e(u.bt.pDb->pBt,
1e9d6 20 26 75 2e 62 74 2e 70 67 6e 6f 2c 20 75 2e 62   &u.bt.pgno, u.b
1e9d7 74 2e 66 6c 61 67 73 29 3b 0a 20 20 70 4f 75 74  t.flags);.  pOut
1e9d8 2d 3e 75 2e 69 20 3d 20 75 2e 62 74 2e 70 67 6e  ->u.i = u.bt.pgn
1e9d9 6f 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  o;.  MemSetTypeF
1e9da 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
1e9db 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
1e9dc 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72 73 65  /* Opcode: Parse
1e9dd 53 63 68 65 6d 61 20 50 31 20 50 32 20 2a 20 50  Schema P1 P2 * P
1e9de 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 61  4 *.**.** Read a
1e9df 6e 64 20 70 61 72 73 65 20 61 6c 6c 20 65 6e 74  nd parse all ent
1e9e0 72 69 65 73 20 66 72 6f 6d 20 74 68 65 20 53 51  ries from the SQ
1e9e1 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
1e9e2 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 50 31  e of database P1
1e9e3 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 20 74  .** that match t
1e9e4 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
1e9e5 50 34 2e 20 20 50 32 20 69 73 20 74 68 65 20 22  P4.  P2 is the "
1e9e6 66 6f 72 63 65 22 20 66 6c 61 67 2e 20 20 20 41  force" flag.   A
1e9e7 6c 77 61 79 73 20 64 6f 0a 2a 2a 20 74 68 65 20  lways do.** the 
1e9e8 70 61 72 73 69 6e 67 20 69 66 20 50 32 20 69 73  parsing if P2 is
1e9e9 20 74 72 75 65 2e 20 20 49 66 20 50 32 20 69 73   true.  If P2 is
1e9ea 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 69   false, then thi
1e9eb 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 0a 2a  s routine is a.*
1e9ec 2a 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73  * no-op if the s
1e9ed 63 68 65 6d 61 20 69 73 20 6e 6f 74 20 63 75 72  chema is not cur
1e9ee 72 65 6e 74 6c 79 20 6c 6f 61 64 65 64 2e 20 20  rently loaded.  
1e9ef 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
1e9f0 69 66 20 50 32 0a 2a 2a 20 69 73 20 66 61 6c 73  if P2.** is fals
1e9f1 65 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41  e, the SQLITE_MA
1e9f2 53 54 45 52 20 74 61 62 6c 65 20 69 73 20 6f 6e  STER table is on
1e9f3 6c 79 20 70 61 72 73 65 64 20 69 66 20 74 68 65  ly parsed if the
1e9f4 20 72 65 73 74 20 6f 66 20 74 68 65 0a 2a 2a 20   rest of the.** 
1e9f5 73 63 68 65 6d 61 20 69 73 20 61 6c 72 65 61 64  schema is alread
1e9f6 79 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 74 68  y loaded into th
1e9f7 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c 65 2e 0a  e symbol table..
1e9f8 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64  **.** This opcod
1e9f9 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 70 61  e invokes the pa
1e9fa 72 73 65 72 20 74 6f 20 63 72 65 61 74 65 20 61  rser to create a
1e9fb 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d 61 63   new virtual mac
1e9fc 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20 72 75  hine,.** then ru
1e9fd 6e 73 20 74 68 65 20 6e 65 77 20 76 69 72 74 75  ns the new virtu
1e9fe 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 74 20  al machine.  It 
1e9ff 69 73 20 74 68 75 73 20 61 20 72 65 2d 65 6e 74  is thus a re-ent
1ea00 72 61 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a  rant opcode..*/.
1ea01 63 61 73 65 20 4f 50 5f 50 61 72 73 65 53 63 68  case OP_ParseSch
1ea02 65 6d 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  ema: {.#if 0  /*
1ea03 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1ea04 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 75   moved into u.bu
1ea05 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20   */.  int iDb;. 
1ea06 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61   const char *zMa
1ea07 73 74 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a 53  ster;.  char *zS
1ea08 71 6c 3b 0a 20 20 49 6e 69 74 44 61 74 61 20 69  ql;.  InitData i
1ea09 6e 69 74 44 61 74 61 3b 0a 23 65 6e 64 69 66 20  nitData;.#endif 
1ea0a 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1ea0b 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1ea0c 62 75 20 2a 2f 0a 0a 20 20 75 2e 62 75 2e 69 44  bu */..  u.bu.iD
1ea0d 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61  b = pOp->p1;.  a
1ea0e 73 73 65 72 74 28 20 75 2e 62 75 2e 69 44 62 3e  ssert( u.bu.iDb>
1ea0f 3d 30 20 26 26 20 75 2e 62 75 2e 69 44 62 3c 64  =0 && u.bu.iDb<d
1ea10 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20  b->nDb );..  /* 
1ea11 49 66 20 70 4f 70 2d 3e 70 32 20 69 73 20 30 2c  If pOp->p2 is 0,
1ea12 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64   then this opcod
1ea13 65 20 69 73 20 62 65 69 6e 67 20 65 78 65 63 75  e is being execu
1ea14 74 65 64 20 74 6f 20 72 65 61 64 20 61 0a 20 20  ted to read a.  
1ea15 2a 2a 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20 66  ** single row, f
1ea16 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65 20 72  or example the r
1ea17 6f 77 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ow corresponding
1ea18 20 74 6f 20 61 20 6e 65 77 20 69 6e 64 65 78 0a   to a new index.
1ea19 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20    ** created by 
1ea1a 74 68 69 73 20 56 44 42 45 2c 20 66 72 6f 6d 20  this VDBE, from 
1ea1b 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  the sqlite_maste
1ea1c 72 20 74 61 62 6c 65 2e 20 49 74 20 6f 6e 6c 79  r table. It only
1ea1d 0a 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20  .  ** does this 
1ea1e 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  if the correspon
1ea1f 64 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 73  ding in-memory s
1ea20 63 68 65 6d 61 20 69 73 20 63 75 72 72 65 6e 74  chema is current
1ea21 6c 79 0a 20 20 2a 2a 20 6c 6f 61 64 65 64 2e 20  ly.  ** loaded. 
1ea22 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 6e  Otherwise, the n
1ea23 65 77 20 69 6e 64 65 78 20 64 65 66 69 6e 69 74  ew index definit
1ea24 69 6f 6e 20 63 61 6e 20 62 65 20 6c 6f 61 64 65  ion can be loade
1ea25 64 20 61 6c 6f 6e 67 0a 20 20 2a 2a 20 77 69 74  d along.  ** wit
1ea26 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68  h the rest of th
1ea27 65 20 73 63 68 65 6d 61 20 77 68 65 6e 20 69 74  e schema when it
1ea28 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20   is required..  
1ea29 2a 2a 0a 20 20 2a 2a 20 41 6c 74 68 6f 75 67 68  **.  ** Although
1ea2a 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 74 68   the mutex on th
1ea2b 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63  e BtShared objec
1ea2c 74 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e  t that correspon
1ea2d 64 73 20 74 6f 0a 20 20 2a 2a 20 64 61 74 61 62  ds to.  ** datab
1ea2e 61 73 65 20 75 2e 62 75 2e 69 44 62 20 28 74 68  ase u.bu.iDb (th
1ea2f 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  e database conta
1ea30 69 6e 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  ining the sqlite
1ea31 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20  _master table.  
1ea32 2a 2a 20 72 65 61 64 20 62 79 20 74 68 69 73 20  ** read by this 
1ea33 69 6e 73 74 72 75 63 74 69 6f 6e 29 20 69 73 20  instruction) is 
1ea34 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 2c 20  currently held, 
1ea35 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20  it is necessary 
1ea36 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74  to.  ** obtain t
1ea37 68 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 61 6c  he mutexes on al
1ea38 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  l attached datab
1ea39 61 73 65 73 20 62 65 66 6f 72 65 20 63 68 65 63  ases before chec
1ea3a 6b 69 6e 67 20 69 66 0a 20 20 2a 2a 20 74 68 65  king if.  ** the
1ea3b 20 73 63 68 65 6d 61 20 6f 66 20 75 2e 62 75 2e   schema of u.bu.
1ea3c 69 44 62 20 69 73 20 6c 6f 61 64 65 64 2e 20 54  iDb is loaded. T
1ea3d 68 69 73 20 69 73 20 62 65 63 61 75 73 65 2c 20  his is because, 
1ea3e 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a  at the start of.
1ea3f 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33    ** the sqlite3
1ea40 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 62 65 6c  _exec() call bel
1ea41 6f 77 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  ow, SQLite will 
1ea42 69 6e 76 6f 6b 65 0a 20 20 2a 2a 20 73 71 6c 69  invoke.  ** sqli
1ea43 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
1ea44 28 29 2e 20 49 66 20 61 6c 6c 20 6d 75 74 65 78  (). If all mutex
1ea45 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 72 65 61  es are not alrea
1ea46 64 79 20 68 65 6c 64 2c 20 74 68 65 0a 20 20 2a  dy held, the.  *
1ea47 2a 20 75 2e 62 75 2e 69 44 62 20 6d 75 74 65 78  * u.bu.iDb mutex
1ea48 20 6d 61 79 20 62 65 20 74 65 6d 70 6f 72 61 72   may be temporar
1ea49 69 6c 79 20 72 65 6c 65 61 73 65 64 20 74 6f 20  ily released to 
1ea4a 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2e 20  avoid deadlock. 
1ea4b 49 66 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 70  If.  ** this hap
1ea4c 70 65 6e 73 2c 20 74 68 65 6e 20 73 6f 6d 65 20  pens, then some 
1ea4d 6f 74 68 65 72 20 74 68 72 65 61 64 20 6d 61 79  other thread may
1ea4e 20 64 65 6c 65 74 65 20 74 68 65 20 69 6e 2d 6d   delete the in-m
1ea4f 65 6d 6f 72 79 0a 20 20 2a 2a 20 73 63 68 65 6d  emory.  ** schem
1ea50 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 75 2e  a of database u.
1ea51 62 75 2e 69 44 62 20 62 65 66 6f 72 65 20 74 68  bu.iDb before th
1ea52 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
1ea53 72 75 6e 73 2e 20 54 68 65 20 73 63 68 65 6d 61  runs. The schema
1ea54 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62  .  ** will not b
1ea55 65 20 72 65 6c 6f 61 64 65 64 20 62 65 63 75 61  e reloaded becua
1ea56 73 65 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e  se the db->init.
1ea57 62 75 73 79 20 66 6c 61 67 20 69 73 20 73 65 74  busy flag is set
1ea58 2e 20 54 68 69 73 0a 20 20 2a 2a 20 63 61 6e 20  . This.  ** can 
1ea59 72 65 73 75 6c 74 20 69 6e 20 61 20 22 6e 6f 20  result in a "no 
1ea5a 73 75 63 68 20 74 61 62 6c 65 3a 20 73 71 6c 69  such table: sqli
1ea5b 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 6d  te_master" or "m
1ea5c 61 6c 66 6f 72 6d 65 64 0a 20 20 2a 2a 20 64 61  alformed.  ** da
1ea5d 74 61 62 61 73 65 20 73 63 68 65 6d 61 22 20 65  tabase schema" e
1ea5e 72 72 6f 72 20 62 65 69 6e 67 20 72 65 74 75 72  rror being retur
1ea5f 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e  ned to the user.
1ea60 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
1ea61 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
1ea62 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b 75  sMutex(db->aDb[u
1ea63 2e 62 75 2e 69 44 62 5d 2e 70 42 74 29 20 29 3b  .bu.iDb].pBt) );
1ea64 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1ea65 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 69  nterAll(db);.  i
1ea66 66 28 20 70 4f 70 2d 3e 70 32 20 7c 7c 20 44 62  f( pOp->p2 || Db
1ea67 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20  HasProperty(db, 
1ea68 75 2e 62 75 2e 69 44 62 2c 20 44 42 5f 53 63 68  u.bu.iDb, DB_Sch
1ea69 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20  emaLoaded) ){.  
1ea6a 20 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 20 3d    u.bu.zMaster =
1ea6b 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 75 2e   SCHEMA_TABLE(u.
1ea6c 62 75 2e 69 44 62 29 3b 0a 20 20 20 20 75 2e 62  bu.iDb);.    u.b
1ea6d 75 2e 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20  u.initData.db = 
1ea6e 64 62 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e 69  db;.    u.bu.ini
1ea6f 74 44 61 74 61 2e 69 44 62 20 3d 20 70 4f 70 2d  tData.iDb = pOp-
1ea70 3e 70 31 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e  >p1;.    u.bu.in
1ea71 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 20  itData.pzErrMsg 
1ea72 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20  = &p->zErrMsg;. 
1ea73 20 20 20 75 2e 62 75 2e 7a 53 71 6c 20 3d 20 73     u.bu.zSql = s
1ea74 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
1ea75 2c 0a 20 20 20 20 20 20 20 22 53 45 4c 45 43 54  ,.       "SELECT
1ea76 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c   name, rootpage,
1ea77 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25   sql FROM '%q'.%
1ea78 73 20 57 48 45 52 45 20 25 73 22 2c 0a 20 20 20  s WHERE %s",.   
1ea79 20 20 20 20 64 62 2d 3e 61 44 62 5b 75 2e 62 75      db->aDb[u.bu
1ea7a 2e 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 75 2e 62  .iDb].zName, u.b
1ea7b 75 2e 7a 4d 61 73 74 65 72 2c 20 70 4f 70 2d 3e  u.zMaster, pOp->
1ea7c 70 34 2e 7a 29 3b 0a 20 20 20 20 69 66 28 20 75  p4.z);.    if( u
1ea7d 2e 62 75 2e 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20  .bu.zSql==0 ){. 
1ea7e 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1ea7f 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73  _NOMEM;.    }els
1ea80 65 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  e{.      (void)s
1ea81 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1ea82 64 62 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  db);.      asser
1ea83 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  t( db->init.busy
1ea84 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62 2d  ==0 );.      db-
1ea85 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a  >init.busy = 1;.
1ea86 20 20 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 44        u.bu.initD
1ea87 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f  ata.rc = SQLITE_
1ea88 4f 4b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  OK;.      assert
1ea89 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
1ea8a 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 63 20  led );.      rc 
1ea8b 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64  = sqlite3_exec(d
1ea8c 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 2c 20 73 71  b, u.bu.zSql, sq
1ea8d 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63  lite3InitCallbac
1ea8e 6b 2c 20 26 75 2e 62 75 2e 69 6e 69 74 44 61 74  k, &u.bu.initDat
1ea8f 61 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  a, 0);.      if(
1ea90 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1ea91 20 72 63 20 3d 20 75 2e 62 75 2e 69 6e 69 74 44   rc = u.bu.initD
1ea92 61 74 61 2e 72 63 3b 0a 20 20 20 20 20 20 73 71  ata.rc;.      sq
1ea93 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1ea94 75 2e 62 75 2e 7a 53 71 6c 29 3b 0a 20 20 20 20  u.bu.zSql);.    
1ea95 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20    db->init.busy 
1ea96 3d 20 30 3b 0a 20 20 20 20 20 20 28 76 6f 69 64  = 0;.      (void
1ea97 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
1ea98 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  (db);.    }.  }.
1ea99 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1ea9a 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66  aveAll(db);.  if
1ea9b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
1ea9c 45 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e  EM ){.    goto n
1ea9d 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72 65  o_mem;.  }.  bre
1ea9e 61 6b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69  ak;.}..#if !defi
1ea9f 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1eaa0 41 4e 41 4c 59 5a 45 29 0a 2f 2a 20 4f 70 63 6f  ANALYZE)./* Opco
1eaa1 64 65 3a 20 4c 6f 61 64 41 6e 61 6c 79 73 69 73  de: LoadAnalysis
1eaa2 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   P1 * * * *.**.*
1eaa3 2a 20 52 65 61 64 20 74 68 65 20 73 71 6c 69 74  * Read the sqlit
1eaa4 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 66 6f  e_stat1 table fo
1eaa5 72 20 64 61 74 61 62 61 73 65 20 50 31 20 61 6e  r database P1 an
1eaa6 64 20 6c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65  d load the conte
1eaa7 6e 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 61  nt.** of that ta
1eaa8 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 69 6e 74  ble into the int
1eaa9 65 72 6e 61 6c 20 69 6e 64 65 78 20 68 61 73 68  ernal index hash
1eaaa 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 77 69   table.  This wi
1eaab 6c 6c 20 63 61 75 73 65 0a 2a 2a 20 74 68 65 20  ll cause.** the 
1eaac 61 6e 61 6c 79 73 69 73 20 74 6f 20 62 65 20 75  analysis to be u
1eaad 73 65 64 20 77 68 65 6e 20 70 72 65 70 61 72 69  sed when prepari
1eaae 6e 67 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e  ng all subsequen
1eaaf 74 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 63 61  t queries..*/.ca
1eab0 73 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73  se OP_LoadAnalys
1eab1 69 73 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20  is: {.  assert( 
1eab2 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1eab3 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  p->p1<db->nDb );
1eab4 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
1eab5 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20  nalysisLoad(db, 
1eab6 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 62 72 65 61  pOp->p1);.  brea
1eab7 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  k;  .}.#endif /*
1eab8 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1eab9 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 20 2a  _OMIT_ANALYZE) *
1eaba 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72  /../* Opcode: Dr
1eabb 6f 70 54 61 62 6c 65 20 50 31 20 2a 20 2a 20 50  opTable P1 * * P
1eabc 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65  4 *.**.** Remove
1eabd 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69   the internal (i
1eabe 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73  n-memory) data s
1eabf 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64  tructures that d
1eac0 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 74  escribe.** the t
1eac1 61 62 6c 65 20 6e 61 6d 65 64 20 50 34 20 69 6e  able named P4 in
1eac2 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 54   database P1.  T
1eac3 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66  his is called af
1eac4 74 65 72 20 61 20 74 61 62 6c 65 0a 2a 2a 20 69  ter a table.** i
1eac5 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64  s dropped in ord
1eac6 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69  er to keep the i
1eac7 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e  nternal represen
1eac8 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  tation of the.**
1eac9 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65   schema consiste
1eaca 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20  nt with what is 
1eacb 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65  on disk..*/.case
1eacc 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 3a 20 7b   OP_DropTable: {
1eacd 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  .  sqlite3Unlink
1eace 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 64  AndDeleteTable(d
1eacf 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d  b, pOp->p1, pOp-
1ead0 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b  >p4.z);.  break;
1ead1 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44  .}../* Opcode: D
1ead2 72 6f 70 49 6e 64 65 78 20 50 31 20 2a 20 2a 20  ropIndex P1 * * 
1ead3 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76  P4 *.**.** Remov
1ead4 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28  e the internal (
1ead5 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20  in-memory) data 
1ead6 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
1ead7 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20  describe.** the 
1ead8 69 6e 64 65 78 20 6e 61 6d 65 64 20 50 34 20 69  index named P4 i
1ead9 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20  n database P1.  
1eada 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61  This is called a
1eadb 66 74 65 72 20 61 6e 20 69 6e 64 65 78 0a 2a 2a  fter an index.**
1eadc 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f   is dropped in o
1eadd 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65  rder to keep the
1eade 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73   internal repres
1eadf 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  entation of the.
1eae0 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73  ** schema consis
1eae1 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 20 69  tent with what i
1eae2 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61  s on disk..*/.ca
1eae3 73 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 3a  se OP_DropIndex:
1eae4 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69   {.  sqlite3Unli
1eae5 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78  nkAndDeleteIndex
1eae6 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  (db, pOp->p1, pO
1eae7 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61  p->p4.z);.  brea
1eae8 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1eae9 20 44 72 6f 70 54 72 69 67 67 65 72 20 50 31 20   DropTrigger P1 
1eaea 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52  * * P4 *.**.** R
1eaeb 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e  emove the intern
1eaec 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64  al (in-memory) d
1eaed 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74  ata structures t
1eaee 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20  hat describe.** 
1eaef 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65  the trigger name
1eaf0 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65  d P4 in database
1eaf1 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61   P1.  This is ca
1eaf2 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 72 69  lled after a tri
1eaf3 67 67 65 72 0a 2a 2a 20 69 73 20 64 72 6f 70 70  gger.** is dropp
1eaf4 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b  ed in order to k
1eaf5 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  eep the internal
1eaf6 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1eaf7 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61  of the.** schema
1eaf8 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68   consistent with
1eaf9 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b   what is on disk
1eafa 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f  ..*/.case OP_Dro
1eafb 70 54 72 69 67 67 65 72 3a 20 7b 0a 20 20 73 71  pTrigger: {.  sq
1eafc 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
1eafd 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20  leteTrigger(db, 
1eafe 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34  pOp->p1, pOp->p4
1eaff 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .z);.  break;.}.
1eb00 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1eb01 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
1eb02 43 48 45 43 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a  CHECK./* Opcode:
1eb03 20 49 6e 74 65 67 72 69 74 79 43 6b 20 50 31 20   IntegrityCk P1 
1eb04 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a  P2 P3 * P5.**.**
1eb05 20 44 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20   Do an analysis 
1eb06 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79  of the currently
1eb07 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20   open database. 
1eb08 20 53 74 6f 72 65 20 69 6e 0a 2a 2a 20 72 65 67   Store in.** reg
1eb09 69 73 74 65 72 20 50 31 20 74 68 65 20 74 65 78  ister P1 the tex
1eb0a 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65  t of an error me
1eb0b 73 73 61 67 65 20 64 65 73 63 72 69 62 69 6e 67  ssage describing
1eb0c 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a   any problems..*
1eb0d 2a 20 49 66 20 6e 6f 20 70 72 6f 62 6c 65 6d 73  * If no problems
1eb0e 20 61 72 65 20 66 6f 75 6e 64 2c 20 73 74 6f 72   are found, stor
1eb0f 65 20 61 20 4e 55 4c 4c 20 69 6e 20 72 65 67 69  e a NULL in regi
1eb10 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54  ster P1..**.** T
1eb11 68 65 20 72 65 67 69 73 74 65 72 20 50 33 20 63  he register P3 c
1eb12 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 78 69  ontains the maxi
1eb13 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c  mum number of al
1eb14 6c 6f 77 65 64 20 65 72 72 6f 72 73 2e 0a 2a 2a  lowed errors..**
1eb15 20 41 74 20 6d 6f 73 74 20 72 65 67 28 50 33 29   At most reg(P3)
1eb16 20 65 72 72 6f 72 73 20 77 69 6c 6c 20 62 65 20   errors will be 
1eb17 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 20 49 6e 20  reported..** In 
1eb18 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65  other words, the
1eb19 20 61 6e 61 6c 79 73 69 73 20 73 74 6f 70 73 20   analysis stops 
1eb1a 61 73 20 73 6f 6f 6e 20 61 73 20 72 65 67 28 50  as soon as reg(P
1eb1b 31 29 20 65 72 72 6f 72 73 20 61 72 65 20 0a 2a  1) errors are .*
1eb1c 2a 20 73 65 65 6e 2e 20 20 52 65 67 28 50 31 29  * seen.  Reg(P1)
1eb1d 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68   is updated with
1eb1e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
1eb1f 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 2e  rrors remaining.
1eb20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20  .**.** The root 
1eb21 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6f 66 20  page numbers of 
1eb22 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68  all tables in th
1eb23 65 20 64 61 74 61 62 61 73 65 20 61 72 65 20 69  e database are i
1eb24 6e 74 65 67 65 72 0a 2a 2a 20 73 74 6f 72 65 64  nteger.** stored
1eb25 20 69 6e 20 72 65 67 28 50 31 29 2c 20 72 65 67   in reg(P1), reg
1eb26 28 50 31 2b 31 29 2c 20 72 65 67 28 50 31 2b 32  (P1+1), reg(P1+2
1eb27 29 2c 20 2e 2e 2e 2e 20 20 54 68 65 72 65 20 61  ), ....  There a
1eb28 72 65 20 50 32 20 74 61 62 6c 65 73 0a 2a 2a 20  re P2 tables.** 
1eb29 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  total..**.** If 
1eb2a 50 35 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20  P5 is not zero, 
1eb2b 74 68 65 20 63 68 65 63 6b 20 69 73 20 64 6f 6e  the check is don
1eb2c 65 20 6f 6e 20 74 68 65 20 61 75 78 69 6c 69 61  e on the auxilia
1eb2d 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66  ry database.** f
1eb2e 69 6c 65 2c 20 6e 6f 74 20 74 68 65 20 6d 61 69  ile, not the mai
1eb2f 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
1eb30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
1eb31 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d  de is used to im
1eb32 70 6c 65 6d 65 6e 74 20 74 68 65 20 69 6e 74 65  plement the inte
1eb33 67 72 69 74 79 5f 63 68 65 63 6b 20 70 72 61 67  grity_check prag
1eb34 6d 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  ma..*/.case OP_I
1eb35 6e 74 65 67 72 69 74 79 43 6b 3a 20 7b 0a 23 69  ntegrityCk: {.#i
1eb36 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1eb37 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1eb38 74 6f 20 75 2e 62 76 20 2a 2f 0a 20 20 69 6e 74  to u.bv */.  int
1eb39 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20   nRoot;      /* 
1eb3a 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73  Number of tables
1eb3b 20 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d   to check.  (Num
1eb3c 62 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65  ber of root page
1eb3d 73 2e 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52  s.) */.  int *aR
1eb3e 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61  oot;     /* Arra
1eb3f 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75  y of rootpage nu
1eb40 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73  mbers for tables
1eb41 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a   to be checked *
1eb42 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20  /.  int j;      
1eb43 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1eb44 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72  ter */.  int nEr
1eb45 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  r;       /* Numb
1eb46 65 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70  er of errors rep
1eb47 6f 72 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  orted */.  char 
1eb48 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65  *z;        /* Te
1eb49 78 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  xt of the error 
1eb4a 72 65 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d 20  report */.  Mem 
1eb4b 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52  *pnErr;     /* R
1eb4c 65 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20  egister keeping 
1eb4d 74 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20  track of errors 
1eb4e 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 23 65 6e  remaining */.#en
1eb4f 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1eb50 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1eb51 6f 20 75 2e 62 76 20 2a 2f 0a 0a 20 20 75 2e 62  o u.bv */..  u.b
1eb52 76 2e 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70  v.nRoot = pOp->p
1eb53 32 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  2;.  assert( u.b
1eb54 76 2e 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20 75  v.nRoot>0 );.  u
1eb55 2e 62 76 2e 61 52 6f 6f 74 20 3d 20 73 71 6c 69  .bv.aRoot = sqli
1eb56 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
1eb57 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28  b, sizeof(int)*(
1eb58 75 2e 62 76 2e 6e 52 6f 6f 74 2b 31 29 20 29 3b  u.bv.nRoot+1) );
1eb59 0a 20 20 69 66 28 20 75 2e 62 76 2e 61 52 6f 6f  .  if( u.bv.aRoo
1eb5a 74 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  t==0 ) goto no_m
1eb5b 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  em;.  assert( pO
1eb5c 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p3>0 && pOp->
1eb5d 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p3<=p->nMem );. 
1eb5e 20 75 2e 62 76 2e 70 6e 45 72 72 20 3d 20 26 70   u.bv.pnErr = &p
1eb5f 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
1eb60 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 62 76  .  assert( (u.bv
1eb61 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20  .pnErr->flags & 
1eb62 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a 20  MEM_Int)!=0 );. 
1eb63 20 61 73 73 65 72 74 28 20 28 75 2e 62 76 2e 70   assert( (u.bv.p
1eb64 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20 28 4d  nErr->flags & (M
1eb65 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29  EM_Str|MEM_Blob)
1eb66 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 20 3d  )==0 );.  pIn1 =
1eb67 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1eb68 31 5d 3b 0a 20 20 66 6f 72 28 75 2e 62 76 2e 6a  1];.  for(u.bv.j
1eb69 3d 30 3b 20 75 2e 62 76 2e 6a 3c 75 2e 62 76 2e  =0; u.bv.j<u.bv.
1eb6a 6e 52 6f 6f 74 3b 20 75 2e 62 76 2e 6a 2b 2b 29  nRoot; u.bv.j++)
1eb6b 7b 0a 20 20 20 20 75 2e 62 76 2e 61 52 6f 6f 74  {.    u.bv.aRoot
1eb6c 5b 75 2e 62 76 2e 6a 5d 20 3d 20 28 69 6e 74 29  [u.bv.j] = (int)
1eb6d 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
1eb6e 6c 75 65 28 26 70 49 6e 31 5b 75 2e 62 76 2e 6a  lue(&pIn1[u.bv.j
1eb6f 5d 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 76 2e 61  ]);.  }.  u.bv.a
1eb70 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d 20 3d 20 30  Root[u.bv.j] = 0
1eb71 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1eb72 3e 70 35 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  >p5<db->nDb );. 
1eb73 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
1eb74 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70  eeMask & (1<<pOp
1eb75 2d 3e 70 35 29 29 21 3d 30 20 29 3b 0a 20 20 75  ->p5))!=0 );.  u
1eb76 2e 62 76 2e 7a 20 3d 20 73 71 6c 69 74 65 33 42  .bv.z = sqlite3B
1eb77 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65  treeIntegrityChe
1eb78 63 6b 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e  ck(db->aDb[pOp->
1eb79 70 35 5d 2e 70 42 74 2c 20 75 2e 62 76 2e 61 52  p5].pBt, u.bv.aR
1eb7a 6f 6f 74 2c 20 75 2e 62 76 2e 6e 52 6f 6f 74 2c  oot, u.bv.nRoot,
1eb7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1eb7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb7d 20 20 28 69 6e 74 29 75 2e 62 76 2e 70 6e 45 72    (int)u.bv.pnEr
1eb7e 72 2d 3e 75 2e 69 2c 20 26 75 2e 62 76 2e 6e 45  r->u.i, &u.bv.nE
1eb7f 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  rr);.  sqlite3Db
1eb80 46 72 65 65 28 64 62 2c 20 75 2e 62 76 2e 61 52  Free(db, u.bv.aR
1eb81 6f 6f 74 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45  oot);.  u.bv.pnE
1eb82 72 72 2d 3e 75 2e 69 20 2d 3d 20 75 2e 62 76 2e  rr->u.i -= u.bv.
1eb83 6e 45 72 72 3b 0a 20 20 73 71 6c 69 74 65 33 56  nErr;.  sqlite3V
1eb84 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 49  dbeMemSetNull(pI
1eb85 6e 31 29 3b 0a 20 20 69 66 28 20 75 2e 62 76 2e  n1);.  if( u.bv.
1eb86 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 61  nErr==0 ){.    a
1eb87 73 73 65 72 74 28 20 75 2e 62 76 2e 7a 3d 3d 30  ssert( u.bv.z==0
1eb88 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   );.  }else if( 
1eb89 75 2e 62 76 2e 7a 3d 3d 30 20 29 7b 0a 20 20 20  u.bv.z==0 ){.   
1eb8a 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1eb8b 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
1eb8c 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
1eb8d 70 49 6e 31 2c 20 75 2e 62 76 2e 7a 2c 20 2d 31  pIn1, u.bv.z, -1
1eb8e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 73  , SQLITE_UTF8, s
1eb8f 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
1eb90 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  }.  UPDATE_MAX_B
1eb91 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20  LOBSIZE(pIn1);. 
1eb92 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1eb93 67 65 45 6e 63 6f 64 69 6e 67 28 70 49 6e 31 2c  geEncoding(pIn1,
1eb94 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 62 72   encoding);.  br
1eb95 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1eb96 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
1eb97 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a  EGRITY_CHECK */.
1eb98 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53  ./* Opcode: RowS
1eb99 65 74 41 64 64 20 50 31 20 50 32 20 2a 20 2a 20  etAdd P1 P2 * * 
1eb9a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 74  *.**.** Insert t
1eb9b 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  he integer value
1eb9c 20 68 65 6c 64 20 62 79 20 72 65 67 69 73 74 65   held by registe
1eb9d 72 20 50 32 20 69 6e 74 6f 20 61 20 62 6f 6f 6c  r P2 into a bool
1eb9e 65 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 65 6c  ean index.** hel
1eb9f 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  d in register P1
1eba0 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 73 73 65 72  ..**.** An asser
1eba1 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 50 32  tion fails if P2
1eba2 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67   is not an integ
1eba3 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  er..*/.case OP_R
1eba4 6f 77 53 65 74 41 64 64 3a 20 7b 20 20 20 20 20  owSetAdd: {     
1eba5 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20    /* in2 */.#if 
1eba6 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1eba7 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1eba8 20 75 2e 62 77 20 2a 2f 0a 20 20 4d 65 6d 20 2a   u.bw */.  Mem *
1eba9 70 49 64 78 3b 0a 20 20 4d 65 6d 20 2a 70 56 61  pIdx;.  Mem *pVa
1ebaa 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  l;.#endif /* loc
1ebab 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1ebac 65 64 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a  ed into u.bw */.
1ebad 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1ebae 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d  1>0 && pOp->p1<=
1ebaf 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 62  p->nMem );.  u.b
1ebb0 77 2e 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65  w.pIdx = &p->aMe
1ebb1 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  m[pOp->p1];.  as
1ebb2 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1ebb3 26 26 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e  && pOp->p2<=p->n
1ebb4 4d 65 6d 20 29 3b 0a 20 20 75 2e 62 77 2e 70 56  Mem );.  u.bw.pV
1ebb5 61 6c 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  al = &p->aMem[pO
1ebb6 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74  p->p2];.  assert
1ebb7 28 20 28 75 2e 62 77 2e 70 56 61 6c 2d 3e 66 6c  ( (u.bw.pVal->fl
1ebb8 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d  ags & MEM_Int)!=
1ebb9 30 20 29 3b 0a 20 20 69 66 28 20 28 75 2e 62 77  0 );.  if( (u.bw
1ebba 2e 70 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d  .pIdx->flags & M
1ebbb 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b  EM_RowSet)==0 ){
1ebbc 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ebbd 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 75 2e 62  MemSetRowSet(u.b
1ebbe 77 2e 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28  w.pIdx);.    if(
1ebbf 20 28 75 2e 62 77 2e 70 49 64 78 2d 3e 66 6c 61   (u.bw.pIdx->fla
1ebc0 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
1ebc1 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ==0 ) goto no_me
1ebc2 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  m;.  }.  sqlite3
1ebc3 52 6f 77 53 65 74 49 6e 73 65 72 74 28 75 2e 62  RowSetInsert(u.b
1ebc4 77 2e 70 49 64 78 2d 3e 75 2e 70 52 6f 77 53 65  w.pIdx->u.pRowSe
1ebc5 74 2c 20 75 2e 62 77 2e 70 56 61 6c 2d 3e 75 2e  t, u.bw.pVal->u.
1ebc6 69 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  i);.  break;.}..
1ebc7 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65  /* Opcode: RowSe
1ebc8 74 52 65 61 64 20 50 31 20 50 32 20 50 33 20 2a  tRead P1 P2 P3 *
1ebc9 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74   *.**.** Extract
1ebca 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61   the smallest va
1ebcb 6c 75 65 20 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e  lue from boolean
1ebcc 20 69 6e 64 65 78 20 50 31 20 61 6e 64 20 70 75   index P1 and pu
1ebcd 74 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74  t that value int
1ebce 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33  o.** register P3
1ebcf 2e 20 20 4f 72 2c 20 69 66 20 62 6f 6f 6c 65 61  .  Or, if boolea
1ebd0 6e 20 69 6e 64 65 78 20 50 31 20 69 73 20 69 6e  n index P1 is in
1ebd1 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2c 20 6c  itially empty, l
1ebd2 65 61 76 65 20 50 33 0a 2a 2a 20 75 6e 63 68 61  eave P3.** uncha
1ebd3 6e 67 65 64 20 61 6e 64 20 6a 75 6d 70 20 74 6f  nged and jump to
1ebd4 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e   instruction P2.
1ebd5 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53  .*/.case OP_RowS
1ebd6 65 74 52 65 61 64 3a 20 7b 20 20 20 20 20 20 20  etRead: {       
1ebd7 2f 2a 20 6a 75 6d 70 2c 20 6f 75 74 33 20 2a 2f  /* jump, out3 */
1ebd8 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1ebd9 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ebda 20 69 6e 74 6f 20 75 2e 62 78 20 2a 2f 0a 20 20   into u.bx */.  
1ebdb 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 69 36 34  Mem *pIdx;.  i64
1ebdc 20 76 61 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   val;.#endif /* 
1ebdd 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1ebde 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78 20  moved into u.bx 
1ebdf 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1ebe0 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p1>0 && pOp->p
1ebe1 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  1<=p->nMem );.  
1ebe2 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52  CHECK_FOR_INTERR
1ebe3 55 50 54 3b 0a 20 20 75 2e 62 78 2e 70 49 64 78  UPT;.  u.bx.pIdx
1ebe4 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1ebe5 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26  >p1];.  pOut = &
1ebe6 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1ebe7 3b 0a 20 20 69 66 28 20 28 75 2e 62 78 2e 70 49  ;.  if( (u.bx.pI
1ebe8 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  dx->flags & MEM_
1ebe9 52 6f 77 53 65 74 29 3d 3d 30 0a 20 20 20 7c 7c  RowSet)==0.   ||
1ebea 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65   sqlite3RowSetNe
1ebeb 78 74 28 75 2e 62 78 2e 70 49 64 78 2d 3e 75 2e  xt(u.bx.pIdx->u.
1ebec 70 52 6f 77 53 65 74 2c 20 26 75 2e 62 78 2e 76  pRowSet, &u.bx.v
1ebed 61 6c 29 3d 3d 30 0a 20 20 29 7b 0a 20 20 20 20  al)==0.  ){.    
1ebee 2f 2a 20 54 68 65 20 62 6f 6f 6c 65 61 6e 20 69  /* The boolean i
1ebef 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20 2a 2f  ndex is empty */
1ebf0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ebf1 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 2e 62 78 2e  MemSetNull(u.bx.
1ebf2 70 49 64 78 29 3b 0a 20 20 20 20 70 63 20 3d 20  pIdx);.    pc = 
1ebf3 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d  pOp->p2 - 1;.  }
1ebf4 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 41 20 76  else{.    /* A v
1ebf5 61 6c 75 65 20 77 61 73 20 70 75 6c 6c 65 64 20  alue was pulled 
1ebf6 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 2a  from the index *
1ebf7 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  /.    assert( pO
1ebf8 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p3>0 && pOp->
1ebf9 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p3<=p->nMem );. 
1ebfa 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1ebfb 6d 53 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20  mSetInt64(pOut, 
1ebfc 75 2e 62 78 2e 76 61 6c 29 3b 0a 20 20 7d 0a 20  u.bx.val);.  }. 
1ebfd 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1ebfe 63 6f 64 65 3a 20 52 6f 77 53 65 74 54 65 73 74  code: RowSetTest
1ebff 20 50 31 20 50 32 20 50 33 20 50 34 0a 2a 2a 0a   P1 P2 P3 P4.**.
1ec00 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 69  ** Register P3 i
1ec01 73 20 61 73 73 75 6d 65 64 20 74 6f 20 68 6f 6c  s assumed to hol
1ec02 64 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67  d a 64-bit integ
1ec03 65 72 20 76 61 6c 75 65 2e 20 49 66 20 72 65 67  er value. If reg
1ec04 69 73 74 65 72 20 50 31 0a 2a 2a 20 63 6f 6e 74  ister P1.** cont
1ec05 61 69 6e 73 20 61 20 52 6f 77 53 65 74 20 6f 62  ains a RowSet ob
1ec06 6a 65 63 74 20 61 6e 64 20 74 68 61 74 20 52 6f  ject and that Ro
1ec07 77 53 65 74 20 6f 62 6a 65 63 74 20 63 6f 6e 74  wSet object cont
1ec08 61 69 6e 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75  ains.** the valu
1ec09 65 20 68 65 6c 64 20 69 6e 20 50 33 2c 20 6a 75  e held in P3, ju
1ec0a 6d 70 20 74 6f 20 72 65 67 69 73 74 65 72 20 50  mp to register P
1ec0b 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e  2. Otherwise, in
1ec0c 73 65 72 74 20 74 68 65 0a 2a 2a 20 69 6e 74 65  sert the.** inte
1ec0d 67 65 72 20 69 6e 20 50 33 20 69 6e 74 6f 20 74  ger in P3 into t
1ec0e 68 65 20 52 6f 77 53 65 74 20 61 6e 64 20 63 6f  he RowSet and co
1ec0f 6e 74 69 6e 75 65 20 6f 6e 20 74 6f 20 74 68 65  ntinue on to the
1ec10 0a 2a 2a 20 6e 65 78 74 20 6f 70 63 6f 64 65 2e  .** next opcode.
1ec11 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65  .**.** The RowSe
1ec12 74 20 6f 62 6a 65 63 74 20 69 73 20 6f 70 74 69  t object is opti
1ec13 6d 69 7a 65 64 20 66 6f 72 20 74 68 65 20 63 61  mized for the ca
1ec14 73 65 20 77 68 65 72 65 20 73 75 63 63 65 73 73  se where success
1ec15 69 76 65 20 73 65 74 73 0a 2a 2a 20 6f 66 20 69  ive sets.** of i
1ec16 6e 74 65 67 65 72 73 2c 20 77 68 65 72 65 20 65  ntegers, where e
1ec17 61 63 68 20 73 65 74 20 63 6f 6e 74 61 69 6e 73  ach set contains
1ec18 20 6e 6f 20 64 75 70 6c 69 63 61 74 65 73 2e 20   no duplicates. 
1ec19 45 61 63 68 20 73 65 74 0a 2a 2a 20 6f 66 20 76  Each set.** of v
1ec1a 61 6c 75 65 73 20 69 73 20 69 64 65 6e 74 69 66  alues is identif
1ec1b 69 65 64 20 62 79 20 61 20 75 6e 69 71 75 65 20  ied by a unique 
1ec1c 50 34 20 76 61 6c 75 65 2e 20 54 68 65 20 66 69  P4 value. The fi
1ec1d 72 73 74 20 73 65 74 0a 2a 2a 20 6d 75 73 74 20  rst set.** must 
1ec1e 68 61 76 65 20 50 34 3d 3d 30 2c 20 74 68 65 20  have P4==0, the 
1ec1f 66 69 6e 61 6c 20 73 65 74 20 50 34 3d 2d 31 2e  final set P4=-1.
1ec20 20 20 50 34 20 6d 75 73 74 20 62 65 20 65 69 74    P4 must be eit
1ec21 68 65 72 20 2d 31 20 6f 72 0a 2a 2a 20 6e 6f 6e  her -1 or.** non
1ec22 2d 6e 65 67 61 74 69 76 65 2e 20 20 46 6f 72 20  -negative.  For 
1ec23 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 76 61 6c  non-negative val
1ec24 75 65 73 20 6f 66 20 50 34 20 6f 6e 6c 79 20 74  ues of P4 only t
1ec25 68 65 20 6c 6f 77 65 72 20 34 0a 2a 2a 20 62 69  he lower 4.** bi
1ec26 74 73 20 61 72 65 20 73 69 67 6e 69 66 69 63 61  ts are significa
1ec27 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61  nt..**.** This a
1ec28 6c 6c 6f 77 73 20 6f 70 74 69 6d 69 7a 61 74 69  llows optimizati
1ec29 6f 6e 73 3a 20 28 61 29 20 77 68 65 6e 20 50 34  ons: (a) when P4
1ec2a 3d 3d 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ==0 there is no 
1ec2b 6e 65 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20  need to test.** 
1ec2c 74 68 65 20 72 6f 77 73 65 74 20 6f 62 6a 65 63  the rowset objec
1ec2d 74 20 66 6f 72 20 50 33 2c 20 61 73 20 69 74 20  t for P3, as it 
1ec2e 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f  is guaranteed no
1ec2f 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 69 74 2c  t to contain it,
1ec30 0a 2a 2a 20 28 62 29 20 77 68 65 6e 20 50 34 3d  .** (b) when P4=
1ec31 3d 2d 31 20 74 68 65 72 65 20 69 73 20 6e 6f 20  =-1 there is no 
1ec32 6e 65 65 64 20 74 6f 20 69 6e 73 65 72 74 20 74  need to insert t
1ec33 68 65 20 76 61 6c 75 65 2c 20 61 73 20 69 74 20  he value, as it 
1ec34 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 62 65  will.** never be
1ec35 20 74 65 73 74 65 64 20 66 6f 72 2c 20 61 6e 64   tested for, and
1ec36 20 28 63 29 20 77 68 65 6e 20 61 20 76 61 6c 75   (c) when a valu
1ec37 65 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f  e that is part o
1ec38 66 20 73 65 74 20 58 20 69 73 0a 2a 2a 20 69 6e  f set X is.** in
1ec39 73 65 72 74 65 64 2c 20 74 68 65 72 65 20 69 73  serted, there is
1ec3a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 73 65 61 72   no need to sear
1ec3b 63 68 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ch to see if the
1ec3c 20 73 61 6d 65 20 76 61 6c 75 65 20 77 61 73 0a   same value was.
1ec3d 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e  ** previously in
1ec3e 73 65 72 74 65 64 20 61 73 20 70 61 72 74 20 6f  serted as part o
1ec3f 66 20 73 65 74 20 58 20 28 6f 6e 6c 79 20 69 66  f set X (only if
1ec40 20 69 74 20 77 61 73 20 70 72 65 76 69 6f 75 73   it was previous
1ec41 6c 79 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 61  ly.** inserted a
1ec42 73 20 70 61 72 74 20 6f 66 20 73 6f 6d 65 20 6f  s part of some o
1ec43 74 68 65 72 20 73 65 74 29 2e 0a 2a 2f 0a 63 61  ther set)..*/.ca
1ec44 73 65 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74  se OP_RowSetTest
1ec45 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1ec46 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
1ec47 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69 66   in1, in3 */.#if
1ec48 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1ec49 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1ec4a 6f 20 75 2e 62 79 20 2a 2f 0a 20 20 69 6e 74 20  o u.by */.  int 
1ec4b 69 53 65 74 3b 0a 20 20 69 6e 74 20 65 78 69 73  iSet;.  int exis
1ec4c 74 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  ts;.#endif /* lo
1ec4d 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1ec4e 76 65 64 20 69 6e 74 6f 20 75 2e 62 79 20 2a 2f  ved into u.by */
1ec4f 0a 0a 20 20 75 2e 62 79 2e 69 53 65 74 20 3d 20  ..  u.by.iSet = 
1ec50 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 61 73 73  pOp->p4.i;.  ass
1ec51 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73  ert( pIn3->flags
1ec52 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 0a 20 20 2f  &MEM_Int );..  /
1ec53 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e  * If there is an
1ec54 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61  ything other tha
1ec55 6e 20 61 20 72 6f 77 73 65 74 20 6f 62 6a 65 63  n a rowset objec
1ec56 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  t in memory cell
1ec57 20 50 31 2c 0a 20 20 2a 2a 20 64 65 6c 65 74 65   P1,.  ** delete
1ec58 20 69 74 20 6e 6f 77 20 61 6e 64 20 69 6e 69 74   it now and init
1ec59 69 61 6c 69 7a 65 20 50 31 20 77 69 74 68 20 61  ialize P1 with a
1ec5a 6e 20 65 6d 70 74 79 20 72 6f 77 73 65 74 0a 20  n empty rowset. 
1ec5b 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d   */.  if( (pIn1-
1ec5c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
1ec5d 53 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73  Set)==0 ){.    s
1ec5e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1ec5f 52 6f 77 53 65 74 28 70 49 6e 31 29 3b 0a 20 20  RowSet(pIn1);.  
1ec60 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1ec61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
1ec62 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ==0 ) goto no_me
1ec63 6d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  m;.  }..  assert
1ec64 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1ec65 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 73 73  4_INT32 );.  ass
1ec66 65 72 74 28 20 75 2e 62 79 2e 69 53 65 74 3d 3d  ert( u.by.iSet==
1ec67 2d 31 20 7c 7c 20 75 2e 62 79 2e 69 53 65 74 3e  -1 || u.by.iSet>
1ec68 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 79  =0 );.  if( u.by
1ec69 2e 69 53 65 74 20 29 7b 0a 20 20 20 20 75 2e 62  .iSet ){.    u.b
1ec6a 79 2e 65 78 69 73 74 73 20 3d 20 73 71 6c 69 74  y.exists = sqlit
1ec6b 65 33 52 6f 77 53 65 74 54 65 73 74 28 70 49 6e  e3RowSetTest(pIn
1ec6c 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 0a 20 20  1->u.pRowSet,.  
1ec6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ec6e 20 20 20 20 20 20 20 20 20 20 20 20 20 28 75 38               (u8
1ec6f 29 28 75 2e 62 79 2e 69 53 65 74 3e 3d 30 20 3f  )(u.by.iSet>=0 ?
1ec70 20 75 2e 62 79 2e 69 53 65 74 20 26 20 30 78 66   u.by.iSet & 0xf
1ec71 20 3a 20 30 78 66 66 29 2c 0a 20 20 20 20 20 20   : 0xff),.      
1ec72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ec73 20 20 20 20 20 20 20 20 20 70 49 6e 33 2d 3e 75           pIn3->u
1ec74 2e 69 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62  .i);.    if( u.b
1ec75 79 2e 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20  y.exists ){.    
1ec76 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1ec77 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   1;.      break;
1ec78 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
1ec79 20 75 2e 62 79 2e 69 53 65 74 3e 3d 30 20 29 7b   u.by.iSet>=0 ){
1ec7a 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53  .    sqlite3RowS
1ec7b 65 74 49 6e 73 65 72 74 28 70 49 6e 31 2d 3e 75  etInsert(pIn1->u
1ec7c 2e 70 52 6f 77 53 65 74 2c 20 70 49 6e 33 2d 3e  .pRowSet, pIn3->
1ec7d 75 2e 69 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61  u.i);.  }.  brea
1ec7e 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  k;.}...#ifndef S
1ec7f 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
1ec80 45 52 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50  ER../* Opcode: P
1ec81 72 6f 67 72 61 6d 20 50 31 20 50 32 20 50 33 20  rogram P1 P2 P3 
1ec82 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75  P4 *.**.** Execu
1ec83 74 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70  te the trigger p
1ec84 72 6f 67 72 61 6d 20 70 61 73 73 65 64 20 61 73  rogram passed as
1ec85 20 50 34 20 28 74 79 70 65 20 50 34 5f 53 55 42   P4 (type P4_SUB
1ec86 50 52 4f 47 52 41 4d 29 2e 20 0a 2a 2a 0a 2a 2a  PROGRAM). .**.**
1ec87 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 74 68 65   P1 contains the
1ec88 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   address of the 
1ec89 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74  memory cell that
1ec8a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 69   contains the fi
1ec8b 72 73 74 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63  rst memory .** c
1ec8c 65 6c 6c 20 69 6e 20 61 6e 20 61 72 72 61 79 20  ell in an array 
1ec8d 6f 66 20 76 61 6c 75 65 73 20 75 73 65 64 20 61  of values used a
1ec8e 73 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  s arguments to t
1ec8f 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20  he sub-program. 
1ec90 50 32 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20  P2 .** contains 
1ec91 74 68 65 20 61 64 64 72 65 73 73 20 74 6f 20 6a  the address to j
1ec92 75 6d 70 20 74 6f 20 69 66 20 74 68 65 20 73 75  ump to if the su
1ec93 62 2d 70 72 6f 67 72 61 6d 20 74 68 72 6f 77 73  b-program throws
1ec94 20 61 6e 20 49 47 4e 4f 52 45 20 0a 2a 2a 20 65   an IGNORE .** e
1ec95 78 63 65 70 74 69 6f 6e 20 75 73 69 6e 67 20 74  xception using t
1ec96 68 65 20 52 41 49 53 45 28 29 20 66 75 6e 63 74  he RAISE() funct
1ec97 69 6f 6e 2e 20 52 65 67 69 73 74 65 72 20 50 33  ion. Register P3
1ec98 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 61 64   contains the ad
1ec99 64 72 65 73 73 20 0a 2a 2a 20 6f 66 20 61 20 6d  dress .** of a m
1ec9a 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 68  emory cell in th
1ec9b 69 73 20 28 74 68 65 20 70 61 72 65 6e 74 29 20  is (the parent) 
1ec9c 56 4d 20 74 68 61 74 20 69 73 20 75 73 65 64 20  VM that is used 
1ec9d 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20  to allocate the 
1ec9e 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72 65 71 75 69  .** memory requi
1ec9f 72 65 64 20 62 79 20 74 68 65 20 73 75 62 2d 76  red by the sub-v
1eca0 64 62 65 20 61 74 20 72 75 6e 74 69 6d 65 2e 0a  dbe at runtime..
1eca1 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f  **.** P4 is a po
1eca2 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 4d 20  inter to the VM 
1eca3 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
1eca4 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a  rigger program..
1eca5 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 72 6f 67 72  */.case OP_Progr
1eca6 61 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  am: {        /* 
1eca7 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f  jump */.#if 0  /
1eca8 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1eca9 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1ecaa 7a 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b  z */.  int nMem;
1ecab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ecac 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
1ecad 72 79 20 72 65 67 69 73 74 65 72 73 20 66 6f 72  ry registers for
1ecae 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a   sub-program */.
1ecaf 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20    int nByte;    
1ecb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
1ecb1 65 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 70  es of runtime sp
1ecb2 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72  ace required for
1ecb3 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a   sub-program */.
1ecb4 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20 20    Mem *pRt;     
1ecb5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1ecb6 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61 74  ister to allocat
1ecb7 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20  e runtime space 
1ecb8 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20  */.  Mem *pMem; 
1ecb9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ecba 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20  Used to iterate 
1ecbb 74 68 72 6f 75 67 68 20 6d 65 6d 6f 72 79 20 63  through memory c
1ecbc 65 6c 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  ells */.  Mem *p
1ecbd 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  End;            
1ecbe 20 20 2f 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79    /* Last memory
1ecbf 20 63 65 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72   cell in new arr
1ecc0 61 79 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 6d  ay */.  VdbeFram
1ecc1 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 20  e *pFrame;      
1ecc2 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61 6d  /* New vdbe fram
1ecc3 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e 20  e to execute in 
1ecc4 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20  */.  SubProgram 
1ecc5 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a 20  *pProgram;   /* 
1ecc6 53 75 62 2d 70 72 6f 67 72 61 6d 20 74 6f 20 65  Sub-program to e
1ecc7 78 65 63 75 74 65 20 2a 2f 0a 20 20 76 6f 69 64  xecute */.  void
1ecc8 20 2a 74 3b 20 20 20 20 20 20 20 20 20 20 20 20   *t;            
1ecc9 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 69 64 65      /* Token ide
1ecca 6e 74 69 66 79 69 6e 67 20 74 72 69 67 67 65 72  ntifying trigger
1eccb 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f   */.#endif /* lo
1eccc 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1eccd 76 65 64 20 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f  ved into u.bz */
1ecce 0a 0a 20 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61  ..  u.bz.pProgra
1eccf 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 6f  m = pOp->p4.pPro
1ecd0 67 72 61 6d 3b 0a 20 20 75 2e 62 7a 2e 70 52 74  gram;.  u.bz.pRt
1ecd1 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1ecd2 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p3];.  assert( 
1ecd3 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e  u.bz.pProgram->n
1ecd4 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  Op>0 );..  /* If
1ecd5 20 74 68 65 20 70 35 20 66 6c 61 67 20 69 73 20   the p5 flag is 
1ecd6 63 6c 65 61 72 2c 20 74 68 65 6e 20 72 65 63 75  clear, then recu
1ecd7 72 73 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e  rsive invocation
1ecd8 20 6f 66 20 74 72 69 67 67 65 72 73 20 69 73 0a   of triggers is.
1ecd9 20 20 2a 2a 20 64 69 73 61 62 6c 65 64 20 66 6f    ** disabled fo
1ecda 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  r backwards comp
1ecdb 61 74 69 62 69 6c 69 74 79 20 28 70 35 20 69 73  atibility (p5 is
1ecdc 20 73 65 74 20 69 66 20 74 68 69 73 20 73 75 62   set if this sub
1ecdd 2d 70 72 6f 67 72 61 6d 0a 20 20 2a 2a 20 69 73  -program.  ** is
1ecde 20 72 65 61 6c 6c 79 20 61 20 74 72 69 67 67 65   really a trigge
1ecdf 72 2c 20 6e 6f 74 20 61 20 66 6f 72 65 69 67 6e  r, not a foreign
1ece0 20 6b 65 79 20 61 63 74 69 6f 6e 2c 20 61 6e 64   key action, and
1ece1 20 74 68 65 20 66 6c 61 67 20 73 65 74 0a 20 20   the flag set.  
1ece2 2a 2a 20 61 6e 64 20 63 6c 65 61 72 65 64 20 62  ** and cleared b
1ece3 79 20 74 68 65 20 22 50 52 41 47 4d 41 20 72 65  y the "PRAGMA re
1ece4 63 75 72 73 69 76 65 5f 74 72 69 67 67 65 72 73  cursive_triggers
1ece5 22 20 63 6f 6d 6d 61 6e 64 20 69 73 20 63 6c 65  " command is cle
1ece6 61 72 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ar)..  **.  ** I
1ece7 74 20 69 73 20 72 65 63 75 72 73 69 76 65 20 69  t is recursive i
1ece8 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 72 69  nvocation of tri
1ece9 67 67 65 72 73 2c 20 61 74 20 74 68 65 20 53 51  ggers, at the SQ
1ecea 4c 20 6c 65 76 65 6c 2c 20 74 68 61 74 20 69 73  L level, that is
1eceb 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65 64 2e 20  .  ** disabled. 
1ecec 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 61 20  In some cases a 
1eced 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 6d  single trigger m
1ecee 61 79 20 67 65 6e 65 72 61 74 65 20 6d 6f 72 65  ay generate more
1ecef 20 74 68 61 6e 20 6f 6e 65 0a 20 20 2a 2a 20 53   than one.  ** S
1ecf0 75 62 50 72 6f 67 72 61 6d 20 28 69 66 20 74 68  ubProgram (if th
1ecf1 65 20 74 72 69 67 67 65 72 20 6d 61 79 20 62 65  e trigger may be
1ecf2 20 65 78 65 63 75 74 65 64 20 77 69 74 68 20 6d   executed with m
1ecf3 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 69 66  ore than one dif
1ecf4 66 65 72 65 6e 74 0a 20 20 2a 2a 20 4f 4e 20 43  ferent.  ** ON C
1ecf5 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68  ONFLICT algorith
1ecf6 6d 29 2e 20 53 75 62 50 72 6f 67 72 61 6d 20 73  m). SubProgram s
1ecf7 74 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69  tructures associ
1ecf8 61 74 65 64 20 77 69 74 68 20 61 0a 20 20 2a 2a  ated with a.  **
1ecf9 20 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20   single trigger 
1ecfa 61 6c 6c 20 68 61 76 65 20 74 68 65 20 73 61 6d  all have the sam
1ecfb 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  e value for the 
1ecfc 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e  SubProgram.token
1ecfd 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 2e 20  .  ** variable. 
1ecfe 20 2a 2f 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70   */.  if( pOp->p
1ecff 35 20 29 7b 0a 20 20 20 20 75 2e 62 7a 2e 74 20  5 ){.    u.bz.t 
1ed00 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d  = u.bz.pProgram-
1ed01 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28  >token;.    for(
1ed02 75 2e 62 7a 2e 70 46 72 61 6d 65 3d 70 2d 3e 70  u.bz.pFrame=p->p
1ed03 46 72 61 6d 65 3b 20 75 2e 62 7a 2e 70 46 72 61  Frame; u.bz.pFra
1ed04 6d 65 20 26 26 20 75 2e 62 7a 2e 70 46 72 61 6d  me && u.bz.pFram
1ed05 65 2d 3e 74 6f 6b 65 6e 21 3d 75 2e 62 7a 2e 74  e->token!=u.bz.t
1ed06 3b 20 75 2e 62 7a 2e 70 46 72 61 6d 65 3d 75 2e  ; u.bz.pFrame=u.
1ed07 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65  bz.pFrame->pPare
1ed08 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62  nt);.    if( u.b
1ed09 7a 2e 70 46 72 61 6d 65 20 29 20 62 72 65 61 6b  z.pFrame ) break
1ed0a 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e  ;.  }..  if( p->
1ed0b 6e 46 72 61 6d 65 3e 3d 64 62 2d 3e 61 4c 69 6d  nFrame>=db->aLim
1ed0c 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
1ed0d 54 52 49 47 47 45 52 5f 44 45 50 54 48 5d 20 29  TRIGGER_DEPTH] )
1ed0e 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
1ed0f 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c  E_ERROR;.    sql
1ed10 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1ed11 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
1ed12 74 6f 6f 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20  too many levels 
1ed13 6f 66 20 74 72 69 67 67 65 72 20 72 65 63 75 72  of trigger recur
1ed14 73 69 6f 6e 22 29 3b 0a 20 20 20 20 62 72 65 61  sion");.    brea
1ed15 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 67  k;.  }..  /* Reg
1ed16 69 73 74 65 72 20 75 2e 62 7a 2e 70 52 74 20 69  ister u.bz.pRt i
1ed17 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  s used to store 
1ed18 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69  the memory requi
1ed19 72 65 64 20 74 6f 20 73 61 76 65 20 74 68 65 20  red to save the 
1ed1a 73 74 61 74 65 0a 20 20 2a 2a 20 6f 66 20 74 68  state.  ** of th
1ed1b 65 20 63 75 72 72 65 6e 74 20 70 72 6f 67 72 61  e current progra
1ed1c 6d 2c 20 61 6e 64 20 74 68 65 20 6d 65 6d 6f 72  m, and the memor
1ed1d 79 20 72 65 71 75 69 72 65 64 20 61 74 20 72 75  y required at ru
1ed1e 6e 74 69 6d 65 20 74 6f 20 65 78 65 63 75 74 65  ntime to execute
1ed1f 0a 20 20 2a 2a 20 74 68 65 20 74 72 69 67 67 65  .  ** the trigge
1ed20 72 20 70 72 6f 67 72 61 6d 2e 20 49 66 20 74 68  r program. If th
1ed21 69 73 20 74 72 69 67 67 65 72 20 68 61 73 20 62  is trigger has b
1ed22 65 65 6e 20 66 69 72 65 64 20 62 65 66 6f 72 65  een fired before
1ed23 2c 20 74 68 65 6e 20 75 2e 62 7a 2e 70 52 74 0a  , then u.bz.pRt.
1ed24 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20    ** is already 
1ed25 61 6c 6c 6f 63 61 74 65 64 2e 20 4f 74 68 65 72  allocated. Other
1ed26 77 69 73 65 2c 20 69 74 20 6d 75 73 74 20 62 65  wise, it must be
1ed27 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a   initialized.  *
1ed28 2f 0a 20 20 69 66 28 20 28 75 2e 62 7a 2e 70 52  /.  if( (u.bz.pR
1ed29 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61  t->flags&MEM_Fra
1ed2a 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  me)==0 ){.    /*
1ed2b 20 53 75 62 50 72 6f 67 72 61 6d 2e 6e 4d 65 6d   SubProgram.nMem
1ed2c 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
1ed2d 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20  umber of memory 
1ed2e 63 65 6c 6c 73 20 75 73 65 64 20 62 79 20 74 68  cells used by th
1ed2f 65 0a 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d  e.    ** program
1ed30 20 73 74 6f 72 65 64 20 69 6e 20 53 75 62 50 72   stored in SubPr
1ed31 6f 67 72 61 6d 2e 61 4f 70 2e 20 41 73 20 77 65  ogram.aOp. As we
1ed32 6c 6c 20 61 73 20 74 68 65 73 65 2c 20 6f 6e 65  ll as these, one
1ed33 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 63   memory.    ** c
1ed34 65 6c 6c 20 69 73 20 72 65 71 75 69 72 65 64 20  ell is required 
1ed35 66 6f 72 20 65 61 63 68 20 63 75 72 73 6f 72 20  for each cursor 
1ed36 75 73 65 64 20 62 79 20 74 68 65 20 70 72 6f 67  used by the prog
1ed37 72 61 6d 2e 20 53 65 74 20 6c 6f 63 61 6c 0a 20  ram. Set local. 
1ed38 20 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 75     ** variable u
1ed39 2e 62 7a 2e 6e 4d 65 6d 20 28 61 6e 64 20 6c 61  .bz.nMem (and la
1ed3a 74 65 72 2c 20 56 64 62 65 46 72 61 6d 65 2e 6e  ter, VdbeFrame.n
1ed3b 43 68 69 6c 64 4d 65 6d 29 20 74 6f 20 74 68 69  ChildMem) to thi
1ed3c 73 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a  s value..    */.
1ed3d 20 20 20 20 75 2e 62 7a 2e 6e 4d 65 6d 20 3d 20      u.bz.nMem = 
1ed3e 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e  u.bz.pProgram->n
1ed3f 4d 65 6d 20 2b 20 75 2e 62 7a 2e 70 50 72 6f 67  Mem + u.bz.pProg
1ed40 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 20 75  ram->nCsr;.    u
1ed41 2e 62 7a 2e 6e 42 79 74 65 20 3d 20 52 4f 55 4e  .bz.nByte = ROUN
1ed42 44 38 28 73 69 7a 65 6f 66 28 56 64 62 65 46 72  D8(sizeof(VdbeFr
1ed43 61 6d 65 29 29 0a 20 20 20 20 20 20 20 20 20 20  ame)).          
1ed44 20 20 20 20 2b 20 75 2e 62 7a 2e 6e 4d 65 6d 20      + u.bz.nMem 
1ed45 2a 20 73 69 7a 65 6f 66 28 4d 65 6d 29 0a 20 20  * sizeof(Mem).  
1ed46 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 75 2e              + u.
1ed47 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73  bz.pProgram->nCs
1ed48 72 20 2a 20 73 69 7a 65 6f 66 28 56 64 62 65 43  r * sizeof(VdbeC
1ed49 75 72 73 6f 72 20 2a 29 3b 0a 20 20 20 20 75 2e  ursor *);.    u.
1ed4a 62 7a 2e 70 46 72 61 6d 65 20 3d 20 73 71 6c 69  bz.pFrame = sqli
1ed4b 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
1ed4c 64 62 2c 20 75 2e 62 7a 2e 6e 42 79 74 65 29 3b  db, u.bz.nByte);
1ed4d 0a 20 20 20 20 69 66 28 20 21 75 2e 62 7a 2e 70  .    if( !u.bz.p
1ed4e 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 67  Frame ){.      g
1ed4f 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20  oto no_mem;.    
1ed50 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
1ed51 65 4d 65 6d 52 65 6c 65 61 73 65 28 75 2e 62 7a  eMemRelease(u.bz
1ed52 2e 70 52 74 29 3b 0a 20 20 20 20 75 2e 62 7a 2e  .pRt);.    u.bz.
1ed53 70 52 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  pRt->flags = MEM
1ed54 5f 46 72 61 6d 65 3b 0a 20 20 20 20 75 2e 62 7a  _Frame;.    u.bz
1ed55 2e 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65 20 3d  .pRt->u.pFrame =
1ed56 20 75 2e 62 7a 2e 70 46 72 61 6d 65 3b 0a 0a 20   u.bz.pFrame;.. 
1ed57 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e     u.bz.pFrame->
1ed58 76 20 3d 20 70 3b 0a 20 20 20 20 75 2e 62 7a 2e  v = p;.    u.bz.
1ed59 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65  pFrame->nChildMe
1ed5a 6d 20 3d 20 75 2e 62 7a 2e 6e 4d 65 6d 3b 0a 20  m = u.bz.nMem;. 
1ed5b 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e     u.bz.pFrame->
1ed5c 6e 43 68 69 6c 64 43 73 72 20 3d 20 75 2e 62 7a  nChildCsr = u.bz
1ed5d 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b  .pProgram->nCsr;
1ed5e 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65  .    u.bz.pFrame
1ed5f 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 75  ->pc = pc;.    u
1ed60 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d  .bz.pFrame->aMem
1ed61 20 3d 20 70 2d 3e 61 4d 65 6d 3b 0a 20 20 20 20   = p->aMem;.    
1ed62 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65  u.bz.pFrame->nMe
1ed63 6d 20 3d 20 70 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  m = p->nMem;.   
1ed64 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 70   u.bz.pFrame->ap
1ed65 43 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 3b 0a  Csr = p->apCsr;.
1ed66 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d      u.bz.pFrame-
1ed67 3e 6e 43 75 72 73 6f 72 20 3d 20 70 2d 3e 6e 43  >nCursor = p->nC
1ed68 75 72 73 6f 72 3b 0a 20 20 20 20 75 2e 62 7a 2e  ursor;.    u.bz.
1ed69 70 46 72 61 6d 65 2d 3e 61 4f 70 20 3d 20 70 2d  pFrame->aOp = p-
1ed6a 3e 61 4f 70 3b 0a 20 20 20 20 75 2e 62 7a 2e 70  >aOp;.    u.bz.p
1ed6b 46 72 61 6d 65 2d 3e 6e 4f 70 20 3d 20 70 2d 3e  Frame->nOp = p->
1ed6c 6e 4f 70 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46  nOp;.    u.bz.pF
1ed6d 72 61 6d 65 2d 3e 74 6f 6b 65 6e 20 3d 20 75 2e  rame->token = u.
1ed6e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b  bz.pProgram->tok
1ed6f 65 6e 3b 0a 0a 20 20 20 20 75 2e 62 7a 2e 70 45  en;..    u.bz.pE
1ed70 6e 64 20 3d 20 26 56 64 62 65 46 72 61 6d 65 4d  nd = &VdbeFrameM
1ed71 65 6d 28 75 2e 62 7a 2e 70 46 72 61 6d 65 29 5b  em(u.bz.pFrame)[
1ed72 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68  u.bz.pFrame->nCh
1ed73 69 6c 64 4d 65 6d 5d 3b 0a 20 20 20 20 66 6f 72  ildMem];.    for
1ed74 28 75 2e 62 7a 2e 70 4d 65 6d 3d 56 64 62 65 46  (u.bz.pMem=VdbeF
1ed75 72 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72  rameMem(u.bz.pFr
1ed76 61 6d 65 29 3b 20 75 2e 62 7a 2e 70 4d 65 6d 21  ame); u.bz.pMem!
1ed77 3d 75 2e 62 7a 2e 70 45 6e 64 3b 20 75 2e 62 7a  =u.bz.pEnd; u.bz
1ed78 2e 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  .pMem++){.      
1ed79 75 2e 62 7a 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73  u.bz.pMem->flags
1ed7a 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
1ed7b 20 20 20 75 2e 62 7a 2e 70 4d 65 6d 2d 3e 64 62     u.bz.pMem->db
1ed7c 20 3d 20 64 62 3b 0a 20 20 20 20 7d 0a 20 20 7d   = db;.    }.  }
1ed7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62 7a 2e 70  else{.    u.bz.p
1ed7e 46 72 61 6d 65 20 3d 20 75 2e 62 7a 2e 70 52 74  Frame = u.bz.pRt
1ed7f 2d 3e 75 2e 70 46 72 61 6d 65 3b 0a 20 20 20 20  ->u.pFrame;.    
1ed80 61 73 73 65 72 74 28 20 75 2e 62 7a 2e 70 50 72  assert( u.bz.pPr
1ed81 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 2b 75 2e 62 7a  ogram->nMem+u.bz
1ed82 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d  .pProgram->nCsr=
1ed83 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43  =u.bz.pFrame->nC
1ed84 68 69 6c 64 4d 65 6d 20 29 3b 0a 20 20 20 20 61  hildMem );.    a
1ed85 73 73 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f  ssert( u.bz.pPro
1ed86 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 62 7a  gram->nCsr==u.bz
1ed87 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43  .pFrame->nChildC
1ed88 73 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  sr );.    assert
1ed89 28 20 70 63 3d 3d 75 2e 62 7a 2e 70 46 72 61 6d  ( pc==u.bz.pFram
1ed8a 65 2d 3e 70 63 20 29 3b 0a 20 20 7d 0a 0a 20 20  e->pc );.  }..  
1ed8b 70 2d 3e 6e 46 72 61 6d 65 2b 2b 3b 0a 20 20 75  p->nFrame++;.  u
1ed8c 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72  .bz.pFrame->pPar
1ed8d 65 6e 74 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b  ent = p->pFrame;
1ed8e 0a 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e  .  u.bz.pFrame->
1ed8f 6c 61 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e  lastRowid = db->
1ed90 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 75 2e 62  lastRowid;.  u.b
1ed91 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67  z.pFrame->nChang
1ed92 65 20 3d 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a  e = p->nChange;.
1ed93 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30    p->nChange = 0
1ed94 3b 0a 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20  ;.  p->pFrame = 
1ed95 75 2e 62 7a 2e 70 46 72 61 6d 65 3b 0a 20 20 70  u.bz.pFrame;.  p
1ed96 2d 3e 61 4d 65 6d 20 3d 20 26 56 64 62 65 46 72  ->aMem = &VdbeFr
1ed97 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72 61  ameMem(u.bz.pFra
1ed98 6d 65 29 5b 2d 31 5d 3b 0a 20 20 70 2d 3e 6e 4d  me)[-1];.  p->nM
1ed99 65 6d 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d 65  em = u.bz.pFrame
1ed9a 2d 3e 6e 43 68 69 6c 64 4d 65 6d 3b 0a 20 20 70  ->nChildMem;.  p
1ed9b 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36  ->nCursor = (u16
1ed9c 29 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43  )u.bz.pFrame->nC
1ed9d 68 69 6c 64 43 73 72 3b 0a 20 20 70 2d 3e 61 70  hildCsr;.  p->ap
1ed9e 43 73 72 20 3d 20 28 56 64 62 65 43 75 72 73 6f  Csr = (VdbeCurso
1ed9f 72 20 2a 2a 29 26 70 2d 3e 61 4d 65 6d 5b 70 2d  r **)&p->aMem[p-
1eda0 3e 6e 4d 65 6d 2b 31 5d 3b 0a 20 20 70 2d 3e 61  >nMem+1];.  p->a
1eda1 4f 70 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72  Op = u.bz.pProgr
1eda2 61 6d 2d 3e 61 4f 70 3b 0a 20 20 70 2d 3e 6e 4f  am->aOp;.  p->nO
1eda3 70 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61  p = u.bz.pProgra
1eda4 6d 2d 3e 6e 4f 70 3b 0a 20 20 70 63 20 3d 20 2d  m->nOp;.  pc = -
1eda5 31 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  1;..  break;.}..
1eda6 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72 61 6d  /* Opcode: Param
1eda7 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1eda8 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  ** This opcode i
1eda9 73 20 6f 6e 6c 79 20 65 76 65 72 20 70 72 65 73  s only ever pres
1edaa 65 6e 74 20 69 6e 20 73 75 62 2d 70 72 6f 67 72  ent in sub-progr
1edab 61 6d 73 20 63 61 6c 6c 65 64 20 76 69 61 20 74  ams called via t
1edac 68 65 20 0a 2a 2a 20 4f 50 5f 50 72 6f 67 72 61  he .** OP_Progra
1edad 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 43  m instruction. C
1edae 6f 70 79 20 61 20 76 61 6c 75 65 20 63 75 72 72  opy a value curr
1edaf 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20  ently stored in 
1edb0 61 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c  a memory .** cel
1edb1 6c 20 6f 66 20 74 68 65 20 63 61 6c 6c 69 6e 67  l of the calling
1edb2 20 28 70 61 72 65 6e 74 29 20 66 72 61 6d 65 20   (parent) frame 
1edb3 74 6f 20 63 65 6c 6c 20 50 32 20 69 6e 20 74 68  to cell P2 in th
1edb4 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 73  e current frames
1edb5 20 0a 2a 2a 20 61 64 64 72 65 73 73 20 73 70 61   .** address spa
1edb6 63 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  ce. This is used
1edb7 20 62 79 20 74 72 69 67 67 65 72 20 70 72 6f 67   by trigger prog
1edb8 72 61 6d 73 20 74 6f 20 61 63 63 65 73 73 20 74  rams to access t
1edb9 68 65 20 6e 65 77 2e 2a 20 0a 2a 2a 20 61 6e 64  he new.* .** and
1edba 20 6f 6c 64 2e 2a 20 76 61 6c 75 65 73 2e 0a 2a   old.* values..*
1edbb 2a 0a 2a 2a 20 54 68 65 20 61 64 64 72 65 73 73  *.** The address
1edbc 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 6e 20   of the cell in 
1edbd 74 68 65 20 70 61 72 65 6e 74 20 66 72 61 6d 65  the parent frame
1edbe 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
1edbf 79 20 61 64 64 69 6e 67 0a 2a 2a 20 74 68 65 20  y adding.** the 
1edc0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 31 20  value of the P1 
1edc1 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
1edc2 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 31 20  value of the P1 
1edc3 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a  argument to the.
1edc4 2a 2a 20 63 61 6c 6c 69 6e 67 20 4f 50 5f 50 72  ** calling OP_Pr
1edc5 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f  ogram instructio
1edc6 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61  n..*/.case OP_Pa
1edc7 72 61 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20  ram: {          
1edc8 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
1edc9 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  ase */.#if 0  /*
1edca 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1edcb 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61   moved into u.ca
1edcc 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 6d 65 20   */.  VdbeFrame 
1edcd 2a 70 46 72 61 6d 65 3b 0a 20 20 4d 65 6d 20 2a  *pFrame;.  Mem *
1edce 70 49 6e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  pIn;.#endif /* l
1edcf 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1edd0 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20 2a  oved into u.ca *
1edd1 2f 0a 20 20 75 2e 63 61 2e 70 46 72 61 6d 65 20  /.  u.ca.pFrame 
1edd2 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 75  = p->pFrame;.  u
1edd3 2e 63 61 2e 70 49 6e 20 3d 20 26 75 2e 63 61 2e  .ca.pIn = &u.ca.
1edd4 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70  pFrame->aMem[pOp
1edd5 2d 3e 70 31 20 2b 20 75 2e 63 61 2e 70 46 72 61  ->p1 + u.ca.pFra
1edd6 6d 65 2d 3e 61 4f 70 5b 75 2e 63 61 2e 70 46 72  me->aOp[u.ca.pFr
1edd7 61 6d 65 2d 3e 70 63 5d 2e 70 31 5d 3b 0a 20 20  ame->pc].p1];.  
1edd8 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68  sqlite3VdbeMemSh
1edd9 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20  allowCopy(pOut, 
1edda 75 2e 63 61 2e 70 49 6e 2c 20 4d 45 4d 5f 45 70  u.ca.pIn, MEM_Ep
1eddb 68 65 6d 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  hem);.  break;.}
1eddc 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e  ..#endif /* #ifn
1eddd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1edde 54 52 49 47 47 45 52 20 2a 2f 0a 0a 23 69 66 6e  TRIGGER */..#ifn
1eddf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ede0 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 2f 2a 20 4f  FOREIGN_KEY./* O
1ede1 70 63 6f 64 65 3a 20 46 6b 43 6f 75 6e 74 65 72  pcode: FkCounter
1ede2 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1ede3 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 61 20 22  ** Increment a "
1ede4 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
1ede5 65 72 22 20 62 79 20 50 32 20 28 50 32 20 6d 61  er" by P2 (P2 ma
1ede6 79 20 62 65 20 6e 65 67 61 74 69 76 65 20 6f 72  y be negative or
1ede7 20 70 6f 73 69 74 69 76 65 29 2e 0a 2a 2a 20 49   positive)..** I
1ede8 66 20 50 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  f P1 is non-zero
1ede9 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  , the database c
1edea 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65  onstraint counte
1edeb 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  r is incremented
1edec 20 0a 2a 2a 20 28 64 65 66 65 72 72 65 64 20 66   .** (deferred f
1eded 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
1edee 72 61 69 6e 74 73 29 2e 20 4f 74 68 65 72 77 69  raints). Otherwi
1edef 73 65 2c 20 69 66 20 50 31 20 69 73 20 7a 65 72  se, if P1 is zer
1edf0 6f 2c 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65  o, the .** state
1edf1 6d 65 6e 74 20 63 6f 75 6e 74 65 72 20 69 73 20  ment counter is 
1edf2 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 69 6d 6d  incremented (imm
1edf3 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b  ediate foreign k
1edf4 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 29 2e  ey constraints).
1edf5 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6b 43 6f  .*/.case OP_FkCo
1edf6 75 6e 74 65 72 3a 20 7b 0a 20 20 69 66 28 20 70  unter: {.  if( p
1edf7 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 64 62  Op->p1 ){.    db
1edf8 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20  ->nDeferredCons 
1edf9 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 65  += pOp->p2;.  }e
1edfa 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6e 46 6b 43  lse{.    p->nFkC
1edfb 6f 6e 73 74 72 61 69 6e 74 20 2b 3d 20 70 4f 70  onstraint += pOp
1edfc 2d 3e 70 32 3b 0a 20 20 7d 0a 20 20 62 72 65 61  ->p2;.  }.  brea
1edfd 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1edfe 20 46 6b 49 66 5a 65 72 6f 20 50 31 20 50 32 20   FkIfZero P1 P2 
1edff 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  * * *.**.** This
1ee00 20 6f 70 63 6f 64 65 20 74 65 73 74 73 20 69 66   opcode tests if
1ee01 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63   a foreign key c
1ee02 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65  onstraint-counte
1ee03 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 7a  r is currently z
1ee04 65 72 6f 2e 0a 2a 2a 20 49 66 20 73 6f 2c 20 6a  ero..** If so, j
1ee05 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69  ump to instructi
1ee06 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65  on P2. Otherwise
1ee07 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  , fall through t
1ee08 6f 20 74 68 65 20 6e 65 78 74 20 0a 2a 2a 20 69  o the next .** i
1ee09 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nstruction..**.*
1ee0a 2a 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d 7a  * If P1 is non-z
1ee0b 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a 75  ero, then the ju
1ee0c 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74  mp is taken if t
1ee0d 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 73  he database cons
1ee0e 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 0a 2a  traint-counter.*
1ee0f 2a 20 69 73 20 7a 65 72 6f 20 28 74 68 65 20 6f  * is zero (the o
1ee10 6e 65 20 74 68 61 74 20 63 6f 75 6e 74 73 20 64  ne that counts d
1ee11 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
1ee12 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73 29 2e 20  nt violations). 
1ee13 49 66 20 50 31 20 69 73 0a 2a 2a 20 7a 65 72 6f  If P1 is.** zero
1ee14 2c 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  , the jump is ta
1ee15 6b 65 6e 20 69 66 20 74 68 65 20 73 74 61 74 65  ken if the state
1ee16 6d 65 6e 74 20 63 6f 6e 73 74 72 61 69 6e 74 2d  ment constraint-
1ee17 63 6f 75 6e 74 65 72 20 69 73 20 7a 65 72 6f 0a  counter is zero.
1ee18 2a 2a 20 28 69 6d 6d 65 64 69 61 74 65 20 66 6f  ** (immediate fo
1ee19 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
1ee1a 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73 29  aint violations)
1ee1b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6b 49  ..*/.case OP_FkI
1ee1c 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20 20 20  fZero: {        
1ee1d 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 66   /* jump */.  if
1ee1e 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20  ( pOp->p1 ){.   
1ee1f 20 69 66 28 20 64 62 2d 3e 6e 44 65 66 65 72 72   if( db->nDeferr
1ee20 65 64 43 6f 6e 73 3d 3d 30 20 29 20 70 63 20 3d  edCons==0 ) pc =
1ee21 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 65   pOp->p2-1;.  }e
1ee22 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  lse{.    if( p->
1ee23 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3d 3d 30  nFkConstraint==0
1ee24 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d   ) pc = pOp->p2-
1ee25 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1ee26 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e  }.#endif /* #ifn
1ee27 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ee28 46 4f 52 45 49 47 4e 5f 4b 45 59 20 2a 2f 0a 0a  FOREIGN_KEY */..
1ee29 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ee2a 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
1ee2b 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 65 6d  T./* Opcode: Mem
1ee2c 4d 61 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  Max P1 P2 * * *.
1ee2d 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 72 65  **.** P1 is a re
1ee2e 67 69 73 74 65 72 20 69 6e 20 74 68 65 20 72 6f  gister in the ro
1ee2f 6f 74 20 66 72 61 6d 65 20 6f 66 20 74 68 69 73  ot frame of this
1ee30 20 56 4d 20 28 74 68 65 20 72 6f 6f 74 20 66 72   VM (the root fr
1ee31 61 6d 65 20 69 73 0a 2a 2a 20 64 69 66 66 65 72  ame is.** differ
1ee32 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 63 75 72  ent from the cur
1ee33 72 65 6e 74 20 66 72 61 6d 65 20 69 66 20 74 68  rent frame if th
1ee34 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  is instruction i
1ee35 73 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64  s being executed
1ee36 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 73 75 62  .** within a sub
1ee37 2d 70 72 6f 67 72 61 6d 29 2e 20 53 65 74 20 74  -program). Set t
1ee38 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69  he value of regi
1ee39 73 74 65 72 20 50 31 20 74 6f 20 74 68 65 20 6d  ster P1 to the m
1ee3a 61 78 69 6d 75 6d 20 6f 66 20 0a 2a 2a 20 69 74  aximum of .** it
1ee3b 73 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  s current value 
1ee3c 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e  and the value in
1ee3d 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a   register P2..**
1ee3e 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63  .** This instruc
1ee3f 74 69 6f 6e 20 74 68 72 6f 77 73 20 61 6e 20 65  tion throws an e
1ee40 72 72 6f 72 20 69 66 20 74 68 65 20 6d 65 6d 6f  rror if the memo
1ee41 72 79 20 63 65 6c 6c 20 69 73 20 6e 6f 74 20 69  ry cell is not i
1ee42 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 61 6e 20 69  nitially.** an i
1ee43 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 73 65 20  nteger..*/.case 
1ee44 4f 50 5f 4d 65 6d 4d 61 78 3a 20 7b 20 20 20 20  OP_MemMax: {    
1ee45 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69      /* in2 */.#i
1ee46 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1ee47 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ee48 74 6f 20 75 2e 63 62 20 2a 2f 0a 20 20 4d 65 6d  to u.cb */.  Mem
1ee49 20 2a 70 49 6e 31 3b 0a 20 20 56 64 62 65 46 72   *pIn1;.  VdbeFr
1ee4a 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 23 65 6e  ame *pFrame;.#en
1ee4b 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1ee4c 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1ee4d 6f 20 75 2e 63 62 20 2a 2f 0a 20 20 69 66 28 20  o u.cb */.  if( 
1ee4e 70 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20  p->pFrame ){.   
1ee4f 20 66 6f 72 28 75 2e 63 62 2e 70 46 72 61 6d 65   for(u.cb.pFrame
1ee50 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 63 62  =p->pFrame; u.cb
1ee51 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74  .pFrame->pParent
1ee52 3b 20 75 2e 63 62 2e 70 46 72 61 6d 65 3d 75 2e  ; u.cb.pFrame=u.
1ee53 63 62 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65  cb.pFrame->pPare
1ee54 6e 74 29 3b 0a 20 20 20 20 75 2e 63 62 2e 70 49  nt);.    u.cb.pI
1ee55 6e 31 20 3d 20 26 75 2e 63 62 2e 70 46 72 61 6d  n1 = &u.cb.pFram
1ee56 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  e->aMem[pOp->p1]
1ee57 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75  ;.  }else{.    u
1ee58 2e 63 62 2e 70 49 6e 31 20 3d 20 26 70 2d 3e 61  .cb.pIn1 = &p->a
1ee59 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1ee5a 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  }.  sqlite3VdbeM
1ee5b 65 6d 49 6e 74 65 67 65 72 69 66 79 28 75 2e 63  emIntegerify(u.c
1ee5c 62 2e 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74  b.pIn1);.  sqlit
1ee5d 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
1ee5e 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 69 66 28  ify(pIn2);.  if(
1ee5f 20 75 2e 63 62 2e 70 49 6e 31 2d 3e 75 2e 69 3c   u.cb.pIn1->u.i<
1ee60 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20 20 20  pIn2->u.i){.    
1ee61 75 2e 63 62 2e 70 49 6e 31 2d 3e 75 2e 69 20 3d  u.cb.pIn1->u.i =
1ee62 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a   pIn2->u.i;.  }.
1ee63 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1ee64 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1ee65 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a  _AUTOINCREMENT *
1ee66 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66  /../* Opcode: If
1ee67 50 6f 73 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  Pos P1 P2 * * *.
1ee68 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c  **.** If the val
1ee69 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  ue of register P
1ee6a 31 20 69 73 20 31 20 6f 72 20 67 72 65 61 74 65  1 is 1 or greate
1ee6b 72 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a  r, jump to P2..*
1ee6c 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67  *.** It is illeg
1ee6d 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 69  al to use this i
1ee6e 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20  nstruction on a 
1ee6f 72 65 67 69 73 74 65 72 20 74 68 61 74 20 64 6f  register that do
1ee70 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69  es.** not contai
1ee71 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41  n an integer.  A
1ee72 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c  n assertion faul
1ee73 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66  t will result if
1ee74 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73   you try..*/.cas
1ee75 65 20 4f 50 5f 49 66 50 6f 73 3a 20 7b 20 20 20  e OP_IfPos: {   
1ee76 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1ee77 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  1 */.  assert( p
1ee78 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49  In1->flags&MEM_I
1ee79 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31  nt );.  if( pIn1
1ee7a 2d 3e 75 2e 69 3e 30 20 29 7b 0a 20 20 20 20 20  ->u.i>0 ){.     
1ee7b 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1ee7c 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1ee7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4e  ../* Opcode: IfN
1ee7e 65 67 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  eg P1 P2 * * *.*
1ee7f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  *.** If the valu
1ee80 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31  e of register P1
1ee81 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65   is less than ze
1ee82 72 6f 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20  ro, jump to P2. 
1ee83 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c  .**.** It is ill
1ee84 65 67 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73  egal to use this
1ee85 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20   instruction on 
1ee86 61 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20  a register that 
1ee87 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74  does.** not cont
1ee88 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20  ain an integer. 
1ee89 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
1ee8a 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20  ult will result 
1ee8b 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63  if you try..*/.c
1ee8c 61 73 65 20 4f 50 5f 49 66 4e 65 67 3a 20 7b 20  ase OP_IfNeg: { 
1ee8d 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1ee8e 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  in1 */.  assert(
1ee8f 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d   pIn1->flags&MEM
1ee90 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49  _Int );.  if( pI
1ee91 6e 31 2d 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20  n1->u.i<0 ){.   
1ee92 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1ee93 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1ee94 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
1ee95 66 5a 65 72 6f 20 50 31 20 50 32 20 2a 20 2a 20  fZero P1 P2 * * 
1ee96 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76  *.**.** If the v
1ee97 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72  alue of register
1ee98 20 50 31 20 69 73 20 65 78 61 63 74 6c 79 20 30   P1 is exactly 0
1ee99 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a  , jump to P2. .*
1ee9a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67  *.** It is illeg
1ee9b 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 69  al to use this i
1ee9c 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20  nstruction on a 
1ee9d 72 65 67 69 73 74 65 72 20 74 68 61 74 20 64 6f  register that do
1ee9e 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69  es.** not contai
1ee9f 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41  n an integer.  A
1eea0 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c  n assertion faul
1eea1 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66  t will result if
1eea2 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73   you try..*/.cas
1eea3 65 20 4f 50 5f 49 66 5a 65 72 6f 3a 20 7b 20 20  e OP_IfZero: {  
1eea4 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69        /* jump, i
1eea5 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  n1 */.  assert( 
1eea6 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pIn1->flags&MEM_
1eea7 49 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e  Int );.  if( pIn
1eea8 31 2d 3e 75 2e 69 3d 3d 30 20 29 7b 0a 20 20 20  1->u.i==0 ){.   
1eea9 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1eeaa 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1eeab 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41  .}../* Opcode: A
1eeac 67 67 53 74 65 70 20 2a 20 50 32 20 50 33 20 50  ggStep * P2 P3 P
1eead 34 20 50 35 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75  4 P5.**.** Execu
1eeae 74 65 20 74 68 65 20 73 74 65 70 20 66 75 6e 63  te the step func
1eeaf 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72  tion for an aggr
1eeb0 65 67 61 74 65 2e 20 20 54 68 65 0a 2a 2a 20 66  egate.  The.** f
1eeb1 75 6e 63 74 69 6f 6e 20 68 61 73 20 50 35 20 61  unction has P5 a
1eeb2 72 67 75 6d 65 6e 74 73 2e 20 20 20 50 34 20 69  rguments.   P4 i
1eeb3 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
1eeb4 68 65 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74  he FuncDef.** st
1eeb5 72 75 63 74 75 72 65 20 74 68 61 74 20 73 70 65  ructure that spe
1eeb6 63 69 66 69 65 73 20 74 68 65 20 66 75 6e 63 74  cifies the funct
1eeb7 69 6f 6e 2e 20 20 55 73 65 20 72 65 67 69 73 74  ion.  Use regist
1eeb8 65 72 0a 2a 2a 20 50 33 20 61 73 20 74 68 65 20  er.** P3 as the 
1eeb9 61 63 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a  accumulator..**.
1eeba 2a 2a 20 54 68 65 20 50 35 20 61 72 67 75 6d 65  ** The P5 argume
1eebb 6e 74 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72  nts are taken fr
1eebc 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61  om register P2 a
1eebd 6e 64 20 69 74 73 0a 2a 2a 20 73 75 63 63 65 73  nd its.** succes
1eebe 73 6f 72 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  sors..*/.case OP
1eebf 5f 41 67 67 53 74 65 70 3a 20 7b 0a 23 69 66 20  _AggStep: {.#if 
1eec0 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1eec1 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1eec2 20 75 2e 63 63 20 2a 2f 0a 20 20 69 6e 74 20 6e   u.cc */.  int n
1eec3 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d  ;.  int i;.  Mem
1eec4 20 2a 70 4d 65 6d 3b 0a 20 20 4d 65 6d 20 2a 70   *pMem;.  Mem *p
1eec5 52 65 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63  Rec;.  sqlite3_c
1eec6 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71  ontext ctx;.  sq
1eec7 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
1eec8 56 61 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  Val;.#endif /* l
1eec9 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1eeca 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 63 20 2a  oved into u.cc *
1eecb 2f 0a 0a 20 20 75 2e 63 63 2e 6e 20 3d 20 70 4f  /..  u.cc.n = pO
1eecc 70 2d 3e 70 35 3b 0a 20 20 61 73 73 65 72 74 28  p->p5;.  assert(
1eecd 20 75 2e 63 63 2e 6e 3e 3d 30 20 29 3b 0a 20 20   u.cc.n>=0 );.  
1eece 75 2e 63 63 2e 70 52 65 63 20 3d 20 26 70 2d 3e  u.cc.pRec = &p->
1eecf 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1eed0 20 75 2e 63 63 2e 61 70 56 61 6c 20 3d 20 70 2d   u.cc.apVal = p-
1eed1 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74  >apArg;.  assert
1eed2 28 20 75 2e 63 63 2e 61 70 56 61 6c 20 7c 7c 20  ( u.cc.apVal || 
1eed3 75 2e 63 63 2e 6e 3d 3d 30 20 29 3b 0a 20 20 66  u.cc.n==0 );.  f
1eed4 6f 72 28 75 2e 63 63 2e 69 3d 30 3b 20 75 2e 63  or(u.cc.i=0; u.c
1eed5 63 2e 69 3c 75 2e 63 63 2e 6e 3b 20 75 2e 63 63  c.i<u.cc.n; u.cc
1eed6 2e 69 2b 2b 2c 20 75 2e 63 63 2e 70 52 65 63 2b  .i++, u.cc.pRec+
1eed7 2b 29 7b 0a 20 20 20 20 75 2e 63 63 2e 61 70 56  +){.    u.cc.apV
1eed8 61 6c 5b 75 2e 63 63 2e 69 5d 20 3d 20 75 2e 63  al[u.cc.i] = u.c
1eed9 63 2e 70 52 65 63 3b 0a 20 20 20 20 73 71 6c 69  c.pRec;.    sqli
1eeda 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54  te3VdbeMemStoreT
1eedb 79 70 65 28 75 2e 63 63 2e 70 52 65 63 29 3b 0a  ype(u.cc.pRec);.
1eedc 20 20 7d 0a 20 20 75 2e 63 63 2e 63 74 78 2e 70    }.  u.cc.ctx.p
1eedd 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Func = pOp->p4.p
1eede 46 75 6e 63 3b 0a 20 20 61 73 73 65 72 74 28 20  Func;.  assert( 
1eedf 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70  pOp->p3>0 && pOp
1eee0 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
1eee1 0a 20 20 75 2e 63 63 2e 63 74 78 2e 70 4d 65 6d  .  u.cc.ctx.pMem
1eee2 20 3d 20 75 2e 63 63 2e 70 4d 65 6d 20 3d 20 26   = u.cc.pMem = &
1eee3 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1eee4 3b 0a 20 20 75 2e 63 63 2e 70 4d 65 6d 2d 3e 6e  ;.  u.cc.pMem->n
1eee5 2b 2b 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 73  ++;.  u.cc.ctx.s
1eee6 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  .flags = MEM_Nul
1eee7 6c 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 73 2e  l;.  u.cc.ctx.s.
1eee8 7a 20 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 74  z = 0;.  u.cc.ct
1eee9 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b  x.s.zMalloc = 0;
1eeea 0a 20 20 75 2e 63 63 2e 63 74 78 2e 73 2e 78 44  .  u.cc.ctx.s.xD
1eeeb 65 6c 20 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63  el = 0;.  u.cc.c
1eeec 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20  tx.s.db = db;.  
1eeed 75 2e 63 63 2e 63 74 78 2e 69 73 45 72 72 6f 72  u.cc.ctx.isError
1eeee 20 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 74 78   = 0;.  u.cc.ctx
1eeef 2e 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69 66  .pColl = 0;.  if
1eef0 28 20 75 2e 63 63 2e 63 74 78 2e 70 46 75 6e 63  ( u.cc.ctx.pFunc
1eef1 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
1eef2 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29  _FUNC_NEEDCOLL )
1eef3 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  {.    assert( pO
1eef4 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20  p>p->aOp );.    
1eef5 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e  assert( pOp[-1].
1eef6 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53  p4type==P4_COLLS
1eef7 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  EQ );.    assert
1eef8 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65  ( pOp[-1].opcode
1eef9 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a  ==OP_CollSeq );.
1eefa 20 20 20 20 75 2e 63 63 2e 63 74 78 2e 70 43 6f      u.cc.ctx.pCo
1eefb 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e  ll = pOp[-1].p4.
1eefc 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 28 75 2e  pColl;.  }.  (u.
1eefd 63 63 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 78 53  cc.ctx.pFunc->xS
1eefe 74 65 70 29 28 26 75 2e 63 63 2e 63 74 78 2c 20  tep)(&u.cc.ctx, 
1eeff 75 2e 63 63 2e 6e 2c 20 75 2e 63 63 2e 61 70 56  u.cc.n, u.cc.apV
1ef00 61 6c 29 3b 0a 20 20 69 66 28 20 75 2e 63 63 2e  al);.  if( u.cc.
1ef01 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20  ctx.isError ){. 
1ef02 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1ef03 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1ef04 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
1ef05 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 75  e3_value_text(&u
1ef06 2e 63 63 2e 63 74 78 2e 73 29 29 3b 0a 20 20 20  .cc.ctx.s));.   
1ef07 20 72 63 20 3d 20 75 2e 63 63 2e 63 74 78 2e 69   rc = u.cc.ctx.i
1ef08 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 73 71  sError;.  }.  sq
1ef09 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1ef0a 61 73 65 28 26 75 2e 63 63 2e 63 74 78 2e 73 29  ase(&u.cc.ctx.s)
1ef0b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1ef0c 20 4f 70 63 6f 64 65 3a 20 41 67 67 46 69 6e 61   Opcode: AggFina
1ef0d 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  l P1 P2 * P4 *.*
1ef0e 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65  *.** Execute the
1ef0f 20 66 69 6e 61 6c 69 7a 65 72 20 66 75 6e 63 74   finalizer funct
1ef10 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65  ion for an aggre
1ef11 67 61 74 65 2e 20 20 50 31 20 69 73 0a 2a 2a 20  gate.  P1 is.** 
1ef12 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  the memory locat
1ef13 69 6f 6e 20 74 68 61 74 20 69 73 20 74 68 65 20  ion that is the 
1ef14 61 63 63 75 6d 75 6c 61 74 6f 72 20 66 6f 72 20  accumulator for 
1ef15 74 68 65 20 61 67 67 72 65 67 61 74 65 2e 0a 2a  the aggregate..*
1ef16 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e  *.** P2 is the n
1ef17 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
1ef18 74 73 20 74 68 61 74 20 74 68 65 20 73 74 65 70  ts that the step
1ef19 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20   function takes 
1ef1a 61 6e 64 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  and.** P4 is a p
1ef1b 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75  ointer to the Fu
1ef1c 6e 63 44 65 66 20 66 6f 72 20 74 68 69 73 20 66  ncDef for this f
1ef1d 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 50 32  unction.  The P2
1ef1e 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20  .** argument is 
1ef1f 6e 6f 74 20 75 73 65 64 20 62 79 20 74 68 69 73  not used by this
1ef20 20 6f 70 63 6f 64 65 2e 20 20 49 74 20 69 73 20   opcode.  It is 
1ef21 6f 6e 6c 79 20 74 68 65 72 65 20 74 6f 20 64 69  only there to di
1ef22 73 61 6d 62 69 67 75 61 74 65 0a 2a 2a 20 66 75  sambiguate.** fu
1ef23 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e  nctions that can
1ef24 20 74 61 6b 65 20 76 61 72 79 69 6e 67 20 6e 75   take varying nu
1ef25 6d 62 65 72 73 20 6f 66 20 61 72 67 75 6d 65 6e  mbers of argumen
1ef26 74 73 2e 20 20 54 68 65 0a 2a 2a 20 50 34 20 61  ts.  The.** P4 a
1ef27 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 6c 79 20  rgument is only 
1ef28 6e 65 65 64 65 64 20 66 6f 72 20 74 68 65 20 64  needed for the d
1ef29 65 67 65 6e 65 72 61 74 65 20 63 61 73 65 20 77  egenerate case w
1ef2a 68 65 72 65 0a 2a 2a 20 74 68 65 20 73 74 65 70  here.** the step
1ef2b 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 6e 6f   function was no
1ef2c 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 61 6c  t previously cal
1ef2d 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  led..*/.case OP_
1ef2e 41 67 67 46 69 6e 61 6c 3a 20 7b 0a 23 69 66 20  AggFinal: {.#if 
1ef2f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1ef30 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1ef31 20 75 2e 63 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a   u.cd */.  Mem *
1ef32 70 4d 65 6d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  pMem;.#endif /* 
1ef33 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1ef34 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 64 20  moved into u.cd 
1ef35 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1ef36 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p1>0 && pOp->p
1ef37 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  1<=p->nMem );.  
1ef38 75 2e 63 64 2e 70 4d 65 6d 20 3d 20 26 70 2d 3e  u.cd.pMem = &p->
1ef39 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1ef3a 20 61 73 73 65 72 74 28 20 28 75 2e 63 64 2e 70   assert( (u.cd.p
1ef3b 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e 28 4d  Mem->flags & ~(M
1ef3c 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 67 29  EM_Null|MEM_Agg)
1ef3d 29 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73  )==0 );.  rc = s
1ef3e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e  qlite3VdbeMemFin
1ef3f 61 6c 69 7a 65 28 75 2e 63 64 2e 70 4d 65 6d 2c  alize(u.cd.pMem,
1ef40 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 29 3b   pOp->p4.pFunc);
1ef41 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
1ef42 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1ef43 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1ef44 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33  b, "%s", sqlite3
1ef45 5f 76 61 6c 75 65 5f 74 65 78 74 28 75 2e 63 64  _value_text(u.cd
1ef46 2e 70 4d 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73  .pMem));.  }.  s
1ef47 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1ef48 45 6e 63 6f 64 69 6e 67 28 75 2e 63 64 2e 70 4d  Encoding(u.cd.pM
1ef49 65 6d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  em, encoding);. 
1ef4a 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1ef4b 53 49 5a 45 28 75 2e 63 64 2e 70 4d 65 6d 29 3b  SIZE(u.cd.pMem);
1ef4c 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
1ef4d 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 64  beMemTooBig(u.cd
1ef4e 2e 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 67 6f  .pMem) ){.    go
1ef4f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1ef50 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66    break;.}...#if
1ef51 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1ef52 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 26 26  _OMIT_VACUUM) &&
1ef53 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1ef54 5f 4f 4d 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a  _OMIT_ATTACH)./*
1ef55 20 4f 70 63 6f 64 65 3a 20 56 61 63 75 75 6d 20   Opcode: Vacuum 
1ef56 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  * * * * *.**.** 
1ef57 56 61 63 75 75 6d 20 74 68 65 20 65 6e 74 69 72  Vacuum the entir
1ef58 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
1ef59 73 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 63 61  s opcode will ca
1ef5a 75 73 65 20 6f 74 68 65 72 20 76 69 72 74 75 61  use other virtua
1ef5b 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 73 20 74 6f  l.** machines to
1ef5c 20 62 65 20 63 72 65 61 74 65 64 20 61 6e 64 20   be created and 
1ef5d 72 75 6e 2e 20 20 49 74 20 6d 61 79 20 6e 6f 74  run.  It may not
1ef5e 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
1ef5f 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e  within.** a tran
1ef60 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65  saction..*/.case
1ef61 20 4f 50 5f 56 61 63 75 75 6d 3a 20 7b 0a 20 20   OP_Vacuum: {.  
1ef62 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1ef63 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20  yOff(db) ) goto 
1ef64 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1ef65 75 73 65 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c  use; .  rc = sql
1ef66 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28 26 70  ite3RunVacuum(&p
1ef67 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 29 3b 0a  ->zErrMsg, db);.
1ef68 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1ef69 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f  etyOn(db) ) goto
1ef6a 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1ef6b 73 75 73 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  suse;.  break;.}
1ef6c 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65  .#endif..#if !de
1ef6d 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1ef6e 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 2f 2a  T_AUTOVACUUM)./*
1ef6f 20 4f 70 63 6f 64 65 3a 20 49 6e 63 72 56 61 63   Opcode: IncrVac
1ef70 75 75 6d 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  uum P1 P2 * * *.
1ef71 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20  **.** Perform a 
1ef72 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 74  single step of t
1ef73 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76  he incremental v
1ef74 61 63 75 75 6d 20 70 72 6f 63 65 64 75 72 65 20  acuum procedure 
1ef75 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 20 64 61 74  on.** the P1 dat
1ef76 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 76 61  abase. If the va
1ef77 63 75 75 6d 20 68 61 73 20 66 69 6e 69 73 68 65  cuum has finishe
1ef78 64 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72  d, jump to instr
1ef79 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 2e 20 4f 74  uction.** P2. Ot
1ef7a 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68  herwise, fall th
1ef7b 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
1ef7c 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  t instruction..*
1ef7d 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 63 72 56 61  /.case OP_IncrVa
1ef7e 63 75 75 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f  cuum: {        /
1ef7f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20  * jump */.#if 0 
1ef80 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ef81 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ef82 2e 63 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  .ce */.  Btree *
1ef83 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  pBt;.#endif /* l
1ef84 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1ef85 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 65 20 2a  oved into u.ce *
1ef86 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  /..  assert( pOp
1ef87 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1ef88 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  p1<db->nDb );.  
1ef89 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
1ef8a 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d  eMask & (1<<pOp-
1ef8b 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e  >p1))!=0 );.  u.
1ef8c 63 65 2e 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  ce.pBt = db->aDb
1ef8d 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20  [pOp->p1].pBt;. 
1ef8e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1ef8f 65 65 49 6e 63 72 56 61 63 75 75 6d 28 75 2e 63  eeIncrVacuum(u.c
1ef90 65 2e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63  e.pBt);.  if( rc
1ef91 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
1ef92 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1ef93 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20  2 - 1;.    rc = 
1ef94 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1ef95 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1ef96 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 78 70  ../* Opcode: Exp
1ef97 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  ire P1 * * * *.*
1ef98 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65 63 6f  *.** Cause preco
1ef99 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74  mpiled statement
1ef9a 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78 70 69  s to become expi
1ef9b 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65 64 20  red. An expired 
1ef9c 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66 61 69  statement.** fai
1ef9d 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ls with an error
1ef9e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f   code of SQLITE_
1ef9f 53 43 48 45 4d 41 20 69 66 20 69 74 20 69 73 20  SCHEMA if it is 
1efa0 65 76 65 72 20 65 78 65 63 75 74 65 64 20 0a 2a  ever executed .*
1efa1 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33 5f 73  * (via sqlite3_s
1efa2 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49  tep())..** .** I
1efa3 66 20 50 31 20 69 73 20 30 2c 20 74 68 65 6e 20  f P1 is 0, then 
1efa4 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  all SQL statemen
1efa5 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65  ts become expire
1efa6 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d  d. If P1 is non-
1efa7 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e  zero,.** then on
1efa8 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79  ly the currently
1efa9 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65   executing state
1efaa 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74 65 64  ment is affected
1efab 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 78  . .*/.case OP_Ex
1efac 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20 21 70  pire: {.  if( !p
1efad 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73 71  Op->p1 ){.    sq
1efae 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
1efaf 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
1efb0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1efb1 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a  p->expired = 1;.
1efb2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1efb3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1efb4 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1efb5 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61 62 6c  ./* Opcode: Tabl
1efb6 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33 20 50  eLock P1 P2 P3 P
1efb7 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e  4 *.**.** Obtain
1efb8 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 61 72   a lock on a par
1efb9 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 54  ticular table. T
1efba 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1efbb 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65  is only used whe
1efbc 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d  n.** the shared-
1efbd 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73  cache feature is
1efbe 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a   enabled. .**.**
1efbf 20 50 31 20 69 73 20 74 68 65 20 69 6e 64 65 78   P1 is the index
1efc0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1efc1 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b   in sqlite3.aDb[
1efc2 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  ] of the databas
1efc3 65 0a 2a 2a 20 6f 6e 20 77 68 69 63 68 20 74 68  e.** on which th
1efc4 65 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72  e lock is acquir
1efc5 65 64 2e 20 20 41 20 72 65 61 64 6c 6f 63 6b 20  ed.  A readlock 
1efc6 69 73 20 6f 62 74 61 69 6e 65 64 20 69 66 20 50  is obtained if P
1efc7 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61 20 77 72 69  3==0 or.** a wri
1efc8 74 65 20 6c 6f 63 6b 20 69 66 20 50 33 3d 3d 31  te lock if P3==1
1efc9 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63 6f 6e 74 61  ..**.** P2 conta
1efca 69 6e 73 20 74 68 65 20 72 6f 6f 74 2d 70 61 67  ins the root-pag
1efcb 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
1efcc 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 50 34  o lock..**.** P4
1efcd 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e   contains a poin
1efce 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20  ter to the name 
1efcf 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  of the table bei
1efd0 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 68 69 73 20  ng locked. This 
1efd1 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 73 65 64 20  is only.** used 
1efd2 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65  to generate an e
1efd3 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66 20  rror message if 
1efd4 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20  the lock cannot 
1efd5 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a  be obtained..*/.
1efd6 63 61 73 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63  case OP_TableLoc
1efd7 6b 3a 20 7b 0a 20 20 75 38 20 69 73 57 72 69 74  k: {.  u8 isWrit
1efd8 65 4c 6f 63 6b 20 3d 20 28 75 38 29 70 4f 70 2d  eLock = (u8)pOp-
1efd9 3e 70 33 3b 0a 20 20 69 66 28 20 69 73 57 72 69  >p3;.  if( isWri
1efda 74 65 4c 6f 63 6b 20 7c 7c 20 30 3d 3d 28 64 62  teLock || 0==(db
1efdb 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52  ->flags&SQLITE_R
1efdc 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20  eadUncommitted) 
1efdd 29 7b 0a 20 20 20 20 69 6e 74 20 70 31 20 3d 20  ){.    int p1 = 
1efde 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 20 20 61 73  pOp->p1; .    as
1efdf 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70  sert( p1>=0 && p
1efe0 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20  1<db->nDb );.   
1efe1 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
1efe2 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 31 29  eeMask & (1<<p1)
1efe3 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  )!=0 );.    asse
1efe4 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d  rt( isWriteLock=
1efe5 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63  =0 || isWriteLoc
1efe6 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 72 63 20 3d  k==1 );.    rc =
1efe7 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63   sqlite3BtreeLoc
1efe8 6b 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b 70  kTable(db->aDb[p
1efe9 31 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 2c  1].pBt, pOp->p2,
1efea 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20   isWriteLock);. 
1efeb 20 20 20 69 66 28 20 28 72 63 26 30 78 46 46 29     if( (rc&0xFF)
1efec 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  ==SQLITE_LOCKED 
1efed 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
1efee 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34  har *z = pOp->p4
1efef 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  .z;.      sqlite
1eff0 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1eff1 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 64 61 74  ErrMsg, db, "dat
1eff2 61 62 61 73 65 20 74 61 62 6c 65 20 69 73 20 6c  abase table is l
1eff3 6f 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29 3b 0a  ocked: %s", z);.
1eff4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61      }.  }.  brea
1eff5 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
1eff6 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1eff7 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e  D_CACHE */..#ifn
1eff8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1eff9 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20  VIRTUALTABLE./* 
1effa 4f 70 63 6f 64 65 3a 20 56 42 65 67 69 6e 20 2a  Opcode: VBegin *
1effb 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20   * * P4 *.**.** 
1effc 50 34 20 6d 61 79 20 62 65 20 61 20 70 6f 69 6e  P4 may be a poin
1effd 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65  ter to an sqlite
1effe 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
1efff 2e 20 49 66 20 73 6f 2c 20 63 61 6c 6c 20 74 68  . If so, call th
1f000 65 20 0a 2a 2a 20 78 42 65 67 69 6e 20 6d 65 74  e .** xBegin met
1f001 68 6f 64 20 66 6f 72 20 74 68 61 74 20 74 61 62  hod for that tab
1f002 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 2c 20  le..**.** Also, 
1f003 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 50  whether or not P
1f004 34 20 69 73 20 73 65 74 2c 20 63 68 65 63 6b 20  4 is set, check 
1f005 74 68 61 74 20 74 68 69 73 20 69 73 20 6e 6f 74  that this is not
1f006 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72   being called fr
1f007 6f 6d 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 63  om.** within a c
1f008 61 6c 6c 62 61 63 6b 20 74 6f 20 61 20 76 69 72  allback to a vir
1f009 74 75 61 6c 20 74 61 62 6c 65 20 78 53 79 6e 63  tual table xSync
1f00a 28 29 20 6d 65 74 68 6f 64 2e 20 49 66 20 69 74  () method. If it
1f00b 20 69 73 2c 20 74 68 65 20 65 72 72 6f 72 0a 2a   is, the error.*
1f00c 2a 20 63 6f 64 65 20 77 69 6c 6c 20 62 65 20 73  * code will be s
1f00d 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c 4f 43  et to SQLITE_LOC
1f00e 4b 45 44 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  KED..*/.case OP_
1f00f 56 42 65 67 69 6e 3a 20 7b 0a 23 69 66 20 30 20  VBegin: {.#if 0 
1f010 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1f011 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1f012 2e 63 66 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20  .cf */.  VTable 
1f013 2a 70 56 54 61 62 3b 0a 23 65 6e 64 69 66 20 2f  *pVTab;.#endif /
1f014 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1f015 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
1f016 66 20 2a 2f 0a 20 20 75 2e 63 66 2e 70 56 54 61  f */.  u.cf.pVTa
1f017 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61  b = pOp->p4.pVta
1f018 62 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  b;.  rc = sqlite
1f019 33 56 74 61 62 42 65 67 69 6e 28 64 62 2c 20 75  3VtabBegin(db, u
1f01a 2e 63 66 2e 70 56 54 61 62 29 3b 0a 20 20 69 66  .cf.pVTab);.  if
1f01b 28 20 75 2e 63 66 2e 70 56 54 61 62 20 29 7b 0a  ( u.cf.pVTab ){.
1f01c 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f01d 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  e(db, p->zErrMsg
1f01e 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73  );.    p->zErrMs
1f01f 67 20 3d 20 75 2e 63 66 2e 70 56 54 61 62 2d 3e  g = u.cf.pVTab->
1f020 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a  pVtab->zErrMsg;.
1f021 20 20 20 20 75 2e 63 66 2e 70 56 54 61 62 2d 3e      u.cf.pVTab->
1f022 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d  pVtab->zErrMsg =
1f023 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   0;.  }.  break;
1f024 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1f025 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1f026 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  TABLE */..#ifnde
1f027 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1f028 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70  RTUALTABLE./* Op
1f029 63 6f 64 65 3a 20 56 43 72 65 61 74 65 20 50 31  code: VCreate P1
1f02a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20   * * P4 *.**.** 
1f02b 50 34 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  P4 is the name o
1f02c 66 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  f a virtual tabl
1f02d 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31  e in database P1
1f02e 2e 20 43 61 6c 6c 20 74 68 65 20 78 43 72 65 61  . Call the xCrea
1f02f 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 66 6f 72  te method.** for
1f030 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a   that table..*/.
1f031 63 61 73 65 20 4f 50 5f 56 43 72 65 61 74 65 3a  case OP_VCreate:
1f032 20 7b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   {.  rc = sqlite
1f033 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28  3VtabCallCreate(
1f034 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70  db, pOp->p1, pOp
1f035 2d 3e 70 34 2e 7a 2c 20 26 70 2d 3e 7a 45 72 72  ->p4.z, &p->zErr
1f036 4d 73 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Msg);.  break;.}
1f037 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1f038 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1f039 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BLE */..#ifndef 
1f03a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1f03b 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f  UALTABLE./* Opco
1f03c 64 65 3a 20 56 44 65 73 74 72 6f 79 20 50 31 20  de: VDestroy P1 
1f03d 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50  * * P4 *.**.** P
1f03e 34 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  4 is the name of
1f03f 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1f040 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e   in database P1.
1f041 20 20 43 61 6c 6c 20 74 68 65 20 78 44 65 73 74    Call the xDest
1f042 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66  roy method.** of
1f043 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a   that table..*/.
1f044 63 61 73 65 20 4f 50 5f 56 44 65 73 74 72 6f 79  case OP_VDestroy
1f045 3a 20 7b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d  : {.  p->inVtabM
1f046 65 74 68 6f 64 20 3d 20 32 3b 0a 20 20 72 63 20  ethod = 2;.  rc 
1f047 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c  = sqlite3VtabCal
1f048 6c 44 65 73 74 72 6f 79 28 64 62 2c 20 70 4f 70  lDestroy(db, pOp
1f049 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29  ->p1, pOp->p4.z)
1f04a 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74  ;.  p->inVtabMet
1f04b 68 6f 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b  hod = 0;.  break
1f04c 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1f04d 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1f04e 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64  LTABLE */..#ifnd
1f04f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1f050 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f  IRTUALTABLE./* O
1f051 70 63 6f 64 65 3a 20 56 4f 70 65 6e 20 50 31 20  pcode: VOpen P1 
1f052 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50  * * P4 *.**.** P
1f053 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
1f054 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  o a virtual tabl
1f055 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c  e object, an sql
1f056 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74  ite3_vtab struct
1f057 75 72 65 2e 0a 2a 2a 20 50 31 20 69 73 20 61 20  ure..** P1 is a 
1f058 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e 20 20  cursor number.  
1f059 54 68 69 73 20 6f 70 63 6f 64 65 20 6f 70 65 6e  This opcode open
1f05a 73 20 61 20 63 75 72 73 6f 72 20 74 6f 20 74 68  s a cursor to th
1f05b 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62  e virtual.** tab
1f05c 6c 65 20 61 6e 64 20 73 74 6f 72 65 73 20 74 68  le and stores th
1f05d 61 74 20 63 75 72 73 6f 72 20 69 6e 20 50 31 2e  at cursor in P1.
1f05e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4f 70 65  .*/.case OP_VOpe
1f05f 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  n: {.#if 0  /* l
1f060 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1f061 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 67 20 2a  oved into u.cg *
1f062 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1f063 70 43 75 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pCur;.  sqlite3_
1f064 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74  vtab_cursor *pVt
1f065 61 62 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69  abCursor;.  sqli
1f066 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
1f067 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  .  sqlite3_modul
1f068 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64  e *pModule;.#end
1f069 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1f06a 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f06b 20 75 2e 63 67 20 2a 2f 0a 0a 20 20 75 2e 63 67   u.cg */..  u.cg
1f06c 2e 70 43 75 72 20 3d 20 30 3b 0a 20 20 75 2e 63  .pCur = 0;.  u.c
1f06d 67 2e 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20  g.pVtabCursor = 
1f06e 30 3b 0a 20 20 75 2e 63 67 2e 70 56 74 61 62 20  0;.  u.cg.pVtab 
1f06f 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d  = pOp->p4.pVtab-
1f070 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 67 2e 70  >pVtab;.  u.cg.p
1f071 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65  Module = (sqlite
1f072 33 5f 6d 6f 64 75 6c 65 20 2a 29 75 2e 63 67 2e  3_module *)u.cg.
1f073 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a  pVtab->pModule;.
1f074 20 20 61 73 73 65 72 74 28 75 2e 63 67 2e 70 56    assert(u.cg.pV
1f075 74 61 62 20 26 26 20 75 2e 63 67 2e 70 4d 6f 64  tab && u.cg.pMod
1f076 75 6c 65 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  ule);.  if( sqli
1f077 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
1f078 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1f079 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72  e_to_misuse;.  r
1f07a 63 20 3d 20 75 2e 63 67 2e 70 4d 6f 64 75 6c 65  c = u.cg.pModule
1f07b 2d 3e 78 4f 70 65 6e 28 75 2e 63 67 2e 70 56 74  ->xOpen(u.cg.pVt
1f07c 61 62 2c 20 26 75 2e 63 67 2e 70 56 74 61 62 43  ab, &u.cg.pVtabC
1f07d 75 72 73 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65  ursor);.  sqlite
1f07e 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1f07f 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45  ErrMsg);.  p->zE
1f080 72 72 4d 73 67 20 3d 20 75 2e 63 67 2e 70 56 74  rrMsg = u.cg.pVt
1f081 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75  ab->zErrMsg;.  u
1f082 2e 63 67 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  .cg.pVtab->zErrM
1f083 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71  sg = 0;.  if( sq
1f084 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1f085 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1f086 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20  ue_to_misuse;.  
1f087 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72  if( SQLITE_OK==r
1f088 63 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 69 74  c ){.    /* Init
1f089 69 61 6c 69 7a 65 20 73 71 6c 69 74 65 33 5f 76  ialize sqlite3_v
1f08a 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 20  tab_cursor base 
1f08b 63 6c 61 73 73 20 2a 2f 0a 20 20 20 20 75 2e 63  class */.    u.c
1f08c 67 2e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  g.pVtabCursor->p
1f08d 56 74 61 62 20 3d 20 75 2e 63 67 2e 70 56 74 61  Vtab = u.cg.pVta
1f08e 62 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69  b;..    /* Initi
1f08f 61 6c 69 73 65 20 76 64 62 65 20 63 75 72 73 6f  alise vdbe curso
1f090 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20  r object */.    
1f091 75 2e 63 67 2e 70 43 75 72 20 3d 20 61 6c 6c 6f  u.cg.pCur = allo
1f092 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f  cateCursor(p, pO
1f093 70 2d 3e 70 31 2c 20 30 2c 20 2d 31 2c 20 30 29  p->p1, 0, -1, 0)
1f094 3b 0a 20 20 20 20 69 66 28 20 75 2e 63 67 2e 70  ;.    if( u.cg.p
1f095 43 75 72 20 29 7b 0a 20 20 20 20 20 20 75 2e 63  Cur ){.      u.c
1f096 67 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  g.pCur->pVtabCur
1f097 73 6f 72 20 3d 20 75 2e 63 67 2e 70 56 74 61 62  sor = u.cg.pVtab
1f098 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 75 2e  Cursor;.      u.
1f099 63 67 2e 70 43 75 72 2d 3e 70 4d 6f 64 75 6c 65  cg.pCur->pModule
1f09a 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72   = u.cg.pVtabCur
1f09b 73 6f 72 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64  sor->pVtab->pMod
1f09c 75 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ule;.    }else{.
1f09d 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
1f09e 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
1f09f 20 20 75 2e 63 67 2e 70 4d 6f 64 75 6c 65 2d 3e    u.cg.pModule->
1f0a0 78 43 6c 6f 73 65 28 75 2e 63 67 2e 70 56 74 61  xClose(u.cg.pVta
1f0a1 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a  bCursor);.    }.
1f0a2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1f0a3 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1f0a4 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1f0a5 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
1f0a6 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1f0a7 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
1f0a8 3a 20 56 46 69 6c 74 65 72 20 50 31 20 50 32 20  : VFilter P1 P2 
1f0a9 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 31  P3 P4 *.**.** P1
1f0aa 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65   is a cursor ope
1f0ab 6e 65 64 20 75 73 69 6e 67 20 56 4f 70 65 6e 2e  ned using VOpen.
1f0ac 20 20 50 32 20 69 73 20 61 6e 20 61 64 64 72 65    P2 is an addre
1f0ad 73 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66  ss to jump to if
1f0ae 0a 2a 2a 20 74 68 65 20 66 69 6c 74 65 72 65 64  .** the filtered
1f0af 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65   result set is e
1f0b0 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69  mpty..**.** P4 i
1f0b1 73 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72  s either NULL or
1f0b2 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 77   a string that w
1f0b3 61 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  as generated by 
1f0b4 74 68 65 20 78 42 65 73 74 49 6e 64 65 78 0a 2a  the xBestIndex.*
1f0b5 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  * method of the 
1f0b6 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 69 6e 74  module.  The int
1f0b7 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74  erpretation of t
1f0b8 68 65 20 50 34 20 73 74 72 69 6e 67 20 69 73 20  he P4 string is 
1f0b9 6c 65 66 74 0a 2a 2a 20 74 6f 20 74 68 65 20 6d  left.** to the m
1f0ba 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  odule implementa
1f0bb 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tion..**.** This
1f0bc 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20   opcode invokes 
1f0bd 74 68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68  the xFilter meth
1f0be 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61  od on the virtua
1f0bf 6c 20 74 61 62 6c 65 20 73 70 65 63 69 66 69 65  l table specifie
1f0c0 64 0a 2a 2a 20 62 79 20 50 31 2e 20 20 54 68 65  d.** by P1.  The
1f0c1 20 69 6e 74 65 67 65 72 20 71 75 65 72 79 20 70   integer query p
1f0c2 6c 61 6e 20 70 61 72 61 6d 65 74 65 72 20 74 6f  lan parameter to
1f0c3 20 78 46 69 6c 74 65 72 20 69 73 20 73 74 6f 72   xFilter is stor
1f0c4 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a  ed in register.*
1f0c5 2a 20 50 33 2e 20 52 65 67 69 73 74 65 72 20 50  * P3. Register P
1f0c6 33 2b 31 20 73 74 6f 72 65 73 20 74 68 65 20 61  3+1 stores the a
1f0c7 72 67 63 20 70 61 72 61 6d 65 74 65 72 20 74 6f  rgc parameter to
1f0c8 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 74 68   be passed to th
1f0c9 65 0a 2a 2a 20 78 46 69 6c 74 65 72 20 6d 65 74  e.** xFilter met
1f0ca 68 6f 64 2e 20 52 65 67 69 73 74 65 72 73 20 50  hod. Registers P
1f0cb 33 2b 32 2e 2e 50 33 2b 31 2b 61 72 67 63 20 61  3+2..P3+1+argc a
1f0cc 72 65 20 74 68 65 20 61 72 67 63 0a 2a 2a 20 61  re the argc.** a
1f0cd 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65  dditional parame
1f0ce 74 65 72 73 20 77 68 69 63 68 20 61 72 65 20 70  ters which are p
1f0cf 61 73 73 65 64 20 74 6f 0a 2a 2a 20 78 46 69 6c  assed to.** xFil
1f0d0 74 65 72 20 61 73 20 61 72 67 76 2e 20 52 65 67  ter as argv. Reg
1f0d1 69 73 74 65 72 20 50 33 2b 32 20 62 65 63 6f 6d  ister P3+2 becom
1f0d2 65 73 20 61 72 67 76 5b 30 5d 20 77 68 65 6e 20  es argv[0] when 
1f0d3 70 61 73 73 65 64 20 74 6f 20 78 46 69 6c 74 65  passed to xFilte
1f0d4 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20  r..**.** A jump 
1f0d5 69 73 20 6d 61 64 65 20 74 6f 20 50 32 20 69 66  is made to P2 if
1f0d6 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1f0d7 61 66 74 65 72 20 66 69 6c 74 65 72 69 6e 67 20  after filtering 
1f0d8 77 6f 75 6c 64 20 62 65 20 65 6d 70 74 79 2e 0a  would be empty..
1f0d9 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 46 69 6c 74  */.case OP_VFilt
1f0da 65 72 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20  er: {   /* jump 
1f0db 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1f0dc 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1f0dd 65 64 20 69 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a  ed into u.ch */.
1f0de 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e    int nArg;.  in
1f0df 74 20 69 51 75 65 72 79 3b 0a 20 20 63 6f 6e 73  t iQuery;.  cons
1f0e0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
1f0e1 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d   *pModule;.  Mem
1f0e2 20 2a 70 51 75 65 72 79 3b 0a 20 20 4d 65 6d 20   *pQuery;.  Mem 
1f0e3 2a 70 41 72 67 63 3b 0a 20 20 73 71 6c 69 74 65  *pArgc;.  sqlite
1f0e4 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
1f0e5 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 73 71  VtabCursor;.  sq
1f0e6 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
1f0e7 62 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  b;.  VdbeCursor 
1f0e8 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72 65 73  *pCur;.  int res
1f0e9 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d  ;.  int i;.  Mem
1f0ea 20 2a 2a 61 70 41 72 67 3b 0a 23 65 6e 64 69 66   **apArg;.#endif
1f0eb 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1f0ec 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1f0ed 2e 63 68 20 2a 2f 0a 0a 20 20 75 2e 63 68 2e 70  .ch */..  u.ch.p
1f0ee 51 75 65 72 79 20 3d 20 26 70 2d 3e 61 4d 65 6d  Query = &p->aMem
1f0ef 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63  [pOp->p3];.  u.c
1f0f0 68 2e 70 41 72 67 63 20 3d 20 26 75 2e 63 68 2e  h.pArgc = &u.ch.
1f0f1 70 51 75 65 72 79 5b 31 5d 3b 0a 20 20 75 2e 63  pQuery[1];.  u.c
1f0f2 68 2e 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73  h.pCur = p->apCs
1f0f3 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 52 45  r[pOp->p1];.  RE
1f0f4 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1f0f5 2d 3e 70 33 2c 20 75 2e 63 68 2e 70 51 75 65 72  ->p3, u.ch.pQuer
1f0f6 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  y);.  assert( u.
1f0f7 63 68 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75  ch.pCur->pVtabCu
1f0f8 72 73 6f 72 20 29 3b 0a 20 20 75 2e 63 68 2e 70  rsor );.  u.ch.p
1f0f9 56 74 61 62 43 75 72 73 6f 72 20 3d 20 75 2e 63  VtabCursor = u.c
1f0fa 68 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  h.pCur->pVtabCur
1f0fb 73 6f 72 3b 0a 20 20 75 2e 63 68 2e 70 56 74 61  sor;.  u.ch.pVta
1f0fc 62 20 3d 20 75 2e 63 68 2e 70 56 74 61 62 43 75  b = u.ch.pVtabCu
1f0fd 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 75  rsor->pVtab;.  u
1f0fe 2e 63 68 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e  .ch.pModule = u.
1f0ff 63 68 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c  ch.pVtab->pModul
1f100 65 3b 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68  e;..  /* Grab th
1f101 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 61  e index number a
1f102 6e 64 20 61 72 67 63 20 70 61 72 61 6d 65 74 65  nd argc paramete
1f103 72 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  rs */.  assert( 
1f104 28 75 2e 63 68 2e 70 51 75 65 72 79 2d 3e 66 6c  (u.ch.pQuery->fl
1f105 61 67 73 26 4d 45 4d 5f 49 6e 74 29 21 3d 30 20  ags&MEM_Int)!=0 
1f106 26 26 20 75 2e 63 68 2e 70 41 72 67 63 2d 3e 66  && u.ch.pArgc->f
1f107 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b  lags==MEM_Int );
1f108 0a 20 20 75 2e 63 68 2e 6e 41 72 67 20 3d 20 28  .  u.ch.nArg = (
1f109 69 6e 74 29 75 2e 63 68 2e 70 41 72 67 63 2d 3e  int)u.ch.pArgc->
1f10a 75 2e 69 3b 0a 20 20 75 2e 63 68 2e 69 51 75 65  u.i;.  u.ch.iQue
1f10b 72 79 20 3d 20 28 69 6e 74 29 75 2e 63 68 2e 70  ry = (int)u.ch.p
1f10c 51 75 65 72 79 2d 3e 75 2e 69 3b 0a 0a 20 20 2f  Query->u.i;..  /
1f10d 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69  * Invoke the xFi
1f10e 6c 74 65 72 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  lter method */. 
1f10f 20 7b 0a 20 20 20 20 75 2e 63 68 2e 72 65 73 20   {.    u.ch.res 
1f110 3d 20 30 3b 0a 20 20 20 20 75 2e 63 68 2e 61 70  = 0;.    u.ch.ap
1f111 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a  Arg = p->apArg;.
1f112 20 20 20 20 66 6f 72 28 75 2e 63 68 2e 69 20 3d      for(u.ch.i =
1f113 20 30 3b 20 75 2e 63 68 2e 69 3c 75 2e 63 68 2e   0; u.ch.i<u.ch.
1f114 6e 41 72 67 3b 20 75 2e 63 68 2e 69 2b 2b 29 7b  nArg; u.ch.i++){
1f115 0a 20 20 20 20 20 20 75 2e 63 68 2e 61 70 41 72  .      u.ch.apAr
1f116 67 5b 75 2e 63 68 2e 69 5d 20 3d 20 26 75 2e 63  g[u.ch.i] = &u.c
1f117 68 2e 70 41 72 67 63 5b 75 2e 63 68 2e 69 2b 31  h.pArgc[u.ch.i+1
1f118 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ];.      sqlite3
1f119 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65  VdbeMemStoreType
1f11a 28 75 2e 63 68 2e 61 70 41 72 67 5b 75 2e 63 68  (u.ch.apArg[u.ch
1f11b 2e 69 5d 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  .i]);.    }..   
1f11c 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1f11d 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
1f11e 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1f11f 73 75 73 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56  suse;.    p->inV
1f120 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20  tabMethod = 1;. 
1f121 20 20 20 72 63 20 3d 20 75 2e 63 68 2e 70 4d 6f     rc = u.ch.pMo
1f122 64 75 6c 65 2d 3e 78 46 69 6c 74 65 72 28 75 2e  dule->xFilter(u.
1f123 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 2c 20  ch.pVtabCursor, 
1f124 75 2e 63 68 2e 69 51 75 65 72 79 2c 20 70 4f 70  u.ch.iQuery, pOp
1f125 2d 3e 70 34 2e 7a 2c 20 75 2e 63 68 2e 6e 41 72  ->p4.z, u.ch.nAr
1f126 67 2c 20 75 2e 63 68 2e 61 70 41 72 67 29 3b 0a  g, u.ch.apArg);.
1f127 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74      p->inVtabMet
1f128 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  hod = 0;.    sql
1f129 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1f12a 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
1f12b 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63  p->zErrMsg = u.c
1f12c 68 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  h.pVtab->zErrMsg
1f12d 3b 0a 20 20 20 20 75 2e 63 68 2e 70 56 74 61 62  ;.    u.ch.pVtab
1f12e 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1f12f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1f130 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e  E_OK ){.      u.
1f131 63 68 2e 72 65 73 20 3d 20 75 2e 63 68 2e 70 4d  ch.res = u.ch.pM
1f132 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 68  odule->xEof(u.ch
1f133 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20  .pVtabCursor);. 
1f134 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
1f135 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1f136 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1f137 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20  e_to_misuse;..  
1f138 20 20 69 66 28 20 75 2e 63 68 2e 72 65 73 20 29    if( u.ch.res )
1f139 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  {.      pc = pOp
1f13a 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a  ->p2 - 1;.    }.
1f13b 20 20 7d 0a 20 20 75 2e 63 68 2e 70 43 75 72 2d    }.  u.ch.pCur-
1f13c 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20  >nullRow = 0;.. 
1f13d 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1f13e 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1f13f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
1f140 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f141 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1f142 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f  E./* Opcode: VCo
1f143 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20 2a 20  lumn P1 P2 P3 * 
1f144 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68  *.**.** Store th
1f145 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
1f146 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a  2-th column of.*
1f147 2a 20 74 68 65 20 72 6f 77 20 6f 66 20 74 68 65  * the row of the
1f148 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74   virtual-table t
1f149 68 61 74 20 74 68 65 20 0a 2a 2a 20 50 31 20 63  hat the .** P1 c
1f14a 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e  ursor is pointin
1f14b 67 20 74 6f 20 69 6e 74 6f 20 72 65 67 69 73 74  g to into regist
1f14c 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f  er P3..*/.case O
1f14d 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 23 69 66  P_VColumn: {.#if
1f14e 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1f14f 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1f150 6f 20 75 2e 63 69 20 2a 2f 0a 20 20 73 71 6c 69  o u.ci */.  sqli
1f151 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
1f152 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
1f153 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
1f154 3b 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a  ;.  Mem *pDest;.
1f155 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1f156 74 20 73 43 6f 6e 74 65 78 74 3b 0a 23 65 6e 64  t sContext;.#end
1f157 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1f158 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f159 20 75 2e 63 69 20 2a 2f 0a 0a 20 20 56 64 62 65   u.ci */..  Vdbe
1f15a 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70  Cursor *pCur = p
1f15b 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1f15c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
1f15d 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b  ->pVtabCursor );
1f15e 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1f15f 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
1f160 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e  =p->nMem );.  u.
1f161 63 69 2e 70 44 65 73 74 20 3d 20 26 70 2d 3e 61  ci.pDest = &p->a
1f162 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
1f163 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f  if( pCur->nullRo
1f164 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  w ){.    sqlite3
1f165 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 75  VdbeMemSetNull(u
1f166 2e 63 69 2e 70 44 65 73 74 29 3b 0a 20 20 20 20  .ci.pDest);.    
1f167 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63  break;.  }.  u.c
1f168 69 2e 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e  i.pVtab = pCur->
1f169 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74  pVtabCursor->pVt
1f16a 61 62 3b 0a 20 20 75 2e 63 69 2e 70 4d 6f 64 75  ab;.  u.ci.pModu
1f16b 6c 65 20 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d  le = u.ci.pVtab-
1f16c 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65  >pModule;.  asse
1f16d 72 74 28 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65  rt( u.ci.pModule
1f16e 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d  ->xColumn );.  m
1f16f 65 6d 73 65 74 28 26 75 2e 63 69 2e 73 43 6f 6e  emset(&u.ci.sCon
1f170 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28  text, 0, sizeof(
1f171 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 29 29 3b  u.ci.sContext));
1f172 0a 0a 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75  ..  /* The outpu
1f173 74 20 63 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61  t cell may alrea
1f174 64 79 20 68 61 76 65 20 61 20 62 75 66 66 65 72  dy have a buffer
1f175 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65   allocated. Move
1f176 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e  .  ** the curren
1f177 74 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20 75 2e  t contents to u.
1f178 63 69 2e 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f  ci.sContext.s so
1f179 20 69 6e 20 63 61 73 65 20 74 68 65 20 75 73 65   in case the use
1f17a 72 2d 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20  r-function.  ** 
1f17b 63 61 6e 20 75 73 65 20 74 68 65 20 61 6c 72 65  can use the alre
1f17c 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75  ady allocated bu
1f17d 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20  ffer instead of 
1f17e 61 6c 6c 6f 63 61 74 69 6e 67 20 61 0a 20 20 2a  allocating a.  *
1f17f 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a  * new one..  */.
1f180 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1f181 4d 6f 76 65 28 26 75 2e 63 69 2e 73 43 6f 6e 74  Move(&u.ci.sCont
1f182 65 78 74 2e 73 2c 20 75 2e 63 69 2e 70 44 65 73  ext.s, u.ci.pDes
1f183 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65  t);.  MemSetType
1f184 46 6c 61 67 28 26 75 2e 63 69 2e 73 43 6f 6e 74  Flag(&u.ci.sCont
1f185 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  ext.s, MEM_Null)
1f186 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ;..  if( sqlite3
1f187 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
1f188 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1f189 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d  o_misuse;.  rc =
1f18a 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78   u.ci.pModule->x
1f18b 43 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74  Column(pCur->pVt
1f18c 61 62 43 75 72 73 6f 72 2c 20 26 75 2e 63 69 2e  abCursor, &u.ci.
1f18d 73 43 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70  sContext, pOp->p
1f18e 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  2);.  sqlite3DbF
1f18f 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1f190 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73  sg);.  p->zErrMs
1f191 67 20 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e  g = u.ci.pVtab->
1f192 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 69 2e  zErrMsg;.  u.ci.
1f193 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d  pVtab->zErrMsg =
1f194 20 30 3b 0a 20 20 69 66 28 20 75 2e 63 69 2e 73   0;.  if( u.ci.s
1f195 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 20  Context.isError 
1f196 29 7b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 69  ){.    rc = u.ci
1f197 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f  .sContext.isErro
1f198 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70  r;.  }..  /* Cop
1f199 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  y the result of 
1f19a 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  the function to 
1f19b 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72 2e  the P3 register.
1f19c 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73   We.  ** do this
1f19d 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
1f19e 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e  hether or not an
1f19f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20   error occurred 
1f1a0 74 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 20  to ensure any.  
1f1a1 2a 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63  ** dynamic alloc
1f1a2 61 74 69 6f 6e 20 69 6e 20 75 2e 63 69 2e 73 43  ation in u.ci.sC
1f1a3 6f 6e 74 65 78 74 2e 73 20 28 61 20 4d 65 6d 20  ontext.s (a Mem 
1f1a4 73 74 72 75 63 74 29 20 69 73 20 20 72 65 6c 65  struct) is  rele
1f1a5 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  ased..  */.  sql
1f1a6 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e  ite3VdbeChangeEn
1f1a7 63 6f 64 69 6e 67 28 26 75 2e 63 69 2e 73 43 6f  coding(&u.ci.sCo
1f1a8 6e 74 65 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e  ntext.s, encodin
1f1a9 67 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54  g);.  REGISTER_T
1f1aa 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e  RACE(pOp->p3, u.
1f1ab 63 69 2e 70 44 65 73 74 29 3b 0a 20 20 73 71 6c  ci.pDest);.  sql
1f1ac 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
1f1ad 75 2e 63 69 2e 70 44 65 73 74 2c 20 26 75 2e 63  u.ci.pDest, &u.c
1f1ae 69 2e 73 43 6f 6e 74 65 78 74 2e 73 29 3b 0a 20  i.sContext.s);. 
1f1af 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1f1b0 53 49 5a 45 28 75 2e 63 69 2e 70 44 65 73 74 29  SIZE(u.ci.pDest)
1f1b1 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ;..  if( sqlite3
1f1b2 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a  SafetyOn(db) ){.
1f1b3 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1f1b4 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20  ue_to_misuse;.  
1f1b5 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  }.  if( sqlite3V
1f1b6 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63  dbeMemTooBig(u.c
1f1b7 69 2e 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20  i.pDest) ){.    
1f1b8 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20  goto too_big;.  
1f1b9 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  }.  break;.}.#en
1f1ba 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1f1bb 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
1f1bc 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
1f1bd 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1f1be 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ABLE./* Opcode: 
1f1bf 56 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20  VNext P1 P2 * * 
1f1c0 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20  *.**.** Advance 
1f1c1 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31  virtual table P1
1f1c2 20 74 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77   to the next row
1f1c3 20 69 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73   in its result s
1f1c4 65 74 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74  et and.** jump t
1f1c5 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32  o instruction P2
1f1c6 2e 20 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69  .  Or, if the vi
1f1c7 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20  rtual table has 
1f1c8 72 65 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65  reached.** the e
1f1c9 6e 64 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74  nd of its result
1f1ca 20 73 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20   set, then fall 
1f1cb 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
1f1cc 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ext instruction.
1f1cd 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78  .*/.case OP_VNex
1f1ce 74 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a  t: {   /* jump *
1f1cf 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1f1d0 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1f1d1 64 20 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a 20  d into u.cj */. 
1f1d2 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1f1d3 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71  Vtab;.  const sq
1f1d4 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1f1d5 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73  odule;.  int res
1f1d6 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ;.  VdbeCursor *
1f1d7 70 43 75 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  pCur;.#endif /* 
1f1d8 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1f1d9 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20  moved into u.cj 
1f1da 2a 2f 0a 0a 20 20 75 2e 63 6a 2e 72 65 73 20 3d  */..  u.cj.res =
1f1db 20 30 3b 0a 20 20 75 2e 63 6a 2e 70 43 75 72 20   0;.  u.cj.pCur 
1f1dc 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1f1dd 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1f1de 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 43  .cj.pCur->pVtabC
1f1df 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 75  ursor );.  if( u
1f1e0 2e 63 6a 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f  .cj.pCur->nullRo
1f1e1 77 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a  w ){.    break;.
1f1e2 20 20 7d 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62    }.  u.cj.pVtab
1f1e3 20 3d 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 56   = u.cj.pCur->pV
1f1e4 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1f1e5 3b 0a 20 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65  ;.  u.cj.pModule
1f1e6 20 3d 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 70   = u.cj.pVtab->p
1f1e7 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74  Module;.  assert
1f1e8 28 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e  ( u.cj.pModule->
1f1e9 78 4e 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49  xNext );..  /* I
1f1ea 6e 76 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 28  nvoke the xNext(
1f1eb 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  ) method of the 
1f1ec 6d 6f 64 75 6c 65 2e 20 54 68 65 72 65 20 69 73  module. There is
1f1ed 20 6e 6f 20 77 61 79 20 66 6f 72 20 74 68 65 0a   no way for the.
1f1ee 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20    ** underlying 
1f1ef 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74  implementation t
1f1f0 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  o return an erro
1f1f1 72 20 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 20  r if one occurs 
1f1f2 64 75 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78  during.  ** xNex
1f1f3 74 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 66  t(). Instead, if
1f1f4 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1f1f5 2c 20 74 72 75 65 20 69 73 20 72 65 74 75 72 6e  , true is return
1f1f6 65 64 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74  ed (indicating t
1f1f7 68 61 74 0a 20 20 2a 2a 20 64 61 74 61 20 69 73  hat.  ** data is
1f1f8 20 61 76 61 69 6c 61 62 6c 65 29 20 61 6e 64 20   available) and 
1f1f9 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72  the error code r
1f1fa 65 74 75 72 6e 65 64 20 77 68 65 6e 20 78 43 6f  eturned when xCo
1f1fb 6c 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d  lumn or.  ** som
1f1fc 65 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 20 69  e other method i
1f1fd 73 20 6e 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f  s next invoked o
1f1fe 6e 20 74 68 65 20 73 61 76 65 20 76 69 72 74 75  n the save virtu
1f1ff 61 6c 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2e  al table cursor.
1f200 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
1f201 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
1f202 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1f203 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 70  e_to_misuse;.  p
1f204 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
1f205 20 31 3b 0a 20 20 72 63 20 3d 20 75 2e 63 6a 2e   1;.  rc = u.cj.
1f206 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 75  pModule->xNext(u
1f207 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 43  .cj.pCur->pVtabC
1f208 75 72 73 6f 72 29 3b 0a 20 20 70 2d 3e 69 6e 56  ursor);.  p->inV
1f209 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20  tabMethod = 0;. 
1f20a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1f20b 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  b, p->zErrMsg);.
1f20c 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75    p->zErrMsg = u
1f20d 2e 63 6a 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  .cj.pVtab->zErrM
1f20e 73 67 3b 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62  sg;.  u.cj.pVtab
1f20f 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1f210 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1f211 4f 4b 20 29 7b 0a 20 20 20 20 75 2e 63 6a 2e 72  OK ){.    u.cj.r
1f212 65 73 20 3d 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c  es = u.cj.pModul
1f213 65 2d 3e 78 45 6f 66 28 75 2e 63 6a 2e 70 43 75  e->xEof(u.cj.pCu
1f214 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b  r->pVtabCursor);
1f215 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
1f216 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1f217 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1f218 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66  to_misuse;..  if
1f219 28 20 21 75 2e 63 6a 2e 72 65 73 20 29 7b 0a 20  ( !u.cj.res ){. 
1f21a 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69     /* If there i
1f21b 73 20 64 61 74 61 2c 20 6a 75 6d 70 20 74 6f 20  s data, jump to 
1f21c 50 32 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70  P2 */.    pc = p
1f21d 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a  Op->p2 - 1;.  }.
1f21e 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1f21f 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1f220 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1f221 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1f222 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1f223 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52  LE./* Opcode: VR
1f224 65 6e 61 6d 65 20 50 31 20 2a 20 2a 20 50 34 20  ename P1 * * P4 
1f225 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  *.**.** P4 is a 
1f226 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72  pointer to a vir
1f227 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63  tual table objec
1f228 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74  t, an sqlite3_vt
1f229 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ab structure..**
1f22a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
1f22b 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70  okes the corresp
1f22c 6f 6e 64 69 6e 67 20 78 52 65 6e 61 6d 65 20 6d  onding xRename m
1f22d 65 74 68 6f 64 2e 20 54 68 65 20 76 61 6c 75 65  ethod. The value
1f22e 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20  .** in register 
1f22f 50 31 20 69 73 20 70 61 73 73 65 64 20 61 73 20  P1 is passed as 
1f230 74 68 65 20 7a 4e 61 6d 65 20 61 72 67 75 6d 65  the zName argume
1f231 6e 74 20 74 6f 20 74 68 65 20 78 52 65 6e 61 6d  nt to the xRenam
1f232 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73  e method..*/.cas
1f233 65 20 4f 50 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a  e OP_VRename: {.
1f234 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1f235 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1f236 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 20 20 73  into u.ck */.  s
1f237 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
1f238 61 62 3b 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65  ab;.  Mem *pName
1f239 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1f23a 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1f23b 64 20 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 0a  d into u.ck */..
1f23c 20 20 75 2e 63 6b 2e 70 56 74 61 62 20 3d 20 70    u.ck.pVtab = p
1f23d 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56  Op->p4.pVtab->pV
1f23e 74 61 62 3b 0a 20 20 75 2e 63 6b 2e 70 4e 61 6d  tab;.  u.ck.pNam
1f23f 65 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  e = &p->aMem[pOp
1f240 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1f241 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f   u.ck.pVtab->pMo
1f242 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b  dule->xRename );
1f243 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1f244 45 28 70 4f 70 2d 3e 70 31 2c 20 75 2e 63 6b 2e  E(pOp->p1, u.ck.
1f245 70 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74  pName);.  assert
1f246 28 20 75 2e 63 6b 2e 70 4e 61 6d 65 2d 3e 66 6c  ( u.ck.pName->fl
1f247 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 3b  ags & MEM_Str );
1f248 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1f249 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1f24a 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1f24b 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75  misuse;.  rc = u
1f24c 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  .ck.pVtab->pModu
1f24d 6c 65 2d 3e 78 52 65 6e 61 6d 65 28 75 2e 63 6b  le->xRename(u.ck
1f24e 2e 70 56 74 61 62 2c 20 75 2e 63 6b 2e 70 4e 61  .pVtab, u.ck.pNa
1f24f 6d 65 2d 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65  me->z);.  sqlite
1f250 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1f251 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45  ErrMsg);.  p->zE
1f252 72 72 4d 73 67 20 3d 20 75 2e 63 6b 2e 70 56 74  rrMsg = u.ck.pVt
1f253 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75  ab->zErrMsg;.  u
1f254 2e 63 6b 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  .ck.pVtab->zErrM
1f255 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71  sg = 0;.  if( sq
1f256 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1f257 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1f258 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20  ue_to_misuse;.. 
1f259 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1f25a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1f25b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1f25c 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 55  LE./* Opcode: VU
1f25d 70 64 61 74 65 20 50 31 20 50 32 20 50 33 20 50  pdate P1 P2 P3 P
1f25e 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20  4 *.**.** P4 is 
1f25f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76  a pointer to a v
1f260 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a  irtual table obj
1f261 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f  ect, an sqlite3_
1f262 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a  vtab structure..
1f263 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  ** This opcode i
1f264 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65  nvokes the corre
1f265 73 70 6f 6e 64 69 6e 67 20 78 55 70 64 61 74 65  sponding xUpdate
1f266 20 6d 65 74 68 6f 64 2e 20 50 32 20 76 61 6c 75   method. P2 valu
1f267 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 69 67  es.** are contig
1f268 75 6f 75 73 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  uous memory cell
1f269 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 50 33  s starting at P3
1f26a 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20   to pass to the 
1f26b 78 55 70 64 61 74 65 20 0a 2a 2a 20 69 6e 76 6f  xUpdate .** invo
1f26c 63 61 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75  cation. The valu
1f26d 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 28 50  e in register (P
1f26e 33 2b 50 32 2d 31 29 20 63 6f 72 72 65 73 70 6f  3+P2-1) correspo
1f26f 6e 64 73 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70  nds to the .** p
1f270 32 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  2th element of t
1f271 68 65 20 61 72 67 76 20 61 72 72 61 79 20 70 61  he argv array pa
1f272 73 73 65 64 20 74 6f 20 78 55 70 64 61 74 65 2e  ssed to xUpdate.
1f273 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 55 70 64 61  .**.** The xUpda
1f274 74 65 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20 64  te method will d
1f275 6f 20 61 20 44 45 4c 45 54 45 20 6f 72 20 61 6e  o a DELETE or an
1f276 20 49 4e 53 45 52 54 20 6f 72 20 62 6f 74 68 2e   INSERT or both.
1f277 0a 2a 2a 20 54 68 65 20 61 72 67 76 5b 30 5d 20  .** The argv[0] 
1f278 65 6c 65 6d 65 6e 74 20 28 77 68 69 63 68 20 63  element (which c
1f279 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 6d 65  orresponds to me
1f27a 6d 6f 72 79 20 63 65 6c 6c 20 50 33 29 0a 2a 2a  mory cell P3).**
1f27b 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66   is the rowid of
1f27c 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65   a row to delete
1f27d 2e 20 20 49 66 20 61 72 67 76 5b 30 5d 20 69 73  .  If argv[0] is
1f27e 20 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 0a 2a   NULL then no .*
1f27f 2a 20 64 65 6c 65 74 69 6f 6e 20 6f 63 63 75 72  * deletion occur
1f280 73 2e 20 20 54 68 65 20 61 72 67 76 5b 31 5d 20  s.  The argv[1] 
1f281 65 6c 65 6d 65 6e 74 20 69 73 20 74 68 65 20 72  element is the r
1f282 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20  owid of the new 
1f283 0a 2a 2a 20 72 6f 77 2e 20 20 54 68 69 73 20 63  .** row.  This c
1f284 61 6e 20 62 65 20 4e 55 4c 4c 20 74 6f 20 68 61  an be NULL to ha
1f285 76 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ve the virtual t
1f286 61 62 6c 65 20 73 65 6c 65 63 74 20 74 68 65 20  able select the 
1f287 6e 65 77 20 0a 2a 2a 20 72 6f 77 69 64 20 66 6f  new .** rowid fo
1f288 72 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20 73  r itself.  The s
1f289 75 62 73 65 71 75 65 6e 74 20 65 6c 65 6d 65 6e  ubsequent elemen
1f28a 74 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 20  ts in the array 
1f28b 61 72 65 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75  are .** the valu
1f28c 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  es of columns in
1f28d 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a   the new row..**
1f28e 0a 2a 2a 20 49 66 20 50 32 3d 3d 31 20 74 68 65  .** If P2==1 the
1f28f 6e 20 6e 6f 20 69 6e 73 65 72 74 20 69 73 20 70  n no insert is p
1f290 65 72 66 6f 72 6d 65 64 2e 20 20 61 72 67 76 5b  erformed.  argv[
1f291 30 5d 20 69 73 20 74 68 65 20 72 6f 77 69 64 20  0] is the rowid 
1f292 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 74 6f 20 64  of.** a row to d
1f293 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 20  elete..**.** P1 
1f294 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 66 6c 61  is a boolean fla
1f295 67 2e 20 49 66 20 69 74 20 69 73 20 73 65 74 20  g. If it is set 
1f296 74 6f 20 74 72 75 65 20 61 6e 64 20 74 68 65 20  to true and the 
1f297 78 55 70 64 61 74 65 20 63 61 6c 6c 0a 2a 2a 20  xUpdate call.** 
1f298 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74  is successful, t
1f299 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  hen the value re
1f29a 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
1f29b 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
1f29c 77 69 64 28 29 20 0a 2a 2a 20 69 73 20 73 65 74  wid() .** is set
1f29d 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
1f29e 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74   the rowid for t
1f29f 68 65 20 72 6f 77 20 6a 75 73 74 20 69 6e 73 65  he row just inse
1f2a0 72 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  rted..*/.case OP
1f2a1 5f 56 55 70 64 61 74 65 3a 20 7b 0a 23 69 66 20  _VUpdate: {.#if 
1f2a2 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1f2a3 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f2a4 20 75 2e 63 6c 20 2a 2f 0a 20 20 73 71 6c 69 74   u.cl */.  sqlit
1f2a5 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1f2a6 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65    sqlite3_module
1f2a7 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74   *pModule;.  int
1f2a8 20 6e 41 72 67 3b 0a 20 20 69 6e 74 20 69 3b 0a   nArg;.  int i;.
1f2a9 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72    sqlite_int64 r
1f2aa 6f 77 69 64 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70  owid;.  Mem **ap
1f2ab 41 72 67 3b 0a 20 20 4d 65 6d 20 2a 70 58 3b 0a  Arg;.  Mem *pX;.
1f2ac 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1f2ad 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1f2ae 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f 0a 0a 20 20  into u.cl */..  
1f2af 75 2e 63 6c 2e 70 56 74 61 62 20 3d 20 70 4f 70  u.cl.pVtab = pOp
1f2b0 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61  ->p4.pVtab->pVta
1f2b1 62 3b 0a 20 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c  b;.  u.cl.pModul
1f2b2 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64  e = (sqlite3_mod
1f2b3 75 6c 65 20 2a 29 75 2e 63 6c 2e 70 56 74 61 62  ule *)u.cl.pVtab
1f2b4 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 75 2e 63  ->pModule;.  u.c
1f2b5 6c 2e 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32  l.nArg = pOp->p2
1f2b6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1f2b7 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42  >p4type==P4_VTAB
1f2b8 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53   );.  if( ALWAYS
1f2b9 28 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78  (u.cl.pModule->x
1f2ba 55 70 64 61 74 65 29 20 29 7b 0a 20 20 20 20 75  Update) ){.    u
1f2bb 2e 63 6c 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61  .cl.apArg = p->a
1f2bc 70 41 72 67 3b 0a 20 20 20 20 75 2e 63 6c 2e 70  pArg;.    u.cl.p
1f2bd 58 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  X = &p->aMem[pOp
1f2be 2d 3e 70 33 5d 3b 0a 20 20 20 20 66 6f 72 28 75  ->p3];.    for(u
1f2bf 2e 63 6c 2e 69 3d 30 3b 20 75 2e 63 6c 2e 69 3c  .cl.i=0; u.cl.i<
1f2c0 75 2e 63 6c 2e 6e 41 72 67 3b 20 75 2e 63 6c 2e  u.cl.nArg; u.cl.
1f2c1 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
1f2c2 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54  te3VdbeMemStoreT
1f2c3 79 70 65 28 75 2e 63 6c 2e 70 58 29 3b 0a 20 20  ype(u.cl.pX);.  
1f2c4 20 20 20 20 75 2e 63 6c 2e 61 70 41 72 67 5b 75      u.cl.apArg[u
1f2c5 2e 63 6c 2e 69 5d 20 3d 20 75 2e 63 6c 2e 70 58  .cl.i] = u.cl.pX
1f2c6 3b 0a 20 20 20 20 20 20 75 2e 63 6c 2e 70 58 2b  ;.      u.cl.pX+
1f2c7 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  +;.    }.    if(
1f2c8 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1f2c9 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1f2ca 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1f2cb 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 6c 2e  ;.    rc = u.cl.
1f2cc 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65  pModule->xUpdate
1f2cd 28 75 2e 63 6c 2e 70 56 74 61 62 2c 20 75 2e 63  (u.cl.pVtab, u.c
1f2ce 6c 2e 6e 41 72 67 2c 20 75 2e 63 6c 2e 61 70 41  l.nArg, u.cl.apA
1f2cf 72 67 2c 20 26 75 2e 63 6c 2e 72 6f 77 69 64 29  rg, &u.cl.rowid)
1f2d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
1f2d1 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1f2d2 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72  sg);.    p->zErr
1f2d3 4d 73 67 20 3d 20 75 2e 63 6c 2e 70 56 74 61 62  Msg = u.cl.pVtab
1f2d4 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75  ->zErrMsg;.    u
1f2d5 2e 63 6c 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  .cl.pVtab->zErrM
1f2d6 73 67 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  sg = 0;.    if( 
1f2d7 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1f2d8 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1f2d9 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1f2da 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1f2db 54 45 5f 4f 4b 20 26 26 20 70 4f 70 2d 3e 70 31  TE_OK && pOp->p1
1f2dc 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
1f2dd 28 20 75 2e 63 6c 2e 6e 41 72 67 3e 31 20 26 26  ( u.cl.nArg>1 &&
1f2de 20 75 2e 63 6c 2e 61 70 41 72 67 5b 30 5d 20 26   u.cl.apArg[0] &
1f2df 26 20 28 75 2e 63 6c 2e 61 70 41 72 67 5b 30 5d  & (u.cl.apArg[0]
1f2e0 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c  ->flags&MEM_Null
1f2e1 29 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c  ) );.      db->l
1f2e2 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 63 6c 2e  astRowid = u.cl.
1f2e3 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20  rowid;.    }.   
1f2e4 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20   p->nChange++;. 
1f2e5 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1f2e6 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1f2e7 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1f2e8 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 20 53 51   */..#ifndef  SQ
1f2e9 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
1f2ea 50 52 41 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64  PRAGMAS./* Opcod
1f2eb 65 3a 20 50 61 67 65 63 6f 75 6e 74 20 50 31 20  e: Pagecount P1 
1f2ec 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57  P2 * * *.**.** W
1f2ed 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74  rite the current
1f2ee 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
1f2ef 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 20   in database P1 
1f2f0 74 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50  to memory cell P
1f2f1 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61  2..*/.case OP_Pa
1f2f2 67 65 63 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20  gecount: {      
1f2f3 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1f2f4 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20  erelease */.#if 
1f2f5 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1f2f6 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f2f7 20 75 2e 63 6d 20 2a 2f 0a 20 20 69 6e 74 20 70   u.cm */.  int p
1f2f8 31 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a  1;.  int nPage;.
1f2f9 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
1f2fa 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1f2fb 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f2fc 20 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a 0a 20   into u.cm */.. 
1f2fd 20 75 2e 63 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e   u.cm.p1 = pOp->
1f2fe 70 31 3b 0a 20 20 75 2e 63 6d 2e 70 50 61 67 65  p1;.  u.cm.pPage
1f2ff 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
1f300 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 75 2e  Pager(db->aDb[u.
1f301 63 6d 2e 70 31 5d 2e 70 42 74 29 3b 0a 20 20 72  cm.p1].pBt);.  r
1f302 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1f303 50 61 67 65 63 6f 75 6e 74 28 75 2e 63 6d 2e 70  Pagecount(u.cm.p
1f304 50 61 67 65 72 2c 20 26 75 2e 63 6d 2e 6e 50 61  Pager, &u.cm.nPa
1f305 67 65 29 3b 0a 20 20 2f 2a 20 4f 50 5f 50 61 67  ge);.  /* OP_Pag
1f306 65 63 6f 75 6e 74 20 69 73 20 61 6c 77 61 79 73  ecount is always
1f307 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
1f308 68 69 6e 20 61 20 72 65 61 64 20 74 72 61 6e 73  hin a read trans
1f309 61 63 74 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a  action.  The.  *
1f30a 2a 20 70 61 67 65 20 63 6f 75 6e 74 20 68 61 73  * page count has
1f30b 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 75   already been su
1f30c 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20  ccessfully read 
1f30d 61 6e 64 20 63 61 63 68 65 64 2e 20 20 53 6f 20  and cached.  So 
1f30e 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  the.  ** sqlite3
1f30f 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29  PagerPagecount()
1f310 20 63 61 6c 6c 20 61 62 6f 76 65 20 63 61 6e 6e   call above cann
1f311 6f 74 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 69 66  ot fail. */.  if
1f312 28 20 41 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c  ( ALWAYS(rc==SQL
1f313 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 70  ITE_OK) ){.    p
1f314 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
1f315 5f 49 6e 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  _Int;.    pOut->
1f316 75 2e 69 20 3d 20 75 2e 63 6d 2e 6e 50 61 67 65  u.i = u.cm.nPage
1f317 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1f318 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
1f319 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
1f31a 43 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72  CE./* Opcode: Tr
1f31b 61 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a  ace * * * P4 *.*
1f31c 2a 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e 67 20  *.** If tracing 
1f31d 69 73 20 65 6e 61 62 6c 65 64 20 28 62 79 20 74  is enabled (by t
1f31e 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65  he sqlite3_trace
1f31f 28 29 29 20 69 6e 74 65 72 66 61 63 65 2c 20 74  ()) interface, t
1f320 68 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d 38  hen.** the UTF-8
1f321 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 65   string containe
1f322 64 20 69 6e 20 50 34 20 69 73 20 65 6d 69 74 74  d in P4 is emitt
1f323 65 64 20 6f 6e 20 74 68 65 20 74 72 61 63 65 20  ed on the trace 
1f324 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 73  callback..*/.cas
1f325 65 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 23 69  e OP_Trace: {.#i
1f326 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1f327 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1f328 74 6f 20 75 2e 63 6e 20 2a 2f 0a 20 20 63 68 61  to u.cn */.  cha
1f329 72 20 2a 7a 54 72 61 63 65 3b 0a 23 65 6e 64 69  r *zTrace;.#endi
1f32a 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1f32b 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1f32c 75 2e 63 6e 20 2a 2f 0a 0a 20 20 75 2e 63 6e 2e  u.cn */..  u.cn.
1f32d 7a 54 72 61 63 65 20 3d 20 28 70 4f 70 2d 3e 70  zTrace = (pOp->p
1f32e 34 2e 7a 20 3f 20 70 4f 70 2d 3e 70 34 2e 7a 20  4.z ? pOp->p4.z 
1f32f 3a 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 69 66  : p->zSql);.  if
1f330 28 20 75 2e 63 6e 2e 7a 54 72 61 63 65 20 29 7b  ( u.cn.zTrace ){
1f331 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 54 72  .    if( db->xTr
1f332 61 63 65 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  ace ){.      db-
1f333 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54 72 61  >xTrace(db->pTra
1f334 63 65 41 72 67 2c 20 75 2e 63 6e 2e 7a 54 72 61  ceArg, u.cn.zTra
1f335 63 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65  ce);.    }.#ifde
1f336 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
1f337 20 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67     if( (db->flag
1f338 73 20 26 20 53 51 4c 49 54 45 5f 53 71 6c 54 72  s & SQLITE_SqlTr
1f339 61 63 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ace)!=0 ){.     
1f33a 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
1f33b 6e 74 66 28 22 53 51 4c 2d 74 72 61 63 65 3a 20  ntf("SQL-trace: 
1f33c 25 73 5c 6e 22 2c 20 75 2e 63 6e 2e 7a 54 72 61  %s\n", u.cn.zTra
1f33d 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ce);.    }.#endi
1f33e 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55  f /* SQLITE_DEBU
1f33f 47 20 2a 2f 0a 20 20 7d 0a 20 20 62 72 65 61 6b  G */.  }.  break
1f340 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20  ;.}.#endif.../* 
1f341 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70 20 2a 20 2a  Opcode: Noop * *
1f342 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20   * * *.**.** Do 
1f343 6e 6f 74 68 69 6e 67 2e 20 20 54 68 69 73 20 69  nothing.  This i
1f344 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 66  nstruction is of
1f345 74 65 6e 20 75 73 65 66 75 6c 20 61 73 20 61 20  ten useful as a 
1f346 6a 75 6d 70 0a 2a 2a 20 64 65 73 74 69 6e 61 74  jump.** destinat
1f347 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68  ion..*/./*.** Th
1f348 65 20 6d 61 67 69 63 20 45 78 70 6c 61 69 6e 20  e magic Explain 
1f349 6f 70 63 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20  opcode are only 
1f34a 69 6e 73 65 72 74 65 64 20 77 68 65 6e 20 65 78  inserted when ex
1f34b 70 6c 61 69 6e 3d 3d 32 20 28 77 68 69 63 68 0a  plain==2 (which.
1f34c 2a 2a 20 69 73 20 74 6f 20 73 61 79 20 77 68 65  ** is to say whe
1f34d 6e 20 74 68 65 20 45 58 50 4c 41 49 4e 20 51 55  n the EXPLAIN QU
1f34e 45 52 59 20 50 4c 41 4e 20 73 79 6e 74 61 78 20  ERY PLAN syntax 
1f34f 69 73 20 75 73 65 64 2e 29 0a 2a 2a 20 54 68 69  is used.).** Thi
1f350 73 20 6f 70 63 6f 64 65 20 72 65 63 6f 72 64 73  s opcode records
1f351 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
1f352 6d 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e  m the optimizer.
1f353 20 20 49 74 20 69 73 20 74 68 65 0a 2a 2a 20 74    It is the.** t
1f354 68 65 20 73 61 6d 65 20 61 73 20 61 20 6e 6f 2d  he same as a no-
1f355 6f 70 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65  op.  This opcode
1f356 73 6e 65 76 65 72 20 61 70 70 65 61 72 73 20 69  snever appears i
1f357 6e 20 61 20 72 65 61 6c 20 56 4d 20 70 72 6f 67  n a real VM prog
1f358 72 61 6d 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74 3a  ram..*/.default:
1f359 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54   {          /* T
1f35a 68 69 73 20 69 73 20 72 65 61 6c 6c 79 20 4f 50  his is really OP
1f35b 5f 4e 6f 6f 70 20 61 6e 64 20 4f 50 5f 45 78 70  _Noop and OP_Exp
1f35c 6c 61 69 6e 20 2a 2f 0a 20 20 62 72 65 61 6b 3b  lain */.  break;
1f35d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
1f35e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f35f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f361 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f362 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 73 65 73 20  **.** The cases 
1f363 6f 66 20 74 68 65 20 73 77 69 74 63 68 20 73 74  of the switch st
1f364 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 20 74 68  atement above th
1f365 69 73 20 6c 69 6e 65 20 73 68 6f 75 6c 64 20 61  is line should a
1f366 6c 6c 20 62 65 20 69 6e 64 65 6e 74 65 64 0a 2a  ll be indented.*
1f367 2a 20 62 79 20 36 20 73 70 61 63 65 73 2e 20 20  * by 6 spaces.  
1f368 42 75 74 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73  But the left-mos
1f369 74 20 36 20 73 70 61 63 65 73 20 68 61 76 65 20  t 6 spaces have 
1f36a 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 74 6f 20  been removed to 
1f36b 69 6d 70 72 6f 76 65 20 74 68 65 0a 2a 2a 20 72  improve the.** r
1f36c 65 61 64 61 62 69 6c 69 74 79 2e 20 20 46 72 6f  eadability.  Fro
1f36d 6d 20 74 68 69 73 20 70 6f 69 6e 74 20 6f 6e 20  m this point on 
1f36e 64 6f 77 6e 2c 20 74 68 65 20 6e 6f 72 6d 61 6c  down, the normal
1f36f 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 72 75 6c   indentation rul
1f370 65 73 20 61 72 65 0a 2a 2a 20 72 65 73 74 6f 72  es are.** restor
1f371 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ed..************
1f372 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f373 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f374 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f375 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f376 2a 2f 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66  */.    }..#ifdef
1f377 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20   VDBE_PROFILE.  
1f378 20 20 7b 0a 20 20 20 20 20 20 75 36 34 20 65 6c    {.      u64 el
1f379 61 70 73 65 64 20 3d 20 73 71 6c 69 74 65 33 48  apsed = sqlite3H
1f37a 77 74 69 6d 65 28 29 20 2d 20 73 74 61 72 74 3b  wtime() - start;
1f37b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c  .      pOp->cycl
1f37c 65 73 20 2b 3d 20 65 6c 61 70 73 65 64 3b 0a 20  es += elapsed;. 
1f37d 20 20 20 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b       pOp->cnt++;
1f37e 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20 20 66  .#if 0.        f
1f37f 70 72 69 6e 74 66 28 73 74 64 6f 75 74 2c 20 22  printf(stdout, "
1f380 25 31 30 6c 6c 75 20 22 2c 20 65 6c 61 70 73 65  %10llu ", elapse
1f381 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  d);.        sqli
1f382 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73  te3VdbePrintOp(s
1f383 74 64 6f 75 74 2c 20 6f 72 69 67 50 63 2c 20 26  tdout, origPc, &
1f384 70 2d 3e 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b  p->aOp[origPc]);
1f385 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65  .#endif.    }.#e
1f386 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65  ndif..    /* The
1f387 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20   following code 
1f388 61 64 64 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20  adds nothing to 
1f389 74 68 65 20 61 63 74 75 61 6c 20 66 75 6e 63 74  the actual funct
1f38a 69 6f 6e 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20  ionality.    ** 
1f38b 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20  of the program. 
1f38c 20 49 74 20 69 73 20 6f 6e 6c 79 20 68 65 72 65   It is only here
1f38d 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
1f38e 20 64 65 62 75 67 67 69 6e 67 2e 0a 20 20 20 20   debugging..    
1f38f 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20  ** On the other 
1f390 68 61 6e 64 2c 20 69 74 20 64 6f 65 73 20 62 75  hand, it does bu
1f391 72 6e 20 43 50 55 20 63 79 63 6c 65 73 20 65 76  rn CPU cycles ev
1f392 65 72 79 20 74 69 6d 65 20 74 68 72 6f 75 67 68  ery time through
1f393 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 76 61 6c  .    ** the eval
1f394 75 61 74 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20  uator loop.  So 
1f395 77 65 20 63 61 6e 20 6c 65 61 76 65 20 69 74 20  we can leave it 
1f396 6f 75 74 20 77 68 65 6e 20 4e 44 45 42 55 47 20  out when NDEBUG 
1f397 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20  is defined..    
1f398 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  */.#ifndef NDEBU
1f399 47 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63  G.    assert( pc
1f39a 3e 3d 2d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f  >=-1 && pc<p->nO
1f39b 70 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  p );..#ifdef SQL
1f39c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66  ITE_DEBUG.    if
1f39d 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20  ( p->trace ){.  
1f39e 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 20      if( rc!=0 ) 
1f39f 66 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65  fprintf(p->trace
1f3a0 2c 22 72 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a  ,"rc=%d\n",rc);.
1f3a1 20 20 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70        if( opProp
1f3a2 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54  erty & OPFLG_OUT
1f3a3 32 5f 50 52 45 52 45 4c 45 41 53 45 20 29 7b 0a  2_PRERELEASE ){.
1f3a4 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72          register
1f3a5 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 20  Trace(p->trace, 
1f3a6 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a  pOp->p2, pOut);.
1f3a7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1f3a8 28 20 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f  ( opProperty & O
1f3a9 50 46 4c 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20  PFLG_OUT3 ){.   
1f3aa 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72 61       registerTra
1f3ab 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70  ce(p->trace, pOp
1f3ac 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 20  ->p3, pOut);.   
1f3ad 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
1f3ae 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42  f  /* SQLITE_DEB
1f3af 55 47 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a  UG */.#endif  /*
1f3b0 20 4e 44 45 42 55 47 20 2a 2f 0a 20 20 7d 20 20   NDEBUG */.  }  
1f3b1 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 74 68  /* The end of th
1f3b2 65 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20 74  e for(;;) loop t
1f3b3 68 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68  he loops through
1f3b4 20 6f 70 63 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f   opcodes */..  /
1f3b5 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68  * If we reach th
1f3b6 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61  is point, it mea
1f3b7 6e 73 20 74 68 61 74 20 65 78 65 63 75 74 69 6f  ns that executio
1f3b8 6e 20 69 73 20 66 69 6e 69 73 68 65 64 20 77 69  n is finished wi
1f3b9 74 68 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72  th.  ** an error
1f3ba 20 6f 66 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 20   of some kind.. 
1f3bb 20 2a 2f 0a 76 64 62 65 5f 65 72 72 6f 72 5f 68   */.vdbe_error_h
1f3bc 61 6c 74 3a 0a 20 20 61 73 73 65 72 74 28 20 72  alt:.  assert( r
1f3bd 63 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72  c );.  p->rc = r
1f3be 63 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  c;.  sqlite3Vdbe
1f3bf 48 61 6c 74 28 70 29 3b 0a 20 20 69 66 28 20 72  Halt(p);.  if( r
1f3c0 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c==SQLITE_IOERR_
1f3c1 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c  NOMEM ) db->mall
1f3c2 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
1f3c3 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1f3c4 52 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73  R;..  /* This is
1f3c5 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 6f 75   the only way ou
1f3c6 74 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64  t of this proced
1f3c7 75 72 65 2e 20 20 57 65 20 68 61 76 65 20 74 6f  ure.  We have to
1f3c8 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 74 68  .  ** release th
1f3c9 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 62 74 72  e mutexes on btr
1f3ca 65 65 73 20 74 68 61 74 20 77 65 72 65 20 61 63  ees that were ac
1f3cb 71 75 69 72 65 64 20 61 74 20 74 68 65 0a 20 20  quired at the.  
1f3cc 2a 2a 20 74 6f 70 2e 20 2a 2f 0a 76 64 62 65 5f  ** top. */.vdbe_
1f3cd 72 65 74 75 72 6e 3a 0a 20 20 73 71 6c 69 74 65  return:.  sqlite
1f3ce 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  3BtreeMutexArray
1f3cf 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78  Leave(&p->aMutex
1f3d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1f3d1 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65  .  /* Jump to he
1f3d2 72 65 20 69 66 20 61 20 73 74 72 69 6e 67 20 6f  re if a string o
1f3d3 72 20 62 6c 6f 62 20 6c 61 72 67 65 72 20 74 68  r blob larger th
1f3d4 61 6e 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  an SQLITE_MAX_LE
1f3d5 4e 47 54 48 0a 20 20 2a 2a 20 69 73 20 65 6e 63  NGTH.  ** is enc
1f3d6 6f 75 6e 74 65 72 65 64 2e 0a 20 20 2a 2f 0a 74  ountered..  */.t
1f3d7 6f 6f 5f 62 69 67 3a 0a 20 20 73 71 6c 69 74 65  oo_big:.  sqlite
1f3d8 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1f3d9 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 73 74 72  ErrMsg, db, "str
1f3da 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20  ing or blob too 
1f3db 62 69 67 22 29 3b 0a 20 20 72 63 20 3d 20 53 51  big");.  rc = SQ
1f3dc 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 67  LITE_TOOBIG;.  g
1f3dd 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68  oto vdbe_error_h
1f3de 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20  alt;..  /* Jump 
1f3df 74 6f 20 68 65 72 65 20 69 66 20 61 20 6d 61 6c  to here if a mal
1f3e0 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a  loc() fails..  *
1f3e1 2f 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e  /.no_mem:.  db->
1f3e2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
1f3e3 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 74  ;.  sqlite3SetSt
1f3e4 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1f3e5 2c 20 64 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65  , db, "out of me
1f3e6 6d 6f 72 79 22 29 3b 0a 20 20 72 63 20 3d 20 53  mory");.  rc = S
1f3e7 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 67  QLITE_NOMEM;.  g
1f3e8 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68  oto vdbe_error_h
1f3e9 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20  alt;..  /* Jump 
1f3ea 74 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 20 53  to here for an S
1f3eb 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72  QLITE_MISUSE err
1f3ec 6f 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64  or..  */.abort_d
1f3ed 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3a 0a 20 20  ue_to_misuse:.  
1f3ee 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55  rc = SQLITE_MISU
1f3ef 53 45 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68  SE;.  /* Fall th
1f3f0 72 75 20 69 6e 74 6f 20 61 62 6f 72 74 5f 64 75  ru into abort_du
1f3f1 65 5f 74 6f 5f 65 72 72 6f 72 20 2a 2f 0a 0a 20  e_to_error */.. 
1f3f2 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65   /* Jump to here
1f3f3 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6b   for any other k
1f3f4 69 6e 64 20 6f 66 20 66 61 74 61 6c 20 65 72 72  ind of fatal err
1f3f5 6f 72 2e 20 20 54 68 65 20 22 72 63 22 20 76 61  or.  The "rc" va
1f3f6 72 69 61 62 6c 65 0a 20 20 2a 2a 20 73 68 6f 75  riable.  ** shou
1f3f7 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f  ld hold the erro
1f3f8 72 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 61  r number..  */.a
1f3f9 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1f3fa 72 3a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  r:.  assert( p->
1f3fb 7a 45 72 72 4d 73 67 3d 3d 30 20 29 3b 0a 20 20  zErrMsg==0 );.  
1f3fc 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1f3fd 69 6c 65 64 20 29 20 72 63 20 3d 20 53 51 4c 49  iled ) rc = SQLI
1f3fe 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 69 66 28 20  TE_NOMEM;.  if( 
1f3ff 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc!=SQLITE_IOERR
1f400 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71  _NOMEM ){.    sq
1f401 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1f402 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1f403 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72  "%s", sqlite3Err
1f404 53 74 72 28 72 63 29 29 3b 0a 20 20 7d 0a 20 20  Str(rc));.  }.  
1f405 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f  goto vdbe_error_
1f406 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70  halt;..  /* Jump
1f407 20 74 6f 20 68 65 72 65 20 69 66 20 74 68 65 20   to here if the 
1f408 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
1f409 74 28 29 20 41 50 49 20 73 65 74 73 20 74 68 65  t() API sets the
1f40a 20 69 6e 74 65 72 72 75 70 74 0a 20 20 2a 2a 20   interrupt.  ** 
1f40b 66 6c 61 67 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74  flag..  */.abort
1f40c 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70  _due_to_interrup
1f40d 74 3a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  t:.  assert( db-
1f40e 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65  >u1.isInterrupte
1f40f 64 20 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49  d );.  rc = SQLI
1f410 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20  TE_INTERRUPT;.  
1f411 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71  p->rc = rc;.  sq
1f412 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1f413 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1f414 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72  "%s", sqlite3Err
1f415 53 74 72 28 72 63 29 29 3b 0a 20 20 67 6f 74 6f  Str(rc));.  goto
1f416 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74   vdbe_error_halt
1f417 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
1f418 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65  **** End of vdbe
1f419 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1f41a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f41b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f41c 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1f41d 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1f41e 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a  vdbeblob.c *****
1f41f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f421 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
1f422 4d 61 79 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20  May 1.**.** The 
1f423 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1f424 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1f425 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1f426 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1f427 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1f428 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1f429 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1f42a 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1f42b 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1f42c 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1f42d 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1f42e 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1f42f 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1f430 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1f431 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1f432 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1f433 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1f434 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f435 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f436 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f437 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f438 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
1f439 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
1f43a 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  de used to imple
1f43b 6d 65 6e 74 20 69 6e 63 72 65 6d 65 6e 74 61 6c  ment incremental
1f43c 20 42 4c 4f 42 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a   BLOB I/O..**.**
1f43d 20 24 49 64 3a 20 76 64 62 65 62 6c 6f 62 2e 63   $Id: vdbeblob.c
1f43e 2c 76 20 31 2e 33 35 20 32 30 30 39 2f 30 37 2f  ,v 1.35 2009/07/
1f43f 30 32 20 30 37 3a 34 37 3a 33 33 20 64 61 6e 69  02 07:47:33 dani
1f440 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
1f441 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1f442 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
1f443 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c  ./*.** Valid sql
1f444 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c  ite3_blob* handl
1f445 65 73 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72  es point to Incr
1f446 62 6c 6f 62 20 73 74 72 75 63 74 75 72 65 73 2e  blob structures.
1f447 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
1f448 63 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72  ct Incrblob Incr
1f449 62 6c 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e 63  blob;.struct Inc
1f44a 72 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c  rblob {.  int fl
1f44b 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
1f44c 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c    /* Copy of "fl
1f44d 61 67 73 22 20 70 61 73 73 65 64 20 74 6f 20 73  ags" passed to s
1f44e 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
1f44f 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  () */.  int nByt
1f450 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1f451 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20  /* Size of open 
1f452 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73 20 2a  blob, in bytes *
1f453 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b  /.  int iOffset;
1f454 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
1f455 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 62 6c  yte offset of bl
1f456 6f 62 20 69 6e 20 63 75 72 73 6f 72 20 64 61 74  ob in cursor dat
1f457 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20  a */.  BtCursor 
1f458 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20 2f  *pCsr;         /
1f459 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  * Cursor pointin
1f45a 67 20 61 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f  g at blob row */
1f45b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
1f45c 2a 70 53 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74  *pStmt;    /* St
1f45d 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20  atement holding 
1f45e 63 75 72 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20  cursor open */. 
1f45f 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
1f460 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1f461 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62  associated datab
1f462 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ase */.};../*.**
1f463 20 4f 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e   Open a blob han
1f464 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
1f465 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1f466 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69  lob_open(.  sqli
1f467 74 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20  te3* db,        
1f468 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1f469 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
1f46a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1f46b 7a 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  zDb,        /* T
1f46c 68 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61  he attached data
1f46d 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
1f46e 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f  the blob */.  co
1f46f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
1f470 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  ,     /* The tab
1f471 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
1f472 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73  e blob */.  cons
1f473 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c  t char *zColumn,
1f474 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d      /* The colum
1f475 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  n containing the
1f476 20 62 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74   blob */.  sqlit
1f477 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20  e_int64 iRow,   
1f478 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f     /* The row co
1f479 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f  ntaining the glo
1f47a 62 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  b */.  int flags
1f47b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1f47c 2a 20 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77  * True -> read/w
1f47d 72 69 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c  rite access, fal
1f47e 73 65 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20  se -> read-only 
1f47f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  */.  sqlite3_blo
1f480 62 20 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20  b **ppBlob   /* 
1f481 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73  Handle for acces
1f482 73 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65  sing the blob re
1f483 74 75 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29  turned here */.)
1f484 7b 0a 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74  {.  int nAttempt
1f485 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c   = 0;.  int iCol
1f486 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1f487 2f 2a 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c  /* Index of zCol
1f488 75 6d 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72  umn in row-recor
1f489 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20  d */..  /* This 
1f48a 56 44 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65  VDBE program see
1f48b 6b 73 20 61 20 62 74 72 65 65 20 63 75 72 73 6f  ks a btree curso
1f48c 72 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66  r to the identif
1f48d 69 65 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62  ied .  ** db/tab
1f48e 6c 65 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68  le/row entry. Th
1f48f 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69  e reason for usi
1f490 6e 67 20 61 20 76 64 62 65 20 70 72 6f 67 72 61  ng a vdbe progra
1f491 6d 20 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f  m instead.  ** o
1f492 66 20 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74  f writing code t
1f493 6f 20 75 73 65 20 74 68 65 20 62 2d 74 72 65 65  o use the b-tree
1f494 20 6c 61 79 65 72 20 64 69 72 65 63 74 6c 79 20   layer directly 
1f495 69 73 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a  is that the.  **
1f496 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69   vdbe program wi
1f497 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67  ll take advantag
1f498 65 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73  e of the various
1f499 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20   transaction,.  
1f49a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65  ** locking and e
1f49b 72 72 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e  rror handling in
1f49c 66 72 61 73 74 72 75 63 74 75 72 65 20 62 75 69  frastructure bui
1f49d 6c 74 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65  lt into the vdbe
1f49e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65  ..  **.  ** Afte
1f49f 72 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75  r seeking the cu
1f4a0 72 73 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65  rsor, the vdbe e
1f4a1 78 65 63 75 74 65 73 20 61 6e 20 4f 50 5f 52 65  xecutes an OP_Re
1f4a2 73 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f  sultRow..  ** Co
1f4a3 64 65 20 65 78 74 65 72 6e 61 6c 20 74 6f 20 74  de external to t
1f4a4 68 65 20 56 64 62 65 20 74 68 65 6e 20 22 62 6f  he Vdbe then "bo
1f4a5 72 72 6f 77 73 22 20 74 68 65 20 62 2d 74 72 65  rrows" the b-tre
1f4a6 65 20 63 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a  e cursor and.  *
1f4a7 2a 20 75 73 65 73 20 69 74 20 74 6f 20 69 6d 70  * uses it to imp
1f4a8 6c 65 6d 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f  lement the blob_
1f4a9 72 65 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69  read(), blob_wri
1f4aa 74 65 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62  te() and .  ** b
1f4ab 6c 6f 62 5f 62 79 74 65 73 28 29 20 66 75 6e 63  lob_bytes() func
1f4ac 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
1f4ad 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f   The sqlite3_blo
1f4ae 62 5f 63 6c 6f 73 65 28 29 20 66 75 6e 63 74 69  b_close() functi
1f4af 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65  on finalizes the
1f4b0 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20   vdbe program,. 
1f4b1 20 2a 2a 20 77 68 69 63 68 20 63 6c 6f 73 65 73   ** which closes
1f4b2 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 73   the b-tree curs
1f4b3 6f 72 20 61 6e 64 20 28 70 6f 73 73 69 62 6c 79  or and (possibly
1f4b4 29 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20  ) commits the . 
1f4b5 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e   ** transaction.
1f4b6 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63  .  */.  static c
1f4b7 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
1f4b8 6f 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20  openBlob[] = {. 
1f4b9 20 20 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69     {OP_Transacti
1f4ba 6f 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  on, 0, 0, 0},   
1f4bb 20 20 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20    /* 0: Start a 
1f4bc 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  transaction */. 
1f4bd 20 20 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f     {OP_VerifyCoo
1f4be 6b 69 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  kie, 0, 0, 0},  
1f4bf 20 20 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68    /* 1: Check th
1f4c0 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  e schema cookie 
1f4c1 2a 2f 0a 20 20 20 20 7b 4f 50 5f 54 61 62 6c 65  */.    {OP_Table
1f4c2 4c 6f 63 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20  Lock, 0, 0, 0}, 
1f4c3 20 20 20 20 20 20 2f 2a 20 32 3a 20 41 63 71 75        /* 2: Acqu
1f4c4 69 72 65 20 61 20 72 65 61 64 20 6f 72 20 77 72  ire a read or wr
1f4c5 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20  ite lock */..   
1f4c6 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 66   /* One of the f
1f4c7 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73  ollowing two ins
1f4c8 74 72 75 63 74 69 6f 6e 73 20 69 73 20 72 65 70  tructions is rep
1f4c9 6c 61 63 65 64 20 62 79 20 61 6e 20 4f 50 5f 4e  laced by an OP_N
1f4ca 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  oop. */.    {OP_
1f4cb 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20  OpenRead, 0, 0, 
1f4cc 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 33 3a  0},        /* 3:
1f4cd 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66   Open cursor 0 f
1f4ce 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20  or reading */.  
1f4cf 20 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c    {OP_OpenWrite,
1f4d0 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20   0, 0, 0},      
1f4d1 20 2f 2a 20 34 3a 20 4f 70 65 6e 20 63 75 72 73   /* 4: Open curs
1f4d2 6f 72 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72  or 0 for read/wr
1f4d3 69 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f  ite */..    {OP_
1f4d4 56 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20  Variable, 1, 1, 
1f4d5 31 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 35 3a  1},        /* 5:
1f4d6 20 50 75 73 68 20 74 68 65 20 72 6f 77 69 64 20   Push the rowid 
1f4d7 74 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a  to the stack */.
1f4d8 20 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74      {OP_NotExist
1f4d9 73 2c 20 30 2c 20 39 2c 20 31 7d 2c 20 20 20 20  s, 0, 9, 1},    
1f4da 20 20 20 2f 2a 20 36 3a 20 53 65 65 6b 20 74 68     /* 6: Seek th
1f4db 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20  e cursor */.    
1f4dc 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30  {OP_Column, 0, 0
1f4dd 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f  , 1},          /
1f4de 2a 20 37 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  * 7  */.    {OP_
1f4df 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c  ResultRow, 1, 0,
1f4e0 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 38 20   0},       /* 8 
1f4e1 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73   */.    {OP_Clos
1f4e2 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20  e, 0, 0, 0},    
1f4e3 20 20 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a         /* 9  */.
1f4e4 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c      {OP_Halt, 0,
1f4e5 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20   0, 0},         
1f4e6 20 20 20 2f 2a 20 31 30 20 2a 2f 0a 20 20 7d 3b     /* 10 */.  };
1f4e7 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b  ..  Vdbe *v = 0;
1f4e8 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1f4e9 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a  TE_OK;.  char *z
1f4ea 45 72 72 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65  Err = 0;.  Table
1f4eb 20 2a 70 54 61 62 3b 0a 20 20 50 61 72 73 65 20   *pTab;.  Parse 
1f4ec 2a 70 50 61 72 73 65 3b 0a 0a 20 20 2a 70 70 42  *pParse;..  *ppB
1f4ed 6c 6f 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  lob = 0;.  sqlit
1f4ee 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
1f4ef 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 50 61  b->mutex);.  pPa
1f4f0 72 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61  rse = sqlite3Sta
1f4f1 63 6b 41 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  ckAllocRaw(db, s
1f4f2 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b  izeof(*pParse));
1f4f3 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30  .  if( pParse==0
1f4f4 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
1f4f5 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
1f4f6 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75  oto blob_open_ou
1f4f7 74 3b 0a 20 20 7d 0a 20 20 64 6f 20 7b 0a 20 20  t;.  }.  do {.  
1f4f8 20 20 6d 65 6d 73 65 74 28 70 50 61 72 73 65 2c    memset(pParse,
1f4f9 20 30 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65   0, sizeof(Parse
1f4fa 29 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  ));.    pParse->
1f4fb 64 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 69 66  db = db;..    if
1f4fc 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1f4fd 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73  n(db) ){.      s
1f4fe 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f4ff 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73 71   zErr);.      sq
1f500 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64  lite3StackFree(d
1f501 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20  b, pParse);.    
1f502 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1f503 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
1f504 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
1f505 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
1f506 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
1f507 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62  BtreeEnterAll(db
1f508 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71  );.    pTab = sq
1f509 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
1f50a 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 54 61 62  (pParse, 0, zTab
1f50b 6c 65 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66  le, zDb);.    if
1f50c 28 20 70 54 61 62 20 26 26 20 49 73 56 69 72 74  ( pTab && IsVirt
1f50d 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
1f50e 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20     pTab = 0;.   
1f50f 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1f510 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
1f511 6f 74 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20  ot open virtual 
1f512 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62  table: %s", zTab
1f513 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  le);.    }.#ifnd
1f514 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1f515 49 45 57 0a 20 20 20 20 69 66 28 20 70 54 61 62  IEW.    if( pTab
1f516 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   && pTab->pSelec
1f517 74 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20  t ){.      pTab 
1f518 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
1f519 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1f51a 65 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20  e, "cannot open 
1f51b 76 69 65 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c  view: %s", zTabl
1f51c 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  e);.    }.#endif
1f51d 0a 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29  .    if( !pTab )
1f51e 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72  {.      if( pPar
1f51f 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20  se->zErrMsg ){. 
1f520 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
1f521 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a  Free(db, zErr);.
1f522 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 70          zErr = p
1f523 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a  Parse->zErrMsg;.
1f524 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
1f525 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
1f526 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
1f527 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1f528 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
1f529 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
1f52a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1f52b 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
1f52c 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f        goto blob_
1f52d 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  open_out;.    }.
1f52e 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72  .    /* Now sear
1f52f 63 68 20 70 54 61 62 20 66 6f 72 20 74 68 65 20  ch pTab for the 
1f530 65 78 61 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f  exact column. */
1f531 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b  .    for(iCol=0;
1f532 20 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43   iCol < pTab->nC
1f533 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20  ol; iCol++) {.  
1f534 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1f535 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f  trICmp(pTab->aCo
1f536 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a  l[iCol].zName, z
1f537 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20  Column)==0 ){.  
1f538 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1f539 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1f53a 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e  f( iCol==pTab->n
1f53b 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Col ){.      sql
1f53c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
1f53d 45 72 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72  Err);.      zErr
1f53e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
1f53f 66 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 63  f(db, "no such c
1f540 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20  olumn: \"%s\"", 
1f541 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20  zColumn);.      
1f542 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1f543 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  R;.      (void)s
1f544 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1f545 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  db);.      sqlit
1f546 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28  e3BtreeLeaveAll(
1f547 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  db);.      goto 
1f548 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  blob_open_out;. 
1f549 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
1f54a 74 68 65 20 76 61 6c 75 65 20 69 73 20 62 65 69  the value is bei
1f54b 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72  ng opened for wr
1f54c 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 68 61  iting, check tha
1f54d 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c  t the.    ** col
1f54e 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 65 78  umn is not index
1f54f 65 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20  ed, and that it 
1f550 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61  is not part of a
1f551 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 0a 20   foreign key. . 
1f552 20 20 20 2a 2a 20 49 74 20 69 73 20 61 67 61 69     ** It is agai
1f553 6e 73 74 20 74 68 65 20 72 75 6c 65 73 20 74 6f  nst the rules to
1f554 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74   open a column t
1f555 6f 20 77 68 69 63 68 20 65 69 74 68 65 72 20 6f  o which either o
1f556 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 64  f these.    ** d
1f557 65 73 63 72 69 70 74 69 6f 6e 73 20 61 70 70 6c  escriptions appl
1f558 69 65 73 20 66 6f 72 20 77 72 69 74 69 6e 67 2e  ies for writing.
1f559 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61    */.    if( fla
1f55a 67 73 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73  gs ){.      cons
1f55b 74 20 63 68 61 72 20 2a 7a 46 61 75 6c 74 20 3d  t char *zFault =
1f55c 20 30 3b 0a 20 20 20 20 20 20 49 6e 64 65 78 20   0;.      Index 
1f55d 2a 70 49 64 78 3b 0a 23 69 66 6e 64 65 66 20 53  *pIdx;.#ifndef S
1f55e 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
1f55f 47 4e 5f 4b 45 59 0a 20 20 20 20 20 20 69 66 28  GN_KEY.      if(
1f560 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54   db->flags&SQLIT
1f561 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b  E_ForeignKeys ){
1f562 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63  .        /* Chec
1f563 6b 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d  k that the colum
1f564 6e 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  n is not part of
1f565 20 61 6e 20 46 4b 20 63 68 69 6c 64 20 6b 65 79   an FK child key
1f566 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 49 74 0a   definition. It.
1f567 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f          ** is no
1f568 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 63  t necessary to c
1f569 68 65 63 6b 20 69 66 20 69 74 20 69 73 20 70 61  heck if it is pa
1f56a 72 74 20 6f 66 20 61 20 70 61 72 65 6e 74 20 6b  rt of a parent k
1f56b 65 79 2c 20 61 73 20 70 61 72 65 6e 74 0a 20 20  ey, as parent.  
1f56c 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6c        ** key col
1f56d 75 6d 6e 73 20 6d 75 73 74 20 62 65 20 69 6e 64  umns must be ind
1f56e 65 78 65 64 2e 20 54 68 65 20 63 68 65 63 6b 20  exed. The check 
1f56f 62 65 6c 6f 77 20 77 69 6c 6c 20 70 69 63 6b 20  below will pick 
1f570 75 70 20 74 68 69 73 20 0a 20 20 20 20 20 20 20  up this .       
1f571 20 2a 2a 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20   ** case.  */.  
1f572 20 20 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 65        FKey *pFKe
1f573 79 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70  y;.        for(p
1f574 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b 65 79  FKey=pTab->pFKey
1f575 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70  ; pFKey; pFKey=p
1f576 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29  FKey->pNextFrom)
1f577 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
1f578 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  j;.          for
1f579 28 6a 3d 30 3b 20 6a 3c 70 46 4b 65 79 2d 3e 6e  (j=0; j<pFKey->n
1f57a 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Col; j++){.     
1f57b 20 20 20 20 20 20 20 69 66 28 20 70 46 4b 65 79         if( pFKey
1f57c 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 3d  ->aCol[j].iFrom=
1f57d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20  =iCol ){.       
1f57e 20 20 20 20 20 20 20 7a 46 61 75 6c 74 20 3d 20         zFault = 
1f57f 22 66 6f 72 65 69 67 6e 20 6b 65 79 22 3b 0a 20  "foreign key";. 
1f580 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1f581 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1f582 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69   }.      }.#endi
1f583 66 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78  f.      for(pIdx
1f584 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
1f585 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
1f586 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
1f587 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66  int j;.        f
1f588 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e  or(j=0; j<pIdx->
1f589 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20  nColumn; j++){. 
1f58a 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64           if( pId
1f58b 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d  x->aiColumn[j]==
1f58c 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  iCol ){.        
1f58d 20 20 20 20 7a 46 61 75 6c 74 20 3d 20 22 69 6e      zFault = "in
1f58e 64 65 78 65 64 22 3b 0a 20 20 20 20 20 20 20 20  dexed";.        
1f58f 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1f590 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1f591 7a 46 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  zFault ){.      
1f592 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1f593 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20  db, zErr);.     
1f594 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65     zErr = sqlite
1f595 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 63 61  3MPrintf(db, "ca
1f596 6e 6e 6f 74 20 6f 70 65 6e 20 25 73 20 63 6f 6c  nnot open %s col
1f597 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 22  umn for writing"
1f598 2c 20 7a 46 61 75 6c 74 29 3b 0a 20 20 20 20 20  , zFault);.     
1f599 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1f59a 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 28 76  RROR;.        (v
1f59b 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
1f59c 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20  yOff(db);.      
1f59d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1f59e 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  aveAll(db);.    
1f59f 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70      goto blob_op
1f5a0 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  en_out;.      }.
1f5a1 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73      }..    v = s
1f5a2 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65  qlite3VdbeCreate
1f5a3 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20  (db);.    if( v 
1f5a4 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  ){.      int iDb
1f5a5 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
1f5a6 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
1f5a7 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20  ->pSchema);.    
1f5a8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f5a9 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66  OpList(v, sizeof
1f5aa 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f  (openBlob)/sizeo
1f5ab 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f  f(VdbeOpList), o
1f5ac 70 65 6e 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20  penBlob);.      
1f5ad 66 6c 61 67 73 20 3d 20 21 21 66 6c 61 67 73 3b  flags = !!flags;
1f5ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5af 20 2f 2a 20 66 6c 61 67 73 20 3d 20 28 66 6c 61   /* flags = (fla
1f5b0 67 73 20 3f 20 31 20 3a 20 30 29 3b 20 2a 2f 0a  gs ? 1 : 0); */.
1f5b1 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67  .      /* Config
1f5b2 75 72 65 20 74 68 65 20 4f 50 5f 54 72 61 6e 73  ure the OP_Trans
1f5b3 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  action */.      
1f5b4 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f5b5 65 50 31 28 76 2c 20 30 2c 20 69 44 62 29 3b 0a  eP1(v, 0, iDb);.
1f5b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f5b7 65 43 68 61 6e 67 65 50 32 28 76 2c 20 30 2c 20  eChangeP2(v, 0, 
1f5b8 66 6c 61 67 73 29 3b 0a 0a 20 20 20 20 20 20 2f  flags);..      /
1f5b9 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20  * Configure the 
1f5ba 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20  OP_VerifyCookie 
1f5bb 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
1f5bc 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
1f5bd 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  1, iDb);.      s
1f5be 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1f5bf 50 32 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70  P2(v, 1, pTab->p
1f5c0 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
1f5c1 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f  ookie);..      /
1f5c2 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75  * Make sure a mu
1f5c3 74 65 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74  tex is held on t
1f5c4 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61  he table to be a
1f5c5 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20  ccessed */.     
1f5c6 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
1f5c7 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a  Btree(v, iDb); .
1f5c8 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67  .      /* Config
1f5c9 75 72 65 20 74 68 65 20 4f 50 5f 54 61 62 6c 65  ure the OP_Table
1f5ca 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e  Lock instruction
1f5cb 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
1f5cc 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
1f5cd 20 32 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20   2, iDb);.      
1f5ce 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f5cf 65 50 32 28 76 2c 20 32 2c 20 70 54 61 62 2d 3e  eP2(v, 2, pTab->
1f5d0 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c  tnum);.      sql
1f5d1 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
1f5d2 28 76 2c 20 32 2c 20 66 6c 61 67 73 29 3b 0a 20  (v, 2, flags);. 
1f5d3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f5d4 43 68 61 6e 67 65 50 34 28 76 2c 20 32 2c 20 70  ChangeP4(v, 2, p
1f5d5 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54  Tab->zName, P4_T
1f5d6 52 41 4e 53 49 45 4e 54 29 3b 0a 0a 20 20 20 20  RANSIENT);..    
1f5d7 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74 68    /* Remove eith
1f5d8 65 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57 72  er the OP_OpenWr
1f5d9 69 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64 2e  ite or OpenRead.
1f5da 20 53 65 74 20 74 68 65 20 50 32 20 0a 20 20 20   Set the P2 .   
1f5db 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20     ** parameter 
1f5dc 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20  of the other to 
1f5dd 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 20 2a 2f 0a  pTab->tnum.  */.
1f5de 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f5df 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c  eChangeToNoop(v,
1f5e0 20 34 20 2d 20 66 6c 61 67 73 2c 20 31 29 3b 0a   4 - flags, 1);.
1f5e1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f5e2 65 43 68 61 6e 67 65 50 32 28 76 2c 20 33 20 2b  eChangeP2(v, 3 +
1f5e3 20 66 6c 61 67 73 2c 20 70 54 61 62 2d 3e 74 6e   flags, pTab->tn
1f5e4 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  um);.      sqlit
1f5e5 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76  e3VdbeChangeP3(v
1f5e6 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 69 44 62  , 3 + flags, iDb
1f5e7 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e  );..      /* Con
1f5e8 66 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65  figure the numbe
1f5e9 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f  r of columns. Co
1f5ea 6e 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73  nfigure the curs
1f5eb 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74  or to.      ** t
1f5ec 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61  hink that the ta
1f5ed 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65  ble has one more
1f5ee 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20   column than it 
1f5ef 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20  really.      ** 
1f5f0 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75  does. An OP_Colu
1f5f1 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74  mn to retrieve t
1f5f2 68 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f  his imaginary co
1f5f3 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20  lumn will.      
1f5f4 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  ** always return
1f5f5 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68   an SQL NULL. Th
1f5f6 69 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63  is is useful bec
1f5f7 61 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20  ause it means.  
1f5f8 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e      ** we can in
1f5f9 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74  voke OP_Column t
1f5fa 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64  o fill in the vd
1f5fb 62 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20  be cursors type 
1f5fc 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66  .      ** and of
1f5fd 66 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f  fset cache witho
1f5fe 75 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49  ut causing any I
1f5ff 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  O..      */.    
1f600 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1f601 6e 67 65 50 34 28 76 2c 20 33 2b 66 6c 61 67 73  ngeP4(v, 3+flags
1f602 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  , SQLITE_INT_TO_
1f603 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31  PTR(pTab->nCol+1
1f604 29 2c 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20  ),P4_INT32);.   
1f605 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1f606 61 6e 67 65 50 32 28 76 2c 20 37 2c 20 70 54 61  angeP2(v, 7, pTa
1f607 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20  b->nCol);.      
1f608 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
1f609 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
1f60a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1f60b 52 65 61 64 79 28 76 2c 20 31 2c 20 31 2c 20 31  Ready(v, 1, 1, 1
1f60c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
1f60d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20    }.    }.   .  
1f60e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1f60f 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  aveAll(db);.    
1f610 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65  rc = sqlite3Safe
1f611 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69  tyOff(db);.    i
1f612 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c  f( NEVER(rc!=SQL
1f613 49 54 45 5f 4f 4b 29 20 7c 7c 20 64 62 2d 3e 6d  ITE_OK) || db->m
1f614 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1f615 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1f616 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  pen_out;.    }..
1f617 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
1f618 5f 69 6e 74 36 34 28 28 73 71 6c 69 74 65 33 5f  _int64((sqlite3_
1f619 73 74 6d 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f  stmt *)v, 1, iRo
1f61a 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  w);.    rc = sql
1f61b 69 74 65 33 5f 73 74 65 70 28 28 73 71 6c 69 74  ite3_step((sqlit
1f61c 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20  e3_stmt *)v);.  
1f61d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1f61e 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 6e 41  _ROW ){.      nA
1f61f 74 74 65 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20  ttempt++;.      
1f620 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e  rc = sqlite3_fin
1f621 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73  alize((sqlite3_s
1f622 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 20 20  tmt *)v);.      
1f623 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1f624 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a  , zErr);.      z
1f625 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Err = sqlite3MPr
1f626 69 6e 74 66 28 64 62 2c 20 73 71 6c 69 74 65 33  intf(db, sqlite3
1f627 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20  _errmsg(db));.  
1f628 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 7d      v = 0;.    }
1f629 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 41 74 74  .  } while( nAtt
1f62a 65 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d 53 51  empt<5 && rc==SQ
1f62b 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 3b 0a 0a  LITE_SCHEMA );..
1f62c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1f62d 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54  _ROW ){.    /* T
1f62e 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 20 68 61  he row-record ha
1f62f 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 73 75  s been opened su
1f630 63 63 65 73 73 66 75 6c 6c 79 2e 20 43 68 65 63  ccessfully. Chec
1f631 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a  k that the.    *
1f632 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75 65 73  * column in ques
1f633 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 65  tion contains te
1f634 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20 49 66  xt or a blob. If
1f635 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20   it contains.   
1f636 20 2a 2a 20 74 65 78 74 2c 20 69 74 20 69 73 20   ** text, it is 
1f637 75 70 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  up to the caller
1f638 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e 63 6f   to get the enco
1f639 64 69 6e 67 20 72 69 67 68 74 2e 0a 20 20 20 20  ding right..    
1f63a 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f 62 20  */.    Incrblob 
1f63b 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33 32 20  *pBlob;.    u32 
1f63c 74 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b  type = v->apCsr[
1f63d 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c 5d 3b  0]->aType[iCol];
1f63e 0a 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c 31  ..    if( type<1
1f63f 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  2 ){.      sqlit
1f640 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
1f641 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d  r);.      zErr =
1f642 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1f643 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e  db, "cannot open
1f644 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25   value of type %
1f645 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79  s",.          ty
1f646 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79  pe==0?"null": ty
1f647 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69  pe==7?"real": "i
1f648 6e 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b  nteger".      );
1f649 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1f64a 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
1f64b 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f  goto blob_open_o
1f64c 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  ut;.    }.    pB
1f64d 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20  lob = (Incrblob 
1f64e 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
1f64f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
1f650 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20  (Incrblob));.   
1f651 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1f652 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73  ailed ){.      s
1f653 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f654 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67   pBlob);.      g
1f655 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75  oto blob_open_ou
1f656 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c  t;.    }.    pBl
1f657 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67  ob->flags = flag
1f658 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43  s;.    pBlob->pC
1f659 73 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30  sr =  v->apCsr[0
1f65a 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20  ]->pCursor;.    
1f65b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1f65c 72 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70  rCursor(pBlob->p
1f65d 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Csr);.    sqlite
1f65e 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66  3BtreeCacheOverf
1f65f 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29  low(pBlob->pCsr)
1f660 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1f661 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 42  eeLeaveCursor(pB
1f662 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  lob->pCsr);.    
1f663 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28  pBlob->pStmt = (
1f664 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76  sqlite3_stmt *)v
1f665 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66  ;.    pBlob->iOf
1f666 66 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b  fset = v->apCsr[
1f667 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c  0]->aOffset[iCol
1f668 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42  ];.    pBlob->nB
1f669 79 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  yte = sqlite3Vdb
1f66a 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74  eSerialTypeLen(t
1f66b 79 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d  ype);.    pBlob-
1f66c 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70  >db = db;.    *p
1f66d 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 33  pBlob = (sqlite3
1f66e 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20  _blob *)pBlob;. 
1f66f 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
1f670 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72  K;.  }else if( r
1f671 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1f672 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f673 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  e(db, zErr);.   
1f674 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d   zErr = sqlite3M
1f675 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73  Printf(db, "no s
1f676 75 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22  uch rowid: %lld"
1f677 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20  , iRow);.    rc 
1f678 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1f679 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f    }..blob_open_o
1f67a 75 74 3a 0a 20 20 69 66 28 20 76 20 26 26 20 28  ut:.  if( v && (
1f67b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
1f67c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1f67d 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  d) ){.    sqlite
1f67e 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29  3VdbeFinalize(v)
1f67f 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
1f680 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 7a 45 72  rror(db, rc, zEr
1f681 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  r);.  sqlite3DbF
1f682 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  ree(db, zErr);. 
1f683 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
1f684 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20  e(db, pParse);. 
1f685 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
1f686 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
1f687 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1f688 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
1f689 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1f68a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c  /*.** Close a bl
1f68b 6f 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77  ob handle that w
1f68c 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72  as previously cr
1f68d 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73  eated using.** s
1f68e 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
1f68f 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
1f690 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
1f691 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33  ob_close(sqlite3
1f692 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20  _blob *pBlob){. 
1f693 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28   Incrblob *p = (
1f694 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62  Incrblob *)pBlob
1f695 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  ;.  int rc;.  sq
1f696 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66  lite3 *db;..  if
1f697 28 20 70 20 29 7b 0a 20 20 20 20 64 62 20 3d 20  ( p ){.    db = 
1f698 70 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74  p->db;.    sqlit
1f699 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
1f69a 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72  b->mutex);.    r
1f69b 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  c = sqlite3_fina
1f69c 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a  lize(p->pStmt);.
1f69d 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f69e 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71  e(db, p);.    sq
1f69f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1f6a0 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
1f6a1 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1f6a2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1f6a3 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1f6a4 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72  *.** Perform a r
1f6a5 65 61 64 20 6f 72 20 77 72 69 74 65 20 6f 70 65  ead or write ope
1f6a6 72 61 74 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62  ration on a blob
1f6a7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
1f6a8 6c 6f 62 52 65 61 64 57 72 69 74 65 28 0a 20 20  lobReadWrite(.  
1f6a9 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42  sqlite3_blob *pB
1f6aa 6c 6f 62 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c  lob, .  void *z,
1f6ab 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e   .  int n, .  in
1f6ac 74 20 69 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e  t iOffset, .  in
1f6ad 74 20 28 2a 78 43 61 6c 6c 29 28 42 74 43 75 72  t (*xCall)(BtCur
1f6ae 73 6f 72 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20  sor*, u32, u32, 
1f6af 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20  void*).){.  int 
1f6b0 72 63 3b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a  rc;.  Incrblob *
1f6b1 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29  p = (Incrblob *)
1f6b2 70 42 6c 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76  pBlob;.  Vdbe *v
1f6b3 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
1f6b4 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ..  if( p==0 ) r
1f6b5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1f6b6 55 53 45 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64  USE;.  db = p->d
1f6b7 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  b;.  sqlite3_mut
1f6b8 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
1f6b9 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 64 62 65  ex);.  v = (Vdbe
1f6ba 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 69  *)p->pStmt;..  i
1f6bb 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65  f( n<0 || iOffse
1f6bc 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b  t<0 || (iOffset+
1f6bd 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20  n)>p->nByte ){. 
1f6be 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73     /* Request is
1f6bf 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52   out of range. R
1f6c0 65 74 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e  eturn a transien
1f6c1 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 20  t error. */.    
1f6c2 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1f6c3 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  R;.    sqlite3Er
1f6c4 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45  ror(db, SQLITE_E
1f6c5 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 65 6c  RROR, 0);.  } el
1f6c6 73 65 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20  se if( v==0 ){. 
1f6c7 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69     /* If there i
1f6c8 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68  s no statement h
1f6c9 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20  andle, then the 
1f6ca 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a  blob-handle has.
1f6cb 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62      ** already b
1f6cc 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 64 2e  een invalidated.
1f6cd 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41   Return SQLITE_A
1f6ce 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63 61 73  BORT in this cas
1f6cf 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  e..    */.    rc
1f6d0 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b   = SQLITE_ABORT;
1f6d1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1f6d2 20 43 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72   Call either Btr
1f6d3 65 65 44 61 74 61 28 29 20 6f 72 20 42 74 72 65  eeData() or Btre
1f6d4 65 50 75 74 44 61 74 61 28 29 2e 20 49 66 20 53  ePutData(). If S
1f6d5 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20  QLITE_ABORT is. 
1f6d6 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20     ** returned, 
1f6d7 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61  clean-up the sta
1f6d8 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20  tement handle.. 
1f6d9 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
1f6da 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b  ( db == v->db );
1f6db 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1f6dc 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e  eEnterCursor(p->
1f6dd 70 43 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20  pCsr);.    rc = 
1f6de 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69  xCall(p->pCsr, i
1f6df 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65  Offset+p->iOffse
1f6e0 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71  t, n, z);.    sq
1f6e1 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43  lite3BtreeLeaveC
1f6e2 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a  ursor(p->pCsr);.
1f6e3 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1f6e4 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20  TE_ABORT ){.    
1f6e5 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e    sqlite3VdbeFin
1f6e6 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20  alize(v);.      
1f6e7 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20  p->pStmt = 0;.  
1f6e8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
1f6e9 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b  b->errCode = rc;
1f6ea 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72  .      v->rc = r
1f6eb 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  c;.    }.  }.  r
1f6ec 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
1f6ed 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
1f6ee 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1f6ef 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
1f6f0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1f6f1 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72  .** Read data fr
1f6f2 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  om a blob handle
1f6f3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1f6f4 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
1f6f5 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c  _read(sqlite3_bl
1f6f6 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20  ob *pBlob, void 
1f6f7 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69  *z, int n, int i
1f6f8 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72  Offset){.  retur
1f6f9 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28  n blobReadWrite(
1f6fa 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66  pBlob, z, n, iOf
1f6fb 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72  fset, sqlite3Btr
1f6fc 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  eeData);.}../*.*
1f6fd 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20  * Write data to 
1f6fe 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a  a blob handle..*
1f6ff 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1f700 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72   sqlite3_blob_wr
1f701 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ite(sqlite3_blob
1f702 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76   *pBlob, const v
1f703 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69  oid *z, int n, i
1f704 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72  nt iOffset){.  r
1f705 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72  eturn blobReadWr
1f706 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64  ite(pBlob, (void
1f707 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74   *)z, n, iOffset
1f708 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75  , sqlite3BtreePu
1f709 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  tData);.}../*.**
1f70a 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61   Query a blob ha
1f70b 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a  ndle for the siz
1f70c 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a  e of the data..*
1f70d 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f  *.** The Incrblo
1f70e 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73  b.nByte field is
1f70f 20 66 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c   fixed for the l
1f710 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49  ifetime of the I
1f711 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f  ncrblob.** so no
1f712 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72   mutex is requir
1f713 65 64 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a  ed for access..*
1f714 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1f715 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79   sqlite3_blob_by
1f716 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  tes(sqlite3_blob
1f717 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72   *pBlob){.  Incr
1f718 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62  blob *p = (Incrb
1f719 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72  lob *)pBlob;.  r
1f71a 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 42 79  eturn p ? p->nBy
1f71b 74 65 20 3a 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69  te : 0;.}..#endi
1f71c 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c  f /* #ifndef SQL
1f71d 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
1f71e 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  B */../*********
1f71f 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
1f720 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  eblob.c ********
1f721 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f722 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f723 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1f724 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1f725 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a   journal.c *****
1f726 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f727 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f728 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
1f729 20 41 75 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a   August 22.**.**
1f72a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1f72b 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1f72c 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1f72d 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1f72e 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1f72f 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1f730 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1f731 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1f732 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1f733 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1f734 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1f735 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1f736 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1f737 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1f738 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1f739 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1f73a 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1f73b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f73c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f73d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f73e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f73f 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1f740 40 28 23 29 20 24 49 64 3a 20 6a 6f 75 72 6e 61  @(#) $Id: journa
1f741 6c 2e 63 2c 76 20 31 2e 39 20 32 30 30 39 2f 30  l.c,v 1.9 2009/0
1f742 31 2f 32 30 20 31 37 3a 30 36 3a 32 37 20 64 61  1/20 17:06:27 da
1f743 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
1f744 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
1f745 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
1f746 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  WRITE../*.** Thi
1f747 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
1f748 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69 6e 64  s a special kind
1f749 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   of sqlite3_file
1f74a 20 6f 62 6a 65 63 74 20 75 73 65 64 0a 2a 2a 20   object used.** 
1f74b 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63 72 65  by SQLite to cre
1f74c 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ate journal file
1f74d 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d  s if the atomic-
1f74e 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  write optimizati
1f74f 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64  on.** is enabled
1f750 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 73 74  ..**.** The dist
1f751 69 6e 63 74 69 76 65 20 63 68 61 72 61 63 74 65  inctive characte
1f752 72 69 73 74 69 63 20 6f 66 20 74 68 69 73 20 73  ristic of this s
1f753 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 73 20 74  qlite3_file is t
1f754 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 74 75 61  hat the.** actua
1f755 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c 65 20 69  l on disk file i
1f756 73 20 63 72 65 61 74 65 64 20 6c 61 7a 69 6c 79  s created lazily
1f757 2e 20 57 68 65 6e 20 74 68 65 20 66 69 6c 65 20  . When the file 
1f758 69 73 20 63 72 65 61 74 65 64 2c 0a 2a 2a 20 74  is created,.** t
1f759 68 65 20 63 61 6c 6c 65 72 20 73 70 65 63 69 66  he caller specif
1f75a 69 65 73 20 61 20 62 75 66 66 65 72 20 73 69 7a  ies a buffer siz
1f75b 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  e for an in-memo
1f75c 72 79 20 62 75 66 66 65 72 20 74 6f 0a 2a 2a 20  ry buffer to.** 
1f75d 62 65 20 75 73 65 64 20 74 6f 20 73 65 72 76 69  be used to servi
1f75e 63 65 20 72 65 61 64 28 29 20 61 6e 64 20 77 72  ce read() and wr
1f75f 69 74 65 28 29 20 72 65 71 75 65 73 74 73 2e 20  ite() requests. 
1f760 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 0a  The actual file.
1f761 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f  ** on disk is no
1f762 74 20 63 72 65 61 74 65 64 20 6f 72 20 70 6f 70  t created or pop
1f763 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 65 69 74  ulated until eit
1f764 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20  her:.**.**   1) 
1f765 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65  The in-memory re
1f766 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 67 72 6f  presentation gro
1f767 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72  ws too large for
1f768 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a   the allocated .
1f769 2a 2a 20 20 20 20 20 20 62 75 66 66 65 72 2c 20  **      buffer, 
1f76a 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 73  or.**   2) The s
1f76b 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65  qlite3JournalCre
1f76c 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ate() function i
1f76d 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a 0a 0a  s called..*/....
1f76e 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e 61 6c 46  /*.** A JournalF
1f76f 69 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 61 20  ile object is a 
1f770 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69  subclass of sqli
1f771 74 65 33 5f 66 69 6c 65 20 75 73 65 64 20 62 79  te3_file used by
1f772 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65 6e 20 66  .** as an open f
1f773 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 6a  ile handle for j
1f774 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f  ournal files..*/
1f775 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46  .struct JournalF
1f776 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ile {.  sqlite3_
1f777 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74  io_methods *pMet
1f778 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f 4f 20 6d  hod;    /* I/O m
1f779 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 72 6e 61  ethods on journa
1f77a 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 69 6e 74  l files */.  int
1f77b 20 6e 42 75 66 3b 20 20 20 20 20 20 20 20 20 20   nBuf;          
1f77c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f77d 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b 5d 20 69  Size of zBuf[] i
1f77e 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61  n bytes */.  cha
1f77f 72 20 2a 7a 42 75 66 3b 20 20 20 20 20 20 20 20  r *zBuf;        
1f780 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f781 53 70 61 63 65 20 74 6f 20 62 75 66 66 65 72 20  Space to buffer 
1f782 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65 73 20 2a  journal writes *
1f783 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65 3b 20 20  /.  int iSize;  
1f784 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f785 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66      /* Amount of
1f786 20 7a 42 75 66 5b 5d 20 63 75 72 72 65 6e 74 6c   zBuf[] currentl
1f787 79 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20  y used */.  int 
1f788 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
1f789 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1f78a 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20  Open flags */.  
1f78b 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
1f78c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
1f78d 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75 6e  /* The "real" un
1f78e 64 65 72 6c 79 69 6e 67 20 56 46 53 20 2a 2f 0a  derlying VFS */.
1f78f 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1f790 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20 20 20  pReal;          
1f791 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20    /* The "real" 
1f792 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20  underlying file 
1f793 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20  descriptor */.  
1f794 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a 6f 75  const char *zJou
1f795 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20  rnal;           
1f796 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a  /* Name of the j
1f797 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 7d  ournal file */.}
1f798 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
1f799 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a 6f 75   JournalFile Jou
1f79a 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a 2a 2a  rnalFile;../*.**
1f79b 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   If it does not 
1f79c 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c 20  already exists, 
1f79d 63 72 65 61 74 65 20 61 6e 64 20 70 6f 70 75 6c  create and popul
1f79e 61 74 65 20 74 68 65 20 6f 6e 2d 64 69 73 6b 20  ate the on-disk 
1f79f 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 4a 6f 75  file .** for Jou
1f7a0 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a 2f 0a 73  rnalFile p..*/.s
1f7a1 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 74 65  tatic int create
1f7a2 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  File(JournalFile
1f7a3 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d   *p){.  int rc =
1f7a4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
1f7a5 28 20 21 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  ( !p->pReal ){. 
1f7a6 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20     sqlite3_file 
1f7a7 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c 69 74 65  *pReal = (sqlite
1f7a8 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31 5d 3b 0a  3_file *)&p[1];.
1f7a9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1f7aa 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66 73 2c 20  OsOpen(p->pVfs, 
1f7ab 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 52 65  p->zJournal, pRe
1f7ac 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c 20 30 29  al, p->flags, 0)
1f7ad 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1f7ae 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1f7af 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70 52 65 61   p->pReal = pRea
1f7b0 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  l;.      if( p->
1f7b1 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20  iSize>0 ){.     
1f7b2 20 20 20 61 73 73 65 72 74 28 70 2d 3e 69 53 69     assert(p->iSi
1f7b3 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0a 20 20  ze<=p->nBuf);.  
1f7b4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1f7b5 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 52 65  e3OsWrite(p->pRe
1f7b6 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70 2d 3e  al, p->zBuf, p->
1f7b7 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20  iSize, 0);.     
1f7b8 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
1f7b9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1f7ba 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c  ** Close the fil
1f7bb 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1f7bc 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74   jrnlClose(sqlit
1f7bd 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a  e3_file *pJfd){.
1f7be 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70    JournalFile *p
1f7bf 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20   = (JournalFile 
1f7c0 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d  *)pJfd;.  if( p-
1f7c1 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73 71  >pReal ){.    sq
1f7c2 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 2d 3e  lite3OsClose(p->
1f7c3 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 73 71  pReal);.  }.  sq
1f7c4 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 42  lite3_free(p->zB
1f7c5 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  uf);.  return SQ
1f7c6 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1f7c7 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d  * Read data from
1f7c8 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74   the file..*/.st
1f7c9 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 52 65 61  atic int jrnlRea
1f7ca 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  d(.  sqlite3_fil
1f7cb 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54  e *pJfd,    /* T
1f7cc 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1f7cd 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65  from which to re
1f7ce 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42  ad */.  void *zB
1f7cf 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  uf,            /
1f7d0 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c 74  * Put the result
1f7d1 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  s here */.  int 
1f7d2 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20  iAmt,           
1f7d3 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1f7d4 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f  bytes to read */
1f7d5 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
1f7d6 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67  iOfst     /* Beg
1f7d7 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68  in reading at th
1f7d8 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a  is offset */.){.
1f7d9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1f7da 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46  E_OK;.  JournalF
1f7db 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61  ile *p = (Journa
1f7dc 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20  lFile *)pJfd;.  
1f7dd 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a  if( p->pReal ){.
1f7de 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1f7df 4f 73 52 65 61 64 28 70 2d 3e 70 52 65 61 6c 2c  OsRead(p->pReal,
1f7e0 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66   zBuf, iAmt, iOf
1f7e1 73 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  st);.  }else if(
1f7e2 20 28 69 41 6d 74 2b 69 4f 66 73 74 29 3e 70 2d   (iAmt+iOfst)>p-
1f7e3 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63  >iSize ){.    rc
1f7e4 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
1f7e5 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 65  SHORT_READ;.  }e
1f7e6 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  lse{.    memcpy(
1f7e7 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b 69  zBuf, &p->zBuf[i
1f7e8 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20 20  Ofst], iAmt);.  
1f7e9 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1f7ea 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61  ../*.** Write da
1f7eb 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a  ta to the file..
1f7ec 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1f7ed 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  nlWrite(.  sqlit
1f7ee 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1f7ef 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
1f7f0 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68   file into which
1f7f1 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63   to write */.  c
1f7f2 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c  onst void *zBuf,
1f7f3 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61        /* Take da
1f7f4 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  ta to be written
1f7f5 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20   from here */.  
1f7f6 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1f7f7 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1f7f8 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69   of bytes to wri
1f7f9 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  te */.  sqlite_i
1f7fa 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f  nt64 iOfst     /
1f7fb 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20  * Begin writing 
1f7fc 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69  at this offset i
1f7fd 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  nto the file */.
1f7fe 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1f7ff 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e  LITE_OK;.  Journ
1f800 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
1f801 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
1f802 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c  .  if( !p->pReal
1f803 20 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74 29   && (iOfst+iAmt)
1f804 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20 20  >p->nBuf ){.    
1f805 72 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65 28  rc = createFile(
1f806 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  p);.  }.  if( rc
1f807 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1f808 20 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20     if( p->pReal 
1f809 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
1f80a 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e  lite3OsWrite(p->
1f80b 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d  pReal, zBuf, iAm
1f80c 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20 7d  t, iOfst);.    }
1f80d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63  else{.      memc
1f80e 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73  py(&p->zBuf[iOfs
1f80f 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29 3b  t], zBuf, iAmt);
1f810 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53  .      if( p->iS
1f811 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74 29  ize<(iOfst+iAmt)
1f812 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
1f813 53 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69 41  Size = (iOfst+iA
1f814 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  mt);.      }.   
1f815 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1f816 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  rc;.}../*.** Tru
1f817 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a  ncate the file..
1f818 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1f819 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  nlTruncate(sqlit
1f81a 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73  e3_file *pJfd, s
1f81b 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65  qlite_int64 size
1f81c 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1f81d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e  LITE_OK;.  Journ
1f81e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
1f81f 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
1f820 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20  .  if( p->pReal 
1f821 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1f822 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 2d  te3OsTruncate(p-
1f823 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a 20  >pReal, size);. 
1f824 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 3c   }else if( size<
1f825 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20  p->iSize ){.    
1f826 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65 3b  p->iSize = size;
1f827 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1f828 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20  ;.}../*.** Sync 
1f829 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1f82a 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e 63  tic int jrnlSync
1f82b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1f82c 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  Jfd, int flags){
1f82d 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f 75  .  int rc;.  Jou
1f82e 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
1f82f 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
1f830 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61  d;.  if( p->pRea
1f831 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
1f832 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e 70  lite3OsSync(p->p
1f833 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20 20  Real, flags);.  
1f834 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1f835 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1f836 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1f837 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73  *.** Query the s
1f838 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
1f839 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  in bytes..*/.sta
1f83a 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c 65  tic int jrnlFile
1f83b 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  Size(sqlite3_fil
1f83c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f  e *pJfd, sqlite_
1f83d 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20  int64 *pSize){. 
1f83e 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1f83f 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69  _OK;.  JournalFi
1f840 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c  le *p = (Journal
1f841 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69  File *)pJfd;.  i
1f842 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  f( p->pReal ){. 
1f843 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1f844 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52 65  sFileSize(p->pRe
1f845 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d 65  al, pSize);.  }e
1f846 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20  lse{.    *pSize 
1f847 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  = (sqlite_int64)
1f848 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a 20   p->iSize;.  }. 
1f849 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1f84a 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65  *.** Table of me
1f84b 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e 61  thods for Journa
1f84c 6c 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69  lFile sqlite3_fi
1f84d 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74  le object..*/.st
1f84e 61 74 69 63 20 73 74 72 75 63 74 20 73 71 6c 69  atic struct sqli
1f84f 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4a  te3_io_methods J
1f850 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64  ournalFileMethod
1f851 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20  s = {.  1,      
1f852 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69         /* iVersi
1f853 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f 73  on */.  jrnlClos
1f854 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65  e,     /* xClose
1f855 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c 20   */.  jrnlRead, 
1f856 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f       /* xRead */
1f857 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20  .  jrnlWrite,   
1f858 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20    /* xWrite */. 
1f859 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20   jrnlTruncate,  
1f85a 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a  /* xTruncate */.
1f85b 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20    jrnlSync,     
1f85c 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6a   /* xSync */.  j
1f85d 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a  rnlFileSize,  /*
1f85e 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20   xFileSize */.  
1f85f 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1f860 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20  * xLock */.  0, 
1f861 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1f862 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20  Unlock */.  0,  
1f863 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
1f864 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
1f865 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1f866 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e       /* xFileCon
1f867 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20  trol */.  0,    
1f868 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63           /* xSec
1f869 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20  torSize */.  0  
1f86a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1f86b 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
1f86c 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20  stics */.};../* 
1f86d 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e  .** Open a journ
1f86e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  al file..*/.SQLI
1f86f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1f870 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65  qlite3JournalOpe
1f871 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  n(.  sqlite3_vfs
1f872 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20   *pVfs,         
1f873 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 73  /* The VFS to us
1f874 65 20 66 6f 72 20 61 63 74 75 61 6c 20 66 69 6c  e for actual fil
1f875 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f 6e 73 74  e I/O */.  const
1f876 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
1f877 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1f878 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1f879 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  e */.  sqlite3_f
1f87a 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 20 20  ile *pJfd,      
1f87b 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65    /* Preallocate
1f87c 64 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 20 68 61  d, blank file ha
1f87d 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  ndle */.  int fl
1f87e 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
1f87f 20 20 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 20       /* Opening 
1f880 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  flags */.  int n
1f881 42 75 66 20 20 20 20 20 20 20 20 20 20 20 20 20  Buf             
1f882 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 62        /* Bytes b
1f883 75 66 66 65 72 65 64 20 62 65 66 6f 72 65 20 6f  uffered before o
1f884 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20  pening the file 
1f885 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46  */.){.  JournalF
1f886 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61  ile *p = (Journa
1f887 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20  lFile *)pJfd;.  
1f888 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 6c  memset(p, 0, sql
1f889 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  ite3JournalSize(
1f88a 70 56 66 73 29 29 3b 0a 20 20 69 66 28 20 6e 42  pVfs));.  if( nB
1f88b 75 66 3e 30 20 29 7b 0a 20 20 20 20 70 2d 3e 7a  uf>0 ){.    p->z
1f88c 42 75 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Buf = sqlite3Mal
1f88d 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 29 3b 0a 20  locZero(nBuf);. 
1f88e 20 20 20 69 66 28 20 21 70 2d 3e 7a 42 75 66 20     if( !p->zBuf 
1f88f 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1f890 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1f891 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
1f892 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f   return sqlite3O
1f893 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d  sOpen(pVfs, zNam
1f894 65 2c 20 70 4a 66 64 2c 20 66 6c 61 67 73 2c 20  e, pJfd, flags, 
1f895 30 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4d 65  0);.  }.  p->pMe
1f896 74 68 6f 64 20 3d 20 26 4a 6f 75 72 6e 61 6c 46  thod = &JournalF
1f897 69 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 20 70 2d  ileMethods;.  p-
1f898 3e 6e 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20 20  >nBuf = nBuf;.  
1f899 70 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73  p->flags = flags
1f89a 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 20  ;.  p->zJournal 
1f89b 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d 3e 70 56  = zName;.  p->pV
1f89c 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 72 65 74  fs = pVfs;.  ret
1f89d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1f89e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61  ../*.** If the a
1f89f 72 67 75 6d 65 6e 74 20 70 20 70 6f 69 6e 74 73  rgument p points
1f8a0 20 74 6f 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c   to a JournalFil
1f8a1 65 20 73 74 72 75 63 74 75 72 65 2c 20 61 6e 64  e structure, and
1f8a2 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a   the underlying.
1f8a3 2a 2a 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20  ** file has not 
1f8a4 79 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64  yet been created
1f8a5 2c 20 63 72 65 61 74 65 20 69 74 20 6e 6f 77 2e  , create it now.
1f8a6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1f8a7 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f  TE int sqlite3Jo
1f8a8 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69  urnalCreate(sqli
1f8a9 74 65 33 5f 66 69 6c 65 20 2a 70 29 7b 0a 20 20  te3_file *p){.  
1f8aa 69 66 28 20 70 2d 3e 70 4d 65 74 68 6f 64 73 21  if( p->pMethods!
1f8ab 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74  =&JournalFileMet
1f8ac 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75  hods ){.    retu
1f8ad 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1f8ae 7d 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74  }.  return creat
1f8af 65 46 69 6c 65 28 28 4a 6f 75 72 6e 61 6c 46 69  eFile((JournalFi
1f8b0 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a  le *)p);.}../* .
1f8b1 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1f8b2 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
1f8b3 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20  quired to store 
1f8b4 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 68  a JournalFile th
1f8b5 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70  at uses vfs.** p
1f8b6 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68  Vfs to create th
1f8b7 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d  e underlying on-
1f8b8 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53  disk files..*/.S
1f8b9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1f8ba 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  t sqlite3Journal
1f8bb 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73  Size(sqlite3_vfs
1f8bc 20 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75 72   *pVfs){.  retur
1f8bd 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c  n (pVfs->szOsFil
1f8be 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61 6c  e+sizeof(Journal
1f8bf 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69 66  File));.}.#endif
1f8c0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1f8c1 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61 6c  * End of journal
1f8c2 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1f8c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8c5 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1f8c6 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d  * Begin file mem
1f8c7 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a  journal.c ******
1f8c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8ca 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74  /./*.** 2008 Oct
1f8cb 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65  ober 7.**.** The
1f8cc 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1f8cd 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1f8ce 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1f8cf 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1f8d0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1f8d1 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1f8d2 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1f8d3 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1f8d4 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1f8d5 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1f8d6 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1f8d7 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1f8d8 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1f8d9 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1f8da 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1f8db 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1f8dc 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
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 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8e1 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
1f8e2 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
1f8e3 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65  ode use to imple
1f8e4 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  ment an in-memor
1f8e5 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  y rollback journ
1f8e6 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65  al..** The in-me
1f8e7 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  mory rollback jo
1f8e8 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74 6f  urnal is used to
1f8e9 20 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61 63   journal transac
1f8ea 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a 6d  tions for.** ":m
1f8eb 65 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73 65  emory:" database
1f8ec 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 6a  s and when the j
1f8ed 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f  ournal_mode=MEMO
1f8ee 52 59 20 70 72 61 67 6d 61 20 69 73 20 75 73 65  RY pragma is use
1f8ef 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  d..**.** @(#) $I
1f8f0 64 3a 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 2c  d: memjournal.c,
1f8f1 76 20 31 2e 31 32 20 32 30 30 39 2f 30 35 2f 30  v 1.12 2009/05/0
1f8f2 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65  4 11:42:30 danie
1f8f3 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
1f8f4 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
1f8f5 72 65 6e 63 65 73 20 74 6f 20 69 6e 74 65 72 6e  rences to intern
1f8f6 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f  al structures */
1f8f7 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1f8f8 4d 65 6d 4a 6f 75 72 6e 61 6c 20 4d 65 6d 4a 6f  MemJournal MemJo
1f8f9 75 72 6e 61 6c 3b 0a 74 79 70 65 64 65 66 20 73  urnal;.typedef s
1f8fa 74 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20  truct FilePoint 
1f8fb 46 69 6c 65 50 6f 69 6e 74 3b 0a 74 79 70 65 64  FilePoint;.typed
1f8fc 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 43 68  ef struct FileCh
1f8fd 75 6e 6b 20 46 69 6c 65 43 68 75 6e 6b 3b 0a 0a  unk FileChunk;..
1f8fe 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  /* Space to hold
1f8ff 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
1f900 75 72 6e 61 6c 20 69 73 20 61 6c 6c 6f 63 61 74  urnal is allocat
1f901 65 64 20 69 6e 20 69 6e 63 72 65 6d 65 6e 74 73  ed in increments
1f902 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6d 61 6e 79   of.** this many
1f903 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   bytes..**.** Th
1f904 65 20 73 69 7a 65 20 63 68 6f 73 65 6e 20 69 73  e size chosen is
1f905 20 61 20 6c 69 74 74 6c 65 20 6c 65 73 73 20 74   a little less t
1f906 68 61 6e 20 61 20 70 6f 77 65 72 20 6f 66 20 74  han a power of t
1f907 77 6f 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a  wo.  That way,.*
1f908 2a 20 74 68 65 20 46 69 6c 65 43 68 75 6e 6b 20  * the FileChunk 
1f909 6f 62 6a 65 63 74 20 77 69 6c 6c 20 68 61 76 65  object will have
1f90a 20 61 20 73 69 7a 65 20 74 68 61 74 20 61 6c 6d   a size that alm
1f90b 6f 73 74 20 65 78 61 63 74 6c 79 20 66 69 6c 6c  ost exactly fill
1f90c 73 0a 2a 2a 20 61 20 70 6f 77 65 72 2d 6f 66 2d  s.** a power-of-
1f90d 74 77 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  two allocation. 
1f90e 20 54 68 69 73 20 6d 69 6d 69 6d 69 7a 65 73 20   This mimimizes 
1f90f 77 61 73 74 65 64 20 73 70 61 63 65 20 69 6e 20  wasted space in 
1f910 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 0a 2a 2a 20  power-of-two.** 
1f911 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
1f912 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f  s..*/.#define JO
1f913 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20  URNAL_CHUNKSIZE 
1f914 28 28 69 6e 74 29 28 31 30 32 34 2d 73 69 7a 65  ((int)(1024-size
1f915 6f 66 28 46 69 6c 65 43 68 75 6e 6b 2a 29 29 29  of(FileChunk*)))
1f916 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69  ../* Macro to fi
1f917 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f  nd the minimum o
1f918 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61  f two numeric va
1f919 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  lues..*/.#ifndef
1f91a 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49   MIN.# define MI
1f91b 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f  N(x,y) ((x)<(y)?
1f91c 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a  (x):(y)).#endif.
1f91d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62  ./*.** The rollb
1f91e 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63  ack journal is c
1f91f 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e  omposed of a lin
1f920 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73  ked list of thes
1f921 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  e structures..*/
1f922 0a 73 74 72 75 63 74 20 46 69 6c 65 43 68 75 6e  .struct FileChun
1f923 6b 20 7b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20  k {.  FileChunk 
1f924 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
1f925 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68        /* Next ch
1f926 75 6e 6b 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  unk in the journ
1f927 61 6c 20 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e  al */.  u8 zChun
1f928 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53  k[JOURNAL_CHUNKS
1f929 49 5a 45 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65  IZE];   /* Conte
1f92a 6e 74 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b  nt of this chunk
1f92b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
1f92c 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
1f92d 73 20 6f 62 6a 65 63 74 20 73 65 72 76 65 73 20  s object serves 
1f92e 61 73 20 61 20 63 75 72 73 6f 72 20 69 6e 74 6f  as a cursor into
1f92f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
1f930 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75  urnal..** The cu
1f931 72 73 6f 72 20 63 61 6e 20 62 65 20 65 69 74 68  rsor can be eith
1f932 65 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f  er for reading o
1f933 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74  r writing..*/.st
1f934 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b  ruct FilePoint {
1f935 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
1f936 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20   iOffset;       
1f937 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f     /* Offset fro
1f938 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
1f939 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20  of the file */. 
1f93a 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75   FileChunk *pChu
1f93b 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nk;             
1f93c 20 2f 2a 20 53 70 65 63 69 66 69 63 20 63 68 75   /* Specific chu
1f93d 6e 6b 20 69 6e 74 6f 20 77 68 69 63 68 20 63 75  nk into which cu
1f93e 72 73 6f 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d  rsor points */.}
1f93f 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75  ;../*.** This su
1f940 62 63 6c 61 73 73 20 69 73 20 61 20 73 75 62 63  bclass is a subc
1f941 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  lass of sqlite3_
1f942 66 69 6c 65 2e 20 20 45 61 63 68 20 6f 70 65 6e  file.  Each open
1f943 20 6d 65 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a   memory-journal.
1f944 2a 2a 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  ** is an instanc
1f945 65 20 6f 66 20 74 68 69 73 20 63 6c 61 73 73 2e  e of this class.
1f946 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f  .*/.struct MemJo
1f947 75 72 6e 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65  urnal {.  sqlite
1f948 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
1f949 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72  ethod;    /* Par
1f94a 65 6e 74 20 63 6c 61 73 73 2e 20 4d 55 53 54 20  ent class. MUST 
1f94b 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 46 69  BE FIRST */.  Fi
1f94c 6c 65 43 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b  leChunk *pFirst;
1f94d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1f94e 20 48 65 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f   Head of in-memo
1f94f 72 79 20 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f  ry chunk-list */
1f950 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64  .  FilePoint end
1f951 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  point;          
1f952 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1f953 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1f954 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f  file */.  FilePo
1f955 69 6e 74 20 72 65 61 64 70 6f 69 6e 74 3b 20 20  int readpoint;  
1f956 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
1f957 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20  nter to the end 
1f958 6f 66 20 74 68 65 20 6c 61 73 74 20 78 52 65 61  of the last xRea
1f959 64 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  d() */.};../*.**
1f95a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20   Read data from 
1f95b 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f  the in-memory jo
1f95c 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 54 68 69  urnal file.  Thi
1f95d 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  s is the impleme
1f95e 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68  ntation.** of th
1f95f 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 52  e sqlite3_vfs.xR
1f960 65 61 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ead method..*/.s
1f961 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e  tatic int memjrn
1f962 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33  lRead(.  sqlite3
1f963 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20  _file *pJfd,    
1f964 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
1f965 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74  ile from which t
1f966 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64  o read */.  void
1f967 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20   *zBuf,         
1f968 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65     /* Put the re
1f969 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20  sults here */.  
1f96a 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1f96b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1f96c 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61   of bytes to rea
1f96d 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e  d */.  sqlite_in
1f96e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a  t64 iOfst     /*
1f96f 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
1f970 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
1f971 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c  .){.  MemJournal
1f972 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61   *p = (MemJourna
1f973 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a  l *)pJfd;.  u8 *
1f974 7a 4f 75 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69  zOut = zBuf;.  i
1f975 6e 74 20 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b  nt nRead = iAmt;
1f976 0a 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66  .  int iChunkOff
1f977 73 65 74 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b  set;.  FileChunk
1f978 20 2a 70 43 68 75 6e 6b 3b 0a 0a 20 20 2f 2a 20   *pChunk;..  /* 
1f979 53 51 4c 69 74 65 20 6e 65 76 65 72 20 74 72 69  SQLite never tri
1f97a 65 73 20 74 6f 20 72 65 61 64 20 70 61 73 74 20  es to read past 
1f97b 74 68 65 20 65 6e 64 20 6f 66 20 61 20 72 6f 6c  the end of a rol
1f97c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69  lback journal fi
1f97d 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  le */.  assert( 
1f97e 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d 3e 65  iOfst+iAmt<=p->e
1f97f 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  ndpoint.iOffset 
1f980 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 65 61  );..  if( p->rea
1f981 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 21 3d  dpoint.iOffset!=
1f982 69 4f 66 73 74 20 7c 7c 20 69 4f 66 73 74 3d 3d  iOfst || iOfst==
1f983 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1f984 5f 69 6e 74 36 34 20 69 4f 66 66 20 3d 20 30 3b  _int64 iOff = 0;
1f985 0a 20 20 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d  .    for(pChunk=
1f986 70 2d 3e 70 46 69 72 73 74 3b 20 0a 20 20 20 20  p->pFirst; .    
1f987 20 20 20 20 41 4c 57 41 59 53 28 70 43 68 75 6e      ALWAYS(pChun
1f988 6b 29 20 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52  k) && (iOff+JOUR
1f989 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d  NAL_CHUNKSIZE)<=
1f98a 69 4f 66 73 74 3b 0a 20 20 20 20 20 20 20 20 70  iOfst;.        p
1f98b 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e  Chunk=pChunk->pN
1f98c 65 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ext.    ){.     
1f98d 20 69 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c   iOff += JOURNAL
1f98e 5f 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20  _CHUNKSIZE;.    
1f98f 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  }.  }else{.    p
1f990 43 68 75 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70  Chunk = p->readp
1f991 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d  oint.pChunk;.  }
1f992 0a 0a 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74  ..  iChunkOffset
1f993 20 3d 20 28 69 6e 74 29 28 69 4f 66 73 74 25 4a   = (int)(iOfst%J
1f994 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
1f995 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e  );.  do {.    in
1f996 74 20 69 53 70 61 63 65 20 3d 20 4a 4f 55 52 4e  t iSpace = JOURN
1f997 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69  AL_CHUNKSIZE - i
1f998 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 20  ChunkOffset;.   
1f999 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e   int nCopy = MIN
1f99a 28 6e 52 65 61 64 2c 20 28 4a 4f 55 52 4e 41 4c  (nRead, (JOURNAL
1f99b 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68  _CHUNKSIZE - iCh
1f99c 75 6e 6b 4f 66 66 73 65 74 29 29 3b 0a 20 20 20  unkOffset));.   
1f99d 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 26 70   memcpy(zOut, &p
1f99e 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43  Chunk->zChunk[iC
1f99f 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20 6e 43 6f  hunkOffset], nCo
1f9a0 70 79 29 3b 0a 20 20 20 20 7a 4f 75 74 20 2b 3d  py);.    zOut +=
1f9a1 20 6e 43 6f 70 79 3b 0a 20 20 20 20 6e 52 65 61   nCopy;.    nRea
1f9a2 64 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20  d -= iSpace;.   
1f9a3 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20   iChunkOffset = 
1f9a4 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 52  0;.  } while( nR
1f9a5 65 61 64 3e 3d 30 20 26 26 20 28 70 43 68 75 6e  ead>=0 && (pChun
1f9a6 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 29  k=pChunk->pNext)
1f9a7 21 3d 30 20 26 26 20 6e 52 65 61 64 3e 30 20 29  !=0 && nRead>0 )
1f9a8 3b 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74  ;.  p->readpoint
1f9a9 2e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 73 74  .iOffset = iOfst
1f9aa 2b 69 41 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64  +iAmt;.  p->read
1f9ab 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70  point.pChunk = p
1f9ac 43 68 75 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e  Chunk;..  return
1f9ad 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1f9ae 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20  *.** Write data 
1f9af 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a  to the file..*/.
1f9b0 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72  static int memjr
1f9b1 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  nlWrite(.  sqlit
1f9b2 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1f9b3 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
1f9b4 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68   file into which
1f9b5 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63   to write */.  c
1f9b6 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c  onst void *zBuf,
1f9b7 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61        /* Take da
1f9b8 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  ta to be written
1f9b9 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20   from here */.  
1f9ba 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1f9bb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1f9bc 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69   of bytes to wri
1f9bd 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  te */.  sqlite_i
1f9be 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f  nt64 iOfst     /
1f9bf 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20  * Begin writing 
1f9c0 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69  at this offset i
1f9c1 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  nto the file */.
1f9c2 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ){.  MemJournal 
1f9c3 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c  *p = (MemJournal
1f9c4 20 2a 29 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e   *)pJfd;.  int n
1f9c5 57 72 69 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20  Write = iAmt;.  
1f9c6 75 38 20 2a 7a 57 72 69 74 65 20 3d 20 28 75 38  u8 *zWrite = (u8
1f9c7 20 2a 29 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41   *)zBuf;..  /* A
1f9c8 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72  n in-memory jour
1f9c9 6e 61 6c 20 66 69 6c 65 20 73 68 6f 75 6c 64 20  nal file should 
1f9ca 6f 6e 6c 79 20 65 76 65 72 20 62 65 20 61 70 70  only ever be app
1f9cb 65 6e 64 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d  ended to. Random
1f9cc 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 77 72 69  .  ** access wri
1f9cd 74 65 73 20 61 72 65 20 6e 6f 74 20 72 65 71 75  tes are not requ
1f9ce 69 72 65 64 20 62 79 20 73 71 6c 69 74 65 2e 0a  ired by sqlite..
1f9cf 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69    */.  assert( i
1f9d0 4f 66 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e  Ofst==p->endpoin
1f9d1 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a 20 20 55  t.iOffset );.  U
1f9d2 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
1f9d3 69 4f 66 73 74 29 3b 0a 0a 20 20 77 68 69 6c 65  iOfst);..  while
1f9d4 28 20 6e 57 72 69 74 65 3e 30 20 29 7b 0a 20 20  ( nWrite>0 ){.  
1f9d5 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68    FileChunk *pCh
1f9d6 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e  unk = p->endpoin
1f9d7 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 69 6e  t.pChunk;.    in
1f9d8 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d  t iChunkOffset =
1f9d9 20 28 69 6e 74 29 28 70 2d 3e 65 6e 64 70 6f 69   (int)(p->endpoi
1f9da 6e 74 2e 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e  nt.iOffset%JOURN
1f9db 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20  AL_CHUNKSIZE);. 
1f9dc 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20     int iSpace = 
1f9dd 4d 49 4e 28 6e 57 72 69 74 65 2c 20 4a 4f 55 52  MIN(nWrite, JOUR
1f9de 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20  NAL_CHUNKSIZE - 
1f9df 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a  iChunkOffset);..
1f9e0 20 20 20 20 69 66 28 20 69 43 68 75 6e 6b 4f 66      if( iChunkOf
1f9e1 66 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  fset==0 ){.     
1f9e2 20 2f 2a 20 4e 65 77 20 63 68 75 6e 6b 20 69 73   /* New chunk is
1f9e3 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 74   required to ext
1f9e4 65 6e 64 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f  end the file. */
1f9e5 0a 20 20 20 20 20 20 46 69 6c 65 43 68 75 6e 6b  .      FileChunk
1f9e6 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *pNew = sqlite3
1f9e7 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46  _malloc(sizeof(F
1f9e8 69 6c 65 43 68 75 6e 6b 29 29 3b 0a 20 20 20 20  ileChunk));.    
1f9e9 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20    if( !pNew ){. 
1f9ea 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
1f9eb 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
1f9ec 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1f9ed 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  pNew->pNext = 0;
1f9ee 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 75 6e  .      if( pChun
1f9ef 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  k ){.        ass
1f9f0 65 72 74 28 20 70 2d 3e 70 46 69 72 73 74 20 29  ert( p->pFirst )
1f9f1 3b 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b  ;.        pChunk
1f9f2 2d 3e 70 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a  ->pNext = pNew;.
1f9f3 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1f9f4 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 2d       assert( !p-
1f9f5 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20  >pFirst );.     
1f9f6 20 20 20 70 2d 3e 70 46 69 72 73 74 20 3d 20 70     p->pFirst = p
1f9f7 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  New;.      }.   
1f9f8 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70     p->endpoint.p
1f9f9 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20  Chunk = pNew;.  
1f9fa 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28    }..    memcpy(
1f9fb 26 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68  &p->endpoint.pCh
1f9fc 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75  unk->zChunk[iChu
1f9fd 6e 6b 4f 66 66 73 65 74 5d 2c 20 7a 57 72 69 74  nkOffset], zWrit
1f9fe 65 2c 20 69 53 70 61 63 65 29 3b 0a 20 20 20 20  e, iSpace);.    
1f9ff 7a 57 72 69 74 65 20 2b 3d 20 69 53 70 61 63 65  zWrite += iSpace
1fa00 3b 0a 20 20 20 20 6e 57 72 69 74 65 20 2d 3d 20  ;.    nWrite -= 
1fa01 69 53 70 61 63 65 3b 0a 20 20 20 20 70 2d 3e 65  iSpace;.    p->e
1fa02 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  ndpoint.iOffset 
1fa03 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 7d 0a 0a  += iSpace;.  }..
1fa04 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1fa05 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  OK;.}../*.** Tru
1fa06 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a  ncate the file..
1fa07 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
1fa08 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71  mjrnlTruncate(sq
1fa09 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1fa0a 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73  , sqlite_int64 s
1fa0b 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e  ize){.  MemJourn
1fa0c 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72  al *p = (MemJour
1fa0d 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 46 69  nal *)pJfd;.  Fi
1fa0e 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b  leChunk *pChunk;
1fa0f 0a 20 20 61 73 73 65 72 74 28 73 69 7a 65 3d 3d  .  assert(size==
1fa10 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  0);.  UNUSED_PAR
1fa11 41 4d 45 54 45 52 28 73 69 7a 65 29 3b 0a 20 20  AMETER(size);.  
1fa12 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 46 69 72  pChunk = p->pFir
1fa13 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 68  st;.  while( pCh
1fa14 75 6e 6b 20 29 7b 0a 20 20 20 20 46 69 6c 65 43  unk ){.    FileC
1fa15 68 75 6e 6b 20 2a 70 54 6d 70 20 3d 20 70 43 68  hunk *pTmp = pCh
1fa16 75 6e 6b 3b 0a 20 20 20 20 70 43 68 75 6e 6b 20  unk;.    pChunk 
1fa17 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b  = pChunk->pNext;
1fa18 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
1fa19 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 73  e(pTmp);.  }.  s
1fa1a 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c  qlite3MemJournal
1fa1b 4f 70 65 6e 28 70 4a 66 64 29 3b 0a 20 20 72 65  Open(pJfd);.  re
1fa1c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1fa1d 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74  }../*.** Close t
1fa1e 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  he file..*/.stat
1fa1f 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c  ic int memjrnlCl
1fa20 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
1fa21 20 2a 70 4a 66 64 29 7b 0a 20 20 6d 65 6d 6a 72   *pJfd){.  memjr
1fa22 6e 6c 54 72 75 6e 63 61 74 65 28 70 4a 66 64 2c  nlTruncate(pJfd,
1fa23 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51   0);.  return SQ
1fa24 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
1fa25 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65  ** Sync the file
1fa26 2e 0a 2a 2a 0a 2a 2a 20 53 79 6e 63 69 6e 67 20  ..**.** Syncing 
1fa27 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  an in-memory jou
1fa28 72 6e 61 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  rnal is a no-op.
1fa29 20 20 41 6e 64 2c 20 69 6e 20 66 61 63 74 2c 20    And, in fact, 
1fa2a 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
1fa2b 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20  is never called 
1fa2c 69 6e 20 61 20 77 6f 72 6b 69 6e 67 20 69 6d 70  in a working imp
1fa2d 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
1fa2e 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
1fa2f 6e 0a 2a 2a 20 65 78 69 73 74 73 20 70 75 72 65  n.** exists pure
1fa30 6c 79 20 61 73 20 61 20 63 6f 6e 74 69 6e 67 65  ly as a continge
1fa31 6e 63 79 2c 20 69 6e 20 63 61 73 65 20 73 6f 6d  ncy, in case som
1fa32 65 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 69 6e  e malfunction in
1fa33 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70   some other.** p
1fa34 61 72 74 20 6f 66 20 53 51 4c 69 74 65 20 63 61  art of SQLite ca
1fa35 75 73 65 73 20 53 79 6e 63 20 74 6f 20 62 65 20  uses Sync to be 
1fa36 63 61 6c 6c 65 64 20 62 79 20 6d 69 73 74 61 6b  called by mistak
1fa37 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1fa38 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 28 73 71 6c   memjrnlSync(sql
1fa39 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73  ite3_file *NotUs
1fa3a 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32  ed, int NotUsed2
1fa3b 29 7b 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f  ){   /*NO_TEST*/
1fa3c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1fa3d 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
1fa3e 74 55 73 65 64 32 29 3b 20 20 20 20 20 20 20 20  tUsed2);        
1fa3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa40 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 61 73  /*NO_TEST*/.  as
1fa41 73 65 72 74 28 20 30 20 29 3b 20 20 20 20 20 20  sert( 0 );      
1fa42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa44 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f             /*NO_
1fa45 54 45 53 54 2a 2f 0a 20 20 72 65 74 75 72 6e 20  TEST*/.  return 
1fa46 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
1fa47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa49 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
1fa4a 2f 0a 7d 20 20 20 20 20 20 20 20 20 20 20 20 20  /.}             
1fa4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa4e 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 0a 2f 2a   /*NO_TEST*/../*
1fa4f 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69  .** Query the si
1fa50 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ze of the file i
1fa51 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  n bytes..*/.stat
1fa52 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69  ic int memjrnlFi
1fa53 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66  leSize(sqlite3_f
1fa54 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74  ile *pJfd, sqlit
1fa55 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b  e_int64 *pSize){
1fa56 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70  .  MemJournal *p
1fa57 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a   = (MemJournal *
1fa58 29 70 4a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20  )pJfd;.  *pSize 
1fa59 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  = (sqlite_int64)
1fa5a 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66   p->endpoint.iOf
1fa5b 66 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53  fset;.  return S
1fa5c 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1fa5d 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68  ** Table of meth
1fa5e 6f 64 73 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e  ods for MemJourn
1fa5f 61 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  al sqlite3_file 
1fa60 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69  object..*/.stati
1fa61 63 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  c struct sqlite3
1fa62 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a  _io_methods MemJ
1fa63 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20  ournalMethods = 
1fa64 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  {.  1,          
1fa65 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
1fa66 6e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c  n */.  memjrnlCl
1fa67 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f  ose,     /* xClo
1fa68 73 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52  se */.  memjrnlR
1fa69 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65  ead,      /* xRe
1fa6a 61 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57  ad */.  memjrnlW
1fa6b 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72  rite,     /* xWr
1fa6c 69 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c  ite */.  memjrnl
1fa6d 54 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54  Truncate,  /* xT
1fa6e 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d  runcate */.  mem
1fa6f 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f  jrnlSync,      /
1fa70 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d  * xSync */.  mem
1fa71 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f  jrnlFileSize,  /
1fa72 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20  * xFileSize */. 
1fa73 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1fa74 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20     /* xLock */. 
1fa75 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1fa76 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f     /* xUnlock */
1fa77 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
1fa78 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65       /* xCheckRe
1fa79 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20  servedLock */.  
1fa7a 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1fa7b 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f    /* xFileContro
1fa7c 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  l */.  0,       
1fa7d 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63           /* xSec
1fa7e 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20  torSize */.  0  
1fa7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1fa80 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74  * xDeviceCharact
1fa81 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a  eristics */.};..
1fa82 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f  /* .** Open a jo
1fa83 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53  urnal file..*/.S
1fa84 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1fa85 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  id sqlite3MemJou
1fa86 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33  rnalOpen(sqlite3
1fa87 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20  _file *pJfd){.  
1fa88 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20  MemJournal *p = 
1fa89 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a  (MemJournal *)pJ
1fa8a 66 64 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49  fd;.  assert( EI
1fa8b 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
1fa8c 4e 54 28 70 29 20 29 3b 0a 20 20 6d 65 6d 73 65  NT(p) );.  memse
1fa8d 74 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d  t(p, 0, sqlite3M
1fa8e 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29  emJournalSize())
1fa8f 3b 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d  ;.  p->pMethod =
1fa90 20 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68   &MemJournalMeth
1fa91 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ods;.}../*.** Re
1fa92 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
1fa93 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 70 61 73   file-handle pas
1fa94 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65  sed as an argume
1fa95 6e 74 20 69 73 20 0a 2a 2a 20 61 6e 20 69 6e 2d  nt is .** an in-
1fa96 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 0a  memory journal .
1fa97 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1fa98 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d  E int sqlite3IsM
1fa99 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65  emJournal(sqlite
1fa9a 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20  3_file *pJfd){. 
1fa9b 20 72 65 74 75 72 6e 20 70 4a 66 64 2d 3e 70 4d   return pJfd->pM
1fa9c 65 74 68 6f 64 73 3d 3d 26 4d 65 6d 4a 6f 75 72  ethods==&MemJour
1fa9d 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f  nalMethods;.}../
1fa9e 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  * .** Return the
1fa9f 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1faa0 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
1faa1 72 65 20 61 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  re a MemJournal 
1faa2 74 68 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a  that uses vfs.**
1faa3 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 65 20   pVfs to create 
1faa4 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f  the underlying o
1faa5 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f  n-disk files..*/
1faa6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1faa7 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f  int sqlite3MemJo
1faa8 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 7b  urnalSize(void){
1faa9 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66  .  return sizeof
1faaa 28 4d 65 6d 4a 6f 75 72 6e 61 6c 29 3b 0a 7d 0a  (MemJournal);.}.
1faab 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1faac 20 45 6e 64 20 6f 66 20 6d 65 6d 6a 6f 75 72 6e   End of memjourn
1faad 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  al.c ***********
1faae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1fab0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1fab1 20 42 65 67 69 6e 20 66 69 6c 65 20 77 61 6c 6b   Begin file walk
1fab2 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
1fab3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fab4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1fab5 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75  ./*.** 2008 Augu
1fab6 73 74 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 16.**.** The 
1fab7 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1fab8 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1fab9 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1faba 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1fabb 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1fabc 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1fabd 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1fabe 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1fabf 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1fac0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1fac1 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1fac2 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1fac3 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1fac4 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1fac5 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1fac6 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1fac7 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1fac8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fac9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1facb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1facc 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
1facd 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69  e contains routi
1face 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c  nes used for wal
1facf 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20  king the parser 
1fad0 74 72 65 65 20 66 6f 72 0a 2a 2a 20 61 6e 20 53  tree for.** an S
1fad1 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  QL statement..**
1fad2 0a 2a 2a 20 24 49 64 3a 20 77 61 6c 6b 65 72 2e  .** $Id: walker.
1fad3 63 2c 76 20 31 2e 37 20 32 30 30 39 2f 30 36 2f  c,v 1.7 2009/06/
1fad4 31 35 20 32 33 3a 31 35 3a 35 39 20 64 72 68 20  15 23:15:59 drh 
1fad5 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  Exp $.*/.../*.**
1fad6 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73   Walk an express
1fad7 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e 76 6f 6b  ion tree.  Invok
1fad8 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f  e the callback o
1fad9 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6e 6f 64  nce for each nod
1fada 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 72  e.** of the expr
1fadb 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 20 64 65  ession, while de
1fadc 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e 20 6f 74  cending.  (In ot
1fadd 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63  her words, the c
1fade 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e  allback.** is in
1fadf 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 76 69 73  voked before vis
1fae0 69 74 69 6e 67 20 63 68 69 6c 64 72 65 6e 2e 29  iting children.)
1fae1 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
1fae2 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65  n value from the
1fae3 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64   callback should
1fae4 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 57   be one of the W
1fae5 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74  RC_*.** constant
1fae6 73 20 74 6f 20 73 70 65 63 69 66 79 20 68 6f 77  s to specify how
1fae7 20 74 6f 20 70 72 6f 63 65 65 64 20 77 69 74 68   to proceed with
1fae8 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a   the walk..**.**
1fae9 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65      WRC_Continue
1faea 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 65 20 64        Continue d
1faeb 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 6e 20 74  escending down t
1faec 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 20  he tree..**.**  
1faed 20 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20    WRC_Prune     
1faee 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 73 63 65      Do not desce
1faef 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e 6f  nd into child no
1faf0 64 65 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77 0a  des.  But allow.
1faf1 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1faf2 20 20 20 20 20 20 20 20 74 68 65 20 77 61 6c 6b          the walk
1faf3 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
1faf4 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73 2e  h sibling nodes.
1faf5 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 41 62  .**.**    WRC_Ab
1faf6 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f 20 6e  ort         Do n
1faf7 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b 73  o more callbacks
1faf8 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20 73 74  .  Unwind the st
1faf9 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ack and.**      
1fafa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fafb 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70 2d 6c  return the top-l
1fafc 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c 2e 0a  evel walk call..
1fafd 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  **.** The return
1fafe 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73   value from this
1faff 20 72 6f 75 74 69 6e 65 20 69 73 20 57 52 43 5f   routine is WRC_
1fb00 41 62 6f 72 74 20 74 6f 20 61 62 61 6e 64 6f 6e  Abort to abandon
1fb01 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 0a 2a   the tree walk.*
1fb02 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e 74 69 6e  * and WRC_Contin
1fb03 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a  ue to continue..
1fb04 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1fb05 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c  E int sqlite3Wal
1fb06 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57  kExpr(Walker *pW
1fb07 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78  alker, Expr *pEx
1fb08 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  pr){.  int rc;. 
1fb09 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
1fb0a 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
1fb0b 6e 75 65 3b 0a 20 20 74 65 73 74 63 61 73 65 28  nue;.  testcase(
1fb0c 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
1fb0d 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e  (pExpr, EP_Token
1fb0e 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65 73 74 63  Only) );.  testc
1fb0f 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f 70  ase( ExprHasProp
1fb10 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52  erty(pExpr, EP_R
1fb11 65 64 75 63 65 64 29 20 29 3b 0a 20 20 72 63 20  educed) );.  rc 
1fb12 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72  = pWalker->xExpr
1fb13 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72  Callback(pWalker
1fb14 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20  , pExpr);.  if( 
1fb15 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65  rc==WRC_Continue
1fb16 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26  .              &
1fb17 26 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f  & !ExprHasAnyPro
1fb18 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f 54  perty(pExpr,EP_T
1fb19 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20  okenOnly) ){.   
1fb1a 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1fb1b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 45  Expr(pWalker, pE
1fb1c 78 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72 65  xpr->pLeft) ) re
1fb1d 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1fb1e 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
1fb1f 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  alkExpr(pWalker,
1fb20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20   pExpr->pRight) 
1fb21 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1fb22 72 74 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72  rt;.    if( Expr
1fb23 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
1fb24 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
1fb25 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71   ){.      if( sq
1fb26 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
1fb27 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e  pWalker, pExpr->
1fb28 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72 65 74  x.pSelect) ) ret
1fb29 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1fb2a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1fb2b 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  if( sqlite3WalkE
1fb2c 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c  xprList(pWalker,
1fb2d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29   pExpr->x.pList)
1fb2e 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1fb2f 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ort;.    }.  }. 
1fb30 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 52 43   return rc & WRC
1fb31 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _Abort;.}../*.**
1fb32 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c   Call sqlite3Wal
1fb33 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72  kExpr() for ever
1fb34 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  y expression in 
1fb35 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 69 6c 0a  list p or until.
1fb36 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 65 71 75  ** an abort requ
1fb37 65 73 74 20 69 73 20 73 65 65 6e 2e 0a 2a 2f 0a  est is seen..*/.
1fb38 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1fb39 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  nt sqlite3WalkEx
1fb3a 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 20 2a 70  prList(Walker *p
1fb3b 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c 69 73 74  Walker, ExprList
1fb3c 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20   *p){.  int i;. 
1fb3d 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
1fb3e 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
1fb3f 69 66 28 20 70 20 29 7b 0a 20 20 20 20 66 6f 72  if( p ){.    for
1fb40 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 74  (i=p->nExpr, pIt
1fb41 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d  em=p->a; i>0; i-
1fb42 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  -, pItem++){.   
1fb43 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61     if( sqlite3Wa
1fb44 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20  lkExpr(pWalker, 
1fb45 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20  pItem->pExpr) ) 
1fb46 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1fb47 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1fb48 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
1fb49 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b  e;.}../*.** Walk
1fb4a 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73   all expressions
1fb4b 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1fb4c 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1fb4d 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20  t p.  Do.** not 
1fb4e 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 4c 45 43  invoke the SELEC
1fb4f 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c  T callback on p,
1fb50 20 62 75 74 20 64 6f 20 28 6f 66 20 63 6f 75 72   but do (of cour
1fb51 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a 20 61 6e  se) invoke.** an
1fb52 79 20 65 78 70 72 20 63 61 6c 6c 62 61 63 6b 73  y expr callbacks
1fb53 20 61 6e 64 20 53 45 4c 45 43 54 20 63 61 6c 6c   and SELECT call
1fb54 62 61 63 6b 73 20 74 68 61 74 20 63 6f 6d 65 20  backs that come 
1fb55 66 72 6f 6d 20 73 75 62 71 75 65 72 69 65 73 2e  from subqueries.
1fb56 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f 41  .** Return WRC_A
1fb57 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f 6e 74  bort or WRC_Cont
1fb58 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  inue..*/.SQLITE_
1fb59 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1fb5a 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70  te3WalkSelectExp
1fb5b 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  r(Walker *pWalke
1fb5c 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  r, Select *p){. 
1fb5d 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1fb5e 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72  ExprList(pWalker
1fb5f 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 29 20 72  , p->pEList) ) r
1fb60 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1fb61 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61  .  if( sqlite3Wa
1fb62 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20  lkExpr(pWalker, 
1fb63 70 2d 3e 70 57 68 65 72 65 29 20 29 20 72 65 74  p->pWhere) ) ret
1fb64 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1fb65 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1fb66 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72  ExprList(pWalker
1fb67 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 20 29  , p->pGroupBy) )
1fb68 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1fb69 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  t;.  if( sqlite3
1fb6a 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72  WalkExpr(pWalker
1fb6b 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 20 29 20  , p->pHaving) ) 
1fb6c 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1fb6d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  ;.  if( sqlite3W
1fb6e 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
1fb6f 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  ker, p->pOrderBy
1fb70 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1fb71 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  bort;.  if( sqli
1fb72 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1fb73 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20  ker, p->pLimit) 
1fb74 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1fb75 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rt;.  if( sqlite
1fb76 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
1fb77 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 20 29  r, p->pOffset) )
1fb78 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1fb79 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f  t;.  return WRC_
1fb7a 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a  Continue;.}../*.
1fb7b 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 61 72 73  ** Walk the pars
1fb7c 65 20 74 72 65 65 73 20 61 73 73 6f 63 69 61 74  e trees associat
1fb7d 65 64 20 77 69 74 68 20 61 6c 6c 20 73 75 62 71  ed with all subq
1fb7e 75 65 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a  ueries in the.**
1fb7f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
1fb80 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1fb81 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 6e 76 6f   p.  Do not invo
1fb82 6b 65 20 74 68 65 20 73 65 6c 65 63 74 0a 2a 2a  ke the select.**
1fb83 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c 20   callback on p, 
1fb84 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 20 69 74  but do invoke it
1fb85 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d 20 63 6c   on each FROM cl
1fb86 61 75 73 65 20 73 75 62 71 75 65 72 79 0a 2a 2a  ause subquery.**
1fb87 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 75 62 71   and on any subq
1fb88 75 65 72 69 65 73 20 66 75 72 74 68 65 72 20 64  ueries further d
1fb89 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 65 65 2e  own in the tree.
1fb8a 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 57 52 43    Return .** WRC
1fb8b 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f  _Abort or WRC_Co
1fb8c 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 4c 49 54  ntinue;.*/.SQLIT
1fb8d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1fb8e 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46  lite3WalkSelectF
1fb8f 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  rom(Walker *pWal
1fb90 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b  ker, Select *p){
1fb91 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
1fb92 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  ;.  int i;.  str
1fb93 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
1fb94 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 53 72 63   *pItem;..  pSrc
1fb95 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 69 66   = p->pSrc;.  if
1fb96 28 20 41 4c 57 41 59 53 28 70 53 72 63 29 20 29  ( ALWAYS(pSrc) )
1fb97 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 72 63  {.    for(i=pSrc
1fb98 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d 70 53  ->nSrc, pItem=pS
1fb99 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c  rc->a; i>0; i--,
1fb9a 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1fb9b 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1fb9c 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20  Select(pWalker, 
1fb9d 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20  pItem->pSelect) 
1fb9e 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
1fb9f 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
1fba0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1fba1 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
1fba2 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a 2a 2a 20  inue;.} ../*.** 
1fba3 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c 6b  Call sqlite3Walk
1fba4 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72 79  Expr() for every
1fba5 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 53   expression in S
1fba6 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
1fba7 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71 6c  p..** Invoke sql
1fba8 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 29  ite3WalkSelect()
1fba9 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 20   for subqueries 
1fbaa 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
1fbab 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 74 68 65  se and.** on the
1fbac 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
1fbad 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 72 69 6f   chain, p->pPrio
1fbae 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  r..**.** Return 
1fbaf 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 75 6e 64  WRC_Continue und
1fbb0 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e 64 69 74  er normal condit
1fbb1 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e 20 57 52  ions.  Return WR
1fbb2 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a 20 74 68  C_Abort if.** th
1fbb3 65 72 65 20 69 73 20 61 6e 20 61 62 6f 72 74 20  ere is an abort 
1fbb4 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 49  request..**.** I
1fbb5 66 20 74 68 65 20 57 61 6c 6b 65 72 20 64 6f 65  f the Walker doe
1fbb6 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 78 53  s not have an xS
1fbb7 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 29 20  electCallback() 
1fbb8 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
1fbb9 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 20  e.** is a no-op 
1fbba 72 65 74 75 72 6e 69 6e 67 20 57 52 43 5f 43 6f  returning WRC_Co
1fbbb 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ntinue..*/.SQLIT
1fbbc 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1fbbd 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
1fbbe 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
1fbbf 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69   Select *p){.  i
1fbc0 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 3d 3d  nt rc;.  if( p==
1fbc1 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e 78 53  0 || pWalker->xS
1fbc2 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 3d 3d 30  electCallback==0
1fbc3 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f   ) return WRC_Co
1fbc4 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d 20 57  ntinue;.  rc = W
1fbc5 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 77  RC_Continue;.  w
1fbc6 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 20 20 20  hile( p  ){.    
1fbc7 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 53  rc = pWalker->xS
1fbc8 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 70 57  electCallback(pW
1fbc9 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 20 20 69  alker, p);.    i
1fbca 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b 0a 20  f( rc ) break;. 
1fbcb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61     if( sqlite3Wa
1fbcc 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70 57 61  lkSelectExpr(pWa
1fbcd 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 72  lker, p) ) retur
1fbce 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
1fbcf 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1fbd0 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 61 6c 6b  SelectFrom(pWalk
1fbd1 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20  er, p) ) return 
1fbd2 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 70  WRC_Abort;.    p
1fbd3 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
1fbd4 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20  }.  return rc & 
1fbd5 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a  WRC_Abort;.}../*
1fbd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1fbd7 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20 2a 2a  d of walker.c **
1fbd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1fbdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1fbdc 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c 76 65  gin file resolve
1fbdd 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1fbde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbdf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1fbe0 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20  .** 2008 August 
1fbe1 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  18.**.** The aut
1fbe2 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1fbe3 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1fbe4 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1fbe5 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1fbe6 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1fbe7 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1fbe8 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1fbe9 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1fbea 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
1fbeb 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
1fbec 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
1fbed 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1fbee 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1fbef 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1fbf0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1fbf1 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1fbf2 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1fbf3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbf7 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
1fbf8 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69  e contains routi
1fbf9 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c  nes used for wal
1fbfa 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20  king the parser 
1fbfb 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65 73 6f  tree and.** reso
1fbfc 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69 66 69  lve all identifi
1fbfd 65 72 73 20 62 79 20 61 73 73 6f 63 69 61 74 69  ers by associati
1fbfe 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61 20 70  ng them with a p
1fbff 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74 61 62  articular.** tab
1fc00 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a  le and column..*
1fc01 2a 0a 2a 2a 20 24 49 64 3a 20 72 65 73 6f 6c 76  *.** $Id: resolv
1fc02 65 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39 2f  e.c,v 1.30 2009/
1fc03 30 36 2f 31 35 20 32 33 3a 31 35 3a 35 39 20 64  06/15 23:15:59 d
1fc04 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  rh Exp $.*/../*.
1fc05 2a 2a 20 54 75 72 6e 20 74 68 65 20 70 45 78 70  ** Turn the pExp
1fc06 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74  r expression int
1fc07 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 74  o an alias for t
1fc08 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d  he iCol-th colum
1fc09 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 75  n of the.** resu
1fc0a 6c 74 20 73 65 74 20 69 6e 20 70 45 4c 69 73 74  lt set in pEList
1fc0b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ..**.** If the r
1fc0c 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e  esult set column
1fc0d 20 69 73 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c   is a simple col
1fc0e 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 2c 20 74  umn reference, t
1fc0f 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
1fc10 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20 65 78 61  .** makes an exa
1fc11 63 74 20 63 6f 70 79 2e 20 20 42 75 74 20 66 6f  ct copy.  But fo
1fc12 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64  r any other kind
1fc13 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 2c 20   of expression, 
1fc14 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
1fc15 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  make a copy of t
1fc16 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f  he result set co
1fc17 6c 75 6d 6e 20 61 73 20 74 68 65 20 61 72 67 75  lumn as the argu
1fc18 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 54  ment to the.** T
1fc19 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 2e 20 20  K_AS operator.  
1fc1a 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74  The TK_AS operat
1fc1b 6f 72 20 63 61 75 73 65 73 20 74 68 65 20 65 78  or causes the ex
1fc1c 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 0a 2a  pression to be.*
1fc1d 2a 20 65 76 61 6c 75 61 74 65 64 20 6a 75 73 74  * evaluated just
1fc1e 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 6e 20 72   once and then r
1fc1f 65 75 73 65 64 20 66 6f 72 20 65 61 63 68 20 61  eused for each a
1fc20 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lias..**.** The 
1fc21 72 65 61 73 6f 6e 20 66 6f 72 20 73 75 70 70 72  reason for suppr
1fc22 65 73 73 69 6e 67 20 74 68 65 20 54 4b 5f 41 53  essing the TK_AS
1fc23 20 74 65 72 6d 20 77 68 65 6e 20 74 68 65 20 65   term when the e
1fc24 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73  xpression is a s
1fc25 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  imple.** column 
1fc26 72 65 66 65 72 65 6e 63 65 20 69 73 20 73 6f 20  reference is so 
1fc27 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20  that the column 
1fc28 72 65 66 65 72 65 6e 63 65 20 77 69 6c 6c 20 62  reference will b
1fc29 65 20 72 65 63 6f 67 6e 69 7a 65 64 20 61 73 0a  e recognized as.
1fc2a 2a 2a 20 75 73 61 62 6c 65 20 62 79 20 69 6e 64  ** usable by ind
1fc2b 69 63 65 73 20 77 69 74 68 69 6e 20 74 68 65 20  ices within the 
1fc2c 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f  WHERE clause pro
1fc2d 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 2e 20 0a  cessing logic. .
1fc2e 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20 54 68 65  **.** Hack:  The
1fc2f 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20   TK_AS operator 
1fc30 69 73 20 69 6e 68 69 62 69 74 65 64 20 69 66 20  is inhibited if 
1fc31 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27 2e 20 20  zType[0]=='G'.  
1fc32 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 68  This means.** th
1fc33 61 74 20 69 6e 20 61 20 47 52 4f 55 50 20 42 59  at in a GROUP BY
1fc34 20 63 6c 61 75 73 65 2c 20 74 68 65 20 65 78 70   clause, the exp
1fc35 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61 6c 75  ression is evalu
1fc36 61 74 65 64 20 74 77 69 63 65 2e 20 20 48 65 6e  ated twice.  Hen
1fc37 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  ce:.**.**     SE
1fc38 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 35 20  LECT random()%5 
1fc39 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46  AS x, count(*) F
1fc3a 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 42 59  ROM tab GROUP BY
1fc3b 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65 71 75 69   x.**.** Is equi
1fc3c 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a  valent to:.**.**
1fc3d 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64       SELECT rand
1fc3e 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75  om()%5 AS x, cou
1fc3f 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47  nt(*) FROM tab G
1fc40 52 4f 55 50 20 42 59 20 72 61 6e 64 6f 6d 28 29  ROUP BY random()
1fc41 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73  %5.**.** The res
1fc42 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d 28 29 25  ult of random()%
1fc43 35 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42  5 in the GROUP B
1fc44 59 20 63 6c 61 75 73 65 20 69 73 20 70 72 6f 62  Y clause is prob
1fc45 61 62 6c 79 20 64 69 66 66 65 72 65 6e 74 0a 2a  ably different.*
1fc46 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c  * from the resul
1fc47 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d  t in the result-
1fc48 73 65 74 2e 20 20 57 65 20 6d 69 67 68 74 20 66  set.  We might f
1fc49 69 78 20 74 68 69 73 20 73 6f 6d 65 64 61 79 2e  ix this someday.
1fc4a 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20 61 67 61    Or.** then aga
1fc4b 69 6e 2c 20 77 65 20 6d 69 67 68 74 20 6e 6f 74  in, we might not
1fc4c 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ....*/.static vo
1fc4d 69 64 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28  id resolveAlias(
1fc4e 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1fc4f 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  ,         /* Par
1fc50 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
1fc51 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
1fc52 73 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 72 65  st,      /* A re
1fc53 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 6e  sult set */.  in
1fc54 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  t iCol,         
1fc55 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c 75 6d 6e       /* A column
1fc56 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1fc57 65 74 2e 20 20 30 2e 2e 70 45 4c 69 73 74 2d 3e  et.  0..pEList->
1fc58 6e 45 78 70 72 2d 31 20 2a 2f 0a 20 20 45 78 70  nExpr-1 */.  Exp
1fc59 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20  r *pExpr,       
1fc5a 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 6f 72 6d      /* Transform
1fc5b 20 74 68 69 73 20 69 6e 74 6f 20 61 6e 20 61 6c   this into an al
1fc5c 69 61 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c  ias to the resul
1fc5d 74 20 73 65 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  t set */.  const
1fc5e 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20   char *zType    
1fc5f 20 20 2f 2a 20 22 47 52 4f 55 50 22 20 6f 72 20    /* "GROUP" or 
1fc60 22 4f 52 44 45 52 22 20 6f 72 20 22 22 20 2a 2f  "ORDER" or "" */
1fc61 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4f 72 69  .){.  Expr *pOri
1fc62 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g;           /* 
1fc63 54 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75  The iCol-th colu
1fc64 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  mn of the result
1fc65 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a   set */.  Expr *
1fc66 70 44 75 70 3b 20 20 20 20 20 20 20 20 20 20 20  pDup;           
1fc67 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 4f 72 69   /* Copy of pOri
1fc68 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  g */.  sqlite3 *
1fc69 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  db;           /*
1fc6a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
1fc6b 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61  nnection */..  a
1fc6c 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20 26  ssert( iCol>=0 &
1fc6d 26 20 69 43 6f 6c 3c 70 45 4c 69 73 74 2d 3e 6e  & iCol<pEList->n
1fc6e 45 78 70 72 20 29 3b 0a 20 20 70 4f 72 69 67 20  Expr );.  pOrig 
1fc6f 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c  = pEList->a[iCol
1fc70 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 73 65 72  ].pExpr;.  asser
1fc71 74 28 20 70 4f 72 69 67 21 3d 30 20 29 3b 0a 20  t( pOrig!=0 );. 
1fc72 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 2d 3e   assert( pOrig->
1fc73 66 6c 61 67 73 20 26 20 45 50 5f 52 65 73 6f 6c  flags & EP_Resol
1fc74 76 65 64 20 29 3b 0a 20 20 64 62 20 3d 20 70 50  ved );.  db = pP
1fc75 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
1fc76 70 4f 72 69 67 2d 3e 6f 70 21 3d 54 4b 5f 43 4f  pOrig->op!=TK_CO
1fc77 4c 55 4d 4e 20 26 26 20 7a 54 79 70 65 5b 30 5d  LUMN && zType[0]
1fc78 21 3d 27 47 27 20 29 7b 0a 20 20 20 20 70 44 75  !='G' ){.    pDu
1fc79 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  p = sqlite3ExprD
1fc7a 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29  up(db, pOrig, 0)
1fc7b 3b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c  ;.    pDup = sql
1fc7c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
1fc7d 2c 20 54 4b 5f 41 53 2c 20 70 44 75 70 2c 20 30  , TK_AS, pDup, 0
1fc7e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44  , 0);.    if( pD
1fc7f 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  up==0 ) return;.
1fc80 20 20 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e      if( pEList->
1fc81 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3d 3d  a[iCol].iAlias==
1fc82 30 20 29 7b 0a 20 20 20 20 20 20 70 45 4c 69 73  0 ){.      pELis
1fc83 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61  t->a[iCol].iAlia
1fc84 73 20 3d 20 28 75 31 36 29 28 2b 2b 70 50 61 72  s = (u16)(++pPar
1fc85 73 65 2d 3e 6e 41 6c 69 61 73 29 3b 0a 20 20 20  se->nAlias);.   
1fc86 20 7d 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61   }.    pDup->iTa
1fc87 62 6c 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b  ble = pEList->a[
1fc88 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20  iCol].iAlias;.  
1fc89 7d 65 6c 73 65 20 69 66 28 20 45 78 70 72 48 61  }else if( ExprHa
1fc8a 73 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c  sProperty(pOrig,
1fc8b 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c   EP_IntValue) ||
1fc8c 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e   pOrig->u.zToken
1fc8d 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 75 70 20  ==0 ){.    pDup 
1fc8e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
1fc8f 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29 3b 0a  (db, pOrig, 0);.
1fc90 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20      if( pDup==0 
1fc91 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73  ) return;.  }els
1fc92 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f  e{.    char *zTo
1fc93 6b 65 6e 20 3d 20 70 4f 72 69 67 2d 3e 75 2e 7a  ken = pOrig->u.z
1fc94 54 6f 6b 65 6e 3b 0a 20 20 20 20 61 73 73 65 72  Token;.    asser
1fc95 74 28 20 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a  t( zToken!=0 );.
1fc96 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f      pOrig->u.zTo
1fc97 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 70 44 75  ken = 0;.    pDu
1fc98 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  p = sqlite3ExprD
1fc99 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29  up(db, pOrig, 0)
1fc9a 3b 0a 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a  ;.    pOrig->u.z
1fc9b 54 6f 6b 65 6e 20 3d 20 7a 54 6f 6b 65 6e 3b 0a  Token = zToken;.
1fc9c 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20      if( pDup==0 
1fc9d 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 61 73  ) return;.    as
1fc9e 73 65 72 74 28 20 28 70 44 75 70 2d 3e 66 6c 61  sert( (pDup->fla
1fc9f 67 73 20 26 20 28 45 50 5f 52 65 64 75 63 65 64  gs & (EP_Reduced
1fca0 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 29 3d  |EP_TokenOnly))=
1fca1 3d 30 20 29 3b 0a 20 20 20 20 70 44 75 70 2d 3e  =0 );.    pDup->
1fca2 66 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 4d 61  flags2 |= EP2_Ma
1fca3 6c 6c 6f 63 65 64 54 6f 6b 65 6e 3b 0a 20 20 20  llocedToken;.   
1fca4 20 70 44 75 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20   pDup->u.zToken 
1fca5 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
1fca6 70 28 64 62 2c 20 7a 54 6f 6b 65 6e 29 3b 0a 20  p(db, zToken);. 
1fca7 20 7d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e   }.  if( pExpr->
1fca8 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
1fca9 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 70 44 75  llate ){.    pDu
1fcaa 70 2d 3e 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72  p->pColl = pExpr
1fcab 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75  ->pColl;.    pDu
1fcac 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45  p->flags |= EP_E
1fcad 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20  xpCollate;.  }. 
1fcae 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61   sqlite3ExprClea
1fcaf 72 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20  r(db, pExpr);.  
1fcb0 6d 65 6d 63 70 79 28 70 45 78 70 72 2c 20 70 44  memcpy(pExpr, pD
1fcb1 75 70 2c 20 73 69 7a 65 6f 66 28 2a 70 45 78 70  up, sizeof(*pExp
1fcb2 72 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  r));.  sqlite3Db
1fcb3 46 72 65 65 28 64 62 2c 20 70 44 75 70 29 3b 0a  Free(db, pDup);.
1fcb4 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74  }../*.** Given t
1fcb5 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c  he name of a col
1fcb6 75 6d 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  umn of the form 
1fcb7 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20  X.Y.Z or Y.Z or 
1fcb8 6a 75 73 74 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a  just Z, look up.
1fcb9 2a 2a 20 74 68 61 74 20 6e 61 6d 65 20 69 6e 20  ** that name in 
1fcba 74 68 65 20 73 65 74 20 6f 66 20 73 6f 75 72 63  the set of sourc
1fcbb 65 20 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63  e tables in pSrc
1fcbc 4c 69 73 74 20 61 6e 64 20 6d 61 6b 65 20 74 68  List and make th
1fcbd 65 20 70 45 78 70 72 20 0a 2a 2a 20 65 78 70 72  e pExpr .** expr
1fcbe 65 73 73 69 6f 6e 20 6e 6f 64 65 20 72 65 66 65  ession node refe
1fcbf 72 20 62 61 63 6b 20 74 6f 20 74 68 61 74 20 73  r back to that s
1fcc0 6f 75 72 63 65 20 63 6f 6c 75 6d 6e 2e 20 20 54  ource column.  T
1fcc1 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
1fcc2 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65  nges.** are made
1fcc3 20 74 6f 20 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a   to pExpr:.**.**
1fcc4 20 20 20 20 70 45 78 70 72 2d 3e 69 44 62 20 20      pExpr->iDb  
1fcc5 20 20 20 20 20 20 20 20 20 53 65 74 20 74 68 65           Set the
1fcc6 20 69 6e 64 65 78 20 69 6e 20 64 62 2d 3e 61 44   index in db->aD
1fcc7 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62  b[] of the datab
1fcc8 61 73 65 20 58 0a 2a 2a 20 20 20 20 20 20 20 20  ase X.**        
1fcc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcca 20 28 65 76 65 6e 20 69 66 20 58 20 69 73 20 69   (even if X is i
1fccb 6d 70 6c 69 65 64 29 2e 0a 2a 2a 20 20 20 20 70  mplied)..**    p
1fccc 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 20 20 20  Expr->iTable    
1fccd 20 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63      Set to the c
1fcce 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72  ursor number for
1fccf 20 74 68 65 20 74 61 62 6c 65 20 6f 62 74 61 69   the table obtai
1fcd0 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ned.**          
1fcd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
1fcd2 72 6f 6d 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a  rom pSrcList..**
1fcd3 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20      pExpr->pTab 
1fcd4 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 73 20           Points 
1fcd5 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  to the Table str
1fcd6 75 63 74 75 72 65 20 6f 66 20 58 2e 59 20 28 65  ucture of X.Y (e
1fcd7 76 65 6e 20 69 66 0a 2a 2a 20 20 20 20 20 20 20  ven if.**       
1fcd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcd9 20 20 58 20 61 6e 64 2f 6f 72 20 59 20 61 72 65    X and/or Y are
1fcda 20 69 6d 70 6c 69 65 64 2e 29 0a 2a 2a 20 20 20   implied.).**   
1fcdb 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
1fcdc 20 20 20 20 20 20 53 65 74 20 74 6f 20 74 68 65        Set to the
1fcdd 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77   column number w
1fcde 69 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ithin the table.
1fcdf 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  .**    pExpr->op
1fce0 20 20 20 20 20 20 20 20 20 20 20 20 53 65 74 20              Set 
1fce1 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a  to TK_COLUMN..**
1fce2 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74      pExpr->pLeft
1fce3 20 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70           Any exp
1fce4 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69  ression this poi
1fce5 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65  nts to is delete
1fce6 64 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70  d.**    pExpr->p
1fce7 52 69 67 68 74 20 20 20 20 20 20 20 20 41 6e 79  Right        Any
1fce8 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69 73   expression this
1fce9 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 65   points to is de
1fcea 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  leted..**.** The
1fceb 20 7a 44 62 20 76 61 72 69 61 62 6c 65 20 69 73   zDb variable is
1fcec 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1fced 20 64 61 74 61 62 61 73 65 20 28 74 68 65 20 22   database (the "
1fcee 58 22 29 2e 20 20 54 68 69 73 20 76 61 6c 75 65  X").  This value
1fcef 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 20   may be.** NULL 
1fcf0 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 6e 61 6d  meaning that nam
1fcf1 65 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  e is of the form
1fcf2 20 59 2e 5a 20 6f 72 20 5a 2e 20 20 41 6e 79 20   Y.Z or Z.  Any 
1fcf3 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 62 61  available databa
1fcf4 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  se.** can be use
1fcf5 64 2e 20 20 54 68 65 20 7a 54 61 62 6c 65 20 76  d.  The zTable v
1fcf6 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 6e  ariable is the n
1fcf7 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1fcf8 20 28 74 68 65 20 22 59 22 29 2e 20 20 54 68 69   (the "Y").  Thi
1fcf9 73 0a 2a 2a 20 76 61 6c 75 65 20 63 61 6e 20 62  s.** value can b
1fcfa 65 20 4e 55 4c 4c 20 69 66 20 7a 44 62 20 69 73  e NULL if zDb is
1fcfb 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20 20 49 66 20   also NULL.  If 
1fcfc 7a 54 61 62 6c 65 20 69 73 20 4e 55 4c 4c 20 69  zTable is NULL i
1fcfd 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20  t.** means that 
1fcfe 74 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20  the form of the 
1fcff 6e 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68  name is Z and th
1fd00 61 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20  at columns from 
1fd01 61 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e  any table.** can
1fd02 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   be used..**.** 
1fd03 49 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e  If the name cann
1fd04 6f 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75  ot be resolved u
1fd05 6e 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65  nambiguously, le
1fd06 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
1fd07 73 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73  sage.** in pPars
1fd08 65 20 61 6e 64 20 72 65 74 75 72 6e 20 57 52 43  e and return WRC
1fd09 5f 41 62 6f 72 74 2e 20 20 52 65 74 75 72 6e 20  _Abort.  Return 
1fd0a 57 52 43 5f 50 72 75 6e 65 20 6f 6e 20 73 75 63  WRC_Prune on suc
1fd0b 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cess..*/.static 
1fd0c 69 6e 74 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 0a  int lookupName(.
1fd0d 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1fd0e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
1fd0f 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
1fd10 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1fd11 44 62 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  Db,     /* Name 
1fd12 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1fd13 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65  containing table
1fd14 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63  , or NULL */.  c
1fd15 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c  onst char *zTab,
1fd16 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
1fd17 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
1fd18 63 6f 6c 75 6d 6e 2c 20 6f 72 20 4e 55 4c 4c 20  column, or NULL 
1fd19 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1fd1a 2a 7a 43 6f 6c 2c 20 20 20 20 2f 2a 20 4e 61 6d  *zCol,    /* Nam
1fd1b 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e  e of the column.
1fd1c 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
1fd1d 74 20 2a 70 4e 43 2c 20 20 20 20 2f 2a 20 54 68  t *pNC,    /* Th
1fd1e 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 75  e name context u
1fd1f 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74  sed to resolve t
1fd20 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70  he name */.  Exp
1fd21 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20  r *pExpr        
1fd22 20 20 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 45    /* Make this E
1fd23 58 50 52 20 6e 6f 64 65 20 70 6f 69 6e 74 20 74  XPR node point t
1fd24 6f 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 63  o the selected c
1fd25 6f 6c 75 6d 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e  olumn */.){.  in
1fd26 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
1fd27 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1fd28 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  ers */.  int cnt
1fd29 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1fd2a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1fd2b 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67  mber of matching
1fd2c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f   column names */
1fd2d 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 20 3d 20  .  int cntTab = 
1fd2e 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
1fd2f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1fd30 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65  f matching table
1fd31 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c 69   names */.  sqli
1fd32 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1fd33 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a  ->db;         /*
1fd34 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
1fd35 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74  nnection */.  st
1fd36 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
1fd37 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 20  m *pItem;       
1fd38 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 69  /* Use for loopi
1fd39 6e 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74  ng over pSrcList
1fd3a 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 75   items */.  stru
1fd3b 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
1fd3c 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f 2a  *pMatch = 0;  /*
1fd3d 20 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70 53   The matching pS
1fd3e 72 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a 20  rcList item */. 
1fd3f 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 54   NameContext *pT
1fd40 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 20  opNC = pNC;     
1fd41 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d 65     /* First name
1fd42 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 6c  context in the l
1fd43 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20  ist */.  Schema 
1fd44 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b 20 20 20  *pSchema = 0;   
1fd45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63             /* Sc
1fd46 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72  hema of the expr
1fd47 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ession */.  int 
1fd48 69 73 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 0a  isTrigger = 0;..
1fd49 20 20 61 73 73 65 72 74 28 20 70 4e 43 20 29 3b    assert( pNC );
1fd4a 20 20 20 20 20 2f 2a 20 74 68 65 20 6e 61 6d 65       /* the name
1fd4b 20 63 6f 6e 74 65 78 74 20 63 61 6e 6e 6f 74 20   context cannot 
1fd4c 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73  be NULL. */.  as
1fd4d 73 65 72 74 28 20 7a 43 6f 6c 20 29 3b 20 20 20  sert( zCol );   
1fd4e 20 2f 2a 20 54 68 65 20 5a 20 69 6e 20 58 2e 59   /* The Z in X.Y
1fd4f 2e 5a 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c  .Z cannot be NUL
1fd50 4c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7e  L */.  assert( ~
1fd51 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
1fd52 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b  ty(pExpr, EP_Tok
1fd53 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65  enOnly|EP_Reduce
1fd54 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74  d) );..  /* Init
1fd55 69 61 6c 69 7a 65 20 74 68 65 20 6e 6f 64 65 20  ialize the node 
1fd56 74 6f 20 6e 6f 2d 6d 61 74 63 68 20 2a 2f 0a 20  to no-match */. 
1fd57 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
1fd58 20 2d 31 3b 0a 20 20 70 45 78 70 72 2d 3e 70 54   -1;.  pExpr->pT
1fd59 61 62 20 3d 20 30 3b 0a 20 20 45 78 70 72 53 65  ab = 0;.  ExprSe
1fd5a 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78  tIrreducible(pEx
1fd5b 70 72 29 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74  pr);..  /* Start
1fd5c 20 61 74 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f   at the inner-mo
1fd5d 73 74 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 6d  st context and m
1fd5e 6f 76 65 20 6f 75 74 77 61 72 64 20 75 6e 74 69  ove outward unti
1fd5f 6c 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75  l a match is fou
1fd60 6e 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 70  nd */.  while( p
1fd61 4e 43 20 26 26 20 63 6e 74 3d 3d 30 20 29 7b 0a  NC && cnt==0 ){.
1fd62 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45      ExprList *pE
1fd63 4c 69 73 74 3b 0a 20 20 20 20 53 72 63 4c 69 73  List;.    SrcLis
1fd64 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e  t *pSrcList = pN
1fd65 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 0a 20 20  C->pSrcList;..  
1fd66 20 20 69 66 28 20 70 53 72 63 4c 69 73 74 20 29    if( pSrcList )
1fd67 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c  {.      for(i=0,
1fd68 20 70 49 74 65 6d 3d 70 53 72 63 4c 69 73 74 2d   pItem=pSrcList-
1fd69 3e 61 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e  >a; i<pSrcList->
1fd6a 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  nSrc; i++, pItem
1fd6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62  ++){.        Tab
1fd6c 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20  le *pTab;.      
1fd6d 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20    int iDb;.     
1fd6e 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b     Column *pCol;
1fd6f 0a 20 20 0a 20 20 20 20 20 20 20 20 70 54 61 62  .  .        pTab
1fd70 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a   = pItem->pTab;.
1fd71 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1fd72 70 54 61 62 21 3d 30 20 26 26 20 70 54 61 62 2d  pTab!=0 && pTab-
1fd73 3e 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 20  >zName!=0 );.   
1fd74 20 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74       iDb = sqlit
1fd75 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1fd76 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
1fd77 61 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  a);.        asse
1fd78 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30  rt( pTab->nCol>0
1fd79 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
1fd7a 7a 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20  zTab ){.        
1fd7b 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c    if( pItem->zAl
1fd7c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ias ){.         
1fd7d 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d     char *zTabNam
1fd7e 65 20 3d 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  e = pItem->zAlia
1fd7f 73 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  s;.            i
1fd80 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
1fd81 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62  p(zTabName, zTab
1fd82 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  )!=0 ) continue;
1fd83 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
1fd84 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  {.            ch
1fd85 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70  ar *zTabName = p
1fd86 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
1fd87 20 20 20 20 20 20 20 20 69 66 28 20 4e 45 56 45          if( NEVE
1fd88 52 28 7a 54 61 62 4e 61 6d 65 3d 3d 30 29 20 7c  R(zTabName==0) |
1fd89 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
1fd8a 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29  (zTabName, zTab)
1fd8b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
1fd8c 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
1fd8d 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1fd8e 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 44 62           if( zDb
1fd8f 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74  !=0 && sqlite3St
1fd90 72 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44  rICmp(db->aDb[iD
1fd91 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 21 3d  b].zName, zDb)!=
1fd92 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
1fd93 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
1fd94 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1fd95 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1fd96 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d  .        if( 0==
1fd97 28 63 6e 74 54 61 62 2b 2b 29 20 29 7b 0a 20 20  (cntTab++) ){.  
1fd98 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1fd99 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69  Table = pItem->i
1fd9a 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20  Cursor;.        
1fd9b 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20    pExpr->pTab = 
1fd9c 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20  pTab;.          
1fd9d 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e  pSchema = pTab->
1fd9e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20  pSchema;.       
1fd9f 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49 74 65     pMatch = pIte
1fda0 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  m;.        }.   
1fda1 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 43       for(j=0, pC
1fda2 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a  ol=pTab->aCol; j
1fda3 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  <pTab->nCol; j++
1fda4 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
1fda5 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1fda6 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e  StrICmp(pCol->zN
1fda7 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b  ame, zCol)==0 ){
1fda8 0a 20 20 20 20 20 20 20 20 20 20 20 20 49 64 4c  .            IdL
1fda9 69 73 74 20 2a 70 55 73 69 6e 67 3b 0a 20 20 20  ist *pUsing;.   
1fdaa 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a           cnt++;.
1fdab 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
1fdac 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65  r->iTable = pIte
1fdad 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20  m->iCursor;.    
1fdae 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1fdaf 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20  Tab = pTab;.    
1fdb0 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 3d          pMatch =
1fdb1 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20   pItem;.        
1fdb2 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 70 54      pSchema = pT
1fdb3 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20  ab->pSchema;.   
1fdb4 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 73           /* Subs
1fdb5 74 69 74 75 74 65 20 74 68 65 20 72 6f 77 69 64  titute the rowid
1fdb6 20 28 63 6f 6c 75 6d 6e 20 2d 31 29 20 66 6f 72   (column -1) for
1fdb7 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49   the INTEGER PRI
1fdb8 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 20 20  MARY KEY */.    
1fdb9 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1fdba 43 6f 6c 75 6d 6e 20 3d 20 6a 3d 3d 70 54 61 62  Column = j==pTab
1fdbb 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20 28  ->iPKey ? -1 : (
1fdbc 69 31 36 29 6a 3b 0a 20 20 20 20 20 20 20 20 20  i16)j;.         
1fdbd 20 20 20 69 66 28 20 69 3c 70 53 72 63 4c 69 73     if( i<pSrcLis
1fdbe 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 20 20  t->nSrc-1 ){.   
1fdbf 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
1fdc0 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65  Item[1].jointype
1fdc1 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b   & JT_NATURAL ){
1fdc2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1fdc3 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61 74 63   /* If this matc
1fdc4 68 20 6f 63 63 75 72 72 65 64 20 69 6e 20 74 68  h occurred in th
1fdc5 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20  e left table of 
1fdc6 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 6e 2c 0a  a natural join,.
1fdc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fdc8 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 74 68 65  ** then skip the
1fdc9 20 72 69 67 68 74 20 74 61 62 6c 65 20 74 6f 20   right table to 
1fdca 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63 61 74  avoid a duplicat
1fdcb 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 20  e match */.     
1fdcc 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
1fdcd 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ++;.            
1fdce 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20      i++;.       
1fdcf 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
1fdd0 20 28 70 55 73 69 6e 67 20 3d 20 70 49 74 65 6d   (pUsing = pItem
1fdd1 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d 30 20 29  [1].pUsing)!=0 )
1fdd2 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1fdd3 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61 74    /* If this mat
1fdd4 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 63  ch occurs on a c
1fdd5 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 69 6e  olumn that is in
1fdd6 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73   the USING claus
1fdd7 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e.              
1fdd8 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 6e 2c 20    ** of a join, 
1fdd9 73 6b 69 70 20 74 68 65 20 73 65 61 72 63 68 20  skip the search 
1fdda 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61 62  of the right tab
1fddb 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 0a 20  le of the join. 
1fddc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
1fddd 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70  * to avoid a dup
1fdde 6c 69 63 61 74 65 20 6d 61 74 63 68 20 74 68 65  licate match the
1fddf 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  re. */.         
1fde0 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20         int k;.  
1fde1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f                fo
1fde2 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 6e 67 2d  r(k=0; k<pUsing-
1fde3 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20  >nId; k++){.    
1fde4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
1fde5 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1fde6 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d 2e 7a 4e  (pUsing->a[k].zN
1fde7 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b  ame, zCol)==0 ){
1fde8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1fde9 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20       pItem++;.  
1fdea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fdeb 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
1fdec 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
1fded 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1fdee 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1fdef 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fdf0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fdf1 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1fdf2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1fdf3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1fdf4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69       }.    }..#i
1fdf5 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1fdf6 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 2f 2a  T_TRIGGER.    /*
1fdf7 20 49 66 20 77 65 20 68 61 76 65 20 6e 6f 74 20   If we have not 
1fdf8 61 6c 72 65 61 64 79 20 72 65 73 6f 6c 76 65 64  already resolved
1fdf9 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 65 6e 20   the name, then 
1fdfa 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a 20 69 74  maybe .    ** it
1fdfb 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f 72 20 6f   is a new.* or o
1fdfc 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 61 72 67  ld.* trigger arg
1fdfd 75 6d 65 6e 74 20 72 65 66 65 72 65 6e 63 65 0a  ument reference.
1fdfe 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a      */.    if( z
1fdff 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 21 3d 30  Db==0 && zTab!=0
1fe00 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 20 70 50   && cnt==0 && pP
1fe01 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
1fe02 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  b!=0 ){.      in
1fe03 74 20 6f 70 20 3d 20 70 50 61 72 73 65 2d 3e 65  t op = pParse->e
1fe04 54 72 69 67 67 65 72 4f 70 3b 0a 20 20 20 20 20  TriggerOp;.     
1fe05 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30   Table *pTab = 0
1fe06 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1fe07 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 7c 7c  op==TK_DELETE ||
1fe08 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 7c   op==TK_UPDATE |
1fe09 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20  | op==TK_INSERT 
1fe0a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 21  );.      if( op!
1fe0b 3d 54 4b 5f 44 45 4c 45 54 45 20 26 26 20 73 71  =TK_DELETE && sq
1fe0c 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6e 65  lite3StrICmp("ne
1fe0d 77 22 2c 7a 54 61 62 29 20 3d 3d 20 30 20 29 7b  w",zTab) == 0 ){
1fe0e 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e  .        pExpr->
1fe0f 69 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20  iTable = 1;.    
1fe10 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 73      pTab = pPars
1fe11 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b 0a  e->pTriggerTab;.
1fe12 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1fe13 6f 70 21 3d 54 4b 5f 49 4e 53 45 52 54 20 26 26  op!=TK_INSERT &&
1fe14 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1fe15 22 6f 6c 64 22 2c 7a 54 61 62 29 3d 3d 30 20 29  "old",zTab)==0 )
1fe16 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  {.        pExpr-
1fe17 3e 69 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20  >iTable = 0;.   
1fe18 20 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72       pTab = pPar
1fe19 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b  se->pTriggerTab;
1fe1a 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
1fe1b 69 66 28 20 70 54 61 62 20 29 7b 20 0a 20 20 20  if( pTab ){ .   
1fe1c 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20       int iCol;. 
1fe1d 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d         pSchema =
1fe1e 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
1fe1f 20 20 20 20 20 20 20 20 63 6e 74 54 61 62 2b 2b          cntTab++
1fe20 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  ;.        if( sq
1fe21 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43 6f  lite3IsRowid(zCo
1fe22 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  l) ){.          
1fe23 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20  iCol = -1;.     
1fe24 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1fe25 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
1fe26 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  iCol<pTab->nCol;
1fe27 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20   iCol++){.      
1fe28 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43        Column *pC
1fe29 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c  ol = &pTab->aCol
1fe2a 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 20 20  [iCol];.        
1fe2b 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1fe2c 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61  trICmp(pCol->zNa
1fe2d 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a  me, zCol)==0 ){.
1fe2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
1fe2f 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 69 50  ( iCol==pTab->iP
1fe30 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Key ){.         
1fe31 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31         iCol = -1
1fe32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1fe33 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
1fe34 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
1fe35 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
1fe36 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1fe37 20 20 20 69 66 28 20 69 43 6f 6c 3c 70 54 61 62     if( iCol<pTab
1fe38 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ->nCol ){.      
1fe39 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20      cnt++;.     
1fe3a 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20       if( iCol<0 
1fe3b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
1fe3c 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d  Expr->affinity =
1fe3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
1fe3e 47 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  GER;.          }
1fe3f 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e  else if( pExpr->
1fe40 69 54 61 62 6c 65 3d 3d 30 20 29 7b 0a 20 20 20  iTable==0 ){.   
1fe41 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73           testcas
1fe42 65 28 20 69 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20  e( iCol==31 );. 
1fe43 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63             testc
1fe44 61 73 65 28 20 69 43 6f 6c 3d 3d 33 32 20 29 3b  ase( iCol==32 );
1fe45 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61  .            pPa
1fe46 72 73 65 2d 3e 6f 6c 64 6d 61 73 6b 20 7c 3d 20  rse->oldmask |= 
1fe47 28 69 43 6f 6c 3e 3d 33 32 20 3f 20 30 78 66 66  (iCol>=32 ? 0xff
1fe48 66 66 66 66 66 66 20 3a 20 28 28 28 75 33 32 29  ffffff : (((u32)
1fe49 31 29 3c 3c 69 43 6f 6c 29 29 3b 0a 20 20 20 20  1)<<iCol));.    
1fe4a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fe4b 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e    pExpr->iColumn
1fe4c 20 3d 20 28 69 31 36 29 69 43 6f 6c 3b 0a 20 20   = (i16)iCol;.  
1fe4d 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1fe4e 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20  Tab = pTab;.    
1fe4f 20 20 20 20 20 20 69 73 54 72 69 67 67 65 72 20        isTrigger 
1fe50 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
1fe51 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
1fe52 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
1fe53 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1fe54 47 45 52 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a  GER) */..    /*.
1fe55 20 20 20 20 2a 2a 20 50 65 72 68 61 70 73 20 74      ** Perhaps t
1fe56 68 65 20 6e 61 6d 65 20 69 73 20 61 20 72 65 66  he name is a ref
1fe57 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 52 4f  erence to the RO
1fe58 57 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  WID.    */.    i
1fe59 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 63 6e 74  f( cnt==0 && cnt
1fe5a 54 61 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65  Tab==1 && sqlite
1fe5b 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29  3IsRowid(zCol) )
1fe5c 7b 0a 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b  {.      cnt = 1;
1fe5d 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43  .      pExpr->iC
1fe5e 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20  olumn = -1;.    
1fe5f 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74    pExpr->affinit
1fe60 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49  y = SQLITE_AFF_I
1fe61 4e 54 45 47 45 52 3b 0a 20 20 20 20 7d 0a 0a 20  NTEGER;.    }.. 
1fe62 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20     /*.    ** If 
1fe63 74 68 65 20 69 6e 70 75 74 20 69 73 20 6f 66 20  the input is of 
1fe64 74 68 65 20 66 6f 72 6d 20 5a 20 28 6e 6f 74 20  the form Z (not 
1fe65 59 2e 5a 20 6f 72 20 58 2e 59 2e 5a 29 20 74 68  Y.Z or X.Y.Z) th
1fe66 65 6e 20 74 68 65 20 6e 61 6d 65 20 5a 0a 20 20  en the name Z.  
1fe67 20 20 2a 2a 20 6d 69 67 68 74 20 72 65 66 65 72    ** might refer
1fe68 20 74 6f 20 61 6e 20 72 65 73 75 6c 74 2d 73 65   to an result-se
1fe69 74 20 61 6c 69 61 73 2e 20 20 54 68 69 73 20 68  t alias.  This h
1fe6a 61 70 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d  appens, for exam
1fe6b 70 6c 65 2c 20 77 68 65 6e 0a 20 20 20 20 2a 2a  ple, when.    **
1fe6c 20 77 65 20 61 72 65 20 72 65 73 6f 6c 76 69 6e   we are resolvin
1fe6d 67 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57  g names in the W
1fe6e 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74  HERE clause of t
1fe6f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d  he following com
1fe70 6d 61 6e 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  mand:.    **.   
1fe71 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61   **     SELECT a
1fe72 2b 62 20 41 53 20 78 20 46 52 4f 4d 20 74 61 62  +b AS x FROM tab
1fe73 6c 65 20 57 48 45 52 45 20 78 3c 31 30 3b 0a 20  le WHERE x<10;. 
1fe74 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 6e 20     **.    ** In 
1fe75 63 61 73 65 73 20 6c 69 6b 65 20 74 68 69 73 2c  cases like this,
1fe76 20 72 65 70 6c 61 63 65 20 70 45 78 70 72 20 77   replace pExpr w
1fe77 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ith a copy of th
1fe78 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  e expression tha
1fe79 74 0a 20 20 20 20 2a 2a 20 66 6f 72 6d 73 20 74  t.    ** forms t
1fe7a 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 65 6e  he result set en
1fe7b 74 72 79 20 28 22 61 2b 62 22 20 69 6e 20 74 68  try ("a+b" in th
1fe7c 65 20 65 78 61 6d 70 6c 65 29 20 61 6e 64 20 72  e example) and r
1fe7d 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c  eturn immediatel
1fe7e 79 2e 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74  y..    ** Note t
1fe7f 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73 69  hat the expressi
1fe80 6f 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  on in the result
1fe81 20 73 65 74 20 73 68 6f 75 6c 64 20 68 61 76 65   set should have
1fe82 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 20 20   already been.  
1fe83 20 20 2a 2a 20 72 65 73 6f 6c 76 65 64 20 62 79    ** resolved by
1fe84 20 74 68 65 20 74 69 6d 65 20 74 68 65 20 57 48   the time the WH
1fe85 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 72 65  ERE clause is re
1fe86 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  solved..    */. 
1fe87 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26     if( cnt==0 &&
1fe88 20 28 70 45 4c 69 73 74 20 3d 20 70 4e 43 2d 3e   (pEList = pNC->
1fe89 70 45 4c 69 73 74 29 21 3d 30 20 26 26 20 7a 54  pEList)!=0 && zT
1fe8a 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66  ab==0 ){.      f
1fe8b 6f 72 28 6a 3d 30 3b 20 6a 3c 70 45 4c 69 73 74  or(j=0; j<pEList
1fe8c 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20  ->nExpr; j++){. 
1fe8d 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73         char *zAs
1fe8e 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e   = pEList->a[j].
1fe8f 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69  zName;.        i
1fe90 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c  f( zAs!=0 && sql
1fe91 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c  ite3StrICmp(zAs,
1fe92 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20   zCol)==0 ){.   
1fe93 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4f 72         Expr *pOr
1fe94 69 67 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73  ig;.          as
1fe95 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65  sert( pExpr->pLe
1fe96 66 74 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e  ft==0 && pExpr->
1fe97 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20  pRight==0 );.   
1fe98 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1fe99 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30  Expr->x.pList==0
1fe9a 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   );.          as
1fe9b 73 65 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70  sert( pExpr->x.p
1fe9c 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20  Select==0 );.   
1fe9d 20 20 20 20 20 20 20 70 4f 72 69 67 20 3d 20 70         pOrig = p
1fe9e 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70  EList->a[j].pExp
1fe9f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  r;.          if(
1fea0 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20   !pNC->allowAgg 
1fea1 26 26 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  && ExprHasProper
1fea2 74 79 28 70 4f 72 69 67 2c 20 45 50 5f 41 67 67  ty(pOrig, EP_Agg
1fea3 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
1fea4 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1fea5 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65  (pParse, "misuse
1fea6 20 6f 66 20 61 6c 69 61 73 65 64 20 61 67 67 72   of aliased aggr
1fea7 65 67 61 74 65 20 25 73 22 2c 20 7a 41 73 29 3b  egate %s", zAs);
1fea8 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74  .            ret
1fea9 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1feaa 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1feab 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 61       resolveAlia
1feac 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  s(pParse, pEList
1fead 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 29 3b  , j, pExpr, "");
1feae 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 3d  .          cnt =
1feaf 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d   1;.          pM
1feb0 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20  atch = 0;.      
1feb1 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 62      assert( zTab
1feb2 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b  ==0 && zDb==0 );
1feb3 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1feb4 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3b 0a  lookupname_end;.
1feb5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1feb6 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  } .    }..    /*
1feb7 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20   Advance to the 
1feb8 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e 74 65 78  next name contex
1feb9 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 77 69 6c  t.  The loop wil
1feba 6c 20 65 78 69 74 20 77 68 65 6e 20 65 69 74 68  l exit when eith
1febb 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 68 61 76  er.    ** we hav
1febc 65 20 61 20 6d 61 74 63 68 20 28 63 6e 74 3e 30  e a match (cnt>0
1febd 29 20 6f 72 20 77 68 65 6e 20 77 65 20 72 75 6e  ) or when we run
1febe 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 63 6f 6e   out of name con
1febf 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  texts..    */.  
1fec0 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29 7b 0a    if( cnt==0 ){.
1fec1 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e 43 2d        pNC = pNC-
1fec2 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  >pNext;.    }.  
1fec3 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 20  }..  /*.  ** If 
1fec4 58 20 61 6e 64 20 59 20 61 72 65 20 4e 55 4c 4c  X and Y are NULL
1fec5 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73   (in other words
1fec6 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c   if only the col
1fec7 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 0a 20 20  umn name Z is.  
1fec8 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 61 6e 64  ** supplied) and
1fec9 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 5a 20   the value of Z 
1feca 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 64  is enclosed in d
1fecb 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c 20 74 68  ouble-quotes, th
1fecc 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 61 20 73  en.  ** Z is a s
1fecd 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 69 66  tring literal if
1fece 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61 74 63   it doesn't matc
1fecf 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d  h any column nam
1fed0 65 73 2e 20 20 49 6e 20 74 68 61 74 0a 20 20 2a  es.  In that.  *
1fed1 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 65 64 20  * case, we need 
1fed2 74 6f 20 72 65 74 75 72 6e 20 72 69 67 68 74 20  to return right 
1fed3 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d 61 6b  away and not mak
1fed4 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f  e any changes to
1fed5 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a 20 20 2a  .  ** pExpr..  *
1fed6 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 65 20 6e  *.  ** Because n
1fed7 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 73 20  o reference was 
1fed8 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20 63 6f  made to outer co
1fed9 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e 43 2d  ntexts, the pNC-
1feda 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 65 6c 64  >nRef.  ** field
1fedb 73 20 61 72 65 20 6e 6f 74 20 63 68 61 6e 67 65  s are not change
1fedc 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 65 78 74  d in any context
1fedd 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74  ..  */.  if( cnt
1fede 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 26  ==0 && zTab==0 &
1fedf 26 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  & ExprHasPropert
1fee0 79 28 70 45 78 70 72 2c 45 50 5f 44 62 6c 51 75  y(pExpr,EP_DblQu
1fee1 6f 74 65 64 29 20 29 7b 0a 20 20 20 20 70 45 78  oted) ){.    pEx
1fee2 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49  pr->op = TK_STRI
1fee3 4e 47 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70  NG;.    pExpr->p
1fee4 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  Tab = 0;.    ret
1fee5 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
1fee6 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 6e   }..  /*.  ** cn
1fee7 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 72 65  t==0 means there
1fee8 20 77 61 73 20 6e 6f 74 20 6d 61 74 63 68 2e 20   was not match. 
1fee9 20 63 6e 74 3e 31 20 6d 65 61 6e 73 20 74 68 65   cnt>1 means the
1feea 72 65 20 77 65 72 65 20 74 77 6f 20 6f 72 0a 20  re were two or. 
1feeb 20 2a 2a 20 6d 6f 72 65 20 6d 61 74 63 68 65 73   ** more matches
1feec 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 77  .  Either way, w
1feed 65 20 68 61 76 65 20 61 6e 20 65 72 72 6f 72 2e  e have an error.
1feee 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74 21  .  */.  if( cnt!
1feef 3d 31 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  =1 ){.    const 
1fef0 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20  char *zErr;.    
1fef1 7a 45 72 72 20 3d 20 63 6e 74 3d 3d 30 20 3f 20  zErr = cnt==0 ? 
1fef2 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 22  "no such column"
1fef3 20 3a 20 22 61 6d 62 69 67 75 6f 75 73 20 63 6f   : "ambiguous co
1fef4 6c 75 6d 6e 20 6e 61 6d 65 22 3b 0a 20 20 20 20  lumn name";.    
1fef5 69 66 28 20 7a 44 62 20 29 7b 0a 20 20 20 20 20  if( zDb ){.     
1fef6 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1fef7 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73  (pParse, "%s: %s
1fef8 2e 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a  .%s.%s", zErr, z
1fef9 44 62 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b  Db, zTab, zCol);
1fefa 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a  .    }else if( z
1fefb 54 61 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Tab ){.      sql
1fefc 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1fefd 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22  rse, "%s: %s.%s"
1fefe 2c 20 7a 45 72 72 2c 20 7a 54 61 62 2c 20 7a 43  , zErr, zTab, zC
1feff 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ol);.    }else{.
1ff00 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1ff01 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
1ff02 73 3a 20 25 73 22 2c 20 7a 45 72 72 2c 20 7a 43  s: %s", zErr, zC
1ff03 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ol);.    }.    p
1ff04 54 6f 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  TopNC->nErr++;. 
1ff05 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 63 6f   }..  /* If a co
1ff06 6c 75 6d 6e 20 66 72 6f 6d 20 61 20 74 61 62 6c  lumn from a tabl
1ff07 65 20 69 6e 20 70 53 72 63 4c 69 73 74 20 69 73  e in pSrcList is
1ff08 20 72 65 66 65 72 65 6e 63 65 64 2c 20 74 68 65   referenced, the
1ff09 6e 20 72 65 63 6f 72 64 0a 20 20 2a 2a 20 74 68  n record.  ** th
1ff0a 69 73 20 66 61 63 74 20 69 6e 20 74 68 65 20 70  is fact in the p
1ff0b 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 63 6f 6c 55  SrcList.a[].colU
1ff0c 73 65 64 20 62 69 74 6d 61 73 6b 2e 20 20 43 6f  sed bitmask.  Co
1ff0d 6c 75 6d 6e 20 30 20 63 61 75 73 65 73 0a 20 20  lumn 0 causes.  
1ff0e 2a 2a 20 62 69 74 20 30 20 74 6f 20 62 65 20 73  ** bit 0 to be s
1ff0f 65 74 2e 20 20 43 6f 6c 75 6d 6e 20 31 20 73 65  et.  Column 1 se
1ff10 74 73 20 62 69 74 20 31 2e 20 20 41 6e 64 20 73  ts bit 1.  And s
1ff11 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 65  o forth.  If the
1ff12 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d  .  ** column num
1ff13 62 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74  ber is greater t
1ff14 68 61 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  han the number o
1ff15 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 62 69  f bits in the bi
1ff16 74 6d 61 73 6b 0a 20 20 2a 2a 20 74 68 65 6e 20  tmask.  ** then 
1ff17 73 65 74 20 74 68 65 20 68 69 67 68 2d 6f 72 64  set the high-ord
1ff18 65 72 20 62 69 74 20 6f 66 20 74 68 65 20 62 69  er bit of the bi
1ff19 74 6d 61 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66  tmask..  */.  if
1ff1a 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ( pExpr->iColumn
1ff1b 3e 3d 30 20 26 26 20 70 4d 61 74 63 68 21 3d 30  >=0 && pMatch!=0
1ff1c 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20   ){.    int n = 
1ff1d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  pExpr->iColumn;.
1ff1e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d      testcase( n=
1ff1f 3d 42 4d 53 2d 31 20 29 3b 0a 20 20 20 20 69 66  =BMS-1 );.    if
1ff20 28 20 6e 3e 3d 42 4d 53 20 29 7b 0a 20 20 20 20  ( n>=BMS ){.    
1ff21 20 20 6e 20 3d 20 42 4d 53 2d 31 3b 0a 20 20 20    n = BMS-1;.   
1ff22 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
1ff23 4d 61 74 63 68 2d 3e 69 43 75 72 73 6f 72 3d 3d  Match->iCursor==
1ff24 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b  pExpr->iTable );
1ff25 0a 20 20 20 20 70 4d 61 74 63 68 2d 3e 63 6f 6c  .    pMatch->col
1ff26 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d 61 73  Used |= ((Bitmas
1ff27 6b 29 31 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a 20 20  k)1)<<n;.  }..  
1ff28 2f 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e 64 20  /* Clean up and 
1ff29 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71  return.  */.  sq
1ff2a 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1ff2b 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  db, pExpr->pLeft
1ff2c 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66  );.  pExpr->pLef
1ff2d 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  t = 0;.  sqlite3
1ff2e 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1ff2f 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20  Expr->pRight);. 
1ff30 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 3d   pExpr->pRight =
1ff31 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e 6f 70 20   0;.  pExpr->op 
1ff32 3d 20 28 69 73 54 72 69 67 67 65 72 20 3f 20 54  = (isTrigger ? T
1ff33 4b 5f 54 52 49 47 47 45 52 20 3a 20 54 4b 5f 43  K_TRIGGER : TK_C
1ff34 4f 4c 55 4d 4e 29 3b 0a 6c 6f 6f 6b 75 70 6e 61  OLUMN);.lookupna
1ff35 6d 65 5f 65 6e 64 3a 0a 20 20 69 66 28 20 63 6e  me_end:.  if( cn
1ff36 74 3d 3d 31 20 29 7b 0a 20 20 20 20 61 73 73 65  t==1 ){.    asse
1ff37 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20  rt( pNC!=0 );.  
1ff38 20 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61    sqlite3AuthRea
1ff39 64 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  d(pParse, pExpr,
1ff3a 20 70 53 63 68 65 6d 61 2c 20 70 4e 43 2d 3e 70   pSchema, pNC->p
1ff3b 53 72 63 4c 69 73 74 29 3b 0a 20 20 20 20 2f 2a  SrcList);.    /*
1ff3c 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 6e   Increment the n
1ff3d 52 65 66 20 76 61 6c 75 65 20 6f 6e 20 61 6c 6c  Ref value on all
1ff3e 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73 20 66   name contexts f
1ff3f 72 6f 6d 20 54 6f 70 4e 43 20 75 70 20 74 6f 0a  rom TopNC up to.
1ff40 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74      ** the point
1ff41 20 77 68 65 72 65 20 74 68 65 20 6e 61 6d 65 20   where the name 
1ff42 6d 61 74 63 68 65 64 2e 20 2a 2f 0a 20 20 20 20  matched. */.    
1ff43 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 61  for(;;){.      a
1ff44 73 73 65 72 74 28 20 70 54 6f 70 4e 43 21 3d 30  ssert( pTopNC!=0
1ff45 20 29 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43   );.      pTopNC
1ff46 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20  ->nRef++;.      
1ff47 69 66 28 20 70 54 6f 70 4e 43 3d 3d 70 4e 43 20  if( pTopNC==pNC 
1ff48 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70  ) break;.      p
1ff49 54 6f 70 4e 43 20 3d 20 70 54 6f 70 4e 43 2d 3e  TopNC = pTopNC->
1ff4a 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  pNext;.    }.   
1ff4b 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
1ff4c 65 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  e;.  } else {.  
1ff4d 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
1ff4e 72 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  rt;.  }.}../*.**
1ff4f 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65   Allocate and re
1ff50 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
1ff51 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  o an expression 
1ff52 74 6f 20 6c 6f 61 64 20 74 68 65 20 63 6f 6c 75  to load the colu
1ff53 6d 6e 20 69 43 6f 6c 0a 2a 2a 20 66 72 6f 6d 20  mn iCol.** from 
1ff54 64 61 74 61 73 6f 75 72 63 65 20 69 53 72 63 20  datasource iSrc 
1ff55 64 61 74 61 73 6f 75 72 63 65 20 69 6e 20 53 72  datasource in Sr
1ff56 63 4c 69 73 74 20 70 53 72 63 2e 0a 2a 2f 0a 53  cList pSrc..*/.S
1ff57 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
1ff58 70 72 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74  pr *sqlite3Creat
1ff59 65 43 6f 6c 75 6d 6e 45 78 70 72 28 73 71 6c 69  eColumnExpr(sqli
1ff5a 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74  te3 *db, SrcList
1ff5b 20 2a 70 53 72 63 2c 20 69 6e 74 20 69 53 72 63   *pSrc, int iSrc
1ff5c 2c 20 69 6e 74 20 69 43 6f 6c 29 7b 0a 20 20 45  , int iCol){.  E
1ff5d 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  xpr *p = sqlite3
1ff5e 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b  ExprAlloc(db, TK
1ff5f 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20 30 29 3b 0a  _COLUMN, 0, 0);.
1ff60 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73    if( p ){.    s
1ff61 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
1ff62 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72  em *pItem = &pSr
1ff63 63 2d 3e 61 5b 69 53 72 63 5d 3b 0a 20 20 20 20  c->a[iSrc];.    
1ff64 70 2d 3e 70 54 61 62 20 3d 20 70 49 74 65 6d 2d  p->pTab = pItem-
1ff65 3e 70 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 54  >pTab;.    p->iT
1ff66 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43  able = pItem->iC
1ff67 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70  ursor;.    if( p
1ff68 2d 3e 70 54 61 62 2d 3e 69 50 4b 65 79 3d 3d 69  ->pTab->iPKey==i
1ff69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  Col ){.      p->
1ff6a 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20  iColumn = -1;.  
1ff6b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
1ff6c 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56  ->iColumn = (ynV
1ff6d 61 72 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 70  ar)iCol;.      p
1ff6e 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d  Item->colUsed |=
1ff6f 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 28   ((Bitmask)1)<<(
1ff70 69 43 6f 6c 3e 3d 42 4d 53 20 3f 20 42 4d 53 2d  iCol>=BMS ? BMS-
1ff71 31 20 3a 20 69 43 6f 6c 29 3b 0a 20 20 20 20 7d  1 : iCol);.    }
1ff72 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70  .    ExprSetProp
1ff73 65 72 74 79 28 70 2c 20 45 50 5f 52 65 73 6f 6c  erty(p, EP_Resol
1ff74 76 65 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ved);.  }.  retu
1ff75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn p;.}../*.** T
1ff76 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
1ff77 61 6c 6c 62 61 63 6b 20 66 6f 72 20 73 71 6c 69  allback for sqli
1ff78 74 65 33 57 61 6c 6b 45 78 70 72 28 29 2e 0a 2a  te3WalkExpr()..*
1ff79 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 73 79 6d  *.** Resolve sym
1ff7a 62 6f 6c 69 63 20 6e 61 6d 65 73 20 69 6e 74 6f  bolic names into
1ff7b 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 72 61   TK_COLUMN opera
1ff7c 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63 75 72  tors for the cur
1ff7d 72 65 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69 6e 20  rent.** node in 
1ff7e 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
1ff7f 72 65 65 2e 20 20 52 65 74 75 72 6e 20 30 20 74  ree.  Return 0 t
1ff80 6f 20 63 6f 6e 74 69 6e 75 65 20 74 68 65 20 73  o continue the s
1ff81 65 61 72 63 68 20 64 6f 77 6e 0a 2a 2a 20 74 68  earch down.** th
1ff82 65 20 74 72 65 65 20 6f 72 20 32 20 74 6f 20 61  e tree or 2 to a
1ff83 62 6f 72 74 20 74 68 65 20 74 72 65 65 20 77 61  bort the tree wa
1ff84 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  lk..**.** This r
1ff85 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65 73  outine also does
1ff86 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20   error checking 
1ff87 61 6e 64 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74  and name resolut
1ff88 69 6f 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e 63 74  ion for.** funct
1ff89 69 6f 6e 20 6e 61 6d 65 73 2e 20 20 54 68 65 20  ion names.  The 
1ff8a 6f 70 65 72 61 74 6f 72 20 66 6f 72 20 61 67 67  operator for agg
1ff8b 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
1ff8c 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 74   is changed.** t
1ff8d 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  o TK_AGG_FUNCTIO
1ff8e 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  N..*/.static int
1ff8f 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70   resolveExprStep
1ff90 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
1ff91 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  , Expr *pExpr){.
1ff92 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
1ff93 4e 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  NC;.  Parse *pPa
1ff94 72 73 65 3b 0a 0a 20 20 70 4e 43 20 3d 20 70 57  rse;..  pNC = pW
1ff95 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20  alker->u.pNC;.  
1ff96 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29  assert( pNC!=0 )
1ff97 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 4e 43  ;.  pParse = pNC
1ff98 2d 3e 70 50 61 72 73 65 3b 0a 20 20 61 73 73 65  ->pParse;.  asse
1ff99 72 74 28 20 70 50 61 72 73 65 3d 3d 70 57 61 6c  rt( pParse==pWal
1ff9a 6b 65 72 2d 3e 70 50 61 72 73 65 20 29 3b 0a 0a  ker->pParse );..
1ff9b 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79    if( ExprHasAny
1ff9c 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
1ff9d 45 50 5f 52 65 73 6f 6c 76 65 64 29 20 29 20 72  EP_Resolved) ) r
1ff9e 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
1ff9f 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72  .  ExprSetProper
1ffa0 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73  ty(pExpr, EP_Res
1ffa1 6f 6c 76 65 64 29 3b 0a 23 69 66 6e 64 65 66 20  olved);.#ifndef 
1ffa2 4e 44 45 42 55 47 0a 20 20 69 66 28 20 70 4e 43  NDEBUG.  if( pNC
1ffa3 2d 3e 70 53 72 63 4c 69 73 74 20 26 26 20 70 4e  ->pSrcList && pN
1ffa4 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 41 6c  C->pSrcList->nAl
1ffa5 6c 6f 63 3e 30 20 29 7b 0a 20 20 20 20 53 72 63  loc>0 ){.    Src
1ffa6 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d  List *pSrcList =
1ffa7 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a   pNC->pSrcList;.
1ffa8 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
1ffa9 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 43 2d 3e 70  or(i=0; i<pNC->p
1ffaa 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  SrcList->nSrc; i
1ffab 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
1ffac 74 28 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69  t( pSrcList->a[i
1ffad 5d 2e 69 43 75 72 73 6f 72 3e 3d 30 20 26 26 20  ].iCursor>=0 && 
1ffae 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69  pSrcList->a[i].i
1ffaf 43 75 72 73 6f 72 3c 70 50 61 72 73 65 2d 3e 6e  Cursor<pParse->n
1ffb0 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Tab);.    }.  }.
1ffb1 23 65 6e 64 69 66 0a 20 20 73 77 69 74 63 68 28  #endif.  switch(
1ffb2 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 0a 23   pExpr->op ){..#
1ffb3 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
1ffb4 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f  E_ENABLE_UPDATE_
1ffb5 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26  DELETE_LIMIT) &&
1ffb6 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1ffb7 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a  _OMIT_SUBQUERY).
1ffb8 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69      /* The speci
1ffb9 61 6c 20 6f 70 65 72 61 74 6f 72 20 54 4b 5f 52  al operator TK_R
1ffba 4f 57 20 6d 65 61 6e 73 20 75 73 65 20 74 68 65  OW means use the
1ffbb 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 66   rowid for the f
1ffbc 69 72 73 74 0a 20 20 20 20 2a 2a 20 63 6f 6c 75  irst.    ** colu
1ffbd 6d 6e 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  mn in the FROM c
1ffbe 6c 61 75 73 65 2e 20 20 54 68 69 73 20 69 73 20  lause.  This is 
1ffbf 75 73 65 64 20 62 79 20 74 68 65 20 4c 49 4d 49  used by the LIMI
1ffc0 54 20 61 6e 64 20 4f 52 44 45 52 20 42 59 0a 20  T and ORDER BY. 
1ffc1 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 70 72 6f     ** clause pro
1ffc2 63 65 73 73 69 6e 67 20 6f 6e 20 55 50 44 41 54  cessing on UPDAT
1ffc3 45 20 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61  E and DELETE sta
1ffc4 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a  tements..    */.
1ffc5 20 20 20 20 63 61 73 65 20 54 4b 5f 52 4f 57 3a      case TK_ROW:
1ffc6 20 7b 0a 20 20 20 20 20 20 53 72 63 4c 69 73 74   {.      SrcList
1ffc7 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43   *pSrcList = pNC
1ffc8 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20  ->pSrcList;.    
1ffc9 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
1ffca 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
1ffcb 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63      assert( pSrc
1ffcc 4c 69 73 74 20 26 26 20 70 53 72 63 4c 69 73 74  List && pSrcList
1ffcd 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 20  ->nSrc==1 );.   
1ffce 20 20 20 70 49 74 65 6d 20 3d 20 70 53 72 63 4c     pItem = pSrcL
1ffcf 69 73 74 2d 3e 61 3b 20 0a 20 20 20 20 20 20 70  ist->a; .      p
1ffd0 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f  Expr->op = TK_CO
1ffd1 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 70 45 78 70  LUMN;.      pExp
1ffd2 72 2d 3e 70 54 61 62 20 3d 20 70 49 74 65 6d 2d  r->pTab = pItem-
1ffd3 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 70 45 78  >pTab;.      pEx
1ffd4 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74  pr->iTable = pIt
1ffd5 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20  em->iCursor;.   
1ffd6 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
1ffd7 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45  n = -1;.      pE
1ffd8 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20  xpr->affinity = 
1ffd9 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
1ffda 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ER;.      break;
1ffdb 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
1ffdc 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
1ffdd 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45  ENABLE_UPDATE_DE
1ffde 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21  LETE_LIMIT) && !
1ffdf 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1ffe0 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f  MIT_SUBQUERY) */
1ffe1 0a 0a 20 20 20 20 2f 2a 20 41 20 6c 6f 6e 65 20  ..    /* A lone 
1ffe2 69 64 65 6e 74 69 66 69 65 72 20 69 73 20 74 68  identifier is th
1ffe3 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75  e name of a colu
1ffe4 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  mn..    */.    c
1ffe5 61 73 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 20 20  ase TK_ID: {.   
1ffe6 20 20 20 72 65 74 75 72 6e 20 6c 6f 6f 6b 75 70     return lookup
1ffe7 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 30 2c 20  Name(pParse, 0, 
1ffe8 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  0, pExpr->u.zTok
1ffe9 65 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b  en, pNC, pExpr);
1ffea 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
1ffeb 20 41 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e   A table name an
1ffec 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20  d column name:  
1ffed 20 20 20 49 44 2e 49 44 0a 20 20 20 20 2a 2a 20     ID.ID.    ** 
1ffee 4f 72 20 61 20 64 61 74 61 62 61 73 65 2c 20 74  Or a database, t
1ffef 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a  able and column:
1fff0 20 20 49 44 2e 49 44 2e 49 44 0a 20 20 20 20 2a    ID.ID.ID.    *
1fff1 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 44 4f  /.    case TK_DO
1fff2 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  T: {.      const
1fff3 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 3b 0a   char *zColumn;.
1fff4 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1fff5 20 2a 7a 54 61 62 6c 65 3b 0a 20 20 20 20 20 20   *zTable;.      
1fff6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
1fff7 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 69  .      Expr *pRi
1fff8 67 68 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 69  ght;..      /* i
1fff9 66 28 20 70 53 72 63 4c 69 73 74 3d 3d 30 20 29  f( pSrcList==0 )
1fffa 20 62 72 65 61 6b 3b 20 2a 2f 0a 20 20 20 20 20   break; */.     
1fffb 20 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d   pRight = pExpr-
1fffc 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 69  >pRight;.      i
1fffd 66 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54  f( pRight->op==T
1fffe 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20  K_ID ){.        
1ffff 7a 44 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  zDb = 0;.       
20000 20 7a 54 61 62 6c 65 20 3d 20 70 45 78 70 72 2d   zTable = pExpr-
20001 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e  >pLeft->u.zToken
20002 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d  ;.        zColum
20003 6e 20 3d 20 70 52 69 67 68 74 2d 3e 75 2e 7a 54  n = pRight->u.zT
20004 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73  oken;.      }els
20005 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e{.        asser
20006 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54  t( pRight->op==T
20007 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20 20 20 20  K_DOT );.       
20008 20 7a 44 62 20 3d 20 70 45 78 70 72 2d 3e 70 4c   zDb = pExpr->pL
20009 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20  eft->u.zToken;. 
2000a 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20 3d 20         zTable = 
2000b 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d 3e 75  pRight->pLeft->u
2000c 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20  .zToken;.       
2000d 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67 68   zColumn = pRigh
2000e 74 2d 3e 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f  t->pRight->u.zTo
2000f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ken;.      }.   
20010 20 20 20 72 65 74 75 72 6e 20 6c 6f 6f 6b 75 70     return lookup
20011 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 44 62  Name(pParse, zDb
20012 2c 20 7a 54 61 62 6c 65 2c 20 7a 43 6f 6c 75 6d  , zTable, zColum
20013 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a  n, pNC, pExpr);.
20014 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
20015 73 6f 6c 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e  solve function n
20016 61 6d 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ames.    */.    
20017 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55  case TK_CONST_FU
20018 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  NC:.    case TK_
20019 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20  FUNCTION: {.    
2001a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2001b 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2001c 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 61 72  st;    /* The ar
2001d 67 75 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20  gument list */. 
2001e 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69       int n = pLi
2001f 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70  st ? pList->nExp
20020 72 20 3a 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d  r : 0;    /* Num
20021 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
20022 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 6f   */.      int no
20023 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 30 3b 20  _such_func = 0; 
20024 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
20025 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f   no such functio
20026 6e 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20  n exists */.    
20027 20 20 69 6e 74 20 77 72 6f 6e 67 5f 6e 75 6d 5f    int wrong_num_
20028 61 72 67 73 20 3d 20 30 3b 20 20 20 20 20 2f 2a  args = 0;     /*
20029 20 54 72 75 65 20 69 66 20 77 72 6f 6e 67 20 6e   True if wrong n
2002a 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
2002b 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ts */.      int 
2002c 69 73 5f 61 67 67 20 3d 20 30 3b 20 20 20 20 20  is_agg = 0;     
2002d 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2002e 69 66 20 69 73 20 61 6e 20 61 67 67 72 65 67 61  if is an aggrega
2002f 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  te function */. 
20030 20 20 20 20 20 69 6e 74 20 61 75 74 68 3b 20 20       int auth;  
20031 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20032 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f   /* Authorizatio
20033 6e 20 74 6f 20 75 73 65 20 74 68 65 20 66 75 6e  n to use the fun
20034 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69  ction */.      i
20035 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20 20 20  nt nId;         
20036 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
20037 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
20038 72 73 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 6e  rs in function n
20039 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e  ame */.      con
2003a 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 20 20  st char *zId;   
2003b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2003c 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e 20 2a  function name. *
2003d 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20  /.      FuncDef 
2003e 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 20 20  *pDef;          
2003f 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69      /* Informati
20040 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 75 6e  on about the fun
20041 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 75  ction */.      u
20042 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72  8 enc = ENC(pPar
20043 73 65 2d 3e 64 62 29 3b 20 20 20 2f 2a 20 54 68  se->db);   /* Th
20044 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64  e database encod
20045 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 20 20 74 65  ing */..      te
20046 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
20047 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43  p==TK_CONST_FUNC
20048 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
20049 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
2004a 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
2004b 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20  Select) );.     
2004c 20 7a 49 64 20 3d 20 70 45 78 70 72 2d 3e 75 2e   zId = pExpr->u.
2004d 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e 49  zToken;.      nI
2004e 64 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  d = sqlite3Strle
2004f 6e 33 30 28 7a 49 64 29 3b 0a 20 20 20 20 20 20  n30(zId);.      
20050 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69  pDef = sqlite3Fi
20051 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73  ndFunction(pPars
20052 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c  e->db, zId, nId,
20053 20 6e 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20   n, enc, 0);.   
20054 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29     if( pDef==0 )
20055 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d  {.        pDef =
20056 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
20057 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c  tion(pParse->db,
20058 20 7a 49 64 2c 20 6e 49 64 2c 20 2d 31 2c 20 65   zId, nId, -1, e
20059 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  nc, 0);.        
2005a 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20  if( pDef==0 ){. 
2005b 20 20 20 20 20 20 20 20 20 6e 6f 5f 73 75 63 68           no_such
2005c 5f 66 75 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20  _func = 1;.     
2005d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2005e 20 20 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72      wrong_num_ar
2005f 67 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  gs = 1;.        
20060 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
20061 20 20 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20         is_agg = 
20062 70 44 65 66 2d 3e 78 46 75 6e 63 3d 3d 30 3b 0a  pDef->xFunc==0;.
20063 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20        }.#ifndef 
20064 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
20065 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20 20  ORIZATION.      
20066 69 66 28 20 70 44 65 66 20 29 7b 0a 20 20 20 20  if( pDef ){.    
20067 20 20 20 20 61 75 74 68 20 3d 20 73 71 6c 69 74      auth = sqlit
20068 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
20069 73 65 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 54  se, SQLITE_FUNCT
2006a 49 4f 4e 2c 20 30 2c 20 70 44 65 66 2d 3e 7a 4e  ION, 0, pDef->zN
2006b 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ame, 0);.       
2006c 20 69 66 28 20 61 75 74 68 21 3d 53 51 4c 49 54   if( auth!=SQLIT
2006d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
2006e 20 20 69 66 28 20 61 75 74 68 3d 3d 53 51 4c 49    if( auth==SQLI
2006f 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 20  TE_DENY ){.     
20070 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
20071 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
20072 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 20 74  not authorized t
20073 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e 3a 20  o use function: 
20074 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  %s",.           
20075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20076 20 20 20 20 20 20 20 20 20 70 44 65 66 2d 3e 7a           pDef->z
20077 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Name);.         
20078 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a     pNC->nErr++;.
20079 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2007a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20        pExpr->op 
2007b 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20  = TK_NULL;.     
2007c 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
2007d 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 20 20 7d  Prune;.        }
2007e 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
2007f 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 67        if( is_agg
20080 20 26 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41   && !pNC->allowA
20081 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  gg ){.        sq
20082 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
20083 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66  arse, "misuse of
20084 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
20085 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64  ion %.*s()", nId
20086 2c 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70  ,zId);.        p
20087 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20  NC->nErr++;.    
20088 20 20 20 20 69 73 5f 61 67 67 20 3d 20 30 3b 0a      is_agg = 0;.
20089 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2008a 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 29 7b 0a  no_such_func ){.
2008b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2008c 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2008d 22 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f  "no such functio
2008e 6e 3a 20 25 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a  n: %.*s", nId, z
2008f 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43  Id);.        pNC
20090 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20  ->nErr++;.      
20091 7d 65 6c 73 65 20 69 66 28 20 77 72 6f 6e 67 5f  }else if( wrong_
20092 6e 75 6d 5f 61 72 67 73 20 29 7b 0a 20 20 20 20  num_args ){.    
20093 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
20094 4d 73 67 28 70 50 61 72 73 65 2c 22 77 72 6f 6e  Msg(pParse,"wron
20095 67 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  g number of argu
20096 6d 65 6e 74 73 20 74 6f 20 66 75 6e 63 74 69 6f  ments to functio
20097 6e 20 25 2e 2a 73 28 29 22 2c 0a 20 20 20 20 20  n %.*s()",.     
20098 20 20 20 20 20 20 20 20 6e 49 64 2c 20 7a 49 64          nId, zId
20099 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e  );.        pNC->
2009a 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  nErr++;.      }.
2009b 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 67        if( is_agg
2009c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70   ){.        pExp
2009d 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 46  r->op = TK_AGG_F
2009e 55 4e 43 54 49 4f 4e 3b 0a 20 20 20 20 20 20 20  UNCTION;.       
2009f 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31   pNC->hasAgg = 1
200a0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
200a1 69 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43  if( is_agg ) pNC
200a2 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a  ->allowAgg = 0;.
200a3 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c        sqlite3Wal
200a4 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65  kExprList(pWalke
200a5 72 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 20  r, pList);.     
200a6 20 69 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e   if( is_agg ) pN
200a7 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b  C->allowAgg = 1;
200a8 0a 20 20 20 20 20 20 2f 2a 20 46 49 58 20 4d 45  .      /* FIX ME
200a9 3a 20 20 43 6f 6d 70 75 74 65 20 70 45 78 70 72  :  Compute pExpr
200aa 2d 3e 61 66 66 69 6e 69 74 79 20 62 61 73 65 64  ->affinity based
200ab 20 6f 6e 20 74 68 65 20 65 78 70 65 63 74 65 64   on the expected
200ac 20 72 65 74 75 72 6e 0a 20 20 20 20 20 20 2a 2a   return.      **
200ad 20 74 79 70 65 20 6f 66 20 74 68 65 20 66 75 6e   type of the fun
200ae 63 74 69 6f 6e 20 0a 20 20 20 20 20 20 2a 2f 0a  ction .      */.
200af 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
200b0 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 23 69  _Prune;.    }.#i
200b1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
200b2 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 63  T_SUBQUERY.    c
200b3 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20  ase TK_SELECT:. 
200b4 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54     case TK_EXIST
200b5 53 3a 20 20 74 65 73 74 63 61 73 65 28 20 70 45  S:  testcase( pE
200b6 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53  xpr->op==TK_EXIS
200b7 54 53 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  TS );.#endif.   
200b8 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20   case TK_IN: {. 
200b9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
200ba 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20  Expr->op==TK_IN 
200bb 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45 78 70  );.      if( Exp
200bc 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
200bd 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
200be 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ) ){.        int
200bf 20 6e 52 65 66 20 3d 20 70 4e 43 2d 3e 6e 52 65   nRef = pNC->nRe
200c0 66 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f;.#ifndef SQLIT
200c1 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20  E_OMIT_CHECK.   
200c2 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73       if( pNC->is
200c3 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20  Check ){.       
200c4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
200c5 73 67 28 70 50 61 72 73 65 2c 22 73 75 62 71 75  sg(pParse,"subqu
200c6 65 72 69 65 73 20 70 72 6f 68 69 62 69 74 65 64  eries prohibited
200c7 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72   in CHECK constr
200c8 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 20  aints");.       
200c9 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
200ca 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c    sqlite3WalkSel
200cb 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78  ect(pWalker, pEx
200cc 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a  pr->x.pSelect);.
200cd 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
200ce 70 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20  pNC->nRef>=nRef 
200cf 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e  );.        if( n
200d0 52 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29  Ref!=pNC->nRef )
200d1 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72  {.          Expr
200d2 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70  SetProperty(pExp
200d3 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29  r, EP_VarSelect)
200d4 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
200d5 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
200d6 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
200d7 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b  QLITE_OMIT_CHECK
200d8 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52  .    case TK_VAR
200d9 49 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69  IABLE: {.      i
200da 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20  f( pNC->isCheck 
200db 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
200dc 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
200dd 65 2c 22 70 61 72 61 6d 65 74 65 72 73 20 70 72  e,"parameters pr
200de 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43  ohibited in CHEC
200df 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b  K constraints");
200e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
200e1 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
200e2 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  if.  }.  return 
200e3 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c  (pParse->nErr ||
200e4 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
200e5 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43  locFailed) ? WRC
200e6 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f 43 6f 6e  _Abort : WRC_Con
200e7 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tinue;.}../*.** 
200e8 70 45 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74  pEList is a list
200e9 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20   of expressions 
200ea 77 68 69 63 68 20 61 72 65 20 72 65 61 6c 6c 79  which are really
200eb 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
200ec 6f 66 20 74 68 65 0a 2a 2a 20 61 20 53 45 4c 45  of the.** a SELE
200ed 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 70  CT statement.  p
200ee 45 20 69 73 20 61 20 74 65 72 6d 20 69 6e 20 61  E is a term in a
200ef 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52  n ORDER BY or GR
200f0 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a  OUP BY clause..*
200f1 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
200f2 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20  hecks to see if 
200f3 70 45 20 69 73 20 61 20 73 69 6d 70 6c 65 20 69  pE is a simple i
200f4 64 65 6e 74 69 66 69 65 72 20 77 68 69 63 68 20  dentifier which 
200f5 63 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 74  corresponds.** t
200f6 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66  o the AS-name of
200f7 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 65 72 6d   one of the term
200f8 73 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  s of the express
200f9 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 69 74  ion list.  If it
200fa 20 69 73 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75   is,.** this rou
200fb 74 69 6e 65 20 72 65 74 75 72 6e 20 61 6e 20 69  tine return an i
200fc 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31  nteger between 1
200fd 20 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69   and N where N i
200fe 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  s the number of.
200ff 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70  ** elements in p
20100 45 4c 69 73 74 2c 20 63 6f 72 72 65 73 70 6f 6e  EList, correspon
20101 64 69 6e 67 20 74 6f 20 74 68 65 20 6d 61 74 63  ding to the matc
20102 68 69 6e 67 20 65 6e 74 72 79 2e 20 20 49 66 20  hing entry.  If 
20103 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d  there is.** no m
20104 61 74 63 68 2c 20 6f 72 20 69 66 20 70 45 20 69  atch, or if pE i
20105 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 69  s not a simple i
20106 64 65 6e 74 69 66 69 65 72 2c 20 74 68 65 6e 20  dentifier, then 
20107 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
20108 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20  return 0..**.** 
20109 70 45 4c 69 73 74 20 68 61 73 20 62 65 65 6e 20  pEList has been 
2010a 72 65 73 6f 6c 76 65 64 2e 20 20 70 45 20 68 61  resolved.  pE ha
2010b 73 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  s not..*/.static
2010c 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 73 4e 61   int resolveAsNa
2010d 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  me(.  Parse *pPa
2010e 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
2010f 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ing context for 
20110 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a  error messages *
20111 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
20112 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f  List,  /* List o
20113 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f  f expressions to
20114 20 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20   scan */.  Expr 
20115 2a 70 45 20 20 20 20 20 20 20 20 20 20 20 2f 2a  *pE           /*
20116 20 45 78 70 72 65 73 73 69 6f 6e 20 77 65 20 61   Expression we a
20117 72 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 74  re trying to mat
20118 63 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  ch */.){.  int i
20119 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2011a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2011b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
2011c 45 54 45 52 28 70 50 61 72 73 65 29 3b 0a 0a 20  ETER(pParse);.. 
2011d 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f   if( pE->op==TK_
2011e 49 44 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  ID ){.    char *
2011f 7a 43 6f 6c 20 3d 20 70 45 2d 3e 75 2e 7a 54 6f  zCol = pE->u.zTo
20120 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  ken;.    for(i=0
20121 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ; i<pEList->nExp
20122 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63  r; i++){.      c
20123 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73  har *zAs = pELis
20124 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20  t->a[i].zName;. 
20125 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20       if( zAs!=0 
20126 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
20127 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20  p(zAs, zCol)==0 
20128 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
20129 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20  n i+1;.      }. 
2012a 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2012b 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45  n 0;.}../*.** pE
2012c 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
2012d 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77   an expression w
2012e 68 69 63 68 20 69 73 20 61 20 73 69 6e 67 6c 65  hich is a single
2012f 20 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20   term in the.** 
20130 4f 52 44 45 52 20 42 59 20 6f 66 20 61 20 63 6f  ORDER BY of a co
20131 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 20 20  mpound SELECT.  
20132 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68  The expression h
20133 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 6e  as not been.** n
20134 61 6d 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a  ame resolved..**
20135 0a 2a 2a 20 41 74 20 74 68 65 20 70 6f 69 6e 74  .** At the point
20136 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
20137 20 63 61 6c 6c 65 64 2c 20 77 65 20 61 6c 72 65   called, we alre
20138 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 74 68  ady know that th
20139 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65  e.** ORDER BY te
2013a 72 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74  rm is not an int
2013b 65 67 65 72 20 69 6e 64 65 78 20 69 6e 74 6f 20  eger index into 
2013c 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20  the result set. 
2013d 20 54 68 61 74 0a 2a 2a 20 63 61 73 65 20 69 73   That.** case is
2013e 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20   handled by the 
2013f 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e  calling routine.
20140 0a 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  .**.** Attempt t
20141 6f 20 6d 61 74 63 68 20 70 45 20 61 67 61 69 6e  o match pE again
20142 73 74 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f  st result set co
20143 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6c 65 66  lumns in the lef
20144 74 2d 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45 43 54  t-most.** SELECT
20145 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74   statement.  Ret
20146 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 20  urn the index i 
20147 6f 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20  of the matching 
20148 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20 61 6e  column,.** as an
20149 20 69 6e 64 69 63 61 74 69 6f 6e 20 74 6f 20 74   indication to t
2014a 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20 69  he caller that i
2014b 74 20 73 68 6f 75 6c 64 20 73 6f 72 74 20 62 79  t should sort by
2014c 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e   the i-th column
2014d 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f  ..** The left-mo
2014e 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 31 2e 20  st column is 1. 
2014f 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
20150 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
20151 6e 65 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 61  ned is the.** sa
20152 6d 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  me integer value
20153 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 75   that would be u
20154 73 65 64 20 69 6e 20 74 68 65 20 53 51 4c 20 73  sed in the SQL s
20155 74 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 64 69  tatement to indi
20156 63 61 74 65 0a 2a 2a 20 74 68 65 20 63 6f 6c 75  cate.** the colu
20157 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  mn..**.** If the
20158 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20  re is no match, 
20159 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72  return 0.  Retur
2015a 6e 20 2d 31 20 69 66 20 61 6e 20 65 72 72 6f 72  n -1 if an error
2015b 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74   occurs..*/.stat
2015c 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72  ic int resolveOr
2015d 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c  derByTermToExprL
2015e 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ist(.  Parse *pP
2015f 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72  arse,     /* Par
20160 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72  sing context for
20161 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
20162 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
20163 6c 65 63 74 2c 20 20 20 2f 2a 20 54 68 65 20 53  lect,   /* The S
20164 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
20165 77 69 74 68 20 74 68 65 20 4f 52 44 45 52 20 42  with the ORDER B
20166 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  Y clause */.  Ex
20167 70 72 20 2a 70 45 20 20 20 20 20 20 20 20 20 20  pr *pE          
20168 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69 63   /* The specific
20169 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 2a   ORDER BY term *
2016a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
2016b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2016c 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45  p counter */.  E
2016d 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
2016e 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73    /* The columns
2016f 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
20170 65 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74  et */.  NameCont
20171 65 78 74 20 6e 63 3b 20 20 20 20 2f 2a 20 4e 61  ext nc;    /* Na
20172 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 72  me context for r
20173 65 73 6f 6c 76 69 6e 67 20 70 45 20 2a 2f 0a 0a  esolving pE */..
20174 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
20175 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70  3ExprIsInteger(p
20176 45 2c 20 26 69 29 3d 3d 30 20 29 3b 0a 20 20 70  E, &i)==0 );.  p
20177 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d  EList = pSelect-
20178 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52  >pEList;..  /* R
20179 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73  esolve all names
2017a 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59   in the ORDER BY
2017b 20 74 65 72 6d 20 65 78 70 72 65 73 73 69 6f 6e   term expression
2017c 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  .  */.  memset(&
2017d 6e 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6e 63  nc, 0, sizeof(nc
2017e 29 29 3b 0a 20 20 6e 63 2e 70 50 61 72 73 65 20  ));.  nc.pParse 
2017f 3d 20 70 50 61 72 73 65 3b 0a 20 20 6e 63 2e 70  = pParse;.  nc.p
20180 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65 63  SrcList = pSelec
20181 74 2d 3e 70 53 72 63 3b 0a 20 20 6e 63 2e 70 45  t->pSrc;.  nc.pE
20182 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20  List = pEList;. 
20183 20 6e 63 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31   nc.allowAgg = 1
20184 3b 0a 20 20 6e 63 2e 6e 45 72 72 20 3d 20 30 3b  ;.  nc.nErr = 0;
20185 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65  .  if( sqlite3Re
20186 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26  solveExprNames(&
20187 6e 63 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 73  nc, pE) ){.    s
20188 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72  qlite3ErrorClear
20189 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 72 65  (pParse);.    re
2018a 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 0;.  }..  /
2018b 2a 20 54 72 79 20 74 6f 20 6d 61 74 63 68 20 74  * Try to match t
2018c 68 65 20 4f 52 44 45 52 20 42 59 20 65 78 70 72  he ORDER BY expr
2018d 65 73 73 69 6f 6e 20 61 67 61 69 6e 73 74 20 61  ession against a
2018e 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a  n expression.  *
2018f 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  * in the result 
20190 73 65 74 2e 20 20 52 65 74 75 72 6e 20 61 6e 20  set.  Return an 
20191 31 2d 62 61 73 65 64 20 69 6e 64 65 78 20 6f 66  1-based index of
20192 20 74 68 65 20 6d 61 74 63 68 69 6e 67 0a 20 20   the matching.  
20193 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 6e  ** result-set en
20194 74 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  try..  */.  for(
20195 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e  i=0; i<pEList->n
20196 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
20197 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43  if( sqlite3ExprC
20198 6f 6d 70 61 72 65 28 70 45 4c 69 73 74 2d 3e 61  ompare(pEList->a
20199 5b 69 5d 2e 70 45 78 70 72 2c 20 70 45 29 20 29  [i].pExpr, pE) )
2019a 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  {.      return i
2019b 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  +1;.    }.  }.. 
2019c 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 2c   /* If no match,
2019d 20 72 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20   return 0. */.  
2019e 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
2019f 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 4f  ** Generate an O
201a0 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
201a1 20 42 59 20 74 65 72 6d 20 6f 75 74 2d 6f 66 2d   BY term out-of-
201a2 72 61 6e 67 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a  range error..*/.
201a3 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f  static void reso
201a4 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72  lveOutOfRangeErr
201a5 6f 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  or(.  Parse *pPa
201a6 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
201a7 54 68 65 20 65 72 72 6f 72 20 63 6f 6e 74 65 78  The error contex
201a8 74 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20  t into which to 
201a9 77 72 69 74 65 20 74 68 65 20 65 72 72 6f 72 20  write the error 
201aa 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
201ab 2a 7a 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22  *zType,     /* "
201ac 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50  ORDER" or "GROUP
201ad 22 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20  " */.  int i,   
201ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
201af 20 54 68 65 20 69 6e 64 65 78 20 28 31 2d 62 61   The index (1-ba
201b0 73 65 64 29 20 6f 66 20 74 68 65 20 74 65 72 6d  sed) of the term
201b1 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f   out of range */
201b2 0a 20 20 69 6e 74 20 6d 78 20 20 20 20 20 20 20  .  int mx       
201b3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72            /* Lar
201b4 67 65 73 74 20 70 65 72 6d 69 73 73 69 62 6c 65  gest permissible
201b5 20 76 61 6c 75 65 20 6f 66 20 69 20 2a 2f 0a 29   value of i */.)
201b6 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  {.  sqlite3Error
201b7 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20  Msg(pParse, .   
201b8 20 22 25 72 20 25 73 20 42 59 20 74 65 72 6d 20   "%r %s BY term 
201b9 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73  out of range - s
201ba 68 6f 75 6c 64 20 62 65 20 22 0a 20 20 20 20 22  hould be ".    "
201bb 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 25 64  between 1 and %d
201bc 22 2c 20 69 2c 20 7a 54 79 70 65 2c 20 6d 78 29  ", i, zType, mx)
201bd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79  ;.}../*.** Analy
201be 7a 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ze the ORDER BY 
201bf 63 6c 61 75 73 65 20 69 6e 20 61 20 63 6f 6d 70  clause in a comp
201c0 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74  ound SELECT stat
201c1 65 6d 65 6e 74 2e 20 20 20 4d 6f 64 69 66 79 0a  ement.   Modify.
201c2 2a 2a 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20  ** each term of 
201c3 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
201c4 75 73 65 20 69 73 20 61 20 63 6f 6e 73 74 61 6e  use is a constan
201c5 74 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65  t integer betwee
201c6 6e 20 31 0a 2a 2a 20 61 6e 64 20 4e 20 77 68 65  n 1.** and N whe
201c7 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62  re N is the numb
201c8 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
201c9 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45   the compound SE
201ca 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45  LECT..**.** ORDE
201cb 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20  R BY terms that 
201cc 61 72 65 20 61 6c 72 65 61 64 79 20 61 6e 20 69  are already an i
201cd 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31  nteger between 1
201ce 20 61 6e 64 20 4e 20 61 72 65 0a 2a 2a 20 75 6e   and N are.** un
201cf 6d 6f 64 69 66 69 65 64 2e 20 20 4f 52 44 45 52  modified.  ORDER
201d0 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61   BY terms that a
201d1 72 65 20 69 6e 74 65 67 65 72 73 20 6f 75 74 73  re integers outs
201d2 69 64 65 20 74 68 65 20 72 61 6e 67 65 20 6f 66  ide the range of
201d3 0a 2a 2a 20 31 20 74 68 72 6f 75 67 68 20 4e 20  .** 1 through N 
201d4 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f  generate an erro
201d5 72 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72  r.  ORDER BY ter
201d6 6d 73 20 74 68 61 74 20 61 72 65 20 65 78 70 72  ms that are expr
201d7 65 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d  essions.** are m
201d8 61 74 63 68 65 64 20 61 67 61 69 6e 73 74 20 72  atched against r
201d9 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73  esult set expres
201da 73 69 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f 75 6e  sions of compoun
201db 64 20 53 45 4c 45 43 54 0a 2a 2a 20 62 65 67 69  d SELECT.** begi
201dc 6e 6e 69 6e 67 20 77 69 74 68 20 74 68 65 20 6c  nning with the l
201dd 65 66 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20  eft-most SELECT 
201de 61 6e 64 20 77 6f 72 6b 69 6e 67 20 74 6f 77 61  and working towa
201df 72 64 20 74 68 65 20 72 69 67 68 74 2e 0a 2a 2a  rd the right..**
201e0 20 41 74 20 74 68 65 20 66 69 72 73 74 20 6d 61   At the first ma
201e1 74 63 68 2c 20 74 68 65 20 4f 52 44 45 52 20 42  tch, the ORDER B
201e2 59 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  Y expression is 
201e3 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f  transformed into
201e4 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20  .** the integer 
201e5 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a  column number..*
201e6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
201e7 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
201e8 20 73 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63   seen..*/.static
201e9 20 69 6e 74 20 72 65 73 6f 6c 76 65 43 6f 6d 70   int resolveComp
201ea 6f 75 6e 64 4f 72 64 65 72 42 79 28 0a 20 20 50  oundOrderBy(.  P
201eb 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
201ec 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
201ed 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20  context.  Leave 
201ee 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68  error messages h
201ef 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ere */.  Select 
201f0 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f  *pSelect       /
201f1 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
201f2 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e  tement containin
201f3 67 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 2a  g the ORDER BY *
201f4 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
201f5 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
201f6 42 79 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  By;.  ExprList *
201f7 70 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65  pEList;.  sqlite
201f8 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 6d 6f 72  3 *db;.  int mor
201f9 65 54 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f  eToDo = 1;..  pO
201fa 72 64 65 72 42 79 20 3d 20 70 53 65 6c 65 63 74  rderBy = pSelect
201fb 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66  ->pOrderBy;.  if
201fc 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20  ( pOrderBy==0 ) 
201fd 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d  return 0;.  db =
201fe 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66   pParse->db;.#if
201ff 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55   SQLITE_MAX_COLU
20200 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42  MN.  if( pOrderB
20201 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69  y->nExpr>db->aLi
20202 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
20203 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20  _COLUMN] ){.    
20204 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
20205 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e  pParse, "too man
20206 79 20 74 65 72 6d 73 20 69 6e 20 4f 52 44 45 52  y terms in ORDER
20207 20 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20   BY clause");.  
20208 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
20209 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d 30  #endif.  for(i=0
2020a 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45  ; i<pOrderBy->nE
2020b 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  xpr; i++){.    p
2020c 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f  OrderBy->a[i].do
2020d 6e 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 53  ne = 0;.  }.  pS
2020e 65 6c 65 63 74 2d 3e 70 4e 65 78 74 20 3d 20 30  elect->pNext = 0
2020f 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65  ;.  while( pSele
20210 63 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20  ct->pPrior ){.  
20211 20 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f    pSelect->pPrio
20212 72 2d 3e 70 4e 65 78 74 20 3d 20 70 53 65 6c 65  r->pNext = pSele
20213 63 74 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 20  ct;.    pSelect 
20214 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  = pSelect->pPrio
20215 72 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  r;.  }.  while( 
20216 70 53 65 6c 65 63 74 20 26 26 20 6d 6f 72 65 54  pSelect && moreT
20217 6f 44 6f 20 29 7b 0a 20 20 20 20 73 74 72 75 63  oDo ){.    struc
20218 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
20219 2a 70 49 74 65 6d 3b 0a 20 20 20 20 6d 6f 72 65  *pItem;.    more
2021a 54 6f 44 6f 20 3d 20 30 3b 0a 20 20 20 20 70 45  ToDo = 0;.    pE
2021b 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  List = pSelect->
2021c 70 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65  pEList;.    asse
2021d 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b  rt( pEList!=0 );
2021e 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49  .    for(i=0, pI
2021f 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b  tem=pOrderBy->a;
20220 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   i<pOrderBy->nEx
20221 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; i++, pItem++
20222 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f  ){.      int iCo
20223 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 45 78  l = -1;.      Ex
20224 70 72 20 2a 70 45 2c 20 2a 70 44 75 70 3b 0a 20  pr *pE, *pDup;. 
20225 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
20226 64 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  done ) continue;
20227 0a 20 20 20 20 20 20 70 45 20 3d 20 70 49 74 65  .      pE = pIte
20228 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  m->pExpr;.      
20229 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49  if( sqlite3ExprI
2022a 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43  sInteger(pE, &iC
2022b 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ol) ){.        i
2022c 66 28 20 69 43 6f 6c 3c 3d 30 20 7c 7c 20 69 43  f( iCol<=0 || iC
2022d 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  ol>pEList->nExpr
2022e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
2022f 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45  solveOutOfRangeE
20230 72 72 6f 72 28 70 50 61 72 73 65 2c 20 22 4f 52  rror(pParse, "OR
20231 44 45 52 22 2c 20 69 2b 31 2c 20 70 45 4c 69 73  DER", i+1, pELis
20232 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20  t->nExpr);.     
20233 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
20234 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
20235 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 43  else{.        iC
20236 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61  ol = resolveAsNa
20237 6d 65 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73  me(pParse, pELis
20238 74 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20  t, pE);.        
20239 69 66 28 20 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20  if( iCol==0 ){. 
2023a 20 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20           pDup = 
2023b 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2023c 62 2c 20 70 45 2c 20 30 29 3b 0a 20 20 20 20 20  b, pE, 0);.     
2023d 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61       if( !db->ma
2023e 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2023f 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
20240 28 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20  (pDup);.        
20241 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c      iCol = resol
20242 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 6f 45  veOrderByTermToE
20243 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  xprList(pParse, 
20244 70 53 65 6c 65 63 74 2c 20 70 44 75 70 29 3b 0a  pSelect, pDup);.
20245 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
20246 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
20247 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70  rDelete(db, pDup
20248 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
20249 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
2024a 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  Col>0 ){.       
2024b 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
2024c 3d 20 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  = pE->pColl;.   
2024d 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d       int flags =
2024e 20 70 45 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f   pE->flags & EP_
2024f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20  ExpCollate;.    
20250 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
20251 65 6c 65 74 65 28 64 62 2c 20 70 45 29 3b 0a 20  elete(db, pE);. 
20252 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45         pItem->pE
20253 78 70 72 20 3d 20 70 45 20 3d 20 73 71 6c 69 74  xpr = pE = sqlit
20254 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e  e3Expr(db, TK_IN
20255 54 45 47 45 52 2c 20 30 29 3b 0a 20 20 20 20 20  TEGER, 0);.     
20256 20 20 20 69 66 28 20 70 45 3d 3d 30 20 29 20 72     if( pE==0 ) r
20257 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20  eturn 1;.       
20258 20 70 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f   pE->pColl = pCo
20259 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e  ll;.        pE->
2025a 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56  flags |= EP_IntV
2025b 61 6c 75 65 20 7c 20 66 6c 61 67 73 3b 0a 20 20  alue | flags;.  
2025c 20 20 20 20 20 20 70 45 2d 3e 75 2e 69 56 61 6c        pE->u.iVal
2025d 75 65 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20  ue = iCol;.     
2025e 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d     pItem->iCol =
2025f 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20   (u16)iCol;.    
20260 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20      pItem->done 
20261 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 1;.      }else
20262 7b 0a 20 20 20 20 20 20 20 20 6d 6f 72 65 54 6f  {.        moreTo
20263 44 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  Do = 1;.      }.
20264 20 20 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63      }.    pSelec
20265 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65  t = pSelect->pNe
20266 78 74 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  xt;.  }.  for(i=
20267 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e  0; i<pOrderBy->n
20268 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
20269 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  if( pOrderBy->a[
2026a 69 5d 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20  i].done==0 ){.  
2026b 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2026c 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 72 20  Msg(pParse, "%r 
2026d 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 64 6f  ORDER BY term do
2026e 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79  es not match any
2026f 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
20270 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65  column in the re
20271 73 75 6c 74 20 73 65 74 22 2c 20 69 2b 31 29 3b  sult set", i+1);
20272 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
20273 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
20274 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
20275 43 68 65 63 6b 20 65 76 65 72 79 20 74 65 72 6d  Check every term
20276 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59   in the ORDER BY
20277 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61   or GROUP BY cla
20278 75 73 65 20 70 4f 72 64 65 72 42 79 20 6f 66 0a  use pOrderBy of.
20279 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  ** the SELECT st
2027a 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e  atement pSelect.
2027b 20 20 49 66 20 61 6e 79 20 74 65 72 6d 20 69 73    If any term is
2027c 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 0a   reference to a.
2027d 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 65 78  ** result set ex
2027e 70 72 65 73 73 69 6f 6e 20 28 61 73 20 64 65 74  pression (as det
2027f 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 45  ermined by the E
20280 78 70 72 4c 69 73 74 2e 61 2e 69 43 6f 6c 20 66  xprList.a.iCol f
20281 69 65 6c 64 29 0a 2a 2a 20 74 68 65 6e 20 63 6f  ield).** then co
20282 6e 76 65 72 74 20 74 68 61 74 20 74 65 72 6d 20  nvert that term 
20283 69 6e 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74  into a copy of t
20284 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
20285 20 72 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20 63   result set.** c
20286 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  olumn..**.** If 
20287 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65 20 64  any errors are d
20288 65 74 65 63 74 65 64 2c 20 61 64 64 20 61 6e 20  etected, add an 
20289 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f  error message to
2028a 20 70 50 61 72 73 65 20 61 6e 64 0a 2a 2a 20 72   pParse and.** r
2028b 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  eturn non-zero. 
2028c 20 52 65 74 75 72 6e 20 7a 65 72 6f 20 69 66 20   Return zero if 
2028d 6e 6f 20 65 72 72 6f 72 73 20 61 72 65 20 73 65  no errors are se
2028e 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  en..*/.SQLITE_PR
2028f 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
20290 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f  3ResolveOrderGro
20291 75 70 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70  upBy(.  Parse *p
20292 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
20293 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
20294 2e 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d  .  Leave error m
20295 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a  essages here */.
20296 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
20297 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53  t,      /* The S
20298 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
20299 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63  containing the c
2029a 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c  lause */.  ExprL
2029b 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
2029c 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
2029d 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61   or GROUP BY cla
2029e 75 73 65 20 74 6f 20 62 65 20 70 72 6f 63 65 73  use to be proces
2029f 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  sed */.  const c
202a0 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20 20 2f  har *zType     /
202a1 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52  * "ORDER" or "GR
202a2 4f 55 50 22 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  OUP" */.){.  int
202a3 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   i;.  sqlite3 *d
202a4 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
202a5 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
202a6 73 74 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70  st;.  struct Exp
202a7 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
202a8 6d 3b 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72  m;..  if( pOrder
202a9 42 79 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d  By==0 || pParse-
202aa 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
202ab 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69  d ) return 0;.#i
202ac 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c  f SQLITE_MAX_COL
202ad 55 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72  UMN.  if( pOrder
202ae 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c  By->nExpr>db->aL
202af 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
202b0 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20  T_COLUMN] ){.   
202b1 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
202b2 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
202b3 6e 79 20 74 65 72 6d 73 20 69 6e 20 25 73 20 42  ny terms in %s B
202b4 59 20 63 6c 61 75 73 65 22 2c 20 7a 54 79 70 65  Y clause", zType
202b5 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
202b6 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 45  .  }.#endif.  pE
202b7 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  List = pSelect->
202b8 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74  pEList;.  assert
202b9 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 20 20  ( pEList!=0 );  
202ba 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  /* sqlite3Select
202bb 4e 65 77 28 29 20 67 75 61 72 61 6e 74 65 65 73  New() guarantees
202bc 20 74 68 69 73 20 2a 2f 0a 20 20 66 6f 72 28 69   this */.  for(i
202bd 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72  =0, pItem=pOrder
202be 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42  By->a; i<pOrderB
202bf 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70  y->nExpr; i++, p
202c0 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  Item++){.    if(
202c1 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a   pItem->iCol ){.
202c2 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
202c3 3e 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45  >iCol>pEList->nE
202c4 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72  xpr ){.        r
202c5 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65  esolveOutOfRange
202c6 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54  Error(pParse, zT
202c7 79 70 65 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74  ype, i+1, pEList
202c8 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20  ->nExpr);.      
202c9 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
202ca 20 20 7d 0a 20 20 20 20 20 20 72 65 73 6f 6c 76    }.      resolv
202cb 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70  eAlias(pParse, p
202cc 45 4c 69 73 74 2c 20 70 49 74 65 6d 2d 3e 69 43  EList, pItem->iC
202cd 6f 6c 2d 31 2c 20 70 49 74 65 6d 2d 3e 70 45 78  ol-1, pItem->pEx
202ce 70 72 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20  pr, zType);.    
202cf 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
202d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65  ;.}../*.** pOrde
202d1 72 42 79 20 69 73 20 61 6e 20 4f 52 44 45 52 20  rBy is an ORDER 
202d2 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63  BY or GROUP BY c
202d3 6c 61 75 73 65 20 69 6e 20 53 45 4c 45 43 54 20  lause in SELECT 
202d4 73 74 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63  statement pSelec
202d5 74 2e 0a 2a 2a 20 54 68 65 20 4e 61 6d 65 20 63  t..** The Name c
202d6 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53 45  ontext of the SE
202d7 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69  LECT statement i
202d8 73 20 70 4e 43 2e 20 20 7a 54 79 70 65 20 69 73  s pNC.  zType is
202d9 20 65 69 74 68 65 72 0a 2a 2a 20 22 4f 52 44 45   either.** "ORDE
202da 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 64 65  R" or "GROUP" de
202db 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63 68  pending on which
202dc 20 74 79 70 65 20 6f 66 20 63 6c 61 75 73 65 20   type of clause 
202dd 70 4f 72 64 65 72 42 79 20 69 73 2e 0a 2a 2a 0a  pOrderBy is..**.
202de 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
202df 72 65 73 6f 6c 76 65 73 20 65 61 63 68 20 74 65  resolves each te
202e0 72 6d 20 6f 66 20 74 68 65 20 63 6c 61 75 73 65  rm of the clause
202e1 20 69 6e 74 6f 20 61 6e 20 65 78 70 72 65 73 73   into an express
202e2 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f  ion..** If the o
202e3 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73 20  rder-by term is 
202e4 61 6e 20 69 6e 74 65 67 65 72 20 49 20 62 65 74  an integer I bet
202e5 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 28 77 68  ween 1 and N (wh
202e6 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20  ere N is the.** 
202e7 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
202e8 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
202e9 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43  set of the SELEC
202ea 54 29 20 74 68 65 6e 20 74 68 65 20 65 78 70 72  T) then the expr
202eb 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65  ession.** in the
202ec 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 61   resolution is a
202ed 20 63 6f 70 79 20 6f 66 20 74 68 65 20 49 2d 74   copy of the I-t
202ee 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70  h result-set exp
202ef 72 65 73 73 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20  ression.  If.** 
202f0 74 68 65 20 6f 72 64 65 72 2d 62 79 20 74 65 72  the order-by ter
202f1 6d 20 69 73 20 61 6e 20 69 64 65 6e 74 69 66 79  m is an identify
202f2 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64   that correspond
202f3 73 20 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65  s to the AS-name
202f4 20 6f 66 0a 2a 2a 20 61 20 72 65 73 75 6c 74 2d   of.** a result-
202f5 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2c 20  set expression, 
202f6 74 68 65 6e 20 74 68 65 20 74 65 72 6d 20 72 65  then the term re
202f7 73 6f 6c 76 65 73 20 74 6f 20 61 20 63 6f 70 79  solves to a copy
202f8 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c   of the.** resul
202f9 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t-set expression
202fa 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68  .  Otherwise, th
202fb 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
202fc 72 65 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a 20 74  resolved in.** t
202fd 68 65 20 75 73 75 61 6c 20 77 61 79 20 2d 20 75  he usual way - u
202fe 73 69 6e 67 20 73 71 6c 69 74 65 33 52 65 73 6f  sing sqlite3Reso
202ff 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a  lveExprNames()..
20300 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
20301 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ne returns the n
20302 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e  umber of errors.
20303 20 20 49 66 20 65 72 72 6f 72 73 20 6f 63 63 75    If errors occu
20304 72 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 61 70  r, then.** an ap
20305 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20  propriate error 
20306 6d 65 73 73 61 67 65 20 6d 69 67 68 74 20 62 65  message might be
20307 20 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e   left in pParse.
20308 20 20 28 4f 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a    (OOM errors.**
20309 20 65 78 63 65 70 74 65 64 2e 29 0a 2a 2f 0a 73   excepted.).*/.s
2030a 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76  tatic int resolv
2030b 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20  eOrderGroupBy(. 
2030c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e   NameContext *pN
2030d 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61  C,     /* The na
2030e 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68  me context of th
2030f 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
20310 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  nt */.  Select *
20311 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a  pSelect,      /*
20312 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
20313 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 70 4f  ement holding pO
20314 72 64 65 72 42 79 20 2a 2f 0a 20 20 45 78 70 72  rderBy */.  Expr
20315 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
20316 20 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59    /* An ORDER BY
20317 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61   or GROUP BY cla
20318 75 73 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 2a  use to resolve *
20319 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2031a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 45 69 74  zType     /* Eit
2031b 68 65 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22  her "ORDER" or "
2031c 47 52 4f 55 50 22 2c 20 61 73 20 61 70 70 72 6f  GROUP", as appro
2031d 70 72 69 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 69  priate */.){.  i
2031e 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
2031f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20320 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
20321 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20  .  int iCol;    
20322 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20323 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62    /* Column numb
20324 65 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45  er */.  struct E
20325 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
20326 74 65 6d 3b 20 20 20 2f 2a 20 41 20 74 65 72 6d  tem;   /* A term
20327 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
20328 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 50 61 72   clause */.  Par
20329 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
2032a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2032b 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2032c 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 3b  /.  int nResult;
2032d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2032e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2032f 74 65 72 6d 73 20 69 6e 20 74 68 65 20 72 65 73  terms in the res
20330 75 6c 74 20 73 65 74 20 2a 2f 0a 0a 20 20 69 66  ult set */..  if
20331 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20  ( pOrderBy==0 ) 
20332 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 52 65 73  return 0;.  nRes
20333 75 6c 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ult = pSelect->p
20334 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  EList->nExpr;.  
20335 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
20336 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  arse;.  for(i=0,
20337 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d   pItem=pOrderBy-
20338 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e  >a; i<pOrderBy->
20339 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
2033a 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a  m++){.    Expr *
2033b 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70  pE = pItem->pExp
2033c 72 3b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 72 65  r;.    iCol = re
2033d 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72  solveAsName(pPar
2033e 73 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  se, pSelect->pEL
2033f 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20 69 66  ist, pE);.    if
20340 28 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20  ( iCol>0 ){.    
20341 20 20 2f 2a 20 49 66 20 61 6e 20 41 53 2d 6e 61    /* If an AS-na
20342 6d 65 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e  me match is foun
20343 64 2c 20 6d 61 72 6b 20 74 68 69 73 20 4f 52 44  d, mark this ORD
20344 45 52 20 42 59 20 63 6f 6c 75 6d 6e 20 61 73 20  ER BY column as 
20345 62 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61  being.      ** a
20346 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f   copy of the iCo
20347 6c 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20  l-th result-set 
20348 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 73 75 62  column.  The sub
20349 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a  sequent call to.
2034a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33        ** sqlite3
2034b 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75  ResolveOrderGrou
2034c 70 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65  pBy() will conve
2034d 72 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  rt the expressio
2034e 6e 20 74 6f 20 61 0a 20 20 20 20 20 20 2a 2a 20  n to a.      ** 
2034f 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c  copy of the iCol
20350 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65  -th result-set e
20351 78 70 72 65 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20  xpression. */.  
20352 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20      pItem->iCol 
20353 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20  = (u16)iCol;.   
20354 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
20355 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
20356 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
20357 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20  pE, &iCol) ){.  
20358 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
20359 20 42 59 20 74 65 72 6d 20 69 73 20 61 6e 20 69   BY term is an i
2035a 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e  nteger constant.
2035b 20 20 41 67 61 69 6e 2c 20 73 65 74 20 74 68 65    Again, set the
2035c 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a   column.      **
2035d 20 6e 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20   number so that 
2035e 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72  sqlite3ResolveOr
2035f 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c  derGroupBy() wil
20360 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 0a 20 20  l convert the.  
20361 20 20 20 20 2a 2a 20 6f 72 64 65 72 2d 62 79 20      ** order-by 
20362 74 65 72 6d 20 74 6f 20 61 20 63 6f 70 79 20 6f  term to a copy o
20363 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74  f the result-set
20364 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
20365 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 31 20       if( iCol<1 
20366 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c  ){.        resol
20367 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f  veOutOfRangeErro
20368 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c  r(pParse, zType,
20369 20 69 2b 31 2c 20 6e 52 65 73 75 6c 74 29 3b 0a   i+1, nResult);.
2036a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
2036b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2036c 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75  pItem->iCol = (u
2036d 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63  16)iCol;.      c
2036e 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a  ontinue;.    }..
2036f 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65      /* Otherwise
20370 2c 20 74 72 65 61 74 20 74 68 65 20 4f 52 44 45  , treat the ORDE
20371 52 20 42 59 20 74 65 72 6d 20 61 73 20 61 6e 20  R BY term as an 
20372 6f 72 64 69 6e 61 72 79 20 65 78 70 72 65 73 73  ordinary express
20373 69 6f 6e 20 2a 2f 0a 20 20 20 20 70 49 74 65 6d  ion */.    pItem
20374 2d 3e 69 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->iCol = 0;.    
20375 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  if( sqlite3Resol
20376 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e 43 2c  veExprNames(pNC,
20377 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65   pE) ){.      re
20378 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
20379 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
2037a 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72  e3ResolveOrderGr
2037b 6f 75 70 42 79 28 70 50 61 72 73 65 2c 20 70 53  oupBy(pParse, pS
2037c 65 6c 65 63 74 2c 20 70 4f 72 64 65 72 42 79 2c  elect, pOrderBy,
2037d 20 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   zType);.}../*.*
2037e 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20  * Resolve names 
2037f 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  in the SELECT st
20380 61 74 65 6d 65 6e 74 20 70 20 61 6e 64 20 61 6c  atement p and al
20381 6c 20 6f 66 20 69 74 73 20 64 65 73 63 65 6e 64  l of its descend
20382 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ents..*/.static 
20383 69 6e 74 20 72 65 73 6f 6c 76 65 53 65 6c 65 63  int resolveSelec
20384 74 53 74 65 70 28 57 61 6c 6b 65 72 20 2a 70 57  tStep(Walker *pW
20385 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70  alker, Select *p
20386 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ){.  NameContext
20387 20 2a 70 4f 75 74 65 72 4e 43 3b 20 20 2f 2a 20   *pOuterNC;  /* 
20388 43 6f 6e 74 65 78 74 20 74 68 61 74 20 63 6f 6e  Context that con
20389 74 61 69 6e 73 20 74 68 69 73 20 53 45 4c 45 43  tains this SELEC
2038a 54 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  T */.  NameConte
2038b 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 20 2f  xt sNC;        /
2038c 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f  * Name context o
2038d 66 20 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f  f this SELECT */
2038e 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70 6f 75 6e  .  int isCompoun
2038f 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  d;         /* Tr
20390 75 65 20 69 66 20 70 20 69 73 20 61 20 63 6f 6d  ue if p is a com
20391 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 2a 2f 0a  pound select */.
20392 20 20 69 6e 74 20 6e 43 6f 6d 70 6f 75 6e 64 3b    int nCompound;
20393 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
20394 62 65 72 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20  ber of compound 
20395 74 65 72 6d 73 20 70 72 6f 63 65 73 73 65 64 20  terms processed 
20396 73 6f 20 66 61 72 20 2a 2f 0a 20 20 50 61 72 73  so far */.  Pars
20397 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
20398 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
20399 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
2039a 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20  List *pEList;   
2039b 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 73 65      /* Result se
2039c 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  t expression lis
2039d 74 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  t */.  int i;   
2039e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2039f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
203a0 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47  /.  ExprList *pG
203a1 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a 20 54  roupBy;     /* T
203a2 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  he GROUP BY clau
203a3 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  se */.  Select *
203a4 70 4c 65 66 74 6d 6f 73 74 3b 20 20 20 20 20 20  pLeftmost;      
203a5 2f 2a 20 4c 65 66 74 2d 6d 6f 73 74 20 6f 66 20  /* Left-most of 
203a6 53 45 4c 45 43 54 20 6f 66 20 61 20 63 6f 6d 70  SELECT of a comp
203a7 6f 75 6e 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ound */.  sqlite
203a8 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
203a9 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
203aa 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 0a 0a  nnection */.  ..
203ab 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
203ac 3b 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c  ;.  if( p->selFl
203ad 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65  ags & SF_Resolve
203ae 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
203af 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20  WRC_Prune;.  }. 
203b0 20 70 4f 75 74 65 72 4e 43 20 3d 20 70 57 61 6c   pOuterNC = pWal
203b1 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 70 50  ker->u.pNC;.  pP
203b2 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e  arse = pWalker->
203b3 70 50 61 72 73 65 3b 0a 20 20 64 62 20 3d 20 70  pParse;.  db = p
203b4 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a  Parse->db;..  /*
203b5 20 4e 6f 72 6d 61 6c 6c 79 20 73 71 6c 69 74 65   Normally sqlite
203b6 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20  3SelectExpand() 
203b7 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 66  will be called f
203b8 69 72 73 74 20 61 6e 64 20 77 69 6c 6c 20 68 61  irst and will ha
203b9 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  ve.  ** already 
203ba 65 78 70 61 6e 64 65 64 20 74 68 69 73 20 53 45  expanded this SE
203bb 4c 45 43 54 2e 20 20 48 6f 77 65 76 65 72 2c 20  LECT.  However, 
203bc 69 66 20 74 68 69 73 20 69 73 20 61 20 73 75 62  if this is a sub
203bd 71 75 65 72 79 20 77 69 74 68 69 6e 0a 20 20 2a  query within.  *
203be 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c  * an expression,
203bf 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
203c0 78 70 72 4e 61 6d 65 73 28 29 20 77 69 6c 6c 20  xprNames() will 
203c1 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 6f 75  be called withou
203c2 74 20 61 0a 20 20 2a 2a 20 70 72 69 6f 72 20 63  t a.  ** prior c
203c3 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 53 65  all to sqlite3Se
203c4 6c 65 63 74 45 78 70 61 6e 64 28 29 2e 20 20 57  lectExpand().  W
203c5 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73  hen that happens
203c6 2c 20 6c 65 74 0a 20 20 2a 2a 20 73 71 6c 69 74  , let.  ** sqlit
203c7 65 33 53 65 6c 65 63 74 50 72 65 70 28 29 20 64  e3SelectPrep() d
203c8 6f 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 72 6f  o all of the pro
203c9 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73  cessing for this
203ca 20 53 45 4c 45 43 54 2e 0a 20 20 2a 2a 20 73 71   SELECT..  ** sq
203cb 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28  lite3SelectPrep(
203cc 29 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f  ) will invoke bo
203cd 74 68 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  th sqlite3Select
203ce 45 78 70 61 6e 64 28 29 20 61 6e 64 0a 20 20 2a  Expand() and.  *
203cf 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * this routine i
203d0 6e 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72  n the correct or
203d1 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  der..  */.  if( 
203d2 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  (p->selFlags & S
203d3 46 5f 45 78 70 61 6e 64 65 64 29 3d 3d 30 20 29  F_Expanded)==0 )
203d4 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  {.    sqlite3Sel
203d5 65 63 74 50 72 65 70 28 70 50 61 72 73 65 2c 20  ectPrep(pParse, 
203d6 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20  p, pOuterNC);.  
203d7 20 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65    return (pParse
203d8 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61  ->nErr || db->ma
203d9 6c 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52  llocFailed) ? WR
203da 43 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f 50 72  C_Abort : WRC_Pr
203db 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 69 73 43 6f  une;.  }..  isCo
203dc 6d 70 6f 75 6e 64 20 3d 20 70 2d 3e 70 50 72 69  mpound = p->pPri
203dd 6f 72 21 3d 30 3b 0a 20 20 6e 43 6f 6d 70 6f 75  or!=0;.  nCompou
203de 6e 64 20 3d 20 30 3b 0a 20 20 70 4c 65 66 74 6d  nd = 0;.  pLeftm
203df 6f 73 74 20 3d 20 70 3b 0a 20 20 77 68 69 6c 65  ost = p;.  while
203e0 28 20 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ( p ){.    asser
203e1 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  t( (p->selFlags 
203e2 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d  & SF_Expanded)!=
203e3 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
203e4 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20   (p->selFlags & 
203e5 53 46 5f 52 65 73 6f 6c 76 65 64 29 3d 3d 30 20  SF_Resolved)==0 
203e6 29 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61  );.    p->selFla
203e7 67 73 20 7c 3d 20 53 46 5f 52 65 73 6f 6c 76 65  gs |= SF_Resolve
203e8 64 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c  d;..    /* Resol
203e9 76 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  ve the expressio
203ea 6e 73 20 69 6e 20 74 68 65 20 4c 49 4d 49 54 20  ns in the LIMIT 
203eb 61 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73  and OFFSET claus
203ec 65 73 2e 20 54 68 65 73 65 0a 20 20 20 20 2a 2a  es. These.    **
203ed 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64   are not allowed
203ee 20 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79   to refer to any
203ef 20 6e 61 6d 65 73 2c 20 73 6f 20 70 61 73 73 20   names, so pass 
203f0 61 6e 20 65 6d 70 74 79 20 4e 61 6d 65 43 6f 6e  an empty NameCon
203f1 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  text..    */.   
203f2 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
203f3 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
203f4 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20     sNC.pParse = 
203f5 70 50 61 72 73 65 3b 0a 20 20 20 20 69 66 28 20  pParse;.    if( 
203f6 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
203f7 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d  prNames(&sNC, p-
203f8 3e 70 4c 69 6d 69 74 29 20 7c 7c 0a 20 20 20 20  >pLimit) ||.    
203f9 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c      sqlite3Resol
203fa 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43  veExprNames(&sNC
203fb 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b  , p->pOffset) ){
203fc 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52  .      return WR
203fd 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  C_Abort;.    }. 
203fe 20 0a 20 20 20 20 2f 2a 20 53 65 74 20 75 70 20   .    /* Set up 
203ff 74 68 65 20 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63  the local name-c
20400 6f 6e 74 65 78 74 20 74 6f 20 70 61 73 73 20 74  ontext to pass t
20401 6f 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  o sqlite3Resolve
20402 45 78 70 72 4e 61 6d 65 73 28 29 20 74 6f 0a 20  ExprNames() to. 
20403 20 20 20 2a 2a 20 72 65 73 6f 6c 76 65 20 74 68     ** resolve th
20404 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70  e result-set exp
20405 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 20 20  ression list..  
20406 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 61 6c 6c    */.    sNC.all
20407 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 73  owAgg = 1;.    s
20408 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 2d  NC.pSrcList = p-
20409 3e 70 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 70  >pSrc;.    sNC.p
2040a 4e 65 78 74 20 3d 20 70 4f 75 74 65 72 4e 43 3b  Next = pOuterNC;
2040b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c  .  .    /* Resol
2040c 76 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20  ve names in the 
2040d 72 65 73 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20  result set. */. 
2040e 20 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70     pEList = p->p
2040f 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72  EList;.    asser
20410 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
20411 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
20412 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  EList->nExpr; i+
20413 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  +){.      Expr *
20414 70 58 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69  pX = pEList->a[i
20415 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69  ].pExpr;.      i
20416 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  f( sqlite3Resolv
20417 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c  eExprNames(&sNC,
20418 20 70 58 29 20 29 7b 0a 20 20 20 20 20 20 20 20   pX) ){.        
20419 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2041a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2041b 20 20 0a 20 20 20 20 2f 2a 20 52 65 63 75 72 73    .    /* Recurs
2041c 69 76 65 6c 79 20 72 65 73 6f 6c 76 65 20 6e 61  ively resolve na
2041d 6d 65 73 20 69 6e 20 61 6c 6c 20 73 75 62 71 75  mes in all subqu
2041e 65 72 69 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20  eries.    */.   
2041f 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70   for(i=0; i<p->p
20420 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  Src->nSrc; i++){
20421 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72  .      struct Sr
20422 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
20423 6d 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b  m = &p->pSrc->a[
20424 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  i];.      if( pI
20425 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  tem->pSelect ){.
20426 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
20427 61 72 20 2a 7a 53 61 76 65 64 43 6f 6e 74 65 78  ar *zSavedContex
20428 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74  t = pParse->zAut
20429 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20  hContext;.      
2042a 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61    if( pItem->zNa
2042b 6d 65 20 29 20 70 50 61 72 73 65 2d 3e 7a 41 75  me ) pParse->zAu
2042c 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 49 74 65  thContext = pIte
2042d 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  m->zName;.      
2042e 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65    sqlite3Resolve
2042f 53 65 6c 65 63 74 4e 61 6d 65 73 28 70 50 61 72  SelectNames(pPar
20430 73 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65  se, pItem->pSele
20431 63 74 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20  ct, pOuterNC);. 
20432 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a         pParse->z
20433 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53  AuthContext = zS
20434 61 76 65 64 43 6f 6e 74 65 78 74 3b 0a 20 20 20  avedContext;.   
20435 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d       if( pParse-
20436 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
20437 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
20438 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
20439 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20      }.    }.  . 
2043a 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61     /* If there a
2043b 72 65 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20  re no aggregate 
2043c 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65  functions in the
2043d 20 72 65 73 75 6c 74 2d 73 65 74 2c 20 61 6e 64   result-set, and
2043e 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 0a 20 20   no GROUP BY .  
2043f 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c    ** expression,
20440 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 67   do not allow ag
20441 67 72 65 67 61 74 65 73 20 69 6e 20 61 6e 79 20  gregates in any 
20442 6f 66 20 74 68 65 20 6f 74 68 65 72 20 65 78 70  of the other exp
20443 72 65 73 73 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f  ressions..    */
20444 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d  .    assert( (p-
20445 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41  >selFlags & SF_A
20446 67 67 72 65 67 61 74 65 29 3d 3d 30 20 29 3b 0a  ggregate)==0 );.
20447 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70      pGroupBy = p
20448 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20  ->pGroupBy;.    
20449 69 66 28 20 70 47 72 6f 75 70 42 79 20 7c 7c 20  if( pGroupBy || 
2044a 73 4e 43 2e 68 61 73 41 67 67 20 29 7b 0a 20 20  sNC.hasAgg ){.  
2044b 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20      p->selFlags 
2044c 7c 3d 20 53 46 5f 41 67 67 72 65 67 61 74 65 3b  |= SF_Aggregate;
2044d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2044e 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d    sNC.allowAgg =
2044f 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20   0;.    }.  .   
20450 20 2f 2a 20 49 66 20 61 20 48 41 56 49 4e 47 20   /* If a HAVING 
20451 63 6c 61 75 73 65 20 69 73 20 70 72 65 73 65 6e  clause is presen
20452 74 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75  t, then there mu
20453 73 74 20 62 65 20 61 20 47 52 4f 55 50 20 42 59  st be a GROUP BY
20454 20 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a   clause..    */.
20455 20 20 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69      if( p->pHavi
20456 6e 67 20 26 26 20 21 70 47 72 6f 75 70 42 79 20  ng && !pGroupBy 
20457 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
20458 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
20459 20 22 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61   "a GROUP BY cla
2045a 75 73 65 20 69 73 20 72 65 71 75 69 72 65 64 20  use is required 
2045b 62 65 66 6f 72 65 20 48 41 56 49 4e 47 22 29 3b  before HAVING");
2045c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52  .      return WR
2045d 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  C_Abort;.    }. 
2045e 20 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65   .    /* Add the
2045f 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
20460 20 74 6f 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e   to the name-con
20461 74 65 78 74 20 62 65 66 6f 72 65 20 70 61 72 73  text before pars
20462 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f  ing the.    ** o
20463 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73  ther expressions
20464 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73   in the SELECT s
20465 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69  tatement. This i
20466 73 20 73 6f 20 74 68 61 74 0a 20 20 20 20 2a 2a  s so that.    **
20467 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
20468 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
20469 20 28 65 74 63 2e 29 20 63 61 6e 20 72 65 66 65   (etc.) can refe
2046a 72 20 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73  r to expressions
2046b 20 62 79 0a 20 20 20 20 2a 2a 20 61 6c 69 61 73   by.    ** alias
2046c 65 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  es in the result
2046d 20 73 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20   set..    **.   
2046e 20 2a 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a   ** Minor point:
2046f 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
20470 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 65  case, then the e
20471 78 70 72 65 73 73 69 6f 6e 20 77 69 6c 6c 20 62  xpression will b
20472 65 0a 20 20 20 20 2a 2a 20 72 65 2d 65 76 61 6c  e.    ** re-eval
20473 75 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 72  uated for each r
20474 65 66 65 72 65 6e 63 65 20 74 6f 20 69 74 2e 0a  eference to it..
20475 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70      */.    sNC.p
20476 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  EList = p->pELis
20477 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  t;.    if( sqlit
20478 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
20479 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 57 68 65  es(&sNC, p->pWhe
2047a 72 65 29 20 7c 7c 0a 20 20 20 20 20 20 20 73 71  re) ||.       sq
2047b 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
2047c 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70  Names(&sNC, p->p
2047d 48 61 76 69 6e 67 29 0a 20 20 20 20 29 7b 0a 20  Having).    ){. 
2047e 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
2047f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 0a 20 20  Abort;.    }..  
20480 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
20481 59 20 61 6e 64 20 47 52 4f 55 50 20 42 59 20 63  Y and GROUP BY c
20482 6c 61 75 73 65 73 20 6d 61 79 20 6e 6f 74 20 72  lauses may not r
20483 65 66 65 72 20 74 6f 20 74 65 72 6d 73 20 69 6e  efer to terms in
20484 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75  .    ** outer qu
20485 65 72 69 65 73 20 0a 20 20 20 20 2a 2f 0a 20 20  eries .    */.  
20486 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b    sNC.pNext = 0;
20487 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67  .    sNC.allowAg
20488 67 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 50  g = 1;..    /* P
20489 72 6f 63 65 73 73 20 74 68 65 20 4f 52 44 45 52  rocess the ORDER
2048a 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 73   BY clause for s
2048b 69 6e 67 6c 65 74 6f 6e 20 53 45 4c 45 43 54 20  ingleton SELECT 
2048c 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20  statements..    
2048d 2a 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  ** The ORDER BY 
2048e 63 6c 61 75 73 65 20 66 6f 72 20 63 6f 6d 70 6f  clause for compo
2048f 75 6e 64 73 20 53 45 4c 45 43 54 20 73 74 61 74  unds SELECT stat
20490 65 6d 65 6e 74 73 20 69 73 20 68 61 6e 64 6c 65  ements is handle
20491 64 0a 20 20 20 20 2a 2a 20 62 65 6c 6f 77 2c 20  d.    ** below, 
20492 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65  after all of the
20493 20 72 65 73 75 6c 74 2d 73 65 74 73 20 66 6f 72   result-sets for
20494 20 61 6c 6c 20 6f 66 20 74 68 65 20 65 6c 65 6d   all of the elem
20495 65 6e 74 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74  ents of.    ** t
20496 68 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65  he compound have
20497 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a   been resolved..
20498 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
20499 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65  isCompound && re
2049a 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
2049b 79 28 26 73 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f  y(&sNC, p, p->pO
2049c 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29  rderBy, "ORDER")
2049d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2049e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
2049f 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  }.    if( db->ma
204a0 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
204a1 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
204a2 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  bort;.    }.  . 
204a3 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68     /* Resolve th
204a4 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  e GROUP BY claus
204a5 65 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65 20  e.  At the same 
204a6 74 69 6d 65 2c 20 6d 61 6b 65 20 73 75 72 65 20  time, make sure 
204a7 0a 20 20 20 20 2a 2a 20 74 68 65 20 47 52 4f 55  .    ** the GROU
204a8 50 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73  P BY clause does
204a9 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 67 67   not contain agg
204aa 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
204ab 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
204ac 20 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20   pGroupBy ){.   
204ad 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
204ae 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
204af 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 72      .      if( r
204b0 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
204b1 42 79 28 26 73 4e 43 2c 20 70 2c 20 70 47 72 6f  By(&sNC, p, pGro
204b2 75 70 42 79 2c 20 22 47 52 4f 55 50 22 29 20 7c  upBy, "GROUP") |
204b3 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
204b4 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ed ){.        re
204b5 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
204b6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
204b7 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72  r(i=0, pItem=pGr
204b8 6f 75 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f  oupBy->a; i<pGro
204b9 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  upBy->nExpr; i++
204ba 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
204bb 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50      if( ExprHasP
204bc 72 6f 70 65 72 74 79 28 70 49 74 65 6d 2d 3e 70  roperty(pItem->p
204bd 45 78 70 72 2c 20 45 50 5f 41 67 67 29 20 29 7b  Expr, EP_Agg) ){
204be 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
204bf 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
204c0 65 2c 20 22 61 67 67 72 65 67 61 74 65 20 66 75  e, "aggregate fu
204c1 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20  nctions are not 
204c2 61 6c 6c 6f 77 65 64 20 69 6e 20 22 0a 20 20 20  allowed in ".   
204c3 20 20 20 20 20 20 20 20 20 20 20 22 74 68 65 20             "the 
204c4 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 22  GROUP BY clause"
204c5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
204c6 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
204c7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
204c8 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
204c9 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65  dvance to the ne
204ca 78 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63  xt term of the c
204cb 6f 6d 70 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20  ompound.    */. 
204cc 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72     p = p->pPrior
204cd 3b 0a 20 20 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b  ;.    nCompound+
204ce 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73  +;.  }..  /* Res
204cf 6f 6c 76 65 20 74 68 65 20 4f 52 44 45 52 20 42  olve the ORDER B
204d0 59 20 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20  Y on a compound 
204d1 53 45 4c 45 43 54 20 61 66 74 65 72 20 61 6c 6c  SELECT after all
204d2 20 74 65 72 6d 73 20 6f 66 0a 20 20 2a 2a 20 74   terms of.  ** t
204d3 68 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65  he compound have
204d4 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a   been resolved..
204d5 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d    */.  if( isCom
204d6 70 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65  pound && resolve
204d7 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28  CompoundOrderBy(
204d8 70 50 61 72 73 65 2c 20 70 4c 65 66 74 6d 6f 73  pParse, pLeftmos
204d9 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  t) ){.    return
204da 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a   WRC_Abort;.  }.
204db 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72  .  return WRC_Pr
204dc 75 6e 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  une;.}../*.** Th
204dd 69 73 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73  is routine walks
204de 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
204df 72 65 65 20 61 6e 64 20 72 65 73 6f 6c 76 65 73  ree and resolves
204e0 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 0a 2a   references to.*
204e1 2a 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20  * table columns 
204e2 61 6e 64 20 72 65 73 75 6c 74 2d 73 65 74 20 63  and result-set c
204e3 6f 6c 75 6d 6e 73 2e 20 20 41 74 20 74 68 65 20  olumns.  At the 
204e4 73 61 6d 65 20 74 69 6d 65 2c 20 64 6f 20 65 72  same time, do er
204e5 72 6f 72 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20  ror.** checking 
204e6 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 61 67  on function usag
204e7 65 20 61 6e 64 20 73 65 74 20 61 20 66 6c 61 67  e and set a flag
204e8 20 69 66 20 61 6e 79 20 61 67 67 72 65 67 61 74   if any aggregat
204e9 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61  e functions.** a
204ea 72 65 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54  re seen..**.** T
204eb 6f 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 20  o resolve table 
204ec 63 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63  columns referenc
204ed 65 73 20 77 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e  es we look for n
204ee 6f 64 65 73 20 28 6f 72 20 73 75 62 74 72 65 65  odes (or subtree
204ef 73 29 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f  s) of the .** fo
204f0 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20  rm X.Y.Z or Y.Z 
204f1 6f 72 20 6a 75 73 74 20 5a 20 77 68 65 72 65 0a  or just Z where.
204f2 2a 2a 0a 2a 2a 20 20 20 20 20 20 58 3a 20 20 20  **.**      X:   
204f3 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61  The name of a da
204f4 74 61 62 61 73 65 2e 20 20 45 78 3a 20 20 22 6d  tabase.  Ex:  "m
204f5 61 69 6e 22 20 6f 72 20 22 74 65 6d 70 22 20 6f  ain" or "temp" o
204f6 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  r.**           t
204f7 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65  he symbolic name
204f8 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6e 20   assigned to an 
204f9 41 54 54 41 43 48 2d 65 64 20 64 61 74 61 62 61  ATTACH-ed databa
204fa 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59  se..**.**      Y
204fb 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  :   The name of 
204fc 61 20 74 61 62 6c 65 20 69 6e 20 61 20 46 52 4f  a table in a FRO
204fd 4d 20 63 6c 61 75 73 65 2e 20 20 4f 72 20 69 6e  M clause.  Or in
204fe 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 20 20   a trigger.**   
204ff 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 74          one of t
20500 68 65 20 73 70 65 63 69 61 6c 20 6e 61 6d 65 73  he special names
20501 20 22 6f 6c 64 22 20 6f 72 20 22 6e 65 77 22 2e   "old" or "new".
20502 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a 20 20  .**.**      Z:  
20503 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63   The name of a c
20504 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 59  olumn in table Y
20505 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f 64 65  ..**.** The node
20506 20 61 74 20 74 68 65 20 72 6f 6f 74 20 6f 66 20   at the root of 
20507 74 68 65 20 73 75 62 74 72 65 65 20 69 73 20 6d  the subtree is m
20508 6f 64 69 66 69 65 64 20 61 73 20 66 6f 6c 6c 6f  odified as follo
20509 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45 78 70  ws:.**.**    Exp
2050a 72 2e 6f 70 20 20 20 20 20 20 20 20 43 68 61 6e  r.op        Chan
2050b 67 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e  ged to TK_COLUMN
2050c 0a 2a 2a 20 20 20 20 45 78 70 72 2e 70 54 61 62  .**    Expr.pTab
2050d 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20        Points to 
2050e 74 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74  the Table object
2050f 20 66 6f 72 20 58 2e 59 0a 2a 2a 20 20 20 20 45   for X.Y.**    E
20510 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 20 20 54 68  xpr.iColumn   Th
20511 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69  e column index i
20512 6e 20 58 2e 59 2e 20 20 2d 31 20 66 6f 72 20 74  n X.Y.  -1 for t
20513 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20  he rowid..**    
20514 45 78 70 72 2e 69 54 61 62 6c 65 20 20 20 20 54  Expr.iTable    T
20515 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e  he VDBE cursor n
20516 75 6d 62 65 72 20 66 6f 72 20 58 2e 59 0a 2a 2a  umber for X.Y.**
20517 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76  .**.** To resolv
20518 65 20 72 65 73 75 6c 74 2d 73 65 74 20 72 65 66  e result-set ref
20519 65 72 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f  erences, look fo
2051a 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  r expression nod
2051b 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72  es of the.** for
2051c 6d 20 5a 20 28 77 69 74 68 20 6e 6f 20 58 20 61  m Z (with no X a
2051d 6e 64 20 59 20 70 72 65 66 69 78 29 20 77 68 65  nd Y prefix) whe
2051e 72 65 20 74 68 65 20 5a 20 6d 61 74 63 68 65 73  re the Z matches
2051f 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a   the right-hand.
20520 2a 2a 20 73 69 7a 65 20 6f 66 20 61 6e 20 41 53  ** size of an AS
20521 20 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 72   clause in the r
20522 65 73 75 6c 74 2d 73 65 74 20 6f 66 20 61 20 53  esult-set of a S
20523 45 4c 45 43 54 2e 20 20 54 68 65 20 5a 20 65 78  ELECT.  The Z ex
20524 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 72  pression.** is r
20525 65 70 6c 61 63 65 64 20 62 79 20 61 20 63 6f 70  eplaced by a cop
20526 79 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 68 61  y of the left-ha
20527 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 72  nd side of the r
20528 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73  esult-set expres
20529 73 69 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e  sion..** Table-n
2052a 61 6d 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e  ame and function
2052b 20 72 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 63 75   resolution occu
2052c 72 73 20 6f 6e 20 74 68 65 20 73 75 62 73 74 69  rs on the substi
2052d 74 75 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e  tuted expression
2052e 0a 2a 2a 20 74 72 65 65 2e 20 20 46 6f 72 20 65  .** tree.  For e
2052f 78 61 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a  xample, in:.**.*
20530 2a 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b  *      SELECT a+
20531 62 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79  b AS x, c+d AS y
20532 20 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42   FROM t1 ORDER B
20533 59 20 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22  Y x;.**.** The "
20534 78 22 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6f  x" term of the o
20535 72 64 65 72 20 62 79 20 69 73 20 72 65 70 6c 61  rder by is repla
20536 63 65 64 20 62 79 20 22 61 2b 62 22 20 74 6f 20  ced by "a+b" to 
20537 72 65 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20  render:.**.**   
20538 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53     SELECT a+b AS
20539 20 78 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f   x, c+d AS y FRO
2053a 4d 20 74 31 20 4f 52 44 45 52 20 42 59 20 61 2b  M t1 ORDER BY a+
2053b 62 3b 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f  b;.**.** Functio
2053c 6e 20 63 61 6c 6c 73 20 61 72 65 20 63 68 65 63  n calls are chec
2053d 6b 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ked to make sure
2053e 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69   that the functi
2053f 6f 6e 20 69 73 20 0a 2a 2a 20 64 65 66 69 6e 65  on is .** define
20540 64 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63  d and that the c
20541 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66  orrect number of
20542 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 73   arguments are s
20543 70 65 63 69 66 69 65 64 2e 0a 2a 2a 20 49 66 20  pecified..** If 
20544 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
20545 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  an aggregate fun
20546 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20  ction, then the 
20547 70 4e 43 2d 3e 68 61 73 41 67 67 20 69 73 0a 2a  pNC->hasAgg is.*
20548 2a 20 73 65 74 20 61 6e 64 20 74 68 65 20 6f 70  * set and the op
20549 63 6f 64 65 20 69 73 20 63 68 61 6e 67 65 64 20  code is changed 
2054a 66 72 6f 6d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e  from TK_FUNCTION
2054b 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54   to TK_AGG_FUNCT
2054c 49 4f 4e 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 78  ION..** If an ex
2054d 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e  pression contain
2054e 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  s aggregate func
2054f 74 69 6f 6e 73 20 74 68 65 6e 20 74 68 65 20 45  tions then the E
20550 50 5f 41 67 67 0a 2a 2a 20 70 72 6f 70 65 72 74  P_Agg.** propert
20551 79 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73  y on the express
20552 69 6f 6e 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a  ion is set..**.*
20553 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  * An error messa
20554 67 65 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50  ge is left in pP
20555 61 72 73 65 20 69 66 20 61 6e 79 74 68 69 6e 67  arse if anything
20556 20 69 73 20 61 6d 69 73 73 2e 20 20 54 68 65 20   is amiss.  The 
20557 6e 75 6d 62 65 72 0a 2a 2a 20 69 66 20 65 72 72  number.** if err
20558 6f 72 73 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ors is returned.
20559 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2055a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65  TE int sqlite3Re
2055b 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 20  solveExprNames( 
2055c 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  .  NameContext *
2055d 70 4e 43 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61  pNC,       /* Na
2055e 6d 65 73 70 61 63 65 20 74 6f 20 72 65 73 6f 6c  mespace to resol
2055f 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  ve expressions i
20560 6e 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  n. */.  Expr *pE
20561 78 70 72 20 20 20 20 20 20 20 20 20 20 20 20 20  xpr             
20562 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
20563 6e 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64  n to be analyzed
20564 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 73 61  . */.){.  int sa
20565 76 65 64 48 61 73 41 67 67 3b 0a 20 20 57 61 6c  vedHasAgg;.  Wal
20566 6b 65 72 20 77 3b 0a 0a 20 20 69 66 28 20 70 45  ker w;..  if( pE
20567 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  xpr==0 ) return 
20568 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  0;.#if SQLITE_MA
20569 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20  X_EXPR_DEPTH>0. 
2056a 20 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50   {.    Parse *pP
2056b 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72  arse = pNC->pPar
2056c 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  se;.    if( sqli
2056d 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67  te3ExprCheckHeig
2056e 68 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ht(pParse, pExpr
2056f 2d 3e 6e 48 65 69 67 68 74 2b 70 4e 43 2d 3e 70  ->nHeight+pNC->p
20570 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 29 20  Parse->nHeight) 
20571 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
20572 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  1;.    }.    pPa
20573 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20  rse->nHeight += 
20574 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a  pExpr->nHeight;.
20575 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 61 76    }.#endif.  sav
20576 65 64 48 61 73 41 67 67 20 3d 20 70 4e 43 2d 3e  edHasAgg = pNC->
20577 68 61 73 41 67 67 3b 0a 20 20 70 4e 43 2d 3e 68  hasAgg;.  pNC->h
20578 61 73 41 67 67 20 3d 20 30 3b 0a 20 20 77 2e 78  asAgg = 0;.  w.x
20579 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72  ExprCallback = r
2057a 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 3b 0a  esolveExprStep;.
2057b 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62    w.xSelectCallb
2057c 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c  ack = resolveSel
2057d 65 63 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 61  ectStep;.  w.pPa
2057e 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73  rse = pNC->pPars
2057f 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70  e;.  w.u.pNC = p
20580 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c  NC;.  sqlite3Wal
20581 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29  kExpr(&w, pExpr)
20582 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ;.#if SQLITE_MAX
20583 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20  _EXPR_DEPTH>0.  
20584 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65  pNC->pParse->nHe
20585 69 67 68 74 20 2d 3d 20 70 45 78 70 72 2d 3e 6e  ight -= pExpr->n
20586 48 65 69 67 68 74 3b 0a 23 65 6e 64 69 66 0a 20  Height;.#endif. 
20587 20 69 66 28 20 70 4e 43 2d 3e 6e 45 72 72 3e 30   if( pNC->nErr>0
20588 20 7c 7c 20 77 2e 70 50 61 72 73 65 2d 3e 6e 45   || w.pParse->nE
20589 72 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 70 72  rr>0 ){.    Expr
2058a 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70  SetProperty(pExp
2058b 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a 20 20  r, EP_Error);.  
2058c 7d 0a 20 20 69 66 28 20 70 4e 43 2d 3e 68 61 73  }.  if( pNC->has
2058d 41 67 67 20 29 7b 0a 20 20 20 20 45 78 70 72 53  Agg ){.    ExprS
2058e 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  etProperty(pExpr
2058f 2c 20 45 50 5f 41 67 67 29 3b 0a 20 20 7d 65 6c  , EP_Agg);.  }el
20590 73 65 20 69 66 28 20 73 61 76 65 64 48 61 73 41  se if( savedHasA
20591 67 67 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e 68  gg ){.    pNC->h
20592 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a 20  asAgg = 1;.  }. 
20593 20 72 65 74 75 72 6e 20 45 78 70 72 48 61 73 50   return ExprHasP
20594 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
20595 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a  P_Error);.}.../*
20596 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20  .** Resolve all 
20597 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 65 78 70  names in all exp
20598 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 20 53 45  ressions of a SE
20599 4c 45 43 54 20 61 6e 64 20 69 6e 20 61 6c 6c 0a  LECT and in all.
2059a 2a 2a 20 64 65 63 65 6e 64 65 6e 74 73 20 6f 66  ** decendents of
2059b 20 74 68 65 20 53 45 4c 45 43 54 2c 20 69 6e 63   the SELECT, inc
2059c 6c 75 64 69 6e 67 20 63 6f 6d 70 6f 75 6e 64 73  luding compounds
2059d 20 6f 66 66 20 6f 66 20 70 2d 3e 70 50 72 69 6f   off of p->pPrio
2059e 72 2c 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73  r,.** subqueries
2059f 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 2c   in expressions,
205a0 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73 20   and subqueries 
205a1 75 73 65 64 20 61 73 20 46 52 4f 4d 20 63 6c 61  used as FROM cla
205a2 75 73 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a 2a 2a  use.** terms..**
205a3 0a 2a 2a 20 53 65 65 20 73 71 6c 69 74 65 33 52  .** See sqlite3R
205a4 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
205a5 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74  ) for a descript
205a6 69 6f 6e 20 6f 66 20 74 68 65 20 6b 69 6e 64 73  ion of the kinds
205a7 20 6f 66 0a 2a 2a 20 74 72 61 6e 73 66 6f 72 6d   of.** transform
205a8 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 63 63 75  ations that occu
205a9 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 45 4c  r..**.** All SEL
205aa 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 73  ECT statements s
205ab 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20  hould have been 
205ac 65 78 70 61 6e 64 65 64 20 75 73 69 6e 67 0a 2a  expanded using.*
205ad 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  * sqlite3SelectE
205ae 78 70 61 6e 64 28 29 20 70 72 69 6f 72 20 74 6f  xpand() prior to
205af 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72   invoking this r
205b0 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54  outine..*/.SQLIT
205b1 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
205b2 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c  qlite3ResolveSel
205b3 65 63 74 4e 61 6d 65 73 28 0a 20 20 50 61 72 73  ectNames(.  Pars
205b4 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
205b5 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
205b6 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
205b7 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
205b8 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45       /* The SELE
205b9 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69  CT statement bei
205ba 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 4e  ng coded. */.  N
205bb 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74  ameContext *pOut
205bc 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63 6f  erNC  /* Name co
205bd 6e 74 65 78 74 20 66 6f 72 20 70 61 72 65 6e 74  ntext for parent
205be 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
205bf 74 20 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b 65 72  t */.){.  Walker
205c0 20 77 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   w;..  assert( p
205c1 21 3d 30 20 29 3b 0a 20 20 77 2e 78 45 78 70 72  !=0 );.  w.xExpr
205c2 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c  Callback = resol
205c3 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 77 2e  veExprStep;.  w.
205c4 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
205c5 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53  = resolveSelectS
205c6 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20  tep;.  w.pParse 
205c7 3d 20 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 2e  = pParse;.  w.u.
205c8 70 4e 43 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a  pNC = pOuterNC;.
205c9 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c    sqlite3WalkSel
205ca 65 63 74 28 26 77 2c 20 70 29 3b 0a 7d 0a 0a 2f  ect(&w, p);.}../
205cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
205cc 6e 64 20 6f 66 20 72 65 73 6f 6c 76 65 2e 63 20  nd of resolve.c 
205cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
205d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
205d1 65 67 69 6e 20 66 69 6c 65 20 65 78 70 72 2e 63  egin file expr.c
205d2 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
205d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
205d5 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
205d6 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
205d7 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
205d8 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
205d9 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
205da 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
205db 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
205dc 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
205dd 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
205de 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
205df 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
205e0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
205e1 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
205e2 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
205e3 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
205e4 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
205e5 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
205e6 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
205e7 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
205e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205ec 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
205ed 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74  le contains rout
205ee 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 61 6e  ines used for an
205ef 61 6c 79 7a 69 6e 67 20 65 78 70 72 65 73 73 69  alyzing expressi
205f0 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 67  ons and.** for g
205f1 65 6e 65 72 61 74 69 6e 67 20 56 44 42 45 20 63  enerating VDBE c
205f2 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 61 74  ode that evaluat
205f3 65 73 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  es expressions i
205f4 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a  n SQLite..*/../*
205f5 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 27  .** Return the '
205f6 61 66 66 69 6e 69 74 79 27 20 6f 66 20 74 68 65  affinity' of the
205f7 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70   expression pExp
205f8 72 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20  r if any..**.** 
205f9 49 66 20 70 45 78 70 72 20 69 73 20 61 20 63 6f  If pExpr is a co
205fa 6c 75 6d 6e 2c 20 61 20 72 65 66 65 72 65 6e 63  lumn, a referenc
205fb 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69  e to a column vi
205fc 61 20 61 6e 20 27 41 53 27 20 61 6c 69 61 73 2c  a an 'AS' alias,
205fd 0a 2a 2a 20 6f 72 20 61 20 73 75 62 2d 73 65 6c  .** or a sub-sel
205fe 65 63 74 20 77 69 74 68 20 61 20 63 6f 6c 75 6d  ect with a colum
205ff 6e 20 61 73 20 74 68 65 20 72 65 74 75 72 6e 20  n as the return 
20600 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20  value, then the 
20601 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 6f 66 20  .** affinity of 
20602 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72  that column is r
20603 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69  eturned. Otherwi
20604 73 65 2c 20 30 78 30 30 20 69 73 20 72 65 74 75  se, 0x00 is retu
20605 72 6e 65 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74  rned,.** indicat
20606 69 6e 67 20 6e 6f 20 61 66 66 69 6e 69 74 79 20  ing no affinity 
20607 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69  for the expressi
20608 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74  on..**.** i.e. t
20609 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2060a 65 78 70 72 65 73 73 73 69 6f 6e 73 20 69 6e 20  expresssions in 
2060b 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
2060c 61 74 65 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20  atements all.** 
2060d 68 61 76 65 20 61 6e 20 61 66 66 69 6e 69 74 79  have an affinity
2060e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54  :.**.** CREATE T
2060f 41 42 4c 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53  ABLE t1(a);.** S
20610 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20  ELECT * FROM t1 
20611 57 48 45 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45  WHERE a;.** SELE
20612 43 54 20 61 20 41 53 20 62 20 46 52 4f 4d 20 74  CT a AS b FROM t
20613 31 20 57 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45  1 WHERE b;.** SE
20614 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57  LECT * FROM t1 W
20615 48 45 52 45 20 28 73 65 6c 65 63 74 20 61 20 66  HERE (select a f
20616 72 6f 6d 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49  rom t1);.*/.SQLI
20617 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
20618 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
20619 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29  ity(Expr *pExpr)
2061a 7b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 70 45 78  {.  int op = pEx
2061b 70 72 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70  pr->op;.  if( op
2061c 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20  ==TK_SELECT ){. 
2061d 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
2061e 2d 3e 66 6c 61 67 73 26 45 50 5f 78 49 73 53 65  ->flags&EP_xIsSe
2061f 6c 65 63 74 20 29 3b 0a 20 20 20 20 72 65 74 75  lect );.    retu
20620 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41 66  rn sqlite3ExprAf
20621 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 78 2e  finity(pExpr->x.
20622 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d  pSelect->pEList-
20623 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20  >a[0].pExpr);.  
20624 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
20625 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28  _OMIT_CAST.  if(
20626 20 6f 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a   op==TK_CAST ){.
20627 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
20628 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
20629 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pr, EP_IntValue)
2062a 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   );.    return s
2062b 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79  qlite3AffinityTy
2062c 70 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  pe(pExpr->u.zTok
2062d 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  en);.  }.#endif.
2062e 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47    if( (op==TK_AG
2062f 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d  G_COLUMN || op==
20630 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d  TK_COLUMN || op=
20631 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20 0a 20  =TK_REGISTER) . 
20632 20 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62    && pExpr->pTab
20633 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20  !=0.  ){.    /* 
20634 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20  op==TK_REGISTER 
20635 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21 3d  && pExpr->pTab!=
20636 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 70  0 happens when p
20637 45 78 70 72 20 77 61 73 20 6f 72 69 67 69 6e 61  Expr was origina
20638 6c 6c 79 0a 20 20 20 20 2a 2a 20 61 20 54 4b 5f  lly.    ** a TK_
20639 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 70  COLUMN but was p
2063a 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 61  reviously evalua
2063b 74 65 64 20 61 6e 64 20 63 61 63 68 65 64 20 69  ted and cached i
2063c 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  n a register */.
2063d 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 45 78 70      int j = pExp
2063e 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  r->iColumn;.    
2063f 69 66 28 20 6a 3c 30 20 29 20 72 65 74 75 72 6e  if( j<0 ) return
20640 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
20641 47 45 52 3b 0a 20 20 20 20 61 73 73 65 72 74 28  GER;.    assert(
20642 20 70 45 78 70 72 2d 3e 70 54 61 62 20 26 26 20   pExpr->pTab && 
20643 6a 3c 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 6e  j<pExpr->pTab->n
20644 43 6f 6c 20 29 3b 0a 20 20 20 20 72 65 74 75 72  Col );.    retur
20645 6e 20 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 61  n pExpr->pTab->a
20646 43 6f 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 79 3b  Col[j].affinity;
20647 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 45  .  }.  return pE
20648 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3b 0a 7d  xpr->affinity;.}
20649 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
2064a 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2064b 63 65 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f  ce for expressio
2064c 6e 20 70 45 78 70 72 20 74 6f 20 62 65 20 74 68  n pExpr to be th
2064d 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73  e collating.** s
2064e 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 62 79  equence named by
2064f 20 70 54 6f 6b 65 6e 2e 20 20 20 52 65 74 75 72   pToken.   Retur
20650 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
20651 68 65 20 72 65 76 69 73 65 64 20 65 78 70 72 65  he revised expre
20652 73 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 63 6f  ssion..** The co
20653 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
20654 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65   is marked as "e
20655 78 70 6c 69 63 69 74 22 20 75 73 69 6e 67 20 74  xplicit" using t
20656 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65  he EP_ExpCollate
20657 0a 2a 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65 78  .** flag.  An ex
20658 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e 67  plicit collating
20659 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 20 6f   sequence will o
2065a 76 65 72 72 69 64 65 20 69 6d 70 6c 69 63 69 74  verride implicit
2065b 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  .** collating se
2065c 71 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49  quences..*/.SQLI
2065d 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
2065e 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43  *sqlite3ExprSetC
2065f 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73  oll(Parse *pPars
20660 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  e, Expr *pExpr, 
20661 54 6f 6b 65 6e 20 2a 70 43 6f 6c 6c 4e 61 6d 65  Token *pCollName
20662 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  ){.  char *zColl
20663 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
20664 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d   /* Dequoted nam
20665 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  e of collation s
20666 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 43 6f 6c  equence */.  Col
20667 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 73  lSeq *pColl;.  s
20668 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
20669 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c  rse->db;.  zColl
2066a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
2066b 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43 6f 6c  omToken(db, pCol
2066c 6c 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 45  lName);.  if( pE
2066d 78 70 72 20 26 26 20 7a 43 6f 6c 6c 20 29 7b 0a  xpr && zColl ){.
2066e 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
2066f 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
20670 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b  (pParse, zColl);
20671 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  .    if( pColl )
20672 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70  {.      pExpr->p
20673 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20  Coll = pColl;.  
20674 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73      pExpr->flags
20675 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74   |= EP_ExpCollat
20676 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  e;.    }.  }.  s
20677 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
20678 20 7a 43 6f 6c 6c 29 3b 0a 20 20 72 65 74 75 72   zColl);.  retur
20679 6e 20 70 45 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  n pExpr;.}../*.*
2067a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 66  * Return the def
2067b 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  ault collation s
2067c 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20  equence for the 
2067d 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72  expression pExpr
2067e 2e 20 49 66 0a 2a 2a 20 74 68 65 72 65 20 69 73  . If.** there is
2067f 20 6e 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c   no default coll
20680 61 74 69 6f 6e 20 74 79 70 65 2c 20 72 65 74 75  ation type, retu
20681 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rn 0..*/.SQLITE_
20682 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
20683 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c  *sqlite3ExprColl
20684 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73  Seq(Parse *pPars
20685 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  e, Expr *pExpr){
20686 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
20687 6c 20 3d 20 30 3b 0a 20 20 45 78 70 72 20 2a 70  l = 0;.  Expr *p
20688 20 3d 20 70 45 78 70 72 3b 0a 20 20 77 68 69 6c   = pExpr;.  whil
20689 65 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b 0a  e( ALWAYS(p) ){.
2068a 20 20 20 20 69 6e 74 20 6f 70 3b 0a 20 20 20 20      int op;.    
2068b 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70 43 6f 6c 6c  pColl = p->pColl
2068c 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20  ;.    if( pColl 
2068d 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 6f 70 20  ) break;.    op 
2068e 3d 20 70 2d 3e 6f 70 3b 0a 20 20 20 20 69 66 28  = p->op;.    if(
2068f 20 70 2d 3e 70 54 61 62 21 3d 30 20 26 26 20 28   p->pTab!=0 && (
20690 0a 20 20 20 20 20 20 20 20 6f 70 3d 3d 54 4b 5f  .        op==TK_
20691 41 47 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70  AGG_COLUMN || op
20692 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f  ==TK_COLUMN || o
20693 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 7c  p==TK_REGISTER |
20694 7c 20 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52  | op==TK_TRIGGER
20695 0a 20 20 20 20 29 29 7b 0a 20 20 20 20 20 20 2f  .    )){.      /
20696 2a 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45  * op==TK_REGISTE
20697 52 20 26 26 20 70 2d 3e 70 54 61 62 21 3d 30 20  R && p->pTab!=0 
20698 68 61 70 70 65 6e 73 20 77 68 65 6e 20 70 45 78  happens when pEx
20699 70 72 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c  pr was originall
2069a 79 0a 20 20 20 20 20 20 2a 2a 20 61 20 54 4b 5f  y.      ** a TK_
2069b 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 70  COLUMN but was p
2069c 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 61  reviously evalua
2069d 74 65 64 20 61 6e 64 20 63 61 63 68 65 64 20 69  ted and cached i
2069e 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  n a register */.
2069f 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
206a0 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 69   *zColl;.      i
206a1 6e 74 20 6a 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d  nt j = p->iColum
206a2 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 6a 3e 3d  n;.      if( j>=
206a3 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
206a4 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
206a5 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 7a  e->db;.        z
206a6 43 6f 6c 6c 20 3d 20 70 2d 3e 70 54 61 62 2d 3e  Coll = p->pTab->
206a7 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20  aCol[j].zColl;. 
206a8 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73         pColl = s
206a9 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
206aa 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a  q(db, ENC(db), z
206ab 43 6f 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20  Coll, 0);.      
206ac 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d    pExpr->pColl =
206ad 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a   pColl;.      }.
206ae 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
206af 20 7d 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 54   }.    if( op!=T
206b0 4b 5f 43 41 53 54 20 26 26 20 6f 70 21 3d 54 4b  K_CAST && op!=TK
206b1 5f 55 50 4c 55 53 20 29 7b 0a 20 20 20 20 20 20  _UPLUS ){.      
206b2 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
206b3 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20   p = p->pLeft;. 
206b4 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
206b5 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 70 50 61  CheckCollSeq(pPa
206b6 72 73 65 2c 20 70 43 6f 6c 6c 29 20 29 7b 20 0a  rse, pColl) ){ .
206b7 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20      pColl = 0;. 
206b8 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c   }.  return pCol
206b9 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70  l;.}../*.** pExp
206ba 72 20 69 73 20 61 6e 20 6f 70 65 72 61 6e 64 20  r is an operand 
206bb 6f 66 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20  of a comparison 
206bc 6f 70 65 72 61 74 6f 72 2e 20 20 61 66 66 32 20  operator.  aff2 
206bd 69 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61  is the.** type a
206be 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 6f  ffinity of the o
206bf 74 68 65 72 20 6f 70 65 72 61 6e 64 2e 20 20 54  ther operand.  T
206c0 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
206c1 72 6e 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20  rns the.** type 
206c2 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68  affinity that sh
206c3 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72  ould be used for
206c4 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
206c5 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c  operator..*/.SQL
206c6 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
206c7 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41   sqlite3CompareA
206c8 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45  ffinity(Expr *pE
206c9 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 7b  xpr, char aff2){
206ca 0a 20 20 63 68 61 72 20 61 66 66 31 20 3d 20 73  .  char aff1 = s
206cb 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
206cc 74 79 28 70 45 78 70 72 29 3b 0a 20 20 69 66 28  ty(pExpr);.  if(
206cd 20 61 66 66 31 20 26 26 20 61 66 66 32 20 29 7b   aff1 && aff2 ){
206ce 0a 20 20 20 20 2f 2a 20 42 6f 74 68 20 73 69 64  .    /* Both sid
206cf 65 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72  es of the compar
206d0 69 73 6f 6e 20 61 72 65 20 63 6f 6c 75 6d 6e 73  ison are columns
206d1 2e 20 49 66 20 6f 6e 65 20 68 61 73 20 6e 75 6d  . If one has num
206d2 65 72 69 63 0a 20 20 20 20 2a 2a 20 61 66 66 69  eric.    ** affi
206d3 6e 69 74 79 2c 20 75 73 65 20 74 68 61 74 2e 20  nity, use that. 
206d4 4f 74 68 65 72 77 69 73 65 20 75 73 65 20 6e 6f  Otherwise use no
206d5 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 2a   affinity..    *
206d6 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  /.    if( sqlite
206d7 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69  3IsNumericAffini
206d8 74 79 28 61 66 66 31 29 20 7c 7c 20 73 71 6c 69  ty(aff1) || sqli
206d9 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69  te3IsNumericAffi
206da 6e 69 74 79 28 61 66 66 32 29 20 29 7b 0a 20 20  nity(aff2) ){.  
206db 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
206dc 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20  E_AFF_NUMERIC;. 
206dd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
206de 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46  return SQLITE_AF
206df 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20  F_NONE;.    }.  
206e0 7d 65 6c 73 65 20 69 66 28 20 21 61 66 66 31 20  }else if( !aff1 
206e1 26 26 20 21 61 66 66 32 20 29 7b 0a 20 20 20 20  && !aff2 ){.    
206e2 2f 2a 20 4e 65 69 74 68 65 72 20 73 69 64 65 20  /* Neither side 
206e3 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
206e4 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20  n is a column.  
206e5 43 6f 6d 70 61 72 65 20 74 68 65 0a 20 20 20 20  Compare the.    
206e6 2a 2a 20 72 65 73 75 6c 74 73 20 64 69 72 65 63  ** results direc
206e7 74 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  tly..    */.    
206e8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46  return SQLITE_AF
206e9 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b  F_NONE;.  }else{
206ea 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 73 69 64 65  .    /* One side
206eb 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68   is a column, th
206ec 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2e 20  e other is not. 
206ed 55 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20  Use the columns 
206ee 61 66 66 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20  affinity. */.   
206ef 20 61 73 73 65 72 74 28 20 61 66 66 31 3d 3d 30   assert( aff1==0
206f0 20 7c 7c 20 61 66 66 32 3d 3d 30 20 29 3b 0a 20   || aff2==0 );. 
206f1 20 20 20 72 65 74 75 72 6e 20 28 61 66 66 31 20     return (aff1 
206f2 2b 20 61 66 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a  + aff2);.  }.}..
206f3 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61  /*.** pExpr is a
206f4 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72   comparison oper
206f5 61 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 74 68  ator.  Return th
206f6 65 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20  e type affinity 
206f7 74 68 61 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62  that should.** b
206f8 65 20 61 70 70 6c 69 65 64 20 74 6f 20 62 6f 74  e applied to bot
206f9 68 20 6f 70 65 72 61 6e 64 73 20 70 72 69 6f 72  h operands prior
206fa 20 74 6f 20 64 6f 69 6e 67 20 74 68 65 20 63 6f   to doing the co
206fb 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61  mparison..*/.sta
206fc 74 69 63 20 63 68 61 72 20 63 6f 6d 70 61 72 69  tic char compari
206fd 73 6f 6e 41 66 66 69 6e 69 74 79 28 45 78 70 72  sonAffinity(Expr
206fe 20 2a 70 45 78 70 72 29 7b 0a 20 20 63 68 61 72   *pExpr){.  char
206ff 20 61 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20   aff;.  assert( 
20700 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51  pExpr->op==TK_EQ
20701 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54   || pExpr->op==T
20702 4b 5f 49 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f  K_IN || pExpr->o
20703 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20  p==TK_LT ||.    
20704 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d        pExpr->op=
20705 3d 54 4b 5f 47 54 20 7c 7c 20 70 45 78 70 72 2d  =TK_GT || pExpr-
20706 3e 6f 70 3d 3d 54 4b 5f 47 45 20 7c 7c 20 70 45  >op==TK_GE || pE
20707 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c  xpr->op==TK_LE |
20708 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70  |.          pExp
20709 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e 45 20 7c 7c 20  r->op==TK_NE || 
2070a 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53  pExpr->op==TK_IS
2070b 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54   || pExpr->op==T
2070c 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 61 73 73  K_ISNOT );.  ass
2070d 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66  ert( pExpr->pLef
2070e 74 20 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c  t );.  aff = sql
2070f 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
20710 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a  (pExpr->pLeft);.
20711 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69    if( pExpr->pRi
20712 67 68 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d  ght ){.    aff =
20713 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41   sqlite3CompareA
20714 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70  ffinity(pExpr->p
20715 52 69 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d  Right, aff);.  }
20716 65 6c 73 65 20 69 66 28 20 45 78 70 72 48 61 73  else if( ExprHas
20717 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
20718 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
20719 0a 20 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74  .    aff = sqlit
2071a 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74  e3CompareAffinit
2071b 79 28 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  y(pExpr->x.pSele
2071c 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d  ct->pEList->a[0]
2071d 2e 70 45 78 70 72 2c 20 61 66 66 29 3b 0a 20 20  .pExpr, aff);.  
2071e 7d 65 6c 73 65 20 69 66 28 20 21 61 66 66 20 29  }else if( !aff )
2071f 7b 0a 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49  {.    aff = SQLI
20720 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d  TE_AFF_NONE;.  }
20721 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d  .  return aff;.}
20722 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73  ../*.** pExpr is
20723 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78   a comparison ex
20724 70 72 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d  pression, eg. '=
20725 27 2c 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20  ', '<', IN(...) 
20726 65 74 63 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69  etc..** idx_affi
20727 6e 69 74 79 20 69 73 20 74 68 65 20 61 66 66 69  nity is the affi
20728 6e 69 74 79 20 6f 66 20 61 6e 20 69 6e 64 65 78  nity of an index
20729 65 64 20 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72  ed column. Retur
2072a 6e 20 74 72 75 65 0a 2a 2a 20 69 66 20 74 68 65  n true.** if the
2072b 20 69 6e 64 65 78 20 77 69 74 68 20 61 66 66 69   index with affi
2072c 6e 69 74 79 20 69 64 78 5f 61 66 66 69 6e 69 74  nity idx_affinit
2072d 79 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f  y may be used to
2072e 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68   implement.** th
2072f 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20  e comparison in 
20730 70 45 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pExpr..*/.SQLITE
20731 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
20732 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
20733 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c  yOk(Expr *pExpr,
20734 20 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69   char idx_affini
20735 74 79 29 7b 0a 20 20 63 68 61 72 20 61 66 66 20  ty){.  char aff 
20736 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69  = comparisonAffi
20737 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 20 20 73  nity(pExpr);.  s
20738 77 69 74 63 68 28 20 61 66 66 20 29 7b 0a 20 20  witch( aff ){.  
20739 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46    case SQLITE_AF
2073a 46 5f 4e 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65  F_NONE:.      re
2073b 74 75 72 6e 20 31 3b 0a 20 20 20 20 63 61 73 65  turn 1;.    case
2073c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
2073d 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  :.      return i
2073e 64 78 5f 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  dx_affinity==SQL
2073f 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20  ITE_AFF_TEXT;.  
20740 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
20741 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49   return sqlite3I
20742 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79  sNumericAffinity
20743 28 69 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a  (idx_affinity);.
20744 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
20745 75 72 6e 20 74 68 65 20 50 35 20 76 61 6c 75 65  urn the P5 value
20746 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
20747 75 73 65 64 20 66 6f 72 20 61 20 62 69 6e 61 72  used for a binar
20748 79 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20  y comparison.** 
20749 6f 70 63 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f  opcode (OP_Eq, O
2074a 50 5f 47 65 20 65 74 63 2e 29 20 75 73 65 64 20  P_Ge etc.) used 
2074b 74 6f 20 63 6f 6d 70 61 72 65 20 70 45 78 70 72  to compare pExpr
2074c 31 20 61 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f  1 and pExpr2..*/
2074d 0a 73 74 61 74 69 63 20 75 38 20 62 69 6e 61 72  .static u8 binar
2074e 79 43 6f 6d 70 61 72 65 50 35 28 45 78 70 72 20  yCompareP5(Expr 
2074f 2a 70 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70  *pExpr1, Expr *p
20750 45 78 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49  Expr2, int jumpI
20751 66 4e 75 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66  fNull){.  u8 aff
20752 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33   = (char)sqlite3
20753 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78  ExprAffinity(pEx
20754 70 72 32 29 3b 0a 20 20 61 66 66 20 3d 20 28 75  pr2);.  aff = (u
20755 38 29 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65  8)sqlite3Compare
20756 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 31 2c  Affinity(pExpr1,
20757 20 61 66 66 29 20 7c 20 28 75 38 29 6a 75 6d 70   aff) | (u8)jump
20758 49 66 4e 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e  IfNull;.  return
20759 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   aff;.}../*.** R
2075a 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
2075b 74 6f 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  to the collation
2075c 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20 73   sequence that s
2075d 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 62 79  hould be used by
2075e 0a 2a 2a 20 61 20 62 69 6e 61 72 79 20 63 6f 6d  .** a binary com
2075f 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
20760 20 63 6f 6d 70 61 72 69 6e 67 20 70 4c 65 66 74   comparing pLeft
20761 20 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a   and pRight..**.
20762 2a 2a 20 49 66 20 74 68 65 20 6c 65 66 74 20 68  ** If the left h
20763 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 20 68  and expression h
20764 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73  as a collating s
20765 65 71 75 65 6e 63 65 20 74 79 70 65 2c 20 74 68  equence type, th
20766 65 6e 20 69 74 20 69 73 0a 2a 2a 20 75 73 65 64  en it is.** used
20767 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20  . Otherwise the 
20768 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
20769 63 65 20 66 6f 72 20 74 68 65 20 72 69 67 68 74  ce for the right
2076a 20 68 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e   hand expression
2076b 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20 6f 72 20  .** is used, or 
2076c 74 68 65 20 64 65 66 61 75 6c 74 20 28 42 49 4e  the default (BIN
2076d 41 52 59 29 20 69 66 20 6e 65 69 74 68 65 72 20  ARY) if neither 
2076e 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61  expression has a
2076f 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79   collating.** ty
20770 70 65 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65  pe..**.** Argume
20771 6e 74 20 70 52 69 67 68 74 20 28 62 75 74 20 6e  nt pRight (but n
20772 6f 74 20 70 4c 65 66 74 29 20 6d 61 79 20 62 65  ot pLeft) may be
20773 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e   a null pointer.
20774 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 2a   In this case,.*
20775 2a 20 69 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73  * it is not cons
20776 69 64 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  idered..*/.SQLIT
20777 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
20778 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79  q *sqlite3Binary
20779 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a  CompareCollSeq(.
2077a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2077b 20 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c   .  Expr *pLeft,
2077c 20 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74   .  Expr *pRight
2077d 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  .){.  CollSeq *p
2077e 43 6f 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20  Coll;.  assert( 
2077f 70 4c 65 66 74 20 29 3b 0a 20 20 69 66 28 20 70  pLeft );.  if( p
20780 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50  Left->flags & EP
20781 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20  _ExpCollate ){. 
20782 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74     assert( pLeft
20783 2d 3e 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70  ->pColl );.    p
20784 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43  Coll = pLeft->pC
20785 6f 6c 6c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  oll;.  }else if(
20786 20 70 52 69 67 68 74 20 26 26 20 70 52 69 67 68   pRight && pRigh
20787 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  t->flags & EP_Ex
20788 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20  pCollate ){.    
20789 61 73 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e  assert( pRight->
2078a 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f  pColl );.    pCo
2078b 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f  ll = pRight->pCo
2078c 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ll;.  }else{.   
2078d 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
2078e 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
2078f 73 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20  se, pLeft);.    
20790 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20  if( !pColl ){.  
20791 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
20792 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
20793 50 61 72 73 65 2c 20 70 52 69 67 68 74 29 3b 0a  Parse, pRight);.
20794 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
20795 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  rn pColl;.}../*.
20796 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20  ** Generate the 
20797 6f 70 65 72 61 6e 64 73 20 66 6f 72 20 61 20 63  operands for a c
20798 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74  omparison operat
20799 69 6f 6e 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20  ion.  Before.** 
2079a 67 65 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63  generating the c
2079b 6f 64 65 20 66 6f 72 20 65 61 63 68 20 6f 70 65  ode for each ope
2079c 72 61 6e 64 2c 20 73 65 74 20 74 68 65 20 45 50  rand, set the EP
2079d 5f 41 6e 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20  _AnyAff.** flag 
2079e 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  on the expressio
2079f 6e 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c  n so that it wil
207a0 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 75 73 65  l be able to use
207a1 64 20 61 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f  d a.** cached co
207a2 6c 75 6d 6e 20 76 61 6c 75 65 20 74 68 61 74 20  lumn value that 
207a3 68 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 75  has previously u
207a4 6e 64 65 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61  ndergone an.** a
207a5 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a  ffinity change..
207a6 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
207a7 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
207a8 64 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ds(.  Parse *pPa
207a9 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69  rse,    /* Parsi
207aa 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  ng and code gene
207ab 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
207ac 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c  /.  Expr *pLeft,
207ad 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66        /* The lef
207ae 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  t operand */.  i
207af 6e 74 20 2a 70 52 65 67 4c 65 66 74 2c 20 20 20  nt *pRegLeft,   
207b0 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65   /* Register whe
207b1 72 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  re left operand 
207b2 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69  is stored */.  i
207b3 6e 74 20 2a 70 46 72 65 65 4c 65 66 74 2c 20 20  nt *pFreeLeft,  
207b4 20 2f 2a 20 46 72 65 65 20 74 68 69 73 20 72 65   /* Free this re
207b5 67 69 73 74 65 72 20 77 68 65 6e 20 64 6f 6e 65  gister when done
207b6 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67   */.  Expr *pRig
207b7 68 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72  ht,     /* The r
207b8 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  ight operand */.
207b9 20 20 69 6e 74 20 2a 70 52 65 67 52 69 67 68 74    int *pRegRight
207ba 2c 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ,   /* Register 
207bb 77 68 65 72 65 20 72 69 67 68 74 20 6f 70 65 72  where right oper
207bc 61 6e 64 20 69 73 20 73 74 6f 72 65 64 20 2a 2f  and is stored */
207bd 0a 20 20 69 6e 74 20 2a 70 46 72 65 65 52 69 67  .  int *pFreeRig
207be 68 74 20 20 20 2f 2a 20 57 72 69 74 65 20 74 65  ht   /* Write te
207bf 6d 70 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  mp register for 
207c0 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 74 68  right operand th
207c1 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c  ere */.){.  whil
207c2 65 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  e( pLeft->op==TK
207c3 5f 55 50 4c 55 53 20 29 20 70 4c 65 66 74 20 3d  _UPLUS ) pLeft =
207c4 20 70 4c 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20   pLeft->pLeft;. 
207c5 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d   pLeft->flags |=
207c6 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70   EP_AnyAff;.  *p
207c7 52 65 67 4c 65 66 74 20 3d 20 73 71 6c 69 74 65  RegLeft = sqlite
207c8 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
207c9 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 46 72  arse, pLeft, pFr
207ca 65 65 4c 65 66 74 29 3b 0a 20 20 77 68 69 6c 65  eeLeft);.  while
207cb 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b  ( pRight->op==TK
207cc 5f 55 50 4c 55 53 20 29 20 70 52 69 67 68 74 20  _UPLUS ) pRight 
207cd 3d 20 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b  = pRight->pLeft;
207ce 0a 20 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73  .  pRight->flags
207cf 20 7c 3d 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20   |= EP_AnyAff;. 
207d0 20 2a 70 52 65 67 52 69 67 68 74 20 3d 20 73 71   *pRegRight = sq
207d1 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
207d2 70 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  p(pParse, pRight
207d3 2c 20 70 46 72 65 65 52 69 67 68 74 29 3b 0a 7d  , pFreeRight);.}
207d4 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
207d5 20 63 6f 64 65 20 66 6f 72 20 61 20 63 6f 6d 70   code for a comp
207d6 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e  arison operator.
207d7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
207d8 6f 64 65 43 6f 6d 70 61 72 65 28 0a 20 20 50 61  odeCompare(.  Pa
207d9 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
207da 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 28  /* The parsing (
207db 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74  and code generat
207dc 69 6e 67 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  ing) context */.
207dd 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20    Expr *pLeft,  
207de 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20      /* The left 
207df 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70  operand */.  Exp
207e0 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f  r *pRight,     /
207e1 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 72  * The right oper
207e2 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63  and */.  int opc
207e3 6f 64 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  ode,       /* Th
207e4 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63  e comparison opc
207e5 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31  ode */.  int in1
207e6 2c 20 69 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65  , int in2, /* Re
207e7 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f  gister holding o
207e8 70 65 72 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74  perands */.  int
207e9 20 64 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f   dest,         /
207ea 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 74  * Jump here if t
207eb 72 75 65 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a  rue.  */.  int j
207ec 75 6d 70 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20  umpIfNull    /* 
207ed 49 66 20 74 72 75 65 2c 20 6a 75 6d 70 20 69 66  If true, jump if
207ee 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20   either operand 
207ef 69 73 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20  is NULL */.){.  
207f0 69 6e 74 20 70 35 3b 0a 20 20 69 6e 74 20 61 64  int p5;.  int ad
207f1 64 72 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  dr;.  CollSeq *p
207f2 34 3b 0a 0a 20 20 70 34 20 3d 20 73 71 6c 69 74  4;..  p4 = sqlit
207f3 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43  e3BinaryCompareC
207f4 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
207f5 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20  Left, pRight);. 
207f6 20 70 35 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70   p5 = binaryComp
207f7 61 72 65 50 35 28 70 4c 65 66 74 2c 20 70 52 69  areP5(pLeft, pRi
207f8 67 68 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  ght, jumpIfNull)
207f9 3b 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74  ;.  addr = sqlit
207fa 65 33 56 64 62 65 41 64 64 4f 70 34 28 70 50 61  e3VdbeAddOp4(pPa
207fb 72 73 65 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f  rse->pVdbe, opco
207fc 64 65 2c 20 69 6e 32 2c 20 64 65 73 74 2c 20 69  de, in2, dest, i
207fd 6e 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n1,.            
207fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
207ff 76 6f 69 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c  void*)p4, P4_COL
20800 4c 53 45 51 29 3b 0a 20 20 73 71 6c 69 74 65 33  LSEQ);.  sqlite3
20801 56 64 62 65 43 68 61 6e 67 65 50 35 28 70 50 61  VdbeChangeP5(pPa
20802 72 73 65 2d 3e 70 56 64 62 65 2c 20 28 75 38 29  rse->pVdbe, (u8)
20803 70 35 29 3b 0a 20 20 69 66 28 20 28 70 35 20 26  p5);.  if( (p5 &
20804 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b   SQLITE_AFF_MASK
20805 29 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  )!=SQLITE_AFF_NO
20806 4e 45 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  NE ){.    sqlite
20807 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
20808 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
20809 20 69 6e 31 2c 20 31 29 3b 0a 20 20 20 20 73 71   in1, 1);.    sq
2080a 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
2080b 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
2080c 72 73 65 2c 20 69 6e 32 2c 20 31 29 3b 0a 20 20  rse, in2, 1);.  
2080d 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b  }.  return addr;
2080e 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  .}..#if SQLITE_M
2080f 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a  AX_EXPR_DEPTH>0.
20810 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74  /*.** Check that
20811 20 61 72 67 75 6d 65 6e 74 20 6e 48 65 69 67 68   argument nHeigh
20812 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f  t is less than o
20813 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d  r equal to the m
20814 61 78 69 6d 75 6d 0a 2a 2a 20 65 78 70 72 65 73  aximum.** expres
20815 73 69 6f 6e 20 64 65 70 74 68 20 61 6c 6c 6f 77  sion depth allow
20816 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74  ed. If it is not
20817 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72  , leave an error
20818 20 6d 65 73 73 61 67 65 20 69 6e 0a 2a 2a 20 70   message in.** p
20819 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Parse..*/.SQLITE
2081a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2081b 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69  ite3ExprCheckHei
2081c 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  ght(Parse *pPars
2081d 65 2c 20 69 6e 74 20 6e 48 65 69 67 68 74 29 7b  e, int nHeight){
2081e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2081f 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6d 78 48  TE_OK;.  int mxH
20820 65 69 67 68 74 20 3d 20 70 50 61 72 73 65 2d 3e  eight = pParse->
20821 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
20822 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50  E_LIMIT_EXPR_DEP
20823 54 48 5d 3b 0a 20 20 69 66 28 20 6e 48 65 69 67  TH];.  if( nHeig
20824 68 74 3e 6d 78 48 65 69 67 68 74 20 29 7b 0a 20  ht>mxHeight ){. 
20825 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
20826 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
20827 20 20 20 22 45 78 70 72 65 73 73 69 6f 6e 20 74     "Expression t
20828 72 65 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65  ree is too large
20829 20 28 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20   (maximum depth 
2082a 25 64 29 22 2c 20 6d 78 48 65 69 67 68 74 0a 20  %d)", mxHeight. 
2082b 20 20 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 53     );.    rc = S
2082c 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2082d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2082e 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ./* The followin
2082f 67 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e  g three function
20830 73 2c 20 68 65 69 67 68 74 4f 66 45 78 70 72 28  s, heightOfExpr(
20831 29 2c 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c  ), heightOfExprL
20832 69 73 74 28 29 0a 2a 2a 20 61 6e 64 20 68 65 69  ist().** and hei
20833 67 68 74 4f 66 53 65 6c 65 63 74 28 29 2c 20 61  ghtOfSelect(), a
20834 72 65 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  re used to deter
20835 6d 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 6d  mine the maximum
20836 20 68 65 69 67 68 74 0a 2a 2a 20 6f 66 20 61 6e   height.** of an
20837 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  y expression tre
20838 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20  e referenced by 
20839 74 68 65 20 73 74 72 75 63 74 75 72 65 20 70 61  the structure pa
2083a 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 66  ssed as the.** f
2083b 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  irst argument..*
2083c 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d 61 78  *.** If this max
2083d 69 6d 75 6d 20 68 65 69 67 68 74 20 69 73 20 67  imum height is g
2083e 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
2083f 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 70 6f  current value po
20840 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70  inted.** to by p
20841 6e 48 65 69 67 68 74 2c 20 74 68 65 20 73 65 63  nHeight, the sec
20842 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74  ond parameter, t
20843 68 65 6e 20 73 65 74 20 2a 70 6e 48 65 69 67 68  hen set *pnHeigh
20844 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c  t to that.** val
20845 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ue..*/.static vo
20846 69 64 20 68 65 69 67 68 74 4f 66 45 78 70 72 28  id heightOfExpr(
20847 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e  Expr *p, int *pn
20848 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70  Height){.  if( p
20849 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e   ){.    if( p->n
2084a 48 65 69 67 68 74 3e 2a 70 6e 48 65 69 67 68 74  Height>*pnHeight
2084b 20 29 7b 0a 20 20 20 20 20 20 2a 70 6e 48 65 69   ){.      *pnHei
2084c 67 68 74 20 3d 20 70 2d 3e 6e 48 65 69 67 68 74  ght = p->nHeight
2084d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74  ;.    }.  }.}.st
2084e 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74  atic void height
2084f 4f 66 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c  OfExprList(ExprL
20850 69 73 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48  ist *p, int *pnH
20851 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20  eight){.  if( p 
20852 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
20853 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
20854 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
20855 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28     heightOfExpr(
20856 70 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70  p->a[i].pExpr, p
20857 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a  nHeight);.    }.
20858 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
20859 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74  d heightOfSelect
2085a 28 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20  (Select *p, int 
2085b 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66  *pnHeight){.  if
2085c 28 20 70 20 29 7b 0a 20 20 20 20 68 65 69 67 68  ( p ){.    heigh
2085d 74 4f 66 45 78 70 72 28 70 2d 3e 70 57 68 65 72  tOfExpr(p->pWher
2085e 65 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20  e, pnHeight);.  
2085f 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70    heightOfExpr(p
20860 2d 3e 70 48 61 76 69 6e 67 2c 20 70 6e 48 65 69  ->pHaving, pnHei
20861 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74  ght);.    height
20862 4f 66 45 78 70 72 28 70 2d 3e 70 4c 69 6d 69 74  OfExpr(p->pLimit
20863 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20  , pnHeight);.   
20864 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d   heightOfExpr(p-
20865 3e 70 4f 66 66 73 65 74 2c 20 70 6e 48 65 69 67  >pOffset, pnHeig
20866 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f  ht);.    heightO
20867 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c  fExprList(p->pEL
20868 69 73 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a  ist, pnHeight);.
20869 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72      heightOfExpr
2086a 4c 69 73 74 28 70 2d 3e 70 47 72 6f 75 70 42 79  List(p->pGroupBy
2086b 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20  , pnHeight);.   
2086c 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73   heightOfExprLis
2086d 74 28 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70  t(p->pOrderBy, p
2086e 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65  nHeight);.    he
2086f 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e  ightOfSelect(p->
20870 70 50 72 69 6f 72 2c 20 70 6e 48 65 69 67 68 74  pPrior, pnHeight
20871 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
20872 53 65 74 20 74 68 65 20 45 78 70 72 2e 6e 48 65  Set the Expr.nHe
20873 69 67 68 74 20 76 61 72 69 61 62 6c 65 20 69 6e  ight variable in
20874 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 70   the structure p
20875 61 73 73 65 64 20 61 73 20 61 6e 20 0a 2a 2a 20  assed as an .** 
20876 61 72 67 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70  argument. An exp
20877 72 65 73 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20  ression with no 
20878 63 68 69 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70  children, Expr.p
20879 4c 69 73 74 20 6f 72 20 0a 2a 2a 20 45 78 70 72  List or .** Expr
2087a 2e 70 53 65 6c 65 63 74 20 6d 65 6d 62 65 72 20  .pSelect member 
2087b 68 61 73 20 61 20 68 65 69 67 68 74 20 6f 66 20  has a height of 
2087c 31 2e 20 41 6e 79 20 6f 74 68 65 72 20 65 78 70  1. Any other exp
2087d 72 65 73 73 69 6f 6e 0a 2a 2a 20 68 61 73 20 61  ression.** has a
2087e 20 68 65 69 67 68 74 20 65 71 75 61 6c 20 74 6f   height equal to
2087f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69   the maximum hei
20880 67 68 74 20 6f 66 20 61 6e 79 20 6f 74 68 65 72  ght of any other
20881 20 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20   .** referenced 
20882 45 78 70 72 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a  Expr plus one..*
20883 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78  /.static void ex
20884 70 72 53 65 74 48 65 69 67 68 74 28 45 78 70 72  prSetHeight(Expr
20885 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69   *p){.  int nHei
20886 67 68 74 20 3d 20 30 3b 0a 20 20 68 65 69 67 68  ght = 0;.  heigh
20887 74 4f 66 45 78 70 72 28 70 2d 3e 70 4c 65 66 74  tOfExpr(p->pLeft
20888 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68  , &nHeight);.  h
20889 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70  eightOfExpr(p->p
2088a 52 69 67 68 74 2c 20 26 6e 48 65 69 67 68 74 29  Right, &nHeight)
2088b 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50  ;.  if( ExprHasP
2088c 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49  roperty(p, EP_xI
2088d 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
2088e 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70  heightOfSelect(p
2088f 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 26 6e 48  ->x.pSelect, &nH
20890 65 69 67 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  eight);.  }else{
20891 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70  .    heightOfExp
20892 72 4c 69 73 74 28 70 2d 3e 78 2e 70 4c 69 73 74  rList(p->x.pList
20893 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d  , &nHeight);.  }
20894 0a 20 20 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20  .  p->nHeight = 
20895 6e 48 65 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a  nHeight + 1;.}..
20896 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78  /*.** Set the Ex
20897 70 72 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61  pr.nHeight varia
20898 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 65 78  ble using the ex
20899 70 72 53 65 74 48 65 69 67 68 74 28 29 20 66 75  prSetHeight() fu
2089a 6e 63 74 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68  nction. If.** th
2089b 65 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61  e height is grea
2089c 74 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78  ter than the max
2089d 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70  imum allowed exp
2089e 72 65 73 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a  ression depth,.*
2089f 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72  * leave an error
208a0 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53   in pParse..*/.S
208a1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
208a2 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 53 65  id sqlite3ExprSe
208a3 74 48 65 69 67 68 74 28 50 61 72 73 65 20 2a 70  tHeight(Parse *p
208a4 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 29 7b  Parse, Expr *p){
208a5 0a 20 20 65 78 70 72 53 65 74 48 65 69 67 68 74  .  exprSetHeight
208a6 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  (p);.  sqlite3Ex
208a7 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 70 50  prCheckHeight(pP
208a8 61 72 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74  arse, p->nHeight
208a9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
208aa 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68  rn the maximum h
208ab 65 69 67 68 74 20 6f 66 20 61 6e 79 20 65 78 70  eight of any exp
208ac 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66  ression tree ref
208ad 65 72 65 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68  erenced.** by th
208ae 65 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65  e select stateme
208af 6e 74 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  nt passed as an 
208b0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  argument..*/.SQL
208b1 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
208b2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
208b3 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a  rHeight(Select *
208b4 70 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68  p){.  int nHeigh
208b5 74 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f  t = 0;.  heightO
208b6 66 53 65 6c 65 63 74 28 70 2c 20 26 6e 48 65 69  fSelect(p, &nHei
208b7 67 68 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e  ght);.  return n
208b8 48 65 69 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a  Height;.}.#else.
208b9 20 20 23 64 65 66 69 6e 65 20 65 78 70 72 53 65    #define exprSe
208ba 74 48 65 69 67 68 74 28 79 29 0a 23 65 6e 64 69  tHeight(y).#endi
208bb 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f  f /* SQLITE_MAX_
208bc 45 58 50 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a  EXPR_DEPTH>0 */.
208bd 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
208be 69 6e 65 20 69 73 20 74 68 65 20 63 6f 72 65 20  ine is the core 
208bf 61 6c 6c 6f 63 61 74 6f 72 20 66 6f 72 20 45 78  allocator for Ex
208c0 70 72 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20  pr nodes..**.** 
208c1 43 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20  Construct a new 
208c2 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
208c3 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
208c4 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 4d 65 6d  nter to it.  Mem
208c5 6f 72 79 0a 2a 2a 20 66 6f 72 20 74 68 69 73 20  ory.** for this 
208c6 6e 6f 64 65 20 61 6e 64 20 66 6f 72 20 74 68 65  node and for the
208c7 20 70 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74   pToken argument
208c8 20 69 73 20 61 20 73 69 6e 67 6c 65 20 61 6c 6c   is a single all
208c9 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 62 74 61 69  ocation.** obtai
208ca 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
208cb 44 62 4d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65  DbMalloc().  The
208cc 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
208cd 6e 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69  n.** is responsi
208ce 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73  ble for making s
208cf 75 72 65 20 74 68 65 20 6e 6f 64 65 20 65 76 65  ure the node eve
208d0 6e 74 75 61 6c 6c 79 20 67 65 74 73 20 66 72 65  ntually gets fre
208d1 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 65 71  ed..**.** If deq
208d2 75 6f 74 65 20 69 73 20 74 72 75 65 2c 20 74 68  uote is true, th
208d3 65 6e 20 74 68 65 20 74 6f 6b 65 6e 20 28 69 66  en the token (if
208d4 20 69 74 20 65 78 69 73 74 73 29 20 69 73 20 64   it exists) is d
208d5 65 71 75 6f 74 65 64 2e 0a 2a 2a 20 49 66 20 64  equoted..** If d
208d6 65 71 75 6f 74 65 20 69 73 20 66 61 6c 73 65 2c  equote is false,
208d7 20 6e 6f 20 64 65 71 75 6f 74 69 6e 67 20 69 73   no dequoting is
208d8 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 54   performance.  T
208d9 68 65 20 64 65 51 75 6f 74 65 0a 2a 2a 20 70 61  he deQuote.** pa
208da 72 61 6d 65 74 65 72 20 69 73 20 69 67 6e 6f 72  rameter is ignor
208db 65 64 20 69 66 20 70 54 6f 6b 65 6e 20 69 73 20  ed if pToken is 
208dc 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 74  NULL or if the t
208dd 6f 6b 65 6e 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a  oken does not.**
208de 20 61 70 70 65 61 72 20 74 6f 20 62 65 20 71 75   appear to be qu
208df 6f 74 65 64 2e 20 20 49 66 20 74 68 65 20 71 75  oted.  If the qu
208e0 6f 74 65 73 20 77 65 72 65 20 6f 66 20 74 68 65  otes were of the
208e1 20 66 6f 72 6d 20 22 2e 2e 2e 22 20 28 64 6f 75   form "..." (dou
208e2 62 6c 65 2d 71 75 6f 74 65 73 29 0a 2a 2a 20 74  ble-quotes).** t
208e3 68 65 6e 20 74 68 65 20 45 50 5f 44 62 6c 51 75  hen the EP_DblQu
208e4 6f 74 65 64 20 66 6c 61 67 20 69 73 20 73 65 74  oted flag is set
208e5 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   on the expressi
208e6 6f 6e 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53  on node..**.** S
208e7 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 49 66  pecial case:  If
208e8 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20   op==TK_INTEGER 
208e9 61 6e 64 20 70 54 6f 6b 65 6e 20 70 6f 69 6e 74  and pToken point
208ea 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 74 68  s to a string th
208eb 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 74 72 61  at.** can be tra
208ec 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 33  nslated into a 3
208ed 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 74  2-bit integer, t
208ee 68 65 6e 20 74 68 65 20 74 6f 6b 65 6e 20 69 73  hen the token is
208ef 20 6e 6f 74 0a 2a 2a 20 73 74 6f 72 65 64 20 69   not.** stored i
208f0 6e 20 75 2e 7a 54 6f 6b 65 6e 2e 20 20 49 6e 73  n u.zToken.  Ins
208f1 74 65 61 64 2c 20 74 68 65 20 69 6e 74 65 67 65  tead, the intege
208f2 72 20 76 61 6c 75 65 73 20 69 73 20 77 72 69 74  r values is writ
208f3 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 75 2e 69 56  ten.** into u.iV
208f4 61 6c 75 65 20 61 6e 64 20 74 68 65 20 45 50 5f  alue and the EP_
208f5 49 6e 74 56 61 6c 75 65 20 66 6c 61 67 20 69 73  IntValue flag is
208f6 20 73 65 74 2e 20 20 4e 6f 20 65 78 74 72 61 20   set.  No extra 
208f7 73 74 6f 72 61 67 65 0a 2a 2a 20 69 73 20 61 6c  storage.** is al
208f8 6c 6f 63 61 74 65 64 20 74 6f 20 68 6f 6c 64 20  located to hold 
208f9 74 68 65 20 69 6e 74 65 67 65 72 20 74 65 78 74  the integer text
208fa 20 61 6e 64 20 74 68 65 20 64 65 71 75 6f 74 65   and the dequote
208fb 20 66 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64   flag is ignored
208fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
208fd 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
208fe 33 45 78 70 72 41 6c 6c 6f 63 28 0a 20 20 73 71  3ExprAlloc(.  sq
208ff 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
20900 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20        /* Handle 
20901 66 6f 72 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  for sqlite3DbMal
20902 6c 6f 63 5a 65 72 6f 28 29 20 28 6d 61 79 20 62  locZero() (may b
20903 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74  e null) */.  int
20904 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   op,            
20905 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
20906 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 63  on opcode */.  c
20907 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  onst Token *pTok
20908 65 6e 2c 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20  en,    /* Token 
20909 61 72 67 75 6d 65 6e 74 2e 20 20 4d 69 67 68 74  argument.  Might
2090a 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e   be NULL */.  in
2090b 74 20 64 65 71 75 6f 74 65 20 20 20 20 20 20 20  t dequote       
2090c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
2090d 20 64 65 71 75 6f 74 65 20 2a 2f 0a 29 7b 0a 20   dequote */.){. 
2090e 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 69   Expr *pNew;.  i
2090f 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 3b 0a 20  nt nExtra = 0;. 
20910 20 69 6e 74 20 69 56 61 6c 75 65 20 3d 20 30 3b   int iValue = 0;
20911 0a 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29  ..  if( pToken )
20912 7b 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b  {.    if( op!=TK
20913 5f 49 4e 54 45 47 45 52 20 7c 7c 20 70 54 6f 6b  _INTEGER || pTok
20914 65 6e 2d 3e 7a 3d 3d 30 0a 20 20 20 20 20 20 20  en->z==0.       
20915 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 47 65 74     || sqlite3Get
20916 49 6e 74 33 32 28 70 54 6f 6b 65 6e 2d 3e 7a 2c  Int32(pToken->z,
20917 20 26 69 56 61 6c 75 65 29 3d 3d 30 20 29 7b 0a   &iValue)==0 ){.
20918 20 20 20 20 20 20 6e 45 78 74 72 61 20 3d 20 70        nExtra = p
20919 54 6f 6b 65 6e 2d 3e 6e 2b 31 3b 0a 20 20 20 20  Token->n+1;.    
2091a 7d 0a 20 20 7d 0a 20 20 70 4e 65 77 20 3d 20 73  }.  }.  pNew = s
2091b 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
2091c 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78  ro(db, sizeof(Ex
2091d 70 72 29 2b 6e 45 78 74 72 61 29 3b 0a 20 20 69  pr)+nExtra);.  i
2091e 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70  f( pNew ){.    p
2091f 4e 65 77 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70  New->op = (u8)op
20920 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 69 41 67 67  ;.    pNew->iAgg
20921 20 3d 20 2d 31 3b 0a 20 20 20 20 69 66 28 20 70   = -1;.    if( p
20922 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 69  Token ){.      i
20923 66 28 20 6e 45 78 74 72 61 3d 3d 30 20 29 7b 0a  f( nExtra==0 ){.
20924 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c          pNew->fl
20925 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c  ags |= EP_IntVal
20926 75 65 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  ue;.        pNew
20927 2d 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 56 61  ->u.iValue = iVa
20928 6c 75 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  lue;.      }else
20929 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b  {.        int c;
2092a 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75  .        pNew->u
2092b 2e 7a 54 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a  .zToken = (char*
2092c 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20  )&pNew[1];.     
2092d 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e     memcpy(pNew->
2092e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e  u.zToken, pToken
2092f 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b  ->z, pToken->n);
20930 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75  .        pNew->u
20931 2e 7a 54 6f 6b 65 6e 5b 70 54 6f 6b 65 6e 2d 3e  .zToken[pToken->
20932 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  n] = 0;.        
20933 69 66 28 20 64 65 71 75 6f 74 65 20 26 26 20 6e  if( dequote && n
20934 45 78 74 72 61 3e 3d 33 20 0a 20 20 20 20 20 20  Extra>=3 .      
20935 20 20 20 20 20 20 20 26 26 20 28 28 63 20 3d 20         && ((c = 
20936 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27  pToken->z[0])=='
20937 5c 27 27 20 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c  \'' || c=='"' ||
20938 20 63 3d 3d 27 5b 27 20 7c 7c 20 63 3d 3d 27 60   c=='[' || c=='`
20939 27 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ') ){.          
2093a 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 70  sqlite3Dequote(p
2093b 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a  New->u.zToken);.
2093c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3d            if( c=
2093d 3d 27 22 27 20 29 20 70 4e 65 77 2d 3e 66 6c 61  ='"' ) pNew->fla
2093e 67 73 20 7c 3d 20 45 50 5f 44 62 6c 51 75 6f 74  gs |= EP_DblQuot
2093f 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ed;.        }.  
20940 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20      }.    }.#if 
20941 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
20942 44 45 50 54 48 3e 30 0a 20 20 20 20 70 4e 65 77  DEPTH>0.    pNew
20943 2d 3e 6e 48 65 69 67 68 74 20 3d 20 31 3b 0a 23  ->nHeight = 1;.#
20944 65 6e 64 69 66 20 20 0a 20 20 7d 0a 20 20 72 65  endif  .  }.  re
20945 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn pNew;.}../*
20946 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
20947 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f  ew expression no
20948 64 65 20 66 72 6f 6d 20 61 20 7a 65 72 6f 2d 74  de from a zero-t
20949 65 72 6d 69 6e 61 74 65 64 20 74 6f 6b 65 6e 20  erminated token 
2094a 74 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65  that has.** alre
2094b 61 64 79 20 62 65 65 6e 20 64 65 71 75 6f 74 65  ady been dequote
2094c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
2094d 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
2094e 65 33 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65  e3Expr(.  sqlite
2094f 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
20950 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20    /* Handle for 
20951 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
20952 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e 75  ero() (may be nu
20953 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c  ll) */.  int op,
20954 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20955 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f   /* Expression o
20956 70 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  pcode */.  const
20957 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 20 20   char *zToken   
20958 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75     /* Token argu
20959 6d 65 6e 74 2e 20 20 4d 69 67 68 74 20 62 65 20  ment.  Might be 
2095a 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 6f 6b  NULL */.){.  Tok
2095b 65 6e 20 78 3b 0a 20 20 78 2e 7a 20 3d 20 7a 54  en x;.  x.z = zT
2095c 6f 6b 65 6e 3b 0a 20 20 78 2e 6e 20 3d 20 7a 54  oken;.  x.n = zT
2095d 6f 6b 65 6e 20 3f 20 73 71 6c 69 74 65 33 53 74  oken ? sqlite3St
2095e 72 6c 65 6e 33 30 28 7a 54 6f 6b 65 6e 29 20 3a  rlen30(zToken) :
2095f 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c   0;.  return sql
20960 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62  ite3ExprAlloc(db
20961 2c 20 6f 70 2c 20 26 78 2c 20 30 29 3b 0a 7d 0a  , op, &x, 0);.}.
20962 0a 2f 2a 0a 2a 2a 20 41 74 74 61 63 68 20 73 75  ./*.** Attach su
20963 62 74 72 65 65 73 20 70 4c 65 66 74 20 61 6e 64  btrees pLeft and
20964 20 70 52 69 67 68 74 20 74 6f 20 74 68 65 20 45   pRight to the E
20965 78 70 72 20 6e 6f 64 65 20 70 52 6f 6f 74 2e 0a  xpr node pRoot..
20966 2a 2a 0a 2a 2a 20 49 66 20 70 52 6f 6f 74 3d 3d  **.** If pRoot==
20967 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20  NULL that means 
20968 74 68 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c  that a memory al
20969 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68  location error h
2096a 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20  as occurred..** 
2096b 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 64 65  In that case, de
2096c 6c 65 74 65 20 74 68 65 20 73 75 62 74 72 65 65  lete the subtree
2096d 73 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  s pLeft and pRig
2096e 68 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ht..*/.SQLITE_PR
2096f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
20970 65 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74  e3ExprAttachSubt
20971 72 65 65 73 28 0a 20 20 73 71 6c 69 74 65 33 20  rees(.  sqlite3 
20972 2a 64 62 2c 0a 20 20 45 78 70 72 20 2a 70 52 6f  *db,.  Expr *pRo
20973 6f 74 2c 0a 20 20 45 78 70 72 20 2a 70 4c 65 66  ot,.  Expr *pLef
20974 74 2c 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68  t,.  Expr *pRigh
20975 74 0a 29 7b 0a 20 20 69 66 28 20 70 52 6f 6f 74  t.){.  if( pRoot
20976 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
20977 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
20978 6c 65 64 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  led );.    sqlit
20979 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2097a 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 6c   pLeft);.    sql
2097b 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2097c 62 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 7d 65  b, pRight);.  }e
2097d 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 52 69  lse{.    if( pRi
2097e 67 68 74 20 29 7b 0a 20 20 20 20 20 20 70 52 6f  ght ){.      pRo
2097f 6f 74 2d 3e 70 52 69 67 68 74 20 3d 20 70 52 69  ot->pRight = pRi
20980 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ght;.      if( p
20981 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45  Right->flags & E
20982 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a  P_ExpCollate ){.
20983 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66          pRoot->f
20984 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f  lags |= EP_ExpCo
20985 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70  llate;.        p
20986 52 6f 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52  Root->pColl = pR
20987 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  ight->pColl;.   
20988 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
20989 66 28 20 70 4c 65 66 74 20 29 7b 0a 20 20 20 20  f( pLeft ){.    
2098a 20 20 70 52 6f 6f 74 2d 3e 70 4c 65 66 74 20 3d    pRoot->pLeft =
2098b 20 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 69 66   pLeft;.      if
2098c 28 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26  ( pLeft->flags &
2098d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29   EP_ExpCollate )
2098e 7b 0a 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d  {.        pRoot-
2098f 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70  >flags |= EP_Exp
20990 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20  Collate;.       
20991 20 70 52 6f 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20   pRoot->pColl = 
20992 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20  pLeft->pColl;.  
20993 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
20994 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70 52  exprSetHeight(pR
20995 6f 6f 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  oot);.  }.}../*.
20996 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 45 78  ** Allocate a Ex
20997 70 72 20 6e 6f 64 65 20 77 68 69 63 68 20 6a 6f  pr node which jo
20998 69 6e 73 20 61 73 20 6d 61 6e 79 20 61 73 20 74  ins as many as t
20999 77 6f 20 73 75 62 74 72 65 65 73 2e 0a 2a 2a 0a  wo subtrees..**.
2099a 2a 2a 20 4f 6e 65 20 6f 72 20 62 6f 74 68 20 6f  ** One or both o
2099b 66 20 74 68 65 20 73 75 62 74 72 65 65 73 20 63  f the subtrees c
2099c 61 6e 20 62 65 20 4e 55 4c 4c 2e 20 20 52 65 74  an be NULL.  Ret
2099d 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
2099e 20 74 68 65 20 6e 65 77 0a 2a 2a 20 45 78 70 72   the new.** Expr
2099f 20 6e 6f 64 65 2e 20 20 4f 72 2c 20 69 66 20 61   node.  Or, if a
209a0 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75  n OOM error occu
209a1 72 73 2c 20 73 65 74 20 70 50 61 72 73 65 2d 3e  rs, set pParse->
209a2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
209a3 2c 0a 2a 2a 20 66 72 65 65 20 74 68 65 20 73 75  ,.** free the su
209a4 62 74 72 65 65 73 20 61 6e 64 20 72 65 74 75 72  btrees and retur
209a5 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54  n NULL..*/.SQLIT
209a6 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
209a7 73 71 6c 69 74 65 33 50 45 78 70 72 28 0a 20 20  sqlite3PExpr(.  
209a8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
209a9 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
209aa 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
209ab 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
209ac 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
209ad 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a  ssion opcode */.
209ae 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20    Expr *pLeft,  
209af 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66            /* Lef
209b0 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45  t operand */.  E
209b1 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20  xpr *pRight,    
209b2 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20         /* Right 
209b3 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e  operand */.  con
209b4 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  st Token *pToken
209b5 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
209b6 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45   token */.){.  E
209b7 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  xpr *p = sqlite3
209b8 45 78 70 72 41 6c 6c 6f 63 28 70 50 61 72 73 65  ExprAlloc(pParse
209b9 2d 3e 64 62 2c 20 6f 70 2c 20 70 54 6f 6b 65 6e  ->db, op, pToken
209ba 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  , 1);.  sqlite3E
209bb 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65 65  xprAttachSubtree
209bc 73 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 2c  s(pParse->db, p,
209bd 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b   pLeft, pRight);
209be 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
209bf 2f 2a 0a 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65  /*.** Join two e
209c0 78 70 72 65 73 73 69 6f 6e 73 20 75 73 69 6e 67  xpressions using
209c1 20 61 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72   an AND operator
209c2 2e 20 20 49 66 20 65 69 74 68 65 72 20 65 78 70  .  If either exp
209c3 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55  ression is.** NU
209c4 4c 4c 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65  LL, then just re
209c5 74 75 72 6e 20 74 68 65 20 6f 74 68 65 72 20 65  turn the other e
209c6 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51  xpression..*/.SQ
209c7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
209c8 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e  r *sqlite3ExprAn
209c9 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45  d(sqlite3 *db, E
209ca 78 70 72 20 2a 70 4c 65 66 74 2c 20 45 78 70 72  xpr *pLeft, Expr
209cb 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 69 66 28   *pRight){.  if(
209cc 20 70 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20   pLeft==0 ){.   
209cd 20 72 65 74 75 72 6e 20 70 52 69 67 68 74 3b 0a   return pRight;.
209ce 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 69 67    }else if( pRig
209cf 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ht==0 ){.    ret
209d0 75 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c  urn pLeft;.  }el
209d1 73 65 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e  se{.    Expr *pN
209d2 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
209d3 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 41 4e 44  Alloc(db, TK_AND
209d4 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
209d5 69 74 65 33 45 78 70 72 41 74 74 61 63 68 53 75  ite3ExprAttachSu
209d6 62 74 72 65 65 73 28 64 62 2c 20 70 4e 65 77 2c  btrees(db, pNew,
209d7 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b   pLeft, pRight);
209d8 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4e 65 77  .    return pNew
209d9 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
209da 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 65  onstruct a new e
209db 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66  xpression node f
209dc 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69  or a function wi
209dd 74 68 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61  th multiple.** a
209de 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c  rguments..*/.SQL
209df 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
209e0 20 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e   *sqlite3ExprFun
209e1 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61  ction(Parse *pPa
209e2 72 73 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70  rse, ExprList *p
209e3 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f  List, Token *pTo
209e4 6b 65 6e 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e  ken){.  Expr *pN
209e5 65 77 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ew;.  sqlite3 *d
209e6 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
209e7 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e    assert( pToken
209e8 20 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c   );.  pNew = sql
209e9 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62  ite3ExprAlloc(db
209ea 2c 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 2c 20 70  , TK_FUNCTION, p
209eb 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20 20 69 66 28  Token, 1);.  if(
209ec 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   pNew==0 ){.    
209ed 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
209ee 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29  elete(db, pList)
209ef 3b 20 2f 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72  ; /* Avoid memor
209f0 79 20 6c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c  y leak when mall
209f1 6f 63 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20  oc fails */.    
209f2 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
209f3 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20  pNew->x.pList = 
209f4 70 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28  pList;.  assert(
209f5 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
209f6 79 28 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65  y(pNew, EP_xIsSe
209f7 6c 65 63 74 29 20 29 3b 0a 20 20 73 71 6c 69 74  lect) );.  sqlit
209f8 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
209f9 70 50 61 72 73 65 2c 20 70 4e 65 77 29 3b 0a 20  pParse, pNew);. 
209fa 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
209fb 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 61 20  ./*.** Assign a 
209fc 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20  variable number 
209fd 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  to an expression
209fe 20 74 68 61 74 20 65 6e 63 6f 64 65 73 20 61 20   that encodes a 
209ff 77 69 6c 64 63 61 72 64 0a 2a 2a 20 69 6e 20 74  wildcard.** in t
20a00 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20  he original SQL 
20a01 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a  statement.  .**.
20a02 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 63 6f 6e  ** Wildcards con
20a03 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 69 6e  sisting of a sin
20a04 67 6c 65 20 22 3f 22 20 61 72 65 20 61 73 73 69  gle "?" are assi
20a05 67 6e 65 64 20 74 68 65 20 6e 65 78 74 20 73 65  gned the next se
20a06 71 75 65 6e 74 69 61 6c 0a 2a 2a 20 76 61 72 69  quential.** vari
20a07 61 62 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a  able number..**.
20a08 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20  ** Wildcards of 
20a09 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20  the form "?nnn" 
20a0a 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65  are assigned the
20a0b 20 6e 75 6d 62 65 72 20 22 6e 6e 6e 22 2e 20 20   number "nnn".  
20a0c 57 65 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20  We make.** sure 
20a0d 22 6e 6e 6e 22 20 69 73 20 6e 6f 74 20 74 6f 6f  "nnn" is not too
20a0e 20 62 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64   be to avoid a d
20a0f 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65  enial of service
20a10 20 61 74 74 61 63 6b 20 77 68 65 6e 0a 2a 2a 20   attack when.** 
20a11 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
20a12 74 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 6e 20  t comes from an 
20a13 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 2e  external source.
20a14 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73  .**.** Wildcards
20a15 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3a 61   of the form ":a
20a16 61 61 22 2c 20 22 40 61 61 61 22 2c 20 6f 72 20  aa", "@aaa", or 
20a17 22 24 61 61 61 22 20 61 72 65 20 61 73 73 69 67  "$aaa" are assig
20a18 6e 65 64 20 74 68 65 20 73 61 6d 65 20 6e 75 6d  ned the same num
20a19 62 65 72 0a 2a 2a 20 61 73 20 74 68 65 20 70 72  ber.** as the pr
20a1a 65 76 69 6f 75 73 20 69 6e 73 74 61 6e 63 65 20  evious instance 
20a1b 6f 66 20 74 68 65 20 73 61 6d 65 20 77 69 6c 64  of the same wild
20a1c 63 61 72 64 2e 20 20 4f 72 20 69 66 20 74 68 69  card.  Or if thi
20a1d 73 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a  s is the first.*
20a1e 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
20a1f 65 20 77 69 6c 64 63 61 72 64 2c 20 74 68 65 20  e wildcard, the 
20a20 6e 65 78 74 20 73 65 71 75 65 6e 69 61 6c 20 76  next sequenial v
20a21 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 69  ariable number i
20a22 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64 2e 0a 2a  s.** assigned..*
20a23 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20a24 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
20a25 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72  rAssignVarNumber
20a26 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
20a27 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20  Expr *pExpr){.  
20a28 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
20a29 61 72 73 65 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73  arse->db;.  cons
20a2a 74 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 69 66  t char *z;..  if
20a2b 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74  ( pExpr==0 ) ret
20a2c 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 21  urn;.  assert( !
20a2d 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
20a2e 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74  ty(pExpr, EP_Int
20a2f 56 61 6c 75 65 7c 45 50 5f 52 65 64 75 63 65 64  Value|EP_Reduced
20a30 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29  |EP_TokenOnly) )
20a31 3b 0a 20 20 7a 20 3d 20 70 45 78 70 72 2d 3e 75  ;.  z = pExpr->u
20a32 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72  .zToken;.  asser
20a33 74 28 20 7a 21 3d 30 20 29 3b 0a 20 20 61 73 73  t( z!=0 );.  ass
20a34 65 72 74 28 20 7a 5b 30 5d 21 3d 30 20 29 3b 0a  ert( z[0]!=0 );.
20a35 20 20 69 66 28 20 7a 5b 31 5d 3d 3d 30 20 29 7b    if( z[1]==0 ){
20a36 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64  .    /* Wildcard
20a37 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22   of the form "?"
20a38 2e 20 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65  .  Assign the ne
20a39 78 74 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  xt variable numb
20a3a 65 72 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  er */.    assert
20a3b 28 20 7a 5b 30 5d 3d 3d 27 3f 27 20 29 3b 0a 20  ( z[0]=='?' );. 
20a3c 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
20a3d 6e 20 3d 20 28 79 6e 56 61 72 29 28 2b 2b 70 50  n = (ynVar)(++pP
20a3e 61 72 73 65 2d 3e 6e 56 61 72 29 3b 0a 20 20 7d  arse->nVar);.  }
20a3f 65 6c 73 65 20 69 66 28 20 7a 5b 30 5d 3d 3d 27  else if( z[0]=='
20a40 3f 27 20 29 7b 0a 20 20 20 20 2f 2a 20 57 69 6c  ?' ){.    /* Wil
20a41 64 63 61 72 64 20 6f 66 20 74 68 65 20 66 6f 72  dcard of the for
20a42 6d 20 22 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65  m "?nnn".  Conve
20a43 72 74 20 22 6e 6e 6e 22 20 74 6f 20 61 6e 20 69  rt "nnn" to an i
20a44 6e 74 65 67 65 72 20 61 6e 64 0a 20 20 20 20 2a  nteger and.    *
20a45 2a 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20  * use it as the 
20a46 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20  variable number 
20a47 2a 2f 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 61  */.    int i = a
20a48 74 6f 69 28 28 63 68 61 72 2a 29 26 7a 5b 31 5d  toi((char*)&z[1]
20a49 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 43  );.    pExpr->iC
20a4a 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56 61 72 29 69  olumn = (ynVar)i
20a4b 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
20a4c 69 3d 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74  i==0 );.    test
20a4d 63 61 73 65 28 20 69 3d 3d 31 20 29 3b 0a 20 20  case( i==1 );.  
20a4e 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 64    testcase( i==d
20a4f 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
20a50 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f  _LIMIT_VARIABLE_
20a51 4e 55 4d 42 45 52 5d 2d 31 20 29 3b 0a 20 20 20  NUMBER]-1 );.   
20a52 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 64 62   testcase( i==db
20a53 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
20a54 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
20a55 55 4d 42 45 52 5d 20 29 3b 0a 20 20 20 20 69 66  UMBER] );.    if
20a56 28 20 69 3c 31 20 7c 7c 20 69 3e 64 62 2d 3e 61  ( i<1 || i>db->a
20a57 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
20a58 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  IT_VARIABLE_NUMB
20a59 45 52 5d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ER] ){.      sql
20a5a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
20a5b 72 73 65 2c 20 22 76 61 72 69 61 62 6c 65 20 6e  rse, "variable n
20a5c 75 6d 62 65 72 20 6d 75 73 74 20 62 65 20 62 65  umber must be be
20a5d 74 77 65 65 6e 20 3f 31 20 61 6e 64 20 3f 25 64  tween ?1 and ?%d
20a5e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  ",.          db-
20a5f 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
20a60 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55  IMIT_VARIABLE_NU
20a61 4d 42 45 52 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  MBER]);.    }.  
20a62 20 20 69 66 28 20 69 3e 70 50 61 72 73 65 2d 3e    if( i>pParse->
20a63 6e 56 61 72 20 29 7b 0a 20 20 20 20 20 20 70 50  nVar ){.      pP
20a64 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 69 3b 0a  arse->nVar = i;.
20a65 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
20a66 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 73 20     /* Wildcards 
20a67 6c 69 6b 65 20 22 3a 61 61 61 22 2c 20 22 24 61  like ":aaa", "$a
20a68 61 61 22 20 6f 72 20 22 40 61 61 61 22 2e 20 20  aa" or "@aaa".  
20a69 52 65 75 73 65 20 74 68 65 20 73 61 6d 65 20 76  Reuse the same v
20a6a 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 6e  ariable.    ** n
20a6b 75 6d 62 65 72 20 61 73 20 74 68 65 20 70 72 69  umber as the pri
20a6c 6f 72 20 61 70 70 65 61 72 61 6e 63 65 20 6f 66  or appearance of
20a6d 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20   the same name, 
20a6e 6f 72 20 69 66 20 74 68 65 20 6e 61 6d 65 0a 20  or if the name. 
20a6f 20 20 20 2a 2a 20 68 61 73 20 6e 65 76 65 72 20     ** has never 
20a70 61 70 70 65 61 72 65 64 20 62 65 66 6f 72 65 2c  appeared before,
20a71 20 72 65 75 73 65 20 74 68 65 20 73 61 6d 65 20   reuse the same 
20a72 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 0a  variable number.
20a73 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69      */.    int i
20a74 3b 0a 20 20 20 20 75 33 32 20 6e 3b 0a 20 20 20  ;.    u32 n;.   
20a75 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
20a76 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 66 6f 72  en30(z);.    for
20a77 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e  (i=0; i<pParse->
20a78 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  nVarExpr; i++){.
20a79 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d        Expr *pE =
20a7a 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78   pParse->apVarEx
20a7b 70 72 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73  pr[i];.      ass
20a7c 65 72 74 28 20 70 45 21 3d 30 20 29 3b 0a 20 20  ert( pE!=0 );.  
20a7d 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70      if( memcmp(p
20a7e 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 7a 2c 20  E->u.zToken, z, 
20a7f 6e 29 3d 3d 30 20 26 26 20 70 45 2d 3e 75 2e 7a  n)==0 && pE->u.z
20a80 54 6f 6b 65 6e 5b 6e 5d 3d 3d 30 20 29 7b 0a 20  Token[n]==0 ){. 
20a81 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43         pExpr->iC
20a82 6f 6c 75 6d 6e 20 3d 20 70 45 2d 3e 69 43 6f 6c  olumn = pE->iCol
20a83 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 62 72 65  umn;.        bre
20a84 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
20a85 7d 0a 20 20 20 20 69 66 28 20 69 3e 3d 70 50 61  }.    if( i>=pPa
20a86 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 20 29 7b  rse->nVarExpr ){
20a87 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43  .      pExpr->iC
20a88 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56 61 72 29 28  olumn = (ynVar)(
20a89 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 29 3b  ++pParse->nVar);
20a8a 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73  .      if( pPars
20a8b 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70 50 61  e->nVarExpr>=pPa
20a8c 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c  rse->nVarExprAll
20a8d 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  oc-1 ){.        
20a8e 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
20a8f 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73 65 2d  Alloc += pParse-
20a90 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b  >nVarExprAlloc +
20a91 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70 50 61   10;.        pPa
20a92 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 20 3d  rse->apVarExpr =
20a93 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
20a94 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
20a95 72 65 65 28 0a 20 20 20 20 20 20 20 20 20 20 20  ree(.           
20a96 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 20 20     db,.         
20a97 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56       pParse->apV
20a98 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20  arExpr,.        
20a99 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56        pParse->nV
20a9a 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69 7a 65  arExprAlloc*size
20a9b 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56 61 72  of(pParse->apVar
20a9c 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20 20 20  Expr[0]).       
20a9d 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a       );.      }.
20a9e 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d        if( !db->m
20a9f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
20aa0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
20aa1 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
20aa2 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70  !=0 );.        p
20aa3 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
20aa4 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70  [pParse->nVarExp
20aa5 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a 20 20  r++] = pExpr;.  
20aa6 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20      }.    }.  } 
20aa7 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e  .  if( !pParse->
20aa8 6e 45 72 72 20 26 26 20 70 50 61 72 73 65 2d 3e  nErr && pParse->
20aa9 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  nVar>db->aLimit[
20aaa 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
20aab 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b  IABLE_NUMBER] ){
20aac 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
20aad 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f  rMsg(pParse, "to
20aae 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 69 61  o many SQL varia
20aaf 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  bles");.  }.}../
20ab0 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20 65 78  *.** Clear an ex
20ab1 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75  pression structu
20ab2 72 65 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74  re without delet
20ab3 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75 72  ing the structur
20ab4 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53 75 62  e itself..** Sub
20ab5 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c  structure is del
20ab6 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eted..*/.SQLITE_
20ab7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
20ab8 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 71  ite3ExprClear(sq
20ab9 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20  lite3 *db, Expr 
20aba 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
20abb 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 45 78  !=0 );.  if( !Ex
20abc 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
20abd 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  (p, EP_TokenOnly
20abe 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
20abf 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
20ac0 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71  ->pLeft);.    sq
20ac1 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
20ac2 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a  db, p->pRight);.
20ac3 20 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73      if( !ExprHas
20ac4 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52  Property(p, EP_R
20ac5 65 64 75 63 65 64 29 20 26 26 20 28 70 2d 3e 66  educed) && (p->f
20ac6 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c  lags2 & EP2_Mall
20ac7 6f 63 65 64 54 6f 6b 65 6e 29 21 3d 30 20 29 7b  ocedToken)!=0 ){
20ac8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
20ac9 46 72 65 65 28 64 62 2c 20 70 2d 3e 75 2e 7a 54  Free(db, p->u.zT
20aca 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20  oken);.    }.   
20acb 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
20acc 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65  erty(p, EP_xIsSe
20acd 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 73  lect) ){.      s
20ace 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
20acf 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 6c  te(db, p->x.pSel
20ad0 65 63 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ect);.    }else{
20ad1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
20ad2 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
20ad3 20 70 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20   p->x.pList);.  
20ad4 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
20ad5 20 52 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c   Recursively del
20ad6 65 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f  ete an expressio
20ad7 6e 20 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54  n tree..*/.SQLIT
20ad8 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
20ad9 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
20ada 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78  (sqlite3 *db, Ex
20adb 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  pr *p){.  if( p=
20adc 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  =0 ) return;.  s
20add 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28  qlite3ExprClear(
20ade 64 62 2c 20 70 29 3b 0a 20 20 69 66 28 20 21 45  db, p);.  if( !E
20adf 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20ae0 2c 20 45 50 5f 53 74 61 74 69 63 29 20 29 7b 0a  , EP_Static) ){.
20ae1 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
20ae2 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a  e(db, p);.  }.}.
20ae3 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
20ae4 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
20ae5 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
20ae6 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73  the expression s
20ae7 74 72 75 63 74 75 72 65 20 0a 2a 2a 20 70 61 73  tructure .** pas
20ae8 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
20ae9 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20   argument. This 
20aea 69 73 20 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66  is always one of
20aeb 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 0a   EXPR_FULLSIZE,.
20aec 2a 2a 20 45 58 50 52 5f 52 45 44 55 43 45 44 53  ** EXPR_REDUCEDS
20aed 49 5a 45 20 6f 72 20 45 58 50 52 5f 54 4f 4b 45  IZE or EXPR_TOKE
20aee 4e 4f 4e 4c 59 53 49 5a 45 2e 0a 2a 2f 0a 73 74  NONLYSIZE..*/.st
20aef 61 74 69 63 20 69 6e 74 20 65 78 70 72 53 74 72  atic int exprStr
20af0 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70 29  uctSize(Expr *p)
20af1 7b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50  {.  if( ExprHasP
20af2 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f  roperty(p, EP_To
20af3 6b 65 6e 4f 6e 6c 79 29 20 29 20 72 65 74 75 72  kenOnly) ) retur
20af4 6e 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59  n EXPR_TOKENONLY
20af5 53 49 5a 45 3b 0a 20 20 69 66 28 20 45 78 70 72  SIZE;.  if( Expr
20af6 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45  HasProperty(p, E
20af7 50 5f 52 65 64 75 63 65 64 29 20 29 20 72 65 74  P_Reduced) ) ret
20af8 75 72 6e 20 45 58 50 52 5f 52 45 44 55 43 45 44  urn EXPR_REDUCED
20af9 53 49 5a 45 3b 0a 20 20 72 65 74 75 72 6e 20 45  SIZE;.  return E
20afa 58 50 52 5f 46 55 4c 4c 53 49 5a 45 3b 0a 7d 0a  XPR_FULLSIZE;.}.
20afb 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 75 70 65 64  ./*.** The duped
20afc 45 78 70 72 2a 53 69 7a 65 28 29 20 72 6f 75 74  Expr*Size() rout
20afd 69 6e 65 73 20 65 61 63 68 20 72 65 74 75 72 6e  ines each return
20afe 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
20aff 79 74 65 73 20 72 65 71 75 69 72 65 64 0a 2a 2a  ytes required.**
20b00 20 74 6f 20 73 74 6f 72 65 20 61 20 63 6f 70 79   to store a copy
20b01 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f   of an expressio
20b02 6e 20 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20  n or expression 
20b03 74 72 65 65 2e 20 20 54 68 65 79 20 64 69 66 66  tree.  They diff
20b04 65 72 20 69 6e 0a 2a 2a 20 68 6f 77 20 6d 75 63  er in.** how muc
20b05 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 69 73  h of the tree is
20b06 20 6d 65 61 73 75 72 65 64 2e 0a 2a 2a 0a 2a 2a   measured..**.**
20b07 20 20 20 20 20 64 75 70 65 64 45 78 70 72 53 74       dupedExprSt
20b08 72 75 63 74 53 69 7a 65 28 29 20 20 20 20 20 53  ructSize()     S
20b09 69 7a 65 20 6f 66 20 6f 6e 6c 79 20 74 68 65 20  ize of only the 
20b0a 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20 0a  Expr structure .
20b0b 2a 2a 20 20 20 20 20 64 75 70 65 64 45 78 70 72  **     dupedExpr
20b0c 4e 6f 64 65 53 69 7a 65 28 29 20 20 20 20 20 20  NodeSize()      
20b0d 20 53 69 7a 65 20 6f 66 20 45 78 70 72 20 2b 20   Size of Expr + 
20b0e 73 70 61 63 65 20 66 6f 72 20 74 6f 6b 65 6e 0a  space for token.
20b0f 2a 2a 20 20 20 20 20 64 75 70 65 64 45 78 70 72  **     dupedExpr
20b10 53 69 7a 65 28 29 20 20 20 20 20 20 20 20 20 20  Size()          
20b11 20 45 78 70 72 20 2b 20 74 6f 6b 65 6e 20 2b 20   Expr + token + 
20b12 73 75 62 74 72 65 65 20 63 6f 6d 70 6f 6e 65 6e  subtree componen
20b13 74 73 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ts.**.**********
20b14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b18 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 75 70 65  *.**.** The dupe
20b19 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28  dExprStructSize(
20b1a 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  ) function retur
20b1b 6e 73 20 74 77 6f 20 76 61 6c 75 65 73 20 4f 52  ns two values OR
20b1c 2d 65 64 20 74 6f 67 65 74 68 65 72 3a 20 20 0a  -ed together:  .
20b1d 2a 2a 20 28 31 29 20 74 68 65 20 73 70 61 63 65  ** (1) the space
20b1e 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 20   required for a 
20b1f 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 72  copy of the Expr
20b20 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20   structure only 
20b21 61 6e 64 20 0a 2a 2a 20 28 32 29 20 74 68 65 20  and .** (2) the 
20b22 45 50 5f 78 78 78 20 66 6c 61 67 73 20 74 68 61  EP_xxx flags tha
20b23 74 20 69 6e 64 69 63 61 74 65 20 77 68 61 74 20  t indicate what 
20b24 74 68 65 20 73 74 72 75 63 74 75 72 65 20 73 69  the structure si
20b25 7a 65 20 73 68 6f 75 6c 64 20 62 65 2e 0a 2a 2a  ze should be..**
20b26 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
20b27 65 73 20 69 73 20 61 6c 77 61 79 73 20 6f 6e 65  es is always one
20b28 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   of:.**.**      
20b29 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 0a 2a 2a  EXPR_FULLSIZE.**
20b2a 20 20 20 20 20 20 45 58 50 52 5f 52 45 44 55 43        EXPR_REDUC
20b2b 45 44 53 49 5a 45 20 20 20 7c 20 45 50 5f 52 65  EDSIZE   | EP_Re
20b2c 64 75 63 65 64 0a 2a 2a 20 20 20 20 20 20 45 58  duced.**      EX
20b2d 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45  PR_TOKENONLYSIZE
20b2e 20 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 0a   | EP_TokenOnly.
20b2f 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f  **.** The size o
20b30 66 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  f the structure 
20b31 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 62 79 20  can be found by 
20b32 6d 61 73 6b 69 6e 67 20 74 68 65 20 72 65 74 75  masking the retu
20b33 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74  rn value.** of t
20b34 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68  his routine with
20b35 20 30 78 66 66 66 2e 20 20 54 68 65 20 66 6c 61   0xfff.  The fla
20b36 67 73 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 20  gs can be found 
20b37 62 79 20 6d 61 73 6b 69 6e 67 20 74 68 65 0a 2a  by masking the.*
20b38 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 77  * return value w
20b39 69 74 68 20 45 50 5f 52 65 64 75 63 65 64 7c 45  ith EP_Reduced|E
20b3a 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 2e 0a 2a 2a 0a  P_TokenOnly..**.
20b3b 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 74  ** Note that wit
20b3c 68 20 66 6c 61 67 73 3d 3d 45 58 50 52 44 55 50  h flags==EXPRDUP
20b3d 5f 52 45 44 55 43 45 2c 20 74 68 69 73 20 72 6f  _REDUCE, this ro
20b3e 75 74 69 6e 65 73 20 77 6f 72 6b 73 20 6f 6e 20  utines works on 
20b3f 66 75 6c 6c 2d 73 69 7a 65 0a 2a 2a 20 28 75 6e  full-size.** (un
20b40 72 65 64 75 63 65 64 29 20 45 78 70 72 20 6f 62  reduced) Expr ob
20b41 6a 65 63 74 73 20 61 73 20 74 68 65 79 20 6f 72  jects as they or
20b42 20 6f 72 69 67 69 6e 61 6c 6c 79 20 63 6f 6e 73   originally cons
20b43 74 72 75 63 74 65 64 20 62 79 20 74 68 65 20 70  tructed by the p
20b44 61 72 73 65 72 2e 0a 2a 2a 20 44 75 72 69 6e 67  arser..** During
20b45 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 61 6c   expression anal
20b46 79 73 69 73 2c 20 65 78 74 72 61 20 69 6e 66 6f  ysis, extra info
20b47 72 6d 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 75  rmation is compu
20b48 74 65 64 20 61 6e 64 20 6d 6f 76 65 64 20 69 6e  ted and moved in
20b49 74 6f 0a 2a 2a 20 6c 61 74 65 72 20 70 61 72 74  to.** later part
20b4a 73 20 6f 66 20 74 65 68 20 45 78 70 72 20 6f 62  s of teh Expr ob
20b4b 6a 65 63 74 20 61 6e 64 20 74 68 61 74 20 65 78  ject and that ex
20b4c 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  tra information 
20b4d 6d 69 67 68 74 20 67 65 74 20 63 68 6f 70 70 65  might get choppe
20b4e 64 0a 2a 2a 20 6f 66 66 20 69 66 20 74 68 65 20  d.** off if the 
20b4f 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65  expression is re
20b50 64 75 63 65 64 2e 20 20 4e 6f 74 65 20 61 6c 73  duced.  Note als
20b51 6f 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e  o that it does n
20b52 6f 74 20 77 6f 72 6b 20 74 6f 0a 2a 2a 20 6d 61  ot work to.** ma
20b53 6b 65 20 61 20 45 58 50 52 44 55 50 5f 52 45 44  ke a EXPRDUP_RED
20b54 55 43 45 20 63 6f 70 79 20 6f 66 20 61 20 72 65  UCE copy of a re
20b55 64 75 63 65 64 20 65 78 70 72 65 73 73 69 6f 6e  duced expression
20b56 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 6c 65  .  It is only le
20b57 67 61 6c 0a 2a 2a 20 74 6f 20 72 65 64 75 63 65  gal.** to reduce
20b58 20 61 20 70 72 69 73 74 69 6e 65 20 65 78 70 72   a pristine expr
20b59 65 73 73 69 6f 6e 20 74 72 65 65 20 66 72 6f 6d  ession tree from
20b5a 20 74 68 65 20 70 61 72 73 65 72 2e 20 20 54 68   the parser.  Th
20b5b 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
20b5c 0a 2a 2a 20 6f 66 20 64 75 70 65 64 45 78 70 72  .** of dupedExpr
20b5d 53 74 72 75 63 74 53 69 7a 65 28 29 20 63 6f 6e  StructSize() con
20b5e 74 61 69 6e 20 6d 75 6c 74 69 70 6c 65 20 61 73  tain multiple as
20b5f 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
20b60 73 20 74 68 61 74 20 61 74 74 65 6d 70 74 0a 2a  s that attempt.*
20b61 2a 20 74 6f 20 65 6e 66 6f 72 63 65 20 74 68 69  * to enforce thi
20b62 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f  s constraint..*/
20b63 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65  .static int dupe
20b64 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28  dExprStructSize(
20b65 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61  Expr *p, int fla
20b66 67 73 29 7b 0a 20 20 69 6e 74 20 6e 53 69 7a 65  gs){.  int nSize
20b67 3b 0a 20 20 61 73 73 65 72 74 28 20 66 6c 61 67  ;.  assert( flag
20b68 73 3d 3d 45 58 50 52 44 55 50 5f 52 45 44 55 43  s==EXPRDUP_REDUC
20b69 45 20 7c 7c 20 66 6c 61 67 73 3d 3d 30 20 29 3b  E || flags==0 );
20b6a 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 66 6c 61   /* Only one fla
20b6b 67 20 76 61 6c 75 65 20 61 6c 6c 6f 77 65 64 20  g value allowed 
20b6c 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 28 66 6c 61  */.  if( 0==(fla
20b6d 67 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43  gs&EXPRDUP_REDUC
20b6e 45 29 20 29 7b 0a 20 20 20 20 6e 53 69 7a 65 20  E) ){.    nSize 
20b6f 3d 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 3b  = EXPR_FULLSIZE;
20b70 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
20b71 73 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e  sert( !ExprHasAn
20b72 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  yProperty(p, EP_
20b73 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64  TokenOnly|EP_Red
20b74 75 63 65 64 29 20 29 3b 0a 20 20 20 20 61 73 73  uced) );.    ass
20b75 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
20b76 70 65 72 74 79 28 70 2c 20 45 50 5f 46 72 6f 6d  perty(p, EP_From
20b77 4a 6f 69 6e 29 20 29 3b 20 0a 20 20 20 20 61 73  Join) ); .    as
20b78 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 32  sert( (p->flags2
20b79 20 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54   & EP2_MallocedT
20b7a 6f 6b 65 6e 29 3d 3d 30 20 29 3b 0a 20 20 20 20  oken)==0 );.    
20b7b 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67  assert( (p->flag
20b7c 73 32 20 26 20 45 50 32 5f 49 72 72 65 64 75 63  s2 & EP2_Irreduc
20b7d 69 62 6c 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20  ible)==0 );.    
20b7e 69 66 28 20 70 2d 3e 70 4c 65 66 74 20 7c 7c 20  if( p->pLeft || 
20b7f 70 2d 3e 70 52 69 67 68 74 20 7c 7c 20 70 2d 3e  p->pRight || p->
20b80 70 43 6f 6c 6c 20 7c 7c 20 70 2d 3e 78 2e 70 4c  pColl || p->x.pL
20b81 69 73 74 20 29 7b 0a 20 20 20 20 20 20 6e 53 69  ist ){.      nSi
20b82 7a 65 20 3d 20 45 58 50 52 5f 52 45 44 55 43 45  ze = EXPR_REDUCE
20b83 44 53 49 5a 45 20 7c 20 45 50 5f 52 65 64 75 63  DSIZE | EP_Reduc
20b84 65 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ed;.    }else{. 
20b85 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50       nSize = EXP
20b86 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20  R_TOKENONLYSIZE 
20b87 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 3b 0a  | EP_TokenOnly;.
20b88 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
20b89 72 6e 20 6e 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a  rn nSize;.}../*.
20b8a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
20b8b 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 70 61   returns the spa
20b8c 63 65 20 69 6e 20 62 79 74 65 73 20 72 65 71 75  ce in bytes requ
20b8d 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  ired to store th
20b8e 65 20 63 6f 70 79 20 0a 2a 2a 20 6f 66 20 74 68  e copy .** of th
20b8f 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65  e Expr structure
20b90 20 61 6e 64 20 61 20 63 6f 70 79 20 6f 66 20 74   and a copy of t
20b91 68 65 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65 6e  he Expr.u.zToken
20b92 20 73 74 72 69 6e 67 20 28 69 66 20 74 68 61 74   string (if that
20b93 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 64 65  .** string is de
20b94 66 69 6e 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69  fined.).*/.stati
20b95 63 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 4e  c int dupedExprN
20b96 6f 64 65 53 69 7a 65 28 45 78 70 72 20 2a 70 2c  odeSize(Expr *p,
20b97 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69   int flags){.  i
20b98 6e 74 20 6e 42 79 74 65 20 3d 20 64 75 70 65 64  nt nByte = duped
20b99 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 70  ExprStructSize(p
20b9a 2c 20 66 6c 61 67 73 29 20 26 20 30 78 66 66 66  , flags) & 0xfff
20b9b 3b 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73  ;.  if( !ExprHas
20b9c 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49  Property(p, EP_I
20b9d 6e 74 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75  ntValue) && p->u
20b9e 2e 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 6e  .zToken ){.    n
20b9f 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 53  Byte += sqlite3S
20ba0 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f  trlen30(p->u.zTo
20ba1 6b 65 6e 29 2b 31 3b 0a 20 20 7d 0a 20 20 72 65  ken)+1;.  }.  re
20ba2 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 42 79 74  turn ROUND8(nByt
20ba3 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  e);.}../*.** Ret
20ba4 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
20ba5 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64  f bytes required
20ba6 20 74 6f 20 63 72 65 61 74 65 20 61 20 64 75 70   to create a dup
20ba7 6c 69 63 61 74 65 20 6f 66 20 74 68 65 20 0a 2a  licate of the .*
20ba8 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 73  * expression pas
20ba9 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
20baa 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 73   argument. The s
20bab 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
20bac 73 20 61 0a 2a 2a 20 6d 61 73 6b 20 63 6f 6e 74  s a.** mask cont
20bad 61 69 6e 69 6e 67 20 45 58 50 52 44 55 50 5f 58  aining EXPRDUP_X
20bae 58 58 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20  XX flags..**.** 
20baf 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
20bb0 65 64 20 69 6e 63 6c 75 64 65 73 20 73 70 61 63  ed includes spac
20bb1 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f  e to create a co
20bb2 70 79 20 6f 66 20 74 68 65 20 45 78 70 72 20 73  py of the Expr s
20bb3 74 72 75 63 74 0a 2a 2a 20 69 74 73 65 6c 66 20  truct.** itself 
20bb4 61 6e 64 20 74 68 65 20 62 75 66 66 65 72 20 72  and the buffer r
20bb5 65 66 65 72 72 65 64 20 74 6f 20 62 79 20 45 78  eferred to by Ex
20bb6 70 72 2e 75 2e 7a 54 6f 6b 65 6e 2c 20 69 66 20  pr.u.zToken, if 
20bb7 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  any..**.** If th
20bb8 65 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  e EXPRDUP_REDUCE
20bb9 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
20bba 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  en the return va
20bbb 6c 75 65 20 69 6e 63 6c 75 64 65 73 20 0a 2a 2a  lue includes .**
20bbc 20 73 70 61 63 65 20 74 6f 20 64 75 70 6c 69 63   space to duplic
20bbd 61 74 65 20 61 6c 6c 20 45 78 70 72 20 6e 6f 64  ate all Expr nod
20bbe 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 20 66  es in the tree f
20bbf 6f 72 6d 65 64 20 62 79 20 45 78 70 72 2e 70 4c  ormed by Expr.pL
20bc0 65 66 74 20 0a 2a 2a 20 61 6e 64 20 45 78 70 72  eft .** and Expr
20bc1 2e 70 52 69 67 68 74 20 76 61 72 69 61 62 6c 65  .pRight variable
20bc2 73 20 28 62 75 74 20 6e 6f 74 20 66 6f 72 20 61  s (but not for a
20bc3 6e 79 20 73 74 72 75 63 74 75 72 65 73 20 70 6f  ny structures po
20bc4 69 6e 74 65 64 20 74 6f 20 6f 72 20 0a 2a 2a 20  inted to or .** 
20bc5 64 65 73 63 65 6e 64 65 64 20 66 72 6f 6d 20 74  descended from t
20bc6 68 65 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20  he Expr.x.pList 
20bc7 6f 72 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63  or Expr.x.pSelec
20bc8 74 20 76 61 72 69 61 62 6c 65 73 29 2e 0a 2a 2f  t variables)..*/
20bc9 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65  .static int dupe
20bca 64 45 78 70 72 53 69 7a 65 28 45 78 70 72 20 2a  dExprSize(Expr *
20bcb 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  p, int flags){. 
20bcc 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b 0a   int nByte = 0;.
20bcd 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e    if( p ){.    n
20bce 42 79 74 65 20 3d 20 64 75 70 65 64 45 78 70 72  Byte = dupedExpr
20bcf 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67  NodeSize(p, flag
20bd0 73 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67  s);.    if( flag
20bd1 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  s&EXPRDUP_REDUCE
20bd2 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20   ){.      nByte 
20bd3 2b 3d 20 64 75 70 65 64 45 78 70 72 53 69 7a 65  += dupedExprSize
20bd4 28 70 2d 3e 70 4c 65 66 74 2c 20 66 6c 61 67 73  (p->pLeft, flags
20bd5 29 20 2b 20 64 75 70 65 64 45 78 70 72 53 69 7a  ) + dupedExprSiz
20bd6 65 28 70 2d 3e 70 52 69 67 68 74 2c 20 66 6c 61  e(p->pRight, fla
20bd7 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  gs);.    }.  }. 
20bd8 20 72 65 74 75 72 6e 20 6e 42 79 74 65 3b 0a 7d   return nByte;.}
20bd9 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
20bda 63 74 69 6f 6e 20 69 73 20 73 69 6d 69 6c 61 72  ction is similar
20bdb 20 74 6f 20 73 71 6c 69 74 65 33 45 78 70 72 44   to sqlite3ExprD
20bdc 75 70 28 29 2c 20 65 78 63 65 70 74 20 74 68 61  up(), except tha
20bdd 74 20 69 66 20 70 7a 42 75 66 66 65 72 20 0a 2a  t if pzBuffer .*
20bde 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68  * is not NULL th
20bdf 65 6e 20 2a 70 7a 42 75 66 66 65 72 20 69 73 20  en *pzBuffer is 
20be0 61 73 73 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74  assumed to point
20be1 20 74 6f 20 61 20 62 75 66 66 65 72 20 6c 61 72   to a buffer lar
20be2 67 65 20 65 6e 6f 75 67 68 20 0a 2a 2a 20 74 6f  ge enough .** to
20be3 20 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20   store the copy 
20be4 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70 2c  of expression p,
20be5 20 74 68 65 20 63 6f 70 69 65 73 20 6f 66 20 70   the copies of p
20be6 2d 3e 75 2e 7a 54 6f 6b 65 6e 0a 2a 2a 20 28 69  ->u.zToken.** (i
20be7 66 20 61 70 70 6c 69 63 61 62 6c 65 29 2c 20 61  f applicable), a
20be8 6e 64 20 74 68 65 20 63 6f 70 69 65 73 20 6f 66  nd the copies of
20be9 20 74 68 65 20 70 2d 3e 70 4c 65 66 74 20 61 6e   the p->pLeft an
20bea 64 20 70 2d 3e 70 52 69 67 68 74 20 65 78 70 72  d p->pRight expr
20beb 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 69 66 20 61  essions,.** if a
20bec 6e 79 2e 20 42 65 66 6f 72 65 20 72 65 74 75 72  ny. Before retur
20bed 6e 69 6e 67 2c 20 2a 70 7a 42 75 66 66 65 72 20  ning, *pzBuffer 
20bee 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 66 69  is set to the fi
20bef 72 73 74 20 62 79 74 65 20 70 61 73 73 65 64 20  rst byte passed 
20bf0 74 68 65 0a 2a 2a 20 70 6f 72 74 69 6f 6e 20 6f  the.** portion o
20bf1 66 20 74 68 65 20 62 75 66 66 65 72 20 63 6f 70  f the buffer cop
20bf2 69 65 64 20 69 6e 74 6f 20 62 79 20 74 68 69 73  ied into by this
20bf3 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
20bf4 61 74 69 63 20 45 78 70 72 20 2a 65 78 70 72 44  atic Expr *exprD
20bf5 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
20bf6 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61  Expr *p, int fla
20bf7 67 73 2c 20 75 38 20 2a 2a 70 7a 42 75 66 66 65  gs, u8 **pzBuffe
20bf8 72 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77  r){.  Expr *pNew
20bf9 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
20bfa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
20bfb 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  lue to return */
20bfc 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
20bfd 63 6f 6e 73 74 20 69 6e 74 20 69 73 52 65 64 75  const int isRedu
20bfe 63 65 64 20 3d 20 28 66 6c 61 67 73 26 45 58 50  ced = (flags&EXP
20bff 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20  RDUP_REDUCE);.  
20c00 20 20 75 38 20 2a 7a 41 6c 6c 6f 63 3b 0a 20 20    u8 *zAlloc;.  
20c01 20 20 75 33 32 20 73 74 61 74 69 63 46 6c 61 67    u32 staticFlag
20c02 20 3d 20 30 3b 0a 0a 20 20 20 20 61 73 73 65 72   = 0;..    asser
20c03 74 28 20 70 7a 42 75 66 66 65 72 3d 3d 30 20 7c  t( pzBuffer==0 |
20c04 7c 20 69 73 52 65 64 75 63 65 64 20 29 3b 0a 0a  | isReduced );..
20c05 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75      /* Figure ou
20c06 74 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65  t where to write
20c07 20 74 68 65 20 6e 65 77 20 45 78 70 72 20 73 74   the new Expr st
20c08 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 20 20  ructure. */.    
20c09 69 66 28 20 70 7a 42 75 66 66 65 72 20 29 7b 0a  if( pzBuffer ){.
20c0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d 20 2a        zAlloc = *
20c0b 70 7a 42 75 66 66 65 72 3b 0a 20 20 20 20 20 20  pzBuffer;.      
20c0c 73 74 61 74 69 63 46 6c 61 67 20 3d 20 45 50 5f  staticFlag = EP_
20c0d 53 74 61 74 69 63 3b 0a 20 20 20 20 7d 65 6c 73  Static;.    }els
20c0e 65 7b 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20  e{.      zAlloc 
20c0f 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
20c10 63 52 61 77 28 64 62 2c 20 64 75 70 65 64 45 78  cRaw(db, dupedEx
20c11 70 72 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29  prSize(p, flags)
20c12 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65  );.    }.    pNe
20c13 77 20 3d 20 28 45 78 70 72 20 2a 29 7a 41 6c 6c  w = (Expr *)zAll
20c14 6f 63 3b 0a 0a 20 20 20 20 69 66 28 20 70 4e 65  oc;..    if( pNe
20c15 77 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65  w ){.      /* Se
20c16 74 20 6e 4e 65 77 53 69 7a 65 20 74 6f 20 74 68  t nNewSize to th
20c17 65 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 65 64  e size allocated
20c18 20 66 6f 72 20 74 68 65 20 73 74 72 75 63 74 75   for the structu
20c19 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 0a 20 20  re pointed to.  
20c1a 20 20 20 20 2a 2a 20 62 79 20 70 4e 65 77 2e 20      ** by pNew. 
20c1b 54 68 69 73 20 69 73 20 65 69 74 68 65 72 20 45  This is either E
20c1c 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 20 45 58  XPR_FULLSIZE, EX
20c1d 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f  PR_REDUCEDSIZE o
20c1e 72 0a 20 20 20 20 20 20 2a 2a 20 45 58 50 52 5f  r.      ** EXPR_
20c1f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 20 6e  TOKENONLYSIZE. n
20c20 54 6f 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20  Token is set to 
20c21 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
20c22 74 65 73 20 63 6f 6e 73 75 6d 65 64 0a 20 20 20  tes consumed.   
20c23 20 20 20 2a 2a 20 62 79 20 74 68 65 20 63 6f 70     ** by the cop
20c24 79 20 6f 66 20 74 68 65 20 70 2d 3e 75 2e 7a 54  y of the p->u.zT
20c25 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28 69 66 20  oken string (if 
20c26 61 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  any)..      */. 
20c27 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67       const unsig
20c28 6e 65 64 20 6e 53 74 72 75 63 74 53 69 7a 65 20  ned nStructSize 
20c29 3d 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63  = dupedExprStruc
20c2a 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b  tSize(p, flags);
20c2b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  .      const int
20c2c 20 6e 4e 65 77 53 69 7a 65 20 3d 20 6e 53 74 72   nNewSize = nStr
20c2d 75 63 74 53 69 7a 65 20 26 20 30 78 66 66 66 3b  uctSize & 0xfff;
20c2e 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65  .      int nToke
20c2f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 21 45 78  n;.      if( !Ex
20c30 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
20c31 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26   EP_IntValue) &&
20c32 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a   p->u.zToken ){.
20c33 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d          nToken =
20c34 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
20c35 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 20 2b 20  (p->u.zToken) + 
20c36 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
20c37 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d          nToken =
20c38 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
20c39 20 20 69 66 28 20 69 73 52 65 64 75 63 65 64 20    if( isReduced 
20c3a 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
20c3b 74 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  t( ExprHasProper
20c3c 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64  ty(p, EP_Reduced
20c3d 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  )==0 );.        
20c3e 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70  memcpy(zAlloc, p
20c3f 2c 20 6e 4e 65 77 53 69 7a 65 29 3b 0a 20 20 20  , nNewSize);.   
20c40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
20c41 20 20 69 6e 74 20 6e 53 69 7a 65 20 3d 20 65 78    int nSize = ex
20c42 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 29 3b  prStructSize(p);
20c43 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
20c44 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 53 69 7a 65  zAlloc, p, nSize
20c45 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65  );.        memse
20c46 74 28 26 7a 41 6c 6c 6f 63 5b 6e 53 69 7a 65 5d  t(&zAlloc[nSize]
20c47 2c 20 30 2c 20 45 58 50 52 5f 46 55 4c 4c 53 49  , 0, EXPR_FULLSI
20c48 5a 45 2d 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20  ZE-nSize);.     
20c49 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74   }..      /* Set
20c4a 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 2c   the EP_Reduced,
20c4b 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 2c 20 61   EP_TokenOnly, a
20c4c 6e 64 20 45 50 5f 53 74 61 74 69 63 20 66 6c 61  nd EP_Static fla
20c4d 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79  gs appropriately
20c4e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 2d  . */.      pNew-
20c4f 3e 66 6c 61 67 73 20 26 3d 20 7e 28 45 50 5f 52  >flags &= ~(EP_R
20c50 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f  educed|EP_TokenO
20c51 6e 6c 79 7c 45 50 5f 53 74 61 74 69 63 29 3b 0a  nly|EP_Static);.
20c52 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67        pNew->flag
20c53 73 20 7c 3d 20 6e 53 74 72 75 63 74 53 69 7a 65  s |= nStructSize
20c54 20 26 20 28 45 50 5f 52 65 64 75 63 65 64 7c 45   & (EP_Reduced|E
20c55 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 3b 0a 20 20  P_TokenOnly);.  
20c56 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20      pNew->flags 
20c57 7c 3d 20 73 74 61 74 69 63 46 6c 61 67 3b 0a 0a  |= staticFlag;..
20c58 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68        /* Copy th
20c59 65 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74  e p->u.zToken st
20c5a 72 69 6e 67 2c 20 69 66 20 61 6e 79 2e 20 2a 2f  ring, if any. */
20c5b 0a 20 20 20 20 20 20 69 66 28 20 6e 54 6f 6b 65  .      if( nToke
20c5c 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  n ){.        cha
20c5d 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4e 65 77  r *zToken = pNew
20c5e 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 28 63 68  ->u.zToken = (ch
20c5f 61 72 2a 29 26 7a 41 6c 6c 6f 63 5b 6e 4e 65 77  ar*)&zAlloc[nNew
20c60 53 69 7a 65 5d 3b 0a 20 20 20 20 20 20 20 20 6d  Size];.        m
20c61 65 6d 63 70 79 28 7a 54 6f 6b 65 6e 2c 20 70 2d  emcpy(zToken, p-
20c62 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65  >u.zToken, nToke
20c63 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  n);.      }..   
20c64 20 20 20 69 66 28 20 30 3d 3d 28 28 70 2d 3e 66     if( 0==((p->f
20c65 6c 61 67 73 7c 70 4e 65 77 2d 3e 66 6c 61 67 73  lags|pNew->flags
20c66 29 20 26 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  ) & EP_TokenOnly
20c67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
20c68 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 4e 65 77  Fill in the pNew
20c69 2d 3e 78 2e 70 53 65 6c 65 63 74 20 6f 72 20 70  ->x.pSelect or p
20c6a 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 6d 65 6d  New->x.pList mem
20c6b 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ber. */.        
20c6c 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
20c6d 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c  rty(p, EP_xIsSel
20c6e 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ect) ){.        
20c6f 20 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63    pNew->x.pSelec
20c70 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  t = sqlite3Selec
20c71 74 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 53  tDup(db, p->x.pS
20c72 65 6c 65 63 74 2c 20 69 73 52 65 64 75 63 65 64  elect, isReduced
20c73 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
20c74 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77  {.          pNew
20c75 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 73 71 6c 69  ->x.pList = sqli
20c76 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
20c77 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 69  b, p->x.pList, i
20c78 73 52 65 64 75 63 65 64 29 3b 0a 20 20 20 20 20  sReduced);.     
20c79 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
20c7a 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 70      /* Fill in p
20c7b 4e 65 77 2d 3e 70 4c 65 66 74 20 61 6e 64 20 70  New->pLeft and p
20c7c 4e 65 77 2d 3e 70 52 69 67 68 74 2e 20 2a 2f 0a  New->pRight. */.
20c7d 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
20c7e 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 4e 65  sAnyProperty(pNe
20c7f 77 2c 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50  w, EP_Reduced|EP
20c80 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20  _TokenOnly) ){. 
20c81 20 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 2b 3d         zAlloc +=
20c82 20 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69   dupedExprNodeSi
20c83 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20  ze(p, flags);.  
20c84 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
20c85 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20  sProperty(pNew, 
20c86 45 50 5f 52 65 64 75 63 65 64 29 20 29 7b 0a 20  EP_Reduced) ){. 
20c87 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70           pNew->p
20c88 4c 65 66 74 20 3d 20 65 78 70 72 44 75 70 28 64  Left = exprDup(d
20c89 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 45 58 50  b, p->pLeft, EXP
20c8a 52 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a 41  RDUP_REDUCE, &zA
20c8b 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 20  lloc);.         
20c8c 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20   pNew->pRight = 
20c8d 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70  exprDup(db, p->p
20c8e 52 69 67 68 74 2c 20 45 58 50 52 44 55 50 5f 52  Right, EXPRDUP_R
20c8f 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b  EDUCE, &zAlloc);
20c90 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
20c91 20 20 20 69 66 28 20 70 7a 42 75 66 66 65 72 20     if( pzBuffer 
20c92 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 7a  ){.          *pz
20c93 42 75 66 66 65 72 20 3d 20 7a 41 6c 6c 6f 63 3b  Buffer = zAlloc;
20c94 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
20c95 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
20c96 70 4e 65 77 2d 3e 66 6c 61 67 73 32 20 3d 20 30  pNew->flags2 = 0
20c97 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 45  ;.        if( !E
20c98 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
20c99 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  y(p, EP_TokenOnl
20c9a 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  y) ){.          
20c9b 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 73 71  pNew->pLeft = sq
20c9c 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
20c9d 20 70 2d 3e 70 4c 65 66 74 2c 20 30 29 3b 0a 20   p->pLeft, 0);. 
20c9e 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70           pNew->p
20c9f 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45  Right = sqlite3E
20ca0 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52  xprDup(db, p->pR
20ca1 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  ight, 0);.      
20ca2 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
20ca3 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
20ca4 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  pNew;.}../*.** T
20ca5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 72 6f  he following gro
20ca6 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 6d  up of routines m
20ca7 61 6b 65 20 64 65 65 70 20 63 6f 70 69 65 73 20  ake deep copies 
20ca8 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a  of expressions,.
20ca9 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  ** expression li
20caa 73 74 73 2c 20 49 44 20 6c 69 73 74 73 2c 20 61  sts, ID lists, a
20cab 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d  nd select statem
20cac 65 6e 74 73 2e 20 20 54 68 65 20 63 6f 70 69 65  ents.  The copie
20cad 73 20 63 61 6e 0a 2a 2a 20 62 65 20 64 65 6c 65  s can.** be dele
20cae 74 65 64 20 28 62 79 20 62 65 69 6e 67 20 70 61  ted (by being pa
20caf 73 73 65 64 20 74 6f 20 74 68 65 69 72 20 72 65  ssed to their re
20cb0 73 70 65 63 74 69 76 65 20 2e 2e 2e 44 65 6c 65  spective ...Dele
20cb1 74 65 28 29 20 72 6f 75 74 69 6e 65 73 29 0a 2a  te() routines).*
20cb2 2a 20 77 69 74 68 6f 75 74 20 65 66 66 65 63 74  * without effect
20cb3 69 6e 67 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  ing the original
20cb4 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 70  s..**.** The exp
20cb5 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 49 44  ression list, ID
20cb6 2c 20 61 6e 64 20 73 6f 75 72 63 65 20 6c 69 73  , and source lis
20cb7 74 73 20 72 65 74 75 72 6e 20 62 79 20 73 71 6c  ts return by sql
20cb8 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
20cb9 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 49 64 4c  ),.** sqlite3IdL
20cba 69 73 74 44 75 70 28 29 2c 20 61 6e 64 20 73 71  istDup(), and sq
20cbb 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28  lite3SrcListDup(
20cbc 29 20 63 61 6e 20 6e 6f 74 20 62 65 20 66 75 72  ) can not be fur
20cbd 74 68 65 72 20 65 78 70 61 6e 64 65 64 20 0a 2a  ther expanded .*
20cbe 2a 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20  * by subsequent 
20cbf 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 2a  calls to sqlite*
20cc0 4c 69 73 74 41 70 70 65 6e 64 28 29 20 72 6f 75  ListAppend() rou
20cc1 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79  tines..**.** Any
20cc2 20 74 61 62 6c 65 73 20 74 68 61 74 20 74 68 65   tables that the
20cc3 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 70   SrcList might p
20cc4 6f 69 6e 74 20 74 6f 20 61 72 65 20 6e 6f 74 20  oint to are not 
20cc5 64 75 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 0a 2a  duplicated..**.*
20cc6 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61  * The flags para
20cc7 6d 65 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61  meter contains a
20cc8 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20   combination of 
20cc9 74 68 65 20 45 58 50 52 44 55 50 5f 58 58 58 20  the EXPRDUP_XXX 
20cca 66 6c 61 67 73 2e 0a 2a 2a 20 49 66 20 74 68 65  flags..** If the
20ccb 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20   EXPRDUP_REDUCE 
20ccc 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65  flag is set, the
20ccd 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  n the structure 
20cce 72 65 74 75 72 6e 65 64 20 69 73 20 61 0a 2a 2a  returned is a.**
20ccf 20 74 72 75 6e 63 61 74 65 64 20 76 65 72 73 69   truncated versi
20cd0 6f 6e 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20  on of the usual 
20cd1 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20 74  Expr structure t
20cd2 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72  hat will be stor
20cd3 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66  ed as.** part of
20cd4 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72   the in-memory r
20cd5 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
20cd6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
20cd7 68 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  hema..*/.SQLITE_
20cd8 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
20cd9 6c 69 74 65 33 45 78 70 72 44 75 70 28 73 71 6c  lite3ExprDup(sql
20cda 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a  ite3 *db, Expr *
20cdb 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  p, int flags){. 
20cdc 20 72 65 74 75 72 6e 20 65 78 70 72 44 75 70 28   return exprDup(
20cdd 64 62 2c 20 70 2c 20 66 6c 61 67 73 2c 20 30 29  db, p, flags, 0)
20cde 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
20cdf 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c  TE ExprList *sql
20ce0 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
20ce1 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
20ce2 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c  rList *p, int fl
20ce3 61 67 73 29 7b 0a 20 20 45 78 70 72 4c 69 73 74  ags){.  ExprList
20ce4 20 2a 70 4e 65 77 3b 0a 20 20 73 74 72 75 63 74   *pNew;.  struct
20ce5 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
20ce6 70 49 74 65 6d 2c 20 2a 70 4f 6c 64 49 74 65 6d  pItem, *pOldItem
20ce7 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  ;.  int i;.  if(
20ce8 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
20ce9 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
20cea 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
20ceb 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20  , sizeof(*pNew) 
20cec 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  );.  if( pNew==0
20ced 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
20cee 4e 65 77 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20  New->iECursor = 
20cef 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45 78 70 72  0;.  pNew->nExpr
20cf0 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20   = pNew->nAlloc 
20cf1 3d 20 70 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 4e  = p->nExpr;.  pN
20cf2 65 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20 3d 20  ew->a = pItem = 
20cf3 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
20cf4 61 77 28 64 62 2c 20 20 70 2d 3e 6e 45 78 70 72  aw(db,  p->nExpr
20cf5 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29  *sizeof(p->a[0])
20cf6 20 29 3b 0a 20 20 69 66 28 20 70 49 74 65 6d 3d   );.  if( pItem=
20cf7 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
20cf8 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77  3DbFree(db, pNew
20cf9 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
20cfa 0a 20 20 7d 20 0a 20 20 70 4f 6c 64 49 74 65 6d  .  } .  pOldItem
20cfb 20 3d 20 70 2d 3e 61 3b 0a 20 20 66 6f 72 28 69   = p->a;.  for(i
20cfc 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20  =0; i<p->nExpr; 
20cfd 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 2c 20 70 4f  i++, pItem++, pO
20cfe 6c 64 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 45  ldItem++){.    E
20cff 78 70 72 20 2a 70 4f 6c 64 45 78 70 72 20 3d 20  xpr *pOldExpr = 
20d00 70 4f 6c 64 49 74 65 6d 2d 3e 70 45 78 70 72 3b  pOldItem->pExpr;
20d01 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70  .    pItem->pExp
20d02 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  r = sqlite3ExprD
20d03 75 70 28 64 62 2c 20 70 4f 6c 64 45 78 70 72 2c  up(db, pOldExpr,
20d04 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 49 74   flags);.    pIt
20d05 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  em->zName = sqli
20d06 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
20d07 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  pOldItem->zName)
20d08 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 53 70  ;.    pItem->zSp
20d09 61 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  an = sqlite3DbSt
20d0a 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  rDup(db, pOldIte
20d0b 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 20 20 70  m->zSpan);.    p
20d0c 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20  Item->sortOrder 
20d0d 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 73 6f 72 74  = pOldItem->sort
20d0e 4f 72 64 65 72 3b 0a 20 20 20 20 70 49 74 65 6d  Order;.    pItem
20d0f 2d 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20  ->done = 0;.    
20d10 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 70 4f  pItem->iCol = pO
20d11 6c 64 49 74 65 6d 2d 3e 69 43 6f 6c 3b 0a 20 20  ldItem->iCol;.  
20d12 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20    pItem->iAlias 
20d13 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 41 6c 69  = pOldItem->iAli
20d14 61 73 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  as;.  }.  return
20d15 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pNew;.}../*.** 
20d16 49 66 20 63 75 72 73 6f 72 73 2c 20 74 72 69 67  If cursors, trig
20d17 67 65 72 73 2c 20 76 69 65 77 73 20 61 6e 64 20  gers, views and 
20d18 73 75 62 71 75 65 72 69 65 73 20 61 72 65 20 61  subqueries are a
20d19 6c 6c 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a  ll omitted from.
20d1a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2c 20 74 68  ** the build, th
20d1b 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66  en none of the f
20d1c 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
20d1d 73 2c 20 65 78 63 65 70 74 20 66 6f 72 20 0a 2a  s, except for .*
20d1e 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  * sqlite3SelectD
20d1f 75 70 28 29 2c 20 63 61 6e 20 62 65 20 63 61 6c  up(), can be cal
20d20 6c 65 64 2e 20 73 71 6c 69 74 65 33 53 65 6c 65  led. sqlite3Sele
20d21 63 74 44 75 70 28 29 20 69 73 20 73 6f 6d 65 74  ctDup() is somet
20d22 69 6d 65 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77  imes.** called w
20d23 69 74 68 20 61 20 4e 55 4c 4c 20 61 72 67 75 6d  ith a NULL argum
20d24 65 6e 74 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ent..*/.#if !def
20d25 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
20d26 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
20d27 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
20d28 52 49 47 47 45 52 29 20 5c 0a 20 7c 7c 20 21 64  RIGGER) \. || !d
20d29 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
20d2a 49 54 5f 53 55 42 51 55 45 52 59 29 0a 53 51 4c  IT_SUBQUERY).SQL
20d2b 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c  ITE_PRIVATE SrcL
20d2c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c  ist *sqlite3SrcL
20d2d 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a  istDup(sqlite3 *
20d2e 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20  db, SrcList *p, 
20d2f 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53 72  int flags){.  Sr
20d30 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69  cList *pNew;.  i
20d31 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74  nt i;.  int nByt
20d32 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  e;.  if( p==0 ) 
20d33 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 42 79 74  return 0;.  nByt
20d34 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b  e = sizeof(*p) +
20d35 20 28 70 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69   (p->nSrc>0 ? si
20d36 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20  zeof(p->a[0]) * 
20d37 28 70 2d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29  (p->nSrc-1) : 0)
20d38 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
20d39 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
20d3a 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28  , nByte );.  if(
20d3b 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72   pNew==0 ) retur
20d3c 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 53 72  n 0;.  pNew->nSr
20d3d 63 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63  c = pNew->nAlloc
20d3e 20 3d 20 70 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f   = p->nSrc;.  fo
20d3f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 53 72 63  r(i=0; i<p->nSrc
20d40 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
20d41 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
20d42 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65  *pNewItem = &pNe
20d43 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72  w->a[i];.    str
20d44 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
20d45 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d   *pOldItem = &p-
20d46 3e 61 5b 69 5d 3b 0a 20 20 20 20 54 61 62 6c 65  >a[i];.    Table
20d47 20 2a 70 54 61 62 3b 0a 20 20 20 20 70 4e 65 77   *pTab;.    pNew
20d48 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20  Item->zDatabase 
20d49 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
20d4a 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
20d4b 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
20d4c 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20  pNewItem->zName 
20d4d 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
20d4e 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
20d4f 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77  zName);.    pNew
20d50 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73  Item->zAlias = s
20d51 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
20d52 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c  b, pOldItem->zAl
20d53 69 61 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74  ias);.    pNewIt
20d54 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 3d 20 70  em->jointype = p
20d55 4f 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70  OldItem->jointyp
20d56 65 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  e;.    pNewItem-
20d57 3e 69 43 75 72 73 6f 72 20 3d 20 70 4f 6c 64 49  >iCursor = pOldI
20d58 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
20d59 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 73 50 6f    pNewItem->isPo
20d5a 70 75 6c 61 74 65 64 20 3d 20 70 4f 6c 64 49 74  pulated = pOldIt
20d5b 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3b  em->isPopulated;
20d5c 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a  .    pNewItem->z
20d5d 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44  Index = sqlite3D
20d5e 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  bStrDup(db, pOld
20d5f 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20  Item->zIndex);. 
20d60 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6e 6f 74     pNewItem->not
20d61 49 6e 64 65 78 65 64 20 3d 20 70 4f 6c 64 49 74  Indexed = pOldIt
20d62 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3b 0a  em->notIndexed;.
20d63 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 49      pNewItem->pI
20d64 6e 64 65 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  ndex = pOldItem-
20d65 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 54 61  >pIndex;.    pTa
20d66 62 20 3d 20 70 4e 65 77 49 74 65 6d 2d 3e 70 54  b = pNewItem->pT
20d67 61 62 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70  ab = pOldItem->p
20d68 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61  Tab;.    if( pTa
20d69 62 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 2d  b ){.      pTab-
20d6a 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20  >nRef++;.    }. 
20d6b 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 53 65     pNewItem->pSe
20d6c 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
20d6d 6c 65 63 74 44 75 70 28 64 62 2c 20 70 4f 6c 64  lectDup(db, pOld
20d6e 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 66  Item->pSelect, f
20d6f 6c 61 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 49  lags);.    pNewI
20d70 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 6c 69 74  tem->pOn = sqlit
20d71 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f  e3ExprDup(db, pO
20d72 6c 64 49 74 65 6d 2d 3e 70 4f 6e 2c 20 66 6c 61  ldItem->pOn, fla
20d73 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  gs);.    pNewIte
20d74 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 73 71 6c 69  m->pUsing = sqli
20d75 74 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c  te3IdListDup(db,
20d76 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 55 73 69 6e   pOldItem->pUsin
20d77 67 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  g);.    pNewItem
20d78 2d 3e 63 6f 6c 55 73 65 64 20 3d 20 70 4f 6c 64  ->colUsed = pOld
20d79 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20  Item->colUsed;. 
20d7a 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
20d7b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
20d7c 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74  TE IdList *sqlit
20d7d 65 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69  e3IdListDup(sqli
20d7e 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20  te3 *db, IdList 
20d7f 2a 70 29 7b 0a 20 20 49 64 4c 69 73 74 20 2a 70  *p){.  IdList *p
20d80 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  New;.  int i;.  
20d81 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
20d82 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  n 0;.  pNew = sq
20d83 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
20d84 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65  (db, sizeof(*pNe
20d85 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  w) );.  if( pNew
20d86 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
20d87 20 20 70 4e 65 77 2d 3e 6e 49 64 20 3d 20 70 4e    pNew->nId = pN
20d88 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e  ew->nAlloc = p->
20d89 6e 49 64 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d  nId;.  pNew->a =
20d8a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
20d8b 52 61 77 28 64 62 2c 20 70 2d 3e 6e 49 64 2a 73  Raw(db, p->nId*s
20d8c 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29  izeof(p->a[0]) )
20d8d 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 61 3d  ;.  if( pNew->a=
20d8e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
20d8f 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77  3DbFree(db, pNew
20d90 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
20d91 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
20d92 69 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a  i<p->nId; i++){.
20d93 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73      struct IdLis
20d94 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d  t_item *pNewItem
20d95 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a   = &pNew->a[i];.
20d96 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73      struct IdLis
20d97 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d  t_item *pOldItem
20d98 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20   = &p->a[i];.   
20d99 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65   pNewItem->zName
20d9a 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
20d9b 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d  up(db, pOldItem-
20d9c 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65  >zName);.    pNe
20d9d 77 49 74 65 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c  wItem->idx = pOl
20d9e 64 49 74 65 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a  dItem->idx;.  }.
20d9f 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
20da0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20da1 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53  Select *sqlite3S
20da2 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33  electDup(sqlite3
20da3 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c   *db, Select *p,
20da4 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53   int flags){.  S
20da5 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 69  elect *pNew;.  i
20da6 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
20da7 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c   0;.  pNew = sql
20da8 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
20da9 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 29  db, sizeof(*p) )
20daa 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
20dab 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
20dac 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71 6c  ew->pEList = sql
20dad 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
20dae 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 66  db, p->pEList, f
20daf 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  lags);.  pNew->p
20db0 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Src = sqlite3Src
20db1 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70  ListDup(db, p->p
20db2 53 72 63 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  Src, flags);.  p
20db3 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d 20 73 71  New->pWhere = sq
20db4 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
20db5 20 70 2d 3e 70 57 68 65 72 65 2c 20 66 6c 61 67   p->pWhere, flag
20db6 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f  s);.  pNew->pGro
20db7 75 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78  upBy = sqlite3Ex
20db8 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d  prListDup(db, p-
20db9 3e 70 47 72 6f 75 70 42 79 2c 20 66 6c 61 67 73  >pGroupBy, flags
20dba 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69  );.  pNew->pHavi
20dbb 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ng = sqlite3Expr
20dbc 44 75 70 28 64 62 2c 20 70 2d 3e 70 48 61 76 69  Dup(db, p->pHavi
20dbd 6e 67 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e  ng, flags);.  pN
20dbe 65 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73  ew->pOrderBy = s
20dbf 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
20dc0 70 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42  p(db, p->pOrderB
20dc1 79 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65  y, flags);.  pNe
20dc2 77 2d 3e 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20  w->op = p->op;. 
20dc3 20 70 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20   pNew->pPrior = 
20dc4 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
20dc5 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20  (db, p->pPrior, 
20dc6 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e  flags);.  pNew->
20dc7 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33  pLimit = sqlite3
20dc8 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70  ExprDup(db, p->p
20dc9 4c 69 6d 69 74 2c 20 66 6c 61 67 73 29 3b 0a 20  Limit, flags);. 
20dca 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d   pNew->pOffset =
20dcb 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
20dcc 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20  db, p->pOffset, 
20dcd 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e  flags);.  pNew->
20dce 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 70 4e  iLimit = 0;.  pN
20dcf 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b  ew->iOffset = 0;
20dd0 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67  .  pNew->selFlag
20dd1 73 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20  s = p->selFlags 
20dd2 26 20 7e 53 46 5f 55 73 65 73 45 70 68 65 6d 65  & ~SF_UsesEpheme
20dd3 72 61 6c 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69  ral;.  pNew->pRi
20dd4 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 70  ghtmost = 0;.  p
20dd5 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  New->addrOpenEph
20dd6 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65  m[0] = -1;.  pNe
20dd7 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  w->addrOpenEphm[
20dd8 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d  1] = -1;.  pNew-
20dd9 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d  >addrOpenEphm[2]
20dda 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20   = -1;.  return 
20ddb 70 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a 53 51  pNew;.}.#else.SQ
20ddc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c  LITE_PRIVATE Sel
20ddd 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65  ect *sqlite3Sele
20dde 63 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  ctDup(sqlite3 *d
20ddf 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e  b, Select *p, in
20de0 74 20 66 6c 61 67 73 29 7b 0a 20 20 61 73 73 65  t flags){.  asse
20de1 72 74 28 20 70 3d 3d 30 20 29 3b 0a 20 20 72 65  rt( p==0 );.  re
20de2 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66  turn 0;.}.#endif
20de3 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e  .../*.** Add a n
20de4 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68  ew element to th
20de5 65 20 65 6e 64 20 6f 66 20 61 6e 20 65 78 70 72  e end of an expr
20de6 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66  ession list.  If
20de7 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20 69 6e 69   pList is.** ini
20de8 74 69 61 6c 6c 79 20 4e 55 4c 4c 2c 20 74 68 65  tially NULL, the
20de9 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 65  n create a new e
20dea 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a  xpression list..
20deb 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72  **.** If a memor
20dec 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
20ded 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 65  or occurs, the e
20dee 6e 74 69 72 65 20 6c 69 73 74 20 69 73 20 66 72  ntire list is fr
20def 65 65 64 20 61 6e 64 0a 2a 2a 20 4e 55 4c 4c 20  eed and.** NULL 
20df0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66  is returned.  If
20df1 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 73 20 72 65 74   non-NULL is ret
20df2 75 72 6e 65 64 2c 20 74 68 65 6e 20 69 74 20 69  urned, then it i
20df3 73 20 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20  s guaranteed.** 
20df4 74 68 61 74 20 74 68 65 20 6e 65 77 20 65 6e 74  that the new ent
20df5 72 79 20 77 61 73 20 73 75 63 63 65 73 73 66 75  ry was successfu
20df6 6c 6c 79 20 61 70 70 65 6e 64 65 64 2e 0a 2a 2f  lly appended..*/
20df7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20df8 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65  ExprList *sqlite
20df9 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
20dfa 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
20dfb 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
20dfc 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
20dfd 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
20dfe 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  st,        /* Li
20dff 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61  st to which to a
20e00 70 70 65 6e 64 2e 20 4d 69 67 68 74 20 62 65 20  ppend. Might be 
20e01 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a  NULL */.  Expr *
20e02 70 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20  pExpr           
20e03 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
20e04 74 6f 20 62 65 20 61 70 70 65 6e 64 65 64 2e 20  to be appended. 
20e05 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f  Might be NULL */
20e06 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
20e07 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
20e08 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
20e09 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71  {.    pList = sq
20e0a 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
20e0b 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70  o(db, sizeof(Exp
20e0c 72 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66  rList) );.    if
20e0d 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  ( pList==0 ){.  
20e0e 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
20e0f 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
20e10 74 28 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63  t( pList->nAlloc
20e11 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28  ==0 );.  }.  if(
20e12 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3c 3d   pList->nAlloc<=
20e13 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a  pList->nExpr ){.
20e14 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
20e15 69 73 74 5f 69 74 65 6d 20 2a 61 3b 0a 20 20 20  ist_item *a;.   
20e16 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 2d 3e   int n = pList->
20e17 6e 41 6c 6c 6f 63 2a 32 20 2b 20 34 3b 0a 20 20  nAlloc*2 + 4;.  
20e18 20 20 61 20 3d 20 73 71 6c 69 74 65 33 44 62 52    a = sqlite3DbR
20e19 65 61 6c 6c 6f 63 28 64 62 2c 20 70 4c 69 73 74  ealloc(db, pList
20e1a 2d 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66 28 70 4c  ->a, n*sizeof(pL
20e1b 69 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 20  ist->a[0]));.   
20e1c 20 69 66 28 20 61 3d 3d 30 20 29 7b 0a 20 20 20   if( a==0 ){.   
20e1d 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a     goto no_mem;.
20e1e 20 20 20 20 7d 0a 20 20 20 20 70 4c 69 73 74 2d      }.    pList-
20e1f 3e 61 20 3d 20 61 3b 0a 20 20 20 20 70 4c 69 73  >a = a;.    pLis
20e20 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c 69  t->nAlloc = sqli
20e21 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
20e22 64 62 2c 20 61 29 2f 73 69 7a 65 6f 66 28 61 5b  db, a)/sizeof(a[
20e23 30 5d 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  0]);.  }.  asser
20e24 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 29  t( pList->a!=0 )
20e25 3b 0a 20 20 69 66 28 20 31 20 29 7b 0a 20 20 20  ;.  if( 1 ){.   
20e26 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
20e27 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26  _item *pItem = &
20e28 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e  pList->a[pList->
20e29 6e 45 78 70 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65  nExpr++];.    me
20e2a 6d 73 65 74 28 70 49 74 65 6d 2c 20 30 2c 20 73  mset(pItem, 0, s
20e2b 69 7a 65 6f 66 28 2a 70 49 74 65 6d 29 29 3b 0a  izeof(*pItem));.
20e2c 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72      pItem->pExpr
20e2d 20 3d 20 70 45 78 70 72 3b 0a 20 20 7d 0a 20 20   = pExpr;.  }.  
20e2e 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 0a 6e  return pList;..n
20e2f 6f 5f 6d 65 6d 3a 20 20 20 20 20 0a 20 20 2f 2a  o_mem:     .  /*
20e30 20 41 76 6f 69 64 20 6c 65 61 6b 69 6e 67 20 6d   Avoid leaking m
20e31 65 6d 6f 72 79 20 69 66 20 6d 61 6c 6c 6f 63 20  emory if malloc 
20e32 68 61 73 20 66 61 69 6c 65 64 2e 20 2a 2f 0a 20  has failed. */. 
20e33 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
20e34 74 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20  te(db, pExpr);. 
20e35 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
20e36 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74  Delete(db, pList
20e37 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  );.  return 0;.}
20e38 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
20e39 45 78 70 72 4c 69 73 74 2e 61 5b 5d 2e 7a 4e 61  ExprList.a[].zNa
20e3a 6d 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  me element of th
20e3b 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
20e3c 61 64 64 65 64 20 69 74 65 6d 0a 2a 2a 20 6f 6e  added item.** on
20e3d 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20e3e 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73  list..**.** pLis
20e3f 74 20 6d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20  t might be NULL 
20e40 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d  following an OOM
20e41 20 65 72 72 6f 72 2e 20 20 42 75 74 20 70 4e 61   error.  But pNa
20e42 6d 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  me should never 
20e43 62 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 49 66 20  be.** NULL.  If 
20e44 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
20e45 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 20 70  ion fails, the p
20e46 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
20e47 63 46 61 69 6c 65 64 20 66 6c 61 67 0a 2a 2a 20  cFailed flag.** 
20e48 69 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54  is set..*/.SQLIT
20e49 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
20e4a 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65  qlite3ExprListSe
20e4b 74 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a  tName(.  Parse *
20e4c 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
20e4d 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
20e4e 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ext */.  ExprLis
20e4f 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20  t *pList,       
20e50 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 63   /* List to whic
20e51 68 20 74 6f 20 61 64 64 20 74 68 65 20 73 70 61  h to add the spa
20e52 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  n. */.  Token *p
20e53 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20  Name,           
20e54 2f 2a 20 4e 61 6d 65 20 74 6f 20 62 65 20 61 64  /* Name to be ad
20e55 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 65 71  ded */.  int deq
20e56 75 6f 74 65 20 20 20 20 20 20 20 20 20 20 20 20  uote            
20e57 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 61 75 73   /* True to caus
20e58 65 20 74 68 65 20 6e 61 6d 65 20 74 6f 20 62 65  e the name to be
20e59 20 64 65 71 75 6f 74 65 64 20 2a 2f 0a 29 7b 0a   dequoted */.){.
20e5a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21    assert( pList!
20e5b 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62  =0 || pParse->db
20e5c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 21 3d  ->mallocFailed!=
20e5d 30 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  0 );.  if( pList
20e5e 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45   ){.    struct E
20e5f 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
20e60 74 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  tem;.    assert(
20e61 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20   pList->nExpr>0 
20e62 29 3b 0a 20 20 20 20 70 49 74 65 6d 20 3d 20 26  );.    pItem = &
20e63 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e  pList->a[pList->
20e64 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20 61 73  nExpr-1];.    as
20e65 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 7a 4e 61  sert( pItem->zNa
20e66 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 49 74  me==0 );.    pIt
20e67 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  em->zName = sqli
20e68 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 50 61  te3DbStrNDup(pPa
20e69 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 2d 3e  rse->db, pName->
20e6a 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20  z, pName->n);.  
20e6b 20 20 69 66 28 20 64 65 71 75 6f 74 65 20 26 26    if( dequote &&
20e6c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20   pItem->zName ) 
20e6d 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 70  sqlite3Dequote(p
20e6e 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Item->zName);.  
20e6f 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  }.}../*.** Set t
20e70 68 65 20 45 78 70 72 4c 69 73 74 2e 61 5b 5d 2e  he ExprList.a[].
20e71 7a 53 70 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66  zSpan element of
20e72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
20e73 6c 79 20 61 64 64 65 64 20 69 74 65 6d 0a 2a 2a  ly added item.**
20e74 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   on the expressi
20e75 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70  on list..**.** p
20e76 4c 69 73 74 20 6d 69 67 68 74 20 62 65 20 4e 55  List might be NU
20e77 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20  LL following an 
20e78 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75 74 20  OOM error.  But 
20e79 70 53 70 61 6e 20 73 68 6f 75 6c 64 20 6e 65 76  pSpan should nev
20e7a 65 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20  er be.** NULL.  
20e7b 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  If a memory allo
20e7c 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68  cation fails, th
20e7d 65 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  e pParse->db->ma
20e7e 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 0a  llocFailed flag.
20e7f 2a 2a 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 53 51  ** is set..*/.SQ
20e80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20e81 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  d sqlite3ExprLis
20e82 74 53 65 74 53 70 61 6e 28 0a 20 20 50 61 72 73  tSetSpan(.  Pars
20e83 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
20e84 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
20e85 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
20e86 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20  List *pList,    
20e87 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77      /* List to w
20e88 68 69 63 68 20 74 6f 20 61 64 64 20 74 68 65 20  hich to add the 
20e89 73 70 61 6e 2e 20 2a 2f 0a 20 20 45 78 70 72 53  span. */.  ExprS
20e8a 70 61 6e 20 2a 70 53 70 61 6e 20 20 20 20 20 20  pan *pSpan      
20e8b 20 20 20 2f 2a 20 54 68 65 20 73 70 61 6e 20 74     /* The span t
20e8c 6f 20 62 65 20 61 64 64 65 64 20 2a 2f 0a 29 7b  o be added */.){
20e8d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
20e8e 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61   pParse->db;.  a
20e8f 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20  ssert( pList!=0 
20e90 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
20e91 6c 65 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  led!=0 );.  if( 
20e92 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 74 72  pList ){.    str
20e93 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
20e94 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69 73  m *pItem = &pLis
20e95 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70  t->a[pList->nExp
20e96 72 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74  r-1];.    assert
20e97 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30  ( pList->nExpr>0
20e98 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
20e99 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
20e9a 20 7c 7c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72   || pItem->pExpr
20e9b 3d 3d 70 53 70 61 6e 2d 3e 70 45 78 70 72 20 29  ==pSpan->pExpr )
20e9c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
20e9d 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a  ree(db, pItem->z
20e9e 53 70 61 6e 29 3b 0a 20 20 20 20 70 49 74 65 6d  Span);.    pItem
20e9f 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69 74 65  ->zSpan = sqlite
20ea0 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
20ea1 63 68 61 72 2a 29 70 53 70 61 6e 2d 3e 7a 53 74  char*)pSpan->zSt
20ea2 61 72 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  art,.           
20ea3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ea4 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 28 70           (int)(p
20ea5 53 70 61 6e 2d 3e 7a 45 6e 64 20 2d 20 70 53 70  Span->zEnd - pSp
20ea6 61 6e 2d 3e 7a 53 74 61 72 74 29 29 3b 0a 20 20  an->zStart));.  
20ea7 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  }.}../*.** If th
20ea8 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  e expression lis
20ea9 74 20 70 45 4c 69 73 74 20 63 6f 6e 74 61 69 6e  t pEList contain
20eaa 73 20 6d 6f 72 65 20 74 68 61 6e 20 69 4c 69 6d  s more than iLim
20eab 69 74 20 65 6c 65 6d 65 6e 74 73 2c 0a 2a 2a 20  it elements,.** 
20eac 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
20ead 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
20eae 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20eaf 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
20eb0 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e  ExprListCheckLen
20eb1 67 74 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50  gth(.  Parse *pP
20eb2 61 72 73 65 2c 0a 20 20 45 78 70 72 4c 69 73 74  arse,.  ExprList
20eb3 20 2a 70 45 4c 69 73 74 2c 0a 20 20 63 6f 6e 73   *pEList,.  cons
20eb4 74 20 63 68 61 72 20 2a 7a 4f 62 6a 65 63 74 0a  t char *zObject.
20eb5 29 7b 0a 20 20 69 6e 74 20 6d 78 20 3d 20 70 50  ){.  int mx = pP
20eb6 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74  arse->db->aLimit
20eb7 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  [SQLITE_LIMIT_CO
20eb8 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73 74 63 61 73  LUMN];.  testcas
20eb9 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c  e( pEList && pEL
20eba 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 20 29  ist->nExpr==mx )
20ebb 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 45  ;.  testcase( pE
20ebc 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e  List && pEList->
20ebd 6e 45 78 70 72 3d 3d 6d 78 2b 31 20 29 3b 0a 20  nExpr==mx+1 );. 
20ebe 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 70   if( pEList && p
20ebf 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 6d 78 20  EList->nExpr>mx 
20ec0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
20ec1 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
20ec2 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73  too many columns
20ec3 20 69 6e 20 25 73 22 2c 20 7a 4f 62 6a 65 63 74   in %s", zObject
20ec4 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
20ec5 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65  Delete an entire
20ec6 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
20ec7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20ec8 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
20ec9 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73  ExprListDelete(s
20eca 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72  qlite3 *db, Expr
20ecb 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20  List *pList){.  
20ecc 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
20ecd 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
20ece 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73  Item;.  if( pLis
20ecf 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  t==0 ) return;. 
20ed0 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e   assert( pList->
20ed1 61 21 3d 30 20 7c 7c 20 28 70 4c 69 73 74 2d 3e  a!=0 || (pList->
20ed2 6e 45 78 70 72 3d 3d 30 20 26 26 20 70 4c 69 73  nExpr==0 && pLis
20ed3 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 20 29 3b  t->nAlloc==0) );
20ed4 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74  .  assert( pList
20ed5 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 74 2d 3e  ->nExpr<=pList->
20ed6 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 6f 72 28  nAlloc );.  for(
20ed7 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20  pItem=pList->a, 
20ed8 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
20ed9 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
20eda 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  +){.    sqlite3E
20edb 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 49  xprDelete(db, pI
20edc 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20  tem->pExpr);.   
20edd 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
20ede 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  b, pItem->zName)
20edf 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
20ee0 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a  ree(db, pItem->z
20ee1 53 70 61 6e 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  Span);.  }.  sql
20ee2 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
20ee3 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69  List->a);.  sqli
20ee4 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
20ee5 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ist);.}../*.** T
20ee6 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
20ee7 65 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63  e Walker callbac
20ee8 6b 73 2e 20 20 57 61 6c 6b 65 72 2e 75 2e 70 69  ks.  Walker.u.pi
20ee9 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a   is a pointer.**
20eea 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20   to an integer. 
20eeb 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
20eec 61 72 65 20 63 68 65 63 6b 69 6e 67 20 61 6e 20  are checking an 
20eed 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73 65  expression to se
20eee 65 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 20  e.** if it is a 
20eef 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 74 20 2a  constant.  Set *
20ef0 57 61 6c 6b 65 72 2e 75 2e 70 69 20 74 6f 20 30  Walker.u.pi to 0
20ef1 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
20ef2 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e  on is.** not con
20ef3 73 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  stant..**.** The
20ef4 73 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74  se callback rout
20ef5 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  ines are used to
20ef6 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66   implement the f
20ef7 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
20ef8 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
20ef9 73 43 6f 6e 73 74 61 6e 74 28 29 0a 2a 2a 20 20  sConstant().**  
20efa 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73     sqlite3ExprIs
20efb 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28  ConstantNotJoin(
20efc 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
20efd 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72  ExprIsConstantOr
20efe 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f  Function().**.*/
20eff 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72  .static int expr
20f00 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57  NodeIsConstant(W
20f01 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20  alker *pWalker, 
20f02 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 0a 20  Expr *pExpr){.. 
20f03 20 2f 2a 20 49 66 20 70 57 61 6c 6b 65 72 2d 3e   /* If pWalker->
20f04 75 2e 69 20 69 73 20 33 20 74 68 65 6e 20 61 6e  u.i is 3 then an
20f05 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 65 78  y term of the ex
20f06 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f  pression that co
20f07 6d 65 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68  mes from.  ** th
20f08 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  e ON or USING cl
20f09 61 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 20  auses of a join 
20f0a 64 69 73 71 75 61 6c 69 66 69 65 73 20 74 68 65  disqualifies the
20f0b 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a   expression.  **
20f0c 20 66 72 6f 6d 20 62 65 69 6e 67 20 63 6f 6e 73   from being cons
20f0d 69 64 65 72 65 64 20 63 6f 6e 73 74 61 6e 74 2e  idered constant.
20f0e 20 2a 2f 0a 20 20 69 66 28 20 70 57 61 6c 6b 65   */.  if( pWalke
20f0f 72 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 45 78 70  r->u.i==3 && Exp
20f10 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
20f11 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f  pExpr, EP_FromJo
20f12 69 6e 29 20 29 7b 0a 20 20 20 20 70 57 61 6c 6b  in) ){.    pWalk
20f13 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20  er->u.i = 0;.   
20f14 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
20f15 74 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68  t;.  }..  switch
20f16 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20  ( pExpr->op ){. 
20f17 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 66     /* Consider f
20f18 75 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65 20 63  unctions to be c
20f19 6f 6e 73 74 61 6e 74 20 69 66 20 61 6c 6c 20 74  onstant if all t
20f1a 68 65 69 72 20 61 72 67 75 6d 65 6e 74 73 20 61  heir arguments a
20f1b 72 65 20 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20  re constant.    
20f1c 2a 2a 20 61 6e 64 20 70 57 61 6c 6b 65 72 2d 3e  ** and pWalker->
20f1d 75 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 20 63 61  u.i==2 */.    ca
20f1e 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a  se TK_FUNCTION:.
20f1f 20 20 20 20 20 20 69 66 28 20 70 57 61 6c 6b 65        if( pWalke
20f20 72 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65 74 75  r->u.i==2 ) retu
20f21 72 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46  rn 0;.      /* F
20f22 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20  all through */. 
20f23 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20     case TK_ID:. 
20f24 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d     case TK_COLUM
20f25 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  N:.    case TK_A
20f26 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20  GG_FUNCTION:.   
20f27 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c   case TK_AGG_COL
20f28 55 4d 4e 3a 0a 20 20 20 20 20 20 74 65 73 74 63  UMN:.      testc
20f29 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ase( pExpr->op==
20f2a 54 4b 5f 49 44 20 29 3b 0a 20 20 20 20 20 20 74  TK_ID );.      t
20f2b 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
20f2c 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b  op==TK_COLUMN );
20f2d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
20f2e 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41   pExpr->op==TK_A
20f2f 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20  GG_FUNCTION );. 
20f30 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
20f31 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47  Expr->op==TK_AGG
20f32 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20  _COLUMN );.     
20f33 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20   pWalker->u.i = 
20f34 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  0;.      return 
20f35 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 64  WRC_Abort;.    d
20f36 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 74 65  efault:.      te
20f37 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
20f38 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 20  p==TK_SELECT ); 
20f39 2f 2a 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43  /* selectNodeIsC
20f3a 6f 6e 73 74 61 6e 74 20 77 69 6c 6c 20 64 69 73  onstant will dis
20f3b 61 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20 74  allow */.      t
20f3c 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
20f3d 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b  op==TK_EXISTS );
20f3e 20 2f 2a 20 73 65 6c 65 63 74 4e 6f 64 65 49 73   /* selectNodeIs
20f3f 43 6f 6e 73 74 61 6e 74 20 77 69 6c 6c 20 64 69  Constant will di
20f40 73 61 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20  sallow */.      
20f41 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
20f42 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  nue;.  }.}.stati
20f43 63 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f 64 65  c int selectNode
20f44 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65  IsConstant(Walke
20f45 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
20f46 63 74 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  ct *NotUsed){.  
20f47 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
20f48 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 70 57 61  (NotUsed);.  pWa
20f49 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20  lker->u.i = 0;. 
20f4a 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
20f4b 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  t;.}.static int 
20f4c 65 78 70 72 49 73 43 6f 6e 73 74 28 45 78 70 72  exprIsConst(Expr
20f4d 20 2a 70 2c 20 69 6e 74 20 69 6e 69 74 46 6c 61   *p, int initFla
20f4e 67 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a  g){.  Walker w;.
20f4f 20 20 77 2e 75 2e 69 20 3d 20 69 6e 69 74 46 6c    w.u.i = initFl
20f50 61 67 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c  ag;.  w.xExprCal
20f51 6c 62 61 63 6b 20 3d 20 65 78 70 72 4e 6f 64 65  lback = exprNode
20f52 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 77 2e  IsConstant;.  w.
20f53 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
20f54 3d 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f  = selectNodeIsCo
20f55 6e 73 74 61 6e 74 3b 0a 20 20 73 71 6c 69 74 65  nstant;.  sqlite
20f56 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 29  3WalkExpr(&w, p)
20f57 3b 0a 20 20 72 65 74 75 72 6e 20 77 2e 75 2e 69  ;.  return w.u.i
20f58 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20  ;.}../*.** Walk 
20f59 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
20f5a 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66  ee.  Return 1 if
20f5b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20f5c 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61  is constant.** a
20f5d 6e 64 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c  nd 0 if it invol
20f5e 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72  ves variables or
20f5f 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 2e   function calls.
20f60 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70  .**.** For the p
20f61 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20  urposes of this 
20f62 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62  function, a doub
20f63 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le-quoted string
20f64 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20   (ex: "abc").** 
20f65 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20  is considered a 
20f66 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 20 73  variable but a s
20f67 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72  ingle-quoted str
20f68 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29 20  ing (ex: 'abc') 
20f69 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74  is.** a constant
20f6a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20f6b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
20f6c 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78  xprIsConstant(Ex
20f6d 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  pr *p){.  return
20f6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20   exprIsConst(p, 
20f6f 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c  1);.}../*.** Wal
20f70 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  k an expression 
20f71 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20  tree.  Return 1 
20f72 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
20f73 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a  n is constant.**
20f74 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 6f 72   that does no or
20f75 69 67 69 6e 61 74 65 20 66 72 6f 6d 20 74 68 65  iginate from the
20f76 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
20f77 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 2e 0a  uses of a join..
20f78 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20 69  ** Return 0 if i
20f79 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61  t involves varia
20f7a 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e  bles or function
20f7b 20 63 61 6c 6c 73 20 6f 72 20 74 65 72 6d 73 20   calls or terms 
20f7c 66 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f 72  from.** an ON or
20f7d 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a   USING clause..*
20f7e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20f7f 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
20f80 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69  IsConstantNotJoi
20f81 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 65  n(Expr *p){.  re
20f82 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 74  turn exprIsConst
20f83 28 70 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  (p, 3);.}../*.**
20f84 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73   Walk an express
20f85 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72  ion tree.  Retur
20f86 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65  n 1 if the expre
20f87 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e  ssion is constan
20f88 74 0a 2a 2a 20 6f 72 20 61 20 66 75 6e 63 74 69  t.** or a functi
20f89 6f 6e 20 63 61 6c 6c 20 77 69 74 68 20 63 6f 6e  on call with con
20f8a 73 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 73 2e  stant arguments.
20f8b 20 20 52 65 74 75 72 6e 20 61 6e 64 20 30 20 69    Return and 0 i
20f8c 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 61  f there.** are a
20f8d 6e 79 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a  ny variables..**
20f8e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70  .** For the purp
20f8f 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66 75 6e  oses of this fun
20f90 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d  ction, a double-
20f91 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65  quoted string (e
20f92 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 73 20  x: "abc").** is 
20f93 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76 61 72  considered a var
20f94 69 61 62 6c 65 20 62 75 74 20 61 20 73 69 6e 67  iable but a sing
20f95 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le-quoted string
20f96 20 28 65 78 3a 20 27 61 62 63 27 29 20 69 73 0a   (ex: 'abc') is.
20f97 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a  ** a constant..*
20f98 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20f99 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
20f9a 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63  IsConstantOrFunc
20f9b 74 69 6f 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20  tion(Expr *p){. 
20f9c 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f   return exprIsCo
20f9d 6e 73 74 28 70 2c 20 32 29 3b 0a 7d 0a 0a 2f 2a  nst(p, 2);.}../*
20f9e 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
20f9f 73 73 69 6f 6e 20 70 20 63 6f 64 65 73 20 61 20  ssion p codes a 
20fa0 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72  constant integer
20fa1 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 20 65   that is small e
20fa2 6e 6f 75 67 68 0a 2a 2a 20 74 6f 20 66 69 74 20  nough.** to fit 
20fa3 69 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65  in a 32-bit inte
20fa4 67 65 72 2c 20 72 65 74 75 72 6e 20 31 20 61 6e  ger, return 1 an
20fa5 64 20 70 75 74 20 74 68 65 20 76 61 6c 75 65 20  d put the value 
20fa6 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  of the integer.*
20fa7 2a 20 69 6e 20 2a 70 56 61 6c 75 65 2e 20 20 49  * in *pValue.  I
20fa8 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
20fa9 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67   is not an integ
20faa 65 72 20 6f 72 20 69 66 20 69 74 20 69 73 20 74  er or if it is t
20fab 6f 6f 20 62 69 67 0a 2a 2a 20 74 6f 20 66 69 74  oo big.** to fit
20fac 20 69 6e 20 61 20 73 69 67 6e 65 64 20 33 32 2d   in a signed 32-
20fad 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74  bit integer, ret
20fae 75 72 6e 20 30 20 61 6e 64 20 6c 65 61 76 65 20  urn 0 and leave 
20faf 2a 70 56 61 6c 75 65 20 75 6e 63 68 61 6e 67 65  *pValue unchange
20fb0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
20fb1 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
20fb2 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78  ExprIsInteger(Ex
20fb3 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 56 61 6c  pr *p, int *pVal
20fb4 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ue){.  int rc = 
20fb5 30 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67  0;.  if( p->flag
20fb6 73 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20  s & EP_IntValue 
20fb7 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75 65 20 3d  ){.    *pValue =
20fb8 20 70 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20   p->u.iValue;.  
20fb9 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
20fba 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20    switch( p->op 
20fbb 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  ){.    case TK_I
20fbc 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20  NTEGER: {.      
20fbd 72 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 49  rc = sqlite3GetI
20fbe 6e 74 33 32 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e  nt32(p->u.zToken
20fbf 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 20  , pValue);.     
20fc0 20 61 73 73 65 72 74 28 20 72 63 3d 3d 30 20 29   assert( rc==0 )
20fc1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
20fc2 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
20fc3 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20  _UPLUS: {.      
20fc4 72 63 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  rc = sqlite3Expr
20fc5 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65  IsInteger(p->pLe
20fc6 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20  ft, pValue);.   
20fc7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
20fc8 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e      case TK_UMIN
20fc9 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  US: {.      int 
20fca 76 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  v;.      if( sql
20fcb 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65  ite3ExprIsIntege
20fcc 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 76 29 20  r(p->pLeft, &v) 
20fcd 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 56 61 6c  ){.        *pVal
20fce 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20  ue = -v;.       
20fcf 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d   rc = 1;.      }
20fd0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
20fd1 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
20fd2 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66   break;.  }.  if
20fd3 28 20 72 63 20 29 7b 0a 20 20 20 20 61 73 73 65  ( rc ){.    asse
20fd4 72 74 28 20 45 78 70 72 48 61 73 41 6e 79 50 72  rt( ExprHasAnyPr
20fd5 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64  operty(p, EP_Red
20fd6 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  uced|EP_TokenOnl
20fd7 79 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y).             
20fd8 20 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 32 20    || (p->flags2 
20fd9 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f  & EP2_MallocedTo
20fda 6b 65 6e 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70  ken)==0 );.    p
20fdb 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 45 47 45  ->op = TK_INTEGE
20fdc 52 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  R;.    p->flags 
20fdd 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a  |= EP_IntValue;.
20fde 20 20 20 20 70 2d 3e 75 2e 69 56 61 6c 75 65 20      p->u.iValue 
20fdf 3d 20 2a 70 56 61 6c 75 65 3b 0a 20 20 7d 0a 20  = *pValue;.  }. 
20fe0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
20fe1 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
20fe2 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 73 74   if the given st
20fe3 72 69 6e 67 20 69 73 20 61 20 72 6f 77 2d 69 64  ring is a row-id
20fe4 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f   column name..*/
20fe5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20fe6 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77  int sqlite3IsRow
20fe7 69 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  id(const char *z
20fe8 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ){.  if( sqlite3
20fe9 53 74 72 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 57  StrICmp(z, "_ROW
20fea 49 44 5f 22 29 3d 3d 30 20 29 20 72 65 74 75 72  ID_")==0 ) retur
20feb 6e 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  n 1;.  if( sqlit
20fec 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 52 4f  e3StrICmp(z, "RO
20fed 57 49 44 22 29 3d 3d 30 20 29 20 72 65 74 75 72  WID")==0 ) retur
20fee 6e 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  n 1;.  if( sqlit
20fef 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 4f 49  e3StrICmp(z, "OI
20ff0 44 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  D")==0 ) return 
20ff1 31 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  1;.  return 0;.}
20ff2 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
20ff3 72 75 65 20 69 66 20 77 65 20 61 72 65 20 61 62  rue if we are ab
20ff4 6c 65 20 74 6f 20 74 68 65 20 49 4e 20 6f 70 65  le to the IN ope
20ff5 72 61 74 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69  rator optimizati
20ff6 6f 6e 20 6f 6e 20 61 0a 2a 2a 20 71 75 65 72 79  on on a.** query
20ff7 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a   of the form.**.
20ff8 2a 2a 20 20 20 20 20 20 20 78 20 49 4e 20 28 53  **       x IN (S
20ff9 45 4c 45 43 54 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a  ELECT ...).**.**
20ffa 20 57 68 65 72 65 20 74 68 65 20 53 45 4c 45 43   Where the SELEC
20ffb 54 2e 2e 2e 20 63 6c 61 75 73 65 20 69 73 20 61  T... clause is a
20ffc 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74  s specified by t
20ffd 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  he parameter to 
20ffe 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e  this.** routine.
20fff 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 65 6c 65 63  .**.** The Selec
21000 74 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20  t object passed 
21001 69 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  in has already b
21002 65 65 6e 20 70 72 65 70 72 6f 63 65 73 73 65 64  een preprocessed
21003 20 61 6e 64 20 6e 6f 0a 2a 2a 20 65 72 72 6f 72   and no.** error
21004 73 20 68 61 76 65 20 62 65 65 6e 20 66 6f 75 6e  s have been foun
21005 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  d..*/.#ifndef SQ
21006 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
21007 52 59 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  RY.static int is
21008 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70  CandidateForInOp
21009 74 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  t(Select *p){.  
2100a 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20  SrcList *pSrc;. 
2100b 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2100c 74 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  t;.  Table *pTab
2100d 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
2100e 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
2100f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 69             /* ri
21010 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  ght-hand side of
21011 20 49 4e 20 69 73 20 53 45 4c 45 43 54 20 2a 2f   IN is SELECT */
21012 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72  .  if( p->pPrior
21013 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
21014 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74            /* Not
21015 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
21016 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73  CT */.  if( p->s
21017 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69  elFlags & (SF_Di
21018 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67  stinct|SF_Aggreg
21019 61 74 65 29 20 29 7b 0a 20 20 20 20 74 65 73 74  ate) ){.    test
2101a 63 61 73 65 28 20 28 70 2d 3e 73 65 6c 46 6c 61  case( (p->selFla
2101b 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63  gs & (SF_Distinc
2101c 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 29  t|SF_Aggregate))
2101d 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74 20 29 3b  ==SF_Distinct );
2101e 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28  .    testcase( (
2101f 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53  p->selFlags & (S
21020 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67  F_Distinct|SF_Ag
21021 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 41 67  gregate))==SF_Ag
21022 67 72 65 67 61 74 65 20 29 3b 0a 20 20 20 20 72  gregate );.    r
21023 65 74 75 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44  eturn 0; /* No D
21024 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20  ISTINCT keyword 
21025 61 6e 64 20 6e 6f 20 61 67 67 72 65 67 61 74 65  and no aggregate
21026 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20   functions */.  
21027 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  }.  assert( p->p
21028 47 72 6f 75 70 42 79 3d 3d 30 20 29 3b 20 20 20  GroupBy==0 );   
21029 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
2102a 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c  s no GROUP BY cl
2102b 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d  ause */.  if( p-
2102c 3e 70 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e  >pLimit ) return
2102d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2102e 20 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54   /* Has no LIMIT
2102f 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 61 73 73   clause */.  ass
21030 65 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74 3d  ert( p->pOffset=
21031 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20  =0 );           
21032 20 20 20 20 2f 2a 20 4e 6f 20 4c 49 4d 49 54 20      /* No LIMIT 
21033 6d 65 61 6e 73 20 6e 6f 20 4f 46 46 53 45 54 20  means no OFFSET 
21034 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65  */.  if( p->pWhe
21035 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  re ) return 0;  
21036 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
21037 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75  as no WHERE clau
21038 73 65 20 2a 2f 0a 20 20 70 53 72 63 20 3d 20 70  se */.  pSrc = p
21039 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74  ->pSrc;.  assert
2103a 28 20 70 53 72 63 21 3d 30 20 29 3b 0a 20 20 69  ( pSrc!=0 );.  i
2103b 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31  f( pSrc->nSrc!=1
2103c 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
2103d 20 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c 65 20        /* Single 
2103e 74 65 72 6d 20 69 6e 20 46 52 4f 4d 20 63 6c 61  term in FROM cla
2103f 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 72  use */.  if( pSr
21040 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74 20  c->a[0].pSelect 
21041 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
21042 2f 2a 20 46 52 4f 4d 20 69 73 20 6e 6f 74 20 61  /* FROM is not a
21043 20 73 75 62 71 75 65 72 79 20 6f 72 20 76 69 65   subquery or vie
21044 77 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 70 53  w */.  pTab = pS
21045 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20  rc->a[0].pTab;. 
21046 20 69 66 28 20 4e 45 56 45 52 28 70 54 61 62 3d   if( NEVER(pTab=
21047 3d 30 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  =0) ) return 0;.
21048 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
21049 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 20  pSelect==0 );   
2104a 20 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d           /* FROM
2104b 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61   clause is not a
2104c 20 76 69 65 77 20 2a 2f 0a 20 20 69 66 28 20 49   view */.  if( I
2104d 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
2104e 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
2104f 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
21050 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74   not a virtual t
21051 61 62 6c 65 20 2a 2f 0a 20 20 70 45 4c 69 73 74  able */.  pEList
21052 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20   = p->pEList;.  
21053 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  if( pEList->nExp
21054 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b  r!=1 ) return 0;
21055 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 6f         /* One co
21056 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75  lumn in the resu
21057 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 66 28 20  lt set */.  if( 
21058 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
21059 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  pr->op!=TK_COLUM
2105a 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a  N ) return 0; /*
2105b 20 52 65 73 75 6c 74 20 69 73 20 61 20 63 6f 6c   Result is a col
2105c 75 6d 6e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  umn */.  return 
2105d 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  1;.}.#endif /* S
2105e 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2105f 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ERY */../*.** Th
21060 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
21061 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65  sed by the imple
21062 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
21063 20 49 4e 20 28 2e 2e 2e 29 20 6f 70 65 72 61 74   IN (...) operat
21064 6f 72 2e 0a 2a 2a 20 49 74 27 73 20 6a 6f 62 20  or..** It's job 
21065 69 73 20 74 6f 20 66 69 6e 64 20 6f 72 20 63 72  is to find or cr
21066 65 61 74 65 20 61 20 62 2d 74 72 65 65 20 73 74  eate a b-tree st
21067 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79  ructure that may
21068 20 62 65 20 75 73 65 64 0a 2a 2a 20 65 69 74 68   be used.** eith
21069 65 72 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6d  er to test for m
2106a 65 6d 62 65 72 73 68 69 70 20 6f 66 20 74 68 65  embership of the
2106b 20 28 2e 2e 2e 29 20 73 65 74 20 6f 72 20 74 6f   (...) set or to
2106c 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
2106d 0a 2a 2a 20 69 74 73 20 6d 65 6d 62 65 72 73 2c  .** its members,
2106e 20 73 6b 69 70 70 69 6e 67 20 64 75 70 6c 69 63   skipping duplic
2106f 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ates..**.** The 
21070 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 75 72  index of the cur
21071 73 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68  sor opened on th
21072 65 20 62 2d 74 72 65 65 20 28 64 61 74 61 62 61  e b-tree (databa
21073 73 65 20 74 61 62 6c 65 2c 20 64 61 74 61 62 61  se table, databa
21074 73 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 72 20  se index .** or 
21075 65 70 68 65 72 6d 61 6c 20 74 61 62 6c 65 29 20  ephermal table) 
21076 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 58 2d  is stored in pX-
21077 3e 69 54 61 62 6c 65 20 62 65 66 6f 72 65 20 74  >iTable before t
21078 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
21079 75 72 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 65 74  urns..** The ret
2107a 75 72 6e 65 64 20 76 61 6c 75 65 20 6f 66 20 74  urned value of t
2107b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64  his function ind
2107c 69 63 61 74 65 73 20 74 68 65 20 62 2d 74 72 65  icates the b-tre
2107d 65 20 74 79 70 65 2c 20 61 73 20 66 6f 6c 6c 6f  e type, as follo
2107e 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 5f 49  ws:.**.**   IN_I
2107f 4e 44 45 58 5f 52 4f 57 49 44 20 2d 20 54 68 65  NDEX_ROWID - The
21080 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e   cursor was open
21081 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  ed on a database
21082 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 49 4e 5f   table..**   IN_
21083 49 4e 44 45 58 5f 49 4e 44 45 58 20 2d 20 54 68  INDEX_INDEX - Th
21084 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65  e cursor was ope
21085 6e 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73  ned on a databas
21086 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 20 20 49 4e  e index..**   IN
21087 5f 49 4e 44 45 58 5f 45 50 48 20 2d 20 20 20 54  _INDEX_EPH -   T
21088 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70  he cursor was op
21089 65 6e 65 64 20 6f 6e 20 61 20 73 70 65 63 69 61  ened on a specia
2108a 6c 6c 79 20 63 72 65 61 74 65 64 20 61 6e 64 0a  lly created and.
2108b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2108c 20 20 20 20 20 20 70 6f 70 75 6c 61 74 65 64 20        populated 
2108d 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e  epheremal table.
2108e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 69 73 74 69  .**.** An existi
2108f 6e 67 20 62 2d 74 72 65 65 20 6d 61 79 20 6f 6e  ng b-tree may on
21090 6c 79 20 62 65 20 75 73 65 64 20 69 66 20 74 68  ly be used if th
21091 65 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74  e SELECT is of t
21092 68 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 66 6f 72  he simple.** for
21093 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c  m:.**.**     SEL
21094 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f  ECT <column> FRO
21095 4d 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20  M <table>.**.** 
21096 49 66 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e  If the prNotFoun
21097 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30  d parameter is 0
21098 2c 20 74 68 65 6e 20 74 68 65 20 62 2d 74 72 65  , then the b-tre
21099 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74  e will be used t
2109a 6f 20 69 74 65 72 61 74 65 0a 2a 2a 20 74 68 72  o iterate.** thr
2109b 6f 75 67 68 20 74 68 65 20 73 65 74 20 6d 65 6d  ough the set mem
2109c 62 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 20 61  bers, skipping a
2109d 6e 79 20 64 75 70 6c 69 63 61 74 65 73 2e 20 49  ny duplicates. I
2109e 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 0a 2a  n this case an.*
2109f 2a 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c  * epheremal tabl
210a0 65 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 75  e must be used u
210a1 6e 6c 65 73 73 20 74 68 65 20 73 65 6c 65 63 74  nless the select
210a2 65 64 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 67  ed <column> is g
210a3 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20  uaranteed.** to 
210a4 62 65 20 75 6e 69 71 75 65 20 2d 20 65 69 74 68  be unique - eith
210a5 65 72 20 62 65 63 61 75 73 65 20 69 74 20 69 73  er because it is
210a6 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   an INTEGER PRIM
210a7 41 52 59 20 4b 45 59 20 6f 72 20 69 74 0a 2a 2a  ARY KEY or it.**
210a8 20 68 61 73 20 61 20 55 4e 49 51 55 45 20 63 6f   has a UNIQUE co
210a9 6e 73 74 72 61 69 6e 74 20 6f 72 20 55 4e 49 51  nstraint or UNIQ
210aa 55 45 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  UE index..**.** 
210ab 49 66 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e  If the prNotFoun
210ac 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  d parameter is n
210ad 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 20 62  ot 0, then the b
210ae 2d 74 72 65 65 20 77 69 6c 6c 20 62 65 20 75 73  -tree will be us
210af 65 64 20 0a 2a 2a 20 66 6f 72 20 66 61 73 74 20  ed .** for fast 
210b0 73 65 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74  set membership t
210b1 65 73 74 73 2e 20 49 6e 20 74 68 69 73 20 63 61  ests. In this ca
210b2 73 65 20 61 6e 20 65 70 68 65 72 65 6d 61 6c 20  se an epheremal 
210b3 74 61 62 6c 65 20 6d 75 73 74 20 0a 2a 2a 20 62  table must .** b
210b4 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20 3c 63  e used unless <c
210b5 6f 6c 75 6d 6e 3e 20 69 73 20 61 6e 20 49 4e 54  olumn> is an INT
210b6 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
210b7 20 6f 72 20 61 6e 20 69 6e 64 65 78 20 63 61 6e   or an index can
210b8 20 0a 2a 2a 20 62 65 20 66 6f 75 6e 64 20 77 69   .** be found wi
210b9 74 68 20 3c 63 6f 6c 75 6d 6e 3e 20 61 73 20 69  th <column> as i
210ba 74 73 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c  ts left-most col
210bb 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  umn..**.** When 
210bc 74 68 65 20 62 2d 74 72 65 65 20 69 73 20 62 65  the b-tree is be
210bd 69 6e 67 20 75 73 65 64 20 66 6f 72 20 6d 65 6d  ing used for mem
210be 62 65 72 73 68 69 70 20 74 65 73 74 73 2c 20 74  bership tests, t
210bf 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
210c0 69 6f 6e 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20  ion.** needs to 
210c1 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20  know whether or 
210c2 6e 6f 74 20 74 68 65 20 73 74 72 75 63 74 75 72  not the structur
210c3 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51  e contains an SQ
210c4 4c 20 4e 55 4c 4c 20 0a 2a 2a 20 76 61 6c 75 65  L NULL .** value
210c5 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 72   in order to cor
210c6 72 65 63 74 6c 79 20 65 76 61 6c 75 61 74 65 20  rectly evaluate 
210c7 65 78 70 72 65 73 73 69 6f 6e 73 20 6c 69 6b 65  expressions like
210c8 20 22 58 20 49 4e 20 28 59 2c 20 5a 29 22 2e 0a   "X IN (Y, Z)"..
210c9 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  ** If there is a
210ca 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65   chance that the
210cb 20 62 2d 74 72 65 65 20 6d 69 67 68 74 20 63 6f   b-tree might co
210cc 6e 74 61 69 6e 20 61 20 4e 55 4c 4c 20 76 61 6c  ntain a NULL val
210cd 75 65 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65  ue at.** runtime
210ce 2c 20 74 68 65 6e 20 61 20 72 65 67 69 73 74 65  , then a registe
210cf 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  r is allocated a
210d0 6e 64 20 74 68 65 20 72 65 67 69 73 74 65 72 20  nd the register 
210d1 6e 75 6d 62 65 72 20 77 72 69 74 74 65 6e 0a 2a  number written.*
210d2 2a 20 74 6f 20 2a 70 72 4e 6f 74 46 6f 75 6e 64  * to *prNotFound
210d3 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  . If there is no
210d4 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65   chance that the
210d5 20 62 2d 74 72 65 65 20 63 6f 6e 74 61 69 6e 73   b-tree contains
210d6 20 61 0a 2a 2a 20 4e 55 4c 4c 20 76 61 6c 75 65   a.** NULL value
210d7 2c 20 74 68 65 6e 20 2a 70 72 4e 6f 74 46 6f 75  , then *prNotFou
210d8 6e 64 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61  nd is left uncha
210d9 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  nged..**.** If a
210da 20 72 65 67 69 73 74 65 72 20 69 73 20 61 6c 6c   register is all
210db 6f 63 61 74 65 64 20 61 6e 64 20 69 74 73 20 6c  ocated and its l
210dc 6f 63 61 74 69 6f 6e 20 73 74 6f 72 65 64 20 69  ocation stored i
210dd 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2c 20 74  n *prNotFound, t
210de 68 65 6e 0a 2a 2a 20 69 74 73 20 69 6e 69 74 69  hen.** its initi
210df 61 6c 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c  al value is NULL
210e0 2e 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 20  . If the b-tree 
210e1 64 6f 65 73 20 6e 6f 74 20 72 65 6d 61 69 6e 20  does not remain 
210e2 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 66 6f 72 20  constant.** for 
210e3 74 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20  the duration of 
210e4 74 68 65 20 71 75 65 72 79 20 28 69 2e 65 2e 20  the query (i.e. 
210e5 74 68 65 20 53 45 4c 45 43 54 20 74 68 61 74 20  the SELECT that 
210e6 67 65 6e 65 72 61 74 65 73 20 74 68 65 20 62 2d  generates the b-
210e7 74 72 65 65 0a 2a 2a 20 69 73 20 61 20 63 6f 72  tree.** is a cor
210e8 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79  related subquery
210e9 29 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  ) then the value
210ea 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65   of the allocate
210eb 64 20 72 65 67 69 73 74 65 72 20 69 73 0a 2a 2a  d register is.**
210ec 20 72 65 73 65 74 20 74 6f 20 4e 55 4c 4c 20 65   reset to NULL e
210ed 61 63 68 20 74 69 6d 65 20 74 68 65 20 62 2d 74  ach time the b-t
210ee 72 65 65 20 69 73 20 72 65 70 6f 70 75 6c 61 74  ree is repopulat
210ef 65 64 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20  ed. This allows 
210f0 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f  the.** caller to
210f1 20 75 73 65 20 76 64 62 65 20 63 6f 64 65 20 65   use vdbe code e
210f2 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65  quivalent to the
210f3 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
210f4 2a 20 20 20 69 66 28 20 72 65 67 69 73 74 65 72  *   if( register
210f5 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20  ==NULL ){.**    
210f6 20 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73   has_null = <tes
210f7 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74  t if data struct
210f8 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c  ure contains nul
210f9 6c 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73 74  l>.**     regist
210fa 65 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a  er = 1.**   }.**
210fb 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  .** in order to 
210fc 61 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 68  avoid running th
210fd 65 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20  e <test if data 
210fe 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
210ff 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74  ns null>.** test
21100 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e   more often than
21101 20 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   is necessary..*
21102 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
21103 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53  _OMIT_SUBQUERY.S
21104 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
21105 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49  t sqlite3FindInI
21106 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72  ndex(Parse *pPar
21107 73 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69 6e  se, Expr *pX, in
21108 74 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a  t *prNotFound){.
21109 20 20 53 65 6c 65 63 74 20 2a 70 3b 20 20 20 20    Select *p;    
2110a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2110b 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43          /* SELEC
2110c 54 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 6f  T to the right o
2110d 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  f IN operator */
2110e 0a 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 30  .  int eType = 0
2110f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
21110 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65           /* Type
21111 20 6f 66 20 52 48 53 20 74 61 62 6c 65 2e 20 49   of RHS table. I
21112 4e 5f 49 4e 44 45 58 5f 2a 20 2a 2f 0a 20 20 69  N_INDEX_* */.  i
21113 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 73 65  nt iTab = pParse
21114 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 20 20  ->nTab++;       
21115 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f       /* Cursor o
21116 66 20 74 68 65 20 52 48 53 20 74 61 62 6c 65 20  f the RHS table 
21117 2a 2f 0a 20 20 69 6e 74 20 6d 75 73 74 42 65 55  */.  int mustBeU
21118 6e 69 71 75 65 20 3d 20 28 70 72 4e 6f 74 46 6f  nique = (prNotFo
21119 75 6e 64 3d 3d 30 29 3b 20 20 20 2f 2a 20 54 72  und==0);   /* Tr
2111a 75 65 20 69 66 20 52 48 53 20 6d 75 73 74 20 62  ue if RHS must b
2111b 65 20 75 6e 69 71 75 65 20 2a 2f 0a 0a 20 20 61  e unique */..  a
2111c 73 73 65 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54  ssert( pX->op==T
2111d 4b 5f 49 4e 20 29 3b 0a 0a 20 20 2f 2a 20 43 68  K_IN );..  /* Ch
2111e 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 61 6e  eck to see if an
2111f 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65 20   existing table 
21120 6f 72 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20  or index can be 
21121 75 73 65 64 20 74 6f 0a 20 20 2a 2a 20 73 61 74  used to.  ** sat
21122 69 73 66 79 20 74 68 65 20 71 75 65 72 79 2e 20  isfy the query. 
21123 20 54 68 69 73 20 69 73 20 70 72 65 66 65 72 61   This is prefera
21124 62 6c 65 20 74 6f 20 67 65 6e 65 72 61 74 69 6e  ble to generatin
21125 67 20 61 20 6e 65 77 20 0a 20 20 2a 2a 20 65 70  g a new .  ** ep
21126 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20  hemeral table.. 
21127 20 2a 2f 0a 20 20 70 20 3d 20 28 45 78 70 72 48   */.  p = (ExprH
21128 61 73 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45  asProperty(pX, E
21129 50 5f 78 49 73 53 65 6c 65 63 74 29 20 3f 20 70  P_xIsSelect) ? p
2112a 58 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3a 20 30  X->x.pSelect : 0
2112b 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  );.  if( ALWAYS(
2112c 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 29  pParse->nErr==0)
2112d 20 26 26 20 69 73 43 61 6e 64 69 64 61 74 65 46   && isCandidateF
2112e 6f 72 49 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20  orInOpt(p) ){.  
2112f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
21130 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
21131 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
21132 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
21133 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78  */.    Expr *pEx
21134 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e  pr = p->pEList->
21135 61 5b 30 5d 2e 70 45 78 70 72 3b 20 20 20 2f 2a  a[0].pExpr;   /*
21136 20 45 78 70 72 65 73 73 69 6f 6e 20 3c 63 6f 6c   Expression <col
21137 75 6d 6e 3e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  umn> */.    int 
21138 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43  iCol = pExpr->iC
21139 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
2113a 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
2113b 6f 6c 75 6d 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a  olumn <column> *
2113c 2f 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20  /.    Vdbe *v = 
2113d 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2113e 50 61 72 73 65 29 3b 20 20 20 20 20 20 2f 2a 20  Parse);      /* 
2113f 56 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  Virtual machine 
21140 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20  being coded */. 
21141 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
21142 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70   p->pSrc->a[0].p
21143 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54 61 62  Tab;      /* Tab
21144 6c 65 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20  le <table>. */. 
21145 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20     int iDb;     
21146 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21147 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
21148 61 62 61 73 65 20 69 64 78 20 66 6f 72 20 70 54  abase idx for pT
21149 61 62 20 2a 2f 0a 20 20 20 0a 20 20 20 20 2f 2a  ab */.   .    /*
2114a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 56 65 72 69   Code an OP_Veri
2114b 66 79 43 6f 6f 6b 69 65 20 61 6e 64 20 4f 50 5f  fyCookie and OP_
2114c 54 61 62 6c 65 4c 6f 63 6b 20 66 6f 72 20 3c 74  TableLock for <t
2114d 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 44  able>. */.    iD
2114e 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2114f 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
21150 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
21151 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69   sqlite3CodeVeri
21152 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c  fySchema(pParse,
21153 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
21154 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72  e3TableLock(pPar
21155 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74  se, iDb, pTab->t
21156 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e  num, 0, pTab->zN
21157 61 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68  ame);..    /* Th
21158 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f  is function is o
21159 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20  nly called from 
2115a 74 77 6f 20 70 6c 61 63 65 73 2e 20 49 6e 20 62  two places. In b
2115b 6f 74 68 20 63 61 73 65 73 20 74 68 65 20 76 64  oth cases the vd
2115c 62 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 61 6c  be.    ** has al
2115d 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63  ready been alloc
2115e 61 74 65 64 2e 20 53 6f 20 61 73 73 75 6d 65 20  ated. So assume 
2115f 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 29  sqlite3GetVdbe()
21160 20 69 73 20 61 6c 77 61 79 73 0a 20 20 20 20 2a   is always.    *
21161 2a 20 73 75 63 63 65 73 73 66 75 6c 20 68 65 72  * successful her
21162 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  e..    */.    as
21163 73 65 72 74 28 76 29 3b 0a 20 20 20 20 69 66 28  sert(v);.    if(
21164 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20   iCol<0 ){.     
21165 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50   int iMem = ++pP
21166 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
21167 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 0a 20 20    int iAddr;..  
21168 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69      iAddr = sqli
21169 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2116a 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20   OP_If, iMem);. 
2116b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2116c 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2116d 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a  eger, 1, iMem);.
2116e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70  .      sqlite3Op
2116f 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  enTable(pParse, 
21170 69 54 61 62 2c 20 69 44 62 2c 20 70 54 61 62 2c  iTab, iDb, pTab,
21171 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20   OP_OpenRead);. 
21172 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f       eType = IN_
21173 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 0a 20 20  INDEX_ROWID;..  
21174 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
21175 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64 64 72  umpHere(v, iAddr
21176 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
21177 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b      Index *pIdx;
21178 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21179 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
2117a 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
2117b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  ..      /* The c
2117c 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2117d 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 6f  e used by the co
2117e 6d 70 61 72 69 73 6f 6e 2e 20 49 66 20 61 6e 20  mparison. If an 
2117f 69 6e 64 65 78 20 69 73 20 74 6f 0a 20 20 20 20  index is to.    
21180 20 20 2a 2a 20 62 65 20 75 73 65 64 20 69 6e 20    ** be used in 
21181 70 6c 61 63 65 20 6f 66 20 61 20 74 65 6d 70 2d  place of a temp-
21182 74 61 62 6c 65 2c 20 69 74 20 6d 75 73 74 20 62  table, it must b
21183 65 20 6f 72 64 65 72 65 64 20 61 63 63 6f 72 64  e ordered accord
21184 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ing.      ** to 
21185 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  this collation s
21186 65 71 75 65 6e 63 65 2e 20 20 2a 2f 0a 20 20 20  equence.  */.   
21187 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 71     CollSeq *pReq
21188 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79   = sqlite3Binary
21189 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70  CompareCollSeq(p
2118a 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74  Parse, pX->pLeft
2118b 2c 20 70 45 78 70 72 29 3b 0a 0a 20 20 20 20 20  , pExpr);..     
2118c 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74   /* Check that t
2118d 68 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74  he affinity that
2118e 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f   will be used to
2118f 20 70 65 72 66 6f 72 6d 20 74 68 65 20 0a 20 20   perform the .  
21190 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f      ** compariso
21191 6e 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  n is the same as
21192 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   the affinity of
21193 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a   the column. If.
21194 20 20 20 20 20 20 2a 2a 20 69 74 20 69 73 20 6e        ** it is n
21195 6f 74 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f  ot, it is not po
21196 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e  ssible to use an
21197 79 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 2a  y index..      *
21198 2f 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66  /.      char aff
21199 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66   = comparisonAff
2119a 69 6e 69 74 79 28 70 58 29 3b 0a 20 20 20 20 20  inity(pX);.     
2119b 20 69 6e 74 20 61 66 66 69 6e 69 74 79 5f 6f 6b   int affinity_ok
2119c 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69   = (pTab->aCol[i
2119d 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 61  Col].affinity==a
2119e 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49 54 45 5f  ff||aff==SQLITE_
2119f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 20 20 20  AFF_NONE);..    
211a0 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
211a1 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 20 26 26  >pIndex; pIdx &&
211a2 20 65 54 79 70 65 3d 3d 30 20 26 26 20 61 66 66   eType==0 && aff
211a3 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64 78 3d 70  inity_ok; pIdx=p
211a4 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
211a5 20 20 20 20 20 69 66 28 20 28 70 49 64 78 2d 3e       if( (pIdx->
211a6 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 43 6f  aiColumn[0]==iCo
211a7 6c 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 73  l).         && s
211a8 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
211a9 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 70  q(db, ENC(db), p
211aa 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 2c 20  Idx->azColl[0], 
211ab 30 29 3d 3d 70 52 65 71 0a 20 20 20 20 20 20 20  0)==pReq.       
211ac 20 20 26 26 20 28 21 6d 75 73 74 42 65 55 6e 69    && (!mustBeUni
211ad 71 75 65 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43  que || (pIdx->nC
211ae 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78  olumn==1 && pIdx
211af 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f  ->onError!=OE_No
211b0 6e 65 29 29 0a 20 20 20 20 20 20 20 20 29 7b 0a  ne)).        ){.
211b1 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 4d            int iM
211b2 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  em = ++pParse->n
211b3 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69  Mem;.          i
211b4 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20  nt iAddr;.      
211b5 20 20 20 20 63 68 61 72 20 2a 70 4b 65 79 3b 0a      char *pKey;.
211b6 20 20 0a 20 20 20 20 20 20 20 20 20 20 70 4b 65    .          pKe
211b7 79 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  y = (char *)sqli
211b8 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
211b9 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20  pParse, pIdx);. 
211ba 20 20 20 20 20 20 20 20 20 69 41 64 64 72 20 3d           iAddr =
211bb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
211bc 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65  p1(v, OP_If, iMe
211bd 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  m);.          sq
211be 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
211bf 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
211c0 2c 20 69 4d 65 6d 29 3b 0a 20 20 0a 20 20 20 20  , iMem);.  .    
211c1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
211c2 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70  eAddOp4(v, OP_Op
211c3 65 6e 52 65 61 64 2c 20 69 54 61 62 2c 20 70 49  enRead, iTab, pI
211c4 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20  dx->tnum, iDb,. 
211c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
211c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4b                pK
211c7 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  ey,P4_KEYINFO_HA
211c8 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 20 20  NDOFF);.        
211c9 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
211ca 2c 20 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e  , "%s", pIdx->zN
211cb 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20  ame));.         
211cc 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45   eType = IN_INDE
211cd 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20 20 20 20  X_INDEX;..      
211ce 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
211cf 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64 64 72  umpHere(v, iAddr
211d0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
211d1 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26 26 20 21   prNotFound && !
211d2 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
211d3 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  .notNull ){.    
211d4 20 20 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f          *prNotFo
211d5 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  und = ++pParse->
211d6 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20  nMem;.          
211d7 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
211d8 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
211d9 20 69 66 28 20 65 54 79 70 65 3d 3d 30 20 29 7b   if( eType==0 ){
211da 0a 20 20 20 20 2f 2a 20 43 6f 75 6c 64 20 6e 6f  .    /* Could no
211db 74 20 66 6f 75 6e 64 20 61 6e 20 65 78 69 73 74  t found an exist
211dc 69 6e 67 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  ing table or ind
211dd 65 78 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  ex to use as the
211de 20 52 48 53 20 62 2d 74 72 65 65 2e 0a 20 20 20   RHS b-tree..   
211df 20 2a 2a 20 57 65 20 77 69 6c 6c 20 68 61 76 65   ** We will have
211e0 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e 20   to generate an 
211e1 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  ephemeral table 
211e2 74 6f 20 64 6f 20 74 68 65 20 6a 6f 62 2e 0a 20  to do the job.. 
211e3 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 4d     */.    int rM
211e4 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a  ayHaveNull = 0;.
211e5 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49      eType = IN_I
211e6 4e 44 45 58 5f 45 50 48 3b 0a 20 20 20 20 69 66  NDEX_EPH;.    if
211e7 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a  ( prNotFound ){.
211e8 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e        *prNotFoun
211e9 64 20 3d 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c  d = rMayHaveNull
211ea 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
211eb 6d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  m;.    }else if(
211ec 20 70 58 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c   pX->pLeft->iCol
211ed 75 6d 6e 3c 30 20 26 26 20 21 45 78 70 72 48 61  umn<0 && !ExprHa
211ee 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 58 2c  sAnyProperty(pX,
211ef 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
211f0 7b 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d 20  {.      eType = 
211f1 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a  IN_INDEX_ROWID;.
211f2 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
211f3 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70  3CodeSubselect(p
211f4 50 61 72 73 65 2c 20 70 58 2c 20 72 4d 61 79 48  Parse, pX, rMayH
211f5 61 76 65 4e 75 6c 6c 2c 20 65 54 79 70 65 3d 3d  aveNull, eType==
211f6 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 29 3b  IN_INDEX_ROWID);
211f7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 58  .  }else{.    pX
211f8 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b  ->iTable = iTab;
211f9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 65 54  .  }.  return eT
211fa 79 70 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ype;.}.#endif../
211fb 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
211fc 64 65 20 66 6f 72 20 73 63 61 6c 61 72 20 73 75  de for scalar su
211fd 62 71 75 65 72 69 65 73 20 75 73 65 64 20 61 73  bqueries used as
211fe 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a   an expression.*
211ff 2a 20 61 6e 64 20 49 4e 20 6f 70 65 72 61 74 6f  * and IN operato
21200 72 73 2e 20 20 45 78 61 6d 70 6c 65 73 3a 0a 2a  rs.  Examples:.*
21201 2a 0a 2a 2a 20 20 20 20 20 28 53 45 4c 45 43 54  *.**     (SELECT
21202 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 20 20   a FROM b)      
21203 20 20 20 20 2d 2d 20 73 75 62 71 75 65 72 79 0a      -- subquery.
21204 2a 2a 20 20 20 20 20 45 58 49 53 54 53 20 28 53  **     EXISTS (S
21205 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20  ELECT a FROM b) 
21206 20 20 2d 2d 20 45 58 49 53 54 53 20 73 75 62 71    -- EXISTS subq
21207 75 65 72 79 0a 2a 2a 20 20 20 20 20 78 20 49 4e  uery.**     x IN
21208 20 28 34 2c 35 2c 31 31 29 20 20 20 20 20 20 20   (4,5,11)       
21209 20 20 20 20 20 20 20 2d 2d 20 49 4e 20 6f 70 65         -- IN ope
2120a 72 61 74 6f 72 20 77 69 74 68 20 6c 69 73 74 20  rator with list 
2120b 6f 6e 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  on right-hand si
2120c 64 65 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28  de.**     x IN (
2120d 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29  SELECT a FROM b)
2120e 20 20 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61       -- IN opera
2120f 74 6f 72 20 77 69 74 68 20 73 75 62 71 75 65 72  tor with subquer
21210 79 20 6f 6e 20 74 68 65 20 72 69 67 68 74 0a 2a  y on the right.*
21211 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70 72 20 70  *.** The pExpr p
21212 61 72 61 6d 65 74 65 72 20 64 65 73 63 72 69 62  arameter describ
21213 65 73 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  es the expressio
21214 6e 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  n that contains 
21215 74 68 65 20 49 4e 0a 2a 2a 20 6f 70 65 72 61 74  the IN.** operat
21216 6f 72 20 6f 72 20 73 75 62 71 75 65 72 79 2e 0a  or or subquery..
21217 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74  **.** If paramet
21218 65 72 20 69 73 52 6f 77 69 64 20 69 73 20 6e 6f  er isRowid is no
21219 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 65 78 70  n-zero, then exp
2121a 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 73  ression pExpr is
2121b 20 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74   guaranteed.** t
2121c 6f 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d  o be of the form
2121d 20 22 3c 72 6f 77 69 64 3e 20 49 4e 20 28 3f 2c   "<rowid> IN (?,
2121e 20 3f 2c 20 3f 29 22 2c 20 77 68 65 72 65 20 3c   ?, ?)", where <
2121f 72 6f 77 69 64 3e 20 69 73 20 61 20 72 65 66 65  rowid> is a refe
21220 72 65 6e 63 65 0a 2a 2a 20 74 6f 20 73 6f 6d 65  rence.** to some
21221 20 69 6e 74 65 67 65 72 20 6b 65 79 20 63 6f 6c   integer key col
21222 75 6d 6e 20 6f 66 20 61 20 74 61 62 6c 65 20 42  umn of a table B
21223 2d 54 72 65 65 2e 20 49 6e 20 74 68 69 73 20 63  -Tree. In this c
21224 61 73 65 2c 20 75 73 65 20 61 6e 0a 2a 2a 20 69  ase, use an.** i
21225 6e 74 6b 65 79 20 42 2d 54 72 65 65 20 74 6f 20  ntkey B-Tree to 
21226 73 74 6f 72 65 20 74 68 65 20 73 65 74 20 6f 66  store the set of
21227 20 49 4e 28 2e 2e 2e 29 20 76 61 6c 75 65 73 20   IN(...) values 
21228 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 75  instead of the u
21229 73 75 61 6c 0a 2a 2a 20 28 73 6c 6f 77 65 72 29  sual.** (slower)
2122a 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   variable length
2122b 20 6b 65 79 73 20 42 2d 54 72 65 65 2e 0a 2a 2a   keys B-Tree..**
2122c 0a 2a 2a 20 49 66 20 72 4d 61 79 48 61 76 65 4e  .** If rMayHaveN
2122d 75 6c 6c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  ull is non-zero,
2122e 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
2122f 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69   the operation i
21230 73 20 61 6e 20 49 4e 0a 2a 2a 20 28 6e 6f 74 20  s an IN.** (not 
21231 61 20 53 45 4c 45 43 54 20 6f 72 20 45 58 49 53  a SELECT or EXIS
21232 54 53 29 20 61 6e 64 20 74 68 61 74 20 74 68 65  TS) and that the
21233 20 52 48 53 20 6d 69 67 68 74 20 63 6f 6e 74 61   RHS might conta
21234 69 6e 73 20 4e 55 4c 4c 73 2e 0a 2a 2a 20 46 75  ins NULLs..** Fu
21235 72 74 68 65 72 6d 6f 72 65 2c 20 74 68 65 20 49  rthermore, the I
21236 4e 20 69 73 20 69 6e 20 61 20 57 48 45 52 45 20  N is in a WHERE 
21237 63 6c 61 75 73 65 20 61 6e 64 20 74 68 61 74 20  clause and that 
21238 77 65 20 72 65 61 6c 6c 79 20 77 61 6e 74 0a 2a  we really want.*
21239 2a 20 74 6f 20 69 74 65 72 61 74 65 20 6f 76 65  * to iterate ove
2123a 72 20 74 68 65 20 52 48 53 20 6f 66 20 74 68 65  r the RHS of the
2123b 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 69 6e 20   IN operator in 
2123c 6f 72 64 65 72 20 74 6f 20 71 75 69 63 6b 6c 79  order to quickly
2123d 20 6c 6f 63 61 74 65 0a 2a 2a 20 61 6c 6c 20 63   locate.** all c
2123e 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 4c 48 53  orresponding LHS
2123f 20 65 6c 65 6d 65 6e 74 73 2e 20 20 41 6c 6c 20   elements.  All 
21240 74 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  this routine doe
21241 73 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 0a  s is initialize.
21242 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20  ** the register 
21243 67 69 76 65 6e 20 62 79 20 72 4d 61 79 48 61 76  given by rMayHav
21244 65 4e 75 6c 6c 20 74 6f 20 4e 55 4c 4c 2e 20 20  eNull to NULL.  
21245 43 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 73  Calling routines
21246 20 77 69 6c 6c 20 74 61 6b 65 0a 2a 2a 20 63 61   will take.** ca
21247 72 65 20 6f 66 20 63 68 61 6e 67 69 6e 67 20 74  re of changing t
21248 68 69 73 20 72 65 67 69 73 74 65 72 20 76 61 6c  his register val
21249 75 65 20 74 6f 20 6e 6f 6e 2d 4e 55 4c 4c 20 69  ue to non-NULL i
2124a 66 20 74 68 65 20 52 48 53 20 69 73 20 4e 55 4c  f the RHS is NUL
2124b 4c 2d 66 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  L-free..**.** If
2124c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 73   rMayHaveNull is
2124d 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e   zero, that mean
2124e 73 20 74 68 61 74 20 74 68 65 20 73 75 62 71 75  s that the subqu
2124f 65 72 79 20 69 73 20 62 65 69 6e 67 20 75 73 65  ery is being use
21250 64 0a 2a 2a 20 66 6f 72 20 6d 65 6d 62 65 72 73  d.** for members
21251 68 69 70 20 74 65 73 74 69 6e 67 20 6f 6e 6c 79  hip testing only
21252 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e  .  There is no n
21253 65 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a  eed to initializ
21254 65 20 61 6e 79 0a 2a 2a 20 72 65 67 69 73 74 65  e any.** registe
21255 72 73 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  rs to indicate t
21256 68 65 20 70 72 65 73 65 6e 73 65 20 6f 72 20 61  he presense or a
21257 62 73 65 6e 63 65 20 6f 66 20 4e 55 4c 4c 73 20  bsence of NULLs 
21258 6f 6e 20 74 68 65 20 52 48 53 2e 0a 2a 2a 0a 2a  on the RHS..**.*
21259 2a 20 46 6f 72 20 61 20 53 45 4c 45 43 54 20 6f  * For a SELECT o
2125a 72 20 45 58 49 53 54 53 20 6f 70 65 72 61 74 6f  r EXISTS operato
2125b 72 2c 20 72 65 74 75 72 6e 20 74 68 65 20 72 65  r, return the re
2125c 67 69 73 74 65 72 20 74 68 61 74 20 68 6f 6c 64  gister that hold
2125d 73 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2e  s the.** result.
2125e 20 20 46 6f 72 20 49 4e 20 6f 70 65 72 61 74 6f    For IN operato
2125f 72 73 20 6f 72 20 69 66 20 61 6e 20 65 72 72 6f  rs or if an erro
21260 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 72 65  r occurs, the re
21261 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 30 2e  turn value is 0.
21262 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
21263 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
21264 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21265 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 53  int sqlite3CodeS
21266 75 62 73 65 6c 65 63 74 28 0a 20 20 50 61 72 73  ubselect(.  Pars
21267 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
21268 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
21269 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
2126a 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
2126b 20 20 20 20 2f 2a 20 54 68 65 20 49 4e 2c 20 53      /* The IN, S
2126c 45 4c 45 43 54 2c 20 6f 72 20 45 58 49 53 54 53  ELECT, or EXISTS
2126d 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69   operator */.  i
2126e 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c  nt rMayHaveNull,
2126f 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
21270 65 72 20 74 68 61 74 20 72 65 63 6f 72 64 73 20  er that records 
21271 77 68 65 74 68 65 72 20 4e 55 4c 4c 73 20 65 78  whether NULLs ex
21272 69 73 74 20 69 6e 20 52 48 53 20 2a 2f 0a 20 20  ist in RHS */.  
21273 69 6e 74 20 69 73 52 6f 77 69 64 20 20 20 20 20  int isRowid     
21274 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72          /* If tr
21275 75 65 2c 20 4c 48 53 20 6f 66 20 49 4e 20 6f 70  ue, LHS of IN op
21276 65 72 61 74 6f 72 20 69 73 20 61 20 72 6f 77 69  erator is a rowi
21277 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 74 65  d */.){.  int te
21278 73 74 41 64 64 72 20 3d 20 30 3b 20 20 20 20 20  stAddr = 0;     
21279 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2127a 20 20 2f 2a 20 4f 6e 65 2d 74 69 6d 65 20 74 65    /* One-time te
2127b 73 74 20 61 64 64 72 65 73 73 20 2a 2f 0a 20 20  st address */.  
2127c 69 6e 74 20 72 52 65 67 20 3d 20 30 3b 20 20 20  int rReg = 0;   
2127d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2127e 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
2127f 74 65 72 20 73 74 6f 72 69 6e 67 20 72 65 73 75  ter storing resu
21280 6c 74 69 6e 67 20 2a 2f 0a 20 20 56 64 62 65 20  lting */.  Vdbe 
21281 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
21282 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
21283 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29  f( NEVER(v==0) )
21284 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 71 6c   return 0;.  sql
21285 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73  ite3ExprCachePus
21286 68 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 2f 2a  h(pParse);..  /*
21287 20 54 68 69 73 20 63 6f 64 65 20 6d 75 73 74 20   This code must 
21288 62 65 20 72 75 6e 20 69 6e 20 69 74 73 20 65 6e  be run in its en
21289 74 69 72 65 74 79 20 65 76 65 72 79 20 74 69 6d  tirety every tim
2128a 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e 74 65  e it is encounte
2128b 72 65 64 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20  red.  ** if any 
2128c 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2128d 20 69 73 20 74 72 75 65 3a 0a 20 20 2a 2a 0a 20   is true:.  **. 
2128e 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69   **    *  The ri
2128f 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73  ght-hand side is
21290 20 61 20 63 6f 72 72 65 6c 61 74 65 64 20 73 75   a correlated su
21291 62 71 75 65 72 79 0a 20 20 2a 2a 20 20 20 20 2a  bquery.  **    *
21292 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64    The right-hand
21293 20 73 69 64 65 20 69 73 20 61 6e 20 65 78 70 72   side is an expr
21294 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e 74  ession list cont
21295 61 69 6e 69 6e 67 20 76 61 72 69 61 62 6c 65 73  aining variables
21296 0a 20 20 2a 2a 20 20 20 20 2a 20 20 57 65 20 61  .  **    *  We a
21297 72 65 20 69 6e 73 69 64 65 20 61 20 74 72 69 67  re inside a trig
21298 67 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  ger.  **.  ** If
21299 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76   all of the abov
2129a 65 20 61 72 65 20 66 61 6c 73 65 2c 20 74 68 65  e are false, the
2129b 6e 20 77 65 20 63 61 6e 20 72 75 6e 20 74 68 69  n we can run thi
2129c 73 20 63 6f 64 65 20 6a 75 73 74 20 6f 6e 63 65  s code just once
2129d 0a 20 20 2a 2a 20 73 61 76 65 20 74 68 65 20 72  .  ** save the r
2129e 65 73 75 6c 74 73 2c 20 61 6e 64 20 72 65 75 73  esults, and reus
2129f 65 20 74 68 65 20 73 61 6d 65 20 72 65 73 75 6c  e the same resul
212a0 74 20 6f 6e 20 73 75 62 73 65 71 75 65 6e 74 20  t on subsequent 
212a1 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a  invocations..  *
212a2 2f 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73  /.  if( !ExprHas
212a3 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70  AnyProperty(pExp
212a4 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29  r, EP_VarSelect)
212a5 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54 72   && !pParse->pTr
212a6 69 67 67 65 72 54 61 62 20 29 7b 0a 20 20 20 20  iggerTab ){.    
212a7 69 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70 50 61 72  int mem = ++pPar
212a8 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
212a9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
212aa 76 2c 20 4f 50 5f 49 66 2c 20 6d 65 6d 29 3b 0a  v, OP_If, mem);.
212ab 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 73      testAddr = s
212ac 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
212ad 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
212ae 31 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 61 73 73  1, mem);.    ass
212af 65 72 74 28 20 74 65 73 74 41 64 64 72 3e 30 20  ert( testAddr>0 
212b0 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  || pParse->db->m
212b1 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
212b2 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45   }..  switch( pE
212b3 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63  xpr->op ){.    c
212b4 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20  ase TK_IN: {.   
212b5 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79     char affinity
212b6 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20  ;.      KeyInfo 
212b7 6b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69  keyInfo;.      i
212b8 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20  nt addr;        
212b9 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50  /* Address of OP
212ba 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69  _OpenEphemeral i
212bb 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20  nstruction */.  
212bc 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20      Expr *pLeft 
212bd 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a  = pExpr->pLeft;.
212be 0a 20 20 20 20 20 20 69 66 28 20 72 4d 61 79 48  .      if( rMayH
212bf 61 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20  aveNull ){.     
212c0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
212c1 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
212c2 20 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c   0, rMayHaveNull
212c3 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
212c4 20 20 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c    affinity = sql
212c5 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
212c6 28 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20  (pLeft);..      
212c7 2f 2a 20 57 68 65 74 68 65 72 20 74 68 69 73 20  /* Whether this 
212c8 69 73 20 61 6e 20 27 78 20 49 4e 28 53 45 4c 45  is an 'x IN(SELE
212c9 43 54 2e 2e 2e 29 27 20 6f 72 20 61 6e 20 27 78  CT...)' or an 'x
212ca 20 49 4e 28 3c 65 78 70 72 6c 69 73 74 3e 29 27   IN(<exprlist>)'
212cb 0a 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73  .      ** expres
212cc 73 69 6f 6e 20 69 74 20 69 73 20 68 61 6e 64 6c  sion it is handl
212cd 65 64 20 74 68 65 20 73 61 6d 65 20 77 61 79 2e  ed the same way.
212ce 20 41 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   A virtual table
212cf 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69   is .      ** fi
212d0 6c 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c 65  lled with single
212d1 2d 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65 79  -field index key
212d2 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  s representing t
212d3 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20  he results.     
212d4 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45 4c   ** from the SEL
212d5 45 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70 72  ECT or the <expr
212d6 6c 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a  list>..      **.
212d7 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20        ** If the 
212d8 27 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20 69  'x' expression i
212d9 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  s a column value
212da 2c 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e  , or the SELECT.
212db 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74  ...      ** stat
212dc 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20  ement returns a 
212dd 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68  column value, th
212de 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  en the affinity 
212df 6f 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a  of that.      **
212e0 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20   column is used 
212e1 74 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e 64  to build the ind
212e2 65 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74 68  ex keys. If both
212e3 20 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20 20   'x' and the.   
212e4 20 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e 20     ** SELECT... 
212e5 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f  statement are co
212e6 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65  lumns, then nume
212e7 72 69 63 20 61 66 66 69 6e 69 74 79 20 69 73 20  ric affinity is 
212e8 75 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 66  used.      ** if
212e9 20 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20 68   either column h
212ea 61 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49 4e  as NUMERIC or IN
212eb 54 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e 20  TEGER affinity. 
212ec 49 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20 20  If neither.     
212ed 20 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65 20   ** 'x' nor the 
212ee 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d  SELECT... statem
212ef 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c  ent are columns,
212f0 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66   then numeric af
212f1 66 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a 20  finity.      ** 
212f2 69 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20 2a  is used..      *
212f3 2f 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69  /.      pExpr->i
212f4 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e  Table = pParse->
212f5 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61 64  nTab++;.      ad
212f6 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
212f7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65  AddOp2(v, OP_Ope
212f8 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 70  nEphemeral, pExp
212f9 72 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52 6f  r->iTable, !isRo
212fa 77 69 64 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73  wid);.      mems
212fb 65 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30 2c 20  et(&keyInfo, 0, 
212fc 73 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f 29 29  sizeof(keyInfo))
212fd 3b 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e  ;.      keyInfo.
212fe 6e 46 69 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20  nField = 1;..   
212ff 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
21300 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
21301 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
21302 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31         /* Case 1
21303 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 53  :     expr IN (S
21304 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20 20  ELECT ...).     
21305 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
21306 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
21307 6f 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75  o write the resu
21308 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63  lts of the selec
21309 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 6f  t into the tempo
2130a 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20  rary.        ** 
2130b 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64 20  table allocated 
2130c 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76 65  and opened above
2130d 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2130e 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20       SelectDest 
2130f 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45 78  dest;.        Ex
21310 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
21311 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
21312 20 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20 20   !isRowid );.   
21313 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
21314 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
21315 2c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70 72  , SRT_Set, pExpr
21316 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 20  ->iTable);.     
21317 20 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74 79     dest.affinity
21318 20 3d 20 28 75 38 29 61 66 66 69 6e 69 74 79 3b   = (u8)affinity;
21319 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2131a 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 26   (pExpr->iTable&
2131b 30 78 30 30 30 30 46 46 46 46 29 3d 3d 70 45 78  0x0000FFFF)==pEx
2131c 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20  pr->iTable );.  
2131d 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2131e 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
2131f 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
21320 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20  , &dest) ){.    
21321 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
21322 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
21323 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72    pEList = pExpr
21324 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c  ->x.pSelect->pEL
21325 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ist;.        if(
21326 20 41 4c 57 41 59 53 28 70 45 4c 69 73 74 21 3d   ALWAYS(pEList!=
21327 30 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78  0 && pEList->nEx
21328 70 72 3e 30 29 20 29 7b 20 0a 20 20 20 20 20 20  pr>0) ){ .      
21329 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c      keyInfo.aCol
2132a 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42 69  l[0] = sqlite3Bi
2132b 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53  naryCompareCollS
2132c 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  eq(pParse, pExpr
2132d 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20 20  ->pLeft,.       
2132e 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61         pEList->a
2132f 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  [0].pExpr);.    
21330 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
21331 65 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70  e if( pExpr->x.p
21332 4c 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20  List!=0 ){.     
21333 20 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20     /* Case 2:   
21334 20 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c    expr IN (exprl
21335 69 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ist).        **.
21336 20 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65          ** For e
21337 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20  ach expression, 
21338 62 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b  build an index k
21339 65 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c  ey from the eval
2133a 75 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20  uation and.     
2133b 20 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 69     ** store it i
2133c 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  n the temporary 
2133d 74 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e  table. If <expr>
2133e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68   is a column, th
2133f 65 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 2a  en use.        *
21340 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61  * that columns a
21341 66 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 69  ffinity when bui
21342 6c 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 73  lding index keys
21343 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 6e  . If <expr> is n
21344 6f 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20  ot.        ** a 
21345 63 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65  column, use nume
21346 72 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 20  ric affinity..  
21347 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
21348 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20   int i;.        
21349 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20  ExprList *pList 
2134a 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2134b 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74  ;.        struct
2134c 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
2134d 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69  pItem;.        i
2134e 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a  nt r1, r2, r3;..
2134f 20 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66          if( !aff
21350 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20  inity ){.       
21351 20 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51     affinity = SQ
21352 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20  LITE_AFF_NONE;. 
21353 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
21354 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30   keyInfo.aColl[0
21355 5d 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  ] = sqlite3ExprC
21356 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
21357 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20  Expr->pLeft);.. 
21358 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74         /* Loop t
21359 68 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72  hrough each expr
2135a 65 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c  ession in <exprl
2135b 69 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20  ist>. */.       
2135c 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
2135d 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2135e 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71  .        r2 = sq
2135f 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
21360 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20  pParse);.       
21361 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21362 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
21363 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66  , r2);.        f
21364 6f 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70  or(i=pList->nExp
21365 72 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  r, pItem=pList->
21366 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74  a; i>0; i--, pIt
21367 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  em++){.         
21368 20 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74   Expr *pE2 = pIt
21369 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  em->pExpr;.     
2136a 20 20 20 20 20 69 6e 74 20 69 56 61 6c 54 6f 49       int iValToI
2136b 6e 73 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  ns;..          /
2136c 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
2136d 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74  ion is not const
2136e 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c 6c  ant then we will
2136f 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20   need to.       
21370 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 68     ** disable th
21371 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73 20  e test that was 
21372 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 20  generated above 
21373 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 0a  that makes sure.
21374 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69            ** thi
21375 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63  s code only exec
21376 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61  utes once.  Beca
21377 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f  use for a non-co
21378 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 20  nstant.         
21379 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 77   ** expression w
2137a 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e 20  e need to rerun 
2137b 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 74  this code each t
2137c 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ime..          *
2137d 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
2137e 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71 6c  testAddr && !sql
2137f 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
21380 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 20  nt(pE2) ){.     
21381 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21382 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76  beChangeToNoop(v
21383 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 29  , testAddr-1, 2)
21384 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65  ;.            te
21385 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20  stAddr = 0;.    
21386 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
21387 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74     /* Evaluate t
21388 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  he expression an
21389 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f  d insert it into
2138a 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20   the temp table 
2138b 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  */.          if(
2138c 20 69 73 52 6f 77 69 64 20 26 26 20 73 71 6c 69   isRowid && sqli
2138d 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
2138e 28 70 45 32 2c 20 26 69 56 61 6c 54 6f 49 6e 73  (pE2, &iValToIns
2138f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
21390 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21391 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 49  p3(v, OP_InsertI
21392 6e 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  nt, pExpr->iTabl
21393 65 2c 20 72 32 2c 20 69 56 61 6c 54 6f 49 6e 73  e, r2, iValToIns
21394 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
21395 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
21396 72 33 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r3 = sqlite3Expr
21397 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73  CodeTarget(pPars
21398 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a 20 20 20  e, pE2, r1);.   
21399 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73 52           if( isR
2139a 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20  owid ){.        
2139b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2139c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75  eAddOp2(v, OP_Mu
2139d 73 74 42 65 49 6e 74 2c 20 72 33 2c 0a 20 20 20  stBeInt, r3,.   
2139e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2139f 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
213a0 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
213a1 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20  ddr(v)+2);.     
213a2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
213a3 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
213a4 5f 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e  _Insert, pExpr->
213a5 69 54 61 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b  iTable, r2, r3);
213a6 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c  .            }el
213a7 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
213a8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
213a9 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
213aa 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 2c  cord, r3, 1, r2,
213ab 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a   &affinity, 1);.
213ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
213ad 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
213ae 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
213af 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20 20  rse, r3, 1);.   
213b0 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
213b1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
213b2 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45  OP_IdxInsert, pE
213b3 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29  xpr->iTable, r2)
213b4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
213b5 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
213b6 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
213b7 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
213b8 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
213b9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
213ba 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
213bb 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20  Parse, r2);.    
213bc 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 69    }.      if( !i
213bd 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  sRowid ){.      
213be 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
213bf 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28  ngeP4(v, addr, (
213c0 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f 2c  void *)&keyInfo,
213c1 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20   P4_KEYINFO);.  
213c2 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
213c3 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61  k;.    }..    ca
213c4 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20  se TK_EXISTS:.  
213c5 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54    case TK_SELECT
213c6 3a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  :.    default: {
213c7 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69  .      /* If thi
213c8 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 63  s has to be a sc
213c9 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 65  alar SELECT.  Ge
213ca 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 70  nerate code to p
213cb 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  ut the.      ** 
213cc 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 65  value of this se
213cd 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79  lect in a memory
213ce 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 64   cell and record
213cf 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 20   the number.    
213d0 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f    ** of the memo
213d1 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75  ry cell in iColu
213d2 6d 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73 20  mn.  If this is 
213d3 61 6e 20 45 58 49 53 54 53 2c 20 77 72 69 74 65  an EXISTS, write
213d4 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74  .      ** an int
213d5 65 67 65 72 20 30 20 28 6e 6f 74 20 65 78 69 73  eger 0 (not exis
213d6 74 73 29 20 6f 72 20 31 20 28 65 78 69 73 74 73  ts) or 1 (exists
213d7 29 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72 79 20  ) into a memory 
213d8 63 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6e  cell.      ** an
213d9 64 20 72 65 63 6f 72 64 20 74 68 61 74 20 6d 65  d record that me
213da 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f  mory cell in iCo
213db 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  lumn..      */. 
213dc 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
213dd 74 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20  t Token one = { 
213de 22 31 22 2c 20 31 20 7d 3b 20 20 2f 2a 20 54 6f  "1", 1 };  /* To
213df 6b 65 6e 20 66 6f 72 20 6c 69 74 65 72 61 6c 20  ken for literal 
213e0 76 61 6c 75 65 20 31 20 2a 2f 0a 20 20 20 20 20  value 1 */.     
213e1 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20   Select *pSel;  
213e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213e3 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54         /* SELECT
213e4 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 6e   statement to en
213e5 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 53 65  code */.      Se
213e6 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 20  lectDest dest;  
213e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213e8 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 65      /* How to de
213e9 61 6c 20 77 69 74 68 20 53 45 4c 45 43 74 20 72  al with SELECt r
213ea 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 20 20 20 20  esult */..      
213eb 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d  testcase( pExpr-
213ec 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29  >op==TK_EXISTS )
213ed 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
213ee 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
213ef 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20  SELECT );.      
213f0 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
213f1 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 7c 7c 20  p==TK_EXISTS || 
213f2 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45  pExpr->op==TK_SE
213f3 4c 45 43 54 20 29 3b 0a 0a 20 20 20 20 20 20 61  LECT );..      a
213f4 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72  ssert( ExprHasPr
213f5 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
213f6 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20  _xIsSelect) );. 
213f7 20 20 20 20 20 70 53 65 6c 20 3d 20 70 45 78 70       pSel = pExp
213f8 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20  r->x.pSelect;.  
213f9 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
213fa 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c  tDestInit(&dest,
213fb 20 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d   0, ++pParse->nM
213fc 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  em);.      if( p
213fd 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c  Expr->op==TK_SEL
213fe 45 43 54 20 29 7b 0a 20 20 20 20 20 20 20 20 64  ECT ){.        d
213ff 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f  est.eDest = SRT_
21400 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  Mem;.        sql
21401 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21402 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65  , OP_Null, 0, de
21403 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20  st.iParm);.     
21404 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
21405 76 2c 20 22 49 6e 69 74 20 73 75 62 71 75 65 72  v, "Init subquer
21406 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20  y result"));.   
21407 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21408 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53    dest.eDest = S
21409 52 54 5f 45 78 69 73 74 73 3b 0a 20 20 20 20 20  RT_Exists;.     
2140a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2140b 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2140c 65 72 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72  er, 0, dest.iPar
2140d 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65  m);.        Vdbe
2140e 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69  Comment((v, "Ini
2140f 74 20 45 58 49 53 54 53 20 72 65 73 75 6c 74 22  t EXISTS result"
21410 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
21411 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
21412 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
21413 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20  pSel->pLimit);. 
21414 20 20 20 20 20 70 53 65 6c 2d 3e 70 4c 69 6d 69       pSel->pLimi
21415 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  t = sqlite3PExpr
21416 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54 45  (pParse, TK_INTE
21417 47 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e 65 29  GER, 0, 0, &one)
21418 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
21419 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
2141a 2c 20 70 53 65 6c 2c 20 26 64 65 73 74 29 20 29  , pSel, &dest) )
2141b 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
2141c 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
2141d 20 20 72 52 65 67 20 3d 20 64 65 73 74 2e 69 50    rReg = dest.iP
2141e 61 72 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 53  arm;.      ExprS
2141f 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45  etIrreducible(pE
21420 78 70 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61  xpr);.      brea
21421 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  k;.    }.  }..  
21422 69 66 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a  if( testAddr ){.
21423 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
21424 75 6d 70 48 65 72 65 28 76 2c 20 74 65 73 74 41  umpHere(v, testA
21425 64 64 72 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71  ddr-1);.  }.  sq
21426 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
21427 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 0a 20  p(pParse, 1);.. 
21428 20 72 65 74 75 72 6e 20 72 52 65 67 3b 0a 7d 0a   return rReg;.}.
21429 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2142a 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a  _OMIT_SUBQUERY *
2142b 2f 0a 0a 2f 2a 0a 2a 2a 20 44 75 70 6c 69 63 61  /../*.** Duplica
2142c 74 65 20 61 6e 20 38 2d 62 79 74 65 20 76 61 6c  te an 8-byte val
2142d 75 65 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  ue.*/.static cha
2142e 72 20 2a 64 75 70 38 62 79 74 65 73 28 56 64 62  r *dup8bytes(Vdb
2142f 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72  e *v, const char
21430 20 2a 69 6e 29 7b 0a 20 20 63 68 61 72 20 2a 6f   *in){.  char *o
21431 75 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ut = sqlite3DbMa
21432 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 56  llocRaw(sqlite3V
21433 64 62 65 44 62 28 76 29 2c 20 38 29 3b 0a 20 20  dbeDb(v), 8);.  
21434 69 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 6d  if( out ){.    m
21435 65 6d 63 70 79 28 6f 75 74 2c 20 69 6e 2c 20 38  emcpy(out, in, 8
21436 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
21437 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  out;.}../*.** Ge
21438 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75  nerate an instru
21439 63 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20  ction that will 
2143a 70 75 74 20 74 68 65 20 66 6c 6f 61 74 69 6e 67  put the floating
2143b 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 20   point.** value 
2143c 64 65 73 63 72 69 62 65 64 20 62 79 20 7a 5b 30  described by z[0
2143d 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69  ..n-1] into regi
2143e 73 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a  ster iMem..**.**
2143f 20 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20   The z[] string 
21440 77 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f  will probably no
21441 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  t be zero-termin
21442 61 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a  ated.  But the .
21443 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65  ** z[n] characte
21444 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  r is guaranteed 
21445 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20  to be something 
21446 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f  that does not lo
21447 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63  ok.** like the c
21448 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74  ontinuation of t
21449 68 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74  he number..*/.st
2144a 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 52 65  atic void codeRe
2144b 61 6c 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73  al(Vdbe *v, cons
2144c 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  t char *z, int n
2144d 65 67 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69  egateFlag, int i
2144e 4d 65 6d 29 7b 0a 20 20 69 66 28 20 41 4c 57 41  Mem){.  if( ALWA
2144f 59 53 28 7a 21 3d 30 29 20 29 7b 0a 20 20 20 20  YS(z!=0) ){.    
21450 64 6f 75 62 6c 65 20 76 61 6c 75 65 3b 0a 20 20  double value;.  
21451 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20    char *zV;.    
21452 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 2c 20 26  sqlite3AtoF(z, &
21453 76 61 6c 75 65 29 3b 0a 20 20 20 20 61 73 73 65  value);.    asse
21454 72 74 28 20 21 73 71 6c 69 74 65 33 49 73 4e 61  rt( !sqlite3IsNa
21455 4e 28 76 61 6c 75 65 29 20 29 3b 20 2f 2a 20 54  N(value) ); /* T
21456 68 65 20 6e 65 77 20 41 74 6f 46 20 6e 65 76 65  he new AtoF neve
21457 72 20 72 65 74 75 72 6e 73 20 4e 61 4e 20 2a 2f  r returns NaN */
21458 0a 20 20 20 20 69 66 28 20 6e 65 67 61 74 65 46  .    if( negateF
21459 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76  lag ) value = -v
2145a 61 6c 75 65 3b 0a 20 20 20 20 7a 56 20 3d 20 64  alue;.    zV = d
2145b 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61  up8bytes(v, (cha
2145c 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20  r*)&value);.    
2145d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2145e 34 28 76 2c 20 4f 50 5f 52 65 61 6c 2c 20 30 2c  4(v, OP_Real, 0,
2145f 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34   iMem, 0, zV, P4
21460 5f 52 45 41 4c 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a  _REAL);.  }.}...
21461 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61  /*.** Generate a
21462 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  n instruction th
21463 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20  at will put the 
21464 69 6e 74 65 67 65 72 20 64 65 73 63 72 69 62 65  integer describe
21465 20 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e   by.** text z[0.
21466 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73  .n-1] into regis
21467 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20  ter iMem..**.** 
21468 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77  The z[] string w
21469 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74  ill probably not
2146a 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61   be zero-termina
2146b 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a  ted.  But the .*
2146c 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72  * z[n] character
2146d 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
2146e 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74  o be something t
2146f 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f  hat does not loo
21470 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f  k.** like the co
21471 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68  ntinuation of th
21472 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
21473 74 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74  tic void codeInt
21474 65 67 65 72 28 56 64 62 65 20 2a 76 2c 20 45 78  eger(Vdbe *v, Ex
21475 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6e  pr *pExpr, int n
21476 65 67 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d  egFlag, int iMem
21477 29 7b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  ){.  if( pExpr->
21478 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 61  flags & EP_IntVa
21479 6c 75 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  lue ){.    int i
2147a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c   = pExpr->u.iVal
2147b 75 65 3b 0a 20 20 20 20 69 66 28 20 6e 65 67 46  ue;.    if( negF
2147c 6c 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20  lag ) i = -i;.  
2147d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2147e 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2147f 72 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d  r, i, iMem);.  }
21480 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20  else{.    const 
21481 63 68 61 72 20 2a 7a 20 3d 20 70 45 78 70 72 2d  char *z = pExpr-
21482 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 61  >u.zToken;.    a
21483 73 73 65 72 74 28 20 7a 21 3d 30 20 29 3b 0a 20  ssert( z!=0 );. 
21484 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
21485 74 73 49 6e 36 34 42 69 74 73 28 7a 2c 20 6e 65  tsIn64Bits(z, ne
21486 67 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 20 20  gFlag) ){.      
21487 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20  i64 value;.     
21488 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 20   char *zV;.     
21489 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a   sqlite3Atoi64(z
2148a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20  , &value);.     
2148b 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 76   if( negFlag ) v
2148c 61 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20  alue = -value;. 
2148d 20 20 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79       zV = dup8by
2148e 74 65 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76  tes(v, (char*)&v
2148f 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  alue);.      sql
21490 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
21491 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c 20 69  , OP_Int64, 0, i
21492 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34 5f 49  Mem, 0, zV, P4_I
21493 4e 54 36 34 29 3b 0a 20 20 20 20 7d 65 6c 73 65  NT64);.    }else
21494 7b 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c  {.      codeReal
21495 28 76 2c 20 7a 2c 20 6e 65 67 46 6c 61 67 2c 20  (v, z, negFlag, 
21496 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  iMem);.    }.  }
21497 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
21498 61 20 63 61 63 68 65 20 65 6e 74 72 79 2e 0a 2a  a cache entry..*
21499 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 61  /.static void ca
2149a 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28 50 61  cheEntryClear(Pa
2149b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 73 74 72  rse *pParse, str
2149c 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70  uct yColCache *p
2149d 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 65 6d 70  ){.  if( p->temp
2149e 52 65 67 20 29 7b 0a 20 20 20 20 69 66 28 20 70  Reg ){.    if( p
2149f 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c  Parse->nTempReg<
214a0 41 72 72 61 79 53 69 7a 65 28 70 50 61 72 73 65  ArraySize(pParse
214a1 2d 3e 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20  ->aTempReg) ){. 
214a2 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54 65       pParse->aTe
214a3 6d 70 52 65 67 5b 70 50 61 72 73 65 2d 3e 6e 54  mpReg[pParse->nT
214a4 65 6d 70 52 65 67 2b 2b 5d 20 3d 20 70 2d 3e 69  empReg++] = p->i
214a5 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  Reg;.    }.    p
214a6 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20  ->tempReg = 0;. 
214a7 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 63   }.}.../*.** Rec
214a8 6f 72 64 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d  ord in the colum
214a9 6e 20 63 61 63 68 65 20 74 68 61 74 20 61 20 70  n cache that a p
214aa 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e  articular column
214ab 20 66 72 6f 6d 20 61 0a 2a 2a 20 70 61 72 74 69   from a.** parti
214ac 63 75 6c 61 72 20 74 61 62 6c 65 20 69 73 20 73  cular table is s
214ad 74 6f 72 65 64 20 69 6e 20 61 20 70 61 72 74 69  tored in a parti
214ae 63 75 6c 61 72 20 72 65 67 69 73 74 65 72 2e 0a  cular register..
214af 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
214b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
214b1 70 72 43 61 63 68 65 53 74 6f 72 65 28 50 61 72  prCacheStore(Par
214b2 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
214b3 69 54 61 62 2c 20 69 6e 74 20 69 43 6f 6c 2c 20  iTab, int iCol, 
214b4 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74  int iReg){.  int
214b5 20 69 3b 0a 20 20 69 6e 74 20 6d 69 6e 4c 72 75   i;.  int minLru
214b6 3b 0a 20 20 69 6e 74 20 69 64 78 4c 72 75 3b 0a  ;.  int idxLru;.
214b7 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63    struct yColCac
214b8 68 65 20 2a 70 3b 0a 0a 20 20 61 73 73 65 72 74  he *p;..  assert
214b9 28 20 69 52 65 67 3e 30 20 29 3b 20 20 2f 2a 20  ( iReg>0 );  /* 
214ba 52 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73  Register numbers
214bb 20 61 72 65 20 61 6c 77 61 79 73 20 70 6f 73 69   are always posi
214bc 74 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74  tive */.  assert
214bd 28 20 69 43 6f 6c 3e 3d 2d 31 20 26 26 20 69 43  ( iCol>=-1 && iC
214be 6f 6c 3c 33 32 37 36 38 20 29 3b 20 20 2f 2a 20  ol<32768 );  /* 
214bf 46 69 6e 69 74 65 20 63 6f 6c 75 6d 6e 20 6e 75  Finite column nu
214c0 6d 62 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a 20 46  mbers */..  /* F
214c1 69 72 73 74 20 72 65 70 6c 61 63 65 20 61 6e 79  irst replace any
214c2 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 20   existing entry 
214c3 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d  */.  for(i=0, p=
214c4 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
214c5 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f  e; i<SQLITE_N_CO
214c6 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b  LCACHE; i++, p++
214c7 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52  ){.    if( p->iR
214c8 65 67 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d  eg && p->iTable=
214c9 3d 69 54 61 62 20 26 26 20 70 2d 3e 69 43 6f 6c  =iTab && p->iCol
214ca 75 6d 6e 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20  umn==iCol ){.   
214cb 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65     cacheEntryCle
214cc 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20  ar(pParse, p);. 
214cd 20 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20 3d       p->iLevel =
214ce 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c   pParse->iCacheL
214cf 65 76 65 6c 3b 0a 20 20 20 20 20 20 70 2d 3e 69  evel;.      p->i
214d0 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20  Reg = iReg;.    
214d1 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d    p->affChange =
214d2 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75   0;.      p->lru
214d3 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68   = pParse->iCach
214d4 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65  eCnt++;.      re
214d5 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
214d6 0a 20 20 2f 2a 20 46 69 6e 64 20 61 6e 20 65 6d  .  /* Find an em
214d7 70 74 79 20 73 6c 6f 74 20 61 6e 64 20 72 65 70  pty slot and rep
214d8 6c 61 63 65 20 69 74 20 2a 2f 0a 20 20 66 6f 72  lace it */.  for
214d9 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
214da 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
214db 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
214dc 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
214dd 66 28 20 70 2d 3e 69 52 65 67 3d 3d 30 20 29 7b  f( p->iReg==0 ){
214de 0a 20 20 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c  .      p->iLevel
214df 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68   = pParse->iCach
214e0 65 4c 65 76 65 6c 3b 0a 20 20 20 20 20 20 70 2d  eLevel;.      p-
214e1 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a  >iTable = iTab;.
214e2 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e        p->iColumn
214e3 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 70   = iCol;.      p
214e4 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a 20  ->iReg = iReg;. 
214e5 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67       p->affChang
214e6 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  e = 0;.      p->
214e7 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 20  tempReg = 0;.   
214e8 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72     p->lru = pPar
214e9 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b  se->iCacheCnt++;
214ea 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
214eb 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52     }.  }..  /* R
214ec 65 70 6c 61 63 65 20 74 68 65 20 6c 61 73 74 20  eplace the last 
214ed 72 65 63 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f  recently used */
214ee 0a 20 20 6d 69 6e 4c 72 75 20 3d 20 30 78 37 66  .  minLru = 0x7f
214ef 66 66 66 66 66 66 3b 0a 20 20 69 64 78 4c 72 75  ffffff;.  idxLru
214f0 20 3d 20 2d 31 3b 0a 20 20 66 6f 72 28 69 3d 30   = -1;.  for(i=0
214f1 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c  , p=pParse->aCol
214f2 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f  Cache; i<SQLITE_
214f3 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c  N_COLCACHE; i++,
214f4 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70   p++){.    if( p
214f5 2d 3e 6c 72 75 3c 6d 69 6e 4c 72 75 20 29 7b 0a  ->lru<minLru ){.
214f6 20 20 20 20 20 20 69 64 78 4c 72 75 20 3d 20 69        idxLru = i
214f7 3b 0a 20 20 20 20 20 20 6d 69 6e 4c 72 75 20 3d  ;.      minLru =
214f8 20 70 2d 3e 6c 72 75 3b 0a 20 20 20 20 7d 0a 20   p->lru;.    }. 
214f9 20 7d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28   }.  if( ALWAYS(
214fa 69 64 78 4c 72 75 3e 3d 30 29 20 29 7b 0a 20 20  idxLru>=0) ){.  
214fb 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61    p = &pParse->a
214fc 43 6f 6c 43 61 63 68 65 5b 69 64 78 4c 72 75 5d  ColCache[idxLru]
214fd 3b 0a 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20  ;.    p->iLevel 
214fe 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65  = pParse->iCache
214ff 4c 65 76 65 6c 3b 0a 20 20 20 20 70 2d 3e 69 54  Level;.    p->iT
21500 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 20  able = iTab;.   
21501 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43   p->iColumn = iC
21502 6f 6c 3b 0a 20 20 20 20 70 2d 3e 69 52 65 67 20  ol;.    p->iReg 
21503 3d 20 69 52 65 67 3b 0a 20 20 20 20 70 2d 3e 61  = iReg;.    p->a
21504 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20  ffChange = 0;.  
21505 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30    p->tempReg = 0
21506 3b 0a 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70  ;.    p->lru = p
21507 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74  Parse->iCacheCnt
21508 2b 2b 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ++;.    return;.
21509 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64    }.}../*.** Ind
2150a 69 63 61 74 65 20 74 68 61 74 20 61 20 72 65 67  icate that a reg
2150b 69 73 74 65 72 20 69 73 20 62 65 69 6e 67 20 6f  ister is being o
2150c 76 65 72 77 72 69 74 74 65 6e 2e 20 20 50 75 72  verwritten.  Pur
2150d 67 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a  ge the register.
2150e 2a 2a 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75  ** from the colu
2150f 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c  mn cache..*/.SQL
21510 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21511 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
21512 65 52 65 6d 6f 76 65 28 50 61 72 73 65 20 2a 70  eRemove(Parse *p
21513 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 29  Parse, int iReg)
21514 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
21515 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70  uct yColCache *p
21516 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70  ;.  for(i=0, p=p
21517 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
21518 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c  ; i<SQLITE_N_COL
21519 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29  CACHE; i++, p++)
2151a 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65  {.    if( p->iRe
2151b 67 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20 20 20  g==iReg ){.     
2151c 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72   cacheEntryClear
2151d 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20  (pParse, p);.   
2151e 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a     p->iReg = 0;.
2151f 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
21520 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20  ** Remember the 
21521 63 75 72 72 65 6e 74 20 63 6f 6c 75 6d 6e 20 63  current column c
21522 61 63 68 65 20 63 6f 6e 74 65 78 74 2e 20 20 41  ache context.  A
21523 6e 79 20 6e 65 77 20 65 6e 74 72 69 65 73 20 61  ny new entries a
21524 64 64 65 64 0a 2a 2a 20 61 64 64 65 64 20 74 6f  dded.** added to
21525 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68   the column cach
21526 65 20 61 66 74 65 72 20 74 68 69 73 20 63 61 6c  e after this cal
21527 6c 20 61 72 65 20 72 65 6d 6f 76 65 64 20 77 68  l are removed wh
21528 65 6e 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73  en the.** corres
21529 70 6f 6e 64 69 6e 67 20 70 6f 70 20 6f 63 63 75  ponding pop occu
2152a 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rs..*/.SQLITE_PR
2152b 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2152c 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28  e3ExprCachePush(
2152d 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
2152e 20 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65    pParse->iCache
2152f 4c 65 76 65 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a  Level++;.}../*.*
21530 2a 20 52 65 6d 6f 76 65 20 66 72 6f 6d 20 74 68  * Remove from th
21531 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 61  e column cache a
21532 6e 79 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  ny entries that 
21533 77 65 72 65 20 61 64 64 65 64 20 73 69 6e 63 65  were added since
21534 20 74 68 65 0a 2a 2a 20 74 68 65 20 70 72 65 76   the.** the prev
21535 69 6f 75 73 20 4e 20 50 75 73 68 20 6f 70 65 72  ious N Push oper
21536 61 74 69 6f 6e 73 2e 20 20 49 6e 20 6f 74 68 65  ations.  In othe
21537 72 20 77 6f 72 64 73 2c 20 72 65 73 74 6f 72 65  r words, restore
21538 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 74 6f   the cache.** to
21539 20 74 68 65 20 73 74 61 74 65 20 69 74 20 77 61   the state it wa
2153a 73 20 69 6e 20 4e 20 50 75 73 68 65 73 20 61 67  s in N Pushes ag
2153b 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  o..*/.SQLITE_PRI
2153c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2153d 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 50 61  3ExprCachePop(Pa
2153e 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
2153f 20 4e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20   N){.  int i;.  
21540 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
21541 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e   *p;.  assert( N
21542 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
21543 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65  pParse->iCacheLe
21544 76 65 6c 3e 3d 4e 20 29 3b 0a 20 20 70 50 61 72  vel>=N );.  pPar
21545 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20  se->iCacheLevel 
21546 2d 3d 20 4e 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  -= N;.  for(i=0,
21547 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   p=pParse->aColC
21548 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e  ache; i<SQLITE_N
21549 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20  _COLCACHE; i++, 
2154a 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  p++){.    if( p-
2154b 3e 69 52 65 67 20 26 26 20 70 2d 3e 69 4c 65 76  >iReg && p->iLev
2154c 65 6c 3e 70 50 61 72 73 65 2d 3e 69 43 61 63 68  el>pParse->iCach
2154d 65 4c 65 76 65 6c 20 29 7b 0a 20 20 20 20 20 20  eLevel ){.      
2154e 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28  cacheEntryClear(
2154f 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20  pParse, p);.    
21550 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20    p->iReg = 0;. 
21551 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
21552 2a 20 57 68 65 6e 20 61 20 63 61 63 68 65 64 20  * When a cached 
21553 63 6f 6c 75 6d 6e 20 69 73 20 72 65 75 73 65 64  column is reused
21554 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  , make sure that
21555 20 69 74 73 20 72 65 67 69 73 74 65 72 20 69 73   its register is
21556 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 76  .** no longer av
21557 61 69 6c 61 62 6c 65 20 61 73 20 61 20 74 65 6d  ailable as a tem
21558 70 20 72 65 67 69 73 74 65 72 2e 20 20 74 69 63  p register.  tic
21559 6b 65 74 20 23 33 38 37 39 3a 20 20 74 68 61 74  ket #3879:  that
2155a 20 73 61 6d 65 0a 2a 2a 20 72 65 67 69 73 74 65   same.** registe
2155b 72 20 6d 69 67 68 74 20 62 65 20 69 6e 20 74 68  r might be in th
2155c 65 20 63 61 63 68 65 20 69 6e 20 6d 75 6c 74 69  e cache in multi
2155d 70 6c 65 20 70 6c 61 63 65 73 2c 20 73 6f 20 62  ple places, so b
2155e 65 20 73 75 72 65 20 74 6f 0a 2a 2a 20 67 65 74  e sure to.** get
2155f 20 74 68 65 6d 20 61 6c 6c 2e 0a 2a 2f 0a 73 74   them all..*/.st
21560 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
21561 33 45 78 70 72 43 61 63 68 65 50 69 6e 52 65 67  3ExprCachePinReg
21562 69 73 74 65 72 28 50 61 72 73 65 20 2a 70 50 61  ister(Parse *pPa
21563 72 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a  rse, int iReg){.
21564 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
21565 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a  t yColCache *p;.
21566 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61    for(i=0, p=pPa
21567 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20  rse->aColCache; 
21568 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  i<SQLITE_N_COLCA
21569 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a  CHE; i++, p++){.
2156a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d      if( p->iReg=
2156b 3d 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 70  =iReg ){.      p
2156c 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20  ->tempReg = 0;. 
2156d 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
2156e 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2156f 74 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61 63  that will extrac
21570 74 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68  t the iColumn-th
21571 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 0a 2a 2a 20   column from.** 
21572 74 61 62 6c 65 20 70 54 61 62 20 61 6e 64 20 73  table pTab and s
21573 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20  tore the column 
21574 76 61 6c 75 65 20 69 6e 20 61 20 72 65 67 69 73  value in a regis
21575 74 65 72 2e 20 20 41 6e 20 65 66 66 6f 72 74 0a  ter.  An effort.
21576 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 73 74  ** is made to st
21577 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76  ore the column v
21578 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
21579 20 69 52 65 67 2c 20 62 75 74 20 74 68 69 73 20   iReg, but this 
2157a 69 73 0a 2a 2a 20 6e 6f 74 20 67 75 61 72 61 6e  is.** not guaran
2157b 74 65 65 64 2e 20 20 54 68 65 20 6c 6f 63 61 74  teed.  The locat
2157c 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ion of the colum
2157d 6e 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72  n value is retur
2157e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ned..**.** There
2157f 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e   must be an open
21580 20 63 75 72 73 6f 72 20 74 6f 20 70 54 61 62 20   cursor to pTab 
21581 69 6e 20 69 54 61 62 6c 65 20 77 68 65 6e 20 74  in iTable when t
21582 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69  his routine.** i
21583 73 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 43  s called.  If iC
21584 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e 20 63 6f 64  olumn<0 then cod
21585 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 74  e is generated t
21586 68 61 74 20 65 78 74 72 61 63 74 73 20 74 68 65  hat extracts the
21587 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68   rowid..**.** Th
21588 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74  is routine might
21589 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 75 73   attempt to reus
2158a 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
2158b 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a  he column that.*
2158c 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  * has already be
2158d 65 6e 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 61  en loaded into a
2158e 20 72 65 67 69 73 74 65 72 2e 20 20 54 68 65 20   register.  The 
2158f 76 61 6c 75 65 20 77 69 6c 6c 20 61 6c 77 61 79  value will alway
21590 73 0a 2a 2a 20 62 65 20 75 73 65 64 20 69 66 20  s.** be used if 
21591 69 74 20 68 61 73 20 6e 6f 74 20 75 6e 64 65 72  it has not under
21592 67 6f 6e 65 20 61 6e 79 20 61 66 66 69 6e 69 74  gone any affinit
21593 79 20 63 68 61 6e 67 65 73 2e 20 20 42 75 74 20  y changes.  But 
21594 69 66 0a 2a 2a 20 61 6e 20 61 66 66 69 6e 69 74  if.** an affinit
21595 79 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63  y change has occ
21596 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20  urred, then the 
21597 63 61 63 68 65 64 20 76 61 6c 75 65 20 77 69 6c  cached value wil
21598 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 75 73 65  l only be.** use
21599 64 20 69 66 20 61 6c 6c 6f 77 41 66 66 43 68 6e  d if allowAffChn
2159a 67 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 53 51  g is true..*/.SQ
2159b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2159c 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2159d 47 65 74 43 6f 6c 75 6d 6e 28 0a 20 20 50 61 72  GetColumn(.  Par
2159e 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a  se *pParse,   /*
2159f 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64   Parsing and cod
215a0 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
215a1 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
215a2 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 44 65  *pTab,     /* De
215a3 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65  scription of the
215a4 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 72 65   table we are re
215a5 61 64 69 6e 67 20 66 72 6f 6d 20 2a 2f 0a 20 20  ading from */.  
215a6 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20  int iColumn,    
215a7 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
215a8 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f   table column */
215a9 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20  .  int iTable,  
215aa 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f      /* The curso
215ab 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  r pointing to th
215ac 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
215ad 20 69 52 65 67 2c 20 20 20 20 20 20 20 20 2f 2a   iReg,        /*
215ae 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 68   Store results h
215af 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 6c  ere */.  int all
215b0 6f 77 41 66 66 43 68 6e 67 20 2f 2a 20 54 72 75  owAffChng /* Tru
215b1 65 20 69 66 20 70 72 69 6f 72 20 61 66 66 69 6e  e if prior affin
215b2 69 74 79 20 63 68 61 6e 67 65 73 20 61 72 65 20  ity changes are 
215b3 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20  OK */.){.  Vdbe 
215b4 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
215b5 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  be;.  int i;.  s
215b6 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20  truct yColCache 
215b7 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20  *p;..  for(i=0, 
215b8 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
215b9 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
215ba 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
215bb 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ++){.    if( p->
215bc 69 52 65 67 3e 30 20 26 26 20 70 2d 3e 69 54 61  iReg>0 && p->iTa
215bd 62 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70  ble==iTable && p
215be 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75  ->iColumn==iColu
215bf 6d 6e 0a 20 20 20 20 20 20 20 20 20 20 20 26 26  mn.           &&
215c0 20 28 21 70 2d 3e 61 66 66 43 68 61 6e 67 65 20   (!p->affChange 
215c1 7c 7c 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 29  || allowAffChng)
215c2 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75   ){.      p->lru
215c3 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68   = pParse->iCach
215c4 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 73 71  eCnt++;.      sq
215c5 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 69  lite3ExprCachePi
215c6 6e 52 65 67 69 73 74 65 72 28 70 50 61 72 73 65  nRegister(pParse
215c7 2c 20 70 2d 3e 69 52 65 67 29 3b 0a 20 20 20 20  , p->iReg);.    
215c8 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 52 65 67    return p->iReg
215c9 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 20 0a 20 20  ;.    }.  }  .  
215ca 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a  assert( v!=0 );.
215cb 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20    if( iColumn<0 
215cc 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
215cd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
215ce 6f 77 69 64 2c 20 69 54 61 62 6c 65 2c 20 69 52  owid, iTable, iR
215cf 65 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  eg);.  }else if(
215d0 20 41 4c 57 41 59 53 28 70 54 61 62 21 3d 30 29   ALWAYS(pTab!=0)
215d1 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20 3d   ){.    int op =
215d2 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
215d3 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 3a 20   ? OP_VColumn : 
215d4 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73  OP_Column;.    s
215d5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
215d6 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c 20  (v, op, iTable, 
215d7 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a  iColumn, iReg);.
215d8 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d      sqlite3Colum
215d9 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62  nDefault(v, pTab
215da 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29  , iColumn, iReg)
215db 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
215dc 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 70 50  xprCacheStore(pP
215dd 61 72 73 65 2c 20 69 54 61 62 6c 65 2c 20 69 43  arse, iTable, iC
215de 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20  olumn, iReg);.  
215df 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a  return iReg;.}..
215e0 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6c 6c 20  /*.** Clear all 
215e1 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e 74  column cache ent
215e2 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ries..*/.SQLITE_
215e3 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
215e4 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65  ite3ExprCacheCle
215e5 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ar(Parse *pParse
215e6 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  ){.  int i;.  st
215e7 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a  ruct yColCache *
215e8 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  p;..  for(i=0, p
215e9 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63  =pParse->aColCac
215ea 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43  he; i<SQLITE_N_C
215eb 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b  OLCACHE; i++, p+
215ec 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69  +){.    if( p->i
215ed 52 65 67 20 29 7b 0a 20 20 20 20 20 20 63 61 63  Reg ){.      cac
215ee 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61  heEntryClear(pPa
215ef 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70  rse, p);.      p
215f0 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 20  ->iReg = 0;.    
215f1 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  }.  }.}../*.** R
215f2 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74  ecord the fact t
215f3 68 61 74 20 61 6e 20 61 66 66 69 6e 69 74 79 20  hat an affinity 
215f4 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72  change has occur
215f5 72 65 64 20 6f 6e 20 69 43 6f 75 6e 74 0a 2a 2a  red on iCount.**
215f6 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74   registers start
215f7 69 6e 67 20 77 69 74 68 20 69 53 74 61 72 74 2e  ing with iStart.
215f8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
215f9 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
215fa 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
215fb 43 68 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50  Change(Parse *pP
215fc 61 72 73 65 2c 20 69 6e 74 20 69 53 74 61 72 74  arse, int iStart
215fd 2c 20 69 6e 74 20 69 43 6f 75 6e 74 29 7b 0a 20  , int iCount){. 
215fe 20 69 6e 74 20 69 45 6e 64 20 3d 20 69 53 74 61   int iEnd = iSta
215ff 72 74 20 2b 20 69 43 6f 75 6e 74 20 2d 20 31 3b  rt + iCount - 1;
21600 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
21601 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b  ct yColCache *p;
21602 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50  .  for(i=0, p=pP
21603 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b  arse->aColCache;
21604 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43   i<SQLITE_N_COLC
21605 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b  ACHE; i++, p++){
21606 0a 20 20 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e  .    int r = p->
21607 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e  iReg;.    if( r>
21608 3d 69 53 74 61 72 74 20 26 26 20 72 3c 3d 69 45  =iStart && r<=iE
21609 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61  nd ){.      p->a
2160a 66 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20 20  ffChange = 1;.  
2160b 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2160c 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2160d 6f 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 66  o move content f
2160e 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46  rom registers iF
2160f 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67  rom...iFrom+nReg
21610 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54  -1.** over to iT
21611 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 4b  o..iTo+nReg-1. K
21612 65 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63  eep the column c
21613 61 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 2e  ache up-to-date.
21614 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
21615 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
21616 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73  xprCodeMove(Pars
21617 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
21618 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69  From, int iTo, i
21619 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20  nt nReg){.  int 
2161a 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  i;.  struct yCol
2161b 43 61 63 68 65 20 2a 70 3b 0a 20 20 69 66 28 20  Cache *p;.  if( 
2161c 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69 54 6f  NEVER(iFrom==iTo
2161d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71  ) ) return;.  sq
2161e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2161f 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f  pParse->pVdbe, O
21620 50 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20 69  P_Move, iFrom, i
21621 54 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 66 6f 72  To, nReg);.  for
21622 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
21623 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
21624 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
21625 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
21626 6e 74 20 78 20 3d 20 70 2d 3e 69 52 65 67 3b 0a  nt x = p->iReg;.
21627 20 20 20 20 69 66 28 20 78 3e 3d 69 46 72 6f 6d      if( x>=iFrom
21628 20 26 26 20 78 3c 69 46 72 6f 6d 2b 6e 52 65 67   && x<iFrom+nReg
21629 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65   ){.      p->iRe
2162a 67 20 2b 3d 20 69 54 6f 2d 69 46 72 6f 6d 3b 0a  g += iTo-iFrom;.
2162b 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
2162c 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
2162d 20 74 6f 20 63 6f 70 79 20 63 6f 6e 74 65 6e 74   to copy content
2162e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20   from registers 
2162f 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52  iFrom...iFrom+nR
21630 65 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20  eg-1.** over to 
21631 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e  iTo..iTo+nReg-1.
21632 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
21633 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
21634 78 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73  xprCodeCopy(Pars
21635 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
21636 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69  From, int iTo, i
21637 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20  nt nReg){.  int 
21638 69 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69  i;.  if( NEVER(i
21639 46 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74  From==iTo) ) ret
2163a 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  urn;.  for(i=0; 
2163b 69 3c 6e 52 65 67 3b 20 69 2b 2b 29 7b 0a 20 20  i<nReg; i++){.  
2163c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2163d 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62  Op2(pParse->pVdb
2163e 65 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 46 72 6f  e, OP_Copy, iFro
2163f 6d 2b 69 2c 20 69 54 6f 2b 69 29 3b 0a 20 20 7d  m+i, iTo+i);.  }
21640 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
21641 20 74 72 75 65 20 69 66 20 61 6e 79 20 72 65 67   true if any reg
21642 69 73 74 65 72 20 69 6e 20 74 68 65 20 72 61 6e  ister in the ran
21643 67 65 20 69 46 72 6f 6d 2e 2e 69 54 6f 20 28 69  ge iFrom..iTo (i
21644 6e 63 6c 75 73 69 76 65 29 0a 2a 2a 20 69 73 20  nclusive).** is 
21645 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
21646 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  the column cache
21647 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
21648 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68  usedAsColumnCach
21649 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
2164a 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20   int iFrom, int 
2164b 69 54 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  iTo){.  int i;. 
2164c 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68   struct yColCach
2164d 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  e *p;.  for(i=0,
2164e 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   p=pParse->aColC
2164f 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e  ache; i<SQLITE_N
21650 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20  _COLCACHE; i++, 
21651 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 20  p++){.    int r 
21652 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69  = p->iReg;.    i
21653 66 28 20 72 3e 3d 69 46 72 6f 6d 20 26 26 20 72  f( r>=iFrom && r
21654 3c 3d 69 54 6f 20 29 20 72 65 74 75 72 6e 20 31  <=iTo ) return 1
21655 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
21656 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
21657 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69  e last instructi
21658 6f 6e 20 63 6f 64 65 64 20 69 73 20 61 6e 20 65  on coded is an e
21659 70 68 65 6d 65 72 61 6c 20 63 6f 70 79 20 6f 66  phemeral copy of
2165a 20 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 20 72   any of.** the r
2165b 65 67 69 73 74 65 72 73 20 69 6e 20 74 68 65 20  egisters in the 
2165c 6e 52 65 67 20 72 65 67 69 73 74 65 72 73 20 62  nReg registers b
2165d 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 69 52  eginning with iR
2165e 65 67 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76  eg, then.** conv
2165f 65 72 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73  ert the last ins
21660 74 72 75 63 74 69 6f 6e 20 66 72 6f 6d 20 4f 50  truction from OP
21661 5f 53 43 6f 70 79 20 74 6f 20 4f 50 5f 43 6f 70  _SCopy to OP_Cop
21662 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
21663 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
21664 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61  3ExprHardCopy(Pa
21665 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
21666 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 29   iReg, int nReg)
21667 7b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b  {.  VdbeOp *pOp;
21668 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 61  .  Vdbe *v;..  a
21669 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64  ssert( pParse->d
2166a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d  b->mallocFailed=
2166b 3d 30 20 29 3b 0a 20 20 76 20 3d 20 70 50 61 72  =0 );.  v = pPar
2166c 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73  se->pVdbe;.  ass
2166d 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 70  ert( v!=0 );.  p
2166e 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  Op = sqlite3Vdbe
2166f 47 65 74 4f 70 28 76 2c 20 2d 31 29 3b 0a 20 20  GetOp(v, -1);.  
21670 61 73 73 65 72 74 28 20 70 4f 70 21 3d 30 20 29  assert( pOp!=0 )
21671 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63  ;.  if( pOp->opc
21672 6f 64 65 3d 3d 4f 50 5f 53 43 6f 70 79 20 26 26  ode==OP_SCopy &&
21673 20 70 4f 70 2d 3e 70 31 3e 3d 69 52 65 67 20 26   pOp->p1>=iReg &
21674 26 20 70 4f 70 2d 3e 70 31 3c 69 52 65 67 2b 6e  & pOp->p1<iReg+n
21675 52 65 67 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e  Reg ){.    pOp->
21676 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 43 6f 70 79  opcode = OP_Copy
21677 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
21678 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
21679 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20  store the value 
2167a 6f 66 20 74 68 65 20 69 41 6c 69 61 73 2d 74 68  of the iAlias-th
2167b 20 61 6c 69 61 73 20 69 6e 20 72 65 67 69 73 74   alias in regist
2167c 65 72 0a 2a 2a 20 74 61 72 67 65 74 2e 20 20 54  er.** target.  T
2167d 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  he first time th
2167e 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 70 45  is is called, pE
2167f 78 70 72 20 69 73 20 65 76 61 6c 75 61 74 65 64  xpr is evaluated
21680 20 74 6f 20 63 6f 6d 70 75 74 65 0a 2a 2a 20 74   to compute.** t
21681 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
21682 61 6c 69 61 73 2e 20 20 54 68 65 20 76 61 6c 75  alias.  The valu
21683 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61  e is stored in a
21684 6e 20 61 75 78 69 6c 69 61 72 79 20 72 65 67 69  n auxiliary regi
21685 73 74 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ster.** and the 
21686 6e 75 6d 62 65 72 20 6f 66 20 74 68 61 74 20 72  number of that r
21687 65 67 69 73 74 65 72 20 69 73 20 72 65 74 75 72  egister is retur
21688 6e 65 64 2e 20 20 4f 6e 20 73 75 62 73 65 71 75  ned.  On subsequ
21689 65 6e 74 20 63 61 6c 6c 73 2c 0a 2a 2a 20 74 68  ent calls,.** th
2168a 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65  e register numbe
2168b 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 77 69  r is returned wi
2168c 74 68 6f 75 74 20 67 65 6e 65 72 61 74 69 6e 67  thout generating
2168d 20 61 6e 79 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a   any code..**.**
2168e 20 4e 6f 74 65 20 74 68 61 74 20 69 6e 20 6f 72   Note that in or
2168f 64 65 72 20 66 6f 72 20 74 68 69 73 20 74 6f 20  der for this to 
21690 77 6f 72 6b 2c 20 63 6f 64 65 20 6d 75 73 74 20  work, code must 
21691 62 65 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20  be generated in 
21692 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6f 72 64 65  the.** same orde
21693 72 20 74 68 61 74 20 69 74 20 69 73 20 65 78 65  r that it is exe
21694 63 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 69  cuted..**.** Ali
21695 61 73 65 73 20 61 72 65 20 6e 75 6d 62 65 72 65  ases are numbere
21696 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20  d starting with 
21697 31 2e 20 20 53 6f 20 69 41 6c 69 61 73 20 69 73  1.  So iAlias is
21698 20 69 6e 20 74 68 65 20 72 61 6e 67 65 0a 2a 2a   in the range.**
21699 20 6f 66 20 31 20 74 6f 20 70 50 61 72 73 65 2d   of 1 to pParse-
2169a 3e 6e 41 6c 69 61 73 20 69 6e 63 6c 75 73 69 76  >nAlias inclusiv
2169b 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 70 50 61 72 73  e.  .**.** pPars
2169c 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73  e->aAlias[iAlias
2169d 2d 31 5d 20 72 65 63 6f 72 64 73 20 74 68 65 20  -1] records the 
2169e 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20  register number 
2169f 77 68 65 72 65 20 74 68 65 20 76 61 6c 75 65 0a  where the value.
216a0 2a 2a 20 6f 66 20 74 68 65 20 69 41 6c 69 61 73  ** of the iAlias
216a1 2d 74 68 20 61 6c 69 61 73 20 69 73 20 73 74 6f  -th alias is sto
216a2 72 65 64 2e 20 20 49 66 20 7a 65 72 6f 2c 20 74  red.  If zero, t
216a3 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74  hat means that t
216a4 68 65 0a 2a 2a 20 61 6c 69 61 73 20 68 61 73 20  he.** alias has 
216a5 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 6f 6d  not yet been com
216a6 70 75 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  puted..*/.static
216a7 20 69 6e 74 20 63 6f 64 65 41 6c 69 61 73 28 50   int codeAlias(P
216a8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
216a9 74 20 69 41 6c 69 61 73 2c 20 45 78 70 72 20 2a  t iAlias, Expr *
216aa 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65  pExpr, int targe
216ab 74 29 7b 0a 23 69 66 20 30 0a 20 20 73 71 6c 69  t){.#if 0.  sqli
216ac 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
216ad 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 52 65 67  ->db;.  int iReg
216ae 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
216af 6e 41 6c 69 61 73 41 6c 6c 6f 63 3c 70 50 61 72  nAliasAlloc<pPar
216b0 73 65 2d 3e 6e 41 6c 69 61 73 20 29 7b 0a 20 20  se->nAlias ){.  
216b1 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73    pParse->aAlias
216b2 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
216b3 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70 50  locOrFree(db, pP
216b4 61 72 73 65 2d 3e 61 41 6c 69 61 73 2c 0a 20 20  arse->aAlias,.  
216b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
216b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
216b7 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 41  izeof(pParse->aA
216b8 6c 69 61 73 5b 30 5d 29 2a 70 50 61 72 73 65 2d  lias[0])*pParse-
216b9 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 20 20 74  >nAlias );.    t
216ba 65 73 74 63 61 73 65 28 20 64 62 2d 3e 6d 61 6c  estcase( db->mal
216bb 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 70 50 61  locFailed && pPa
216bc 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63  rse->nAliasAlloc
216bd 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62  >0 );.    if( db
216be 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
216bf 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 6d   return 0;.    m
216c0 65 6d 73 65 74 28 26 70 50 61 72 73 65 2d 3e 61  emset(&pParse->a
216c1 41 6c 69 61 73 5b 70 50 61 72 73 65 2d 3e 6e 41  Alias[pParse->nA
216c2 6c 69 61 73 41 6c 6c 6f 63 5d 2c 20 30 2c 0a 20  liasAlloc], 0,. 
216c3 20 20 20 20 20 20 20 20 20 20 28 70 50 61 72 73            (pPars
216c4 65 2d 3e 6e 41 6c 69 61 73 2d 70 50 61 72 73 65  e->nAlias-pParse
216c5 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 29 2a 73  ->nAliasAlloc)*s
216c6 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 41  izeof(pParse->aA
216c7 6c 69 61 73 5b 30 5d 29 29 3b 0a 20 20 20 20 70  lias[0]));.    p
216c8 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c  Parse->nAliasAll
216c9 6f 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 41 6c  oc = pParse->nAl
216ca 69 61 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  ias;.  }.  asser
216cb 74 28 20 69 41 6c 69 61 73 3e 30 20 26 26 20 69  t( iAlias>0 && i
216cc 41 6c 69 61 73 3c 3d 70 50 61 72 73 65 2d 3e 6e  Alias<=pParse->n
216cd 41 6c 69 61 73 20 29 3b 0a 20 20 69 52 65 67 20  Alias );.  iReg 
216ce 3d 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73  = pParse->aAlias
216cf 5b 69 41 6c 69 61 73 2d 31 5d 3b 0a 20 20 69 66  [iAlias-1];.  if
216d0 28 20 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20  ( iReg==0 ){.   
216d1 20 69 66 28 20 70 50 61 72 73 65 2d 3e 69 43 61   if( pParse->iCa
216d2 63 68 65 4c 65 76 65 6c 3e 30 20 29 7b 0a 20 20  cheLevel>0 ){.  
216d3 20 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74      iReg = sqlit
216d4 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
216d5 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
216d6 74 61 72 67 65 74 29 3b 0a 20 20 20 20 7d 65 6c  target);.    }el
216d7 73 65 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d  se{.      iReg =
216d8 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
216d9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
216da 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
216db 45 78 70 72 2c 20 69 52 65 67 29 3b 0a 20 20 20  Expr, iReg);.   
216dc 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61     pParse->aAlia
216dd 73 5b 69 41 6c 69 61 73 2d 31 5d 20 3d 20 69 52  s[iAlias-1] = iR
216de 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  eg;.    }.  }.  
216df 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 23 65 6c  return iReg;.#el
216e0 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  se.  UNUSED_PARA
216e1 4d 45 54 45 52 28 69 41 6c 69 61 73 29 3b 0a 20  METER(iAlias);. 
216e2 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45   return sqlite3E
216e3 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50  xprCodeTarget(pP
216e4 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72  arse, pExpr, tar
216e5 67 65 74 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  get);.#endif.}..
216e6 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
216e7 6f 64 65 20 69 6e 74 6f 20 74 68 65 20 63 75 72  ode into the cur
216e8 72 65 6e 74 20 56 64 62 65 20 74 6f 20 65 76 61  rent Vdbe to eva
216e9 6c 75 61 74 65 20 74 68 65 20 67 69 76 65 6e 0a  luate the given.
216ea 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  ** expression.  
216eb 41 74 74 65 6d 70 74 20 74 6f 20 73 74 6f 72 65  Attempt to store
216ec 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20   the results in 
216ed 72 65 67 69 73 74 65 72 20 22 74 61 72 67 65 74  register "target
216ee 22 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  "..** Return the
216ef 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65 20   register where 
216f0 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72  results are stor
216f1 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74  ed..**.** With t
216f2 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65  his routine, the
216f3 72 65 20 69 73 20 6e 6f 20 67 75 61 72 61 6e 74  re is no guarant
216f4 65 65 20 74 68 61 74 20 72 65 73 75 6c 74 73 20  ee that results 
216f5 77 69 6c 6c 0a 2a 2a 20 62 65 20 73 74 6f 72 65  will.** be store
216f6 64 20 69 6e 20 74 61 72 67 65 74 2e 20 20 54 68  d in target.  Th
216f7 65 20 72 65 73 75 6c 74 20 6d 69 67 68 74 20 62  e result might b
216f8 65 20 73 74 6f 72 65 64 20 69 6e 20 73 6f 6d 65  e stored in some
216f9 20 6f 74 68 65 72 0a 2a 2a 20 72 65 67 69 73 74   other.** regist
216fa 65 72 20 69 66 20 69 74 20 69 73 20 63 6f 6e 76  er if it is conv
216fb 65 6e 69 65 6e 74 20 74 6f 20 64 6f 20 73 6f 2e  enient to do so.
216fc 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
216fd 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 63  nction.** must c
216fe 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20  heck the return 
216ff 63 6f 64 65 20 61 6e 64 20 6d 6f 76 65 20 74 68  code and move th
21700 65 20 72 65 73 75 6c 74 73 20 74 6f 20 74 68 65  e results to the
21701 20 64 65 73 69 72 65 64 0a 2a 2a 20 72 65 67 69   desired.** regi
21702 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ster..*/.SQLITE_
21703 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
21704 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65  te3ExprCodeTarge
21705 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
21706 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e   Expr *pExpr, in
21707 74 20 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62  t target){.  Vdb
21708 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
21709 56 64 62 65 3b 20 20 2f 2a 20 54 68 65 20 56 4d  Vdbe;  /* The VM
2170a 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
2170b 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b  ion */.  int op;
2170c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2170d 20 20 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64 65     /* The opcode
2170e 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a   being coded */.
2170f 20 20 69 6e 74 20 69 6e 52 65 67 20 3d 20 74 61    int inReg = ta
21710 72 67 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 52  rget;       /* R
21711 65 73 75 6c 74 73 20 73 74 6f 72 65 64 20 69 6e  esults stored in
21712 20 72 65 67 69 73 74 65 72 20 69 6e 52 65 67 20   register inReg 
21713 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65  */.  int regFree
21714 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  1 = 0;         /
21715 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72  * If non-zero fr
21716 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72  ee this temporar
21717 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  y register */.  
21718 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 30  int regFree2 = 0
21719 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  ;         /* If 
2171a 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68  non-zero free th
2171b 69 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  is temporary reg
2171c 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72  ister */.  int r
2171d 31 2c 20 72 32 2c 20 72 33 2c 20 72 34 3b 20 20  1, r2, r3, r4;  
2171e 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20       /* Various 
2171f 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73  register numbers
21720 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
21721 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
21722 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
21723 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20  connection */.. 
21724 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e   assert( target>
21725 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61  0 && target<=pPa
21726 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69  rse->nMem );.  i
21727 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 61  f( v==0 ){.    a
21728 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64  ssert( pParse->d
21729 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2172a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
2172b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 45 78 70  .  }..  if( pExp
2172c 72 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 70 20 3d  r==0 ){.    op =
2172d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73   TK_NULL;.  }els
2172e 65 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70  e{.    op = pExp
2172f 72 2d 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 77 69  r->op;.  }.  swi
21730 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
21731 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  ase TK_AGG_COLUM
21732 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e  N: {.      AggIn
21733 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70  fo *pAggInfo = p
21734 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a  Expr->pAggInfo;.
21735 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67        struct Agg
21736 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d  Info_col *pCol =
21737 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c   &pAggInfo->aCol
21738 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 3b 0a 20  [pExpr->iAgg];. 
21739 20 20 20 20 20 69 66 28 20 21 70 41 67 67 49 6e       if( !pAggIn
2173a 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 29  fo->directMode )
2173b 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
2173c 28 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30 20 29  ( pCol->iMem>0 )
2173d 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20  ;.        inReg 
2173e 3d 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a 20 20  = pCol->iMem;.  
2173f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21740 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 67     }else if( pAg
21741 67 49 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 69 6e  gInfo->useSortin
21742 67 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20  gIdx ){.        
21743 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21744 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
21745 70 41 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 69 6e  pAggInfo->sortin
21746 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20  gIdx,.          
21747 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21748 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65      pCol->iSorte
21749 72 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29  rColumn, target)
2174a 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
2174b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
2174c 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c  * Otherwise, fal
2174d 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20  l thru into the 
2174e 54 4b 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a  TK_COLUMN case *
2174f 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  /.    }.    case
21750 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20   TK_COLUMN: {.  
21751 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69      if( pExpr->i
21752 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20  Table<0 ){.     
21753 20 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20     /* This only 
21754 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64  happens when cod
21755 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72  ing check constr
21756 61 69 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20  aints */.       
21757 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
21758 3e 63 6b 42 61 73 65 3e 30 20 29 3b 0a 20 20 20  >ckBase>0 );.   
21759 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78       inReg = pEx
2175a 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 70 50  pr->iColumn + pP
2175b 61 72 73 65 2d 3e 63 6b 42 61 73 65 3b 0a 20 20  arse->ckBase;.  
2175c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2175d 20 20 20 74 65 73 74 63 61 73 65 28 20 28 70 45     testcase( (pE
2175e 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  xpr->flags & EP_
2175f 41 6e 79 41 66 66 29 21 3d 30 20 29 3b 0a 20 20  AnyAff)!=0 );.  
21760 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71        inReg = sq
21761 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
21762 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70  Column(pParse, p
21763 45 78 70 72 2d 3e 70 54 61 62 2c 0a 20 20 20 20  Expr->pTab,.    
21764 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21765 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
21766 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 45 78  pr->iColumn, pEx
21767 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67  pr->iTable, targ
21768 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  et,.            
21769 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2176a 20 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67       pExpr->flag
2176b 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 3b 0a  s & EP_AnyAff);.
2176c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
2176d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
2176e 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20  ase TK_INTEGER: 
2176f 7b 0a 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65  {.      codeInte
21770 67 65 72 28 76 2c 20 70 45 78 70 72 2c 20 30 2c  ger(v, pExpr, 0,
21771 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
21772 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
21773 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 20   case TK_FLOAT: 
21774 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
21775 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
21776 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
21777 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 63 6f  lue) );.      co
21778 64 65 52 65 61 6c 28 76 2c 20 70 45 78 70 72 2d  deReal(v, pExpr-
21779 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 2c 20 74 61  >u.zToken, 0, ta
2177a 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65  rget);.      bre
2177b 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
2177c 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a  se TK_STRING: {.
2177d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
2177e 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
2177f 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75  Expr, EP_IntValu
21780 65 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  e) );.      sqli
21781 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
21782 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
21783 74 61 72 67 65 74 2c 20 30 2c 20 70 45 78 70 72  target, 0, pExpr
21784 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a  ->u.zToken, 0);.
21785 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21786 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e   }.    case TK_N
21787 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  ULL: {.      sql
21788 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21789 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61  , OP_Null, 0, ta
2178a 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65  rget);.      bre
2178b 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  ak;.    }.#ifnde
2178c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c  f SQLITE_OMIT_BL
2178d 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63  OB_LITERAL.    c
2178e 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20  ase TK_BLOB: {. 
2178f 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20       int n;.    
21790 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b    const char *z;
21791 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 42 6c  .      char *zBl
21792 6f 62 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ob;.      assert
21793 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
21794 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74  ty(pExpr, EP_Int
21795 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20  Value) );.      
21796 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75  assert( pExpr->u
21797 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20  .zToken[0]=='x' 
21798 7c 7c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  || pExpr->u.zTok
21799 65 6e 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20  en[0]=='X' );.  
2179a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
2179b 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d  r->u.zToken[1]==
2179c 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 7a 20  '\'' );.      z 
2179d 3d 20 26 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  = &pExpr->u.zTok
2179e 65 6e 5b 32 5d 3b 0a 20 20 20 20 20 20 6e 20 3d  en[2];.      n =
2179f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
217a0 28 7a 29 20 2d 20 31 3b 0a 20 20 20 20 20 20 61  (z) - 1;.      a
217a1 73 73 65 72 74 28 20 7a 5b 6e 5d 3d 3d 27 5c 27  ssert( z[n]=='\'
217a2 27 20 29 3b 0a 20 20 20 20 20 20 7a 42 6c 6f 62  ' );.      zBlob
217a3 20 3d 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42   = sqlite3HexToB
217a4 6c 6f 62 28 73 71 6c 69 74 65 33 56 64 62 65 44  lob(sqlite3VdbeD
217a5 62 28 76 29 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20  b(v), z, n);.   
217a6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
217a7 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c  dOp4(v, OP_Blob,
217a8 20 6e 2f 32 2c 20 74 61 72 67 65 74 2c 20 30 2c   n/2, target, 0,
217a9 20 7a 42 6c 6f 62 2c 20 50 34 5f 44 59 4e 41 4d   zBlob, P4_DYNAM
217aa 49 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  IC);.      break
217ab 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
217ac 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41     case TK_VARIA
217ad 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 56 64 62  BLE: {.      Vdb
217ae 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20  eOp *pOp;.      
217af 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
217b0 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
217b1 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a  EP_IntValue) );.
217b2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
217b3 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 21 3d 30  xpr->u.zToken!=0
217b4 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
217b5 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  ( pExpr->u.zToke
217b6 6e 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 20 20 20  n[0]!=0 );.     
217b7 20 69 66 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54   if( pExpr->u.zT
217b8 6f 6b 65 6e 5b 31 5d 3d 3d 30 0a 20 20 20 20 20  oken[1]==0.     
217b9 20 20 20 20 26 26 20 28 70 4f 70 20 3d 20 73 71      && (pOp = sq
217ba 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
217bb 2c 20 2d 31 29 29 2d 3e 6f 70 63 6f 64 65 3d 3d  , -1))->opcode==
217bc 4f 50 5f 56 61 72 69 61 62 6c 65 0a 20 20 20 20  OP_Variable.    
217bd 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 31 2b       && pOp->p1+
217be 70 4f 70 2d 3e 70 33 3d 3d 70 45 78 70 72 2d 3e  pOp->p3==pExpr->
217bf 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20  iColumn.        
217c0 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d   && pOp->p2+pOp-
217c1 3e 70 33 3d 3d 74 61 72 67 65 74 0a 20 20 20 20  >p3==target.    
217c2 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 34 2e       && pOp->p4.
217c3 7a 3d 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 20  z==0.      ){.  
217c4 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
217c5 70 72 65 76 69 6f 75 73 20 69 6e 73 74 72 75 63  previous instruc
217c6 74 69 6f 6e 20 77 61 73 20 61 20 63 6f 70 79 20  tion was a copy 
217c7 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  of the previous 
217c8 75 6e 6e 61 6d 65 64 0a 20 20 20 20 20 20 20 20  unnamed.        
217c9 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e 74  ** parameter int
217ca 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 72  o the previous r
217cb 65 67 69 73 74 65 72 2c 20 74 68 65 6e 20 73 69  egister, then si
217cc 6d 70 6c 79 20 69 6e 63 72 65 6d 65 6e 74 20 74  mply increment t
217cd 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65  he.        ** re
217ce 70 65 61 74 20 63 6f 75 6e 74 20 6f 6e 20 74 68  peat count on th
217cf 65 20 70 72 69 6f 72 20 69 6e 73 74 72 75 63 74  e prior instruct
217d0 69 6f 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20  ion rather than 
217d1 6d 61 6b 69 6e 67 20 61 20 6e 65 77 0a 20 20 20  making a new.   
217d2 20 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 63 74       ** instruct
217d3 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ion..        */.
217d4 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 33 2b          pOp->p3+
217d5 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  +;.      }else{.
217d6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
217d7 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
217d8 56 61 72 69 61 62 6c 65 2c 20 70 45 78 70 72 2d  Variable, pExpr-
217d9 3e 69 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74  >iColumn, target
217da 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 1);.        if
217db 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  ( pExpr->u.zToke
217dc 6e 5b 31 5d 21 3d 30 20 29 7b 0a 20 20 20 20 20  n[1]!=0 ){.     
217dd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
217de 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
217df 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c  pExpr->u.zToken,
217e0 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   0);.        }. 
217e1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
217e2 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
217e3 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52 3a 20  se TK_REGISTER: 
217e4 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20  {.      inReg = 
217e5 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20  pExpr->iTable;. 
217e6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
217e7 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 53  }.    case TK_AS
217e8 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20  : {.      inReg 
217e9 3d 20 63 6f 64 65 41 6c 69 61 73 28 70 50 61 72  = codeAlias(pPar
217ea 73 65 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  se, pExpr->iTabl
217eb 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
217ec 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
217ed 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66  break;.    }.#if
217ee 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
217ef 5f 43 41 53 54 0a 20 20 20 20 63 61 73 65 20 54  _CAST.    case T
217f0 4b 5f 43 41 53 54 3a 20 7b 0a 20 20 20 20 20 20  K_CAST: {.      
217f1 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f  /* Expressions o
217f2 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 20 43 41  f the form:   CA
217f3 53 54 28 70 4c 65 66 74 20 41 53 20 74 6f 6b 65  ST(pLeft AS toke
217f4 6e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  n) */.      int 
217f5 61 66 66 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20  aff, to_op;.    
217f6 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65    inReg = sqlite
217f7 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
217f8 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
217f9 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20  Left, target);. 
217fa 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
217fb 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
217fc 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
217fd 29 20 29 3b 0a 20 20 20 20 20 20 61 66 66 20 3d  ) );.      aff =
217fe 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79   sqlite3Affinity
217ff 54 79 70 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54  Type(pExpr->u.zT
21800 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 74 6f 5f  oken);.      to_
21801 6f 70 20 3d 20 61 66 66 20 2d 20 53 51 4c 49 54  op = aff - SQLIT
21802 45 5f 41 46 46 5f 54 45 58 54 20 2b 20 4f 50 5f  E_AFF_TEXT + OP_
21803 54 6f 54 65 78 74 3b 0a 20 20 20 20 20 20 61 73  ToText;.      as
21804 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  sert( to_op==OP_
21805 54 6f 54 65 78 74 20 20 20 20 7c 7c 20 61 66 66  ToText    || aff
21806 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  !=SQLITE_AFF_TEX
21807 54 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73  T    );.      as
21808 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  sert( to_op==OP_
21809 54 6f 42 6c 6f 62 20 20 20 20 7c 7c 20 61 66 66  ToBlob    || aff
2180a 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  !=SQLITE_AFF_NON
2180b 45 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73  E    );.      as
2180c 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  sert( to_op==OP_
2180d 54 6f 4e 75 6d 65 72 69 63 20 7c 7c 20 61 66 66  ToNumeric || aff
2180e 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  !=SQLITE_AFF_NUM
2180f 45 52 49 43 20 29 3b 0a 20 20 20 20 20 20 61 73  ERIC );.      as
21810 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  sert( to_op==OP_
21811 54 6f 49 6e 74 20 20 20 20 20 7c 7c 20 61 66 66  ToInt     || aff
21812 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  !=SQLITE_AFF_INT
21813 45 47 45 52 20 29 3b 0a 20 20 20 20 20 20 61 73  EGER );.      as
21814 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  sert( to_op==OP_
21815 54 6f 52 65 61 6c 20 20 20 20 7c 7c 20 61 66 66  ToReal    || aff
21816 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  !=SQLITE_AFF_REA
21817 4c 20 20 20 20 29 3b 0a 20 20 20 20 20 20 74 65  L    );.      te
21818 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f  stcase( to_op==O
21819 50 5f 54 6f 54 65 78 74 20 29 3b 0a 20 20 20 20  P_ToText );.    
2181a 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f    testcase( to_o
2181b 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 29 3b 0a  p==OP_ToBlob );.
2181c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2181d 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65  to_op==OP_ToNume
2181e 72 69 63 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ric );.      tes
2181f 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50  tcase( to_op==OP
21820 5f 54 6f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20  _ToInt );.      
21821 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d  testcase( to_op=
21822 3d 4f 50 5f 54 6f 52 65 61 6c 20 29 3b 0a 20 20  =OP_ToReal );.  
21823 20 20 20 20 69 66 28 20 69 6e 52 65 67 21 3d 74      if( inReg!=t
21824 61 72 67 65 74 20 29 7b 0a 20 20 20 20 20 20 20  arget ){.       
21825 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21826 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
21827 69 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b 0a  inReg, target);.
21828 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20          inReg = 
21829 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 7d 0a  target;.      }.
2182a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2182b 65 41 64 64 4f 70 31 28 76 2c 20 74 6f 5f 6f 70  eAddOp1(v, to_op
2182c 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20  , inReg);.      
2182d 74 65 73 74 63 61 73 65 28 20 75 73 65 64 41 73  testcase( usedAs
2182e 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72  ColumnCache(pPar
2182f 73 65 2c 20 69 6e 52 65 67 2c 20 69 6e 52 65 67  se, inReg, inReg
21830 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) );.      sqlit
21831 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
21832 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
21833 2c 20 69 6e 52 65 67 2c 20 31 29 3b 0a 20 20 20  , inReg, 1);.   
21834 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21835 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
21836 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 20 20  _OMIT_CAST */.  
21837 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20    case TK_LT:.  
21838 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20    case TK_LE:.  
21839 20 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20    case TK_GT:.  
2183a 20 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20    case TK_GE:.  
2183b 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20    case TK_NE:.  
2183c 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a    case TK_EQ: {.
2183d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
2183e 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20  _LT==OP_Lt );.  
2183f 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c      assert( TK_L
21840 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20  E==OP_Le );.    
21841 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d    assert( TK_GT=
21842 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20  =OP_Gt );.      
21843 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f  assert( TK_GE==O
21844 50 5f 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Ge );.      as
21845 73 65 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f  sert( TK_EQ==OP_
21846 45 71 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Eq );.      asse
21847 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65  rt( TK_NE==OP_Ne
21848 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21849 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b  se( op==TK_LT );
2184a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2184b 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20   op==TK_LE );.  
2184c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
2184d 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20  ==TK_GT );.     
2184e 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
2184f 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65  K_GE );.      te
21850 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45  stcase( op==TK_E
21851 51 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  Q );.      testc
21852 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29  ase( op==TK_NE )
21853 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
21854 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72  areOperands(pPar
21855 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
21856 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31  , &r1, &regFree1
21857 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
21858 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21859 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68      pExpr->pRigh
2185a 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65  t, &r2, &regFree
2185b 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f  2);.      codeCo
2185c 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45  mpare(pParse, pE
2185d 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70  xpr->pLeft, pExp
2185e 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20  r->pRight, op,. 
2185f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21860 20 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20   r1, r2, inReg, 
21861 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b  SQLITE_STOREP2);
21862 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21863 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
21864 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21865 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20  regFree2==0 );. 
21866 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21867 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53  }.    case TK_IS
21868 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53  :.    case TK_IS
21869 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73  NOT: {.      tes
2186a 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53  tcase( op==TK_IS
2186b 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2186c 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 4f 54  se( op==TK_ISNOT
2186d 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f   );.      codeCo
2186e 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50  mpareOperands(pP
2186f 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
21870 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65  ft, &r1, &regFre
21871 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  e1,.            
21872 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21873 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69        pExpr->pRi
21874 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72  ght, &r2, &regFr
21875 65 65 32 29 3b 0a 20 20 20 20 20 20 6f 70 20 3d  ee2);.      op =
21876 20 28 6f 70 3d 3d 54 4b 5f 49 53 29 20 3f 20 54   (op==TK_IS) ? T
21877 4b 5f 45 51 20 3a 20 54 4b 5f 4e 45 3b 0a 20 20  K_EQ : TK_NE;.  
21878 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
21879 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
2187a 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Left, pExpr->pRi
2187b 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20  ght, op,.       
2187c 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72             r1, r
2187d 32 2c 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45  2, inReg, SQLITE
2187e 5f 53 54 4f 52 45 50 32 20 7c 20 53 51 4c 49 54  _STOREP2 | SQLIT
2187f 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20 20 20 20 20  E_NULLEQ);.     
21880 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
21881 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
21882 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
21883 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e2==0 );.      b
21884 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
21885 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0a 20 20 20  case TK_AND:.   
21886 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0a 20 20 20   case TK_OR:.   
21887 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 3a 0a 20   case TK_PLUS:. 
21888 20 20 20 63 61 73 65 20 54 4b 5f 53 54 41 52 3a     case TK_STAR:
21889 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4d 49 4e  .    case TK_MIN
2188a 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  US:.    case TK_
2188b 52 45 4d 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  REM:.    case TK
2188c 5f 42 49 54 41 4e 44 3a 0a 20 20 20 20 63 61 73  _BITAND:.    cas
2188d 65 20 54 4b 5f 42 49 54 4f 52 3a 0a 20 20 20 20  e TK_BITOR:.    
2188e 63 61 73 65 20 54 4b 5f 53 4c 41 53 48 3a 0a 20  case TK_SLASH:. 
2188f 20 20 20 63 61 73 65 20 54 4b 5f 4c 53 48 49 46     case TK_LSHIF
21890 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52  T:.    case TK_R
21891 53 48 49 46 54 3a 20 0a 20 20 20 20 63 61 73 65  SHIFT: .    case
21892 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 7b 0a 20 20   TK_CONCAT: {.  
21893 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 41      assert( TK_A
21894 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20  ND==OP_And );.  
21895 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4f      assert( TK_O
21896 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a 20 20 20 20  R==OP_Or );.    
21897 20 20 61 73 73 65 72 74 28 20 54 4b 5f 50 4c 55    assert( TK_PLU
21898 53 3d 3d 4f 50 5f 41 64 64 20 29 3b 0a 20 20 20  S==OP_Add );.   
21899 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4d 49     assert( TK_MI
2189a 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74  NUS==OP_Subtract
2189b 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
2189c 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d  ( TK_REM==OP_Rem
2189d 61 69 6e 64 65 72 20 29 3b 0a 20 20 20 20 20 20  ainder );.      
2189e 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 41 4e  assert( TK_BITAN
2189f 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20 29 3b 0a  D==OP_BitAnd );.
218a0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
218a1 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72  _BITOR==OP_BitOr
218a2 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
218a3 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44  ( TK_SLASH==OP_D
218a4 69 76 69 64 65 20 29 3b 0a 20 20 20 20 20 20 61  ivide );.      a
218a5 73 73 65 72 74 28 20 54 4b 5f 4c 53 48 49 46 54  ssert( TK_LSHIFT
218a6 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 29  ==OP_ShiftLeft )
218a7 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
218a8 54 4b 5f 52 53 48 49 46 54 3d 3d 4f 50 5f 53 68  TK_RSHIFT==OP_Sh
218a9 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 20 20  iftRight );.    
218aa 20 20 61 73 73 65 72 74 28 20 54 4b 5f 43 4f 4e    assert( TK_CON
218ab 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 61 74 20 29  CAT==OP_Concat )
218ac 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
218ad 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a  ( op==TK_AND );.
218ae 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
218af 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 20  op==TK_OR );.   
218b0 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
218b1 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a 20 20 20 20  =TK_PLUS );.    
218b2 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
218b3 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a 20 20 20 20  TK_MINUS );.    
218b4 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
218b5 54 4b 5f 52 45 4d 20 29 3b 0a 20 20 20 20 20 20  TK_REM );.      
218b6 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
218b7 5f 42 49 54 41 4e 44 20 29 3b 0a 20 20 20 20 20  _BITAND );.     
218b8 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
218b9 4b 5f 42 49 54 4f 52 20 29 3b 0a 20 20 20 20 20  K_BITOR );.     
218ba 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
218bb 4b 5f 53 4c 41 53 48 20 29 3b 0a 20 20 20 20 20  K_SLASH );.     
218bc 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
218bd 4b 5f 4c 53 48 49 46 54 20 29 3b 0a 20 20 20 20  K_LSHIFT );.    
218be 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
218bf 54 4b 5f 52 53 48 49 46 54 20 29 3b 0a 20 20 20  TK_RSHIFT );.   
218c0 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
218c1 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 3b 0a 20 20  =TK_CONCAT );.  
218c2 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
218c3 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
218c4 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
218c5 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  t, &regFree1);. 
218c6 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65       r2 = sqlite
218c7 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
218c8 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69  arse, pExpr->pRi
218c9 67 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b  ght, &regFree2);
218ca 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
218cb 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20  beAddOp3(v, op, 
218cc 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b  r2, r1, target);
218cd 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
218ce 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
218cf 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
218d0 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20  regFree2==0 );. 
218d1 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
218d2 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d  }.    case TK_UM
218d3 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 45 78  INUS: {.      Ex
218d4 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70  pr *pLeft = pExp
218d5 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r->pLeft;.      
218d6 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b  assert( pLeft );
218d7 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74  .      if( pLeft
218d8 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29  ->op==TK_FLOAT )
218d9 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
218da 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
218db 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74  ty(pExpr, EP_Int
218dc 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20  Value) );.      
218dd 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 70 4c    codeReal(v, pL
218de 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 31  eft->u.zToken, 1
218df 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
218e0 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 66 74   }else if( pLeft
218e1 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52  ->op==TK_INTEGER
218e2 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65   ){.        code
218e3 49 6e 74 65 67 65 72 28 76 2c 20 70 4c 65 66 74  Integer(v, pLeft
218e4 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 1, target);.  
218e5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
218e6 20 20 20 72 65 67 46 72 65 65 31 20 3d 20 72 31     regFree1 = r1
218e7 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
218e8 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
218e9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
218ea 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
218eb 74 65 67 65 72 2c 20 30 2c 20 72 31 29 3b 0a 20  teger, 0, r1);. 
218ec 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69         r2 = sqli
218ed 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
218ee 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
218ef 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 32 29  Left, &regFree2)
218f0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
218f1 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
218f2 50 5f 53 75 62 74 72 61 63 74 2c 20 72 32 2c 20  P_Subtract, r2, 
218f3 72 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  r1, target);.   
218f4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
218f5 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20  egFree2==0 );.  
218f6 20 20 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65      }.      inRe
218f7 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20  g = target;.    
218f8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
218f9 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4e 4f     case TK_BITNO
218fa 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  T:.    case TK_N
218fb 4f 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  OT: {.      asse
218fc 72 74 28 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f  rt( TK_BITNOT==O
218fd 50 5f 42 69 74 4e 6f 74 20 29 3b 0a 20 20 20 20  P_BitNot );.    
218fe 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54    assert( TK_NOT
218ff 3d 3d 4f 50 5f 4e 6f 74 20 29 3b 0a 20 20 20 20  ==OP_Not );.    
21900 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21901 54 4b 5f 42 49 54 4e 4f 54 20 29 3b 0a 20 20 20  TK_BITNOT );.   
21902 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21903 3d 54 4b 5f 4e 4f 54 20 29 3b 0a 20 20 20 20 20  =TK_NOT );.     
21904 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r1 = sqlite3Exp
21905 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
21906 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
21907 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20  &regFree1);.    
21908 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
21909 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree1==0 );.     
2190a 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b   inReg = target;
2190b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2190c 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20  beAddOp2(v, op, 
2190d 72 31 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20  r1, inReg);.    
2190e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2190f 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c     case TK_ISNUL
21910 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  L:.    case TK_N
21911 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  OTNULL: {.      
21912 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20  int addr;.      
21913 61 73 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c  assert( TK_ISNUL
21914 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a  L==OP_IsNull );.
21915 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
21916 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74  _NOTNULL==OP_Not
21917 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65  Null );.      te
21918 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49  stcase( op==TK_I
21919 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74  SNULL );.      t
2191a 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2191b 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20  NOTNULL );.     
2191c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2191d 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2191e 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 1, target);.  
2191f 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
21920 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
21921 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
21922 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  t, &regFree1);. 
21923 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
21924 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
21925 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
21926 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
21927 6f 70 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73  op, r1);.      s
21928 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21929 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74  (v, OP_AddImm, t
2192a 61 72 67 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20  arget, -1);.    
2192b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2192c 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
2192d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2192e 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41   }.    case TK_A
2192f 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20  GG_FUNCTION: {. 
21930 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 49       AggInfo *pI
21931 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67  nfo = pExpr->pAg
21932 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28  gInfo;.      if(
21933 20 70 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20   pInfo==0 ){.   
21934 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
21935 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
21936 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
21937 29 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) );.        sql
21938 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
21939 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20  rse, "misuse of 
2193a 61 67 67 72 65 67 61 74 65 3a 20 25 73 28 29 22  aggregate: %s()"
2193b 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  , pExpr->u.zToke
2193c 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  n);.      }else{
2193d 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d  .        inReg =
2193e 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45   pInfo->aFunc[pE
2193f 78 70 72 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b  xpr->iAgg].iMem;
21940 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
21941 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
21942 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55  case TK_CONST_FU
21943 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  NC:.    case TK_
21944 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20  FUNCTION: {.    
21945 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46 61 72    ExprList *pFar
21946 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  g;       /* List
21947 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 67   of function arg
21948 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20  uments */.      
21949 69 6e 74 20 6e 46 61 72 67 3b 20 20 20 20 20 20  int nFarg;      
2194a 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2194b 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 67   of function arg
2194c 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20  uments */.      
2194d 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20  FuncDef *pDef;  
2194e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75         /* The fu
2194f 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
21950 6e 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20  n object */.    
21951 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20    int nId;      
21952 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
21953 74 68 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  th of the functi
21954 6f 6e 20 6e 61 6d 65 20 69 6e 20 62 79 74 65 73  on name in bytes
21955 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   */.      const 
21956 63 68 61 72 20 2a 7a 49 64 3b 20 20 20 20 20 20  char *zId;      
21957 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e   /* The function
21958 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 69   name */.      i
21959 6e 74 20 63 6f 6e 73 74 4d 61 73 6b 20 3d 20 30  nt constMask = 0
2195a 3b 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66  ;     /* Mask of
2195b 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
2195c 6e 74 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e  nts that are con
2195d 73 74 61 6e 74 20 2a 2f 0a 20 20 20 20 20 20 69  stant */.      i
2195e 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
2195f 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
21960 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 75  unter */.      u
21961 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b  8 enc = ENC(db);
21962 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 65 78        /* The tex
21963 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20  t encoding used 
21964 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73 65  by this database
21965 20 2a 2f 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65   */.      CollSe
21966 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 20 20 20  q *pColl = 0;   
21967 20 2f 2a 20 41 20 63 6f 6c 6c 61 74 69 6e 67 20   /* A collating 
21968 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 20 20 20  sequence */..   
21969 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
2196a 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2196b 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
2196c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2196d 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54  se( op==TK_CONST
2196e 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20 20 20 74  _FUNC );.      t
2196f 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21970 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20  FUNCTION );.    
21971 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79    if( ExprHasAny
21972 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
21973 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b  EP_TokenOnly) ){
21974 0a 20 20 20 20 20 20 20 20 70 46 61 72 67 20 3d  .        pFarg =
21975 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   0;.      }else{
21976 0a 20 20 20 20 20 20 20 20 70 46 61 72 67 20 3d  .        pFarg =
21977 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b   pExpr->x.pList;
21978 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e  .      }.      n
21979 46 61 72 67 20 3d 20 70 46 61 72 67 20 3f 20 70  Farg = pFarg ? p
2197a 46 61 72 67 2d 3e 6e 45 78 70 72 20 3a 20 30 3b  Farg->nExpr : 0;
2197b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
2197c 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2197d 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c  pExpr, EP_IntVal
2197e 75 65 29 20 29 3b 0a 20 20 20 20 20 20 7a 49 64  ue) );.      zId
2197f 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b   = pExpr->u.zTok
21980 65 6e 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20  en;.      nId = 
21981 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
21982 7a 49 64 29 3b 0a 20 20 20 20 20 20 70 44 65 66  zId);.      pDef
21983 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
21984 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 49 64 2c 20  nction(db, zId, 
21985 6e 49 64 2c 20 6e 46 61 72 67 2c 20 65 6e 63 2c  nId, nFarg, enc,
21986 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   0);.      if( p
21987 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Def==0 ){.      
21988 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
21989 67 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f  g(pParse, "unkno
2198a 77 6e 20 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a  wn function: %.*
2198b 73 28 29 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b  s()", nId, zId);
2198c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2198d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2198e 28 20 70 46 61 72 67 20 29 7b 0a 20 20 20 20 20  ( pFarg ){.     
2198f 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47     r1 = sqlite3G
21990 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
21991 73 65 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20  se, nFarg);.    
21992 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
21993 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29  achePush(pParse)
21994 3b 20 20 20 20 20 2f 2a 20 54 69 63 6b 65 74 20  ;     /* Ticket 
21995 32 65 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a  2ea2425d34be */.
21996 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
21997 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
21998 70 50 61 72 73 65 2c 20 70 46 61 72 67 2c 20 72  pParse, pFarg, r
21999 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73  1, 1);.        s
2199a 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
2199b 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 20 20  op(pParse, 1);  
2199c 20 2f 2a 20 54 69 63 6b 65 74 20 32 65 61 32 34   /* Ticket 2ea24
2199d 32 35 64 33 34 62 65 20 2a 2f 0a 20 20 20 20 20  25d34be */.     
2199e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2199f 72 31 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  r1 = 0;.      }.
219a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
219a1 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
219a2 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62  .      /* Possib
219a3 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20  ly overload the 
219a4 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20  function if the 
219a5 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
219a6 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 76 69 72  s.      ** a vir
219a7 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d  tual table colum
219a8 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  n..      **.    
219a9 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69 78 20 66    ** For infix f
219aa 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20  unctions (LIKE, 
219ab 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e  GLOB, REGEXP, an
219ac 64 20 4d 41 54 43 48 29 20 75 73 65 20 74 68 65  d MATCH) use the
219ad 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64  .      ** second
219ae 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74   argument, not t
219af 68 65 20 66 69 72 73 74 2c 20 61 73 20 74 68 65  he first, as the
219b0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 65 73   argument to tes
219b1 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65  t to.      ** se
219b2 65 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6c  e if it is a col
219b3 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c  umn in a virtual
219b4 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73   table.  This is
219b5 20 64 6f 6e 65 20 62 65 63 61 75 73 65 0a 20 20   done because.  
219b6 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 20      ** the left 
219b7 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 66 69 78  operand of infix
219b8 20 66 75 6e 63 74 69 6f 6e 73 20 28 74 68 65 20   functions (the 
219b9 6f 70 65 72 61 6e 64 20 77 65 20 77 61 6e 74 20  operand we want 
219ba 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74  to.      ** cont
219bb 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29  rol overloading)
219bc 20 65 6e 64 73 20 75 70 20 61 73 20 74 68 65 20   ends up as the 
219bd 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
219be 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  to the.      ** 
219bf 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 65  function.  The e
219c0 78 70 72 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f  xpression "A glo
219c1 62 20 42 22 20 69 73 20 65 71 75 69 76 61 6c 65  b B" is equivale
219c2 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20  nt to .      ** 
219c3 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20 57 65 20  "glob(B,A).  We 
219c4 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20  want to use the 
219c5 41 20 69 6e 20 22 41 20 67 6c 6f 62 20 42 22 20  A in "A glob B" 
219c6 74 6f 20 74 65 73 74 0a 20 20 20 20 20 20 2a 2a  to test.      **
219c7 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76   for function ov
219c8 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20  erloading.  But 
219c9 77 65 20 75 73 65 20 74 68 65 20 42 20 74 65 72  we use the B ter
219ca 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22  m in "glob(B,A)"
219cb 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
219cc 20 69 66 28 20 6e 46 61 72 67 3e 3d 32 20 26 26   if( nFarg>=2 &&
219cd 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26   (pExpr->flags &
219ce 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 29 20 29   EP_InfixFunc) )
219cf 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d  {.        pDef =
219d0 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72   sqlite3VtabOver
219d1 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c  loadFunction(db,
219d2 20 70 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46   pDef, nFarg, pF
219d3 61 72 67 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29  arg->a[1].pExpr)
219d4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
219d5 28 20 6e 46 61 72 67 3e 30 20 29 7b 0a 20 20 20  ( nFarg>0 ){.   
219d6 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69       pDef = sqli
219d7 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46  te3VtabOverloadF
219d8 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66  unction(db, pDef
219d9 2c 20 6e 46 61 72 67 2c 20 70 46 61 72 67 2d 3e  , nFarg, pFarg->
219da 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  a[0].pExpr);.   
219db 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
219dc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 61    for(i=0; i<nFa
219dd 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  rg; i++){.      
219de 20 20 69 66 28 20 69 3c 33 32 20 26 26 20 73 71    if( i<32 && sq
219df 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
219e0 61 6e 74 28 70 46 61 72 67 2d 3e 61 5b 69 5d 2e  ant(pFarg->a[i].
219e1 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
219e2 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d      constMask |=
219e3 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20   (1<<i);.       
219e4 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 28   }.        if( (
219e5 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51  pDef->flags & SQ
219e6 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f  LITE_FUNC_NEEDCO
219e7 4c 4c 29 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c  LL)!=0 && !pColl
219e8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43   ){.          pC
219e9 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
219ea 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
219eb 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78   pFarg->a[i].pEx
219ec 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pr);.        }. 
219ed 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
219ee 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53   pDef->flags & S
219ef 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
219f0 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 69  OLL ){.        i
219f1 66 28 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c  f( !pColl ) pCol
219f2 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  l = db->pDfltCol
219f3 6c 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69  l; .        sqli
219f4 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
219f5 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20   OP_CollSeq, 0, 
219f6 30 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43  0, 0, (char *)pC
219f7 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29  oll, P4_COLLSEQ)
219f8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
219f9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
219fa 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  4(v, OP_Function
219fb 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c  , constMask, r1,
219fc 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20   target,.       
219fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
219fe 20 28 63 68 61 72 2a 29 70 44 65 66 2c 20 50 34   (char*)pDef, P4
219ff 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 20  _FUNCDEF);.     
21a00 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
21a01 67 65 50 35 28 76 2c 20 28 75 38 29 6e 46 61 72  geP5(v, (u8)nFar
21a02 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 46  g);.      if( nF
21a03 61 72 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73  arg ){.        s
21a04 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
21a05 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
21a06 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20  1, nFarg);.     
21a07 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
21a08 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
21a09 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
21a0a 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20  r1, nFarg);.    
21a0b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
21a0c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21a0d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20  IT_SUBQUERY.    
21a0e 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a  case TK_EXISTS:.
21a0f 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45      case TK_SELE
21a10 43 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  CT: {.      test
21a11 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 58 49  case( op==TK_EXI
21a12 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73  STS );.      tes
21a13 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 45  tcase( op==TK_SE
21a14 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 69 6e  LECT );.      in
21a15 52 65 67 20 3d 20 73 71 6c 69 74 65 33 43 6f 64  Reg = sqlite3Cod
21a16 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73  eSubselect(pPars
21a17 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  e, pExpr, 0, 0);
21a18 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21a19 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21a1a 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  IN: {.      int 
21a1b 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 30 3b 0a 20  rNotFound = 0;. 
21a1c 20 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61 76       int rMayHav
21a1d 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20  eNull = 0;.     
21a1e 20 69 6e 74 20 6a 32 2c 20 6a 33 2c 20 6a 34 2c   int j2, j3, j4,
21a1f 20 6a 35 3b 0a 20 20 20 20 20 20 63 68 61 72 20   j5;.      char 
21a20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20  affinity;.      
21a21 69 6e 74 20 65 54 79 70 65 3b 0a 0a 20 20 20 20  int eType;..    
21a22 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
21a23 74 28 28 76 2c 20 22 62 65 67 69 6e 20 49 4e 20  t((v, "begin IN 
21a24 65 78 70 72 20 72 25 64 22 2c 20 74 61 72 67 65  expr r%d", targe
21a25 74 29 29 3b 0a 20 20 20 20 20 20 65 54 79 70 65  t));.      eType
21a26 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e   = sqlite3FindIn
21a27 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 45  Index(pParse, pE
21a28 78 70 72 2c 20 26 72 4d 61 79 48 61 76 65 4e 75  xpr, &rMayHaveNu
21a29 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ll);.      if( r
21a2a 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 7b 0a 20  MayHaveNull ){. 
21a2b 20 20 20 20 20 20 20 72 4e 6f 74 46 6f 75 6e 64         rNotFound
21a2c 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
21a2d 6d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  m;.      }..    
21a2e 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
21a2f 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20  the affinity to 
21a30 75 73 65 20 74 6f 20 63 72 65 61 74 65 20 61 20  use to create a 
21a31 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73  key from the res
21a32 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66  ults.      ** of
21a33 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e   the expression.
21a34 20 61 66 66 69 6e 69 74 79 53 74 72 20 73 74 6f   affinityStr sto
21a35 72 65 73 20 61 20 73 74 61 74 69 63 20 73 74 72  res a static str
21a36 69 6e 67 20 73 75 69 74 61 62 6c 65 20 66 6f 72  ing suitable for
21a37 0a 20 20 20 20 20 20 2a 2a 20 50 34 20 6f 66 20  .      ** P4 of 
21a38 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2e 0a 20  OP_MakeRecord.. 
21a39 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 66       */.      af
21a3a 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 69  finity = compari
21a3b 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70  sonAffinity(pExp
21a3c 72 29 3b 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 43  r);...      /* C
21a3d 6f 64 65 20 74 68 65 20 3c 65 78 70 72 3e 20 66  ode the <expr> f
21a3e 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49 4e 20 28  rom "<expr> IN (
21a3f 2e 2e 2e 29 22 2e 20 54 68 65 20 74 65 6d 70 6f  ...)". The tempo
21a40 72 61 72 79 20 74 61 62 6c 65 0a 20 20 20 20 20  rary table.     
21a41 20 2a 2a 20 70 45 78 70 72 2d 3e 69 54 61 62 6c   ** pExpr->iTabl
21a42 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76  e contains the v
21a43 61 6c 75 65 73 20 74 68 61 74 20 6d 61 6b 65 20  alues that make 
21a44 75 70 20 74 68 65 20 28 2e 2e 2e 29 20 73 65 74  up the (...) set
21a45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
21a46 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
21a47 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20  ePush(pParse);. 
21a48 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21a49 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78  Code(pParse, pEx
21a4a 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65  pr->pLeft, targe
21a4b 74 29 3b 0a 20 20 20 20 20 20 6a 32 20 3d 20 73  t);.      j2 = s
21a4c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
21a4d 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 74  (v, OP_IsNull, t
21a4e 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 69 66  arget);.      if
21a4f 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45  ( eType==IN_INDE
21a50 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20  X_ROWID ){.     
21a51 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56     j3 = sqlite3V
21a52 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
21a53 4d 75 73 74 42 65 49 6e 74 2c 20 74 61 72 67 65  MustBeInt, targe
21a54 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 34 20 3d  t);.        j4 =
21a55 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21a56 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
21a57 74 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  ts, pExpr->iTabl
21a58 65 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20  e, 0, target);. 
21a59 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21a5a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
21a5b 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65  nteger, 1, targe
21a5c 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 3d  t);.        j5 =
21a5d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21a5e 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a  p0(v, OP_Goto);.
21a5f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21a60 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
21a61 33 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  3);.        sqli
21a62 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
21a63 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 20  v, j4);.        
21a64 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21a65 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
21a66 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   0, target);.   
21a67 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21a68 20 20 72 32 20 3d 20 72 65 67 46 72 65 65 32 20    r2 = regFree2 
21a69 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
21a6a 52 65 67 28 70 50 61 72 73 65 29 3b 0a 0a 20 20  Reg(pParse);..  
21a6b 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20        /* Create 
21a6c 61 20 72 65 63 6f 72 64 20 61 6e 64 20 74 65 73  a record and tes
21a6d 74 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 65 72  t for set member
21a6e 73 68 69 70 2e 20 49 66 20 74 68 65 20 73 65 74  ship. If the set
21a6f 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20   contains.      
21a70 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 2c 20    ** the value, 
21a71 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65  then jump to the
21a72 20 65 6e 64 20 6f 66 20 74 68 65 20 74 65 73 74   end of the test
21a73 20 63 6f 64 65 2e 20 54 68 65 20 74 61 72 67 65   code. The targe
21a74 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 67  t.        ** reg
21a75 69 73 74 65 72 20 73 74 69 6c 6c 20 63 6f 6e 74  ister still cont
21a76 61 69 6e 73 20 74 68 65 20 74 72 75 65 20 28 31  ains the true (1
21a77 29 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20  ) value written 
21a78 74 6f 20 69 74 20 65 61 72 6c 69 65 72 2e 0a 20  to it earlier.. 
21a79 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
21a7a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21a7b 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
21a7c 63 6f 72 64 2c 20 74 61 72 67 65 74 2c 20 31 2c  cord, target, 1,
21a7d 20 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20   r2, &affinity, 
21a7e 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
21a7f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21a80 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
21a81 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20  target);.       
21a82 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j5 = sqlite3Vdb
21a83 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f  eAddOp3(v, OP_Fo
21a84 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62  und, pExpr->iTab
21a85 6c 65 2c 20 30 2c 20 72 32 29 3b 0a 0a 20 20 20  le, 0, r2);..   
21a86 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73       /* If the s
21a87 65 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65  et membership te
21a88 73 74 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74  st fails, then t
21a89 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
21a8a 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 22 78 20   .        ** "x 
21a8b 49 4e 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 73  IN (...)" expres
21a8c 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 69 74  sion must be eit
21a8d 68 65 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49  her 0 or NULL. I
21a8e 66 20 74 68 65 20 73 65 74 0a 20 20 20 20 20 20  f the set.      
21a8f 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f    ** contains no
21a90 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68   NULL values, th
21a91 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
21a92 20 30 2e 20 49 66 20 74 68 65 20 73 65 74 20 0a   0. If the set .
21a93 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61          ** conta
21a94 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ins one or more 
21a95 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65  NULL values, the
21a96 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  n the result of 
21a97 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 65  the.        ** e
21a98 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6c 73  xpression is als
21a99 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20  o NULL..        
21a9a 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  */.        if( r
21a9b 4e 6f 74 46 6f 75 6e 64 3d 3d 30 20 29 7b 0a 20  NotFound==0 ){. 
21a9c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
21a9d 20 62 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20   branch runs if 
21a9e 69 74 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 63  it is known at c
21a9f 6f 6d 70 69 6c 65 20 74 69 6d 65 20 28 6e 6f 77  ompile time (now
21aa0 29 20 74 68 61 74 20 0a 20 20 20 20 20 20 20 20  ) that .        
21aa1 20 20 2a 2a 20 74 68 65 20 73 65 74 20 63 6f 6e    ** the set con
21aa2 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61  tains no NULL va
21aa3 6c 75 65 73 2e 20 54 68 69 73 20 68 61 70 70 65  lues. This happe
21aa4 6e 73 20 61 73 20 74 68 65 20 72 65 73 75 6c 74  ns as the result
21aa5 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66  .          ** of
21aa6 20 61 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f   a "NOT NULL" co
21aa7 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20  nstraint in the 
21aa8 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
21aa9 20 4e 6f 20 6e 65 65 64 0a 20 20 20 20 20 20 20   No need.       
21aaa 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 68     ** to test th
21aab 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  e data structure
21aac 20 61 74 20 72 75 6e 74 69 6d 65 20 69 6e 20 74   at runtime in t
21aad 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20  his case..      
21aae 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
21aaf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21ab0 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
21ab1 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 0, target);.  
21ab2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
21ab3 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62         /* This b
21ab4 6c 6f 63 6b 20 70 6f 70 75 6c 61 74 65 73 20 74  lock populates t
21ab5 68 65 20 72 4e 6f 74 46 6f 75 6e 64 20 72 65 67  he rNotFound reg
21ab6 69 73 74 65 72 20 77 69 74 68 20 65 69 74 68 65  ister with eithe
21ab7 72 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 20  r NULL.         
21ab8 20 2a 2a 20 6f 72 20 30 20 28 61 6e 20 69 6e 74   ** or 0 (an int
21ab9 65 67 65 72 20 76 61 6c 75 65 29 2e 20 49 66 20  eger value). If 
21aba 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75  the data structu
21abb 72 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 0a  re contains one.
21abc 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20            ** or 
21abd 6d 6f 72 65 20 4e 55 4c 4c 73 2c 20 74 68 65 6e  more NULLs, then
21abe 20 73 65 74 20 72 4e 6f 74 46 6f 75 6e 64 20 74   set rNotFound t
21abf 6f 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73  o NULL. Otherwis
21ac0 65 2c 20 73 65 74 20 69 74 0a 20 20 20 20 20 20  e, set it.      
21ac1 20 20 20 20 2a 2a 20 74 6f 20 30 2e 20 49 66 20      ** to 0. If 
21ac2 72 65 67 69 73 74 65 72 20 72 4d 61 79 48 61 76  register rMayHav
21ac3 65 4e 75 6c 6c 20 69 73 20 61 6c 72 65 61 64 79  eNull is already
21ac4 20 73 65 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c   set to some val
21ac5 75 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ue.          ** 
21ac6 6f 74 68 65 72 20 74 68 61 6e 20 4e 55 4c 4c 2c  other than NULL,
21ac7 20 74 68 65 6e 20 74 68 65 20 74 65 73 74 20 68   then the test h
21ac8 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
21ac9 72 75 6e 20 61 6e 64 20 0a 20 20 20 20 20 20 20  run and .       
21aca 20 20 20 2a 2a 20 72 4e 6f 74 46 6f 75 6e 64 20     ** rNotFound 
21acb 69 73 20 61 6c 72 65 61 64 79 20 70 6f 70 75 6c  is already popul
21acc 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20  ated..          
21acd 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74 61  */.          sta
21ace 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6e  tic const char n
21acf 75 6c 6c 52 65 63 6f 72 64 5b 5d 20 3d 20 7b 20  ullRecord[] = { 
21ad0 30 78 30 32 2c 20 30 78 30 30 20 7d 3b 0a 20 20  0x02, 0x00 };.  
21ad1 20 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c          j3 = sql
21ad2 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
21ad3 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 4d  , OP_NotNull, rM
21ad4 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20  ayHaveNull);.   
21ad5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21ad6 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
21ad7 75 6c 6c 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e  ull, 0, rNotFoun
21ad8 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  d);.          sq
21ad9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
21ada 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 32 2c 20 72  v, OP_Blob, 2, r
21adb 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 30 2c 20  MayHaveNull, 0, 
21adc 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
21add 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 75                nu
21ade 6c 6c 52 65 63 6f 72 64 2c 20 50 34 5f 53 54 41  llRecord, P4_STA
21adf 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20  TIC);.          
21ae0 6a 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j4 = sqlite3Vdbe
21ae1 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75  AddOp3(v, OP_Fou
21ae2 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  nd, pExpr->iTabl
21ae3 65 2c 20 30 2c 20 72 4d 61 79 48 61 76 65 4e 75  e, 0, rMayHaveNu
21ae4 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  ll);.          s
21ae5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21ae6 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
21ae7 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20  0, rNotFound);. 
21ae8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
21ae9 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
21aea 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  j4);.          s
21aeb 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
21aec 72 65 28 76 2c 20 6a 33 29 3b 0a 0a 20 20 20 20  re(v, j3);..    
21aed 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68        /* Copy th
21aee 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73  e value of regis
21aef 74 65 72 20 72 4e 6f 74 46 6f 75 6e 64 20 28 77  ter rNotFound (w
21af0 68 69 63 68 20 69 73 20 65 69 74 68 65 72 20 4e  hich is either N
21af1 55 4c 4c 20 6f 72 20 30 29 0a 20 20 20 20 20 20  ULL or 0).      
21af2 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20      ** into the 
21af3 74 61 72 67 65 74 20 72 65 67 69 73 74 65 72 2e  target register.
21af4 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68   This will be th
21af5 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a  e result of the.
21af6 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70            ** exp
21af7 72 65 73 73 69 6f 6e 2e 0a 20 20 20 20 20 20 20  ression..       
21af8 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
21af9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21afa 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 4e  2(v, OP_Copy, rN
21afb 6f 74 46 6f 75 6e 64 2c 20 74 61 72 67 65 74 29  otFound, target)
21afc 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
21afd 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
21afe 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
21aff 20 6a 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   j2);.      sqli
21b00 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
21b01 76 2c 20 6a 35 29 3b 0a 20 20 20 20 20 20 73 71  v, j5);.      sq
21b02 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
21b03 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20  p(pParse, 1);.  
21b04 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
21b05 28 76 2c 20 22 65 6e 64 20 49 4e 20 65 78 70 72  (v, "end IN expr
21b06 20 72 25 64 22 2c 20 74 61 72 67 65 74 29 29 3b   r%d", target));
21b07 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21b08 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f    }.#endif.    /
21b09 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 20 42 45  *.    **    x BE
21b0a 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20  TWEEN y AND z.  
21b0b 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73    **.    ** This
21b0c 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74   is equivalent t
21b0d 6f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  o.    **.    ** 
21b0e 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a     x>=y AND x<=z
21b0f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58  .    **.    ** X
21b10 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45   is stored in pE
21b11 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20  xpr->pLeft..    
21b12 2a 2a 20 59 20 69 73 20 73 74 6f 72 65 64 20 69  ** Y is stored i
21b13 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  n pExpr->pList->
21b14 61 5b 30 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20  a[0].pExpr..    
21b15 2a 2a 20 5a 20 69 73 20 73 74 6f 72 65 64 20 69  ** Z is stored i
21b16 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  n pExpr->pList->
21b17 61 5b 31 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20  a[1].pExpr..    
21b18 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42  */.    case TK_B
21b19 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20  ETWEEN: {.      
21b1a 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45  Expr *pLeft = pE
21b1b 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  xpr->pLeft;.    
21b1c 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
21b1d 74 5f 69 74 65 6d 20 2a 70 4c 49 74 65 6d 20 3d  t_item *pLItem =
21b1e 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d   pExpr->x.pList-
21b1f 3e 61 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  >a;.      Expr *
21b20 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d  pRight = pLItem-
21b21 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20 20 63  >pExpr;..      c
21b22 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
21b23 64 73 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  ds(pParse, pLeft
21b24 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31  , &r1, &regFree1
21b25 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
21b26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b27 20 20 20 20 70 52 69 67 68 74 2c 20 26 72 32 2c      pRight, &r2,
21b28 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20   &regFree2);.   
21b29 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
21b2a 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
21b2b 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
21b2c 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree2==0 );.     
21b2d 20 72 33 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r3 = sqlite3Get
21b2e 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
21b2f 0a 20 20 20 20 20 20 72 34 20 3d 20 73 71 6c 69  .      r4 = sqli
21b30 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
21b31 61 72 73 65 29 3b 0a 20 20 20 20 20 20 63 6f 64  arse);.      cod
21b32 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c  eCompare(pParse,
21b33 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20   pLeft, pRight, 
21b34 4f 50 5f 47 65 2c 0a 20 20 20 20 20 20 20 20 20  OP_Ge,.         
21b35 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c           r1, r2,
21b36 20 72 33 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52   r3, SQLITE_STOR
21b37 45 50 32 29 3b 0a 20 20 20 20 20 20 70 4c 49 74  EP2);.      pLIt
21b38 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 70 52 69 67  em++;.      pRig
21b39 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78  ht = pLItem->pEx
21b3a 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  pr;.      sqlite
21b3b 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
21b3c 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32  pParse, regFree2
21b3d 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71  );.      r2 = sq
21b3e 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
21b3f 70 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  p(pParse, pRight
21b40 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
21b41 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
21b42 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20  gFree2==0 );.   
21b43 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70     codeCompare(p
21b44 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52  Parse, pLeft, pR
21b45 69 67 68 74 2c 20 4f 50 5f 4c 65 2c 20 72 31 2c  ight, OP_Le, r1,
21b46 20 72 32 2c 20 72 34 2c 20 53 51 4c 49 54 45 5f   r2, r4, SQLITE_
21b47 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20  STOREP2);.      
21b48 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21b49 33 28 76 2c 20 4f 50 5f 41 6e 64 2c 20 72 33 2c  3(v, OP_And, r3,
21b4a 20 72 34 2c 20 74 61 72 67 65 74 29 3b 0a 20 20   r4, target);.  
21b4b 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
21b4c 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
21b4d 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r3);.      sql
21b4e 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
21b4f 65 67 28 70 50 61 72 73 65 2c 20 72 34 29 3b 0a  eg(pParse, r4);.
21b50 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21b51 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55   }.    case TK_U
21b52 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e  PLUS: {.      in
21b53 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Reg = sqlite3Exp
21b54 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72  rCodeTarget(pPar
21b55 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
21b56 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21b57 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
21b58 20 20 20 63 61 73 65 20 54 4b 5f 54 52 49 47 47     case TK_TRIGG
21b59 45 52 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49  ER: {.      /* I
21b5a 66 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20  f the opcode is 
21b5b 54 4b 5f 54 52 49 47 47 45 52 2c 20 74 68 65 6e  TK_TRIGGER, then
21b5c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
21b5d 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20  is a reference. 
21b5e 20 20 20 20 20 2a 2a 20 74 6f 20 61 20 63 6f 6c       ** to a col
21b5f 75 6d 6e 20 69 6e 20 74 68 65 20 6e 65 77 2e 2a  umn in the new.*
21b60 20 6f 72 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f   or old.* pseudo
21b61 2d 74 61 62 6c 65 73 20 61 76 61 69 6c 61 62 6c  -tables availabl
21b62 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 72  e to.      ** tr
21b63 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20  igger programs. 
21b64 49 6e 20 74 68 69 73 20 63 61 73 65 20 45 78 70  In this case Exp
21b65 72 2e 69 54 61 62 6c 65 20 69 73 20 73 65 74 20  r.iTable is set 
21b66 74 6f 20 31 20 66 6f 72 20 74 68 65 0a 20 20 20  to 1 for the.   
21b67 20 20 20 2a 2a 20 6e 65 77 2e 2a 20 70 73 65 75     ** new.* pseu
21b68 64 6f 2d 74 61 62 6c 65 2c 20 6f 72 20 30 20 66  do-table, or 0 f
21b69 6f 72 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65  or the old.* pse
21b6a 75 64 6f 2d 74 61 62 6c 65 2e 20 45 78 70 72 2e  udo-table. Expr.
21b6b 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a  iColumn.      **
21b6c 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 63   is set to the c
21b6d 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 70 73 65  olumn of the pse
21b6e 75 64 6f 2d 74 61 62 6c 65 20 74 6f 20 72 65 61  udo-table to rea
21b6f 64 2c 20 6f 72 20 74 6f 20 2d 31 20 74 6f 0a 20  d, or to -1 to. 
21b70 20 20 20 20 20 2a 2a 20 72 65 61 64 20 74 68 65       ** read the
21b71 20 72 6f 77 69 64 20 66 69 65 6c 64 2e 0a 20 20   rowid field..  
21b72 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
21b73 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  The expression i
21b74 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73  s implemented us
21b75 69 6e 67 20 61 6e 20 4f 50 5f 50 61 72 61 6d 20  ing an OP_Param 
21b76 6f 70 63 6f 64 65 2e 20 54 68 65 20 70 31 0a 20  opcode. The p1. 
21b77 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65       ** paramete
21b78 72 20 69 73 20 73 65 74 20 74 6f 20 30 20 66 6f  r is set to 0 fo
21b79 72 20 61 6e 20 6f 6c 64 2e 72 6f 77 69 64 20 72  r an old.rowid r
21b7a 65 66 65 72 65 6e 63 65 2c 20 6f 72 20 74 6f 20  eference, or to 
21b7b 28 69 2b 31 29 0a 20 20 20 20 20 20 2a 2a 20 74  (i+1).      ** t
21b7c 6f 20 72 65 66 65 72 65 6e 63 65 20 61 6e 6f 74  o reference anot
21b7d 68 65 72 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  her column of th
21b7e 65 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74  e old.* pseudo-t
21b7f 61 62 6c 65 2c 20 77 68 65 72 65 20 0a 20 20 20  able, where .   
21b80 20 20 20 2a 2a 20 69 20 69 73 20 74 68 65 20 69     ** i is the i
21b81 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75  ndex of the colu
21b82 6d 6e 2e 20 46 6f 72 20 61 20 6e 65 77 2e 72 6f  mn. For a new.ro
21b83 77 69 64 20 72 65 66 65 72 65 6e 63 65 2c 20 70  wid reference, p
21b84 31 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 73 65  1 is.      ** se
21b85 74 20 74 6f 20 28 6e 2b 31 29 2c 20 77 68 65 72  t to (n+1), wher
21b86 65 20 6e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e n is the numbe
21b87 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
21b88 65 61 63 68 20 70 73 65 75 64 6f 2d 74 61 62 6c  each pseudo-tabl
21b89 65 2e 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 20  e..      ** For 
21b8a 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61  a reference to a
21b8b 6e 79 20 6f 74 68 65 72 20 63 6f 6c 75 6d 6e 20  ny other column 
21b8c 69 6e 20 74 68 65 20 6e 65 77 2e 2a 20 70 73 65  in the new.* pse
21b8d 75 64 6f 2d 74 61 62 6c 65 2c 20 70 31 0a 20 20  udo-table, p1.  
21b8e 20 20 20 20 2a 2a 20 69 73 20 73 65 74 20 74 6f      ** is set to
21b8f 20 28 6e 2b 32 2b 69 29 2c 20 77 68 65 72 65 20   (n+2+i), where 
21b90 6e 20 61 6e 64 20 69 20 61 72 65 20 61 73 20 64  n and i are as d
21b91 65 66 69 6e 65 64 20 70 72 65 76 69 6f 75 73 6c  efined previousl
21b92 79 2e 20 46 6f 72 0a 20 20 20 20 20 20 2a 2a 20  y. For.      ** 
21b93 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20  example, if the 
21b94 74 61 62 6c 65 20 6f 6e 20 77 68 69 63 68 20 74  table on which t
21b95 72 69 67 67 65 72 73 20 61 72 65 20 62 65 69 6e  riggers are bein
21b96 67 20 66 69 72 65 64 20 69 73 0a 20 20 20 20 20  g fired is.     
21b97 20 2a 2a 20 64 65 63 6c 61 72 65 64 20 61 73 3a   ** declared as:
21b98 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
21b99 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  **   CREATE TABL
21b9a 45 20 74 31 28 61 2c 20 62 29 3b 0a 20 20 20 20  E t1(a, b);.    
21b9b 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68    **.      ** Th
21b9c 65 6e 20 70 31 20 69 73 20 69 6e 74 65 72 70 72  en p1 is interpr
21b9d 65 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  eted as follows:
21b9e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
21b9f 2a 2a 20 20 20 70 31 3d 3d 30 20 20 20 2d 3e 20  **   p1==0   -> 
21ba0 20 20 20 6f 6c 64 2e 72 6f 77 69 64 20 20 20 20     old.rowid    
21ba1 20 70 31 3d 3d 33 20 20 20 2d 3e 20 20 20 20 6e   p1==3   ->    n
21ba2 65 77 2e 72 6f 77 69 64 0a 20 20 20 20 20 20 2a  ew.rowid.      *
21ba3 2a 20 20 20 70 31 3d 3d 31 20 20 20 2d 3e 20 20  *   p1==1   ->  
21ba4 20 20 6f 6c 64 2e 61 20 20 20 20 20 20 20 20 20    old.a         
21ba5 70 31 3d 3d 34 20 20 20 2d 3e 20 20 20 20 6e 65  p1==4   ->    ne
21ba6 77 2e 61 0a 20 20 20 20 20 20 2a 2a 20 20 20 70  w.a.      **   p
21ba7 31 3d 3d 32 20 20 20 2d 3e 20 20 20 20 6f 6c 64  1==2   ->    old
21ba8 2e 62 20 20 20 20 20 20 20 20 20 70 31 3d 3d 35  .b         p1==5
21ba9 20 20 20 2d 3e 20 20 20 20 6e 65 77 2e 62 20 20     ->    new.b  
21baa 20 20 20 20 20 0a 20 20 20 20 20 20 2a 2f 0a 20       .      */. 
21bab 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
21bac 20 3d 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a   = pExpr->pTab;.
21bad 20 20 20 20 20 20 69 6e 74 20 70 31 20 3d 20 70        int p1 = p
21bae 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 2a 20 28  Expr->iTable * (
21baf 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 20 2b 20  pTab->nCol+1) + 
21bb0 31 20 2b 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  1 + pExpr->iColu
21bb1 6d 6e 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72  mn;..      asser
21bb2 74 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  t( pExpr->iTable
21bb3 3d 3d 30 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54  ==0 || pExpr->iT
21bb4 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20  able==1 );.     
21bb5 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
21bb6 69 43 6f 6c 75 6d 6e 3e 3d 2d 31 20 26 26 20 70  iColumn>=-1 && p
21bb7 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 54  Expr->iColumn<pT
21bb8 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20  ab->nCol );.    
21bb9 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
21bba 69 50 4b 65 79 3c 30 20 7c 7c 20 70 45 78 70 72  iPKey<0 || pExpr
21bbb 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70 54 61 62 2d  ->iColumn!=pTab-
21bbc 3e 69 50 4b 65 79 20 29 3b 0a 20 20 20 20 20 20  >iPKey );.      
21bbd 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26  assert( p1>=0 &&
21bbe 20 70 31 3c 28 70 54 61 62 2d 3e 6e 43 6f 6c 2a   p1<(pTab->nCol*
21bbf 32 2b 32 29 20 29 3b 0a 0a 20 20 20 20 20 20 73  2+2) );..      s
21bc0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21bc1 28 76 2c 20 4f 50 5f 50 61 72 61 6d 2c 20 70 31  (v, OP_Param, p1
21bc2 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21bc3 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
21bc4 20 22 25 73 2e 25 73 20 2d 3e 20 24 25 64 22 2c   "%s.%s -> $%d",
21bc5 0a 20 20 20 20 20 20 20 20 28 70 45 78 70 72 2d  .        (pExpr-
21bc6 3e 69 54 61 62 6c 65 20 3f 20 22 6e 65 77 22 20  >iTable ? "new" 
21bc7 3a 20 22 6f 6c 64 22 29 2c 0a 20 20 20 20 20 20  : "old"),.      
21bc8 20 20 28 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d    (pExpr->iColum
21bc9 6e 3c 30 20 3f 20 22 72 6f 77 69 64 22 20 3a 20  n<0 ? "rowid" : 
21bca 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 61 43 6f  pExpr->pTab->aCo
21bcb 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  l[pExpr->iColumn
21bcc 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20  ].zName),.      
21bcd 20 20 74 61 72 67 65 74 0a 20 20 20 20 20 20 29    target.      )
21bce 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  );..      /* If 
21bcf 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 52  the column has R
21bd0 45 41 4c 20 61 66 66 69 6e 69 74 79 2c 20 69 74  EAL affinity, it
21bd1 20 6d 61 79 20 63 75 72 72 65 6e 74 6c 79 20 62   may currently b
21bd2 65 20 73 74 6f 72 65 64 20 61 73 20 61 6e 0a 20  e stored as an. 
21bd3 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e       ** integer.
21bd4 20 55 73 65 20 4f 50 5f 52 65 61 6c 41 66 66 69   Use OP_RealAffi
21bd5 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20 73 75 72  nity to make sur
21bd6 65 20 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72  e it is really r
21bd7 65 61 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69  eal.  */.      i
21bd8 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  f( pExpr->iColum
21bd9 6e 3e 3d 30 20 0a 20 20 20 20 20 20 20 26 26 20  n>=0 .       && 
21bda 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72  pTab->aCol[pExpr
21bdb 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e  ->iColumn].affin
21bdc 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
21bdd 52 45 41 4c 0a 20 20 20 20 20 20 29 7b 0a 20 20  REAL.      ){.  
21bde 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21bdf 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65  eAddOp1(v, OP_Re
21be0 61 6c 41 66 66 69 6e 69 74 79 2c 20 74 61 72 67  alAffinity, targ
21be1 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  et);.      }.   
21be2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21be3 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
21be4 46 6f 72 6d 20 41 3a 0a 20 20 20 20 2a 2a 20 20  Form A:.    **  
21be5 20 43 41 53 45 20 78 20 57 48 45 4e 20 65 31 20   CASE x WHEN e1 
21be6 54 48 45 4e 20 72 31 20 57 48 45 4e 20 65 32 20  THEN r1 WHEN e2 
21be7 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e  THEN r2 ... WHEN
21be8 20 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45   eN THEN rN ELSE
21be9 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20   y END.    **.  
21bea 20 20 2a 2a 20 46 6f 72 6d 20 42 3a 0a 20 20 20    ** Form B:.   
21beb 20 2a 2a 20 20 20 43 41 53 45 20 57 48 45 4e 20   **   CASE WHEN 
21bec 65 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20  e1 THEN r1 WHEN 
21bed 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57  e2 THEN r2 ... W
21bee 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e 20 45  HEN eN THEN rN E
21bef 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a  LSE y END.    **
21bf0 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41 20 69  .    ** Form A i
21bf1 73 20 63 61 6e 20 62 65 20 74 72 61 6e 73 66 6f  s can be transfo
21bf2 72 6d 65 64 20 69 6e 74 6f 20 74 68 65 20 65 71  rmed into the eq
21bf3 75 69 76 61 6c 65 6e 74 20 66 6f 72 6d 20 42 20  uivalent form B 
21bf4 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20  as follows:.    
21bf5 2a 2a 20 20 20 43 41 53 45 20 57 48 45 4e 20 78  **   CASE WHEN x
21bf6 3d 65 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e  =e1 THEN r1 WHEN
21bf7 20 78 3d 65 32 20 54 48 45 4e 20 72 32 20 2e 2e   x=e2 THEN r2 ..
21bf8 2e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  ..    **        
21bf9 57 48 45 4e 20 78 3d 65 4e 20 54 48 45 4e 20 72  WHEN x=eN THEN r
21bfa 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20  N ELSE y END.   
21bfb 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 28 69 66   **.    ** X (if
21bfc 20 69 74 20 65 78 69 73 74 73 29 20 69 73 20 69   it exists) is i
21bfd 6e 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a  n pExpr->pLeft..
21bfe 20 20 20 20 2a 2a 20 59 20 69 73 20 69 6e 20 70      ** Y is in p
21bff 45 78 70 72 2d 3e 70 52 69 67 68 74 2e 20 20 54  Expr->pRight.  T
21c00 68 65 20 59 20 69 73 20 61 6c 73 6f 20 6f 70 74  he Y is also opt
21c01 69 6f 6e 61 6c 2e 20 20 49 66 20 74 68 65 72 65  ional.  If there
21c02 20 69 73 20 6e 6f 0a 20 20 20 20 2a 2a 20 45 4c   is no.    ** EL
21c03 53 45 20 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f  SE clause and no
21c04 20 6f 74 68 65 72 20 74 65 72 6d 20 6d 61 74 63   other term matc
21c05 68 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65  hes, then the re
21c06 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20  sult of the.    
21c07 2a 2a 20 65 78 70 72 73 73 69 6f 6e 20 69 73 20  ** exprssion is 
21c08 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 45 69 20  NULL..    ** Ei 
21c09 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69  is in pExpr->pLi
21c0a 73 74 2d 3e 61 5b 69 2a 32 5d 20 61 6e 64 20 52  st->a[i*2] and R
21c0b 69 20 69 73 20 70 45 78 70 72 2d 3e 70 4c 69 73  i is pExpr->pLis
21c0c 74 2d 3e 61 5b 69 2a 32 2b 31 5d 2e 0a 20 20 20  t->a[i*2+1]..   
21c0d 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72   **.    ** The r
21c0e 65 73 75 6c 74 20 6f 66 20 74 68 65 20 65 78 70  esult of the exp
21c0f 72 65 73 73 69 6f 6e 20 69 73 20 74 68 65 20 52  ression is the R
21c10 69 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  i for the first 
21c11 6d 61 74 63 68 69 6e 67 20 45 69 2c 0a 20 20 20  matching Ei,.   
21c12 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 72 65 20   ** or if there 
21c13 69 73 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 45  is no matching E
21c14 69 2c 20 74 68 65 20 45 4c 53 45 20 74 65 72 6d  i, the ELSE term
21c15 20 59 2c 20 6f 72 20 69 66 20 74 68 65 72 65 20   Y, or if there 
21c16 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 20 45 4c 53  is.    ** no ELS
21c17 45 20 74 65 72 6d 2c 20 4e 55 4c 4c 2e 0a 20 20  E term, NULL..  
21c18 20 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74    */.    default
21c19 3a 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b  : assert( op==TK
21c1a 5f 43 41 53 45 20 29 3b 20 7b 0a 20 20 20 20 20  _CASE ); {.     
21c1b 20 69 6e 74 20 65 6e 64 4c 61 62 65 6c 3b 20 20   int endLabel;  
21c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c1d 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65 6c     /* GOTO label
21c1e 20 66 6f 72 20 65 6e 64 20 6f 66 20 43 41 53 45   for end of CASE
21c1f 20 73 74 6d 74 20 2a 2f 0a 20 20 20 20 20 20 69   stmt */.      i
21c20 6e 74 20 6e 65 78 74 43 61 73 65 3b 20 20 20 20  nt nextCase;    
21c21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c22 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65 6c 20 66   /* GOTO label f
21c23 6f 72 20 6e 65 78 74 20 57 48 45 4e 20 63 6c 61  or next WHEN cla
21c24 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  use */.      int
21c25 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20   nExpr;         
21c26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
21c27 2a 20 32 78 20 6e 75 6d 62 65 72 20 6f 66 20 57  * 2x number of W
21c28 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20  HEN terms */.   
21c29 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
21c2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c2b 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
21c2c 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 45 78  nter */.      Ex
21c2d 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20  prList *pEList; 
21c2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c2f 2f 2a 20 4c 69 73 74 20 6f 66 20 57 48 45 4e 20  /* List of WHEN 
21c30 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 73  terms */.      s
21c31 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
21c32 74 65 6d 20 2a 61 4c 69 73 74 65 6c 65 6d 3b 20  tem *aListelem; 
21c33 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 57 48 45   /* Array of WHE
21c34 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20  N terms */.     
21c35 20 45 78 70 72 20 6f 70 43 6f 6d 70 61 72 65 3b   Expr opCompare;
21c36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c37 20 20 20 2f 2a 20 54 68 65 20 58 3d 3d 45 69 20     /* The X==Ei 
21c38 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
21c39 20 20 20 20 45 78 70 72 20 63 61 63 68 65 58 3b      Expr cacheX;
21c3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20        /* Cached 
21c3c 65 78 70 72 65 73 73 69 6f 6e 20 58 20 2a 2f 0a  expression X */.
21c3d 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 3b 20        Expr *pX; 
21c3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c3f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58          /* The X
21c40 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
21c41 20 20 20 20 20 45 78 70 72 20 2a 70 54 65 73 74       Expr *pTest
21c42 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
21c43 20 20 20 20 20 20 20 2f 2a 20 58 3d 3d 45 69 20         /* X==Ei 
21c44 28 66 6f 72 6d 20 41 29 20 6f 72 20 6a 75 73 74  (form A) or just
21c45 20 45 69 20 28 66 6f 72 6d 20 42 29 20 2a 2f 0a   Ei (form B) */.
21c46 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20        VVA_ONLY( 
21c47 69 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c 20  int iCacheLevel 
21c48 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65  = pParse->iCache
21c49 4c 65 76 65 6c 3b 20 29 0a 0a 20 20 20 20 20 20  Level; )..      
21c4a 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
21c4b 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
21c4c 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 26 26  EP_xIsSelect) &&
21c4d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20   pExpr->x.pList 
21c4e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21c4f 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d  (pExpr->x.pList-
21c50 3e 6e 45 78 70 72 20 25 20 32 29 20 3d 3d 20 30  >nExpr % 2) == 0
21c51 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21c52 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e  pExpr->x.pList->
21c53 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20 20 20 20  nExpr > 0);.    
21c54 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72    pEList = pExpr
21c55 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20  ->x.pList;.     
21c56 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20 70 45 4c   aListelem = pEL
21c57 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 6e 45  ist->a;.      nE
21c58 78 70 72 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45  xpr = pEList->nE
21c59 78 70 72 3b 0a 20 20 20 20 20 20 65 6e 64 4c 61  xpr;.      endLa
21c5a 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62  bel = sqlite3Vdb
21c5b 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
21c5c 20 20 20 20 20 69 66 28 20 28 70 58 20 3d 20 70       if( (pX = p
21c5d 45 78 70 72 2d 3e 70 4c 65 66 74 29 21 3d 30 20  Expr->pLeft)!=0 
21c5e 29 7b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65  ){.        cache
21c5f 58 20 3d 20 2a 70 58 3b 0a 20 20 20 20 20 20 20  X = *pX;.       
21c60 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f   testcase( pX->o
21c61 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a  p==TK_COLUMN );.
21c62 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
21c63 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47  ( pX->op==TK_REG
21c64 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20  ISTER );.       
21c65 20 63 61 63 68 65 58 2e 69 54 61 62 6c 65 20 3d   cacheX.iTable =
21c66 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21c67 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 58 2c  Temp(pParse, pX,
21c68 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20   &regFree1);.   
21c69 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
21c6a 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
21c6b 20 20 20 20 20 20 63 61 63 68 65 58 2e 6f 70 20        cacheX.op 
21c6c 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20  = TK_REGISTER;. 
21c6d 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65         opCompare
21c6e 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20  .op = TK_EQ;.   
21c6f 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70       opCompare.p
21c70 4c 65 66 74 20 3d 20 26 63 61 63 68 65 58 3b 0a  Left = &cacheX;.
21c71 20 20 20 20 20 20 20 20 70 54 65 73 74 20 3d 20          pTest = 
21c72 26 6f 70 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20  &opCompare;.    
21c73 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d    }.      for(i=
21c74 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69 2b  0; i<nExpr; i=i+
21c75 32 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  2){.        sqli
21c76 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68  te3ExprCachePush
21c77 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
21c78 20 20 69 66 28 20 70 58 20 29 7b 0a 20 20 20 20    if( pX ){.    
21c79 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
21c7a 65 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  est!=0 );.      
21c7b 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 52      opCompare.pR
21c7c 69 67 68 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d  ight = aListelem
21c7d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
21c7e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21c7f 20 20 20 20 70 54 65 73 74 20 3d 20 61 4c 69 73      pTest = aLis
21c80 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a  telem[i].pExpr;.
21c81 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
21c82 20 20 6e 65 78 74 43 61 73 65 20 3d 20 73 71 6c    nextCase = sql
21c83 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
21c84 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 74 65  l(v);.        te
21c85 73 74 63 61 73 65 28 20 70 54 65 73 74 2d 3e 6f  stcase( pTest->o
21c86 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a  p==TK_COLUMN );.
21c87 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
21c88 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
21c89 65 2c 20 70 54 65 73 74 2c 20 6e 65 78 74 43 61  e, pTest, nextCa
21c8a 73 65 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  se, SQLITE_JUMPI
21c8b 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20  FNULL);.        
21c8c 74 65 73 74 63 61 73 65 28 20 61 4c 69 73 74 65  testcase( aListe
21c8d 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e  lem[i+1].pExpr->
21c8e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b  op==TK_COLUMN );
21c8f 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
21c90 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31  e( aListelem[i+1
21c91 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ].pExpr->op==TK_
21c92 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20  REGISTER );.    
21c93 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
21c94 6f 64 65 28 70 50 61 72 73 65 2c 20 61 4c 69 73  ode(pParse, aLis
21c95 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72  telem[i+1].pExpr
21c96 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21c97 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21c98 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
21c99 20 30 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20   0, endLabel);. 
21c9a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
21c9b 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73  prCachePop(pPars
21c9c 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73  e, 1);.        s
21c9d 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
21c9e 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78 74 43 61  eLabel(v, nextCa
21c9f 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  se);.      }.   
21ca0 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52     if( pExpr->pR
21ca1 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20  ight ){.        
21ca2 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
21ca3 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20  Push(pParse);.  
21ca4 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21ca5 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45  rCode(pParse, pE
21ca6 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 74 61 72  xpr->pRight, tar
21ca7 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  get);.        sq
21ca8 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
21ca9 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20  p(pParse, 1);.  
21caa 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21cab 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21cac 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
21cad 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   0, target);.   
21cae 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
21caf 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
21cb0 6c 65 64 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e  led || pParse->n
21cb1 45 72 72 3e 30 20 0a 20 20 20 20 20 20 20 20 20  Err>0 .         
21cb2 20 20 7c 7c 20 70 50 61 72 73 65 2d 3e 69 43 61    || pParse->iCa
21cb3 63 68 65 4c 65 76 65 6c 3d 3d 69 43 61 63 68 65  cheLevel==iCache
21cb4 4c 65 76 65 6c 20 29 3b 0a 20 20 20 20 20 20 73  Level );.      s
21cb5 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
21cb6 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61 62  eLabel(v, endLab
21cb7 65 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  el);.      break
21cb8 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
21cb9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
21cba 47 45 52 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  GER.    case TK_
21cbb 52 41 49 53 45 3a 20 7b 0a 20 20 20 20 20 20 61  RAISE: {.      a
21cbc 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 61 66  ssert( pExpr->af
21cbd 66 69 6e 69 74 79 3d 3d 4f 45 5f 52 6f 6c 6c 62  finity==OE_Rollb
21cbe 61 63 6b 20 0a 20 20 20 20 20 20 20 20 20 20 20  ack .           
21cbf 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69  || pExpr->affini
21cc0 74 79 3d 3d 4f 45 5f 41 62 6f 72 74 0a 20 20 20  ty==OE_Abort.   
21cc1 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70 72          || pExpr
21cc2 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 46  ->affinity==OE_F
21cc3 61 69 6c 0a 20 20 20 20 20 20 20 20 20 20 20 7c  ail.           |
21cc4 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74  | pExpr->affinit
21cc5 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 0a 20 20 20  y==OE_Ignore.   
21cc6 20 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20     );.      if( 
21cc7 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65  !pParse->pTrigge
21cc8 72 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20  rTab ){.        
21cc9 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
21cca 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
21ccb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
21ccc 52 41 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c 79  RAISE() may only
21ccd 20 62 65 20 75 73 65 64 20 77 69 74 68 69 6e 20   be used within 
21cce 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61  a trigger-progra
21ccf 6d 22 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  m");.        ret
21cd0 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
21cd1 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
21cd2 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 41 62 6f  affinity==OE_Abo
21cd3 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  rt ){.        sq
21cd4 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50  lite3MayAbort(pP
21cd5 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  arse);.      }. 
21cd6 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
21cd7 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
21cd8 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
21cd9 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ) );.      if( p
21cda 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d  Expr->affinity==
21cdb 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20  OE_Ignore ){.   
21cdc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21cdd 41 64 64 4f 70 34 28 0a 20 20 20 20 20 20 20 20  AddOp4(.        
21cde 20 20 20 20 76 2c 20 4f 50 5f 48 61 6c 74 2c 20      v, OP_Halt, 
21cdf 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f 49 67  SQLITE_OK, OE_Ig
21ce0 6e 6f 72 65 2c 20 30 2c 20 70 45 78 70 72 2d 3e  nore, 0, pExpr->
21ce1 75 2e 7a 54 6f 6b 65 6e 2c 30 29 3b 0a 20 20 20  u.zToken,0);.   
21ce2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21ce3 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e    sqlite3HaltCon
21ce4 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c 20  straint(pParse, 
21ce5 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 2c  pExpr->affinity,
21ce6 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
21ce7 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  , 0);.      }.. 
21ce8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21ce9 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73  }.#endif.  }.  s
21cea 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
21ceb 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
21cec 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65  Free1);.  sqlite
21ced 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
21cee 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32  pParse, regFree2
21cef 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65  );.  return inRe
21cf0 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  g;.}../*.** Gene
21cf1 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61  rate code to eva
21cf2 6c 75 61 74 65 20 61 6e 20 65 78 70 72 65 73 73  luate an express
21cf3 69 6f 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68  ion and store th
21cf4 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74  e results.** int
21cf5 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 52  o a register.  R
21cf6 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74  eturn the regist
21cf7 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20  er number where 
21cf8 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 61  the results.** a
21cf9 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a  re stored..**.**
21cfa 20 49 66 20 74 68 65 20 72 65 67 69 73 74 65 72   If the register
21cfb 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20   is a temporary 
21cfc 72 65 67 69 73 74 65 72 20 74 68 61 74 20 63 61  register that ca
21cfd 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64  n be deallocated
21cfe 2c 0a 2a 2a 20 74 68 65 6e 20 77 72 69 74 65 20  ,.** then write 
21cff 69 74 73 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20  its number into 
21d00 2a 70 52 65 67 2e 20 20 49 66 20 74 68 65 20 72  *pReg.  If the r
21d01 65 73 75 6c 74 20 72 65 67 69 73 74 65 72 20 69  esult register i
21d02 73 20 6e 6f 74 0a 2a 2a 20 61 20 74 65 6d 70 6f  s not.** a tempo
21d03 72 61 72 79 2c 20 74 68 65 6e 20 73 65 74 20 2a  rary, then set *
21d04 70 52 65 67 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f  pReg to zero..*/
21d05 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21d06 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
21d07 6f 64 65 54 65 6d 70 28 50 61 72 73 65 20 2a 70  odeTemp(Parse *p
21d08 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
21d09 70 72 2c 20 69 6e 74 20 2a 70 52 65 67 29 7b 0a  pr, int *pReg){.
21d0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
21d0b 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
21d0c 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 32 20 3d  rse);.  int r2 =
21d0d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21d0e 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
21d0f 45 78 70 72 2c 20 72 31 29 3b 0a 20 20 69 66 28  Expr, r1);.  if(
21d10 20 72 32 3d 3d 72 31 20 29 7b 0a 20 20 20 20 2a   r2==r1 ){.    *
21d11 70 52 65 67 20 3d 20 72 31 3b 0a 20 20 7d 65 6c  pReg = r1;.  }el
21d12 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  se{.    sqlite3R
21d13 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
21d14 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 2a  arse, r1);.    *
21d15 70 52 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pReg = 0;.  }.  
21d16 72 65 74 75 72 6e 20 72 32 3b 0a 7d 0a 0a 2f 2a  return r2;.}../*
21d17 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
21d18 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c  e that will eval
21d19 75 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 20  uate expression 
21d1a 70 45 78 70 72 20 61 6e 64 20 73 74 6f 72 65 20  pExpr and store 
21d1b 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69  the.** results i
21d1c 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65  n register targe
21d1d 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20  t.  The results 
21d1e 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74  are guaranteed t
21d1f 6f 20 61 70 70 65 61 72 0a 2a 2a 20 69 6e 20 72  o appear.** in r
21d20 65 67 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a  egister target..
21d21 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21d22 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
21d23 72 43 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61  rCode(Parse *pPa
21d24 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
21d25 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20  , int target){. 
21d26 20 69 6e 74 20 69 6e 52 65 67 3b 0a 0a 20 20 61   int inReg;..  a
21d27 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20  ssert( target>0 
21d28 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 72 73  && target<=pPars
21d29 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 6e 52  e->nMem );.  inR
21d2a 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  eg = sqlite3Expr
21d2b 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73  CodeTarget(pPars
21d2c 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74  e, pExpr, target
21d2d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
21d2e 72 73 65 2d 3e 70 56 64 62 65 20 7c 7c 20 70 50  rse->pVdbe || pP
21d2f 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
21d30 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
21d31 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20 26 26  inReg!=target &&
21d32 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 29   pParse->pVdbe )
21d33 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
21d34 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e  eAddOp2(pParse->
21d35 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f 70 79 2c  pVdbe, OP_SCopy,
21d36 20 69 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b   inReg, target);
21d37 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 74 61  .  }.  return ta
21d38 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  rget;.}../*.** G
21d39 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
21d3a 74 20 65 76 61 6c 75 74 65 73 20 74 68 65 20 67  t evalutes the g
21d3b 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  iven expression 
21d3c 61 6e 64 20 70 75 74 73 20 74 68 65 20 72 65 73  and puts the res
21d3d 75 6c 74 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74  ult.** in regist
21d3e 65 72 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a  er target..**.**
21d3f 20 41 6c 73 6f 20 6d 61 6b 65 20 61 20 63 6f 70   Also make a cop
21d40 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  y of the express
21d41 69 6f 6e 20 72 65 73 75 6c 74 73 20 69 6e 74 6f  ion results into
21d42 20 61 6e 6f 74 68 65 72 20 22 63 61 63 68 65 22   another "cache"
21d43 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64   register.** and
21d44 20 6d 6f 64 69 66 79 20 74 68 65 20 65 78 70 72   modify the expr
21d45 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74 20 74  ession so that t
21d46 68 65 20 6e 65 78 74 20 74 69 6d 65 20 69 74 20  he next time it 
21d47 69 73 20 65 76 61 6c 75 61 74 65 64 2c 0a 2a 2a  is evaluated,.**
21d48 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
21d49 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 61 63   copy of the cac
21d4a 68 65 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a  he register..**.
21d4b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
21d4c 69 73 20 75 73 65 64 20 66 6f 72 20 65 78 70 72  is used for expr
21d4d 65 73 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65  essions that are
21d4e 20 75 73 65 64 20 6d 75 6c 74 69 70 6c 65 20 0a   used multiple .
21d4f 2a 2a 20 74 69 6d 65 73 2e 20 20 54 68 65 79 20  ** times.  They 
21d50 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e  are evaluated on
21d51 63 65 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c  ce and the resul
21d52 74 73 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ts of the expres
21d53 73 69 6f 6e 0a 2a 2a 20 61 72 65 20 72 65 75 73  sion.** are reus
21d54 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
21d55 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
21d56 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68  3ExprCodeAndCach
21d57 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
21d58 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e   Expr *pExpr, in
21d59 74 20 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62  t target){.  Vdb
21d5a 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
21d5b 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 6e 52 65  Vdbe;.  int inRe
21d5c 67 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 6c  g;.  inReg = sql
21d5d 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
21d5e 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67  rse, pExpr, targ
21d5f 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74  et);.  assert( t
21d60 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 2f 2a 20  arget>0 );.  /* 
21d61 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
21d62 63 61 6c 6c 65 64 20 66 6f 72 20 74 65 72 6d 73  called for terms
21d63 20 74 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50   to INSERT or UP
21d64 44 41 54 45 2e 20 20 41 6e 64 20 74 68 65 20 6f  DATE.  And the o
21d65 6e 6c 79 0a 20 20 2a 2a 20 6f 74 68 65 72 20 70  nly.  ** other p
21d66 6c 61 63 65 20 77 68 65 72 65 20 65 78 70 72 65  lace where expre
21d67 73 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 63 6f  ssions can be co
21d68 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 54 4b 5f  nverted into TK_
21d69 52 45 47 49 53 54 45 52 20 69 73 0a 20 20 2a 2a  REGISTER is.  **
21d6a 20 69 6e 20 57 48 45 52 45 20 63 6c 61 75 73 65   in WHERE clause
21d6b 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 53 6f   processing.  So
21d6c 20 61 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6d   as currently im
21d6d 70 6c 65 6d 65 6e 74 65 64 2c 20 74 68 65 72 65  plemented, there
21d6e 20 69 73 0a 20 20 2a 2a 20 6e 6f 20 77 61 79 20   is.  ** no way 
21d6f 66 6f 72 20 61 20 54 4b 5f 52 45 47 49 53 54 45  for a TK_REGISTE
21d70 52 20 74 6f 20 65 78 69 73 74 20 68 65 72 65 2e  R to exist here.
21d71 20 20 42 75 74 20 69 74 20 73 65 65 6d 73 20 70    But it seems p
21d72 72 75 64 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 6b  rudent to.  ** k
21d73 65 65 70 20 74 68 65 20 41 4c 57 41 59 53 28 29  eep the ALWAYS()
21d74 20 69 6e 20 63 61 73 65 20 74 68 65 20 63 6f 6e   in case the con
21d75 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 63 68  ditions above ch
21d76 61 6e 67 65 20 77 69 74 68 20 66 75 74 75 72 65  ange with future
21d77 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69  .  ** modificati
21d78 6f 6e 73 20 6f 72 20 65 6e 68 61 6e 63 65 6d 65  ons or enhanceme
21d79 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 28 20 41 4c  nts. */.  if( AL
21d7a 57 41 59 53 28 70 45 78 70 72 2d 3e 6f 70 21 3d  WAYS(pExpr->op!=
21d7b 54 4b 5f 52 45 47 49 53 54 45 52 29 20 29 7b 20  TK_REGISTER) ){ 
21d7c 20 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 0a   .    int iMem;.
21d7d 20 20 20 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61      iMem = ++pPa
21d7e 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73  rse->nMem;.    s
21d7f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21d80 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 6e 52  (v, OP_Copy, inR
21d81 65 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 70  eg, iMem);.    p
21d82 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69  Expr->iTable = i
21d83 4d 65 6d 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e  Mem;.    pExpr->
21d84 6f 70 32 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b  op2 = pExpr->op;
21d85 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d  .    pExpr->op =
21d86 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20   TK_REGISTER;.  
21d87 7d 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65 67  }.  return inReg
21d88 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
21d89 6e 20 54 52 55 45 20 69 66 20 70 45 78 70 72 20  n TRUE if pExpr 
21d8a 69 73 20 61 6e 20 63 6f 6e 73 74 61 6e 74 20 65  is an constant e
21d8b 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69  xpression that i
21d8c 73 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a  s appropriate.**
21d8d 20 66 6f 72 20 66 61 63 74 6f 72 69 6e 67 20 6f   for factoring o
21d8e 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 20 20 41  ut of a loop.  A
21d8f 70 70 72 6f 70 72 69 61 74 65 20 65 78 70 72 65  ppropriate expre
21d90 73 73 69 6f 6e 73 20 61 72 65 3a 0a 2a 2a 0a 2a  ssions are:.**.*
21d91 2a 20 20 20 20 2a 20 20 41 6e 79 20 65 78 70 72  *    *  Any expr
21d92 65 73 73 69 6f 6e 20 74 68 61 74 20 65 76 61 6c  ession that eval
21d93 75 61 74 65 73 20 74 6f 20 74 77 6f 20 6f 72 20  uates to two or 
21d94 6d 6f 72 65 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a  more opcodes..**
21d95 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 4f 50  .**    *  Any OP
21d96 5f 49 6e 74 65 67 65 72 2c 20 4f 50 5f 52 65 61  _Integer, OP_Rea
21d97 6c 2c 20 4f 50 5f 53 74 72 69 6e 67 2c 20 4f 50  l, OP_String, OP
21d98 5f 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  _Blob, OP_Null, 
21d99 0a 2a 2a 20 20 20 20 20 20 20 6f 72 20 4f 50 5f  .**       or OP_
21d9a 56 61 72 69 61 62 6c 65 20 74 68 61 74 20 64 6f  Variable that do
21d9b 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
21d9c 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 0a 2a  e placed in a .*
21d9d 2a 20 20 20 20 20 20 20 73 70 65 63 69 66 69 63  *       specific
21d9e 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a 2a   register..**.**
21d9f 20 54 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69   There is no poi
21da0 6e 74 20 69 6e 20 66 61 63 74 6f 72 69 6e 67 20  nt in factoring 
21da1 6f 75 74 20 73 69 6e 67 6c 65 2d 69 6e 73 74 72  out single-instr
21da2 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 0a  uction constant.
21da3 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74  ** expressions t
21da4 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 70  hat need to be p
21da5 6c 61 63 65 64 20 69 6e 20 61 20 70 61 72 74 69  laced in a parti
21da6 63 75 6c 61 72 20 72 65 67 69 73 74 65 72 2e 20  cular register. 
21da7 20 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 20 66 61   .** We could fa
21da8 63 74 6f 72 20 74 68 65 6d 20 6f 75 74 2c 20 62  ctor them out, b
21da9 75 74 20 74 68 65 6e 20 77 65 20 77 6f 75 6c 64  ut then we would
21daa 20 65 6e 64 20 75 70 20 61 64 64 69 6e 67 20 61   end up adding a
21dab 6e 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79 20 69 6e  n.** OP_SCopy in
21dac 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6d 6f 76  struction to mov
21dad 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 74 6f  e the value into
21dae 20 74 68 65 20 63 6f 72 72 65 63 74 20 72 65 67   the correct reg
21daf 69 73 74 65 72 0a 2a 2a 20 6c 61 74 65 72 2e 20  ister.** later. 
21db0 20 57 65 20 6d 69 67 68 74 20 61 73 20 77 65 6c   We might as wel
21db1 6c 20 6a 75 73 74 20 75 73 65 20 74 68 65 20 6f  l just use the o
21db2 72 69 67 69 6e 61 6c 20 69 6e 73 74 72 75 63 74  riginal instruct
21db3 69 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76 6f 69 64  ion and.** avoid
21db4 20 74 68 65 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a   the OP_SCopy..*
21db5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 41  /.static int isA
21db6 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46 61 63  ppropriateForFac
21db7 74 6f 72 69 6e 67 28 45 78 70 72 20 2a 70 29 7b  toring(Expr *p){
21db8 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45  .  if( !sqlite3E
21db9 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74  xprIsConstantNot
21dba 4a 6f 69 6e 28 70 29 20 29 7b 0a 20 20 20 20 72  Join(p) ){.    r
21dbb 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4f 6e 6c  eturn 0;  /* Onl
21dbc 79 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 65  y constant expre
21dbd 73 73 69 6f 6e 73 20 61 72 65 20 61 70 70 72 6f  ssions are appro
21dbe 70 72 69 61 74 65 20 66 6f 72 20 66 61 63 74 6f  priate for facto
21dbf 72 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  ring */.  }.  if
21dc0 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 45 50  ( (p->flags & EP
21dc1 5f 46 69 78 65 64 44 65 73 74 29 3d 3d 30 20 29  _FixedDest)==0 )
21dc2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20  {.    return 1; 
21dc3 20 2f 2a 20 41 6e 79 20 63 6f 6e 73 74 61 6e 74   /* Any constant
21dc4 20 77 69 74 68 6f 75 74 20 61 20 66 69 78 65 64   without a fixed
21dc5 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20   destination is 
21dc6 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 20  appropriate */. 
21dc7 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f   }.  while( p->o
21dc8 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70 20  p==TK_UPLUS ) p 
21dc9 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 73 77  = p->pLeft;.  sw
21dca 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 23  itch( p->op ){.#
21dcb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21dcc 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a  IT_BLOB_LITERAL.
21dcd 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42      case TK_BLOB
21dce 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73  :.#endif.    cas
21dcf 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 0a 20  e TK_VARIABLE:. 
21dd0 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47     case TK_INTEG
21dd1 45 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ER:.    case TK_
21dd2 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61 73 65 20  FLOAT:.    case 
21dd3 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73  TK_NULL:.    cas
21dd4 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20  e TK_STRING: {. 
21dd5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
21dd6 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 3b  ->op==TK_BLOB );
21dd7 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21dd8 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 52 49 41   p->op==TK_VARIA
21dd9 42 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73  BLE );.      tes
21dda 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b  tcase( p->op==TK
21ddb 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20 20  _INTEGER );.    
21ddc 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f    testcase( p->o
21ddd 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 3b 0a 20  p==TK_FLOAT );. 
21dde 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
21ddf 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 3b  ->op==TK_NULL );
21de0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21de1 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e   p->op==TK_STRIN
21de2 47 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 53 69  G );.      /* Si
21de3 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e  ngle-instruction
21de4 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 20   constants with 
21de5 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74  a fixed destinat
21de6 69 6f 6e 20 61 72 65 0a 20 20 20 20 20 20 2a 2a  ion are.      **
21de7 20 62 65 74 74 65 72 20 64 6f 6e 65 20 69 6e 2d   better done in-
21de8 6c 69 6e 65 2e 20 20 49 66 20 77 65 20 66 61 63  line.  If we fac
21de9 74 6f 72 20 74 68 65 6d 2c 20 74 68 65 79 20 77  tor them, they w
21dea 69 6c 6c 20 6a 75 73 74 20 65 6e 64 0a 20 20 20  ill just end.   
21deb 20 20 20 2a 2a 20 75 70 20 67 65 6e 65 72 61 74     ** up generat
21dec 69 6e 67 20 61 6e 20 4f 50 5f 53 43 6f 70 79 20  ing an OP_SCopy 
21ded 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75  to move the valu
21dee 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61  e to the destina
21def 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 72 65  tion.      ** re
21df0 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20 20 20 20  gister. */.     
21df1 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
21df2 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d 49  .    case TK_UMI
21df3 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  NUS: {.      if(
21df4 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54   p->pLeft->op==T
21df5 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 2d 3e 70 4c  K_FLOAT || p->pL
21df6 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45  eft->op==TK_INTE
21df7 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20 72  GER ){.        r
21df8 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
21df9 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21dfa 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
21dfb 20 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   {.      break;.
21dfc 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
21dfd 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  rn 1;.}../*.** I
21dfe 66 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6e  f pExpr is a con
21dff 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e  stant expression
21e00 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72   that is appropr
21e01 69 61 74 65 20 66 6f 72 0a 2a 2a 20 66 61 63 74  iate for.** fact
21e02 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c  oring out of a l
21e03 6f 6f 70 2c 20 74 68 65 6e 20 65 76 61 6c 75 61  oop, then evalua
21e04 74 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  te the expressio
21e05 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67 69  n.** into a regi
21e06 73 74 65 72 20 61 6e 64 20 63 6f 6e 76 65 72 74  ster and convert
21e07 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
21e08 69 6e 74 6f 20 61 20 54 4b 5f 52 45 47 49 53 54  into a TK_REGIST
21e09 45 52 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  ER.** expression
21e0a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
21e0b 65 76 61 6c 43 6f 6e 73 74 45 78 70 72 28 57 61  evalConstExpr(Wa
21e0c 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45  lker *pWalker, E
21e0d 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 50  xpr *pExpr){.  P
21e0e 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70  arse *pParse = p
21e0f 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a  Walker->pParse;.
21e10 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
21e11 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20  >op ){.    case 
21e12 54 4b 5f 49 4e 3a 0a 20 20 20 20 63 61 73 65 20  TK_IN:.    case 
21e13 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b 0a 20  TK_REGISTER: {. 
21e14 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
21e15 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20  Prune;.    }.   
21e16 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f   case TK_FUNCTIO
21e17 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  N:.    case TK_A
21e18 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20  GG_FUNCTION:.   
21e19 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46   case TK_CONST_F
21e1a 55 4e 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  UNC: {.      /* 
21e1b 54 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f  The arguments to
21e1c 20 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 76 65   a function have
21e1d 20 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61   a fixed destina
21e1e 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 4d  tion..      ** M
21e1f 61 72 6b 20 74 68 65 6d 20 74 68 69 73 20 77 61  ark them this wa
21e20 79 20 74 6f 20 61 76 6f 69 64 20 67 65 6e 65 72  y to avoid gener
21e21 61 74 65 64 20 75 6e 6e 65 65 64 65 64 20 4f 50  ated unneeded OP
21e22 5f 53 43 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20  _SCopy.      ** 
21e23 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 0a 20  instructions. . 
21e24 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78       */.      Ex
21e25 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  prList *pList = 
21e26 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a  pExpr->x.pList;.
21e27 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
21e28 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
21e29 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
21e2a 63 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ct) );.      if(
21e2b 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20   pList ){.      
21e2c 20 20 69 6e 74 20 69 20 3d 20 70 4c 69 73 74 2d    int i = pList-
21e2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  >nExpr;.        
21e2e 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
21e2f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 4c  item *pItem = pL
21e30 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20  ist->a;.        
21e31 66 6f 72 28 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20  for(; i>0; i--, 
21e32 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
21e33 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70      if( ALWAYS(p
21e34 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20 70  Item->pExpr) ) p
21e35 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 66 6c 61  Item->pExpr->fla
21e36 67 73 20 7c 3d 20 45 50 5f 46 69 78 65 64 44 65  gs |= EP_FixedDe
21e37 73 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  st;.        }.  
21e38 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
21e39 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  k;.    }.  }.  i
21e3a 66 28 20 69 73 41 70 70 72 6f 70 72 69 61 74 65  f( isAppropriate
21e3b 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 70 45 78  ForFactoring(pEx
21e3c 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  pr) ){.    int r
21e3d 31 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  1 = ++pParse->nM
21e3e 65 6d 3b 0a 20 20 20 20 69 6e 74 20 72 32 3b 0a  em;.    int r2;.
21e3f 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33      r2 = sqlite3
21e40 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
21e41 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72 31  Parse, pExpr, r1
21e42 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52  );.    if( NEVER
21e43 28 72 31 21 3d 72 32 29 20 29 20 73 71 6c 69 74  (r1!=r2) ) sqlit
21e44 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
21e45 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
21e46 20 20 70 45 78 70 72 2d 3e 6f 70 32 20 3d 20 70    pExpr->op2 = p
21e47 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 20 20 70 45  Expr->op;.    pE
21e48 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47  xpr->op = TK_REG
21e49 49 53 54 45 52 3b 0a 20 20 20 20 70 45 78 70 72  ISTER;.    pExpr
21e4a 2d 3e 69 54 61 62 6c 65 20 3d 20 72 32 3b 0a 20  ->iTable = r2;. 
21e4b 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
21e4c 75 6e 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  une;.  }.  retur
21e4d 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  n WRC_Continue;.
21e4e 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 65 76 61 6c  }../*.** Preeval
21e4f 75 61 74 65 20 63 6f 6e 73 74 61 6e 74 20 73 75  uate constant su
21e50 62 65 78 70 72 65 73 73 69 6f 6e 73 20 77 69 74  bexpressions wit
21e51 68 69 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74  hin pExpr and st
21e52 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c  ore the.** resul
21e53 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 2e  ts in registers.
21e54 20 20 4d 6f 64 69 66 79 20 70 45 78 70 72 20 73    Modify pExpr s
21e55 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6e 73 74  o that the const
21e56 61 6e 74 20 73 75 62 65 78 70 72 65 73 69 6f 6e  ant subexpresion
21e57 73 0a 2a 2a 20 61 72 65 20 54 4b 5f 52 45 47 49  s.** are TK_REGI
21e58 53 54 45 52 20 6f 70 63 6f 64 65 73 20 74 68 61  STER opcodes tha
21e59 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70  t refer to the p
21e5a 72 65 63 6f 6d 70 75 74 65 64 20 76 61 6c 75 65  recomputed value
21e5b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
21e5c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
21e5d 33 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e  3ExprCodeConstan
21e5e 74 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ts(Parse *pParse
21e5f 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  , Expr *pExpr){.
21e60 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e    Walker w;.  w.
21e61 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20  xExprCallback = 
21e62 65 76 61 6c 43 6f 6e 73 74 45 78 70 72 3b 0a 20  evalConstExpr;. 
21e63 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
21e64 63 6b 20 3d 20 30 3b 0a 20 20 77 2e 70 50 61 72  ck = 0;.  w.pPar
21e65 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73  se = pParse;.  s
21e66 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26  qlite3WalkExpr(&
21e67 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 0a 2f  w, pExpr);.}.../
21e68 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
21e69 64 65 20 74 68 61 74 20 70 75 73 68 65 73 20 74  de that pushes t
21e6a 68 65 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72  he value of ever
21e6b 79 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  y element of the
21e6c 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73   given.** expres
21e6d 73 69 6f 6e 20 6c 69 73 74 20 69 6e 74 6f 20 61  sion list into a
21e6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67   sequence of reg
21e6f 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67  isters beginning
21e70 20 61 74 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a   at target..**.*
21e71 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
21e72 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
21e73 65 76 61 6c 75 61 74 65 64 2e 0a 2a 2f 0a 53 51  evaluated..*/.SQ
21e74 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
21e75 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21e76 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73  ExprList(.  Pars
21e77 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
21e78 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
21e79 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
21e7a 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65  *pList,   /* The
21e7b 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
21e7c 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a   to be coded */.
21e7d 20 20 69 6e 74 20 74 61 72 67 65 74 2c 20 20 20    int target,   
21e7e 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f       /* Where to
21e7f 20 77 72 69 74 65 20 72 65 73 75 6c 74 73 20 2a   write results *
21e80 2f 0a 20 20 69 6e 74 20 64 6f 48 61 72 64 43 6f  /.  int doHardCo
21e81 70 79 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 61  py     /* Make a
21e82 20 68 61 72 64 20 63 6f 70 79 20 6f 66 20 65 76   hard copy of ev
21e83 65 72 79 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29  ery element */.)
21e84 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  {.  struct ExprL
21e85 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
21e86 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 61  .  int i, n;.  a
21e87 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20  ssert( pList!=0 
21e88 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 61 72  );.  assert( tar
21e89 67 65 74 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 70  get>0 );.  n = p
21e8a 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 66  List->nExpr;.  f
21e8b 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  or(pItem=pList->
21e8c 61 2c 20 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b  a, i=0; i<n; i++
21e8d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
21e8e 69 66 28 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61  if( pItem->iAlia
21e8f 73 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  s ){.      int i
21e90 52 65 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28  Reg = codeAlias(
21e91 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 69  pParse, pItem->i
21e92 41 6c 69 61 73 2c 20 70 49 74 65 6d 2d 3e 70 45  Alias, pItem->pE
21e93 78 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a  xpr, target+i);.
21e94 20 20 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20        Vdbe *v = 
21e95 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
21e96 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 66  Parse);.      if
21e97 28 20 69 52 65 67 21 3d 74 61 72 67 65 74 2b 69  ( iReg!=target+i
21e98 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
21e99 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21e9a 20 4f 50 5f 53 43 6f 70 79 2c 20 69 52 65 67 2c   OP_SCopy, iReg,
21e9b 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20   target+i);.    
21e9c 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
21e9d 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21e9e 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 49 74  Code(pParse, pIt
21e9f 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65  em->pExpr, targe
21ea0 74 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  t+i);.    }.    
21ea1 69 66 28 20 64 6f 48 61 72 64 43 6f 70 79 20 26  if( doHardCopy &
21ea2 26 20 21 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  & !pParse->db->m
21ea3 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
21ea4 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21ea5 48 61 72 64 43 6f 70 79 28 70 50 61 72 73 65 2c  HardCopy(pParse,
21ea6 20 74 61 72 67 65 74 2c 20 6e 29 3b 0a 20 20 20   target, n);.   
21ea7 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
21ea8 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  n;.}../*.** Gene
21ea9 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20  rate code for a 
21eaa 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69  boolean expressi
21eab 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 20 6a  on such that a j
21eac 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74  ump is made.** t
21ead 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65 73  o the label "des
21eae 74 22 20 69 66 20 74 68 65 20 65 78 70 72 65 73  t" if the expres
21eaf 73 69 6f 6e 20 69 73 20 74 72 75 65 20 62 75 74  sion is true but
21eb0 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f   execution.** co
21eb1 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 68 74  ntinues straight
21eb2 20 74 68 72 75 20 69 66 20 74 68 65 20 65 78 70   thru if the exp
21eb3 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65  ression is false
21eb4 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65  ..**.** If the e
21eb5 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 61  xpression evalua
21eb6 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69  tes to NULL (nei
21eb7 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66 61  ther true nor fa
21eb8 6c 73 65 29 2c 20 74 68 65 6e 0a 2a 2a 20 74 61  lse), then.** ta
21eb9 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 74  ke the jump if t
21eba 68 65 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 66 6c  he jumpIfNull fl
21ebb 61 67 20 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d  ag is SQLITE_JUM
21ebc 50 49 46 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54  PIFNULL..**.** T
21ebd 68 69 73 20 63 6f 64 65 20 64 65 70 65 6e 64 73  his code depends
21ebe 20 6f 6e 20 74 68 65 20 66 61 63 74 20 74 68 61   on the fact tha
21ebf 74 20 63 65 72 74 61 69 6e 20 74 6f 6b 65 6e 20  t certain token 
21ec0 76 61 6c 75 65 73 20 28 65 78 3a 20 54 4b 5f 45  values (ex: TK_E
21ec1 51 29 0a 2a 2a 20 61 72 65 20 74 68 65 20 73 61  Q).** are the sa
21ec2 6d 65 20 61 73 20 6f 70 63 6f 64 65 20 76 61 6c  me as opcode val
21ec3 75 65 73 20 28 65 78 3a 20 4f 50 5f 45 71 29 20  ues (ex: OP_Eq) 
21ec4 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74  that implement t
21ec5 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
21ec6 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  .** operation.  
21ec7 53 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e 74 73  Special comments
21ec8 20 69 6e 20 76 64 62 65 2e 63 20 61 6e 64 20 74   in vdbe.c and t
21ec9 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b  he mkopcodeh.awk
21eca 20 73 63 72 69 70 74 20 69 6e 0a 2a 2a 20 74 68   script in.** th
21ecb 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73 20 63  e make process c
21ecc 61 75 73 65 20 74 68 65 73 65 20 76 61 6c 75 65  ause these value
21ecd 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41 73 73  s to align.  Ass
21ece 65 72 74 28 29 73 20 69 6e 20 74 68 65 20 63 6f  ert()s in the co
21ecf 64 65 0a 2a 2a 20 62 65 6c 6f 77 20 76 65 72 69  de.** below veri
21ed0 66 79 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62  fy that the numb
21ed1 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65 64 20  ers are aligned 
21ed2 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a 53 51  correctly..*/.SQ
21ed3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
21ed4 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54  d sqlite3ExprIfT
21ed5 72 75 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  rue(Parse *pPars
21ed6 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  e, Expr *pExpr, 
21ed7 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 6a 75  int dest, int ju
21ed8 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62  mpIfNull){.  Vdb
21ed9 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
21eda 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d  Vdbe;.  int op =
21edb 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65   0;.  int regFre
21edc 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65  e1 = 0;.  int re
21edd 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e  gFree2 = 0;.  in
21ede 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73  t r1, r2;..  ass
21edf 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ert( jumpIfNull=
21ee0 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55  =SQLITE_JUMPIFNU
21ee1 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c  LL || jumpIfNull
21ee2 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e 45 56  ==0 );.  if( NEV
21ee3 45 52 28 76 3d 3d 30 29 20 29 20 20 20 20 20 72  ER(v==0) )     r
21ee4 65 74 75 72 6e 3b 20 20 2f 2a 20 45 78 69 73 74  eturn;  /* Exist
21ee5 61 6e 63 65 20 6f 66 20 56 44 42 45 20 63 68 65  ance of VDBE che
21ee6 63 6b 65 64 20 62 79 20 63 61 6c 6c 65 72 20 2a  cked by caller *
21ee7 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 45  /.  if( NEVER(pE
21ee8 78 70 72 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  xpr==0) ) return
21ee9 3b 20 20 2f 2a 20 4e 6f 20 77 61 79 20 74 68 69  ;  /* No way thi
21eea 73 20 63 61 6e 20 68 61 70 70 65 6e 20 2a 2f 0a  s can happen */.
21eeb 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70    op = pExpr->op
21eec 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  ;.  switch( op )
21eed 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e  {.    case TK_AN
21eee 44 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64  D: {.      int d
21eef 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  2 = sqlite3VdbeM
21ef0 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
21ef1 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
21ef2 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
21ef3 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
21ef4 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29  achePush(pParse)
21ef5 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
21ef6 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
21ef7 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
21ef8 20 64 32 2c 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53   d2,jumpIfNull^S
21ef9 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
21efa 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21efb 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73  ExprIfTrue(pPars
21efc 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  e, pExpr->pRight
21efd 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
21efe 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ll);.      sqlit
21eff 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
21f00 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 20  el(v, d2);.     
21f01 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
21f02 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b  ePop(pParse, 1);
21f03 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21f04 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21f05 4f 52 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  OR: {.      test
21f06 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
21f07 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
21f08 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
21f09 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
21f0a 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49  eft, dest, jumpI
21f0b 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71  fNull);.      sq
21f0c 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
21f0d 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
21f0e 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d  Right, dest, jum
21f0f 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
21f10 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
21f11 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a   case TK_NOT: {.
21f12 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21f13 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
21f14 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
21f15 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
21f16 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
21f17 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
21f18 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21f19 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21f1a 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54  K_LT:.    case T
21f1b 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_LE:.    case T
21f1c 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54  K_GT:.    case T
21f1d 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_GE:.    case T
21f1e 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_NE:.    case T
21f1f 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73  K_EQ: {.      as
21f20 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f  sert( TK_LT==OP_
21f21 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Lt );.      asse
21f22 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65  rt( TK_LE==OP_Le
21f23 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21f24 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29  ( TK_GT==OP_Gt )
21f25 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21f26 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a  TK_GE==OP_Ge );.
21f27 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
21f28 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20  _EQ==OP_Eq );.  
21f29 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e      assert( TK_N
21f2a 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20  E==OP_Ne );.    
21f2b 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21f2c 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74  TK_LT );.      t
21f2d 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21f2e 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LE );.      test
21f2f 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20  case( op==TK_GT 
21f30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21f31 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a  e( op==TK_GE );.
21f32 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21f33 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20  op==TK_EQ );.   
21f34 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21f35 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20  =TK_NE );.      
21f36 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
21f37 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Null==0 );.     
21f38 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72   codeCompareOper
21f39 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78  ands(pParse, pEx
21f3a 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20  pr->pLeft, &r1, 
21f3b 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20  &regFree1,.     
21f3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f3d 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
21f3e 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c  pr->pRight, &r2,
21f3f 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20   &regFree2);.   
21f40 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70     codeCompare(p
21f41 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
21f42 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  eft, pExpr->pRig
21f43 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20  ht, op,.        
21f44 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32            r1, r2
21f45 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
21f46 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  ll);.      testc
21f47 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
21f48 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21f49 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20  se( regFree2==0 
21f4a 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21f4b 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21f4c 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73 65 20 54  K_IS:.    case T
21f4d 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20 20 20 20  K_ISNOT: {.     
21f4e 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21f4f 4b 5f 49 53 20 29 3b 0a 20 20 20 20 20 20 74 65  K_IS );.      te
21f50 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49  stcase( op==TK_I
21f51 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 63 6f  SNOT );.      co
21f52 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64  deCompareOperand
21f53 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  s(pParse, pExpr-
21f54 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65  >pLeft, &r1, &re
21f55 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20  gFree1,.        
21f56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f57 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
21f58 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72  >pRight, &r2, &r
21f59 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
21f5a 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b 5f 49 53 29  op = (op==TK_IS)
21f5b 20 3f 20 54 4b 5f 45 51 20 3a 20 54 4b 5f 4e 45   ? TK_EQ : TK_NE
21f5c 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
21f5d 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  are(pParse, pExp
21f5e 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d  r->pLeft, pExpr-
21f5f 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20  >pRight, op,.   
21f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
21f61 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 53 51 4c  1, r2, dest, SQL
21f62 49 54 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20 20 20  ITE_NULLEQ);.   
21f63 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
21f64 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
21f65 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
21f66 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree2==0 );.     
21f67 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21f68 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c    case TK_ISNULL
21f69 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f  :.    case TK_NO
21f6a 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 61  TNULL: {.      a
21f6b 73 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c  ssert( TK_ISNULL
21f6c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20  ==OP_IsNull );. 
21f6d 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
21f6e 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e  NOTNULL==OP_NotN
21f6f 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ull );.      tes
21f70 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53  tcase( op==TK_IS
21f71 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65  NULL );.      te
21f72 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e  stcase( op==TK_N
21f73 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20  OTNULL );.      
21f74 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r1 = sqlite3Expr
21f75 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
21f76 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
21f77 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
21f78 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21f79 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64 65  p2(v, op, r1, de
21f7a 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  st);.      testc
21f7b 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
21f7c 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
21f7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
21f7e 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20  TK_BETWEEN: {.  
21f7f 20 20 20 20 2f 2a 20 20 20 20 78 20 42 45 54 57      /*    x BETW
21f80 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20  EEN y AND z.    
21f81 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 73    **.      ** Is
21f82 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a   equivalent to .
21f83 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
21f84 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c  *    x>=y AND x<
21f85 3d 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  =z.      **.    
21f86 20 20 2a 2a 20 43 6f 64 65 20 69 74 20 61 73 20    ** Code it as 
21f87 73 75 63 68 2c 20 74 61 6b 69 6e 67 20 63 61 72  such, taking car
21f88 65 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 6d  e to do the comm
21f89 6f 6e 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  on subexpression
21f8a 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e  .      ** elemen
21f8b 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a 20 20 20  tation of x..   
21f8c 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72     */.      Expr
21f8d 20 65 78 70 72 41 6e 64 3b 0a 20 20 20 20 20 20   exprAnd;.      
21f8e 45 78 70 72 20 63 6f 6d 70 4c 65 66 74 3b 0a 20  Expr compLeft;. 
21f8f 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70 52 69       Expr compRi
21f90 67 68 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20  ght;.      Expr 
21f91 65 78 70 72 58 3b 0a 0a 20 20 20 20 20 20 61 73  exprX;..      as
21f92 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
21f93 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
21f94 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20  _xIsSelect) );. 
21f95 20 20 20 20 20 65 78 70 72 58 20 3d 20 2a 70 45       exprX = *pE
21f96 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  xpr->pLeft;.    
21f97 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20 54    exprAnd.op = T
21f98 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65 78 70  K_AND;.      exp
21f99 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26 63 6f  rAnd.pLeft = &co
21f9a 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78  mpLeft;.      ex
21f9b 70 72 41 6e 64 2e 70 52 69 67 68 74 20 3d 20 26  prAnd.pRight = &
21f9c 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20  compRight;.     
21f9d 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d 20 54   compLeft.op = T
21f9e 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70  K_GE;.      comp
21f9f 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 65 78  Left.pLeft = &ex
21fa0 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c  prX;.      compL
21fa1 65 66 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78  eft.pRight = pEx
21fa2 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30  pr->x.pList->a[0
21fa3 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 63  ].pExpr;.      c
21fa4 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 54 4b  ompRight.op = TK
21fa5 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52  _LE;.      compR
21fa6 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 65 78  ight.pLeft = &ex
21fa7 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52  prX;.      compR
21fa8 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20 70 45  ight.pRight = pE
21fa9 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b  xpr->x.pList->a[
21faa 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  1].pExpr;.      
21fab 65 78 70 72 58 2e 69 54 61 62 6c 65 20 3d 20 73  exprX.iTable = s
21fac 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
21fad 6d 70 28 70 50 61 72 73 65 2c 20 26 65 78 70 72  mp(pParse, &expr
21fae 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  X, &regFree1);. 
21faf 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
21fb0 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
21fb1 20 20 20 20 65 78 70 72 58 2e 6f 70 20 3d 20 54      exprX.op = T
21fb2 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20  K_REGISTER;.    
21fb3 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
21fb4 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
21fb5 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
21fb6 54 72 75 65 28 70 50 61 72 73 65 2c 20 26 65 78  True(pParse, &ex
21fb7 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d  prAnd, dest, jum
21fb8 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
21fb9 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
21fba 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
21fbb 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78    r1 = sqlite3Ex
21fbc 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
21fbd 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46 72  e, pExpr, &regFr
21fbe 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ee1);.      sqli
21fbf 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
21fc0 20 4f 50 5f 49 66 2c 20 72 31 2c 20 64 65 73 74   OP_If, r1, dest
21fc1 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29  , jumpIfNull!=0)
21fc2 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21fc3 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
21fc4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21fc5 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
21fc6 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
21fc7 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
21fc8 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
21fc9 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65  (pParse, regFree
21fca 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  1);.  sqlite3Rel
21fcb 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
21fcc 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 20 20  se, regFree2);  
21fcd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
21fce 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f  te code for a bo
21fcf 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  olean expression
21fd0 20 73 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d   such that a jum
21fd1 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20  p is made.** to 
21fd2 74 68 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22  the label "dest"
21fd3 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
21fd4 6f 6e 20 69 73 20 66 61 6c 73 65 20 62 75 74 20  on is false but 
21fd5 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e  execution.** con
21fd6 74 69 6e 75 65 73 20 73 74 72 61 69 67 68 74 20  tinues straight 
21fd7 74 68 72 75 20 69 66 20 74 68 65 20 65 78 70 72  thru if the expr
21fd8 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 2e 0a  ession is true..
21fd9 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
21fda 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65  ression evaluate
21fdb 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68  s to NULL (neith
21fdc 65 72 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73  er true nor fals
21fdd 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20  e) then.** jump 
21fde 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 69 73  if jumpIfNull is
21fdf 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
21fe0 4c 4c 20 6f 72 20 66 61 6c 6c 20 74 68 72 6f 75  LL or fall throu
21fe1 67 68 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c  gh if jumpIfNull
21fe2 0a 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a 53 51 4c  .** is 0..*/.SQL
21fe3 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21fe4 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
21fe5 6c 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  lse(Parse *pPars
21fe6 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  e, Expr *pExpr, 
21fe7 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 6a 75  int dest, int ju
21fe8 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62  mpIfNull){.  Vdb
21fe9 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
21fea 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d  Vdbe;.  int op =
21feb 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65   0;.  int regFre
21fec 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65  e1 = 0;.  int re
21fed 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e  gFree2 = 0;.  in
21fee 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73  t r1, r2;..  ass
21fef 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ert( jumpIfNull=
21ff0 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55  =SQLITE_JUMPIFNU
21ff1 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c  LL || jumpIfNull
21ff2 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e 45 56  ==0 );.  if( NEV
21ff3 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72  ER(v==0) ) retur
21ff4 6e 3b 20 2f 2a 20 45 78 69 73 74 61 6e 63 65 20  n; /* Existance 
21ff5 6f 66 20 56 44 42 45 20 63 68 65 63 6b 65 64 20  of VDBE checked 
21ff6 62 79 20 63 61 6c 6c 65 72 20 2a 2f 0a 20 20 69  by caller */.  i
21ff7 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 20 20  f( pExpr==0 )   
21ff8 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 54   return;..  /* T
21ff9 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78 70  he value of pExp
21ffa 72 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61 72 65  r->op and op are
21ffb 20 72 65 6c 61 74 65 64 20 61 73 20 66 6f 6c 6c   related as foll
21ffc 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  ows:.  **.  **  
21ffd 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20       pExpr->op  
21ffe 20 20 20 20 20 20 20 20 20 20 6f 70 0a 20 20 2a            op.  *
21fff 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  *       --------
22000 2d 20 20 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d  -          -----
22001 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20 20 20  -----.  **      
22002 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20   TK_ISNULL      
22003 20 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 0a 20      OP_NotNull. 
22004 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e 4f 54   **       TK_NOT
22005 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 4f 50 5f  NULL         OP_
22006 49 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20  IsNull.  **     
22007 20 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20    TK_NE         
22008 20 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a 2a 20       OP_Eq.  ** 
22009 20 20 20 20 20 20 54 4b 5f 45 51 20 20 20 20 20        TK_EQ     
2200a 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 65 0a 20           OP_Ne. 
2200b 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47 54 20   **       TK_GT 
2200c 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f               OP_
2200d 4c 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b  Le.  **       TK
2200e 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20  _LE             
2200f 20 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20 20 20   OP_Gt.  **     
22010 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20    TK_GE         
22011 20 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a 2a 20       OP_Lt.  ** 
22012 20 20 20 20 20 20 54 4b 5f 4c 54 20 20 20 20 20        TK_LT     
22013 20 20 20 20 20 20 20 20 20 4f 50 5f 47 65 0a 20           OP_Ge. 
22014 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f 74 68   **.  ** For oth
22015 65 72 20 76 61 6c 75 65 73 20 6f 66 20 70 45 78  er values of pEx
22016 70 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20 75 6e  pr->op, op is un
22017 64 65 66 69 6e 65 64 20 61 6e 64 20 75 6e 75 73  defined and unus
22018 65 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76 61 6c  ed..  ** The val
22019 75 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f 50  ue of TK_ and OP
2201a 5f 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20  _ constants are 
2201b 61 72 72 61 6e 67 65 64 20 73 75 63 68 20 74 68  arranged such th
2201c 61 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e 20 63  at we.  ** can c
2201d 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 70 70 69  ompute the mappi
2201e 6e 67 20 61 62 6f 76 65 20 75 73 69 6e 67 20 74  ng above using t
2201f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70  he following exp
22020 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20 41 73  ression..  ** As
22021 73 65 72 74 28 29 73 20 76 65 72 69 66 79 20 74  sert()s verify t
22022 68 61 74 20 74 68 65 20 63 6f 6d 70 75 74 61 74  hat the computat
22023 69 6f 6e 20 69 73 20 63 6f 72 72 65 63 74 2e 0a  ion is correct..
22024 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28 70 45    */.  op = ((pE
22025 78 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53 4e 55  xpr->op+(TK_ISNU
22026 4c 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f 49 53  LL&1))^1)-(TK_IS
22027 4e 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a 20 56  NULL&1);..  /* V
22028 65 72 69 66 79 20 63 6f 72 72 65 63 74 20 61 6c  erify correct al
22029 69 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f 20 61  ignment of TK_ a
2202a 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73  nd OP_ constants
2202b 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
2202c 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 53  pExpr->op!=TK_IS
2202d 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e  NULL || op==OP_N
2202e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65  otNull );.  asse
2202f 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
22030 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f 70 3d  K_NOTNULL || op=
22031 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20  =OP_IsNull );.  
22032 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
22033 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 3d 3d  p!=TK_NE || op==
22034 4f 50 5f 45 71 20 29 3b 0a 20 20 61 73 73 65 72  OP_Eq );.  asser
22035 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  t( pExpr->op!=TK
22036 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 65  _EQ || op==OP_Ne
22037 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
22038 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 20 7c  xpr->op!=TK_LT |
22039 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20  | op==OP_Ge );. 
2203a 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
2203b 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f 70 3d  op!=TK_LE || op=
2203c 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73 73 65  =OP_Gt );.  asse
2203d 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
2203e 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c  K_GT || op==OP_L
2203f 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
22040 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 45 20  Expr->op!=TK_GE 
22041 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29 3b 0a  || op==OP_Lt );.
22042 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72  .  switch( pExpr
22043 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ->op ){.    case
22044 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20   TK_AND: {.     
22045 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
22046 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
22047 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
22048 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78  alse(pParse, pEx
22049 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c  pr->pLeft, dest,
2204a 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
2204b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
2204c 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
2204d 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65  Expr->pRight, de
2204e 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
2204f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
22050 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
22051 4f 52 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  OR: {.      int 
22052 64 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  d2 = sqlite3Vdbe
22053 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
22054 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
22055 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
22056 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
22057 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65  CachePush(pParse
22058 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
22059 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73  ExprIfTrue(pPars
2205a 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
2205b 20 64 32 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 5e   d2, jumpIfNull^
2205c 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
2205d 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  L);.      sqlite
2205e 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
2205f 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
22060 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  ht, dest, jumpIf
22061 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c  Null);.      sql
22062 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
22063 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20  abel(v, d2);.   
22064 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
22065 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31  chePop(pParse, 1
22066 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
22067 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
22068 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 73  K_NOT: {.      s
22069 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65  qlite3ExprIfTrue
2206a 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
2206b 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d  pLeft, dest, jum
2206c 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
2206d 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
2206e 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20   case TK_LT:.   
2206f 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20   case TK_LE:.   
22070 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20   case TK_GT:.   
22071 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20   case TK_GE:.   
22072 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20   case TK_NE:.   
22073 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20   case TK_EQ: {. 
22074 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
22075 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20  p==TK_LT );.    
22076 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
22077 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74  TK_LE );.      t
22078 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
22079 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  GT );.      test
2207a 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20  case( op==TK_GE 
2207b 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2207c 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a  e( op==TK_EQ );.
2207d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2207e 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20  op==TK_NE );.   
2207f 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
22080 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
22081 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f      codeCompareO
22082 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20  perands(pParse, 
22083 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
22084 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20  1, &regFree1,.  
22085 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22086 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22087 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26  pExpr->pRight, &
22088 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a  r2, &regFree2);.
22089 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
2208a 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
2208b 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70  >pLeft, pExpr->p
2208c 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20  Right, op,.     
2208d 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c               r1,
2208e 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49   r2, dest, jumpI
2208f 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65  fNull);.      te
22090 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
22091 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ==0 );.      tes
22092 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
22093 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
22094 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
22095 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73  e TK_IS:.    cas
22096 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20  e TK_ISNOT: {.  
22097 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
22098 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 20 29  xpr->op==TK_IS )
22099 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2209a 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2209b 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 63  ISNOT );.      c
2209c 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
2209d 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ds(pParse, pExpr
2209e 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72  ->pLeft, &r1, &r
2209f 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20  egFree1,.       
220a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
220a1 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
220a2 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26  ->pRight, &r2, &
220a3 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
220a4 20 6f 70 20 3d 20 28 70 45 78 70 72 2d 3e 6f 70   op = (pExpr->op
220a5 3d 3d 54 4b 5f 49 53 29 20 3f 20 54 4b 5f 4e 45  ==TK_IS) ? TK_NE
220a6 20 3a 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20 20   : TK_EQ;.      
220a7 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72  codeCompare(pPar
220a8 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
220a9 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
220aa 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20   op,.           
220ab 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 64         r1, r2, d
220ac 65 73 74 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  est, SQLITE_NULL
220ad 45 51 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  EQ);.      testc
220ae 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
220af 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
220b0 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20  se( regFree2==0 
220b1 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
220b2 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
220b3 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61  K_ISNULL:.    ca
220b4 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b  se TK_NOTNULL: {
220b5 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
220b6 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29   op==TK_ISNULL )
220b7 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
220b8 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c  ( op==TK_NOTNULL
220b9 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73   );.      r1 = s
220ba 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
220bb 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  mp(pParse, pExpr
220bc 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65  ->pLeft, &regFre
220bd 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  e1);.      sqlit
220be 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
220bf 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a 20  op, r1, dest);. 
220c0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
220c1 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
220c2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
220c3 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54  .    case TK_BET
220c4 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  WEEN: {.      /*
220c5 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79 20      x BETWEEN y 
220c6 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a 20  AND z.      **. 
220c7 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75 69 76       ** Is equiv
220c8 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 20  alent to .      
220c9 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 78  **.      **    x
220ca 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20  >=y AND x<=z.   
220cb 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 43     **.      ** C
220cc 6f 64 65 20 69 74 20 61 73 20 73 75 63 68 2c 20  ode it as such, 
220cd 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f 20 64  taking care to d
220ce 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 62  o the common sub
220cf 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 20  expression.     
220d0 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f 6e   ** elementation
220d1 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f 0a   of x..      */.
220d2 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 41        Expr exprA
220d3 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63  nd;.      Expr c
220d4 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 45  ompLeft;.      E
220d5 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a 20  xpr compRight;. 
220d6 20 20 20 20 20 45 78 70 72 20 65 78 70 72 58 3b       Expr exprX;
220d7 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
220d8 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
220d9 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
220da 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 65  lect) );.      e
220db 78 70 72 58 20 3d 20 2a 70 45 78 70 72 2d 3e 70  xprX = *pExpr->p
220dc 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72  Left;.      expr
220dd 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b  And.op = TK_AND;
220de 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70  .      exprAnd.p
220df 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65 66 74  Left = &compLeft
220e0 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e  ;.      exprAnd.
220e1 70 52 69 67 68 74 20 3d 20 26 63 6f 6d 70 52 69  pRight = &compRi
220e2 67 68 74 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c  ght;.      compL
220e3 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b 0a  eft.op = TK_GE;.
220e4 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70        compLeft.p
220e5 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b 0a 20  Left = &exprX;. 
220e6 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 52       compLeft.pR
220e7 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e  ight = pExpr->x.
220e8 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  pList->a[0].pExp
220e9 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67  r;.      compRig
220ea 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b 0a 20  ht.op = TK_LE;. 
220eb 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 70       compRight.p
220ec 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b 0a 20  Left = &exprX;. 
220ed 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 70       compRight.p
220ee 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78  Right = pExpr->x
220ef 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78  .pList->a[1].pEx
220f0 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72 58 2e  pr;.      exprX.
220f1 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  iTable = sqlite3
220f2 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
220f3 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26 72 65  rse, &exprX, &re
220f4 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74  gFree1);.      t
220f5 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
220f6 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 78  1==0 );.      ex
220f7 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49  prX.op = TK_REGI
220f8 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65 73 74  STER;.      test
220f9 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
220fa 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
220fb 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
220fc 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e 64  pParse, &exprAnd
220fd 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
220fe 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ll);.      break
220ff 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
22100 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 31 20  ult: {.      r1 
22101 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
22102 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45  eTemp(pParse, pE
22103 78 70 72 2c 20 26 72 65 67 46 72 65 65 31 29 3b  xpr, &regFree1);
22104 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22105 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
22106 66 4e 6f 74 2c 20 72 31 2c 20 64 65 73 74 2c 20  fNot, r1, dest, 
22107 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a  jumpIfNull!=0);.
22108 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
22109 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
2210a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
2210b 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
2210c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2210d 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
2210e 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2210f 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29  Parse, regFree1)
22110 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
22111 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
22112 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 7d 0a 0a  , regFree2);.}..
22113 2f 2a 0a 2a 2a 20 44 6f 20 61 20 64 65 65 70 20  /*.** Do a deep 
22114 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 77  comparison of tw
22115 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  o expression tre
22116 65 73 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45  es.  Return TRUE
22117 20 28 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a 20 69   (non-zero).** i
22118 66 20 74 68 65 79 20 61 72 65 20 69 64 65 6e 74  f they are ident
22119 69 63 61 6c 20 61 6e 64 20 72 65 74 75 72 6e 20  ical and return 
2211a 46 41 4c 53 45 20 69 66 20 74 68 65 79 20 64 69  FALSE if they di
2211b 66 66 65 72 20 69 6e 20 61 6e 79 20 77 61 79 2e  ffer in any way.
2211c 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
2211d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
2211e 6c 6c 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20  ll return FALSE 
2211f 65 76 65 6e 20 69 66 20 74 68 65 20 74 77 6f 20  even if the two 
22120 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 72  expressions.** r
22121 65 61 6c 6c 79 20 61 72 65 20 65 71 75 69 76 61  eally are equiva
22122 6c 65 6e 74 2e 20 20 49 66 20 77 65 20 63 61 6e  lent.  If we can
22123 6e 6f 74 20 70 72 6f 76 65 20 74 68 61 74 20 74  not prove that t
22124 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  he expressions a
22125 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63 61 6c 2c  re.** identical,
22126 20 77 65 20 72 65 74 75 72 6e 20 46 41 4c 53 45   we return FALSE
22127 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61 66 65   just to be safe
22128 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f  .  So if this ro
22129 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73  utine.** returns
2212a 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 79 6f 75   false, then you
2212b 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6b   do not really k
2212c 6e 6f 77 20 66 6f 72 20 63 65 72 74 61 69 6e 20  now for certain 
2212d 69 66 20 74 68 65 20 74 77 6f 0a 2a 2a 20 65 78  if the two.** ex
2212e 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 74 68  pressions are th
2212f 65 20 73 61 6d 65 2e 20 20 42 75 74 20 69 66 20  e same.  But if 
22130 79 6f 75 20 67 65 74 20 61 20 54 52 55 45 20 72  you get a TRUE r
22131 65 74 75 72 6e 2c 20 74 68 65 6e 20 79 6f 75 0a  eturn, then you.
22132 2a 2a 20 63 61 6e 20 62 65 20 73 75 72 65 20 74  ** can be sure t
22133 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  he expressions a
22134 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 49 6e  re the same.  In
22135 20 74 68 65 20 70 6c 61 63 65 73 20 77 68 65 72   the places wher
22136 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  e.** this routin
22137 65 20 69 73 20 75 73 65 64 2c 20 69 74 20 64 6f  e is used, it do
22138 65 73 20 6e 6f 74 20 68 75 72 74 20 74 6f 20 67  es not hurt to g
22139 65 74 20 61 6e 20 65 78 74 72 61 20 46 41 4c 53  et an extra FALS
2213a 45 20 2d 20 74 68 61 74 0a 2a 2a 20 6a 75 73 74  E - that.** just
2213b 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e   might result in
2213c 20 73 6f 6d 65 20 73 6c 69 67 68 74 6c 79 20 73   some slightly s
2213d 6c 6f 77 65 72 20 63 6f 64 65 2e 20 20 42 75 74  lower code.  But
2213e 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 61 6e   returning.** an
2213f 20 69 6e 63 6f 72 72 65 63 74 20 54 52 55 45 20   incorrect TRUE 
22140 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 61 20  could lead to a 
22141 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  malfunction..*/.
22142 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
22143 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
22144 6d 70 61 72 65 28 45 78 70 72 20 2a 70 41 2c 20  mpare(Expr *pA, 
22145 45 78 70 72 20 2a 70 42 29 7b 0a 20 20 69 6e 74  Expr *pB){.  int
22146 20 69 3b 0a 20 20 69 66 28 20 70 41 3d 3d 30 7c   i;.  if( pA==0|
22147 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  |pB==0 ){.    re
22148 74 75 72 6e 20 70 42 3d 3d 70 41 3b 0a 20 20 7d  turn pB==pA;.  }
22149 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72  .  assert( !Expr
2214a 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
2214b 41 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  A, EP_TokenOnly|
2214c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20  EP_Reduced) );. 
2214d 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
2214e 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 42 2c  sAnyProperty(pB,
2214f 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50   EP_TokenOnly|EP
22150 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 69  _Reduced) );.  i
22151 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
22152 74 79 28 70 41 2c 20 45 50 5f 78 49 73 53 65 6c  ty(pA, EP_xIsSel
22153 65 63 74 29 20 7c 7c 20 45 78 70 72 48 61 73 50  ect) || ExprHasP
22154 72 6f 70 65 72 74 79 28 70 42 2c 20 45 50 5f 78  roperty(pB, EP_x
22155 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20  IsSelect) ){.   
22156 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
22157 20 69 66 28 20 28 70 41 2d 3e 66 6c 61 67 73 20   if( (pA->flags 
22158 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29 21 3d  & EP_Distinct)!=
22159 28 70 42 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  (pB->flags & EP_
2215a 44 69 73 74 69 6e 63 74 29 20 29 20 72 65 74 75  Distinct) ) retu
2215b 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41 2d 3e  rn 0;.  if( pA->
2215c 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20 72 65 74  op!=pB->op ) ret
2215d 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71  urn 0;.  if( !sq
2215e 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
2215f 28 70 41 2d 3e 70 4c 65 66 74 2c 20 70 42 2d 3e  (pA->pLeft, pB->
22160 70 4c 65 66 74 29 20 29 20 72 65 74 75 72 6e 20  pLeft) ) return 
22161 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  0;.  if( !sqlite
22162 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 41 2d  3ExprCompare(pA-
22163 3e 70 52 69 67 68 74 2c 20 70 42 2d 3e 70 52 69  >pRight, pB->pRi
22164 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ght) ) return 0;
22165 0a 0a 20 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c  ..  if( pA->x.pL
22166 69 73 74 20 26 26 20 70 42 2d 3e 78 2e 70 4c 69  ist && pB->x.pLi
22167 73 74 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41  st ){.    if( pA
22168 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ->x.pList->nExpr
22169 21 3d 70 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e  !=pB->x.pList->n
2216a 45 78 70 72 20 29 20 72 65 74 75 72 6e 20 30 3b  Expr ) return 0;
2216b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2216c 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78  pA->x.pList->nEx
2216d 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
2216e 45 78 70 72 20 2a 70 45 78 70 72 41 20 3d 20 70  Expr *pExprA = p
2216f 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 69 5d  A->x.pList->a[i]
22170 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 45 78  .pExpr;.      Ex
22171 70 72 20 2a 70 45 78 70 72 42 20 3d 20 70 42 2d  pr *pExprB = pB-
22172 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  >x.pList->a[i].p
22173 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20  Expr;.      if( 
22174 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70  !sqlite3ExprComp
22175 61 72 65 28 70 45 78 70 72 41 2c 20 70 45 78 70  are(pExprA, pExp
22176 72 42 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  rB) ) return 0;.
22177 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
22178 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 20 7c 7c  ( pA->x.pList ||
22179 20 70 42 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a   pB->x.pList ){.
2217a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2217b 7d 0a 0a 20 20 69 66 28 20 70 41 2d 3e 69 54 61  }..  if( pA->iTa
2217c 62 6c 65 21 3d 70 42 2d 3e 69 54 61 62 6c 65 20  ble!=pB->iTable 
2217d 7c 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d 6e 21 3d  || pA->iColumn!=
2217e 70 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29 20 72 65  pB->iColumn ) re
2217f 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 45 78  turn 0;.  if( Ex
22180 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 41  prHasProperty(pA
22181 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
22182 7b 0a 20 20 20 20 69 66 28 20 21 45 78 70 72 48  {.    if( !ExprH
22183 61 73 50 72 6f 70 65 72 74 79 28 70 42 2c 20 45  asProperty(pB, E
22184 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c 20 70  P_IntValue) || p
22185 41 2d 3e 75 2e 69 56 61 6c 75 65 21 3d 70 42 2d  A->u.iValue!=pB-
22186 3e 75 2e 69 56 61 6c 75 65 20 29 7b 0a 20 20 20  >u.iValue ){.   
22187 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
22188 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70   }.  }else if( p
22189 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e  A->op!=TK_COLUMN
2218a 20 26 26 20 70 41 2d 3e 75 2e 7a 54 6f 6b 65 6e   && pA->u.zToken
2218b 20 29 7b 0a 20 20 20 20 69 66 28 20 45 78 70 72   ){.    if( Expr
2218c 48 61 73 50 72 6f 70 65 72 74 79 28 70 42 2c 20  HasProperty(pB, 
2218d 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c 20  EP_IntValue) || 
2218e 4e 45 56 45 52 28 70 42 2d 3e 75 2e 7a 54 6f 6b  NEVER(pB->u.zTok
2218f 65 6e 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20  en==0) ) return 
22190 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  0;.    if( sqlit
22191 65 33 53 74 72 49 43 6d 70 28 70 41 2d 3e 75 2e  e3StrICmp(pA->u.
22192 7a 54 6f 6b 65 6e 2c 70 42 2d 3e 75 2e 7a 54 6f  zToken,pB->u.zTo
22193 6b 65 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ken)!=0 ){.     
22194 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
22195 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
22196 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  .}.../*.** Add a
22197 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20   new element to 
22198 74 68 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43  the pAggInfo->aC
22199 6f 6c 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 74  ol[] array.  Ret
2219a 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66  urn the index of
2219b 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 6d  .** the new elem
2219c 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20 6e  ent.  Return a n
2219d 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69  egative number i
2219e 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a  f malloc fails..
2219f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64  */.static int ad
221a0 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 73  dAggInfoColumn(s
221a1 71 6c 69 74 65 33 20 2a 64 62 2c 20 41 67 67 49  qlite3 *db, AggI
221a2 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 69  nfo *pInfo){.  i
221a3 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61  nt i;.  pInfo->a
221a4 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 41 72 72  Col = sqlite3Arr
221a5 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20  ayAllocate(.    
221a6 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 70 49     db,.       pI
221a7 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a 20 20 20 20 20  nfo->aCol,.     
221a8 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e    sizeof(pInfo->
221a9 61 43 6f 6c 5b 30 5d 29 2c 0a 20 20 20 20 20 20  aCol[0]),.      
221aa 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66   3,.       &pInf
221ab 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20  o->nColumn,.    
221ac 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75     &pInfo->nColu
221ad 6d 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 20  mnAlloc,.       
221ae 26 69 0a 20 20 29 3b 0a 20 20 72 65 74 75 72 6e  &i.  );.  return
221af 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a   i;.}    ../*.**
221b0 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65   Add a new eleme
221b1 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 49 6e  nt to the pAggIn
221b2 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 61 72 72 61  fo->aFunc[] arra
221b3 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69  y.  Return the i
221b4 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e  ndex of.** the n
221b5 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74  ew element.  Ret
221b6 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e  urn a negative n
221b7 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20  umber if malloc 
221b8 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  fails..*/.static
221b9 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 46   int addAggInfoF
221ba 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  unc(sqlite3 *db,
221bb 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29   AggInfo *pInfo)
221bc 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e  {.  int i;.  pIn
221bd 66 6f 2d 3e 61 46 75 6e 63 20 3d 20 73 71 6c 69  fo->aFunc = sqli
221be 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65  te3ArrayAllocate
221bf 28 0a 20 20 20 20 20 20 20 64 62 2c 20 0a 20 20  (.       db, .  
221c0 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e       pInfo->aFun
221c1 63 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66  c,.       sizeof
221c2 28 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d  (pInfo->aFunc[0]
221c3 29 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 20  ),.       3,.   
221c4 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e      &pInfo->nFun
221c5 63 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f  c,.       &pInfo
221c6 2d 3e 6e 46 75 6e 63 41 6c 6c 6f 63 2c 0a 20 20  ->nFuncAlloc,.  
221c7 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 72       &i.  );.  r
221c8 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a  eturn i;.}    ..
221c9 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  /*.** This is th
221ca 65 20 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20  e xExprCallback 
221cb 66 6f 72 20 61 20 74 72 65 65 20 77 61 6c 6b 65  for a tree walke
221cc 72 2e 20 20 49 74 20 69 73 20 75 73 65 64 20 74  r.  It is used t
221cd 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 73  o.** implement s
221ce 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
221cf 65 41 67 67 72 65 67 61 74 65 73 28 29 2e 20 20  eAggregates().  
221d0 53 65 65 20 73 71 6c 69 74 65 33 45 78 70 72 41  See sqlite3ExprA
221d1 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
221d2 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  .** for addition
221d3 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
221d4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e  */.static int an
221d5 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 28 57  alyzeAggregate(W
221d6 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20  alker *pWalker, 
221d7 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20  Expr *pExpr){.  
221d8 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43 6f 6e  int i;.  NameCon
221d9 74 65 78 74 20 2a 70 4e 43 20 3d 20 70 57 61 6c  text *pNC = pWal
221da 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 50 61  ker->u.pNC;.  Pa
221db 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 4e  rse *pParse = pN
221dc 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 53 72 63  C->pParse;.  Src
221dd 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d  List *pSrcList =
221de 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a   pNC->pSrcList;.
221df 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49    AggInfo *pAggI
221e0 6e 66 6f 20 3d 20 70 4e 43 2d 3e 70 41 67 67 49  nfo = pNC->pAggI
221e1 6e 66 6f 3b 0a 0a 20 20 73 77 69 74 63 68 28 20  nfo;..  switch( 
221e2 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20  pExpr->op ){.   
221e3 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c   case TK_AGG_COL
221e4 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  UMN:.    case TK
221e5 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20  _COLUMN: {.     
221e6 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72   testcase( pExpr
221e7 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c  ->op==TK_AGG_COL
221e8 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73  UMN );.      tes
221e9 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70  tcase( pExpr->op
221ea 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20  ==TK_COLUMN );. 
221eb 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f       /* Check to
221ec 20 73 65 65 20 69 66 20 74 68 65 20 63 6f 6c 75   see if the colu
221ed 6d 6e 20 69 73 20 69 6e 20 6f 6e 65 20 6f 66 20  mn is in one of 
221ee 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68  the tables in th
221ef 65 20 46 52 4f 4d 0a 20 20 20 20 20 20 2a 2a 20  e FROM.      ** 
221f0 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 61 67  clause of the ag
221f1 67 72 65 67 61 74 65 20 71 75 65 72 79 20 2a 2f  gregate query */
221f2 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59  .      if( ALWAY
221f3 53 28 70 53 72 63 4c 69 73 74 21 3d 30 29 20 29  S(pSrcList!=0) )
221f4 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74  {.        struct
221f5 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
221f6 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d  Item = pSrcList-
221f7 3e 61 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  >a;.        for(
221f8 69 3d 30 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d  i=0; i<pSrcList-
221f9 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65  >nSrc; i++, pIte
221fa 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
221fb 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63  struct AggInfo_c
221fc 6f 6c 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20 20  ol *pCol;.      
221fd 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
221fe 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
221ff 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f  pExpr, EP_TokenO
22200 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20  nly|EP_Reduced) 
22201 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
22202 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d   pExpr->iTable==
22203 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 29  pItem->iCursor )
22204 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {.            /*
22205 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68 69   If we reach thi
22206 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e  s point, it mean
22207 73 20 74 68 61 74 20 70 45 78 70 72 20 72 65 66  s that pExpr ref
22208 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 0a 20  ers to a table. 
22209 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68             ** th
2220a 61 74 20 69 73 20 69 6e 20 74 68 65 20 46 52 4f  at is in the FRO
2220b 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  M clause of the 
2220c 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79 2e  aggregate query.
2220d 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a    .            *
2220e 2a 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  *.            **
2220f 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79 20 66   Make an entry f
22210 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e  or the column in
22211 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b   pAggInfo->aCol[
22212 5d 20 69 66 20 74 68 65 72 65 0a 20 20 20 20 20  ] if there.     
22213 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74         ** is not
22214 20 61 6e 20 65 6e 74 72 79 20 74 68 65 72 65 20   an entry there 
22215 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 20 20 20  already..       
22216 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
22217 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20      int k;.     
22218 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 70 41         pCol = pA
22219 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 0a 20 20  ggInfo->aCol;.  
2221a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6b 3d            for(k=
2221b 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e  0; k<pAggInfo->n
2221c 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20 70 43 6f  Column; k++, pCo
2221d 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  l++){.          
2221e 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 54      if( pCol->iT
2221f 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61  able==pExpr->iTa
22220 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20 20  ble &&.         
22221 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69           pCol->i
22222 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e 69  Column==pExpr->i
22223 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20  Column ){.      
22224 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
22225 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
22226 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
22227 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28             if( (
22228 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f  k>=pAggInfo->nCo
22229 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20 20 20 20  lumn).          
2222a 20 20 20 26 26 20 28 6b 20 3d 20 61 64 64 41 67     && (k = addAg
2222b 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70 50 61 72  gInfoColumn(pPar
2222c 73 65 2d 3e 64 62 2c 20 70 41 67 67 49 6e 66 6f  se->db, pAggInfo
2222d 29 29 3e 3d 30 20 0a 20 20 20 20 20 20 20 20 20  ))>=0 .         
2222e 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20     ){.          
2222f 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 41 67 67      pCol = &pAgg
22230 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d 3b 0a 20  Info->aCol[k];. 
22231 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
22232 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78 70 72 2d  l->pTab = pExpr-
22233 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20  >pTab;.         
22234 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54 61 62 6c       pCol->iTabl
22235 65 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  e = pExpr->iTabl
22236 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e;.             
22237 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 20 3d   pCol->iColumn =
22238 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b   pExpr->iColumn;
22239 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
2223a 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50  Col->iMem = ++pP
2223b 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
2223c 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
2223d 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20  iSorterColumn = 
2223e 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  -1;.            
2223f 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72 20 3d 20    pCol->pExpr = 
22240 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20  pExpr;.         
22241 20 20 20 20 20 69 66 28 20 70 41 67 67 49 6e 66       if( pAggInf
22242 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a 20  o->pGroupBy ){. 
22243 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
22244 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20 20 20 20  nt j, n;.       
22245 20 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 73           ExprLis
22246 74 20 2a 70 47 42 20 3d 20 70 41 67 67 49 6e 66  t *pGB = pAggInf
22247 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20  o->pGroupBy;.   
22248 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72               str
22249 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
2224a 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47 42 2d 3e  m *pTerm = pGB->
2224b 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a;.             
2224c 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e 45 78 70     n = pGB->nExp
2224d 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r;.             
2224e 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 3b     for(j=0; j<n;
2224f 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a   j++, pTerm++){.
22250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22251 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 54 65    Expr *pE = pTe
22252 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  rm->pExpr;.     
22253 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
22254 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55   pE->op==TK_COLU
22255 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61 62 6c 65  MN && pE->iTable
22256 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20  ==pExpr->iTable 
22257 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  &&.             
22258 20 20 20 20 20 20 20 20 20 70 45 2d 3e 69 43 6f           pE->iCo
22259 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e 69 43 6f  lumn==pExpr->iCo
2225a 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  lumn ){.        
2225b 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
2225c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20  ->iSorterColumn 
2225d 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = j;.           
2225e 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2225f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22260 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
22261 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
22262 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
22263 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53      if( pCol->iS
22264 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30 20 29 7b  orterColumn<0 ){
22265 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22266 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f   pCol->iSorterCo
22267 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e 66 6f 2d  lumn = pAggInfo-
22268 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 2b  >nSortingColumn+
22269 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  +;.             
2226a 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d   }.            }
2226b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
2226c 54 68 65 72 65 20 69 73 20 6e 6f 77 20 61 6e 20  There is now an 
2226d 65 6e 74 72 79 20 66 6f 72 20 70 45 78 70 72 20  entry for pExpr 
2226e 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f  in pAggInfo->aCo
2226f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20 20 20 20  l[] (either.    
22270 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63 61 75          ** becau
22271 73 65 20 69 74 20 77 61 73 20 74 68 65 72 65 20  se it was there 
22272 62 65 66 6f 72 65 20 6f 72 20 62 65 63 61 75 73  before or becaus
22273 65 20 77 65 20 6a 75 73 74 20 63 72 65 61 74 65  e we just create
22274 64 20 69 74 29 2e 0a 20 20 20 20 20 20 20 20 20  d it)..         
22275 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68     ** Convert th
22276 65 20 70 45 78 70 72 20 74 6f 20 62 65 20 61 20  e pExpr to be a 
22277 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 72 65  TK_AGG_COLUMN re
22278 66 65 72 72 69 6e 67 20 74 6f 20 74 68 61 74 0a  ferring to that.
22279 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70              ** p
2227a 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20  AggInfo->aCol[] 
2227b 65 6e 74 72 79 2e 0a 20 20 20 20 20 20 20 20 20  entry..         
2227c 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
2227d 20 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63    ExprSetIrreduc
2227e 69 62 6c 65 28 70 45 78 70 72 29 3b 0a 20 20 20  ible(pExpr);.   
2227f 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
22280 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 67 49  pAggInfo = pAggI
22281 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nfo;.           
22282 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f   pExpr->op = TK_
22283 41 47 47 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20  AGG_COLUMN;.    
22284 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
22285 41 67 67 20 3d 20 28 69 31 36 29 6b 3b 0a 20 20  Agg = (i16)k;.  
22286 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
22287 0a 20 20 20 20 20 20 20 20 20 20 7d 20 2f 2a 20  .          } /* 
22288 65 6e 64 69 66 20 70 45 78 70 72 2d 3e 69 54 61  endif pExpr->iTa
22289 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72  ble==pItem->iCur
2228a 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d  sor */.        }
2228b 20 2f 2a 20 65 6e 64 20 6c 6f 6f 70 20 6f 76 65   /* end loop ove
2228c 72 20 70 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20  r pSrcList */.  
2228d 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
2228e 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
2228f 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
22290 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a  AGG_FUNCTION: {.
22291 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 4e 43        /* The pNC
22292 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 74 65 73 74  ->nDepth==0 test
22293 20 63 61 75 73 65 73 20 61 67 67 72 65 67 61 74   causes aggregat
22294 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 73  e functions in s
22295 75 62 71 75 65 72 69 65 73 0a 20 20 20 20 20 20  ubqueries.      
22296 2a 2a 20 74 6f 20 62 65 20 69 67 6e 6f 72 65 64  ** to be ignored
22297 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4e   */.      if( pN
22298 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b 0a  C->nDepth==0 ){.
22299 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b          /* Check
2229a 20 74 6f 20 73 65 65 20 69 66 20 70 45 78 70 72   to see if pExpr
2229b 20 69 73 20 61 20 64 75 70 6c 69 63 61 74 65 20   is a duplicate 
2229c 6f 66 20 61 6e 6f 74 68 65 72 20 61 67 67 72 65  of another aggre
2229d 67 61 74 65 20 0a 20 20 20 20 20 20 20 20 2a 2a  gate .        **
2229e 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 69   function that i
2229f 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
222a0 20 70 41 67 67 49 6e 66 6f 20 73 74 72 75 63 74   pAggInfo struct
222a1 75 72 65 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ure.        */. 
222a2 20 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67         struct Ag
222a3 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 49 74 65  gInfo_func *pIte
222a4 6d 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46  m = pAggInfo->aF
222a5 75 6e 63 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  unc;.        for
222a6 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f  (i=0; i<pAggInfo
222a7 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 49  ->nFunc; i++, pI
222a8 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  tem++){.        
222a9 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
222aa 72 43 6f 6d 70 61 72 65 28 70 49 74 65 6d 2d 3e  rCompare(pItem->
222ab 70 45 78 70 72 2c 20 70 45 78 70 72 29 20 29 7b  pExpr, pExpr) ){
222ac 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
222ad 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
222ae 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
222af 20 20 69 66 28 20 69 3e 3d 70 41 67 67 49 6e 66    if( i>=pAggInf
222b0 6f 2d 3e 6e 46 75 6e 63 20 29 7b 0a 20 20 20 20  o->nFunc ){.    
222b1 20 20 20 20 20 20 2f 2a 20 70 45 78 70 72 20 69        /* pExpr i
222b2 73 20 6f 72 69 67 69 6e 61 6c 2e 20 20 4d 61 6b  s original.  Mak
222b3 65 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 6e  e a new entry in
222b4 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63   pAggInfo->aFunc
222b5 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  [].          */.
222b6 20 20 20 20 20 20 20 20 20 20 75 38 20 65 6e 63            u8 enc
222b7 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d 3e 64   = ENC(pParse->d
222b8 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 20  b);.          i 
222b9 3d 20 61 64 64 41 67 67 49 6e 66 6f 46 75 6e 63  = addAggInfoFunc
222ba 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 41 67  (pParse->db, pAg
222bb 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20  gInfo);.        
222bc 20 20 69 66 28 20 69 3e 3d 30 20 29 7b 0a 20 20    if( i>=0 ){.  
222bd 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
222be 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
222bf 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
222c0 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20  Select) );.     
222c1 20 20 20 20 20 20 20 70 49 74 65 6d 20 3d 20 26         pItem = &
222c2 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b  pAggInfo->aFunc[
222c3 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  i];.            
222c4 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70  pItem->pExpr = p
222c5 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Expr;.          
222c6 20 20 70 49 74 65 6d 2d 3e 69 4d 65 6d 20 3d 20    pItem->iMem = 
222c7 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
222c8 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
222c9 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
222ca 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49  erty(pExpr, EP_I
222cb 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20  ntValue) );.    
222cc 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70          pItem->p
222cd 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 46 69  Func = sqlite3Fi
222ce 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73  ndFunction(pPars
222cf 65 2d 3e 64 62 2c 0a 20 20 20 20 20 20 20 20 20  e->db,.         
222d0 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
222d1 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 73 71 6c 69 74  >u.zToken, sqlit
222d2 65 33 53 74 72 6c 65 6e 33 30 28 70 45 78 70 72  e3Strlen30(pExpr
222d3 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0a 20 20 20  ->u.zToken),.   
222d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222d5 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3f  pExpr->x.pList ?
222d6 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d   pExpr->x.pList-
222d7 3e 6e 45 78 70 72 20 3a 20 30 2c 20 65 6e 63 2c  >nExpr : 0, enc,
222d8 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
222d9 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67   if( pExpr->flag
222da 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 20  s & EP_Distinct 
222db 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
222dc 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69 6e 63   pItem->iDistinc
222dd 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  t = pParse->nTab
222de 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ++;.            
222df 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
222e0 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73       pItem->iDis
222e1 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20  tinct = -1;.    
222e2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
222e3 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
222e4 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20          /* Make 
222e5 70 45 78 70 72 20 70 6f 69 6e 74 20 74 6f 20 74  pExpr point to t
222e6 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 70  he appropriate p
222e7 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d  AggInfo->aFunc[]
222e8 20 65 6e 74 72 79 0a 20 20 20 20 20 20 20 20 2a   entry.        *
222e9 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
222ea 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f  ( !ExprHasAnyPro
222eb 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
222ec 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64  TokenOnly|EP_Red
222ed 75 63 65 64 29 20 29 3b 0a 20 20 20 20 20 20 20  uced) );.       
222ee 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69   ExprSetIrreduci
222ef 62 6c 65 28 70 45 78 70 72 29 3b 0a 20 20 20 20  ble(pExpr);.    
222f0 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20      pExpr->iAgg 
222f1 3d 20 28 69 31 36 29 69 3b 0a 20 20 20 20 20 20  = (i16)i;.      
222f2 20 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66    pExpr->pAggInf
222f3 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20  o = pAggInfo;.  
222f4 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
222f5 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 7d 0a  _Prune;.      }.
222f6 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
222f7 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
222f8 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e  .}.static int an
222f9 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 49  alyzeAggregatesI
222fa 6e 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a  nSelect(Walker *
222fb 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20  pWalker, Select 
222fc 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 4e 61 6d  *pSelect){.  Nam
222fd 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20  eContext *pNC = 
222fe 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a  pWalker->u.pNC;.
222ff 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74    if( pNC->nDept
22300 68 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 43 2d  h==0 ){.    pNC-
22301 3e 6e 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20 73  >nDepth++;.    s
22302 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
22303 28 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c 65 63  (pWalker, pSelec
22304 74 29 3b 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65  t);.    pNC->nDe
22305 70 74 68 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72  pth--;.    retur
22306 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d  n WRC_Prune;.  }
22307 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
22308 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20   WRC_Continue;. 
22309 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c   }.}../*.** Anal
2230a 79 7a 65 20 74 68 65 20 67 69 76 65 6e 20 65 78  yze the given ex
2230b 70 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69 6e 67  pression looking
2230c 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66   for aggregate f
2230d 75 6e 63 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20  unctions and.** 
2230e 66 6f 72 20 76 61 72 69 61 62 6c 65 73 20 74 68  for variables th
2230f 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 61 64  at need to be ad
22310 64 65 64 20 74 6f 20 74 68 65 20 70 50 61 72 73  ded to the pPars
22311 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 2e  e->aAgg[] array.
22312 0a 2a 2a 20 4d 61 6b 65 20 61 64 64 69 74 69 6f  .** Make additio
22313 6e 61 6c 20 65 6e 74 72 69 65 73 20 74 6f 20 74  nal entries to t
22314 68 65 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b  he pParse->aAgg[
22315 5d 20 61 72 72 61 79 20 61 73 20 6e 65 63 65 73  ] array as neces
22316 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  sary..**.** This
22317 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20   routine should 
22318 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 61  only be called a
22319 66 74 65 72 20 74 68 65 20 65 78 70 72 65 73 73  fter the express
2231a 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20  ion has been.** 
2231b 61 6e 61 6c 79 7a 65 64 20 62 79 20 73 71 6c 69  analyzed by sqli
2231c 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
2231d 6d 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  mes()..*/.SQLITE
2231e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2231f 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
22320 41 67 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43  Aggregates(NameC
22321 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45 78 70  ontext *pNC, Exp
22322 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c  r *pExpr){.  Wal
22323 6b 65 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72  ker w;.  w.xExpr
22324 43 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79  Callback = analy
22325 7a 65 41 67 67 72 65 67 61 74 65 3b 0a 20 20 77  zeAggregate;.  w
22326 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  .xSelectCallback
22327 20 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67   = analyzeAggreg
22328 61 74 65 73 49 6e 53 65 6c 65 63 74 3b 0a 20 20  atesInSelect;.  
22329 77 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20  w.u.pNC = pNC;. 
2232a 20 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 70 53   assert( pNC->pS
2232b 72 63 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 73  rcList!=0 );.  s
2232c 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26  qlite3WalkExpr(&
2232d 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a  w, pExpr);.}../*
2232e 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33  .** Call sqlite3
2232f 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65  ExprAnalyzeAggre
22330 67 61 74 65 73 28 29 20 66 6f 72 20 65 76 65 72  gates() for ever
22331 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  y expression in 
22332 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  an.** expression
22333 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 20 74   list.  Return t
22334 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  he number of err
22335 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ors..**.** If an
22336 20 65 72 72 6f 72 20 69 73 20 66 6f 75 6e 64 2c   error is found,
22337 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 69 73   the analysis is
22338 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a 2f 0a 53   cut short..*/.S
22339 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2233a 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e  id sqlite3ExprAn
2233b 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61 6d  alyzeAggList(Nam
2233c 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45  eContext *pNC, E
2233d 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  xprList *pList){
2233e 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
2233f 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
22340 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70    int i;.  if( p
22341 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28  List ){.    for(
22342 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20  pItem=pList->a, 
22343 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
22344 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
22345 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
22346 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72  3ExprAnalyzeAggr
22347 65 67 61 74 65 73 28 70 4e 43 2c 20 70 49 74 65  egates(pNC, pIte
22348 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d  m->pExpr);.    }
22349 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  .  }.}../*.** Al
2234a 6c 6f 63 61 74 65 20 61 20 73 69 6e 67 6c 65 20  locate a single 
2234b 6e 65 77 20 72 65 67 69 73 74 65 72 20 66 6f 72  new register for
2234c 20 75 73 65 20 74 6f 20 68 6f 6c 64 20 73 6f 6d   use to hold som
2234d 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 72  e intermediate r
2234e 65 73 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  esult..*/.SQLITE
2234f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
22350 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50  ite3GetTempReg(P
22351 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
22352 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65   if( pParse->nTe
22353 6d 70 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20  mpReg==0 ){.    
22354 72 65 74 75 72 6e 20 2b 2b 70 50 61 72 73 65 2d  return ++pParse-
22355 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74  >nMem;.  }.  ret
22356 75 72 6e 20 70 50 61 72 73 65 2d 3e 61 54 65 6d  urn pParse->aTem
22357 70 52 65 67 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e  pReg[--pParse->n
22358 54 65 6d 70 52 65 67 5d 3b 0a 7d 0a 0a 2f 2a 0a  TempReg];.}../*.
22359 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 20  ** Deallocate a 
2235a 72 65 67 69 73 74 65 72 2c 20 6d 61 6b 69 6e 67  register, making
2235b 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 72   available for r
2235c 65 75 73 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74  euse for some ot
2235d 68 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65 2e 0a  her.** purpose..
2235e 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73  **.** If a regis
2235f 74 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ter is currently
22360 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 74   being used by t
22361 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2c  he column cache,
22362 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 6c   then.** the dal
22363 6c 6f 63 61 74 69 6f 6e 20 69 73 20 64 65 66 65  location is defe
22364 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65 20 63  rred until the c
22365 6f 6c 75 6d 6e 20 63 61 63 68 65 20 6c 69 6e 65  olumn cache line
22366 20 74 68 61 74 20 75 73 65 73 0a 2a 2a 20 74 68   that uses.** th
22367 65 20 72 65 67 69 73 74 65 72 20 62 65 63 6f 6d  e register becom
22368 65 73 20 73 74 61 6c 65 2e 0a 2a 2f 0a 53 51 4c  es stale..*/.SQL
22369 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2236a 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2236b 65 6d 70 52 65 67 28 50 61 72 73 65 20 2a 70 50  empReg(Parse *pP
2236c 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b  arse, int iReg){
2236d 0a 20 20 69 66 28 20 69 52 65 67 20 26 26 20 70  .  if( iReg && p
2236e 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c  Parse->nTempReg<
2236f 41 72 72 61 79 53 69 7a 65 28 70 50 61 72 73 65  ArraySize(pParse
22370 2d 3e 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20  ->aTempReg) ){. 
22371 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 74     int i;.    st
22372 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a  ruct yColCache *
22373 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  p;.    for(i=0, 
22374 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
22375 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
22376 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
22377 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
22378 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a  ->iReg==iReg ){.
22379 20 20 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52          p->tempR
2237a 65 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  eg = 1;.        
2237b 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
2237c 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65      }.    pParse
2237d 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61 72 73  ->aTempReg[pPars
2237e 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d  e->nTempReg++] =
2237f 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a   iReg;.  }.}../*
22380 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20  .** Allocate or 
22381 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f  deallocate a blo
22382 63 6b 20 6f 66 20 6e 52 65 67 20 63 6f 6e 73 65  ck of nReg conse
22383 63 75 74 69 76 65 20 72 65 67 69 73 74 65 72 73  cutive registers
22384 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
22385 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
22386 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65  tTempRange(Parse
22387 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6e 52   *pParse, int nR
22388 65 67 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b  eg){.  int i, n;
22389 0a 20 20 69 20 3d 20 70 50 61 72 73 65 2d 3e 69  .  i = pParse->i
2238a 52 61 6e 67 65 52 65 67 3b 0a 20 20 6e 20 3d 20  RangeReg;.  n = 
2238b 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65  pParse->nRangeRe
2238c 67 3b 0a 20 20 69 66 28 20 6e 52 65 67 3c 3d 6e  g;.  if( nReg<=n
2238d 20 26 26 20 21 75 73 65 64 41 73 43 6f 6c 75 6d   && !usedAsColum
2238e 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 69  nCache(pParse, i
2238f 2c 20 69 2b 6e 2d 31 29 20 29 7b 0a 20 20 20 20  , i+n-1) ){.    
22390 70 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65  pParse->iRangeRe
22391 67 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70  g += nReg;.    p
22392 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67  Parse->nRangeReg
22393 20 2d 3d 20 6e 52 65 67 3b 0a 20 20 7d 65 6c 73   -= nReg;.  }els
22394 65 7b 0a 20 20 20 20 69 20 3d 20 70 50 61 72 73  e{.    i = pPars
22395 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70  e->nMem+1;.    p
22396 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e  Parse->nMem += n
22397 52 65 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Reg;.  }.  retur
22398 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  n i;.}.SQLITE_PR
22399 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2239a 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
2239b 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ge(Parse *pParse
2239c 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 6e 74 20  , int iReg, int 
2239d 6e 52 65 67 29 7b 0a 20 20 69 66 28 20 6e 52 65  nReg){.  if( nRe
2239e 67 3e 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65  g>pParse->nRange
2239f 52 65 67 20 29 7b 0a 20 20 20 20 70 50 61 72 73  Reg ){.    pPars
223a0 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 3d 20 6e  e->nRangeReg = n
223a1 52 65 67 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  Reg;.    pParse-
223a2 3e 69 52 61 6e 67 65 52 65 67 20 3d 20 69 52 65  >iRangeReg = iRe
223a3 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  g;.  }.}../*****
223a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
223a5 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   expr.c ********
223a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223a8 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
223a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
223aa 66 69 6c 65 20 61 6c 74 65 72 2e 63 20 2a 2a 2a  file alter.c ***
223ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223ad 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
223ae 32 30 30 35 20 46 65 62 72 75 61 72 79 20 31 35  2005 February 15
223af 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
223b0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
223b1 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
223b2 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
223b3 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
223b4 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
223b5 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
223b6 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
223b7 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
223b8 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
223b9 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
223ba 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
223bb 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
223bc 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
223bd 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
223be 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
223bf 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
223c0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
223c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
223c5 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
223c6 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75  tains C code rou
223c7 74 69 6e 65 73 20 74 68 61 74 20 75 73 65 64 20  tines that used 
223c8 74 6f 20 67 65 6e 65 72 61 74 65 20 56 44 42 45  to generate VDBE
223c9 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20 69 6d   code.** that im
223ca 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 41 4c 54  plements the ALT
223cb 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64  ER TABLE command
223cc 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 6c 74  ..**.** $Id: alt
223cd 65 72 2e 63 2c 76 20 31 2e 36 32 20 32 30 30 39  er.c,v 1.62 2009
223ce 2f 30 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20  /07/24 17:58:53 
223cf 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
223d0 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
223d1 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
223d2 65 20 6f 6e 6c 79 20 65 78 69 73 74 73 20 69 66  e only exists if
223d3 20 77 65 20 61 72 65 20 6e 6f 74 20 6f 6d 69 74   we are not omit
223d4 74 69 6e 67 20 74 68 65 0a 2a 2a 20 41 4c 54 45  ting the.** ALTE
223d5 52 20 54 41 42 4c 45 20 6c 6f 67 69 63 20 66 72  R TABLE logic fr
223d6 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f  om the build..*/
223d7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
223d8 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a  OMIT_ALTERTABLE.
223d9 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
223da 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79  ction is used by
223db 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20 74   SQL generated t
223dc 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
223dd 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20  .** ALTER TABLE 
223de 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66 69 72  command. The fir
223df 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  st argument is t
223e0 68 65 20 74 65 78 74 20 6f 66 20 61 20 43 52 45  he text of a CRE
223e1 41 54 45 20 54 41 42 4c 45 20 6f 72 0a 2a 2a 20  ATE TABLE or.** 
223e2 43 52 45 41 54 45 20 49 4e 44 45 58 20 63 6f 6d  CREATE INDEX com
223e3 6d 61 6e 64 2e 20 54 68 65 20 73 65 63 6f 6e 64  mand. The second
223e4 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65   is a table name
223e5 2e 20 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  . The table name
223e6 20 69 6e 20 0a 2a 2a 20 74 68 65 20 43 52 45 41   in .** the CREA
223e7 54 45 20 54 41 42 4c 45 20 6f 72 20 43 52 45 41  TE TABLE or CREA
223e8 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65  TE INDEX stateme
223e9 6e 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 77  nt is replaced w
223ea 69 74 68 20 74 68 65 20 74 68 69 72 64 0a 2a 2a  ith the third.**
223eb 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68   argument and th
223ec 65 20 72 65 73 75 6c 74 20 72 65 74 75 72 6e 65  e result returne
223ed 64 2e 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a  d. Examples:.**.
223ee 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65  ** sqlite_rename
223ef 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45 20 54  _table('CREATE T
223f0 41 42 4c 45 20 61 62 63 28 61 2c 20 62 2c 20 63  ABLE abc(a, b, c
223f1 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20 20  )', 'def').**   
223f2 20 20 2d 3e 20 27 43 52 45 41 54 45 20 54 41 42    -> 'CREATE TAB
223f3 4c 45 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27  LE def(a, b, c)'
223f4 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65  .**.** sqlite_re
223f5 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52 45 41  name_table('CREA
223f6 54 45 20 49 4e 44 45 58 20 69 20 4f 4e 20 61 62  TE INDEX i ON ab
223f7 63 28 61 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a  c(a)', 'def').**
223f8 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20       -> 'CREATE 
223f9 49 4e 44 45 58 20 69 20 4f 4e 20 64 65 66 28 61  INDEX i ON def(a
223fa 2c 20 62 2c 20 63 29 27 0a 2a 2f 0a 73 74 61 74  , b, c)'.*/.stat
223fb 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 54 61  ic void renameTa
223fc 62 6c 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  bleFunc(.  sqlit
223fd 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
223fe 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
223ff 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
22400 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
22401 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
22402 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69  nst *zSql = sqli
22403 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
22404 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67  rgv[0]);.  unsig
22405 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ned char const *
22406 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c  zTableName = sql
22407 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
22408 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74  argv[1]);..  int
22409 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20   token;.  Token 
2240a 74 6e 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e 65  tname;.  unsigne
2240b 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43  d char const *zC
2240c 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74  sr = zSql;.  int
2240d 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72   len = 0;.  char
2240e 20 2a 7a 52 65 74 3b 0a 0a 20 20 73 71 6c 69 74   *zRet;..  sqlit
2240f 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
22410 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
22411 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20  le(context);..  
22412 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
22413 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a  (NotUsed);..  /*
22414 20 54 68 65 20 70 72 69 6e 63 69 70 6c 65 20 75   The principle u
22415 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  sed to locate th
22416 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20  e table name in 
22417 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
22418 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74   .  ** statement
22419 20 69 73 20 74 68 61 74 20 74 68 65 20 74 61 62   is that the tab
2241a 6c 65 20 6e 61 6d 65 20 69 73 20 74 68 65 20 66  le name is the f
2241b 69 72 73 74 20 6e 6f 6e 2d 73 70 61 63 65 20 74  irst non-space t
2241c 6f 6b 65 6e 20 74 68 61 74 0a 20 20 2a 2a 20 69  oken that.  ** i
2241d 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f  s immediately fo
2241e 6c 6c 6f 77 65 64 20 62 79 20 61 20 54 4b 5f 4c  llowed by a TK_L
2241f 50 20 6f 72 20 54 4b 5f 55 53 49 4e 47 20 74 6f  P or TK_USING to
22420 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ken..  */.  if( 
22421 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b  zSql ){.    do {
22422 0a 20 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73  .      if( !*zCs
22423 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  r ){.        /* 
22424 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74  Ran out of input
22425 20 62 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20   before finding 
22426 61 6e 20 6f 70 65 6e 69 6e 67 20 62 72 61 63 6b  an opening brack
22427 65 74 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e  et. Return NULL.
22428 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75   */.        retu
22429 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  rn;.      }..   
2242a 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20     /* Store the 
2242b 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43 73 72 20  token that zCsr 
2242c 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61  points to in tna
2242d 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61  me. */.      tna
2242e 6d 65 2e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 43  me.z = (char*)zC
2242f 73 72 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e  sr;.      tname.
22430 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20  n = len;..      
22431 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43 73 72 20  /* Advance zCsr 
22432 74 6f 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65  to the next toke
22433 6e 2e 20 53 74 6f 72 65 20 74 68 61 74 20 74 6f  n. Store that to
22434 6b 65 6e 20 74 79 70 65 20 69 6e 20 27 74 6f 6b  ken type in 'tok
22435 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e  en',.      ** an
22436 64 20 69 74 73 20 6c 65 6e 67 74 68 20 69 6e 20  d its length in 
22437 27 6c 65 6e 27 20 28 74 6f 20 62 65 20 75 73 65  'len' (to be use
22438 64 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e  d next iteration
22439 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a   of this loop)..
2243a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64        */.      d
2243b 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 43 73 72  o {.        zCsr
2243c 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20   += len;.       
2243d 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 65   len = sqlite3Ge
2243e 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f  tToken(zCsr, &to
2243f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 20 77 68  ken);.      } wh
22440 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53  ile( token==TK_S
22441 50 41 43 45 20 29 3b 0a 20 20 20 20 20 20 61 73  PACE );.      as
22442 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a 20  sert( len>0 );. 
22443 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65     } while( toke
22444 6e 21 3d 54 4b 5f 4c 50 20 26 26 20 74 6f 6b 65  n!=TK_LP && toke
22445 6e 21 3d 54 4b 5f 55 53 49 4e 47 20 29 3b 0a 0a  n!=TK_USING );..
22446 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74      zRet = sqlit
22447 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
22448 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20 28 28  .*s\"%w\"%s", ((
22449 75 38 2a 29 74 6e 61 6d 65 2e 7a 29 20 2d 20 7a  u8*)tname.z) - z
2244a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20  Sql, zSql, .    
2244b 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74     zTableName, t
2244c 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b  name.z+tname.n);
2244d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
2244e 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
2244f 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49  , zRet, -1, SQLI
22450 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d  TE_DYNAMIC);.  }
22451 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 43  .}../*.** This C
22452 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d   function implem
22453 65 6e 74 73 20 61 6e 20 53 51 4c 20 75 73 65 72  ents an SQL user
22454 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 69   function that i
22455 73 20 75 73 65 64 20 62 79 20 53 51 4c 20 63 6f  s used by SQL co
22456 64 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20  de.** generated 
22457 62 79 20 74 68 65 20 41 4c 54 45 52 20 54 41 42  by the ALTER TAB
22458 4c 45 20 2e 2e 2e 20 52 45 4e 41 4d 45 20 63 6f  LE ... RENAME co
22459 6d 6d 61 6e 64 20 74 6f 20 6d 6f 64 69 66 79 20  mmand to modify 
2245a 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 0a 2a  the definition.*
2245b 2a 20 6f 66 20 61 6e 79 20 66 6f 72 65 69 67 6e  * of any foreign
2245c 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73   key constraints
2245d 20 74 68 61 74 20 75 73 65 20 74 68 65 20 74 61   that use the ta
2245e 62 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 6d 65  ble being rename
2245f 64 20 61 73 20 74 68 65 20 0a 2a 2a 20 70 61 72  d as the .** par
22460 65 6e 74 20 74 61 62 6c 65 2e 20 49 74 20 69 73  ent table. It is
22461 20 70 61 73 73 65 64 20 74 68 72 65 65 20 61 72   passed three ar
22462 67 75 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  guments:.**.**  
22463 20 31 29 20 54 68 65 20 63 6f 6d 70 6c 65 74 65   1) The complete
22464 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45   text of the CRE
22465 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
22466 65 6e 74 20 62 65 69 6e 67 20 6d 6f 64 69 66 69  ent being modifi
22467 65 64 2c 0a 2a 2a 20 20 20 32 29 20 54 68 65 20  ed,.**   2) The 
22468 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  old name of the 
22469 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65 6e 61  table being rena
2246a 6d 65 64 2c 20 61 6e 64 0a 2a 2a 20 20 20 33 29  med, and.**   3)
2246b 20 54 68 65 20 6e 65 77 20 6e 61 6d 65 20 6f 66   The new name of
2246c 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
2246d 20 72 65 6e 61 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20   renamed..**.** 
2246e 49 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  It returns the n
2246f 65 77 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ew CREATE TABLE 
22470 73 74 61 74 65 6d 65 6e 74 2e 20 46 6f 72 20 65  statement. For e
22471 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  xample:.**.**   
22472 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 70 61  sqlite_rename_pa
22473 72 65 6e 74 28 27 43 52 45 41 54 45 20 54 41 42  rent('CREATE TAB
22474 4c 45 20 74 31 28 61 20 52 45 46 45 52 45 4e 43  LE t1(a REFERENC
22475 45 53 20 74 32 29 27 2c 20 27 74 32 27 2c 20 27  ES t2)', 't2', '
22476 74 33 27 29 0a 2a 2a 20 20 20 20 20 20 20 2d 3e  t3').**       ->
22477 20 27 43 52 45 41 54 45 20 54 41 42 4c 45 20 74   'CREATE TABLE t
22478 31 28 61 20 52 45 46 45 52 45 4e 43 45 53 20 74  1(a REFERENCES t
22479 33 29 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  3)'.*/.#ifndef S
2247a 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
2247b 47 4e 5f 4b 45 59 0a 73 74 61 74 69 63 20 76 6f  GN_KEY.static vo
2247c 69 64 20 72 65 6e 61 6d 65 50 61 72 65 6e 74 46  id renameParentF
2247d 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
2247e 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
2247f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
22480 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
22481 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69  **argv.){.  sqli
22482 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
22483 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
22484 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
22485 63 68 61 72 20 2a 7a 4f 75 74 70 75 74 20 3d 20  char *zOutput = 
22486 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65 73 75  0;.  char *zResu
22487 6c 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  lt;.  unsigned c
22488 68 61 72 20 63 6f 6e 73 74 20 2a 7a 49 6e 70 75  har const *zInpu
22489 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
2248a 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
2248b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
2248c 20 63 6f 6e 73 74 20 2a 7a 4f 6c 64 20 3d 20 73   const *zOld = s
2248d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2248e 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 75 6e  t(argv[1]);.  un
2248f 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
22490 74 20 2a 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65  t *zNew = sqlite
22491 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
22492 76 5b 32 5d 29 3b 0a 0a 20 20 75 6e 73 69 67 6e  v[2]);..  unsign
22493 65 64 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ed const char *z
22494 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  ;         /* Poi
22495 6e 74 65 72 20 74 6f 20 74 6f 6b 65 6e 20 2a 2f  nter to token */
22496 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20  .  int n;       
22497 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22498 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
22499 74 6f 6b 65 6e 20 7a 20 2a 2f 0a 20 20 69 6e 74  token z */.  int
2249a 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20   token;         
2249b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2249c 54 79 70 65 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  Type of token */
2249d 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
2249e 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
2249f 20 66 6f 72 28 7a 3d 7a 49 6e 70 75 74 3b 20 2a   for(z=zInput; *
224a0 7a 3b 20 7a 3d 7a 2b 6e 29 7b 0a 20 20 20 20 6e  z; z=z+n){.    n
224a1 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b   = sqlite3GetTok
224a2 65 6e 28 7a 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20  en(z, &token);. 
224a3 20 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b     if( token==TK
224a4 5f 52 45 46 45 52 45 4e 43 45 53 20 29 7b 0a 20  _REFERENCES ){. 
224a5 20 20 20 20 20 63 68 61 72 20 2a 7a 50 61 72 65       char *zPare
224a6 6e 74 3b 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20  nt;.      do {. 
224a7 20 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20         z += n;. 
224a8 20 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74         n = sqlit
224a9 65 33 47 65 74 54 6f 6b 65 6e 28 7a 2c 20 26 74  e3GetToken(z, &t
224aa 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 77 68  oken);.      }wh
224ab 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53  ile( token==TK_S
224ac 50 41 43 45 20 29 3b 0a 0a 20 20 20 20 20 20 7a  PACE );..      z
224ad 50 61 72 65 6e 74 20 3d 20 73 71 6c 69 74 65 33  Parent = sqlite3
224ae 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63  DbStrNDup(db, (c
224af 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a 2c 20 6e  onst char *)z, n
224b0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50 61  );.      if( zPa
224b1 72 65 6e 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b  rent==0 ) break;
224b2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65  .      sqlite3De
224b3 71 75 6f 74 65 28 7a 50 61 72 65 6e 74 29 3b 0a  quote(zParent);.
224b4 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c        if( 0==sql
224b5 69 74 65 33 53 74 72 49 43 6d 70 28 28 63 6f 6e  ite3StrICmp((con
224b6 73 74 20 63 68 61 72 20 2a 29 7a 4f 6c 64 2c 20  st char *)zOld, 
224b7 7a 50 61 72 65 6e 74 29 20 29 7b 0a 20 20 20 20  zParent) ){.    
224b8 20 20 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 3d      char *zOut =
224b9 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
224ba 64 62 2c 20 22 25 73 25 2e 2a 73 5c 22 25 77 5c  db, "%s%.*s\"%w\
224bb 22 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  "", .           
224bc 20 28 7a 4f 75 74 70 75 74 3f 7a 4f 75 74 70 75   (zOutput?zOutpu
224bd 74 3a 22 22 29 2c 20 7a 2d 7a 49 6e 70 75 74 2c  t:""), z-zInput,
224be 20 7a 49 6e 70 75 74 2c 20 28 63 6f 6e 73 74 20   zInput, (const 
224bf 63 68 61 72 20 2a 29 7a 4e 65 77 0a 20 20 20 20  char *)zNew.    
224c0 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 73      );.        s
224c1 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
224c2 20 7a 4f 75 74 70 75 74 29 3b 0a 20 20 20 20 20   zOutput);.     
224c3 20 20 20 7a 4f 75 74 70 75 74 20 3d 20 7a 4f 75     zOutput = zOu
224c4 74 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e 70 75  t;.        zInpu
224c5 74 20 3d 20 26 7a 5b 6e 5d 3b 0a 20 20 20 20 20  t = &z[n];.     
224c6 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
224c7 44 62 46 72 65 65 28 64 62 2c 20 7a 50 61 72 65  DbFree(db, zPare
224c8 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  nt);.    }.  }..
224c9 20 20 7a 52 65 73 75 6c 74 20 3d 20 73 71 6c 69    zResult = sqli
224ca 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
224cb 25 73 25 73 22 2c 20 28 7a 4f 75 74 70 75 74 3f  %s%s", (zOutput?
224cc 7a 4f 75 74 70 75 74 3a 22 22 29 2c 20 7a 49 6e  zOutput:""), zIn
224cd 70 75 74 29 2c 20 0a 20 20 73 71 6c 69 74 65 33  put), .  sqlite3
224ce 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
224cf 74 65 78 74 2c 20 7a 52 65 73 75 6c 74 2c 20 2d  text, zResult, -
224d0 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  1, SQLITE_DYNAMI
224d1 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  C);.  sqlite3DbF
224d2 72 65 65 28 64 62 2c 20 7a 4f 75 74 70 75 74 29  ree(db, zOutput)
224d3 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ;.}.#endif..#ifn
224d4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
224d5 54 52 49 47 47 45 52 0a 2f 2a 20 54 68 69 73 20  TRIGGER./* This 
224d6 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
224d7 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65   by SQL generate
224d8 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
224d9 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c  he.** ALTER TABL
224da 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66  E command. The f
224db 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
224dc 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 43   the text of a C
224dd 52 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 2a  REATE TRIGGER .*
224de 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65  * statement. The
224df 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62   second is a tab
224e0 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62  le name. The tab
224e1 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43  le name in the C
224e2 52 45 41 54 45 20 0a 2a 2a 20 54 52 49 47 47 45  REATE .** TRIGGE
224e3 52 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72  R statement is r
224e4 65 70 6c 61 63 65 64 20 77 69 74 68 20 74 68 65  eplaced with the
224e5 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20   third argument 
224e6 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 0a  and the result .
224e7 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69  ** returned. Thi
224e8 73 20 69 73 20 61 6e 61 6c 61 67 6f 75 73 20 74  s is analagous t
224e9 6f 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e  o renameTableFun
224ea 63 28 29 20 61 62 6f 76 65 2c 20 65 78 63 65 70  c() above, excep
224eb 74 20 66 6f 72 20 43 52 45 41 54 45 0a 2a 2a 20  t for CREATE.** 
224ec 54 52 49 47 47 45 52 2c 20 6e 6f 74 20 43 52 45  TRIGGER, not CRE
224ed 41 54 45 20 49 4e 44 45 58 20 61 6e 64 20 43 52  ATE INDEX and CR
224ee 45 41 54 45 20 54 41 42 4c 45 2e 0a 2a 2f 0a 73  EATE TABLE..*/.s
224ef 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d  tatic void renam
224f0 65 54 72 69 67 67 65 72 46 75 6e 63 28 0a 20 20  eTriggerFunc(.  
224f1 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
224f2 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
224f3 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
224f4 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
224f5 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
224f6 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d  ar const *zSql =
224f7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
224f8 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
224f9 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
224fa 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20  nst *zTableName 
224fb 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
224fc 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a  text(argv[1]);..
224fd 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54    int token;.  T
224fe 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 69 6e  oken tname;.  in
224ff 74 20 64 69 73 74 20 3d 20 33 3b 0a 20 20 75 6e  t dist = 3;.  un
22500 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
22501 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a  t *zCsr = zSql;.
22502 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20    int len = 0;. 
22503 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 20 20 73   char *zRet;.  s
22504 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
22505 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
22506 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
22507 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
22508 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a  ETER(NotUsed);..
22509 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70    /* The princip
2250a 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74  le used to locat
2250b 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  e the table name
2250c 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54   in the CREATE T
2250d 52 49 47 47 45 52 20 0a 20 20 2a 2a 20 73 74 61  RIGGER .  ** sta
2250e 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74  tement is that t
2250f 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73  he table name is
22510 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e   the first token
22511 20 74 68 61 74 20 69 73 20 69 6d 6d 65 64 69 61   that is immedia
22512 74 65 64 6c 79 0a 20 20 2a 2a 20 70 72 65 63 65  tedly.  ** prece
22513 64 65 64 20 62 79 20 65 69 74 68 65 72 20 54 4b  ded by either TK
22514 5f 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54 20 61 6e  _ON or TK_DOT an
22515 64 20 69 6d 6d 65 64 69 61 74 65 64 6c 79 20 66  d immediatedly f
22516 6f 6c 6c 6f 77 65 64 20 62 79 20 6f 6e 65 0a 20  ollowed by one. 
22517 20 2a 2a 20 6f 66 20 54 4b 5f 57 48 45 4e 2c 20   ** of TK_WHEN, 
22518 54 4b 5f 42 45 47 49 4e 20 6f 72 20 54 4b 5f 46  TK_BEGIN or TK_F
22519 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a  OR..  */.  if( z
2251a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a  Sql ){.    do {.
2251b 0a 20 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73  .      if( !*zCs
2251c 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  r ){.        /* 
2251d 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74  Ran out of input
2251e 20 62 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20   before finding 
2251f 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  the table name. 
22520 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a  Return NULL. */.
22521 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
22522 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
22523 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65  * Store the toke
22524 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e  n that zCsr poin
22525 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20  ts to in tname. 
22526 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a  */.      tname.z
22527 20 3d 20 28 63 68 61 72 2a 29 7a 43 73 72 3b 0a   = (char*)zCsr;.
22528 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d 20        tname.n = 
22529 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41  len;..      /* A
2252a 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f 20 74  dvance zCsr to t
2252b 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53  he next token. S
2252c 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 6e 20  tore that token 
2252d 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e 27 2c  type in 'token',
2252e 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 69 74  .      ** and it
2252f 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e  s length in 'len
22530 27 20 28 74 6f 20 62 65 20 75 73 65 64 20 6e 65  ' (to be used ne
22531 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  xt iteration of 
22532 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 20  this loop)..    
22533 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a    */.      do {.
22534 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20          zCsr += 
22535 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e  len;.        len
22536 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b   = sqlite3GetTok
22537 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e 29  en(zCsr, &token)
22538 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20  ;.      }while( 
22539 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20  token==TK_SPACE 
2253a 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2253b 20 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20 20 20 20   len>0 );..     
2253c 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 27 64 69   /* Variable 'di
2253d 73 74 27 20 73 74 6f 72 65 73 20 74 68 65 20 6e  st' stores the n
2253e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20  umber of tokens 
2253f 72 65 61 64 20 73 69 6e 63 65 20 74 68 65 20 6d  read since the m
22540 6f 73 74 0a 20 20 20 20 20 20 2a 2a 20 72 65 63  ost.      ** rec
22541 65 6e 74 20 54 4b 5f 44 4f 54 20 6f 72 20 54 4b  ent TK_DOT or TK
22542 5f 4f 4e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  _ON. This means 
22543 74 68 61 74 20 77 68 65 6e 20 61 20 57 48 45 4e  that when a WHEN
22544 2c 20 46 4f 52 20 6f 72 20 42 45 47 49 4e 20 0a  , FOR or BEGIN .
22545 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69        ** token i
22546 73 20 72 65 61 64 20 61 6e 64 20 27 64 69 73 74  s read and 'dist
22547 27 20 65 71 75 61 6c 73 20 32 2c 20 74 68 65 20  ' equals 2, the 
22548 63 6f 6e 64 69 74 69 6f 6e 20 73 74 61 74 65 64  condition stated
22549 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20   above.      ** 
2254a 74 6f 20 62 65 20 6d 65 74 2e 0a 20 20 20 20 20  to be met..     
2254b 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74   **.      ** Not
2254c 65 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f 74  e that ON cannot
2254d 20 62 65 20 61 20 64 61 74 61 62 61 73 65 2c 20   be a database, 
2254e 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20  table or column 
2254f 6e 61 6d 65 2c 20 73 6f 0a 20 20 20 20 20 20 2a  name, so.      *
22550 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  * there is no ne
22551 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75  ed to worry abou
22552 74 20 73 79 6e 74 61 78 20 6c 69 6b 65 20 0a 20  t syntax like . 
22553 20 20 20 20 20 2a 2a 20 22 43 52 45 41 54 45 20       ** "CREATE 
22554 54 52 49 47 47 45 52 20 2e 2e 2e 20 4f 4e 20 4f  TRIGGER ... ON O
22555 4e 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e 22 20  N.ON BEGIN ..." 
22556 65 74 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  etc..      */.  
22557 20 20 20 20 64 69 73 74 2b 2b 3b 0a 20 20 20 20      dist++;.    
22558 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f    if( token==TK_
22559 44 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54 4b  DOT || token==TK
2255a 5f 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 64  _ON ){.        d
2255b 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ist = 0;.      }
2255c 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20 64 69  .    } while( di
2255d 73 74 21 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e 21  st!=2 || (token!
2255e 3d 54 4b 5f 57 48 45 4e 20 26 26 20 74 6f 6b 65  =TK_WHEN && toke
2255f 6e 21 3d 54 4b 5f 46 4f 52 20 26 26 20 74 6f 6b  n!=TK_FOR && tok
22560 65 6e 21 3d 54 4b 5f 42 45 47 49 4e 29 20 29 3b  en!=TK_BEGIN) );
22561 0a 0a 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c  ..    /* Variabl
22562 65 20 74 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e 74  e tname now cont
22563 61 69 6e 73 20 74 68 65 20 74 6f 6b 65 6e 20 74  ains the token t
22564 68 61 74 20 69 73 20 74 68 65 20 6f 6c 64 20 74  hat is the old t
22565 61 62 6c 65 2d 6e 61 6d 65 0a 20 20 20 20 2a 2a  able-name.    **
22566 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54   in the CREATE T
22567 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
22568 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 52 65  ..    */.    zRe
22569 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
2256a 74 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77  tf(db, "%.*s\"%w
2256b 5c 22 25 73 22 2c 20 28 28 75 38 2a 29 74 6e 61  \"%s", ((u8*)tna
2256c 6d 65 2e 7a 29 20 2d 20 7a 53 71 6c 2c 20 7a 53  me.z) - zSql, zS
2256d 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 62  ql, .       zTab
2256e 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b  leName, tname.z+
2256f 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 71  tname.n);.    sq
22570 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
22571 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c  t(context, zRet,
22572 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41   -1, SQLITE_DYNA
22573 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  MIC);.  }.}.#end
22574 69 66 20 20 20 2f 2a 20 21 53 51 4c 49 54 45 5f  if   /* !SQLITE_
22575 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
22576 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
22577 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f  built-in functio
22578 6e 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20  ns used to help 
22579 69 6d 70 6c 65 6d 65 6e 74 20 41 4c 54 45 52 20  implement ALTER 
2257a 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c 49 54 45 5f  TABLE.*/.SQLITE_
2257b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2257c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f  ite3AlterFunctio
2257d 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ns(sqlite3 *db){
2257e 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  .  sqlite3Create
2257f 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65  Func(db, "sqlite
22580 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 22 2c 20  _rename_table", 
22581 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  2, SQLITE_UTF8, 
22582 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
22583 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e 61              rena
22584 6d 65 54 61 62 6c 65 46 75 6e 63 2c 20 30 2c 20  meTableFunc, 0, 
22585 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0);.#ifndef SQLI
22586 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
22587 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46    sqlite3CreateF
22588 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65 5f  unc(db, "sqlite_
22589 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 22 2c  rename_trigger",
2258a 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
2258b 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
2258c 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e               ren
2258d 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63 2c 20  ameTriggerFunc, 
2258e 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 23 69  0, 0);.#endif.#i
2258f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22590 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20  T_FOREIGN_KEY.  
22591 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
22592 63 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 72 65  c(db, "sqlite_re
22593 6e 61 6d 65 5f 70 61 72 65 6e 74 22 2c 20 33 2c  name_parent", 3,
22594 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c   SQLITE_UTF8, 0,
22595 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22596 20 20 20 20 20 20 20 20 20 20 72 65 6e 61 6d 65            rename
22597 50 61 72 65 6e 74 46 75 6e 63 2c 20 30 2c 20 30  ParentFunc, 0, 0
22598 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  );.#endif.}../*.
22599 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2259a 20 69 73 20 75 73 65 64 20 74 6f 20 63 72 65 61   is used to crea
2259b 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 20 65  te the text of e
2259c 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68  xpressions of th
2259d 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  e form:.**.**   
2259e 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e 74 31 3e  name=<constant1>
2259f 20 4f 52 20 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61   OR name=<consta
225a0 6e 74 32 3e 20 4f 52 20 2e 2e 2e 0a 2a 2a 0a 2a  nt2> OR ....**.*
225a1 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 57  * If argument zW
225a2 68 65 72 65 20 69 73 20 4e 55 4c 4c 2c 20 74 68  here is NULL, th
225a3 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 73 74 72  en a pointer str
225a4 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ing containing t
225a5 68 65 20 74 65 78 74 20 0a 2a 2a 20 22 6e 61 6d  he text .** "nam
225a6 65 3d 3c 63 6f 6e 73 74 61 6e 74 3e 22 20 69 73  e=<constant>" is
225a7 20 72 65 74 75 72 6e 65 64 2c 20 77 68 65 72 65   returned, where
225a8 20 3c 63 6f 6e 73 74 61 6e 74 3e 20 69 73 20 74   <constant> is t
225a9 68 65 20 71 75 6f 74 65 64 20 76 65 72 73 69 6f  he quoted versio
225aa 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69  n.** of the stri
225ab 6e 67 20 70 61 73 73 65 64 20 61 73 20 61 72 67  ng passed as arg
225ac 75 6d 65 6e 74 20 7a 43 6f 6e 73 74 61 6e 74 2e  ument zConstant.
225ad 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 62 75   The returned bu
225ae 66 66 65 72 20 69 73 0a 2a 2a 20 61 6c 6c 6f 63  ffer is.** alloc
225af 61 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74  ated using sqlit
225b0 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2e 20 49 74  e3DbMalloc(). It
225b1 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69   is the responsi
225b2 62 69 6c 69 74 79 20 6f 66 20 74 68 65 0a 2a 2a  bility of the.**
225b3 20 63 61 6c 6c 65 72 20 74 6f 20 65 6e 73 75 72   caller to ensur
225b4 65 20 74 68 61 74 20 69 74 20 69 73 20 65 76 65  e that it is eve
225b5 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a  ntually freed..*
225b6 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74  *.** If argument
225b7 20 7a 57 68 65 72 65 20 69 73 20 6e 6f 74 20 4e   zWhere is not N
225b8 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 73 74  ULL, then the st
225b9 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20 69 73  ring returned is
225ba 20 0a 2a 2a 20 22 3c 77 68 65 72 65 3e 20 4f 52   .** "<where> OR
225bb 20 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e 74 3e   name=<constant>
225bc 22 2c 20 77 68 65 72 65 20 3c 77 68 65 72 65 3e  ", where <where>
225bd 20 69 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73   is the contents
225be 20 6f 66 20 7a 57 68 65 72 65 2e 0a 2a 2a 20 49   of zWhere..** I
225bf 6e 20 74 68 69 73 20 63 61 73 65 20 7a 57 68 65  n this case zWhe
225c0 72 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20  re is passed to 
225c1 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20  sqlite3DbFree() 
225c2 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
225c3 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74 69 63 20  ..** .*/.static 
225c4 63 68 61 72 20 2a 77 68 65 72 65 4f 72 4e 61 6d  char *whereOrNam
225c5 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  e(sqlite3 *db, c
225c6 68 61 72 20 2a 7a 57 68 65 72 65 2c 20 63 68 61  har *zWhere, cha
225c7 72 20 2a 7a 43 6f 6e 73 74 61 6e 74 29 7b 0a 20  r *zConstant){. 
225c8 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 69   char *zNew;.  i
225c9 66 28 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20  f( !zWhere ){.  
225ca 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    zNew = sqlite3
225cb 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d  MPrintf(db, "nam
225cc 65 3d 25 51 22 2c 20 7a 43 6f 6e 73 74 61 6e 74  e=%Q", zConstant
225cd 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
225ce 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 50  zNew = sqlite3MP
225cf 72 69 6e 74 66 28 64 62 2c 20 22 25 73 20 4f 52  rintf(db, "%s OR
225d0 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65 72   name=%Q", zWher
225d1 65 2c 20 7a 43 6f 6e 73 74 61 6e 74 29 3b 0a 20  e, zConstant);. 
225d2 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
225d3 28 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20  (db, zWhere);.  
225d4 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b  }.  return zNew;
225d5 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .}..#if !defined
225d6 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52  (SQLITE_OMIT_FOR
225d7 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21 64 65  EIGN_KEY) && !de
225d8 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
225d9 54 5f 54 52 49 47 47 45 52 29 0a 2f 2a 0a 2a 2a  T_TRIGGER)./*.**
225da 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65   Generate the te
225db 78 74 20 6f 66 20 61 20 57 48 45 52 45 20 65 78  xt of a WHERE ex
225dc 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63  pression which c
225dd 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  an be used to se
225de 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 61 62 6c  lect all.** tabl
225df 65 73 20 74 68 61 74 20 68 61 76 65 20 66 6f 72  es that have for
225e0 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
225e1 69 6e 74 73 20 74 68 61 74 20 72 65 66 65 72 20  ints that refer 
225e2 74 6f 20 74 61 62 6c 65 20 70 54 61 62 20 28 69  to table pTab (i
225e3 2e 65 2e 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e  .e..** constrain
225e4 74 73 20 66 6f 72 20 77 68 69 63 68 20 70 54 61  ts for which pTa
225e5 62 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20  b is the parent 
225e6 74 61 62 6c 65 29 20 66 72 6f 6d 20 74 68 65 20  table) from the 
225e7 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 0a 2a 2a  sqlite_master.**
225e8 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
225e9 63 20 63 68 61 72 20 2a 77 68 65 72 65 46 6f 72  c char *whereFor
225ea 65 69 67 6e 4b 65 79 73 28 50 61 72 73 65 20 2a  eignKeys(Parse *
225eb 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
225ec 54 61 62 29 7b 0a 20 20 46 4b 65 79 20 2a 70 3b  Tab){.  FKey *p;
225ed 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20  .  char *zWhere 
225ee 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c  = 0;.  for(p=sql
225ef 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
225f0 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70 2d 3e  (pTab); p; p=p->
225f1 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20 7a 57  pNextTo){.    zW
225f2 68 65 72 65 20 3d 20 77 68 65 72 65 4f 72 4e 61  here = whereOrNa
225f3 6d 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  me(pParse->db, z
225f4 57 68 65 72 65 2c 20 70 2d 3e 70 46 72 6f 6d 2d  Where, p->pFrom-
225f5 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 72  >zName);.  }.  r
225f6 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a 7d 0a  eturn zWhere;.}.
225f7 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65  #endif../*.** Ge
225f8 6e 65 72 61 74 65 20 74 68 65 20 74 65 78 74 20  nerate the text 
225f9 6f 66 20 61 20 57 48 45 52 45 20 65 78 70 72 65  of a WHERE expre
225fa 73 73 69 6f 6e 20 77 68 69 63 68 20 63 61 6e 20  ssion which can 
225fb 62 65 20 75 73 65 64 20 74 6f 20 73 65 6c 65 63  be used to selec
225fc 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70 6f 72 61  t all.** tempora
225fd 72 79 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74  ry triggers on t
225fe 61 62 6c 65 20 70 54 61 62 20 66 72 6f 6d 20 74  able pTab from t
225ff 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d  he sqlite_temp_m
22600 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 66 0a  aster table. If.
22601 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 68 61  ** table pTab ha
22602 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72 79 20 74  s no temporary t
22603 72 69 67 67 65 72 73 2c 20 6f 72 20 69 73 20 69  riggers, or is i
22604 74 73 65 6c 66 20 73 74 6f 72 65 64 20 69 6e 20  tself stored in 
22605 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 72  the .** temporar
22606 79 20 64 61 74 61 62 61 73 65 2c 20 4e 55 4c 4c  y database, NULL
22607 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
22608 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 77 68  .static char *wh
22609 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 28  ereTempTriggers(
2260a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
2260b 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 54  able *pTab){.  T
2260c 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 0a 20  rigger *pTrig;. 
2260d 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 3d 20   char *zWhere = 
2260e 30 3b 0a 20 20 63 6f 6e 73 74 20 53 63 68 65 6d  0;.  const Schem
2260f 61 20 2a 70 54 65 6d 70 53 63 68 65 6d 61 20 3d  a *pTempSchema =
22610 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62   pParse->db->aDb
22611 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20 2f 2a 20  [1].pSchema; /* 
22612 54 65 6d 70 20 64 62 20 73 63 68 65 6d 61 20 2a  Temp db schema *
22613 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74  /..  /* If the t
22614 61 62 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63 61  able is not loca
22615 74 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70 2d  ted in the temp-
22616 64 62 20 28 69 6e 20 77 68 69 63 68 20 63 61 73  db (in which cas
22617 65 20 4e 55 4c 4c 20 69 73 20 0a 20 20 2a 2a 20  e NULL is .  ** 
22618 72 65 74 75 72 6e 65 64 2c 20 6c 6f 6f 70 20 74  returned, loop t
22619 68 72 6f 75 67 68 20 74 68 65 20 74 61 62 6c 65  hrough the table
2261a 73 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65  s list of trigge
2261b 72 73 2e 20 46 6f 72 20 65 61 63 68 20 74 72 69  rs. For each tri
2261c 67 67 65 72 0a 20 20 2a 2a 20 74 68 61 74 20 69  gger.  ** that i
2261d 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68  s not part of th
2261e 65 20 74 65 6d 70 2d 64 62 20 73 63 68 65 6d 61  e temp-db schema
2261f 2c 20 61 64 64 20 61 20 63 6c 61 75 73 65 20 74  , add a clause t
22620 6f 20 74 68 65 20 57 48 45 52 45 20 0a 20 20 2a  o the WHERE .  *
22621 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 62 65 69  * expression bei
22622 6e 67 20 62 75 69 6c 74 20 75 70 20 69 6e 20 7a  ng built up in z
22623 57 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  Where..  */.  if
22624 28 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 21  ( pTab->pSchema!
22625 3d 70 54 65 6d 70 53 63 68 65 6d 61 20 29 7b 0a  =pTempSchema ){.
22626 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
22627 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
22628 20 20 66 6f 72 28 70 54 72 69 67 3d 73 71 6c 69    for(pTrig=sqli
22629 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70  te3TriggerList(p
2262a 50 61 72 73 65 2c 20 70 54 61 62 29 3b 20 70 54  Parse, pTab); pT
2262b 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72 69 67  rig; pTrig=pTrig
2262c 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
2262d 69 66 28 20 70 54 72 69 67 2d 3e 70 53 63 68 65  if( pTrig->pSche
2262e 6d 61 3d 3d 70 54 65 6d 70 53 63 68 65 6d 61 20  ma==pTempSchema 
2262f 29 7b 0a 20 20 20 20 20 20 20 20 7a 57 68 65 72  ){.        zWher
22630 65 20 3d 20 77 68 65 72 65 4f 72 4e 61 6d 65 28  e = whereOrName(
22631 64 62 2c 20 7a 57 68 65 72 65 2c 20 70 54 72 69  db, zWhere, pTri
22632 67 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  g->zName);.     
22633 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
22634 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a 7d 0a  eturn zWhere;.}.
22635 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
22636 63 6f 64 65 20 74 6f 20 64 72 6f 70 20 61 6e 64  code to drop and
22637 20 72 65 6c 6f 61 64 20 74 68 65 20 69 6e 74 65   reload the inte
22638 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74  rnal representat
22639 69 6f 6e 20 6f 66 20 74 61 62 6c 65 0a 2a 2a 20  ion of table.** 
2263a 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20 64 61  pTab from the da
2263b 74 61 62 61 73 65 2c 20 69 6e 63 6c 75 64 69 6e  tabase, includin
2263c 67 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 74  g triggers and t
2263d 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72  emporary trigger
2263e 73 2e 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a  s..** Argument z
2263f 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  Name is the name
22640 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e   of the table in
22641 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
22642 68 65 6d 61 20 61 74 0a 2a 2a 20 74 68 65 20 74  hema at.** the t
22643 69 6d 65 20 74 68 65 20 67 65 6e 65 72 61 74 65  ime the generate
22644 64 20 63 6f 64 65 20 69 73 20 65 78 65 63 75 74  d code is execut
22645 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20  ed. This can be 
22646 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a  different from.*
22647 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 69 66  * pTab->zName if
22648 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
22649 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 74  s being called t
2264a 6f 20 63 6f 64 65 20 70 61 72 74 20 6f 66 20 61  o code part of a
2264b 6e 20 0a 2a 2a 20 22 41 4c 54 45 52 20 54 41 42  n .** "ALTER TAB
2264c 4c 45 20 52 45 4e 41 4d 45 20 54 4f 22 20 73 74  LE RENAME TO" st
2264d 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74  atement..*/.stat
2264e 69 63 20 76 6f 69 64 20 72 65 6c 6f 61 64 54 61  ic void reloadTa
2264f 62 6c 65 53 63 68 65 6d 61 28 50 61 72 73 65 20  bleSchema(Parse 
22650 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
22651 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72  pTab, const char
22652 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62 65   *zName){.  Vdbe
22653 20 2a 76 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68   *v;.  char *zWh
22654 65 72 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20  ere;.  int iDb; 
22655 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22656 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61    /* Index of da
22657 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
22658 67 20 70 54 61 62 20 2a 2f 0a 23 69 66 6e 64 65  g pTab */.#ifnde
22659 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2265a 49 47 47 45 52 0a 20 20 54 72 69 67 67 65 72 20  IGGER.  Trigger 
2265b 2a 70 54 72 69 67 3b 0a 23 65 6e 64 69 66 0a 0a  *pTrig;.#endif..
2265c 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
2265d 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
2265e 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20  if( NEVER(v==0) 
2265f 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
22660 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
22661 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
22662 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20  pParse->db) );. 
22663 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
22664 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
22665 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
22666 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74  chema);.  assert
22667 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 23 69 66  ( iDb>=0 );..#if
22668 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22669 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 44 72  _TRIGGER.  /* Dr
2266a 6f 70 20 61 6e 79 20 74 61 62 6c 65 20 74 72 69  op any table tri
2266b 67 67 65 72 73 20 66 72 6f 6d 20 74 68 65 20 69  ggers from the i
2266c 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20  nternal schema. 
2266d 2a 2f 0a 20 20 66 6f 72 28 70 54 72 69 67 3d 73  */.  for(pTrig=s
2266e 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73  qlite3TriggerLis
2266f 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  t(pParse, pTab);
22670 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54   pTrig; pTrig=pT
22671 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  rig->pNext){.   
22672 20 69 6e 74 20 69 54 72 69 67 44 62 20 3d 20 73   int iTrigDb = s
22673 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
22674 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
22675 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b  pTrig->pSchema);
22676 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 54 72  .    assert( iTr
22677 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20 69 54 72  igDb==iDb || iTr
22678 69 67 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 73  igDb==1 );.    s
22679 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2267a 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67  (v, OP_DropTrigg
2267b 65 72 2c 20 69 54 72 69 67 44 62 2c 20 30 2c 20  er, iTrigDb, 0, 
2267c 30 2c 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 2c  0, pTrig->zName,
2267d 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a   0);.  }.#endif.
2267e 0a 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74  .  /* Drop the t
2267f 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 66  able and index f
22680 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  rom the internal
22681 20 73 63 68 65 6d 61 2e 20 20 2a 2f 0a 20 20 73   schema.  */.  s
22682 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
22683 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65  (v, OP_DropTable
22684 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61  , iDb, 0, 0, pTa
22685 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 20  b->zName, 0);.. 
22686 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 74   /* Reload the t
22687 61 62 6c 65 2c 20 69 6e 64 65 78 20 61 6e 64 20  able, index and 
22688 70 65 72 6d 61 6e 65 6e 74 20 74 72 69 67 67 65  permanent trigge
22689 72 20 73 63 68 65 6d 61 73 2e 20 2a 2f 0a 20 20  r schemas. */.  
2268a 7a 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  zWhere = sqlite3
2268b 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e  MPrintf(pParse->
2268c 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 25 51  db, "tbl_name=%Q
2268d 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  ", zName);.  if(
2268e 20 21 7a 57 68 65 72 65 20 29 20 72 65 74 75 72   !zWhere ) retur
2268f 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  n;.  sqlite3Vdbe
22690 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72  AddOp4(v, OP_Par
22691 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30  seSchema, iDb, 0
22692 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f  , 0, zWhere, P4_
22693 44 59 4e 41 4d 49 43 29 3b 0a 0a 23 69 66 6e 64  DYNAMIC);..#ifnd
22694 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
22695 52 49 47 47 45 52 0a 20 20 2f 2a 20 4e 6f 77 2c  RIGGER.  /* Now,
22696 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   if the table is
22697 20 6e 6f 74 20 73 74 6f 72 65 64 20 69 6e 20 74   not stored in t
22698 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65  he temp database
22699 2c 20 72 65 6c 6f 61 64 20 61 6e 79 20 74 65 6d  , reload any tem
2269a 70 20 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 73  p .  ** triggers
2269b 2e 20 44 6f 6e 27 74 20 75 73 65 20 49 4e 28 2e  . Don't use IN(.
2269c 2e 2e 29 20 69 6e 20 63 61 73 65 20 53 51 4c 49  ..) in case SQLI
2269d 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
2269e 20 69 73 20 64 65 66 69 6e 65 64 2e 20 0a 20 20   is defined. .  
2269f 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65 72 65  */.  if( (zWhere
226a0 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65  =whereTempTrigge
226a1 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  rs(pParse, pTab)
226a2 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )!=0 ){.    sqli
226a3 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
226a4 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c   OP_ParseSchema,
226a5 20 31 2c 20 30 2c 20 30 2c 20 7a 57 68 65 72 65   1, 0, 0, zWhere
226a6 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  , P4_DYNAMIC);. 
226a7 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
226a8 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
226a9 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
226aa 65 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 78  e "ALTER TABLE x
226ab 78 78 20 52 45 4e 41 4d 45 20 54 4f 20 79 79 79  xx RENAME TO yyy
226ac 22 20 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 0a  " .** command. .
226ad 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
226ae 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
226af 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 0a  terRenameTable(.
226b0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
226b1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
226b2 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 2a  arser context. *
226b3 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
226b4 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  c,            /*
226b5 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 72 65   The table to re
226b6 6e 61 6d 65 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  name. */.  Token
226b7 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20   *pName         
226b8 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
226b9 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 29  table name. */.)
226ba 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  {.  int iDb;    
226bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
226bc 20 44 61 74 61 62 61 73 65 20 74 68 61 74 20 63   Database that c
226bd 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 61 62 6c  ontains the tabl
226be 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62  e */.  char *zDb
226bf 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
226c0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61   /* Name of data
226c1 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20 54 61  base iDb */.  Ta
226c2 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
226c3 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
226c4 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 2a   being renamed *
226c5 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  /.  char *zName 
226c6 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
226c7 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64   NULL-terminated
226c8 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 4e 61 6d   version of pNam
226c9 65 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 20  e */ .  sqlite3 
226ca 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
226cb 3b 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f  ; /* Database co
226cc 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
226cd 74 20 6e 54 61 62 4e 61 6d 65 3b 20 20 20 20 20  t nTabName;     
226ce 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
226cf 72 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61  r of UTF-8 chara
226d0 63 74 65 72 73 20 69 6e 20 7a 54 61 62 4e 61 6d  cters in zTabNam
226d1 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
226d2 72 20 2a 7a 54 61 62 4e 61 6d 65 3b 20 20 20 20  r *zTabName;    
226d3 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 61 6d   /* Original nam
226d4 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  e of the table *
226d5 2f 0a 20 20 56 64 62 65 20 2a 76 3b 0a 23 69 66  /.  Vdbe *v;.#if
226d6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
226d7 5f 54 52 49 47 47 45 52 0a 20 20 63 68 61 72 20  _TRIGGER.  char 
226d8 2a 7a 57 68 65 72 65 20 3d 20 30 3b 20 20 20 20  *zWhere = 0;    
226d9 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 63 6c       /* Where cl
226da 61 75 73 65 20 74 6f 20 6c 6f 63 61 74 65 20 74  ause to locate t
226db 65 6d 70 20 74 72 69 67 67 65 72 73 20 2a 2f 0a  emp triggers */.
226dc 23 65 6e 64 69 66 0a 20 20 56 54 61 62 6c 65 20  #endif.  VTable 
226dd 2a 70 56 54 61 62 20 3d 20 30 3b 20 20 20 20 20  *pVTab = 0;     
226de 20 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f 20 69     /* Non-zero i
226df 66 20 74 68 69 73 20 69 73 20 61 20 76 2d 74 61  f this is a v-ta
226e0 62 20 77 69 74 68 20 61 6e 20 78 52 65 6e 61 6d  b with an xRenam
226e1 65 28 29 20 2a 2f 0a 20 20 0a 20 20 69 66 28 20  e() */.  .  if( 
226e2 4e 45 56 45 52 28 64 62 2d 3e 6d 61 6c 6c 6f 63  NEVER(db->malloc
226e3 46 61 69 6c 65 64 29 20 29 20 67 6f 74 6f 20 65  Failed) ) goto e
226e4 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
226e5 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63  ;.  assert( pSrc
226e6 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 61  ->nSrc==1 );.  a
226e7 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
226e8 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
226e9 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29  es(pParse->db) )
226ea 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  ;..  pTab = sqli
226eb 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70  te3LocateTable(p
226ec 50 61 72 73 65 2c 20 30 2c 20 70 53 72 63 2d 3e  Parse, 0, pSrc->
226ed 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72 63  a[0].zName, pSrc
226ee 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65  ->a[0].zDatabase
226ef 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 29  );.  if( !pTab )
226f0 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
226f1 65 5f 74 61 62 6c 65 3b 0a 20 20 69 44 62 20 3d  e_table;.  iDb =
226f2 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
226f3 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
226f4 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
226f5 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ;.  zDb = db->aD
226f6 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20  b[iDb].zName;.. 
226f7 20 2f 2a 20 47 65 74 20 61 20 4e 55 4c 4c 20 74   /* Get a NULL t
226f8 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f  erminated versio
226f9 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  n of the new tab
226fa 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a 4e  le name. */.  zN
226fb 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ame = sqlite3Nam
226fc 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
226fd 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 4e  Name);.  if( !zN
226fe 61 6d 65 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  ame ) goto exit_
226ff 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 0a 20  rename_table;.. 
22700 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61   /* Check that a
22701 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
22702 6e 61 6d 65 64 20 27 7a 4e 61 6d 65 27 20 64 6f  named 'zName' do
22703 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65  es not already e
22704 78 69 73 74 0a 20 20 2a 2a 20 69 6e 20 64 61 74  xist.  ** in dat
22705 61 62 61 73 65 20 69 44 62 2e 20 49 66 20 73 6f  abase iDb. If so
22706 2c 20 74 68 69 73 20 69 73 20 61 6e 20 65 72 72  , this is an err
22707 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  or..  */.  if( s
22708 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
22709 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 20  db, zName, zDb) 
2270a 7c 7c 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e  || sqlite3FindIn
2270b 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a  dex(db, zName, z
2270c 44 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Db) ){.    sqlit
2270d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2270e 65 2c 20 0a 20 20 20 20 20 20 20 20 22 74 68 65  e, .        "the
2270f 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e  re is already an
22710 6f 74 68 65 72 20 74 61 62 6c 65 20 6f 72 20 69  other table or i
22711 6e 64 65 78 20 77 69 74 68 20 74 68 69 73 20 6e  ndex with this n
22712 61 6d 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29  ame: %s", zName)
22713 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
22714 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
22715 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  }..  /* Make sur
22716 65 20 69 74 20 69 73 20 6e 6f 74 20 61 20 73 79  e it is not a sy
22717 73 74 65 6d 20 74 61 62 6c 65 20 62 65 69 6e 67  stem table being
22718 20 61 6c 74 65 72 65 64 2c 20 6f 72 20 61 20 72   altered, or a r
22719 65 73 65 72 76 65 64 20 6e 61 6d 65 0a 20 20 2a  eserved name.  *
2271a 2a 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65  * that the table
2271b 20 69 73 20 62 65 69 6e 67 20 72 65 6e 61 6d 65   is being rename
2271c 64 20 74 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  d to..  */.  if(
2271d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2271e 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3e 36 20  (pTab->zName)>6 
2271f 0a 20 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65  .   && 0==sqlite
22720 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e  3StrNICmp(pTab->
22721 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22  zName, "sqlite_"
22722 2c 20 37 29 0a 20 20 29 7b 0a 20 20 20 20 73 71  , 7).  ){.    sq
22723 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
22724 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20  arse, "table %s 
22725 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 65 72  may not be alter
22726 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ed", pTab->zName
22727 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
22728 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20  _rename_table;. 
22729 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f   }.  if( SQLITE_
2272a 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b  OK!=sqlite3Check
2272b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73  ObjectName(pPars
2272c 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  e, zName) ){.   
2272d 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
2272e 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69  e_table;.  }..#i
2272f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22730 54 5f 56 49 45 57 0a 20 20 69 66 28 20 70 54 61  T_VIEW.  if( pTa
22731 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  b->pSelect ){.  
22732 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
22733 67 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 20  g(pParse, "view 
22734 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c  %s may not be al
22735 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e  tered", pTab->zN
22736 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ame);.    goto e
22737 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
22738 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
22739 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2273a 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
2273b 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
2273c 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61  authorization ca
2273d 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 28  llback. */.  if(
2273e 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2273f 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
22740 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20 7a 44  _ALTER_TABLE, zD
22741 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  b, pTab->zName, 
22742 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  0) ){.    goto e
22743 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
22744 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
22745 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22746 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
22747 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65 77   if( sqlite3View
22748 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  GetColumnNames(p
22749 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a  Parse, pTab) ){.
2274a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65      goto exit_re
2274b 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  name_table;.  }.
2274c 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
2274d 70 54 61 62 29 20 29 7b 0a 20 20 20 20 70 56 54  pTab) ){.    pVT
2274e 61 62 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  ab = sqlite3GetV
2274f 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b  Table(db, pTab);
22750 0a 20 20 20 20 69 66 28 20 70 56 54 61 62 2d 3e  .    if( pVTab->
22751 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e  pVtab->pModule->
22752 78 52 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  xRename==0 ){.  
22753 20 20 20 20 70 56 54 61 62 20 3d 20 30 3b 0a 20      pVTab = 0;. 
22754 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
22755 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72  .  /* Begin a tr
22756 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 63 6f  ansaction and co
22757 64 65 20 74 68 65 20 56 65 72 69 66 79 43 6f 6f  de the VerifyCoo
22758 6b 69 65 20 66 6f 72 20 64 61 74 61 62 61 73 65  kie for database
22759 20 69 44 62 2e 20 0a 20 20 2a 2a 20 54 68 65 6e   iDb. .  ** Then
2275a 20 6d 6f 64 69 66 79 20 74 68 65 20 73 63 68 65   modify the sche
2275b 6d 61 20 63 6f 6f 6b 69 65 20 28 73 69 6e 63 65  ma cookie (since
2275c 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45   the ALTER TABLE
2275d 20 6d 6f 64 69 66 69 65 73 20 74 68 65 0a 20 20   modifies the.  
2275e 2a 2a 20 73 63 68 65 6d 61 29 2e 20 4f 70 65 6e  ** schema). Open
2275f 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61   a statement tra
22760 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 20  nsaction if the 
22761 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 75  table is a virtu
22762 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 0a 20  al.  ** table.. 
22763 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
22764 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
22765 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a  ;.  if( v==0 ){.
22766 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65      goto exit_re
22767 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  name_table;.  }.
22768 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
22769 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
2276a 72 73 65 2c 20 70 56 54 61 62 21 3d 30 2c 20 69  rse, pVTab!=0, i
2276b 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 68  Db);.  sqlite3Ch
2276c 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
2276d 65 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49  e, iDb);..  /* I
2276e 66 20 74 68 69 73 20 69 73 20 61 20 76 69 72 74  f this is a virt
2276f 75 61 6c 20 74 61 62 6c 65 2c 20 69 6e 76 6f 6b  ual table, invok
22770 65 20 74 68 65 20 78 52 65 6e 61 6d 65 28 29 20  e the xRename() 
22771 66 75 6e 63 74 69 6f 6e 20 69 66 0a 20 20 2a 2a  function if.  **
22772 20 6f 6e 65 20 69 73 20 64 65 66 69 6e 65 64 2e   one is defined.
22773 20 54 68 65 20 78 52 65 6e 61 6d 65 28 29 20 63   The xRename() c
22774 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d 6f 64  allback will mod
22775 69 66 79 20 74 68 65 20 6e 61 6d 65 73 0a 20 20  ify the names.  
22776 2a 2a 20 6f 66 20 61 6e 79 20 72 65 73 6f 75 72  ** of any resour
22777 63 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  ces used by the 
22778 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  v-table implemen
22779 74 61 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e  tation (includin
2277a 67 20 6f 74 68 65 72 0a 20 20 2a 2a 20 53 51 4c  g other.  ** SQL
2277b 69 74 65 20 74 61 62 6c 65 73 29 20 74 68 61 74  ite tables) that
2277c 20 61 72 65 20 69 64 65 6e 74 69 66 69 65 64 20   are identified 
2277d 62 79 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  by the name of t
2277e 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2277f 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
22780 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
22781 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70 56  ALTABLE.  if( pV
22782 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  Tab ){.    int i
22783 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
22784 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  m;.    sqlite3Vd
22785 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
22786 74 72 69 6e 67 38 2c 20 30 2c 20 69 2c 20 30 2c  tring8, 0, i, 0,
22787 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20   zName, 0);.    
22788 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22789 34 28 76 2c 20 4f 50 5f 56 52 65 6e 61 6d 65 2c  4(v, OP_VRename,
2278a 20 69 2c 20 30 2c 20 30 2c 28 63 6f 6e 73 74 20   i, 0, 0,(const 
2278b 63 68 61 72 2a 29 70 56 54 61 62 2c 20 50 34 5f  char*)pVTab, P4_
2278c 56 54 41 42 29 3b 0a 20 20 20 20 73 71 6c 69 74  VTAB);.    sqlit
2278d 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73  e3MayAbort(pPars
2278e 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  e);.  }.#endif..
2278f 20 20 2f 2a 20 66 69 67 75 72 65 20 6f 75 74 20    /* figure out 
22790 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d 38 20 63  how many UTF-8 c
22791 68 61 72 61 63 74 65 72 73 20 61 72 65 20 69 6e  haracters are in
22792 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a 54 61 62   zName */.  zTab
22793 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61  Name = pTab->zNa
22794 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d 65 20 3d  me;.  nTabName =
22795 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61 72   sqlite3Utf8Char
22796 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c 20 2d 31  Len(zTabName, -1
22797 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  );..#if !defined
22798 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52  (SQLITE_OMIT_FOR
22799 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21 64 65  EIGN_KEY) && !de
2279a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2279b 54 5f 54 52 49 47 47 45 52 29 0a 20 20 69 66 28  T_TRIGGER).  if(
2279c 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54   db->flags&SQLIT
2279d 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b  E_ForeignKeys ){
2279e 0a 20 20 20 20 2f 2a 20 49 66 20 66 6f 72 65 69  .    /* If forei
2279f 67 6e 2d 6b 65 79 20 73 75 70 70 6f 72 74 20 69  gn-key support i
227a0 73 20 65 6e 61 62 6c 65 64 2c 20 72 65 77 72 69  s enabled, rewri
227a1 74 65 20 74 68 65 20 43 52 45 41 54 45 20 54 41  te the CREATE TA
227a2 42 4c 45 20 0a 20 20 20 20 2a 2a 20 73 74 61 74  BLE .    ** stat
227a3 65 6d 65 6e 74 73 20 63 6f 72 72 65 73 70 6f 6e  ements correspon
227a4 64 69 6e 67 20 74 6f 20 61 6c 6c 20 63 68 69 6c  ding to all chil
227a5 64 20 74 61 62 6c 65 73 20 6f 66 20 66 6f 72 65  d tables of fore
227a6 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
227a7 6e 74 73 0a 20 20 20 20 2a 2a 20 66 6f 72 20 77  nts.    ** for w
227a8 68 69 63 68 20 74 68 65 20 72 65 6e 61 6d 65 64  hich the renamed
227a9 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 70 61   table is the pa
227aa 72 65 6e 74 20 74 61 62 6c 65 2e 20 20 2a 2f 0a  rent table.  */.
227ab 20 20 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d      if( (zWhere=
227ac 77 68 65 72 65 46 6f 72 65 69 67 6e 4b 65 79 73  whereForeignKeys
227ad 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21  (pParse, pTab))!
227ae 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
227af 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
227b0 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
227b1 20 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 65    "UPDATE sqlite
227b2 5f 6d 61 73 74 65 72 20 53 45 54 20 22 0a 20 20  _master SET ".  
227b3 20 20 20 20 20 20 20 20 20 20 20 20 22 73 71 6c              "sql
227b4 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65   = sqlite_rename
227b5 5f 70 61 72 65 6e 74 28 73 71 6c 2c 20 25 51 2c  _parent(sql, %Q,
227b6 20 25 51 29 20 22 0a 20 20 20 20 20 20 20 20 20   %Q) ".         
227b7 20 20 20 20 20 22 57 48 45 52 45 20 25 73 3b 22       "WHERE %s;"
227b8 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 7a 4e 61 6d  , zTabName, zNam
227b9 65 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 20 20  e, zWhere);.    
227ba 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
227bb 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 20  db, zWhere);.   
227bc 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
227bd 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 73   /* Modify the s
227be 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
227bf 6c 65 20 74 6f 20 75 73 65 20 74 68 65 20 6e 65  le to use the ne
227c0 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f  w table name. */
227c1 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64  .  sqlite3Nested
227c2 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
227c3 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 25      "UPDATE %Q.%
227c4 73 20 53 45 54 20 22 0a 23 69 66 64 65 66 20 53  s SET ".#ifdef S
227c5 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
227c6 45 52 0a 20 20 20 20 20 20 20 20 20 20 22 73 71  ER.          "sq
227c7 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d  l = sqlite_renam
227c8 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29  e_table(sql, %Q)
227c9 2c 20 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20  , ".#else.      
227ca 20 20 20 20 22 73 71 6c 20 3d 20 43 41 53 45 20      "sql = CASE 
227cb 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57  ".            "W
227cc 48 45 4e 20 74 79 70 65 20 3d 20 27 74 72 69 67  HEN type = 'trig
227cd 67 65 72 27 20 54 48 45 4e 20 73 71 6c 69 74 65  ger' THEN sqlite
227ce 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 28  _rename_trigger(
227cf 73 71 6c 2c 20 25 51 29 22 0a 20 20 20 20 20 20  sql, %Q)".      
227d0 20 20 20 20 20 20 22 45 4c 53 45 20 73 71 6c 69        "ELSE sqli
227d1 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28  te_rename_table(
227d2 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c 20 22 0a  sql, %Q) END, ".
227d3 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20  #endif.         
227d4 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 2c   "tbl_name = %Q,
227d5 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 6e 61   ".          "na
227d6 6d 65 20 3d 20 43 41 53 45 20 22 0a 20 20 20 20  me = CASE ".    
227d7 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 74 79          "WHEN ty
227d8 70 65 3d 27 74 61 62 6c 65 27 20 54 48 45 4e 20  pe='table' THEN 
227d9 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  %Q ".           
227da 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c 49 4b 45   "WHEN name LIKE
227db 20 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64   'sqlite_autoind
227dc 65 78 25 25 27 20 41 4e 44 20 74 79 70 65 3d 27  ex%%' AND type='
227dd 69 6e 64 65 78 27 20 54 48 45 4e 20 22 0a 20 20  index' THEN ".  
227de 20 20 20 20 20 20 20 20 20 20 20 22 27 73 71 6c             "'sql
227df 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 27 20  ite_autoindex_' 
227e0 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72 28  || %Q || substr(
227e1 6e 61 6d 65 2c 25 64 2b 31 38 29 20 22 0a 20 20  name,%d+18) ".  
227e2 20 20 20 20 20 20 20 20 20 20 22 45 4c 53 45 20            "ELSE 
227e3 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20 20 20 20  name END ".     
227e4 20 22 57 48 45 52 45 20 74 62 6c 5f 6e 61 6d 65   "WHERE tbl_name
227e5 3d 25 51 20 41 4e 44 20 22 0a 20 20 20 20 20 20  =%Q AND ".      
227e6 20 20 20 20 22 28 74 79 70 65 3d 27 74 61 62 6c      "(type='tabl
227e7 65 27 20 4f 52 20 74 79 70 65 3d 27 69 6e 64 65  e' OR type='inde
227e8 78 27 20 4f 52 20 74 79 70 65 3d 27 74 72 69 67  x' OR type='trig
227e9 67 65 72 27 29 3b 22 2c 20 0a 20 20 20 20 20 20  ger');", .      
227ea 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  zDb, SCHEMA_TABL
227eb 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65 2c 20 7a  E(iDb), zName, z
227ec 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 0a 23 69  Name, zName, .#i
227ed 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
227ee 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20  T_TRIGGER.      
227ef 7a 4e 61 6d 65 2c 0a 23 65 6e 64 69 66 0a 20 20  zName,.#endif.  
227f0 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 54 61 62 4e      zName, nTabN
227f1 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 0a 20 20  ame, zTabName.  
227f2 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
227f3 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
227f4 45 4d 45 4e 54 0a 20 20 2f 2a 20 49 66 20 74 68  EMENT.  /* If th
227f5 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
227f6 65 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 69  e table exists i
227f7 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65 2c  n this database,
227f8 20 74 68 65 6e 20 75 70 64 61 74 65 20 0a 20 20   then update .  
227f9 2a 2a 20 69 74 20 77 69 74 68 20 74 68 65 20 6e  ** it with the n
227fa 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 0a 20  ew table name.. 
227fb 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
227fc 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22  3FindTable(db, "
227fd 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22  sqlite_sequence"
227fe 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 71  , zDb) ){.    sq
227ff 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
22800 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
22801 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e   "UPDATE \"%w\".
22802 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
22803 73 65 74 20 6e 61 6d 65 20 3d 20 25 51 20 57 48  set name = %Q WH
22804 45 52 45 20 6e 61 6d 65 20 3d 20 25 51 22 2c 0a  ERE name = %Q",.
22805 20 20 20 20 20 20 20 20 7a 44 62 2c 20 7a 4e 61          zDb, zNa
22806 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  me, pTab->zName)
22807 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
22808 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22809 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 49  T_TRIGGER.  /* I
2280a 66 20 74 68 65 72 65 20 61 72 65 20 54 45 4d 50  f there are TEMP
2280b 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69   triggers on thi
2280c 73 20 74 61 62 6c 65 2c 20 6d 6f 64 69 66 79 20  s table, modify 
2280d 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f  the sqlite_temp_
2280e 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74 61 62 6c  master.  ** tabl
2280f 65 2e 20 44 6f 6e 27 74 20 64 6f 20 74 68 69 73  e. Don't do this
22810 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   if the table be
22811 69 6e 67 20 41 4c 54 45 52 65 64 20 69 73 20 69  ing ALTERed is i
22812 74 73 65 6c 66 20 6c 6f 63 61 74 65 64 20 69 6e  tself located in
22813 0a 20 20 2a 2a 20 74 68 65 20 74 65 6d 70 20 64  .  ** the temp d
22814 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  atabase..  */.  
22815 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72  if( (zWhere=wher
22816 65 54 65 6d 70 54 72 69 67 67 65 72 73 28 70 50  eTempTriggers(pP
22817 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20  arse, pTab))!=0 
22818 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  ){.    sqlite3Ne
22819 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
2281a 2c 20 0a 20 20 20 20 20 20 20 20 22 55 50 44 41  , .        "UPDA
2281b 54 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d  TE sqlite_temp_m
2281c 61 73 74 65 72 20 53 45 54 20 22 0a 20 20 20 20  aster SET ".    
2281d 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73          "sql = s
2281e 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69  qlite_rename_tri
2281f 67 67 65 72 28 73 71 6c 2c 20 25 51 29 2c 20 22  gger(sql, %Q), "
22820 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 74 62  .            "tb
22821 6c 5f 6e 61 6d 65 20 3d 20 25 51 20 22 0a 20 20  l_name = %Q ".  
22822 20 20 20 20 20 20 20 20 20 20 22 57 48 45 52 45            "WHERE
22823 20 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c 20 7a 4e   %s;", zName, zN
22824 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20  ame, zWhere);.  
22825 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
22826 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 7d  db, zWhere);.  }
22827 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65  .#endif..#if !de
22828 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
22829 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26  T_FOREIGN_KEY) &
2282a 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
2282b 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
2282c 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 26    if( db->flags&
2282d 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
2282e 79 73 20 29 7b 0a 20 20 20 20 46 4b 65 79 20 2a  ys ){.    FKey *
2282f 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c  p;.    for(p=sql
22830 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
22831 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70 2d 3e  (pTab); p; p=p->
22832 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20 20 20  pNextTo){.      
22833 54 61 62 6c 65 20 2a 70 46 72 6f 6d 20 3d 20 70  Table *pFrom = p
22834 2d 3e 70 46 72 6f 6d 3b 0a 20 20 20 20 20 20 69  ->pFrom;.      i
22835 66 28 20 70 46 72 6f 6d 21 3d 70 54 61 62 20 29  f( pFrom!=pTab )
22836 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 6f 61 64  {.        reload
22837 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50 61 72  TableSchema(pPar
22838 73 65 2c 20 70 2d 3e 70 46 72 6f 6d 2c 20 70 46  se, p->pFrom, pF
22839 72 6f 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  rom->zName);.   
2283a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
2283b 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70  endif..  /* Drop
2283c 20 61 6e 64 20 72 65 6c 6f 61 64 20 74 68 65 20   and reload the 
2283d 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 73  internal table s
2283e 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 72 65 6c 6f  chema. */.  relo
2283f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50  adTableSchema(pP
22840 61 72 73 65 2c 20 70 54 61 62 2c 20 7a 4e 61 6d  arse, pTab, zNam
22841 65 29 3b 0a 0a 65 78 69 74 5f 72 65 6e 61 6d 65  e);..exit_rename
22842 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 74 65  _table:.  sqlite
22843 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64  3SrcListDelete(d
22844 62 2c 20 70 53 72 63 29 3b 0a 20 20 73 71 6c 69  b, pSrc);.  sqli
22845 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e  te3DbFree(db, zN
22846 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  ame);.}.../*.** 
22847 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
22848 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 66   make sure the f
22849 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 65  ile format numbe
2284a 72 20 69 73 20 61 74 20 6c 65 61 73 74 20 6d 69  r is at least mi
2284b 6e 46 6f 72 6d 61 74 2e 0a 2a 2a 20 54 68 65 20  nFormat..** The 
2284c 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 77  generated code w
2284d 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65  ill increase the
2284e 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d   file format num
2284f 62 65 72 20 69 66 20 6e 65 63 65 73 73 61 72 79  ber if necessary
22850 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
22851 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
22852 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61  MinimumFileForma
22853 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
22854 20 69 6e 74 20 69 44 62 2c 20 69 6e 74 20 6d 69   int iDb, int mi
22855 6e 46 6f 72 6d 61 74 29 7b 0a 20 20 56 64 62 65  nFormat){.  Vdbe
22856 20 2a 76 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74   *v;.  v = sqlit
22857 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
22858 29 3b 0a 20 20 2f 2a 20 54 68 65 20 56 44 42 45  );.  /* The VDBE
22859 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65   should have bee
2285a 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 66 6f  n allocated befo
2285b 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  re this routine 
2285c 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2a 20  is called..  ** 
2285d 49 66 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69  If that allocati
2285e 6f 6e 20 66 61 69 6c 65 64 2c 20 77 65 20 77 6f  on failed, we wo
2285f 75 6c 64 20 68 61 76 65 20 71 75 69 74 20 62 65  uld have quit be
22860 66 6f 72 65 20 72 65 61 63 68 69 6e 67 20 74 68  fore reaching th
22861 69 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 2a 2f  is.  ** point */
22862 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 76 29  .  if( ALWAYS(v)
22863 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 20 3d   ){.    int r1 =
22864 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
22865 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
22866 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33  int r2 = sqlite3
22867 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
22868 65 29 3b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a  e);.    int j1;.
22869 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2286a 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61 64  ddOp3(v, OP_Read
2286b 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 72 31 2c  Cookie, iDb, r1,
2286c 20 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d   BTREE_FILE_FORM
2286d 41 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  AT);.    sqlite3
2286e 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
2286f 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
22870 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
22871 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6d 69 6e 46  OP_Integer, minF
22872 6f 72 6d 61 74 2c 20 72 32 29 3b 0a 20 20 20 20  ormat, r2);.    
22873 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
22874 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 47 65 2c  AddOp3(v, OP_Ge,
22875 20 72 32 2c 20 30 2c 20 72 31 29 3b 0a 20 20 20   r2, 0, r1);.   
22876 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22877 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b  p3(v, OP_SetCook
22878 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 46  ie, iDb, BTREE_F
22879 49 4c 45 5f 46 4f 52 4d 41 54 2c 20 72 32 29 3b  ILE_FORMAT, r2);
2287a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2287b 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
2287c 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
2287d 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2287e 65 2c 20 72 31 29 3b 0a 20 20 20 20 73 71 6c 69  e, r1);.    sqli
2287f 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
22880 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20  g(pParse, r2);. 
22881 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
22882 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
22883 6c 65 64 20 61 66 74 65 72 20 61 6e 20 22 41 4c  led after an "AL
22884 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44  TER TABLE ... AD
22885 44 22 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  D" statement.** 
22886 68 61 73 20 62 65 65 6e 20 70 61 72 73 65 64 2e  has been parsed.
22887 20 41 72 67 75 6d 65 6e 74 20 70 43 6f 6c 44 65   Argument pColDe
22888 66 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  f contains the t
22889 65 78 74 20 6f 66 20 74 68 65 20 6e 65 77 0a 2a  ext of the new.*
2288a 2a 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74  * column definit
2288b 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54  ion..**.** The T
2288c 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 70  able structure p
2288d 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2288e 20 77 61 73 20 65 78 74 65 6e 64 65 64 20 74 6f   was extended to
2288f 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 74 68 65 20   include.** the 
22890 6e 65 77 20 63 6f 6c 75 6d 6e 20 64 75 72 69 6e  new column durin
22891 67 20 70 61 72 73 69 6e 67 2e 0a 2a 2f 0a 53 51  g parsing..*/.SQ
22892 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
22893 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69  d sqlite3AlterFi
22894 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61  nishAddColumn(Pa
22895 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
22896 65 6e 20 2a 70 43 6f 6c 44 65 66 29 7b 0a 20 20  en *pColDef){.  
22897 54 61 62 6c 65 20 2a 70 4e 65 77 3b 20 20 20 20  Table *pNew;    
22898 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70            /* Cop
22899 79 20 6f 66 20 70 50 61 72 73 65 2d 3e 70 4e 65  y of pParse->pNe
2289a 77 54 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c  wTable */.  Tabl
2289b 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
2289c 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62        /* Table b
2289d 65 69 6e 67 20 61 6c 74 65 72 65 64 20 2a 2f 0a  eing altered */.
2289e 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
2289f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
228a0 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a  atabase number *
228a1 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
228a2 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  zDb;          /*
228a3 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a   Database name *
228a4 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
228a5 7a 54 61 62 3b 20 20 20 20 20 20 20 20 20 2f 2a  zTab;         /*
228a6 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   Table name */. 
228a7 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20   char *zCol;    
228a8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
228a9 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 63 6f  ll-terminated co
228aa 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20  lumn definition 
228ab 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  */.  Column *pCo
228ac 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
228ad 2a 20 54 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e  * The new column
228ae 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 66 6c   */.  Expr *pDfl
228af 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
228b0 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65  /* Default value
228b1 20 66 6f 72 20 74 68 65 20 6e 65 77 20 63 6f 6c   for the new col
228b2 75 6d 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  umn */.  sqlite3
228b3 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
228b4 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
228b5 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2a  se connection; *
228b6 2f 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  /..  db = pParse
228b7 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72  ->db;.  if( pPar
228b8 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
228b9 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
228ba 65 74 75 72 6e 3b 0a 20 20 70 4e 65 77 20 3d 20  eturn;.  pNew = 
228bb 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
228bc 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65  e;.  assert( pNe
228bd 77 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  w );..  assert( 
228be 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
228bf 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20  sAllMutexes(db) 
228c0 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  );.  iDb = sqlit
228c1 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
228c2 64 62 2c 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d  db, pNew->pSchem
228c3 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e  a);.  zDb = db->
228c4 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
228c5 20 20 7a 54 61 62 20 3d 20 26 70 4e 65 77 2d 3e    zTab = &pNew->
228c6 7a 4e 61 6d 65 5b 31 36 5d 3b 20 20 2f 2a 20 53  zName[16];  /* S
228c7 6b 69 70 20 74 68 65 20 22 73 71 6c 69 74 65 5f  kip the "sqlite_
228c8 61 6c 74 65 72 74 61 62 5f 22 20 70 72 65 66 69  altertab_" prefi
228c9 78 20 6f 6e 20 74 68 65 20 6e 61 6d 65 20 2a 2f  x on the name */
228ca 0a 20 20 70 43 6f 6c 20 3d 20 26 70 4e 65 77 2d  .  pCol = &pNew-
228cb 3e 61 43 6f 6c 5b 70 4e 65 77 2d 3e 6e 43 6f 6c  >aCol[pNew->nCol
228cc 2d 31 5d 3b 0a 20 20 70 44 66 6c 74 20 3d 20 70  -1];.  pDflt = p
228cd 43 6f 6c 2d 3e 70 44 66 6c 74 3b 0a 20 20 70 54  Col->pDflt;.  pT
228ce 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ab = sqlite3Find
228cf 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20  Table(db, zTab, 
228d0 7a 44 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20  zDb);.  assert( 
228d1 70 54 61 62 20 29 3b 0a 0a 23 69 66 6e 64 65 66  pTab );..#ifndef
228d2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
228d3 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20  HORIZATION.  /* 
228d4 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 6f  Invoke the autho
228d5 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
228d6 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  k. */.  if( sqli
228d7 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
228d8 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45  rse, SQLITE_ALTE
228d9 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54  R_TABLE, zDb, pT
228da 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b  ab->zName, 0) ){
228db 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
228dc 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
228dd 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   the default val
228de 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 63  ue for the new c
228df 6f 6c 75 6d 6e 20 77 61 73 20 73 70 65 63 69 66  olumn was specif
228e0 69 65 64 20 77 69 74 68 20 61 20 0a 20 20 2a 2a  ied with a .  **
228e1 20 6c 69 74 65 72 61 6c 20 4e 55 4c 4c 2c 20 74   literal NULL, t
228e2 68 65 6e 20 73 65 74 20 70 44 66 6c 74 20 74 6f  hen set pDflt to
228e3 20 30 2e 20 54 68 69 73 20 73 69 6d 70 6c 69 66   0. This simplif
228e4 69 65 73 20 63 68 65 63 6b 69 6e 67 0a 20 20 2a  ies checking.  *
228e5 2a 20 66 6f 72 20 61 6e 20 53 51 4c 20 4e 55 4c  * for an SQL NUL
228e6 4c 20 64 65 66 61 75 6c 74 20 62 65 6c 6f 77 2e  L default below.
228e7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c  .  */.  if( pDfl
228e8 74 20 26 26 20 70 44 66 6c 74 2d 3e 6f 70 3d 3d  t && pDflt->op==
228e9 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70  TK_NULL ){.    p
228ea 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  Dflt = 0;.  }.. 
228eb 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74   /* Check that t
228ec 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73  he new column is
228ed 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 61   not specified a
228ee 73 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72  s PRIMARY KEY or
228ef 20 55 4e 49 51 55 45 2e 0a 20 20 2a 2a 20 49 66   UNIQUE..  ** If
228f0 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54 20   there is a NOT 
228f1 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 2c  NULL constraint,
228f2 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75 6c   then the defaul
228f3 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 0a  t value for the.
228f4 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6d 75 73 74    ** column must
228f5 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 20 20   not be NULL..  
228f6 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69  */.  if( pCol->i
228f7 73 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20 20 20  sPrimKey ){.    
228f8 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
228f9 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20  pParse, "Cannot 
228fa 61 64 64 20 61 20 50 52 49 4d 41 52 59 20 4b 45  add a PRIMARY KE
228fb 59 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20 20  Y column");.    
228fc 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66  return;.  }.  if
228fd 28 20 70 4e 65 77 2d 3e 70 49 6e 64 65 78 20 29  ( pNew->pIndex )
228fe 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
228ff 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43  orMsg(pParse, "C
22900 61 6e 6e 6f 74 20 61 64 64 20 61 20 55 4e 49 51  annot add a UNIQ
22901 55 45 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20  UE column");.   
22902 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
22903 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51  f( (db->flags&SQ
22904 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73  LITE_ForeignKeys
22905 29 20 26 26 20 70 4e 65 77 2d 3e 70 46 4b 65 79  ) && pNew->pFKey
22906 20 26 26 20 70 44 66 6c 74 20 29 7b 0a 20 20 20   && pDflt ){.   
22907 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
22908 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
22909 20 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20    "Cannot add a 
2290a 52 45 46 45 52 45 4e 43 45 53 20 63 6f 6c 75 6d  REFERENCES colum
2290b 6e 20 77 69 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20  n with non-NULL 
2290c 64 65 66 61 75 6c 74 20 76 61 6c 75 65 22 29 3b  default value");
2290d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
2290e 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e 6e 6f 74  .  if( pCol->not
2290f 4e 75 6c 6c 20 26 26 20 21 70 44 66 6c 74 20 29  Null && !pDflt )
22910 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
22911 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
22912 20 20 20 20 20 20 20 22 43 61 6e 6e 6f 74 20 61         "Cannot a
22913 64 64 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  dd a NOT NULL co
22914 6c 75 6d 6e 20 77 69 74 68 20 64 65 66 61 75 6c  lumn with defaul
22915 74 20 76 61 6c 75 65 20 4e 55 4c 4c 22 29 3b 0a  t value NULL");.
22916 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
22917 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65  .  /* Ensure the
22918 20 64 65 66 61 75 6c 74 20 65 78 70 72 65 73 73   default express
22919 69 6f 6e 20 69 73 20 73 6f 6d 65 74 68 69 6e 67  ion is something
2291a 20 74 68 61 74 20 73 71 6c 69 74 65 33 56 61 6c   that sqlite3Val
2291b 75 65 46 72 6f 6d 45 78 70 72 28 29 0a 20 20 2a  ueFromExpr().  *
2291c 2a 20 63 61 6e 20 68 61 6e 64 6c 65 20 28 69 2e  * can handle (i.
2291d 65 2e 20 6e 6f 74 20 43 55 52 52 45 4e 54 5f 54  e. not CURRENT_T
2291e 49 4d 45 20 65 74 63 2e 29 0a 20 20 2a 2f 0a 20  IME etc.).  */. 
2291f 20 69 66 28 20 70 44 66 6c 74 20 29 7b 0a 20 20   if( pDflt ){.  
22920 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
22921 2a 70 56 61 6c 3b 0a 20 20 20 20 69 66 28 20 73  *pVal;.    if( s
22922 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45  qlite3ValueFromE
22923 78 70 72 28 64 62 2c 20 70 44 66 6c 74 2c 20 53  xpr(db, pDflt, S
22924 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
22925 54 45 5f 41 46 46 5f 4e 4f 4e 45 2c 20 26 70 56  TE_AFF_NONE, &pV
22926 61 6c 29 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  al) ){.      db-
22927 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
22928 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  1;.      return;
22929 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
2292a 70 56 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71  pVal ){.      sq
2292b 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2292c 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64  arse, "Cannot ad
2292d 64 20 61 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20  d a column with 
2292e 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 20 64 65 66  non-constant def
2292f 61 75 6c 74 22 29 3b 0a 20 20 20 20 20 20 72 65  ault");.      re
22930 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
22931 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
22932 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f  (pVal);.  }..  /
22933 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 43 52 45  * Modify the CRE
22934 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
22935 65 6e 74 2e 20 2a 2f 0a 20 20 7a 43 6f 6c 20 3d  ent. */.  zCol =
22936 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75   sqlite3DbStrNDu
22937 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 43 6f  p(db, (char*)pCo
22938 6c 44 65 66 2d 3e 7a 2c 20 70 43 6f 6c 44 65 66  lDef->z, pColDef
22939 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6c  ->n);.  if( zCol
2293a 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45   ){.    char *zE
2293b 6e 64 20 3d 20 26 7a 43 6f 6c 5b 70 43 6f 6c 44  nd = &zCol[pColD
2293c 65 66 2d 3e 6e 2d 31 5d 3b 0a 20 20 20 20 77 68  ef->n-1];.    wh
2293d 69 6c 65 28 20 7a 45 6e 64 3e 7a 43 6f 6c 20 26  ile( zEnd>zCol &
2293e 26 20 28 2a 7a 45 6e 64 3d 3d 27 3b 27 20 7c 7c  & (*zEnd==';' ||
2293f 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28   sqlite3Isspace(
22940 2a 7a 45 6e 64 29 29 20 29 7b 0a 20 20 20 20 20  *zEnd)) ){.     
22941 20 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c 30 27 3b   *zEnd-- = '\0';
22942 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
22943 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
22944 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22  arse, .        "
22945 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e 25 73  UPDATE \"%w\".%s
22946 20 53 45 54 20 22 0a 20 20 20 20 20 20 20 20 20   SET ".         
22947 20 22 73 71 6c 20 3d 20 73 75 62 73 74 72 28 73   "sql = substr(s
22948 71 6c 2c 31 2c 25 64 29 20 7c 7c 20 27 2c 20 27  ql,1,%d) || ', '
22949 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72   || %Q || substr
2294a 28 73 71 6c 2c 25 64 29 20 22 0a 20 20 20 20 20  (sql,%d) ".     
2294b 20 20 20 22 57 48 45 52 45 20 74 79 70 65 20 3d     "WHERE type =
2294c 20 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d   'table' AND nam
2294d 65 20 3d 20 25 51 22 2c 20 0a 20 20 20 20 20 20  e = %Q", .      
2294e 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  zDb, SCHEMA_TABL
2294f 45 28 69 44 62 29 2c 20 70 4e 65 77 2d 3e 61 64  E(iDb), pNew->ad
22950 64 43 6f 6c 4f 66 66 73 65 74 2c 20 7a 43 6f 6c  dColOffset, zCol
22951 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66  , pNew->addColOf
22952 66 73 65 74 2b 31 2c 0a 20 20 20 20 20 20 7a 54  fset+1,.      zT
22953 61 62 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71  ab.    );.    sq
22954 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
22955 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  zCol);.  }..  /*
22956 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   If the default 
22957 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65 77  value of the new
22958 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 2c   column is NULL,
22959 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 66 69   then set the fi
2295a 6c 65 0a 20 20 2a 2a 20 66 6f 72 6d 61 74 20 74  le.  ** format t
2295b 6f 20 32 2e 20 49 66 20 74 68 65 20 64 65 66 61  o 2. If the defa
2295c 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ult value of the
2295d 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 6e   new column is n
2295e 6f 74 20 4e 55 4c 4c 2c 0a 20 20 2a 2a 20 74 68  ot NULL,.  ** th
2295f 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 62 65  e file format be
22960 63 6f 6d 65 73 20 33 2e 0a 20 20 2a 2f 0a 20 20  comes 3..  */.  
22961 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69  sqlite3MinimumFi
22962 6c 65 46 6f 72 6d 61 74 28 70 50 61 72 73 65 2c  leFormat(pParse,
22963 20 69 44 62 2c 20 70 44 66 6c 74 20 3f 20 33 20   iDb, pDflt ? 3 
22964 3a 20 32 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f  : 2);..  /* Relo
22965 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66  ad the schema of
22966 20 74 68 65 20 6d 6f 64 69 66 69 65 64 20 74 61   the modified ta
22967 62 6c 65 2e 20 2a 2f 0a 20 20 72 65 6c 6f 61 64  ble. */.  reload
22968 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50 61 72  TableSchema(pPar
22969 73 65 2c 20 70 54 61 62 2c 20 70 54 61 62 2d 3e  se, pTab, pTab->
2296a 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  zName);.}../*.**
2296b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
2296c 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
2296d 70 61 72 73 65 72 20 61 66 74 65 72 20 74 68 65  parser after the
2296e 20 74 61 62 6c 65 2d 6e 61 6d 65 20 69 6e 0a 2a   table-name in.*
2296f 2a 20 61 6e 20 22 41 4c 54 45 52 20 54 41 42 4c  * an "ALTER TABL
22970 45 20 3c 74 61 62 6c 65 2d 6e 61 6d 65 3e 20 41  E <table-name> A
22971 44 44 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73  DD" statement is
22972 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d 65 6e   parsed. Argumen
22973 74 20 0a 2a 2a 20 70 53 72 63 20 69 73 20 74 68  t .** pSrc is th
22974 65 20 66 75 6c 6c 2d 6e 61 6d 65 20 6f 66 20 74  e full-name of t
22975 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61  he table being a
22976 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  ltered..**.** Th
22977 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73  is routine makes
22978 20 61 20 28 70 61 72 74 69 61 6c 29 20 63 6f 70   a (partial) cop
22979 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73  y of the Table s
2297a 74 72 75 63 74 75 72 65 0a 2a 2a 20 66 6f 72 20  tructure.** for 
2297b 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
2297c 61 6c 74 65 72 65 64 20 61 6e 64 20 73 65 74 73  altered and sets
2297d 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65   Parse.pNewTable
2297e 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20   to point.** to 
2297f 69 74 2e 20 52 6f 75 74 69 6e 65 73 20 63 61 6c  it. Routines cal
22980 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
22981 72 20 61 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  r as the column 
22982 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a 20 69 73  definition.** is
22983 20 70 61 72 73 65 64 20 28 69 2e 65 2e 20 73 71   parsed (i.e. sq
22984 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 29  lite3AddColumn()
22985 29 20 61 64 64 20 74 68 65 20 6e 65 77 20 43 6f  ) add the new Co
22986 6c 75 6d 6e 20 64 61 74 61 20 74 6f 20 0a 2a 2a  lumn data to .**
22987 20 74 68 65 20 63 6f 70 79 2e 20 54 68 65 20 63   the copy. The c
22988 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65  opy of the Table
22989 20 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65   structure is de
2298a 6c 65 74 65 64 20 62 79 20 74 6f 6b 65 6e 69 7a  leted by tokeniz
2298b 65 2e 63 20 0a 2a 2a 20 61 66 74 65 72 20 70 61  e.c .** after pa
2298c 72 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68 65  rsing is finishe
2298d 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  d..**.** Routine
2298e 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e   sqlite3AlterFin
2298f 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 29 20 77  ishAddColumn() w
22990 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74 6f  ill be called to
22991 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 64   complete.** cod
22992 69 6e 67 20 74 68 65 20 22 41 4c 54 45 52 20 54  ing the "ALTER T
22993 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 74  ABLE ... ADD" st
22994 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
22995 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
22996 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69  sqlite3AlterBegi
22997 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65  nAddColumn(Parse
22998 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73   *pParse, SrcLis
22999 74 20 2a 70 53 72 63 29 7b 0a 20 20 54 61 62 6c  t *pSrc){.  Tabl
2299a 65 20 2a 70 4e 65 77 3b 0a 20 20 54 61 62 6c 65  e *pNew;.  Table
2299b 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65 20 2a   *pTab;.  Vdbe *
2299c 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20  v;.  int iDb;.  
2299d 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 41 6c  int i;.  int nAl
2299e 6c 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  loc;.  sqlite3 *
2299f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
229a0 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 74  ..  /* Look up t
229a1 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61  he table being a
229a2 6c 74 65 72 65 64 2e 20 2a 2f 0a 20 20 61 73 73  ltered. */.  ass
229a3 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65  ert( pParse->pNe
229a4 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 61  wTable==0 );.  a
229a5 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
229a6 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
229a7 65 73 28 64 62 29 20 29 3b 0a 20 20 69 66 28 20  es(db) );.  if( 
229a8 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
229a9 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67   ) goto exit_beg
229aa 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20  in_add_column;. 
229ab 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
229ac 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
229ad 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b 30 5d  e, 0, pSrc->a[0]
229ae 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b  .zName, pSrc->a[
229af 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
229b0 20 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74   if( !pTab ) got
229b1 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  o exit_begin_add
229b2 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23 69 66 6e 64 65  _column;..#ifnde
229b3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
229b4 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
229b5 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
229b6 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
229b7 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
229b8 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20  "virtual tables 
229b9 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 65 72  may not be alter
229ba 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ed");.    goto e
229bb 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
229bc 6c 75 6d 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  lumn;.  }.#endif
229bd 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
229be 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 6e 20   this is not an 
229bf 61 74 74 65 6d 70 74 20 74 6f 20 41 4c 54 45 52  attempt to ALTER
229c0 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 69 66   a view. */.  if
229c1 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
229c2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
229c3 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
229c4 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c  Cannot add a col
229c5 75 6d 6e 20 74 6f 20 61 20 76 69 65 77 22 29 3b  umn to a view");
229c6 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62  .    goto exit_b
229c7 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
229c8 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
229c9 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73  pTab->addColOffs
229ca 65 74 3e 30 20 29 3b 0a 20 20 69 44 62 20 3d 20  et>0 );.  iDb = 
229cb 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
229cc 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70  ndex(db, pTab->p
229cd 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a 20 50  Schema);..  /* P
229ce 75 74 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ut a copy of the
229cf 20 54 61 62 6c 65 20 73 74 72 75 63 74 20 69 6e   Table struct in
229d0 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65   Parse.pNewTable
229d1 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 73 71   for the.  ** sq
229d2 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 29  lite3AddColumn()
229d3 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 66 72   function and fr
229d4 69 65 6e 64 73 20 74 6f 20 6d 6f 64 69 66 79 2e  iends to modify.
229d5 20 20 42 75 74 20 6d 6f 64 69 66 79 0a 20 20 2a    But modify.  *
229d6 2a 20 74 68 65 20 6e 61 6d 65 20 62 79 20 61 64  * the name by ad
229d7 64 69 6e 67 20 61 6e 20 22 73 71 6c 69 74 65 5f  ding an "sqlite_
229d8 61 6c 74 65 72 74 61 62 5f 22 20 70 72 65 66 69  altertab_" prefi
229d9 78 2e 20 20 42 79 20 61 64 64 69 6e 67 20 74 68  x.  By adding th
229da 69 73 0a 20 20 2a 2a 20 70 72 65 66 69 78 2c 20  is.  ** prefix, 
229db 77 65 20 69 6e 73 75 72 65 20 74 68 61 74 20 74  we insure that t
229dc 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20 6e 6f 74  he name will not
229dd 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20 61 6e   collide with an
229de 20 65 78 69 73 74 69 6e 67 0a 20 20 2a 2a 20 74   existing.  ** t
229df 61 62 6c 65 20 62 65 63 61 75 73 65 20 75 73 65  able because use
229e0 72 20 74 61 62 6c 65 20 61 72 65 20 6e 6f 74 20  r table are not 
229e1 61 6c 6c 6f 77 65 64 20 74 6f 20 68 61 76 65 20  allowed to have 
229e2 74 68 65 20 22 73 71 6c 69 74 65 5f 22 0a 20 20  the "sqlite_".  
229e3 2a 2a 20 70 72 65 66 69 78 20 6f 6e 20 74 68 65  ** prefix on the
229e4 69 72 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20  ir name..  */.  
229e5 70 4e 65 77 20 3d 20 28 54 61 62 6c 65 2a 29 73  pNew = (Table*)s
229e6 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
229e7 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61  ro(db, sizeof(Ta
229e8 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 70 4e  ble));.  if( !pN
229e9 65 77 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62  ew ) goto exit_b
229ea 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
229eb 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  .  pParse->pNewT
229ec 61 62 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 70  able = pNew;.  p
229ed 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  New->nRef = 1;. 
229ee 20 70 4e 65 77 2d 3e 64 62 4d 65 6d 20 3d 20 70   pNew->dbMem = p
229ef 54 61 62 2d 3e 64 62 4d 65 6d 3b 0a 20 20 70 4e  Tab->dbMem;.  pN
229f0 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70 54 61 62 2d  ew->nCol = pTab-
229f1 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73 65 72 74 28  >nCol;.  assert(
229f2 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e 30 20 29 3b   pNew->nCol>0 );
229f3 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20 28 28 28 70  .  nAlloc = (((p
229f4 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29 2f 38 29 2a  New->nCol-1)/8)*
229f5 38 29 2b 38 3b 0a 20 20 61 73 73 65 72 74 28 20  8)+8;.  assert( 
229f6 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77 2d 3e 6e 43  nAlloc>=pNew->nC
229f7 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63 25 38 3d 3d  ol && nAlloc%8==
229f8 30 20 26 26 20 6e 41 6c 6c 6f 63 2d 70 4e 65 77  0 && nAlloc-pNew
229f9 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a 20 20 70 4e  ->nCol<8 );.  pN
229fa 65 77 2d 3e 61 43 6f 6c 20 3d 20 28 43 6f 6c 75  ew->aCol = (Colu
229fb 6d 6e 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  mn*)sqlite3DbMal
229fc 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
229fd 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e 41 6c 6c 6f  of(Column)*nAllo
229fe 63 29 3b 0a 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d  c);.  pNew->zNam
229ff 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  e = sqlite3MPrin
22a00 74 66 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 61  tf(db, "sqlite_a
22a01 6c 74 65 72 74 61 62 5f 25 73 22 2c 20 70 54 61  ltertab_%s", pTa
22a02 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b->zName);.  if(
22a03 20 21 70 4e 65 77 2d 3e 61 43 6f 6c 20 7c 7c 20   !pNew->aCol || 
22a04 21 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 29 7b 0a  !pNew->zName ){.
22a05 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
22a06 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 67 6f  iled = 1;.    go
22a07 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64  to exit_begin_ad
22a08 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20  d_column;.  }.  
22a09 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 61 43 6f  memcpy(pNew->aCo
22a0a 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 73  l, pTab->aCol, s
22a0b 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 70 4e  izeof(Column)*pN
22a0c 65 77 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72  ew->nCol);.  for
22a0d 28 69 3d 30 3b 20 69 3c 70 4e 65 77 2d 3e 6e 43  (i=0; i<pNew->nC
22a0e 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 43 6f  ol; i++){.    Co
22a0f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 4e  lumn *pCol = &pN
22a10 65 77 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20  ew->aCol[i];.   
22a11 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 73   pCol->zName = s
22a12 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
22a13 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b  b, pCol->zName);
22a14 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c  .    pCol->zColl
22a15 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e   = 0;.    pCol->
22a16 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 70  zType = 0;.    p
22a17 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 30 3b 0a  Col->pDflt = 0;.
22a18 20 20 20 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 20      pCol->zDflt 
22a19 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d  = 0;.  }.  pNew-
22a1a 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61  >pSchema = db->a
22a1b 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b  Db[iDb].pSchema;
22a1c 0a 20 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f  .  pNew->addColO
22a1d 66 66 73 65 74 20 3d 20 70 54 61 62 2d 3e 61 64  ffset = pTab->ad
22a1e 64 43 6f 6c 4f 66 66 73 65 74 3b 0a 20 20 70 4e  dColOffset;.  pN
22a1f 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 0a 20  ew->nRef = 1;.. 
22a20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e   /* Begin a tran
22a21 73 61 63 74 69 6f 6e 20 61 6e 64 20 69 6e 63 72  saction and incr
22a22 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61  ement the schema
22a23 20 63 6f 6f 6b 69 65 2e 20 20 2a 2f 0a 20 20 73   cookie.  */.  s
22a24 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
22a25 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
22a26 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 76 20 3d  , 0, iDb);.  v =
22a27 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
22a28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21  pParse);.  if( !
22a29 76 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65  v ) goto exit_be
22a2a 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a  gin_add_column;.
22a2b 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43    sqlite3ChangeC
22a2c 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44  ookie(pParse, iD
22a2d 62 29 3b 0a 0a 65 78 69 74 5f 62 65 67 69 6e 5f  b);..exit_begin_
22a2e 61 64 64 5f 63 6f 6c 75 6d 6e 3a 0a 20 20 73 71  add_column:.  sq
22a2f 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
22a30 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a 20 20  te(db, pSrc);.  
22a31 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66  return;.}.#endif
22a32 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4c 54 45    /* SQLITE_ALTE
22a33 52 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a  R_TABLE */../***
22a34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
22a35 6f 66 20 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a  of alter.c *****
22a36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
22a39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
22a3a 6e 20 66 69 6c 65 20 61 6e 61 6c 79 7a 65 2e 63  n file analyze.c
22a3b 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
22a3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
22a3e 2a 20 32 30 30 35 20 4a 75 6c 79 20 38 0a 2a 2a  * 2005 July 8.**
22a3f 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
22a40 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
22a41 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
22a42 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
22a43 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
22a44 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
22a45 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
22a46 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
22a47 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
22a48 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
22a49 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
22a4a 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
22a4b 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
22a4c 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
22a4d 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
22a4e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
22a4f 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
22a50 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
22a51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
22a55 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
22a56 6e 73 20 63 6f 64 65 20 61 73 73 6f 63 69 61 74  ns code associat
22a57 65 64 20 77 69 74 68 20 74 68 65 20 41 4e 41 4c  ed with the ANAL
22a58 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  YZE command..**.
22a59 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 61 6e 61  ** @(#) $Id: ana
22a5a 6c 79 7a 65 2e 63 2c 76 20 31 2e 35 32 20 32 30  lyze.c,v 1.52 20
22a5b 30 39 2f 30 34 2f 31 36 20 31 37 3a 34 35 3a 34  09/04/16 17:45:4
22a5c 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23  8 drh Exp $.*/.#
22a5d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
22a5e 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a 2f 2a 0a 2a  IT_ANALYZE../*.*
22a5f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67  * This routine g
22a60 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 68  enerates code th
22a61 61 74 20 6f 70 65 6e 73 20 74 68 65 20 73 71 6c  at opens the sql
22a62 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20  ite_stat1 table 
22a63 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 77  for.** writing w
22a64 69 74 68 20 63 75 72 73 6f 72 20 69 53 74 61 74  ith cursor iStat
22a65 43 75 72 2e 20 49 66 20 74 68 65 20 6c 69 62 72  Cur. If the libr
22a66 61 72 79 20 77 61 73 20 62 75 69 6c 74 20 77 69  ary was built wi
22a67 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45  th the.** SQLITE
22a68 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 6d 61  _ENABLE_STAT2 ma
22a69 63 72 6f 20 64 65 66 69 6e 65 64 2c 20 74 68 65  cro defined, the
22a6a 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  n the sqlite_sta
22a6b 74 32 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 6f  t2 table is.** o
22a6c 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e  pened for writin
22a6d 67 20 75 73 69 6e 67 20 63 75 72 73 6f 72 20 28  g using cursor (
22a6e 69 53 74 61 74 43 75 72 2b 31 29 0a 2a 2a 0a 2a  iStatCur+1).**.*
22a6f 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f  * If the sqlite_
22a70 73 74 61 74 31 20 74 61 62 6c 65 73 20 64 6f 65  stat1 tables doe
22a71 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79  s not previously
22a72 20 65 78 69 73 74 2c 20 69 74 20 69 73 20 63 72   exist, it is cr
22a73 65 61 74 65 64 2e 0a 2a 2a 20 53 69 6d 69 6c 61  eated..** Simila
22a74 72 6c 79 2c 20 69 66 20 74 68 65 20 73 71 6c 69  rly, if the sqli
22a75 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 64  te_stat2 table d
22a76 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 61 6e  oes not exist an
22a77 64 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a  d the library.**
22a78 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   is compiled wit
22a79 68 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  h SQLITE_ENABLE_
22a7a 53 54 41 54 32 20 64 65 66 69 6e 65 64 2c 20 69  STAT2 defined, i
22a7b 74 20 69 73 20 63 72 65 61 74 65 64 2e 20 0a 2a  t is created. .*
22a7c 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 57  *.** Argument zW
22a7d 68 65 72 65 20 6d 61 79 20 62 65 20 61 20 70 6f  here may be a po
22a7e 69 6e 74 65 72 20 74 6f 20 61 20 62 75 66 66 65  inter to a buffe
22a7f 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 74  r containing a t
22a80 61 62 6c 65 20 6e 61 6d 65 2c 0a 2a 2a 20 6f 72  able name,.** or
22a81 20 69 74 20 6d 61 79 20 62 65 20 61 20 4e 55 4c   it may be a NUL
22a82 4c 20 70 6f 69 6e 74 65 72 2e 20 49 66 20 69 74  L pointer. If it
22a83 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
22a84 65 6e 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69  en all entries i
22a85 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f  n.** the sqlite_
22a86 73 74 61 74 31 20 61 6e 64 20 28 69 66 20 61 70  stat1 and (if ap
22a87 70 6c 69 63 61 62 6c 65 29 20 73 71 6c 69 74 65  plicable) sqlite
22a88 5f 73 74 61 74 32 20 74 61 62 6c 65 73 20 61 73  _stat2 tables as
22a89 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68  sociated.** with
22a8a 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65   the named table
22a8b 20 61 72 65 20 64 65 6c 65 74 65 64 2e 20 49 66   are deleted. If
22a8c 20 7a 57 68 65 72 65 3d 3d 30 2c 20 74 68 65 6e   zWhere==0, then
22a8d 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74   code is generat
22a8e 65 64 0a 2a 2a 20 74 6f 20 64 65 6c 65 74 65 20  ed.** to delete 
22a8f 61 6c 6c 20 73 74 61 74 20 74 61 62 6c 65 20 65  all stat table e
22a90 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  ntries..*/.stati
22a91 63 20 76 6f 69 64 20 6f 70 65 6e 53 74 61 74 54  c void openStatT
22a92 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70  able(.  Parse *p
22a93 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
22a94 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
22a95 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  xt */.  int iDb,
22a96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a97 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
22a98 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 69  we are looking i
22a99 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74  n */.  int iStat
22a9a 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Cur,           /
22a9b 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74  * Open the sqlit
22a9c 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 6f 6e  e_stat1 table on
22a9d 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a   this cursor */.
22a9e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57    const char *zW
22a9f 68 65 72 65 20 20 20 20 20 20 2f 2a 20 44 65 6c  here      /* Del
22aa0 65 74 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f  ete entries asso
22aa1 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
22aa2 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73   table */.){.  s
22aa3 74 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20  tatic struct {. 
22aa4 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
22aa5 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Name;.    const 
22aa6 63 68 61 72 20 2a 7a 43 6f 6c 73 3b 0a 20 20 7d  char *zCols;.  }
22aa7 20 61 54 61 62 6c 65 5b 5d 20 3d 20 7b 0a 20 20   aTable[] = {.  
22aa8 20 20 7b 20 22 73 71 6c 69 74 65 5f 73 74 61 74    { "sqlite_stat
22aa9 31 22 2c 20 22 74 62 6c 2c 69 64 78 2c 73 74 61  1", "tbl,idx,sta
22aaa 74 22 20 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c  t" },.#ifdef SQL
22aab 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
22aac 0a 20 20 20 20 7b 20 22 73 71 6c 69 74 65 5f 73  .    { "sqlite_s
22aad 74 61 74 32 22 2c 20 22 74 62 6c 2c 69 64 78 2c  tat2", "tbl,idx,
22aae 73 61 6d 70 6c 65 6e 6f 2c 73 61 6d 70 6c 65 22  sampleno,sample"
22aaf 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a   },.#endif.  };.
22ab0 0a 20 20 69 6e 74 20 61 52 6f 6f 74 5b 5d 20 3d  .  int aRoot[] =
22ab1 20 7b 30 2c 20 30 7d 3b 0a 20 20 75 38 20 61 43   {0, 0};.  u8 aC
22ab2 72 65 61 74 65 54 62 6c 5b 5d 20 3d 20 7b 30 2c  reateTbl[] = {0,
22ab3 20 30 7d 3b 0a 0a 20 20 69 6e 74 20 69 3b 0a 20   0};..  int i;. 
22ab4 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
22ab5 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20  Parse->db;.  Db 
22ab6 2a 70 44 62 3b 0a 20 20 56 64 62 65 20 2a 76 20  *pDb;.  Vdbe *v 
22ab7 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
22ab8 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
22ab9 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  v==0 ) return;. 
22aba 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
22abb 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
22abc 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 61 73  exes(db) );.  as
22abd 73 65 72 74 28 20 73 71 6c 69 74 65 33 56 64 62  sert( sqlite3Vdb
22abe 65 44 62 28 76 29 3d 3d 64 62 20 29 3b 0a 20 20  eDb(v)==db );.  
22abf 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
22ac0 44 62 5d 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  Db];..  for(i=0;
22ac1 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 54 61   i<ArraySize(aTa
22ac2 62 6c 65 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ble); i++){.    
22ac3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
22ac4 20 3d 20 61 54 61 62 6c 65 5b 69 5d 2e 7a 4e 61   = aTable[i].zNa
22ac5 6d 65 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  me;.    Table *p
22ac6 53 74 61 74 3b 0a 20 20 20 20 69 66 28 20 28 70  Stat;.    if( (p
22ac7 53 74 61 74 20 3d 20 73 71 6c 69 74 65 33 46 69  Stat = sqlite3Fi
22ac8 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
22ac9 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d 3d  , pDb->zName))==
22aca 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  0 ){.      /* Th
22acb 65 20 73 71 6c 69 74 65 5f 73 74 61 74 5b 31 32  e sqlite_stat[12
22acc 5d 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74  ] table does not
22acd 20 65 78 69 73 74 2e 20 43 72 65 61 74 65 20 69   exist. Create i
22ace 74 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 20 0a  t. Note that a .
22acf 20 20 20 20 20 20 2a 2a 20 73 69 64 65 2d 65 66        ** side-ef
22ad0 66 65 63 74 20 6f 66 20 74 68 65 20 43 52 45 41  fect of the CREA
22ad1 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
22ad2 6e 74 20 69 73 20 74 6f 20 6c 65 61 76 65 20 74  nt is to leave t
22ad3 68 65 20 72 6f 6f 74 70 61 67 65 20 0a 20 20 20  he rootpage .   
22ad4 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77     ** of the new
22ad5 20 74 61 62 6c 65 20 69 6e 20 72 65 67 69 73 74   table in regist
22ad6 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f  er pParse->regRo
22ad7 6f 74 2e 20 54 68 69 73 20 69 73 20 69 6d 70 6f  ot. This is impo
22ad8 72 74 61 6e 74 20 0a 20 20 20 20 20 20 2a 2a 20  rtant .      ** 
22ad9 62 65 63 61 75 73 65 20 74 68 65 20 4f 70 65 6e  because the Open
22ada 57 72 69 74 65 20 6f 70 63 6f 64 65 20 62 65 6c  Write opcode bel
22adb 6f 77 20 77 69 6c 6c 20 62 65 20 6e 65 65 64 69  ow will be needi
22adc 6e 67 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20  ng it. */.      
22add 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
22ade 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
22adf 20 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42       "CREATE TAB
22ae0 4c 45 20 25 51 2e 25 73 28 25 73 29 22 2c 20 70  LE %Q.%s(%s)", p
22ae1 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 54 61 62 2c  Db->zName, zTab,
22ae2 20 61 54 61 62 6c 65 5b 69 5d 2e 7a 43 6f 6c 73   aTable[i].zCols
22ae3 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
22ae4 61 52 6f 6f 74 5b 69 5d 20 3d 20 70 50 61 72 73  aRoot[i] = pPars
22ae5 65 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20 20 20  e->regRoot;.    
22ae6 20 20 61 43 72 65 61 74 65 54 62 6c 5b 69 5d 20    aCreateTbl[i] 
22ae7 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 1;.    }else{.
22ae8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
22ae9 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  le already exist
22aea 73 2e 20 49 66 20 7a 57 68 65 72 65 20 69 73 20  s. If zWhere is 
22aeb 6e 6f 74 20 4e 55 4c 4c 2c 20 64 65 6c 65 74 65  not NULL, delete
22aec 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 0a 20 20   all entries .  
22aed 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65      ** associate
22aee 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65  d with the table
22aef 20 7a 57 68 65 72 65 2e 20 49 66 20 7a 57 68 65   zWhere. If zWhe
22af0 72 65 20 69 73 20 4e 55 4c 4c 2c 20 64 65 6c 65  re is NULL, dele
22af1 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  te the.      ** 
22af2 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20  entire contents 
22af3 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 2a 2f  of the table. */
22af4 0a 20 20 20 20 20 20 61 52 6f 6f 74 5b 69 5d 20  .      aRoot[i] 
22af5 3d 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a 20  = pStat->tnum;. 
22af6 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c       sqlite3Tabl
22af7 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44  eLock(pParse, iD
22af8 62 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 31 2c 20  b, aRoot[i], 1, 
22af9 7a 54 61 62 29 3b 0a 20 20 20 20 20 20 69 66 28  zTab);.      if(
22afa 20 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 20 20   zWhere ){.     
22afb 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
22afc 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
22afd 20 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45           "DELETE
22afe 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52   FROM %Q.%s WHER
22aff 45 20 74 62 6c 3d 25 51 22 2c 20 70 44 62 2d 3e  E tbl=%Q", pDb->
22b00 7a 4e 61 6d 65 2c 20 7a 54 61 62 2c 20 7a 57 68  zName, zTab, zWh
22b01 65 72 65 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  ere.        );. 
22b02 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
22b03 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74      /* The sqlit
22b04 65 5f 73 74 61 74 5b 31 32 5d 20 74 61 62 6c 65  e_stat[12] table
22b05 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e   already exists.
22b06 20 20 44 65 6c 65 74 65 20 61 6c 6c 20 72 6f 77    Delete all row
22b07 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71  s. */.        sq
22b08 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22b09 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 61 52 6f  v, OP_Clear, aRo
22b0a 6f 74 5b 69 5d 2c 20 69 44 62 29 3b 0a 20 20 20  ot[i], iDb);.   
22b0b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
22b0c 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 71    /* Open the sq
22b0d 6c 69 74 65 5f 73 74 61 74 5b 31 32 5d 20 74 61  lite_stat[12] ta
22b0e 62 6c 65 73 20 66 6f 72 20 77 72 69 74 69 6e 67  bles for writing
22b0f 2e 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  . */.  for(i=0; 
22b10 69 3c 41 72 72 61 79 53 69 7a 65 28 61 54 61 62  i<ArraySize(aTab
22b11 6c 65 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  le); i++){.    s
22b12 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22b13 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  (v, OP_OpenWrite
22b14 2c 20 69 53 74 61 74 43 75 72 2b 69 2c 20 61 52  , iStatCur+i, aR
22b15 6f 6f 74 5b 69 5d 2c 20 69 44 62 29 3b 0a 20 20  oot[i], iDb);.  
22b16 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
22b17 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68  ngeP4(v, -1, (ch
22b18 61 72 20 2a 29 33 2c 20 50 34 5f 49 4e 54 33 32  ar *)3, P4_INT32
22b19 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22b1a 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 61 43  beChangeP5(v, aC
22b1b 72 65 61 74 65 54 62 6c 5b 69 5d 29 3b 0a 20 20  reateTbl[i]);.  
22b1c 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
22b1d 61 74 65 20 63 6f 64 65 20 74 6f 20 64 6f 20 61  ate code to do a
22b1e 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6c  n analysis of al
22b1f 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69  l indices associ
22b20 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 73  ated with.** a s
22b21 69 6e 67 6c 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a  ingle table..*/.
22b22 73 74 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c  static void anal
22b23 79 7a 65 4f 6e 65 54 61 62 6c 65 28 0a 20 20 50  yzeOneTable(.  P
22b24 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
22b25 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
22b26 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  t */.  Table *pT
22b27 61 62 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65  ab,     /* Table
22b28 20 77 68 6f 73 65 20 69 6e 64 69 63 65 73 20 61   whose indices a
22b29 72 65 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  re to be analyze
22b2a 64 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74  d */.  int iStat
22b2b 43 75 72 2c 20 20 20 20 2f 2a 20 49 6e 64 65 78  Cur,    /* Index
22b2c 20 6f 66 20 56 64 62 65 43 75 72 73 6f 72 20 74   of VdbeCursor t
22b2d 68 61 74 20 77 72 69 74 65 73 20 74 68 65 20 73  hat writes the s
22b2e 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
22b2f 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 20  e */.  int iMem 
22b30 20 20 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c          /* Avail
22b31 61 62 6c 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61  able memory loca
22b32 74 69 6f 6e 73 20 62 65 67 69 6e 20 68 65 72 65  tions begin here
22b33 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
22b34 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
22b35 62 3b 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73  b;    /* Databas
22b36 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 49 6e  e handle */.  In
22b37 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20  dex *pIdx;      
22b38 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e             /* An
22b39 20 69 6e 64 65 78 20 74 6f 20 62 65 69 6e 67 20   index to being 
22b3a 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e  analyzed */.  in
22b3b 74 20 69 49 64 78 43 75 72 3b 20 20 20 20 20 20  t iIdxCur;      
22b3c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
22b3d 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 69 6e 64  rsor open on ind
22b3e 65 78 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65  ex being analyze
22b3f 64 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  d */.  Vdbe *v; 
22b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22b41 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75      /* The virtu
22b42 61 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67  al machine being
22b43 20 62 75 69 6c 74 20 75 70 20 2a 2f 0a 20 20 69   built up */.  i
22b44 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
22b45 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
22b46 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
22b47 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f 70 3b 20   int topOfLoop; 
22b48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22b49 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   The top of the 
22b4a 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 65 6e  loop */.  int en
22b4b 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20  dOfLoop;        
22b4c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6e         /* The en
22b4d 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  d of the loop */
22b4e 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20  .  int addr;    
22b4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22b50 2f 2a 20 54 68 65 20 61 64 64 72 65 73 73 20 6f  /* The address o
22b51 66 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e  f an instruction
22b52 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
22b53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22b54 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64     /* Index of d
22b55 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
22b56 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74  ng pTab */.  int
22b57 20 72 65 67 54 61 62 6e 61 6d 65 20 3d 20 69 4d   regTabname = iM
22b58 65 6d 2b 2b 3b 20 20 20 20 20 2f 2a 20 52 65 67  em++;     /* Reg
22b59 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  ister containing
22b5a 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   table name */. 
22b5b 20 69 6e 74 20 72 65 67 49 64 78 6e 61 6d 65 20   int regIdxname 
22b5c 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 2f 2a  = iMem++;     /*
22b5d 20 52 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69   Register contai
22b5e 6e 69 6e 67 20 69 6e 64 65 78 20 6e 61 6d 65 20  ning index name 
22b5f 2a 2f 0a 20 20 69 6e 74 20 72 65 67 53 61 6d 70  */.  int regSamp
22b60 6c 65 6e 6f 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20  leno = iMem++;  
22b61 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 63 6f    /* Register co
22b62 6e 74 61 69 6e 69 6e 67 20 6e 65 78 74 20 73 61  ntaining next sa
22b63 6d 70 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  mple number */. 
22b64 20 69 6e 74 20 72 65 67 43 6f 6c 20 3d 20 69 4d   int regCol = iM
22b65 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 2f 2a  em++;         /*
22b66 20 43 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 6f   Content of a co
22b67 6c 75 6d 6e 20 61 6e 61 6c 79 7a 65 64 20 74 61  lumn analyzed ta
22b68 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ble */.  int reg
22b69 52 65 63 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20  Rec = iMem++;   
22b6a 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
22b6b 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6d 70 6c 65  r holding comple
22b6c 74 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  ted record */.  
22b6d 69 6e 74 20 72 65 67 54 65 6d 70 20 3d 20 69 4d  int regTemp = iM
22b6e 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 2f 2a 20  em++;        /* 
22b6f 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65  Temporary use re
22b70 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  gister */.  int 
22b71 72 65 67 52 6f 77 69 64 20 3d 20 69 4d 65 6d 2b  regRowid = iMem+
22b72 2b 3b 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69  +;       /* Rowi
22b73 64 20 66 6f 72 20 74 68 65 20 69 6e 73 65 72 74  d for the insert
22b74 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 0a 23 69  ed record */..#i
22b75 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
22b76 4c 45 5f 53 54 41 54 32 0a 20 20 69 6e 74 20 72  LE_STAT2.  int r
22b77 65 67 54 65 6d 70 32 20 3d 20 69 4d 65 6d 2b 2b  egTemp2 = iMem++
22b78 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f  ;       /* Tempo
22b79 72 61 72 79 20 75 73 65 20 72 65 67 69 73 74 65  rary use registe
22b7a 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 53 61  r */.  int regSa
22b7b 6d 70 6c 65 72 65 63 6e 6f 20 3d 20 69 4d 65 6d  mplerecno = iMem
22b7c 2b 2b 3b 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20  ++; /* Index of 
22b7d 6e 65 78 74 20 73 61 6d 70 6c 65 20 74 6f 20 72  next sample to r
22b7e 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 72  ecord */.  int r
22b7f 65 67 52 65 63 6e 6f 20 3d 20 69 4d 65 6d 2b 2b  egRecno = iMem++
22b80 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65  ;       /* Curre
22b81 6e 74 20 73 61 6d 70 6c 65 20 69 6e 64 65 78 20  nt sample index 
22b82 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c 61 73 74  */.  int regLast
22b83 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20   = iMem++;      
22b84 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6c 61    /* Index of la
22b85 73 74 20 73 61 6d 70 6c 65 20 74 6f 20 72 65 63  st sample to rec
22b86 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ord */.  int reg
22b87 46 69 72 73 74 20 3d 20 69 4d 65 6d 2b 2b 3b 20  First = iMem++; 
22b88 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
22b89 66 20 66 69 72 73 74 20 73 61 6d 70 6c 65 20 74  f first sample t
22b8a 6f 20 72 65 63 6f 72 64 20 2a 2f 0a 23 65 6e 64  o record */.#end
22b8b 69 66 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  if..  v = sqlite
22b8c 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
22b8d 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c 20  ;.  if( v==0 || 
22b8e 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20 7c  NEVER(pTab==0) |
22b8f 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d  | pTab->pIndex==
22b90 30 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e  0 ){.    /* Do n
22b91 6f 20 61 6e 61 6c 79 73 69 73 20 66 6f 72 20 74  o analysis for t
22b92 61 62 6c 65 73 20 74 68 61 74 20 68 61 76 65 20  ables that have 
22b93 6e 6f 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  no indices */.  
22b94 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
22b95 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
22b96 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
22b97 78 65 73 28 64 62 29 20 29 3b 0a 20 20 69 44 62  xes(db) );.  iDb
22b98 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
22b99 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
22b9a 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73  ->pSchema);.  as
22b9b 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a  sert( iDb>=0 );.
22b9c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22b9d 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
22b9e 4e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  N.  if( sqlite3A
22b9f 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
22ba0 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a 45 2c   SQLITE_ANALYZE,
22ba1 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c   pTab->zName, 0,
22ba2 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69  .      db->aDb[i
22ba3 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 7b 0a 20  Db].zName ) ){. 
22ba4 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23     return;.  }.#
22ba5 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 73 74 61  endif..  /* Esta
22ba6 62 6c 69 73 68 20 61 20 72 65 61 64 2d 6c 6f 63  blish a read-loc
22ba7 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 61  k on the table a
22ba8 74 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63  t the shared-cac
22ba9 68 65 20 6c 65 76 65 6c 2e 20 2a 2f 0a 20 20 73  he level. */.  s
22baa 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28  qlite3TableLock(
22bab 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61  pParse, iDb, pTa
22bac 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62  b->tnum, 0, pTab
22bad 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69 49 64  ->zName);..  iId
22bae 78 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  xCur = pParse->n
22baf 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64  Tab++;.  for(pId
22bb0 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
22bb1 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
22bb2 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74  >pNext){.    int
22bb3 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43   nCol = pIdx->nC
22bb4 6f 6c 75 6d 6e 3b 0a 20 20 20 20 4b 65 79 49 6e  olumn;.    KeyIn
22bb5 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74  fo *pKey = sqlit
22bb6 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70  e3IndexKeyinfo(p
22bb7 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 0a 20  Parse, pIdx);.. 
22bb8 20 20 20 69 66 28 20 69 4d 65 6d 2b 31 2b 28 6e     if( iMem+1+(n
22bb9 43 6f 6c 2a 32 29 3e 70 50 61 72 73 65 2d 3e 6e  Col*2)>pParse->n
22bba 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 50 61  Mem ){.      pPa
22bbb 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 69 4d 65 6d  rse->nMem = iMem
22bbc 2b 31 2b 28 6e 43 6f 6c 2a 32 29 3b 0a 20 20 20  +1+(nCol*2);.   
22bbd 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20   }..    /* Open 
22bbe 61 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  a cursor to the 
22bbf 69 6e 64 65 78 20 74 6f 20 62 65 20 61 6e 61 6c  index to be anal
22bc0 79 7a 65 64 2e 20 2a 2f 0a 20 20 20 20 61 73 73  yzed. */.    ass
22bc1 65 72 74 28 20 69 44 62 3d 3d 73 71 6c 69 74 65  ert( iDb==sqlite
22bc2 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
22bc3 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61  b, pIdx->pSchema
22bc4 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) );.    sqlite3
22bc5 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
22bc6 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64 78 43  _OpenRead, iIdxC
22bc7 75 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  ur, pIdx->tnum, 
22bc8 69 44 62 2c 0a 20 20 20 20 20 20 20 20 28 63 68  iDb,.        (ch
22bc9 61 72 20 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45  ar *)pKey, P4_KE
22bca 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
22bcb 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
22bcc 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d 3e  (v, "%s", pIdx->
22bcd 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20 20 20 2f 2a  zName));..    /*
22bce 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 72 65   Populate the re
22bcf 67 69 73 74 65 72 73 20 63 6f 6e 74 61 69 6e 69  gisters containi
22bd0 6e 67 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  ng the table and
22bd1 20 69 6e 64 65 78 20 6e 61 6d 65 73 2e 20 2a 2f   index names. */
22bd2 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70  .    if( pTab->p
22bd3 49 6e 64 65 78 3d 3d 70 49 64 78 20 29 7b 0a 20  Index==pIdx ){. 
22bd4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22bd5 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
22bd6 69 6e 67 38 2c 20 30 2c 20 72 65 67 54 61 62 6e  ing8, 0, regTabn
22bd7 61 6d 65 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e  ame, 0, pTab->zN
22bd8 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  ame, 0);.    }. 
22bd9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22bda 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
22bdb 67 38 2c 20 30 2c 20 72 65 67 49 64 78 6e 61 6d  g8, 0, regIdxnam
22bdc 65 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d  e, 0, pIdx->zNam
22bdd 65 2c 20 30 29 3b 0a 0a 23 69 66 64 65 66 20 53  e, 0);..#ifdef S
22bde 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
22bdf 54 32 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  T2..    /* If th
22be0 69 73 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  is iteration of 
22be1 74 68 65 20 6c 6f 6f 70 20 69 73 20 67 65 6e 65  the loop is gene
22be2 72 61 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 61  rating code to a
22be3 6e 61 6c 79 7a 65 20 74 68 65 0a 20 20 20 20 2a  nalyze the.    *
22be4 2a 20 66 69 72 73 74 20 69 6e 64 65 78 20 69 6e  * first index in
22be5 20 74 68 65 20 70 54 61 62 2d 3e 70 49 6e 64 65   the pTab->pInde
22be6 78 20 6c 69 73 74 2c 20 74 68 65 6e 20 72 65 67  x list, then reg
22be7 69 73 74 65 72 20 72 65 67 4c 61 73 74 20 68 61  ister regLast ha
22be8 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 65  s.    ** not bee
22be9 6e 20 70 6f 70 75 6c 61 74 65 64 2e 20 49 6e 20  n populated. In 
22bea 74 68 69 73 20 63 61 73 65 20 70 6f 70 75 6c 61  this case popula
22beb 74 65 20 69 74 20 6e 6f 77 2e 20 20 2a 2f 0a 20  te it now.  */. 
22bec 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70 49 6e     if( pTab->pIn
22bed 64 65 78 3d 3d 70 49 64 78 20 29 7b 0a 20 20 20  dex==pIdx ){.   
22bee 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22bef 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
22bf0 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  er, SQLITE_INDEX
22bf1 5f 53 41 4d 50 4c 45 53 2c 20 72 65 67 53 61 6d  _SAMPLES, regSam
22bf2 70 6c 65 72 65 63 6e 6f 29 3b 0a 20 20 20 20 20  plerecno);.     
22bf3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22bf4 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
22bf5 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53  , SQLITE_INDEX_S
22bf6 41 4d 50 4c 45 53 2a 32 2d 31 2c 20 72 65 67 54  AMPLES*2-1, regT
22bf7 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  emp);.      sqli
22bf8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22bf9 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 53 51 4c   OP_Integer, SQL
22bfa 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45  ITE_INDEX_SAMPLE
22bfb 53 2a 32 2c 20 72 65 67 54 65 6d 70 32 29 3b 0a  S*2, regTemp2);.
22bfc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22bfd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
22bfe 6f 75 6e 74 2c 20 69 49 64 78 43 75 72 2c 20 72  ount, iIdxCur, r
22bff 65 67 4c 61 73 74 29 3b 0a 20 20 20 20 20 20 73  egLast);.      s
22c00 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22c01 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
22c02 72 65 67 46 69 72 73 74 29 3b 0a 20 20 20 20 20  regFirst);.     
22c03 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
22c04 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
22c05 4c 74 2c 20 72 65 67 53 61 6d 70 6c 65 72 65 63  Lt, regSamplerec
22c06 6e 6f 2c 20 30 2c 20 72 65 67 4c 61 73 74 29 3b  no, 0, regLast);
22c07 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22c08 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44  beAddOp3(v, OP_D
22c09 69 76 69 64 65 2c 20 72 65 67 54 65 6d 70 32 2c  ivide, regTemp2,
22c0a 20 72 65 67 4c 61 73 74 2c 20 72 65 67 46 69 72   regLast, regFir
22c0b 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  st);.      sqlit
22c0c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
22c0d 4f 50 5f 4d 75 6c 74 69 70 6c 79 2c 20 72 65 67  OP_Multiply, reg
22c0e 4c 61 73 74 2c 20 72 65 67 54 65 6d 70 2c 20 72  Last, regTemp, r
22c0f 65 67 4c 61 73 74 29 3b 0a 20 20 20 20 20 20 73  egLast);.      s
22c10 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22c11 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72  (v, OP_AddImm, r
22c12 65 67 4c 61 73 74 2c 20 53 51 4c 49 54 45 5f 49  egLast, SQLITE_I
22c13 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2a 32 2d 32  NDEX_SAMPLES*2-2
22c14 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
22c15 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
22c16 5f 44 69 76 69 64 65 2c 20 20 72 65 67 54 65 6d  _Divide,  regTem
22c17 70 32 2c 20 72 65 67 4c 61 73 74 2c 20 72 65 67  p2, regLast, reg
22c18 4c 61 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  Last);.      sql
22c19 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
22c1a 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 7d  (v, addr);.    }
22c1b 0a 0a 20 20 20 20 2f 2a 20 5a 65 72 6f 20 74 68  ..    /* Zero th
22c1c 65 20 72 65 67 53 61 6d 70 6c 65 6e 6f 20 61 6e  e regSampleno an
22c1d 64 20 72 65 67 52 65 63 6e 6f 20 72 65 67 69 73  d regRecno regis
22c1e 74 65 72 73 2e 20 2a 2f 0a 20 20 20 20 73 71 6c  ters. */.    sql
22c1f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22c20 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
22c21 20 72 65 67 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20   regSampleno);. 
22c22 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22c23 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
22c24 65 72 2c 20 30 2c 20 72 65 67 52 65 63 6e 6f 29  er, 0, regRecno)
22c25 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
22c26 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
22c27 70 79 2c 20 72 65 67 46 69 72 73 74 2c 20 72 65  py, regFirst, re
22c28 67 53 61 6d 70 6c 65 72 65 63 6e 6f 29 3b 0a 23  gSamplerecno);.#
22c29 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68  endif..    /* Th
22c2a 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  e block of memor
22c2b 79 20 63 65 6c 6c 73 20 69 6e 69 74 69 61 6c 69  y cells initiali
22c2c 7a 65 64 20 68 65 72 65 20 69 73 20 75 73 65 64  zed here is used
22c2d 20 61 73 20 66 6f 6c 6c 6f 77 73 2e 0a 20 20 20   as follows..   
22c2e 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69 4d   **.    **    iM
22c2f 65 6d 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  em:             
22c30 20 20 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20     .    **      
22c31 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62    The total numb
22c32 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  er of rows in th
22c33 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a  e table..    **.
22c34 20 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d 2b 31      **    iMem+1
22c35 20 2e 2e 20 69 4d 65 6d 2b 6e 43 6f 6c 3a 20 0a   .. iMem+nCol: .
22c36 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 4e 75      **        Nu
22c37 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74  mber of distinct
22c38 20 65 6e 74 72 69 65 73 20 69 6e 20 69 6e 64 65   entries in inde
22c39 78 20 63 6f 6e 73 69 64 65 72 69 6e 67 20 74 68  x considering th
22c3a 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  e .    **       
22c3b 20 6c 65 66 74 2d 6d 6f 73 74 20 4e 20 63 6f 6c   left-most N col
22c3c 75 6d 6e 73 20 6f 6e 6c 79 2c 20 77 68 65 72 65  umns only, where
22c3d 20 4e 20 69 73 20 62 65 74 77 65 65 6e 20 31 20   N is between 1 
22c3e 61 6e 64 20 6e 43 6f 6c 2c 20 0a 20 20 20 20 2a  and nCol, .    *
22c3f 2a 20 20 20 20 20 20 20 20 69 6e 63 6c 75 73 69  *        inclusi
22c40 76 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ve..    **.    *
22c41 2a 20 20 20 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 31  *    iMem+nCol+1
22c42 20 2e 2e 20 4d 65 6d 2b 32 2a 6e 43 6f 6c 3a 20   .. Mem+2*nCol: 
22c43 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20   .    **        
22c44 50 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 6f  Previous value o
22c45 66 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  f indexed column
22c46 73 2c 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20  s, from left to 
22c47 72 69 67 68 74 2e 0a 20 20 20 20 2a 2a 0a 20 20  right..    **.  
22c48 20 20 2a 2a 20 43 65 6c 6c 73 20 69 4d 65 6d 20    ** Cells iMem 
22c49 74 68 72 6f 75 67 68 20 69 4d 65 6d 2b 6e 43 6f  through iMem+nCo
22c4a 6c 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65  l are initialize
22c4b 64 20 74 6f 20 30 2e 20 54 68 65 20 6f 74 68 65  d to 0. The othe
22c4c 72 73 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 69  rs are .    ** i
22c4d 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 63 6f  nitialized to co
22c4e 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c  ntain an SQL NUL
22c4f 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  L..    */.    fo
22c50 72 28 69 3d 30 3b 20 69 3c 3d 6e 43 6f 6c 3b 20  r(i=0; i<=nCol; 
22c51 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
22c52 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22c53 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
22c54 69 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 7d 0a 20  iMem+i);.    }. 
22c55 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
22c56 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
22c57 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22c58 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
22c59 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b   iMem+nCol+i+1);
22c5a 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53  .    }..    /* S
22c5b 74 61 72 74 20 74 68 65 20 61 6e 61 6c 79 73 69  tart the analysi
22c5c 73 20 6c 6f 6f 70 2e 20 54 68 69 73 20 6c 6f 6f  s loop. This loo
22c5d 70 20 72 75 6e 73 20 74 68 72 6f 75 67 68 20 61  p runs through a
22c5e 6c 6c 20 74 68 65 20 65 6e 74 72 69 65 73 20 69  ll the entries i
22c5f 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 64  n.    ** the ind
22c60 65 78 20 62 2d 74 72 65 65 2e 20 20 2a 2f 0a 20  ex b-tree.  */. 
22c61 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73     endOfLoop = s
22c62 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
22c63 62 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69  bel(v);.    sqli
22c64 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22c65 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49 64 78   OP_Rewind, iIdx
22c66 43 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b  Cur, endOfLoop);
22c67 0a 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 20 3d  .    topOfLoop =
22c68 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
22c69 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
22c6a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22c6b 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
22c6c 69 4d 65 6d 2c 20 31 29 3b 0a 0a 20 20 20 20 66  iMem, 1);..    f
22c6d 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
22c6e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
22c6f 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
22c70 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78   OP_Column, iIdx
22c71 43 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29 3b  Cur, i, regCol);
22c72 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
22c73 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20 20 20 20  NABLE_STAT2.    
22c74 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a 20 20    if( i==0 ){.  
22c75 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69        /* Check i
22c76 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 68 61  f the record tha
22c77 74 20 63 75 72 73 6f 72 20 69 49 64 78 43 75 72  t cursor iIdxCur
22c78 20 70 6f 69 6e 74 73 20 74 6f 20 63 6f 6e 74 61   points to conta
22c79 69 6e 73 20 61 0a 20 20 20 20 20 20 20 20 2a 2a  ins a.        **
22c7a 20 76 61 6c 75 65 20 74 68 61 74 20 73 68 6f 75   value that shou
22c7b 6c 64 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20  ld be stored in 
22c7c 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32  the sqlite_stat2
22c7d 20 74 61 62 6c 65 2e 20 49 66 20 73 6f 2c 0a 20   table. If so,. 
22c7e 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 20         ** store 
22c7f 69 74 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  it.  */.        
22c80 69 6e 74 20 6e 65 20 3d 20 73 71 6c 69 74 65 33  int ne = sqlite3
22c81 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
22c82 5f 4e 65 2c 20 72 65 67 52 65 63 6e 6f 2c 20 30  _Ne, regRecno, 0
22c83 2c 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f  , regSamplerecno
22c84 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
22c85 74 28 20 72 65 67 54 61 62 6e 61 6d 65 2b 31 3d  t( regTabname+1=
22c86 3d 72 65 67 49 64 78 6e 61 6d 65 20 0a 20 20 20  =regIdxname .   
22c87 20 20 20 20 20 20 20 20 20 20 26 26 20 72 65 67            && reg
22c88 54 61 62 6e 61 6d 65 2b 32 3d 3d 72 65 67 53 61  Tabname+2==regSa
22c89 6d 70 6c 65 6e 6f 0a 20 20 20 20 20 20 20 20 20  mpleno.         
22c8a 20 20 20 20 26 26 20 72 65 67 54 61 62 6e 61 6d      && regTabnam
22c8b 65 2b 33 3d 3d 72 65 67 43 6f 6c 0a 20 20 20 20  e+3==regCol.    
22c8c 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 73      );.        s
22c8d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
22c8e 50 35 28 76 2c 20 53 51 4c 49 54 45 5f 4a 55 4d  P5(v, SQLITE_JUM
22c8f 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20  PIFNULL);.      
22c90 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22c91 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
22c92 63 6f 72 64 2c 20 72 65 67 54 61 62 6e 61 6d 65  cord, regTabname
22c93 2c 20 34 2c 20 72 65 67 52 65 63 2c 20 22 61 61  , 4, regRec, "aa
22c94 61 62 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ab", 0);.       
22c95 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22c96 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
22c97 64 2c 20 69 53 74 61 74 43 75 72 2b 31 2c 20 72  d, iStatCur+1, r
22c98 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  egRowid);.      
22c99 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22c9a 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
22c9b 2c 20 69 53 74 61 74 43 75 72 2b 31 2c 20 72 65  , iStatCur+1, re
22c9c 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64 29 3b  gRec, regRowid);
22c9d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6c  ..        /* Cal
22c9e 63 75 6c 61 74 65 20 6e 65 77 20 76 61 6c 75 65  culate new value
22c9f 73 20 66 6f 72 20 72 65 67 53 61 6d 70 6c 65 72  s for regSampler
22ca0 65 63 6e 6f 20 61 6e 64 20 72 65 67 53 61 6d 70  ecno and regSamp
22ca1 6c 65 6e 6f 2e 0a 20 20 20 20 20 20 20 20 2a 2a  leno..        **
22ca2 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 73 61  .        **   sa
22ca3 6d 70 6c 65 6e 6f 20 3d 20 73 61 6d 70 6c 65 6e  mpleno = samplen
22ca4 6f 20 2b 20 31 0a 20 20 20 20 20 20 20 20 2a 2a  o + 1.        **
22ca5 20 20 20 73 61 6d 70 6c 65 72 65 63 6e 6f 20 3d     samplerecno =
22ca6 20 73 61 6d 70 6c 65 72 65 63 6e 6f 2b 28 72 65   samplerecno+(re
22ca7 6d 61 69 6e 69 6e 67 20 72 65 63 6f 72 64 73 29  maining records)
22ca8 2f 28 72 65 6d 61 69 6e 69 6e 67 20 73 61 6d 70  /(remaining samp
22ca9 6c 65 73 29 0a 20 20 20 20 20 20 20 20 2a 2f 0a  les).        */.
22caa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
22cab 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22cac 41 64 64 49 6d 6d 2c 20 72 65 67 53 61 6d 70 6c  AddImm, regSampl
22cad 65 6e 6f 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  eno, 1);.       
22cae 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22caf 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 63  p3(v, OP_Subtrac
22cb0 74 2c 20 72 65 67 52 65 63 6e 6f 2c 20 72 65 67  t, regRecno, reg
22cb1 4c 61 73 74 2c 20 72 65 67 54 65 6d 70 29 3b 0a  Last, regTemp);.
22cb2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
22cb3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22cb4 41 64 64 49 6d 6d 2c 20 72 65 67 54 65 6d 70 2c  AddImm, regTemp,
22cb5 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71   -1);.        sq
22cb6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22cb7 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 53  v, OP_Integer, S
22cb8 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
22cb9 4c 45 53 2c 20 72 65 67 54 65 6d 70 32 29 3b 0a  LES, regTemp2);.
22cba 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
22cbb 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
22cbc 53 75 62 74 72 61 63 74 2c 20 72 65 67 53 61 6d  Subtract, regSam
22cbd 70 6c 65 6e 6f 2c 20 72 65 67 54 65 6d 70 32 2c  pleno, regTemp2,
22cbe 20 72 65 67 54 65 6d 70 32 29 3b 0a 20 20 20 20   regTemp2);.    
22cbf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22cc0 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 69 76 69  ddOp3(v, OP_Divi
22cc1 64 65 2c 20 72 65 67 54 65 6d 70 32 2c 20 72 65  de, regTemp2, re
22cc2 67 54 65 6d 70 2c 20 72 65 67 54 65 6d 70 29 3b  gTemp, regTemp);
22cc3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22cc4 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
22cc5 5f 41 64 64 2c 20 72 65 67 53 61 6d 70 6c 65 72  _Add, regSampler
22cc6 65 63 6e 6f 2c 20 72 65 67 54 65 6d 70 2c 20 72  ecno, regTemp, r
22cc7 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 29 3b 0a  egSamplerecno);.
22cc8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22cc9 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
22cca 6e 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ne);.        sql
22ccb 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22ccc 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67  , OP_AddImm, reg
22ccd 52 65 63 6e 6f 2c 20 31 29 3b 0a 20 20 20 20 20  Recno, 1);.     
22cce 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20   }.#endif..     
22ccf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22cd0 70 33 28 76 2c 20 4f 50 5f 4e 65 2c 20 72 65 67  p3(v, OP_Ne, reg
22cd1 43 6f 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f  Col, 0, iMem+nCo
22cd2 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 20 20 2f 2a  l+i+1);.      /*
22cd3 2a 2a 2a 20 54 4f 44 4f 3a 20 20 61 64 64 20 63  *** TODO:  add c
22cd4 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
22cd5 65 20 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 20 20 73  e *****/.      s
22cd6 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
22cd7 50 35 28 76 2c 20 53 51 4c 49 54 45 5f 4a 55 4d  P5(v, SQLITE_JUM
22cd8 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a  PIFNULL);.    }.
22cd9 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
22cda 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
22cdb 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c 6f 63    /* If a malloc
22cdc 20 66 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63   failure has occ
22cdd 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20  urred, then the 
22cde 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 65 78  result of the ex
22cdf 70 72 65 73 73 69 6f 6e 20 0a 20 20 20 20 20 20  pression .      
22ce0 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ** passed as the
22ce1 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
22ce2 20 74 6f 20 74 68 65 20 63 61 6c 6c 20 74 6f 20   to the call to 
22ce3 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
22ce4 65 72 65 28 29 20 0a 20 20 20 20 20 20 2a 2a 20  ere() .      ** 
22ce5 62 65 6c 6f 77 20 6d 61 79 20 62 65 20 6e 65 67  below may be neg
22ce6 61 74 69 76 65 2e 20 57 68 69 63 68 20 63 61 75  ative. Which cau
22ce7 73 65 73 20 61 6e 20 61 73 73 65 72 74 28 29 20  ses an assert() 
22ce8 74 6f 20 66 61 69 6c 20 28 6f 72 20 61 6e 0a 20  to fail (or an. 
22ce9 20 20 20 20 20 2a 2a 20 6f 75 74 2d 6f 66 2d 62       ** out-of-b
22cea 6f 75 6e 64 73 20 77 72 69 74 65 20 69 66 20 53  ounds write if S
22ceb 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 6e  QLITE_DEBUG is n
22cec 6f 74 20 64 65 66 69 6e 65 64 29 2e 20 20 2a 2f  ot defined).  */
22ced 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
22cee 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
22cef 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
22cf0 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f 66 4c  _Goto, 0, endOfL
22cf1 6f 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  oop);.    for(i=
22cf2 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
22cf3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22cf4 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 73 71  beJumpHere(v, sq
22cf5 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
22cf6 41 64 64 72 28 76 29 2d 28 6e 43 6f 6c 2a 32 29  Addr(v)-(nCol*2)
22cf7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
22cf8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
22cf9 5f 41 64 64 49 6d 6d 2c 20 69 4d 65 6d 2b 69 2b  _AddImm, iMem+i+
22cfa 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  1, 1);.      sql
22cfb 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22cfc 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64  , OP_Column, iId
22cfd 78 43 75 72 2c 20 69 2c 20 69 4d 65 6d 2b 6e 43  xCur, i, iMem+nC
22cfe 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a  ol+i+1);.    }..
22cff 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68      /* End of th
22d00 65 20 61 6e 61 6c 79 73 69 73 20 6c 6f 6f 70 2e  e analysis loop.
22d01 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
22d02 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
22d03 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20  v, endOfLoop);. 
22d04 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22d05 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
22d06 20 69 49 64 78 43 75 72 2c 20 74 6f 70 4f 66 4c   iIdxCur, topOfL
22d07 6f 6f 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  oop);.    sqlite
22d08 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
22d09 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78 43 75 72  P_Close, iIdxCur
22d0a 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65  );..    /* Store
22d0b 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20   the results in 
22d0c 73 71 6c 69 74 65 5f 73 74 61 74 31 2e 0a 20 20  sqlite_stat1..  
22d0d 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
22d0e 72 65 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67  result is a sing
22d0f 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 73 71  le row of the sq
22d10 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
22d11 2e 20 20 54 68 65 20 66 69 72 73 74 0a 20 20 20  .  The first.   
22d12 20 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 20   ** two columns 
22d13 61 72 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  are the names of
22d14 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69   the table and i
22d15 6e 64 65 78 2e 20 20 54 68 65 20 74 68 69 72 64  ndex.  The third
22d16 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 69   column.    ** i
22d17 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6d 70 6f  s a string compo
22d18 73 65 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66  sed of a list of
22d19 20 69 6e 74 65 67 65 72 20 73 74 61 74 69 73 74   integer statist
22d1a 69 63 73 20 61 62 6f 75 74 20 74 68 65 0a 20 20  ics about the.  
22d1b 20 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65    ** index.  The
22d1c 20 66 69 72 73 74 20 69 6e 74 65 67 65 72 20 69   first integer i
22d1d 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 74 68  n the list is th
22d1e 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
22d1f 66 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a  f entries.    **
22d20 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 20   in the index.  
22d21 54 68 65 72 65 20 69 73 20 6f 6e 65 20 61 64 64  There is one add
22d22 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20  itional integer 
22d23 69 6e 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20  in the list for 
22d24 65 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75  each.    ** colu
22d25 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  mn of the table.
22d26 20 20 54 68 69 73 20 61 64 64 69 74 69 6f 6e 61    This additiona
22d27 6c 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 67  l integer is a g
22d28 75 65 73 73 20 6f 66 20 68 6f 77 20 6d 61 6e 79  uess of how many
22d29 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f 66 20  .    ** rows of 
22d2a 74 68 65 20 74 61 62 6c 65 20 74 68 65 20 69 6e  the table the in
22d2b 64 65 78 20 77 69 6c 6c 20 73 65 6c 65 63 74 2e  dex will select.
22d2c 20 20 49 66 20 44 20 69 73 20 74 68 65 20 63 6f    If D is the co
22d2d 75 6e 74 20 6f 66 20 64 69 73 74 69 6e 63 74 0a  unt of distinct.
22d2e 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 61 6e      ** values an
22d2f 64 20 4b 20 69 73 20 74 68 65 20 74 6f 74 61 6c  d K is the total
22d30 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 2c   number of rows,
22d31 20 74 68 65 6e 20 74 68 65 20 69 6e 74 65 67 65   then the intege
22d32 72 20 69 73 20 63 6f 6d 70 75 74 65 64 0a 20 20  r is computed.  
22d33 20 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a 2a 0a    ** as:.    **.
22d34 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 49 20      **        I 
22d35 3d 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20 20 20  = (K+D-1)/D.    
22d36 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3d 3d  **.    ** If K==
22d37 30 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 79 20  0 then no entry 
22d38 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 68 65  is made into the
22d39 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
22d3a 62 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20 49 66  ble.  .    ** If
22d3b 20 4b 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20   K>0 then it is 
22d3c 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 20  always the case 
22d3d 74 68 65 20 44 3e 30 20 73 6f 20 64 69 76 69 73  the D>0 so divis
22d3e 69 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20 20 20  ion by zero.    
22d3f 2a 2a 20 69 73 20 6e 65 76 65 72 20 70 6f 73 73  ** is never poss
22d40 69 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ible..    */.   
22d41 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
22d42 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
22d43 49 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a 20 20  IfNot, iMem);.  
22d44 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22d45 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
22d46 20 69 4d 65 6d 2c 20 72 65 67 53 61 6d 70 6c 65   iMem, regSample
22d47 6e 6f 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  no);.    for(i=0
22d48 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
22d49 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22d4a 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
22d4b 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 54 65 6d  ring8, 0, regTem
22d4c 70 2c 20 30 2c 20 22 20 22 2c 20 30 29 3b 0a 20  p, 0, " ", 0);. 
22d4d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22d4e 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6e  AddOp3(v, OP_Con
22d4f 63 61 74 2c 20 72 65 67 54 65 6d 70 2c 20 72 65  cat, regTemp, re
22d50 67 53 61 6d 70 6c 65 6e 6f 2c 20 72 65 67 53 61  gSampleno, regSa
22d51 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20 20 20 20 73  mpleno);.      s
22d52 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22d53 28 76 2c 20 4f 50 5f 41 64 64 2c 20 69 4d 65 6d  (v, OP_Add, iMem
22d54 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 72 65 67 54  , iMem+i+1, regT
22d55 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  emp);.      sqli
22d56 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22d57 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 54   OP_AddImm, regT
22d58 65 6d 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  emp, -1);.      
22d59 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22d5a 33 28 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20  3(v, OP_Divide, 
22d5b 69 4d 65 6d 2b 69 2b 31 2c 20 72 65 67 54 65 6d  iMem+i+1, regTem
22d5c 70 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20  p, regTemp);.   
22d5d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22d5e 64 4f 70 31 28 76 2c 20 4f 50 5f 54 6f 49 6e 74  dOp1(v, OP_ToInt
22d5f 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20  , regTemp);.    
22d60 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22d61 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74  Op3(v, OP_Concat
22d62 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67 53 61  , regTemp, regSa
22d63 6d 70 6c 65 6e 6f 2c 20 72 65 67 53 61 6d 70 6c  mpleno, regSampl
22d64 65 6e 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  eno);.    }.    
22d65 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22d66 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  4(v, OP_MakeReco
22d67 72 64 2c 20 72 65 67 54 61 62 6e 61 6d 65 2c 20  rd, regTabname, 
22d68 33 2c 20 72 65 67 52 65 63 2c 20 22 61 61 61 22  3, regRec, "aaa"
22d69 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
22d6a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22d6b 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53 74 61  P_NewRowid, iSta
22d6c 74 43 75 72 2c 20 72 65 67 52 6f 77 69 64 29 3b  tCur, regRowid);
22d6d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22d6e 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
22d6f 65 72 74 2c 20 69 53 74 61 74 43 75 72 2c 20 72  ert, iStatCur, r
22d70 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64 29  egRec, regRowid)
22d71 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
22d72 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46  eChangeP5(v, OPF
22d73 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20  LAG_APPEND);.   
22d74 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
22d75 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20  Here(v, addr);. 
22d76 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65   }.}../*.** Gene
22d77 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
22d78 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20 6d 6f  ill cause the mo
22d79 73 74 20 72 65 63 65 6e 74 20 69 6e 64 65 78 20  st recent index 
22d7a 61 6e 61 6c 79 73 69 73 20 74 6f 0a 2a 2a 20 62  analysis to.** b
22d7b 65 20 6c 61 6f 64 65 64 20 69 6e 74 6f 20 69 6e  e laoded into in
22d7c 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 6c  ternal hash tabl
22d7d 65 73 20 77 68 65 72 65 20 69 73 20 63 61 6e 20  es where is can 
22d7e 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 74  be used..*/.stat
22d7f 69 63 20 76 6f 69 64 20 6c 6f 61 64 41 6e 61 6c  ic void loadAnal
22d80 79 73 69 73 28 50 61 72 73 65 20 2a 70 50 61 72  ysis(Parse *pPar
22d81 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20  se, int iDb){.  
22d82 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
22d83 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
22d84 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20  ;.  if( v ){.   
22d85 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22d86 70 31 28 76 2c 20 4f 50 5f 4c 6f 61 64 41 6e 61  p1(v, OP_LoadAna
22d87 6c 79 73 69 73 2c 20 69 44 62 29 3b 0a 20 20 7d  lysis, iDb);.  }
22d88 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
22d89 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
22d8a 6c 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73  l do an analysis
22d8b 20 6f 66 20 61 6e 20 65 6e 74 69 72 65 20 64 61   of an entire da
22d8c 74 61 62 61 73 65 0a 2a 2f 0a 73 74 61 74 69 63  tabase.*/.static
22d8d 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 44 61 74   void analyzeDat
22d8e 61 62 61 73 65 28 50 61 72 73 65 20 2a 70 50 61  abase(Parse *pPa
22d8f 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20  rse, int iDb){. 
22d90 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
22d91 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 53 63 68  Parse->db;.  Sch
22d92 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 64  ema *pSchema = d
22d93 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
22d94 65 6d 61 3b 20 20 20 20 2f 2a 20 53 63 68 65 6d  ema;    /* Schem
22d95 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 69 44  a of database iD
22d96 62 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  b */.  HashElem 
22d97 2a 6b 3b 0a 20 20 69 6e 74 20 69 53 74 61 74 43  *k;.  int iStatC
22d98 75 72 3b 0a 20 20 69 6e 74 20 69 4d 65 6d 3b 0a  ur;.  int iMem;.
22d99 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57  .  sqlite3BeginW
22d9a 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
22d9b 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20  arse, 0, iDb);. 
22d9c 20 69 53 74 61 74 43 75 72 20 3d 20 70 50 61 72   iStatCur = pPar
22d9d 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 70 50 61 72  se->nTab;.  pPar
22d9e 73 65 2d 3e 6e 54 61 62 20 2b 3d 20 32 3b 0a 20  se->nTab += 2;. 
22d9f 20 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70   openStatTable(p
22da0 50 61 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61  Parse, iDb, iSta
22da1 74 43 75 72 2c 20 30 29 3b 0a 20 20 69 4d 65 6d  tCur, 0);.  iMem
22da2 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b   = pParse->nMem+
22da3 31 3b 0a 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74  1;.  for(k=sqlit
22da4 65 48 61 73 68 46 69 72 73 74 28 26 70 53 63 68  eHashFirst(&pSch
22da5 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 6b  ema->tblHash); k
22da6 3b 20 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; k=sqliteHashNe
22da7 78 74 28 6b 29 29 7b 0a 20 20 20 20 54 61 62 6c  xt(k)){.    Tabl
22da8 65 20 2a 70 54 61 62 20 3d 20 28 54 61 62 6c 65  e *pTab = (Table
22da9 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  *)sqliteHashData
22daa 28 6b 29 3b 0a 20 20 20 20 61 6e 61 6c 79 7a 65  (k);.    analyze
22dab 4f 6e 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  OneTable(pParse,
22dac 20 70 54 61 62 2c 20 69 53 74 61 74 43 75 72 2c   pTab, iStatCur,
22dad 20 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 6c 6f   iMem);.  }.  lo
22dae 61 64 41 6e 61 6c 79 73 69 73 28 70 50 61 72 73  adAnalysis(pPars
22daf 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  e, iDb);.}../*.*
22db0 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
22db1 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20  that will do an 
22db2 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 20 73 69  analysis of a si
22db3 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a  ngle table in.**
22db4 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a   a database..*/.
22db5 73 74 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c  static void anal
22db6 79 7a 65 54 61 62 6c 65 28 50 61 72 73 65 20 2a  yzeTable(Parse *
22db7 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
22db8 54 61 62 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b  Tab){.  int iDb;
22db9 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 3b  .  int iStatCur;
22dba 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ..  assert( pTab
22dbb 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
22dbc 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
22dbd 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61  dsAllMutexes(pPa
22dbe 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44  rse->db) );.  iD
22dbf 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
22dc0 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
22dc1 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
22dc2 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65  ma);.  sqlite3Be
22dc3 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
22dc4 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
22dc5 29 3b 0a 20 20 69 53 74 61 74 43 75 72 20 3d 20  );.  iStatCur = 
22dc6 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20  pParse->nTab;.  
22dc7 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 2b 3d 20  pParse->nTab += 
22dc8 32 3b 0a 20 20 6f 70 65 6e 53 74 61 74 54 61 62  2;.  openStatTab
22dc9 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  le(pParse, iDb, 
22dca 69 53 74 61 74 43 75 72 2c 20 70 54 61 62 2d 3e  iStatCur, pTab->
22dcb 7a 4e 61 6d 65 29 3b 0a 20 20 61 6e 61 6c 79 7a  zName);.  analyz
22dcc 65 4f 6e 65 54 61 62 6c 65 28 70 50 61 72 73 65  eOneTable(pParse
22dcd 2c 20 70 54 61 62 2c 20 69 53 74 61 74 43 75 72  , pTab, iStatCur
22dce 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31  , pParse->nMem+1
22dcf 29 3b 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69  );.  loadAnalysi
22dd0 73 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  s(pParse, iDb);.
22dd1 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
22dd2 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 41  e code for the A
22dd3 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 20  NALYZE command. 
22dd4 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
22dd5 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  s this routine.*
22dd6 2a 20 77 68 65 6e 20 69 74 20 72 65 63 6f 67 6e  * when it recogn
22dd7 69 7a 65 73 20 61 6e 20 41 4e 41 4c 59 5a 45 20  izes an ANALYZE 
22dd8 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20  command..**.**  
22dd9 20 20 20 20 20 20 41 4e 41 4c 59 5a 45 20 20 20        ANALYZE   
22dda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22ddb 20 20 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a 2a           -- 1.**
22ddc 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a 45 20          ANALYZE 
22ddd 20 3c 64 61 74 61 62 61 73 65 3e 20 20 20 20 20   <database>     
22dde 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0a             -- 2.
22ddf 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a  **        ANALYZ
22de0 45 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f  E  ?<database>.?
22de1 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d 20  <tablename>  -- 
22de2 33 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20 63  3.**.** Form 1 c
22de3 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69 63 65  auses all indice
22de4 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63 68 65  s in all attache
22de5 64 20 64 61 74 61 62 61 73 65 73 20 74 6f 20 62  d databases to b
22de6 65 20 61 6e 61 6c 79 7a 65 64 2e 0a 2a 2a 20 46  e analyzed..** F
22de7 6f 72 6d 20 32 20 61 6e 61 6c 79 7a 65 73 20 61  orm 2 analyzes a
22de8 6c 6c 20 69 6e 64 69 63 65 73 20 74 68 65 20 73  ll indices the s
22de9 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 6e  ingle database n
22dea 61 6d 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20 33 20  amed..** Form 3 
22deb 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e 64  analyzes all ind
22dec 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ices associated 
22ded 77 69 74 68 20 74 68 65 20 6e 61 6d 65 64 20 74  with the named t
22dee 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
22def 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
22df0 69 74 65 33 41 6e 61 6c 79 7a 65 28 50 61 72 73  ite3Analyze(Pars
22df1 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
22df2 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65 6e 20   *pName1, Token 
22df3 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 73 71 6c 69  *pName2){.  sqli
22df4 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
22df5 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b  ->db;.  int iDb;
22df6 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72  .  int i;.  char
22df7 20 2a 7a 2c 20 2a 7a 44 62 3b 0a 20 20 54 61 62   *z, *zDb;.  Tab
22df8 6c 65 20 2a 70 54 61 62 3b 0a 20 20 54 6f 6b 65  le *pTab;.  Toke
22df9 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 3b 0a 0a  n *pTableName;..
22dfa 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61    /* Read the da
22dfb 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49  tabase schema. I
22dfc 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
22dfd 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f  s, leave an erro
22dfe 72 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61  r message.  ** a
22dff 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73  nd code in pPars
22e00 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c  e and return NUL
22e01 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  L. */.  assert( 
22e02 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
22e03 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72  sAllMutexes(pPar
22e04 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 66 28  se->db) );.  if(
22e05 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
22e06 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
22e07 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74  arse) ){.    ret
22e08 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  urn;.  }..  asse
22e09 72 74 28 20 70 4e 61 6d 65 32 21 3d 30 20 7c 7c  rt( pName2!=0 ||
22e0a 20 70 4e 61 6d 65 31 3d 3d 30 20 29 3b 0a 20 20   pName1==0 );.  
22e0b 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b  if( pName1==0 ){
22e0c 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31 3a 20  .    /* Form 1: 
22e0d 20 41 6e 61 6c 79 7a 65 20 65 76 65 72 79 74 68   Analyze everyth
22e0e 69 6e 67 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  ing */.    for(i
22e0f 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
22e10 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  ++){.      if( i
22e11 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20  ==1 ) continue; 
22e12 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e 61 6c 79   /* Do not analy
22e13 7a 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61  ze the TEMP data
22e14 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 61 6e  base */.      an
22e15 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50  alyzeDatabase(pP
22e16 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 7d 0a  arse, i);.    }.
22e17 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61 6d    }else if( pNam
22e18 65 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  e2->n==0 ){.    
22e19 2f 2a 20 46 6f 72 6d 20 32 3a 20 20 41 6e 61 6c  /* Form 2:  Anal
22e1a 79 7a 65 20 74 68 65 20 64 61 74 61 62 61 73 65  yze the database
22e1b 20 6f 72 20 74 61 62 6c 65 20 6e 61 6d 65 64 20   or table named 
22e1c 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c  */.    iDb = sql
22e1d 69 74 65 33 46 69 6e 64 44 62 28 64 62 2c 20 70  ite3FindDb(db, p
22e1e 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20  Name1);.    if( 
22e1f 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  iDb>=0 ){.      
22e20 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28  analyzeDatabase(
22e21 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20  pParse, iDb);.  
22e22 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
22e23 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
22e24 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d  omToken(db, pNam
22e25 65 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  e1);.      if( z
22e26 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62   ){.        pTab
22e27 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
22e28 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
22e29 20 7a 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   z, 0);.        
22e2a 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
22e2b 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , z);.        if
22e2c 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20  ( pTab ){.      
22e2d 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65      analyzeTable
22e2e 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
22e2f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
22e30 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  }.    }.  }else{
22e31 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 33 3a 20  .    /* Form 3: 
22e32 41 6e 61 6c 79 7a 65 20 74 68 65 20 66 75 6c 6c  Analyze the full
22e33 79 20 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c  y qualified tabl
22e34 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 69 44  e name */.    iD
22e35 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61  b = sqlite3TwoPa
22e36 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
22e37 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26  Name1, pName2, &
22e38 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20  pTableName);.   
22e39 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20   if( iDb>=0 ){. 
22e3a 20 20 20 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61       zDb = db->a
22e3b 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
22e3c 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33       z = sqlite3
22e3d 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
22e3e 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20  , pTableName);. 
22e3f 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 20       if( z ){.  
22e40 20 20 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c        pTab = sql
22e41 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
22e42 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c 20 7a 44  pParse, 0, z, zD
22e43 62 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  b);.        sqli
22e44 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29  te3DbFree(db, z)
22e45 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54  ;.        if( pT
22e46 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ab ){.          
22e47 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50 61  analyzeTable(pPa
22e48 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  rse, pTab);.    
22e49 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
22e4a 20 20 7d 20 20 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a    }   .  }.}../*
22e4b 0a 2a 2a 20 55 73 65 64 20 74 6f 20 70 61 73 73  .** Used to pass
22e4c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
22e4d 6d 20 74 68 65 20 61 6e 61 6c 79 7a 65 72 20 72  m the analyzer r
22e4e 65 61 64 65 72 20 74 68 72 6f 75 67 68 20 74 6f  eader through to
22e4f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b   the.** callback
22e50 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 74 79 70   routine..*/.typ
22e51 65 64 65 66 20 73 74 72 75 63 74 20 61 6e 61 6c  edef struct anal
22e52 79 73 69 73 49 6e 66 6f 20 61 6e 61 6c 79 73 69  ysisInfo analysi
22e53 73 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20 61 6e  sInfo;.struct an
22e54 61 6c 79 73 69 73 49 6e 66 6f 20 7b 0a 20 20 73  alysisInfo {.  s
22e55 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f  qlite3 *db;.  co
22e56 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 62  nst char *zDatab
22e57 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  ase;.};../*.** T
22e58 68 69 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  his callback is 
22e59 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72  invoked once for
22e5a 20 65 61 63 68 20 69 6e 64 65 78 20 77 68 65 6e   each index when
22e5b 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a 2a 20   reading the.** 
22e5c 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
22e5d 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20  le.  .**.**     
22e5e 61 72 67 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f  argv[0] = name o
22e5f 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 20  f the index.**  
22e60 20 20 20 61 72 67 76 5b 31 5d 20 3d 20 72 65 73     argv[1] = res
22e61 75 6c 74 73 20 6f 66 20 61 6e 61 6c 79 73 69 73  ults of analysis
22e62 20 2d 20 6f 6e 20 69 6e 74 65 67 65 72 20 66 6f   - on integer fo
22e63 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f  r each column.*/
22e64 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61 6c  .static int anal
22e65 79 73 69 73 4c 6f 61 64 65 72 28 76 6f 69 64 20  ysisLoader(void 
22e66 2a 70 44 61 74 61 2c 20 69 6e 74 20 61 72 67 63  *pData, int argc
22e67 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 63  , char **argv, c
22e68 68 61 72 20 2a 2a 4e 6f 74 55 73 65 64 29 7b 0a  har **NotUsed){.
22e69 20 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 2a    analysisInfo *
22e6a 70 49 6e 66 6f 20 3d 20 28 61 6e 61 6c 79 73 69  pInfo = (analysi
22e6b 73 49 6e 66 6f 2a 29 70 44 61 74 61 3b 0a 20 20  sInfo*)pData;.  
22e6c 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20  Index *pIndex;. 
22e6d 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 75 6e 73   int i, c;.  uns
22e6e 69 67 6e 65 64 20 69 6e 74 20 76 3b 0a 20 20 63  igned int v;.  c
22e6f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 0a 20  onst char *z;.. 
22e70 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 32   assert( argc==2
22e71 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   );.  UNUSED_PAR
22e72 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
22e73 20 61 72 67 63 29 3b 0a 0a 20 20 69 66 28 20 61   argc);..  if( a
22e74 72 67 76 3d 3d 30 20 7c 7c 20 61 72 67 76 5b 30  rgv==0 || argv[0
22e75 5d 3d 3d 30 20 7c 7c 20 61 72 67 76 5b 31 5d 3d  ]==0 || argv[1]=
22e76 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
22e77 20 30 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78   0;.  }.  pIndex
22e78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e   = sqlite3FindIn
22e79 64 65 78 28 70 49 6e 66 6f 2d 3e 64 62 2c 20 61  dex(pInfo->db, a
22e7a 72 67 76 5b 30 5d 2c 20 70 49 6e 66 6f 2d 3e 7a  rgv[0], pInfo->z
22e7b 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28  Database);.  if(
22e7c 20 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20   pIndex==0 ){.  
22e7d 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
22e7e 20 20 7a 20 3d 20 61 72 67 76 5b 31 5d 3b 0a 20    z = argv[1];. 
22e7f 20 66 6f 72 28 69 3d 30 3b 20 2a 7a 20 26 26 20   for(i=0; *z && 
22e80 69 3c 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75  i<=pIndex->nColu
22e81 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20  mn; i++){.    v 
22e82 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  = 0;.    while( 
22e83 28 63 3d 7a 5b 30 5d 29 3e 3d 27 30 27 20 26 26  (c=z[0])>='0' &&
22e84 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20   c<='9' ){.     
22e85 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20   v = v*10 + c - 
22e86 27 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a  '0';.      z++;.
22e87 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78      }.    pIndex
22e88 2d 3e 61 69 52 6f 77 45 73 74 5b 69 5d 20 3d 20  ->aiRowEst[i] = 
22e89 76 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27  v;.    if( *z=='
22e8a 20 27 20 29 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20   ' ) z++;.  }.  
22e8b 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
22e8c 2a 2a 20 49 66 20 74 68 65 20 49 6e 64 65 78 2e  ** If the Index.
22e8d 61 53 61 6d 70 6c 65 20 76 61 72 69 61 62 6c 65  aSample variable
22e8e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 64 65   is not NULL, de
22e8f 6c 65 74 65 20 74 68 65 20 61 53 61 6d 70 6c 65  lete the aSample
22e90 5b 5d 20 61 72 72 61 79 0a 2a 2a 20 61 6e 64 20  [] array.** and 
22e91 69 74 73 20 63 6f 6e 74 65 6e 74 73 2e 0a 2a 2f  its contents..*/
22e92 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22e93 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
22e94 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 49  teIndexSamples(I
22e95 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 23 69 66  ndex *pIdx){.#if
22e96 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
22e97 45 5f 53 54 41 54 32 0a 20 20 69 66 28 20 70 49  E_STAT2.  if( pI
22e98 64 78 2d 3e 61 53 61 6d 70 6c 65 20 29 7b 0a 20  dx->aSample ){. 
22e99 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 73 71     int j;.    sq
22e9a 6c 69 74 65 33 20 2a 64 62 4d 65 6d 20 3d 20 70  lite3 *dbMem = p
22e9b 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 4d  Idx->pTable->dbM
22e9c 65 6d 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b  em;.    for(j=0;
22e9d 20 6a 3c 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f   j<SQLITE_INDEX_
22e9e 53 41 4d 50 4c 45 53 3b 20 6a 2b 2b 29 7b 0a 20  SAMPLES; j++){. 
22e9f 20 20 20 20 20 49 6e 64 65 78 53 61 6d 70 6c 65       IndexSample
22ea0 20 2a 70 20 3d 20 26 70 49 64 78 2d 3e 61 53 61   *p = &pIdx->aSa
22ea1 6d 70 6c 65 5b 6a 5d 3b 0a 20 20 20 20 20 20 69  mple[j];.      i
22ea2 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d 53 51 4c  f( p->eType==SQL
22ea3 49 54 45 5f 54 45 58 54 20 7c 7c 20 70 2d 3e 65  ITE_TEXT || p->e
22ea4 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f  Type==SQLITE_BLO
22ea5 42 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  B ){.        sql
22ea6 69 74 65 33 44 62 46 72 65 65 28 70 49 64 78 2d  ite3DbFree(pIdx-
22ea7 3e 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 2c 20  >pTable->dbMem, 
22ea8 70 2d 3e 75 2e 7a 29 3b 0a 20 20 20 20 20 20 7d  p->u.z);.      }
22ea9 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
22eaa 65 33 44 62 46 72 65 65 28 64 62 4d 65 6d 2c 20  e3DbFree(dbMem, 
22eab 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 29 3b 0a  pIdx->aSample);.
22eac 20 20 20 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c      pIdx->aSampl
22ead 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6c 73 65  e = 0;.  }.#else
22eae 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
22eaf 54 45 52 28 70 49 64 78 29 3b 0a 23 65 6e 64 69  TER(pIdx);.#endi
22eb0 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20  f.}../*.** Load 
22eb1 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
22eb2 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
22eb3 61 6e 64 20 73 71 6c 69 74 65 5f 73 74 61 74 32  and sqlite_stat2
22eb4 20 74 61 62 6c 65 73 2e 20 54 68 65 0a 2a 2a 20   tables. The.** 
22eb5 63 6f 6e 74 65 6e 74 73 20 6f 66 20 73 71 6c 69  contents of sqli
22eb6 74 65 5f 73 74 61 74 31 20 61 72 65 20 75 73 65  te_stat1 are use
22eb7 64 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68  d to populate th
22eb8 65 20 49 6e 64 65 78 2e 61 69 52 6f 77 45 73 74  e Index.aiRowEst
22eb9 5b 5d 0a 2a 2a 20 61 72 72 61 79 73 2e 20 54 68  [].** arrays. Th
22eba 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 73 71  e contents of sq
22ebb 6c 69 74 65 5f 73 74 61 74 32 20 61 72 65 20 75  lite_stat2 are u
22ebc 73 65 64 20 74 6f 20 70 6f 70 75 6c 61 74 65 20  sed to populate 
22ebd 74 68 65 0a 2a 2a 20 49 6e 64 65 78 2e 61 53 61  the.** Index.aSa
22ebe 6d 70 6c 65 5b 5d 20 61 72 72 61 79 73 2e 0a 2a  mple[] arrays..*
22ebf 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 71 6c 69  *.** If the sqli
22ec0 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 69  te_stat1 table i
22ec1 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e  s not present in
22ec2 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 53   the database, S
22ec3 51 4c 49 54 45 5f 45 52 52 4f 52 0a 2a 2a 20 69  QLITE_ERROR.** i
22ec4 73 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74  s returned. In t
22ec5 68 69 73 20 63 61 73 65 2c 20 65 76 65 6e 20 69  his case, even i
22ec6 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
22ec7 53 54 41 54 32 20 77 61 73 20 64 65 66 69 6e 65  STAT2 was define
22ec8 64 20 0a 2a 2a 20 64 75 72 69 6e 67 20 63 6f 6d  d .** during com
22ec9 70 69 6c 61 74 69 6f 6e 20 61 6e 64 20 74 68 65  pilation and the
22eca 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61   sqlite_stat2 ta
22ecb 62 6c 65 20 69 73 20 70 72 65 73 65 6e 74 2c 20  ble is present, 
22ecc 6e 6f 20 64 61 74 61 20 69 73 20 0a 2a 2a 20 72  no data is .** r
22ecd 65 61 64 20 66 72 6f 6d 20 69 74 2e 0a 2a 2a 0a  ead from it..**.
22ece 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 4e 41  ** If SQLITE_ENA
22ecf 42 4c 45 5f 53 54 41 54 32 20 77 61 73 20 64 65  BLE_STAT2 was de
22ed0 66 69 6e 65 64 20 64 75 72 69 6e 67 20 63 6f 6d  fined during com
22ed1 70 69 6c 61 74 69 6f 6e 20 61 6e 64 20 74 68 65  pilation and the
22ed2 20 0a 2a 2a 20 73 71 6c 69 74 65 5f 73 74 61 74   .** sqlite_stat
22ed3 32 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 70  2 table is not p
22ed4 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61  resent in the da
22ed5 74 61 62 61 73 65 2c 20 53 51 4c 49 54 45 5f 45  tabase, SQLITE_E
22ed6 52 52 4f 52 20 69 73 0a 2a 2a 20 72 65 74 75 72  RROR is.** retur
22ed7 6e 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 69 6e  ned. However, in
22ed8 20 74 68 69 73 20 63 61 73 65 2c 20 64 61 74 61   this case, data
22ed9 20 69 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68   is read from th
22eda 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 0a 2a  e sqlite_stat1.*
22edb 2a 20 74 61 62 6c 65 20 28 69 66 20 69 74 20 69  * table (if it i
22edc 73 20 70 72 65 73 65 6e 74 29 20 62 65 66 6f 72  s present) befor
22edd 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a  e returning..**.
22ede 2a 2a 20 49 66 20 61 6e 20 4f 4f 4d 20 65 72 72  ** If an OOM err
22edf 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 69 73 20  or occurs, this 
22ee0 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20  function always 
22ee1 73 65 74 73 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  sets db->mallocF
22ee2 61 69 6c 65 64 2e 0a 2a 2a 20 54 68 69 73 20 6d  ailed..** This m
22ee3 65 61 6e 73 20 69 66 20 74 68 65 20 63 61 6c 6c  eans if the call
22ee4 65 72 20 64 6f 65 73 20 6e 6f 74 20 63 61 72 65  er does not care
22ee5 20 61 62 6f 75 74 20 6f 74 68 65 72 20 65 72 72   about other err
22ee6 6f 72 73 2c 20 74 68 65 20 72 65 74 75 72 6e 0a  ors, the return.
22ee7 2a 2a 20 63 6f 64 65 20 6d 61 79 20 62 65 20 69  ** code may be i
22ee8 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  gnored..*/.SQLIT
22ee9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
22eea 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61  lite3AnalysisLoa
22eeb 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  d(sqlite3 *db, i
22eec 6e 74 20 69 44 62 29 7b 0a 20 20 61 6e 61 6c 79  nt iDb){.  analy
22eed 73 69 73 49 6e 66 6f 20 73 49 6e 66 6f 3b 0a 20  sisInfo sInfo;. 
22eee 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20   HashElem *i;.  
22eef 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e  char *zSql;.  in
22ef0 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
22ef1 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64   iDb>=0 && iDb<d
22ef2 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65  b->nDb );.  asse
22ef3 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  rt( db->aDb[iDb]
22ef4 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73  .pBt!=0 );.  ass
22ef5 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
22ef6 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e  eHoldsMutex(db->
22ef7 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 20 29 3b  aDb[iDb].pBt) );
22ef8 0a 0a 20 20 2f 2a 20 43 6c 65 61 72 20 61 6e 79  ..  /* Clear any
22ef9 20 70 72 69 6f 72 20 73 74 61 74 69 73 74 69 63   prior statistic
22efa 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 73 71 6c  s */.  for(i=sql
22efb 69 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62  iteHashFirst(&db
22efc 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
22efd 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 69 3b 69  ma->idxHash);i;i
22efe 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
22eff 69 29 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  i)){.    Index *
22f00 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48 61 73  pIdx = sqliteHas
22f01 68 44 61 74 61 28 69 29 3b 0a 20 20 20 20 73 71  hData(i);.    sq
22f02 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45  lite3DefaultRowE
22f03 73 74 28 70 49 64 78 29 3b 0a 20 20 20 20 73 71  st(pIdx);.    sq
22f04 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78  lite3DeleteIndex
22f05 53 61 6d 70 6c 65 73 28 70 49 64 78 29 3b 0a 20  Samples(pIdx);. 
22f06 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74   }..  /* Check t
22f07 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
22f08 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
22f09 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 73  le exists */.  s
22f0a 49 6e 66 6f 2e 64 62 20 3d 20 64 62 3b 0a 20 20  Info.db = db;.  
22f0b 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 20  sInfo.zDatabase 
22f0c 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
22f0d 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c 69  Name;.  if( sqli
22f0e 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
22f0f 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c   "sqlite_stat1",
22f10 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65   sInfo.zDatabase
22f11 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
22f12 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
22f13 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20  .  }..  /* Load 
22f14 6e 65 77 20 73 74 61 74 69 73 74 69 63 73 20 6f  new statistics o
22f15 75 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ut of the sqlite
22f16 5f 73 74 61 74 31 20 74 61 62 6c 65 20 2a 2f 0a  _stat1 table */.
22f17 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
22f18 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20  MPrintf(db, .   
22f19 20 20 20 22 53 45 4c 45 43 54 20 69 64 78 2c 20     "SELECT idx, 
22f1a 73 74 61 74 20 46 52 4f 4d 20 25 51 2e 73 71 6c  stat FROM %Q.sql
22f1b 69 74 65 5f 73 74 61 74 31 22 2c 20 73 49 6e 66  ite_stat1", sInf
22f1c 6f 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  o.zDatabase);.  
22f1d 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20  if( zSql==0 ){. 
22f1e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
22f1f 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  OMEM;.  }else{. 
22f20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
22f21 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
22f22 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
22f23 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 61  exec(db, zSql, a
22f24 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 2c 20 26  nalysisLoader, &
22f25 73 49 6e 66 6f 2c 20 30 29 3b 0a 20 20 20 20 28  sInfo, 0);.    (
22f26 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
22f27 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73 71  tyOn(db);.    sq
22f28 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
22f29 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f  zSql);.  }...  /
22f2a 2a 20 4c 6f 61 64 20 74 68 65 20 73 74 61 74 69  * Load the stati
22f2b 73 74 69 63 73 20 66 72 6f 6d 20 74 68 65 20 73  stics from the s
22f2c 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c  qlite_stat2 tabl
22f2d 65 2e 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e. */.#ifdef SQL
22f2e 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
22f2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
22f30 45 5f 4f 4b 20 26 26 20 21 73 71 6c 69 74 65 33  E_OK && !sqlite3
22f31 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73  FindTable(db, "s
22f32 71 6c 69 74 65 5f 73 74 61 74 32 22 2c 20 73 49  qlite_stat2", sI
22f33 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 29 20 29  nfo.zDatabase) )
22f34 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
22f35 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 69  E_ERROR;.  }.  i
22f36 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
22f37 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
22f38 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b  stmt *pStmt = 0;
22f39 0a 0a 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c  ..    zSql = sql
22f3a 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
22f3b 0a 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54  .        "SELECT
22f3c 20 69 64 78 2c 73 61 6d 70 6c 65 6e 6f 2c 73 61   idx,sampleno,sa
22f3d 6d 70 6c 65 20 46 52 4f 4d 20 25 51 2e 73 71 6c  mple FROM %Q.sql
22f3e 69 74 65 5f 73 74 61 74 32 22 2c 20 73 49 6e 66  ite_stat2", sInf
22f3f 6f 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  o.zDatabase);.  
22f40 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20    if( !zSql ){. 
22f41 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
22f42 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73  _NOMEM;.    }els
22f43 65 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  e{.      (void)s
22f44 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
22f45 64 62 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  db);.      rc = 
22f46 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
22f47 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70  db, zSql, -1, &p
22f48 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  Stmt, 0);.      
22f49 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
22f4a 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 20  etyOn(db);.     
22f4b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
22f4c 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 20 20 7d 0a  b, zSql);.    }.
22f4d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
22f4e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
22f4f 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
22f50 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20  etyOff(db);.    
22f51 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
22f52 5f 73 74 65 70 28 70 53 74 6d 74 29 3d 3d 53 51  _step(pStmt)==SQ
22f53 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
22f54 20 20 20 20 63 68 61 72 20 2a 7a 49 6e 64 65 78      char *zIndex
22f55 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
22f56 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70  e3_column_text(p
22f57 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  Stmt, 0);.      
22f58 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20    Index *pIdx = 
22f59 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78  sqlite3FindIndex
22f5a 28 64 62 2c 20 7a 49 6e 64 65 78 2c 20 73 49 6e  (db, zIndex, sIn
22f5b 66 6f 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  fo.zDatabase);. 
22f5c 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 20         if( pIdx 
22f5d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
22f5e 20 69 53 61 6d 70 6c 65 20 3d 20 73 71 6c 69 74   iSample = sqlit
22f5f 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70 53  e3_column_int(pS
22f60 74 6d 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  tmt, 1);.       
22f61 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4d 65     sqlite3 *dbMe
22f62 6d 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  m = pIdx->pTable
22f63 2d 3e 64 62 4d 65 6d 3b 0a 20 20 20 20 20 20 20  ->dbMem;.       
22f64 20 20 20 61 73 73 65 72 74 28 20 64 62 4d 65 6d     assert( dbMem
22f65 3d 3d 64 62 20 7c 7c 20 64 62 4d 65 6d 3d 3d 30  ==db || dbMem==0
22f66 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66   );.          if
22f67 28 20 69 53 61 6d 70 6c 65 3c 53 51 4c 49 54 45  ( iSample<SQLITE
22f68 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 26  _INDEX_SAMPLES &
22f69 26 20 69 53 61 6d 70 6c 65 3e 3d 30 20 29 7b 0a  & iSample>=0 ){.
22f6a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
22f6b 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f  eType = sqlite3_
22f6c 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74 6d  column_type(pStm
22f6d 74 2c 20 32 29 3b 0a 0a 20 20 20 20 20 20 20 20  t, 2);..        
22f6e 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61 53      if( pIdx->aS
22f6f 61 6d 70 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20  ample==0 ){.    
22f70 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63            static
22f71 20 63 6f 6e 73 74 20 69 6e 74 20 73 7a 20 3d 20   const int sz = 
22f72 73 69 7a 65 6f 66 28 49 6e 64 65 78 53 61 6d 70  sizeof(IndexSamp
22f73 6c 65 29 2a 53 51 4c 49 54 45 5f 49 4e 44 45 58  le)*SQLITE_INDEX
22f74 5f 53 41 4d 50 4c 45 53 3b 0a 20 20 20 20 20 20  _SAMPLES;.      
22f75 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 61 53          pIdx->aS
22f76 61 6d 70 6c 65 20 3d 20 28 49 6e 64 65 78 53 61  ample = (IndexSa
22f77 6d 70 6c 65 20 2a 29 73 71 6c 69 74 65 33 44 62  mple *)sqlite3Db
22f78 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 4d 65 6d  MallocZero(dbMem
22f79 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 20 20 20  , sz);.         
22f7a 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61       if( pIdx->a
22f7b 53 61 6d 70 6c 65 3d 3d 30 20 29 7b 0a 20 20 20  Sample==0 ){.   
22f7c 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d               db-
22f7d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
22f7e 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1;.             
22f7f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
22f80 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
22f81 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
22f82 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
22f83 78 2d 3e 61 53 61 6d 70 6c 65 20 29 3b 0a 20 20  x->aSample );.  
22f84 20 20 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20            {.    
22f85 20 20 20 20 20 20 20 20 20 20 49 6e 64 65 78 53            IndexS
22f86 61 6d 70 6c 65 20 2a 70 53 61 6d 70 6c 65 20 3d  ample *pSample =
22f87 20 26 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 5b   &pIdx->aSample[
22f88 69 53 61 6d 70 6c 65 5d 3b 0a 20 20 20 20 20 20  iSample];.      
22f89 20 20 20 20 20 20 20 20 70 53 61 6d 70 6c 65 2d          pSample-
22f8a 3e 65 54 79 70 65 20 3d 20 28 75 38 29 65 54 79  >eType = (u8)eTy
22f8b 70 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  pe;.            
22f8c 20 20 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c    if( eType==SQL
22f8d 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c 20 65  ITE_INTEGER || e
22f8e 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f  Type==SQLITE_FLO
22f8f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  AT ){.          
22f90 20 20 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e 75        pSample->u
22f91 2e 72 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  .r = sqlite3_col
22f92 75 6d 6e 5f 64 6f 75 62 6c 65 28 70 53 74 6d 74  umn_double(pStmt
22f93 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 2);.          
22f94 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 54      }else if( eT
22f95 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54  ype==SQLITE_TEXT
22f96 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
22f97 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20  E_BLOB ){.      
22f98 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
22f99 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74  char *z = (const
22f9a 20 63 68 61 72 20 2a 29 28 0a 20 20 20 20 20 20   char *)(.      
22f9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65                (e
22f9c 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f  Type==SQLITE_BLO
22f9d 42 29 20 3f 0a 20 20 20 20 20 20 20 20 20 20 20  B) ?.           
22f9e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22f9f 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74  _column_blob(pSt
22fa0 6d 74 2c 20 32 29 3a 0a 20 20 20 20 20 20 20 20  mt, 2):.        
22fa1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
22fa2 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
22fa3 70 53 74 6d 74 2c 20 32 29 0a 20 20 20 20 20 20  pStmt, 2).      
22fa4 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20            );.   
22fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
22fa6 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c   n = sqlite3_col
22fa7 75 6d 6e 5f 62 79 74 65 73 28 70 53 74 6d 74 2c  umn_bytes(pStmt,
22fa8 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   2);.           
22fa9 20 20 20 20 20 69 66 28 20 6e 3e 32 34 20 29 7b       if( n>24 ){
22faa 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22fab 20 20 20 6e 20 3d 20 32 34 3b 0a 20 20 20 20 20     n = 24;.     
22fac 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
22fad 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 61               pSa
22fae 6d 70 6c 65 2d 3e 6e 42 79 74 65 20 3d 20 28 75  mple->nByte = (u
22faf 38 29 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20  8)n;.           
22fb0 20 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e 75 2e       pSample->u.
22fb1 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  z = sqlite3DbMal
22fb2 6c 6f 63 52 61 77 28 64 62 4d 65 6d 2c 20 6e 29  locRaw(dbMem, n)
22fb3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
22fb4 20 20 69 66 28 20 70 53 61 6d 70 6c 65 2d 3e 75    if( pSample->u
22fb5 2e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  .z ){.          
22fb6 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70          memcpy(p
22fb7 53 61 6d 70 6c 65 2d 3e 75 2e 7a 2c 20 7a 2c 20  Sample->u.z, z, 
22fb8 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  n);.            
22fb9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
22fba 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d               db-
22fbb 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
22fbc 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1;.             
22fbd 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
22fbe 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
22fbf 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
22fc0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
22fc1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
22fc2 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
22fc3 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e  rc = sqlite3_fin
22fc4 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20  alize(pStmt);.  
22fc5 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
22fc6 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
22fc7 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
22fc8 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
22fc9 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 64  E_NOMEM ){.    d
22fca 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22fcb 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 1;.  }.  retur
22fcc 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66  n rc;.}...#endif
22fcd 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
22fce 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a 2a 2a  ANALYZE */../***
22fcf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
22fd0 6f 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a  of analyze.c ***
22fd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
22fd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
22fd5 6e 20 66 69 6c 65 20 61 74 74 61 63 68 2e 63 20  n file attach.c 
22fd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
22fd9 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a  * 2003 April 6.*
22fda 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
22fdb 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
22fdc 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
22fdd 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
22fde 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
22fdf 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
22fe0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
22fe1 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
22fe2 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
22fe3 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
22fe4 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
22fe5 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
22fe6 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
22fe7 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
22fe8 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
22fe9 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
22fea 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
22feb 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
22fec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
22ff0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
22ff1 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  ins code used to
22ff2 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 41   implement the A
22ff3 54 54 41 43 48 20 61 6e 64 20 44 45 54 41 43 48  TTACH and DETACH
22ff4 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a   commands..**.**
22ff5 20 24 49 64 3a 20 61 74 74 61 63 68 2e 63 2c 76   $Id: attach.c,v
22ff6 20 31 2e 39 33 20 32 30 30 39 2f 30 35 2f 33 31   1.93 2009/05/31
22ff7 20 32 31 3a 32 31 3a 34 31 20 64 72 68 20 45 78   21:21:41 drh Ex
22ff8 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  p $.*/..#ifndef 
22ff9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41  SQLITE_OMIT_ATTA
22ffa 43 48 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65  CH./*.** Resolve
22ffb 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
22ffc 68 61 74 20 77 61 73 20 70 61 72 74 20 6f 66 20  hat was part of 
22ffd 61 6e 20 41 54 54 41 43 48 20 6f 72 20 44 45 54  an ATTACH or DET
22ffe 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  ACH statement. T
22fff 68 69 73 0a 2a 2a 20 69 73 20 73 6c 69 67 68 74  his.** is slight
23000 6c 79 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  ly different fro
23001 6d 20 72 65 73 6f 6c 76 69 6e 67 20 61 20 6e 6f  m resolving a no
23002 72 6d 61 6c 20 53 51 4c 20 65 78 70 72 65 73 73  rmal SQL express
23003 69 6f 6e 2c 20 62 65 63 61 75 73 65 20 73 69 6d  ion, because sim
23004 70 6c 65 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65  ple.** identifie
23005 72 73 20 61 72 65 20 74 72 65 61 74 65 64 20 61  rs are treated a
23006 73 20 73 74 72 69 6e 67 73 2c 20 6e 6f 74 20 70  s strings, not p
23007 6f 73 73 69 62 6c 65 20 63 6f 6c 75 6d 6e 20 6e  ossible column n
23008 61 6d 65 73 20 6f 72 20 61 6c 69 61 73 65 73 2e  ames or aliases.
23009 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 69 66 20 74  .**.** i.e. if t
2300a 68 65 20 70 61 72 73 65 72 20 73 65 65 73 3a 0a  he parser sees:.
2300b 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48  **.**     ATTACH
2300c 20 44 41 54 41 42 41 53 45 20 61 62 63 20 41 53   DATABASE abc AS
2300d 20 64 65 66 0a 2a 2a 0a 2a 2a 20 69 74 20 74 72   def.**.** it tr
2300e 65 61 74 73 20 74 68 65 20 74 77 6f 20 65 78 70  eats the two exp
2300f 72 65 73 73 69 6f 6e 73 20 61 73 20 6c 69 74 65  ressions as lite
23010 72 61 6c 20 73 74 72 69 6e 67 73 20 27 61 62 63  ral strings 'abc
23011 27 20 61 6e 64 20 27 64 65 66 27 20 69 6e 73 74  ' and 'def' inst
23012 65 61 64 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 69 6e  ead of.** lookin
23013 67 20 66 6f 72 20 63 6f 6c 75 6d 6e 73 20 6f 66  g for columns of
23014 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a   the same name..
23015 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20  **.** This only 
23016 61 70 70 6c 69 65 73 20 74 6f 20 74 68 65 20 72  applies to the r
23017 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 70 45 78 70  oot node of pExp
23018 72 2c 20 73 6f 20 74 68 65 20 73 74 61 74 65 6d  r, so the statem
23019 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41  ent:.**.**     A
2301a 54 54 41 43 48 20 44 41 54 41 42 41 53 45 20 61  TTACH DATABASE a
2301b 62 63 7c 7c 64 65 66 20 41 53 20 27 64 62 32 27  bc||def AS 'db2'
2301c 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c  .**.** will fail
2301d 20 62 65 63 61 75 73 65 20 6e 65 69 74 68 65 72   because neither
2301e 20 61 62 63 20 6f 72 20 64 65 66 20 63 61 6e 20   abc or def can 
2301f 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2f 0a  be resolved..*/.
23020 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c  static int resol
23021 76 65 41 74 74 61 63 68 45 78 70 72 28 4e 61 6d  veAttachExpr(Nam
23022 65 43 6f 6e 74 65 78 74 20 2a 70 4e 61 6d 65 2c  eContext *pName,
23023 20 45 78 70 72 20 2a 70 45 78 70 72 29 0a 7b 0a   Expr *pExpr).{.
23024 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
23025 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 45 78 70  E_OK;.  if( pExp
23026 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78  r ){.    if( pEx
23027 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 44 20 29 7b  pr->op!=TK_ID ){
23028 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
23029 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
2302a 6d 65 73 28 70 4e 61 6d 65 2c 20 70 45 78 70 72  mes(pName, pExpr
2302b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
2302c 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 73  =SQLITE_OK && !s
2302d 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
2302e 74 61 6e 74 28 70 45 78 70 72 29 20 29 7b 0a 20  tant(pExpr) ){. 
2302f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
23030 72 6f 72 4d 73 67 28 70 4e 61 6d 65 2d 3e 70 50  rorMsg(pName->pP
23031 61 72 73 65 2c 20 22 69 6e 76 61 6c 69 64 20 6e  arse, "invalid n
23032 61 6d 65 3a 20 5c 22 25 73 5c 22 22 2c 20 70 45  ame: \"%s\"", pE
23033 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a  xpr->u.zToken);.
23034 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
23035 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
23036 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
23037 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20        pExpr->op 
23038 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20  = TK_STRING;.   
23039 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2303a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  rc;.}../*.** An 
2303b 53 51 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f  SQL user-functio
2303c 6e 20 72 65 67 69 73 74 65 72 65 64 20 74 6f 20  n registered to 
2303d 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61  do the work of a
2303e 6e 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65  n ATTACH stateme
2303f 6e 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65  nt. The.** three
23040 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68   arguments to th
23041 65 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20  e function come 
23042 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 6e  directly from an
23043 20 61 74 74 61 63 68 20 73 74 61 74 65 6d 65 6e   attach statemen
23044 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54  t:.**.**     ATT
23045 41 43 48 20 44 41 54 41 42 41 53 45 20 78 20 41  ACH DATABASE x A
23046 53 20 79 20 4b 45 59 20 7a 0a 2a 2a 0a 2a 2a 20  S y KEY z.**.** 
23047 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74      SELECT sqlit
23048 65 5f 61 74 74 61 63 68 28 78 2c 20 79 2c 20 7a  e_attach(x, y, z
23049 29 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f  ).**.** If the o
2304a 70 74 69 6f 6e 61 6c 20 22 4b 45 59 20 7a 22 20  ptional "KEY z" 
2304b 73 79 6e 74 61 78 20 69 73 20 6f 6d 69 74 74 65  syntax is omitte
2304c 64 2c 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 69  d, an SQL NULL i
2304d 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 0a  s passed as the.
2304e 2a 2a 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  ** third argumen
2304f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
23050 64 20 61 74 74 61 63 68 46 75 6e 63 28 0a 20 20  d attachFunc(.  
23051 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
23052 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
23053 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
23054 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
23055 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
23056 74 20 72 63 20 3d 20 30 3b 0a 20 20 73 71 6c 69  t rc = 0;.  sqli
23057 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
23058 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
23059 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
2305a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
2305b 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e;.  const char 
2305c 2a 7a 46 69 6c 65 3b 0a 20 20 44 62 20 2a 61 4e  *zFile;.  Db *aN
2305d 65 77 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  ew;.  char *zErr
2305e 44 79 6e 20 3d 20 30 3b 0a 0a 20 20 55 4e 55 53  Dyn = 0;..  UNUS
2305f 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
23060 55 73 65 64 29 3b 0a 0a 20 20 7a 46 69 6c 65 20  Used);..  zFile 
23061 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
23062 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
23063 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a  xt(argv[0]);.  z
23064 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  Name = (const ch
23065 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
23066 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
23067 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 3d 3d 30  ;.  if( zFile==0
23068 20 29 20 7a 46 69 6c 65 20 3d 20 22 22 3b 0a 20   ) zFile = "";. 
23069 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20   if( zName==0 ) 
2306a 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a 0a 20 20 2f  zName = "";..  /
2306b 2a 20 43 68 65 63 6b 20 66 6f 72 20 74 68 65 20  * Check for the 
2306c 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 73  following errors
2306d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
2306e 2a 20 54 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63  * Too many attac
2306f 68 65 64 20 64 61 74 61 62 61 73 65 73 2c 0a 20  hed databases,. 
23070 20 2a 2a 20 20 20 20 20 2a 20 54 72 61 6e 73 61   **     * Transa
23071 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20  ction currently 
23072 6f 70 65 6e 0a 20 20 2a 2a 20 20 20 20 20 2a 20  open.  **     * 
23073 53 70 65 63 69 66 69 65 64 20 64 61 74 61 62 61  Specified databa
23074 73 65 20 6e 61 6d 65 20 61 6c 72 65 61 64 79 20  se name already 
23075 62 65 69 6e 67 20 75 73 65 64 2e 0a 20 20 2a 2f  being used..  */
23076 0a 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 3d  .  if( db->nDb>=
23077 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
23078 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44  E_LIMIT_ATTACHED
23079 5d 2b 32 20 29 7b 0a 20 20 20 20 7a 45 72 72 44  ]+2 ){.    zErrD
2307a 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  yn = sqlite3MPri
2307b 6e 74 66 28 64 62 2c 20 22 74 6f 6f 20 6d 61 6e  ntf(db, "too man
2307c 79 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  y attached datab
2307d 61 73 65 73 20 2d 20 6d 61 78 20 25 64 22 2c 20  ases - max %d", 
2307e 0a 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69  .      db->aLimi
2307f 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41  t[SQLITE_LIMIT_A
23080 54 54 41 43 48 45 44 5d 0a 20 20 20 20 29 3b 0a  TTACHED].    );.
23081 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f      goto attach_
23082 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28  error;.  }.  if(
23083 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
23084 20 29 7b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20   ){.    zErrDyn 
23085 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
23086 28 64 62 2c 20 22 63 61 6e 6e 6f 74 20 41 54 54  (db, "cannot ATT
23087 41 43 48 20 64 61 74 61 62 61 73 65 20 77 69 74  ACH database wit
23088 68 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 22  hin transaction"
23089 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61  );.    goto atta
2308a 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20  ch_error;.  }.  
2308b 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
2308c 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68  Db; i++){.    ch
2308d 61 72 20 2a 7a 20 3d 20 64 62 2d 3e 61 44 62 5b  ar *z = db->aDb[
2308e 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 61 73  i].zName;.    as
2308f 73 65 72 74 28 20 7a 20 26 26 20 7a 4e 61 6d 65  sert( z && zName
23090 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   );.    if( sqli
23091 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 4e  te3StrICmp(z, zN
23092 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ame)==0 ){.     
23093 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74   zErrDyn = sqlit
23094 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 64  e3MPrintf(db, "d
23095 61 74 61 62 61 73 65 20 25 73 20 69 73 20 61 6c  atabase %s is al
23096 72 65 61 64 79 20 69 6e 20 75 73 65 22 2c 20 7a  ready in use", z
23097 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74  Name);.      got
23098 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a  o attach_error;.
23099 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2309a 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 6e 65 77  Allocate the new
2309b 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 62   entry in the db
2309c 2d 3e 61 44 62 5b 5d 20 61 72 72 61 79 20 61 6e  ->aDb[] array an
2309d 64 20 69 6e 69 74 69 61 6c 69 73 65 20 74 68 65  d initialise the
2309e 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20 68 61 73   schema.  ** has
2309f 68 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20  h tables..  */. 
230a0 20 69 66 28 20 64 62 2d 3e 61 44 62 3d 3d 64 62   if( db->aDb==db
230a1 2d 3e 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20  ->aDbStatic ){. 
230a2 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65     aNew = sqlite
230a3 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
230a4 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b   sizeof(db->aDb[
230a5 30 5d 29 2a 33 20 29 3b 0a 20 20 20 20 69 66 28  0])*3 );.    if(
230a6 20 61 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72   aNew==0 ) retur
230a7 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 4e  n;.    memcpy(aN
230a8 65 77 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a  ew, db->aDb, siz
230a9 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a  eof(db->aDb[0])*
230aa 32 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  2);.  }else{.   
230ab 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   aNew = sqlite3D
230ac 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 64 62 2d  bRealloc(db, db-
230ad 3e 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d  >aDb, sizeof(db-
230ae 3e 61 44 62 5b 30 5d 29 2a 28 64 62 2d 3e 6e 44  >aDb[0])*(db->nD
230af 62 2b 31 29 20 29 3b 0a 20 20 20 20 69 66 28 20  b+1) );.    if( 
230b0 61 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  aNew==0 ) return
230b1 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 20  ;.  }.  db->aDb 
230b2 3d 20 61 4e 65 77 3b 0a 20 20 61 4e 65 77 20 3d  = aNew;.  aNew =
230b3 20 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44   &db->aDb[db->nD
230b4 62 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 4e 65  b];.  memset(aNe
230b5 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 61 4e  w, 0, sizeof(*aN
230b6 65 77 29 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e  ew));..  /* Open
230b7 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
230b8 6c 65 2e 20 49 66 20 74 68 65 20 62 74 72 65 65  le. If the btree
230b9 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79   is successfully
230ba 20 6f 70 65 6e 65 64 2c 20 75 73 65 0a 20 20 2a   opened, use.  *
230bb 2a 20 69 74 20 74 6f 20 6f 62 74 61 69 6e 20 74  * it to obtain t
230bc 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
230bd 6d 61 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e  ma. At this poin
230be 74 20 74 68 65 20 73 63 68 65 6d 61 20 6d 61 79  t the schema may
230bf 0a 20 20 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74  .  ** or may not
230c0 20 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64 2e   be initialised.
230c1 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
230c2 69 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79  ite3BtreeFactory
230c3 28 64 62 2c 20 7a 46 69 6c 65 2c 20 30 2c 20 53  (db, zFile, 0, S
230c4 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41  QLITE_DEFAULT_CA
230c5 43 48 45 5f 53 49 5a 45 2c 0a 20 20 20 20 20 20  CHE_SIZE,.      
230c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
230c7 20 20 20 20 20 64 62 2d 3e 6f 70 65 6e 46 6c 61       db->openFla
230c8 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs | SQLITE_OPEN
230c9 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20 20  _MAIN_DB,.      
230ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
230cb 20 20 20 20 20 26 61 4e 65 77 2d 3e 70 42 74 29       &aNew->pBt)
230cc 3b 0a 20 20 64 62 2d 3e 6e 44 62 2b 2b 3b 0a 20  ;.  db->nDb++;. 
230cd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
230ce 43 4f 4e 53 54 52 41 49 4e 54 20 29 7b 0a 20 20  CONSTRAINT ){.  
230cf 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
230d0 52 4f 52 3b 0a 20 20 20 20 7a 45 72 72 44 79 6e  ROR;.    zErrDyn
230d1 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
230d2 66 28 64 62 2c 20 22 64 61 74 61 62 61 73 65 20  f(db, "database 
230d3 69 73 20 61 6c 72 65 61 64 79 20 61 74 74 61 63  is already attac
230d4 68 65 64 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69  hed");.  }else i
230d5 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
230d6 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70   ){.    Pager *p
230d7 50 61 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d  Pager;.    aNew-
230d8 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74  >pSchema = sqlit
230d9 65 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20  e3SchemaGet(db, 
230da 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20  aNew->pBt);.    
230db 69 66 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65  if( !aNew->pSche
230dc 6d 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ma ){.      rc =
230dd 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
230de 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65     }else if( aNe
230df 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  w->pSchema->file
230e0 5f 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d  _format && aNew-
230e1 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45  >pSchema->enc!=E
230e2 4e 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20  NC(db) ){.      
230e3 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
230e4 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20  3MPrintf(db, .  
230e5 20 20 20 20 20 20 22 61 74 74 61 63 68 65 64 20        "attached 
230e6 64 61 74 61 62 61 73 65 73 20 6d 75 73 74 20 75  databases must u
230e7 73 65 20 74 68 65 20 73 61 6d 65 20 74 65 78 74  se the same text
230e8 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d 61 69   encoding as mai
230e9 6e 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20  n database");.  
230ea 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
230eb 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20  ERROR;.    }.   
230ec 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65   pPager = sqlite
230ed 33 42 74 72 65 65 50 61 67 65 72 28 61 4e 65 77  3BtreePager(aNew
230ee 2d 3e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69  ->pBt);.    sqli
230ef 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d  te3PagerLockingM
230f0 6f 64 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e  ode(pPager, db->
230f1 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 29 3b 0a 20  dfltLockMode);. 
230f2 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a     sqlite3PagerJ
230f3 6f 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65  ournalMode(pPage
230f4 72 2c 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e  r, db->dfltJourn
230f5 61 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 61  alMode);.  }.  a
230f6 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c  New->zName = sql
230f7 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
230f8 20 7a 4e 61 6d 65 29 3b 0a 20 20 61 4e 65 77 2d   zName);.  aNew-
230f9 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20  >safety_level = 
230fa 33 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 48  3;..#if SQLITE_H
230fb 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 28 20 72  AS_CODEC.  if( r
230fc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
230fd 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73      extern int s
230fe 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63  qlite3CodecAttac
230ff 68 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c  h(sqlite3*, int,
23100 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
23101 74 29 3b 0a 20 20 20 20 65 78 74 65 72 6e 20 76  t);.    extern v
23102 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 63  oid sqlite3Codec
23103 47 65 74 4b 65 79 28 73 71 6c 69 74 65 33 2a 2c  GetKey(sqlite3*,
23104 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e   int, void**, in
23105 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4b 65  t*);.    int nKe
23106 79 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4b 65  y;.    char *zKe
23107 79 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d 20 73  y;.    int t = s
23108 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
23109 65 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20  e(argv[2]);.    
2310a 73 77 69 74 63 68 28 20 74 20 29 7b 0a 20 20 20  switch( t ){.   
2310b 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
2310c 4e 54 45 47 45 52 3a 0a 20 20 20 20 20 20 63 61  NTEGER:.      ca
2310d 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a  se SQLITE_FLOAT:
2310e 0a 20 20 20 20 20 20 20 20 7a 45 72 72 44 79 6e  .        zErrDyn
2310f 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
23110 75 70 28 64 62 2c 20 22 49 6e 76 61 6c 69 64 20  up(db, "Invalid 
23111 6b 65 79 20 76 61 6c 75 65 22 29 3b 0a 20 20 20  key value");.   
23112 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
23113 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20  _ERROR;.        
23114 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 0a  break;.        .
23115 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
23116 45 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 63 61  E_TEXT:.      ca
23117 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a  se SQLITE_BLOB:.
23118 20 20 20 20 20 20 20 20 6e 4b 65 79 20 3d 20 73          nKey = s
23119 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
2311a 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  es(argv[2]);.   
2311b 20 20 20 20 20 7a 4b 65 79 20 3d 20 28 63 68 61       zKey = (cha
2311c 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
2311d 65 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d 29 3b  e_blob(argv[2]);
2311e 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
2311f 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68  lite3CodecAttach
23120 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20  (db, db->nDb-1, 
23121 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20  zKey, nKey);.   
23122 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20       break;..   
23123 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e     case SQLITE_N
23124 55 4c 4c 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20  ULL:.        /* 
23125 4e 6f 20 6b 65 79 20 73 70 65 63 69 66 69 65 64  No key specified
23126 2e 20 20 55 73 65 20 74 68 65 20 6b 65 79 20 66  .  Use the key f
23127 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74  rom the main dat
23128 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 20  abase */.       
23129 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74   sqlite3CodecGet
2312a 4b 65 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64  Key(db, 0, (void
2312b 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29  **)&zKey, &nKey)
2312c 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
2312d 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63  qlite3CodecAttac
2312e 68 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c  h(db, db->nDb-1,
2312f 20 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20   zKey, nKey);.  
23130 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23131 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
23132 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20   /* If the file 
23133 77 61 73 20 6f 70 65 6e 65 64 20 73 75 63 63 65  was opened succe
23134 73 73 66 75 6c 6c 79 2c 20 72 65 61 64 20 74 68  ssfully, read th
23135 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65  e schema for the
23136 20 6e 65 77 20 64 61 74 61 62 61 73 65 2e 0a 20   new database.. 
23137 20 2a 2a 20 49 66 20 74 68 69 73 20 66 61 69 6c   ** If this fail
23138 73 2c 20 6f 72 20 69 66 20 6f 70 65 6e 69 6e 67  s, or if opening
23139 20 74 68 65 20 66 69 6c 65 20 66 61 69 6c 65 64   the file failed
2313a 2c 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65  , then close the
2313b 20 66 69 6c 65 20 61 6e 64 20 0a 20 20 2a 2a 20   file and .  ** 
2313c 72 65 6d 6f 76 65 20 74 68 65 20 65 6e 74 72 79  remove the entry
2313d 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 61 44   from the db->aD
2313e 62 5b 5d 20 61 72 72 61 79 2e 20 69 2e 65 2e 20  b[] array. i.e. 
2313f 70 75 74 20 65 76 65 72 79 74 68 69 6e 67 20 62  put everything b
23140 61 63 6b 20 74 68 65 20 77 61 79 0a 20 20 2a 2a  ack the way.  **
23141 20 77 65 20 66 6f 75 6e 64 20 69 74 2e 0a 20 20   we found it..  
23142 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
23143 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 28 76  ITE_OK ){.    (v
23144 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
23145 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73 71 6c  yOn(db);.    sql
23146 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
23147 6c 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20  l(db);.    rc = 
23148 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62 2c 20  sqlite3Init(db, 
23149 26 7a 45 72 72 44 79 6e 29 3b 0a 20 20 20 20 73  &zErrDyn);.    s
2314a 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
2314b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 28 76 6f  All(db);.    (vo
2314c 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
2314d 4f 66 66 28 64 62 29 3b 0a 20 20 7d 0a 20 20 69  Off(db);.  }.  i
2314e 66 28 20 72 63 20 29 7b 0a 20 20 20 20 69 6e 74  f( rc ){.    int
2314f 20 69 44 62 20 3d 20 64 62 2d 3e 6e 44 62 20 2d   iDb = db->nDb -
23150 20 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   1;.    assert( 
23151 69 44 62 3e 3d 32 20 29 3b 0a 20 20 20 20 69 66  iDb>=2 );.    if
23152 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  ( db->aDb[iDb].p
23153 42 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  Bt ){.      sqli
23154 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 64 62  te3BtreeClose(db
23155 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 3b  ->aDb[iDb].pBt);
23156 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69  .      db->aDb[i
23157 44 62 5d 2e 70 42 74 20 3d 20 30 3b 0a 20 20 20  Db].pBt = 0;.   
23158 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e     db->aDb[iDb].
23159 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 20  pSchema = 0;.   
2315a 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65   }.    sqlite3Re
2315b 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
2315c 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 64 62  a(db, 0);.    db
2315d 2d 3e 6e 44 62 20 3d 20 69 44 62 3b 0a 20 20 20  ->nDb = iDb;.   
2315e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2315f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c  NOMEM || rc==SQL
23160 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20  ITE_IOERR_NOMEM 
23161 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
23162 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
23163 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
23164 65 65 28 64 62 2c 20 7a 45 72 72 44 79 6e 29 3b  ee(db, zErrDyn);
23165 0a 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d  .      zErrDyn =
23166 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
23167 64 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f  db, "out of memo
23168 72 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ry");.    }else 
23169 69 66 28 20 7a 45 72 72 44 79 6e 3d 3d 30 20 29  if( zErrDyn==0 )
2316a 7b 0a 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20  {.      zErrDyn 
2316b 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2316c 28 64 62 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20  (db, "unable to 
2316d 6f 70 65 6e 20 64 61 74 61 62 61 73 65 3a 20 25  open database: %
2316e 73 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20  s", zFile);.    
2316f 7d 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63  }.    goto attac
23170 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 0a  h_error;.  }.  .
23171 20 20 72 65 74 75 72 6e 3b 0a 0a 61 74 74 61 63    return;..attac
23172 68 5f 65 72 72 6f 72 3a 0a 20 20 2f 2a 20 52 65  h_error:.  /* Re
23173 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 66  turn an error if
23174 20 77 65 20 67 65 74 20 68 65 72 65 20 2a 2f 0a   we get here */.
23175 20 20 69 66 28 20 7a 45 72 72 44 79 6e 20 29 7b    if( zErrDyn ){
23176 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
23177 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
23178 74 2c 20 7a 45 72 72 44 79 6e 2c 20 2d 31 29 3b  t, zErrDyn, -1);
23179 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2317a 65 65 28 64 62 2c 20 7a 45 72 72 44 79 6e 29 3b  ee(db, zErrDyn);
2317b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 20  .  }.  if( rc ) 
2317c 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2317d 72 72 6f 72 5f 63 6f 64 65 28 63 6f 6e 74 65 78  rror_code(contex
2317e 74 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  t, rc);.}../*.**
2317f 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 75 6e   An SQL user-fun
23180 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
23181 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20   to do the work 
23182 6f 66 20 61 6e 20 44 45 54 41 43 48 20 73 74 61  of an DETACH sta
23183 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 74  tement. The.** t
23184 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74  hree arguments t
23185 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63  o the function c
23186 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f  ome directly fro
23187 6d 20 61 20 64 65 74 61 63 68 20 73 74 61 74 65  m a detach state
23188 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ment:.**.**     
23189 44 45 54 41 43 48 20 44 41 54 41 42 41 53 45 20  DETACH DATABASE 
2318a 78 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  x.**.**     SELE
2318b 43 54 20 73 71 6c 69 74 65 5f 64 65 74 61 63 68  CT sqlite_detach
2318c 28 78 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  (x).*/.static vo
2318d 69 64 20 64 65 74 61 63 68 46 75 6e 63 28 0a 20  id detachFunc(. 
2318e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2318f 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
23190 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
23191 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
23192 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  .){.  const char
23193 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74   *zName = (const
23194 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
23195 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
23196 30 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  0]);.  sqlite3 *
23197 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
23198 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
23199 6f 6e 74 65 78 74 29 3b 0a 20 20 69 6e 74 20 69  ontext);.  int i
2319a 3b 0a 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b  ;.  Db *pDb = 0;
2319b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38  .  char zErr[128
2319c 5d 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  ];..  UNUSED_PAR
2319d 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
2319e 0a 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30  ..  if( zName==0
2319f 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a 20   ) zName = "";. 
231a0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
231a1 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  nDb; i++){.    p
231a2 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d  Db = &db->aDb[i]
231a3 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70  ;.    if( pDb->p
231a4 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  Bt==0 ) continue
231a5 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
231a6 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e  3StrICmp(pDb->zN
231a7 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29  ame, zName)==0 )
231a8 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69   break;.  }..  i
231a9 66 28 20 69 3e 3d 64 62 2d 3e 6e 44 62 20 29 7b  f( i>=db->nDb ){
231aa 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
231ab 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
231ac 72 29 2c 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63  r),zErr, "no suc
231ad 68 20 64 61 74 61 62 61 73 65 3a 20 25 73 22 2c  h database: %s",
231ae 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74   zName);.    got
231af 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a  o detach_error;.
231b0 20 20 7d 0a 20 20 69 66 28 20 69 3c 32 20 29 7b    }.  if( i<2 ){
231b1 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
231b2 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
231b3 72 29 2c 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74  r),zErr, "cannot
231b4 20 64 65 74 61 63 68 20 64 61 74 61 62 61 73 65   detach database
231b5 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   %s", zName);.  
231b6 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72    goto detach_er
231b7 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  ror;.  }.  if( !
231b8 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
231b9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
231ba 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
231bb 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20  rr), zErr,.     
231bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
231bd 22 63 61 6e 6e 6f 74 20 44 45 54 41 43 48 20 64  "cannot DETACH d
231be 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 74  atabase within t
231bf 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20  ransaction");.  
231c0 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72    goto detach_er
231c1 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  ror;.  }.  if( s
231c2 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52  qlite3BtreeIsInR
231c3 65 61 64 54 72 61 6e 73 28 70 44 62 2d 3e 70 42  eadTrans(pDb->pB
231c4 74 29 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72  t) || sqlite3Btr
231c5 65 65 49 73 49 6e 42 61 63 6b 75 70 28 70 44 62  eeIsInBackup(pDb
231c6 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 73 71  ->pBt) ){.    sq
231c7 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
231c8 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72  izeof(zErr),zErr
231c9 2c 20 22 64 61 74 61 62 61 73 65 20 25 73 20 69  , "database %s i
231ca 73 20 6c 6f 63 6b 65 64 22 2c 20 7a 4e 61 6d 65  s locked", zName
231cb 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61  );.    goto deta
231cc 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20  ch_error;.  }.. 
231cd 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
231ce 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20  se(pDb->pBt);.  
231cf 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20  pDb->pBt = 0;.  
231d0 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30  pDb->pSchema = 0
231d1 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65 74  ;.  sqlite3Reset
231d2 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
231d3 62 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 3b  b, 0);.  return;
231d4 0a 0a 64 65 74 61 63 68 5f 65 72 72 6f 72 3a 0a  ..detach_error:.
231d5 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
231d6 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
231d7 7a 45 72 72 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a  zErr, -1);.}../*
231d8 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 64 75  .** This procedu
231d9 72 65 20 67 65 6e 65 72 61 74 65 73 20 56 44 42  re generates VDB
231da 45 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e  E code for a sin
231db 67 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  gle invocation o
231dc 66 20 65 69 74 68 65 72 20 74 68 65 0a 2a 2a 20  f either the.** 
231dd 73 71 6c 69 74 65 5f 64 65 74 61 63 68 28 29 20  sqlite_detach() 
231de 6f 72 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68  or sqlite_attach
231df 28 29 20 53 51 4c 20 75 73 65 72 20 66 75 6e 63  () SQL user func
231e0 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tions..*/.static
231e1 20 76 6f 69 64 20 63 6f 64 65 41 74 74 61 63 68   void codeAttach
231e2 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
231e3 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e,       /* The 
231e4 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
231e5 2f 0a 20 20 69 6e 74 20 74 79 70 65 2c 20 20 20  /.  int type,   
231e6 20 20 20 20 20 20 20 20 20 2f 2a 20 45 69 74 68           /* Eith
231e7 65 72 20 53 51 4c 49 54 45 5f 41 54 54 41 43 48  er SQLITE_ATTACH
231e8 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 54 41 43   or SQLITE_DETAC
231e9 48 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a  H */.  FuncDef *
231ea 70 46 75 6e 63 2c 20 20 20 20 20 20 2f 2a 20 46  pFunc,      /* F
231eb 75 6e 63 44 65 66 20 77 72 61 70 70 65 72 20 66  uncDef wrapper f
231ec 6f 72 20 64 65 74 61 63 68 46 75 6e 63 28 29 20  or detachFunc() 
231ed 6f 72 20 61 74 74 61 63 68 46 75 6e 63 28 29 20  or attachFunc() 
231ee 2a 2f 0a 20 20 45 78 70 72 20 2a 70 41 75 74 68  */.  Expr *pAuth
231ef 41 72 67 2c 20 20 20 20 20 20 2f 2a 20 45 78 70  Arg,      /* Exp
231f0 72 65 73 73 69 6f 6e 20 74 6f 20 70 61 73 73 20  ression to pass 
231f1 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  to authorization
231f2 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 45   callback */.  E
231f3 78 70 72 20 2a 70 46 69 6c 65 6e 61 6d 65 2c 20  xpr *pFilename, 
231f4 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64      /* Name of d
231f5 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
231f6 20 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 2c    Expr *pDbname,
231f7 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
231f8 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
231f9 6f 20 75 73 65 20 69 6e 74 65 72 6e 61 6c 6c 79  o use internally
231fa 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4b 65 79   */.  Expr *pKey
231fb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
231fc 74 61 62 61 73 65 20 6b 65 79 20 66 6f 72 20 65  tabase key for e
231fd 6e 63 72 79 70 74 69 6f 6e 20 65 78 74 65 6e 73  ncryption extens
231fe 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ion */.){.  int 
231ff 72 63 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78  rc;.  NameContex
23200 74 20 73 4e 61 6d 65 3b 0a 20 20 56 64 62 65 20  t sName;.  Vdbe 
23201 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 2a 20 64  *v;.  sqlite3* d
23202 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
23203 20 20 69 6e 74 20 72 65 67 41 72 67 73 3b 0a 0a    int regArgs;..
23204 20 20 6d 65 6d 73 65 74 28 26 73 4e 61 6d 65 2c    memset(&sName,
23205 20 30 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65 43   0, sizeof(NameC
23206 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73 4e 61 6d  ontext));.  sNam
23207 65 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  e.pParse = pPars
23208 65 3b 0a 0a 20 20 69 66 28 20 0a 20 20 20 20 20  e;..  if( .     
23209 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
2320a 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45  = resolveAttachE
2320b 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 46 69 6c  xpr(&sName, pFil
2320c 65 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20  ename)) ||.     
2320d 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
2320e 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45  = resolveAttachE
2320f 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 44 62 6e  xpr(&sName, pDbn
23210 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53  ame)) ||.      S
23211 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
23212 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70  resolveAttachExp
23213 72 28 26 73 4e 61 6d 65 2c 20 70 4b 65 79 29 29  r(&sName, pKey))
23214 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  .  ){.    pParse
23215 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f  ->nErr++;.    go
23216 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20  to attach_end;. 
23217 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
23218 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
23219 41 54 49 4f 4e 0a 20 20 69 66 28 20 70 41 75 74  ATION.  if( pAut
2321a 68 41 72 67 20 29 7b 0a 20 20 20 20 63 68 61 72  hArg ){.    char
2321b 20 2a 7a 41 75 74 68 41 72 67 20 3d 20 70 41 75   *zAuthArg = pAu
2321c 74 68 41 72 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b  thArg->u.zToken;
2321d 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 7a  .    if( NEVER(z
2321e 41 75 74 68 41 72 67 3d 3d 30 29 20 29 7b 0a 20  AuthArg==0) ){. 
2321f 20 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68       goto attach
23220 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _end;.    }.    
23221 72 63 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68  rc = sqlite3Auth
23222 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 74 79  Check(pParse, ty
23223 70 65 2c 20 7a 41 75 74 68 41 72 67 2c 20 30 2c  pe, zAuthArg, 0,
23224 20 30 29 3b 0a 20 20 20 20 69 66 28 72 63 21 3d   0);.    if(rc!=
23225 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
23226 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65     goto attach_e
23227 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  nd;.    }.  }.#e
23228 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
23229 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
2322a 4e 20 2a 2f 0a 0a 0a 20 20 76 20 3d 20 73 71 6c  N */...  v = sql
2322b 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2322c 73 65 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d  se);.  regArgs =
2322d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2322e 61 6e 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b  ange(pParse, 4);
2322f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  .  sqlite3ExprCo
23230 64 65 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65  de(pParse, pFile
23231 6e 61 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a  name, regArgs);.
23232 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
23233 65 28 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d  e(pParse, pDbnam
23234 65 2c 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20  e, regArgs+1);. 
23235 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
23236 28 70 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72  (pParse, pKey, r
23237 65 67 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73  egArgs+2);..  as
23238 73 65 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d  sert( v || db->m
23239 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
2323a 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71   if( v ){.    sq
2323b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2323c 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20  v, OP_Function, 
2323d 30 2c 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75  0, regArgs+3-pFu
2323e 6e 63 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67  nc->nArg, regArg
2323f 73 2b 33 29 3b 0a 20 20 20 20 61 73 73 65 72 74  s+3);.    assert
23240 28 20 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d 2d  ( pFunc->nArg==-
23241 31 20 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41 72  1 || (pFunc->nAr
23242 67 26 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d 3e  g&0xff)==pFunc->
23243 6e 41 72 67 20 29 3b 0a 20 20 20 20 73 71 6c 69  nArg );.    sqli
23244 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
23245 76 2c 20 28 75 38 29 28 70 46 75 6e 63 2d 3e 6e  v, (u8)(pFunc->n
23246 41 72 67 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  Arg));.    sqlit
23247 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
23248 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 70 46  , -1, (char *)pF
23249 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29  unc, P4_FUNCDEF)
2324a 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61  ;..    /* Code a
2324b 6e 20 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f 72  n OP_Expire. For
2324c 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 74 65   an ATTACH state
2324d 6d 65 6e 74 2c 20 73 65 74 20 50 31 20 74 6f 20  ment, set P1 to 
2324e 74 72 75 65 20 28 65 78 70 69 72 65 20 74 68 69  true (expire thi
2324f 73 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65  s.    ** stateme
23250 6e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 45  nt only). For DE
23251 54 41 43 48 2c 20 73 65 74 20 69 74 20 74 6f 20  TACH, set it to 
23252 66 61 6c 73 65 20 28 65 78 70 69 72 65 20 61 6c  false (expire al
23253 6c 20 65 78 69 73 74 69 6e 67 0a 20 20 20 20 2a  l existing.    *
23254 2a 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 20  * statements).. 
23255 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
23256 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
23257 50 5f 45 78 70 69 72 65 2c 20 28 74 79 70 65 3d  P_Expire, (type=
23258 3d 53 51 4c 49 54 45 5f 41 54 54 41 43 48 29 29  =SQLITE_ATTACH))
23259 3b 0a 20 20 7d 0a 20 20 0a 61 74 74 61 63 68 5f  ;.  }.  .attach_
2325a 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 45 78  end:.  sqlite3Ex
2325b 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 46 69  prDelete(db, pFi
2325c 6c 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  lename);.  sqlit
2325d 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2325e 20 70 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71 6c   pDbname);.  sql
2325f 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
23260 62 2c 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a 0a  b, pKey);.}../*.
23261 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65  ** Called by the
23262 20 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69   parser to compi
23263 6c 65 20 61 20 44 45 54 41 43 48 20 73 74 61 74  le a DETACH stat
23264 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ement..**.**    
23265 20 44 45 54 41 43 48 20 70 44 62 6e 61 6d 65 0a   DETACH pDbname.
23266 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23267 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
23268 74 61 63 68 28 50 61 72 73 65 20 2a 70 50 61 72  tach(Parse *pPar
23269 73 65 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d  se, Expr *pDbnam
2326a 65 29 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e  e){.  static Fun
2326b 63 44 65 66 20 64 65 74 61 63 68 5f 66 75 6e 63  cDef detach_func
2326c 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20   = {.    1,     
2326d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41             /* nA
2326e 72 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45  rg */.    SQLITE
2326f 5f 55 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69  _UTF8,      /* i
23270 50 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30  PrefEnc */.    0
23271 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
23272 20 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20   /* flags */.   
23273 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
23274 20 20 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20     /* pUserData 
23275 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
23276 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78           /* pNex
23277 74 20 2a 2f 0a 20 20 20 20 64 65 74 61 63 68 46  t */.    detachF
23278 75 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46  unc,       /* xF
23279 75 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  unc */.    0,   
2327a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2327b 78 53 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20  xStep */.    0, 
2327c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2327d 2a 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20  * xFinalize */. 
2327e 20 20 20 22 73 71 6c 69 74 65 5f 64 65 74 61 63     "sqlite_detac
2327f 68 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f  h",  /* zName */
23280 0a 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20  .    0          
23281 20 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20         /* pHash 
23282 2a 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74  */.  };.  codeAt
23283 74 61 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c  tach(pParse, SQL
23284 49 54 45 5f 44 45 54 41 43 48 2c 20 26 64 65 74  ITE_DETACH, &det
23285 61 63 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61 6d  ach_func, pDbnam
23286 65 2c 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d 65  e, 0, 0, pDbname
23287 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  );.}../*.** Call
23288 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
23289 20 74 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 41   to compile an A
2328a 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e  TTACH statement.
2328b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43  .**.**     ATTAC
2328c 48 20 70 20 41 53 20 70 44 62 6e 61 6d 65 20 4b  H p AS pDbname K
2328d 45 59 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49 54  EY pKey.*/.SQLIT
2328e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2328f 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72  qlite3Attach(Par
23290 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
23291 20 2a 70 2c 20 45 78 70 72 20 2a 70 44 62 6e 61   *p, Expr *pDbna
23292 6d 65 2c 20 45 78 70 72 20 2a 70 4b 65 79 29 7b  me, Expr *pKey){
23293 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63 44 65  .  static FuncDe
23294 66 20 61 74 74 61 63 68 5f 66 75 6e 63 20 3d 20  f attach_func = 
23295 7b 0a 20 20 20 20 33 2c 20 20 20 20 20 20 20 20  {.    3,        
23296 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20          /* nArg 
23297 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 54  */.    SQLITE_UT
23298 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 65  F8,      /* iPre
23299 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20  fEnc */.    0,  
2329a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2329b 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c   flags */.    0,
2329c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2329d 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a 2f 0a  /* pUserData */.
2329e 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
2329f 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a        /* pNext *
232a0 2f 0a 20 20 20 20 61 74 74 61 63 68 46 75 6e 63  /.    attachFunc
232a1 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63  ,       /* xFunc
232a2 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
232a3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74            /* xSt
232a4 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  ep */.    0,    
232a5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
232a6 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20  Finalize */.    
232a7 22 73 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c  "sqlite_attach",
232a8 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20    /* zName */.  
232a9 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
232aa 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a      /* pHash */.
232ab 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 63    };.  codeAttac
232ac 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  h(pParse, SQLITE
232ad 5f 41 54 54 41 43 48 2c 20 26 61 74 74 61 63 68  _ATTACH, &attach
232ae 5f 66 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44 62  _func, p, p, pDb
232af 6e 61 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d 0a 23  name, pKey);.}.#
232b0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
232b1 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a  OMIT_ATTACH */..
232b2 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
232b3 20 61 20 44 62 46 69 78 65 72 20 73 74 72 75 63   a DbFixer struc
232b4 74 75 72 65 2e 20 20 54 68 69 73 20 72 6f 75 74  ture.  This rout
232b5 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  ine must be call
232b6 65 64 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 70  ed prior.** to p
232b7 61 73 73 69 6e 67 20 74 68 65 20 73 74 72 75 63  assing the struc
232b8 74 75 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 74  ture to one of t
232b9 68 65 20 73 71 6c 69 74 65 46 69 78 41 41 41 41  he sqliteFixAAAA
232ba 28 29 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f  () routines belo
232bb 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  w..**.** The ret
232bc 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61  urn value indica
232bd 74 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e  tes whether or n
232be 6f 74 20 66 69 78 61 74 69 6f 6e 20 69 73 20 72  ot fixation is r
232bf 65 71 75 69 72 65 64 2e 20 20 54 52 55 45 0a 2a  equired.  TRUE.*
232c0 2a 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 65  * means we do ne
232c1 65 64 20 74 6f 20 66 69 78 20 74 68 65 20 64 61  ed to fix the da
232c2 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65  tabase reference
232c3 73 2c 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 77  s, FALSE means w
232c4 65 20 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c  e do not..*/.SQL
232c5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
232c6 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 0a  sqlite3FixInit(.
232c7 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
232c8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 78        /* The fix
232c9 65 72 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c  er to be initial
232ca 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20  ized */.  Parse 
232cb 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
232cc 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   Error messages 
232cd 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20  will be written 
232ce 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44  here */.  int iD
232cf 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
232d0 20 54 68 69 73 20 69 73 20 74 68 65 20 64 61 74   This is the dat
232d1 61 62 61 73 65 20 74 68 61 74 20 6d 75 73 74 20  abase that must 
232d2 62 65 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e  be used */.  con
232d3 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20  st char *zType, 
232d4 20 2f 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69   /* "view", "tri
232d5 67 67 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78  gger", or "index
232d6 22 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b  " */.  const Tok
232d7 65 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61  en *pName  /* Na
232d8 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20  me of the view, 
232d9 74 72 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65  trigger, or inde
232da 78 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  x */.){.  sqlite
232db 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 4e 45  3 *db;..  if( NE
232dc 56 45 52 28 69 44 62 3c 30 29 20 7c 7c 20 69 44  VER(iDb<0) || iD
232dd 62 3d 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b  b==1 ) return 0;
232de 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
232df 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62  db;.  assert( db
232e0 2d 3e 6e 44 62 3e 69 44 62 20 29 3b 0a 20 20 70  ->nDb>iDb );.  p
232e1 46 69 78 2d 3e 70 50 61 72 73 65 20 3d 20 70 50  Fix->pParse = pP
232e2 61 72 73 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 44  arse;.  pFix->zD
232e3 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  b = db->aDb[iDb]
232e4 2e 7a 4e 61 6d 65 3b 0a 20 20 70 46 69 78 2d 3e  .zName;.  pFix->
232e5 7a 54 79 70 65 20 3d 20 7a 54 79 70 65 3b 0a 20  zType = zType;. 
232e6 20 70 46 69 78 2d 3e 70 4e 61 6d 65 20 3d 20 70   pFix->pName = p
232e7 4e 61 6d 65 3b 0a 20 20 72 65 74 75 72 6e 20 31  Name;.  return 1
232e8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
232e9 6f 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20  ollowing set of 
232ea 72 6f 75 74 69 6e 65 73 20 77 61 6c 6b 20 74 68  routines walk th
232eb 72 6f 75 67 68 20 74 68 65 20 70 61 72 73 65 20  rough the parse 
232ec 74 72 65 65 20 61 6e 64 20 61 73 73 69 67 6e 0a  tree and assign.
232ed 2a 2a 20 61 20 73 70 65 63 69 66 69 63 20 64 61  ** a specific da
232ee 74 61 62 61 73 65 20 74 6f 20 61 6c 6c 20 74 61  tabase to all ta
232ef 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20 77  ble references w
232f0 68 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73  here the databas
232f1 65 20 6e 61 6d 65 0a 2a 2a 20 77 61 73 20 6c 65  e name.** was le
232f2 66 74 20 75 6e 73 70 65 63 69 66 69 65 64 20 69  ft unspecified i
232f3 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53  n the original S
232f4 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54  QL statement.  T
232f5 68 65 20 70 46 69 78 20 73 74 72 75 63 74 75 72  he pFix structur
232f6 65 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62  e.** must have b
232f7 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  een initialized 
232f8 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20  by a prior call 
232f9 74 6f 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69  to sqlite3FixIni
232fa 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  t()..**.** These
232fb 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73   routines are us
232fc 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ed to make sure 
232fd 74 68 61 74 20 61 6e 20 69 6e 64 65 78 2c 20 74  that an index, t
232fe 72 69 67 67 65 72 2c 20 6f 72 0a 2a 2a 20 76 69  rigger, or.** vi
232ff 65 77 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61  ew in one databa
23300 73 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 66 65  se does not refe
23301 72 20 74 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20  r to objects in 
23302 61 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 61  a different data
23303 62 61 73 65 2e 0a 2a 2a 20 28 45 78 63 65 70 74  base..** (Except
23304 69 6f 6e 3a 20 69 6e 64 69 63 65 73 2c 20 74 72  ion: indices, tr
23305 69 67 67 65 72 73 2c 20 61 6e 64 20 76 69 65 77  iggers, and view
23306 73 20 69 6e 20 74 68 65 20 54 45 4d 50 20 64 61  s in the TEMP da
23307 74 61 62 61 73 65 20 61 72 65 0a 2a 2a 20 61 6c  tabase are.** al
23308 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74  lowed to refer t
23309 6f 20 61 6e 79 74 68 69 6e 67 2e 29 20 20 49 66  o anything.)  If
2330a 20 61 20 72 65 66 65 72 65 6e 63 65 20 69 73 20   a reference is 
2330b 65 78 70 6c 69 63 69 74 6c 79 20 6d 61 64 65 0a  explicitly made.
2330c 2a 2a 20 74 6f 20 61 6e 20 6f 62 6a 65 63 74 20  ** to an object 
2330d 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 64  in a different d
2330e 61 74 61 62 61 73 65 2c 20 61 6e 20 65 72 72 6f  atabase, an erro
2330f 72 20 6d 65 73 73 61 67 65 20 69 73 20 61 64 64  r message is add
23310 65 64 20 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2d  ed to.** pParse-
23311 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 74 68 65  >zErrMsg and the
23312 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75  se routines retu
23313 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66  rn non-zero.  If
23314 20 65 76 65 72 79 74 68 69 6e 67 0a 2a 2a 20 63   everything.** c
23315 68 65 63 6b 73 20 6f 75 74 2c 20 74 68 65 73 65  hecks out, these
23316 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
23317 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   0..*/.SQLITE_PR
23318 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
23319 33 46 69 78 53 72 63 4c 69 73 74 28 0a 20 20 44  3FixSrcList(.  D
2331a 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20  bFixer *pFix,   
2331b 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f      /* Context o
2331c 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a  f the fixation *
2331d 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69  /.  SrcList *pLi
2331e 73 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  st       /* The 
2331f 53 6f 75 72 63 65 20 6c 69 73 74 20 74 6f 20 63  Source list to c
23320 68 65 63 6b 20 61 6e 64 20 6d 6f 64 69 66 79 20  heck and modify 
23321 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  */.){.  int i;. 
23322 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
23323 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  ;.  struct SrcLi
23324 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
23325 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 4c 69  .  if( NEVER(pLi
23326 73 74 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20  st==0) ) return 
23327 30 3b 0a 20 20 7a 44 62 20 3d 20 70 46 69 78 2d  0;.  zDb = pFix-
23328 3e 7a 44 62 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  >zDb;.  for(i=0,
23329 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b   pItem=pList->a;
2332a 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20   i<pList->nSrc; 
2332b 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
2332c 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44     if( pItem->zD
2332d 61 74 61 62 61 73 65 3d 3d 30 20 29 7b 0a 20 20  atabase==0 ){.  
2332e 20 20 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61      pItem->zData
2332f 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62  base = sqlite3Db
23330 53 74 72 44 75 70 28 70 46 69 78 2d 3e 70 50 61  StrDup(pFix->pPa
23331 72 73 65 2d 3e 64 62 2c 20 7a 44 62 29 3b 0a 20  rse->db, zDb);. 
23332 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c     }else if( sql
23333 69 74 65 33 53 74 72 49 43 6d 70 28 70 49 74 65  ite3StrICmp(pIte
23334 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 7a 44 62  m->zDatabase,zDb
23335 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )!=0 ){.      sq
23336 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 46  lite3ErrorMsg(pF
23337 69 78 2d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20  ix->pParse,.    
23338 20 20 20 20 20 22 25 73 20 25 54 20 63 61 6e 6e       "%s %T cann
23339 6f 74 20 72 65 66 65 72 65 6e 63 65 20 6f 62 6a  ot reference obj
2333a 65 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65  ects in database
2333b 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 70   %s",.         p
2333c 46 69 78 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78  Fix->zType, pFix
2333d 2d 3e 70 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e  ->pName, pItem->
2333e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
2333f 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
23340 7d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  }.#if !defined(S
23341 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
23342 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
23343 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
23344 29 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ).    if( sqlite
23345 33 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c  3FixSelect(pFix,
23346 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29   pItem->pSelect)
23347 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
23348 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
23349 78 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d  xpr(pFix, pItem-
2334a 3e 70 4f 6e 29 20 29 20 72 65 74 75 72 6e 20 31  >pOn) ) return 1
2334b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72  ;.#endif.  }.  r
2334c 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 69 66 20 21  eturn 0;.}.#if !
2334d 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2334e 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
2334f 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
23350 54 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49 54  T_TRIGGER).SQLIT
23351 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
23352 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 0a  lite3FixSelect(.
23353 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
23354 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
23355 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f  t of the fixatio
23356 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  n */.  Select *p
23357 53 65 6c 65 63 74 20 20 20 20 20 20 2f 2a 20 54  Select      /* T
23358 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
23359 65 6e 74 20 74 6f 20 62 65 20 66 69 78 65 64 20  ent to be fixed 
2335a 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  to one database 
2335b 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  */.){.  while( p
2335c 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 69 66  Select ){.    if
2335d 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  ( sqlite3FixExpr
2335e 4c 69 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65  List(pFix, pSele
2335f 63 74 2d 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20  ct->pEList) ){. 
23360 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
23361 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
23362 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 70  ite3FixSrcList(p
23363 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53  Fix, pSelect->pS
23364 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  rc) ){.      ret
23365 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
23366 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
23367 78 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63  xpr(pFix, pSelec
23368 74 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20  t->pWhere) ){.  
23369 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2336a 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
2336b 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c  te3FixExpr(pFix,
2336c 20 70 53 65 6c 65 63 74 2d 3e 70 48 61 76 69 6e   pSelect->pHavin
2336d 67 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  g) ){.      retu
2336e 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
2336f 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
23370 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  t->pPrior;.  }. 
23371 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c   return 0;.}.SQL
23372 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
23373 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 0a  sqlite3FixExpr(.
23374 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
23375 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
23376 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20  of the fixation 
23377 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
23378 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
23379 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20  xpression to be 
2337a 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74  fixed to one dat
2337b 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68  abase */.){.  wh
2337c 69 6c 65 28 20 70 45 78 70 72 20 29 7b 0a 20 20  ile( pExpr ){.  
2337d 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79    if( ExprHasAny
2337e 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2337f 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 20  EP_TokenOnly) ) 
23380 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 45  break;.    if( E
23381 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
23382 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
23383 63 74 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ct) ){.      if(
23384 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63   sqlite3FixSelec
23385 74 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 78  t(pFix, pExpr->x
23386 2e 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 75  .pSelect) ) retu
23387 72 6e 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rn 1;.    }else{
23388 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
23389 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46  e3FixExprList(pF
2338a 69 78 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  ix, pExpr->x.pLi
2338b 73 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  st) ) return 1;.
2338c 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
2338d 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69  lite3FixExpr(pFi
2338e 78 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  x, pExpr->pRight
2338f 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
23390 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  n 1;.    }.    p
23391 45 78 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Expr = pExpr->pL
23392 65 66 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  eft;.  }.  retur
23393 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  n 0;.}.SQLITE_PR
23394 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
23395 33 46 69 78 45 78 70 72 4c 69 73 74 28 0a 20 20  3FixExprList(.  
23396 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20  DbFixer *pFix,  
23397 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66     /* Context of
23398 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f   the fixation */
23399 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
2339a 73 74 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70  st    /* The exp
2339b 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69  ression to be fi
2339c 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62  xed to one datab
2339d 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ase */.){.  int 
2339e 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  i;.  struct Expr
2339f 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
233a0 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
233a1 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66   ) return 0;.  f
233a2 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c  or(i=0, pItem=pL
233a3 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d  ist->a; i<pList-
233a4 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  >nExpr; i++, pIt
233a5 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73  em++){.    if( s
233a6 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46  qlite3FixExpr(pF
233a7 69 78 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  ix, pItem->pExpr
233a8 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
233a9 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
233aa 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
233ab 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
233ac 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
233ad 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
233ae 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54 72  int sqlite3FixTr
233af 69 67 67 65 72 53 74 65 70 28 0a 20 20 44 62 46  iggerStep(.  DbF
233b0 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20  ixer *pFix,     
233b1 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68  /* Context of th
233b2 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20  e fixation */.  
233b3 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74  TriggerStep *pSt
233b4 65 70 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65  ep /* The trigge
233b5 72 20 73 74 65 70 20 62 65 20 66 69 78 65 64 20  r step be fixed 
233b6 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  to one database 
233b7 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  */.){.  while( p
233b8 53 74 65 70 20 29 7b 0a 20 20 20 20 69 66 28 20  Step ){.    if( 
233b9 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74  sqlite3FixSelect
233ba 28 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 53  (pFix, pStep->pS
233bb 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20  elect) ){.      
233bc 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
233bd 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
233be 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 74  ixExpr(pFix, pSt
233bf 65 70 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20  ep->pWhere) ){. 
233c0 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
233c1 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
233c2 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28  ite3FixExprList(
233c3 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 45 78  pFix, pStep->pEx
233c4 70 72 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20  prList) ){.     
233c5 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
233c6 0a 20 20 20 20 70 53 74 65 70 20 3d 20 70 53 74  .    pStep = pSt
233c7 65 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20  ep->pNext;.  }. 
233c8 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
233c9 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
233ca 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 74 74 61  **** End of atta
233cb 63 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ch.c ***********
233cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233ce 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
233cf 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
233d0 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  auth.c *********
233d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233d3 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20  ***/./*.** 2003 
233d4 4a 61 6e 75 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a  January 11.**.**
233d5 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
233d6 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
233d7 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
233d8 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
233d9 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
233da 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
233db 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
233dc 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
233dd 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
233de 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
233df 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
233e0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
233e1 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
233e2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
233e3 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
233e4 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
233e5 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
233e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
233eb 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
233ec 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70  code used to imp
233ed 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74  lement the sqlit
233ee 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
233ef 72 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 69  r().** API.  Thi
233f0 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 61 6e  s facility is an
233f1 20 6f 70 74 69 6f 6e 61 6c 20 66 65 61 74 75 72   optional featur
233f2 65 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79  e of the library
233f3 2e 20 20 45 6d 62 65 64 64 65 64 0a 2a 2a 20 73  .  Embedded.** s
233f4 79 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e  ystems that do n
233f5 6f 74 20 6e 65 65 64 20 74 68 69 73 20 66 61 63  ot need this fac
233f6 69 6c 69 74 79 20 6d 61 79 20 6f 6d 69 74 20 69  ility may omit i
233f7 74 20 62 79 20 72 65 63 6f 6d 70 69 6c 69 6e 67  t by recompiling
233f8 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20  .** the library 
233f9 77 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 4f 4d  with -DSQLITE_OM
233fa 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
233fb 3d 31 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 75  =1.**.** $Id: au
233fc 74 68 2e 63 2c 76 20 31 2e 33 32 20 32 30 30 39  th.c,v 1.32 2009
233fd 2f 30 37 2f 30 32 20 31 38 3a 34 30 3a 33 35 20  /07/02 18:40:35 
233fe 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
233ff 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20  $.*/../*.** All 
23400 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74  of the code in t
23401 68 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65 20  his file may be 
23402 6f 6d 69 74 74 65 64 20 62 79 20 64 65 66 69 6e  omitted by defin
23403 69 6e 67 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  ing a single.** 
23404 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65  macro..*/.#ifnde
23405 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
23406 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a  THORIZATION../*.
23407 2a 2a 20 53 65 74 20 6f 72 20 63 6c 65 61 72 20  ** Set or clear 
23408 74 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f  the access autho
23409 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
2340a 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 63  n..**.** The acc
2340b 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  ess authorizatio
2340c 6e 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  n function is be
2340d 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74   called during t
2340e 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a  he compilation.*
2340f 2a 20 70 68 61 73 65 20 74 6f 20 76 65 72 69 66  * phase to verif
23410 79 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20  y that the user 
23411 68 61 73 20 72 65 61 64 20 61 6e 64 2f 6f 72 20  has read and/or 
23412 77 72 69 74 65 20 61 63 63 65 73 73 20 70 65 72  write access per
23413 6d 69 73 73 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61  mission on.** va
23414 72 69 6f 75 73 20 66 69 65 6c 64 73 20 6f 66 20  rious fields of 
23415 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
23416 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
23417 74 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75  t to the auth fu
23418 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 20 63  nction.** is a c
23419 6f 70 79 20 6f 66 20 74 68 65 20 33 72 64 20 61  opy of the 3rd a
2341a 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
2341b 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 65  routine.  The se
2341c 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  cond argument.**
2341d 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e   to the auth fun
2341e 63 74 69 6f 6e 20 69 73 20 6f 6e 65 20 6f 66 20  ction is one of 
2341f 74 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 3a  these constants:
23420 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  .**.**       SQL
23421 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58  ITE_CREATE_INDEX
23422 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
23423 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 0a 2a 2a  _CREATE_TABLE.**
23424 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
23425 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 0a  EATE_TEMP_INDEX.
23426 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
23427 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c  CREATE_TEMP_TABL
23428 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  E.**       SQLIT
23429 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52  E_CREATE_TEMP_TR
2342a 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53  IGGER.**       S
2342b 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
2342c 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20  P_VIEW.**       
2342d 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52  SQLITE_CREATE_TR
2342e 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53  IGGER.**       S
2342f 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45  QLITE_CREATE_VIE
23430 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  W.**       SQLIT
23431 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20  E_DELETE.**     
23432 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e    SQLITE_DROP_IN
23433 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  DEX.**       SQL
23434 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 0a 2a  ITE_DROP_TABLE.*
23435 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
23436 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a  ROP_TEMP_INDEX.*
23437 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
23438 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a  ROP_TEMP_TABLE.*
23439 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
2343a 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52  ROP_TEMP_TRIGGER
2343b 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
2343c 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 0a  _DROP_TEMP_VIEW.
2343d 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
2343e 44 52 4f 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20  DROP_TRIGGER.** 
2343f 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
23440 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20  P_VIEW.**       
23441 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 0a 2a 2a  SQLITE_INSERT.**
23442 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 50 52         SQLITE_PR
23443 41 47 4d 41 0a 2a 2a 20 20 20 20 20 20 20 53 51  AGMA.**       SQ
23444 4c 49 54 45 5f 52 45 41 44 0a 2a 2a 20 20 20 20  LITE_READ.**    
23445 20 20 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54     SQLITE_SELECT
23446 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
23447 5f 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20  _TRANSACTION.** 
23448 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 50 44        SQLITE_UPD
23449 41 54 45 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68  ATE.**.** The th
2344a 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61  ird and fourth a
2344b 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
2344c 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 61 72  auth function ar
2344d 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a  e the name of.**
2344e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74   the table and t
2344f 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61  he column that a
23450 72 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65  re being accesse
23451 64 2e 20 20 54 68 65 20 61 75 74 68 20 66 75 6e  d.  The auth fun
23452 63 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20  ction.** should 
23453 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51  return either SQ
23454 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f  LITE_OK, SQLITE_
23455 44 45 4e 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f  DENY, or SQLITE_
23456 49 47 4e 4f 52 45 2e 20 20 49 66 0a 2a 2a 20 53  IGNORE.  If.** S
23457 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
23458 72 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74  rned, it means t
23459 68 61 74 20 61 63 63 65 73 73 20 69 73 20 61 6c  hat access is al
2345a 6c 6f 77 65 64 2e 20 20 53 51 4c 49 54 45 5f 44  lowed.  SQLITE_D
2345b 45 4e 59 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61  ENY.** means tha
2345c 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  t the SQL statem
2345d 65 6e 74 20 77 69 6c 6c 20 6e 65 76 65 72 2d 72  ent will never-r
2345e 75 6e 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33  un - the sqlite3
2345f 5f 65 78 65 63 28 29 20 63 61 6c 6c 0a 2a 2a 20  _exec() call.** 
23460 77 69 6c 6c 20 72 65 74 75 72 6e 20 77 69 74 68  will return with
23461 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49   an error.  SQLI
23462 54 45 5f 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20  TE_IGNORE means 
23463 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 61  that the SQL sta
23464 74 65 6d 65 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64  tement.** should
23465 20 72 75 6e 20 62 75 74 20 61 74 74 65 6d 70 74   run but attempt
23466 73 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 70  s to read the sp
23467 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 77  ecified column w
23468 69 6c 6c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a  ill return NULL.
23469 2a 2a 20 61 6e 64 20 61 74 74 65 6d 70 74 73 20  ** and attempts 
2346a 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 6f 6c  to write the col
2346b 75 6d 6e 20 77 69 6c 6c 20 62 65 20 69 67 6e 6f  umn will be igno
2346c 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69  red..**.** Setti
2346d 6e 67 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  ng the auth func
2346e 74 69 6f 6e 20 74 6f 20 4e 55 4c 4c 20 64 69 73  tion to NULL dis
2346f 61 62 6c 65 73 20 74 68 69 73 20 68 6f 6f 6b 2e  ables this hook.
23470 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a    The default.**
23471 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20   setting of the 
23472 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73  auth function is
23473 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45   NULL..*/.SQLITE
23474 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
23475 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
23476 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
23477 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
23478 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
23479 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
2347a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
2347b 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69  st char*),.  voi
2347c 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c  d *pArg.){.  sql
2347d 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
2347e 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
2347f 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68  b->xAuth = xAuth
23480 3b 0a 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67  ;.  db->pAuthArg
23481 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
23482 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
23483 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
23484 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
23485 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
23486 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
23487 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  E_OK;.}../*.** W
23488 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rite an error me
23489 73 73 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73  ssage into pPars
2348a 65 2d 3e 7a 45 72 72 4d 73 67 20 74 68 61 74 20  e->zErrMsg that 
2348b 65 78 70 6c 61 69 6e 73 20 74 68 61 74 20 74 68  explains that th
2348c 65 0a 2a 2a 20 75 73 65 72 2d 73 75 70 70 6c 69  e.** user-suppli
2348d 65 64 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ed authorization
2348e 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
2348f 65 64 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61  ed an illegal va
23490 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  lue..*/.static v
23491 6f 69 64 20 73 71 6c 69 74 65 41 75 74 68 42 61  oid sqliteAuthBa
23492 64 52 65 74 75 72 6e 43 6f 64 65 28 50 61 72 73  dReturnCode(Pars
23493 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71  e *pParse){.  sq
23494 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
23495 61 72 73 65 2c 20 22 61 75 74 68 6f 72 69 7a 65  arse, "authorize
23496 72 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 22 29 3b  r malfunction");
23497 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20  .  pParse->rc = 
23498 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a  SQLITE_ERROR;.}.
23499 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
2349a 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
2349b 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 70 65 72  callback for per
2349c 6d 69 73 73 69 6f 6e 20 74 6f 20 72 65 61 64 20  mission to read 
2349d 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 20 66 72 6f 6d  column zCol from
2349e 0a 2a 2a 20 74 61 62 6c 65 20 7a 54 61 62 20 69  .** table zTab i
2349f 6e 20 64 61 74 61 62 61 73 65 20 7a 44 62 2e 20  n database zDb. 
234a0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73  This function as
234a1 73 75 6d 65 73 20 74 68 61 74 20 61 6e 20 61 75  sumes that an au
234a2 74 68 6f 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63  thorization.** c
234a3 61 6c 6c 62 61 63 6b 20 68 61 73 20 62 65 65 6e  allback has been
234a4 20 72 65 67 69 73 74 65 72 65 64 20 28 69 2e 65   registered (i.e
234a5 2e 20 74 68 61 74 20 73 71 6c 69 74 65 33 2e 78  . that sqlite3.x
234a6 41 75 74 68 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  Auth is not NULL
234a7 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49  )..**.** If SQLI
234a8 54 45 5f 49 47 4e 4f 52 45 20 69 73 20 72 65 74  TE_IGNORE is ret
234a9 75 72 6e 65 64 20 61 6e 64 20 70 45 78 70 72 20  urned and pExpr 
234aa 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
234ab 6e 20 70 45 78 70 72 20 69 73 20 63 68 61 6e 67  n pExpr is chang
234ac 65 64 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20  ed.** to an SQL 
234ad 4e 55 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e 2e  NULL expression.
234ae 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 70   Otherwise, if p
234af 45 78 70 72 20 69 73 20 4e 55 4c 4c 2c 20 74 68  Expr is NULL, th
234b0 65 6e 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  en SQLITE_IGNORE
234b1 0a 2a 2a 20 69 73 20 74 72 65 61 74 65 64 20 61  .** is treated a
234b2 73 20 53 51 4c 49 54 45 5f 44 45 4e 59 2e 20 49  s SQLITE_DENY. I
234b3 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65  n this case an e
234b4 72 72 6f 72 20 69 73 20 6c 65 66 74 20 69 6e 20  rror is left in 
234b5 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  pParse..*/.SQLIT
234b6 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
234b7 6c 69 74 65 33 41 75 74 68 52 65 61 64 43 6f 6c  lite3AuthReadCol
234b8 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
234b9 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
234ba 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
234bb 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63  r context */.  c
234bc 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c  onst char *zTab,
234bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
234be 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  * Table name */.
234bf 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
234c0 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ol,             
234c1 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65    /* Column name
234c2 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 20 20 20   */.  int iDb   
234c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
234c4 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
234c5 66 20 63 6f 6e 74 61 69 6e 69 6e 67 20 64 61 74  f containing dat
234c6 61 62 61 73 65 2e 20 2a 2f 0a 29 7b 0a 20 20 73  abase. */.){.  s
234c7 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
234c8 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f  rse->db;       /
234c9 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
234ca 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62  e */.  char *zDb
234cb 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
234cc 7a 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f  zName; /* Name o
234cd 66 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  f attached datab
234ce 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ase */.  int rc;
234cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
234d0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68           /* Auth
234d1 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
234d2 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 72 63 20 3d   code */..  rc =
234d3 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70   db->xAuth(db->p
234d4 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54 45 5f  AuthArg, SQLITE_
234d5 52 45 41 44 2c 20 7a 54 61 62 2c 7a 43 6f 6c 2c  READ, zTab,zCol,
234d6 7a 44 62 2c 70 50 61 72 73 65 2d 3e 7a 41 75 74  zDb,pParse->zAut
234d7 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28  hContext);.  if(
234d8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59   rc==SQLITE_DENY
234d9 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   ){.    if( db->
234da 6e 44 62 3e 32 20 7c 7c 20 69 44 62 21 3d 30 20  nDb>2 || iDb!=0 
234db 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
234dc 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
234dd 20 22 61 63 63 65 73 73 20 74 6f 20 25 73 2e 25   "access to %s.%
234de 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69 74  s.%s is prohibit
234df 65 64 22 2c 7a 44 62 2c 7a 54 61 62 2c 7a 43 6f  ed",zDb,zTab,zCo
234e0 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  l);.    }else{. 
234e1 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
234e2 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 63  rMsg(pParse, "ac
234e3 63 65 73 73 20 74 6f 20 25 73 2e 25 73 20 69 73  cess to %s.%s is
234e4 20 70 72 6f 68 69 62 69 74 65 64 22 2c 20 7a 54   prohibited", zT
234e5 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d  ab, zCol);.    }
234e6 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
234e7 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20  = SQLITE_AUTH;. 
234e8 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53   }else if( rc!=S
234e9 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 26 26 20  QLITE_IGNORE && 
234ea 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
234eb 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 68 42  .    sqliteAuthB
234ec 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61  adReturnCode(pPa
234ed 72 73 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rse);.  }.  retu
234ee 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
234ef 54 68 65 20 70 45 78 70 72 20 73 68 6f 75 6c 64  The pExpr should
234f0 20 62 65 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20   be a TK_COLUMN 
234f1 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 65  expression.  The
234f2 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20   table referred 
234f3 74 6f 0a 2a 2a 20 69 73 20 69 6e 20 70 54 61 62  to.** is in pTab
234f4 4c 69 73 74 20 6f 72 20 65 6c 73 65 20 69 74 20  List or else it 
234f5 69 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c  is the NEW or OL
234f6 44 20 74 61 62 6c 65 20 6f 66 20 61 20 74 72 69  D table of a tri
234f7 67 67 65 72 2e 20 20 0a 2a 2a 20 43 68 65 63 6b  gger.  .** Check
234f8 20 74 6f 20 73 65 65 20 69 66 20 69 74 20 69 73   to see if it is
234f9 20 4f 4b 20 74 6f 20 72 65 61 64 20 74 68 69 73   OK to read this
234fa 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75   particular colu
234fb 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  mn..**.** If the
234fc 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72   auth function r
234fd 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 47  eturns SQLITE_IG
234fe 4e 4f 52 45 2c 20 63 68 61 6e 67 65 20 74 68 65  NORE, change the
234ff 20 54 4b 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 69   TK_COLUMN .** i
23500 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 74 6f 20  nstruction into 
23501 61 20 54 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20 74  a TK_NULL.  If t
23502 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e  he auth function
23503 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
23504 44 45 4e 59 2c 0a 2a 2a 20 74 68 65 6e 20 67 65  DENY,.** then ge
23505 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e  nerate an error.
23506 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23507 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
23508 75 74 68 52 65 61 64 28 0a 20 20 50 61 72 73 65  uthRead(.  Parse
23509 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2350a 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
2350b 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
2350c 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
2350d 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
2350e 69 6f 6e 20 74 6f 20 63 68 65 63 6b 20 61 75 74  ion to check aut
2350f 68 6f 72 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a 2f  horization on */
23510 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65  .  Schema *pSche
23511 6d 61 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ma,      /* The 
23512 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78  schema of the ex
23513 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 72  pression */.  Sr
23514 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20  cList *pTabList 
23515 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65      /* All table
23516 20 74 68 61 74 20 70 45 78 70 72 20 6d 69 67 68   that pExpr migh
23517 74 20 72 65 66 65 72 20 74 6f 20 2a 2f 0a 29 7b  t refer to */.){
23518 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
23519 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 54   pParse->db;.  T
2351a 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20  able *pTab = 0; 
2351b 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
2351c 65 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a  e being read */.
2351d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
2351e 6f 6c 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ol;     /* Name 
2351f 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66  of the column of
23520 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
23521 69 6e 74 20 69 53 72 63 3b 20 20 20 20 20 20 20  int iSrc;       
23522 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
23523 6e 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 20  n pTabList->a[] 
23524 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72  of table being r
23525 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ead */.  int iDb
23526 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
23527 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74  * The index of t
23528 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20  he database the 
23529 65 78 70 72 65 73 73 69 6f 6e 20 72 65 66 65 72  expression refer
2352a 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 43  s to */.  int iC
2352b 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
2352c 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
2352d 6d 6e 20 69 6e 20 74 61 62 6c 65 20 2a 2f 0a 0a  mn in table */..
2352e 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d    if( db->xAuth=
2352f 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
23530 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
23531 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
23532 2d 3e 64 62 2c 20 70 53 63 68 65 6d 61 29 3b 0a  ->db, pSchema);.
23533 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20    if( iDb<0 ){. 
23534 20 20 20 2f 2a 20 41 6e 20 61 74 74 65 6d 70 74     /* An attempt
23535 20 74 6f 20 72 65 61 64 20 61 20 63 6f 6c 75 6d   to read a colum
23536 6e 20 6f 75 74 20 6f 66 20 61 20 73 75 62 71 75  n out of a subqu
23537 65 72 79 20 6f 72 20 6f 74 68 65 72 0a 20 20 20  ery or other.   
23538 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61   ** temporary ta
23539 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  ble. */.    retu
2353a 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  rn;.  }..  asser
2353b 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  t( pExpr->op==TK
2353c 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72  _COLUMN || pExpr
2353d 2d 3e 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52  ->op==TK_TRIGGER
2353e 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d   );.  if( pExpr-
2353f 3e 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 20  >op==TK_TRIGGER 
23540 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 50  ){.    pTab = pP
23541 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
23542 62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  b;.  }else{.    
23543 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74  assert( pTabList
23544 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 53 72 63   );.    for(iSrc
23545 3d 30 3b 20 41 4c 57 41 59 53 28 69 53 72 63 3c  =0; ALWAYS(iSrc<
23546 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 29 3b  pTabList->nSrc);
23547 20 69 53 72 63 2b 2b 29 7b 0a 20 20 20 20 20 20   iSrc++){.      
23548 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  if( pExpr->iTabl
23549 65 3d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  e==pTabList->a[i
2354a 53 72 63 5d 2e 69 43 75 72 73 6f 72 20 29 7b 0a  Src].iCursor ){.
2354b 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70          pTab = p
2354c 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d  TabList->a[iSrc]
2354d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 62  .pTab;.        b
2354e 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2354f 20 20 7d 0a 20 20 7d 0a 20 20 69 43 6f 6c 20 3d    }.  }.  iCol =
23550 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b   pExpr->iColumn;
23551 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 54 61  .  if( NEVER(pTa
23552 62 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a  b==0) ) return;.
23553 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29  .  if( iCol>=0 )
23554 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 43  {.    assert( iC
23555 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b  ol<pTab->nCol );
23556 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62  .    zCol = pTab
23557 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61  ->aCol[iCol].zNa
23558 6d 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  me;.  }else if( 
23559 70 54 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29  pTab->iPKey>=0 )
2355a 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  {.    assert( pT
2355b 61 62 2d 3e 69 50 4b 65 79 3c 70 54 61 62 2d 3e  ab->iPKey<pTab->
2355c 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c  nCol );.    zCol
2355d 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54   = pTab->aCol[pT
2355e 61 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65  ab->iPKey].zName
2355f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
23560 43 6f 6c 20 3d 20 22 52 4f 57 49 44 22 3b 0a 20  Col = "ROWID";. 
23561 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62   }.  assert( iDb
23562 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
23563 44 62 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  Db );.  if( SQLI
23564 54 45 5f 49 47 4e 4f 52 45 3d 3d 73 71 6c 69 74  TE_IGNORE==sqlit
23565 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28 70 50  e3AuthReadCol(pP
23566 61 72 73 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  arse, pTab->zNam
23567 65 2c 20 7a 43 6f 6c 2c 20 69 44 62 29 20 29 7b  e, zCol, iDb) ){
23568 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d  .    pExpr->op =
23569 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a   TK_NULL;.  }.}.
2356a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 74  ./*.** Do an aut
2356b 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b  horization check
2356c 20 75 73 69 6e 67 20 74 68 65 20 63 6f 64 65 20   using the code 
2356d 61 6e 64 20 61 72 67 75 6d 65 6e 74 73 20 67 69  and arguments gi
2356e 76 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20  ven.  Return.** 
2356f 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b  either SQLITE_OK
23570 20 28 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 54   (zero) or SQLIT
23571 45 5f 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c 49  E_IGNORE or SQLI
23572 54 45 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 4c  TE_DENY.  If SQL
23573 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20 72  ITE_DENY.** is r
23574 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 68  eturned, then th
23575 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 6e  e error count an
23576 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  d error message 
23577 69 6e 20 70 50 61 72 73 65 20 61 72 65 0a 2a 2a  in pParse are.**
23578 20 6d 6f 64 69 66 69 65 64 20 61 70 70 72 6f 70   modified approp
23579 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  riately..*/.SQLI
2357a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2357b 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2357c 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2357d 2c 0a 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20 20  ,.  int code,.  
2357e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67  const char *zArg
2357f 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  1,.  const char 
23580 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 20  *zArg2,.  const 
23581 63 68 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a 20  char *zArg3.){. 
23582 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
23583 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
23584 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74   rc;..  /* Don't
23585 20 64 6f 20 61 6e 79 20 61 75 74 68 6f 72 69 7a   do any authoriz
23586 61 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20  ation checks if 
23587 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
23588 69 6e 69 74 69 61 6c 69 73 69 6e 67 0a 20 20 2a  initialising.  *
23589 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 72 73  * or if the pars
2358a 65 72 20 69 73 20 62 65 69 6e 67 20 69 6e 76 6f  er is being invo
2358b 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ked from within 
2358c 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
2358d 76 74 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  vtab..  */.  if(
2358e 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c   db->init.busy |
2358f 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41  | IN_DECLARE_VTA
23590 42 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  B ){.    return 
23591 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
23592 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d    if( db->xAuth=
23593 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
23594 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
23595 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68    rc = db->xAuth
23596 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 63  (db->pAuthArg, c
23597 6f 64 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72 67  ode, zArg1, zArg
23598 32 2c 20 7a 41 72 67 33 2c 20 70 50 61 72 73 65  2, zArg3, pParse
23599 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b  ->zAuthContext);
2359a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2359b 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 73 71  E_DENY ){.    sq
2359c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2359d 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f  arse, "not autho
2359e 72 69 7a 65 64 22 29 3b 0a 20 20 20 20 70 50 61  rized");.    pPa
2359f 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
235a0 5f 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69  _AUTH;.  }else i
235a1 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
235a2 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49   && rc!=SQLITE_I
235a3 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 72 63 20  GNORE ){.    rc 
235a4 3d 20 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a 20  = SQLITE_DENY;. 
235a5 20 20 20 73 71 6c 69 74 65 41 75 74 68 42 61 64     sqliteAuthBad
235a6 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73  ReturnCode(pPars
235a7 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
235a8 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75   rc;.}../*.** Pu
235a9 73 68 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74  sh an authorizat
235aa 69 6f 6e 20 63 6f 6e 74 65 78 74 2e 20 20 41 66  ion context.  Af
235ab 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
235ac 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 0a   is called, the.
235ad 2a 2a 20 7a 41 72 67 33 20 61 72 67 75 6d 65 6e  ** zArg3 argumen
235ae 74 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69  t to authorizati
235af 6f 6e 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c  on callbacks wil
235b0 6c 20 62 65 20 7a 43 6f 6e 74 65 78 74 20 75 6e  l be zContext un
235b1 74 69 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e 20 20  til.** popped.  
235b2 4f 72 20 69 66 20 70 50 61 72 73 65 3d 3d 30 2c  Or if pParse==0,
235b3 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
235b4 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c   a no-op..*/.SQL
235b5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
235b6 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
235b7 65 78 74 50 75 73 68 28 0a 20 20 50 61 72 73 65  extPush(.  Parse
235b8 20 2a 70 50 61 72 73 65 2c 0a 20 20 41 75 74 68   *pParse,.  Auth
235b9 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78  Context *pContex
235ba 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t, .  const char
235bb 20 2a 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a 20 20   *zContext.){.  
235bc 61 73 73 65 72 74 28 20 70 50 61 72 73 65 20 29  assert( pParse )
235bd 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50  ;.  pContext->pP
235be 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
235bf 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68   pContext->zAuth
235c0 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65  Context = pParse
235c1 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a  ->zAuthContext;.
235c2 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43    pParse->zAuthC
235c3 6f 6e 74 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78  ontext = zContex
235c4 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20  t;.}../*.** Pop 
235c5 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  an authorization
235c6 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20 77 61   context that wa
235c7 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70 75 73  s previously pus
235c8 68 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65  hed.** by sqlite
235c9 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68  3AuthContextPush
235ca 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
235cb 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
235cc 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75  uthContextPop(Au
235cd 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  thContext *pCont
235ce 65 78 74 29 7b 0a 20 20 69 66 28 20 70 43 6f 6e  ext){.  if( pCon
235cf 74 65 78 74 2d 3e 70 50 61 72 73 65 20 29 7b 0a  text->pParse ){.
235d0 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50      pContext->pP
235d1 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
235d2 78 74 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 7a  xt = pContext->z
235d3 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20  AuthContext;.   
235d4 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73   pContext->pPars
235d5 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 23 65  e = 0;.  }.}..#e
235d6 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
235d7 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
235d8 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  N */../*********
235d9 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 75 74  ***** End of aut
235da 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.c ************
235db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235dd 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
235de 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
235df 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a   build.c *******
235e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235e2 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
235e3 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
235e4 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
235e5 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
235e6 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
235e7 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
235e8 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
235e9 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
235ea 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
235eb 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
235ec 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
235ed 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
235ee 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
235ef 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
235f0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
235f1 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
235f2 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
235f3 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
235f4 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
235f5 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
235f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
235fa 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
235fb 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e  ns C code routin
235fc 65 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c  es that are call
235fd 65 64 20 62 79 20 74 68 65 20 53 51 4c 69 74 65  ed by the SQLite
235fe 20 70 61 72 73 65 72 0a 2a 2a 20 77 68 65 6e 20   parser.** when 
235ff 73 79 6e 74 61 78 20 72 75 6c 65 73 20 61 72 65  syntax rules are
23600 20 72 65 64 75 63 65 64 2e 20 20 54 68 65 20 72   reduced.  The r
23601 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20  outines in this 
23602 66 69 6c 65 20 68 61 6e 64 6c 65 20 74 68 65 0a  file handle the.
23603 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6b 69 6e  ** following kin
23604 64 73 20 6f 66 20 53 51 4c 20 73 79 6e 74 61 78  ds of SQL syntax
23605 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41  :.**.**     CREA
23606 54 45 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20  TE TABLE.**     
23607 44 52 4f 50 20 54 41 42 4c 45 0a 2a 2a 20 20 20  DROP TABLE.**   
23608 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 0a 2a    CREATE INDEX.*
23609 2a 20 20 20 20 20 44 52 4f 50 20 49 4e 44 45 58  *     DROP INDEX
2360a 0a 2a 2a 20 20 20 20 20 63 72 65 61 74 69 6e 67  .**     creating
2360b 20 49 44 20 6c 69 73 74 73 0a 2a 2a 20 20 20 20   ID lists.**    
2360c 20 42 45 47 49 4e 20 54 52 41 4e 53 41 43 54 49   BEGIN TRANSACTI
2360d 4f 4e 0a 2a 2a 20 20 20 20 20 43 4f 4d 4d 49 54  ON.**     COMMIT
2360e 0a 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b  .**     ROLLBACK
2360f 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 75 69 6c  .**.** $Id: buil
23610 64 2e 63 2c 76 20 31 2e 35 35 37 20 32 30 30 39  d.c,v 1.557 2009
23611 2f 30 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20  /07/24 17:58:53 
23612 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
23613 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  $.*/../*.** This
23614 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
23615 65 64 20 77 68 65 6e 20 61 20 6e 65 77 20 53 51  ed when a new SQ
23616 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62  L statement is b
23617 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 62  eginning to.** b
23618 65 20 70 61 72 73 65 64 2e 20 20 49 6e 69 74 69  e parsed.  Initi
23619 61 6c 69 7a 65 20 74 68 65 20 70 50 61 72 73 65  alize the pParse
2361a 20 73 74 72 75 63 74 75 72 65 20 61 73 20 6e 65   structure as ne
2361b 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eded..*/.SQLITE_
2361c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2361d 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 50  ite3BeginParse(P
2361e 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
2361f 74 20 65 78 70 6c 61 69 6e 46 6c 61 67 29 7b 0a  t explainFlag){.
23620 20 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69    pParse->explai
23621 6e 20 3d 20 28 75 38 29 65 78 70 6c 61 69 6e 46  n = (u8)explainF
23622 6c 61 67 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  lag;.  pParse->n
23623 56 61 72 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 6e  Var = 0;.}..#ifn
23624 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23625 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a  SHARED_CACHE./*.
23626 2a 2a 20 54 68 65 20 54 61 62 6c 65 4c 6f 63 6b  ** The TableLock
23627 20 73 74 72 75 63 74 75 72 65 20 69 73 20 6f 6e   structure is on
23628 6c 79 20 75 73 65 64 20 62 79 20 74 68 65 20 73  ly used by the s
23629 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28  qlite3TableLock(
2362a 29 20 61 6e 64 0a 2a 2a 20 63 6f 64 65 54 61 62  ) and.** codeTab
2362b 6c 65 4c 6f 63 6b 73 28 29 20 66 75 6e 63 74 69  leLocks() functi
2362c 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54  ons..*/.struct T
2362d 61 62 6c 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74  ableLock {.  int
2362e 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
2362f 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
23630 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
23631 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63   table to be loc
23632 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  ked */.  int iTa
23633 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b;            /*
23634 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   The root page o
23635 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  f the table to b
23636 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38  e locked */.  u8
23637 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 20 20 20   isWriteLock;   
23638 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 77     /* True for w
23639 72 69 74 65 20 6c 6f 63 6b 2e 20 20 46 61 6c 73  rite lock.  Fals
2363a 65 20 66 6f 72 20 61 20 72 65 61 64 20 6c 6f 63  e for a read loc
2363b 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  k */.  const cha
2363c 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 4e  r *zName;   /* N
2363d 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
2363e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65   */.};../*.** Re
2363f 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68  cord the fact th
23640 61 74 20 77 65 20 77 61 6e 74 20 74 6f 20 6c 6f  at we want to lo
23641 63 6b 20 61 20 74 61 62 6c 65 20 61 74 20 72 75  ck a table at ru
23642 6e 2d 74 69 6d 65 2e 20 20 0a 2a 2a 0a 2a 2a 20  n-time.  .**.** 
23643 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  The table to be 
23644 6c 6f 63 6b 65 64 20 68 61 73 20 72 6f 6f 74 20  locked has root 
23645 70 61 67 65 20 69 54 61 62 20 61 6e 64 20 69 73  page iTab and is
23646 20 66 6f 75 6e 64 20 69 6e 20 64 61 74 61 62 61   found in databa
23647 73 65 20 69 44 62 2e 0a 2a 2a 20 41 20 72 65 61  se iDb..** A rea
23648 64 20 6f 72 20 61 20 77 72 69 74 65 20 6c 6f 63  d or a write loc
23649 6b 20 63 61 6e 20 62 65 20 74 61 6b 65 6e 20 64  k can be taken d
2364a 65 70 65 6e 64 69 6e 67 20 6f 6e 20 69 73 57 72  epending on isWr
2364b 69 74 65 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54  itelock..**.** T
2364c 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74  his routine just
2364d 20 72 65 63 6f 72 64 73 20 74 68 65 20 66 61 63   records the fac
2364e 74 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 20  t that the lock 
2364f 69 73 20 64 65 73 69 72 65 64 2e 20 20 54 68 65  is desired.  The
23650 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 6d 61 6b 65  .** code to make
23651 20 74 68 65 20 6c 6f 63 6b 20 6f 63 63 75 72 20   the lock occur 
23652 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  is generated by 
23653 61 20 6c 61 74 65 72 20 63 61 6c 6c 20 74 6f 0a  a later call to.
23654 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b  ** codeTableLock
23655 73 28 29 20 77 68 69 63 68 20 6f 63 63 75 72 73  s() which occurs
23656 20 64 75 72 69 6e 67 20 73 71 6c 69 74 65 33 46   during sqlite3F
23657 69 6e 69 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a  inishCoding()..*
23658 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23659 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62   void sqlite3Tab
2365a 6c 65 4c 6f 63 6b 28 0a 20 20 50 61 72 73 65 20  leLock(.  Parse 
2365b 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
2365c 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2365d 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  */.  int iDb,   
2365e 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2365f 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
23660 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
23661 74 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f  table to lock */
23662 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20 20 20  .  int iTab,    
23663 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61        /* Root pa
23664 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
23665 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63   table to be loc
23666 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 73 57 72  ked */.  u8 isWr
23667 69 74 65 4c 6f 63 6b 2c 20 20 20 20 2f 2a 20 54  iteLock,    /* T
23668 72 75 65 20 66 6f 72 20 61 20 77 72 69 74 65 20  rue for a write 
23669 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  lock */.  const 
2366a 63 68 61 72 20 2a 7a 4e 61 6d 65 20 20 2f 2a 20  char *zName  /* 
2366b 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  Name of the tabl
2366c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a  e to be locked *
2366d 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54  /.){.  Parse *pT
2366e 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65  oplevel = sqlite
2366f 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70  3ParseToplevel(p
23670 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 69 3b  Parse);.  int i;
23671 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b 0a 20  .  int nBytes;. 
23672 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b 0a 20   TableLock *p;. 
23673 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
23674 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  );..  for(i=0; i
23675 3c 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62  <pToplevel->nTab
23676 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20  leLock; i++){.  
23677 20 20 70 20 3d 20 26 70 54 6f 70 6c 65 76 65 6c    p = &pToplevel
23678 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b  ->aTableLock[i];
23679 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 44 62 3d  .    if( p->iDb=
2367a 3d 69 44 62 20 26 26 20 70 2d 3e 69 54 61 62 3d  =iDb && p->iTab=
2367b 3d 69 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70  =iTab ){.      p
2367c 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20  ->isWriteLock = 
2367d 28 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20  (p->isWriteLock 
2367e 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b  || isWriteLock);
2367f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
23680 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6e 42 79 74     }.  }..  nByt
23681 65 73 20 3d 20 73 69 7a 65 6f 66 28 54 61 62 6c  es = sizeof(Tabl
23682 65 4c 6f 63 6b 29 20 2a 20 28 70 54 6f 70 6c 65  eLock) * (pTople
23683 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b  vel->nTableLock+
23684 31 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d  1);.  pToplevel-
23685 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d 0a 20 20  >aTableLock =.  
23686 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61      sqlite3DbRea
23687 6c 6c 6f 63 4f 72 46 72 65 65 28 70 54 6f 70 6c  llocOrFree(pTopl
23688 65 76 65 6c 2d 3e 64 62 2c 20 70 54 6f 70 6c 65  evel->db, pTople
23689 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c  vel->aTableLock,
2368a 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20   nBytes);.  if( 
2368b 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c  pToplevel->aTabl
2368c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 20 3d  eLock ){.    p =
2368d 20 26 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61   &pToplevel->aTa
2368e 62 6c 65 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76 65  bleLock[pTopleve
2368f 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 2b 5d  l->nTableLock++]
23690 3b 0a 20 20 20 20 70 2d 3e 69 44 62 20 3d 20 69  ;.    p->iDb = i
23691 44 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 20  Db;.    p->iTab 
23692 3d 20 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69  = iTab;.    p->i
23693 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 69 73 57  sWriteLock = isW
23694 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 70 2d  riteLock;.    p-
23695 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a  >zName = zName;.
23696 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f    }else{.    pTo
23697 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f  plevel->nTableLo
23698 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 54 6f 70  ck = 0;.    pTop
23699 6c 65 76 65 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  level->db->mallo
2369a 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d  cFailed = 1;.  }
2369b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61  .}../*.** Code a
2369c 6e 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69  n OP_TableLock i
2369d 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 65  nstruction for e
2369e 61 63 68 20 74 61 62 6c 65 20 6c 6f 63 6b 65 64  ach table locked
2369f 20 62 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65   by the.** state
236a0 6d 65 6e 74 20 28 63 6f 6e 66 69 67 75 72 65 64  ment (configured
236a1 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c   by calls to sql
236a2 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29  ite3TableLock())
236a3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
236a4 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28   codeTableLocks(
236a5 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
236a6 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20    int i;.  Vdbe 
236a7 2a 70 56 64 62 65 3b 20 0a 0a 20 20 70 56 64 62  *pVdbe; ..  pVdb
236a8 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  e = sqlite3GetVd
236a9 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73  be(pParse);.  as
236aa 73 65 72 74 28 20 70 56 64 62 65 21 3d 30 20 29  sert( pVdbe!=0 )
236ab 3b 20 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 56  ; /* sqlite3GetV
236ac 64 62 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 3a  dbe cannot fail:
236ad 20 56 44 42 45 20 61 6c 72 65 61 64 79 20 61 6c   VDBE already al
236ae 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 66 6f  located */..  fo
236af 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d  r(i=0; i<pParse-
236b0 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b  >nTableLock; i++
236b1 29 7b 0a 20 20 20 20 54 61 62 6c 65 4c 6f 63 6b  ){.    TableLock
236b2 20 2a 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61   *p = &pParse->a
236b3 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20  TableLock[i];.  
236b4 20 20 69 6e 74 20 70 31 20 3d 20 70 2d 3e 69 44    int p1 = p->iD
236b5 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  b;.    sqlite3Vd
236b6 62 65 41 64 64 4f 70 34 28 70 56 64 62 65 2c 20  beAddOp4(pVdbe, 
236b7 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 70 31  OP_TableLock, p1
236b8 2c 20 70 2d 3e 69 54 61 62 2c 20 70 2d 3e 69 73  , p->iTab, p->is
236b9 57 72 69 74 65 4c 6f 63 6b 2c 0a 20 20 20 20 20  WriteLock,.     
236ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
236bb 20 70 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54   p->zName, P4_ST
236bc 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c  ATIC);.  }.}.#el
236bd 73 65 0a 20 20 23 64 65 66 69 6e 65 20 63 6f 64  se.  #define cod
236be 65 54 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0a 23  eTableLocks(x).#
236bf 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
236c0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
236c1 6c 65 64 20 61 66 74 65 72 20 61 20 73 69 6e 67  led after a sing
236c2 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  le SQL statement
236c3 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 61 72   has been.** par
236c4 73 65 64 20 61 6e 64 20 61 20 56 44 42 45 20 70  sed and a VDBE p
236c5 72 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74  rogram to execut
236c6 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74  e that statement
236c7 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65   has been.** pre
236c8 70 61 72 65 64 2e 20 20 54 68 69 73 20 72 6f 75  pared.  This rou
236c9 74 69 6e 65 20 70 75 74 73 20 74 68 65 20 66 69  tine puts the fi
236ca 6e 69 73 68 69 6e 67 20 74 6f 75 63 68 65 73 20  nishing touches 
236cb 6f 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 20 70  on the.** VDBE p
236cc 72 6f 67 72 61 6d 20 61 6e 64 20 72 65 73 65 74  rogram and reset
236cd 73 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72  s the pParse str
236ce 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 6e  ucture for the n
236cf 65 78 74 0a 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a  ext.** parse..**
236d0 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66  .** Note that if
236d1 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72   an error occurr
236d2 65 64 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20  ed, it might be 
236d3 74 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a  the case that.**
236d4 20 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 77 61   no VDBE code wa
236d5 73 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a  s generated..*/.
236d6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
236d7 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73  oid sqlite3Finis
236d8 68 43 6f 64 69 6e 67 28 50 61 72 73 65 20 2a 70  hCoding(Parse *p
236d9 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
236da 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76  3 *db;.  Vdbe *v
236db 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  ;..  db = pParse
236dc 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e  ->db;.  if( db->
236dd 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
236de 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61  eturn;.  if( pPa
236df 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65  rse->nested ) re
236e0 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72  turn;.  if( pPar
236e1 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72  se->nErr ) retur
236e2 6e 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62  n;..  /* Begin b
236e3 79 20 67 65 6e 65 72 61 74 69 6e 67 20 73 6f 6d  y generating som
236e4 65 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f  e termination co
236e5 64 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  de at the end of
236e6 20 74 68 65 0a 20 20 2a 2a 20 76 64 62 65 20 70   the.  ** vdbe p
236e7 72 6f 67 72 61 6d 0a 20 20 2a 2f 0a 20 20 76 20  rogram.  */.  v 
236e8 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
236e9 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65  (pParse);.  asse
236ea 72 74 28 20 21 70 50 61 72 73 65 2d 3e 69 73 4d  rt( !pParse->isM
236eb 75 6c 74 69 57 72 69 74 65 20 0a 20 20 20 20 20  ultiWrite .     
236ec 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65    || sqlite3Vdbe
236ed 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 76  AssertMayAbort(v
236ee 2c 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f  , pParse->mayAbo
236ef 72 74 29 29 3b 0a 20 20 69 66 28 20 76 20 29 7b  rt));.  if( v ){
236f0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
236f1 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 48 61 6c  AddOp0(v, OP_Hal
236f2 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20  t);..    /* The 
236f3 63 6f 6f 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74  cookie mask cont
236f4 61 69 6e 73 20 6f 6e 65 20 62 69 74 20 66 6f 72  ains one bit for
236f5 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66   each database f
236f6 69 6c 65 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a  ile open..    **
236f7 20 28 42 69 74 20 30 20 69 73 20 66 6f 72 20 6d   (Bit 0 is for m
236f8 61 69 6e 2c 20 62 69 74 20 31 20 69 73 20 66 6f  ain, bit 1 is fo
236f9 72 20 74 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66  r temp, and so f
236fa 6f 72 74 68 2e 29 20 20 42 69 74 73 20 61 72 65  orth.)  Bits are
236fb 0a 20 20 20 20 2a 2a 20 73 65 74 20 66 6f 72 20  .    ** set for 
236fc 65 61 63 68 20 64 61 74 61 62 61 73 65 20 74 68  each database th
236fd 61 74 20 69 73 20 75 73 65 64 2e 20 20 47 65 6e  at is used.  Gen
236fe 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74  erate code to st
236ff 61 72 74 20 61 0a 20 20 20 20 2a 2a 20 74 72 61  art a.    ** tra
23700 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68  nsaction on each
23701 20 75 73 65 64 20 64 61 74 61 62 61 73 65 20 61   used database a
23702 6e 64 20 74 6f 20 76 65 72 69 66 79 20 74 68 65  nd to verify the
23703 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 20   schema cookie. 
23704 20 20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 75 73     ** on each us
23705 65 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20  ed database..   
23706 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72   */.    if( pPar
23707 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30  se->cookieGoto>0
23708 20 29 7b 0a 20 20 20 20 20 20 75 33 32 20 6d 61   ){.      u32 ma
23709 73 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44  sk;.      int iD
2370a 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  b;.      sqlite3
2370b 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2370c 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f  pParse->cookieGo
2370d 74 6f 2d 31 29 3b 0a 20 20 20 20 20 20 66 6f 72  to-1);.      for
2370e 28 69 44 62 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20  (iDb=0, mask=1; 
2370f 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 6d 61 73  iDb<db->nDb; mas
23710 6b 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29 7b 0a 20  k<<=1, iDb++){. 
23711 20 20 20 20 20 20 20 69 66 28 20 28 6d 61 73 6b         if( (mask
23712 20 26 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69   & pParse->cooki
23713 65 4d 61 73 6b 29 3d 3d 30 20 29 20 63 6f 6e 74  eMask)==0 ) cont
23714 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 73 71  inue;.        sq
23715 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72  lite3VdbeUsesBtr
23716 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20  ee(v, iDb);.    
23717 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23718 64 64 4f 70 32 28 76 2c 4f 50 5f 54 72 61 6e 73  ddOp2(v,OP_Trans
23719 61 63 74 69 6f 6e 2c 20 69 44 62 2c 20 28 6d 61  action, iDb, (ma
2371a 73 6b 20 26 20 70 50 61 72 73 65 2d 3e 77 72 69  sk & pParse->wri
2371b 74 65 4d 61 73 6b 29 21 3d 30 29 3b 0a 20 20 20  teMask)!=0);.   
2371c 20 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69       if( db->ini
2371d 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20  t.busy==0 ){.   
2371e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2371f 62 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 56 65  beAddOp2(v,OP_Ve
23720 72 69 66 79 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  rifyCookie, iDb,
23721 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 56   pParse->cookieV
23722 61 6c 75 65 5b 69 44 62 5d 29 3b 0a 20 20 20 20  alue[iDb]);.    
23723 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69      }.      }.#i
23724 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
23725 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
23726 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69       {.        i
23727 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f  nt i;.        fo
23728 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d  r(i=0; i<pParse-
23729 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29  >nVtabLock; i++)
2372a 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72  {.          char
2372b 20 2a 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a   *vtab = (char *
2372c 29 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c  )sqlite3GetVTabl
2372d 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 70  e(db, pParse->ap
2372e 56 74 61 62 4c 6f 63 6b 5b 69 5d 29 3b 0a 20 20  VtabLock[i]);.  
2372f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
23730 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
23731 56 42 65 67 69 6e 2c 20 30 2c 20 30 2c 20 30 2c  VBegin, 0, 0, 0,
23732 20 76 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b   vtab, P4_VTAB);
23733 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
23734 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 74 61 62     pParse->nVtab
23735 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20  Lock = 0;.      
23736 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20  }.#endif..      
23737 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20  /* Once all the 
23738 63 6f 6f 6b 69 65 73 20 68 61 76 65 20 62 65 65  cookies have bee
23739 6e 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 74  n verified and t
2373a 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e  ransactions open
2373b 65 64 2c 20 0a 20 20 20 20 20 20 2a 2a 20 6f 62  ed, .      ** ob
2373c 74 61 69 6e 20 74 68 65 20 72 65 71 75 69 72 65  tain the require
2373d 64 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 54  d table-locks. T
2373e 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75  his is a no-op u
2373f 6e 6c 65 73 73 20 74 68 65 20 0a 20 20 20 20 20  nless the .     
23740 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65   ** shared-cache
23741 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62   feature is enab
23742 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  led..      */.  
23743 20 20 20 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63      codeTableLoc
23744 6b 73 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20  ks(pParse);..   
23745 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65     /* Initialize
23746 20 61 6e 79 20 41 55 54 4f 49 4e 43 52 45 4d 45   any AUTOINCREME
23747 4e 54 20 64 61 74 61 20 73 74 72 75 63 74 75 72  NT data structur
23748 65 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20  es required..   
23749 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
2374a 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  te3Autoincrement
2374b 42 65 67 69 6e 28 70 50 61 72 73 65 29 3b 0a 0a  Begin(pParse);..
2374c 20 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79        /* Finally
2374d 2c 20 6a 75 6d 70 20 62 61 63 6b 20 74 6f 20 74  , jump back to t
2374e 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
2374f 74 68 65 20 65 78 65 63 75 74 61 62 6c 65 20 63  the executable c
23750 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71  ode. */.      sq
23751 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
23752 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70  v, OP_Goto, 0, p
23753 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
23754 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a  o);.    }.  }...
23755 20 20 2f 2a 20 47 65 74 20 74 68 65 20 56 44 42    /* Get the VDB
23756 45 20 70 72 6f 67 72 61 6d 20 72 65 61 64 79 20  E program ready 
23757 66 6f 72 20 65 78 65 63 75 74 69 6f 6e 0a 20 20  for execution.  
23758 2a 2f 0a 20 20 69 66 28 20 76 20 26 26 20 41 4c  */.  if( v && AL
23759 57 41 59 53 28 70 50 61 72 73 65 2d 3e 6e 45 72  WAYS(pParse->nEr
2375a 72 3d 3d 30 29 20 26 26 20 21 64 62 2d 3e 6d 61  r==0) && !db->ma
2375b 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 23 69  llocFailed ){.#i
2375c 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
2375d 47 0a 20 20 20 20 46 49 4c 45 20 2a 74 72 61 63  G.    FILE *trac
2375e 65 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26  e = (db->flags &
2375f 20 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63   SQLITE_VdbeTrac
23760 65 29 21 3d 30 20 3f 20 73 74 64 6f 75 74 20 3a  e)!=0 ? stdout :
23761 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   0;.    sqlite3V
23762 64 62 65 54 72 61 63 65 28 76 2c 20 74 72 61 63  dbeTrace(v, trac
23763 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 61  e);.#endif.    a
23764 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 69  ssert( pParse->i
23765 43 61 63 68 65 4c 65 76 65 6c 3d 3d 30 20 29 3b  CacheLevel==0 );
23766 20 20 2f 2a 20 44 69 73 61 62 6c 65 73 20 61 6e    /* Disables an
23767 64 20 72 65 2d 65 6e 61 62 6c 65 73 20 6d 61 74  d re-enables mat
23768 63 68 20 2a 2f 0a 20 20 20 20 2f 2a 20 41 20 6d  ch */.    /* A m
23769 69 6e 69 6d 75 6d 20 6f 66 20 6f 6e 65 20 63 75  inimum of one cu
2376a 72 73 6f 72 20 69 73 20 72 65 71 75 69 72 65 64  rsor is required
2376b 20 69 66 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e   if autoincremen
2376c 74 20 69 73 20 75 73 65 64 0a 20 20 20 20 2a 20  t is used.    * 
2376d 20 53 65 65 20 74 69 63 6b 65 74 20 5b 61 36 39   See ticket [a69
2376e 36 33 37 39 63 31 66 30 38 38 36 36 5d 20 2a 2f  6379c1f08866] */
2376f 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d  .    if( pParse-
23770 3e 70 41 69 6e 63 21 3d 30 20 26 26 20 70 50 61  >pAinc!=0 && pPa
23771 72 73 65 2d 3e 6e 54 61 62 3d 3d 30 20 29 20 70  rse->nTab==0 ) p
23772 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 31 3b  Parse->nTab = 1;
23773 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23774 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 70 50 61  MakeReady(v, pPa
23775 72 73 65 2d 3e 6e 56 61 72 2c 20 70 50 61 72 73  rse->nVar, pPars
23776 65 2d 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20 20 20  e->nMem,.       
23777 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23778 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2c 20    pParse->nTab, 
23779 70 50 61 72 73 65 2d 3e 6e 4d 61 78 41 72 67 2c  pParse->nMaxArg,
2377a 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
2377b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2377c 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
2377d 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20  e->isMultiWrite 
2377e 26 26 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 62  && pParse->mayAb
2377f 6f 72 74 29 3b 0a 20 20 20 20 70 50 61 72 73 65  ort);.    pParse
23780 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f  ->rc = SQLITE_DO
23781 4e 45 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  NE;.    pParse->
23782 63 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b  colNamesSet = 0;
23783 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61  .  }else if( pPa
23784 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  rse->rc==SQLITE_
23785 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  OK ){.    pParse
23786 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52  ->rc = SQLITE_ER
23787 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73  ROR;.  }.  pPars
23788 65 2d 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70  e->nTab = 0;.  p
23789 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b  Parse->nMem = 0;
2378a 0a 20 20 70 50 61 72 73 65 2d 3e 6e 53 65 74 20  .  pParse->nSet 
2378b 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  = 0;.  pParse->n
2378c 56 61 72 20 3d 20 30 3b 0a 20 20 70 50 61 72 73  Var = 0;.  pPars
2378d 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20  e->cookieMask = 
2378e 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f  0;.  pParse->coo
2378f 6b 69 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a 0a  kieGoto = 0;.}..
23790 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61  /*.** Run the pa
23791 72 73 65 72 20 61 6e 64 20 63 6f 64 65 20 67 65  rser and code ge
23792 6e 65 72 61 74 6f 72 20 72 65 63 75 72 73 69 76  nerator recursiv
23793 65 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ely in order to 
23794 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65  generate.** code
23795 20 66 6f 72 20 74 68 65 20 53 51 4c 20 73 74 61   for the SQL sta
23796 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 6f 6e 74  tement given ont
23797 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
23798 20 70 50 61 72 73 65 20 63 6f 6e 74 65 78 74 0a   pParse context.
23799 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64  ** currently und
2379a 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e  er construction.
2379b 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 73 65    When the parse
2379c 72 20 69 73 20 72 75 6e 20 72 65 63 75 72 73 69  r is run recursi
2379d 76 65 6c 79 0a 2a 2a 20 74 68 69 73 20 77 61 79  vely.** this way
2379e 2c 20 74 68 65 20 66 69 6e 61 6c 20 4f 50 5f 48  , the final OP_H
2379f 61 6c 74 20 69 73 20 6e 6f 74 20 61 70 70 65 6e  alt is not appen
237a0 64 65 64 20 61 6e 64 20 6f 74 68 65 72 20 69 6e  ded and other in
237a1 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20  itialization.** 
237a2 61 6e 64 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e  and finalization
237a3 20 73 74 65 70 73 20 61 72 65 20 6f 6d 69 74 74   steps are omitt
237a4 65 64 20 62 65 63 61 75 73 65 20 74 68 6f 73 65  ed because those
237a5 20 61 72 65 20 68 61 6e 64 6c 69 6e 67 20 62 79   are handling by
237a6 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73   the.** outermos
237a7 74 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20  t parser..**.** 
237a8 4e 6f 74 20 65 76 65 72 79 74 68 69 6e 67 20 69  Not everything i
237a9 73 20 6e 65 73 74 61 62 6c 65 2e 20 20 54 68 69  s nestable.  Thi
237aa 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 64 65  s facility is de
237ab 73 69 67 6e 65 64 20 74 6f 20 70 65 72 6d 69 74  signed to permit
237ac 0a 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50 44 41  .** INSERT, UPDA
237ad 54 45 2c 20 61 6e 64 20 44 45 4c 45 54 45 20 6f  TE, and DELETE o
237ae 70 65 72 61 74 69 6f 6e 73 20 61 67 61 69 6e 73  perations agains
237af 74 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e  t SQLITE_MASTER.
237b0 20 20 55 73 65 0a 2a 2a 20 63 61 72 65 20 69 66    Use.** care if
237b1 20 79 6f 75 20 64 65 63 69 64 65 20 74 6f 20 74   you decide to t
237b2 72 79 20 74 6f 20 75 73 65 20 74 68 69 73 20 72  ry to use this r
237b3 6f 75 74 69 6e 65 20 66 6f 72 20 73 6f 6d 65 20  outine for some 
237b4 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a  other purposes..
237b5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
237b6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65  E void sqlite3Ne
237b7 73 74 65 64 50 61 72 73 65 28 50 61 72 73 65 20  stedParse(Parse 
237b8 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63  *pParse, const c
237b9 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e  har *zFormat, ..
237ba 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  .){.  va_list ap
237bb 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a  ;.  char *zSql;.
237bc 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20    char *zErrMsg 
237bd 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  = 0;.  sqlite3 *
237be 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
237bf 0a 23 20 64 65 66 69 6e 65 20 53 41 56 45 5f 53  .# define SAVE_S
237c0 5a 20 20 28 73 69 7a 65 6f 66 28 50 61 72 73 65  Z  (sizeof(Parse
237c1 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 50 61 72  ) - offsetof(Par
237c2 73 65 2c 6e 56 61 72 29 29 0a 20 20 63 68 61 72  se,nVar)).  char
237c3 20 73 61 76 65 42 75 66 5b 53 41 56 45 5f 53 5a   saveBuf[SAVE_SZ
237c4 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65  ];..  if( pParse
237c5 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e 3b  ->nErr ) return;
237c6 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
237c7 65 2d 3e 6e 65 73 74 65 64 3c 31 30 20 29 3b 20  e->nested<10 ); 
237c8 20 2f 2a 20 4e 65 73 74 69 6e 67 20 73 68 6f 75   /* Nesting shou
237c9 6c 64 20 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69  ld only be of li
237ca 6d 69 74 65 64 20 64 65 70 74 68 20 2a 2f 0a 20  mited depth */. 
237cb 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
237cc 6f 72 6d 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d  ormat);.  zSql =
237cd 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66   sqlite3VMPrintf
237ce 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  (db, zFormat, ap
237cf 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  );.  va_end(ap);
237d0 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29  .  if( zSql==0 )
237d1 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 20 20  {.    return;   
237d2 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 74  /* A malloc must
237d3 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a   have failed */.
237d4 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65    }.  pParse->ne
237d5 73 74 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79  sted++;.  memcpy
237d6 28 73 61 76 65 42 75 66 2c 20 26 70 50 61 72 73  (saveBuf, &pPars
237d7 65 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a  e->nVar, SAVE_SZ
237d8 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 50 61  );.  memset(&pPa
237d9 72 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41  rse->nVar, 0, SA
237da 56 45 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65  VE_SZ);.  sqlite
237db 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72 73  3RunParser(pPars
237dc 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73  e, zSql, &zErrMs
237dd 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  g);.  sqlite3DbF
237de 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29  ree(db, zErrMsg)
237df 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
237e0 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d  e(db, zSql);.  m
237e1 65 6d 63 70 79 28 26 70 50 61 72 73 65 2d 3e 6e  emcpy(&pParse->n
237e2 56 61 72 2c 20 73 61 76 65 42 75 66 2c 20 53 41  Var, saveBuf, SA
237e3 56 45 5f 53 5a 29 3b 0a 20 20 70 50 61 72 73 65  VE_SZ);.  pParse
237e4 2d 3e 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f  ->nested--;.}../
237e5 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20  *.** Locate the 
237e6 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74  in-memory struct
237e7 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62  ure that describ
237e8 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  es a particular 
237e9 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c  database.** tabl
237ea 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65  e given the name
237eb 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 61   of that table a
237ec 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20  nd (optionally) 
237ed 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a  the name of the.
237ee 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  ** database cont
237ef 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65  aining the table
237f0 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69  .  Return NULL i
237f1 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a  f not found..**.
237f2 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73 65 20  ** If zDatabase 
237f3 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61  is 0, all databa
237f4 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64  ses are searched
237f5 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61   for the table a
237f6 6e 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  nd the.** first 
237f7 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 20 69  matching table i
237f8 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f  s returned.  (No
237f9 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 64 75   checking for du
237fa 70 6c 69 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a  plicate table.**
237fb 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29   names is done.)
237fc 20 20 54 68 65 20 73 65 61 72 63 68 20 6f 72 64    The search ord
237fd 65 72 20 69 73 20 54 45 4d 50 20 66 69 72 73 74  er is TEMP first
237fe 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65  , then MAIN, the
237ff 6e 20 61 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61  n any.** auxilia
23800 72 79 20 64 61 74 61 62 61 73 65 73 20 61 64 64  ry databases add
23801 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 54 54  ed using the ATT
23802 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  ACH command..**.
23803 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69  ** See also sqli
23804 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 29  te3LocateTable()
23805 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23806 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74  ATE Table *sqlit
23807 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71 6c 69  e3FindTable(sqli
23808 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
23809 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73  har *zName, cons
2380a 74 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73  t char *zDatabas
2380b 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d  e){.  Table *p =
2380c 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69   0;.  int i;.  i
2380d 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 61 73 73 65  nt nName;.  asse
2380e 72 74 28 20 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a  rt( zName!=0 );.
2380f 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    nName = sqlite
23810 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
23811 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54  ;.  for(i=OMIT_T
23812 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62  EMPDB; i<db->nDb
23813 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  ; i++){.    int 
23814 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20  j = (i<2) ? i^1 
23815 3a 20 69 3b 20 20 20 2f 2a 20 53 65 61 72 63 68  : i;   /* Search
23816 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49   TEMP before MAI
23817 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 61  N */.    if( zDa
23818 74 61 62 61 73 65 21 3d 30 20 26 26 20 73 71 6c  tabase!=0 && sql
23819 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74  ite3StrICmp(zDat
2381a 61 62 61 73 65 2c 20 64 62 2d 3e 61 44 62 5b 6a  abase, db->aDb[j
2381b 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69  ].zName) ) conti
2381c 6e 75 65 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c  nue;.    p = sql
2381d 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 64 62  ite3HashFind(&db
2381e 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61  ->aDb[j].pSchema
2381f 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 4e 61 6d 65  ->tblHash, zName
23820 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66  , nName);.    if
23821 28 20 70 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  ( p ) break;.  }
23822 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
23823 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65  /*.** Locate the
23824 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63   in-memory struc
23825 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
23826 62 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72  bes a particular
23827 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62   database.** tab
23828 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d  le given the nam
23829 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20  e of that table 
2382a 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29  and (optionally)
2382b 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
2382c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
2382d 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c  taining the tabl
2382e 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  e.  Return NULL 
2382f 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 20 20 41  if not found.  A
23830 6c 73 6f 20 6c 65 61 76 65 20 61 6e 0a 2a 2a 20  lso leave an.** 
23831 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
23832 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
23833 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66  ..**.** The diff
23834 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74  erence between t
23835 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20  his routine and 
23836 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
23837 28 29 20 69 73 20 74 68 61 74 20 74 68 69 73 0a  () is that this.
23838 2a 2a 20 72 6f 75 74 69 6e 65 20 6c 65 61 76 65  ** routine leave
23839 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  s an error messa
2383a 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45  ge in pParse->zE
2383b 72 72 4d 73 67 20 77 68 65 72 65 0a 2a 2a 20 73  rrMsg where.** s
2383c 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
2383d 29 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 53  ) does not..*/.S
2383e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61  QLITE_PRIVATE Ta
2383f 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61  ble *sqlite3Loca
23840 74 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  teTable(.  Parse
23841 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
23842 20 20 2f 2a 20 63 6f 6e 74 65 78 74 20 69 6e 20    /* context in 
23843 77 68 69 63 68 20 74 6f 20 72 65 70 6f 72 74 20  which to report 
23844 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20  errors */.  int 
23845 69 73 56 69 65 77 2c 20 20 20 20 20 20 20 20 20  isView,         
23846 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 6f     /* True if lo
23847 6f 6b 69 6e 67 20 66 6f 72 20 61 20 56 49 45 57  oking for a VIEW
23848 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 54   rather than a T
23849 41 42 4c 45 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ABLE */.  const 
2384a 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
2384b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
2384c 74 61 62 6c 65 20 77 65 20 61 72 65 20 6c 6f 6f  table we are loo
2384d 6b 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 63 6f  king for */.  co
2384e 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 61 73 65  nst char *zDbase
2384f 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
23850 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d  the database.  M
23851 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a  ight be NULL */.
23852 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 0a  ){.  Table *p;..
23853 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61    /* Read the da
23854 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49  tabase schema. I
23855 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
23856 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f  s, leave an erro
23857 72 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61  r message.  ** a
23858 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73  nd code in pPars
23859 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c  e and return NUL
2385a 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49  L. */.  if( SQLI
2385b 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65  TE_OK!=sqlite3Re
2385c 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
2385d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2385e 3b 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c  ;.  }..  p = sql
2385f 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 70 50  ite3FindTable(pP
23860 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 2c  arse->db, zName,
23861 20 7a 44 62 61 73 65 29 3b 0a 20 20 69 66 28 20   zDbase);.  if( 
23862 70 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73  p==0 ){.    cons
23863 74 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 69  t char *zMsg = i
23864 73 56 69 65 77 20 3f 20 22 6e 6f 20 73 75 63 68  sView ? "no such
23865 20 76 69 65 77 22 20 3a 20 22 6e 6f 20 73 75 63   view" : "no suc
23866 68 20 74 61 62 6c 65 22 3b 0a 20 20 20 20 69 66  h table";.    if
23867 28 20 7a 44 62 61 73 65 20 29 7b 0a 20 20 20 20  ( zDbase ){.    
23868 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
23869 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25  g(pParse, "%s: %
2386a 73 2e 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 44 62  s.%s", zMsg, zDb
2386b 61 73 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  ase, zName);.   
2386c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
2386d 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2386e 61 72 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20  arse, "%s: %s", 
2386f 7a 4d 73 67 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  zMsg, zName);.  
23870 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e    }.    pParse->
23871 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b  checkSchema = 1;
23872 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
23873 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65  .}../*.** Locate
23874 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73   the in-memory s
23875 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65  tructure that de
23876 73 63 72 69 62 65 73 20 0a 2a 2a 20 61 20 70 61  scribes .** a pa
23877 72 74 69 63 75 6c 61 72 20 69 6e 64 65 78 20 67  rticular index g
23878 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  iven the name of
23879 20 74 68 61 74 20 69 6e 64 65 78 0a 2a 2a 20 61   that index.** a
2387a 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  nd the name of t
2387b 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74  he database that
2387c 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6e   contains the in
2387d 64 65 78 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e  dex..** Return N
2387e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64  ULL if not found
2387f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61  ..**.** If zData
23880 62 61 73 65 20 69 73 20 30 2c 20 61 6c 6c 20 64  base is 0, all d
23881 61 74 61 62 61 73 65 73 20 61 72 65 20 73 65 61  atabases are sea
23882 72 63 68 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a  rched for the.**
23883 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 66   table and the f
23884 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 69 6e  irst matching in
23885 64 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 2e  dex is returned.
23886 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e 67 0a 2a    (No checking.*
23887 2a 20 66 6f 72 20 64 75 70 6c 69 63 61 74 65 20  * for duplicate 
23888 69 6e 64 65 78 20 6e 61 6d 65 73 20 69 73 20 64  index names is d
23889 6f 6e 65 2e 29 20 20 54 68 65 20 73 65 61 72 63  one.)  The searc
2388a 68 20 6f 72 64 65 72 20 69 73 0a 2a 2a 20 54 45  h order is.** TE
2388b 4d 50 20 66 69 72 73 74 2c 20 74 68 65 6e 20 4d  MP first, then M
2388c 41 49 4e 2c 20 74 68 65 6e 20 61 6e 79 20 61 75  AIN, then any au
2388d 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
2388e 73 20 61 64 64 65 64 0a 2a 2a 20 75 73 69 6e 67  s added.** using
2388f 20 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d   the ATTACH comm
23890 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  and..*/.SQLITE_P
23891 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71  RIVATE Index *sq
23892 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 73  lite3FindIndex(s
23893 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
23894 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63  t char *zName, c
23895 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 29 7b  onst char *zDb){
23896 0a 20 20 49 6e 64 65 78 20 2a 70 20 3d 20 30 3b  .  Index *p = 0;
23897 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
23898 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
23899 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a  trlen30(zName);.
2389a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d    for(i=OMIT_TEM
2389b 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  PDB; i<db->nDb; 
2389c 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20  i++){.    int j 
2389d 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20  = (i<2) ? i^1 : 
2389e 69 3b 20 20 2f 2a 20 53 65 61 72 63 68 20 54 45  i;  /* Search TE
2389f 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a  MP before MAIN *
238a0 2f 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53  /.    Schema *pS
238a1 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b  chema = db->aDb[
238a2 6a 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20  j].pSchema;.    
238a3 61 73 73 65 72 74 28 20 70 53 63 68 65 6d 61 20  assert( pSchema 
238a4 29 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 26  );.    if( zDb &
238a5 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
238a6 28 7a 44 62 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d  (zDb, db->aDb[j]
238a7 2e 7a 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e  .zName) ) contin
238a8 75 65 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69  ue;.    p = sqli
238a9 74 65 33 48 61 73 68 46 69 6e 64 28 26 70 53 63  te3HashFind(&pSc
238aa 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a  hema->idxHash, z
238ab 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
238ac 20 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b    if( p ) break;
238ad 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
238ae 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69  .}../*.** Reclai
238af 6d 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65  m the memory use
238b0 64 20 62 79 20 61 6e 20 69 6e 64 65 78 0a 2a 2f  d by an index.*/
238b1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65  .static void fre
238b2 65 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29  eIndex(Index *p)
238b3 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
238b4 3d 20 70 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 4d  = p->pTable->dbM
238b5 65 6d 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  em;.#ifndef SQLI
238b6 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a  TE_OMIT_ANALYZE.
238b7 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49    sqlite3DeleteI
238b8 6e 64 65 78 53 61 6d 70 6c 65 73 28 70 29 3b 0a  ndexSamples(p);.
238b9 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
238ba 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 43  DbFree(db, p->zC
238bb 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 74 65  olAff);.  sqlite
238bc 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
238bd 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
238be 74 68 65 20 67 69 76 65 6e 20 69 6e 64 65 78 20  the given index 
238bf 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 68  from the index h
238c0 61 73 68 20 74 61 62 6c 65 2c 20 61 6e 64 20 66  ash table, and f
238c1 72 65 65 0a 2a 2a 20 69 74 73 20 6d 65 6d 6f 72  ree.** its memor
238c2 79 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a  y structures..**
238c3 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 69 73  .** The index is
238c4 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
238c5 65 20 64 61 74 61 62 61 73 65 20 68 61 73 68 20  e database hash 
238c6 74 61 62 6c 65 73 20 62 75 74 0a 2a 2a 20 69 74  tables but.** it
238c7 20 69 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 65 64   is not unlinked
238c8 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20   from the Table 
238c9 74 68 61 74 20 69 74 20 69 6e 64 65 78 65 73 2e  that it indexes.
238ca 0a 2a 2a 20 55 6e 6c 69 6e 6b 69 6e 67 20 66 72  .** Unlinking fr
238cb 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 6d 75 73  om the Table mus
238cc 74 20 62 65 20 64 6f 6e 65 20 62 79 20 74 68 65  t be done by the
238cd 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
238ce 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
238cf 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49  d sqlite3DeleteI
238d0 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29 7b 0a  ndex(Index *p){.
238d1 20 20 49 6e 64 65 78 20 2a 70 4f 6c 64 3b 0a 20    Index *pOld;. 
238d2 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
238d3 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 0a  me = p->zName;..
238d4 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33    pOld = sqlite3
238d5 48 61 73 68 49 6e 73 65 72 74 28 26 70 2d 3e 70  HashInsert(&p->p
238d6 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c  Schema->idxHash,
238d7 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20   zName,.        
238d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
238d9 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e     sqlite3Strlen
238da 33 30 28 7a 4e 61 6d 65 29 2c 20 30 29 3b 0a 20  30(zName), 0);. 
238db 20 61 73 73 65 72 74 28 20 70 4f 6c 64 3d 3d 30   assert( pOld==0
238dc 20 7c 7c 20 70 4f 6c 64 3d 3d 70 20 29 3b 0a 20   || pOld==p );. 
238dd 20 66 72 65 65 49 6e 64 65 78 28 70 29 3b 0a 7d   freeIndex(p);.}
238de 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20  ../*.** For the 
238df 69 6e 64 65 78 20 63 61 6c 6c 65 64 20 7a 49 64  index called zId
238e0 78 4e 61 6d 65 20 77 68 69 63 68 20 69 73 20 66  xName which is f
238e1 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61  ound in the data
238e2 62 61 73 65 20 69 44 62 2c 0a 2a 2a 20 75 6e 6c  base iDb,.** unl
238e3 69 6b 65 20 74 68 61 74 20 69 6e 64 65 78 20 66  ike that index f
238e4 72 6f 6d 20 69 74 73 20 54 61 62 6c 65 20 74 68  rom its Table th
238e5 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 69 6e  en remove the in
238e6 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  dex from.** the 
238e7 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c 65  index hash table
238e8 20 61 6e 64 20 66 72 65 65 20 61 6c 6c 20 6d 65   and free all me
238e9 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 20  mory structures 
238ea 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
238eb 74 68 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2f  th the index..*/
238ec 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
238ed 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69  void sqlite3Unli
238ee 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78  nkAndDeleteIndex
238ef 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
238f0 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61  t iDb, const cha
238f1 72 20 2a 7a 49 64 78 4e 61 6d 65 29 7b 0a 20 20  r *zIdxName){.  
238f2 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20  Index *pIndex;. 
238f3 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 48 61 73 68   int len;.  Hash
238f4 20 2a 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61   *pHash = &db->a
238f5 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
238f6 3e 69 64 78 48 61 73 68 3b 0a 0a 20 20 6c 65 6e  >idxHash;..  len
238f7 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
238f8 33 30 28 7a 49 64 78 4e 61 6d 65 29 3b 0a 20 20  30(zIdxName);.  
238f9 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
238fa 48 61 73 68 49 6e 73 65 72 74 28 70 48 61 73 68  HashInsert(pHash
238fb 2c 20 7a 49 64 78 4e 61 6d 65 2c 20 6c 65 6e 2c  , zIdxName, len,
238fc 20 30 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65   0);.  if( pInde
238fd 78 20 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e  x ){.    if( pIn
238fe 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e  dex->pTable->pIn
238ff 64 65 78 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20  dex==pIndex ){. 
23900 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 54 61       pIndex->pTa
23901 62 6c 65 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49  ble->pIndex = pI
23902 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ndex->pNext;.   
23903 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e   }else{.      In
23904 64 65 78 20 2a 70 3b 0a 20 20 20 20 20 20 2f 2a  dex *p;.      /*
23905 20 4a 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f   Justification o
23906 66 20 41 4c 57 41 59 53 28 29 3b 20 20 54 68 65  f ALWAYS();  The
23907 20 69 6e 64 65 78 20 6d 75 73 74 20 62 65 20 6f   index must be o
23908 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 0a 20 20  n the list of.  
23909 20 20 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20      ** indices. 
2390a 2a 2f 0a 20 20 20 20 20 20 70 20 3d 20 70 49 6e  */.      p = pIn
2390b 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e  dex->pTable->pIn
2390c 64 65 78 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  dex;.      while
2390d 28 20 41 4c 57 41 59 53 28 70 29 20 26 26 20 70  ( ALWAYS(p) && p
2390e 2d 3e 70 4e 65 78 74 21 3d 70 49 6e 64 65 78 20  ->pNext!=pIndex 
2390f 29 7b 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b  ){ p = p->pNext;
23910 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57   }.      if( ALW
23911 41 59 53 28 70 20 26 26 20 70 2d 3e 70 4e 65 78  AYS(p && p->pNex
23912 74 3d 3d 70 49 6e 64 65 78 29 20 29 7b 0a 20 20  t==pIndex) ){.  
23913 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d        p->pNext =
23914 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a   pIndex->pNext;.
23915 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
23916 20 20 66 72 65 65 49 6e 64 65 78 28 70 49 6e 64    freeIndex(pInd
23917 65 78 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66  ex);.  }.  db->f
23918 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49  lags |= SQLITE_I
23919 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a  nternChanges;.}.
2391a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c  ./*.** Erase all
2391b 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74   schema informat
2391c 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d  ion from the in-
2391d 6d 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62 6c  memory hash tabl
2391e 65 73 20 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c  es of.** a singl
2391f 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
23920 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
23921 6c 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d  led to reclaim m
23922 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20  emory.** before 
23923 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6c 6f  the database clo
23924 73 65 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f  ses.  It is also
23925 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61   called during a
23926 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20   rollback.** if 
23927 74 68 65 72 65 20 77 65 72 65 20 73 63 68 65 6d  there were schem
23928 61 20 63 68 61 6e 67 65 73 20 64 75 72 69 6e 67  a changes during
23929 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
2392a 20 6f 72 20 69 66 20 61 0a 2a 2a 20 73 63 68 65   or if a.** sche
2392b 6d 61 2d 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74  ma-cookie mismat
2392c 63 68 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a  ch occurs..**.**
2392d 20 49 66 20 69 44 62 3d 3d 30 20 74 68 65 6e 20   If iDb==0 then 
2392e 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 6e  reset the intern
2392f 61 6c 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73  al schema tables
23930 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73   for all databas
23931 65 0a 2a 2a 20 66 69 6c 65 73 2e 20 20 49 66 20  e.** files.  If 
23932 69 44 62 3e 3d 31 20 74 68 65 6e 20 72 65 73 65  iDb>=1 then rese
23933 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  t the internal s
23934 63 68 65 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74  chema for only t
23935 68 65 0a 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c  he.** single fil
23936 65 20 69 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a  e indicated..*/.
23937 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
23938 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74  oid sqlite3Reset
23939 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73  InternalSchema(s
2393a 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
2393b 69 44 62 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  iDb){.  int i, j
2393c 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ;.  assert( iDb>
2393d 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
2393e 62 20 29 3b 0a 0a 20 20 69 66 28 20 69 44 62 3d  b );..  if( iDb=
2393f 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
23940 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64  3BtreeEnterAll(d
23941 62 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  b);.  }.  for(i=
23942 69 44 62 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  iDb; i<db->nDb; 
23943 69 2b 2b 29 7b 0a 20 20 20 20 44 62 20 2a 70 44  i++){.    Db *pD
23944 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b  b = &db->aDb[i];
23945 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53  .    if( pDb->pS
23946 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 61  chema ){.      a
23947 73 73 65 72 74 28 69 3d 3d 31 20 7c 7c 20 28 70  ssert(i==1 || (p
23948 44 62 2d 3e 70 42 74 20 26 26 20 73 71 6c 69 74  Db->pBt && sqlit
23949 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
2394a 78 28 70 44 62 2d 3e 70 42 74 29 29 29 3b 0a 20  x(pDb->pBt)));. 
2394b 20 20 20 20 20 73 71 6c 69 74 65 33 53 63 68 65       sqlite3Sche
2394c 6d 61 46 72 65 65 28 70 44 62 2d 3e 70 53 63 68  maFree(pDb->pSch
2394d 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ema);.    }.    
2394e 69 66 28 20 69 44 62 3e 30 20 29 20 72 65 74 75  if( iDb>0 ) retu
2394f 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rn;.  }.  assert
23950 28 20 69 44 62 3d 3d 30 20 29 3b 0a 20 20 64 62  ( iDb==0 );.  db
23951 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49  ->flags &= ~SQLI
23952 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
23953 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 55  ;.  sqlite3VtabU
23954 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 29 3b 0a 20  nlockList(db);. 
23955 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
23956 76 65 41 6c 6c 28 64 62 29 3b 0a 0a 20 20 2f 2a  veAll(db);..  /*
23957 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   If one or more 
23958 6f 66 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  of the auxiliary
23959 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
2395a 68 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 2c  has been closed,
2395b 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 6d 6f 76  .  ** then remov
2395c 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20  e them from the 
2395d 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
2395e 73 65 20 6c 69 73 74 2e 20 20 57 65 20 74 61 6b  se list.  We tak
2395f 65 20 74 68 65 0a 20 20 2a 2a 20 6f 70 70 6f 72  e the.  ** oppor
23960 74 75 6e 69 74 79 20 74 6f 20 64 6f 20 74 68 69  tunity to do thi
23961 73 20 68 65 72 65 20 73 69 6e 63 65 20 77 65 20  s here since we 
23962 68 61 76 65 20 6a 75 73 74 20 64 65 6c 65 74 65  have just delete
23963 64 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 2a  d all of the.  *
23964 2a 20 73 63 68 65 6d 61 20 68 61 73 68 20 74 61  * schema hash ta
23965 62 6c 65 73 20 61 6e 64 20 74 68 65 72 65 66 6f  bles and therefo
23966 72 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74  re do not have t
23967 6f 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67  o make any chang
23968 65 73 0a 20 20 2a 2a 20 74 6f 20 61 6e 79 20 6f  es.  ** to any o
23969 66 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a  f those tables..
2396a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6a 3d 32    */.  for(i=j=2
2396b 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
2396c 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44 62  ){.    struct Db
2396d 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
2396e 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62  [i];.    if( pDb
2396f 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ->pBt==0 ){.    
23970 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23971 64 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b  db, pDb->zName);
23972 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d  .      pDb->zNam
23973 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e  e = 0;.      con
23974 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20  tinue;.    }.   
23975 20 69 66 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20   if( j<i ){.    
23976 20 20 64 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64    db->aDb[j] = d
23977 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d  b->aDb[i];.    }
23978 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20  .    j++;.  }.  
23979 6d 65 6d 73 65 74 28 26 64 62 2d 3e 61 44 62 5b  memset(&db->aDb[
2397a 6a 5d 2c 20 30 2c 20 28 64 62 2d 3e 6e 44 62 2d  j], 0, (db->nDb-
2397b 6a 29 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44  j)*sizeof(db->aD
2397c 62 5b 6a 5d 29 29 3b 0a 20 20 64 62 2d 3e 6e 44  b[j]));.  db->nD
2397d 62 20 3d 20 6a 3b 0a 20 20 69 66 28 20 64 62 2d  b = j;.  if( db-
2397e 3e 6e 44 62 3c 3d 32 20 26 26 20 64 62 2d 3e 61  >nDb<=2 && db->a
2397f 44 62 21 3d 64 62 2d 3e 61 44 62 53 74 61 74 69  Db!=db->aDbStati
23980 63 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  c ){.    memcpy(
23981 64 62 2d 3e 61 44 62 53 74 61 74 69 63 2c 20 64  db->aDbStatic, d
23982 62 2d 3e 61 44 62 2c 20 32 2a 73 69 7a 65 6f 66  b->aDb, 2*sizeof
23983 28 64 62 2d 3e 61 44 62 5b 30 5d 29 29 3b 0a 20  (db->aDb[0]));. 
23984 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
23985 28 64 62 2c 20 64 62 2d 3e 61 44 62 29 3b 0a 20  (db, db->aDb);. 
23986 20 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d     db->aDb = db-
23987 3e 61 44 62 53 74 61 74 69 63 3b 0a 20 20 7d 0a  >aDbStatic;.  }.
23988 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
23989 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
2398a 77 68 65 6e 20 61 20 63 6f 6d 6d 69 74 20 6f 63  when a commit oc
2398b 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  curs..*/.SQLITE_
2398c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2398d 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e  ite3CommitIntern
2398e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65  alChanges(sqlite
2398f 33 20 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 66 6c  3 *db){.  db->fl
23990 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49  ags &= ~SQLITE_I
23991 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a  nternChanges;.}.
23992 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
23993 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72   column names fr
23994 6f 6d 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69  om a table or vi
23995 65 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ew..*/.static vo
23996 69 64 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f  id sqliteResetCo
23997 6c 75 6d 6e 4e 61 6d 65 73 28 54 61 62 6c 65 20  lumnNames(Table 
23998 2a 70 54 61 62 6c 65 29 7b 0a 20 20 69 6e 74 20  *pTable){.  int 
23999 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  i;.  Column *pCo
2399a 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  l;.  sqlite3 *db
2399b 20 3d 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d   = pTable->dbMem
2399c 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 64 62  ;.  testcase( db
2399d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
2399e 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20   pTable!=0 );.  
2399f 69 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62  if( (pCol = pTab
239a0 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a  le->aCol)!=0 ){.
239a1 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
239a2 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  Table->nCol; i++
239a3 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
239a4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
239a5 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b  b, pCol->zName);
239a6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
239a7 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  prDelete(db, pCo
239a8 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20  l->pDflt);.     
239a9 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
239aa 62 2c 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 29 3b  b, pCol->zDflt);
239ab 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
239ac 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a  Free(db, pCol->z
239ad 54 79 70 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  Type);.      sql
239ae 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
239af 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20  Col->zColl);.   
239b0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
239b1 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d  Free(db, pTable-
239b2 3e 61 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 70 54  >aCol);.  }.  pT
239b3 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a  able->aCol = 0;.
239b4 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d    pTable->nCol =
239b5 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d   0;.}../*.** Rem
239b6 6f 76 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64  ove the memory d
239b7 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 61  ata structures a
239b8 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
239b9 68 65 20 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c  he given.** Tabl
239ba 65 2e 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61  e.  No changes a
239bb 72 65 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20  re made to disk 
239bc 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  by this routine.
239bd 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
239be 69 6e 65 20 6a 75 73 74 20 64 65 6c 65 74 65 73  ine just deletes
239bf 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74   the data struct
239c0 75 72 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f  ure.  It does no
239c1 74 20 75 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20  t unlink.** the 
239c2 74 61 62 6c 65 20 64 61 74 61 20 73 74 72 75 63  table data struc
239c3 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 68 61  ture from the ha
239c4 73 68 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69  sh table.  But i
239c5 74 20 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a  t does destroy.*
239c6 2a 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75  * memory structu
239c7 72 65 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63  res of the indic
239c8 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b  es and foreign k
239c9 65 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  eys associated w
239ca 69 74 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c  ith .** the tabl
239cb 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
239cc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
239cd 33 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62  3DeleteTable(Tab
239ce 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49  le *pTable){.  I
239cf 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70  ndex *pIndex, *p
239d0 4e 65 78 74 3b 0a 20 20 73 71 6c 69 74 65 33 20  Next;.  sqlite3 
239d1 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 54 61 62  *db;..  if( pTab
239d2 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  le==0 ) return;.
239d3 20 20 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 64    db = pTable->d
239d4 62 4d 65 6d 3b 0a 20 20 74 65 73 74 63 61 73 65  bMem;.  testcase
239d5 28 20 64 62 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a  ( db==0 );..  /*
239d6 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74   Do not delete t
239d7 68 65 20 74 61 62 6c 65 20 75 6e 74 69 6c 20 74  he table until t
239d8 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
239d9 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e  nt reaches zero.
239da 20 2a 2f 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52   */.  pTable->nR
239db 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 54 61 62  ef--;.  if( pTab
239dc 6c 65 2d 3e 6e 52 65 66 3e 30 20 29 7b 0a 20 20  le->nRef>0 ){.  
239dd 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
239de 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e  assert( pTable->
239df 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a  nRef==0 );..  /*
239e0 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 64 69   Delete all indi
239e1 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
239e2 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 0a 20  ith this table. 
239e3 20 2a 2f 0a 20 20 66 6f 72 28 70 49 6e 64 65 78   */.  for(pIndex
239e4 20 3d 20 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65   = pTable->pInde
239e5 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65  x; pIndex; pInde
239e6 78 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e  x=pNext){.    pN
239e7 65 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e  ext = pIndex->pN
239e8 65 78 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ext;.    assert(
239e9 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61   pIndex->pSchema
239ea 3d 3d 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d  ==pTable->pSchem
239eb 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  a );.    sqlite3
239ec 44 65 6c 65 74 65 49 6e 64 65 78 28 70 49 6e 64  DeleteIndex(pInd
239ed 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44  ex);.  }..  /* D
239ee 65 6c 65 74 65 20 61 6e 79 20 66 6f 72 65 69 67  elete any foreig
239ef 6e 20 6b 65 79 73 20 61 74 74 61 63 68 65 64 20  n keys attached 
239f0 74 6f 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a  to this table. *
239f1 2f 0a 20 20 73 71 6c 69 74 65 33 46 6b 44 65 6c  /.  sqlite3FkDel
239f2 65 74 65 28 70 54 61 62 6c 65 29 3b 0a 0a 20 20  ete(pTable);..  
239f3 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 54 61  /* Delete the Ta
239f4 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 74  ble structure it
239f5 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  self..  */.  sql
239f6 69 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61  iteResetColumnNa
239f7 6d 65 73 28 70 54 61 62 6c 65 29 3b 0a 20 20 73  mes(pTable);.  s
239f8 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
239f9 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b   pTable->zName);
239fa 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
239fb 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 43 6f  (db, pTable->zCo
239fc 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 74 65 33  lAff);.  sqlite3
239fd 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
239fe 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74   pTable->pSelect
239ff 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
23a00 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 73  E_OMIT_CHECK.  s
23a01 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
23a02 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 43 68  (db, pTable->pCh
23a03 65 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73  eck);.#endif.  s
23a04 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28  qlite3VtabClear(
23a05 70 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74  pTable);.  sqlit
23a06 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61  e3DbFree(db, pTa
23a07 62 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  ble);.}../*.** U
23a08 6e 6c 69 6e 6b 20 74 68 65 20 67 69 76 65 6e 20  nlink the given 
23a09 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 68  table from the h
23a0a 61 73 68 20 74 61 62 6c 65 73 20 61 6e 64 20 74  ash tables and t
23a0b 68 65 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a  he delete the.**
23a0c 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   table structure
23a0d 20 77 69 74 68 20 61 6c 6c 20 69 74 73 20 69 6e   with all its in
23a0e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65 69 67  dices and foreig
23a0f 6e 20 6b 65 79 73 2e 0a 2a 2f 0a 53 51 4c 49 54  n keys..*/.SQLIT
23a10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23a11 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
23a12 65 6c 65 74 65 54 61 62 6c 65 28 73 71 6c 69 74  eleteTable(sqlit
23a13 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c  e3 *db, int iDb,
23a14 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
23a15 62 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20  bName){.  Table 
23a16 2a 70 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 0a  *p;.  Db *pDb;..
23a17 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20    assert( db!=0 
23a18 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
23a19 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
23a1a 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
23a1b 7a 54 61 62 4e 61 6d 65 20 26 26 20 7a 54 61 62  zTabName && zTab
23a1c 4e 61 6d 65 5b 30 5d 20 29 3b 0a 20 20 70 44 62  Name[0] );.  pDb
23a1d 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
23a1e 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48  ;.  p = sqlite3H
23a1f 61 73 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e  ashInsert(&pDb->
23a20 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
23a21 2c 20 7a 54 61 62 4e 61 6d 65 2c 0a 20 20 20 20  , zTabName,.    
23a22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23a23 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65      sqlite3Strle
23a24 6e 33 30 28 7a 54 61 62 4e 61 6d 65 29 2c 30 29  n30(zTabName),0)
23a25 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74  ;.  sqlite3Delet
23a26 65 54 61 62 6c 65 28 70 29 3b 0a 20 20 64 62 2d  eTable(p);.  db-
23a27 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45  >flags |= SQLITE
23a28 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a  _InternChanges;.
23a29 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61  }../*.** Given a
23a2a 20 74 6f 6b 65 6e 2c 20 72 65 74 75 72 6e 20 61   token, return a
23a2b 20 73 74 72 69 6e 67 20 74 68 61 74 20 63 6f 6e   string that con
23a2c 73 69 73 74 73 20 6f 66 20 74 68 65 20 74 65 78  sists of the tex
23a2d 74 20 6f 66 20 74 68 61 74 0a 2a 2a 20 74 6f 6b  t of that.** tok
23a2e 65 6e 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f  en.  Space to ho
23a2f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  ld the returned 
23a30 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 6f 62 74  string.** is obt
23a31 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
23a32 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75  eMalloc() and mu
23a33 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74  st be freed by t
23a34 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75  he calling.** fu
23a35 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e  nction..**.** An
23a36 79 20 71 75 6f 74 61 74 69 6f 6e 20 6d 61 72 6b  y quotation mark
23a37 73 20 28 65 78 3a 20 20 22 6e 61 6d 65 22 2c 20  s (ex:  "name", 
23a38 27 6e 61 6d 65 27 2c 20 5b 6e 61 6d 65 5d 2c 20  'name', [name], 
23a39 6f 72 20 60 6e 61 6d 65 60 29 20 74 68 61 74 0a  or `name`) that.
23a3a 2a 2a 20 73 75 72 72 6f 75 6e 64 20 74 68 65 20  ** surround the 
23a3b 62 6f 64 79 20 6f 66 20 74 68 65 20 74 6f 6b 65  body of the toke
23a3c 6e 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a  n are removed..*
23a3d 2a 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20  *.** Tokens are 
23a3e 6f 66 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e 74  often just point
23a3f 65 72 73 20 69 6e 74 6f 20 74 68 65 20 6f 72 69  ers into the ori
23a40 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 61  ginal SQL text a
23a41 6e 64 20 73 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74  nd so.** are not
23a42 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64   \000 terminated
23a43 20 61 6e 64 20 61 72 65 20 6e 6f 74 20 70 65 72   and are not per
23a44 73 69 73 74 65 6e 74 2e 20 20 54 68 65 20 72 65  sistent.  The re
23a45 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a  turned string.**
23a46 20 69 73 20 5c 30 30 30 20 74 65 72 6d 69 6e 61   is \000 termina
23a47 74 65 64 20 61 6e 64 20 69 73 20 70 65 72 73 69  ted and is persi
23a48 73 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  stent..*/.SQLITE
23a49 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
23a4a 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
23a4b 6b 65 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ken(sqlite3 *db,
23a4c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a   Token *pName){.
23a4d 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20    char *zName;. 
23a4e 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20   if( pName ){.  
23a4f 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
23a50 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
23a51 63 68 61 72 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20  char*)pName->z, 
23a52 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 73  pName->n);.    s
23a53 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e  qlite3Dequote(zN
23a54 61 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ame);.  }else{. 
23a55 20 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20     zName = 0;.  
23a56 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65  }.  return zName
23a57 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
23a58 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  the sqlite_maste
23a59 72 20 74 61 62 6c 65 20 73 74 6f 72 65 64 20 69  r table stored i
23a5a 6e 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65  n database numbe
23a5b 72 20 69 44 62 20 66 6f 72 0a 2a 2a 20 77 72 69  r iDb for.** wri
23a5c 74 69 6e 67 2e 20 54 68 65 20 74 61 62 6c 65 20  ting. The table 
23a5d 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20  is opened using 
23a5e 63 75 72 73 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c  cursor 0..*/.SQL
23a5f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
23a60 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74   sqlite3OpenMast
23a61 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70  erTable(Parse *p
23a62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64  , int iDb){.  Vd
23a63 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
23a64 65 74 56 64 62 65 28 70 29 3b 0a 20 20 73 71 6c  etVdbe(p);.  sql
23a65 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  ite3TableLock(p,
23a66 20 69 44 62 2c 20 4d 41 53 54 45 52 5f 52 4f 4f   iDb, MASTER_ROO
23a67 54 2c 20 31 2c 20 53 43 48 45 4d 41 5f 54 41 42  T, 1, SCHEMA_TAB
23a68 4c 45 28 69 44 62 29 29 3b 0a 20 20 73 71 6c 69  LE(iDb));.  sqli
23a69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
23a6a 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30   OP_OpenWrite, 0
23a6b 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 69  , MASTER_ROOT, i
23a6c 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  Db);.  sqlite3Vd
23a6d 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
23a6e 2c 20 28 63 68 61 72 20 2a 29 35 2c 20 50 34 5f  , (char *)5, P4_
23a6f 49 4e 54 33 32 29 3b 20 20 2f 2a 20 35 20 63 6f  INT32);  /* 5 co
23a70 6c 75 6d 6e 20 74 61 62 6c 65 20 2a 2f 0a 20 20  lumn table */.  
23a71 69 66 28 20 70 2d 3e 6e 54 61 62 3d 3d 30 20 29  if( p->nTab==0 )
23a72 7b 0a 20 20 20 20 70 2d 3e 6e 54 61 62 20 3d 20  {.    p->nTab = 
23a73 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  1;.  }.}../*.** 
23a74 50 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20  Parameter zName 
23a75 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 2d  points to a nul-
23a76 74 65 72 6d 69 6e 61 74 65 64 20 62 75 66 66 65  terminated buffe
23a77 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  r containing the
23a78 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 64 61   name.** of a da
23a79 74 61 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20  tabase ("main", 
23a7a 22 74 65 6d 70 22 20 6f 72 20 74 68 65 20 6e 61  "temp" or the na
23a7b 6d 65 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65  me of an attache
23a7c 64 20 64 62 29 2e 20 54 68 69 73 0a 2a 2a 20 66  d db). This.** f
23a7d 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
23a7e 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
23a7f 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20   named database 
23a80 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72  in db->aDb[], or
23a81 0a 2a 2a 20 2d 31 20 69 66 20 74 68 65 20 6e 61  .** -1 if the na
23a82 6d 65 64 20 64 62 20 63 61 6e 6e 6f 74 20 62 65  med db cannot be
23a83 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54   found..*/.SQLIT
23a84 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
23a85 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28  lite3FindDbName(
23a86 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
23a87 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b  st char *zName){
23a88 0a 20 20 69 6e 74 20 69 20 3d 20 2d 31 3b 20 20  .  int i = -1;  
23a89 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
23a8a 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69  se number */.  i
23a8b 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  f( zName ){.    
23a8c 44 62 20 2a 70 44 62 3b 0a 20 20 20 20 69 6e 74  Db *pDb;.    int
23a8d 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
23a8e 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20  en30(zName);.   
23a8f 20 66 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d   for(i=(db->nDb-
23a90 31 29 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62  1), pDb=&db->aDb
23a91 5b 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20  [i]; i>=0; i--, 
23a92 70 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66  pDb--){.      if
23a93 28 20 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  ( (!OMIT_TEMPDB 
23a94 7c 7c 20 69 21 3d 31 20 29 20 26 26 20 6e 3d 3d  || i!=1 ) && n==
23a95 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
23a96 70 44 62 2d 3e 7a 4e 61 6d 65 29 20 26 26 20 0a  pDb->zName) && .
23a97 20 20 20 20 20 20 20 20 20 20 30 3d 3d 73 71 6c            0==sql
23a98 69 74 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d  ite3StrICmp(pDb-
23a99 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 20 29  >zName, zName) )
23a9a 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
23a9b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
23a9c 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d   }.  return i;.}
23a9d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 6f 6b 65  ../*.** The toke
23a9e 6e 20 2a 70 4e 61 6d 65 20 63 6f 6e 74 61 69 6e  n *pName contain
23a9f 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  s the name of a 
23aa0 64 61 74 61 62 61 73 65 20 28 65 69 74 68 65 72  database (either
23aa1 20 22 6d 61 69 6e 22 20 6f 72 0a 2a 2a 20 22 74   "main" or.** "t
23aa2 65 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65  emp" or the name
23aa3 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20   of an attached 
23aa4 64 62 29 2e 20 54 68 69 73 20 72 6f 75 74 69 6e  db). This routin
23aa5 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a  e returns the.**
23aa6 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61   index of the na
23aa7 6d 65 64 20 64 61 74 61 62 61 73 65 20 69 6e 20  med database in 
23aa8 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 20 2d 31  db->aDb[], or -1
23aa9 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 64 62   if the named db
23aaa 20 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78   .** does not ex
23aab 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ist..*/.SQLITE_P
23aac 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
23aad 65 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33  e3FindDb(sqlite3
23aae 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61   *db, Token *pNa
23aaf 6d 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  me){.  int i;   
23ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23ab1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
23ab2 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a  atabase number *
23ab3 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  /.  char *zName;
23ab4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23ab5 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
23ab6 20 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e   we are searchin
23ab7 67 20 66 6f 72 20 2a 2f 0a 20 20 7a 4e 61 6d 65  g for */.  zName
23ab8 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
23ab9 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d  omToken(db, pNam
23aba 65 29 3b 0a 20 20 69 20 3d 20 73 71 6c 69 74 65  e);.  i = sqlite
23abb 33 46 69 6e 64 44 62 4e 61 6d 65 28 64 62 2c 20  3FindDbName(db, 
23abc 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  zName);.  sqlite
23abd 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d  3DbFree(db, zNam
23abe 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a  e);.  return i;.
23abf 7d 0a 0a 2f 2a 20 54 68 65 20 74 61 62 6c 65 20  }../* The table 
23ac0 6f 72 20 76 69 65 77 20 6f 72 20 74 72 69 67 67  or view or trigg
23ac1 65 72 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65  er name is passe
23ac2 64 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  d to this routin
23ac3 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a 2a 2a 20  e via tokens.** 
23ac4 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65  pName1 and pName
23ac5 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  2. If the table 
23ac6 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71  name was fully q
23ac7 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78  ualified, for ex
23ac8 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 45  ample:.**.** CRE
23ac9 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79  ATE TABLE xxx.yy
23aca 79 20 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20  y (...);.** .** 
23acb 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73  Then pName1 is s
23acc 65 74 20 74 6f 20 22 78 78 78 22 20 61 6e 64 20  et to "xxx" and 
23acd 70 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e  pName2 "yyy". On
23ace 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 20   the other hand 
23acf 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  if.** the table 
23ad0 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c  name is not full
23ad1 79 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e 65  y qualified, i.e
23ad2 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20  .:.**.** CREATE 
23ad3 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a  TABLE yyy(...);.
23ad4 2a 2a 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65  **.** Then pName
23ad5 31 20 69 73 20 73 65 74 20 74 6f 20 22 79 79 79  1 is set to "yyy
23ad6 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 20  " and pName2 is 
23ad7 22 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ""..**.** This r
23ad8 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
23ad9 2a 70 70 55 6e 71 75 61 6c 20 70 6f 69 6e 74 65  *ppUnqual pointe
23ada 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  r to point at th
23adb 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20  e token (pName1 
23adc 6f 72 0a 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68  or.** pName2) th
23add 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 75 6e  at stores the un
23ade 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20  qualified table 
23adf 6e 61 6d 65 2e 20 20 54 68 65 20 69 6e 64 65 78  name.  The index
23ae0 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   of the.** datab
23ae1 61 73 65 20 22 78 78 78 22 20 69 73 20 72 65 74  ase "xxx" is ret
23ae2 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
23ae3 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
23ae4 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28  ite3TwoPartName(
23ae5 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
23ae6 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  ,      /* Parsin
23ae7 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  g and code gener
23ae8 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
23ae9 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31  .  Token *pName1
23aea 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 78  ,      /* The "x
23aeb 78 78 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20  xx" in the name 
23aec 22 78 78 78 2e 79 79 79 22 20 6f 72 20 22 78 78  "xxx.yyy" or "xx
23aed 78 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  x" */.  Token *p
23aee 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20 54  Name2,      /* T
23aef 68 65 20 22 79 79 79 22 20 69 6e 20 74 68 65 20  he "yyy" in the 
23af0 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20 2a  name "xxx.yyy" *
23af1 2f 0a 20 20 54 6f 6b 65 6e 20 2a 2a 70 55 6e 71  /.  Token **pUnq
23af2 75 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 65  ual     /* Write
23af3 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64   the unqualified
23af4 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 68 65 72   object name her
23af5 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44  e */.){.  int iD
23af6 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
23af7 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
23af8 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6f 62  e holding the ob
23af9 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ject */.  sqlite
23afa 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
23afb 64 62 3b 0a 0a 20 20 69 66 28 20 41 4c 57 41 59  db;..  if( ALWAY
23afc 53 28 70 4e 61 6d 65 32 21 3d 30 29 20 26 26 20  S(pName2!=0) && 
23afd 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20  pName2->n>0 ){. 
23afe 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e     if( db->init.
23aff 62 75 73 79 20 29 20 7b 0a 20 20 20 20 20 20 73  busy ) {.      s
23b00 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
23b01 50 61 72 73 65 2c 20 22 63 6f 72 72 75 70 74 20  Parse, "corrupt 
23b02 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20 20  database");.    
23b03 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
23b04 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d  ;.      return -
23b05 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 55  1;.    }.    *pU
23b06 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65 32 3b 0a  nqual = pName2;.
23b07 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
23b08 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d  3FindDb(db, pNam
23b09 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62  e1);.    if( iDb
23b0a 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  <0 ){.      sqli
23b0b 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
23b0c 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 74  se, "unknown dat
23b0d 61 62 61 73 65 20 25 54 22 2c 20 70 4e 61 6d 65  abase %T", pName
23b0e 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  1);.      pParse
23b0f 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20  ->nErr++;.      
23b10 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d  return -1;.    }
23b11 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
23b12 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 69  sert( db->init.i
23b13 44 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e 69  Db==0 || db->ini
23b14 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 69 44  t.busy );.    iD
23b15 62 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62  b = db->init.iDb
23b16 3b 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d  ;.    *pUnqual =
23b17 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 0a 20 20 72   pName1;.  }.  r
23b18 65 74 75 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a  eturn iDb;.}../*
23b19 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
23b1a 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 63   is used to chec
23b1b 6b 20 69 66 20 74 68 65 20 55 54 46 2d 38 20 73  k if the UTF-8 s
23b1c 74 72 69 6e 67 20 7a 4e 61 6d 65 20 69 73 20 61  tring zName is a
23b1d 20 6c 65 67 61 6c 0a 2a 2a 20 75 6e 71 75 61 6c   legal.** unqual
23b1e 69 66 69 65 64 20 6e 61 6d 65 20 66 6f 72 20 61  ified name for a
23b1f 20 6e 65 77 20 73 63 68 65 6d 61 20 6f 62 6a 65   new schema obje
23b20 63 74 20 28 74 61 62 6c 65 2c 20 69 6e 64 65 78  ct (table, index
23b21 2c 20 76 69 65 77 20 6f 72 0a 2a 2a 20 74 72 69  , view or.** tri
23b22 67 67 65 72 29 2e 20 41 6c 6c 20 6e 61 6d 65 73  gger). All names
23b23 20 61 72 65 20 6c 65 67 61 6c 20 65 78 63 65 70   are legal excep
23b24 74 20 74 68 6f 73 65 20 74 68 61 74 20 62 65 67  t those that beg
23b25 69 6e 20 77 69 74 68 20 74 68 65 20 73 74 72 69  in with the stri
23b26 6e 67 0a 2a 2a 20 22 73 71 6c 69 74 65 5f 22 20  ng.** "sqlite_" 
23b27 28 69 6e 20 75 70 70 65 72 2c 20 6c 6f 77 65 72  (in upper, lower
23b28 20 6f 72 20 6d 69 78 65 64 20 63 61 73 65 29 2e   or mixed case).
23b29 20 54 68 69 73 20 70 6f 72 74 69 6f 6e 20 6f 66   This portion of
23b2a 20 74 68 65 20 6e 61 6d 65 73 70 61 63 65 0a 2a   the namespace.*
23b2b 2a 20 69 73 20 72 65 73 65 72 76 65 64 20 66 6f  * is reserved fo
23b2c 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 2e 0a  r internal use..
23b2d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23b2e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65  E int sqlite3Che
23b2f 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72  ckObjectName(Par
23b30 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
23b31 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a  t char *zName){.
23b32 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 64    if( !pParse->d
23b33 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20  b->init.busy && 
23b34 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
23b35 30 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  0 .          && 
23b36 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61  (pParse->db->fla
23b37 67 73 20 26 20 53 51 4c 49 54 45 5f 57 72 69 74  gs & SQLITE_Writ
23b38 65 53 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20  eSchema)==0.    
23b39 20 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c 69        && 0==sqli
23b3a 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61 6d  te3StrNICmp(zNam
23b3b 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29  e, "sqlite_", 7)
23b3c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
23b3d 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
23b3e 22 6f 62 6a 65 63 74 20 6e 61 6d 65 20 72 65 73  "object name res
23b3f 65 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e  erved for intern
23b40 61 6c 20 75 73 65 3a 20 25 73 22 2c 20 7a 4e 61  al use: %s", zNa
23b41 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  me);.    return 
23b42 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
23b43 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
23b44 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42  E_OK;.}../*.** B
23b45 65 67 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e  egin constructin
23b46 67 20 61 20 6e 65 77 20 74 61 62 6c 65 20 72 65  g a new table re
23b47 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e 20  presentation in 
23b48 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73  memory.  This is
23b49 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f 66  .** the first of
23b4a 20 73 65 76 65 72 61 6c 20 61 63 74 69 6f 6e 20   several action 
23b4b 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 67 65  routines that ge
23b4c 74 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 70  t called in resp
23b4d 6f 6e 73 65 0a 2a 2a 20 74 6f 20 61 20 43 52 45  onse.** to a CRE
23b4e 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
23b4f 65 6e 74 2e 20 20 49 6e 20 70 61 72 74 69 63 75  ent.  In particu
23b50 6c 61 72 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  lar, this routin
23b51 65 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61  e is called.** a
23b52 66 74 65 72 20 73 65 65 69 6e 67 20 74 6f 6b 65  fter seeing toke
23b53 6e 73 20 22 43 52 45 41 54 45 22 20 61 6e 64 20  ns "CREATE" and 
23b54 22 54 41 42 4c 45 22 20 61 6e 64 20 74 68 65 20  "TABLE" and the 
23b55 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20  table name. The 
23b56 69 73 54 65 6d 70 0a 2a 2a 20 66 6c 61 67 20 69  isTemp.** flag i
23b57 73 20 74 72 75 65 20 69 66 20 74 68 65 20 74 61  s true if the ta
23b58 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 73 74  ble should be st
23b59 6f 72 65 64 20 69 6e 20 74 68 65 20 61 75 78 69  ored in the auxi
23b5a 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a  liary database.*
23b5b 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f  * file instead o
23b5c 66 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  f in the main da
23b5d 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68  tabase file.  Th
23b5e 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 74  is is normally t
23b5f 68 65 20 63 61 73 65 0a 2a 2a 20 77 68 65 6e 20  he case.** when 
23b60 74 68 65 20 22 54 45 4d 50 22 20 6f 72 20 22 54  the "TEMP" or "T
23b61 45 4d 50 4f 52 41 52 59 22 20 6b 65 79 77 6f 72  EMPORARY" keywor
23b62 64 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77  d occurs in betw
23b63 65 65 6e 0a 2a 2a 20 43 52 45 41 54 45 20 61 6e  een.** CREATE an
23b64 64 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54  d TABLE..**.** T
23b65 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63  he new table rec
23b66 6f 72 64 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  ord is initializ
23b67 65 64 20 61 6e 64 20 70 75 74 20 69 6e 20 70 50  ed and put in pP
23b68 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2e  arse->pNewTable.
23b69 0a 2a 2a 20 41 73 20 6d 6f 72 65 20 6f 66 20 74  .** As more of t
23b6a 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
23b6b 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72  statement is par
23b6c 73 65 64 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  sed, additional 
23b6d 61 63 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e  action.** routin
23b6e 65 73 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65  es will be calle
23b6f 64 20 74 6f 20 61 64 64 20 6d 6f 72 65 20 69 6e  d to add more in
23b70 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 69  formation to thi
23b71 73 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 41 74 20  s record..** At 
23b72 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43  the end of the C
23b73 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
23b74 65 6d 65 6e 74 2c 20 74 68 65 20 73 71 6c 69 74  ement, the sqlit
23b75 65 33 45 6e 64 54 61 62 6c 65 28 29 20 72 6f 75  e3EndTable() rou
23b76 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65  tine.** is calle
23b77 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  d to complete th
23b78 65 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 6f  e construction o
23b79 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  f the new table 
23b7a 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54  record..*/.SQLIT
23b7b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23b7c 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65  qlite3StartTable
23b7d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
23b7e 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63  e,   /* Parser c
23b7f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65  ontext */.  Toke
23b80 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a 20  n *pName1,   /* 
23b81 46 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68  First part of th
23b82 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
23b83 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20  ble or view */. 
23b84 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20   Token *pName2, 
23b85 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74    /* Second part
23b86 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   of the name of 
23b87 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65  the table or vie
23b88 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d  w */.  int isTem
23b89 70 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  p,      /* True 
23b8a 69 66 20 74 68 69 73 20 69 73 20 61 20 54 45 4d  if this is a TEM
23b8b 50 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  P table */.  int
23b8c 20 69 73 56 69 65 77 2c 20 20 20 20 20 20 2f 2a   isView,      /*
23b8d 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73   True if this is
23b8e 20 61 20 56 49 45 57 20 2a 2f 0a 20 20 69 6e 74   a VIEW */.  int
23b8f 20 69 73 56 69 72 74 75 61 6c 2c 20 20 20 2f 2a   isVirtual,   /*
23b90 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73   True if this is
23b91 20 61 20 56 49 52 54 55 41 4c 20 74 61 62 6c 65   a VIRTUAL table
23b92 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20   */.  int noErr 
23b93 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74         /* Do not
23b94 68 69 6e 67 20 69 66 20 74 61 62 6c 65 20 61 6c  hing if table al
23b95 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a  ready exists */.
23b96 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  ){.  Table *pTab
23b97 6c 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  le;.  char *zNam
23b98 65 20 3d 20 30 3b 20 2f 2a 20 54 68 65 20 6e 61  e = 0; /* The na
23b99 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61  me of the new ta
23b9a 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ble */.  sqlite3
23b9b 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
23b9c 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  b;.  Vdbe *v;.  
23b9d 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
23b9e 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d   /* Database num
23b9f 62 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68  ber to create th
23ba0 65 20 74 61 62 6c 65 20 69 6e 20 2a 2f 0a 20 20  e table in */.  
23ba1 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20  Token *pName;   
23ba2 20 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 20   /* Unqualified 
23ba3 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
23ba4 65 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 0a  e to create */..
23ba5 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f    /* The table o
23ba6 72 20 76 69 65 77 20 6e 61 6d 65 20 74 6f 20 63  r view name to c
23ba7 72 65 61 74 65 20 69 73 20 70 61 73 73 65 64 20  reate is passed 
23ba8 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
23ba9 76 69 61 20 74 6f 6b 65 6e 73 0a 20 20 2a 2a 20  via tokens.  ** 
23baa 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65  pName1 and pName
23bab 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  2. If the table 
23bac 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71  name was fully q
23bad 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78  ualified, for ex
23bae 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ample:.  **.  **
23baf 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 78   CREATE TABLE xx
23bb0 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 20 20 2a  x.yyy (...);.  *
23bb1 2a 20 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61  * .  ** Then pNa
23bb2 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 78  me1 is set to "x
23bb3 78 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 22  xx" and pName2 "
23bb4 79 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68  yyy". On the oth
23bb5 65 72 20 68 61 6e 64 20 69 66 0a 20 20 2a 2a 20  er hand if.  ** 
23bb6 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  the table name i
23bb7 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61 6c  s not fully qual
23bb8 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 20 20 2a  ified, i.e.:.  *
23bb9 2a 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41  *.  ** CREATE TA
23bba 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 20 20  BLE yyy(...);.  
23bbb 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61  **.  ** Then pNa
23bbc 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 79  me1 is set to "y
23bbd 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69  yy" and pName2 i
23bbe 73 20 22 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  s ""..  **.  ** 
23bbf 54 68 65 20 63 61 6c 6c 20 62 65 6c 6f 77 20 73  The call below s
23bc0 65 74 73 20 74 68 65 20 70 4e 61 6d 65 20 70 6f  ets the pName po
23bc1 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61  inter to point a
23bc2 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61  t the token (pNa
23bc3 6d 65 31 20 6f 72 0a 20 20 2a 2a 20 70 4e 61 6d  me1 or.  ** pNam
23bc4 65 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 20  e2) that stores 
23bc5 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20  the unqualified 
23bc6 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20  table name. The 
23bc7 76 61 72 69 61 62 6c 65 20 69 44 62 20 69 73 0a  variable iDb is.
23bc8 20 20 2a 2a 20 73 65 74 20 74 6f 20 74 68 65 20    ** set to the 
23bc9 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  index of the dat
23bca 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20 74  abase that the t
23bcb 61 62 6c 65 20 6f 72 20 76 69 65 77 20 69 73 20  able or view is 
23bcc 74 6f 20 62 65 0a 20 20 2a 2a 20 63 72 65 61 74  to be.  ** creat
23bcd 65 64 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 44  ed in..  */.  iD
23bce 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61  b = sqlite3TwoPa
23bcf 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
23bd0 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26  Name1, pName2, &
23bd1 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 44  pName);.  if( iD
23bd2 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  b<0 ) return;.  
23bd3 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42  if( !OMIT_TEMPDB
23bd4 20 26 26 20 69 73 54 65 6d 70 20 26 26 20 69 44   && isTemp && iD
23bd5 62 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  b>1 ){.    /* If
23bd6 20 63 72 65 61 74 69 6e 67 20 61 20 74 65 6d 70   creating a temp
23bd7 20 74 61 62 6c 65 2c 20 74 68 65 20 6e 61 6d 65   table, the name
23bd8 20 6d 61 79 20 6e 6f 74 20 62 65 20 71 75 61 6c   may not be qual
23bd9 69 66 69 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c  ified */.    sql
23bda 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
23bdb 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20  rse, "temporary 
23bdc 74 61 62 6c 65 20 6e 61 6d 65 20 6d 75 73 74 20  table name must 
23bdd 62 65 20 75 6e 71 75 61 6c 69 66 69 65 64 22 29  be unqualified")
23bde 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
23bdf 7d 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45  }.  if( !OMIT_TE
23be0 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29  MPDB && isTemp )
23be1 20 69 44 62 20 3d 20 31 3b 0a 0a 20 20 70 50 61   iDb = 1;..  pPa
23be2 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20  rse->sNameToken 
23be3 3d 20 2a 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d  = *pName;.  zNam
23be4 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
23be5 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61  romToken(db, pNa
23be6 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65  me);.  if( zName
23be7 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
23be8 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
23be9 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63  qlite3CheckObjec
23bea 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e  tName(pParse, zN
23beb 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ame) ){.    goto
23bec 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
23bed 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62  or;.  }.  if( db
23bee 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29 20  ->init.iDb==1 ) 
23bef 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 69 66 6e  isTemp = 1;.#ifn
23bf0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23bf1 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
23bf2 61 73 73 65 72 74 28 20 28 69 73 54 65 6d 70 20  assert( (isTemp 
23bf3 26 20 31 29 3d 3d 69 73 54 65 6d 70 20 29 3b 0a  & 1)==isTemp );.
23bf4 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65    {.    int code
23bf5 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 62 20  ;.    char *zDb 
23bf6 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
23bf7 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 71  Name;.    if( sq
23bf8 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
23bf9 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e  Parse, SQLITE_IN
23bfa 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42  SERT, SCHEMA_TAB
23bfb 4c 45 28 69 73 54 65 6d 70 29 2c 20 30 2c 20 7a  LE(isTemp), 0, z
23bfc 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  Db) ){.      got
23bfd 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72  o begin_table_er
23bfe 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ror;.    }.    i
23bff 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20  f( isView ){.   
23c00 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
23c01 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b  PDB && isTemp ){
23c02 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
23c03 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
23c04 4d 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d  MP_VIEW;.      }
23c05 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f  else{.        co
23c06 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41  de = SQLITE_CREA
23c07 54 45 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d  TE_VIEW;.      }
23c08 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
23c09 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
23c0a 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a  DB && isTemp ){.
23c0b 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
23c0c 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
23c0d 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d  P_TABLE;.      }
23c0e 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f  else{.        co
23c0f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41  de = SQLITE_CREA
23c10 54 45 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20  TE_TABLE;.      
23c11 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
23c12 21 69 73 56 69 72 74 75 61 6c 20 26 26 20 73 71  !isVirtual && sq
23c13 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
23c14 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e 61  Parse, code, zNa
23c15 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20  me, 0, zDb) ){. 
23c16 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f       goto begin_
23c17 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20  table_error;.   
23c18 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
23c19 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
23c1a 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65  e new table name
23c1b 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69 64   does not collid
23c1c 65 20 77 69 74 68 20 61 6e 20 65 78 69 73 74 69  e with an existi
23c1d 6e 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72  ng.  ** index or
23c1e 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74   table name in t
23c1f 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
23c20 2e 20 20 49 73 73 75 65 20 61 6e 20 65 72 72 6f  .  Issue an erro
23c21 72 20 6d 65 73 73 61 67 65 20 69 66 0a 20 20 2a  r message if.  *
23c22 2a 20 69 74 20 64 6f 65 73 2e 20 54 68 65 20 65  * it does. The e
23c23 78 63 65 70 74 69 6f 6e 20 69 73 20 69 66 20 74  xception is if t
23c24 68 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69  he statement bei
23c25 6e 67 20 70 61 72 73 65 64 20 77 61 73 20 70 61  ng parsed was pa
23c26 73 73 65 64 0a 20 20 2a 2a 20 74 6f 20 61 6e 20  ssed.  ** to an 
23c27 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
23c28 76 74 61 62 28 29 20 63 61 6c 6c 2e 20 49 6e 20  vtab() call. In 
23c29 74 68 61 74 20 63 61 73 65 20 6f 6e 6c 79 20 74  that case only t
23c2a 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a  he column names.
23c2b 20 20 2a 2a 20 61 6e 64 20 74 79 70 65 73 20 77    ** and types w
23c2c 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 73 6f 20  ill be used, so 
23c2d 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
23c2e 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6e 61 6d   to test for nam
23c2f 65 73 70 61 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c  espace.  ** coll
23c30 69 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  isions..  */.  i
23c31 66 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56  f( !IN_DECLARE_V
23c32 54 41 42 20 29 7b 0a 20 20 20 20 69 66 28 20 53  TAB ){.    if( S
23c33 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
23c34 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
23c35 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  se) ){.      got
23c36 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72  o begin_table_er
23c37 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ror;.    }.    p
23c38 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 46  Table = sqlite3F
23c39 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61  indTable(db, zNa
23c3a 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  me, db->aDb[iDb]
23c3b 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28  .zName);.    if(
23c3c 20 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20   pTable ){.     
23c3d 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20   if( !noErr ){. 
23c3e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
23c3f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23c40 74 61 62 6c 65 20 25 54 20 61 6c 72 65 61 64 79  table %T already
23c41 20 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65 29   exists", pName)
23c42 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
23c43 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65  goto begin_table
23c44 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20  _error;.    }.  
23c45 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
23c46 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65  dIndex(db, zName
23c47 2c 20 30 29 21 3d 30 20 26 26 20 28 69 44 62 3d  , 0)!=0 && (iDb=
23c48 3d 30 20 7c 7c 20 21 64 62 2d 3e 69 6e 69 74 2e  =0 || !db->init.
23c49 62 75 73 79 29 20 29 7b 0a 20 20 20 20 20 20 73  busy) ){.      s
23c4a 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
23c4b 50 61 72 73 65 2c 20 22 74 68 65 72 65 20 69 73  Parse, "there is
23c4c 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 65   already an inde
23c4d 78 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e 61  x named %s", zNa
23c4e 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  me);.      goto 
23c4f 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
23c50 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  r;.    }.  }..  
23c51 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  pTable = sqlite3
23c52 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
23c53 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b   sizeof(Table));
23c54 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30  .  if( pTable==0
23c55 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c   ){.    db->mall
23c56 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
23c57 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
23c58 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
23c59 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b   pParse->nErr++;
23c5a 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f  .    goto begin_
23c5b 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d  table_error;.  }
23c5c 0a 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65  .  pTable->zName
23c5d 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62   = zName;.  pTab
23c5e 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a  le->iPKey = -1;.
23c5f 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d    pTable->pSchem
23c60 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  a = db->aDb[iDb]
23c61 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 61 62  .pSchema;.  pTab
23c62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  le->nRef = 1;.  
23c63 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 20 3d 20  pTable->dbMem = 
23c64 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  0;.  assert( pPa
23c65 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d  rse->pNewTable==
23c66 30 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70  0 );.  pParse->p
23c67 4e 65 77 54 61 62 6c 65 20 3d 20 70 54 61 62 6c  NewTable = pTabl
23c68 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  e;..  /* If this
23c69 20 69 73 20 74 68 65 20 6d 61 67 69 63 20 73 71   is the magic sq
23c6a 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
23c6b 62 6c 65 20 75 73 65 64 20 62 79 20 61 75 74 6f  ble used by auto
23c6c 69 6e 63 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20  increment,.  ** 
23c6d 74 68 65 6e 20 72 65 63 6f 72 64 20 61 20 70 6f  then record a po
23c6e 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 74 61  inter to this ta
23c6f 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20  ble in the main 
23c70 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75  database structu
23c71 72 65 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20  re.  ** so that 
23c72 49 4e 53 45 52 54 20 63 61 6e 20 66 69 6e 64 20  INSERT can find 
23c73 74 68 65 20 74 61 62 6c 65 20 65 61 73 69 6c 79  the table easily
23c74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
23c75 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
23c76 4e 43 52 45 4d 45 4e 54 0a 20 20 69 66 28 20 21  NCREMENT.  if( !
23c77 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26  pParse->nested &
23c78 26 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20  & strcmp(zName, 
23c79 22 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65  "sqlite_sequence
23c7a 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 61  ")==0 ){.    pTa
23c7b 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53  ble->pSchema->pS
23c7c 65 71 54 61 62 20 3d 20 70 54 61 62 6c 65 3b 0a  eqTab = pTable;.
23c7d 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
23c7e 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e   Begin generatin
23c7f 67 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20  g the code that 
23c80 77 69 6c 6c 20 69 6e 73 65 72 74 20 74 68 65 20  will insert the 
23c81 74 61 62 6c 65 20 72 65 63 6f 72 64 20 69 6e 74  table record int
23c82 6f 0a 20 20 2a 2a 20 74 68 65 20 53 51 4c 49 54  o.  ** the SQLIT
23c83 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20  E_MASTER table. 
23c84 20 4e 6f 74 65 20 69 6e 20 70 61 72 74 69 63 75   Note in particu
23c85 6c 61 72 20 74 68 61 74 20 77 65 20 6d 75 73 74  lar that we must
23c86 20 67 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20 61   go ahead.  ** a
23c87 6e 64 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20  nd allocate the 
23c88 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f  record number fo
23c89 72 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72  r the table entr
23c8a 79 20 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20 61  y now.  Before a
23c8b 6e 79 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59 20  ny.  ** PRIMARY 
23c8c 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 6b 65  KEY or UNIQUE ke
23c8d 79 77 6f 72 64 73 20 61 72 65 20 70 61 72 73 65  ywords are parse
23c8e 64 2e 20 20 54 68 6f 73 65 20 6b 65 79 77 6f 72  d.  Those keywor
23c8f 64 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 20 20  ds will cause.  
23c90 2a 2a 20 69 6e 64 69 63 65 73 20 74 6f 20 62 65  ** indices to be
23c91 20 63 72 65 61 74 65 64 20 61 6e 64 20 74 68 65   created and the
23c92 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 6d 75   table record mu
23c93 73 74 20 63 6f 6d 65 20 62 65 66 6f 72 65 20 74  st come before t
23c94 68 65 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73  he .  ** indices
23c95 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 72 65  .  Hence, the re
23c96 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20  cord number for 
23c97 74 68 65 20 74 61 62 6c 65 20 6d 75 73 74 20 62  the table must b
23c98 65 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a 2a  e allocated.  **
23c99 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   now..  */.  if(
23c9a 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
23c9b 26 26 20 28 76 20 3d 20 73 71 6c 69 74 65 33 47  && (v = sqlite3G
23c9c 65 74 56 64 62 65 28 70 50 61 72 73 65 29 29 21  etVdbe(pParse))!
23c9d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31  =0 ){.    int j1
23c9e 3b 0a 20 20 20 20 69 6e 74 20 66 69 6c 65 46 6f  ;.    int fileFo
23c9f 72 6d 61 74 3b 0a 20 20 20 20 69 6e 74 20 72 65  rmat;.    int re
23ca0 67 31 2c 20 72 65 67 32 2c 20 72 65 67 33 3b 0a  g1, reg2, reg3;.
23ca1 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
23ca2 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
23ca3 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a  Parse, 0, iDb);.
23ca4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
23ca5 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
23ca6 45 0a 20 20 20 20 69 66 28 20 69 73 56 69 72 74  E.    if( isVirt
23ca7 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ual ){.      sql
23ca8 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
23ca9 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20  , OP_VBegin);.  
23caa 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
23cab 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 66  /* If the file f
23cac 6f 72 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64 69  ormat and encodi
23cad 6e 67 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ng in the databa
23cae 73 65 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e  se have not been
23caf 20 73 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73 65   set, .    ** se
23cb0 74 20 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20 20  t them now..    
23cb1 2a 2f 0a 20 20 20 20 72 65 67 31 20 3d 20 70 50  */.    reg1 = pP
23cb2 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 3d  arse->regRowid =
23cb3 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
23cb4 0a 20 20 20 20 72 65 67 32 20 3d 20 70 50 61 72  .    reg2 = pPar
23cb5 73 65 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b 2b  se->regRoot = ++
23cb6 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
23cb7 20 20 72 65 67 33 20 3d 20 2b 2b 70 50 61 72 73    reg3 = ++pPars
23cb8 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
23cb9 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
23cba 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c  , OP_ReadCookie,
23cbb 20 69 44 62 2c 20 72 65 67 33 2c 20 42 54 52 45   iDb, reg3, BTRE
23cbc 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 29 3b 0a  E_FILE_FORMAT);.
23cbd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55      sqlite3VdbeU
23cbe 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29  sesBtree(v, iDb)
23cbf 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74  ;.    j1 = sqlit
23cc0 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
23cc1 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b 0a 20 20  OP_If, reg3);.  
23cc2 20 20 66 69 6c 65 46 6f 72 6d 61 74 20 3d 20 28    fileFormat = (
23cc3 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
23cc4 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74  TE_LegacyFileFmt
23cc5 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 20 20 20  )!=0 ?.         
23cc6 20 20 20 20 20 20 20 20 20 31 20 3a 20 53 51 4c           1 : SQL
23cc7 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52  ITE_MAX_FILE_FOR
23cc8 4d 41 54 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  MAT;.    sqlite3
23cc9 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
23cca 5f 49 6e 74 65 67 65 72 2c 20 66 69 6c 65 46 6f  _Integer, fileFo
23ccb 72 6d 61 74 2c 20 72 65 67 33 29 3b 0a 20 20 20  rmat, reg3);.   
23ccc 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23ccd 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b  p3(v, OP_SetCook
23cce 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 46  ie, iDb, BTREE_F
23ccf 49 4c 45 5f 46 4f 52 4d 41 54 2c 20 72 65 67 33  ILE_FORMAT, reg3
23cd0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23cd1 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
23cd2 6e 74 65 67 65 72 2c 20 45 4e 43 28 64 62 29 2c  nteger, ENC(db),
23cd3 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69   reg3);.    sqli
23cd4 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
23cd5 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69   OP_SetCookie, i
23cd6 44 62 2c 20 42 54 52 45 45 5f 54 45 58 54 5f 45  Db, BTREE_TEXT_E
23cd7 4e 43 4f 44 49 4e 47 2c 20 72 65 67 33 29 3b 0a  NCODING, reg3);.
23cd8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
23cd9 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a  umpHere(v, j1);.
23cda 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 6a 75 73  .    /* This jus
23cdb 74 20 63 72 65 61 74 65 73 20 61 20 70 6c 61 63  t creates a plac
23cdc 65 2d 68 6f 6c 64 65 72 20 72 65 63 6f 72 64 20  e-holder record 
23cdd 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  in the sqlite_ma
23cde 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 20 20  ster table..    
23cdf 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 63 72  ** The record cr
23ce0 65 61 74 65 64 20 64 6f 65 73 20 6e 6f 74 20 63  eated does not c
23ce1 6f 6e 74 61 69 6e 20 61 6e 79 74 68 69 6e 67 20  ontain anything 
23ce2 79 65 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65  yet.  It will be
23ce3 20 72 65 70 6c 61 63 65 64 0a 20 20 20 20 2a 2a   replaced.    **
23ce4 20 62 79 20 74 68 65 20 72 65 61 6c 20 65 6e 74   by the real ent
23ce5 72 79 20 69 6e 20 63 6f 64 65 20 67 65 6e 65 72  ry in code gener
23ce6 61 74 65 64 20 61 74 20 73 71 6c 69 74 65 33 45  ated at sqlite3E
23ce7 6e 64 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a  ndTable()..    *
23ce8 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77  *.    ** The row
23ce9 69 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65  id for the new e
23cea 6e 74 72 79 20 69 73 20 6c 65 66 74 20 69 6e 20  ntry is left in 
23ceb 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65 2d  register pParse-
23cec 3e 72 65 67 52 6f 77 69 64 2e 0a 20 20 20 20 2a  >regRowid..    *
23ced 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20  * The root page 
23cee 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
23cef 77 20 74 61 62 6c 65 20 69 73 20 6c 65 66 74 20  w table is left 
23cf0 69 6e 20 72 65 67 20 70 50 61 72 73 65 2d 3e 72  in reg pParse->r
23cf1 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a 2a 20 54  egRoot..    ** T
23cf2 68 65 20 72 6f 77 69 64 20 61 6e 64 20 72 6f 6f  he rowid and roo
23cf3 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 76 61  t page number va
23cf4 6c 75 65 73 20 61 72 65 20 6e 65 65 64 65 64 20  lues are needed 
23cf5 62 79 20 74 68 65 20 63 6f 64 65 20 74 68 61 74  by the code that
23cf6 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 45  .    ** sqlite3E
23cf7 6e 64 54 61 62 6c 65 20 77 69 6c 6c 20 67 65 6e  ndTable will gen
23cf8 65 72 61 74 65 2e 0a 20 20 20 20 2a 2f 0a 23 69  erate..    */.#i
23cf9 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
23cfa 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
23cfb 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
23cfc 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
23cfd 45 29 0a 20 20 20 20 69 66 28 20 69 73 56 69 65  E).    if( isVie
23cfe 77 20 7c 7c 20 69 73 56 69 72 74 75 61 6c 20 29  w || isVirtual )
23cff 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
23d00 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
23d01 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 32  Integer, 0, reg2
23d02 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e  );.    }else.#en
23d03 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  dif.    {.      
23d04 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23d05 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 54 61  2(v, OP_CreateTa
23d06 62 6c 65 2c 20 69 44 62 2c 20 72 65 67 32 29 3b  ble, iDb, reg2);
23d07 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
23d08 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c  e3OpenMasterTabl
23d09 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
23d0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23d0b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
23d0c 6f 77 69 64 2c 20 30 2c 20 72 65 67 31 29 3b 0a  owid, 0, reg1);.
23d0d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23d0e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
23d0f 2c 20 30 2c 20 72 65 67 33 29 3b 0a 20 20 20 20  , 0, reg3);.    
23d10 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23d11 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
23d12 30 2c 20 72 65 67 33 2c 20 72 65 67 31 29 3b 0a  0, reg3, reg1);.
23d13 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
23d14 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
23d15 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73  G_APPEND);.    s
23d16 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
23d17 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20  (v, OP_Close);. 
23d18 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20   }..  /* Normal 
23d19 28 6e 6f 6e 2d 65 72 72 6f 72 29 20 72 65 74 75  (non-error) retu
23d1a 72 6e 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3b  rn. */.  return;
23d1b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72  ..  /* If an err
23d1c 6f 72 20 6f 63 63 75 72 73 2c 20 77 65 20 6a 75  or occurs, we ju
23d1d 6d 70 20 68 65 72 65 20 2a 2f 0a 62 65 67 69 6e  mp here */.begin
23d1e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3a 0a 20 20  _table_error:.  
23d1f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
23d20 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75  , zName);.  retu
23d21 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rn;.}../*.** Thi
23d22 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20  s macro is used 
23d23 74 6f 20 63 6f 6d 70 61 72 65 20 74 77 6f 20 73  to compare two s
23d24 74 72 69 6e 67 73 20 69 6e 20 61 20 63 61 73 65  trings in a case
23d25 2d 69 6e 73 65 6e 73 69 74 69 76 65 20 6d 61 6e  -insensitive man
23d26 6e 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 73 6c  ner..** It is sl
23d27 69 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68  ightly faster th
23d28 61 6e 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  an calling sqlit
23d29 65 33 53 74 72 49 43 6d 70 28 29 20 64 69 72 65  e3StrICmp() dire
23d2a 63 74 6c 79 2c 20 62 75 74 0a 2a 2a 20 70 72 6f  ctly, but.** pro
23d2b 64 75 63 65 73 20 6c 61 72 67 65 72 20 63 6f 64  duces larger cod
23d2c 65 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47  e..**.** WARNING
23d2d 3a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20  : This macro is 
23d2e 6e 6f 74 20 63 6f 6d 70 61 74 69 62 6c 65 20 77  not compatible w
23d2f 69 74 68 20 74 68 65 20 73 74 72 63 6d 70 28 29  ith the strcmp()
23d30 20 66 61 6d 69 6c 79 2e 20 49 74 0a 2a 2a 20 72   family. It.** r
23d31 65 74 75 72 6e 73 20 74 72 75 65 20 69 66 20 74  eturns true if t
23d32 68 65 20 74 77 6f 20 73 74 72 69 6e 67 73 20 61  he two strings a
23d33 72 65 20 65 71 75 61 6c 2c 20 6f 74 68 65 72 77  re equal, otherw
23d34 69 73 65 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 64  ise false..*/.#d
23d35 65 66 69 6e 65 20 53 54 52 49 43 4d 50 28 78 2c  efine STRICMP(x,
23d36 20 79 29 20 28 5c 0a 73 71 6c 69 74 65 33 55 70   y) (\.sqlite3Up
23d37 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73  perToLower[*(uns
23d38 69 67 6e 65 64 20 63 68 61 72 20 2a 29 28 78 29  igned char *)(x)
23d39 5d 3d 3d 20 20 20 5c 0a 73 71 6c 69 74 65 33 55  ]==   \.sqlite3U
23d3a 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e  pperToLower[*(un
23d3b 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 28 79  signed char *)(y
23d3c 29 5d 20 20 20 20 20 5c 0a 26 26 20 73 71 6c 69  )]     \.&& sqli
23d3d 74 65 33 53 74 72 49 43 6d 70 28 28 78 29 2b 31  te3StrICmp((x)+1
23d3e 2c 28 79 29 2b 31 29 3d 3d 30 20 29 0a 0a 2f 2a  ,(y)+1)==0 )../*
23d3f 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 63 6f  .** Add a new co
23d40 6c 75 6d 6e 20 74 6f 20 74 68 65 20 74 61 62 6c  lumn to the tabl
23d41 65 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e  e currently bein
23d42 67 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a  g constructed..*
23d43 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20  *.** The parser 
23d44 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
23d45 6e 65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ne once for each
23d46 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74   column declarat
23d47 69 6f 6e 0a 2a 2a 20 69 6e 20 61 20 43 52 45 41  ion.** in a CREA
23d48 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
23d49 6e 74 2e 20 20 73 71 6c 69 74 65 33 53 74 61 72  nt.  sqlite3Star
23d4a 74 54 61 62 6c 65 28 29 20 67 65 74 73 20 63 61  tTable() gets ca
23d4b 6c 6c 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 6f  lled.** first to
23d4c 20 67 65 74 20 74 68 69 6e 67 73 20 67 6f 69 6e   get things goin
23d4d 67 2e 20 20 54 68 65 6e 20 74 68 69 73 20 72 6f  g.  Then this ro
23d4e 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
23d4f 66 6f 72 20 65 61 63 68 0a 2a 2a 20 63 6f 6c 75  for each.** colu
23d50 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  mn..*/.SQLITE_PR
23d51 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
23d52 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73  e3AddColumn(Pars
23d53 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
23d54 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c   *pName){.  Tabl
23d55 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  e *p;.  int i;. 
23d56 20 63 68 61 72 20 2a 7a 3b 0a 20 20 43 6f 6c 75   char *z;.  Colu
23d57 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69  mn *pCol;.  sqli
23d58 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
23d59 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 70 20 3d  ->db;.  if( (p =
23d5a 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
23d5b 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  le)==0 ) return;
23d5c 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
23d5d 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e  COLUMN.  if( p->
23d5e 6e 43 6f 6c 2b 31 3e 64 62 2d 3e 61 4c 69 6d 69  nCol+1>db->aLimi
23d5f 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  t[SQLITE_LIMIT_C
23d60 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71  OLUMN] ){.    sq
23d61 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
23d62 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
23d63 63 6f 6c 75 6d 6e 73 20 6f 6e 20 25 73 22 2c 20  columns on %s", 
23d64 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72  p->zName);.    r
23d65 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69  eturn;.  }.#endi
23d66 66 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e  f.  z = sqlite3N
23d67 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
23d68 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a   pName);.  if( z
23d69 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
23d6a 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43  for(i=0; i<p->nC
23d6b 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  ol; i++){.    if
23d6c 28 20 53 54 52 49 43 4d 50 28 7a 2c 20 70 2d 3e  ( STRICMP(z, p->
23d6d 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29  aCol[i].zName) )
23d6e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
23d6f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
23d70 22 64 75 70 6c 69 63 61 74 65 20 63 6f 6c 75 6d  "duplicate colum
23d71 6e 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 29 3b  n name: %s", z);
23d72 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
23d73 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20  Free(db, z);.   
23d74 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
23d75 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 6e  .  }.  if( (p->n
23d76 43 6f 6c 20 26 20 30 78 37 29 3d 3d 30 20 29 7b  Col & 0x7)==0 ){
23d77 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65  .    Column *aNe
23d78 77 3b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71  w;.    aNew = sq
23d79 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64  lite3DbRealloc(d
23d7a 62 2c 70 2d 3e 61 43 6f 6c 2c 28 70 2d 3e 6e 43  b,p->aCol,(p->nC
23d7b 6f 6c 2b 38 29 2a 73 69 7a 65 6f 66 28 70 2d 3e  ol+8)*sizeof(p->
23d7c 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 69  aCol[0]));.    i
23d7d 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  f( aNew==0 ){.  
23d7e 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
23d7f 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20  e(db, z);.      
23d80 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
23d81 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 61 4e 65 77    p->aCol = aNew
23d82 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 20 3d 20 26  ;.  }.  pCol = &
23d83 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d  p->aCol[p->nCol]
23d84 3b 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 6c 2c  ;.  memset(pCol,
23d85 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 61 43   0, sizeof(p->aC
23d86 6f 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 6f 6c 2d  ol[0]));.  pCol-
23d87 3e 7a 4e 61 6d 65 20 3d 20 7a 3b 0a 20 0a 20 20  >zName = z;. .  
23d88 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
23d89 6f 20 74 79 70 65 20 73 70 65 63 69 66 69 65 64  o type specified
23d8a 2c 20 63 6f 6c 75 6d 6e 73 20 68 61 76 65 20 74  , columns have t
23d8b 68 65 20 64 65 66 61 75 6c 74 20 61 66 66 69 6e  he default affin
23d8c 69 74 79 0a 20 20 2a 2a 20 27 4e 4f 4e 45 27 2e  ity.  ** 'NONE'.
23d8d 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74   If there is a t
23d8e 79 70 65 20 73 70 65 63 69 66 69 65 64 2c 20 74  ype specified, t
23d8f 68 65 6e 20 73 71 6c 69 74 65 33 41 64 64 43 6f  hen sqlite3AddCo
23d90 6c 75 6d 6e 54 79 70 65 28 29 20 77 69 6c 6c 0a  lumnType() will.
23d91 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6e    ** be called n
23d92 65 78 74 20 74 6f 20 73 65 74 20 70 43 6f 6c 2d  ext to set pCol-
23d93 3e 61 66 66 69 6e 69 74 79 20 63 6f 72 72 65 63  >affinity correc
23d94 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c  tly..  */.  pCol
23d95 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c  ->affinity = SQL
23d96 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
23d97 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a  p->nCol++;.}../*
23d98 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
23d99 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
23d9a 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69  e parser while i
23d9b 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a  n the middle of.
23d9c 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45  ** parsing a CRE
23d9d 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
23d9e 65 6e 74 2e 20 20 41 20 22 4e 4f 54 20 4e 55 4c  ent.  A "NOT NUL
23d9f 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 61  L" constraint ha
23da0 73 0a 2a 2a 20 62 65 65 6e 20 73 65 65 6e 20 6f  s.** been seen o
23da1 6e 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 69  n a column.  Thi
23da2 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
23da3 68 65 20 6e 6f 74 4e 75 6c 6c 20 66 6c 61 67 20  he notNull flag 
23da4 6f 6e 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  on.** the column
23da5 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
23da6 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a   construction..*
23da7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23da8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
23da9 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70  NotNull(Parse *p
23daa 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72  Parse, int onErr
23dab 6f 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  or){.  Table *p;
23dac 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  .  p = pParse->p
23dad 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20  NewTable;.  if( 
23dae 70 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d  p==0 || NEVER(p-
23daf 3e 6e 43 6f 6c 3c 31 29 20 29 20 72 65 74 75 72  >nCol<1) ) retur
23db0 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c 5b 70 2d 3e  n;.  p->aCol[p->
23db1 6e 43 6f 6c 2d 31 5d 2e 6e 6f 74 4e 75 6c 6c 20  nCol-1].notNull 
23db2 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 7d  = (u8)onError;.}
23db3 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 65  ../*.** Scan the
23db4 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6e 61 6d   column type nam
23db5 65 20 7a 54 79 70 65 20 28 6c 65 6e 67 74 68 20  e zType (length 
23db6 6e 54 79 70 65 29 20 61 6e 64 20 72 65 74 75 72  nType) and retur
23db7 6e 20 74 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61  n the.** associa
23db8 74 65 64 20 61 66 66 69 6e 69 74 79 20 74 79 70  ted affinity typ
23db9 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
23dba 75 74 69 6e 65 20 64 6f 65 73 20 61 20 63 61 73  utine does a cas
23dbb 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 65  e-independent se
23dbc 61 72 63 68 20 6f 66 20 7a 54 79 70 65 20 66 6f  arch of zType fo
23dbd 72 20 74 68 65 20 0a 2a 2a 20 73 75 62 73 74 72  r the .** substr
23dbe 69 6e 67 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c  ings in the foll
23dbf 6f 77 69 6e 67 20 74 61 62 6c 65 2e 20 49 66 20  owing table. If 
23dc0 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74  one of the subst
23dc1 72 69 6e 67 73 20 69 73 0a 2a 2a 20 66 6f 75 6e  rings is.** foun
23dc2 64 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  d, the correspon
23dc3 64 69 6e 67 20 61 66 66 69 6e 69 74 79 20 69 73  ding affinity is
23dc4 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 7a 54   returned. If zT
23dc5 79 70 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  ype contains.** 
23dc6 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66  more than one of
23dc7 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 2c   the substrings,
23dc8 20 65 6e 74 72 69 65 73 20 74 6f 77 61 72 64 20   entries toward 
23dc9 74 68 65 20 74 6f 70 20 6f 66 20 0a 2a 2a 20 74  the top of .** t
23dca 68 65 20 74 61 62 6c 65 20 74 61 6b 65 20 70 72  he table take pr
23dcb 69 6f 72 69 74 79 2e 20 46 6f 72 20 65 78 61 6d  iority. For exam
23dcc 70 6c 65 2c 20 69 66 20 7a 54 79 70 65 20 69 73  ple, if zType is
23dcd 20 27 42 4c 4f 42 49 4e 54 27 2c 20 0a 2a 2a 20   'BLOBINT', .** 
23dce 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
23dcf 45 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  ER is returned..
23dd0 2a 2a 0a 2a 2a 20 53 75 62 73 74 72 69 6e 67 20  **.** Substring 
23dd1 20 20 20 20 7c 20 41 66 66 69 6e 69 74 79 0a 2a      | Affinity.*
23dd2 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
23dd3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
23dd4 2d 2d 0a 2a 2a 20 27 49 4e 54 27 20 20 20 20 20  --.** 'INT'     
23dd5 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46      | SQLITE_AFF
23dd6 5f 49 4e 54 45 47 45 52 0a 2a 2a 20 27 43 48 41  _INTEGER.** 'CHA
23dd7 52 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49  R'        | SQLI
23dd8 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27  TE_AFF_TEXT.** '
23dd9 43 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53  CLOB'        | S
23dda 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a  QLITE_AFF_TEXT.*
23ddb 2a 20 27 54 45 58 54 27 20 20 20 20 20 20 20 20  * 'TEXT'        
23ddc 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  | SQLITE_AFF_TEX
23ddd 54 0a 2a 2a 20 27 42 4c 4f 42 27 20 20 20 20 20  T.** 'BLOB'     
23dde 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
23ddf 4e 4f 4e 45 0a 2a 2a 20 27 52 45 41 4c 27 20 20  NONE.** 'REAL'  
23de0 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
23de1 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 46 4c 4f 41  FF_REAL.** 'FLOA
23de2 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54  '        | SQLIT
23de3 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 44  E_AFF_REAL.** 'D
23de4 4f 55 42 27 20 20 20 20 20 20 20 20 7c 20 53 51  OUB'        | SQ
23de5 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a  LITE_AFF_REAL.**
23de6 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74  .** If none of t
23de7 68 65 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e  he substrings in
23de8 20 74 68 65 20 61 62 6f 76 65 20 74 61 62 6c 65   the above table
23de9 20 61 72 65 20 66 6f 75 6e 64 2c 0a 2a 2a 20 53   are found,.** S
23dea 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
23deb 43 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  C is returned..*
23dec 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23ded 20 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 66   char sqlite3Aff
23dee 69 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 20  inityType(const 
23def 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 33  char *zIn){.  u3
23df0 32 20 68 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  2 h = 0;.  char 
23df1 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
23df2 5f 4e 55 4d 45 52 49 43 3b 0a 0a 20 20 69 66 28  _NUMERIC;..  if(
23df3 20 7a 49 6e 20 29 20 77 68 69 6c 65 28 20 7a 49   zIn ) while( zI
23df4 6e 5b 30 5d 20 29 7b 0a 20 20 20 20 68 20 3d 20  n[0] ){.    h = 
23df5 28 68 3c 3c 38 29 20 2b 20 73 71 6c 69 74 65 33  (h<<8) + sqlite3
23df6 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 2a 7a  UpperToLower[(*z
23df7 49 6e 29 26 30 78 66 66 5d 3b 0a 20 20 20 20 7a  In)&0xff];.    z
23df8 49 6e 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3d  In++;.    if( h=
23df9 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 68 27  =(('c'<<24)+('h'
23dfa 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27  <<16)+('a'<<8)+'
23dfb 72 27 29 20 29 7b 20 20 20 20 20 20 20 20 20 20  r') ){          
23dfc 20 20 20 2f 2a 20 43 48 41 52 20 2a 2f 0a 20 20     /* CHAR */.  
23dfd 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
23dfe 5f 41 46 46 5f 54 45 58 54 3b 20 0a 20 20 20 20  _AFF_TEXT; .    
23dff 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27  }else if( h==(('
23e00 63 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36  c'<<24)+('l'<<16
23e01 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20  )+('o'<<8)+'b') 
23e02 29 7b 20 20 20 20 20 20 20 2f 2a 20 43 4c 4f 42  ){       /* CLOB
23e03 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20   */.      aff = 
23e04 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b  SQLITE_AFF_TEXT;
23e05 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68  .    }else if( h
23e06 3d 3d 28 28 27 74 27 3c 3c 32 34 29 2b 28 27 65  ==(('t'<<24)+('e
23e07 27 3c 3c 31 36 29 2b 28 27 78 27 3c 3c 38 29 2b  '<<16)+('x'<<8)+
23e08 27 74 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a  't') ){       /*
23e09 20 54 45 58 54 20 2a 2f 0a 20 20 20 20 20 20 61   TEXT */.      a
23e0a 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
23e0b 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20  TEXT;.    }else 
23e0c 69 66 28 20 68 3d 3d 28 28 27 62 27 3c 3c 32 34  if( h==(('b'<<24
23e0d 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27  )+('l'<<16)+('o'
23e0e 3c 3c 38 29 2b 27 62 27 29 20 20 20 20 20 20 20  <<8)+'b')       
23e0f 20 20 20 2f 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20     /* BLOB */.  
23e10 20 20 20 20 20 20 26 26 20 28 61 66 66 3d 3d 53        && (aff==S
23e11 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
23e12 43 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45  C || aff==SQLITE
23e13 5f 41 46 46 5f 52 45 41 4c 29 20 29 7b 0a 20 20  _AFF_REAL) ){.  
23e14 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
23e15 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 23 69 66 6e 64  _AFF_NONE;.#ifnd
23e16 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
23e17 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
23e18 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28    }else if( h==(
23e19 28 27 72 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c  ('r'<<24)+('e'<<
23e1a 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 6c 27  16)+('a'<<8)+'l'
23e1b 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45  )          /* RE
23e1c 41 4c 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26  AL */.        &&
23e1d 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46   aff==SQLITE_AFF
23e1e 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20  _NUMERIC ){.    
23e1f 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
23e20 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c  FF_REAL;.    }el
23e21 73 65 20 69 66 28 20 68 3d 3d 28 28 27 66 27 3c  se if( h==(('f'<
23e22 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28  <24)+('l'<<16)+(
23e23 27 6f 27 3c 3c 38 29 2b 27 61 27 29 20 20 20 20  'o'<<8)+'a')    
23e24 20 20 20 20 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f        /* FLOA */
23e25 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 3d  .        && aff=
23e26 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
23e27 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66  RIC ){.      aff
23e28 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45   = SQLITE_AFF_RE
23e29 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  AL;.    }else if
23e2a 28 20 68 3d 3d 28 28 27 64 27 3c 3c 32 34 29 2b  ( h==(('d'<<24)+
23e2b 28 27 6f 27 3c 3c 31 36 29 2b 28 27 75 27 3c 3c  ('o'<<16)+('u'<<
23e2c 38 29 2b 27 62 27 29 20 20 20 20 20 20 20 20 20  8)+'b')         
23e2d 20 2f 2a 20 44 4f 55 42 20 2a 2f 0a 20 20 20 20   /* DOUB */.    
23e2e 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49      && aff==SQLI
23e2f 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29  TE_AFF_NUMERIC )
23e30 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51  {.      aff = SQ
23e31 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 23  LITE_AFF_REAL;.#
23e32 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 20  endif.    }else 
23e33 69 66 28 20 28 68 26 30 78 30 30 46 46 46 46 46  if( (h&0x00FFFFF
23e34 46 29 3d 3d 28 28 27 69 27 3c 3c 31 36 29 2b 28  F)==(('i'<<16)+(
23e35 27 6e 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20  'n'<<8)+'t') ){ 
23e36 20 20 20 2f 2a 20 49 4e 54 20 2a 2f 0a 20 20 20     /* INT */.   
23e37 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
23e38 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
23e39 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
23e3a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 61 66    }..  return af
23e3b 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  f;.}../*.** This
23e3c 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
23e3d 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
23e3e 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69   while in the mi
23e3f 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69  ddle of.** parsi
23e40 6e 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  ng a CREATE TABL
23e41 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  E statement.  Th
23e42 65 20 70 46 69 72 73 74 20 74 6f 6b 65 6e 20 69  e pFirst token i
23e43 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74  s the first.** t
23e44 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73 65 71 75  oken in the sequ
23e45 65 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20 74  ence of tokens t
23e46 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65  hat describe the
23e47 20 74 79 70 65 20 6f 66 20 74 68 65 0a 2a 2a 20   type of the.** 
23e48 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79  column currently
23e49 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
23e4a 69 6f 6e 2e 20 20 20 70 4c 61 73 74 20 69 73 20  ion.   pLast is 
23e4b 74 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a  the last token.*
23e4c 2a 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63  * in the sequenc
23e4d 65 2e 20 20 55 73 65 20 74 68 69 73 20 69 6e 66  e.  Use this inf
23e4e 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 73  ormation to cons
23e4f 74 72 75 63 74 20 61 20 73 74 72 69 6e 67 0a 2a  truct a string.*
23e50 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  * that contains 
23e51 74 68 65 20 74 79 70 65 6e 61 6d 65 20 6f 66 20  the typename of 
23e52 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 73  the column and s
23e53 74 6f 72 65 20 74 68 61 74 20 73 74 72 69 6e 67  tore that string
23e54 0a 2a 2a 20 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f  .** in zType..*/
23e55 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45   .SQLITE_PRIVATE
23e56 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
23e57 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65  ColumnType(Parse
23e58 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
23e59 2a 70 54 79 70 65 29 7b 0a 20 20 54 61 62 6c 65  *pType){.  Table
23e5a 20 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70   *p;.  Column *p
23e5b 43 6f 6c 3b 0a 0a 20 20 70 20 3d 20 70 50 61 72  Col;..  p = pPar
23e5c 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
23e5d 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56   if( p==0 || NEV
23e5e 45 52 28 70 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20  ER(p->nCol<1) ) 
23e5f 72 65 74 75 72 6e 3b 0a 20 20 70 43 6f 6c 20 3d  return;.  pCol =
23e60 20 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f   &p->aCol[p->nCo
23e61 6c 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  l-1];.  assert( 
23e62 70 43 6f 6c 2d 3e 7a 54 79 70 65 3d 3d 30 20 29  pCol->zType==0 )
23e63 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20  ;.  pCol->zType 
23e64 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
23e65 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64  mToken(pParse->d
23e66 62 2c 20 70 54 79 70 65 29 3b 0a 20 20 70 43 6f  b, pType);.  pCo
23e67 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73 71  l->affinity = sq
23e68 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70  lite3AffinityTyp
23e69 65 28 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a  e(pCol->zType);.
23e6a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 65 78 70  }../*.** The exp
23e6b 72 65 73 73 69 6f 6e 20 69 73 20 74 68 65 20 64  ression is the d
23e6c 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
23e6d 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
23e6e 6c 79 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 0a  ly added column.
23e6f 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ** of the table 
23e70 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20  currently under 
23e71 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a  construction..**
23e72 0a 2a 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75  .** Default valu
23e73 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6d 75  e expressions mu
23e74 73 74 20 62 65 20 63 6f 6e 73 74 61 6e 74 2e 20  st be constant. 
23e75 20 52 61 69 73 65 20 61 6e 20 65 78 63 65 70 74   Raise an except
23e76 69 6f 6e 20 69 66 20 74 68 69 73 0a 2a 2a 20 69  ion if this.** i
23e77 73 20 6e 6f 74 20 74 68 65 20 63 61 73 65 2e 0a  s not the case..
23e78 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
23e79 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  ne is called by 
23e7a 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c 65  the parser while
23e7b 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
23e7c 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43  f.** parsing a C
23e7d 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
23e7e 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ement..*/.SQLITE
23e7f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23e80 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56  lite3AddDefaultV
23e81 61 6c 75 65 28 50 61 72 73 65 20 2a 70 50 61 72  alue(Parse *pPar
23e82 73 65 2c 20 45 78 70 72 53 70 61 6e 20 2a 70 53  se, ExprSpan *pS
23e83 70 61 6e 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  pan){.  Table *p
23e84 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c  ;.  Column *pCol
23e85 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
23e86 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
23e87 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  p = pParse->pNew
23e88 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 21 3d  Table;.  if( p!=
23e89 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 20 3d 20  0 ){.    pCol = 
23e8a 26 28 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f  &(p->aCol[p->nCo
23e8b 6c 2d 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 21  l-1]);.    if( !
23e8c 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
23e8d 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28  stantOrFunction(
23e8e 70 53 70 61 6e 2d 3e 70 45 78 70 72 29 20 29 7b  pSpan->pExpr) ){
23e8f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
23e90 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23e91 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66  default value of
23e92 20 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20 69 73 20   column [%s] is 
23e93 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 22 2c 0a 20  not constant",. 
23e94 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a           pCol->z
23e95 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Name);.    }else
23e96 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 70  {.      /* A cop
23e97 79 20 6f 66 20 70 45 78 70 72 20 69 73 20 75 73  y of pExpr is us
23e98 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  ed instead of th
23e99 65 20 6f 72 69 67 69 6e 61 6c 2c 20 61 73 20 70  e original, as p
23e9a 45 78 70 72 20 63 6f 6e 74 61 69 6e 73 0a 20 20  Expr contains.  
23e9b 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 73 20 74 68      ** tokens th
23e9c 61 74 20 70 6f 69 6e 74 20 74 6f 20 76 6f 6c 61  at point to vola
23e9d 74 69 6c 65 20 6d 65 6d 6f 72 79 2e 20 54 68 65  tile memory. The
23e9e 20 27 73 70 61 6e 27 20 6f 66 20 74 68 65 20 65   'span' of the e
23e9f 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20  xpression.      
23ea0 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 62  ** is required b
23ea1 79 20 70 72 61 67 6d 61 20 74 61 62 6c 65 5f 69  y pragma table_i
23ea2 6e 66 6f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  nfo..      */.  
23ea3 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
23ea4 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e  elete(db, pCol->
23ea5 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43  pDflt);.      pC
23ea6 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 73 71 6c 69  ol->pDflt = sqli
23ea7 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
23ea8 53 70 61 6e 2d 3e 70 45 78 70 72 2c 20 45 58 50  Span->pExpr, EXP
23ea9 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20  RDUP_REDUCE);.  
23eaa 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
23eab 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 44 66 6c  e(db, pCol->zDfl
23eac 74 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e  t);.      pCol->
23ead 7a 44 66 6c 74 20 3d 20 73 71 6c 69 74 65 33 44  zDflt = sqlite3D
23eae 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68  bStrNDup(db, (ch
23eaf 61 72 2a 29 70 53 70 61 6e 2d 3e 7a 53 74 61 72  ar*)pSpan->zStar
23eb0 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
23eb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23eb2 20 20 20 20 20 20 20 20 28 69 6e 74 29 28 70 53          (int)(pS
23eb3 70 61 6e 2d 3e 7a 45 6e 64 20 2d 20 70 53 70 61  pan->zEnd - pSpa
23eb4 6e 2d 3e 7a 53 74 61 72 74 29 29 3b 0a 20 20 20  n->zStart));.   
23eb5 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
23eb6 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
23eb7 53 70 61 6e 2d 3e 70 45 78 70 72 29 3b 0a 7d 0a  Span->pExpr);.}.
23eb8 0a 2f 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65  ./*.** Designate
23eb9 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
23eba 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20   for the table. 
23ebb 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74   pList is a list
23ebc 20 6f 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66   of names .** of
23ebd 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f   columns that fo
23ebe 72 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  rm the primary k
23ebf 65 79 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73  ey.  If pList is
23ec0 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a   NULL, then the.
23ec1 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  ** most recently
23ec2 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66   added column of
23ec3 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68   the table is th
23ec4 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a  e primary key..*
23ec5 2a 0a 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e  *.** A table can
23ec6 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e   have at most on
23ec7 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20  e primary key.  
23ec8 49 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72  If the table alr
23ec9 65 61 64 79 20 68 61 73 0a 2a 2a 20 61 20 70 72  eady has.** a pr
23eca 69 6d 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74  imary key (and t
23ecb 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e  his is the secon
23ecc 64 20 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74  d primary key) t
23ecd 68 65 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a  hen create an.**
23ece 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66   error..**.** If
23ecf 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
23ed0 20 69 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20   is on a single 
23ed1 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61 74  column whose dat
23ed2 61 74 79 70 65 20 69 73 20 49 4e 54 45 47 45 52  atype is INTEGER
23ed3 2c 0a 2a 2a 20 74 68 65 6e 20 77 65 20 77 69 6c  ,.** then we wil
23ed4 6c 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 61  l try to use tha
23ed5 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20  t column as the 
23ed6 72 6f 77 69 64 2e 20 20 53 65 74 20 74 68 65 20  rowid.  Set the 
23ed7 54 61 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66  Table.iPKey.** f
23ed8 69 65 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c  ield of the tabl
23ed9 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  e under construc
23eda 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 69  tion to be the i
23edb 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49  ndex of the.** I
23edc 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
23edd 45 59 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c  EY column.  Tabl
23ede 65 2e 69 50 4b 65 79 20 69 73 20 73 65 74 20 74  e.iPKey is set t
23edf 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73  o -1 if there is
23ee0 0a 2a 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20 50  .** no INTEGER P
23ee1 52 49 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a  RIMARY KEY..**.*
23ee2 2a 20 49 66 20 74 68 65 20 6b 65 79 20 69 73 20  * If the key is 
23ee3 6e 6f 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50  not an INTEGER P
23ee4 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e  RIMARY KEY, then
23ee5 20 63 72 65 61 74 65 20 61 20 75 6e 69 71 75 65   create a unique
23ee6 0a 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68  .** index for th
23ee7 65 20 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78  e key.  No index
23ee8 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20   is created for 
23ee9 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
23eea 4b 45 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  KEYs..*/.SQLITE_
23eeb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23eec 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
23eed 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  y(.  Parse *pPar
23eee 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  se,    /* Parsin
23eef 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
23ef0 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
23ef1 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c   /* List of fiel
23ef2 64 20 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e  d names to be in
23ef3 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f  dexed */.  int o
23ef4 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20  nError,      /* 
23ef5 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20  What to do with 
23ef6 61 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e  a uniqueness con
23ef7 66 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61  flict */.  int a
23ef8 75 74 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20  utoInc,      /* 
23ef9 54 72 75 65 20 69 66 20 74 68 65 20 41 55 54 4f  True if the AUTO
23efa 49 4e 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72  INCREMENT keywor
23efb 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  d is present */.
23efc 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20    int sortOrder 
23efd 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f      /* SQLITE_SO
23efe 5f 41 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53  _ASC or SQLITE_S
23eff 4f 5f 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54  O_DESC */.){.  T
23f00 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61  able *pTab = pPa
23f01 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
23f02 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20    char *zType = 
23f03 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20  0;.  int iCol = 
23f04 2d 31 2c 20 69 3b 0a 20 20 69 66 28 20 70 54 61  -1, i;.  if( pTa
23f05 62 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41  b==0 || IN_DECLA
23f06 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 70  RE_VTAB ) goto p
23f07 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b  rimary_key_exit;
23f08 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62  .  if( pTab->tab
23f09 46 6c 61 67 73 20 26 20 54 46 5f 48 61 73 50 72  Flags & TF_HasPr
23f0a 69 6d 61 72 79 4b 65 79 20 29 7b 0a 20 20 20 20  imaryKey ){.    
23f0b 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
23f0c 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22  pParse, .      "
23f0d 74 61 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73  table \"%s\" has
23f0e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70   more than one p
23f0f 72 69 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61  rimary key", pTa
23f10 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  b->zName);.    g
23f11 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f  oto primary_key_
23f12 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62  exit;.  }.  pTab
23f13 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46  ->tabFlags |= TF
23f14 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a  _HasPrimaryKey;.
23f15 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
23f16 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61  {.    iCol = pTa
23f17 62 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20  b->nCol - 1;.   
23f18 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c   pTab->aCol[iCol
23f19 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b  ].isPrimKey = 1;
23f1a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f  .  }else{.    fo
23f1b 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  r(i=0; i<pList->
23f1c 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
23f1d 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69     for(iCol=0; i
23f1e 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  Col<pTab->nCol; 
23f1f 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20  iCol++){.       
23f20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
23f21 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  Cmp(pList->a[i].
23f22 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f  zName, pTab->aCo
23f23 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d  l[iCol].zName)==
23f24 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  0 ){.          b
23f25 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
23f26 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
23f27 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  ( iCol<pTab->nCo
23f28 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61  l ){.        pTa
23f29 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73  b->aCol[iCol].is
23f2a 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20  PrimKey = 1;.   
23f2b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
23f2c 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e  f( pList->nExpr>
23f2d 31 20 29 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20  1 ) iCol = -1;. 
23f2e 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30   }.  if( iCol>=0
23f2f 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e   && iCol<pTab->n
23f30 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 54 79 70 65  Col ){.    zType
23f31 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   = pTab->aCol[iC
23f32 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20  ol].zType;.  }. 
23f33 20 69 66 28 20 7a 54 79 70 65 20 26 26 20 73 71   if( zType && sq
23f34 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79  lite3StrICmp(zTy
23f35 70 65 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d  pe, "INTEGER")==
23f36 30 0a 20 20 20 20 20 20 20 20 26 26 20 73 6f 72  0.        && sor
23f37 74 4f 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53  tOrder==SQLITE_S
23f38 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20 70 54 61  O_ASC ){.    pTa
23f39 62 2d 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b  b->iPKey = iCol;
23f3a 0a 20 20 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f  .    pTab->keyCo
23f3b 6e 66 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72  nf = (u8)onError
23f3c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 75  ;.    assert( au
23f3d 74 6f 49 6e 63 3d 3d 30 20 7c 7c 20 61 75 74 6f  toInc==0 || auto
23f3e 49 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 54  Inc==1 );.    pT
23f3f 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20  ab->tabFlags |= 
23f40 61 75 74 6f 49 6e 63 2a 54 46 5f 41 75 74 6f 69  autoInc*TF_Autoi
23f41 6e 63 72 65 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73  ncrement;.  }els
23f42 65 20 69 66 28 20 61 75 74 6f 49 6e 63 20 29 7b  e if( autoInc ){
23f43 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
23f44 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45  OMIT_AUTOINCREME
23f45 4e 54 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  NT.    sqlite3Er
23f46 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23f47 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73  AUTOINCREMENT is
23f48 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 6f 6e   only allowed on
23f49 20 61 6e 20 22 0a 20 20 20 20 20 20 20 22 49 4e   an ".       "IN
23f4a 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
23f4b 59 22 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65  Y");.#endif.  }e
23f4c 6c 73 65 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  lse{.    Index *
23f4d 70 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  p;.    p = sqlit
23f4e 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50  e3CreateIndex(pP
23f4f 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 70  arse, 0, 0, 0, p
23f50 4c 69 73 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 30  List, onError, 0
23f51 2c 20 30 2c 20 73 6f 72 74 4f 72 64 65 72 2c 20  , 0, sortOrder, 
23f52 30 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b  0);.    if( p ){
23f53 0a 20 20 20 20 20 20 70 2d 3e 61 75 74 6f 49 6e  .      p->autoIn
23f54 64 65 78 20 3d 20 32 3b 0a 20 20 20 20 7d 0a 20  dex = 2;.    }. 
23f55 20 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20     pList = 0;.  
23f56 7d 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65  }..primary_key_e
23f57 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78  xit:.  sqlite3Ex
23f58 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  prListDelete(pPa
23f59 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b  rse->db, pList);
23f5a 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  .  return;.}../*
23f5b 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48  .** Add a new CH
23f5c 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74  ECK constraint t
23f5d 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72  o the table curr
23f5e 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
23f5f 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  truction..*/.SQL
23f60 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
23f61 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b   sqlite3AddCheck
23f62 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61  Constraint(.  Pa
23f63 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
23f64 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
23f65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43  xt */.  Expr *pC
23f66 68 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65  heckExpr  /* The
23f67 20 63 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f   check expressio
23f68 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  n */.){.  sqlite
23f69 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
23f6a 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  db;.#ifndef SQLI
23f6b 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20  TE_OMIT_CHECK.  
23f6c 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50  Table *pTab = pP
23f6d 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
23f6e 0a 20 20 69 66 28 20 70 54 61 62 20 26 26 20 21  .  if( pTab && !
23f6f 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
23f70 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 70 43 68  ){.    pTab->pCh
23f71 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 45 78 70  eck = sqlite3Exp
23f72 72 41 6e 64 28 64 62 2c 20 70 54 61 62 2d 3e 70  rAnd(db, pTab->p
23f73 43 68 65 63 6b 2c 20 70 43 68 65 63 6b 45 78 70  Check, pCheckExp
23f74 72 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  r);.  }else.#end
23f75 69 66 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 74  if.  {.    sqlit
23f76 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
23f77 20 70 43 68 65 63 6b 45 78 70 72 29 3b 0a 20 20   pCheckExpr);.  
23f78 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  }.}../*.** Set t
23f79 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e  he collation fun
23f7a 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73  ction of the mos
23f7b 74 20 72 65 63 65 6e 74 6c 79 20 70 61 72 73 65  t recently parse
23f7c 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a  d table column.*
23f7d 2a 20 74 6f 20 74 68 65 20 43 6f 6c 6c 53 65 71  * to the CollSeq
23f7e 20 67 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54   given..*/.SQLIT
23f7f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23f80 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65  qlite3AddCollate
23f81 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72  Type(Parse *pPar
23f82 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65  se, Token *pToke
23f83 6e 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a  n){.  Table *p;.
23f84 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20    int i;.  char 
23f85 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  *zColl;         
23f86 20 20 20 20 20 2f 2a 20 44 65 71 75 6f 74 65 64       /* Dequoted
23f87 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69   name of collati
23f88 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20  on sequence */. 
23f89 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20   sqlite3 *db;.. 
23f8a 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65   if( (p = pParse
23f8b 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20  ->pNewTable)==0 
23f8c 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20  ) return;.  i = 
23f8d 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 64 62 20  p->nCol-1;.  db 
23f8e 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
23f8f 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e  zColl = sqlite3N
23f90 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
23f91 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20   pToken);.  if( 
23f92 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b  !zColl ) return;
23f93 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4c  ..  if( sqlite3L
23f94 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61  ocateCollSeq(pPa
23f95 72 73 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20  rse, zColl) ){. 
23f96 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
23f97 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a      p->aCol[i].z
23f98 43 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20  Coll = zColl;.  
23f99 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63  .    /* If the c
23f9a 6f 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65  olumn is declare
23f9b 64 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49  d as "<name> PRI
23f9c 4d 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45  MARY KEY COLLATE
23f9d 20 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a   <type>",.    **
23f9e 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d   then an index m
23f9f 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 72 65  ay have been cre
23fa0 61 74 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c  ated on this col
23fa1 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20  umn before the. 
23fa2 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20     ** collation 
23fa3 74 79 70 65 20 77 61 73 20 61 64 64 65 64 2e 20  type was added. 
23fa4 43 6f 72 72 65 63 74 20 74 68 69 73 20 69 66 20  Correct this if 
23fa5 69 74 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a  it is the case..
23fa6 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70      */.    for(p
23fa7 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70  Idx=p->pIndex; p
23fa8 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
23fa9 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73  pNext){.      as
23faa 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c  sert( pIdx->nCol
23fab 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  umn==1 );.      
23fac 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  if( pIdx->aiColu
23fad 6d 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20  mn[0]==i ){.    
23fae 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c      pIdx->azColl
23faf 5b 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d  [0] = p->aCol[i]
23fb0 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a  .zColl;.      }.
23fb1 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
23fb2 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
23fb3 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d  (db, zColl);.  }
23fb4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
23fb5 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
23fb6 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
23fb7 71 75 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62  quence for datab
23fb8 61 73 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a  ase native text.
23fb9 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e  ** encoding iden
23fba 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73 74  tified by the st
23fbb 72 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67  ring zName, leng
23fbc 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  th nName..**.** 
23fbd 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  If the requested
23fbe 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
23fbf 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c  nce is not avail
23fc0 61 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61  able, or not ava
23fc1 69 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65  ilable.** in the
23fc2 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65   database native
23fc3 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63   encoding, the c
23fc4 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79  ollation factory
23fc5 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a   is invoked to.*
23fc6 2a 20 72 65 71 75 65 73 74 20 69 74 2e 20 49 66  * request it. If
23fc7 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
23fc8 61 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20  actory does not 
23fc9 73 75 70 70 6c 79 20 73 75 63 68 20 61 20 73 65  supply such a se
23fca 71 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74  quence,.** and t
23fcb 68 65 20 73 65 71 75 65 6e 63 65 20 69 73 20 61  he sequence is a
23fcc 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74  vailable in anot
23fcd 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  her text encodin
23fce 67 2c 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a  g, then that is.
23fcf 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74  ** returned inst
23fd0 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f  ead..**.** If no
23fd1 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
23fd2 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61   requested colla
23fd3 74 69 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61  tions sequence a
23fd4 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72  re available, or
23fd5 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f  .** another erro
23fd6 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69  r occurs, NULL i
23fd7 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61  s returned and a
23fd8 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
23fd9 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20  written into.** 
23fda 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  pParse..**.** Th
23fdb 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
23fdc 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73  wrapper around s
23fdd 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
23fde 71 28 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69  q().  This routi
23fdf 6e 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68  ne.** invokes th
23fe0 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74  e collation fact
23fe1 6f 72 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64  ory if the named
23fe2 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f   collation canno
23fe3 74 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e  t be found.** an
23fe4 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65  d generates an e
23fe5 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a  rror message..**
23fe6 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71  .** See also: sq
23fe7 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
23fe8 28 29 2c 20 73 71 6c 69 74 65 33 47 65 74 43 6f  (), sqlite3GetCo
23fe9 6c 6c 53 65 71 28 29 0a 2a 2f 0a 53 51 4c 49 54  llSeq().*/.SQLIT
23fea 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
23feb 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  q *sqlite3Locate
23fec 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70  CollSeq(Parse *p
23fed 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
23fee 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 73 71 6c  r *zName){.  sql
23fef 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
23ff0 65 2d 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63 20  e->db;.  u8 enc 
23ff1 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 75 38 20  = ENC(db);.  u8 
23ff2 69 6e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e 69  initbusy = db->i
23ff3 6e 69 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c 6c  nit.busy;.  Coll
23ff4 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 70  Seq *pColl;..  p
23ff5 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69  Coll = sqlite3Fi
23ff6 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e  ndCollSeq(db, en
23ff7 63 2c 20 7a 4e 61 6d 65 2c 20 69 6e 69 74 62 75  c, zName, initbu
23ff8 73 79 29 3b 0a 20 20 69 66 28 20 21 69 6e 69 74  sy);.  if( !init
23ff9 62 75 73 79 20 26 26 20 28 21 70 43 6f 6c 6c 20  busy && (!pColl 
23ffa 7c 7c 20 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70 29  || !pColl->xCmp)
23ffb 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20   ){.    pColl = 
23ffc 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65  sqlite3GetCollSe
23ffd 71 28 64 62 2c 20 65 6e 63 2c 20 70 43 6f 6c 6c  q(db, enc, pColl
23ffe 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66  , zName);.    if
23fff 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
24000 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
24001 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75  g(pParse, "no su
24002 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ch collation seq
24003 75 65 6e 63 65 3a 20 25 73 22 2c 20 7a 4e 61 6d  uence: %s", zNam
24004 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  e);.    }.  }.. 
24005 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d   return pColl;.}
24006 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  .../*.** Generat
24007 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
24008 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73   increment the s
24009 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 2a 2a  chema cookie..**
2400a 0a 2a 2a 20 54 68 65 20 73 63 68 65 6d 61 20 63  .** The schema c
2400b 6f 6f 6b 69 65 20 69 73 20 75 73 65 64 20 74 6f  ookie is used to
2400c 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 6e 20   determine when 
2400d 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74  the schema for t
2400e 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  he.** database c
2400f 68 61 6e 67 65 73 2e 20 20 41 66 74 65 72 20 65  hanges.  After e
24010 61 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ach schema chang
24011 65 2c 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 61  e, the cookie va
24012 6c 75 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20  lue.** changes. 
24013 20 57 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20   When a process 
24014 66 69 72 73 74 20 72 65 61 64 73 20 74 68 65 20  first reads the 
24015 73 63 68 65 6d 61 20 69 74 20 72 65 63 6f 72 64  schema it record
24016 73 20 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 2e  s the.** cookie.
24017 20 20 54 68 65 72 65 61 66 74 65 72 2c 20 77 68    Thereafter, wh
24018 65 6e 65 76 65 72 20 69 74 20 67 6f 65 73 20 74  enever it goes t
24019 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74  o access the dat
2401a 61 62 61 73 65 2c 0a 2a 2a 20 69 74 20 63 68 65  abase,.** it che
2401b 63 6b 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 74  cks the cookie t
2401c 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
2401d 73 63 68 65 6d 61 20 68 61 73 20 6e 6f 74 20 63  schema has not c
2401e 68 61 6e 67 65 64 0a 2a 2a 20 73 69 6e 63 65 20  hanged.** since 
2401f 69 74 20 77 61 73 20 6c 61 73 74 20 72 65 61 64  it was last read
24020 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 70 6c 61  ..**.** This pla
24021 6e 20 69 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 74  n is not complet
24022 65 6c 79 20 62 75 6c 6c 65 74 2d 70 72 6f 6f 66  ely bullet-proof
24023 2e 20 20 49 74 20 69 73 20 70 6f 73 73 69 62 6c  .  It is possibl
24024 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 63 68  e for.** the sch
24025 65 6d 61 20 74 6f 20 63 68 61 6e 67 65 20 6d 75  ema to change mu
24026 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 61 6e 64  ltiple times and
24027 20 66 6f 72 20 74 68 65 20 63 6f 6f 6b 69 65 20   for the cookie 
24028 74 6f 20 62 65 0a 2a 2a 20 73 65 74 20 62 61 63  to be.** set bac
24029 6b 20 74 6f 20 70 72 69 6f 72 20 76 61 6c 75 65  k to prior value
2402a 2e 20 20 42 75 74 20 73 63 68 65 6d 61 20 63 68  .  But schema ch
2402b 61 6e 67 65 73 20 61 72 65 20 69 6e 66 72 65 71  anges are infreq
2402c 75 65 6e 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20  uent.** and the 
2402d 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 68  probability of h
2402e 69 74 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20  itting the same 
2402f 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20 69 73 20  cookie value is 
24030 6f 6e 6c 79 0a 2a 2a 20 31 20 63 68 61 6e 63 65  only.** 1 chance
24031 20 69 6e 20 32 5e 33 32 2e 20 20 53 6f 20 77 65   in 2^32.  So we
24032 27 72 65 20 73 61 66 65 20 65 6e 6f 75 67 68 2e  're safe enough.
24033 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
24034 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
24035 68 61 6e 67 65 43 6f 6f 6b 69 65 28 50 61 72 73  hangeCookie(Pars
24036 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
24037 44 62 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20  Db){.  int r1 = 
24038 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
24039 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c  g(pParse);.  sql
2403a 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2403b 65 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76  e->db;.  Vdbe *v
2403c 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2403d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2403e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2403f 67 65 72 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  ger, db->aDb[iDb
24040 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d  ].pSchema->schem
24041 61 5f 63 6f 6f 6b 69 65 2b 31 2c 20 72 31 29 3b  a_cookie+1, r1);
24042 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
24043 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f  dOp3(v, OP_SetCo
24044 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45  okie, iDb, BTREE
24045 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c  _SCHEMA_VERSION,
24046 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52   r1);.  sqlite3R
24047 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
24048 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a  arse, r1);.}../*
24049 0a 2a 2a 20 4d 65 61 73 75 72 65 20 74 68 65 20  .** Measure the 
2404a 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
2404b 74 65 72 73 20 6e 65 65 64 65 64 20 74 6f 20 6f  ters needed to o
2404c 75 74 70 75 74 20 74 68 65 20 67 69 76 65 6e 0a  utput the given.
2404d 2a 2a 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20  ** identifier.  
2404e 54 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72  The number retur
2404f 6e 65 64 20 69 6e 63 6c 75 64 65 73 20 61 6e 79  ned includes any
24050 20 71 75 6f 74 65 73 20 75 73 65 64 0a 2a 2a 20   quotes used.** 
24051 62 75 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63  but does not inc
24052 6c 75 64 65 20 74 68 65 20 6e 75 6c 6c 20 74 65  lude the null te
24053 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20  rminator..**.** 
24054 54 68 65 20 65 73 74 69 6d 61 74 65 20 69 73 20  The estimate is 
24055 63 6f 6e 73 65 72 76 61 74 69 76 65 2e 20 20 49  conservative.  I
24056 74 20 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65  t might be large
24057 72 20 74 68 61 74 20 77 68 61 74 20 69 73 0a 2a  r that what is.*
24058 2a 20 72 65 61 6c 6c 79 20 6e 65 65 64 65 64 2e  * really needed.
24059 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
2405a 64 65 6e 74 4c 65 6e 67 74 68 28 63 6f 6e 73 74  dentLength(const
2405b 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74   char *z){.  int
2405c 20 6e 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 2a   n;.  for(n=0; *
2405d 7a 3b 20 6e 2b 2b 2c 20 7a 2b 2b 29 7b 0a 20 20  z; n++, z++){.  
2405e 20 20 69 66 28 20 2a 7a 3d 3d 27 22 27 20 29 7b    if( *z=='"' ){
2405f 20 6e 2b 2b 3b 20 7d 0a 20 20 7d 0a 20 20 72 65   n++; }.  }.  re
24060 74 75 72 6e 20 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f  turn n + 2;.}../
24061 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70  *.** The first p
24062 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f  arameter is a po
24063 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 75 74 70  inter to an outp
24064 75 74 20 62 75 66 66 65 72 2e 20 54 68 65 20 73  ut buffer. The s
24065 65 63 6f 6e 64 20 0a 2a 2a 20 70 61 72 61 6d 65  econd .** parame
24066 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ter is a pointer
24067 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74   to an integer t
24068 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
24069 20 6f 66 66 73 65 74 20 61 74 0a 2a 2a 20 77 68   offset at.** wh
2406a 69 63 68 20 74 6f 20 77 72 69 74 65 20 69 6e 74  ich to write int
2406b 6f 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  o the output buf
2406c 66 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69  fer. This functi
2406d 6f 6e 20 63 6f 70 69 65 73 20 74 68 65 0a 2a 2a  on copies the.**
2406e 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   nul-terminated 
2406f 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74  string pointed t
24070 6f 20 62 79 20 74 68 65 20 74 68 69 72 64 20 70  o by the third p
24071 61 72 61 6d 65 74 65 72 2c 20 7a 53 69 67 6e 65  arameter, zSigne
24072 64 49 64 65 6e 74 2c 0a 2a 2a 20 74 6f 20 74 68  dIdent,.** to th
24073 65 20 73 70 65 63 69 66 69 65 64 20 6f 66 66 73  e specified offs
24074 65 74 20 69 6e 20 74 68 65 20 62 75 66 66 65 72  et in the buffer
24075 20 61 6e 64 20 75 70 64 61 74 65 73 20 2a 70 49   and updates *pI
24076 64 78 20 74 6f 20 72 65 66 65 72 0a 2a 2a 20 74  dx to refer.** t
24077 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  o the first byte
24078 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20   after the last 
24079 62 79 74 65 20 77 72 69 74 74 65 6e 20 62 65 66  byte written bef
2407a 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
2407b 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72  * .** If the str
2407c 69 6e 67 20 7a 53 69 67 6e 65 64 49 64 65 6e 74  ing zSignedIdent
2407d 20 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 65   consists entire
2407e 6c 79 20 6f 66 20 61 6c 70 68 61 2d 6e 75 6d 65  ly of alpha-nume
2407f 72 69 63 0a 2a 2a 20 63 68 61 72 61 63 74 65 72  ric.** character
24080 73 2c 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69  s, does not begi
24081 6e 20 77 69 74 68 20 61 20 64 69 67 69 74 20 61  n with a digit a
24082 6e 64 20 69 73 20 6e 6f 74 20 61 6e 20 53 51 4c  nd is not an SQL
24083 20 6b 65 79 77 6f 72 64 2c 0a 2a 2a 20 74 68 65   keyword,.** the
24084 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 74  n it is copied t
24085 6f 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  o the output buf
24086 66 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 69  fer exactly as i
24087 74 20 69 73 2e 20 4f 74 68 65 72 77 69 73 65 2c  t is. Otherwise,
24088 0a 2a 2a 20 69 74 20 69 73 20 71 75 6f 74 65 64  .** it is quoted
24089 20 75 73 69 6e 67 20 64 6f 75 62 6c 65 2d 71 75   using double-qu
2408a 6f 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  otes..*/.static 
2408b 76 6f 69 64 20 69 64 65 6e 74 50 75 74 28 63 68  void identPut(ch
2408c 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 78  ar *z, int *pIdx
2408d 2c 20 63 68 61 72 20 2a 7a 53 69 67 6e 65 64 49  , char *zSignedI
2408e 64 65 6e 74 29 7b 0a 20 20 75 6e 73 69 67 6e 65  dent){.  unsigne
2408f 64 20 63 68 61 72 20 2a 7a 49 64 65 6e 74 20 3d  d char *zIdent =
24090 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a   (unsigned char*
24091 29 7a 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a 20  )zSignedIdent;. 
24092 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51   int i, j, needQ
24093 75 6f 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49 64  uote;.  i = *pId
24094 78 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a  x;..  for(j=0; z
24095 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a  Ident[j]; j++){.
24096 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
24097 49 73 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b 6a  Isalnum(zIdent[j
24098 5d 29 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d 21  ]) && zIdent[j]!
24099 3d 27 5f 27 20 29 20 62 72 65 61 6b 3b 0a 20 20  ='_' ) break;.  
2409a 7d 0a 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20  }.  needQuote = 
2409b 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a  sqlite3Isdigit(z
2409c 49 64 65 6e 74 5b 30 5d 29 20 7c 7c 20 73 71 6c  Ident[0]) || sql
2409d 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28  ite3KeywordCode(
2409e 7a 49 64 65 6e 74 2c 20 6a 29 21 3d 54 4b 5f 49  zIdent, j)!=TK_I
2409f 44 3b 0a 20 20 69 66 28 20 21 6e 65 65 64 51 75  D;.  if( !needQu
240a0 6f 74 65 20 29 7b 0a 20 20 20 20 6e 65 65 64 51  ote ){.    needQ
240a1 75 6f 74 65 20 3d 20 7a 49 64 65 6e 74 5b 6a 5d  uote = zIdent[j]
240a2 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 65 65  ;.  }..  if( nee
240a3 64 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20  dQuote ) z[i++] 
240a4 3d 20 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30  = '"';.  for(j=0
240a5 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b  ; zIdent[j]; j++
240a6 29 7b 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20  ){.    z[i++] = 
240a7 7a 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69  zIdent[j];.    i
240a8 66 28 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22  f( zIdent[j]=='"
240a9 27 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27  ' ) z[i++] = '"'
240aa 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64  ;.  }.  if( need
240ab 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d  Quote ) z[i++] =
240ac 20 27 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30   '"';.  z[i] = 0
240ad 3b 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d  ;.  *pIdx = i;.}
240ae 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
240af 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   a CREATE TABLE 
240b0 73 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70  statement approp
240b1 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 67 69  riate for the gi
240b2 76 65 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d  ven.** table.  M
240b3 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68  emory to hold th
240b4 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 74  e text of the st
240b5 61 74 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 69  atement is obtai
240b6 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69  ned.** from sqli
240b7 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d  teMalloc() and m
240b8 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20  ust be freed by 
240b9 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
240ba 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
240bb 63 68 61 72 20 2a 63 72 65 61 74 65 54 61 62 6c  char *createTabl
240bc 65 53 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64  eStmt(sqlite3 *d
240bd 62 2c 20 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20  b, Table *p){.  
240be 69 6e 74 20 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63  int i, k, n;.  c
240bf 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 63 68  har *zStmt;.  ch
240c0 61 72 20 2a 7a 53 65 70 2c 20 2a 7a 53 65 70 32  ar *zSep, *zSep2
240c1 2c 20 2a 7a 45 6e 64 3b 0a 20 20 43 6f 6c 75 6d  , *zEnd;.  Colum
240c2 6e 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30  n *pCol;.  n = 0
240c3 3b 0a 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70  ;.  for(pCol = p
240c4 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70  ->aCol, i=0; i<p
240c5 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  ->nCol; i++, pCo
240c6 6c 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 69  l++){.    n += i
240c7 64 65 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d  dentLength(pCol-
240c8 3e 7a 4e 61 6d 65 29 20 2b 20 35 3b 0a 20 20 7d  >zName) + 5;.  }
240c9 0a 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e  .  n += identLen
240ca 67 74 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  gth(p->zName);. 
240cb 20 69 66 28 20 6e 3c 35 30 20 29 7b 20 0a 20 20   if( n<50 ){ .  
240cc 20 20 7a 53 65 70 20 3d 20 22 22 3b 0a 20 20 20    zSep = "";.   
240cd 20 7a 53 65 70 32 20 3d 20 22 2c 22 3b 0a 20 20   zSep2 = ",";.  
240ce 20 20 7a 45 6e 64 20 3d 20 22 29 22 3b 0a 20 20    zEnd = ")";.  
240cf 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70 20  }else{.    zSep 
240d0 3d 20 22 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 53  = "\n  ";.    zS
240d1 65 70 32 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a 20  ep2 = ",\n  ";. 
240d2 20 20 20 7a 45 6e 64 20 3d 20 22 5c 6e 29 22 3b     zEnd = "\n)";
240d3 0a 20 20 7d 0a 20 20 6e 20 2b 3d 20 33 35 20 2b  .  }.  n += 35 +
240d4 20 36 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53   6*p->nCol;.  zS
240d5 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  tmt = sqlite3Mal
240d6 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 69 66 28 20  loc( n );.  if( 
240d7 7a 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20  zStmt==0 ){.    
240d8 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
240d9 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
240da 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
240db 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53  3_snprintf(n, zS
240dc 74 6d 74 2c 20 22 43 52 45 41 54 45 20 54 41 42  tmt, "CREATE TAB
240dd 4c 45 20 22 29 3b 0a 20 20 6b 20 3d 20 73 71 6c  LE ");.  k = sql
240de 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 53 74  ite3Strlen30(zSt
240df 6d 74 29 3b 0a 20 20 69 64 65 6e 74 50 75 74 28  mt);.  identPut(
240e0 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a 4e  zStmt, &k, p->zN
240e1 61 6d 65 29 3b 0a 20 20 7a 53 74 6d 74 5b 6b 2b  ame);.  zStmt[k+
240e2 2b 5d 20 3d 20 27 28 27 3b 0a 20 20 66 6f 72 28  +] = '(';.  for(
240e3 70 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20 69 3d  pCol=p->aCol, i=
240e4 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b  0; i<p->nCol; i+
240e5 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  +, pCol++){.    
240e6 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
240e7 72 20 2a 20 63 6f 6e 73 74 20 61 7a 54 79 70 65  r * const azType
240e8 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 2f  [] = {.        /
240e9 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  * SQLITE_AFF_TEX
240ea 54 20 20 20 20 2a 2f 20 22 20 54 45 58 54 22 2c  T    */ " TEXT",
240eb 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49  .        /* SQLI
240ec 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 2a  TE_AFF_NONE    *
240ed 2f 20 22 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a  / "",.        /*
240ee 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45   SQLITE_AFF_NUME
240ef 52 49 43 20 2a 2f 20 22 20 4e 55 4d 22 2c 0a 20  RIC */ " NUM",. 
240f0 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45         /* SQLITE
240f1 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 2a 2f 20  _AFF_INTEGER */ 
240f2 22 20 49 4e 54 22 2c 0a 20 20 20 20 20 20 20 20  " INT",.        
240f3 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45  /* SQLITE_AFF_RE
240f4 41 4c 20 20 20 20 2a 2f 20 22 20 52 45 41 4c 22  AL    */ " REAL"
240f5 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20  .    };.    int 
240f6 6c 65 6e 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  len;.    const c
240f7 68 61 72 20 2a 7a 54 79 70 65 3b 0a 0a 20 20 20  har *zType;..   
240f8 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
240f9 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d  f(n-k, &zStmt[k]
240fa 2c 20 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20 2b  , zSep);.    k +
240fb 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
240fc 30 28 26 7a 53 74 6d 74 5b 6b 5d 29 3b 0a 20 20  0(&zStmt[k]);.  
240fd 20 20 7a 53 65 70 20 3d 20 7a 53 65 70 32 3b 0a    zSep = zSep2;.
240fe 20 20 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74      identPut(zSt
240ff 6d 74 2c 20 26 6b 2c 20 70 43 6f 6c 2d 3e 7a 4e  mt, &k, pCol->zN
24100 61 6d 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ame);.    assert
24101 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  ( pCol->affinity
24102 2d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54  -SQLITE_AFF_TEXT
24103 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 61 73 73   >= 0 );.    ass
24104 65 72 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e  ert( pCol->affin
24105 69 74 79 2d 53 51 4c 49 54 45 5f 41 46 46 5f 54  ity-SQLITE_AFF_T
24106 45 58 54 20 3c 20 73 69 7a 65 6f 66 28 61 7a 54  EXT < sizeof(azT
24107 79 70 65 29 2f 73 69 7a 65 6f 66 28 61 7a 54 79  ype)/sizeof(azTy
24108 70 65 5b 30 5d 29 20 29 3b 0a 20 20 20 20 74 65  pe[0]) );.    te
24109 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66  stcase( pCol->af
2410a 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
2410b 46 46 5f 54 45 58 54 20 29 3b 0a 20 20 20 20 74  FF_TEXT );.    t
2410c 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61  estcase( pCol->a
2410d 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
2410e 41 46 46 5f 4e 4f 4e 45 20 29 3b 0a 20 20 20 20  AFF_NONE );.    
2410f 74 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e  testcase( pCol->
24110 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
24111 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a  _AFF_NUMERIC );.
24112 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43      testcase( pC
24113 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51  ol->affinity==SQ
24114 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
24115 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
24116 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  ( pCol->affinity
24117 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  ==SQLITE_AFF_REA
24118 4c 20 29 3b 0a 20 20 20 20 0a 20 20 20 20 7a 54  L );.    .    zT
24119 79 70 65 20 3d 20 61 7a 54 79 70 65 5b 70 43 6f  ype = azType[pCo
2411a 6c 2d 3e 61 66 66 69 6e 69 74 79 20 2d 20 53 51  l->affinity - SQ
2411b 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 5d 3b 0a  LITE_AFF_TEXT];.
2411c 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65      len = sqlite
2411d 33 53 74 72 6c 65 6e 33 30 28 7a 54 79 70 65 29  3Strlen30(zType)
2411e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  ;.    assert( pC
2411f 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51  ol->affinity==SQ
24120 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 0a 20  LITE_AFF_NONE . 
24121 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 43             || pC
24122 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 73 71  ol->affinity==sq
24123 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70  lite3AffinityTyp
24124 65 28 7a 54 79 70 65 29 20 29 3b 0a 20 20 20 20  e(zType) );.    
24125 6d 65 6d 63 70 79 28 26 7a 53 74 6d 74 5b 6b 5d  memcpy(&zStmt[k]
24126 2c 20 7a 54 79 70 65 2c 20 6c 65 6e 29 3b 0a 20  , zType, len);. 
24127 20 20 20 6b 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20     k += len;.   
24128 20 61 73 73 65 72 74 28 20 6b 3c 3d 6e 20 29 3b   assert( k<=n );
24129 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73  .  }.  sqlite3_s
2412a 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53  nprintf(n-k, &zS
2412b 74 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 45  tmt[k], "%s", zE
2412c 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 53  nd);.  return zS
2412d 74 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  tmt;.}../*.** Th
2412e 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
2412f 6c 6c 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74  lled to report t
24130 68 65 20 66 69 6e 61 6c 20 22 29 22 20 74 68 61  he final ")" tha
24131 74 20 74 65 72 6d 69 6e 61 74 65 73 0a 2a 2a 20  t terminates.** 
24132 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
24133 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
24134 54 68 65 20 74 61 62 6c 65 20 73 74 72 75 63 74  The table struct
24135 75 72 65 20 74 68 61 74 20 6f 74 68 65 72 20 61  ure that other a
24136 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 68  ction routines h
24137 61 76 65 20 62 65 65 6e 20 62 75 69 6c 64 69 6e  ave been buildin
24138 67 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f  g.** is added to
24139 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61   the internal ha
2413a 73 68 20 74 61 62 6c 65 73 2c 20 61 73 73 75 6d  sh tables, assum
2413b 69 6e 67 20 6e 6f 20 65 72 72 6f 72 73 20 68 61  ing no errors ha
2413c 76 65 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 0a  ve.** occurred..
2413d 2a 2a 0a 2a 2a 20 41 6e 20 65 6e 74 72 79 20 66  **.** An entry f
2413e 6f 72 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  or the table is 
2413f 6d 61 64 65 20 69 6e 20 74 68 65 20 6d 61 73 74  made in the mast
24140 65 72 20 74 61 62 6c 65 20 6f 6e 20 64 69 73 6b  er table on disk
24141 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 74 68 69 73  , unless.** this
24142 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20   is a temporary 
24143 74 61 62 6c 65 20 6f 72 20 64 62 2d 3e 69 6e 69  table or db->ini
24144 74 2e 62 75 73 79 3d 3d 31 2e 20 20 57 68 65 6e  t.busy==1.  When
24145 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d   db->init.busy==
24146 31 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 77 65  1.** it means we
24147 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65   are reading the
24148 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
24149 61 62 6c 65 20 62 65 63 61 75 73 65 20 77 65 20  able because we 
2414a 6a 75 73 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65  just.** connecte
2414b 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  d to the databas
2414c 65 20 6f 72 20 62 65 63 61 75 73 65 20 74 68 65  e or because the
2414d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
2414e 61 62 6c 65 20 68 61 73 0a 2a 2a 20 72 65 63 65  able has.** rece
2414f 6e 74 6c 79 20 63 68 61 6e 67 65 64 2c 20 73 6f  ntly changed, so
24150 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74   the entry for t
24151 68 69 73 20 74 61 62 6c 65 20 61 6c 72 65 61 64  his table alread
24152 79 20 65 78 69 73 74 73 20 69 6e 0a 2a 2a 20 74  y exists in.** t
24153 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
24154 20 74 61 62 6c 65 2e 20 20 57 65 20 64 6f 20 6e   table.  We do n
24155 6f 74 20 77 61 6e 74 20 74 6f 20 63 72 65 61 74  ot want to creat
24156 65 20 69 74 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a  e it again..**.*
24157 2a 20 49 66 20 74 68 65 20 70 53 65 6c 65 63 74  * If the pSelect
24158 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74   argument is not
24159 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20   NULL, it means 
2415a 74 68 61 74 20 74 68 69 73 20 72 6f 75 74 69 6e  that this routin
2415b 65 0a 2a 2a 20 77 61 73 20 63 61 6c 6c 65 64 20  e.** was called 
2415c 74 6f 20 63 72 65 61 74 65 20 61 20 74 61 62 6c  to create a tabl
2415d 65 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f 6d  e generated from
2415e 20 61 20 0a 2a 2a 20 22 43 52 45 41 54 45 20 54   a .** "CREATE T
2415f 41 42 4c 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45  ABLE ... AS SELE
24160 43 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e  CT ..." statemen
24161 74 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e  t.  The column n
24162 61 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 6e  ames of.** the n
24163 65 77 20 74 61 62 6c 65 20 77 69 6c 6c 20 6d 61  ew table will ma
24164 74 63 68 20 74 68 65 20 72 65 73 75 6c 74 20 73  tch the result s
24165 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  et of the SELECT
24166 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
24167 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
24168 45 6e 64 54 61 62 6c 65 28 0a 20 20 50 61 72 73  EndTable(.  Pars
24169 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2416a 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e      /* Parse con
2416b 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  text */.  Token 
2416c 2a 70 43 6f 6e 73 2c 20 20 20 20 20 20 20 20 20  *pCons,         
2416d 20 20 2f 2a 20 54 68 65 20 27 2c 27 20 74 6f 6b    /* The ',' tok
2416e 65 6e 20 61 66 74 65 72 20 74 68 65 20 6c 61 73  en after the las
2416f 74 20 63 6f 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a  t column defn. *
24170 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c  /.  Token *pEnd,
24171 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
24172 68 65 20 66 69 6e 61 6c 20 27 29 27 20 74 6f 6b  he final ')' tok
24173 65 6e 20 69 6e 20 74 68 65 20 43 52 45 41 54 45  en in the CREATE
24174 20 54 41 42 4c 45 20 2a 2f 0a 20 20 53 65 6c 65   TABLE */.  Sele
24175 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20  ct *pSelect     
24176 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 66 72      /* Select fr
24177 6f 6d 20 61 20 22 43 52 45 41 54 45 20 2e 2e 2e  om a "CREATE ...
24178 20 41 53 20 53 45 4c 45 43 54 22 20 2a 2f 0a 29   AS SELECT" */.)
24179 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20  {.  Table *p;.  
2417a 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2417b 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
2417c 69 44 62 3b 0a 0a 20 20 69 66 28 20 28 70 45 6e  iDb;..  if( (pEn
2417d 64 3d 3d 30 20 26 26 20 70 53 65 6c 65 63 74 3d  d==0 && pSelect=
2417e 3d 30 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  =0) || db->mallo
2417f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72  cFailed ){.    r
24180 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d  eturn;.  }.  p =
24181 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
24182 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  le;.  if( p==0 )
24183 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65   return;..  asse
24184 72 74 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75  rt( !db->init.bu
24185 73 79 20 7c 7c 20 21 70 53 65 6c 65 63 74 20 29  sy || !pSelect )
24186 3b 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  ;..  iDb = sqlit
24187 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
24188 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b  db, p->pSchema);
24189 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2418a 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a  _OMIT_CHECK.  /*
2418b 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69   Resolve names i
2418c 6e 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73  n all CHECK cons
2418d 74 72 61 69 6e 74 20 65 78 70 72 65 73 73 69 6f  traint expressio
2418e 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ns..  */.  if( p
2418f 2d 3e 70 43 68 65 63 6b 20 29 7b 0a 20 20 20 20  ->pCheck ){.    
24190 53 72 63 4c 69 73 74 20 73 53 72 63 3b 20 20 20  SrcList sSrc;   
24191 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24192 2f 2a 20 46 61 6b 65 20 53 72 63 4c 69 73 74 20  /* Fake SrcList 
24193 66 6f 72 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  for pParse->pNew
24194 54 61 62 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d  Table */.    Nam
24195 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20  eContext sNC;   
24196 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24197 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  Name context for
24198 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
24199 6c 65 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65  le */..    memse
2419a 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f  t(&sNC, 0, sizeo
2419b 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d  f(sNC));.    mem
2419c 73 65 74 28 26 73 53 72 63 2c 20 30 2c 20 73 69  set(&sSrc, 0, si
2419d 7a 65 6f 66 28 73 53 72 63 29 29 3b 0a 20 20 20  zeof(sSrc));.   
2419e 20 73 53 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a   sSrc.nSrc = 1;.
2419f 20 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e      sSrc.a[0].zN
241a0 61 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a  ame = p->zName;.
241a1 20 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54      sSrc.a[0].pT
241a2 61 62 20 3d 20 70 3b 0a 20 20 20 20 73 53 72 63  ab = p;.    sSrc
241a3 2e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20  .a[0].iCursor = 
241a4 2d 31 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72  -1;.    sNC.pPar
241a5 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20  se = pParse;.   
241a6 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20   sNC.pSrcList = 
241a7 26 73 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69  &sSrc;.    sNC.i
241a8 73 43 68 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20  sCheck = 1;.    
241a9 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  if( sqlite3Resol
241aa 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43  veExprNames(&sNC
241ab 2c 20 70 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a  , p->pCheck) ){.
241ac 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
241ad 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f    }.  }.#endif /
241ae 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
241af 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f  E_OMIT_CHECK) */
241b0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62  ..  /* If the db
241b1 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31  ->init.busy is 1
241b2 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65   it means we are
241b3 20 72 65 61 64 69 6e 67 20 74 68 65 20 53 51 4c   reading the SQL
241b4 20 6f 66 66 20 74 68 65 0a 20 20 2a 2a 20 22 73   off the.  ** "s
241b5 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72  qlite_master" or
241b6 20 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61   "sqlite_temp_ma
241b7 73 74 65 72 22 20 74 61 62 6c 65 20 6f 6e 20 74  ster" table on t
241b8 68 65 20 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f  he disk..  ** So
241b9 20 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f   do not write to
241ba 20 74 68 65 20 64 69 73 6b 20 61 67 61 69 6e 2e   the disk again.
241bb 20 20 45 78 74 72 61 63 74 20 74 68 65 20 72 6f    Extract the ro
241bc 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20  ot page number. 
241bd 20 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c   ** for the tabl
241be 65 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69  e from the db->i
241bf 6e 69 74 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c  nit.newTnum fiel
241c0 64 2e 20 20 28 54 68 65 20 70 61 67 65 20 6e 75  d.  (The page nu
241c1 6d 62 65 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64  mber.  ** should
241c2 20 68 61 76 65 20 62 65 65 6e 20 70 75 74 20 74   have been put t
241c3 68 65 72 65 20 62 79 20 74 68 65 20 73 71 6c 69  here by the sqli
241c4 74 65 4f 70 65 6e 43 62 20 72 6f 75 74 69 6e 65  teOpenCb routine
241c5 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  .).  */.  if( db
241c6 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
241c7 20 20 20 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d     p->tnum = db-
241c8 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20  >init.newTnum;. 
241c9 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20   }..  /* If not 
241ca 69 6e 69 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68  initializing, th
241cb 65 6e 20 63 72 65 61 74 65 20 61 20 72 65 63 6f  en create a reco
241cc 72 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74  rd for the new t
241cd 61 62 6c 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65  able.  ** in the
241ce 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74   SQLITE_MASTER t
241cf 61 62 6c 65 20 6f 66 20 74 68 65 20 64 61 74 61  able of the data
241d0 62 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  base..  **.  ** 
241d1 49 66 20 74 68 69 73 20 69 73 20 61 20 54 45 4d  If this is a TEM
241d2 50 4f 52 41 52 59 20 74 61 62 6c 65 2c 20 77 72  PORARY table, wr
241d3 69 74 65 20 74 68 65 20 65 6e 74 72 79 20 69 6e  ite the entry in
241d4 74 6f 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  to the auxiliary
241d5 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65  .  ** file inste
241d6 61 64 20 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d  ad of into the m
241d7 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
241d8 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64  e..  */.  if( !d
241d9 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a  b->init.busy ){.
241da 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56      int n;.    V
241db 64 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72  dbe *v;.    char
241dc 20 2a 7a 54 79 70 65 3b 20 20 20 20 2f 2a 20 22   *zType;    /* "
241dd 76 69 65 77 22 20 6f 72 20 22 74 61 62 6c 65 22  view" or "table"
241de 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 54   */.    char *zT
241df 79 70 65 32 3b 20 20 20 2f 2a 20 22 56 49 45 57  ype2;   /* "VIEW
241e0 22 20 6f 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a  " or "TABLE" */.
241e1 20 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b      char *zStmt;
241e2 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74      /* Text of t
241e3 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
241e4 6f 72 20 43 52 45 41 54 45 20 56 49 45 57 20 73  or CREATE VIEW s
241e5 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20  tatement */..   
241e6 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
241e7 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  dbe(pParse);.   
241e8 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29   if( NEVER(v==0)
241e9 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20   ) return;..    
241ea 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
241eb 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30  1(v, OP_Close, 0
241ec 29 3b 0a 0a 20 20 20 20 2f 2a 20 0a 20 20 20 20  );..    /* .    
241ed 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 7a 54  ** Initialize zT
241ee 79 70 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20  ype for the new 
241ef 76 69 65 77 20 6f 72 20 74 61 62 6c 65 2e 0a 20  view or table.. 
241f0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d     */.    if( p-
241f1 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20  >pSelect==0 ){. 
241f2 20 20 20 20 20 2f 2a 20 41 20 72 65 67 75 6c 61       /* A regula
241f3 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20  r table */.     
241f4 20 7a 54 79 70 65 20 3d 20 22 74 61 62 6c 65 22   zType = "table"
241f5 3b 0a 20 20 20 20 20 20 7a 54 79 70 65 32 20 3d  ;.      zType2 =
241f6 20 22 54 41 42 4c 45 22 3b 0a 23 69 66 6e 64 65   "TABLE";.#ifnde
241f7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
241f8 45 57 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  EW.    }else{.  
241f9 20 20 20 20 2f 2a 20 41 20 76 69 65 77 20 2a 2f      /* A view */
241fa 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22  .      zType = "
241fb 76 69 65 77 22 3b 0a 20 20 20 20 20 20 7a 54 79  view";.      zTy
241fc 70 65 32 20 3d 20 22 56 49 45 57 22 3b 0a 23 65  pe2 = "VIEW";.#e
241fd 6e 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ndif.    }..    
241fe 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
241ff 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 20  CREATE TABLE xx 
24200 41 53 20 53 45 4c 45 43 54 20 2e 2e 2e 2c 20 65  AS SELECT ..., e
24201 78 65 63 75 74 65 20 74 68 65 20 53 45 4c 45 43  xecute the SELEC
24202 54 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65  T.    ** stateme
24203 6e 74 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 74  nt to populate t
24204 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 20 54 68  he new table. Th
24205 65 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62  e root-page numb
24206 65 72 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a  er for the.    *
24207 2a 20 6e 65 77 20 74 61 62 6c 65 20 69 73 20 69  * new table is i
24208 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 72 73  n register pPars
24209 65 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 20 20  e->regRoot..    
2420a 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e 63 65 20 74  **.    ** Once t
2420b 68 65 20 53 45 4c 45 43 54 20 68 61 73 20 62 65  he SELECT has be
2420c 65 6e 20 63 6f 64 65 64 20 62 79 20 73 71 6c 69  en coded by sqli
2420d 74 65 33 53 65 6c 65 63 74 28 29 2c 20 69 74 20  te3Select(), it 
2420e 69 73 20 69 6e 20 61 0a 20 20 20 20 2a 2a 20 73  is in a.    ** s
2420f 75 69 74 61 62 6c 65 20 73 74 61 74 65 20 74 6f  uitable state to
24210 20 71 75 65 72 79 20 66 6f 72 20 74 68 65 20 63   query for the c
24211 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 20  olumn names and 
24212 74 79 70 65 73 20 74 6f 20 62 65 20 75 73 65 64  types to be used
24213 0a 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20 6e  .    ** by the n
24214 65 77 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a  ew table..    **
24215 0a 20 20 20 20 2a 2a 20 41 20 73 68 61 72 65 64  .    ** A shared
24216 2d 63 61 63 68 65 20 77 72 69 74 65 2d 6c 6f 63  -cache write-loc
24217 6b 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  k is not require
24218 64 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68  d to write to th
24219 65 20 6e 65 77 20 74 61 62 6c 65 2c 0a 20 20 20  e new table,.   
2421a 20 2a 2a 20 61 73 20 61 20 73 63 68 65 6d 61 2d   ** as a schema-
2421b 6c 6f 63 6b 20 6d 75 73 74 20 68 61 76 65 20 61  lock must have a
2421c 6c 72 65 61 64 79 20 62 65 65 6e 20 6f 62 74 61  lready been obta
2421d 69 6e 65 64 20 74 6f 20 63 72 65 61 74 65 20 69  ined to create i
2421e 74 2e 20 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20  t. Since.    ** 
2421f 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 65 78  a schema-lock ex
24220 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 72  cludes all other
24221 20 64 61 74 61 62 61 73 65 20 75 73 65 72 73 2c   database users,
24222 20 74 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20   the write-lock 
24223 77 6f 75 6c 64 0a 20 20 20 20 2a 2a 20 62 65 20  would.    ** be 
24224 72 65 64 75 6e 64 61 6e 74 2e 0a 20 20 20 20 2a  redundant..    *
24225 2f 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65 63  /.    if( pSelec
24226 74 20 29 7b 0a 20 20 20 20 20 20 53 65 6c 65 63  t ){.      Selec
24227 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20  tDest dest;.    
24228 20 20 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62    Table *pSelTab
24229 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
2422a 70 50 61 72 73 65 2d 3e 6e 54 61 62 3d 3d 31 29  pParse->nTab==1)
2422b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2422c 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2422d 4f 70 65 6e 57 72 69 74 65 2c 20 31 2c 20 70 50  OpenWrite, 1, pP
2422e 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 20 69  arse->regRoot, i
2422f 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
24230 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
24231 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72  , 1);.      pPar
24232 73 65 2d 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 20  se->nTab = 2;.  
24233 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
24234 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c  tDestInit(&dest,
24235 20 53 52 54 5f 54 61 62 6c 65 2c 20 31 29 3b 0a   SRT_Table, 1);.
24236 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
24237 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ect(pParse, pSel
24238 65 63 74 2c 20 26 64 65 73 74 29 3b 0a 20 20 20  ect, &dest);.   
24239 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2423a 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp1(v, OP_Close
2423b 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 1);.      if( 
2423c 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20  pParse->nErr==0 
2423d 29 7b 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54  ){.        pSelT
2423e 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75  ab = sqlite3Resu
2423f 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50  ltSetOfSelect(pP
24240 61 72 73 65 2c 20 70 53 65 6c 65 63 74 29 3b 0a  arse, pSelect);.
24241 20 20 20 20 20 20 20 20 69 66 28 20 70 53 65 6c          if( pSel
24242 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Tab==0 ) return;
24243 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
24244 20 70 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20   p->aCol==0 );. 
24245 20 20 20 20 20 20 20 70 2d 3e 6e 43 6f 6c 20 3d         p->nCol =
24246 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a   pSelTab->nCol;.
24247 20 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 20          p->aCol 
24248 3d 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b  = pSelTab->aCol;
24249 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62  .        pSelTab
2424a 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->nCol = 0;.    
2424b 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f      pSelTab->aCo
2424c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73  l = 0;.        s
2424d 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
2424e 65 28 70 53 65 6c 54 61 62 29 3b 0a 20 20 20 20  e(pSelTab);.    
2424f 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
24250 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f  * Compute the co
24251 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74  mplete text of t
24252 68 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d  he CREATE statem
24253 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ent */.    if( p
24254 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
24255 7a 53 74 6d 74 20 3d 20 63 72 65 61 74 65 54 61  zStmt = createTa
24256 62 6c 65 53 74 6d 74 28 64 62 2c 20 70 29 3b 0a  bleStmt(db, p);.
24257 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
24258 20 6e 20 3d 20 28 69 6e 74 29 28 70 45 6e 64 2d   n = (int)(pEnd-
24259 3e 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61  >z - pParse->sNa
2425a 6d 65 54 6f 6b 65 6e 2e 7a 29 20 2b 20 31 3b 0a  meToken.z) + 1;.
2425b 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71        zStmt = sq
2425c 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2425d 20 0a 20 20 20 20 20 20 20 20 20 20 22 43 52 45   .          "CRE
2425e 41 54 45 20 25 73 20 25 2e 2a 73 22 2c 20 7a 54  ATE %s %.*s", zT
2425f 79 70 65 32 2c 20 6e 2c 20 70 50 61 72 73 65 2d  ype2, n, pParse-
24260 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 0a 20 20  >sNameToken.z.  
24261 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20      );.    }..  
24262 20 20 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20    /* A slot for 
24263 74 68 65 20 72 65 63 6f 72 64 20 68 61 73 20 61  the record has a
24264 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f  lready been allo
24265 63 61 74 65 64 20 69 6e 20 74 68 65 20 0a 20 20  cated in the .  
24266 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54    ** SQLITE_MAST
24267 45 52 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75  ER table.  We ju
24268 73 74 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74  st need to updat
24269 65 20 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68  e that slot with
2426a 20 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20   all.    ** the 
2426b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76  information we'v
2426c 65 20 63 6f 6c 6c 65 63 74 65 64 2e 0a 20 20 20  e collected..   
2426d 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e   */.    sqlite3N
2426e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
2426f 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54 45  e,.      "UPDATE
24270 20 25 51 2e 25 73 20 22 0a 20 20 20 20 20 20 20   %Q.%s ".       
24271 20 20 22 53 45 54 20 74 79 70 65 3d 27 25 73 27    "SET type='%s'
24272 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e  , name=%Q, tbl_n
24273 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65  ame=%Q, rootpage
24274 3d 23 25 64 2c 20 73 71 6c 3d 25 51 20 22 0a 20  =#%d, sql=%Q ". 
24275 20 20 20 20 20 20 22 57 48 45 52 45 20 72 6f 77        "WHERE row
24276 69 64 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64  id=#%d",.      d
24277 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
24278 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  e, SCHEMA_TABLE(
24279 69 44 62 29 2c 0a 20 20 20 20 20 20 7a 54 79 70  iDb),.      zTyp
2427a 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d  e,.      p->zNam
2427b 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d  e,.      p->zNam
2427c 65 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  e,.      pParse-
2427d 3e 72 65 67 52 6f 6f 74 2c 0a 20 20 20 20 20 20  >regRoot,.      
2427e 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50 61  zStmt,.      pPa
2427f 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20  rse->regRowid.  
24280 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33    );.    sqlite3
24281 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74  DbFree(db, zStmt
24282 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
24283 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
24284 65 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65  e, iDb);..#ifnde
24285 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
24286 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20  TOINCREMENT.    
24287 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
24288 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 63 72  if we need to cr
24289 65 61 74 65 20 61 6e 20 73 71 6c 69 74 65 5f 73  eate an sqlite_s
2428a 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 66 6f  equence table fo
2428b 72 0a 20 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67  r.    ** keeping
2428c 20 74 72 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e   track of autoin
2428d 63 72 65 6d 65 6e 74 20 6b 65 79 73 2e 0a 20 20  crement keys..  
2428e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e    */.    if( p->
2428f 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75  tabFlags & TF_Au
24290 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20  toincrement ){. 
24291 20 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26       Db *pDb = &
24292 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20  db->aDb[iDb];.  
24293 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63      if( pDb->pSc
24294 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 3d 3d 30  hema->pSeqTab==0
24295 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
24296 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
24297 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20  Parse,.         
24298 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25   "CREATE TABLE %
24299 51 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  Q.sqlite_sequenc
2429a 65 28 6e 61 6d 65 2c 73 65 71 29 22 2c 0a 20 20  e(name,seq)",.  
2429b 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61          pDb->zNa
2429c 6d 65 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20  me.        );.  
2429d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64      }.    }.#end
2429e 69 66 0a 0a 20 20 20 20 2f 2a 20 52 65 70 61 72  if..    /* Repar
2429f 73 65 20 65 76 65 72 79 74 68 69 6e 67 20 74 6f  se everything to
242a0 20 75 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65   update our inte
242a1 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 74  rnal data struct
242a2 75 72 65 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69  ures */.    sqli
242a3 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
242a4 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c   OP_ParseSchema,
242a5 20 69 44 62 2c 20 30 2c 20 30 2c 0a 20 20 20 20   iDb, 0, 0,.    
242a6 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e      sqlite3MPrin
242a7 74 66 28 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65  tf(db, "tbl_name
242a8 3d 27 25 71 27 22 2c 70 2d 3e 7a 4e 61 6d 65 29  ='%q'",p->zName)
242a9 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  , P4_DYNAMIC);. 
242aa 20 7d 0a 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68   }...  /* Add th
242ab 65 20 74 61 62 6c 65 20 74 6f 20 74 68 65 20 69  e table to the i
242ac 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65  n-memory represe
242ad 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  ntation of the d
242ae 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  atabase..  */.  
242af 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
242b0 79 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  y ){.    Table *
242b1 70 4f 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d 61  pOld;.    Schema
242b2 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 2d 3e 70   *pSchema = p->p
242b3 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 4f 6c 64  Schema;.    pOld
242b4 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e   = sqlite3HashIn
242b5 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74  sert(&pSchema->t
242b6 62 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 6d 65  blHash, p->zName
242b7 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
242b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
242b9 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
242ba 2d 3e 7a 4e 61 6d 65 29 2c 70 29 3b 0a 20 20 20  ->zName),p);.   
242bb 20 69 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20   if( pOld ){.   
242bc 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 4f     assert( p==pO
242bd 6c 64 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63  ld );  /* Malloc
242be 20 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65   must have faile
242bf 64 20 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73  d inside HashIns
242c0 65 72 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 64  ert() */.      d
242c1 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
242c2 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 1;.      retur
242c3 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  n;.    }.    pPa
242c4 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d  rse->pNewTable =
242c5 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6e 54 61 62   0;.    db->nTab
242c6 6c 65 2b 2b 3b 0a 20 20 20 20 64 62 2d 3e 66 6c  le++;.    db->fl
242c7 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e  ags |= SQLITE_In
242c8 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 0a 23 69  ternChanges;..#i
242c9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
242ca 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 20  T_ALTERTABLE.   
242cb 20 69 66 28 20 21 70 2d 3e 70 53 65 6c 65 63 74   if( !p->pSelect
242cc 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   ){.      const 
242cd 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63  char *zName = (c
242ce 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 50 61 72  onst char *)pPar
242cf 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a  se->sNameToken.z
242d0 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d  ;.      int nNam
242d1 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e;.      assert(
242d2 20 21 70 53 65 6c 65 63 74 20 26 26 20 70 43 6f   !pSelect && pCo
242d3 6e 73 20 26 26 20 70 45 6e 64 20 29 3b 0a 20 20  ns && pEnd );.  
242d4 20 20 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e 7a      if( pCons->z
242d5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
242d6 43 6f 6e 73 20 3d 20 70 45 6e 64 3b 0a 20 20 20  Cons = pEnd;.   
242d7 20 20 20 7d 0a 20 20 20 20 20 20 6e 4e 61 6d 65     }.      nName
242d8 20 3d 20 28 69 6e 74 29 28 28 63 6f 6e 73 74 20   = (int)((const 
242d9 63 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20  char *)pCons->z 
242da 2d 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  - zName);.      
242db 70 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20  p->addColOffset 
242dc 3d 20 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74  = 13 + sqlite3Ut
242dd 66 38 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c  f8CharLen(zName,
242de 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23   nName);.    }.#
242df 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66  endif.  }.}..#if
242e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
242e1 5f 56 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20  _VIEW./*.** The 
242e2 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
242e3 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64  s routine in ord
242e4 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  er to create a n
242e5 65 77 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54  ew VIEW.*/.SQLIT
242e6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
242e7 71 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77  qlite3CreateView
242e8 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
242e9 65 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61  e,     /* The pa
242ea 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
242eb 0a 20 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e  .  Token *pBegin
242ec 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45  ,     /* The CRE
242ed 41 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62  ATE token that b
242ee 65 67 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d  egins the statem
242ef 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ent */.  Token *
242f0 70 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54  pName1,     /* T
242f1 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f  he token that ho
242f2 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  lds the name of 
242f3 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f  the view */.  To
242f4 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20  ken *pName2,    
242f5 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68   /* The token th
242f6 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d  at holds the nam
242f7 65 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f  e of the view */
242f8 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
242f9 63 74 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43  ct,   /* A SELEC
242fa 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  T statement that
242fb 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65   will become the
242fc 20 6e 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69   new view */.  i
242fd 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20  nt isTemp,      
242fe 20 20 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20    /* TRUE for a 
242ff 54 45 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a  TEMPORARY view *
24300 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20  /.  int noErr   
24301 20 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65         /* Suppre
24302 73 73 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ss error message
24303 73 20 69 66 20 56 49 45 57 20 61 6c 72 65 61 64  s if VIEW alread
24304 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20  y exists */.){. 
24305 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74   Table *p;.  int
24306 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   n;.  const char
24307 20 2a 7a 3b 0a 20 20 54 6f 6b 65 6e 20 73 45 6e   *z;.  Token sEn
24308 64 3b 0a 20 20 44 62 46 69 78 65 72 20 73 46 69  d;.  DbFixer sFi
24309 78 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  x;.  Token *pNam
2430a 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20  e;.  int iDb;.  
2430b 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2430c 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28  arse->db;..  if(
2430d 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 30 20   pParse->nVar>0 
2430e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2430f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
24310 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e  parameters are n
24311 6f 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 76 69  ot allowed in vi
24312 65 77 73 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  ews");.    sqlit
24313 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
24314 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  b, pSelect);.   
24315 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73   return;.  }.  s
24316 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65  qlite3StartTable
24317 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c  (pParse, pName1,
24318 20 70 4e 61 6d 65 32 2c 20 69 73 54 65 6d 70 2c   pName2, isTemp,
24319 20 31 2c 20 30 2c 20 6e 6f 45 72 72 29 3b 0a 20   1, 0, noErr);. 
2431a 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65   p = pParse->pNe
2431b 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 3d  wTable;.  if( p=
2431c 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
2431d 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2431e 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  , pSelect);.    
2431f 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73  return;.  }.  as
24320 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45  sert( pParse->nE
24321 72 72 3d 3d 30 20 29 3b 20 2f 2a 20 49 66 20 73  rr==0 ); /* If s
24322 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65  qlite3StartTable
24323 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 4e 55 4c 4c   return non-NULL
24324 20 74 68 65 6e 0a 20 20 20 20 20 20 20 20 20 20   then.          
24325 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24326 20 20 20 2a 2a 20 74 68 65 72 65 20 63 6f 75 6c     ** there coul
24327 64 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20  d not have been 
24328 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73 71  an error */.  sq
24329 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65  lite3TwoPartName
2432a 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c  (pParse, pName1,
2432b 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29   pName2, &pName)
2432c 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
2432d 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
2432e 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  b, p->pSchema);.
2432f 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
24330 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72  Init(&sFix, pPar
24331 73 65 2c 20 69 44 62 2c 20 22 76 69 65 77 22 2c  se, iDb, "view",
24332 20 70 4e 61 6d 65 29 0a 20 20 20 20 26 26 20 73   pName).    && s
24333 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28  qlite3FixSelect(
24334 26 73 46 69 78 2c 20 70 53 65 6c 65 63 74 29 0a  &sFix, pSelect).
24335 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33    ){.    sqlite3
24336 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
24337 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72   pSelect);.    r
24338 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eturn;.  }..  /*
24339 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20   Make a copy of 
2433a 74 68 65 20 65 6e 74 69 72 65 20 53 45 4c 45 43  the entire SELEC
2433b 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  T statement that
2433c 20 64 65 66 69 6e 65 73 20 74 68 65 20 76 69 65   defines the vie
2433d 77 2e 0a 20 20 2a 2a 20 54 68 69 73 20 77 69 6c  w..  ** This wil
2433e 6c 20 66 6f 72 63 65 20 61 6c 6c 20 74 68 65 20  l force all the 
2433f 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c  Expr.token.z val
24340 75 65 73 20 74 6f 20 62 65 20 64 79 6e 61 6d 69  ues to be dynami
24341 63 61 6c 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63  cally.  ** alloc
24342 61 74 65 64 20 72 61 74 68 65 72 20 74 68 61 6e  ated rather than
24343 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 69 6e   point to the in
24344 70 75 74 20 73 74 72 69 6e 67 20 2d 20 77 68 69  put string - whi
24345 63 68 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20  ch means that.  
24346 2a 2a 20 74 68 65 79 20 77 69 6c 6c 20 70 65 72  ** they will per
24347 73 69 73 74 20 61 66 74 65 72 20 74 68 65 20 63  sist after the c
24348 75 72 72 65 6e 74 20 73 71 6c 69 74 65 33 5f 65  urrent sqlite3_e
24349 78 65 63 28 29 20 63 61 6c 6c 20 72 65 74 75 72  xec() call retur
2434a 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 53  ns..  */.  p->pS
2434b 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53  elect = sqlite3S
2434c 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53 65  electDup(db, pSe
2434d 6c 65 63 74 2c 20 45 58 50 52 44 55 50 5f 52 45  lect, EXPRDUP_RE
2434e 44 55 43 45 29 3b 0a 20 20 73 71 6c 69 74 65 33  DUCE);.  sqlite3
2434f 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
24350 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 28   pSelect);.  if(
24351 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
24352 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  d ){.    return;
24353 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e  .  }.  if( !db->
24354 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
24355 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
24356 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
24357 65 2c 20 70 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  e, p);.  }..  /*
24358 20 4c 6f 63 61 74 65 20 74 68 65 20 65 6e 64 20   Locate the end 
24359 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49  of the CREATE VI
2435a 45 57 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 4d  EW statement.  M
2435b 61 6b 65 20 73 45 6e 64 20 70 6f 69 6e 74 20 74  ake sEnd point t
2435c 6f 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 2e 0a  o.  ** the end..
2435d 20 20 2a 2f 0a 20 20 73 45 6e 64 20 3d 20 70 50    */.  sEnd = pP
2435e 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
2435f 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 73  ;.  if( ALWAYS(s
24360 45 6e 64 2e 7a 5b 30 5d 21 3d 30 29 20 26 26 20  End.z[0]!=0) && 
24361 73 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29  sEnd.z[0]!=';' )
24362 7b 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20  {.    sEnd.z += 
24363 73 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45  sEnd.n;.  }.  sE
24364 6e 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20  nd.n = 0;.  n = 
24365 28 69 6e 74 29 28 73 45 6e 64 2e 7a 20 2d 20 70  (int)(sEnd.z - p
24366 42 65 67 69 6e 2d 3e 7a 29 3b 0a 20 20 7a 20 3d  Begin->z);.  z =
24367 20 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68   pBegin->z;.  wh
24368 69 6c 65 28 20 41 4c 57 41 59 53 28 6e 3e 30 29  ile( ALWAYS(n>0)
24369 20 26 26 20 73 71 6c 69 74 65 33 49 73 73 70 61   && sqlite3Isspa
2436a 63 65 28 7a 5b 6e 2d 31 5d 29 20 29 7b 20 6e 2d  ce(z[n-1]) ){ n-
2436b 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d 20  -; }.  sEnd.z = 
2436c 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 2e  &z[n-1];.  sEnd.
2436d 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 65  n = 1;..  /* Use
2436e 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65   sqlite3EndTable
2436f 28 29 20 74 6f 20 61 64 64 20 74 68 65 20 76 69  () to add the vi
24370 65 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54 45  ew to the SQLITE
24371 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a 2f  _MASTER table */
24372 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62  .  sqlite3EndTab
24373 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26 73  le(pParse, 0, &s
24374 45 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 72  End, 0);.  retur
24375 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  n;.}.#endif /* S
24376 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20  QLITE_OMIT_VIEW 
24377 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  */..#if !defined
24378 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
24379 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  W) || !defined(S
2437a 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2437b 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 54  ALTABLE)./*.** T
2437c 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
2437d 72 65 20 70 54 61 62 6c 65 20 69 73 20 72 65 61  re pTable is rea
2437e 6c 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69 6c  lly a VIEW.  Fil
2437f 6c 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f  l in the names o
24380 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 73  f.** the columns
24381 20 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e 20   of the view in 
24382 74 68 65 20 70 54 61 62 6c 65 20 73 74 72 75 63  the pTable struc
24383 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 68  ture.  Return th
24384 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 65  e number.** of e
24385 72 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 72  rrors.  If an er
24386 72 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61 76  ror is seen leav
24387 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
24388 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45  ge in pParse->zE
24389 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rrMsg..*/.SQLITE
2438a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2438b 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
2438c 6e 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70 50  nNames(Parse *pP
2438d 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
2438e 62 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ble){.  Table *p
2438f 53 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 66  SelTab;   /* A f
24390 61 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77  ake table from w
24391 68 69 63 68 20 77 65 20 67 65 74 20 74 68 65 20  hich we get the 
24392 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
24393 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20  Select *pSel;   
24394 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 65    /* Copy of the
24395 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d 70   SELECT that imp
24396 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77  lements the view
24397 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d   */.  int nErr =
24398 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65   0;     /* Numbe
24399 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f  r of errors enco
2439a 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74  untered */.  int
2439b 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   n;            /
2439c 2a 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68 6f  * Temporarily ho
2439d 6c 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  lds the number o
2439e 66 20 63 75 72 73 6f 72 73 20 61 73 73 69 67 6e  f cursors assign
2439f 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ed */.  sqlite3 
243a0 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
243a1 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63  ;  /* Database c
243a2 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d 61  onnection for ma
243a3 6c 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a 20  lloc errors */. 
243a4 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f   int (*xAuth)(vo
243a5 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  id*,int,const ch
243a6 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
243a7 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
243a8 74 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61 73 73  t char*);..  ass
243a9 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 0a  ert( pTable );..
243aa 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
243ab 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
243ac 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 74  .  if( sqlite3Vt
243ad 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 50  abCallConnect(pP
243ae 61 72 73 65 2c 20 70 54 61 62 6c 65 29 20 29 7b  arse, pTable) ){
243af 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
243b0 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
243b1 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
243b2 61 62 6c 65 29 20 29 20 72 65 74 75 72 6e 20 30  able) ) return 0
243b3 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  ;.#endif..#ifnde
243b4 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
243b5 45 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74 69  EW.  /* A positi
243b6 76 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 68  ve nCol means th
243b7 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20  e columns names 
243b8 66 6f 72 20 74 68 69 73 20 76 69 65 77 20 61 72  for this view ar
243b9 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6b  e.  ** already k
243ba 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  nown..  */.  if(
243bb 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30 20   pTable->nCol>0 
243bc 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f  ) return 0;..  /
243bd 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6e 43 6f  * A negative nCo
243be 6c 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 6d  l is a special m
243bf 61 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74 68  arker meaning th
243c0 61 74 20 77 65 20 61 72 65 20 63 75 72 72 65 6e  at we are curren
243c1 74 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20  tly.  ** trying 
243c2 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 63  to compute the c
243c3 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 66  olumn names.  If
243c4 20 77 65 20 65 6e 74 65 72 20 74 68 69 73 20 72   we enter this r
243c5 6f 75 74 69 6e 65 20 77 69 74 68 0a 20 20 2a 2a  outine with.  **
243c6 20 61 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c   a negative nCol
243c7 2c 20 69 74 20 6d 65 61 6e 73 20 74 77 6f 20 6f  , it means two o
243c8 72 20 6d 6f 72 65 20 76 69 65 77 73 20 66 6f 72  r more views for
243c9 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20 74  m a loop, like t
243ca 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  his:.  **.  **  
243cb 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20 6f     CREATE VIEW o
243cc 6e 65 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46  ne AS SELECT * F
243cd 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20 20  ROM two;.  **   
243ce 20 20 43 52 45 41 54 45 20 56 49 45 57 20 74 77    CREATE VIEW tw
243cf 6f 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52  o AS SELECT * FR
243d0 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a  OM one;.  **.  *
243d1 2a 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 65 20  * Actually, the 
243d2 65 72 72 6f 72 20 61 62 6f 76 65 20 69 73 20 6e  error above is n
243d3 6f 77 20 63 61 75 67 68 74 20 70 72 69 6f 72 20  ow caught prior 
243d4 74 6f 20 72 65 61 63 68 69 6e 67 20 74 68 69 73  to reaching this
243d5 20 70 6f 69 6e 74 2e 0a 20 20 2a 2a 20 42 75 74   point..  ** But
243d6 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
243d7 65 73 74 20 69 73 20 73 74 69 6c 6c 20 69 6d 70  est is still imp
243d8 6f 72 74 61 6e 74 20 61 73 20 69 74 20 64 6f 65  ortant as it doe
243d9 73 20 63 6f 6d 65 20 75 70 0a 20 20 2a 2a 20 69  s come up.  ** i
243da 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  n the following:
243db 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 20 20  .  ** .  **     
243dc 43 52 45 41 54 45 20 54 41 42 4c 45 20 6d 61 69  CREATE TABLE mai
243dd 6e 2e 65 78 31 28 61 29 3b 0a 20 20 2a 2a 20 20  n.ex1(a);.  **  
243de 20 20 20 43 52 45 41 54 45 20 54 45 4d 50 20 56     CREATE TEMP V
243df 49 45 57 20 65 78 31 20 41 53 20 53 45 4c 45 43  IEW ex1 AS SELEC
243e0 54 20 61 20 46 52 4f 4d 20 65 78 31 3b 0a 20 20  T a FROM ex1;.  
243e1 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20  **     SELECT * 
243e2 46 52 4f 4d 20 74 65 6d 70 2e 65 78 31 3b 0a 20  FROM temp.ex1;. 
243e3 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c 65   */.  if( pTable
243e4 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20  ->nCol<0 ){.    
243e5 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
243e6 70 50 61 72 73 65 2c 20 22 76 69 65 77 20 25 73  pParse, "view %s
243e7 20 69 73 20 63 69 72 63 75 6c 61 72 6c 79 20 64   is circularly d
243e8 65 66 69 6e 65 64 22 2c 20 70 54 61 62 6c 65 2d  efined", pTable-
243e9 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  >zName);.    ret
243ea 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 1;.  }.  ass
243eb 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f  ert( pTable->nCo
243ec 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  l>=0 );..  /* If
243ed 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61 72   we get this far
243ee 2c 20 69 74 20 6d 65 61 6e 73 20 77 65 20 6e 65  , it means we ne
243ef 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68  ed to compute th
243f0 65 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a 20  e table names.. 
243f1 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68   ** Note that th
243f2 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  e call to sqlite
243f3 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65  3ResultSetOfSele
243f4 63 74 28 29 20 77 69 6c 6c 20 65 78 70 61 6e 64  ct() will expand
243f5 20 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65 6c   any.  ** "*" el
243f6 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 72 65  ements in the re
243f7 73 75 6c 74 73 20 73 65 74 20 6f 66 20 74 68 65  sults set of the
243f8 20 76 69 65 77 20 61 6e 64 20 77 69 6c 6c 20 61   view and will a
243f9 73 73 69 67 6e 20 63 75 72 73 6f 72 73 0a 20 20  ssign cursors.  
243fa 2a 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d 65 6e  ** to the elemen
243fb 74 73 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  ts of the FROM c
243fc 6c 61 75 73 65 2e 20 20 42 75 74 20 77 65 20 64  lause.  But we d
243fd 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 73 65  o not want these
243fe 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f   changes.  ** to
243ff 20 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20 20   be permanent.  
24400 53 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69  So the computati
24401 6f 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61 20  on is done on a 
24402 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45  copy of the SELE
24403 43 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  CT.  ** statemen
24404 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74  t that defines t
24405 68 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20 20  he view..  */.  
24406 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e  assert( pTable->
24407 70 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 53 65  pSelect );.  pSe
24408 6c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  l = sqlite3Selec
24409 74 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d  tDup(db, pTable-
2440a 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20  >pSelect, 0);.  
2440b 69 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 20  if( pSel ){.    
2440c 75 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69  u8 enableLookasi
2440d 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  de = db->lookasi
2440e 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 20  de.bEnabled;.   
2440f 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61   n = pParse->nTa
24410 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72  b;.    sqlite3Sr
24411 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f  cListAssignCurso
24412 72 73 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d  rs(pParse, pSel-
24413 3e 70 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62  >pSrc);.    pTab
24414 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 20  le->nCol = -1;. 
24415 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
24416 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 23  .bEnabled = 0;.#
24417 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24418 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
24419 0a 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d  .    xAuth = db-
2441a 3e 78 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e  >xAuth;.    db->
2441b 78 41 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70  xAuth = 0;.    p
2441c 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33  SelTab = sqlite3
2441d 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63  ResultSetOfSelec
2441e 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b  t(pParse, pSel);
2441f 0a 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d  .    db->xAuth =
24420 20 78 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20   xAuth;.#else.  
24421 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69    pSelTab = sqli
24422 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65  te3ResultSetOfSe
24423 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65  lect(pParse, pSe
24424 6c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 64  l);.#endif.    d
24425 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
24426 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f  abled = enableLo
24427 6f 6b 61 73 69 64 65 3b 0a 20 20 20 20 70 50 61  okaside;.    pPa
24428 72 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20  rse->nTab = n;. 
24429 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 20 29     if( pSelTab )
2442a 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
2442b 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20  pTable->aCol==0 
2442c 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d  );.      pTable-
2442d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d  >nCol = pSelTab-
2442e 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61  >nCol;.      pTa
2442f 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c  ble->aCol = pSel
24430 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20  Tab->aCol;.     
24431 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d   pSelTab->nCol =
24432 20 30 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61   0;.      pSelTa
24433 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20  b->aCol = 0;.   
24434 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
24435 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a  Table(pSelTab);.
24436 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53        pTable->pS
24437 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20  chema->flags |= 
24438 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b  DB_UnresetViews;
24439 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2443a 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d    pTable->nCol =
2443b 20 30 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b   0;.      nErr++
2443c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2443d 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2443e 64 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d 20 65  db, pSel);.  } e
2443f 6c 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b  lse {.    nErr++
24440 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
24441 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
24442 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72   */.  return nEr
24443 72 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  r;  .}.#endif /*
24444 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
24445 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21  _OMIT_VIEW) || !
24446 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
24447 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
24448 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  ) */..#ifndef SQ
24449 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f  LITE_OMIT_VIEW./
2444a 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63  *.** Clear the c
2444b 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d  olumn names from
2444c 20 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64   every VIEW in d
2444d 61 74 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a  atabase idx..*/.
2444e 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
2444f 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73  teViewResetAll(s
24450 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
24451 69 64 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  idx){.  HashElem
24452 20 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61   *i;.  if( !DbHa
24453 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64  sProperty(db, id
24454 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65  x, DB_UnresetVie
24455 77 73 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ws) ) return;.  
24456 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
24457 46 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69  First(&db->aDb[i
24458 64 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c  dx].pSchema->tbl
24459 48 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74  Hash); i;i=sqlit
2445a 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20  eHashNext(i)){. 
2445b 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
2445c 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28   sqliteHashData(
2445d 69 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  i);.    if( pTab
2445e 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
2445f 20 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f     sqliteResetCo
24460 6c 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b  lumnNames(pTab);
24461 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43  .    }.  }.  DbC
24462 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c  learProperty(db,
24463 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74   idx, DB_Unreset
24464 56 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a  Views);.}.#else.
24465 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56  # define sqliteV
24466 69 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29  iewResetAll(A,B)
24467 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
24468 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a  E_OMIT_VIEW */..
24469 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2446a 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ion is called by
2446b 20 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a   the VDBE to adj
2446c 75 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ust the internal
2446d 20 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20   schema.** used 
2446e 62 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74  by SQLite when t
2446f 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d  he btree layer m
24470 6f 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f  oves a table roo
24471 74 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72  t page. The.** r
24472 6f 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61  oot-page of a ta
24473 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20  ble or index in 
24474 64 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73  database iDb has
24475 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46   changed from iF
24476 72 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a  rom.** to iTo..*
24477 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32  *.** Ticket #172
24478 38 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74  8:  The symbol t
24479 61 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c  able might still
2447a 20 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61   contain informa
2447b 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65  tion.** on table
2447c 73 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73  s and/or indices
2447d 20 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72   that are the pr
2447e 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64  ocess of being d
2447f 65 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f  eleted..** If yo
24480 75 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f  u are unlucky, o
24481 6e 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65  ne of those dele
24482 74 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74  ted indices or t
24483 61 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68  ables might.** h
24484 61 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f  ave the same roo
24485 74 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20  tpage number as 
24486 74 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f  the real table o
24487 72 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a  r index that is.
24488 2a 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20  ** being moved. 
24489 20 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74   So we cannot st
2448a 6f 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74  op searching aft
2448b 65 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  er the first mat
2448c 63 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74  ch .** because t
2448d 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d  he first match m
2448e 69 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20  ight be for one 
2448f 6f 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69  of the deleted i
24490 6e 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62  ndices.** or tab
24491 6c 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20  les and not the 
24492 74 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74  table/index that
24493 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69   is actually bei
24494 6e 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20  ng moved..** We 
24495 6d 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f  must continue lo
24496 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20  oping until all 
24497 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63  tables and indic
24498 65 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70  es with.** rootp
24499 61 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20  age==iFrom have 
2449a 62 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74  been converted t
2449b 6f 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67  o have a rootpag
2449c 65 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f  e of iTo.** in o
2449d 72 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61  rder to be certa
2449e 69 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74  in that we got t
2449f 68 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f  he right one..*/
244a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
244a1 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
244a2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
244a3 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50  oid sqlite3RootP
244a4 61 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62  ageMoved(Db *pDb
244a5 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74  , int iFrom, int
244a6 20 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65   iTo){.  HashEle
244a7 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68  m *pElem;.  Hash
244a8 20 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73   *pHash;..  pHas
244a9 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d  h = &pDb->pSchem
244aa 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f  a->tblHash;.  fo
244ab 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61  r(pElem=sqliteHa
244ac 73 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20  shFirst(pHash); 
244ad 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c  pElem; pElem=sql
244ae 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65  iteHashNext(pEle
244af 6d 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  m)){.    Table *
244b0 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73  pTab = sqliteHas
244b1 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20  hData(pElem);.  
244b2 20 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d    if( pTab->tnum
244b3 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  ==iFrom ){.     
244b4 20 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54   pTab->tnum = iT
244b5 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  o;.    }.  }.  p
244b6 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63  Hash = &pDb->pSc
244b7 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20  hema->idxHash;. 
244b8 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74   for(pElem=sqlit
244b9 65 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68  eHashFirst(pHash
244ba 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d  ); pElem; pElem=
244bb 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
244bc 45 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65  Elem)){.    Inde
244bd 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65  x *pIdx = sqlite
244be 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b  HashData(pElem);
244bf 0a 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74  .    if( pIdx->t
244c0 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20  num==iFrom ){.  
244c1 20 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d      pIdx->tnum =
244c2 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   iTo;.    }.  }.
244c3 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
244c4 57 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72  Write code to er
244c5 61 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69  ase the table wi
244c6 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61  th root-page iTa
244c7 62 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73  ble from databas
244c8 65 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77  e iDb..** Also w
244c9 72 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64  rite code to mod
244ca 69 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  ify the sqlite_m
244cb 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20  aster table and 
244cc 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a  internal schema.
244cd 2a 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67  ** if a root-pag
244ce 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62  e of another tab
244cf 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74  le is moved by t
244d0 68 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77  he btree-layer w
244d1 68 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67  hilst.** erasing
244d2 20 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61   iTable (this ca
244d3 6e 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e  n happen with an
244d4 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74   auto-vacuum dat
244d5 61 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74  abase)..*/ .stat
244d6 69 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52  ic void destroyR
244d7 6f 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70  ootPage(Parse *p
244d8 50 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c  Parse, int iTabl
244d9 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56  e, int iDb){.  V
244da 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
244db 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
244dc 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69  .  int r1 = sqli
244dd 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
244de 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
244df 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
244e0 5f 44 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65  _Destroy, iTable
244e1 2c 20 72 31 2c 20 69 44 62 29 3b 0a 20 20 73 71  , r1, iDb);.  sq
244e2 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50  lite3MayAbort(pP
244e3 61 72 73 65 29 3b 0a 23 69 66 6e 64 65 66 20 53  arse);.#ifndef S
244e4 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
244e5 41 43 55 55 4d 0a 20 20 2f 2a 20 4f 50 5f 44 65  ACUUM.  /* OP_De
244e6 73 74 72 6f 79 20 73 74 6f 72 65 73 20 61 6e 20  stroy stores an 
244e7 69 6e 20 69 6e 74 65 67 65 72 20 72 31 2e 20 49  in integer r1. I
244e8 66 20 74 68 69 73 20 69 6e 74 65 67 65 72 0a 20  f this integer. 
244e9 20 2a 2a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c   ** is non-zero,
244ea 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20   then it is the 
244eb 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
244ec 20 6f 66 20 61 20 74 61 62 6c 65 20 6d 6f 76 65   of a table move
244ed 64 20 74 6f 0a 20 20 2a 2a 20 6c 6f 63 61 74 69  d to.  ** locati
244ee 6f 6e 20 69 54 61 62 6c 65 2e 20 54 68 65 20 66  on iTable. The f
244ef 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6d 6f  ollowing code mo
244f0 64 69 66 69 65 73 20 74 68 65 20 73 71 6c 69 74  difies the sqlit
244f1 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 74  e_master table t
244f2 6f 0a 20 20 2a 2a 20 72 65 66 6c 65 63 74 20 74  o.  ** reflect t
244f3 68 69 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  his..  **.  ** T
244f4 68 65 20 22 23 4e 4e 4e 22 20 69 6e 20 74 68 65  he "#NNN" in the
244f5 20 53 51 4c 20 69 73 20 61 20 73 70 65 63 69 61   SQL is a specia
244f6 6c 20 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20  l constant that 
244f7 6d 65 61 6e 73 20 77 68 61 74 65 76 65 72 20 76  means whatever v
244f8 61 6c 75 65 0a 20 20 2a 2a 20 69 73 20 69 6e 20  alue.  ** is in 
244f9 72 65 67 69 73 74 65 72 20 4e 4e 4e 2e 20 20 53  register NNN.  S
244fa 65 65 20 67 72 61 6d 6d 61 72 20 72 75 6c 65 73  ee grammar rules
244fb 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
244fc 20 74 68 65 20 54 4b 5f 52 45 47 49 53 54 45 52   the TK_REGISTER
244fd 0a 20 20 2a 2a 20 74 6f 6b 65 6e 20 66 6f 72 20  .  ** token for 
244fe 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
244ff 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73  mation..  */.  s
24500 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
24501 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  e(pParse, .     
24502 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 53 45  "UPDATE %Q.%s SE
24503 54 20 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48  T rootpage=%d WH
24504 45 52 45 20 23 25 64 20 41 4e 44 20 72 6f 6f 74  ERE #%d AND root
24505 70 61 67 65 3d 23 25 64 22 2c 0a 20 20 20 20 20  page=#%d",.     
24506 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b  pParse->db->aDb[
24507 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45  iDb].zName, SCHE
24508 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 69  MA_TABLE(iDb), i
24509 54 61 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a  Table, r1, r1);.
2450a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
2450b 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2450c 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f  Parse, r1);.}../
2450d 2a 0a 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20  *.** Write VDBE 
2450e 63 6f 64 65 20 74 6f 20 65 72 61 73 65 20 74 61  code to erase ta
2450f 62 6c 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c  ble pTab and all
24510 20 61 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69   associated indi
24511 63 65 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20  ces on disk..** 
24512 43 6f 64 65 20 74 6f 20 75 70 64 61 74 65 20 74  Code to update t
24513 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
24514 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65   tables and inte
24515 72 6e 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69  rnal schema defi
24516 6e 69 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61  nitions.** in ca
24517 73 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62  se a root-page b
24518 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74  elonging to anot
24519 68 65 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76  her table is mov
2451a 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20  ed by the btree 
2451b 6c 61 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f  layer.** is also
2451c 20 61 64 64 65 64 20 28 74 68 69 73 20 63 61 6e   added (this can
2451d 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20   happen with an 
2451e 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
2451f 62 61 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  base)..*/.static
24520 20 76 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62   void destroyTab
24521 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  le(Parse *pParse
24522 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
24523 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
24524 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
24525 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69  Index *pIdx;.  i
24526 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  nt iDb = sqlite3
24527 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
24528 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
24529 70 53 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74  pSchema);.  dest
2452a 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72  royRootPage(pPar
2452b 73 65 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  se, pTab->tnum, 
2452c 69 44 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78  iDb);.  for(pIdx
2452d 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
2452e 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
2452f 70 4e 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74  pNext){.    dest
24530 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72  royRootPage(pPar
24531 73 65 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  se, pIdx->tnum, 
24532 69 44 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  iDb);.  }.#else.
24533 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61    /* If the data
24534 62 61 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f  base may be auto
24535 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20  -vacuum capable 
24536 28 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  (if SQLITE_OMIT_
24537 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20  AUTOVACUUM.  ** 
24538 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
24539 20 74 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f   then it is impo
2453a 72 74 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50  rtant to call OP
2453b 5f 44 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a  _Destroy on the.
2453c 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69    ** table and i
2453d 6e 64 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20  ndex root-pages 
2453e 69 6e 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69  in order, starti
2453f 6e 67 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65  ng with the nume
24540 72 69 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61  rically .  ** la
24541 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20  rgest root-page 
24542 6e 75 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61  number. This gua
24543 72 61 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e  rantees that non
24544 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61  e of the root-pa
24545 67 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64  ges.  ** to be d
24546 65 73 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f  estroyed is relo
24547 63 61 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c  cated by an earl
24548 69 65 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20  ier OP_Destroy. 
24549 69 2e 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a  i.e. if the.  **
2454a 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20   following were 
2454b 63 6f 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  coded:.  **.  **
2454c 20 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a   OP_Destroy 4 0.
2454d 20 20 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50    ** ....  ** OP
2454e 5f 44 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a  _Destroy 5 0.  *
2454f 2a 0a 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20  *.  ** and root 
24550 70 61 67 65 20 35 20 68 61 70 70 65 6e 65 64 20  page 5 happened 
24551 74 6f 20 62 65 20 74 68 65 20 6c 61 72 67 65 73  to be the larges
24552 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62  t root-page numb
24553 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64  er in the.  ** d
24554 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f  atabase, then ro
24555 6f 74 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20  ot page 5 would 
24556 62 65 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65  be moved to page
24557 20 34 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20   4 by the .  ** 
24558 22 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22  "OP_Destroy 4 0"
24559 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62   opcode. The sub
2455a 73 65 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74  sequent "OP_Dest
2455b 72 6f 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68  roy 5 0" would h
2455c 69 74 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c  it.  ** a free-l
2455d 69 73 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20  ist page..  */. 
2455e 20 69 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62   int iTab = pTab
2455f 2d 3e 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44  ->tnum;.  int iD
24560 65 73 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20  estroyed = 0;.. 
24561 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20   while( 1 ){.   
24562 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
24563 20 20 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d    int iLargest =
24564 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65   0;..    if( iDe
24565 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54  stroyed==0 || iT
24566 61 62 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b  ab<iDestroyed ){
24567 0a 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20  .      iLargest 
24568 3d 20 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20  = iTab;.    }.  
24569 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
2456a 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
2456b 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
2456c 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 49 64 78  {.      int iIdx
2456d 20 3d 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20   = pIdx->tnum;. 
2456e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
2456f 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62  x->pSchema==pTab
24570 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20  ->pSchema );.   
24571 20 20 20 69 66 28 20 28 69 44 65 73 74 72 6f 79     if( (iDestroy
24572 65 64 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69  ed==0 || (iIdx<i
24573 44 65 73 74 72 6f 79 65 64 29 29 20 26 26 20 69  Destroyed)) && i
24574 49 64 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a  Idx>iLargest ){.
24575 20 20 20 20 20 20 20 20 69 4c 61 72 67 65 73 74          iLargest
24576 20 3d 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d   = iIdx;.      }
24577 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
24578 4c 61 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20  Largest==0 ){.  
24579 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2457a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
2457b 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
2457c 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
2457d 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
2457e 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 64 65  chema);.      de
2457f 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50  stroyRootPage(pP
24580 61 72 73 65 2c 20 69 4c 61 72 67 65 73 74 2c 20  arse, iLargest, 
24581 69 44 62 29 3b 0a 20 20 20 20 20 20 69 44 65 73  iDb);.      iDes
24582 74 72 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73  troyed = iLarges
24583 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  t;.    }.  }.#en
24584 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  dif.}../*.** Thi
24585 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
24586 6c 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f  led to do the wo
24587 72 6b 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42  rk of a DROP TAB
24588 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  LE statement..**
24589 20 70 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61   pName is the na
2458a 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
2458b 74 6f 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a  to be dropped..*
2458c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2458d 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
2458e 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50  pTable(Parse *pP
2458f 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
24590 4e 61 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77  Name, int isView
24591 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20  , int noErr){.  
24592 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56  Table *pTab;.  V
24593 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65  dbe *v;.  sqlite
24594 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
24595 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a  db;.  int iDb;..
24596 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
24597 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
24598 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
24599 6c 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  le;.  }.  assert
2459a 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d  ( pParse->nErr==
2459b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2459c 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  Name->nSrc==1 );
2459d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
2459e 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
2459f 72 73 65 2c 20 69 73 56 69 65 77 2c 20 0a 20 20  rse, isView, .  
245a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
245a1 20 20 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d            pName-
245a2 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61  >a[0].zName, pNa
245a3 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61  me->a[0].zDataba
245a4 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62  se);..  if( pTab
245a5 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  ==0 ){.    if( n
245a6 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71  oErr ){.      sq
245a7 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28  lite3ErrorClear(
245a8 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20  pParse);.    }. 
245a9 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
245aa 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69  p_table;.  }.  i
245ab 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
245ac 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
245ad 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
245ae 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26  assert( iDb>=0 &
245af 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  & iDb<db->nDb );
245b0 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69  ..  /* If pTab i
245b1 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  s a virtual tabl
245b2 65 2c 20 63 61 6c 6c 20 56 69 65 77 47 65 74 43  e, call ViewGetC
245b3 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 74 6f 20  olumnNames() to 
245b4 65 6e 73 75 72 65 0a 20 20 2a 2a 20 69 74 20 69  ensure.  ** it i
245b5 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20  s initialized.. 
245b6 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74   */.  if( IsVirt
245b7 75 61 6c 28 70 54 61 62 29 20 26 26 20 73 71 6c  ual(pTab) && sql
245b8 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
245b9 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
245ba 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Tab) ){.    goto
245bb 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65   exit_drop_table
245bc 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  ;.  }.#ifndef SQ
245bd 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
245be 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20  IZATION.  {.    
245bf 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 6f  int code;.    co
245c0 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d  nst char *zTab =
245c1 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
245c2 62 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  b);.    const ch
245c3 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
245c4 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
245c5 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41    const char *zA
245c6 72 67 32 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  rg2 = 0;.    if(
245c7 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
245c8 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
245c9 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30  _DELETE, zTab, 0
245ca 2c 20 7a 44 62 29 29 7b 0a 20 20 20 20 20 20 67  , zDb)){.      g
245cb 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
245cc 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ble;.    }.    i
245cd 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20  f( isView ){.   
245ce 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
245cf 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b  PDB && iDb==1 ){
245d0 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
245d1 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
245d2 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c  _VIEW;.      }el
245d3 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65  se{.        code
245d4 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56   = SQLITE_DROP_V
245d5 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66  IEW;.      }.#if
245d6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
245d7 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
245d8 20 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56 69    }else if( IsVi
245d9 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
245da 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
245db 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 3b 0a  TE_DROP_VTABLE;.
245dc 20 20 20 20 20 20 7a 41 72 67 32 20 3d 20 73 71        zArg2 = sq
245dd 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64  lite3GetVTable(d
245de 62 2c 20 70 54 61 62 29 2d 3e 70 4d 6f 64 2d 3e  b, pTab)->pMod->
245df 7a 4e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20  zName;.#endif.  
245e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
245e1 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
245e2 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20  && iDb==1 ){.   
245e3 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
245e4 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42  TE_DROP_TEMP_TAB
245e5 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  LE;.      }else{
245e6 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
245e7 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c  SQLITE_DROP_TABL
245e8 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  E;.      }.    }
245e9 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
245ea 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
245eb 2c 20 63 6f 64 65 2c 20 70 54 61 62 2d 3e 7a 4e  , code, pTab->zN
245ec 61 6d 65 2c 20 7a 41 72 67 32 2c 20 7a 44 62 29  ame, zArg2, zDb)
245ed 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   ){.      goto e
245ee 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a  xit_drop_table;.
245ef 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
245f0 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
245f1 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45  Parse, SQLITE_DE
245f2 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  LETE, pTab->zNam
245f3 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20  e, 0, zDb) ){.  
245f4 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
245f5 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a  op_table;.    }.
245f6 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28    }.#endif.  if(
245f7 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
245f8 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73  (pTab->zName, "s
245f9 71 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29  qlite_", 7)==0 )
245fa 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
245fb 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
245fc 61 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20  able %s may not 
245fd 62 65 20 64 72 6f 70 70 65 64 22 2c 20 70 54 61  be dropped", pTa
245fe 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  b->zName);.    g
245ff 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
24600 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  ble;.  }..#ifnde
24601 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
24602 45 57 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 44  EW.  /* Ensure D
24603 52 4f 50 20 54 41 42 4c 45 20 69 73 20 6e 6f 74  ROP TABLE is not
24604 20 75 73 65 64 20 6f 6e 20 61 20 76 69 65 77 2c   used on a view,
24605 20 61 6e 64 20 44 52 4f 50 20 56 49 45 57 20 69   and DROP VIEW i
24606 73 20 6e 6f 74 20 75 73 65 64 0a 20 20 2a 2a 20  s not used.  ** 
24607 6f 6e 20 61 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  on a table..  */
24608 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 26 26  .  if( isView &&
24609 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d   pTab->pSelect==
2460a 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
2460b 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2460c 20 22 75 73 65 20 44 52 4f 50 20 54 41 42 4c 45   "use DROP TABLE
2460d 20 74 6f 20 64 65 6c 65 74 65 20 74 61 62 6c 65   to delete table
2460e 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d   %s", pTab->zNam
2460f 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  e);.    goto exi
24610 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
24611 7d 0a 20 20 69 66 28 20 21 69 73 56 69 65 77 20  }.  if( !isView 
24612 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  && pTab->pSelect
24613 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
24614 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
24615 22 75 73 65 20 44 52 4f 50 20 56 49 45 57 20 74  "use DROP VIEW t
24616 6f 20 64 65 6c 65 74 65 20 76 69 65 77 20 25 73  o delete view %s
24617 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
24618 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
24619 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23  rop_table;.  }.#
2461a 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65  endif..  /* Gene
2461b 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6d  rate code to rem
2461c 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20 66 72  ove the table fr
2461d 6f 6d 20 74 68 65 20 6d 61 73 74 65 72 20 74 61  om the master ta
2461e 62 6c 65 0a 20 20 2a 2a 20 6f 6e 20 64 69 73 6b  ble.  ** on disk
2461f 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c  ..  */.  v = sql
24620 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
24621 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  se);.  if( v ){.
24622 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 54 72      Trigger *pTr
24623 69 67 67 65 72 3b 0a 20 20 20 20 44 62 20 2a 70  igger;.    Db *p
24624 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  Db = &db->aDb[iD
24625 62 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  b];.    sqlite3B
24626 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
24627 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44  on(pParse, 1, iD
24628 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  b);..#ifndef SQL
24629 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2462a 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 49 73  TABLE.    if( Is
2462b 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
2462c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2462d 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56  beAddOp0(v, OP_V
2462e 42 65 67 69 6e 29 3b 0a 20 20 20 20 7d 0a 23 65  Begin);.    }.#e
2462f 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33  ndif.    sqlite3
24630 46 6b 44 72 6f 70 54 61 62 6c 65 28 70 50 61 72  FkDropTable(pPar
24631 73 65 2c 20 70 4e 61 6d 65 2c 20 70 54 61 62 29  se, pName, pTab)
24632 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61  ;..    /* Drop a
24633 6c 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f  ll triggers asso
24634 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
24635 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70  table being drop
24636 70 65 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a  ped. Code.    **
24637 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f   is generated to
24638 20 72 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20   remove entries 
24639 66 72 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74  from sqlite_mast
2463a 65 72 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a  er and/or.    **
2463b 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73   sqlite_temp_mas
2463c 74 65 72 20 69 66 20 72 65 71 75 69 72 65 64 2e  ter if required.
2463d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69  .    */.    pTri
2463e 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72  gger = sqlite3Tr
2463f 69 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65  iggerList(pParse
24640 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 77 68 69  , pTab);.    whi
24641 6c 65 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a  le( pTrigger ){.
24642 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
24643 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d  rigger->pSchema=
24644 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 7c  =pTab->pSchema |
24645 7c 20 0a 20 20 20 20 20 20 20 20 20 20 70 54 72  | .          pTr
24646 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d  igger->pSchema==
24647 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65  db->aDb[1].pSche
24648 6d 61 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ma );.      sqli
24649 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74  te3DropTriggerPt
2464a 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67  r(pParse, pTrigg
2464b 65 72 29 3b 0a 20 20 20 20 20 20 70 54 72 69 67  er);.      pTrig
2464c 67 65 72 20 3d 20 70 54 72 69 67 67 65 72 2d 3e  ger = pTrigger->
2464d 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 23 69  pNext;.    }..#i
2464e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2464f 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a  T_AUTOINCREMENT.
24650 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e      /* Remove an
24651 79 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65  y entries of the
24652 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
24653 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65   table associate
24654 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68  d with.    ** th
24655 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72  e table being dr
24656 6f 70 70 65 64 2e 20 54 68 69 73 20 69 73 20 64  opped. This is d
24657 6f 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 74  one before the t
24658 61 62 6c 65 20 69 73 20 64 72 6f 70 70 65 64 0a  able is dropped.
24659 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20 62 74      ** at the bt
2465a 72 65 65 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61  ree level, in ca
2465b 73 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65  se the sqlite_se
2465c 71 75 65 6e 63 65 20 74 61 62 6c 65 20 6e 65 65  quence table nee
2465d 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76  ds to.    ** mov
2465e 65 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66  e as a result of
2465f 20 74 68 65 20 64 72 6f 70 20 28 63 61 6e 20 68   the drop (can h
24660 61 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61  appen in auto-va
24661 63 75 75 6d 20 6d 6f 64 65 29 2e 0a 20 20 20 20  cuum mode)..    
24662 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  */.    if( pTab-
24663 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41  >tabFlags & TF_A
24664 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a  utoincrement ){.
24665 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73        sqlite3Nes
24666 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
24667 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45  .        "DELETE
24668 20 46 52 4f 4d 20 25 73 2e 73 71 6c 69 74 65 5f   FROM %s.sqlite_
24669 73 65 71 75 65 6e 63 65 20 57 48 45 52 45 20 6e  sequence WHERE n
2466a 61 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 20 20  ame=%Q",.       
2466b 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61   pDb->zName, pTa
2466c 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29  b->zName.      )
2466d 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ;.    }.#endif..
2466e 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20      /* Drop all 
2466f 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61  SQLITE_MASTER ta
24670 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 65 6e  ble and index en
24671 74 72 69 65 73 20 74 68 61 74 20 72 65 66 65 72  tries that refer
24672 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 74   to the.    ** t
24673 61 62 6c 65 2e 20 54 68 65 20 70 72 6f 67 72 61  able. The progra
24674 6d 20 6e 61 6d 65 20 6c 6f 6f 70 73 20 74 68 72  m name loops thr
24675 6f 75 67 68 20 74 68 65 20 6d 61 73 74 65 72 20  ough the master 
24676 74 61 62 6c 65 20 61 6e 64 20 64 65 6c 65 74 65  table and delete
24677 73 0a 20 20 20 20 2a 2a 20 65 76 65 72 79 20 72  s.    ** every r
24678 6f 77 20 74 68 61 74 20 72 65 66 65 72 73 20 74  ow that refers t
24679 6f 20 61 20 74 61 62 6c 65 20 6f 66 20 74 68 65  o a table of the
2467a 20 73 61 6d 65 20 6e 61 6d 65 20 61 73 20 74 68   same name as th
2467b 65 20 6f 6e 65 20 62 65 69 6e 67 0a 20 20 20 20  e one being.    
2467c 2a 2a 20 64 72 6f 70 70 65 64 2e 20 54 72 69 67  ** dropped. Trig
2467d 67 65 72 73 20 61 72 65 20 68 61 6e 64 6c 65 64  gers are handled
2467e 20 73 65 70 65 72 61 74 65 6c 79 20 62 65 63 61   seperately beca
2467f 75 73 65 20 61 20 74 72 69 67 67 65 72 20 63 61  use a trigger ca
24680 6e 20 62 65 0a 20 20 20 20 2a 2a 20 63 72 65 61  n be.    ** crea
24681 74 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70 20  ted in the temp 
24682 64 61 74 61 62 61 73 65 20 74 68 61 74 20 72 65  database that re
24683 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 20  fers to a table 
24684 69 6e 20 61 6e 6f 74 68 65 72 0a 20 20 20 20 2a  in another.    *
24685 2a 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20  * database..    
24686 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  */.    sqlite3Ne
24687 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
24688 2c 20 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45  , .        "DELE
24689 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48  TE FROM %Q.%s WH
2468a 45 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20  ERE tbl_name=%Q 
2468b 61 6e 64 20 74 79 70 65 21 3d 27 74 72 69 67 67  and type!='trigg
2468c 65 72 27 22 2c 0a 20 20 20 20 20 20 20 20 70 44  er'",.        pD
2468d 62 2d 3e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b->zName, SCHEMA
2468e 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 54 61  _TABLE(iDb), pTa
2468f 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20  b->zName);..    
24690 2f 2a 20 44 72 6f 70 20 61 6e 79 20 73 74 61 74  /* Drop any stat
24691 69 73 74 69 63 73 20 66 72 6f 6d 20 74 68 65 20  istics from the 
24692 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
24693 6c 65 2c 20 69 66 20 69 74 20 65 78 69 73 74 73  le, if it exists
24694 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69   */.    if( sqli
24695 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
24696 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c   "sqlite_stat1",
24697 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
24698 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71  ame) ){.      sq
24699 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2469a 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
2469b 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51   "DELETE FROM %Q
2469c 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 20 57 48  .sqlite_stat1 WH
2469d 45 52 45 20 74 62 6c 3d 25 51 22 2c 20 70 44 62  ERE tbl=%Q", pDb
2469e 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a  ->zName, pTab->z
2469f 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20  Name.      );.  
246a0 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 69 73    }..    if( !is
246a1 56 69 65 77 20 26 26 20 21 49 73 56 69 72 74 75  View && !IsVirtu
246a2 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
246a3 20 20 64 65 73 74 72 6f 79 54 61 62 6c 65 28 70    destroyTable(p
246a4 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
246a5 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f    }..    /* Remo
246a6 76 65 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74  ve the table ent
246a7 72 79 20 66 72 6f 6d 20 53 51 4c 69 74 65 27 73  ry from SQLite's
246a8 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61   internal schema
246a9 20 61 6e 64 20 6d 6f 64 69 66 79 0a 20 20 20 20   and modify.    
246aa 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  ** the schema co
246ab 6f 6b 69 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  okie..    */.   
246ac 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
246ad 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71  Tab) ){.      sq
246ae 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
246af 76 2c 20 4f 50 5f 56 44 65 73 74 72 6f 79 2c 20  v, OP_VDestroy, 
246b0 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d  iDb, 0, 0, pTab-
246b1 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
246b2 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
246b3 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72  eAddOp4(v, OP_Dr
246b4 6f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c  opTable, iDb, 0,
246b5 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c   0, pTab->zName,
246b6 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
246b7 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61  ChangeCookie(pPa
246b8 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20  rse, iDb);.  }. 
246b9 20 73 71 6c 69 74 65 56 69 65 77 52 65 73 65 74   sqliteViewReset
246ba 41 6c 6c 28 64 62 2c 20 69 44 62 29 3b 0a 0a 65  All(db, iDb);..e
246bb 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3a 0a  xit_drop_table:.
246bc 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
246bd 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d 65  Delete(db, pName
246be 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
246bf 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
246c0 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  ed to create a n
246c1 65 77 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f  ew foreign key o
246c2 6e 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63  n the table.** c
246c3 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63  urrently under c
246c4 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 70 46  onstruction.  pF
246c5 72 6f 6d 43 6f 6c 20 64 65 74 65 72 6d 69 6e 65  romCol determine
246c6 73 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 0a  s which columns.
246c7 2a 2a 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e  ** in the curren
246c8 74 20 74 61 62 6c 65 20 70 6f 69 6e 74 20 74 6f  t table point to
246c9 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
246ca 2e 20 20 49 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d  .  If pFromCol==
246cb 30 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63  0 then.** connec
246cc 74 20 74 68 65 20 6b 65 79 20 74 6f 20 74 68 65  t the key to the
246cd 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 73   last column ins
246ce 65 72 74 65 64 2e 20 20 70 54 6f 20 69 73 20 74  erted.  pTo is t
246cf 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68  he name of.** th
246d0 65 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64  e table referred
246d1 20 74 6f 2e 20 20 70 54 6f 43 6f 6c 20 69 73 20   to.  pToCol is 
246d2 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 73  a list of tables
246d3 20 69 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a   in the other.**
246d4 20 70 54 6f 20 74 61 62 6c 65 20 74 68 61 74 20   pTo table that 
246d5 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  the foreign key 
246d6 70 6f 69 6e 74 73 20 74 6f 2e 20 20 66 6c 61 67  points to.  flag
246d7 73 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a  s contains all.*
246d8 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  * information ab
246d9 6f 75 74 20 74 68 65 20 63 6f 6e 66 6c 69 63 74  out the conflict
246da 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f   resolution algo
246db 72 69 74 68 6d 73 20 73 70 65 63 69 66 69 65 64  rithms specified
246dc 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20 44 45  .** in the ON DE
246dd 4c 45 54 45 2c 20 4f 4e 20 55 50 44 41 54 45 20  LETE, ON UPDATE 
246de 61 6e 64 20 4f 4e 20 49 4e 53 45 52 54 20 63 6c  and ON INSERT cl
246df 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  auses..**.** An 
246e0 46 4b 65 79 20 73 74 72 75 63 74 75 72 65 20 69  FKey structure i
246e1 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 61 64  s created and ad
246e2 64 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ded to the table
246e3 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e   currently.** un
246e4 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
246e5 20 69 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e   in the pParse->
246e6 70 4e 65 77 54 61 62 6c 65 20 66 69 65 6c 64 2e  pNewTable field.
246e7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 65 69  .**.** The forei
246e8 67 6e 20 6b 65 79 20 69 73 20 73 65 74 20 66 6f  gn key is set fo
246e9 72 20 49 4d 4d 45 44 49 41 54 45 20 70 72 6f 63  r IMMEDIATE proc
246ea 65 73 73 69 6e 67 2e 20 20 41 20 73 75 62 73 65  essing.  A subse
246eb 71 75 65 6e 74 20 63 61 6c 6c 0a 2a 2a 20 74 6f  quent call.** to
246ec 20 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72   sqlite3DeferFor
246ed 65 69 67 6e 4b 65 79 28 29 20 6d 69 67 68 74 20  eignKey() might 
246ee 63 68 61 6e 67 65 20 74 68 69 73 20 74 6f 20 44  change this to D
246ef 45 46 45 52 52 45 44 2e 0a 2a 2f 0a 53 51 4c 49  EFERRED..*/.SQLI
246f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
246f1 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72  sqlite3CreateFor
246f2 65 69 67 6e 4b 65 79 28 0a 20 20 50 61 72 73 65  eignKey(.  Parse
246f3 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
246f4 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
246f5 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
246f6 20 2a 70 46 72 6f 6d 43 6f 6c 2c 20 20 2f 2a 20   *pFromCol,  /* 
246f7 43 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20  Columns in this 
246f8 74 61 62 6c 65 20 74 68 61 74 20 70 6f 69 6e 74  table that point
246f9 20 74 6f 20 6f 74 68 65 72 20 74 61 62 6c 65 20   to other table 
246fa 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c  */.  Token *pTo,
246fb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
246fc 65 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74  e of the other t
246fd 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  able */.  ExprLi
246fe 73 74 20 2a 70 54 6f 43 6f 6c 2c 20 20 20 20 2f  st *pToCol,    /
246ff 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  * Columns in the
24700 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a   other table */.
24701 20 20 69 6e 74 20 66 6c 61 67 73 20 20 20 20 20    int flags     
24702 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69         /* Confli
24703 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c  ct resolution al
24704 67 6f 72 69 74 68 6d 73 2e 20 2a 2f 0a 29 7b 0a  gorithms. */.){.
24705 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
24706 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 6e  pParse->db;.#ifn
24707 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
24708 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 46 4b  FOREIGN_KEY.  FK
24709 65 79 20 2a 70 46 4b 65 79 20 3d 20 30 3b 0a 20  ey *pFKey = 0;. 
2470a 20 46 4b 65 79 20 2a 70 4e 65 78 74 54 6f 3b 0a   FKey *pNextTo;.
2470b 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 70 50 61    Table *p = pPa
2470c 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
2470d 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69    int nByte;.  i
2470e 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c  nt i;.  int nCol
2470f 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20  ;.  char *z;..  
24710 61 73 73 65 72 74 28 20 70 54 6f 21 3d 30 20 29  assert( pTo!=0 )
24711 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
24712 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
24713 29 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20  ) goto fk_end;. 
24714 20 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30   if( pFromCol==0
24715 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c   ){.    int iCol
24716 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20   = p->nCol-1;.  
24717 20 20 69 66 28 20 4e 45 56 45 52 28 69 43 6f 6c    if( NEVER(iCol
24718 3c 30 29 20 29 20 67 6f 74 6f 20 66 6b 5f 65 6e  <0) ) goto fk_en
24719 64 3b 0a 20 20 20 20 69 66 28 20 70 54 6f 43 6f  d;.    if( pToCo
2471a 6c 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78  l && pToCol->nEx
2471b 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 73  pr!=1 ){.      s
2471c 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2471d 50 61 72 73 65 2c 20 22 66 6f 72 65 69 67 6e 20  Parse, "foreign 
2471e 6b 65 79 20 6f 6e 20 25 73 22 0a 20 20 20 20 20  key on %s".     
2471f 20 20 20 20 22 20 73 68 6f 75 6c 64 20 72 65 66      " should ref
24720 65 72 65 6e 63 65 20 6f 6e 6c 79 20 6f 6e 65 20  erence only one 
24721 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20  column of table 
24722 25 54 22 2c 0a 20 20 20 20 20 20 20 20 20 70 2d  %T",.         p-
24723 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d  >aCol[iCol].zNam
24724 65 2c 20 70 54 6f 29 3b 0a 20 20 20 20 20 20 67  e, pTo);.      g
24725 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20  oto fk_end;.    
24726 7d 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 31 3b 0a  }.    nCol = 1;.
24727 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 6f 43    }else if( pToC
24728 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45  ol && pToCol->nE
24729 78 70 72 21 3d 70 46 72 6f 6d 43 6f 6c 2d 3e 6e  xpr!=pFromCol->n
2472a 45 78 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  Expr ){.    sqli
2472b 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2472c 73 65 2c 0a 20 20 20 20 20 20 20 20 22 6e 75 6d  se,.        "num
2472d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
2472e 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 6f  n foreign key do
2472f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65  es not match the
24730 20 6e 75 6d 62 65 72 20 6f 66 20 22 0a 20 20 20   number of ".   
24731 20 20 20 20 20 22 63 6f 6c 75 6d 6e 73 20 69 6e       "columns in
24732 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20   the referenced 
24733 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 67 6f 74  table");.    got
24734 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d 65 6c 73  o fk_end;.  }els
24735 65 7b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 46  e{.    nCol = pF
24736 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 3b 0a 20  romCol->nExpr;. 
24737 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a   }.  nByte = siz
24738 65 6f 66 28 2a 70 46 4b 65 79 29 20 2b 20 28 6e  eof(*pFKey) + (n
24739 43 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66 28 70 46  Col-1)*sizeof(pF
2473a 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20 2b 20  Key->aCol[0]) + 
2473b 70 54 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20 69 66  pTo->n + 1;.  if
2473c 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20  ( pToCol ){.    
2473d 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 43 6f  for(i=0; i<pToCo
2473e 6c 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  l->nExpr; i++){.
2473f 20 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73        nByte += s
24740 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
24741 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  ToCol->a[i].zNam
24742 65 29 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20  e) + 1;.    }.  
24743 7d 0a 20 20 70 46 4b 65 79 20 3d 20 73 71 6c 69  }.  pFKey = sqli
24744 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
24745 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 69  db, nByte );.  i
24746 66 28 20 70 46 4b 65 79 3d 3d 30 20 29 7b 0a 20  f( pFKey==0 ){. 
24747 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a     goto fk_end;.
24748 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 70 46 72    }.  pFKey->pFr
24749 6f 6d 20 3d 20 70 3b 0a 20 20 70 46 4b 65 79 2d  om = p;.  pFKey-
2474a 3e 70 4e 65 78 74 46 72 6f 6d 20 3d 20 70 2d 3e  >pNextFrom = p->
2474b 70 46 4b 65 79 3b 0a 20 20 7a 20 3d 20 28 63 68  pFKey;.  z = (ch
2474c 61 72 2a 29 26 70 46 4b 65 79 2d 3e 61 43 6f 6c  ar*)&pFKey->aCol
2474d 5b 6e 43 6f 6c 5d 3b 0a 20 20 70 46 4b 65 79 2d  [nCol];.  pFKey-
2474e 3e 7a 54 6f 20 3d 20 7a 3b 0a 20 20 6d 65 6d 63  >zTo = z;.  memc
2474f 70 79 28 7a 2c 20 70 54 6f 2d 3e 7a 2c 20 70 54  py(z, pTo->z, pT
24750 6f 2d 3e 6e 29 3b 0a 20 20 7a 5b 70 54 6f 2d 3e  o->n);.  z[pTo->
24751 6e 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  n] = 0;.  sqlite
24752 33 44 65 71 75 6f 74 65 28 7a 29 3b 0a 20 20 7a  3Dequote(z);.  z
24753 20 2b 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 20   += pTo->n+1;.  
24754 70 46 4b 65 79 2d 3e 6e 43 6f 6c 20 3d 20 6e 43  pFKey->nCol = nC
24755 6f 6c 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 43  ol;.  if( pFromC
24756 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 70 46 4b  ol==0 ){.    pFK
24757 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f  ey->aCol[0].iFro
24758 6d 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20  m = p->nCol-1;. 
24759 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28   }else{.    for(
2475a 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
2475b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  ){.      int j;.
2475c 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
2475d 3c 70 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a  <p->nCol; j++){.
2475e 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
2475f 74 65 33 53 74 72 49 43 6d 70 28 70 2d 3e 61 43  te3StrICmp(p->aC
24760 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 46 72  ol[j].zName, pFr
24761 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  omCol->a[i].zNam
24762 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)==0 ){.       
24763 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69     pFKey->aCol[i
24764 5d 2e 69 46 72 6f 6d 20 3d 20 6a 3b 0a 20 20 20  ].iFrom = j;.   
24765 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
24766 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
24767 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 70 2d 3e        if( j>=p->
24768 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  nCol ){.        
24769 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2476a 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
2476b 20 20 20 22 75 6e 6b 6e 6f 77 6e 20 63 6f 6c 75     "unknown colu
2476c 6d 6e 20 5c 22 25 73 5c 22 20 69 6e 20 66 6f 72  mn \"%s\" in for
2476d 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74  eign key definit
2476e 69 6f 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20  ion", .         
2476f 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e   pFromCol->a[i].
24770 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
24771 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20  goto fk_end;.   
24772 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
24773 20 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20   if( pToCol ){. 
24774 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
24775 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
24776 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53  int n = sqlite3S
24777 74 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e  trlen30(pToCol->
24778 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  a[i].zName);.   
24779 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69     pFKey->aCol[i
2477a 5d 2e 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20 20  ].zCol = z;.    
2477b 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 43    memcpy(z, pToC
2477c 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  ol->a[i].zName, 
2477d 6e 29 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d  n);.      z[n] =
2477e 20 30 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e   0;.      z += n
2477f 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  +1;.    }.  }.  
24780 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65  pFKey->isDeferre
24781 64 20 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e  d = 0;.  pFKey->
24782 61 41 63 74 69 6f 6e 5b 30 5d 20 3d 20 28 75 38  aAction[0] = (u8
24783 29 28 66 6c 61 67 73 20 26 20 30 78 66 66 29 3b  )(flags & 0xff);
24784 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
24785 4e 20 44 45 4c 45 54 45 20 61 63 74 69 6f 6e 20  N DELETE action 
24786 2a 2f 0a 20 20 70 46 4b 65 79 2d 3e 61 41 63 74  */.  pFKey->aAct
24787 69 6f 6e 5b 31 5d 20 3d 20 28 75 38 29 28 28 66  ion[1] = (u8)((f
24788 6c 61 67 73 20 3e 3e 20 38 20 29 20 26 20 30 78  lags >> 8 ) & 0x
24789 66 66 29 3b 20 20 20 20 2f 2a 20 4f 4e 20 55 50  ff);    /* ON UP
2478a 44 41 54 45 20 61 63 74 69 6f 6e 20 2a 2f 0a 0a  DATE action */..
2478b 20 20 70 4e 65 78 74 54 6f 20 3d 20 28 46 4b 65    pNextTo = (FKe
2478c 79 20 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49  y *)sqlite3HashI
2478d 6e 73 65 72 74 28 26 70 2d 3e 70 53 63 68 65 6d  nsert(&p->pSchem
2478e 61 2d 3e 66 6b 65 79 48 61 73 68 2c 20 0a 20 20  a->fkeyHash, .  
2478f 20 20 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20      pFKey->zTo, 
24790 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
24791 70 46 4b 65 79 2d 3e 7a 54 6f 29 2c 20 28 76 6f  pFKey->zTo), (vo
24792 69 64 20 2a 29 70 46 4b 65 79 0a 20 20 29 3b 0a  id *)pFKey.  );.
24793 20 20 69 66 28 20 70 4e 65 78 74 54 6f 3d 3d 70    if( pNextTo==p
24794 46 4b 65 79 20 29 7b 0a 20 20 20 20 64 62 2d 3e  FKey ){.    db->
24795 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
24796 3b 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e  ;.    goto fk_en
24797 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65  d;.  }.  if( pNe
24798 78 74 54 6f 20 29 7b 0a 20 20 20 20 61 73 73 65  xtTo ){.    asse
24799 72 74 28 20 70 4e 65 78 74 54 6f 2d 3e 70 50 72  rt( pNextTo->pPr
2479a 65 76 54 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 70  evTo==0 );.    p
2479b 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20  FKey->pNextTo = 
2479c 70 4e 65 78 74 54 6f 3b 0a 20 20 20 20 70 4e 65  pNextTo;.    pNe
2479d 78 74 54 6f 2d 3e 70 50 72 65 76 54 6f 20 3d 20  xtTo->pPrevTo = 
2479e 70 46 4b 65 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  pFKey;.  }..  /*
2479f 20 4c 69 6e 6b 20 74 68 65 20 66 6f 72 65 69 67   Link the foreig
247a0 6e 20 6b 65 79 20 74 6f 20 74 68 65 20 74 61 62  n key to the tab
247a1 6c 65 20 61 73 20 74 68 65 20 6c 61 73 74 20 73  le as the last s
247a2 74 65 70 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70  tep..  */.  p->p
247a3 46 4b 65 79 20 3d 20 70 46 4b 65 79 3b 0a 20 20  FKey = pFKey;.  
247a4 70 46 4b 65 79 20 3d 20 30 3b 0a 0a 66 6b 5f 65  pFKey = 0;..fk_e
247a5 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46  nd:.  sqlite3DbF
247a6 72 65 65 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a  ree(db, pFKey);.
247a7 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
247a8 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
247a9 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20  OREIGN_KEY) */. 
247aa 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
247ab 44 65 6c 65 74 65 28 64 62 2c 20 70 46 72 6f 6d  Delete(db, pFrom
247ac 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  Col);.  sqlite3E
247ad 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
247ae 2c 20 70 54 6f 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a  , pToCol);.}../*
247af 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
247b0 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
247b1 61 6e 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d  an INITIALLY IMM
247b2 45 44 49 41 54 45 20 6f 72 20 49 4e 49 54 49 41  EDIATE or INITIA
247b3 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 2a 2a 20  LLY DEFERRED.** 
247b4 63 6c 61 75 73 65 20 69 73 20 73 65 65 6e 20 61  clause is seen a
247b5 73 20 70 61 72 74 20 6f 66 20 61 20 66 6f 72 65  s part of a fore
247b6 69 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69  ign key definiti
247b7 6f 6e 2e 20 20 54 68 65 20 69 73 44 65 66 65 72  on.  The isDefer
247b8 72 65 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  red.** parameter
247b9 20 69 73 20 31 20 66 6f 72 20 49 4e 49 54 49 41   is 1 for INITIA
247ba 4c 4c 59 20 44 45 46 45 52 52 45 44 20 61 6e 64  LLY DEFERRED and
247bb 20 30 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59   0 for INITIALLY
247bc 20 49 4d 4d 45 44 49 41 54 45 2e 0a 2a 2a 20 54   IMMEDIATE..** T
247bd 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74  he behavior of t
247be 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
247bf 20 63 72 65 61 74 65 64 20 66 6f 72 65 69 67 6e   created foreign
247c0 20 6b 65 79 20 69 73 20 61 64 6a 75 73 74 65 64   key is adjusted
247c1 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e  .** accordingly.
247c2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
247c3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
247c4 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50  eferForeignKey(P
247c5 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
247c6 74 20 69 73 44 65 66 65 72 72 65 64 29 7b 0a 23  t isDeferred){.#
247c7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
247c8 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20  IT_FOREIGN_KEY. 
247c9 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
247ca 46 4b 65 79 20 2a 70 46 4b 65 79 3b 0a 20 20 69  FKey *pFKey;.  i
247cb 66 28 20 28 70 54 61 62 20 3d 20 70 50 61 72 73  f( (pTab = pPars
247cc 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30  e->pNewTable)==0
247cd 20 7c 7c 20 28 70 46 4b 65 79 20 3d 20 70 54 61   || (pFKey = pTa
247ce 62 2d 3e 70 46 4b 65 79 29 3d 3d 30 20 29 20 72  b->pFKey)==0 ) r
247cf 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28  eturn;.  assert(
247d0 20 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20 7c   isDeferred==0 |
247d1 7c 20 69 73 44 65 66 65 72 72 65 64 3d 3d 31 20  | isDeferred==1 
247d2 29 3b 20 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32  ); /* EV: R-3032
247d3 33 2d 32 31 39 31 37 20 2a 2f 0a 20 20 70 46 4b  3-21917 */.  pFK
247d4 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d  ey->isDeferred =
247d5 20 28 75 38 29 69 73 44 65 66 65 72 72 65 64 3b   (u8)isDeferred;
247d6 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
247d7 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
247d8 68 61 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61  hat will erase a
247d9 6e 64 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20  nd refill index 
247da 2a 70 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a  *pIdx.  This is.
247db 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69  ** used to initi
247dc 61 6c 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72  alize a newly cr
247dd 65 61 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74  eated index or t
247de 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a  o recompute the.
247df 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e  ** content of an
247e0 20 69 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e   index in respon
247e1 73 65 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20  se to a REINDEX 
247e2 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69  command..**.** i
247e3 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73  f memRootPage is
247e4 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69   not negative, i
247e5 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
247e6 20 69 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a   index is newly.
247e7 2a 2a 20 63 72 65 61 74 65 64 2e 20 20 54 68 65  ** created.  The
247e8 20 72 65 67 69 73 74 65 72 20 73 70 65 63 69 66   register specif
247e9 69 65 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61  ied by memRootPa
247ea 67 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a  ge contains the.
247eb 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  ** root page num
247ec 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ber of the index
247ed 2e 20 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67  .  If memRootPag
247ee 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74  e is negative, t
247ef 68 65 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78  hen.** the index
247f0 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
247f1 61 6e 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61  and must be clea
247f2 72 65 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67  red before being
247f3 20 72 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a   refilled and.**
247f4 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
247f5 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64  umber of the ind
247f6 65 78 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d  ex is taken from
247f7 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a   pIndex->tnum..*
247f8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
247f9 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78  lite3RefillIndex
247fa 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
247fb 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69  Index *pIndex, i
247fc 6e 74 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b  nt memRootPage){
247fd 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
247fe 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b   pIndex->pTable;
247ff 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74    /* The table t
24800 68 61 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a  hat is indexed *
24801 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70  /.  int iTab = p
24802 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20  Parse->nTab++;  
24803 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73     /* Btree curs
24804 6f 72 20 75 73 65 64 20 66 6f 72 20 70 54 61 62  or used for pTab
24805 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d   */.  int iIdx =
24806 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
24807 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75       /* Btree cu
24808 72 73 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49  rsor used for pI
24809 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64  ndex */.  int ad
2480a 64 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  dr1;            
2480b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
2480c 65 73 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f  ess of top of lo
2480d 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d  op */.  int tnum
2480e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2480f 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70         /* Root p
24810 61 67 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a  age of index */.
24811 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
24812 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24813 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
24814 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74  e into this virt
24815 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20  ual machine */. 
24816 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20   KeyInfo *pKey; 
24817 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24818 2f 2a 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69  /* KeyInfo for i
24819 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ndex */.  int re
2481a 67 49 64 78 4b 65 79 3b 20 20 20 20 20 20 20 20  gIdxKey;        
2481b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
2481c 73 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67  sters containing
2481d 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20 2a   the index key *
2481e 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72  /.  int regRecor
2481f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
24820 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
24821 6f 6c 64 69 6e 67 20 61 73 73 65 6d 62 6c 69 65  olding assemblie
24822 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a  d index record *
24823 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
24824 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
24825 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
24826 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
24827 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c  .  int iDb = sql
24828 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
24829 78 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53  x(db, pIndex->pS
2482a 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66  chema);..#ifndef
2482b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
2482c 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28  HORIZATION.  if(
2482d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2482e 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
2482f 5f 52 45 49 4e 44 45 58 2c 20 70 49 6e 64 65 78  _REINDEX, pIndex
24830 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20  ->zName, 0,.    
24831 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a    db->aDb[iDb].z
24832 4e 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65  Name ) ){.    re
24833 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
24834 0a 0a 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61  ..  /* Require a
24835 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74   write-lock on t
24836 68 65 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66  he table to perf
24837 6f 72 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69  orm this operati
24838 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54  on */.  sqlite3T
24839 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c  ableLock(pParse,
2483a 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d   iDb, pTab->tnum
2483b 2c 20 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 1, pTab->zName
2483c 29 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  );..  v = sqlite
2483d 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
2483e 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72  ;.  if( v==0 ) r
2483f 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d  eturn;.  if( mem
24840 52 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20  RootPage>=0 ){. 
24841 20 20 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f     tnum = memRoo
24842 74 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  tPage;.  }else{.
24843 20 20 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65      tnum = pInde
24844 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c  x->tnum;.    sql
24845 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24846 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d  , OP_Clear, tnum
24847 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b  , iDb);.  }.  pK
24848 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
24849 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
2484a 20 70 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69   pIndex);.  sqli
2484b 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2484c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69   OP_OpenWrite, i
2484d 49 64 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20  Idx, tnum, iDb, 
2484e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2484f 20 20 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65       (char *)pKe
24850 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P4_KEYINFO_HA
24851 4e 44 4f 46 46 29 3b 0a 20 20 69 66 28 20 6d 65  NDOFF);.  if( me
24852 6d 52 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a  mRootPage>=0 ){.
24853 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
24854 68 61 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20  hangeP5(v, 1);. 
24855 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e   }.  sqlite3Open
24856 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54  Table(pParse, iT
24857 61 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f  ab, iDb, pTab, O
24858 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 61  P_OpenRead);.  a
24859 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
2485a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2485b 65 77 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b  ewind, iTab, 0);
2485c 0a 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73  .  regRecord = s
2485d 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2485e 28 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67 49  (pParse);.  regI
2485f 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 47  dxKey = sqlite3G
24860 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28  enerateIndexKey(
24861 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20  pParse, pIndex, 
24862 69 54 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c  iTab, regRecord,
24863 20 31 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65   1);.  if( pInde
24864 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
24865 6f 6e 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  one ){.    const
24866 20 69 6e 74 20 72 65 67 52 6f 77 69 64 20 3d 20   int regRowid = 
24867 72 65 67 49 64 78 4b 65 79 20 2b 20 70 49 6e 64  regIdxKey + pInd
24868 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20  ex->nColumn;.   
24869 20 63 6f 6e 73 74 20 69 6e 74 20 6a 32 20 3d 20   const int j2 = 
2486a 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2486b 6e 74 41 64 64 72 28 76 29 20 2b 20 32 3b 0a 20  ntAddr(v) + 2;. 
2486c 20 20 20 76 6f 69 64 20 2a 20 63 6f 6e 73 74 20     void * const 
2486d 70 52 65 67 4b 65 79 20 3d 20 53 51 4c 49 54 45  pRegKey = SQLITE
2486e 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72 65 67 49  _INT_TO_PTR(regI
2486f 64 78 4b 65 79 29 3b 0a 0a 20 20 20 20 2f 2a 20  dxKey);..    /* 
24870 54 68 65 20 72 65 67 69 73 74 65 72 73 20 61 63  The registers ac
24871 63 65 73 73 65 64 20 62 79 20 74 68 65 20 4f 50  cessed by the OP
24872 5f 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65  _IsUnique opcode
24873 20 77 65 72 65 20 61 6c 6c 6f 63 61 74 65 64 0a   were allocated.
24874 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 73 71 6c      ** using sql
24875 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
24876 28 29 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65  () inside of the
24877 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
24878 49 6e 64 65 78 4b 65 79 28 29 0a 20 20 20 20 2a  IndexKey().    *
24879 2a 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 4a 75  * call above. Ju
2487a 73 74 20 62 65 66 6f 72 65 20 74 68 61 74 20 66  st before that f
2487b 75 6e 63 74 69 6f 6e 20 77 61 73 20 66 72 65 65  unction was free
2487c 64 20 74 68 65 79 20 77 65 72 65 20 72 65 6c 65  d they were rele
2487d 61 73 65 64 0a 20 20 20 20 2a 2a 20 28 6d 61 64  ased.    ** (mad
2487e 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74  e available to t
2487f 68 65 20 63 6f 6d 70 69 6c 65 72 20 66 6f 72 20  he compiler for 
24880 72 65 75 73 65 29 20 75 73 69 6e 67 20 0a 20 20  reuse) using .  
24881 20 20 2a 2a 20 73 71 6c 69 74 65 33 52 65 6c 65    ** sqlite3Rele
24882 61 73 65 54 65 6d 70 52 61 6e 67 65 28 29 2e 20  aseTempRange(). 
24883 53 6f 20 69 6e 20 73 6f 6d 65 20 77 61 79 73 20  So in some ways 
24884 68 61 76 69 6e 67 20 74 68 65 20 4f 50 5f 49 73  having the OP_Is
24885 55 6e 69 71 75 65 0a 20 20 20 20 2a 2a 20 6f 70  Unique.    ** op
24886 63 6f 64 65 20 75 73 65 20 74 68 65 20 76 61 6c  code use the val
24887 75 65 73 20 73 74 6f 72 65 64 20 77 69 74 68 69  ues stored withi
24888 6e 20 73 65 65 6d 73 20 64 61 6e 67 65 72 6f 75  n seems dangerou
24889 73 2e 20 48 6f 77 65 76 65 72 2c 20 73 69 6e 63  s. However, sinc
2488a 65 0a 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20  e.    ** we can 
2488b 62 65 20 73 75 72 65 20 74 68 61 74 20 6e 6f 20  be sure that no 
2488c 6f 74 68 65 72 20 74 65 6d 70 20 72 65 67 69 73  other temp regis
2488d 74 65 72 73 20 68 61 76 65 20 62 65 65 6e 20 61  ters have been a
2488e 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 2a 2a 20  llocated.    ** 
2488f 73 69 6e 63 65 20 73 71 6c 69 74 65 33 52 65 6c  since sqlite3Rel
24890 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 29 20  easeTempRange() 
24891 77 61 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69  was called, it i
24892 73 20 73 61 66 65 20 74 6f 20 64 6f 20 73 6f 2e  s safe to do so.
24893 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
24894 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
24895 20 4f 50 5f 49 73 55 6e 69 71 75 65 2c 20 69 49   OP_IsUnique, iI
24896 64 78 2c 20 6a 32 2c 20 72 65 67 52 6f 77 69 64  dx, j2, regRowid
24897 2c 20 70 52 65 67 4b 65 79 2c 20 50 34 5f 49 4e  , pRegKey, P4_IN
24898 54 33 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  T32);.    sqlite
24899 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28  3HaltConstraint(
2489a 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c  .        pParse,
2489b 20 4f 45 5f 41 62 6f 72 74 2c 20 22 69 6e 64 65   OE_Abort, "inde
2489c 78 65 64 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20  xed columns are 
2489d 6e 6f 74 20 75 6e 69 71 75 65 22 2c 20 50 34 5f  not unique", P4_
2489e 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20 73  STATIC);.  }.  s
2489f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
248a0 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
248a1 2c 20 69 49 64 78 2c 20 72 65 67 52 65 63 6f 72  , iIdx, regRecor
248a2 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  d);.  sqlite3Vdb
248a3 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46  eChangeP5(v, OPF
248a4 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
248a5 54 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  T);.  sqlite3Rel
248a6 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
248a7 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a  se, regRecord);.
248a8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
248a9 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
248aa 69 54 61 62 2c 20 61 64 64 72 31 2b 31 29 3b 0a  iTab, addr1+1);.
248ab 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
248ac 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
248ad 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
248ae 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp1(v, OP_Close
248af 2c 20 69 54 61 62 29 3b 0a 20 20 73 71 6c 69 74  , iTab);.  sqlit
248b0 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
248b1 4f 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78 29 3b  OP_Close, iIdx);
248b2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
248b3 20 61 20 6e 65 77 20 69 6e 64 65 78 20 66 6f 72   a new index for
248b4 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 2e 20 20   an SQL table.  
248b5 70 4e 61 6d 65 31 2e 70 4e 61 6d 65 32 20 69 73  pName1.pName2 is
248b6 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
248b7 20 69 6e 64 65 78 20 0a 2a 2a 20 61 6e 64 20 70   index .** and p
248b8 54 62 6c 4c 69 73 74 20 69 73 20 74 68 65 20 6e  TblList is the n
248b9 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
248ba 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69   that is to be i
248bb 6e 64 65 78 65 64 2e 20 20 42 6f 74 68 20 77 69  ndexed.  Both wi
248bc 6c 6c 20 0a 2a 2a 20 62 65 20 4e 55 4c 4c 20 66  ll .** be NULL f
248bd 6f 72 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79  or a primary key
248be 20 6f 72 20 61 6e 20 69 6e 64 65 78 20 74 68 61   or an index tha
248bf 74 20 69 73 20 63 72 65 61 74 65 64 20 74 6f 20  t is created to 
248c0 73 61 74 69 73 66 79 20 61 0a 2a 2a 20 55 4e 49  satisfy a.** UNI
248c1 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20  QUE constraint. 
248c2 20 49 66 20 70 54 61 62 6c 65 20 61 6e 64 20 70   If pTable and p
248c3 49 6e 64 65 78 20 61 72 65 20 4e 55 4c 4c 2c 20  Index are NULL, 
248c4 75 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  use pParse->pNew
248c5 54 61 62 6c 65 0a 2a 2a 20 61 73 20 74 68 65 20  Table.** as the 
248c6 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65  table to be inde
248c7 78 65 64 2e 20 20 70 50 61 72 73 65 2d 3e 70 4e  xed.  pParse->pN
248c8 65 77 54 61 62 6c 65 20 69 73 20 61 20 74 61 62  ewTable is a tab
248c9 6c 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 63 75  le that is.** cu
248ca 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f  rrently being co
248cb 6e 73 74 72 75 63 74 65 64 20 62 79 20 61 20 43  nstructed by a C
248cc 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
248cd 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69  ement..**.** pLi
248ce 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20  st is a list of 
248cf 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e  columns to be in
248d0 64 65 78 65 64 2e 20 20 70 4c 69 73 74 20 77 69  dexed.  pList wi
248d1 6c 6c 20 62 65 20 4e 55 4c 4c 20 69 66 20 74 68  ll be NULL if th
248d2 69 73 0a 2a 2a 20 69 73 20 61 20 70 72 69 6d 61  is.** is a prima
248d3 72 79 20 6b 65 79 20 6f 72 20 75 6e 69 71 75 65  ry key or unique
248d4 2d 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74  -constraint on t
248d5 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63  he most recent c
248d6 6f 6c 75 6d 6e 20 61 64 64 65 64 0a 2a 2a 20 74  olumn added.** t
248d7 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72  o the table curr
248d8 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
248d9 74 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2a 0a 2a  truction.  .**.*
248da 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 69  * If the index i
248db 73 20 63 72 65 61 74 65 64 20 73 75 63 63 65 73  s created succes
248dc 73 66 75 6c 6c 79 2c 20 72 65 74 75 72 6e 20 61  sfully, return a
248dd 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
248de 6e 65 77 20 49 6e 64 65 78 0a 2a 2a 20 73 74 72  new Index.** str
248df 75 63 74 75 72 65 2e 20 54 68 69 73 20 69 73 20  ucture. This is 
248e0 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 41  used by sqlite3A
248e1 64 64 50 72 69 6d 61 72 79 4b 65 79 28 29 20 74  ddPrimaryKey() t
248e2 6f 20 6d 61 72 6b 20 74 68 65 20 69 6e 64 65 78  o mark the index
248e3 0a 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65  .** as the table
248e4 73 20 70 72 69 6d 61 72 79 20 6b 65 79 20 28 49  s primary key (I
248e5 6e 64 65 78 2e 61 75 74 6f 49 6e 64 65 78 3d 3d  ndex.autoIndex==
248e6 32 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  2)..*/.SQLITE_PR
248e7 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71 6c  IVATE Index *sql
248e8 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28  ite3CreateIndex(
248e9 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
248ea 2c 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e 66  ,     /* All inf
248eb 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
248ec 68 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20 54  his parse */.  T
248ed 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20  oken *pName1,   
248ee 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20    /* First part 
248ef 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d  of index name. M
248f0 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ay be NULL */.  
248f1 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20  Token *pName2,  
248f2 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72     /* Second par
248f3 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e  t of index name.
248f4 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a   May be NULL */.
248f5 20 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c 4e    SrcList *pTblN
248f6 61 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74 6f  ame, /* Table to
248f7 20 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61 72   index. Use pPar
248f8 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 66  se->pNewTable if
248f9 20 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74   0 */.  ExprList
248fa 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41 20   *pList,   /* A 
248fb 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  list of columns 
248fc 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f  to be indexed */
248fd 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20  .  int onError, 
248fe 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72        /* OE_Abor
248ff 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45  t, OE_Ignore, OE
24900 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f  _Replace, or OE_
24901 4e 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  None */.  Token 
24902 2a 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a 20  *pStart,     /* 
24903 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e  The CREATE token
24904 20 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 69   that begins thi
24905 73 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  s statement */. 
24906 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20   Token *pEnd,   
24907 20 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20 74      /* The ")" t
24908 68 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20 43  hat closes the C
24909 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74  REATE INDEX stat
2490a 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73  ement */.  int s
2490b 6f 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f 2a  ortOrder,     /*
2490c 20 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20 70   Sort order of p
2490d 72 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e 20  rimary key when 
2490e 70 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a 20  pList==NULL */. 
2490f 20 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74 20   int ifNotExist 
24910 20 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72 6f      /* Omit erro
24911 72 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65 61  r if index alrea
24912 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a  dy exists */.){.
24913 20 20 49 6e 64 65 78 20 2a 70 52 65 74 20 3d 20    Index *pRet = 
24914 30 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  0;     /* Pointe
24915 72 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20  r to return */. 
24916 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30   Table *pTab = 0
24917 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74  ;     /* Table t
24918 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a  o be indexed */.
24919 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 20    Index *pIndex 
2491a 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e  = 0;   /* The in
2491b 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 74 65  dex to be create
2491c 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  d */.  char *zNa
2491d 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e  me = 0;     /* N
2491e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ame of the index
2491f 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b   */.  int nName;
24920 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
24921 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
24922 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20  rs in zName */. 
24923 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54 6f 6b   int i, j;.  Tok
24924 65 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20 20 20  en nullId;      
24925 20 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65 6e 20    /* Fake token 
24926 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 49 44 20  for an empty ID 
24927 6c 69 73 74 20 2a 2f 0a 20 20 44 62 46 69 78 65  list */.  DbFixe
24928 72 20 73 46 69 78 3b 20 20 20 20 20 20 20 20 2f  r sFix;        /
24929 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 6e 67 20  * For assigning 
2492a 64 61 74 61 62 61 73 65 20 6e 61 6d 65 73 20 74  database names t
2492b 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  o pTable */.  in
2492c 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b  t sortOrderMask;
2492d 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e 6f 72     /* 1 to honor
2492e 20 44 45 53 43 20 69 6e 20 69 6e 64 65 78 2e 20   DESC in index. 
2492f 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20 2a 2f   0 to ignore. */
24930 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
24931 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44   pParse->db;.  D
24932 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20  b *pDb;         
24933 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69      /* The speci
24934 66 69 63 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  fic table contai
24935 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 65 64  ning the indexed
24936 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69   database */.  i
24937 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
24938 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
24939 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61  the database tha
2493a 74 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74  t is being writt
2493b 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  en */.  Token *p
2493c 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f 2a 20  Name = 0;    /* 
2493d 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65  Unqualified name
2493e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 74 6f   of the index to
2493f 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 73 74 72   create */.  str
24940 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
24941 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20 2f 2a  m *pListItem; /*
24942 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
24943 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  r pList */.  int
24944 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 45 78   nCol;.  int nEx
24945 74 72 61 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  tra = 0;.  char 
24946 2a 7a 45 78 74 72 61 3b 0a 0a 20 20 61 73 73 65  *zExtra;..  asse
24947 72 74 28 20 70 53 74 61 72 74 3d 3d 30 20 7c 7c  rt( pStart==0 ||
24948 20 70 45 6e 64 21 3d 30 20 29 3b 20 2f 2a 20 70   pEnd!=0 ); /* p
24949 45 6e 64 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d  End must be non-
2494a 4e 55 4c 4c 20 69 66 20 70 53 74 61 72 74 20 69  NULL if pStart i
2494b 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  s */.  assert( p
2494c 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29  Parse->nErr==0 )
2494d 3b 20 20 20 20 20 20 2f 2a 20 4e 65 76 65 72 20  ;      /* Never 
2494e 63 61 6c 6c 65 64 20 77 69 74 68 20 70 72 69 6f  called with prio
2494f 72 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 66  r errors */.  if
24950 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
24951 65 64 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45  ed || IN_DECLARE
24952 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 67 6f 74  _VTAB ){.    got
24953 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24954 64 65 78 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53  dex;.  }.  if( S
24955 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
24956 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
24957 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  se) ){.    goto 
24958 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
24959 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  x;.  }..  /*.  *
2495a 2a 20 46 69 6e 64 20 74 68 65 20 74 61 62 6c 65  * Find the table
2495b 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69   that is to be i
2495c 6e 64 65 78 65 64 2e 20 20 52 65 74 75 72 6e 20  ndexed.  Return 
2495d 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 66 6f 75  early if not fou
2495e 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  nd..  */.  if( p
2495f 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a 20  TblName!=0 ){.. 
24960 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 74 77     /* Use the tw
24961 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 6e 61 6d  o-part index nam
24962 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  e to determine t
24963 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 20  he database .   
24964 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20 66 6f   ** to search fo
24965 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27 46 69  r the table. 'Fi
24966 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  x' the table nam
24967 65 20 74 6f 20 74 68 69 73 20 64 62 0a 20 20 20  e to this db.   
24968 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f 6b 69   ** before looki
24969 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c 65 2e  ng up the table.
2496a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
2496b 72 74 28 20 70 4e 61 6d 65 31 20 26 26 20 70 4e  rt( pName1 && pN
2496c 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 44 62 20  ame2 );.    iDb 
2496d 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
2496e 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
2496f 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e  me1, pName2, &pN
24970 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44  ame);.    if( iD
24971 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  b<0 ) goto exit_
24972 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 23  create_index;..#
24973 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24974 49 54 5f 54 45 4d 50 44 42 0a 20 20 20 20 2f 2a  IT_TEMPDB.    /*
24975 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 6e 61   If the index na
24976 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69 66 69  me was unqualifi
24977 65 64 2c 20 63 68 65 63 6b 20 69 66 20 74 68 65  ed, check if the
24978 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20 2a   the table.    *
24979 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c  * is a temp tabl
2497a 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68  e. If so, set th
2497b 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 31 2e  e database to 1.
2497c 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 0a   Do not do this.
2497d 20 20 20 20 2a 2a 20 69 66 20 69 6e 69 74 69 61      ** if initia
2497e 6c 69 73 69 6e 67 20 61 20 64 61 74 61 62 61 73  lising a databas
2497f 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f  e schema..    */
24980 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  .    if( !db->in
24981 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20  it.busy ){.     
24982 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53   pTab = sqlite3S
24983 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61  rcListLookup(pPa
24984 72 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a  rse, pTblName);.
24985 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32        if( pName2
24986 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62 20 26  ->n==0 && pTab &
24987 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d  & pTab->pSchema=
24988 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68  =db->aDb[1].pSch
24989 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ema ){.        i
2498a 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  Db = 1;.      }.
2498b 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
2498c 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
2498d 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72  Init(&sFix, pPar
2498e 73 65 2c 20 69 44 62 2c 20 22 69 6e 64 65 78 22  se, iDb, "index"
2498f 2c 20 70 4e 61 6d 65 29 20 26 26 0a 20 20 20 20  , pName) &&.    
24990 20 20 20 20 73 71 6c 69 74 65 33 46 69 78 53 72      sqlite3FixSr
24991 63 4c 69 73 74 28 26 73 46 69 78 2c 20 70 54 62  cList(&sFix, pTb
24992 6c 4e 61 6d 65 29 0a 20 20 20 20 29 7b 0a 20 20  lName).    ){.  
24993 20 20 20 20 2f 2a 20 42 65 63 61 75 73 65 20 74      /* Because t
24994 68 65 20 70 61 72 73 65 72 20 63 6f 6e 73 74 72  he parser constr
24995 75 63 74 73 20 70 54 62 6c 4e 61 6d 65 20 66 72  ucts pTblName fr
24996 6f 6d 20 61 20 73 69 6e 67 6c 65 20 69 64 65 6e  om a single iden
24997 74 69 66 69 65 72 2c 0a 20 20 20 20 20 20 2a 2a  tifier,.      **
24998 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69   sqlite3FixSrcLi
24999 73 74 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69  st can never fai
2499a 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  l. */.      asse
2499b 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rt(0);.    }.   
2499c 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
2499d 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
2499e 65 2c 20 30 2c 20 70 54 62 6c 4e 61 6d 65 2d 3e  e, 0, pTblName->
2499f 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 0a 20 20 20  a[0].zName, .   
249a0 20 20 20 20 20 70 54 62 6c 4e 61 6d 65 2d 3e 61       pTblName->a
249a1 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a  [0].zDatabase);.
249a2 20 20 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c      if( !pTab ||
249a3 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
249a4 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72  d ) goto exit_cr
249a5 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
249a6 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b  assert( db->aDb[
249a7 69 44 62 5d 2e 70 53 63 68 65 6d 61 3d 3d 70 54  iDb].pSchema==pT
249a8 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20  ab->pSchema );. 
249a9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
249aa 72 74 28 20 70 4e 61 6d 65 3d 3d 30 20 29 3b 0a  rt( pName==0 );.
249ab 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 73      pTab = pPars
249ac 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20  e->pNewTable;.  
249ad 20 20 69 66 28 20 21 70 54 61 62 20 29 20 67 6f    if( !pTab ) go
249ae 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
249af 6e 64 65 78 3b 0a 20 20 20 20 69 44 62 20 3d 20  ndex;.    iDb = 
249b0 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
249b1 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70  ndex(db, pTab->p
249b2 53 63 68 65 6d 61 29 3b 0a 20 20 7d 0a 20 20 70  Schema);.  }.  p
249b3 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  Db = &db->aDb[iD
249b4 62 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  b];..  assert( p
249b5 54 61 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  Tab!=0 );.  asse
249b6 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  rt( pParse->nErr
249b7 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 73 71 6c  ==0 );.  if( sql
249b8 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61  ite3StrNICmp(pTa
249b9 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74  b->zName, "sqlit
249ba 65 5f 22 2c 20 37 29 3d 3d 30 20 0a 20 20 20 20  e_", 7)==0 .    
249bb 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 26 70 54     && memcmp(&pT
249bc 61 62 2d 3e 7a 4e 61 6d 65 5b 37 5d 2c 22 61 6c  ab->zName[7],"al
249bd 74 65 72 74 61 62 5f 22 2c 39 29 21 3d 30 20 29  tertab_",9)!=0 )
249be 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
249bf 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
249c0 61 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20  able %s may not 
249c1 62 65 20 69 6e 64 65 78 65 64 22 2c 20 70 54 61  be indexed", pTa
249c2 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  b->zName);.    g
249c3 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
249c4 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 69 66 6e 64  index;.  }.#ifnd
249c5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
249c6 49 45 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e  IEW.  if( pTab->
249c7 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73  pSelect ){.    s
249c8 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
249c9 50 61 72 73 65 2c 20 22 76 69 65 77 73 20 6d 61  Parse, "views ma
249ca 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64  y not be indexed
249cb 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  ");.    goto exi
249cc 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
249cd 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64    }.#endif.#ifnd
249ce 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
249cf 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66  IRTUALTABLE.  if
249d0 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
249d1 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
249d2 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
249d3 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73   "virtual tables
249d4 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65   may not be inde
249d5 78 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  xed");.    goto 
249d6 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
249d7 78 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  x;.  }.#endif.. 
249d8 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68   /*.  ** Find th
249d9 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e  e name of the in
249da 64 65 78 2e 20 20 4d 61 6b 65 20 73 75 72 65 20  dex.  Make sure 
249db 74 68 65 72 65 20 69 73 20 6e 6f 74 20 61 6c 72  there is not alr
249dc 65 61 64 79 20 61 6e 6f 74 68 65 72 0a 20 20 2a  eady another.  *
249dd 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65  * index or table
249de 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e   with the same n
249df 61 6d 65 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a  ame.  .  **.  **
249e0 20 45 78 63 65 70 74 69 6f 6e 3a 20 20 49 66 20   Exception:  If 
249e1 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 74  we are reading t
249e2 68 65 20 6e 61 6d 65 73 20 6f 66 20 70 65 72 6d  he names of perm
249e3 61 6e 65 6e 74 20 69 6e 64 69 63 65 73 20 66 72  anent indices fr
249e4 6f 6d 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69  om the.  ** sqli
249e5 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
249e6 28 62 65 63 61 75 73 65 20 73 6f 6d 65 20 6f 74  (because some ot
249e7 68 65 72 20 70 72 6f 63 65 73 73 20 63 68 61 6e  her process chan
249e8 67 65 64 20 74 68 65 20 73 63 68 65 6d 61 29 20  ged the schema) 
249e9 61 6e 64 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20  and.  ** one of 
249ea 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20  the index names 
249eb 63 6f 6c 6c 69 64 65 73 20 77 69 74 68 20 74 68  collides with th
249ec 65 20 6e 61 6d 65 20 6f 66 20 61 20 74 65 6d 70  e name of a temp
249ed 6f 72 61 72 79 20 74 61 62 6c 65 20 6f 72 0a 20  orary table or. 
249ee 20 2a 2a 20 69 6e 64 65 78 2c 20 74 68 65 6e 20   ** index, then 
249ef 77 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65  we will continue
249f0 20 74 6f 20 70 72 6f 63 65 73 73 20 74 68 69 73   to process this
249f1 20 69 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a   index..  **.  *
249f2 2a 20 49 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74  * If pName==0 it
249f3 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 61   means that we a
249f4 72 65 0a 20 20 2a 2a 20 64 65 61 6c 69 6e 67 20  re.  ** dealing 
249f5 77 69 74 68 20 61 20 70 72 69 6d 61 72 79 20 6b  with a primary k
249f6 65 79 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e  ey or UNIQUE con
249f7 73 74 72 61 69 6e 74 2e 20 20 57 65 20 68 61 76  straint.  We hav
249f8 65 20 74 6f 20 69 6e 76 65 6e 74 20 6f 75 72 0a  e to invent our.
249f9 20 20 2a 2a 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20    ** own name.. 
249fa 20 2a 2f 0a 20 20 69 66 28 20 70 4e 61 6d 65 20   */.  if( pName 
249fb 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73  ){.    zName = s
249fc 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
249fd 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ken(db, pName);.
249fe 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30      if( zName==0
249ff 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
24a00 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69  ate_index;.    i
24a01 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
24a02 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74  lite3CheckObject
24a03 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61  Name(pParse, zNa
24a04 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  me) ){.      got
24a05 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24a06 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  dex;.    }.    i
24a07 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  f( !db->init.bus
24a08 79 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  y ){.      if( s
24a09 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
24a0a 64 62 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30  db, zName, 0)!=0
24a0b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
24a0c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
24a0d 73 65 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c  se, "there is al
24a0e 72 65 61 64 79 20 61 20 74 61 62 6c 65 20 6e 61  ready a table na
24a0f 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  med %s", zName);
24a10 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78  .        goto ex
24a11 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
24a12 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
24a13 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
24a14 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d  ndIndex(db, zNam
24a15 65 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d  e, pDb->zName)!=
24a16 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  0 ){.      if( !
24a17 69 66 4e 6f 74 45 78 69 73 74 20 29 7b 0a 20 20  ifNotExist ){.  
24a18 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
24a19 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69  orMsg(pParse, "i
24a1a 6e 64 65 78 20 25 73 20 61 6c 72 65 61 64 79 20  ndex %s already 
24a1b 65 78 69 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b  exists", zName);
24a1c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67  .      }.      g
24a1d 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
24a1e 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d  index;.    }.  }
24a1f 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 3b  else{.    int n;
24a20 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 4c 6f 6f  .    Index *pLoo
24a21 70 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70  p;.    for(pLoop
24a22 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 2c 20 6e  =pTab->pIndex, n
24a23 3d 31 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70  =1; pLoop; pLoop
24a24 3d 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e  =pLoop->pNext, n
24a25 2b 2b 29 7b 7d 0a 20 20 20 20 7a 4e 61 6d 65 20  ++){}.    zName 
24a26 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
24a27 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 61 75 74  (db, "sqlite_aut
24a28 6f 69 6e 64 65 78 5f 25 73 5f 25 64 22 2c 20 70  oindex_%s_%d", p
24a29 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a  Tab->zName, n);.
24a2a 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30      if( zName==0
24a2b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   ){.      goto e
24a2c 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24a2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
24a2e 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 75 74 68  * Check for auth
24a2f 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 63 72 65  orization to cre
24a30 61 74 65 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20  ate an index..  
24a31 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
24a32 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
24a33 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 63 6f 6e  TION.  {.    con
24a34 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 70  st char *zDb = p
24a35 44 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69  Db->zName;.    i
24a36 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
24a37 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
24a38 54 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d  TE_INSERT, SCHEM
24a39 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 30 2c  A_TABLE(iDb), 0,
24a3a 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67   zDb) ){.      g
24a3b 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
24a3c 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20  index;.    }.   
24a3d 20 69 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41   i = SQLITE_CREA
24a3e 54 45 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66  TE_INDEX;.    if
24a3f 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
24a40 26 20 69 44 62 3d 3d 31 20 29 20 69 20 3d 20 53  & iDb==1 ) i = S
24a41 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
24a42 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28  P_INDEX;.    if(
24a43 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
24a44 6b 28 70 50 61 72 73 65 2c 20 69 2c 20 7a 4e 61  k(pParse, i, zNa
24a45 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  me, pTab->zName,
24a46 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67   zDb) ){.      g
24a47 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
24a48 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d  index;.    }.  }
24a49 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
24a4a 20 70 4c 69 73 74 3d 3d 30 2c 20 69 74 20 6d 65   pList==0, it me
24a4b 61 6e 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ans this routine
24a4c 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 6d   was called to m
24a4d 61 6b 65 20 61 20 70 72 69 6d 61 72 79 0a 20 20  ake a primary.  
24a4e 2a 2a 20 6b 65 79 20 6f 75 74 20 6f 66 20 74 68  ** key out of th
24a4f 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 61 64  e last column ad
24a50 64 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ded to the table
24a51 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
24a52 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 63 72 65  ion..  ** So cre
24a53 61 74 65 20 61 20 66 61 6b 65 20 6c 69 73 74 20  ate a fake list 
24a54 74 6f 20 73 69 6d 75 6c 61 74 65 20 74 68 69 73  to simulate this
24a55 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69  ..  */.  if( pLi
24a56 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 75 6c  st==0 ){.    nul
24a57 6c 49 64 2e 7a 20 3d 20 70 54 61 62 2d 3e 61 43  lId.z = pTab->aC
24a58 6f 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 5d  ol[pTab->nCol-1]
24a59 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 75 6c 6c  .zName;.    null
24a5a 49 64 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  Id.n = sqlite3St
24a5b 72 6c 65 6e 33 30 28 28 63 68 61 72 2a 29 6e 75  rlen30((char*)nu
24a5c 6c 6c 49 64 2e 7a 29 3b 0a 20 20 20 20 70 4c 69  llId.z);.    pLi
24a5d 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
24a5e 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
24a5f 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  e, 0, 0);.    if
24a60 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 67 6f 74  ( pList==0 ) got
24a61 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24a62 64 65 78 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dex;.    sqlite3
24a63 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28  ExprListSetName(
24a64 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 26  pParse, pList, &
24a65 6e 75 6c 6c 49 64 2c 20 30 29 3b 0a 20 20 20 20  nullId, 0);.    
24a66 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f 72 74  pList->a[0].sort
24a67 4f 72 64 65 72 20 3d 20 28 75 38 29 73 6f 72 74  Order = (u8)sort
24a68 4f 72 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Order;.  }..  /*
24a69 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
24a6a 6d 61 6e 79 20 62 79 74 65 73 20 6f 66 20 73 70  many bytes of sp
24a6b 61 63 65 20 61 72 65 20 72 65 71 75 69 72 65 64  ace are required
24a6c 20 74 6f 20 73 74 6f 72 65 20 65 78 70 6c 69 63   to store explic
24a6d 69 74 6c 79 0a 20 20 2a 2a 20 73 70 65 63 69 66  itly.  ** specif
24a6e 69 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ied collation se
24a6f 71 75 65 6e 63 65 20 6e 61 6d 65 73 2e 0a 20 20  quence names..  
24a70 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
24a71 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  pList->nExpr; i+
24a72 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45  +){.    Expr *pE
24a73 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69  xpr = pList->a[i
24a74 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28  ].pExpr;.    if(
24a75 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20   pExpr ){.      
24a76 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d  CollSeq *pColl =
24a77 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20   pExpr->pColl;. 
24a78 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 70       /* Either p
24a79 43 6f 6c 6c 21 3d 30 20 6f 72 20 74 68 65 72 65  Coll!=0 or there
24a7a 20 77 61 73 20 61 6e 20 4f 4f 4d 20 66 61 69 6c   was an OOM fail
24a7b 75 72 65 2e 20 20 42 75 74 20 69 66 20 61 6e 20  ure.  But if an 
24a7c 4f 4f 4d 0a 20 20 20 20 20 20 2a 2a 20 66 61 69  OOM.      ** fai
24a7d 6c 75 72 65 20 77 65 20 68 61 76 65 20 71 75 69  lure we have qui
24a7e 74 20 62 65 66 6f 72 65 20 72 65 61 63 68 69 6e  t before reachin
24a7f 67 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 2a 2f  g this point. */
24a80 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59  .      if( ALWAY
24a81 53 28 70 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20  S(pColl) ){.    
24a82 20 20 20 20 6e 45 78 74 72 61 20 2b 3d 20 28 31      nExtra += (1
24a83 20 2b 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   + sqlite3Strlen
24a84 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29  30(pColl->zName)
24a85 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
24a86 0a 20 20 7d 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a  .  }..  /* .  **
24a87 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 69 6e   Allocate the in
24a88 64 65 78 20 73 74 72 75 63 74 75 72 65 2e 20 0a  dex structure. .
24a89 20 20 2a 2f 0a 20 20 6e 4e 61 6d 65 20 3d 20 73    */.  nName = s
24a8a 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
24a8b 4e 61 6d 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20  Name);.  nCol = 
24a8c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  pList->nExpr;.  
24a8d 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
24a8e 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
24a8f 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49   .      sizeof(I
24a90 6e 64 65 78 29 20 2b 20 20 20 20 20 20 20 20 20  ndex) +         
24a91 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74       /* Index st
24a92 72 75 63 74 75 72 65 20 20 2a 2f 0a 20 20 20 20  ructure  */.    
24a93 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43    sizeof(int)*nC
24a94 6f 6c 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f  ol +           /
24a95 2a 20 49 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e  * Index.aiColumn
24a96 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65     */.      size
24a97 6f 66 28 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29  of(int)*(nCol+1)
24a98 20 2b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65   +       /* Inde
24a99 78 2e 61 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a  x.aiRowEst   */.
24a9a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 63 68 61        sizeof(cha
24a9b 72 20 2a 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20  r *)*nCol +     
24a9c 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f     /* Index.azCo
24a9d 6c 6c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  ll     */.      
24a9e 73 69 7a 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20  sizeof(u8)*nCol 
24a9f 2b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  +            /* 
24aa0 49 6e 64 65 78 2e 61 53 6f 72 74 4f 72 64 65 72  Index.aSortOrder
24aa1 20 2a 2f 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20   */.      nName 
24aa2 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 20  + 1 +           
24aa3 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e         /* Index.
24aa4 7a 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20  zName      */.  
24aa5 20 20 20 20 6e 45 78 74 72 61 20 20 20 20 20 20      nExtra      
24aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24aa7 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65   /* Collation se
24aa8 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a  quence names */.
24aa9 20 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d    );.  if( db->m
24aaa 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
24aab 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
24aac 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20  ate_index;.  }. 
24aad 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20   pIndex->azColl 
24aae 3d 20 28 63 68 61 72 2a 2a 29 28 26 70 49 6e 64  = (char**)(&pInd
24aaf 65 78 5b 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78  ex[1]);.  pIndex
24ab0 2d 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e  ->aiColumn = (in
24ab1 74 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a  t *)(&pIndex->az
24ab2 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70  Coll[nCol]);.  p
24ab3 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 20  Index->aiRowEst 
24ab4 3d 20 28 75 6e 73 69 67 6e 65 64 20 2a 29 28 26  = (unsigned *)(&
24ab5 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e  pIndex->aiColumn
24ab6 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65  [nCol]);.  pInde
24ab7 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20  x->aSortOrder = 
24ab8 28 75 38 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e  (u8 *)(&pIndex->
24ab9 61 69 52 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d  aiRowEst[nCol+1]
24aba 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61  );.  pIndex->zNa
24abb 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70  me = (char *)(&p
24abc 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65  Index->aSortOrde
24abd 72 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74  r[nCol]);.  zExt
24abe 72 61 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70  ra = (char *)(&p
24abf 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61  Index->zName[nNa
24ac0 6d 65 2b 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79  me+1]);.  memcpy
24ac1 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20  (pIndex->zName, 
24ac2 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b  zName, nName+1);
24ac3 0a 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c  .  pIndex->pTabl
24ac4 65 20 3d 20 70 54 61 62 3b 0a 20 20 70 49 6e 64  e = pTab;.  pInd
24ac5 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c  ex->nColumn = pL
24ac6 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49  ist->nExpr;.  pI
24ac7 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20  ndex->onError = 
24ac8 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 70  (u8)onError;.  p
24ac9 49 6e 64 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78  Index->autoIndex
24aca 20 3d 20 28 75 38 29 28 70 4e 61 6d 65 3d 3d 30   = (u8)(pName==0
24acb 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 70 53 63  );.  pIndex->pSc
24acc 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69  hema = db->aDb[i
24acd 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 0a 20 20  Db].pSchema;..  
24ace 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
24acf 69 66 20 77 65 20 73 68 6f 75 6c 64 20 68 6f 6e  if we should hon
24ad0 6f 72 20 44 45 53 43 20 72 65 71 75 65 73 74 73  or DESC requests
24ad1 20 6f 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e   on index column
24ad2 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 62  s.  */.  if( pDb
24ad3 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
24ad4 66 6f 72 6d 61 74 3e 3d 34 20 29 7b 0a 20 20 20  format>=4 ){.   
24ad5 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d   sortOrderMask =
24ad6 20 2d 31 3b 20 20 20 2f 2a 20 48 6f 6e 6f 72 20   -1;   /* Honor 
24ad7 44 45 53 43 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b  DESC */.  }else{
24ad8 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72 4d 61  .    sortOrderMa
24ad9 73 6b 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 67  sk = 0;    /* Ig
24ada 6e 6f 72 65 20 44 45 53 43 20 2a 2f 0a 20 20 7d  nore DESC */.  }
24adb 0a 0a 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20  ..  /* Scan the 
24adc 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63 6f 6c  names of the col
24add 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c  umns of the tabl
24ade 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20  e to be indexed 
24adf 61 6e 64 0a 20 20 2a 2a 20 6c 6f 61 64 20 74 68  and.  ** load th
24ae0 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73  e column indices
24ae1 20 69 6e 74 6f 20 74 68 65 20 49 6e 64 65 78 20   into the Index 
24ae2 73 74 72 75 63 74 75 72 65 2e 20 20 52 65 70 6f  structure.  Repo
24ae3 72 74 20 61 6e 20 65 72 72 6f 72 0a 20 20 2a 2a  rt an error.  **
24ae4 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 69   if any column i
24ae5 73 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a  s not found..  *
24ae6 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 20 41 64  *.  ** TODO:  Ad
24ae7 64 20 61 20 74 65 73 74 20 74 6f 20 6d 61 6b 65  d a test to make
24ae8 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73   sure that the s
24ae9 61 6d 65 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f  ame column is no
24aea 74 20 6e 61 6d 65 64 0a 20 20 2a 2a 20 6d 6f 72  t named.  ** mor
24aeb 65 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68  e than once with
24aec 69 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65  in the same inde
24aed 78 2e 20 20 4f 6e 6c 79 20 74 68 65 20 66 69 72  x.  Only the fir
24aee 73 74 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20  st instance of. 
24aef 20 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77   ** the column w
24af0 69 6c 6c 20 65 76 65 72 20 62 65 20 75 73 65 64  ill ever be used
24af1 20 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a 65   by the optimize
24af2 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 75 73  r.  Note that us
24af3 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d  ing the.  ** sam
24af4 65 20 63 6f 6c 75 6d 6e 20 6d 6f 72 65 20 74 68  e column more th
24af5 61 6e 20 6f 6e 63 65 20 63 61 6e 6e 6f 74 20 62  an once cannot b
24af6 65 20 61 6e 20 65 72 72 6f 72 20 62 65 63 61 75  e an error becau
24af7 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 0a 20  se that would . 
24af8 20 2a 2a 20 62 72 65 61 6b 20 62 61 63 6b 77 61   ** break backwa
24af9 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  rds compatibilit
24afa 79 20 2d 20 69 74 20 6e 65 65 64 73 20 74 6f 20  y - it needs to 
24afb 62 65 20 61 20 77 61 72 6e 69 6e 67 2e 0a 20 20  be a warning..  
24afc 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c  */.  for(i=0, pL
24afd 69 73 74 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  istItem=pList->a
24afe 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ; i<pList->nExpr
24aff 3b 20 69 2b 2b 2c 20 70 4c 69 73 74 49 74 65 6d  ; i++, pListItem
24b00 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ++){.    const c
24b01 68 61 72 20 2a 7a 43 6f 6c 4e 61 6d 65 20 3d 20  har *zColName = 
24b02 70 4c 69 73 74 49 74 65 6d 2d 3e 7a 4e 61 6d 65  pListItem->zName
24b03 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 54  ;.    Column *pT
24b04 61 62 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20 72  abCol;.    int r
24b05 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65  equestedSortOrde
24b06 72 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f  r;.    char *zCo
24b07 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
24b08 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69        /* Collati
24b09 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65  on sequence name
24b0a 20 2a 2f 0a 0a 20 20 20 20 66 6f 72 28 6a 3d 30   */..    for(j=0
24b0b 2c 20 70 54 61 62 43 6f 6c 3d 70 54 61 62 2d 3e  , pTabCol=pTab->
24b0c 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  aCol; j<pTab->nC
24b0d 6f 6c 3b 20 6a 2b 2b 2c 20 70 54 61 62 43 6f 6c  ol; j++, pTabCol
24b0e 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ++){.      if( s
24b0f 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 43  qlite3StrICmp(zC
24b10 6f 6c 4e 61 6d 65 2c 20 70 54 61 62 43 6f 6c 2d  olName, pTabCol-
24b11 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65  >zName)==0 ) bre
24b12 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ak;.    }.    if
24b13 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20  ( j>=pTab->nCol 
24b14 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
24b15 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
24b16 20 22 74 61 62 6c 65 20 25 73 20 68 61 73 20 6e   "table %s has n
24b17 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25  o column named %
24b18 73 22 2c 0a 20 20 20 20 20 20 20 20 70 54 61 62  s",.        pTab
24b19 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 4e 61 6d  ->zName, zColNam
24b1a 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65  e);.      goto e
24b1b 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24b1c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64  ;.    }.    pInd
24b1d 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20  ex->aiColumn[i] 
24b1e 3d 20 6a 3b 0a 20 20 20 20 2f 2a 20 4a 75 73 74  = j;.    /* Just
24b1f 69 66 69 63 61 74 69 6f 6e 20 6f 66 20 74 68 65  ification of the
24b20 20 41 4c 57 41 59 53 28 70 4c 69 73 74 49 74 65   ALWAYS(pListIte
24b21 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 29  m->pExpr->pColl)
24b22 3a 20 20 42 65 63 61 75 73 65 20 6f 66 0a 20 20  :  Because of.  
24b23 20 20 2a 2a 20 74 68 65 20 77 61 79 20 74 68 65    ** the way the
24b24 20 22 69 64 78 6c 69 73 74 22 20 6e 6f 6e 2d 74   "idxlist" non-t
24b25 65 72 6d 69 6e 61 6c 20 69 73 20 63 6f 6e 73 74  erminal is const
24b26 72 75 63 74 65 64 20 62 79 20 74 68 65 20 70 61  ructed by the pa
24b27 72 73 65 72 2c 0a 20 20 20 20 2a 2a 20 69 66 20  rser,.    ** if 
24b28 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72  pListItem->pExpr
24b29 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65   is not null the
24b2a 6e 20 65 69 74 68 65 72 20 70 4c 69 73 74 49 74  n either pListIt
24b2b 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c  em->pExpr->pColl
24b2c 0a 20 20 20 20 2a 2a 20 6d 75 73 74 20 65 78 69  .    ** must exi
24b2d 73 74 20 6f 72 20 65 6c 73 65 20 74 68 65 72 65  st or else there
24b2e 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20   must have been 
24b2f 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42  an OOM error.  B
24b30 75 74 20 69 66 20 74 68 65 72 65 0a 20 20 20 20  ut if there.    
24b31 2a 2a 20 77 61 73 20 61 6e 20 4f 4f 4d 20 65 72  ** was an OOM er
24b32 72 6f 72 2c 20 77 65 20 77 6f 75 6c 64 20 6e 65  ror, we would ne
24b33 76 65 72 20 72 65 61 63 68 20 74 68 69 73 20 70  ver reach this p
24b34 6f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28  oint. */.    if(
24b35 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70   pListItem->pExp
24b36 72 20 26 26 20 41 4c 57 41 59 53 28 70 4c 69 73  r && ALWAYS(pLis
24b37 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43  tItem->pExpr->pC
24b38 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 69 6e  oll) ){.      in
24b39 74 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7a  t nColl;.      z
24b3a 43 6f 6c 6c 20 3d 20 70 4c 69 73 74 49 74 65 6d  Coll = pListItem
24b3b 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 2d 3e  ->pExpr->pColl->
24b3c 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 6e 43 6f  zName;.      nCo
24b3d 6c 6c 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  ll = sqlite3Strl
24b3e 65 6e 33 30 28 7a 43 6f 6c 6c 29 20 2b 20 31 3b  en30(zColl) + 1;
24b3f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
24b40 45 78 74 72 61 3e 3d 6e 43 6f 6c 6c 20 29 3b 0a  Extra>=nColl );.
24b41 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 45 78        memcpy(zEx
24b42 74 72 61 2c 20 7a 43 6f 6c 6c 2c 20 6e 43 6f 6c  tra, zColl, nCol
24b43 6c 29 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20  l);.      zColl 
24b44 3d 20 7a 45 78 74 72 61 3b 0a 20 20 20 20 20 20  = zExtra;.      
24b45 7a 45 78 74 72 61 20 2b 3d 20 6e 43 6f 6c 6c 3b  zExtra += nColl;
24b46 0a 20 20 20 20 20 20 6e 45 78 74 72 61 20 2d 3d  .      nExtra -=
24b47 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73   nColl;.    }els
24b48 65 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d  e{.      zColl =
24b49 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a   pTab->aCol[j].z
24b4a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 66 28 20  Coll;.      if( 
24b4b 21 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20  !zColl ){.      
24b4c 20 20 7a 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44    zColl = db->pD
24b4d 66 6c 74 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a  fltColl->zName;.
24b4e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
24b4f 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
24b50 62 75 73 79 20 26 26 20 21 73 71 6c 69 74 65 33  busy && !sqlite3
24b51 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50  LocateCollSeq(pP
24b52 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a  arse, zColl) ){.
24b53 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
24b54 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
24b55 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e    }.    pIndex->
24b56 61 7a 43 6f 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c  azColl[i] = zCol
24b57 6c 3b 0a 20 20 20 20 72 65 71 75 65 73 74 65 64  l;.    requested
24b58 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4c 69 73  SortOrder = pLis
24b59 74 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72  tItem->sortOrder
24b5a 20 26 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b   & sortOrderMask
24b5b 3b 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 53  ;.    pIndex->aS
24b5c 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 28 75  ortOrder[i] = (u
24b5d 38 29 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f  8)requestedSortO
24b5e 72 64 65 72 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  rder;.  }.  sqli
24b5f 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74  te3DefaultRowEst
24b60 28 70 49 6e 64 65 78 29 3b 0a 0a 20 20 69 66 28  (pIndex);..  if(
24b61 20 70 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 70   pTab==pParse->p
24b62 4e 65 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  NewTable ){.    
24b63 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  /* This routine 
24b64 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  has been called 
24b65 74 6f 20 63 72 65 61 74 65 20 61 6e 20 61 75 74  to create an aut
24b66 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 61 73 20  omatic index as 
24b67 61 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 20  a.    ** result 
24b68 6f 66 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59  of a PRIMARY KEY
24b69 20 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73   or UNIQUE claus
24b6a 65 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 64 65  e on a column de
24b6b 66 69 6e 69 74 69 6f 6e 2c 20 6f 72 0a 20 20 20  finition, or.   
24b6c 20 2a 2a 20 61 20 50 52 49 4d 41 52 59 20 4b 45   ** a PRIMARY KE
24b6d 59 20 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75  Y or UNIQUE clau
24b6e 73 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  se following the
24b6f 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69   column definiti
24b70 6f 6e 73 2e 0a 20 20 20 20 2a 2a 20 69 2e 65 2e  ons..    ** i.e.
24b71 20 6f 6e 65 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a   one of:.    **.
24b72 20 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41      ** CREATE TA
24b73 42 4c 45 20 74 28 78 20 50 52 49 4d 41 52 59 20  BLE t(x PRIMARY 
24b74 4b 45 59 2c 20 79 29 3b 0a 20 20 20 20 2a 2a 20  KEY, y);.    ** 
24b75 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78  CREATE TABLE t(x
24b76 2c 20 79 2c 20 55 4e 49 51 55 45 28 78 2c 20 79  , y, UNIQUE(x, y
24b77 29 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ));.    **.    *
24b78 2a 20 45 69 74 68 65 72 20 77 61 79 2c 20 63 68  * Either way, ch
24b79 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
24b7a 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20  e table already 
24b7b 68 61 73 20 73 75 63 68 20 61 6e 20 69 6e 64 65  has such an inde
24b7c 78 2e 20 49 66 0a 20 20 20 20 2a 2a 20 73 6f 2c  x. If.    ** so,
24b7d 20 64 6f 6e 27 74 20 62 6f 74 68 65 72 20 63 72   don't bother cr
24b7e 65 61 74 69 6e 67 20 74 68 69 73 20 6f 6e 65 2e  eating this one.
24b7f 20 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69   This only appli
24b80 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20 61 75 74  es to.    ** aut
24b81 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74  omatically creat
24b82 65 64 20 69 6e 64 69 63 65 73 2e 20 55 73 65 72  ed indices. User
24b83 73 20 63 61 6e 20 64 6f 20 61 73 20 74 68 65 79  s can do as they
24b84 20 77 69 73 68 20 77 69 74 68 0a 20 20 20 20 2a   wish with.    *
24b85 2a 20 65 78 70 6c 69 63 69 74 20 69 6e 64 69 63  * explicit indic
24b86 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  es..    **.    *
24b87 2a 20 54 77 6f 20 55 4e 49 51 55 45 20 6f 72 20  * Two UNIQUE or 
24b88 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73  PRIMARY KEY cons
24b89 74 72 61 69 6e 74 73 20 61 72 65 20 63 6f 6e 73  traints are cons
24b8a 69 64 65 72 65 64 20 65 71 75 69 76 61 6c 65 6e  idered equivalen
24b8b 74 0a 20 20 20 20 2a 2a 20 28 61 6e 64 20 74 68  t.    ** (and th
24b8c 75 73 20 73 75 70 70 72 65 73 73 69 6e 67 20 74  us suppressing t
24b8d 68 65 20 73 65 63 6f 6e 64 20 6f 6e 65 29 20 65  he second one) e
24b8e 76 65 6e 20 69 66 20 74 68 65 79 20 68 61 76 65  ven if they have
24b8f 20 64 69 66 66 65 72 65 6e 74 0a 20 20 20 20 2a   different.    *
24b90 2a 20 73 6f 72 74 20 6f 72 64 65 72 73 2e 0a 20  * sort orders.. 
24b91 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20     **.    ** If 
24b92 74 68 65 72 65 20 61 72 65 20 64 69 66 66 65 72  there are differ
24b93 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ent collating se
24b94 71 75 65 6e 63 65 73 20 6f 72 20 69 66 20 74 68  quences or if th
24b95 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 20  e columns of.   
24b96 20 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69   ** the constrai
24b97 6e 74 20 6f 63 63 75 72 20 69 6e 20 64 69 66 66  nt occur in diff
24b98 65 72 65 6e 74 20 6f 72 64 65 72 73 2c 20 74 68  erent orders, th
24b99 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  en the constrain
24b9a 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 63 6f  ts are.    ** co
24b9b 6e 73 69 64 65 72 65 64 20 64 69 73 74 69 6e 63  nsidered distinc
24b9c 74 20 61 6e 64 20 62 6f 74 68 20 72 65 73 75 6c  t and both resul
24b9d 74 20 69 6e 20 73 65 70 61 72 61 74 65 20 69 6e  t in separate in
24b9e 64 69 63 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  dices..    */.  
24b9f 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
24ba0 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62     for(pIdx=pTab
24ba1 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
24ba2 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
24ba3 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a  ){.      int k;.
24ba4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
24ba5 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  dx->onError!=OE_
24ba6 4e 6f 6e 65 20 29 3b 0a 20 20 20 20 20 20 61 73  None );.      as
24ba7 73 65 72 74 28 20 70 49 64 78 2d 3e 61 75 74 6f  sert( pIdx->auto
24ba8 49 6e 64 65 78 20 29 3b 0a 20 20 20 20 20 20 61  Index );.      a
24ba9 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e 6f  ssert( pIndex->o
24baa 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20  nError!=OE_None 
24bab 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 49  );..      if( pI
24bac 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 49 6e  dx->nColumn!=pIn
24bad 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 20 63  dex->nColumn ) c
24bae 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 66  ontinue;.      f
24baf 6f 72 28 6b 3d 30 3b 20 6b 3c 70 49 64 78 2d 3e  or(k=0; k<pIdx->
24bb0 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 29 7b 0a 20  nColumn; k++){. 
24bb1 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
24bb2 72 20 2a 7a 31 3b 0a 20 20 20 20 20 20 20 20 63  r *z1;.        c
24bb3 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20  onst char *z2;. 
24bb4 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d         if( pIdx-
24bb5 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 21 3d 70 49  >aiColumn[k]!=pI
24bb6 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b  ndex->aiColumn[k
24bb7 5d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  ] ) break;.     
24bb8 20 20 20 7a 31 20 3d 20 70 49 64 78 2d 3e 61 7a     z1 = pIdx->az
24bb9 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20  Coll[k];.       
24bba 20 7a 32 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a   z2 = pIndex->az
24bbb 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20  Coll[k];.       
24bbc 20 69 66 28 20 7a 31 21 3d 7a 32 20 26 26 20 73   if( z1!=z2 && s
24bbd 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 31  qlite3StrICmp(z1
24bbe 2c 20 7a 32 29 20 29 20 62 72 65 61 6b 3b 0a 20  , z2) ) break;. 
24bbf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
24bc0 20 6b 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d   k==pIdx->nColum
24bc1 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  n ){.        if(
24bc2 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d   pIdx->onError!=
24bc3 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20  pIndex->onError 
24bc4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
24bc5 54 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20  This constraint 
24bc6 63 72 65 61 74 65 73 20 74 68 65 20 73 61 6d 65  creates the same
24bc7 20 69 6e 64 65 78 20 61 73 20 61 20 70 72 65 76   index as a prev
24bc8 69 6f 75 73 0a 20 20 20 20 20 20 20 20 20 20 2a  ious.          *
24bc9 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 73 70 65  * constraint spe
24bca 63 69 66 69 65 64 20 73 6f 6d 65 77 68 65 72 65  cified somewhere
24bcb 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54   in the CREATE T
24bcc 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ABLE statement..
24bcd 20 20 20 20 20 20 20 20 20 20 2a 2a 20 48 6f 77            ** How
24bce 65 76 65 72 20 74 68 65 20 4f 4e 20 43 4f 4e 46  ever the ON CONF
24bcf 4c 49 43 54 20 63 6c 61 75 73 65 73 20 61 72 65  LICT clauses are
24bd0 20 64 69 66 66 65 72 65 6e 74 2e 20 49 66 20 62   different. If b
24bd1 6f 74 68 20 74 68 69 73 20 0a 20 20 20 20 20 20  oth this .      
24bd2 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e      ** constrain
24bd3 74 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f  t and the previo
24bd4 75 73 20 65 71 75 69 76 61 6c 65 6e 74 20 63 6f  us equivalent co
24bd5 6e 73 74 72 61 69 6e 74 20 68 61 76 65 20 65 78  nstraint have ex
24bd6 70 6c 69 63 69 74 0a 20 20 20 20 20 20 20 20 20  plicit.         
24bd7 20 2a 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20   ** ON CONFLICT 
24bd8 63 6c 61 75 73 65 73 20 74 68 69 73 20 69 73 20  clauses this is 
24bd9 61 6e 20 65 72 72 6f 72 2e 20 4f 74 68 65 72 77  an error. Otherw
24bda 69 73 65 2c 20 75 73 65 20 74 68 65 0a 20 20 20  ise, use the.   
24bdb 20 20 20 20 20 20 20 2a 2a 20 65 78 70 6c 69 63         ** explic
24bdc 69 74 6c 79 20 73 70 65 63 69 66 69 65 64 20 62  itly specified b
24bdd 65 68 61 76 69 6f 75 72 20 66 6f 72 20 74 68 65  ehaviour for the
24bde 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20   index..        
24bdf 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69    */.          i
24be0 66 28 20 21 28 70 49 64 78 2d 3e 6f 6e 45 72 72  f( !(pIdx->onErr
24be1 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 7c  or==OE_Default |
24be2 7c 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f  | pIndex->onErro
24be3 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 29 20 29  r==OE_Default) )
24be4 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  {.            sq
24be5 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24be6 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20  arse, .         
24be7 20 20 20 20 20 20 20 22 63 6f 6e 66 6c 69 63 74         "conflict
24be8 69 6e 67 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  ing ON CONFLICT 
24be9 63 6c 61 75 73 65 73 20 73 70 65 63 69 66 69 65  clauses specifie
24bea 64 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  d", 0);.        
24beb 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
24bec 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d  ( pIdx->onError=
24bed 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
24bee 20 20 20 20 20 20 20 20 20 20 20 70 49 64 78 2d             pIdx-
24bef 3e 6f 6e 45 72 72 6f 72 20 3d 20 70 49 6e 64 65  >onError = pInde
24bf0 78 2d 3e 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20  x->onError;.    
24bf1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
24bf2 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  }.        goto e
24bf3 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24bf4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
24bf5 20 20 7d 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74    }..  /* Link t
24bf6 68 65 20 6e 65 77 20 49 6e 64 65 78 20 73 74 72  he new Index str
24bf7 75 63 74 75 72 65 20 74 6f 20 69 74 73 20 74 61  ucture to its ta
24bf8 62 6c 65 20 61 6e 64 20 74 6f 20 74 68 65 20 6f  ble and to the o
24bf9 74 68 65 72 0a 20 20 2a 2a 20 69 6e 2d 6d 65 6d  ther.  ** in-mem
24bfa 6f 72 79 20 64 61 74 61 62 61 73 65 20 73 74 72  ory database str
24bfb 75 63 74 75 72 65 73 2e 20 0a 20 20 2a 2f 0a 20  uctures. .  */. 
24bfc 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75   if( db->init.bu
24bfd 73 79 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20  sy ){.    Index 
24bfe 2a 70 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69  *p;.    p = sqli
24bff 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70  te3HashInsert(&p
24c00 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e  Index->pSchema->
24c01 69 64 78 48 61 73 68 2c 20 0a 20 20 20 20 20 20  idxHash, .      
24c02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24c03 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d      pIndex->zNam
24c04 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  e, sqlite3Strlen
24c05 33 30 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65  30(pIndex->zName
24c06 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
24c07 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 6e               pIn
24c08 64 65 78 29 3b 0a 20 20 20 20 69 66 28 20 70 20  dex);.    if( p 
24c09 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
24c0a 20 70 3d 3d 70 49 6e 64 65 78 20 29 3b 20 20 2f   p==pIndex );  /
24c0b 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61  * Malloc must ha
24c0c 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 20  ve failed */.   
24c0d 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
24c0e 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 67  led = 1;.      g
24c0f 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
24c10 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20  index;.    }.   
24c11 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51   db->flags |= SQ
24c12 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67  LITE_InternChang
24c13 65 73 3b 0a 20 20 20 20 69 66 28 20 70 54 62 6c  es;.    if( pTbl
24c14 4e 61 6d 65 21 3d 30 20 29 7b 0a 20 20 20 20 20  Name!=0 ){.     
24c15 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 3d 20   pIndex->tnum = 
24c16 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d  db->init.newTnum
24c17 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
24c18 2a 20 49 66 20 74 68 65 20 64 62 2d 3e 69 6e 69  * If the db->ini
24c19 74 2e 62 75 73 79 20 69 73 20 30 20 74 68 65 6e  t.busy is 0 then
24c1a 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 64 65   create the inde
24c1b 78 20 6f 6e 20 64 69 73 6b 2e 20 20 54 68 69 73  x on disk.  This
24c1c 0a 20 20 2a 2a 20 69 6e 76 6f 6c 76 65 73 20 77  .  ** involves w
24c1d 72 69 74 69 6e 67 20 74 68 65 20 69 6e 64 65 78  riting the index
24c1e 20 69 6e 74 6f 20 74 68 65 20 6d 61 73 74 65 72   into the master
24c1f 20 74 61 62 6c 65 20 61 6e 64 20 66 69 6c 6c 69   table and filli
24c20 6e 67 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 69  ng in the.  ** i
24c21 6e 64 65 78 20 77 69 74 68 20 74 68 65 20 63 75  ndex with the cu
24c22 72 72 65 6e 74 20 74 61 62 6c 65 20 63 6f 6e 74  rrent table cont
24c23 65 6e 74 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ents..  **.  ** 
24c24 54 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  The db->init.bus
24c25 79 20 69 73 20 30 20 77 68 65 6e 20 74 68 65 20  y is 0 when the 
24c26 75 73 65 72 20 66 69 72 73 74 20 65 6e 74 65 72  user first enter
24c27 73 20 61 20 43 52 45 41 54 45 20 49 4e 44 45 58  s a CREATE INDEX
24c28 20 0a 20 20 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20   .  ** command. 
24c29 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69   db->init.busy i
24c2a 73 20 31 20 77 68 65 6e 20 61 20 64 61 74 61 62  s 1 when a datab
24c2b 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 61 6e  ase is opened an
24c2c 64 20 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 49  d .  ** CREATE I
24c2d 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 73 20  NDEX statements 
24c2e 61 72 65 20 72 65 61 64 20 6f 75 74 20 6f 66 20  are read out of 
24c2f 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65  the master table
24c30 2e 20 20 49 6e 0a 20 20 2a 2a 20 74 68 65 20 6c  .  In.  ** the l
24c31 61 74 74 65 72 20 63 61 73 65 20 74 68 65 20 69  atter case the i
24c32 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78 69  ndex already exi
24c33 73 74 73 20 6f 6e 20 64 69 73 6b 2c 20 77 68 69  sts on disk, whi
24c34 63 68 20 69 73 20 77 68 79 0a 20 20 2a 2a 20 77  ch is why.  ** w
24c35 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20  e don't want to 
24c36 72 65 63 72 65 61 74 65 20 69 74 2e 0a 20 20 2a  recreate it..  *
24c37 2a 0a 20 20 2a 2a 20 49 66 20 70 54 62 6c 4e 61  *.  ** If pTblNa
24c38 6d 65 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74  me==0 it means t
24c39 68 69 73 20 69 6e 64 65 78 20 69 73 20 67 65 6e  his index is gen
24c3a 65 72 61 74 65 64 20 61 73 20 61 20 70 72 69 6d  erated as a prim
24c3b 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20 6f 72 20  ary key.  ** or 
24c3c 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e  UNIQUE constrain
24c3d 74 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 41  t of a CREATE TA
24c3e 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  BLE statement.  
24c3f 53 69 6e 63 65 20 74 68 65 20 74 61 62 6c 65 0a  Since the table.
24c40 20 20 2a 2a 20 68 61 73 20 6a 75 73 74 20 62 65    ** has just be
24c41 65 6e 20 63 72 65 61 74 65 64 2c 20 69 74 20 63  en created, it c
24c42 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 61 74 61 20  ontains no data 
24c43 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69 6e  and the index in
24c44 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 20 20 2a  itialization.  *
24c45 2a 20 73 74 65 70 20 63 61 6e 20 62 65 20 73 6b  * step can be sk
24c46 69 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 6c  ipped..  */.  el
24c47 73 65 7b 20 2f 2a 20 69 66 28 20 64 62 2d 3e 69  se{ /* if( db->i
24c48 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 20 2a 2f  nit.busy==0 ) */
24c49 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  .    Vdbe *v;.  
24c4a 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20    char *zStmt;. 
24c4b 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b     int iMem = ++
24c4c 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20  pParse->nMem;.. 
24c4d 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
24c4e 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
24c4f 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f     if( v==0 ) go
24c50 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24c51 6e 64 65 78 3b 0a 0a 0a 20 20 20 20 2f 2a 20 43  ndex;...    /* C
24c52 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74 70 61  reate the rootpa
24c53 67 65 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78  ge for the index
24c54 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
24c55 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
24c56 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31  ration(pParse, 1
24c57 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
24c58 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
24c59 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 2c   OP_CreateIndex,
24c5a 20 69 44 62 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20   iDb, iMem);..  
24c5b 20 20 2f 2a 20 47 61 74 68 65 72 20 74 68 65 20    /* Gather the 
24c5c 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
24c5d 20 74 68 65 20 43 52 45 41 54 45 20 49 4e 44 45   the CREATE INDE
24c5e 58 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 74 6f  X statement into
24c5f 0a 20 20 20 20 2a 2a 20 74 68 65 20 7a 53 74 6d  .    ** the zStm
24c60 74 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a  t variable.    *
24c61 2f 0a 20 20 20 20 69 66 28 20 70 53 74 61 72 74  /.    if( pStart
24c62 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
24c63 28 20 70 45 6e 64 21 3d 30 20 29 3b 0a 20 20 20  ( pEnd!=0 );.   
24c64 20 20 20 2f 2a 20 41 20 6e 61 6d 65 64 20 69 6e     /* A named in
24c65 64 65 78 20 77 69 74 68 20 61 6e 20 65 78 70 6c  dex with an expl
24c66 69 63 69 74 20 43 52 45 41 54 45 20 49 4e 44 45  icit CREATE INDE
24c67 58 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  X statement */. 
24c68 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c       zStmt = sql
24c69 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
24c6a 22 43 52 45 41 54 45 25 73 20 49 4e 44 45 58 20  "CREATE%s INDEX 
24c6b 25 2e 2a 73 22 2c 0a 20 20 20 20 20 20 20 20 6f  %.*s",.        o
24c6c 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20  nError==OE_None 
24c6d 3f 20 22 22 20 3a 20 22 20 55 4e 49 51 55 45 22  ? "" : " UNIQUE"
24c6e 2c 0a 20 20 20 20 20 20 20 20 70 45 6e 64 2d 3e  ,.        pEnd->
24c6f 7a 20 2d 20 70 4e 61 6d 65 2d 3e 7a 20 2b 20 31  z - pName->z + 1
24c70 2c 0a 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d  ,.        pName-
24c71 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  >z);.    }else{.
24c72 20 20 20 20 20 20 2f 2a 20 41 6e 20 61 75 74 6f        /* An auto
24c73 6d 61 74 69 63 20 69 6e 64 65 78 20 63 72 65 61  matic index crea
24c74 74 65 64 20 62 79 20 61 20 50 52 49 4d 41 52 59  ted by a PRIMARY
24c75 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63   KEY or UNIQUE c
24c76 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20  onstraint */.   
24c77 20 20 20 2f 2a 20 7a 53 74 6d 74 20 3d 20 73 71     /* zStmt = sq
24c78 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 22 22 29  lite3MPrintf("")
24c79 3b 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74  ; */.      zStmt
24c7a 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 0;.    }..   
24c7b 20 2f 2a 20 41 64 64 20 61 6e 20 65 6e 74 72 79   /* Add an entry
24c7c 20 69 6e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65   in sqlite_maste
24c7d 72 20 66 6f 72 20 74 68 69 73 20 69 6e 64 65 78  r for this index
24c7e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
24c7f 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
24c80 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
24c81 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 2e  "INSERT INTO %Q.
24c82 25 73 20 56 41 4c 55 45 53 28 27 69 6e 64 65 78  %s VALUES('index
24c83 27 2c 25 51 2c 25 51 2c 23 25 64 2c 25 51 29 3b  ',%Q,%Q,#%d,%Q);
24c84 22 2c 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61  ",.        db->a
24c85 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53  Db[iDb].zName, S
24c86 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
24c87 2c 0a 20 20 20 20 20 20 20 20 70 49 6e 64 65 78  ,.        pIndex
24c88 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  ->zName,.       
24c89 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20   pTab->zName,.  
24c8a 20 20 20 20 20 20 69 4d 65 6d 2c 0a 20 20 20 20        iMem,.    
24c8b 20 20 20 20 7a 53 74 6d 74 0a 20 20 20 20 29 3b      zStmt.    );
24c8c 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
24c8d 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 0a  ee(db, zStmt);..
24c8e 20 20 20 20 2f 2a 20 46 69 6c 6c 20 74 68 65 20      /* Fill the 
24c8f 69 6e 64 65 78 20 77 69 74 68 20 64 61 74 61 20  index with data 
24c90 61 6e 64 20 72 65 70 61 72 73 65 20 74 68 65 20  and reparse the 
24c91 73 63 68 65 6d 61 2e 20 43 6f 64 65 20 61 6e 20  schema. Code an 
24c92 4f 50 5f 45 78 70 69 72 65 0a 20 20 20 20 2a 2a  OP_Expire.    **
24c93 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 61   to invalidate a
24c94 6c 6c 20 70 72 65 2d 63 6f 6d 70 69 6c 65 64 20  ll pre-compiled 
24c95 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20  statements..    
24c96 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 62 6c 4e  */.    if( pTblN
24c97 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ame ){.      sql
24c98 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28  ite3RefillIndex(
24c99 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20  pParse, pIndex, 
24c9a 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c  iMem);.      sql
24c9b 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
24c9c 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
24c9d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24c9e 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72  AddOp4(v, OP_Par
24c9f 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30  seSchema, iDb, 0
24ca0 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 73 71  , 0,.         sq
24ca1 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
24ca2 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70 49   "name='%q'", pI
24ca3 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34  ndex->zName), P4
24ca4 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20  _DYNAMIC);.     
24ca5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24ca6 70 31 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c  p1(v, OP_Expire,
24ca7 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a   0);.    }.  }..
24ca8 20 20 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67    /* When adding
24ca9 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 74 68 65   an index to the
24caa 20 6c 69 73 74 20 6f 66 20 69 6e 64 69 63 65 73   list of indices
24cab 20 66 6f 72 20 61 20 74 61 62 6c 65 2c 20 6d 61   for a table, ma
24cac 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 61 6c 6c  ke.  ** sure all
24cad 20 69 6e 64 69 63 65 73 20 6c 61 62 65 6c 65 64   indices labeled
24cae 20 4f 45 5f 52 65 70 6c 61 63 65 20 63 6f 6d 65   OE_Replace come
24caf 20 61 66 74 65 72 20 61 6c 6c 20 74 68 6f 73 65   after all those
24cb0 20 6c 61 62 65 6c 65 64 0a 20 20 2a 2a 20 4f 45   labeled.  ** OE
24cb1 5f 49 67 6e 6f 72 65 2e 20 20 54 68 69 73 20 69  _Ignore.  This i
24cb2 73 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20  s necessary for 
24cb3 74 68 65 20 63 6f 72 72 65 63 74 20 63 6f 6e 73  the correct cons
24cb4 74 72 61 69 6e 74 20 63 68 65 63 6b 0a 20 20 2a  traint check.  *
24cb5 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 28 69 6e  * processing (in
24cb6 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
24cb7 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73  ConstraintChecks
24cb8 28 29 29 20 61 73 20 70 61 72 74 20 6f 66 0a 20  ()) as part of. 
24cb9 20 2a 2a 20 55 50 44 41 54 45 20 61 6e 64 20 49   ** UPDATE and I
24cba 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73  NSERT statements
24cbb 2e 20 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  .  .  */.  if( d
24cbc 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20  b->init.busy || 
24cbd 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20  pTblName==0 ){. 
24cbe 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d     if( onError!=
24cbf 4f 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 54  OE_Replace || pT
24cc0 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 0a 20 20  ab->pIndex==0.  
24cc1 20 20 20 20 20 20 20 7c 7c 20 70 54 61 62 2d 3e         || pTab->
24cc2 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d  pIndex->onError=
24cc3 3d 4f 45 5f 52 65 70 6c 61 63 65 29 7b 0a 20 20  =OE_Replace){.  
24cc4 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78      pIndex->pNex
24cc5 74 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78  t = pTab->pIndex
24cc6 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 49  ;.      pTab->pI
24cc7 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 3b 0a 20  ndex = pIndex;. 
24cc8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
24cc9 49 6e 64 65 78 20 2a 70 4f 74 68 65 72 20 3d 20  Index *pOther = 
24cca 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20  pTab->pIndex;.  
24ccb 20 20 20 20 77 68 69 6c 65 28 20 70 4f 74 68 65      while( pOthe
24ccc 72 2d 3e 70 4e 65 78 74 20 26 26 20 70 4f 74 68  r->pNext && pOth
24ccd 65 72 2d 3e 70 4e 65 78 74 2d 3e 6f 6e 45 72 72  er->pNext->onErr
24cce 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29  or!=OE_Replace )
24ccf 7b 0a 20 20 20 20 20 20 20 20 70 4f 74 68 65 72  {.        pOther
24cd0 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74   = pOther->pNext
24cd1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
24cd2 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20  pIndex->pNext = 
24cd3 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20  pOther->pNext;. 
24cd4 20 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65       pOther->pNe
24cd5 78 74 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20  xt = pIndex;.   
24cd6 20 7d 0a 20 20 20 20 70 52 65 74 20 3d 20 70 49   }.    pRet = pI
24cd7 6e 64 65 78 3b 0a 20 20 20 20 70 49 6e 64 65 78  ndex;.    pIndex
24cd8 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
24cd9 43 6c 65 61 6e 20 75 70 20 62 65 66 6f 72 65 20  Clean up before 
24cda 65 78 69 74 69 6e 67 20 2a 2f 0a 65 78 69 74 5f  exiting */.exit_
24cdb 63 72 65 61 74 65 5f 69 6e 64 65 78 3a 0a 20 20  create_index:.  
24cdc 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20  if( pIndex ){.  
24cdd 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
24cde 49 6e 64 65 78 2d 3e 7a 43 6f 6c 41 66 66 29 3b  Index->zColAff);
24cdf 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
24ce0 65 65 28 64 62 2c 20 70 49 6e 64 65 78 29 3b 0a  ee(db, pIndex);.
24ce1 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70    }.  sqlite3Exp
24ce2 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
24ce3 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  pList);.  sqlite
24ce4 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64  3SrcListDelete(d
24ce5 62 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20  b, pTblName);.  
24ce6 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
24ce7 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75  , zName);.  retu
24ce8 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pRet;.}../*.*
24ce9 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e 64 65 78  * Fill the Index
24cea 2e 61 69 52 6f 77 45 73 74 5b 5d 20 61 72 72 61  .aiRowEst[] arra
24ceb 79 20 77 69 74 68 20 64 65 66 61 75 6c 74 20 69  y with default i
24cec 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 69 6e 66  nformation - inf
24ced 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62  ormation.** to b
24cee 65 20 75 73 65 64 20 77 68 65 6e 20 77 65 20 68  e used when we h
24cef 61 76 65 20 6e 6f 74 20 72 75 6e 20 74 68 65 20  ave not run the 
24cf0 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e  ANALYZE command.
24cf1 0a 2a 2a 0a 2a 2a 20 61 69 52 6f 77 45 73 74 5b  .**.** aiRowEst[
24cf2 30 5d 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f  0] is suppose to
24cf3 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d   contain the num
24cf4 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
24cf5 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a  in the index..**
24cf6 20 53 69 6e 63 65 20 77 65 20 64 6f 20 6e 6f 74   Since we do not
24cf7 20 6b 6e 6f 77 2c 20 67 75 65 73 73 20 31 20 6d   know, guess 1 m
24cf8 69 6c 6c 69 6f 6e 2e 20 20 61 69 52 6f 77 45 73  illion.  aiRowEs
24cf9 74 5b 31 5d 20 69 73 20 61 6e 20 65 73 74 69 6d  t[1] is an estim
24cfa 61 74 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 75  ate of the.** nu
24cfb 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
24cfc 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 6d  the table that m
24cfd 61 74 63 68 20 61 6e 79 20 70 61 72 74 69 63 75  atch any particu
24cfe 6c 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65  lar value of the
24cff 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e  .** first column
24d00 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20   of the index.  
24d01 61 69 52 6f 77 45 73 74 5b 32 5d 20 69 73 20 61  aiRowEst[2] is a
24d02 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68  n estimate of th
24d03 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 72  e number.** of r
24d04 6f 77 73 20 74 68 61 74 20 6d 61 74 63 68 20 61  ows that match a
24d05 6e 79 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f  ny particular co
24d06 6d 62 69 6e 69 61 74 69 6f 6e 20 6f 66 20 74 68  mbiniation of th
24d07 65 20 66 69 72 73 74 20 32 20 63 6f 6c 75 6d 6e  e first 2 column
24d08 73 0a 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 65  s.** of the inde
24d09 78 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68  x.  And so forth
24d0a 2e 20 20 49 74 20 6d 75 73 74 20 61 6c 77 61 79  .  It must alway
24d0b 73 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  s be the case th
24d0c 61 74 0a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  at.*.**         
24d0d 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61    aiRowEst[N]<=a
24d0e 69 52 6f 77 45 73 74 5b 4e 2d 31 5d 0a 2a 2a 20  iRowEst[N-1].** 
24d0f 20 20 20 20 20 20 20 20 20 20 61 69 52 6f 77 45            aiRowE
24d10 73 74 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41  st[N]>=1.**.** A
24d11 70 61 72 74 20 66 72 6f 6d 20 74 68 61 74 2c 20  part from that, 
24d12 77 65 20 68 61 76 65 20 6c 69 74 74 6c 65 20 74  we have little t
24d13 6f 20 67 6f 20 6f 6e 20 62 65 73 69 64 65 73 20  o go on besides 
24d14 69 6e 74 75 69 74 69 6f 6e 20 61 73 20 74 6f 0a  intuition as to.
24d15 2a 2a 20 68 6f 77 20 61 69 52 6f 77 45 73 74 5b  ** how aiRowEst[
24d16 5d 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74  ] should be init
24d17 69 61 6c 69 7a 65 64 2e 20 20 54 68 65 20 6e 75  ialized.  The nu
24d18 6d 62 65 72 73 20 67 65 6e 65 72 61 74 65 64 20  mbers generated 
24d19 68 65 72 65 0a 2a 2a 20 61 72 65 20 62 61 73 65  here.** are base
24d1a 64 20 6f 6e 20 74 79 70 69 63 61 6c 20 76 61 6c  d on typical val
24d1b 75 65 73 20 66 6f 75 6e 64 20 69 6e 20 61 63 74  ues found in act
24d1c 75 61 6c 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a  ual indices..*/.
24d1d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
24d1e 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 61 75  oid sqlite3Defau
24d1f 6c 74 52 6f 77 45 73 74 28 49 6e 64 65 78 20 2a  ltRowEst(Index *
24d20 70 49 64 78 29 7b 0a 20 20 75 6e 73 69 67 6e 65  pIdx){.  unsigne
24d21 64 20 2a 61 20 3d 20 70 49 64 78 2d 3e 61 69 52  d *a = pIdx->aiR
24d22 6f 77 45 73 74 3b 0a 20 20 69 6e 74 20 69 3b 0a  owEst;.  int i;.
24d23 20 20 61 73 73 65 72 74 28 20 61 21 3d 30 20 29    assert( a!=0 )
24d24 3b 0a 20 20 61 5b 30 5d 20 3d 20 31 30 30 30 30  ;.  a[0] = 10000
24d25 30 30 3b 0a 20 20 66 6f 72 28 69 3d 70 49 64 78  00;.  for(i=pIdx
24d26 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 3e 3d 35 3b  ->nColumn; i>=5;
24d27 20 69 2d 2d 29 7b 0a 20 20 20 20 61 5b 69 5d 20   i--){.    a[i] 
24d28 3d 20 35 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65  = 5;.  }.  while
24d29 28 20 69 3e 3d 31 20 29 7b 0a 20 20 20 20 61 5b  ( i>=1 ){.    a[
24d2a 69 5d 20 3d 20 31 31 20 2d 20 69 3b 0a 20 20 20  i] = 11 - i;.   
24d2b 20 69 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20   i--;.  }.  if( 
24d2c 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  pIdx->onError!=O
24d2d 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20 20 61 5b  E_None ){.    a[
24d2e 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d  pIdx->nColumn] =
24d2f 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   1;.  }.}../*.**
24d30 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
24d31 6c 6c 20 64 72 6f 70 20 61 6e 20 65 78 69 73 74  ll drop an exist
24d32 69 6e 67 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e  ing named index.
24d33 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a    This routine.*
24d34 2a 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  * implements the
24d35 20 44 52 4f 50 20 49 4e 44 45 58 20 73 74 61 74   DROP INDEX stat
24d36 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ement..*/.SQLITE
24d37 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
24d38 6c 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50  lite3DropIndex(P
24d39 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72  arse *pParse, Sr
24d3a 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e  cList *pName, in
24d3b 74 20 69 66 45 78 69 73 74 73 29 7b 0a 20 20 49  t ifExists){.  I
24d3c 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20  ndex *pIndex;.  
24d3d 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74  Vdbe *v;.  sqlit
24d3e 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
24d3f 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  >db;.  int iDb;.
24d40 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
24d41 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 20 20 20  e->nErr==0 );   
24d42 2f 2a 20 4e 65 76 65 72 20 63 61 6c 6c 65 64 20  /* Never called 
24d43 77 69 74 68 20 70 72 69 6f 72 20 65 72 72 6f 72  with prior error
24d44 73 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d  s */.  if( db->m
24d45 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
24d46 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
24d47 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 61  p_index;.  }.  a
24d48 73 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53  ssert( pName->nS
24d49 72 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 53  rc==1 );.  if( S
24d4a 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
24d4b 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
24d4c 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  se) ){.    goto 
24d4d 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b  exit_drop_index;
24d4e 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20  .  }.  pIndex = 
24d4f 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78  sqlite3FindIndex
24d50 28 64 62 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d  (db, pName->a[0]
24d51 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61  .zName, pName->a
24d52 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a  [0].zDatabase);.
24d53 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20    if( pIndex==0 
24d54 29 7b 0a 20 20 20 20 69 66 28 20 21 69 66 45 78  ){.    if( !ifEx
24d55 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 73 71  ists ){.      sq
24d56 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24d57 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69  arse, "no such i
24d58 6e 64 65 78 3a 20 25 53 22 2c 20 70 4e 61 6d 65  ndex: %S", pName
24d59 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
24d5a 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68  pParse->checkSch
24d5b 65 6d 61 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74  ema = 1;.    got
24d5c 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65  o exit_drop_inde
24d5d 78 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e  x;.  }.  if( pIn
24d5e 64 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29  dex->autoIndex )
24d5f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
24d60 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69  orMsg(pParse, "i
24d61 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  ndex associated 
24d62 77 69 74 68 20 55 4e 49 51 55 45 20 22 0a 20 20  with UNIQUE ".  
24d63 20 20 20 20 22 6f 72 20 50 52 49 4d 41 52 59 20      "or PRIMARY 
24d64 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  KEY constraint c
24d65 61 6e 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64  annot be dropped
24d66 22 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20  ", 0);.    goto 
24d67 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b  exit_drop_index;
24d68 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c  .  }.  iDb = sql
24d69 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
24d6a 78 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53  x(db, pIndex->pS
24d6b 63 68 65 6d 61 29 3b 0a 23 69 66 6e 64 65 66 20  chema);.#ifndef 
24d6c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
24d6d 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20  ORIZATION.  {.  
24d6e 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c    int code = SQL
24d6f 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 3b 0a  ITE_DROP_INDEX;.
24d70 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
24d71 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65  = pIndex->pTable
24d72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
24d73 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
24d74 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  iDb].zName;.    
24d75 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
24d76 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28   = SCHEMA_TABLE(
24d77 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71  iDb);.    if( sq
24d78 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
24d79 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45  Parse, SQLITE_DE
24d7a 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a  LETE, zTab, 0, z
24d7b 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  Db) ){.      got
24d7c 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65  o exit_drop_inde
24d7d 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  x;.    }.    if(
24d7e 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
24d7f 20 69 44 62 20 29 20 63 6f 64 65 20 3d 20 53 51   iDb ) code = SQ
24d80 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49  LITE_DROP_TEMP_I
24d81 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71  NDEX;.    if( sq
24d82 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
24d83 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 6e  Parse, code, pIn
24d84 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62  dex->zName, pTab
24d85 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b  ->zName, zDb) ){
24d86 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
24d87 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20  _drop_index;.   
24d88 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
24d89 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
24d8a 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20  e to remove the 
24d8b 69 6e 64 65 78 20 61 6e 64 20 66 72 6f 6d 20 74  index and from t
24d8c 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20  he master table 
24d8d 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  */.  v = sqlite3
24d8e 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
24d8f 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20  .  if( v ){.    
24d90 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
24d91 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
24d92 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20  e, 1, iDb);.    
24d93 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
24d94 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
24d95 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
24d96 51 2e 25 73 20 57 48 45 52 45 20 6e 61 6d 65 3d  Q.%s WHERE name=
24d97 25 51 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e  %Q",.       db->
24d98 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
24d99 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
24d9a 29 2c 0a 20 20 20 20 20 20 20 70 49 6e 64 65 78  ),.       pIndex
24d9b 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20  ->zName.    );. 
24d9c 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
24d9d 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c  ndTable(db, "sql
24d9e 69 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e  ite_stat1", db->
24d9f 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20  aDb[iDb].zName) 
24da0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
24da1 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
24da2 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c  se,.        "DEL
24da3 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69  ETE FROM %Q.sqli
24da4 74 65 5f 73 74 61 74 31 20 57 48 45 52 45 20 69  te_stat1 WHERE i
24da5 64 78 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20  dx=%Q",.        
24da6 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
24da7 6d 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d  me, pIndex->zNam
24da8 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d  e.      );.    }
24da9 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e  .    sqlite3Chan
24daa 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c  geCookie(pParse,
24dab 20 69 44 62 29 3b 0a 20 20 20 20 64 65 73 74 72   iDb);.    destr
24dac 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73  oyRootPage(pPars
24dad 65 2c 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2c  e, pIndex->tnum,
24dae 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
24daf 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
24db0 4f 50 5f 44 72 6f 70 49 6e 64 65 78 2c 20 69 44  OP_DropIndex, iD
24db1 62 2c 20 30 2c 20 30 2c 20 70 49 6e 64 65 78 2d  b, 0, 0, pIndex-
24db2 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a  >zName, 0);.  }.
24db3 0a 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78  .exit_drop_index
24db4 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  :.  sqlite3SrcLi
24db5 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61  stDelete(db, pNa
24db6 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41  me);.}../*.** pA
24db7 72 72 61 79 20 69 73 20 61 20 70 6f 69 6e 74 65  rray is a pointe
24db8 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66  r to an array of
24db9 20 6f 62 6a 65 63 74 73 2e 20 20 45 61 63 68 20   objects.  Each 
24dba 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a  object in the.**
24dbb 20 61 72 72 61 79 20 69 73 20 73 7a 45 6e 74 72   array is szEntr
24dbc 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e  y bytes in size.
24dbd 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61    This routine a
24dbe 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a  llocates a new.*
24dbf 2a 20 6f 62 6a 65 63 74 20 6f 6e 20 74 68 65 20  * object on the 
24dc0 65 6e 64 20 6f 66 20 74 68 65 20 61 72 72 61 79  end of the array
24dc1 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e 45 6e 74 72 79  ..**.** *pnEntry
24dc2 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
24dc3 66 20 65 6e 74 72 69 65 73 20 61 6c 72 65 61 64  f entries alread
24dc4 79 20 69 6e 20 75 73 65 2e 20 20 2a 70 6e 41 6c  y in use.  *pnAl
24dc5 6c 6f 63 20 69 73 0a 2a 2a 20 74 68 65 20 70 72  loc is.** the pr
24dc6 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74  eviously allocat
24dc7 65 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61  ed size of the a
24dc8 72 72 61 79 2e 20 20 69 6e 69 74 53 69 7a 65 20  rray.  initSize 
24dc9 69 73 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73  is the.** sugges
24dca 74 65 64 20 69 6e 69 74 69 61 6c 20 61 72 72 61  ted initial arra
24dcb 79 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f  y size allocatio
24dcc 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64  n..**.** The ind
24dcd 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e  ex of the new en
24dce 74 72 79 20 69 73 20 72 65 74 75 72 6e 65 64 20  try is returned 
24dcf 69 6e 20 2a 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20  in *pIdx..**.** 
24dd0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
24dd1 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
24dd2 6f 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f  o the array of o
24dd3 62 6a 65 63 74 73 2e 20 20 54 68 69 73 0a 2a 2a  bjects.  This.**
24dd4 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 73 61   might be the sa
24dd5 6d 65 20 61 73 20 74 68 65 20 70 41 72 72 61 79  me as the pArray
24dd6 20 70 61 72 61 6d 65 74 65 72 20 6f 72 20 69 74   parameter or it
24dd7 20 6d 69 67 68 74 20 62 65 20 61 20 64 69 66 66   might be a diff
24dd8 65 72 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74 65 72  erent.** pointer
24dd9 20 69 66 20 74 68 65 20 61 72 72 61 79 20 77 61   if the array wa
24dda 73 20 72 65 73 69 7a 65 64 2e 0a 2a 2f 0a 53 51  s resized..*/.SQ
24ddb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
24ddc 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41  d *sqlite3ArrayA
24ddd 6c 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74  llocate(.  sqlit
24dde 65 33 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20  e3 *db,      /* 
24ddf 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f  Connection to no
24de0 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66  tify of malloc f
24de1 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69  ailures */.  voi
24de2 64 20 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f  d *pArray,     /
24de3 2a 20 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63  * Array of objec
24de4 74 73 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65  ts.  Might be re
24de5 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69  allocated */.  i
24de6 6e 74 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20  nt szEntry,     
24de7 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68   /* Size of each
24de8 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61   object in the a
24de9 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 6e  rray */.  int in
24dea 69 74 53 69 7a 65 2c 20 20 20 20 20 2f 2a 20 53  itSize,     /* S
24deb 75 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c  uggested initial
24dec 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20   allocation, in 
24ded 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e  elements */.  in
24dee 74 20 2a 70 6e 45 6e 74 72 79 2c 20 20 20 20 20  t *pnEntry,     
24def 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a  /* Number of obj
24df0 65 63 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69  ects currently i
24df1 6e 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a  n use */.  int *
24df2 70 6e 41 6c 6c 6f 63 2c 20 20 20 20 20 2f 2a 20  pnAlloc,     /* 
24df3 43 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20  Current size of 
24df4 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  the allocation, 
24df5 69 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20  in elements */. 
24df6 20 69 6e 74 20 2a 70 49 64 78 20 20 20 20 20 20   int *pIdx      
24df7 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
24df8 69 6e 64 65 78 20 6f 66 20 61 20 6e 65 77 20 73  index of a new s
24df9 6c 6f 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  lot here */.){. 
24dfa 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20   char *z;.  if( 
24dfb 2a 70 6e 45 6e 74 72 79 20 3e 3d 20 2a 70 6e 41  *pnEntry >= *pnA
24dfc 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 76 6f 69 64  lloc ){.    void
24dfd 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20   *pNew;.    int 
24dfe 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20 6e 65 77  newSize;.    new
24dff 53 69 7a 65 20 3d 20 28 2a 70 6e 41 6c 6c 6f 63  Size = (*pnAlloc
24e00 29 2a 32 20 2b 20 69 6e 69 74 53 69 7a 65 3b 0a  )*2 + initSize;.
24e01 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
24e02 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20  e3DbRealloc(db, 
24e03 70 41 72 72 61 79 2c 20 6e 65 77 53 69 7a 65 2a  pArray, newSize*
24e04 73 7a 45 6e 74 72 79 29 3b 0a 20 20 20 20 69 66  szEntry);.    if
24e05 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20  ( pNew==0 ){.   
24e06 20 20 20 2a 70 49 64 78 20 3d 20 2d 31 3b 0a 20     *pIdx = -1;. 
24e07 20 20 20 20 20 72 65 74 75 72 6e 20 70 41 72 72       return pArr
24e08 61 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70  ay;.    }.    *p
24e09 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33  nAlloc = sqlite3
24e0a 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c  DbMallocSize(db,
24e0b 20 70 4e 65 77 29 2f 73 7a 45 6e 74 72 79 3b 0a   pNew)/szEntry;.
24e0c 20 20 20 20 70 41 72 72 61 79 20 3d 20 70 4e 65      pArray = pNe
24e0d 77 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68  w;.  }.  z = (ch
24e0e 61 72 2a 29 70 41 72 72 61 79 3b 0a 20 20 6d 65  ar*)pArray;.  me
24e0f 6d 73 65 74 28 26 7a 5b 2a 70 6e 45 6e 74 72 79  mset(&z[*pnEntry
24e10 20 2a 20 73 7a 45 6e 74 72 79 5d 2c 20 30 2c 20   * szEntry], 0, 
24e11 73 7a 45 6e 74 72 79 29 3b 0a 20 20 2a 70 49 64  szEntry);.  *pId
24e12 78 20 3d 20 2a 70 6e 45 6e 74 72 79 3b 0a 20 20  x = *pnEntry;.  
24e13 2b 2b 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 72 65  ++*pnEntry;.  re
24e14 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 7d 0a 0a  turn pArray;.}..
24e15 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e  /*.** Append a n
24e16 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68  ew element to th
24e17 65 20 67 69 76 65 6e 20 49 64 4c 69 73 74 2e 20  e given IdList. 
24e18 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 49 64   Create a new Id
24e19 4c 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20  List if.** need 
24e1a 62 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20  be..**.** A new 
24e1b 49 64 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e  IdList is return
24e1c 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6d  ed, or NULL if m
24e1d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a  alloc() fails..*
24e1e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24e1f 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33   IdList *sqlite3
24e20 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c  IdListAppend(sql
24e21 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74  ite3 *db, IdList
24e22 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a   *pList, Token *
24e23 70 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 69  pToken){.  int i
24e24 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
24e25 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20   ){.    pList = 
24e26 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
24e27 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 49  ero(db, sizeof(I
24e28 64 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66  dList) );.    if
24e29 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74  ( pList==0 ) ret
24e2a 75 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73 74  urn 0;.    pList
24e2b 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20  ->nAlloc = 0;.  
24e2c 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 73  }.  pList->a = s
24e2d 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63  qlite3ArrayAlloc
24e2e 61 74 65 28 0a 20 20 20 20 20 20 64 62 2c 0a 20  ate(.      db,. 
24e2f 20 20 20 20 20 70 4c 69 73 74 2d 3e 61 2c 0a 20       pList->a,. 
24e30 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c 69 73       sizeof(pLis
24e31 74 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20 20 20  t->a[0]),.      
24e32 35 2c 0a 20 20 20 20 20 20 26 70 4c 69 73 74 2d  5,.      &pList-
24e33 3e 6e 49 64 2c 0a 20 20 20 20 20 20 26 70 4c 69  >nId,.      &pLi
24e34 73 74 2d 3e 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20  st->nAlloc,.    
24e35 20 20 26 69 0a 20 20 29 3b 0a 20 20 69 66 28 20    &i.  );.  if( 
24e36 69 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  i<0 ){.    sqlit
24e37 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64  e3IdListDelete(d
24e38 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 72  b, pList);.    r
24e39 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70  eturn 0;.  }.  p
24e3a 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  List->a[i].zName
24e3b 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
24e3c 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b  omToken(db, pTok
24e3d 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c  en);.  return pL
24e3e 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  ist;.}../*.** De
24e3f 6c 65 74 65 20 61 6e 20 49 64 4c 69 73 74 2e 0a  lete an IdList..
24e40 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24e41 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 64  E void sqlite3Id
24e42 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74  ListDelete(sqlit
24e43 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a  e3 *db, IdList *
24e44 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b  pList){.  int i;
24e45 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
24e46 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28  ) return;.  for(
24e47 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49  i=0; i<pList->nI
24e48 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  d; i++){.    sql
24e49 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
24e4a 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  List->a[i].zName
24e4b 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
24e4c 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74  DbFree(db, pList
24e4d 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ->a);.  sqlite3D
24e4e 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29  bFree(db, pList)
24e4f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
24e50 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 70  n the index in p
24e51 4c 69 73 74 20 6f 66 20 74 68 65 20 69 64 65 6e  List of the iden
24e52 74 69 66 69 65 72 20 6e 61 6d 65 64 20 7a 49 64  tifier named zId
24e53 2e 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a 2a 20  .  Return -1.** 
24e54 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f  if not found..*/
24e55 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
24e56 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69 73  int sqlite3IdLis
24e57 74 49 6e 64 65 78 28 49 64 4c 69 73 74 20 2a 70  tIndex(IdList *p
24e58 4c 69 73 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  List, const char
24e59 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20   *zName){.  int 
24e5a 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  i;.  if( pList==
24e5b 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  0 ) return -1;. 
24e5c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
24e5d 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20  t->nId; i++){.  
24e5e 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
24e5f 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d  ICmp(pList->a[i]
24e60 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d  .zName, zName)==
24e61 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a 20 20  0 ) return i;.  
24e62 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d  }.  return -1;.}
24e63 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 61 6e 64 20 74  ../*.** Expand t
24e64 68 65 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  he space allocat
24e65 65 64 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  ed for the given
24e66 20 53 72 63 4c 69 73 74 20 6f 62 6a 65 63 74 20   SrcList object 
24e67 62 79 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20 6e  by.** creating n
24e68 45 78 74 72 61 20 6e 65 77 20 73 6c 6f 74 73 20  Extra new slots 
24e69 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 69 53 74  beginning at iSt
24e6a 61 72 74 2e 20 20 69 53 74 61 72 74 20 69 73 20  art.  iStart is 
24e6b 7a 65 72 6f 20 62 61 73 65 64 2e 0a 2a 2a 20 4e  zero based..** N
24e6c 65 77 20 73 6c 6f 74 73 20 61 72 65 20 7a 65 72  ew slots are zer
24e6d 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65  oed..**.** For e
24e6e 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 20  xample, suppose 
24e6f 61 20 53 72 63 4c 69 73 74 20 69 6e 69 74 69 61  a SrcList initia
24e70 6c 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f  lly contains two
24e71 20 65 6e 74 72 69 65 73 3a 20 41 2c 42 2e 0a 2a   entries: A,B..*
24e72 2a 20 54 6f 20 61 70 70 65 6e 64 20 33 20 6e 65  * To append 3 ne
24e73 77 20 65 6e 74 72 69 65 73 20 6f 6e 74 6f 20 74  w entries onto t
24e74 68 65 20 65 6e 64 2c 20 64 6f 20 74 68 69 73 3a  he end, do this:
24e75 0a 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65  .**.**    sqlite
24e76 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28  3SrcListEnlarge(
24e77 64 62 2c 20 70 53 72 63 6c 69 73 74 2c 20 33 2c  db, pSrclist, 3,
24e78 20 32 29 3b 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72   2);.**.** After
24e79 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 20   the call above 
24e7a 69 74 20 77 6f 75 6c 64 20 63 6f 6e 74 61 69 6e  it would contain
24e7b 3a 20 20 41 2c 20 42 2c 20 6e 69 6c 2c 20 6e 69  :  A, B, nil, ni
24e7c 6c 2c 20 6e 69 6c 2e 0a 2a 2a 20 49 66 20 74 68  l, nil..** If th
24e7d 65 20 69 53 74 61 72 74 20 61 72 67 75 6d 65 6e  e iStart argumen
24e7e 74 20 68 61 64 20 62 65 65 6e 20 31 20 69 6e 73  t had been 1 ins
24e7f 74 65 61 64 20 6f 66 20 32 2c 20 74 68 65 6e 20  tead of 2, then 
24e80 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 77 6f  the result.** wo
24e81 75 6c 64 20 68 61 76 65 20 62 65 65 6e 3a 20 20  uld have been:  
24e82 41 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c  A, nil, nil, nil
24e83 2c 20 42 2e 20 20 54 6f 20 70 72 65 70 65 6e 64  , B.  To prepend
24e84 20 74 68 65 20 6e 65 77 20 73 6c 6f 74 73 2c 0a   the new slots,.
24e85 2a 2a 20 74 68 65 20 69 53 74 61 72 74 20 76 61  ** the iStart va
24e86 6c 75 65 20 77 6f 75 6c 64 20 62 65 20 30 2e 20  lue would be 0. 
24e87 20 54 68 65 20 72 65 73 75 6c 74 20 74 68 65 6e   The result then
24e88 20 77 6f 75 6c 64 0a 2a 2a 20 62 65 3a 20 6e 69   would.** be: ni
24e89 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 41 2c 20  l, nil, nil, A, 
24e8a 42 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65  B..**.** If a me
24e8b 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
24e8c 66 61 69 6c 73 20 74 68 65 20 53 72 63 4c 69 73  fails the SrcLis
24e8d 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 20  t is unchanged. 
24e8e 20 54 68 65 0a 2a 2a 20 64 62 2d 3e 6d 61 6c 6c   The.** db->mall
24e8f 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 77 69  ocFailed flag wi
24e90 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 74 72 75  ll be set to tru
24e91 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
24e92 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71  VATE SrcList *sq
24e93 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61  lite3SrcListEnla
24e94 72 67 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  rge(.  sqlite3 *
24e95 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 44 61 74  db,       /* Dat
24e96 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
24e97 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 4f 4f   to notify of OO
24e98 4d 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 53 72  M errors */.  Sr
24e99 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20  cList *pSrc,    
24e9a 20 2f 2a 20 54 68 65 20 53 72 63 4c 69 73 74 20   /* The SrcList 
24e9b 74 6f 20 62 65 20 65 6e 6c 61 72 67 65 64 20 2a  to be enlarged *
24e9c 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20  /.  int nExtra, 
24e9d 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
24e9e 20 6f 66 20 6e 65 77 20 73 6c 6f 74 73 20 74 6f   of new slots to
24e9f 20 61 64 64 20 74 6f 20 70 53 72 63 2d 3e 61 5b   add to pSrc->a[
24ea0 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 72  ] */.  int iStar
24ea1 74 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  t         /* Ind
24ea2 65 78 20 69 6e 20 70 53 72 63 2d 3e 61 5b 5d 20  ex in pSrc->a[] 
24ea3 6f 66 20 66 69 72 73 74 20 6e 65 77 20 73 6c 6f  of first new slo
24ea4 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  t */.){.  int i;
24ea5 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68  ..  /* Sanity ch
24ea6 65 63 6b 69 6e 67 20 6f 6e 20 63 61 6c 6c 69 6e  ecking on callin
24ea7 67 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a  g parameters */.
24ea8 20 20 61 73 73 65 72 74 28 20 69 53 74 61 72 74    assert( iStart
24ea9 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  >=0 );.  assert(
24eaa 20 6e 45 78 74 72 61 3e 3d 31 20 29 3b 0a 20 20   nExtra>=1 );.  
24eab 61 73 73 65 72 74 28 20 70 53 72 63 21 3d 30 20  assert( pSrc!=0 
24eac 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74  );.  assert( iSt
24ead 61 72 74 3c 3d 70 53 72 63 2d 3e 6e 53 72 63 20  art<=pSrc->nSrc 
24eae 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  );..  /* Allocat
24eaf 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 70 61  e additional spa
24eb0 63 65 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a  ce if needed */.
24eb1 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 72 63    if( pSrc->nSrc
24eb2 2b 6e 45 78 74 72 61 3e 70 53 72 63 2d 3e 6e 41  +nExtra>pSrc->nA
24eb3 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 53 72 63 4c  lloc ){.    SrcL
24eb4 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69  ist *pNew;.    i
24eb5 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 70 53 72 63  nt nAlloc = pSrc
24eb6 2d 3e 6e 53 72 63 2b 6e 45 78 74 72 61 3b 0a 20  ->nSrc+nExtra;. 
24eb7 20 20 20 69 6e 74 20 6e 47 6f 74 3b 0a 20 20 20     int nGot;.   
24eb8 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
24eb9 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 53 72  bRealloc(db, pSr
24eba 63 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  c,.             
24ebb 20 20 73 69 7a 65 6f 66 28 2a 70 53 72 63 29 20    sizeof(*pSrc) 
24ebc 2b 20 28 6e 41 6c 6c 6f 63 2d 31 29 2a 73 69 7a  + (nAlloc-1)*siz
24ebd 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 20  eof(pSrc->a[0]) 
24ebe 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d  );.    if( pNew=
24ebf 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
24ec0 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  rt( db->mallocFa
24ec1 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65  iled );.      re
24ec2 74 75 72 6e 20 70 53 72 63 3b 0a 20 20 20 20 7d  turn pSrc;.    }
24ec3 0a 20 20 20 20 70 53 72 63 20 3d 20 70 4e 65 77  .    pSrc = pNew
24ec4 3b 0a 20 20 20 20 6e 47 6f 74 20 3d 20 28 73 71  ;.    nGot = (sq
24ec5 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
24ec6 65 28 64 62 2c 20 70 4e 65 77 29 20 2d 20 73 69  e(db, pNew) - si
24ec7 7a 65 6f 66 28 2a 70 53 72 63 29 29 2f 73 69 7a  zeof(*pSrc))/siz
24ec8 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2b  eof(pSrc->a[0])+
24ec9 31 3b 0a 20 20 20 20 70 53 72 63 2d 3e 6e 41 6c  1;.    pSrc->nAl
24eca 6c 6f 63 20 3d 20 28 75 31 36 29 6e 47 6f 74 3b  loc = (u16)nGot;
24ecb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20  .  }..  /* Move 
24ecc 65 78 69 73 74 69 6e 67 20 73 6c 6f 74 73 20 74  existing slots t
24ecd 68 61 74 20 63 6f 6d 65 20 61 66 74 65 72 20 74  hat come after t
24ece 68 65 20 6e 65 77 6c 79 20 69 6e 73 65 72 74 65  he newly inserte
24ecf 64 20 73 6c 6f 74 73 0a 20 20 2a 2a 20 6f 75 74  d slots.  ** out
24ed0 20 6f 66 20 74 68 65 20 77 61 79 20 2a 2f 0a 20   of the way */. 
24ed1 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72   for(i=pSrc->nSr
24ed2 63 2d 31 3b 20 69 3e 3d 69 53 74 61 72 74 3b 20  c-1; i>=iStart; 
24ed3 69 2d 2d 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e  i--){.    pSrc->
24ed4 61 5b 69 2b 6e 45 78 74 72 61 5d 20 3d 20 70 53  a[i+nExtra] = pS
24ed5 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20  rc->a[i];.  }.  
24ed6 70 53 72 63 2d 3e 6e 53 72 63 20 2b 3d 20 28 69  pSrc->nSrc += (i
24ed7 31 36 29 6e 45 78 74 72 61 3b 0a 0a 20 20 2f 2a  16)nExtra;..  /*
24ed8 20 5a 65 72 6f 20 74 68 65 20 6e 65 77 6c 79 20   Zero the newly 
24ed9 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20  allocated slots 
24eda 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 70 53 72  */.  memset(&pSr
24edb 63 2d 3e 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c  c->a[iStart], 0,
24edc 20 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b   sizeof(pSrc->a[
24edd 30 5d 29 2a 6e 45 78 74 72 61 29 3b 0a 20 20 66  0])*nExtra);.  f
24ede 6f 72 28 69 3d 69 53 74 61 72 74 3b 20 69 3c 69  or(i=iStart; i<i
24edf 53 74 61 72 74 2b 6e 45 78 74 72 61 3b 20 69 2b  Start+nExtra; i+
24ee0 2b 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b  +){.    pSrc->a[
24ee1 69 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b  i].iCursor = -1;
24ee2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72  .  }..  /* Retur
24ee3 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
24ee4 68 65 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c  he enlarged SrcL
24ee5 69 73 74 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ist */.  return 
24ee6 70 53 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  pSrc;.}.../*.** 
24ee7 41 70 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62  Append a new tab
24ee8 6c 65 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67  le name to the g
24ee9 69 76 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43  iven SrcList.  C
24eea 72 65 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c  reate a new SrcL
24eeb 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62  ist if.** need b
24eec 65 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20  e.  A new entry 
24eed 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 74 68  is created in th
24eee 65 20 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69  e SrcList even i
24eef 66 20 70 54 61 62 6c 65 20 69 73 20 4e 55 4c 4c  f pTable is NULL
24ef0 2e 0a 2a 2a 0a 2a 2a 20 41 20 53 72 63 4c 69 73  ..**.** A SrcLis
24ef1 74 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f  t is returned, o
24ef2 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20  r NULL if there 
24ef3 69 73 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e  is an OOM error.
24ef4 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 0a 2a    The returned.*
24ef5 2a 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20  * SrcList might 
24ef6 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  be the same as t
24ef7 68 65 20 53 72 63 4c 69 73 74 20 74 68 61 74 20  he SrcList that 
24ef8 77 61 73 20 69 6e 70 75 74 20 6f 72 20 69 74 20  was input or it 
24ef9 6d 69 67 68 74 20 62 65 0a 2a 2a 20 61 20 6e 65  might be.** a ne
24efa 77 20 6f 6e 65 2e 20 20 49 66 20 61 6e 20 4f 4f  w one.  If an OO
24efb 4d 20 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63  M error does occ
24efc 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 70 72  urs, then the pr
24efd 69 6f 72 20 76 61 6c 75 65 20 6f 66 20 70 4c 69  ior value of pLi
24efe 73 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e  st.** that is in
24eff 70 75 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74  put to this rout
24f00 69 6e 65 20 69 73 20 61 75 74 6f 6d 61 74 69 63  ine is automatic
24f01 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a  ally freed..**.*
24f02 2a 20 49 66 20 70 44 61 74 61 62 61 73 65 20 69  * If pDatabase i
24f03 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d  s not null, it m
24f04 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 61  eans that the ta
24f05 62 6c 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f  ble has an optio
24f06 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  nal.** database 
24f07 6e 61 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69  name prefix.  Li
24f08 6b 65 20 74 68 69 73 3a 20 20 22 64 61 74 61 62  ke this:  "datab
24f09 61 73 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65  ase.table".  The
24f0a 20 70 44 61 74 61 62 61 73 65 0a 2a 2a 20 70 6f   pDatabase.** po
24f0b 69 6e 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c  ints to the tabl
24f0c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70  e name and the p
24f0d 54 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20  Table points to 
24f0e 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
24f0f 65 2e 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69 73  e..** The SrcLis
24f10 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c  t.a[].zName fiel
24f11 64 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68  d is filled with
24f12 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
24f13 77 68 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20 63  which might.** c
24f14 6f 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 20  ome from pTable 
24f15 28 69 66 20 70 44 61 74 61 62 61 73 65 20 69 73  (if pDatabase is
24f16 20 4e 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20 70   NULL) or from p
24f17 44 61 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20 53  Database.  .** S
24f18 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74 61  rcList.a[].zData
24f19 62 61 73 65 20 69 73 20 66 69 6c 6c 65 64 20 77  base is filled w
24f1a 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65  ith the database
24f1b 20 6e 61 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c   name from pTabl
24f1c 65 2c 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e 55  e,.** or with NU
24f1d 4c 4c 20 69 66 20 6e 6f 20 64 61 74 61 62 61 73  LL if no databas
24f1e 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a  e is specified..
24f1f 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  **.** In other w
24f20 6f 72 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c 69  ords, if call li
24f21 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
24f22 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72         sqlite3Sr
24f23 63 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c  cListAppend(D,A,
24f24 42 2c 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e  B,0);.**.** Then
24f25 20 42 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61   B is a table na
24f26 6d 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62  me and the datab
24f27 61 73 65 20 6e 61 6d 65 20 69 73 20 75 6e 73 70  ase name is unsp
24f28 65 63 69 66 69 65 64 2e 20 20 49 66 20 63 61 6c  ecified.  If cal
24f29 6c 65 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73  led.** like this
24f2a 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
24f2b 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
24f2c 70 65 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a  pend(D,A,B,C);.*
24f2d 2a 0a 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74  *.** Then C is t
24f2e 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e  he table name an
24f2f 64 20 42 20 69 73 20 74 68 65 20 64 61 74 61 62  d B is the datab
24f30 61 73 65 20 6e 61 6d 65 2e 20 20 49 66 20 43 20  ase name.  If C 
24f31 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 74 68  is defined.** th
24f32 65 6e 20 73 6f 20 69 73 20 42 2e 20 20 49 6e 20  en so is B.  In 
24f33 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 65 20  other words, we 
24f34 6e 65 76 65 72 20 68 61 76 65 20 61 20 63 61 73  never have a cas
24f35 65 20 77 68 65 72 65 3a 0a 2a 2a 0a 2a 2a 20 20  e where:.**.**  
24f36 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72         sqlite3Sr
24f37 63 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c  cListAppend(D,A,
24f38 30 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20 42 6f 74 68  0,C);.**.** Both
24f39 20 70 54 61 62 6c 65 20 61 6e 64 20 70 44 61 74   pTable and pDat
24f3a 61 62 61 73 65 20 61 72 65 20 61 73 73 75 6d 65  abase are assume
24f3b 64 20 74 6f 20 62 65 20 71 75 6f 74 65 64 2e 20  d to be quoted. 
24f3c 20 54 68 65 79 20 61 72 65 20 64 65 71 75 6f 74   They are dequot
24f3d 65 64 0a 2a 2a 20 62 65 66 6f 72 65 20 62 65 69  ed.** before bei
24f3e 6e 67 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  ng added to the 
24f3f 53 72 63 4c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49  SrcList..*/.SQLI
24f40 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69  TE_PRIVATE SrcLi
24f41 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69  st *sqlite3SrcLi
24f42 73 74 41 70 70 65 6e 64 28 0a 20 20 73 71 6c 69  stAppend(.  sqli
24f43 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
24f44 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f  /* Connection to
24f45 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f   notify of mallo
24f46 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20  c failures */.  
24f47 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  SrcList *pList, 
24f48 20 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 6f      /* Append to
24f49 20 74 68 69 73 20 53 72 63 4c 69 73 74 2e 20 4e   this SrcList. N
24f4a 55 4c 4c 20 63 72 65 61 74 65 73 20 61 20 6e 65  ULL creates a ne
24f4b 77 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 54  w SrcList */.  T
24f4c 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20  oken *pTable,   
24f4d 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 61     /* Table to a
24f4e 70 70 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ppend */.  Token
24f4f 20 2a 70 44 61 74 61 62 61 73 65 20 20 20 20 2f   *pDatabase    /
24f50 2a 20 44 61 74 61 62 61 73 65 20 6f 66 20 74 68  * Database of th
24f51 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20  e table */.){.  
24f52 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
24f53 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 61 73  tem *pItem;.  as
24f54 73 65 72 74 28 20 70 44 61 74 61 62 61 73 65 3d  sert( pDatabase=
24f55 3d 30 20 7c 7c 20 70 54 61 62 6c 65 21 3d 30 20  =0 || pTable!=0 
24f56 29 3b 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61  );  /* Cannot ha
24f57 76 65 20 43 20 77 69 74 68 6f 75 74 20 42 20 2a  ve C without B *
24f58 2f 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  /.  if( pList==0
24f59 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20   ){.    pList = 
24f5a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
24f5b 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 53  ero(db, sizeof(S
24f5c 72 63 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69  rcList) );.    i
24f5d 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
24f5e 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73  turn 0;.    pLis
24f5f 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 31 3b 0a 20  t->nAlloc = 1;. 
24f60 20 7d 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c   }.  pList = sql
24f61 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
24f62 67 65 28 64 62 2c 20 70 4c 69 73 74 2c 20 31 2c  ge(db, pList, 1,
24f63 20 70 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 0a 20   pList->nSrc);. 
24f64 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
24f65 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c  ailed ){.    sql
24f66 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
24f67 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20  e(db, pList);.  
24f68 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
24f69 20 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74    pItem = &pList
24f6a 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 53 72 63 2d  ->a[pList->nSrc-
24f6b 31 5d 3b 0a 20 20 69 66 28 20 70 44 61 74 61 62  1];.  if( pDatab
24f6c 61 73 65 20 26 26 20 70 44 61 74 61 62 61 73 65  ase && pDatabase
24f6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44  ->z==0 ){.    pD
24f6e 61 74 61 62 61 73 65 20 3d 20 30 3b 0a 20 20 7d  atabase = 0;.  }
24f6f 0a 20 20 69 66 28 20 70 44 61 74 61 62 61 73 65  .  if( pDatabase
24f70 20 29 7b 0a 20 20 20 20 54 6f 6b 65 6e 20 2a 70   ){.    Token *p
24f71 54 65 6d 70 20 3d 20 70 44 61 74 61 62 61 73 65  Temp = pDatabase
24f72 3b 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20  ;.    pDatabase 
24f73 3d 20 70 54 61 62 6c 65 3b 0a 20 20 20 20 70 54  = pTable;.    pT
24f74 61 62 6c 65 20 3d 20 70 54 65 6d 70 3b 0a 20 20  able = pTemp;.  
24f75 7d 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  }.  pItem->zName
24f76 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
24f77 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62  omToken(db, pTab
24f78 6c 65 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 7a 44  le);.  pItem->zD
24f79 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65  atabase = sqlite
24f7a 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
24f7b 62 2c 20 70 44 61 74 61 62 61 73 65 29 3b 0a 20  b, pDatabase);. 
24f7c 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 7d   return pList;.}
24f7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 56  ../*.** Assign V
24f7e 64 62 65 43 75 72 73 6f 72 20 69 6e 64 65 78 20  dbeCursor index 
24f7f 6e 75 6d 62 65 72 73 20 74 6f 20 61 6c 6c 20 74  numbers to all t
24f80 61 62 6c 65 73 20 69 6e 20 61 20 53 72 63 4c 69  ables in a SrcLi
24f81 73 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  st.*/.SQLITE_PRI
24f82 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
24f83 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75  3SrcListAssignCu
24f84 72 73 6f 72 73 28 50 61 72 73 65 20 2a 70 50 61  rsors(Parse *pPa
24f85 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c  rse, SrcList *pL
24f86 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ist){.  int i;. 
24f87 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
24f88 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 61  item *pItem;.  a
24f89 73 73 65 72 74 28 70 4c 69 73 74 20 7c 7c 20 70  ssert(pList || p
24f8a 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
24f8b 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28  cFailed );.  if(
24f8c 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f   pList ){.    fo
24f8d 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69  r(i=0, pItem=pLi
24f8e 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e  st->a; i<pList->
24f8f 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  nSrc; i++, pItem
24f90 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
24f91 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3e 3d 30  Item->iCursor>=0
24f92 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
24f93 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d  pItem->iCursor =
24f94 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
24f95 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d  .      if( pItem
24f96 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
24f97 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c       sqlite3SrcL
24f98 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73  istAssignCursors
24f99 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e  (pParse, pItem->
24f9a 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 29 3b 0a  pSelect->pSrc);.
24f9b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
24f9c 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  }.}../*.** Delet
24f9d 65 20 61 6e 20 65 6e 74 69 72 65 20 53 72 63 4c  e an entire SrcL
24f9e 69 73 74 20 69 6e 63 6c 75 64 69 6e 67 20 61 6c  ist including al
24f9f 6c 20 69 74 73 20 73 75 62 73 74 72 75 63 74 75  l its substructu
24fa0 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  re..*/.SQLITE_PR
24fa1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
24fa2 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
24fa3 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63  sqlite3 *db, Src
24fa4 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20  List *pList){.  
24fa5 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
24fa6 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
24fa7 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  tem;.  if( pList
24fa8 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
24fa9 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d  for(pItem=pList-
24faa 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74  >a, i=0; i<pList
24fab 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74  ->nSrc; i++, pIt
24fac 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74  em++){.    sqlit
24fad 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74  e3DbFree(db, pIt
24fae 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a  em->zDatabase);.
24faf 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
24fb0 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61  e(db, pItem->zNa
24fb1 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  me);.    sqlite3
24fb2 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d  DbFree(db, pItem
24fb3 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 73  ->zAlias);.    s
24fb4 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
24fb5 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b   pItem->zIndex);
24fb6 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
24fb7 74 65 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70  teTable(pItem->p
24fb8 54 61 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Tab);.    sqlite
24fb9 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
24fba 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74  , pItem->pSelect
24fbb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
24fbc 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74  prDelete(db, pIt
24fbd 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 73 71  em->pOn);.    sq
24fbe 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
24fbf 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 55 73  e(db, pItem->pUs
24fc0 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ing);.  }.  sqli
24fc1 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
24fc2 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ist);.}../*.** T
24fc3 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
24fc4 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
24fc5 73 65 72 20 74 6f 20 61 64 64 20 61 20 6e 65 77  ser to add a new
24fc6 20 74 65 72 6d 20 74 6f 20 74 68 65 0a 2a 2a 20   term to the.** 
24fc7 65 6e 64 20 6f 66 20 61 20 67 72 6f 77 69 6e 67  end of a growing
24fc8 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54   FROM clause.  T
24fc9 68 65 20 22 70 22 20 70 61 72 61 6d 65 74 65 72  he "p" parameter
24fca 20 69 73 20 74 68 65 20 70 61 72 74 20 6f 66 0a   is the part of.
24fcb 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  ** the FROM clau
24fcc 73 65 20 74 68 61 74 20 68 61 73 20 61 6c 72 65  se that has alre
24fcd 61 64 79 20 62 65 65 6e 20 63 6f 6e 73 74 72 75  ady been constru
24fce 63 74 65 64 2e 20 20 22 70 22 20 69 73 20 4e 55  cted.  "p" is NU
24fcf 4c 4c 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73  LL.** if this is
24fd0 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 20   the first term 
24fd1 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
24fd2 73 65 2e 20 20 70 54 61 62 6c 65 20 61 6e 64 20  se.  pTable and 
24fd3 70 44 61 74 61 62 61 73 65 0a 2a 2a 20 61 72 65  pDatabase.** are
24fd4 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
24fd5 20 74 61 62 6c 65 20 61 6e 64 20 64 61 74 61 62   table and datab
24fd6 61 73 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65  ase named in the
24fd7 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
24fd8 6d 2e 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20  m..** pDatabase 
24fd9 69 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 64  is NULL if the d
24fda 61 74 61 62 61 73 65 20 6e 61 6d 65 20 71 75 61  atabase name qua
24fdb 6c 69 66 69 65 72 20 69 73 20 6d 69 73 73 69 6e  lifier is missin
24fdc 67 20 2d 20 74 68 65 0a 2a 2a 20 75 73 75 61 6c  g - the.** usual
24fdd 20 63 61 73 65 2e 20 20 49 66 20 74 68 65 20 74   case.  If the t
24fde 65 72 6d 20 68 61 73 20 61 20 61 6c 69 61 73 2c  erm has a alias,
24fdf 20 74 68 65 6e 20 70 41 6c 69 61 73 20 70 6f 69   then pAlias poi
24fe0 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c  nts to the.** al
24fe1 69 61 73 20 74 6f 6b 65 6e 2e 20 20 49 66 20 74  ias token.  If t
24fe2 68 65 20 74 65 72 6d 20 69 73 20 61 20 73 75 62  he term is a sub
24fe3 71 75 65 72 79 2c 20 74 68 65 6e 20 70 53 75 62  query, then pSub
24fe4 71 75 65 72 79 20 69 73 20 74 68 65 0a 2a 2a 20  query is the.** 
24fe5 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
24fe6 20 74 68 61 74 20 74 68 65 20 73 75 62 71 75 65   that the subque
24fe7 72 79 20 65 6e 63 6f 64 65 73 2e 20 20 54 68 65  ry encodes.  The
24fe8 20 70 54 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 70   pTable and.** p
24fe9 44 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74  Database paramet
24fea 65 72 73 20 61 72 65 20 4e 55 4c 4c 20 66 6f 72  ers are NULL for
24feb 20 73 75 62 71 75 65 72 69 65 73 2e 20 20 54 68   subqueries.  Th
24fec 65 20 70 4f 6e 20 61 6e 64 20 70 55 73 69 6e 67  e pOn and pUsing
24fed 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61  .** parameters a
24fee 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  re the content o
24fef 66 20 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49  f the ON and USI
24ff0 4e 47 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a  NG clauses..**.*
24ff1 2a 20 52 65 74 75 72 6e 20 61 20 6e 65 77 20 53  * Return a new S
24ff2 72 63 4c 69 73 74 20 77 68 69 63 68 20 65 6e 63  rcList which enc
24ff3 6f 64 65 73 20 69 73 20 74 68 65 20 46 52 4f 4d  odes is the FROM
24ff4 20 77 69 74 68 20 74 68 65 20 6e 65 77 0a 2a 2a   with the new.**
24ff5 20 74 65 72 6d 20 61 64 64 65 64 2e 0a 2a 2f 0a   term added..*/.
24ff6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
24ff7 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
24ff8 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d  rcListAppendFrom
24ff9 54 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70  Term(.  Parse *p
24ffa 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
24ffb 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
24ffc 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
24ffd 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
24ffe 2f 2a 20 54 68 65 20 6c 65 66 74 20 70 61 72 74  /* The left part
24fff 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
25000 75 73 65 20 61 6c 72 65 61 64 79 20 73 65 65 6e  use already seen
25001 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61   */.  Token *pTa
25002 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ble,          /*
25003 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   Name of the tab
25004 6c 65 20 74 6f 20 61 64 64 20 74 6f 20 74 68 65  le to add to the
25005 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
25006 20 20 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61    Token *pDataba
25007 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  se,       /* Nam
25008 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
25009 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61  e containing pTa
2500a 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ble */.  Token *
2500b 70 41 6c 69 61 73 2c 20 20 20 20 20 20 20 20 20  pAlias,         
2500c 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61   /* The right-ha
2500d 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 41  nd side of the A
2500e 53 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20  S subexpression 
2500f 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75  */.  Select *pSu
25010 62 71 75 65 72 79 2c 20 20 20 20 20 20 2f 2a 20  bquery,      /* 
25011 41 20 73 75 62 71 75 65 72 79 20 75 73 65 64 20  A subquery used 
25012 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61  in place of a ta
25013 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78  ble name */.  Ex
25014 70 72 20 2a 70 4f 6e 2c 20 20 20 20 20 20 20 20  pr *pOn,        
25015 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e 20        /* The ON 
25016 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e  clause of a join
25017 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 55   */.  IdList *pU
25018 73 69 6e 67 20 20 20 20 20 20 20 20 20 20 2f 2a  sing          /*
25019 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73   The USING claus
2501a 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 29  e of a join */.)
2501b 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  {.  struct SrcLi
2501c 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
2501d 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2501e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
2501f 28 20 21 70 20 26 26 20 28 70 4f 6e 20 7c 7c 20  ( !p && (pOn || 
25020 70 55 73 69 6e 67 29 20 29 7b 0a 20 20 20 20 73  pUsing) ){.    s
25021 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
25022 50 61 72 73 65 2c 20 22 61 20 4a 4f 49 4e 20 63  Parse, "a JOIN c
25023 6c 61 75 73 65 20 69 73 20 72 65 71 75 69 72 65  lause is require
25024 64 20 62 65 66 6f 72 65 20 25 73 22 2c 20 0a 20  d before %s", . 
25025 20 20 20 20 20 28 70 4f 6e 20 3f 20 22 4f 4e 22       (pOn ? "ON"
25026 20 3a 20 22 55 53 49 4e 47 22 29 0a 20 20 20 20   : "USING").    
25027 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 70 70 65  );.    goto appe
25028 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0a 20  nd_from_error;. 
25029 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33   }.  p = sqlite3
2502a 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62  SrcListAppend(db
2502b 2c 20 70 2c 20 70 54 61 62 6c 65 2c 20 70 44 61  , p, pTable, pDa
2502c 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70  tabase);.  if( p
2502d 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d 3e  ==0 || NEVER(p->
2502e 6e 53 72 63 3d 3d 30 29 20 29 7b 0a 20 20 20 20  nSrc==0) ){.    
2502f 67 6f 74 6f 20 61 70 70 65 6e 64 5f 66 72 6f 6d  goto append_from
25030 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 49  _error;.  }.  pI
25031 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e  tem = &p->a[p->n
25032 53 72 63 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74  Src-1];.  assert
25033 28 20 70 41 6c 69 61 73 21 3d 30 20 29 3b 0a 20  ( pAlias!=0 );. 
25034 20 69 66 28 20 70 41 6c 69 61 73 2d 3e 6e 20 29   if( pAlias->n )
25035 7b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 41 6c  {.    pItem->zAl
25036 69 61 73 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ias = sqlite3Nam
25037 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
25038 41 6c 69 61 73 29 3b 0a 20 20 7d 0a 20 20 70 49  Alias);.  }.  pI
25039 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70  tem->pSelect = p
2503a 53 75 62 71 75 65 72 79 3b 0a 20 20 70 49 74 65  Subquery;.  pIte
2503b 6d 2d 3e 70 4f 6e 20 3d 20 70 4f 6e 3b 0a 20 20  m->pOn = pOn;.  
2503c 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20  pItem->pUsing = 
2503d 70 55 73 69 6e 67 3b 0a 20 20 72 65 74 75 72 6e  pUsing;.  return
2503e 20 70 3b 0a 0a 20 61 70 70 65 6e 64 5f 66 72 6f   p;.. append_fro
2503f 6d 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72  m_error:.  asser
25040 74 28 20 70 3d 3d 30 20 29 3b 0a 20 20 73 71 6c  t( p==0 );.  sql
25041 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
25042 62 2c 20 70 4f 6e 29 3b 0a 20 20 73 71 6c 69 74  b, pOn);.  sqlit
25043 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64  e3IdListDelete(d
25044 62 2c 20 70 55 73 69 6e 67 29 3b 0a 20 20 73 71  b, pUsing);.  sq
25045 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
25046 65 28 64 62 2c 20 70 53 75 62 71 75 65 72 79 29  e(db, pSubquery)
25047 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
25048 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e  ./*.** Add an IN
25049 44 45 58 45 44 20 42 59 20 6f 72 20 4e 4f 54 20  DEXED BY or NOT 
2504a 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20 74  INDEXED clause t
2504b 6f 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  o the most recen
2504c 74 6c 79 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c  tly added .** el
2504d 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 73 6f 75  ement of the sou
2504e 72 63 65 2d 6c 69 73 74 20 70 61 73 73 65 64 20  rce-list passed 
2504f 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
25050 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
25051 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
25052 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64  qlite3SrcListInd
25053 65 78 65 64 42 79 28 50 61 72 73 65 20 2a 70 50  exedBy(Parse *pP
25054 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
25055 2c 20 54 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65  , Token *pIndexe
25056 64 42 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20  dBy){.  assert( 
25057 70 49 6e 64 65 78 65 64 42 79 21 3d 30 20 29 3b  pIndexedBy!=0 );
25058 0a 20 20 69 66 28 20 70 20 26 26 20 41 4c 57 41  .  if( p && ALWA
25059 59 53 28 70 2d 3e 6e 53 72 63 3e 30 29 20 29 7b  YS(p->nSrc>0) ){
2505a 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
2505b 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
2505c 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d  = &p->a[p->nSrc-
2505d 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  1];.    assert( 
2505e 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65  pItem->notIndexe
2505f 64 3d 3d 30 20 26 26 20 70 49 74 65 6d 2d 3e 7a  d==0 && pItem->z
25060 49 6e 64 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20  Index==0 );.    
25061 69 66 28 20 70 49 6e 64 65 78 65 64 42 79 2d 3e  if( pIndexedBy->
25062 6e 3d 3d 31 20 26 26 20 21 70 49 6e 64 65 78 65  n==1 && !pIndexe
25063 64 42 79 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20  dBy->z ){.      
25064 2f 2a 20 41 20 22 4e 4f 54 20 49 4e 44 45 58 45  /* A "NOT INDEXE
25065 44 22 20 63 6c 61 75 73 65 20 77 61 73 20 73 75  D" clause was su
25066 70 70 6c 69 65 64 2e 20 53 65 65 20 70 61 72 73  pplied. See pars
25067 65 2e 79 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f  e.y .      ** co
25068 6e 73 74 72 75 63 74 20 22 69 6e 64 65 78 65 64  nstruct "indexed
25069 5f 6f 70 74 22 20 66 6f 72 20 64 65 74 61 69 6c  _opt" for detail
2506a 73 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65  s. */.      pIte
2506b 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20  m->notIndexed = 
2506c 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
2506d 20 20 20 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65      pItem->zInde
2506e 78 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  x = sqlite3NameF
2506f 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d  romToken(pParse-
25070 3e 64 62 2c 20 70 49 6e 64 65 78 65 64 42 79 29  >db, pIndexedBy)
25071 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
25072 2a 0a 2a 2a 20 57 68 65 6e 20 62 75 69 6c 64 69  *.** When buildi
25073 6e 67 20 75 70 20 61 20 46 52 4f 4d 20 63 6c 61  ng up a FROM cla
25074 75 73 65 20 69 6e 20 74 68 65 20 70 61 72 73 65  use in the parse
25075 72 2c 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72  r, the join oper
25076 61 74 6f 72 0a 2a 2a 20 69 73 20 69 6e 69 74 69  ator.** is initi
25077 61 6c 6c 79 20 61 74 74 61 63 68 65 64 20 74 6f  ally attached to
25078 20 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e   the left operan
25079 64 2e 20 20 42 75 74 20 74 68 65 20 63 6f 64 65  d.  But the code
2507a 20 67 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 65 78   generator.** ex
2507b 70 65 63 74 73 20 74 68 65 20 6a 6f 69 6e 20 6f  pects the join o
2507c 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 6f 6e  perator to be on
2507d 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   the right opera
2507e 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  nd.  This routin
2507f 65 0a 2a 2a 20 53 68 69 66 74 73 20 61 6c 6c 20  e.** Shifts all 
25080 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 73 20 66  join operators f
25081 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68  rom left to righ
25082 74 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20  t for an entire 
25083 46 52 4f 4d 0a 2a 2a 20 63 6c 61 75 73 65 2e 0a  FROM.** clause..
25084 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 20 53  **.** Example: S
25085 75 70 70 6f 73 65 20 74 68 65 20 6a 6f 69 6e 20  uppose the join 
25086 69 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  is like this:.**
25087 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 41 20  .**           A 
25088 6e 61 74 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f  natural cross jo
25089 69 6e 20 42 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  in B.**.** The o
2508a 70 65 72 61 74 6f 72 20 69 73 20 22 6e 61 74 75  perator is "natu
2508b 72 61 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 22 2e  ral cross join".
2508c 20 20 54 68 65 20 41 20 61 6e 64 20 42 20 6f 70    The A and B op
2508d 65 72 61 6e 64 73 20 61 72 65 20 73 74 6f 72 65  erands are store
2508e 64 0a 2a 2a 20 69 6e 20 70 2d 3e 61 5b 30 5d 20  d.** in p->a[0] 
2508f 61 6e 64 20 70 2d 3e 61 5b 31 5d 2c 20 72 65 73  and p->a[1], res
25090 70 65 63 74 69 76 65 6c 79 2e 20 20 54 68 65 20  pectively.  The 
25091 70 61 72 73 65 72 20 69 6e 69 74 69 61 6c 6c 79  parser initially
25092 20 73 74 6f 72 65 73 20 74 68 65 0a 2a 2a 20 6f   stores the.** o
25093 70 65 72 61 74 6f 72 20 77 69 74 68 20 41 2e 20  perator with A. 
25094 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   This routine sh
25095 69 66 74 73 20 74 68 61 74 20 6f 70 65 72 61 74  ifts that operat
25096 6f 72 20 6f 76 65 72 20 74 6f 20 42 2e 0a 2a 2f  or over to B..*/
25097 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
25098 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
25099 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65  istShiftJoinType
2509a 28 53 72 63 4c 69 73 74 20 2a 70 29 7b 0a 20 20  (SrcList *p){.  
2509b 69 66 28 20 70 20 26 26 20 70 2d 3e 61 20 29 7b  if( p && p->a ){
2509c 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
2509d 66 6f 72 28 69 3d 70 2d 3e 6e 53 72 63 2d 31 3b  for(i=p->nSrc-1;
2509e 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20   i>0; i--){.    
2509f 20 20 70 2d 3e 61 5b 69 5d 2e 6a 6f 69 6e 74 79    p->a[i].jointy
250a0 70 65 20 3d 20 70 2d 3e 61 5b 69 2d 31 5d 2e 6a  pe = p->a[i-1].j
250a1 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 7d 0a 20  ointype;.    }. 
250a2 20 20 20 70 2d 3e 61 5b 30 5d 2e 6a 6f 69 6e 74     p->a[0].joint
250a3 79 70 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  ype = 0;.  }.}..
250a4 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72  /*.** Begin a tr
250a5 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c  ansaction.*/.SQL
250a6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
250a7 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61   sqlite3BeginTra
250a8 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a  nsaction(Parse *
250a9 70 50 61 72 73 65 2c 20 69 6e 74 20 74 79 70 65  pParse, int type
250aa 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
250ab 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  ;.  Vdbe *v;.  i
250ac 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28  nt i;..  assert(
250ad 20 70 50 61 72 73 65 21 3d 30 20 29 3b 0a 20 20   pParse!=0 );.  
250ae 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
250af 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30  .  assert( db!=0
250b0 20 29 3b 0a 2f 2a 20 20 69 66 28 20 64 62 2d 3e   );./*  if( db->
250b1 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20  aDb[0].pBt==0 ) 
250b2 72 65 74 75 72 6e 3b 20 2a 2f 0a 20 20 69 66 28  return; */.  if(
250b3 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
250b4 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
250b5 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 42  _TRANSACTION, "B
250b6 45 47 49 4e 22 2c 20 30 2c 20 30 29 20 29 7b 0a  EGIN", 0, 0) ){.
250b7 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
250b8 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
250b9 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
250ba 69 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b  if( !v ) return;
250bb 0a 20 20 69 66 28 20 74 79 70 65 21 3d 54 4b 5f  .  if( type!=TK_
250bc 44 45 46 45 52 52 45 44 20 29 7b 0a 20 20 20 20  DEFERRED ){.    
250bd 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
250be 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
250bf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
250c0 32 28 76 2c 20 4f 50 5f 54 72 61 6e 73 61 63 74  2(v, OP_Transact
250c1 69 6f 6e 2c 20 69 2c 20 28 74 79 70 65 3d 3d 54  ion, i, (type==T
250c2 4b 5f 45 58 43 4c 55 53 49 56 45 29 2b 31 29 3b  K_EXCLUSIVE)+1);
250c3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
250c4 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
250c5 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
250c6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
250c7 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69  (v, OP_AutoCommi
250c8 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  t, 0, 0);.}../*.
250c9 2a 2a 20 43 6f 6d 6d 69 74 20 61 20 74 72 61 6e  ** Commit a tran
250ca 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54  saction.*/.SQLIT
250cb 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
250cc 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e  qlite3CommitTran
250cd 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70  saction(Parse *p
250ce 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
250cf 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76  3 *db;.  Vdbe *v
250d0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
250d1 72 73 65 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d  rse!=0 );.  db =
250d2 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61   pParse->db;.  a
250d3 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a  ssert( db!=0 );.
250d4 2f 2a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b  /*  if( db->aDb[
250d5 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75  0].pBt==0 ) retu
250d6 72 6e 3b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  rn; */.  if( sql
250d7 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
250d8 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41  arse, SQLITE_TRA
250d9 4e 53 41 43 54 49 4f 4e 2c 20 22 43 4f 4d 4d 49  NSACTION, "COMMI
250da 54 22 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20  T", 0, 0) ){.   
250db 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76   return;.  }.  v
250dc 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
250dd 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
250de 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   v ){.    sqlite
250df 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
250e0 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c  P_AutoCommit, 1,
250e1 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   0);.  }.}../*.*
250e2 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 20 74 72 61  * Rollback a tra
250e3 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49  nsaction.*/.SQLI
250e4 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
250e5 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54  sqlite3RollbackT
250e6 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65  ransaction(Parse
250e7 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c   *pParse){.  sql
250e8 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65  ite3 *db;.  Vdbe
250e9 20 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   *v;..  assert( 
250ea 70 50 61 72 73 65 21 3d 30 20 29 3b 0a 20 20 64  pParse!=0 );.  d
250eb 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
250ec 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20    assert( db!=0 
250ed 29 3b 0a 2f 2a 20 20 69 66 28 20 64 62 2d 3e 61  );./*  if( db->a
250ee 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72  Db[0].pBt==0 ) r
250ef 65 74 75 72 6e 3b 20 2a 2f 0a 20 20 69 66 28 20  eturn; */.  if( 
250f0 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
250f1 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
250f2 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 52 4f  TRANSACTION, "RO
250f3 4c 4c 42 41 43 4b 22 2c 20 30 2c 20 30 29 20 29  LLBACK", 0, 0) )
250f4 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
250f5 7d 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  }.  v = sqlite3G
250f6 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
250f7 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73    if( v ){.    s
250f8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
250f9 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69  (v, OP_AutoCommi
250fa 74 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a  t, 1, 1);.  }.}.
250fb 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
250fc 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62  tion is called b
250fd 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68 65  y the parser whe
250fe 6e 20 69 74 20 70 61 72 73 65 73 20 61 20 63 6f  n it parses a co
250ff 6d 6d 61 6e 64 20 74 6f 20 63 72 65 61 74 65 2c  mmand to create,
25100 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 72 20 72  .** release or r
25101 6f 6c 6c 62 61 63 6b 20 61 6e 20 53 51 4c 20 73  ollback an SQL s
25102 61 76 65 70 6f 69 6e 74 2e 20 0a 2a 2f 0a 53 51  avepoint. .*/.SQ
25103 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
25104 64 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69  d sqlite3Savepoi
25105 6e 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  nt(Parse *pParse
25106 2c 20 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20  , int op, Token 
25107 2a 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20  *pName){.  char 
25108 2a 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  *zName = sqlite3
25109 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50  NameFromToken(pP
2510a 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 29  arse->db, pName)
2510b 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b  ;.  if( zName ){
2510c 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73  .    Vdbe *v = s
2510d 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2510e 61 72 73 65 29 3b 0a 23 69 66 6e 64 65 66 20 53  arse);.#ifndef S
2510f 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
25110 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 73 74 61  RIZATION.    sta
25111 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
25112 61 7a 5b 5d 20 3d 20 7b 20 22 42 45 47 49 4e 22  az[] = { "BEGIN"
25113 2c 20 22 52 45 4c 45 41 53 45 22 2c 20 22 52 4f  , "RELEASE", "RO
25114 4c 4c 42 41 43 4b 22 20 7d 3b 0a 20 20 20 20 61  LLBACK" };.    a
25115 73 73 65 72 74 28 20 21 53 41 56 45 50 4f 49 4e  ssert( !SAVEPOIN
25116 54 5f 42 45 47 49 4e 20 26 26 20 53 41 56 45 50  T_BEGIN && SAVEP
25117 4f 49 4e 54 5f 52 45 4c 45 41 53 45 3d 3d 31 20  OINT_RELEASE==1 
25118 26 26 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  && SAVEPOINT_ROL
25119 4c 42 41 43 4b 3d 3d 32 20 29 3b 0a 23 65 6e 64  LBACK==2 );.#end
2511a 69 66 0a 20 20 20 20 69 66 28 20 21 76 20 7c 7c  if.    if( !v ||
2511b 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2511c 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
2511d 5f 53 41 56 45 50 4f 49 4e 54 2c 20 61 7a 5b 6f  _SAVEPOINT, az[o
2511e 70 5d 2c 20 7a 4e 61 6d 65 2c 20 30 29 20 29 7b  p], zName, 0) ){
2511f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
25120 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  Free(pParse->db,
25121 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72   zName);.      r
25122 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
25123 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25124 70 34 28 76 2c 20 4f 50 5f 53 61 76 65 70 6f 69  p4(v, OP_Savepoi
25125 6e 74 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 7a 4e  nt, op, 0, 0, zN
25126 61 6d 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29  ame, P4_DYNAMIC)
25127 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  ;.  }.}../*.** M
25128 61 6b 65 20 73 75 72 65 20 74 68 65 20 54 45 4d  ake sure the TEM
25129 50 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  P database is op
2512a 65 6e 20 61 6e 64 20 61 76 61 69 6c 61 62 6c 65  en and available
2512b 20 66 6f 72 20 75 73 65 2e 20 20 52 65 74 75 72   for use.  Retur
2512c 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  n.** the number 
2512d 6f 66 20 65 72 72 6f 72 73 2e 20 20 4c 65 61 76  of errors.  Leav
2512e 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73  e any error mess
2512f 61 67 65 73 20 69 6e 20 74 68 65 20 70 50 61 72  ages in the pPar
25130 73 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  se structure..*/
25131 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
25132 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  int sqlite3OpenT
25133 65 6d 70 44 61 74 61 62 61 73 65 28 50 61 72 73  empDatabase(Pars
25134 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71  e *pParse){.  sq
25135 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
25136 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62  se->db;.  if( db
25137 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 3d 3d 30 20  ->aDb[1].pBt==0 
25138 26 26 20 21 70 50 61 72 73 65 2d 3e 65 78 70 6c  && !pParse->expl
25139 61 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ain ){.    int r
2513a 63 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  c;.    static co
2513b 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20  nst int flags = 
2513c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
2513d 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
2513e 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c   |.          SQL
2513f 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20  ITE_OPEN_CREATE 
25140 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  |.          SQLI
25141 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
25142 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51  E |.          SQ
25143 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
25144 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20  ONCLOSE |.      
25145 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
25146 54 45 4d 50 5f 44 42 3b 0a 0a 20 20 20 20 72 63  TEMP_DB;..    rc
25147 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46   = sqlite3BtreeF
25148 61 63 74 6f 72 79 28 64 62 2c 20 30 2c 20 30 2c  actory(db, 0, 0,
25149 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
2514a 43 41 43 48 45 5f 53 49 5a 45 2c 20 66 6c 61 67  CACHE_SIZE, flag
2514b 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
2514c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2514d 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 31 5d 2e      &db->aDb[1].
2514e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  pBt);.    if( rc
2514f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
25150 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
25151 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e  rMsg(pParse, "un
25152 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61 20 74  able to open a t
25153 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
25154 65 20 22 0a 20 20 20 20 20 20 20 20 22 66 69 6c  e ".        "fil
25155 65 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65  e for storing te
25156 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 22 29  mporary tables")
25157 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
25158 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 72  rc = rc;.      r
25159 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
2515a 20 20 20 61 73 73 65 72 74 28 20 28 64 62 2d 3e     assert( (db->
2515b 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49  flags & SQLITE_I
2515c 6e 54 72 61 6e 73 29 3d 3d 30 20 7c 7c 20 64 62  nTrans)==0 || db
2515d 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 3b 0a  ->autoCommit );.
2515e 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
2515f 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29  aDb[1].pSchema )
25160 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ;.    sqlite3Pag
25161 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 73 71  erJournalMode(sq
25162 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
25163 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 2c  db->aDb[1].pBt),
25164 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
25165 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d               db-
25166 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65  >dfltJournalMode
25167 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
25168 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  0;.}../*.** Gene
25169 72 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 74  rate VDBE code t
2516a 68 61 74 20 77 69 6c 6c 20 76 65 72 69 66 79 20  hat will verify 
2516b 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
2516c 65 20 61 6e 64 20 73 74 61 72 74 0a 2a 2a 20 61  e and start.** a
2516d 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f   read-transactio
2516e 6e 20 66 6f 72 20 61 6c 6c 20 6e 61 6d 65 64 20  n for all named 
2516f 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 0a  database files..
25170 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6d 70 6f  **.** It is impo
25171 72 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 73  rtant that all s
25172 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 62 65  chema cookies be
25173 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 61 6c   verified and al
25174 6c 0a 2a 2a 20 72 65 61 64 20 74 72 61 6e 73 61  l.** read transa
25175 63 74 69 6f 6e 73 20 62 65 20 73 74 61 72 74 65  ctions be starte
25176 64 20 62 65 66 6f 72 65 20 61 6e 79 74 68 69 6e  d before anythin
25177 67 20 65 6c 73 65 20 68 61 70 70 65 6e 73 20 69  g else happens i
25178 6e 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 70 72  n.** the VDBE pr
25179 6f 67 72 61 6d 2e 20 20 42 75 74 20 74 68 69 73  ogram.  But this
2517a 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20   routine can be 
2517b 63 61 6c 6c 65 64 20 61 66 74 65 72 20 6d 75 63  called after muc
2517c 68 20 6f 74 68 65 72 0a 2a 2a 20 63 6f 64 65 20  h other.** code 
2517d 68 61 73 20 62 65 65 6e 20 67 65 6e 65 72 61 74  has been generat
2517e 65 64 2e 20 20 53 6f 20 68 65 72 65 20 69 73 20  ed.  So here is 
2517f 77 68 61 74 20 77 65 20 64 6f 3a 0a 2a 2a 0a 2a  what we do:.**.*
25180 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65  * The first time
25181 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
25182 20 63 61 6c 6c 65 64 2c 20 77 65 20 63 6f 64 65   called, we code
25183 20 61 6e 20 4f 50 5f 47 6f 74 6f 20 74 68 61 74   an OP_Goto that
25184 0a 2a 2a 20 77 69 6c 6c 20 6a 75 6d 70 20 74 6f  .** will jump to
25185 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 61 74   a subroutine at
25186 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
25187 70 72 6f 67 72 61 6d 2e 20 20 54 68 65 6e 20 77  program.  Then w
25188 65 0a 2a 2a 20 72 65 63 6f 72 64 20 65 76 65 72  e.** record ever
25189 79 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  y database that 
2518a 6e 65 65 64 73 20 69 74 73 20 73 63 68 65 6d 61  needs its schema
2518b 20 76 65 72 69 66 69 65 64 20 69 6e 20 74 68 65   verified in the
2518c 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  .** pParse->cook
2518d 69 65 4d 61 73 6b 20 66 69 65 6c 64 2e 20 20 4c  ieMask field.  L
2518e 61 74 65 72 2c 20 61 66 74 65 72 20 61 6c 6c 20  ater, after all 
2518f 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 62  other code has b
25190 65 65 6e 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64  een.** generated
25191 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65  , the subroutine
25192 20 74 68 61 74 20 64 6f 65 73 20 74 68 65 20 63   that does the c
25193 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 61 74 69  ookie verificati
25194 6f 6e 73 20 61 6e 64 0a 2a 2a 20 73 74 61 72 74  ons and.** start
25195 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  s the transactio
25196 6e 73 20 77 69 6c 6c 20 62 65 20 63 6f 64 65 64  ns will be coded
25197 20 61 6e 64 20 74 68 65 20 4f 50 5f 47 6f 74 6f   and the OP_Goto
25198 20 50 32 20 76 61 6c 75 65 0a 2a 2a 20 77 69 6c   P2 value.** wil
25199 6c 20 62 65 20 6d 61 64 65 20 74 6f 20 70 6f 69  l be made to poi
2519a 6e 74 20 74 6f 20 74 68 61 74 20 73 75 62 72 6f  nt to that subro
2519b 75 74 69 6e 65 2e 20 20 54 68 65 20 67 65 6e 65  utine.  The gene
2519c 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  ration of the.**
2519d 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 61   cookie verifica
2519e 74 69 6f 6e 20 73 75 62 72 6f 75 74 69 6e 65 20  tion subroutine 
2519f 63 6f 64 65 20 68 61 70 70 65 6e 73 20 69 6e 20  code happens in 
251a0 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64  sqlite3FinishCod
251a1 69 6e 67 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ing()..**.** If 
251a2 69 44 62 3c 30 20 74 68 65 6e 20 63 6f 64 65 20  iDb<0 then code 
251a3 74 68 65 20 4f 50 5f 47 6f 74 6f 20 6f 6e 6c 79  the OP_Goto only
251a4 20 2d 20 64 6f 6e 27 74 20 73 65 74 20 66 6c 61   - don't set fla
251a5 67 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 0a  g to verify the.
251a6 2a 2a 20 73 63 68 65 6d 61 20 6f 6e 20 61 6e 79  ** schema on any
251a7 20 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 69   databases.  Thi
251a8 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  s can be used to
251a9 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 20 4f 50   position the OP
251aa 5f 47 6f 74 6f 0a 2a 2a 20 65 61 72 6c 79 20 69  _Goto.** early i
251ab 6e 20 74 68 65 20 63 6f 64 65 2c 20 62 65 66 6f  n the code, befo
251ac 72 65 20 77 65 20 6b 6e 6f 77 20 69 66 20 61 6e  re we know if an
251ad 79 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  y database table
251ae 73 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2e 0a  s will be used..
251af 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
251b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
251b1 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50  deVerifySchema(P
251b2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
251b3 74 20 69 44 62 29 7b 0a 20 20 50 61 72 73 65 20  t iDb){.  Parse 
251b4 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c  *pToplevel = sql
251b5 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
251b6 6c 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 69 66  l(pParse);..  if
251b7 28 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f  ( pToplevel->coo
251b8 6b 69 65 47 6f 74 6f 3d 3d 30 20 29 7b 0a 20 20  kieGoto==0 ){.  
251b9 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
251ba 74 65 33 47 65 74 56 64 62 65 28 70 54 6f 70 6c  te3GetVdbe(pTopl
251bb 65 76 65 6c 29 3b 0a 20 20 20 20 69 66 28 20 76  evel);.    if( v
251bc 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 20 20 2f  ==0 ) return;  /
251bd 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70  * This only happ
251be 65 6e 73 20 69 66 20 74 68 65 72 65 20 77 61 73  ens if there was
251bf 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72 20 2a   a prior error *
251c0 2f 0a 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d  /.    pToplevel-
251c1 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20 3d 20 73 71  >cookieGoto = sq
251c2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
251c3 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30  v, OP_Goto, 0, 0
251c4 29 2b 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  )+1;.  }.  if( i
251c5 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  Db>=0 ){.    sql
251c6 69 74 65 33 20 2a 64 62 20 3d 20 70 54 6f 70 6c  ite3 *db = pTopl
251c7 65 76 65 6c 2d 3e 64 62 3b 0a 20 20 20 20 69 6e  evel->db;.    in
251c8 74 20 6d 61 73 6b 3b 0a 0a 20 20 20 20 61 73 73  t mask;..    ass
251c9 65 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62  ert( iDb<db->nDb
251ca 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
251cb 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
251cc 21 3d 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 3b  !=0 || iDb==1 );
251cd 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62  .    assert( iDb
251ce 3c 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41  <SQLITE_MAX_ATTA
251cf 43 48 45 44 2b 32 20 29 3b 0a 20 20 20 20 6d 61  CHED+2 );.    ma
251d0 73 6b 20 3d 20 31 3c 3c 69 44 62 3b 0a 20 20 20  sk = 1<<iDb;.   
251d1 20 69 66 28 20 28 70 54 6f 70 6c 65 76 65 6c 2d   if( (pToplevel-
251d2 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 26 20 6d 61  >cookieMask & ma
251d3 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  sk)==0 ){.      
251d4 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69  pToplevel->cooki
251d5 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20  eMask |= mask;. 
251d6 20 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e       pToplevel->
251d7 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d  cookieValue[iDb]
251d8 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
251d9 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f  pSchema->schema_
251da 63 6f 6f 6b 69 65 3b 0a 20 20 20 20 20 20 69 66  cookie;.      if
251db 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
251dc 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20  & iDb==1 ){.    
251dd 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
251de 65 6d 70 44 61 74 61 62 61 73 65 28 70 54 6f 70  empDatabase(pTop
251df 6c 65 76 65 6c 29 3b 0a 20 20 20 20 20 20 7d 0a  level);.      }.
251e0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
251e1 2a 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 45  ** Generate VDBE
251e2 20 63 6f 64 65 20 74 68 61 74 20 70 72 65 70 61   code that prepa
251e3 72 65 73 20 66 6f 72 20 64 6f 69 6e 67 20 61 6e  res for doing an
251e4 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 0a   operation that.
251e5 2a 2a 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20  ** might change 
251e6 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  the database..**
251e7 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
251e8 20 73 74 61 72 74 73 20 61 20 6e 65 77 20 74 72   starts a new tr
251e9 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 77 65 20  ansaction if we 
251ea 61 72 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20  are not already 
251eb 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e  within.** a tran
251ec 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 77 65 20  saction.  If we 
251ed 61 72 65 20 61 6c 72 65 61 64 79 20 77 69 74 68  are already with
251ee 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
251ef 2c 20 74 68 65 6e 20 61 20 63 68 65 63 6b 70 6f  , then a checkpo
251f0 69 6e 74 0a 2a 2a 20 69 73 20 73 65 74 20 69 66  int.** is set if
251f1 20 74 68 65 20 73 65 74 53 74 61 74 65 6d 65 6e   the setStatemen
251f2 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  t parameter is t
251f3 72 75 65 2e 20 20 41 20 63 68 65 63 6b 70 6f 69  rue.  A checkpoi
251f4 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20  nt should.** be 
251f5 73 65 74 20 66 6f 72 20 6f 70 65 72 61 74 69 6f  set for operatio
251f6 6e 73 20 74 68 61 74 20 6d 69 67 68 74 20 66 61  ns that might fa
251f7 69 6c 20 28 64 75 65 20 74 6f 20 61 20 63 6f 6e  il (due to a con
251f8 73 74 72 61 69 6e 74 29 20 70 61 72 74 20 6f 66  straint) part of
251f9 0a 2a 2a 20 74 68 65 20 77 61 79 20 74 68 72 6f  .** the way thro
251fa 75 67 68 20 61 6e 64 20 77 68 69 63 68 20 77 69  ugh and which wi
251fb 6c 6c 20 6e 65 65 64 20 74 6f 20 75 6e 64 6f 20  ll need to undo 
251fc 73 6f 6d 65 20 77 72 69 74 65 73 20 77 69 74 68  some writes with
251fd 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 0a 2a 2a  out having to.**
251fe 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 77 68   rollback the wh
251ff 6f 6c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ole transaction.
25200 20 20 46 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73    For operations
25201 20 77 68 65 72 65 20 61 6c 6c 20 63 6f 6e 73 74   where all const
25202 72 61 69 6e 74 73 0a 2a 2a 20 63 61 6e 20 62 65  raints.** can be
25203 20 63 68 65 63 6b 65 64 20 62 65 66 6f 72 65 20   checked before 
25204 61 6e 79 20 63 68 61 6e 67 65 73 20 61 72 65 20  any changes are 
25205 6d 61 64 65 20 74 6f 20 74 68 65 20 64 61 74 61  made to the data
25206 62 61 73 65 2c 20 69 74 20 69 73 20 6e 65 76 65  base, it is neve
25207 72 0a 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 74  r.** necessary t
25208 6f 20 75 6e 64 6f 20 61 20 77 72 69 74 65 20 61  o undo a write a
25209 6e 64 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e  nd the checkpoin
2520a 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  t should not be 
2520b 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  set..*/.SQLITE_P
2520c 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2520d 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
2520e 72 61 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50  ration(Parse *pP
2520f 61 72 73 65 2c 20 69 6e 74 20 73 65 74 53 74 61  arse, int setSta
25210 74 65 6d 65 6e 74 2c 20 69 6e 74 20 69 44 62 29  tement, int iDb)
25211 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c  {.  Parse *pTopl
25212 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61  evel = sqlite3Pa
25213 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72  rseToplevel(pPar
25214 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 6f  se);.  sqlite3Co
25215 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70  deVerifySchema(p
25216 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 70  Parse, iDb);.  p
25217 54 6f 70 6c 65 76 65 6c 2d 3e 77 72 69 74 65 4d  Toplevel->writeM
25218 61 73 6b 20 7c 3d 20 31 3c 3c 69 44 62 3b 0a 20  ask |= 1<<iDb;. 
25219 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 69 73 4d 75   pToplevel->isMu
2521a 6c 74 69 57 72 69 74 65 20 7c 3d 20 73 65 74 53  ltiWrite |= setS
2521b 74 61 74 65 6d 65 6e 74 3b 0a 7d 0a 0a 2f 2a 0a  tatement;.}../*.
2521c 2a 2a 20 49 6e 64 69 63 61 74 65 20 74 68 61 74  ** Indicate that
2521d 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 63   the statement c
2521e 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63  urrently under c
2521f 6f 6e 73 74 72 75 63 74 69 6f 6e 20 6d 69 67 68  onstruction migh
25220 74 20 77 72 69 74 65 0a 2a 2a 20 6d 6f 72 65 20  t write.** more 
25221 74 68 61 6e 20 6f 6e 65 20 65 6e 74 72 79 20 28  than one entry (
25222 65 78 61 6d 70 6c 65 3a 20 64 65 6c 65 74 69 6e  example: deletin
25223 67 20 6f 6e 65 20 72 6f 77 20 74 68 65 6e 20 69  g one row then i
25224 6e 73 65 72 74 69 6e 67 20 61 6e 6f 74 68 65 72  nserting another
25225 2c 0a 2a 2a 20 69 6e 73 65 72 74 69 6e 67 20 6d  ,.** inserting m
25226 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 69 6e 20  ultiple rows in 
25227 61 20 74 61 62 6c 65 2c 20 6f 72 20 69 6e 73 65  a table, or inse
25228 72 74 69 6e 67 20 61 20 72 6f 77 20 61 6e 64 20  rting a row and 
25229 69 6e 64 65 78 20 65 6e 74 72 69 65 73 2e 29 0a  index entries.).
2522a 2a 2a 20 49 66 20 61 6e 20 61 62 6f 72 74 20 6f  ** If an abort o
2522b 63 63 75 72 73 20 61 66 74 65 72 20 73 6f 6d 65  ccurs after some
2522c 20 6f 66 20 74 68 65 73 65 20 77 72 69 74 65 73   of these writes
2522d 20 68 61 76 65 20 63 6f 6d 70 6c 65 74 65 64 2c   have completed,
2522e 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 0a 2a 2a   then it will.**
2522f 20 62 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f   be necessary to
25230 20 75 6e 64 6f 20 74 68 65 20 63 6f 6d 70 6c 65   undo the comple
25231 74 65 64 20 77 72 69 74 65 73 2e 0a 2a 2f 0a 53  ted writes..*/.S
25232 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
25233 69 64 20 73 71 6c 69 74 65 33 4d 75 6c 74 69 57  id sqlite3MultiW
25234 72 69 74 65 28 50 61 72 73 65 20 2a 70 50 61 72  rite(Parse *pPar
25235 73 65 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54  se){.  Parse *pT
25236 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65  oplevel = sqlite
25237 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70  3ParseToplevel(p
25238 50 61 72 73 65 29 3b 0a 20 20 70 54 6f 70 6c 65  Parse);.  pTople
25239 76 65 6c 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74  vel->isMultiWrit
2523a 65 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  e = 1;.}../* .**
2523b 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   The code genera
2523c 74 6f 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  tor calls this r
2523d 6f 75 74 69 6e 65 20 69 66 20 69 73 20 64 69 73  outine if is dis
2523e 63 6f 76 65 72 73 20 74 68 61 74 20 69 74 20 69  covers that it i
2523f 73 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 6f  s.** possible to
25240 20 61 62 6f 72 74 20 61 20 73 74 61 74 65 6d 65   abort a stateme
25241 6e 74 20 70 72 69 6f 72 20 74 6f 20 63 6f 6d 70  nt prior to comp
25242 6c 65 74 69 6f 6e 2e 20 20 49 6e 20 6f 72 64 65  letion.  In orde
25243 72 20 74 6f 20 0a 2a 2a 20 70 65 72 66 6f 72 6d  r to .** perform
25244 20 74 68 69 73 20 61 62 6f 72 74 20 77 69 74 68   this abort with
25245 6f 75 74 20 63 6f 72 72 75 70 74 69 6e 67 20 74  out corrupting t
25246 68 65 20 64 61 74 61 62 61 73 65 2c 20 77 65 20  he database, we 
25247 6e 65 65 64 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20  need to make.** 
25248 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 74  sure that the st
25249 61 74 65 6d 65 6e 74 20 69 73 20 70 72 6f 74 65  atement is prote
2524a 63 74 65 64 20 62 79 20 61 20 73 74 61 74 65 6d  cted by a statem
2524b 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ent transaction.
2524c 0a 2a 2a 0a 2a 2a 20 54 65 63 68 6e 69 63 61 6c  .**.** Technical
2524d 6c 79 2c 20 77 65 20 6f 6e 6c 79 20 6e 65 65 64  ly, we only need
2524e 20 74 6f 20 73 65 74 20 74 68 65 20 6d 61 79 41   to set the mayA
2524f 62 6f 72 74 20 66 6c 61 67 20 69 66 20 74 68 65  bort flag if the
25250 0a 2a 2a 20 69 73 4d 75 6c 74 69 57 72 69 74 65  .** isMultiWrite
25251 20 66 6c 61 67 20 77 61 73 20 70 72 65 76 69 6f   flag was previo
25252 75 73 6c 79 20 73 65 74 2e 20 20 54 68 65 72 65  usly set.  There
25253 20 69 73 20 61 20 74 69 6d 65 20 64 65 70 65 6e   is a time depen
25254 64 65 6e 63 79 0a 2a 2a 20 73 75 63 68 20 74 68  dency.** such th
25255 61 74 20 74 68 65 20 61 62 6f 72 74 20 6d 75 73  at the abort mus
25256 74 20 6f 63 63 75 72 20 61 66 74 65 72 20 74 68  t occur after th
25257 65 20 6d 75 6c 74 69 77 72 69 74 65 2e 20 20 54  e multiwrite.  T
25258 68 69 73 20 6d 61 6b 65 73 0a 2a 2a 20 73 6f 6d  his makes.** som
25259 65 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 76  e statements inv
2525a 6f 6c 76 69 6e 67 20 74 68 65 20 52 45 50 4c 41  olving the REPLA
2525b 43 45 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f  CE conflict reso
2525c 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d  lution algorithm
2525d 0a 2a 2a 20 67 6f 20 61 20 6c 69 74 74 6c 65 20  .** go a little 
2525e 66 61 73 74 65 72 2e 20 20 42 75 74 20 74 61 6b  faster.  But tak
2525f 69 6e 67 20 61 64 76 61 6e 74 61 67 65 20 6f 66  ing advantage of
25260 20 74 68 69 73 20 74 69 6d 65 20 64 65 70 65 6e   this time depen
25261 64 65 6e 63 79 0a 2a 2a 20 6d 61 6b 65 73 20 69  dency.** makes i
25262 74 20 6d 6f 72 65 20 64 69 66 66 69 63 75 6c 74  t more difficult
25263 20 74 6f 20 70 72 6f 76 65 20 74 68 61 74 20 74   to prove that t
25264 68 65 20 63 6f 64 65 20 69 73 20 63 6f 72 72 65  he code is corre
25265 63 74 20 28 69 6e 20 0a 2a 2a 20 70 61 72 74 69  ct (in .** parti
25266 63 75 6c 61 72 2c 20 69 74 20 70 72 65 76 65 6e  cular, it preven
25267 74 73 20 75 73 20 66 72 6f 6d 20 77 72 69 74 69  ts us from writi
25268 6e 67 20 61 6e 20 65 66 66 65 63 74 69 76 65 0a  ng an effective.
25269 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
2526a 6e 20 6f 66 20 73 71 6c 69 74 65 33 41 73 73 65  n of sqlite3Asse
2526b 72 74 4d 61 79 41 62 6f 72 74 28 29 29 20 61 6e  rtMayAbort()) an
2526c 64 20 73 6f 20 77 65 20 68 61 76 65 20 63 68 6f  d so we have cho
2526d 73 65 6e 0a 2a 2a 20 74 6f 20 74 61 6b 65 20 74  sen.** to take t
2526e 68 65 20 73 61 66 65 20 72 6f 75 74 65 20 61 6e  he safe route an
2526f 64 20 73 6b 69 70 20 74 68 65 20 6f 70 74 69 6d  d skip the optim
25270 69 7a 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ization..*/.SQLI
25271 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
25272 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28  sqlite3MayAbort(
25273 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
25274 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
25275 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  el = sqlite3Pars
25276 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
25277 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e  );.  pToplevel->
25278 6d 61 79 41 62 6f 72 74 20 3d 20 31 3b 0a 7d 0a  mayAbort = 1;.}.
25279 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f  ./*.** Code an O
2527a 50 5f 48 61 6c 74 20 74 68 61 74 20 63 61 75 73  P_Halt that caus
2527b 65 73 20 74 68 65 20 76 64 62 65 20 74 6f 20 72  es the vdbe to r
2527c 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f  eturn an SQLITE_
2527d 43 4f 4e 53 54 52 41 49 4e 54 0a 2a 2a 20 65 72  CONSTRAINT.** er
2527e 72 6f 72 2e 20 54 68 65 20 6f 6e 45 72 72 6f 72  ror. The onError
2527f 20 70 61 72 61 6d 65 74 65 72 20 64 65 74 65 72   parameter deter
25280 6d 69 6e 65 73 20 77 68 69 63 68 20 28 69 66 20  mines which (if 
25281 61 6e 79 29 20 6f 66 20 74 68 65 20 73 74 61 74  any) of the stat
25282 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64 2f 6f 72 20  ement.** and/or 
25283 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
25284 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
25285 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ck..*/.SQLITE_PR
25286 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
25287 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74  e3HaltConstraint
25288 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
25289 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 63 68 61  int onError, cha
2528a 72 20 2a 70 34 2c 20 69 6e 74 20 70 34 74 79 70  r *p4, int p4typ
2528b 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  e){.  Vdbe *v = 
2528c 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2528d 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 6f 6e  Parse);.  if( on
2528e 45 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20  Error==OE_Abort 
2528f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61  ){.    sqlite3Ma
25290 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a  yAbort(pParse);.
25291 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
25292 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61  eAddOp4(v, OP_Ha
25293 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  lt, SQLITE_CONST
25294 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20  RAINT, onError, 
25295 30 2c 20 70 34 2c 20 70 34 74 79 70 65 29 3b 0a  0, p4, p4type);.
25296 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  }../*.** Check t
25297 6f 20 73 65 65 20 69 66 20 70 49 6e 64 65 78 20  o see if pIndex 
25298 75 73 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69  uses the collati
25299 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c  ng sequence pCol
2529a 6c 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72  l.  Return.** tr
2529b 75 65 20 69 66 20 69 74 20 64 6f 65 73 20 61 6e  ue if it does an
2529c 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 64 6f  d false if it do
2529d 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64  es not..*/.#ifnd
2529e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52  ef SQLITE_OMIT_R
2529f 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20 69 6e  EINDEX.static in
252a0 74 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68  t collationMatch
252a1 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f  (const char *zCo
252a2 6c 6c 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  ll, Index *pInde
252a3 78 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61  x){.  int i;.  a
252a4 73 73 65 72 74 28 20 7a 43 6f 6c 6c 21 3d 30 20  ssert( zColl!=0 
252a5 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
252a6 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  pIndex->nColumn;
252a7 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74   i++){.    const
252a8 20 63 68 61 72 20 2a 7a 20 3d 20 70 49 6e 64 65   char *z = pInde
252a9 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20  x->azColl[i];.  
252aa 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 29    assert( z!=0 )
252ab 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c  ;.    if( 0==sql
252ac 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a  ite3StrICmp(z, z
252ad 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 72  Coll) ){.      r
252ae 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
252af 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
252b0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
252b1 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64  ecompute all ind
252b2 69 63 65 73 20 6f 66 20 70 54 61 62 20 74 68 61  ices of pTab tha
252b3 74 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74  t use the collat
252b4 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f  ing sequence pCo
252b5 6c 6c 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c 3d  ll..** If pColl=
252b6 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74  =0 then recomput
252b7 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66  e all indices of
252b8 20 70 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 65   pTab..*/.#ifnde
252b9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45  f SQLITE_OMIT_RE
252ba 49 4e 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69  INDEX.static voi
252bb 64 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28 50  d reindexTable(P
252bc 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
252bd 62 6c 65 20 2a 70 54 61 62 2c 20 63 68 61 72 20  ble *pTab, char 
252be 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20  const *zColl){. 
252bf 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20   Index *pIndex; 
252c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
252c1 41 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61  An index associa
252c2 74 65 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f  ted with pTab */
252c3 0a 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70  ..  for(pIndex=p
252c4 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e  Tab->pIndex; pIn
252c5 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64  dex; pIndex=pInd
252c6 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  ex->pNext){.    
252c7 69 66 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20  if( zColl==0 || 
252c8 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a  collationMatch(z
252c9 43 6f 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b  Coll, pIndex) ){
252ca 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d  .      int iDb =
252cb 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
252cc 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
252cd 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
252ce 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
252cf 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
252d0 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
252d1 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
252d2 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61  3RefillIndex(pPa
252d3 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29  rse, pIndex, -1)
252d4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65  ;.    }.  }.}.#e
252d5 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f  ndif../*.** Reco
252d6 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65  mpute all indice
252d7 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20  s of all tables 
252d8 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  in all databases
252d9 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 69 6e   where the.** in
252da 64 69 63 65 73 20 75 73 65 20 74 68 65 20 63 6f  dices use the co
252db 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
252dc 20 70 43 6f 6c 6c 2e 20 20 49 66 20 70 43 6f 6c   pColl.  If pCol
252dd 6c 3d 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70  l==0 then recomp
252de 75 74 65 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69 63  ute.** all indic
252df 65 73 20 65 76 65 72 79 77 68 65 72 65 2e 0a 2a  es everywhere..*
252e0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
252e1 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74  _OMIT_REINDEX.st
252e2 61 74 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65  atic void reinde
252e3 78 44 61 74 61 62 61 73 65 73 28 50 61 72 73 65  xDatabases(Parse
252e4 20 2a 70 50 61 72 73 65 2c 20 63 68 61 72 20 63   *pParse, char c
252e5 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20  onst *zColl){.  
252e6 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20  Db *pDb;        
252e7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
252e8 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
252e9 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
252ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252eb 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
252ec 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a  e index number *
252ed 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
252ee 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
252ef 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
252f0 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
252f1 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20 20  HashElem *k;    
252f2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
252f3 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
252f4 74 61 62 6c 65 73 20 69 6e 20 70 44 62 20 2a 2f  tables in pDb */
252f5 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20  .  Table *pTab; 
252f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
252f7 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65  * A table in the
252f8 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 0a 20 20   database */..  
252f9 66 6f 72 28 69 44 62 3d 30 2c 20 70 44 62 3d 64  for(iDb=0, pDb=d
252fa 62 2d 3e 61 44 62 3b 20 69 44 62 3c 64 62 2d 3e  b->aDb; iDb<db->
252fb 6e 44 62 3b 20 69 44 62 2b 2b 2c 20 70 44 62 2b  nDb; iDb++, pDb+
252fc 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  +){.    assert( 
252fd 70 44 62 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f  pDb!=0 );.    fo
252fe 72 28 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(k=sqliteHashFi
252ff 72 73 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d  rst(&pDb->pSchem
25300 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 20 6b 3b  a->tblHash);  k;
25301 20 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78   k=sqliteHashNex
25302 74 28 6b 29 29 7b 0a 20 20 20 20 20 20 70 54 61  t(k)){.      pTa
25303 62 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69  b = (Table*)sqli
25304 74 65 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20  teHashData(k);. 
25305 20 20 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c       reindexTabl
25306 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
25307 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20  zColl);.    }.  
25308 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  }.}.#endif../*.*
25309 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2530a 66 6f 72 20 74 68 65 20 52 45 49 4e 44 45 58 20  for the REINDEX 
2530b 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20  command..**.**  
2530c 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 20        REINDEX   
2530d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2530e 20 20 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a 2a           -- 1.**
2530f 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20          REINDEX 
25310 20 3c 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20 20   <collation>    
25311 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0a             -- 2.
25312 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45  **        REINDE
25313 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f  X  ?<database>.?
25314 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d 20  <tablename>  -- 
25315 33 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e  3.**        REIN
25316 44 45 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e  DEX  ?<database>
25317 2e 3f 3c 69 6e 64 65 78 6e 61 6d 65 3e 20 20 2d  .?<indexname>  -
25318 2d 20 34 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31  - 4.**.** Form 1
25319 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69   causes all indi
2531a 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63  ces in all attac
2531b 68 65 64 20 64 61 74 61 62 61 73 65 73 20 74 6f  hed databases to
2531c 20 62 65 20 72 65 62 75 69 6c 74 2e 0a 2a 2a 20   be rebuilt..** 
2531d 46 6f 72 6d 20 32 20 72 65 62 75 69 6c 64 73 20  Form 2 rebuilds 
2531e 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61  all indices in a
2531f 6c 6c 20 64 61 74 61 62 61 73 65 73 20 74 68 61  ll databases tha
25320 74 20 75 73 65 20 74 68 65 20 6e 61 6d 65 64 0a  t use the named.
25321 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  ** collating fun
25322 63 74 69 6f 6e 2e 20 20 46 6f 72 6d 73 20 33 20  ction.  Forms 3 
25323 61 6e 64 20 34 20 72 65 62 75 69 6c 64 20 74 68  and 4 rebuild th
25324 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 6f 72  e named index or
25325 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20   all.** indices 
25326 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
25327 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e  the named table.
25328 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
25329 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a  TE_OMIT_REINDEX.
2532a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2532b 6f 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64  oid sqlite3Reind
2532c 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ex(Parse *pParse
2532d 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c  , Token *pName1,
2532e 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b   Token *pName2){
2532f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
25330 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
25331 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * Collating sequ
25332 65 6e 63 65 20 74 6f 20 62 65 20 72 65 69 6e 64  ence to be reind
25333 65 78 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f  exed, or NULL */
25334 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20  .  char *z;     
25335 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25336 2a 20 4e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c  * Name of a tabl
25337 65 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20  e or index */.  
25338 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
25339 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2533a 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
2533b 61 73 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ase */.  Table *
2533c 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
2533d 20 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20       /* A table 
2533e 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
2533f 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  */.  Index *pInd
25340 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ex;             
25341 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73   /* An index ass
25342 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 54 61  ociated with pTa
25343 62 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  b */.  int iDb; 
25344 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25345 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
25346 73 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20  se index number 
25347 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
25348 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
25349 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2534a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
2534b 20 54 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61 6d 65   Token *pObjName
2534c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2534d 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  Name of the tabl
2534e 65 20 6f 72 20 69 6e 64 65 78 20 74 6f 20 62 65  e or index to be
2534f 20 72 65 69 6e 64 65 78 65 64 20 2a 2f 0a 0a 20   reindexed */.. 
25350 20 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61 74   /* Read the dat
25351 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66  abase schema. If
25352 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
25353 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72  , leave an error
25354 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e   message.  ** an
25355 64 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65  d code in pParse
25356 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c   and return NULL
25357 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54  . */.  if( SQLIT
25358 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61  E_OK!=sqlite3Rea
25359 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
2535a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
2535b 20 7d 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 31   }..  if( pName1
2535c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 69 6e 64  ==0 ){.    reind
2535d 65 78 44 61 74 61 62 61 73 65 73 28 70 50 61 72  exDatabases(pPar
2535e 73 65 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75  se, 0);.    retu
2535f 72 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  rn;.  }else if( 
25360 4e 45 56 45 52 28 70 4e 61 6d 65 32 3d 3d 30 29  NEVER(pName2==0)
25361 20 7c 7c 20 70 4e 61 6d 65 32 2d 3e 7a 3d 3d 30   || pName2->z==0
25362 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43   ){.    char *zC
25363 6f 6c 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 28  oll;.    assert(
25364 20 70 4e 61 6d 65 31 2d 3e 7a 20 29 3b 0a 20 20   pName1->z );.  
25365 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    zColl = sqlite
25366 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70  3NameFromToken(p
25367 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65  Parse->db, pName
25368 31 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 43 6f  1);.    if( !zCo
25369 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  ll ) return;.   
2536a 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
2536b 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
2536c 45 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20  ENC(db), zColl, 
2536d 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c  0);.    if( pCol
2536e 6c 20 29 7b 0a 20 20 20 20 20 20 72 65 69 6e 64  l ){.      reind
2536f 65 78 44 61 74 61 62 61 73 65 73 28 70 50 61 72  exDatabases(pPar
25370 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20  se, zColl);.    
25371 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
25372 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20  db, zColl);.    
25373 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
25374 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
25375 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20  e(db, zColl);.  
25376 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  }.  iDb = sqlite
25377 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61  3TwoPartName(pPa
25378 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61  rse, pName1, pNa
25379 6d 65 32 2c 20 26 70 4f 62 6a 4e 61 6d 65 29 3b  me2, &pObjName);
2537a 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 72  .  if( iDb<0 ) r
2537b 65 74 75 72 6e 3b 0a 20 20 7a 20 3d 20 73 71 6c  eturn;.  z = sql
2537c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
2537d 6e 28 64 62 2c 20 70 4f 62 6a 4e 61 6d 65 29 3b  n(db, pObjName);
2537e 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65  .  if( z==0 ) re
2537f 74 75 72 6e 3b 0a 20 20 7a 44 62 20 3d 20 64 62  turn;.  zDb = db
25380 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
25381 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ;.  pTab = sqlit
25382 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
25383 7a 2c 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 70  z, zDb);.  if( p
25384 54 61 62 20 29 7b 0a 20 20 20 20 72 65 69 6e 64  Tab ){.    reind
25385 65 78 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  exTable(pParse, 
25386 70 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 73 71  pTab, 0);.    sq
25387 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
25388 7a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  z);.    return;.
25389 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73    }.  pIndex = s
2538a 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
2538b 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 73  db, z, zDb);.  s
2538c 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2538d 20 7a 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65   z);.  if( pInde
2538e 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  x ){.    sqlite3
2538f 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
25390 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
25391 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
25392 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72  RefillIndex(pPar
25393 73 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b  se, pIndex, -1);
25394 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
25395 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d  .  sqlite3ErrorM
25396 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 61 62  sg(pParse, "unab
25397 6c 65 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74  le to identify t
25398 68 65 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20  he object to be 
25399 72 65 69 6e 64 65 78 65 64 22 29 3b 0a 7d 0a 23  reindexed");.}.#
2539a 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
2539b 75 72 6e 20 61 20 64 79 6e 61 6d 69 63 6c 79 20  urn a dynamicly 
2539c 61 6c 6c 6f 63 61 74 65 64 20 4b 65 79 49 6e 66  allocated KeyInf
2539d 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  o structure that
2539e 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20   can be used.** 
2539f 77 69 74 68 20 4f 50 5f 4f 70 65 6e 52 65 61 64  with OP_OpenRead
253a0 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65   or OP_OpenWrite
253a1 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 62   to access datab
253a2 61 73 65 20 69 6e 64 65 78 20 70 49 64 78 2e 0a  ase index pIdx..
253a3 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73  **.** If success
253a4 66 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74  ful, a pointer t
253a5 6f 20 74 68 65 20 6e 65 77 20 73 74 72 75 63 74  o the new struct
253a6 75 72 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ure is returned.
253a7 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a 2a 2a   In this case.**
253a8 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72   the caller is r
253a9 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63  esponsible for c
253aa 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 44 62  alling sqlite3Db
253ab 46 72 65 65 28 64 62 2c 20 29 20 6f 6e 20 74 68  Free(db, ) on th
253ac 65 20 72 65 74 75 72 6e 65 64 20 0a 2a 2a 20 70  e returned .** p
253ad 6f 69 6e 74 65 72 2e 20 49 66 20 61 6e 20 65 72  ointer. If an er
253ae 72 6f 72 20 6f 63 63 75 72 73 20 28 6f 75 74 20  ror occurs (out 
253af 6f 66 20 6d 65 6d 6f 72 79 20 6f 72 20 6d 69 73  of memory or mis
253b0 73 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a  sing collation .
253b1 2a 2a 20 73 65 71 75 65 6e 63 65 29 2c 20 4e 55  ** sequence), NU
253b2 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  LL is returned a
253b3 6e 64 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  nd the state of 
253b4 70 50 61 72 73 65 20 75 70 64 61 74 65 64 20 74  pParse updated t
253b5 6f 20 72 65 66 6c 65 63 74 0a 2a 2a 20 74 68 65  o reflect.** the
253b6 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54   error..*/.SQLIT
253b7 45 5f 50 52 49 56 41 54 45 20 4b 65 79 49 6e 66  E_PRIVATE KeyInf
253b8 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 4b  o *sqlite3IndexK
253b9 65 79 69 6e 66 6f 28 50 61 72 73 65 20 2a 70 50  eyinfo(Parse *pP
253ba 61 72 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 64  arse, Index *pId
253bb 78 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  x){.  int i;.  i
253bc 6e 74 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e  nt nCol = pIdx->
253bd 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20 6e  nColumn;.  int n
253be 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 4b  Bytes = sizeof(K
253bf 65 79 49 6e 66 6f 29 20 2b 20 28 6e 43 6f 6c 2d  eyInfo) + (nCol-
253c0 31 29 2a 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65  1)*sizeof(CollSe
253c1 71 2a 29 20 2b 20 6e 43 6f 6c 3b 0a 20 20 73 71  q*) + nCol;.  sq
253c2 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
253c3 73 65 2d 3e 64 62 3b 0a 20 20 4b 65 79 49 6e 66  se->db;.  KeyInf
253c4 6f 20 2a 70 4b 65 79 20 3d 20 28 4b 65 79 49 6e  o *pKey = (KeyIn
253c5 66 6f 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  fo *)sqlite3DbMa
253c6 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79  llocZero(db, nBy
253c7 74 65 73 29 3b 0a 0a 20 20 69 66 28 20 70 4b 65  tes);..  if( pKe
253c8 79 20 29 7b 0a 20 20 20 20 70 4b 65 79 2d 3e 64  y ){.    pKey->d
253c9 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
253ca 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f      pKey->aSortO
253cb 72 64 65 72 20 3d 20 28 75 38 20 2a 29 26 28 70  rder = (u8 *)&(p
253cc 4b 65 79 2d 3e 61 43 6f 6c 6c 5b 6e 43 6f 6c 5d  Key->aColl[nCol]
253cd 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 26  );.    assert( &
253ce 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pKey->aSortOrder
253cf 5b 6e 43 6f 6c 5d 3d 3d 26 28 28 28 75 38 20 2a  [nCol]==&(((u8 *
253d0 29 70 4b 65 79 29 5b 6e 42 79 74 65 73 5d 29 20  )pKey)[nBytes]) 
253d1 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
253d2 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
253d3 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20      char *zColl 
253d4 3d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69  = pIdx->azColl[i
253d5 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ];.      assert(
253d6 20 7a 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20   zColl );.      
253d7 70 4b 65 79 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d  pKey->aColl[i] =
253d8 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f   sqlite3LocateCo
253d9 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43  llSeq(pParse, zC
253da 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 70 4b 65 79  oll);.      pKey
253db 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20  ->aSortOrder[i] 
253dc 3d 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64  = pIdx->aSortOrd
253dd 65 72 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  er[i];.    }.   
253de 20 70 4b 65 79 2d 3e 6e 46 69 65 6c 64 20 3d 20   pKey->nField = 
253df 28 75 31 36 29 6e 43 6f 6c 3b 0a 20 20 7d 0a 0a  (u16)nCol;.  }..
253e0 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
253e1 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  rr ){.    sqlite
253e2 33 44 62 46 72 65 65 28 64 62 2c 20 70 4b 65 79  3DbFree(db, pKey
253e3 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 30 3b  );.    pKey = 0;
253e4 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4b  .  }.  return pK
253e5 65 79 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ey;.}../********
253e6 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 75  ****** End of bu
253e7 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ild.c **********
253e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253ea 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
253eb 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
253ec 65 20 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a  e callback.c ***
253ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253ef 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
253f0 35 20 4d 61 79 20 32 33 20 0a 2a 2a 0a 2a 2a 20  5 May 23 .**.** 
253f1 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
253f2 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
253f3 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
253f4 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
253f5 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
253f6 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
253f7 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
253f8 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
253f9 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
253fa 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
253fb 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
253fc 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
253fd 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
253fe 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
253ff 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
25400 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
25401 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
25402 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25403 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25404 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25405 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25406 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
25407 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
25408 73 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64  s functions used
25409 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 69   to access the i
2540a 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62  nternal hash tab
2540b 6c 65 73 0a 2a 2a 20 6f 66 20 75 73 65 72 20 64  les.** of user d
2540c 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  efined functions
2540d 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   and collation s
2540e 65 71 75 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20  equences..**.** 
2540f 24 49 64 3a 20 63 61 6c 6c 62 61 63 6b 2e 63 2c  $Id: callback.c,
25410 76 20 31 2e 34 32 20 32 30 30 39 2f 30 36 2f 31  v 1.42 2009/06/1
25411 37 20 30 30 3a 33 35 3a 33 31 20 64 72 68 20 45  7 00:35:31 drh E
25412 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  xp $.*/.../*.** 
25413 49 6e 76 6f 6b 65 20 74 68 65 20 27 63 6f 6c 6c  Invoke the 'coll
25414 61 74 69 6f 6e 20 6e 65 65 64 65 64 27 20 63 61  ation needed' ca
25415 6c 6c 62 61 63 6b 20 74 6f 20 72 65 71 75 65 73  llback to reques
25416 74 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  t a collation se
25417 71 75 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65  quence.** in the
25418 20 65 6e 63 6f 64 69 6e 67 20 65 6e 63 20 6f 66   encoding enc of
25419 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e   name zName, len
2541a 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2f 0a 73 74  gth nName..*/.st
2541b 61 74 69 63 20 76 6f 69 64 20 63 61 6c 6c 43 6f  atic void callCo
2541c 6c 6c 4e 65 65 64 65 64 28 73 71 6c 69 74 65 33  llNeeded(sqlite3
2541d 20 2a 64 62 2c 20 69 6e 74 20 65 6e 63 2c 20 63   *db, int enc, c
2541e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2541f 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  ){.  assert( !db
25420 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 7c 7c  ->xCollNeeded ||
25421 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65   !db->xCollNeede
25422 64 31 36 20 29 3b 0a 20 20 69 66 28 20 64 62 2d  d16 );.  if( db-
25423 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 29 7b 0a  >xCollNeeded ){.
25424 20 20 20 20 63 68 61 72 20 2a 7a 45 78 74 65 72      char *zExter
25425 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53  nal = sqlite3DbS
25426 74 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29  trDup(db, zName)
25427 3b 0a 20 20 20 20 69 66 28 20 21 7a 45 78 74 65  ;.    if( !zExte
25428 72 6e 61 6c 20 29 20 72 65 74 75 72 6e 3b 0a 20  rnal ) return;. 
25429 20 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64     db->xCollNeed
2542a 65 64 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64  ed(db->pCollNeed
2542b 65 64 41 72 67 2c 20 64 62 2c 20 65 6e 63 2c 20  edArg, db, enc, 
2542c 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20  zExternal);.    
2542d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2542e 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20  , zExternal);.  
2542f 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
25430 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 69 66  _OMIT_UTF16.  if
25431 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65  ( db->xCollNeede
25432 64 31 36 20 29 7b 0a 20 20 20 20 63 68 61 72 20  d16 ){.    char 
25433 63 6f 6e 73 74 20 2a 7a 45 78 74 65 72 6e 61 6c  const *zExternal
25434 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  ;.    sqlite3_va
25435 6c 75 65 20 2a 70 54 6d 70 20 3d 20 73 71 6c 69  lue *pTmp = sqli
25436 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 3b  te3ValueNew(db);
25437 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
25438 65 53 65 74 53 74 72 28 70 54 6d 70 2c 20 2d 31  eSetStr(pTmp, -1
25439 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f  , zName, SQLITE_
2543a 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
2543b 54 49 43 29 3b 0a 20 20 20 20 7a 45 78 74 65 72  TIC);.    zExter
2543c 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  nal = sqlite3Val
2543d 75 65 54 65 78 74 28 70 54 6d 70 2c 20 53 51 4c  ueText(pTmp, SQL
2543e 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29  ITE_UTF16NATIVE)
2543f 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 74 65 72  ;.    if( zExter
25440 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  nal ){.      db-
25441 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 28 64  >xCollNeeded16(d
25442 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  b->pCollNeededAr
25443 67 2c 20 64 62 2c 20 28 69 6e 74 29 45 4e 43 28  g, db, (int)ENC(
25444 64 62 29 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b  db), zExternal);
25445 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
25446 65 33 56 61 6c 75 65 46 72 65 65 28 70 54 6d 70  e3ValueFree(pTmp
25447 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  );.  }.#endif.}.
25448 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
25449 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 66  ine is called if
2544a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
2544b 61 63 74 6f 72 79 20 66 61 69 6c 73 20 74 6f 20  actory fails to 
2544c 64 65 6c 69 76 65 72 20 61 0a 2a 2a 20 63 6f 6c  deliver a.** col
2544d 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  lation function 
2544e 69 6e 20 74 68 65 20 62 65 73 74 20 65 6e 63 6f  in the best enco
2544f 64 69 6e 67 20 62 75 74 20 74 68 65 72 65 20 6d  ding but there m
25450 61 79 20 62 65 20 6f 74 68 65 72 20 76 65 72 73  ay be other vers
25451 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ions.** of this 
25452 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69  collation functi
25453 6f 6e 20 28 66 6f 72 20 6f 74 68 65 72 20 74 65  on (for other te
25454 78 74 20 65 6e 63 6f 64 69 6e 67 73 29 20 61 76  xt encodings) av
25455 61 69 6c 61 62 6c 65 2e 20 55 73 65 20 6f 6e 65  ailable. Use one
25456 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 69 6e 73  .** of these ins
25457 74 65 61 64 20 69 66 20 74 68 65 79 20 65 78 69  tead if they exi
25458 73 74 2e 20 41 76 6f 69 64 20 61 20 55 54 46 2d  st. Avoid a UTF-
25459 38 20 3c 2d 3e 20 55 54 46 2d 31 36 20 63 6f 6e  8 <-> UTF-16 con
2545a 76 65 72 73 69 6f 6e 20 69 66 0a 2a 2a 20 70 6f  version if.** po
2545b 73 73 69 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  ssible..*/.stati
2545c 63 20 69 6e 74 20 73 79 6e 74 68 43 6f 6c 6c 53  c int synthCollS
2545d 65 71 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  eq(sqlite3 *db, 
2545e 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b  CollSeq *pColl){
2545f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
25460 6c 32 3b 0a 20 20 63 68 61 72 20 2a 7a 20 3d 20  l2;.  char *z = 
25461 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pColl->zName;.  
25462 69 6e 74 20 69 3b 0a 20 20 73 74 61 74 69 63 20  int i;.  static 
25463 63 6f 6e 73 74 20 75 38 20 61 45 6e 63 5b 5d 20  const u8 aEnc[] 
25464 3d 20 7b 20 53 51 4c 49 54 45 5f 55 54 46 31 36  = { SQLITE_UTF16
25465 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  BE, SQLITE_UTF16
25466 4c 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 20  LE, SQLITE_UTF8 
25467 7d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  };.  for(i=0; i<
25468 33 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 6f  3; i++){.    pCo
25469 6c 6c 32 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  ll2 = sqlite3Fin
2546a 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 61 45 6e  dCollSeq(db, aEn
2546b 63 5b 69 5d 2c 20 7a 2c 20 30 29 3b 0a 20 20 20  c[i], z, 0);.   
2546c 20 69 66 28 20 70 43 6f 6c 6c 32 2d 3e 78 43 6d   if( pColl2->xCm
2546d 70 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65  p!=0 ){.      me
2546e 6d 63 70 79 28 70 43 6f 6c 6c 2c 20 70 43 6f 6c  mcpy(pColl, pCol
2546f 6c 32 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  l2, sizeof(CollS
25470 65 71 29 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c  eq));.      pCol
25471 6c 2d 3e 78 44 65 6c 20 3d 20 30 3b 20 20 20 20  l->xDel = 0;    
25472 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63       /* Do not c
25473 6f 70 79 20 74 68 65 20 64 65 73 74 72 75 63 74  opy the destruct
25474 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  or */.      retu
25475 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
25476 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
25477 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d   SQLITE_ERROR;.}
25478 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
25479 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73  ction is respons
2547a 69 62 6c 65 20 66 6f 72 20 69 6e 76 6f 6b 69 6e  ible for invokin
2547b 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  g the collation 
2547c 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b  factory callback
2547d 0a 2a 2a 20 6f 72 20 73 75 62 73 74 69 74 75 74  .** or substitut
2547e 69 6e 67 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20  ing a collation 
2547f 73 65 71 75 65 6e 63 65 20 6f 66 20 61 20 64 69  sequence of a di
25480 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67  fferent encoding
25481 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 72 65 71   when the.** req
25482 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  uested collation
25483 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74   sequence is not
25484 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
25485 65 20 64 65 73 69 72 65 64 20 65 6e 63 6f 64 69  e desired encodi
25486 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 69 74  ng..** .** If it
25487 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
25488 65 6e 20 70 43 6f 6c 6c 20 6d 75 73 74 20 70 6f  en pColl must po
25489 69 6e 74 20 74 6f 20 74 68 65 20 64 61 74 61 62  int to the datab
2548a 61 73 65 20 6e 61 74 69 76 65 20 65 6e 63 6f 64  ase native encod
2548b 69 6e 67 20 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f  ing .** collatio
2548c 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68 20  n sequence with 
2548d 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67  name zName, leng
2548e 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  th nName..**.** 
2548f 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
25490 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 63   is either the c
25491 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
25492 65 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20  e to be used in 
25493 64 61 74 61 62 61 73 65 0a 2a 2a 20 64 62 20 66  database.** db f
25494 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70  or collation typ
25495 65 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65  e name zName, le
25496 6e 67 74 68 20 6e 4e 61 6d 65 2c 20 6f 72 20 4e  ngth nName, or N
25497 55 4c 4c 2c 20 69 66 20 6e 6f 20 63 6f 6c 6c 61  ULL, if no colla
25498 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65  tion.** sequence
25499 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2e 0a 2a   can be found..*
2549a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73  *.** See also: s
2549b 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c  qlite3LocateColl
2549c 53 65 71 28 29 2c 20 73 71 6c 69 74 65 33 46 69  Seq(), sqlite3Fi
2549d 6e 64 43 6f 6c 6c 53 65 71 28 29 0a 2a 2f 0a 53  ndCollSeq().*/.S
2549e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f  QLITE_PRIVATE Co
2549f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65  llSeq *sqlite3Ge
254a0 74 43 6f 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69  tCollSeq(.  sqli
254a1 74 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20  te3* db,        
254a2 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
254a3 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
254a4 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20    u8 enc,       
254a5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
254a6 65 73 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 20  esired encoding 
254a7 66 6f 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  for the collatin
254a8 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20  g sequence */.  
254a9 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 2c 20  CollSeq *pColl, 
254aa 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69        /* Collati
254ab 6e 67 20 73 65 71 75 65 6e 63 65 20 77 69 74 68  ng sequence with
254ac 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67   native encoding
254ad 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63  , or NULL */.  c
254ae 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
254af 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e       /* Collatin
254b0 67 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20  g sequence name 
254b1 2a 2f 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  */.){.  CollSeq 
254b2 2a 70 3b 0a 0a 20 20 70 20 3d 20 70 43 6f 6c 6c  *p;..  p = pColl
254b3 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20  ;.  if( !p ){.  
254b4 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e    p = sqlite3Fin
254b5 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63  dCollSeq(db, enc
254b6 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d  , zName, 0);.  }
254b7 0a 20 20 69 66 28 20 21 70 20 7c 7c 20 21 70 2d  .  if( !p || !p-
254b8 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 2f 2a 20  >xCmp ){.    /* 
254b9 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  No collation seq
254ba 75 65 6e 63 65 20 6f 66 20 74 68 69 73 20 74 79  uence of this ty
254bb 70 65 20 66 6f 72 20 74 68 69 73 20 65 6e 63 6f  pe for this enco
254bc 64 69 6e 67 20 69 73 20 72 65 67 69 73 74 65 72  ding is register
254bd 65 64 2e 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 20  ed..    ** Call 
254be 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61  the collation fa
254bf 63 74 6f 72 79 20 74 6f 20 73 65 65 20 69 66 20  ctory to see if 
254c0 69 74 20 63 61 6e 20 73 75 70 70 6c 79 20 75 73  it can supply us
254c1 20 77 69 74 68 20 6f 6e 65 2e 0a 20 20 20 20 2a   with one..    *
254c2 2f 0a 20 20 20 20 63 61 6c 6c 43 6f 6c 6c 4e 65  /.    callCollNe
254c3 65 64 65 64 28 64 62 2c 20 65 6e 63 2c 20 7a 4e  eded(db, enc, zN
254c4 61 6d 65 29 3b 0a 20 20 20 20 70 20 3d 20 73 71  ame);.    p = sq
254c5 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
254c6 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c  (db, enc, zName,
254c7 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70   0);.  }.  if( p
254c8 20 26 26 20 21 70 2d 3e 78 43 6d 70 20 26 26 20   && !p->xCmp && 
254c9 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 64 62 2c  synthCollSeq(db,
254ca 20 70 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 30   p) ){.    p = 0
254cb 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
254cc 21 70 20 7c 7c 20 70 2d 3e 78 43 6d 70 20 29 3b  !p || p->xCmp );
254cd 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
254ce 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
254cf 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20  ne is called on 
254d0 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
254d1 65 6e 63 65 20 62 65 66 6f 72 65 20 69 74 20 69  ence before it i
254d2 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 68 65  s used to.** che
254d3 63 6b 20 74 68 61 74 20 69 74 20 69 73 20 64 65  ck that it is de
254d4 66 69 6e 65 64 2e 20 41 6e 20 75 6e 64 65 66 69  fined. An undefi
254d5 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ned collation se
254d6 71 75 65 6e 63 65 20 65 78 69 73 74 73 20 77 68  quence exists wh
254d7 65 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65  en.** a database
254d8 20 69 73 20 6c 6f 61 64 65 64 20 74 68 61 74 20   is loaded that 
254d9 63 6f 6e 74 61 69 6e 73 20 72 65 66 65 72 65 6e  contains referen
254da 63 65 73 20 74 6f 20 63 6f 6c 6c 61 74 69 6f 6e  ces to collation
254db 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 74 68   sequences.** th
254dc 61 74 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e  at have not been
254dd 20 64 65 66 69 6e 65 64 20 62 79 20 73 71 6c 69   defined by sqli
254de 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
254df 74 69 6f 6e 28 29 20 65 74 63 2e 0a 2a 2a 0a 2a  tion() etc..**.*
254e0 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 74  * If required, t
254e1 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c  his routine call
254e2 73 20 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e  s the 'collation
254e3 20 6e 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63   needed' callbac
254e4 6b 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20  k to.** request 
254e5 61 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  a definition of 
254e6 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  the collating se
254e7 71 75 65 6e 63 65 2e 20 49 66 20 74 68 69 73 20  quence. If this 
254e8 64 6f 65 73 6e 27 74 20 77 6f 72 6b 2c 20 0a 2a  doesn't work, .*
254e9 2a 20 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20  * an equivalent 
254ea 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
254eb 63 65 20 74 68 61 74 20 75 73 65 73 20 61 20 74  ce that uses a t
254ec 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 64 69 66  ext encoding dif
254ed 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74  ferent.** from t
254ee 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
254ef 20 69 73 20 73 75 62 73 74 69 74 75 74 65 64 2c   is substituted,
254f0 20 69 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c   if one is avail
254f1 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
254f2 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
254f3 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28  te3CheckCollSeq(
254f4 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 43  Parse *pParse, C
254f5 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a  ollSeq *pColl){.
254f6 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20    if( pColl ){. 
254f7 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
254f8 4e 61 6d 65 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e  Name = pColl->zN
254f9 61 6d 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ame;.    sqlite3
254fa 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
254fb 62 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  b;.    CollSeq *
254fc 70 20 3d 20 73 71 6c 69 74 65 33 47 65 74 43 6f  p = sqlite3GetCo
254fd 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62  llSeq(db, ENC(db
254fe 29 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 29  ), pColl, zName)
254ff 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a  ;.    if( !p ){.
25500 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
25501 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
25502 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e  o such collation
25503 20 73 65 71 75 65 6e 63 65 3a 20 25 73 22 2c 20   sequence: %s", 
25504 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 50  zName);.      pP
25505 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
25506 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
25507 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
25508 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 43     assert( p==pC
25509 6f 6c 6c 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74  oll );.  }.  ret
2550a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2550b 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65  ..../*.** Locate
2550c 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65   and return an e
2550d 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 64 62  ntry from the db
2550e 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74  .aCollSeq hash t
2550f 61 62 6c 65 2e 20 49 66 20 74 68 65 20 65 6e 74  able. If the ent
25510 72 79 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  ry.** specified 
25511 62 79 20 7a 4e 61 6d 65 20 61 6e 64 20 6e 4e 61  by zName and nNa
25512 6d 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20  me is not found 
25513 61 6e 64 20 70 61 72 61 6d 65 74 65 72 20 27 63  and parameter 'c
25514 72 65 61 74 65 27 20 69 73 0a 2a 2a 20 74 72 75  reate' is.** tru
25515 65 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  e, then create a
25516 20 6e 65 77 20 65 6e 74 72 79 2e 20 4f 74 68 65   new entry. Othe
25517 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c  rwise return NUL
25518 4c 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 6f  L..**.** Each po
25519 69 6e 74 65 72 20 73 74 6f 72 65 64 20 69 6e 20  inter stored in 
2551a 74 68 65 20 73 71 6c 69 74 65 33 2e 61 43 6f 6c  the sqlite3.aCol
2551b 6c 53 65 71 20 68 61 73 68 20 74 61 62 6c 65 20  lSeq hash table 
2551c 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 2a 2a 20 61  contains an.** a
2551d 72 72 61 79 20 6f 66 20 74 68 72 65 65 20 43 6f  rray of three Co
2551e 6c 6c 53 65 71 20 73 74 72 75 63 74 75 72 65 73  llSeq structures
2551f 2e 20 54 68 65 20 66 69 72 73 74 20 69 73 20 74  . The first is t
25520 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
25521 75 65 6e 63 65 0a 2a 2a 20 70 72 65 66 66 65 72  uence.** preffer
25522 72 65 64 20 66 6f 72 20 55 54 46 2d 38 2c 20 74  red for UTF-8, t
25523 68 65 20 73 65 63 6f 6e 64 20 55 54 46 2d 31 36  he second UTF-16
25524 6c 65 2c 20 61 6e 64 20 74 68 65 20 74 68 69 72  le, and the thir
25525 64 20 55 54 46 2d 31 36 62 65 2e 0a 2a 2a 0a 2a  d UTF-16be..**.*
25526 2a 20 53 74 6f 72 65 64 20 69 6d 6d 65 64 69 61  * Stored immedia
25527 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 74  tely after the t
25528 68 72 65 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  hree collation s
25529 65 71 75 65 6e 63 65 73 20 69 73 20 61 20 63 6f  equences is a co
2552a 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c  py of.** the col
2552b 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2552c 6e 61 6d 65 2e 20 41 20 70 6f 69 6e 74 65 72 20  name. A pointer 
2552d 74 6f 20 74 68 69 73 20 73 74 72 69 6e 67 20 69  to this string i
2552e 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 65  s stored in.** e
2552f 61 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ach collation se
25530 71 75 65 6e 63 65 20 73 74 72 75 63 74 75 72 65  quence structure
25531 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c  ..*/.static Coll
25532 53 65 71 20 2a 66 69 6e 64 43 6f 6c 6c 53 65 71  Seq *findCollSeq
25533 45 6e 74 72 79 28 0a 20 20 73 71 6c 69 74 65 33  Entry(.  sqlite3
25534 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
25535 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
25536 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
25537 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
25538 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
25539 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2553a 63 65 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61  ce */.  int crea
2553b 74 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  te            /*
2553c 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 65 6e   Create a new en
2553d 74 72 79 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  try if true */.)
2553e 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
2553f 6c 6c 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20  ll;.  int nName 
25540 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
25541 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c  0(zName);.  pCol
25542 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  l = sqlite3HashF
25543 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ind(&db->aCollSe
25544 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  q, zName, nName)
25545 3b 0a 0a 20 20 69 66 28 20 30 3d 3d 70 43 6f 6c  ;..  if( 0==pCol
25546 6c 20 26 26 20 63 72 65 61 74 65 20 29 7b 0a 20  l && create ){. 
25547 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
25548 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
25549 62 2c 20 33 2a 73 69 7a 65 6f 66 28 2a 70 43 6f  b, 3*sizeof(*pCo
2554a 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 20  ll) + nName + 1 
2554b 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c  );.    if( pColl
2554c 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65   ){.      CollSe
2554d 71 20 2a 70 44 65 6c 20 3d 20 30 3b 0a 20 20 20  q *pDel = 0;.   
2554e 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d     pColl[0].zNam
2554f 65 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c  e = (char*)&pCol
25550 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c  l[3];.      pCol
25551 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54  l[0].enc = SQLIT
25552 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 43  E_UTF8;.      pC
25553 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 28  oll[1].zName = (
25554 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b  char*)&pColl[3];
25555 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e  .      pColl[1].
25556 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
25557 31 36 4c 45 3b 0a 20 20 20 20 20 20 70 43 6f 6c  16LE;.      pCol
25558 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68  l[2].zName = (ch
25559 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20  ar*)&pColl[3];. 
2555a 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d 2e 65 6e       pColl[2].en
2555b 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36  c = SQLITE_UTF16
2555c 42 45 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  BE;.      memcpy
2555d 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c  (pColl[0].zName,
2555e 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a   zName, nName);.
2555f 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a        pColl[0].z
25560 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b  Name[nName] = 0;
25561 0a 20 20 20 20 20 20 70 44 65 6c 20 3d 20 73 71  .      pDel = sq
25562 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
25563 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 70  &db->aCollSeq, p
25564 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 6e  Coll[0].zName, n
25565 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29 3b 0a 0a 20  Name, pColl);.. 
25566 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c       /* If a mal
25567 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f 63  loc() failure oc
25568 63 75 72 72 65 64 20 69 6e 20 73 71 6c 69 74 65  curred in sqlite
25569 33 48 61 73 68 49 6e 73 65 72 74 28 29 2c 20 69  3HashInsert(), i
2556a 74 20 77 69 6c 6c 20 0a 20 20 20 20 20 20 2a 2a  t will .      **
2556b 20 72 65 74 75 72 6e 20 74 68 65 20 70 43 6f 6c   return the pCol
2556c 6c 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20  l pointer to be 
2556d 64 65 6c 65 74 65 64 20 28 62 65 63 61 75 73 65  deleted (because
2556e 20 69 74 20 77 61 73 6e 27 74 20 61 64 64 65 64   it wasn't added
2556f 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65  .      ** to the
25570 20 68 61 73 68 20 74 61 62 6c 65 29 2e 0a 20 20   hash table)..  
25571 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73      */.      ass
25572 65 72 74 28 20 70 44 65 6c 3d 3d 30 20 7c 7c 20  ert( pDel==0 || 
25573 70 44 65 6c 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20  pDel==pColl );. 
25574 20 20 20 20 20 69 66 28 20 70 44 65 6c 21 3d 30       if( pDel!=0
25575 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e   ){.        db->
25576 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
25577 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
25578 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 6c  3DbFree(db, pDel
25579 29 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  );.        pColl
2557a 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
2557b 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2557c 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pColl;.}../*.**
2557d 20 50 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65   Parameter zName
2557e 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 55 54 46   points to a UTF
2557f 2d 38 20 65 6e 63 6f 64 65 64 20 73 74 72 69 6e  -8 encoded strin
25580 67 20 6e 4e 61 6d 65 20 62 79 74 65 73 20 6c 6f  g nName bytes lo
25581 6e 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ng..** Return th
25582 65 20 43 6f 6c 6c 53 65 71 2a 20 70 6f 69 6e 74  e CollSeq* point
25583 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6c 6c 61  er for the colla
25584 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
25585 6d 65 64 20 7a 4e 61 6d 65 0a 2a 2a 20 66 6f 72  med zName.** for
25586 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 27 65   the encoding 'e
25587 6e 63 27 20 66 72 6f 6d 20 74 68 65 20 64 61 74  nc' from the dat
25588 61 62 61 73 65 20 27 64 62 27 2e 0a 2a 2a 0a 2a  abase 'db'..**.*
25589 2a 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 73  * If the entry s
2558a 70 65 63 69 66 69 65 64 20 69 73 20 6e 6f 74 20  pecified is not 
2558b 66 6f 75 6e 64 20 61 6e 64 20 27 63 72 65 61 74  found and 'creat
2558c 65 27 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  e' is true, then
2558d 20 63 72 65 61 74 65 20 61 0a 2a 2a 20 6e 65 77   create a.** new
2558e 20 65 6e 74 72 79 2e 20 20 4f 74 68 65 72 77 69   entry.  Otherwi
2558f 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  se return NULL..
25590 2a 2a 0a 2a 2a 20 41 20 73 65 70 61 72 61 74 65  **.** A separate
25591 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   function sqlite
25592 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29  3LocateCollSeq()
25593 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72   is a wrapper ar
25594 6f 75 6e 64 0a 2a 2a 20 74 68 69 73 20 72 6f 75  ound.** this rou
25595 74 69 6e 65 2e 20 20 73 71 6c 69 74 65 33 4c 6f  tine.  sqlite3Lo
25596 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 6e  cateCollSeq() in
25597 76 6f 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74  vokes the collat
25598 69 6f 6e 20 66 61 63 74 6f 72 79 0a 2a 2a 20 69  ion factory.** i
25599 66 20 6e 65 63 65 73 73 61 72 79 20 61 6e 64 20  f necessary and 
2559a 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72  generates an err
2559b 6f 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68  or message if th
2559c 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
2559d 65 6e 63 65 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62  ence.** cannot b
2559e 65 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53  e found..**.** S
2559f 65 65 20 61 6c 73 6f 3a 20 73 71 6c 69 74 65 33  ee also: sqlite3
255a0 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 2c  LocateCollSeq(),
255a1 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53   sqlite3GetCollS
255a2 65 71 28 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  eq().*/.SQLITE_P
255a3 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
255a4 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
255a5 65 71 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  eq(.  sqlite3 *d
255a6 62 2c 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20 63  b,.  u8 enc,.  c
255a7 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
255a8 2c 0a 20 20 69 6e 74 20 63 72 65 61 74 65 0a 29  ,.  int create.)
255a9 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
255aa 6c 6c 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20  ll;.  if( zName 
255ab 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 66  ){.    pColl = f
255ac 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28  indCollSeqEntry(
255ad 64 62 2c 20 7a 4e 61 6d 65 2c 20 63 72 65 61 74  db, zName, creat
255ae 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
255af 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66   pColl = db->pDf
255b0 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 61 73  ltColl;.  }.  as
255b1 73 65 72 74 28 20 53 51 4c 49 54 45 5f 55 54 46  sert( SQLITE_UTF
255b2 38 3d 3d 31 20 26 26 20 53 51 4c 49 54 45 5f 55  8==1 && SQLITE_U
255b3 54 46 31 36 4c 45 3d 3d 32 20 26 26 20 53 51 4c  TF16LE==2 && SQL
255b4 49 54 45 5f 55 54 46 31 36 42 45 3d 3d 33 20 29  ITE_UTF16BE==3 )
255b5 3b 0a 20 20 61 73 73 65 72 74 28 20 65 6e 63 3e  ;.  assert( enc>
255b6 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26 20  =SQLITE_UTF8 && 
255b7 65 6e 63 3c 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc<=SQLITE_UTF1
255b8 36 42 45 20 29 3b 0a 20 20 69 66 28 20 70 43 6f  6BE );.  if( pCo
255b9 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b 3d 20 65 6e  ll ) pColl += en
255ba 63 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 43  c-1;.  return pC
255bb 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 20 44 75 72 69 6e  oll;.}../* Durin
255bc 67 20 74 68 65 20 73 65 61 72 63 68 20 66 6f 72  g the search for
255bd 20 74 68 65 20 62 65 73 74 20 66 75 6e 63 74 69   the best functi
255be 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 74  on definition, t
255bf 68 69 73 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a  his procedure.**
255c0 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 74 65   is called to te
255c1 73 74 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 20  st how well the 
255c2 66 75 6e 63 74 69 6f 6e 20 70 61 73 73 65 64 20  function passed 
255c3 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
255c4 75 6d 65 6e 74 0a 2a 2a 20 6d 61 74 63 68 65 73  ument.** matches
255c5 20 74 68 65 20 72 65 71 75 65 73 74 20 66 6f 72   the request for
255c6 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
255c7 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 20   nArg arguments 
255c8 69 6e 20 61 20 73 79 73 74 65 6d 0a 2a 2a 20 74  in a system.** t
255c9 68 61 74 20 75 73 65 73 20 65 6e 63 6f 64 69 6e  hat uses encodin
255ca 67 20 65 6e 63 2e 20 54 68 65 20 76 61 6c 75 65  g enc. The value
255cb 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 61   returned indica
255cc 74 65 73 20 68 6f 77 20 77 65 6c 6c 20 74 68 65  tes how well the
255cd 0a 2a 2a 20 72 65 71 75 65 73 74 20 69 73 20 6d  .** request is m
255ce 61 74 63 68 65 64 2e 20 41 20 68 69 67 68 65 72  atched. A higher
255cf 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73   value indicates
255d0 20 61 20 62 65 74 74 65 72 20 6d 61 74 63 68 2e   a better match.
255d1 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
255d2 6e 65 64 20 76 61 6c 75 65 20 69 73 20 61 6c 77  ned value is alw
255d3 61 79 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e  ays between 0 an
255d4 64 20 36 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  d 6, as follows:
255d5 0a 2a 2a 0a 2a 2a 20 30 3a 20 4e 6f 74 20 61 20  .**.** 0: Not a 
255d6 6d 61 74 63 68 2c 20 6f 72 20 69 66 20 6e 41 72  match, or if nAr
255d7 67 3c 30 20 61 6e 64 20 74 68 65 20 66 75 6e 63  g<0 and the func
255d8 74 69 6f 6e 20 69 73 20 68 61 73 20 6e 6f 20 69  tion is has no i
255d9 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a  mplementation..*
255da 2a 20 31 3a 20 41 20 76 61 72 69 61 62 6c 65 20  * 1: A variable 
255db 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63 74 69  arguments functi
255dc 6f 6e 20 74 68 61 74 20 70 72 65 66 65 72 73 20  on that prefers 
255dd 55 54 46 2d 38 20 77 68 65 6e 20 61 20 55 54 46  UTF-8 when a UTF
255de 2d 31 36 0a 2a 2a 20 20 20 20 65 6e 63 6f 64 69  -16.**    encodi
255df 6e 67 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ng is requested,
255e0 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a   or vice versa..
255e1 2a 2a 20 32 3a 20 41 20 76 61 72 69 61 62 6c 65  ** 2: A variable
255e2 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63 74   arguments funct
255e3 69 6f 6e 20 74 68 61 74 20 75 73 65 73 20 55 54  ion that uses UT
255e4 46 2d 31 36 42 45 20 77 68 65 6e 20 55 54 46 2d  F-16BE when UTF-
255e5 31 36 4c 45 20 69 73 0a 2a 2a 20 20 20 20 72 65  16LE is.**    re
255e6 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65  quested, or vice
255e7 20 76 65 72 73 61 2e 0a 2a 2a 20 33 3a 20 41 20   versa..** 3: A 
255e8 76 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65 6e  variable argumen
255e9 74 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 69 6e  ts function usin
255ea 67 20 74 68 65 20 73 61 6d 65 20 74 65 78 74 20  g the same text 
255eb 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 20 34 3a 20  encoding..** 4: 
255ec 41 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  A function with 
255ed 74 68 65 20 65 78 61 63 74 20 6e 75 6d 62 65 72  the exact number
255ee 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 72 65   of arguments re
255ef 71 75 65 73 74 65 64 20 74 68 61 74 0a 2a 2a 20  quested that.** 
255f0 20 20 20 70 72 65 66 65 72 73 20 55 54 46 2d 38     prefers UTF-8
255f1 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36 20 65   when a UTF-16 e
255f2 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 71 75 65  ncoding is reque
255f3 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65  sted, or vice ve
255f4 72 73 61 2e 0a 2a 2a 20 35 3a 20 41 20 66 75 6e  rsa..** 5: A fun
255f5 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 65  ction with the e
255f6 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61  xact number of a
255f7 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65 73 74  rguments request
255f8 65 64 20 74 68 61 74 0a 2a 2a 20 20 20 20 70 72  ed that.**    pr
255f9 65 66 65 72 73 20 55 54 46 2d 31 36 4c 45 20 77  efers UTF-16LE w
255fa 68 65 6e 20 55 54 46 2d 31 36 42 45 20 69 73 20  hen UTF-16BE is 
255fb 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69  requested, or vi
255fc 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 36 3a 20  ce versa..** 6: 
255fd 41 6e 20 65 78 61 63 74 20 6d 61 74 63 68 2e 0a  An exact match..
255fe 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  **.*/.static int
255ff 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 46 75   matchQuality(Fu
25600 6e 63 44 65 66 20 2a 70 2c 20 69 6e 74 20 6e 41  ncDef *p, int nA
25601 72 67 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69  rg, u8 enc){.  i
25602 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b 0a 20 20  nt match = 0;.  
25603 69 66 28 20 70 2d 3e 6e 41 72 67 3d 3d 2d 31 20  if( p->nArg==-1 
25604 7c 7c 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67  || p->nArg==nArg
25605 20 0a 20 20 20 7c 7c 20 28 6e 41 72 67 3d 3d 2d   .   || (nArg==-
25606 31 20 26 26 20 28 70 2d 3e 78 46 75 6e 63 21 3d  1 && (p->xFunc!=
25607 30 20 7c 7c 20 70 2d 3e 78 53 74 65 70 21 3d 30  0 || p->xStep!=0
25608 29 29 0a 20 20 29 7b 0a 20 20 20 20 6d 61 74 63  )).  ){.    matc
25609 68 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 70  h = 1;.    if( p
2560a 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c 20  ->nArg==nArg || 
2560b 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 20  nArg==-1 ){.    
2560c 20 20 6d 61 74 63 68 20 3d 20 34 3b 0a 20 20 20    match = 4;.   
2560d 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 3d 3d   }.    if( enc==
2560e 70 2d 3e 69 50 72 65 66 45 6e 63 20 29 7b 0a 20  p->iPrefEnc ){. 
2560f 20 20 20 20 20 6d 61 74 63 68 20 2b 3d 20 32 3b       match += 2;
25610 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 20  .    }.    else 
25611 69 66 28 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45  if( (enc==SQLITE
25612 5f 55 54 46 31 36 4c 45 20 26 26 20 70 2d 3e 69  _UTF16LE && p->i
25613 50 72 65 66 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  PrefEnc==SQLITE_
25614 55 54 46 31 36 42 45 29 20 7c 7c 0a 20 20 20 20  UTF16BE) ||.    
25615 20 20 20 20 20 20 20 20 20 28 65 6e 63 3d 3d 53           (enc==S
25616 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 26 26  QLITE_UTF16BE &&
25617 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51   p->iPrefEnc==SQ
25618 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 20 29 7b  LITE_UTF16LE) ){
25619 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b 3d 20  .      match += 
2561a 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
2561b 65 74 75 72 6e 20 6d 61 74 63 68 3b 0a 7d 0a 0a  eturn match;.}..
2561c 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 61 20 46  /*.** Search a F
2561d 75 6e 63 44 65 66 48 61 73 68 20 66 6f 72 20 61  uncDefHash for a
2561e 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
2561f 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 20 20  he given name.  
25620 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e  Return.** a poin
25621 74 65 72 20 74 6f 20 74 68 65 20 6d 61 74 63 68  ter to the match
25622 69 6e 67 20 46 75 6e 63 44 65 66 20 69 66 20 66  ing FuncDef if f
25623 6f 75 6e 64 2c 20 6f 72 20 30 20 69 66 20 74 68  ound, or 0 if th
25624 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e  ere is no match.
25625 0a 2a 2f 0a 73 74 61 74 69 63 20 46 75 6e 63 44  .*/.static FuncD
25626 65 66 20 2a 66 75 6e 63 74 69 6f 6e 53 65 61 72  ef *functionSear
25627 63 68 28 0a 20 20 46 75 6e 63 44 65 66 48 61 73  ch(.  FuncDefHas
25628 68 20 2a 70 48 61 73 68 2c 20 20 2f 2a 20 48 61  h *pHash,  /* Ha
25629 73 68 20 74 61 62 6c 65 20 74 6f 20 73 65 61 72  sh table to sear
2562a 63 68 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20  ch */.  int h,  
2562b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2562c 48 61 73 68 20 6f 66 20 74 68 65 20 6e 61 6d 65  Hash of the name
2562d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2562e 20 2a 7a 46 75 6e 63 2c 20 20 20 2f 2a 20 4e 61   *zFunc,   /* Na
2562f 6d 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 2a  me of function *
25630 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 20 20 20  /.  int nFunc   
25631 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
25632 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a  er of bytes in z
25633 46 75 6e 63 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e  Func */.){.  Fun
25634 63 44 65 66 20 2a 70 3b 0a 20 20 66 6f 72 28 70  cDef *p;.  for(p
25635 3d 70 48 61 73 68 2d 3e 61 5b 68 5d 3b 20 70 3b  =pHash->a[h]; p;
25636 20 70 3d 70 2d 3e 70 48 61 73 68 29 7b 0a 20 20   p=p->pHash){.  
25637 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
25638 4e 49 43 6d 70 28 70 2d 3e 7a 4e 61 6d 65 2c 20  NICmp(p->zName, 
25639 7a 46 75 6e 63 2c 20 6e 46 75 6e 63 29 3d 3d 30  zFunc, nFunc)==0
2563a 20 26 26 20 70 2d 3e 7a 4e 61 6d 65 5b 6e 46 75   && p->zName[nFu
2563b 6e 63 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  nc]==0 ){.      
2563c 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 7d 0a  return p;.    }.
2563d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
2563e 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
2563f 61 20 6e 65 77 20 46 75 6e 63 44 65 66 20 69 6e  a new FuncDef in
25640 74 6f 20 61 20 46 75 6e 63 44 65 66 48 61 73 68  to a FuncDefHash
25641 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a   hash table..*/.
25642 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
25643 6f 69 64 20 73 71 6c 69 74 65 33 46 75 6e 63 44  oid sqlite3FuncD
25644 65 66 49 6e 73 65 72 74 28 0a 20 20 46 75 6e 63  efInsert(.  Func
25645 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 2c 20  DefHash *pHash, 
25646 20 2f 2a 20 54 68 65 20 68 61 73 68 20 74 61 62   /* The hash tab
25647 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f  le into which to
25648 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 46 75 6e   insert */.  Fun
25649 63 44 65 66 20 2a 70 44 65 66 20 20 20 20 20 20  cDef *pDef      
2564a 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f    /* The functio
2564b 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 74 6f 20  n definition to 
2564c 69 6e 73 65 72 74 20 2a 2f 0a 29 7b 0a 20 20 46  insert */.){.  F
2564d 75 6e 63 44 65 66 20 2a 70 4f 74 68 65 72 3b 0a  uncDef *pOther;.
2564e 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71    int nName = sq
2564f 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44  lite3Strlen30(pD
25650 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 75 38  ef->zName);.  u8
25651 20 63 31 20 3d 20 28 75 38 29 70 44 65 66 2d 3e   c1 = (u8)pDef->
25652 7a 4e 61 6d 65 5b 30 5d 3b 0a 20 20 69 6e 74 20  zName[0];.  int 
25653 68 20 3d 20 28 73 71 6c 69 74 65 33 55 70 70 65  h = (sqlite3Uppe
25654 72 54 6f 4c 6f 77 65 72 5b 63 31 5d 20 2b 20 6e  rToLower[c1] + n
25655 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53 69 7a  Name) % ArraySiz
25656 65 28 70 48 61 73 68 2d 3e 61 29 3b 0a 20 20 70  e(pHash->a);.  p
25657 4f 74 68 65 72 20 3d 20 66 75 6e 63 74 69 6f 6e  Other = function
25658 53 65 61 72 63 68 28 70 48 61 73 68 2c 20 68 2c  Search(pHash, h,
25659 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e   pDef->zName, nN
2565a 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4f 74 68  ame);.  if( pOth
2565b 65 72 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  er ){.    assert
2565c 28 20 70 4f 74 68 65 72 21 3d 70 44 65 66 20 26  ( pOther!=pDef &
2565d 26 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 21  & pOther->pNext!
2565e 3d 70 44 65 66 20 29 3b 0a 20 20 20 20 70 44 65  =pDef );.    pDe
2565f 66 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68 65  f->pNext = pOthe
25660 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70 4f  r->pNext;.    pO
25661 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20 70 44  ther->pNext = pD
25662 65 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ef;.  }else{.   
25663 20 70 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20 30   pDef->pNext = 0
25664 3b 0a 20 20 20 20 70 44 65 66 2d 3e 70 48 61 73  ;.    pDef->pHas
25665 68 20 3d 20 70 48 61 73 68 2d 3e 61 5b 68 5d 3b  h = pHash->a[h];
25666 0a 20 20 20 20 70 48 61 73 68 2d 3e 61 5b 68 5d  .    pHash->a[h]
25667 20 3d 20 70 44 65 66 3b 0a 20 20 7d 0a 7d 0a 20   = pDef;.  }.}. 
25668 20 0a 20 20 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61   .  ../*.** Loca
25669 74 65 20 61 20 75 73 65 72 20 66 75 6e 63 74 69  te a user functi
2566a 6f 6e 20 67 69 76 65 6e 20 61 20 6e 61 6d 65 2c  on given a name,
2566b 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67   a number of arg
2566c 75 6d 65 6e 74 73 20 61 6e 64 20 61 20 66 6c 61  uments and a fla
2566d 67 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20  g.** indicating 
2566e 77 68 65 74 68 65 72 20 74 68 65 20 66 75 6e 63  whether the func
2566f 74 69 6f 6e 20 70 72 65 66 65 72 73 20 55 54 46  tion prefers UTF
25670 2d 31 36 20 6f 76 65 72 20 55 54 46 2d 38 2e 20  -16 over UTF-8. 
25671 20 52 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69   Return a.** poi
25672 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63  nter to the Func
25673 44 65 66 20 73 74 72 75 63 74 75 72 65 20 74 68  Def structure th
25674 61 74 20 64 65 66 69 6e 65 73 20 74 68 61 74 20  at defines that 
25675 66 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 72 65 74  function, or ret
25676 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 20 69 66 20 74  urn.** NULL if t
25677 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  he function does
25678 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a   not exist..**.*
25679 2a 20 49 66 20 74 68 65 20 63 72 65 61 74 65 46  * If the createF
2567a 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20  lag argument is 
2567b 74 72 75 65 2c 20 74 68 65 6e 20 61 20 6e 65 77  true, then a new
2567c 20 28 62 6c 61 6e 6b 29 20 46 75 6e 63 44 65 66   (blank) FuncDef
2567d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73  .** structure is
2567e 20 63 72 65 61 74 65 64 20 61 6e 64 20 6c 69 6b   created and lik
2567f 65 64 20 69 6e 74 6f 20 74 68 65 20 22 64 62 22  ed into the "db"
25680 20 73 74 72 75 63 74 75 72 65 20 69 66 20 61 0a   structure if a.
25681 2a 2a 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 66  ** no matching f
25682 75 6e 63 74 69 6f 6e 20 70 72 65 76 69 6f 75 73  unction previous
25683 6c 79 20 65 78 69 73 74 65 64 2e 20 20 57 68 65  ly existed.  Whe
25684 6e 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20  n createFlag is 
25685 74 72 75 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20  true.** and the 
25686 6e 41 72 67 20 70 61 72 61 6d 65 74 65 72 20 69  nArg parameter i
25687 73 20 2d 31 2c 20 74 68 65 6e 20 6f 6e 6c 79 20  s -1, then only 
25688 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  a function that 
25689 61 63 63 65 70 74 73 0a 2a 2a 20 61 6e 79 20 6e  accepts.** any n
2568a 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
2568b 74 73 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  ts will be retur
2568c 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 72  ned..**.** If cr
2568d 65 61 74 65 46 6c 61 67 20 69 73 20 66 61 6c 73  eateFlag is fals
2568e 65 20 61 6e 64 20 6e 41 72 67 20 69 73 20 2d 31  e and nArg is -1
2568f 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74  , then the first
25690 20 76 61 6c 69 64 0a 2a 2a 20 66 75 6e 63 74 69   valid.** functi
25691 6f 6e 20 66 6f 75 6e 64 20 69 73 20 72 65 74 75  on found is retu
25692 72 6e 65 64 2e 20 20 41 20 66 75 6e 63 74 69 6f  rned.  A functio
25693 6e 20 69 73 20 76 61 6c 69 64 20 69 66 20 65 69  n is valid if ei
25694 74 68 65 72 20 78 46 75 6e 63 0a 2a 2a 20 6f 72  ther xFunc.** or
25695 20 78 53 74 65 70 20 69 73 20 6e 6f 6e 2d 7a 65   xStep is non-ze
25696 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 72 65  ro..**.** If cre
25697 61 74 65 46 6c 61 67 20 69 73 20 66 61 6c 73 65  ateFlag is false
25698 2c 20 74 68 65 6e 20 61 20 66 75 6e 63 74 69 6f  , then a functio
25699 6e 20 77 69 74 68 20 74 68 65 20 72 65 71 75 69  n with the requi
2569a 72 65 64 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20  red name and.** 
2569b 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
2569c 6e 74 73 20 6d 61 79 20 62 65 20 72 65 74 75 72  nts may be retur
2569d 6e 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 20  ned even if the 
2569e 65 54 65 78 74 52 65 70 20 66 6c 61 67 20 64 6f  eTextRep flag do
2569f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68 20  es not.** match 
256a0 74 68 61 74 20 72 65 71 75 65 73 74 65 64 2e 0a  that requested..
256a1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
256a2 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74  E FuncDef *sqlit
256a3 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 0a  e3FindFunction(.
256a4 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
256a5 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20       /* An open 
256a6 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f  database */.  co
256a7 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
256a8 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
256a9 66 75 6e 63 74 69 6f 6e 2e 20 20 4e 6f 74 20 6e  function.  Not n
256aa 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 2a  ull-terminated *
256ab 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 20 20  /.  int nName,  
256ac 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
256ad 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69   of characters i
256ae 6e 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  n the name */.  
256af 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20  int nArg,       
256b0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
256b1 61 72 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d  arguments.  -1 m
256b2 65 61 6e 73 20 61 6e 79 20 6e 75 6d 62 65 72 20  eans any number 
256b3 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20  */.  u8 enc,    
256b4 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65          /* Prefe
256b5 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69  rred text encodi
256b6 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61  ng */.  int crea
256b7 74 65 46 6c 61 67 20 20 20 20 20 2f 2a 20 43 72  teFlag     /* Cr
256b8 65 61 74 65 20 6e 65 77 20 65 6e 74 72 79 20 69  eate new entry i
256b9 66 20 74 72 75 65 20 61 6e 64 20 64 6f 65 73 20  f true and does 
256ba 6e 6f 74 20 6f 74 68 65 72 77 69 73 65 20 65 78  not otherwise ex
256bb 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63  ist */.){.  Func
256bc 44 65 66 20 2a 70 3b 20 20 20 20 20 20 20 20 20  Def *p;         
256bd 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
256be 61 62 6c 65 20 2a 2f 0a 20 20 46 75 6e 63 44 65  able */.  FuncDe
256bf 66 20 2a 70 42 65 73 74 20 3d 20 30 3b 20 2f 2a  f *pBest = 0; /*
256c0 20 42 65 73 74 20 6d 61 74 63 68 20 66 6f 75 6e   Best match foun
256c1 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e  d so far */.  in
256c2 74 20 62 65 73 74 53 63 6f 72 65 20 3d 20 30 3b  t bestScore = 0;
256c3 20 20 2f 2a 20 53 63 6f 72 65 20 6f 66 20 62 65    /* Score of be
256c4 73 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 69 6e  st match */.  in
256c5 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  t h;            
256c6 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20    /* Hash value 
256c7 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 65  */...  assert( e
256c8 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
256c9 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  || enc==SQLITE_U
256ca 54 46 31 36 4c 45 20 7c 7c 20 65 6e 63 3d 3d 53  TF16LE || enc==S
256cb 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b  QLITE_UTF16BE );
256cc 0a 20 20 68 20 3d 20 28 73 71 6c 69 74 65 33 55  .  h = (sqlite3U
256cd 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29  pperToLower[(u8)
256ce 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20 6e 4e 61 6d  zName[0]] + nNam
256cf 65 29 20 25 20 41 72 72 61 79 53 69 7a 65 28 64  e) % ArraySize(d
256d0 62 2d 3e 61 46 75 6e 63 2e 61 29 3b 0a 0a 20 20  b->aFunc.a);..  
256d1 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 20  /* First search 
256d2 66 6f 72 20 61 20 6d 61 74 63 68 20 61 6d 6f 6e  for a match amon
256d3 67 73 74 20 74 68 65 20 61 70 70 6c 69 63 61 74  gst the applicat
256d4 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
256d5 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 20  tions..  */.  p 
256d6 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68  = functionSearch
256d7 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20 68 2c 20  (&db->aFunc, h, 
256d8 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20  zName, nName);. 
256d9 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
256da 20 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d 61 74   int score = mat
256db 63 68 51 75 61 6c 69 74 79 28 70 2c 20 6e 41 72  chQuality(p, nAr
256dc 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20 69 66 28  g, enc);.    if(
256dd 20 73 63 6f 72 65 3e 62 65 73 74 53 63 6f 72 65   score>bestScore
256de 20 29 7b 0a 20 20 20 20 20 20 70 42 65 73 74 20   ){.      pBest 
256df 3d 20 70 3b 0a 20 20 20 20 20 20 62 65 73 74 53  = p;.      bestS
256e0 63 6f 72 65 20 3d 20 73 63 6f 72 65 3b 0a 20 20  core = score;.  
256e1 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70    }.    p = p->p
256e2 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Next;.  }..  /* 
256e3 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66  If no match is f
256e4 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 74 68 65  ound, search the
256e5 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69   built-in functi
256e6 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45  ons..  **.  ** E
256e7 78 63 65 70 74 2c 20 69 66 20 63 72 65 61 74 65  xcept, if create
256e8 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68  Flag is true, th
256e9 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  at means that we
256ea 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 0a 20   are trying to. 
256eb 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6e 65   ** install a ne
256ec 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20 57 68 61  w function.  Wha
256ed 74 65 76 65 72 20 46 75 6e 63 44 65 66 20 73 74  tever FuncDef st
256ee 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72  ructure is retur
256ef 6e 65 64 20 77 69 6c 6c 0a 20 20 2a 2a 20 68 61  ned will.  ** ha
256f0 76 65 20 66 69 65 6c 64 73 20 6f 76 65 72 77 72  ve fields overwr
256f1 69 74 74 65 6e 20 77 69 74 68 20 6e 65 77 20 69  itten with new i
256f2 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f  nformation appro
256f3 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 0a 20  priate for the. 
256f4 20 2a 2a 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e   ** new function
256f5 2e 20 20 42 75 74 20 74 68 65 20 46 75 6e 63 44  .  But the FuncD
256f6 65 66 73 20 66 6f 72 20 62 75 69 6c 74 2d 69 6e  efs for built-in
256f7 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 72   functions are r
256f8 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53  ead-only..  ** S
256f9 6f 20 77 65 20 6d 75 73 74 20 6e 6f 74 20 73 65  o we must not se
256fa 61 72 63 68 20 66 6f 72 20 62 75 69 6c 74 2d 69  arch for built-i
256fb 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67  ns when creating
256fc 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e   a new function.
256fd 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 21 63 72  .  */ .  if( !cr
256fe 65 61 74 65 46 6c 61 67 20 26 26 20 21 70 42 65  eateFlag && !pBe
256ff 73 74 20 29 7b 0a 20 20 20 20 46 75 6e 63 44 65  st ){.    FuncDe
25700 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26  fHash *pHash = &
25701 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61  GLOBAL(FuncDefHa
25702 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  sh, sqlite3Globa
25703 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 20  lFunctions);.   
25704 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61   p = functionSea
25705 72 63 68 28 70 48 61 73 68 2c 20 68 2c 20 7a 4e  rch(pHash, h, zN
25706 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
25707 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
25708 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d     int score = m
25709 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c 20 6e  atchQuality(p, n
2570a 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20 20  Arg, enc);.     
2570b 20 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74 53   if( score>bestS
2570c 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20  core ){.        
2570d 70 42 65 73 74 20 3d 20 70 3b 0a 20 20 20 20 20  pBest = p;.     
2570e 20 20 20 62 65 73 74 53 63 6f 72 65 20 3d 20 73     bestScore = s
2570f 63 6f 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  core;.      }.  
25710 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74      p = p->pNext
25711 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
25712 2a 20 49 66 20 74 68 65 20 63 72 65 61 74 65 46  * If the createF
25713 6c 61 67 20 70 61 72 61 6d 65 74 65 72 20 69 73  lag parameter is
25714 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 73 65   true and the se
25715 61 72 63 68 20 64 69 64 20 6e 6f 74 20 72 65 76  arch did not rev
25716 65 61 6c 20 61 6e 0a 20 20 2a 2a 20 65 78 61 63  eal an.  ** exac
25717 74 20 6d 61 74 63 68 20 66 6f 72 20 74 68 65 20  t match for the 
25718 6e 61 6d 65 2c 20 6e 75 6d 62 65 72 20 6f 66 20  name, number of 
25719 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 65 6e  arguments and en
2571a 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 61 64 64  coding, then add
2571b 20 61 0a 20 20 2a 2a 20 6e 65 77 20 65 6e 74 72   a.  ** new entr
2571c 79 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  y to the hash ta
2571d 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 69  ble and return i
2571e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 72  t..  */.  if( cr
2571f 65 61 74 65 46 6c 61 67 20 26 26 20 28 62 65 73  eateFlag && (bes
25720 74 53 63 6f 72 65 3c 36 20 7c 7c 20 70 42 65 73  tScore<6 || pBes
25721 74 2d 3e 6e 41 72 67 21 3d 6e 41 72 67 29 20 26  t->nArg!=nArg) &
25722 26 20 0a 20 20 20 20 20 20 28 70 42 65 73 74 20  & .      (pBest 
25723 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
25724 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
25725 28 2a 70 42 65 73 74 29 2b 6e 4e 61 6d 65 2b 31  (*pBest)+nName+1
25726 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 42 65  ))!=0 ){.    pBe
25727 73 74 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  st->zName = (cha
25728 72 20 2a 29 26 70 42 65 73 74 5b 31 5d 3b 0a 20  r *)&pBest[1];. 
25729 20 20 20 70 42 65 73 74 2d 3e 6e 41 72 67 20 3d     pBest->nArg =
2572a 20 28 75 31 36 29 6e 41 72 67 3b 0a 20 20 20 20   (u16)nArg;.    
2572b 70 42 65 73 74 2d 3e 69 50 72 65 66 45 6e 63 20  pBest->iPrefEnc 
2572c 3d 20 65 6e 63 3b 0a 20 20 20 20 6d 65 6d 63 70  = enc;.    memcp
2572d 79 28 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20  y(pBest->zName, 
2572e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20  zName, nName);. 
2572f 20 20 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 5b     pBest->zName[
25730 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20  nName] = 0;.    
25731 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e  sqlite3FuncDefIn
25732 73 65 72 74 28 26 64 62 2d 3e 61 46 75 6e 63 2c  sert(&db->aFunc,
25733 20 70 42 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20   pBest);.  }..  
25734 69 66 28 20 70 42 65 73 74 20 26 26 20 28 70 42  if( pBest && (pB
25735 65 73 74 2d 3e 78 53 74 65 70 20 7c 7c 20 70 42  est->xStep || pB
25736 65 73 74 2d 3e 78 46 75 6e 63 20 7c 7c 20 63 72  est->xFunc || cr
25737 65 61 74 65 46 6c 61 67 29 20 29 7b 0a 20 20 20  eateFlag) ){.   
25738 20 72 65 74 75 72 6e 20 70 42 65 73 74 3b 0a 20   return pBest;. 
25739 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
2573a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c  ../*.** Free all
2573b 20 72 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20   resources held 
2573c 62 79 20 74 68 65 20 73 63 68 65 6d 61 20 73 74  by the schema st
2573d 72 75 63 74 75 72 65 2e 20 54 68 65 20 76 6f 69  ructure. The voi
2573e 64 2a 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e  d* argument poin
2573f 74 73 0a 2a 2a 20 61 74 20 61 20 53 63 68 65 6d  ts.** at a Schem
25740 61 20 73 74 72 75 63 74 2e 20 54 68 69 73 20 66  a struct. This f
25741 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  unction does not
25742 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46   call sqlite3DbF
25743 72 65 65 28 64 62 2c 20 29 20 6f 6e 20 74 68 65  ree(db, ) on the
25744 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 74 73   .** pointer its
25745 65 6c 66 2c 20 69 74 20 6a 75 73 74 20 63 6c 65  elf, it just cle
25746 61 6e 73 20 75 70 20 73 75 62 73 69 64 75 61 72  ans up subsiduar
25747 79 20 72 65 73 6f 75 72 63 65 73 20 28 69 2e 65  y resources (i.e
25748 2e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a  . the contents.*
25749 2a 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20  * of the schema 
2574a 68 61 73 68 20 74 61 62 6c 65 73 29 2e 0a 2a 2a  hash tables)..**
2574b 0a 2a 2a 20 54 68 65 20 53 63 68 65 6d 61 2e 63  .** The Schema.c
2574c 61 63 68 65 5f 73 69 7a 65 20 76 61 72 69 61 62  ache_size variab
2574d 6c 65 20 69 73 20 6e 6f 74 20 63 6c 65 61 72 65  le is not cleare
2574e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
2574f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
25750 33 53 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64  3SchemaFree(void
25751 20 2a 70 29 7b 0a 20 20 48 61 73 68 20 74 65 6d   *p){.  Hash tem
25752 70 31 3b 0a 20 20 48 61 73 68 20 74 65 6d 70 32  p1;.  Hash temp2
25753 3b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 45  ;.  HashElem *pE
25754 6c 65 6d 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70  lem;.  Schema *p
25755 53 63 68 65 6d 61 20 3d 20 28 53 63 68 65 6d 61  Schema = (Schema
25756 20 2a 29 70 3b 0a 0a 20 20 74 65 6d 70 31 20 3d   *)p;..  temp1 =
25757 20 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73   pSchema->tblHas
25758 68 3b 0a 20 20 74 65 6d 70 32 20 3d 20 70 53 63  h;.  temp2 = pSc
25759 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 3b 0a  hema->trigHash;.
2575a 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
2575b 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 72 69 67  t(&pSchema->trig
2575c 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33  Hash);.  sqlite3
2575d 48 61 73 68 43 6c 65 61 72 28 26 70 53 63 68 65  HashClear(&pSche
2575e 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 0a 20 20  ma->idxHash);.  
2575f 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65  for(pElem=sqlite
25760 48 61 73 68 46 69 72 73 74 28 26 74 65 6d 70 32  HashFirst(&temp2
25761 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d  ); pElem; pElem=
25762 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
25763 45 6c 65 6d 29 29 7b 0a 20 20 20 20 73 71 6c 69  Elem)){.    sqli
25764 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
25765 28 30 2c 20 28 54 72 69 67 67 65 72 2a 29 73 71  (0, (Trigger*)sq
25766 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c  liteHashData(pEl
25767 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  em));.  }.  sqli
25768 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 74 65  te3HashClear(&te
25769 6d 70 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 48  mp2);.  sqlite3H
2576a 61 73 68 49 6e 69 74 28 26 70 53 63 68 65 6d 61  ashInit(&pSchema
2576b 2d 3e 74 62 6c 48 61 73 68 29 3b 0a 20 20 66 6f  ->tblHash);.  fo
2576c 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61  r(pElem=sqliteHa
2576d 73 68 46 69 72 73 74 28 26 74 65 6d 70 31 29 3b  shFirst(&temp1);
2576e 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71   pElem; pElem=sq
2576f 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c  liteHashNext(pEl
25770 65 6d 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20  em)){.    Table 
25771 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61  *pTab = sqliteHa
25772 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20  shData(pElem);. 
25773 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d     assert( pTab-
25774 3e 64 62 4d 65 6d 3d 3d 30 20 29 3b 0a 20 20 20  >dbMem==0 );.   
25775 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
25776 62 6c 65 28 70 54 61 62 29 3b 0a 20 20 7d 0a 20  ble(pTab);.  }. 
25777 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
25778 72 28 26 74 65 6d 70 31 29 3b 0a 20 20 73 71 6c  r(&temp1);.  sql
25779 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 70  ite3HashClear(&p
2577a 53 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68  Schema->fkeyHash
2577b 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d 3e 70 53  );.  pSchema->pS
2577c 65 71 54 61 62 20 3d 20 30 3b 0a 20 20 70 53 63  eqTab = 0;.  pSc
2577d 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  hema->flags &= ~
2577e 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 3b  DB_SchemaLoaded;
2577f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61  .}../*.** Find a
25780 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 63  nd return the sc
25781 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20  hema associated 
25782 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 43  with a BTree.  C
25783 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6f  reate.** a new o
25784 6e 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  ne if necessary.
25785 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
25786 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74  TE Schema *sqlit
25787 65 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69  e3SchemaGet(sqli
25788 74 65 33 20 2a 64 62 2c 20 42 74 72 65 65 20 2a  te3 *db, Btree *
25789 70 42 74 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a  pBt){.  Schema *
2578a 20 70 3b 0a 20 20 69 66 28 20 70 42 74 20 29 7b   p;.  if( pBt ){
2578b 0a 20 20 20 20 70 20 3d 20 28 53 63 68 65 6d 61  .    p = (Schema
2578c 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 53   *)sqlite3BtreeS
2578d 63 68 65 6d 61 28 70 42 74 2c 20 73 69 7a 65 6f  chema(pBt, sizeo
2578e 66 28 53 63 68 65 6d 61 29 2c 20 73 71 6c 69 74  f(Schema), sqlit
2578f 65 33 53 63 68 65 6d 61 46 72 65 65 29 3b 0a 20  e3SchemaFree);. 
25790 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20   }else{.    p = 
25791 28 53 63 68 65 6d 61 20 2a 29 73 71 6c 69 74 65  (Schema *)sqlite
25792 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65  3MallocZero(size
25793 6f 66 28 53 63 68 65 6d 61 29 29 3b 0a 20 20 7d  of(Schema));.  }
25794 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20  .  if( !p ){.   
25795 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
25796 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69  d = 1;.  }else i
25797 66 20 28 20 30 3d 3d 70 2d 3e 66 69 6c 65 5f 66  f ( 0==p->file_f
25798 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 73 71 6c  ormat ){.    sql
25799 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d  ite3HashInit(&p-
2579a 3e 74 62 6c 48 61 73 68 29 3b 0a 20 20 20 20 73  >tblHash);.    s
2579b 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
2579c 70 2d 3e 69 64 78 48 61 73 68 29 3b 0a 20 20 20  p->idxHash);.   
2579d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74   sqlite3HashInit
2579e 28 26 70 2d 3e 74 72 69 67 48 61 73 68 29 3b 0a  (&p->trigHash);.
2579f 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
257a0 6e 69 74 28 26 70 2d 3e 66 6b 65 79 48 61 73 68  nit(&p->fkeyHash
257a1 29 3b 0a 20 20 20 20 70 2d 3e 65 6e 63 20 3d 20  );.    p->enc = 
257a2 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 7d  SQLITE_UTF8;.  }
257a3 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
257a4 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
257a5 45 6e 64 20 6f 66 20 63 61 6c 6c 62 61 63 6b 2e  End of callback.
257a6 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
257a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
257a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
257a9 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
257aa 42 65 67 69 6e 20 66 69 6c 65 20 64 65 6c 65 74  Begin file delet
257ab 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
257ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
257ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
257ae 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
257af 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
257b0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
257b1 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
257b2 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
257b3 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
257b4 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
257b5 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
257b6 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
257b7 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
257b8 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
257b9 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
257ba 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
257bb 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
257bc 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
257bd 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
257be 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
257bf 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
257c0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
257c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
257c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
257c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
257c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
257c5 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
257c6 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ile contains C c
257c7 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ode routines tha
257c8 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20  t are called by 
257c9 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 69 6e  the parser.** in
257ca 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61   order to genera
257cb 74 65 20 63 6f 64 65 20 66 6f 72 20 44 45 4c 45  te code for DELE
257cc 54 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e  TE FROM statemen
257cd 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64  ts..**.** $Id: d
257ce 65 6c 65 74 65 2e 63 2c 76 20 31 2e 32 30 37 20  elete.c,v 1.207 
257cf 32 30 30 39 2f 30 38 2f 30 38 20 31 38 3a 30 31  2009/08/08 18:01
257d0 3a 30 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :08 drh Exp $.*/
257d1 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 20 75 70 20  ../*.** Look up 
257d2 65 76 65 72 79 20 74 61 62 6c 65 20 74 68 61 74  every table that
257d3 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 70 53 72   is named in pSr
257d4 63 2e 20 20 49 66 20 61 6e 79 20 74 61 62 6c 65  c.  If any table
257d5 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 0a 2a   is not found,.*
257d6 2a 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d  * add an error m
257d7 65 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65  essage to pParse
257d8 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 72 65  ->zErrMsg and re
257d9 74 75 72 6e 20 4e 55 4c 4c 2e 20 20 49 66 20 61  turn NULL.  If a
257da 6c 6c 20 74 61 62 6c 65 73 0a 2a 2a 20 61 72 65  ll tables.** are
257db 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 61   found, return a
257dc 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
257dd 6c 61 73 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  last table..*/.S
257de 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61  QLITE_PRIVATE Ta
257df 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c  ble *sqlite3SrcL
257e0 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20  istLookup(Parse 
257e1 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74  *pParse, SrcList
257e2 20 2a 70 53 72 63 29 7b 0a 20 20 73 74 72 75 63   *pSrc){.  struc
257e3 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
257e4 70 49 74 65 6d 20 3d 20 70 53 72 63 2d 3e 61 3b  pItem = pSrc->a;
257e5 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
257e6 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 20    assert( pItem 
257e7 26 26 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31  && pSrc->nSrc==1
257e8 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   );.  pTab = sql
257e9 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
257ea 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65 6d  pParse, 0, pItem
257eb 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e  ->zName, pItem->
257ec 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 73 71  zDatabase);.  sq
257ed 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
257ee 28 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a 20  (pItem->pTab);. 
257ef 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70   pItem->pTab = p
257f0 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61 62 20  Tab;.  if( pTab 
257f1 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 6e 52 65  ){.    pTab->nRe
257f2 66 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  f++;.  }.  if( s
257f3 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c  qlite3IndexedByL
257f4 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 49  ookup(pParse, pI
257f5 74 65 6d 29 20 29 7b 0a 20 20 20 20 70 54 61 62  tem) ){.    pTab
257f6 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
257f7 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pTab;.}../*.*
257f8 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  * Check to make 
257f9 73 75 72 65 20 74 68 65 20 67 69 76 65 6e 20 74  sure the given t
257fa 61 62 6c 65 20 69 73 20 77 72 69 74 61 62 6c 65  able is writable
257fb 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 0a  .  If it is not.
257fc 2a 2a 20 77 72 69 74 61 62 6c 65 2c 20 67 65 6e  ** writable, gen
257fd 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d  erate an error m
257fe 65 73 73 61 67 65 20 61 6e 64 20 72 65 74 75 72  essage and retur
257ff 6e 20 31 2e 20 20 49 66 20 69 74 20 69 73 0a 2a  n 1.  If it is.*
25800 2a 20 77 72 69 74 61 62 6c 65 20 72 65 74 75 72  * writable retur
25801 6e 20 30 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  n 0;.*/.SQLITE_P
25802 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
25803 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 50 61 72  e3IsReadOnly(Par
25804 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
25805 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 76 69 65  e *pTab, int vie
25806 77 4f 6b 29 7b 0a 20 20 2f 2a 20 41 20 74 61 62  wOk){.  /* A tab
25807 6c 65 20 69 73 20 6e 6f 74 20 77 72 69 74 61 62  le is not writab
25808 6c 65 20 75 6e 64 65 72 20 74 68 65 20 66 6f 6c  le under the fol
25809 6c 6f 77 69 6e 67 20 63 69 72 63 75 6d 73 74 61  lowing circumsta
2580a 6e 63 65 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  nces:.  **.  ** 
2580b 20 20 31 29 20 49 74 20 69 73 20 61 20 76 69 72    1) It is a vir
2580c 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 6e  tual table and n
2580d 6f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  o implementation
2580e 20 6f 66 20 74 68 65 20 78 55 70 64 61 74 65 20   of the xUpdate 
2580f 6d 65 74 68 6f 64 0a 20 20 2a 2a 20 20 20 20 20  method.  **     
25810 20 68 61 73 20 62 65 65 6e 20 70 72 6f 76 69 64   has been provid
25811 65 64 2c 20 6f 72 0a 20 20 2a 2a 20 20 20 32 29  ed, or.  **   2)
25812 20 49 74 20 69 73 20 61 20 73 79 73 74 65 6d 20   It is a system 
25813 74 61 62 6c 65 20 28 69 2e 65 2e 20 73 71 6c 69  table (i.e. sqli
25814 74 65 5f 6d 61 73 74 65 72 29 2c 20 74 68 69 73  te_master), this
25815 20 63 61 6c 6c 20 69 73 20 6e 6f 74 0a 20 20 2a   call is not.  *
25816 2a 20 20 20 20 20 20 70 61 72 74 20 6f 66 20 61  *      part of a
25817 20 6e 65 73 74 65 64 20 70 61 72 73 65 20 61 6e   nested parse an
25818 64 20 77 72 69 74 61 62 6c 65 5f 73 63 68 65 6d  d writable_schem
25819 61 20 70 72 61 67 6d 61 20 68 61 73 20 6e 6f 74  a pragma has not
2581a 20 0a 20 20 2a 2a 20 20 20 20 20 20 62 65 65 6e   .  **      been
2581b 20 73 70 65 63 69 66 69 65 64 2e 0a 20 20 2a 2a   specified..  **
2581c 0a 20 20 2a 2a 20 49 6e 20 65 69 74 68 65 72 20  .  ** In either 
2581d 63 61 73 65 20 6c 65 61 76 65 20 61 6e 20 65 72  case leave an er
2581e 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70  ror message in p
2581f 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e  Parse and return
25820 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 20 20 2a 2f 0a   non-zero..  */.
25821 20 20 69 66 28 20 28 20 49 73 56 69 72 74 75 61    if( ( IsVirtua
25822 6c 28 70 54 61 62 29 20 0a 20 20 20 20 20 26 26  l(pTab) .     &&
25823 20 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c   sqlite3GetVTabl
25824 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  e(pParse->db, pT
25825 61 62 29 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75  ab)->pMod->pModu
25826 6c 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30 20 29  le->xUpdate==0 )
25827 0a 20 20 20 7c 7c 20 28 20 28 70 54 61 62 2d 3e  .   || ( (pTab->
25828 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 52 65  tabFlags & TF_Re
25829 61 64 6f 6e 6c 79 29 21 3d 30 0a 20 20 20 20 20  adonly)!=0.     
2582a 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e  && (pParse->db->
2582b 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57  flags & SQLITE_W
2582c 72 69 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a 20  riteSchema)==0. 
2582d 20 20 20 20 26 26 20 70 50 61 72 73 65 2d 3e 6e      && pParse->n
2582e 65 73 74 65 64 3d 3d 30 20 29 0a 20 20 29 7b 0a  ested==0 ).  ){.
2582f 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
25830 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
25831 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  le %s may not be
25832 20 6d 6f 64 69 66 69 65 64 22 2c 20 70 54 61 62   modified", pTab
25833 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  ->zName);.    re
25834 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 23 69 66  turn 1;.  }..#if
25835 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
25836 5f 56 49 45 57 0a 20 20 69 66 28 20 21 76 69 65  _VIEW.  if( !vie
25837 77 4f 6b 20 26 26 20 70 54 61 62 2d 3e 70 53 65  wOk && pTab->pSe
25838 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  lect ){.    sqli
25839 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2583a 73 65 2c 22 63 61 6e 6e 6f 74 20 6d 6f 64 69 66  se,"cannot modif
2583b 79 20 25 73 20 62 65 63 61 75 73 65 20 69 74 20  y %s because it 
2583c 69 73 20 61 20 76 69 65 77 22 2c 70 54 61 62 2d  is a view",pTab-
2583d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  >zName);.    ret
2583e 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69  urn 1;.  }.#endi
2583f 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  f.  return 0;.}.
25840 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
25841 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
25842 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
25843 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
25844 29 0a 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74 65  )./*.** Evaluate
25845 20 61 20 76 69 65 77 20 61 6e 64 20 73 74 6f 72   a view and stor
25846 65 20 69 74 73 20 72 65 73 75 6c 74 20 69 6e 20  e its result in 
25847 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  an ephemeral tab
25848 6c 65 2e 20 20 54 68 65 0a 2a 2a 20 70 57 68 65  le.  The.** pWhe
25849 72 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  re argument is a
2584a 6e 20 6f 70 74 69 6f 6e 61 6c 20 57 48 45 52 45  n optional WHERE
2584b 20 63 6c 61 75 73 65 20 74 68 61 74 20 72 65 73   clause that res
2584c 74 72 69 63 74 73 20 74 68 65 0a 2a 2a 20 73 65  tricts the.** se
2584d 74 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65  t of rows in the
2584e 20 76 69 65 77 20 74 68 61 74 20 61 72 65 20 74   view that are t
2584f 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68  o be added to th
25850 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  e ephemeral tabl
25851 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
25852 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
25853 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77  3MaterializeView
25854 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
25855 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  e,       /* Pars
25856 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
25857 20 54 61 62 6c 65 20 2a 70 56 69 65 77 2c 20 20   Table *pView,  
25858 20 20 20 20 20 20 2f 2a 20 56 69 65 77 20 64 65        /* View de
25859 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  finition */.  Ex
2585a 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20  pr *pWhere,     
2585b 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 57     /* Optional W
2585c 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62  HERE clause to b
2585d 65 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e 74  e added */.  int
2585e 20 69 43 75 72 20 20 20 20 20 20 20 20 20 20 20   iCur           
2585f 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
25860 65 72 20 66 6f 72 20 65 70 68 65 6d 65 72 69 61  er for ephemeria
25861 6c 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20  l table */.){.  
25862 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
25863 0a 20 20 53 65 6c 65 63 74 20 2a 70 44 75 70 3b  .  Select *pDup;
25864 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
25865 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
25866 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 53 65  pDup = sqlite3Se
25867 6c 65 63 74 44 75 70 28 64 62 2c 20 70 56 69 65  lectDup(db, pVie
25868 77 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a  w->pSelect, 0);.
25869 20 20 69 66 28 20 70 57 68 65 72 65 20 29 7b 0a    if( pWhere ){.
2586a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 46 72      SrcList *pFr
2586b 6f 6d 3b 0a 20 20 20 20 0a 20 20 20 20 70 57 68  om;.    .    pWh
2586c 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
2586d 72 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c  rDup(db, pWhere,
2586e 20 30 29 3b 0a 20 20 20 20 70 46 72 6f 6d 20 3d   0);.    pFrom =
2586f 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
25870 70 70 65 6e 64 28 64 62 2c 20 30 2c 20 30 2c 20  ppend(db, 0, 0, 
25871 30 29 3b 0a 20 20 20 20 69 66 28 20 70 46 72 6f  0);.    if( pFro
25872 6d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  m ){.      asser
25873 74 28 20 70 46 72 6f 6d 2d 3e 6e 53 72 63 3d 3d  t( pFrom->nSrc==
25874 31 20 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d  1 );.      pFrom
25875 2d 3e 61 5b 30 5d 2e 7a 41 6c 69 61 73 20 3d 20  ->a[0].zAlias = 
25876 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
25877 64 62 2c 20 70 56 69 65 77 2d 3e 7a 4e 61 6d 65  db, pView->zName
25878 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e  );.      pFrom->
25879 61 5b 30 5d 2e 70 53 65 6c 65 63 74 20 3d 20 70  a[0].pSelect = p
2587a 44 75 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72  Dup;.      asser
2587b 74 28 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70  t( pFrom->a[0].p
2587c 4f 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  On==0 );.      a
2587d 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 61 5b  ssert( pFrom->a[
2587e 30 5d 2e 70 55 73 69 6e 67 3d 3d 30 20 29 3b 0a  0].pUsing==0 );.
2587f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
25880 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
25881 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a  lete(db, pDup);.
25882 20 20 20 20 7d 0a 20 20 20 20 70 44 75 70 20 3d      }.    pDup =
25883 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
25884 77 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 72  w(pParse, 0, pFr
25885 6f 6d 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30  om, pWhere, 0, 0
25886 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
25887 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65   }.  sqlite3Sele
25888 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
25889 2c 20 53 52 54 5f 45 70 68 65 6d 54 61 62 2c 20  , SRT_EphemTab, 
2588a 69 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  iCur);.  sqlite3
2588b 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2588c 44 75 70 2c 20 26 64 65 73 74 29 3b 0a 20 20 73  Dup, &dest);.  s
2588d 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2588e 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a  te(db, pDup);.}.
2588f 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
25890 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
25891 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64  IEW) && !defined
25892 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
25893 47 47 45 52 29 20 2a 2f 0a 0a 23 69 66 20 64 65  GGER) */..#if de
25894 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
25895 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54  BLE_UPDATE_DELET
25896 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66  E_LIMIT) && !def
25897 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
25898 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a  _SUBQUERY)./*.**
25899 20 47 65 6e 65 72 61 74 65 20 61 6e 20 65 78 70   Generate an exp
2589a 72 65 73 73 69 6f 6e 20 74 72 65 65 20 74 6f 20  ression tree to 
2589b 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 57 48  implement the WH
2589c 45 52 45 2c 20 4f 52 44 45 52 20 42 59 2c 0a 2a  ERE, ORDER BY,.*
2589d 2a 20 61 6e 64 20 4c 49 4d 49 54 2f 4f 46 46 53  * and LIMIT/OFFS
2589e 45 54 20 70 6f 72 74 69 6f 6e 20 6f 66 20 44 45  ET portion of DE
2589f 4c 45 54 45 20 61 6e 64 20 55 50 44 41 54 45 20  LETE and UPDATE 
258a0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
258a1 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  *     DELETE FRO
258a2 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57 48 45  M table_wxyz WHE
258a3 52 45 20 61 3c 35 20 4f 52 44 45 52 20 42 59 20  RE a<5 ORDER BY 
258a4 61 20 4c 49 4d 49 54 20 31 3b 0a 2a 2a 20 20 20  a LIMIT 1;.**   
258a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
258a6 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f           \______
258a7 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
258a8 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20  ____/.**        
258a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
258aa 20 20 20 20 20 20 20 70 4c 69 6d 69 74 57 68 65         pLimitWhe
258ab 72 65 20 28 70 49 6e 43 6c 61 75 73 65 29 0a 2a  re (pInClause).*
258ac 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
258ad 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 4c 69   Expr *sqlite3Li
258ae 6d 69 74 57 68 65 72 65 28 0a 20 20 50 61 72 73  mitWhere(.  Pars
258af 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
258b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
258b1 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
258b2 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
258b3 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
258b4 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   /* the FROM cla
258b5 75 73 65 20 2d 2d 20 77 68 69 63 68 20 74 61 62  use -- which tab
258b6 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20  les to scan */. 
258b7 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20   Expr *pWhere,  
258b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
258b9 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
258ba 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
258bb 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
258bc 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20  OrderBy,        
258bd 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
258be 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  Y clause.  May b
258bf 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72  e null */.  Expr
258c0 20 2a 70 4c 69 6d 69 74 2c 20 20 20 20 20 20 20   *pLimit,       
258c1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
258c2 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 4d  LIMIT clause.  M
258c3 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20  ay be null */.  
258c4 45 78 70 72 20 2a 70 4f 66 66 73 65 74 2c 20 20  Expr *pOffset,  
258c5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
258c6 54 68 65 20 4f 46 46 53 45 54 20 63 6c 61 75 73  The OFFSET claus
258c7 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
258c8 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 74 6d 74  */.  char *zStmt
258c9 54 79 70 65 20 20 20 20 20 20 20 20 20 20 20 20  Type            
258ca 20 20 2f 2a 20 45 69 74 68 65 72 20 44 45 4c 45    /* Either DELE
258cb 54 45 20 6f 72 20 55 50 44 41 54 45 2e 20 20 46  TE or UPDATE.  F
258cc 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  or error message
258cd 73 2e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20  s. */.){.  Expr 
258ce 2a 70 57 68 65 72 65 52 6f 77 69 64 20 3d 20 4e  *pWhereRowid = N
258cf 55 4c 4c 3b 20 20 20 20 2f 2a 20 57 48 45 52 45  ULL;    /* WHERE
258d0 20 72 6f 77 69 64 20 2e 2e 20 2a 2f 0a 20 20 45   rowid .. */.  E
258d1 78 70 72 20 2a 70 49 6e 43 6c 61 75 73 65 20 3d  xpr *pInClause =
258d2 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20 57   NULL;      /* W
258d3 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20  HERE rowid IN ( 
258d4 73 65 6c 65 63 74 20 29 20 2a 2f 0a 20 20 45 78  select ) */.  Ex
258d5 70 72 20 2a 70 53 65 6c 65 63 74 52 6f 77 69 64  pr *pSelectRowid
258d6 20 3d 20 4e 55 4c 4c 3b 20 20 20 2f 2a 20 53 45   = NULL;   /* SE
258d7 4c 45 43 54 20 72 6f 77 69 64 20 2e 2e 2e 20 2a  LECT rowid ... *
258d8 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
258d9 4c 69 73 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20  List = NULL;    
258da 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6c   /* Expression l
258db 69 73 74 20 63 6f 6e 74 61 6e 69 6e 67 20 6f 6e  ist contaning on
258dc 6c 79 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20  ly pSelectRowid 
258dd 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  */.  SrcList *pS
258de 65 6c 65 63 74 53 72 63 20 3d 20 4e 55 4c 4c 3b  electSrc = NULL;
258df 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f 77 69    /* SELECT rowi
258e0 64 20 46 52 4f 4d 20 78 20 2e 2e 2e 20 28 64 75  d FROM x ... (du
258e1 70 20 6f 66 20 70 53 72 63 29 20 2a 2f 0a 20 20  p of pSrc) */.  
258e2 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20  Select *pSelect 
258e3 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20  = NULL;      /* 
258e4 43 6f 6d 70 6c 65 74 65 20 53 45 4c 45 43 54 20  Complete SELECT 
258e5 74 72 65 65 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68  tree */..  /* Ch
258e6 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 69  eck that there i
258e7 73 6e 27 74 20 61 6e 20 4f 52 44 45 52 20 42 59  sn't an ORDER BY
258e8 20 77 69 74 68 6f 75 74 20 61 20 4c 49 4d 49 54   without a LIMIT
258e9 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20   clause..  */.  
258ea 69 66 28 20 70 4f 72 64 65 72 42 79 20 26 26 20  if( pOrderBy && 
258eb 28 70 4c 69 6d 69 74 20 3d 3d 20 30 29 20 29 20  (pLimit == 0) ) 
258ec 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
258ed 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 4f  orMsg(pParse, "O
258ee 52 44 45 52 20 42 59 20 77 69 74 68 6f 75 74 20  RDER BY without 
258ef 4c 49 4d 49 54 20 6f 6e 20 25 73 22 2c 20 7a 53  LIMIT on %s", zS
258f0 74 6d 74 54 79 70 65 29 3b 0a 20 20 20 20 70 50  tmtType);.    pP
258f1 61 72 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72  arse->parseError
258f2 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6c   = 1;.    goto l
258f3 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
258f4 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  up_2;.  }..  /* 
258f5 57 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20  We only need to 
258f6 67 65 6e 65 72 61 74 65 20 61 20 73 65 6c 65 63  generate a selec
258f7 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 66 20  t expression if 
258f8 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20  there.  ** is a 
258f9 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20 74 65 72  limit/offset ter
258fa 6d 20 74 6f 20 65 6e 66 6f 72 63 65 2e 0a 20 20  m to enforce..  
258fb 2a 2f 0a 20 20 69 66 28 20 70 4c 69 6d 69 74 20  */.  if( pLimit 
258fc 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 2f 2a 20  == 0 ) {.    /* 
258fd 69 66 20 70 4c 69 6d 69 74 20 69 73 20 6e 75 6c  if pLimit is nul
258fe 6c 2c 20 70 4f 66 66 73 65 74 20 77 69 6c 6c 20  l, pOffset will 
258ff 61 6c 77 61 79 73 20 62 65 20 6e 75 6c 6c 20 61  always be null a
25900 73 20 77 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 61  s well. */.    a
25901 73 73 65 72 74 28 20 70 4f 66 66 73 65 74 20 3d  ssert( pOffset =
25902 3d 20 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72  = 0 );.    retur
25903 6e 20 70 57 68 65 72 65 3b 0a 20 20 7d 0a 0a 20  n pWhere;.  }.. 
25904 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
25905 65 6c 65 63 74 20 65 78 70 72 65 73 73 69 6f 6e  elect expression
25906 20 74 72 65 65 20 74 6f 20 65 6e 66 6f 72 63 65   tree to enforce
25907 20 74 68 65 20 6c 69 6d 69 74 2f 6f 66 66 73 65   the limit/offse
25908 74 20 0a 20 20 2a 2a 20 74 65 72 6d 20 66 6f 72  t .  ** term for
25909 20 74 68 65 20 44 45 4c 45 54 45 20 6f 72 20 55   the DELETE or U
2590a 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
2590b 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20    For example:. 
2590c 20 2a 2a 20 20 20 44 45 4c 45 54 45 20 46 52 4f   **   DELETE FRO
2590d 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20  M table_a WHERE 
2590e 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20 42 59 20  col1=1 ORDER BY 
2590f 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20 4f 46 46  col2 LIMIT 1 OFF
25910 53 45 54 20 31 0a 20 20 2a 2a 20 62 65 63 6f 6d  SET 1.  ** becom
25911 65 73 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54  es:.  **   DELET
25912 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57  E FROM table_a W
25913 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20  HERE rowid IN ( 
25914 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  .  **     SELECT
25915 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 61 62 6c   rowid FROM tabl
25916 65 5f 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31  e_a WHERE col1=1
25917 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c   ORDER BY col2 L
25918 49 4d 49 54 20 31 20 4f 46 46 53 45 54 20 31 0a  IMIT 1 OFFSET 1.
25919 20 20 2a 2a 20 20 20 29 3b 0a 20 20 2a 2f 0a 0a    **   );.  */..
2591a 20 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d    pSelectRowid =
2591b 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2591c 61 72 73 65 2c 20 54 4b 5f 52 4f 57 2c 20 30 2c  arse, TK_ROW, 0,
2591d 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53   0, 0);.  if( pS
2591e 65 6c 65 63 74 52 6f 77 69 64 20 3d 3d 20 30 20  electRowid == 0 
2591f 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65  ) goto limit_whe
25920 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20  re_cleanup_2;.  
25921 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  pEList = sqlite3
25922 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
25923 50 61 72 73 65 2c 20 30 2c 20 70 53 65 6c 65 63  Parse, 0, pSelec
25924 74 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 70  tRowid);.  if( p
25925 45 4c 69 73 74 20 3d 3d 20 30 20 29 20 67 6f 74  EList == 0 ) got
25926 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c  o limit_where_cl
25927 65 61 6e 75 70 5f 32 3b 0a 0a 20 20 2f 2a 20 64  eanup_2;..  /* d
25928 75 70 6c 69 63 61 74 65 20 74 68 65 20 46 52 4f  uplicate the FRO
25929 4d 20 63 6c 61 75 73 65 20 61 73 20 69 74 20 69  M clause as it i
2592a 73 20 6e 65 65 64 65 64 20 62 79 20 62 6f 74 68  s needed by both
2592b 20 74 68 65 20 44 45 4c 45 54 45 2f 55 50 44 41   the DELETE/UPDA
2592c 54 45 20 74 72 65 65 0a 20 20 2a 2a 20 61 6e 64  TE tree.  ** and
2592d 20 74 68 65 20 53 45 4c 45 43 54 20 73 75 62 74   the SELECT subt
2592e 72 65 65 2e 20 2a 2f 0a 20 20 70 53 65 6c 65 63  ree. */.  pSelec
2592f 74 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72  tSrc = sqlite3Sr
25930 63 4c 69 73 74 44 75 70 28 70 50 61 72 73 65 2d  cListDup(pParse-
25931 3e 64 62 2c 20 70 53 72 63 2c 20 30 29 3b 0a 20  >db, pSrc, 0);. 
25932 20 69 66 28 20 70 53 65 6c 65 63 74 53 72 63 20   if( pSelectSrc 
25933 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 73 71 6c  == 0 ) {.    sql
25934 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
25935 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
25936 45 4c 69 73 74 29 3b 0a 20 20 20 20 67 6f 74 6f  EList);.    goto
25937 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65   limit_where_cle
25938 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20 2f  anup_2;.  }..  /
25939 2a 20 67 65 6e 65 72 61 74 65 20 74 68 65 20 53  * generate the S
2593a 45 4c 45 43 54 20 65 78 70 72 65 73 73 69 6f 6e  ELECT expression
2593b 20 74 72 65 65 2e 20 2a 2f 0a 20 20 70 53 65 6c   tree. */.  pSel
2593c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
2593d 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 70 45  ectNew(pParse,pE
2593e 4c 69 73 74 2c 70 53 65 6c 65 63 74 53 72 63 2c  List,pSelectSrc,
2593f 70 57 68 65 72 65 2c 30 2c 30 2c 0a 20 20 20 20  pWhere,0,0,.    
25940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25941 20 20 20 20 20 20 20 20 20 70 4f 72 64 65 72 42           pOrderB
25942 79 2c 30 2c 70 4c 69 6d 69 74 2c 70 4f 66 66 73  y,0,pLimit,pOffs
25943 65 74 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65  et);.  if( pSele
25944 63 74 20 3d 3d 20 30 20 29 20 72 65 74 75 72 6e  ct == 0 ) return
25945 20 30 3b 0a 0a 20 20 2f 2a 20 6e 6f 77 20 67 65   0;..  /* now ge
25946 6e 65 72 61 74 65 20 74 68 65 20 6e 65 77 20 57  nerate the new W
25947 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 63 6c  HERE rowid IN cl
25948 61 75 73 65 20 66 6f 72 20 74 68 65 20 44 45 4c  ause for the DEL
25949 45 54 45 2f 55 44 50 41 54 45 20 2a 2f 0a 20 20  ETE/UDPATE */.  
2594a 70 57 68 65 72 65 52 6f 77 69 64 20 3d 20 73 71  pWhereRowid = sq
2594b 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2594c 65 2c 20 54 4b 5f 52 4f 57 2c 20 30 2c 20 30 2c  e, TK_ROW, 0, 0,
2594d 20 30 29 3b 0a 20 20 69 66 28 20 70 57 68 65 72   0);.  if( pWher
2594e 65 52 6f 77 69 64 20 3d 3d 20 30 20 29 20 67 6f  eRowid == 0 ) go
2594f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63  to limit_where_c
25950 6c 65 61 6e 75 70 5f 31 3b 0a 20 20 70 49 6e 43  leanup_1;.  pInC
25951 6c 61 75 73 65 20 3d 20 73 71 6c 69 74 65 33 50  lause = sqlite3P
25952 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
25953 49 4e 2c 20 70 57 68 65 72 65 52 6f 77 69 64 2c  IN, pWhereRowid,
25954 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 49   0, 0);.  if( pI
25955 6e 43 6c 61 75 73 65 20 3d 3d 20 30 20 29 20 67  nClause == 0 ) g
25956 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f  oto limit_where_
25957 63 6c 65 61 6e 75 70 5f 31 3b 0a 0a 20 20 70 49  cleanup_1;..  pI
25958 6e 43 6c 61 75 73 65 2d 3e 78 2e 70 53 65 6c 65  nClause->x.pSele
25959 63 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20  ct = pSelect;.  
2595a 70 49 6e 43 6c 61 75 73 65 2d 3e 66 6c 61 67 73  pInClause->flags
2595b 20 7c 3d 20 45 50 5f 78 49 73 53 65 6c 65 63 74   |= EP_xIsSelect
2595c 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53  ;.  sqlite3ExprS
2595d 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  etHeight(pParse,
2595e 20 70 49 6e 43 6c 61 75 73 65 29 3b 0a 20 20 72   pInClause);.  r
2595f 65 74 75 72 6e 20 70 49 6e 43 6c 61 75 73 65 3b  eturn pInClause;
25960 0a 0a 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67  ..  /* something
25961 20 77 65 6e 74 20 77 72 6f 6e 67 2e 20 63 6c 65   went wrong. cle
25962 61 6e 20 75 70 20 61 6e 79 74 68 69 6e 67 20 61  an up anything a
25963 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 6c 69 6d  llocated. */.lim
25964 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
25965 5f 31 3a 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  _1:.  sqlite3Sel
25966 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  ectDelete(pParse
25967 2d 3e 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  ->db, pSelect);.
25968 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 6c 69 6d    return 0;..lim
25969 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
2596a 5f 32 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70  _2:.  sqlite3Exp
2596b 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  rDelete(pParse->
2596c 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 73  db, pWhere);.  s
2596d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
2596e 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
2596f 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 73 71   pOrderBy);.  sq
25970 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
25971 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69 6d  pParse->db, pLim
25972 69 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  it);.  sqlite3Ex
25973 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  prDelete(pParse-
25974 3e 64 62 2c 20 70 4f 66 66 73 65 74 29 3b 0a 20  >db, pOffset);. 
25975 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
25976 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
25977 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44  QLITE_ENABLE_UPD
25978 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54  ATE_DELETE_LIMIT
25979 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
2597a 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2597b 52 59 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65  RY) */../*.** Ge
2597c 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2597d 61 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 73 74  a DELETE FROM st
2597e 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  atement..**.**  
2597f 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74     DELETE FROM t
25980 61 62 6c 65 5f 77 78 79 7a 20 57 48 45 52 45 20  able_wxyz WHERE 
25981 61 3c 35 20 41 4e 44 20 62 20 4e 4f 54 20 4e 55  a<5 AND b NOT NU
25982 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  LL;.**          
25983 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f         \________
25984 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f  /       \_______
25985 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20  _________/.**   
25986 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
25987 54 61 62 4c 69 73 74 20 20 20 20 20 20 20 20 20  TabList         
25988 20 20 20 20 20 70 57 68 65 72 65 0a 2a 2f 0a 53       pWhere.*/.S
25989 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2598a 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
2598b 46 72 6f 6d 28 0a 20 20 50 61 72 73 65 20 2a 70  From(.  Parse *p
2598c 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
2598d 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
2598e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73  text */.  SrcLis
2598f 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20  t *pTabList,    
25990 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72   /* The table fr
25991 6f 6d 20 77 68 69 63 68 20 77 65 20 73 68 6f 75  om which we shou
25992 6c 64 20 64 65 6c 65 74 65 20 74 68 69 6e 67 73  ld delete things
25993 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
25994 72 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  re           /* 
25995 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
25996 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a  .  May be null *
25997 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 20  /.){.  Vdbe *v; 
25998 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25999 20 54 68 65 20 76 69 72 74 75 61 6c 20 64 61 74   The virtual dat
2599a 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f 0a  abase engine */.
2599b 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
2599c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2599d 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68  table from which
2599e 20 72 65 63 6f 72 64 73 20 77 69 6c 6c 20 62 65   records will be
2599f 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 63 6f   deleted */.  co
259a0 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  nst char *zDb;  
259a1 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
259a2 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67  database holding
259a3 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 65   pTab */.  int e
259a4 6e 64 2c 20 61 64 64 72 20 3d 20 30 3b 20 20 20  nd, addr = 0;   
259a5 20 20 2f 2a 20 41 20 63 6f 75 70 6c 65 20 61 64    /* A couple ad
259a6 64 72 65 73 73 65 73 20 6f 66 20 67 65 6e 65 72  dresses of gener
259a7 61 74 65 64 20 63 6f 64 65 20 2a 2f 0a 20 20 69  ated code */.  i
259a8 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
259a9 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
259aa 75 6e 74 65 72 20 2a 2f 0a 20 20 57 68 65 72 65  unter */.  Where
259ab 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20  Info *pWInfo;   
259ac 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
259ad 20 61 62 6f 75 74 20 74 68 65 20 57 48 45 52 45   about the WHERE
259ae 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 49 6e 64   clause */.  Ind
259af 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
259b0 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
259b1 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20  ng over indices 
259b2 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
259b3 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20    int iCur;     
259b4 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
259b5 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
259b6 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 73 71 6c  or pTab */.  sql
259b7 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
259b8 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61 74 61      /* Main data
259b9 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a  base structure *
259ba 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20  /.  AuthContext 
259bb 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20 41 75  sContext;  /* Au
259bc 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74  thorization cont
259bd 65 78 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  ext */.  NameCon
259be 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20  text sNC;       
259bf 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
259c0 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65  to resolve expre
259c1 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20 69  ssions in */.  i
259c2 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
259c3 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
259c4 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e  e number */.  in
259c5 74 20 6d 65 6d 43 6e 74 20 3d 20 2d 31 3b 20 20  t memCnt = -1;  
259c6 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
259c7 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 63 68 61  ell used for cha
259c8 6e 67 65 20 63 6f 75 6e 74 69 6e 67 20 2a 2f 0a  nge counting */.
259c9 20 20 69 6e 74 20 72 63 61 75 74 68 3b 20 20 20    int rcauth;   
259ca 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
259cb 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 75  e returned by au
259cc 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c  thorization call
259cd 62 61 63 6b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  back */..#ifndef
259ce 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
259cf 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65  GGER.  int isVie
259d0 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
259d1 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
259d2 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 6c  ttempting to del
259d3 65 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77 20  ete from a view 
259d4 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  */.  Trigger *pT
259d5 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 20 20  rigger;         
259d6 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62    /* List of tab
259d7 6c 65 20 74 72 69 67 67 65 72 73 2c 20 69 66 20  le triggers, if 
259d8 72 65 71 75 69 72 65 64 20 2a 2f 0a 23 65 6e 64  required */.#end
259d9 69 66 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 43  if..  memset(&sC
259da 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f  ontext, 0, sizeo
259db 66 28 73 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20  f(sContext));.  
259dc 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
259dd 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
259de 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
259df 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
259e0 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
259e1 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61  cleanup;.  }.  a
259e2 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d  ssert( pTabList-
259e3 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f  >nSrc==1 );..  /
259e4 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62  * Locate the tab
259e5 6c 65 20 77 68 69 63 68 20 77 65 20 77 61 6e 74  le which we want
259e6 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 54 68 69   to delete.  Thi
259e7 73 20 74 61 62 6c 65 20 68 61 73 20 74 6f 20 62  s table has to b
259e8 65 0a 20 20 2a 2a 20 70 75 74 20 69 6e 20 61 6e  e.  ** put in an
259e9 20 53 72 63 4c 69 73 74 20 73 74 72 75 63 74 75   SrcList structu
259ea 72 65 20 62 65 63 61 75 73 65 20 73 6f 6d 65 20  re because some 
259eb 6f 66 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  of the subroutin
259ec 65 73 20 77 65 0a 20 20 2a 2a 20 77 69 6c 6c 20  es we.  ** will 
259ed 62 65 20 63 61 6c 6c 69 6e 67 20 61 72 65 20 64  be calling are d
259ee 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20  esigned to work 
259ef 77 69 74 68 20 6d 75 6c 74 69 70 6c 65 20 74 61  with multiple ta
259f0 62 6c 65 73 20 61 6e 64 20 65 78 70 65 63 74 0a  bles and expect.
259f1 20 20 2a 2a 20 61 6e 20 53 72 63 4c 69 73 74 2a    ** an SrcList*
259f2 20 70 61 72 61 6d 65 74 65 72 20 69 6e 73 74 65   parameter inste
259f3 61 64 20 6f 66 20 6a 75 73 74 20 61 20 54 61 62  ad of just a Tab
259f4 6c 65 2a 20 70 61 72 61 6d 65 74 65 72 2e 0a 20  le* parameter.. 
259f5 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   */.  pTab = sql
259f6 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
259f7 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  p(pParse, pTabLi
259f8 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d  st);.  if( pTab=
259f9 3d 30 20 29 20 20 67 6f 74 6f 20 64 65 6c 65 74  =0 )  goto delet
259fa 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a  e_from_cleanup;.
259fb 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
259fc 20 69 66 20 77 65 20 68 61 76 65 20 61 6e 79 20   if we have any 
259fd 74 72 69 67 67 65 72 73 20 61 6e 64 20 69 66 20  triggers and if 
259fe 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 0a  the table being.
259ff 20 20 2a 2a 20 64 65 6c 65 74 65 64 20 66 72 6f    ** deleted fro
25a00 6d 20 69 73 20 61 20 76 69 65 77 0a 20 20 2a 2f  m is a view.  */
25a01 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
25a02 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 70  OMIT_TRIGGER.  p
25a03 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65  Trigger = sqlite
25a04 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70  3TriggersExist(p
25a05 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f  Parse, pTab, TK_
25a06 44 45 4c 45 54 45 2c 20 30 2c 20 30 29 3b 0a 20  DELETE, 0, 0);. 
25a07 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e   isView = pTab->
25a08 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73  pSelect!=0;.#els
25a09 65 0a 23 20 64 65 66 69 6e 65 20 70 54 72 69 67  e.# define pTrig
25a0a 67 65 72 20 30 0a 23 20 64 65 66 69 6e 65 20 69  ger 0.# define i
25a0b 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23  sView 0.#endif.#
25a0c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
25a0d 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69  T_VIEW.# undef i
25a0e 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69  sView.# define i
25a0f 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a  sView 0.#endif..
25a10 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20    /* If pTab is 
25a11 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d  really a view, m
25a12 61 6b 65 20 73 75 72 65 20 69 74 20 68 61 73 20  ake sure it has 
25a13 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
25a14 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
25a15 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
25a16 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
25a17 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Tab) ){.    goto
25a18 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
25a19 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  anup;.  }..  if(
25a1a 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e   sqlite3IsReadOn
25a1b 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ly(pParse, pTab,
25a1c 20 28 70 54 72 69 67 67 65 72 3f 31 3a 30 29 29   (pTrigger?1:0))
25a1d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
25a1e 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
25a1f 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71  ;.  }.  iDb = sq
25a20 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
25a21 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  ex(db, pTab->pSc
25a22 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28  hema);.  assert(
25a23 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
25a24 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b    zDb = db->aDb[
25a25 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63  iDb].zName;.  rc
25a26 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75  auth = sqlite3Au
25a27 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
25a28 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 70  SQLITE_DELETE, p
25a29 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a  Tab->zName, 0, z
25a2a 44 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  Db);.  assert( r
25a2b 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b  cauth==SQLITE_OK
25a2c 20 7c 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49   || rcauth==SQLI
25a2d 54 45 5f 44 45 4e 59 20 7c 7c 20 72 63 61 75 74  TE_DENY || rcaut
25a2e 68 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  h==SQLITE_IGNORE
25a2f 20 29 3b 0a 20 20 69 66 28 20 72 63 61 75 74 68   );.  if( rcauth
25a30 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b  ==SQLITE_DENY ){
25a31 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
25a32 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
25a33 20 7d 0a 20 20 61 73 73 65 72 74 28 21 69 73 56   }.  assert(!isV
25a34 69 65 77 20 7c 7c 20 70 54 72 69 67 67 65 72 29  iew || pTrigger)
25a35 3b 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 20  ;..  /* Assign  
25a36 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f  cursor number to
25a37 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 61   the table and a
25a38 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 2e 0a  ll its indices..
25a39 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
25a3a 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  TabList->nSrc==1
25a3b 20 29 3b 0a 20 20 69 43 75 72 20 3d 20 70 54 61   );.  iCur = pTa
25a3c 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72  bList->a[0].iCur
25a3d 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  sor = pParse->nT
25a3e 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78  ab++;.  for(pIdx
25a3f 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
25a40 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
25a41 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 50 61 72  pNext){.    pPar
25a42 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a  se->nTab++;.  }.
25a43 0a 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20  .  /* Start the 
25a44 76 69 65 77 20 63 6f 6e 74 65 78 74 0a 20 20 2a  view context.  *
25a45 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29  /.  if( isView )
25a46 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74  {.    sqlite3Aut
25a47 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61  hContextPush(pPa
25a48 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20  rse, &sContext, 
25a49 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
25a4a 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65  }..  /* Begin ge
25a4b 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20  nerating code.. 
25a4c 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
25a4d 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
25a4e 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a  ;.  if( v==0 ){.
25a4f 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f      goto delete_
25a50 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  from_cleanup;.  
25a51 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  }.  if( pParse->
25a52 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69  nested==0 ) sqli
25a53 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e  te3VdbeCountChan
25a54 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65  ges(v);.  sqlite
25a55 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
25a56 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20  tion(pParse, 1, 
25a57 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77  iDb);..  /* If w
25a58 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20  e are trying to 
25a59 64 65 6c 65 74 65 20 66 72 6f 6d 20 61 20 76 69  delete from a vi
25a5a 65 77 2c 20 72 65 61 6c 69 7a 65 20 74 68 61 74  ew, realize that
25a5b 20 76 69 65 77 20 69 6e 74 6f 0a 20 20 2a 2a 20   view into.  ** 
25a5c 61 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  a ephemeral tabl
25a5d 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66  e..  */.#if !def
25a5e 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
25a5f 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e  _VIEW) && !defin
25a60 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
25a61 52 49 47 47 45 52 29 0a 20 20 69 66 28 20 69 73  RIGGER).  if( is
25a62 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69  View ){.    sqli
25a63 74 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69  te3MaterializeVi
25a64 65 77 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ew(pParse, pTab,
25a65 20 70 57 68 65 72 65 2c 20 69 43 75 72 29 3b 0a   pWhere, iCur);.
25a66 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
25a67 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c   Resolve the col
25a68 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 74 68 65  umn names in the
25a69 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20   WHERE clause.. 
25a6a 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e   */.  memset(&sN
25a6b 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
25a6c 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65  ));.  sNC.pParse
25a6d 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 4e 43   = pParse;.  sNC
25a6e 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62  .pSrcList = pTab
25a6f 4c 69 73 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  List;.  if( sqli
25a70 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
25a71 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65 72 65  mes(&sNC, pWhere
25a72 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65  ) ){.    goto de
25a73 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75  lete_from_cleanu
25a74 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69  p;.  }..  /* Ini
25a75 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e  tialize the coun
25a76 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ter of the numbe
25a77 72 20 6f 66 20 72 6f 77 73 20 64 65 6c 65 74 65  r of rows delete
25a78 64 2c 20 69 66 0a 20 20 2a 2a 20 77 65 20 61 72  d, if.  ** we ar
25a79 65 20 63 6f 75 6e 74 69 6e 67 20 72 6f 77 73 2e  e counting rows.
25a7a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
25a7b 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
25a7c 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20  ountRows ){.    
25a7d 6d 65 6d 43 6e 74 20 3d 20 2b 2b 70 50 61 72 73  memCnt = ++pPars
25a7e 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
25a7f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25a80 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
25a81 20 6d 65 6d 43 6e 74 29 3b 0a 20 20 7d 0a 0a 23   memCnt);.  }..#
25a82 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
25a83 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49  IT_TRUNCATE_OPTI
25a84 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 53 70  MIZATION.  /* Sp
25a85 65 63 69 61 6c 20 63 61 73 65 3a 20 41 20 44 45  ecial case: A DE
25a86 4c 45 54 45 20 77 69 74 68 6f 75 74 20 61 20 57  LETE without a W
25a87 48 45 52 45 20 63 6c 61 75 73 65 20 64 65 6c 65  HERE clause dele
25a88 74 65 73 20 65 76 65 72 79 74 68 69 6e 67 2e 0a  tes everything..
25a89 20 20 2a 2a 20 49 74 20 69 73 20 65 61 73 69 65    ** It is easie
25a8a 72 20 6a 75 73 74 20 74 6f 20 65 72 61 73 65 20  r just to erase 
25a8b 74 68 65 20 77 68 6f 6c 65 20 74 61 62 6c 65 2e  the whole table.
25a8c 20 50 72 69 6f 72 20 74 6f 20 76 65 72 73 69 6f   Prior to versio
25a8d 6e 20 33 2e 36 2e 35 2c 0a 20 20 2a 2a 20 74 68  n 3.6.5,.  ** th
25a8e 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  is optimization 
25a8f 63 61 75 73 65 64 20 74 68 65 20 72 6f 77 20 63  caused the row c
25a90 68 61 6e 67 65 20 63 6f 75 6e 74 20 28 74 68 65  hange count (the
25a91 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
25a92 62 79 20 0a 20 20 2a 2a 20 41 50 49 20 66 75 6e  by .  ** API fun
25a93 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 5f 63 6f  ction sqlite3_co
25a94 75 6e 74 5f 63 68 61 6e 67 65 73 29 20 74 6f 20  unt_changes) to 
25a95 62 65 20 73 65 74 20 69 6e 63 6f 72 72 65 63 74  be set incorrect
25a96 6c 79 2e 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  ly.  */.  if( rc
25a97 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  auth==SQLITE_OK 
25a98 26 26 20 70 57 68 65 72 65 3d 3d 30 20 26 26 20  && pWhere==0 && 
25a99 21 70 54 72 69 67 67 65 72 20 26 26 20 21 49 73  !pTrigger && !Is
25a9a 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 0a 20  Virtual(pTab) . 
25a9b 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 46    && 0==sqlite3F
25a9c 6b 52 65 71 75 69 72 65 64 28 70 50 61 72 73 65  kRequired(pParse
25a9d 2c 20 70 54 61 62 2c 20 30 2c 20 30 29 0a 20 20  , pTab, 0, 0).  
25a9e 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  ){.    assert( !
25a9f 69 73 56 69 65 77 20 29 3b 0a 20 20 20 20 73 71  isView );.    sq
25aa0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
25aa1 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 54 61  v, OP_Clear, pTa
25aa2 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 20 6d 65  b->tnum, iDb, me
25aa3 6d 43 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20  mCnt,.          
25aa4 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
25aa5 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54  ->zName, P4_STAT
25aa6 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 70 49 64  IC);.    for(pId
25aa7 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
25aa8 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
25aa9 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61  >pNext){.      a
25aaa 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53 63  ssert( pIdx->pSc
25aab 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
25aac 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ema );.      sql
25aad 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25aae 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 49 64 78  , OP_Clear, pIdx
25aaf 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20  ->tnum, iDb);.  
25ab0 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64    }.  }else.#end
25ab1 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
25ab2 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d  T_TRUNCATE_OPTIM
25ab3 49 5a 41 54 49 4f 4e 20 2a 2f 0a 20 20 2f 2a 20  IZATION */.  /* 
25ab4 54 68 65 20 75 73 75 61 6c 20 63 61 73 65 3a 20  The usual case: 
25ab5 54 68 65 72 65 20 69 73 20 61 20 57 48 45 52 45  There is a WHERE
25ab6 20 63 6c 61 75 73 65 20 73 6f 20 77 65 20 68 61   clause so we ha
25ab7 76 65 20 74 6f 20 73 63 61 6e 20 74 68 72 6f 75  ve to scan throu
25ab8 67 68 0a 20 20 2a 2a 20 74 68 65 20 74 61 62 6c  gh.  ** the tabl
25ab9 65 20 61 6e 64 20 70 69 63 6b 20 77 68 69 63 68  e and pick which
25aba 20 72 65 63 6f 72 64 73 20 74 6f 20 64 65 6c 65   records to dele
25abb 74 65 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20  te..  */.  {.   
25abc 20 69 6e 74 20 69 52 6f 77 53 65 74 20 3d 20 2b   int iRowSet = +
25abd 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20  +pParse->nMem;  
25abe 20 2f 2a 20 52 65 67 69 73 74 65 72 20 66 6f 72   /* Register for
25abf 20 72 6f 77 73 65 74 20 6f 66 20 72 6f 77 73 20   rowset of rows 
25ac0 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20  to delete */.   
25ac1 20 69 6e 74 20 69 52 6f 77 69 64 20 3d 20 2b 2b   int iRowid = ++
25ac2 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20  pParse->nMem;   
25ac3 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 74 6f   /* Used for sto
25ac4 72 69 6e 67 20 72 6f 77 69 64 20 76 61 6c 75 65  ring rowid value
25ac5 73 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65  s. */.    int re
25ac6 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  gRowid;         
25ac7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 74            /* Act
25ac8 75 61 6c 20 72 65 67 69 73 74 65 72 20 63 6f 6e  ual register con
25ac9 74 61 69 6e 69 6e 67 20 72 6f 77 69 64 73 20 2a  taining rowids *
25aca 2f 0a 0a 20 20 20 20 2f 2a 20 43 6f 6c 6c 65 63  /..    /* Collec
25acb 74 20 72 6f 77 69 64 73 20 6f 66 20 65 76 65 72  t rowids of ever
25acc 79 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65  y row to be dele
25acd 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ted..    */.    
25ace 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25acf 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
25ad0 20 69 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 70   iRowSet);.    p
25ad1 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57  WInfo = sqlite3W
25ad2 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65  hereBegin(pParse
25ad3 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65  , pTabList, pWhe
25ad4 72 65 2c 30 2c 57 48 45 52 45 5f 44 55 50 4c 49  re,0,WHERE_DUPLI
25ad5 43 41 54 45 53 5f 4f 4b 29 3b 0a 20 20 20 20 69  CATES_OK);.    i
25ad6 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67  f( pWInfo==0 ) g
25ad7 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
25ad8 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 72 65 67  cleanup;.    reg
25ad9 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 45  Rowid = sqlite3E
25ada 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e  xprCodeGetColumn
25adb 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 2d  (pParse, pTab, -
25adc 31 2c 20 69 43 75 72 2c 20 69 52 6f 77 69 64 2c  1, iCur, iRowid,
25add 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
25ade 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25adf 5f 52 6f 77 53 65 74 41 64 64 2c 20 69 52 6f 77  _RowSetAdd, iRow
25ae0 53 65 74 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  Set, regRowid);.
25ae1 20 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67      if( db->flag
25ae2 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74  s & SQLITE_Count
25ae3 52 6f 77 73 20 29 7b 0a 20 20 20 20 20 20 73 71  Rows ){.      sq
25ae4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25ae5 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 6d 65  v, OP_AddImm, me
25ae6 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20 20 7d 0a  mCnt, 1);.    }.
25ae7 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65      sqlite3Where
25ae8 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 0a 20 20  End(pWInfo);..  
25ae9 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 76 65 72    /* Delete ever
25aea 79 20 69 74 65 6d 20 77 68 6f 73 65 20 6b 65 79  y item whose key
25aeb 20 77 61 73 20 77 72 69 74 74 65 6e 20 74 6f 20   was written to 
25aec 74 68 65 20 6c 69 73 74 20 64 75 72 69 6e 67 20  the list during 
25aed 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  the.    ** datab
25aee 61 73 65 20 73 63 61 6e 2e 20 20 57 65 20 68 61  ase scan.  We ha
25aef 76 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 65  ve to delete ite
25af0 6d 73 20 61 66 74 65 72 20 74 68 65 20 73 63 61  ms after the sca
25af1 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 0a 20 20  n is complete.  
25af2 20 20 2a 2a 20 62 65 63 61 75 73 65 20 64 65 6c    ** because del
25af3 65 74 69 6e 67 20 61 6e 20 69 74 65 6d 20 63 61  eting an item ca
25af4 6e 20 63 68 61 6e 67 65 20 74 68 65 20 73 63 61  n change the sca
25af5 6e 20 6f 72 64 65 72 2e 20 20 2a 2f 0a 20 20 20  n order.  */.   
25af6 20 65 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64   end = sqlite3Vd
25af7 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
25af8 0a 20 20 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74  .    /* Unless t
25af9 68 69 73 20 69 73 20 61 20 76 69 65 77 2c 20 6f  his is a view, o
25afa 70 65 6e 20 63 75 72 73 6f 72 73 20 66 6f 72 20  pen cursors for 
25afb 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65  the table we are
25afc 20 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 69 6e   .    ** deletin
25afd 67 20 66 72 6f 6d 20 61 6e 64 20 61 6c 6c 20 69  g from and all i
25afe 74 73 20 69 6e 64 69 63 65 73 2e 20 49 66 20 74  ts indices. If t
25aff 68 69 73 20 69 73 20 61 20 76 69 65 77 2c 20 74  his is a view, t
25b00 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f  hen the.    ** o
25b01 6e 6c 79 20 65 66 66 65 63 74 20 74 68 69 73 20  nly effect this 
25b02 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 69 73  statement has is
25b03 20 74 6f 20 66 69 72 65 20 74 68 65 20 49 4e 53   to fire the INS
25b04 54 45 41 44 20 4f 46 20 0a 20 20 20 20 2a 2a 20  TEAD OF .    ** 
25b05 74 72 69 67 67 65 72 73 2e 20 20 2a 2f 0a 20 20  triggers.  */.  
25b06 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
25b07 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70  .      sqlite3Op
25b08 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65  enTableAndIndice
25b09 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  s(pParse, pTab, 
25b0a 69 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  iCur, OP_OpenWri
25b0b 74 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  te);.    }..    
25b0c 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
25b0d 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52  beAddOp3(v, OP_R
25b0e 6f 77 53 65 74 52 65 61 64 2c 20 69 52 6f 77 53  owSetRead, iRowS
25b0f 65 74 2c 20 65 6e 64 2c 20 69 52 6f 77 69 64 29  et, end, iRowid)
25b10 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  ;..    /* Delete
25b11 20 74 68 65 20 72 6f 77 20 2a 2f 0a 23 69 66 6e   the row */.#ifn
25b12 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
25b13 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
25b14 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
25b15 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 63 6f  Tab) ){.      co
25b16 6e 73 74 20 63 68 61 72 20 2a 70 56 54 61 62 20  nst char *pVTab 
25b17 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
25b18 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65  sqlite3GetVTable
25b19 28 64 62 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  (db, pTab);.    
25b1a 20 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b    sqlite3VtabMak
25b1b 65 57 72 69 74 61 62 6c 65 28 70 50 61 72 73 65  eWritable(pParse
25b1c 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 73  , pTab);.      s
25b1d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
25b1e 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20  (v, OP_VUpdate, 
25b1f 30 2c 20 31 2c 20 69 52 6f 77 69 64 2c 20 70 56  0, 1, iRowid, pV
25b20 54 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  Tab, P4_VTAB);. 
25b21 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41       sqlite3MayA
25b22 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20  bort(pParse);.  
25b23 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
25b24 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 63     {.      int c
25b25 6f 75 6e 74 20 3d 20 28 70 50 61 72 73 65 2d 3e  ount = (pParse->
25b26 6e 65 73 74 65 64 3d 3d 30 29 3b 20 20 20 20 2f  nested==0);    /
25b27 2a 20 54 72 75 65 20 74 6f 20 63 6f 75 6e 74 20  * True to count 
25b28 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 20 20 20  changes */.     
25b29 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
25b2a 52 6f 77 44 65 6c 65 74 65 28 70 50 61 72 73 65  RowDelete(pParse
25b2b 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 69 52  , pTab, iCur, iR
25b2c 6f 77 69 64 2c 20 63 6f 75 6e 74 2c 20 70 54 72  owid, count, pTr
25b2d 69 67 67 65 72 2c 20 4f 45 5f 44 65 66 61 75 6c  igger, OE_Defaul
25b2e 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  t);.    }..    /
25b2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 64 65 6c  * End of the del
25b30 65 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20  ete loop */.    
25b31 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25b32 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
25b33 20 61 64 64 72 29 3b 0a 20 20 20 20 73 71 6c 69   addr);.    sqli
25b34 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
25b35 62 65 6c 28 76 2c 20 65 6e 64 29 3b 0a 0a 20 20  bel(v, end);..  
25b36 20 20 2f 2a 20 43 6c 6f 73 65 20 74 68 65 20 63    /* Close the c
25b37 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74  ursors open on t
25b38 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 74 73  he table and its
25b39 20 69 6e 64 65 78 65 73 2e 20 2a 2f 0a 20 20 20   indexes. */.   
25b3a 20 69 66 28 20 21 69 73 56 69 65 77 20 26 26 20   if( !isView && 
25b3b 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  !IsVirtual(pTab)
25b3c 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d   ){.      for(i=
25b3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  1, pIdx=pTab->pI
25b3e 6e 64 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c  ndex; pIdx; i++,
25b3f 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
25b40 74 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t){.        sqli
25b41 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
25b42 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 20   OP_Close, iCur 
25b43 2b 20 69 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 29  + i, pIdx->tnum)
25b44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
25b45 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25b46 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  1(v, OP_Close, i
25b47 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Cur);.    }.  }.
25b48 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65  .  /* Update the
25b49 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
25b4a 20 74 61 62 6c 65 20 62 79 20 73 74 6f 72 69 6e   table by storin
25b4b 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  g the content of
25b4c 20 74 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d 75   the.  ** maximu
25b4d 6d 20 72 6f 77 69 64 20 63 6f 75 6e 74 65 72 20  m rowid counter 
25b4e 76 61 6c 75 65 73 20 72 65 63 6f 72 64 65 64 20  values recorded 
25b4f 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20  while inserting 
25b50 69 6e 74 6f 0a 20 20 2a 2a 20 61 75 74 6f 69 6e  into.  ** autoin
25b51 63 72 65 6d 65 6e 74 20 74 61 62 6c 65 73 2e 0a  crement tables..
25b52 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73    */.  if( pPars
25b53 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26 20  e->nested==0 && 
25b54 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
25b55 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  Tab==0 ){.    sq
25b56 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65  lite3Autoincreme
25b57 6e 74 45 6e 64 28 70 50 61 72 73 65 29 3b 0a 20  ntEnd(pParse);. 
25b58 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20   }..  /* Return 
25b59 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
25b5a 77 73 20 74 68 61 74 20 77 65 72 65 20 64 65 6c  ws that were del
25b5b 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f  eted. If this ro
25b5c 75 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67  utine is .  ** g
25b5d 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62  enerating code b
25b5e 65 63 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c  ecause of a call
25b5f 20 74 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65   to sqlite3Neste
25b60 64 50 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74  dParse(), do not
25b61 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65  .  ** invoke the
25b62 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
25b63 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  on..  */.  if( (
25b64 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
25b65 5f 43 6f 75 6e 74 52 6f 77 73 29 20 26 26 20 21  _CountRows) && !
25b66 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26  pParse->nested &
25b67 26 20 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67  & !pParse->pTrig
25b68 67 65 72 54 61 62 20 29 7b 0a 20 20 20 20 73 71  gerTab ){.    sq
25b69 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25b6a 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
25b6b 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20   memCnt, 1);.   
25b6c 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
25b6d 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
25b6e 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
25b6f 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
25b70 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77  LNAME_NAME, "row
25b71 73 20 64 65 6c 65 74 65 64 22 2c 20 53 51 4c 49  s deleted", SQLI
25b72 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a  TE_STATIC);.  }.
25b73 0a 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65  .delete_from_cle
25b74 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 41  anup:.  sqlite3A
25b75 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26 73  uthContextPop(&s
25b76 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69  Context);.  sqli
25b77 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
25b78 28 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a  (db, pTabList);.
25b79 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
25b7a 65 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b  ete(db, pWhere);
25b7b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 2f 2a 20  .  return;.}./* 
25b7c 4d 61 6b 65 20 73 75 72 65 20 22 69 73 56 69 65  Make sure "isVie
25b7d 77 22 20 61 6e 64 20 6f 74 68 65 72 20 6d 61 63  w" and other mac
25b7e 72 6f 73 20 64 65 66 69 6e 65 64 20 61 62 6f 76  ros defined abov
25b7f 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e  e are undefined.
25b80 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 74 68   Otherwise.** th
25b81 65 6c 79 20 6d 61 79 20 69 6e 74 65 72 66 65 72  ely may interfer
25b82 65 20 77 69 74 68 20 63 6f 6d 70 69 6c 61 74 69  e with compilati
25b83 6f 6e 20 6f 66 20 6f 74 68 65 72 20 66 75 6e 63  on of other func
25b84 74 69 6f 6e 73 20 69 6e 20 74 68 69 73 20 66 69  tions in this fi
25b85 6c 65 0a 2a 2a 20 28 6f 72 20 69 6e 20 61 6e 6f  le.** (or in ano
25b86 74 68 65 72 20 66 69 6c 65 2c 20 69 66 20 74 68  ther file, if th
25b87 69 73 20 66 69 6c 65 20 62 65 63 6f 6d 65 73 20  is file becomes 
25b88 70 61 72 74 20 6f 66 20 74 68 65 20 61 6d 61 6c  part of the amal
25b89 67 61 6d 61 74 69 6f 6e 29 2e 20 20 2a 2f 0a 23  gamation).  */.#
25b8a 69 66 64 65 66 20 69 73 56 69 65 77 0a 20 23 75  ifdef isView. #u
25b8b 6e 64 65 66 20 69 73 56 69 65 77 0a 23 65 6e 64  ndef isView.#end
25b8c 69 66 0a 23 69 66 64 65 66 20 70 54 72 69 67 67  if.#ifdef pTrigg
25b8d 65 72 0a 20 23 75 6e 64 65 66 20 70 54 72 69 67  er. #undef pTrig
25b8e 67 65 72 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ger.#endif../*.*
25b8f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67  * This routine g
25b90 65 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f  enerates VDBE co
25b91 64 65 20 74 68 61 74 20 63 61 75 73 65 73 20 61  de that causes a
25b92 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61   single row of a
25b93 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  .** single table
25b94 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 0a   to be deleted..
25b95 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6d  **.** The VDBE m
25b96 75 73 74 20 62 65 20 69 6e 20 61 20 70 61 72 74  ust be in a part
25b97 69 63 75 6c 61 72 20 73 74 61 74 65 20 77 68 65  icular state whe
25b98 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
25b99 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65  s called..** The
25b9a 73 65 20 61 72 65 20 74 68 65 20 72 65 71 75 69  se are the requi
25b9b 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  rements:.**.**  
25b9c 20 31 2e 20 20 41 20 72 65 61 64 2f 77 72 69 74   1.  A read/writ
25b9d 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  e cursor pointin
25b9e 67 20 74 6f 20 70 54 61 62 2c 20 74 68 65 20 74  g to pTab, the t
25b9f 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
25ba0 74 68 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20  the row.**      
25ba1 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2c 20   to be deleted, 
25ba2 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 61  must be opened a
25ba3 73 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  s cursor number 
25ba4 24 69 43 75 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 32  $iCur..**.**   2
25ba5 2e 20 20 52 65 61 64 2f 77 72 69 74 65 20 63 75  .  Read/write cu
25ba6 72 73 6f 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e  rsors for all in
25ba7 64 69 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75  dices of pTab mu
25ba8 73 74 20 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a  st be open as.**
25ba9 20 20 20 20 20 20 20 63 75 72 73 6f 72 20 6e 75         cursor nu
25baa 6d 62 65 72 20 62 61 73 65 2b 69 20 66 6f 72 20  mber base+i for 
25bab 74 68 65 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a  the i-th index..
25bac 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 54 68 65 20  **.**   3.  The 
25bad 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66  record number of
25bae 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64   the row to be d
25baf 65 6c 65 74 65 64 20 6d 75 73 74 20 62 65 20 73  eleted must be s
25bb0 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 20 20 20 20  tored in.**     
25bb1 20 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 52    memory cell iR
25bb2 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  owid..**.** This
25bb3 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
25bb4 65 73 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76  es code to remov
25bb5 65 20 62 6f 74 68 20 74 68 65 20 74 61 62 6c 65  e both the table
25bb6 20 72 65 63 6f 72 64 20 61 6e 64 20 61 6c 6c 20   record and all 
25bb7 0a 2a 2a 20 69 6e 64 65 78 20 65 6e 74 72 69 65  .** index entrie
25bb8 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20  s that point to 
25bb9 74 68 61 74 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a  that record..*/.
25bba 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
25bbb 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72  oid sqlite3Gener
25bbc 61 74 65 52 6f 77 44 65 6c 65 74 65 28 0a 20 20  ateRowDelete(.  
25bbd 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
25bbe 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
25bbf 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
25bc0 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a   *pTab,       /*
25bc1 20 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   Table containin
25bc2 67 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20  g the row to be 
25bc3 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74  deleted */.  int
25bc4 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20   iCur,          
25bc5 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
25bc6 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a   for the table *
25bc7 2f 0a 20 20 69 6e 74 20 69 52 6f 77 69 64 2c 20  /.  int iRowid, 
25bc8 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
25bc9 20 63 65 6c 6c 20 74 68 61 74 20 63 6f 6e 74 61   cell that conta
25bca 69 6e 73 20 74 68 65 20 72 6f 77 69 64 20 74 6f  ins the rowid to
25bcb 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74   delete */.  int
25bcc 20 63 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 20   count,         
25bcd 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  /* If non-zero, 
25bce 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f  increment the ro
25bcf 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  w change counter
25bd0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
25bd1 54 72 69 67 67 65 72 2c 20 2f 2a 20 4c 69 73 74  Trigger, /* List
25bd2 20 6f 66 20 74 72 69 67 67 65 72 73 20 74 6f 20   of triggers to 
25bd3 28 70 6f 74 65 6e 74 69 61 6c 6c 79 29 20 66 69  (potentially) fi
25bd4 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 63 6f  re */.  int onco
25bd5 6e 66 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65  nf         /* De
25bd6 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43  fault ON CONFLIC
25bd7 54 20 70 6f 6c 69 63 79 20 66 6f 72 20 74 72 69  T policy for tri
25bd8 67 67 65 72 73 20 2a 2f 0a 29 7b 0a 20 20 56 64  ggers */.){.  Vd
25bd9 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
25bda 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 2f 2a  pVdbe;        /*
25bdb 20 56 64 62 65 20 2a 2f 0a 20 20 69 6e 74 20 69   Vdbe */.  int i
25bdc 4f 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Old = 0;        
25bdd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
25bde 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20  rst register in 
25bdf 4f 4c 44 2e 2a 20 61 72 72 61 79 20 2a 2f 0a 20  OLD.* array */. 
25be0 20 69 6e 74 20 69 4c 61 62 65 6c 3b 20 20 20 20   int iLabel;    
25be1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25be2 20 2f 2a 20 4c 61 62 65 6c 20 72 65 73 6f 6c 76   /* Label resolv
25be3 65 64 20 74 6f 20 65 6e 64 20 6f 66 20 67 65 6e  ed to end of gen
25be4 65 72 61 74 65 64 20 63 6f 64 65 20 2a 2f 0a 0a  erated code */..
25be5 20 20 2f 2a 20 56 64 62 65 20 69 73 20 67 75 61    /* Vdbe is gua
25be6 72 61 6e 74 65 65 64 20 74 6f 20 68 61 76 65 20  ranteed to have 
25be7 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62  been allocated b
25be8 79 20 74 68 69 73 20 73 74 61 67 65 2e 20 2a 2f  y this stage. */
25be9 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a  .  assert( v );.
25bea 0a 20 20 2f 2a 20 53 65 65 6b 20 63 75 72 73 6f  .  /* Seek curso
25beb 72 20 69 43 75 72 20 74 6f 20 74 68 65 20 72 6f  r iCur to the ro
25bec 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 49 66 20  w to delete. If 
25bed 74 68 69 73 20 72 6f 77 20 6e 6f 20 6c 6f 6e 67  this row no long
25bee 65 72 20 65 78 69 73 74 73 20 0a 20 20 2a 2a 20  er exists .  ** 
25bef 28 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e  (this can happen
25bf0 20 69 66 20 61 20 74 72 69 67 67 65 72 20 70 72   if a trigger pr
25bf1 6f 67 72 61 6d 20 68 61 73 20 61 6c 72 65 61 64  ogram has alread
25bf2 79 20 64 65 6c 65 74 65 64 20 69 74 29 2c 20 64  y deleted it), d
25bf3 6f 0a 20 20 2a 2a 20 6e 6f 74 20 61 74 74 65 6d  o.  ** not attem
25bf4 70 74 20 74 6f 20 64 65 6c 65 74 65 20 69 74 20  pt to delete it 
25bf5 6f 72 20 66 69 72 65 20 61 6e 79 20 44 45 4c 45  or fire any DELE
25bf6 54 45 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f  TE triggers.  */
25bf7 0a 20 20 69 4c 61 62 65 6c 20 3d 20 73 71 6c 69  .  iLabel = sqli
25bf8 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
25bf9 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  (v);.  sqlite3Vd
25bfa 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
25bfb 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20  otExists, iCur, 
25bfc 69 4c 61 62 65 6c 2c 20 69 52 6f 77 69 64 29 3b  iLabel, iRowid);
25bfd 0a 20 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  . .  /* If there
25bfe 20 61 72 65 20 61 6e 79 20 74 72 69 67 67 65 72   are any trigger
25bff 73 20 74 6f 20 66 69 72 65 2c 20 61 6c 6c 6f 63  s to fire, alloc
25c00 61 74 65 20 61 20 72 61 6e 67 65 20 6f 66 20 72  ate a range of r
25c01 65 67 69 73 74 65 72 73 20 74 6f 0a 20 20 2a 2a  egisters to.  **
25c02 20 75 73 65 20 66 6f 72 20 74 68 65 20 6f 6c 64   use for the old
25c03 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e  .* references in
25c04 20 74 68 65 20 74 72 69 67 67 65 72 73 2e 20 20   the triggers.  
25c05 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
25c06 46 6b 52 65 71 75 69 72 65 64 28 70 50 61 72 73  FkRequired(pPars
25c07 65 2c 20 70 54 61 62 2c 20 30 2c 20 30 29 20 7c  e, pTab, 0, 0) |
25c08 7c 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20  | pTrigger ){.  
25c09 20 20 75 33 32 20 6d 61 73 6b 3b 20 20 20 20 20    u32 mask;     
25c0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c0b 2f 2a 20 4d 61 73 6b 20 6f 66 20 4f 4c 44 2e 2a  /* Mask of OLD.*
25c0c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 75 73 65 20   columns in use 
25c0d 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b  */.    int iCol;
25c0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c0f 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
25c10 20 75 73 65 64 20 77 68 69 6c 65 20 70 6f 70 75   used while popu
25c11 6c 61 74 69 6e 67 20 4f 4c 44 2e 2a 20 2a 2f 0a  lating OLD.* */.
25c12 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 6f  .    /* TODO: Co
25c13 75 6c 64 20 75 73 65 20 74 65 6d 70 6f 72 61 72  uld use temporar
25c14 79 20 72 65 67 69 73 74 65 72 73 20 68 65 72 65  y registers here
25c15 2e 20 41 6c 73 6f 20 63 6f 75 6c 64 20 61 74 74  . Also could att
25c16 65 6d 70 74 20 74 6f 0a 20 20 20 20 2a 2a 20 61  empt to.    ** a
25c17 76 6f 69 64 20 63 6f 70 79 69 6e 67 20 74 68 65  void copying the
25c18 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
25c19 20 72 6f 77 69 64 20 72 65 67 69 73 74 65 72 2e   rowid register.
25c1a 20 20 2a 2f 0a 20 20 20 20 6d 61 73 6b 20 3d 20    */.    mask = 
25c1b 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4f 6c  sqlite3TriggerOl
25c1c 64 6d 61 73 6b 28 70 50 61 72 73 65 2c 20 70 54  dmask(pParse, pT
25c1d 72 69 67 67 65 72 2c 20 30 2c 20 70 54 61 62 2c  rigger, 0, pTab,
25c1e 20 6f 6e 63 6f 6e 66 29 3b 0a 20 20 20 20 6d 61   onconf);.    ma
25c1f 73 6b 20 7c 3d 20 73 71 6c 69 74 65 33 46 6b 4f  sk |= sqlite3FkO
25c20 6c 64 6d 61 73 6b 28 70 50 61 72 73 65 2c 20 70  ldmask(pParse, p
25c21 54 61 62 29 3b 0a 20 20 20 20 69 4f 6c 64 20 3d  Tab);.    iOld =
25c22 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b   pParse->nMem+1;
25c23 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
25c24 6d 20 2b 3d 20 28 31 20 2b 20 70 54 61 62 2d 3e  m += (1 + pTab->
25c25 6e 43 6f 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 50  nCol);..    /* P
25c26 6f 70 75 6c 61 74 65 20 74 68 65 20 4f 4c 44 2e  opulate the OLD.
25c27 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 72  * pseudo-table r
25c28 65 67 69 73 74 65 72 20 61 72 72 61 79 2e 20 54  egister array. T
25c29 68 65 73 65 20 76 61 6c 75 65 73 20 77 69 6c 6c  hese values will
25c2a 20 62 65 20 0a 20 20 20 20 2a 2a 20 75 73 65 64   be .    ** used
25c2b 20 62 79 20 61 6e 79 20 42 45 46 4f 52 45 20 61   by any BEFORE a
25c2c 6e 64 20 41 46 54 45 52 20 74 72 69 67 67 65 72  nd AFTER trigger
25c2d 73 20 74 68 61 74 20 65 78 69 73 74 2e 20 20 2a  s that exist.  *
25c2e 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
25c2f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
25c30 70 79 2c 20 69 52 6f 77 69 64 2c 20 69 4f 6c 64  py, iRowid, iOld
25c31 29 3b 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d  );.    for(iCol=
25c32 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  0; iCol<pTab->nC
25c33 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20  ol; iCol++){.   
25c34 20 20 20 69 66 28 20 6d 61 73 6b 3d 3d 30 78 66     if( mask==0xf
25c35 66 66 66 66 66 66 66 20 7c 7c 20 6d 61 73 6b 26  fffffff || mask&
25c36 28 31 3c 3c 69 43 6f 6c 29 20 29 7b 0a 20 20 20  (1<<iCol) ){.   
25c37 20 20 20 20 20 69 6e 74 20 69 54 61 72 67 65 74       int iTarget
25c38 20 3d 20 69 4f 6c 64 20 2b 20 69 43 6f 6c 20 2b   = iOld + iCol +
25c39 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   1;.        sqli
25c3a 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
25c3b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72   OP_Column, iCur
25c3c 2c 20 69 43 6f 6c 2c 20 69 54 61 72 67 65 74 29  , iCol, iTarget)
25c3d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
25c3e 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76  3ColumnDefault(v
25c3f 2c 20 70 54 61 62 2c 20 69 43 6f 6c 2c 20 69 54  , pTab, iCol, iT
25c40 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a  arget);.      }.
25c41 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e      }..    /* In
25c42 76 6f 6b 65 20 42 45 46 4f 52 45 20 44 45 4c 45  voke BEFORE DELE
25c43 54 45 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  TE trigger progr
25c44 61 6d 73 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  ams. */.    sqli
25c45 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
25c46 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67  r(pParse, pTrigg
25c47 65 72 2c 20 0a 20 20 20 20 20 20 20 20 54 4b 5f  er, .        TK_
25c48 44 45 4c 45 54 45 2c 20 30 2c 20 54 52 49 47 47  DELETE, 0, TRIGG
25c49 45 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c  ER_BEFORE, pTab,
25c4a 20 69 4f 6c 64 2c 20 6f 6e 63 6f 6e 66 2c 20 69   iOld, onconf, i
25c4b 4c 61 62 65 6c 0a 20 20 20 20 29 3b 0a 0a 20 20  Label.    );..  
25c4c 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 63 75    /* Seek the cu
25c4d 72 73 6f 72 20 74 6f 20 74 68 65 20 72 6f 77 20  rsor to the row 
25c4e 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 61 67  to be deleted ag
25c4f 61 69 6e 2e 20 49 74 20 6d 61 79 20 62 65 20 74  ain. It may be t
25c50 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 42  hat.    ** the B
25c51 45 46 4f 52 45 20 74 72 69 67 67 65 72 73 20 63  EFORE triggers c
25c52 6f 64 65 64 20 61 62 6f 76 65 20 68 61 76 65 20  oded above have 
25c53 61 6c 72 65 61 64 79 20 72 65 6d 6f 76 65 64 20  already removed 
25c54 74 68 65 20 72 6f 77 0a 20 20 20 20 2a 2a 20 62  the row.    ** b
25c55 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 44 6f  eing deleted. Do
25c56 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
25c57 64 65 6c 65 74 65 20 74 68 65 20 72 6f 77 20 61  delete the row a
25c58 20 73 65 63 6f 6e 64 20 74 69 6d 65 2c 20 61 6e   second time, an
25c59 64 20 0a 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74  d .    ** do not
25c5a 20 66 69 72 65 20 41 46 54 45 52 20 74 72 69 67   fire AFTER trig
25c5b 67 65 72 73 2e 20 20 2a 2f 0a 20 20 20 20 73 71  gers.  */.    sq
25c5c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
25c5d 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
25c5e 20 69 43 75 72 2c 20 69 4c 61 62 65 6c 2c 20 69   iCur, iLabel, i
25c5f 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20  Rowid);..    /* 
25c60 44 6f 20 46 4b 20 70 72 6f 63 65 73 73 69 6e 67  Do FK processing
25c61 2e 20 54 68 69 73 20 63 61 6c 6c 20 63 68 65 63  . This call chec
25c62 6b 73 20 74 68 61 74 20 61 6e 79 20 46 4b 20 63  ks that any FK c
25c63 6f 6e 73 74 72 61 69 6e 74 73 20 74 68 61 74 0a  onstraints that.
25c64 20 20 20 20 2a 2a 20 72 65 66 65 72 20 74 6f 20      ** refer to 
25c65 74 68 69 73 20 74 61 62 6c 65 20 28 69 2e 65 2e  this table (i.e.
25c66 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 74 74   constraints att
25c67 61 63 68 65 64 20 74 6f 20 6f 74 68 65 72 20 74  ached to other t
25c68 61 62 6c 65 73 29 20 0a 20 20 20 20 2a 2a 20 61  ables) .    ** a
25c69 72 65 20 6e 6f 74 20 76 69 6f 6c 61 74 65 64 20  re not violated 
25c6a 62 79 20 64 65 6c 65 74 69 6e 67 20 74 68 69 73  by deleting this
25c6b 20 72 6f 77 2e 20 20 2a 2f 0a 20 20 20 20 73 71   row.  */.    sq
25c6c 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 70 50 61  lite3FkCheck(pPa
25c6d 72 73 65 2c 20 70 54 61 62 2c 20 69 4f 6c 64 2c  rse, pTab, iOld,
25c6e 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44   0);.  }..  /* D
25c6f 65 6c 65 74 65 20 74 68 65 20 69 6e 64 65 78 20  elete the index 
25c70 61 6e 64 20 74 61 62 6c 65 20 65 6e 74 72 69 65  and table entrie
25c71 73 2e 20 53 6b 69 70 20 74 68 69 73 20 73 74 65  s. Skip this ste
25c72 70 20 69 66 20 70 54 61 62 20 69 73 20 72 65 61  p if pTab is rea
25c73 6c 6c 79 0a 20 20 2a 2a 20 61 20 76 69 65 77 20  lly.  ** a view 
25c74 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74  (in which case t
25c75 68 65 20 6f 6e 6c 79 20 65 66 66 65 63 74 20 6f  he only effect o
25c76 66 20 74 68 65 20 44 45 4c 45 54 45 20 73 74 61  f the DELETE sta
25c77 74 65 6d 65 6e 74 20 69 73 20 74 6f 0a 20 20 2a  tement is to.  *
25c78 2a 20 66 69 72 65 20 74 68 65 20 49 4e 53 54 45  * fire the INSTE
25c79 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 29 2e  AD OF triggers).
25c7a 20 20 2a 2f 20 0a 20 20 69 66 28 20 70 54 61 62    */ .  if( pTab
25c7b 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a  ->pSelect==0 ){.
25c7c 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
25c7d 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74  ateRowIndexDelet
25c7e 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
25c7f 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 73 71  iCur, 0);.    sq
25c80 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25c81 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43  v, OP_Delete, iC
25c82 75 72 2c 20 28 63 6f 75 6e 74 3f 4f 50 46 4c 41  ur, (count?OPFLA
25c83 47 5f 4e 43 48 41 4e 47 45 3a 30 29 29 3b 0a 20  G_NCHANGE:0));. 
25c84 20 20 20 69 66 28 20 63 6f 75 6e 74 20 29 7b 0a     if( count ){.
25c85 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25c86 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
25c87 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34   pTab->zName, P4
25c88 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a  _STATIC);.    }.
25c89 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 61 6e 79    }..  /* Do any
25c8a 20 4f 4e 20 43 41 53 43 41 44 45 2c 20 53 45 54   ON CASCADE, SET
25c8b 20 4e 55 4c 4c 20 6f 72 20 53 45 54 20 44 45 46   NULL or SET DEF
25c8c 41 55 4c 54 20 6f 70 65 72 61 74 69 6f 6e 73 20  AULT operations 
25c8d 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a  required to.  **
25c8e 20 68 61 6e 64 6c 65 20 72 6f 77 73 20 28 70 6f   handle rows (po
25c8f 73 73 69 62 6c 79 20 69 6e 20 6f 74 68 65 72 20  ssibly in other 
25c90 74 61 62 6c 65 73 29 20 74 68 61 74 20 72 65 66  tables) that ref
25c91 65 72 20 76 69 61 20 61 20 66 6f 72 65 69 67 6e  er via a foreign
25c92 20 6b 65 79 0a 20 20 2a 2a 20 74 6f 20 74 68 65   key.  ** to the
25c93 20 72 6f 77 20 6a 75 73 74 20 64 65 6c 65 74 65   row just delete
25c94 64 2e 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33  d. */ .  sqlite3
25c95 46 6b 41 63 74 69 6f 6e 73 28 70 50 61 72 73 65  FkActions(pParse
25c96 2c 20 70 54 61 62 2c 20 30 2c 20 69 4f 6c 64 29  , pTab, 0, iOld)
25c97 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 41  ;..  /* Invoke A
25c98 46 54 45 52 20 44 45 4c 45 54 45 20 74 72 69 67  FTER DELETE trig
25c99 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 2a 2f  ger programs. */
25c9a 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  .  sqlite3CodeRo
25c9b 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  wTrigger(pParse,
25c9c 20 70 54 72 69 67 67 65 72 2c 20 0a 20 20 20 20   pTrigger, .    
25c9d 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20    TK_DELETE, 0, 
25c9e 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70  TRIGGER_AFTER, p
25c9f 54 61 62 2c 20 69 4f 6c 64 2c 20 6f 6e 63 6f 6e  Tab, iOld, oncon
25ca0 66 2c 20 69 4c 61 62 65 6c 0a 20 20 29 3b 0a 0a  f, iLabel.  );..
25ca1 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
25ca2 66 20 74 68 65 20 72 6f 77 20 68 61 64 20 61 6c  f the row had al
25ca3 72 65 61 64 79 20 62 65 65 6e 20 64 65 6c 65 74  ready been delet
25ca4 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 42 45  ed before any BE
25ca5 46 4f 52 45 0a 20 20 2a 2a 20 74 72 69 67 67 65  FORE.  ** trigge
25ca6 72 20 70 72 6f 67 72 61 6d 73 20 77 65 72 65 20  r programs were 
25ca7 69 6e 76 6f 6b 65 64 2e 20 4f 72 20 69 66 20 61  invoked. Or if a
25ca8 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
25ca9 20 74 68 72 6f 77 73 20 61 20 0a 20 20 2a 2a 20   throws a .  ** 
25caa 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20 65 78  RAISE(IGNORE) ex
25cab 63 65 70 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 73  ception.  */.  s
25cac 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
25cad 65 4c 61 62 65 6c 28 76 2c 20 69 4c 61 62 65 6c  eLabel(v, iLabel
25cae 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
25caf 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
25cb0 65 73 20 56 44 42 45 20 63 6f 64 65 20 74 68 61  es VDBE code tha
25cb1 74 20 63 61 75 73 65 73 20 74 68 65 20 64 65 6c  t causes the del
25cb2 65 74 69 6f 6e 20 6f 66 20 61 6c 6c 0a 2a 2a 20  etion of all.** 
25cb3 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61 73  index entries as
25cb4 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
25cb5 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20  single row of a 
25cb6 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e 0a 2a 2a  single table..**
25cb7 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73  .** The VDBE mus
25cb8 74 20 62 65 20 69 6e 20 61 20 70 61 72 74 69 63  t be in a partic
25cb9 75 6c 61 72 20 73 74 61 74 65 20 77 68 65 6e 20  ular state when 
25cba 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
25cbb 63 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65  called..** These
25cbc 20 61 72 65 20 74 68 65 20 72 65 71 75 69 72 65   are the require
25cbd 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  ments:.**.**   1
25cbe 2e 20 20 41 20 72 65 61 64 2f 77 72 69 74 65 20  .  A read/write 
25cbf 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
25cc0 74 6f 20 70 54 61 62 2c 20 74 68 65 20 74 61 62  to pTab, the tab
25cc1 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
25cc2 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74  e row.**       t
25cc3 6f 20 62 65 20 64 65 6c 65 74 65 64 2c 20 6d 75  o be deleted, mu
25cc4 73 74 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20  st be opened as 
25cc5 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 69  cursor number "i
25cc6 43 75 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e  Cur"..**.**   2.
25cc7 20 20 52 65 61 64 2f 77 72 69 74 65 20 63 75 72    Read/write cur
25cc8 73 6f 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64  sors for all ind
25cc9 69 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73  ices of pTab mus
25cca 74 20 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20  t be open as.** 
25ccb 20 20 20 20 20 20 63 75 72 73 6f 72 20 6e 75 6d        cursor num
25ccc 62 65 72 20 69 43 75 72 2b 69 20 66 6f 72 20 74  ber iCur+i for t
25ccd 68 65 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a  he i-th index..*
25cce 2a 0a 2a 2a 20 20 20 33 2e 20 20 54 68 65 20 22  *.**   3.  The "
25ccf 69 43 75 72 22 20 63 75 72 73 6f 72 20 6d 75 73  iCur" cursor mus
25cd0 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f  t be pointing to
25cd1 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 69 73   the row that is
25cd2 20 74 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20   to be.**       
25cd3 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  deleted..*/.SQLI
25cd4 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
25cd5 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
25cd6 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 0a 20  owIndexDelete(. 
25cd7 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
25cd8 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61      /* Parsing a
25cd9 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69  nd code generati
25cda 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
25cdb 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
25cdc 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74     /* Table cont
25cdd 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 20 74  aining the row t
25cde 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a  o be deleted */.
25cdf 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20    int iCur,     
25ce0 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
25ce1 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61  umber for the ta
25ce2 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52  ble */.  int *aR
25ce3 65 67 49 64 78 20 20 20 20 20 20 20 2f 2a 20 4f  egIdx       /* O
25ce4 6e 6c 79 20 64 65 6c 65 74 65 20 69 66 20 61 52  nly delete if aR
25ce5 65 67 49 64 78 21 3d 30 20 26 26 20 61 52 65 67  egIdx!=0 && aReg
25ce6 49 64 78 5b 69 5d 3e 30 20 2a 2f 0a 29 7b 0a 20  Idx[i]>0 */.){. 
25ce7 20 69 6e 74 20 69 3b 0a 20 20 49 6e 64 65 78 20   int i;.  Index 
25ce8 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20 72 31 3b  *pIdx;.  int r1;
25ce9 0a 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64  ..  for(i=1, pId
25cea 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
25ceb 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d  pIdx; i++, pIdx=
25cec 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
25ced 20 20 69 66 28 20 61 52 65 67 49 64 78 21 3d 30    if( aRegIdx!=0
25cee 20 26 26 20 61 52 65 67 49 64 78 5b 69 2d 31 5d   && aRegIdx[i-1]
25cef 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
25cf0 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
25cf1 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79  GenerateIndexKey
25cf2 28 70 50 61 72 73 65 2c 20 70 49 64 78 2c 20 69  (pParse, pIdx, i
25cf3 43 75 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Cur, 0, 0);.    
25cf4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25cf5 33 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  3(pParse->pVdbe,
25cf6 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 2c 20 69   OP_IdxDelete, i
25cf7 43 75 72 2b 69 2c 20 72 31 2c 70 49 64 78 2d 3e  Cur+i, r1,pIdx->
25cf8 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 7d 0a  nColumn+1);.  }.
25cf9 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
25cfa 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
25cfb 20 61 73 73 65 6d 62 6c 65 20 61 6e 20 69 6e 64   assemble an ind
25cfc 65 78 20 6b 65 79 20 61 6e 64 20 70 75 74 20 69  ex key and put i
25cfd 74 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a  t in register.**
25cfe 20 72 65 67 4f 75 74 2e 20 20 54 68 65 20 6b 65   regOut.  The ke
25cff 79 20 77 69 74 68 20 62 65 20 66 6f 72 20 69 6e  y with be for in
25d00 64 65 78 20 70 49 64 78 20 77 68 69 63 68 20 69  dex pIdx which i
25d01 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54  s an index on pT
25d02 61 62 2e 0a 2a 2a 20 69 43 75 72 20 69 73 20 74  ab..** iCur is t
25d03 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20 63 75  he index of a cu
25d04 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65  rsor open on the
25d05 20 70 54 61 62 20 74 61 62 6c 65 20 61 6e 64 20   pTab table and 
25d06 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 74  pointing to.** t
25d07 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 6e 65  he entry that ne
25d08 65 64 73 20 69 6e 64 65 78 69 6e 67 2e 0a 2a 2a  eds indexing..**
25d09 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 72 65 67  .** Return a reg
25d0a 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 69  ister number whi
25d0b 63 68 20 69 73 20 74 68 65 20 66 69 72 73 74 20  ch is the first 
25d0c 69 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a  in a block of.**
25d0d 20 72 65 67 69 73 74 65 72 73 20 74 68 61 74 20   registers that 
25d0e 68 6f 6c 64 73 20 74 68 65 20 65 6c 65 6d 65 6e  holds the elemen
25d0f 74 73 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ts of the index 
25d10 6b 65 79 2e 20 20 54 68 65 0a 2a 2a 20 62 6c 6f  key.  The.** blo
25d11 63 6b 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ck of registers 
25d12 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
25d13 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 20   deallocated by 
25d14 74 68 65 20 74 69 6d 65 0a 2a 2a 20 74 68 69 73  the time.** this
25d15 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
25d16 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
25d17 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47  ATE int sqlite3G
25d18 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28  enerateIndexKey(
25d19 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
25d1a 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  ,     /* Parsing
25d1b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 49 6e   context */.  In
25d1c 64 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20  dex *pIdx,      
25d1d 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f   /* The index fo
25d1e 72 20 77 68 69 63 68 20 74 6f 20 67 65 6e 65 72  r which to gener
25d1f 61 74 65 20 61 20 6b 65 79 20 2a 2f 0a 20 20 69  ate a key */.  i
25d20 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20  nt iCur,        
25d21 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
25d22 65 72 20 66 6f 72 20 74 68 65 20 70 49 64 78 2d  er for the pIdx-
25d23 3e 70 54 61 62 6c 65 20 74 61 62 6c 65 20 2a 2f  >pTable table */
25d24 0a 20 20 69 6e 74 20 72 65 67 4f 75 74 2c 20 20  .  int regOut,  
25d25 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
25d26 68 65 20 6e 65 77 20 69 6e 64 65 78 20 6b 65 79  he new index key
25d27 20 74 6f 20 74 68 69 73 20 72 65 67 69 73 74 65   to this registe
25d28 72 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 4d 61 6b  r */.  int doMak
25d29 65 52 65 63 20 20 20 20 20 20 2f 2a 20 52 75 6e  eRec      /* Run
25d2a 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f   the OP_MakeReco
25d2b 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  rd instruction i
25d2c 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 56  f true */.){.  V
25d2d 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
25d2e 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6a 3b  >pVdbe;.  int j;
25d2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
25d30 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20   pIdx->pTable;. 
25d31 20 69 6e 74 20 72 65 67 42 61 73 65 3b 0a 20 20   int regBase;.  
25d32 69 6e 74 20 6e 43 6f 6c 3b 0a 0a 20 20 6e 43 6f  int nCol;..  nCo
25d33 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  l = pIdx->nColum
25d34 6e 3b 0a 20 20 72 65 67 42 61 73 65 20 3d 20 73  n;.  regBase = s
25d35 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
25d36 67 65 28 70 50 61 72 73 65 2c 20 6e 43 6f 6c 2b  ge(pParse, nCol+
25d37 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  1);.  sqlite3Vdb
25d38 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
25d39 77 69 64 2c 20 69 43 75 72 2c 20 72 65 67 42 61  wid, iCur, regBa
25d3a 73 65 2b 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28  se+nCol);.  for(
25d3b 6a 3d 30 3b 20 6a 3c 6e 43 6f 6c 3b 20 6a 2b 2b  j=0; j<nCol; j++
25d3c 29 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d  ){.    int idx =
25d3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
25d3e 6a 5d 3b 0a 20 20 20 20 69 66 28 20 69 64 78 3d  j];.    if( idx=
25d3f 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a  =pTab->iPKey ){.
25d40 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25d41 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
25d42 6f 70 79 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f  opy, regBase+nCo
25d43 6c 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20  l, regBase+j);. 
25d44 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
25d45 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25d46 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
25d47 69 43 75 72 2c 20 69 64 78 2c 20 72 65 67 42 61  iCur, idx, regBa
25d48 73 65 2b 6a 29 3b 0a 20 20 20 20 20 20 73 71 6c  se+j);.      sql
25d49 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c  ite3ColumnDefaul
25d4a 74 28 76 2c 20 70 54 61 62 2c 20 69 64 78 2c 20  t(v, pTab, idx, 
25d4b 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  -1);.    }.  }. 
25d4c 20 69 66 28 20 64 6f 4d 61 6b 65 52 65 63 20 29   if( doMakeRec )
25d4d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
25d4e 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  eAddOp3(v, OP_Ma
25d4f 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73  keRecord, regBas
25d50 65 2c 20 6e 43 6f 6c 2b 31 2c 20 72 65 67 4f 75  e, nCol+1, regOu
25d51 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
25d52 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
25d53 31 2c 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41  1, sqlite3IndexA
25d54 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49  ffinityStr(v, pI
25d55 64 78 29 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  dx), 0);.    sql
25d56 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
25d57 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
25d58 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f  se, regBase, nCo
25d59 6c 2b 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  l+1);.  }.  sqli
25d5a 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
25d5b 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42  nge(pParse, regB
25d5c 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20  ase, nCol+1);.  
25d5d 72 65 74 75 72 6e 20 72 65 67 42 61 73 65 3b 0a  return regBase;.
25d5e 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.../***********
25d5f 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 65 6c 65 74  *** End of delet
25d60 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
25d61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d63 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
25d64 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66  *** Begin file f
25d65 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  unc.c **********
25d66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d68 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 32 20 46  **/./*.** 2002 F
25d69 65 62 72 75 61 72 79 20 32 33 0a 2a 2a 0a 2a 2a  ebruary 23.**.**
25d6a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
25d6b 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
25d6c 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
25d6d 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
25d6e 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
25d6f 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
25d70 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
25d71 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
25d72 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
25d73 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
25d74 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
25d75 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
25d76 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
25d77 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
25d78 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
25d79 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
25d7a 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
25d7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
25d80 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
25d81 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20  the C functions 
25d82 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 76  that implement v
25d83 61 72 69 6f 75 73 20 53 51 4c 0a 2a 2a 20 66 75  arious SQL.** fu
25d84 6e 63 74 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  nctions of SQLit
25d85 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  e.  .**.** There
25d86 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70   is only one exp
25d87 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20  orted symbol in 
25d88 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20  this file - the 
25d89 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69  function.** sqli
25d8a 74 65 52 65 67 69 73 74 65 72 42 75 69 6c 64 69  teRegisterBuildi
25d8b 6e 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75  nFunctions() fou
25d8c 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d  nd at the bottom
25d8d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a   of the file..**
25d8e 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20   All other code 
25d8f 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a  has file scope..
25d90 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
25d91 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66   the collating f
25d92 75 6e 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74  unction associat
25d93 65 64 20 77 69 74 68 20 61 20 66 75 6e 63 74 69  ed with a functi
25d94 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f  on..*/.static Co
25d95 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65  llSeq *sqlite3Ge
25d96 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 73 71 6c  tFuncCollSeq(sql
25d97 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25d98 6e 74 65 78 74 29 7b 0a 20 20 72 65 74 75 72 6e  ntext){.  return
25d99 20 63 6f 6e 74 65 78 74 2d 3e 70 43 6f 6c 6c 3b   context->pColl;
25d9a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
25d9b 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
25d9c 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 6d 69  non-aggregate mi
25d9d 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20 66 75  n() and max() fu
25d9e 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69  nctions.*/.stati
25d9f 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 46 75 6e  c void minmaxFun
25da0 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
25da1 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
25da2 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
25da3 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
25da4 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  v.){.  int i;.  
25da5 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20 2f 2a 20  int mask;    /* 
25da6 30 20 66 6f 72 20 6d 69 6e 28 29 20 6f 72 20 30  0 for min() or 0
25da7 78 66 66 66 66 66 66 66 66 20 66 6f 72 20 6d 61  xffffffff for ma
25da8 78 28 29 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65  x() */.  int iBe
25da9 73 74 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  st;.  CollSeq *p
25daa 43 6f 6c 6c 3b 0a 0a 20 20 61 73 73 65 72 74 28  Coll;..  assert(
25dab 20 61 72 67 63 3e 31 20 29 3b 0a 20 20 6d 61 73   argc>1 );.  mas
25dac 6b 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72  k = sqlite3_user
25dad 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3d 3d  _data(context)==
25dae 30 20 3f 20 30 20 3a 20 2d 31 3b 0a 20 20 70 43  0 ? 0 : -1;.  pC
25daf 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74  oll = sqlite3Get
25db0 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74  FuncCollSeq(cont
25db1 65 78 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ext);.  assert( 
25db2 70 43 6f 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72  pColl );.  asser
25db3 74 28 20 6d 61 73 6b 3d 3d 2d 31 20 7c 7c 20 6d  t( mask==-1 || m
25db4 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 69 42 65 73  ask==0 );.  iBes
25db5 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c  t = 0;.  if( sql
25db6 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
25db7 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45  argv[0])==SQLITE
25db8 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a  _NULL ) return;.
25db9 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67    for(i=1; i<arg
25dba 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  c; i++){.    if(
25dbb 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
25dbc 79 70 65 28 61 72 67 76 5b 69 5d 29 3d 3d 53 51  ype(argv[i])==SQ
25dbd 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75  LITE_NULL ) retu
25dbe 72 6e 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c  rn;.    if( (sql
25dbf 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61  ite3MemCompare(a
25dc0 72 67 76 5b 69 42 65 73 74 5d 2c 20 61 72 67 76  rgv[iBest], argv
25dc1 5b 69 5d 2c 20 70 43 6f 6c 6c 29 5e 6d 61 73 6b  [i], pColl)^mask
25dc2 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 74 65  )>=0 ){.      te
25dc3 73 74 63 61 73 65 28 20 6d 61 73 6b 3d 3d 30 20  stcase( mask==0 
25dc4 29 3b 0a 20 20 20 20 20 20 69 42 65 73 74 20 3d  );.      iBest =
25dc5 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   i;.    }.  }.  
25dc6 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
25dc7 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  alue(context, ar
25dc8 67 76 5b 69 42 65 73 74 5d 29 3b 0a 7d 0a 0a 2f  gv[iBest]);.}../
25dc9 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
25dca 74 79 70 65 20 6f 66 20 74 68 65 20 61 72 67 75  type of the argu
25dcb 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ment..*/.static 
25dcc 76 6f 69 64 20 74 79 70 65 6f 66 46 75 6e 63 28  void typeofFunc(
25dcd 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
25dce 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
25dcf 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
25dd0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
25dd1 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  gv.){.  const ch
25dd2 61 72 20 2a 7a 20 3d 20 30 3b 0a 20 20 55 4e 55  ar *z = 0;.  UNU
25dd3 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
25dd4 74 55 73 65 64 29 3b 0a 20 20 73 77 69 74 63 68  tUsed);.  switch
25dd5 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
25dd6 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b  type(argv[0]) ){
25dd7 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
25dd8 5f 49 4e 54 45 47 45 52 3a 20 7a 20 3d 20 22 69  _INTEGER: z = "i
25dd9 6e 74 65 67 65 72 22 3b 20 62 72 65 61 6b 3b 0a  nteger"; break;.
25dda 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
25ddb 54 45 58 54 3a 20 20 20 20 7a 20 3d 20 22 74 65  TEXT:    z = "te
25ddc 78 74 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20  xt";    break;. 
25ddd 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
25dde 4c 4f 41 54 3a 20 20 20 7a 20 3d 20 22 72 65 61  LOAT:   z = "rea
25ddf 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  l";    break;.  
25de0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c    case SQLITE_BL
25de1 4f 42 3a 20 20 20 20 7a 20 3d 20 22 62 6c 6f 62  OB:    z = "blob
25de2 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ";    break;.   
25de3 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20   default:       
25de4 20 20 20 20 20 20 7a 20 3d 20 22 6e 75 6c 6c 22        z = "null"
25de5 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  ;    break;.  }.
25de6 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25de7 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
25de8 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41  , -1, SQLITE_STA
25de9 54 49 43 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  TIC);.}.../*.** 
25dea 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
25deb 66 20 74 68 65 20 6c 65 6e 67 74 68 28 29 20 66  f the length() f
25dec 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69  unction.*/.stati
25ded 63 20 76 6f 69 64 20 6c 65 6e 67 74 68 46 75 6e  c void lengthFun
25dee 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
25def 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
25df0 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
25df1 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
25df2 76 0a 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a  v.){.  int len;.
25df3 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
25df4 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =1 );.  UNUSED_P
25df5 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a  ARAMETER(argc);.
25df6 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65    switch( sqlite
25df7 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
25df8 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73  v[0]) ){.    cas
25df9 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20  e SQLITE_BLOB:. 
25dfa 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
25dfb 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65  NTEGER:.    case
25dfc 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b   SQLITE_FLOAT: {
25dfd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
25dfe 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78  esult_int(contex
25dff 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t, sqlite3_value
25e00 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 29  _bytes(argv[0]))
25e01 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
25e02 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
25e03 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20  LITE_TEXT: {.   
25e04 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65     const unsigne
25e05 64 20 63 68 61 72 20 2a 7a 20 3d 20 73 71 6c 69  d char *z = sqli
25e06 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
25e07 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69  rgv[0]);.      i
25e08 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( z==0 ) return
25e09 3b 0a 20 20 20 20 20 20 6c 65 6e 20 3d 20 30 3b  ;.      len = 0;
25e0a 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a  .      while( *z
25e0b 20 29 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e 2b   ){.        len+
25e0c 2b 3b 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54  +;.        SQLIT
25e0d 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a  E_SKIP_UTF8(z);.
25e0e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
25e0f 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
25e10 28 63 6f 6e 74 65 78 74 2c 20 6c 65 6e 29 3b 0a  (context, len);.
25e11 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
25e12 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
25e13 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
25e14 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e 74  result_null(cont
25e15 65 78 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ext);.      brea
25e16 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  k;.    }.  }.}..
25e17 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
25e18 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 62 73 28  tion of the abs(
25e19 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74  ) function.*/.st
25e1a 61 74 69 63 20 76 6f 69 64 20 61 62 73 46 75 6e  atic void absFun
25e1b 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
25e1c 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
25e1d 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
25e1e 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 61  lue **argv){.  a
25e1f 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29  ssert( argc==1 )
25e20 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
25e21 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 73 77  ETER(argc);.  sw
25e22 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61  itch( sqlite3_va
25e23 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
25e24 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  ) ){.    case SQ
25e25 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a  LITE_INTEGER: {.
25e26 20 20 20 20 20 20 69 36 34 20 69 56 61 6c 20 3d        i64 iVal =
25e27 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
25e28 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20  nt64(argv[0]);. 
25e29 20 20 20 20 20 69 66 28 20 69 56 61 6c 3c 30 20       if( iVal<0 
25e2a 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28  ){.        if( (
25e2b 69 56 61 6c 3c 3c 31 29 3d 3d 30 20 29 7b 0a 20  iVal<<1)==0 ){. 
25e2c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25e2d 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
25e2e 6e 74 65 78 74 2c 20 22 69 6e 74 65 67 65 72 20  ntext, "integer 
25e2f 6f 76 65 72 66 6c 6f 77 22 2c 20 2d 31 29 3b 0a  overflow", -1);.
25e30 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
25e31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
25e32 20 20 20 20 69 56 61 6c 20 3d 20 2d 69 56 61 6c      iVal = -iVal
25e33 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20  ;.      } .     
25e34 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25e35 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 69  int64(context, i
25e36 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61  Val);.      brea
25e37 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
25e38 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20 7b  e SQLITE_NULL: {
25e39 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
25e3a 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e 74 65  esult_null(conte
25e3b 78 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  xt);.      break
25e3c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
25e3d 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 64 6f 75  ult: {.      dou
25e3e 62 6c 65 20 72 56 61 6c 20 3d 20 73 71 6c 69 74  ble rVal = sqlit
25e3f 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
25e40 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
25e41 69 66 28 20 72 56 61 6c 3c 30 20 29 20 72 56 61  if( rVal<0 ) rVa
25e42 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20 20 20 20 20  l = -rVal;.     
25e43 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25e44 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20  double(context, 
25e45 72 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  rVal);.      bre
25e46 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ak;.    }.  }.}.
25e47 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
25e48 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 62  ation of the sub
25e49 73 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a  str() function..
25e4a 2a 2a 0a 2a 2a 20 73 75 62 73 74 72 28 78 2c 70  **.** substr(x,p
25e4b 31 2c 70 32 29 20 20 72 65 74 75 72 6e 73 20 70  1,p2)  returns p
25e4c 32 20 63 68 61 72 61 63 74 65 72 73 20 6f 66 20  2 characters of 
25e4d 78 5b 5d 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  x[] beginning wi
25e4e 74 68 20 70 31 2e 0a 2a 2a 20 70 31 20 69 73 20  th p1..** p1 is 
25e4f 31 2d 69 6e 64 65 78 65 64 2e 20 20 53 6f 20 73  1-indexed.  So s
25e50 75 62 73 74 72 28 78 2c 31 2c 31 29 20 72 65 74  ubstr(x,1,1) ret
25e51 75 72 6e 73 20 74 68 65 20 66 69 72 73 74 20 63  urns the first c
25e52 68 61 72 61 63 74 65 72 0a 2a 2a 20 6f 66 20 78  haracter.** of x
25e53 2e 20 20 49 66 20 78 20 69 73 20 74 65 78 74 2c  .  If x is text,
25e54 20 74 68 65 6e 20 77 65 20 61 63 74 75 61 6c 6c   then we actuall
25e55 79 20 63 6f 75 6e 74 20 55 54 46 2d 38 20 63 68  y count UTF-8 ch
25e56 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20  aracters..** If 
25e57 78 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65  x is a blob, the
25e58 6e 20 77 65 20 63 6f 75 6e 74 20 62 79 74 65 73  n we count bytes
25e59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 31 20 69 73  ..**.** If p1 is
25e5a 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20   negative, then 
25e5b 77 65 20 62 65 67 69 6e 20 61 62 73 28 70 31 29  we begin abs(p1)
25e5c 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66   from the end of
25e5d 20 78 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   x[]..*/.static 
25e5e 76 6f 69 64 20 73 75 62 73 74 72 46 75 6e 63 28  void substrFunc(
25e5f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
25e60 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
25e61 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
25e62 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
25e63 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
25e64 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63  ned char *z;.  c
25e65 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
25e66 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 6c 65  ar *z2;.  int le
25e67 6e 3b 0a 20 20 69 6e 74 20 70 30 74 79 70 65 3b  n;.  int p0type;
25e68 0a 20 20 69 36 34 20 70 31 2c 20 70 32 3b 0a 20  .  i64 p1, p2;. 
25e69 20 69 6e 74 20 6e 65 67 50 32 20 3d 20 30 3b 0a   int negP2 = 0;.
25e6a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
25e6b 3d 33 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b  =3 || argc==2 );
25e6c 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  .  if( sqlite3_v
25e6d 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31  alue_type(argv[1
25e6e 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0a  ])==SQLITE_NULL.
25e6f 20 20 20 7c 7c 20 28 61 72 67 63 3d 3d 33 20 26     || (argc==3 &
25e70 26 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  & sqlite3_value_
25e71 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3d 3d 53  type(argv[2])==S
25e72 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 20 20 29 7b  QLITE_NULL).  ){
25e73 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
25e74 0a 20 20 70 30 74 79 70 65 20 3d 20 73 71 6c 69  .  p0type = sqli
25e75 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
25e76 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 70  rgv[0]);.  if( p
25e77 30 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c  0type==SQLITE_BL
25e78 4f 42 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20  OB ){.    len = 
25e79 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
25e7a 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
25e7b 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    z = sqlite3_va
25e7c 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
25e7d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20  );.    if( z==0 
25e7e 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 61 73  ) return;.    as
25e7f 73 65 72 74 28 20 6c 65 6e 3d 3d 73 71 6c 69 74  sert( len==sqlit
25e80 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
25e81 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 7d 65 6c  rgv[0]) );.  }el
25e82 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  se{.    z = sqli
25e83 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
25e84 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[0]);.    if(
25e85 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   z==0 ) return;.
25e86 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20      len = 0;.   
25e87 20 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 3b 20   for(z2=z; *z2; 
25e88 6c 65 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51  len++){.      SQ
25e89 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a  LITE_SKIP_UTF8(z
25e8a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  2);.    }.  }.  
25e8b 70 31 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  p1 = sqlite3_val
25e8c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b  ue_int(argv[1]);
25e8d 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29  .  if( argc==3 )
25e8e 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c 69 74  {.    p2 = sqlit
25e8f 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67  e3_value_int(arg
25e90 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 70  v[2]);.    if( p
25e91 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32 20  2<0 ){.      p2 
25e92 3d 20 2d 70 32 3b 0a 20 20 20 20 20 20 6e 65 67  = -p2;.      neg
25e93 50 32 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  P2 = 1;.    }.  
25e94 7d 65 6c 73 65 7b 0a 20 20 20 20 70 32 20 3d 20  }else{.    p2 = 
25e95 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
25e96 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
25e97 74 29 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  t)->aLimit[SQLIT
25e98 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b  E_LIMIT_LENGTH];
25e99 0a 20 20 7d 0a 20 20 69 66 28 20 70 31 3c 30 20  .  }.  if( p1<0 
25e9a 29 7b 0a 20 20 20 20 70 31 20 2b 3d 20 6c 65 6e  ){.    p1 += len
25e9b 3b 0a 20 20 20 20 69 66 28 20 70 31 3c 30 20 29  ;.    if( p1<0 )
25e9c 7b 0a 20 20 20 20 20 20 70 32 20 2b 3d 20 70 31  {.      p2 += p1
25e9d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c 30  ;.      if( p2<0
25e9e 20 29 20 70 32 20 3d 20 30 3b 0a 20 20 20 20 20   ) p2 = 0;.     
25e9f 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20   p1 = 0;.    }. 
25ea0 20 7d 65 6c 73 65 20 69 66 28 20 70 31 3e 30 20   }else if( p1>0 
25ea1 29 7b 0a 20 20 20 20 70 31 2d 2d 3b 0a 20 20 7d  ){.    p1--;.  }
25ea2 65 6c 73 65 20 69 66 28 20 70 32 3e 30 20 29 7b  else if( p2>0 ){
25ea3 0a 20 20 20 20 70 32 2d 2d 3b 0a 20 20 7d 0a 20  .    p2--;.  }. 
25ea4 20 69 66 28 20 6e 65 67 50 32 20 29 7b 0a 20 20   if( negP2 ){.  
25ea5 20 20 70 31 20 2d 3d 20 70 32 3b 0a 20 20 20 20    p1 -= p2;.    
25ea6 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20  if( p1<0 ){.    
25ea7 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20 20    p2 += p1;.    
25ea8 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d 0a    p1 = 0;.    }.
25ea9 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 31    }.  assert( p1
25eaa 3e 3d 30 20 26 26 20 70 32 3e 3d 30 20 29 3b 0a  >=0 && p2>=0 );.
25eab 20 20 69 66 28 20 70 31 2b 70 32 3e 6c 65 6e 20    if( p1+p2>len 
25eac 29 7b 0a 20 20 20 20 70 32 20 3d 20 6c 65 6e 2d  ){.    p2 = len-
25ead 70 31 3b 0a 20 20 20 20 69 66 28 20 70 32 3c 30  p1;.    if( p2<0
25eae 20 29 20 70 32 20 3d 20 30 3b 0a 20 20 7d 0a 20   ) p2 = 0;.  }. 
25eaf 20 69 66 28 20 70 30 74 79 70 65 21 3d 53 51 4c   if( p0type!=SQL
25eb0 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20  ITE_BLOB ){.    
25eb1 77 68 69 6c 65 28 20 2a 7a 20 26 26 20 70 31 20  while( *z && p1 
25eb2 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ){.      SQLITE_
25eb3 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20  SKIP_UTF8(z);.  
25eb4 20 20 20 20 70 31 2d 2d 3b 0a 20 20 20 20 7d 0a      p1--;.    }.
25eb5 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a      for(z2=z; *z
25eb6 32 20 26 26 20 70 32 3b 20 70 32 2d 2d 29 7b 0a  2 && p2; p2--){.
25eb7 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
25eb8 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20 20 20 20  P_UTF8(z2);.    
25eb9 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  }.    sqlite3_re
25eba 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
25ebb 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 28 69 6e  t, (char*)z, (in
25ebc 74 29 28 7a 32 2d 7a 29 2c 20 53 51 4c 49 54 45  t)(z2-z), SQLITE
25ebd 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
25ebe 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
25ebf 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f  3_result_blob(co
25ec0 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 26 7a  ntext, (char*)&z
25ec1 5b 70 31 5d 2c 20 28 69 6e 74 29 70 32 2c 20 53  [p1], (int)p2, S
25ec2 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
25ec3 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
25ec4 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
25ec5 20 74 68 65 20 72 6f 75 6e 64 28 29 20 66 75 6e   the round() fun
25ec6 63 74 69 6f 6e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ction.*/.#ifndef
25ec7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
25ec8 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 73 74 61 74  ATING_POINT.stat
25ec9 69 63 20 76 6f 69 64 20 72 6f 75 6e 64 46 75 6e  ic void roundFun
25eca 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
25ecb 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
25ecc 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
25ecd 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 69  lue **argv){.  i
25ece 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 75 62  nt n = 0;.  doub
25ecf 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 42  le r;.  char *zB
25ed0 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72  uf;.  assert( ar
25ed1 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32  gc==1 || argc==2
25ed2 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d   );.  if( argc==
25ed3 32 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c  2 ){.    if( SQL
25ed4 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65  ITE_NULL==sqlite
25ed5 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
25ed6 76 5b 31 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a  v[1]) ) return;.
25ed7 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f      n = sqlite3_
25ed8 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31  value_int(argv[1
25ed9 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 33 30  ]);.    if( n>30
25eda 20 29 20 6e 20 3d 20 33 30 3b 0a 20 20 20 20 69   ) n = 30;.    i
25edb 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a  f( n<0 ) n = 0;.
25edc 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
25edd 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
25ede 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  v[0])==SQLITE_NU
25edf 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 72  LL ) return;.  r
25ee0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
25ee1 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29  _double(argv[0])
25ee2 3b 0a 20 20 7a 42 75 66 20 3d 20 73 71 6c 69 74  ;.  zBuf = sqlit
25ee3 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 2e 2a 66  e3_mprintf("%.*f
25ee4 22 2c 6e 2c 72 29 3b 0a 20 20 69 66 28 20 7a 42  ",n,r);.  if( zB
25ee5 75 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  uf==0 ){.    sql
25ee6 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
25ee7 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29  r_nomem(context)
25ee8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
25ee9 71 6c 69 74 65 33 41 74 6f 46 28 7a 42 75 66 2c  qlite3AtoF(zBuf,
25eea 20 26 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   &r);.    sqlite
25eeb 33 5f 66 72 65 65 28 7a 42 75 66 29 3b 0a 20 20  3_free(zBuf);.  
25eec 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25eed 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c  _double(context,
25eee 20 72 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69   r);.  }.}.#endi
25eef 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  f../*.** Allocat
25ef0 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66  e nByte bytes of
25ef1 20 73 70 61 63 65 20 75 73 69 6e 67 20 73 71 6c   space using sql
25ef2 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49  ite3_malloc(). I
25ef3 66 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74  f the.** allocat
25ef4 69 6f 6e 20 66 61 69 6c 73 2c 20 63 61 6c 6c 20  ion fails, call 
25ef5 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
25ef6 72 72 6f 72 5f 6e 6f 6d 65 6d 28 29 20 74 6f 20  rror_nomem() to 
25ef7 6e 6f 74 69 66 79 0a 2a 2a 20 74 68 65 20 64 61  notify.** the da
25ef8 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68  tabase handle th
25ef9 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20  at malloc() has 
25efa 66 61 69 6c 65 64 20 61 6e 64 20 72 65 74 75 72  failed and retur
25efb 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 6e 42  n NULL..** If nB
25efc 79 74 65 20 69 73 20 6c 61 72 67 65 72 20 74 68  yte is larger th
25efd 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73  an the maximum s
25efe 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 65  tring or blob le
25eff 6e 67 74 68 2c 20 74 68 65 6e 0a 2a 2a 20 72 61  ngth, then.** ra
25f00 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f  ise an SQLITE_TO
25f01 4f 42 49 47 20 65 78 63 65 70 74 69 6f 6e 20 61  OBIG exception a
25f02 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  nd return NULL..
25f03 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
25f04 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 73 71  contextMalloc(sq
25f05 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
25f06 6f 6e 74 65 78 74 2c 20 69 36 34 20 6e 42 79 74  ontext, i64 nByt
25f07 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  e){.  char *z;. 
25f08 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
25f09 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
25f0a 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
25f0b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79  );.  assert( nBy
25f0c 74 65 3e 30 20 29 3b 0a 20 20 74 65 73 74 63 61  te>0 );.  testca
25f0d 73 65 28 20 6e 42 79 74 65 3d 3d 64 62 2d 3e 61  se( nByte==db->a
25f0e 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
25f0f 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20  IT_LENGTH] );.  
25f10 74 65 73 74 63 61 73 65 28 20 6e 42 79 74 65 3d  testcase( nByte=
25f11 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  =db->aLimit[SQLI
25f12 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
25f13 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  +1 );.  if( nByt
25f14 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  e>db->aLimit[SQL
25f15 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
25f16 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
25f17 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  _result_error_to
25f18 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20  obig(context);. 
25f19 20 20 20 7a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73     z = 0;.  }els
25f1a 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  e{.    z = sqlit
25f1b 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29 6e 42  e3Malloc((int)nB
25f1c 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  yte);.    if( !z
25f1d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
25f1e 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  3_result_error_n
25f1f 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20  omem(context);. 
25f20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
25f21 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  n z;.}../*.** Im
25f22 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
25f23 74 68 65 20 75 70 70 65 72 28 29 20 61 6e 64 20  the upper() and 
25f24 6c 6f 77 65 72 28 29 20 53 51 4c 20 66 75 6e 63  lower() SQL func
25f25 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tions..*/.static
25f26 20 76 6f 69 64 20 75 70 70 65 72 46 75 6e 63 28   void upperFunc(
25f27 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
25f28 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72  *context, int ar
25f29 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
25f2a 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63 68 61  e **argv){.  cha
25f2b 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63  r *z1;.  const c
25f2c 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69  har *z2;.  int i
25f2d 2c 20 6e 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  , n;.  UNUSED_PA
25f2e 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
25f2f 20 7a 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c   z2 = (char*)sql
25f30 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
25f31 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20  argv[0]);.  n = 
25f32 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
25f33 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
25f34 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
25f35 68 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65  he call to _byte
25f36 73 28 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76  s() does not inv
25f37 61 6c 69 64 61 74 65 20 74 68 65 20 5f 74 65 78  alidate the _tex
25f38 74 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  t() pointer */. 
25f39 20 61 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68   assert( z2==(ch
25f3a 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
25f3b 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20  e_text(argv[0]) 
25f3c 29 3b 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20  );.  if( z2 ){. 
25f3d 20 20 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d     z1 = contextM
25f3e 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28  alloc(context, (
25f3f 28 69 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20  (i64)n)+1);.    
25f40 69 66 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20  if( z1 ){.      
25f41 6d 65 6d 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e  memcpy(z1, z2, n
25f42 2b 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  +1);.      for(i
25f43 3d 30 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b  =0; z1[i]; i++){
25f44 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d  .        z1[i] =
25f45 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 54 6f   (char)sqlite3To
25f46 75 70 70 65 72 28 7a 31 5b 69 5d 29 3b 0a 20 20  upper(z1[i]);.  
25f47 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
25f48 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
25f49 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 2d 31 2c  context, z1, -1,
25f4a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
25f4b 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74      }.  }.}.stat
25f4c 69 63 20 76 6f 69 64 20 6c 6f 77 65 72 46 75 6e  ic void lowerFun
25f4d 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
25f4e 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
25f4f 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
25f50 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 75  lue **argv){.  u
25f51 38 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63  8 *z1;.  const c
25f52 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69  har *z2;.  int i
25f53 2c 20 6e 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  , n;.  UNUSED_PA
25f54 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
25f55 20 7a 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c   z2 = (char*)sql
25f56 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
25f57 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20  argv[0]);.  n = 
25f58 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
25f59 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
25f5a 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
25f5b 68 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65  he call to _byte
25f5c 73 28 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76  s() does not inv
25f5d 61 6c 69 64 61 74 65 20 74 68 65 20 5f 74 65 78  alidate the _tex
25f5e 74 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  t() pointer */. 
25f5f 20 61 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68   assert( z2==(ch
25f60 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
25f61 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20  e_text(argv[0]) 
25f62 29 3b 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20  );.  if( z2 ){. 
25f63 20 20 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d     z1 = contextM
25f64 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28  alloc(context, (
25f65 28 69 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20  (i64)n)+1);.    
25f66 69 66 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20  if( z1 ){.      
25f67 6d 65 6d 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e  memcpy(z1, z2, n
25f68 2b 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  +1);.      for(i
25f69 3d 30 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b  =0; z1[i]; i++){
25f6a 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d  .        z1[i] =
25f6b 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28   sqlite3Tolower(
25f6c 7a 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  z1[i]);.      }.
25f6d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
25f6e 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
25f6f 74 2c 20 28 63 68 61 72 20 2a 29 7a 31 2c 20 2d  t, (char *)z1, -
25f70 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29  1, sqlite3_free)
25f71 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
25f72 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
25f73 69 6f 6e 20 6f 66 20 74 68 65 20 49 46 4e 55 4c  ion of the IFNUL
25f74 4c 28 29 2c 20 4e 56 4c 28 29 2c 20 61 6e 64 20  L(), NVL(), and 
25f75 43 4f 41 4c 45 53 43 45 28 29 20 66 75 6e 63 74  COALESCE() funct
25f76 69 6f 6e 73 2e 20 20 0a 2a 2a 20 41 6c 6c 20 74  ions.  .** All t
25f77 68 72 65 65 20 64 6f 20 74 68 65 20 73 61 6d 65  hree do the same
25f78 20 74 68 69 6e 67 2e 20 20 54 68 65 79 20 72 65   thing.  They re
25f79 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 20 6e  turn the first n
25f7a 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 61 72 67 75 6d  on-NULL.** argum
25f7b 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ent..*/.static v
25f7c 6f 69 64 20 69 66 6e 75 6c 6c 46 75 6e 63 28 0a  oid ifnullFunc(.
25f7d 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
25f7e 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
25f7f 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
25f80 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
25f81 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72  {.  int i;.  for
25f82 28 69 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b  (i=0; i<argc; i+
25f83 2b 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49  +){.    if( SQLI
25f84 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33  TE_NULL!=sqlite3
25f85 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
25f86 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 73 71  [i]) ){.      sq
25f87 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
25f88 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76  ue(context, argv
25f89 5b 69 5d 29 3b 0a 20 20 20 20 20 20 62 72 65 61  [i]);.      brea
25f8a 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  k;.    }.  }.}..
25f8b 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
25f8c 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 28 29  tion of random()
25f8d 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61 6e 64  .  Return a rand
25f8e 6f 6d 20 69 6e 74 65 67 65 72 2e 20 20 0a 2a 2f  om integer.  .*/
25f8f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 61 6e  .static void ran
25f90 64 6f 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  domFunc(.  sqlit
25f91 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
25f92 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
25f93 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
25f94 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29  lue **NotUsed2.)
25f95 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  {.  sqlite_int64
25f96 20 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   r;.  UNUSED_PAR
25f97 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
25f98 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71   NotUsed2);.  sq
25f99 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
25f9a 28 73 69 7a 65 6f 66 28 72 29 2c 20 26 72 29 3b  (sizeof(r), &r);
25f9b 0a 20 20 69 66 28 20 72 3c 30 20 29 7b 0a 20 20  .  if( r<0 ){.  
25f9c 20 20 2f 2a 20 57 65 20 6e 65 65 64 20 74 6f 20    /* We need to 
25f9d 70 72 65 76 65 6e 74 20 61 20 72 61 6e 64 6f 6d  prevent a random
25f9e 20 6e 75 6d 62 65 72 20 6f 66 20 30 78 38 30 30   number of 0x800
25f9f 30 30 30 30 30 30 30 30 30 30 30 30 30 20 0a 20  0000000000000 . 
25fa0 20 20 20 2a 2a 20 28 6f 72 20 2d 39 32 32 33 33     ** (or -92233
25fa1 37 32 30 33 36 38 35 34 37 37 35 38 30 38 29 20  72036854775808) 
25fa2 73 69 6e 63 65 20 77 68 65 6e 20 79 6f 75 20 64  since when you d
25fa3 6f 20 61 62 73 28 29 20 6f 66 20 74 68 61 74 0a  o abs() of that.
25fa4 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66      ** number of
25fa5 20 79 6f 75 20 67 65 74 20 74 68 65 20 73 61 6d   you get the sam
25fa6 65 20 76 61 6c 75 65 20 62 61 63 6b 20 61 67 61  e value back aga
25fa7 69 6e 2e 20 20 54 6f 20 64 6f 20 74 68 69 73 0a  in.  To do this.
25fa8 20 20 20 20 2a 2a 20 69 6e 20 61 20 77 61 79 20      ** in a way 
25fa9 74 68 61 74 20 69 73 20 74 65 73 74 61 62 6c 65  that is testable
25faa 2c 20 6d 61 73 6b 20 74 68 65 20 73 69 67 6e 20  , mask the sign 
25fab 62 69 74 20 6f 66 66 20 6f 66 20 6e 65 67 61 74  bit off of negat
25fac 69 76 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65  ive.    ** value
25fad 73 2c 20 72 65 73 75 6c 74 69 6e 67 20 69 6e 20  s, resulting in 
25fae 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65  a positive value
25faf 2e 20 20 54 68 65 6e 20 74 61 6b 65 20 74 68 65  .  Then take the
25fb0 20 0a 20 20 20 20 2a 2a 20 32 73 20 63 6f 6d 70   .    ** 2s comp
25fb1 6c 65 6d 65 6e 74 20 6f 66 20 74 68 61 74 20 70  lement of that p
25fb2 6f 73 69 74 69 76 65 20 76 61 6c 75 65 2e 20 20  ositive value.  
25fb3 54 68 65 20 65 6e 64 20 72 65 73 75 6c 74 20 63  The end result c
25fb4 61 6e 0a 20 20 20 20 2a 2a 20 74 68 65 72 65 66  an.    ** theref
25fb5 6f 72 65 20 62 65 20 6e 6f 20 6c 65 73 73 20 74  ore be no less t
25fb6 68 61 6e 20 2d 39 32 32 33 33 37 32 30 33 36 38  han -92233720368
25fb7 35 34 37 37 35 38 30 37 2e 0a 20 20 20 20 2a 2f  54775807..    */
25fb8 0a 20 20 20 20 72 20 3d 20 2d 28 72 20 5e 20 28  .    r = -(r ^ (
25fb9 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  ((sqlite3_int64)
25fba 31 29 3c 3c 36 33 29 29 3b 0a 20 20 7d 0a 20 20  1)<<63));.  }.  
25fbb 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
25fbc 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 72 29  nt64(context, r)
25fbd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
25fbe 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e  mentation of ran
25fbf 64 6f 6d 62 6c 6f 62 28 4e 29 2e 20 20 52 65 74  domblob(N).  Ret
25fc0 75 72 6e 20 61 20 72 61 6e 64 6f 6d 20 62 6c 6f  urn a random blo
25fc1 62 0a 2a 2a 20 74 68 61 74 20 69 73 20 4e 20 62  b.** that is N b
25fc2 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74  ytes long..*/.st
25fc3 61 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d  atic void random
25fc4 42 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f  Blob(.  sqlite3_
25fc5 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
25fc6 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
25fc7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
25fc8 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b  argv.){.  int n;
25fc9 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
25fca 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61   *p;.  assert( a
25fcb 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53  rgc==1 );.  UNUS
25fcc 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
25fcd 63 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65  c);.  n = sqlite
25fce 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
25fcf 5b 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3c 31 20  [0]);.  if( n<1 
25fd0 29 7b 0a 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20  ){.    n = 1;.  
25fd1 7d 0a 20 20 70 20 3d 20 63 6f 6e 74 65 78 74 4d  }.  p = contextM
25fd2 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e  alloc(context, n
25fd3 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  );.  if( p ){.  
25fd4 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
25fd5 6e 65 73 73 28 6e 2c 20 70 29 3b 0a 20 20 20 20  ness(n, p);.    
25fd6 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
25fd7 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68  lob(context, (ch
25fd8 61 72 2a 29 70 2c 20 6e 2c 20 73 71 6c 69 74 65  ar*)p, n, sqlite
25fd9 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a  3_free);.  }.}..
25fda 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
25fdb 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61 73 74  tion of the last
25fdc 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20  _insert_rowid() 
25fdd 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  SQL function.  T
25fde 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c  he return.** val
25fdf 75 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ue is the same a
25fe0 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 61  s the sqlite3_la
25fe1 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
25fe2 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a  ) API function..
25fe3 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  */.static void l
25fe4 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
25fe5 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
25fe6 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20  ext *context, . 
25fe7 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20   int NotUsed, . 
25fe8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
25fe9 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73  *NotUsed2.){.  s
25fea 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
25feb 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
25fec 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
25fed 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
25fee 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
25fef 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74  tUsed2);.  sqlit
25ff0 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
25ff1 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33  context, sqlite3
25ff2 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
25ff3 69 64 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  id(db));.}../*.*
25ff4 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
25ff5 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 28   of the changes(
25ff6 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20  ) SQL function. 
25ff7 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
25ff8 65 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65  e is the.** same
25ff9 20 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f   as the sqlite3_
25ffa 63 68 61 6e 67 65 73 28 29 20 41 50 49 20 66 75  changes() API fu
25ffb 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
25ffc 63 20 76 6f 69 64 20 63 68 61 6e 67 65 73 28 0a  c void changes(.
25ffd 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
25ffe 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
25fff 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
26000 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74  ite3_value **Not
26001 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74  Used2.){.  sqlit
26002 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
26003 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
26004 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55  le(context);.  U
26005 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
26006 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
26007 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  d2);.  sqlite3_r
26008 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78  esult_int(contex
26009 74 2c 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  t, sqlite3_chang
2600a 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  es(db));.}../*.*
2600b 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
2600c 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 5f 63 68   of the total_ch
2600d 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63  anges() SQL func
2600e 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72  tion.  The retur
2600f 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68  n value is.** th
26010 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71  e same as the sq
26011 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
26012 67 65 73 28 29 20 41 50 49 20 66 75 6e 63 74 69  ges() API functi
26013 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
26014 69 64 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  id total_changes
26015 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
26016 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
26017 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
26018 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
26019 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c  otUsed2.){.  sql
2601a 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
2601b 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
2601c 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ndle(context);. 
2601d 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
2601e 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
2601f 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  sed2);.  sqlite3
26020 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
26021 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 74 6f 74  ext, sqlite3_tot
26022 61 6c 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b  al_changes(db));
26023 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75  .}../*.** A stru
26024 63 74 75 72 65 20 64 65 66 69 6e 69 6e 67 20 68  cture defining h
26025 6f 77 20 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74  ow to do GLOB-st
26026 79 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e  yle comparisons.
26027 0a 2a 2f 0a 73 74 72 75 63 74 20 63 6f 6d 70 61  .*/.struct compa
26028 72 65 49 6e 66 6f 20 7b 0a 20 20 75 38 20 6d 61  reInfo {.  u8 ma
26029 74 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74  tchAll;.  u8 mat
2602a 63 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63  chOne;.  u8 matc
2602b 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73  hSet;.  u8 noCas
2602c 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72  e;.};../*.** For
2602d 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 6d   LIKE and GLOB m
2602e 61 74 63 68 69 6e 67 20 6f 6e 20 45 42 43 44 49  atching on EBCDI
2602f 43 20 6d 61 63 68 69 6e 65 73 2c 20 61 73 73 75  C machines, assu
26030 6d 65 20 74 68 61 74 20 65 76 65 72 79 0a 2a 2a  me that every.**
26031 20 63 68 61 72 61 63 74 65 72 20 69 73 20 65 78   character is ex
26032 61 63 74 6c 79 20 6f 6e 65 20 62 79 74 65 20 69  actly one byte i
26033 6e 20 73 69 7a 65 2e 20 20 41 6c 73 6f 2c 20 61  n size.  Also, a
26034 6c 6c 20 63 68 61 72 61 63 74 65 72 73 20 61 72  ll characters ar
26035 65 0a 2a 2a 20 61 62 6c 65 20 74 6f 20 70 61 72  e.** able to par
26036 74 69 63 69 70 61 74 65 20 69 6e 20 75 70 70 65  ticipate in uppe
26037 72 2d 63 61 73 65 2d 74 6f 2d 6c 6f 77 65 72 2d  r-case-to-lower-
26038 63 61 73 65 20 6d 61 70 70 69 6e 67 73 20 69 6e  case mappings in
26039 20 45 42 43 44 49 43 0a 2a 2a 20 77 68 65 72 65   EBCDIC.** where
2603a 61 73 20 6f 6e 6c 79 20 63 68 61 72 61 63 74 65  as only characte
2603b 72 73 20 6c 65 73 73 20 74 68 61 6e 20 30 78 38  rs less than 0x8
2603c 30 20 64 6f 20 69 6e 20 41 53 43 49 49 2e 0a 2a  0 do in ASCII..*
2603d 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
2603e 4c 49 54 45 5f 45 42 43 44 49 43 29 0a 23 20 64  LITE_EBCDIC).# d
2603f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55 74 66  efine sqlite3Utf
26040 38 52 65 61 64 28 41 2c 43 29 20 20 20 20 28 2a  8Read(A,C)    (*
26041 28 41 2b 2b 29 29 0a 23 20 64 65 66 69 6e 65 20  (A++)).# define 
26042 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72  GlogUpperToLower
26043 28 41 29 20 20 20 20 20 41 20 3d 20 73 71 6c 69  (A)     A = sqli
26044 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
26045 41 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  A].#else.# defin
26046 65 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77  e GlogUpperToLow
26047 65 72 28 41 29 20 20 20 20 20 69 66 28 20 41 3c  er(A)     if( A<
26048 30 78 38 30 20 29 7b 20 41 20 3d 20 73 71 6c 69  0x80 ){ A = sqli
26049 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
2604a 41 5d 3b 20 7d 0a 23 65 6e 64 69 66 0a 0a 73 74  A]; }.#endif..st
2604b 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
2604c 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 67 6c  t compareInfo gl
2604d 6f 62 49 6e 66 6f 20 3d 20 7b 20 27 2a 27 2c 20  obInfo = { '*', 
2604e 27 3f 27 2c 20 27 5b 27 2c 20 30 20 7d 3b 0a 2f  '?', '[', 0 };./
2604f 2a 20 54 68 65 20 63 6f 72 72 65 63 74 20 53 51  * The correct SQ
26050 4c 2d 39 32 20 62 65 68 61 76 69 6f 72 20 69 73  L-92 behavior is
26051 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70   for the LIKE op
26052 65 72 61 74 6f 72 20 74 6f 20 69 67 6e 6f 72 65  erator to ignore
26053 0a 2a 2a 20 63 61 73 65 2e 20 20 54 68 75 73 20  .** case.  Thus 
26054 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20 77 6f   'a' LIKE 'A' wo
26055 75 6c 64 20 62 65 20 74 72 75 65 2e 20 2a 2f 0a  uld be true. */.
26056 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72  static const str
26057 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
26058 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 20 3d 20 7b  likeInfoNorm = {
26059 20 27 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20   '%', '_',   0, 
2605a 31 20 7d 3b 0a 2f 2a 20 49 66 20 53 51 4c 49 54  1 };./* If SQLIT
2605b 45 5f 43 41 53 45 5f 53 45 4e 53 49 54 49 56 45  E_CASE_SENSITIVE
2605c 5f 4c 49 4b 45 20 69 73 20 64 65 66 69 6e 65 64  _LIKE is defined
2605d 2c 20 74 68 65 6e 20 74 68 65 20 4c 49 4b 45 20  , then the LIKE 
2605e 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73 20 63  operator.** is c
2605f 61 73 65 20 73 65 6e 73 69 74 69 76 65 20 63 61  ase sensitive ca
26060 75 73 69 6e 67 20 27 61 27 20 4c 49 4b 45 20 27  using 'a' LIKE '
26061 41 27 20 74 6f 20 62 65 20 66 61 6c 73 65 20 2a  A' to be false *
26062 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  /.static const s
26063 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
26064 6f 20 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 20  o likeInfoAlt = 
26065 7b 20 27 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c  { '%', '_',   0,
26066 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d   0 };../*.** Com
26067 70 61 72 65 20 74 77 6f 20 55 54 46 2d 38 20 73  pare two UTF-8 s
26068 74 72 69 6e 67 73 20 66 6f 72 20 65 71 75 61 6c  trings for equal
26069 69 74 79 20 77 68 65 72 65 20 74 68 65 20 66 69  ity where the fi
2606a 72 73 74 20 73 74 72 69 6e 67 20 63 61 6e 0a 2a  rst string can.*
2606b 2a 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65  * potentially be
2606c 20 61 20 22 67 6c 6f 62 22 20 65 78 70 72 65 73   a "glob" expres
2606d 73 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 74 72  sion.  Return tr
2606e 75 65 20 28 31 29 20 69 66 20 74 68 65 79 0a 2a  ue (1) if they.*
2606f 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61  * are the same a
26070 6e 64 20 66 61 6c 73 65 20 28 30 29 20 69 66 20  nd false (0) if 
26071 74 68 65 79 20 61 72 65 20 64 69 66 66 65 72 65  they are differe
26072 6e 74 2e 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62 62 69  nt..**.** Globbi
26073 6e 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  ng rules:.**.** 
26074 20 20 20 20 20 27 2a 27 20 20 20 20 20 20 20 4d       '*'       M
26075 61 74 63 68 65 73 20 61 6e 79 20 73 65 71 75 65  atches any seque
26076 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d  nce of zero or m
26077 6f 72 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a  ore characters..
26078 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 3f 27 20 20  **.**      '?'  
26079 20 20 20 20 20 4d 61 74 63 68 65 73 20 65 78 61       Matches exa
2607a 63 74 6c 79 20 6f 6e 65 20 63 68 61 72 61 63 74  ctly one charact
2607b 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 2e  er..**.**     [.
2607c 2e 2e 5d 20 20 20 20 20 20 4d 61 74 63 68 65 73  ..]      Matches
2607d 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 66   one character f
2607e 72 6f 6d 20 74 68 65 20 65 6e 63 6c 6f 73 65 64  rom the enclosed
2607f 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20   list of.**     
26080 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 61             chara
26081 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  cters..**.**    
26082 20 5b 5e 2e 2e 2e 5d 20 20 20 20 20 4d 61 74 63   [^...]     Matc
26083 68 65 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65  hes one characte
26084 72 20 6e 6f 74 20 69 6e 20 74 68 65 20 65 6e 63  r not in the enc
26085 6c 6f 73 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a  losed list..**.*
26086 2a 20 57 69 74 68 20 74 68 65 20 5b 2e 2e 2e 5d  * With the [...]
26087 20 61 6e 64 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 63   and [^...] matc
26088 68 69 6e 67 2c 20 61 20 27 5d 27 20 63 68 61 72  hing, a ']' char
26089 61 63 74 65 72 20 63 61 6e 20 62 65 20 69 6e 63  acter can be inc
2608a 6c 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20  luded.** in the 
2608b 6c 69 73 74 20 62 79 20 6d 61 6b 69 6e 67 20 69  list by making i
2608c 74 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72  t the first char
2608d 61 63 74 65 72 20 61 66 74 65 72 20 27 5b 27 20  acter after '[' 
2608e 6f 72 20 27 5e 27 2e 20 20 41 0a 2a 2a 20 72 61  or '^'.  A.** ra
2608f 6e 67 65 20 6f 66 20 63 68 61 72 61 63 74 65 72  nge of character
26090 73 20 63 61 6e 20 62 65 20 73 70 65 63 69 66 69  s can be specifi
26091 65 64 20 75 73 69 6e 67 20 27 2d 27 2e 20 20 45  ed using '-'.  E
26092 78 61 6d 70 6c 65 3a 0a 2a 2a 20 22 5b 61 2d 7a  xample:.** "[a-z
26093 5d 22 20 6d 61 74 63 68 65 73 20 61 6e 79 20 73  ]" matches any s
26094 69 6e 67 6c 65 20 6c 6f 77 65 72 2d 63 61 73 65  ingle lower-case
26095 20 6c 65 74 74 65 72 2e 20 20 54 6f 20 6d 61 74   letter.  To mat
26096 63 68 20 61 20 27 2d 27 2c 20 6d 61 6b 65 0a 2a  ch a '-', make.*
26097 2a 20 69 74 20 74 68 65 20 6c 61 73 74 20 63 68  * it the last ch
26098 61 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 6c  aracter in the l
26099 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ist..**.** This 
2609a 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c  routine is usual
2609b 6c 79 20 71 75 69 63 6b 2c 20 62 75 74 20 63 61  ly quick, but ca
2609c 6e 20 62 65 20 4e 2a 2a 32 20 69 6e 20 74 68 65  n be N**2 in the
2609d 20 77 6f 72 73 74 20 63 61 73 65 2e 0a 2a 2a 0a   worst case..**.
2609e 2a 2a 20 48 69 6e 74 73 3a 20 74 6f 20 6d 61 74  ** Hints: to mat
2609f 63 68 20 27 2a 27 20 6f 72 20 27 3f 27 2c 20 70  ch '*' or '?', p
260a0 75 74 20 74 68 65 6d 20 69 6e 20 22 5b 5d 22 2e  ut them in "[]".
260a1 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a    Like this:.**.
260a2 2a 2a 20 20 20 20 20 20 20 20 20 61 62 63 5b 2a  **         abc[*
260a3 5d 78 79 7a 20 20 20 20 20 20 20 20 4d 61 74 63  ]xyz        Matc
260a4 68 65 73 20 22 61 62 63 2a 78 79 7a 22 20 6f 6e  hes "abc*xyz" on
260a5 6c 79 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ly.*/.static int
260a6 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28   patternCompare(
260a7 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 50 61  .  const u8 *zPa
260a8 74 74 65 72 6e 2c 20 20 20 20 20 20 20 20 20 20  ttern,          
260a9 20 20 20 20 2f 2a 20 54 68 65 20 67 6c 6f 62 20      /* The glob 
260aa 70 61 74 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e  pattern */.  con
260ab 73 74 20 75 38 20 2a 7a 53 74 72 69 6e 67 2c 20  st u8 *zString, 
260ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
260ad 20 54 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63   The string to c
260ae 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 74  ompare against t
260af 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e  he glob */.  con
260b0 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72  st struct compar
260b1 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 2c 20 2f 2a  eInfo *pInfo, /*
260b2 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
260b3 75 74 20 68 6f 77 20 74 6f 20 64 6f 20 74 68 65  ut how to do the
260b4 20 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 20 63 6f   compare */.  co
260b5 6e 73 74 20 69 6e 74 20 65 73 63 20 20 20 20 20  nst int esc     
260b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
260b7 2a 20 54 68 65 20 65 73 63 61 70 65 20 63 68 61  * The escape cha
260b8 72 61 63 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69  racter */.){.  i
260b9 6e 74 20 63 2c 20 63 32 3b 0a 20 20 69 6e 74 20  nt c, c2;.  int 
260ba 69 6e 76 65 72 74 3b 0a 20 20 69 6e 74 20 73 65  invert;.  int se
260bb 65 6e 3b 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e  en;.  u8 matchOn
260bc 65 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68  e = pInfo->match
260bd 4f 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68 41  One;.  u8 matchA
260be 6c 6c 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63  ll = pInfo->matc
260bf 68 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68  hAll;.  u8 match
260c0 53 65 74 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74  Set = pInfo->mat
260c1 63 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61  chSet;.  u8 noCa
260c2 73 65 20 3d 20 70 49 6e 66 6f 2d 3e 6e 6f 43 61  se = pInfo->noCa
260c3 73 65 3b 20 0a 20 20 69 6e 74 20 70 72 65 76 45  se; .  int prevE
260c4 73 63 61 70 65 20 3d 20 30 3b 20 20 20 20 20 2f  scape = 0;     /
260c5 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 70 72  * True if the pr
260c6 65 76 69 6f 75 73 20 63 68 61 72 61 63 74 65 72  evious character
260c7 20 77 61 73 20 27 65 73 63 61 70 65 27 20 2a 2f   was 'escape' */
260c8 0a 0a 20 20 77 68 69 6c 65 28 20 28 63 20 3d 20  ..  while( (c = 
260c9 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
260ca 7a 50 61 74 74 65 72 6e 2c 26 7a 50 61 74 74 65  zPattern,&zPatte
260cb 72 6e 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69  rn))!=0 ){.    i
260cc 66 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26  f( !prevEscape &
260cd 26 20 63 3d 3d 6d 61 74 63 68 41 6c 6c 20 29 7b  & c==matchAll ){
260ce 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63  .      while( (c
260cf 3d 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64  =sqlite3Utf8Read
260d0 28 7a 50 61 74 74 65 72 6e 2c 26 7a 50 61 74 74  (zPattern,&zPatt
260d1 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63 68 41 6c  ern)) == matchAl
260d2 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l.              
260d3 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63 68 4f 6e   || c == matchOn
260d4 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  e ){.        if(
260d5 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 26 26 20   c==matchOne && 
260d6 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
260d7 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e  zString, &zStrin
260d8 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  g)==0 ){.       
260d9 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
260da 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
260db 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b       if( c==0 ){
260dc 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
260dd 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  1;.      }else i
260de 66 28 20 63 3d 3d 65 73 63 20 29 7b 0a 20 20 20  f( c==esc ){.   
260df 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33       c = sqlite3
260e0 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
260e1 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  n, &zPattern);. 
260e2 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20         if( c==0 
260e3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
260e4 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  urn 0;.        }
260e5 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
260e6 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a   c==matchSet ){.
260e7 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
260e8 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 20 20 20  esc==0 );       
260e9 20 20 2f 2a 20 54 68 69 73 20 69 73 20 47 4c 4f    /* This is GLO
260ea 42 2c 20 6e 6f 74 20 4c 49 4b 45 20 2a 2f 0a 20  B, not LIKE */. 
260eb 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d         assert( m
260ec 61 74 63 68 53 65 74 3c 30 78 38 30 20 29 3b 20  atchSet<0x80 ); 
260ed 20 2f 2a 20 27 5b 27 20 69 73 20 61 20 73 69 6e   /* '[' is a sin
260ee 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61 63 74  gle-byte charact
260ef 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68  er */.        wh
260f0 69 6c 65 28 20 2a 7a 53 74 72 69 6e 67 20 26 26  ile( *zString &&
260f1 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28   patternCompare(
260f2 26 7a 50 61 74 74 65 72 6e 5b 2d 31 5d 2c 7a 53  &zPattern[-1],zS
260f3 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73 63 29  tring,pInfo,esc)
260f4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
260f5 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
260f6 38 28 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20  8(zString);.    
260f7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65      }.        re
260f8 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 21 3d 30  turn *zString!=0
260f9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
260fa 77 68 69 6c 65 28 20 28 63 32 20 3d 20 73 71 6c  while( (c2 = sql
260fb 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74  ite3Utf8Read(zSt
260fc 72 69 6e 67 2c 26 7a 53 74 72 69 6e 67 29 29 21  ring,&zString))!
260fd 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  =0 ){.        if
260fe 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20  ( noCase ){.    
260ff 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54        GlogUpperT
26100 6f 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20 20 20  oLower(c2);.    
26101 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54        GlogUpperT
26102 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20  oLower(c);.     
26103 20 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 21       while( c2 !
26104 3d 20 30 20 26 26 20 63 32 20 21 3d 20 63 20 29  = 0 && c2 != c )
26105 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 32  {.            c2
26106 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
26107 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74  ad(zString, &zSt
26108 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20  ring);.         
26109 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f     GlogUpperToLo
2610a 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20 20 20  wer(c2);.       
2610b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c     }.        }el
2610c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68  se{.          wh
2610d 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26 26 20  ile( c2 != 0 && 
2610e 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20  c2 != c ){.     
2610f 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69         c2 = sqli
26110 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72  te3Utf8Read(zStr
26111 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a  ing, &zString);.
26112 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
26113 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
26114 28 20 63 32 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( c2==0 ) return
26115 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   0;.        if( 
26116 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a  patternCompare(z
26117 50 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e 67 2c  Pattern,zString,
26118 70 49 6e 66 6f 2c 65 73 63 29 20 29 20 72 65 74  pInfo,esc) ) ret
26119 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
2611a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
2611b 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72     }else if( !pr
2611c 65 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d  evEscape && c==m
2611d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20  atchOne ){.     
2611e 20 69 66 28 20 73 71 6c 69 74 65 33 55 74 66 38   if( sqlite3Utf8
2611f 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a  Read(zString, &z
26120 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20 20  String)==0 ){.  
26121 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
26122 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
26123 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 53 65  e if( c==matchSe
26124 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70  t ){.      int p
26125 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20 20  rior_c = 0;.    
26126 20 20 61 73 73 65 72 74 28 20 65 73 63 3d 3d 30    assert( esc==0
26127 20 29 3b 20 20 20 20 2f 2a 20 54 68 69 73 20 6f   );    /* This o
26128 6e 6c 79 20 6f 63 63 75 72 73 20 66 6f 72 20 47  nly occurs for G
26129 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20 2a 2f  LOB, not LIKE */
2612a 0a 20 20 20 20 20 20 73 65 65 6e 20 3d 20 30 3b  .      seen = 0;
2612b 0a 20 20 20 20 20 20 69 6e 76 65 72 74 20 3d 20  .      invert = 
2612c 30 3b 0a 20 20 20 20 20 20 63 20 3d 20 73 71 6c  0;.      c = sql
2612d 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74  ite3Utf8Read(zSt
2612e 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3b  ring, &zString);
2612f 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20  .      if( c==0 
26130 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
26131 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74    c2 = sqlite3Ut
26132 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c  f8Read(zPattern,
26133 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20   &zPattern);.   
26134 20 20 20 69 66 28 20 63 32 3d 3d 27 5e 27 20 29     if( c2=='^' )
26135 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 65 72 74  {.        invert
26136 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63 32   = 1;.        c2
26137 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
26138 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50  ad(zPattern, &zP
26139 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d  attern);.      }
2613a 0a 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27  .      if( c2=='
2613b 5d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ]' ){.        if
2613c 28 20 63 3d 3d 27 5d 27 20 29 20 73 65 65 6e 20  ( c==']' ) seen 
2613d 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63 32 20  = 1;.        c2 
2613e 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
2613f 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61  d(zPattern, &zPa
26140 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  ttern);.      }.
26141 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 32 20        while( c2 
26142 26 26 20 63 32 21 3d 27 5d 27 20 29 7b 0a 20 20  && c2!=']' ){.  
26143 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27 2d        if( c2=='-
26144 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d  ' && zPattern[0]
26145 21 3d 27 5d 27 20 26 26 20 7a 50 61 74 74 65 72  !=']' && zPatter
26146 6e 5b 30 5d 21 3d 30 20 26 26 20 70 72 69 6f 72  n[0]!=0 && prior
26147 5f 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  _c>0 ){.        
26148 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74    c2 = sqlite3Ut
26149 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c  f8Read(zPattern,
2614a 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20   &zPattern);.   
2614b 20 20 20 20 20 20 20 69 66 28 20 63 3e 3d 70 72         if( c>=pr
2614c 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63 32 20 29  ior_c && c<=c2 )
2614d 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20   seen = 1;.     
2614e 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 30       prior_c = 0
2614f 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
26150 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63  .          if( c
26151 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20 20 20 20  ==c2 ){.        
26152 20 20 20 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20      seen = 1;.  
26153 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
26154 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 63 32      prior_c = c2
26155 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
26156 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33      c2 = sqlite3
26157 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
26158 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  n, &zPattern);. 
26159 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2615a 20 63 32 3d 3d 30 20 7c 7c 20 28 73 65 65 6e 20   c2==0 || (seen 
2615b 5e 20 69 6e 76 65 72 74 29 3d 3d 30 20 29 7b 0a  ^ invert)==0 ){.
2615c 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
2615d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2615e 6c 73 65 20 69 66 28 20 65 73 63 3d 3d 63 20 26  lse if( esc==c &
2615f 26 20 21 70 72 65 76 45 73 63 61 70 65 20 29 7b  & !prevEscape ){
26160 0a 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70  .      prevEscap
26161 65 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  e = 1;.    }else
26162 7b 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c  {.      c2 = sql
26163 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74  ite3Utf8Read(zSt
26164 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3b  ring, &zString);
26165 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73  .      if( noCas
26166 65 20 29 7b 0a 20 20 20 20 20 20 20 20 47 6c 6f  e ){.        Glo
26167 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 29  gUpperToLower(c)
26168 3b 0a 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70  ;.        GlogUp
26169 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a  perToLower(c2);.
2616a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2616b 28 20 63 21 3d 63 32 20 29 7b 0a 20 20 20 20 20  ( c!=c2 ){.     
2616c 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
2616d 20 20 20 7d 0a 20 20 20 20 20 20 70 72 65 76 45     }.      prevE
2616e 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 20 20 7d  scape = 0;.    }
2616f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  .  }.  return *z
26170 53 74 72 69 6e 67 3d 3d 30 3b 0a 7d 0a 0a 2f 2a  String==0;.}../*
26171 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75  .** Count the nu
26172 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
26173 61 74 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72  at the LIKE oper
26174 61 74 6f 72 20 28 6f 72 20 47 4c 4f 42 20 77 68  ator (or GLOB wh
26175 69 63 68 20 69 73 0a 2a 2a 20 6a 75 73 74 20 61  ich is.** just a
26176 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20 4c 49   variation of LI
26177 4b 45 29 20 67 65 74 73 20 63 61 6c 6c 65 64 2e  KE) gets called.
26178 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66    This is used f
26179 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 6f 6e  or testing.** on
2617a 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ly..*/.#ifdef SQ
2617b 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
2617c 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2617d 5f 6c 69 6b 65 5f 63 6f 75 6e 74 20 3d 20 30 3b  _like_count = 0;
2617e 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
2617f 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
26180 66 20 74 68 65 20 6c 69 6b 65 28 29 20 53 51 4c  f the like() SQL
26181 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73   function.  This
26182 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d   function implem
26183 65 6e 74 73 0a 2a 2a 20 74 68 65 20 62 75 69 6c  ents.** the buil
26184 64 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 72 61 74  d-in LIKE operat
26185 6f 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 61  or.  The first a
26186 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66  rgument to the f
26187 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 0a 2a  unction is the.*
26188 2a 20 70 61 74 74 65 72 6e 20 61 6e 64 20 74 68  * pattern and th
26189 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
2618a 74 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 2e  t is the string.
2618b 20 20 53 6f 2c 20 74 68 65 20 53 51 4c 20 73 74    So, the SQL st
2618c 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20  atements:.**.** 
2618d 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42 0a 2a        A LIKE B.*
2618e 2a 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e  *.** is implemen
2618f 74 65 64 20 61 73 20 6c 69 6b 65 28 42 2c 41 29  ted as like(B,A)
26190 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 61 6d  ..**.** This sam
26191 65 20 66 75 6e 63 74 69 6f 6e 20 28 77 69 74 68  e function (with
26192 20 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6d   a different com
26193 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75  pareInfo structu
26194 72 65 29 20 63 6f 6d 70 75 74 65 73 0a 2a 2a 20  re) computes.** 
26195 74 68 65 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f  the GLOB operato
26196 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
26197 64 20 6c 69 6b 65 46 75 6e 63 28 0a 20 20 73 71  d likeFunc(.  sq
26198 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
26199 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20 61  ontext, .  int a
2619a 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  rgc, .  sqlite3_
2619b 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
2619c 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
2619d 20 63 68 61 72 20 2a 7a 41 2c 20 2a 7a 42 3b 0a   char *zA, *zB;.
2619e 20 20 69 6e 74 20 65 73 63 61 70 65 20 3d 20 30    int escape = 0
2619f 3b 0a 20 20 69 6e 74 20 6e 50 61 74 3b 0a 20 20  ;.  int nPat;.  
261a0 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
261a1 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
261a2 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
261a3 3b 0a 0a 20 20 7a 42 20 3d 20 73 71 6c 69 74 65  ;..  zB = sqlite
261a4 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
261a5 76 5b 30 5d 29 3b 0a 20 20 7a 41 20 3d 20 73 71  v[0]);.  zA = sq
261a6 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
261a7 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 2f 2a  (argv[1]);..  /*
261a8 20 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e 67 74   Limit the lengt
261a9 68 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f 72  h of the LIKE or
261aa 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f   GLOB pattern to
261ab 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a   avoid problems.
261ac 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72 65 63    ** of deep rec
261ad 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62  ursion and N*N b
261ae 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74 74 65  ehavior in patte
261af 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a  rnCompare()..  *
261b0 2f 0a 20 20 6e 50 61 74 20 3d 20 73 71 6c 69 74  /.  nPat = sqlit
261b1 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
261b2 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65 73 74 63  rgv[0]);.  testc
261b3 61 73 65 28 20 6e 50 61 74 3d 3d 64 62 2d 3e 61  ase( nPat==db->a
261b4 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
261b5 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  IT_LIKE_PATTERN_
261b6 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74 65 73  LENGTH] );.  tes
261b7 74 63 61 73 65 28 20 6e 50 61 74 3d 3d 64 62 2d  tcase( nPat==db-
261b8 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
261b9 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52  IMIT_LIKE_PATTER
261ba 4e 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20  N_LENGTH]+1 );. 
261bb 20 69 66 28 20 6e 50 61 74 20 3e 20 64 62 2d 3e   if( nPat > db->
261bc 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
261bd 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MIT_LIKE_PATTERN
261be 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
261bf 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
261c0 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c  rror(context, "L
261c1 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74  IKE or GLOB patt
261c2 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22  ern too complex"
261c3 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72  , -1);.    retur
261c4 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
261c5 20 7a 42 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c   zB==sqlite3_val
261c6 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
261c7 20 29 3b 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67   );  /* Encoding
261c8 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67 65 20   did not change 
261c9 2a 2f 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  */..  if( argc==
261ca 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  3 ){.    /* The 
261cb 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72  escape character
261cc 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e   string must con
261cd 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65  sist of a single
261ce 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
261cf 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69  ..    ** Otherwi
261d0 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  se, return an er
261d1 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ror..    */.    
261d2 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
261d3 68 61 72 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69  har *zEsc = sqli
261d4 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
261d5 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[2]);.    if(
261d6 20 7a 45 73 63 3d 3d 30 20 29 20 72 65 74 75 72   zEsc==0 ) retur
261d7 6e 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  n;.    if( sqlit
261d8 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 28 63  e3Utf8CharLen((c
261d9 68 61 72 2a 29 7a 45 73 63 2c 20 2d 31 29 21 3d  har*)zEsc, -1)!=
261da 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  1 ){.      sqlit
261db 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
261dc 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20  context, .      
261dd 20 20 20 20 22 45 53 43 41 50 45 20 65 78 70 72      "ESCAPE expr
261de 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61  ession must be a
261df 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65   single characte
261e0 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72  r", -1);.      r
261e1 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
261e2 20 65 73 63 61 70 65 20 3d 20 73 71 6c 69 74 65   escape = sqlite
261e3 33 55 74 66 38 52 65 61 64 28 7a 45 73 63 2c 20  3Utf8Read(zEsc, 
261e4 26 7a 45 73 63 29 3b 0a 20 20 7d 0a 20 20 69 66  &zEsc);.  }.  if
261e5 28 20 7a 41 20 26 26 20 7a 42 20 29 7b 0a 20 20  ( zA && zB ){.  
261e6 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65    struct compare
261e7 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 73 71  Info *pInfo = sq
261e8 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
261e9 63 6f 6e 74 65 78 74 29 3b 0a 23 69 66 64 65 66  context);.#ifdef
261ea 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20   SQLITE_TEST.   
261eb 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f   sqlite3_like_co
261ec 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20  unt++;.#endif.  
261ed 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72    .    sqlite3_r
261ee 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78  esult_int(contex
261ef 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72  t, patternCompar
261f0 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e 66 6f 2c  e(zB, zA, pInfo,
261f1 20 65 73 63 61 70 65 29 29 3b 0a 20 20 7d 0a 7d   escape));.  }.}
261f2 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
261f3 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4e 55  tation of the NU
261f4 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e 63 74 69  LLIF(x,y) functi
261f5 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20  on.  The result 
261f6 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  is the first.** 
261f7 61 72 67 75 6d 65 6e 74 20 69 66 20 74 68 65 20  argument if the 
261f8 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 64 69  arguments are di
261f9 66 66 65 72 65 6e 74 2e 20 20 54 68 65 20 72 65  fferent.  The re
261fa 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 69 66 20  sult is NULL if 
261fb 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73  the.** arguments
261fc 20 61 72 65 20 65 71 75 61 6c 20 74 6f 20 65 61   are equal to ea
261fd 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 73 74 61  ch other..*/.sta
261fe 74 69 63 20 76 6f 69 64 20 6e 75 6c 6c 69 66 46  tic void nullifF
261ff 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
26200 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
26201 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
26202 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
26203 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 43 6f 6c 6c  **argv.){.  Coll
26204 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c  Seq *pColl = sql
26205 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
26206 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55  eq(context);.  U
26207 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
26208 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20  NotUsed);.  if( 
26209 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72  sqlite3MemCompar
2620a 65 28 61 72 67 76 5b 30 5d 2c 20 61 72 67 76 5b  e(argv[0], argv[
2620b 31 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30 20 29 7b  1], pColl)!=0 ){
2620c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
2620d 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78  ult_value(contex
2620e 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7d  t, argv[0]);.  }
2620f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
26210 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
26211 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e 28 29  sqlite_version()
26212 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
26213 72 65 73 75 6c 74 20 69 73 20 74 68 65 20 76 65  result is the ve
26214 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  rsion.** of the 
26215 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 74  SQLite library t
26216 68 61 74 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a  hat is running..
26217 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76  */.static void v
26218 65 72 73 69 6f 6e 46 75 6e 63 28 0a 20 20 73 71  ersionFunc(.  sq
26219 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2621a 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f  ontext,.  int No
2621b 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  tUsed,.  sqlite3
2621c 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64  _value **NotUsed
2621d 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  2.){.  UNUSED_PA
2621e 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
2621f 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73  , NotUsed2);.  s
26220 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
26221 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69  xt(context, sqli
26222 74 65 33 5f 76 65 72 73 69 6f 6e 2c 20 2d 31 2c  te3_version, -1,
26223 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
26224 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
26225 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
26226 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64  sqlite_source_id
26227 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65  () function. The
26228 20 72 65 73 75 6c 74 20 69 73 20 61 20 73 74 72   result is a str
26229 69 6e 67 0a 2a 2a 20 74 68 61 74 20 69 64 65 6e  ing.** that iden
2622a 74 69 66 69 65 73 20 74 68 65 20 70 61 72 74 69  tifies the parti
2622b 63 75 6c 61 72 20 76 65 72 73 69 6f 6e 20 6f 66  cular version of
2622c 20 74 68 65 20 73 6f 75 72 63 65 20 63 6f 64 65   the source code
2622d 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 0a 2a   used to build.*
2622e 2a 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 74 61  * SQLite..*/.sta
2622f 74 69 63 20 76 6f 69 64 20 73 6f 75 72 63 65 69  tic void sourcei
26230 64 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  dFunc(.  sqlite3
26231 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
26232 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
26233 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
26234 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a  e **NotUsed2.){.
26235 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
26236 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74  ER2(NotUsed, Not
26237 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65  Used2);.  sqlite
26238 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
26239 6e 74 65 78 74 2c 20 53 51 4c 49 54 45 5f 53 4f  ntext, SQLITE_SO
2623a 55 52 43 45 5f 49 44 2c 20 2d 31 2c 20 53 51 4c  URCE_ID, -1, SQL
2623b 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a  ITE_STATIC);.}..
2623c 2f 2a 20 41 72 72 61 79 20 66 6f 72 20 63 6f 6e  /* Array for con
2623d 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 68 61 6c  verting from hal
2623e 66 2d 62 79 74 65 73 20 28 6e 79 62 62 6c 65 73  f-bytes (nybbles
2623f 29 20 69 6e 74 6f 20 41 53 43 49 49 20 68 65 78  ) into ASCII hex
26240 0a 2a 2a 20 64 69 67 69 74 73 2e 20 2a 2f 0a 73  .** digits. */.s
26241 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
26242 20 68 65 78 64 69 67 69 74 73 5b 5d 20 3d 20 7b   hexdigits[] = {
26243 0a 20 20 27 30 27 2c 20 27 31 27 2c 20 27 32 27  .  '0', '1', '2'
26244 2c 20 27 33 27 2c 20 27 34 27 2c 20 27 35 27 2c  , '3', '4', '5',
26245 20 27 36 27 2c 20 27 37 27 2c 0a 20 20 27 38 27   '6', '7',.  '8'
26246 2c 20 27 39 27 2c 20 27 41 27 2c 20 27 42 27 2c  , '9', 'A', 'B',
26247 20 27 43 27 2c 20 27 44 27 2c 20 27 45 27 2c 20   'C', 'D', 'E', 
26248 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45  'F' .};../*.** E
26249 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 54 68  XPERIMENTAL - Th
2624a 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 6f 66 66  is is not an off
2624b 69 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e 2e 20  icial function. 
2624c 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d   The interface m
2624d 61 79 0a 2a 2a 20 63 68 61 6e 67 65 2e 20 20 54  ay.** change.  T
2624e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
2624f 20 64 69 73 61 70 70 65 61 72 2e 20 20 44 6f 20   disappear.  Do 
26250 6e 6f 74 20 77 72 69 74 65 20 63 6f 64 65 20 74  not write code t
26251 68 61 74 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f  hat depends.** o
26252 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  n this function.
26253 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  .**.** Implement
26254 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 51 55 4f  ation of the QUO
26255 54 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  TE() function.  
26256 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 61  This function ta
26257 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  kes a single.** 
26258 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68  argument.  If th
26259 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 75  e argument is nu
2625a 6d 65 72 69 63 2c 20 74 68 65 20 72 65 74 75 72  meric, the retur
2625b 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73  n value is the s
2625c 61 6d 65 20 61 73 0a 2a 2a 20 74 68 65 20 61 72  ame as.** the ar
2625d 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20  gument.  If the 
2625e 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c 4c  argument is NULL
2625f 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
26260 75 65 20 69 73 20 74 68 65 20 73 74 72 69 6e 67  ue is the string
26261 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20 4f 74 68  .** "NULL".  Oth
26262 65 72 77 69 73 65 2c 20 74 68 65 20 61 72 67 75  erwise, the argu
26263 6d 65 6e 74 20 69 73 20 65 6e 63 6c 6f 73 65 64  ment is enclosed
26264 20 69 6e 20 73 69 6e 67 6c 65 20 71 75 6f 74 65   in single quote
26265 73 20 77 69 74 68 0a 2a 2a 20 73 69 6e 67 6c 65  s with.** single
26266 2d 71 75 6f 74 65 20 65 73 63 61 70 65 73 2e 0a  -quote escapes..
26267 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 71  */.static void q
26268 75 6f 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33  uoteFunc(sqlite3
26269 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
2626a 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
2626b 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
2626c 76 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61 72  v){.  assert( ar
2626d 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45  gc==1 );.  UNUSE
2626e 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
2626f 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c  );.  switch( sql
26270 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
26271 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20  argv[0]) ){.    
26272 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  case SQLITE_INTE
26273 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 53 51  GER:.    case SQ
26274 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20  LITE_FLOAT: {.  
26275 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
26276 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74  lt_value(context
26277 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20  , argv[0]);.    
26278 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
26279 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42     case SQLITE_B
2627a 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 63 68 61  LOB: {.      cha
2627b 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20  r *zText = 0;.  
2627c 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a      char const *
2627d 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f  zBlob = sqlite3_
2627e 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
2627f 30 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  0]);.      int n
26280 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 76  Blob = sqlite3_v
26281 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
26282 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0]);.      asser
26283 74 28 20 7a 42 6c 6f 62 3d 3d 73 71 6c 69 74 65  t( zBlob==sqlite
26284 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
26285 76 5b 30 5d 29 20 29 3b 20 2f 2a 20 4e 6f 20 65  v[0]) ); /* No e
26286 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a  ncoding change *
26287 2f 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d 20  /.      zText = 
26288 28 63 68 61 72 20 2a 29 63 6f 6e 74 65 78 74 4d  (char *)contextM
26289 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28  alloc(context, (
2628a 32 2a 28 69 36 34 29 6e 42 6c 6f 62 29 2b 34 29  2*(i64)nBlob)+4)
2628b 3b 20 0a 20 20 20 20 20 20 69 66 28 20 7a 54 65  ; .      if( zTe
2628c 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  xt ){.        in
2628d 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  t i;.        for
2628e 28 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62 3b 20 69  (i=0; i<nBlob; i
2628f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  ++){.          z
26290 54 65 78 74 5b 28 69 2a 32 29 2b 32 5d 20 3d 20  Text[(i*2)+2] = 
26291 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62  hexdigits[(zBlob
26292 5b 69 5d 3e 3e 34 29 26 30 78 30 46 5d 3b 0a 20  [i]>>4)&0x0F];. 
26293 20 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28           zText[(
26294 69 2a 32 29 2b 33 5d 20 3d 20 68 65 78 64 69 67  i*2)+3] = hexdig
26295 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 29 26 30  its[(zBlob[i])&0
26296 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  x0F];.        }.
26297 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 6e          zText[(n
26298 42 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27 5c 27  Blob*2)+2] = '\'
26299 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74  ';.        zText
2629a 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20 3d 20  [(nBlob*2)+3] = 
2629b 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7a 54  '\0';.        zT
2629c 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0a 20 20  ext[0] = 'X';.  
2629d 20 20 20 20 20 20 7a 54 65 78 74 5b 31 5d 20 3d        zText[1] =
2629e 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 73   '\'';.        s
2629f 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
262a0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 54 65 78  xt(context, zTex
262a1 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52  t, -1, SQLITE_TR
262a2 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20  ANSIENT);.      
262a3 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
262a4 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Text);.      }. 
262a5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
262a6 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
262a7 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20  E_TEXT: {.      
262a8 69 6e 74 20 69 2c 6a 3b 0a 20 20 20 20 20 20 75  int i,j;.      u
262a9 36 34 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73  64 n;.      cons
262aa 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
262ab 2a 7a 41 72 67 20 3d 20 73 71 6c 69 74 65 33 5f  *zArg = sqlite3_
262ac 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
262ad 30 5d 29 3b 0a 20 20 20 20 20 20 63 68 61 72 20  0]);.      char 
262ae 2a 7a 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 7a  *z;..      if( z
262af 41 72 67 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Arg==0 ) return;
262b0 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  .      for(i=0, 
262b1 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b  n=0; zArg[i]; i+
262b2 2b 29 7b 20 69 66 28 20 7a 41 72 67 5b 69 5d 3d  +){ if( zArg[i]=
262b3 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d 0a 20  ='\'' ) n++; }. 
262b4 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65 78 74       z = context
262b5 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20  Malloc(context, 
262b6 28 28 69 36 34 29 69 29 2b 28 28 69 36 34 29 6e  ((i64)i)+((i64)n
262b7 29 2b 33 29 3b 0a 20 20 20 20 20 20 69 66 28 20  )+3);.      if( 
262b8 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b 30  z ){.        z[0
262b9 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20  ] = '\'';.      
262ba 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31 3b 20    for(i=0, j=1; 
262bb 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  zArg[i]; i++){. 
262bc 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20           z[j++] 
262bd 3d 20 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 20 20  = zArg[i];.     
262be 20 20 20 20 20 69 66 28 20 7a 41 72 67 5b 69 5d       if( zArg[i]
262bf 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20  =='\'' ){.      
262c0 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27        z[j++] = '
262c1 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  \'';.          }
262c2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
262c3 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27     z[j++] = '\''
262c4 3b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 5d 20 3d  ;.        z[j] =
262c5 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   0;.        sqli
262c6 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
262c7 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c 20 73  context, z, j, s
262c8 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
262c9 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
262ca 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
262cb 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73  ault: {.      as
262cc 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76 61  sert( sqlite3_va
262cd 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
262ce 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )==SQLITE_NULL )
262cf 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
262d0 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
262d1 65 78 74 2c 20 22 4e 55 4c 4c 22 2c 20 34 2c 20  ext, "NULL", 4, 
262d2 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
262d3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
262d4 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
262d5 54 68 65 20 68 65 78 28 29 20 66 75 6e 63 74 69  The hex() functi
262d6 6f 6e 2e 20 20 49 6e 74 65 72 70 72 65 74 20 74  on.  Interpret t
262d7 68 65 20 61 72 67 75 6d 65 6e 74 20 61 73 20 61  he argument as a
262d8 20 62 6c 6f 62 2e 20 20 52 65 74 75 72 6e 0a 2a   blob.  Return.*
262d9 2a 20 61 20 68 65 78 61 64 65 63 69 6d 61 6c 20  * a hexadecimal 
262da 72 65 6e 64 65 72 69 6e 67 20 61 73 20 74 65 78  rendering as tex
262db 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
262dc 64 20 68 65 78 46 75 6e 63 28 0a 20 20 73 71 6c  d hexFunc(.  sql
262dd 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
262de 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
262df 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
262e0 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69  ue **argv.){.  i
262e1 6e 74 20 69 2c 20 6e 3b 0a 20 20 63 6f 6e 73 74  nt i, n;.  const
262e2 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
262e3 70 42 6c 6f 62 3b 0a 20 20 63 68 61 72 20 2a 7a  pBlob;.  char *z
262e4 48 65 78 2c 20 2a 7a 3b 0a 20 20 61 73 73 65 72  Hex, *z;.  asser
262e5 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20  t( argc==1 );.  
262e6 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
262e7 28 61 72 67 63 29 3b 0a 20 20 70 42 6c 6f 62 20  (argc);.  pBlob 
262e8 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
262e9 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20  blob(argv[0]);. 
262ea 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
262eb 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
262ec 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 6c  );.  assert( pBl
262ed 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75  ob==sqlite3_valu
262ee 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20  e_blob(argv[0]) 
262ef 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69  );  /* No encodi
262f0 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a  ng change */.  z
262f1 20 3d 20 7a 48 65 78 20 3d 20 63 6f 6e 74 65 78   = zHex = contex
262f2 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
262f3 20 28 28 69 36 34 29 6e 29 2a 32 20 2b 20 31 29   ((i64)n)*2 + 1)
262f4 3b 0a 20 20 69 66 28 20 7a 48 65 78 20 29 7b 0a  ;.  if( zHex ){.
262f5 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
262f6 3b 20 69 2b 2b 2c 20 70 42 6c 6f 62 2b 2b 29 7b  ; i++, pBlob++){
262f7 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
262f8 63 68 61 72 20 63 20 3d 20 2a 70 42 6c 6f 62 3b  char c = *pBlob;
262f9 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20  .      *(z++) = 
262fa 68 65 78 64 69 67 69 74 73 5b 28 63 3e 3e 34 29  hexdigits[(c>>4)
262fb 26 30 78 66 5d 3b 0a 20 20 20 20 20 20 2a 28 7a  &0xf];.      *(z
262fc 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b  ++) = hexdigits[
262fd 63 26 30 78 66 5d 3b 0a 20 20 20 20 7d 0a 20 20  c&0xf];.    }.  
262fe 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 20 20 73 71    *z = 0;.    sq
262ff 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
26300 74 28 63 6f 6e 74 65 78 74 2c 20 7a 48 65 78 2c  t(context, zHex,
26301 20 6e 2a 32 2c 20 73 71 6c 69 74 65 33 5f 66 72   n*2, sqlite3_fr
26302 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ee);.  }.}../*.*
26303 2a 20 54 68 65 20 7a 65 72 6f 62 6c 6f 62 28 4e  * The zeroblob(N
26304 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  ) function retur
26305 6e 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  ns a zero-filled
26306 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 20 4e 20   blob of size N 
26307 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  bytes..*/.static
26308 20 76 6f 69 64 20 7a 65 72 6f 62 6c 6f 62 46 75   void zeroblobFu
26309 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
2630a 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
2630b 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
2630c 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
2630d 67 76 0a 29 7b 0a 20 20 69 36 34 20 6e 3b 0a 20  gv.){.  i64 n;. 
2630e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
2630f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
26310 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
26311 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  );.  assert( arg
26312 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==1 );.  UNUSED
26313 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29  _PARAMETER(argc)
26314 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  n = sqlite3_
26315 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76  value_int64(argv
26316 5b 30 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65  [0]);.  testcase
26317 28 20 6e 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b  ( n==db->aLimit[
26318 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
26319 47 54 48 5d 20 29 3b 0a 20 20 74 65 73 74 63 61  GTH] );.  testca
2631a 73 65 28 20 6e 3d 3d 64 62 2d 3e 61 4c 69 6d 69  se( n==db->aLimi
2631b 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
2631c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 69 66  ENGTH]+1 );.  if
2631d 28 20 6e 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  ( n>db->aLimit[S
2631e 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
2631f 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  TH] ){.    sqlit
26320 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
26321 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b  toobig(context);
26322 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
26323 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72  lite3_result_zer
26324 6f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28  oblob(context, (
26325 69 6e 74 29 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  int)n);.  }.}../
26326 2a 0a 2a 2a 20 54 68 65 20 72 65 70 6c 61 63 65  *.** The replace
26327 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  () function.  Th
26328 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72  ree arguments ar
26329 65 20 61 6c 6c 20 73 74 72 69 6e 67 73 3a 20 63  e all strings: c
2632a 61 6c 6c 0a 2a 2a 20 74 68 65 6d 20 41 2c 20 42  all.** them A, B
2632b 2c 20 61 6e 64 20 43 2e 20 54 68 65 20 72 65 73  , and C. The res
2632c 75 6c 74 20 69 73 20 61 6c 73 6f 20 61 20 73 74  ult is also a st
2632d 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 64 65  ring which is de
2632e 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 41 20  rived.** from A 
2632f 62 79 20 72 65 70 6c 61 63 69 6e 67 20 65 76 65  by replacing eve
26330 72 79 20 6f 63 63 75 72 61 6e 63 65 20 6f 66 20  ry occurance of 
26331 42 20 77 69 74 68 20 43 2e 20 20 54 68 65 20 6d  B with C.  The m
26332 61 74 63 68 0a 2a 2a 20 6d 75 73 74 20 62 65 20  atch.** must be 
26333 65 78 61 63 74 2e 20 20 43 6f 6c 6c 61 74 69 6e  exact.  Collatin
26334 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 20  g sequences are 
26335 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61  not used..*/.sta
26336 74 69 63 20 76 6f 69 64 20 72 65 70 6c 61 63 65  tic void replace
26337 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
26338 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
26339 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
2633a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2633b 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
2633c 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
2633d 53 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  Str;        /* T
2633e 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20  he input string 
2633f 41 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73  A */.  const uns
26340 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 50 61 74  igned char *zPat
26341 74 65 72 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20  tern;    /* The 
26342 70 61 74 74 65 72 6e 20 73 74 72 69 6e 67 20 42  pattern string B
26343 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   */.  const unsi
26344 67 6e 65 64 20 63 68 61 72 20 2a 7a 52 65 70 3b  gned char *zRep;
26345 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
26346 65 70 6c 61 63 65 6d 65 6e 74 20 73 74 72 69 6e  eplacement strin
26347 67 20 43 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  g C */.  unsigne
26348 64 20 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20  d char *zOut;   
26349 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2634a 65 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e  e output */.  in
2634b 74 20 6e 53 74 72 3b 20 20 20 20 20 20 20 20 20  t nStr;         
2634c 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
2634d 66 20 7a 53 74 72 20 2a 2f 0a 20 20 69 6e 74 20  f zStr */.  int 
2634e 6e 50 61 74 74 65 72 6e 3b 20 20 20 20 20 20 20  nPattern;       
2634f 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
26350 7a 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e  zPattern */.  in
26351 74 20 6e 52 65 70 3b 20 20 20 20 20 20 20 20 20  t nRep;         
26352 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
26353 66 20 7a 52 65 70 20 2a 2f 0a 20 20 69 36 34 20  f zRep */.  i64 
26354 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20  nOut;           
26355 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
26356 73 69 7a 65 20 6f 66 20 7a 4f 75 74 20 2a 2f 0a  size of zOut */.
26357 20 20 69 6e 74 20 6c 6f 6f 70 4c 69 6d 69 74 3b    int loopLimit;
26358 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
26359 73 74 20 7a 53 74 72 5b 5d 20 74 68 61 74 20 6d  st zStr[] that m
2635a 69 67 68 74 20 6d 61 74 63 68 20 7a 50 61 74 74  ight match zPatt
2635b 65 72 6e 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  ern[] */.  int i
2635c 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  , j;            
2635d 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2635e 74 65 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72  ters */..  asser
2635f 74 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20  t( argc==3 );.  
26360 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
26361 28 61 72 67 63 29 3b 0a 20 20 7a 53 74 72 20 3d  (argc);.  zStr =
26362 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
26363 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
26364 69 66 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65  if( zStr==0 ) re
26365 74 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73  turn;.  nStr = s
26366 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
26367 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61  es(argv[0]);.  a
26368 73 73 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c  ssert( zStr==sql
26369 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2636a 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20  argv[0]) );  /* 
2636b 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e  No encoding chan
2636c 67 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e  ge */.  zPattern
2636d 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2636e 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
2636f 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d    if( zPattern==
26370 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
26371 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
26372 79 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53 51  ype(argv[1])==SQ
26373 4c 49 54 45 5f 4e 55 4c 4c 0a 20 20 20 20 20 20  LITE_NULL.      
26374 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
26375 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
26376 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 6d 61 6c  le(context)->mal
26377 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
26378 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
26379 66 28 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d 3d  f( zPattern[0]==
2637a 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
2637b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2637c 79 70 65 28 61 72 67 76 5b 31 5d 29 21 3d 53 51  ype(argv[1])!=SQ
2637d 4c 49 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20  LITE_NULL );.   
2637e 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2637f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61  value(context, a
26380 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72 65 74  rgv[0]);.    ret
26381 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 50 61 74 74  urn;.  }.  nPatt
26382 65 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ern = sqlite3_va
26383 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31  lue_bytes(argv[1
26384 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 50  ]);.  assert( zP
26385 61 74 74 65 72 6e 3d 3d 73 71 6c 69 74 65 33 5f  attern==sqlite3_
26386 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
26387 31 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e  1]) );  /* No en
26388 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f  coding change */
26389 0a 20 20 7a 52 65 70 20 3d 20 73 71 6c 69 74 65  .  zRep = sqlite
2638a 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
2638b 76 5b 32 5d 29 3b 0a 20 20 69 66 28 20 7a 52 65  v[2]);.  if( zRe
2638c 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
2638d 20 6e 52 65 70 20 3d 20 73 71 6c 69 74 65 33 5f   nRep = sqlite3_
2638e 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
2638f 5b 32 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20  [2]);.  assert( 
26390 7a 52 65 70 3d 3d 73 71 6c 69 74 65 33 5f 76 61  zRep==sqlite3_va
26391 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d  lue_text(argv[2]
26392 29 20 29 3b 0a 20 20 6e 4f 75 74 20 3d 20 6e 53  ) );.  nOut = nS
26393 74 72 20 2b 20 31 3b 0a 20 20 61 73 73 65 72 74  tr + 1;.  assert
26394 28 20 6e 4f 75 74 3c 53 51 4c 49 54 45 5f 4d 41  ( nOut<SQLITE_MA
26395 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 7a 4f  X_LENGTH );.  zO
26396 75 74 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c  ut = contextMall
26397 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 69 36 34  oc(context, (i64
26398 29 6e 4f 75 74 29 3b 0a 20 20 69 66 28 20 7a 4f  )nOut);.  if( zO
26399 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ut==0 ){.    ret
2639a 75 72 6e 3b 0a 20 20 7d 0a 20 20 6c 6f 6f 70 4c  urn;.  }.  loopL
2639b 69 6d 69 74 20 3d 20 6e 53 74 72 20 2d 20 6e 50  imit = nStr - nP
2639c 61 74 74 65 72 6e 3b 20 20 0a 20 20 66 6f 72 28  attern;  .  for(
2639d 69 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f 70 4c 69  i=j=0; i<=loopLi
2639e 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  mit; i++){.    i
2639f 66 28 20 7a 53 74 72 5b 69 5d 21 3d 7a 50 61 74  f( zStr[i]!=zPat
263a0 74 65 72 6e 5b 30 5d 20 7c 7c 20 6d 65 6d 63 6d  tern[0] || memcm
263a1 70 28 26 7a 53 74 72 5b 69 5d 2c 20 7a 50 61 74  p(&zStr[i], zPat
263a2 74 65 72 6e 2c 20 6e 50 61 74 74 65 72 6e 29 20  tern, nPattern) 
263a3 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 2b  ){.      zOut[j+
263a4 2b 5d 20 3d 20 7a 53 74 72 5b 69 5d 3b 0a 20 20  +] = zStr[i];.  
263a5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75    }else{.      u
263a6 38 20 2a 7a 4f 6c 64 3b 0a 20 20 20 20 20 20 73  8 *zOld;.      s
263a7 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
263a8 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
263a9 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
263aa 0a 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d 20 6e  .      nOut += n
263ab 52 65 70 20 2d 20 6e 50 61 74 74 65 72 6e 3b 0a  Rep - nPattern;.
263ac 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
263ad 6e 4f 75 74 2d 31 3d 3d 64 62 2d 3e 61 4c 69 6d  nOut-1==db->aLim
263ae 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
263af 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 20 20 20  LENGTH] );.     
263b0 20 74 65 73 74 63 61 73 65 28 20 6e 4f 75 74 2d   testcase( nOut-
263b1 32 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  2==db->aLimit[SQ
263b2 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
263b3 48 5d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  H] );.      if( 
263b4 6e 4f 75 74 2d 31 3e 64 62 2d 3e 61 4c 69 6d 69  nOut-1>db->aLimi
263b5 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
263b6 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20  ENGTH] ){.      
263b7 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
263b8 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f  _error_toobig(co
263b9 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20  ntext);.        
263ba 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
263bb 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 20 20 20  , zOut);.       
263bc 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
263bd 0a 20 20 20 20 20 20 7a 4f 6c 64 20 3d 20 7a 4f  .      zOld = zO
263be 75 74 3b 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d  ut;.      zOut =
263bf 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
263c0 28 7a 4f 75 74 2c 20 28 69 6e 74 29 6e 4f 75 74  (zOut, (int)nOut
263c1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4f 75  );.      if( zOu
263c2 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
263c3 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
263c4 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
263c5 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  xt);.        sql
263c6 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
263c7 4f 6c 64 29 3b 0a 20 20 20 20 20 20 20 20 72 65  Old);.        re
263c8 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
263c9 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74      memcpy(&zOut
263ca 5b 6a 5d 2c 20 7a 52 65 70 2c 20 6e 52 65 70 29  [j], zRep, nRep)
263cb 3b 0a 20 20 20 20 20 20 6a 20 2b 3d 20 6e 52 65  ;.      j += nRe
263cc 70 3b 0a 20 20 20 20 20 20 69 20 2b 3d 20 6e 50  p;.      i += nP
263cd 61 74 74 65 72 6e 2d 31 3b 0a 20 20 20 20 7d 0a  attern-1;.    }.
263ce 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6a 2b    }.  assert( j+
263cf 6e 53 74 72 2d 69 2b 31 3d 3d 6e 4f 75 74 20 29  nStr-i+1==nOut )
263d0 3b 0a 20 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74  ;.  memcpy(&zOut
263d1 5b 6a 5d 2c 20 26 7a 53 74 72 5b 69 5d 2c 20 6e  [j], &zStr[i], n
263d2 53 74 72 2d 69 29 3b 0a 20 20 6a 20 2b 3d 20 6e  Str-i);.  j += n
263d3 53 74 72 20 2d 20 69 3b 0a 20 20 61 73 73 65 72  Str - i;.  asser
263d4 74 28 20 6a 3c 3d 6e 4f 75 74 20 29 3b 0a 20 20  t( j<=nOut );.  
263d5 7a 4f 75 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73  zOut[j] = 0;.  s
263d6 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
263d7 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  xt(context, (cha
263d8 72 2a 29 7a 4f 75 74 2c 20 6a 2c 20 73 71 6c 69  r*)zOut, j, sqli
263d9 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a  te3_free);.}../*
263da 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
263db 6f 6e 20 6f 66 20 74 68 65 20 54 52 49 4d 28 29  on of the TRIM()
263dc 2c 20 4c 54 52 49 4d 28 29 2c 20 61 6e 64 20 52  , LTRIM(), and R
263dd 54 52 49 4d 28 29 20 66 75 6e 63 74 69 6f 6e 73  TRIM() functions
263de 2e 0a 2a 2a 20 54 68 65 20 75 73 65 72 64 61 74  ..** The userdat
263df 61 20 69 73 20 30 78 31 20 66 6f 72 20 6c 65 66  a is 0x1 for lef
263e0 74 20 74 72 69 6d 2c 20 30 78 32 20 66 6f 72 20  t trim, 0x2 for 
263e1 72 69 67 68 74 20 74 72 69 6d 2c 20 30 78 33 20  right trim, 0x3 
263e2 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61  for both..*/.sta
263e3 74 69 63 20 76 6f 69 64 20 74 72 69 6d 46 75 6e  tic void trimFun
263e4 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
263e5 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
263e6 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
263e7 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
263e8 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  v.){.  const uns
263e9 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b  igned char *zIn;
263ea 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75           /* Inpu
263eb 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 6f  t string */.  co
263ec 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
263ed 72 20 2a 7a 43 68 61 72 53 65 74 3b 20 20 20 20  r *zCharSet;    
263ee 2f 2a 20 53 65 74 20 6f 66 20 63 68 61 72 61 63  /* Set of charac
263ef 74 65 72 73 20 74 6f 20 74 72 69 6d 20 2a 2f 0a  ters to trim */.
263f0 20 20 69 6e 74 20 6e 49 6e 3b 20 20 20 20 20 20    int nIn;      
263f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
263f2 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
263f3 20 62 79 74 65 73 20 69 6e 20 69 6e 70 75 74 20   bytes in input 
263f4 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20  */.  int flags; 
263f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
263f6 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 74 72 69         /* 1: tri
263f7 6d 6c 65 66 74 20 20 32 3a 20 74 72 69 6d 72 69  mleft  2: trimri
263f8 67 68 74 20 20 33 3a 20 74 72 69 6d 20 2a 2f 0a  ght  3: trim */.
263f9 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
263fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
263fb 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
263fc 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ter */.  unsigne
263fd 64 20 63 68 61 72 20 2a 61 4c 65 6e 20 3d 20 30  d char *aLen = 0
263fe 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  ;          /* Le
263ff 6e 67 74 68 20 6f 66 20 65 61 63 68 20 63 68 61  ngth of each cha
26400 72 61 63 74 65 72 20 69 6e 20 7a 43 68 61 72 53  racter in zCharS
26401 65 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  et */.  unsigned
26402 20 63 68 61 72 20 2a 2a 61 7a 43 68 61 72 20 3d   char **azChar =
26403 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64   0;       /* Ind
26404 69 76 69 64 75 61 6c 20 63 68 61 72 61 63 74 65  ividual characte
26405 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a  rs in zCharSet *
26406 2f 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b 20 20  /.  int nChar;  
26407 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26408 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
26409 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  of characters in
2640a 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 0a 20 20   zCharSet */..  
2640b 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
2640c 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d  e_type(argv[0])=
2640d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a  =SQLITE_NULL ){.
2640e 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2640f 20 20 7a 49 6e 20 3d 20 73 71 6c 69 74 65 33 5f    zIn = sqlite3_
26410 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
26411 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d  0]);.  if( zIn==
26412 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 49  0 ) return;.  nI
26413 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
26414 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
26415 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 49 6e 3d  ;.  assert( zIn=
26416 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
26417 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a  ext(argv[0]) );.
26418 20 20 69 66 28 20 61 72 67 63 3d 3d 31 20 29 7b    if( argc==1 ){
26419 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
2641a 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
2641b 6c 65 6e 4f 6e 65 5b 5d 20 3d 20 7b 20 31 20 7d  lenOne[] = { 1 }
2641c 3b 0a 20 20 20 20 73 74 61 74 69 63 20 75 6e 73  ;.    static uns
2641d 69 67 6e 65 64 20 63 68 61 72 20 2a 20 63 6f 6e  igned char * con
2641e 73 74 20 61 7a 4f 6e 65 5b 5d 20 3d 20 7b 20 28  st azOne[] = { (
2641f 75 38 2a 29 22 20 22 20 7d 3b 0a 20 20 20 20 6e  u8*)" " };.    n
26420 43 68 61 72 20 3d 20 31 3b 0a 20 20 20 20 61 4c  Char = 1;.    aL
26421 65 6e 20 3d 20 28 75 38 2a 29 6c 65 6e 4f 6e 65  en = (u8*)lenOne
26422 3b 0a 20 20 20 20 61 7a 43 68 61 72 20 3d 20 28  ;.    azChar = (
26423 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a  unsigned char **
26424 29 61 7a 4f 6e 65 3b 0a 20 20 20 20 7a 43 68 61  )azOne;.    zCha
26425 72 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  rSet = 0;.  }els
26426 65 20 69 66 28 20 28 7a 43 68 61 72 53 65 74 20  e if( (zCharSet 
26427 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
26428 74 65 78 74 28 61 72 67 76 5b 31 5d 29 29 3d 3d  text(argv[1]))==
26429 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  0 ){.    return;
2642a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f  .  }else{.    co
2642b 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2642c 72 20 2a 7a 3b 0a 20 20 20 20 66 6f 72 28 7a 3d  r *z;.    for(z=
2642d 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d  zCharSet, nChar=
2642e 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b  0; *z; nChar++){
2642f 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b  .      SQLITE_SK
26430 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20  IP_UTF8(z);.    
26431 7d 0a 20 20 20 20 69 66 28 20 6e 43 68 61 72 3e  }.    if( nChar>
26432 30 20 29 7b 0a 20 20 20 20 20 20 61 7a 43 68 61  0 ){.      azCha
26433 72 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  r = contextMallo
26434 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34  c(context, ((i64
26435 29 6e 43 68 61 72 29 2a 28 73 69 7a 65 6f 66 28  )nChar)*(sizeof(
26436 63 68 61 72 2a 29 2b 31 29 29 3b 0a 20 20 20 20  char*)+1));.    
26437 20 20 69 66 28 20 61 7a 43 68 61 72 3d 3d 30 20    if( azChar==0 
26438 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
26439 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  n;.      }.     
2643a 20 61 4c 65 6e 20 3d 20 28 75 6e 73 69 67 6e 65   aLen = (unsigne
2643b 64 20 63 68 61 72 2a 29 26 61 7a 43 68 61 72 5b  d char*)&azChar[
2643c 6e 43 68 61 72 5d 3b 0a 20 20 20 20 20 20 66 6f  nChar];.      fo
2643d 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20 6e 43  r(z=zCharSet, nC
2643e 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61 72  har=0; *z; nChar
2643f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 7a 43  ++){.        azC
26440 68 61 72 5b 6e 43 68 61 72 5d 20 3d 20 28 75 6e  har[nChar] = (un
26441 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 3b  signed char *)z;
26442 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  .        SQLITE_
26443 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20  SKIP_UTF8(z);.  
26444 20 20 20 20 20 20 61 4c 65 6e 5b 6e 43 68 61 72        aLen[nChar
26445 5d 20 3d 20 28 75 38 29 28 7a 20 2d 20 61 7a 43  ] = (u8)(z - azC
26446 68 61 72 5b 6e 43 68 61 72 5d 29 3b 0a 20 20 20  har[nChar]);.   
26447 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
26448 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a   if( nChar>0 ){.
26449 20 20 20 20 66 6c 61 67 73 20 3d 20 53 51 4c 49      flags = SQLI
2644a 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 73 71  TE_PTR_TO_INT(sq
2644b 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
2644c 63 6f 6e 74 65 78 74 29 29 3b 0a 20 20 20 20 69  context));.    i
2644d 66 28 20 66 6c 61 67 73 20 26 20 31 20 29 7b 0a  f( flags & 1 ){.
2644e 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e        while( nIn
2644f 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  >0 ){.        in
26450 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20  t len = 0;.     
26451 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
26452 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  har; i++){.     
26453 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b       len = aLen[
26454 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  i];.          if
26455 28 20 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65  ( len<=nIn && me
26456 6d 63 6d 70 28 7a 49 6e 2c 20 61 7a 43 68 61 72  mcmp(zIn, azChar
26457 5b 69 5d 2c 20 6c 65 6e 29 3d 3d 30 20 29 20 62  [i], len)==0 ) b
26458 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
26459 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 6e          if( i>=n
2645a 43 68 61 72 20 29 20 62 72 65 61 6b 3b 0a 20 20  Char ) break;.  
2645b 20 20 20 20 20 20 7a 49 6e 20 2b 3d 20 6c 65 6e        zIn += len
2645c 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d  ;.        nIn -=
2645d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20   len;.      }.  
2645e 20 20 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67    }.    if( flag
2645f 73 20 26 20 32 20 29 7b 0a 20 20 20 20 20 20 77  s & 2 ){.      w
26460 68 69 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20  hile( nIn>0 ){. 
26461 20 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d         int len =
26462 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   0;.        for(
26463 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20 69 2b  i=0; i<nChar; i+
26464 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65  +){.          le
26465 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20  n = aLen[i];.   
26466 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3c 3d         if( len<=
26467 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28 26 7a  nIn && memcmp(&z
26468 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c 61 7a 43 68  In[nIn-len],azCh
26469 61 72 5b 69 5d 2c 6c 65 6e 29 3d 3d 30 20 29 20  ar[i],len)==0 ) 
2646a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
2646b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d  .        if( i>=
2646c 6e 43 68 61 72 20 29 20 62 72 65 61 6b 3b 0a 20  nChar ) break;. 
2646d 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d 20 6c 65         nIn -= le
2646e 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  n;.      }.    }
2646f 0a 20 20 20 20 69 66 28 20 7a 43 68 61 72 53 65  .    if( zCharSe
26470 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
26471 65 33 5f 66 72 65 65 28 61 7a 43 68 61 72 29 3b  e3_free(azChar);
26472 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
26473 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
26474 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a  (context, (char*
26475 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53 51 4c 49 54  )zIn, nIn, SQLIT
26476 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a  E_TRANSIENT);.}.
26477 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
26478 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a 20 43 6f  SOUNDEX./*.** Co
26479 6d 70 75 74 65 20 74 68 65 20 73 6f 75 6e 64 65  mpute the sounde
2647a 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20  x encoding of a 
2647b 77 6f 72 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  word..*/.static 
2647c 76 6f 69 64 20 73 6f 75 6e 64 65 78 46 75 6e 63  void soundexFunc
2647d 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
2647e 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
2647f 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
26480 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
26481 0a 29 7b 0a 20 20 63 68 61 72 20 7a 52 65 73 75  .){.  char zResu
26482 6c 74 5b 38 5d 3b 0a 20 20 63 6f 6e 73 74 20 75  lt[8];.  const u
26483 38 20 2a 7a 49 6e 3b 0a 20 20 69 6e 74 20 69 2c  8 *zIn;.  int i,
26484 20 6a 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   j;.  static con
26485 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
26486 20 69 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20 20 20   iCode[] = {.   
26487 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
26488 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
26489 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2648a 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c  .    0, 0, 0, 0,
2648b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2648c 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2648d 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30  , 0,.    0, 0, 0
2648e 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2648f 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
26490 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20  0, 0, 0,.    0, 
26491 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
26492 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
26493 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20   0, 0, 0, 0,.   
26494 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20   0, 0, 1, 2, 3, 
26495 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32  0, 1, 2, 0, 0, 2
26496 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c  , 2, 4, 5, 5, 0,
26497 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c  .    1, 2, 6, 2,
26498 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20   3, 0, 1, 0, 2, 
26499 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 2, 0, 0, 0, 0
2649a 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31  , 0,.    0, 0, 1
2649b 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c  , 2, 3, 0, 1, 2,
2649c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20   0, 0, 2, 2, 4, 
2649d 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20  5, 5, 0,.    1, 
2649e 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31  2, 6, 2, 3, 0, 1
2649f 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c  , 0, 2, 0, 2, 0,
264a0 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 7d   0, 0, 0, 0,.  }
264a1 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ;.  assert( argc
264a2 3d 3d 31 20 29 3b 0a 20 20 7a 49 6e 20 3d 20 28  ==1 );.  zIn = (
264a3 75 38 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  u8*)sqlite3_valu
264a4 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
264a5 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20  .  if( zIn==0 ) 
264a6 7a 49 6e 20 3d 20 28 75 38 2a 29 22 22 3b 0a 20  zIn = (u8*)"";. 
264a7 20 66 6f 72 28 69 3d 30 3b 20 7a 49 6e 5b 69 5d   for(i=0; zIn[i]
264a8 20 26 26 20 21 73 71 6c 69 74 65 33 49 73 61 6c   && !sqlite3Isal
264a9 70 68 61 28 7a 49 6e 5b 69 5d 29 3b 20 69 2b 2b  pha(zIn[i]); i++
264aa 29 7b 7d 0a 20 20 69 66 28 20 7a 49 6e 5b 69 5d  ){}.  if( zIn[i]
264ab 20 29 7b 0a 20 20 20 20 75 38 20 70 72 65 76 63   ){.    u8 prevc
264ac 6f 64 65 20 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b  ode = iCode[zIn[
264ad 69 5d 26 30 78 37 66 5d 3b 0a 20 20 20 20 7a 52  i]&0x7f];.    zR
264ae 65 73 75 6c 74 5b 30 5d 20 3d 20 73 71 6c 69 74  esult[0] = sqlit
264af 65 33 54 6f 75 70 70 65 72 28 7a 49 6e 5b 69 5d  e3Toupper(zIn[i]
264b0 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20  );.    for(j=1; 
264b1 6a 3c 34 20 26 26 20 7a 49 6e 5b 69 5d 3b 20 69  j<4 && zIn[i]; i
264b2 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 63  ++){.      int c
264b3 6f 64 65 20 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b  ode = iCode[zIn[
264b4 69 5d 26 30 78 37 66 5d 3b 0a 20 20 20 20 20 20  i]&0x7f];.      
264b5 69 66 28 20 63 6f 64 65 3e 30 20 29 7b 0a 20 20  if( code>0 ){.  
264b6 20 20 20 20 20 20 69 66 28 20 63 6f 64 65 21 3d        if( code!=
264b7 70 72 65 76 63 6f 64 65 20 29 7b 0a 20 20 20 20  prevcode ){.    
264b8 20 20 20 20 20 20 70 72 65 76 63 6f 64 65 20 3d        prevcode =
264b9 20 63 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 20   code;.         
264ba 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20   zResult[j++] = 
264bb 63 6f 64 65 20 2b 20 27 30 27 3b 0a 20 20 20 20  code + '0';.    
264bc 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
264bd 65 7b 0a 20 20 20 20 20 20 20 20 70 72 65 76 63  e{.        prevc
264be 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ode = 0;.      }
264bf 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65  .    }.    while
264c0 28 20 6a 3c 34 20 29 7b 0a 20 20 20 20 20 20 7a  ( j<4 ){.      z
264c1 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 27 30  Result[j++] = '0
264c2 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 52 65  ';.    }.    zRe
264c3 73 75 6c 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20  sult[j] = 0;.   
264c4 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
264c5 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52  text(context, zR
264c6 65 73 75 6c 74 2c 20 34 2c 20 53 51 4c 49 54 45  esult, 4, SQLITE
264c7 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
264c8 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
264c9 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
264ca 6e 74 65 78 74 2c 20 22 3f 30 30 30 22 2c 20 34  ntext, "?000", 4
264cb 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
264cc 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ;.  }.}.#endif..
264cd 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
264ce 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
264cf 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 74  ON./*.** A funct
264d0 69 6f 6e 20 74 68 61 74 20 6c 6f 61 64 73 20 61  ion that loads a
264d1 20 73 68 61 72 65 64 2d 6c 69 62 72 61 72 79 20   shared-library 
264d2 65 78 74 65 6e 73 69 6f 6e 20 74 68 65 6e 20 72  extension then r
264d3 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  eturns NULL..*/.
264d4 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 64  static void load
264d5 45 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  Ext(sqlite3_cont
264d6 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
264d7 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
264d8 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
264d9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
264da 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  le = (const char
264db 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
264dc 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
264dd 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
264de 72 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  roc;.  sqlite3 *
264df 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
264e0 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
264e1 6f 6e 74 65 78 74 29 3b 0a 20 20 63 68 61 72 20  ontext);.  char 
264e2 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20  *zErrMsg = 0;.. 
264e3 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a   if( argc==2 ){.
264e4 20 20 20 20 7a 50 72 6f 63 20 3d 20 28 63 6f 6e      zProc = (con
264e5 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
264e6 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
264e7 76 5b 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  v[1]);.  }else{.
264e8 20 20 20 20 7a 50 72 6f 63 20 3d 20 30 3b 0a 20      zProc = 0;. 
264e9 20 7d 0a 20 20 69 66 28 20 7a 46 69 6c 65 20 26   }.  if( zFile &
264ea 26 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65  & sqlite3_load_e
264eb 78 74 65 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69  xtension(db, zFi
264ec 6c 65 2c 20 7a 50 72 6f 63 2c 20 26 7a 45 72 72  le, zProc, &zErr
264ed 4d 73 67 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  Msg) ){.    sqli
264ee 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
264ef 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 4d 73  (context, zErrMs
264f0 67 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69  g, -1);.    sqli
264f1 74 65 33 5f 66 72 65 65 28 7a 45 72 72 4d 73 67  te3_free(zErrMsg
264f2 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  );.  }.}.#endif.
264f3 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
264f4 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
264f5 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68  wing structure h
264f6 6f 6c 64 73 20 74 68 65 20 63 6f 6e 74 65 78 74  olds the context
264f7 20 6f 66 20 61 0a 2a 2a 20 73 75 6d 28 29 20 6f   of a.** sum() o
264f8 72 20 61 76 67 28 29 20 61 67 67 72 65 67 61 74  r avg() aggregat
264f9 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 2e 0a 2a  e computation..*
264fa 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
264fb 20 53 75 6d 43 74 78 20 53 75 6d 43 74 78 3b 0a   SumCtx SumCtx;.
264fc 73 74 72 75 63 74 20 53 75 6d 43 74 78 20 7b 0a  struct SumCtx {.
264fd 20 20 64 6f 75 62 6c 65 20 72 53 75 6d 3b 20 20    double rSum;  
264fe 20 20 20 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20      /* Floating 
264ff 70 6f 69 6e 74 20 73 75 6d 20 2a 2f 0a 20 20 69  point sum */.  i
26500 36 34 20 69 53 75 6d 3b 20 20 20 20 20 20 20 20  64 iSum;        
26501 20 2f 2a 20 49 6e 74 65 67 65 72 20 73 75 6d 20   /* Integer sum 
26502 2a 2f 20 20 20 0a 20 20 69 36 34 20 63 6e 74 3b  */   .  i64 cnt;
26503 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
26504 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
26505 73 75 6d 6d 65 64 20 2a 2f 0a 20 20 75 38 20 6f  summed */.  u8 o
26506 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 2f 2a  verflow;      /*
26507 20 54 72 75 65 20 69 66 20 69 6e 74 65 67 65 72   True if integer
26508 20 6f 76 65 72 66 6c 6f 77 20 73 65 65 6e 20 2a   overflow seen *
26509 2f 0a 20 20 75 38 20 61 70 70 72 6f 78 3b 20 20  /.  u8 approx;  
2650a 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2650b 20 6e 6f 6e 2d 69 6e 74 65 67 65 72 20 76 61 6c   non-integer val
2650c 75 65 20 77 61 73 20 69 6e 70 75 74 20 74 6f 20  ue was input to 
2650d 74 68 65 20 73 75 6d 20 2a 2f 0a 7d 3b 0a 0a 2f  the sum */.};../
2650e 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73  *.** Routines us
2650f 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68  ed to compute th
26510 65 20 73 75 6d 2c 20 61 76 65 72 61 67 65 2c 20  e sum, average, 
26511 61 6e 64 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a  and total..**.**
26512 20 54 68 65 20 53 55 4d 28 29 20 66 75 6e 63 74   The SUM() funct
26513 69 6f 6e 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20  ion follows the 
26514 28 62 72 6f 6b 65 6e 29 20 53 51 4c 20 73 74 61  (broken) SQL sta
26515 6e 64 61 72 64 20 77 68 69 63 68 20 6d 65 61 6e  ndard which mean
26516 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 72 65 74  s.** that it ret
26517 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69 74 20  urns NULL if it 
26518 73 75 6d 73 20 6f 76 65 72 20 6e 6f 20 69 6e 70  sums over no inp
26519 75 74 73 2e 20 20 54 4f 54 41 4c 20 72 65 74 75  uts.  TOTAL retu
2651a 72 6e 73 0a 2a 2a 20 30 2e 30 20 69 6e 20 74 68  rns.** 0.0 in th
2651b 61 74 20 63 61 73 65 2e 20 20 49 6e 20 61 64 64  at case.  In add
2651c 69 74 69 6f 6e 2c 20 54 4f 54 41 4c 20 61 6c 77  ition, TOTAL alw
2651d 61 79 73 20 72 65 74 75 72 6e 73 20 61 20 66 6c  ays returns a fl
2651e 6f 61 74 20 77 68 65 72 65 0a 2a 2a 20 53 55 4d  oat where.** SUM
2651f 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 61 6e   might return an
26520 20 69 6e 74 65 67 65 72 20 69 66 20 69 74 20 6e   integer if it n
26521 65 76 65 72 20 65 6e 63 6f 75 6e 74 65 72 73 20  ever encounters 
26522 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  a floating point
26523 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 54 4f 54 41  .** value.  TOTA
26524 4c 20 6e 65 76 65 72 20 66 61 69 6c 73 2c 20 62  L never fails, b
26525 75 74 20 53 55 4d 20 6d 69 67 68 74 20 74 68 72  ut SUM might thr
26526 6f 75 67 68 20 61 6e 20 65 78 63 65 70 74 69 6f  ough an exceptio
26527 6e 20 69 66 0a 2a 2a 20 69 74 20 6f 76 65 72 66  n if.** it overf
26528 6c 6f 77 73 20 61 6e 20 69 6e 74 65 67 65 72 2e  lows an integer.
26529 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2652a 73 75 6d 53 74 65 70 28 73 71 6c 69 74 65 33 5f  sumStep(sqlite3_
2652b 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2652c 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
2652d 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
2652e 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a  ){.  SumCtx *p;.
2652f 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 61 73    int type;.  as
26530 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b  sert( argc==1 );
26531 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
26532 54 45 52 28 61 72 67 63 29 3b 0a 20 20 70 20 3d  TER(argc);.  p =
26533 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
26534 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
26535 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b  xt, sizeof(*p));
26536 0a 20 20 74 79 70 65 20 3d 20 73 71 6c 69 74 65  .  type = sqlite
26537 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
26538 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20  type(argv[0]);. 
26539 20 69 66 28 20 70 20 26 26 20 74 79 70 65 21 3d   if( p && type!=
2653a 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20  SQLITE_NULL ){. 
2653b 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20     p->cnt++;.   
2653c 20 69 66 28 20 74 79 70 65 3d 3d 53 51 4c 49 54   if( type==SQLIT
2653d 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20  E_INTEGER ){.   
2653e 20 20 20 69 36 34 20 76 20 3d 20 73 71 6c 69 74     i64 v = sqlit
2653f 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61  e3_value_int64(a
26540 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70  rgv[0]);.      p
26541 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b 0a 20 20 20  ->rSum += v;.   
26542 20 20 20 69 66 28 20 28 70 2d 3e 61 70 70 72 6f     if( (p->appro
26543 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f 77 29 3d 3d  x|p->overflow)==
26544 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 36 34  0 ){.        i64
26545 20 69 4e 65 77 53 75 6d 20 3d 20 70 2d 3e 69 53   iNewSum = p->iS
26546 75 6d 20 2b 20 76 3b 0a 20 20 20 20 20 20 20 20  um + v;.        
26547 69 6e 74 20 73 31 20 3d 20 28 69 6e 74 29 28 70  int s1 = (int)(p
26548 2d 3e 69 53 75 6d 20 3e 3e 20 28 73 69 7a 65 6f  ->iSum >> (sizeo
26549 66 28 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20 20  f(i64)*8-1));.  
2654a 20 20 20 20 20 20 69 6e 74 20 73 32 20 3d 20 28        int s2 = (
2654b 69 6e 74 29 28 76 20 20 20 20 20 20 20 3e 3e 20  int)(v       >> 
2654c 28 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31  (sizeof(i64)*8-1
2654d 29 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ));.        int 
2654e 73 33 20 3d 20 28 69 6e 74 29 28 69 4e 65 77 53  s3 = (int)(iNewS
2654f 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36  um >> (sizeof(i6
26550 34 29 2a 38 2d 31 29 29 3b 0a 20 20 20 20 20 20  4)*8-1));.      
26551 20 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 3d 20    p->overflow = 
26552 28 28 73 31 26 73 32 26 7e 73 33 29 20 7c 20 28  ((s1&s2&~s3) | (
26553 7e 73 31 26 7e 73 32 26 73 33 29 29 3f 31 3a 30  ~s1&~s2&s3))?1:0
26554 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 53 75  ;.        p->iSu
26555 6d 20 3d 20 69 4e 65 77 53 75 6d 3b 0a 20 20 20  m = iNewSum;.   
26556 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
26557 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d        p->rSum +=
26558 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
26559 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a  ouble(argv[0]);.
2655a 20 20 20 20 20 20 70 2d 3e 61 70 70 72 6f 78 20        p->approx 
2655b 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  = 1;.    }.  }.}
2655c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d  .static void sum
2655d 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33  Finalize(sqlite3
2655e 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
2655f 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b  t){.  SumCtx *p;
26560 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61  .  p = sqlite3_a
26561 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
26562 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20  (context, 0);.  
26563 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e  if( p && p->cnt>
26564 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  0 ){.    if( p->
26565 6f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  overflow ){.    
26566 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
26567 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 22  _error(context,"
26568 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77  integer overflow
26569 22 2c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ",-1);.    }else
2656a 20 69 66 28 20 70 2d 3e 61 70 70 72 6f 78 20 29   if( p->approx )
2656b 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
2656c 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f  result_double(co
2656d 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 29 3b  ntext, p->rSum);
2656e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2656f 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
26570 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20  _int64(context, 
26571 70 2d 3e 69 53 75 6d 29 3b 0a 20 20 20 20 7d 0a  p->iSum);.    }.
26572 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
26573 64 20 61 76 67 46 69 6e 61 6c 69 7a 65 28 73 71  d avgFinalize(sq
26574 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
26575 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74  ontext){.  SumCt
26576 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
26577 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
26578 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
26579 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  );.  if( p && p-
2657a 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 73 71  >cnt>0 ){.    sq
2657b 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
2657c 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e  ble(context, p->
2657d 72 53 75 6d 2f 28 64 6f 75 62 6c 65 29 70 2d 3e  rSum/(double)p->
2657e 63 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74  cnt);.  }.}.stat
2657f 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 46 69 6e  ic void totalFin
26580 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
26581 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
26582 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20  .  SumCtx *p;.  
26583 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
26584 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
26585 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 2f 2a 20  ntext, 0);.  /* 
26586 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73  (double)0 In cas
26587 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  e of SQLITE_OMIT
26588 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e  _FLOATING_POINT.
26589 2e 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .. */.  sqlite3_
2658a 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f  result_double(co
2658b 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d 3e 72 53  ntext, p ? p->rS
2658c 75 6d 20 3a 20 28 64 6f 75 62 6c 65 29 30 29 3b  um : (double)0);
2658d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
2658e 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
2658f 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66  e keeps track of
26590 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69   state informati
26591 6f 6e 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f  on for the.** co
26592 75 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 20  unt() aggregate 
26593 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70  function..*/.typ
26594 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 75 6e  edef struct Coun
26595 74 43 74 78 20 43 6f 75 6e 74 43 74 78 3b 0a 73  tCtx CountCtx;.s
26596 74 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20 7b  truct CountCtx {
26597 0a 20 20 69 36 34 20 6e 3b 0a 7d 3b 0a 0a 2f 2a  .  i64 n;.};../*
26598 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20  .** Routines to 
26599 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 6f  implement the co
2659a 75 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 20  unt() aggregate 
2659b 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
2659c 74 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 53 74  tic void countSt
2659d 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ep(sqlite3_conte
2659e 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74  xt *context, int
2659f 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
265a0 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20  alue **argv){.  
265a1 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20 70  CountCtx *p;.  p
265a2 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65   = sqlite3_aggre
265a3 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
265a4 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29  text, sizeof(*p)
265a5 29 3b 0a 20 20 69 66 28 20 28 61 72 67 63 3d 3d  );.  if( (argc==
265a6 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  0 || SQLITE_NULL
265a7 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  !=sqlite3_value_
265a8 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 20 26  type(argv[0])) &
265a9 26 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 2b  & p ){.    p->n+
265aa 2b 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  +;.  }..#ifndef 
265ab 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
265ac 45 43 41 54 45 44 0a 20 20 2f 2a 20 54 68 65 20  ECATED.  /* The 
265ad 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
265ae 65 5f 63 6f 75 6e 74 28 29 20 66 75 6e 63 74 69  e_count() functi
265af 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64  on is deprecated
265b0 2e 20 20 42 75 74 20 6a 75 73 74 20 74 6f 20 6d  .  But just to m
265b1 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 69 74  ake.  ** sure it
265b2 20 73 74 69 6c 6c 20 6f 70 65 72 61 74 65 73 20   still operates 
265b3 63 6f 72 72 65 63 74 6c 79 2c 20 76 65 72 69 66  correctly, verif
265b4 79 20 74 68 61 74 20 69 74 73 20 63 6f 75 6e 74  y that its count
265b5 20 61 67 72 65 65 73 20 77 69 74 68 20 6f 75 72   agrees with our
265b6 20 0a 20 20 2a 2a 20 69 6e 74 65 72 6e 61 6c 20   .  ** internal 
265b7 63 6f 75 6e 74 20 77 68 65 6e 20 75 73 69 6e 67  count when using
265b8 20 63 6f 75 6e 74 28 2a 29 20 61 6e 64 20 77 68   count(*) and wh
265b9 65 6e 20 74 68 65 20 74 6f 74 61 6c 20 63 6f 75  en the total cou
265ba 6e 74 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 65  nt can be.  ** e
265bb 78 70 72 65 73 73 65 64 20 61 73 20 61 20 33 32  xpressed as a 32
265bc 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 20 2a 2f  -bit integer. */
265bd 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
265be 3d 31 20 7c 7c 20 70 3d 3d 30 20 7c 7c 20 70 2d  =1 || p==0 || p-
265bf 3e 6e 3e 30 78 37 66 66 66 66 66 66 66 0a 20 20  >n>0x7fffffff.  
265c0 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6e 3d          || p->n=
265c1 3d 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  =sqlite3_aggrega
265c2 74 65 5f 63 6f 75 6e 74 28 63 6f 6e 74 65 78 74  te_count(context
265c3 29 20 29 3b 0a 23 65 6e 64 69 66 0a 7d 20 20 20  ) );.#endif.}   
265c4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 75  .static void cou
265c5 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  ntFinalize(sqlit
265c6 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
265c7 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78  ext){.  CountCtx
265c8 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74   *p;.  p = sqlit
265c9 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
265ca 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29  text(context, 0)
265cb 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
265cc 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74  lt_int64(context
265cd 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 30 29 3b  , p ? p->n : 0);
265ce 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  .}../*.** Routin
265cf 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  es to implement 
265d0 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20  min() and max() 
265d1 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
265d2 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ons..*/.static v
265d3 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 70 28 0a  oid minmaxStep(.
265d4 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
265d5 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69  t *context, .  i
265d6 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73  nt NotUsed, .  s
265d7 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
265d8 72 67 76 0a 29 7b 0a 20 20 4d 65 6d 20 2a 70 41  rgv.){.  Mem *pA
265d9 72 67 20 20 3d 20 28 4d 65 6d 20 2a 29 61 72 67  rg  = (Mem *)arg
265da 76 5b 30 5d 3b 0a 20 20 4d 65 6d 20 2a 70 42 65  v[0];.  Mem *pBe
265db 73 74 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  st;.  UNUSED_PAR
265dc 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
265dd 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ..  if( sqlite3_
265de 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
265df 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])==SQLITE_NULL
265e0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 42 65   ) return;.  pBe
265e1 73 74 20 3d 20 28 4d 65 6d 20 2a 29 73 71 6c 69  st = (Mem *)sqli
265e2 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
265e3 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  ntext(context, s
265e4 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 29 3b 0a  izeof(*pBest));.
265e5 20 20 69 66 28 20 21 70 42 65 73 74 20 29 20 72    if( !pBest ) r
265e6 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 70 42  eturn;..  if( pB
265e7 65 73 74 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20  est->flags ){.  
265e8 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 20 20 69    int max;.    i
265e9 6e 74 20 63 6d 70 3b 0a 20 20 20 20 43 6f 6c 6c  nt cmp;.    Coll
265ea 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c  Seq *pColl = sql
265eb 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
265ec 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  eq(context);.   
265ed 20 2f 2a 20 54 68 69 73 20 73 74 65 70 20 66 75   /* This step fu
265ee 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66  nction is used f
265ef 6f 72 20 62 6f 74 68 20 74 68 65 20 6d 69 6e 28  or both the min(
265f0 29 20 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72  ) and max() aggr
265f1 65 67 61 74 65 73 2c 0a 20 20 20 20 2a 2a 20 74  egates,.    ** t
265f2 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e  he only differen
265f3 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 74  ce between the t
265f4 77 6f 20 62 65 69 6e 67 20 74 68 61 74 20 74 68  wo being that th
265f5 65 20 73 65 6e 73 65 20 6f 66 20 74 68 65 0a 20  e sense of the. 
265f6 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e     ** comparison
265f7 20 69 73 20 69 6e 76 65 72 74 65 64 2e 20 46 6f   is inverted. Fo
265f8 72 20 74 68 65 20 6d 61 78 28 29 20 61 67 67 72  r the max() aggr
265f9 65 67 61 74 65 2c 20 74 68 65 0a 20 20 20 20 2a  egate, the.    *
265fa 2a 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  * sqlite3_user_d
265fb 61 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 72  ata() function r
265fc 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 2d  eturns (void *)-
265fd 31 2e 20 46 6f 72 20 6d 69 6e 28 29 20 69 74 0a  1. For min() it.
265fe 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 28      ** returns (
265ff 76 6f 69 64 20 2a 29 64 62 2c 20 77 68 65 72 65  void *)db, where
26600 20 64 62 20 69 73 20 74 68 65 20 73 71 6c 69 74   db is the sqlit
26601 65 33 2a 20 64 61 74 61 62 61 73 65 20 70 6f 69  e3* database poi
26602 6e 74 65 72 2e 0a 20 20 20 20 2a 2a 20 54 68 65  nter..    ** The
26603 72 65 66 6f 72 65 20 74 68 65 20 6e 65 78 74 20  refore the next 
26604 73 74 61 74 65 6d 65 6e 74 20 73 65 74 73 20 76  statement sets v
26605 61 72 69 61 62 6c 65 20 27 6d 61 78 27 20 74 6f  ariable 'max' to
26606 20 31 20 66 6f 72 20 74 68 65 20 6d 61 78 28 29   1 for the max()
26607 0a 20 20 20 20 2a 2a 20 61 67 67 72 65 67 61 74  .    ** aggregat
26608 65 2c 20 6f 72 20 30 20 66 6f 72 20 6d 69 6e 28  e, or 0 for min(
26609 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 61  )..    */.    ma
2660a 78 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72  x = sqlite3_user
2660b 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 21 3d  _data(context)!=
2660c 30 3b 0a 20 20 20 20 63 6d 70 20 3d 20 73 71 6c  0;.    cmp = sql
2660d 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70  ite3MemCompare(p
2660e 42 65 73 74 2c 20 70 41 72 67 2c 20 70 43 6f 6c  Best, pArg, pCol
2660f 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 6d 61 78  l);.    if( (max
26610 20 26 26 20 63 6d 70 3c 30 29 20 7c 7c 20 28 21   && cmp<0) || (!
26611 6d 61 78 20 26 26 20 63 6d 70 3e 30 29 20 29 7b  max && cmp>0) ){
26612 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26613 62 65 4d 65 6d 43 6f 70 79 28 70 42 65 73 74 2c  beMemCopy(pBest,
26614 20 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a 20 20   pArg);.    }.  
26615 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
26616 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42  e3VdbeMemCopy(pB
26617 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a  est, pArg);.  }.
26618 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69  }.static void mi
26619 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 73 71 6c  nMaxFinalize(sql
2661a 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
2661b 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c 69 74 65  ntext){.  sqlite
2661c 33 5f 76 61 6c 75 65 20 2a 70 52 65 73 3b 0a 20  3_value *pRes;. 
2661d 20 70 52 65 73 20 3d 20 28 73 71 6c 69 74 65 33   pRes = (sqlite3
2661e 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69 74 65 33  _value *)sqlite3
2661f 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
26620 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  xt(context, 0);.
26621 20 20 69 66 28 20 70 52 65 73 20 29 7b 0a 20 20    if( pRes ){.  
26622 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 52 65    if( ALWAYS(pRe
26623 73 2d 3e 66 6c 61 67 73 29 20 29 7b 0a 20 20 20  s->flags) ){.   
26624 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
26625 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
26626 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20   pRes);.    }.  
26627 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
26628 52 65 6c 65 61 73 65 28 70 52 65 73 29 3b 0a 20  Release(pRes);. 
26629 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 67 72 6f 75   }.}../*.** grou
2662a 70 5f 63 6f 6e 63 61 74 28 45 58 50 52 2c 20 3f  p_concat(EXPR, ?
2662b 53 45 50 41 52 41 54 4f 52 3f 29 0a 2a 2f 0a 73  SEPARATOR?).*/.s
2662c 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70  tatic void group
2662d 43 6f 6e 63 61 74 53 74 65 70 28 0a 20 20 73 71  ConcatStep(.  sq
2662e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2662f 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
26630 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
26631 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
26632 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c  const char *zVal
26633 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41  ;.  StrAccum *pA
26634 63 63 75 6d 3b 0a 20 20 63 6f 6e 73 74 20 63 68  ccum;.  const ch
26635 61 72 20 2a 7a 53 65 70 3b 0a 20 20 69 6e 74 20  ar *zSep;.  int 
26636 6e 56 61 6c 2c 20 6e 53 65 70 3b 0a 20 20 61 73  nVal, nSep;.  as
26637 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c  sert( argc==1 ||
26638 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66   argc==2 );.  if
26639 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
2663a 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53  type(argv[0])==S
2663b 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74  QLITE_NULL ) ret
2663c 75 72 6e 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20  urn;.  pAccum = 
2663d 28 53 74 72 41 63 63 75 6d 2a 29 73 71 6c 69 74  (StrAccum*)sqlit
2663e 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
2663f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69  text(context, si
26640 7a 65 6f 66 28 2a 70 41 63 63 75 6d 29 29 3b 0a  zeof(*pAccum));.
26641 0a 20 20 69 66 28 20 70 41 63 63 75 6d 20 29 7b  .  if( pAccum ){
26642 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
26643 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
26644 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
26645 74 65 78 74 29 3b 0a 20 20 20 20 69 6e 74 20 66  text);.    int f
26646 69 72 73 74 54 65 72 6d 20 3d 20 70 41 63 63 75  irstTerm = pAccu
26647 6d 2d 3e 75 73 65 4d 61 6c 6c 6f 63 3d 3d 30 3b  m->useMalloc==0;
26648 0a 20 20 20 20 70 41 63 63 75 6d 2d 3e 75 73 65  .    pAccum->use
26649 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 20 20  Malloc = 1;.    
2664a 70 41 63 63 75 6d 2d 3e 6d 78 41 6c 6c 6f 63 20  pAccum->mxAlloc 
2664b 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  = db->aLimit[SQL
2664c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
2664d 5d 3b 0a 20 20 20 20 69 66 28 20 21 66 69 72 73  ];.    if( !firs
2664e 74 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 69  tTerm ){.      i
2664f 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20  f( argc==2 ){.  
26650 20 20 20 20 20 20 7a 53 65 70 20 3d 20 28 63 68        zSep = (ch
26651 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
26652 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
26653 0a 20 20 20 20 20 20 20 20 6e 53 65 70 20 3d 20  .        nSep = 
26654 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
26655 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  tes(argv[1]);.  
26656 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
26657 20 20 20 7a 53 65 70 20 3d 20 22 2c 22 3b 0a 20     zSep = ",";. 
26658 20 20 20 20 20 20 20 6e 53 65 70 20 3d 20 31 3b         nSep = 1;
26659 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
2665a 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
2665b 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 65  pend(pAccum, zSe
2665c 70 2c 20 6e 53 65 70 29 3b 0a 20 20 20 20 7d 0a  p, nSep);.    }.
2665d 20 20 20 20 7a 56 61 6c 20 3d 20 28 63 68 61 72      zVal = (char
2665e 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
2665f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
26660 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65     nVal = sqlite
26661 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
26662 67 76 5b 30 5d 29 3b 0a 20 20 20 20 73 71 6c 69  gv[0]);.    sqli
26663 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
26664 64 28 70 41 63 63 75 6d 2c 20 7a 56 61 6c 2c 20  d(pAccum, zVal, 
26665 6e 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  nVal);.  }.}.sta
26666 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70 43 6f  tic void groupCo
26667 6e 63 61 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c  ncatFinalize(sql
26668 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
26669 6e 74 65 78 74 29 7b 0a 20 20 53 74 72 41 63 63  ntext){.  StrAcc
2666a 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20 70 41  um *pAccum;.  pA
2666b 63 63 75 6d 20 3d 20 73 71 6c 69 74 65 33 5f 61  ccum = sqlite3_a
2666c 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
2666d 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20  (context, 0);.  
2666e 69 66 28 20 70 41 63 63 75 6d 20 29 7b 0a 20 20  if( pAccum ){.  
2666f 20 20 69 66 28 20 70 41 63 63 75 6d 2d 3e 74 6f    if( pAccum->to
26670 6f 42 69 67 20 29 7b 0a 20 20 20 20 20 20 73 71  oBig ){.      sq
26671 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
26672 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78  or_toobig(contex
26673 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  t);.    }else if
26674 28 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63  ( pAccum->malloc
26675 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
26676 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
26677 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
26678 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 20  xt);.    }else{ 
26679 20 20 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65     .      sqlite
2667a 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
2667b 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 53 74  ntext, sqlite3St
2667c 72 41 63 63 75 6d 46 69 6e 69 73 68 28 70 41 63  rAccumFinish(pAc
2667d 63 75 6d 29 2c 20 2d 31 2c 20 0a 20 20 20 20 20  cum), -1, .     
2667e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2667f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
26680 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  e);.    }.  }.}.
26681 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
26682 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
26683 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65  all of the above
26684 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20   C functions as 
26685 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  SQL.** functions
26686 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  .  This should b
26687 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69  e the only routi
26688 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  ne in this file 
26689 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c  with.** external
2668a 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c   linkage..*/.SQL
2668b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2668c 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
2668d 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73  BuiltinFunctions
2668e 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 23  (sqlite3 *db){.#
2668f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
26690 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20  IT_ALTERTABLE.  
26691 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63  sqlite3AlterFunc
26692 74 69 6f 6e 73 28 64 62 29 3b 0a 23 65 6e 64 69  tions(db);.#endi
26693 66 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c  f.  if( !db->mal
26694 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
26695 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
26696 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
26697 69 6f 6e 28 64 62 2c 20 22 4d 41 54 43 48 22 2c  ion(db, "MATCH",
26698 20 32 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   2);.    assert(
26699 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
2669a 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  M || rc==SQLITE_
2669b 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  OK );.    if( rc
2669c 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
2669d 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
2669e 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
2669f 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
266a0 20 53 65 74 20 74 68 65 20 4c 49 4b 45 4f 50 54   Set the LIKEOPT
266a1 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 32 2d 61   flag on the 2-a
266a2 72 67 75 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e  rgument function
266a3 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20   with the given 
266a4 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  name..*/.static 
266a5 76 6f 69 64 20 73 65 74 4c 69 6b 65 4f 70 74 46  void setLikeOptF
266a6 6c 61 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  lag(sqlite3 *db,
266a7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
266a8 6d 65 2c 20 75 38 20 66 6c 61 67 56 61 6c 29 7b  me, u8 flagVal){
266a9 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66  .  FuncDef *pDef
266aa 3b 0a 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74  ;.  pDef = sqlit
266ab 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64  e3FindFunction(d
266ac 62 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65  b, zName, sqlite
266ad 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
266ae 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
266af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
266b0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
266b1 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  );.  if( ALWAYS(
266b2 70 44 65 66 29 20 29 7b 0a 20 20 20 20 70 44 65  pDef) ){.    pDe
266b3 66 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 56  f->flags = flagV
266b4 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  al;.  }.}../*.**
266b5 20 52 65 67 69 73 74 65 72 20 74 68 65 20 62 75   Register the bu
266b6 69 6c 74 2d 69 6e 20 4c 49 4b 45 20 61 6e 64 20  ilt-in LIKE and 
266b7 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20  GLOB functions. 
266b8 20 54 68 65 20 63 61 73 65 53 65 6e 73 69 74 69   The caseSensiti
266b9 76 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  ve.** parameter 
266ba 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68  determines wheth
266bb 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 4c 49  er or not the LI
266bc 4b 45 20 6f 70 65 72 61 74 6f 72 20 69 73 20 63  KE operator is c
266bd 61 73 65 0a 2a 2a 20 73 65 6e 73 69 74 69 76 65  ase.** sensitive
266be 2e 20 20 47 4c 4f 42 20 69 73 20 61 6c 77 61 79  .  GLOB is alway
266bf 73 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65  s case sensitive
266c0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
266c1 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
266c2 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63  RegisterLikeFunc
266c3 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64  tions(sqlite3 *d
266c4 62 2c 20 69 6e 74 20 63 61 73 65 53 65 6e 73 69  b, int caseSensi
266c5 74 69 76 65 29 7b 0a 20 20 73 74 72 75 63 74 20  tive){.  struct 
266c6 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e  compareInfo *pIn
266c7 66 6f 3b 0a 20 20 69 66 28 20 63 61 73 65 53 65  fo;.  if( caseSe
266c8 6e 73 69 74 69 76 65 20 29 7b 0a 20 20 20 20 70  nsitive ){.    p
266c9 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74 20 63  Info = (struct c
266ca 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b  ompareInfo*)&lik
266cb 65 49 6e 66 6f 41 6c 74 3b 0a 20 20 7d 65 6c 73  eInfoAlt;.  }els
266cc 65 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 28  e{.    pInfo = (
266cd 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e  struct compareIn
266ce 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72  fo*)&likeInfoNor
266cf 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  m;.  }.  sqlite3
266d0 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22  CreateFunc(db, "
266d1 6c 69 6b 65 22 2c 20 32 2c 20 53 51 4c 49 54 45  like", 2, SQLITE
266d2 5f 41 4e 59 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b  _ANY, pInfo, lik
266d3 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20  eFunc, 0, 0);.  
266d4 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
266d5 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 33 2c  c(db, "like", 3,
266d6 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 70 49 6e   SQLITE_ANY, pIn
266d7 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c  fo, likeFunc, 0,
266d8 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 72   0);.  sqlite3Cr
266d9 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 67 6c  eateFunc(db, "gl
266da 6f 62 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  ob", 2, SQLITE_A
266db 4e 59 2c 20 0a 20 20 20 20 20 20 28 73 74 72 75  NY, .      (stru
266dc 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29  ct compareInfo*)
266dd 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c 69 6b 65 46  &globInfo, likeF
266de 75 6e 63 2c 20 30 2c 30 29 3b 0a 20 20 73 65 74  unc, 0,0);.  set
266df 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64 62 2c 20  LikeOptFlag(db, 
266e0 22 67 6c 6f 62 22 2c 20 53 51 4c 49 54 45 5f 46  "glob", SQLITE_F
266e1 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 54  UNC_LIKE | SQLIT
266e2 45 5f 46 55 4e 43 5f 43 41 53 45 29 3b 0a 20 20  E_FUNC_CASE);.  
266e3 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64  setLikeOptFlag(d
266e4 62 2c 20 22 6c 69 6b 65 22 2c 20 0a 20 20 20 20  b, "like", .    
266e5 20 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 20    caseSensitive 
266e6 3f 20 28 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c  ? (SQLITE_FUNC_L
266e7 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e  IKE | SQLITE_FUN
266e8 43 5f 43 41 53 45 29 20 3a 20 53 51 4c 49 54 45  C_CASE) : SQLITE
266e9 5f 46 55 4e 43 5f 4c 49 4b 45 29 3b 0a 7d 0a 0a  _FUNC_LIKE);.}..
266ea 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 70 6f 69 6e  /*.** pExpr poin
266eb 74 73 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73  ts to an express
266ec 69 6f 6e 20 77 68 69 63 68 20 69 6d 70 6c 65 6d  ion which implem
266ed 65 6e 74 73 20 61 20 66 75 6e 63 74 69 6f 6e 2e  ents a function.
266ee 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 20 61 70    If.** it is ap
266ef 70 72 6f 70 72 69 61 74 65 20 74 6f 20 61 70 70  propriate to app
266f0 6c 79 20 74 68 65 20 4c 49 4b 45 20 6f 70 74 69  ly the LIKE opti
266f1 6d 69 7a 61 74 69 6f 6e 20 74 6f 20 74 68 61 74  mization to that
266f2 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 65   function.** the
266f3 6e 20 73 65 74 20 61 57 63 5b 30 5d 20 74 68 72  n set aWc[0] thr
266f4 6f 75 67 68 20 61 57 63 5b 32 5d 20 74 6f 20 74  ough aWc[2] to t
266f5 68 65 20 77 69 6c 64 63 61 72 64 20 63 68 61 72  he wildcard char
266f6 61 63 74 65 72 73 20 61 6e 64 0a 2a 2a 20 72 65  acters and.** re
266f7 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20 74  turn TRUE.  If t
266f8 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e  he function is n
266f9 6f 74 20 61 20 4c 49 4b 45 2d 73 74 79 6c 65 20  ot a LIKE-style 
266fa 66 75 6e 63 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a  function then.**
266fb 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a   return FALSE..*
266fc 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
266fd 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c 69   int sqlite3IsLi
266fe 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74  keFunction(sqlit
266ff 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 45  e3 *db, Expr *pE
26700 78 70 72 2c 20 69 6e 74 20 2a 70 49 73 4e 6f 63  xpr, int *pIsNoc
26701 61 73 65 2c 20 63 68 61 72 20 2a 61 57 63 29 7b  ase, char *aWc){
26702 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66  .  FuncDef *pDef
26703 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ;.  if( pExpr->o
26704 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 0a  p!=TK_FUNCTION .
26705 20 20 20 7c 7c 20 21 70 45 78 70 72 2d 3e 78 2e     || !pExpr->x.
26706 70 4c 69 73 74 20 0a 20 20 20 7c 7c 20 70 45 78  pList .   || pEx
26707 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78  pr->x.pList->nEx
26708 70 72 21 3d 32 0a 20 20 29 7b 0a 20 20 20 20 72  pr!=2.  ){.    r
26709 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61  eturn 0;.  }.  a
2670a 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
2670b 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2670c 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
2670d 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33    pDef = sqlite3
2670e 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c  FindFunction(db,
2670f 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
26710 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
26711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26712 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
26713 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29  pExpr->u.zToken)
26714 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
26715 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
26716 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
26717 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  );.  if( NEVER(p
26718 44 65 66 3d 3d 30 29 20 7c 7c 20 28 70 44 65 66  Def==0) || (pDef
26719 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2671a 5f 46 55 4e 43 5f 4c 49 4b 45 29 3d 3d 30 20 29  _FUNC_LIKE)==0 )
2671b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2671c 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 65    }..  /* The me
2671d 6d 63 70 79 28 29 20 73 74 61 74 65 6d 65 6e 74  mcpy() statement
2671e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
2671f 65 20 77 69 6c 64 63 61 72 64 20 63 68 61 72 61  e wildcard chara
26720 63 74 65 72 73 20 61 72 65 0a 20 20 2a 2a 20 74  cters are.  ** t
26721 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 73  he first three s
26722 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65  tatements in the
26723 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 73 74 72   compareInfo str
26724 75 63 74 75 72 65 2e 20 20 54 68 65 0a 20 20 2a  ucture.  The.  *
26725 2a 20 61 73 73 65 72 74 73 28 29 20 74 68 61 74  * asserts() that
26726 20 66 6f 6c 6c 6f 77 20 76 65 72 69 66 79 20 74   follow verify t
26727 68 61 74 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20  hat assumption. 
26728 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 61 57 63   */.  memcpy(aWc
26729 2c 20 70 44 65 66 2d 3e 70 55 73 65 72 44 61 74  , pDef->pUserDat
2672a 61 2c 20 33 29 3b 0a 20 20 61 73 73 65 72 74 28  a, 3);.  assert(
2672b 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66   (char*)&likeInf
2672c 6f 41 6c 74 20 3d 3d 20 28 63 68 61 72 2a 29 26  oAlt == (char*)&
2672d 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63  likeInfoAlt.matc
2672e 68 41 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  hAll );.  assert
2672f 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69 6b 65  ( &((char*)&like
26730 49 6e 66 6f 41 6c 74 29 5b 31 5d 20 3d 3d 20 28  InfoAlt)[1] == (
26731 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41  char*)&likeInfoA
26732 6c 74 2e 6d 61 74 63 68 4f 6e 65 20 29 3b 0a 20  lt.matchOne );. 
26733 20 61 73 73 65 72 74 28 20 26 28 28 63 68 61 72   assert( &((char
26734 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b  *)&likeInfoAlt)[
26735 32 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69  2] == (char*)&li
26736 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 53  keInfoAlt.matchS
26737 65 74 20 29 3b 0a 20 20 2a 70 49 73 4e 6f 63 61  et );.  *pIsNoca
26738 73 65 20 3d 20 28 70 44 65 66 2d 3e 66 6c 61 67  se = (pDef->flag
26739 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  s & SQLITE_FUNC_
2673a 43 41 53 45 29 3d 3d 30 3b 0a 20 20 72 65 74 75  CASE)==0;.  retu
2673b 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn 1;.}../*.** A
2673c 6c 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 46 75  ll all of the Fu
2673d 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65 73  ncDef structures
2673e 20 69 6e 20 74 68 65 20 61 42 75 69 6c 74 69 6e   in the aBuiltin
2673f 46 75 6e 63 5b 5d 20 61 72 72 61 79 20 61 62 6f  Func[] array abo
26740 76 65 0a 2a 2a 20 74 6f 20 74 68 65 20 67 6c 6f  ve.** to the glo
26741 62 61 6c 20 66 75 6e 63 74 69 6f 6e 20 68 61 73  bal function has
26742 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 6f  h table.  This o
26743 63 63 75 72 73 20 61 74 20 73 74 61 72 74 2d 74  ccurs at start-t
26744 69 6d 65 20 28 61 73 0a 2a 2a 20 61 20 63 6f 6e  ime (as.** a con
26745 73 65 71 75 65 6e 63 65 20 6f 66 20 63 61 6c 6c  sequence of call
26746 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  ing sqlite3_init
26747 69 61 6c 69 7a 65 28 29 29 2e 0a 2a 2a 0a 2a 2a  ialize())..**.**
26748 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74   After this rout
26749 69 6e 65 20 72 75 6e 73 0a 2a 2f 0a 53 51 4c 49  ine runs.*/.SQLI
2674a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2674b 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 47  sqlite3RegisterG
2674c 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76  lobalFunctions(v
2674d 6f 69 64 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20  oid){.  /*.  ** 
2674e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72  The following ar
2674f 72 61 79 20 68 6f 6c 64 73 20 46 75 6e 63 44 65  ray holds FuncDe
26750 66 20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72  f structures for
26751 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 75 6e 63   all of the func
26752 74 69 6f 6e 73 0a 20 20 2a 2a 20 64 65 66 69 6e  tions.  ** defin
26753 65 64 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e  ed in this file.
26754 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61  .  **.  ** The a
26755 72 72 61 79 20 63 61 6e 6e 6f 74 20 62 65 20 63  rray cannot be c
26756 6f 6e 73 74 61 6e 74 20 73 69 6e 63 65 20 63 68  onstant since ch
26757 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 74  anges are made t
26758 6f 20 74 68 65 0a 20 20 2a 2a 20 46 75 6e 63 44  o the.  ** FuncD
26759 65 66 2e 70 48 61 73 68 20 65 6c 65 6d 65 6e 74  ef.pHash element
2675a 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e  s at start-time.
2675b 20 20 54 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f    The elements o
2675c 66 20 74 68 69 73 20 61 72 72 61 79 0a 20 20 2a  f this array.  *
2675d 2a 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20  * are read-only 
2675e 61 66 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61  after initializa
2675f 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65  tion is complete
26760 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  ..  */.  static 
26761 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44  SQLITE_WSD FuncD
26762 65 66 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b  ef aBuiltinFunc[
26763 5d 20 3d 20 7b 0a 20 20 20 20 46 55 4e 43 54 49  ] = {.    FUNCTI
26764 4f 4e 28 6c 74 72 69 6d 2c 20 20 20 20 20 20 20  ON(ltrim,       
26765 20 20 20 20 20 20 20 31 2c 20 31 2c 20 30 2c 20         1, 1, 0, 
26766 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20  trimFunc        
26767 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
26768 28 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20  (ltrim,         
26769 20 20 20 20 20 32 2c 20 31 2c 20 30 2c 20 74 72       2, 1, 0, tr
2676a 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29  imFunc         )
2676b 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72  ,.    FUNCTION(r
2676c 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20  trim,           
2676d 20 20 20 31 2c 20 32 2c 20 30 2c 20 74 72 69 6d     1, 2, 0, trim
2676e 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a  Func         ),.
2676f 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 74 72      FUNCTION(rtr
26770 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  im,             
26771 20 32 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46 75   2, 2, 0, trimFu
26772 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20  nc         ),.  
26773 20 20 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c    FUNCTION(trim,
26774 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
26775 2c 20 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63  , 3, 0, trimFunc
26776 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
26777 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20  FUNCTION(trim,  
26778 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20               2, 
26779 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20  3, 0, trimFunc  
2677a 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
2677b 4e 43 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20  NCTION(min,     
2677c 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c            -1, 0,
2677d 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20   1, minmaxFunc  
2677e 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
2677f 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20  TION(min,       
26780 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 31           0, 0, 1
26781 2c 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20  , 0             
26782 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47     ),.    AGGREG
26783 41 54 45 28 6d 69 6e 2c 20 20 20 20 20 20 20 20  ATE(min,        
26784 20 20 20 20 20 20 20 31 2c 20 30 2c 20 31 2c 20         1, 0, 1, 
26785 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20 20 20  minmaxStep,     
26786 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20   minMaxFinalize 
26787 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
26788 6d 61 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  max,            
26789 20 20 20 2d 31 2c 20 31 2c 20 31 2c 20 6d 69 6e     -1, 1, 1, min
2678a 6d 61 78 46 75 6e 63 20 20 20 20 20 20 20 29 2c  maxFunc       ),
2678b 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61  .    FUNCTION(ma
2678c 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
2678d 20 20 30 2c 20 31 2c 20 31 2c 20 30 20 20 20 20    0, 1, 1, 0    
2678e 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20              ),. 
2678f 20 20 20 41 47 47 52 45 47 41 54 45 28 6d 61 78     AGGREGATE(max
26790 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26791 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 53  1, 1, 1, minmaxS
26792 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78  tep,      minMax
26793 46 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20  Finalize ),.    
26794 46 55 4e 43 54 49 4f 4e 28 74 79 70 65 6f 66 2c  FUNCTION(typeof,
26795 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
26796 30 2c 20 30 2c 20 74 79 70 65 6f 66 46 75 6e 63  0, 0, typeofFunc
26797 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
26798 4e 43 54 49 4f 4e 28 6c 65 6e 67 74 68 2c 20 20  NCTION(length,  
26799 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
2679a 20 30 2c 20 6c 65 6e 67 74 68 46 75 6e 63 20 20   0, lengthFunc  
2679b 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
2679c 54 49 4f 4e 28 73 75 62 73 74 72 2c 20 20 20 20  TION(substr,    
2679d 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30           2, 0, 0
2679e 2c 20 73 75 62 73 74 72 46 75 6e 63 20 20 20 20  , substrFunc    
2679f 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
267a0 4f 4e 28 73 75 62 73 74 72 2c 20 20 20 20 20 20  ON(substr,      
267a1 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30 2c 20         3, 0, 0, 
267a2 73 75 62 73 74 72 46 75 6e 63 20 20 20 20 20 20  substrFunc      
267a3 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
267a4 28 61 62 73 2c 20 20 20 20 20 20 20 20 20 20 20  (abs,           
267a5 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 61 62       1, 0, 0, ab
267a6 73 46 75 6e 63 20 20 20 20 20 20 20 20 20 20 29  sFunc          )
267a7 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ,.#ifndef SQLITE
267a8 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
267a9 4f 49 4e 54 0a 20 20 20 20 46 55 4e 43 54 49 4f  OINT.    FUNCTIO
267aa 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20 20 20 20  N(round,        
267ab 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 72        1, 0, 0, r
267ac 6f 75 6e 64 46 75 6e 63 20 20 20 20 20 20 20 20  oundFunc        
267ad 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
267ae 72 6f 75 6e 64 2c 20 20 20 20 20 20 20 20 20 20  round,          
267af 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 72 6f 75      2, 0, 0, rou
267b0 6e 64 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c  ndFunc        ),
267b1 0a 23 65 6e 64 69 66 0a 20 20 20 20 46 55 4e 43  .#endif.    FUNC
267b2 54 49 4f 4e 28 75 70 70 65 72 2c 20 20 20 20 20  TION(upper,     
267b3 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
267b4 2c 20 75 70 70 65 72 46 75 6e 63 20 20 20 20 20  , upperFunc     
267b5 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
267b6 4f 4e 28 6c 6f 77 65 72 2c 20 20 20 20 20 20 20  ON(lower,       
267b7 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
267b8 6c 6f 77 65 72 46 75 6e 63 20 20 20 20 20 20 20  lowerFunc       
267b9 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
267ba 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20  (coalesce,      
267bb 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 30 20       1, 0, 0, 0 
267bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29                 )
267bd 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63  ,.    FUNCTION(c
267be 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20  oalesce,        
267bf 20 20 2d 31 2c 20 30 2c 20 30 2c 20 69 66 6e 75    -1, 0, 0, ifnu
267c0 6c 6c 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a  llFunc       ),.
267c1 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61      FUNCTION(coa
267c2 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20 20 20  lesce,          
267c3 20 30 2c 20 30 2c 20 30 2c 20 30 20 20 20 20 20   0, 0, 0, 0     
267c4 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20             ),.  
267c5 20 20 46 55 4e 43 54 49 4f 4e 28 68 65 78 2c 20    FUNCTION(hex, 
267c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
267c7 2c 20 30 2c 20 30 2c 20 68 65 78 46 75 6e 63 20  , 0, 0, hexFunc 
267c8 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
267c9 46 55 4e 43 54 49 4f 4e 28 69 66 6e 75 6c 6c 2c  FUNCTION(ifnull,
267ca 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20               2, 
267cb 30 2c 20 31 2c 20 69 66 6e 75 6c 6c 46 75 6e 63  0, 1, ifnullFunc
267cc 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
267cd 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 2c 20 20  NCTION(random,  
267ce 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c             0, 0,
267cf 20 30 2c 20 72 61 6e 64 6f 6d 46 75 6e 63 20 20   0, randomFunc  
267d0 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
267d1 54 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c 6f 62 2c  TION(randomblob,
267d2 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
267d3 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 20 20 20  , randomBlob    
267d4 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
267d5 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20 20 20 20 20  ON(nullif,      
267d6 20 20 20 20 20 20 20 32 2c 20 30 2c 20 31 2c 20         2, 0, 1, 
267d7 6e 75 6c 6c 69 66 46 75 6e 63 20 20 20 20 20 20  nullifFunc      
267d8 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
267d9 28 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e 2c  (sqlite_version,
267da 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 76 65       0, 0, 0, ve
267db 72 73 69 6f 6e 46 75 6e 63 20 20 20 20 20 20 29  rsionFunc      )
267dc 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73  ,.    FUNCTION(s
267dd 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 2c  qlite_source_id,
267de 20 20 20 30 2c 20 30 2c 20 30 2c 20 73 6f 75 72     0, 0, 0, sour
267df 63 65 69 64 46 75 6e 63 20 20 20 20 20 29 2c 0a  ceidFunc     ),.
267e0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 71 75 6f      FUNCTION(quo
267e1 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
267e2 20 31 2c 20 30 2c 20 30 2c 20 71 75 6f 74 65 46   1, 0, 0, quoteF
267e3 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20  unc        ),.  
267e4 20 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73 74 5f    FUNCTION(last_
267e5 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20 20 30  insert_rowid,  0
267e6 2c 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69 6e 73  , 0, 0, last_ins
267e7 65 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20 20 20  ert_rowid),.    
267e8 46 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67 65 73  FUNCTION(changes
267e9 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20  ,            0, 
267ea 30 2c 20 30 2c 20 63 68 61 6e 67 65 73 20 20 20  0, 0, changes   
267eb 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
267ec 4e 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63 68 61  NCTION(total_cha
267ed 6e 67 65 73 2c 20 20 20 20 20 20 30 2c 20 30 2c  nges,      0, 0,
267ee 20 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65   0, total_change
267ef 73 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43  s    ),.    FUNC
267f0 54 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20 20 20  TION(replace,   
267f1 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30           3, 0, 0
267f2 2c 20 72 65 70 6c 61 63 65 46 75 6e 63 20 20 20  , replaceFunc   
267f3 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
267f4 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20 20 20  ON(zeroblob,    
267f5 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
267f6 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 20 20  zeroblobFunc    
267f7 20 29 2c 0a 20 20 23 69 66 64 65 66 20 53 51 4c   ),.  #ifdef SQL
267f8 49 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20 20 20  ITE_SOUNDEX.    
267f9 46 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64 65 78  FUNCTION(soundex
267fa 2c 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20  ,            1, 
267fb 30 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46 75 6e  0, 0, soundexFun
267fc 63 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e 64  c      ),.  #end
267fd 69 66 0a 20 20 23 69 66 6e 64 65 66 20 53 51 4c  if.  #ifndef SQL
267fe 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
267ff 54 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55 4e 43  TENSION.    FUNC
26800 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73  TION(load_extens
26801 69 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c 20 30  ion,     1, 0, 0
26802 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20  , loadExt       
26803 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
26804 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  ON(load_extensio
26805 6e 2c 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20  n,     2, 0, 0, 
26806 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20 20 20  loadExt         
26807 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20 20   ),.  #endif.   
26808 20 41 47 47 52 45 47 41 54 45 28 73 75 6d 2c 20   AGGREGATE(sum, 
26809 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
2680a 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20   0, 0, sumStep, 
2680b 20 20 20 20 20 20 20 20 73 75 6d 46 69 6e 61 6c          sumFinal
2680c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47  ize    ),.    AG
2680d 47 52 45 47 41 54 45 28 74 6f 74 61 6c 2c 20 20  GREGATE(total,  
2680e 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
2680f 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20   0, sumStep,    
26810 20 20 20 20 20 74 6f 74 61 6c 46 69 6e 61 6c 69       totalFinali
26811 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47  ze    ),.    AGG
26812 52 45 47 41 54 45 28 61 76 67 2c 20 20 20 20 20  REGATE(avg,     
26813 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
26814 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20  0, sumStep,     
26815 20 20 20 20 61 76 67 46 69 6e 61 6c 69 7a 65 20      avgFinalize 
26816 20 20 20 29 2c 0a 20 2f 2a 20 41 47 47 52 45 47     ),. /* AGGREG
26817 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20  ATE(count,      
26818 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20         0, 0, 0, 
26819 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20  countStep,      
2681a 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20   countFinalize  
2681b 29 2c 20 2a 2f 0a 20 20 20 20 7b 30 2c 53 51 4c  ), */.    {0,SQL
2681c 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f  ITE_UTF8,SQLITE_
2681d 46 55 4e 43 5f 43 4f 55 4e 54 2c 30 2c 30 2c 30  FUNC_COUNT,0,0,0
2681e 2c 63 6f 75 6e 74 53 74 65 70 2c 63 6f 75 6e 74  ,countStep,count
2681f 46 69 6e 61 6c 69 7a 65 2c 22 63 6f 75 6e 74 22  Finalize,"count"
26820 2c 30 7d 2c 0a 20 20 20 20 41 47 47 52 45 47 41  ,0},.    AGGREGA
26821 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 20  TE(count,       
26822 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 63        1, 0, 0, c
26823 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 20  ountStep,       
26824 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29  countFinalize  )
26825 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28  ,.    AGGREGATE(
26826 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 20 20  group_concat,   
26827 20 20 20 31 2c 20 30 2c 20 30 2c 20 67 72 6f 75     1, 0, 0, grou
26828 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 72 6f  pConcatStep, gro
26829 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65  upConcatFinalize
2682a 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45  ),.    AGGREGATE
2682b 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 20  (group_concat,  
2682c 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 67 72 6f      2, 0, 0, gro
2682d 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 72  upConcatStep, gr
2682e 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a  oupConcatFinaliz
2682f 65 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b 45 46  e),.  .    LIKEF
26830 55 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26 67 6c  UNC(glob, 2, &gl
26831 6f 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45 5f 46  obInfo, SQLITE_F
26832 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f  UNC_LIKE|SQLITE_
26833 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 23 69  FUNC_CASE),.  #i
26834 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41 53 45  fdef SQLITE_CASE
26835 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 0a  _SENSITIVE_LIKE.
26836 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b      LIKEFUNC(lik
26837 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 6f 41  e, 2, &likeInfoA
26838 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  lt, SQLITE_FUNC_
26839 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43  LIKE|SQLITE_FUNC
2683a 5f 43 41 53 45 29 2c 0a 20 20 20 20 4c 49 4b 45  _CASE),.    LIKE
2683b 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20 26 6c  FUNC(like, 3, &l
2683c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 4c 49  ikeInfoAlt, SQLI
2683d 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c  TE_FUNC_LIKE|SQL
2683e 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a  ITE_FUNC_CASE),.
2683f 20 20 23 65 6c 73 65 0a 20 20 20 20 4c 49 4b 45    #else.    LIKE
26840 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 26 6c  FUNC(like, 2, &l
26841 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53 51 4c  ikeInfoNorm, SQL
26842 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 2c 0a  ITE_FUNC_LIKE),.
26843 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b      LIKEFUNC(lik
26844 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e  e, 3, &likeInfoN
26845 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43  orm, SQLITE_FUNC
26846 5f 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64 69 66  _LIKE),.  #endif
26847 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69 3b 0a  .  };..  int i;.
26848 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70    FuncDefHash *p
26849 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46  Hash = &GLOBAL(F
2684a 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69  uncDefHash, sqli
2684b 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f  te3GlobalFunctio
2684c 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a  ns);.  FuncDef *
2684d 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66  aFunc = (FuncDef
2684e 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  *)&GLOBAL(FuncDe
2684f 66 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 29  f, aBuiltinFunc)
26850 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
26851 41 72 72 61 79 53 69 7a 65 28 61 42 75 69 6c 74  ArraySize(aBuilt
26852 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b 0a 20  inFunc); i++){. 
26853 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65     sqlite3FuncDe
26854 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26  fInsert(pHash, &
26855 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 20  aFunc[i]);.  }. 
26856 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
26857 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e  DateTimeFunction
26858 73 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  s();.}../*******
26859 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
2685a 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  unc.c **********
2685b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2685c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2685d 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2685e 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2685f 6c 65 20 66 6b 65 79 2e 63 20 2a 2a 2a 2a 2a 2a  le fkey.c ******
26860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26861 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26862 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 0a 2a 2a  ******/./*.**.**
26863 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
26864 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
26865 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
26866 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
26867 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
26868 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
26869 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2686a 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2686b 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2686c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2686d 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2686e 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2686f 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
26870 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
26871 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
26872 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
26873 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
26874 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26875 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26876 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26877 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26878 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
26879 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2687a 63 6f 64 65 20 75 73 65 64 20 62 79 20 74 68 65  code used by the
2687b 20 63 6f 6d 70 69 6c 65 72 20 74 6f 20 61 64 64   compiler to add
2687c 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 2a 2a 20   foreign key.** 
2687d 73 75 70 70 6f 72 74 20 74 6f 20 63 6f 6d 70 69  support to compi
2687e 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  led SQL statemen
2687f 74 73 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ts..*/..#ifndef 
26880 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
26881 49 47 4e 5f 4b 45 59 0a 23 69 66 6e 64 65 66 20  IGN_KEY.#ifndef 
26882 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
26883 47 45 52 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72  GER../*.** Defer
26884 72 65 64 20 61 6e 64 20 49 6d 6d 65 64 69 61 74  red and Immediat
26885 65 20 46 4b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d  e FKs.** -------
26886 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26887 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 46 6f 72 65 69 67  ---.**.** Foreig
26888 6e 20 6b 65 79 73 20 69 6e 20 53 51 4c 69 74 65  n keys in SQLite
26889 20 63 6f 6d 65 20 69 6e 20 74 77 6f 20 66 6c 61   come in two fla
2688a 76 6f 75 72 73 3a 20 64 65 66 65 72 72 65 64 20  vours: deferred 
2688b 61 6e 64 20 69 6d 6d 65 64 69 61 74 65 2e 0a 2a  and immediate..*
2688c 2a 20 49 66 20 61 6e 20 69 6d 6d 65 64 69 61 74  * If an immediat
2688d 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  e foreign key co
2688e 6e 73 74 72 61 69 6e 74 20 69 73 20 76 69 6f 6c  nstraint is viol
2688f 61 74 65 64 2c 20 53 51 4c 49 54 45 5f 43 4f 4e  ated, SQLITE_CON
26890 53 54 52 41 49 4e 54 0a 2a 2a 20 69 73 20 72 65  STRAINT.** is re
26891 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 63  turned and the c
26892 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74  urrent statement
26893 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c   transaction rol
26894 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 61 20 0a  led back. If a .
26895 2a 2a 20 64 65 66 65 72 72 65 64 20 66 6f 72 65  ** deferred fore
26896 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
26897 6e 74 20 69 73 20 76 69 6f 6c 61 74 65 64 2c 20  nt is violated, 
26898 6e 6f 20 61 63 74 69 6f 6e 20 69 73 20 74 61 6b  no action is tak
26899 65 6e 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65  en .** immediate
2689a 6c 79 2e 20 48 6f 77 65 76 65 72 20 69 66 20 74  ly. However if t
2689b 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 61  he application a
2689c 74 74 65 6d 70 74 73 20 74 6f 20 63 6f 6d 6d 69  ttempts to commi
2689d 74 20 74 68 65 20 0a 2a 2a 20 74 72 61 6e 73 61  t the .** transa
2689e 63 74 69 6f 6e 20 62 65 66 6f 72 65 20 66 69 78  ction before fix
2689f 69 6e 67 20 74 68 65 20 63 6f 6e 73 74 72 61 69  ing the constrai
268a0 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 2c 20 74 68  nt violation, th
268a1 65 20 61 74 74 65 6d 70 74 20 66 61 69 6c 73 2e  e attempt fails.
268a2 0a 2a 2a 0a 2a 2a 20 44 65 66 65 72 72 65 64 20  .**.** Deferred 
268a3 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20  constraints are 
268a4 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e  implemented usin
268a5 67 20 61 20 73 69 6d 70 6c 65 20 63 6f 75 6e 74  g a simple count
268a6 65 72 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  er associated.**
268a7 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
268a8 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 63  se handle. The c
268a9 6f 75 6e 74 65 72 20 69 73 20 73 65 74 20 74 6f  ounter is set to
268aa 20 7a 65 72 6f 20 65 61 63 68 20 74 69 6d 65 20   zero each time 
268ab 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74  a .** database t
268ac 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70  ransaction is op
268ad 65 6e 65 64 2e 20 45 61 63 68 20 74 69 6d 65 20  ened. Each time 
268ae 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65  a statement is e
268af 78 65 63 75 74 65 64 20 0a 2a 2a 20 74 68 61 74  xecuted .** that
268b0 20 63 61 75 73 65 73 20 61 20 66 6f 72 65 69 67   causes a foreig
268b1 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e 2c  n key violation,
268b2 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20   the counter is 
268b3 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 45 61 63  incremented. Eac
268b4 68 0a 2a 2a 20 74 69 6d 65 20 61 20 73 74 61 74  h.** time a stat
268b5 65 6d 65 6e 74 20 69 73 20 65 78 65 63 75 74 65  ement is execute
268b6 64 20 74 68 61 74 20 72 65 6d 6f 76 65 73 20 61  d that removes a
268b7 6e 20 65 78 69 73 74 69 6e 67 20 76 69 6f 6c 61  n existing viola
268b8 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  tion from.** the
268b9 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63   database, the c
268ba 6f 75 6e 74 65 72 20 69 73 20 64 65 63 72 65 6d  ounter is decrem
268bb 65 6e 74 65 64 2e 20 57 68 65 6e 20 74 68 65 20  ented. When the 
268bc 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a  transaction is.*
268bd 2a 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74 68 65  * committed, the
268be 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20 69 66   commit fails if
268bf 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   the current val
268c0 75 65 20 6f 66 20 74 68 65 20 63 6f 75 6e 74 65  ue of the counte
268c1 72 20 69 73 0a 2a 2a 20 67 72 65 61 74 65 72 20  r is.** greater 
268c2 74 68 61 6e 20 7a 65 72 6f 2e 20 54 68 69 73 20  than zero. This 
268c3 73 63 68 65 6d 65 20 68 61 73 20 74 77 6f 20 62  scheme has two b
268c4 69 67 20 64 72 61 77 62 61 63 6b 73 3a 0a 2a 2a  ig drawbacks:.**
268c5 0a 2a 2a 20 20 20 2a 20 57 68 65 6e 20 61 20 63  .**   * When a c
268c6 6f 6d 6d 69 74 20 66 61 69 6c 73 20 64 75 65 20  ommit fails due 
268c7 74 6f 20 61 20 64 65 66 65 72 72 65 64 20 66 6f  to a deferred fo
268c8 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
268c9 61 69 6e 74 2c 20 0a 2a 2a 20 20 20 20 20 74 68  aint, .**     th
268ca 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f  ere is no way to
268cb 20 74 65 6c 6c 20 77 68 69 63 68 20 66 6f 72 65   tell which fore
268cc 69 67 6e 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  ign constraint i
268cd 73 20 6e 6f 74 20 73 61 74 69 73 66 69 65 64 2c  s not satisfied,
268ce 0a 2a 2a 20 20 20 20 20 6f 72 20 77 68 69 63 68  .**     or which
268cf 20 72 6f 77 20 69 74 20 69 73 20 6e 6f 74 20 73   row it is not s
268d0 61 74 69 73 66 69 65 64 20 66 6f 72 2e 0a 2a 2a  atisfied for..**
268d1 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 64  .**   * If the d
268d2 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 73  atabase contains
268d3 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f   foreign key vio
268d4 6c 61 74 69 6f 6e 73 20 77 68 65 6e 20 74 68 65  lations when the
268d5 20 0a 2a 2a 20 20 20 20 20 74 72 61 6e 73 61 63   .**     transac
268d6 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2c 20  tion is opened, 
268d7 74 68 69 73 20 6d 61 79 20 63 61 75 73 65 20 74  this may cause t
268d8 68 65 20 6d 65 63 68 61 6e 69 73 6d 20 74 6f 20  he mechanism to 
268d9 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  malfunction..**.
268da 2a 2a 20 44 65 73 70 69 74 65 20 74 68 65 73 65  ** Despite these
268db 20 70 72 6f 62 6c 65 6d 73 2c 20 74 68 69 73 20   problems, this 
268dc 61 70 70 72 6f 61 63 68 20 69 73 20 61 64 6f 70  approach is adop
268dd 74 65 64 20 61 73 20 69 74 20 73 65 65 6d 73 20  ted as it seems 
268de 73 69 6d 70 6c 65 72 0a 2a 2a 20 74 68 61 6e 20  simpler.** than 
268df 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 73  the alternatives
268e0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 53 45 52 54 20 6f  ..**.** INSERT o
268e1 70 65 72 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a  perations:.**.**
268e2 20 20 20 49 2e 31 29 20 46 6f 72 20 65 61 63 68     I.1) For each
268e3 20 46 4b 20 66 6f 72 20 77 68 69 63 68 20 74 68   FK for which th
268e4 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 63  e table is the c
268e5 68 69 6c 64 20 74 61 62 6c 65 2c 20 73 65 61 72  hild table, sear
268e6 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 74 68 65  ch.**        the
268e7 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 66 6f   parent table fo
268e8 72 20 61 20 6d 61 74 63 68 2e 20 49 66 20 6e 6f  r a match. If no
268e9 6e 65 20 69 73 20 66 6f 75 6e 64 20 69 6e 63 72  ne is found incr
268ea 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 20 20 20  ement the.**    
268eb 20 20 20 20 63 6f 6e 73 74 72 61 69 6e 74 20 63      constraint c
268ec 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20  ounter..**.**   
268ed 49 2e 32 29 20 46 6f 72 20 65 61 63 68 20 46 4b  I.2) For each FK
268ee 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 74   for which the t
268ef 61 62 6c 65 20 69 73 20 74 68 65 20 70 61 72 65  able is the pare
268f0 6e 74 20 74 61 62 6c 65 2c 20 0a 2a 2a 20 20 20  nt table, .**   
268f1 20 20 20 20 20 73 65 61 72 63 68 20 74 68 65 20       search the 
268f2 63 68 69 6c 64 20 74 61 62 6c 65 20 66 6f 72 20  child table for 
268f3 72 6f 77 73 20 74 68 61 74 20 63 6f 72 72 65 73  rows that corres
268f4 70 6f 6e 64 20 74 6f 20 74 68 65 20 6e 65 77 0a  pond to the new.
268f5 2a 2a 20 20 20 20 20 20 20 20 72 6f 77 20 69 6e  **        row in
268f6 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
268f7 65 2e 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  e. Decrement the
268f8 20 63 6f 75 6e 74 65 72 20 66 6f 72 20 65 61 63   counter for eac
268f9 68 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  h row.**        
268fa 66 6f 75 6e 64 20 28 61 73 20 74 68 65 20 63 6f  found (as the co
268fb 6e 73 74 72 61 69 6e 74 20 69 73 20 6e 6f 77 20  nstraint is now 
268fc 73 61 74 69 73 66 69 65 64 29 2e 0a 2a 2a 0a 2a  satisfied)..**.*
268fd 2a 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69  * DELETE operati
268fe 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 44 2e 31  ons:.**.**   D.1
268ff 29 20 46 6f 72 20 65 61 63 68 20 46 4b 20 66 6f  ) For each FK fo
26900 72 20 77 68 69 63 68 20 74 68 65 20 74 61 62 6c  r which the tabl
26901 65 20 69 73 20 74 68 65 20 63 68 69 6c 64 20 74  e is the child t
26902 61 62 6c 65 2c 20 0a 2a 2a 20 20 20 20 20 20 20  able, .**       
26903 20 73 65 61 72 63 68 20 74 68 65 20 70 61 72 65   search the pare
26904 6e 74 20 74 61 62 6c 65 20 66 6f 72 20 61 20 72  nt table for a r
26905 6f 77 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  ow that correspo
26906 6e 64 73 20 74 6f 20 74 68 65 20 0a 2a 2a 20 20  nds to the .**  
26907 20 20 20 20 20 20 64 65 6c 65 74 65 64 20 72 6f        deleted ro
26908 77 20 69 6e 20 74 68 65 20 63 68 69 6c 64 20 74  w in the child t
26909 61 62 6c 65 2e 20 49 66 20 73 75 63 68 20 61 20  able. If such a 
2690a 72 6f 77 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64  row is not found
2690b 2c 20 0a 2a 2a 20 20 20 20 20 20 20 20 64 65 63  , .**        dec
2690c 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74  rement the count
2690d 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 2e 32 29  er..**.**   D.2)
2690e 20 46 6f 72 20 65 61 63 68 20 46 4b 20 66 6f 72   For each FK for
2690f 20 77 68 69 63 68 20 74 68 65 20 74 61 62 6c 65   which the table
26910 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 74   is the parent t
26911 61 62 6c 65 2c 20 73 65 61 72 63 68 20 0a 2a 2a  able, search .**
26912 20 20 20 20 20 20 20 20 74 68 65 20 63 68 69 6c          the chil
26913 64 20 74 61 62 6c 65 20 66 6f 72 20 72 6f 77 73  d table for rows
26914 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64   that correspond
26915 20 74 6f 20 74 68 65 20 64 65 6c 65 74 65 64 20   to the deleted 
26916 72 6f 77 20 0a 2a 2a 20 20 20 20 20 20 20 20 69  row .**        i
26917 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  n the parent tab
26918 6c 65 2e 20 46 6f 72 20 65 61 63 68 20 66 6f 75  le. For each fou
26919 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  nd increment the
2691a 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   counter..**.** 
2691b 55 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e  UPDATE operation
2691c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 41 6e 20 55 50  s:.**.**   An UP
2691d 44 41 54 45 20 63 6f 6d 6d 61 6e 64 20 72 65 71  DATE command req
2691e 75 69 72 65 73 20 74 68 61 74 20 61 6c 6c 20 34  uires that all 4
2691f 20 73 74 65 70 73 20 61 62 6f 76 65 20 61 72 65   steps above are
26920 20 74 61 6b 65 6e 2c 20 62 75 74 20 6f 6e 6c 79   taken, but only
26921 0a 2a 2a 20 20 20 66 6f 72 20 46 4b 20 63 6f 6e  .**   for FK con
26922 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77 68 69  straints for whi
26923 63 68 20 74 68 65 20 61 66 66 65 63 74 65 64 20  ch the affected 
26924 63 6f 6c 75 6d 6e 73 20 61 72 65 20 61 63 74 75  columns are actu
26925 61 6c 6c 79 20 0a 2a 2a 20 20 20 6d 6f 64 69 66  ally .**   modif
26926 69 65 64 20 28 76 61 6c 75 65 73 20 6d 75 73 74  ied (values must
26927 20 62 65 20 63 6f 6d 70 61 72 65 64 20 61 74 20   be compared at 
26928 72 75 6e 74 69 6d 65 29 2e 0a 2a 2a 0a 2a 2a 20  runtime)..**.** 
26929 4e 6f 74 65 20 74 68 61 74 20 49 2e 31 20 61 6e  Note that I.1 an
2692a 64 20 44 2e 31 20 61 72 65 20 76 65 72 79 20 73  d D.1 are very s
2692b 69 6d 69 6c 61 72 20 6f 70 65 72 61 74 69 6f 6e  imilar operation
2692c 73 2c 20 61 73 20 61 72 65 20 49 2e 32 20 61 6e  s, as are I.2 an
2692d 64 20 44 2e 32 2e 0a 2a 2a 20 54 68 69 73 20 73  d D.2..** This s
2692e 69 6d 70 6c 69 66 69 65 73 20 74 68 65 20 69 6d  implifies the im
2692f 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 20 62  plementation a b
26930 69 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  it..**.** For th
26931 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 69 6d  e purposes of im
26932 6d 65 64 69 61 74 65 20 46 4b 20 63 6f 6e 73 74  mediate FK const
26933 72 61 69 6e 74 73 2c 20 74 68 65 20 4f 52 20 52  raints, the OR R
26934 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63 74 0a  EPLACE conflict.
26935 2a 2a 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73  ** resolution is
26936 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 64   considered to d
26937 65 6c 65 74 65 20 72 6f 77 73 20 62 65 66 6f 72  elete rows befor
26938 65 20 74 68 65 20 6e 65 77 20 72 6f 77 20 69 73  e the new row is
26939 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 20 49 66   inserted..** If
2693a 20 61 20 64 65 6c 65 74 65 20 63 61 75 73 65 64   a delete caused
2693b 20 62 79 20 4f 52 20 52 45 50 4c 41 43 45 20 76   by OR REPLACE v
2693c 69 6f 6c 61 74 65 73 20 61 6e 20 46 4b 20 63 6f  iolates an FK co
2693d 6e 73 74 72 61 69 6e 74 2c 20 61 6e 20 65 78 63  nstraint, an exc
2693e 65 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 74 68 72  eption.** is thr
2693f 6f 77 6e 2c 20 65 76 65 6e 20 69 66 20 74 68 65  own, even if the
26940 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 77   FK constraint w
26941 6f 75 6c 64 20 62 65 20 73 61 74 69 73 66 69 65  ould be satisfie
26942 64 20 61 66 74 65 72 20 74 68 65 20 6e 65 77 20  d after the new 
26943 0a 2a 2a 20 72 6f 77 20 69 73 20 69 6e 73 65 72  .** row is inser
26944 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6d 6d 65 64  ted..**.** Immed
26945 69 61 74 65 20 63 6f 6e 73 74 72 61 69 6e 74 73  iate constraints
26946 20 61 72 65 20 75 73 75 61 6c 6c 79 20 68 61 6e   are usually han
26947 64 6c 65 64 20 73 69 6d 69 6c 61 72 6c 79 2e 20  dled similarly. 
26948 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65  The only differe
26949 6e 63 65 20 0a 2a 2a 20 69 73 20 74 68 61 74 20  nce .** is that 
2694a 74 68 65 20 63 6f 75 6e 74 65 72 20 75 73 65 64  the counter used
2694b 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 70 61   is stored as pa
2694c 72 74 20 6f 66 20 65 61 63 68 20 69 6e 64 69 76  rt of each indiv
2694d 69 64 75 61 6c 20 73 74 61 74 65 6d 65 6e 74 0a  idual statement.
2694e 2a 2a 20 6f 62 6a 65 63 74 20 28 73 74 72 75 63  ** object (struc
2694f 74 20 56 64 62 65 29 2e 20 49 66 2c 20 61 66 74  t Vdbe). If, aft
26950 65 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  er the statement
26951 20 68 61 73 20 72 75 6e 2c 20 69 74 73 20 69 6d   has run, its im
26952 6d 65 64 69 61 74 65 0a 2a 2a 20 63 6f 6e 73 74  mediate.** const
26953 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 20 69 73  raint counter is
26954 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65   greater than ze
26955 72 6f 2c 20 69 74 20 72 65 74 75 72 6e 73 20 53  ro, it returns S
26956 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
26957 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74  .** and the stat
26958 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
26959 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n is rolled back
2695a 2e 20 41 6e 20 65 78 63 65 70 74 69 6f 6e 20 69  . An exception i
2695b 73 20 61 6e 20 49 4e 53 45 52 54 0a 2a 2a 20 73  s an INSERT.** s
2695c 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 69 6e  tatement that in
2695d 73 65 72 74 73 20 61 20 73 69 6e 67 6c 65 20 72  serts a single r
2695e 6f 77 20 6f 6e 6c 79 20 28 6e 6f 20 74 72 69 67  ow only (no trig
2695f 67 65 72 73 29 2e 20 49 6e 20 74 68 69 73 20 63  gers). In this c
26960 61 73 65 2c 0a 2a 2a 20 69 6e 73 74 65 61 64 20  ase,.** instead 
26961 6f 66 20 75 73 69 6e 67 20 61 20 63 6f 75 6e 74  of using a count
26962 65 72 2c 20 61 6e 20 65 78 63 65 70 74 69 6f 6e  er, an exception
26963 20 69 73 20 74 68 72 6f 77 6e 20 69 6d 6d 65 64   is thrown immed
26964 69 61 74 65 6c 79 20 69 66 20 74 68 65 0a 2a 2a  iately if the.**
26965 20 49 4e 53 45 52 54 20 76 69 6f 6c 61 74 65 73   INSERT violates
26966 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63   a foreign key c
26967 6f 6e 73 74 72 61 69 6e 74 2e 20 54 68 69 73 20  onstraint. This 
26968 69 73 20 6e 65 63 65 73 73 61 72 79 20 61 73 20  is necessary as 
26969 73 75 63 68 0a 2a 2a 20 61 6e 20 49 4e 53 45 52  such.** an INSER
2696a 54 20 64 6f 65 73 20 6e 6f 74 20 6f 70 65 6e 20  T does not open 
2696b 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  a statement tran
2696c 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  saction..**.** T
2696d 4f 44 4f 3a 20 48 6f 77 20 73 68 6f 75 6c 64 20  ODO: How should 
2696e 64 72 6f 70 70 69 6e 67 20 61 20 74 61 62 6c 65  dropping a table
2696f 20 62 65 20 68 61 6e 64 6c 65 64 3f 20 48 6f 77   be handled? How
26970 20 73 68 6f 75 6c 64 20 72 65 6e 61 6d 69 6e 67   should renaming
26971 20 61 20 0a 2a 2a 20 74 61 62 6c 65 20 62 65 20   a .** table be 
26972 68 61 6e 64 6c 65 64 3f 0a 2a 2a 0a 2a 2a 0a 2a  handled?.**.**.*
26973 2a 20 51 75 65 72 79 20 41 50 49 20 4e 6f 74 65  * Query API Note
26974 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  s.** -----------
26975 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72  ----.**.** Befor
26976 65 20 63 6f 64 69 6e 67 20 61 6e 20 55 50 44 41  e coding an UPDA
26977 54 45 20 6f 72 20 44 45 4c 45 54 45 20 72 6f 77  TE or DELETE row
26978 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 20   operation, the 
26979 63 6f 64 65 2d 67 65 6e 65 72 61 74 6f 72 0a 2a  code-generator.*
2697a 2a 20 66 6f 72 20 74 68 6f 73 65 20 74 77 6f 20  * for those two 
2697b 6f 70 65 72 61 74 69 6f 6e 73 20 6e 65 65 64 73  operations needs
2697c 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 72   to know whether
2697d 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 65 72   or not the oper
2697e 61 74 69 6f 6e 0a 2a 2a 20 72 65 71 75 69 72 65  ation.** require
2697f 73 20 61 6e 79 20 46 4b 20 70 72 6f 63 65 73 73  s any FK process
26980 69 6e 67 20 61 6e 64 2c 20 69 66 20 73 6f 2c 20  ing and, if so, 
26981 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 6f 66  which columns of
26982 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a   the original.**
26983 20 72 6f 77 20 61 72 65 20 72 65 71 75 69 72 65   row are require
26984 64 20 62 79 20 74 68 65 20 46 4b 20 70 72 6f 63  d by the FK proc
26985 65 73 73 69 6e 67 20 56 44 42 45 20 63 6f 64 65  essing VDBE code
26986 20 28 69 2e 65 2e 20 69 66 20 46 4b 73 20 77 65   (i.e. if FKs we
26987 72 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65  re.** implemente
26988 64 20 75 73 69 6e 67 20 74 72 69 67 67 65 72 73  d using triggers
26989 2c 20 77 68 69 63 68 20 6f 66 20 74 68 65 20 6f  , which of the o
2698a 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 77 6f 75  ld.* columns wou
2698b 6c 64 20 62 65 20 0a 2a 2a 20 61 63 63 65 73 73  ld be .** access
2698c 65 64 29 2e 20 4e 6f 20 69 6e 66 6f 72 6d 61 74  ed). No informat
2698d 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 20  ion is required 
2698e 62 79 20 74 68 65 20 63 6f 64 65 2d 67 65 6e 65  by the code-gene
2698f 72 61 74 6f 72 20 62 65 66 6f 72 65 0a 2a 2a 20  rator before.** 
26990 63 6f 64 69 6e 67 20 61 6e 20 49 4e 53 45 52 54  coding an INSERT
26991 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20   operation. The 
26992 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 62  functions used b
26993 79 20 74 68 65 20 55 50 44 41 54 45 2f 44 45 4c  y the UPDATE/DEL
26994 45 54 45 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6f  ETE.** generatio
26995 6e 20 63 6f 64 65 20 74 6f 20 71 75 65 72 79 20  n code to query 
26996 66 6f 72 20 74 68 69 73 20 69 6e 66 6f 72 6d 61  for this informa
26997 74 69 6f 6e 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20  tion are:.**.** 
26998 20 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69    sqlite3FkRequi
26999 72 65 64 28 29 20 2d 20 54 65 73 74 20 74 6f 20  red() - Test to 
2699a 73 65 65 20 69 66 20 46 4b 20 70 72 6f 63 65 73  see if FK proces
2699b 73 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64  sing is required
2699c 2e 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 6b  ..**   sqlite3Fk
2699d 4f 6c 64 6d 61 73 6b 28 29 20 20 2d 20 51 75 65  Oldmask()  - Que
2699e 72 79 20 66 6f 72 20 74 68 65 20 73 65 74 20 6f  ry for the set o
2699f 66 20 72 65 71 75 69 72 65 64 20 6f 6c 64 2e 2a  f required old.*
269a0 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 0a   columns..**.**.
269a1 2a 2a 20 45 78 74 65 72 6e 61 6c 6c 79 20 61 63  ** Externally ac
269a2 63 65 73 73 69 62 6c 65 20 6d 6f 64 75 6c 65 20  cessible module 
269a3 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 2d 2d 2d  functions.** ---
269a4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
269a5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
269a6 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 20 73 71 6c 69  ---.**.**   sqli
269a7 74 65 33 46 6b 43 68 65 63 6b 28 29 20 20 20 20  te3FkCheck()    
269a8 2d 20 43 68 65 63 6b 20 66 6f 72 20 66 6f 72 65  - Check for fore
269a9 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69 6f  ign key violatio
269aa 6e 73 2e 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33  ns..**   sqlite3
269ab 46 6b 41 63 74 69 6f 6e 73 28 29 20 20 2d 20 43  FkActions()  - C
269ac 6f 64 65 20 74 72 69 67 67 65 72 73 20 66 6f 72  ode triggers for
269ad 20 4f 4e 20 55 50 44 41 54 45 2f 4f 4e 20 44 45   ON UPDATE/ON DE
269ae 4c 45 54 45 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a  LETE actions..**
269af 20 20 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65     sqlite3FkDele
269b0 74 65 28 29 20 20 20 2d 20 44 65 6c 65 74 65 20  te()   - Delete 
269b1 61 6e 20 46 4b 65 79 20 73 74 72 75 63 74 75 72  an FKey structur
269b2 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 44 42  e..*/../*.** VDB
269b3 45 20 43 61 6c 6c 69 6e 67 20 43 6f 6e 76 65 6e  E Calling Conven
269b4 74 69 6f 6e 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d  tion.** --------
269b5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
269b6 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a  **.** Example:.*
269b7 2a 0a 2a 2a 20 20 20 46 6f 72 20 74 68 65 20 66  *.**   For the f
269b8 6f 6c 6c 6f 77 69 6e 67 20 49 4e 53 45 52 54 20  ollowing INSERT 
269b9 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a  statement:.**.**
269ba 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
269bb 45 20 74 31 28 61 2c 20 62 20 49 4e 54 45 47 45  E t1(a, b INTEGE
269bc 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63  R PRIMARY KEY, c
269bd 29 3b 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54  );.**     INSERT
269be 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
269bf 31 2c 20 32 2c 20 33 2e 31 29 3b 0a 2a 2a 0a 2a  1, 2, 3.1);.**.*
269c0 2a 20 20 20 52 65 67 69 73 74 65 72 20 28 78 29  *   Register (x)
269c1 3a 20 20 20 20 20 20 20 20 32 20 20 20 20 28 74  :        2    (t
269c2 79 70 65 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20  ype integer).** 
269c3 20 20 52 65 67 69 73 74 65 72 20 28 78 2b 31 29    Register (x+1)
269c4 3a 20 20 20 20 20 20 31 20 20 20 20 28 74 79 70  :      1    (typ
269c5 65 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20 20 20  e integer).**   
269c6 52 65 67 69 73 74 65 72 20 28 78 2b 32 29 3a 20  Register (x+2): 
269c7 20 20 20 20 20 4e 55 4c 4c 20 28 74 79 70 65 20       NULL (type 
269c8 4e 55 4c 4c 29 0a 2a 2a 20 20 20 52 65 67 69 73  NULL).**   Regis
269c9 74 65 72 20 28 78 2b 33 29 3a 20 20 20 20 20 20  ter (x+3):      
269ca 33 2e 31 20 20 28 74 79 70 65 20 72 65 61 6c 29  3.1  (type real)
269cb 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 66 6f 72  .*/../*.** A for
269cc 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
269cd 69 6e 74 20 72 65 71 75 69 72 65 73 20 74 68 61  int requires tha
269ce 74 20 74 68 65 20 6b 65 79 20 63 6f 6c 75 6d 6e  t the key column
269cf 73 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 0a  s in the parent.
269d0 2a 2a 20 74 61 62 6c 65 20 61 72 65 20 63 6f 6c  ** table are col
269d1 6c 65 63 74 69 76 65 6c 79 20 73 75 62 6a 65 63  lectively subjec
269d2 74 20 74 6f 20 61 20 55 4e 49 51 55 45 20 6f 72  t to a UNIQUE or
269d3 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e   PRIMARY KEY con
269d4 73 74 72 61 69 6e 74 2e 0a 2a 2a 20 47 69 76 65  straint..** Give
269d5 6e 20 74 68 61 74 20 70 50 61 72 65 6e 74 20 69  n that pParent i
269d6 73 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  s the parent tab
269d7 6c 65 20 66 6f 72 20 66 6f 72 65 69 67 6e 20 6b  le for foreign k
269d8 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 46  ey constraint pF
269d9 4b 65 79 2c 20 0a 2a 2a 20 73 65 61 72 63 68 20  Key, .** search 
269da 74 68 65 20 73 63 68 65 6d 61 20 61 20 75 6e 69  the schema a uni
269db 71 75 65 20 69 6e 64 65 78 20 6f 6e 20 74 68 65  que index on the
269dc 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
269dd 6d 6e 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 73  mns. .**.** If s
269de 75 63 63 65 73 73 66 75 6c 2c 20 7a 65 72 6f 20  uccessful, zero 
269df 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
269e0 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 69  the parent key i
269e1 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49  s an INTEGER PRI
269e2 4d 41 52 59 20 0a 2a 2a 20 4b 45 59 20 63 6f 6c  MARY .** KEY col
269e3 75 6d 6e 2c 20 74 68 65 6e 20 6f 75 74 70 75 74  umn, then output
269e4 20 76 61 72 69 61 62 6c 65 20 2a 70 70 49 64 78   variable *ppIdx
269e5 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
269e6 20 4f 74 68 65 72 77 69 73 65 2c 20 2a 70 70 49   Otherwise, *ppI
269e7 64 78 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  dx .** is set to
269e8 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 75 6e   point to the un
269e9 69 71 75 65 20 69 6e 64 65 78 2e 20 0a 2a 2a 20  ique index. .** 
269ea 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e  .** If the paren
269eb 74 20 6b 65 79 20 63 6f 6e 73 69 73 74 73 20 6f  t key consists o
269ec 66 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  f a single colum
269ed 6e 20 28 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  n (the foreign k
269ee 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a  ey constraint.**
269ef 20 69 73 20 6e 6f 74 20 61 20 63 6f 6d 70 6f 73   is not a compos
269f0 69 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 29  ite foreign key)
269f1 2c 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  , output variabl
269f2 65 20 2a 70 61 69 43 6f 6c 20 69 73 20 73 65 74  e *paiCol is set
269f3 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 4f 74 68   to NULL..** Oth
269f4 65 72 77 69 73 65 2c 20 69 74 20 69 73 20 73 65  erwise, it is se
269f5 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 6e  t to point to an
269f6 20 61 6c 6c 6f 63 61 74 65 64 20 61 72 72 61 79   allocated array
269f7 20 6f 66 20 73 69 7a 65 20 4e 2c 20 77 68 65 72   of size N, wher
269f8 65 0a 2a 2a 20 4e 20 69 73 20 74 68 65 20 6e 75  e.** N is the nu
269f9 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
269fa 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65  in the parent ke
269fb 79 2e 20 54 68 65 20 66 69 72 73 74 20 65 6c 65  y. The first ele
269fc 6d 65 6e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61  ment of the.** a
269fd 72 72 61 79 20 69 73 20 74 68 65 20 69 6e 64 65  rray is the inde
269fe 78 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 74  x of the child t
269ff 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74  able column that
26a00 20 69 73 20 6d 61 70 70 65 64 20 62 79 20 74 68   is mapped by th
26a01 65 20 46 4b 0a 2a 2a 20 63 6f 6e 73 74 72 61 69  e FK.** constrai
26a02 6e 74 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74  nt to the parent
26a03 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 73 74   table column st
26a04 6f 72 65 64 20 69 6e 20 74 68 65 20 6c 65 66 74  ored in the left
26a05 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  -most column.** 
26a06 6f 66 20 69 6e 64 65 78 20 2a 70 70 49 64 78 2e  of index *ppIdx.
26a07 20 54 68 65 20 73 65 63 6f 6e 64 20 65 6c 65 6d   The second elem
26a08 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 79  ent of the array
26a09 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
26a0a 20 74 68 65 0a 2a 2a 20 63 68 69 6c 64 20 74 61   the.** child ta
26a0b 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  ble column that 
26a0c 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74  corresponds to t
26a0d 68 65 20 73 65 63 6f 6e 64 20 6c 65 66 74 2d 6d  he second left-m
26a0e 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a  ost column of.**
26a0f 20 2a 70 70 49 64 78 2c 20 61 6e 64 20 73 6f 20   *ppIdx, and so 
26a10 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  on..**.** If the
26a11 20 72 65 71 75 69 72 65 64 20 69 6e 64 65 78 20   required index 
26a12 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c  cannot be found,
26a13 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 3a   either because:
26a14 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 54 68 65 20  .**.**   1) The 
26a15 6e 61 6d 65 64 20 70 61 72 65 6e 74 20 6b 65 79  named parent key
26a16 20 63 6f 6c 75 6d 6e 73 20 64 6f 20 6e 6f 74 20   columns do not 
26a17 65 78 69 73 74 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  exist, or.**.** 
26a18 20 20 32 29 20 54 68 65 20 6e 61 6d 65 64 20 70    2) The named p
26a19 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
26a1a 73 20 64 6f 20 65 78 69 73 74 2c 20 62 75 74 20  s do exist, but 
26a1b 61 72 65 20 6e 6f 74 20 73 75 62 6a 65 63 74 20  are not subject 
26a1c 74 6f 20 61 0a 2a 2a 20 20 20 20 20 20 55 4e 49  to a.**      UNI
26a1d 51 55 45 20 6f 72 20 50 52 49 4d 41 52 59 20 4b  QUE or PRIMARY K
26a1e 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6f  EY constraint, o
26a1f 72 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 4e 6f 20  r.**.**   3) No 
26a20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d  parent key colum
26a21 6e 73 20 77 65 72 65 20 70 72 6f 76 69 64 65 64  ns were provided
26a22 20 65 78 70 6c 69 63 69 74 6c 79 20 61 73 20 70   explicitly as p
26a23 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20  art of the.**   
26a24 20 20 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64     foreign key d
26a25 65 66 69 6e 69 74 69 6f 6e 2c 20 61 6e 64 20 74  efinition, and t
26a26 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  he parent table 
26a27 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 0a  does not have a.
26a28 2a 2a 20 20 20 20 20 20 50 52 49 4d 41 52 59 20  **      PRIMARY 
26a29 4b 45 59 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20  KEY, or.**.**   
26a2a 34 29 20 4e 6f 20 70 61 72 65 6e 74 20 6b 65 79  4) No parent key
26a2b 20 63 6f 6c 75 6d 6e 73 20 77 65 72 65 20 70 72   columns were pr
26a2c 6f 76 69 64 65 64 20 65 78 70 6c 69 63 69 74 6c  ovided explicitl
26a2d 79 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  y as part of the
26a2e 0a 2a 2a 20 20 20 20 20 20 66 6f 72 65 69 67 6e  .**      foreign
26a2f 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2c   key definition,
26a30 20 61 6e 64 20 74 68 65 20 50 52 49 4d 41 52 59   and the PRIMARY
26a31 20 4b 45 59 20 6f 66 20 74 68 65 20 70 61 72 65   KEY of the pare
26a32 6e 74 20 74 61 62 6c 65 20 0a 2a 2a 20 20 20 20  nt table .**    
26a33 20 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20    consists of a 
26a34 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62  a different numb
26a35 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f  er of columns to
26a36 20 74 68 65 20 63 68 69 6c 64 20 6b 65 79 20 69   the child key i
26a37 6e 20 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 63  n .**      the c
26a38 68 69 6c 64 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  hild table..**.*
26a39 2a 20 74 68 65 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  * then non-zero 
26a3a 69 73 20 72 65 74 75 72 6e 65 64 2c 20 61 6e 64  is returned, and
26a3b 20 61 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20   a "foreign key 
26a3c 6d 69 73 6d 61 74 63 68 22 20 65 72 72 6f 72 20  mismatch" error 
26a3d 6c 6f 61 64 65 64 0a 2a 2a 20 69 6e 74 6f 20 70  loaded.** into p
26a3e 50 61 72 73 65 2e 20 49 66 20 61 6e 20 4f 4f 4d  Parse. If an OOM
26a3f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6e   error occurs, n
26a40 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 65 74 75 72  on-zero is retur
26a41 6e 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 70  ned and the.** p
26a42 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
26a43 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 73 20  cFailed flag is 
26a44 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  set..*/.static i
26a45 6e 74 20 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64  nt locateFkeyInd
26a46 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ex(.  Parse *pPa
26a47 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
26a48 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63        /* Parse c
26a49 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20  ontext to store 
26a4a 61 6e 79 20 65 72 72 6f 72 20 69 6e 20 2a 2f 0a  any error in */.
26a4b 20 20 54 61 62 6c 65 20 2a 70 50 61 72 65 6e 74    Table *pParent
26a4c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26a4d 20 20 2f 2a 20 50 61 72 65 6e 74 20 74 61 62 6c    /* Parent tabl
26a4e 65 20 6f 66 20 46 4b 20 63 6f 6e 73 74 72 61 69  e of FK constrai
26a4f 6e 74 20 70 46 4b 65 79 20 2a 2f 0a 20 20 46 4b  nt pFKey */.  FK
26a50 65 79 20 2a 70 46 4b 65 79 2c 20 20 20 20 20 20  ey *pFKey,      
26a51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26a52 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f 20   Foreign key to 
26a53 66 69 6e 64 20 69 6e 64 65 78 20 66 6f 72 20 2a  find index for *
26a54 2f 0a 20 20 49 6e 64 65 78 20 2a 2a 70 70 49 64  /.  Index **ppId
26a55 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
26a56 20 20 20 20 2f 2a 20 4f 55 54 3a 20 55 6e 69 71      /* OUT: Uniq
26a57 75 65 20 69 6e 64 65 78 20 6f 6e 20 70 61 72 65  ue index on pare
26a58 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  nt table */.  in
26a59 74 20 2a 2a 70 61 69 43 6f 6c 20 20 20 20 20 20  t **paiCol      
26a5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26a5b 20 4f 55 54 3a 20 4d 61 70 20 6f 66 20 69 6e 64   OUT: Map of ind
26a5c 65 78 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 46  ex columns in pF
26a5d 4b 65 79 20 2a 2f 0a 29 7b 0a 20 20 49 6e 64 65  Key */.){.  Inde
26a5e 78 20 2a 70 49 64 78 20 3d 20 30 3b 20 20 20 20  x *pIdx = 0;    
26a5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a60 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75  /* Value to retu
26a61 72 6e 20 76 69 61 20 2a 70 70 49 64 78 20 2a 2f  rn via *ppIdx */
26a62 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c 20 3d 20  .  int *aiCol = 
26a63 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
26a64 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
26a65 74 6f 20 72 65 74 75 72 6e 20 76 69 61 20 2a 70  to return via *p
26a66 61 69 43 6f 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e  aiCol */.  int n
26a67 43 6f 6c 20 3d 20 70 46 4b 65 79 2d 3e 6e 43 6f  Col = pFKey->nCo
26a68 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
26a69 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
26a6a 6d 6e 73 20 69 6e 20 70 61 72 65 6e 74 20 6b 65  mns in parent ke
26a6b 79 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4b 65  y */.  char *zKe
26a6c 79 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b  y = pFKey->aCol[
26a6d 30 5d 2e 7a 43 6f 6c 3b 20 20 20 2f 2a 20 4e 61  0].zCol;   /* Na
26a6e 6d 65 20 6f 66 20 6c 65 66 74 2d 6d 6f 73 74 20  me of left-most 
26a6f 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d  parent key colum
26a70 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 63  n */..  /* The c
26a71 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73  aller is respons
26a72 69 62 6c 65 20 66 6f 72 20 7a 65 72 6f 69 6e 67  ible for zeroing
26a73 20 6f 75 74 70 75 74 20 70 61 72 61 6d 65 74 65   output paramete
26a74 72 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  rs. */.  assert(
26a75 20 70 70 49 64 78 20 26 26 20 2a 70 70 49 64 78   ppIdx && *ppIdx
26a76 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
26a77 20 21 70 61 69 43 6f 6c 20 7c 7c 20 2a 70 61 69   !paiCol || *pai
26a78 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Col==0 );.  asse
26a79 72 74 28 20 70 50 61 72 73 65 20 29 3b 0a 0a 20  rt( pParse );.. 
26a7a 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
26a7b 20 6e 6f 6e 2d 63 6f 6d 70 6f 73 69 74 65 20 28   non-composite (
26a7c 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 29 20 66  single column) f
26a7d 6f 72 65 69 67 6e 20 6b 65 79 2c 20 63 68 65 63  oreign key, chec
26a7e 6b 20 69 66 20 69 74 20 0a 20 20 2a 2a 20 6d 61  k if it .  ** ma
26a7f 70 73 20 74 6f 20 74 68 65 20 49 4e 54 45 47 45  ps to the INTEGE
26a80 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 66  R PRIMARY KEY of
26a81 20 74 61 62 6c 65 20 70 50 61 72 65 6e 74 2e 20   table pParent. 
26a82 49 66 20 73 6f 2c 20 6c 65 61 76 65 20 2a 70 70  If so, leave *pp
26a83 49 64 78 20 0a 20 20 2a 2a 20 61 6e 64 20 2a 70  Idx .  ** and *p
26a84 61 69 43 6f 6c 20 73 65 74 20 74 6f 20 7a 65 72  aiCol set to zer
26a85 6f 20 61 6e 64 20 72 65 74 75 72 6e 20 65 61 72  o and return ear
26a86 6c 79 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f  ly. .  **.  ** O
26a87 74 68 65 72 77 69 73 65 2c 20 66 6f 72 20 61 20  therwise, for a 
26a88 63 6f 6d 70 6f 73 69 74 65 20 66 6f 72 65 69 67  composite foreig
26a89 6e 20 6b 65 79 20 28 6d 6f 72 65 20 74 68 61 6e  n key (more than
26a8a 20 6f 6e 65 20 63 6f 6c 75 6d 6e 29 2c 20 61 6c   one column), al
26a8b 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 73 70 61 63  locate.  ** spac
26a8c 65 20 66 6f 72 20 74 68 65 20 61 69 43 6f 6c 20  e for the aiCol 
26a8d 61 72 72 61 79 20 28 72 65 74 75 72 6e 65 64 20  array (returned 
26a8e 76 69 61 20 6f 75 74 70 75 74 20 70 61 72 61 6d  via output param
26a8f 65 74 65 72 20 2a 70 61 69 43 6f 6c 29 2e 0a 20  eter *paiCol).. 
26a90 20 2a 2a 20 4e 6f 6e 2d 63 6f 6d 70 6f 73 69 74   ** Non-composit
26a91 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 64  e foreign keys d
26a92 6f 20 6e 6f 74 20 72 65 71 75 69 72 65 20 74 68  o not require th
26a93 65 20 61 69 43 6f 6c 20 61 72 72 61 79 2e 0a 20  e aiCol array.. 
26a94 20 2a 2f 0a 20 20 69 66 28 20 6e 43 6f 6c 3d 3d   */.  if( nCol==
26a95 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  1 ){.    /* The 
26a96 46 4b 20 6d 61 70 73 20 74 6f 20 74 68 65 20 49  FK maps to the I
26a97 50 4b 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65  PK if any of the
26a98 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74   following are t
26a99 72 75 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  rue:.    **.    
26a9a 2a 2a 20 20 20 31 29 20 54 68 65 72 65 20 69 73  **   1) There is
26a9b 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   an INTEGER PRIM
26a9c 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 61  ARY KEY column a
26a9d 6e 64 20 74 68 65 20 46 4b 20 69 73 20 69 6d 70  nd the FK is imp
26a9e 6c 69 63 69 74 6c 79 20 0a 20 20 20 20 2a 2a 20  licitly .    ** 
26a9f 20 20 20 20 20 6d 61 70 70 65 64 20 74 6f 20 74       mapped to t
26aa0 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f  he primary key o
26aa1 66 20 74 61 62 6c 65 20 70 50 61 72 65 6e 74 2c  f table pParent,
26aa2 20 6f 72 0a 20 20 20 20 2a 2a 20 20 20 32 29 20   or.    **   2) 
26aa3 54 68 65 20 46 4b 20 69 73 20 65 78 70 6c 69 63  The FK is explic
26aa4 69 74 6c 79 20 6d 61 70 70 65 64 20 74 6f 20 61  itly mapped to a
26aa5 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64   column declared
26aa6 20 61 73 20 49 4e 54 45 47 45 52 0a 20 20 20 20   as INTEGER.    
26aa7 2a 2a 20 20 20 20 20 20 50 52 49 4d 41 52 59 20  **      PRIMARY 
26aa8 4b 45 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  KEY..    */.    
26aa9 69 66 28 20 70 50 61 72 65 6e 74 2d 3e 69 50 4b  if( pParent->iPK
26aaa 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  ey>=0 ){.      i
26aab 66 28 20 21 7a 4b 65 79 20 29 20 72 65 74 75 72  f( !zKey ) retur
26aac 6e 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 21  n 0;.      if( !
26aad 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
26aae 50 61 72 65 6e 74 2d 3e 61 43 6f 6c 5b 70 50 61  Parent->aCol[pPa
26aaf 72 65 6e 74 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61  rent->iPKey].zNa
26ab0 6d 65 2c 20 7a 4b 65 79 29 20 29 20 72 65 74 75  me, zKey) ) retu
26ab1 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  rn 0;.    }.  }e
26ab2 6c 73 65 20 69 66 28 20 70 61 69 43 6f 6c 20 29  lse if( paiCol )
26ab3 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 43  {.    assert( nC
26ab4 6f 6c 3e 31 20 29 3b 0a 20 20 20 20 61 69 43 6f  ol>1 );.    aiCo
26ab5 6c 20 3d 20 28 69 6e 74 20 2a 29 73 71 6c 69 74  l = (int *)sqlit
26ab6 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 50  e3DbMallocRaw(pP
26ab7 61 72 73 65 2d 3e 64 62 2c 20 6e 43 6f 6c 2a 73  arse->db, nCol*s
26ab8 69 7a 65 6f 66 28 69 6e 74 29 29 3b 0a 20 20 20  izeof(int));.   
26ab9 20 69 66 28 20 21 61 69 43 6f 6c 20 29 20 72 65   if( !aiCol ) re
26aba 74 75 72 6e 20 31 3b 0a 20 20 20 20 2a 70 61 69  turn 1;.    *pai
26abb 43 6f 6c 20 3d 20 61 69 43 6f 6c 3b 0a 20 20 7d  Col = aiCol;.  }
26abc 0a 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 50 61  ..  for(pIdx=pPa
26abd 72 65 6e 74 2d 3e 70 49 6e 64 65 78 3b 20 70 49  rent->pIndex; pI
26abe 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
26abf 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70  Next){.    if( p
26ac0 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 6e 43  Idx->nColumn==nC
26ac1 6f 6c 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72  ol && pIdx->onEr
26ac2 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 20  ror!=OE_None ){ 
26ac3 0a 20 20 20 20 20 20 2f 2a 20 70 49 64 78 20 69  .      /* pIdx i
26ac4 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  s a UNIQUE index
26ac5 20 28 6f 72 20 61 20 50 52 49 4d 41 52 59 20 4b   (or a PRIMARY K
26ac6 45 59 29 20 61 6e 64 20 68 61 73 20 74 68 65 20  EY) and has the 
26ac7 72 69 67 68 74 20 6e 75 6d 62 65 72 0a 20 20 20  right number.   
26ac8 20 20 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73     ** of columns
26ac9 2e 20 49 66 20 65 61 63 68 20 69 6e 64 65 78 65  . If each indexe
26aca 64 20 63 6f 6c 75 6d 6e 20 63 6f 72 72 65 73 70  d column corresp
26acb 6f 6e 64 73 20 74 6f 20 61 20 66 6f 72 65 69 67  onds to a foreig
26acc 6e 20 6b 65 79 0a 20 20 20 20 20 20 2a 2a 20 63  n key.      ** c
26acd 6f 6c 75 6d 6e 20 6f 66 20 70 46 4b 65 79 2c 20  olumn of pFKey, 
26ace 74 68 65 6e 20 74 68 69 73 20 69 6e 64 65 78 20  then this index 
26acf 69 73 20 61 20 77 69 6e 6e 65 72 2e 20 20 2a 2f  is a winner.  */
26ad0 0a 0a 20 20 20 20 20 20 69 66 28 20 7a 4b 65 79  ..      if( zKey
26ad1 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
26ad2 2a 20 49 66 20 7a 4b 65 79 20 69 73 20 4e 55 4c  * If zKey is NUL
26ad3 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 66 6f 72  L, then this for
26ad4 65 69 67 6e 20 6b 65 79 20 69 73 20 69 6d 70 6c  eign key is impl
26ad5 69 63 69 74 6c 79 20 6d 61 70 70 65 64 20 74 6f  icitly mapped to
26ad6 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65   .        ** the
26ad7 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 66 20   PRIMARY KEY of 
26ad8 74 61 62 6c 65 20 70 50 61 72 65 6e 74 2e 20 54  table pParent. T
26ad9 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69  he PRIMARY KEY i
26ada 6e 64 65 78 20 6d 61 79 20 62 65 20 0a 20 20 20  ndex may be .   
26adb 20 20 20 20 20 2a 2a 20 69 64 65 6e 74 69 66 69       ** identifi
26adc 65 64 20 62 79 20 74 68 65 20 74 65 73 74 20 28  ed by the test (
26add 49 6e 64 65 78 2e 61 75 74 6f 49 6e 64 65 78 3d  Index.autoIndex=
26ade 3d 32 29 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  =2).  */.       
26adf 20 69 66 28 20 70 49 64 78 2d 3e 61 75 74 6f 49   if( pIdx->autoI
26ae0 6e 64 65 78 3d 3d 32 20 29 7b 0a 20 20 20 20 20  ndex==2 ){.     
26ae1 20 20 20 20 20 69 66 28 20 61 69 43 6f 6c 20 29       if( aiCol )
26ae2 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
26ae3 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20  t i;.           
26ae4 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
26ae5 3b 20 69 2b 2b 29 20 61 69 43 6f 6c 5b 69 5d 20  ; i++) aiCol[i] 
26ae6 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d  = pFKey->aCol[i]
26ae7 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20  .iFrom;.        
26ae8 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72    }.          br
26ae9 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
26aea 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
26aeb 20 20 20 20 2f 2a 20 49 66 20 7a 4b 65 79 20 69      /* If zKey i
26aec 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 6e  s non-NULL, then
26aed 20 74 68 69 73 20 66 6f 72 65 69 67 6e 20 6b 65   this foreign ke
26aee 79 20 77 61 73 20 64 65 63 6c 61 72 65 64 20 74  y was declared t
26aef 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 70  o.        ** map
26af0 20 74 6f 20 61 6e 20 65 78 70 6c 69 63 69 74 20   to an explicit 
26af1 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  list of columns 
26af2 69 6e 20 74 61 62 6c 65 20 70 50 61 72 65 6e 74  in table pParent
26af3 2e 20 43 68 65 63 6b 20 69 66 20 74 68 69 73 0a  . Check if this.
26af4 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 78          ** index
26af5 20 6d 61 74 63 68 65 73 20 74 68 6f 73 65 20 63   matches those c
26af6 6f 6c 75 6d 6e 73 2e 20 41 6c 73 6f 2c 20 63 68  olumns. Also, ch
26af7 65 63 6b 20 74 68 61 74 20 74 68 65 20 69 6e 64  eck that the ind
26af8 65 78 20 75 73 65 73 0a 20 20 20 20 20 20 20 20  ex uses.        
26af9 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 63  ** the default c
26afa 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
26afb 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  es for each colu
26afc 6d 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  mn. */.        i
26afd 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 20 20 20  nt i, j;.       
26afe 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
26aff 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
26b00 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 49 64    int iCol = pId
26b01 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 20  x->aiColumn[i]; 
26b02 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
26b03 63 6f 6c 75 6d 6e 20 69 6e 20 70 61 72 65 6e 74  column in parent
26b04 20 74 62 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20   tbl */.        
26b05 20 20 63 68 61 72 20 2a 7a 44 66 6c 74 43 6f 6c    char *zDfltCol
26b06 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
26b07 20 20 20 20 2f 2a 20 44 65 66 2e 20 63 6f 6c 6c      /* Def. coll
26b08 61 74 69 6f 6e 20 66 6f 72 20 63 6f 6c 75 6d 6e  ation for column
26b09 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 68   */.          ch
26b0a 61 72 20 2a 7a 49 64 78 43 6f 6c 3b 20 20 20 20  ar *zIdxCol;    
26b0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b0c 2f 2a 20 4e 61 6d 65 20 6f 66 20 69 6e 64 65 78  /* Name of index
26b0d 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 0a 20 20  ed column */..  
26b0e 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
26b0f 65 20 69 6e 64 65 78 20 75 73 65 73 20 61 20 63  e index uses a c
26b10 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
26b11 65 20 74 68 61 74 20 69 73 20 64 69 66 66 65 72  e that is differ
26b12 65 6e 74 20 66 72 6f 6d 0a 20 20 20 20 20 20 20  ent from.       
26b13 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c     ** the defaul
26b14 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  t collation sequ
26b15 65 6e 63 65 20 66 6f 72 20 74 68 65 20 63 6f 6c  ence for the col
26b16 75 6d 6e 2c 20 74 68 69 73 20 69 6e 64 65 78 20  umn, this index 
26b17 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  is.          ** 
26b18 75 6e 75 73 61 62 6c 65 2e 20 42 61 69 6c 20 6f  unusable. Bail o
26b19 75 74 20 65 61 72 6c 79 20 69 6e 20 74 68 69 73  ut early in this
26b1a 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20   case.  */.     
26b1b 20 20 20 20 20 7a 44 66 6c 74 43 6f 6c 6c 20 3d       zDfltColl =
26b1c 20 70 50 61 72 65 6e 74 2d 3e 61 43 6f 6c 5b 69   pParent->aCol[i
26b1d 43 6f 6c 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20  Col].zColl;.    
26b1e 20 20 20 20 20 20 69 66 28 20 21 7a 44 66 6c 74        if( !zDflt
26b1f 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Coll ){.        
26b20 20 20 20 20 7a 44 66 6c 74 43 6f 6c 6c 20 3d 20      zDfltColl = 
26b21 22 42 49 4e 41 52 59 22 3b 0a 20 20 20 20 20 20  "BINARY";.      
26b22 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
26b23 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
26b24 6d 70 28 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b  mp(pIdx->azColl[
26b25 69 5d 2c 20 7a 44 66 6c 74 43 6f 6c 6c 29 20 29  i], zDfltColl) )
26b26 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 20   break;..       
26b27 20 20 20 7a 49 64 78 43 6f 6c 20 3d 20 70 50 61     zIdxCol = pPa
26b28 72 65 6e 74 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  rent->aCol[iCol]
26b29 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
26b2a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f    for(j=0; j<nCo
26b2b 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; j++){.       
26b2c 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
26b2d 53 74 72 49 43 6d 70 28 70 46 4b 65 79 2d 3e 61  StrICmp(pFKey->a
26b2e 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 2c 20 7a 49 64  Col[j].zCol, zId
26b2f 78 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  xCol)==0 ){.    
26b30 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 69            if( ai
26b31 43 6f 6c 20 29 20 61 69 43 6f 6c 5b 69 5d 20 3d  Col ) aiCol[i] =
26b32 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e   pFKey->aCol[j].
26b33 69 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20 20  iFrom;.         
26b34 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
26b35 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
26b36 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
26b37 69 66 28 20 6a 3d 3d 6e 43 6f 6c 20 29 20 62 72  if( j==nCol ) br
26b38 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
26b39 20 20 20 20 20 20 20 69 66 28 20 69 3d 3d 6e 43         if( i==nC
26b3a 6f 6c 20 29 20 62 72 65 61 6b 3b 20 20 20 20 20  ol ) break;     
26b3b 20 2f 2a 20 70 49 64 78 20 69 73 20 75 73 61 62   /* pIdx is usab
26b3c 6c 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  le */.      }.  
26b3d 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21    }.  }..  if( !
26b3e 70 49 64 78 20 29 7b 0a 20 20 20 20 69 66 28 20  pIdx ){.    if( 
26b3f 21 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65  !pParse->disable
26b40 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20 20 20  Triggers ){.    
26b41 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
26b42 67 28 70 50 61 72 73 65 2c 20 22 66 6f 72 65 69  g(pParse, "forei
26b43 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68 22  gn key mismatch"
26b44 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
26b45 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73  ite3DbFree(pPars
26b46 65 2d 3e 64 62 2c 20 61 69 43 6f 6c 29 3b 0a 20  e->db, aiCol);. 
26b47 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
26b48 0a 0a 20 20 2a 70 70 49 64 78 20 3d 20 70 49 64  ..  *ppIdx = pId
26b49 78 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  x;.  return 0;.}
26b4a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
26b4b 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
26b4c 77 68 65 6e 20 61 20 72 6f 77 20 69 73 20 69 6e  when a row is in
26b4d 73 65 72 74 65 64 20 69 6e 74 6f 20 6f 72 20 64  serted into or d
26b4e 65 6c 65 74 65 64 20 66 72 6f 6d 20 74 68 65 20  eleted from the 
26b4f 0a 2a 2a 20 63 68 69 6c 64 20 74 61 62 6c 65 20  .** child table 
26b50 6f 66 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  of foreign key c
26b51 6f 6e 73 74 72 61 69 6e 74 20 70 46 4b 65 79 2e  onstraint pFKey.
26b52 20 49 66 20 61 6e 20 53 51 4c 20 55 50 44 41 54   If an SQL UPDAT
26b53 45 20 69 73 20 65 78 65 63 75 74 65 64 20 0a 2a  E is executed .*
26b54 2a 20 6f 6e 20 74 68 65 20 63 68 69 6c 64 20 74  * on the child t
26b55 61 62 6c 65 20 6f 66 20 70 46 4b 65 79 2c 20 74  able of pFKey, t
26b56 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
26b57 69 6e 76 6f 6b 65 64 20 74 77 69 63 65 20 66 6f  invoked twice fo
26b58 72 20 65 61 63 68 20 72 6f 77 0a 2a 2a 20 61 66  r each row.** af
26b59 66 65 63 74 65 64 20 2d 20 6f 6e 63 65 20 74 6f  fected - once to
26b5a 20 22 64 65 6c 65 74 65 22 20 74 68 65 20 6f 6c   "delete" the ol
26b5b 64 20 72 6f 77 2c 20 61 6e 64 20 74 68 65 6e 20  d row, and then 
26b5c 61 67 61 69 6e 20 74 6f 20 22 69 6e 73 65 72 74  again to "insert
26b5d 22 20 74 68 65 0a 2a 2a 20 6e 65 77 20 72 6f 77  " the.** new row
26b5e 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 74 69 6d  ..**.** Each tim
26b5f 65 20 69 74 20 69 73 20 63 61 6c 6c 65 64 2c 20  e it is called, 
26b60 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 67 65  this function ge
26b61 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64  nerates VDBE cod
26b62 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 0a  e to locate the.
26b63 2a 2a 20 72 6f 77 20 69 6e 20 74 68 65 20 70 61  ** row in the pa
26b64 72 65 6e 74 20 74 61 62 6c 65 20 74 68 61 74 20  rent table that 
26b65 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74  corresponds to t
26b66 68 65 20 72 6f 77 20 62 65 69 6e 67 20 69 6e 73  he row being ins
26b67 65 72 74 65 64 20 69 6e 74 6f 20 0a 2a 2a 20 6f  erted into .** o
26b68 72 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 74  r deleted from t
26b69 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 20  he child table. 
26b6a 49 66 20 74 68 65 20 70 61 72 65 6e 74 20 72 6f  If the parent ro
26b6b 77 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20  w can be found, 
26b6c 6e 6f 20 0a 2a 2a 20 73 70 65 63 69 61 6c 20 61  no .** special a
26b6d 63 74 69 6f 6e 20 69 73 20 74 61 6b 65 6e 2e 20  ction is taken. 
26b6e 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68  Otherwise, if th
26b6f 65 20 70 61 72 65 6e 74 20 72 6f 77 20 63 61 6e  e parent row can
26b70 20 2a 6e 6f 74 2a 20 62 65 0a 2a 2a 20 66 6f 75   *not* be.** fou
26b71 6e 64 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74  nd in the parent
26b72 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20   table:.**.**   
26b73 4f 70 65 72 61 74 69 6f 6e 20 7c 20 46 4b 20 74  Operation | FK t
26b74 79 70 65 20 20 20 7c 20 41 63 74 69 6f 6e 20 74  ype   | Action t
26b75 61 6b 65 6e 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  aken.**   ------
26b76 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b77 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b78 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b79 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b7a 2d 2d 2d 2d 0a 2a 2a 20 20 20 49 4e 53 45 52 54  ----.**   INSERT
26b7b 20 20 20 20 20 20 69 6d 6d 65 64 69 61 74 65 20        immediate 
26b7c 20 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20    Increment the 
26b7d 22 69 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74  "immediate const
26b7e 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a  raint counter"..
26b7f 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45 54 45 20 20  **.**   DELETE  
26b80 20 20 20 20 69 6d 6d 65 64 69 61 74 65 20 20 20      immediate   
26b81 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 22 69  Decrement the "i
26b82 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74 72 61  mmediate constra
26b83 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a  int counter"..**
26b84 0a 2a 2a 20 20 20 49 4e 53 45 52 54 20 20 20 20  .**   INSERT    
26b85 20 20 64 65 66 65 72 72 65 64 20 20 20 20 49 6e    deferred    In
26b86 63 72 65 6d 65 6e 74 20 74 68 65 20 22 64 65 66  crement the "def
26b87 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74  erred constraint
26b88 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a 2a   counter"..**.**
26b89 20 20 20 44 45 4c 45 54 45 20 20 20 20 20 20 64     DELETE      d
26b8a 65 66 65 72 72 65 64 20 20 20 20 44 65 63 72 65  eferred    Decre
26b8b 6d 65 6e 74 20 74 68 65 20 22 64 65 66 65 72 72  ment the "deferr
26b8c 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f  ed constraint co
26b8d 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 54 68  unter"..**.** Th
26b8e 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 61  ese operations a
26b8f 72 65 20 69 64 65 6e 74 69 66 69 65 64 20 69 6e  re identified in
26b90 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 74 20   the comment at 
26b91 74 68 65 20 74 6f 70 20 6f 66 20 74 68 69 73 20  the top of this 
26b92 66 69 6c 65 20 0a 2a 2a 20 28 66 6b 65 79 2e 63  file .** (fkey.c
26b93 29 20 61 73 20 22 49 2e 31 22 20 61 6e 64 20 22  ) as "I.1" and "
26b94 44 2e 31 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  D.1"..*/.static 
26b95 76 6f 69 64 20 66 6b 4c 6f 6f 6b 75 70 50 61 72  void fkLookupPar
26b96 65 6e 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ent(.  Parse *pP
26b97 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
26b98 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  Parse context */
26b99 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20  .  int iDb,     
26b9a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
26b9b 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f  x of database ho
26b9c 75 73 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20  using pTab */.  
26b9d 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
26b9e 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20        /* Parent 
26b9f 74 61 62 6c 65 20 6f 66 20 46 4b 20 70 46 4b 65  table of FK pFKe
26ba0 79 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  y */.  Index *pI
26ba1 64 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dx,          /* 
26ba2 55 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e 20  Unique index on 
26ba3 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d  parent key colum
26ba4 6e 73 20 69 6e 20 70 54 61 62 20 2a 2f 0a 20 20  ns in pTab */.  
26ba5 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 20 20 20  FKey *pFKey,    
26ba6 20 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e        /* Foreign
26ba7 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
26ba8 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c 2c  */.  int *aiCol,
26ba9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
26baa 70 20 66 72 6f 6d 20 70 61 72 65 6e 74 20 6b 65  p from parent ke
26bab 79 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 69  y columns to chi
26bac 6c 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73  ld table columns
26bad 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74   */.  int regDat
26bae 61 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  a,          /* A
26baf 64 64 72 65 73 73 20 6f 66 20 61 72 72 61 79 20  ddress of array 
26bb0 63 6f 6e 74 61 69 6e 69 6e 67 20 63 68 69 6c 64  containing child
26bb1 20 74 61 62 6c 65 20 72 6f 77 20 2a 2f 0a 20 20   table row */.  
26bb2 69 6e 74 20 6e 49 6e 63 72 2c 20 20 20 20 20 20  int nIncr,      
26bb3 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65        /* Increme
26bb4 6e 74 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f  nt constraint co
26bb5 75 6e 74 65 72 20 62 79 20 74 68 69 73 20 2a 2f  unter by this */
26bb6 0a 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65 20  .  int isIgnore 
26bb7 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74           /* If t
26bb8 72 75 65 2c 20 70 72 65 74 65 6e 64 20 70 54 61  rue, pretend pTa
26bb9 62 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 4e  b contains all N
26bba 55 4c 4c 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b  ULL values */.){
26bbb 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
26bbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bbd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26bbe 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c  Iterator variabl
26bbf 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d  e */.  Vdbe *v =
26bc0 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
26bc1 70 50 61 72 73 65 29 3b 20 20 20 20 20 20 20 20  pParse);        
26bc2 20 2f 2a 20 56 64 62 65 20 74 6f 20 61 64 64 20   /* Vdbe to add 
26bc3 63 6f 64 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74  code to */.  int
26bc4 20 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e   iCur = pParse->
26bc5 6e 54 61 62 20 2d 20 31 3b 20 20 20 20 20 20 20  nTab - 1;       
26bc6 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
26bc7 20 6e 75 6d 62 65 72 20 74 6f 20 75 73 65 20 2a   number to use *
26bc8 2f 0a 20 20 69 6e 74 20 69 4f 6b 20 3d 20 73 71  /.  int iOk = sq
26bc9 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
26bca 65 6c 28 76 29 3b 20 20 20 20 20 20 20 20 2f 2a  el(v);        /*
26bcb 20 6a 75 6d 70 20 68 65 72 65 20 69 66 20 70 61   jump here if pa
26bcc 72 65 6e 74 20 6b 65 79 20 66 6f 75 6e 64 20 2a  rent key found *
26bcd 2f 0a 0a 20 20 2f 2a 20 49 66 20 6e 49 6e 63 72  /..  /* If nIncr
26bce 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65   is less than ze
26bcf 72 6f 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 61  ro, then check a
26bd0 74 20 72 75 6e 74 69 6d 65 20 69 66 20 74 68 65  t runtime if the
26bd1 72 65 20 61 72 65 20 61 6e 79 0a 20 20 2a 2a 20  re are any.  ** 
26bd2 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 6f 6e 73  outstanding cons
26bd3 74 72 61 69 6e 74 73 20 74 6f 20 72 65 73 6f 6c  traints to resol
26bd4 76 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65  ve. If there are
26bd5 20 6e 6f 74 2c 20 74 68 65 72 65 20 69 73 20 6e   not, there is n
26bd6 6f 20 6e 65 65 64 0a 20 20 2a 2a 20 74 6f 20 63  o need.  ** to c
26bd7 68 65 63 6b 20 69 66 20 64 65 6c 65 74 69 6e 67  heck if deleting
26bd8 20 74 68 69 73 20 72 6f 77 20 72 65 73 6f 6c 76   this row resolv
26bd9 65 73 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69  es any outstandi
26bda 6e 67 20 76 69 6f 6c 61 74 69 6f 6e 73 2e 0a 20  ng violations.. 
26bdb 20 2a 2a 0a 20 20 2a 2a 20 43 68 65 63 6b 20 69   **.  ** Check i
26bdc 66 20 61 6e 79 20 6f 66 20 74 68 65 20 6b 65 79  f any of the key
26bdd 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
26bde 63 68 69 6c 64 20 74 61 62 6c 65 20 72 6f 77 20  child table row 
26bdf 61 72 65 20 4e 55 4c 4c 2e 20 49 66 20 0a 20 20  are NULL. If .  
26be0 2a 2a 20 61 6e 79 20 61 72 65 2c 20 74 68 65 6e  ** any are, then
26be1 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
26be2 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 73 61  is considered sa
26be3 74 69 73 66 69 65 64 2e 20 4e 6f 20 6e 65 65 64  tisfied. No need
26be4 20 74 6f 20 0a 20 20 2a 2a 20 73 65 61 72 63 68   to .  ** search
26be5 20 66 6f 72 20 61 20 6d 61 74 63 68 69 6e 67 20   for a matching 
26be6 72 6f 77 20 69 6e 20 74 68 65 20 70 61 72 65 6e  row in the paren
26be7 74 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69  t table.  */.  i
26be8 66 28 20 6e 49 6e 63 72 3c 30 20 29 7b 0a 20 20  f( nIncr<0 ){.  
26be9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26bea 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 49 66 5a 65  Op2(v, OP_FkIfZe
26beb 72 6f 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65 66  ro, pFKey->isDef
26bec 65 72 72 65 64 2c 20 69 4f 6b 29 3b 0a 20 20 7d  erred, iOk);.  }
26bed 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46  .  for(i=0; i<pF
26bee 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Key->nCol; i++){
26bef 0a 20 20 20 20 69 6e 74 20 69 52 65 67 20 3d 20  .    int iReg = 
26bf0 61 69 43 6f 6c 5b 69 5d 20 2b 20 72 65 67 44 61  aiCol[i] + regDa
26bf1 74 61 20 2b 20 31 3b 0a 20 20 20 20 73 71 6c 69  ta + 1;.    sqli
26bf2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26bf3 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 69 52 65 67   OP_IsNull, iReg
26bf4 2c 20 69 4f 6b 29 3b 0a 20 20 7d 0a 0a 20 20 69  , iOk);.  }..  i
26bf5 66 28 20 69 73 49 67 6e 6f 72 65 3d 3d 30 20 29  f( isIgnore==0 )
26bf6 7b 0a 20 20 20 20 69 66 28 20 70 49 64 78 3d 3d  {.    if( pIdx==
26bf7 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  0 ){.      /* If
26bf8 20 70 49 64 78 20 69 73 20 4e 55 4c 4c 2c 20 74   pIdx is NULL, t
26bf9 68 65 6e 20 74 68 65 20 70 61 72 65 6e 74 20 6b  hen the parent k
26bfa 65 79 20 69 73 20 74 68 65 20 49 4e 54 45 47 45  ey is the INTEGE
26bfb 52 20 50 52 49 4d 41 52 59 20 4b 45 59 0a 20 20  R PRIMARY KEY.  
26bfc 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66      ** column of
26bfd 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
26bfe 65 20 28 74 61 62 6c 65 20 70 54 61 62 29 2e 20  e (table pTab). 
26bff 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 4d   */.      int iM
26c00 75 73 74 42 65 49 6e 74 3b 20 20 20 20 20 20 20  ustBeInt;       
26c01 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
26c02 73 73 20 6f 66 20 4d 75 73 74 42 65 49 6e 74 20  ss of MustBeInt 
26c03 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20  instruction */. 
26c04 20 20 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70       int regTemp
26c05 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
26c06 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
26c07 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65  .      /* Invoke
26c08 20 4d 75 73 74 42 65 49 6e 74 20 74 6f 20 63 6f   MustBeInt to co
26c09 65 72 63 65 20 74 68 65 20 63 68 69 6c 64 20 6b  erce the child k
26c0a 65 79 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 69  ey value to an i
26c0b 6e 74 65 67 65 72 20 28 69 2e 65 2e 20 0a 20 20  nteger (i.e. .  
26c0c 20 20 20 20 2a 2a 20 61 70 70 6c 79 20 74 68 65      ** apply the
26c0d 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
26c0e 20 70 61 72 65 6e 74 20 6b 65 79 29 2e 20 49 66   parent key). If
26c0f 20 74 68 69 73 20 66 61 69 6c 73 2c 20 74 68 65   this fails, the
26c10 6e 20 74 68 65 72 65 0a 20 20 20 20 20 20 2a 2a  n there.      **
26c11 20 69 73 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20   is no matching 
26c12 70 61 72 65 6e 74 20 6b 65 79 2e 20 42 65 66 6f  parent key. Befo
26c13 72 65 20 75 73 69 6e 67 20 4d 75 73 74 42 65 49  re using MustBeI
26c14 6e 74 2c 20 6d 61 6b 65 20 61 20 63 6f 70 79 20  nt, make a copy 
26c15 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  of.      ** the 
26c16 76 61 6c 75 65 2e 20 4f 74 68 65 72 77 69 73 65  value. Otherwise
26c17 2c 20 74 68 65 20 76 61 6c 75 65 20 69 6e 73 65  , the value inse
26c18 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 63 68  rted into the ch
26c19 69 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 0a 20  ild key column. 
26c1a 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 68 61 76       ** will hav
26c1b 65 20 49 4e 54 45 47 45 52 20 61 66 66 69 6e 69  e INTEGER affini
26c1c 74 79 20 61 70 70 6c 69 65 64 20 74 6f 20 69 74  ty applied to it
26c1d 2c 20 77 68 69 63 68 20 6d 61 79 20 6e 6f 74 20  , which may not 
26c1e 62 65 20 63 6f 72 72 65 63 74 2e 20 20 2a 2f 0a  be correct.  */.
26c1f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26c20 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
26c21 6f 70 79 2c 20 61 69 43 6f 6c 5b 30 5d 2b 31 2b  opy, aiCol[0]+1+
26c22 72 65 67 44 61 74 61 2c 20 72 65 67 54 65 6d 70  regData, regTemp
26c23 29 3b 0a 20 20 20 20 20 20 69 4d 75 73 74 42 65  );.      iMustBe
26c24 49 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Int = sqlite3Vdb
26c25 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75  eAddOp2(v, OP_Mu
26c26 73 74 42 65 49 6e 74 2c 20 72 65 67 54 65 6d 70  stBeInt, regTemp
26c27 2c 20 30 29 3b 0a 20 20 0a 20 20 20 20 20 20 2f  , 0);.  .      /
26c28 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20  * If the parent 
26c29 74 61 62 6c 65 20 69 73 20 74 68 65 20 73 61 6d  table is the sam
26c2a 65 20 61 73 20 74 68 65 20 63 68 69 6c 64 20 74  e as the child t
26c2b 61 62 6c 65 2c 20 61 6e 64 20 77 65 20 61 72 65  able, and we are
26c2c 20 61 62 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20   about.      ** 
26c2d 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  to increment the
26c2e 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e   constraint-coun
26c2f 74 65 72 20 28 69 2e 65 2e 20 74 68 69 73 20 69  ter (i.e. this i
26c30 73 20 61 6e 20 49 4e 53 45 52 54 20 6f 70 65 72  s an INSERT oper
26c31 61 74 69 6f 6e 29 2c 0a 20 20 20 20 20 20 2a 2a  ation),.      **
26c32 20 74 68 65 6e 20 63 68 65 63 6b 20 69 66 20 74   then check if t
26c33 68 65 20 72 6f 77 20 62 65 69 6e 67 20 69 6e 73  he row being ins
26c34 65 72 74 65 64 20 6d 61 74 63 68 65 73 20 69 74  erted matches it
26c35 73 65 6c 66 2e 20 49 66 20 73 6f 2c 20 64 6f 20  self. If so, do 
26c36 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63  not.      ** inc
26c37 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 6e 73 74  rement the const
26c38 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 2e 20 20  raint-counter.  
26c39 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  */.      if( pTa
26c3a 62 3d 3d 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 20  b==pFKey->pFrom 
26c3b 26 26 20 6e 49 6e 63 72 3d 3d 31 20 29 7b 0a 20  && nIncr==1 ){. 
26c3c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26c3d 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 45  beAddOp3(v, OP_E
26c3e 71 2c 20 72 65 67 44 61 74 61 2c 20 69 4f 6b 2c  q, regData, iOk,
26c3f 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20   regTemp);.     
26c40 20 7d 0a 20 20 0a 20 20 20 20 20 20 73 71 6c 69   }.  .      sqli
26c41 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
26c42 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c 20  rse, iCur, iDb, 
26c43 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  pTab, OP_OpenRea
26c44 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  d);.      sqlite
26c45 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
26c46 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75  P_NotExists, iCu
26c47 72 2c 20 30 2c 20 72 65 67 54 65 6d 70 29 3b 0a  r, 0, regTemp);.
26c48 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26c49 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
26c4a 74 6f 2c 20 30 2c 20 69 4f 6b 29 3b 0a 20 20 20  to, 0, iOk);.   
26c4b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
26c4c 6d 70 48 65 72 65 28 76 2c 20 73 71 6c 69 74 65  mpHere(v, sqlite
26c4d 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
26c4e 28 76 29 2d 32 29 3b 0a 20 20 20 20 20 20 73 71  (v)-2);.      sq
26c4f 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
26c50 65 28 76 2c 20 69 4d 75 73 74 42 65 49 6e 74 29  e(v, iMustBeInt)
26c51 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
26c52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
26c53 61 72 73 65 2c 20 72 65 67 54 65 6d 70 29 3b 0a  arse, regTemp);.
26c54 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
26c55 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70 46 4b 65   int nCol = pFKe
26c56 79 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 69  y->nCol;.      i
26c57 6e 74 20 72 65 67 54 65 6d 70 20 3d 20 73 71 6c  nt regTemp = sql
26c58 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
26c59 28 70 50 61 72 73 65 2c 20 6e 43 6f 6c 29 3b 0a  (pParse, nCol);.
26c5a 20 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 63        int regRec
26c5b 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
26c5c 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
26c5d 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
26c5e 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
26c5f 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
26c60 70 49 64 78 29 3b 0a 20 20 0a 20 20 20 20 20 20  pIdx);.  .      
26c61 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26c62 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  3(v, OP_OpenRead
26c63 2c 20 69 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e  , iCur, pIdx->tn
26c64 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  um, iDb);.      
26c65 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
26c66 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72  eP4(v, -1, (char
26c67 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e  *)pKey, P4_KEYIN
26c68 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
26c69 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
26c6a 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
26c6b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26c6c 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
26c6d 20 61 69 43 6f 6c 5b 69 5d 2b 31 2b 72 65 67 44   aiCol[i]+1+regD
26c6e 61 74 61 2c 20 72 65 67 54 65 6d 70 2b 69 29 3b  ata, regTemp+i);
26c6f 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  .      }.  .    
26c70 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 72 65    /* If the pare
26c71 6e 74 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  nt table is the 
26c72 73 61 6d 65 20 61 73 20 74 68 65 20 63 68 69 6c  same as the chil
26c73 64 20 74 61 62 6c 65 2c 20 61 6e 64 20 77 65 20  d table, and we 
26c74 61 72 65 20 61 62 6f 75 74 0a 20 20 20 20 20 20  are about.      
26c75 2a 2a 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20  ** to increment 
26c76 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63  the constraint-c
26c77 6f 75 6e 74 65 72 20 28 69 2e 65 2e 20 74 68 69  ounter (i.e. thi
26c78 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 6f  s is an INSERT o
26c79 70 65 72 61 74 69 6f 6e 29 2c 0a 20 20 20 20 20  peration),.     
26c7a 20 2a 2a 20 74 68 65 6e 20 63 68 65 63 6b 20 69   ** then check i
26c7b 66 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20  f the row being 
26c7c 69 6e 73 65 72 74 65 64 20 6d 61 74 63 68 65 73  inserted matches
26c7d 20 69 74 73 65 6c 66 2e 20 49 66 20 73 6f 2c 20   itself. If so, 
26c7e 64 6f 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20  do not.      ** 
26c7f 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f  increment the co
26c80 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72  nstraint-counter
26c81 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  .  */.      if( 
26c82 70 54 61 62 3d 3d 70 46 4b 65 79 2d 3e 70 46 72  pTab==pFKey->pFr
26c83 6f 6d 20 26 26 20 6e 49 6e 63 72 3d 3d 31 20 29  om && nIncr==1 )
26c84 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 4a  {.        int iJ
26c85 75 6d 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ump = sqlite3Vdb
26c86 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 20  eCurrentAddr(v) 
26c87 2b 20 6e 43 6f 6c 20 2b 20 31 3b 0a 20 20 20 20  + nCol + 1;.    
26c88 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
26c89 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
26c8a 20 20 20 20 20 69 6e 74 20 69 43 68 69 6c 64 20       int iChild 
26c8b 3d 20 61 69 43 6f 6c 5b 69 5d 2b 31 2b 72 65 67  = aiCol[i]+1+reg
26c8c 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 20 20  Data;.          
26c8d 69 6e 74 20 69 50 61 72 65 6e 74 20 3d 20 70 49  int iParent = pI
26c8e 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 2b  dx->aiColumn[i]+
26c8f 31 2b 72 65 67 44 61 74 61 3b 0a 20 20 20 20 20  1+regData;.     
26c90 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26c91 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c  AddOp3(v, OP_Ne,
26c92 20 69 43 68 69 6c 64 2c 20 69 4a 75 6d 70 2c 20   iChild, iJump, 
26c93 69 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20  iParent);.      
26c94 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
26c95 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26c96 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 4f 6b   OP_Goto, 0, iOk
26c97 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20  );.      }.  .  
26c98 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26c99 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
26c9a 52 65 63 6f 72 64 2c 20 72 65 67 54 65 6d 70 2c  Record, regTemp,
26c9b 20 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0a   nCol, regRec);.
26c9c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26c9d 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
26c9e 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66   sqlite3IndexAff
26c9f 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49 64 78  inityStr(v, pIdx
26ca0 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  ), 0);.      sql
26ca1 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
26ca2 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 69 43 75 72  , OP_Found, iCur
26ca3 2c 20 69 4f 6b 2c 20 72 65 67 52 65 63 29 3b 0a  , iOk, regRec);.
26ca4 20 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33    .      sqlite3
26ca5 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
26ca6 50 61 72 73 65 2c 20 72 65 67 52 65 63 29 3b 0a  Parse, regRec);.
26ca7 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
26ca8 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
26ca9 61 72 73 65 2c 20 72 65 67 54 65 6d 70 2c 20 6e  arse, regTemp, n
26caa 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Col);.    }.  }.
26cab 0a 20 20 69 66 28 20 21 70 46 4b 65 79 2d 3e 69  .  if( !pFKey->i
26cac 73 44 65 66 65 72 72 65 64 20 26 26 20 21 70 50  sDeferred && !pP
26cad 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20  arse->pToplevel 
26cae 26 26 20 21 70 50 61 72 73 65 2d 3e 69 73 4d 75  && !pParse->isMu
26caf 6c 74 69 57 72 69 74 65 20 29 7b 0a 20 20 20 20  ltiWrite ){.    
26cb0 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a  /* Special case:
26cb1 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49   If this is an I
26cb2 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 20  NSERT statement 
26cb3 74 68 61 74 20 77 69 6c 6c 20 69 6e 73 65 72 74  that will insert
26cb4 20 65 78 61 63 74 6c 79 0a 20 20 20 20 2a 2a 20   exactly.    ** 
26cb5 6f 6e 65 20 72 6f 77 20 69 6e 74 6f 20 74 68 65  one row into the
26cb6 20 74 61 62 6c 65 2c 20 72 61 69 73 65 20 61 20   table, raise a 
26cb7 63 6f 6e 73 74 72 61 69 6e 74 20 69 6d 6d 65 64  constraint immed
26cb8 69 61 74 65 6c 79 20 69 6e 73 74 65 61 64 20 6f  iately instead o
26cb9 66 0a 20 20 20 20 2a 2a 20 69 6e 63 72 65 6d 65  f.    ** increme
26cba 6e 74 69 6e 67 20 61 20 63 6f 75 6e 74 65 72 2e  nting a counter.
26cbb 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61   This is necessa
26cbc 72 79 20 61 73 20 74 68 65 20 56 4d 20 63 6f 64  ry as the VM cod
26cbd 65 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 2a  e is being.    *
26cbe 2a 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20  * generated for 
26cbf 77 69 6c 6c 20 6e 6f 74 20 6f 70 65 6e 20 61 20  will not open a 
26cc0 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
26cc1 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61  ction.  */.    a
26cc2 73 73 65 72 74 28 20 6e 49 6e 63 72 3d 3d 31 20  ssert( nIncr==1 
26cc3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61  );.    sqlite3Ha
26cc4 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20  ltConstraint(.  
26cc5 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 4f 45        pParse, OE
26cc6 5f 41 62 6f 72 74 2c 20 22 66 6f 72 65 69 67 6e  _Abort, "foreign
26cc7 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
26cc8 66 61 69 6c 65 64 22 2c 20 50 34 5f 53 54 41 54  failed", P4_STAT
26cc9 49 43 0a 20 20 20 20 29 3b 0a 20 20 7d 65 6c 73  IC.    );.  }els
26cca 65 7b 0a 20 20 20 20 69 66 28 20 6e 49 6e 63 72  e{.    if( nIncr
26ccb 3e 30 20 26 26 20 70 46 4b 65 79 2d 3e 69 73 44  >0 && pFKey->isD
26ccc 65 66 65 72 72 65 64 3d 3d 30 20 29 7b 0a 20 20  eferred==0 ){.  
26ccd 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
26cce 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29  Toplevel(pParse)
26ccf 2d 3e 6d 61 79 41 62 6f 72 74 20 3d 20 31 3b 0a  ->mayAbort = 1;.
26cd0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
26cd1 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26cd2 50 5f 46 6b 43 6f 75 6e 74 65 72 2c 20 70 46 4b  P_FkCounter, pFK
26cd3 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 2c 20  ey->isDeferred, 
26cd4 6e 49 6e 63 72 29 3b 0a 20 20 7d 0a 0a 20 20 73  nIncr);.  }..  s
26cd5 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
26cd6 65 4c 61 62 65 6c 28 76 2c 20 69 4f 6b 29 3b 0a  eLabel(v, iOk);.
26cd7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26cd8 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op1(v, OP_Close,
26cd9 20 69 43 75 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   iCur);.}../*.**
26cda 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
26cdb 73 20 63 61 6c 6c 65 64 20 74 6f 20 67 65 6e 65  s called to gene
26cdc 72 61 74 65 20 63 6f 64 65 20 65 78 65 63 75 74  rate code execut
26cdd 65 64 20 77 68 65 6e 20 61 20 72 6f 77 20 69 73  ed when a row is
26cde 20 64 65 6c 65 74 65 64 0a 2a 2a 20 66 72 6f 6d   deleted.** from
26cdf 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
26ce0 65 20 6f 66 20 66 6f 72 65 69 67 6e 20 6b 65 79  e of foreign key
26ce1 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 46 4b 65   constraint pFKe
26ce2 79 20 61 6e 64 2c 20 69 66 20 70 46 4b 65 79 20  y and, if pFKey 
26ce3 69 73 20 0a 2a 2a 20 64 65 66 65 72 72 65 64 2c  is .** deferred,
26ce4 20 77 68 65 6e 20 61 20 72 6f 77 20 69 73 20 69   when a row is i
26ce5 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65  nserted into the
26ce6 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 57 68 65   same table. Whe
26ce7 6e 20 67 65 6e 65 72 61 74 69 6e 67 0a 2a 2a 20  n generating.** 
26ce8 63 6f 64 65 20 66 6f 72 20 61 6e 20 53 51 4c 20  code for an SQL 
26ce9 55 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e  UPDATE operation
26cea 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
26ceb 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 74 77  may be called tw
26cec 69 63 65 20 2d 0a 2a 2a 20 6f 6e 63 65 20 74 6f  ice -.** once to
26ced 20 22 64 65 6c 65 74 65 22 20 74 68 65 20 6f 6c   "delete" the ol
26cee 64 20 72 6f 77 20 61 6e 64 20 6f 6e 63 65 20 74  d row and once t
26cef 6f 20 22 69 6e 73 65 72 74 22 20 74 68 65 20 6e  o "insert" the n
26cf0 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68  ew row..**.** Th
26cf1 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  e code generated
26cf2 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
26cf3 6e 20 73 63 61 6e 73 20 74 68 72 6f 75 67 68 20  n scans through 
26cf4 74 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65 20  the rows in the 
26cf5 63 68 69 6c 64 0a 2a 2a 20 74 61 62 6c 65 20 74  child.** table t
26cf6 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 20 74  hat correspond t
26cf7 6f 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  o the parent tab
26cf8 6c 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c  le row being del
26cf9 65 74 65 64 20 6f 72 20 69 6e 73 65 72 74 65 64  eted or inserted
26cfa 2e 0a 2a 2a 20 46 6f 72 20 65 61 63 68 20 63 68  ..** For each ch
26cfb 69 6c 64 20 72 6f 77 20 66 6f 75 6e 64 2c 20 6f  ild row found, o
26cfc 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
26cfd 69 6e 67 20 61 63 74 69 6f 6e 73 20 69 73 20 74  ing actions is t
26cfe 61 6b 65 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 70  aken:.**.**   Op
26cff 65 72 61 74 69 6f 6e 20 7c 20 46 4b 20 74 79 70  eration | FK typ
26d00 65 20 20 20 7c 20 41 63 74 69 6f 6e 20 74 61 6b  e   | Action tak
26d01 65 6e 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  en.**   --------
26d02 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26d03 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26d04 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26d05 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26d06 2d 2d 0a 2a 2a 20 20 20 44 45 4c 45 54 45 20 20  --.**   DELETE  
26d07 20 20 20 20 69 6d 6d 65 64 69 61 74 65 20 20 20      immediate   
26d08 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 22 69  Increment the "i
26d09 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74 72 61  mmediate constra
26d0a 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a  int counter"..**
26d0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d0c 20 20 20 20 20 20 20 20 20 20 20 4f 72 2c 20 69             Or, i
26d0d 66 20 74 68 65 20 4f 4e 20 28 55 50 44 41 54 45  f the ON (UPDATE
26d0e 7c 44 45 4c 45 54 45 29 20 61 63 74 69 6f 6e 20  |DELETE) action 
26d0f 69 73 20 52 45 53 54 52 49 43 54 2c 0a 2a 2a 20  is RESTRICT,.** 
26d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d11 20 20 20 20 20 20 20 20 20 20 74 68 72 6f 77 20            throw 
26d12 61 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  a "foreign key c
26d13 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
26d14 22 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a  " exception..**.
26d15 2a 2a 20 20 20 49 4e 53 45 52 54 20 20 20 20 20  **   INSERT     
26d16 20 69 6d 6d 65 64 69 61 74 65 20 20 20 44 65 63   immediate   Dec
26d17 72 65 6d 65 6e 74 20 74 68 65 20 22 69 6d 6d 65  rement the "imme
26d18 64 69 61 74 65 20 63 6f 6e 73 74 72 61 69 6e 74  diate constraint
26d19 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a 2a   counter"..**.**
26d1a 20 20 20 44 45 4c 45 54 45 20 20 20 20 20 20 64     DELETE      d
26d1b 65 66 65 72 72 65 64 20 20 20 20 49 6e 63 72 65  eferred    Incre
26d1c 6d 65 6e 74 20 74 68 65 20 22 64 65 66 65 72 72  ment the "deferr
26d1d 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f  ed constraint co
26d1e 75 6e 74 65 72 22 2e 0a 2a 2a 20 20 20 20 20 20  unter"..**      
26d1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d20 20 20 20 20 20 4f 72 2c 20 69 66 20 74 68 65 20       Or, if the 
26d21 4f 4e 20 28 55 50 44 41 54 45 7c 44 45 4c 45 54  ON (UPDATE|DELET
26d22 45 29 20 61 63 74 69 6f 6e 20 69 73 20 52 45 53  E) action is RES
26d23 54 52 49 43 54 2c 0a 2a 2a 20 20 20 20 20 20 20  TRICT,.**       
26d24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d25 20 20 20 20 74 68 72 6f 77 20 61 20 22 66 6f 72      throw a "for
26d26 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
26d27 69 6e 74 20 66 61 69 6c 65 64 22 20 65 78 63 65  int failed" exce
26d28 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49  ption..**.**   I
26d29 4e 53 45 52 54 20 20 20 20 20 20 64 65 66 65 72  NSERT      defer
26d2a 72 65 64 20 20 20 20 44 65 63 72 65 6d 65 6e 74  red    Decrement
26d2b 20 74 68 65 20 22 64 65 66 65 72 72 65 64 20 63   the "deferred c
26d2c 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65  onstraint counte
26d2d 72 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  r"..**.** These 
26d2e 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 69  operations are i
26d2f 64 65 6e 74 69 66 69 65 64 20 69 6e 20 74 68 65  dentified in the
26d30 20 63 6f 6d 6d 65 6e 74 20 61 74 20 74 68 65 20   comment at the 
26d31 74 6f 70 20 6f 66 20 74 68 69 73 20 66 69 6c 65  top of this file
26d32 20 0a 2a 2a 20 28 66 6b 65 79 2e 63 29 20 61 73   .** (fkey.c) as
26d33 20 22 49 2e 32 22 20 61 6e 64 20 22 44 2e 32 22   "I.2" and "D.2"
26d34 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
26d35 20 66 6b 53 63 61 6e 43 68 69 6c 64 72 65 6e 28   fkScanChildren(
26d36 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
26d37 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26d38 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
26d39 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ext */.  SrcList
26d3a 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 20   *pSrc,         
26d3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 72 63 4c           /* SrcL
26d3c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ist containing t
26d3d 68 65 20 74 61 62 6c 65 20 74 6f 20 73 63 61 6e  he table to scan
26d3e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
26d3f 62 2c 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  b,.  Index *pIdx
26d40 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26d41 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20       /* Foreign 
26d42 6b 65 79 20 69 6e 64 65 78 20 2a 2f 0a 20 20 46  key index */.  F
26d43 4b 65 79 20 2a 70 46 4b 65 79 2c 20 20 20 20 20  Key *pFKey,     
26d44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26d45 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 72 65  * Foreign key re
26d46 6c 61 74 69 6f 6e 73 68 69 70 20 2a 2f 0a 20 20  lationship */.  
26d47 69 6e 74 20 2a 61 69 43 6f 6c 2c 20 20 20 20 20  int *aiCol,     
26d48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d49 2f 2a 20 4d 61 70 20 66 72 6f 6d 20 70 49 64 78  /* Map from pIdx
26d4a 20 63 6f 6c 73 20 74 6f 20 63 68 69 6c 64 20 74   cols to child t
26d4b 61 62 6c 65 20 63 6f 6c 73 20 2a 2f 0a 20 20 69  able cols */.  i
26d4c 6e 74 20 72 65 67 44 61 74 61 2c 20 20 20 20 20  nt regData,     
26d4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26d4e 2a 20 52 65 66 65 72 65 6e 63 65 64 20 74 61 62  * Referenced tab
26d4f 6c 65 20 64 61 74 61 20 73 74 61 72 74 73 20 68  le data starts h
26d50 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e  ere */.  int nIn
26d51 63 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  cr              
26d52 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75           /* Amou
26d53 6e 74 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20  nt to increment 
26d54 64 65 66 65 72 72 65 64 20 63 6f 75 6e 74 65 72  deferred counter
26d55 20 62 79 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69   by */.){.  sqli
26d56 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
26d57 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44  ->db;       /* D
26d58 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
26d59 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
26d5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d5b 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
26d5c 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 45 78  variable */.  Ex
26d5d 70 72 20 2a 70 57 68 65 72 65 20 3d 20 30 3b 20  pr *pWhere = 0; 
26d5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26d5f 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f   WHERE clause to
26d60 20 73 63 61 6e 20 77 69 74 68 20 2a 2f 0a 20 20   scan with */.  
26d61 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 61 6d  NameContext sNam
26d62 65 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20  eContext;       
26d63 2f 2a 20 43 6f 6e 74 65 78 74 20 75 73 65 64 20  /* Context used 
26d64 74 6f 20 72 65 73 6f 6c 76 65 20 57 48 45 52 45  to resolve WHERE
26d65 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
26d66 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20  reInfo *pWInfo; 
26d67 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26d68 43 6f 6e 74 65 78 74 20 75 73 65 64 20 62 79 20  Context used by 
26d69 73 71 6c 69 74 65 33 57 68 65 72 65 58 58 58 28  sqlite3WhereXXX(
26d6a 29 20 2a 2f 0a 20 20 69 6e 74 20 69 46 6b 49 66  ) */.  int iFkIf
26d6b 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20  Zero = 0;       
26d6c 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
26d6d 73 20 6f 66 20 4f 50 5f 46 6b 49 66 5a 65 72 6f  s of OP_FkIfZero
26d6e 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20   */.  Vdbe *v = 
26d6f 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
26d70 50 61 72 73 65 29 3b 0a 0a 20 20 61 73 73 65 72  Parse);..  asser
26d71 74 28 20 21 70 49 64 78 20 7c 7c 20 70 49 64 78  t( !pIdx || pIdx
26d72 2d 3e 70 54 61 62 6c 65 3d 3d 70 54 61 62 20 29  ->pTable==pTab )
26d73 3b 0a 0a 20 20 69 66 28 20 6e 49 6e 63 72 3c 30  ;..  if( nIncr<0
26d74 20 29 7b 0a 20 20 20 20 69 46 6b 49 66 5a 65 72   ){.    iFkIfZer
26d75 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  o = sqlite3VdbeA
26d76 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 49 66  ddOp2(v, OP_FkIf
26d77 5a 65 72 6f 2c 20 70 46 4b 65 79 2d 3e 69 73 44  Zero, pFKey->isD
26d78 65 66 65 72 72 65 64 2c 20 30 29 3b 0a 20 20 7d  eferred, 0);.  }
26d79 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 61 6e  ..  /* Create an
26d7a 20 45 78 70 72 20 6f 62 6a 65 63 74 20 72 65 70   Expr object rep
26d7b 72 65 73 65 6e 74 69 6e 67 20 61 6e 20 53 51 4c  resenting an SQL
26d7c 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 6b 65   expression like
26d7d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 3c 70  :.  **.  **   <p
26d7e 61 72 65 6e 74 2d 6b 65 79 31 3e 20 3d 20 3c 63  arent-key1> = <c
26d7f 68 69 6c 64 2d 6b 65 79 31 3e 20 41 4e 44 20 3c  hild-key1> AND <
26d80 70 61 72 65 6e 74 2d 6b 65 79 32 3e 20 3d 20 3c  parent-key2> = <
26d81 63 68 69 6c 64 2d 6b 65 79 32 3e 20 2e 2e 2e 0a  child-key2> ....
26d82 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 6f    **.  ** The co
26d83 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
26d84 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f   used for the co
26d85 6d 70 61 72 69 73 6f 6e 20 73 68 6f 75 6c 64 20  mparison should 
26d86 62 65 20 74 68 61 74 20 6f 66 0a 20 20 2a 2a 20  be that of.  ** 
26d87 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63  the parent key c
26d88 6f 6c 75 6d 6e 73 2e 20 54 68 65 20 61 66 66 69  olumns. The affi
26d89 6e 69 74 79 20 6f 66 20 74 68 65 20 70 61 72 65  nity of the pare
26d8a 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 73 68  nt key column sh
26d8b 6f 75 6c 64 0a 20 20 2a 2a 20 62 65 20 61 70 70  ould.  ** be app
26d8c 6c 69 65 64 20 74 6f 20 65 61 63 68 20 63 68 69  lied to each chi
26d8d 6c 64 20 6b 65 79 20 76 61 6c 75 65 20 62 65 66  ld key value bef
26d8e 6f 72 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  ore the comparis
26d8f 6f 6e 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a  on takes place..
26d90 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
26d91 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69  i<pFKey->nCol; i
26d92 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
26d93 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20  Left;           
26d94 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
26d95 66 72 6f 6d 20 70 61 72 65 6e 74 20 74 61 62 6c  from parent tabl
26d96 65 20 72 6f 77 20 2a 2f 0a 20 20 20 20 45 78 70  e row */.    Exp
26d97 72 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20  r *pRight;      
26d98 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
26d99 6c 75 6d 6e 20 72 65 66 20 74 6f 20 63 68 69 6c  lumn ref to chil
26d9a 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45  d table */.    E
26d9b 78 70 72 20 2a 70 45 71 3b 20 20 20 20 20 20 20  xpr *pEq;       
26d9c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26d9d 45 78 70 72 65 73 73 69 6f 6e 20 28 70 4c 65 66  Expression (pLef
26d9e 74 20 3d 20 70 52 69 67 68 74 29 20 2a 2f 0a 20  t = pRight) */. 
26d9f 20 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20     int iCol;    
26da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26da1 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
26da2 75 6d 6e 20 69 6e 20 63 68 69 6c 64 20 74 61 62  umn in child tab
26da3 6c 65 20 2a 2f 20 0a 20 20 20 20 63 6f 6e 73 74  le */ .    const
26da4 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20   char *zCol;    
26da5 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
26da6 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63 68   of column in ch
26da7 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20  ild table */..  
26da8 20 20 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65    pLeft = sqlite
26da9 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 52 45 47  3Expr(db, TK_REG
26daa 49 53 54 45 52 2c 20 30 29 3b 0a 20 20 20 20 69  ISTER, 0);.    i
26dab 66 28 20 70 4c 65 66 74 20 29 7b 0a 20 20 20 20  f( pLeft ){.    
26dac 20 20 2f 2a 20 53 65 74 20 74 68 65 20 63 6f 6c    /* Set the col
26dad 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
26dae 61 6e 64 20 61 66 66 69 6e 69 74 79 20 6f 66 20  and affinity of 
26daf 74 68 65 20 4c 48 53 20 6f 66 20 65 61 63 68 20  the LHS of each 
26db0 54 4b 5f 45 51 0a 20 20 20 20 20 20 2a 2a 20 65  TK_EQ.      ** e
26db1 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 74 68 65  xpression to the
26db2 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
26db3 6d 6e 20 64 65 66 61 75 6c 74 73 2e 20 20 2a 2f  mn defaults.  */
26db4 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78 20  .      if( pIdx 
26db5 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d  ){.        Colum
26db6 6e 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20 20 20  n *pCol;.       
26db7 20 69 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 61 69   iCol = pIdx->ai
26db8 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20  Column[i];.     
26db9 20 20 20 70 43 6f 6c 20 3d 20 26 70 49 64 78 2d     pCol = &pIdx-
26dba 3e 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b 69 43  >pTable->aCol[iC
26dbb 6f 6c 5d 3b 0a 20 20 20 20 20 20 20 20 70 4c 65  ol];.        pLe
26dbc 66 74 2d 3e 69 54 61 62 6c 65 20 3d 20 72 65 67  ft->iTable = reg
26dbd 44 61 74 61 2b 69 43 6f 6c 2b 31 3b 0a 20 20 20  Data+iCol+1;.   
26dbe 20 20 20 20 20 70 4c 65 66 74 2d 3e 61 66 66 69       pLeft->affi
26dbf 6e 69 74 79 20 3d 20 70 43 6f 6c 2d 3e 61 66 66  nity = pCol->aff
26dc0 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 70  inity;.        p
26dc1 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71  Left->pColl = sq
26dc2 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
26dc3 65 71 28 70 50 61 72 73 65 2c 20 70 43 6f 6c 2d  eq(pParse, pCol-
26dc4 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 7d  >zColl);.      }
26dc5 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 4c  else{.        pL
26dc6 65 66 74 2d 3e 69 54 61 62 6c 65 20 3d 20 72 65  eft->iTable = re
26dc7 67 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 70  gData;.        p
26dc8 4c 65 66 74 2d 3e 61 66 66 69 6e 69 74 79 20 3d  Left->affinity =
26dc9 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
26dca 47 45 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  GER;.      }.   
26dcb 20 7d 0a 20 20 20 20 69 43 6f 6c 20 3d 20 61 69   }.    iCol = ai
26dcc 43 6f 6c 20 3f 20 61 69 43 6f 6c 5b 69 5d 20 3a  Col ? aiCol[i] :
26dcd 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e   pFKey->aCol[0].
26dce 69 46 72 6f 6d 3b 0a 20 20 20 20 61 73 73 65 72  iFrom;.    asser
26dcf 74 28 20 69 43 6f 6c 3e 3d 30 20 29 3b 0a 20 20  t( iCol>=0 );.  
26dd0 20 20 7a 43 6f 6c 20 3d 20 70 46 4b 65 79 2d 3e    zCol = pFKey->
26dd1 70 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 43 6f 6c  pFrom->aCol[iCol
26dd2 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 70 52 69  ].zName;.    pRi
26dd3 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ght = sqlite3Exp
26dd4 72 28 64 62 2c 20 54 4b 5f 49 44 2c 20 7a 43 6f  r(db, TK_ID, zCo
26dd5 6c 29 3b 0a 20 20 20 20 70 45 71 20 3d 20 73 71  l);.    pEq = sq
26dd6 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
26dd7 65 2c 20 54 4b 5f 45 51 2c 20 70 4c 65 66 74 2c  e, TK_EQ, pLeft,
26dd8 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20   pRight, 0);.   
26dd9 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65   pWhere = sqlite
26dda 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 57 68  3ExprAnd(db, pWh
26ddb 65 72 65 2c 20 70 45 71 29 3b 0a 20 20 7d 0a 0a  ere, pEq);.  }..
26ddc 20 20 2f 2a 20 49 66 20 74 68 65 20 63 68 69 6c    /* If the chil
26ddd 64 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 73  d table is the s
26dde 61 6d 65 20 61 73 20 74 68 65 20 70 61 72 65 6e  ame as the paren
26ddf 74 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 69  t table, and thi
26de0 73 20 73 63 61 6e 0a 20 20 2a 2a 20 69 73 20 74  s scan.  ** is t
26de1 61 6b 69 6e 67 20 70 6c 61 63 65 20 61 73 20 70  aking place as p
26de2 61 72 74 20 6f 66 20 61 20 44 45 4c 45 54 45 20  art of a DELETE 
26de3 6f 70 65 72 61 74 69 6f 6e 20 28 6f 70 65 72 61  operation (opera
26de4 74 69 6f 6e 20 44 2e 32 29 2c 20 6f 6d 69 74 20  tion D.2), omit 
26de5 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69  the.  ** row bei
26de6 6e 67 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20  ng deleted from 
26de7 74 68 65 20 73 63 61 6e 20 62 79 20 61 64 64 69  the scan by addi
26de8 6e 67 20 28 24 72 6f 77 69 64 20 21 3d 20 72 6f  ng ($rowid != ro
26de9 77 69 64 29 20 74 6f 20 74 68 65 20 57 48 45 52  wid) to the WHER
26dea 45 20 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2c 20  E .  ** clause, 
26deb 77 68 65 72 65 20 24 72 6f 77 69 64 20 69 73 20  where $rowid is 
26dec 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
26ded 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74   row being delet
26dee 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 54  ed.  */.  if( pT
26def 61 62 3d 3d 70 46 4b 65 79 2d 3e 70 46 72 6f 6d  ab==pFKey->pFrom
26df0 20 26 26 20 6e 49 6e 63 72 3e 30 20 29 7b 0a 20   && nIncr>0 ){. 
26df1 20 20 20 45 78 70 72 20 2a 70 45 71 3b 20 20 20     Expr *pEq;   
26df2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26df3 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 28   /* Expression (
26df4 70 4c 65 66 74 20 3d 20 70 52 69 67 68 74 29 20  pLeft = pRight) 
26df5 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65  */.    Expr *pLe
26df6 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ft;             
26df7 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 72       /* Value fr
26df8 6f 6d 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  om parent table 
26df9 72 6f 77 20 2a 2f 0a 20 20 20 20 45 78 70 72 20  row */.    Expr 
26dfa 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 20  *pRight;        
26dfb 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
26dfc 6d 6e 20 72 65 66 20 74 6f 20 63 68 69 6c 64 20  mn ref to child 
26dfd 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 70 4c 65  table */.    pLe
26dfe 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ft = sqlite3Expr
26dff 28 64 62 2c 20 54 4b 5f 52 45 47 49 53 54 45 52  (db, TK_REGISTER
26e00 2c 20 30 29 3b 0a 20 20 20 20 70 52 69 67 68 74  , 0);.    pRight
26e01 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
26e02 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 29  b, TK_COLUMN, 0)
26e03 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 20  ;.    if( pLeft 
26e04 26 26 20 70 52 69 67 68 74 20 29 7b 0a 20 20 20  && pRight ){.   
26e05 20 20 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65     pLeft->iTable
26e06 20 3d 20 72 65 67 44 61 74 61 3b 0a 20 20 20 20   = regData;.    
26e07 20 20 70 4c 65 66 74 2d 3e 61 66 66 69 6e 69 74    pLeft->affinit
26e08 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49  y = SQLITE_AFF_I
26e09 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 70 52  NTEGER;.      pR
26e0a 69 67 68 74 2d 3e 69 54 61 62 6c 65 20 3d 20 70  ight->iTable = p
26e0b 53 72 63 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f  Src->a[0].iCurso
26e0c 72 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74 2d  r;.      pRight-
26e0d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20  >iColumn = -1;. 
26e0e 20 20 20 7d 0a 20 20 20 20 70 45 71 20 3d 20 73     }.    pEq = s
26e0f 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
26e10 73 65 2c 20 54 4b 5f 4e 45 2c 20 70 4c 65 66 74  se, TK_NE, pLeft
26e11 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20  , pRight, 0);.  
26e12 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74    pWhere = sqlit
26e13 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 57  e3ExprAnd(db, pW
26e14 68 65 72 65 2c 20 70 45 71 29 3b 0a 20 20 7d 0a  here, pEq);.  }.
26e15 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68  .  /* Resolve th
26e16 65 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e 20  e references in 
26e17 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
26e18 2e 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73  . */.  memset(&s
26e19 4e 61 6d 65 43 6f 6e 74 65 78 74 2c 20 30 2c 20  NameContext, 0, 
26e1a 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e 74 65  sizeof(NameConte
26e1b 78 74 29 29 3b 0a 20 20 73 4e 61 6d 65 43 6f 6e  xt));.  sNameCon
26e1c 74 65 78 74 2e 70 53 72 63 4c 69 73 74 20 3d 20  text.pSrcList = 
26e1d 70 53 72 63 3b 0a 20 20 73 4e 61 6d 65 43 6f 6e  pSrc;.  sNameCon
26e1e 74 65 78 74 2e 70 50 61 72 73 65 20 3d 20 70 50  text.pParse = pP
26e1f 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 52  arse;.  sqlite3R
26e20 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
26e21 26 73 4e 61 6d 65 43 6f 6e 74 65 78 74 2c 20 70  &sNameContext, p
26e22 57 68 65 72 65 29 3b 0a 0a 20 20 2f 2a 20 43 72  Where);..  /* Cr
26e23 65 61 74 65 20 56 44 42 45 20 74 6f 20 6c 6f 6f  eate VDBE to loo
26e24 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 6e  p through the en
26e25 74 72 69 65 73 20 69 6e 20 70 53 72 63 20 74 68  tries in pSrc th
26e26 61 74 20 6d 61 74 63 68 20 74 68 65 20 57 48 45  at match the WHE
26e27 52 45 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e 20  RE.  ** clause. 
26e28 49 66 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  If the constrain
26e29 74 20 69 73 20 6e 6f 74 20 64 65 66 65 72 72 65  t is not deferre
26e2a 64 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65  d, throw an exce
26e2b 70 74 69 6f 6e 20 66 6f 72 0a 20 20 2a 2a 20 65  ption for.  ** e
26e2c 61 63 68 20 72 6f 77 20 66 6f 75 6e 64 2e 20 4f  ach row found. O
26e2d 74 68 65 72 77 69 73 65 2c 20 66 6f 72 20 64 65  therwise, for de
26e2e 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  ferred constrain
26e2f 74 73 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68  ts, increment th
26e30 65 0a 20 20 2a 2a 20 64 65 66 65 72 72 65 64 20  e.  ** deferred 
26e31 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
26e32 65 72 20 62 79 20 6e 49 6e 63 72 20 66 6f 72 20  er by nIncr for 
26e33 65 61 63 68 20 72 6f 77 20 73 65 6c 65 63 74 65  each row selecte
26e34 64 2e 20 20 2a 2f 0a 20 20 70 57 49 6e 66 6f 20  d.  */.  pWInfo 
26e35 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  = sqlite3WhereBe
26e36 67 69 6e 28 70 50 61 72 73 65 2c 20 70 53 72 63  gin(pParse, pSrc
26e37 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30 29 3b  , pWhere, 0, 0);
26e38 0a 20 20 69 66 28 20 6e 49 6e 63 72 3e 30 20 26  .  if( nIncr>0 &
26e39 26 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72  & pFKey->isDefer
26e3a 72 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  red==0 ){.    sq
26e3b 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76  lite3ParseToplev
26e3c 65 6c 28 70 50 61 72 73 65 29 2d 3e 6d 61 79 41  el(pParse)->mayA
26e3d 62 6f 72 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  bort = 1;.  }.  
26e3e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26e3f 32 28 76 2c 20 4f 50 5f 46 6b 43 6f 75 6e 74 65  2(v, OP_FkCounte
26e40 72 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65  r, pFKey->isDefe
26e41 72 72 65 64 2c 20 6e 49 6e 63 72 29 3b 0a 20 20  rred, nIncr);.  
26e42 69 66 28 20 70 57 49 6e 66 6f 20 29 7b 0a 20 20  if( pWInfo ){.  
26e43 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e    sqlite3WhereEn
26e44 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a  d(pWInfo);.  }..
26e45 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20 74 68    /* Clean up th
26e46 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63  e WHERE clause c
26e47 6f 6e 73 74 72 75 63 74 65 64 20 61 62 6f 76 65  onstructed above
26e48 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78  . */.  sqlite3Ex
26e49 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68  prDelete(db, pWh
26e4a 65 72 65 29 3b 0a 20 20 69 66 28 20 69 46 6b 49  ere);.  if( iFkI
26e4b 66 5a 65 72 6f 20 29 7b 0a 20 20 20 20 73 71 6c  fZero ){.    sql
26e4c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
26e4d 28 76 2c 20 69 46 6b 49 66 5a 65 72 6f 29 3b 0a  (v, iFkIfZero);.
26e4e 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
26e4f 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
26e50 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
26e51 74 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c 69  the head of a li
26e52 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 46 4b 0a  nked list of FK.
26e53 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66  ** constraints f
26e54 6f 72 20 77 68 69 63 68 20 74 61 62 6c 65 20 70  or which table p
26e55 54 61 62 20 69 73 20 74 68 65 20 70 61 72 65 6e  Tab is the paren
26e56 74 20 74 61 62 6c 65 2e 20 46 6f 72 20 65 78 61  t table. For exa
26e57 6d 70 6c 65 2c 0a 2a 2a 20 67 69 76 65 6e 20 74  mple,.** given t
26e58 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63 68  he following sch
26e59 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 43 52 45  ema:.**.**   CRE
26e5a 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 20 50  ATE TABLE t1(a P
26e5b 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 2a 2a 20  RIMARY KEY);.** 
26e5c 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
26e5d 32 28 62 20 52 45 46 45 52 45 4e 43 45 53 20 74  2(b REFERENCES t
26e5e 31 28 61 29 3b 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c  1(a);.**.** Call
26e5f 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
26e60 6e 20 77 69 74 68 20 74 61 62 6c 65 20 22 74 31  n with table "t1
26e61 22 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  " as an argument
26e62 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
26e63 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 46 4b 65  er.** to the FKe
26e64 79 20 73 74 72 75 63 74 75 72 65 20 72 65 70 72  y structure repr
26e65 65 73 65 6e 74 69 6e 67 20 74 68 65 20 66 6f 72  esenting the for
26e66 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
26e67 69 6e 74 20 6f 6e 20 74 61 62 6c 65 0a 2a 2a 20  int on table.** 
26e68 22 74 32 22 2e 20 43 61 6c 6c 69 6e 67 20 74 68  "t2". Calling th
26e69 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  is function with
26e6a 20 22 74 32 22 20 61 73 20 74 68 65 20 61 72 67   "t2" as the arg
26e6b 75 6d 65 6e 74 20 77 6f 75 6c 64 20 72 65 74 75  ument would retu
26e6c 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69  rn a.** NULL poi
26e6d 6e 74 65 72 20 28 61 73 20 74 68 65 72 65 20 61  nter (as there a
26e6e 72 65 20 6e 6f 20 46 4b 20 63 6f 6e 73 74 72 61  re no FK constra
26e6f 69 6e 74 73 20 66 6f 72 20 77 68 69 63 68 20 74  ints for which t
26e70 32 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 0a  2 is the parent.
26e71 2a 2a 20 74 61 62 6c 65 29 2e 0a 2a 2f 0a 53 51  ** table)..*/.SQ
26e72 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 4b 65  LITE_PRIVATE FKe
26e73 79 20 2a 73 71 6c 69 74 65 33 46 6b 52 65 66 65  y *sqlite3FkRefe
26e74 72 65 6e 63 65 73 28 54 61 62 6c 65 20 2a 70 54  rences(Table *pT
26e75 61 62 29 7b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  ab){.  int nName
26e76 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
26e77 33 30 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  30(pTab->zName);
26e78 0a 20 20 72 65 74 75 72 6e 20 28 46 4b 65 79 20  .  return (FKey 
26e79 2a 29 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  *)sqlite3HashFin
26e7a 64 28 26 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  d(&pTab->pSchema
26e7b 2d 3e 66 6b 65 79 48 61 73 68 2c 20 70 54 61 62  ->fkeyHash, pTab
26e7c 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  ->zName, nName);
26e7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65  .}../*.** The se
26e7e 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
26e7f 20 61 20 54 72 69 67 67 65 72 20 73 74 72 75 63   a Trigger struc
26e80 74 75 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62  ture allocated b
26e81 79 20 74 68 65 20 0a 2a 2a 20 66 6b 41 63 74 69  y the .** fkActi
26e82 6f 6e 54 72 69 67 67 65 72 28 29 20 72 6f 75 74  onTrigger() rout
26e83 69 6e 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ine. This functi
26e84 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 65 20 54  on deletes the T
26e85 72 69 67 67 65 72 20 73 74 72 75 63 74 75 72 65  rigger structure
26e86 0a 2a 2a 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69  .** and all of i
26e87 74 73 20 73 75 62 2d 63 6f 6d 70 6f 6e 65 6e 74  ts sub-component
26e88 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 72 69  s..**.** The Tri
26e89 67 67 65 72 20 73 74 72 75 63 74 75 72 65 20 6f  gger structure o
26e8a 72 20 61 6e 79 20 6f 66 20 69 74 73 20 73 75 62  r any of its sub
26e8b 2d 63 6f 6d 70 6f 6e 65 6e 74 73 20 6d 61 79 20  -components may 
26e8c 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 6f  be allocated fro
26e8d 6d 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69  m.** the lookasi
26e8e 64 65 20 62 75 66 66 65 72 20 62 65 6c 6f 6e 67  de buffer belong
26e8f 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73 65 20  ing to database 
26e90 68 61 6e 64 6c 65 20 64 62 4d 65 6d 2e 0a 2a 2f  handle dbMem..*/
26e91 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 6b 54  .static void fkT
26e92 72 69 67 67 65 72 44 65 6c 65 74 65 28 73 71 6c  riggerDelete(sql
26e93 69 74 65 33 20 2a 64 62 4d 65 6d 2c 20 54 72 69  ite3 *dbMem, Tri
26e94 67 67 65 72 20 2a 70 29 7b 0a 20 20 69 66 28 20  gger *p){.  if( 
26e95 70 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72  p ){.    Trigger
26e96 53 74 65 70 20 2a 70 53 74 65 70 20 3d 20 70 2d  Step *pStep = p-
26e97 3e 73 74 65 70 5f 6c 69 73 74 3b 0a 20 20 20 20  >step_list;.    
26e98 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
26e99 65 28 64 62 4d 65 6d 2c 20 70 53 74 65 70 2d 3e  e(dbMem, pStep->
26e9a 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c  pWhere);.    sql
26e9b 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
26e9c 74 65 28 64 62 4d 65 6d 2c 20 70 53 74 65 70 2d  te(dbMem, pStep-
26e9d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a 20 20 20  >pExprList);.   
26e9e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
26e9f 6c 65 74 65 28 64 62 4d 65 6d 2c 20 70 53 74 65  lete(dbMem, pSte
26ea0 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  p->pSelect);.   
26ea1 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
26ea2 74 65 28 64 62 4d 65 6d 2c 20 70 2d 3e 70 57 68  te(dbMem, p->pWh
26ea3 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  en);.    sqlite3
26ea4 44 62 46 72 65 65 28 64 62 4d 65 6d 2c 20 70 29  DbFree(dbMem, p)
26ea5 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
26ea6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
26ea7 63 61 6c 6c 65 64 20 74 6f 20 67 65 6e 65 72 61  called to genera
26ea8 74 65 20 63 6f 64 65 20 74 68 61 74 20 72 75 6e  te code that run
26ea9 73 20 77 68 65 6e 20 74 61 62 6c 65 20 70 54 61  s when table pTa
26eaa 62 20 69 73 0a 2a 2a 20 62 65 69 6e 67 20 64 72  b is.** being dr
26eab 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 64  opped from the d
26eac 61 74 61 62 61 73 65 2e 20 54 68 65 20 53 72 63  atabase. The Src
26ead 4c 69 73 74 20 70 61 73 73 65 64 20 61 73 20 74  List passed as t
26eae 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
26eaf 6e 74 0a 2a 2a 20 74 6f 20 74 68 69 73 20 66 75  nt.** to this fu
26eb0 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  nction contains 
26eb1 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 67  a single entry g
26eb2 75 61 72 61 6e 74 65 65 64 20 74 6f 20 72 65 73  uaranteed to res
26eb3 6f 6c 76 65 20 74 6f 0a 2a 2a 20 74 61 62 6c 65  olve to.** table
26eb4 20 70 54 61 62 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72   pTab..**.** Nor
26eb5 6d 61 6c 6c 79 2c 20 6e 6f 20 63 6f 64 65 20 69  mally, no code i
26eb6 73 20 72 65 71 75 69 72 65 64 2e 20 48 6f 77 65  s required. Howe
26eb7 76 65 72 2c 20 69 66 20 65 69 74 68 65 72 0a 2a  ver, if either.*
26eb8 2a 0a 2a 2a 20 20 20 28 61 29 20 54 68 65 20 74  *.**   (a) The t
26eb9 61 62 6c 65 20 69 73 20 74 68 65 20 70 61 72 65  able is the pare
26eba 6e 74 20 74 61 62 6c 65 20 6f 66 20 61 20 46 4b  nt table of a FK
26ebb 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6f 72 0a   constraint, or.
26ebc 2a 2a 20 20 20 28 62 29 20 54 68 65 20 74 61 62  **   (b) The tab
26ebd 6c 65 20 69 73 20 74 68 65 20 63 68 69 6c 64 20  le is the child 
26ebe 74 61 62 6c 65 20 6f 66 20 61 20 64 65 66 65 72  table of a defer
26ebf 72 65 64 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e  red FK constrain
26ec0 74 20 61 6e 64 20 69 74 20 69 73 0a 2a 2a 20 20  t and it is.**  
26ec1 20 20 20 20 20 64 65 74 65 72 6d 69 6e 65 64 20       determined 
26ec2 61 74 20 72 75 6e 74 69 6d 65 20 74 68 61 74 20  at runtime that 
26ec3 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61  there are outsta
26ec4 6e 64 69 6e 67 20 64 65 66 65 72 72 65 64 20 46  nding deferred F
26ec5 4b 20 0a 2a 2a 20 20 20 20 20 20 20 63 6f 6e 73  K .**       cons
26ec6 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
26ec7 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
26ec8 65 2c 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68  e,.**.** then th
26ec9 65 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20  e equivalent of 
26eca 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 3c 74 62  "DELETE FROM <tb
26ecb 6c 3e 22 20 69 73 20 65 78 65 63 75 74 65 64 20  l>" is executed 
26ecc 62 65 66 6f 72 65 20 64 72 6f 70 70 69 6e 67 0a  before dropping.
26ecd 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f  ** the table fro
26ece 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  m the database. 
26ecf 54 72 69 67 67 65 72 73 20 61 72 65 20 64 69 73  Triggers are dis
26ed0 61 62 6c 65 64 20 77 68 69 6c 65 20 72 75 6e 6e  abled while runn
26ed1 69 6e 67 20 74 68 69 73 0a 2a 2a 20 44 45 4c 45  ing this.** DELE
26ed2 54 45 2c 20 62 75 74 20 66 6f 72 65 69 67 6e 20  TE, but foreign 
26ed3 6b 65 79 20 61 63 74 69 6f 6e 73 20 61 72 65 20  key actions are 
26ed4 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  not..*/.SQLITE_P
26ed5 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
26ed6 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65 28 50  te3FkDropTable(P
26ed7 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72  arse *pParse, Sr
26ed8 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20 54 61  cList *pName, Ta
26ed9 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 73 71  ble *pTab){.  sq
26eda 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
26edb 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 64  se->db;.  if( (d
26edc 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
26edd 46 6f 72 65 69 67 6e 4b 65 79 73 29 20 26 26 20  ForeignKeys) && 
26ede 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  !IsVirtual(pTab)
26edf 20 26 26 20 21 70 54 61 62 2d 3e 70 53 65 6c 65   && !pTab->pSele
26ee0 63 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 53  ct ){.    int iS
26ee1 6b 69 70 20 3d 20 30 3b 0a 20 20 20 20 56 64 62  kip = 0;.    Vdb
26ee2 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
26ee3 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 0a  tVdbe(pParse);..
26ee4 20 20 20 20 61 73 73 65 72 74 28 20 76 20 29 3b      assert( v );
26ee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26ee6 20 20 2f 2a 20 56 44 42 45 20 68 61 73 20 61 6c    /* VDBE has al
26ee7 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63  ready been alloc
26ee8 61 74 65 64 20 2a 2f 0a 20 20 20 20 69 66 28 20  ated */.    if( 
26ee9 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e  sqlite3FkReferen
26eea 63 65 73 28 70 54 61 62 29 3d 3d 30 20 29 7b 0a  ces(pTab)==0 ){.
26eeb 20 20 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20        /* Search 
26eec 66 6f 72 20 61 20 64 65 66 65 72 72 65 64 20 66  for a deferred f
26eed 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
26eee 72 61 69 6e 74 20 66 6f 72 20 77 68 69 63 68 20  raint for which 
26eef 74 68 69 73 20 74 61 62 6c 65 0a 20 20 20 20 20  this table.     
26ef0 20 2a 2a 20 69 73 20 74 68 65 20 63 68 69 6c 64   ** is the child
26ef1 20 74 61 62 6c 65 2e 20 49 66 20 6f 6e 65 20 63   table. If one c
26ef2 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20  annot be found, 
26ef3 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 0a  return without .
26ef4 20 20 20 20 20 20 2a 2a 20 67 65 6e 65 72 61 74        ** generat
26ef5 69 6e 67 20 61 6e 79 20 56 44 42 45 20 63 6f 64  ing any VDBE cod
26ef6 65 2e 20 49 66 20 6f 6e 65 20 63 61 6e 20 62 65  e. If one can be
26ef7 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 6a 75 6d   found, then jum
26ef8 70 20 6f 76 65 72 0a 20 20 20 20 20 20 2a 2a 20  p over.      ** 
26ef9 74 68 65 20 65 6e 74 69 72 65 20 44 45 4c 45 54  the entire DELET
26efa 45 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e  E if there are n
26efb 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65  o outstanding de
26efc 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  ferred constrain
26efd 74 73 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 6e  ts.      ** when
26efe 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   this statement 
26eff 69 73 20 72 75 6e 2e 20 20 2a 2f 0a 20 20 20 20  is run.  */.    
26f00 20 20 46 4b 65 79 20 2a 70 3b 0a 20 20 20 20 20    FKey *p;.     
26f01 20 66 6f 72 28 70 3d 70 54 61 62 2d 3e 70 46 4b   for(p=pTab->pFK
26f02 65 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  ey; p; p=p->pNex
26f03 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 20 20  tFrom){.        
26f04 69 66 28 20 70 2d 3e 69 73 44 65 66 65 72 72 65  if( p->isDeferre
26f05 64 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  d ) break;.     
26f06 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 20   }.      if( !p 
26f07 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20  ) return;.      
26f08 69 53 6b 69 70 20 3d 20 73 71 6c 69 74 65 33 56  iSkip = sqlite3V
26f09 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
26f0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26f0b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46  beAddOp2(v, OP_F
26f0c 6b 49 66 5a 65 72 6f 2c 20 31 2c 20 69 53 6b 69  kIfZero, 1, iSki
26f0d 70 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70  p);.    }..    p
26f0e 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 54 72  Parse->disableTr
26f0f 69 67 67 65 72 73 20 3d 20 31 3b 0a 20 20 20 20  iggers = 1;.    
26f10 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f  sqlite3DeleteFro
26f11 6d 28 70 50 61 72 73 65 2c 20 73 71 6c 69 74 65  m(pParse, sqlite
26f12 33 53 72 63 4c 69 73 74 44 75 70 28 64 62 2c 20  3SrcListDup(db, 
26f13 70 4e 61 6d 65 2c 20 30 29 2c 20 30 29 3b 0a 20  pName, 0), 0);. 
26f14 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62     pParse->disab
26f15 6c 65 54 72 69 67 67 65 72 73 20 3d 20 30 3b 0a  leTriggers = 0;.
26f16 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 44  .    /* If the D
26f17 45 4c 45 54 45 20 68 61 73 20 67 65 6e 65 72 61  ELETE has genera
26f18 74 65 64 20 69 6d 6d 65 64 69 61 74 65 20 66 6f  ted immediate fo
26f19 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
26f1a 61 69 6e 74 20 0a 20 20 20 20 2a 2a 20 76 69 6f  aint .    ** vio
26f1b 6c 61 74 69 6f 6e 73 2c 20 68 61 6c 74 20 74 68  lations, halt th
26f1c 65 20 56 44 42 45 20 61 6e 64 20 72 65 74 75 72  e VDBE and retur
26f1d 6e 20 61 6e 20 65 72 72 6f 72 20 61 74 20 74 68  n an error at th
26f1e 69 73 20 70 6f 69 6e 74 2c 20 62 65 66 6f 72 65  is point, before
26f1f 0a 20 20 20 20 2a 2a 20 61 6e 79 20 6d 6f 64 69  .    ** any modi
26f20 66 69 63 61 74 69 6f 6e 73 20 74 6f 20 74 68 65  fications to the
26f21 20 73 63 68 65 6d 61 20 61 72 65 20 6d 61 64 65   schema are made
26f22 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73  . This is becaus
26f23 65 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20  e statement.    
26f24 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  ** transactions 
26f25 61 72 65 20 6e 6f 74 20 61 62 6c 65 20 74 6f 20  are not able to 
26f26 72 6f 6c 6c 62 61 63 6b 20 73 63 68 65 6d 61 20  rollback schema 
26f27 63 68 61 6e 67 65 73 2e 20 20 2a 2f 0a 20 20 20  changes.  */.   
26f28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26f29 70 32 28 76 2c 20 4f 50 5f 46 6b 49 66 5a 65 72  p2(v, OP_FkIfZer
26f2a 6f 2c 20 30 2c 20 73 71 6c 69 74 65 33 56 64 62  o, 0, sqlite3Vdb
26f2b 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b  eCurrentAddr(v)+
26f2c 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48  2);.    sqlite3H
26f2d 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20  altConstraint(. 
26f2e 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 4f         pParse, O
26f2f 45 5f 41 62 6f 72 74 2c 20 22 66 6f 72 65 69 67  E_Abort, "foreig
26f30 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
26f31 20 66 61 69 6c 65 64 22 2c 20 50 34 5f 53 54 41   failed", P4_STA
26f32 54 49 43 0a 20 20 20 20 29 3b 0a 0a 20 20 20 20  TIC.    );..    
26f33 69 66 28 20 69 53 6b 69 70 20 29 7b 0a 20 20 20  if( iSkip ){.   
26f34 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
26f35 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 53  solveLabel(v, iS
26f36 6b 69 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  kip);.    }.  }.
26f37 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
26f38 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
26f39 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 2c   when inserting,
26f3a 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 75 70 64   deleting or upd
26f3b 61 74 69 6e 67 20 61 20 72 6f 77 20 6f 66 0a 2a  ating a row of.*
26f3c 2a 20 74 61 62 6c 65 20 70 54 61 62 20 74 6f 20  * table pTab to 
26f3d 67 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f  generate VDBE co
26f3e 64 65 20 74 6f 20 70 65 72 66 6f 72 6d 20 66 6f  de to perform fo
26f3f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
26f40 61 69 6e 74 20 0a 2a 2a 20 70 72 6f 63 65 73 73  aint .** process
26f41 69 6e 67 20 66 6f 72 20 74 68 65 20 6f 70 65 72  ing for the oper
26f42 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  ation..**.** For
26f43 20 61 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74   a DELETE operat
26f44 69 6f 6e 2c 20 70 61 72 61 6d 65 74 65 72 20 72  ion, parameter r
26f45 65 67 4f 6c 64 20 69 73 20 70 61 73 73 65 64 20  egOld is passed 
26f46 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
26f47 0a 2a 2a 20 66 69 72 73 74 20 72 65 67 69 73 74  .** first regist
26f48 65 72 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f  er in an array o
26f49 66 20 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29  f (pTab->nCol+1)
26f4a 20 72 65 67 69 73 74 65 72 73 20 63 6f 6e 74 61   registers conta
26f4b 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 72 6f 77  ining the.** row
26f4c 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20 62 65  id of the row be
26f4d 69 6e 67 20 64 65 6c 65 74 65 64 2c 20 66 6f 6c  ing deleted, fol
26f4e 6c 6f 77 65 64 20 62 79 20 65 61 63 68 20 6f 66  lowed by each of
26f4f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75   the column valu
26f50 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 72 6f 77  es.** of the row
26f51 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2c 20   being deleted, 
26f52 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67  from left to rig
26f53 68 74 2e 20 50 61 72 61 6d 65 74 65 72 20 72 65  ht. Parameter re
26f54 67 4e 65 77 20 69 73 20 70 61 73 73 65 64 0a 2a  gNew is passed.*
26f55 2a 20 7a 65 72 6f 20 69 6e 20 74 68 69 73 20 63  * zero in this c
26f56 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61  ase..**.** For a
26f57 6e 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69  n INSERT operati
26f58 6f 6e 2c 20 72 65 67 4f 6c 64 20 69 73 20 70 61  on, regOld is pa
26f59 73 73 65 64 20 7a 65 72 6f 20 61 6e 64 20 72 65  ssed zero and re
26f5a 67 4e 65 77 20 69 73 20 70 61 73 73 65 64 20 74  gNew is passed t
26f5b 68 65 0a 2a 2a 20 66 69 72 73 74 20 72 65 67 69  he.** first regi
26f5c 73 74 65 72 20 6f 66 20 61 6e 20 61 72 72 61 79  ster of an array
26f5d 20 6f 66 20 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b   of (pTab->nCol+
26f5e 31 29 20 72 65 67 69 73 74 65 72 73 20 63 6f 6e  1) registers con
26f5f 74 61 69 6e 69 6e 67 20 74 68 65 20 6e 65 77 0a  taining the new.
26f60 2a 2a 20 72 6f 77 20 64 61 74 61 2e 0a 2a 2a 0a  ** row data..**.
26f61 2a 2a 20 46 6f 72 20 61 6e 20 55 50 44 41 54 45  ** For an UPDATE
26f62 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 73   operation, this
26f63 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
26f64 6c 65 64 20 74 77 69 63 65 2e 20 4f 6e 63 65 20  led twice. Once 
26f65 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6f 72  before.** the or
26f66 69 67 69 6e 61 6c 20 72 65 63 6f 72 64 20 69 73  iginal record is
26f67 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 74 68   deleted from th
26f68 65 20 74 61 62 6c 65 20 75 73 69 6e 67 20 74 68  e table using th
26f69 65 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e  e calling conven
26f6a 74 69 6f 6e 0a 2a 2a 20 64 65 73 63 72 69 62 65  tion.** describe
26f6b 64 20 66 6f 72 20 44 45 4c 45 54 45 2e 20 54 68  d for DELETE. Th
26f6c 65 6e 20 61 67 61 69 6e 20 61 66 74 65 72 20 74  en again after t
26f6d 68 65 20 6f 72 69 67 69 6e 61 6c 20 72 65 63 6f  he original reco
26f6e 72 64 20 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a  rd is deleted.**
26f6f 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 65 20   but before the 
26f70 6e 65 77 20 72 65 63 6f 72 64 20 69 73 20 69 6e  new record is in
26f71 73 65 72 74 65 64 20 75 73 69 6e 67 20 74 68 65  serted using the
26f72 20 49 4e 53 45 52 54 20 63 6f 6e 76 65 6e 74 69   INSERT conventi
26f73 6f 6e 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  on. .*/.SQLITE_P
26f74 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
26f75 74 65 33 46 6b 43 68 65 63 6b 28 0a 20 20 50 61  te3FkCheck(.  Pa
26f76 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
26f77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26f78 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
26f79 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
26f7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f7b 20 20 20 20 2f 2a 20 52 6f 77 20 69 73 20 62 65      /* Row is be
26f7c 69 6e 67 20 64 65 6c 65 74 65 64 20 66 72 6f 6d  ing deleted from
26f7d 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 20 0a   this table */ .
26f7e 20 20 69 6e 74 20 72 65 67 4f 6c 64 2c 20 20 20    int regOld,   
26f7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f80 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 72 6f    /* Previous ro
26f81 77 20 64 61 74 61 20 69 73 20 73 74 6f 72 65 64  w data is stored
26f82 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72   here */.  int r
26f83 65 67 4e 65 77 20 20 20 20 20 20 20 20 20 20 20  egNew           
26f84 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
26f85 77 20 72 6f 77 20 64 61 74 61 20 69 73 20 73 74  w row data is st
26f86 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ored here */.){.
26f87 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
26f88 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
26f89 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
26f8a 6e 64 6c 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a  ndle */.  Vdbe *
26f8b 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
26f8c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20            /* VM 
26f8d 74 6f 20 77 72 69 74 65 20 63 6f 64 65 20 74 6f  to write code to
26f8e 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65   */.  FKey *pFKe
26f8f 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
26f90 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
26f91 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
26f92 20 46 4b 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44   FKs */.  int iD
26f93 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
26f94 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
26f95 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 63  ex of database c
26f96 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20 2a  ontaining pTab *
26f97 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
26f98 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  zDb;            
26f99 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64      /* Name of d
26f9a 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
26f9b 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74  ng pTab */.  int
26f9c 20 69 73 49 67 6e 6f 72 65 45 72 72 6f 72 73 20   isIgnoreErrors 
26f9d 3d 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  = pParse->disabl
26f9e 65 54 72 69 67 67 65 72 73 3b 0a 0a 20 20 2f 2a  eTriggers;..  /*
26f9f 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20   Exactly one of 
26fa0 72 65 67 4f 6c 64 20 61 6e 64 20 72 65 67 4e 65  regOld and regNe
26fa1 77 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 6e 2d  w should be non-
26fa2 7a 65 72 6f 2e 20 2a 2f 0a 20 20 61 73 73 65 72  zero. */.  asser
26fa3 74 28 20 28 72 65 67 4f 6c 64 3d 3d 30 29 21 3d  t( (regOld==0)!=
26fa4 28 72 65 67 4e 65 77 3d 3d 30 29 20 29 3b 0a 0a  (regNew==0) );..
26fa5 20 20 2f 2a 20 49 66 20 66 6f 72 65 69 67 6e 2d    /* If foreign-
26fa6 6b 65 79 73 20 61 72 65 20 64 69 73 61 62 6c 65  keys are disable
26fa7 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  d, this function
26fa8 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a   is a no-op. */.
26fa9 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73    if( (db->flags
26faa 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b  &SQLITE_ForeignK
26fab 65 79 73 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  eys)==0 ) return
26fac 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  ;..  v = sqlite3
26fad 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
26fae 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
26faf 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
26fb0 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
26fb1 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ;.  zDb = db->aD
26fb2 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20  b[iDb].zName;.. 
26fb3 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
26fb4 20 61 6c 6c 20 74 68 65 20 66 6f 72 65 69 67 6e   all the foreign
26fb5 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73   key constraints
26fb6 20 66 6f 72 20 77 68 69 63 68 20 70 54 61 62 20   for which pTab 
26fb7 69 73 20 74 68 65 0a 20 20 2a 2a 20 63 68 69 6c  is the.  ** chil
26fb8 64 20 74 61 62 6c 65 20 28 74 68 65 20 74 61 62  d table (the tab
26fb9 6c 65 20 74 68 61 74 20 74 68 65 20 66 6f 72 65  le that the fore
26fba 69 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69  ign key definiti
26fbb 6f 6e 20 69 73 20 70 61 72 74 20 6f 66 29 2e 20  on is part of). 
26fbc 20 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d   */.  for(pFKey=
26fbd 70 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 46 4b  pTab->pFKey; pFK
26fbe 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65 79 2d  ey; pFKey=pFKey-
26fbf 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20  >pNextFrom){.   
26fc0 20 54 61 62 6c 65 20 2a 70 54 6f 3b 20 20 20 20   Table *pTo;    
26fc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26fc2 2a 20 50 61 72 65 6e 74 20 74 61 62 6c 65 20 6f  * Parent table o
26fc3 66 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 46  f foreign key pF
26fc4 4b 65 79 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78  Key */.    Index
26fc5 20 2a 70 49 64 78 20 3d 20 30 3b 20 20 20 20 20   *pIdx = 0;     
26fc6 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
26fc7 78 20 6f 6e 20 6b 65 79 20 63 6f 6c 75 6d 6e 73  x on key columns
26fc8 20 69 6e 20 70 54 6f 20 2a 2f 0a 20 20 20 20 69   in pTo */.    i
26fc9 6e 74 20 2a 61 69 46 72 65 65 20 3d 20 30 3b 0a  nt *aiFree = 0;.
26fca 20 20 20 20 69 6e 74 20 2a 61 69 43 6f 6c 3b 0a      int *aiCol;.
26fcb 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20      int iCol;.  
26fcc 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74    int i;.    int
26fcd 20 69 73 49 67 6e 6f 72 65 20 3d 20 30 3b 0a 0a   isIgnore = 0;..
26fce 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20      /* Find the 
26fcf 70 61 72 65 6e 74 20 74 61 62 6c 65 20 6f 66 20  parent table of 
26fd0 74 68 69 73 20 66 6f 72 65 69 67 6e 20 6b 65 79  this foreign key
26fd1 2e 20 41 6c 73 6f 20 66 69 6e 64 20 61 20 75 6e  . Also find a un
26fd2 69 71 75 65 20 69 6e 64 65 78 20 0a 20 20 20 20  ique index .    
26fd3 2a 2a 20 6f 6e 20 74 68 65 20 70 61 72 65 6e 74  ** on the parent
26fd4 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20   key columns in 
26fd5 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
26fd6 2e 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 74  . If either of t
26fd7 68 65 73 65 20 0a 20 20 20 20 2a 2a 20 73 63 68  hese .    ** sch
26fd8 65 6d 61 20 69 74 65 6d 73 20 63 61 6e 6e 6f 74  ema items cannot
26fd9 20 62 65 20 6c 6f 63 61 74 65 64 2c 20 73 65 74   be located, set
26fda 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 70 50 61   an error in pPa
26fdb 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 0a  rse and return .
26fdc 20 20 20 20 2a 2a 20 65 61 72 6c 79 2e 20 20 2a      ** early.  *
26fdd 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65  /.    if( pParse
26fde 2d 3e 64 69 73 61 62 6c 65 54 72 69 67 67 65 72  ->disableTrigger
26fdf 73 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 20 3d  s ){.      pTo =
26fe0 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
26fe1 65 28 64 62 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f  e(db, pFKey->zTo
26fe2 2c 20 7a 44 62 29 3b 0a 20 20 20 20 7d 65 6c 73  , zDb);.    }els
26fe3 65 7b 0a 20 20 20 20 20 20 70 54 6f 20 3d 20 73  e{.      pTo = s
26fe4 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
26fe5 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 4b  e(pParse, 0, pFK
26fe6 65 79 2d 3e 7a 54 6f 2c 20 7a 44 62 29 3b 0a 20  ey->zTo, zDb);. 
26fe7 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 54     }.    if( !pT
26fe8 6f 20 7c 7c 20 6c 6f 63 61 74 65 46 6b 65 79 49  o || locateFkeyI
26fe9 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 54 6f  ndex(pParse, pTo
26fea 2c 20 70 46 4b 65 79 2c 20 26 70 49 64 78 2c 20  , pFKey, &pIdx, 
26feb 26 61 69 46 72 65 65 29 20 29 7b 0a 20 20 20 20  &aiFree) ){.    
26fec 20 20 69 66 28 20 21 69 73 49 67 6e 6f 72 65 45    if( !isIgnoreE
26fed 72 72 6f 72 73 20 7c 7c 20 64 62 2d 3e 6d 61 6c  rrors || db->mal
26fee 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
26fef 72 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e  rn;.      contin
26ff0 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  ue;.    }.    as
26ff1 73 65 72 74 28 20 70 46 4b 65 79 2d 3e 6e 43 6f  sert( pFKey->nCo
26ff2 6c 3d 3d 31 20 7c 7c 20 28 61 69 46 72 65 65 20  l==1 || (aiFree 
26ff3 26 26 20 70 49 64 78 29 20 29 3b 0a 0a 20 20 20  && pIdx) );..   
26ff4 20 69 66 28 20 61 69 46 72 65 65 20 29 7b 0a 20   if( aiFree ){. 
26ff5 20 20 20 20 20 61 69 43 6f 6c 20 3d 20 61 69 46       aiCol = aiF
26ff6 72 65 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ree;.    }else{.
26ff7 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 70 46 4b        iCol = pFK
26ff8 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f  ey->aCol[0].iFro
26ff9 6d 3b 0a 20 20 20 20 20 20 61 69 43 6f 6c 20 3d  m;.      aiCol =
26ffa 20 26 69 43 6f 6c 3b 0a 20 20 20 20 7d 0a 20 20   &iCol;.    }.  
26ffb 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 4b    for(i=0; i<pFK
26ffc 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ey->nCol; i++){.
26ffd 20 20 20 20 20 20 69 66 28 20 61 69 43 6f 6c 5b        if( aiCol[
26ffe 69 5d 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20  i]==pTab->iPKey 
26fff 29 7b 0a 20 20 20 20 20 20 20 20 61 69 43 6f 6c  ){.        aiCol
27000 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  [i] = -1;.      
27001 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
27002 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
27003 49 4f 4e 0a 20 20 20 20 20 20 2f 2a 20 52 65 71  ION.      /* Req
27004 75 65 73 74 20 70 65 72 6d 69 73 73 69 6f 6e 20  uest permission 
27005 74 6f 20 72 65 61 64 20 74 68 65 20 70 61 72 65  to read the pare
27006 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e 20  nt key columns. 
27007 49 66 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a  If the .      **
27008 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
27009 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
2700a 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c 20 62  SQLITE_IGNORE, b
2700b 65 68 61 76 65 20 61 73 20 69 66 20 61 6e 79 0a  ehave as if any.
2700c 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20        ** values 
2700d 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 70 61  read from the pa
2700e 72 65 6e 74 20 74 61 62 6c 65 20 61 72 65 20 4e  rent table are N
2700f 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  ULL. */.      if
27010 28 20 64 62 2d 3e 78 41 75 74 68 20 29 7b 0a 20  ( db->xAuth ){. 
27011 20 20 20 20 20 20 20 69 6e 74 20 72 63 61 75 74         int rcaut
27012 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  h;.        char 
27013 2a 7a 43 6f 6c 20 3d 20 70 54 6f 2d 3e 61 43 6f  *zCol = pTo->aCo
27014 6c 5b 70 49 64 78 20 3f 20 70 49 64 78 2d 3e 61  l[pIdx ? pIdx->a
27015 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3a 20 70 54 6f  iColumn[i] : pTo
27016 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b 0a  ->iPKey].zName;.
27017 20 20 20 20 20 20 20 20 72 63 61 75 74 68 20 3d          rcauth =
27018 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64   sqlite3AuthRead
27019 43 6f 6c 28 70 50 61 72 73 65 2c 20 70 54 6f 2d  Col(pParse, pTo-
2701a 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 2c 20 69 44  >zName, zCol, iD
2701b 62 29 3b 0a 20 20 20 20 20 20 20 20 69 73 49 67  b);.        isIg
2701c 6e 6f 72 65 20 3d 20 28 72 63 61 75 74 68 3d 3d  nore = (rcauth==
2701d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 29 3b 0a  SQLITE_IGNORE);.
2701e 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
2701f 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 61 6b     }..    /* Tak
27020 65 20 61 20 73 68 61 72 65 64 2d 63 61 63 68 65  e a shared-cache
27021 20 61 64 76 69 73 6f 72 79 20 72 65 61 64 2d 6c   advisory read-l
27022 6f 63 6b 20 6f 6e 20 74 68 65 20 70 61 72 65 6e  ock on the paren
27023 74 20 74 61 62 6c 65 2e 20 41 6c 6c 6f 63 61 74  t table. Allocat
27024 65 20 0a 20 20 20 20 2a 2a 20 61 20 63 75 72 73  e .    ** a curs
27025 6f 72 20 74 6f 20 75 73 65 20 74 6f 20 73 65 61  or to use to sea
27026 72 63 68 20 74 68 65 20 75 6e 69 71 75 65 20 69  rch the unique i
27027 6e 64 65 78 20 6f 6e 20 74 68 65 20 70 61 72 65  ndex on the pare
27028 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 0a  nt key columns .
27029 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 70 61      ** in the pa
2702a 72 65 6e 74 20 74 61 62 6c 65 2e 20 20 2a 2f 0a  rent table.  */.
2702b 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65      sqlite3Table
2702c 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62  Lock(pParse, iDb
2702d 2c 20 70 54 6f 2d 3e 74 6e 75 6d 2c 20 30 2c 20  , pTo->tnum, 0, 
2702e 70 54 6f 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  pTo->zName);.   
2702f 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
27030 0a 0a 20 20 20 20 69 66 28 20 72 65 67 4f 6c 64  ..    if( regOld
27031 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  !=0 ){.      /* 
27032 41 20 72 6f 77 20 69 73 20 62 65 69 6e 67 20 72  A row is being r
27033 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20  emoved from the 
27034 63 68 69 6c 64 20 74 61 62 6c 65 2e 20 53 65 61  child table. Sea
27035 72 63 68 20 66 6f 72 20 74 68 65 20 70 61 72 65  rch for the pare
27036 6e 74 2e 0a 20 20 20 20 20 20 2a 2a 20 49 66 20  nt..      ** If 
27037 74 68 65 20 70 61 72 65 6e 74 20 64 6f 65 73 20  the parent does 
27038 6e 6f 74 20 65 78 69 73 74 2c 20 72 65 6d 6f 76  not exist, remov
27039 69 6e 67 20 74 68 65 20 63 68 69 6c 64 20 72 6f  ing the child ro
2703a 77 20 72 65 73 6f 6c 76 65 73 20 61 6e 20 0a 20  w resolves an . 
2703b 20 20 20 20 20 2a 2a 20 6f 75 74 73 74 61 6e 64       ** outstand
2703c 69 6e 67 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ing foreign key 
2703d 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
2703e 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 66  tion. */.      f
2703f 6b 4c 6f 6f 6b 75 70 50 61 72 65 6e 74 28 70 50  kLookupParent(pP
27040 61 72 73 65 2c 20 69 44 62 2c 20 70 54 6f 2c 20  arse, iDb, pTo, 
27041 70 49 64 78 2c 20 70 46 4b 65 79 2c 20 61 69 43  pIdx, pFKey, aiC
27042 6f 6c 2c 20 72 65 67 4f 6c 64 2c 20 2d 31 2c 69  ol, regOld, -1,i
27043 73 49 67 6e 6f 72 65 29 3b 0a 20 20 20 20 7d 0a  sIgnore);.    }.
27044 20 20 20 20 69 66 28 20 72 65 67 4e 65 77 21 3d      if( regNew!=
27045 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  0 ){.      /* A 
27046 72 6f 77 20 69 73 20 62 65 69 6e 67 20 61 64 64  row is being add
27047 65 64 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20  ed to the child 
27048 74 61 62 6c 65 2e 20 49 66 20 61 20 70 61 72 65  table. If a pare
27049 6e 74 20 72 6f 77 20 63 61 6e 6e 6f 74 0a 20 20  nt row cannot.  
2704a 20 20 20 20 2a 2a 20 62 65 20 66 6f 75 6e 64 2c      ** be found,
2704b 20 61 64 64 69 6e 67 20 74 68 65 20 63 68 69 6c   adding the chil
2704c 64 20 72 6f 77 20 68 61 73 20 76 69 6f 6c 61 74  d row has violat
2704d 65 64 20 74 68 65 20 46 4b 20 63 6f 6e 73 74 72  ed the FK constr
2704e 61 69 6e 74 2e 20 2a 2f 20 0a 20 20 20 20 20 20  aint. */ .      
2704f 66 6b 4c 6f 6f 6b 75 70 50 61 72 65 6e 74 28 70  fkLookupParent(p
27050 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 6f 2c  Parse, iDb, pTo,
27051 20 70 49 64 78 2c 20 70 46 4b 65 79 2c 20 61 69   pIdx, pFKey, ai
27052 43 6f 6c 2c 20 72 65 67 4e 65 77 2c 20 2b 31 2c  Col, regNew, +1,
27053 69 73 49 67 6e 6f 72 65 29 3b 0a 20 20 20 20 7d  isIgnore);.    }
27054 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ..    sqlite3DbF
27055 72 65 65 28 64 62 2c 20 61 69 46 72 65 65 29 3b  ree(db, aiFree);
27056 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20  .  }..  /* Loop 
27057 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 20  through all the 
27058 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
27059 74 72 61 69 6e 74 73 20 74 68 61 74 20 72 65 66  traints that ref
2705a 65 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65  er to this table
2705b 20 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65 79 20   */.  for(pFKey 
2705c 3d 20 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72  = sqlite3FkRefer
2705d 65 6e 63 65 73 28 70 54 61 62 29 3b 20 70 46 4b  ences(pTab); pFK
2705e 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65 79 2d  ey; pFKey=pFKey-
2705f 3e 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20 49  >pNextTo){.    I
27060 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b 20  ndex *pIdx = 0; 
27061 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27062 46 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 64 65  Foreign key inde
27063 78 20 66 6f 72 20 70 46 4b 65 79 20 2a 2f 0a 20  x for pFKey */. 
27064 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
27065 3b 0a 20 20 20 20 69 6e 74 20 2a 61 69 43 6f 6c  ;.    int *aiCol
27066 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 21   = 0;..    if( !
27067 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65  pFKey->isDeferre
27068 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54  d && !pParse->pT
27069 6f 70 6c 65 76 65 6c 20 26 26 20 21 70 50 61 72  oplevel && !pPar
2706a 73 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65  se->isMultiWrite
2706b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
2706c 28 20 72 65 67 4f 6c 64 3d 3d 30 20 26 26 20 72  ( regOld==0 && r
2706d 65 67 4e 65 77 21 3d 30 20 29 3b 0a 20 20 20 20  egNew!=0 );.    
2706e 20 20 2f 2a 20 49 6e 73 65 72 74 69 6e 67 20 61    /* Inserting a
2706f 20 73 69 6e 67 6c 65 20 72 6f 77 20 69 6e 74 6f   single row into
27070 20 61 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20   a parent table 
27071 63 61 6e 6e 6f 74 20 63 61 75 73 65 20 61 6e 20  cannot cause an 
27072 69 6d 6d 65 64 69 61 74 65 0a 20 20 20 20 20 20  immediate.      
27073 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76  ** foreign key v
27074 69 6f 6c 61 74 69 6f 6e 2e 20 53 6f 20 64 6f 20  iolation. So do 
27075 6e 6f 74 68 69 6e 67 20 69 6e 20 74 68 69 73 20  nothing in this 
27076 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20  case.  */.      
27077 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a  continue;.    }.
27078 0a 20 20 20 20 69 66 28 20 6c 6f 63 61 74 65 46  .    if( locateF
27079 6b 65 79 49 6e 64 65 78 28 70 50 61 72 73 65 2c  keyIndex(pParse,
2707a 20 70 54 61 62 2c 20 70 46 4b 65 79 2c 20 26 70   pTab, pFKey, &p
2707b 49 64 78 2c 20 26 61 69 43 6f 6c 29 20 29 7b 0a  Idx, &aiCol) ){.
2707c 20 20 20 20 20 20 69 66 28 20 21 69 73 49 67 6e        if( !isIgn
2707d 6f 72 65 45 72 72 6f 72 73 20 7c 7c 20 64 62 2d  oreErrors || db-
2707e 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
2707f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 63 6f  return;.      co
27080 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20  ntinue;.    }.  
27081 20 20 61 73 73 65 72 74 28 20 61 69 43 6f 6c 20    assert( aiCol 
27082 7c 7c 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3d 3d  || pFKey->nCol==
27083 31 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65  1 );..    /* Cre
27084 61 74 65 20 61 20 53 72 63 4c 69 73 74 20 73 74  ate a SrcList st
27085 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69  ructure containi
27086 6e 67 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  ng a single tabl
27087 65 20 28 74 68 65 20 74 61 62 6c 65 20 0a 20 20  e (the table .  
27088 20 20 2a 2a 20 74 68 65 20 66 6f 72 65 69 67 6e    ** the foreign
27089 20 6b 65 79 20 74 68 61 74 20 72 65 66 65 72 73   key that refers
2708a 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20 69   to this table i
2708b 73 20 61 74 74 61 63 68 65 64 20 74 6f 29 2e 20  s attached to). 
2708c 54 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 72  This.    ** is r
2708d 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20  equired for the 
2708e 73 71 6c 69 74 65 33 57 68 65 72 65 58 58 58 28  sqlite3WhereXXX(
2708f 29 20 69 6e 74 65 72 66 61 63 65 2e 20 20 2a 2f  ) interface.  */
27090 0a 20 20 20 20 70 53 72 63 20 3d 20 73 71 6c 69  .    pSrc = sqli
27091 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
27092 28 64 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  (db, 0, 0, 0);. 
27093 20 20 20 69 66 28 20 70 53 72 63 20 29 7b 0a 20     if( pSrc ){. 
27094 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c       struct SrcL
27095 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
27096 3d 20 70 53 72 63 2d 3e 61 3b 0a 20 20 20 20 20  = pSrc->a;.     
27097 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70   pItem->pTab = p
27098 46 4b 65 79 2d 3e 70 46 72 6f 6d 3b 0a 20 20 20  FKey->pFrom;.   
27099 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20     pItem->zName 
2709a 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e  = pFKey->pFrom->
2709b 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 70 49 74  zName;.      pIt
2709c 65 6d 2d 3e 70 54 61 62 2d 3e 6e 52 65 66 2b 2b  em->pTab->nRef++
2709d 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69  ;.      pItem->i
2709e 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d  Cursor = pParse-
2709f 3e 6e 54 61 62 2b 2b 3b 0a 20 20 0a 20 20 20 20  >nTab++;.  .    
270a0 20 20 69 66 28 20 72 65 67 4e 65 77 21 3d 30 20    if( regNew!=0 
270a1 29 7b 0a 20 20 20 20 20 20 20 20 66 6b 53 63 61  ){.        fkSca
270a2 6e 43 68 69 6c 64 72 65 6e 28 70 50 61 72 73 65  nChildren(pParse
270a3 2c 20 70 53 72 63 2c 20 70 54 61 62 2c 20 70 49  , pSrc, pTab, pI
270a4 64 78 2c 20 70 46 4b 65 79 2c 20 61 69 43 6f 6c  dx, pFKey, aiCol
270a5 2c 20 72 65 67 4e 65 77 2c 20 2d 31 29 3b 0a 20  , regNew, -1);. 
270a6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
270a7 20 72 65 67 4f 6c 64 21 3d 30 20 29 7b 0a 20 20   regOld!=0 ){.  
270a8 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72        /* If ther
270a9 65 20 69 73 20 61 20 52 45 53 54 52 49 43 54 20  e is a RESTRICT 
270aa 61 63 74 69 6f 6e 20 63 6f 6e 66 69 67 75 72 65  action configure
270ab 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  d for the curren
270ac 74 20 6f 70 65 72 61 74 69 6f 6e 0a 20 20 20 20  t operation.    
270ad 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 70 61      ** on the pa
270ae 72 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 74 68  rent table of th
270af 69 73 20 46 4b 2c 20 74 68 65 6e 20 74 68 72 6f  is FK, then thro
270b0 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 0a  w an exception .
270b1 20 20 20 20 20 20 20 20 2a 2a 20 69 6d 6d 65 64          ** immed
270b2 69 61 74 65 6c 79 20 69 66 20 74 68 65 20 46 4b  iately if the FK
270b3 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 76   constraint is v
270b4 69 6f 6c 61 74 65 64 2c 20 65 76 65 6e 20 69 66  iolated, even if
270b5 20 74 68 69 73 20 69 73 20 61 0a 20 20 20 20 20   this is a.     
270b6 20 20 20 2a 2a 20 64 65 66 65 72 72 65 64 20 74     ** deferred t
270b7 72 69 67 67 65 72 2e 20 54 68 61 74 27 73 20 77  rigger. That's w
270b8 68 61 74 20 52 45 53 54 52 49 43 54 20 6d 65 61  hat RESTRICT mea
270b9 6e 73 2e 20 54 6f 20 64 65 66 65 72 20 63 68 65  ns. To defer che
270ba 63 6b 69 6e 67 0a 20 20 20 20 20 20 20 20 2a 2a  cking.        **
270bb 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 2c   the constraint,
270bc 20 74 68 65 20 46 4b 20 73 68 6f 75 6c 64 20 73   the FK should s
270bd 70 65 63 69 66 79 20 4e 4f 20 41 43 54 49 4f 4e  pecify NO ACTION
270be 20 28 72 65 70 72 65 73 65 6e 74 65 64 0a 20 20   (represented.  
270bf 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 4f        ** using O
270c0 45 5f 4e 6f 6e 65 29 2e 20 4e 4f 20 41 43 54 49  E_None). NO ACTI
270c1 4f 4e 20 69 73 20 74 68 65 20 64 65 66 61 75 6c  ON is the defaul
270c2 74 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66  t.  */.        f
270c3 6b 53 63 61 6e 43 68 69 6c 64 72 65 6e 28 70 50  kScanChildren(pP
270c4 61 72 73 65 2c 20 70 53 72 63 2c 20 70 54 61 62  arse, pSrc, pTab
270c5 2c 20 70 49 64 78 2c 20 70 46 4b 65 79 2c 20 61  , pIdx, pFKey, a
270c6 69 43 6f 6c 2c 20 72 65 67 4f 6c 64 2c 20 31 29  iCol, regOld, 1)
270c7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
270c8 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 30  pItem->zName = 0
270c9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ;.      sqlite3S
270ca 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
270cb 20 70 53 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20   pSrc);.    }.  
270cc 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
270cd 64 62 2c 20 61 69 43 6f 6c 29 3b 0a 20 20 7d 0a  db, aiCol);.  }.
270ce 7d 0a 0a 23 64 65 66 69 6e 65 20 43 4f 4c 55 4d  }..#define COLUM
270cf 4e 5f 4d 41 53 4b 28 78 29 20 28 28 28 78 29 3e  N_MASK(x) (((x)>
270d0 33 31 29 20 3f 20 30 78 66 66 66 66 66 66 66 66  31) ? 0xffffffff
270d1 20 3a 20 28 28 75 33 32 29 31 3c 3c 28 78 29 29   : ((u32)1<<(x))
270d2 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  )../*.** This fu
270d3 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
270d4 20 62 65 66 6f 72 65 20 67 65 6e 65 72 61 74 69   before generati
270d5 6e 67 20 63 6f 64 65 20 74 6f 20 75 70 64 61 74  ng code to updat
270d6 65 20 6f 72 20 64 65 6c 65 74 65 20 61 20 0a 2a  e or delete a .*
270d7 2a 20 72 6f 77 20 63 6f 6e 74 61 69 6e 65 64 20  * row contained 
270d8 69 6e 20 74 61 62 6c 65 20 70 54 61 62 2e 0a 2a  in table pTab..*
270d9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
270da 20 75 33 32 20 73 71 6c 69 74 65 33 46 6b 4f 6c   u32 sqlite3FkOl
270db 64 6d 61 73 6b 28 0a 20 20 50 61 72 73 65 20 2a  dmask(.  Parse *
270dc 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
270dd 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
270de 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  e context */.  T
270df 61 62 6c 65 20 2a 70 54 61 62 20 20 20 20 20 20  able *pTab      
270e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
270e1 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 6d 6f  * Table being mo
270e2 64 69 66 69 65 64 20 2a 2f 0a 29 7b 0a 20 20 75  dified */.){.  u
270e3 33 32 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 69  32 mask = 0;.  i
270e4 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66  f( pParse->db->f
270e5 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65  lags&SQLITE_Fore
270e6 69 67 6e 4b 65 79 73 20 29 7b 0a 20 20 20 20 46  ignKeys ){.    F
270e7 4b 65 79 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20  Key *p;.    int 
270e8 69 3b 0a 20 20 20 20 66 6f 72 28 70 3d 70 54 61  i;.    for(p=pTa
270e9 62 2d 3e 70 46 4b 65 79 3b 20 70 3b 20 70 3d 70  b->pFKey; p; p=p
270ea 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20  ->pNextFrom){.  
270eb 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
270ec 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 20 6d 61 73  ->nCol; i++) mas
270ed 6b 20 7c 3d 20 43 4f 4c 55 4d 4e 5f 4d 41 53 4b  k |= COLUMN_MASK
270ee 28 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f  (p->aCol[i].iFro
270ef 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  m);.    }.    fo
270f0 72 28 70 3d 73 71 6c 69 74 65 33 46 6b 52 65 66  r(p=sqlite3FkRef
270f1 65 72 65 6e 63 65 73 28 70 54 61 62 29 3b 20 70  erences(pTab); p
270f2 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 54 6f 29 7b  ; p=p->pNextTo){
270f3 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49  .      Index *pI
270f4 64 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 6c 6f  dx = 0;.      lo
270f5 63 61 74 65 46 6b 65 79 49 6e 64 65 78 28 70 50  cateFkeyIndex(pP
270f6 61 72 73 65 2c 20 70 54 61 62 2c 20 70 2c 20 26  arse, pTab, p, &
270f7 70 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 20 20  pIdx, 0);.      
270f8 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20  if( pIdx ){.    
270f9 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
270fa 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Idx->nColumn; i+
270fb 2b 29 20 6d 61 73 6b 20 7c 3d 20 43 4f 4c 55 4d  +) mask |= COLUM
270fc 4e 5f 4d 41 53 4b 28 70 49 64 78 2d 3e 61 69 43  N_MASK(pIdx->aiC
270fd 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20 20 20  olumn[i]);.     
270fe 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
270ff 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 2f  eturn mask;.}../
27100 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
27101 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 65 66  on is called bef
27102 6f 72 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63  ore generating c
27103 6f 64 65 20 74 6f 20 75 70 64 61 74 65 20 6f 72  ode to update or
27104 20 64 65 6c 65 74 65 20 61 20 0a 2a 2a 20 72 6f   delete a .** ro
27105 77 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  w contained in t
27106 61 62 6c 65 20 70 54 61 62 2e 20 49 66 20 74 68  able pTab. If th
27107 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61  e operation is a
27108 20 44 45 4c 45 54 45 2c 20 74 68 65 6e 0a 2a 2a   DELETE, then.**
27109 20 70 61 72 61 6d 65 74 65 72 20 61 43 68 61 6e   parameter aChan
2710a 67 65 20 69 73 20 70 61 73 73 65 64 20 61 20 4e  ge is passed a N
2710b 55 4c 4c 20 76 61 6c 75 65 2e 20 46 6f 72 20 61  ULL value. For a
2710c 6e 20 55 50 44 41 54 45 2c 20 61 43 68 61 6e 67  n UPDATE, aChang
2710d 65 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 61  e points.** to a
2710e 6e 20 61 72 72 61 79 20 6f 66 20 73 69 7a 65 20  n array of size 
2710f 4e 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  N, where N is th
27110 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
27111 6d 6e 73 20 69 6e 20 74 61 62 6c 65 20 70 54 61  mns in table pTa
27112 62 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 27 74  b..** If the i't
27113 68 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20  h column is not 
27114 6d 6f 64 69 66 69 65 64 20 62 79 20 74 68 65 20  modified by the 
27115 55 50 44 41 54 45 2c 20 74 68 65 6e 20 74 68 65  UPDATE, then the
27116 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 0a   corresponding .
27117 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  ** entry in the 
27118 61 43 68 61 6e 67 65 5b 5d 20 61 72 72 61 79 20  aChange[] array 
27119 69 73 20 73 65 74 20 74 6f 20 2d 31 2e 20 49 66  is set to -1. If
2711a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 6d   the column is m
2711b 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 74 68 65 20  odified,.** the 
2711c 76 61 6c 75 65 20 69 73 20 30 20 6f 72 20 67 72  value is 0 or gr
2711d 65 61 74 65 72 2e 20 50 61 72 61 6d 65 74 65 72  eater. Parameter
2711e 20 63 68 6e 67 52 6f 77 69 64 20 69 73 20 73 65   chngRowid is se
2711f 74 20 74 6f 20 74 72 75 65 20 69 66 20 74 68 65  t to true if the
27120 0a 2a 2a 20 55 50 44 41 54 45 20 73 74 61 74 65  .** UPDATE state
27121 6d 65 6e 74 20 6d 6f 64 69 66 69 65 73 20 74 68  ment modifies th
27122 65 20 72 6f 77 69 64 20 66 69 65 6c 64 73 20 6f  e rowid fields o
27123 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  f the table..**.
27124 2a 2a 20 49 66 20 61 6e 79 20 66 6f 72 65 69 67  ** If any foreig
27125 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69 6e 67  n key processing
27126 20 77 69 6c 6c 20 62 65 20 72 65 71 75 69 72 65   will be require
27127 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  d, this function
27128 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 72 75 65   returns.** true
27129 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  . If there is no
2712a 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 72 65 6c   foreign key rel
2712b 61 74 65 64 20 70 72 6f 63 65 73 73 69 6e 67 2c  ated processing,
2712c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a   this function .
2712d 2a 2a 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65  ** returns false
2712e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2712f 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
27130 6b 52 65 71 75 69 72 65 64 28 0a 20 20 50 61 72  kRequired(.  Par
27131 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
27132 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27133 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  Parse context */
27134 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
27135 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27136 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e     /* Table bein
27137 67 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20 20  g modified */.  
27138 69 6e 74 20 2a 61 43 68 61 6e 67 65 2c 20 20 20  int *aChange,   
27139 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2713a 2f 2a 20 4e 6f 6e 2d 4e 55 4c 4c 20 66 6f 72 20  /* Non-NULL for 
2713b 55 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e  UPDATE operation
2713c 73 20 2a 2f 0a 20 20 69 6e 74 20 63 68 6e 67 52  s */.  int chngR
2713d 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20  owid            
2713e 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66         /* True f
2713f 6f 72 20 55 50 44 41 54 45 20 74 68 61 74 20 61  or UPDATE that a
27140 66 66 65 63 74 73 20 72 6f 77 69 64 20 2a 2f 0a  ffects rowid */.
27141 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ){.  if( pParse-
27142 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  >db->flags&SQLIT
27143 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b  E_ForeignKeys ){
27144 0a 20 20 20 20 69 66 28 20 21 61 43 68 61 6e 67  .    if( !aChang
27145 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  e ){.      /* A 
27146 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e  DELETE operation
27147 2e 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 70 72  . Foreign key pr
27148 6f 63 65 73 73 69 6e 67 20 69 73 20 72 65 71 75  ocessing is requ
27149 69 72 65 64 20 69 66 20 74 68 65 20 0a 20 20 20  ired if the .   
2714a 20 20 20 2a 2a 20 74 61 62 6c 65 20 69 6e 20 71     ** table in q
2714b 75 65 73 74 69 6f 6e 20 69 73 20 65 69 74 68 65  uestion is eithe
2714c 72 20 74 68 65 20 63 68 69 6c 64 20 6f 72 20 70  r the child or p
2714d 61 72 65 6e 74 20 74 61 62 6c 65 20 66 6f 72 20  arent table for 
2714e 61 6e 79 20 0a 20 20 20 20 20 20 2a 2a 20 66 6f  any .      ** fo
2714f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
27150 61 69 6e 74 2e 20 20 2a 2f 0a 20 20 20 20 20 20  aint.  */.      
27151 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 46  return (sqlite3F
27152 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54 61 62  kReferences(pTab
27153 29 20 7c 7c 20 70 54 61 62 2d 3e 70 46 4b 65 79  ) || pTab->pFKey
27154 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
27155 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61      /* This is a
27156 6e 20 55 50 44 41 54 45 2e 20 46 6f 72 65 69 67  n UPDATE. Foreig
27157 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69 6e 67  n key processing
27158 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65   is only require
27159 64 20 69 66 20 74 68 65 0a 20 20 20 20 20 20 2a  d if the.      *
2715a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6d 6f 64 69  * operation modi
2715b 66 69 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  fies one or more
2715c 20 63 68 69 6c 64 20 6f 72 20 70 61 72 65 6e 74   child or parent
2715d 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e 20 2a 2f   key columns. */
2715e 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
2715f 20 20 20 20 46 4b 65 79 20 2a 70 3b 0a 0a 20 20      FKey *p;..  
27160 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20      /* Check if 
27161 61 6e 79 20 63 68 69 6c 64 20 6b 65 79 20 63 6f  any child key co
27162 6c 75 6d 6e 73 20 61 72 65 20 62 65 69 6e 67 20  lumns are being 
27163 6d 6f 64 69 66 69 65 64 2e 20 2a 2f 0a 20 20 20  modified. */.   
27164 20 20 20 66 6f 72 28 70 3d 70 54 61 62 2d 3e 70     for(p=pTab->p
27165 46 4b 65 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  FKey; p; p=p->pN
27166 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20  extFrom){.      
27167 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
27168 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
27169 20 20 20 20 20 20 69 6e 74 20 69 43 68 69 6c 64        int iChild
2716a 4b 65 79 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d  Key = p->aCol[i]
2716b 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20  .iFrom;.        
2716c 20 20 69 66 28 20 61 43 68 61 6e 67 65 5b 69 43    if( aChange[iC
2716d 68 69 6c 64 4b 65 79 5d 3e 3d 30 20 29 20 72 65  hildKey]>=0 ) re
2716e 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20  turn 1;.        
2716f 20 20 69 66 28 20 69 43 68 69 6c 64 4b 65 79 3d    if( iChildKey=
27170 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 26 26 20  =pTab->iPKey && 
27171 63 68 6e 67 52 6f 77 69 64 20 29 20 72 65 74 75  chngRowid ) retu
27172 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rn 1;.        }.
27173 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
27174 2a 20 43 68 65 63 6b 20 69 66 20 61 6e 79 20 70  * Check if any p
27175 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
27176 73 20 61 72 65 20 62 65 69 6e 67 20 6d 6f 64 69  s are being modi
27177 66 69 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 66  fied. */.      f
27178 6f 72 28 70 3d 73 71 6c 69 74 65 33 46 6b 52 65  or(p=sqlite3FkRe
27179 66 65 72 65 6e 63 65 73 28 70 54 61 62 29 3b 20  ferences(pTab); 
2717a 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 54 6f 29  p; p=p->pNextTo)
2717b 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  {.        for(i=
2717c 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b  0; i<p->nCol; i+
2717d 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68  +){.          ch
2717e 61 72 20 2a 7a 4b 65 79 20 3d 20 70 2d 3e 61 43  ar *zKey = p->aC
2717f 6f 6c 5b 69 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20  ol[i].zCol;.    
27180 20 20 20 20 20 20 69 6e 74 20 69 4b 65 79 3b 0a        int iKey;.
27181 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 4b            for(iK
27182 65 79 3d 30 3b 20 69 4b 65 79 3c 70 54 61 62 2d  ey=0; iKey<pTab-
27183 3e 6e 43 6f 6c 3b 20 69 4b 65 79 2b 2b 29 7b 0a  >nCol; iKey++){.
27184 20 20 20 20 20 20 20 20 20 20 20 20 43 6f 6c 75              Colu
27185 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62  mn *pCol = &pTab
27186 2d 3e 61 43 6f 6c 5b 69 4b 65 79 5d 3b 0a 20 20  ->aCol[iKey];.  
27187 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28 7a            if( (z
27188 4b 65 79 20 3f 20 21 73 71 6c 69 74 65 33 53 74  Key ? !sqlite3St
27189 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d  rICmp(pCol->zNam
2718a 65 2c 20 7a 4b 65 79 29 20 3a 20 70 43 6f 6c 2d  e, zKey) : pCol-
2718b 3e 69 73 50 72 69 6d 4b 65 79 29 20 29 7b 0a 20  >isPrimKey) ){. 
2718c 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
2718d 20 61 43 68 61 6e 67 65 5b 69 4b 65 79 5d 3e 3d   aChange[iKey]>=
2718e 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
2718f 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
27190 69 4b 65 79 3d 3d 70 54 61 62 2d 3e 69 50 4b 65  iKey==pTab->iPKe
27191 79 20 26 26 20 63 68 6e 67 52 6f 77 69 64 20 29  y && chngRowid )
27192 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
27193 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
27194 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
27195 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
27196 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
27197 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
27198 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ion is called wh
27199 65 6e 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20  en an UPDATE or 
2719a 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e  DELETE operation
2719b 20 69 73 20 62 65 69 6e 67 20 0a 2a 2a 20 63 6f   is being .** co
2719c 6d 70 69 6c 65 64 20 6f 6e 20 74 61 62 6c 65 20  mpiled on table 
2719d 70 54 61 62 2c 20 77 68 69 63 68 20 69 73 20 74  pTab, which is t
2719e 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  he parent table 
2719f 6f 66 20 66 6f 72 65 69 67 6e 2d 6b 65 79 20 70  of foreign-key p
271a0 46 4b 65 79 2e 0a 2a 2a 20 49 66 20 74 68 65 20  FKey..** If the 
271a1 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f  current operatio
271a2 6e 20 69 73 20 61 6e 20 55 50 44 41 54 45 2c 20  n is an UPDATE, 
271a3 74 68 65 6e 20 74 68 65 20 70 43 68 61 6e 67 65  then the pChange
271a4 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a  s parameter is.*
271a5 2a 20 70 61 73 73 65 64 20 61 20 70 6f 69 6e 74  * passed a point
271a6 65 72 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f  er to the list o
271a7 66 20 63 6f 6c 75 6d 6e 73 20 62 65 69 6e 67 20  f columns being 
271a8 6d 6f 64 69 66 69 65 64 2e 20 49 66 20 69 74 20  modified. If it 
271a9 69 73 20 61 0a 2a 2a 20 44 45 4c 45 54 45 2c 20  is a.** DELETE, 
271aa 70 43 68 61 6e 67 65 73 20 69 73 20 70 61 73 73  pChanges is pass
271ab 65 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ed a NULL pointe
271ac 72 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 72 65 74 75  r..**.** It retu
271ad 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
271ae 20 61 20 54 72 69 67 67 65 72 20 73 74 72 75 63   a Trigger struc
271af 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ture containing 
271b0 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 65 71 75  a trigger.** equ
271b1 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65 20 4f  ivalent to the O
271b2 4e 20 55 50 44 41 54 45 20 6f 72 20 4f 4e 20 44  N UPDATE or ON D
271b3 45 4c 45 54 45 20 61 63 74 69 6f 6e 20 73 70 65  ELETE action spe
271b4 63 69 66 69 65 64 20 62 79 20 70 46 4b 65 79 2e  cified by pFKey.
271b5 0a 2a 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f  .** If the actio
271b6 6e 20 69 73 20 22 4e 4f 20 41 43 54 49 4f 4e 22  n is "NO ACTION"
271b7 20 6f 72 20 22 52 45 53 54 52 49 43 54 22 2c 20   or "RESTRICT", 
271b8 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  then a NULL poin
271b9 74 65 72 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  ter is.** return
271ba 65 64 20 28 74 68 65 73 65 20 61 63 74 69 6f 6e  ed (these action
271bb 73 20 72 65 71 75 69 72 65 20 6e 6f 20 73 70 65  s require no spe
271bc 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 20 62 79  cial handling by
271bd 20 74 68 65 20 74 72 69 67 67 65 72 73 0a 2a 2a   the triggers.**
271be 20 73 75 62 2d 73 79 73 74 65 6d 2c 20 63 6f 64   sub-system, cod
271bf 65 20 66 6f 72 20 74 68 65 6d 20 69 73 20 63 72  e for them is cr
271c0 65 61 74 65 64 20 62 79 20 66 6b 53 63 61 6e 43  eated by fkScanC
271c1 68 69 6c 64 72 65 6e 28 29 29 2e 0a 2a 2a 0a 2a  hildren())..**.*
271c2 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  * For example, i
271c3 66 20 70 46 4b 65 79 20 69 73 20 74 68 65 20 66  f pFKey is the f
271c4 6f 72 65 69 67 6e 20 6b 65 79 20 61 6e 64 20 70  oreign key and p
271c5 54 61 62 20 69 73 20 74 61 62 6c 65 20 22 70 22  Tab is table "p"
271c6 20 69 6e 20 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c   in .** the foll
271c7 6f 77 69 6e 67 20 73 63 68 65 6d 61 3a 0a 2a 2a  owing schema:.**
271c8 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42  .**   CREATE TAB
271c9 4c 45 20 70 28 70 6b 20 50 52 49 4d 41 52 59 20  LE p(pk PRIMARY 
271ca 4b 45 59 29 3b 0a 2a 2a 20 20 20 43 52 45 41 54  KEY);.**   CREAT
271cb 45 20 54 41 42 4c 45 20 63 28 63 6b 20 52 45 46  E TABLE c(ck REF
271cc 45 52 45 4e 43 45 53 20 70 20 4f 4e 20 44 45 4c  ERENCES p ON DEL
271cd 45 54 45 20 43 41 53 43 41 44 45 29 3b 0a 2a 2a  ETE CASCADE);.**
271ce 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74  .** then the ret
271cf 75 72 6e 65 64 20 74 72 69 67 67 65 72 20 73 74  urned trigger st
271d0 72 75 63 74 75 72 65 20 69 73 20 65 71 75 69 76  ructure is equiv
271d1 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20  alent to:.**.** 
271d2 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52    CREATE TRIGGER
271d3 20 2e 2e 2e 20 44 45 4c 45 54 45 20 4f 4e 20 70   ... DELETE ON p
271d4 20 42 45 47 49 4e 0a 2a 2a 20 20 20 20 20 44 45   BEGIN.**     DE
271d5 4c 45 54 45 20 46 52 4f 4d 20 63 20 57 48 45 52  LETE FROM c WHER
271d6 45 20 63 6b 20 3d 20 6f 6c 64 2e 70 6b 3b 0a 2a  E ck = old.pk;.*
271d7 2a 20 20 20 45 4e 44 3b 0a 2a 2a 0a 2a 2a 20 54  *   END;.**.** T
271d8 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 6e  he returned poin
271d9 74 65 72 20 69 73 20 63 61 63 68 65 64 20 61 73  ter is cached as
271da 20 70 61 72 74 20 6f 66 20 74 68 65 20 66 6f 72   part of the for
271db 65 69 67 6e 20 6b 65 79 20 6f 62 6a 65 63 74 2e  eign key object.
271dc 20 49 74 0a 2a 2a 20 69 73 20 65 76 65 6e 74 75   It.** is eventu
271dd 61 6c 6c 79 20 66 72 65 65 64 20 61 6c 6f 6e 67  ally freed along
271de 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20 6f   with the rest o
271df 66 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65  f the foreign ke
271e0 79 20 6f 62 6a 65 63 74 20 62 79 20 0a 2a 2a 20  y object by .** 
271e1 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28  sqlite3FkDelete(
271e2 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 72 69  )..*/.static Tri
271e3 67 67 65 72 20 2a 66 6b 41 63 74 69 6f 6e 54 72  gger *fkActionTr
271e4 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a  igger(.  Parse *
271e5 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
271e6 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
271e7 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  e context */.  T
271e8 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
271e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
271ea 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 75 70  * Table being up
271eb 64 61 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64  dated or deleted
271ec 20 66 72 6f 6d 20 2a 2f 0a 20 20 46 4b 65 79 20   from */.  FKey 
271ed 2a 70 46 4b 65 79 2c 20 20 20 20 20 20 20 20 20  *pFKey,         
271ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
271ef 72 65 69 67 6e 20 6b 65 79 20 74 6f 20 67 65 74  reign key to get
271f0 20 61 63 74 69 6f 6e 20 66 6f 72 20 2a 2f 0a 20   action for */. 
271f1 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e   ExprList *pChan
271f2 67 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ges             
271f3 20 2f 2a 20 43 68 61 6e 67 65 2d 6c 69 73 74 20   /* Change-list 
271f4 66 6f 72 20 55 50 44 41 54 45 2c 20 4e 55 4c 4c  for UPDATE, NULL
271f5 20 66 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a 29   for DELETE */.)
271f6 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
271f7 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
271f8 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
271f9 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  handle */.  int 
271fa 61 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20  action;         
271fb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
271fc 6e 65 20 6f 66 20 4f 45 5f 4e 6f 6e 65 2c 20 4f  ne of OE_None, O
271fd 45 5f 43 61 73 63 61 64 65 20 65 74 63 2e 20 2a  E_Cascade etc. *
271fe 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  /.  Trigger *pTr
271ff 69 67 67 65 72 3b 20 20 20 20 20 20 20 20 20 20  igger;          
27200 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 64      /* Trigger d
27201 65 66 69 6e 69 74 69 6f 6e 20 74 6f 20 72 65 74  efinition to ret
27202 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 63  urn */.  int iAc
27203 74 69 6f 6e 20 3d 20 28 70 43 68 61 6e 67 65 73  tion = (pChanges
27204 21 3d 30 29 3b 20 20 20 20 2f 2a 20 31 20 66 6f  !=0);    /* 1 fo
27205 72 20 55 50 44 41 54 45 2c 20 30 20 66 6f 72 20  r UPDATE, 0 for 
27206 44 45 4c 45 54 45 20 2a 2f 0a 0a 20 20 61 63 74  DELETE */..  act
27207 69 6f 6e 20 3d 20 70 46 4b 65 79 2d 3e 61 41 63  ion = pFKey->aAc
27208 74 69 6f 6e 5b 69 41 63 74 69 6f 6e 5d 3b 0a 20  tion[iAction];. 
27209 20 70 54 72 69 67 67 65 72 20 3d 20 70 46 4b 65   pTrigger = pFKe
2720a 79 2d 3e 61 70 54 72 69 67 67 65 72 5b 69 41 63  y->apTrigger[iAc
2720b 74 69 6f 6e 5d 3b 0a 0a 20 20 69 66 28 20 61 63  tion];..  if( ac
2720c 74 69 6f 6e 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26  tion!=OE_None &&
2720d 20 21 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20   !pTrigger ){.  
2720e 20 20 75 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61    u8 enableLooka
2720f 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  side;           
27210 2f 2a 20 43 6f 70 79 20 6f 66 20 64 62 2d 3e 6c  /* Copy of db->l
27211 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
27212 64 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 63 6f  d */.    char co
27213 6e 73 74 20 2a 7a 46 72 6f 6d 3b 20 20 20 20 20  nst *zFrom;     
27214 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
27215 66 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f  f child table */
27216 0a 20 20 20 20 69 6e 74 20 6e 46 72 6f 6d 3b 20  .    int nFrom; 
27217 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27218 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 69 6e 20     /* Length in 
27219 62 79 74 65 73 20 6f 66 20 7a 46 72 6f 6d 20 2a  bytes of zFrom *
2721a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  /.    Index *pId
2721b 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  x = 0;          
2721c 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 6b 65      /* Parent ke
2721d 79 20 69 6e 64 65 78 20 66 6f 72 20 74 68 69 73  y index for this
2721e 20 46 4b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a   FK */.    int *
2721f 61 69 43 6f 6c 20 3d 20 30 3b 20 20 20 20 20 20  aiCol = 0;      
27220 20 20 20 20 20 20 20 20 20 2f 2a 20 63 68 69 6c           /* chil
27221 64 20 74 61 62 6c 65 20 63 6f 6c 73 20 2d 3e 20  d table cols -> 
27222 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 73 20  parent key cols 
27223 2a 2f 0a 20 20 20 20 54 72 69 67 67 65 72 53 74  */.    TriggerSt
27224 65 70 20 2a 70 53 74 65 70 20 3d 20 30 3b 20 20  ep *pStep = 0;  
27225 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 28        /* First (
27226 6f 6e 6c 79 29 20 73 74 65 70 20 6f 66 20 74 72  only) step of tr
27227 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f  igger program */
27228 0a 20 20 20 20 45 78 70 72 20 2a 70 57 68 65 72  .    Expr *pWher
27229 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
2722a 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75     /* WHERE clau
2722b 73 65 20 6f 66 20 74 72 69 67 67 65 72 20 73 74  se of trigger st
2722c 65 70 20 2a 2f 0a 20 20 20 20 45 78 70 72 4c 69  ep */.    ExprLi
2722d 73 74 20 2a 70 4c 69 73 74 20 3d 20 30 3b 20 20  st *pList = 0;  
2722e 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67          /* Chang
2722f 65 73 20 6c 69 73 74 20 69 66 20 4f 4e 20 55 50  es list if ON UP
27230 44 41 54 45 20 43 41 53 43 41 44 45 20 2a 2f 0a  DATE CASCADE */.
27231 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c      Select *pSel
27232 65 63 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ect = 0;        
27233 20 20 2f 2a 20 49 66 20 52 45 53 54 52 49 43 54    /* If RESTRICT
27234 2c 20 22 53 45 4c 45 43 54 20 52 41 49 53 45 28  , "SELECT RAISE(
27235 2e 2e 2e 29 22 20 2a 2f 0a 20 20 20 20 69 6e 74  ...)" */.    int
27236 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
27237 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
27238 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
27239 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 57 68  */.    Expr *pWh
2723a 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  en = 0;         
2723b 20 20 20 20 20 2f 2a 20 57 48 45 4e 20 63 6c 61       /* WHEN cla
2723c 75 73 65 20 66 6f 72 20 74 68 65 20 74 72 69 67  use for the trig
2723d 67 65 72 20 2a 2f 0a 0a 20 20 20 20 69 66 28 20  ger */..    if( 
2723e 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64 65 78 28  locateFkeyIndex(
2723f 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 46  pParse, pTab, pF
27240 4b 65 79 2c 20 26 70 49 64 78 2c 20 26 61 69 43  Key, &pIdx, &aiC
27241 6f 6c 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ol) ) return 0;.
27242 20 20 20 20 61 73 73 65 72 74 28 20 61 69 43 6f      assert( aiCo
27243 6c 20 7c 7c 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c  l || pFKey->nCol
27244 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28  ==1 );..    for(
27245 69 3d 30 3b 20 69 3c 70 46 4b 65 79 2d 3e 6e 43  i=0; i<pFKey->nC
27246 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
27247 54 6f 6b 65 6e 20 74 4f 6c 64 20 3d 20 7b 20 22  Token tOld = { "
27248 6f 6c 64 22 2c 20 33 20 7d 3b 20 20 2f 2a 20 4c  old", 3 };  /* L
27249 69 74 65 72 61 6c 20 22 6f 6c 64 22 20 74 6f 6b  iteral "old" tok
2724a 65 6e 20 2a 2f 0a 20 20 20 20 20 20 54 6f 6b 65  en */.      Toke
2724b 6e 20 74 4e 65 77 20 3d 20 7b 20 22 6e 65 77 22  n tNew = { "new"
2724c 2c 20 33 20 7d 3b 20 20 2f 2a 20 4c 69 74 65 72  , 3 };  /* Liter
2724d 61 6c 20 22 6e 65 77 22 20 74 6f 6b 65 6e 20 2a  al "new" token *
2724e 2f 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 74 46  /.      Token tF
2724f 72 6f 6d 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  romCol;         
27250 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63      /* Name of c
27251 6f 6c 75 6d 6e 20 69 6e 20 63 68 69 6c 64 20 74  olumn in child t
27252 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 54 6f  able */.      To
27253 6b 65 6e 20 74 54 6f 43 6f 6c 3b 20 20 20 20 20  ken tToCol;     
27254 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
27255 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70  e of column in p
27256 61 72 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20  arent table */. 
27257 20 20 20 20 20 69 6e 74 20 69 46 72 6f 6d 43 6f       int iFromCo
27258 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
27259 20 2f 2a 20 49 64 78 20 6f 66 20 63 6f 6c 75 6d   /* Idx of colum
2725a 6e 20 69 6e 20 63 68 69 6c 64 20 74 61 62 6c 65  n in child table
2725b 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
2725c 70 45 71 3b 20 20 20 20 20 20 20 20 20 20 20 20  pEq;            
2725d 20 20 20 20 20 20 2f 2a 20 74 46 72 6f 6d 43 6f        /* tFromCo
2725e 6c 20 3d 20 4f 4c 44 2e 74 54 6f 43 6f 6c 20 2a  l = OLD.tToCol *
2725f 2f 0a 0a 20 20 20 20 20 20 69 46 72 6f 6d 43 6f  /..      iFromCo
27260 6c 20 3d 20 61 69 43 6f 6c 20 3f 20 61 69 43 6f  l = aiCol ? aiCo
27261 6c 5b 69 5d 20 3a 20 70 46 4b 65 79 2d 3e 61 43  l[i] : pFKey->aC
27262 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20  ol[0].iFrom;.   
27263 20 20 20 61 73 73 65 72 74 28 20 69 46 72 6f 6d     assert( iFrom
27264 43 6f 6c 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20  Col>=0 );.      
27265 74 54 6f 43 6f 6c 2e 7a 20 3d 20 70 49 64 78 20  tToCol.z = pIdx 
27266 3f 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64  ? pTab->aCol[pId
27267 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 2e  x->aiColumn[i]].
27268 7a 4e 61 6d 65 20 3a 20 22 6f 69 64 22 3b 0a 20  zName : "oid";. 
27269 20 20 20 20 20 74 46 72 6f 6d 43 6f 6c 2e 7a 20       tFromCol.z 
2726a 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e  = pFKey->pFrom->
2726b 61 43 6f 6c 5b 69 46 72 6f 6d 43 6f 6c 5d 2e 7a  aCol[iFromCol].z
2726c 4e 61 6d 65 3b 0a 0a 20 20 20 20 20 20 74 54 6f  Name;..      tTo
2726d 43 6f 6c 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53  Col.n = sqlite3S
2726e 74 72 6c 65 6e 33 30 28 74 54 6f 43 6f 6c 2e 7a  trlen30(tToCol.z
2726f 29 3b 0a 20 20 20 20 20 20 74 46 72 6f 6d 43 6f  );.      tFromCo
27270 6c 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  l.n = sqlite3Str
27271 6c 65 6e 33 30 28 74 46 72 6f 6d 43 6f 6c 2e 7a  len30(tFromCol.z
27272 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72 65  );..      /* Cre
27273 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73 69  ate the expressi
27274 6f 6e 20 22 4f 4c 44 2e 7a 54 6f 43 6f 6c 20 3d  on "OLD.zToCol =
27275 20 7a 46 72 6f 6d 43 6f 6c 22 2e 20 49 74 20 69   zFromCol". It i
27276 73 20 69 6d 70 6f 72 74 61 6e 74 0a 20 20 20 20  s important.    
27277 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 22 4f    ** that the "O
27278 4c 44 2e 7a 54 6f 43 6f 6c 22 20 74 65 72 6d 20  LD.zToCol" term 
27279 69 73 20 6f 6e 20 74 68 65 20 4c 48 53 20 6f 66  is on the LHS of
2727a 20 74 68 65 20 3d 20 6f 70 65 72 61 74 6f 72 2c   the = operator,
2727b 20 73 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 61   so.      ** tha
2727c 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 61  t the affinity a
2727d 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  nd collation seq
2727e 75 65 6e 63 65 20 61 73 73 6f 63 69 61 74 65 64  uence associated
2727f 20 77 69 74 68 20 74 68 65 0a 20 20 20 20 20 20   with the.      
27280 2a 2a 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  ** parent table 
27281 61 72 65 20 75 73 65 64 20 66 6f 72 20 74 68 65  are used for the
27282 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 2a 2f 0a   comparison. */.
27283 20 20 20 20 20 20 70 45 71 20 3d 20 73 71 6c 69        pEq = sqli
27284 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
27285 20 54 4b 5f 45 51 2c 0a 20 20 20 20 20 20 20 20   TK_EQ,.        
27286 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70    sqlite3PExpr(p
27287 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 0a  Parse, TK_DOT, .
27288 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
27289 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2728a 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74   TK_ID, 0, 0, &t
2728b 4f 6c 64 29 2c 0a 20 20 20 20 20 20 20 20 20 20  Old),.          
2728c 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70    sqlite3PExpr(p
2728d 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
2728e 20 30 2c 20 26 74 54 6f 43 6f 6c 29 0a 20 20 20   0, &tToCol).   
2728f 20 20 20 20 20 20 20 2c 20 30 29 2c 0a 20 20 20         , 0),.   
27290 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
27291 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
27292 44 2c 20 30 2c 20 30 2c 20 26 74 46 72 6f 6d 43  D, 0, 0, &tFromC
27293 6f 6c 29 0a 20 20 20 20 20 20 2c 20 30 29 3b 0a  ol).      , 0);.
27294 20 20 20 20 20 20 70 57 68 65 72 65 20 3d 20 73        pWhere = s
27295 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64 62  qlite3ExprAnd(db
27296 2c 20 70 57 68 65 72 65 2c 20 70 45 71 29 3b 0a  , pWhere, pEq);.
27297 0a 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4f 4e  .      /* For ON
27298 20 55 50 44 41 54 45 2c 20 63 6f 6e 73 74 72 75   UPDATE, constru
27299 63 74 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d  ct the next term
2729a 20 6f 66 20 74 68 65 20 57 48 45 4e 20 63 6c 61   of the WHEN cla
2729b 75 73 65 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68  use..      ** Th
2729c 65 20 66 69 6e 61 6c 20 57 48 45 4e 20 63 6c 61  e final WHEN cla
2729d 75 73 65 20 77 69 6c 6c 20 62 65 20 6c 69 6b 65  use will be like
2729e 20 74 68 69 73 3a 0a 20 20 20 20 20 20 2a 2a 0a   this:.      **.
2729f 20 20 20 20 20 20 2a 2a 20 20 20 20 57 48 45 4e        **    WHEN
272a0 20 4e 4f 54 28 6f 6c 64 2e 63 6f 6c 31 20 49 53   NOT(old.col1 IS
272a1 20 6e 65 77 2e 63 6f 6c 31 20 41 4e 44 20 2e 2e   new.col1 AND ..
272a2 2e 20 41 4e 44 20 6f 6c 64 2e 63 6f 6c 4e 20 49  . AND old.colN I
272a3 53 20 6e 65 77 2e 63 6f 6c 4e 29 0a 20 20 20 20  S new.colN).    
272a4 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70    */.      if( p
272a5 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20  Changes ){.     
272a6 20 20 20 70 45 71 20 3d 20 73 71 6c 69 74 65 33     pEq = sqlite3
272a7 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
272a8 5f 49 53 2c 0a 20 20 20 20 20 20 20 20 20 20 20  _IS,.           
272a9 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
272aa 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 0a 20  arse, TK_DOT, . 
272ab 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
272ac 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
272ad 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
272ae 74 4f 6c 64 29 2c 0a 20 20 20 20 20 20 20 20 20  tOld),.         
272af 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70       sqlite3PExp
272b0 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
272b1 20 30 2c 20 30 2c 20 26 74 54 6f 43 6f 6c 29 2c   0, 0, &tToCol),
272b2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30  .              0
272b3 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ),.            s
272b4 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
272b5 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 0a 20 20 20  se, TK_DOT, .   
272b6 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
272b7 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
272b8 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 4e  TK_ID, 0, 0, &tN
272b9 65 77 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ew),.           
272ba 20 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28     sqlite3PExpr(
272bb 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
272bc 2c 20 30 2c 20 26 74 54 6f 43 6f 6c 29 2c 0a 20  , 0, &tToCol),. 
272bd 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29 2c               0),
272be 0a 20 20 20 20 20 20 20 20 20 20 20 20 30 29 3b  .            0);
272bf 0a 20 20 20 20 20 20 20 20 70 57 68 65 6e 20 3d  .        pWhen =
272c0 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28   sqlite3ExprAnd(
272c1 64 62 2c 20 70 57 68 65 6e 2c 20 70 45 71 29 3b  db, pWhen, pEq);
272c2 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  .      }.  .    
272c3 20 20 69 66 28 20 61 63 74 69 6f 6e 21 3d 4f 45    if( action!=OE
272c4 5f 52 65 73 74 72 69 63 74 20 26 26 20 28 61 63  _Restrict && (ac
272c5 74 69 6f 6e 21 3d 4f 45 5f 43 61 73 63 61 64 65  tion!=OE_Cascade
272c6 20 7c 7c 20 70 43 68 61 6e 67 65 73 29 20 29 7b   || pChanges) ){
272c7 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70  .        Expr *p
272c8 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 69 66 28  New;.        if(
272c9 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f 43 61 73 63   action==OE_Casc
272ca 61 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ade ){.         
272cb 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 50   pNew = sqlite3P
272cc 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
272cd 44 4f 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20  DOT, .          
272ce 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70    sqlite3PExpr(p
272cf 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
272d0 20 30 2c 20 26 74 4e 65 77 29 2c 0a 20 20 20 20   0, &tNew),.    
272d1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
272d2 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
272d3 49 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f 43 6f  ID, 0, 0, &tToCo
272d4 6c 29 0a 20 20 20 20 20 20 20 20 20 20 2c 20 30  l).          , 0
272d5 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
272d6 20 69 66 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f   if( action==OE_
272d7 53 65 74 44 66 6c 74 20 29 7b 0a 20 20 20 20 20  SetDflt ){.     
272d8 20 20 20 20 20 45 78 70 72 20 2a 70 44 66 6c 74       Expr *pDflt
272d9 20 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d   = pFKey->pFrom-
272da 3e 61 43 6f 6c 5b 69 46 72 6f 6d 43 6f 6c 5d 2e  >aCol[iFromCol].
272db 70 44 66 6c 74 3b 0a 20 20 20 20 20 20 20 20 20  pDflt;.         
272dc 20 69 66 28 20 70 44 66 6c 74 20 29 7b 0a 20 20   if( pDflt ){.  
272dd 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d            pNew =
272de 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
272df 64 62 2c 20 70 44 66 6c 74 2c 20 30 29 3b 0a 20  db, pDflt, 0);. 
272e0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
272e1 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 77              pNew
272e2 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
272e3 70 50 61 72 73 65 2c 20 54 4b 5f 4e 55 4c 4c 2c  pParse, TK_NULL,
272e4 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20   0, 0, 0);.     
272e5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
272e6 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
272e7 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 50 45  pNew = sqlite3PE
272e8 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
272e9 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ULL, 0, 0, 0);. 
272ea 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
272eb 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
272ec 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
272ed 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 70 4e  Parse, pList, pN
272ee 65 77 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ew);.        sql
272ef 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e  ite3ExprListSetN
272f0 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73  ame(pParse, pLis
272f1 74 2c 20 26 74 46 72 6f 6d 43 6f 6c 2c 20 30 29  t, &tFromCol, 0)
272f2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
272f3 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
272f4 65 28 64 62 2c 20 61 69 43 6f 6c 29 3b 0a 0a 20  e(db, aiCol);.. 
272f5 20 20 20 7a 46 72 6f 6d 20 3d 20 70 46 4b 65 79     zFrom = pFKey
272f6 2d 3e 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3b 0a  ->pFrom->zName;.
272f7 20 20 20 20 6e 46 72 6f 6d 20 3d 20 73 71 6c 69      nFrom = sqli
272f8 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 72 6f  te3Strlen30(zFro
272f9 6d 29 3b 0a 0a 20 20 20 20 69 66 28 20 61 63 74  m);..    if( act
272fa 69 6f 6e 3d 3d 4f 45 5f 52 65 73 74 72 69 63 74  ion==OE_Restrict
272fb 20 29 7b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20   ){.      Token 
272fc 74 46 72 6f 6d 3b 0a 20 20 20 20 20 20 45 78 70  tFrom;.      Exp
272fd 72 20 2a 70 52 61 69 73 65 3b 20 0a 0a 20 20 20  r *pRaise; ..   
272fe 20 20 20 74 46 72 6f 6d 2e 7a 20 3d 20 7a 46 72     tFrom.z = zFr
272ff 6f 6d 3b 0a 20 20 20 20 20 20 74 46 72 6f 6d 2e  om;.      tFrom.
27300 6e 20 3d 20 6e 46 72 6f 6d 3b 0a 20 20 20 20 20  n = nFrom;.     
27301 20 70 52 61 69 73 65 20 3d 20 73 71 6c 69 74 65   pRaise = sqlite
27302 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 52 41 49  3Expr(db, TK_RAI
27303 53 45 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65 79  SE, "foreign key
27304 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
27305 65 64 22 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ed");.      if( 
27306 70 52 61 69 73 65 20 29 7b 0a 20 20 20 20 20 20  pRaise ){.      
27307 20 20 70 52 61 69 73 65 2d 3e 61 66 66 69 6e 69    pRaise->affini
27308 74 79 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20  ty = OE_Abort;. 
27309 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 53 65       }.      pSe
2730a 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
2730b 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 20  lectNew(pParse, 
2730c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2730d 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2730e 28 70 50 61 72 73 65 2c 20 30 2c 20 70 52 61 69  (pParse, 0, pRai
2730f 73 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 73  se),.          s
27310 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
27311 65 6e 64 28 64 62 2c 20 30 2c 20 26 74 46 72 6f  end(db, 0, &tFro
27312 6d 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20  m, 0),.         
27313 20 70 57 68 65 72 65 2c 0a 20 20 20 20 20 20 20   pWhere,.       
27314 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30     0, 0, 0, 0, 0
27315 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  , 0.      );.   
27316 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20     pWhere = 0;. 
27317 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 20     }..    /* In 
27318 74 68 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c  the current impl
27319 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 70 54 61 62  ementation, pTab
2731a 2d 3e 64 62 4d 65 6d 3d 3d 30 20 66 6f 72 20 61  ->dbMem==0 for a
2731b 6c 6c 20 74 61 62 6c 65 73 20 65 78 63 65 70 74  ll tables except
2731c 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74 65 6d 70  .    ** for temp
2731d 6f 72 61 72 79 20 74 61 62 6c 65 73 20 75 73 65  orary tables use
2731e 64 20 74 6f 20 64 65 73 63 72 69 62 65 20 73 75  d to describe su
2731f 62 71 75 65 72 69 65 73 2e 20 20 41 6e 64 20 74  bqueries.  And t
27320 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 2a 2a 20  emporary.    ** 
27321 74 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 68 61  tables do not ha
27322 76 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  ve foreign key c
27323 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 48 65 6e  onstraints.  Hen
27324 63 65 2c 20 70 54 61 62 2d 3e 64 62 4d 65 6d 0a  ce, pTab->dbMem.
27325 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 61 6c      ** should al
27326 77 61 79 73 20 62 65 20 30 20 74 68 65 72 65 2e  ways be 0 there.
27327 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 6e 61 62  .    */.    enab
27328 6c 65 4c 6f 6f 6b 61 73 69 64 65 20 3d 20 64 62  leLookaside = db
27329 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
2732a 62 6c 65 64 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f  bled;.    db->lo
2732b 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
2732c 20 3d 20 30 3b 0a 0a 20 20 20 20 70 54 72 69 67   = 0;..    pTrig
2732d 67 65 72 20 3d 20 28 54 72 69 67 67 65 72 20 2a  ger = (Trigger *
2732e 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  )sqlite3DbMalloc
2732f 5a 65 72 6f 28 64 62 2c 20 0a 20 20 20 20 20 20  Zero(db, .      
27330 20 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72    sizeof(Trigger
27331 29 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 73  ) +         /* s
27332 74 72 75 63 74 20 54 72 69 67 67 65 72 20 2a 2f  truct Trigger */
27333 0a 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28  .        sizeof(
27334 54 72 69 67 67 65 72 53 74 65 70 29 20 2b 20 20  TriggerStep) +  
27335 20 20 20 2f 2a 20 53 69 6e 67 6c 65 20 73 74 65     /* Single ste
27336 70 20 69 6e 20 74 72 69 67 67 65 72 20 70 72 6f  p in trigger pro
27337 67 72 61 6d 20 2a 2f 0a 20 20 20 20 20 20 20 20  gram */.        
27338 6e 46 72 6f 6d 20 2b 20 31 20 20 20 20 20 20 20  nFrom + 1       
27339 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
2733a 63 65 20 66 6f 72 20 70 53 74 65 70 2d 3e 74 61  ce for pStep->ta
2733b 72 67 65 74 2e 7a 20 2a 2f 0a 20 20 20 20 29 3b  rget.z */.    );
2733c 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67 65  .    if( pTrigge
2733d 72 20 29 7b 0a 20 20 20 20 20 20 70 53 74 65 70  r ){.      pStep
2733e 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 73 74 65   = pTrigger->ste
2733f 70 5f 6c 69 73 74 20 3d 20 28 54 72 69 67 67 65  p_list = (Trigge
27340 72 53 74 65 70 20 2a 29 26 70 54 72 69 67 67 65  rStep *)&pTrigge
27341 72 5b 31 5d 3b 0a 20 20 20 20 20 20 70 53 74 65  r[1];.      pSte
27342 70 2d 3e 74 61 72 67 65 74 2e 7a 20 3d 20 28 63  p->target.z = (c
27343 68 61 72 20 2a 29 26 70 53 74 65 70 5b 31 5d 3b  har *)&pStep[1];
27344 0a 20 20 20 20 20 20 70 53 74 65 70 2d 3e 74 61  .      pStep->ta
27345 72 67 65 74 2e 6e 20 3d 20 6e 46 72 6f 6d 3b 0a  rget.n = nFrom;.
27346 20 20 20 20 20 20 6d 65 6d 63 70 79 28 28 63 68        memcpy((ch
27347 61 72 20 2a 29 70 53 74 65 70 2d 3e 74 61 72 67  ar *)pStep->targ
27348 65 74 2e 7a 2c 20 7a 46 72 6f 6d 2c 20 6e 46 72  et.z, zFrom, nFr
27349 6f 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 70 53  om);.  .      pS
2734a 74 65 70 2d 3e 70 57 68 65 72 65 20 3d 20 73 71  tep->pWhere = sq
2734b 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2734c 20 70 57 68 65 72 65 2c 20 45 58 50 52 44 55 50   pWhere, EXPRDUP
2734d 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 20 20  _REDUCE);.      
2734e 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74  pStep->pExprList
2734f 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
27350 73 74 44 75 70 28 64 62 2c 20 70 4c 69 73 74 2c  stDup(db, pList,
27351 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
27352 3b 0a 20 20 20 20 20 20 70 53 74 65 70 2d 3e 70  ;.      pStep->p
27353 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
27354 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53  SelectDup(db, pS
27355 65 6c 65 63 74 2c 20 45 58 50 52 44 55 50 5f 52  elect, EXPRDUP_R
27356 45 44 55 43 45 29 3b 0a 20 20 20 20 20 20 69 66  EDUCE);.      if
27357 28 20 70 57 68 65 6e 20 29 7b 0a 20 20 20 20 20  ( pWhen ){.     
27358 20 20 20 70 57 68 65 6e 20 3d 20 73 71 6c 69 74     pWhen = sqlit
27359 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2735a 54 4b 5f 4e 4f 54 2c 20 70 57 68 65 6e 2c 20 30  TK_NOT, pWhen, 0
2735b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 54  , 0);.        pT
2735c 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 20 3d 20  rigger->pWhen = 
2735d 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2735e 62 2c 20 70 57 68 65 6e 2c 20 45 58 50 52 44 55  b, pWhen, EXPRDU
2735f 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 20  P_REDUCE);.     
27360 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
27361 20 52 65 2d 65 6e 61 62 6c 65 20 74 68 65 20 6c   Re-enable the l
27362 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 2c  ookaside buffer,
27363 20 69 66 20 69 74 20 77 61 73 20 64 69 73 61 62   if it was disab
27364 6c 65 64 20 65 61 72 6c 69 65 72 2e 20 2a 2f 0a  led earlier. */.
27365 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
27366 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61  e.bEnabled = ena
27367 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 0a 0a 20  bleLookaside;.. 
27368 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
27369 6c 65 74 65 28 64 62 2c 20 70 57 68 65 72 65 29  lete(db, pWhere)
2736a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2736b 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65  rDelete(db, pWhe
2736c 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  n);.    sqlite3E
2736d 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2736e 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 73 71  , pList);.    sq
2736f 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
27370 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  e(db, pSelect);.
27371 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
27372 6f 63 46 61 69 6c 65 64 3d 3d 31 20 29 7b 0a 20  ocFailed==1 ){. 
27373 20 20 20 20 20 66 6b 54 72 69 67 67 65 72 44 65       fkTriggerDe
27374 6c 65 74 65 28 64 62 2c 20 70 54 72 69 67 67 65  lete(db, pTrigge
27375 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  r);.      return
27376 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73   0;.    }..    s
27377 77 69 74 63 68 28 20 61 63 74 69 6f 6e 20 29 7b  witch( action ){
27378 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52  .      case OE_R
27379 65 73 74 72 69 63 74 3a 0a 20 20 20 20 20 20 20  estrict:.       
2737a 20 70 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f   pStep->op = TK_
2737b 53 45 4c 45 43 54 3b 20 0a 20 20 20 20 20 20 20  SELECT; .       
2737c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2737d 73 65 20 4f 45 5f 43 61 73 63 61 64 65 3a 20 0a  se OE_Cascade: .
2737e 20 20 20 20 20 20 20 20 69 66 28 20 21 70 43 68          if( !pCh
2737f 61 6e 67 65 73 20 29 7b 20 0a 20 20 20 20 20 20  anges ){ .      
27380 20 20 20 20 70 53 74 65 70 2d 3e 6f 70 20 3d 20      pStep->op = 
27381 54 4b 5f 44 45 4c 45 54 45 3b 20 0a 20 20 20 20  TK_DELETE; .    
27382 20 20 20 20 20 20 62 72 65 61 6b 3b 20 0a 20 20        break; .  
27383 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65        }.      de
27384 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 70  fault:.        p
27385 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 55 50  Step->op = TK_UP
27386 44 41 54 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20  DATE;.    }.    
27387 70 53 74 65 70 2d 3e 70 54 72 69 67 20 3d 20 70  pStep->pTrig = p
27388 54 72 69 67 67 65 72 3b 0a 20 20 20 20 70 54 72  Trigger;.    pTr
27389 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 20 3d  igger->pSchema =
2738a 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
2738b 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e 70 54      pTrigger->pT
2738c 61 62 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d  abSchema = pTab-
2738d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 46  >pSchema;.    pF
2738e 4b 65 79 2d 3e 61 70 54 72 69 67 67 65 72 5b 69  Key->apTrigger[i
2738f 41 63 74 69 6f 6e 5d 20 3d 20 70 54 72 69 67 67  Action] = pTrigg
27390 65 72 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  er;.    pTrigger
27391 2d 3e 6f 70 20 3d 20 28 70 43 68 61 6e 67 65 73  ->op = (pChanges
27392 20 3f 20 54 4b 5f 55 50 44 41 54 45 20 3a 20 54   ? TK_UPDATE : T
27393 4b 5f 44 45 4c 45 54 45 29 3b 0a 20 20 7d 0a 0a  K_DELETE);.  }..
27394 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67 65    return pTrigge
27395 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  r;.}../*.** This
27396 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
27397 6c 65 64 20 77 68 65 6e 20 64 65 6c 65 74 69 6e  led when deletin
27398 67 20 6f 72 20 75 70 64 61 74 69 6e 67 20 61 20  g or updating a 
27399 72 6f 77 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  row to implement
2739a 0a 2a 2a 20 61 6e 79 20 72 65 71 75 69 72 65 64  .** any required
2739b 20 43 41 53 43 41 44 45 2c 20 53 45 54 20 4e 55   CASCADE, SET NU
2739c 4c 4c 20 6f 72 20 53 45 54 20 44 45 46 41 55 4c  LL or SET DEFAUL
2739d 54 20 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  T actions..*/.SQ
2739e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2739f 64 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f  d sqlite3FkActio
273a0 6e 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ns(.  Parse *pPa
273a1 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
273a2 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63        /* Parse c
273a3 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
273a4 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
273a5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
273a6 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74  able being updat
273a7 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20 66 72  ed or deleted fr
273a8 6f 6d 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  om */.  ExprList
273a9 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 20 20 20   *pChanges,     
273aa 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67          /* Chang
273ab 65 2d 6c 69 73 74 20 66 6f 72 20 55 50 44 41 54  e-list for UPDAT
273ac 45 2c 20 4e 55 4c 4c 20 66 6f 72 20 44 45 4c 45  E, NULL for DELE
273ad 54 45 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f  TE */.  int regO
273ae 6c 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ld              
273af 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
273b0 73 73 20 6f 66 20 61 72 72 61 79 20 63 6f 6e 74  ss of array cont
273b1 61 69 6e 69 6e 67 20 6f 6c 64 20 72 6f 77 20 2a  aining old row *
273b2 2f 0a 29 7b 0a 20 20 2f 2a 20 49 66 20 66 6f 72  /.){.  /* If for
273b3 65 69 67 6e 2d 6b 65 79 20 73 75 70 70 6f 72 74  eign-key support
273b4 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 69 74 65   is enabled, ite
273b5 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c  rate through all
273b6 20 46 4b 73 20 74 68 61 74 20 0a 20 20 2a 2a 20   FKs that .  ** 
273b7 72 65 66 65 72 20 74 6f 20 74 61 62 6c 65 20 70  refer to table p
273b8 54 61 62 2e 20 49 66 20 74 68 65 72 65 20 69 73  Tab. If there is
273b9 20 61 6e 20 61 63 74 69 6f 6e 20 61 73 73 6f 63   an action assoc
273ba 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 46  iated with the F
273bb 4b 20 0a 20 20 2a 2a 20 66 6f 72 20 74 68 69 73  K .  ** for this
273bc 20 6f 70 65 72 61 74 69 6f 6e 20 28 65 69 74 68   operation (eith
273bd 65 72 20 75 70 64 61 74 65 20 6f 72 20 64 65 6c  er update or del
273be 65 74 65 29 2c 20 69 6e 76 6f 6b 65 20 74 68 65  ete), invoke the
273bf 20 61 73 73 6f 63 69 61 74 65 64 20 0a 20 20 2a   associated .  *
273c0 2a 20 74 72 69 67 67 65 72 20 73 75 62 2d 70 72  * trigger sub-pr
273c1 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 69 66 28  ogram.  */.  if(
273c2 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61   pParse->db->fla
273c3 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67  gs&SQLITE_Foreig
273c4 6e 4b 65 79 73 20 29 7b 0a 20 20 20 20 46 4b 65  nKeys ){.    FKe
273c5 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20  y *pFKey;       
273c6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
273c7 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
273c8 2a 2f 0a 20 20 20 20 66 6f 72 28 70 46 4b 65 79  */.    for(pFKey
273c9 20 3d 20 73 71 6c 69 74 65 33 46 6b 52 65 66 65   = sqlite3FkRefe
273ca 72 65 6e 63 65 73 28 70 54 61 62 29 3b 20 70 46  rences(pTab); pF
273cb 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65 79  Key; pFKey=pFKey
273cc 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20  ->pNextTo){.    
273cd 20 20 54 72 69 67 67 65 72 20 2a 70 41 63 74 69    Trigger *pActi
273ce 6f 6e 20 3d 20 66 6b 41 63 74 69 6f 6e 54 72 69  on = fkActionTri
273cf 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 54 61  gger(pParse, pTa
273d0 62 2c 20 70 46 4b 65 79 2c 20 70 43 68 61 6e 67  b, pFKey, pChang
273d1 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  es);.      if( p
273d2 41 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  Action ){.      
273d3 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77    sqlite3CodeRow
273d4 54 72 69 67 67 65 72 44 69 72 65 63 74 28 70 50  TriggerDirect(pP
273d5 61 72 73 65 2c 20 70 41 63 74 69 6f 6e 2c 20 70  arse, pAction, p
273d6 54 61 62 2c 20 72 65 67 4f 6c 64 2c 20 4f 45 5f  Tab, regOld, OE_
273d7 41 62 6f 72 74 2c 20 30 29 3b 0a 20 20 20 20 20  Abort, 0);.     
273d8 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
273d9 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65 66  #endif /* ifndef
273da 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
273db 47 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46  GGER */../*.** F
273dc 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61  ree all memory a
273dd 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 66  ssociated with f
273de 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e  oreign key defin
273df 69 74 69 6f 6e 73 20 61 74 74 61 63 68 65 64 20  itions attached 
273e0 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62  to.** table pTab
273e1 2e 20 52 65 6d 6f 76 65 20 74 68 65 20 64 65 6c  . Remove the del
273e2 65 74 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79  eted foreign key
273e3 73 20 66 72 6f 6d 20 74 68 65 20 53 63 68 65 6d  s from the Schem
273e4 61 2e 66 6b 65 79 48 61 73 68 0a 2a 2a 20 68 61  a.fkeyHash.** ha
273e5 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  sh table..*/.SQL
273e6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
273e7 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74 65   sqlite3FkDelete
273e8 28 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20  (Table *pTab){. 
273e9 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20   FKey *pFKey;   
273ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
273eb 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
273ec 69 61 62 6c 65 20 2a 2f 0a 20 20 46 4b 65 79 20  iable */.  FKey 
273ed 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
273ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
273ef 70 79 20 6f 66 20 70 46 4b 65 79 2d 3e 70 4e 65  py of pFKey->pNe
273f0 78 74 46 72 6f 6d 20 2a 2f 0a 0a 20 20 66 6f 72  xtFrom */..  for
273f1 28 70 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b  (pFKey=pTab->pFK
273f2 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79  ey; pFKey; pFKey
273f3 3d 70 4e 65 78 74 29 7b 0a 0a 20 20 20 20 2f 2a  =pNext){..    /*
273f4 20 52 65 6d 6f 76 65 20 74 68 65 20 46 4b 20 66   Remove the FK f
273f5 72 6f 6d 20 74 68 65 20 66 6b 65 79 48 61 73 68  rom the fkeyHash
273f6 20 68 61 73 68 20 74 61 62 6c 65 2e 20 2a 2f 0a   hash table. */.
273f7 20 20 20 20 69 66 28 20 70 46 4b 65 79 2d 3e 70      if( pFKey->p
273f8 50 72 65 76 54 6f 20 29 7b 0a 20 20 20 20 20 20  PrevTo ){.      
273f9 70 46 4b 65 79 2d 3e 70 50 72 65 76 54 6f 2d 3e  pFKey->pPrevTo->
273fa 70 4e 65 78 74 54 6f 20 3d 20 70 46 4b 65 79 2d  pNextTo = pFKey-
273fb 3e 70 4e 65 78 74 54 6f 3b 0a 20 20 20 20 7d 65  >pNextTo;.    }e
273fc 6c 73 65 7b 0a 20 20 20 20 20 20 76 6f 69 64 20  lse{.      void 
273fd 2a 64 61 74 61 20 3d 20 28 76 6f 69 64 20 2a 29  *data = (void *)
273fe 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 3b 0a  pFKey->pNextTo;.
273ff 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
27400 20 2a 7a 20 3d 20 28 64 61 74 61 20 3f 20 70 46   *z = (data ? pF
27401 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 2d 3e 7a 54  Key->pNextTo->zT
27402 6f 20 3a 20 70 46 4b 65 79 2d 3e 7a 54 6f 29 3b  o : pFKey->zTo);
27403 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 48 61  .      sqlite3Ha
27404 73 68 49 6e 73 65 72 74 28 26 70 54 61 62 2d 3e  shInsert(&pTab->
27405 70 53 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73  pSchema->fkeyHas
27406 68 2c 20 7a 2c 20 73 71 6c 69 74 65 33 53 74 72  h, z, sqlite3Str
27407 6c 65 6e 33 30 28 7a 29 2c 20 64 61 74 61 29 3b  len30(z), data);
27408 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
27409 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 29 7b  FKey->pNextTo ){
2740a 0a 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 70 4e  .      pFKey->pN
2740b 65 78 74 54 6f 2d 3e 70 50 72 65 76 54 6f 20 3d  extTo->pPrevTo =
2740c 20 70 46 4b 65 79 2d 3e 70 50 72 65 76 54 6f 3b   pFKey->pPrevTo;
2740d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
2740e 65 6c 65 74 65 20 61 6e 79 20 74 72 69 67 67 65  elete any trigge
2740f 72 73 20 63 72 65 61 74 65 64 20 74 6f 20 69 6d  rs created to im
27410 70 6c 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 20  plement actions 
27411 66 6f 72 20 74 68 69 73 20 46 4b 2e 20 2a 2f 0a  for this FK. */.
27412 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
27413 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20  MIT_TRIGGER.    
27414 66 6b 54 72 69 67 67 65 72 44 65 6c 65 74 65 28  fkTriggerDelete(
27415 70 54 61 62 2d 3e 64 62 4d 65 6d 2c 20 70 46 4b  pTab->dbMem, pFK
27416 65 79 2d 3e 61 70 54 72 69 67 67 65 72 5b 30 5d  ey->apTrigger[0]
27417 29 3b 0a 20 20 20 20 66 6b 54 72 69 67 67 65 72  );.    fkTrigger
27418 44 65 6c 65 74 65 28 70 54 61 62 2d 3e 64 62 4d  Delete(pTab->dbM
27419 65 6d 2c 20 70 46 4b 65 79 2d 3e 61 70 54 72 69  em, pFKey->apTri
2741a 67 67 65 72 5b 31 5d 29 3b 0a 23 65 6e 64 69 66  gger[1]);.#endif
2741b 0a 0a 20 20 20 20 2f 2a 20 45 56 3a 20 52 2d 33  ..    /* EV: R-3
2741c 30 33 32 33 2d 32 31 39 31 37 20 45 61 63 68 20  0323-21917 Each 
2741d 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
2741e 74 72 61 69 6e 74 20 69 6e 20 53 51 4c 69 74 65  traint in SQLite
2741f 20 69 73 0a 20 20 20 20 2a 2a 20 63 6c 61 73 73   is.    ** class
27420 69 66 69 65 64 20 61 73 20 65 69 74 68 65 72 20  ified as either 
27421 69 6d 6d 65 64 69 61 74 65 20 6f 72 20 64 65 66  immediate or def
27422 65 72 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  erred..    */.  
27423 20 20 61 73 73 65 72 74 28 20 70 46 4b 65 79 2d    assert( pFKey-
27424 3e 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20 7c  >isDeferred==0 |
27425 7c 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72  | pFKey->isDefer
27426 72 65 64 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 70  red==1 );..    p
27427 4e 65 78 74 20 3d 20 70 46 4b 65 79 2d 3e 70 4e  Next = pFKey->pN
27428 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 73 71 6c  extFrom;.    sql
27429 69 74 65 33 44 62 46 72 65 65 28 70 54 61 62 2d  ite3DbFree(pTab-
2742a 3e 64 62 4d 65 6d 2c 20 70 46 4b 65 79 29 3b 0a  >dbMem, pFKey);.
2742b 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20    }.}.#endif /* 
2742c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2742d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20 2a  IT_FOREIGN_KEY *
2742e 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2742f 2a 2a 20 45 6e 64 20 6f 66 20 66 6b 65 79 2e 63  ** End of fkey.c
27430 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
27431 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27432 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27433 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
27434 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 69 6e  ** Begin file in
27435 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  sert.c *********
27436 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27437 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27438 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
27439 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
2743a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2743b 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2743c 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2743d 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2743e 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2743f 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
27440 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
27441 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
27442 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
27443 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
27444 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
27445 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
27446 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
27447 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
27448 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
27449 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2744a 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
2744b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2744c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2744d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2744e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2744f 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
27450 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
27451 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20  C code routines 
27452 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20  that are called 
27453 62 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a  by the parser.**
27454 20 74 6f 20 68 61 6e 64 6c 65 20 49 4e 53 45 52   to handle INSER
27455 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20  T statements in 
27456 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  SQLite..**.** $I
27457 64 3a 20 69 6e 73 65 72 74 2e 63 2c 76 20 31 2e  d: insert.c,v 1.
27458 32 37 30 20 32 30 30 39 2f 30 37 2f 32 34 20 31  270 2009/07/24 1
27459 37 3a 35 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31  7:58:53 danielk1
2745a 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
2745b 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
2745c 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e  e that will open
2745d 20 61 20 74 61 62 6c 65 20 66 6f 72 20 72 65 61   a table for rea
2745e 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ding..*/.SQLITE_
2745f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
27460 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 0a 20  ite3OpenTable(. 
27461 20 50 61 72 73 65 20 2a 70 2c 20 20 20 20 20 20   Parse *p,      
27462 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
27463 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45  e into this VDBE
27464 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20   */.  int iCur, 
27465 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72        /* The cur
27466 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
27467 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
27468 20 69 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20   iDb,        /* 
27469 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 64  The database ind
2746a 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44  ex in sqlite3.aD
2746b 62 5b 5d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  b[] */.  Table *
2746c 70 54 61 62 2c 20 20 20 20 2f 2a 20 54 68 65 20  pTab,    /* The 
2746d 74 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e  table to be open
2746e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f  ed */.  int opco
2746f 64 65 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70  de      /* OP_Op
27470 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65  enRead or OP_Ope
27471 6e 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 56  nWrite */.){.  V
27472 64 62 65 20 2a 76 3b 0a 20 20 69 66 28 20 49 73  dbe *v;.  if( Is
27473 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20  Virtual(pTab) ) 
27474 72 65 74 75 72 6e 3b 0a 20 20 76 20 3d 20 73 71  return;.  v = sq
27475 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29 3b  lite3GetVdbe(p);
27476 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64  .  assert( opcod
27477 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20  e==OP_OpenWrite 
27478 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70  || opcode==OP_Op
27479 65 6e 52 65 61 64 20 29 3b 0a 20 20 73 71 6c 69  enRead );.  sqli
2747a 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  te3TableLock(p, 
2747b 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  iDb, pTab->tnum,
2747c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65   (opcode==OP_Ope
2747d 6e 57 72 69 74 65 29 3f 31 3a 30 2c 20 70 54 61  nWrite)?1:0, pTa
2747e 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  b->zName);.  sql
2747f 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
27480 2c 20 6f 70 63 6f 64 65 2c 20 69 43 75 72 2c 20  , opcode, iCur, 
27481 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  pTab->tnum, iDb)
27482 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ;.  sqlite3VdbeC
27483 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 53  hangeP4(v, -1, S
27484 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
27485 28 70 54 61 62 2d 3e 6e 43 6f 6c 29 2c 20 50 34  (pTab->nCol), P4
27486 5f 49 4e 54 33 32 29 3b 0a 20 20 56 64 62 65 43  _INT32);.  VdbeC
27487 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
27488 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a   pTab->zName));.
27489 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2748a 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
2748b 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
2748c 20 73 74 72 69 6e 67 20 61 73 73 6f 63 69 61 74   string associat
2748d 65 64 20 77 69 74 68 20 69 6e 64 65 78 0a 2a 2a  ed with index.**
2748e 20 70 49 64 78 2e 20 41 20 63 6f 6c 75 6d 6e 20   pIdx. A column 
2748f 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
27490 68 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65  has one characte
27491 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  r for each colum
27492 6e 20 69 6e 20 0a 2a 2a 20 74 68 65 20 74 61 62  n in .** the tab
27493 6c 65 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  le, according to
27494 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   the affinity of
27495 20 74 68 65 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a   the column:.**.
27496 2a 2a 20 20 43 68 61 72 61 63 74 65 72 20 20 20  **  Character   
27497 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69     Column affini
27498 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ty.**  ---------
27499 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2749a 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20  -----.**  'a'   
2749b 20 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a           TEXT.**
2749c 20 20 27 62 27 20 20 20 20 20 20 20 20 20 20 20    'b'           
2749d 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20   NONE.**  'c'   
2749e 20 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43           NUMERIC
2749f 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20 20  .**  'd'        
274a0 20 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20      INTEGER.**  
274a1 27 65 27 20 20 20 20 20 20 20 20 20 20 20 20 52  'e'            R
274a2 45 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 74  EAL.**.** An ext
274a3 72 61 20 27 62 27 20 69 73 20 61 70 70 65 6e 64  ra 'b' is append
274a4 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  ed to the end of
274a5 20 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63   the string to c
274a6 6f 76 65 72 20 74 68 65 0a 2a 2a 20 72 6f 77 69  over the.** rowi
274a7 64 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61  d that appears a
274a8 73 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  s the last colum
274a9 6e 20 69 6e 20 65 76 65 72 79 20 69 6e 64 65 78  n in every index
274aa 2e 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 66  ..**.** Memory f
274ab 6f 72 20 74 68 65 20 62 75 66 66 65 72 20 63 6f  or the buffer co
274ac 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 6f 6c  ntaining the col
274ad 75 6d 6e 20 69 6e 64 65 78 20 61 66 66 69 6e 69  umn index affini
274ae 74 79 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20  ty string.** is 
274af 6d 61 6e 61 67 65 64 20 61 6c 6f 6e 67 20 77 69  managed along wi
274b0 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  th the rest of t
274b1 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  he Index structu
274b2 72 65 2e 20 49 74 20 77 69 6c 6c 20 62 65 0a 2a  re. It will be.*
274b3 2a 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20  * released when 
274b4 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64  sqlite3DeleteInd
274b5 65 78 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a  ex() is called..
274b6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
274b7 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  E const char *sq
274b8 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69  lite3IndexAffini
274b9 74 79 53 74 72 28 56 64 62 65 20 2a 76 2c 20 49  tyStr(Vdbe *v, I
274ba 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69  ndex *pIdx){.  i
274bb 66 28 20 21 70 49 64 78 2d 3e 7a 43 6f 6c 41 66  f( !pIdx->zColAf
274bc 66 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  f ){.    /* The 
274bd 66 69 72 73 74 20 74 69 6d 65 20 61 20 63 6f 6c  first time a col
274be 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
274bf 69 6e 67 20 66 6f 72 20 61 20 70 61 72 74 69 63  ing for a partic
274c0 75 6c 61 72 20 69 6e 64 65 78 20 69 73 0a 20 20  ular index is.  
274c1 20 20 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 69    ** required, i
274c2 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  t is allocated a
274c3 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72  nd populated her
274c4 65 2e 20 49 74 20 69 73 20 74 68 65 6e 20 73 74  e. It is then st
274c5 6f 72 65 64 20 61 73 0a 20 20 20 20 2a 2a 20 61  ored as.    ** a
274c6 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 49   member of the I
274c7 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 66  ndex structure f
274c8 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 75 73  or subsequent us
274c9 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e..    **.    **
274ca 20 54 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   The column affi
274cb 6e 69 74 79 20 73 74 72 69 6e 67 20 77 69 6c 6c  nity string will
274cc 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 64   eventually be d
274cd 65 6c 65 74 65 64 20 62 79 0a 20 20 20 20 2a 2a  eleted by.    **
274ce 20 73 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64   sqliteDeleteInd
274cf 65 78 28 29 20 77 68 65 6e 20 74 68 65 20 49 6e  ex() when the In
274d0 64 65 78 20 73 74 72 75 63 74 75 72 65 20 69 74  dex structure it
274d1 73 65 6c 66 20 69 73 20 63 6c 65 61 6e 65 64 0a  self is cleaned.
274d2 20 20 20 20 2a 2a 20 75 70 2e 0a 20 20 20 20 2a      ** up..    *
274d3 2f 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20  /.    int n;.   
274d4 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
274d5 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20  Idx->pTable;.   
274d6 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
274d7 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 3b  qlite3VdbeDb(v);
274d8 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41  .    pIdx->zColA
274d9 66 66 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  ff = (char *)sql
274da 69 74 65 33 4d 61 6c 6c 6f 63 28 70 49 64 78 2d  ite3Malloc(pIdx-
274db 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20  >nColumn+2);.   
274dc 20 69 66 28 20 21 70 49 64 78 2d 3e 7a 43 6f 6c   if( !pIdx->zCol
274dd 41 66 66 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  Aff ){.      db-
274de 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
274df 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  1;.      return 
274e0 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  0;.    }.    for
274e1 28 6e 3d 30 3b 20 6e 3c 70 49 64 78 2d 3e 6e 43  (n=0; n<pIdx->nC
274e2 6f 6c 75 6d 6e 3b 20 6e 2b 2b 29 7b 0a 20 20 20  olumn; n++){.   
274e3 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66     pIdx->zColAff
274e4 5b 6e 5d 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  [n] = pTab->aCol
274e5 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  [pIdx->aiColumn[
274e6 6e 5d 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20  n]].affinity;.  
274e7 20 20 7d 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43    }.    pIdx->zC
274e8 6f 6c 41 66 66 5b 6e 2b 2b 5d 20 3d 20 53 51 4c  olAff[n++] = SQL
274e9 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
274ea 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b    pIdx->zColAff[
274eb 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 0a 20 20  n] = 0;.  }. .  
274ec 72 65 74 75 72 6e 20 70 49 64 78 2d 3e 7a 43 6f  return pIdx->zCo
274ed 6c 41 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  lAff;.}../*.** S
274ee 65 74 20 50 34 20 6f 66 20 74 68 65 20 6d 6f 73  et P4 of the mos
274ef 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72  t recently inser
274f0 74 65 64 20 6f 70 63 6f 64 65 20 74 6f 20 61 20  ted opcode to a 
274f1 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a  column affinity.
274f2 2a 2a 20 73 74 72 69 6e 67 20 66 6f 72 20 74 61  ** string for ta
274f3 62 6c 65 20 70 54 61 62 2e 20 41 20 63 6f 6c 75  ble pTab. A colu
274f4 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  mn affinity stri
274f5 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61 72 61  ng has one chara
274f6 63 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61 63 68  cter.** for each
274f7 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 65 64 20   column indexed 
274f8 62 79 20 74 68 65 20 69 6e 64 65 78 2c 20 61 63  by the index, ac
274f9 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 61  cording to the a
274fa 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 0a 2a  ffinity of the.*
274fb 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20  * column:.**.** 
274fc 20 43 68 61 72 61 63 74 65 72 20 20 20 20 20 20   Character      
274fd 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a  Column affinity.
274fe 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **  ------------
274ff 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27500 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20 20  --.**  'a'      
27501 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20 27        TEXT.**  '
27502 62 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 4f  b'            NO
27503 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20 20  NE.**  'c'      
27504 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a 2a        NUMERIC.**
27505 20 20 27 64 27 20 20 20 20 20 20 20 20 20 20 20    'd'           
27506 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65 27   INTEGER.**  'e'
27507 20 20 20 20 20 20 20 20 20 20 20 20 52 45 41 4c              REAL
27508 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
27509 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54  TE void sqlite3T
2750a 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28  ableAffinityStr(
2750b 56 64 62 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a  Vdbe *v, Table *
2750c 70 54 61 62 29 7b 0a 20 20 2f 2a 20 54 68 65 20  pTab){.  /* The 
2750d 66 69 72 73 74 20 74 69 6d 65 20 61 20 63 6f 6c  first time a col
2750e 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
2750f 69 6e 67 20 66 6f 72 20 61 20 70 61 72 74 69 63  ing for a partic
27510 75 6c 61 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20  ular table.  ** 
27511 69 73 20 72 65 71 75 69 72 65 64 2c 20 69 74 20  is required, it 
27512 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
27513 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72 65 2e   populated here.
27514 20 49 74 20 69 73 20 74 68 65 6e 20 0a 20 20 2a   It is then .  *
27515 2a 20 73 74 6f 72 65 64 20 61 73 20 61 20 6d 65  * stored as a me
27516 6d 62 65 72 20 6f 66 20 74 68 65 20 54 61 62 6c  mber of the Tabl
27517 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  e structure for 
27518 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 2e 0a  subsequent use..
27519 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 6f    **.  ** The co
2751a 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
2751b 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75  ring will eventu
2751c 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20  ally be deleted 
2751d 62 79 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 44  by.  ** sqlite3D
2751e 65 6c 65 74 65 54 61 62 6c 65 28 29 20 77 68 65  eleteTable() whe
2751f 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  n the Table stru
27520 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 20  cture itself is 
27521 63 6c 65 61 6e 65 64 20 75 70 2e 0a 20 20 2a 2f  cleaned up..  */
27522 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e 7a 43  .  if( !pTab->zC
27523 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 63 68 61  olAff ){.    cha
27524 72 20 2a 7a 43 6f 6c 41 66 66 3b 0a 20 20 20 20  r *zColAff;.    
27525 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74  int i;.    sqlit
27526 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
27527 56 64 62 65 44 62 28 76 29 3b 0a 0a 20 20 20 20  VdbeDb(v);..    
27528 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68 61 72 20  zColAff = (char 
27529 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  *)sqlite3Malloc(
2752a 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20  pTab->nCol+1);. 
2752b 20 20 20 69 66 28 20 21 7a 43 6f 6c 41 66 66 20     if( !zColAff 
2752c 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
2752d 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2752e 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2752f 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   }..    for(i=0;
27530 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
27531 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 41  ++){.      zColA
27532 66 66 5b 69 5d 20 3d 20 70 54 61 62 2d 3e 61 43  ff[i] = pTab->aC
27533 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3b 0a  ol[i].affinity;.
27534 20 20 20 20 7d 0a 20 20 20 20 7a 43 6f 6c 41 66      }.    zColAf
27535 66 5b 70 54 61 62 2d 3e 6e 43 6f 6c 5d 20 3d 20  f[pTab->nCol] = 
27536 27 5c 30 27 3b 0a 0a 20 20 20 20 70 54 61 62 2d  '\0';..    pTab-
27537 3e 7a 43 6f 6c 41 66 66 20 3d 20 7a 43 6f 6c 41  >zColAff = zColA
27538 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  ff;.  }..  sqlit
27539 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
2753a 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 43 6f 6c  , -1, pTab->zCol
2753b 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Aff, 0);.}../*.*
2753c 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  * Return non-zer
2753d 6f 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 70  o if the table p
2753e 54 61 62 20 69 6e 20 64 61 74 61 62 61 73 65 20  Tab in database 
2753f 69 44 62 20 6f 72 20 61 6e 79 20 6f 66 20 69 74  iDb or any of it
27540 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20 68 61 76  s indices.** hav
27541 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 61 74  e been opened at
27542 20 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 68   any point in th
27543 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 62  e VDBE program b
27544 65 67 69 6e 6e 69 6e 67 20 61 74 20 6c 6f 63 61  eginning at loca
27545 74 69 6f 6e 0a 2a 2a 20 69 53 74 61 72 74 41 64  tion.** iStartAd
27546 64 72 20 74 68 72 6f 75 67 68 74 20 74 68 65 20  dr throught the 
27547 65 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72  end of the progr
27548 61 6d 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  am.  This is use
27549 64 20 74 6f 20 73 65 65 20 69 66 20 0a 2a 2a 20  d to see if .** 
2754a 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74  a statement of t
2754b 68 65 20 66 6f 72 6d 20 20 22 49 4e 53 45 52 54  he form  "INSERT
2754c 20 49 4e 54 4f 20 3c 69 44 62 2c 20 70 54 61 62   INTO <iDb, pTab
2754d 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20 63 61  > SELECT ..." ca
2754e 6e 20 0a 2a 2a 20 72 75 6e 20 77 69 74 68 6f 75  n .** run withou
2754f 74 20 75 73 69 6e 67 20 74 65 6d 70 6f 72 61 72  t using temporar
27550 79 20 74 61 62 6c 65 20 66 6f 72 20 74 68 65 20  y table for the 
27551 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53  results of the S
27552 45 4c 45 43 54 2e 20 0a 2a 2f 0a 73 74 61 74 69  ELECT. .*/.stati
27553 63 20 69 6e 74 20 72 65 61 64 73 54 61 62 6c 65  c int readsTable
27554 28 50 61 72 73 65 20 2a 70 2c 20 69 6e 74 20 69  (Parse *p, int i
27555 53 74 61 72 74 41 64 64 72 2c 20 69 6e 74 20 69  StartAddr, int i
27556 44 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29  Db, Table *pTab)
27557 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71  {.  Vdbe *v = sq
27558 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29 3b  lite3GetVdbe(p);
27559 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
2755a 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64  iEnd = sqlite3Vd
2755b 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2755c 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2755d 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2755e 4c 45 0a 20 20 56 54 61 62 6c 65 20 2a 70 56 54  LE.  VTable *pVT
2755f 61 62 20 3d 20 49 73 56 69 72 74 75 61 6c 28 70  ab = IsVirtual(p
27560 54 61 62 29 20 3f 20 73 71 6c 69 74 65 33 47 65  Tab) ? sqlite3Ge
27561 74 56 54 61 62 6c 65 28 70 2d 3e 64 62 2c 20 70  tVTable(p->db, p
27562 54 61 62 29 20 3a 20 30 3b 0a 23 65 6e 64 69 66  Tab) : 0;.#endif
27563 0a 0a 20 20 66 6f 72 28 69 3d 69 53 74 61 72 74  ..  for(i=iStart
27564 41 64 64 72 3b 20 69 3c 69 45 6e 64 3b 20 69 2b  Addr; i<iEnd; i+
27565 2b 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a  +){.    VdbeOp *
27566 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  pOp = sqlite3Vdb
27567 65 47 65 74 4f 70 28 76 2c 20 69 29 3b 0a 20 20  eGetOp(v, i);.  
27568 20 20 61 73 73 65 72 74 28 20 70 4f 70 21 3d 30    assert( pOp!=0
27569 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   );.    if( pOp-
2756a 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e  >opcode==OP_Open
2756b 52 65 61 64 20 26 26 20 70 4f 70 2d 3e 70 33 3d  Read && pOp->p3=
2756c 3d 69 44 62 20 29 7b 0a 20 20 20 20 20 20 49 6e  =iDb ){.      In
2756d 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 20  dex *pIndex;.   
2756e 20 20 20 69 6e 74 20 74 6e 75 6d 20 3d 20 70 4f     int tnum = pO
2756f 70 2d 3e 70 32 3b 0a 20 20 20 20 20 20 69 66 28  p->p2;.      if(
27570 20 74 6e 75 6d 3d 3d 70 54 61 62 2d 3e 74 6e 75   tnum==pTab->tnu
27571 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  m ){.        ret
27572 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
27573 20 20 20 20 20 66 6f 72 28 70 49 6e 64 65 78 3d       for(pIndex=
27574 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
27575 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e  ndex; pIndex=pIn
27576 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  dex->pNext){.   
27577 20 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70       if( tnum==p
27578 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 29 7b 0a 20  Index->tnum ){. 
27579 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
2757a 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
2757b 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64     }.    }.#ifnd
2757c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2757d 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
2757e 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
2757f 3d 4f 50 5f 56 4f 70 65 6e 20 26 26 20 70 4f 70  =OP_VOpen && pOp
27580 2d 3e 70 34 2e 70 56 74 61 62 3d 3d 70 56 54 61  ->p4.pVtab==pVTa
27581 62 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  b ){.      asser
27582 74 28 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62  t( pOp->p4.pVtab
27583 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  !=0 );.      ass
27584 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
27585 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a 20 20 20  ==P4_VTAB );.   
27586 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
27587 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20   }.#endif.  }.  
27588 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 69 66  return 0;.}..#if
27589 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2758a 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f  _AUTOINCREMENT./
2758b 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 6f 72 20 63  *.** Locate or c
2758c 72 65 61 74 65 20 61 6e 20 41 75 74 6f 69 6e 63  reate an Autoinc
2758d 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 61  Info structure a
2758e 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2758f 61 62 6c 65 20 70 54 61 62 0a 2a 2a 20 77 68 69  able pTab.** whi
27590 63 68 20 69 73 20 69 6e 20 64 61 74 61 62 61 73  ch is in databas
27591 65 20 69 44 62 2e 20 20 52 65 74 75 72 6e 20 74  e iDb.  Return t
27592 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62  he register numb
27593 65 72 20 66 6f 72 20 74 68 65 20 72 65 67 69 73  er for the regis
27594 74 65 72 0a 2a 2a 20 74 68 61 74 20 68 6f 6c 64  ter.** that hold
27595 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f  s the maximum ro
27596 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  wid..**.** There
27597 20 69 73 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20   is at most one 
27598 41 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75  AutoincInfo stru
27599 63 74 75 72 65 20 70 65 72 20 74 61 62 6c 65 20  cture per table 
2759a 65 76 65 6e 20 69 66 20 74 68 65 0a 2a 2a 20 73  even if the.** s
2759b 61 6d 65 20 74 61 62 6c 65 20 69 73 20 61 75 74  ame table is aut
2759c 6f 69 6e 63 72 65 6d 65 6e 74 65 64 20 6d 75 6c  oincremented mul
2759d 74 69 70 6c 65 20 74 69 6d 65 73 20 64 75 65 20  tiple times due 
2759e 74 6f 20 69 6e 73 65 72 74 73 20 77 69 74 68 69  to inserts withi
2759f 6e 0a 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 20  n.** triggers.  
275a0 41 20 6e 65 77 20 41 75 74 6f 69 6e 63 49 6e 66  A new AutoincInf
275a1 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63  o structure is c
275a2 72 65 61 74 65 64 20 69 66 20 74 68 69 73 20 69  reated if this i
275a3 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 75  s the.** first u
275a4 73 65 20 6f 66 20 74 61 62 6c 65 20 70 54 61 62  se of table pTab
275a5 2e 20 20 4f 6e 20 32 6e 64 20 61 6e 64 20 73 75  .  On 2nd and su
275a6 62 73 65 71 75 65 6e 74 20 75 73 65 73 2c 20 74  bsequent uses, t
275a7 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 41  he original.** A
275a8 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75 63  utoincInfo struc
275a9 74 75 72 65 20 69 73 20 75 73 65 64 2e 0a 2a 2a  ture is used..**
275aa 0a 2a 2a 20 54 68 72 65 65 20 6d 65 6d 6f 72 79  .** Three memory
275ab 20 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 61   locations are a
275ac 6c 6c 6f 63 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20  llocated:.**.** 
275ad 20 20 28 31 29 20 20 52 65 67 69 73 74 65 72 20    (1)  Register 
275ae 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 61 6d 65  to hold the name
275af 20 6f 66 20 74 68 65 20 70 54 61 62 20 74 61 62   of the pTab tab
275b0 6c 65 2e 0a 2a 2a 20 20 20 28 32 29 20 20 52 65  le..**   (2)  Re
275b1 67 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74  gister to hold t
275b2 68 65 20 6d 61 78 69 6d 75 6d 20 52 4f 57 49 44  he maximum ROWID
275b3 20 6f 66 20 70 54 61 62 2e 0a 2a 2a 20 20 20 28   of pTab..**   (
275b4 33 29 20 20 52 65 67 69 73 74 65 72 20 74 6f 20  3)  Register to 
275b5 68 6f 6c 64 20 74 68 65 20 72 6f 77 69 64 20 69  hold the rowid i
275b6 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  n sqlite_sequenc
275b7 65 20 6f 66 20 70 54 61 62 0a 2a 2a 0a 2a 2a 20  e of pTab.**.** 
275b8 54 68 65 20 32 6e 64 20 72 65 67 69 73 74 65 72  The 2nd register
275b9 20 69 73 20 74 68 65 20 6f 6e 65 20 74 68 61 74   is the one that
275ba 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54   is returned.  T
275bb 68 61 74 20 69 73 20 61 6c 6c 20 74 68 65 0a 2a  hat is all the.*
275bc 2a 20 69 6e 73 65 72 74 20 72 6f 75 74 69 6e 65  * insert routine
275bd 20 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 61   needs to know a
275be 62 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  bout..*/.static 
275bf 69 6e 74 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  int autoIncBegin
275c0 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
275c1 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69  e,      /* Parsi
275c2 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
275c3 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20  int iDb,        
275c4 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
275c5 74 68 65 20 64 61 74 61 62 61 73 65 20 68 6f 6c  the database hol
275c6 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 54  ding pTab */.  T
275c7 61 62 6c 65 20 2a 70 54 61 62 20 20 20 20 20 20  able *pTab      
275c8 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
275c9 77 65 20 61 72 65 20 77 72 69 74 69 6e 67 20 74  we are writing t
275ca 6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6d 65  o */.){.  int me
275cb 6d 49 64 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  mId = 0;      /*
275cc 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
275cd 67 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20  g maximum rowid 
275ce 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74  */.  if( pTab->t
275cf 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74  abFlags & TF_Aut
275d0 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20  oincrement ){.  
275d1 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
275d2 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  el = sqlite3Pars
275d3 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
275d4 29 3b 0a 20 20 20 20 41 75 74 6f 69 6e 63 49 6e  );.    AutoincIn
275d5 66 6f 20 2a 70 49 6e 66 6f 3b 0a 0a 20 20 20 20  fo *pInfo;..    
275d6 70 49 6e 66 6f 20 3d 20 70 54 6f 70 6c 65 76 65  pInfo = pTopleve
275d7 6c 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 77 68  l->pAinc;.    wh
275d8 69 6c 65 28 20 70 49 6e 66 6f 20 26 26 20 70 49  ile( pInfo && pI
275d9 6e 66 6f 2d 3e 70 54 61 62 21 3d 70 54 61 62 20  nfo->pTab!=pTab 
275da 29 7b 20 70 49 6e 66 6f 20 3d 20 70 49 6e 66 6f  ){ pInfo = pInfo
275db 2d 3e 70 4e 65 78 74 3b 20 7d 0a 20 20 20 20 69  ->pNext; }.    i
275dc 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20  f( pInfo==0 ){. 
275dd 20 20 20 20 20 70 49 6e 66 6f 20 3d 20 73 71 6c       pInfo = sql
275de 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
275df 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65  pParse->db, size
275e0 6f 66 28 2a 70 49 6e 66 6f 29 29 3b 0a 20 20 20  of(*pInfo));.   
275e1 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20     if( pInfo==0 
275e2 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
275e3 20 20 70 49 6e 66 6f 2d 3e 70 4e 65 78 74 20 3d    pInfo->pNext =
275e4 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e   pToplevel->pAin
275e5 63 3b 0a 20 20 20 20 20 20 70 54 6f 70 6c 65 76  c;.      pToplev
275e6 65 6c 2d 3e 70 41 69 6e 63 20 3d 20 70 49 6e 66  el->pAinc = pInf
275e7 6f 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  o;.      pInfo->
275e8 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20  pTab = pTab;.   
275e9 20 20 20 70 49 6e 66 6f 2d 3e 69 44 62 20 3d 20     pInfo->iDb = 
275ea 69 44 62 3b 0a 20 20 20 20 20 20 70 54 6f 70 6c  iDb;.      pTopl
275eb 65 76 65 6c 2d 3e 6e 4d 65 6d 2b 2b 3b 20 20 20  evel->nMem++;   
275ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
275ed 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f  * Register to ho
275ee 6c 64 20 6e 61 6d 65 20 6f 66 20 74 61 62 6c 65  ld name of table
275ef 20 2a 2f 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d   */.      pInfo-
275f0 3e 72 65 67 43 74 72 20 3d 20 2b 2b 70 54 6f 70  >regCtr = ++pTop
275f1 6c 65 76 65 6c 2d 3e 6e 4d 65 6d 3b 20 20 2f 2a  level->nMem;  /*
275f2 20 4d 61 78 20 72 6f 77 69 64 20 72 65 67 69 73   Max rowid regis
275f3 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 54 6f  ter */.      pTo
275f4 70 6c 65 76 65 6c 2d 3e 6e 4d 65 6d 2b 2b 3b 20  plevel->nMem++; 
275f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275f6 20 2f 2a 20 52 6f 77 69 64 20 69 6e 20 73 71 6c   /* Rowid in sql
275f7 69 74 65 5f 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ite_sequence */.
275f8 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 49 64 20      }.    memId 
275f9 3d 20 70 49 6e 66 6f 2d 3e 72 65 67 43 74 72 3b  = pInfo->regCtr;
275fa 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 65  .  }.  return me
275fb 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  mId;.}../*.** Th
275fc 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72  is routine gener
275fd 61 74 65 73 20 63 6f 64 65 20 74 68 61 74 20 77  ates code that w
275fe 69 6c 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20 61  ill initialize a
275ff 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 67  ll of the.** reg
27600 69 73 74 65 72 20 75 73 65 64 20 62 79 20 74 68  ister used by th
27601 65 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20  e autoincrement 
27602 74 72 61 63 6b 65 72 2e 20 20 0a 2a 2f 0a 53 51  tracker.  .*/.SQ
27603 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
27604 64 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63  d sqlite3Autoinc
27605 72 65 6d 65 6e 74 42 65 67 69 6e 28 50 61 72 73  rementBegin(Pars
27606 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 41 75  e *pParse){.  Au
27607 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 3b 20 20 20  toincInfo *p;   
27608 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f           /* Info
27609 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6e  rmation about an
2760a 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a   AUTOINCREMENT *
2760b 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2760c 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f  = pParse->db;  /
2760d 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
2760e 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 44  onnection */.  D
2760f 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20  b *pDb;         
27610 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
27611 61 62 61 73 65 20 6f 6e 6c 79 20 61 75 74 6f 69  abase only autoi
27612 6e 63 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  nc table */.  in
27613 74 20 6d 65 6d 49 64 3b 20 20 20 20 20 20 20 20  t memId;        
27614 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
27615 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6d 61 78  ster holding max
27616 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20   rowid */.  int 
27617 61 64 64 72 3b 20 20 20 20 20 20 20 20 20 20 20  addr;           
27618 20 20 20 20 20 20 20 2f 2a 20 41 20 56 44 42 45         /* A VDBE
27619 20 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 56 64   address */.  Vd
2761a 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2761b 70 56 64 62 65 3b 20 20 20 2f 2a 20 56 44 42 45  pVdbe;   /* VDBE
2761c 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
2761d 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69  ion */..  /* Thi
2761e 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76  s routine is nev
2761f 65 72 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67  er called during
27620 20 74 72 69 67 67 65 72 2d 67 65 6e 65 72 61 74   trigger-generat
27621 69 6f 6e 2e 20 20 49 74 20 69 73 0a 20 20 2a 2a  ion.  It is.  **
27622 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f   only called fro
27623 6d 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20  m the top-level 
27624 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
27625 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
27626 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
27627 20 70 50 61 72 73 65 3d 3d 73 71 6c 69 74 65 33   pParse==sqlite3
27628 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50  ParseToplevel(pP
27629 61 72 73 65 29 20 29 3b 0a 0a 20 20 61 73 73 65  arse) );..  asse
2762a 72 74 28 20 76 20 29 3b 20 20 20 2f 2a 20 57 65  rt( v );   /* We
2762b 20 66 61 69 6c 65 64 20 6c 6f 6e 67 20 61 67 6f   failed long ago
2762c 20 69 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   if this is not 
2762d 73 6f 20 2a 2f 0a 20 20 66 6f 72 28 70 20 3d 20  so */.  for(p = 
2762e 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 3b 20 70  pParse->pAinc; p
2762f 3b 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 29 7b  ; p = p->pNext){
27630 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  .    pDb = &db->
27631 61 44 62 5b 70 2d 3e 69 44 62 5d 3b 0a 20 20 20  aDb[p->iDb];.   
27632 20 6d 65 6d 49 64 20 3d 20 70 2d 3e 72 65 67 43   memId = p->regC
27633 74 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  tr;.    sqlite3O
27634 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
27635 20 30 2c 20 70 2d 3e 69 44 62 2c 20 70 44 62 2d   0, p->iDb, pDb-
27636 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61  >pSchema->pSeqTa
27637 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b  b, OP_OpenRead);
27638 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
27639 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2763a 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr(v);.    sqlit
2763b 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2763c 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 6d  OP_String8, 0, m
2763d 65 6d 49 64 2d 31 2c 20 30 2c 20 70 2d 3e 70 54  emId-1, 0, p->pT
2763e 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  ab->zName, 0);. 
2763f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27640 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
27641 64 2c 20 30 2c 20 61 64 64 72 2b 39 29 3b 0a 20  d, 0, addr+9);. 
27642 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27643 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
27644 6e 2c 20 30 2c 20 30 2c 20 6d 65 6d 49 64 29 3b  n, 0, 0, memId);
27645 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27646 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c  AddOp3(v, OP_Ne,
27647 20 6d 65 6d 49 64 2d 31 2c 20 61 64 64 72 2b 37   memId-1, addr+7
27648 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71  , memId);.    sq
27649 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2764a 35 28 76 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  5(v, SQLITE_JUMP
2764b 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 73 71 6c  IFNULL);.    sql
2764c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2764d 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 30 2c 20 6d  , OP_Rowid, 0, m
2764e 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c  emId+1);.    sql
2764f 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
27650 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20  , OP_Column, 0, 
27651 31 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73  1, memId);.    s
27652 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27653 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
27654 61 64 64 72 2b 39 29 3b 0a 20 20 20 20 73 71 6c  addr+9);.    sql
27655 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27656 2c 20 4f 50 5f 4e 65 78 74 2c 20 30 2c 20 61 64  , OP_Next, 0, ad
27657 64 72 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr+2);.    sqlit
27658 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27659 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d  OP_Integer, 0, m
2765a 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  emId);.    sqlit
2765b 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
2765c 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 7d  OP_Close);.  }.}
2765d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74  ../*.** Update t
2765e 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  he maximum rowid
2765f 20 66 6f 72 20 61 6e 20 61 75 74 6f 69 6e 63 72   for an autoincr
27660 65 6d 65 6e 74 20 63 61 6c 63 75 6c 61 74 69 6f  ement calculatio
27661 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  n..**.** This ro
27662 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20  utine should be 
27663 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20  called when the 
27664 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
27665 20 68 6f 6c 64 73 20 61 0a 2a 2a 20 6e 65 77 20   holds a.** new 
27666 72 6f 77 69 64 20 74 68 61 74 20 69 73 20 61 62  rowid that is ab
27667 6f 75 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74  out to be insert
27668 65 64 2e 20 20 49 66 20 74 68 61 74 20 6e 65 77  ed.  If that new
27669 20 72 6f 77 69 64 20 69 73 0a 2a 2a 20 6c 61 72   rowid is.** lar
2766a 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78  ger than the max
2766b 69 6d 75 6d 20 72 6f 77 69 64 20 69 6e 20 74 68  imum rowid in th
2766c 65 20 6d 65 6d 49 64 20 6d 65 6d 6f 72 79 20 63  e memId memory c
2766d 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ell, then the.**
2766e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20   memory cell is 
2766f 75 70 64 61 74 65 64 2e 20 20 54 68 65 20 73 74  updated.  The st
27670 61 63 6b 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ack is unchanged
27671 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
27672 20 61 75 74 6f 49 6e 63 53 74 65 70 28 50 61 72   autoIncStep(Par
27673 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
27674 6d 65 6d 49 64 2c 20 69 6e 74 20 72 65 67 52 6f  memId, int regRo
27675 77 69 64 29 7b 0a 20 20 69 66 28 20 6d 65 6d 49  wid){.  if( memI
27676 64 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  d>0 ){.    sqlit
27677 65 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61  e3VdbeAddOp2(pPa
27678 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4d  rse->pVdbe, OP_M
27679 65 6d 4d 61 78 2c 20 6d 65 6d 49 64 2c 20 72 65  emMax, memId, re
2767a 67 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 7d 0a 0a  gRowid);.  }.}..
2767b 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2767c 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74 68 65  ne generates the
2767d 20 63 6f 64 65 20 6e 65 65 64 65 64 20 74 6f 20   code needed to 
2767e 77 72 69 74 65 20 61 75 74 6f 69 6e 63 72 65 6d  write autoincrem
2767f 65 6e 74 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 72  ent.** maximum r
27680 6f 77 69 64 20 76 61 6c 75 65 73 20 62 61 63 6b  owid values back
27681 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65   into the sqlite
27682 5f 73 65 71 75 65 6e 63 65 20 72 65 67 69 73 74  _sequence regist
27683 65 72 2e 0a 2a 2a 20 45 76 65 72 79 20 73 74 61  er..** Every sta
27684 74 65 6d 65 6e 74 20 74 68 61 74 20 6d 69 67 68  tement that migh
27685 74 20 64 6f 20 61 6e 20 49 4e 53 45 52 54 20 69  t do an INSERT i
27686 6e 74 6f 20 61 6e 20 61 75 74 6f 69 6e 63 72 65  nto an autoincre
27687 6d 65 6e 74 0a 2a 2a 20 74 61 62 6c 65 20 28 65  ment.** table (e
27688 69 74 68 65 72 20 64 69 72 65 63 74 6c 79 20 6f  ither directly o
27689 72 20 74 68 72 6f 75 67 68 20 74 72 69 67 67 65  r through trigge
2768a 72 73 29 20 6e 65 65 64 73 20 74 6f 20 63 61 6c  rs) needs to cal
2768b 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  l this.** routin
2768c 65 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68  e just before th
2768d 65 20 22 65 78 69 74 22 20 63 6f 64 65 2e 0a 2a  e "exit" code..*
2768e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2768f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
27690 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 50 61  oincrementEnd(Pa
27691 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
27692 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 3b 0a  AutoincInfo *p;.
27693 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
27694 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c  se->pVdbe;.  sql
27695 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
27696 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74  e->db;..  assert
27697 28 20 76 20 29 3b 0a 20 20 66 6f 72 28 70 20 3d  ( v );.  for(p =
27698 20 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 3b 20   pParse->pAinc; 
27699 70 3b 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 29  p; p = p->pNext)
2769a 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20  {.    Db *pDb = 
2769b 26 64 62 2d 3e 61 44 62 5b 70 2d 3e 69 44 62 5d  &db->aDb[p->iDb]
2769c 3b 0a 20 20 20 20 69 6e 74 20 6a 31 2c 20 6a 32  ;.    int j1, j2
2769d 2c 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a 20 20  , j3, j4, j5;.  
2769e 20 20 69 6e 74 20 69 52 65 63 3b 0a 20 20 20 20    int iRec;.    
2769f 69 6e 74 20 6d 65 6d 49 64 20 3d 20 70 2d 3e 72  int memId = p->r
276a0 65 67 43 74 72 3b 0a 0a 20 20 20 20 69 52 65 63  egCtr;..    iRec
276a1 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
276a2 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
276a3 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
276a4 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 2d  le(pParse, 0, p-
276a5 3e 69 44 62 2c 20 70 44 62 2d 3e 70 53 63 68 65  >iDb, pDb->pSche
276a6 6d 61 2d 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f  ma->pSeqTab, OP_
276a7 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20  OpenWrite);.    
276a8 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
276a9 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
276aa 4e 75 6c 6c 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a  Null, memId+1);.
276ab 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33      j2 = sqlite3
276ac 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
276ad 5f 52 65 77 69 6e 64 29 3b 0a 20 20 20 20 6a 33  _Rewind);.    j3
276ae 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
276af 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
276b0 6e 2c 20 30 2c 20 30 2c 20 69 52 65 63 29 3b 0a  n, 0, 0, iRec);.
276b1 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 33      j4 = sqlite3
276b2 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
276b3 5f 45 71 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c  _Eq, memId-1, 0,
276b4 20 69 52 65 63 29 3b 0a 20 20 20 20 73 71 6c 69   iRec);.    sqli
276b5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
276b6 20 4f 50 5f 4e 65 78 74 2c 20 30 2c 20 6a 33 29   OP_Next, 0, j3)
276b7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
276b8 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 29  eJumpHere(v, j2)
276b9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
276ba 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
276bb 77 52 6f 77 69 64 2c 20 30 2c 20 6d 65 6d 49 64  wRowid, 0, memId
276bc 2b 31 29 3b 0a 20 20 20 20 6a 35 20 3d 20 73 71  +1);.    j5 = sq
276bd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
276be 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 20  v, OP_Goto);.   
276bf 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
276c0 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20  Here(v, j4);.   
276c1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
276c2 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
276c3 30 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20  0, memId+1);.   
276c4 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
276c5 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20  Here(v, j1);.   
276c6 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
276c7 48 65 72 65 28 76 2c 20 6a 35 29 3b 0a 20 20 20  Here(v, j5);.   
276c8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
276c9 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
276ca 6f 72 64 2c 20 6d 65 6d 49 64 2d 31 2c 20 32 2c  ord, memId-1, 2,
276cb 20 69 52 65 63 29 3b 0a 20 20 20 20 73 71 6c 69   iRec);.    sqli
276cc 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
276cd 20 4f 50 5f 49 6e 73 65 72 74 2c 20 30 2c 20 69   OP_Insert, 0, i
276ce 52 65 63 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20  Rec, memId+1);. 
276cf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
276d0 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
276d1 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71  _APPEND);.    sq
276d2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
276d3 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20  v, OP_Close);.  
276d4 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
276d5 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
276d6 69 52 65 63 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c  iRec);.  }.}.#el
276d7 73 65 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49  se./*.** If SQLI
276d8 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
276d9 45 4d 45 4e 54 20 69 73 20 64 65 66 69 6e 65 64  EMENT is defined
276da 2c 20 74 68 65 6e 20 74 68 65 20 74 68 72 65 65  , then the three
276db 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 62 6f   routines.** abo
276dc 76 65 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70  ve are all no-op
276dd 73 0a 2a 2f 0a 23 20 64 65 66 69 6e 65 20 61 75  s.*/.# define au
276de 74 6f 49 6e 63 42 65 67 69 6e 28 41 2c 42 2c 43  toIncBegin(A,B,C
276df 29 20 28 30 29 0a 23 20 64 65 66 69 6e 65 20 61  ) (0).# define a
276e0 75 74 6f 49 6e 63 53 74 65 70 28 41 2c 42 2c 43  utoIncStep(A,B,C
276e1 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ).#endif /* SQLI
276e2 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
276e3 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f 2a 20 46 6f  EMENT */.../* Fo
276e4 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
276e5 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n */.static int 
276e6 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e  xferOptimization
276e7 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
276e8 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
276e9 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
276ea 20 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20 20   Table *pDest,  
276eb 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
276ec 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 72  ble we are inser
276ed 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 53  ting into */.  S
276ee 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
276ef 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54       /* A SELECT
276f0 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73   statement to us
276f1 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73 6f  e as the data so
276f2 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e  urce */.  int on
276f3 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20  Error,          
276f4 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65  /* How to handle
276f5 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f   constraint erro
276f6 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 44  rs */.  int iDbD
276f7 65 73 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a  est           /*
276f8 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 66   The database of
276f9 20 70 44 65 73 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a   pDest */.);../*
276fa 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
276fb 20 69 73 20 63 61 6c 6c 20 74 6f 20 68 61 6e 64   is call to hand
276fc 6c 65 20 53 51 4c 20 6f 66 20 74 68 65 20 66 6f  le SQL of the fo
276fd 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a 2a  llowing forms:.*
276fe 2a 0a 2a 2a 20 20 20 20 69 6e 73 65 72 74 20 69  *.**    insert i
276ff 6e 74 6f 20 54 41 42 4c 45 20 28 49 44 4c 49 53  nto TABLE (IDLIS
27700 54 29 20 76 61 6c 75 65 73 28 45 58 50 52 4c 49  T) values(EXPRLI
27701 53 54 29 0a 2a 2a 20 20 20 20 69 6e 73 65 72 74  ST).**    insert
27702 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44 4c   into TABLE (IDL
27703 49 53 54 29 20 73 65 6c 65 63 74 0a 2a 2a 0a 2a  IST) select.**.*
27704 2a 20 54 68 65 20 49 44 4c 49 53 54 20 66 6f 6c  * The IDLIST fol
27705 6c 6f 77 69 6e 67 20 74 68 65 20 74 61 62 6c 65  lowing the table
27706 20 6e 61 6d 65 20 69 73 20 61 6c 77 61 79 73 20   name is always 
27707 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 6f 6d  optional.  If om
27708 69 74 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 61  itted,.** then a
27709 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f 6c   list of all col
2770a 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 74 61 62  umns for the tab
2770b 6c 65 20 69 73 20 73 75 62 73 74 69 74 75 74 65  le is substitute
2770c 64 2e 20 20 54 68 65 20 49 44 4c 49 53 54 0a 2a  d.  The IDLIST.*
2770d 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65  * appears in the
2770e 20 70 43 6f 6c 75 6d 6e 20 70 61 72 61 6d 65 74   pColumn paramet
2770f 65 72 2e 20 20 70 43 6f 6c 75 6d 6e 20 69 73 20  er.  pColumn is 
27710 4e 55 4c 4c 20 69 66 20 49 44 4c 49 53 54 20 69  NULL if IDLIST i
27711 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a  s omitted..**.**
27712 20 54 68 65 20 70 4c 69 73 74 20 70 61 72 61 6d   The pList param
27713 65 74 65 72 20 68 6f 6c 64 73 20 45 58 50 52 4c  eter holds EXPRL
27714 49 53 54 20 69 6e 20 74 68 65 20 66 69 72 73 74  IST in the first
27715 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 49 4e 53   form of the INS
27716 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ERT.** statement
27717 20 61 62 6f 76 65 2c 20 61 6e 64 20 70 53 65 6c   above, and pSel
27718 65 63 74 20 69 73 20 4e 55 4c 4c 2e 20 20 46 6f  ect is NULL.  Fo
27719 72 20 74 68 65 20 73 65 63 6f 6e 64 20 66 6f 72  r the second for
2771a 6d 2c 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20 4e  m, pList is.** N
2771b 55 4c 4c 20 61 6e 64 20 70 53 65 6c 65 63 74 20  ULL and pSelect 
2771c 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
2771d 74 68 65 20 73 65 6c 65 63 74 20 73 74 61 74 65  the select state
2771e 6d 65 6e 74 20 75 73 65 64 20 74 6f 20 67 65 6e  ment used to gen
2771f 65 72 61 74 65 0a 2a 2a 20 64 61 74 61 20 66 6f  erate.** data fo
27720 72 20 74 68 65 20 69 6e 73 65 72 74 2e 0a 2a 2a  r the insert..**
27721 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e  .** The code gen
27722 65 72 61 74 65 64 20 66 6f 6c 6c 6f 77 73 20 6f  erated follows o
27723 6e 65 20 6f 66 20 66 6f 75 72 20 74 65 6d 70 6c  ne of four templ
27724 61 74 65 73 2e 20 20 46 6f 72 20 61 20 73 69 6d  ates.  For a sim
27725 70 6c 65 0a 2a 2a 20 73 65 6c 65 63 74 20 77 69  ple.** select wi
27726 74 68 20 64 61 74 61 20 63 6f 6d 69 6e 67 20 66  th data coming f
27727 72 6f 6d 20 61 20 56 41 4c 55 45 53 20 63 6c 61  rom a VALUES cla
27728 75 73 65 2c 20 74 68 65 20 63 6f 64 65 20 65 78  use, the code ex
27729 65 63 75 74 65 73 0a 2a 2a 20 6f 6e 63 65 20 73  ecutes.** once s
2772a 74 72 61 69 67 68 74 20 64 6f 77 6e 20 74 68 72  traight down thr
2772b 6f 75 67 68 2e 20 20 50 73 65 75 64 6f 2d 63 6f  ough.  Pseudo-co
2772c 64 65 20 66 6f 6c 6c 6f 77 73 20 28 77 65 20 63  de follows (we c
2772d 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 74 68 65 20  all this.** the 
2772e 22 31 73 74 20 74 65 6d 70 6c 61 74 65 22 29 3a  "1st template"):
2772f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f  .**.**         o
27730 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f 72  pen write cursor
27731 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64 20   to <table> and 
27732 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20 20  its indices.**  
27733 20 20 20 20 20 20 20 70 75 74 73 20 56 41 4c 55         puts VALU
27734 45 53 20 63 6c 61 75 73 65 20 65 78 70 72 65 73  ES clause expres
27735 73 69 6f 6e 73 20 6f 6e 74 6f 20 74 68 65 20 73  sions onto the s
27736 74 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20  tack.**         
27737 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74  write the result
27738 69 6e 67 20 72 65 63 6f 72 64 20 69 6e 74 6f 20  ing record into 
27739 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20  <table>.**      
2773a 20 20 20 63 6c 65 61 6e 75 70 0a 2a 2a 0a 2a 2a     cleanup.**.**
2773b 20 54 68 65 20 74 68 72 65 65 20 72 65 6d 61 69   The three remai
2773c 6e 69 6e 67 20 74 65 6d 70 6c 61 74 65 73 20 61  ning templates a
2773d 73 73 75 6d 65 20 74 68 65 20 73 74 61 74 65 6d  ssume the statem
2773e 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ent is of the fo
2773f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45 52  rm.**.**   INSER
27740 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 3e 20 53  T INTO <table> S
27741 45 4c 45 43 54 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20  ELECT ....**.** 
27742 49 66 20 74 68 65 20 53 45 4c 45 43 54 20 63 6c  If the SELECT cl
27743 61 75 73 65 20 69 73 20 6f 66 20 74 68 65 20 72  ause is of the r
27744 65 73 74 72 69 63 74 65 64 20 66 6f 72 6d 20 22  estricted form "
27745 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 3c 74  SELECT * FROM <t
27746 61 62 6c 65 32 3e 22 20 2d 0a 2a 2a 20 69 6e 20  able2>" -.** in 
27747 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66 20 74  other words if t
27748 68 65 20 53 45 4c 45 43 54 20 70 75 6c 6c 73 20  he SELECT pulls 
27749 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d  all columns from
2774a 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 0a   a single table.
2774b 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 69 73 20  ** and there is 
2774c 6e 6f 20 57 48 45 52 45 20 6f 72 20 4c 49 4d 49  no WHERE or LIMI
2774d 54 20 6f 72 20 47 52 4f 55 50 20 42 59 20 6f 72  T or GROUP BY or
2774e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2774f 73 2c 20 61 6e 64 0a 2a 2a 20 69 66 20 3c 74 61  s, and.** if <ta
27750 62 6c 65 32 3e 20 61 6e 64 20 3c 74 61 62 6c 65  ble2> and <table
27751 31 3e 20 61 72 65 20 64 69 73 74 69 6e 63 74 20  1> are distinct 
27752 74 61 62 6c 65 73 20 62 75 74 20 68 61 76 65 20  tables but have 
27753 69 64 65 6e 74 69 63 61 6c 0a 2a 2a 20 73 63 68  identical.** sch
27754 65 6d 61 73 2c 20 69 6e 63 6c 75 64 69 6e 67 20  emas, including 
27755 61 6c 6c 20 74 68 65 20 73 61 6d 65 20 69 6e 64  all the same ind
27756 69 63 65 73 2c 20 74 68 65 6e 20 61 20 73 70 65  ices, then a spe
27757 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61 74 69 6f  cial optimizatio
27758 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20  n.** is invoked 
27759 74 68 61 74 20 63 6f 70 69 65 73 20 72 61 77 20  that copies raw 
2775a 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 3c 74 61  records from <ta
2775b 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f 20 3c 74  ble2> over to <t
2775c 61 62 6c 65 31 3e 2e 0a 2a 2a 20 53 65 65 20 74  able1>..** See t
2775d 68 65 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74  he xferOptimizat
2775e 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ion() function f
2775f 6f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  or the implement
27760 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 0a 2a 2a  ation of this.**
27761 20 74 65 6d 70 6c 61 74 65 2e 20 20 54 68 69 73   template.  This
27762 20 69 73 20 74 68 65 20 32 6e 64 20 74 65 6d 70   is the 2nd temp
27763 6c 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  late..**.**     
27764 20 20 20 20 6f 70 65 6e 20 61 20 77 72 69 74 65      open a write
27765 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c   cursor to <tabl
27766 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 70  e>.**         op
27767 65 6e 20 72 65 61 64 20 63 75 72 73 6f 72 20 6f  en read cursor o
27768 6e 20 3c 74 61 62 6c 65 32 3e 0a 2a 2a 20 20 20  n <table2>.**   
27769 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 20 61        transfer a
2776a 6c 6c 20 72 65 63 6f 72 64 73 20 69 6e 20 3c 74  ll records in <t
2776b 61 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f 20 3c  able2> over to <
2776c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20  table>.**       
2776d 20 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73 0a    close cursors.
2776e 2a 2a 20 20 20 20 20 20 20 20 20 66 6f 72 65 61  **         forea
2776f 63 68 20 69 6e 64 65 78 20 6f 6e 20 3c 74 61 62  ch index on <tab
27770 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  le>.**          
27771 20 6f 70 65 6e 20 61 20 77 72 69 74 65 20 63 75   open a write cu
27772 72 73 6f 72 20 6f 6e 20 74 68 65 20 3c 74 61 62  rsor on the <tab
27773 6c 65 3e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20  le> index.**    
27774 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 72 65         open a re
27775 61 64 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 65  ad cursor on the
27776 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 3c   corresponding <
27777 74 61 62 6c 65 32 3e 20 69 6e 64 65 78 0a 2a 2a  table2> index.**
27778 20 20 20 20 20 20 20 20 20 20 20 74 72 61 6e 73             trans
27779 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64 73 20  fer all records 
2777a 66 72 6f 6d 20 74 68 65 20 72 65 61 64 20 74 6f  from the read to
2777b 20 74 68 65 20 77 72 69 74 65 20 63 75 72 73 6f   the write curso
2777c 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  rs.**           
2777d 63 6c 6f 73 65 20 63 75 72 73 6f 72 73 0a 2a 2a  close cursors.**
2777e 20 20 20 20 20 20 20 20 20 65 6e 64 20 66 6f 72           end for
2777f 65 61 63 68 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33  each.**.** The 3
27780 72 64 20 74 65 6d 70 6c 61 74 65 20 69 73 20 66  rd template is f
27781 6f 72 20 77 68 65 6e 20 74 68 65 20 73 65 63 6f  or when the seco
27782 6e 64 20 74 65 6d 70 6c 61 74 65 20 64 6f 65 73  nd template does
27783 20 6e 6f 74 20 61 70 70 6c 79 0a 2a 2a 20 61 6e   not apply.** an
27784 64 20 74 68 65 20 53 45 4c 45 43 54 20 63 6c 61  d the SELECT cla
27785 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61  use does not rea
27786 64 20 66 72 6f 6d 20 3c 74 61 62 6c 65 3e 20 61  d from <table> a
27787 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a 20 54  t any time..** T
27788 68 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  he generated cod
27789 65 20 66 6f 6c 6c 6f 77 73 20 74 68 69 73 20 74  e follows this t
2778a 65 6d 70 6c 61 74 65 3a 0a 2a 2a 0a 2a 2a 20 20  emplate:.**.**  
2778b 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30 0a         EOF <- 0.
2778c 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d 20  **         X <- 
2778d 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74  A.**         got
2778e 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41 3a 20 73  o B.**      A: s
2778f 65 74 75 70 20 66 6f 72 20 74 68 65 20 53 45 4c  etup for the SEL
27790 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 6c  ECT.**         l
27791 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 72 6f 77  oop over the row
27792 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a  s in the SELECT.
27793 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 61  **           loa
27794 64 20 76 61 6c 75 65 73 20 69 6e 74 6f 20 72 65  d values into re
27795 67 69 73 74 65 72 73 20 52 2e 2e 52 2b 6e 0a 2a  gisters R..R+n.*
27796 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65 6c  *           yiel
27797 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 65  d X.**         e
27798 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20  nd loop.**      
27799 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65 72     cleanup after
2779a 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20   the SELECT.**  
2779b 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31 0a         EOF <- 1.
2779c 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c 64  **         yield
2779d 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f   X.**         go
2779e 74 6f 20 41 0a 2a 2a 20 20 20 20 20 20 42 3a 20  to A.**      B: 
2779f 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f  open write curso
277a0 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64  r to <table> and
277a1 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20   its indices.** 
277a2 20 20 20 20 20 43 3a 20 79 69 65 6c 64 20 58 0a       C: yield X.
277a3 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f  **         if EO
277a4 46 20 67 6f 74 6f 20 44 0a 2a 2a 20 20 20 20 20  F goto D.**     
277a5 20 20 20 20 69 6e 73 65 72 74 20 74 68 65 20 73      insert the s
277a6 65 6c 65 63 74 20 72 65 73 75 6c 74 20 69 6e 74  elect result int
277a7 6f 20 3c 74 61 62 6c 65 3e 20 66 72 6f 6d 20 52  o <table> from R
277a8 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ..R+n.**        
277a9 20 67 6f 74 6f 20 43 0a 2a 2a 20 20 20 20 20 20   goto C.**      
277aa 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2a 0a 2a 2a  D: cleanup.**.**
277ab 20 54 68 65 20 34 74 68 20 74 65 6d 70 6c 61 74   The 4th templat
277ac 65 20 69 73 20 75 73 65 64 20 69 66 20 74 68 65  e is used if the
277ad 20 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65 6e   insert statemen
277ae 74 20 74 61 6b 65 73 20 69 74 73 0a 2a 2a 20 76  t takes its.** v
277af 61 6c 75 65 73 20 66 72 6f 6d 20 61 20 53 45 4c  alues from a SEL
277b0 45 43 54 20 62 75 74 20 74 68 65 20 64 61 74 61  ECT but the data
277b1 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74   is being insert
277b2 65 64 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 0a  ed into a table.
277b3 2a 2a 20 74 68 61 74 20 69 73 20 61 6c 73 6f 20  ** that is also 
277b4 72 65 61 64 20 61 73 20 70 61 72 74 20 6f 66 20  read as part of 
277b5 74 68 65 20 53 45 4c 45 43 54 2e 20 20 49 6e 20  the SELECT.  In 
277b6 74 68 65 20 74 68 69 72 64 20 66 6f 72 6d 2c 0a  the third form,.
277b7 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 75 73  ** we have to us
277b8 65 20 61 20 69 6e 74 65 72 6d 65 64 69 61 74 65  e a intermediate
277b9 20 74 61 62 6c 65 20 74 6f 20 73 74 6f 72 65 20   table to store 
277ba 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 0a 2a  the results of.*
277bb 2a 20 74 68 65 20 73 65 6c 65 63 74 2e 20 20 54  * the select.  T
277bc 68 65 20 74 65 6d 70 6c 61 74 65 20 69 73 20 6c  he template is l
277bd 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
277be 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30          EOF <- 0
277bf 0a 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d  .**         X <-
277c0 20 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f   A.**         go
277c1 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41 3a 20  to B.**      A: 
277c2 73 65 74 75 70 20 66 6f 72 20 74 68 65 20 53 45  setup for the SE
277c3 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
277c4 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 74 61  loop over the ta
277c5 62 6c 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45  bles in the SELE
277c6 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  CT.**           
277c7 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e 74 6f 20  load value into 
277c8 72 65 67 69 73 74 65 72 20 52 2e 2e 52 2b 6e 0a  register R..R+n.
277c9 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65  **           yie
277ca 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20  ld X.**         
277cb 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20  end loop.**     
277cc 20 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65      cleanup afte
277cd 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20  r the SELECT.** 
277ce 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31          EOF <- 1
277cf 0a 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c  .**         yiel
277d0 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 68  d X.**         h
277d1 61 6c 74 2d 65 72 72 6f 72 0a 2a 2a 20 20 20 20  alt-error.**    
277d2 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d 70 20 74    B: open temp t
277d3 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 4c 3a 20  able.**      L: 
277d4 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20  yield X.**      
277d5 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 4d     if EOF goto M
277d6 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65  .**         inse
277d7 72 74 20 72 6f 77 20 66 72 6f 6d 20 52 2e 2e 52  rt row from R..R
277d8 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20 74 61 62  +n into temp tab
277d9 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f  le.**         go
277da 74 6f 20 4c 0a 2a 2a 20 20 20 20 20 20 4d 3a 20  to L.**      M: 
277db 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f  open write curso
277dc 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64  r to <table> and
277dd 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20   its indices.** 
277de 20 20 20 20 20 20 20 20 72 65 77 69 6e 64 20 74          rewind t
277df 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  emp table.**    
277e0 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 72    C: loop over r
277e1 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d 65 64 69  ows of intermedi
277e2 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  ate table.**    
277e3 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 20         transfer 
277e4 76 61 6c 75 65 73 20 66 6f 72 6d 20 69 6e 74 65  values form inte
277e5 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20 69  rmediate table i
277e6 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20  nto <table>.**  
277e7 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a         end loop.
277e8 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e  **      D: clean
277e9 75 70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  up.*/.SQLITE_PRI
277ea 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
277eb 33 49 6e 73 65 72 74 28 0a 20 20 50 61 72 73 65  3Insert(.  Parse
277ec 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
277ed 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65   /* Parser conte
277ee 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
277ef 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 2f 2a  *pTabList,    /*
277f0 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 69   Name of table i
277f1 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65  nto which we are
277f2 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20   inserting */.  
277f3 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
277f4 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
277f5 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e   values to be in
277f6 73 65 72 74 65 64 20 2a 2f 0a 20 20 53 65 6c 65  serted */.  Sele
277f7 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20  ct *pSelect,    
277f8 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74    /* A SELECT st
277f9 61 74 65 6d 65 6e 74 20 74 6f 20 75 73 65 20 61  atement to use a
277fa 73 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 63  s the data sourc
277fb 65 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70  e */.  IdList *p
277fc 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 2f 2a 20  Column,      /* 
277fd 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 63 6f 72  Column names cor
277fe 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 49 44  responding to ID
277ff 4c 49 53 54 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f  LIST. */.  int o
27800 6e 45 72 72 6f 72 20 20 20 20 20 20 20 20 20 20  nError          
27801 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c   /* How to handl
27802 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72  e constraint err
27803 6f 72 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ors */.){.  sqli
27804 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
27805 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61    /* The main da
27806 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
27807 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
27808 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  b;          /* T
27809 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65  he table to inse
2780a 72 74 20 69 6e 74 6f 2e 20 20 61 6b 61 20 54 41  rt into.  aka TA
2780b 42 4c 45 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  BLE */.  char *z
2780c 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Tab;           /
2780d 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
2780e 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
2780f 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
27810 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
27811 2a 7a 44 62 3b 20 20 20 20 20 20 2f 2a 20 4e 61  *zDb;      /* Na
27812 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
27813 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20  se holding this 
27814 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
27815 2c 20 6a 2c 20 69 64 78 3b 20 20 20 20 20 20 20  , j, idx;       
27816 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
27817 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  s */.  Vdbe *v; 
27818 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27819 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
2781a 74 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c 20  to this virtual 
2781b 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 49 6e 64  machine */.  Ind
2781c 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
2781d 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
2781e 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20 6f  g over indices o
2781f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  f the table */. 
27820 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20   int nColumn;   
27821 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
27822 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
27823 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74  he data */.  int
27824 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 20 20 20   nHidden = 0;   
27825 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
27826 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e 73 20 69  hidden columns i
27827 66 20 54 41 42 4c 45 20 69 73 20 76 69 72 74 75  f TABLE is virtu
27828 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65  al */.  int base
27829 43 75 72 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  Cur = 0;      /*
2782a 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d   VDBE Cursor num
2782b 62 65 72 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a  ber for pTab */.
2782c 20 20 69 6e 74 20 6b 65 79 43 6f 6c 75 6d 6e 20    int keyColumn 
2782d 3d 20 2d 31 3b 20 20 20 2f 2a 20 43 6f 6c 75 6d  = -1;   /* Colum
2782e 6e 20 74 68 61 74 20 69 73 20 74 68 65 20 49 4e  n that is the IN
2782f 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
27830 59 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f 66  Y */.  int endOf
27831 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 2f 2a 20  Loop;        /* 
27832 4c 61 62 65 6c 20 66 6f 72 20 74 68 65 20 65 6e  Label for the en
27833 64 20 6f 66 20 74 68 65 20 69 6e 73 65 72 74 69  d of the inserti
27834 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  on loop */.  int
27835 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 3d 20   useTempTable = 
27836 30 3b 20 2f 2a 20 53 74 6f 72 65 20 53 45 4c 45  0; /* Store SELE
27837 43 54 20 72 65 73 75 6c 74 73 20 69 6e 20 69 6e  CT results in in
27838 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
27839 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63 54 61 62   */.  int srcTab
2783a 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 44   = 0;       /* D
2783b 61 74 61 20 63 6f 6d 65 73 20 66 72 6f 6d 20 74  ata comes from t
2783c 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 63 75  his temporary cu
2783d 72 73 6f 72 20 69 66 20 3e 3d 30 20 2a 2f 0a 20  rsor if >=0 */. 
2783e 20 69 6e 74 20 61 64 64 72 49 6e 73 54 6f 70 20   int addrInsTop 
2783f 3d 20 30 3b 20 20 20 2f 2a 20 4a 75 6d 70 20 74  = 0;   /* Jump t
27840 6f 20 6c 61 62 65 6c 20 22 44 22 20 2a 2f 0a 20  o label "D" */. 
27841 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 20 3d 20   int addrCont = 
27842 30 3b 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66  0;     /* Top of
27843 20 69 6e 73 65 72 74 20 6c 6f 6f 70 2e 20 4c 61   insert loop. La
27844 62 65 6c 20 22 43 22 20 69 6e 20 74 65 6d 70 6c  bel "C" in templ
27845 61 74 65 73 20 33 20 61 6e 64 20 34 20 2a 2f 0a  ates 3 and 4 */.
27846 20 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74    int addrSelect
27847 20 3d 20 30 3b 20 20 20 2f 2a 20 41 64 64 72 65   = 0;   /* Addre
27848 73 73 20 6f 66 20 63 6f 72 6f 75 74 69 6e 65 20  ss of coroutine 
27849 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
2784a 74 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  the SELECT */.  
2784b 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
2784c 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61        /* Destina
2784d 74 69 6f 6e 20 66 6f 72 20 53 45 4c 45 43 54 20  tion for SELECT 
2784e 6f 6e 20 72 68 73 20 6f 66 20 49 4e 53 45 52 54  on rhs of INSERT
2784f 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
27850 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
27851 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65  ndex of database
27852 20 68 6f 6c 64 69 6e 67 20 54 41 42 4c 45 20 2a   holding TABLE *
27853 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20  /.  Db *pDb;    
27854 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
27855 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
27856 6e 69 6e 67 20 74 61 62 6c 65 20 62 65 69 6e 67  ning table being
27857 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 2a   inserted into *
27858 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 46 6c  /.  int appendFl
27859 61 67 20 3d 20 30 3b 20 20 20 2f 2a 20 54 72 75  ag = 0;   /* Tru
2785a 65 20 69 66 20 74 68 65 20 69 6e 73 65 72 74 20  e if the insert 
2785b 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  is likely to be 
2785c 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 0a 20 20  an append */..  
2785d 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 6f  /* Register allo
2785e 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  cations */.  int
2785f 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 20 3d   regFromSelect =
27860 20 30 3b 2f 2a 20 42 61 73 65 20 72 65 67 69 73   0;/* Base regis
27861 74 65 72 20 66 6f 72 20 64 61 74 61 20 63 6f 6d  ter for data com
27862 69 6e 67 20 66 72 6f 6d 20 53 45 4c 45 43 54 20  ing from SELECT 
27863 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 75 74 6f  */.  int regAuto
27864 69 6e 63 20 3d 20 30 3b 20 20 20 2f 2a 20 52 65  inc = 0;   /* Re
27865 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74  gister holding t
27866 68 65 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  he AUTOINCREMENT
27867 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
27868 74 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20  t regRowCount = 
27869 30 3b 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65  0;  /* Memory ce
2786a 6c 6c 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  ll used for the 
2786b 72 6f 77 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  row counter */. 
2786c 20 69 6e 74 20 72 65 67 49 6e 73 3b 20 20 20 20   int regIns;    
2786d 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20         /* Block 
2786e 6f 66 20 72 65 67 73 20 68 6f 6c 64 69 6e 67 20  of regs holding 
2786f 72 6f 77 69 64 2b 64 61 74 61 20 62 65 69 6e 67  rowid+data being
27870 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69   inserted */.  i
27871 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20  nt regRowid;    
27872 20 20 20 20 20 2f 2a 20 72 65 67 69 73 74 65 72       /* register
27873 73 20 68 6f 6c 64 69 6e 67 20 69 6e 73 65 72 74  s holding insert
27874 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20   rowid */.  int 
27875 72 65 67 44 61 74 61 3b 20 20 20 20 20 20 20 20  regData;        
27876 20 20 2f 2a 20 72 65 67 69 73 74 65 72 20 68 6f    /* register ho
27877 6c 64 69 6e 67 20 66 69 72 73 74 20 63 6f 6c 75  lding first colu
27878 6d 6e 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a  mn to insert */.
27879 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b    int regRecord;
2787a 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 6c 64 73          /* Holds
2787b 20 74 68 65 20 61 73 73 65 6d 62 6c 69 65 64 20   the assemblied 
2787c 72 6f 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  row record */.  
2787d 69 6e 74 20 72 65 67 45 6f 66 20 3d 20 30 3b 20  int regEof = 0; 
2787e 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
2787f 72 20 72 65 63 6f 72 64 69 6e 67 20 65 6e 64 20  r recording end 
27880 6f 66 20 53 45 4c 45 43 54 20 64 61 74 61 20 2a  of SELECT data *
27881 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78  /.  int *aRegIdx
27882 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4f 6e 65   = 0;     /* One
27883 20 72 65 67 69 73 74 65 72 20 61 6c 6c 6f 63 61   register alloca
27884 74 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65  ted to each inde
27885 78 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  x */..#ifndef SQ
27886 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
27887 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20  R.  int isView; 
27888 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27889 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74 65 6d  /* True if attem
2788a 70 74 69 6e 67 20 74 6f 20 69 6e 73 65 72 74 20  pting to insert 
2788b 69 6e 74 6f 20 61 20 76 69 65 77 20 2a 2f 0a 20  into a view */. 
2788c 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
2788d 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er;          /* 
2788e 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73  List of triggers
2788f 20 6f 6e 20 70 54 61 62 2c 20 69 66 20 72 65 71   on pTab, if req
27890 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74  uired */.  int t
27891 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  mask;           
27892 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
27893 66 20 74 72 69 67 67 65 72 20 74 69 6d 65 73 20  f trigger times 
27894 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62 20  */.#endif..  db 
27895 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
27896 6d 65 6d 73 65 74 28 26 64 65 73 74 2c 20 30 2c  memset(&dest, 0,
27897 20 73 69 7a 65 6f 66 28 64 65 73 74 29 29 3b 0a   sizeof(dest));.
27898 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
27899 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
2789a 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
2789b 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
2789c 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63  p;.  }..  /* Loc
2789d 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 69 6e  ate the table in
2789e 74 6f 20 77 68 69 63 68 20 77 65 20 77 69 6c 6c  to which we will
2789f 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20 6e 65   be inserting ne
278a0 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20  w information.. 
278a1 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
278a2 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20  abList->nSrc==1 
278a3 29 3b 0a 20 20 7a 54 61 62 20 3d 20 70 54 61 62  );.  zTab = pTab
278a4 4c 69 73 74 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  List->a[0].zName
278a5 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 7a 54  ;.  if( NEVER(zT
278a6 61 62 3d 3d 30 29 20 29 20 67 6f 74 6f 20 69 6e  ab==0) ) goto in
278a7 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
278a8 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72  pTab = sqlite3Sr
278a9 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72  cListLookup(pPar
278aa 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20  se, pTabList);. 
278ab 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a   if( pTab==0 ){.
278ac 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
278ad 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
278ae 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
278af 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
278b0 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
278b1 61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e  assert( iDb<db->
278b2 6e 44 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26  nDb );.  pDb = &
278b3 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20  db->aDb[iDb];.  
278b4 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65  zDb = pDb->zName
278b5 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  ;.  if( sqlite3A
278b6 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
278b7 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20   SQLITE_INSERT, 
278b8 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20  pTab->zName, 0, 
278b9 7a 44 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  zDb) ){.    goto
278ba 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
278bb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72  .  }..  /* Figur
278bc 65 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65  e out if we have
278bd 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e   any triggers an
278be 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62  d if the table b
278bf 65 69 6e 67 0a 20 20 2a 2a 20 69 6e 73 65 72 74  eing.  ** insert
278c0 65 64 20 69 6e 74 6f 20 69 73 20 61 20 76 69 65  ed into is a vie
278c1 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  w.  */.#ifndef S
278c2 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
278c3 45 52 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20  ER.  pTrigger = 
278c4 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45  sqlite3TriggersE
278c5 78 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  xist(pParse, pTa
278c6 62 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c  b, TK_INSERT, 0,
278c7 20 26 74 6d 61 73 6b 29 3b 0a 20 20 69 73 56 69   &tmask);.  isVi
278c8 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65  ew = pTab->pSele
278c9 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64  ct!=0;.#else.# d
278ca 65 66 69 6e 65 20 70 54 72 69 67 67 65 72 20 30  efine pTrigger 0
278cb 0a 23 20 64 65 66 69 6e 65 20 74 6d 61 73 6b 20  .# define tmask 
278cc 30 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69 65  0.# define isVie
278cd 77 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  w 0.#endif.#ifde
278ce 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
278cf 45 57 0a 23 20 75 6e 64 65 66 20 69 73 56 69 65  EW.# undef isVie
278d0 77 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69 65  w.# define isVie
278d1 77 20 30 0a 23 65 6e 64 69 66 0a 20 20 61 73 73  w 0.#endif.  ass
278d2 65 72 74 28 20 28 70 54 72 69 67 67 65 72 20 26  ert( (pTrigger &
278d3 26 20 74 6d 61 73 6b 29 20 7c 7c 20 28 70 54 72  & tmask) || (pTr
278d4 69 67 67 65 72 3d 3d 30 20 26 26 20 74 6d 61 73  igger==0 && tmas
278d5 6b 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  k==0) );..  /* I
278d6 66 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79  f pTab is really
278d7 20 61 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75   a view, make su
278d8 72 65 20 69 74 20 68 61 73 20 62 65 65 6e 20 69  re it has been i
278d9 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a  nitialized..  **
278da 20 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61   ViewGetColumnNa
278db 6d 65 73 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70  mes() is a no-op
278dc 20 69 66 20 70 54 61 62 20 69 73 20 6e 6f 74 20   if pTab is not 
278dd 61 20 76 69 65 77 20 28 6f 72 20 76 69 72 74 75  a view (or virtu
278de 61 6c 20 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20  al .  ** module 
278df 74 61 62 6c 65 29 2e 0a 20 20 2a 2f 0a 20 20 69  table)..  */.  i
278e0 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65  f( sqlite3ViewGe
278e1 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  tColumnNames(pPa
278e2 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20  rse, pTab) ){.  
278e3 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
278e4 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
278e5 20 45 6e 73 75 72 65 20 74 68 61 74 3a 0a 20 20   Ensure that:.  
278e6 2a 20 20 28 61 29 20 74 68 65 20 74 61 62 6c 65  *  (a) the table
278e7 20 69 73 20 6e 6f 74 20 72 65 61 64 2d 6f 6e 6c   is not read-onl
278e8 79 2c 20 0a 20 20 2a 20 20 28 62 29 20 74 68 61  y, .  *  (b) tha
278e9 74 20 69 66 20 69 74 20 69 73 20 61 20 76 69 65  t if it is a vie
278ea 77 20 74 68 65 6e 20 4f 4e 20 49 4e 53 45 52 54  w then ON INSERT
278eb 20 74 72 69 67 67 65 72 73 20 65 78 69 73 74 0a   triggers exist.
278ec 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
278ed 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61  e3IsReadOnly(pPa
278ee 72 73 65 2c 20 70 54 61 62 2c 20 74 6d 61 73 6b  rse, pTab, tmask
278ef 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  ) ){.    goto in
278f0 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
278f1 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
278f2 20 61 20 56 44 42 45 0a 20 20 2a 2f 0a 20 20 76   a VDBE.  */.  v
278f3 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
278f4 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
278f5 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 69 6e 73   v==0 ) goto ins
278f6 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69  ert_cleanup;.  i
278f7 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  f( pParse->neste
278f8 64 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64  d==0 ) sqlite3Vd
278f9 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 76  beCountChanges(v
278fa 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  );.  sqlite3Begi
278fb 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
278fc 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 20  pParse, pSelect 
278fd 7c 7c 20 70 54 72 69 67 67 65 72 2c 20 69 44 62  || pTrigger, iDb
278fe 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
278ff 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54  TE_OMIT_XFER_OPT
27900 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61  .  /* If the sta
27901 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65  tement is of the
27902 20 66 6f 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20   form.  **.  ** 
27903 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
27904 4f 20 3c 74 61 62 6c 65 31 3e 20 53 45 4c 45 43  O <table1> SELEC
27905 54 20 2a 20 46 52 4f 4d 20 3c 74 61 62 6c 65 32  T * FROM <table2
27906 3e 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  >;.  **.  ** The
27907 6e 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d 69  n special optimi
27908 7a 61 74 69 6f 6e 73 20 63 61 6e 20 62 65 20 61  zations can be a
27909 70 70 6c 69 65 64 20 74 68 61 74 20 6d 61 6b 65  pplied that make
2790a 20 74 68 65 20 74 72 61 6e 73 66 65 72 0a 20 20   the transfer.  
2790b 2a 2a 20 76 65 72 79 20 66 61 73 74 20 61 6e 64  ** very fast and
2790c 20 77 68 69 63 68 20 72 65 64 75 63 65 20 66 72   which reduce fr
2790d 61 67 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 69  agmentation of i
2790e 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a  ndices..  **.  *
2790f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 32 6e  * This is the 2n
27910 64 20 74 65 6d 70 6c 61 74 65 2e 0a 20 20 2a 2f  d template..  */
27911 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d  .  if( pColumn==
27912 30 20 26 26 20 78 66 65 72 4f 70 74 69 6d 69 7a  0 && xferOptimiz
27913 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 54  ation(pParse, pT
27914 61 62 2c 20 70 53 65 6c 65 63 74 2c 20 6f 6e 45  ab, pSelect, onE
27915 72 72 6f 72 2c 20 69 44 62 29 20 29 7b 0a 20 20  rror, iDb) ){.  
27916 20 20 61 73 73 65 72 74 28 20 21 70 54 72 69 67    assert( !pTrig
27917 67 65 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ger );.    asser
27918 74 28 20 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20  t( pList==0 );. 
27919 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 65     goto insert_e
2791a 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  nd;.  }.#endif /
2791b 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46  * SQLITE_OMIT_XF
2791c 45 52 5f 4f 50 54 20 2a 2f 0a 0a 20 20 2f 2a 20  ER_OPT */..  /* 
2791d 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 41 55  If this is an AU
2791e 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c  TOINCREMENT tabl
2791f 65 2c 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73  e, look up the s
27920 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69  equence number i
27921 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74  n the.  ** sqlit
27922 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
27923 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e   and store it in
27924 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 72 65 67   memory cell reg
27925 41 75 74 6f 69 6e 63 2e 0a 20 20 2a 2f 0a 20 20  Autoinc..  */.  
27926 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 61 75 74  regAutoinc = aut
27927 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73 65  oIncBegin(pParse
27928 2c 20 69 44 62 2c 20 70 54 61 62 29 3b 0a 0a 20  , iDb, pTab);.. 
27929 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
2792a 6f 77 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20  ow many columns 
2792b 6f 66 20 64 61 74 61 20 61 72 65 20 73 75 70 70  of data are supp
2792c 6c 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 61  lied.  If the da
2792d 74 61 0a 20 20 2a 2a 20 69 73 20 63 6f 6d 69 6e  ta.  ** is comin
2792e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20  g from a SELECT 
2792f 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20  statement, then 
27930 67 65 6e 65 72 61 74 65 20 61 20 63 6f 2d 72 6f  generate a co-ro
27931 75 74 69 6e 65 20 74 68 61 74 0a 20 20 2a 2a 20  utine that.  ** 
27932 70 72 6f 64 75 63 65 73 20 61 20 73 69 6e 67 6c  produces a singl
27933 65 20 72 6f 77 20 6f 66 20 74 68 65 20 53 45 4c  e row of the SEL
27934 45 43 54 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f  ECT on each invo
27935 63 61 74 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a  cation.  The.  *
27936 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73 20  * co-routine is 
27937 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65  the common heade
27938 72 20 74 6f 20 74 68 65 20 33 72 64 20 61 6e 64  r to the 3rd and
27939 20 34 74 68 20 74 65 6d 70 6c 61 74 65 73 2e 0a   4th templates..
2793a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65    */.  if( pSele
2793b 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20 44 61 74  ct ){.    /* Dat
2793c 61 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d  a is coming from
2793d 20 61 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65   a SELECT.  Gene
2793e 72 61 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70  rate code to imp
2793f 6c 65 6d 65 6e 74 20 74 68 61 74 20 53 45 4c 45  lement that SELE
27940 43 54 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 63  CT.    ** as a c
27941 6f 2d 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20  o-routine.  The 
27942 63 6f 64 65 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  code is common t
27943 6f 20 62 6f 74 68 20 74 68 65 20 33 72 64 20 61  o both the 3rd a
27944 6e 64 20 34 74 68 0a 20 20 20 20 2a 2a 20 74 65  nd 4th.    ** te
27945 6d 70 6c 61 74 65 73 3a 0a 20 20 20 20 2a 2a 0a  mplates:.    **.
27946 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 45      **         E
27947 4f 46 20 3c 2d 20 30 0a 20 20 20 20 2a 2a 20 20  OF <- 0.    **  
27948 20 20 20 20 20 20 20 58 20 3c 2d 20 41 0a 20 20         X <- A.  
27949 20 20 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74    **         got
2794a 6f 20 42 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  o B.    **      
2794b 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65  A: setup for the
2794c 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 20   SELECT.    **  
2794d 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72         loop over
2794e 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74   the tables in t
2794f 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a  he SELECT.    **
27950 20 20 20 20 20 20 20 20 20 20 20 6c 6f 61 64 20             load 
27951 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73  value into regis
27952 74 65 72 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a  ter R..R+n.    *
27953 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65 6c  *           yiel
27954 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  d X.    **      
27955 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20 20 20 20     end loop.    
27956 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
27957 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c  up after the SEL
27958 45 43 54 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ECT.    **      
27959 20 20 20 45 4f 46 20 3c 2d 20 31 0a 20 20 20 20     EOF <- 1.    
2795a 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c 64  **         yield
2795b 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   X.    **       
2795c 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a 20 20 20    halt-error.   
2795d 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e 20 65 61   **.    ** On ea
2795e 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  ch invocation of
2795f 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 2c   the co-routine,
27960 20 69 74 20 70 75 74 73 20 61 20 73 69 6e 67 6c   it puts a singl
27961 65 20 72 6f 77 20 6f 66 20 74 68 65 0a 20 20 20  e row of the.   
27962 20 2a 2a 20 53 45 4c 45 43 54 20 72 65 73 75 6c   ** SELECT resul
27963 74 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73  t into registers
27964 20 64 65 73 74 2e 69 4d 65 6d 2e 2e 2e 64 65 73   dest.iMem...des
27965 74 2e 69 4d 65 6d 2b 64 65 73 74 2e 6e 4d 65 6d  t.iMem+dest.nMem
27966 2d 31 2e 0a 20 20 20 20 2a 2a 20 28 54 68 65 73  -1..    ** (Thes
27967 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65  e output registe
27968 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  rs are allocated
27969 20 62 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63   by sqlite3Selec
2796a 74 28 29 2e 29 20 20 57 68 65 6e 0a 20 20 20 20  t().)  When.    
2796b 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 63 6f  ** the SELECT co
2796c 6d 70 6c 65 74 65 73 2c 20 69 74 20 73 65 74 73  mpletes, it sets
2796d 20 74 68 65 20 45 4f 46 20 66 6c 61 67 20 73 74   the EOF flag st
2796e 6f 72 65 64 20 69 6e 20 72 65 67 45 6f 66 2e 0a  ored in regEof..
2796f 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72      */.    int r
27970 63 2c 20 6a 31 3b 0a 0a 20 20 20 20 72 65 67 45  c, j1;..    regE
27971 6f 66 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  of = ++pParse->n
27972 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
27973 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27974 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67  _Integer, 0, reg
27975 45 6f 66 29 3b 20 20 20 20 20 20 2f 2a 20 45 4f  Eof);      /* EO
27976 46 20 3c 2d 20 30 20 2a 2f 0a 20 20 20 20 56 64  F <- 0 */.    Vd
27977 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 53  beComment((v, "S
27978 45 4c 45 43 54 20 65 6f 66 20 66 6c 61 67 22 29  ELECT eof flag")
27979 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  );.    sqlite3Se
2797a 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
2797b 73 74 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e  st, SRT_Coroutin
2797c 65 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65  e, ++pParse->nMe
2797d 6d 29 3b 0a 20 20 20 20 61 64 64 72 53 65 6c 65  m);.    addrSele
2797e 63 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ct = sqlite3Vdbe
2797f 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b 32  CurrentAddr(v)+2
27980 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27981 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
27982 74 65 67 65 72 2c 20 61 64 64 72 53 65 6c 65 63  teger, addrSelec
27983 74 2d 31 2c 20 64 65 73 74 2e 69 50 61 72 6d 29  t-1, dest.iParm)
27984 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74  ;.    j1 = sqlit
27985 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27986 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a  OP_Goto, 0, 0);.
27987 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
27988 28 76 2c 20 22 4a 75 6d 70 20 6f 76 65 72 20 53  (v, "Jump over S
27989 45 4c 45 43 54 20 63 6f 72 6f 75 74 69 6e 65 22  ELECT coroutine"
2798a 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f  ));..    /* Reso
2798b 6c 76 65 20 74 68 65 20 65 78 70 72 65 73 73 69  lve the expressi
2798c 6f 6e 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43  ons in the SELEC
2798d 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  T statement and 
2798e 65 78 65 63 75 74 65 20 69 74 2e 20 2a 2f 0a 20  execute it. */. 
2798f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
27990 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53  elect(pParse, pS
27991 65 6c 65 63 74 2c 20 26 64 65 73 74 29 3b 0a 20  elect, &dest);. 
27992 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73     assert( pPars
27993 65 2d 3e 6e 45 72 72 3d 3d 30 20 7c 7c 20 72 63  e->nErr==0 || rc
27994 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 7c   );.    if( rc |
27995 7c 20 4e 45 56 45 52 28 70 50 61 72 73 65 2d 3e  | NEVER(pParse->
27996 6e 45 72 72 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c  nErr) || db->mal
27997 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
27998 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
27999 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20  leanup;.    }.  
2799a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2799b 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2799c 72 2c 20 31 2c 20 72 65 67 45 6f 66 29 3b 20 20  r, 1, regEof);  
2799d 20 20 20 20 20 20 20 2f 2a 20 45 4f 46 20 3c 2d         /* EOF <-
2799e 20 31 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65   1 */.    sqlite
2799f 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
279a0 50 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50  P_Yield, dest.iP
279a1 61 72 6d 29 3b 20 20 20 2f 2a 20 79 69 65 6c 64  arm);   /* yield
279a2 20 58 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65   X */.    sqlite
279a3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
279a4 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 49  P_Halt, SQLITE_I
279a5 4e 54 45 52 4e 41 4c 2c 20 4f 45 5f 41 62 6f 72  NTERNAL, OE_Abor
279a6 74 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  t);.    VdbeComm
279a7 65 6e 74 28 28 76 2c 20 22 45 6e 64 20 6f 66 20  ent((v, "End of 
279a8 53 45 4c 45 43 54 20 63 6f 72 6f 75 74 69 6e 65  SELECT coroutine
279a9 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  "));.    sqlite3
279aa 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
279ab 6a 31 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  j1);            
279ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
279ad 20 6c 61 62 65 6c 20 42 3a 20 2a 2f 0a 0a 20 20   label B: */..  
279ae 20 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 20    regFromSelect 
279af 3d 20 64 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 20  = dest.iMem;.   
279b0 20 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74   assert( pSelect
279b1 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20  ->pEList );.    
279b2 6e 43 6f 6c 75 6d 6e 20 3d 20 70 53 65 6c 65 63  nColumn = pSelec
279b3 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  t->pEList->nExpr
279b4 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65  ;.    assert( de
279b5 73 74 2e 6e 4d 65 6d 3d 3d 6e 43 6f 6c 75 6d 6e  st.nMem==nColumn
279b6 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20   );..    /* Set 
279b7 75 73 65 54 65 6d 70 54 61 62 6c 65 20 74 6f 20  useTempTable to 
279b8 54 52 55 45 20 69 66 20 74 68 65 20 72 65 73 75  TRUE if the resu
279b9 6c 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  lt of the SELECT
279ba 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a   statement.    *
279bb 2a 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74  * should be writ
279bc 74 65 6e 20 69 6e 74 6f 20 61 20 74 65 6d 70 6f  ten into a tempo
279bd 72 61 72 79 20 74 61 62 6c 65 20 28 74 65 6d 70  rary table (temp
279be 6c 61 74 65 20 34 29 2e 20 20 53 65 74 20 74 6f  late 4).  Set to
279bf 0a 20 20 20 20 2a 2a 20 46 41 4c 53 45 20 69 66  .    ** FALSE if
279c0 20 65 61 63 68 2a 20 72 6f 77 20 6f 66 20 74 68   each* row of th
279c1 65 20 53 45 4c 45 43 54 20 63 61 6e 20 62 65 20  e SELECT can be 
279c2 77 72 69 74 74 65 6e 20 64 69 72 65 63 74 6c 79  written directly
279c3 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65   into.    ** the
279c4 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 74 61 62   destination tab
279c5 6c 65 20 28 74 65 6d 70 6c 61 74 65 20 33 29 2e  le (template 3).
279c6 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41  .    **.    ** A
279c7 20 74 65 6d 70 20 74 61 62 6c 65 20 6d 75 73 74   temp table must
279c8 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65 20   be used if the 
279c9 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61  table being upda
279ca 74 65 64 20 69 73 20 61 6c 73 6f 20 6f 6e 65 0a  ted is also one.
279cb 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61      ** of the ta
279cc 62 6c 65 73 20 62 65 69 6e 67 20 72 65 61 64 20  bles being read 
279cd 62 79 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  by the SELECT st
279ce 61 74 65 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 75  atement.  Also u
279cf 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 74 65 6d  se a .    ** tem
279d0 70 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 63  p table in the c
279d1 61 73 65 20 6f 66 20 72 6f 77 20 74 72 69 67 67  ase of row trigg
279d2 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ers..    */.    
279d3 69 66 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20  if( pTrigger || 
279d4 72 65 61 64 73 54 61 62 6c 65 28 70 50 61 72 73  readsTable(pPars
279d5 65 2c 20 61 64 64 72 53 65 6c 65 63 74 2c 20 69  e, addrSelect, i
279d6 44 62 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  Db, pTab) ){.   
279d7 20 20 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20     useTempTable 
279d8 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 1;.    }..    
279d9 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65  if( useTempTable
279da 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76   ){.      /* Inv
279db 6f 6b 65 20 74 68 65 20 63 6f 72 6f 75 74 69 6e  oke the coroutin
279dc 65 20 74 6f 20 65 78 74 72 61 63 74 20 69 6e 66  e to extract inf
279dd 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68  ormation from th
279de 65 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a  e SELECT.      *
279df 2a 20 61 6e 64 20 61 64 64 20 69 74 20 74 6f 20  * and add it to 
279e0 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c  a transient tabl
279e1 65 20 73 72 63 54 61 62 2e 20 20 54 68 65 20 63  e srcTab.  The c
279e2 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a 20 20  ode generated.  
279e3 20 20 20 20 2a 2a 20 68 65 72 65 20 69 73 20 66      ** here is f
279e4 72 6f 6d 20 74 68 65 20 34 74 68 20 74 65 6d 70  rom the 4th temp
279e5 6c 61 74 65 3a 0a 20 20 20 20 20 20 2a 2a 0a 20  late:.      **. 
279e6 20 20 20 20 20 2a 2a 20 20 20 20 20 20 42 3a 20       **      B: 
279e7 6f 70 65 6e 20 74 65 6d 70 20 74 61 62 6c 65 0a  open temp table.
279e8 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 4c 3a        **      L:
279e9 20 79 69 65 6c 64 20 58 0a 20 20 20 20 20 20 2a   yield X.      *
279ea 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f 46  *         if EOF
279eb 20 67 6f 74 6f 20 4d 0a 20 20 20 20 20 20 2a 2a   goto M.      **
279ec 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 20           insert 
279ed 72 6f 77 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 20  row from R..R+n 
279ee 69 6e 74 6f 20 74 65 6d 70 20 74 61 62 6c 65 0a  into temp table.
279ef 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
279f0 20 67 6f 74 6f 20 4c 0a 20 20 20 20 20 20 2a 2a   goto L.      **
279f1 20 20 20 20 20 20 4d 3a 20 2e 2e 2e 0a 20 20 20        M: ....   
279f2 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20     */.      int 
279f3 72 65 67 52 65 63 3b 20 20 20 20 20 20 20 20 20  regRec;         
279f4 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20   /* Register to 
279f5 68 6f 6c 64 20 70 61 63 6b 65 64 20 72 65 63 6f  hold packed reco
279f6 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  rd */.      int 
279f7 72 65 67 54 65 6d 70 52 6f 77 69 64 3b 20 20 20  regTempRowid;   
279f8 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20   /* Register to 
279f9 68 6f 6c 64 20 74 65 6d 70 20 74 61 62 6c 65 20  hold temp table 
279fa 52 4f 57 49 44 20 2a 2f 0a 20 20 20 20 20 20 69  ROWID */.      i
279fb 6e 74 20 61 64 64 72 54 6f 70 3b 20 20 20 20 20  nt addrTop;     
279fc 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 22 4c 22      /* Label "L"
279fd 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
279fe 64 72 49 66 3b 20 20 20 20 20 20 20 20 20 20 2f  drIf;          /
279ff 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6a 75 6d  * Address of jum
27a00 70 20 74 6f 20 4d 20 2a 2f 0a 0a 20 20 20 20 20  p to M */..     
27a01 20 73 72 63 54 61 62 20 3d 20 70 50 61 72 73 65   srcTab = pParse
27a02 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20  ->nTab++;.      
27a03 72 65 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33  regRec = sqlite3
27a04 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
27a05 65 29 3b 0a 20 20 20 20 20 20 72 65 67 54 65 6d  e);.      regTem
27a06 70 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  pRowid = sqlite3
27a07 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
27a08 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
27a09 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27a0a 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
27a0b 20 73 72 63 54 61 62 2c 20 6e 43 6f 6c 75 6d 6e   srcTab, nColumn
27a0c 29 3b 0a 20 20 20 20 20 20 61 64 64 72 54 6f 70  );.      addrTop
27a0d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
27a0e 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
27a0f 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20  , dest.iParm);. 
27a10 20 20 20 20 20 61 64 64 72 49 66 20 3d 20 73 71       addrIf = sq
27a11 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
27a12 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
27a13 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27a14 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
27a15 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
27a16 46 72 6f 6d 53 65 6c 65 63 74 2c 20 6e 43 6f 6c  FromSelect, nCol
27a17 75 6d 6e 2c 20 72 65 67 52 65 63 29 3b 0a 20 20  umn, regRec);.  
27a18 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27a19 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
27a1a 6f 77 69 64 2c 20 73 72 63 54 61 62 2c 20 72 65  owid, srcTab, re
27a1b 67 54 65 6d 70 52 6f 77 69 64 29 3b 0a 20 20 20  gTempRowid);.   
27a1c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27a1d 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
27a1e 74 2c 20 73 72 63 54 61 62 2c 20 72 65 67 52 65  t, srcTab, regRe
27a1f 63 2c 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29  c, regTempRowid)
27a20 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27a21 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27a22 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 54 6f 70  Goto, 0, addrTop
27a23 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27a24 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
27a25 61 64 64 72 49 66 29 3b 0a 20 20 20 20 20 20 73  addrIf);.      s
27a26 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
27a27 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
27a28 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Rec);.      sqli
27a29 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
27a2a 67 28 70 50 61 72 73 65 2c 20 72 65 67 54 65 6d  g(pParse, regTem
27a2b 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20  pRowid);.    }. 
27a2c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
27a2d 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 20  his is the case 
27a2e 69 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 20  if the data for 
27a2f 74 68 65 20 49 4e 53 45 52 54 20 69 73 20 63 6f  the INSERT is co
27a30 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55  ming from a VALU
27a31 45 53 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73 65  ES.    ** clause
27a32 0a 20 20 20 20 2a 2f 0a 20 20 20 20 4e 61 6d 65  .    */.    Name
27a33 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20  Context sNC;.   
27a34 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
27a35 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
27a36 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20     sNC.pParse = 
27a37 70 50 61 72 73 65 3b 0a 20 20 20 20 73 72 63 54  pParse;.    srcT
27a38 61 62 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73  ab = -1;.    ass
27a39 65 72 74 28 20 75 73 65 54 65 6d 70 54 61 62 6c  ert( useTempTabl
27a3a 65 3d 3d 30 20 29 3b 0a 20 20 20 20 6e 43 6f 6c  e==0 );.    nCol
27a3b 75 6d 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c  umn = pList ? pL
27a3c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a  ist->nExpr : 0;.
27a3d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
27a3e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
27a3f 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
27a40 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
27a41 26 73 4e 43 2c 20 70 4c 69 73 74 2d 3e 61 5b 69  &sNC, pList->a[i
27a42 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ].pExpr) ){.    
27a43 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
27a44 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d  cleanup;.      }
27a45 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
27a46 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e   Make sure the n
27a47 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
27a48 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
27a49 61 74 61 20 6d 61 74 63 68 65 73 20 74 68 65 20  ata matches the 
27a4a 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 63  number.  ** of c
27a4b 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 73  olumns to be ins
27a4c 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74  erted into the t
27a4d 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  able..  */.  if(
27a4e 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
27a4f 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
27a50 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
27a51 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 48 69 64 64  ++){.      nHidd
27a52 65 6e 20 2b 3d 20 28 49 73 48 69 64 64 65 6e 43  en += (IsHiddenC
27a53 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f  olumn(&pTab->aCo
27a54 6c 5b 69 5d 29 20 3f 20 31 20 3a 20 30 29 3b 0a  l[i]) ? 1 : 0);.
27a55 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
27a56 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43  pColumn==0 && nC
27a57 6f 6c 75 6d 6e 20 26 26 20 6e 43 6f 6c 75 6d 6e  olumn && nColumn
27a58 21 3d 28 70 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48  !=(pTab->nCol-nH
27a59 69 64 64 65 6e 29 20 29 7b 0a 20 20 20 20 73 71  idden) ){.    sq
27a5a 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
27a5b 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 22 74  arse, .       "t
27a5c 61 62 6c 65 20 25 53 20 68 61 73 20 25 64 20 63  able %S has %d c
27a5d 6f 6c 75 6d 6e 73 20 62 75 74 20 25 64 20 76 61  olumns but %d va
27a5e 6c 75 65 73 20 77 65 72 65 20 73 75 70 70 6c 69  lues were suppli
27a5f 65 64 22 2c 0a 20 20 20 20 20 20 20 70 54 61 62  ed",.       pTab
27a60 4c 69 73 74 2c 20 30 2c 20 70 54 61 62 2d 3e 6e  List, 0, pTab->n
27a61 43 6f 6c 2d 6e 48 69 64 64 65 6e 2c 20 6e 43 6f  Col-nHidden, nCo
27a62 6c 75 6d 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20  lumn);.    goto 
27a63 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
27a64 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d    }.  if( pColum
27a65 6e 21 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 21  n!=0 && nColumn!
27a66 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b  =pColumn->nId ){
27a67 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
27a68 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 64  rMsg(pParse, "%d
27a69 20 76 61 6c 75 65 73 20 66 6f 72 20 25 64 20 63   values for %d c
27a6a 6f 6c 75 6d 6e 73 22 2c 20 6e 43 6f 6c 75 6d 6e  olumns", nColumn
27a6b 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 3b  , pColumn->nId);
27a6c 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
27a6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
27a6e 20 2f 2a 20 49 66 20 74 68 65 20 49 4e 53 45 52   /* If the INSER
27a6f 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 63 6c  T statement incl
27a70 75 64 65 64 20 61 6e 20 49 44 4c 49 53 54 20 74  uded an IDLIST t
27a71 65 72 6d 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73  erm, then make s
27a72 75 72 65 0a 20 20 2a 2a 20 61 6c 6c 20 65 6c 65  ure.  ** all ele
27a73 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 49 44 4c  ments of the IDL
27a74 49 53 54 20 72 65 61 6c 6c 79 20 61 72 65 20 63  IST really are c
27a75 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61  olumns of the ta
27a76 62 6c 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65  ble and .  ** re
27a77 6d 65 6d 62 65 72 20 74 68 65 20 63 6f 6c 75 6d  member the colum
27a78 6e 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a  n indices..  **.
27a79 20 20 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c    ** If the tabl
27a7a 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52  e has an INTEGER
27a7b 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
27a7c 75 6d 6e 20 61 6e 64 20 74 68 61 74 20 63 6f 6c  umn and that col
27a7d 75 6d 6e 0a 20 20 2a 2a 20 69 73 20 6e 61 6d 65  umn.  ** is name
27a7e 64 20 69 6e 20 74 68 65 20 49 44 4c 49 53 54 2c  d in the IDLIST,
27a7f 20 74 68 65 6e 20 72 65 63 6f 72 64 20 69 6e 20   then record in 
27a80 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61  the keyColumn va
27a81 72 69 61 62 6c 65 0a 20 20 2a 2a 20 74 68 65 20  riable.  ** the 
27a82 69 6e 64 65 78 20 69 6e 74 6f 20 49 44 4c 49 53  index into IDLIS
27a83 54 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79  T of the primary
27a84 20 6b 65 79 20 63 6f 6c 75 6d 6e 2e 20 20 6b 65   key column.  ke
27a85 79 43 6f 6c 75 6d 6e 20 69 73 0a 20 20 2a 2a 20  yColumn is.  ** 
27a86 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
27a87 20 70 72 69 6d 61 72 79 20 6b 65 79 20 61 73 20   primary key as 
27a88 69 74 20 61 70 70 65 61 72 73 20 69 6e 20 49 44  it appears in ID
27a89 4c 49 53 54 2c 20 6e 6f 74 20 61 73 0a 20 20 2a  LIST, not as.  *
27a8a 2a 20 69 73 20 61 70 70 65 61 72 73 20 69 6e 20  * is appears in 
27a8b 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62  the original tab
27a8c 6c 65 2e 20 20 28 54 68 65 20 69 6e 64 65 78 20  le.  (The index 
27a8d 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79 0a 20  of the primary. 
27a8e 20 2a 2a 20 6b 65 79 20 69 6e 20 74 68 65 20 6f   ** key in the o
27a8f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 20 69 73  riginal table is
27a90 20 70 54 61 62 2d 3e 69 50 4b 65 79 2e 29 0a 20   pTab->iPKey.). 
27a91 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
27a92 6e 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  n ){.    for(i=0
27a93 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64  ; i<pColumn->nId
27a94 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43  ; i++){.      pC
27a95 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20  olumn->a[i].idx 
27a96 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  = -1;.    }.    
27a97 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75  for(i=0; i<pColu
27a98 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20  mn->nId; i++){. 
27a99 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
27a9a 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  pTab->nCol; j++)
27a9b 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  {.        if( sq
27a9c 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
27a9d 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  lumn->a[i].zName
27a9e 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e  , pTab->aCol[j].
27a9f 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
27aa0 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e         pColumn->
27aa1 61 5b 69 5d 2e 69 64 78 20 3d 20 6a 3b 0a 20 20  a[i].idx = j;.  
27aa2 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70          if( j==p
27aa3 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
27aa4 20 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c            keyCol
27aa5 75 6d 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20  umn = i;.       
27aa6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
27aa7 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
27aa8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
27aa9 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20  ( j>=pTab->nCol 
27aaa 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
27aab 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 70 43  qlite3IsRowid(pC
27aac 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
27aad 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  e) ){.          
27aae 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20  keyColumn = i;. 
27aaf 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
27ab0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
27ab1 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
27ab2 22 74 61 62 6c 65 20 25 53 20 68 61 73 20 6e 6f  "table %S has no
27ab3 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73   column named %s
27ab4 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
27ab5 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70 43   pTabList, 0, pC
27ab6 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
27ab7 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50  e);.          pP
27ab8 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
27ab9 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73          goto ins
27aba 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ert_cleanup;.   
27abb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
27abc 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
27abd 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 49 44  f there is no ID
27abe 4c 49 53 54 20 74 65 72 6d 20 62 75 74 20 74 68  LIST term but th
27abf 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 69  e table has an i
27ac0 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 0a 20  nteger primary. 
27ac1 20 2a 2a 20 6b 65 79 2c 20 74 68 65 20 73 65 74   ** key, the set
27ac2 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76   the keyColumn v
27ac3 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 70  ariable to the p
27ac4 72 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d  rimary key colum
27ac5 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20  n index.  ** in 
27ac6 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62  the original tab
27ac7 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20  le definition.. 
27ac8 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
27ac9 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 3e  n==0 && nColumn>
27aca 30 20 29 7b 0a 20 20 20 20 6b 65 79 43 6f 6c 75  0 ){.    keyColu
27acb 6d 6e 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79  mn = pTab->iPKey
27acc 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20  ;.  }.    .  /* 
27acd 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63  Initialize the c
27ace 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74 6f 20  ount of rows to 
27acf 62 65 20 69 6e 73 65 72 74 65 64 0a 20 20 2a 2f  be inserted.  */
27ad0 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
27ad1 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52   & SQLITE_CountR
27ad2 6f 77 73 20 29 7b 0a 20 20 20 20 72 65 67 52 6f  ows ){.    regRo
27ad3 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61 72 73  wCount = ++pPars
27ad4 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
27ad5 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27ad6 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
27ad7 20 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b 0a 20   regRowCount);. 
27ad8 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73   }..  /* If this
27ad9 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 2c 20   is not a view, 
27ada 6f 70 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61  open the table a
27adb 6e 64 20 61 6e 64 20 61 6c 6c 20 69 6e 64 69 63  nd and all indic
27adc 65 73 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 56  es */.  if( !isV
27add 69 65 77 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  iew ){.    int n
27ade 49 64 78 3b 0a 0a 20 20 20 20 62 61 73 65 43 75  Idx;..    baseCu
27adf 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
27ae0 3b 0a 20 20 20 20 6e 49 64 78 20 3d 20 73 71 6c  ;.    nIdx = sql
27ae1 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
27ae2 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20  Indices(pParse, 
27ae3 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 4f  pTab, baseCur, O
27ae4 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20  P_OpenWrite);.  
27ae5 20 20 61 52 65 67 49 64 78 20 3d 20 73 71 6c 69    aRegIdx = sqli
27ae6 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
27ae7 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28  b, sizeof(int)*(
27ae8 6e 49 64 78 2b 31 29 29 3b 0a 20 20 20 20 69 66  nIdx+1));.    if
27ae9 28 20 61 52 65 67 49 64 78 3d 3d 30 20 29 7b 0a  ( aRegIdx==0 ){.
27aea 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72        goto inser
27aeb 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d  t_cleanup;.    }
27aec 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
27aed 6e 49 64 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nIdx; i++){.    
27aee 20 20 61 52 65 67 49 64 78 5b 69 5d 20 3d 20 2b    aRegIdx[i] = +
27aef 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
27af0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
27af1 68 69 73 20 69 73 20 74 68 65 20 74 6f 70 20 6f  his is the top o
27af2 66 20 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72  f the main inser
27af3 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69  tion loop */.  i
27af4 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  f( useTempTable 
27af5 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62  ){.    /* This b
27af6 6c 6f 63 6b 20 63 6f 64 65 73 20 74 68 65 20 74  lock codes the t
27af7 6f 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e  op of loop only.
27af8 20 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 6c    The complete l
27af9 6f 6f 70 20 69 73 20 74 68 65 0a 20 20 20 20 2a  oop is the.    *
27afa 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 73 65 75  * following pseu
27afb 64 6f 63 6f 64 65 20 28 74 65 6d 70 6c 61 74 65  docode (template
27afc 20 34 29 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20   4):.    **.    
27afd 2a 2a 20 20 20 20 20 20 20 20 20 72 65 77 69 6e  **         rewin
27afe 64 20 74 65 6d 70 20 74 61 62 6c 65 0a 20 20 20  d temp table.   
27aff 20 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70   **      C: loop
27b00 20 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69 6e   over rows of in
27b01 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
27b02 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
27b03 20 20 74 72 61 6e 73 66 65 72 20 76 61 6c 75 65    transfer value
27b04 73 20 66 6f 72 6d 20 69 6e 74 65 72 6d 65 64 69  s form intermedi
27b05 61 74 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 3c  ate table into <
27b06 74 61 62 6c 65 3e 0a 20 20 20 20 2a 2a 20 20 20  table>.    **   
27b07 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20        end loop. 
27b08 20 20 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e 2e     **      D: ..
27b09 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64 64  ..    */.    add
27b0a 72 49 6e 73 54 6f 70 20 3d 20 73 71 6c 69 74 65  rInsTop = sqlite
27b0b 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
27b0c 50 5f 52 65 77 69 6e 64 2c 20 73 72 63 54 61 62  P_Rewind, srcTab
27b0d 29 3b 0a 20 20 20 20 61 64 64 72 43 6f 6e 74 20  );.    addrCont 
27b0e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
27b0f 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 7d  rentAddr(v);.  }
27b10 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 74  else if( pSelect
27b11 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
27b12 62 6c 6f 63 6b 20 63 6f 64 65 73 20 74 68 65 20  block codes the 
27b13 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79  top of loop only
27b14 2e 20 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20  .  The complete 
27b15 6c 6f 6f 70 20 69 73 20 74 68 65 0a 20 20 20 20  loop is the.    
27b16 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 73 65  ** following pse
27b17 75 64 6f 63 6f 64 65 20 28 74 65 6d 70 6c 61 74  udocode (templat
27b18 65 20 33 29 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  e 3):.    **.   
27b19 20 2a 2a 20 20 20 20 20 20 43 3a 20 79 69 65 6c   **      C: yiel
27b1a 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  d X.    **      
27b1b 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44     if EOF goto D
27b1c 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
27b1d 69 6e 73 65 72 74 20 74 68 65 20 73 65 6c 65 63  insert the selec
27b1e 74 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 3c 74  t result into <t
27b1f 61 62 6c 65 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b  able> from R..R+
27b20 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  n.    **        
27b21 20 67 6f 74 6f 20 43 0a 20 20 20 20 2a 2a 20 20   goto C.    **  
27b22 20 20 20 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a      D: ....    *
27b23 2f 0a 20 20 20 20 61 64 64 72 43 6f 6e 74 20 3d  /.    addrCont =
27b24 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27b25 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
27b26 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20  dest.iParm);.   
27b27 20 61 64 64 72 49 6e 73 54 6f 70 20 3d 20 73 71   addrInsTop = sq
27b28 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
27b29 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
27b2a 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c  );.  }..  /* All
27b2b 6f 63 61 74 65 20 72 65 67 69 73 74 65 72 73 20  ocate registers 
27b2c 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  for holding the 
27b2d 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77  rowid of the new
27b2e 20 72 6f 77 2c 0a 20 20 2a 2a 20 74 68 65 20 63   row,.  ** the c
27b2f 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65  ontent of the ne
27b30 77 20 72 6f 77 2c 20 61 6e 64 20 74 68 65 20 61  w row, and the a
27b31 73 73 65 6d 62 6c 69 65 64 20 72 6f 77 20 72 65  ssemblied row re
27b32 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 72 65 67  cord..  */.  reg
27b33 52 65 63 6f 72 64 20 3d 20 2b 2b 70 50 61 72 73  Record = ++pPars
27b34 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 52 6f  e->nMem;.  regRo
27b35 77 69 64 20 3d 20 72 65 67 49 6e 73 20 3d 20 70  wid = regIns = p
27b36 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20  Parse->nMem+1;. 
27b37 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d   pParse->nMem +=
27b38 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 2b 20 31 3b   pTab->nCol + 1;
27b39 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
27b3a 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 72 65  (pTab) ){.    re
27b3b 67 52 6f 77 69 64 2b 2b 3b 0a 20 20 20 20 70 50  gRowid++;.    pP
27b3c 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20  arse->nMem++;.  
27b3d 7d 0a 20 20 72 65 67 44 61 74 61 20 3d 20 72 65  }.  regData = re
27b3e 67 52 6f 77 69 64 2b 31 3b 0a 0a 20 20 2f 2a 20  gRowid+1;..  /* 
27b3f 52 75 6e 20 74 68 65 20 42 45 46 4f 52 45 20 61  Run the BEFORE a
27b40 6e 64 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  nd INSTEAD OF tr
27b41 69 67 67 65 72 73 2c 20 69 66 20 74 68 65 72 65  iggers, if there
27b42 20 61 72 65 20 61 6e 79 0a 20 20 2a 2f 0a 20 20   are any.  */.  
27b43 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69  endOfLoop = sqli
27b44 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
27b45 28 76 29 3b 0a 20 20 69 66 28 20 74 6d 61 73 6b  (v);.  if( tmask
27b46 20 26 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52   & TRIGGER_BEFOR
27b47 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 67  E ){.    int reg
27b48 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 47 65  Cols = sqlite3Ge
27b49 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  tTempRange(pPars
27b4a 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29  e, pTab->nCol+1)
27b4b 3b 0a 0a 20 20 20 20 2f 2a 20 62 75 69 6c 64 20  ;..    /* build 
27b4c 74 68 65 20 4e 45 57 2e 2a 20 72 65 66 65 72 65  the NEW.* refere
27b4d 6e 63 65 20 72 6f 77 2e 20 20 4e 6f 74 65 20 74  nce row.  Note t
27b4e 68 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20  hat if there is 
27b4f 61 6e 20 49 4e 54 45 47 45 52 0a 20 20 20 20 2a  an INTEGER.    *
27b50 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e  * PRIMARY KEY in
27b51 74 6f 20 77 68 69 63 68 20 61 20 4e 55 4c 4c 20  to which a NULL 
27b52 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65  is being inserte
27b53 64 2c 20 74 68 61 74 20 4e 55 4c 4c 20 77 69 6c  d, that NULL wil
27b54 6c 20 62 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e  l be.    ** tran
27b55 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 75 6e  slated into a un
27b56 69 71 75 65 20 49 44 20 66 6f 72 20 74 68 65 20  ique ID for the 
27b57 72 6f 77 2e 20 20 42 75 74 20 6f 6e 20 61 20 42  row.  But on a B
27b58 45 46 4f 52 45 20 74 72 69 67 67 65 72 2c 0a 20  EFORE trigger,. 
27b59 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e 6f 74 20     ** we do not 
27b5a 6b 6e 6f 77 20 77 68 61 74 20 74 68 65 20 75 6e  know what the un
27b5b 69 71 75 65 20 49 44 20 77 69 6c 6c 20 62 65 20  ique ID will be 
27b5c 28 62 65 63 61 75 73 65 20 74 68 65 20 69 6e 73  (because the ins
27b5d 65 72 74 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e  ert has.    ** n
27b5e 6f 74 20 68 61 70 70 65 6e 65 64 20 79 65 74 29  ot happened yet)
27b5f 20 73 6f 20 77 65 20 73 75 62 73 74 69 74 75 74   so we substitut
27b60 65 20 61 20 72 6f 77 69 64 20 6f 66 20 2d 31 0a  e a rowid of -1.
27b61 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6b      */.    if( k
27b62 65 79 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20  eyColumn<0 ){.  
27b63 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27b64 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
27b65 67 65 72 2c 20 2d 31 2c 20 72 65 67 43 6f 6c 73  ger, -1, regCols
27b66 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
27b67 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20      int j1;.    
27b68 20 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62    if( useTempTab
27b69 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  le ){.        sq
27b6a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
27b6b 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72  v, OP_Column, sr
27b6c 63 54 61 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c  cTab, keyColumn,
27b6d 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 20   regCols);.     
27b6e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
27b6f 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 3d  assert( pSelect=
27b70 3d 30 20 29 3b 20 20 2f 2a 20 4f 74 68 65 72 77  =0 );  /* Otherw
27b71 69 73 65 20 75 73 65 54 65 6d 70 54 61 62 6c 65  ise useTempTable
27b72 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 20   is true */.    
27b73 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
27b74 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73  ode(pParse, pLis
27b75 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e  t->a[keyColumn].
27b76 70 45 78 70 72 2c 20 72 65 67 43 6f 6c 73 29 3b  pExpr, regCols);
27b77 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a  .      }.      j
27b78 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
27b79 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e  ddOp1(v, OP_NotN
27b7a 75 6c 6c 2c 20 72 65 67 43 6f 6c 73 29 3b 0a 20  ull, regCols);. 
27b7b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27b7c 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
27b7d 65 67 65 72 2c 20 2d 31 2c 20 72 65 67 43 6f 6c  eger, -1, regCol
27b7e 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  s);.      sqlite
27b7f 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
27b80 20 6a 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   j1);.      sqli
27b81 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
27b82 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72   OP_MustBeInt, r
27b83 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 7d 0a 0a  egCols);.    }..
27b84 20 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61      /* Cannot ha
27b85 76 65 20 74 72 69 67 67 65 72 73 20 6f 6e 20 61  ve triggers on a
27b86 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
27b87 49 66 20 69 74 20 77 65 72 65 20 70 6f 73 73 69  If it were possi
27b88 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 74 68 69 73  ble,.    ** this
27b89 20 62 6c 6f 63 6b 20 77 6f 75 6c 64 20 68 61 76   block would hav
27b8a 65 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72  e to account for
27b8b 20 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e 2e 0a   hidden column..
27b8c 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
27b8d 74 28 20 21 49 73 56 69 72 74 75 61 6c 28 70 54  t( !IsVirtual(pT
27b8e 61 62 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  ab) );..    /* C
27b8f 72 65 61 74 65 20 74 68 65 20 6e 65 77 20 63 6f  reate the new co
27b90 6c 75 6d 6e 20 64 61 74 61 0a 20 20 20 20 2a 2f  lumn data.    */
27b91 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
27b92 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
27b93 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c  {.      if( pCol
27b94 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  umn==0 ){.      
27b95 20 20 6a 20 3d 20 69 3b 0a 20 20 20 20 20 20 7d    j = i;.      }
27b96 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f  else{.        fo
27b97 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e  r(j=0; j<pColumn
27b98 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ->nId; j++){.   
27b99 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75         if( pColu
27b9a 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20  mn->a[j].idx==i 
27b9b 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
27b9c 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
27b9d 20 69 66 28 20 70 43 6f 6c 75 6d 6e 20 26 26 20   if( pColumn && 
27b9e 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20  j>=pColumn->nId 
27b9f 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
27ba0 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
27ba1 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
27ba2 2e 70 44 66 6c 74 2c 20 72 65 67 43 6f 6c 73 2b  .pDflt, regCols+
27ba3 69 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  i+1);.      }els
27ba4 65 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62  e if( useTempTab
27ba5 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  le ){.        sq
27ba6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
27ba7 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72  v, OP_Column, sr
27ba8 63 54 61 62 2c 20 6a 2c 20 72 65 67 43 6f 6c 73  cTab, j, regCols
27ba9 2b 69 2b 31 29 3b 20 0a 20 20 20 20 20 20 7d 65  +i+1); .      }e
27baa 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
27bab 65 72 74 28 20 70 53 65 6c 65 63 74 3d 3d 30 20  ert( pSelect==0 
27bac 29 3b 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20  ); /* Otherwise 
27bad 75 73 65 54 65 6d 70 54 61 62 6c 65 20 69 73 20  useTempTable is 
27bae 74 72 75 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  true */.        
27baf 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41  sqlite3ExprCodeA
27bb0 6e 64 43 61 63 68 65 28 70 50 61 72 73 65 2c 20  ndCache(pParse, 
27bb1 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70  pList->a[j].pExp
27bb2 72 2c 20 72 65 67 43 6f 6c 73 2b 69 2b 31 29 3b  r, regCols+i+1);
27bb3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
27bb4 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
27bb5 73 20 61 6e 20 49 4e 53 45 52 54 20 6f 6e 20 61  s an INSERT on a
27bb6 20 76 69 65 77 20 77 69 74 68 20 61 6e 20 49 4e   view with an IN
27bb7 53 54 45 41 44 20 4f 46 20 49 4e 53 45 52 54 20  STEAD OF INSERT 
27bb8 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20  trigger,.    ** 
27bb9 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 61  do not attempt a
27bba 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 62  ny conversions b
27bbb 65 66 6f 72 65 20 61 73 73 65 6d 62 6c 69 6e 67  efore assembling
27bbc 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 20   the record..   
27bbd 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   ** If this is a
27bbe 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 61 74 74   real table, att
27bbf 65 6d 70 74 20 63 6f 6e 76 65 72 73 69 6f 6e 73  empt conversions
27bc0 20 61 73 20 72 65 71 75 69 72 65 64 20 62 79 20   as required by 
27bc1 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65  the.    ** table
27bc2 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 69   column affiniti
27bc3 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  es..    */.    i
27bc4 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  f( !isView ){.  
27bc5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27bc6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66 66 69  ddOp2(v, OP_Affi
27bc7 6e 69 74 79 2c 20 72 65 67 43 6f 6c 73 2b 31 2c  nity, regCols+1,
27bc8 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
27bc9 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65      sqlite3Table
27bca 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70  AffinityStr(v, p
27bcb 54 61 62 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  Tab);.    }..   
27bcc 20 2f 2a 20 46 69 72 65 20 42 45 46 4f 52 45 20   /* Fire BEFORE 
27bcd 6f 72 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  or INSTEAD OF tr
27bce 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 73 71  iggers */.    sq
27bcf 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
27bd0 67 65 72 28 70 50 61 72 73 65 2c 20 70 54 72 69  ger(pParse, pTri
27bd1 67 67 65 72 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  gger, TK_INSERT,
27bd2 20 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f   0, TRIGGER_BEFO
27bd3 52 45 2c 20 0a 20 20 20 20 20 20 20 20 70 54 61  RE, .        pTa
27bd4 62 2c 20 72 65 67 43 6f 6c 73 2d 70 54 61 62 2d  b, regCols-pTab-
27bd5 3e 6e 43 6f 6c 2d 31 2c 20 6f 6e 45 72 72 6f 72  >nCol-1, onError
27bd6 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 0a 20  , endOfLoop);.. 
27bd7 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
27bd8 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
27bd9 65 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54 61 62  e, regCols, pTab
27bda 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20 20 7d 0a 0a  ->nCol+1);.  }..
27bdb 20 20 2f 2a 20 50 75 73 68 20 74 68 65 20 72 65    /* Push the re
27bdc 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20  cord number for 
27bdd 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 6f 6e  the new entry on
27bde 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54  to the stack.  T
27bdf 68 65 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 6e  he.  ** record n
27be0 75 6d 62 65 72 20 69 73 20 61 20 72 61 6e 64 6f  umber is a rando
27be1 6d 6c 79 20 67 65 6e 65 72 61 74 65 20 69 6e 74  mly generate int
27be2 65 67 65 72 20 63 72 65 61 74 65 64 20 62 79 20  eger created by 
27be3 4e 65 77 52 6f 77 69 64 0a 20 20 2a 2a 20 65 78  NewRowid.  ** ex
27be4 63 65 70 74 20 77 68 65 6e 20 74 68 65 20 74 61  cept when the ta
27be5 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47  ble has an INTEG
27be6 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63  ER PRIMARY KEY c
27be7 6f 6c 75 6d 6e 2c 20 69 6e 20 77 68 69 63 68 0a  olumn, in which.
27be8 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 72 65    ** case the re
27be9 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 74  cord number is t
27bea 68 65 20 73 61 6d 65 20 61 73 20 74 68 61 74 20  he same as that 
27beb 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2f 0a 20 20  column. .  */.  
27bec 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20  if( !isView ){. 
27bed 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c     if( IsVirtual
27bee 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
27bef 2f 2a 20 54 68 65 20 72 6f 77 20 74 68 61 74 20  /* The row that 
27bf0 74 68 65 20 56 55 70 64 61 74 65 20 6f 70 63 6f  the VUpdate opco
27bf1 64 65 20 77 69 6c 6c 20 64 65 6c 65 74 65 3a 20  de will delete: 
27bf2 6e 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 73 71  none */.      sq
27bf3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27bf4 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
27bf5 65 67 49 6e 73 29 3b 0a 20 20 20 20 7d 0a 20 20  egIns);.    }.  
27bf6 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3e    if( keyColumn>
27bf7 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  =0 ){.      if( 
27bf8 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a  useTempTable ){.
27bf9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
27bfa 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
27bfb 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20  Column, srcTab, 
27bfc 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 6f  keyColumn, regRo
27bfd 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  wid);.      }els
27bfe 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b  e if( pSelect ){
27bff 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27c00 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27c01 5f 53 43 6f 70 79 2c 20 72 65 67 46 72 6f 6d 53  _SCopy, regFromS
27c02 65 6c 65 63 74 2b 6b 65 79 43 6f 6c 75 6d 6e 2c  elect+keyColumn,
27c03 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
27c04 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
27c05 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20   VdbeOp *pOp;.  
27c06 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
27c07 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c  rCode(pParse, pL
27c08 69 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d 6e  ist->a[keyColumn
27c09 5d 2e 70 45 78 70 72 2c 20 72 65 67 52 6f 77 69  ].pExpr, regRowi
27c0a 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 20  d);.        pOp 
27c0b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74  = sqlite3VdbeGet
27c0c 4f 70 28 76 2c 20 2d 31 29 3b 0a 20 20 20 20 20  Op(v, -1);.     
27c0d 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4f     if( ALWAYS(pO
27c0e 70 29 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f 64  p) && pOp->opcod
27c0f 65 3d 3d 4f 50 5f 4e 75 6c 6c 20 26 26 20 21 49  e==OP_Null && !I
27c10 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
27c11 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 70 65  {.          appe
27c12 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20  ndFlag = 1;.    
27c13 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64        pOp->opcod
27c14 65 20 3d 20 4f 50 5f 4e 65 77 52 6f 77 69 64 3b  e = OP_NewRowid;
27c15 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e  .          pOp->
27c16 70 31 20 3d 20 62 61 73 65 43 75 72 3b 0a 20 20  p1 = baseCur;.  
27c17 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20          pOp->p2 
27c18 3d 20 72 65 67 52 6f 77 69 64 3b 0a 20 20 20 20  = regRowid;.    
27c19 20 20 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d 20        pOp->p3 = 
27c1a 72 65 67 41 75 74 6f 69 6e 63 3b 0a 20 20 20 20  regAutoinc;.    
27c1b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
27c1c 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52      /* If the PR
27c1d 49 4d 41 52 59 20 4b 45 59 20 65 78 70 72 65 73  IMARY KEY expres
27c1e 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68  sion is NULL, th
27c1f 65 6e 20 75 73 65 20 4f 50 5f 4e 65 77 52 6f 77  en use OP_NewRow
27c20 69 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 67  id.      ** to g
27c21 65 6e 65 72 61 74 65 20 61 20 75 6e 69 71 75 65  enerate a unique
27c22 20 70 72 69 6d 61 72 79 20 6b 65 79 20 76 61 6c   primary key val
27c23 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ue..      */.   
27c24 20 20 20 69 66 28 20 21 61 70 70 65 6e 64 46 6c     if( !appendFl
27c25 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  ag ){.        in
27c26 74 20 6a 31 3b 0a 20 20 20 20 20 20 20 20 69 66  t j1;.        if
27c27 28 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61  ( !IsVirtual(pTa
27c28 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  b) ){.          
27c29 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
27c2a 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
27c2b 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69 64 29 3b  Null, regRowid);
27c2c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
27c2d 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
27c2e 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 62 61 73  OP_NewRowid, bas
27c2f 65 43 75 72 2c 20 72 65 67 52 6f 77 69 64 2c 20  eCur, regRowid, 
27c30 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a 20 20 20  regAutoinc);.   
27c31 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
27c32 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
27c33 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
27c34 7b 0a 20 20 20 20 20 20 20 20 20 20 6a 31 20 3d  {.          j1 =
27c35 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
27c36 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
27c37 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27c38 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73  eAddOp2(v, OP_Is
27c39 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69 64 2c 20  Null, regRowid, 
27c3a 6a 31 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 7d  j1+2);.        }
27c3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27c3c 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
27c3d 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67 52  _MustBeInt, regR
27c3e 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20  owid);.      }. 
27c3f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56     }else if( IsV
27c40 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
27c41 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27c42 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
27c43 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 69 64 29  ll, 0, regRowid)
27c44 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
27c45 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27c46 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 77 52 6f  dOp3(v, OP_NewRo
27c47 77 69 64 2c 20 62 61 73 65 43 75 72 2c 20 72 65  wid, baseCur, re
27c48 67 52 6f 77 69 64 2c 20 72 65 67 41 75 74 6f 69  gRowid, regAutoi
27c49 6e 63 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e  nc);.      appen
27c4a 64 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 7d  dFlag = 1;.    }
27c4b 0a 20 20 20 20 61 75 74 6f 49 6e 63 53 74 65 70  .    autoIncStep
27c4c 28 70 50 61 72 73 65 2c 20 72 65 67 41 75 74 6f  (pParse, regAuto
27c4d 69 6e 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  inc, regRowid);.
27c4e 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 6f 6e 74  .    /* Push ont
27c4f 6f 20 74 68 65 20 73 74 61 63 6b 2c 20 64 61 74  o the stack, dat
27c50 61 20 66 6f 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e  a for all column
27c51 73 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e 74  s of the new ent
27c52 72 79 2c 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20  ry, beginning.  
27c53 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 66 69    ** with the fi
27c54 72 73 74 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20  rst column..    
27c55 2a 2f 0a 20 20 20 20 6e 48 69 64 64 65 6e 20 3d  */.    nHidden =
27c56 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   0;.    for(i=0;
27c57 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
27c58 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ++){.      int i
27c59 52 65 67 53 74 6f 72 65 20 3d 20 72 65 67 52 6f  RegStore = regRo
27c5a 77 69 64 2b 31 2b 69 3b 0a 20 20 20 20 20 20 69  wid+1+i;.      i
27c5b 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65  f( i==pTab->iPKe
27c5c 79 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  y ){.        /* 
27c5d 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  The value of the
27c5e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
27c5f 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 69 73 20 61   KEY column is a
27c60 6c 77 61 79 73 20 61 20 4e 55 4c 4c 2e 0a 20 20  lways a NULL..  
27c61 20 20 20 20 20 20 2a 2a 20 57 68 65 6e 65 76 65        ** Wheneve
27c62 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73  r this column is
27c63 20 72 65 61 64 2c 20 74 68 65 20 72 65 63 6f 72   read, the recor
27c64 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20 62 65  d number will be
27c65 20 73 75 62 73 74 69 74 75 74 65 64 0a 20 20 20   substituted.   
27c66 20 20 20 20 20 2a 2a 20 69 6e 20 69 74 73 20 70       ** in its p
27c67 6c 61 63 65 2e 20 20 53 6f 20 77 69 6c 6c 20 66  lace.  So will f
27c68 69 6c 6c 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20  ill this column 
27c69 77 69 74 68 20 61 20 4e 55 4c 4c 20 74 6f 20 61  with a NULL to a
27c6a 76 6f 69 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  void.        ** 
27c6b 74 61 6b 69 6e 67 20 75 70 20 64 61 74 61 20 73  taking up data s
27c6c 70 61 63 65 20 77 69 74 68 20 69 6e 66 6f 72 6d  pace with inform
27c6d 61 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20  ation that will 
27c6e 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 20 2a  never be used. *
27c6f 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
27c70 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27c71 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 65 67 53  P_Null, 0, iRegS
27c72 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 20 20 63  tore);.        c
27c73 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d  ontinue;.      }
27c74 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75  .      if( pColu
27c75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  mn==0 ){.       
27c76 20 69 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c   if( IsHiddenCol
27c77 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b  umn(&pTab->aCol[
27c78 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  i]) ){.         
27c79 20 61 73 73 65 72 74 28 20 49 73 56 69 72 74 75   assert( IsVirtu
27c7a 61 6c 28 70 54 61 62 29 20 29 3b 0a 20 20 20 20  al(pTab) );.    
27c7b 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20        j = -1;.  
27c7c 20 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e 2b          nHidden+
27c7d 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  +;.        }else
27c7e 7b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 3d 20  {.          j = 
27c7f 69 20 2d 20 6e 48 69 64 64 65 6e 3b 0a 20 20 20  i - nHidden;.   
27c80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
27c81 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  se{.        for(
27c82 6a 3d 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e  j=0; j<pColumn->
27c83 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  nId; j++){.     
27c84 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e       if( pColumn
27c85 2d 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20 29 20  ->a[j].idx==i ) 
27c86 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
27c87 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
27c88 66 28 20 6a 3c 30 20 7c 7c 20 6e 43 6f 6c 75 6d  f( j<0 || nColum
27c89 6e 3d 3d 30 20 7c 7c 20 28 70 43 6f 6c 75 6d 6e  n==0 || (pColumn
27c8a 20 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e   && j>=pColumn->
27c8b 6e 49 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20  nId) ){.        
27c8c 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
27c8d 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43  pParse, pTab->aC
27c8e 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 69 52 65  ol[i].pDflt, iRe
27c8f 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d  gStore);.      }
27c90 65 6c 73 65 20 69 66 28 20 75 73 65 54 65 6d 70  else if( useTemp
27c91 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20  Table ){.       
27c92 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27c93 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
27c94 20 73 72 63 54 61 62 2c 20 6a 2c 20 69 52 65 67   srcTab, j, iReg
27c95 53 74 6f 72 65 29 3b 20 0a 20 20 20 20 20 20 7d  Store); .      }
27c96 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 74  else if( pSelect
27c97 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
27c98 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27c99 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 46 72   OP_SCopy, regFr
27c9a 6f 6d 53 65 6c 65 63 74 2b 6a 2c 20 69 52 65 67  omSelect+j, iReg
27c9b 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d 65  Store);.      }e
27c9c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
27c9d 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
27c9e 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d  rse, pList->a[j]
27c9f 2e 70 45 78 70 72 2c 20 69 52 65 67 53 74 6f 72  .pExpr, iRegStor
27ca0 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
27ca1 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  }..    /* Genera
27ca2 74 65 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b  te code to check
27ca3 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 6e 64   constraints and
27ca4 20 67 65 6e 65 72 61 74 65 20 69 6e 64 65 78 20   generate index 
27ca5 6b 65 79 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20  keys and.    ** 
27ca6 64 6f 20 74 68 65 20 69 6e 73 65 72 74 69 6f 6e  do the insertion
27ca7 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66  ..    */.#ifndef
27ca8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
27ca9 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66  TUALTABLE.    if
27caa 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
27cab 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  ) ){.      const
27cac 20 63 68 61 72 20 2a 70 56 54 61 62 20 3d 20 28   char *pVTab = (
27cad 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
27cae 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62  ite3GetVTable(db
27caf 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 73  , pTab);.      s
27cb0 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72  qlite3VtabMakeWr
27cb1 69 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  itable(pParse, p
27cb2 54 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Tab);.      sqli
27cb3 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
27cb4 20 4f 50 5f 56 55 70 64 61 74 65 2c 20 31 2c 20   OP_VUpdate, 1, 
27cb5 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 72 65  pTab->nCol+2, re
27cb6 67 49 6e 73 2c 20 70 56 54 61 62 2c 20 50 34 5f  gIns, pVTab, P4_
27cb7 56 54 41 42 29 3b 0a 20 20 20 20 20 20 73 71 6c  VTAB);.      sql
27cb8 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61  ite3MayAbort(pPa
27cb9 72 73 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a  rse);.    }else.
27cba 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20  #endif.    {.   
27cbb 20 20 20 69 6e 74 20 69 73 52 65 70 6c 61 63 65     int isReplace
27cbc 3b 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 74  ;    /* Set to t
27cbd 72 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e  rue if constrain
27cbe 74 73 20 6d 61 79 20 63 61 75 73 65 20 61 20 72  ts may cause a r
27cbf 65 70 6c 61 63 65 20 2a 2f 0a 20 20 20 20 20 20  eplace */.      
27cc0 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43  sqlite3GenerateC
27cc1 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28  onstraintChecks(
27cc2 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61  pParse, pTab, ba
27cc3 73 65 43 75 72 2c 20 72 65 67 49 6e 73 2c 20 61  seCur, regIns, a
27cc4 52 65 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20  RegIdx,.        
27cc5 20 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 2c 20    keyColumn>=0, 
27cc6 30 2c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64 4f  0, onError, endO
27cc7 66 4c 6f 6f 70 2c 20 26 69 73 52 65 70 6c 61 63  fLoop, &isReplac
27cc8 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20  e.      );.     
27cc9 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28   sqlite3FkCheck(
27cca 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c  pParse, pTab, 0,
27ccb 20 72 65 67 49 6e 73 29 3b 0a 20 20 20 20 20 20   regIns);.      
27ccc 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49  sqlite3CompleteI
27ccd 6e 73 65 72 74 69 6f 6e 28 0a 20 20 20 20 20 20  nsertion(.      
27cce 20 20 20 20 70 50 61 72 73 65 2c 20 70 54 61 62      pParse, pTab
27ccf 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 49 6e  , baseCur, regIn
27cd0 73 2c 20 61 52 65 67 49 64 78 2c 20 30 2c 20 61  s, aRegIdx, 0, a
27cd1 70 70 65 6e 64 46 6c 61 67 2c 20 69 73 52 65 70  ppendFlag, isRep
27cd2 6c 61 63 65 3d 3d 30 0a 20 20 20 20 20 20 29 3b  lace==0.      );
27cd3 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
27cd4 20 55 70 64 61 74 65 20 74 68 65 20 63 6f 75 6e   Update the coun
27cd5 74 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 61  t of rows that a
27cd6 72 65 20 69 6e 73 65 72 74 65 64 0a 20 20 2a 2f  re inserted.  */
27cd7 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67  .  if( (db->flag
27cd8 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74  s & SQLITE_Count
27cd9 52 6f 77 73 29 21 3d 30 20 29 7b 0a 20 20 20 20  Rows)!=0 ){.    
27cda 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27cdb 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
27cdc 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b  regRowCount, 1);
27cdd 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 54 72 69  .  }..  if( pTri
27cde 67 67 65 72 20 29 7b 0a 20 20 20 20 2f 2a 20 43  gger ){.    /* C
27cdf 6f 64 65 20 41 46 54 45 52 20 74 72 69 67 67 65  ode AFTER trigge
27ce0 72 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  rs */.    sqlite
27ce1 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
27ce2 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
27ce3 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20  , TK_INSERT, 0, 
27ce4 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20 0a  TRIGGER_AFTER, .
27ce5 20 20 20 20 20 20 20 20 70 54 61 62 2c 20 72 65          pTab, re
27ce6 67 44 61 74 61 2d 32 2d 70 54 61 62 2d 3e 6e 43  gData-2-pTab->nC
27ce7 6f 6c 2c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64  ol, onError, end
27ce8 4f 66 4c 6f 6f 70 29 3b 0a 20 20 7d 0a 0a 20 20  OfLoop);.  }..  
27ce9 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66  /* The bottom of
27cea 20 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72 74   the main insert
27ceb 69 6f 6e 20 6c 6f 6f 70 2c 20 69 66 20 74 68 65  ion loop, if the
27cec 20 64 61 74 61 20 73 6f 75 72 63 65 0a 20 20 2a   data source.  *
27ced 2a 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74  * is a SELECT st
27cee 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20  atement..  */.  
27cef 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
27cf0 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66  veLabel(v, endOf
27cf1 4c 6f 6f 70 29 3b 0a 20 20 69 66 28 20 75 73 65  Loop);.  if( use
27cf2 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  TempTable ){.   
27cf3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27cf4 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73  p2(v, OP_Next, s
27cf5 72 63 54 61 62 2c 20 61 64 64 72 43 6f 6e 74 29  rcTab, addrCont)
27cf6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27cf7 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
27cf8 72 49 6e 73 54 6f 70 29 3b 0a 20 20 20 20 73 71  rInsTop);.    sq
27cf9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
27cfa 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 73 72 63  v, OP_Close, src
27cfb 54 61 62 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Tab);.  }else if
27cfc 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
27cfd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27cfe 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
27cff 2c 20 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20  , addrCont);.   
27d00 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
27d01 48 65 72 65 28 76 2c 20 61 64 64 72 49 6e 73 54  Here(v, addrInsT
27d02 6f 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  op);.  }..  if( 
27d03 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  !IsVirtual(pTab)
27d04 20 26 26 20 21 69 73 56 69 65 77 20 29 7b 0a 20   && !isView ){. 
27d05 20 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20     /* Close all 
27d06 74 61 62 6c 65 73 20 6f 70 65 6e 65 64 20 2a 2f  tables opened */
27d07 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27d08 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
27d09 73 65 2c 20 62 61 73 65 43 75 72 29 3b 0a 20 20  se, baseCur);.  
27d0a 20 20 66 6f 72 28 69 64 78 3d 31 2c 20 70 49 64    for(idx=1, pId
27d0b 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
27d0c 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
27d0d 3e 70 4e 65 78 74 2c 20 69 64 78 2b 2b 29 7b 0a  >pNext, idx++){.
27d0e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27d0f 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
27d10 6f 73 65 2c 20 69 64 78 2b 62 61 73 65 43 75 72  ose, idx+baseCur
27d11 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 69 6e  );.    }.  }..in
27d12 73 65 72 74 5f 65 6e 64 3a 0a 20 20 2f 2a 20 55  sert_end:.  /* U
27d13 70 64 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  pdate the sqlite
27d14 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
27d15 62 79 20 73 74 6f 72 69 6e 67 20 74 68 65 20 63  by storing the c
27d16 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a 20 20  ontent of the.  
27d17 2a 2a 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  ** maximum rowid
27d18 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73 20   counter values 
27d19 72 65 63 6f 72 64 65 64 20 77 68 69 6c 65 20 69  recorded while i
27d1a 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 0a 20 20  nserting into.  
27d1b 2a 2a 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  ** autoincrement
27d1c 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
27d1d 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  if( pParse->nest
27d1e 65 64 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d  ed==0 && pParse-
27d1f 3e 70 54 72 69 67 67 65 72 54 61 62 3d 3d 30 20  >pTriggerTab==0 
27d20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75  ){.    sqlite3Au
27d21 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 70  toincrementEnd(p
27d22 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  Parse);.  }..  /
27d23 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68  *.  ** Return th
27d24 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
27d25 20 69 6e 73 65 72 74 65 64 2e 20 49 66 20 74 68   inserted. If th
27d26 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20  is routine is . 
27d27 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63   ** generating c
27d28 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20 61  ode because of a
27d29 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
27d2a 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20 64  NestedParse(), d
27d2b 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b  o not.  ** invok
27d2c 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  e the callback f
27d2d 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  unction..  */.  
27d2e 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26 53  if( (db->flags&S
27d2f 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29  QLITE_CountRows)
27d30 20 26 26 20 21 70 50 61 72 73 65 2d 3e 6e 65 73   && !pParse->nes
27d31 74 65 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e  ted && !pParse->
27d32 70 54 72 69 67 67 65 72 54 61 62 20 29 7b 0a 20  pTriggerTab ){. 
27d33 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27d34 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
27d35 74 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f 75 6e  tRow, regRowCoun
27d36 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  t, 1);.    sqlit
27d37 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
27d38 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
27d39 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
27d3a 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
27d3b 4e 41 4d 45 2c 20 22 72 6f 77 73 20 69 6e 73 65  NAME, "rows inse
27d3c 72 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54  rted", SQLITE_ST
27d3d 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 69 6e 73 65  ATIC);.  }..inse
27d3e 72 74 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71  rt_cleanup:.  sq
27d3f 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
27d40 74 65 28 64 62 2c 20 70 54 61 62 4c 69 73 74 29  te(db, pTabList)
27d41 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
27d42 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c  istDelete(db, pL
27d43 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ist);.  sqlite3S
27d44 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
27d45 70 53 65 6c 65 63 74 29 3b 0a 20 20 73 71 6c 69  pSelect);.  sqli
27d46 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
27d47 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20  db, pColumn);.  
27d48 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
27d49 2c 20 61 52 65 67 49 64 78 29 3b 0a 7d 0a 0a 2f  , aRegIdx);.}../
27d4a 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73 56  * Make sure "isV
27d4b 69 65 77 22 20 61 6e 64 20 6f 74 68 65 72 20 6d  iew" and other m
27d4c 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20 61 62  acros defined ab
27d4d 6f 76 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65  ove are undefine
27d4e 64 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20  d. Otherwise.** 
27d4f 74 68 65 6c 79 20 6d 61 79 20 69 6e 74 65 72 66  thely may interf
27d50 65 72 65 20 77 69 74 68 20 63 6f 6d 70 69 6c 61  ere with compila
27d51 74 69 6f 6e 20 6f 66 20 6f 74 68 65 72 20 66 75  tion of other fu
27d52 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 69 73 20  nctions in this 
27d53 66 69 6c 65 0a 2a 2a 20 28 6f 72 20 69 6e 20 61  file.** (or in a
27d54 6e 6f 74 68 65 72 20 66 69 6c 65 2c 20 69 66 20  nother file, if 
27d55 74 68 69 73 20 66 69 6c 65 20 62 65 63 6f 6d 65  this file become
27d56 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 61 6d  s part of the am
27d57 61 6c 67 61 6d 61 74 69 6f 6e 29 2e 20 20 2a 2f  algamation).  */
27d58 0a 23 69 66 64 65 66 20 69 73 56 69 65 77 0a 20  .#ifdef isView. 
27d59 23 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23 65  #undef isView.#e
27d5a 6e 64 69 66 0a 23 69 66 64 65 66 20 70 54 72 69  ndif.#ifdef pTri
27d5b 67 67 65 72 0a 20 23 75 6e 64 65 66 20 70 54 72  gger. #undef pTr
27d5c 69 67 67 65 72 0a 23 65 6e 64 69 66 0a 23 69 66  igger.#endif.#if
27d5d 64 65 66 20 74 6d 61 73 6b 0a 20 23 75 6e 64 65  def tmask. #unde
27d5e 66 20 74 6d 61 73 6b 0a 23 65 6e 64 69 66 0a 0a  f tmask.#endif..
27d5f 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
27d60 63 6f 64 65 20 74 6f 20 64 6f 20 63 6f 6e 73 74  code to do const
27d61 72 61 69 6e 74 20 63 68 65 63 6b 73 20 70 72 69  raint checks pri
27d62 6f 72 20 74 6f 20 61 6e 20 49 4e 53 45 52 54 20  or to an INSERT 
27d63 6f 72 20 61 6e 20 55 50 44 41 54 45 2e 0a 2a 2a  or an UPDATE..**
27d64 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73  .** The input is
27d65 20 61 20 72 61 6e 67 65 20 6f 66 20 63 6f 6e 73   a range of cons
27d66 65 63 75 74 69 76 65 20 72 65 67 69 73 74 65 72  ecutive register
27d67 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
27d68 0a 2a 2a 20 20 20 20 31 2e 20 20 54 68 65 20 72  .**    1.  The r
27d69 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20  owid of the row 
27d6a 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65  after the update
27d6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 32 2e 20 20 54  ..**.**    2.  T
27d6c 68 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 66  he data in the f
27d6d 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  irst column of t
27d6e 68 65 20 65 6e 74 72 79 20 61 66 74 65 72 20 74  he entry after t
27d6f 68 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a  he update..**.**
27d70 20 20 20 20 69 2e 20 20 44 61 74 61 20 66 72 6f      i.  Data fro
27d71 6d 20 6d 69 64 64 6c 65 20 63 6f 6c 75 6d 6e 73  m middle columns
27d72 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 2e 20  ....**.**    N. 
27d73 20 54 68 65 20 64 61 74 61 20 69 6e 20 74 68 65   The data in the
27d74 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20   last column of 
27d75 74 68 65 20 65 6e 74 72 79 20 61 66 74 65 72 20  the entry after 
27d76 74 68 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a  the update..**.*
27d77 2a 20 54 68 65 20 72 65 67 52 6f 77 69 64 20 70  * The regRowid p
27d78 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
27d79 69 6e 64 65 78 20 6f 66 20 74 68 65 20 72 65 67  index of the reg
27d7a 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  ister containing
27d7b 20 28 31 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69   (1)..**.** If i
27d7c 73 55 70 64 61 74 65 20 69 73 20 74 72 75 65 20  sUpdate is true 
27d7d 61 6e 64 20 72 6f 77 69 64 43 68 6e 67 20 69 73  and rowidChng is
27d7e 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
27d7f 72 6f 77 69 64 43 68 6e 67 20 63 6f 6e 74 61 69  rowidChng contai
27d80 6e 73 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73  ns.** the addres
27d81 73 20 6f 66 20 61 20 72 65 67 69 73 74 65 72 20  s of a register 
27d82 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
27d83 6f 77 69 64 20 62 65 66 6f 72 65 20 74 68 65 20  owid before the 
27d84 75 70 64 61 74 65 20 74 61 6b 65 73 0a 2a 2a 20  update takes.** 
27d85 70 6c 61 63 65 2e 20 69 73 55 70 64 61 74 65 20  place. isUpdate 
27d86 69 73 20 74 72 75 65 20 66 6f 72 20 55 50 44 41  is true for UPDA
27d87 54 45 73 20 61 6e 64 20 66 61 6c 73 65 20 66 6f  TEs and false fo
27d88 72 20 49 4e 53 45 52 54 73 2e 20 49 66 20 69 73  r INSERTs. If is
27d89 55 70 64 61 74 65 0a 2a 2a 20 69 73 20 66 61 6c  Update.** is fal
27d8a 73 65 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 61  se, indicating a
27d8b 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65  n INSERT stateme
27d8c 6e 74 2c 20 74 68 65 6e 20 61 20 6e 6f 6e 2d 7a  nt, then a non-z
27d8d 65 72 6f 20 72 6f 77 69 64 43 68 6e 67 20 0a 2a  ero rowidChng .*
27d8e 2a 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  * indicates that
27d8f 20 74 68 65 20 72 6f 77 69 64 20 77 61 73 20 65   the rowid was e
27d90 78 70 6c 69 63 69 74 6c 79 20 73 70 65 63 69 66  xplicitly specif
27d91 69 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  ied as part of t
27d92 68 65 0a 2a 2a 20 49 4e 53 45 52 54 20 73 74 61  he.** INSERT sta
27d93 74 65 6d 65 6e 74 2e 20 49 66 20 72 6f 77 69 64  tement. If rowid
27d94 43 68 6e 67 20 69 73 20 66 61 6c 73 65 2c 20 69  Chng is false, i
27d95 74 20 6d 65 61 6e 73 20 74 68 61 74 20 20 74 68  t means that  th
27d96 65 20 72 6f 77 69 64 20 69 73 0a 2a 2a 20 63 6f  e rowid is.** co
27d97 6d 70 75 74 65 64 20 61 75 74 6f 6d 61 74 69 63  mputed automatic
27d98 61 6c 6c 79 20 69 6e 20 61 6e 20 69 6e 73 65 72  ally in an inser
27d99 74 20 6f 72 20 74 68 61 74 20 74 68 65 20 72 6f  t or that the ro
27d9a 77 69 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 74  wid value is not
27d9b 20 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 62 79   .** modified by
27d9c 20 61 6e 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a   an update..**.*
27d9d 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72  * The code gener
27d9e 61 74 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  ated by this rou
27d9f 74 69 6e 65 20 73 74 6f 72 65 20 6e 65 77 20 69  tine store new i
27da0 6e 64 65 78 20 65 6e 74 72 69 65 73 20 69 6e 74  ndex entries int
27da1 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 69  o.** registers i
27da2 64 65 6e 74 69 66 69 65 64 20 62 79 20 61 52 65  dentified by aRe
27da3 67 49 64 78 5b 5d 2e 20 20 4e 6f 20 69 6e 64 65  gIdx[].  No inde
27da4 78 20 65 6e 74 72 79 20 69 73 20 63 72 65 61 74  x entry is creat
27da5 65 64 20 66 6f 72 0a 2a 2a 20 69 6e 64 69 63 65  ed for.** indice
27da6 73 20 77 68 65 72 65 20 61 52 65 67 49 64 78 5b  s where aRegIdx[
27da7 69 5d 3d 3d 30 2e 20 20 54 68 65 20 6f 72 64 65  i]==0.  The orde
27da8 72 20 6f 66 20 69 6e 64 69 63 65 73 20 69 6e 20  r of indices in 
27da9 61 52 65 67 49 64 78 5b 5d 20 69 73 0a 2a 2a 20  aRegIdx[] is.** 
27daa 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
27dab 6f 72 64 65 72 20 6f 66 20 69 6e 64 69 63 65 73  order of indices
27dac 20 6f 6e 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c   on the linked l
27dad 69 73 74 20 6f 66 20 69 6e 64 69 63 65 73 0a 2a  ist of indices.*
27dae 2a 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68  * attached to th
27daf 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  e table..**.** T
27db0 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f  his routine also
27db1 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
27db2 74 6f 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61  to check constra
27db3 69 6e 74 73 2e 20 20 4e 4f 54 20 4e 55 4c 4c 2c  ints.  NOT NULL,
27db4 0a 2a 2a 20 43 48 45 43 4b 2c 20 61 6e 64 20 55  .** CHECK, and U
27db5 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74  NIQUE constraint
27db6 73 20 61 72 65 20 61 6c 6c 20 63 68 65 63 6b 65  s are all checke
27db7 64 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 61  d.  If a constra
27db8 69 6e 74 20 66 61 69 6c 73 2c 0a 2a 2a 20 74 68  int fails,.** th
27db9 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  en the appropria
27dba 74 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72  te action is per
27dbb 66 6f 72 6d 65 64 2e 20 20 54 68 65 72 65 20 61  formed.  There a
27dbc 72 65 20 66 69 76 65 20 70 6f 73 73 69 62 6c 65  re five possible
27dbd 0a 2a 2a 20 61 63 74 69 6f 6e 73 3a 20 52 4f 4c  .** actions: ROL
27dbe 4c 42 41 43 4b 2c 20 41 42 4f 52 54 2c 20 46 41  LBACK, ABORT, FA
27dbf 49 4c 2c 20 52 45 50 4c 41 43 45 2c 20 61 6e 64  IL, REPLACE, and
27dc0 20 49 47 4e 4f 52 45 2e 0a 2a 2a 0a 2a 2a 20 20   IGNORE..**.**  
27dc1 43 6f 6e 73 74 72 61 69 6e 74 20 74 79 70 65 20  Constraint type 
27dc2 20 41 63 74 69 6f 6e 20 20 20 20 20 20 20 57 68   Action       Wh
27dc3 61 74 20 48 61 70 70 65 6e 73 0a 2a 2a 20 20 2d  at Happens.**  -
27dc4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
27dc5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 2d 2d 2d  ----------   ---
27dc6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27dc7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27dc8 2d 2d 2d 2d 2d 0a 2a 2a 20 20 61 6e 79 20 20 20  -----.**  any   
27dc9 20 20 20 20 20 20 20 20 20 20 20 52 4f 4c 4c 42             ROLLB
27dca 41 43 4b 20 20 20 20 20 54 68 65 20 63 75 72 72  ACK     The curr
27dcb 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
27dcc 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  is rolled back a
27dcd 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  nd.**           
27dce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27dcf 20 20 20 20 20 73 71 6c 69 74 65 33 5f 65 78 65       sqlite3_exe
27dd0 63 28 29 20 72 65 74 75 72 6e 73 20 69 6d 6d 65  c() returns imme
27dd1 64 69 61 74 65 6c 79 20 77 69 74 68 20 61 0a 2a  diately with a.*
27dd2 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
27dd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27dd4 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20   return code of 
27dd5 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
27dd6 54 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20  T..**.**  any   
27dd7 20 20 20 20 20 20 20 20 20 20 20 41 42 4f 52 54             ABORT
27dd8 20 20 20 20 20 20 20 20 42 61 63 6b 20 6f 75 74          Back out
27dd9 20 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 74 68   changes from th
27dda 65 20 63 75 72 72 65 6e 74 20 63 6f 6d 6d 61 6e  e current comman
27ddb 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
27ddc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ddd 20 20 20 20 6f 6e 6c 79 20 28 64 6f 20 6e 6f 74      only (do not
27dde 20 64 6f 20 61 20 63 6f 6d 70 6c 65 74 65 20 72   do a complete r
27ddf 6f 6c 6c 62 61 63 6b 29 20 74 68 65 6e 0a 2a 2a  ollback) then.**
27de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27de1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27de2 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 65 78  cause sqlite3_ex
27de3 65 63 28 29 20 74 6f 20 72 65 74 75 72 6e 20 69  ec() to return i
27de4 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 20 20  mmediately.**   
27de5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27de6 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69 74               wit
27de7 68 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  h SQLITE_CONSTRA
27de8 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20  INT..**.**  any 
27de9 20 20 20 20 20 20 20 20 20 20 20 20 20 46 41 49               FAI
27dea 4c 20 20 20 20 20 20 20 20 20 53 71 6c 69 74 65  L         Sqlite
27deb 5f 65 78 65 63 28 29 20 72 65 74 75 72 6e 73 20  _exec() returns 
27dec 69 6d 6d 65 64 69 61 74 65 6c 79 20 77 69 74 68  immediately with
27ded 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   a.**           
27dee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27def 20 20 20 20 20 72 65 74 75 72 6e 20 63 6f 64 65       return code
27df0 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54   of SQLITE_CONST
27df1 52 41 49 4e 54 2e 20 20 54 68 65 0a 2a 2a 20 20  RAINT.  The.**  
27df2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27df3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 72                tr
27df4 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74  ansaction is not
27df5 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64   rolled back and
27df6 20 61 6e 79 0a 2a 2a 20 20 20 20 20 20 20 20 20   any.**         
27df7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27df8 20 20 20 20 20 20 20 70 72 69 6f 72 20 63 68 61         prior cha
27df9 6e 67 65 73 20 61 72 65 20 72 65 74 61 69 6e 65  nges are retaine
27dfa 64 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20  d..**.**  any   
27dfb 20 20 20 20 20 20 20 20 20 20 20 49 47 4e 4f 52             IGNOR
27dfc 45 20 20 20 20 20 20 20 54 68 65 20 72 65 63 6f  E       The reco
27dfd 72 64 20 6e 75 6d 62 65 72 20 61 6e 64 20 64 61  rd number and da
27dfe 74 61 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f  ta is popped fro
27dff 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  m.**            
27e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27e01 20 20 20 20 74 68 65 20 73 74 61 63 6b 20 61 6e      the stack an
27e02 64 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6d  d there is an im
27e03 6d 65 64 69 61 74 65 20 6a 75 6d 70 0a 2a 2a 20  mediate jump.** 
27e04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27e05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
27e06 6f 20 6c 61 62 65 6c 20 69 67 6e 6f 72 65 44 65  o label ignoreDe
27e07 73 74 2e 0a 2a 2a 0a 2a 2a 20 20 4e 4f 54 20 4e  st..**.**  NOT N
27e08 55 4c 4c 20 20 20 20 20 20 20 20 20 52 45 50 4c  ULL         REPL
27e09 41 43 45 20 20 20 20 20 20 54 68 65 20 4e 55 4c  ACE      The NUL
27e0a 4c 20 76 61 6c 75 65 20 69 73 20 72 65 70 6c 61  L value is repla
27e0b 63 65 20 62 79 20 74 68 65 20 64 65 66 61 75 6c  ce by the defaul
27e0c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
27e0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27e0e 20 20 20 20 76 61 6c 75 65 20 66 6f 72 20 74 68      value for th
27e0f 61 74 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74  at column.  If t
27e10 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
27e11 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
27e12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27e13 20 20 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20     is NULL, the 
27e14 61 63 74 69 6f 6e 20 69 73 20 74 68 65 20 73 61  action is the sa
27e15 6d 65 20 61 73 20 41 42 4f 52 54 2e 0a 2a 2a 0a  me as ABORT..**.
27e16 2a 2a 20 20 55 4e 49 51 55 45 20 20 20 20 20 20  **  UNIQUE      
27e17 20 20 20 20 20 52 45 50 4c 41 43 45 20 20 20 20       REPLACE    
27e18 20 20 54 68 65 20 6f 74 68 65 72 20 72 6f 77 20    The other row 
27e19 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 77  that conflicts w
27e1a 69 74 68 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20  ith the row.**  
27e1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
27e1d 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 73 20  ing inserted is 
27e1e 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 20  removed..**.**  
27e1f 43 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20  CHECK           
27e20 20 52 45 50 4c 41 43 45 20 20 20 20 20 20 49 6c   REPLACE      Il
27e21 6c 65 67 61 6c 2e 20 20 54 68 65 20 72 65 73 75  legal.  The resu
27e22 6c 74 73 20 69 6e 20 61 6e 20 65 78 63 65 70 74  lts in an except
27e23 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 69 63 68  ion..**.** Which
27e24 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20   action to take 
27e25 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
27e26 20 74 68 65 20 6f 76 65 72 72 69 64 65 45 72 72   the overrideErr
27e27 6f 72 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  or parameter..**
27e28 20 4f 72 20 69 66 20 6f 76 65 72 72 69 64 65 45   Or if overrideE
27e29 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
27e2a 2c 20 74 68 65 6e 20 74 68 65 20 70 50 61 72 73  , then the pPars
27e2b 65 2d 3e 6f 6e 45 72 72 6f 72 20 70 61 72 61 6d  e->onError param
27e2c 65 74 65 72 0a 2a 2a 20 69 73 20 75 73 65 64 2e  eter.** is used.
27e2d 20 20 4f 72 20 69 66 20 70 50 61 72 73 65 2d 3e    Or if pParse->
27e2e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
27e2f 75 6c 74 20 74 68 65 6e 20 74 68 65 20 6f 6e 45  ult then the onE
27e30 72 72 6f 72 20 76 61 6c 75 65 0a 2a 2a 20 66 6f  rror value.** fo
27e31 72 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  r the constraint
27e32 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   is used..**.** 
27e33 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  The calling rout
27e34 69 6e 65 20 6d 75 73 74 20 6f 70 65 6e 20 61 20  ine must open a 
27e35 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
27e36 72 20 66 6f 72 20 70 54 61 62 20 77 69 74 68 0a  r for pTab with.
27e37 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  ** cursor number
27e38 20 22 62 61 73 65 43 75 72 22 2e 20 20 41 6c 6c   "baseCur".  All
27e39 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62   indices of pTab
27e3a 20 6d 75 73 74 20 61 6c 73 6f 20 68 61 76 65 20   must also have 
27e3b 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 2f 77 72 69  open.** read/wri
27e3c 74 65 20 63 75 72 73 6f 72 73 20 77 69 74 68 20  te cursors with 
27e3d 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 62 61  cursor number ba
27e3e 73 65 43 75 72 2b 69 20 66 6f 72 20 74 68 65 20  seCur+i for the 
27e3f 69 2d 74 68 20 63 75 72 73 6f 72 2e 0a 2a 2a 20  i-th cursor..** 
27e40 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 72 65  Except, if there
27e41 20 69 73 20 6e 6f 20 70 6f 73 73 69 62 69 6c 69   is no possibili
27e42 74 79 20 6f 66 20 61 20 52 45 50 4c 41 43 45 20  ty of a REPLACE 
27e43 61 63 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 63  action then.** c
27e44 75 72 73 6f 72 73 20 64 6f 20 6e 6f 74 20 6e 65  ursors do not ne
27e45 65 64 20 74 6f 20 62 65 20 6f 70 65 6e 20 66 6f  ed to be open fo
27e46 72 20 69 6e 64 69 63 65 73 20 77 68 65 72 65 20  r indices where 
27e47 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30 2e 0a 2a  aRegIdx[i]==0..*
27e48 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
27e49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e   void sqlite3Gen
27e4a 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
27e4b 68 65 63 6b 73 28 0a 20 20 50 61 72 73 65 20 2a  hecks(.  Parse *
27e4c 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
27e4d 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
27e4e 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  xt */.  Table *p
27e4f 54 61 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 74  Tab,        /* t
27e50 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  he table into wh
27e51 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65 72  ich we are inser
27e52 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61  ting */.  int ba
27e53 73 65 43 75 72 2c 20 20 20 20 20 20 20 20 2f 2a  seCur,        /*
27e54 20 49 6e 64 65 78 20 6f 66 20 61 20 72 65 61 64   Index of a read
27e55 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f  /write cursor po
27e56 69 6e 74 69 6e 67 20 61 74 20 70 54 61 62 20 2a  inting at pTab *
27e57 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  /.  int regRowid
27e58 2c 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  ,       /* Index
27e59 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66   of the range of
27e5a 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 73   input registers
27e5b 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49   */.  int *aRegI
27e5c 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 52 65 67  dx,       /* Reg
27e5d 69 73 74 65 72 20 75 73 65 64 20 62 79 20 65 61  ister used by ea
27e5e 63 68 20 69 6e 64 65 78 2e 20 20 30 20 66 6f 72  ch index.  0 for
27e5f 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73 20   unused indices 
27e60 2a 2f 0a 20 20 69 6e 74 20 72 6f 77 69 64 43 68  */.  int rowidCh
27e61 6e 67 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ng,      /* True
27e62 20 69 66 20 74 68 65 20 72 6f 77 69 64 20 6d 69   if the rowid mi
27e63 67 68 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68  ght collide with
27e64 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 20   existing entry 
27e65 2a 2f 0a 20 20 69 6e 74 20 69 73 55 70 64 61 74  */.  int isUpdat
27e66 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  e,       /* True
27e67 20 66 6f 72 20 55 50 44 41 54 45 2c 20 46 61 6c   for UPDATE, Fal
27e68 73 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f  se for INSERT */
27e69 0a 20 20 69 6e 74 20 6f 76 65 72 72 69 64 65 45  .  int overrideE
27e6a 72 72 6f 72 2c 20 20 2f 2a 20 4f 76 65 72 72 69  rror,  /* Overri
27e6b 64 65 20 6f 6e 45 72 72 6f 72 20 74 6f 20 74 68  de onError to th
27e6c 69 73 20 69 66 20 6e 6f 74 20 4f 45 5f 44 65 66  is if not OE_Def
27e6d 61 75 6c 74 20 2a 2f 0a 20 20 69 6e 74 20 69 67  ault */.  int ig
27e6e 6e 6f 72 65 44 65 73 74 2c 20 20 20 20 20 2f 2a  noreDest,     /*
27e6f 20 4a 75 6d 70 20 74 6f 20 74 68 69 73 20 6c 61   Jump to this la
27e70 62 65 6c 20 6f 6e 20 61 6e 20 4f 45 5f 49 67 6e  bel on an OE_Ign
27e71 6f 72 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 2a  ore resolution *
27e72 2f 0a 20 20 69 6e 74 20 2a 70 62 4d 61 79 52 65  /.  int *pbMayRe
27e73 70 6c 61 63 65 20 20 20 2f 2a 20 4f 55 54 3a 20  place   /* OUT: 
27e74 53 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 63  Set to true if c
27e75 6f 6e 73 74 72 61 69 6e 74 20 6d 61 79 20 63 61  onstraint may ca
27e76 75 73 65 20 61 20 72 65 70 6c 61 63 65 20 2a 2f  use a replace */
27e77 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  .){.  int i;    
27e78 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c 6f 6f            /* loo
27e79 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 56  p counter */.  V
27e7a 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
27e7b 20 20 20 2f 2a 20 56 44 42 45 20 75 6e 64 65 72     /* VDBE under
27e7c 20 63 6f 6e 73 74 72 75 74 69 6f 6e 20 2a 2f 0a   constrution */.
27e7d 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20    int nCol;     
27e7e 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
27e7f 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  of columns */.  
27e80 69 6e 74 20 6f 6e 45 72 72 6f 72 3b 20 20 20 20  int onError;    
27e81 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20      /* Conflict 
27e82 72 65 73 6f 6c 75 74 69 6f 6e 20 73 74 72 61 74  resolution strat
27e83 65 67 79 20 2a 2f 0a 20 20 69 6e 74 20 6a 31 3b  egy */.  int j1;
27e84 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27e85 41 64 64 72 65 73 73 73 20 6f 66 20 6a 75 6d 70  Addresss of jump
27e86 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a   instruction */.
27e87 20 20 69 6e 74 20 6a 32 20 3d 20 30 2c 20 6a 33    int j2 = 0, j3
27e88 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73  ;     /* Address
27e89 65 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72  es of jump instr
27e8a 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  uctions */.  int
27e8b 20 72 65 67 44 61 74 61 3b 20 20 20 20 20 20 20   regData;       
27e8c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e   /* Register con
27e8d 74 61 69 6e 69 6e 67 20 66 69 72 73 74 20 64 61  taining first da
27e8e 74 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  ta column */.  i
27e8f 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20  nt iCur;        
27e90 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73     /* Table curs
27e91 6f 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 49  or number */.  I
27e92 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
27e93 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
27e94 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 64  o one of the ind
27e95 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 73 65  ices */.  int se
27e96 65 6e 52 65 70 6c 61 63 65 20 3d 20 30 3b 20 2f  enReplace = 0; /
27e97 2a 20 54 72 75 65 20 69 66 20 52 45 50 4c 41 43  * True if REPLAC
27e98 45 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 73  E is used to res
27e99 6f 6c 76 65 20 49 4e 54 20 50 4b 20 63 6f 6e 66  olve INT PK conf
27e9a 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65  lict */.  int re
27e9b 67 4f 6c 64 52 6f 77 69 64 20 3d 20 28 72 6f 77  gOldRowid = (row
27e9c 69 64 43 68 6e 67 20 26 26 20 69 73 55 70 64 61  idChng && isUpda
27e9d 74 65 29 20 3f 20 72 6f 77 69 64 43 68 6e 67 20  te) ? rowidChng 
27e9e 3a 20 72 65 67 52 6f 77 69 64 3b 0a 0a 20 20 76  : regRowid;..  v
27e9f 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
27ea0 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73  e(pParse);.  ass
27ea1 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 61  ert( v!=0 );.  a
27ea2 73 73 65 72 74 28 20 70 54 61 62 2d 3e 70 53 65  ssert( pTab->pSe
27ea3 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 54  lect==0 );  /* T
27ea4 68 69 73 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  his table is not
27ea5 20 61 20 56 49 45 57 20 2a 2f 0a 20 20 6e 43 6f   a VIEW */.  nCo
27ea6 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a  l = pTab->nCol;.
27ea7 20 20 72 65 67 44 61 74 61 20 3d 20 72 65 67 52    regData = regR
27ea8 6f 77 69 64 20 2b 20 31 3b 0a 0a 20 20 2f 2a 20  owid + 1;..  /* 
27ea9 54 65 73 74 20 61 6c 6c 20 4e 4f 54 20 4e 55 4c  Test all NOT NUL
27eaa 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 20  L constraints.. 
27eab 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   */.  for(i=0; i
27eac 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nCol; i++){.   
27ead 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50   if( i==pTab->iP
27eae 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  Key ){.      con
27eaf 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20  tinue;.    }.   
27eb0 20 6f 6e 45 72 72 6f 72 20 3d 20 70 54 61 62 2d   onError = pTab-
27eb1 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c  >aCol[i].notNull
27eb2 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f  ;.    if( onErro
27eb3 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 20 63 6f 6e  r==OE_None ) con
27eb4 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 6f  tinue;.    if( o
27eb5 76 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45  verrideError!=OE
27eb6 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20  _Default ){.    
27eb7 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72    onError = over
27eb8 72 69 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d  rideError;.    }
27eb9 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72  else if( onError
27eba 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a  ==OE_Default ){.
27ebb 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20        onError = 
27ebc 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a  OE_Abort;.    }.
27ebd 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d      if( onError=
27ebe 3d 4f 45 5f 52 65 70 6c 61 63 65 20 26 26 20 70  =OE_Replace && p
27ebf 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66  Tab->aCol[i].pDf
27ec0 6c 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6f  lt==0 ){.      o
27ec1 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72  nError = OE_Abor
27ec2 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  t;.    }.    ass
27ec3 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45  ert( onError==OE
27ec4 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45  _Rollback || onE
27ec5 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c  rror==OE_Abort |
27ec6 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61  | onError==OE_Fa
27ec7 69 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f 6e  il.        || on
27ec8 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65  Error==OE_Ignore
27ec9 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f   || onError==OE_
27eca 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 73  Replace );.    s
27ecb 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29  witch( onError )
27ecc 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f  {.      case OE_
27ecd 41 62 6f 72 74 3a 0a 20 20 20 20 20 20 20 20 73  Abort:.        s
27ece 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70  qlite3MayAbort(p
27ecf 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 63 61  Parse);.      ca
27ed0 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a  se OE_Rollback:.
27ed1 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61        case OE_Fa
27ed2 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 63 68  il: {.        ch
27ed3 61 72 20 2a 7a 4d 73 67 3b 0a 20 20 20 20 20 20  ar *zMsg;.      
27ed4 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
27ed5 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 48  beAddOp3(v, OP_H
27ed6 61 6c 74 49 66 4e 75 6c 6c 2c 0a 20 20 20 20 20  altIfNull,.     
27ed7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ed8 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
27ed9 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20  ITE_CONSTRAINT, 
27eda 6f 6e 45 72 72 6f 72 2c 20 72 65 67 44 61 74 61  onError, regData
27edb 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 7a 4d 73  +i);.        zMs
27edc 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  g = sqlite3MPrin
27edd 74 66 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22  tf(pParse->db, "
27ede 25 73 2e 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  %s.%s may not be
27edf 20 4e 55 4c 4c 22 2c 0a 20 20 20 20 20 20 20 20   NULL",.        
27ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ee1 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
27ee2 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
27ee3 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  .zName);.       
27ee4 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
27ee5 67 65 50 34 28 76 2c 20 2d 31 2c 20 7a 4d 73 67  geP4(v, -1, zMsg
27ee6 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  , P4_DYNAMIC);. 
27ee7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27ee8 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
27ee9 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20   OE_Ignore: {.  
27eea 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27eeb 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73  eAddOp2(v, OP_Is
27eec 4e 75 6c 6c 2c 20 72 65 67 44 61 74 61 2b 69 2c  Null, regData+i,
27eed 20 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20   ignoreDest);.  
27eee 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27eef 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75     }.      defau
27ef0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73  lt: {.        as
27ef1 73 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f  sert( onError==O
27ef2 45 5f 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20  E_Replace );.   
27ef3 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65       j1 = sqlite
27ef4 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
27ef5 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 44 61  P_NotNull, regDa
27ef6 74 61 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 73  ta+i);.        s
27ef7 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
27ef8 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f  Parse, pTab->aCo
27ef9 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 72 65 67 44  l[i].pDflt, regD
27efa 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20 20 20  ata+i);.        
27efb 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
27efc 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20  ere(v, j1);.    
27efd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
27efe 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
27eff 2f 2a 20 54 65 73 74 20 61 6c 6c 20 43 48 45 43  /* Test all CHEC
27f00 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20  K constraints.  
27f01 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
27f02 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 69  E_OMIT_CHECK.  i
27f03 66 28 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20  f( pTab->pCheck 
27f04 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e  && (pParse->db->
27f05 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49  flags & SQLITE_I
27f06 67 6e 6f 72 65 43 68 65 63 6b 73 29 3d 3d 30 20  gnoreChecks)==0 
27f07 29 7b 0a 20 20 20 20 69 6e 74 20 61 6c 6c 4f 6b  ){.    int allOk
27f08 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
27f09 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
27f0a 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 20 3d  pParse->ckBase =
27f0b 20 72 65 67 44 61 74 61 3b 0a 20 20 20 20 73 71   regData;.    sq
27f0c 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
27f0d 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 70 43  pParse, pTab->pC
27f0e 68 65 63 6b 2c 20 61 6c 6c 4f 6b 2c 20 53 51 4c  heck, allOk, SQL
27f0f 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
27f10 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f  .    onError = o
27f11 76 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45  verrideError!=OE
27f12 5f 44 65 66 61 75 6c 74 20 3f 20 6f 76 65 72 72  _Default ? overr
27f13 69 64 65 45 72 72 6f 72 20 3a 20 4f 45 5f 41 62  ideError : OE_Ab
27f14 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45  ort;.    if( onE
27f15 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20  rror==OE_Ignore 
27f16 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
27f17 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27f18 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65  _Goto, 0, ignore
27f19 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Dest);.    }else
27f1a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 48  {.      sqlite3H
27f1b 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 70 50  altConstraint(pP
27f1c 61 72 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 30  arse, onError, 0
27f1d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
27f1e 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
27f1f 76 65 4c 61 62 65 6c 28 76 2c 20 61 6c 6c 4f 6b  veLabel(v, allOk
27f20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  );.  }.#endif /*
27f21 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
27f22 5f 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a  _OMIT_CHECK) */.
27f23 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65  .  /* If we have
27f24 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   an INTEGER PRIM
27f25 41 52 59 20 4b 45 59 2c 20 6d 61 6b 65 20 73 75  ARY KEY, make su
27f26 72 65 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  re the primary k
27f27 65 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e  ey.  ** of the n
27f28 65 77 20 72 65 63 6f 72 64 20 64 6f 65 73 20 6e  ew record does n
27f29 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78  ot previously ex
27f2a 69 73 74 2e 20 20 45 78 63 65 70 74 2c 20 69 66  ist.  Except, if
27f2b 20 74 68 69 73 0a 20 20 2a 2a 20 69 73 20 61 6e   this.  ** is an
27f2c 20 55 50 44 41 54 45 20 61 6e 64 20 74 68 65 20   UPDATE and the 
27f2d 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 6e  primary key is n
27f2e 6f 74 20 63 68 61 6e 67 69 6e 67 2c 20 74 68 61  ot changing, tha
27f2f 74 20 69 73 20 4f 4b 2e 0a 20 20 2a 2f 0a 20 20  t is OK..  */.  
27f30 69 66 28 20 72 6f 77 69 64 43 68 6e 67 20 29 7b  if( rowidChng ){
27f31 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 70  .    onError = p
27f32 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 3b 0a 20 20  Tab->keyConf;.  
27f33 20 20 69 66 28 20 6f 76 65 72 72 69 64 65 45 72    if( overrideEr
27f34 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror!=OE_Default 
27f35 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
27f36 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72   = overrideError
27f37 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
27f38 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
27f39 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45  ult ){.      onE
27f3a 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
27f3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20  .    }.    .    
27f3c 69 66 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a  if( isUpdate ){.
27f3d 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74        j2 = sqlit
27f3e 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
27f3f 4f 50 5f 45 71 2c 20 72 65 67 52 6f 77 69 64 2c  OP_Eq, regRowid,
27f40 20 30 2c 20 72 6f 77 69 64 43 68 6e 67 29 3b 0a   0, rowidChng);.
27f41 20 20 20 20 7d 0a 20 20 20 20 6a 33 20 3d 20 73      }.    j3 = s
27f42 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
27f43 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
27f44 2c 20 62 61 73 65 43 75 72 2c 20 30 2c 20 72 65  , baseCur, 0, re
27f45 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 77 69  gRowid);.    swi
27f46 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a  tch( onError ){.
27f47 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
27f48 0a 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  .        onError
27f49 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
27f4a 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72       /* Fall thr
27f4b 75 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20  u into the next 
27f4c 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  case */.      }.
27f4d 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f        case OE_Ro
27f4e 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61  llback:.      ca
27f4f 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20  se OE_Abort:.   
27f50 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a     case OE_Fail:
27f51 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
27f52 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74  e3HaltConstraint
27f53 28 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72  (.          pPar
27f54 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 22 50 52  se, onError, "PR
27f55 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62  IMARY KEY must b
27f56 65 20 75 6e 69 71 75 65 22 2c 20 50 34 5f 53 54  e unique", P4_ST
27f57 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 62  ATIC);.        b
27f58 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
27f59 20 20 20 20 63 61 73 65 20 4f 45 5f 52 65 70 6c      case OE_Repl
27f5a 61 63 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f  ace: {.        /
27f5b 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 44  * If there are D
27f5c 45 4c 45 54 45 20 74 72 69 67 67 65 72 73 20 6f  ELETE triggers o
27f5d 6e 20 74 68 69 73 20 74 61 62 6c 65 20 61 6e 64  n this table and
27f5e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
27f5f 72 65 63 75 72 73 69 76 65 2d 74 72 69 67 67 65  recursive-trigge
27f60 72 73 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  rs flag is set, 
27f61 63 61 6c 6c 20 47 65 6e 65 72 61 74 65 52 6f 77  call GenerateRow
27f62 44 65 6c 65 74 65 28 29 20 74 6f 0a 20 20 20 20  Delete() to.    
27f63 20 20 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74 68      ** remove th
27f64 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 6f  e conflicting ro
27f65 77 20 66 72 6f 6d 20 74 68 65 20 74 68 65 20 74  w from the the t
27f66 61 62 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20  able. This will 
27f67 66 69 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  fire.        ** 
27f68 74 68 65 20 74 72 69 67 67 65 72 73 20 61 6e 64  the triggers and
27f69 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74 68 65   remove both the
27f6a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78   table and index
27f6b 20 62 2d 74 72 65 65 20 65 6e 74 72 69 65 73 2e   b-tree entries.
27f6c 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
27f6d 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65      ** Otherwise
27f6e 2c 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e  , if there are n
27f6f 6f 20 74 72 69 67 67 65 72 73 20 6f 72 20 74 68  o triggers or th
27f70 65 20 72 65 63 75 72 73 69 76 65 2d 74 72 69 67  e recursive-trig
27f71 67 65 72 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  gers.        ** 
27f72 66 6c 61 67 20 69 73 20 6e 6f 74 20 73 65 74 2c  flag is not set,
27f73 20 63 61 6c 6c 20 47 65 6e 65 72 61 74 65 52 6f   call GenerateRo
27f74 77 49 6e 64 65 78 44 65 6c 65 74 65 28 29 2e 20  wIndexDelete(). 
27f75 54 68 69 73 20 72 65 6d 6f 76 65 73 0a 20 20 20  This removes.   
27f76 20 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 64 65       ** the inde
27f77 78 20 62 2d 74 72 65 65 20 65 6e 74 72 69 65 73  x b-tree entries
27f78 20 6f 6e 6c 79 2e 20 54 68 65 20 74 61 62 6c 65   only. The table
27f79 20 62 2d 74 72 65 65 20 65 6e 74 72 79 20 77 69   b-tree entry wi
27f7a 6c 6c 20 62 65 20 0a 20 20 20 20 20 20 20 20 2a  ll be .        *
27f7b 2a 20 72 65 70 6c 61 63 65 64 20 62 79 20 74 68  * replaced by th
27f7c 65 20 6e 65 77 20 65 6e 74 72 79 20 77 68 65 6e  e new entry when
27f7d 20 69 74 20 69 73 20 69 6e 73 65 72 74 65 64 2e   it is inserted.
27f7e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 54 72 69    */.        Tri
27f7f 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 20 3d  gger *pTrigger =
27f80 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   0;.        if( 
27f81 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67  pParse->db->flag
27f82 73 26 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67  s&SQLITE_RecTrig
27f83 67 65 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20  gers ){.        
27f84 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c    pTrigger = sql
27f85 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73  ite3TriggersExis
27f86 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  t(pParse, pTab, 
27f87 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20 30 29  TK_DELETE, 0, 0)
27f88 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
27f89 20 20 20 20 73 71 6c 69 74 65 33 4d 75 6c 74 69      sqlite3Multi
27f8a 57 72 69 74 65 28 70 50 61 72 73 65 29 3b 0a 20  Write(pParse);. 
27f8b 20 20 20 20 20 20 20 69 66 28 20 70 54 72 69 67         if( pTrig
27f8c 67 65 72 20 7c 7c 20 73 71 6c 69 74 65 33 46 6b  ger || sqlite3Fk
27f8d 52 65 71 75 69 72 65 64 28 70 50 61 72 73 65 2c  Required(pParse,
27f8e 20 70 54 61 62 2c 20 30 2c 20 30 29 20 29 7b 0a   pTab, 0, 0) ){.
27f8f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27f90 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65  3GenerateRowDele
27f91 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  te(.            
27f92 20 20 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20    pParse, pTab, 
27f93 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f 77 69  baseCur, regRowi
27f94 64 2c 20 30 2c 20 70 54 72 69 67 67 65 72 2c 20  d, 0, pTrigger, 
27f95 4f 45 5f 52 65 70 6c 61 63 65 0a 20 20 20 20 20  OE_Replace.     
27f96 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
27f97 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
27f98 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
27f99 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 70  RowIndexDelete(p
27f9a 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73  Parse, pTab, bas
27f9b 65 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  eCur, 0);.      
27f9c 20 20 7d 0a 20 20 20 20 20 20 20 20 73 65 65 6e    }.        seen
27f9d 52 65 70 6c 61 63 65 20 3d 20 31 3b 0a 20 20 20  Replace = 1;.   
27f9e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27f9f 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f    }.      case O
27fa0 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 20  E_Ignore: {.    
27fa1 20 20 20 20 61 73 73 65 72 74 28 20 73 65 65 6e      assert( seen
27fa2 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b 0a 20 20  Replace==0 );.  
27fa3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27fa4 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
27fa5 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65 73  to, 0, ignoreDes
27fa6 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  t);.        brea
27fa7 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
27fa8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27fa9 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b  JumpHere(v, j3);
27faa 0a 20 20 20 20 69 66 28 20 69 73 55 70 64 61 74  .    if( isUpdat
27fab 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
27fac 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
27fad 2c 20 6a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  , j2);.    }.  }
27fae 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20  ..  /* Test all 
27faf 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e  UNIQUE constrain
27fb0 74 73 20 62 79 20 63 72 65 61 74 69 6e 67 20 65  ts by creating e
27fb1 6e 74 72 69 65 73 20 66 6f 72 20 65 61 63 68 20  ntries for each 
27fb2 55 4e 49 51 55 45 0a 20 20 2a 2a 20 69 6e 64 65  UNIQUE.  ** inde
27fb3 78 20 61 6e 64 20 6d 61 6b 69 6e 67 20 73 75 72  x and making sur
27fb4 65 20 74 68 61 74 20 64 75 70 6c 69 63 61 74 65  e that duplicate
27fb5 20 65 6e 74 72 69 65 73 20 64 6f 20 6e 6f 74 20   entries do not 
27fb6 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 0a 20  already exist.. 
27fb7 20 2a 2a 20 41 64 64 20 74 68 65 20 6e 65 77 20   ** Add the new 
27fb8 72 65 63 6f 72 64 73 20 74 6f 20 74 68 65 20 69  records to the i
27fb9 6e 64 69 63 65 73 20 61 73 20 77 65 20 67 6f 2e  ndices as we go.
27fba 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 43 75 72  .  */.  for(iCur
27fbb 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =0, pIdx=pTab->p
27fbc 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
27fbd 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69  x=pIdx->pNext, i
27fbe 43 75 72 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  Cur++){.    int 
27fbf 72 65 67 49 64 78 3b 0a 20 20 20 20 69 6e 74 20  regIdx;.    int 
27fc0 72 65 67 52 3b 0a 0a 20 20 20 20 69 66 28 20 61  regR;..    if( a
27fc1 52 65 67 49 64 78 5b 69 43 75 72 5d 3d 3d 30 20  RegIdx[iCur]==0 
27fc2 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20  ) continue;  /* 
27fc3 53 6b 69 70 20 75 6e 75 73 65 64 20 69 6e 64 69  Skip unused indi
27fc4 63 65 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43  ces */..    /* C
27fc5 72 65 61 74 65 20 61 20 6b 65 79 20 66 6f 72 20  reate a key for 
27fc6 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 69 6e  accessing the in
27fc7 64 65 78 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20  dex entry */.   
27fc8 20 72 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65   regIdx = sqlite
27fc9 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
27fca 61 72 73 65 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c  arse, pIdx->nCol
27fcb 75 6d 6e 2b 31 29 3b 0a 20 20 20 20 66 6f 72 28  umn+1);.    for(
27fcc 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f  i=0; i<pIdx->nCo
27fcd 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
27fce 20 20 69 6e 74 20 69 64 78 20 3d 20 70 49 64 78    int idx = pIdx
27fcf 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20  ->aiColumn[i];. 
27fd0 20 20 20 20 20 69 66 28 20 69 64 78 3d 3d 70 54       if( idx==pT
27fd1 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
27fd2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27fd3 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f  AddOp2(v, OP_SCo
27fd4 70 79 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65  py, regRowid, re
27fd5 67 49 64 78 2b 69 29 3b 0a 20 20 20 20 20 20 7d  gIdx+i);.      }
27fd6 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
27fd7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27fd8 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67  v, OP_SCopy, reg
27fd9 44 61 74 61 2b 69 64 78 2c 20 72 65 67 49 64 78  Data+idx, regIdx
27fda 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  +i);.      }.   
27fdb 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
27fdc 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
27fdd 43 6f 70 79 2c 20 72 65 67 52 6f 77 69 64 2c 20  Copy, regRowid, 
27fde 72 65 67 49 64 78 2b 69 29 3b 0a 20 20 20 20 73  regIdx+i);.    s
27fdf 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
27fe0 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
27fe1 64 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78 2d  d, regIdx, pIdx-
27fe2 3e 6e 43 6f 6c 75 6d 6e 2b 31 2c 20 61 52 65 67  >nColumn+1, aReg
27fe3 49 64 78 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20  Idx[iCur]);.    
27fe4 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
27fe5 65 50 34 28 76 2c 20 2d 31 2c 20 73 71 6c 69 74  eP4(v, -1, sqlit
27fe6 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53  e3IndexAffinityS
27fe7 74 72 28 76 2c 20 70 49 64 78 29 2c 20 30 29 3b  tr(v, pIdx), 0);
27fe8 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
27fe9 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
27fea 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 49  nge(pParse, regI
27feb 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  dx, pIdx->nColum
27fec 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69  n+1);..    /* Fi
27fed 6e 64 20 6f 75 74 20 77 68 61 74 20 61 63 74 69  nd out what acti
27fee 6f 6e 20 74 6f 20 74 61 6b 65 20 69 6e 20 63 61  on to take in ca
27fef 73 65 20 74 68 65 72 65 20 69 73 20 61 6e 20 69  se there is an i
27ff0 6e 64 65 78 69 6e 67 20 63 6f 6e 66 6c 69 63 74  ndexing conflict
27ff1 20 2a 2f 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20   */.    onError 
27ff2 3d 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3b  = pIdx->onError;
27ff3 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72  .    if( onError
27ff4 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 20 0a 20 20  ==OE_None ){ .  
27ff5 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
27ff6 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
27ff7 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78  se, regIdx, pIdx
27ff8 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20  ->nColumn+1);.  
27ff9 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f      continue;  /
27ffa 2a 20 70 49 64 78 20 69 73 20 6e 6f 74 20 61 20  * pIdx is not a 
27ffb 55 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a  UNIQUE index */.
27ffc 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f 76      }.    if( ov
27ffd 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f  errideError!=OE_
27ffe 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20  Default ){.     
27fff 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72   onError = overr
28000 69 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65  ideError;.    }e
28001 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d  lse if( onError=
28002 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
28003 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f       onError = O
28004 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  E_Abort;.    }. 
28005 20 20 20 69 66 28 20 73 65 65 6e 52 65 70 6c 61     if( seenRepla
28006 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ce ){.      if( 
28007 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f  onError==OE_Igno
28008 72 65 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f  re ) onError = O
28009 45 5f 52 65 70 6c 61 63 65 3b 0a 20 20 20 20 20  E_Replace;.     
2800a 20 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f   else if( onErro
2800b 72 3d 3d 4f 45 5f 46 61 69 6c 20 29 20 6f 6e 45  r==OE_Fail ) onE
2800c 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
2800d 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20  .    }.    .    
2800e 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
2800f 69 66 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78  if the new index
28010 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 75   entry will be u
28011 6e 69 71 75 65 20 2a 2f 0a 20 20 20 20 72 65 67  nique */.    reg
28012 52 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  R = sqlite3GetTe
28013 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
28014 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28015 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79  dOp2(v, OP_SCopy
28016 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 72  , regOldRowid, r
28017 65 67 52 29 3b 0a 20 20 20 20 6a 33 20 3d 20 73  egR);.    j3 = s
28018 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
28019 28 76 2c 20 4f 50 5f 49 73 55 6e 69 71 75 65 2c  (v, OP_IsUnique,
2801a 20 62 61 73 65 43 75 72 2b 69 43 75 72 2b 31 2c   baseCur+iCur+1,
2801b 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
2801c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
2801d 65 67 52 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f  egR, SQLITE_INT_
2801e 54 4f 5f 50 54 52 28 72 65 67 49 64 78 29 2c 0a  TO_PTR(regIdx),.
2801f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28020 20 20 20 20 20 20 20 20 20 20 20 50 34 5f 49 4e             P4_IN
28021 54 33 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  T32);.    sqlite
28022 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
28023 65 28 70 50 61 72 73 65 2c 20 72 65 67 49 64 78  e(pParse, regIdx
28024 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b  , pIdx->nColumn+
28025 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65  1);..    /* Gene
28026 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 65  rate code that e
28027 78 65 63 75 74 65 73 20 69 66 20 74 68 65 20 6e  xecutes if the n
28028 65 77 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69  ew index entry i
28029 73 20 6e 6f 74 20 75 6e 69 71 75 65 20 2a 2f 0a  s not unique */.
2802a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72      assert( onEr
2802b 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b  ror==OE_Rollback
2802c 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f   || onError==OE_
2802d 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72 6f 72  Abort || onError
2802e 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20 20  ==OE_Fail.      
2802f 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45    || onError==OE
28030 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45 72 72  _Ignore || onErr
28031 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29  or==OE_Replace )
28032 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e  ;.    switch( on
28033 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 63  Error ){.      c
28034 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a  ase OE_Rollback:
28035 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41  .      case OE_A
28036 62 6f 72 74 3a 0a 20 20 20 20 20 20 63 61 73 65  bort:.      case
28037 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20   OE_Fail: {.    
28038 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20      int j;.     
28039 20 20 20 53 74 72 41 63 63 75 6d 20 65 72 72 4d     StrAccum errM
2803a 73 67 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  sg;.        cons
2803b 74 20 63 68 61 72 20 2a 7a 53 65 70 3b 0a 20 20  t char *zSep;.  
2803c 20 20 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72        char *zErr
2803d 3b 0a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
2803e 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26  e3StrAccumInit(&
2803f 65 72 72 4d 73 67 2c 20 30 2c 20 30 2c 20 32 30  errMsg, 0, 0, 20
28040 30 29 3b 0a 20 20 20 20 20 20 20 20 65 72 72 4d  0);.        errM
28041 73 67 2e 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  sg.db = pParse->
28042 64 62 3b 0a 20 20 20 20 20 20 20 20 7a 53 65 70  db;.        zSep
28043 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   = pIdx->nColumn
28044 3e 31 20 3f 20 22 63 6f 6c 75 6d 6e 73 20 22 20  >1 ? "columns " 
28045 3a 20 22 63 6f 6c 75 6d 6e 20 22 3b 0a 20 20 20  : "column ";.   
28046 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
28047 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a  pIdx->nColumn; j
28048 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63  ++){.          c
28049 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 54 61 62  har *zCol = pTab
2804a 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43  ->aCol[pIdx->aiC
2804b 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b  olumn[j]].zName;
2804c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2804d 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
2804e 28 26 65 72 72 4d 73 67 2c 20 7a 53 65 70 2c 20  (&errMsg, zSep, 
2804f 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  -1);.          z
28050 53 65 70 20 3d 20 22 2c 20 22 3b 0a 20 20 20 20  Sep = ", ";.    
28051 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
28052 41 63 63 75 6d 41 70 70 65 6e 64 28 26 65 72 72  AccumAppend(&err
28053 4d 73 67 2c 20 7a 43 6f 6c 2c 20 2d 31 29 3b 0a  Msg, zCol, -1);.
28054 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
28055 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
28056 6d 41 70 70 65 6e 64 28 26 65 72 72 4d 73 67 2c  mAppend(&errMsg,
28057 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64  .            pId
28058 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22  x->nColumn>1 ? "
28059 20 61 72 65 20 6e 6f 74 20 75 6e 69 71 75 65 22   are not unique"
2805a 20 3a 20 22 20 69 73 20 6e 6f 74 20 75 6e 69 71   : " is not uniq
2805b 75 65 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  ue", -1);.      
2805c 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33    zErr = sqlite3
2805d 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26  StrAccumFinish(&
2805e 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20  errMsg);.       
2805f 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73   sqlite3HaltCons
28060 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c 20 6f  traint(pParse, o
28061 6e 45 72 72 6f 72 2c 20 7a 45 72 72 2c 20 30 29  nError, zErr, 0)
28062 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
28063 33 44 62 46 72 65 65 28 65 72 72 4d 73 67 2e 64  3DbFree(errMsg.d
28064 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  b, zErr);.      
28065 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
28066 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 49  .      case OE_I
28067 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 20 20 20 20  gnore: {.       
28068 20 61 73 73 65 72 74 28 20 73 65 65 6e 52 65 70   assert( seenRep
28069 6c 61 63 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20  lace==0 );.     
2806a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2806b 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2806c 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b   0, ignoreDest);
2806d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2806e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65        }.      de
2806f 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
28070 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
28071 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  er = 0;.        
28072 61 73 73 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d  assert( onError=
28073 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 3b 0a 20  =OE_Replace );. 
28074 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 75         sqlite3Mu
28075 6c 74 69 57 72 69 74 65 28 70 50 61 72 73 65 29  ltiWrite(pParse)
28076 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  ;.        if( pP
28077 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26  arse->db->flags&
28078 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67 65  SQLITE_RecTrigge
28079 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  rs ){.          
2807a 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  pTrigger = sqlit
2807b 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28  e3TriggersExist(
2807c 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b  pParse, pTab, TK
2807d 5f 44 45 4c 45 54 45 2c 20 30 2c 20 30 29 3b 0a  _DELETE, 0, 0);.
2807e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2807f 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74    sqlite3Generat
28080 65 52 6f 77 44 65 6c 65 74 65 28 0a 20 20 20 20  eRowDelete(.    
28081 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20          pParse, 
28082 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72  pTab, baseCur, r
28083 65 67 52 2c 20 30 2c 20 70 54 72 69 67 67 65 72  egR, 0, pTrigger
28084 2c 20 4f 45 5f 52 65 70 6c 61 63 65 0a 20 20 20  , OE_Replace.   
28085 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
28086 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31 3b  seenReplace = 1;
28087 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
28088 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
28089 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2808a 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20  pHere(v, j3);.  
2808b 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2808c 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2808d 72 65 67 52 29 3b 0a 20 20 7d 0a 20 20 0a 20 20  regR);.  }.  .  
2808e 69 66 28 20 70 62 4d 61 79 52 65 70 6c 61 63 65  if( pbMayReplace
2808f 20 29 7b 0a 20 20 20 20 2a 70 62 4d 61 79 52 65   ){.    *pbMayRe
28090 70 6c 61 63 65 20 3d 20 73 65 65 6e 52 65 70 6c  place = seenRepl
28091 61 63 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ace;.  }.}../*.*
28092 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67  * This routine g
28093 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f  enerates code to
28094 20 66 69 6e 69 73 68 20 74 68 65 20 49 4e 53 45   finish the INSE
28095 52 54 20 6f 72 20 55 50 44 41 54 45 20 6f 70 65  RT or UPDATE ope
28096 72 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 77  ration.** that w
28097 61 73 20 73 74 61 72 74 65 64 20 62 79 20 61 20  as started by a 
28098 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71  prior call to sq
28099 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e  lite3GenerateCon
2809a 73 74 72 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a  straintChecks..*
2809b 2a 20 41 20 63 6f 6e 73 65 63 75 74 69 76 65 20  * A consecutive 
2809c 72 61 6e 67 65 20 6f 66 20 72 65 67 69 73 74 65  range of registe
2809d 72 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 72  rs starting at r
2809e 65 67 52 6f 77 69 64 20 63 6f 6e 74 61 69 6e 73  egRowid contains
2809f 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64 20 61 6e   the.** rowid an
280a0 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 74 6f  d the content to
280a1 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a   be inserted..**
280a2 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74  .** The argument
280a3 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  s to this routin
280a4 65 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20  e should be the 
280a5 73 61 6d 65 20 61 73 20 74 68 65 20 66 69 72 73  same as the firs
280a6 74 20 73 69 78 0a 2a 2a 20 61 72 67 75 6d 65 6e  t six.** argumen
280a7 74 73 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e  ts to sqlite3Gen
280a8 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
280a9 68 65 63 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  hecks..*/.SQLITE
280aa 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
280ab 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73  lite3CompleteIns
280ac 65 72 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20  ertion(.  Parse 
280ad 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
280ae 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
280af 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
280b0 70 54 61 62 2c 20 20 20 20 20 20 20 20 2f 2a 20  pTab,        /* 
280b1 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  the table into w
280b2 68 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65  hich we are inse
280b3 72 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62  rting */.  int b
280b4 61 73 65 43 75 72 2c 20 20 20 20 20 20 20 20 2f  aseCur,        /
280b5 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 72 65 61  * Index of a rea
280b6 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70  d/write cursor p
280b7 6f 69 6e 74 69 6e 67 20 61 74 20 70 54 61 62 20  ointing at pTab 
280b8 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69  */.  int regRowi
280b9 64 2c 20 20 20 20 20 20 20 2f 2a 20 52 61 6e 67  d,       /* Rang
280ba 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a  e of content */.
280bb 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 2c 20    int *aRegIdx, 
280bc 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
280bd 72 20 75 73 65 64 20 62 79 20 65 61 63 68 20 69  r used by each i
280be 6e 64 65 78 2e 20 20 30 20 66 6f 72 20 75 6e 75  ndex.  0 for unu
280bf 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20  sed indices */. 
280c0 20 69 6e 74 20 69 73 55 70 64 61 74 65 2c 20 20   int isUpdate,  
280c1 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72       /* True for
280c2 20 55 50 44 41 54 45 2c 20 46 61 6c 73 65 20 66   UPDATE, False f
280c3 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69  or INSERT */.  i
280c4 6e 74 20 61 70 70 65 6e 64 42 69 61 73 2c 20 20  nt appendBias,  
280c5 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
280c6 69 73 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20  is is likely to 
280c7 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a  be an append */.
280c8 20 20 69 6e 74 20 75 73 65 53 65 65 6b 52 65 73    int useSeekRes
280c9 75 6c 74 20 20 20 2f 2a 20 54 72 75 65 20 74 6f  ult   /* True to
280ca 20 73 65 74 20 74 68 65 20 55 53 45 53 45 45 4b   set the USESEEK
280cb 52 45 53 55 4c 54 20 66 6c 61 67 20 6f 6e 20 4f  RESULT flag on O
280cc 50 5f 5b 49 64 78 5d 49 6e 73 65 72 74 20 2a 2f  P_[Idx]Insert */
280cd 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56  .){.  int i;.  V
280ce 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 6e 49  dbe *v;.  int nI
280cf 64 78 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64  dx;.  Index *pId
280d0 78 3b 0a 20 20 75 38 20 70 69 6b 5f 66 6c 61 67  x;.  u8 pik_flag
280d1 73 3b 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  s;.  int regData
280d2 3b 0a 20 20 69 6e 74 20 72 65 67 52 65 63 3b 0a  ;.  int regRec;.
280d3 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
280d4 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
280d5 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
280d6 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d  .  assert( pTab-
280d7 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20  >pSelect==0 );  
280d8 2f 2a 20 54 68 69 73 20 74 61 62 6c 65 20 69 73  /* This table is
280d9 20 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f 0a 20   not a VIEW */. 
280da 20 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49 64   for(nIdx=0, pId
280db 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
280dc 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
280dd 3e 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b  >pNext, nIdx++){
280de 7d 0a 20 20 66 6f 72 28 69 3d 6e 49 64 78 2d 31  }.  for(i=nIdx-1
280df 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20  ; i>=0; i--){.  
280e0 20 20 69 66 28 20 61 52 65 67 49 64 78 5b 69 5d    if( aRegIdx[i]
280e1 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
280e2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
280e3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49  ddOp2(v, OP_IdxI
280e4 6e 73 65 72 74 2c 20 62 61 73 65 43 75 72 2b 69  nsert, baseCur+i
280e5 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 5d 29 3b  +1, aRegIdx[i]);
280e6 0a 20 20 20 20 69 66 28 20 75 73 65 53 65 65 6b  .    if( useSeek
280e7 52 65 73 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  Result ){.      
280e8 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
280e9 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 55 53  eP5(v, OPFLAG_US
280ea 45 53 45 45 4b 52 45 53 55 4c 54 29 3b 0a 20 20  ESEEKRESULT);.  
280eb 20 20 7d 0a 20 20 7d 0a 20 20 72 65 67 44 61 74    }.  }.  regDat
280ec 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20 31  a = regRowid + 1
280ed 3b 0a 20 20 72 65 67 52 65 63 20 3d 20 73 71 6c  ;.  regRec = sql
280ee 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
280ef 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
280f0 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
280f1 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
280f2 67 44 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43 6f  gData, pTab->nCo
280f3 6c 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 73 71  l, regRec);.  sq
280f4 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69  lite3TableAffini
280f5 74 79 53 74 72 28 76 2c 20 70 54 61 62 29 3b 0a  tyStr(v, pTab);.
280f6 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
280f7 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
280f8 28 70 50 61 72 73 65 2c 20 72 65 67 44 61 74 61  (pParse, regData
280f9 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20  , pTab->nCol);. 
280fa 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73   if( pParse->nes
280fb 74 65 64 20 29 7b 0a 20 20 20 20 70 69 6b 5f 66  ted ){.    pik_f
280fc 6c 61 67 73 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  lags = 0;.  }els
280fd 65 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73  e{.    pik_flags
280fe 20 3d 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47   = OPFLAG_NCHANG
280ff 45 3b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73  E;.    pik_flags
28100 20 7c 3d 20 28 69 73 55 70 64 61 74 65 3f 4f 50   |= (isUpdate?OP
28101 46 4c 41 47 5f 49 53 55 50 44 41 54 45 3a 4f 50  FLAG_ISUPDATE:OP
28102 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 29 3b  FLAG_LASTROWID);
28103 0a 20 20 7d 0a 20 20 69 66 28 20 61 70 70 65 6e  .  }.  if( appen
28104 64 42 69 61 73 20 29 7b 0a 20 20 20 20 70 69 6b  dBias ){.    pik
28105 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46 4c 41 47  _flags |= OPFLAG
28106 5f 41 50 50 45 4e 44 3b 0a 20 20 7d 0a 20 20 69  _APPEND;.  }.  i
28107 66 28 20 75 73 65 53 65 65 6b 52 65 73 75 6c 74  f( useSeekResult
28108 20 29 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67   ){.    pik_flag
28109 73 20 7c 3d 20 4f 50 46 4c 41 47 5f 55 53 45 53  s |= OPFLAG_USES
2810a 45 45 4b 52 45 53 55 4c 54 3b 0a 20 20 7d 0a 20  EEKRESULT;.  }. 
2810b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2810c 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
2810d 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 65 63   baseCur, regRec
2810e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 69  , regRowid);.  i
2810f 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74  f( !pParse->nest
28110 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ed ){.    sqlite
28111 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
28112 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65   -1, pTab->zName
28113 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
28114 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  }.  sqlite3VdbeC
28115 68 61 6e 67 65 50 35 28 76 2c 20 70 69 6b 5f 66  hangeP5(v, pik_f
28116 6c 61 67 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  lags);.}../*.** 
28117 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
28118 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20 63 75 72  at will open cur
28119 73 6f 72 73 20 66 6f 72 20 61 20 74 61 62 6c 65  sors for a table
2811a 20 61 6e 64 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20   and for all.** 
2811b 69 6e 64 69 63 65 73 20 6f 66 20 74 68 61 74 20  indices of that 
2811c 74 61 62 6c 65 2e 20 20 54 68 65 20 22 62 61 73  table.  The "bas
2811d 65 43 75 72 22 20 70 61 72 61 6d 65 74 65 72 20  eCur" parameter 
2811e 69 73 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75  is the cursor nu
2811f 6d 62 65 72 20 75 73 65 64 0a 2a 2a 20 66 6f 72  mber used.** for
28120 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 64   the table.  Ind
28121 69 63 65 73 20 61 72 65 20 6f 70 65 6e 65 64 20  ices are opened 
28122 6f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 75  on subsequent cu
28123 72 73 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  rsors..**.** Ret
28124 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
28125 66 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 65  f indices on the
28126 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
28127 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
28128 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e  lite3OpenTableAn
28129 64 49 6e 64 69 63 65 73 28 0a 20 20 50 61 72 73  dIndices(.  Pars
2812a 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20  e *pParse,   /* 
2812b 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2812c 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2812d 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74  ,     /* Table t
2812e 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20  o be opened */. 
2812f 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20   int baseCur,   
28130 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
28131 65 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74  er assigned to t
28132 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
28133 74 20 6f 70 20 20 20 20 20 20 20 20 20 20 20 2f  t op           /
28134 2a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72  * OP_OpenRead or
28135 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a 2f   OP_OpenWrite */
28136 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  .){.  int i;.  i
28137 6e 74 20 69 44 62 3b 0a 20 20 49 6e 64 65 78 20  nt iDb;.  Index 
28138 2a 70 49 64 78 3b 0a 20 20 56 64 62 65 20 2a 76  *pIdx;.  Vdbe *v
28139 3b 0a 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  ;..  if( IsVirtu
2813a 61 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72  al(pTab) ) retur
2813b 6e 20 30 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  n 0;.  iDb = sql
2813c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2813d 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
2813e 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
2813f 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
28140 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73  be(pParse);.  as
28141 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20  sert( v!=0 );.  
28142 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
28143 28 70 50 61 72 73 65 2c 20 62 61 73 65 43 75 72  (pParse, baseCur
28144 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70 29  , iDb, pTab, op)
28145 3b 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64  ;.  for(i=1, pId
28146 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
28147 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
28148 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20  >pNext, i++){.  
28149 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20    KeyInfo *pKey 
2814a 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65  = sqlite3IndexKe
2814b 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49  yinfo(pParse, pI
2814c 64 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  dx);.    assert(
2814d 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d   pIdx->pSchema==
2814e 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b  pTab->pSchema );
2814f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
28150 41 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20 69 2b  AddOp4(v, op, i+
28151 62 61 73 65 43 75 72 2c 20 70 49 64 78 2d 3e 74  baseCur, pIdx->t
28152 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20  num, iDb,.      
28153 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28154 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f  (char*)pKey, P4_
28155 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
28156 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
28157 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78  t((v, "%s", pIdx
28158 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 7d 0a 20  ->zName));.  }. 
28159 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 61   if( pParse->nTa
2815a 62 3c 62 61 73 65 43 75 72 2b 69 20 29 7b 0a 20  b<baseCur+i ){. 
2815b 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20     pParse->nTab 
2815c 3d 20 62 61 73 65 43 75 72 2b 69 3b 0a 20 20 7d  = baseCur+i;.  }
2815d 0a 20 20 72 65 74 75 72 6e 20 69 2d 31 3b 0a 7d  .  return i-1;.}
2815e 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
2815f 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20  _TEST./*.** The 
28160 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c  following global
28161 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63   variable is inc
28162 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65  remented wheneve
28163 72 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 66 65  r the.** transfe
28164 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  r optimization i
28165 73 20 75 73 65 64 2e 20 20 54 68 69 73 20 69 73  s used.  This is
28166 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
28167 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 6f 6e  g.** purposes on
28168 6c 79 20 2d 20 74 6f 20 6d 61 6b 65 20 73 75 72  ly - to make sur
28169 65 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f  e the transfer o
2816a 70 74 69 6d 69 7a 61 74 69 6f 6e 20 72 65 61 6c  ptimization real
2816b 6c 79 0a 2a 2a 20 69 73 20 68 61 70 70 65 6e 69  ly.** is happeni
2816c 6e 67 20 77 68 65 6e 20 69 74 20 69 73 20 73 75  ng when it is su
2816d 70 70 6f 73 65 20 74 6f 2e 0a 2a 2f 0a 53 51 4c  ppose to..*/.SQL
2816e 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2816f 74 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f 75 6e  te3_xferopt_coun
28170 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  t;.#endif /* SQL
28171 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a 23 69  ITE_TEST */...#i
28172 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28173 54 5f 58 46 45 52 5f 4f 50 54 0a 2f 2a 0a 2a 2a  T_XFER_OPT./*.**
28174 20 43 68 65 63 6b 20 74 6f 20 63 6f 6c 6c 61 74   Check to collat
28175 69 6f 6e 20 6e 61 6d 65 73 20 74 6f 20 73 65 65  ion names to see
28176 20 69 66 20 74 68 65 79 20 61 72 65 20 63 6f 6d   if they are com
28177 70 61 74 69 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  patible..*/.stat
28178 69 63 20 69 6e 74 20 78 66 65 72 43 6f 6d 70 61  ic int xferCompa
28179 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 63  tibleCollation(c
2817a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31 2c 20 63  onst char *z1, c
2817b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 29 7b 0a  onst char *z2){.
2817c 20 20 69 66 28 20 7a 31 3d 3d 30 20 29 7b 0a 20    if( z1==0 ){. 
2817d 20 20 20 72 65 74 75 72 6e 20 7a 32 3d 3d 30 3b     return z2==0;
2817e 0a 20 20 7d 0a 20 20 69 66 28 20 7a 32 3d 3d 30  .  }.  if( z2==0
2817f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
28180 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
28181 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 31  qlite3StrICmp(z1
28182 2c 20 7a 32 29 3d 3d 30 3b 0a 7d 0a 0a 0a 2f 2a  , z2)==0;.}.../*
28183 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
28184 20 69 66 20 69 6e 64 65 78 20 70 53 72 63 20 69   if index pSrc i
28185 73 20 63 6f 6d 70 61 74 69 62 6c 65 20 61 73 20  s compatible as 
28186 61 20 73 6f 75 72 63 65 20 6f 66 20 64 61 74 61  a source of data
28187 0a 2a 2a 20 66 6f 72 20 69 6e 64 65 78 20 70 44  .** for index pD
28188 65 73 74 20 69 6e 20 61 6e 20 69 6e 73 65 72 74  est in an insert
28189 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
2818a 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 72 75 6c  zation.  The rul
2818b 65 73 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d 70  es.** for a comp
2818c 61 74 69 62 6c 65 20 69 6e 64 65 78 3a 0a 2a 2a  atible index:.**
2818d 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68 65 20 69  .**    *   The i
2818e 6e 64 65 78 20 69 73 20 6f 76 65 72 20 74 68 65  ndex is over the
2818f 20 73 61 6d 65 20 73 65 74 20 6f 66 20 63 6f 6c   same set of col
28190 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 20 54  umns.**    *   T
28191 68 65 20 73 61 6d 65 20 44 45 53 43 20 61 6e 64  he same DESC and
28192 20 41 53 43 20 6d 61 72 6b 69 6e 67 73 20 6f 63   ASC markings oc
28193 63 75 72 73 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75  curs on all colu
28194 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  mns.**    *   Th
28195 65 20 73 61 6d 65 20 6f 6e 45 72 72 6f 72 20 70  e same onError p
28196 72 6f 63 65 73 73 69 6e 67 20 28 4f 45 5f 41 62  rocessing (OE_Ab
28197 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20  ort, OE_Ignore, 
28198 65 74 63 29 0a 2a 2a 20 20 20 20 2a 20 20 20 54  etc).**    *   T
28199 68 65 20 73 61 6d 65 20 63 6f 6c 6c 61 74 69 6e  he same collatin
2819a 67 20 73 65 71 75 65 6e 63 65 20 6f 6e 20 65 61  g sequence on ea
2819b 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61  ch column.*/.sta
2819c 74 69 63 20 69 6e 74 20 78 66 65 72 43 6f 6d 70  tic int xferComp
2819d 61 74 69 62 6c 65 49 6e 64 65 78 28 49 6e 64 65  atibleIndex(Inde
2819e 78 20 2a 70 44 65 73 74 2c 20 49 6e 64 65 78 20  x *pDest, Index 
2819f 2a 70 53 72 63 29 7b 0a 20 20 69 6e 74 20 69 3b  *pSrc){.  int i;
281a0 0a 20 20 61 73 73 65 72 74 28 20 70 44 65 73 74  .  assert( pDest
281a1 20 26 26 20 70 53 72 63 20 29 3b 0a 20 20 61 73   && pSrc );.  as
281a2 73 65 72 74 28 20 70 44 65 73 74 2d 3e 70 54 61  sert( pDest->pTa
281a3 62 6c 65 21 3d 70 53 72 63 2d 3e 70 54 61 62 6c  ble!=pSrc->pTabl
281a4 65 20 29 3b 0a 20 20 69 66 28 20 70 44 65 73 74  e );.  if( pDest
281a5 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 53 72 63 2d  ->nColumn!=pSrc-
281a6 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  >nColumn ){.    
281a7 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44  return 0;   /* D
281a8 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65 72 20  ifferent number 
281a9 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  of columns */.  
281aa 7d 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 6f  }.  if( pDest->o
281ab 6e 45 72 72 6f 72 21 3d 70 53 72 63 2d 3e 6f 6e  nError!=pSrc->on
281ac 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 72 65 74  Error ){.    ret
281ad 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66  urn 0;   /* Diff
281ae 65 72 65 6e 74 20 63 6f 6e 66 6c 69 63 74 20 72  erent conflict r
281af 65 73 6f 6c 75 74 69 6f 6e 20 73 74 72 61 74 65  esolution strate
281b0 67 69 65 73 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f  gies */.  }.  fo
281b1 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e  r(i=0; i<pSrc->n
281b2 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
281b3 20 20 69 66 28 20 70 53 72 63 2d 3e 61 69 43 6f    if( pSrc->aiCo
281b4 6c 75 6d 6e 5b 69 5d 21 3d 70 44 65 73 74 2d 3e  lumn[i]!=pDest->
281b5 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 29 7b 0a 20  aiColumn[i] ){. 
281b6 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
281b7 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f   /* Different co
281b8 6c 75 6d 6e 73 20 69 6e 64 65 78 65 64 20 2a 2f  lumns indexed */
281b9 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
281ba 53 72 63 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  Src->aSortOrder[
281bb 69 5d 21 3d 70 44 65 73 74 2d 3e 61 53 6f 72 74  i]!=pDest->aSort
281bc 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20  Order[i] ){.    
281bd 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
281be 20 44 69 66 66 65 72 65 6e 74 20 73 6f 72 74 20   Different sort 
281bf 6f 72 64 65 72 73 20 2a 2f 0a 20 20 20 20 7d 0a  orders */.    }.
281c0 20 20 20 20 69 66 28 20 21 78 66 65 72 43 6f 6d      if( !xferCom
281c1 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e  patibleCollation
281c2 28 70 53 72 63 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d  (pSrc->azColl[i]
281c3 2c 70 44 65 73 74 2d 3e 61 7a 43 6f 6c 6c 5b 69  ,pDest->azColl[i
281c4 5d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ]) ){.      retu
281c5 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65  rn 0;   /* Diffe
281c6 72 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73  rent collating s
281c7 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 20 20  equences */.    
281c8 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e  }.  }..  /* If n
281c9 6f 20 74 65 73 74 20 61 62 6f 76 65 20 66 61 69  o test above fai
281ca 6c 73 20 74 68 65 6e 20 74 68 65 20 69 6e 64 69  ls then the indi
281cb 63 65 73 20 6d 75 73 74 20 62 65 20 63 6f 6d 70  ces must be comp
281cc 61 74 69 62 6c 65 20 2a 2f 0a 20 20 72 65 74 75  atible */.  retu
281cd 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn 1;.}../*.** A
281ce 74 74 65 6d 70 74 20 74 68 65 20 74 72 61 6e 73  ttempt the trans
281cf 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  fer optimization
281d0 20 6f 6e 20 49 4e 53 45 52 54 73 20 6f 66 20 74   on INSERTs of t
281d1 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20  he form.**.**   
281d2 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61    INSERT INTO ta
281d3 62 31 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  b1 SELECT * FROM
281d4 20 74 61 62 32 3b 0a 2a 2a 0a 2a 2a 20 54 68 69   tab2;.**.** Thi
281d5 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  s optimization i
281d6 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64  s only attempted
281d7 20 69 66 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29   if.**.**    (1)
281d8 20 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20    tab1 and tab2 
281d9 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c 20 73  have identical s
281da 63 68 65 6d 61 73 20 69 6e 63 6c 75 64 69 6e 67  chemas including
281db 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 20 20 20 20   all the.**     
281dc 20 20 20 20 73 61 6d 65 20 69 6e 64 69 63 65 73      same indices
281dd 20 61 6e 64 20 63 6f 6e 73 74 72 61 69 6e 74 73   and constraints
281de 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 74  .**.**    (2)  t
281df 61 62 31 20 61 6e 64 20 74 61 62 32 20 61 72 65  ab1 and tab2 are
281e0 20 64 69 66 66 65 72 65 6e 74 20 74 61 62 6c 65   different table
281e1 73 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29 20 20  s.**.**    (3)  
281e2 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e 6f  There must be no
281e3 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62   triggers on tab
281e4 31 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29 20 20  1.**.**    (4)  
281e5 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  The result set o
281e6 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  f the SELECT sta
281e7 74 65 6d 65 6e 74 20 69 73 20 22 2a 22 0a 2a 2a  tement is "*".**
281e8 0a 2a 2a 20 20 20 20 28 35 29 20 20 54 68 65 20  .**    (5)  The 
281e9 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
281ea 20 68 61 73 20 6e 6f 20 57 48 45 52 45 2c 20 48   has no WHERE, H
281eb 41 56 49 4e 47 2c 20 4f 52 44 45 52 20 42 59 2c  AVING, ORDER BY,
281ec 20 47 52 4f 55 50 20 42 59 2c 0a 2a 2a 20 20 20   GROUP BY,.**   
281ed 20 20 20 20 20 20 6f 72 20 4c 49 4d 49 54 20 63        or LIMIT c
281ee 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  lause..**.**    
281ef 28 36 29 20 20 54 68 65 20 53 45 4c 45 43 54 20  (6)  The SELECT 
281f0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 73  statement is a s
281f1 69 6d 70 6c 65 20 28 6e 6f 74 20 61 20 63 6f 6d  imple (not a com
281f2 70 6f 75 6e 64 29 20 73 65 6c 65 63 74 20 74 68  pound) select th
281f3 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f  at.**         co
281f4 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 74 61 62 32  ntains only tab2
281f5 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 63 6c 61   in its FROM cla
281f6 75 73 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d  use.**.** This m
281f7 65 74 68 6f 64 20 66 6f 72 20 69 6d 70 6c 65 6d  ethod for implem
281f8 65 6e 74 69 6e 67 20 74 68 65 20 49 4e 53 45 52  enting the INSER
281f9 54 20 74 72 61 6e 73 66 65 72 73 20 72 61 77 20  T transfers raw 
281fa 72 65 63 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20  records from.** 
281fb 74 61 62 32 20 6f 76 65 72 20 74 6f 20 74 61 62  tab2 over to tab
281fc 31 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20  1.  The columns 
281fd 61 72 65 20 6e 6f 74 20 64 65 63 6f 64 65 64 2e  are not decoded.
281fe 20 20 52 61 77 20 72 65 63 6f 72 64 73 20 66 72    Raw records fr
281ff 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 69 63 65  om.** the indice
28200 73 20 6f 66 20 74 61 62 32 20 61 72 65 20 74 72  s of tab2 are tr
28201 61 6e 73 66 65 72 65 64 20 74 6f 20 74 61 62 31  ansfered to tab1
28202 20 61 73 20 77 65 6c 6c 2e 20 20 49 6e 20 73 6f   as well.  In so
28203 20 64 6f 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 72   doing,.** the r
28204 65 73 75 6c 74 69 6e 67 20 74 61 62 31 20 68 61  esulting tab1 ha
28205 73 20 6d 75 63 68 20 6c 65 73 73 20 66 72 61 67  s much less frag
28206 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  mentation..**.**
28207 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
28208 74 75 72 6e 73 20 54 52 55 45 20 69 66 20 74 68  turns TRUE if th
28209 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
2820a 73 20 61 74 74 65 6d 70 74 65 64 2e 20 20 49 66  s attempted.  If
2820b 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 20 63   any.** of the c
2820c 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20  onditions above 
2820d 66 61 69 6c 20 73 6f 20 74 68 61 74 20 74 68 65  fail so that the
2820e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 73 68   optimization sh
2820f 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 61  ould not.** be a
28210 74 74 65 6d 70 74 65 64 2c 20 74 68 65 6e 20 74  ttempted, then t
28211 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
28212 72 6e 73 20 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74  rns FALSE..*/.st
28213 61 74 69 63 20 69 6e 74 20 78 66 65 72 4f 70 74  atic int xferOpt
28214 69 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72  imization(.  Par
28215 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
28216 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
28217 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
28218 2a 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20  *pDest,         
28219 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 77 65 20  /* The table we 
2821a 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e  are inserting in
2821b 74 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  to */.  Select *
2821c 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a  pSelect,      /*
2821d 20 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   A SELECT statem
2821e 65 6e 74 20 74 6f 20 75 73 65 20 61 73 20 74 68  ent to use as th
2821f 65 20 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f  e data source */
28220 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20  .  int onError, 
28221 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
28222 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72  to handle constr
28223 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20  aint errors */. 
28224 20 69 6e 74 20 69 44 62 44 65 73 74 20 20 20 20   int iDbDest    
28225 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
28226 74 61 62 61 73 65 20 6f 66 20 70 44 65 73 74 20  tabase of pDest 
28227 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73 74  */.){.  ExprList
28228 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20   *pEList;       
28229 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2822a 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
2822b 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 54 61  e SELECT */.  Ta
2822c 62 6c 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20  ble *pSrc;      
2822d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2822e 2a 20 54 68 65 20 74 61 62 6c 65 20 69 6e 20 74  * The table in t
2822f 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
28230 66 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 49 6e  f SELECT */.  In
28231 64 65 78 20 2a 70 53 72 63 49 64 78 2c 20 2a 70  dex *pSrcIdx, *p
28232 44 65 73 74 49 64 78 3b 20 20 20 20 20 20 20 2f  DestIdx;       /
28233 2a 20 53 6f 75 72 63 65 20 61 6e 64 20 64 65 73  * Source and des
28234 74 69 6e 61 74 69 6f 6e 20 69 6e 64 69 63 65 73  tination indices
28235 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
28236 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
28237 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 6c 65  ;      /* An ele
28238 6d 65 6e 74 20 6f 66 20 70 53 65 6c 65 63 74 2d  ment of pSelect-
28239 3e 70 53 72 63 20 2a 2f 0a 20 20 69 6e 74 20 69  >pSrc */.  int i
2823a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2823b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2823c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
2823d 20 69 6e 74 20 69 44 62 53 72 63 3b 20 20 20 20   int iDbSrc;    
2823e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2823f 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
28240 65 20 6f 66 20 70 53 72 63 20 2a 2f 0a 20 20 69  e of pSrc */.  i
28241 6e 74 20 69 53 72 63 2c 20 69 44 65 73 74 3b 20  nt iSrc, iDest; 
28242 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28243 2f 2a 20 43 75 72 73 6f 72 73 20 66 72 6f 6d 20  /* Cursors from 
28244 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69  source and desti
28245 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nation */.  int 
28246 61 64 64 72 31 2c 20 61 64 64 72 32 3b 20 20 20  addr1, addr2;   
28247 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28248 4c 6f 6f 70 20 61 64 64 72 65 73 73 65 73 20 2a  Loop addresses *
28249 2f 0a 20 20 69 6e 74 20 65 6d 70 74 79 44 65 73  /.  int emptyDes
2824a 74 54 65 73 74 3b 20 20 20 20 20 20 20 20 20 20  tTest;          
2824b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
2824c 6f 66 20 74 65 73 74 20 66 6f 72 20 65 6d 70 74  of test for empt
2824d 79 20 70 44 65 73 74 20 2a 2f 0a 20 20 69 6e 74  y pDest */.  int
2824e 20 65 6d 70 74 79 53 72 63 54 65 73 74 3b 20 20   emptySrcTest;  
2824f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28250 20 41 64 64 72 65 73 73 20 6f 66 20 74 65 73 74   Address of test
28251 20 66 6f 72 20 65 6d 70 74 79 20 70 53 72 63 20   for empty pSrc 
28252 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20  */.  Vdbe *v;   
28253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28254 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
28255 45 20 77 65 20 61 72 65 20 62 75 69 6c 64 69 6e  E we are buildin
28256 67 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  g */.  KeyInfo *
28257 70 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  pKey;           
28258 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 69          /* Key i
28259 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61  nformation for a
2825a 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  n index */.  int
2825b 20 72 65 67 41 75 74 6f 69 6e 63 3b 20 20 20 20   regAutoinc;    
2825c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2825d 20 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72   Memory register
2825e 20 75 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43   used by AUTOINC
2825f 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 48 61   */.  int destHa
28260 73 55 6e 69 71 75 65 49 64 78 20 3d 20 30 3b 20  sUniqueIdx = 0; 
28261 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
28262 66 20 70 44 65 73 74 20 68 61 73 20 61 20 55 4e  f pDest has a UN
28263 49 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a 20 20  IQUE index */.  
28264 69 6e 74 20 72 65 67 44 61 74 61 2c 20 72 65 67  int regData, reg
28265 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20  Rowid;          
28266 20 2f 2a 20 52 65 67 69 73 74 65 72 73 20 68 6f   /* Registers ho
28267 6c 64 69 6e 67 20 64 61 74 61 20 61 6e 64 20 72  lding data and r
28268 6f 77 69 64 20 2a 2f 0a 0a 20 20 69 66 28 20 70  owid */..  if( p
28269 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20  Select==0 ){.   
2826a 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
2826b 4d 75 73 74 20 62 65 20 6f 66 20 74 68 65 20 66  Must be of the f
2826c 6f 72 6d 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  orm  INSERT INTO
2826d 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20   ... SELECT ... 
2826e 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  */.  }.  if( sql
2826f 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28  ite3TriggerList(
28270 70 50 61 72 73 65 2c 20 70 44 65 73 74 29 20 29  pParse, pDest) )
28271 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
28272 20 20 2f 2a 20 74 61 62 31 20 6d 75 73 74 20 6e    /* tab1 must n
28273 6f 74 20 68 61 76 65 20 74 72 69 67 67 65 72 73  ot have triggers
28274 20 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20   */.  }.#ifndef 
28275 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
28276 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70  UALTABLE.  if( p
28277 44 65 73 74 2d 3e 74 61 62 46 6c 61 67 73 20 26  Dest->tabFlags &
28278 20 54 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a 20   TF_Virtual ){. 
28279 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
2827a 2a 20 74 61 62 31 20 6d 75 73 74 20 6e 6f 74 20  * tab1 must not 
2827b 62 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  be a virtual tab
2827c 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66  le */.  }.#endif
2827d 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d  .  if( onError==
2827e 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20  OE_Default ){.  
2827f 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41    onError = OE_A
28280 62 6f 72 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  bort;.  }.  if( 
28281 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 41 62 6f 72  onError!=OE_Abor
28282 74 20 26 26 20 6f 6e 45 72 72 6f 72 21 3d 4f 45  t && onError!=OE
28283 5f 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20  _Rollback ){.   
28284 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
28285 43 61 6e 6e 6f 74 20 64 6f 20 4f 52 20 52 45 50  Cannot do OR REP
28286 4c 41 43 45 20 6f 72 20 4f 52 20 49 47 4e 4f 52  LACE or OR IGNOR
28287 45 20 6f 72 20 4f 52 20 46 41 49 4c 20 2a 2f 0a  E or OR FAIL */.
28288 20 20 7d 0a 20 20 61 73 73 65 72 74 28 70 53 65    }.  assert(pSe
28289 6c 65 63 74 2d 3e 70 53 72 63 29 3b 20 20 20 2f  lect->pSrc);   /
2828a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 65 76 65 6e  * allocated even
2828b 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
2828c 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
2828d 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 53   if( pSelect->pS
2828e 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a 20  rc->nSrc!=1 ){. 
2828f 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
28290 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6d 75  * FROM clause mu
28291 73 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20  st have exactly 
28292 6f 6e 65 20 74 65 72 6d 20 2a 2f 0a 20 20 7d 0a  one term */.  }.
28293 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70    if( pSelect->p
28294 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63  Src->a[0].pSelec
28295 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
28296 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61  0;   /* FROM cla
28297 75 73 65 20 63 61 6e 6e 6f 74 20 63 6f 6e 74 61  use cannot conta
28298 69 6e 20 61 20 73 75 62 71 75 65 72 79 20 2a 2f  in a subquery */
28299 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65  .  }.  if( pSele
2829a 63 74 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20  ct->pWhere ){.  
2829b 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
2829c 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20   SELECT may not 
2829d 68 61 76 65 20 61 20 57 48 45 52 45 20 63 6c 61  have a WHERE cla
2829e 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  use */.  }.  if(
2829f 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72   pSelect->pOrder
282a0 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  By ){.    return
282a1 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20   0;   /* SELECT 
282a2 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 6e 20  may not have an 
282a3 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
282a4 2a 2f 0a 20 20 7d 0a 20 20 2f 2a 20 44 6f 20 6e  */.  }.  /* Do n
282a5 6f 74 20 6e 65 65 64 20 74 6f 20 74 65 73 74 20  ot need to test 
282a6 66 6f 72 20 61 20 48 41 56 49 4e 47 20 63 6c 61  for a HAVING cla
282a7 75 73 65 2e 20 20 49 66 20 48 41 56 49 4e 47 20  use.  If HAVING 
282a8 69 73 20 70 72 65 73 65 6e 74 20 62 75 74 0a 20  is present but. 
282a9 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20   ** there is no 
282aa 4f 52 44 45 52 20 42 59 2c 20 77 65 20 77 69 6c  ORDER BY, we wil
282ab 6c 20 67 65 74 20 61 6e 20 65 72 72 6f 72 2e 20  l get an error. 
282ac 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74  */.  if( pSelect
282ad 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20  ->pGroupBy ){.  
282ae 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
282af 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20   SELECT may not 
282b0 68 61 76 65 20 61 20 47 52 4f 55 50 20 42 59 20  have a GROUP BY 
282b1 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20  clause */.  }.  
282b2 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 4c 69  if( pSelect->pLi
282b3 6d 69 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  mit ){.    retur
282b4 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54  n 0;   /* SELECT
282b5 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20   may not have a 
282b6 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a  LIMIT clause */.
282b7 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 53    }.  assert( pS
282b8 65 6c 65 63 74 2d 3e 70 4f 66 66 73 65 74 3d 3d  elect->pOffset==
282b9 30 20 29 3b 20 20 2f 2a 20 4d 75 73 74 20 62 65  0 );  /* Must be
282ba 20 73 6f 20 69 66 20 70 4c 69 6d 69 74 3d 3d 30   so if pLimit==0
282bb 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63   */.  if( pSelec
282bc 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20  t->pPrior ){.   
282bd 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
282be 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 62  SELECT may not b
282bf 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65  e a compound que
282c0 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ry */.  }.  if( 
282c1 70 53 65 6c 65 63 74 2d 3e 73 65 6c 46 6c 61 67  pSelect->selFlag
282c2 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 20  s & SF_Distinct 
282c3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
282c4 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79     /* SELECT may
282c5 20 6e 6f 74 20 62 65 20 44 49 53 54 49 4e 43 54   not be DISTINCT
282c6 20 2a 2f 0a 20 20 7d 0a 20 20 70 45 4c 69 73 74   */.  }.  pEList
282c7 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69   = pSelect->pELi
282c8 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45  st;.  assert( pE
282c9 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 69 66 28  List!=0 );.  if(
282ca 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d   pEList->nExpr!=
282cb 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  1 ){.    return 
282cc 30 3b 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75  0;   /* The resu
282cd 6c 74 20 73 65 74 20 6d 75 73 74 20 68 61 76 65  lt set must have
282ce 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 6f 6c   exactly one col
282cf 75 6d 6e 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73  umn */.  }.  ass
282d0 65 72 74 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30  ert( pEList->a[0
282d1 5d 2e 70 45 78 70 72 20 29 3b 0a 20 20 69 66 28  ].pExpr );.  if(
282d2 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
282d3 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 20  xpr->op!=TK_ALL 
282d4 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
282d5 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74     /* The result
282d6 20 73 65 74 20 6d 75 73 74 20 62 65 20 74 68 65   set must be the
282d7 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74 6f   special operato
282d8 72 20 22 2a 22 20 2a 2f 0a 20 20 7d 0a 0a 20 20  r "*" */.  }..  
282d9 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
282da 20 77 65 20 68 61 76 65 20 65 73 74 61 62 6c 69   we have establi
282db 73 68 65 64 20 74 68 61 74 20 74 68 65 20 73 74  shed that the st
282dc 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68  atement is of th
282dd 65 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 20 73  e.  ** correct s
282de 79 6e 74 61 63 74 69 63 20 66 6f 72 6d 20 74 6f  yntactic form to
282df 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20   participate in 
282e0 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f  this optimizatio
282e1 6e 2e 20 20 4e 6f 77 0a 20 20 2a 2a 20 77 65 20  n.  Now.  ** we 
282e2 68 61 76 65 20 74 6f 20 63 68 65 63 6b 20 74 68  have to check th
282e3 65 20 73 65 6d 61 6e 74 69 63 73 2e 0a 20 20 2a  e semantics..  *
282e4 2f 0a 20 20 70 49 74 65 6d 20 3d 20 70 53 65 6c  /.  pItem = pSel
282e5 65 63 74 2d 3e 70 53 72 63 2d 3e 61 3b 0a 20 20  ect->pSrc->a;.  
282e6 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f  pSrc = sqlite3Lo
282e7 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
282e8 2c 20 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  , 0, pItem->zNam
282e9 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62  e, pItem->zDatab
282ea 61 73 65 29 3b 0a 20 20 69 66 28 20 70 53 72 63  ase);.  if( pSrc
282eb 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
282ec 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63  n 0;   /* FROM c
282ed 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63  lause does not c
282ee 6f 6e 74 61 69 6e 20 61 20 72 65 61 6c 20 74 61  ontain a real ta
282ef 62 6c 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  ble */.  }.  if(
282f0 20 70 53 72 63 3d 3d 70 44 65 73 74 20 29 7b 0a   pSrc==pDest ){.
282f1 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
282f2 2f 2a 20 74 61 62 31 20 61 6e 64 20 74 61 62 32  /* tab1 and tab2
282f3 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20   may not be the 
282f4 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  same table */.  
282f5 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
282f6 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
282f7 4c 45 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 74  LE.  if( pSrc->t
282f8 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72  abFlags & TF_Vir
282f9 74 75 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75  tual ){.    retu
282fa 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20  rn 0;   /* tab2 
282fb 6d 75 73 74 20 6e 6f 74 20 62 65 20 61 20 76 69  must not be a vi
282fc 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
282fd 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20   }.#endif.  if( 
282fe 70 53 72 63 2d 3e 70 53 65 6c 65 63 74 20 29 7b  pSrc->pSelect ){
282ff 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
28300 20 2f 2a 20 74 61 62 32 20 6d 61 79 20 6e 6f 74   /* tab2 may not
28301 20 62 65 20 61 20 76 69 65 77 20 2a 2f 0a 20 20   be a view */.  
28302 7d 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 6e  }.  if( pDest->n
28303 43 6f 6c 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 20  Col!=pSrc->nCol 
28304 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
28305 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
28306 63 6f 6c 75 6d 6e 73 20 6d 75 73 74 20 62 65 20  columns must be 
28307 74 68 65 20 73 61 6d 65 20 69 6e 20 74 61 62 31  the same in tab1
28308 20 61 6e 64 20 74 61 62 32 20 2a 2f 0a 20 20 7d   and tab2 */.  }
28309 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50  .  if( pDest->iP
2830a 4b 65 79 21 3d 70 53 72 63 2d 3e 69 50 4b 65 79  Key!=pSrc->iPKey
2830b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2830c 3b 20 20 20 2f 2a 20 42 6f 74 68 20 74 61 62 6c  ;   /* Both tabl
2830d 65 73 20 6d 75 73 74 20 68 61 76 65 20 74 68 65  es must have the
2830e 20 73 61 6d 65 20 49 4e 54 45 47 45 52 20 50 52   same INTEGER PR
2830f 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d  IMARY KEY */.  }
28310 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 44  .  for(i=0; i<pD
28311 65 73 74 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  est->nCol; i++){
28312 0a 20 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e  .    if( pDest->
28313 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79  aCol[i].affinity
28314 21 3d 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e  !=pSrc->aCol[i].
28315 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20  affinity ){.    
28316 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f    return 0;    /
28317 2a 20 41 66 66 69 6e 69 74 79 20 6d 75 73 74 20  * Affinity must 
28318 62 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20 61  be the same on a
28319 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  ll columns */.  
2831a 20 20 7d 0a 20 20 20 20 69 66 28 20 21 78 66 65    }.    if( !xfe
2831b 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61  rCompatibleColla
2831c 74 69 6f 6e 28 70 44 65 73 74 2d 3e 61 43 6f 6c  tion(pDest->aCol
2831d 5b 69 5d 2e 7a 43 6f 6c 6c 2c 20 70 53 72 63 2d  [i].zColl, pSrc-
2831e 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 29 20  >aCol[i].zColl) 
2831f 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
28320 30 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69  0;    /* Collati
28321 6e 67 20 73 65 71 75 65 6e 63 65 20 6d 75 73 74  ng sequence must
28322 20 62 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20   be the same on 
28323 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20  all columns */. 
28324 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 44 65     }.    if( pDe
28325 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e  st->aCol[i].notN
28326 75 6c 6c 20 26 26 20 21 70 53 72 63 2d 3e 61 43  ull && !pSrc->aC
28327 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b  ol[i].notNull ){
28328 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
28329 20 20 20 20 2f 2a 20 74 61 62 32 20 6d 75 73 74      /* tab2 must
2832a 20 62 65 20 4e 4f 54 20 4e 55 4c 4c 20 69 66 20   be NOT NULL if 
2832b 74 61 62 31 20 69 73 20 2a 2f 0a 20 20 20 20 7d  tab1 is */.    }
2832c 0a 20 20 7d 0a 20 20 66 6f 72 28 70 44 65 73 74  .  }.  for(pDest
2832d 49 64 78 3d 70 44 65 73 74 2d 3e 70 49 6e 64 65  Idx=pDest->pInde
2832e 78 3b 20 70 44 65 73 74 49 64 78 3b 20 70 44 65  x; pDestIdx; pDe
2832f 73 74 49 64 78 3d 70 44 65 73 74 49 64 78 2d 3e  stIdx=pDestIdx->
28330 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
28331 70 44 65 73 74 49 64 78 2d 3e 6f 6e 45 72 72 6f  pDestIdx->onErro
28332 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20  r!=OE_None ){.  
28333 20 20 20 20 64 65 73 74 48 61 73 55 6e 69 71 75      destHasUniqu
28334 65 49 64 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  eIdx = 1;.    }.
28335 20 20 20 20 66 6f 72 28 70 53 72 63 49 64 78 3d      for(pSrcIdx=
28336 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b 20 70 53  pSrc->pIndex; pS
28337 72 63 49 64 78 3b 20 70 53 72 63 49 64 78 3d 70  rcIdx; pSrcIdx=p
28338 53 72 63 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  SrcIdx->pNext){.
28339 20 20 20 20 20 20 69 66 28 20 78 66 65 72 43 6f        if( xferCo
2833a 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28 70 44  mpatibleIndex(pD
2833b 65 73 74 49 64 78 2c 20 70 53 72 63 49 64 78 29  estIdx, pSrcIdx)
2833c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
2833d 20 20 20 20 69 66 28 20 70 53 72 63 49 64 78 3d      if( pSrcIdx=
2833e 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
2833f 72 6e 20 30 3b 20 20 20 20 2f 2a 20 70 44 65 73  rn 0;    /* pDes
28340 74 49 64 78 20 68 61 73 20 6e 6f 20 63 6f 72 72  tIdx has no corr
28341 65 73 70 6f 6e 64 69 6e 67 20 69 6e 64 65 78 20  esponding index 
28342 69 6e 20 70 53 72 63 20 2a 2f 0a 20 20 20 20 7d  in pSrc */.    }
28343 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
28344 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20  ITE_OMIT_CHECK. 
28345 20 69 66 28 20 70 44 65 73 74 2d 3e 70 43 68 65   if( pDest->pChe
28346 63 6b 20 26 26 20 21 73 71 6c 69 74 65 33 45 78  ck && !sqlite3Ex
28347 70 72 43 6f 6d 70 61 72 65 28 70 53 72 63 2d 3e  prCompare(pSrc->
28348 70 43 68 65 63 6b 2c 20 70 44 65 73 74 2d 3e 70  pCheck, pDest->p
28349 43 68 65 63 6b 29 20 29 7b 0a 20 20 20 20 72 65  Check) ){.    re
2834a 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 61 62  turn 0;   /* Tab
2834b 6c 65 73 20 68 61 76 65 20 64 69 66 66 65 72 65  les have differe
2834c 6e 74 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61  nt CHECK constra
2834d 69 6e 74 73 2e 20 20 54 69 63 6b 65 74 20 23 32  ints.  Ticket #2
2834e 32 35 32 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69  252 */.  }.#endi
2834f 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 67 65  f..  /* If we ge
28350 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d  t this far, it m
28351 65 61 6e 73 20 65 69 74 68 65 72 3a 0a 20 20 2a  eans either:.  *
28352 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20 57 65  *.  **    *   We
28353 20 63 61 6e 20 61 6c 77 61 79 73 20 64 6f 20 74   can always do t
28354 68 65 20 74 72 61 6e 73 66 65 72 20 69 66 20 74  he transfer if t
28355 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
28356 73 20 61 6e 0a 20 20 2a 2a 20 20 20 20 20 20 20  s an.  **       
28357 20 61 6e 20 69 6e 74 65 67 65 72 20 70 72 69 6d   an integer prim
28358 61 72 79 20 6b 65 79 0a 20 20 2a 2a 0a 20 20 2a  ary key.  **.  *
28359 2a 20 20 20 20 2a 20 20 20 57 65 20 63 61 6e 20  *    *   We can 
2835a 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 64 6f  conditionally do
2835b 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 69 66   the transfer if
2835c 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
2835d 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 74 61 62  .  **        tab
2835e 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 20 20 2a  le is empty..  *
2835f 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
28360 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 78  TEST.  sqlite3_x
28361 66 65 72 6f 70 74 5f 63 6f 75 6e 74 2b 2b 3b 0a  feropt_count++;.
28362 23 65 6e 64 69 66 0a 20 20 69 44 62 53 72 63 20  #endif.  iDbSrc 
28363 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
28364 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
28365 62 2c 20 70 53 72 63 2d 3e 70 53 63 68 65 6d 61  b, pSrc->pSchema
28366 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  );.  v = sqlite3
28367 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
28368 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65  .  sqlite3CodeVe
28369 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73  rifySchema(pPars
2836a 65 2c 20 69 44 62 53 72 63 29 3b 0a 20 20 69 53  e, iDbSrc);.  iS
2836b 72 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  rc = pParse->nTa
2836c 62 2b 2b 3b 0a 20 20 69 44 65 73 74 20 3d 20 70  b++;.  iDest = p
2836d 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
2836e 20 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 61 75   regAutoinc = au
2836f 74 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73  toIncBegin(pPars
28370 65 2c 20 69 44 62 44 65 73 74 2c 20 70 44 65 73  e, iDbDest, pDes
28371 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70 65  t);.  sqlite3Ope
28372 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  nTable(pParse, i
28373 44 65 73 74 2c 20 69 44 62 44 65 73 74 2c 20 70  Dest, iDbDest, p
28374 44 65 73 74 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  Dest, OP_OpenWri
28375 74 65 29 3b 0a 20 20 69 66 28 20 28 70 44 65 73  te);.  if( (pDes
28376 74 2d 3e 69 50 4b 65 79 3c 30 20 26 26 20 70 44  t->iPKey<0 && pD
28377 65 73 74 2d 3e 70 49 6e 64 65 78 21 3d 30 29 20  est->pIndex!=0) 
28378 7c 7c 20 64 65 73 74 48 61 73 55 6e 69 71 75 65  || destHasUnique
28379 49 64 78 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  Idx ){.    /* If
2837a 20 74 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 68   tables do not h
2837b 61 76 65 20 61 6e 20 49 4e 54 45 47 45 52 20 50  ave an INTEGER P
2837c 52 49 4d 41 52 59 20 4b 45 59 20 61 6e 64 20 74  RIMARY KEY and t
2837d 68 65 72 65 0a 20 20 20 20 2a 2a 20 61 72 65 20  here.    ** are 
2837e 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 6f  indices to be co
2837f 70 69 65 64 20 61 6e 64 20 74 68 65 20 64 65 73  pied and the des
28380 74 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20  tination is not 
28381 65 6d 70 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65  empty,.    ** we
28382 20 68 61 76 65 20 74 6f 20 64 69 73 61 6c 6c 6f   have to disallo
28383 77 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f  w the transfer o
28384 70 74 69 6d 69 7a 61 74 69 6f 6e 20 62 65 63 61  ptimization beca
28385 75 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 74  use the.    ** t
28386 68 65 20 72 6f 77 69 64 73 20 6d 69 67 68 74 20  he rowids might 
28387 63 68 61 6e 67 65 20 77 68 69 63 68 20 77 69 6c  change which wil
28388 6c 20 6d 65 73 73 20 75 70 20 69 6e 64 65 78 69  l mess up indexi
28389 6e 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ng..    **.    *
2838a 2a 20 4f 72 20 69 66 20 74 68 65 20 64 65 73 74  * Or if the dest
2838b 69 6e 61 74 69 6f 6e 20 68 61 73 20 61 20 55 4e  ination has a UN
2838c 49 51 55 45 20 69 6e 64 65 78 20 61 6e 64 20 69  IQUE index and i
2838d 73 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20 20 20  s not empty,.   
2838e 20 2a 2a 20 77 65 20 61 6c 73 6f 20 64 69 73 61   ** we also disa
2838f 6c 6c 6f 77 20 74 68 65 20 74 72 61 6e 73 66 65  llow the transfe
28390 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 62  r optimization b
28391 65 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74  ecause we cannot
28392 0a 20 20 20 20 2a 2a 20 69 6e 73 75 72 65 20 74  .    ** insure t
28393 68 61 74 20 61 6c 6c 20 65 6e 74 72 69 65 73 20  hat all entries 
28394 69 6e 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  in the union of 
28395 44 45 53 54 20 61 6e 64 20 53 52 43 20 77 69 6c  DEST and SRC wil
28396 6c 20 62 65 0a 20 20 20 20 2a 2a 20 75 6e 69 71  l be.    ** uniq
28397 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  ue..    */.    a
28398 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
28399 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2839a 65 77 69 6e 64 2c 20 69 44 65 73 74 2c 20 30 29  ewind, iDest, 0)
2839b 3b 0a 20 20 20 20 65 6d 70 74 79 44 65 73 74 54  ;.    emptyDestT
2839c 65 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  est = sqlite3Vdb
2839d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2839e 74 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  to, 0, 0);.    s
2839f 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
283a0 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20  re(v, addr1);.  
283a1 7d 65 6c 73 65 7b 0a 20 20 20 20 65 6d 70 74 79  }else{.    empty
283a2 44 65 73 74 54 65 73 74 20 3d 20 30 3b 0a 20 20  DestTest = 0;.  
283a3 7d 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  }.  sqlite3OpenT
283a4 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 53 72  able(pParse, iSr
283a5 63 2c 20 69 44 62 53 72 63 2c 20 70 53 72 63 2c  c, iDbSrc, pSrc,
283a6 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20   OP_OpenRead);. 
283a7 20 65 6d 70 74 79 53 72 63 54 65 73 74 20 3d 20   emptySrcTest = 
283a8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
283a9 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  2(v, OP_Rewind, 
283aa 69 53 72 63 2c 20 30 29 3b 0a 20 20 72 65 67 44  iSrc, 0);.  regD
283ab 61 74 61 20 3d 20 73 71 6c 69 74 65 33 47 65 74  ata = sqlite3Get
283ac 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
283ad 0a 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71  .  regRowid = sq
283ae 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
283af 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 70  pParse);.  if( p
283b0 44 65 73 74 2d 3e 69 50 4b 65 79 3e 3d 30 20 29  Dest->iPKey>=0 )
283b1 7b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71  {.    addr1 = sq
283b2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
283b3 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 53 72  v, OP_Rowid, iSr
283b4 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  c, regRowid);.  
283b5 20 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74 65    addr2 = sqlite
283b6 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
283b7 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 44 65  P_NotExists, iDe
283b8 73 74 2c 20 30 2c 20 72 65 67 52 6f 77 69 64 29  st, 0, regRowid)
283b9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c  ;.    sqlite3Hal
283ba 74 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 20  tConstraint(.   
283bb 20 20 20 20 20 70 50 61 72 73 65 2c 20 6f 6e 45       pParse, onE
283bc 72 72 6f 72 2c 20 22 50 52 49 4d 41 52 59 20 4b  rror, "PRIMARY K
283bd 45 59 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75  EY must be uniqu
283be 65 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  e", P4_STATIC);.
283bf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
283c0 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 32  umpHere(v, addr2
283c1 29 3b 0a 20 20 20 20 61 75 74 6f 49 6e 63 53 74  );.    autoIncSt
283c2 65 70 28 70 50 61 72 73 65 2c 20 72 65 67 41 75  ep(pParse, regAu
283c3 74 6f 69 6e 63 2c 20 72 65 67 52 6f 77 69 64 29  toinc, regRowid)
283c4 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 44  ;.  }else if( pD
283c5 65 73 74 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29  est->pIndex==0 )
283c6 7b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71  {.    addr1 = sq
283c7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
283c8 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
283c9 69 44 65 73 74 2c 20 72 65 67 52 6f 77 69 64 29  iDest, regRowid)
283ca 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
283cb 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
283cc 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
283cd 6f 77 69 64 2c 20 69 53 72 63 2c 20 72 65 67 52  owid, iSrc, regR
283ce 6f 77 69 64 29 3b 0a 20 20 20 20 61 73 73 65 72  owid);.    asser
283cf 74 28 20 28 70 44 65 73 74 2d 3e 74 61 62 46 6c  t( (pDest->tabFl
283d0 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63  ags & TF_Autoinc
283d1 72 65 6d 65 6e 74 29 3d 3d 30 20 29 3b 0a 20 20  rement)==0 );.  
283d2 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
283d3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 44  ddOp2(v, OP_RowD
283d4 61 74 61 2c 20 69 53 72 63 2c 20 72 65 67 44 61  ata, iSrc, regDa
283d5 74 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ta);.  sqlite3Vd
283d6 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
283d7 6e 73 65 72 74 2c 20 69 44 65 73 74 2c 20 72 65  nsert, iDest, re
283d8 67 44 61 74 61 2c 20 72 65 67 52 6f 77 69 64 29  gData, regRowid)
283d9 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ;.  sqlite3VdbeC
283da 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
283db 47 5f 4e 43 48 41 4e 47 45 7c 4f 50 46 4c 41 47  G_NCHANGE|OPFLAG
283dc 5f 4c 41 53 54 52 4f 57 49 44 7c 4f 50 46 4c 41  _LASTROWID|OPFLA
283dd 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 73 71 6c  G_APPEND);.  sql
283de 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
283df 28 76 2c 20 2d 31 2c 20 70 44 65 73 74 2d 3e 7a  (v, -1, pDest->z
283e0 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 73 71 6c 69  Name, 0);.  sqli
283e1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
283e2 20 4f 50 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20   OP_Next, iSrc, 
283e3 61 64 64 72 31 29 3b 0a 20 20 66 6f 72 28 70 44  addr1);.  for(pD
283e4 65 73 74 49 64 78 3d 70 44 65 73 74 2d 3e 70 49  estIdx=pDest->pI
283e5 6e 64 65 78 3b 20 70 44 65 73 74 49 64 78 3b 20  ndex; pDestIdx; 
283e6 70 44 65 73 74 49 64 78 3d 70 44 65 73 74 49 64  pDestIdx=pDestId
283e7 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 66  x->pNext){.    f
283e8 6f 72 28 70 53 72 63 49 64 78 3d 70 53 72 63 2d  or(pSrcIdx=pSrc-
283e9 3e 70 49 6e 64 65 78 3b 20 41 4c 57 41 59 53 28  >pIndex; ALWAYS(
283ea 70 53 72 63 49 64 78 29 3b 20 70 53 72 63 49 64  pSrcIdx); pSrcId
283eb 78 3d 70 53 72 63 49 64 78 2d 3e 70 4e 65 78 74  x=pSrcIdx->pNext
283ec 29 7b 0a 20 20 20 20 20 20 69 66 28 20 78 66 65  ){.      if( xfe
283ed 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65 78  rCompatibleIndex
283ee 28 70 44 65 73 74 49 64 78 2c 20 70 53 72 63 49  (pDestIdx, pSrcI
283ef 64 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  dx) ) break;.   
283f0 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
283f1 53 72 63 49 64 78 20 29 3b 0a 20 20 20 20 73 71  SrcIdx );.    sq
283f2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
283f3 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72  v, OP_Close, iSr
283f4 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  c, 0);.    sqlit
283f5 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
283f6 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c  OP_Close, iDest,
283f7 20 30 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20   0);.    pKey = 
283f8 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
283f9 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 53 72 63  nfo(pParse, pSrc
283fa 49 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Idx);.    sqlite
283fb 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
283fc 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 53 72 63  P_OpenRead, iSrc
283fd 2c 20 70 53 72 63 49 64 78 2d 3e 74 6e 75 6d 2c  , pSrcIdx->tnum,
283fe 20 69 44 62 53 72 63 2c 0a 20 20 20 20 20 20 20   iDbSrc,.       
283ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
28400 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b  char*)pKey, P4_K
28401 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
28402 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
28403 28 28 76 2c 20 22 25 73 22 2c 20 70 53 72 63 49  ((v, "%s", pSrcI
28404 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20  dx->zName));.   
28405 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49   pKey = sqlite3I
28406 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
28407 73 65 2c 20 70 44 65 73 74 49 64 78 29 3b 0a 20  se, pDestIdx);. 
28408 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28409 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  dOp4(v, OP_OpenW
2840a 72 69 74 65 2c 20 69 44 65 73 74 2c 20 70 44 65  rite, iDest, pDe
2840b 73 74 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62  stIdx->tnum, iDb
2840c 44 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20  Dest,.          
2840d 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
2840e 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49  r*)pKey, P4_KEYI
2840f 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
28410 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
28411 2c 20 22 25 73 22 2c 20 70 44 65 73 74 49 64 78  , "%s", pDestIdx
28412 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61  ->zName));.    a
28413 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
28414 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
28415 65 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b  ewind, iSrc, 0);
28416 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
28417 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
28418 4b 65 79 2c 20 69 53 72 63 2c 20 72 65 67 44 61  Key, iSrc, regDa
28419 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ta);.    sqlite3
2841a 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2841b 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 44 65 73  _IdxInsert, iDes
2841c 74 2c 20 72 65 67 44 61 74 61 2c 20 31 29 3b 0a  t, regData, 1);.
2841d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2841e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
2841f 2c 20 69 53 72 63 2c 20 61 64 64 72 31 2b 31 29  , iSrc, addr1+1)
28420 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
28421 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
28422 72 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  r1);.  }.  sqlit
28423 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
28424 2c 20 65 6d 70 74 79 53 72 63 54 65 73 74 29 3b  , emptySrcTest);
28425 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
28426 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
28427 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 73 71   regRowid);.  sq
28428 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
28429 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 44  Reg(pParse, regD
2842a 61 74 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ata);.  sqlite3V
2842b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2842c 43 6c 6f 73 65 2c 20 69 53 72 63 2c 20 30 29 3b  Close, iSrc, 0);
2842d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2842e 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
2842f 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 69  , iDest, 0);.  i
28430 66 28 20 65 6d 70 74 79 44 65 73 74 54 65 73 74  f( emptyDestTest
28431 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
28432 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28433 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c  Halt, SQLITE_OK,
28434 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
28435 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
28436 65 6d 70 74 79 44 65 73 74 54 65 73 74 29 3b 0a  emptyDestTest);.
28437 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28438 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
28439 65 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20  e, iDest, 0);.  
2843a 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65    return 0;.  }e
2843b 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
2843c 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  1;.  }.}.#endif 
2843d 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58  /* SQLITE_OMIT_X
2843e 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a 2f 2a 2a 2a  FER_OPT */../***
2843f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
28440 6f 66 20 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a  of insert.c ****
28441 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28442 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28443 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
28444 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
28445 6e 20 66 69 6c 65 20 6c 65 67 61 63 79 2e 63 20  n file legacy.c 
28446 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28447 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28448 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
28449 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
2844a 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
2844b 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
2844c 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
2844d 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
2844e 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
2844f 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
28450 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
28451 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
28452 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
28453 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
28454 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
28455 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
28456 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
28457 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
28458 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
28459 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
2845a 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
2845b 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
2845c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2845d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2845e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2845f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28460 2a 2a 0a 2a 2a 20 4d 61 69 6e 20 66 69 6c 65 20  **.** Main file 
28461 66 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20 6c  for the SQLite l
28462 69 62 72 61 72 79 2e 20 20 54 68 65 20 72 6f 75  ibrary.  The rou
28463 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
28464 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  le.** implement 
28465 74 68 65 20 70 72 6f 67 72 61 6d 6d 65 72 20 69  the programmer i
28466 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20  nterface to the 
28467 6c 69 62 72 61 72 79 2e 20 20 52 6f 75 74 69 6e  library.  Routin
28468 65 73 20 69 6e 0a 2a 2a 20 6f 74 68 65 72 20 66  es in.** other f
28469 69 6c 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74  iles are for int
2846a 65 72 6e 61 6c 20 75 73 65 20 62 79 20 53 51 4c  ernal use by SQL
2846b 69 74 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e  ite and should n
2846c 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 65  ot be.** accesse
2846d 64 20 62 79 20 75 73 65 72 73 20 6f 66 20 74 68  d by users of th
2846e 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a  e library..**.**
2846f 20 24 49 64 3a 20 6c 65 67 61 63 79 2e 63 2c 76   $Id: legacy.c,v
28470 20 31 2e 33 35 20 32 30 30 39 2f 30 38 2f 30 37   1.35 2009/08/07
28471 20 31 36 3a 35 36 3a 30 30 20 64 61 6e 69 65 6c   16:56:00 daniel
28472 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
28473 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 53  ./*.** Execute S
28474 51 4c 20 63 6f 64 65 2e 20 20 52 65 74 75 72 6e  QL code.  Return
28475 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49   one of the SQLI
28476 54 45 5f 20 73 75 63 63 65 73 73 2f 66 61 69 6c  TE_ success/fail
28477 75 72 65 0a 2a 2a 20 63 6f 64 65 73 2e 20 20 41  ure.** codes.  A
28478 6c 73 6f 20 77 72 69 74 65 20 61 6e 20 65 72 72  lso write an err
28479 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20  or message into 
2847a 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
2847b 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29  from.** malloc()
2847c 20 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 45 72 72   and make *pzErr
2847d 4d 73 67 20 70 6f 69 6e 74 20 74 6f 20 74 68 61  Msg point to tha
2847e 74 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a  t message..**.**
2847f 20 49 66 20 74 68 65 20 53 51 4c 20 69 73 20 61   If the SQL is a
28480 20 71 75 65 72 79 2c 20 74 68 65 6e 20 66 6f 72   query, then for
28481 20 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65   each row in the
28482 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a   query result.**
28483 20 74 68 65 20 78 43 61 6c 6c 62 61 63 6b 28 29   the xCallback()
28484 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
28485 6c 65 64 2e 20 20 70 41 72 67 20 62 65 63 6f 6d  led.  pArg becom
28486 65 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  es the first.** 
28487 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 61 6c  argument to xCal
28488 6c 62 61 63 6b 28 29 2e 20 20 49 66 20 78 43 61  lback().  If xCa
28489 6c 6c 62 61 63 6b 3d 4e 55 4c 4c 20 74 68 65 6e  llback=NULL then
2848a 20 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20   no callback.** 
2848b 69 73 20 69 6e 76 6f 6b 65 64 2c 20 65 76 65 6e  is invoked, even
2848c 20 66 6f 72 20 71 75 65 72 69 65 73 2e 0a 2a 2f   for queries..*/
2848d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2848e 73 71 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20  sqlite3_exec(.  
2848f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
28490 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
28491 68 65 20 64 61 74 61 62 61 73 65 20 6f 6e 20 77  he database on w
28492 68 69 63 68 20 74 68 65 20 53 51 4c 20 65 78 65  hich the SQL exe
28493 63 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  cutes */.  const
28494 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20   char *zSql,    
28495 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 51         /* The SQ
28496 4c 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64  L to be executed
28497 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 61   */.  sqlite3_ca
28498 6c 6c 62 61 63 6b 20 78 43 61 6c 6c 62 61 63 6b  llback xCallback
28499 2c 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 69 73  , /* Invoke this
2849a 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e   callback routin
2849b 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72  e */.  void *pAr
2849c 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g,              
2849d 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
2849e 6d 65 6e 74 20 74 6f 20 78 43 61 6c 6c 62 61 63  ment to xCallbac
2849f 6b 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  k() */.  char **
284a0 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20 20  pzErrMsg        
284a1 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72       /* Write er
284a2 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 72  ror messages her
284a3 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
284a4 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
284a5 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
284a6 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  code */.  const 
284a7 63 68 61 72 20 2a 7a 4c 65 66 74 6f 76 65 72 3b  char *zLeftover;
284a8 20 20 20 20 20 20 2f 2a 20 54 61 69 6c 20 6f 66        /* Tail of
284a9 20 75 6e 70 72 6f 63 65 73 73 65 64 20 53 51 4c   unprocessed SQL
284aa 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
284ab 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 20 20  mt *pStmt = 0;  
284ac 20 20 2f 2a 20 54 68 65 20 63 75 72 72 65 6e 74    /* The current
284ad 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a   SQL statement *
284ae 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c  /.  char **azCol
284af 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  s = 0;          
284b0 2f 2a 20 4e 61 6d 65 73 20 6f 66 20 72 65 73 75  /* Names of resu
284b1 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  lt columns */.  
284b2 69 6e 74 20 6e 52 65 74 72 79 20 3d 20 30 3b 20  int nRetry = 0; 
284b3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
284b4 75 6d 62 65 72 20 6f 66 20 72 65 74 72 79 20 61  umber of retry a
284b5 74 74 65 6d 70 74 73 20 2a 2f 0a 20 20 69 6e 74  ttempts */.  int
284b6 20 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 3b   callbackIsInit;
284b7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
284b8 20 69 66 20 63 61 6c 6c 62 61 63 6b 20 64 61 74   if callback dat
284b9 61 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  a is initialized
284ba 20 2a 2f 0a 0a 20 20 69 66 28 20 7a 53 71 6c 3d   */..  if( zSql=
284bb 3d 30 20 29 20 7a 53 71 6c 20 3d 20 22 22 3b 0a  =0 ) zSql = "";.
284bc 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
284bd 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
284be 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f  );.  sqlite3Erro
284bf 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c  r(db, SQLITE_OK,
284c0 20 30 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 72   0);.  while( (r
284c1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
284c2 28 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45  (rc==SQLITE_SCHE
284c3 4d 41 20 26 26 20 28 2b 2b 6e 52 65 74 72 79 29  MA && (++nRetry)
284c4 3c 32 29 29 20 26 26 20 7a 53 71 6c 5b 30 5d 20  <2)) && zSql[0] 
284c5 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b  ){.    int nCol;
284c6 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a 56 61  .    char **azVa
284c7 6c 73 20 3d 20 30 3b 0a 0a 20 20 20 20 70 53 74  ls = 0;..    pSt
284c8 6d 74 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  mt = 0;.    rc =
284c9 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
284ca 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  (db, zSql, -1, &
284cb 70 53 74 6d 74 2c 20 26 7a 4c 65 66 74 6f 76 65  pStmt, &zLeftove
284cc 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  r);.    assert( 
284cd 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
284ce 20 70 53 74 6d 74 3d 3d 30 20 29 3b 0a 20 20 20   pStmt==0 );.   
284cf 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
284d0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74  OK ){.      cont
284d1 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  inue;.    }.    
284d2 69 66 28 20 21 70 53 74 6d 74 20 29 7b 0a 20 20  if( !pStmt ){.  
284d3 20 20 20 20 2f 2a 20 74 68 69 73 20 68 61 70 70      /* this happ
284d4 65 6e 73 20 66 6f 72 20 61 20 63 6f 6d 6d 65 6e  ens for a commen
284d5 74 20 6f 72 20 77 68 69 74 65 2d 73 70 61 63 65  t or white-space
284d6 20 2a 2f 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d   */.      zSql =
284d7 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20 20 20   zLeftover;.    
284d8 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
284d9 7d 0a 0a 20 20 20 20 63 61 6c 6c 62 61 63 6b 49  }..    callbackI
284da 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 6e  sInit = 0;.    n
284db 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  Col = sqlite3_co
284dc 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74  lumn_count(pStmt
284dd 29 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 31  );..    while( 1
284de 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   ){.      int i;
284df 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
284e0 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b  te3_step(pStmt);
284e1 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b  ..      /* Invok
284e2 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  e the callback f
284e3 75 6e 63 74 69 6f 6e 20 69 66 20 72 65 71 75 69  unction if requi
284e4 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  red */.      if(
284e5 20 78 43 61 6c 6c 62 61 63 6b 20 26 26 20 28 53   xCallback && (S
284e6 51 4c 49 54 45 5f 52 4f 57 3d 3d 72 63 20 7c 7c  QLITE_ROW==rc ||
284e7 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c   .          (SQL
284e8 49 54 45 5f 44 4f 4e 45 3d 3d 72 63 20 26 26 20  ITE_DONE==rc && 
284e9 21 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 0a  !callbackIsInit.
284ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284eb 20 20 20 20 20 20 20 20 20 20 20 26 26 20 64 62             && db
284ec 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 4e  ->flags&SQLITE_N
284ed 75 6c 6c 43 61 6c 6c 62 61 63 6b 29 29 20 29 7b  ullCallback)) ){
284ee 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 63 61  .        if( !ca
284ef 6c 6c 62 61 63 6b 49 73 49 6e 69 74 20 29 7b 0a  llbackIsInit ){.
284f0 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f 6c 73            azCols
284f1 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
284f2 6f 63 5a 65 72 6f 28 64 62 2c 20 32 2a 6e 43 6f  ocZero(db, 2*nCo
284f3 6c 2a 73 69 7a 65 6f 66 28 63 6f 6e 73 74 20 63  l*sizeof(const c
284f4 68 61 72 2a 29 20 2b 20 31 29 3b 0a 20 20 20 20  har*) + 1);.    
284f5 20 20 20 20 20 20 69 66 28 20 61 7a 43 6f 6c 73        if( azCols
284f6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
284f7 20 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74     goto exec_out
284f8 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
284f9 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
284fa 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
284fb 20 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f 6c             azCol
284fc 73 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a 29 73  s[i] = (char *)s
284fd 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
284fe 6d 65 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20  me(pStmt, i);.  
284ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c            /* sql
28500 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
28501 6d 65 28 29 20 69 6e 73 74 61 6c 6c 73 20 63 6f  me() installs co
28502 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 73 20 55 54  lumn names as UT
28503 46 38 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  F8.            *
28504 2a 20 73 74 72 69 6e 67 73 20 73 6f 20 74 68 65  * strings so the
28505 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72  re is no way for
28506 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28507 6e 61 6d 65 28 29 20 74 6f 20 66 61 69 6c 2e 20  name() to fail. 
28508 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  */.            a
28509 73 73 65 72 74 28 20 61 7a 43 6f 6c 73 5b 69 5d  ssert( azCols[i]
2850a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  !=0 );.         
2850b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 61 6c   }.          cal
2850c 6c 62 61 63 6b 49 73 49 6e 69 74 20 3d 20 31 3b  lbackIsInit = 1;
2850d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2850e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
2850f 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20  E_ROW ){.       
28510 20 20 20 61 7a 56 61 6c 73 20 3d 20 26 61 7a 43     azVals = &azC
28511 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a 20 20 20 20 20  ols[nCol];.     
28512 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
28513 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
28514 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73 5b 69          azVals[i
28515 5d 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  ] = (char *)sqli
28516 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
28517 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20  pStmt, i);.     
28518 20 20 20 20 20 20 20 69 66 28 20 21 61 7a 56 61         if( !azVa
28519 6c 73 5b 69 5d 20 26 26 20 73 71 6c 69 74 65 33  ls[i] && sqlite3
2851a 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74  _column_type(pSt
2851b 6d 74 2c 20 69 29 21 3d 53 51 4c 49 54 45 5f 4e  mt, i)!=SQLITE_N
2851c 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ULL ){.         
2851d 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
2851e 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
2851f 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78           goto ex
28520 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  ec_out;.        
28521 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
28522 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
28523 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63      if( xCallbac
28524 6b 28 70 41 72 67 2c 20 6e 43 6f 6c 2c 20 61 7a  k(pArg, nCol, az
28525 56 61 6c 73 2c 20 61 7a 43 6f 6c 73 29 20 29 7b  Vals, azCols) ){
28526 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
28527 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20  SQLITE_ABORT;.  
28528 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28529 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
2852a 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 20 20  e *)pStmt);.    
2852b 20 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b        pStmt = 0;
2852c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2852d 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
2852e 54 45 5f 41 42 4f 52 54 2c 20 30 29 3b 0a 20 20  TE_ABORT, 0);.  
2852f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 65          goto exe
28530 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d  c_out;.        }
28531 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
28532 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
28533 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OW ){.        rc
28534 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69   = sqlite3VdbeFi
28535 6e 61 6c 69 7a 65 28 28 56 64 62 65 20 2a 29 70  nalize((Vdbe *)p
28536 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 70  Stmt);.        p
28537 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  Stmt = 0;.      
28538 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
28539 5f 53 43 48 45 4d 41 20 29 7b 0a 20 20 20 20 20  _SCHEMA ){.     
2853a 20 20 20 20 20 6e 52 65 74 72 79 20 3d 20 30 3b       nRetry = 0;
2853b 0a 20 20 20 20 20 20 20 20 20 20 7a 53 71 6c 20  .          zSql 
2853c 3d 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20 20  = zLeftover;.   
2853d 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71         while( sq
2853e 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 53 71  lite3Isspace(zSq
2853f 6c 5b 30 5d 29 20 29 20 7a 53 71 6c 2b 2b 3b 0a  l[0]) ) zSql++;.
28540 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
28541 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
28542 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
28543 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 7a  te3DbFree(db, az
28544 43 6f 6c 73 29 3b 0a 20 20 20 20 61 7a 43 6f 6c  Cols);.    azCol
28545 73 20 3d 20 30 3b 0a 20 20 7d 0a 0a 65 78 65 63  s = 0;.  }..exec
28546 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 53 74 6d  _out:.  if( pStm
28547 74 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 46  t ) sqlite3VdbeF
28548 69 6e 61 6c 69 7a 65 28 28 56 64 62 65 20 2a 29  inalize((Vdbe *)
28549 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65  pStmt);.  sqlite
2854a 33 44 62 46 72 65 65 28 64 62 2c 20 61 7a 43 6f  3DbFree(db, azCo
2854b 6c 73 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c  ls);..  rc = sql
2854c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
2854d 72 63 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  rc);.  if( rc!=S
2854e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 41 4c 57 41  QLITE_OK && ALWA
2854f 59 53 28 72 63 3d 3d 73 71 6c 69 74 65 33 5f 65  YS(rc==sqlite3_e
28550 72 72 63 6f 64 65 28 64 62 29 29 20 26 26 20 70  rrcode(db)) && p
28551 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 69  zErrMsg ){.    i
28552 6e 74 20 6e 45 72 72 4d 73 67 20 3d 20 31 20 2b  nt nErrMsg = 1 +
28553 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
28554 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  (sqlite3_errmsg(
28555 64 62 29 29 3b 0a 20 20 20 20 2a 70 7a 45 72 72  db));.    *pzErr
28556 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Msg = sqlite3Mal
28557 6c 6f 63 28 6e 45 72 72 4d 73 67 29 3b 0a 20 20  loc(nErrMsg);.  
28558 20 20 69 66 28 20 2a 70 7a 45 72 72 4d 73 67 20    if( *pzErrMsg 
28559 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
2855a 2a 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74  *pzErrMsg, sqlit
2855b 65 33 5f 65 72 72 6d 73 67 28 64 62 29 2c 20 6e  e3_errmsg(db), n
2855c 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 65 6c  ErrMsg);.    }el
2855d 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  se{.      rc = S
2855e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
2855f 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
28560 64 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  db, SQLITE_NOMEM
28561 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  , 0);.    }.  }e
28562 6c 73 65 20 69 66 28 20 70 7a 45 72 72 4d 73 67  lse if( pzErrMsg
28563 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73   ){.    *pzErrMs
28564 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 61 73  g = 0;.  }..  as
28565 73 65 72 74 28 20 28 72 63 26 64 62 2d 3e 65 72  sert( (rc&db->er
28566 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a 20 20  rMask)==rc );.  
28567 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
28568 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
28569 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2856a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2856b 45 6e 64 20 6f 66 20 6c 65 67 61 63 79 2e 63 20  End of legacy.c 
2856c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2856d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2856e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2856f 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
28570 42 65 67 69 6e 20 66 69 6c 65 20 6c 6f 61 64 65  Begin file loade
28571 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xt.c ***********
28572 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28573 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
28574 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20  /*.** 2006 June 
28575 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
28576 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
28577 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
28578 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
28579 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2857a 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2857b 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2857c 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2857d 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2857e 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2857f 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
28580 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
28581 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
28582 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
28583 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
28584 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
28585 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
28586 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
28587 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28588 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28589 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2858a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2858b 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
2858c 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64  ntains code used
2858d 20 74 6f 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20   to dynamically 
2858e 6c 6f 61 64 20 65 78 74 65 6e 73 69 6f 6e 73 20  load extensions 
2858f 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 53 51 4c 69  into.** the SQLi
28590 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a  te library..**.*
28591 2a 20 24 49 64 3a 20 6c 6f 61 64 65 78 74 2e 63  * $Id: loadext.c
28592 2c 76 20 31 2e 36 30 20 32 30 30 39 2f 30 36 2f  ,v 1.60 2009/06/
28593 30 33 20 30 31 3a 32 34 3a 35 34 20 64 72 68 20  03 01:24:54 drh 
28594 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
28595 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20  f SQLITE_CORE.  
28596 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
28597 4f 52 45 20 31 20 20 2f 2a 20 44 69 73 61 62 6c  ORE 1  /* Disabl
28598 65 20 74 68 65 20 41 50 49 20 72 65 64 65 66 69  e the API redefi
28599 6e 69 74 69 6f 6e 20 69 6e 20 73 71 6c 69 74 65  nition in sqlite
2859a 33 65 78 74 2e 68 20 2a 2f 0a 23 65 6e 64 69 66  3ext.h */.#endif
2859b 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2859c 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 33   Include sqlite3
2859d 65 78 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ext.h in the mid
2859e 64 6c 65 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63  dle of loadext.c
2859f 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f   **************/
285a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
285a1 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69   Begin file sqli
285a2 74 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  te3ext.h *******
285a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
285a5 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65  ./*.** 2006 June
285a6 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   7.**.** The aut
285a7 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
285a8 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
285a9 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
285aa 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
285ab 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
285ac 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
285ad 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
285ae 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
285af 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
285b0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
285b1 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
285b2 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
285b3 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
285b4 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
285b5 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
285b6 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
285b7 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
285b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285bc 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
285bd 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68   file defines th
285be 65 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  e SQLite interfa
285bf 63 65 20 66 6f 72 20 75 73 65 20 62 79 0a 2a 2a  ce for use by.**
285c0 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 69 65   shared librarie
285c1 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 62  s that want to b
285c2 65 20 69 6d 70 6f 72 74 65 64 20 61 73 20 65 78  e imported as ex
285c3 74 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a  tensions into.**
285c4 20 61 6e 20 53 51 4c 69 74 65 20 69 6e 73 74 61   an SQLite insta
285c5 6e 63 65 2e 20 20 53 68 61 72 65 64 20 6c 69 62  nce.  Shared lib
285c6 72 61 72 69 65 73 20 74 68 61 74 20 69 6e 74 65  raries that inte
285c7 6e 64 20 74 6f 20 62 65 20 6c 6f 61 64 65 64 0a  nd to be loaded.
285c8 2a 2a 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73  ** as extensions
285c9 20 62 79 20 53 51 4c 69 74 65 20 73 68 6f 75 6c   by SQLite shoul
285ca 64 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 20  d #include this 
285cb 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20  file instead of 
285cc 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a  .** sqlite3.h..*
285cd 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 73  *.** @(#) $Id: s
285ce 71 6c 69 74 65 33 65 78 74 2e 68 2c 76 20 31 2e  qlite3ext.h,v 1.
285cf 32 35 20 32 30 30 38 2f 31 30 2f 31 32 20 30 30  25 2008/10/12 00
285d0 3a 32 37 3a 35 34 20 73 68 61 6e 65 20 45 78 70  :27:54 shane Exp
285d1 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53   $.*/.#ifndef _S
285d2 51 4c 49 54 45 33 45 58 54 5f 48 5f 0a 23 64 65  QLITE3EXT_H_.#de
285d3 66 69 6e 65 20 5f 53 51 4c 49 54 45 33 45 58 54  fine _SQLITE3EXT
285d4 5f 48 5f 0a 0a 74 79 70 65 64 65 66 20 73 74 72  _H_..typedef str
285d5 75 63 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  uct sqlite3_api_
285d6 72 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33  routines sqlite3
285d7 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 3b 0a 0a  _api_routines;..
285d8 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
285d9 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f  ing structure ho
285da 6c 64 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  lds pointers to 
285db 61 6c 6c 20 6f 66 20 74 68 65 20 53 51 4c 69 74  all of the SQLit
285dc 65 20 41 50 49 0a 2a 2a 20 72 6f 75 74 69 6e 65  e API.** routine
285dd 73 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47  s..**.** WARNING
285de 3a 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d  :  In order to m
285df 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64  aintain backward
285e0 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c  s compatibility,
285e1 20 61 64 64 20 6e 65 77 0a 2a 2a 20 69 6e 74 65   add new.** inte
285e2 72 66 61 63 65 73 20 74 6f 20 74 68 65 20 65 6e  rfaces to the en
285e3 64 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  d of this struct
285e4 75 72 65 20 6f 6e 6c 79 2e 20 20 49 66 20 79 6f  ure only.  If yo
285e5 75 20 69 6e 73 65 72 74 20 6e 65 77 0a 2a 2a 20  u insert new.** 
285e6 69 6e 74 65 72 66 61 63 65 73 20 69 6e 20 74 68  interfaces in th
285e7 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 69 73  e middle of this
285e8 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e   structure, then
285e9 20 6f 6c 64 65 72 20 64 69 66 66 65 72 65 6e 74   older different
285ea 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  .** versions of 
285eb 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20  SQLite will not 
285ec 62 65 20 61 62 6c 65 20 74 6f 20 6c 6f 61 64 20  be able to load 
285ed 65 61 63 68 20 6f 74 68 65 72 73 27 20 73 68 61  each others' sha
285ee 72 65 64 0a 2a 2a 20 6c 69 62 72 61 72 69 65 73  red.** libraries
285ef 21 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  !.*/.struct sqli
285f0 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
285f1 20 7b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 61 67   {.  void * (*ag
285f2 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 29  gregate_context)
285f3 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
285f4 2a 2c 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a 20  *,int nBytes);. 
285f5 20 69 6e 74 20 20 28 2a 61 67 67 72 65 67 61 74   int  (*aggregat
285f6 65 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33  e_count)(sqlite3
285f7 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e  _context*);.  in
285f8 74 20 20 28 2a 62 69 6e 64 5f 62 6c 6f 62 29 28  t  (*bind_blob)(
285f9 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
285fa 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
285fb 74 20 6e 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  t n,void(*)(void
285fc 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  *));.  int  (*bi
285fd 6e 64 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  nd_double)(sqlit
285fe 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 64 6f 75  e3_stmt*,int,dou
285ff 62 6c 65 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  ble);.  int  (*b
28600 69 6e 64 5f 69 6e 74 29 28 73 71 6c 69 74 65 33  ind_int)(sqlite3
28601 5f 73 74 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b  _stmt*,int,int);
28602 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69  .  int  (*bind_i
28603 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 73 74  nt64)(sqlite3_st
28604 6d 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 5f 69  mt*,int,sqlite_i
28605 6e 74 36 34 29 3b 0a 20 20 69 6e 74 20 20 28 2a  nt64);.  int  (*
28606 62 69 6e 64 5f 6e 75 6c 6c 29 28 73 71 6c 69 74  bind_null)(sqlit
28607 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
28608 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61 72   int  (*bind_par
28609 61 6d 65 74 65 72 5f 63 6f 75 6e 74 29 28 73 71  ameter_count)(sq
2860a 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20  lite3_stmt*);.  
2860b 69 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61 72 61  int  (*bind_para
2860c 6d 65 74 65 72 5f 69 6e 64 65 78 29 28 73 71 6c  meter_index)(sql
2860d 69 74 65 33 5f 73 74 6d 74 2a 2c 63 6f 6e 73 74  ite3_stmt*,const
2860e 20 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a 20 20   char*zName);.  
2860f 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 62  const char * (*b
28610 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
28611 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  me)(sqlite3_stmt
28612 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28  *,int);.  int  (
28613 2a 62 69 6e 64 5f 74 65 78 74 29 28 73 71 6c 69  *bind_text)(sqli
28614 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f  te3_stmt*,int,co
28615 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 20 6e 2c  nst char*,int n,
28616 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
28617 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74  .  int  (*bind_t
28618 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 73  ext16)(sqlite3_s
28619 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  tmt*,int,const v
2861a 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29  oid*,int,void(*)
2861b 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20  (void*));.  int 
2861c 20 28 2a 62 69 6e 64 5f 76 61 6c 75 65 29 28 73   (*bind_value)(s
2861d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2861e 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76  ,const sqlite3_v
2861f 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28  alue*);.  int  (
28620 2a 62 75 73 79 5f 68 61 6e 64 6c 65 72 29 28 73  *busy_handler)(s
28621 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76  qlite3*,int(*)(v
28622 6f 69 64 2a 2c 69 6e 74 29 2c 76 6f 69 64 2a 29  oid*,int),void*)
28623 3b 0a 20 20 69 6e 74 20 20 28 2a 62 75 73 79 5f  ;.  int  (*busy_
28624 74 69 6d 65 6f 75 74 29 28 73 71 6c 69 74 65 33  timeout)(sqlite3
28625 2a 2c 69 6e 74 20 6d 73 29 3b 0a 20 20 69 6e 74  *,int ms);.  int
28626 20 20 28 2a 63 68 61 6e 67 65 73 29 28 73 71 6c    (*changes)(sql
28627 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28  ite3*);.  int  (
28628 2a 63 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 2a  *close)(sqlite3*
28629 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 6c  );.  int  (*coll
2862a 61 74 69 6f 6e 5f 6e 65 65 64 65 64 29 28 73 71  ation_needed)(sq
2862b 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 76 6f 69  lite3*,void*,voi
2862c 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  d(*)(void*,sqlit
2862d 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
2862e 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 3b 0a  ,const char*));.
2862f 20 20 69 6e 74 20 20 28 2a 63 6f 6c 6c 61 74 69    int  (*collati
28630 6f 6e 5f 6e 65 65 64 65 64 31 36 29 28 73 71 6c  on_needed16)(sql
28631 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64  ite3*,void*,void
28632 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
28633 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
28634 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20  const void*));. 
28635 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
28636 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 29 28 73 71 6c  column_blob)(sql
28637 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
28638 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  Col);.  int  (*c
28639 6f 6c 75 6d 6e 5f 62 79 74 65 73 29 28 73 71 6c  olumn_bytes)(sql
2863a 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
2863b 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  Col);.  int  (*c
2863c 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 29 28 73  olumn_bytes16)(s
2863d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2863e 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28   iCol);.  int  (
2863f 2a 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 29 28 73  *column_count)(s
28640 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d  qlite3_stmt*pStm
28641 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t);.  const char
28642 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61 74 61   * (*column_data
28643 62 61 73 65 5f 6e 61 6d 65 29 28 73 71 6c 69 74  base_name)(sqlit
28644 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
28645 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
28646 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
28647 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f  name16)(sqlite3_
28648 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f  stmt*,int);.  co
28649 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  nst char * (*col
2864a 75 6d 6e 5f 64 65 63 6c 74 79 70 65 29 28 73 71  umn_decltype)(sq
2864b 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
2864c 69 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  i);.  const void
2864d 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c   * (*column_decl
2864e 74 79 70 65 31 36 29 28 73 71 6c 69 74 65 33 5f  type16)(sqlite3_
2864f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 64 6f  stmt*,int);.  do
28650 75 62 6c 65 20 20 28 2a 63 6f 6c 75 6d 6e 5f 64  uble  (*column_d
28651 6f 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 73  ouble)(sqlite3_s
28652 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
28653 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f    int  (*column_
28654 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  int)(sqlite3_stm
28655 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
28656 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28 2a  sqlite_int64  (*
28657 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 29 28 73 71  column_int64)(sq
28658 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
28659 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73 74 20 63  iCol);.  const c
2865a 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6e  har * (*column_n
2865b 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ame)(sqlite3_stm
2865c 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
2865d 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
2865e 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33  _name16)(sqlite3
2865f 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63  _stmt*,int);.  c
28660 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f  onst char * (*co
28661 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
28662 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
28663 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  int);.  const vo
28664 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72  id * (*column_or
28665 69 67 69 6e 5f 6e 61 6d 65 31 36 29 28 73 71 6c  igin_name16)(sql
28666 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
28667 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
28668 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  (*column_table_n
28669 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ame)(sqlite3_stm
2866a 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
2866b 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
2866c 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 29 28 73  _table_name16)(s
2866d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2866e 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  );.  const unsig
2866f 6e 65 64 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  ned char * (*col
28670 75 6d 6e 5f 74 65 78 74 29 28 73 71 6c 69 74 65  umn_text)(sqlite
28671 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
28672 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
28673 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  * (*column_text1
28674 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  6)(sqlite3_stmt*
28675 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e  ,int iCol);.  in
28676 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 74 79 70 65  t  (*column_type
28677 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
28678 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 73 71 6c  int iCol);.  sql
28679 69 74 65 33 5f 76 61 6c 75 65 2a 20 28 2a 63 6f  ite3_value* (*co
2867a 6c 75 6d 6e 5f 76 61 6c 75 65 29 28 73 71 6c 69  lumn_value)(sqli
2867b 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
2867c 6f 6c 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  ol);.  void * (*
2867d 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 29 28 73 71 6c  commit_hook)(sql
2867e 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69  ite3*,int(*)(voi
2867f 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e  d*),void*);.  in
28680 74 20 20 28 2a 63 6f 6d 70 6c 65 74 65 29 28 63  t  (*complete)(c
28681 6f 6e 73 74 20 63 68 61 72 2a 73 71 6c 29 3b 0a  onst char*sql);.
28682 20 20 69 6e 74 20 20 28 2a 63 6f 6d 70 6c 65 74    int  (*complet
28683 65 31 36 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  e16)(const void*
28684 73 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  sql);.  int  (*c
28685 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 29  reate_collation)
28686 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
28687 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c  char*,int,void*,
28688 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
28689 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
2868a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a  ,const void*));.
2868b 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f    int  (*create_
2868c 63 6f 6c 6c 61 74 69 6f 6e 31 36 29 28 73 71 6c  collation16)(sql
2868d 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64  ite3*,const void
2868e 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28  *,int,void*,int(
2868f 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  *)(void*,int,con
28690 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
28691 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e  st void*));.  in
28692 74 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63  t  (*create_func
28693 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63  tion)(sqlite3*,c
28694 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69  onst char*,int,i
28695 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a  nt,void*,void (*
28696 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
28697 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
28698 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69  te3_value**),voi
28699 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
2869a 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
2869b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
2869c 2c 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28  ,void (*xFinal)(
2869d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2869e 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65  ));.  int  (*cre
2869f 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 29 28  ate_function16)(
286a0 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76  sqlite3*,const v
286a1 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69  oid*,int,int,voi
286a2 64 2a 2c 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  d*,void (*xFunc)
286a3 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
286a4 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
286a5 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53  lue**),void (*xS
286a6 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
286a7 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
286a8 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20  3_value**),void 
286a9 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65  (*xFinal)(sqlite
286aa 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20  3_context*));.  
286ab 69 6e 74 20 28 2a 63 72 65 61 74 65 5f 6d 6f 64  int (*create_mod
286ac 75 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  ule)(sqlite3*,co
286ad 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
286ae 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c  sqlite3_module*,
286af 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28  void*);.  int  (
286b0 2a 64 61 74 61 5f 63 6f 75 6e 74 29 28 73 71 6c  *data_count)(sql
286b1 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29  ite3_stmt*pStmt)
286b2 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 20 28 2a  ;.  sqlite3 * (*
286b3 64 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74  db_handle)(sqlit
286b4 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74  e3_stmt*);.  int
286b5 20 28 2a 64 65 63 6c 61 72 65 5f 76 74 61 62 29   (*declare_vtab)
286b6 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
286b7 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 20 28  char*);.  int  (
286b8 2a 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  *enable_shared_c
286b9 61 63 68 65 29 28 69 6e 74 29 3b 0a 20 20 69 6e  ache)(int);.  in
286ba 74 20 20 28 2a 65 72 72 63 6f 64 65 29 28 73 71  t  (*errcode)(sq
286bb 6c 69 74 65 33 2a 64 62 29 3b 0a 20 20 63 6f 6e  lite3*db);.  con
286bc 73 74 20 63 68 61 72 20 2a 20 28 2a 65 72 72 6d  st char * (*errm
286bd 73 67 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  sg)(sqlite3*);. 
286be 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
286bf 65 72 72 6d 73 67 31 36 29 28 73 71 6c 69 74 65  errmsg16)(sqlite
286c0 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 78  3*);.  int  (*ex
286c1 65 63 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  ec)(sqlite3*,con
286c2 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33  st char*,sqlite3
286c3 5f 63 61 6c 6c 62 61 63 6b 2c 76 6f 69 64 2a 2c  _callback,void*,
286c4 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20  char**);.  int  
286c5 28 2a 65 78 70 69 72 65 64 29 28 73 71 6c 69 74  (*expired)(sqlit
286c6 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74  e3_stmt*);.  int
286c7 20 20 28 2a 66 69 6e 61 6c 69 7a 65 29 28 73 71    (*finalize)(sq
286c8 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74  lite3_stmt*pStmt
286c9 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65  );.  void  (*fre
286ca 65 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  e)(void*);.  voi
286cb 64 20 20 28 2a 66 72 65 65 5f 74 61 62 6c 65 29  d  (*free_table)
286cc 28 63 68 61 72 2a 2a 72 65 73 75 6c 74 29 3b 0a  (char**result);.
286cd 20 20 69 6e 74 20 20 28 2a 67 65 74 5f 61 75 74    int  (*get_aut
286ce 6f 63 6f 6d 6d 69 74 29 28 73 71 6c 69 74 65 33  ocommit)(sqlite3
286cf 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 67  *);.  void * (*g
286d0 65 74 5f 61 75 78 64 61 74 61 29 28 73 71 6c 69  et_auxdata)(sqli
286d1 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
286d2 29 3b 0a 20 20 69 6e 74 20 20 28 2a 67 65 74 5f  );.  int  (*get_
286d3 74 61 62 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c  table)(sqlite3*,
286d4 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68 61 72  const char*,char
286d5 2a 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 63 68  ***,int*,int*,ch
286d6 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ar**);.  int  (*
286d7 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 29 28  global_recover)(
286d8 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 20 28  void);.  void  (
286d9 2a 69 6e 74 65 72 72 75 70 74 78 29 28 73 71 6c  *interruptx)(sql
286da 69 74 65 33 2a 29 3b 0a 20 20 73 71 6c 69 74 65  ite3*);.  sqlite
286db 5f 69 6e 74 36 34 20 20 28 2a 6c 61 73 74 5f 69  _int64  (*last_i
286dc 6e 73 65 72 74 5f 72 6f 77 69 64 29 28 73 71 6c  nsert_rowid)(sql
286dd 69 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  ite3*);.  const 
286de 63 68 61 72 20 2a 20 28 2a 6c 69 62 76 65 72 73  char * (*libvers
286df 69 6f 6e 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e  ion)(void);.  in
286e0 74 20 20 28 2a 6c 69 62 76 65 72 73 69 6f 6e 5f  t  (*libversion_
286e1 6e 75 6d 62 65 72 29 28 76 6f 69 64 29 3b 0a 20  number)(void);. 
286e2 20 76 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 29   void *(*malloc)
286e3 28 69 6e 74 29 3b 0a 20 20 63 68 61 72 20 2a 20  (int);.  char * 
286e4 28 2a 6d 70 72 69 6e 74 66 29 28 63 6f 6e 73 74  (*mprintf)(const
286e5 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 20 20 69   char*,...);.  i
286e6 6e 74 20 20 28 2a 6f 70 65 6e 29 28 63 6f 6e 73  nt  (*open)(cons
286e7 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a  t char*,sqlite3*
286e8 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65  *);.  int  (*ope
286e9 6e 31 36 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  n16)(const void*
286ea 2c 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69  ,sqlite3**);.  i
286eb 6e 74 20 20 28 2a 70 72 65 70 61 72 65 29 28 73  nt  (*prepare)(s
286ec 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
286ed 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  ar*,int,sqlite3_
286ee 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63 68 61  stmt**,const cha
286ef 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70  r**);.  int  (*p
286f0 72 65 70 61 72 65 31 36 29 28 73 71 6c 69 74 65  repare16)(sqlite
286f1 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  3*,const void*,i
286f2 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt,sqlite3_stmt*
286f3 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2a 29 3b  *,const void**);
286f4 0a 20 20 76 6f 69 64 20 2a 20 28 2a 70 72 6f 66  .  void * (*prof
286f5 69 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  ile)(sqlite3*,vo
286f6 69 64 28 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  id(*)(void*,cons
286f7 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75  t char*,sqlite_u
286f8 69 6e 74 36 34 29 2c 76 6f 69 64 2a 29 3b 0a 20  int64),void*);. 
286f9 20 76 6f 69 64 20 20 28 2a 70 72 6f 67 72 65 73   void  (*progres
286fa 73 5f 68 61 6e 64 6c 65 72 29 28 73 71 6c 69 74  s_handler)(sqlit
286fb 65 33 2a 2c 69 6e 74 2c 69 6e 74 28 2a 29 28 76  e3*,int,int(*)(v
286fc 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  oid*),void*);.  
286fd 76 6f 69 64 20 2a 28 2a 72 65 61 6c 6c 6f 63 29  void *(*realloc)
286fe 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 69  (void*,int);.  i
286ff 6e 74 20 20 28 2a 72 65 73 65 74 29 28 73 71 6c  nt  (*reset)(sql
28700 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29  ite3_stmt*pStmt)
28701 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
28702 6c 74 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33  lt_blob)(sqlite3
28703 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20  _context*,const 
28704 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a  void*,int,void(*
28705 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69  )(void*));.  voi
28706 64 20 20 28 2a 72 65 73 75 6c 74 5f 64 6f 75 62  d  (*result_doub
28707 6c 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  le)(sqlite3_cont
28708 65 78 74 2a 2c 64 6f 75 62 6c 65 29 3b 0a 20 20  ext*,double);.  
28709 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 65  void  (*result_e
2870a 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 63 6f  rror)(sqlite3_co
2870b 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61  ntext*,const cha
2870c 72 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20  r*,int);.  void 
2870d 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 31   (*result_error1
2870e 36 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  6)(sqlite3_conte
2870f 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  xt*,const void*,
28710 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  int);.  void  (*
28711 72 65 73 75 6c 74 5f 69 6e 74 29 28 73 71 6c 69  result_int)(sqli
28712 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
28713 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
28714 75 6c 74 5f 69 6e 74 36 34 29 28 73 71 6c 69 74  ult_int64)(sqlit
28715 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69  e3_context*,sqli
28716 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69  te_int64);.  voi
28717 64 20 20 28 2a 72 65 73 75 6c 74 5f 6e 75 6c 6c  d  (*result_null
28718 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
28719 74 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  t*);.  void  (*r
2871a 65 73 75 6c 74 5f 74 65 78 74 29 28 73 71 6c 69  esult_text)(sqli
2871b 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e  te3_context*,con
2871c 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69  st char*,int,voi
2871d 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
2871e 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74  void  (*result_t
2871f 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 63  ext16)(sqlite3_c
28720 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f  ontext*,const vo
28721 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28  id*,int,void(*)(
28722 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20  void*));.  void 
28723 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 31 36   (*result_text16
28724 62 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  be)(sqlite3_cont
28725 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ext*,const void*
28726 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69  ,int,void(*)(voi
28727 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  d*));.  void  (*
28728 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 29  result_text16le)
28729 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2872a 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
2872b 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
2872c 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
2872d 75 6c 74 5f 76 61 6c 75 65 29 28 73 71 6c 69 74  ult_value)(sqlit
2872e 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69  e3_context*,sqli
2872f 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 76  te3_value*);.  v
28730 6f 69 64 20 2a 20 28 2a 72 6f 6c 6c 62 61 63 6b  oid * (*rollback
28731 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c  _hook)(sqlite3*,
28732 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 2c 76  void(*)(void*),v
28733 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  oid*);.  int  (*
28734 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 29 28  set_authorizer)(
28735 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28  sqlite3*,int(*)(
28736 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
28737 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
28738 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
28739 6e 73 74 20 63 68 61 72 2a 29 2c 76 6f 69 64 2a  nst char*),void*
2873a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 73 65 74  );.  void  (*set
2873b 5f 61 75 78 64 61 74 61 29 28 73 71 6c 69 74 65  _auxdata)(sqlite
2873c 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 76  3_context*,int,v
2873d 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 29 28 76 6f  oid*,void (*)(vo
2873e 69 64 2a 29 29 3b 0a 20 20 63 68 61 72 20 2a 20  id*));.  char * 
2873f 28 2a 73 6e 70 72 69 6e 74 66 29 28 69 6e 74 2c  (*snprintf)(int,
28740 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
28741 2a 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28  *,...);.  int  (
28742 2a 73 74 65 70 29 28 73 71 6c 69 74 65 33 5f 73  *step)(sqlite3_s
28743 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  tmt*);.  int  (*
28744 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
28745 61 64 61 74 61 29 28 73 71 6c 69 74 65 33 2a 2c  adata)(sqlite3*,
28746 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
28747 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
28748 61 72 2a 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2a  ar*,char const**
28749 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2a 2c 69 6e  ,char const**,in
2874a 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 20  t*,int*,int*);. 
2874b 20 76 6f 69 64 20 20 28 2a 74 68 72 65 61 64 5f   void  (*thread_
2874c 63 6c 65 61 6e 75 70 29 28 76 6f 69 64 29 3b 0a  cleanup)(void);.
2874d 20 20 69 6e 74 20 20 28 2a 74 6f 74 61 6c 5f 63    int  (*total_c
2874e 68 61 6e 67 65 73 29 28 73 71 6c 69 74 65 33 2a  hanges)(sqlite3*
2874f 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 74 72  );.  void * (*tr
28750 61 63 65 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  ace)(sqlite3*,vo
28751 69 64 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64  id(*xTrace)(void
28752 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 76  *,const char*),v
28753 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  oid*);.  int  (*
28754 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67  transfer_binding
28755 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  s)(sqlite3_stmt*
28756 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  ,sqlite3_stmt*);
28757 0a 20 20 76 6f 69 64 20 2a 20 28 2a 75 70 64 61  .  void * (*upda
28758 74 65 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33  te_hook)(sqlite3
28759 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c  *,void(*)(void*,
2875a 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 2a  int ,char const*
2875b 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2c 73 71 6c  ,char const*,sql
2875c 69 74 65 5f 69 6e 74 36 34 29 2c 76 6f 69 64 2a  ite_int64),void*
2875d 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 75 73  );.  void * (*us
2875e 65 72 5f 64 61 74 61 29 28 73 71 6c 69 74 65 33  er_data)(sqlite3
2875f 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 63 6f  _context*);.  co
28760 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c  nst void * (*val
28761 75 65 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33  ue_blob)(sqlite3
28762 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
28763 20 28 2a 76 61 6c 75 65 5f 62 79 74 65 73 29 28   (*value_bytes)(
28764 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
28765 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f  .  int  (*value_
28766 62 79 74 65 73 31 36 29 28 73 71 6c 69 74 65 33  bytes16)(sqlite3
28767 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 64 6f 75 62  _value*);.  doub
28768 6c 65 20 20 28 2a 76 61 6c 75 65 5f 64 6f 75 62  le  (*value_doub
28769 6c 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  le)(sqlite3_valu
2876a 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61  e*);.  int  (*va
2876b 6c 75 65 5f 69 6e 74 29 28 73 71 6c 69 74 65 33  lue_int)(sqlite3
2876c 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 73 71 6c 69  _value*);.  sqli
2876d 74 65 5f 69 6e 74 36 34 20 20 28 2a 76 61 6c 75  te_int64  (*valu
2876e 65 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33  e_int64)(sqlite3
2876f 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
28770 20 28 2a 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63   (*value_numeric
28771 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 76  _type)(sqlite3_v
28772 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  alue*);.  const 
28773 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 20  unsigned char * 
28774 28 2a 76 61 6c 75 65 5f 74 65 78 74 29 28 73 71  (*value_text)(sq
28775 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
28776 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
28777 76 61 6c 75 65 5f 74 65 78 74 31 36 29 28 73 71  value_text16)(sq
28778 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
28779 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
2877a 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 29 28  value_text16be)(
2877b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2877c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
2877d 28 2a 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  (*value_text16le
2877e 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
2877f 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75  );.  int  (*valu
28780 65 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f  e_type)(sqlite3_
28781 76 61 6c 75 65 2a 29 3b 0a 20 20 63 68 61 72 20  value*);.  char 
28782 2a 28 2a 76 6d 70 72 69 6e 74 66 29 28 63 6f 6e  *(*vmprintf)(con
28783 73 74 20 63 68 61 72 2a 2c 76 61 5f 6c 69 73 74  st char*,va_list
28784 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 3f 3f  );.  /* Added ??
28785 3f 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 6f 76 65  ? */.  int (*ove
28786 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 29 28  rload_function)(
28787 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
28788 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c  char *zFuncName,
28789 20 69 6e 74 20 6e 41 72 67 29 3b 0a 20 20 2f 2a   int nArg);.  /*
2878a 20 41 64 64 65 64 20 62 79 20 33 2e 33 2e 31 33   Added by 3.3.13
2878b 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 70 72 65 70   */.  int (*prep
2878c 61 72 65 5f 76 32 29 28 73 71 6c 69 74 65 33 2a  are_v2)(sqlite3*
2878d 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
2878e 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c  ,sqlite3_stmt**,
2878f 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20  const char**);. 
28790 20 69 6e 74 20 28 2a 70 72 65 70 61 72 65 31 36   int (*prepare16
28791 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  _v2)(sqlite3*,co
28792 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71  nst void*,int,sq
28793 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e  lite3_stmt**,con
28794 73 74 20 76 6f 69 64 2a 2a 29 3b 0a 20 20 69 6e  st void**);.  in
28795 74 20 28 2a 63 6c 65 61 72 5f 62 69 6e 64 69 6e  t (*clear_bindin
28796 67 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  gs)(sqlite3_stmt
28797 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 62  *);.  /* Added b
28798 79 20 33 2e 34 2e 31 20 2a 2f 0a 20 20 69 6e 74  y 3.4.1 */.  int
28799 20 28 2a 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65   (*create_module
2879a 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  _v2)(sqlite3*,co
2879b 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
2879c 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c  sqlite3_module*,
2879d 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 78 44 65  void*,void (*xDe
2879e 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 29 3b  stroy)(void *));
2879f 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 33  .  /* Added by 3
287a0 2e 35 2e 30 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  .5.0 */.  int (*
287a1 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 29 28 73  bind_zeroblob)(s
287a2 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
287a3 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 62  ,int);.  int (*b
287a4 6c 6f 62 5f 62 79 74 65 73 29 28 73 71 6c 69 74  lob_bytes)(sqlit
287a5 65 33 5f 62 6c 6f 62 2a 29 3b 0a 20 20 69 6e 74  e3_blob*);.  int
287a6 20 28 2a 62 6c 6f 62 5f 63 6c 6f 73 65 29 28 73   (*blob_close)(s
287a7 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 29 3b 0a 20  qlite3_blob*);. 
287a8 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 6f 70 65 6e   int (*blob_open
287a9 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
287aa 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
287ab 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  r*,const char*,s
287ac 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
287ad 2c 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2a 29  ,sqlite3_blob**)
287ae 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 72  ;.  int (*blob_r
287af 65 61 64 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f  ead)(sqlite3_blo
287b0 62 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  b*,void*,int,int
287b1 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f  );.  int (*blob_
287b2 77 72 69 74 65 29 28 73 71 6c 69 74 65 33 5f 62  write)(sqlite3_b
287b3 6c 6f 62 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  lob*,const void*
287b4 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  ,int,int);.  int
287b5 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74   (*create_collat
287b6 69 6f 6e 5f 76 32 29 28 73 71 6c 69 74 65 33 2a  ion_v2)(sqlite3*
287b7 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
287b8 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f  ,void*,int(*)(vo
287b9 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
287ba 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
287bb 69 64 2a 29 2c 76 6f 69 64 28 2a 29 28 76 6f 69  id*),void(*)(voi
287bc 64 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 66 69  d*));.  int (*fi
287bd 6c 65 5f 63 6f 6e 74 72 6f 6c 29 28 73 71 6c 69  le_control)(sqli
287be 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
287bf 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73  ,int,void*);.  s
287c0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 28 2a 6d  qlite3_int64 (*m
287c1 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 29  emory_highwater)
287c2 28 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  (int);.  sqlite3
287c3 5f 69 6e 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f  _int64 (*memory_
287c4 75 73 65 64 29 28 76 6f 69 64 29 3b 0a 20 20 73  used)(void);.  s
287c5 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a  qlite3_mutex *(*
287c6 6d 75 74 65 78 5f 61 6c 6c 6f 63 29 28 69 6e 74  mutex_alloc)(int
287c7 29 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65  );.  void (*mute
287c8 78 5f 65 6e 74 65 72 29 28 73 71 6c 69 74 65 33  x_enter)(sqlite3
287c9 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64  _mutex*);.  void
287ca 20 28 2a 6d 75 74 65 78 5f 66 72 65 65 29 28 73   (*mutex_free)(s
287cb 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
287cc 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f 6c    void (*mutex_l
287cd 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  eave)(sqlite3_mu
287ce 74 65 78 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 6d  tex*);.  int (*m
287cf 75 74 65 78 5f 74 72 79 29 28 73 71 6c 69 74 65  utex_try)(sqlite
287d0 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 69 6e 74  3_mutex*);.  int
287d1 20 28 2a 6f 70 65 6e 5f 76 32 29 28 63 6f 6e 73   (*open_v2)(cons
287d2 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a  t char*,sqlite3*
287d3 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
287d4 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 72 65 6c 65  *);.  int (*rele
287d5 61 73 65 5f 6d 65 6d 6f 72 79 29 28 69 6e 74 29  ase_memory)(int)
287d6 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c  ;.  void (*resul
287d7 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 29 28 73  t_error_nomem)(s
287d8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
287d9 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c  ;.  void (*resul
287da 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 29 28  t_error_toobig)(
287db 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
287dc 29 3b 0a 20 20 69 6e 74 20 28 2a 73 6c 65 65 70  );.  int (*sleep
287dd 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28  )(int);.  void (
287de 2a 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  *soft_heap_limit
287df 29 28 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65  )(int);.  sqlite
287e0 33 5f 76 66 73 20 2a 28 2a 76 66 73 5f 66 69 6e  3_vfs *(*vfs_fin
287e1 64 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  d)(const char*);
287e2 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 72 65 67  .  int (*vfs_reg
287e3 69 73 74 65 72 29 28 73 71 6c 69 74 65 33 5f 76  ister)(sqlite3_v
287e4 66 73 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  fs*,int);.  int 
287e5 28 2a 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  (*vfs_unregister
287e6 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 29 3b  )(sqlite3_vfs*);
287e7 0a 20 20 69 6e 74 20 28 2a 78 74 68 72 65 61 64  .  int (*xthread
287e8 73 61 66 65 29 28 76 6f 69 64 29 3b 0a 20 20 76  safe)(void);.  v
287e9 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 7a 65 72  oid (*result_zer
287ea 6f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 63  oblob)(sqlite3_c
287eb 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20  ontext*,int);.  
287ec 76 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 65 72  void (*result_er
287ed 72 6f 72 5f 63 6f 64 65 29 28 73 71 6c 69 74 65  ror_code)(sqlite
287ee 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b  3_context*,int);
287ef 0a 20 20 69 6e 74 20 28 2a 74 65 73 74 5f 63 6f  .  int (*test_co
287f0 6e 74 72 6f 6c 29 28 69 6e 74 2c 20 2e 2e 2e 29  ntrol)(int, ...)
287f1 3b 0a 20 20 76 6f 69 64 20 28 2a 72 61 6e 64 6f  ;.  void (*rando
287f2 6d 6e 65 73 73 29 28 69 6e 74 2c 76 6f 69 64 2a  mness)(int,void*
287f3 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 28 2a  );.  sqlite3 *(*
287f4 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
287f5 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e)(sqlite3_conte
287f6 78 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 65 78  xt*);.  int (*ex
287f7 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f  tended_result_co
287f8 64 65 73 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e  des)(sqlite3*,in
287f9 74 29 3b 0a 20 20 69 6e 74 20 28 2a 6c 69 6d 69  t);.  int (*limi
287fa 74 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c  t)(sqlite3*,int,
287fb 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  int);.  sqlite3_
287fc 73 74 6d 74 20 2a 28 2a 6e 65 78 74 5f 73 74 6d  stmt *(*next_stm
287fd 74 29 28 73 71 6c 69 74 65 33 2a 2c 73 71 6c 69  t)(sqlite3*,sqli
287fe 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 63 6f  te3_stmt*);.  co
287ff 6e 73 74 20 63 68 61 72 20 2a 28 2a 73 71 6c 29  nst char *(*sql)
28800 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
28801 0a 20 20 69 6e 74 20 28 2a 73 74 61 74 75 73 29  .  int (*status)
28802 28 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69  (int,int*,int*,i
28803 6e 74 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  nt);.};../*.** T
28804 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
28805 72 6f 73 20 72 65 64 65 66 69 6e 65 20 74 68 65  ros redefine the
28806 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 73 6f   API routines so
28807 20 74 68 61 74 20 74 68 65 79 20 61 72 65 0a 2a   that they are.*
28808 2a 20 72 65 64 69 72 65 63 74 65 64 20 74 68 72  * redirected thr
28809 6f 75 67 68 74 20 74 68 65 20 67 6c 6f 62 61 6c  ought the global
2880a 20 73 71 6c 69 74 65 33 5f 61 70 69 20 73 74 72   sqlite3_api str
2880b 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
2880c 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 69  is header file i
2880d 73 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20 74  s also used by t
2880e 68 65 20 6c 6f 61 64 65 78 74 2e 63 20 73 6f 75  he loadext.c sou
2880f 72 63 65 20 66 69 6c 65 0a 2a 2a 20 28 70 61 72  rce file.** (par
28810 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 53 51  t of the main SQ
28811 4c 69 74 65 20 6c 69 62 72 61 72 79 20 2d 20 6e  Lite library - n
28812 6f 74 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 29  ot an extension)
28813 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20 63   so that.** it c
28814 61 6e 20 67 65 74 20 61 63 63 65 73 73 20 74 6f  an get access to
28815 20 74 68 65 20 73 71 6c 69 74 65 33 5f 61 70 69   the sqlite3_api
28816 5f 72 6f 75 74 69 6e 65 73 20 73 74 72 75 63 74  _routines struct
28817 75 72 65 0a 2a 2a 20 64 65 66 69 6e 69 74 69 6f  ure.** definitio
28818 6e 2e 20 20 42 75 74 20 74 68 65 20 6d 61 69 6e  n.  But the main
28819 20 6c 69 62 72 61 72 79 20 64 6f 65 73 20 6e 6f   library does no
2881a 74 20 77 61 6e 74 20 74 6f 20 72 65 64 65 66 69  t want to redefi
2881b 6e 65 0a 2a 2a 20 74 68 65 20 41 50 49 2e 20 20  ne.** the API.  
2881c 53 6f 20 74 68 65 20 72 65 64 65 66 69 6e 69 74  So the redefinit
2881d 69 6f 6e 20 6d 61 63 72 6f 73 20 61 72 65 20 6f  ion macros are o
2881e 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 74 68 65  nly valid if the
2881f 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  .** SQLITE_CORE 
28820 6d 61 63 72 6f 73 20 69 73 20 75 6e 64 65 66 69  macros is undefi
28821 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ned..*/.#ifndef 
28822 53 51 4c 49 54 45 5f 43 4f 52 45 0a 23 64 65 66  SQLITE_CORE.#def
28823 69 6e 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72  ine sqlite3_aggr
28824 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 20 20 20  egate_context   
28825 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28826 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
28827 74 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  t.#ifndef SQLITE
28828 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
28829 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2882a 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74  _aggregate_count
2882b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2882c 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65 5f 63  api->aggregate_c
2882d 6f 75 6e 74 0a 23 65 6e 64 69 66 0a 23 64 65 66  ount.#endif.#def
2882e 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
2882f 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20  _blob           
28830 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28831 62 69 6e 64 5f 62 6c 6f 62 0a 23 64 65 66 69 6e  bind_blob.#defin
28832 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64  e sqlite3_bind_d
28833 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  ouble           
28834 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
28835 6e 64 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e  nd_double.#defin
28836 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  e sqlite3_bind_i
28837 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
28838 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
28839 6e 64 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73  nd_int.#define s
2883a 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
2883b 34 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  4             sq
2883c 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f  lite3_api->bind_
2883d 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71  int64.#define sq
2883e 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 20  lite3_bind_null 
2883f 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28840 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 6e  ite3_api->bind_n
28841 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ull.#define sqli
28842 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
28843 65 72 5f 63 6f 75 6e 74 20 20 20 73 71 6c 69 74  er_count   sqlit
28844 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72  e3_api->bind_par
28845 61 6d 65 74 65 72 5f 63 6f 75 6e 74 0a 23 64 65  ameter_count.#de
28846 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
28847 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
28848 78 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  x   sqlite3_api-
28849 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  >bind_parameter_
2884a 69 6e 64 65 78 0a 23 64 65 66 69 6e 65 20 73 71  index.#define sq
2884b 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
2884c 65 74 65 72 5f 6e 61 6d 65 20 20 20 20 73 71 6c  eter_name    sql
2884d 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70  ite3_api->bind_p
2884e 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 0a 23 64  arameter_name.#d
2884f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
28850 6e 64 5f 74 65 78 74 20 20 20 20 20 20 20 20 20  nd_text         
28851 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28852 2d 3e 62 69 6e 64 5f 74 65 78 74 0a 23 64 65 66  ->bind_text.#def
28853 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
28854 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20  _text16         
28855 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28856 62 69 6e 64 5f 74 65 78 74 31 36 0a 23 64 65 66  bind_text16.#def
28857 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
28858 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20  _value          
28859 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2885a 62 69 6e 64 5f 76 61 6c 75 65 0a 23 64 65 66 69  bind_value.#defi
2885b 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  ne sqlite3_busy_
2885c 68 61 6e 64 6c 65 72 20 20 20 20 20 20 20 20 20  handler         
2885d 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
2885e 75 73 79 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66  usy_handler.#def
2885f 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79  ine sqlite3_busy
28860 5f 74 69 6d 65 6f 75 74 20 20 20 20 20 20 20 20  _timeout        
28861 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28862 62 75 73 79 5f 74 69 6d 65 6f 75 74 0a 23 64 65  busy_timeout.#de
28863 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 68 61  fine sqlite3_cha
28864 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 20 20  nges            
28865 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28866 3e 63 68 61 6e 67 65 73 0a 23 64 65 66 69 6e 65  >changes.#define
28867 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 20 20   sqlite3_close  
28868 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28869 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 6f  sqlite3_api->clo
2886a 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  se.#define sqlit
2886b 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
2886c 64 65 64 20 20 20 20 20 20 20 73 71 6c 69 74 65  ded       sqlite
2886d 33 5f 61 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e  3_api->collation
2886e 5f 6e 65 65 64 65 64 0a 23 64 65 66 69 6e 65 20  _needed.#define 
2886f 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
28870 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20 73  n_needed16     s
28871 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 6c  qlite3_api->coll
28872 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 0a 23  ation_needed16.#
28873 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28874 6f 6c 75 6d 6e 5f 62 6c 6f 62 20 20 20 20 20 20  olumn_blob      
28875 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28876 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 0a 23  i->column_blob.#
28877 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28878 6f 6c 75 6d 6e 5f 62 79 74 65 73 20 20 20 20 20  olumn_bytes     
28879 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2887a 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 0a  i->column_bytes.
2887b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2887c 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 20 20  column_bytes16  
2887d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2887e 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  pi->column_bytes
2887f 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
28880 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 20  e3_column_count 
28881 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28882 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 63 6f  3_api->column_co
28883 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  unt.#define sqli
28884 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
28885 61 73 65 5f 6e 61 6d 65 20 20 20 73 71 6c 69 74  ase_name   sqlit
28886 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
28887 61 74 61 62 61 73 65 5f 6e 61 6d 65 0a 23 64 65  atabase_name.#de
28888 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28889 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
2888a 65 31 36 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  e16 sqlite3_api-
2888b 3e 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  >column_database
2888c 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20  _name16.#define 
2888d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
2888e 65 63 6c 74 79 70 65 20 20 20 20 20 20 20 20 73  ecltype        s
2888f 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
28890 6d 6e 5f 64 65 63 6c 74 79 70 65 0a 23 64 65 66  mn_decltype.#def
28891 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28892 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 20 20 20  mn_decltype16   
28893 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28894 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31  column_decltype1
28895 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
28896 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20  3_column_double 
28897 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28898 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 6f 75  _api->column_dou
28899 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ble.#define sqli
2889a 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 20 20  te3_column_int  
2889b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2889c 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69  e3_api->column_i
2889d 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  nt.#define sqlit
2889e 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 20  e3_column_int64 
2889f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
288a0 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e  3_api->column_in
288a1 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t64.#define sqli
288a2 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 20  te3_column_name 
288a3 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
288a4 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e  e3_api->column_n
288a5 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
288a6 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
288a7 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  6          sqlit
288a8 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e  e3_api->column_n
288a9 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ame16.#define sq
288aa 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
288ab 67 69 6e 5f 6e 61 6d 65 20 20 20 20 20 73 71 6c  gin_name     sql
288ac 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
288ad 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 0a 23 64 65  _origin_name.#de
288ae 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
288af 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
288b0 36 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  6   sqlite3_api-
288b1 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  >column_origin_n
288b2 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ame16.#define sq
288b3 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
288b4 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20 73 71 6c  le_name      sql
288b5 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
288b6 5f 74 61 62 6c 65 5f 6e 61 6d 65 0a 23 64 65 66  _table_name.#def
288b7 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
288b8 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20  mn_table_name16 
288b9 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
288ba 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
288bb 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  e16.#define sqli
288bc 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 20  te3_column_text 
288bd 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
288be 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
288bf 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ext.#define sqli
288c0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
288c1 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  6          sqlit
288c2 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
288c3 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ext16.#define sq
288c4 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
288c5 65 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  e            sql
288c6 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
288c7 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71  _type.#define sq
288c8 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
288c9 75 65 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  ue           sql
288ca 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
288cb 5f 76 61 6c 75 65 0a 23 64 65 66 69 6e 65 20 73  _value.#define s
288cc 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
288cd 6f 6b 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ok            sq
288ce 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 6d 69  lite3_api->commi
288cf 74 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73  t_hook.#define s
288d0 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20  qlite3_complete 
288d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
288d2 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c  lite3_api->compl
288d3 65 74 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ete.#define sqli
288d4 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 20  te3_complete16  
288d5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
288d6 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c 65 74 65  e3_api->complete
288d7 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
288d8 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
288d9 69 6f 6e 20 20 20 20 20 20 20 73 71 6c 69 74 65  ion       sqlite
288da 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f  3_api->create_co
288db 6c 6c 61 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20  llation.#define 
288dc 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
288dd 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20 20 73  ollation16     s
288de 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61  qlite3_api->crea
288df 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 0a 23  te_collation16.#
288e0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
288e1 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 20  reate_function  
288e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
288e3 69 2d 3e 63 72 65 61 74 65 5f 66 75 6e 63 74 69  i->create_functi
288e4 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  on.#define sqlit
288e5 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
288e6 6f 6e 31 36 20 20 20 20 20 20 73 71 6c 69 74 65  on16      sqlite
288e7 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 66 75  3_api->create_fu
288e8 6e 63 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65  nction16.#define
288e9 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
288ea 6d 6f 64 75 6c 65 20 20 20 20 20 20 20 20 20 20  module          
288eb 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65  sqlite3_api->cre
288ec 61 74 65 5f 6d 6f 64 75 6c 65 0a 23 64 65 66 69  ate_module.#defi
288ed 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
288ee 65 5f 6d 6f 64 75 6c 65 5f 76 32 20 20 20 20 20  e_module_v2     
288ef 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
288f0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 0a  reate_module_v2.
288f1 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
288f2 64 61 74 61 5f 63 6f 75 6e 74 20 20 20 20 20 20  data_count      
288f3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
288f4 70 69 2d 3e 64 61 74 61 5f 63 6f 75 6e 74 0a 23  pi->data_count.#
288f5 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64  define sqlite3_d
288f6 62 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20  b_handle        
288f7 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
288f8 69 2d 3e 64 62 5f 68 61 6e 64 6c 65 0a 23 64 65  i->db_handle.#de
288f9 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65 63  fine sqlite3_dec
288fa 6c 61 72 65 5f 76 74 61 62 20 20 20 20 20 20 20  lare_vtab       
288fb 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
288fc 3e 64 65 63 6c 61 72 65 5f 76 74 61 62 0a 23 64  >declare_vtab.#d
288fd 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e  efine sqlite3_en
288fe 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
288ff 65 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  e    sqlite3_api
28900 2d 3e 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  ->enable_shared_
28901 63 61 63 68 65 0a 23 64 65 66 69 6e 65 20 73 71  cache.#define sq
28902 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 20 20 20  lite3_errcode   
28903 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28904 69 74 65 33 5f 61 70 69 2d 3e 65 72 72 63 6f 64  ite3_api->errcod
28905 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28906 33 5f 65 72 72 6d 73 67 20 20 20 20 20 20 20 20  3_errmsg        
28907 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28908 5f 61 70 69 2d 3e 65 72 72 6d 73 67 0a 23 64 65  _api->errmsg.#de
28909 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72  fine sqlite3_err
2890a 6d 73 67 31 36 20 20 20 20 20 20 20 20 20 20 20  msg16           
2890b 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2890c 3e 65 72 72 6d 73 67 31 36 0a 23 64 65 66 69 6e  >errmsg16.#defin
2890d 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 20  e sqlite3_exec  
2890e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2890f 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78   sqlite3_api->ex
28910 65 63 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ec.#ifndef SQLIT
28911 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
28912 44 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  D.#define sqlite
28913 33 5f 65 78 70 69 72 65 64 20 20 20 20 20 20 20  3_expired       
28914 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28915 5f 61 70 69 2d 3e 65 78 70 69 72 65 64 0a 23 65  _api->expired.#e
28916 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndif.#define sql
28917 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 20 20  ite3_finalize   
28918 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28919 74 65 33 5f 61 70 69 2d 3e 66 69 6e 61 6c 69 7a  te3_api->finaliz
2891a 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
2891b 33 5f 66 72 65 65 20 20 20 20 20 20 20 20 20 20  3_free          
2891c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2891d 5f 61 70 69 2d 3e 66 72 65 65 0a 23 64 65 66 69  _api->free.#defi
2891e 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  ne sqlite3_free_
2891f 74 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  table           
28920 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66    sqlite3_api->f
28921 72 65 65 5f 74 61 62 6c 65 0a 23 64 65 66 69 6e  ree_table.#defin
28922 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  e sqlite3_get_au
28923 74 6f 63 6f 6d 6d 69 74 20 20 20 20 20 20 20 20  tocommit        
28924 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 65   sqlite3_api->ge
28925 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 0a 23 64 65  t_autocommit.#de
28926 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74  fine sqlite3_get
28927 5f 61 75 78 64 61 74 61 20 20 20 20 20 20 20 20  _auxdata        
28928 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28929 3e 67 65 74 5f 61 75 78 64 61 74 61 0a 23 64 65  >get_auxdata.#de
2892a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74  fine sqlite3_get
2892b 5f 74 61 62 6c 65 20 20 20 20 20 20 20 20 20 20  _table          
2892c 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2892d 3e 67 65 74 5f 74 61 62 6c 65 0a 23 69 66 6e 64  >get_table.#ifnd
2892e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
2892f 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e  EPRECATED.#defin
28930 65 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c  e sqlite3_global
28931 5f 72 65 63 6f 76 65 72 20 20 20 20 20 20 20 20  _recover        
28932 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 6c   sqlite3_api->gl
28933 6f 62 61 6c 5f 72 65 63 6f 76 65 72 0a 23 65 6e  obal_recover.#en
28934 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dif.#define sqli
28935 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 20 20  te3_interrupt   
28936 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28937 65 33 5f 61 70 69 2d 3e 69 6e 74 65 72 72 75 70  e3_api->interrup
28938 74 78 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  tx.#define sqlit
28939 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
2893a 6f 77 69 64 20 20 20 20 20 20 73 71 6c 69 74 65  owid      sqlite
2893b 33 5f 61 70 69 2d 3e 6c 61 73 74 5f 69 6e 73 65  3_api->last_inse
2893c 72 74 5f 72 6f 77 69 64 0a 23 64 65 66 69 6e 65  rt_rowid.#define
2893d 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
2893e 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
2893f 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 62  sqlite3_api->lib
28940 76 65 72 73 69 6f 6e 0a 23 64 65 66 69 6e 65 20  version.#define 
28941 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
28942 6f 6e 5f 6e 75 6d 62 65 72 20 20 20 20 20 20 73  on_number      s
28943 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 62 76  qlite3_api->libv
28944 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 0a 23 64  ersion_number.#d
28945 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 61  efine sqlite3_ma
28946 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20  lloc            
28947 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28948 2d 3e 6d 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65  ->malloc.#define
28949 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
2894a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2894b 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 70 72  sqlite3_api->mpr
2894c 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c  intf.#define sql
2894d 69 74 65 33 5f 6f 70 65 6e 20 20 20 20 20 20 20  ite3_open       
2894e 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2894f 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 0a 23 64  te3_api->open.#d
28950 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70  efine sqlite3_op
28951 65 6e 31 36 20 20 20 20 20 20 20 20 20 20 20 20  en16            
28952 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28953 2d 3e 6f 70 65 6e 31 36 0a 23 64 65 66 69 6e 65  ->open16.#define
28954 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
28955 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28956 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65  sqlite3_api->pre
28957 70 61 72 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  pare.#define sql
28958 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 20 20  ite3_prepare16  
28959 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2895a 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65  te3_api->prepare
2895b 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
2895c 65 33 5f 70 72 65 70 61 72 65 5f 76 32 20 20 20  e3_prepare_v2   
2895d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2895e 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 5f 76  3_api->prepare_v
2895f 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  2.#define sqlite
28960 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20 20  3_prepare16_v2  
28961 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28962 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 31 36 5f  _api->prepare16_
28963 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  v2.#define sqlit
28964 65 33 5f 70 72 6f 66 69 6c 65 20 20 20 20 20 20  e3_profile      
28965 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28966 33 5f 61 70 69 2d 3e 70 72 6f 66 69 6c 65 0a 23  3_api->profile.#
28967 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
28968 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 20  rogress_handler 
28969 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2896a 69 2d 3e 70 72 6f 67 72 65 73 73 5f 68 61 6e 64  i->progress_hand
2896b 6c 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ler.#define sqli
2896c 74 65 33 5f 72 65 61 6c 6c 6f 63 20 20 20 20 20  te3_realloc     
2896d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2896e 65 33 5f 61 70 69 2d 3e 72 65 61 6c 6c 6f 63 0a  e3_api->realloc.
2896f 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28970 72 65 73 65 74 20 20 20 20 20 20 20 20 20 20 20  reset           
28971 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28972 70 69 2d 3e 72 65 73 65 74 0a 23 64 65 66 69 6e  pi->reset.#defin
28973 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
28974 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20  _blob           
28975 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
28976 73 75 6c 74 5f 62 6c 6f 62 0a 23 64 65 66 69 6e  sult_blob.#defin
28977 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
28978 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20  _double         
28979 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
2897a 73 75 6c 74 5f 64 6f 75 62 6c 65 0a 23 64 65 66  sult_double.#def
2897b 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
2897c 6c 74 5f 65 72 72 6f 72 20 20 20 20 20 20 20 20  lt_error        
2897d 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2897e 72 65 73 75 6c 74 5f 65 72 72 6f 72 0a 23 64 65  result_error.#de
2897f 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
28980 75 6c 74 5f 65 72 72 6f 72 31 36 20 20 20 20 20  ult_error16     
28981 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28982 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 0a  >result_error16.
28983 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28984 72 65 73 75 6c 74 5f 69 6e 74 20 20 20 20 20 20  result_int      
28985 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28986 70 69 2d 3e 72 65 73 75 6c 74 5f 69 6e 74 0a 23  pi->result_int.#
28987 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
28988 65 73 75 6c 74 5f 69 6e 74 36 34 20 20 20 20 20  esult_int64     
28989 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2898a 69 2d 3e 72 65 73 75 6c 74 5f 69 6e 74 36 34 0a  i->result_int64.
2898b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2898c 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 20 20 20 20  result_null     
2898d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2898e 70 69 2d 3e 72 65 73 75 6c 74 5f 6e 75 6c 6c 0a  pi->result_null.
2898f 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28990 72 65 73 75 6c 74 5f 74 65 78 74 20 20 20 20 20  result_text     
28991 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28992 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 0a  pi->result_text.
28993 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28994 72 65 73 75 6c 74 5f 74 65 78 74 31 36 20 20 20  result_text16   
28995 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28996 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 31  pi->result_text1
28997 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
28998 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62  3_result_text16b
28999 65 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  e        sqlite3
2899a 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78  _api->result_tex
2899b 74 31 36 62 65 0a 23 64 65 66 69 6e 65 20 73 71  t16be.#define sq
2899c 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2899d 74 31 36 6c 65 20 20 20 20 20 20 20 20 73 71 6c  t16le        sql
2899e 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
2899f 5f 74 65 78 74 31 36 6c 65 0a 23 64 65 66 69 6e  _text16le.#defin
289a0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
289a1 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20  _value          
289a2 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
289a3 73 75 6c 74 5f 76 61 6c 75 65 0a 23 64 65 66 69  sult_value.#defi
289a4 6e 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62  ne sqlite3_rollb
289a5 61 63 6b 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20  ack_hook        
289a6 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
289a7 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 0a 23 64 65  ollback_hook.#de
289a8 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 65 74  fine sqlite3_set
289a9 5f 61 75 74 68 6f 72 69 7a 65 72 20 20 20 20 20  _authorizer     
289aa 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
289ab 3e 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 0a  >set_authorizer.
289ac 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
289ad 73 65 74 5f 61 75 78 64 61 74 61 20 20 20 20 20  set_auxdata     
289ae 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
289af 70 69 2d 3e 73 65 74 5f 61 75 78 64 61 74 61 0a  pi->set_auxdata.
289b0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
289b1 73 6e 70 72 69 6e 74 66 20 20 20 20 20 20 20 20  snprintf        
289b2 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
289b3 70 69 2d 3e 73 6e 70 72 69 6e 74 66 0a 23 64 65  pi->snprintf.#de
289b4 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 74 65  fine sqlite3_ste
289b5 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
289b6 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
289b7 3e 73 74 65 70 0a 23 64 65 66 69 6e 65 20 73 71  >step.#define sq
289b8 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75  lite3_table_colu
289b9 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20 73 71 6c  mn_metadata  sql
289ba 69 74 65 33 5f 61 70 69 2d 3e 74 61 62 6c 65 5f  ite3_api->table_
289bb 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 0a  column_metadata.
289bc 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
289bd 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 20 20  thread_cleanup  
289be 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
289bf 70 69 2d 3e 74 68 72 65 61 64 5f 63 6c 65 61 6e  pi->thread_clean
289c0 75 70 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  up.#define sqlit
289c1 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
289c2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
289c3 33 5f 61 70 69 2d 3e 74 6f 74 61 6c 5f 63 68 61  3_api->total_cha
289c4 6e 67 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c  nges.#define sql
289c5 69 74 65 33 5f 74 72 61 63 65 20 20 20 20 20 20  ite3_trace      
289c6 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
289c7 74 65 33 5f 61 70 69 2d 3e 74 72 61 63 65 0a 23  te3_api->trace.#
289c8 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
289c9 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 23 64  IT_DEPRECATED.#d
289ca 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72  efine sqlite3_tr
289cb 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 20  ansfer_bindings 
289cc 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
289cd 2d 3e 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69  ->transfer_bindi
289ce 6e 67 73 0a 23 65 6e 64 69 66 0a 23 64 65 66 69  ngs.#endif.#defi
289cf 6e 65 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74  ne sqlite3_updat
289d0 65 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20 20 20  e_hook          
289d1 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75    sqlite3_api->u
289d2 70 64 61 74 65 5f 68 6f 6f 6b 0a 23 64 65 66 69  pdate_hook.#defi
289d3 6e 65 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  ne sqlite3_user_
289d4 64 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20  data            
289d5 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75    sqlite3_api->u
289d6 73 65 72 5f 64 61 74 61 0a 23 64 65 66 69 6e 65  ser_data.#define
289d7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
289d8 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 20  lob             
289d9 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
289da 75 65 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20  ue_blob.#define 
289db 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
289dc 74 65 73 20 20 20 20 20 20 20 20 20 20 20 20 73  tes            s
289dd 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
289de 65 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20  e_bytes.#define 
289df 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
289e0 74 65 73 31 36 20 20 20 20 20 20 20 20 20 20 73  tes16          s
289e1 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
289e2 65 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69 6e  e_bytes16.#defin
289e3 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
289e4 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20  double          
289e5 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
289e6 6c 75 65 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69  lue_double.#defi
289e7 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
289e8 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  _int            
289e9 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
289ea 61 6c 75 65 5f 69 6e 74 0a 23 64 65 66 69 6e 65  alue_int.#define
289eb 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
289ec 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 20 20  nt64            
289ed 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
289ee 75 65 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65  ue_int64.#define
289ef 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
289f0 75 6d 65 72 69 63 5f 74 79 70 65 20 20 20 20 20  umeric_type     
289f1 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
289f2 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 0a  ue_numeric_type.
289f3 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
289f4 76 61 6c 75 65 5f 74 65 78 74 20 20 20 20 20 20  value_text      
289f5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
289f6 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 0a 23  pi->value_text.#
289f7 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
289f8 61 6c 75 65 5f 74 65 78 74 31 36 20 20 20 20 20  alue_text16     
289f9 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
289fa 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36 0a  i->value_text16.
289fb 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
289fc 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 20 20  value_text16be  
289fd 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
289fe 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36  pi->value_text16
289ff 62 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  be.#define sqlit
28a00 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c  e3_value_text16l
28a01 65 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  e         sqlite
28a02 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78  3_api->value_tex
28a03 74 31 36 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  t16le.#define sq
28a04 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
28a05 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28a06 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
28a07 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  type.#define sql
28a08 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 20 20 20  ite3_vmprintf   
28a09 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28a0a 74 65 33 5f 61 70 69 2d 3e 76 6d 70 72 69 6e 74  te3_api->vmprint
28a0b 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  f.#define sqlite
28a0c 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
28a0d 69 6f 6e 20 20 20 20 20 20 73 71 6c 69 74 65 33  ion      sqlite3
28a0e 5f 61 70 69 2d 3e 6f 76 65 72 6c 6f 61 64 5f 66  _api->overload_f
28a0f 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20  unction.#define 
28a10 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
28a11 76 32 20 20 20 20 20 20 20 20 20 20 20 20 20 73  v2             s
28a12 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70  qlite3_api->prep
28a13 61 72 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  are_v2.#define s
28a14 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
28a15 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 73 71  _v2           sq
28a16 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61  lite3_api->prepa
28a17 72 65 31 36 5f 76 32 0a 23 64 65 66 69 6e 65 20  re16_v2.#define 
28a18 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69  sqlite3_clear_bi
28a19 6e 64 69 6e 67 73 20 20 20 20 20 20 20 20 20 73  ndings         s
28a1a 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 65 61  qlite3_api->clea
28a1b 72 5f 62 69 6e 64 69 6e 67 73 0a 23 64 65 66 69  r_bindings.#defi
28a1c 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
28a1d 7a 65 72 6f 62 6c 6f 62 20 20 20 20 20 20 20 20  zeroblob        
28a1e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
28a1f 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 0a 23 64 65  ind_zeroblob.#de
28a20 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
28a21 62 5f 62 79 74 65 73 20 20 20 20 20 20 20 20 20  b_bytes         
28a22 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28a23 3e 62 6c 6f 62 5f 62 79 74 65 73 0a 23 64 65 66  >blob_bytes.#def
28a24 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
28a25 5f 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20  _close          
28a26 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28a27 62 6c 6f 62 5f 63 6c 6f 73 65 0a 23 64 65 66 69  blob_close.#defi
28a28 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
28a29 6f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20  open            
28a2a 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
28a2b 6c 6f 62 5f 6f 70 65 6e 0a 23 64 65 66 69 6e 65  lob_open.#define
28a2c 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
28a2d 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ad              
28a2e 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f  sqlite3_api->blo
28a2f 62 5f 72 65 61 64 0a 23 64 65 66 69 6e 65 20 73  b_read.#define s
28a30 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
28a31 65 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  e             sq
28a32 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f  lite3_api->blob_
28a33 77 72 69 74 65 0a 23 64 65 66 69 6e 65 20 73 71  write.#define sq
28a34 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
28a35 6c 61 74 69 6f 6e 5f 76 32 20 20 20 20 73 71 6c  lation_v2    sql
28a36 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65  ite3_api->create
28a37 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 0a 23 64  _collation_v2.#d
28a38 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69  efine sqlite3_fi
28a39 6c 65 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20 20  le_control      
28a3a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28a3b 2d 3e 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 0a 23  ->file_control.#
28a3c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
28a3d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 20  emory_highwater 
28a3e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28a3f 69 2d 3e 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  i->memory_highwa
28a40 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ter.#define sqli
28a41 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 20  te3_memory_used 
28a42 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28a43 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79 5f 75  e3_api->memory_u
28a44 73 65 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  sed.#define sqli
28a45 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 20  te3_mutex_alloc 
28a46 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28a47 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 61 6c  e3_api->mutex_al
28a48 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  loc.#define sqli
28a49 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 20  te3_mutex_enter 
28a4a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28a4b 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 65 6e  e3_api->mutex_en
28a4c 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ter.#define sqli
28a4d 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 20 20  te3_mutex_free  
28a4e 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28a4f 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 66 72  e3_api->mutex_fr
28a50 65 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ee.#define sqlit
28a51 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 20 20  e3_mutex_leave  
28a52 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28a53 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 6c 65 61  3_api->mutex_lea
28a54 76 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ve.#define sqlit
28a55 65 33 5f 6d 75 74 65 78 5f 74 72 79 20 20 20 20  e3_mutex_try    
28a56 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28a57 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 74 72 79  3_api->mutex_try
28a58 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28a59 5f 6f 70 65 6e 5f 76 32 20 20 20 20 20 20 20 20  _open_v2        
28a5a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28a5b 61 70 69 2d 3e 6f 70 65 6e 5f 76 32 0a 23 64 65  api->open_v2.#de
28a5c 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 6c  fine sqlite3_rel
28a5d 65 61 73 65 5f 6d 65 6d 6f 72 79 20 20 20 20 20  ease_memory     
28a5e 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28a5f 3e 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 0a  >release_memory.
28a60 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28a61 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
28a62 65 6d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  em     sqlite3_a
28a63 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72  pi->result_error
28a64 5f 6e 6f 6d 65 6d 0a 23 64 65 66 69 6e 65 20 73  _nomem.#define s
28a65 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
28a66 72 6f 72 5f 74 6f 6f 62 69 67 20 20 20 20 73 71  ror_toobig    sq
28a67 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
28a68 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 0a 23  t_error_toobig.#
28a69 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
28a6a 6c 65 65 70 20 20 20 20 20 20 20 20 20 20 20 20  leep            
28a6b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28a6c 69 2d 3e 73 6c 65 65 70 0a 23 64 65 66 69 6e 65  i->sleep.#define
28a6d 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65   sqlite3_soft_he
28a6e 61 70 5f 6c 69 6d 69 74 20 20 20 20 20 20 20 20  ap_limit        
28a6f 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6f 66  sqlite3_api->sof
28a70 74 5f 68 65 61 70 5f 6c 69 6d 69 74 0a 23 64 65  t_heap_limit.#de
28a71 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 66 73  fine sqlite3_vfs
28a72 5f 66 69 6e 64 20 20 20 20 20 20 20 20 20 20 20  _find           
28a73 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28a74 3e 76 66 73 5f 66 69 6e 64 0a 23 64 65 66 69 6e  >vfs_find.#defin
28a75 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65  e sqlite3_vfs_re
28a76 67 69 73 74 65 72 20 20 20 20 20 20 20 20 20 20  gister          
28a77 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 66   sqlite3_api->vf
28a78 73 5f 72 65 67 69 73 74 65 72 0a 23 64 65 66 69  s_register.#defi
28a79 6e 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75  ne sqlite3_vfs_u
28a7a 6e 72 65 67 69 73 74 65 72 20 20 20 20 20 20 20  nregister       
28a7b 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
28a7c 66 73 5f 75 6e 72 65 67 69 73 74 65 72 0a 23 64  fs_unregister.#d
28a7d 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 68  efine sqlite3_th
28a7e 72 65 61 64 73 61 66 65 20 20 20 20 20 20 20 20  readsafe        
28a7f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28a80 2d 3e 78 74 68 72 65 61 64 73 61 66 65 0a 23 64  ->xthreadsafe.#d
28a81 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
28a82 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 20 20 20  sult_zeroblob   
28a83 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28a84 2d 3e 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f  ->result_zeroblo
28a85 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  b.#define sqlite
28a86 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
28a87 6f 64 65 20 20 20 20 20 20 73 71 6c 69 74 65 33  ode      sqlite3
28a88 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72  _api->result_err
28a89 6f 72 5f 63 6f 64 65 0a 23 64 65 66 69 6e 65 20  or_code.#define 
28a8a 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
28a8b 74 72 6f 6c 20 20 20 20 20 20 20 20 20 20 20 73  trol           s
28a8c 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 65 73 74  qlite3_api->test
28a8d 5f 63 6f 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65  _control.#define
28a8e 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
28a8f 65 73 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ess             
28a90 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 61 6e  sqlite3_api->ran
28a91 64 6f 6d 6e 65 73 73 0a 23 64 65 66 69 6e 65 20  domness.#define 
28a92 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
28a93 64 62 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 73  db_handle      s
28a94 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6e 74  qlite3_api->cont
28a95 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 0a 23 64  ext_db_handle.#d
28a96 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 78  efine sqlite3_ex
28a97 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f  tended_result_co
28a98 64 65 73 20 20 73 71 6c 69 74 65 33 5f 61 70 69  des  sqlite3_api
28a99 2d 3e 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c  ->extended_resul
28a9a 74 5f 63 6f 64 65 73 0a 23 64 65 66 69 6e 65 20  t_codes.#define 
28a9b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 20 20 20  sqlite3_limit   
28a9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
28a9d 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 6d 69  qlite3_api->limi
28a9e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
28a9f 33 5f 6e 65 78 74 5f 73 74 6d 74 20 20 20 20 20  3_next_stmt     
28aa0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28aa1 5f 61 70 69 2d 3e 6e 65 78 74 5f 73 74 6d 74 0a  _api->next_stmt.
28aa2 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28aa3 73 71 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  sql             
28aa4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28aa5 70 69 2d 3e 73 71 6c 0a 23 64 65 66 69 6e 65 20  pi->sql.#define 
28aa6 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 20 20  sqlite3_status  
28aa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
28aa8 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 74 61 74  qlite3_api->stat
28aa9 75 73 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  us.#endif /* SQL
28aaa 49 54 45 5f 43 4f 52 45 20 2a 2f 0a 0a 23 64 65  ITE_CORE */..#de
28aab 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45  fine SQLITE_EXTE
28aac 4e 53 49 4f 4e 5f 49 4e 49 54 31 20 20 20 20 20  NSION_INIT1     
28aad 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
28aae 69 5f 72 6f 75 74 69 6e 65 73 20 2a 73 71 6c 69  i_routines *sqli
28aaf 74 65 33 5f 61 70 69 20 3d 20 30 3b 0a 23 64 65  te3_api = 0;.#de
28ab0 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45  fine SQLITE_EXTE
28ab1 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 76 29 20 20  NSION_INIT2(v)  
28ab2 73 71 6c 69 74 65 33 5f 61 70 69 20 3d 20 76 3b  sqlite3_api = v;
28ab3 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c  ..#endif /* _SQL
28ab4 49 54 45 33 45 58 54 5f 48 5f 20 2a 2f 0a 0a 2f  ITE3EXT_H_ */../
28ab5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
28ab6 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 65 78 74  nd of sqlite3ext
28ab7 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
28ab8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ab9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
28aba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
28abb 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
28abc 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6c  we left off in l
28abd 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  oadext.c *******
28abe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
28abf 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28ac0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
28ac1 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 41  ON../*.** Some A
28ac2 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  PI routines are 
28ac3 6f 6d 69 74 74 65 64 20 77 68 65 6e 20 76 61 72  omitted when var
28ac4 69 6f 75 73 20 66 65 61 74 75 72 65 73 20 61 72  ious features ar
28ac5 65 0a 2a 2a 20 65 78 63 6c 75 64 65 64 20 66 72  e.** excluded fr
28ac6 6f 6d 20 61 20 62 75 69 6c 64 20 6f 66 20 53 51  om a build of SQ
28ac7 4c 69 74 65 2e 20 20 53 75 62 73 74 69 74 75 74  Lite.  Substitut
28ac8 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  e a NULL pointer
28ac9 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 6d 69 73 73  .** for any miss
28aca 69 6e 67 20 41 50 49 73 2e 0a 2a 2f 0a 23 69 66  ing APIs..*/.#if
28acb 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  ndef SQLITE_ENAB
28acc 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
28acd 54 41 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  TA.# define sqli
28ace 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
28acf 61 73 65 5f 6e 61 6d 65 20 20 20 30 0a 23 20 64  ase_name   0.# d
28ad0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
28ad1 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
28ad2 6d 65 31 36 20 30 0a 23 20 64 65 66 69 6e 65 20  me16 0.# define 
28ad3 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
28ad4 61 62 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20 30  able_name      0
28ad5 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28ad6 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
28ad7 61 6d 65 31 36 20 20 20 20 30 0a 23 20 64 65 66  ame16    0.# def
28ad8 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28ad9 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20 20  mn_origin_name  
28ada 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28adb 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
28adc 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a 23  gin_name16   0.#
28add 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28ade 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
28adf 61 64 61 74 61 20 20 30 0a 23 65 6e 64 69 66 0a  adata  0.#endif.
28ae0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
28ae1 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
28ae2 4e 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  N.# define sqlit
28ae3 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
28ae4 72 20 20 20 20 20 20 20 20 20 30 0a 23 65 6e 64  r         0.#end
28ae5 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
28ae6 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 23 20 64  E_OMIT_UTF16.# d
28ae7 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
28ae8 6e 64 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  nd_text16       
28ae9 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
28aea 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
28aeb 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20 30  n_needed16     0
28aec 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28aed 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
28aee 65 31 36 20 20 20 20 20 20 30 0a 23 20 64 65 66  e16      0.# def
28aef 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28af0 6d 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20  mn_name16       
28af1 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28af2 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
28af3 74 31 36 20 20 20 20 20 20 20 20 20 20 30 0a 23  t16          0.#
28af4 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28af5 63 6f 6d 70 6c 65 74 65 31 36 20 20 20 20 20 20  complete16      
28af6 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
28af7 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
28af8 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20  _collation16    
28af9 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
28afa 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
28afb 69 6f 6e 31 36 20 20 20 20 20 20 30 0a 23 20 64  ion16      0.# d
28afc 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72  efine sqlite3_er
28afd 72 6d 73 67 31 36 20 20 20 20 20 20 20 20 20 20  rmsg16          
28afe 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
28aff 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 20 20  sqlite3_open16  
28b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
28b01 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28b02 33 5f 70 72 65 70 61 72 65 31 36 20 20 20 20 20  3_prepare16     
28b03 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
28b04 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
28b05 61 72 65 31 36 5f 76 32 20 20 20 20 20 20 20 20  are16_v2        
28b06 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28b07 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
28b08 6f 72 31 36 20 20 20 20 20 20 20 20 20 30 0a 23  or16         0.#
28b09 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28b0a 72 65 73 75 6c 74 5f 74 65 78 74 31 36 20 20 20  result_text16   
28b0b 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
28b0c 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
28b0d 5f 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20  _text16be       
28b0e 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
28b0f 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
28b10 36 6c 65 20 20 20 20 20 20 20 20 30 0a 23 20 64  6le        0.# d
28b11 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
28b12 6c 75 65 5f 74 65 78 74 31 36 20 20 20 20 20 20  lue_text16      
28b13 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
28b14 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
28b15 78 74 31 36 62 65 20 20 20 20 20 20 20 20 20 30  xt16be         0
28b16 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28b17 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
28b18 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
28b19 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28b1a 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
28b1b 31 36 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71  16 0.# define sq
28b1c 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
28b1d 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20 30 0a 23  le_name16    0.#
28b1e 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28b1f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
28b20 6d 65 31 36 20 20 20 30 0a 23 65 6e 64 69 66 0a  me16   0.#endif.
28b21 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
28b22 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a 23 20 64  MIT_COMPLETE.# d
28b23 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
28b24 6d 70 6c 65 74 65 20 30 0a 23 20 64 65 66 69 6e  mplete 0.# defin
28b25 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  e sqlite3_comple
28b26 74 65 31 36 20 30 0a 23 65 6e 64 69 66 0a 0a 23  te16 0.#endif..#
28b27 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
28b28 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
28b29 41 43 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ACK.# define sql
28b2a 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61  ite3_progress_ha
28b2b 6e 64 6c 65 72 20 30 0a 23 65 6e 64 69 66 0a 0a  ndler 0.#endif..
28b2c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
28b2d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
28b2e 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
28b2f 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 30  _create_module 0
28b30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28b31 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
28b32 76 32 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71  v2 0.# define sq
28b33 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
28b34 61 62 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ab 0.#endif..#if
28b35 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28b36 53 48 41 52 45 44 5f 43 41 43 48 45 0a 23 20 64  SHARED_CACHE.# d
28b37 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e  efine sqlite3_en
28b38 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
28b39 65 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  e 0.#endif..#ifd
28b3a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
28b3b 52 41 43 45 0a 23 20 64 65 66 69 6e 65 20 73 71  RACE.# define sq
28b3c 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 20 20 20  lite3_profile   
28b3d 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
28b3e 71 6c 69 74 65 33 5f 74 72 61 63 65 20 20 20 20  qlite3_trace    
28b3f 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23       0.#endif..#
28b40 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
28b41 54 5f 47 45 54 5f 54 41 42 4c 45 0a 23 20 64 65  T_GET_TABLE.# de
28b42 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65  fine sqlite3_fre
28b43 65 5f 74 61 62 6c 65 20 20 20 20 30 0a 23 20 64  e_table    0.# d
28b44 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65  efine sqlite3_ge
28b45 74 5f 74 61 62 6c 65 20 20 20 20 20 30 0a 23 65  t_table     0.#e
28b46 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
28b47 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
28b48 42 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  B.#define sqlite
28b49 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20  3_bind_zeroblob 
28b4a 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74   0.#define sqlit
28b4b 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20 20  e3_blob_bytes   
28b4c 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69    0.#define sqli
28b4d 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20  te3_blob_close  
28b4e 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c     0.#define sql
28b4f 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20  ite3_blob_open  
28b50 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71      0.#define sq
28b51 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 20  lite3_blob_read 
28b52 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73       0.#define s
28b53 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
28b54 65 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a  e     0.#endif..
28b55 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
28b56 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f  ing structure co
28b57 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20  ntains pointers 
28b58 74 6f 20 61 6c 6c 20 53 51 4c 69 74 65 20 41 50  to all SQLite AP
28b59 49 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41  I routines..** A
28b5a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
28b5b 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61   structure is pa
28b5c 73 73 65 64 20 69 6e 74 6f 20 65 78 74 65 6e 73  ssed into extens
28b5d 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 79 20 61  ions when they a
28b5e 72 65 0a 2a 2a 20 6c 6f 61 64 65 64 20 73 6f 20  re.** loaded so 
28b5f 74 68 61 74 20 74 68 65 20 65 78 74 65 6e 73 69  that the extensi
28b60 6f 6e 20 63 61 6e 20 6d 61 6b 65 20 63 61 6c 6c  on can make call
28b61 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  s back into the 
28b62 53 51 4c 69 74 65 0a 2a 2a 20 6c 69 62 72 61 72  SQLite.** librar
28b63 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 64  y..**.** When ad
28b64 64 69 6e 67 20 6e 65 77 20 41 50 49 73 2c 20 61  ding new APIs, a
28b65 64 64 20 74 68 65 6d 20 74 6f 20 74 68 65 20 62  dd them to the b
28b66 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 20 73 74  ottom of this st
28b67 72 75 63 74 75 72 65 0a 2a 2a 20 69 6e 20 6f 72  ructure.** in or
28b68 64 65 72 20 74 6f 20 70 72 65 73 65 72 76 65 20  der to preserve 
28b69 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
28b6a 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 45  ibility..**.** E
28b6b 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 75  xtensions that u
28b6c 73 65 20 6e 65 77 65 72 20 41 50 49 73 20 73 68  se newer APIs sh
28b6d 6f 75 6c 64 20 66 69 72 73 74 20 63 61 6c 6c 20  ould first call 
28b6e 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c  the.** sqlite3_l
28b6f 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
28b70 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  () to make sure 
28b71 74 68 61 74 20 74 68 65 20 41 50 49 20 74 68 65  that the API the
28b72 79 0a 2a 2a 20 69 6e 74 65 6e 64 20 74 6f 20 75  y.** intend to u
28b73 73 65 20 69 73 20 73 75 70 70 6f 72 74 65 64 20  se is supported 
28b74 62 79 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20  by the library. 
28b75 20 45 78 74 65 6e 73 69 6f 6e 73 20 73 68 6f 75   Extensions shou
28b76 6c 64 0a 2a 2a 20 61 6c 73 6f 20 63 68 65 63 6b  ld.** also check
28b77 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
28b78 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  at the pointer t
28b79 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  o the function i
28b7a 73 0a 2a 2a 20 6e 6f 74 20 4e 55 4c 4c 20 62 65  s.** not NULL be
28b7b 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 69 74 2e  fore calling it.
28b7c 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
28b7d 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
28b7e 74 69 6e 65 73 20 73 71 6c 69 74 65 33 41 70 69  tines sqlite3Api
28b7f 73 20 3d 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  s = {.  sqlite3_
28b80 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
28b81 74 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  t,.#ifndef SQLIT
28b82 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
28b83 44 0a 20 20 73 71 6c 69 74 65 33 5f 61 67 67 72  D.  sqlite3_aggr
28b84 65 67 61 74 65 5f 63 6f 75 6e 74 2c 0a 23 65 6c  egate_count,.#el
28b85 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20  se.  0,.#endif. 
28b86 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
28b87 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  ob,.  sqlite3_bi
28b88 6e 64 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c  nd_double,.  sql
28b89 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 2c 0a 20  ite3_bind_int,. 
28b8a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
28b8b 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  t64,.  sqlite3_b
28b8c 69 6e 64 5f 6e 75 6c 6c 2c 0a 20 20 73 71 6c 69  ind_null,.  sqli
28b8d 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
28b8e 65 72 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69  er_count,.  sqli
28b8f 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
28b90 65 72 5f 69 6e 64 65 78 2c 0a 20 20 73 71 6c 69  er_index,.  sqli
28b91 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
28b92 65 72 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74  er_name,.  sqlit
28b93 65 33 5f 62 69 6e 64 5f 74 65 78 74 2c 0a 20 20  e3_bind_text,.  
28b94 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
28b95 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  t16,.  sqlite3_b
28b96 69 6e 64 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c  ind_value,.  sql
28b97 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65  ite3_busy_handle
28b98 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75 73  r,.  sqlite3_bus
28b99 79 5f 74 69 6d 65 6f 75 74 2c 0a 20 20 73 71 6c  y_timeout,.  sql
28b9a 69 74 65 33 5f 63 68 61 6e 67 65 73 2c 0a 20 20  ite3_changes,.  
28b9b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 2c 0a 20  sqlite3_close,. 
28b9c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
28b9d 6f 6e 5f 6e 65 65 64 65 64 2c 0a 20 20 73 71 6c  on_needed,.  sql
28b9e 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
28b9f 65 65 64 65 64 31 36 2c 0a 20 20 73 71 6c 69 74  eeded16,.  sqlit
28ba0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 2c 0a  e3_column_blob,.
28ba1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28ba2 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69 74 65  _bytes,.  sqlite
28ba3 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
28ba4 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
28ba5 6d 6e 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69  mn_count,.  sqli
28ba6 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
28ba7 61 73 65 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69  ase_name,.  sqli
28ba8 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
28ba9 61 73 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71  ase_name16,.  sq
28baa 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63  lite3_column_dec
28bab 6c 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33  ltype,.  sqlite3
28bac 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
28bad 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
28bae 6c 75 6d 6e 5f 64 6f 75 62 6c 65 2c 0a 20 20 73  lumn_double,.  s
28baf 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
28bb0 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t,.  sqlite3_col
28bb1 75 6d 6e 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c  umn_int64,.  sql
28bb2 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
28bb3 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
28bb4 6d 6e 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c  mn_name16,.  sql
28bb5 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
28bb6 69 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74  in_name,.  sqlit
28bb7 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
28bb8 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74  _name16,.  sqlit
28bb9 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
28bba 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  name,.  sqlite3_
28bbb 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
28bbc 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  e16,.  sqlite3_c
28bbd 6f 6c 75 6d 6e 5f 74 65 78 74 2c 0a 20 20 73 71  olumn_text,.  sq
28bbe 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
28bbf 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  t16,.  sqlite3_c
28bc0 6f 6c 75 6d 6e 5f 74 79 70 65 2c 0a 20 20 73 71  olumn_type,.  sq
28bc1 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
28bc2 75 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ue,.  sqlite3_co
28bc3 6d 6d 69 74 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c  mmit_hook,.  sql
28bc4 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 2c 0a 20  ite3_complete,. 
28bc5 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
28bc6 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  e16,.  sqlite3_c
28bc7 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 2c  reate_collation,
28bc8 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
28bc9 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 2c 0a 20  e_collation16,. 
28bca 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
28bcb 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 73 71 6c 69  function,.  sqli
28bcc 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
28bcd 69 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ion16,.  sqlite3
28bce 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 2c 0a  _create_module,.
28bcf 20 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63    sqlite3_data_c
28bd0 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ount,.  sqlite3_
28bd1 64 62 5f 68 61 6e 64 6c 65 2c 0a 20 20 73 71 6c  db_handle,.  sql
28bd2 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
28bd3 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 6e 61  b,.  sqlite3_ena
28bd4 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65  ble_shared_cache
28bd5 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72 63  ,.  sqlite3_errc
28bd6 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65  ode,.  sqlite3_e
28bd7 72 72 6d 73 67 2c 0a 20 20 73 71 6c 69 74 65 33  rrmsg,.  sqlite3
28bd8 5f 65 72 72 6d 73 67 31 36 2c 0a 20 20 73 71 6c  _errmsg16,.  sql
28bd9 69 74 65 33 5f 65 78 65 63 2c 0a 23 69 66 6e 64  ite3_exec,.#ifnd
28bda 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
28bdb 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69  EPRECATED.  sqli
28bdc 74 65 33 5f 65 78 70 69 72 65 64 2c 0a 23 65 6c  te3_expired,.#el
28bdd 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20  se.  0,.#endif. 
28bde 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
28bdf 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  e,.  sqlite3_fre
28be0 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  e,.  sqlite3_fre
28be1 65 5f 74 61 62 6c 65 2c 0a 20 20 73 71 6c 69 74  e_table,.  sqlit
28be2 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69  e3_get_autocommi
28be3 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74  t,.  sqlite3_get
28be4 5f 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c 69  _auxdata,.  sqli
28be5 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 2c 0a 20  te3_get_table,. 
28be6 20 30 2c 20 20 20 20 20 2f 2a 20 57 61 73 20 73   0,     /* Was s
28be7 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65  qlite3_global_re
28be8 63 6f 76 65 72 28 29 2c 20 62 75 74 20 74 68 61  cover(), but tha
28be9 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65  t function is de
28bea 70 72 65 63 61 74 65 64 20 2a 2f 0a 20 20 73 71  precated */.  sq
28beb 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 2c  lite3_interrupt,
28bec 0a 20 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  .  sqlite3_last_
28bed 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 0a 20 20  insert_rowid,.  
28bee 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
28bef 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69  on,.  sqlite3_li
28bf0 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 2c  bversion_number,
28bf1 0a 20 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  .  sqlite3_mallo
28bf2 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 70 72  c,.  sqlite3_mpr
28bf3 69 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65 33 5f  intf,.  sqlite3_
28bf4 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f  open,.  sqlite3_
28bf5 6f 70 65 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65  open16,.  sqlite
28bf6 33 5f 70 72 65 70 61 72 65 2c 0a 20 20 73 71 6c  3_prepare,.  sql
28bf7 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 2c 0a  ite3_prepare16,.
28bf8 20 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c    sqlite3_profil
28bf9 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 6f  e,.  sqlite3_pro
28bfa 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 2c 0a 20  gress_handler,. 
28bfb 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
28bfc 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65  ,.  sqlite3_rese
28bfd 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  t,.  sqlite3_res
28bfe 75 6c 74 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69  ult_blob,.  sqli
28bff 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
28c00 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  e,.  sqlite3_res
28c01 75 6c 74 5f 65 72 72 6f 72 2c 0a 20 20 73 71 6c  ult_error,.  sql
28c02 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
28c03 72 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  r16,.  sqlite3_r
28c04 65 73 75 6c 74 5f 69 6e 74 2c 0a 20 20 73 71 6c  esult_int,.  sql
28c05 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
28c06 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  4,.  sqlite3_res
28c07 75 6c 74 5f 6e 75 6c 6c 2c 0a 20 20 73 71 6c 69  ult_null,.  sqli
28c08 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2c  te3_result_text,
28c09 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
28c0a 74 5f 74 65 78 74 31 36 2c 0a 20 20 73 71 6c 69  t_text16,.  sqli
28c0b 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
28c0c 36 62 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  6be,.  sqlite3_r
28c0d 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 2c 0a  esult_text16le,.
28c0e 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
28c0f 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65  _value,.  sqlite
28c10 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 2c  3_rollback_hook,
28c11 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  .  sqlite3_set_a
28c12 75 74 68 6f 72 69 7a 65 72 2c 0a 20 20 73 71 6c  uthorizer,.  sql
28c13 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
28c14 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ,.  sqlite3_snpr
28c15 69 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65 33 5f  intf,.  sqlite3_
28c16 73 74 65 70 2c 0a 20 20 73 71 6c 69 74 65 33 5f  step,.  sqlite3_
28c17 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
28c18 61 64 61 74 61 2c 0a 23 69 66 6e 64 65 66 20 53  adata,.#ifndef S
28c19 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
28c1a 43 41 54 45 44 0a 20 20 73 71 6c 69 74 65 33 5f  CATED.  sqlite3_
28c1b 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 2c 0a  thread_cleanup,.
28c1c 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69  #else.  0,.#endi
28c1d 66 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 74 61  f.  sqlite3_tota
28c1e 6c 5f 63 68 61 6e 67 65 73 2c 0a 20 20 73 71 6c  l_changes,.  sql
28c1f 69 74 65 33 5f 74 72 61 63 65 2c 0a 23 69 66 6e  ite3_trace,.#ifn
28c20 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28c21 44 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c  DEPRECATED.  sql
28c22 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
28c23 6e 64 69 6e 67 73 2c 0a 23 65 6c 73 65 0a 20 20  ndings,.#else.  
28c24 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  0,.#endif.  sqli
28c25 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 2c  te3_update_hook,
28c26 0a 20 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  .  sqlite3_user_
28c27 64 61 74 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f  data,.  sqlite3_
28c28 76 61 6c 75 65 5f 62 6c 6f 62 2c 0a 20 20 73 71  value_blob,.  sq
28c29 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
28c2a 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  s,.  sqlite3_val
28c2b 75 65 5f 62 79 74 65 73 31 36 2c 0a 20 20 73 71  ue_bytes16,.  sq
28c2c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
28c2d 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  le,.  sqlite3_va
28c2e 6c 75 65 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74  lue_int,.  sqlit
28c2f 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 2c 0a  e3_value_int64,.
28c30 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
28c31 6e 75 6d 65 72 69 63 5f 74 79 70 65 2c 0a 20 20  numeric_type,.  
28c32 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
28c33 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  xt,.  sqlite3_va
28c34 6c 75 65 5f 74 65 78 74 31 36 2c 0a 20 20 73 71  lue_text16,.  sq
28c35 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
28c36 31 36 62 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  16be,.  sqlite3_
28c37 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 2c 0a  value_text16le,.
28c38 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
28c39 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  type,.  sqlite3_
28c3a 76 6d 70 72 69 6e 74 66 2c 0a 20 20 2f 2a 0a 20  vmprintf,.  /*. 
28c3b 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c   ** The original
28c3c 20 41 50 49 20 73 65 74 20 65 6e 64 73 20 68 65   API set ends he
28c3d 72 65 2e 20 20 41 6c 6c 20 65 78 74 65 6e 73 69  re.  All extensi
28c3e 6f 6e 73 20 63 61 6e 20 63 61 6c 6c 20 61 6e 79  ons can call any
28c3f 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 41 50 49  .  ** of the API
28c40 73 20 61 62 6f 76 65 20 70 72 6f 76 69 64 65 64  s above provided
28c41 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65   that the pointe
28c42 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20  r is not NULL.  
28c43 42 75 74 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20  But.  ** before 
28c44 63 61 6c 6c 69 6e 67 20 41 50 49 73 20 74 68 61  calling APIs tha
28c45 74 20 66 6f 6c 6c 6f 77 2c 20 65 78 74 65 6e 73  t follow, extens
28c46 69 6f 6e 20 73 68 6f 75 6c 64 20 63 68 65 63 6b  ion should check
28c47 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65   the.  ** sqlite
28c48 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
28c49 62 65 72 28 29 20 74 6f 20 6d 61 6b 65 20 73 75  ber() to make su
28c4a 72 65 20 74 68 65 79 20 61 72 65 20 64 65 61 6c  re they are deal
28c4b 69 6e 67 20 77 69 74 68 0a 20 20 2a 2a 20 61 20  ing with.  ** a 
28c4c 6c 69 62 72 61 72 79 20 74 68 61 74 20 69 73 20  library that is 
28c4d 6e 65 77 20 65 6e 6f 75 67 68 20 74 6f 20 73 75  new enough to su
28c4e 70 70 6f 72 74 20 74 68 61 74 20 41 50 49 2e 0a  pport that API..
28c4f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    **************
28c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2f  ***********.  */
28c54 0a 20 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c  .  sqlite3_overl
28c55 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 2c 0a 0a 20  oad_function,.. 
28c56 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 61   /*.  ** Added a
28c57 66 74 65 72 20 33 2e 33 2e 31 33 0a 20 20 2a 2f  fter 3.3.13.  */
28c58 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  .  sqlite3_prepa
28c59 72 65 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33  re_v2,.  sqlite3
28c5a 5f 70 72 65 70 61 72 65 31 36 5f 76 32 2c 0a 20  _prepare16_v2,. 
28c5b 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62   sqlite3_clear_b
28c5c 69 6e 64 69 6e 67 73 2c 0a 0a 20 20 2f 2a 0a 20  indings,..  /*. 
28c5d 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e   ** Added for 3.
28c5e 34 2e 31 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  4.1.  */.  sqlit
28c5f 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
28c60 5f 76 32 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  _v2,..  /*.  ** 
28c61 41 64 64 65 64 20 66 6f 72 20 33 2e 35 2e 30 0a  Added for 3.5.0.
28c62 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62    */.  sqlite3_b
28c63 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 2c 0a 20 20  ind_zeroblob,.  
28c64 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
28c65 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c  es,.  sqlite3_bl
28c66 6f 62 5f 63 6c 6f 73 65 2c 0a 20 20 73 71 6c 69  ob_close,.  sqli
28c67 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 2c 0a 20  te3_blob_open,. 
28c68 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
28c69 61 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c  ad,.  sqlite3_bl
28c6a 6f 62 5f 77 72 69 74 65 2c 0a 20 20 73 71 6c 69  ob_write,.  sqli
28c6b 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
28c6c 74 69 6f 6e 5f 76 32 2c 0a 20 20 73 71 6c 69 74  tion_v2,.  sqlit
28c6d 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 2c  e3_file_control,
28c6e 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  .  sqlite3_memor
28c6f 79 5f 68 69 67 68 77 61 74 65 72 2c 0a 20 20 73  y_highwater,.  s
28c70 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
28c71 65 64 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ed,.#ifdef SQLIT
28c72 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 20 20 30  E_MUTEX_OMIT.  0
28c73 2c 20 0a 20 20 30 2c 20 0a 20 20 30 2c 0a 20 20  , .  0, .  0,.  
28c74 30 2c 0a 20 20 30 2c 0a 23 65 6c 73 65 0a 20 20  0,.  0,.#else.  
28c75 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
28c76 6c 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  loc,.  sqlite3_m
28c77 75 74 65 78 5f 65 6e 74 65 72 2c 0a 20 20 73 71  utex_enter,.  sq
28c78 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
28c79 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ,.  sqlite3_mute
28c7a 78 5f 6c 65 61 76 65 2c 0a 20 20 73 71 6c 69 74  x_leave,.  sqlit
28c7b 65 33 5f 6d 75 74 65 78 5f 74 72 79 2c 0a 23 65  e3_mutex_try,.#e
28c7c 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6f  ndif.  sqlite3_o
28c7d 70 65 6e 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65  pen_v2,.  sqlite
28c7e 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
28c7f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
28c80 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 2c 0a  lt_error_nomem,.
28c81 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
28c82 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 2c 0a 20  _error_toobig,. 
28c83 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 2c 0a   sqlite3_sleep,.
28c84 20 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68    sqlite3_soft_h
28c85 65 61 70 5f 6c 69 6d 69 74 2c 0a 20 20 73 71 6c  eap_limit,.  sql
28c86 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 2c 0a 20  ite3_vfs_find,. 
28c87 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67   sqlite3_vfs_reg
28c88 69 73 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33  ister,.  sqlite3
28c89 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 2c  _vfs_unregister,
28c8a 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65  ..  /*.  ** Adde
28c8b 64 20 66 6f 72 20 33 2e 35 2e 38 0a 20 20 2a 2f  d for 3.5.8.  */
28c8c 0a 20 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  .  sqlite3_threa
28c8d 64 73 61 66 65 2c 0a 20 20 73 71 6c 69 74 65 33  dsafe,.  sqlite3
28c8e 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  _result_zeroblob
28c8f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
28c90 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 2c 0a 20  lt_error_code,. 
28c91 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
28c92 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74 65 33  ntrol,.  sqlite3
28c93 5f 72 61 6e 64 6f 6d 6e 65 73 73 2c 0a 20 20 73  _randomness,.  s
28c94 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
28c95 62 5f 68 61 6e 64 6c 65 2c 0a 0a 20 20 2f 2a 0a  b_handle,..  /*.
28c96 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33    ** Added for 3
28c97 2e 36 2e 30 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  .6.0.  */.  sqli
28c98 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73  te3_extended_res
28c99 75 6c 74 5f 63 6f 64 65 73 2c 0a 20 20 73 71 6c  ult_codes,.  sql
28c9a 69 74 65 33 5f 6c 69 6d 69 74 2c 0a 20 20 73 71  ite3_limit,.  sq
28c9b 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 2c  lite3_next_stmt,
28c9c 0a 20 20 73 71 6c 69 74 65 33 5f 73 71 6c 2c 0a  .  sqlite3_sql,.
28c9d 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73    sqlite3_status
28c9e 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  ,.};../*.** Atte
28c9f 6d 70 74 20 74 6f 20 6c 6f 61 64 20 61 6e 20 53  mpt to load an S
28ca0 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20  QLite extension 
28ca1 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 65  library containe
28ca2 64 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a  d in the file.**
28ca3 20 7a 46 69 6c 65 2e 20 20 54 68 65 20 65 6e 74   zFile.  The ent
28ca4 72 79 20 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f  ry point is zPro
28ca5 63 2e 20 20 7a 50 72 6f 63 20 6d 61 79 20 62 65  c.  zProc may be
28ca6 20 30 20 69 6e 20 77 68 69 63 68 20 63 61 73 65   0 in which case
28ca7 20 61 0a 2a 2a 20 64 65 66 61 75 6c 74 20 65 6e   a.** default en
28ca8 74 72 79 20 70 6f 69 6e 74 20 6e 61 6d 65 20 28  try point name (
28ca9 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
28caa 6e 5f 69 6e 69 74 29 20 69 73 20 75 73 65 64 2e  n_init) is used.
28cab 20 20 55 73 65 0a 2a 2a 20 6f 66 20 74 68 65 20    Use.** of the 
28cac 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 69 73 20  default name is 
28cad 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a  recommended..**.
28cae 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
28caf 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61  _OK on success a
28cb0 6e 64 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  nd SQLITE_ERROR 
28cb1 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  if something goe
28cb2 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49  s wrong..**.** I
28cb3 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
28cb4 73 20 61 6e 64 20 70 7a 45 72 72 4d 73 67 20 69  s and pzErrMsg i
28cb5 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 66 69  s not 0, then fi
28cb6 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74  ll *pzErrMsg wit
28cb7 68 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73  h .** error mess
28cb8 61 67 65 20 74 65 78 74 2e 20 20 54 68 65 20 63  age text.  The c
28cb9 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
28cba 73 68 6f 75 6c 64 20 66 72 65 65 20 74 68 69 73  should free this
28cbb 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 79 20 63 61   memory.** by ca
28cbc 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 44 62 46  lling sqlite3DbF
28cbd 72 65 65 28 64 62 2c 20 29 2e 0a 2a 2f 0a 73 74  ree(db, )..*/.st
28cbe 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
28cbf 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28 0a 20  LoadExtension(. 
28cc0 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
28cc1 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74         /* Load t
28cc2 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74  he extension int
28cc3 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
28cc4 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
28cc5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
28cc6 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  e,    /* Name of
28cc7 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
28cc8 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65  ary containing e
28cc9 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f  xtension */.  co
28cca 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c  nst char *zProc,
28ccb 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69      /* Entry poi
28ccc 6e 74 2e 20 20 55 73 65 20 22 73 71 6c 69 74 65  nt.  Use "sqlite
28ccd 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
28cce 22 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72  " if 0 */.  char
28ccf 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20   **pzErrMsg     
28cd0 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d    /* Put error m
28cd1 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e  essage here if n
28cd2 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  ot 0 */.){.  sql
28cd3 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d  ite3_vfs *pVfs =
28cd4 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 76 6f 69   db->pVfs;.  voi
28cd5 64 20 2a 68 61 6e 64 6c 65 3b 0a 20 20 69 6e 74  d *handle;.  int
28cd6 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69 74 65   (*xInit)(sqlite
28cd7 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20  3*,char**,const 
28cd8 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
28cd9 69 6e 65 73 2a 29 3b 0a 20 20 63 68 61 72 20 2a  ines*);.  char *
28cda 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a 20 20 76  zErrmsg = 0;.  v
28cdb 6f 69 64 20 2a 2a 61 48 61 6e 64 6c 65 3b 0a 20  oid **aHandle;. 
28cdc 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4d 73 67 20   const int nMsg 
28cdd 3d 20 33 30 30 3b 0a 0a 20 20 69 66 28 20 70 7a  = 300;..  if( pz
28cde 45 72 72 4d 73 67 20 29 20 2a 70 7a 45 72 72 4d  ErrMsg ) *pzErrM
28cdf 73 67 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 69  sg = 0;..  /* Ti
28ce0 63 6b 65 74 20 23 31 38 36 33 2e 20 20 54 6f 20  cket #1863.  To 
28ce1 61 76 6f 69 64 20 61 20 63 72 65 61 74 69 6e 67  avoid a creating
28ce2 20 73 65 63 75 72 69 74 79 20 70 72 6f 62 6c 65   security proble
28ce3 6d 73 20 66 6f 72 20 6f 6c 64 65 72 0a 20 20 2a  ms for older.  *
28ce4 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74  * applications t
28ce5 68 61 74 20 72 65 6c 69 6e 6b 20 61 67 61 69 6e  hat relink again
28ce6 73 74 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e  st newer version
28ce7 73 20 6f 66 20 53 51 4c 69 74 65 2c 20 74 68 65  s of SQLite, the
28ce8 0a 20 20 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f  .  ** ability to
28ce9 20 72 75 6e 20 6c 6f 61 64 5f 65 78 74 65 6e 73   run load_extens
28cea 69 6f 6e 20 69 73 20 74 75 72 6e 65 64 20 6f 66  ion is turned of
28ceb 66 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20 4f  f by default.  O
28cec 6e 65 0a 20 20 2a 2a 20 6d 75 73 74 20 63 61 6c  ne.  ** must cal
28ced 6c 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65  l sqlite3_enable
28cee 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
28cef 29 20 74 6f 20 74 75 72 6e 20 6f 6e 20 65 78 74  ) to turn on ext
28cf0 65 6e 73 69 6f 6e 0a 20 20 2a 2a 20 6c 6f 61 64  ension.  ** load
28cf1 69 6e 67 2e 20 20 4f 74 68 65 72 77 69 73 65 20  ing.  Otherwise 
28cf2 79 6f 75 20 67 65 74 20 74 68 65 20 66 6f 6c 6c  you get the foll
28cf3 6f 77 69 6e 67 20 65 72 72 6f 72 2e 0a 20 20 2a  owing error..  *
28cf4 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61  /.  if( (db->fla
28cf5 67 73 20 26 20 53 51 4c 49 54 45 5f 4c 6f 61 64  gs & SQLITE_Load
28cf6 45 78 74 65 6e 73 69 6f 6e 29 3d 3d 30 20 29 7b  Extension)==0 ){
28cf7 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73  .    if( pzErrMs
28cf8 67 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72  g ){.      *pzEr
28cf9 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d  rMsg = sqlite3_m
28cfa 70 72 69 6e 74 66 28 22 6e 6f 74 20 61 75 74 68  printf("not auth
28cfb 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20 20 7d 0a  orized");.    }.
28cfc 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
28cfd 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20  E_ERROR;.  }..  
28cfe 69 66 28 20 7a 50 72 6f 63 3d 3d 30 20 29 7b 0a  if( zProc==0 ){.
28cff 20 20 20 20 7a 50 72 6f 63 20 3d 20 22 73 71 6c      zProc = "sql
28d00 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
28d01 6e 69 74 22 3b 0a 20 20 7d 0a 0a 20 20 68 61 6e  nit";.  }..  han
28d02 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 44  dle = sqlite3OsD
28d03 6c 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c  lOpen(pVfs, zFil
28d04 65 29 3b 0a 20 20 69 66 28 20 68 61 6e 64 6c 65  e);.  if( handle
28d05 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
28d06 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20  zErrMsg ){.     
28d07 20 7a 45 72 72 6d 73 67 20 3d 20 73 71 6c 69 74   zErrmsg = sqlit
28d08 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f  e3StackAllocZero
28d09 28 64 62 2c 20 6e 4d 73 67 29 3b 0a 20 20 20 20  (db, nMsg);.    
28d0a 20 20 69 66 28 20 7a 45 72 72 6d 73 67 20 29 7b    if( zErrmsg ){
28d0b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28d0c 5f 73 6e 70 72 69 6e 74 66 28 6e 4d 73 67 2c 20  _snprintf(nMsg, 
28d0d 7a 45 72 72 6d 73 67 2c 20 0a 20 20 20 20 20 20  zErrmsg, .      
28d0e 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f        "unable to
28d0f 20 6f 70 65 6e 20 73 68 61 72 65 64 20 6c 69 62   open shared lib
28d10 72 61 72 79 20 5b 25 73 5d 22 2c 20 7a 46 69 6c  rary [%s]", zFil
28d11 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
28d12 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 70 56 66  te3OsDlError(pVf
28d13 73 2c 20 6e 4d 73 67 2d 31 2c 20 7a 45 72 72 6d  s, nMsg-1, zErrm
28d14 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a  sg);.        *pz
28d15 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
28d16 44 62 53 74 72 44 75 70 28 30 2c 20 7a 45 72 72  DbStrDup(0, zErr
28d17 6d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 73 71  msg);.        sq
28d18 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64  lite3StackFree(d
28d19 62 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  b, zErrmsg);.   
28d1a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72     }.    }.    r
28d1b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
28d1c 4f 52 3b 0a 20 20 7d 0a 20 20 78 49 6e 69 74 20  OR;.  }.  xInit 
28d1d 3d 20 28 69 6e 74 28 2a 29 28 73 71 6c 69 74 65  = (int(*)(sqlite
28d1e 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20  3*,char**,const 
28d1f 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
28d20 69 6e 65 73 2a 29 29 0a 20 20 20 20 20 20 20 20  ines*)).        
28d21 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28d22 65 33 4f 73 44 6c 53 79 6d 28 70 56 66 73 2c 20  e3OsDlSym(pVfs, 
28d23 68 61 6e 64 6c 65 2c 20 7a 50 72 6f 63 29 3b 0a  handle, zProc);.
28d24 20 20 69 66 28 20 78 49 6e 69 74 3d 3d 30 20 29    if( xInit==0 )
28d25 7b 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d  {.    if( pzErrM
28d26 73 67 20 29 7b 0a 20 20 20 20 20 20 7a 45 72 72  sg ){.      zErr
28d27 6d 73 67 20 3d 20 73 71 6c 69 74 65 33 53 74 61  msg = sqlite3Sta
28d28 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  ckAllocZero(db, 
28d29 6e 4d 73 67 29 3b 0a 20 20 20 20 20 20 69 66 28  nMsg);.      if(
28d2a 20 7a 45 72 72 6d 73 67 20 29 7b 0a 20 20 20 20   zErrmsg ){.    
28d2b 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
28d2c 69 6e 74 66 28 6e 4d 73 67 2c 20 7a 45 72 72 6d  intf(nMsg, zErrm
28d2d 73 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  sg,.            
28d2e 22 6e 6f 20 65 6e 74 72 79 20 70 6f 69 6e 74 20  "no entry point 
28d2f 5b 25 73 5d 20 69 6e 20 73 68 61 72 65 64 20 6c  [%s] in shared l
28d30 69 62 72 61 72 79 20 5b 25 73 5d 22 2c 20 7a 50  ibrary [%s]", zP
28d31 72 6f 63 2c 7a 46 69 6c 65 29 3b 0a 20 20 20 20  roc,zFile);.    
28d32 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 45      sqlite3OsDlE
28d33 72 72 6f 72 28 70 56 66 73 2c 20 6e 4d 73 67 2d  rror(pVfs, nMsg-
28d34 31 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  1, zErrmsg);.   
28d35 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
28d36 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
28d37 28 30 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  (0, zErrmsg);.  
28d38 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61        sqlite3Sta
28d39 63 6b 46 72 65 65 28 64 62 2c 20 7a 45 72 72 6d  ckFree(db, zErrm
28d3a 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  sg);.      }.   
28d3b 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c     sqlite3OsDlCl
28d3c 6f 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c 65  ose(pVfs, handle
28d3d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
28d3e 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
28d3f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 49  ;.  }else if( xI
28d40 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 6d 73 67  nit(db, &zErrmsg
28d41 2c 20 26 73 71 6c 69 74 65 33 41 70 69 73 29 20  , &sqlite3Apis) 
28d42 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72  ){.    if( pzErr
28d43 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a  Msg ){.      *pz
28d44 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
28d45 5f 6d 70 72 69 6e 74 66 28 22 65 72 72 6f 72 20  _mprintf("error 
28d46 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a  during initializ
28d47 61 74 69 6f 6e 3a 20 25 73 22 2c 20 7a 45 72 72  ation: %s", zErr
28d48 6d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  msg);.    }.    
28d49 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
28d4a 72 6d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74  rmsg);.    sqlit
28d4b 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66 73  e3OsDlClose(pVfs
28d4c 2c 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20 72  , handle);.    r
28d4d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
28d4e 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 70  OR;.  }..  /* Ap
28d4f 70 65 6e 64 20 74 68 65 20 6e 65 77 20 73 68 61  pend the new sha
28d50 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e 64  red library hand
28d51 6c 65 20 74 6f 20 74 68 65 20 64 62 2d 3e 61 45  le to the db->aE
28d52 78 74 65 6e 73 69 6f 6e 20 61 72 72 61 79 2e 20  xtension array. 
28d53 2a 2f 0a 20 20 61 48 61 6e 64 6c 65 20 3d 20 73  */.  aHandle = s
28d54 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
28d55 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 68 61  ro(db, sizeof(ha
28d56 6e 64 6c 65 29 2a 28 64 62 2d 3e 6e 45 78 74 65  ndle)*(db->nExte
28d57 6e 73 69 6f 6e 2b 31 29 29 3b 0a 20 20 69 66 28  nsion+1));.  if(
28d58 20 61 48 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20   aHandle==0 ){. 
28d59 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
28d5a 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66  _NOMEM;.  }.  if
28d5b 28 20 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e  ( db->nExtension
28d5c 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79  >0 ){.    memcpy
28d5d 28 61 48 61 6e 64 6c 65 2c 20 64 62 2d 3e 61 45  (aHandle, db->aE
28d5e 78 74 65 6e 73 69 6f 6e 2c 20 73 69 7a 65 6f 66  xtension, sizeof
28d5f 28 68 61 6e 64 6c 65 29 2a 64 62 2d 3e 6e 45 78  (handle)*db->nEx
28d60 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 7d 0a 20 20  tension);.  }.  
28d61 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
28d62 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  , db->aExtension
28d63 29 3b 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e 73  );.  db->aExtens
28d64 69 6f 6e 20 3d 20 61 48 61 6e 64 6c 65 3b 0a 0a  ion = aHandle;..
28d65 20 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e    db->aExtension
28d66 5b 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b  [db->nExtension+
28d67 2b 5d 20 3d 20 68 61 6e 64 6c 65 3b 0a 20 20 72  +] = handle;.  r
28d68 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
28d69 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
28d6a 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65  t sqlite3_load_e
28d6b 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69  xtension(.  sqli
28d6c 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
28d6d 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78    /* Load the ex
28d6e 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69  tension into thi
28d6f 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  s database conne
28d70 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
28d71 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20   char *zFile,   
28d72 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
28d73 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63  shared library c
28d74 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73  ontaining extens
28d75 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
28d76 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f  har *zProc,    /
28d77 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20  * Entry point.  
28d78 55 73 65 20 22 73 71 6c 69 74 65 33 5f 65 78 74  Use "sqlite3_ext
28d79 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 20 69 66 20  ension_init" if 
28d7a 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  0 */.  char **pz
28d7b 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20  ErrMsg       /* 
28d7c 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  Put error messag
28d7d 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20  e here if not 0 
28d7e 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
28d7f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
28d80 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
28d81 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
28d82 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28 64 62  LoadExtension(db
28d83 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20  , zFile, zProc, 
28d84 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72 63 20  pzErrMsg);.  rc 
28d85 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
28d86 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
28d87 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
28d88 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
28d89 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
28d8a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  * Call this rout
28d8b 69 6e 65 20 77 68 65 6e 20 74 68 65 20 64 61 74  ine when the dat
28d8c 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
28d8d 20 69 73 20 63 6c 6f 73 69 6e 67 20 69 6e 20 6f   is closing in o
28d8e 72 64 65 72 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e  rder.** to clean
28d8f 20 75 70 20 6c 6f 61 64 65 64 20 65 78 74 65 6e   up loaded exten
28d90 73 69 6f 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sions.*/.SQLITE_
28d91 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
28d92 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69  ite3CloseExtensi
28d93 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ons(sqlite3 *db)
28d94 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73  {.  int i;.  ass
28d95 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
28d96 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
28d97 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  x) );.  for(i=0;
28d98 20 69 3c 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f   i<db->nExtensio
28d99 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  n; i++){.    sql
28d9a 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 64 62  ite3OsDlClose(db
28d9b 2d 3e 70 56 66 73 2c 20 64 62 2d 3e 61 45 78 74  ->pVfs, db->aExt
28d9c 65 6e 73 69 6f 6e 5b 69 5d 29 3b 0a 20 20 7d 0a  ension[i]);.  }.
28d9d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
28d9e 64 62 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69  db, db->aExtensi
28d9f 6f 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e  on);.}../*.** En
28da0 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20  able or disable 
28da1 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  extension loadin
28da2 67 2e 20 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f  g.  Extension lo
28da3 61 64 69 6e 67 20 69 73 20 64 69 73 61 62 6c 65  ading is disable
28da4 64 20 62 79 0a 2a 2a 20 64 65 66 61 75 6c 74 20  d by.** default 
28da5 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f 70 65  so as not to ope
28da6 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c 65 73  n security holes
28da7 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c 69 63   in older applic
28da8 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ations..*/.SQLIT
28da9 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
28daa 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78  3_enable_load_ex
28dab 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65 33 20  tension(sqlite3 
28dac 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b  *db, int onoff){
28dad 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
28dae 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
28daf 29 3b 0a 20 20 69 66 28 20 6f 6e 6f 66 66 20 29  );.  if( onoff )
28db0 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  {.    db->flags 
28db1 7c 3d 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78  |= SQLITE_LoadEx
28db2 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 65 6c 73 65  tension;.  }else
28db3 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  {.    db->flags 
28db4 26 3d 20 7e 53 51 4c 49 54 45 5f 4c 6f 61 64 45  &= ~SQLITE_LoadE
28db5 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 0a 20 20  xtension;.  }.  
28db6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
28db7 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
28db8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
28db9 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  OK;.}..#endif /*
28dba 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
28dbb 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 0a  D_EXTENSION */..
28dbc 2f 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6f 2d 65  /*.** The auto-e
28dbd 78 74 65 6e 73 69 6f 6e 20 63 6f 64 65 20 61 64  xtension code ad
28dbe 64 65 64 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ded regardless o
28dbf 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  f whether or not
28dc0 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 6c 6f   extension.** lo
28dc1 61 64 69 6e 67 20 69 73 20 73 75 70 70 6f 72 74  ading is support
28dc2 65 64 2e 20 20 57 65 20 6e 65 65 64 20 61 20 64  ed.  We need a d
28dc3 75 6d 6d 79 20 73 71 6c 69 74 65 33 41 70 69 73  ummy sqlite3Apis
28dc4 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 61   pointer for tha
28dc5 74 0a 2a 2a 20 63 6f 64 65 20 69 66 20 72 65 67  t.** code if reg
28dc6 75 6c 61 72 20 65 78 74 65 6e 73 69 6f 6e 20 6c  ular extension l
28dc7 6f 61 64 69 6e 67 20 69 73 20 6e 6f 74 20 61 76  oading is not av
28dc8 61 69 6c 61 62 6c 65 2e 20 20 54 68 69 73 20 69  ailable.  This i
28dc9 73 20 74 68 61 74 0a 2a 2a 20 64 75 6d 6d 79 20  s that.** dummy 
28dca 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 23 69 66 64  pointer..*/.#ifd
28dcb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
28dcc 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 73 74  OAD_EXTENSION.st
28dcd 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
28dce 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
28dcf 73 71 6c 69 74 65 33 41 70 69 73 20 3d 20 7b 20  sqlite3Apis = { 
28dd0 30 20 7d 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  0 };.#endif.../*
28dd1 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
28dd2 67 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73 20 74  g object holds t
28dd3 68 65 20 6c 69 73 74 20 6f 66 20 61 75 74 6f 6d  he list of autom
28dd4 61 74 69 63 61 6c 6c 79 20 6c 6f 61 64 65 64 0a  atically loaded.
28dd5 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a  ** extensions..*
28dd6 2a 0a 2a 2a 20 54 68 69 73 20 6c 69 73 74 20 69  *.** This list i
28dd7 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20  s shared across 
28dd8 74 68 72 65 61 64 73 2e 20 20 54 68 65 20 53 51  threads.  The SQ
28dd9 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
28dda 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 6d 75 74 65  C_MASTER.** mute
28ddb 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77  x must be held w
28ddc 68 69 6c 65 20 61 63 63 65 73 73 69 6e 67 20 74  hile accessing t
28ddd 68 69 73 20 6c 69 73 74 2e 0a 2a 2f 0a 74 79 70  his list..*/.typ
28dde 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
28ddf 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74 20 73  te3AutoExtList s
28de0 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69 73  qlite3AutoExtLis
28de1 74 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45  t;.static SQLITE
28de2 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69  _WSD struct sqli
28de3 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74 20 7b  te3AutoExtList {
28de4 0a 20 20 69 6e 74 20 6e 45 78 74 3b 20 20 20 20  .  int nExt;    
28de5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
28de6 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
28de7 6e 20 61 45 78 74 5b 5d 20 2a 2f 20 20 20 20 20  n aExt[] */     
28de8 20 20 20 20 20 0a 20 20 76 6f 69 64 20 28 2a 2a       .  void (**
28de9 61 45 78 74 29 28 76 6f 69 64 29 3b 20 20 20 2f  aExt)(void);   /
28dea 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  * Pointers to th
28deb 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 69 74  e extension init
28dec 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 20   functions */.} 
28ded 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74 20 3d  sqlite3Autoext =
28dee 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54   { 0, 0 };../* T
28def 68 65 20 22 77 73 64 41 75 74 6f 65 78 74 22 20  he "wsdAutoext" 
28df0 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c  macro will resol
28df1 76 65 20 74 6f 20 74 68 65 20 61 75 74 6f 65 78  ve to the autoex
28df2 74 65 6e 73 69 6f 6e 0a 2a 2a 20 73 74 61 74 65  tension.** state
28df3 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69   vector.  If wri
28df4 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74  table static dat
28df5 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64  a is unsupported
28df6 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a   on the target,.
28df7 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f  ** we have to lo
28df8 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76  cate the state v
28df9 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d  ector at run-tim
28dfa 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20  e.  In the more 
28dfb 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77  common.** case w
28dfc 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74  here writable st
28dfd 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70  atic data is sup
28dfe 70 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 20  ported, wsdStat 
28dff 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74  can refer direct
28e00 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71  ly.** to the "sq
28e01 6c 69 74 65 33 41 75 74 6f 65 78 74 22 20 73 74  lite3Autoext" st
28e02 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61  ate vector decla
28e03 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69  red above..*/.#i
28e04 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
28e05 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73  _WSD.# define ws
28e06 64 41 75 74 6f 65 78 74 49 6e 69 74 20 5c 0a 20  dAutoextInit \. 
28e07 20 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c   sqlite3AutoExtL
28e08 69 73 74 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c  ist *x = &GLOBAL
28e09 28 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c  (sqlite3AutoExtL
28e0a 69 73 74 2c 73 71 6c 69 74 65 33 41 75 74 6f 65  ist,sqlite3Autoe
28e0b 78 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64  xt).# define wsd
28e0c 41 75 74 6f 65 78 74 20 78 5b 30 5d 0a 23 65 6c  Autoext x[0].#el
28e0d 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 41  se.# define wsdA
28e0e 75 74 6f 65 78 74 49 6e 69 74 0a 23 20 64 65 66  utoextInit.# def
28e0f 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 20 73  ine wsdAutoext s
28e10 71 6c 69 74 65 33 41 75 74 6f 65 78 74 0a 23 65  qlite3Autoext.#e
28e11 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  ndif.../*.** Reg
28e12 69 73 74 65 72 20 61 20 73 74 61 74 69 63 61 6c  ister a statical
28e13 6c 79 20 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73  ly linked extens
28e14 69 6f 6e 20 74 68 61 74 20 69 73 20 61 75 74 6f  ion that is auto
28e15 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 6c 6f 61  matically.** loa
28e16 64 65 64 20 62 79 20 65 76 65 72 79 20 6e 65 77  ded by every new
28e17 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
28e18 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
28e19 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
28e1a 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76  auto_extension(v
28e1b 6f 69 64 20 28 2a 78 49 6e 69 74 29 28 76 6f 69  oid (*xInit)(voi
28e1c 64 29 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  d)){.  int rc = 
28e1d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 6e 64  SQLITE_OK;.#ifnd
28e1e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
28e1f 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20 73  UTOINIT.  rc = s
28e20 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
28e21 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  e();.  if( rc ){
28e22 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
28e23 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
28e24 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 23 69   {.    int i;.#i
28e25 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
28e26 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  AFE.    sqlite3_
28e27 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73  mutex *mutex = s
28e28 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
28e29 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
28e2a 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65  ATIC_MASTER);.#e
28e2b 6e 64 69 66 0a 20 20 20 20 77 73 64 41 75 74 6f  ndif.    wsdAuto
28e2c 65 78 74 49 6e 69 74 3b 0a 20 20 20 20 73 71 6c  extInit;.    sql
28e2d 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
28e2e 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 66 6f 72  (mutex);.    for
28e2f 28 69 3d 30 3b 20 69 3c 77 73 64 41 75 74 6f 65  (i=0; i<wsdAutoe
28e30 78 74 2e 6e 45 78 74 3b 20 69 2b 2b 29 7b 0a 20  xt.nExt; i++){. 
28e31 20 20 20 20 20 69 66 28 20 77 73 64 41 75 74 6f       if( wsdAuto
28e32 65 78 74 2e 61 45 78 74 5b 69 5d 3d 3d 78 49 6e  ext.aExt[i]==xIn
28e33 69 74 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  it ) break;.    
28e34 7d 0a 20 20 20 20 69 66 28 20 69 3d 3d 77 73 64  }.    if( i==wsd
28e35 41 75 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0a  Autoext.nExt ){.
28e36 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20        int nByte 
28e37 3d 20 28 77 73 64 41 75 74 6f 65 78 74 2e 6e 45  = (wsdAutoext.nE
28e38 78 74 2b 31 29 2a 73 69 7a 65 6f 66 28 77 73 64  xt+1)*sizeof(wsd
28e39 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 30 5d 29  Autoext.aExt[0])
28e3a 3b 0a 20 20 20 20 20 20 76 6f 69 64 20 28 2a 2a  ;.      void (**
28e3b 61 4e 65 77 29 28 76 6f 69 64 29 3b 0a 20 20 20  aNew)(void);.   
28e3c 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65     aNew = sqlite
28e3d 33 5f 72 65 61 6c 6c 6f 63 28 77 73 64 41 75 74  3_realloc(wsdAut
28e3e 6f 65 78 74 2e 61 45 78 74 2c 20 6e 42 79 74 65  oext.aExt, nByte
28e3f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61 4e 65  );.      if( aNe
28e40 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  w==0 ){.        
28e41 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
28e42 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  M;.      }else{.
28e43 20 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65          wsdAutoe
28e44 78 74 2e 61 45 78 74 20 3d 20 61 4e 65 77 3b 0a  xt.aExt = aNew;.
28e45 20 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65          wsdAutoe
28e46 78 74 2e 61 45 78 74 5b 77 73 64 41 75 74 6f 65  xt.aExt[wsdAutoe
28e47 78 74 2e 6e 45 78 74 5d 20 3d 20 78 49 6e 69 74  xt.nExt] = xInit
28e48 3b 0a 20 20 20 20 20 20 20 20 77 73 64 41 75 74  ;.        wsdAut
28e49 6f 65 78 74 2e 6e 45 78 74 2b 2b 3b 0a 20 20 20  oext.nExt++;.   
28e4a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
28e4b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
28e4c 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 61  ve(mutex);.    a
28e4d 73 73 65 72 74 28 20 28 72 63 26 30 78 66 66 29  ssert( (rc&0xff)
28e4e 3d 3d 72 63 20 29 3b 0a 20 20 20 20 72 65 74 75  ==rc );.    retu
28e4f 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rn rc;.  }.}../*
28e50 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20 61 75  .** Reset the au
28e51 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
28e52 6e 20 6c 6f 61 64 69 6e 67 20 6d 65 63 68 61 6e  n loading mechan
28e53 69 73 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ism..*/.SQLITE_A
28e54 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
28e55 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e  reset_auto_exten
28e56 73 69 6f 6e 28 76 6f 69 64 29 7b 0a 23 69 66 6e  sion(void){.#ifn
28e57 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28e58 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73  AUTOINIT.  if( s
28e59 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
28e5a 65 28 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  e()==SQLITE_OK )
28e5b 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 23 69 66 20  .#endif.  {.#if 
28e5c 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
28e5d 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  E.    sqlite3_mu
28e5e 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c  tex *mutex = sql
28e5f 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
28e60 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
28e61 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64  IC_MASTER);.#end
28e62 69 66 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78  if.    wsdAutoex
28e63 74 49 6e 69 74 3b 0a 20 20 20 20 73 71 6c 69 74  tInit;.    sqlit
28e64 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
28e65 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74  utex);.    sqlit
28e66 65 33 5f 66 72 65 65 28 77 73 64 41 75 74 6f 65  e3_free(wsdAutoe
28e67 78 74 2e 61 45 78 74 29 3b 0a 20 20 20 20 77 73  xt.aExt);.    ws
28e68 64 41 75 74 6f 65 78 74 2e 61 45 78 74 20 3d 20  dAutoext.aExt = 
28e69 30 3b 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78  0;.    wsdAutoex
28e6a 74 2e 6e 45 78 74 20 3d 20 30 3b 0a 20 20 20 20  t.nExt = 0;.    
28e6b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
28e6c 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  ave(mutex);.  }.
28e6d 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 61 6c  }../*.** Load al
28e6e 6c 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  l automatic exte
28e6f 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  nsions..**.** If
28e70 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77   anything goes w
28e71 72 6f 6e 67 2c 20 73 65 74 20 61 6e 20 65 72 72  rong, set an err
28e72 6f 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  or in the databa
28e73 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
28e74 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
28e75 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
28e76 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28  oLoadExtensions(
28e77 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
28e78 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 67 6f 20  int i;.  int go 
28e79 3d 20 31 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e  = 1;.  int (*xIn
28e7a 69 74 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61  it)(sqlite3*,cha
28e7b 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65  r**,const sqlite
28e7c 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29  3_api_routines*)
28e7d 3b 0a 0a 20 20 77 73 64 41 75 74 6f 65 78 74 49  ;..  wsdAutoextI
28e7e 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 41 75  nit;.  if( wsdAu
28e7f 74 6f 65 78 74 2e 6e 45 78 74 3d 3d 30 20 29 7b  toext.nExt==0 ){
28e80 0a 20 20 20 20 2f 2a 20 43 6f 6d 6d 6f 6e 20 63  .    /* Common c
28e81 61 73 65 3a 20 65 61 72 6c 79 20 6f 75 74 20 77  ase: early out w
28e82 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61 76  ithout every hav
28e83 69 6e 67 20 74 6f 20 61 63 71 75 69 72 65 20 61  ing to acquire a
28e84 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 72 65   mutex */.    re
28e85 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
28e86 69 3d 30 3b 20 67 6f 3b 20 69 2b 2b 29 7b 0a 20  i=0; go; i++){. 
28e87 20 20 20 63 68 61 72 20 2a 7a 45 72 72 6d 73 67     char *zErrmsg
28e88 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ;.#if SQLITE_THR
28e89 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69  EADSAFE.    sqli
28e8a 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
28e8b 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
28e8c 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
28e8d 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
28e8e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c  ;.#endif.    sql
28e8f 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
28e90 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28  (mutex);.    if(
28e91 20 69 3e 3d 77 73 64 41 75 74 6f 65 78 74 2e 6e   i>=wsdAutoext.n
28e92 45 78 74 20 29 7b 0a 20 20 20 20 20 20 78 49 6e  Ext ){.      xIn
28e93 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f  it = 0;.      go
28e94 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
28e95 0a 20 20 20 20 20 20 78 49 6e 69 74 20 3d 20 28  .      xInit = (
28e96 69 6e 74 28 2a 29 28 73 71 6c 69 74 65 33 2a 2c  int(*)(sqlite3*,
28e97 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c  char**,const sql
28e98 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
28e99 73 2a 29 29 0a 20 20 20 20 20 20 20 20 20 20 20  s*)).           
28e9a 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45     wsdAutoext.aE
28e9b 78 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  xt[i];.    }.   
28e9c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
28e9d 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 20  eave(mutex);.   
28e9e 20 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a 20 20   zErrmsg = 0;.  
28e9f 20 20 69 66 28 20 78 49 6e 69 74 20 26 26 20 78    if( xInit && x
28ea0 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 6d 73  Init(db, &zErrms
28ea1 67 2c 20 26 73 71 6c 69 74 65 33 41 70 69 73 29  g, &sqlite3Apis)
28ea2 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
28ea3 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
28ea4 45 5f 45 52 52 4f 52 2c 0a 20 20 20 20 20 20 20  E_ERROR,.       
28ea5 20 20 20 20 20 22 61 75 74 6f 6d 61 74 69 63 20       "automatic 
28ea6 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  extension loadin
28ea7 67 20 66 61 69 6c 65 64 3a 20 25 73 22 2c 20 7a  g failed: %s", z
28ea8 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 67  Errmsg);.      g
28ea9 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  o = 0;.    }.   
28eaa 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
28eab 72 72 6d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  rrmsg);.  }.}../
28eac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
28ead 6e 64 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63 20  nd of loadext.c 
28eae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28eaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
28eb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
28eb2 65 67 69 6e 20 66 69 6c 65 20 70 72 61 67 6d 61  egin file pragma
28eb3 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
28eb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28eb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
28eb6 2a 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20  *.** 2003 April 
28eb7 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
28eb8 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
28eb9 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
28eba 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
28ebb 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
28ebc 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
28ebd 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
28ebe 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
28ebf 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
28ec0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
28ec1 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
28ec2 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
28ec3 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
28ec4 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
28ec5 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
28ec6 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
28ec7 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
28ec8 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
28ec9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28eca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ecb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ecc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ecd 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
28ece 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64  ntains code used
28ecf 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
28ed0 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64  e PRAGMA command
28ed1 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 61  ..**.** $Id: pra
28ed2 67 6d 61 2e 63 2c 76 20 31 2e 32 31 34 20 32 30  gma.c,v 1.214 20
28ed3 30 39 2f 30 37 2f 30 32 20 30 37 3a 34 37 3a 33  09/07/02 07:47:3
28ed4 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  3 danielk1977 Ex
28ed5 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 49 67 6e 6f 72  p $.*/../* Ignor
28ed6 65 20 74 68 69 73 20 77 68 6f 6c 65 20 66 69 6c  e this whole fil
28ed7 65 20 69 66 20 70 72 61 67 6d 61 73 20 61 72 65  e if pragmas are
28ed8 20 64 69 73 61 62 6c 65 64 0a 2a 2f 0a 23 69 66   disabled.*/.#if
28ed9 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
28eda 5f 4f 4d 49 54 5f 50 52 41 47 4d 41 29 0a 0a 2f  _OMIT_PRAGMA)../
28edb 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
28edc 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20  he given string 
28edd 61 73 20 61 20 73 61 66 65 74 79 20 6c 65 76 65  as a safety leve
28ede 6c 2e 20 20 52 65 74 75 72 6e 20 30 20 66 6f 72  l.  Return 0 for
28edf 20 4f 46 46 2c 0a 2a 2a 20 31 20 66 6f 72 20 4f   OFF,.** 1 for O
28ee0 4e 20 6f 72 20 4e 4f 52 4d 41 4c 20 61 6e 64 20  N or NORMAL and 
28ee1 32 20 66 6f 72 20 46 55 4c 4c 2e 20 20 52 65 74  2 for FULL.  Ret
28ee2 75 72 6e 20 31 20 66 6f 72 20 61 6e 20 65 6d 70  urn 1 for an emp
28ee3 74 79 20 6f 72 20 0a 2a 2a 20 75 6e 72 65 63 6f  ty or .** unreco
28ee4 67 6e 69 7a 65 64 20 73 74 72 69 6e 67 20 61 72  gnized string ar
28ee5 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  gument..**.** No
28ee6 74 65 20 74 68 61 74 20 74 68 65 20 76 61 6c 75  te that the valu
28ee7 65 73 20 72 65 74 75 72 6e 65 64 20 61 72 65 20  es returned are 
28ee8 6f 6e 65 20 6c 65 73 73 20 74 68 61 74 20 74 68  one less that th
28ee9 65 20 76 61 6c 75 65 73 20 74 68 61 74 0a 2a 2a  e values that.**
28eea 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65   should be passe
28eeb 64 20 69 6e 74 6f 20 73 71 6c 69 74 65 33 42 74  d into sqlite3Bt
28eec 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65  reeSetSafetyLeve
28eed 6c 28 29 2e 20 20 54 68 65 20 69 73 20 64 6f 6e  l().  The is don
28eee 65 0a 2a 2a 20 74 6f 20 73 75 70 70 6f 72 74 20  e.** to support 
28eef 6c 65 67 61 63 79 20 53 51 4c 20 63 6f 64 65 2e  legacy SQL code.
28ef0 20 20 54 68 65 20 73 61 66 65 74 79 20 6c 65 76    The safety lev
28ef1 65 6c 20 75 73 65 64 20 74 6f 20 62 65 20 62 6f  el used to be bo
28ef2 6f 6c 65 61 6e 0a 2a 2a 20 61 6e 64 20 6f 6c 64  olean.** and old
28ef3 65 72 20 73 63 72 69 70 74 73 20 6d 61 79 20 68  er scripts may h
28ef4 61 76 65 20 75 73 65 64 20 6e 75 6d 62 65 72 73  ave used numbers
28ef5 20 30 20 66 6f 72 20 4f 46 46 20 61 6e 64 20 31   0 for OFF and 1
28ef6 20 66 6f 72 20 4f 4e 2e 0a 2a 2f 0a 73 74 61 74   for ON..*/.stat
28ef7 69 63 20 75 38 20 67 65 74 53 61 66 65 74 79 4c  ic u8 getSafetyL
28ef8 65 76 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20  evel(const char 
28ef9 2a 7a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  *z){.           
28efa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28efb 20 20 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31    /* 123456789 1
28efc 32 33 34 35 36 37 38 39 20 2a 2f 0a 20 20 73 74  23456789 */.  st
28efd 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
28efe 7a 54 65 78 74 5b 5d 20 3d 20 22 6f 6e 6f 66 66  zText[] = "onoff
28eff 61 6c 73 65 79 65 73 74 72 75 65 66 75 6c 6c 22  alseyestruefull"
28f00 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
28f01 20 75 38 20 69 4f 66 66 73 65 74 5b 5d 20 3d 20   u8 iOffset[] = 
28f02 7b 30 2c 20 31 2c 20 32 2c 20 34 2c 20 39 2c 20  {0, 1, 2, 4, 9, 
28f03 31 32 2c 20 31 36 7d 3b 0a 20 20 73 74 61 74 69  12, 16};.  stati
28f04 63 20 63 6f 6e 73 74 20 75 38 20 69 4c 65 6e 67  c const u8 iLeng
28f05 74 68 5b 5d 20 3d 20 7b 32 2c 20 32 2c 20 33 2c  th[] = {2, 2, 3,
28f06 20 35 2c 20 33 2c 20 34 2c 20 34 7d 3b 0a 20 20   5, 3, 4, 4};.  
28f07 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
28f08 69 56 61 6c 75 65 5b 5d 20 3d 20 20 7b 31 2c 20  iValue[] =  {1, 
28f09 30 2c 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 32  0, 0, 0, 1, 1, 2
28f0a 7d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20  };.  int i, n;. 
28f0b 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 64 69   if( sqlite3Isdi
28f0c 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 72  git(*z) ){.    r
28f0d 65 74 75 72 6e 20 28 75 38 29 61 74 6f 69 28 7a  eturn (u8)atoi(z
28f0e 29 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c  );.  }.  n = sql
28f0f 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b  ite3Strlen30(z);
28f10 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72  .  for(i=0; i<Ar
28f11 72 61 79 53 69 7a 65 28 69 4c 65 6e 67 74 68 29  raySize(iLength)
28f12 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
28f13 69 4c 65 6e 67 74 68 5b 69 5d 3d 3d 6e 20 26 26  iLength[i]==n &&
28f14 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
28f15 28 26 7a 54 65 78 74 5b 69 4f 66 66 73 65 74 5b  (&zText[iOffset[
28f16 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0a 20  i]],z,n)==0 ){. 
28f17 20 20 20 20 20 72 65 74 75 72 6e 20 69 56 61 6c       return iVal
28f18 75 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  ue[i];.    }.  }
28f19 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
28f1a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  /*.** Interpret 
28f1b 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
28f1c 20 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61   as a boolean va
28f1d 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75  lue..*/.static u
28f1e 38 20 67 65 74 42 6f 6f 6c 65 61 6e 28 63 6f 6e  8 getBoolean(con
28f1f 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72  st char *z){.  r
28f20 65 74 75 72 6e 20 67 65 74 53 61 66 65 74 79 4c  eturn getSafetyL
28f21 65 76 65 6c 28 7a 29 26 31 3b 0a 7d 0a 0a 2f 2a  evel(z)&1;.}../*
28f22 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68  .** Interpret th
28f23 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 61  e given string a
28f24 73 20 61 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65  s a locking mode
28f25 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
28f26 63 20 69 6e 74 20 67 65 74 4c 6f 63 6b 69 6e 67  c int getLocking
28f27 4d 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20  Mode(const char 
28f28 2a 7a 29 7b 0a 20 20 69 66 28 20 7a 20 29 7b 0a  *z){.  if( z ){.
28f29 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74      if( 0==sqlit
28f2a 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 65 78  e3StrICmp(z, "ex
28f2b 63 6c 75 73 69 76 65 22 29 20 29 20 72 65 74 75  clusive") ) retu
28f2c 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  rn PAGER_LOCKING
28f2d 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3b 0a  MODE_EXCLUSIVE;.
28f2e 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74      if( 0==sqlit
28f2f 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 6e 6f  e3StrICmp(z, "no
28f30 72 6d 61 6c 22 29 20 29 20 72 65 74 75 72 6e 20  rmal") ) return 
28f31 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
28f32 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20  E_NORMAL;.  }.  
28f33 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43  return PAGER_LOC
28f34 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 3b 0a  KINGMODE_QUERY;.
28f35 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
28f36 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
28f37 4d 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  M./*.** Interpre
28f38 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  t the given stri
28f39 6e 67 20 61 73 20 61 6e 20 61 75 74 6f 2d 76 61  ng as an auto-va
28f3a 63 75 75 6d 20 6d 6f 64 65 20 76 61 6c 75 65 2e  cuum mode value.
28f3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
28f3c 77 69 6e 67 20 73 74 72 69 6e 67 73 2c 20 22 6e  wing strings, "n
28f3d 6f 6e 65 22 2c 20 22 66 75 6c 6c 22 20 61 6e 64  one", "full" and
28f3e 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 20 61   "incremental" a
28f3f 72 65 20 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c  re .** acceptabl
28f40 65 2c 20 61 73 20 61 72 65 20 74 68 65 69 72 20  e, as are their 
28f41 6e 75 6d 65 72 69 63 20 65 71 75 69 76 61 6c 65  numeric equivale
28f42 6e 74 73 3a 20 30 2c 20 31 20 61 6e 64 20 32 20  nts: 0, 1 and 2 
28f43 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f  respectively..*/
28f44 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 41  .static int getA
28f45 75 74 6f 56 61 63 75 75 6d 28 63 6f 6e 73 74 20  utoVacuum(const 
28f46 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  char *z){.  int 
28f47 69 3b 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69  i;.  if( 0==sqli
28f48 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 6e  te3StrICmp(z, "n
28f49 6f 6e 65 22 29 20 29 20 72 65 74 75 72 6e 20 42  one") ) return B
28f4a 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
28f4b 4e 4f 4e 45 3b 0a 20 20 69 66 28 20 30 3d 3d 73  NONE;.  if( 0==s
28f4c 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c  qlite3StrICmp(z,
28f4d 20 22 66 75 6c 6c 22 29 20 29 20 72 65 74 75 72   "full") ) retur
28f4e 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55  n BTREE_AUTOVACU
28f4f 55 4d 5f 46 55 4c 4c 3b 0a 20 20 69 66 28 20 30  UM_FULL;.  if( 0
28f50 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
28f51 28 7a 2c 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c  (z, "incremental
28f52 22 29 20 29 20 72 65 74 75 72 6e 20 42 54 52 45  ") ) return BTRE
28f53 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43  E_AUTOVACUUM_INC
28f54 52 3b 0a 20 20 69 20 3d 20 61 74 6f 69 28 7a 29  R;.  i = atoi(z)
28f55 3b 0a 20 20 72 65 74 75 72 6e 20 28 75 38 29 28  ;.  return (u8)(
28f56 28 69 3e 3d 30 26 26 69 3c 3d 32 29 3f 69 3a 30  (i>=0&&i<=2)?i:0
28f57 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69  );.}.#endif /* i
28f58 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28f59 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a  T_AUTOVACUUM */.
28f5a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28f5b 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
28f5c 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72  AS./*.** Interpr
28f5d 65 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72  et the given str
28f5e 69 6e 67 20 61 73 20 61 20 74 65 6d 70 20 64 62  ing as a temp db
28f5f 20 6c 6f 63 61 74 69 6f 6e 2e 20 52 65 74 75 72   location. Retur
28f60 6e 20 31 20 66 6f 72 20 66 69 6c 65 0a 2a 2a 20  n 1 for file.** 
28f61 62 61 63 6b 65 64 20 74 65 6d 70 6f 72 61 72 79  backed temporary
28f62 20 64 61 74 61 62 61 73 65 73 2c 20 32 20 66 6f   databases, 2 fo
28f63 72 20 74 68 65 20 52 65 64 2d 42 6c 61 63 6b 20  r the Red-Black 
28f64 74 72 65 65 20 69 6e 20 6d 65 6d 6f 72 79 20 64  tree in memory d
28f65 61 74 61 62 61 73 65 0a 2a 2a 20 61 6e 64 20 30  atabase.** and 0
28f66 20 74 6f 20 75 73 65 20 74 68 65 20 63 6f 6d 70   to use the comp
28f67 69 6c 65 2d 74 69 6d 65 20 64 65 66 61 75 6c 74  ile-time default
28f68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
28f69 67 65 74 54 65 6d 70 53 74 6f 72 65 28 63 6f 6e  getTempStore(con
28f6a 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
28f6b 66 28 20 7a 5b 30 5d 3e 3d 27 30 27 20 26 26 20  f( z[0]>='0' && 
28f6c 7a 5b 30 5d 3c 3d 27 32 27 20 29 7b 0a 20 20 20  z[0]<='2' ){.   
28f6d 20 72 65 74 75 72 6e 20 7a 5b 30 5d 20 2d 20 27   return z[0] - '
28f6e 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  0';.  }else if( 
28f6f 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
28f70 2c 20 22 66 69 6c 65 22 29 3d 3d 30 20 29 7b 0a  , "file")==0 ){.
28f71 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
28f72 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65  }else if( sqlite
28f73 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 6d 65 6d  3StrICmp(z, "mem
28f74 6f 72 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ory")==0 ){.    
28f75 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 65 6c 73  return 2;.  }els
28f76 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  e{.    return 0;
28f77 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
28f78 20 53 51 4c 49 54 45 5f 50 41 47 45 52 5f 50 52   SQLITE_PAGER_PR
28f79 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65  AGMAS */..#ifnde
28f7a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  f SQLITE_OMIT_PA
28f7b 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a  GER_PRAGMAS./*.*
28f7c 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 65 6d  * Invalidate tem
28f7d 70 20 73 74 6f 72 61 67 65 2c 20 65 69 74 68 65  p storage, eithe
28f7e 72 20 77 68 65 6e 20 74 68 65 20 74 65 6d 70 20  r when the temp 
28f7f 73 74 6f 72 61 67 65 20 69 73 20 63 68 61 6e 67  storage is chang
28f80 65 64 0a 2a 2a 20 66 72 6f 6d 20 64 65 66 61 75  ed.** from defau
28f81 6c 74 2c 20 6f 72 20 77 68 65 6e 20 27 66 69 6c  lt, or when 'fil
28f82 65 27 20 61 6e 64 20 74 68 65 20 74 65 6d 70 5f  e' and the temp_
28f83 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20  store_directory 
28f84 68 61 73 20 63 68 61 6e 67 65 64 0a 2a 2f 0a 73  has changed.*/.s
28f85 74 61 74 69 63 20 69 6e 74 20 69 6e 76 61 6c 69  tatic int invali
28f86 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28  dateTempStorage(
28f87 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
28f88 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
28f89 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
28f8a 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74  ( db->aDb[1].pBt
28f8b 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21  !=0 ){.    if( !
28f8c 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 7c  db->autoCommit |
28f8d 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  | sqlite3BtreeIs
28f8e 49 6e 52 65 61 64 54 72 61 6e 73 28 64 62 2d 3e  InReadTrans(db->
28f8f 61 44 62 5b 31 5d 2e 70 42 74 29 20 29 7b 0a 20  aDb[1].pBt) ){. 
28f90 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
28f91 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 65  rMsg(pParse, "te
28f92 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 20  mporary storage 
28f93 63 61 6e 6e 6f 74 20 62 65 20 63 68 61 6e 67 65  cannot be change
28f94 64 20 22 0a 20 20 20 20 20 20 20 20 22 66 72 6f  d ".        "fro
28f95 6d 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73  m within a trans
28f96 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20  action");.      
28f97 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
28f98 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ROR;.    }.    s
28f99 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
28f9a 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29  (db->aDb[1].pBt)
28f9b 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 5b 31 5d  ;.    db->aDb[1]
28f9c 2e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 73 71  .pBt = 0;.    sq
28f9d 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
28f9e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b  alSchema(db, 0);
28f9f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
28fa0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
28fa1 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45  f /* SQLITE_PAGE
28fa2 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69  R_PRAGMAS */..#i
28fa3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28fa4 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a  T_PAGER_PRAGMAS.
28fa5 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 45 4d  /*.** If the TEM
28fa6 50 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  P database is op
28fa7 65 6e 2c 20 63 6c 6f 73 65 20 69 74 20 61 6e 64  en, close it and
28fa8 20 6d 61 72 6b 20 74 68 65 20 64 61 74 61 62 61   mark the databa
28fa9 73 65 20 73 63 68 65 6d 61 0a 2a 2a 20 61 73 20  se schema.** as 
28faa 6e 65 65 64 69 6e 67 20 72 65 6c 6f 61 64 69 6e  needing reloadin
28fab 67 2e 20 20 54 68 69 73 20 6d 75 73 74 20 62 65  g.  This must be
28fac 20 64 6f 6e 65 20 77 68 65 6e 20 75 73 69 6e 67   done when using
28fad 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 4d 50   the SQLITE_TEMP
28fae 5f 53 54 4f 52 45 0a 2a 2a 20 6f 72 20 44 45 46  _STORE.** or DEF
28faf 41 55 4c 54 5f 54 45 4d 50 5f 53 54 4f 52 45 20  AULT_TEMP_STORE 
28fb0 70 72 61 67 6d 61 73 2e 0a 2a 2f 0a 73 74 61 74  pragmas..*/.stat
28fb1 69 63 20 69 6e 74 20 63 68 61 6e 67 65 54 65 6d  ic int changeTem
28fb2 70 53 74 6f 72 61 67 65 28 50 61 72 73 65 20 2a  pStorage(Parse *
28fb3 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
28fb4 61 72 20 2a 7a 53 74 6f 72 61 67 65 54 79 70 65  ar *zStorageType
28fb5 29 7b 0a 20 20 69 6e 74 20 74 73 20 3d 20 67 65  ){.  int ts = ge
28fb6 74 54 65 6d 70 53 74 6f 72 65 28 7a 53 74 6f 72  tTempStore(zStor
28fb7 61 67 65 54 79 70 65 29 3b 0a 20 20 73 71 6c 69  ageType);.  sqli
28fb8 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
28fb9 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e  ->db;.  if( db->
28fba 74 65 6d 70 5f 73 74 6f 72 65 3d 3d 74 73 20 29  temp_store==ts )
28fbb 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
28fbc 4b 3b 0a 20 20 69 66 28 20 69 6e 76 61 6c 69 64  K;.  if( invalid
28fbd 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 20  ateTempStorage( 
28fbe 70 50 61 72 73 65 20 29 20 21 3d 20 53 51 4c 49  pParse ) != SQLI
28fbf 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
28fc0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
28fc1 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 74 65 6d 70  ;.  }.  db->temp
28fc2 5f 73 74 6f 72 65 20 3d 20 28 75 38 29 74 73 3b  _store = (u8)ts;
28fc3 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
28fc4 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  _OK;.}.#endif /*
28fc5 20 53 51 4c 49 54 45 5f 50 41 47 45 52 5f 50 52   SQLITE_PAGER_PR
28fc6 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  AGMAS */../*.** 
28fc7 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
28fc8 20 72 65 74 75 72 6e 20 61 20 73 69 6e 67 6c 65   return a single
28fc9 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a   integer value..
28fca 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
28fcb 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 50  eturnSingleInt(P
28fcc 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
28fcd 6e 73 74 20 63 68 61 72 20 2a 7a 4c 61 62 65 6c  nst char *zLabel
28fce 2c 20 69 36 34 20 76 61 6c 75 65 29 7b 0a 20 20  , i64 value){.  
28fcf 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
28fd0 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
28fd1 3b 0a 20 20 69 6e 74 20 6d 65 6d 20 3d 20 2b 2b  ;.  int mem = ++
28fd2 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
28fd3 69 36 34 20 2a 70 49 36 34 20 3d 20 73 71 6c 69  i64 *pI64 = sqli
28fd4 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
28fd5 50 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f  Parse->db, sizeo
28fd6 66 28 76 61 6c 75 65 29 29 3b 0a 20 20 69 66 28  f(value));.  if(
28fd7 20 70 49 36 34 20 29 7b 0a 20 20 20 20 6d 65 6d   pI64 ){.    mem
28fd8 63 70 79 28 70 49 36 34 2c 20 26 76 61 6c 75 65  cpy(pI64, &value
28fd9 2c 20 73 69 7a 65 6f 66 28 76 61 6c 75 65 29 29  , sizeof(value))
28fda 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
28fdb 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
28fdc 49 6e 74 36 34 2c 20 30 2c 20 6d 65 6d 2c 20 30  Int64, 0, mem, 0
28fdd 2c 20 28 63 68 61 72 2a 29 70 49 36 34 2c 20 50  , (char*)pI64, P
28fde 34 5f 49 4e 54 36 34 29 3b 0a 20 20 73 71 6c 69  4_INT64);.  sqli
28fdf 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
28fe0 73 28 76 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74  s(v, 1);.  sqlit
28fe1 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
28fe2 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
28fe3 41 4d 45 2c 20 7a 4c 61 62 65 6c 2c 20 53 51 4c  AME, zLabel, SQL
28fe4 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73  ITE_STATIC);.  s
28fe5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28fe6 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
28fe7 2c 20 6d 65 6d 2c 20 31 29 3b 0a 7d 0a 0a 23 69  , mem, 1);.}..#i
28fe8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28fe9 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 2f  T_FLAG_PRAGMAS./
28fea 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65  *.** Check to se
28feb 65 20 69 66 20 7a 52 69 67 68 74 20 61 6e 64 20  e if zRight and 
28fec 7a 4c 65 66 74 20 72 65 66 65 72 20 74 6f 20 61  zLeft refer to a
28fed 20 70 72 61 67 6d 61 20 74 68 61 74 20 71 75 65   pragma that que
28fee 72 69 65 73 0a 2a 2a 20 6f 72 20 63 68 61 6e 67  ries.** or chang
28fef 65 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c  es one of the fl
28ff0 61 67 73 20 69 6e 20 64 62 2d 3e 66 6c 61 67 73  ags in db->flags
28ff1 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 73  .  Return 1 if s
28ff2 6f 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a  o and 0 if not..
28ff3 2a 2a 20 41 6c 73 6f 2c 20 69 6d 70 6c 65 6d 65  ** Also, impleme
28ff4 6e 74 20 74 68 65 20 70 72 61 67 6d 61 2e 0a 2a  nt the pragma..*
28ff5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61  /.static int fla
28ff6 67 50 72 61 67 6d 61 28 50 61 72 73 65 20 2a 70  gPragma(Parse *p
28ff7 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
28ff8 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20  r *zLeft, const 
28ff9 63 68 61 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20  char *zRight){. 
28ffa 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74   static const st
28ffb 72 75 63 74 20 73 50 72 61 67 6d 61 54 79 70 65  ruct sPragmaType
28ffc 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61   {.    const cha
28ffd 72 20 2a 7a 4e 61 6d 65 3b 20 20 2f 2a 20 4e 61  r *zName;  /* Na
28ffe 6d 65 20 6f 66 20 74 68 65 20 70 72 61 67 6d 61  me of the pragma
28fff 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61 73 6b   */.    int mask
29000 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ;           /* M
29001 61 73 6b 20 66 6f 72 20 74 68 65 20 64 62 2d 3e  ask for the db->
29002 66 6c 61 67 73 20 76 61 6c 75 65 20 2a 2f 0a 20  flags value */. 
29003 20 7d 20 61 50 72 61 67 6d 61 5b 5d 20 3d 20 7b   } aPragma[] = {
29004 0a 20 20 20 20 7b 20 22 66 75 6c 6c 5f 63 6f 6c  .    { "full_col
29005 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20 20 20 20  umn_names",     
29006 20 20 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f     SQLITE_FullCo
29007 6c 4e 61 6d 65 73 20 20 7d 2c 0a 20 20 20 20 7b  lNames  },.    {
29008 20 22 73 68 6f 72 74 5f 63 6f 6c 75 6d 6e 5f 6e   "short_column_n
29009 61 6d 65 73 22 2c 20 20 20 20 20 20 20 53 51 4c  ames",       SQL
2900a 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65  ITE_ShortColName
2900b 73 20 7d 2c 0a 20 20 20 20 7b 20 22 63 6f 75 6e  s },.    { "coun
2900c 74 5f 63 68 61 6e 67 65 73 22 2c 20 20 20 20 20  t_changes",     
2900d 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 6f         SQLITE_Co
2900e 75 6e 74 52 6f 77 73 20 20 20 20 20 7d 2c 0a 20  untRows     },. 
2900f 20 20 20 7b 20 22 65 6d 70 74 79 5f 72 65 73 75     { "empty_resu
29010 6c 74 5f 63 61 6c 6c 62 61 63 6b 73 22 2c 20 20  lt_callbacks",  
29011 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c   SQLITE_NullCall
29012 62 61 63 6b 20 20 7d 2c 0a 20 20 20 20 7b 20 22  back  },.    { "
29013 6c 65 67 61 63 79 5f 66 69 6c 65 5f 66 6f 72 6d  legacy_file_form
29014 61 74 22 2c 20 20 20 20 20 20 20 53 51 4c 49 54  at",       SQLIT
29015 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 20  E_LegacyFileFmt 
29016 7d 2c 0a 20 20 20 20 7b 20 22 66 75 6c 6c 66 73  },.    { "fullfs
29017 79 6e 63 22 2c 20 20 20 20 20 20 20 20 20 20 20  ync",           
29018 20 20 20 20 20 53 51 4c 49 54 45 5f 46 75 6c 6c       SQLITE_Full
29019 46 53 79 6e 63 20 20 20 20 20 7d 2c 0a 20 20 20  FSync     },.   
2901a 20 7b 20 22 72 65 76 65 72 73 65 5f 75 6e 6f 72   { "reverse_unor
2901b 64 65 72 65 64 5f 73 65 6c 65 63 74 73 22 2c 20  dered_selects", 
2901c 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72  SQLITE_ReverseOr
2901d 64 65 72 20 20 7d 2c 0a 23 69 66 64 65 66 20 53  der  },.#ifdef S
2901e 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
2901f 7b 20 22 73 71 6c 5f 74 72 61 63 65 22 2c 20 20  { "sql_trace",  
29020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
29021 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20  LITE_SqlTrace   
29022 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76 64 62     },.    { "vdb
29023 65 5f 6c 69 73 74 69 6e 67 22 2c 20 20 20 20 20  e_listing",     
29024 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 56          SQLITE_V
29025 64 62 65 4c 69 73 74 69 6e 67 20 20 20 7d 2c 0a  dbeListing   },.
29026 20 20 20 20 7b 20 22 76 64 62 65 5f 74 72 61 63      { "vdbe_trac
29027 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  e",             
29028 20 20 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61    SQLITE_VdbeTra
29029 63 65 20 20 20 20 20 7d 2c 0a 23 65 6e 64 69 66  ce     },.#endif
2902a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2902b 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 7b  OMIT_CHECK.    {
2902c 20 22 69 67 6e 6f 72 65 5f 63 68 65 63 6b 5f 63   "ignore_check_c
2902d 6f 6e 73 74 72 61 69 6e 74 73 22 2c 20 53 51 4c  onstraints", SQL
2902e 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73  ITE_IgnoreChecks
2902f 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20    },.#endif.    
29030 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
29031 20 69 73 20 56 45 52 59 20 65 78 70 65 72 69 6d   is VERY experim
29032 65 6e 74 61 6c 20 2a 2f 0a 20 20 20 20 7b 20 22  ental */.    { "
29033 77 72 69 74 61 62 6c 65 5f 73 63 68 65 6d 61 22  writable_schema"
29034 2c 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  ,          SQLIT
29035 45 5f 57 72 69 74 65 53 63 68 65 6d 61 7c 53 51  E_WriteSchema|SQ
29036 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64  LITE_RecoveryMod
29037 65 20 7d 2c 0a 20 20 20 20 7b 20 22 6f 6d 69 74  e },.    { "omit
29038 5f 72 65 61 64 6c 6f 63 6b 22 2c 20 20 20 20 20  _readlock",     
29039 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4e 6f         SQLITE_No
2903a 52 65 61 64 6c 6f 63 6b 20 20 20 20 7d 2c 0a 0a  Readlock    },..
2903b 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 4d 61 79      /* TODO: May
2903c 62 65 20 69 74 20 73 68 6f 75 6c 64 6e 27 74 20  be it shouldn't 
2903d 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63  be possible to c
2903e 68 61 6e 67 65 20 74 68 65 20 52 65 61 64 55 6e  hange the ReadUn
2903f 63 6f 6d 6d 69 74 74 65 64 0a 20 20 20 20 2a 2a  committed.    **
29040 20 66 6c 61 67 20 69 66 20 74 68 65 72 65 20 61   flag if there a
29041 72 65 20 61 6e 79 20 61 63 74 69 76 65 20 73 74  re any active st
29042 61 74 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 20  atements. */.   
29043 20 7b 20 22 72 65 61 64 5f 75 6e 63 6f 6d 6d 69   { "read_uncommi
29044 74 74 65 64 22 2c 20 20 20 20 20 20 20 20 20 53  tted",         S
29045 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d  QLITE_ReadUncomm
29046 69 74 74 65 64 20 7d 2c 0a 20 20 20 20 7b 20 22  itted },.    { "
29047 72 65 63 75 72 73 69 76 65 5f 74 72 69 67 67 65  recursive_trigge
29048 72 73 22 2c 20 20 20 20 20 20 20 53 51 4c 49 54  rs",       SQLIT
29049 45 5f 52 65 63 54 72 69 67 67 65 72 73 20 7d 2c  E_RecTriggers },
2904a 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66 6c  ..    /* This fl
2904b 61 67 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 73  ag may only be s
2904c 65 74 20 69 66 20 62 6f 74 68 20 66 6f 72 65 69  et if both forei
2904d 67 6e 2d 6b 65 79 20 61 6e 64 20 74 72 69 67 67  gn-key and trigg
2904e 65 72 20 73 75 70 70 6f 72 74 0a 20 20 20 20 2a  er support.    *
2904f 2a 20 61 72 65 20 70 72 65 73 65 6e 74 20 69 6e  * are present in
29050 20 74 68 65 20 62 75 69 6c 64 2e 20 20 2a 2f 0a   the build.  */.
29051 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
29052 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
29053 5f 4b 45 59 29 20 26 26 20 21 64 65 66 69 6e 65  _KEY) && !define
29054 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
29055 49 47 47 45 52 29 0a 20 20 20 20 7b 20 22 66 6f  IGGER).    { "fo
29056 72 65 69 67 6e 5f 6b 65 79 73 22 2c 20 20 20 20  reign_keys",    
29057 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
29058 46 6f 72 65 69 67 6e 4b 65 79 73 20 7d 2c 0a 23  ForeignKeys },.#
29059 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20 69 6e 74  endif.  };.  int
2905a 20 69 3b 0a 20 20 63 6f 6e 73 74 20 73 74 72 75   i;.  const stru
2905b 63 74 20 73 50 72 61 67 6d 61 54 79 70 65 20 2a  ct sPragmaType *
2905c 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d  p;.  for(i=0, p=
2905d 61 50 72 61 67 6d 61 3b 20 69 3c 41 72 72 61 79  aPragma; i<Array
2905e 53 69 7a 65 28 61 50 72 61 67 6d 61 29 3b 20 69  Size(aPragma); i
2905f 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, p++){.    if
29060 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
29061 28 7a 4c 65 66 74 2c 20 70 2d 3e 7a 4e 61 6d 65  (zLeft, p->zName
29062 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )==0 ){.      sq
29063 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
29064 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 56 64  se->db;.      Vd
29065 62 65 20 2a 76 3b 0a 20 20 20 20 20 20 76 20 3d  be *v;.      v =
29066 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
29067 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 61  pParse);.      a
29068 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 20 20  ssert( v!=0 );  
29069 2f 2a 20 41 6c 72 65 61 64 79 20 61 6c 6c 6f 63  /* Already alloc
2906a 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 50  ated by sqlite3P
2906b 72 61 67 6d 61 28 29 20 2a 2f 0a 20 20 20 20 20  ragma() */.     
2906c 20 69 66 28 20 41 4c 57 41 59 53 28 76 29 20 29   if( ALWAYS(v) )
2906d 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 52  {.        if( zR
2906e 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ight==0 ){.     
2906f 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c       returnSingl
29070 65 49 6e 74 28 70 50 61 72 73 65 2c 20 70 2d 3e  eInt(pParse, p->
29071 7a 4e 61 6d 65 2c 20 28 64 62 2d 3e 66 6c 61 67  zName, (db->flag
29072 73 20 26 20 70 2d 3e 6d 61 73 6b 29 21 3d 30 20  s & p->mask)!=0 
29073 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
29074 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
29075 6d 61 73 6b 20 3d 20 70 2d 3e 6d 61 73 6b 3b 20  mask = p->mask; 
29076 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
29077 20 6f 66 20 62 69 74 73 20 74 6f 20 73 65 74 20   of bits to set 
29078 6f 72 20 63 6c 65 61 72 2e 20 2a 2f 0a 20 20 20  or clear. */.   
29079 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61         if( db->a
2907a 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 7b 0a  utoCommit==0 ){.
2907b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
2907c 6f 72 65 69 67 6e 20 6b 65 79 20 73 75 70 70 6f  oreign key suppo
2907d 72 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 65 6e  rt may not be en
2907e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65  abled or disable
2907f 64 20 77 68 69 6c 65 20 6e 6f 74 0a 20 20 20 20  d while not.    
29080 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 61 75          ** in au
29081 74 6f 2d 63 6f 6d 6d 69 74 20 6d 6f 64 65 2e 20  to-commit mode. 
29082 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
29083 6d 61 73 6b 20 26 3d 20 7e 28 53 51 4c 49 54 45  mask &= ~(SQLITE
29084 5f 46 6f 72 65 69 67 6e 4b 65 79 73 29 3b 0a 20  _ForeignKeys);. 
29085 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20           }..    
29086 20 20 20 20 20 20 69 66 28 20 67 65 74 42 6f 6f        if( getBoo
29087 6c 65 61 6e 28 7a 52 69 67 68 74 29 20 29 7b 0a  lean(zRight) ){.
29088 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e              db->
29089 66 6c 61 67 73 20 7c 3d 20 6d 61 73 6b 3b 0a 20  flags |= mask;. 
2908a 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
2908b 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e              db->
2908c 66 6c 61 67 73 20 26 3d 20 7e 6d 61 73 6b 3b 0a  flags &= ~mask;.
2908d 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20            }..   
2908e 20 20 20 20 20 20 20 2f 2a 20 4d 61 6e 79 20 6f         /* Many o
2908f 66 20 74 68 65 20 66 6c 61 67 2d 70 72 61 67 6d  f the flag-pragm
29090 61 73 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f  as modify the co
29091 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  de generated by 
29092 74 68 65 20 53 51 4c 20 0a 20 20 20 20 20 20 20  the SQL .       
29093 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 28     ** compiler (
29094 65 67 2e 20 63 6f 75 6e 74 5f 63 68 61 6e 67 65  eg. count_change
29095 73 29 2e 20 53 6f 20 61 64 64 20 61 6e 20 6f 70  s). So add an op
29096 63 6f 64 65 20 74 6f 20 65 78 70 69 72 65 20 61  code to expire a
29097 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ll.          ** 
29098 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61  compiled SQL sta
29099 74 65 6d 65 6e 74 73 20 61 66 74 65 72 20 6d 6f  tements after mo
2909a 64 69 66 79 69 6e 67 20 61 20 70 72 61 67 6d 61  difying a pragma
2909b 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 20 20   value..        
2909c 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73    */.          s
2909d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2909e 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30  (v, OP_Expire, 0
2909f 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  , 0);.        }.
290a0 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 72        }..      r
290a1 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
290a2 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
290a3 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
290a4 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47  E_OMIT_FLAG_PRAG
290a5 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  MAS */../*.** Re
290a6 74 75 72 6e 20 61 20 68 75 6d 61 6e 2d 72 65 61  turn a human-rea
290a7 64 61 62 6c 65 20 6e 61 6d 65 20 66 6f 72 20 61  dable name for a
290a8 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 73 6f   constraint reso
290a9 6c 75 74 69 6f 6e 20 61 63 74 69 6f 6e 2e 0a 2a  lution action..*
290aa 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
290ab 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
290ac 59 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  Y.static const c
290ad 68 61 72 20 2a 61 63 74 69 6f 6e 4e 61 6d 65 28  har *actionName(
290ae 75 38 20 61 63 74 69 6f 6e 29 7b 0a 20 20 63 6f  u8 action){.  co
290af 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
290b0 0a 20 20 73 77 69 74 63 68 28 20 61 63 74 69 6f  .  switch( actio
290b1 6e 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 45  n ){.    case OE
290b2 5f 53 65 74 4e 75 6c 6c 3a 20 20 7a 4e 61 6d 65  _SetNull:  zName
290b3 20 3d 20 22 53 45 54 20 4e 55 4c 4c 22 3b 20 20   = "SET NULL";  
290b4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
290b5 20 63 61 73 65 20 4f 45 5f 53 65 74 44 66 6c 74   case OE_SetDflt
290b6 3a 20 20 7a 4e 61 6d 65 20 3d 20 22 53 45 54 20  :  zName = "SET 
290b7 44 45 46 41 55 4c 54 22 3b 20 20 20 20 20 62 72  DEFAULT";     br
290b8 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  eak;.    case OE
290b9 5f 43 61 73 63 61 64 65 3a 20 20 7a 4e 61 6d 65  _Cascade:  zName
290ba 20 3d 20 22 43 41 53 43 41 44 45 22 3b 20 20 20   = "CASCADE";   
290bb 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
290bc 20 63 61 73 65 20 4f 45 5f 52 65 73 74 72 69 63   case OE_Restric
290bd 74 3a 20 7a 4e 61 6d 65 20 3d 20 22 52 45 53 54  t: zName = "REST
290be 52 49 43 54 22 3b 20 20 20 20 20 20 20 20 62 72  RICT";        br
290bf 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
290c0 3a 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65  :          zName
290c1 20 3d 20 22 4e 4f 20 41 43 54 49 4f 4e 22 3b 20   = "NO ACTION"; 
290c2 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
290c3 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
290c4 61 63 74 69 6f 6e 3d 3d 4f 45 5f 4e 6f 6e 65 20  action==OE_None 
290c5 29 3b 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20  ); break;.  }.  
290c6 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a  return zName;.}.
290c7 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72  #endif../*.** Pr
290c8 6f 63 65 73 73 20 61 20 70 72 61 67 6d 61 20 73  ocess a pragma s
290c9 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a  tatement.  .**.*
290ca 2a 20 50 72 61 67 6d 61 73 20 61 72 65 20 6f 66  * Pragmas are of
290cb 20 74 68 69 73 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a   this form:.**.*
290cc 2a 20 20 20 20 20 20 50 52 41 47 4d 41 20 5b 64  *      PRAGMA [d
290cd 61 74 61 62 61 73 65 2e 5d 69 64 20 5b 3d 20 76  atabase.]id [= v
290ce 61 6c 75 65 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20  alue].**.** The 
290cf 69 64 65 6e 74 69 66 69 65 72 20 6d 69 67 68 74  identifier might
290d0 20 61 6c 73 6f 20 62 65 20 61 20 73 74 72 69 6e   also be a strin
290d1 67 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73  g.  The value is
290d2 20 61 20 73 74 72 69 6e 67 2c 20 61 6e 64 0a 2a   a string, and.*
290d3 2a 20 69 64 65 6e 74 69 66 69 65 72 2c 20 6f 72  * identifier, or
290d4 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 6d   a number.  If m
290d5 69 6e 75 73 46 6c 61 67 20 69 73 20 74 72 75 65  inusFlag is true
290d6 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
290d7 20 69 73 0a 2a 2a 20 61 20 6e 75 6d 62 65 72 20   is.** a number 
290d8 74 68 61 74 20 77 61 73 20 70 72 65 63 65 64 65  that was precede
290d9 64 20 62 79 20 61 20 6d 69 6e 75 73 20 73 69 67  d by a minus sig
290da 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
290db 6c 65 66 74 20 73 69 64 65 20 69 73 20 22 64 61  left side is "da
290dc 74 61 62 61 73 65 2e 69 64 22 20 74 68 65 6e 20  tabase.id" then 
290dd 70 49 64 31 20 69 73 20 74 68 65 20 64 61 74 61  pId1 is the data
290de 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64  base name.** and
290df 20 70 49 64 32 20 69 73 20 74 68 65 20 69 64 2e   pId2 is the id.
290e0 20 20 49 66 20 74 68 65 20 6c 65 66 74 20 73 69    If the left si
290e1 64 65 20 69 73 20 6a 75 73 74 20 22 69 64 22 20  de is just "id" 
290e2 74 68 65 6e 20 70 49 64 31 20 69 73 20 74 68 65  then pId1 is the
290e3 0a 2a 2a 20 69 64 20 61 6e 64 20 70 49 64 32 20  .** id and pId2 
290e4 69 73 20 61 6e 79 20 65 6d 70 74 79 20 73 74 72  is any empty str
290e5 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
290e6 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
290e7 74 65 33 50 72 61 67 6d 61 28 0a 20 20 50 61 72  te3Pragma(.  Par
290e8 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 54  se *pParse, .  T
290e9 6f 6b 65 6e 20 2a 70 49 64 31 2c 20 20 20 20 20  oken *pId1,     
290ea 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74     /* First part
290eb 20 6f 66 20 5b 64 61 74 61 62 61 73 65 2e 5d 69   of [database.]i
290ec 64 20 66 69 65 6c 64 20 2a 2f 0a 20 20 54 6f 6b  d field */.  Tok
290ed 65 6e 20 2a 70 49 64 32 2c 20 20 20 20 20 20 20  en *pId2,       
290ee 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20   /* Second part 
290ef 6f 66 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 64  of [database.]id
290f0 20 66 69 65 6c 64 2c 20 6f 72 20 4e 55 4c 4c 20   field, or NULL 
290f1 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 56 61 6c  */.  Token *pVal
290f2 75 65 2c 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  ue,      /* Toke
290f3 6e 20 66 6f 72 20 3c 76 61 6c 75 65 3e 2c 20 6f  n for <value>, o
290f4 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20  r NULL */.  int 
290f5 6d 69 6e 75 73 46 6c 61 67 20 20 20 20 20 20 20  minusFlag       
290f6 2f 2a 20 54 72 75 65 20 69 66 20 61 20 27 2d 27  /* True if a '-'
290f7 20 73 69 67 6e 20 70 72 65 63 65 64 65 64 20 3c   sign preceded <
290f8 76 61 6c 75 65 3e 20 2a 2f 0a 29 7b 0a 20 20 63  value> */.){.  c
290f9 68 61 72 20 2a 7a 4c 65 66 74 20 3d 20 30 3b 20  har *zLeft = 0; 
290fa 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74 65 72        /* Nul-ter
290fb 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
290fc 72 69 6e 67 20 3c 69 64 3e 20 2a 2f 0a 20 20 63  ring <id> */.  c
290fd 68 61 72 20 2a 7a 52 69 67 68 74 20 3d 20 30 3b  har *zRight = 0;
290fe 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74 65 72        /* Nul-ter
290ff 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
29100 72 69 6e 67 20 3c 76 61 6c 75 65 3e 2c 20 6f 72  ring <value>, or
29101 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74   NULL */.  const
29102 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 30 3b 20   char *zDb = 0; 
29103 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
29104 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65  e name */.  Toke
29105 6e 20 2a 70 49 64 3b 20 20 20 20 20 20 20 20 20  n *pId;         
29106 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
29107 20 3c 69 64 3e 20 74 6f 6b 65 6e 20 2a 2f 0a 20   <id> token */. 
29108 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
29109 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2910a 61 73 65 20 69 6e 64 65 78 20 66 6f 72 20 3c 64  ase index for <d
2910b 61 74 61 62 61 73 65 3e 20 2a 2f 0a 20 20 73 71  atabase> */.  sq
2910c 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2910d 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44  se->db;.  Db *pD
2910e 62 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  b;.  Vdbe *v = p
2910f 50 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 73  Parse->pVdbe = s
29110 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65  qlite3VdbeCreate
29111 28 64 62 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30  (db);.  if( v==0
29112 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 50 61   ) return;.  pPa
29113 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 0a  rse->nMem = 2;..
29114 20 20 2f 2a 20 49 6e 74 65 72 70 72 65 74 20 74    /* Interpret t
29115 68 65 20 5b 64 61 74 61 62 61 73 65 2e 5d 20 70  he [database.] p
29116 61 72 74 20 6f 66 20 74 68 65 20 70 72 61 67 6d  art of the pragm
29117 61 20 73 74 61 74 65 6d 65 6e 74 2e 20 69 44 62  a statement. iDb
29118 20 69 73 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64   is the.  ** ind
29119 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
2911a 73 65 20 74 68 69 73 20 70 72 61 67 6d 61 20 69  se this pragma i
2911b 73 20 62 65 69 6e 67 20 61 70 70 6c 69 65 64 20  s being applied 
2911c 74 6f 20 69 6e 20 64 62 2e 61 44 62 5b 5d 2e 20  to in db.aDb[]. 
2911d 2a 2f 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  */.  iDb = sqlit
2911e 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50  e3TwoPartName(pP
2911f 61 72 73 65 2c 20 70 49 64 31 2c 20 70 49 64 32  arse, pId1, pId2
29120 2c 20 26 70 49 64 29 3b 0a 20 20 69 66 28 20 69  , &pId);.  if( i
29121 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  Db<0 ) return;. 
29122 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
29123 69 44 62 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  iDb];..  /* If t
29124 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65  he temp database
29125 20 68 61 73 20 62 65 65 6e 20 65 78 70 6c 69 63   has been explic
29126 69 74 6c 79 20 6e 61 6d 65 64 20 61 73 20 70 61  itly named as pa
29127 72 74 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a 20  rt of the .  ** 
29128 70 72 61 67 6d 61 2c 20 6d 61 6b 65 20 73 75 72  pragma, make sur
29129 65 20 69 74 20 69 73 20 6f 70 65 6e 2e 20 0a 20  e it is open. . 
2912a 20 2a 2f 0a 20 20 69 66 28 20 69 44 62 3d 3d 31   */.  if( iDb==1
2912b 20 26 26 20 73 71 6c 69 74 65 33 4f 70 65 6e 54   && sqlite3OpenT
2912c 65 6d 70 44 61 74 61 62 61 73 65 28 70 50 61 72  empDatabase(pPar
2912d 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  se) ){.    retur
2912e 6e 3b 0a 20 20 7d 0a 0a 20 20 7a 4c 65 66 74 20  n;.  }..  zLeft 
2912f 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
29130 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 49 64 29 3b  mToken(db, pId);
29131 0a 20 20 69 66 28 20 21 7a 4c 65 66 74 20 29 20  .  if( !zLeft ) 
29132 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6d 69  return;.  if( mi
29133 6e 75 73 46 6c 61 67 20 29 7b 0a 20 20 20 20 7a  nusFlag ){.    z
29134 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 4d  Right = sqlite3M
29135 50 72 69 6e 74 66 28 64 62 2c 20 22 2d 25 54 22  Printf(db, "-%T"
29136 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 7d 65 6c  , pValue);.  }el
29137 73 65 7b 0a 20 20 20 20 7a 52 69 67 68 74 20 3d  se{.    zRight =
29138 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
29139 54 6f 6b 65 6e 28 64 62 2c 20 70 56 61 6c 75 65  Token(db, pValue
2913a 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  );.  }..  assert
2913b 28 20 70 49 64 32 20 29 3b 0a 20 20 7a 44 62 20  ( pId2 );.  zDb 
2913c 3d 20 70 49 64 32 2d 3e 6e 3e 30 20 3f 20 70 44  = pId2->n>0 ? pD
2913d 62 2d 3e 7a 4e 61 6d 65 20 3a 20 30 3b 0a 20 20  b->zName : 0;.  
2913e 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
2913f 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
29140 49 54 45 5f 50 52 41 47 4d 41 2c 20 7a 4c 65 66  ITE_PRAGMA, zLef
29141 74 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 20  t, zRight, zDb) 
29142 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 72 61 67  ){.    goto prag
29143 6d 61 5f 6f 75 74 3b 0a 20 20 7d 0a 20 0a 23 69  ma_out;.  }. .#i
29144 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29145 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a  T_PAGER_PRAGMAS.
29146 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
29147 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 64 65 66  A [database.]def
29148 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 0a  ault_cache_size.
29149 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
2914a 74 61 62 61 73 65 2e 5d 64 65 66 61 75 6c 74 5f  tabase.]default_
2914b 63 61 63 68 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a  cache_size=N.  *
2914c 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74  *.  ** The first
2914d 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68   form reports th
2914e 65 20 63 75 72 72 65 6e 74 20 70 65 72 73 69 73  e current persis
2914f 74 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f 72  tent setting for
29150 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63   the.  ** page c
29151 61 63 68 65 20 73 69 7a 65 2e 20 20 54 68 65 20  ache size.  The 
29152 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
29153 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  s the maximum nu
29154 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61 67  mber of.  ** pag
29155 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63  es in the page c
29156 61 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f 6e  ache.  The secon
29157 64 20 66 6f 72 6d 20 73 65 74 73 20 62 6f 74 68  d form sets both
29158 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20 2a   the current.  *
29159 2a 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a  * page cache siz
2915a 65 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20  e value and the 
2915b 70 65 72 73 69 73 74 65 6e 74 20 70 61 67 65 20  persistent page 
2915c 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65  cache size value
2915d 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  .  ** stored in 
2915e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
2915f 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  e..  **.  ** The
29160 20 64 65 66 61 75 6c 74 20 63 61 63 68 65 20 73   default cache s
29161 69 7a 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ize is stored in
29162 20 6d 65 74 61 2d 76 61 6c 75 65 20 32 20 6f 66   meta-value 2 of
29163 20 70 61 67 65 20 31 20 6f 66 20 74 68 65 0a 20   page 1 of the. 
29164 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
29165 65 2e 20 20 54 68 65 20 63 61 63 68 65 20 73 69  e.  The cache si
29166 7a 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 74  ze is actually t
29167 68 65 20 61 62 73 6f 6c 75 74 65 20 76 61 6c 75  he absolute valu
29168 65 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 6d  e of.  ** this m
29169 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 20  emory location. 
2916a 20 54 68 65 20 73 69 67 6e 20 6f 66 20 6d 65 74   The sign of met
2916b 61 2d 76 61 6c 75 65 20 32 20 64 65 74 65 72 6d  a-value 2 determ
2916c 69 6e 65 73 20 74 68 65 0a 20 20 2a 2a 20 73 79  ines the.  ** sy
2916d 6e 63 68 72 6f 6e 6f 75 73 20 73 65 74 74 69 6e  nchronous settin
2916e 67 2e 20 20 41 20 6e 65 67 61 74 69 76 65 20 76  g.  A negative v
2916f 61 6c 75 65 20 6d 65 61 6e 73 20 73 79 6e 63 68  alue means synch
29170 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 0a 20 20  ronous is off.  
29171 2a 2a 20 61 6e 64 20 61 20 70 6f 73 69 74 69 76  ** and a positiv
29172 65 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 73 79  e value means sy
29173 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 2e  nchronous is on.
29174 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
29175 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
29176 2c 22 64 65 66 61 75 6c 74 5f 63 61 63 68 65 5f  ,"default_cache_
29177 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  size")==0 ){.   
29178 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64   static const Vd
29179 62 65 4f 70 4c 69 73 74 20 67 65 74 43 61 63 68  beOpList getCach
2917a 65 53 69 7a 65 5b 5d 20 3d 20 7b 0a 20 20 20 20  eSize[] = {.    
2917b 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69    { OP_Transacti
2917c 6f 6e 2c 20 30 2c 20 30 2c 20 20 20 20 20 20 20  on, 0, 0,       
2917d 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 20   0},            
2917e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2917f 30 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f  0 */.      { OP_
29180 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 30 2c 20  ReadCookie,  0, 
29181 31 2c 20 20 20 20 20 20 20 20 42 54 52 45 45 5f  1,        BTREE_
29182 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
29183 5a 45 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20  ZE},  /* 1 */.  
29184 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20      { OP_IfPos, 
29185 20 20 20 20 20 20 31 2c 20 37 2c 20 20 20 20 20        1, 7,     
29186 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f     0},.      { O
29187 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 30  P_Integer,     0
29188 2c 20 32 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a  , 2,        0},.
29189 20 20 20 20 20 20 7b 20 4f 50 5f 53 75 62 74 72        { OP_Subtr
2918a 61 63 74 2c 20 20 20 20 31 2c 20 32 2c 20 20 20  act,    1, 2,   
2918b 20 20 20 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b       1},.      {
2918c 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20 20 20 20   OP_IfPos,      
2918d 20 31 2c 20 37 2c 20 20 20 20 20 20 20 20 30 7d   1, 7,        0}
2918e 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74  ,.      { OP_Int
2918f 65 67 65 72 2c 20 20 20 20 20 30 2c 20 31 2c 20  eger,     0, 1, 
29190 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 20 20         0},      
29191 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29192 20 20 20 2f 2a 20 36 20 2a 2f 0a 20 20 20 20 20     /* 6 */.     
29193 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c   { OP_ResultRow,
29194 20 20 20 31 2c 20 31 2c 20 20 20 20 20 20 20 20     1, 1,        
29195 30 7d 2c 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69  0},.    };.    i
29196 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 69 66 28  nt addr;.    if(
29197 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
29198 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
29199 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
2919a 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
2919b 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a  sBtree(v, iDb);.
2919c 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
2919d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2919e 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
2919f 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
291a0 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
291a1 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
291a2 4e 41 4d 45 2c 20 22 63 61 63 68 65 5f 73 69 7a  NAME, "cache_siz
291a3 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
291a4 43 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  C);.      pParse
291a5 2d 3e 6e 4d 65 6d 20 2b 3d 20 32 3b 0a 20 20 20  ->nMem += 2;.   
291a6 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
291a7 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
291a8 2c 20 41 72 72 61 79 53 69 7a 65 28 67 65 74 43  , ArraySize(getC
291a9 61 63 68 65 53 69 7a 65 29 2c 20 67 65 74 43 61  acheSize), getCa
291aa 63 68 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  cheSize);.      
291ab 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
291ac 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69 44 62  eP1(v, addr, iDb
291ad 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
291ae 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
291af 61 64 64 72 2b 31 2c 20 69 44 62 29 3b 0a 20 20  addr+1, iDb);.  
291b0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
291b1 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b  hangeP1(v, addr+
291b2 36 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  6, SQLITE_DEFAUL
291b3 54 5f 43 41 43 48 45 5f 53 49 5a 45 29 3b 0a 20  T_CACHE_SIZE);. 
291b4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
291b5 69 6e 74 20 73 69 7a 65 20 3d 20 61 74 6f 69 28  int size = atoi(
291b6 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69  zRight);.      i
291b7 66 28 20 73 69 7a 65 3c 30 20 29 20 73 69 7a 65  f( size<0 ) size
291b8 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20 20 20   = -size;.      
291b9 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
291ba 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
291bb 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20  e, 0, iDb);.    
291bc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
291bd 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
291be 72 2c 20 73 69 7a 65 2c 20 31 29 3b 0a 20 20 20  r, size, 1);.   
291bf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
291c0 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61 64 43  dOp3(v, OP_ReadC
291c1 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 32 2c 20 42  ookie, iDb, 2, B
291c2 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  TREE_DEFAULT_CAC
291c3 48 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20  HE_SIZE);.      
291c4 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
291c5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
291c6 66 50 6f 73 2c 20 32 2c 20 30 29 3b 0a 20 20 20  fPos, 2, 0);.   
291c7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
291c8 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
291c9 65 72 2c 20 2d 73 69 7a 65 2c 20 31 29 3b 0a 20  er, -size, 1);. 
291ca 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
291cb 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
291cc 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
291cd 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
291ce 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  _SetCookie, iDb,
291cf 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43   BTREE_DEFAULT_C
291d0 41 43 48 45 5f 53 49 5a 45 2c 20 31 29 3b 0a 20  ACHE_SIZE, 1);. 
291d1 20 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d       pDb->pSchem
291d2 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20  a->cache_size = 
291d3 73 69 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  size;.      sqli
291d4 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65  te3BtreeSetCache
291d5 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70  Size(pDb->pBt, p
291d6 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
291d7 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a  he_size);.    }.
291d8 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20    }else..  /*.  
291d9 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
291da 62 61 73 65 2e 5d 70 61 67 65 5f 73 69 7a 65 0a  base.]page_size.
291db 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
291dc 74 61 62 61 73 65 2e 5d 70 61 67 65 5f 73 69 7a  tabase.]page_siz
291dd 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  e=N.  **.  ** Th
291de 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70  e first form rep
291df 6f 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74  orts the current
291e0 20 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65   setting for the
291e1 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70  .  ** database p
291e2 61 67 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65  age size in byte
291e3 73 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66  s.  The second f
291e4 6f 72 6d 20 73 65 74 73 20 74 68 65 0a 20 20 2a  orm sets the.  *
291e5 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  * database page 
291e6 73 69 7a 65 20 76 61 6c 75 65 2e 20 20 54 68 65  size value.  The
291e7 20 76 61 6c 75 65 20 63 61 6e 20 6f 6e 6c 79 20   value can only 
291e8 62 65 20 73 65 74 20 69 66 0a 20 20 2a 2a 20 74  be set if.  ** t
291e9 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20  he database has 
291ea 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 65  not yet been cre
291eb 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ated..  */.  if(
291ec 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
291ed 7a 4c 65 66 74 2c 22 70 61 67 65 5f 73 69 7a 65  zLeft,"page_size
291ee 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72  ")==0 ){.    Btr
291ef 65 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70  ee *pBt = pDb->p
291f0 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  Bt;.    assert( 
291f1 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 69 66  pBt!=0 );.    if
291f2 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  ( !zRight ){.   
291f3 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 41 4c     int size = AL
291f4 57 41 59 53 28 70 42 74 29 20 3f 20 73 71 6c 69  WAYS(pBt) ? sqli
291f5 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53  te3BtreeGetPageS
291f6 69 7a 65 28 70 42 74 29 20 3a 20 30 3b 0a 20 20  ize(pBt) : 0;.  
291f7 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65      returnSingle
291f8 49 6e 74 28 70 50 61 72 73 65 2c 20 22 70 61 67  Int(pParse, "pag
291f9 65 5f 73 69 7a 65 22 2c 20 73 69 7a 65 29 3b 0a  e_size", size);.
291fa 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
291fb 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 61 79 20 66   /* Malloc may f
291fc 61 69 6c 20 77 68 65 6e 20 73 65 74 74 69 6e 67  ail when setting
291fd 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 2c 20   the page-size, 
291fe 61 73 20 74 68 65 72 65 20 69 73 20 61 6e 20 69  as there is an i
291ff 6e 74 65 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a  nternal.      **
29200 20 62 75 66 66 65 72 20 74 68 61 74 20 74 68 65   buffer that the
29201 20 70 61 67 65 72 20 6d 6f 64 75 6c 65 20 72 65   pager module re
29202 73 69 7a 65 73 20 75 73 69 6e 67 20 73 71 6c 69  sizes using sqli
29203 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 20  te3_realloc().. 
29204 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 62       */.      db
29205 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20 3d  ->nextPagesize =
29206 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20   atoi(zRight);. 
29207 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f       if( SQLITE_
29208 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 42 74  NOMEM==sqlite3Bt
29209 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70  reeSetPageSize(p
2920a 42 74 2c 20 64 62 2d 3e 6e 65 78 74 50 61 67 65  Bt, db->nextPage
2920b 73 69 7a 65 2c 20 2d 31 2c 20 30 29 20 29 7b 0a  size, -1, 0) ){.
2920c 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c          db->mall
2920d 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
2920e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
2920f 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  lse..  /*.  **  
29210 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
29211 2e 5d 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74  .]max_page_count
29212 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
29213 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67  atabase.]max_pag
29214 65 5f 63 6f 75 6e 74 3d 4e 0a 20 20 2a 2a 0a 20  e_count=N.  **. 
29215 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f   ** The first fo
29216 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20 63  rm reports the c
29217 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67 20 66  urrent setting f
29218 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 78 69  or the.  ** maxi
29219 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  mum number of pa
2921a 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
2921b 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 0a  ase file.  The .
2921c 20 20 2a 2a 20 73 65 63 6f 6e 64 20 66 6f 72 6d    ** second form
2921d 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63 68 61   attempts to cha
2921e 6e 67 65 20 74 68 69 73 20 73 65 74 74 69 6e 67  nge this setting
2921f 2e 20 20 42 6f 74 68 0a 20 20 2a 2a 20 66 6f 72  .  Both.  ** for
29220 6d 73 20 72 65 74 75 72 6e 20 74 68 65 20 63 75  ms return the cu
29221 72 72 65 6e 74 20 73 65 74 74 69 6e 67 2e 0a 20  rrent setting.. 
29222 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
29223 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
29224 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 22 29  max_page_count")
29225 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72 65 65  ==0 ){.    Btree
29226 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70 42 74   *pBt = pDb->pBt
29227 3b 0a 20 20 20 20 69 6e 74 20 6e 65 77 4d 61 78  ;.    int newMax
29228 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74   = 0;.    assert
29229 28 20 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20  ( pBt!=0 );.    
2922a 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20  if( zRight ){.  
2922b 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 61 74 6f      newMax = ato
2922c 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 7d  i(zRight);.    }
2922d 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28  .    if( ALWAYS(
2922e 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 65  pBt) ){.      ne
2922f 77 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 42 74  wMax = sqlite3Bt
29230 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  reeMaxPageCount(
29231 70 42 74 2c 20 6e 65 77 4d 61 78 29 3b 0a 20 20  pBt, newMax);.  
29232 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 53 69    }.    returnSi
29233 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20  ngleInt(pParse, 
29234 22 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 22  "max_page_count"
29235 2c 20 6e 65 77 4d 61 78 29 3b 0a 20 20 7d 65 6c  , newMax);.  }el
29236 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  se..  /*.  **  P
29237 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
29238 5d 70 61 67 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a  ]page_count.  **
29239 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  .  ** Return the
2923a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
2923b 20 69 6e 20 74 68 65 20 73 70 65 63 69 66 69 65   in the specifie
2923c 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f  d database..  */
2923d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
2923e 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70 61 67  rICmp(zLeft,"pag
2923f 65 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a  e_count")==0 ){.
29240 20 20 20 20 69 6e 74 20 69 52 65 67 3b 0a 20 20      int iReg;.  
29241 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
29242 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
29243 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
29244 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  t;.    sqlite3Co
29245 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70  deVerifySchema(p
29246 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20  Parse, iDb);.   
29247 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65   iReg = ++pParse
29248 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  ->nMem;.    sqli
29249 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2924a 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 2c 20 69   OP_Pagecount, i
2924b 44 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73  Db, iReg);.    s
2924c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2924d 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
2924e 2c 20 69 52 65 67 2c 20 31 29 3b 0a 20 20 20 20  , iReg, 1);.    
2924f 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
29250 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20  mCols(v, 1);.   
29251 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
29252 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
29253 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 61 67 65  NAME_NAME, "page
29254 5f 63 6f 75 6e 74 22 2c 20 53 51 4c 49 54 45 5f  _count", SQLITE_
29255 53 54 41 54 49 43 29 3b 0a 20 20 7d 65 6c 73 65  STATIC);.  }else
29256 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  ..  /*.  **  PRA
29257 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c  GMA [database.]l
29258 6f 63 6b 69 6e 67 5f 6d 6f 64 65 0a 20 20 2a 2a  ocking_mode.  **
29259 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
2925a 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65  se.]locking_mode
2925b 20 3d 20 28 6e 6f 72 6d 61 6c 7c 65 78 63 6c 75   = (normal|exclu
2925c 73 69 76 65 29 0a 20 20 2a 2f 0a 20 20 69 66 28  sive).  */.  if(
2925d 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2925e 7a 4c 65 66 74 2c 22 6c 6f 63 6b 69 6e 67 5f 6d  zLeft,"locking_m
2925f 6f 64 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ode")==0 ){.    
29260 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 74  const char *zRet
29261 20 3d 20 22 6e 6f 72 6d 61 6c 22 3b 0a 20 20 20   = "normal";.   
29262 20 69 6e 74 20 65 4d 6f 64 65 20 3d 20 67 65 74   int eMode = get
29263 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 7a 52 69 67  LockingMode(zRig
29264 68 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 49  ht);..    if( pI
29265 64 32 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64  d2->n==0 && eMod
29266 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
29267 4d 4f 44 45 5f 51 55 45 52 59 20 29 7b 0a 20 20  MODE_QUERY ){.  
29268 20 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50      /* Simple "P
29269 52 41 47 4d 41 20 6c 6f 63 6b 69 6e 67 5f 6d 6f  RAGMA locking_mo
2926a 64 65 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e 20  de;" statement. 
2926b 54 68 69 73 20 69 73 20 61 20 71 75 65 72 79 20  This is a query 
2926c 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  for.      ** the
2926d 20 63 75 72 72 65 6e 74 20 64 65 66 61 75 6c 74   current default
2926e 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 28 77   locking mode (w
2926f 68 69 63 68 20 6d 61 79 20 62 65 20 64 69 66 66  hich may be diff
29270 65 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a  erent to.      *
29271 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f  * the locking-mo
29272 64 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64  de of the main d
29273 61 74 61 62 61 73 65 29 2e 0a 20 20 20 20 20 20  atabase)..      
29274 2a 2f 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  */.      eMode =
29275 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64   db->dfltLockMod
29276 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  e;.    }else{.  
29277 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
29278 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64  r;.      if( pId
29279 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  2->n==0 ){.     
2927a 20 20 20 2f 2a 20 54 68 69 73 20 69 6e 64 69 63     /* This indic
2927b 61 74 65 73 20 74 68 61 74 20 6e 6f 20 64 61 74  ates that no dat
2927c 61 62 61 73 65 20 6e 61 6d 65 20 77 61 73 20 73  abase name was s
2927d 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74  pecified as part
2927e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  .        ** of t
2927f 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e  he PRAGMA comman
29280 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  d. In this case 
29281 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65  the locking-mode
29282 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20 20 20   must be.       
29283 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c 20 61   ** set on all a
29284 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
29285 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68  s, as well as th
29286 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 2e 0a  e main db file..
29287 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
29288 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20     ** Also, the 
29289 73 71 6c 69 74 65 33 2e 64 66 6c 74 4c 6f 63 6b  sqlite3.dfltLock
2928a 4d 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69 73  Mode variable is
2928b 20 73 65 74 20 73 6f 20 74 68 61 74 0a 20 20 20   set so that.   
2928c 20 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 62 73       ** any subs
2928d 65 71 75 65 6e 74 6c 79 20 61 74 74 61 63 68 65  equently attache
2928e 64 20 64 61 74 61 62 61 73 65 73 20 61 6c 73 6f  d databases also
2928f 20 75 73 65 20 74 68 65 20 73 70 65 63 69 66 69   use the specifi
29290 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c 6f  ed.        ** lo
29291 63 6b 69 6e 67 20 6d 6f 64 65 2e 0a 20 20 20 20  cking mode..    
29292 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
29293 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61  nt ii;.        a
29294 73 73 65 72 74 28 70 44 62 3d 3d 26 64 62 2d 3e  ssert(pDb==&db->
29295 61 44 62 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20  aDb[0]);.       
29296 20 66 6f 72 28 69 69 3d 32 3b 20 69 69 3c 64 62   for(ii=2; ii<db
29297 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20  ->nDb; ii++){.  
29298 20 20 20 20 20 20 20 20 70 50 61 67 65 72 20 3d          pPager =
29299 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
2929a 65 72 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70  er(db->aDb[ii].p
2929b 42 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  Bt);.          s
2929c 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69  qlite3PagerLocki
2929d 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65  ngMode(pPager, e
2929e 4d 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 7d  Mode);.        }
2929f 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66 6c  .        db->dfl
292a0 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20 28 75 38 29  tLockMode = (u8)
292a1 65 4d 6f 64 65 3b 0a 20 20 20 20 20 20 7d 0a 20  eMode;.      }. 
292a2 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71       pPager = sq
292a3 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
292a4 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20  pDb->pBt);.     
292a5 20 65 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65 33   eMode = sqlite3
292a6 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65  PagerLockingMode
292a7 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b  (pPager, eMode);
292a8 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65  .    }..    asse
292a9 72 74 28 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rt(eMode==PAGER_
292aa 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d  LOCKINGMODE_NORM
292ab 41 4c 7c 7c 65 4d 6f 64 65 3d 3d 50 41 47 45 52  AL||eMode==PAGER
292ac 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
292ad 4c 55 53 49 56 45 29 3b 0a 20 20 20 20 69 66 28  LUSIVE);.    if(
292ae 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f   eMode==PAGER_LO
292af 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53  CKINGMODE_EXCLUS
292b0 49 56 45 20 29 7b 0a 20 20 20 20 20 20 7a 52 65  IVE ){.      zRe
292b1 74 20 3d 20 22 65 78 63 6c 75 73 69 76 65 22 3b  t = "exclusive";
292b2 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
292b3 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
292b4 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
292b5 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
292b6 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
292b7 4e 41 4d 45 2c 20 22 6c 6f 63 6b 69 6e 67 5f 6d  NAME, "locking_m
292b8 6f 64 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ode", SQLITE_STA
292b9 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
292ba 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
292bb 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c  P_String8, 0, 1,
292bc 20 30 2c 20 7a 52 65 74 2c 20 30 29 3b 0a 20 20   0, zRet, 0);.  
292bd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
292be 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
292bf 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 65  Row, 1, 1);.  }e
292c0 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  lse..  /*.  **  
292c1 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
292c2 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 0a 20  .]journal_mode. 
292c3 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
292c4 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d  abase.]journal_m
292c5 6f 64 65 20 3d 20 28 64 65 6c 65 74 65 7c 70 65  ode = (delete|pe
292c6 72 73 69 73 74 7c 6f 66 66 7c 74 72 75 6e 63 61  rsist|off|trunca
292c7 74 65 7c 6d 65 6d 6f 72 79 29 0a 20 20 2a 2f 0a  te|memory).  */.
292c8 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
292c9 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6a 6f 75 72  ICmp(zLeft,"jour
292ca 6e 61 6c 5f 6d 6f 64 65 22 29 3d 3d 30 20 29 7b  nal_mode")==0 ){
292cb 0a 20 20 20 20 69 6e 74 20 65 4d 6f 64 65 3b 0a  .    int eMode;.
292cc 20 20 20 20 73 74 61 74 69 63 20 63 68 61 72 20      static char 
292cd 2a 20 63 6f 6e 73 74 20 61 7a 4d 6f 64 65 4e 61  * const azModeNa
292ce 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 22  me[] = {.      "
292cf 64 65 6c 65 74 65 22 2c 20 22 70 65 72 73 69 73  delete", "persis
292d0 74 22 2c 20 22 6f 66 66 22 2c 20 22 74 72 75 6e  t", "off", "trun
292d1 63 61 74 65 22 2c 20 22 6d 65 6d 6f 72 79 22 0a  cate", "memory".
292d2 20 20 20 20 7d 3b 0a 0a 20 20 20 20 69 66 28 20      };..    if( 
292d3 7a 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20  zRight==0 ){.   
292d4 20 20 20 65 4d 6f 64 65 20 3d 20 50 41 47 45 52     eMode = PAGER
292d5 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45  _JOURNALMODE_QUE
292d6 52 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  RY;.    }else{. 
292d7 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c       int n = sql
292d8 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 52 69  ite3Strlen30(zRi
292d9 67 68 74 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64  ght);.      eMod
292da 65 20 3d 20 73 69 7a 65 6f 66 28 61 7a 4d 6f 64  e = sizeof(azMod
292db 65 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28 61 7a  eName)/sizeof(az
292dc 4d 6f 64 65 4e 61 6d 65 5b 30 5d 29 20 2d 20 31  ModeName[0]) - 1
292dd 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 65  ;.      while( e
292de 4d 6f 64 65 3e 3d 30 20 26 26 20 73 71 6c 69 74  Mode>=0 && sqlit
292df 65 33 53 74 72 4e 49 43 6d 70 28 7a 52 69 67 68  e3StrNICmp(zRigh
292e0 74 2c 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b 65 4d  t, azModeName[eM
292e1 6f 64 65 5d 2c 20 6e 29 21 3d 30 20 29 7b 0a 20  ode], n)!=0 ){. 
292e2 20 20 20 20 20 20 20 65 4d 6f 64 65 2d 2d 3b 0a         eMode--;.
292e3 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
292e4 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30    if( pId2->n==0
292e5 20 26 26 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   && eMode==PAGER
292e6 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45  _JOURNALMODE_QUE
292e7 52 59 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53  RY ){.      /* S
292e8 69 6d 70 6c 65 20 22 50 52 41 47 4d 41 20 6a 6f  imple "PRAGMA jo
292e9 75 72 6e 61 6c 5f 6d 6f 64 65 3b 22 20 73 74 61  urnal_mode;" sta
292ea 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20  tement. This is 
292eb 61 20 71 75 65 72 79 20 66 6f 72 0a 20 20 20 20  a query for.    
292ec 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74    ** the current
292ed 20 64 65 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c   default journal
292ee 20 6d 6f 64 65 20 28 77 68 69 63 68 20 6d 61 79   mode (which may
292ef 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f   be different to
292f0 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f  .      ** the jo
292f1 75 72 6e 61 6c 2d 6d 6f 64 65 20 6f 66 20 74 68  urnal-mode of th
292f2 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 29  e main database)
292f3 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
292f4 20 65 4d 6f 64 65 20 3d 20 64 62 2d 3e 64 66 6c   eMode = db->dfl
292f5 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 0a 20 20  tJournalMode;.  
292f6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50    }else{.      P
292f7 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20  ager *pPager;.  
292f8 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d      if( pId2->n=
292f9 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
292fa 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   This indicates 
292fb 74 68 61 74 20 6e 6f 20 64 61 74 61 62 61 73 65  that no database
292fc 20 6e 61 6d 65 20 77 61 73 20 73 70 65 63 69 66   name was specif
292fd 69 65 64 20 61 73 20 70 61 72 74 0a 20 20 20 20  ied as part.    
292fe 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 50 52      ** of the PR
292ff 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e  AGMA command. In
29300 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a   this case the j
29301 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6d 75 73 74  ournal-mode must
29302 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73   be.        ** s
29303 65 74 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68  et on all attach
29304 65 64 20 64 61 74 61 62 61 73 65 73 2c 20 61 73  ed databases, as
29305 20 77 65 6c 6c 20 61 73 20 74 68 65 20 6d 61 69   well as the mai
29306 6e 20 64 62 20 66 69 6c 65 2e 0a 20 20 20 20 20  n db file..     
29307 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
29308 20 41 6c 73 6f 2c 20 74 68 65 20 73 71 6c 69 74   Also, the sqlit
29309 65 33 2e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f  e3.dfltJournalMo
2930a 64 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 73  de variable is s
2930b 65 74 20 73 6f 20 74 68 61 74 0a 20 20 20 20 20  et so that.     
2930c 20 20 20 2a 2a 20 61 6e 79 20 73 75 62 73 65 71     ** any subseq
2930d 75 65 6e 74 6c 79 20 61 74 74 61 63 68 65 64 20  uently attached 
2930e 64 61 74 61 62 61 73 65 73 20 61 6c 73 6f 20 75  databases also u
2930f 73 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64  se the specified
29310 0a 20 20 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72  .        ** jour
29311 6e 61 6c 20 6d 6f 64 65 2e 0a 20 20 20 20 20 20  nal mode..      
29312 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
29313 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61 73 73   ii;.        ass
29314 65 72 74 28 70 44 62 3d 3d 26 64 62 2d 3e 61 44  ert(pDb==&db->aD
29315 62 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 66  b[0]);.        f
29316 6f 72 28 69 69 3d 31 3b 20 69 69 3c 64 62 2d 3e  or(ii=1; ii<db->
29317 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  nDb; ii++){.    
29318 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44        if( db->aD
29319 62 5b 69 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20  b[ii].pBt ){.   
2931a 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 20           pPager 
2931b 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
2931c 67 65 72 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e  ger(db->aDb[ii].
2931d 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pBt);.          
2931e 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f    sqlite3PagerJo
2931f 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72  urnalMode(pPager
29320 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 20 20  , eMode);.      
29321 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
29322 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66 6c 74          db->dflt
29323 4a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 28 75  JournalMode = (u
29324 38 29 65 4d 6f 64 65 3b 0a 20 20 20 20 20 20 7d  8)eMode;.      }
29325 0a 20 20 20 20 20 20 70 50 61 67 65 72 20 3d 20  .      pPager = 
29326 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
29327 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20  r(pDb->pBt);.   
29328 20 20 20 65 4d 6f 64 65 20 3d 20 73 71 6c 69 74     eMode = sqlit
29329 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f  e3PagerJournalMo
2932a 64 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65  de(pPager, eMode
2932b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  );.    }.    ass
2932c 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45  ert( eMode==PAGE
2932d 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45  R_JOURNALMODE_DE
2932e 4c 45 54 45 0a 20 20 20 20 20 20 20 20 20 20 20  LETE.           
2932f 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
29330 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54  ER_JOURNALMODE_T
29331 52 55 4e 43 41 54 45 0a 20 20 20 20 20 20 20 20  RUNCATE.        
29332 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d        || eMode==
29333 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
29334 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 20 20  E_PERSIST.      
29335 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65          || eMode
29336 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
29337 4f 44 45 5f 4f 46 46 0a 20 20 20 20 20 20 20 20  ODE_OFF.        
29338 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d        || eMode==
29339 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
2933a 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 20 20  E_MEMORY );.    
2933b 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
2933c 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20  mCols(v, 1);.   
2933d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2933e 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
2933f 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6a 6f 75 72  NAME_NAME, "jour
29340 6e 61 6c 5f 6d 6f 64 65 22 2c 20 53 51 4c 49 54  nal_mode", SQLIT
29341 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  E_STATIC);.    s
29342 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
29343 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
29344 30 2c 20 31 2c 20 30 2c 20 0a 20 20 20 20 20 20  0, 1, 0, .      
29345 20 20 20 20 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b       azModeName[
29346 65 4d 6f 64 65 5d 2c 20 50 34 5f 53 54 41 54 49  eMode], P4_STATI
29347 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  C);.    sqlite3V
29348 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29349 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29  ResultRow, 1, 1)
2934a 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a  ;.  }else..  /*.
2934b 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
2934c 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f  tabase.]journal_
2934d 73 69 7a 65 5f 6c 69 6d 69 74 0a 20 20 2a 2a 20  size_limit.  ** 
2934e 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
2934f 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f  e.]journal_size_
29350 6c 69 6d 69 74 3d 4e 0a 20 20 2a 2a 0a 20 20 2a  limit=N.  **.  *
29351 2a 20 47 65 74 20 6f 72 20 73 65 74 20 74 68 65  * Get or set the
29352 20 73 69 7a 65 20 6c 69 6d 69 74 20 6f 6e 20 72   size limit on r
29353 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
29354 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  files..  */.  if
29355 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
29356 28 7a 4c 65 66 74 2c 22 6a 6f 75 72 6e 61 6c 5f  (zLeft,"journal_
29357 73 69 7a 65 5f 6c 69 6d 69 74 22 29 3d 3d 30 20  size_limit")==0 
29358 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50  ){.    Pager *pP
29359 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
2935a 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42  reePager(pDb->pB
2935b 74 29 3b 0a 20 20 20 20 69 36 34 20 69 4c 69 6d  t);.    i64 iLim
2935c 69 74 20 3d 20 2d 32 3b 0a 20 20 20 20 69 66 28  it = -2;.    if(
2935d 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20   zRight ){.     
2935e 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a   sqlite3Atoi64(z
2935f 52 69 67 68 74 2c 20 26 69 4c 69 6d 69 74 29 3b  Right, &iLimit);
29360 0a 20 20 20 20 20 20 69 66 28 20 69 4c 69 6d 69  .      if( iLimi
29361 74 3c 2d 31 20 29 20 69 4c 69 6d 69 74 20 3d 20  t<-1 ) iLimit = 
29362 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 4c  -1;.    }.    iL
29363 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61  imit = sqlite3Pa
29364 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  gerJournalSizeLi
29365 6d 69 74 28 70 50 61 67 65 72 2c 20 69 4c 69 6d  mit(pPager, iLim
29366 69 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 53  it);.    returnS
29367 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
29368 20 22 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c   "journal_size_l
29369 69 6d 69 74 22 2c 20 69 4c 69 6d 69 74 29 3b 0a  imit", iLimit);.
2936a 20 20 7d 65 6c 73 65 0a 0a 23 65 6e 64 69 66 20    }else..#endif 
2936b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  /* SQLITE_OMIT_P
2936c 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a  AGER_PRAGMAS */.
2936d 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47  .  /*.  **  PRAG
2936e 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 61 75  MA [database.]au
2936f 74 6f 5f 76 61 63 75 75 6d 0a 20 20 2a 2a 20 20  to_vacuum.  **  
29370 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
29371 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d 3d 4e 0a  .]auto_vacuum=N.
29372 20 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f 72    **.  ** Get or
29373 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f   set the value o
29374 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 27  f the database '
29375 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 61 72  auto-vacuum' par
29376 61 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 54 68 65  ameter..  ** The
29377 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f 66   value is one of
29378 3a 20 20 30 20 4e 4f 4e 45 20 31 20 46 55 4c 4c  :  0 NONE 1 FULL
29379 20 32 20 49 4e 43 52 45 4d 45 4e 54 41 4c 0a 20   2 INCREMENTAL. 
2937a 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
2937b 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
2937c 55 4d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  UM.  if( sqlite3
2937d 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 61  StrICmp(zLeft,"a
2937e 75 74 6f 5f 76 61 63 75 75 6d 22 29 3d 3d 30 20  uto_vacuum")==0 
2937f 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42  ){.    Btree *pB
29380 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20  t = pDb->pBt;.  
29381 20 20 61 73 73 65 72 74 28 20 70 42 74 21 3d 30    assert( pBt!=0
29382 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   );.    if( sqli
29383 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
29384 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67  arse) ){.      g
29385 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
29386 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 7a      }.    if( !z
29387 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69  Right ){.      i
29388 6e 74 20 61 75 74 6f 5f 76 61 63 75 75 6d 3b 0a  nt auto_vacuum;.
29389 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
2938a 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20  (pBt) ){.       
2938b 20 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20    auto_vacuum = 
2938c 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41  sqlite3BtreeGetA
2938d 75 74 6f 56 61 63 75 75 6d 28 70 42 74 29 3b 0a  utoVacuum(pBt);.
2938e 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2938f 20 20 20 20 20 20 61 75 74 6f 5f 76 61 63 75 75        auto_vacuu
29390 6d 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55  m = SQLITE_DEFAU
29391 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3b 0a 20  LT_AUTOVACUUM;. 
29392 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
29393 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
29394 72 73 65 2c 20 22 61 75 74 6f 5f 76 61 63 75 75  rse, "auto_vacuu
29395 6d 22 2c 20 61 75 74 6f 5f 76 61 63 75 75 6d 29  m", auto_vacuum)
29396 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
29397 20 20 20 69 6e 74 20 65 41 75 74 6f 20 3d 20 67     int eAuto = g
29398 65 74 41 75 74 6f 56 61 63 75 75 6d 28 7a 52 69  etAutoVacuum(zRi
29399 67 68 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ght);.      asse
2939a 72 74 28 20 65 41 75 74 6f 3e 3d 30 20 26 26 20  rt( eAuto>=0 && 
2939b 65 41 75 74 6f 3c 3d 32 20 29 3b 0a 20 20 20 20  eAuto<=2 );.    
2939c 20 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61    db->nextAutova
2939d 63 20 3d 20 28 75 38 29 65 41 75 74 6f 3b 0a 20  c = (u8)eAuto;. 
2939e 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28       if( ALWAYS(
2939f 65 41 75 74 6f 3e 3d 30 29 20 29 7b 0a 20 20 20  eAuto>=0) ){.   
293a0 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 53 65 74       /* Call Set
293a1 41 75 74 6f 56 61 63 75 75 6d 28 29 20 74 6f 20  AutoVacuum() to 
293a2 73 65 74 20 69 6e 69 74 69 61 6c 69 7a 65 20 74  set initialize t
293a3 68 65 20 69 6e 74 65 72 6e 61 6c 20 61 75 74 6f  he internal auto
293a4 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20   and.        ** 
293a5 69 6e 63 72 2d 76 61 63 75 75 6d 20 66 6c 61 67  incr-vacuum flag
293a6 73 2e 20 54 68 69 73 20 69 73 20 72 65 71 75 69  s. This is requi
293a7 72 65 64 20 69 6e 20 63 61 73 65 20 74 68 69 73  red in case this
293a8 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20   connection.    
293a9 20 20 20 20 2a 2a 20 63 72 65 61 74 65 73 20 74      ** creates t
293aa 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
293ab 2e 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e  . It is importan
293ac 74 20 74 68 61 74 20 69 74 20 69 73 20 63 72 65  t that it is cre
293ad 61 74 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  ated.        ** 
293ae 61 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  as an auto-vacuu
293af 6d 20 63 61 70 61 62 6c 65 20 64 62 2e 0a 20 20  m capable db..  
293b0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
293b1 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
293b2 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63  3BtreeSetAutoVac
293b3 75 75 6d 28 70 42 74 2c 20 65 41 75 74 6f 29 3b  uum(pBt, eAuto);
293b4 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
293b5 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 65  =SQLITE_OK && (e
293b6 41 75 74 6f 3d 3d 31 20 7c 7c 20 65 41 75 74 6f  Auto==1 || eAuto
293b7 3d 3d 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ==2) ){.        
293b8 20 20 2f 2a 20 57 68 65 6e 20 73 65 74 74 69 6e    /* When settin
293b9 67 20 74 68 65 20 61 75 74 6f 5f 76 61 63 75 75  g the auto_vacuu
293ba 6d 20 6d 6f 64 65 20 74 6f 20 65 69 74 68 65 72  m mode to either
293bb 20 22 66 75 6c 6c 22 20 6f 72 20 0a 20 20 20 20   "full" or .    
293bc 20 20 20 20 20 20 2a 2a 20 22 69 6e 63 72 65 6d        ** "increm
293bd 65 6e 74 61 6c 22 2c 20 77 72 69 74 65 20 74 68  ental", write th
293be 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b  e value of meta[
293bf 36 5d 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  6] in the databa
293c0 73 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  se.          ** 
293c1 66 69 6c 65 2e 20 42 65 66 6f 72 65 20 77 72 69  file. Before wri
293c2 74 69 6e 67 20 74 6f 20 6d 65 74 61 5b 36 5d 2c  ting to meta[6],
293c3 20 63 68 65 63 6b 20 74 68 61 74 20 6d 65 74 61   check that meta
293c4 5b 33 5d 20 69 6e 64 69 63 61 74 65 73 0a 20 20  [3] indicates.  
293c5 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20          ** that 
293c6 74 68 69 73 20 72 65 61 6c 6c 79 20 69 73 20 61  this really is a
293c7 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61  n auto-vacuum ca
293c8 70 61 62 6c 65 20 64 61 74 61 62 61 73 65 2e 0a  pable database..
293c9 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
293ca 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f         static co
293cb 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 73  nst VdbeOpList s
293cc 65 74 4d 65 74 61 36 5b 5d 20 3d 20 7b 0a 20 20  etMeta6[] = {.  
293cd 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 54            { OP_T
293ce 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20 30  ransaction,    0
293cf 2c 20 20 20 20 20 20 20 20 20 31 2c 20 20 20 20  ,         1,    
293d0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 7d 2c               0},
293d1 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20      /* 0 */.    
293d2 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 61          { OP_Rea
293d3 64 43 6f 6f 6b 69 65 2c 20 20 20 20 20 30 2c 20  dCookie,     0, 
293d4 20 20 20 20 20 20 20 20 31 2c 20 20 20 20 20 20          1,      
293d5 20 20 20 42 54 52 45 45 5f 4c 41 52 47 45 53 54     BTREE_LARGEST
293d6 5f 52 4f 4f 54 5f 50 41 47 45 7d 2c 0a 20 20 20  _ROOT_PAGE},.   
293d7 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66           { OP_If
293d8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c  ,             1,
293d9 20 20 20 20 20 20 20 20 20 30 2c 20 20 20 20 20           0,     
293da 20 20 20 20 20 20 20 20 20 20 20 20 30 7d 2c 20              0}, 
293db 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20     /* 2 */.     
293dc 20 20 20 20 20 20 20 7b 20 4f 50 5f 48 61 6c 74         { OP_Halt
293dd 2c 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  ,           SQLI
293de 54 45 5f 4f 4b 2c 20 4f 45 5f 41 62 6f 72 74 2c  TE_OK, OE_Abort,
293df 20 20 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20            0},   
293e0 20 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20   /* 3 */.       
293e1 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65       { OP_Intege
293e2 72 2c 20 20 20 20 20 20 20 20 30 2c 20 20 20 20  r,        0,    
293e3 20 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20       1,         
293e4 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f          0},    /
293e5 2a 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 4 */.         
293e6 20 20 20 7b 20 4f 50 5f 53 65 74 43 6f 6f 6b 69     { OP_SetCooki
293e7 65 2c 20 20 20 20 20 20 30 2c 20 20 20 20 20 20  e,      0,      
293e8 20 20 20 42 54 52 45 45 5f 49 4e 43 52 5f 56 41     BTREE_INCR_VA
293e9 43 55 55 4d 2c 20 31 7d 2c 20 20 20 20 2f 2a 20  CUUM, 1},    /* 
293ea 35 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 7d  5 */.          }
293eb 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ;.          int 
293ec 69 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20  iAddr;.         
293ed 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33   iAddr = sqlite3
293ee 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c  VdbeAddOpList(v,
293ef 20 41 72 72 61 79 53 69 7a 65 28 73 65 74 4d 65   ArraySize(setMe
293f0 74 61 36 29 2c 20 73 65 74 4d 65 74 61 36 29 3b  ta6), setMeta6);
293f1 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
293f2 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
293f3 2c 20 69 41 64 64 72 2c 20 69 44 62 29 3b 0a 20  , iAddr, iDb);. 
293f4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
293f5 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
293f6 69 41 64 64 72 2b 31 2c 20 69 44 62 29 3b 0a 20  iAddr+1, iDb);. 
293f7 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
293f8 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
293f9 69 41 64 64 72 2b 32 2c 20 69 41 64 64 72 2b 34  iAddr+2, iAddr+4
293fa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
293fb 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
293fc 28 76 2c 20 69 41 64 64 72 2b 34 2c 20 65 41 75  (v, iAddr+4, eAu
293fd 74 6f 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20  to-1);.         
293fe 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
293ff 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b 35 2c  geP1(v, iAddr+5,
29400 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20   iDb);.         
29401 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
29402 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20  Btree(v, iDb);. 
29403 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
29404 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23  .    }.  }else.#
29405 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  endif..  /*.  **
29406 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
29407 73 65 2e 5d 69 6e 63 72 65 6d 65 6e 74 61 6c 5f  se.]incremental_
29408 76 61 63 75 75 6d 28 4e 29 0a 20 20 2a 2a 0a 20  vacuum(N).  **. 
29409 20 2a 2a 20 44 6f 20 4e 20 73 74 65 70 73 20 6f   ** Do N steps o
2940a 66 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61  f incremental va
2940b 63 75 75 6d 69 6e 67 20 6f 6e 20 61 20 64 61 74  cuuming on a dat
2940c 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 23 69 66 6e  abase..  */.#ifn
2940d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2940e 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28  AUTOVACUUM.  if(
2940f 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
29410 7a 4c 65 66 74 2c 22 69 6e 63 72 65 6d 65 6e 74  zLeft,"increment
29411 61 6c 5f 76 61 63 75 75 6d 22 29 3d 3d 30 20 29  al_vacuum")==0 )
29412 7b 0a 20 20 20 20 69 6e 74 20 69 4c 69 6d 69 74  {.    int iLimit
29413 2c 20 61 64 64 72 3b 0a 20 20 20 20 69 66 28 20  , addr;.    if( 
29414 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
29415 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  a(pParse) ){.   
29416 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f     goto pragma_o
29417 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ut;.    }.    if
29418 28 20 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 21  ( zRight==0 || !
29419 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28  sqlite3GetInt32(
2941a 7a 52 69 67 68 74 2c 20 26 69 4c 69 6d 69 74 29  zRight, &iLimit)
2941b 20 7c 7c 20 69 4c 69 6d 69 74 3c 3d 30 20 29 7b   || iLimit<=0 ){
2941c 0a 20 20 20 20 20 20 69 4c 69 6d 69 74 20 3d 20  .      iLimit = 
2941d 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 20 20  0x7fffffff;.    
2941e 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67  }.    sqlite3Beg
2941f 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
29420 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29  (pParse, 0, iDb)
29421 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
29422 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
29423 74 65 67 65 72 2c 20 69 4c 69 6d 69 74 2c 20 31  teger, iLimit, 1
29424 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71  );.    addr = sq
29425 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
29426 76 2c 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d  v, OP_IncrVacuum
29427 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
29428 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
29429 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
2942a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2942b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
2942c 64 64 49 6d 6d 2c 20 31 2c 20 2d 31 29 3b 0a 20  ddImm, 1, -1);. 
2942d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2942e 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73  dOp2(v, OP_IfPos
2942f 2c 20 31 2c 20 61 64 64 72 29 3b 0a 20 20 20 20  , 1, addr);.    
29430 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
29431 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20  ere(v, addr);.  
29432 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69  }else.#endif..#i
29433 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29434 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a  T_PAGER_PRAGMAS.
29435 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
29436 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 63 61 63  A [database.]cac
29437 68 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52  he_size.  **  PR
29438 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
29439 63 61 63 68 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a  cache_size=N.  *
2943a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74  *.  ** The first
2943b 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68   form reports th
2943c 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c 20  e current local 
2943d 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a  setting for the.
2943e 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20    ** page cache 
2943f 73 69 7a 65 2e 20 20 54 68 65 20 6c 6f 63 61 6c  size.  The local
29440 20 73 65 74 74 69 6e 67 20 63 61 6e 20 62 65 20   setting can be 
29441 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 20  different from. 
29442 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 65   ** the persiste
29443 6e 74 20 63 61 63 68 65 20 73 69 7a 65 20 76 61  nt cache size va
29444 6c 75 65 20 74 68 61 74 20 69 73 20 73 74 6f 72  lue that is stor
29445 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ed in the databa
29446 73 65 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 73  se.  ** file its
29447 65 6c 66 2e 20 20 54 68 65 20 76 61 6c 75 65 20  elf.  The value 
29448 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20  returned is the 
29449 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
2944a 66 0a 20 20 2a 2a 20 70 61 67 65 73 20 69 6e 20  f.  ** pages in 
2944b 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 20  the page cache. 
2944c 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d   The second form
2944d 20 73 65 74 73 20 74 68 65 20 6c 6f 63 61 6c 0a   sets the local.
2944e 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20    ** page cache 
2944f 73 69 7a 65 20 76 61 6c 75 65 2e 20 20 49 74 20  size value.  It 
29450 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20  does not change 
29451 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 0a 20  the persistent. 
29452 20 2a 2a 20 63 61 63 68 65 20 73 69 7a 65 20 73   ** cache size s
29453 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 64 69 73  tored on the dis
29454 6b 20 73 6f 20 74 68 65 20 63 61 63 68 65 20 73  k so the cache s
29455 69 7a 65 20 77 69 6c 6c 20 72 65 76 65 72 74 0a  ize will revert.
29456 20 20 2a 2a 20 74 6f 20 69 74 73 20 64 65 66 61    ** to its defa
29457 75 6c 74 20 76 61 6c 75 65 20 77 68 65 6e 20 74  ult value when t
29458 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63  he database is c
29459 6c 6f 73 65 64 20 61 6e 64 20 72 65 6f 70 65 6e  losed and reopen
2945a 65 64 2e 0a 20 20 2a 2a 20 4e 20 73 68 6f 75 6c  ed..  ** N shoul
2945b 64 20 62 65 20 61 20 70 6f 73 69 74 69 76 65 20  d be a positive 
2945c 69 6e 74 65 67 65 72 2e 0a 20 20 2a 2f 0a 20 20  integer..  */.  
2945d 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2945e 6d 70 28 7a 4c 65 66 74 2c 22 63 61 63 68 65 5f  mp(zLeft,"cache_
2945f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  size")==0 ){.   
29460 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
29461 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
29462 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
29463 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68  ;.    if( !zRigh
29464 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  t ){.      retur
29465 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73  nSingleInt(pPars
29466 65 2c 20 22 63 61 63 68 65 5f 73 69 7a 65 22 2c  e, "cache_size",
29467 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63   pDb->pSchema->c
29468 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20  ache_size);.    
29469 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
2946a 20 73 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69   size = atoi(zRi
2946b 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ght);.      if( 
2946c 73 69 7a 65 3c 30 20 29 20 73 69 7a 65 20 3d 20  size<0 ) size = 
2946d 2d 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 44 62  -size;.      pDb
2946e 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65  ->pSchema->cache
2946f 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20  _size = size;.  
29470 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
29471 53 65 74 43 61 63 68 65 53 69 7a 65 28 70 44 62  SetCacheSize(pDb
29472 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53 63 68  ->pBt, pDb->pSch
29473 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29  ema->cache_size)
29474 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
29475 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41  .  /*.  **   PRA
29476 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 0a 20  GMA temp_store. 
29477 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d   **   PRAGMA tem
29478 70 5f 73 74 6f 72 65 20 3d 20 22 64 65 66 61 75  p_store = "defau
29479 6c 74 22 7c 22 6d 65 6d 6f 72 79 22 7c 22 66 69  lt"|"memory"|"fi
2947a 6c 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65  le".  **.  ** Re
2947b 74 75 72 6e 20 6f 72 20 73 65 74 20 74 68 65 20  turn or set the 
2947c 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f 66 20 74  local value of t
2947d 68 65 20 74 65 6d 70 5f 73 74 6f 72 65 20 66 6c  he temp_store fl
2947e 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20 20  ag.  Changing.  
2947f 2a 2a 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c  ** the local val
29480 75 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 6b 65  ue does not make
29481 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20   changes to the 
29482 64 69 73 6b 20 66 69 6c 65 20 61 6e 64 20 74 68  disk file and th
29483 65 20 64 65 66 61 75 6c 74 0a 20 20 2a 2a 20 76  e default.  ** v
29484 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73  alue will be res
29485 74 6f 72 65 64 20 74 68 65 20 6e 65 78 74 20 74  tored the next t
29486 69 6d 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ime the database
29487 20 69 73 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a   is opened..  **
29488 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  .  ** Note that 
29489 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66  it is possible f
2948a 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79 20 63  or the library c
2948b 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
2948c 6f 6e 73 20 74 6f 0a 20 20 2a 2a 20 6f 76 65 72  ons to.  ** over
2948d 72 69 64 65 20 74 68 69 73 20 73 65 74 74 69 6e  ride this settin
2948e 67 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  g.  */.  if( sql
2948f 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29490 74 2c 20 22 74 65 6d 70 5f 73 74 6f 72 65 22 29  t, "temp_store")
29491 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21  ==0 ){.    if( !
29492 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
29493 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
29494 70 50 61 72 73 65 2c 20 22 74 65 6d 70 5f 73 74  pParse, "temp_st
29495 6f 72 65 22 2c 20 64 62 2d 3e 74 65 6d 70 5f 73  ore", db->temp_s
29496 74 6f 72 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  tore);.    }else
29497 7b 0a 20 20 20 20 20 20 63 68 61 6e 67 65 54 65  {.      changeTe
29498 6d 70 53 74 6f 72 61 67 65 28 70 50 61 72 73 65  mpStorage(pParse
29499 2c 20 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 7d  , zRight);.    }
2949a 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
2949b 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d   **   PRAGMA tem
2949c 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72  p_store_director
2949d 79 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  y.  **   PRAGMA 
2949e 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
2949f 74 6f 72 79 20 3d 20 22 22 7c 22 64 69 72 65 63  tory = ""|"direc
294a0 74 6f 72 79 5f 6e 61 6d 65 22 0a 20 20 2a 2a 0a  tory_name".  **.
294a1 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73    ** Return or s
294a2 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c  et the local val
294a3 75 65 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73  ue of the temp_s
294a4 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 66  tore_directory f
294a5 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20  lag.  Changing. 
294a6 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 73 65   ** the value se
294a7 74 73 20 61 20 73 70 65 63 69 66 69 63 20 64 69  ts a specific di
294a8 72 65 63 74 6f 72 79 20 74 6f 20 62 65 20 75 73  rectory to be us
294a9 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79  ed for temporary
294aa 20 66 69 6c 65 73 2e 0a 20 20 2a 2a 20 53 65 74   files..  ** Set
294ab 74 69 6e 67 20 74 6f 20 61 20 6e 75 6c 6c 20 73  ting to a null s
294ac 74 72 69 6e 67 20 72 65 76 65 72 74 73 20 74 6f  tring reverts to
294ad 20 74 68 65 20 64 65 66 61 75 6c 74 20 74 65 6d   the default tem
294ae 70 6f 72 61 72 79 20 64 69 72 65 63 74 6f 72 79  porary directory
294af 20 73 65 61 72 63 68 2e 0a 20 20 2a 2a 20 49 66   search..  ** If
294b0 20 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65 63   temporary direc
294b1 74 6f 72 79 20 69 73 20 63 68 61 6e 67 65 64 2c  tory is changed,
294b2 20 74 68 65 6e 20 69 6e 76 61 6c 69 64 61 74 65   then invalidate
294b3 54 65 6d 70 53 74 6f 72 61 67 65 2e 0a 20 20 2a  TempStorage..  *
294b4 2a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  *.  */.  if( sql
294b5 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
294b6 74 2c 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64  t, "temp_store_d
294b7 69 72 65 63 74 6f 72 79 22 29 3d 3d 30 20 29 7b  irectory")==0 ){
294b8 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74  .    if( !zRight
294b9 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71   ){.      if( sq
294ba 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
294bb 74 6f 72 79 20 29 7b 0a 20 20 20 20 20 20 20 20  tory ){.        
294bc 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
294bd 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20  mCols(v, 1);.   
294be 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
294bf 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
294c0 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a   COLNAME_NAME, .
294c1 20 20 20 20 20 20 20 20 20 20 20 20 22 74 65 6d              "tem
294c2 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72  p_store_director
294c3 79 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  y", SQLITE_STATI
294c4 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  C);.        sqli
294c5 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
294c6 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
294c7 31 2c 20 30 2c 20 73 71 6c 69 74 65 33 5f 74 65  1, 0, sqlite3_te
294c8 6d 70 5f 64 69 72 65 63 74 6f 72 79 2c 20 30 29  mp_directory, 0)
294c9 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
294ca 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
294cb 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
294cc 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1);.      }.    
294cd 7d 65 6c 73 65 7b 0a 23 69 66 6e 64 65 66 20 53  }else{.#ifndef S
294ce 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20  QLITE_OMIT_WSD. 
294cf 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b       if( zRight[
294d0 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  0] ){.        in
294d1 74 20 72 63 3b 0a 20 20 20 20 20 20 20 20 69 6e  t rc;.        in
294d2 74 20 72 65 73 3b 0a 20 20 20 20 20 20 20 20 72  t res;.        r
294d3 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63  c = sqlite3OsAcc
294d4 65 73 73 28 64 62 2d 3e 70 56 66 73 2c 20 7a 52  ess(db->pVfs, zR
294d5 69 67 68 74 2c 20 53 51 4c 49 54 45 5f 41 43 43  ight, SQLITE_ACC
294d6 45 53 53 5f 52 45 41 44 57 52 49 54 45 2c 20 26  ESS_READWRITE, &
294d7 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66  res);.        if
294d8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
294d9 7c 7c 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20  || res==0 ){.   
294da 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
294db 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
294dc 6e 6f 74 20 61 20 77 72 69 74 61 62 6c 65 20 64  not a writable d
294dd 69 72 65 63 74 6f 72 79 22 29 3b 0a 20 20 20 20  irectory");.    
294de 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d        goto pragm
294df 61 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d  a_out;.        }
294e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
294e1 66 28 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53  f( SQLITE_TEMP_S
294e2 54 4f 52 45 3d 3d 30 0a 20 20 20 20 20 20 20 7c  TORE==0.       |
294e3 7c 20 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53  | (SQLITE_TEMP_S
294e4 54 4f 52 45 3d 3d 31 20 26 26 20 64 62 2d 3e 74  TORE==1 && db->t
294e5 65 6d 70 5f 73 74 6f 72 65 3c 3d 31 29 0a 20 20  emp_store<=1).  
294e6 20 20 20 20 20 7c 7c 20 28 53 51 4c 49 54 45 5f       || (SQLITE_
294e7 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 20 26 26  TEMP_STORE==2 &&
294e8 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d   db->temp_store=
294e9 3d 31 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  =1).      ){.   
294ea 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 54       invalidateT
294eb 65 6d 70 53 74 6f 72 61 67 65 28 70 50 61 72 73  empStorage(pPars
294ec 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
294ed 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73    sqlite3_free(s
294ee 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
294ef 63 74 6f 72 79 29 3b 0a 20 20 20 20 20 20 69 66  ctory);.      if
294f0 28 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20  ( zRight[0] ){. 
294f1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 74         sqlite3_t
294f2 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20  emp_directory = 
294f3 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
294f4 30 2c 20 7a 52 69 67 68 74 29 3b 0a 20 20 20 20  0, zRight);.    
294f5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
294f6 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69   sqlite3_temp_di
294f7 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a 20 20 20  rectory = 0;.   
294f8 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53     }.#endif /* S
294f9 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 2a  QLITE_OMIT_WSD *
294fa 2f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  /.    }.  }else.
294fb 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
294fc 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
294fd 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 66  ING_STYLE).#  if
294fe 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
294ff 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20  __).#    define 
29500 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
29501 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20  CKING_STYLE 1.# 
29502 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e   else.#    defin
29503 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  e SQLITE_ENABLE_
29504 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a  LOCKING_STYLE 0.
29505 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  #  endif.#endif.
29506 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
29507 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
29508 20 20 2f 2a 0a 20 20 20 2a 2a 20 20 20 50 52 41    /*.   **   PRA
29509 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c  GMA [database.]l
2950a 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 20  ock_proxy_file. 
2950b 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64    **   PRAGMA [d
2950c 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72  atabase.]lock_pr
2950d 6f 78 79 5f 66 69 6c 65 20 3d 20 22 3a 61 75 74  oxy_file = ":aut
2950e 6f 3a 22 7c 22 6c 6f 63 6b 5f 66 69 6c 65 5f 70  o:"|"lock_file_p
2950f 61 74 68 22 0a 20 20 20 2a 2a 0a 20 20 20 2a 2a  ath".   **.   **
29510 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74   Return or set t
29511 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
29512 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 20  lock_proxy_file 
29513 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a  flag.  Changing.
29514 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20     ** the value 
29515 73 65 74 73 20 61 20 73 70 65 63 69 66 69 63 20  sets a specific 
29516 66 69 6c 65 20 74 6f 20 62 65 20 75 73 65 64 20  file to be used 
29517 66 6f 72 20 64 61 74 61 62 61 73 65 20 61 63 63  for database acc
29518 65 73 73 20 6c 6f 63 6b 73 2e 0a 20 20 20 2a 2a  ess locks..   **
29519 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  .   */.  if( sql
2951a 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
2951b 74 2c 20 22 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66  t, "lock_proxy_f
2951c 69 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ile")==0 ){.    
2951d 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20  if( !zRight ){. 
2951e 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67       Pager *pPag
2951f 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
29520 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29  ePager(pDb->pBt)
29521 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 72  ;.      char *pr
29522 6f 78 79 5f 66 69 6c 65 5f 70 61 74 68 20 3d 20  oxy_file_path = 
29523 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 73 71 6c 69  NULL;.      sqli
29524 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 20  te3_file *pFile 
29525 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69  = sqlite3PagerFi
29526 6c 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  le(pPager);.    
29527 20 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43    sqlite3OsFileC
29528 6f 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51  ontrol(pFile, SQ
29529 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_GET_LOCKPRO
2952a 58 59 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20  XYFILE, .       
2952b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2952c 20 20 20 20 26 70 72 6f 78 79 5f 66 69 6c 65 5f      &proxy_file_
2952d 70 61 74 68 29 3b 0a 20 20 20 20 20 20 0a 20 20  path);.      .  
2952e 20 20 20 20 69 66 28 20 70 72 6f 78 79 5f 66 69      if( proxy_fi
2952f 6c 65 5f 70 61 74 68 20 29 7b 0a 20 20 20 20 20  le_path ){.     
29530 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29531 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
29532 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29533 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
29534 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
29535 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
29536 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29537 20 22 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c   "lock_proxy_fil
29538 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
29539 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  C);.        sqli
2953a 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2953b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
2953c 31 2c 20 30 2c 20 70 72 6f 78 79 5f 66 69 6c 65  1, 0, proxy_file
2953d 5f 70 61 74 68 2c 20 30 29 3b 0a 20 20 20 20 20  _path, 0);.     
2953e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2953f 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
29540 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20  tRow, 1, 1);.   
29541 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
29542 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 61        Pager *pPa
29543 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
29544 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74  eePager(pDb->pBt
29545 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29546 5f 66 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 73  _file *pFile = s
29547 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28  qlite3PagerFile(
29548 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 69  pPager);.      i
29549 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 69 66  nt res;.      if
2954a 28 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20  ( zRight[0] ){. 
2954b 20 20 20 20 20 20 20 72 65 73 3d 73 71 6c 69 74         res=sqlit
2954c 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  e3OsFileControl(
2954d 70 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f 53 45  pFile, SQLITE_SE
2954e 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c  T_LOCKPROXYFILE,
2954f 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
29550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29551 20 20 20 20 20 20 20 7a 52 69 67 68 74 29 3b 0a         zRight);.
29552 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20        } else {. 
29553 20 20 20 20 20 20 20 72 65 73 3d 73 71 6c 69 74         res=sqlit
29554 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  e3OsFileControl(
29555 70 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f 53 45  pFile, SQLITE_SE
29556 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c  T_LOCKPROXYFILE,
29557 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
29558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29559 20 20 20 20 20 20 20 4e 55 4c 4c 29 3b 0a 20 20         NULL);.  
2955a 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2955b 72 65 73 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  res!=SQLITE_OK )
2955c 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2955d 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2955e 2c 20 22 66 61 69 6c 65 64 20 74 6f 20 73 65 74  , "failed to set
2955f 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c 65   lock proxy file
29560 22 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  ");.        goto
29561 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
29562 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
29563 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
29564 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
29565 4e 47 5f 53 54 59 4c 45 20 2a 2f 20 20 20 20 20  NG_STYLE */     
29566 20 0a 20 20 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a   .    .  /*.  **
29567 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62     PRAGMA [datab
29568 61 73 65 2e 5d 73 79 6e 63 68 72 6f 6e 6f 75 73  ase.]synchronous
29569 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
2956a 64 61 74 61 62 61 73 65 2e 5d 73 79 6e 63 68 72  database.]synchr
2956b 6f 6e 6f 75 73 3d 4f 46 46 7c 4f 4e 7c 4e 4f 52  onous=OFF|ON|NOR
2956c 4d 41 4c 7c 46 55 4c 4c 0a 20 20 2a 2a 0a 20 20  MAL|FULL.  **.  
2956d 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74  ** Return or set
2956e 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   the local value
2956f 20 6f 66 20 74 68 65 20 73 79 6e 63 68 72 6f 6e   of the synchron
29570 6f 75 73 20 66 6c 61 67 2e 20 20 43 68 61 6e 67  ous flag.  Chang
29571 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63  ing.  ** the loc
29572 61 6c 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f  al value does no
29573 74 20 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74  t make changes t
29574 6f 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20  o the disk file 
29575 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 64 65 66  and the.  ** def
29576 61 75 6c 74 20 76 61 6c 75 65 20 77 69 6c 6c 20  ault value will 
29577 62 65 20 72 65 73 74 6f 72 65 64 20 74 68 65 20  be restored the 
29578 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 64 61  next time the da
29579 74 61 62 61 73 65 20 69 73 0a 20 20 2a 2a 20 6f  tabase is.  ** o
2957a 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  pened..  */.  if
2957b 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
2957c 28 7a 4c 65 66 74 2c 22 73 79 6e 63 68 72 6f 6e  (zLeft,"synchron
2957d 6f 75 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ous")==0 ){.    
2957e 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
2957f 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20  chema(pParse) ) 
29580 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
29581 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74  .    if( !zRight
29582 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
29583 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65  SingleInt(pParse
29584 2c 20 22 73 79 6e 63 68 72 6f 6e 6f 75 73 22 2c  , "synchronous",
29585 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76   pDb->safety_lev
29586 65 6c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65  el-1);.    }else
29587 7b 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d  {.      if( !db-
29588 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
29589 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
2958a 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
2958b 20 20 20 20 20 20 20 20 20 20 20 20 22 53 61 66              "Saf
2958c 65 74 79 20 6c 65 76 65 6c 20 6d 61 79 20 6e 6f  ety level may no
2958d 74 20 62 65 20 63 68 61 6e 67 65 64 20 69 6e 73  t be changed ins
2958e 69 64 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ide a transactio
2958f 6e 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n");.      }else
29590 7b 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 73  {.        pDb->s
29591 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 67 65  afety_level = ge
29592 74 53 61 66 65 74 79 4c 65 76 65 6c 28 7a 52 69  tSafetyLevel(zRi
29593 67 68 74 29 2b 31 3b 0a 20 20 20 20 20 20 7d 0a  ght)+1;.      }.
29594 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65      }.  }else.#e
29595 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
29596 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
29597 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  S */..#ifndef SQ
29598 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50  LITE_OMIT_FLAG_P
29599 52 41 47 4d 41 53 0a 20 20 69 66 28 20 66 6c 61  RAGMAS.  if( fla
2959a 67 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c 20  gPragma(pParse, 
2959b 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74 29 20 29  zLeft, zRight) )
2959c 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61  {.    /* The fla
2959d 67 50 72 61 67 6d 61 28 29 20 73 75 62 72 6f 75  gPragma() subrou
2959e 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65 72 61  tine also genera
2959f 74 65 73 20 61 6e 79 20 6e 65 63 65 73 73 61 72  tes any necessar
295a0 79 20 63 6f 64 65 0a 20 20 20 20 2a 2a 20 74 68  y code.    ** th
295a1 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 6d  ere is nothing m
295a2 6f 72 65 20 74 6f 20 64 6f 20 68 65 72 65 20 2a  ore to do here *
295a3 2f 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  /.  }else.#endif
295a4 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
295a5 46 4c 41 47 5f 50 52 41 47 4d 41 53 20 2a 2f 0a  FLAG_PRAGMAS */.
295a6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
295a7 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52 41 47  OMIT_SCHEMA_PRAG
295a8 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20  MAS.  /*.  **   
295a9 50 52 41 47 4d 41 20 74 61 62 6c 65 5f 69 6e 66  PRAGMA table_inf
295aa 6f 28 3c 74 61 62 6c 65 3e 29 0a 20 20 2a 2a 0a  o(<table>).  **.
295ab 20 20 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 69    ** Return a si
295ac 6e 67 6c 65 20 72 6f 77 20 66 6f 72 20 65 61 63  ngle row for eac
295ad 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
295ae 6e 61 6d 65 64 20 74 61 62 6c 65 2e 20 54 68 65  named table. The
295af 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 2a 2a   columns of.  **
295b0 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 64 61   the returned da
295b1 74 61 20 73 65 74 20 61 72 65 3a 0a 20 20 2a 2a  ta set are:.  **
295b2 0a 20 20 2a 2a 20 63 69 64 3a 20 20 20 20 20 20  .  ** cid:      
295b3 20 20 43 6f 6c 75 6d 6e 20 69 64 20 28 6e 75 6d    Column id (num
295b4 62 65 72 65 64 20 66 72 6f 6d 20 6c 65 66 74 20  bered from left 
295b5 74 6f 20 72 69 67 68 74 2c 20 73 74 61 72 74 69  to right, starti
295b6 6e 67 20 61 74 20 30 29 0a 20 20 2a 2a 20 6e 61  ng at 0).  ** na
295b7 6d 65 3a 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e  me:       Column
295b8 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 79 70 65 3a   name.  ** type:
295b9 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 64 65         Column de
295ba 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 2e 0a  claration type..
295bb 20 20 2a 2a 20 6e 6f 74 6e 75 6c 6c 3a 20 20 20    ** notnull:   
295bc 20 54 72 75 65 20 69 66 20 27 4e 4f 54 20 4e 55   True if 'NOT NU
295bd 4c 4c 27 20 69 73 20 70 61 72 74 20 6f 66 20 63  LL' is part of c
295be 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f  olumn declaratio
295bf 6e 0a 20 20 2a 2a 20 64 66 6c 74 5f 76 61 6c 75  n.  ** dflt_valu
295c0 65 3a 20 54 68 65 20 64 65 66 61 75 6c 74 20 76  e: The default v
295c1 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6c  alue for the col
295c2 75 6d 6e 2c 20 69 66 20 61 6e 79 2e 0a 20 20 2a  umn, if any..  *
295c3 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
295c4 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 74  trICmp(zLeft, "t
295c5 61 62 6c 65 5f 69 6e 66 6f 22 29 3d 3d 30 20 26  able_info")==0 &
295c6 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  & zRight ){.    
295c7 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20  Table *pTab;.   
295c8 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
295c9 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
295ca 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
295cb 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c  ;.    pTab = sql
295cc 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
295cd 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a  , zRight, zDb);.
295ce 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a      if( pTab ){.
295cf 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
295d0 20 20 20 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d     int nHidden =
295d1 20 30 3b 0a 20 20 20 20 20 20 43 6f 6c 75 6d 6e   0;.      Column
295d2 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20 20 73 71   *pCol;.      sq
295d3 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
295d4 6f 6c 73 28 76 2c 20 36 29 3b 0a 20 20 20 20 20  ols(v, 6);.     
295d5 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
295d6 36 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  6;.      sqlite3
295d7 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
295d8 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
295d9 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49 54 45  E, "cid", SQLITE
295da 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
295db 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
295dc 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
295dd 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
295de 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
295df 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
295e0 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
295e1 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   2, COLNAME_NAME
295e2 2c 20 22 74 79 70 65 22 2c 20 53 51 4c 49 54 45  , "type", SQLITE
295e3 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
295e4 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
295e5 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e  lName(v, 3, COLN
295e6 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 6f 74 6e 75  AME_NAME, "notnu
295e7 6c 6c 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ll", SQLITE_STAT
295e8 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
295e9 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
295ea 28 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 4, COLNAME_N
295eb 41 4d 45 2c 20 22 64 66 6c 74 5f 76 61 6c 75 65  AME, "dflt_value
295ec 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
295ed 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
295ee 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
295ef 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 5, COLNAME_NAM
295f0 45 2c 20 22 70 6b 22 2c 20 53 51 4c 49 54 45 5f  E, "pk", SQLITE_
295f1 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
295f2 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
295f3 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
295f4 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 66 6f   pTab);.      fo
295f5 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62  r(i=0, pCol=pTab
295f6 2d 3e 61 43 6f 6c 3b 20 69 3c 70 54 61 62 2d 3e  ->aCol; i<pTab->
295f7 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b  nCol; i++, pCol+
295f8 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
295f9 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 70  IsHiddenColumn(p
295fa 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Col) ){.        
295fb 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20 20 20    nHidden++;.   
295fc 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
295fd 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
295fe 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
295ff 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
29600 65 72 2c 20 69 2d 6e 48 69 64 64 65 6e 2c 20 31  er, i-nHidden, 1
29601 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
29602 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
29603 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32  OP_String8, 0, 2
29604 2c 20 30 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  , 0, pCol->zName
29605 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
29606 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
29607 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
29608 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  , 3, 0,.        
29609 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3f     pCol->zType ?
2960a 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3a 20 22   pCol->zType : "
2960b 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  ", 0);.        s
2960c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2960d 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2960e 28 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 3f  (pCol->notNull ?
2960f 20 31 20 3a 20 30 29 2c 20 34 29 3b 0a 20 20 20   1 : 0), 4);.   
29610 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 7a       if( pCol->z
29611 44 66 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Dflt ){.        
29612 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29613 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
29614 38 2c 20 30 2c 20 35 2c 20 30 2c 20 28 63 68 61  8, 0, 5, 0, (cha
29615 72 2a 29 70 43 6f 6c 2d 3e 7a 44 66 6c 74 2c 20  r*)pCol->zDflt, 
29616 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  0);.        }els
29617 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  e{.          sql
29618 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29619 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 35 29  , OP_Null, 0, 5)
2961a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2961b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2961c 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2961d 67 65 72 2c 20 70 43 6f 6c 2d 3e 69 73 50 72 69  ger, pCol->isPri
2961e 6d 4b 65 79 2c 20 36 29 3b 0a 20 20 20 20 20 20  mKey, 6);.      
2961f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29620 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
29621 52 6f 77 2c 20 31 2c 20 36 29 3b 0a 20 20 20 20  Row, 1, 6);.    
29622 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
29623 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  e..  if( sqlite3
29624 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
29625 69 6e 64 65 78 5f 69 6e 66 6f 22 29 3d 3d 30 20  index_info")==0 
29626 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  && zRight ){.   
29627 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
29628 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
29629 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
2962a 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
2962b 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
2962c 75 74 3b 0a 20 20 20 20 70 49 64 78 20 3d 20 73  ut;.    pIdx = s
2962d 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
2962e 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29  db, zRight, zDb)
2962f 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 20 29  ;.    if( pIdx )
29630 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  {.      int i;. 
29631 20 20 20 20 20 70 54 61 62 20 3d 20 70 49 64 78       pTab = pIdx
29632 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->pTable;.      
29633 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
29634 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20 20  mCols(v, 3);.   
29635 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
29636 3d 20 33 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 3;.      sqlit
29637 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
29638 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
29639 41 4d 45 2c 20 22 73 65 71 6e 6f 22 2c 20 53 51  AME, "seqno", SQ
2963a 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
2963b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2963c 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20  etColName(v, 1, 
2963d 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63  COLNAME_NAME, "c
2963e 69 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  id", SQLITE_STAT
2963f 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
29640 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
29641 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 2, COLNAME_N
29642 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c  AME, "name", SQL
29643 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
29644 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49     for(i=0; i<pI
29645 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  dx->nColumn; i++
29646 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63  ){.        int c
29647 6e 75 6d 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f  num = pIdx->aiCo
29648 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20  lumn[i];.       
29649 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2964a 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2964b 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  , i, 1);.       
2964c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2964d 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2964e 2c 20 63 6e 75 6d 2c 20 32 29 3b 0a 20 20 20 20  , cnum, 2);.    
2964f 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
29650 2d 3e 6e 43 6f 6c 3e 63 6e 75 6d 20 29 3b 0a 20  ->nCol>cnum );. 
29651 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29652 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
29653 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c  tring8, 0, 3, 0,
29654 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 63 6e 75 6d   pTab->aCol[cnum
29655 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ].zName, 0);.   
29656 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29657 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
29658 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20  ultRow, 1, 3);. 
29659 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2965a 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71 6c 69  else..  if( sqli
2965b 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
2965c 2c 20 22 69 6e 64 65 78 5f 6c 69 73 74 22 29 3d  , "index_list")=
2965d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a  =0 && zRight ){.
2965e 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b      Index *pIdx;
2965f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
29660 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
29661 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
29662 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
29663 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62 20  a_out;.    pTab 
29664 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
29665 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a  le(db, zRight, z
29666 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61  Db);.    if( pTa
29667 62 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 73  b ){.      v = s
29668 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
29669 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 49 64  arse);.      pId
2966a 78 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78  x = pTab->pIndex
2966b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78  ;.      if( pIdx
2966c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
2966d 69 20 3d 20 30 3b 20 0a 20 20 20 20 20 20 20 20  i = 0; .        
2966e 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
2966f 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20 20  mCols(v, 3);.   
29670 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
29671 6d 20 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 73  m = 3;.        s
29672 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
29673 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
29674 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20  ME_NAME, "seq", 
29675 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
29676 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29677 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
29678 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
29679 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45  , "name", SQLITE
2967a 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
2967b 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2967c 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f  ColName(v, 2, CO
2967d 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 75 6e 69  LNAME_NAME, "uni
2967e 71 75 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  que", SQLITE_STA
2967f 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 77 68  TIC);.        wh
29680 69 6c 65 28 70 49 64 78 29 7b 0a 20 20 20 20 20  ile(pIdx){.     
29681 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29682 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
29683 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20  eger, i, 1);.   
29684 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29685 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
29686 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c  tring8, 0, 2, 0,
29687 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pIdx->zName, 0)
29688 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
29689 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2968a 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 49 64   OP_Integer, pId
2968b 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
2968c 6f 6e 65 2c 20 33 29 3b 0a 20 20 20 20 20 20 20  one, 3);.       
2968d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2968e 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
2968f 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20 20 20  tRow, 1, 3);.   
29690 20 20 20 20 20 20 20 2b 2b 69 3b 0a 20 20 20 20         ++i;.    
29691 20 20 20 20 20 20 70 49 64 78 20 3d 20 70 49 64        pIdx = pId
29692 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  x->pNext;.      
29693 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
29694 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28  }.  }else..  if(
29695 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
29696 7a 4c 65 66 74 2c 20 22 64 61 74 61 62 61 73 65  zLeft, "database
29697 5f 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a 20 20  _list")==0 ){.  
29698 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 28    int i;.    if(
29699 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
2969a 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
2969b 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
2969c 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2969d 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20  NumCols(v, 3);. 
2969e 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
2969f 3d 20 33 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 3;.    sqlite3
296a0 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
296a1 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
296a2 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49 54 45  E, "seq", SQLITE
296a3 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
296a4 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
296a5 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d  ame(v, 1, COLNAM
296a6 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20  E_NAME, "name", 
296a7 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
296a8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
296a9 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20  etColName(v, 2, 
296aa 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 66  COLNAME_NAME, "f
296ab 69 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ile", SQLITE_STA
296ac 54 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  TIC);.    for(i=
296ad 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
296ae 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62  +){.      if( db
296af 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3d 3d 30 20  ->aDb[i].pBt==0 
296b0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
296b1 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
296b2 62 5b 69 5d 2e 7a 4e 61 6d 65 21 3d 30 20 29 3b  b[i].zName!=0 );
296b3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
296b4 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
296b5 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20  nteger, i, 1);. 
296b6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
296b7 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
296b8 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20 64  ing8, 0, 2, 0, d
296b9 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c  b->aDb[i].zName,
296ba 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
296bb 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
296bc 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33  OP_String8, 0, 3
296bd 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
296be 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46  sqlite3BtreeGetF
296bf 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b  ilename(db->aDb[
296c0 69 5d 2e 70 42 74 29 2c 20 30 29 3b 0a 20 20 20  i].pBt), 0);.   
296c1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
296c2 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
296c3 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20 20 20  tRow, 1, 3);.   
296c4 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66   }.  }else..  if
296c5 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
296c6 28 7a 4c 65 66 74 2c 20 22 63 6f 6c 6c 61 74 69  (zLeft, "collati
296c7 6f 6e 5f 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a  on_list")==0 ){.
296c8 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20      int i = 0;. 
296c9 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a     HashElem *p;.
296ca 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
296cb 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 32 29 3b  etNumCols(v, 2);
296cc 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
296cd 6d 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74  m = 2;.    sqlit
296ce 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
296cf 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
296d0 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49  AME, "seq", SQLI
296d1 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
296d2 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
296d3 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
296d4 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
296d5 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
296d6 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69  ;.    for(p=sqli
296d7 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d  teHashFirst(&db-
296d8 3e 61 43 6f 6c 6c 53 65 71 29 3b 20 70 3b 20 70  >aCollSeq); p; p
296d9 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
296da 70 29 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53  p)){.      CollS
296db 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c  eq *pColl = (Col
296dc 6c 53 65 71 20 2a 29 73 71 6c 69 74 65 48 61 73  lSeq *)sqliteHas
296dd 68 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20  hData(p);.      
296de 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
296df 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
296e0 20 69 2b 2b 2c 20 31 29 3b 0a 20 20 20 20 20 20   i++, 1);.      
296e1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
296e2 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
296e3 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c 6c 2d   0, 2, 0, pColl-
296e4 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
296e5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
296e6 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
296e7 52 6f 77 2c 20 31 2c 20 32 29 3b 0a 20 20 20 20  Row, 1, 2);.    
296e8 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
296e9 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
296ea 53 43 48 45 4d 41 5f 50 52 41 47 4d 41 53 20 2a  SCHEMA_PRAGMAS *
296eb 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
296ec 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
296ed 45 59 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  EY.  if( sqlite3
296ee 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
296ef 66 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69 73 74  foreign_key_list
296f0 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20  ")==0 && zRight 
296f1 29 7b 0a 20 20 20 20 46 4b 65 79 20 2a 70 46 4b  ){.    FKey *pFK
296f2 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  ;.    Table *pTa
296f3 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  b;.    if( sqlit
296f4 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
296f5 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
296f6 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62  ma_out;.    pTab
296f7 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
296f8 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20  ble(db, zRight, 
296f9 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54  zDb);.    if( pT
296fa 61 62 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20  ab ){.      v = 
296fb 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
296fc 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 46  Parse);.      pF
296fd 4b 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 3b  K = pTab->pFKey;
296fe 0a 20 20 20 20 20 20 69 66 28 20 70 46 4b 20 29  .      if( pFK )
296ff 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 20  {.        int i 
29700 3d 20 30 3b 20 0a 20 20 20 20 20 20 20 20 73 71  = 0; .        sq
29701 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
29702 6f 6c 73 28 76 2c 20 38 29 3b 0a 20 20 20 20 20  ols(v, 8);.     
29703 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
29704 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  = 8;.        sql
29705 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
29706 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
29707 5f 4e 41 4d 45 2c 20 22 69 64 22 2c 20 53 51 4c  _NAME, "id", SQL
29708 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
29709 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2970a 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
2970b 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
2970c 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  seq", SQLITE_STA
2970d 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
2970e 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2970f 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d  ame(v, 2, COLNAM
29710 45 5f 4e 41 4d 45 2c 20 22 74 61 62 6c 65 22 2c  E_NAME, "table",
29711 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
29712 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29713 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
29714 2c 20 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 3, COLNAME_NAM
29715 45 2c 20 22 66 72 6f 6d 22 2c 20 53 51 4c 49 54  E, "from", SQLIT
29716 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
29717 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29718 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43  tColName(v, 4, C
29719 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 74 6f  OLNAME_NAME, "to
2971a 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
2971b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2971c 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2971d 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 5, COLNAME_N
2971e 41 4d 45 2c 20 22 6f 6e 5f 75 70 64 61 74 65 22  AME, "on_update"
2971f 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
29720 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29721 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
29722 76 2c 20 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 6, COLNAME_NA
29723 4d 45 2c 20 22 6f 6e 5f 64 65 6c 65 74 65 22 2c  ME, "on_delete",
29724 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
29725 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29726 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
29727 2c 20 37 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 7, COLNAME_NAM
29728 45 2c 20 22 6d 61 74 63 68 22 2c 20 53 51 4c 49  E, "match", SQLI
29729 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
2972a 20 20 20 20 77 68 69 6c 65 28 70 46 4b 29 7b 0a      while(pFK){.
2972b 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b            int j;
2972c 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a  .          for(j
2972d 3d 30 3b 20 6a 3c 70 46 4b 2d 3e 6e 43 6f 6c 3b  =0; j<pFK->nCol;
2972e 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
2972f 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20     char *zCol = 
29730 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f  pFK->aCol[j].zCo
29731 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  l;.            c
29732 68 61 72 20 2a 7a 4f 6e 44 65 6c 65 74 65 20 3d  har *zOnDelete =
29733 20 28 63 68 61 72 20 2a 29 61 63 74 69 6f 6e 4e   (char *)actionN
29734 61 6d 65 28 70 46 4b 2d 3e 61 41 63 74 69 6f 6e  ame(pFK->aAction
29735 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  [0]);.          
29736 20 20 63 68 61 72 20 2a 7a 4f 6e 55 70 64 61 74    char *zOnUpdat
29737 65 20 3d 20 28 63 68 61 72 20 2a 29 61 63 74 69  e = (char *)acti
29738 6f 6e 4e 61 6d 65 28 70 46 4b 2d 3e 61 41 63 74  onName(pFK->aAct
29739 69 6f 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20  ion[1]);.       
2973a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2973b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2973c 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20  eger, i, 1);.   
2973d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2973e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2973f 5f 49 6e 74 65 67 65 72 2c 20 6a 2c 20 32 29 3b  _Integer, j, 2);
29740 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
29741 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
29742 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
29743 20 33 2c 20 30 2c 20 70 46 4b 2d 3e 7a 54 6f 2c   3, 0, pFK->zTo,
29744 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
29745 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29746 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
29747 2c 20 30 2c 20 34 2c 20 30 2c 0a 20 20 20 20 20  , 0, 4, 0,.     
29748 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29749 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61           pTab->a
2974a 43 6f 6c 5b 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d  Col[pFK->aCol[j]
2974b 2e 69 46 72 6f 6d 5d 2e 7a 4e 61 6d 65 2c 20 30  .iFrom].zName, 0
2974c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
2974d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2974e 28 76 2c 20 7a 43 6f 6c 20 3f 20 4f 50 5f 53 74  (v, zCol ? OP_St
2974f 72 69 6e 67 38 20 3a 20 4f 50 5f 4e 75 6c 6c 2c  ring8 : OP_Null,
29750 20 30 2c 20 35 2c 20 30 2c 20 7a 43 6f 6c 2c 20   0, 5, 0, zCol, 
29751 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
29752 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29753 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
29754 20 30 2c 20 36 2c 20 30 2c 20 7a 4f 6e 55 70 64   0, 6, 0, zOnUpd
29755 61 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ate, 0);.       
29756 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29757 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
29758 69 6e 67 38 2c 20 30 2c 20 37 2c 20 30 2c 20 7a  ing8, 0, 7, 0, z
29759 4f 6e 44 65 6c 65 74 65 2c 20 30 29 3b 0a 20 20  OnDelete, 0);.  
2975a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2975b 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2975c 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 38 2c  P_String8, 0, 8,
2975d 20 30 2c 20 22 4e 4f 4e 45 22 2c 20 30 29 3b 0a   0, "NONE", 0);.
2975e 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2975f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29760 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
29761 2c 20 38 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 8);.          
29762 7d 0a 20 20 20 20 20 20 20 20 20 20 2b 2b 69 3b  }.          ++i;
29763 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b 20 3d  .          pFK =
29764 20 70 46 4b 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b   pFK->pNextFrom;
29765 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29766 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
29767 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
29768 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
29769 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a  FOREIGN_KEY) */.
2976a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
2976b 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2976c 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 70 61 72  ICmp(zLeft, "par
2976d 73 65 72 5f 74 72 61 63 65 22 29 3d 3d 30 20 29  ser_trace")==0 )
2976e 7b 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74  {.    if( zRight
2976f 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 67 65   ){.      if( ge
29770 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29  tBoolean(zRight)
29771 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
29772 74 65 33 50 61 72 73 65 72 54 72 61 63 65 28 73  te3ParserTrace(s
29773 74 64 65 72 72 2c 20 22 70 61 72 73 65 72 3a 20  tderr, "parser: 
29774 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ");.      }else{
29775 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29776 50 61 72 73 65 72 54 72 61 63 65 28 30 2c 20 30  ParserTrace(0, 0
29777 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
29778 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a  .  }else.#endif.
29779 0a 20 20 2f 2a 20 52 65 69 6e 73 74 61 6c 6c 20  .  /* Reinstall 
2977a 74 68 65 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f  the LIKE and GLO
2977b 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  B functions.  Th
2977c 65 20 76 61 72 69 61 6e 74 20 6f 66 20 4c 49 4b  e variant of LIK
2977d 45 0a 20 20 2a 2a 20 75 73 65 64 20 77 69 6c 6c  E.  ** used will
2977e 20 62 65 20 63 61 73 65 20 73 65 6e 73 69 74 69   be case sensiti
2977f 76 65 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e 64  ve or not depend
29780 69 6e 67 20 6f 6e 20 74 68 65 20 52 48 53 2e 0a  ing on the RHS..
29781 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
29782 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29783 20 22 63 61 73 65 5f 73 65 6e 73 69 74 69 76 65   "case_sensitive
29784 5f 6c 69 6b 65 22 29 3d 3d 30 20 29 7b 0a 20 20  _like")==0 ){.  
29785 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a    if( zRight ){.
29786 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 67        sqlite3Reg
29787 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f  isterLikeFunctio
29788 6e 73 28 64 62 2c 20 67 65 74 42 6f 6f 6c 65 61  ns(db, getBoolea
29789 6e 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 20 20  n(zRight));.    
2978a 7d 0a 20 20 7d 65 6c 73 65 0a 0a 23 69 66 6e 64  }.  }else..#ifnd
2978b 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52  ef SQLITE_INTEGR
2978c 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f  ITY_CHECK_ERROR_
2978d 4d 41 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  MAX.# define SQL
2978e 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  ITE_INTEGRITY_CH
2978f 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 20 31 30  ECK_ERROR_MAX 10
29790 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  0.#endif..#ifnde
29791 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
29792 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 20 20  TEGRITY_CHECK.  
29793 2f 2a 20 50 72 61 67 6d 61 20 22 71 75 69 63 6b  /* Pragma "quick
29794 5f 63 68 65 63 6b 22 20 69 73 20 61 6e 20 65 78  _check" is an ex
29795 70 65 72 69 6d 65 6e 74 61 6c 20 72 65 64 75 63  perimental reduc
29796 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 0a 20  ed version of . 
29797 20 2a 2a 20 69 6e 74 65 67 72 69 74 79 5f 63 68   ** integrity_ch
29798 65 63 6b 20 64 65 73 69 67 6e 65 64 20 74 6f 20  eck designed to 
29799 64 65 74 65 63 74 20 6d 6f 73 74 20 64 61 74 61  detect most data
2979a 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 0a  base corruption.
2979b 20 20 2a 2a 20 77 69 74 68 6f 75 74 20 6d 6f 73    ** without mos
2979c 74 20 6f 66 20 74 68 65 20 6f 76 65 72 68 65 61  t of the overhea
2979d 64 20 6f 66 20 61 20 66 75 6c 6c 20 69 6e 74 65  d of a full inte
2979e 67 72 69 74 79 2d 63 68 65 63 6b 2e 0a 20 20 2a  grity-check..  *
2979f 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
297a0 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 69  trICmp(zLeft, "i
297a1 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 22 29  ntegrity_check")
297a2 3d 3d 30 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65  ==0.   || sqlite
297a3 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
297a4 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 29 3d 3d  "quick_check")==
297a5 30 20 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20  0 .  ){.    int 
297a6 69 2c 20 6a 2c 20 61 64 64 72 2c 20 6d 78 45 72  i, j, addr, mxEr
297a7 72 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20  r;..    /* Code 
297a8 74 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20  that appears at 
297a9 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
297aa 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e 20  ntegrity check. 
297ab 20 49 66 20 6e 6f 20 65 72 72 6f 72 0a 20 20 20   If no error.   
297ac 20 2a 2a 20 6d 65 73 73 61 67 65 73 20 68 61 76   ** messages hav
297ad 65 20 62 65 65 6e 20 67 65 6e 65 72 61 74 65 64  e been generated
297ae 2c 20 6f 75 74 70 75 74 20 4f 4b 2e 20 20 4f 74  , output OK.  Ot
297af 68 65 72 77 69 73 65 20 6f 75 74 70 75 74 20 74  herwise output t
297b0 68 65 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20  he.    ** error 
297b1 6d 65 73 73 61 67 65 0a 20 20 20 20 2a 2f 0a 20  message.    */. 
297b2 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
297b3 56 64 62 65 4f 70 4c 69 73 74 20 65 6e 64 43 6f  VdbeOpList endCo
297b4 64 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b  de[] = {.      {
297b5 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20   OP_AddImm,     
297b6 20 31 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d   1, 0,        0}
297b7 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20  ,    /* 0 */.   
297b8 20 20 20 7b 20 4f 50 5f 49 66 4e 65 67 2c 20 20     { OP_IfNeg,  
297b9 20 20 20 20 20 31 2c 20 30 2c 20 20 20 20 20 20       1, 0,      
297ba 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f    0},    /* 1 */
297bb 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69  .      { OP_Stri
297bc 6e 67 38 2c 20 20 20 20 20 30 2c 20 33 2c 20 20  ng8,     0, 3,  
297bd 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20        0},    /* 
297be 32 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f  2 */.      { OP_
297bf 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 33 2c 20  ResultRow,   3, 
297c0 31 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20  1,        0},.  
297c1 20 20 7d 3b 0a 0a 20 20 20 20 69 6e 74 20 69 73    };..    int is
297c2 51 75 69 63 6b 20 3d 20 28 7a 4c 65 66 74 5b 30  Quick = (zLeft[0
297c3 5d 3d 3d 27 71 27 29 3b 0a 0a 20 20 20 20 2f 2a  ]=='q');..    /*
297c4 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
297c5 56 44 42 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0a  VDBE program */.
297c6 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
297c7 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
297c8 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
297c9 6f 75 74 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  out;.    pParse-
297ca 3e 6e 4d 65 6d 20 3d 20 36 3b 0a 20 20 20 20 73  >nMem = 6;.    s
297cb 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
297cc 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
297cd 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
297ce 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
297cf 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 6e 74 65 67  AME_NAME, "integ
297d0 72 69 74 79 5f 63 68 65 63 6b 22 2c 20 53 51 4c  rity_check", SQL
297d1 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 0a 20 20  ITE_STATIC);..  
297d2 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d 61 78    /* Set the max
297d3 69 6d 75 6d 20 65 72 72 6f 72 20 63 6f 75 6e 74  imum error count
297d4 20 2a 2f 0a 20 20 20 20 6d 78 45 72 72 20 3d 20   */.    mxErr = 
297d5 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59  SQLITE_INTEGRITY
297d6 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58  _CHECK_ERROR_MAX
297d7 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74  ;.    if( zRight
297d8 20 29 7b 0a 20 20 20 20 20 20 6d 78 45 72 72 20   ){.      mxErr 
297d9 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a  = atoi(zRight);.
297da 20 20 20 20 20 20 69 66 28 20 6d 78 45 72 72 3c        if( mxErr<
297db 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 78  =0 ){.        mx
297dc 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  Err = SQLITE_INT
297dd 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52  EGRITY_CHECK_ERR
297de 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 20 20 7d 0a  OR_MAX;.      }.
297df 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
297e0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
297e1 50 5f 49 6e 74 65 67 65 72 2c 20 6d 78 45 72 72  P_Integer, mxErr
297e2 2c 20 31 29 3b 20 20 2f 2a 20 72 65 67 5b 31 5d  , 1);  /* reg[1]
297e3 20 68 6f 6c 64 73 20 65 72 72 6f 72 73 20 6c 65   holds errors le
297e4 66 74 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 44 6f  ft */..    /* Do
297e5 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68   an integrity ch
297e6 65 63 6b 20 6f 6e 20 65 61 63 68 20 64 61 74 61  eck on each data
297e7 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20  base file */.   
297e8 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
297e9 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
297ea 20 48 61 73 68 45 6c 65 6d 20 2a 78 3b 0a 20 20   HashElem *x;.  
297eb 20 20 20 20 48 61 73 68 20 2a 70 54 62 6c 73 3b      Hash *pTbls;
297ec 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d  .      int cnt =
297ed 20 30 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 4f   0;..      if( O
297ee 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 3d  MIT_TEMPDB && i=
297ef 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a  =1 ) continue;..
297f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64        sqlite3Cod
297f1 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
297f2 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 20 20  arse, i);.      
297f3 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
297f4 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
297f5 66 50 6f 73 2c 20 31 29 3b 20 2f 2a 20 48 61 6c  fPos, 1); /* Hal
297f6 74 20 69 66 20 6f 75 74 20 6f 66 20 65 72 72 6f  t if out of erro
297f7 72 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  rs */.      sqli
297f8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
297f9 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b   OP_Halt, 0, 0);
297fa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
297fb 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
297fc 64 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 44  dr);..      /* D
297fd 6f 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63  o an integrity c
297fe 68 65 63 6b 20 6f 66 20 74 68 65 20 42 2d 54 72  heck of the B-Tr
297ff 65 65 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  ee.      **.    
29800 20 20 2a 2a 20 42 65 67 69 6e 20 62 79 20 66 69    ** Begin by fi
29801 6c 6c 69 6e 67 20 72 65 67 69 73 74 65 72 73 20  lling registers 
29802 32 2c 20 33 2c 20 2e 2e 2e 20 77 69 74 68 20 74  2, 3, ... with t
29803 68 65 20 72 6f 6f 74 20 70 61 67 65 73 20 6e 75  he root pages nu
29804 6d 62 65 72 73 0a 20 20 20 20 20 20 2a 2a 20 66  mbers.      ** f
29805 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 61 6e  or all tables an
29806 64 20 69 6e 64 69 63 65 73 20 69 6e 20 74 68 65  d indices in the
29807 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20   database..     
29808 20 2a 2f 0a 20 20 20 20 20 20 70 54 62 6c 73 20   */.      pTbls 
29809 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53  = &db->aDb[i].pS
2980a 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a  chema->tblHash;.
2980b 20 20 20 20 20 20 66 6f 72 28 78 3d 73 71 6c 69        for(x=sqli
2980c 74 65 48 61 73 68 46 69 72 73 74 28 70 54 62 6c  teHashFirst(pTbl
2980d 73 29 3b 20 78 3b 20 78 3d 73 71 6c 69 74 65 48  s); x; x=sqliteH
2980e 61 73 68 4e 65 78 74 28 78 29 29 7b 0a 20 20 20  ashNext(x)){.   
2980f 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
29810 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74   = sqliteHashDat
29811 61 28 78 29 3b 0a 20 20 20 20 20 20 20 20 49 6e  a(x);.        In
29812 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 20  dex *pIdx;.     
29813 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29814 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
29815 65 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  er, pTab->tnum, 
29816 32 2b 63 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  2+cnt);.        
29817 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 66  cnt++;.        f
29818 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
29819 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
2981a 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
2981b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2981c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2981d 5f 49 6e 74 65 67 65 72 2c 20 70 49 64 78 2d 3e  _Integer, pIdx->
2981e 74 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a 20 20  tnum, 2+cnt);.  
2981f 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20          cnt++;. 
29820 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
29821 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20  ..      /* Make 
29822 73 75 72 65 20 73 75 66 66 69 63 69 65 6e 74 20  sure sufficient 
29823 6e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74  number of regist
29824 65 72 73 20 68 61 76 65 20 62 65 65 6e 20 61 6c  ers have been al
29825 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 20 20 20  located */.     
29826 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 4d 65   if( pParse->nMe
29827 6d 20 3c 20 63 6e 74 2b 34 20 29 7b 0a 20 20 20  m < cnt+4 ){.   
29828 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
29829 6d 20 3d 20 63 6e 74 2b 34 3b 0a 20 20 20 20 20  m = cnt+4;.     
2982a 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20   }..      /* Do 
2982b 74 68 65 20 62 2d 74 72 65 65 20 69 6e 74 65 67  the b-tree integ
2982c 72 69 74 79 20 63 68 65 63 6b 73 20 2a 2f 0a 20  rity checks */. 
2982d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2982e 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 74  AddOp3(v, OP_Int
2982f 65 67 72 69 74 79 43 6b 2c 20 32 2c 20 63 6e 74  egrityCk, 2, cnt
29830 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
29831 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
29832 76 2c 20 28 75 38 29 69 29 3b 0a 20 20 20 20 20  v, (u8)i);.     
29833 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
29834 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
29835 49 73 4e 75 6c 6c 2c 20 32 29 3b 0a 20 20 20 20  IsNull, 2);.    
29836 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29837 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
29838 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20  8, 0, 3, 0,.    
29839 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69       sqlite3MPri
2983a 6e 74 66 28 64 62 2c 20 22 2a 2a 2a 20 69 6e 20  ntf(db, "*** in 
2983b 64 61 74 61 62 61 73 65 20 25 73 20 2a 2a 2a 5c  database %s ***\
2983c 6e 22 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a  n", db->aDb[i].z
2983d 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20  Name),.         
2983e 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  P4_DYNAMIC);.   
2983f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29840 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 6f 76 65 2c  dOp3(v, OP_Move,
29841 20 32 2c 20 34 2c 20 31 29 3b 0a 20 20 20 20 20   2, 4, 1);.     
29842 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29843 70 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c  p3(v, OP_Concat,
29844 20 34 2c 20 33 2c 20 32 29 3b 0a 20 20 20 20 20   4, 3, 2);.     
29845 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29846 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
29847 6f 77 2c 20 32 2c 20 31 29 3b 0a 20 20 20 20 20  ow, 2, 1);.     
29848 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
29849 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a  Here(v, addr);..
2984a 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75        /* Make su
2984b 72 65 20 61 6c 6c 20 74 68 65 20 69 6e 64 69 63  re all the indic
2984c 65 73 20 61 72 65 20 63 6f 6e 73 74 72 75 63 74  es are construct
2984d 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 20 20  ed correctly..  
2984e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72      */.      for
2984f 28 78 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (x=sqliteHashFir
29850 73 74 28 70 54 62 6c 73 29 3b 20 78 20 26 26 20  st(pTbls); x && 
29851 21 69 73 51 75 69 63 6b 3b 20 78 3d 73 71 6c 69  !isQuick; x=sqli
29852 74 65 48 61 73 68 4e 65 78 74 28 78 29 29 7b 0a  teHashNext(x)){.
29853 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70          Table *p
29854 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68  Tab = sqliteHash
29855 44 61 74 61 28 78 29 3b 0a 20 20 20 20 20 20 20  Data(x);.       
29856 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
29857 20 20 20 20 20 20 69 6e 74 20 6c 6f 6f 70 54 6f        int loopTo
29858 70 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20  p;..        if( 
29859 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 20  pTab->pIndex==0 
2985a 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2985b 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
2985c 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2985d 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b 20 20 2f  OP_IfPos, 1);  /
2985e 2a 20 53 74 6f 70 20 69 66 20 6f 75 74 20 6f 66  * Stop if out of
2985f 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 20   errors */.     
29860 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29861 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c  dOp2(v, OP_Halt,
29862 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
29863 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
29864 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20  ere(v, addr);.  
29865 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65        sqlite3Ope
29866 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73  nTableAndIndices
29867 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 31  (pParse, pTab, 1
29868 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a  , OP_OpenRead);.
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 49 6e 74 65 67 65 72 2c 20 30 2c 20 32 29 3b 20  Integer, 0, 2); 
2986c 20 2f 2a 20 72 65 67 28 32 29 20 77 69 6c 6c 20   /* reg(2) will 
2986d 63 6f 75 6e 74 20 65 6e 74 72 69 65 73 20 2a 2f  count entries */
2986e 0a 20 20 20 20 20 20 20 20 6c 6f 6f 70 54 6f 70  .        loopTop
2986f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
29870 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
29871 64 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  d, 1, 0);.      
29872 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29873 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
29874 2c 20 32 2c 20 31 29 3b 20 20 20 2f 2a 20 69 6e  , 2, 1);   /* in
29875 63 72 65 6d 65 6e 74 20 65 6e 74 72 79 20 63 6f  crement entry co
29876 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 66  unt */.        f
29877 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61  or(j=0, pIdx=pTa
29878 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
29879 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
2987a 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  t, j++){.       
2987b 20 20 20 69 6e 74 20 6a 6d 70 32 3b 0a 20 20 20     int jmp2;.   
2987c 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f         static co
2987d 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 69  nst VdbeOpList i
2987e 64 78 45 72 72 5b 5d 20 3d 20 7b 0a 20 20 20 20  dxErr[] = {.    
2987f 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64          { OP_Add
29880 49 6d 6d 2c 20 20 20 20 20 20 31 2c 20 2d 31 2c  Imm,      1, -1,
29881 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20    0},.          
29882 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20    { OP_String8, 
29883 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 20      0,  3,  0}, 
29884 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20     /* 1 */.     
29885 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 6f 77 69         { OP_Rowi
29886 64 2c 20 20 20 20 20 20 20 31 2c 20 20 34 2c 20  d,       1,  4, 
29887 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0},.           
29888 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
29889 20 20 20 30 2c 20 20 35 2c 20 20 30 7d 2c 20 20     0,  5,  0},  
2988a 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20    /* 3 */.      
2988b 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
2988c 67 38 2c 20 20 20 20 20 30 2c 20 20 36 2c 20 20  g8,     0,  6,  
2988d 30 7d 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20  0},    /* 4 */. 
2988e 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
2988f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 34 2c 20  Concat,      4, 
29890 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20 20   3,  3},.       
29891 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74       { OP_Concat
29892 2c 20 20 20 20 20 20 35 2c 20 20 33 2c 20 20 33  ,      5,  3,  3
29893 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  },.            {
29894 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20   OP_Concat,     
29895 20 36 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20   6,  3,  3},.   
29896 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65           { OP_Re
29897 73 75 6c 74 52 6f 77 2c 20 20 20 33 2c 20 20 31  sultRow,   3,  1
29898 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  0},.         
29899 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20     { OP_IfPos,  
2989a 20 20 20 20 20 31 2c 20 20 30 2c 20 20 30 7d 2c       1,  0,  0},
2989b 20 20 20 20 2f 2a 20 39 20 2a 2f 0a 20 20 20 20      /* 9 */.    
2989c 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 48 61 6c          { OP_Hal
2989d 74 2c 20 20 20 20 20 20 20 20 30 2c 20 20 30 2c  t,        0,  0,
2989e 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20    0},.          
2989f 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  };.          sql
298a0 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65  ite3GenerateInde
298a1 78 4b 65 79 28 70 50 61 72 73 65 2c 20 70 49 64  xKey(pParse, pId
298a2 78 2c 20 31 2c 20 33 2c 20 31 29 3b 0a 20 20 20  x, 1, 3, 1);.   
298a3 20 20 20 20 20 20 20 6a 6d 70 32 20 3d 20 73 71         jmp2 = sq
298a4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
298a5 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 6a 2b 32  v, OP_Found, j+2
298a6 2c 20 30 2c 20 33 29 3b 0a 20 20 20 20 20 20 20  , 0, 3);.       
298a7 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
298a8 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
298a9 2c 20 41 72 72 61 79 53 69 7a 65 28 69 64 78 45  , ArraySize(idxE
298aa 72 72 29 2c 20 69 64 78 45 72 72 29 3b 0a 20 20  rr), idxErr);.  
298ab 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
298ac 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61  dbeChangeP4(v, a
298ad 64 64 72 2b 31 2c 20 22 72 6f 77 69 64 20 22 2c  ddr+1, "rowid ",
298ae 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
298af 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
298b0 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64  beChangeP4(v, ad
298b1 64 72 2b 33 2c 20 22 20 6d 69 73 73 69 6e 67 20  dr+3, " missing 
298b2 66 72 6f 6d 20 69 6e 64 65 78 20 22 2c 20 50 34  from index ", P4
298b3 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
298b4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
298b5 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b  hangeP4(v, addr+
298b6 34 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20  4, pIdx->zName, 
298b7 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
298b8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
298b9 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
298ba 72 2b 39 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r+9);.          
298bb 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
298bc 65 72 65 28 76 2c 20 6a 6d 70 32 29 3b 0a 20 20  ere(v, jmp2);.  
298bd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
298be 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
298bf 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 31 2c  2(v, OP_Next, 1,
298c0 20 6c 6f 6f 70 54 6f 70 2b 31 29 3b 0a 20 20 20   loopTop+1);.   
298c1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
298c2 4a 75 6d 70 48 65 72 65 28 76 2c 20 6c 6f 6f 70  JumpHere(v, loop
298c3 54 6f 70 29 3b 0a 20 20 20 20 20 20 20 20 66 6f  Top);.        fo
298c4 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  r(j=0, pIdx=pTab
298c5 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
298c6 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
298c7 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  , j++){.        
298c8 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56    static const V
298c9 64 62 65 4f 70 4c 69 73 74 20 63 6e 74 49 64 78  dbeOpList cntIdx
298ca 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20  [] = {.         
298cb 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72      { OP_Integer
298cc 2c 20 20 20 20 20 20 30 2c 20 20 33 2c 20 20 30  ,      0,  3,  0
298cd 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  },.             
298ce 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20  { OP_Rewind,    
298cf 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20     0,  0,  0},  
298d0 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 1 */.        
298d1 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d       { OP_AddImm
298d2 2c 20 20 20 20 20 20 20 33 2c 20 20 31 2c 20 20  ,       3,  1,  
298d3 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0},.            
298d4 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 20 20 20 20   { OP_Next,     
298d5 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20      0,  0,  0}, 
298d6 20 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20   /* 3 */.       
298d7 20 20 20 20 20 20 7b 20 4f 50 5f 45 71 2c 20 20        { OP_Eq,  
298d8 20 20 20 20 20 20 20 20 20 32 2c 20 20 30 2c 20           2,  0, 
298d9 20 33 7d 2c 20 20 2f 2a 20 34 20 2a 2f 0a 20 20   3},  /* 4 */.  
298da 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
298db 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 20 31 2c  AddImm,       1,
298dc 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20   -1,  0},.      
298dd 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69         { OP_Stri
298de 6e 67 38 2c 20 20 20 20 20 20 30 2c 20 20 32 2c  ng8,      0,  2,
298df 20 20 30 7d 2c 20 20 2f 2a 20 36 20 2a 2f 0a 20    0},  /* 6 */. 
298e0 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
298e1 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30  _String8,      0
298e2 2c 20 20 33 2c 20 20 30 7d 2c 20 20 2f 2a 20 37  ,  3,  0},  /* 7
298e3 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
298e4 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20   { OP_Concat,   
298e5 20 20 20 20 33 2c 20 20 32 2c 20 20 32 7d 2c 0a      3,  2,  2},.
298e6 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f               { O
298e7 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 20  P_ResultRow,    
298e8 32 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20  2,  1,  0},.    
298e9 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20        };.       
298ea 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
298eb 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
298ec 50 5f 49 66 50 6f 73 2c 20 31 29 3b 0a 20 20 20  P_IfPos, 1);.   
298ed 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
298ee 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48  beAddOp2(v, OP_H
298ef 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  alt, 0, 0);.    
298f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
298f1 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
298f2 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 64  r);.          ad
298f3 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
298f4 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72  AddOpList(v, Arr
298f5 61 79 53 69 7a 65 28 63 6e 74 49 64 78 29 2c 20  aySize(cntIdx), 
298f6 63 6e 74 49 64 78 29 3b 0a 20 20 20 20 20 20 20  cntIdx);.       
298f7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
298f8 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b 31  angeP1(v, addr+1
298f9 2c 20 6a 2b 32 29 3b 0a 20 20 20 20 20 20 20 20  , j+2);.        
298fa 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
298fb 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b 31 2c  ngeP2(v, addr+1,
298fc 20 61 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20   addr+4);.      
298fd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
298fe 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b  hangeP1(v, addr+
298ff 33 2c 20 6a 2b 32 29 3b 0a 20 20 20 20 20 20 20  3, j+2);.       
29900 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
29901 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b 33  angeP2(v, addr+3
29902 2c 20 61 64 64 72 2b 32 29 3b 0a 20 20 20 20 20  , addr+2);.     
29903 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29904 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
29905 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +4);.          s
29906 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
29907 50 34 28 76 2c 20 61 64 64 72 2b 36 2c 20 0a 20  P4(v, addr+6, . 
29908 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29909 20 20 20 20 22 77 72 6f 6e 67 20 23 20 6f 66 20      "wrong # of 
2990a 65 6e 74 72 69 65 73 20 69 6e 20 69 6e 64 65 78  entries in index
2990b 20 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a   ", P4_STATIC);.
2990c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2990d 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
2990e 20 61 64 64 72 2b 37 2c 20 70 49 64 78 2d 3e 7a   addr+7, pIdx->z
2990f 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
29910 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
29911 20 20 7d 20 0a 20 20 20 20 7d 0a 20 20 20 20 61    } .    }.    a
29912 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
29913 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72  eAddOpList(v, Ar
29914 72 61 79 53 69 7a 65 28 65 6e 64 43 6f 64 65 29  raySize(endCode)
29915 2c 20 65 6e 64 43 6f 64 65 29 3b 0a 20 20 20 20  , endCode);.    
29916 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29917 65 50 32 28 76 2c 20 61 64 64 72 2c 20 2d 6d 78  eP2(v, addr, -mx
29918 45 72 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Err);.    sqlite
29919 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2991a 20 61 64 64 72 2b 31 29 3b 0a 20 20 20 20 73 71   addr+1);.    sq
2991b 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2991c 34 28 76 2c 20 61 64 64 72 2b 32 2c 20 22 6f 6b  4(v, addr+2, "ok
2991d 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
2991e 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a   }else.#endif /*
2991f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
29920 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a  EGRITY_CHECK */.
29921 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
29922 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a 0a  OMIT_UTF16.  /*.
29923 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 65 6e    **   PRAGMA en
29924 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 20 20 50 52  coding.  **   PR
29925 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20  AGMA encoding = 
29926 22 75 74 66 2d 38 22 7c 22 75 74 66 2d 31 36 22  "utf-8"|"utf-16"
29927 7c 22 75 74 66 2d 31 36 6c 65 22 7c 22 75 74 66  |"utf-16le"|"utf
29928 2d 31 36 62 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a  -16be".  **.  **
29929 20 49 6e 20 69 74 73 20 66 69 72 73 74 20 66 6f   In its first fo
2992a 72 6d 2c 20 74 68 69 73 20 70 72 61 67 6d 61 20  rm, this pragma 
2992b 72 65 74 75 72 6e 73 20 74 68 65 20 65 6e 63 6f  returns the enco
2992c 64 69 6e 67 20 6f 66 20 74 68 65 20 6d 61 69 6e  ding of the main
2992d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  .  ** database. 
2992e 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
2992f 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a  is not initializ
29930 65 64 2c 20 69 74 20 69 73 20 69 6e 69 74 69 61  ed, it is initia
29931 6c 69 7a 65 64 20 6e 6f 77 2e 0a 20 20 2a 2a 0a  lized now..  **.
29932 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20    ** The second 
29933 66 6f 72 6d 20 6f 66 20 74 68 69 73 20 70 72 61  form of this pra
29934 67 6d 61 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  gma is a no-op i
29935 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
29936 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 68 61  ase file.  ** ha
29937 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 62 65  s not already be
29938 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20  en initialized. 
29939 49 6e 20 74 68 69 73 20 63 61 73 65 20 69 74 20  In this case it 
2993a 73 65 74 73 20 74 68 65 20 64 65 66 61 75 6c 74  sets the default
2993b 0a 20 20 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 74  .  ** encoding t
2993c 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64  hat will be used
2993d 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   for the main da
2993e 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 61  tabase file if a
2993f 20 6e 65 77 20 66 69 6c 65 0a 20 20 2a 2a 20 69   new file.  ** i
29940 73 20 63 72 65 61 74 65 64 2e 20 49 66 20 61 6e  s created. If an
29941 20 65 78 69 73 74 69 6e 67 20 6d 61 69 6e 20 64   existing main d
29942 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
29943 6f 70 65 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  opened, then the
29944 0a 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 74 65  .  ** default te
29945 78 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20  xt encoding for 
29946 74 68 65 20 65 78 69 73 74 69 6e 67 20 64 61 74  the existing dat
29947 61 62 61 73 65 20 69 73 20 75 73 65 64 2e 0a 20  abase is used.. 
29948 20 2a 2a 20 0a 20 20 2a 2a 20 49 6e 20 61 6c 6c   ** .  ** In all
29949 20 63 61 73 65 73 20 6e 65 77 20 64 61 74 61 62   cases new datab
2994a 61 73 65 73 20 63 72 65 61 74 65 64 20 75 73 69  ases created usi
2994b 6e 67 20 74 68 65 20 41 54 54 41 43 48 20 63 6f  ng the ATTACH co
2994c 6d 6d 61 6e 64 20 61 72 65 0a 20 20 2a 2a 20 63  mmand are.  ** c
2994d 72 65 61 74 65 64 20 74 6f 20 75 73 65 20 74 68  reated to use th
2994e 65 20 73 61 6d 65 20 64 65 66 61 75 6c 74 20 74  e same default t
2994f 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73 20  ext encoding as 
29950 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
29951 65 2e 20 49 66 0a 20 20 2a 2a 20 74 68 65 20 6d  e. If.  ** the m
29952 61 69 6e 20 64 61 74 61 62 61 73 65 20 68 61 73  ain database has
29953 20 6e 6f 74 20 62 65 65 6e 20 69 6e 69 74 69 61   not been initia
29954 6c 69 7a 65 64 20 61 6e 64 2f 6f 72 20 63 72 65  lized and/or cre
29955 61 74 65 64 20 77 68 65 6e 20 41 54 54 41 43 48  ated when ATTACH
29956 0a 20 20 2a 2a 20 69 73 20 65 78 65 63 75 74 65  .  ** is execute
29957 64 2c 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20  d, this is done 
29958 62 65 66 6f 72 65 20 74 68 65 20 41 54 54 41 43  before the ATTAC
29959 48 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 2a  H operation..  *
2995a 2a 0a 20 20 2a 2a 20 49 6e 20 74 68 65 20 73 65  *.  ** In the se
2995b 63 6f 6e 64 20 66 6f 72 6d 20 74 68 69 73 20 70  cond form this p
2995c 72 61 67 6d 61 20 73 65 74 73 20 74 68 65 20 74  ragma sets the t
2995d 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20  ext encoding to 
2995e 62 65 20 75 73 65 64 20 69 6e 0a 20 20 2a 2a 20  be used in.  ** 
2995f 6e 65 77 20 64 61 74 61 62 61 73 65 20 66 69 6c  new database fil
29960 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  es created using
29961 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 68   this database h
29962 61 6e 64 6c 65 2e 20 49 74 20 69 73 20 6f 6e 6c  andle. It is onl
29963 79 0a 20 20 2a 2a 20 75 73 65 66 75 6c 20 69 66  y.  ** useful if
29964 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61   invoked immedia
29965 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 6d  tely after the m
29966 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 0a 20  ain database i. 
29967 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
29968 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
29969 22 65 6e 63 6f 64 69 6e 67 22 29 3d 3d 30 20 29  "encoding")==0 )
2996a 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
2996b 73 74 20 73 74 72 75 63 74 20 45 6e 63 4e 61 6d  st struct EncNam
2996c 65 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  e {.      char *
2996d 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 75 38 20  zName;.      u8 
2996e 65 6e 63 3b 0a 20 20 20 20 7d 20 65 6e 63 6e 61  enc;.    } encna
2996f 6d 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  mes[] = {.      
29970 7b 20 22 55 54 46 38 22 2c 20 20 20 20 20 53 51  { "UTF8",     SQ
29971 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20  LITE_UTF8       
29972 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46   },.      { "UTF
29973 2d 38 22 2c 20 20 20 20 53 51 4c 49 54 45 5f 55  -8",    SQLITE_U
29974 54 46 38 20 20 20 20 20 20 20 20 7d 2c 20 20 2f  TF8        },  /
29975 2a 20 4d 75 73 74 20 62 65 20 65 6c 65 6d 65 6e  * Must be elemen
29976 74 20 5b 31 5d 20 2a 2f 0a 20 20 20 20 20 20 7b  t [1] */.      {
29977 20 22 55 54 46 2d 31 36 6c 65 22 2c 20 53 51 4c   "UTF-16le", SQL
29978 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20  ITE_UTF16LE     
29979 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 65  },  /* Must be e
2997a 6c 65 6d 65 6e 74 20 5b 32 5d 20 2a 2f 0a 20 20  lement [2] */.  
2997b 20 20 20 20 7b 20 22 55 54 46 2d 31 36 62 65 22      { "UTF-16be"
2997c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  , SQLITE_UTF16BE
2997d 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74       },  /* Must
2997e 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b 33 5d 20   be element [3] 
2997f 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55 54 46 31  */.      { "UTF1
29980 36 6c 65 22 2c 20 20 53 51 4c 49 54 45 5f 55 54  6le",  SQLITE_UT
29981 46 31 36 4c 45 20 20 20 20 20 7d 2c 0a 20 20 20  F16LE     },.   
29982 20 20 20 7b 20 22 55 54 46 31 36 62 65 22 2c 20     { "UTF16be", 
29983 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20   SQLITE_UTF16BE 
29984 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22      },.      { "
29985 55 54 46 2d 31 36 22 2c 20 20 20 30 20 20 20 20  UTF-16",   0    
29986 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c                },
29987 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36   /* SQLITE_UTF16
29988 4e 41 54 49 56 45 20 2a 2f 0a 20 20 20 20 20 20  NATIVE */.      
29989 7b 20 22 55 54 46 31 36 22 2c 20 20 20 20 30 20  { "UTF16",    0 
2998a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2998b 20 7d 2c 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54   }, /* SQLITE_UT
2998c 46 31 36 4e 41 54 49 56 45 20 2a 2f 0a 20 20 20  F16NATIVE */.   
2998d 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20 20 20 20     { 0, 0 }.    
2998e 7d 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 74 72  };.    const str
2998f 75 63 74 20 45 6e 63 4e 61 6d 65 20 2a 70 45 6e  uct EncName *pEn
29990 63 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67  c;.    if( !zRig
29991 68 74 20 29 7b 20 20 20 20 2f 2a 20 22 50 52 41  ht ){    /* "PRA
29992 47 4d 41 20 65 6e 63 6f 64 69 6e 67 22 20 2a 2f  GMA encoding" */
29993 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
29994 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
29995 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
29996 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 20 20 73 71  ma_out;.      sq
29997 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
29998 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20  ols(v, 1);.     
29999 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2999a 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
2999b 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 65 6e 63 6f  NAME_NAME, "enco
2999c 64 69 6e 67 22 2c 20 53 51 4c 49 54 45 5f 53 54  ding", SQLITE_ST
2999d 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
2999e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2999f 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
299a0 20 31 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72   1);.      asser
299a1 74 28 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49  t( encnames[SQLI
299a2 54 45 5f 55 54 46 38 5d 2e 65 6e 63 3d 3d 53 51  TE_UTF8].enc==SQ
299a3 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20  LITE_UTF8 );.   
299a4 20 20 20 61 73 73 65 72 74 28 20 65 6e 63 6e 61     assert( encna
299a5 6d 65 73 5b 53 51 4c 49 54 45 5f 55 54 46 31 36  mes[SQLITE_UTF16
299a6 4c 45 5d 2e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  LE].enc==SQLITE_
299a7 55 54 46 31 36 4c 45 20 29 3b 0a 20 20 20 20 20  UTF16LE );.     
299a8 20 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d 65   assert( encname
299a9 73 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  s[SQLITE_UTF16BE
299aa 5d 2e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ].enc==SQLITE_UT
299ab 46 31 36 42 45 20 29 3b 0a 20 20 20 20 20 20 73  F16BE );.      s
299ac 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
299ad 50 34 28 76 2c 20 2d 31 2c 20 65 6e 63 6e 61 6d  P4(v, -1, encnam
299ae 65 73 5b 45 4e 43 28 70 50 61 72 73 65 2d 3e 64  es[ENC(pParse->d
299af 62 29 5d 2e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54  b)].zName, P4_ST
299b0 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
299b1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
299b2 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
299b3 31 2c 20 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65  1, 1);.    }else
299b4 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
299b5 20 20 20 20 20 20 20 20 20 2f 2a 20 22 50 52 41           /* "PRA
299b6 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20 58  GMA encoding = X
299b7 58 58 22 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20  XX" */.      /* 
299b8 4f 6e 6c 79 20 63 68 61 6e 67 65 20 74 68 65 20  Only change the 
299b9 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 2e  value of sqlite.
299ba 65 6e 63 20 69 66 20 74 68 65 20 64 61 74 61 62  enc if the datab
299bb 61 73 65 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f  ase handle is no
299bc 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 69 74 69  t.      ** initi
299bd 61 6c 69 7a 65 64 2e 20 49 66 20 74 68 65 20 6d  alized. If the m
299be 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 78 69  ain database exi
299bf 73 74 73 2c 20 74 68 65 20 6e 65 77 20 73 71 6c  sts, the new sql
299c0 69 74 65 2e 65 6e 63 20 76 61 6c 75 65 0a 20 20  ite.enc value.  
299c1 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 6f      ** will be o
299c2 76 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e 20  verwritten when 
299c3 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e 65  the schema is ne
299c4 78 74 20 6c 6f 61 64 65 64 2e 20 49 66 20 69 74  xt loaded. If it
299c5 20 64 6f 65 73 20 6e 6f 74 0a 20 20 20 20 20 20   does not.      
299c6 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ** already exist
299c7 73 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 63 72  s, it will be cr
299c8 65 61 74 65 64 20 74 6f 20 75 73 65 20 74 68 65  eated to use the
299c9 20 6e 65 77 20 65 6e 63 6f 64 69 6e 67 20 76 61   new encoding va
299ca 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  lue..      */.  
299cb 20 20 20 20 69 66 28 20 0a 20 20 20 20 20 20 20      if( .       
299cc 20 21 28 44 62 48 61 73 50 72 6f 70 65 72 74 79   !(DbHasProperty
299cd 28 64 62 2c 20 30 2c 20 44 42 5f 53 63 68 65 6d  (db, 0, DB_Schem
299ce 61 4c 6f 61 64 65 64 29 29 20 7c 7c 20 0a 20 20  aLoaded)) || .  
299cf 20 20 20 20 20 20 44 62 48 61 73 50 72 6f 70 65        DbHasPrope
299d0 72 74 79 28 64 62 2c 20 30 2c 20 44 42 5f 45 6d  rty(db, 0, DB_Em
299d1 70 74 79 29 20 0a 20 20 20 20 20 20 29 7b 0a 20  pty) .      ){. 
299d2 20 20 20 20 20 20 20 66 6f 72 28 70 45 6e 63 3d         for(pEnc=
299d3 26 65 6e 63 6e 61 6d 65 73 5b 30 5d 3b 20 70 45  &encnames[0]; pE
299d4 6e 63 2d 3e 7a 4e 61 6d 65 3b 20 70 45 6e 63 2b  nc->zName; pEnc+
299d5 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
299d6 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49  ( 0==sqlite3StrI
299d7 43 6d 70 28 7a 52 69 67 68 74 2c 20 70 45 6e 63  Cmp(zRight, pEnc
299d8 2d 3e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  ->zName) ){.    
299d9 20 20 20 20 20 20 20 20 45 4e 43 28 70 50 61 72          ENC(pPar
299da 73 65 2d 3e 64 62 29 20 3d 20 70 45 6e 63 2d 3e  se->db) = pEnc->
299db 65 6e 63 20 3f 20 70 45 6e 63 2d 3e 65 6e 63 20  enc ? pEnc->enc 
299dc 3a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  : SQLITE_UTF16NA
299dd 54 49 56 45 3b 0a 20 20 20 20 20 20 20 20 20 20  TIVE;.          
299de 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
299df 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
299e0 20 20 20 20 20 20 20 69 66 28 20 21 70 45 6e 63         if( !pEnc
299e1 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20  ->zName ){.     
299e2 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
299e3 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e  rMsg(pParse, "un
299e4 73 75 70 70 6f 72 74 65 64 20 65 6e 63 6f 64 69  supported encodi
299e5 6e 67 3a 20 25 73 22 2c 20 7a 52 69 67 68 74 29  ng: %s", zRight)
299e6 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
299e7 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
299e8 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
299e9 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
299ea 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
299eb 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 56 45 52  _OMIT_SCHEMA_VER
299ec 53 49 4f 4e 5f 50 52 41 47 4d 41 53 0a 20 20 2f  SION_PRAGMAS.  /
299ed 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  *.  **   PRAGMA 
299ee 5b 64 61 74 61 62 61 73 65 2e 5d 73 63 68 65 6d  [database.]schem
299ef 61 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20  a_version.  **  
299f0 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
299f1 65 2e 5d 73 63 68 65 6d 61 5f 76 65 72 73 69 6f  e.]schema_versio
299f2 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e 0a 20 20  n = <integer>.  
299f3 2a 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  **.  **   PRAGMA
299f4 20 5b 64 61 74 61 62 61 73 65 2e 5d 75 73 65 72   [database.]user
299f5 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20  _version.  **   
299f6 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
299f7 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e 20 3d  .]user_version =
299f8 20 3c 69 6e 74 65 67 65 72 3e 0a 20 20 2a 2a 0a   <integer>.  **.
299f9 20 20 2a 2a 20 54 68 65 20 70 72 61 67 6d 61 27    ** The pragma'
299fa 73 20 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e  s schema_version
299fb 20 61 6e 64 20 75 73 65 72 5f 76 65 72 73 69 6f   and user_versio
299fc 6e 20 61 72 65 20 75 73 65 64 20 74 6f 20 73 65  n are used to se
299fd 74 20 6f 72 20 67 65 74 0a 20 20 2a 2a 20 74 68  t or get.  ** th
299fe 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73  e value of the s
299ff 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e  chema-version an
29a00 64 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 2c 20  d user-version, 
29a01 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 42 6f  respectively. Bo
29a02 74 68 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65  th.  ** the sche
29a03 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e 64 20 74  ma-version and t
29a04 68 65 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 20  he user-version 
29a05 61 72 65 20 33 32 2d 62 69 74 20 73 69 67 6e 65  are 32-bit signe
29a06 64 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20  d integers.  ** 
29a07 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 61  stored in the da
29a08 74 61 62 61 73 65 20 68 65 61 64 65 72 2e 0a 20  tabase header.. 
29a09 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 63 68   **.  ** The sch
29a0a 65 6d 61 2d 63 6f 6f 6b 69 65 20 69 73 20 75 73  ema-cookie is us
29a0b 75 61 6c 6c 79 20 6f 6e 6c 79 20 6d 61 6e 69 70  ually only manip
29a0c 75 6c 61 74 65 64 20 69 6e 74 65 72 6e 61 6c 6c  ulated internall
29a0d 79 20 62 79 20 53 51 4c 69 74 65 2e 20 49 74 0a  y by SQLite. It.
29a0e 20 20 2a 2a 20 69 73 20 69 6e 63 72 65 6d 65 6e    ** is incremen
29a0f 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 77 68  ted by SQLite wh
29a10 65 6e 65 76 65 72 20 74 68 65 20 64 61 74 61 62  enever the datab
29a11 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 6d 6f  ase schema is mo
29a12 64 69 66 69 65 64 20 28 62 79 0a 20 20 2a 2a 20  dified (by.  ** 
29a13 63 72 65 61 74 69 6e 67 20 6f 72 20 64 72 6f 70  creating or drop
29a14 70 69 6e 67 20 61 20 74 61 62 6c 65 20 6f 72 20  ping a table or 
29a15 69 6e 64 65 78 29 2e 20 54 68 65 20 73 63 68 65  index). The sche
29a16 6d 61 20 76 65 72 73 69 6f 6e 20 69 73 20 75 73  ma version is us
29a17 65 64 20 62 79 0a 20 20 2a 2a 20 53 51 4c 69 74  ed by.  ** SQLit
29a18 65 20 65 61 63 68 20 74 69 6d 65 20 61 20 71 75  e each time a qu
29a19 65 72 79 20 69 73 20 65 78 65 63 75 74 65 64 20  ery is executed 
29a1a 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  to ensure that t
29a1b 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63 68  he internal cach
29a1c 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63  e.  ** of the sc
29a1d 68 65 6d 61 20 75 73 65 64 20 77 68 65 6e 20 63  hema used when c
29a1e 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 51 4c  ompiling the SQL
29a1f 20 71 75 65 72 79 20 6d 61 74 63 68 65 73 20 74   query matches t
29a20 68 65 20 73 63 68 65 6d 61 20 6f 66 0a 20 20 2a  he schema of.  *
29a21 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  * the database a
29a22 67 61 69 6e 73 74 20 77 68 69 63 68 20 74 68 65  gainst which the
29a23 20 63 6f 6d 70 69 6c 65 64 20 71 75 65 72 79 20   compiled query 
29a24 69 73 20 61 63 74 75 61 6c 6c 79 20 65 78 65 63  is actually exec
29a25 75 74 65 64 2e 0a 20 20 2a 2a 20 53 75 62 76 65  uted..  ** Subve
29a26 72 74 69 6e 67 20 74 68 69 73 20 6d 65 63 68 61  rting this mecha
29a27 6e 69 73 6d 20 62 79 20 75 73 69 6e 67 20 22 50  nism by using "P
29a28 52 41 47 4d 41 20 73 63 68 65 6d 61 5f 76 65 72  RAGMA schema_ver
29a29 73 69 6f 6e 22 20 74 6f 20 6d 6f 64 69 66 79 0a  sion" to modify.
29a2a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 2d    ** the schema-
29a2b 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 74 65 6e  version is poten
29a2c 74 69 61 6c 6c 79 20 64 61 6e 67 65 72 6f 75 73  tially dangerous
29a2d 20 61 6e 64 20 6d 61 79 20 6c 65 61 64 20 74 6f   and may lead to
29a2e 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2a 20 63 72   program.  ** cr
29a2f 61 73 68 65 73 20 6f 72 20 64 61 74 61 62 61 73  ashes or databas
29a30 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 55 73  e corruption. Us
29a31 65 20 77 69 74 68 20 63 61 75 74 69 6f 6e 21 0a  e with caution!.
29a32 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 75 73    **.  ** The us
29a33 65 72 2d 76 65 72 73 69 6f 6e 20 69 73 20 6e 6f  er-version is no
29a34 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c  t used internall
29a35 79 20 62 79 20 53 51 4c 69 74 65 2e 20 49 74 20  y by SQLite. It 
29a36 6d 61 79 20 62 65 20 75 73 65 64 20 62 79 0a 20  may be used by. 
29a37 20 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73   ** applications
29a38 20 66 6f 72 20 61 6e 79 20 70 75 72 70 6f 73 65   for any purpose
29a39 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
29a3a 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29a3b 74 2c 20 22 73 63 68 65 6d 61 5f 76 65 72 73 69  t, "schema_versi
29a3c 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c 7c 20 73  on")==0 .   || s
29a3d 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
29a3e 65 66 74 2c 20 22 75 73 65 72 5f 76 65 72 73 69  eft, "user_versi
29a3f 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c 7c 20 73  on")==0 .   || s
29a40 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
29a41 65 66 74 2c 20 22 66 72 65 65 6c 69 73 74 5f 63  eft, "freelist_c
29a42 6f 75 6e 74 22 29 3d 3d 30 20 0a 20 20 29 7b 0a  ount")==0 .  ){.
29a43 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b      int iCookie;
29a44 20 20 20 2f 2a 20 43 6f 6f 6b 69 65 20 69 6e 64     /* Cookie ind
29a45 65 78 2e 20 31 20 66 6f 72 20 73 63 68 65 6d 61  ex. 1 for schema
29a46 2d 63 6f 6f 6b 69 65 2c 20 36 20 66 6f 72 20 75  -cookie, 6 for u
29a47 73 65 72 2d 63 6f 6f 6b 69 65 2e 20 2a 2f 0a 20  ser-cookie. */. 
29a48 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73     sqlite3VdbeUs
29a49 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b  esBtree(v, iDb);
29a4a 0a 20 20 20 20 73 77 69 74 63 68 28 20 7a 4c 65  .    switch( zLe
29a4b 66 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 63  ft[0] ){.      c
29a4c 61 73 65 20 27 66 27 3a 20 63 61 73 65 20 27 46  ase 'f': case 'F
29a4d 27 3a 0a 20 20 20 20 20 20 20 20 69 43 6f 6f 6b  ':.        iCook
29a4e 69 65 20 3d 20 42 54 52 45 45 5f 46 52 45 45 5f  ie = BTREE_FREE_
29a4f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 20 20  PAGE_COUNT;.    
29a50 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
29a51 20 63 61 73 65 20 27 73 27 3a 20 63 61 73 65 20   case 's': case 
29a52 27 53 27 3a 0a 20 20 20 20 20 20 20 20 69 43 6f  'S':.        iCo
29a53 6f 6b 69 65 20 3d 20 42 54 52 45 45 5f 53 43 48  okie = BTREE_SCH
29a54 45 4d 41 5f 56 45 52 53 49 4f 4e 3b 0a 20 20 20  EMA_VERSION;.   
29a55 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29a56 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
29a57 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52     iCookie = BTR
29a58 45 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e 3b  EE_USER_VERSION;
29a59 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
29a5a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 7a      }..    if( z
29a5b 52 69 67 68 74 20 26 26 20 69 43 6f 6f 6b 69 65  Right && iCookie
29a5c 21 3d 42 54 52 45 45 5f 46 52 45 45 5f 50 41 47  !=BTREE_FREE_PAG
29a5d 45 5f 43 4f 55 4e 54 20 29 7b 0a 20 20 20 20 20  E_COUNT ){.     
29a5e 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73 70   /* Write the sp
29a5f 65 63 69 66 69 65 64 20 63 6f 6f 6b 69 65 20 76  ecified cookie v
29a60 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20 73 74  alue */.      st
29a61 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
29a62 70 4c 69 73 74 20 73 65 74 43 6f 6f 6b 69 65 5b  pList setCookie[
29a63 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b 20  ] = {.        { 
29a64 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20  OP_Transaction, 
29a65 20 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20 20     0,  1,  0},  
29a66 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20    /* 0 */.      
29a67 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20    { OP_Integer, 
29a68 20 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 30         0,  1,  0
29a69 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20  },    /* 1 */.  
29a6a 20 20 20 20 20 20 7b 20 4f 50 5f 53 65 74 43 6f        { OP_SetCo
29a6b 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20 30  okie,      0,  0
29a6c 2c 20 20 31 7d 2c 20 20 20 20 2f 2a 20 32 20 2a  ,  1},    /* 2 *
29a6d 2f 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20  /.      };.     
29a6e 20 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69   int addr = sqli
29a6f 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
29a70 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 73 65  (v, ArraySize(se
29a71 74 43 6f 6f 6b 69 65 29 2c 20 73 65 74 43 6f 6f  tCookie), setCoo
29a72 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  kie);.      sqli
29a73 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
29a74 76 2c 20 61 64 64 72 2c 20 69 44 62 29 3b 0a 20  v, addr, iDb);. 
29a75 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29a76 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
29a77 2b 31 2c 20 61 74 6f 69 28 7a 52 69 67 68 74 29  +1, atoi(zRight)
29a78 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29a79 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
29a7a 61 64 64 72 2b 32 2c 20 69 44 62 29 3b 0a 20 20  addr+2, iDb);.  
29a7b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29a7c 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b  hangeP2(v, addr+
29a7d 32 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20 20  2, iCookie);.   
29a7e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
29a7f 20 52 65 61 64 20 74 68 65 20 73 70 65 63 69 66   Read the specif
29a80 69 65 64 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65  ied cookie value
29a81 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63   */.      static
29a82 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73   const VdbeOpLis
29a83 74 20 72 65 61 64 43 6f 6f 6b 69 65 5b 5d 20 3d  t readCookie[] =
29a84 20 7b 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f   {.        { OP_
29a85 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20  Transaction,    
29a86 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 20 20   0,  0,  0},    
29a87 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 0 */.        
29a88 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c  { OP_ReadCookie,
29a89 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 30 7d        0,  1,  0}
29a8a 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20  ,    /* 1 */.   
29a8b 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74       { OP_Result
29a8c 52 6f 77 2c 20 20 20 20 20 20 20 31 2c 20 20 31  Row,       1,  1
29a8d 2c 20 20 30 7d 0a 20 20 20 20 20 20 7d 3b 0a 20  ,  0}.      };. 
29a8e 20 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20       int addr = 
29a8f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29a90 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a  List(v, ArraySiz
29a91 65 28 72 65 61 64 43 6f 6f 6b 69 65 29 2c 20 72  e(readCookie), r
29a92 65 61 64 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20  eadCookie);.    
29a93 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
29a94 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69  ngeP1(v, addr, i
29a95 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
29a96 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
29a97 2c 20 61 64 64 72 2b 31 2c 20 69 44 62 29 3b 0a  , addr+1, iDb);.
29a98 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29a99 65 43 68 61 6e 67 65 50 33 28 76 2c 20 61 64 64  eChangeP3(v, add
29a9a 72 2b 31 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20  r+1, iCookie);. 
29a9b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29a9c 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
29a9d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29a9e 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
29a9f 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
29aa0 2c 20 7a 4c 65 66 74 2c 20 53 51 4c 49 54 45 5f  , zLeft, SQLITE_
29aa1 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
29aa2 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
29aa3 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
29aa4 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50  SCHEMA_VERSION_P
29aa5 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 20 64  RAGMAS */..#if d
29aa6 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
29aa7 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  BUG) || defined(
29aa8 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 2f  SQLITE_TEST).  /
29aa9 2a 0a 20 20 2a 2a 20 52 65 70 6f 72 74 20 74 68  *.  ** Report th
29aaa 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  e current state 
29aab 6f 66 20 66 69 6c 65 20 6c 6f 67 73 20 66 6f 72  of file logs for
29aac 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 0a 20   all databases. 
29aad 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
29aae 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
29aaf 22 6c 6f 63 6b 5f 73 74 61 74 75 73 22 29 3d 3d  "lock_status")==
29ab0 30 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20  0 ){.    static 
29ab1 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
29ab2 74 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 5d 20 3d  t azLockName[] =
29ab3 20 7b 0a 20 20 20 20 20 20 22 75 6e 6c 6f 63 6b   {.      "unlock
29ab4 65 64 22 2c 20 22 73 68 61 72 65 64 22 2c 20 22  ed", "shared", "
29ab5 72 65 73 65 72 76 65 64 22 2c 20 22 70 65 6e 64  reserved", "pend
29ab6 69 6e 67 22 2c 20 22 65 78 63 6c 75 73 69 76 65  ing", "exclusive
29ab7 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74  ".    };.    int
29ab8 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   i;.    sqlite3V
29ab9 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
29aba 20 32 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d   2);.    pParse-
29abb 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20 20 20 73  >nMem = 2;.    s
29abc 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
29abd 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
29abe 4d 45 5f 4e 41 4d 45 2c 20 22 64 61 74 61 62 61  ME_NAME, "databa
29abf 73 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  se", SQLITE_STAT
29ac0 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
29ac1 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
29ac2 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 1, COLNAME_NAM
29ac3 45 2c 20 22 73 74 61 74 75 73 22 2c 20 53 51 4c  E, "status", SQL
29ac4 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
29ac5 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
29ac6 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
29ac7 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20   Btree *pBt;.   
29ac8 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72     Pager *pPager
29ac9 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
29aca 61 72 20 2a 7a 53 74 61 74 65 20 3d 20 22 75 6e  ar *zState = "un
29acb 6b 6e 6f 77 6e 22 3b 0a 20 20 20 20 20 20 69 6e  known";.      in
29acc 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 64  t j;.      if( d
29acd 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3d  b->aDb[i].zName=
29ace 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
29acf 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29ad0 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
29ad1 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 64  ing8, 0, 1, 0, d
29ad2 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c  b->aDb[i].zName,
29ad3 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
29ad4 20 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62     pBt = db->aDb
29ad5 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69  [i].pBt;.      i
29ad6 66 28 20 70 42 74 3d 3d 30 20 7c 7c 20 28 70 50  f( pBt==0 || (pP
29ad7 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
29ad8 72 65 65 50 61 67 65 72 28 70 42 74 29 29 3d 3d  reePager(pBt))==
29ad9 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 53 74  0 ){.        zSt
29ada 61 74 65 20 3d 20 22 63 6c 6f 73 65 64 22 3b 0a  ate = "closed";.
29adb 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
29adc 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
29add 74 72 6f 6c 28 64 62 2c 20 69 20 3f 20 64 62 2d  trol(db, i ? db-
29ade 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 20 3a 20  >aDb[i].zName : 
29adf 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  0, .            
29ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29ae1 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
29ae2 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 2c  FCNTL_LOCKSTATE,
29ae3 20 26 6a 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20   &j)==SQLITE_OK 
29ae4 29 7b 0a 20 20 20 20 20 20 20 20 20 7a 53 74 61  ){.         zSta
29ae5 74 65 20 3d 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b  te = azLockName[
29ae6 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  j];.      }.    
29ae7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29ae8 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
29ae9 38 2c 20 30 2c 20 32 2c 20 30 2c 20 7a 53 74 61  8, 0, 2, 0, zSta
29aea 74 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  te, P4_STATIC);.
29aeb 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29aec 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
29aed 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b 0a  sultRow, 1, 2);.
29aee 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 0a 23      }..  }else.#
29aef 65 6e 64 69 66 0a 0a 23 69 66 20 53 51 4c 49 54  endif..#if SQLIT
29af0 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66  E_HAS_CODEC.  if
29af1 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
29af2 28 7a 4c 65 66 74 2c 20 22 6b 65 79 22 29 3d 3d  (zLeft, "key")==
29af3 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20  0 && zRight ){. 
29af4 20 20 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 64     sqlite3_key(d
29af5 62 2c 20 7a 52 69 67 68 74 2c 20 73 71 6c 69 74  b, zRight, sqlit
29af6 65 33 53 74 72 6c 65 6e 33 30 28 7a 52 69 67 68  e3Strlen30(zRigh
29af7 74 29 29 3b 0a 20 20 7d 65 6c 73 65 0a 20 20 69  t));.  }else.  i
29af8 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
29af9 70 28 7a 4c 65 66 74 2c 20 22 72 65 6b 65 79 22  p(zLeft, "rekey"
29afa 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
29afb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
29afc 6b 65 79 28 64 62 2c 20 7a 52 69 67 68 74 2c 20  key(db, zRight, 
29afd 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
29afe 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73  zRight));.  }els
29aff 65 0a 20 20 69 66 28 20 7a 52 69 67 68 74 20 26  e.  if( zRight &
29b00 26 20 28 73 71 6c 69 74 65 33 53 74 72 49 43 6d  & (sqlite3StrICm
29b01 70 28 7a 4c 65 66 74 2c 20 22 68 65 78 6b 65 79  p(zLeft, "hexkey
29b02 22 29 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20  ")==0 ||.       
29b03 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29b04 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
29b05 22 68 65 78 72 65 6b 65 79 22 29 3d 3d 30 29 20  "hexrekey")==0) 
29b06 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 68 31  ){.    int i, h1
29b07 2c 20 68 32 3b 0a 20 20 20 20 63 68 61 72 20 7a  , h2;.    char z
29b08 4b 65 79 5b 34 30 5d 3b 0a 20 20 20 20 66 6f 72  Key[40];.    for
29b09 28 69 3d 30 3b 20 28 68 31 20 3d 20 7a 52 69 67  (i=0; (h1 = zRig
29b0a 68 74 5b 69 5d 29 21 3d 30 20 26 26 20 28 68 32  ht[i])!=0 && (h2
29b0b 20 3d 20 7a 52 69 67 68 74 5b 69 2b 31 5d 29 21   = zRight[i+1])!
29b0c 3d 30 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20  =0; i+=2){.     
29b0d 20 68 31 20 2b 3d 20 39 2a 28 31 26 28 68 31 3e   h1 += 9*(1&(h1>
29b0e 3e 36 29 29 3b 0a 20 20 20 20 20 20 68 32 20 2b  >6));.      h2 +
29b0f 3d 20 39 2a 28 31 26 28 68 32 3e 3e 36 29 29 3b  = 9*(1&(h2>>6));
29b10 0a 20 20 20 20 20 20 7a 4b 65 79 5b 69 2f 32 5d  .      zKey[i/2]
29b11 20 3d 20 28 68 32 20 26 20 30 78 30 66 29 20 7c   = (h2 & 0x0f) |
29b12 20 28 28 68 31 20 26 20 30 78 66 29 3c 3c 34 29   ((h1 & 0xf)<<4)
29b13 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
29b14 28 7a 4c 65 66 74 5b 33 5d 20 26 20 30 78 66 29  (zLeft[3] & 0xf)
29b15 3d 3d 30 78 62 20 29 7b 0a 20 20 20 20 20 20 73  ==0xb ){.      s
29b16 71 6c 69 74 65 33 5f 6b 65 79 28 64 62 2c 20 7a  qlite3_key(db, z
29b17 4b 65 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d  Key, i/2);.    }
29b18 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
29b19 74 65 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a 4b  te3_rekey(db, zK
29b1a 65 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d 0a  ey, i/2);.    }.
29b1b 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 23    }else.#endif.#
29b1c 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  if SQLITE_HAS_CO
29b1d 44 45 43 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  DEC || defined(S
29b1e 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52  QLITE_ENABLE_CER
29b1f 4f 44 29 0a 20 20 69 66 28 20 73 71 6c 69 74 65  OD).  if( sqlite
29b20 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
29b21 22 61 63 74 69 76 61 74 65 5f 65 78 74 65 6e 73  "activate_extens
29b22 69 6f 6e 73 22 29 3d 3d 30 20 29 7b 0a 23 69 66  ions")==0 ){.#if
29b23 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
29b24 43 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  C.    if( sqlite
29b25 33 53 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74  3StrNICmp(zRight
29b26 2c 20 22 73 65 65 2d 22 2c 20 34 29 3d 3d 30 20  , "see-", 4)==0 
29b27 29 7b 0a 20 20 20 20 20 20 65 78 74 65 72 6e 20  ){.      extern 
29b28 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 61 63 74  void sqlite3_act
29b29 69 76 61 74 65 5f 73 65 65 28 63 6f 6e 73 74 20  ivate_see(const 
29b2a 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 73 71  char*);.      sq
29b2b 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 73  lite3_activate_s
29b2c 65 65 28 26 7a 52 69 67 68 74 5b 34 5d 29 3b 0a  ee(&zRight[4]);.
29b2d 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66      }.#endif.#if
29b2e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
29b2f 45 5f 43 45 52 4f 44 0a 20 20 20 20 69 66 28 20  E_CEROD.    if( 
29b30 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
29b31 7a 52 69 67 68 74 2c 20 22 63 65 72 6f 64 2d 22  zRight, "cerod-"
29b32 2c 20 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 6)==0 ){.     
29b33 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c   extern void sql
29b34 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65  ite3_activate_ce
29b35 72 6f 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  rod(const char*)
29b36 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
29b37 61 63 74 69 76 61 74 65 5f 63 65 72 6f 64 28 26  activate_cerod(&
29b38 7a 52 69 67 68 74 5b 36 5d 29 3b 0a 20 20 20 20  zRight[6]);.    
29b39 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65  }.#endif.  }else
29b3a 0a 23 65 6e 64 69 66 0a 0a 20 0a 20 20 7b 2f 2a  .#endif.. .  {/*
29b3b 20 45 6d 70 74 79 20 45 4c 53 45 20 63 6c 61 75   Empty ELSE clau
29b3c 73 65 20 2a 2f 7d 0a 0a 20 20 2f 2a 20 43 6f 64  se */}..  /* Cod
29b3d 65 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 20 61  e an OP_Expire a
29b3e 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63  t the end of eac
29b3f 68 20 50 52 41 47 4d 41 20 70 72 6f 67 72 61 6d  h PRAGMA program
29b40 20 74 6f 20 63 61 75 73 65 0a 20 20 2a 2a 20 74   to cause.  ** t
29b41 68 65 20 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e  he VDBE implemen
29b42 74 69 6e 67 20 74 68 65 20 70 72 61 67 6d 61 20  ting the pragma 
29b43 74 6f 20 65 78 70 69 72 65 2e 20 4d 6f 73 74 20  to expire. Most 
29b44 28 61 6c 6c 3f 29 20 70 72 61 67 6d 61 73 0a 20  (all?) pragmas. 
29b45 20 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c   ** are only val
29b46 69 64 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  id for a single 
29b47 65 78 65 63 75 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  execution..  */.
29b48 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29b49 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65  Op2(v, OP_Expire
29b4a 2c 20 31 2c 20 30 29 3b 0a 0a 20 20 2f 2a 0a 20  , 1, 0);..  /*. 
29b4b 20 2a 2a 20 52 65 73 65 74 20 74 68 65 20 73 61   ** Reset the sa
29b4c 66 65 74 79 20 6c 65 76 65 6c 2c 20 69 6e 20 63  fety level, in c
29b4d 61 73 65 20 74 68 65 20 66 75 6c 6c 66 73 79 6e  ase the fullfsyn
29b4e 63 20 66 6c 61 67 20 6f 72 20 73 79 6e 63 68 72  c flag or synchr
29b4f 6f 6e 6f 75 73 0a 20 20 2a 2a 20 73 65 74 74 69  onous.  ** setti
29b50 6e 67 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f  ng changed..  */
29b51 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
29b52 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
29b53 41 53 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74  AS.  if( db->aut
29b54 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73  oCommit ){.    s
29b55 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61  qlite3BtreeSetSa
29b56 66 65 74 79 4c 65 76 65 6c 28 70 44 62 2d 3e 70  fetyLevel(pDb->p
29b57 42 74 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f  Bt, pDb->safety_
29b58 6c 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20 20  level,.         
29b59 20 20 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73        (db->flags
29b5a 26 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e  &SQLITE_FullFSyn
29b5b 63 29 21 3d 30 29 3b 0a 20 20 7d 0a 23 65 6e 64  c)!=0);.  }.#end
29b5c 69 66 0a 70 72 61 67 6d 61 5f 6f 75 74 3a 0a 20  if.pragma_out:. 
29b5d 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
29b5e 62 2c 20 7a 4c 65 66 74 29 3b 0a 20 20 73 71 6c  b, zLeft);.  sql
29b5f 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
29b60 52 69 67 68 74 29 3b 0a 7d 0a 0a 23 65 6e 64 69  Right);.}..#endi
29b61 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
29b62 5f 50 52 41 47 4d 41 20 2a 2f 0a 0a 2f 2a 2a 2a  _PRAGMA */../***
29b63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
29b64 6f 66 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a  of pragma.c ****
29b65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
29b68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
29b69 6e 20 66 69 6c 65 20 70 72 65 70 61 72 65 2e 63  n file prepare.c
29b6a 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
29b6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
29b6d 2a 20 32 30 30 35 20 4d 61 79 20 32 35 0a 2a 2a  * 2005 May 25.**
29b6e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
29b6f 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
29b70 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
29b71 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
29b72 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
29b73 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
29b74 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
29b75 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
29b76 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
29b77 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
29b78 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
29b79 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
29b7a 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
29b7b 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
29b7c 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
29b7d 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
29b7e 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
29b7f 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
29b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
29b84 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
29b85 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ns the implement
29b86 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
29b87 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 0a 2a  ite3_prepare().*
29b88 2a 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 64  * interface, and
29b89 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 63   routines that c
29b8a 6f 6e 74 72 69 62 75 74 65 20 74 6f 20 6c 6f 61  ontribute to loa
29b8b 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73  ding the databas
29b8c 65 20 73 63 68 65 6d 61 0a 2a 2a 20 66 72 6f 6d  e schema.** from
29b8d 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 24 49 64   disk..**.** $Id
29b8e 3a 20 70 72 65 70 61 72 65 2e 63 2c 76 20 31 2e  : prepare.c,v 1.
29b8f 31 33 31 20 32 30 30 39 2f 30 38 2f 30 36 20 31  131 2009/08/06 1
29b90 37 3a 34 33 3a 33 31 20 64 72 68 20 45 78 70 20  7:43:31 drh Exp 
29b91 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c  $.*/../*.** Fill
29b92 20 74 68 65 20 49 6e 69 74 44 61 74 61 20 73 74   the InitData st
29b93 72 75 63 74 75 72 65 20 77 69 74 68 20 61 6e 20  ructure with an 
29b94 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 68  error message th
29b95 61 74 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20  at indicates.** 
29b96 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73  that the databas
29b97 65 20 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f  e is corrupt..*/
29b98 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 72  .static void cor
29b99 72 75 70 74 53 63 68 65 6d 61 28 0a 20 20 49 6e  ruptSchema(.  In
29b9a 69 74 44 61 74 61 20 2a 70 44 61 74 61 2c 20 20  itData *pData,  
29b9b 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 61     /* Initializa
29b9c 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  tion context */.
29b9d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f    const char *zO
29b9e 62 6a 2c 20 20 20 20 2f 2a 20 4f 62 6a 65 63 74  bj,    /* Object
29b9f 20 62 65 69 6e 67 20 70 61 72 73 65 64 20 61 74   being parsed at
29ba0 20 74 68 65 20 70 6f 69 6e 74 20 6f 66 20 65 72   the point of er
29ba1 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ror */.  const c
29ba2 68 61 72 20 2a 7a 45 78 74 72 61 20 20 20 2f 2a  har *zExtra   /*
29ba3 20 45 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69   Error informati
29ba4 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  on */.){.  sqlit
29ba5 65 33 20 2a 64 62 20 3d 20 70 44 61 74 61 2d 3e  e3 *db = pData->
29ba6 64 62 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d  db;.  if( !db->m
29ba7 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 28  allocFailed && (
29ba8 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
29ba9 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 29  TE_RecoveryMode)
29baa 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a  ==0 ){.    if( z
29bab 4f 62 6a 3d 3d 30 20 29 20 7a 4f 62 6a 20 3d 20  Obj==0 ) zObj = 
29bac 22 3f 22 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  "?";.    sqlite3
29bad 53 65 74 53 74 72 69 6e 67 28 70 44 61 74 61 2d  SetString(pData-
29bae 3e 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 0a 20  >pzErrMsg, db,. 
29baf 20 20 20 20 20 22 6d 61 6c 66 6f 72 6d 65 64 20       "malformed 
29bb0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
29bb1 28 25 73 29 22 2c 20 7a 4f 62 6a 29 3b 0a 20 20  (%s)", zObj);.  
29bb2 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a    if( zExtra ){.
29bb3 20 20 20 20 20 20 2a 70 44 61 74 61 2d 3e 70 7a        *pData->pz
29bb4 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
29bb5 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 2a 70 44  MAppendf(db, *pD
29bb6 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 0a  ata->pzErrMsg, .
29bb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29bb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29bb9 20 22 25 73 20 2d 20 25 73 22 2c 20 2a 70 44 61   "%s - %s", *pDa
29bba 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 7a 45  ta->pzErrMsg, zE
29bbb 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  xtra);.    }.  }
29bbc 0a 20 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 64  .  pData->rc = d
29bbd 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
29bbe 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 3a  ? SQLITE_NOMEM :
29bbf 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b   SQLITE_CORRUPT;
29bc0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
29bc1 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72  s the callback r
29bc2 6f 75 74 69 6e 65 20 66 6f 72 20 74 68 65 20 63  outine for the c
29bc3 6f 64 65 20 74 68 61 74 20 69 6e 69 74 69 61 6c  ode that initial
29bc4 69 7a 65 73 20 74 68 65 0a 2a 2a 20 64 61 74 61  izes the.** data
29bc5 62 61 73 65 2e 20 20 53 65 65 20 73 71 6c 69 74  base.  See sqlit
29bc6 65 33 49 6e 69 74 28 29 20 62 65 6c 6f 77 20 66  e3Init() below f
29bc7 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
29bc8 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68  formation..** Th
29bc9 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6c  is routine is al
29bca 73 6f 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74  so called from t
29bcb 68 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  he OP_ParseSchem
29bcc 61 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20  a opcode of the 
29bcd 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68  VDBE..**.** Each
29bce 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69   callback contai
29bcf 6e 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ns the following
29bd0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a   information:.**
29bd1 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 30 5d 20  .**     argv[0] 
29bd2 3d 20 6e 61 6d 65 20 6f 66 20 74 68 69 6e 67 20  = name of thing 
29bd3 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a  being created.**
29bd4 20 20 20 20 20 61 72 67 76 5b 31 5d 20 3d 20 72       argv[1] = r
29bd5 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
29bd6 66 6f 72 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  for table or ind
29bd7 65 78 2e 20 30 20 66 6f 72 20 74 72 69 67 67 65  ex. 0 for trigge
29bd8 72 20 6f 72 20 76 69 65 77 2e 0a 2a 2a 20 20 20  r or view..**   
29bd9 20 20 61 72 67 76 5b 32 5d 20 3d 20 53 51 4c 20    argv[2] = SQL 
29bda 74 65 78 74 20 66 6f 72 20 74 68 65 20 43 52 45  text for the CRE
29bdb 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ATE statement..*
29bdc 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  *.*/.SQLITE_PRIV
29bdd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
29bde 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64  nitCallback(void
29bdf 20 2a 70 49 6e 69 74 2c 20 69 6e 74 20 61 72 67   *pInit, int arg
29be0 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20  c, char **argv, 
29be1 63 68 61 72 20 2a 2a 4e 6f 74 55 73 65 64 29 7b  char **NotUsed){
29be2 0a 20 20 49 6e 69 74 44 61 74 61 20 2a 70 44 61  .  InitData *pDa
29be3 74 61 20 3d 20 28 49 6e 69 74 44 61 74 61 2a 29  ta = (InitData*)
29be4 70 49 6e 69 74 3b 0a 20 20 73 71 6c 69 74 65 33  pInit;.  sqlite3
29be5 20 2a 64 62 20 3d 20 70 44 61 74 61 2d 3e 64 62   *db = pData->db
29be6 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 44  ;.  int iDb = pD
29be7 61 74 61 2d 3e 69 44 62 3b 0a 0a 20 20 61 73 73  ata->iDb;..  ass
29be8 65 72 74 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a  ert( argc==3 );.
29be9 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
29bea 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 61 72 67  ER2(NotUsed, arg
29beb 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  c);.  assert( sq
29bec 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
29bed 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
29bee 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79   DbClearProperty
29bef 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d 70  (db, iDb, DB_Emp
29bf0 74 79 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  ty);.  if( db->m
29bf1 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
29bf2 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61     corruptSchema
29bf3 28 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c  (pData, argv[0],
29bf4 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   0);.    return 
29bf5 31 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  1;.  }..  assert
29bf6 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c  ( iDb>=0 && iDb<
29bf7 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69 66 28  db->nDb );.  if(
29bf8 20 61 72 67 76 3d 3d 30 20 29 20 72 65 74 75 72   argv==0 ) retur
29bf9 6e 20 30 3b 20 20 20 2f 2a 20 4d 69 67 68 74 20  n 0;   /* Might 
29bfa 68 61 70 70 65 6e 20 69 66 20 45 4d 50 54 59 5f  happen if EMPTY_
29bfb 52 45 53 55 4c 54 5f 43 41 4c 4c 42 41 43 4b 53  RESULT_CALLBACKS
29bfc 20 61 72 65 20 6f 6e 20 2a 2f 0a 20 20 69 66 28   are on */.  if(
29bfd 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20   argv[1]==0 ){. 
29bfe 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61     corruptSchema
29bff 28 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c  (pData, argv[0],
29c00 20 30 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   0);.  }else if(
29c01 20 61 72 67 76 5b 32 5d 20 26 26 20 61 72 67 76   argv[2] && argv
29c02 5b 32 5d 5b 30 5d 20 29 7b 0a 20 20 20 20 2f 2a  [2][0] ){.    /*
29c03 20 43 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72   Call the parser
29c04 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20 43 52   to process a CR
29c05 45 41 54 45 20 54 41 42 4c 45 2c 20 49 4e 44 45  EATE TABLE, INDE
29c06 58 20 6f 72 20 56 49 45 57 2e 0a 20 20 20 20 2a  X or VIEW..    *
29c07 2a 20 42 75 74 20 62 65 63 61 75 73 65 20 64 62  * But because db
29c08 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 73  ->init.busy is s
29c09 65 74 20 74 6f 20 31 2c 20 6e 6f 20 56 44 42 45  et to 1, no VDBE
29c0a 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74   code is generat
29c0b 65 64 0a 20 20 20 20 2a 2a 20 6f 72 20 65 78 65  ed.    ** or exe
29c0c 63 75 74 65 64 2e 20 20 41 6c 6c 20 74 68 65 20  cuted.  All the 
29c0d 70 61 72 73 65 72 20 64 6f 65 73 20 69 73 20 62  parser does is b
29c0e 75 69 6c 64 20 74 68 65 20 69 6e 74 65 72 6e 61  uild the interna
29c0f 6c 20 64 61 74 61 0a 20 20 20 20 2a 2a 20 73 74  l data.    ** st
29c10 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64 65  ructures that de
29c11 73 63 72 69 62 65 20 74 68 65 20 74 61 62 6c 65  scribe the table
29c12 2c 20 69 6e 64 65 78 2c 20 6f 72 20 76 69 65 77  , index, or view
29c13 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61  ..    */.    cha
29c14 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20 69 6e 74  r *zErr;.    int
29c15 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72 74 28   rc;.    assert(
29c16 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29   db->init.busy )
29c17 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 69  ;.    db->init.i
29c18 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 64 62  Db = iDb;.    db
29c19 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 20 3d  ->init.newTnum =
29c1a 20 61 74 6f 69 28 61 72 67 76 5b 31 5d 29 3b 0a   atoi(argv[1]);.
29c1b 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6f 72 70      db->init.orp
29c1c 68 61 6e 54 72 69 67 67 65 72 20 3d 20 30 3b 0a  hanTrigger = 0;.
29c1d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
29c1e 5f 65 78 65 63 28 64 62 2c 20 61 72 67 76 5b 32  _exec(db, argv[2
29c1f 5d 2c 20 30 2c 20 30 2c 20 26 7a 45 72 72 29 3b  ], 0, 0, &zErr);
29c20 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 69 44  .    db->init.iD
29c21 62 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72  b = 0;.    asser
29c22 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc!=SQLITE_OK
29c23 20 7c 7c 20 7a 45 72 72 3d 3d 30 20 29 3b 0a 20   || zErr==0 );. 
29c24 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
29c25 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 69 66  !=rc ){.      if
29c26 28 20 64 62 2d 3e 69 6e 69 74 2e 6f 72 70 68 61  ( db->init.orpha
29c27 6e 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  nTrigger ){.    
29c28 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3d      assert( iDb=
29c29 3d 31 20 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  =1 );.      }els
29c2a 65 7b 0a 20 20 20 20 20 20 20 20 70 44 61 74 61  e{.        pData
29c2b 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20  ->rc = rc;.     
29c2c 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
29c2d 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20  E_NOMEM ){.     
29c2e 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
29c2f 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
29c30 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21     }else if( rc!
29c31 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50  =SQLITE_INTERRUP
29c32 54 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  T && rc!=SQLITE_
29c33 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20 20 20  LOCKED ){.      
29c34 20 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d      corruptSchem
29c35 61 28 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d  a(pData, argv[0]
29c36 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 20  , zErr);.       
29c37 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
29c38 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
29c39 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a  b, zErr);.    }.
29c3a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 72 67 76    }else if( argv
29c3b 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f  [0]==0 ){.    co
29c3c 72 72 75 70 74 53 63 68 65 6d 61 28 70 44 61 74  rruptSchema(pDat
29c3d 61 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 65 6c 73  a, 0, 0);.  }els
29c3e 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  e{.    /* If the
29c3f 20 53 51 4c 20 63 6f 6c 75 6d 6e 20 69 73 20 62   SQL column is b
29c40 6c 61 6e 6b 20 69 74 20 6d 65 61 6e 73 20 74 68  lank it means th
29c41 69 73 20 69 73 20 61 6e 20 69 6e 64 65 78 20 74  is is an index t
29c42 68 61 74 0a 20 20 20 20 2a 2a 20 77 61 73 20 63  hat.    ** was c
29c43 72 65 61 74 65 64 20 74 6f 20 62 65 20 74 68 65  reated to be the
29c44 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
29c45 74 6f 20 66 75 6c 66 69 6c 6c 20 61 20 55 4e 49  to fulfill a UNI
29c46 51 55 45 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 74  QUE.    ** const
29c47 72 61 69 6e 74 20 66 6f 72 20 61 20 43 52 45 41  raint for a CREA
29c48 54 45 20 54 41 42 4c 45 2e 20 20 54 68 65 20 69  TE TABLE.  The i
29c49 6e 64 65 78 20 73 68 6f 75 6c 64 20 68 61 76 65  ndex should have
29c4a 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20   already.    ** 
29c4b 62 65 65 6e 20 63 72 65 61 74 65 64 20 77 68 65  been created whe
29c4c 6e 20 77 65 20 70 72 6f 63 65 73 73 65 64 20 74  n we processed t
29c4d 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e  he CREATE TABLE.
29c4e 20 20 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20    All we have.  
29c4f 20 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20    ** to do here 
29c50 69 73 20 72 65 63 6f 72 64 20 74 68 65 20 72 6f  is record the ro
29c51 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66  ot page number f
29c52 6f 72 20 74 68 61 74 20 69 6e 64 65 78 2e 0a 20  or that index.. 
29c53 20 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20     */.    Index 
29c54 2a 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 49 6e  *pIndex;.    pIn
29c55 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  dex = sqlite3Fin
29c56 64 49 6e 64 65 78 28 64 62 2c 20 61 72 67 76 5b  dIndex(db, argv[
29c57 30 5d 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  0], db->aDb[iDb]
29c58 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28  .zName);.    if(
29c59 20 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20   pIndex==0 ){.  
29c5a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6e 20      /* This can 
29c5b 6f 63 63 75 72 20 69 66 20 74 68 65 72 65 20 65  occur if there e
29c5c 78 69 73 74 73 20 61 6e 20 69 6e 64 65 78 20 6f  xists an index o
29c5d 6e 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 77  n a TEMP table w
29c5e 68 69 63 68 0a 20 20 20 20 20 20 2a 2a 20 68 61  hich.      ** ha
29c5f 73 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20  s the same name 
29c60 61 73 20 61 6e 6f 74 68 65 72 20 69 6e 64 65 78  as another index
29c61 20 6f 6e 20 61 20 70 65 72 6d 61 6e 65 6e 74 20   on a permanent 
29c62 69 6e 64 65 78 2e 20 20 53 69 6e 63 65 0a 20 20  index.  Since.  
29c63 20 20 20 20 2a 2a 20 74 68 65 20 70 65 72 6d 61      ** the perma
29c64 6e 65 6e 74 20 74 61 62 6c 65 20 69 73 20 68 69  nent table is hi
29c65 64 64 65 6e 20 62 79 20 74 68 65 20 54 45 4d 50  dden by the TEMP
29c66 20 74 61 62 6c 65 2c 20 77 65 20 63 61 6e 20 61   table, we can a
29c67 6c 73 6f 0a 20 20 20 20 20 20 2a 2a 20 73 61 66  lso.      ** saf
29c68 65 6c 79 20 69 67 6e 6f 72 65 20 74 68 65 20 69  ely ignore the i
29c69 6e 64 65 78 20 6f 6e 20 74 68 65 20 70 65 72 6d  ndex on the perm
29c6a 61 6e 65 6e 74 20 74 61 62 6c 65 2e 0a 20 20 20  anent table..   
29c6b 20 20 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 44     */.      /* D
29c6c 6f 20 4e 6f 74 68 69 6e 67 20 2a 2f 3b 0a 20 20  o Nothing */;.  
29c6d 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
29c6e 74 65 33 47 65 74 49 6e 74 33 32 28 61 72 67 76  te3GetInt32(argv
29c6f 5b 31 5d 2c 20 26 70 49 6e 64 65 78 2d 3e 74 6e  [1], &pIndex->tn
29c70 75 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  um)==0 ){.      
29c71 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44  corruptSchema(pD
29c72 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20 22 69  ata, argv[0], "i
29c73 6e 76 61 6c 69 64 20 72 6f 6f 74 70 61 67 65 22  nvalid rootpage"
29c74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
29c75 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
29c76 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61  * Attempt to rea
29c77 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
29c78 63 68 65 6d 61 20 61 6e 64 20 69 6e 69 74 69 61  chema and initia
29c79 6c 69 7a 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a  lize internal.**
29c7a 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
29c7b 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61   for a single da
29c7c 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68  tabase file.  Th
29c7d 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a  e index of the.*
29c7e 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
29c7f 69 73 20 67 69 76 65 6e 20 62 79 20 69 44 62 2e  is given by iDb.
29c80 20 20 69 44 62 3d 3d 30 20 69 73 20 75 73 65 64    iDb==0 is used
29c81 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 0a 2a 2a   for the main.**
29c82 20 64 61 74 61 62 61 73 65 2e 20 20 69 44 62 3d   database.  iDb=
29c83 3d 31 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  =1 should never 
29c84 62 65 20 75 73 65 64 2e 20 20 69 44 62 3e 3d 32  be used.  iDb>=2
29c85 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20   is used for.** 
29c86 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
29c87 73 65 73 2e 20 20 52 65 74 75 72 6e 20 6f 6e 65  ses.  Return one
29c88 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 20   of the SQLITE_ 
29c89 65 72 72 6f 72 20 63 6f 64 65 73 20 74 6f 0a 2a  error codes to.*
29c8a 2a 20 69 6e 64 69 63 61 74 65 20 73 75 63 63 65  * indicate succe
29c8b 73 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a  ss or failure..*
29c8c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
29c8d 69 74 65 33 49 6e 69 74 4f 6e 65 28 73 71 6c 69  ite3InitOne(sqli
29c8e 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
29c8f 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73  , char **pzErrMs
29c90 67 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  g){.  int rc;.  
29c91 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 73 69 7a  int i;.  int siz
29c92 65 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  e;.  Table *pTab
29c93 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 63  ;.  Db *pDb;.  c
29c94 68 61 72 20 63 6f 6e 73 74 20 2a 61 7a 41 72 67  har const *azArg
29c95 5b 34 5d 3b 0a 20 20 69 6e 74 20 6d 65 74 61 5b  [4];.  int meta[
29c96 35 5d 3b 0a 20 20 49 6e 69 74 44 61 74 61 20 69  5];.  InitData i
29c97 6e 69 74 44 61 74 61 3b 0a 20 20 63 68 61 72 20  nitData;.  char 
29c98 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65 72 53 63  const *zMasterSc
29c99 68 65 6d 61 3b 0a 20 20 63 68 61 72 20 63 6f 6e  hema;.  char con
29c9a 73 74 20 2a 7a 4d 61 73 74 65 72 4e 61 6d 65 20  st *zMasterName 
29c9b 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  = SCHEMA_TABLE(i
29c9c 44 62 29 3b 0a 20 20 69 6e 74 20 6f 70 65 6e 65  Db);.  int opene
29c9d 64 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 30  dTransaction = 0
29c9e 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65  ;..  /*.  ** The
29c9f 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65   master database
29ca0 20 74 61 62 6c 65 20 68 61 73 20 61 20 73 74 72   table has a str
29ca1 75 63 74 75 72 65 20 6c 69 6b 65 20 74 68 69 73  ucture like this
29ca2 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63  .  */.  static c
29ca3 6f 6e 73 74 20 63 68 61 72 20 6d 61 73 74 65 72  onst char master
29ca4 5f 73 63 68 65 6d 61 5b 5d 20 3d 20 0a 20 20 20  _schema[] = .   
29ca5 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20    "CREATE TABLE 
29ca6 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 28 5c 6e  sqlite_master(\n
29ca7 22 0a 20 20 20 20 20 22 20 20 74 79 70 65 20 74  ".     "  type t
29ca8 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ext,\n".     "  
29ca9 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  name text,\n".  
29caa 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74     "  tbl_name t
29cab 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ext,\n".     "  
29cac 72 6f 6f 74 70 61 67 65 20 69 6e 74 65 67 65 72  rootpage integer
29cad 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 73 71 6c  ,\n".     "  sql
29cae 20 74 65 78 74 5c 6e 22 0a 20 20 20 20 20 22 29   text\n".     ")
29caf 22 0a 20 20 3b 0a 23 69 66 6e 64 65 66 20 53 51  ".  ;.#ifndef SQ
29cb0 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42  LITE_OMIT_TEMPDB
29cb1 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
29cb2 63 68 61 72 20 74 65 6d 70 5f 6d 61 73 74 65 72  char temp_master
29cb3 5f 73 63 68 65 6d 61 5b 5d 20 3d 20 0a 20 20 20  _schema[] = .   
29cb4 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 54    "CREATE TEMP T
29cb5 41 42 4c 45 20 73 71 6c 69 74 65 5f 74 65 6d 70  ABLE sqlite_temp
29cb6 5f 6d 61 73 74 65 72 28 5c 6e 22 0a 20 20 20 20  _master(\n".    
29cb7 20 22 20 20 74 79 70 65 20 74 65 78 74 2c 5c 6e   "  type text,\n
29cb8 22 0a 20 20 20 20 20 22 20 20 6e 61 6d 65 20 74  ".     "  name t
29cb9 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ext,\n".     "  
29cba 74 62 6c 5f 6e 61 6d 65 20 74 65 78 74 2c 5c 6e  tbl_name text,\n
29cbb 22 0a 20 20 20 20 20 22 20 20 72 6f 6f 74 70 61  ".     "  rootpa
29cbc 67 65 20 69 6e 74 65 67 65 72 2c 5c 6e 22 0a 20  ge integer,\n". 
29cbd 20 20 20 20 22 20 20 73 71 6c 20 74 65 78 74 5c      "  sql text\
29cbe 6e 22 0a 20 20 20 20 20 22 29 22 0a 20 20 3b 0a  n".     ")".  ;.
29cbf 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
29cc0 74 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68 65  temp_master_sche
29cc1 6d 61 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20 61  ma 0.#endif..  a
29cc2 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
29cc3 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
29cc4 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
29cc5 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 29  b[iDb].pSchema )
29cc6 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
29cc7 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
29cc8 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
29cc9 73 73 65 72 74 28 20 69 44 62 3d 3d 31 20 7c 7c  ssert( iDb==1 ||
29cca 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
29ccb 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b  dsMutex(db->aDb[
29ccc 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a 20 20  iDb].pBt) );..  
29ccd 2f 2a 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61  /* zMasterSchema
29cce 20 61 6e 64 20 7a 49 6e 69 74 53 63 72 69 70 74   and zInitScript
29ccf 20 61 72 65 20 73 65 74 20 74 6f 20 70 6f 69 6e   are set to poin
29cd0 74 20 61 74 20 74 68 65 20 6d 61 73 74 65 72 20  t at the master 
29cd1 73 63 68 65 6d 61 0a 20 20 2a 2a 20 61 6e 64 20  schema.  ** and 
29cd2 69 6e 69 74 69 61 6c 69 73 61 74 69 6f 6e 20 73  initialisation s
29cd3 63 72 69 70 74 20 61 70 70 72 6f 70 72 69 61 74  cript appropriat
29cd4 65 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61  e for the databa
29cd5 73 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69 6e  se being.  ** in
29cd6 69 74 69 61 6c 69 73 65 64 2e 20 7a 4d 61 73 74  itialised. zMast
29cd7 65 72 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61  erName is the na
29cd8 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  me of the master
29cd9 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69   table..  */.  i
29cda 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
29cdb 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20  && iDb==1 ){.   
29cdc 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20 3d   zMasterSchema =
29cdd 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68   temp_master_sch
29cde 65 6d 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ema;.  }else{.  
29cdf 20 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20    zMasterSchema 
29ce0 3d 20 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 3b  = master_schema;
29ce1 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 4e 61  .  }.  zMasterNa
29ce2 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c  me = SCHEMA_TABL
29ce3 45 28 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 43 6f  E(iDb);..  /* Co
29ce4 6e 73 74 72 75 63 74 20 74 68 65 20 73 63 68 65  nstruct the sche
29ce5 6d 61 20 74 61 62 6c 65 73 2e 20 20 2a 2f 0a 20  ma tables.  */. 
29ce6 20 61 7a 41 72 67 5b 30 5d 20 3d 20 7a 4d 61 73   azArg[0] = zMas
29ce7 74 65 72 4e 61 6d 65 3b 0a 20 20 61 7a 41 72 67  terName;.  azArg
29ce8 5b 31 5d 20 3d 20 22 31 22 3b 0a 20 20 61 7a 41  [1] = "1";.  azA
29ce9 72 67 5b 32 5d 20 3d 20 7a 4d 61 73 74 65 72 53  rg[2] = zMasterS
29cea 63 68 65 6d 61 3b 0a 20 20 61 7a 41 72 67 5b 33  chema;.  azArg[3
29ceb 5d 20 3d 20 30 3b 0a 20 20 69 6e 69 74 44 61 74  ] = 0;.  initDat
29cec 61 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 6e 69  a.db = db;.  ini
29ced 74 44 61 74 61 2e 69 44 62 20 3d 20 69 44 62 3b  tData.iDb = iDb;
29cee 0a 20 20 69 6e 69 74 44 61 74 61 2e 72 63 20 3d  .  initData.rc =
29cef 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
29cf0 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 20  itData.pzErrMsg 
29cf1 3d 20 70 7a 45 72 72 4d 73 67 3b 0a 20 20 28 76  = pzErrMsg;.  (v
29cf2 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
29cf3 79 4f 66 66 28 64 62 29 3b 0a 20 20 73 71 6c 69  yOff(db);.  sqli
29cf4 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28  te3InitCallback(
29cf5 26 69 6e 69 74 44 61 74 61 2c 20 33 2c 20 28 63  &initData, 3, (c
29cf6 68 61 72 20 2a 2a 29 61 7a 41 72 67 2c 20 30 29  har **)azArg, 0)
29cf7 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65  ;.  (void)sqlite
29cf8 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
29cf9 20 69 66 28 20 69 6e 69 74 44 61 74 61 2e 72 63   if( initData.rc
29cfa 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 69 6e 69   ){.    rc = ini
29cfb 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 67 6f  tData.rc;.    go
29cfc 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  to error_out;.  
29cfd 7d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  }.  pTab = sqlit
29cfe 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
29cff 7a 4d 61 73 74 65 72 4e 61 6d 65 2c 20 64 62 2d  zMasterName, db-
29d00 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
29d01 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  ;.  if( ALWAYS(p
29d02 54 61 62 29 20 29 7b 0a 20 20 20 20 70 54 61 62  Tab) ){.    pTab
29d03 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46  ->tabFlags |= TF
29d04 5f 52 65 61 64 6f 6e 6c 79 3b 0a 20 20 7d 0a 0a  _Readonly;.  }..
29d05 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 63 75    /* Create a cu
29d06 72 73 6f 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  rsor to hold the
29d07 20 64 61 74 61 62 61 73 65 20 6f 70 65 6e 0a 20   database open. 
29d08 20 2a 2f 0a 20 20 70 44 62 20 3d 20 26 64 62 2d   */.  pDb = &db-
29d09 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66 28  >aDb[iDb];.  if(
29d0a 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a   pDb->pBt==0 ){.
29d0b 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45      if( !OMIT_TE
29d0c 4d 50 44 42 20 26 26 20 41 4c 57 41 59 53 28 69  MPDB && ALWAYS(i
29d0d 44 62 3d 3d 31 29 20 29 7b 0a 20 20 20 20 20 20  Db==1) ){.      
29d0e 44 62 53 65 74 50 72 6f 70 65 72 74 79 28 64 62  DbSetProperty(db
29d0f 2c 20 31 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f  , 1, DB_SchemaLo
29d10 61 64 65 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20  aded);.    }.   
29d11 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
29d12 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  K;.  }..  /* If 
29d13 74 68 65 72 65 20 69 73 20 6e 6f 74 20 61 6c 72  there is not alr
29d14 65 61 64 79 20 61 20 72 65 61 64 2d 6f 6e 6c 79  eady a read-only
29d15 20 28 6f 72 20 72 65 61 64 2d 77 72 69 74 65 29   (or read-write)
29d16 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
29d17 6e 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  ned.  ** on the 
29d18 62 2d 74 72 65 65 20 64 61 74 61 62 61 73 65 2c  b-tree database,
29d19 20 6f 70 65 6e 20 6f 6e 65 20 6e 6f 77 2e 20 49   open one now. I
29d1a 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
29d1b 69 73 20 6f 70 65 6e 65 64 2c 20 69 74 20 0a 20  is opened, it . 
29d1c 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73   ** will be clos
29d1d 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66  ed before this f
29d1e 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e  unction returns.
29d1f 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74    */.  sqlite3Bt
29d20 72 65 65 45 6e 74 65 72 28 70 44 62 2d 3e 70 42  reeEnter(pDb->pB
29d21 74 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74  t);.  if( !sqlit
29d22 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54  e3BtreeIsInReadT
29d23 72 61 6e 73 28 70 44 62 2d 3e 70 42 74 29 20 29  rans(pDb->pBt) )
29d24 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
29d25 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e  e3BtreeBeginTran
29d26 73 28 70 44 62 2d 3e 70 42 74 2c 20 30 29 3b 0a  s(pDb->pBt, 0);.
29d27 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
29d28 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
29d29 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
29d2a 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  pzErrMsg, db, "%
29d2b 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74  s", sqlite3ErrSt
29d2c 72 28 72 63 29 29 3b 0a 20 20 20 20 20 20 67 6f  r(rc));.      go
29d2d 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72  to initone_error
29d2e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
29d2f 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69 6f  openedTransactio
29d30 6e 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  n = 1;.  }..  /*
29d31 20 47 65 74 20 74 68 65 20 64 61 74 61 62 61 73   Get the databas
29d32 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69  e meta informati
29d33 6f 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65  on..  **.  ** Me
29d34 74 61 20 76 61 6c 75 65 73 20 61 72 65 20 61 73  ta values are as
29d35 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20   follows:.  **  
29d36 20 20 6d 65 74 61 5b 30 5d 20 20 20 53 63 68 65    meta[0]   Sche
29d37 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 43 68 61 6e  ma cookie.  Chan
29d38 67 65 73 20 77 69 74 68 20 65 61 63 68 20 73 63  ges with each sc
29d39 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a 20 20 2a  hema change..  *
29d3a 2a 20 20 20 20 6d 65 74 61 5b 31 5d 20 20 20 46  *    meta[1]   F
29d3b 69 6c 65 20 66 6f 72 6d 61 74 20 6f 66 20 73 63  ile format of sc
29d3c 68 65 6d 61 20 6c 61 79 65 72 2e 0a 20 20 2a 2a  hema layer..  **
29d3d 20 20 20 20 6d 65 74 61 5b 32 5d 20 20 20 53 69      meta[2]   Si
29d3e 7a 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 63  ze of the page c
29d3f 61 63 68 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65  ache..  **    me
29d40 74 61 5b 33 5d 20 20 20 4c 61 72 67 65 73 74 20  ta[3]   Largest 
29d41 72 6f 6f 74 70 61 67 65 20 28 61 75 74 6f 2f 69  rootpage (auto/i
29d42 6e 63 72 5f 76 61 63 75 75 6d 20 6d 6f 64 65 29  ncr_vacuum mode)
29d43 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 34 5d  .  **    meta[4]
29d44 20 20 20 44 62 20 74 65 78 74 20 65 6e 63 6f 64     Db text encod
29d45 69 6e 67 2e 20 31 3a 55 54 46 2d 38 20 32 3a 55  ing. 1:UTF-8 2:U
29d46 54 46 2d 31 36 4c 45 20 33 3a 55 54 46 2d 31 36  TF-16LE 3:UTF-16
29d47 42 45 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  BE.  **    meta[
29d48 35 5d 20 20 20 55 73 65 72 20 76 65 72 73 69 6f  5]   User versio
29d49 6e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 36  n.  **    meta[6
29d4a 5d 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20  ]   Incremental 
29d4b 76 61 63 75 75 6d 20 6d 6f 64 65 0a 20 20 2a 2a  vacuum mode.  **
29d4c 20 20 20 20 6d 65 74 61 5b 37 5d 20 20 20 75 6e      meta[7]   un
29d4d 75 73 65 64 0a 20 20 2a 2a 20 20 20 20 6d 65 74  used.  **    met
29d4e 61 5b 38 5d 20 20 20 75 6e 75 73 65 64 0a 20 20  a[8]   unused.  
29d4f 2a 2a 20 20 20 20 6d 65 74 61 5b 39 5d 20 20 20  **    meta[9]   
29d50 75 6e 75 73 65 64 0a 20 20 2a 2a 0a 20 20 2a 2a  unused.  **.  **
29d51 20 4e 6f 74 65 3a 20 54 68 65 20 23 64 65 66 69   Note: The #defi
29d52 6e 65 64 20 53 51 4c 49 54 45 5f 55 54 46 2a 20  ned SQLITE_UTF* 
29d53 73 79 6d 62 6f 6c 73 20 69 6e 20 73 71 6c 69 74  symbols in sqlit
29d54 65 49 6e 74 2e 68 20 63 6f 72 72 65 73 70 6f 6e  eInt.h correspon
29d55 64 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 70 6f  d to.  ** the po
29d56 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 6f 66  ssible values of
29d57 20 6d 65 74 61 5b 34 5d 2e 0a 20 20 2a 2f 0a 20   meta[4]..  */. 
29d58 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61   for(i=0; i<Arra
29d59 79 53 69 7a 65 28 6d 65 74 61 29 3b 20 69 2b 2b  ySize(meta); i++
29d5a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
29d5b 72 65 65 47 65 74 4d 65 74 61 28 70 44 62 2d 3e  reeGetMeta(pDb->
29d5c 70 42 74 2c 20 69 2b 31 2c 20 28 75 33 32 20 2a  pBt, i+1, (u32 *
29d5d 29 26 6d 65 74 61 5b 69 5d 29 3b 0a 20 20 7d 0a  )&meta[i]);.  }.
29d5e 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e    pDb->pSchema->
29d5f 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20  schema_cookie = 
29d60 6d 65 74 61 5b 42 54 52 45 45 5f 53 43 48 45 4d  meta[BTREE_SCHEM
29d61 41 5f 56 45 52 53 49 4f 4e 2d 31 5d 3b 0a 0a 20  A_VERSION-1];.. 
29d62 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 61   /* If opening a
29d63 20 6e 6f 6e 2d 65 6d 70 74 79 20 64 61 74 61 62   non-empty datab
29d64 61 73 65 2c 20 63 68 65 63 6b 20 74 68 65 20 74  ase, check the t
29d65 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e 20 46 6f  ext encoding. Fo
29d66 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 69 6e 20  r the.  ** main 
29d67 64 61 74 61 62 61 73 65 2c 20 73 65 74 20 73 71  database, set sq
29d68 6c 69 74 65 33 2e 65 6e 63 20 74 6f 20 74 68 65  lite3.enc to the
29d69 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
29d6a 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 0a   main database..
29d6b 20 20 2a 2a 20 46 6f 72 20 61 6e 20 61 74 74 61    ** For an atta
29d6c 63 68 65 64 20 64 62 2c 20 69 74 20 69 73 20 61  ched db, it is a
29d6d 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 65  n error if the e
29d6e 6e 63 6f 64 69 6e 67 20 69 73 20 6e 6f 74 20 74  ncoding is not t
29d6f 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 73 20  he same.  ** as 
29d70 73 71 6c 69 74 65 33 2e 65 6e 63 2e 0a 20 20 2a  sqlite3.enc..  *
29d71 2f 0a 20 20 69 66 28 20 6d 65 74 61 5b 42 54 52  /.  if( meta[BTR
29d72 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47  EE_TEXT_ENCODING
29d73 2d 31 5d 20 29 7b 20 20 2f 2a 20 74 65 78 74 20  -1] ){  /* text 
29d74 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20  encoding */.    
29d75 69 66 28 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20  if( iDb==0 ){.  
29d76 20 20 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 3b      u8 encoding;
29d77 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6f 70 65  .      /* If ope
29d78 6e 69 6e 67 20 74 68 65 20 6d 61 69 6e 20 64 61  ning the main da
29d79 74 61 62 61 73 65 2c 20 73 65 74 20 45 4e 43 28  tabase, set ENC(
29d7a 64 62 29 2e 20 2a 2f 0a 20 20 20 20 20 20 65 6e  db). */.      en
29d7b 63 6f 64 69 6e 67 20 3d 20 28 75 38 29 6d 65 74  coding = (u8)met
29d7c 61 5b 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43  a[BTREE_TEXT_ENC
29d7d 4f 44 49 4e 47 2d 31 5d 20 26 20 33 3b 0a 20 20  ODING-1] & 3;.  
29d7e 20 20 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 67      if( encoding
29d7f 3d 3d 30 20 29 20 65 6e 63 6f 64 69 6e 67 20 3d  ==0 ) encoding =
29d80 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20   SQLITE_UTF8;.  
29d81 20 20 20 20 45 4e 43 28 64 62 29 20 3d 20 65 6e      ENC(db) = en
29d82 63 6f 64 69 6e 67 3b 0a 20 20 20 20 20 20 64 62  coding;.      db
29d83 2d 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d 20 73 71  ->pDfltColl = sq
29d84 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
29d85 28 64 62 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  (db, SQLITE_UTF8
29d86 2c 20 22 42 49 4e 41 52 59 22 2c 20 30 29 3b 0a  , "BINARY", 0);.
29d87 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
29d88 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 61   /* If opening a
29d89 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  n attached datab
29d8a 61 73 65 2c 20 74 68 65 20 65 6e 63 6f 64 69 6e  ase, the encodin
29d8b 67 20 6d 75 63 68 20 6d 61 74 63 68 20 45 4e 43  g much match ENC
29d8c 28 64 62 29 20 2a 2f 0a 20 20 20 20 20 20 69 66  (db) */.      if
29d8d 28 20 6d 65 74 61 5b 42 54 52 45 45 5f 54 45 58  ( meta[BTREE_TEX
29d8e 54 5f 45 4e 43 4f 44 49 4e 47 2d 31 5d 21 3d 45  T_ENCODING-1]!=E
29d8f 4e 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20  NC(db) ){.      
29d90 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
29d91 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c  ng(pzErrMsg, db,
29d92 20 22 61 74 74 61 63 68 65 64 20 64 61 74 61 62   "attached datab
29d93 61 73 65 73 20 6d 75 73 74 20 75 73 65 20 74 68  ases must use th
29d94 65 20 73 61 6d 65 22 0a 20 20 20 20 20 20 20 20  e same".        
29d95 20 20 20 20 22 20 74 65 78 74 20 65 6e 63 6f 64      " text encod
29d96 69 6e 67 20 61 73 20 6d 61 69 6e 20 64 61 74 61  ing as main data
29d97 62 61 73 65 22 29 3b 0a 20 20 20 20 20 20 20 20  base");.        
29d98 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
29d99 52 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  R;.        goto 
29d9a 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75  initone_error_ou
29d9b 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
29d9c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 44 62  .  }else{.    Db
29d9d 53 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20  SetProperty(db, 
29d9e 69 44 62 2c 20 44 42 5f 45 6d 70 74 79 29 3b 0a  iDb, DB_Empty);.
29d9f 20 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63 68 65    }.  pDb->pSche
29da0 6d 61 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62  ma->enc = ENC(db
29da1 29 3b 0a 0a 20 20 69 66 28 20 70 44 62 2d 3e 70  );..  if( pDb->p
29da2 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69  Schema->cache_si
29da3 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 73 69 7a  ze==0 ){.    siz
29da4 65 20 3d 20 6d 65 74 61 5b 42 54 52 45 45 5f 44  e = meta[BTREE_D
29da5 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
29da6 45 2d 31 5d 3b 0a 20 20 20 20 69 66 28 20 73 69  E-1];.    if( si
29da7 7a 65 3d 3d 30 20 29 7b 20 73 69 7a 65 20 3d 20  ze==0 ){ size = 
29da8 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43  SQLITE_DEFAULT_C
29da9 41 43 48 45 5f 53 49 5a 45 3b 20 7d 0a 20 20 20  ACHE_SIZE; }.   
29daa 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20 73 69   if( size<0 ) si
29dab 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20  ze = -size;.    
29dac 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
29dad 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b  che_size = size;
29dae 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
29daf 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 70 44  eSetCacheSize(pD
29db0 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53 63  b->pBt, pDb->pSc
29db1 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
29db2 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  );.  }..  /*.  *
29db3 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 31  * file_format==1
29db4 20 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 30 2e      Version 3.0.
29db5 30 2e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72  0..  ** file_for
29db6 6d 61 74 3d 3d 32 20 20 20 20 56 65 72 73 69 6f  mat==2    Versio
29db7 6e 20 33 2e 31 2e 33 2e 20 20 2f 2f 20 41 4c 54  n 3.1.3.  // ALT
29db8 45 52 20 54 41 42 4c 45 20 41 44 44 20 43 4f 4c  ER TABLE ADD COL
29db9 55 4d 4e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f  UMN.  ** file_fo
29dba 72 6d 61 74 3d 3d 33 20 20 20 20 56 65 72 73 69  rmat==3    Versi
29dbb 6f 6e 20 33 2e 31 2e 34 2e 20 20 2f 2f 20 64 69  on 3.1.4.  // di
29dbc 74 74 6f 20 62 75 74 20 77 69 74 68 20 6e 6f 6e  tto but with non
29dbd 2d 4e 55 4c 4c 20 64 65 66 61 75 6c 74 73 0a 20  -NULL defaults. 
29dbe 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d   ** file_format=
29dbf 3d 34 20 20 20 20 56 65 72 73 69 6f 6e 20 33 2e  =4    Version 3.
29dc0 33 2e 30 2e 20 20 2f 2f 20 44 45 53 43 20 69 6e  3.0.  // DESC in
29dc1 64 69 63 65 73 2e 20 20 42 6f 6f 6c 65 61 6e 20  dices.  Boolean 
29dc2 63 6f 6e 73 74 61 6e 74 73 0a 20 20 2a 2f 0a 20  constants.  */. 
29dc3 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
29dc4 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38  ile_format = (u8
29dc5 29 6d 65 74 61 5b 42 54 52 45 45 5f 46 49 4c 45  )meta[BTREE_FILE
29dc6 5f 46 4f 52 4d 41 54 2d 31 5d 3b 0a 20 20 69 66  _FORMAT-1];.  if
29dc7 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  ( pDb->pSchema->
29dc8 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 30 20 29  file_format==0 )
29dc9 7b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65  {.    pDb->pSche
29dca 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20  ma->file_format 
29dcb 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  = 1;.  }.  if( p
29dcc 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c  Db->pSchema->fil
29dcd 65 5f 66 6f 72 6d 61 74 3e 53 51 4c 49 54 45 5f  e_format>SQLITE_
29dce 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20  MAX_FILE_FORMAT 
29dcf 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
29dd0 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67  tString(pzErrMsg
29dd1 2c 20 64 62 2c 20 22 75 6e 73 75 70 70 6f 72 74  , db, "unsupport
29dd2 65 64 20 66 69 6c 65 20 66 6f 72 6d 61 74 22 29  ed file format")
29dd3 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
29dd4 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74  E_ERROR;.    got
29dd5 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f  o initone_error_
29dd6 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  out;.  }..  /* T
29dd7 69 63 6b 65 74 20 23 32 38 30 34 3a 20 20 57 68  icket #2804:  Wh
29dd8 65 6e 20 77 65 20 6f 70 65 6e 20 61 20 64 61 74  en we open a dat
29dd9 61 62 61 73 65 20 69 6e 20 74 68 65 20 6e 65 77  abase in the new
29dda 65 72 20 66 69 6c 65 20 66 6f 72 6d 61 74 2c 0a  er file format,.
29ddb 20 20 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 6c    ** clear the l
29ddc 65 67 61 63 79 5f 66 69 6c 65 5f 66 6f 72 6d 61  egacy_file_forma
29ddd 74 20 70 72 61 67 6d 61 20 66 6c 61 67 20 73 6f  t pragma flag so
29dde 20 74 68 61 74 20 61 20 56 41 43 55 55 4d 20 77   that a VACUUM w
29ddf 69 6c 6c 0a 20 20 2a 2a 20 6e 6f 74 20 64 6f 77  ill.  ** not dow
29de0 6e 67 72 61 64 65 20 74 68 65 20 64 61 74 61 62  ngrade the datab
29de1 61 73 65 20 61 6e 64 20 74 68 75 73 20 69 6e 76  ase and thus inv
29de2 61 6c 69 64 61 74 65 20 61 6e 79 20 64 65 73 63  alidate any desc
29de3 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 69  ending.  ** indi
29de4 63 65 73 20 74 68 61 74 20 74 68 65 20 75 73 65  ces that the use
29de5 72 20 6d 69 67 68 74 20 68 61 76 65 20 63 72 65  r might have cre
29de6 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ated..  */.  if(
29de7 20 69 44 62 3d 3d 30 20 26 26 20 6d 65 74 61 5b   iDb==0 && meta[
29de8 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41  BTREE_FILE_FORMA
29de9 54 2d 31 5d 3e 3d 34 20 29 7b 0a 20 20 20 20 64  T-1]>=4 ){.    d
29dea 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c  b->flags &= ~SQL
29deb 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d  ITE_LegacyFileFm
29dec 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61  t;.  }..  /* Rea
29ded 64 20 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66  d the schema inf
29dee 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 6f 66 20  ormation out of 
29def 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65  the schema table
29df0 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  s.  */.  assert(
29df1 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29   db->init.busy )
29df2 3b 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 2a  ;.  {.    char *
29df3 7a 53 71 6c 3b 0a 20 20 20 20 7a 53 71 6c 20 3d  zSql;.    zSql =
29df4 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
29df5 64 62 2c 20 0a 20 20 20 20 20 20 20 20 22 53 45  db, .        "SE
29df6 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70  LECT name, rootp
29df7 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25  age, sql FROM '%
29df8 71 27 2e 25 73 22 2c 0a 20 20 20 20 20 20 20 20  q'.%s",.        
29df9 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
29dfa 6d 65 2c 20 7a 4d 61 73 74 65 72 4e 61 6d 65 29  me, zMasterName)
29dfb 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  ;.    (void)sqli
29dfc 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
29dfd 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
29dfe 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
29dff 49 4f 4e 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  ION.    {.      
29e00 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69  int (*xAuth)(voi
29e01 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  d*,int,const cha
29e02 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
29e03 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
29e04 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 78   char*);.      x
29e05 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 75 74 68  Auth = db->xAuth
29e06 3b 0a 20 20 20 20 20 20 64 62 2d 3e 78 41 75 74  ;.      db->xAut
29e07 68 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20  h = 0;.#endif.  
29e08 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
29e09 5f 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20  _exec(db, zSql, 
29e0a 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62  sqlite3InitCallb
29e0b 61 63 6b 2c 20 26 69 6e 69 74 44 61 74 61 2c 20  ack, &initData, 
29e0c 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0);.#ifndef SQLI
29e0d 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
29e0e 41 54 49 4f 4e 0a 20 20 20 20 20 20 64 62 2d 3e  ATION.      db->
29e0f 78 41 75 74 68 20 3d 20 78 41 75 74 68 3b 0a 20  xAuth = xAuth;. 
29e10 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
29e11 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
29e12 4b 20 29 20 72 63 20 3d 20 69 6e 69 74 44 61 74  K ) rc = initDat
29e13 61 2e 72 63 3b 0a 20 20 20 20 28 76 6f 69 64 29  a.rc;.    (void)
29e14 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
29e15 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  db);.    sqlite3
29e16 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29  DbFree(db, zSql)
29e17 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
29e18 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 20 20  _OMIT_ANALYZE.  
29e19 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
29e1a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
29e1b 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64  ite3AnalysisLoad
29e1c 28 64 62 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d  (db, iDb);.    }
29e1d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66  .#endif.  }.  if
29e1e 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
29e1f 65 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  ed ){.    rc = S
29e20 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
29e21 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
29e22 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
29e23 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  0);.  }.  if( rc
29e24 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28  ==SQLITE_OK || (
29e25 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
29e26 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 29 29 7b  _RecoveryMode)){
29e27 0a 20 20 20 20 2f 2a 20 42 6c 61 63 6b 20 6d 61  .    /* Black ma
29e28 67 69 63 3a 20 49 66 20 74 68 65 20 53 51 4c 49  gic: If the SQLI
29e29 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20  TE_RecoveryMode 
29e2a 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65  flag is set, the
29e2b 6e 20 63 6f 6e 73 69 64 65 72 0a 20 20 20 20 2a  n consider.    *
29e2c 2a 20 74 68 65 20 73 63 68 65 6d 61 20 6c 6f 61  * the schema loa
29e2d 64 65 64 2c 20 65 76 65 6e 20 69 66 20 65 72 72  ded, even if err
29e2e 6f 72 73 20 6f 63 63 75 72 72 65 64 2e 20 49 6e  ors occurred. In
29e2f 20 74 68 69 73 20 73 69 74 75 61 74 69 6f 6e 20   this situation 
29e30 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 75 72 72  the .    ** curr
29e31 65 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ent sqlite3_prep
29e32 61 72 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 20  are() operation 
29e33 77 69 6c 6c 20 66 61 69 6c 2c 20 62 75 74 20 74  will fail, but t
29e34 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 6e 65  he following one
29e35 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 61 74 74  .    ** will att
29e36 65 6d 70 74 20 74 6f 20 63 6f 6d 70 69 6c 65 20  empt to compile 
29e37 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 74 61  the supplied sta
29e38 74 65 6d 65 6e 74 20 61 67 61 69 6e 73 74 20 77  tement against w
29e39 68 61 74 65 76 65 72 20 73 75 62 73 65 74 0a 20  hatever subset. 
29e3a 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63 68     ** of the sch
29e3b 65 6d 61 20 77 61 73 20 6c 6f 61 64 65 64 20 62  ema was loaded b
29e3c 65 66 6f 72 65 20 74 68 65 20 65 72 72 6f 72 20  efore the error 
29e3d 6f 63 63 75 72 72 65 64 2e 20 54 68 65 20 70 72  occurred. The pr
29e3e 69 6d 61 72 79 0a 20 20 20 20 2a 2a 20 70 75 72  imary.    ** pur
29e3f 70 6f 73 65 20 6f 66 20 74 68 69 73 20 69 73 20  pose of this is 
29e40 74 6f 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 20  to allow access 
29e41 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  to the sqlite_ma
29e42 73 74 65 72 20 74 61 62 6c 65 0a 20 20 20 20 2a  ster table.    *
29e43 2a 20 65 76 65 6e 20 77 68 65 6e 20 69 74 73 20  * even when its 
29e44 63 6f 6e 74 65 6e 74 73 20 68 61 76 65 20 62 65  contents have be
29e45 65 6e 20 63 6f 72 72 75 70 74 65 64 2e 0a 20 20  en corrupted..  
29e46 20 20 2a 2f 0a 20 20 20 20 44 62 53 65 74 50 72    */.    DbSetPr
29e47 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20  operty(db, iDb, 
29e48 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29  DB_SchemaLoaded)
29e49 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
29e4a 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
29e4b 4a 75 6d 70 20 68 65 72 65 20 66 6f 72 20 61 6e  Jump here for an
29e4c 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75   error that occu
29e4d 72 73 20 61 66 74 65 72 20 73 75 63 63 65 73 73  rs after success
29e4e 66 75 6c 6c 79 20 61 6c 6c 6f 63 61 74 69 6e 67  fully allocating
29e4f 0a 20 20 2a 2a 20 63 75 72 4d 61 69 6e 20 61 6e  .  ** curMain an
29e50 64 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  d calling sqlite
29e51 33 42 74 72 65 65 45 6e 74 65 72 28 29 2e 20 46  3BtreeEnter(). F
29e52 6f 72 20 61 6e 20 65 72 72 6f 72 20 74 68 61 74  or an error that
29e53 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20 62 65 66   occurs.  ** bef
29e54 6f 72 65 20 74 68 61 74 20 70 6f 69 6e 74 2c 20  ore that point, 
29e55 6a 75 6d 70 20 74 6f 20 65 72 72 6f 72 5f 6f 75  jump to error_ou
29e56 74 2e 0a 20 20 2a 2f 0a 69 6e 69 74 6f 6e 65 5f  t..  */.initone_
29e57 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 69 66 28  error_out:.  if(
29e58 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69   openedTransacti
29e59 6f 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  on ){.    sqlite
29e5a 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 70 44 62  3BtreeCommit(pDb
29e5b 2d 3e 70 42 74 29 3b 0a 20 20 7d 0a 20 20 73 71  ->pBt);.  }.  sq
29e5c 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
29e5d 70 44 62 2d 3e 70 42 74 29 3b 0a 0a 65 72 72 6f  pDb->pBt);..erro
29e5e 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 3d  r_out:.  if( rc=
29e5f 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c  =SQLITE_NOMEM ||
29e60 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52   rc==SQLITE_IOER
29e61 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 64  R_NOMEM ){.    d
29e62 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
29e63 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 1;.  }.  retur
29e64 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  n rc;.}../*.** I
29e65 6e 69 74 69 61 6c 69 7a 65 20 61 6c 6c 20 64 61  nitialize all da
29e66 74 61 62 61 73 65 20 66 69 6c 65 73 20 2d 20 74  tabase files - t
29e67 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
29e68 20 66 69 6c 65 2c 20 74 68 65 20 66 69 6c 65 0a   file, the file.
29e69 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ** used to store
29e6a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
29e6b 73 2c 20 61 6e 64 20 61 6e 79 20 61 64 64 69 74  s, and any addit
29e6c 69 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66  ional database f
29e6d 69 6c 65 73 0a 2a 2a 20 63 72 65 61 74 65 64 20  iles.** created 
29e6e 75 73 69 6e 67 20 41 54 54 41 43 48 20 73 74 61  using ATTACH sta
29e6f 74 65 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e  tements.  Return
29e70 20 61 20 73 75 63 63 65 73 73 20 63 6f 64 65 2e   a success code.
29e71 20 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72    If an.** error
29e72 20 6f 63 63 75 72 73 2c 20 77 72 69 74 65 20 61   occurs, write a
29e73 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
29e74 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a  into *pzErrMsg..
29e75 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 64 61  **.** After a da
29e76 74 61 62 61 73 65 20 69 73 20 69 6e 69 74 69 61  tabase is initia
29e77 6c 69 7a 65 64 2c 20 74 68 65 20 44 42 5f 53 63  lized, the DB_Sc
29e78 68 65 6d 61 4c 6f 61 64 65 64 20 62 69 74 20 69  hemaLoaded bit i
29e79 73 20 73 65 74 0a 2a 2a 20 62 69 74 20 69 73 20  s set.** bit is 
29e7a 73 65 74 20 69 6e 20 74 68 65 20 66 6c 61 67 73  set in the flags
29e7b 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 44 62   field of the Db
29e7c 20 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 74   structure. If t
29e7d 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66  he database.** f
29e7e 69 6c 65 20 77 61 73 20 6f 66 20 7a 65 72 6f 2d  ile was of zero-
29e7f 6c 65 6e 67 74 68 2c 20 74 68 65 6e 20 74 68 65  length, then the
29e80 20 44 42 5f 45 6d 70 74 79 20 66 6c 61 67 20 69   DB_Empty flag i
29e81 73 20 61 6c 73 6f 20 73 65 74 2e 0a 2a 2f 0a 53  s also set..*/.S
29e82 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
29e83 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73 71  t sqlite3Init(sq
29e84 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20  lite3 *db, char 
29e85 2a 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69  **pzErrMsg){.  i
29e86 6e 74 20 69 2c 20 72 63 3b 0a 20 20 69 6e 74 20  nt i, rc;.  int 
29e87 63 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20  commit_internal 
29e88 3d 20 21 28 64 62 2d 3e 66 6c 61 67 73 26 53 51  = !(db->flags&SQ
29e89 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67  LITE_InternChang
29e8a 65 73 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  es);.  .  assert
29e8b 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
29e8c 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
29e8d 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  );.  rc = SQLITE
29e8e 5f 4f 4b 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e  _OK;.  db->init.
29e8f 62 75 73 79 20 3d 20 31 3b 0a 20 20 66 6f 72 28  busy = 1;.  for(
29e90 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
29e91 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b  OK && i<db->nDb;
29e92 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 44   i++){.    if( D
29e93 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c  bHasProperty(db,
29e94 20 69 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61   i, DB_SchemaLoa
29e95 64 65 64 29 20 7c 7c 20 69 3d 3d 31 20 29 20 63  ded) || i==1 ) c
29e96 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 63 20  ontinue;.    rc 
29e97 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65  = sqlite3InitOne
29e98 28 64 62 2c 20 69 2c 20 70 7a 45 72 72 4d 73 67  (db, i, pzErrMsg
29e99 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
29e9a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
29e9b 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
29e9c 61 28 64 62 2c 20 69 29 3b 0a 20 20 20 20 7d 0a  a(db, i);.    }.
29e9d 20 20 7d 0a 0a 20 20 2f 2a 20 4f 6e 63 65 20 61    }..  /* Once a
29e9e 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 64 61 74  ll the other dat
29e9f 61 62 61 73 65 73 20 68 61 76 65 20 62 65 65 6e  abases have been
29ea0 20 69 6e 69 74 69 61 6c 69 73 65 64 2c 20 6c 6f   initialised, lo
29ea1 61 64 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20  ad the schema.  
29ea2 2a 2a 20 66 6f 72 20 74 68 65 20 54 45 4d 50 20  ** for the TEMP 
29ea3 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 20 69  database. This i
29ea4 73 20 6c 6f 61 64 65 64 20 6c 61 73 74 2c 20 61  s loaded last, a
29ea5 73 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62  s the TEMP datab
29ea6 61 73 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20  ase.  ** schema 
29ea7 6d 61 79 20 63 6f 6e 74 61 69 6e 20 72 65 66 65  may contain refe
29ea8 72 65 6e 63 65 73 20 74 6f 20 6f 62 6a 65 63 74  rences to object
29ea9 73 20 69 6e 20 6f 74 68 65 72 20 64 61 74 61 62  s in other datab
29eaa 61 73 65 73 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  ases..  */.#ifnd
29eab 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
29eac 45 4d 50 44 42 0a 20 20 69 66 28 20 72 63 3d 3d  EMPDB.  if( rc==
29ead 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 41 4c 57  SQLITE_OK && ALW
29eae 41 59 53 28 64 62 2d 3e 6e 44 62 3e 31 29 0a 20  AYS(db->nDb>1). 
29eaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29eb0 20 20 20 26 26 20 21 44 62 48 61 73 50 72 6f 70     && !DbHasProp
29eb1 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53  erty(db, 1, DB_S
29eb2 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a  chemaLoaded) ){.
29eb3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
29eb4 49 6e 69 74 4f 6e 65 28 64 62 2c 20 31 2c 20 70  InitOne(db, 1, p
29eb5 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66  zErrMsg);.    if
29eb6 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71  ( rc ){.      sq
29eb7 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
29eb8 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 31 29 3b  alSchema(db, 1);
29eb9 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
29eba 66 0a 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75  f..  db->init.bu
29ebb 73 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63  sy = 0;.  if( rc
29ebc 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63  ==SQLITE_OK && c
29ebd 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20 29  ommit_internal )
29ebe 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d  {.    sqlite3Com
29ebf 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67  mitInternalChang
29ec0 65 73 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 72  es(db);.  }..  r
29ec1 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a 2f 2a  eturn rc; .}../*
29ec2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
29ec3 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74   is a no-op if t
29ec4 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
29ec5 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  ma is already in
29ec6 69 74 69 61 6c 69 73 65 64 2e 0a 2a 2a 20 4f 74  itialised..** Ot
29ec7 68 65 72 77 69 73 65 2c 20 74 68 65 20 73 63 68  herwise, the sch
29ec8 65 6d 61 20 69 73 20 6c 6f 61 64 65 64 2e 20 41  ema is loaded. A
29ec9 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  n error code is 
29eca 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
29ecb 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
29ecc 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
29ecd 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  a(Parse *pParse)
29ece 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
29ecf 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65  ITE_OK;.  sqlite
29ed0 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
29ed1 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  db;.  assert( sq
29ed2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
29ed3 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
29ed4 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62   if( !db->init.b
29ed5 75 73 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  usy ){.    rc = 
29ed6 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62 2c 20  sqlite3Init(db, 
29ed7 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67  &pParse->zErrMsg
29ed8 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21  );.  }.  if( rc!
29ed9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
29eda 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 72    pParse->rc = r
29edb 63 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  c;.    pParse->n
29edc 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74  Err++;.  }.  ret
29edd 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
29ede 2a 20 43 68 65 63 6b 20 73 63 68 65 6d 61 20 63  * Check schema c
29edf 6f 6f 6b 69 65 73 20 69 6e 20 61 6c 6c 20 64 61  ookies in all da
29ee0 74 61 62 61 73 65 73 2e 20 20 49 66 20 61 6e 79  tabases.  If any
29ee1 20 63 6f 6f 6b 69 65 20 69 73 20 6f 75 74 0a 2a   cookie is out.*
29ee2 2a 20 6f 66 20 64 61 74 65 20 73 65 74 20 70 50  * of date set pP
29ee3 61 72 73 65 2d 3e 72 63 20 74 6f 20 53 51 4c 49  arse->rc to SQLI
29ee4 54 45 5f 53 43 48 45 4d 41 2e 20 20 49 66 20 61  TE_SCHEMA.  If a
29ee5 6c 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  ll schema cookie
29ee6 73 0a 2a 2a 20 6d 61 6b 65 20 6e 6f 20 63 68 61  s.** make no cha
29ee7 6e 67 65 73 20 74 6f 20 70 50 61 72 73 65 2d 3e  nges to pParse->
29ee8 72 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  rc..*/.static vo
29ee9 69 64 20 73 63 68 65 6d 61 49 73 56 61 6c 69 64  id schemaIsValid
29eea 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
29eeb 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
29eec 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
29eed 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 72 63  nt iDb;.  int rc
29eee 3b 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 3b 0a  ;.  int cookie;.
29eef 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
29ef0 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 29  e->checkSchema )
29ef1 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
29ef2 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
29ef3 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  b->mutex) );.  f
29ef4 6f 72 28 69 44 62 3d 30 3b 20 69 44 62 3c 64 62  or(iDb=0; iDb<db
29ef5 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a 20  ->nDb; iDb++){. 
29ef6 20 20 20 69 6e 74 20 6f 70 65 6e 65 64 54 72 61     int openedTra
29ef7 6e 73 61 63 74 69 6f 6e 20 3d 20 30 3b 20 20 20  nsaction = 0;   
29ef8 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
29ef9 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
29efa 73 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 20 20  s opened */.    
29efb 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d  Btree *pBt = db-
29efc 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 20 20  >aDb[iDb].pBt;  
29efd 20 20 20 2f 2a 20 42 74 72 65 65 20 64 61 74 61     /* Btree data
29efe 62 61 73 65 20 74 6f 20 72 65 61 64 20 63 6f 6f  base to read coo
29eff 6b 69 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20  kie from */.    
29f00 69 66 28 20 70 42 74 3d 3d 30 20 29 20 63 6f 6e  if( pBt==0 ) con
29f01 74 69 6e 75 65 3b 0a 0a 20 20 20 20 2f 2a 20 49  tinue;..    /* I
29f02 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 61  f there is not a
29f03 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6f 6e  lready a read-on
29f04 6c 79 20 28 6f 72 20 72 65 61 64 2d 77 72 69 74  ly (or read-writ
29f05 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  e) transaction o
29f06 70 65 6e 65 64 0a 20 20 20 20 2a 2a 20 6f 6e 20  pened.    ** on 
29f07 74 68 65 20 62 2d 74 72 65 65 20 64 61 74 61 62  the b-tree datab
29f08 61 73 65 2c 20 6f 70 65 6e 20 6f 6e 65 20 6e 6f  ase, open one no
29f09 77 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74  w. If a transact
29f0a 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2c 20 69  ion is opened, i
29f0b 74 20 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62  t .    ** will b
29f0c 65 20 63 6c 6f 73 65 64 20 69 6d 6d 65 64 69 61  e closed immedia
29f0d 74 65 6c 79 20 61 66 74 65 72 20 72 65 61 64 69  tely after readi
29f0e 6e 67 20 74 68 65 20 6d 65 74 61 2d 76 61 6c 75  ng the meta-valu
29f0f 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 73  e. */.    if( !s
29f10 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52  qlite3BtreeIsInR
29f11 65 61 64 54 72 61 6e 73 28 70 42 74 29 20 29 7b  eadTrans(pBt) ){
29f12 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
29f13 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61  te3BtreeBeginTra
29f14 6e 73 28 70 42 74 2c 20 30 29 3b 0a 20 20 20 20  ns(pBt, 0);.    
29f15 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
29f16 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51  _NOMEM || rc==SQ
29f17 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
29f18 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e   ){.        db->
29f19 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
29f1a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
29f1b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
29f1c 4b 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  K ) return;.    
29f1d 20 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74    openedTransact
29f1e 69 6f 6e 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a  ion = 1;.    }..
29f1f 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20      /* Read the 
29f20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 66 72  schema cookie fr
29f21 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  om the database.
29f22 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   If it does not 
29f23 6d 61 74 63 68 20 74 68 65 20 0a 20 20 20 20 2a  match the .    *
29f24 2a 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 61  * value stored a
29f25 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e  s part of the in
29f26 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73   the in-memory s
29f27 63 68 65 6d 61 20 72 65 70 72 65 73 65 6e 74 61  chema representa
29f28 74 69 6f 6e 2c 0a 20 20 20 20 2a 2a 20 73 65 74  tion,.    ** set
29f29 20 50 61 72 73 65 2e 72 63 20 74 6f 20 53 51 4c   Parse.rc to SQL
29f2a 49 54 45 5f 53 43 48 45 4d 41 2e 20 2a 2f 0a 20  ITE_SCHEMA. */. 
29f2b 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47     sqlite3BtreeG
29f2c 65 74 4d 65 74 61 28 70 42 74 2c 20 42 54 52 45  etMeta(pBt, BTRE
29f2d 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e  E_SCHEMA_VERSION
29f2e 2c 20 28 75 33 32 20 2a 29 26 63 6f 6f 6b 69 65  , (u32 *)&cookie
29f2f 29 3b 0a 20 20 20 20 69 66 28 20 63 6f 6f 6b 69  );.    if( cooki
29f30 65 21 3d 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e!=db->aDb[iDb].
29f31 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f  pSchema->schema_
29f32 63 6f 6f 6b 69 65 20 29 7b 0a 20 20 20 20 20 20  cookie ){.      
29f33 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
29f34 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 20 20  ITE_SCHEMA;.    
29f35 7d 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20  }..    /* Close 
29f36 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  the transaction,
29f37 20 69 66 20 6f 6e 65 20 77 61 73 20 6f 70 65 6e   if one was open
29f38 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f  ed. */.    if( o
29f39 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69 6f 6e  penedTransaction
29f3a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
29f3b 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 70 42 74  3BtreeCommit(pBt
29f3c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
29f3d 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
29f3e 73 63 68 65 6d 61 20 70 6f 69 6e 74 65 72 20 69  schema pointer i
29f3f 6e 74 6f 20 74 68 65 20 69 44 62 20 69 6e 64 65  nto the iDb inde
29f40 78 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73  x that indicates
29f41 0a 2a 2a 20 77 68 69 63 68 20 64 61 74 61 62 61  .** which databa
29f42 73 65 20 66 69 6c 65 20 69 6e 20 64 62 2d 3e 61  se file in db->a
29f43 44 62 5b 5d 20 74 68 65 20 73 63 68 65 6d 61 20  Db[] the schema 
29f44 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a  refers to..**.**
29f45 20 49 66 20 74 68 65 20 73 61 6d 65 20 64 61 74   If the same dat
29f46 61 62 61 73 65 20 69 73 20 61 74 74 61 63 68 65  abase is attache
29f47 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  d more than once
29f48 2c 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61  , the first.** a
29f49 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
29f4a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
29f4b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
29f4c 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d  int sqlite3Schem
29f4d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33  aToIndex(sqlite3
29f4e 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 70 53   *db, Schema *pS
29f4f 63 68 65 6d 61 29 7b 0a 20 20 69 6e 74 20 69 20  chema){.  int i 
29f50 3d 20 2d 31 30 30 30 30 30 30 3b 0a 0a 20 20 2f  = -1000000;..  /
29f51 2a 20 49 66 20 70 53 63 68 65 6d 61 20 69 73 20  * If pSchema is 
29f52 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65 74 75 72  NULL, then retur
29f53 6e 20 2d 31 30 30 30 30 30 30 2e 20 54 68 69 73  n -1000000. This
29f54 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63 6f   happens when co
29f55 64 65 20 69 6e 20 0a 20 20 2a 2a 20 65 78 70 72  de in .  ** expr
29f56 2e 63 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20  .c is trying to 
29f57 72 65 73 6f 6c 76 65 20 61 20 72 65 66 65 72 65  resolve a refere
29f58 6e 63 65 20 74 6f 20 61 20 74 72 61 6e 73 69 65  nce to a transie
29f59 6e 74 20 74 61 62 6c 65 20 28 69 2e 65 2e 20 6f  nt table (i.e. o
29f5a 6e 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20  ne.  ** created 
29f5b 62 79 20 61 20 73 75 62 2d 73 65 6c 65 63 74 29  by a sub-select)
29f5c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
29f5d 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
29f5e 6f 66 20 74 68 69 73 20 0a 20 20 2a 2a 20 66 75  of this .  ** fu
29f5f 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65  nction should ne
29f60 76 65 72 20 62 65 20 75 73 65 64 2e 0a 20 20 2a  ver be used..  *
29f61 2a 0a 20 20 2a 2a 20 57 65 20 72 65 74 75 72 6e  *.  ** We return
29f62 20 2d 31 30 30 30 30 30 30 20 69 6e 73 74 65 61   -1000000 instea
29f63 64 20 6f 66 20 74 68 65 20 6d 6f 72 65 20 75 73  d of the more us
29f64 75 61 6c 20 2d 31 20 73 69 6d 70 6c 79 20 62 65  ual -1 simply be
29f65 63 61 75 73 65 20 75 73 69 6e 67 0a 20 20 2a 2a  cause using.  **
29f66 20 2d 31 30 30 30 30 30 30 20 61 73 20 74 68 65   -1000000 as the
29f67 20 69 6e 63 6f 72 72 65 63 74 20 69 6e 64 65 78   incorrect index
29f68 20 69 6e 74 6f 20 64 62 2d 3e 61 44 62 5b 5d 20   into db->aDb[] 
29f69 69 73 20 6d 75 63 68 20 0a 20 20 2a 2a 20 6d 6f  is much .  ** mo
29f6a 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 63 61 75  re likely to cau
29f6b 73 65 20 61 20 73 65 67 66 61 75 6c 74 20 74 68  se a segfault th
29f6c 61 6e 20 2d 31 20 28 6f 66 20 63 6f 75 72 73 65  an -1 (of course
29f6d 20 74 68 65 72 65 20 61 72 65 20 61 73 73 65 72   there are asser
29f6e 74 28 29 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  t().  ** stateme
29f6f 6e 74 73 20 74 6f 6f 2c 20 62 75 74 20 69 74 20  nts too, but it 
29f70 6e 65 76 65 72 20 68 75 72 74 73 20 74 6f 20 70  never hurts to p
29f71 6c 61 79 20 74 68 65 20 6f 64 64 73 29 2e 0a 20  lay the odds).. 
29f72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71   */.  assert( sq
29f73 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
29f74 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
29f75 20 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b 0a   if( pSchema ){.
29f76 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 41 4c 57      for(i=0; ALW
29f77 41 59 53 28 69 3c 64 62 2d 3e 6e 44 62 29 3b 20  AYS(i<db->nDb); 
29f78 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
29f79 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68 65  db->aDb[i].pSche
29f7a 6d 61 3d 3d 70 53 63 68 65 6d 61 20 29 7b 0a 20  ma==pSchema ){. 
29f7b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
29f7c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
29f7d 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
29f7e 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7d  i<db->nDb );.  }
29f7f 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a  .  return i;.}..
29f80 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68  /*.** Compile th
29f81 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  e UTF-8 encoded 
29f82 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a 53  SQL statement zS
29f83 71 6c 20 69 6e 74 6f 20 61 20 73 74 61 74 65 6d  ql into a statem
29f84 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73  ent handle..*/.s
29f85 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
29f86 33 50 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69  3Prepare(.  sqli
29f87 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
29f88 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
29f89 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63  e handle. */.  c
29f8a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
29f8b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d           /* UTF-
29f8c 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74  8 encoded SQL st
29f8d 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e  atement. */.  in
29f8e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20  t nBytes,       
29f8f 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
29f90 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
29f91 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76  es. */.  int sav
29f92 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20  eSqlFlag,       
29f93 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f     /* True to co
29f94 70 79 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f  py SQL text into
29f95 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d   the sqlite3_stm
29f96 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 52 65  t */.  Vdbe *pRe
29f97 70 72 65 70 61 72 65 2c 20 20 20 20 20 20 20 20  prepare,        
29f98 20 2f 2a 20 56 4d 20 62 65 69 6e 67 20 72 65 70   /* VM being rep
29f99 72 65 70 61 72 65 64 20 2a 2f 0a 20 20 73 71 6c  repared */.  sql
29f9a 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
29f9b 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
29f9c 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
29f9d 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
29f9e 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  nt */.  const ch
29f9f 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  ar **pzTail     
29fa0 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
29fa1 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
29fa2 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  /.){.  Parse *pP
29fa3 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  arse;           
29fa4 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
29fa5 65 78 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ext */.  char *z
29fa6 45 72 72 4d 73 67 20 3d 20 30 3b 20 20 20 20 20  ErrMsg = 0;     
29fa7 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
29fa8 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  age */.  int rc 
29fa9 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
29faa 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64     /* Result cod
29fab 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  e */.  int i;   
29fac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29fad 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
29fae 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61   */..  /* Alloca
29faf 74 65 20 74 68 65 20 70 61 72 73 69 6e 67 20 63  te the parsing c
29fb0 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 70 50 61 72  ontext */.  pPar
29fb1 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63  se = sqlite3Stac
29fb2 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  kAllocZero(db, s
29fb3 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b  izeof(*pParse));
29fb4 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30  .  if( pParse==0
29fb5 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
29fb6 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
29fb7 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65 3b  oto end_prepare;
29fb8 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 70  .  }.  pParse->p
29fb9 52 65 70 72 65 70 61 72 65 20 3d 20 70 52 65 70  Reprepare = pRep
29fba 72 65 70 61 72 65 3b 0a 0a 20 20 69 66 28 20 73  repare;..  if( s
29fbb 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
29fbc 62 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  b) ){.    rc = S
29fbd 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
29fbe 20 20 67 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61    goto end_prepa
29fbf 72 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  re;.  }.  assert
29fc0 28 20 70 70 53 74 6d 74 20 26 26 20 2a 70 70 53  ( ppStmt && *ppS
29fc1 74 6d 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  tmt==0 );.  asse
29fc2 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rt( !db->mallocF
29fc3 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 72  ailed );.  asser
29fc4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
29fc5 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
29fc6 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20   );..  /* Check 
29fc7 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 69  to verify that i
29fc8 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f  t is possible to
29fc9 20 67 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b   get a read lock
29fca 20 6f 6e 20 61 6c 6c 0a 20 20 2a 2a 20 64 61 74   on all.  ** dat
29fcb 61 62 61 73 65 20 73 63 68 65 6d 61 73 2e 20 20  abase schemas.  
29fcc 54 68 65 20 69 6e 61 62 69 6c 69 74 79 20 74 6f  The inability to
29fcd 20 67 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b   get a read lock
29fce 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 0a   indicates that.
29fcf 20 20 2a 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20    ** some other 
29fd0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
29fd1 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  ion is holding a
29fd2 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 77 68 69   write-lock, whi
29fd3 63 68 20 69 6e 0a 20 20 2a 2a 20 74 75 72 6e 20  ch in.  ** turn 
29fd4 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6f  means that the o
29fd5 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ther connection 
29fd6 68 61 73 20 6d 61 64 65 20 75 6e 63 6f 6d 6d 69  has made uncommi
29fd7 74 74 65 64 20 63 68 61 6e 67 65 73 0a 20 20 2a  tted changes.  *
29fd8 2a 20 74 6f 20 74 68 65 20 73 63 68 65 6d 61 2e  * to the schema.
29fd9 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 72 65 20  .  **.  ** Were 
29fda 77 65 20 74 6f 20 70 72 6f 63 65 65 64 20 61 6e  we to proceed an
29fdb 64 20 70 72 65 70 61 72 65 20 74 68 65 20 73 74  d prepare the st
29fdc 61 74 65 6d 65 6e 74 20 61 67 61 69 6e 73 74 20  atement against 
29fdd 74 68 65 20 75 6e 63 6f 6d 6d 69 74 74 65 64 0a  the uncommitted.
29fde 20 20 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e    ** schema chan
29fdf 67 65 73 20 61 6e 64 20 69 66 20 74 68 6f 73 65  ges and if those
29fe0 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20   schema changes 
29fe1 61 72 65 20 73 75 62 73 65 71 75 65 6e 74 6c 79  are subsequently
29fe2 20 72 6f 6c 6c 65 64 0a 20 20 2a 2a 20 62 61 63   rolled.  ** bac
29fe3 6b 20 61 6e 64 20 64 69 66 66 65 72 65 6e 74 20  k and different 
29fe4 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65  changes are made
29fe5 20 69 6e 20 74 68 65 69 72 20 70 6c 61 63 65 2c   in their place,
29fe6 20 74 68 65 6e 20 77 68 65 6e 20 74 68 69 73 0a   then when this.
29fe7 20 20 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74    ** prepared st
29fe8 61 74 65 6d 65 6e 74 20 67 6f 65 73 20 74 6f 20  atement goes to 
29fe9 72 75 6e 20 74 68 65 20 73 63 68 65 6d 61 20 63  run the schema c
29fea 6f 6f 6b 69 65 20 77 6f 75 6c 64 20 66 61 69 6c  ookie would fail
29feb 20 74 6f 20 64 65 74 65 63 74 0a 20 20 2a 2a 20   to detect.  ** 
29fec 74 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67  the schema chang
29fed 65 2e 20 20 44 69 73 61 73 74 65 72 20 77 6f 75  e.  Disaster wou
29fee 6c 64 20 66 6f 6c 6c 6f 77 2e 0a 20 20 2a 2a 0a  ld follow..  **.
29fef 20 20 2a 2a 20 54 68 69 73 20 74 68 72 65 61 64    ** This thread
29ff0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 6f   is currently ho
29ff1 6c 64 69 6e 67 20 6d 75 74 65 78 65 73 20 6f 6e  lding mutexes on
29ff2 20 61 6c 6c 20 42 74 72 65 65 73 20 28 62 65 63   all Btrees (bec
29ff3 61 75 73 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65  ause.  ** of the
29ff4 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
29ff5 65 72 41 6c 6c 28 29 20 69 6e 20 73 71 6c 69 74  erAll() in sqlit
29ff6 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65  e3LockAndPrepare
29ff7 28 29 29 20 73 6f 20 69 74 0a 20 20 2a 2a 20 69  ()) so it.  ** i
29ff8 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66  s not possible f
29ff9 6f 72 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  or another threa
29ffa 64 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65 77  d to start a new
29ffb 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 0a 20   schema change. 
29ffc 20 2a 2a 20 77 68 69 6c 65 20 74 68 69 73 20 72   ** while this r
29ffd 6f 75 74 69 6e 65 20 69 73 20 72 75 6e 6e 69 6e  outine is runnin
29ffe 67 2e 20 20 48 65 6e 63 65 2c 20 77 65 20 64 6f  g.  Hence, we do
29fff 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 68 6f 6c   not need to hol
2a000 64 20 0a 20 20 2a 2a 20 6c 6f 63 6b 73 20 6f 6e  d .  ** locks on
2a001 20 74 68 65 20 73 63 68 65 6d 61 2c 20 77 65 20   the schema, we 
2a002 6a 75 73 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b  just need to mak
2a003 65 20 73 75 72 65 20 6e 6f 62 6f 64 79 20 65 6c  e sure nobody el
2a004 73 65 20 69 73 20 0a 20 20 2a 2a 20 68 6f 6c 64  se is .  ** hold
2a005 69 6e 67 20 74 68 65 6d 2e 0a 20 20 2a 2a 0a 20  ing them..  **. 
2a006 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 73 65   ** Note that se
2a007 74 74 69 6e 67 20 52 45 41 44 5f 55 4e 43 4f 4d  tting READ_UNCOM
2a008 4d 49 54 54 45 44 20 6f 76 65 72 72 69 64 65 73  MITTED overrides
2a009 20 6d 6f 73 74 20 6c 6f 63 6b 20 64 65 74 65 63   most lock detec
2a00a 74 69 6f 6e 2c 0a 20 20 2a 2a 20 62 75 74 20 69  tion,.  ** but i
2a00b 74 20 64 6f 65 73 20 2a 6e 6f 74 2a 20 6f 76 65  t does *not* ove
2a00c 72 72 69 64 65 20 73 63 68 65 6d 61 20 6c 6f 63  rride schema loc
2a00d 6b 20 64 65 74 65 63 74 69 6f 6e 2c 20 73 6f 20  k detection, so 
2a00e 74 68 69 73 20 61 6c 6c 20 73 74 69 6c 6c 0a 20  this all still. 
2a00f 20 2a 2a 20 77 6f 72 6b 73 20 65 76 65 6e 20 69   ** works even i
2a010 66 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54  f READ_UNCOMMITT
2a011 45 44 20 69 73 20 73 65 74 2e 0a 20 20 2a 2f 0a  ED is set..  */.
2a012 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
2a013 3e 6e 44 62 3b 20 69 2b 2b 29 20 7b 0a 20 20 20  >nDb; i++) {.   
2a014 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
2a015 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
2a016 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
2a017 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
2a018 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
2a019 78 28 70 42 74 29 20 29 3b 0a 20 20 20 20 20 20  x(pBt) );.      
2a01a 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
2a01b 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 70 42  eSchemaLocked(pB
2a01c 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  t);.      if( rc
2a01d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   ){.        cons
2a01e 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62  t char *zDb = db
2a01f 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a  ->aDb[i].zName;.
2a020 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2a021 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22 64 61  rror(db, rc, "da
2a022 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73  tabase schema is
2a023 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20 7a 44   locked: %s", zD
2a024 62 29 3b 0a 20 20 20 20 20 20 20 20 28 76 6f 69  b);.        (voi
2a025 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
2a026 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  ff(db);.        
2a027 74 65 73 74 63 61 73 65 28 20 64 62 2d 3e 66 6c  testcase( db->fl
2a028 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61  ags & SQLITE_Rea
2a029 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 29 3b 0a  dUncommitted );.
2a02a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
2a02b 5f 70 72 65 70 61 72 65 3b 0a 20 20 20 20 20 20  _prepare;.      
2a02c 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73  }.    }.  }..  s
2a02d 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
2a02e 4c 69 73 74 28 64 62 29 3b 0a 0a 20 20 70 50 61  List(db);..  pPa
2a02f 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20  rse->db = db;.  
2a030 69 66 28 20 6e 42 79 74 65 73 3e 3d 30 20 26 26  if( nBytes>=0 &&
2a031 20 28 6e 42 79 74 65 73 3d 3d 30 20 7c 7c 20 7a   (nBytes==0 || z
2a032 53 71 6c 5b 6e 42 79 74 65 73 2d 31 5d 21 3d 30  Sql[nBytes-1]!=0
2a033 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  ) ){.    char *z
2a034 53 71 6c 43 6f 70 79 3b 0a 20 20 20 20 69 6e 74  SqlCopy;.    int
2a035 20 6d 78 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c 69   mxLen = db->aLi
2a036 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
2a037 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20  _SQL_LENGTH];.  
2a038 20 20 74 65 73 74 63 61 73 65 28 20 6e 42 79 74    testcase( nByt
2a039 65 73 3d 3d 6d 78 4c 65 6e 20 29 3b 0a 20 20 20  es==mxLen );.   
2a03a 20 74 65 73 74 63 61 73 65 28 20 6e 42 79 74 65   testcase( nByte
2a03b 73 3d 3d 6d 78 4c 65 6e 2b 31 20 29 3b 0a 20 20  s==mxLen+1 );.  
2a03c 20 20 69 66 28 20 6e 42 79 74 65 73 3e 6d 78 4c    if( nBytes>mxL
2a03d 65 6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  en ){.      sqli
2a03e 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
2a03f 49 54 45 5f 54 4f 4f 42 49 47 2c 20 22 73 74 61  ITE_TOOBIG, "sta
2a040 74 65 6d 65 6e 74 20 74 6f 6f 20 6c 6f 6e 67 22  tement too long"
2a041 29 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  );.      (void)s
2a042 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
2a043 64 62 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  db);.      rc = 
2a044 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
2a045 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  b, SQLITE_TOOBIG
2a046 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  );.      goto en
2a047 64 5f 70 72 65 70 61 72 65 3b 0a 20 20 20 20 7d  d_prepare;.    }
2a048 0a 20 20 20 20 7a 53 71 6c 43 6f 70 79 20 3d 20  .    zSqlCopy = 
2a049 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
2a04a 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65  (db, zSql, nByte
2a04b 73 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 71 6c  s);.    if( zSql
2a04c 43 6f 70 79 20 29 7b 0a 20 20 20 20 20 20 73 71  Copy ){.      sq
2a04d 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 70  lite3RunParser(p
2a04e 50 61 72 73 65 2c 20 7a 53 71 6c 43 6f 70 79 2c  Parse, zSqlCopy,
2a04f 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20   &zErrMsg);.    
2a050 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2a051 64 62 2c 20 7a 53 71 6c 43 6f 70 79 29 3b 0a 20  db, zSqlCopy);. 
2a052 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 54 61       pParse->zTa
2a053 69 6c 20 3d 20 26 7a 53 71 6c 5b 70 50 61 72 73  il = &zSql[pPars
2a054 65 2d 3e 7a 54 61 69 6c 2d 7a 53 71 6c 43 6f 70  e->zTail-zSqlCop
2a055 79 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  y];.    }else{. 
2a056 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 54 61       pParse->zTa
2a057 69 6c 20 3d 20 26 7a 53 71 6c 5b 6e 42 79 74 65  il = &zSql[nByte
2a058 73 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  s];.    }.  }els
2a059 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 75  e{.    sqlite3Ru
2a05a 6e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c 20  nParser(pParse, 
2a05b 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b  zSql, &zErrMsg);
2a05c 0a 20 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e  .  }..  if( db->
2a05d 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2a05e 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d      pParse->rc =
2a05f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2a060 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d   }.  if( pParse-
2a061 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  >rc==SQLITE_DONE
2a062 20 29 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20   ) pParse->rc = 
2a063 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
2a064 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63   pParse->checkSc
2a065 68 65 6d 61 20 29 7b 0a 20 20 20 20 73 63 68 65  hema ){.    sche
2a066 6d 61 49 73 56 61 6c 69 64 28 70 50 61 72 73 65  maIsValid(pParse
2a067 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  );.  }.  if( pPa
2a068 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  rse->rc==SQLITE_
2a069 53 43 48 45 4d 41 20 29 7b 0a 20 20 20 20 73 71  SCHEMA ){.    sq
2a06a 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
2a06b 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b  alSchema(db, 0);
2a06c 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d  .  }.  if( db->m
2a06d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2a06e 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
2a06f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2a070 7d 0a 20 20 69 66 28 20 70 7a 54 61 69 6c 20 29  }.  if( pzTail )
2a071 7b 0a 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20  {.    *pzTail = 
2a072 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 3b 0a 20  pParse->zTail;. 
2a073 20 7d 0a 20 20 72 63 20 3d 20 70 50 61 72 73 65   }.  rc = pParse
2a074 2d 3e 72 63 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ->rc;..#ifndef S
2a075 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
2a076 49 4e 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  IN.  if( rc==SQL
2a077 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 72 73 65  ITE_OK && pParse
2a078 2d 3e 70 56 64 62 65 20 26 26 20 70 50 61 72 73  ->pVdbe && pPars
2a079 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20  e->explain ){.  
2a07a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
2a07b 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a 43 6f  har * const azCo
2a07c 6c 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20  lName[] = {.    
2a07d 20 20 20 22 61 64 64 72 22 2c 20 22 6f 70 63 6f     "addr", "opco
2a07e 64 65 22 2c 20 22 70 31 22 2c 20 22 70 32 22 2c  de", "p1", "p2",
2a07f 20 22 70 33 22 2c 20 22 70 34 22 2c 20 22 70 35   "p3", "p4", "p5
2a080 22 2c 20 22 63 6f 6d 6d 65 6e 74 22 2c 0a 20 20  ", "comment",.  
2a081 20 20 20 20 20 22 6f 72 64 65 72 22 2c 20 22 66       "order", "f
2a082 72 6f 6d 22 2c 20 22 64 65 74 61 69 6c 22 0a 20  rom", "detail". 
2a083 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 69 46     };.    int iF
2a084 69 72 73 74 2c 20 6d 78 3b 0a 20 20 20 20 69 66  irst, mx;.    if
2a085 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69  ( pParse->explai
2a086 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 73 71  n==2 ){.      sq
2a087 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
2a088 6f 6c 73 28 70 50 61 72 73 65 2d 3e 70 56 64 62  ols(pParse->pVdb
2a089 65 2c 20 33 29 3b 0a 20 20 20 20 20 20 69 46 69  e, 3);.      iFi
2a08a 72 73 74 20 3d 20 38 3b 0a 20 20 20 20 20 20 6d  rst = 8;.      m
2a08b 78 20 3d 20 31 31 3b 0a 20 20 20 20 7d 65 6c 73  x = 11;.    }els
2a08c 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
2a08d 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 70  VdbeSetNumCols(p
2a08e 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 38 29  Parse->pVdbe, 8)
2a08f 3b 0a 20 20 20 20 20 20 69 46 69 72 73 74 20 3d  ;.      iFirst =
2a090 20 30 3b 0a 20 20 20 20 20 20 6d 78 20 3d 20 38   0;.      mx = 8
2a091 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
2a092 69 3d 69 46 69 72 73 74 3b 20 69 3c 6d 78 3b 20  i=iFirst; i<mx; 
2a093 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
2a094 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2a095 65 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  e(pParse->pVdbe,
2a096 20 69 2d 69 46 69 72 73 74 2c 20 43 4f 4c 4e 41   i-iFirst, COLNA
2a097 4d 45 5f 4e 41 4d 45 2c 0a 20 20 20 20 20 20 20  ME_NAME,.       
2a098 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a099 20 20 20 20 20 61 7a 43 6f 6c 4e 61 6d 65 5b 69       azColName[i
2a09a 5d 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ], SQLITE_STATIC
2a09b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  );.    }.  }.#en
2a09c 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  dif..  if( sqlit
2a09d 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
2a09e 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
2a09f 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a  TE_MISUSE;.  }..
2a0a0 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e    assert( db->in
2a0a1 69 74 2e 62 75 73 79 3d 3d 30 20 7c 7c 20 73 61  it.busy==0 || sa
2a0a2 76 65 53 71 6c 46 6c 61 67 3d 3d 30 20 29 3b 0a  veSqlFlag==0 );.
2a0a3 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
2a0a4 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20 20 56 64  usy==0 ){.    Vd
2a0a5 62 65 20 2a 70 56 64 62 65 20 3d 20 70 50 61 72  be *pVdbe = pPar
2a0a6 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20 20 73  se->pVdbe;.    s
2a0a7 71 6c 69 74 65 33 56 64 62 65 53 65 74 53 71 6c  qlite3VdbeSetSql
2a0a8 28 70 56 64 62 65 2c 20 7a 53 71 6c 2c 20 28 69  (pVdbe, zSql, (i
2a0a9 6e 74 29 28 70 50 61 72 73 65 2d 3e 7a 54 61 69  nt)(pParse->zTai
2a0aa 6c 2d 7a 53 71 6c 29 2c 20 73 61 76 65 53 71 6c  l-zSql), saveSql
2a0ab 46 6c 61 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28  Flag);.  }.  if(
2a0ac 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 26   pParse->pVdbe &
2a0ad 26 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  & (rc!=SQLITE_OK
2a0ae 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
2a0af 69 6c 65 64 29 20 29 7b 0a 20 20 20 20 73 71 6c  iled) ){.    sql
2a0b0 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
2a0b1 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b  (pParse->pVdbe);
2a0b2 0a 20 20 20 20 61 73 73 65 72 74 28 21 28 2a 70  .    assert(!(*p
2a0b3 70 53 74 6d 74 29 29 3b 0a 20 20 7d 65 6c 73 65  pStmt));.  }else
2a0b4 7b 0a 20 20 20 20 2a 70 70 53 74 6d 74 20 3d 20  {.    *ppStmt = 
2a0b5 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70  (sqlite3_stmt*)p
2a0b6 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
2a0b7 7d 0a 0a 20 20 69 66 28 20 7a 45 72 72 4d 73 67  }..  if( zErrMsg
2a0b8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2a0b9 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22 25 73  rror(db, rc, "%s
2a0ba 22 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  ", zErrMsg);.   
2a0bb 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2a0bc 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d  b, zErrMsg);.  }
2a0bd 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
2a0be 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30  3Error(db, rc, 0
2a0bf 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c  );.  }..  /* Del
2a0c0 65 74 65 20 61 6e 79 20 54 72 69 67 67 65 72 50  ete any TriggerP
2a0c1 72 67 20 73 74 72 75 63 74 75 72 65 73 20 61 6c  rg structures al
2a0c2 6c 6f 63 61 74 65 64 20 77 68 69 6c 65 20 70 61  located while pa
2a0c3 72 73 69 6e 67 20 74 68 69 73 20 73 74 61 74 65  rsing this state
2a0c4 6d 65 6e 74 2e 20 2a 2f 0a 20 20 77 68 69 6c 65  ment. */.  while
2a0c5 28 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67  ( pParse->pTrigg
2a0c6 65 72 50 72 67 20 29 7b 0a 20 20 20 20 54 72 69  erPrg ){.    Tri
2a0c7 67 67 65 72 50 72 67 20 2a 70 54 20 3d 20 70 50  ggerPrg *pT = pP
2a0c8 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 50 72  arse->pTriggerPr
2a0c9 67 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70  g;.    pParse->p
2a0ca 54 72 69 67 67 65 72 50 72 67 20 3d 20 70 54 2d  TriggerPrg = pT-
2a0cb 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69  >pNext;.    sqli
2a0cc 74 65 33 56 64 62 65 50 72 6f 67 72 61 6d 44 65  te3VdbeProgramDe
2a0cd 6c 65 74 65 28 64 62 2c 20 70 54 2d 3e 70 50 72  lete(db, pT->pPr
2a0ce 6f 67 72 61 6d 2c 20 30 29 3b 0a 20 20 20 20 73  ogram, 0);.    s
2a0cf 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2a0d0 20 70 54 29 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 70   pT);.  }..end_p
2a0d1 72 65 70 61 72 65 3a 0a 0a 20 20 73 71 6c 69 74  repare:..  sqlit
2a0d2 65 33 53 74 61 63 6b 46 72 65 65 28 64 62 2c 20  e3StackFree(db, 
2a0d3 70 50 61 72 73 65 29 3b 0a 20 20 72 63 20 3d 20  pParse);.  rc = 
2a0d4 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
2a0d5 62 2c 20 72 63 29 3b 0a 20 20 61 73 73 65 72 74  b, rc);.  assert
2a0d6 28 20 28 72 63 26 64 62 2d 3e 65 72 72 4d 61 73  ( (rc&db->errMas
2a0d7 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 72 65 74 75  k)==rc );.  retu
2a0d8 72 6e 20 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20  rn rc;.}.static 
2a0d9 69 6e 74 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41  int sqlite3LockA
2a0da 6e 64 50 72 65 70 61 72 65 28 0a 20 20 73 71 6c  ndPrepare(.  sql
2a0db 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2a0dc 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2a0dd 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20  se handle. */.  
2a0de 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
2a0df 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46  ,         /* UTF
2a0e0 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73  -8 encoded SQL s
2a0e1 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69  tatement. */.  i
2a0e2 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20  nt nBytes,      
2a0e3 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
2a0e4 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79  th of zSql in by
2a0e5 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61  tes. */.  int sa
2a0e6 76 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20  veSqlFlag,      
2a0e7 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63      /* True to c
2a0e8 6f 70 79 20 53 51 4c 20 74 65 78 74 20 69 6e 74  opy SQL text int
2a0e9 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74  o the sqlite3_st
2a0ea 6d 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 4f  mt */.  Vdbe *pO
2a0eb 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ld,             
2a0ec 20 20 2f 2a 20 56 4d 20 62 65 69 6e 67 20 72 65    /* VM being re
2a0ed 70 72 65 70 61 72 65 64 20 2a 2f 0a 20 20 73 71  prepared */.  sq
2a0ee 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
2a0ef 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
2a0f0 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
2a0f1 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2a0f2 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ent */.  const c
2a0f3 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
2a0f4 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
2a0f5 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
2a0f6 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
2a0f7 20 20 61 73 73 65 72 74 28 20 70 70 53 74 6d 74    assert( ppStmt
2a0f8 21 3d 30 20 29 3b 0a 20 20 2a 70 70 53 74 6d 74  !=0 );.  *ppStmt
2a0f9 20 3d 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c   = 0;.  if( !sql
2a0fa 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f  ite3SafetyCheckO
2a0fb 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74  k(db) ){.    ret
2a0fc 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
2a0fd 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  E;.  }.  sqlite3
2a0fe 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
2a0ff 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74  >mutex);.  sqlit
2a100 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
2a101 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  db);.  rc = sqli
2a102 74 65 33 50 72 65 70 61 72 65 28 64 62 2c 20 7a  te3Prepare(db, z
2a103 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20 73 61 76  Sql, nBytes, sav
2a104 65 53 71 6c 46 6c 61 67 2c 20 70 4f 6c 64 2c 20  eSqlFlag, pOld, 
2a105 70 70 53 74 6d 74 2c 20 70 7a 54 61 69 6c 29 3b  ppStmt, pzTail);
2a106 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2a107 45 5f 53 43 48 45 4d 41 20 29 7b 0a 20 20 20 20  E_SCHEMA ){.    
2a108 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
2a109 28 2a 70 70 53 74 6d 74 29 3b 0a 20 20 20 20 72  (*ppStmt);.    r
2a10a 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61  c = sqlite3Prepa
2a10b 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79  re(db, zSql, nBy
2a10c 74 65 73 2c 20 73 61 76 65 53 71 6c 46 6c 61 67  tes, saveSqlFlag
2a10d 2c 20 70 4f 6c 64 2c 20 70 70 53 74 6d 74 2c 20  , pOld, ppStmt, 
2a10e 70 7a 54 61 69 6c 29 3b 0a 20 20 7d 0a 20 20 73  pzTail);.  }.  s
2a10f 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
2a110 41 6c 6c 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  All(db);.  sqlit
2a111 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2a112 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
2a113 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
2a114 20 52 65 72 75 6e 20 74 68 65 20 63 6f 6d 70 69   Rerun the compi
2a115 6c 61 74 69 6f 6e 20 6f 66 20 61 20 73 74 61 74  lation of a stat
2a116 65 6d 65 6e 74 20 61 66 74 65 72 20 61 20 73 63  ement after a sc
2a117 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a 2a 2a 0a  hema change..**.
2a118 2a 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 6d  ** If the statem
2a119 65 6e 74 20 69 73 20 73 75 63 63 65 73 73 66 75  ent is successfu
2a11a 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 2c 20  lly recompiled, 
2a11b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2a11c 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20  . Otherwise,.** 
2a11d 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  if the statement
2a11e 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 63 6f 6d   cannot be recom
2a11f 70 69 6c 65 64 20 62 65 63 61 75 73 65 20 61 6e  piled because an
2a120 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  other connection
2a121 20 68 61 73 0a 2a 2a 20 6c 6f 63 6b 65 64 20 74   has.** locked t
2a122 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 73 74 65  he sqlite3_maste
2a123 72 20 74 61 62 6c 65 2c 20 72 65 74 75 72 6e 20  r table, return 
2a124 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 20 49  SQLITE_LOCKED. I
2a125 66 20 61 6e 79 20 6f 74 68 65 72 20 65 72 72 6f  f any other erro
2a126 72 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 72 65 74  r.** occurs, ret
2a127 75 72 6e 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  urn SQLITE_SCHEM
2a128 41 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  A..*/.SQLITE_PRI
2a129 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2a12a 52 65 70 72 65 70 61 72 65 28 56 64 62 65 20 2a  Reprepare(Vdbe *
2a12b 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  p){.  int rc;.  
2a12c 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e  sqlite3_stmt *pN
2a12d 65 77 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ew;.  const char
2a12e 20 2a 7a 53 71 6c 3b 0a 20 20 73 71 6c 69 74 65   *zSql;.  sqlite
2a12f 33 20 2a 64 62 3b 0a 0a 20 20 61 73 73 65 72 74  3 *db;..  assert
2a130 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
2a131 68 65 6c 64 28 73 71 6c 69 74 65 33 56 64 62 65  held(sqlite3Vdbe
2a132 44 62 28 70 29 2d 3e 6d 75 74 65 78 29 20 29 3b  Db(p)->mutex) );
2a133 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  .  zSql = sqlite
2a134 33 5f 73 71 6c 28 28 73 71 6c 69 74 65 33 5f 73  3_sql((sqlite3_s
2a135 74 6d 74 20 2a 29 70 29 3b 0a 20 20 61 73 73 65  tmt *)p);.  asse
2a136 72 74 28 20 7a 53 71 6c 21 3d 30 20 29 3b 20 20  rt( zSql!=0 );  
2a137 2f 2a 20 52 65 70 72 65 70 61 72 65 20 6f 6e 6c  /* Reprepare onl
2a138 79 20 63 61 6c 6c 65 64 20 66 6f 72 20 70 72 65  y called for pre
2a139 70 61 72 65 5f 76 32 28 29 20 73 74 61 74 65 6d  pare_v2() statem
2a13a 65 6e 74 73 20 2a 2f 0a 20 20 64 62 20 3d 20 73  ents */.  db = s
2a13b 71 6c 69 74 65 33 56 64 62 65 44 62 28 70 29 3b  qlite3VdbeDb(p);
2a13c 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2a13d 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
2a13e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63  ->mutex) );.  rc
2a13f 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e   = sqlite3LockAn
2a140 64 50 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71  dPrepare(db, zSq
2a141 6c 2c 20 2d 31 2c 20 30 2c 20 70 2c 20 26 70 4e  l, -1, 0, p, &pN
2a142 65 77 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  ew, 0);.  if( rc
2a143 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   ){.    if( rc==
2a144 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
2a145 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
2a146 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
2a147 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  }.    assert( pN
2a148 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74  ew==0 );.    ret
2a149 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  urn (rc==SQLITE_
2a14a 4c 4f 43 4b 45 44 29 20 3f 20 53 51 4c 49 54 45  LOCKED) ? SQLITE
2a14b 5f 4c 4f 43 4b 45 44 20 3a 20 53 51 4c 49 54 45  _LOCKED : SQLITE
2a14c 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 65 6c 73 65  _SCHEMA;.  }else
2a14d 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  {.    assert( pN
2a14e 65 77 21 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 73  ew!=0 );.  }.  s
2a14f 71 6c 69 74 65 33 56 64 62 65 53 77 61 70 28 28  qlite3VdbeSwap((
2a150 56 64 62 65 2a 29 70 4e 65 77 2c 20 70 29 3b 0a  Vdbe*)pNew, p);.
2a151 20 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65    sqlite3Transfe
2a152 72 42 69 6e 64 69 6e 67 73 28 70 4e 65 77 2c 20  rBindings(pNew, 
2a153 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70  (sqlite3_stmt*)p
2a154 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2a155 52 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 28  ResetStepResult(
2a156 28 56 64 62 65 2a 29 70 4e 65 77 29 3b 0a 20 20  (Vdbe*)pNew);.  
2a157 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
2a158 69 7a 65 28 28 56 64 62 65 2a 29 70 4e 65 77 29  ize((Vdbe*)pNew)
2a159 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2a15a 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  E_OK;.}.../*.** 
2a15b 54 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  Two versions of 
2a15c 74 68 65 20 6f 66 66 69 63 69 61 6c 20 41 50 49  the official API
2a15d 2e 20 20 4c 65 67 61 63 79 20 61 6e 64 20 6e 65  .  Legacy and ne
2a15e 77 20 75 73 65 2e 20 20 49 6e 20 74 68 65 20 6c  w use.  In the l
2a15f 65 67 61 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e  egacy.** version
2a160 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53  , the original S
2a161 51 4c 20 74 65 78 74 20 69 73 20 6e 6f 74 20 73  QL text is not s
2a162 61 76 65 64 20 69 6e 20 74 68 65 20 70 72 65 70  aved in the prep
2a163 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ared statement.*
2a164 2a 20 61 6e 64 20 73 6f 20 69 66 20 61 20 73 63  * and so if a sc
2a165 68 65 6d 61 20 63 68 61 6e 67 65 20 6f 63 63 75  hema change occu
2a166 72 73 2c 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  rs, SQLITE_SCHEM
2a167 41 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79  A is returned by
2a168 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70  .** sqlite3_step
2a169 28 29 2e 20 20 49 6e 20 74 68 65 20 6e 65 77 20  ().  In the new 
2a16a 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69  version, the ori
2a16b 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69  ginal SQL text i
2a16c 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e  s retained.** an
2a16d 64 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  d the statement 
2a16e 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
2a16f 20 72 65 63 6f 6d 70 69 6c 65 64 20 69 66 20 61   recompiled if a
2a170 6e 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 0a  n schema change.
2a171 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51  ** occurs..*/.SQ
2a172 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2a173 69 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20 20  ite3_prepare(.  
2a174 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2a175 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2a176 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f  abase handle. */
2a177 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2a178 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Sql,         /* 
2a179 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51  UTF-8 encoded SQ
2a17a 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a  L statement. */.
2a17b 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20    int nBytes,   
2a17c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2a17d 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
2a17e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
2a17f 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
2a180 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
2a181 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2a182 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2a183 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  nt */.  const ch
2a184 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  ar **pzTail     
2a185 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
2a186 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
2a187 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
2a188 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63   rc = sqlite3Loc
2a189 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c 7a  kAndPrepare(db,z
2a18a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 30 2c 70  Sql,nBytes,0,0,p
2a18b 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20  pStmt,pzTail);. 
2a18c 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
2a18d 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74  ITE_OK || ppStmt
2a18e 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d  ==0 || *ppStmt==
2a18f 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a  0 );  /* VERIFY:
2a190 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74   F13021 */.  ret
2a191 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45  urn rc;.}.SQLITE
2a192 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2a193 5f 70 72 65 70 61 72 65 5f 76 32 28 0a 20 20 73  _prepare_v2(.  s
2a194 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2a195 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2a196 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a  base handle. */.
2a197 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
2a198 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55  ql,         /* U
2a199 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c  TF-8 encoded SQL
2a19a 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
2a19b 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20   int nBytes,    
2a19c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
2a19d 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
2a19e 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  bytes. */.  sqli
2a19f 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
2a1a0 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20  t,    /* OUT: A 
2a1a1 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
2a1a2 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2a1a3 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
2a1a4 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20  r **pzTail      
2a1a5 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20   /* OUT: End of 
2a1a6 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f  parsed string */
2a1a7 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
2a1a8 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b  rc = sqlite3Lock
2a1a9 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c 7a 53  AndPrepare(db,zS
2a1aa 71 6c 2c 6e 42 79 74 65 73 2c 31 2c 30 2c 70 70  ql,nBytes,1,0,pp
2a1ab 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20  Stmt,pzTail);.  
2a1ac 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
2a1ad 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d  TE_OK || ppStmt=
2a1ae 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30  =0 || *ppStmt==0
2a1af 20 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20   );  /* VERIFY: 
2a1b0 46 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75  F13021 */.  retu
2a1b1 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69 66 6e 64  rn rc;.}...#ifnd
2a1b2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
2a1b3 54 46 31 36 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69  TF16./*.** Compi
2a1b4 6c 65 20 74 68 65 20 55 54 46 2d 31 36 20 65 6e  le the UTF-16 en
2a1b5 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
2a1b6 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f 20 61 20  ent zSql into a 
2a1b7 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
2a1b8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2a1b9 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 31 36  sqlite3Prepare16
2a1ba 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2a1bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a1bc 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
2a1bd 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76 6f  . */ .  const vo
2a1be 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  id *zSql,       
2a1bf 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64    /* UTF-8 encod
2a1c0 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
2a1c1 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  . */.  int nByte
2a1c2 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
2a1c3 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53   /* Length of zS
2a1c4 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
2a1c5 20 20 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61    int saveSqlFla
2a1c6 67 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  g,          /* T
2a1c7 72 75 65 20 74 6f 20 73 61 76 65 20 53 51 4c 20  rue to save SQL 
2a1c8 74 65 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71  text into the sq
2a1c9 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20  lite3_stmt */.  
2a1ca 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70  sqlite3_stmt **p
2a1cb 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54  pStmt,    /* OUT
2a1cc 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  : A pointer to t
2a1cd 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
2a1ce 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ement */.  const
2a1cf 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20   void **pzTail  
2a1d0 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64       /* OUT: End
2a1d1 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e   of parsed strin
2a1d2 67 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 54 68 69  g */.){.  /* Thi
2a1d3 73 20 66 75 6e 63 74 69 6f 6e 20 63 75 72 72 65  s function curre
2a1d4 6e 74 6c 79 20 77 6f 72 6b 73 20 62 79 20 66 69  ntly works by fi
2a1d5 72 73 74 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67  rst transforming
2a1d6 20 74 68 65 20 55 54 46 2d 31 36 0a 20 20 2a 2a   the UTF-16.  **
2a1d7 20 65 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 20   encoded string 
2a1d8 74 6f 20 55 54 46 2d 38 2c 20 74 68 65 6e 20 69  to UTF-8, then i
2a1d9 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f  nvoking sqlite3_
2a1da 70 72 65 70 61 72 65 28 29 2e 20 54 68 65 0a 20  prepare(). The. 
2a1db 20 2a 2a 20 74 72 69 63 6b 79 20 62 69 74 20 69   ** tricky bit i
2a1dc 73 20 66 69 67 75 72 69 6e 67 20 6f 75 74 20 74  s figuring out t
2a1dd 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 72 65  he pointer to re
2a1de 74 75 72 6e 20 69 6e 20 2a 70 7a 54 61 69 6c 2e  turn in *pzTail.
2a1df 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53  .  */.  char *zS
2a1e0 71 6c 38 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ql8;.  const cha
2a1e1 72 20 2a 7a 54 61 69 6c 38 20 3d 20 30 3b 0a 20  r *zTail8 = 0;. 
2a1e2 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
2a1e3 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  _OK;..  assert( 
2a1e4 70 70 53 74 6d 74 20 29 3b 0a 20 20 2a 70 70 53  ppStmt );.  *ppS
2a1e5 74 6d 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 21  tmt = 0;.  if( !
2a1e6 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
2a1e7 63 6b 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  ckOk(db) ){.    
2a1e8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
2a1e9 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  SUSE;.  }.  sqli
2a1ea 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2a1eb 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7a 53  db->mutex);.  zS
2a1ec 71 6c 38 20 3d 20 73 71 6c 69 74 65 33 55 74 66  ql8 = sqlite3Utf
2a1ed 31 36 74 6f 38 28 64 62 2c 20 7a 53 71 6c 2c 20  16to8(db, zSql, 
2a1ee 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 7a  nBytes);.  if( z
2a1ef 53 71 6c 38 20 29 7b 0a 20 20 20 20 72 63 20 3d  Sql8 ){.    rc =
2a1f0 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50   sqlite3LockAndP
2a1f1 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 38  repare(db, zSql8
2a1f2 2c 20 2d 31 2c 20 73 61 76 65 53 71 6c 46 6c 61  , -1, saveSqlFla
2a1f3 67 2c 20 30 2c 20 70 70 53 74 6d 74 2c 20 26 7a  g, 0, ppStmt, &z
2a1f4 54 61 69 6c 38 29 3b 0a 20 20 7d 0a 0a 20 20 69  Tail8);.  }..  i
2a1f5 66 28 20 7a 54 61 69 6c 38 20 26 26 20 70 7a 54  f( zTail8 && pzT
2a1f6 61 69 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  ail ){.    /* If
2a1f7 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
2a1f8 20 72 65 74 75 72 6e 73 20 61 20 74 61 69 6c 20   returns a tail 
2a1f9 70 6f 69 6e 74 65 72 2c 20 77 65 20 63 61 6c 63  pointer, we calc
2a1fa 75 6c 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a  ulate the.    **
2a1fb 20 65 71 75 69 76 61 6c 65 6e 74 20 70 6f 69 6e   equivalent poin
2a1fc 74 65 72 20 69 6e 74 6f 20 74 68 65 20 55 54 46  ter into the UTF
2a1fd 2d 31 36 20 73 74 72 69 6e 67 20 62 79 20 63 6f  -16 string by co
2a1fe 75 6e 74 69 6e 67 20 74 68 65 20 75 6e 69 63 6f  unting the unico
2a1ff 64 65 0a 20 20 20 20 2a 2a 20 63 68 61 72 61 63  de.    ** charac
2a200 74 65 72 73 20 62 65 74 77 65 65 6e 20 7a 53 71  ters between zSq
2a201 6c 38 20 61 6e 64 20 7a 54 61 69 6c 38 2c 20 61  l8 and zTail8, a
2a202 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e 69 6e  nd then returnin
2a203 67 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 20 20  g a pointer.    
2a204 2a 2a 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62  ** the same numb
2a205 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
2a206 20 69 6e 74 6f 20 74 68 65 20 55 54 46 2d 31 36   into the UTF-16
2a207 20 73 74 72 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a   string..    */.
2a208 20 20 20 20 69 6e 74 20 63 68 61 72 73 5f 70 61      int chars_pa
2a209 72 73 65 64 20 3d 20 73 71 6c 69 74 65 33 55 74  rsed = sqlite3Ut
2a20a 66 38 43 68 61 72 4c 65 6e 28 7a 53 71 6c 38 2c  f8CharLen(zSql8,
2a20b 20 28 69 6e 74 29 28 7a 54 61 69 6c 38 2d 7a 53   (int)(zTail8-zS
2a20c 71 6c 38 29 29 3b 0a 20 20 20 20 2a 70 7a 54 61  ql8));.    *pzTa
2a20d 69 6c 20 3d 20 28 75 38 20 2a 29 7a 53 71 6c 20  il = (u8 *)zSql 
2a20e 2b 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79  + sqlite3Utf16By
2a20f 74 65 4c 65 6e 28 7a 53 71 6c 2c 20 63 68 61 72  teLen(zSql, char
2a210 73 5f 70 61 72 73 65 64 29 3b 0a 20 20 7d 0a 20  s_parsed);.  }. 
2a211 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2a212 62 2c 20 7a 53 71 6c 38 29 3b 20 0a 20 20 72 63  b, zSql8); .  rc
2a213 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
2a214 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
2a215 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2a216 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
2a217 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
2a218 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f 6e 73 20  ** Two versions 
2a219 6f 66 20 74 68 65 20 6f 66 66 69 63 69 61 6c 20  of the official 
2a21a 41 50 49 2e 20 20 4c 65 67 61 63 79 20 61 6e 64  API.  Legacy and
2a21b 20 6e 65 77 20 75 73 65 2e 20 20 49 6e 20 74 68   new use.  In th
2a21c 65 20 6c 65 67 61 63 79 0a 2a 2a 20 76 65 72 73  e legacy.** vers
2a21d 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61  ion, the origina
2a21e 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 6e 6f  l SQL text is no
2a21f 74 20 73 61 76 65 64 20 69 6e 20 74 68 65 20 70  t saved in the p
2a220 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2a221 74 0a 2a 2a 20 61 6e 64 20 73 6f 20 69 66 20 61  t.** and so if a
2a222 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 6f   schema change o
2a223 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 53 43  ccurs, SQLITE_SC
2a224 48 45 4d 41 20 69 73 20 72 65 74 75 72 6e 65 64  HEMA is returned
2a225 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73   by.** sqlite3_s
2a226 74 65 70 28 29 2e 20 20 49 6e 20 74 68 65 20 6e  tep().  In the n
2a227 65 77 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20  ew version, the 
2a228 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78  original SQL tex
2a229 74 20 69 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a  t is retained.**
2a22a 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65   and the stateme
2a22b 6e 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  nt is automatica
2a22c 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 69  lly recompiled i
2a22d 66 20 61 6e 20 73 63 68 65 6d 61 20 63 68 61 6e  f an schema chan
2a22e 67 65 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f  ge.** occurs..*/
2a22f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2a230 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
2a231 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  6(.  sqlite3 *db
2a232 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2a233 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
2a234 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76  e. */ .  const v
2a235 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  oid *zSql,      
2a236 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f     /* UTF-8 enco
2a237 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ded SQL statemen
2a238 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  t. */.  int nByt
2a239 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  es,             
2a23a 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a    /* Length of z
2a23b 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
2a23c 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2a23d 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20  **ppStmt,    /* 
2a23e 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74  OUT: A pointer t
2a23f 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  o the prepared s
2a240 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f  tatement */.  co
2a241 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
2a242 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  l       /* OUT: 
2a243 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74  End of parsed st
2a244 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ring */.){.  int
2a245 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69   rc;.  rc = sqli
2a246 74 65 33 50 72 65 70 61 72 65 31 36 28 64 62 2c  te3Prepare16(db,
2a247 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70 70  zSql,nBytes,0,pp
2a248 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20  Stmt,pzTail);.  
2a249 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
2a24a 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d  TE_OK || ppStmt=
2a24b 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30  =0 || *ppStmt==0
2a24c 20 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20   );  /* VERIFY: 
2a24d 46 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75  F13021 */.  retu
2a24e 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
2a24f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2a250 70 72 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20  prepare16_v2(.  
2a251 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2a252 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2a253 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f  abase handle. */
2a254 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
2a255 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a  zSql,         /*
2a256 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
2a257 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
2a258 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  .  int nBytes,  
2a259 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a25a 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  Length of zSql i
2a25b 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71  n bytes. */.  sq
2a25c 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
2a25d 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
2a25e 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
2a25f 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2a260 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ent */.  const v
2a261 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  oid **pzTail    
2a262 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
2a263 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
2a264 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
2a265 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72    rc = sqlite3Pr
2a266 65 70 61 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c  epare16(db,zSql,
2a267 6e 42 79 74 65 73 2c 31 2c 70 70 53 74 6d 74 2c  nBytes,1,ppStmt,
2a268 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72  pzTail);.  asser
2a269 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
2a26a 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c   || ppStmt==0 ||
2a26b 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20   *ppStmt==0 );  
2a26c 2f 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32  /* VERIFY: F1302
2a26d 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63  1 */.  return rc
2a26e 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
2a26f 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2a270 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
2a271 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 65 70  **** End of prep
2a272 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  are.c **********
2a273 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a274 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a275 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2a276 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2a277 73 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  select.c *******
2a278 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a279 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a27a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
2a27b 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
2a27c 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2a27d 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2a27e 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2a27f 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2a280 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2a281 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2a282 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2a283 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2a284 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2a285 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2a286 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2a287 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2a288 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2a289 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2a28a 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2a28b 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2a28c 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2a28d 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2a28e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a28f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a291 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
2a292 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
2a293 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65  s C code routine
2a294 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65  s that are calle
2a295 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0a  d by the parser.
2a296 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 53 45 4c  ** to handle SEL
2a297 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ECT statements i
2a298 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  n SQLite..**.** 
2a299 24 49 64 3a 20 73 65 6c 65 63 74 2e 63 2c 76 20  $Id: select.c,v 
2a29a 31 2e 35 32 36 20 32 30 30 39 2f 30 38 2f 30 31  1.526 2009/08/01
2a29b 20 31 35 3a 30 39 3a 35 38 20 64 72 68 20 45 78   15:09:58 drh Ex
2a29c 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 44  p $.*/.../*.** D
2a29d 65 6c 65 74 65 20 61 6c 6c 20 74 68 65 20 63 6f  elete all the co
2a29e 6e 74 65 6e 74 20 6f 66 20 61 20 53 65 6c 65 63  ntent of a Selec
2a29f 74 20 73 74 72 75 63 74 75 72 65 20 62 75 74 20  t structure but 
2a2a0 64 6f 20 6e 6f 74 20 64 65 61 6c 6c 6f 63 61 74  do not deallocat
2a2a1 65 0a 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 20  e.** the select 
2a2a2 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66  structure itself
2a2a3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2a2a4 20 63 6c 65 61 72 53 65 6c 65 63 74 28 73 71 6c   clearSelect(sql
2a2a5 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74  ite3 *db, Select
2a2a6 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 45   *p){.  sqlite3E
2a2a7 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2a2a8 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  , p->pEList);.  
2a2a9 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
2a2aa 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 53 72 63  lete(db, p->pSrc
2a2ab 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2a2ac 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 57  Delete(db, p->pW
2a2ad 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  here);.  sqlite3
2a2ae 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
2a2af 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 3b  b, p->pGroupBy);
2a2b0 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
2a2b1 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 48 61 76  lete(db, p->pHav
2a2b2 69 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ing);.  sqlite3E
2a2b3 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2a2b4 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a  , p->pOrderBy);.
2a2b5 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2a2b6 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 50 72  elete(db, p->pPr
2a2b7 69 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ior);.  sqlite3E
2a2b8 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
2a2b9 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71 6c 69  >pLimit);.  sqli
2a2ba 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2a2bb 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 7d  , p->pOffset);.}
2a2bc 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
2a2bd 7a 65 20 61 20 53 65 6c 65 63 74 44 65 73 74 20  ze a SelectDest 
2a2be 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
2a2bf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2a2c0 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  d sqlite3SelectD
2a2c1 65 73 74 49 6e 69 74 28 53 65 6c 65 63 74 44 65  estInit(SelectDe
2a2c2 73 74 20 2a 70 44 65 73 74 2c 20 69 6e 74 20 65  st *pDest, int e
2a2c3 44 65 73 74 2c 20 69 6e 74 20 69 50 61 72 6d 29  Dest, int iParm)
2a2c4 7b 0a 20 20 70 44 65 73 74 2d 3e 65 44 65 73 74  {.  pDest->eDest
2a2c5 20 3d 20 28 75 38 29 65 44 65 73 74 3b 0a 20 20   = (u8)eDest;.  
2a2c6 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 3d 20 69  pDest->iParm = i
2a2c7 50 61 72 6d 3b 0a 20 20 70 44 65 73 74 2d 3e 61  Parm;.  pDest->a
2a2c8 66 66 69 6e 69 74 79 20 3d 20 30 3b 0a 20 20 70  ffinity = 0;.  p
2a2c9 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 30 3b 0a  Dest->iMem = 0;.
2a2ca 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20    pDest->nMem = 
2a2cb 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  0;.}.../*.** All
2a2cc 6f 63 61 74 65 20 61 20 6e 65 77 20 53 65 6c 65  ocate a new Sele
2a2cd 63 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  ct structure and
2a2ce 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
2a2cf 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 73 74 72  r to that.** str
2a2d0 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ucture..*/.SQLIT
2a2d1 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74  E_PRIVATE Select
2a2d2 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e   *sqlite3SelectN
2a2d3 65 77 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ew(.  Parse *pPa
2a2d4 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
2a2d5 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2a2d6 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
2a2d7 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 77 68 69  List,     /* whi
2a2d8 63 68 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 69 6e  ch columns to in
2a2d9 63 6c 75 64 65 20 69 6e 20 74 68 65 20 72 65 73  clude in the res
2a2da 75 6c 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ult */.  SrcList
2a2db 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 2f   *pSrc,        /
2a2dc 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
2a2dd 65 20 2d 2d 20 77 68 69 63 68 20 74 61 62 6c 65  e -- which table
2a2de 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45  s to scan */.  E
2a2df 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20  xpr *pWhere,    
2a2e0 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52       /* the WHER
2a2e1 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  E clause */.  Ex
2a2e2 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79  prList *pGroupBy
2a2e3 2c 20 20 20 2f 2a 20 74 68 65 20 47 52 4f 55 50  ,   /* the GROUP
2a2e4 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
2a2e5 45 78 70 72 20 2a 70 48 61 76 69 6e 67 2c 20 20  Expr *pHaving,  
2a2e6 20 20 20 20 20 20 2f 2a 20 74 68 65 20 48 41 56        /* the HAV
2a2e7 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ING clause */.  
2a2e8 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
2a2e9 42 79 2c 20 20 20 2f 2a 20 74 68 65 20 4f 52 44  By,   /* the ORD
2a2ea 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
2a2eb 20 20 69 6e 74 20 69 73 44 69 73 74 69 6e 63 74    int isDistinct
2a2ec 2c 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20  ,       /* true 
2a2ed 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54 20  if the DISTINCT 
2a2ee 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65  keyword is prese
2a2ef 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c  nt */.  Expr *pL
2a2f0 69 6d 69 74 2c 20 20 20 20 20 20 20 20 20 2f 2a  imit,         /*
2a2f1 20 4c 49 4d 49 54 20 76 61 6c 75 65 2e 20 20 4e   LIMIT value.  N
2a2f2 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73  ULL means not us
2a2f3 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f  ed */.  Expr *pO
2a2f4 66 66 73 65 74 20 20 20 20 20 20 20 20 20 2f 2a  ffset         /*
2a2f5 20 4f 46 46 53 45 54 20 76 61 6c 75 65 2e 20 20   OFFSET value.  
2a2f6 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 20 6f 66  NULL means no of
2a2f7 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c  fset */.){.  Sel
2a2f8 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 53 65 6c  ect *pNew;.  Sel
2a2f9 65 63 74 20 73 74 61 6e 64 69 6e 3b 0a 20 20 73  ect standin;.  s
2a2fa 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2a2fb 72 73 65 2d 3e 64 62 3b 0a 20 20 70 4e 65 77 20  rse->db;.  pNew 
2a2fc 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2a2fd 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2a2fe 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 61 73 73  (*pNew) );.  ass
2a2ff 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
2a300 61 69 6c 65 64 20 7c 7c 20 21 70 4f 66 66 73 65  ailed || !pOffse
2a301 74 20 7c 7c 20 70 4c 69 6d 69 74 20 29 3b 20 2f  t || pLimit ); /
2a302 2a 20 4f 46 46 53 45 54 20 69 6d 70 6c 69 65 73  * OFFSET implies
2a303 20 4c 49 4d 49 54 20 2a 2f 0a 20 20 69 66 28 20   LIMIT */.  if( 
2a304 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70  pNew==0 ){.    p
2a305 4e 65 77 20 3d 20 26 73 74 61 6e 64 69 6e 3b 0a  New = &standin;.
2a306 20 20 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c      memset(pNew,
2a307 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77   0, sizeof(*pNew
2a308 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45  ));.  }.  if( pE
2a309 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70  List==0 ){.    p
2a30a 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  EList = sqlite3E
2a30b 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
2a30c 61 72 73 65 2c 20 30 2c 20 73 71 6c 69 74 65 33  arse, 0, sqlite3
2a30d 45 78 70 72 28 64 62 2c 54 4b 5f 41 4c 4c 2c 30  Expr(db,TK_ALL,0
2a30e 29 29 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e  ));.  }.  pNew->
2a30f 70 45 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b  pEList = pEList;
2a310 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20  .  pNew->pSrc = 
2a311 70 53 72 63 3b 0a 20 20 70 4e 65 77 2d 3e 70 57  pSrc;.  pNew->pW
2a312 68 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20  here = pWhere;. 
2a313 20 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20   pNew->pGroupBy 
2a314 3d 20 70 47 72 6f 75 70 42 79 3b 0a 20 20 70 4e  = pGroupBy;.  pN
2a315 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70 48  ew->pHaving = pH
2a316 61 76 69 6e 67 3b 0a 20 20 70 4e 65 77 2d 3e 70  aving;.  pNew->p
2a317 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72  OrderBy = pOrder
2a318 42 79 3b 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46  By;.  pNew->selF
2a319 6c 61 67 73 20 3d 20 69 73 44 69 73 74 69 6e 63  lags = isDistinc
2a31a 74 20 3f 20 53 46 5f 44 69 73 74 69 6e 63 74 20  t ? SF_Distinct 
2a31b 3a 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20  : 0;.  pNew->op 
2a31c 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20 70  = TK_SELECT;.  p
2a31d 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c  New->pLimit = pL
2a31e 69 6d 69 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f  imit;.  pNew->pO
2a31f 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b  ffset = pOffset;
2a320 0a 20 20 61 73 73 65 72 74 28 20 70 4f 66 66 73  .  assert( pOffs
2a321 65 74 3d 3d 30 20 7c 7c 20 70 4c 69 6d 69 74 21  et==0 || pLimit!
2a322 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 61 64  =0 );.  pNew->ad
2a323 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20  drOpenEphm[0] = 
2a324 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72  -1;.  pNew->addr
2a325 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31  OpenEphm[1] = -1
2a326 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ;.  pNew->addrOp
2a327 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a  enEphm[2] = -1;.
2a328 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2a329 46 61 69 6c 65 64 20 29 20 7b 0a 20 20 20 20 63  Failed ) {.    c
2a32a 6c 65 61 72 53 65 6c 65 63 74 28 64 62 2c 20 70  learSelect(db, p
2a32b 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e  New);.    if( pN
2a32c 65 77 21 3d 26 73 74 61 6e 64 69 6e 20 29 20 73  ew!=&standin ) s
2a32d 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2a32e 20 70 4e 65 77 29 3b 0a 20 20 20 20 70 4e 65 77   pNew);.    pNew
2a32f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
2a330 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
2a331 2a 20 44 65 6c 65 74 65 20 74 68 65 20 67 69 76  * Delete the giv
2a332 65 6e 20 53 65 6c 65 63 74 20 73 74 72 75 63 74  en Select struct
2a333 75 72 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69  ure and all of i
2a334 74 73 20 73 75 62 73 74 72 75 63 74 75 72 65 73  ts substructures
2a335 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2a336 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2a337 53 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c  SelectDelete(sql
2a338 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74  ite3 *db, Select
2a339 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
2a33a 0a 20 20 20 20 63 6c 65 61 72 53 65 6c 65 63 74  .    clearSelect
2a33b 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c  (db, p);.    sql
2a33c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2a33d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2a33e 47 69 76 65 6e 20 31 20 74 6f 20 33 20 69 64 65  Given 1 to 3 ide
2a33f 6e 74 69 66 69 65 72 73 20 70 72 65 63 65 65 64  ntifiers preceed
2a340 69 6e 67 20 74 68 65 20 4a 4f 49 4e 20 6b 65 79  ing the JOIN key
2a341 77 6f 72 64 2c 20 64 65 74 65 72 6d 69 6e 65 20  word, determine 
2a342 74 68 65 0a 2a 2a 20 74 79 70 65 20 6f 66 20 6a  the.** type of j
2a343 6f 69 6e 2e 20 20 52 65 74 75 72 6e 20 61 6e 20  oin.  Return an 
2a344 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
2a345 20 74 68 61 74 20 65 78 70 72 65 73 73 65 73 20   that expresses 
2a346 74 68 61 74 20 74 79 70 65 0a 2a 2a 20 69 6e 20  that type.** in 
2a347 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 6c  terms of the fol
2a348 6c 6f 77 69 6e 67 20 62 69 74 20 76 61 6c 75 65  lowing bit value
2a349 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4a 54 5f  s:.**.**     JT_
2a34a 49 4e 4e 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f  INNER.**     JT_
2a34b 43 52 4f 53 53 0a 2a 2a 20 20 20 20 20 4a 54 5f  CROSS.**     JT_
2a34c 4f 55 54 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f  OUTER.**     JT_
2a34d 4e 41 54 55 52 41 4c 0a 2a 2a 20 20 20 20 20 4a  NATURAL.**     J
2a34e 54 5f 4c 45 46 54 0a 2a 2a 20 20 20 20 20 4a 54  T_LEFT.**     JT
2a34f 5f 52 49 47 48 54 0a 2a 2a 0a 2a 2a 20 41 20 66  _RIGHT.**.** A f
2a350 75 6c 6c 20 6f 75 74 65 72 20 6a 6f 69 6e 20 69  ull outer join i
2a351 73 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 69 6f  s the combinatio
2a352 6e 20 6f 66 20 4a 54 5f 4c 45 46 54 20 61 6e 64  n of JT_LEFT and
2a353 20 4a 54 5f 52 49 47 48 54 2e 0a 2a 2a 0a 2a 2a   JT_RIGHT..**.**
2a354 20 49 66 20 61 6e 20 69 6c 6c 65 67 61 6c 20 6f   If an illegal o
2a355 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f  r unsupported jo
2a356 69 6e 20 74 79 70 65 20 69 73 20 73 65 65 6e 2c  in type is seen,
2a357 20 74 68 65 6e 20 73 74 69 6c 6c 20 72 65 74 75   then still retu
2a358 72 6e 0a 2a 2a 20 61 20 6a 6f 69 6e 20 74 79 70  rn.** a join typ
2a359 65 2c 20 62 75 74 20 70 75 74 20 61 6e 20 65 72  e, but put an er
2a35a 72 6f 72 20 69 6e 20 74 68 65 20 70 50 61 72 73  ror in the pPars
2a35b 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  e structure..*/.
2a35c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2a35d 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79  nt sqlite3JoinTy
2a35e 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  pe(Parse *pParse
2a35f 2c 20 54 6f 6b 65 6e 20 2a 70 41 2c 20 54 6f 6b  , Token *pA, Tok
2a360 65 6e 20 2a 70 42 2c 20 54 6f 6b 65 6e 20 2a 70  en *pB, Token *p
2a361 43 29 7b 0a 20 20 69 6e 74 20 6a 6f 69 6e 74 79  C){.  int jointy
2a362 70 65 20 3d 20 30 3b 0a 20 20 54 6f 6b 65 6e 20  pe = 0;.  Token 
2a363 2a 61 70 41 6c 6c 5b 33 5d 3b 0a 20 20 54 6f 6b  *apAll[3];.  Tok
2a364 65 6e 20 2a 70 3b 0a 20 20 20 20 20 20 20 20 20  en *p;.         
2a365 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a366 20 20 20 20 2f 2a 20 20 20 30 31 32 33 34 35 36      /*   0123456
2a367 37 38 39 20 31 32 33 34 35 36 37 38 39 20 31 32  789 123456789 12
2a368 33 34 35 36 37 38 39 20 31 32 33 20 2a 2f 0a 20  3456789 123 */. 
2a369 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
2a36a 61 72 20 7a 4b 65 79 54 65 78 74 5b 5d 20 3d 20  ar zKeyText[] = 
2a36b 22 6e 61 74 75 72 61 6c 65 66 74 6f 75 74 65 72  "naturaleftouter
2a36c 69 67 68 74 66 75 6c 6c 69 6e 6e 65 72 63 72 6f  ightfullinnercro
2a36d 73 73 22 3b 0a 20 20 73 74 61 74 69 63 20 63 6f  ss";.  static co
2a36e 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20 20  nst struct {.   
2a36f 20 75 38 20 69 3b 20 20 20 20 20 20 20 20 2f 2a   u8 i;        /*
2a370 20 42 65 67 69 6e 6e 69 6e 67 20 6f 66 20 6b 65   Beginning of ke
2a371 79 77 6f 72 64 20 74 65 78 74 20 69 6e 20 7a 4b  yword text in zK
2a372 65 79 54 65 78 74 5b 5d 20 2a 2f 0a 20 20 20 20  eyText[] */.    
2a373 75 38 20 6e 43 68 61 72 3b 20 20 20 20 2f 2a 20  u8 nChar;    /* 
2a374 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6b 65  Length of the ke
2a375 79 77 6f 72 64 20 69 6e 20 63 68 61 72 61 63 74  yword in charact
2a376 65 72 73 20 2a 2f 0a 20 20 20 20 75 38 20 63 6f  ers */.    u8 co
2a377 64 65 3b 20 20 20 20 20 2f 2a 20 4a 6f 69 6e 20  de;     /* Join 
2a378 74 79 70 65 20 6d 61 73 6b 20 2a 2f 0a 20 20 7d  type mask */.  }
2a379 20 61 4b 65 79 77 6f 72 64 5b 5d 20 3d 20 7b 0a   aKeyword[] = {.
2a37a 20 20 20 20 2f 2a 20 6e 61 74 75 72 61 6c 20 2a      /* natural *
2a37b 2f 20 7b 20 30 2c 20 20 37 2c 20 4a 54 5f 4e 41  / { 0,  7, JT_NA
2a37c 54 55 52 41 4c 20 20 20 20 20 20 20 20 20 20 20  TURAL           
2a37d 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 6c       },.    /* l
2a37e 65 66 74 20 20 20 20 2a 2f 20 7b 20 36 2c 20 20  eft    */ { 6,  
2a37f 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 4f 55  4, JT_LEFT|JT_OU
2a380 54 45 52 20 20 20 20 20 20 20 20 20 20 7d 2c 0a  TER          },.
2a381 20 20 20 20 2f 2a 20 6f 75 74 65 72 20 20 20 2a      /* outer   *
2a382 2f 20 7b 20 31 30 2c 20 35 2c 20 4a 54 5f 4f 55  / { 10, 5, JT_OU
2a383 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  TER             
2a384 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 72       },.    /* r
2a385 69 67 68 74 20 20 20 2a 2f 20 7b 20 31 34 2c 20  ight   */ { 14, 
2a386 35 2c 20 4a 54 5f 52 49 47 48 54 7c 4a 54 5f 4f  5, JT_RIGHT|JT_O
2a387 55 54 45 52 20 20 20 20 20 20 20 20 20 7d 2c 0a  UTER         },.
2a388 20 20 20 20 2f 2a 20 66 75 6c 6c 20 20 20 20 2a      /* full    *
2a389 2f 20 7b 20 31 39 2c 20 34 2c 20 4a 54 5f 4c 45  / { 19, 4, JT_LE
2a38a 46 54 7c 4a 54 5f 52 49 47 48 54 7c 4a 54 5f 4f  FT|JT_RIGHT|JT_O
2a38b 55 54 45 52 20 7d 2c 0a 20 20 20 20 2f 2a 20 69  UTER },.    /* i
2a38c 6e 6e 65 72 20 20 20 2a 2f 20 7b 20 32 33 2c 20  nner   */ { 23, 
2a38d 35 2c 20 4a 54 5f 49 4e 4e 45 52 20 20 20 20 20  5, JT_INNER     
2a38e 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 0a               },.
2a38f 20 20 20 20 2f 2a 20 63 72 6f 73 73 20 20 20 2a      /* cross   *
2a390 2f 20 7b 20 32 38 2c 20 35 2c 20 4a 54 5f 49 4e  / { 28, 5, JT_IN
2a391 4e 45 52 7c 4a 54 5f 43 52 4f 53 53 20 20 20 20  NER|JT_CROSS    
2a392 20 20 20 20 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69       },.  };.  i
2a393 6e 74 20 69 2c 20 6a 3b 0a 20 20 61 70 41 6c 6c  nt i, j;.  apAll
2a394 5b 30 5d 20 3d 20 70 41 3b 0a 20 20 61 70 41 6c  [0] = pA;.  apAl
2a395 6c 5b 31 5d 20 3d 20 70 42 3b 0a 20 20 61 70 41  l[1] = pB;.  apA
2a396 6c 6c 5b 32 5d 20 3d 20 70 43 3b 0a 20 20 66 6f  ll[2] = pC;.  fo
2a397 72 28 69 3d 30 3b 20 69 3c 33 20 26 26 20 61 70  r(i=0; i<3 && ap
2a398 41 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20  All[i]; i++){.  
2a399 20 20 70 20 3d 20 61 70 41 6c 6c 5b 69 5d 3b 0a    p = apAll[i];.
2a39a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41      for(j=0; j<A
2a39b 72 72 61 79 53 69 7a 65 28 61 4b 65 79 77 6f 72  rraySize(aKeywor
2a39c 64 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  d); j++){.      
2a39d 69 66 28 20 70 2d 3e 6e 3d 3d 61 4b 65 79 77 6f  if( p->n==aKeywo
2a39e 72 64 5b 6a 5d 2e 6e 43 68 61 72 20 0a 20 20 20  rd[j].nChar .   
2a39f 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
2a3a0 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a  3StrNICmp((char*
2a3a1 29 70 2d 3e 7a 2c 20 26 7a 4b 65 79 54 65 78 74  )p->z, &zKeyText
2a3a2 5b 61 4b 65 79 77 6f 72 64 5b 6a 5d 2e 69 5d 2c  [aKeyword[j].i],
2a3a3 20 70 2d 3e 6e 29 3d 3d 30 20 29 7b 0a 20 20 20   p->n)==0 ){.   
2a3a4 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 7c 3d       jointype |=
2a3a5 20 61 4b 65 79 77 6f 72 64 5b 6a 5d 2e 63 6f 64   aKeyword[j].cod
2a3a6 65 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  e;.        break
2a3a7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2a3a8 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 3d      testcase( j=
2a3a9 3d 30 20 7c 7c 20 6a 3d 3d 31 20 7c 7c 20 6a 3d  =0 || j==1 || j=
2a3aa 3d 32 20 7c 7c 20 6a 3d 3d 33 20 7c 7c 20 6a 3d  =2 || j==3 || j=
2a3ab 3d 34 20 7c 7c 20 6a 3d 3d 35 20 7c 7c 20 6a 3d  =4 || j==5 || j=
2a3ac 3d 36 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3e  =6 );.    if( j>
2a3ad 3d 41 72 72 61 79 53 69 7a 65 28 61 4b 65 79 77  =ArraySize(aKeyw
2a3ae 6f 72 64 29 20 29 7b 0a 20 20 20 20 20 20 6a 6f  ord) ){.      jo
2a3af 69 6e 74 79 70 65 20 7c 3d 20 4a 54 5f 45 52 52  intype |= JT_ERR
2a3b0 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OR;.      break;
2a3b1 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
2a3b2 0a 20 20 20 20 20 28 6a 6f 69 6e 74 79 70 65 20  .     (jointype 
2a3b3 26 20 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f  & (JT_INNER|JT_O
2a3b4 55 54 45 52 29 29 3d 3d 28 4a 54 5f 49 4e 4e 45  UTER))==(JT_INNE
2a3b5 52 7c 4a 54 5f 4f 55 54 45 52 29 20 7c 7c 0a 20  R|JT_OUTER) ||. 
2a3b6 20 20 20 20 28 6a 6f 69 6e 74 79 70 65 20 26 20      (jointype & 
2a3b7 4a 54 5f 45 52 52 4f 52 29 21 3d 30 0a 20 20 29  JT_ERROR)!=0.  )
2a3b8 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
2a3b9 20 2a 7a 53 70 20 3d 20 22 20 22 3b 0a 20 20 20   *zSp = " ";.   
2a3ba 20 61 73 73 65 72 74 28 20 70 42 21 3d 30 20 29   assert( pB!=0 )
2a3bb 3b 0a 20 20 20 20 69 66 28 20 70 43 3d 3d 30 20  ;.    if( pC==0 
2a3bc 29 7b 20 7a 53 70 2b 2b 3b 20 7d 0a 20 20 20 20  ){ zSp++; }.    
2a3bd 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2a3be 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e  pParse, "unknown
2a3bf 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20   or unsupported 
2a3c0 6a 6f 69 6e 20 74 79 70 65 3a 20 22 0a 20 20 20  join type: ".   
2a3c1 20 20 20 20 22 25 54 20 25 54 25 73 25 54 22 2c      "%T %T%s%T",
2a3c2 20 70 41 2c 20 70 42 2c 20 7a 53 70 2c 20 70 43   pA, pB, zSp, pC
2a3c3 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65 20  );.    jointype 
2a3c4 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 65  = JT_INNER;.  }e
2a3c5 6c 73 65 20 69 66 28 20 28 6a 6f 69 6e 74 79 70  lse if( (jointyp
2a3c6 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30  e & JT_OUTER)!=0
2a3c7 20 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 6a   .         && (j
2a3c8 6f 69 6e 74 79 70 65 20 26 20 28 4a 54 5f 4c 45  ointype & (JT_LE
2a3c9 46 54 7c 4a 54 5f 52 49 47 48 54 29 29 21 3d 4a  FT|JT_RIGHT))!=J
2a3ca 54 5f 4c 45 46 54 20 29 7b 0a 20 20 20 20 73 71  T_LEFT ){.    sq
2a3cb 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2a3cc 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 52 49  arse, .      "RI
2a3cd 47 48 54 20 61 6e 64 20 46 55 4c 4c 20 4f 55 54  GHT and FULL OUT
2a3ce 45 52 20 4a 4f 49 4e 73 20 61 72 65 20 6e 6f 74  ER JOINs are not
2a3cf 20 63 75 72 72 65 6e 74 6c 79 20 73 75 70 70 6f   currently suppo
2a3d0 72 74 65 64 22 29 3b 0a 20 20 20 20 6a 6f 69 6e  rted");.    join
2a3d1 74 79 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b  type = JT_INNER;
2a3d2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6a 6f  .  }.  return jo
2a3d3 69 6e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  intype;.}../*.**
2a3d4 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65   Return the inde
2a3d5 78 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e  x of a column in
2a3d6 20 61 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72   a table.  Retur
2a3d7 6e 20 2d 31 20 69 66 20 74 68 65 20 63 6f 6c 75  n -1 if the colu
2a3d8 6d 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e  mn.** is not con
2a3d9 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 74 61  tained in the ta
2a3da 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
2a3db 6e 74 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 54  nt columnIndex(T
2a3dc 61 62 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e 73  able *pTab, cons
2a3dd 74 20 63 68 61 72 20 2a 7a 43 6f 6c 29 7b 0a 20  t char *zCol){. 
2a3de 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
2a3df 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; i<pTab->nCol;
2a3e0 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73   i++){.    if( s
2a3e1 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54  qlite3StrICmp(pT
2a3e2 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d  ab->aCol[i].zNam
2a3e3 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 20 72 65  e, zCol)==0 ) re
2a3e4 74 75 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65  turn i;.  }.  re
2a3e5 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn -1;.}../*.*
2a3e6 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2a3e7 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 74  is used to add t
2a3e8 65 72 6d 73 20 69 6d 70 6c 69 65 64 20 62 79 20  erms implied by 
2a3e9 4a 4f 49 4e 20 73 79 6e 74 61 78 20 74 6f 20 74  JOIN syntax to t
2a3ea 68 65 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61 75  he.** WHERE clau
2a3eb 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66  se expression of
2a3ec 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2a3ed 65 6e 74 2e 20 54 68 65 20 6e 65 77 20 74 65 72  ent. The new ter
2a3ee 6d 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 41  m, which.** is A
2a3ef 4e 44 65 64 20 77 69 74 68 20 74 68 65 20 65 78  NDed with the ex
2a3f0 69 73 74 69 6e 67 20 57 48 45 52 45 20 63 6c 61  isting WHERE cla
2a3f1 75 73 65 2c 20 69 73 20 6f 66 20 74 68 65 20 66  use, is of the f
2a3f2 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 74  orm:.**.**    (t
2a3f3 61 62 31 2e 63 6f 6c 31 20 3d 20 74 61 62 32 2e  ab1.col1 = tab2.
2a3f4 63 6f 6c 32 29 0a 2a 2a 0a 2a 2a 20 77 68 65 72  col2).**.** wher
2a3f5 65 20 74 61 62 31 20 69 73 20 74 68 65 20 69 53  e tab1 is the iS
2a3f6 72 63 27 74 68 20 74 61 62 6c 65 20 69 6e 20 53  rc'th table in S
2a3f7 72 63 4c 69 73 74 20 70 53 72 63 20 61 6e 64 20  rcList pSrc and 
2a3f8 74 61 62 32 20 69 73 20 74 68 65 20 0a 2a 2a 20  tab2 is the .** 
2a3f9 28 69 53 72 63 2b 31 29 27 74 68 2e 20 43 6f 6c  (iSrc+1)'th. Col
2a3fa 75 6d 6e 20 63 6f 6c 31 20 69 73 20 63 6f 6c 75  umn col1 is colu
2a3fb 6d 6e 20 69 43 6f 6c 4c 65 66 74 20 6f 66 20 74  mn iColLeft of t
2a3fc 61 62 31 2c 20 61 6e 64 20 63 6f 6c 32 20 69 73  ab1, and col2 is
2a3fd 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 52  .** column iColR
2a3fe 69 67 68 74 20 6f 66 20 74 61 62 32 2e 0a 2a 2f  ight of tab2..*/
2a3ff 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64  .static void add
2a400 57 68 65 72 65 54 65 72 6d 28 0a 20 20 50 61 72  WhereTerm(.  Par
2a401 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2a402 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a403 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2a404 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  */.  SrcList *pS
2a405 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rc,             
2a406 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
2a407 74 61 62 6c 65 73 20 69 6e 20 46 52 4f 4d 20 63  tables in FROM c
2a408 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lause */.  int i
2a409 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
2a40a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2a40b 64 65 78 20 6f 66 20 66 69 72 73 74 20 74 61 62  dex of first tab
2a40c 6c 65 20 74 6f 20 6a 6f 69 6e 20 69 6e 20 70 53  le to join in pS
2a40d 72 63 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  rc */.  int iCol
2a40e 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 20 20  Left,           
2a40f 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2a410 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 66 69   of column in fi
2a411 72 73 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  rst table */.  i
2a412 6e 74 20 69 43 6f 6c 52 69 67 68 74 2c 20 20 20  nt iColRight,   
2a413 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a414 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d  * Index of colum
2a415 6e 20 69 6e 20 73 65 63 6f 6e 64 20 74 61 62 6c  n in second tabl
2a416 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 75 74  e */.  int isOut
2a417 65 72 4a 6f 69 6e 2c 20 20 20 20 20 20 20 20 20  erJoin,         
2a418 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
2a419 66 20 74 68 69 73 20 69 73 20 61 6e 20 4f 55 54  f this is an OUT
2a41a 45 52 20 6a 6f 69 6e 20 2a 2f 0a 20 20 45 78 70  ER join */.  Exp
2a41b 72 20 2a 2a 70 70 57 68 65 72 65 20 20 20 20 20  r **ppWhere     
2a41c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a41d 49 4e 2f 4f 55 54 3a 20 54 68 65 20 57 48 45 52  IN/OUT: The WHER
2a41e 45 20 63 6c 61 75 73 65 20 74 6f 20 61 64 64 20  E clause to add 
2a41f 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  to */.){.  sqlit
2a420 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2a421 3e 64 62 3b 0a 20 20 45 78 70 72 20 2a 70 45 31  >db;.  Expr *pE1
2a422 3b 0a 20 20 45 78 70 72 20 2a 70 45 32 3b 0a 20  ;.  Expr *pE2;. 
2a423 20 45 78 70 72 20 2a 70 45 71 3b 0a 0a 20 20 61   Expr *pEq;..  a
2a424 73 73 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 72  ssert( pSrc->nSr
2a425 63 3e 28 69 53 72 63 2b 31 29 20 29 3b 0a 20 20  c>(iSrc+1) );.  
2a426 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 61 5b  assert( pSrc->a[
2a427 69 53 72 63 5d 2e 70 54 61 62 20 29 3b 0a 20 20  iSrc].pTab );.  
2a428 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 61 5b  assert( pSrc->a[
2a429 69 53 72 63 2b 31 5d 2e 70 54 61 62 20 29 3b 0a  iSrc+1].pTab );.
2a42a 0a 20 20 70 45 31 20 3d 20 73 71 6c 69 74 65 33  .  pE1 = sqlite3
2a42b 43 72 65 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72  CreateColumnExpr
2a42c 28 64 62 2c 20 70 53 72 63 2c 20 69 53 72 63 2c  (db, pSrc, iSrc,
2a42d 20 69 43 6f 6c 4c 65 66 74 29 3b 0a 20 20 70 45   iColLeft);.  pE
2a42e 32 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74  2 = sqlite3Creat
2a42f 65 43 6f 6c 75 6d 6e 45 78 70 72 28 64 62 2c 20  eColumnExpr(db, 
2a430 70 53 72 63 2c 20 69 53 72 63 2b 31 2c 20 69 43  pSrc, iSrc+1, iC
2a431 6f 6c 52 69 67 68 74 29 3b 0a 0a 20 20 70 45 71  olRight);..  pEq
2a432 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2a433 70 50 61 72 73 65 2c 20 54 4b 5f 45 51 2c 20 70  pParse, TK_EQ, p
2a434 45 31 2c 20 70 45 32 2c 20 30 29 3b 0a 20 20 69  E1, pE2, 0);.  i
2a435 66 28 20 70 45 71 20 26 26 20 69 73 4f 75 74 65  f( pEq && isOute
2a436 72 4a 6f 69 6e 20 29 7b 0a 20 20 20 20 45 78 70  rJoin ){.    Exp
2a437 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 71  rSetProperty(pEq
2a438 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a  , EP_FromJoin);.
2a439 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2a43a 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
2a43b 70 45 71 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  pEq, EP_TokenOnl
2a43c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b  y|EP_Reduced) );
2a43d 0a 20 20 20 20 45 78 70 72 53 65 74 49 72 72 65  .    ExprSetIrre
2a43e 64 75 63 69 62 6c 65 28 70 45 71 29 3b 0a 20 20  ducible(pEq);.  
2a43f 20 20 70 45 71 2d 3e 69 52 69 67 68 74 4a 6f 69    pEq->iRightJoi
2a440 6e 54 61 62 6c 65 20 3d 20 28 69 31 36 29 70 45  nTable = (i16)pE
2a441 32 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 7d 0a 20  2->iTable;.  }. 
2a442 20 2a 70 70 57 68 65 72 65 20 3d 20 73 71 6c 69   *ppWhere = sqli
2a443 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 2a  te3ExprAnd(db, *
2a444 70 70 57 68 65 72 65 2c 20 70 45 71 29 3b 0a 7d  ppWhere, pEq);.}
2a445 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
2a446 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70  EP_FromJoin prop
2a447 65 72 74 79 20 6f 6e 20 61 6c 6c 20 74 65 72 6d  erty on all term
2a448 73 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 65  s of the given e
2a449 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 41 6e  xpression..** An
2a44a 64 20 73 65 74 20 74 68 65 20 45 78 70 72 2e 69  d set the Expr.i
2a44b 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74  RightJoinTable t
2a44c 6f 20 69 54 61 62 6c 65 20 66 6f 72 20 65 76 65  o iTable for eve
2a44d 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a  ry term in the.*
2a44e 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  * expression..**
2a44f 0a 2a 2a 20 54 68 65 20 45 50 5f 46 72 6f 6d 4a  .** The EP_FromJ
2a450 6f 69 6e 20 70 72 6f 70 65 72 74 79 20 69 73 20  oin property is 
2a451 75 73 65 64 20 6f 6e 20 74 65 72 6d 73 20 6f 66  used on terms of
2a452 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
2a453 6f 20 74 65 6c 6c 0a 2a 2a 20 74 68 65 20 4c 45  o tell.** the LE
2a454 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 70 72  FT OUTER JOIN pr
2a455 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 20 74  ocessing logic t
2a456 68 61 74 20 74 68 69 73 20 74 65 72 6d 20 69 73  hat this term is
2a457 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20   part of the.** 
2a458 6a 6f 69 6e 20 72 65 73 74 72 69 63 74 69 6f 6e  join restriction
2a459 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
2a45a 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  e ON or USING cl
2a45b 61 75 73 65 20 61 6e 64 20 6e 6f 74 20 61 20 70  ause and not a p
2a45c 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 6d 6f  art.** of the mo
2a45d 72 65 20 67 65 6e 65 72 61 6c 20 57 48 45 52 45  re general WHERE
2a45e 20 63 6c 61 75 73 65 2e 20 20 54 68 65 73 65 20   clause.  These 
2a45f 74 65 72 6d 73 20 61 72 65 20 6d 6f 76 65 64 20  terms are moved 
2a460 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 57  over to the.** W
2a461 48 45 52 45 20 63 6c 61 75 73 65 20 64 75 72 69  HERE clause duri
2a462 6e 67 20 6a 6f 69 6e 20 70 72 6f 63 65 73 73 69  ng join processi
2a463 6e 67 20 62 75 74 20 77 65 20 6e 65 65 64 20 74  ng but we need t
2a464 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74 20  o remember that 
2a465 74 68 65 79 0a 2a 2a 20 6f 72 69 67 69 6e 61 74  they.** originat
2a466 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20  ed in the ON or 
2a467 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a 2a  USING clause..**
2a468 0a 2a 2a 20 54 68 65 20 45 78 70 72 2e 69 52 69  .** The Expr.iRi
2a469 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74 65 6c  ghtJoinTable tel
2a46a 6c 73 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  ls the WHERE cla
2a46b 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 74  use processing t
2a46c 68 61 74 20 74 68 65 0a 2a 2a 20 65 78 70 72 65  hat the.** expre
2a46d 73 73 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e  ssion depends on
2a46e 20 74 61 62 6c 65 20 69 52 69 67 68 74 4a 6f 69   table iRightJoi
2a46f 6e 54 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74  nTable even if t
2a470 68 61 74 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  hat table is not
2a471 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 6d  .** explicitly m
2a472 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68 65 20  entioned in the 
2a473 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 61  expression.  Tha
2a474 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  t information is
2a475 20 6e 65 65 64 65 64 0a 2a 2a 20 66 6f 72 20 63   needed.** for c
2a476 61 73 65 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ases like this:.
2a477 2a 2a 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54 20  **.**    SELECT 
2a478 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a  * FROM t1 LEFT J
2a479 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74  OIN t2 ON t1.a=t
2a47a 32 2e 62 20 41 4e 44 20 74 31 2e 78 3d 35 0a 2a  2.b AND t1.x=5.*
2a47b 2a 0a 2a 2a 20 54 68 65 20 77 68 65 72 65 20 63  *.** The where c
2a47c 6c 61 75 73 65 20 6e 65 65 64 73 20 74 6f 20 64  lause needs to d
2a47d 65 66 65 72 20 74 68 65 20 68 61 6e 64 6c 69 6e  efer the handlin
2a47e 67 20 6f 66 20 74 68 65 20 74 31 2e 78 3d 35 0a  g of the t1.x=5.
2a47f 2a 2a 20 74 65 72 6d 20 75 6e 74 69 6c 20 61 66  ** term until af
2a480 74 65 72 20 74 68 65 20 74 32 20 6c 6f 6f 70 20  ter the t2 loop 
2a481 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 49 6e  of the join.  In
2a482 20 74 68 61 74 20 77 61 79 2c 20 61 0a 2a 2a 20   that way, a.** 
2a483 4e 55 4c 4c 20 74 32 20 72 6f 77 20 77 69 6c 6c  NULL t2 row will
2a484 20 62 65 20 69 6e 73 65 72 74 65 64 20 77 68 65   be inserted whe
2a485 6e 65 76 65 72 20 74 31 2e 78 21 3d 35 2e 20 20  never t1.x!=5.  
2a486 49 66 20 77 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20  If we do not.** 
2a487 64 65 66 65 72 20 74 68 65 20 68 61 6e 64 6c 69  defer the handli
2a488 6e 67 20 6f 66 20 74 31 2e 78 3d 35 2c 20 69 74  ng of t1.x=5, it
2a489 20 77 69 6c 6c 20 62 65 20 70 72 6f 63 65 73 73   will be process
2a48a 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a  ed immediately.*
2a48b 2a 20 61 66 74 65 72 20 74 68 65 20 74 31 20 6c  * after the t1 l
2a48c 6f 6f 70 20 61 6e 64 20 72 6f 77 73 20 77 69 74  oop and rows wit
2a48d 68 20 74 31 2e 78 21 3d 35 20 77 69 6c 6c 20 6e  h t1.x!=5 will n
2a48e 65 76 65 72 20 61 70 70 65 61 72 20 69 6e 0a 2a  ever appear in.*
2a48f 2a 20 74 68 65 20 6f 75 74 70 75 74 2c 20 77 68  * the output, wh
2a490 69 63 68 20 69 73 20 69 6e 63 6f 72 72 65 63 74  ich is incorrect
2a491 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2a492 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 45 78 70   setJoinExpr(Exp
2a493 72 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c 65  r *p, int iTable
2a494 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b  ){.  while( p ){
2a495 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70  .    ExprSetProp
2a496 65 72 74 79 28 70 2c 20 45 50 5f 46 72 6f 6d 4a  erty(p, EP_FromJ
2a497 6f 69 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 74  oin);.    assert
2a498 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f  ( !ExprHasAnyPro
2a499 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65  perty(p, EP_Toke
2a49a 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64  nOnly|EP_Reduced
2a49b 29 20 29 3b 0a 20 20 20 20 45 78 70 72 53 65 74  ) );.    ExprSet
2a49c 49 72 72 65 64 75 63 69 62 6c 65 28 70 29 3b 0a  Irreducible(p);.
2a49d 20 20 20 20 70 2d 3e 69 52 69 67 68 74 4a 6f 69      p->iRightJoi
2a49e 6e 54 61 62 6c 65 20 3d 20 28 69 31 36 29 69 54  nTable = (i16)iT
2a49f 61 62 6c 65 3b 0a 20 20 20 20 73 65 74 4a 6f 69  able;.    setJoi
2a4a0 6e 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20  nExpr(p->pLeft, 
2a4a1 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 70 20 3d  iTable);.    p =
2a4a2 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 7d 20   p->pRight;.  } 
2a4a3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2a4a4 6f 75 74 69 6e 65 20 70 72 6f 63 65 73 73 65 73  outine processes
2a4a5 20 74 68 65 20 6a 6f 69 6e 20 69 6e 66 6f 72 6d   the join inform
2a4a6 61 74 69 6f 6e 20 66 6f 72 20 61 20 53 45 4c 45  ation for a SELE
2a4a7 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CT statement..**
2a4a8 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c   ON and USING cl
2a4a9 61 75 73 65 73 20 61 72 65 20 63 6f 6e 76 65 72  auses are conver
2a4aa 74 65 64 20 69 6e 74 6f 20 65 78 74 72 61 20 74  ted into extra t
2a4ab 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52  erms of the WHER
2a4ac 45 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 4e 41 54  E clause..** NAT
2a4ad 55 52 41 4c 20 6a 6f 69 6e 73 20 61 6c 73 6f 20  URAL joins also 
2a4ae 63 72 65 61 74 65 20 65 78 74 72 61 20 57 48 45  create extra WHE
2a4af 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 2e  RE clause terms.
2a4b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73  .**.** The terms
2a4b1 20 6f 66 20 61 20 46 52 4f 4d 20 63 6c 61 75 73   of a FROM claus
2a4b2 65 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20  e are contained 
2a4b3 69 6e 20 74 68 65 20 53 65 6c 65 63 74 2e 70 53  in the Select.pS
2a4b4 72 63 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  rc structure..**
2a4b5 20 54 68 65 20 6c 65 66 74 20 6d 6f 73 74 20 74   The left most t
2a4b6 61 62 6c 65 20 69 73 20 74 68 65 20 66 69 72 73  able is the firs
2a4b7 74 20 65 6e 74 72 79 20 69 6e 20 53 65 6c 65 63  t entry in Selec
2a4b8 74 2e 70 53 72 63 2e 20 20 54 68 65 20 72 69 67  t.pSrc.  The rig
2a4b9 68 74 2d 6d 6f 73 74 0a 2a 2a 20 74 61 62 6c 65  ht-most.** table
2a4ba 20 69 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74   is the last ent
2a4bb 72 79 2e 20 20 54 68 65 20 6a 6f 69 6e 20 6f 70  ry.  The join op
2a4bc 65 72 61 74 6f 72 20 69 73 20 68 65 6c 64 20 69  erator is held i
2a4bd 6e 20 74 68 65 20 65 6e 74 72 79 20 74 6f 0a 2a  n the entry to.*
2a4be 2a 20 74 68 65 20 6c 65 66 74 2e 20 20 54 68 75  * the left.  Thu
2a4bf 73 20 65 6e 74 72 79 20 30 20 63 6f 6e 74 61 69  s entry 0 contai
2a4c0 6e 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72  ns the join oper
2a4c1 61 74 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 69  ator for the joi
2a4c2 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 65 6e 74  n between.** ent
2a4c3 72 69 65 73 20 30 20 61 6e 64 20 31 2e 20 20 41  ries 0 and 1.  A
2a4c4 6e 79 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63  ny ON or USING c
2a4c5 6c 61 75 73 65 73 20 61 73 73 6f 63 69 61 74 65  lauses associate
2a4c6 64 20 77 69 74 68 20 74 68 65 20 6a 6f 69 6e 20  d with the join 
2a4c7 61 72 65 0a 2a 2a 20 61 6c 73 6f 20 61 74 74 61  are.** also atta
2a4c8 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65 66 74  ched to the left
2a4c9 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68   entry..**.** Th
2a4ca 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
2a4cb 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
2a4cc 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65   errors encounte
2a4cd 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  red..*/.static i
2a4ce 6e 74 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73  nt sqliteProcess
2a4cf 4a 6f 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72  Join(Parse *pPar
2a4d0 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a  se, Select *p){.
2a4d1 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
2a4d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a4d3 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 20    /* All tables 
2a4d4 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2a4d5 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a  se */.  int i, j
2a4d6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2a4d7 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2a4d8 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 73 74  counters */.  st
2a4d9 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2a4da 6d 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a  m *pLeft;     /*
2a4db 20 4c 65 66 74 20 74 61 62 6c 65 20 62 65 69 6e   Left table bein
2a4dc 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a 20 20 73 74  g joined */.  st
2a4dd 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2a4de 6d 20 2a 70 52 69 67 68 74 3b 20 20 20 20 2f 2a  m *pRight;    /*
2a4df 20 52 69 67 68 74 20 74 61 62 6c 65 20 62 65 69   Right table bei
2a4e0 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a 0a 20 20  ng joined */..  
2a4e1 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a  pSrc = p->pSrc;.
2a4e2 20 20 70 4c 65 66 74 20 3d 20 26 70 53 72 63 2d    pLeft = &pSrc-
2a4e3 3e 61 5b 30 5d 3b 0a 20 20 70 52 69 67 68 74 20  >a[0];.  pRight 
2a4e4 3d 20 26 70 4c 65 66 74 5b 31 5d 3b 0a 20 20 66  = &pLeft[1];.  f
2a4e5 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e  or(i=0; i<pSrc->
2a4e6 6e 53 72 63 2d 31 3b 20 69 2b 2b 2c 20 70 52 69  nSrc-1; i++, pRi
2a4e7 67 68 74 2b 2b 2c 20 70 4c 65 66 74 2b 2b 29 7b  ght++, pLeft++){
2a4e8 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4c 65 66  .    Table *pLef
2a4e9 74 54 61 62 20 3d 20 70 4c 65 66 74 2d 3e 70 54  tTab = pLeft->pT
2a4ea 61 62 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  ab;.    Table *p
2a4eb 52 69 67 68 74 54 61 62 20 3d 20 70 52 69 67 68  RightTab = pRigh
2a4ec 74 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 6e 74  t->pTab;.    int
2a4ed 20 69 73 4f 75 74 65 72 3b 0a 0a 20 20 20 20 69   isOuter;..    i
2a4ee 66 28 20 4e 45 56 45 52 28 70 4c 65 66 74 54 61  f( NEVER(pLeftTa
2a4ef 62 3d 3d 30 20 7c 7c 20 70 52 69 67 68 74 54 61  b==0 || pRightTa
2a4f0 62 3d 3d 30 29 20 29 20 63 6f 6e 74 69 6e 75 65  b==0) ) continue
2a4f1 3b 0a 20 20 20 20 69 73 4f 75 74 65 72 20 3d 20  ;.    isOuter = 
2a4f2 28 70 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70  (pRight->jointyp
2a4f3 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30  e & JT_OUTER)!=0
2a4f4 3b 0a 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74  ;..    /* When t
2a4f5 68 65 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f  he NATURAL keywo
2a4f6 72 64 20 69 73 20 70 72 65 73 65 6e 74 2c 20 61  rd is present, a
2a4f7 64 64 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  dd WHERE clause 
2a4f8 74 65 72 6d 73 20 66 6f 72 0a 20 20 20 20 2a 2a  terms for.    **
2a4f9 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 74 68   every column th
2a4fa 61 74 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65  at the two table
2a4fb 73 20 68 61 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e  s have in common
2a4fc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2a4fd 20 70 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70   pRight->jointyp
2a4fe 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29  e & JT_NATURAL )
2a4ff 7b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67  {.      if( pRig
2a500 68 74 2d 3e 70 4f 6e 20 7c 7c 20 70 52 69 67 68  ht->pOn || pRigh
2a501 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20  t->pUsing ){.   
2a502 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2a503 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 20  rMsg(pParse, "a 
2a504 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 20 6d 61 79  NATURAL join may
2a505 20 6e 6f 74 20 68 61 76 65 20 22 0a 20 20 20 20   not have ".    
2a506 20 20 20 20 20 20 20 22 61 6e 20 4f 4e 20 6f 72         "an ON or
2a507 20 55 53 49 4e 47 20 63 6c 61 75 73 65 22 2c 20   USING clause", 
2a508 30 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  0);.        retu
2a509 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
2a50a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
2a50b 4c 65 66 74 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a  LeftTab->nCol; j
2a50c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  ++){.        cha
2a50d 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 4c 65 66 74  r *zName = pLeft
2a50e 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61  Tab->aCol[j].zNa
2a50f 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  me;.        int 
2a510 69 52 69 67 68 74 43 6f 6c 20 3d 20 63 6f 6c 75  iRightCol = colu
2a511 6d 6e 49 6e 64 65 78 28 70 52 69 67 68 74 54 61  mnIndex(pRightTa
2a512 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b, zName);.     
2a513 20 20 20 69 66 28 20 69 52 69 67 68 74 43 6f 6c     if( iRightCol
2a514 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
2a515 20 61 64 64 57 68 65 72 65 54 65 72 6d 28 70 50   addWhereTerm(pP
2a516 61 72 73 65 2c 20 70 53 72 63 2c 20 69 2c 20 6a  arse, pSrc, i, j
2a517 2c 20 69 52 69 67 68 74 43 6f 6c 2c 20 69 73 4f  , iRightCol, isO
2a518 75 74 65 72 2c 20 26 70 2d 3e 70 57 68 65 72 65  uter, &p->pWhere
2a519 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
2a51a 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
2a51b 2f 2a 20 44 69 73 61 6c 6c 6f 77 20 62 6f 74 68  /* Disallow both
2a51c 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c   ON and USING cl
2a51d 61 75 73 65 73 20 69 6e 20 74 68 65 20 73 61 6d  auses in the sam
2a51e 65 20 6a 6f 69 6e 0a 20 20 20 20 2a 2f 0a 20 20  e join.    */.  
2a51f 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f    if( pRight->pO
2a520 6e 20 26 26 20 70 52 69 67 68 74 2d 3e 70 55 73  n && pRight->pUs
2a521 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ing ){.      sql
2a522 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2a523 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 68 61 76  rse, "cannot hav
2a524 65 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20 55 53  e both ON and US
2a525 49 4e 47 20 22 0a 20 20 20 20 20 20 20 20 22 63  ING ".        "c
2a526 6c 61 75 73 65 73 20 69 6e 20 74 68 65 20 73 61  lauses in the sa
2a527 6d 65 20 6a 6f 69 6e 22 29 3b 0a 20 20 20 20 20  me join");.     
2a528 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
2a529 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65  ..    /* Add the
2a52a 20 4f 4e 20 63 6c 61 75 73 65 20 74 6f 20 74 68   ON clause to th
2a52b 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45  e end of the WHE
2a52c 52 45 20 63 6c 61 75 73 65 2c 20 63 6f 6e 6e 65  RE clause, conne
2a52d 63 74 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 61  cted by.    ** a
2a52e 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a  n AND operator..
2a52f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
2a530 52 69 67 68 74 2d 3e 70 4f 6e 20 29 7b 0a 20 20  Right->pOn ){.  
2a531 20 20 20 20 69 66 28 20 69 73 4f 75 74 65 72 20      if( isOuter 
2a532 29 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 70 52  ) setJoinExpr(pR
2a533 69 67 68 74 2d 3e 70 4f 6e 2c 20 70 52 69 67 68  ight->pOn, pRigh
2a534 74 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20  t->iCursor);.   
2a535 20 20 20 70 2d 3e 70 57 68 65 72 65 20 3d 20 73     p->pWhere = s
2a536 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 70 50  qlite3ExprAnd(pP
2a537 61 72 73 65 2d 3e 64 62 2c 20 70 2d 3e 70 57 68  arse->db, p->pWh
2a538 65 72 65 2c 20 70 52 69 67 68 74 2d 3e 70 4f 6e  ere, pRight->pOn
2a539 29 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74 2d  );.      pRight-
2a53a 3e 70 4f 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  >pOn = 0;.    }.
2a53b 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 65  .    /* Create e
2a53c 78 74 72 61 20 74 65 72 6d 73 20 6f 6e 20 74 68  xtra terms on th
2a53d 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 66  e WHERE clause f
2a53e 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6e  or each column n
2a53f 61 6d 65 64 0a 20 20 20 20 2a 2a 20 69 6e 20 74  amed.    ** in t
2a540 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e  he USING clause.
2a541 20 20 45 78 61 6d 70 6c 65 3a 20 49 66 20 74 68    Example: If th
2a542 65 20 74 77 6f 20 74 61 62 6c 65 73 20 74 6f 20  e two tables to 
2a543 62 65 20 6a 6f 69 6e 65 64 20 61 72 65 20 0a 20  be joined are . 
2a544 20 20 20 2a 2a 20 41 20 61 6e 64 20 42 20 61 6e     ** A and B an
2a545 64 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75  d the USING clau
2a546 73 65 20 6e 61 6d 65 73 20 58 2c 20 59 2c 20 61  se names X, Y, a
2a547 6e 64 20 5a 2c 20 74 68 65 6e 20 61 64 64 20 74  nd Z, then add t
2a548 68 69 73 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68  his.    ** to th
2a549 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 3a 20  e WHERE clause: 
2a54a 20 20 20 41 2e 58 3d 42 2e 58 20 41 4e 44 20 41     A.X=B.X AND A
2a54b 2e 59 3d 42 2e 59 20 41 4e 44 20 41 2e 5a 3d 42  .Y=B.Y AND A.Z=B
2a54c 2e 5a 0a 20 20 20 20 2a 2a 20 52 65 70 6f 72 74  .Z.    ** Report
2a54d 20 61 6e 20 65 72 72 6f 72 20 69 66 20 61 6e 79   an error if any
2a54e 20 63 6f 6c 75 6d 6e 20 6d 65 6e 74 69 6f 6e 65   column mentione
2a54f 64 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63  d in the USING c
2a550 6c 61 75 73 65 20 69 73 0a 20 20 20 20 2a 2a 20  lause is.    ** 
2a551 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  not contained in
2a552 20 62 6f 74 68 20 74 61 62 6c 65 73 20 74 6f 20   both tables to 
2a553 62 65 20 6a 6f 69 6e 65 64 2e 0a 20 20 20 20 2a  be joined..    *
2a554 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74  /.    if( pRight
2a555 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20  ->pUsing ){.    
2a556 20 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 20    IdList *pList 
2a557 3d 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67  = pRight->pUsing
2a558 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ;.      for(j=0;
2a559 20 6a 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 6a   j<pList->nId; j
2a55a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  ++){.        cha
2a55b 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 4c 69 73 74  r *zName = pList
2a55c 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  ->a[j].zName;.  
2a55d 20 20 20 20 20 20 69 6e 74 20 69 4c 65 66 74 43        int iLeftC
2a55e 6f 6c 20 3d 20 63 6f 6c 75 6d 6e 49 6e 64 65 78  ol = columnIndex
2a55f 28 70 4c 65 66 74 54 61 62 2c 20 7a 4e 61 6d 65  (pLeftTab, zName
2a560 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  );.        int i
2a561 52 69 67 68 74 43 6f 6c 20 3d 20 63 6f 6c 75 6d  RightCol = colum
2a562 6e 49 6e 64 65 78 28 70 52 69 67 68 74 54 61 62  nIndex(pRightTab
2a563 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
2a564 20 20 69 66 28 20 69 4c 65 66 74 43 6f 6c 3c 30    if( iLeftCol<0
2a565 20 7c 7c 20 69 52 69 67 68 74 43 6f 6c 3c 30 20   || iRightCol<0 
2a566 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
2a567 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2a568 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 6a 6f 69  rse, "cannot joi
2a569 6e 20 75 73 69 6e 67 20 63 6f 6c 75 6d 6e 20 25  n using column %
2a56a 73 20 2d 20 63 6f 6c 75 6d 6e 20 22 0a 20 20 20  s - column ".   
2a56b 20 20 20 20 20 20 20 20 20 22 6e 6f 74 20 70 72           "not pr
2a56c 65 73 65 6e 74 20 69 6e 20 62 6f 74 68 20 74 61  esent in both ta
2a56d 62 6c 65 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  bles", zName);. 
2a56e 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
2a56f 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
2a570 20 20 20 20 20 61 64 64 57 68 65 72 65 54 65 72       addWhereTer
2a571 6d 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c 20  m(pParse, pSrc, 
2a572 69 2c 20 69 4c 65 66 74 43 6f 6c 2c 20 69 52 69  i, iLeftCol, iRi
2a573 67 68 74 43 6f 6c 2c 20 69 73 4f 75 74 65 72 2c  ghtCol, isOuter,
2a574 20 26 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20   &p->pWhere);.  
2a575 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2a576 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
2a577 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 63 6f 64 65  *.** Insert code
2a578 20 69 6e 74 6f 20 22 76 22 20 74 68 61 74 20 77   into "v" that w
2a579 69 6c 6c 20 70 75 73 68 20 74 68 65 20 72 65 63  ill push the rec
2a57a 6f 72 64 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f  ord on the top o
2a57b 66 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 69  f the.** stack i
2a57c 6e 74 6f 20 74 68 65 20 73 6f 72 74 65 72 2e 0a  nto the sorter..
2a57d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
2a57e 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 0a 20  ushOntoSorter(. 
2a57f 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2a580 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
2a581 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  r context */.  E
2a582 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
2a583 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44  y,    /* The ORD
2a584 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
2a585 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
2a586 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  t,       /* The 
2a587 77 68 6f 6c 65 20 53 45 4c 45 43 54 20 73 74 61  whole SELECT sta
2a588 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  tement */.  int 
2a589 72 65 67 44 61 74 61 20 20 20 20 20 20 20 20 20  regData         
2a58a 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
2a58b 6f 6c 64 69 6e 67 20 64 61 74 61 20 74 6f 20 62  olding data to b
2a58c 65 20 73 6f 72 74 65 64 20 2a 2f 0a 29 7b 0a 20  e sorted */.){. 
2a58d 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
2a58e 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20  e->pVdbe;.  int 
2a58f 6e 45 78 70 72 20 3d 20 70 4f 72 64 65 72 42 79  nExpr = pOrderBy
2a590 2d 3e 6e 45 78 70 72 3b 0a 20 20 69 6e 74 20 72  ->nExpr;.  int r
2a591 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33  egBase = sqlite3
2a592 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
2a593 72 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b 0a 20  rse, nExpr+2);. 
2a594 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 20 3d   int regRecord =
2a595 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2a596 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  eg(pParse);.  sq
2a597 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
2a598 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 73  ear(pParse);.  s
2a599 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78  qlite3ExprCodeEx
2a59a 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
2a59b 4f 72 64 65 72 42 79 2c 20 72 65 67 42 61 73 65  OrderBy, regBase
2a59c 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , 0);.  sqlite3V
2a59d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a59e 53 65 71 75 65 6e 63 65 2c 20 70 4f 72 64 65 72  Sequence, pOrder
2a59f 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20 72 65  By->iECursor, re
2a5a0 67 42 61 73 65 2b 6e 45 78 70 72 29 3b 0a 20 20  gBase+nExpr);.  
2a5a1 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d  sqlite3ExprCodeM
2a5a2 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65 67 44  ove(pParse, regD
2a5a3 61 74 61 2c 20 72 65 67 42 61 73 65 2b 6e 45 78  ata, regBase+nEx
2a5a4 70 72 2b 31 2c 20 31 29 3b 0a 20 20 73 71 6c 69  pr+1, 1);.  sqli
2a5a5 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2a5a6 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
2a5a7 72 65 67 42 61 73 65 2c 20 6e 45 78 70 72 20 2b  regBase, nExpr +
2a5a8 20 32 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a   2, regRecord);.
2a5a9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a5aa 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  Op2(v, OP_IdxIns
2a5ab 65 72 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69  ert, pOrderBy->i
2a5ac 45 43 75 72 73 6f 72 2c 20 72 65 67 52 65 63 6f  ECursor, regReco
2a5ad 72 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  rd);.  sqlite3Re
2a5ae 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2a5af 72 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b  rse, regRecord);
2a5b0 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2a5b1 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
2a5b2 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 45 78 70  e, regBase, nExp
2a5b3 72 2b 32 29 3b 0a 20 20 69 66 28 20 70 53 65 6c  r+2);.  if( pSel
2a5b4 65 63 74 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20  ect->iLimit ){. 
2a5b5 20 20 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64     int addr1, ad
2a5b6 64 72 32 3b 0a 20 20 20 20 69 6e 74 20 69 4c 69  dr2;.    int iLi
2a5b7 6d 69 74 3b 0a 20 20 20 20 69 66 28 20 70 53 65  mit;.    if( pSe
2a5b8 6c 65 63 74 2d 3e 69 4f 66 66 73 65 74 20 29 7b  lect->iOffset ){
2a5b9 0a 20 20 20 20 20 20 69 4c 69 6d 69 74 20 3d 20  .      iLimit = 
2a5ba 70 53 65 6c 65 63 74 2d 3e 69 4f 66 66 73 65 74  pSelect->iOffset
2a5bb 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  +1;.    }else{. 
2a5bc 20 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 53       iLimit = pS
2a5bd 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 3b 0a 20  elect->iLimit;. 
2a5be 20 20 20 7d 0a 20 20 20 20 61 64 64 72 31 20 3d     }.    addr1 =
2a5bf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a5c0 70 31 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c  p1(v, OP_IfZero,
2a5c1 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 73 71   iLimit);.    sq
2a5c2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a5c3 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4c  v, OP_AddImm, iL
2a5c4 69 6d 69 74 2c 20 2d 31 29 3b 0a 20 20 20 20 61  imit, -1);.    a
2a5c5 64 64 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr2 = sqlite3Vd
2a5c6 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47  beAddOp0(v, OP_G
2a5c7 6f 74 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  oto);.    sqlite
2a5c8 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2a5c9 20 61 64 64 72 31 29 3b 0a 20 20 20 20 73 71 6c   addr1);.    sql
2a5ca 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2a5cb 2c 20 4f 50 5f 4c 61 73 74 2c 20 70 4f 72 64 65  , OP_Last, pOrde
2a5cc 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 29 3b 0a  rBy->iECursor);.
2a5cd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a5ce 64 64 4f 70 31 28 76 2c 20 4f 50 5f 44 65 6c 65  ddOp1(v, OP_Dele
2a5cf 74 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45  te, pOrderBy->iE
2a5d0 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 71 6c  Cursor);.    sql
2a5d1 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2a5d2 28 76 2c 20 61 64 64 72 32 29 3b 0a 20 20 20 20  (v, addr2);.    
2a5d3 70 53 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 20  pSelect->iLimit 
2a5d4 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 0;.  }.}../*.*
2a5d5 2a 20 41 64 64 20 63 6f 64 65 20 74 6f 20 69 6d  * Add code to im
2a5d6 70 6c 65 6d 65 6e 74 20 74 68 65 20 4f 46 46 53  plement the OFFS
2a5d7 45 54 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ET.*/.static voi
2a5d8 64 20 63 6f 64 65 4f 66 66 73 65 74 28 0a 20 20  d codeOffset(.  
2a5d9 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20  Vdbe *v,        
2a5da 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
2a5db 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 4d 20  de into this VM 
2a5dc 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
2a5dd 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45         /* The SE
2a5de 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62  LECT statement b
2a5df 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20  eing coded */.  
2a5e0 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 20 20 20  int iContinue   
2a5e1 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
2a5e2 6f 20 73 6b 69 70 20 74 68 65 20 63 75 72 72 65  o skip the curre
2a5e3 6e 74 20 72 65 63 6f 72 64 20 2a 2f 0a 29 7b 0a  nt record */.){.
2a5e4 20 20 69 66 28 20 70 2d 3e 69 4f 66 66 73 65 74    if( p->iOffset
2a5e5 20 26 26 20 69 43 6f 6e 74 69 6e 75 65 21 3d 30   && iContinue!=0
2a5e6 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 64 64 72   ){.    int addr
2a5e7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2a5e8 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
2a5e9 64 49 6d 6d 2c 20 70 2d 3e 69 4f 66 66 73 65 74  dImm, p->iOffset
2a5ea 2c 20 2d 31 29 3b 0a 20 20 20 20 61 64 64 72 20  , -1);.    addr 
2a5eb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2a5ec 4f 70 31 28 76 2c 20 4f 50 5f 49 66 4e 65 67 2c  Op1(v, OP_IfNeg,
2a5ed 20 70 2d 3e 69 4f 66 66 73 65 74 29 3b 0a 20 20   p->iOffset);.  
2a5ee 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a5ef 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
2a5f0 30 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20  0, iContinue);. 
2a5f1 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2a5f2 76 2c 20 22 73 6b 69 70 20 4f 46 46 53 45 54 20  v, "skip OFFSET 
2a5f3 72 65 63 6f 72 64 73 22 29 29 3b 0a 20 20 20 20  records"));.    
2a5f4 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2a5f5 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20  ere(v, addr);.  
2a5f6 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63  }.}../*.** Add c
2a5f7 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63 68  ode that will ch
2a5f8 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  eck to make sure
2a5f9 20 74 68 65 20 4e 20 72 65 67 69 73 74 65 72 73   the N registers
2a5fa 20 73 74 61 72 74 69 6e 67 20 61 74 20 69 4d 65   starting at iMe
2a5fb 6d 0a 2a 2a 20 66 6f 72 6d 20 61 20 64 69 73 74  m.** form a dist
2a5fc 69 6e 63 74 20 65 6e 74 72 79 2e 20 20 69 54 61  inct entry.  iTa
2a5fd 62 20 69 73 20 61 20 73 6f 72 74 69 6e 67 20 69  b is a sorting i
2a5fe 6e 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20  ndex that holds 
2a5ff 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 73 65  previously.** se
2a600 65 6e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20  en combinations 
2a601 6f 66 20 74 68 65 20 4e 20 76 61 6c 75 65 73 2e  of the N values.
2a602 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73    A new entry is
2a603 20 6d 61 64 65 20 69 6e 20 69 54 61 62 0a 2a 2a   made in iTab.**
2a604 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   if the current 
2a605 4e 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 77  N values are new
2a606 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20 74  ..**.** A jump t
2a607 6f 20 61 64 64 72 52 65 70 65 61 74 20 69 73 20  o addrRepeat is 
2a608 6d 61 64 65 20 61 6e 64 20 74 68 65 20 4e 2b 31  made and the N+1
2a609 20 76 61 6c 75 65 73 20 61 72 65 20 70 6f 70 70   values are popp
2a60a 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 73  ed from the.** s
2a60b 74 61 63 6b 20 69 66 20 74 68 65 20 74 6f 70 20  tack if the top 
2a60c 4e 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 6e  N elements are n
2a60d 6f 74 20 64 69 73 74 69 6e 63 74 2e 0a 2a 2f 0a  ot distinct..*/.
2a60e 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65  static void code
2a60f 44 69 73 74 69 6e 63 74 28 0a 20 20 50 61 72 73  Distinct(.  Pars
2a610 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
2a611 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
2a612 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
2a613 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
2a614 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Tab,          /*
2a615 20 41 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78   A sorting index
2a616 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 66 6f   used to test fo
2a617 72 20 64 69 73 74 69 6e 63 74 6e 65 73 73 20 2a  r distinctness *
2a618 2f 0a 20 20 69 6e 74 20 61 64 64 72 52 65 70 65  /.  int addrRepe
2a619 61 74 2c 20 20 20 20 2f 2a 20 4a 75 6d 70 20 74  at,    /* Jump t
2a61a 6f 20 68 65 72 65 20 69 66 20 6e 6f 74 20 64 69  o here if not di
2a61b 73 74 69 6e 63 74 20 2a 2f 0a 20 20 69 6e 74 20  stinct */.  int 
2a61c 4e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  N,             /
2a61d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
2a61e 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4d  ents */.  int iM
2a61f 65 6d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  em           /* 
2a620 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 2a 2f  First element */
2a621 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  .){.  Vdbe *v;. 
2a622 20 69 6e 74 20 72 31 3b 0a 0a 20 20 76 20 3d 20   int r1;..  v = 
2a623 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2a624 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
2a625 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2a626 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2a627 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp3(v, OP_MakeR
2a628 65 63 6f 72 64 2c 20 69 4d 65 6d 2c 20 4e 2c 20  ecord, iMem, N, 
2a629 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  r1);.  sqlite3Vd
2a62a 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46  beAddOp3(v, OP_F
2a62b 6f 75 6e 64 2c 20 69 54 61 62 2c 20 61 64 64 72  ound, iTab, addr
2a62c 52 65 70 65 61 74 2c 20 72 31 29 3b 0a 20 20 73  Repeat, r1);.  s
2a62d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2a62e 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
2a62f 2c 20 69 54 61 62 2c 20 72 31 29 3b 0a 20 20 73  , iTab, r1);.  s
2a630 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2a631 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
2a632 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
2a633 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ate an error mes
2a634 73 61 67 65 20 77 68 65 6e 20 61 20 53 45 4c 45  sage when a SELE
2a635 43 54 20 69 73 20 75 73 65 64 20 77 69 74 68 69  CT is used withi
2a636 6e 20 61 20 73 75 62 65 78 70 72 65 73 73 69 6f  n a subexpressio
2a637 6e 0a 2a 2a 20 28 65 78 61 6d 70 6c 65 3a 20 20  n.** (example:  
2a638 22 61 20 49 4e 20 28 53 45 4c 45 43 54 20 2a 20  "a IN (SELECT * 
2a639 46 52 4f 4d 20 74 61 62 6c 65 29 22 29 20 62 75  FROM table)") bu
2a63a 74 20 69 74 20 68 61 73 20 6d 6f 72 65 20 74 68  t it has more th
2a63b 61 6e 20 31 20 72 65 73 75 6c 74 0a 2a 2a 20 63  an 1 result.** c
2a63c 6f 6c 75 6d 6e 2e 20 20 57 65 20 64 6f 20 74 68  olumn.  We do th
2a63d 69 73 20 69 6e 20 61 20 73 75 62 72 6f 75 74 69  is in a subrouti
2a63e 6e 65 20 62 65 63 61 75 73 65 20 74 68 65 20 65  ne because the e
2a63f 72 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 6d  rror occurs in m
2a640 75 6c 74 69 70 6c 65 0a 2a 2a 20 70 6c 61 63 65  ultiple.** place
2a641 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
2a642 20 63 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f   checkForMultiCo
2a643 6c 75 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72 28  lumnSelectError(
2a644 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2a645 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65  ,       /* Parse
2a646 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 53   context. */.  S
2a647 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74  electDest *pDest
2a648 2c 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69  ,   /* Destinati
2a649 6f 6e 20 6f 66 20 53 45 4c 45 43 54 20 72 65 73  on of SELECT res
2a64a 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  ults */.  int nE
2a64b 78 70 72 20 20 20 20 20 20 20 20 20 20 20 20 2f  xpr            /
2a64c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75  * Number of resu
2a64d 6c 74 20 63 6f 6c 75 6d 6e 73 20 72 65 74 75 72  lt columns retur
2a64e 6e 65 64 20 62 79 20 53 45 4c 45 43 54 20 2a 2f  ned by SELECT */
2a64f 0a 29 7b 0a 20 20 69 6e 74 20 65 44 65 73 74 20  .){.  int eDest 
2a650 3d 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3b 0a  = pDest->eDest;.
2a651 20 20 69 66 28 20 6e 45 78 70 72 3e 31 20 26 26    if( nExpr>1 &&
2a652 20 28 65 44 65 73 74 3d 3d 53 52 54 5f 4d 65 6d   (eDest==SRT_Mem
2a653 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f 53   || eDest==SRT_S
2a654 65 74 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  et) ){.    sqlit
2a655 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2a656 65 2c 20 22 6f 6e 6c 79 20 61 20 73 69 6e 67 6c  e, "only a singl
2a657 65 20 72 65 73 75 6c 74 20 61 6c 6c 6f 77 65 64  e result allowed
2a658 20 66 6f 72 20 22 0a 20 20 20 20 20 20 20 22 61   for ".       "a
2a659 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 73 20   SELECT that is 
2a65a 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65  part of an expre
2a65b 73 73 69 6f 6e 22 29 3b 0a 20 20 20 20 72 65 74  ssion");.    ret
2a65c 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  urn 1;.  }else{.
2a65d 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2a65e 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
2a65f 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65  routine generate
2a660 73 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 74  s the code for t
2a661 68 65 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65  he inside of the
2a662 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a 20 6f   inner loop.** o
2a663 66 20 61 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a  f a SELECT..**.*
2a664 2a 20 49 66 20 73 72 63 54 61 62 20 61 6e 64 20  * If srcTab and 
2a665 6e 43 6f 6c 75 6d 6e 20 61 72 65 20 62 6f 74 68  nColumn are both
2a666 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
2a667 70 45 4c 69 73 74 20 65 78 70 72 65 73 73 69 6f  pEList expressio
2a668 6e 73 0a 2a 2a 20 61 72 65 20 65 76 61 6c 75 61  ns.** are evalua
2a669 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ted in order to 
2a66a 67 65 74 20 74 68 65 20 64 61 74 61 20 66 6f 72  get the data for
2a66b 20 74 68 69 73 20 72 6f 77 2e 20 20 49 66 20 6e   this row.  If n
2a66c 43 6f 6c 75 6d 6e 3e 30 0a 2a 2a 20 74 68 65 6e  Column>0.** then
2a66d 20 64 61 74 61 20 69 73 20 70 75 6c 6c 65 64 20   data is pulled 
2a66e 66 72 6f 6d 20 73 72 63 54 61 62 20 61 6e 64 20  from srcTab and 
2a66f 70 45 4c 69 73 74 20 69 73 20 75 73 65 64 20 6f  pEList is used o
2a670 6e 6c 79 20 74 6f 20 67 65 74 20 74 68 65 0a 2a  nly to get the.*
2a671 2a 20 64 61 74 61 74 79 70 65 73 20 66 6f 72 20  * datatypes for 
2a672 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  each column..*/.
2a673 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 6c 65  static void sele
2a674 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 0a 20 20 50  ctInnerLoop(.  P
2a675 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2a676 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
2a677 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
2a678 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20    Select *p,    
2a679 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2a67a 20 63 6f 6d 70 6c 65 74 65 20 73 65 6c 65 63 74   complete select
2a67b 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67   statement being
2a67c 20 63 6f 64 65 64 20 2a 2f 0a 20 20 45 78 70 72   coded */.  Expr
2a67d 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20  List *pEList,   
2a67e 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 76      /* List of v
2a67f 61 6c 75 65 73 20 62 65 69 6e 67 20 65 78 74 72  alues being extr
2a680 61 63 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 73  acted */.  int s
2a681 72 63 54 61 62 2c 20 20 20 20 20 20 20 20 20 20  rcTab,          
2a682 20 20 20 2f 2a 20 50 75 6c 6c 20 64 61 74 61 20     /* Pull data 
2a683 66 72 6f 6d 20 74 68 69 73 20 74 61 62 6c 65 20  from this table 
2a684 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e  */.  int nColumn
2a685 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2a686 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
2a687 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20  s in the source 
2a688 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c  table */.  ExprL
2a689 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
2a68a 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 4c     /* If not NUL
2a68b 4c 2c 20 73 6f 72 74 20 72 65 73 75 6c 74 73 20  L, sort results 
2a68c 75 73 69 6e 67 20 74 68 69 73 20 6b 65 79 20 2a  using this key *
2a68d 2f 0a 20 20 69 6e 74 20 64 69 73 74 69 6e 63 74  /.  int distinct
2a68e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ,           /* I
2a68f 66 20 3e 3d 30 2c 20 6d 61 6b 65 20 73 75 72 65  f >=0, make sure
2a690 20 72 65 73 75 6c 74 73 20 61 72 65 20 64 69 73   results are dis
2a691 74 69 6e 63 74 20 2a 2f 0a 20 20 53 65 6c 65 63  tinct */.  Selec
2a692 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20  tDest *pDest,   
2a693 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73     /* How to dis
2a694 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73 75  pose of the resu
2a695 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  lts */.  int iCo
2a696 6e 74 69 6e 75 65 2c 20 20 20 20 20 20 20 20 20  ntinue,         
2a697 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
2a698 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e   continue with n
2a699 65 78 74 20 72 6f 77 20 2a 2f 0a 20 20 69 6e 74  ext row */.  int
2a69a 20 69 42 72 65 61 6b 20 20 20 20 20 20 20 20 20   iBreak         
2a69b 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
2a69c 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f  e to break out o
2a69d 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70  f the inner loop
2a69e 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
2a69f 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2a6a0 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
2a6a1 20 68 61 73 44 69 73 74 69 6e 63 74 3b 20 20 20   hasDistinct;   
2a6a2 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2a6a3 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79  the DISTINCT key
2a6a4 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20  word is present 
2a6a5 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 73 75  */.  int regResu
2a6a6 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  lt;             
2a6a7 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 6d 65 6d   /* Start of mem
2a6a8 6f 72 79 20 68 6f 6c 64 69 6e 67 20 72 65 73 75  ory holding resu
2a6a9 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20  lt set */.  int 
2a6aa 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65  eDest = pDest->e
2a6ab 44 65 73 74 3b 20 20 20 2f 2a 20 48 6f 77 20 74  Dest;   /* How t
2a6ac 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 72 65 73  o dispose of res
2a6ad 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69 50  ults */.  int iP
2a6ae 61 72 6d 20 3d 20 70 44 65 73 74 2d 3e 69 50 61  arm = pDest->iPa
2a6af 72 6d 3b 20 20 20 2f 2a 20 46 69 72 73 74 20 61  rm;   /* First a
2a6b0 72 67 75 6d 65 6e 74 20 74 6f 20 64 69 73 70 6f  rgument to dispo
2a6b1 73 61 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  sal method */.  
2a6b2 69 6e 74 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 20  int nResultCol; 
2a6b3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2a6b4 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20  umber of result 
2a6b5 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 0a 20 20 61 73  columns */..  as
2a6b6 73 65 72 74 28 20 76 20 29 3b 0a 20 20 69 66 28  sert( v );.  if(
2a6b7 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72   NEVER(v==0) ) r
2a6b8 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28  eturn;.  assert(
2a6b9 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20   pEList!=0 );.  
2a6ba 68 61 73 44 69 73 74 69 6e 63 74 20 3d 20 64 69  hasDistinct = di
2a6bb 73 74 69 6e 63 74 3e 3d 30 3b 0a 20 20 69 66 28  stinct>=0;.  if(
2a6bc 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 26 26 20   pOrderBy==0 && 
2a6bd 21 68 61 73 44 69 73 74 69 6e 63 74 20 29 7b 0a  !hasDistinct ){.
2a6be 20 20 20 20 63 6f 64 65 4f 66 66 73 65 74 28 76      codeOffset(v
2a6bf 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b  , p, iContinue);
2a6c0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 75 6c 6c 20  .  }..  /* Pull 
2a6c1 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63 6f  the requested co
2a6c2 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  lumns..  */.  if
2a6c3 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20  ( nColumn>0 ){. 
2a6c4 20 20 20 6e 52 65 73 75 6c 74 43 6f 6c 20 3d 20     nResultCol = 
2a6c5 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 7d 65 6c 73 65  nColumn;.  }else
2a6c6 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 43 6f 6c  {.    nResultCol
2a6c7 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72   = pEList->nExpr
2a6c8 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73  ;.  }.  if( pDes
2a6c9 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29 7b 0a 20 20  t->iMem==0 ){.  
2a6ca 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20    pDest->iMem = 
2a6cb 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a  pParse->nMem+1;.
2a6cc 20 20 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 20      pDest->nMem 
2a6cd 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a 20 20  = nResultCol;.  
2a6ce 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
2a6cf 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a 20 20  = nResultCol;.  
2a6d0 7d 65 6c 73 65 7b 20 0a 20 20 20 20 61 73 73 65  }else{ .    asse
2a6d1 72 74 28 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 3d  rt( pDest->nMem=
2a6d2 3d 6e 52 65 73 75 6c 74 43 6f 6c 20 29 3b 0a 20  =nResultCol );. 
2a6d3 20 7d 0a 20 20 72 65 67 52 65 73 75 6c 74 20 3d   }.  regResult =
2a6d4 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3b 0a 20 20   pDest->iMem;.  
2a6d5 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b  if( nColumn>0 ){
2a6d6 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2a6d7 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
2a6d8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2a6d9 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
2a6da 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 69 2c 20  umn, srcTab, i, 
2a6db 72 65 67 52 65 73 75 6c 74 2b 69 29 3b 0a 20 20  regResult+i);.  
2a6dc 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
2a6dd 65 44 65 73 74 21 3d 53 52 54 5f 45 78 69 73 74  eDest!=SRT_Exist
2a6de 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  s ){.    /* If t
2a6df 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69  he destination i
2a6e0 73 20 61 6e 20 45 58 49 53 54 53 28 2e 2e 2e 29  s an EXISTS(...)
2a6e1 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65   expression, the
2a6e2 20 61 63 74 75 61 6c 0a 20 20 20 20 2a 2a 20 76   actual.    ** v
2a6e3 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62  alues returned b
2a6e4 79 20 74 68 65 20 53 45 4c 45 43 54 20 61 72 65  y the SELECT are
2a6e5 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 20   not required.. 
2a6e6 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
2a6e7 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
2a6e8 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71 6c  pParse);.    sql
2a6e9 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
2a6ea 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 45 4c  List(pParse, pEL
2a6eb 69 73 74 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  ist, regResult, 
2a6ec 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
2a6ed 74 29 3b 0a 20 20 7d 0a 20 20 6e 43 6f 6c 75 6d  t);.  }.  nColum
2a6ee 6e 20 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a  n = nResultCol;.
2a6ef 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 44 49 53  .  /* If the DIS
2a6f0 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 77 61  TINCT keyword wa
2a6f1 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 74 68 65  s present on the
2a6f2 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2a6f3 74 0a 20 20 2a 2a 20 61 6e 64 20 74 68 69 73 20  t.  ** and this 
2a6f4 72 6f 77 20 68 61 73 20 62 65 65 6e 20 73 65 65  row has been see
2a6f5 6e 20 62 65 66 6f 72 65 2c 20 74 68 65 6e 20 64  n before, then d
2a6f6 6f 20 6e 6f 74 20 6d 61 6b 65 20 74 68 69 73 20  o not make this 
2a6f7 72 6f 77 0a 20 20 2a 2a 20 70 61 72 74 20 6f 66  row.  ** part of
2a6f8 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 20 20 2a   the result..  *
2a6f9 2f 0a 20 20 69 66 28 20 68 61 73 44 69 73 74 69  /.  if( hasDisti
2a6fa 6e 63 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72  nct ){.    asser
2a6fb 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
2a6fc 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69      assert( pELi
2a6fd 73 74 2d 3e 6e 45 78 70 72 3d 3d 6e 43 6f 6c 75  st->nExpr==nColu
2a6fe 6d 6e 20 29 3b 0a 20 20 20 20 63 6f 64 65 44 69  mn );.    codeDi
2a6ff 73 74 69 6e 63 74 28 70 50 61 72 73 65 2c 20 64  stinct(pParse, d
2a700 69 73 74 69 6e 63 74 2c 20 69 43 6f 6e 74 69 6e  istinct, iContin
2a701 75 65 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 65 67  ue, nColumn, reg
2a702 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 69 66 28  Result);.    if(
2a703 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a   pOrderBy==0 ){.
2a704 20 20 20 20 20 20 63 6f 64 65 4f 66 66 73 65 74        codeOffset
2a705 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75 65  (v, p, iContinue
2a706 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
2a707 69 66 28 20 63 68 65 63 6b 46 6f 72 4d 75 6c 74  if( checkForMult
2a708 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72 72  iColumnSelectErr
2a709 6f 72 28 70 50 61 72 73 65 2c 20 70 44 65 73 74  or(pParse, pDest
2a70a 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
2a70b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
2a70c 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 65    }..  switch( e
2a70d 44 65 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Dest ){.    /* I
2a70e 6e 20 74 68 69 73 20 6d 6f 64 65 2c 20 77 72 69  n this mode, wri
2a70f 74 65 20 65 61 63 68 20 71 75 65 72 79 20 72 65  te each query re
2a710 73 75 6c 74 20 74 6f 20 74 68 65 20 6b 65 79 20  sult to the key 
2a711 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  of the temporary
2a712 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 69 50  .    ** table iP
2a713 61 72 6d 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e  arm..    */.#ifn
2a714 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2a715 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a  COMPOUND_SELECT.
2a716 20 20 20 20 63 61 73 65 20 53 52 54 5f 55 6e 69      case SRT_Uni
2a717 6f 6e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  on: {.      int 
2a718 72 31 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73  r1;.      r1 = s
2a719 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2a71a 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2a71b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a71c 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
2a71d 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e  rd, regResult, n
2a71e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20  Column, r1);.   
2a71f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2a720 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e  dOp2(v, OP_IdxIn
2a721 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 31 29  sert, iParm, r1)
2a722 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
2a723 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2a724 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
2a725 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
2a726 20 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20     /* Construct 
2a727 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68  a record from th
2a728 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 2c 20  e query result, 
2a729 62 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a 20  but instead of. 
2a72a 20 20 20 2a 2a 20 73 61 76 69 6e 67 20 74 68 61     ** saving tha
2a72b 74 20 72 65 63 6f 72 64 2c 20 75 73 65 20 69 74  t record, use it
2a72c 20 61 73 20 61 20 6b 65 79 20 74 6f 20 64 65 6c   as a key to del
2a72d 65 74 65 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f  ete elements fro
2a72e 6d 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 65 6d  m.    ** the tem
2a72f 70 6f 72 61 72 79 20 74 61 62 6c 65 20 69 50 61  porary table iPa
2a730 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  rm..    */.    c
2a731 61 73 65 20 53 52 54 5f 45 78 63 65 70 74 3a 20  ase SRT_Except: 
2a732 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2a733 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2a734 49 64 78 44 65 6c 65 74 65 2c 20 69 50 61 72 6d  IdxDelete, iParm
2a735 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f  , regResult, nCo
2a736 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 62 72 65  lumn);.      bre
2a737 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
2a738 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74  ..    /* Store t
2a739 68 65 20 72 65 73 75 6c 74 20 61 73 20 64 61 74  he result as dat
2a73a 61 20 75 73 69 6e 67 20 61 20 75 6e 69 71 75 65  a using a unique
2a73b 20 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20   key..    */.   
2a73c 20 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a   case SRT_Table:
2a73d 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 70  .    case SRT_Ep
2a73e 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20  hemTab: {.      
2a73f 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33  int r1 = sqlite3
2a740 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2a741 65 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  e);.      testca
2a742 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 54  se( eDest==SRT_T
2a743 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 74 65  able );.      te
2a744 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53  stcase( eDest==S
2a745 52 54 5f 45 70 68 65 6d 54 61 62 20 29 3b 0a 20  RT_EphemTab );. 
2a746 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2a747 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
2a748 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75  eRecord, regResu
2a749 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29  lt, nColumn, r1)
2a74a 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64  ;.      if( pOrd
2a74b 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20  erBy ){.        
2a74c 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70  pushOntoSorter(p
2a74d 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c  Parse, pOrderBy,
2a74e 20 70 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 7d   p, r1);.      }
2a74f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e  else{.        in
2a750 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65  t r2 = sqlite3Ge
2a751 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2a752 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2a753 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a754 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 50 61 72  P_NewRowid, iPar
2a755 6d 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20  m, r2);.        
2a756 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a757 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
2a758 69 50 61 72 6d 2c 20 72 31 2c 20 72 32 29 3b 0a  iParm, r1, r2);.
2a759 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2a75a 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
2a75b 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20  PFLAG_APPEND);. 
2a75c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
2a75d 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2a75e 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20  rse, r2);.      
2a75f 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  }.      sqlite3R
2a760 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2a761 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
2a762 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23   break;.    }..#
2a763 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2a764 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20  IT_SUBQUERY.    
2a765 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63 72 65  /* If we are cre
2a766 61 74 69 6e 67 20 61 20 73 65 74 20 66 6f 72 20  ating a set for 
2a767 61 6e 20 22 65 78 70 72 20 49 4e 20 28 53 45 4c  an "expr IN (SEL
2a768 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72  ECT ...)" constr
2a769 75 63 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e  uct,.    ** then
2a76a 20 74 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65   there should be
2a76b 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f   a single item o
2a76c 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 57 72  n the stack.  Wr
2a76d 69 74 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20  ite this.    ** 
2a76e 69 74 65 6d 20 69 6e 74 6f 20 74 68 65 20 73 65  item into the se
2a76f 74 20 74 61 62 6c 65 20 77 69 74 68 20 62 6f 67  t table with bog
2a770 75 73 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a  us data..    */.
2a771 20 20 20 20 63 61 73 65 20 53 52 54 5f 53 65 74      case SRT_Set
2a772 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
2a773 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
2a774 20 20 20 20 20 20 70 2d 3e 61 66 66 69 6e 69 74        p->affinit
2a775 79 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61  y = sqlite3Compa
2a776 72 65 41 66 66 69 6e 69 74 79 28 70 45 4c 69 73  reAffinity(pELis
2a777 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 70  t->a[0].pExpr, p
2a778 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 29 3b  Dest->affinity);
2a779 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65  .      if( pOrde
2a77a 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  rBy ){.        /
2a77b 2a 20 41 74 20 66 69 72 73 74 20 67 6c 61 6e 63  * At first glanc
2a77c 65 20 79 6f 75 20 77 6f 75 6c 64 20 74 68 69 6e  e you would thin
2a77d 6b 20 77 65 20 63 6f 75 6c 64 20 6f 70 74 69 6d  k we could optim
2a77e 69 7a 65 20 6f 75 74 20 74 68 65 0a 20 20 20 20  ize out the.    
2a77f 20 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20      ** ORDER BY 
2a780 69 6e 20 74 68 69 73 20 63 61 73 65 20 73 69 6e  in this case sin
2a781 63 65 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20  ce the order of 
2a782 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 73  entries in the s
2a783 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 6f  et.        ** do
2a784 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 2e 20 20  es not matter.  
2a785 42 75 74 20 74 68 65 72 65 20 6d 69 67 68 74 20  But there might 
2a786 62 65 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73  be a LIMIT claus
2a787 65 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 20 20  e, in which.    
2a788 20 20 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20      ** case the 
2a789 6f 72 64 65 72 20 64 6f 65 73 20 6d 61 74 74 65  order does matte
2a78a 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 75 73  r */.        pus
2a78b 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72  hOntoSorter(pPar
2a78c 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c  se, pOrderBy, p,
2a78d 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20   regResult);.   
2a78e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2a78f 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
2a790 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2a791 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
2a792 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2a793 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
2a794 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 31 2c 20  , regResult, 1, 
2a795 72 31 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79  r1, &p->affinity
2a796 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 1);.        sq
2a797 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
2a798 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
2a799 72 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  rse, regResult, 
2a79a 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
2a79b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a79c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69   OP_IdxInsert, i
2a79d 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20  Parm, r1);.     
2a79e 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2a79f 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2a7a0 20 72 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20   r1);.      }.  
2a7a1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2a7a2 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79 20  ..    /* If any 
2a7a3 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68 65  row exist in the
2a7a4 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65 63   result set, rec
2a7a5 6f 72 64 20 74 68 61 74 20 66 61 63 74 20 61 6e  ord that fact an
2a7a6 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a  d abort..    */.
2a7a7 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 69      case SRT_Exi
2a7a8 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  sts: {.      sql
2a7a9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2a7aa 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
2a7ab 20 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 2f   iParm);.      /
2a7ac 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75  * The LIMIT clau
2a7ad 73 65 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74  se will terminat
2a7ae 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75  e the loop for u
2a7af 73 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b  s */.      break
2a7b0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2a7b1 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61  If this is a sca
2a7b2 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20  lar select that 
2a7b3 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78  is part of an ex
2a7b4 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20  pression, then. 
2a7b5 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20     ** store the 
2a7b6 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61  results in the a
2a7b7 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72  ppropriate memor
2a7b8 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b  y cell and break
2a7b9 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74   out.    ** of t
2a7ba 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20  he scan loop..  
2a7bb 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
2a7bc 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61  T_Mem: {.      a
2a7bd 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d  ssert( nColumn==
2a7be 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  1 );.      if( p
2a7bf 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
2a7c0 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65     pushOntoSorte
2a7c1 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  r(pParse, pOrder
2a7c2 42 79 2c 20 70 2c 20 72 65 67 52 65 73 75 6c 74  By, p, regResult
2a7c3 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
2a7c4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2a7c5 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72  xprCodeMove(pPar
2a7c6 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 69  se, regResult, i
2a7c7 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20  Parm, 1);.      
2a7c8 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63    /* The LIMIT c
2a7c9 6c 61 75 73 65 20 77 69 6c 6c 20 6a 75 6d 70 20  lause will jump 
2a7ca 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  out of the loop 
2a7cb 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20  for us */.      
2a7cc 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
2a7cd 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23     }.#endif /* #
2a7ce 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2a7cf 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a  IT_SUBQUERY */..
2a7d0 20 20 20 20 2f 2a 20 53 65 6e 64 20 74 68 65 20      /* Send the 
2a7d1 64 61 74 61 20 74 6f 20 74 68 65 20 63 61 6c 6c  data to the call
2a7d2 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6f 72  back function or
2a7d3 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e 65   to a subroutine
2a7d4 2e 20 20 49 6e 20 74 68 65 0a 20 20 20 20 2a 2a  .  In the.    **
2a7d5 20 63 61 73 65 20 6f 66 20 61 20 73 75 62 72 6f   case of a subro
2a7d6 75 74 69 6e 65 2c 20 74 68 65 20 73 75 62 72 6f  utine, the subro
2a7d7 75 74 69 6e 65 20 69 74 73 65 6c 66 20 69 73 20  utine itself is 
2a7d8 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a  responsible for.
2a7d9 20 20 20 20 2a 2a 20 70 6f 70 70 69 6e 67 20 74      ** popping t
2a7da 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  he data from the
2a7db 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20   stack..    */. 
2a7dc 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72 6f     case SRT_Coro
2a7dd 75 74 69 6e 65 3a 0a 20 20 20 20 63 61 73 65 20  utine:.    case 
2a7de 53 52 54 5f 4f 75 74 70 75 74 3a 20 7b 0a 20 20  SRT_Output: {.  
2a7df 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44      testcase( eD
2a7e0 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69  est==SRT_Corouti
2a7e1 6e 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ne );.      test
2a7e2 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54  case( eDest==SRT
2a7e3 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20 20 20 20  _Output );.     
2a7e4 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
2a7e5 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 20  .        int r1 
2a7e6 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2a7e7 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
2a7e8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2a7e9 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
2a7ea 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75  eRecord, regResu
2a7eb 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29  lt, nColumn, r1)
2a7ec 3b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e  ;.        pushOn
2a7ed 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c  toSorter(pParse,
2a7ee 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 31   pOrderBy, p, r1
2a7ef 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2a7f0 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2a7f1 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
2a7f2 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 44      }else if( eD
2a7f3 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69  est==SRT_Corouti
2a7f4 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ne ){.        sq
2a7f5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2a7f6 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65  v, OP_Yield, pDe
2a7f7 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20  st->iParm);.    
2a7f8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2a7f9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a7fa 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
2a7fb 6f 77 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e  ow, regResult, n
2a7fc 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20  Column);.       
2a7fd 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2a7fe 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
2a7ff 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75 6c  pParse, regResul
2a800 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  t, nColumn);.   
2a801 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
2a802 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 21 64 65  ;.    }..#if !de
2a803 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2a804 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20 2f  T_TRIGGER).    /
2a805 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 72 65  * Discard the re
2a806 73 75 6c 74 73 2e 20 20 54 68 69 73 20 69 73 20  sults.  This is 
2a807 75 73 65 64 20 66 6f 72 20 53 45 4c 45 43 54 20  used for SELECT 
2a808 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64  statements insid
2a809 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 6f 64  e.    ** the bod
2a80a 79 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20  y of a TRIGGER. 
2a80b 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20   The purpose of 
2a80c 73 75 63 68 20 73 65 6c 65 63 74 73 20 69 73 20  such selects is 
2a80d 74 6f 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 75  to call.    ** u
2a80e 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ser-defined func
2a80f 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20  tions that have 
2a810 73 69 64 65 20 65 66 66 65 63 74 73 2e 20 20 57  side effects.  W
2a811 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 0a 20 20  e do not care.  
2a812 20 20 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 61    ** about the a
2a813 63 74 75 61 6c 20 72 65 73 75 6c 74 73 20 6f 66  ctual results of
2a814 20 74 68 65 20 73 65 6c 65 63 74 2e 0a 20 20 20   the select..   
2a815 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a   */.    default:
2a816 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
2a817 20 65 44 65 73 74 3d 3d 53 52 54 5f 44 69 73 63   eDest==SRT_Disc
2a818 61 72 64 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ard );.      bre
2a819 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
2a81a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20  .  }..  /* Jump 
2a81b 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
2a81c 65 20 6c 6f 6f 70 20 69 66 20 74 68 65 20 4c 49  e loop if the LI
2a81d 4d 49 54 20 69 73 20 72 65 61 63 68 65 64 2e 0a  MIT is reached..
2a81e 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c    */.  if( p->iL
2a81f 69 6d 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65  imit ){.    asse
2a820 72 74 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20  rt( pOrderBy==0 
2a821 29 3b 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  );  /* If there 
2a822 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 2c 20  is an ORDER BY, 
2a823 74 68 65 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20  the call to.    
2a824 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a825 20 20 20 20 20 20 20 20 2a 2a 20 70 75 73 68 4f          ** pushO
2a826 6e 74 6f 53 6f 72 74 65 72 28 29 20 77 6f 75 6c  ntoSorter() woul
2a827 64 20 68 61 76 65 20 63 6c 65 61 72 65 64 20 70  d have cleared p
2a828 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 20 20  ->iLimit */.    
2a829 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a82a 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
2a82b 70 2d 3e 69 4c 69 6d 69 74 2c 20 2d 31 29 3b 0a  p->iLimit, -1);.
2a82c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a82d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65  ddOp2(v, OP_IfZe
2a82e 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69  ro, p->iLimit, i
2a82f 42 72 65 61 6b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Break);.  }.}../
2a830 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 65 78  *.** Given an ex
2a831 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 67  pression list, g
2a832 65 6e 65 72 61 74 65 20 61 20 4b 65 79 49 6e 66  enerate a KeyInf
2a833 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  o structure that
2a834 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65 20   records.** the 
2a835 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2a836 63 65 20 66 6f 72 20 65 61 63 68 20 65 78 70 72  ce for each expr
2a837 65 73 73 69 6f 6e 20 69 6e 20 74 68 61 74 20 65  ession in that e
2a838 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a  xpression list..
2a839 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 70  **.** If the Exp
2a83a 72 4c 69 73 74 20 69 73 20 61 6e 20 4f 52 44 45  rList is an ORDE
2a83b 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59  R BY or GROUP BY
2a83c 20 63 6c 61 75 73 65 20 74 68 65 6e 20 74 68 65   clause then the
2a83d 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20 4b 65   resulting.** Ke
2a83e 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
2a83f 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  is appropriate f
2a840 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  or initializing 
2a841 61 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20  a virtual index 
2a842 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  to.** implement 
2a843 74 68 61 74 20 63 6c 61 75 73 65 2e 20 20 49 66  that clause.  If
2a844 20 74 68 65 20 45 78 70 72 4c 69 73 74 20 69 73   the ExprList is
2a845 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2a846 6f 66 20 61 20 53 45 4c 45 43 54 0a 2a 2a 20 74  of a SELECT.** t
2a847 68 65 6e 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  hen the KeyInfo 
2a848 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70 70  structure is app
2a849 72 6f 70 72 69 61 74 65 20 66 6f 72 20 69 6e 69  ropriate for ini
2a84a 74 69 61 6c 69 7a 69 6e 67 20 61 20 76 69 72 74  tializing a virt
2a84b 75 61 6c 0a 2a 2a 20 69 6e 64 65 78 20 74 6f 20  ual.** index to 
2a84c 69 6d 70 6c 65 6d 65 6e 74 20 61 20 44 49 53 54  implement a DIST
2a84d 49 4e 43 54 20 74 65 73 74 2e 0a 2a 2a 0a 2a 2a  INCT test..**.**
2a84e 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
2a84f 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63  he KeyInfo struc
2a850 74 75 72 65 20 69 73 20 6f 62 74 61 69 6e 20 66  ture is obtain f
2a851 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 20 54 68 65  rom malloc.  The
2a852 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63   calling.** func
2a853 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69  tion is responsi
2a854 62 6c 65 20 66 6f 72 20 73 65 65 69 6e 67 20 74  ble for seeing t
2a855 68 61 74 20 74 68 69 73 20 73 74 72 75 63 74 75  hat this structu
2a856 72 65 20 69 73 20 65 76 65 6e 74 75 61 6c 6c 79  re is eventually
2a857 0a 2a 2a 20 66 72 65 65 64 2e 20 20 41 64 64 20  .** freed.  Add 
2a858 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  the KeyInfo stru
2a859 63 74 75 72 65 20 74 6f 20 74 68 65 20 50 34 20  cture to the P4 
2a85a 66 69 65 6c 64 20 6f 66 20 61 6e 20 6f 70 63 6f  field of an opco
2a85b 64 65 20 75 73 69 6e 67 0a 2a 2a 20 50 34 5f 4b  de using.** P4_K
2a85c 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69  EYINFO_HANDOFF i
2a85d 73 20 74 68 65 20 75 73 75 61 6c 20 77 61 79 20  s the usual way 
2a85e 6f 66 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  of dealing with 
2a85f 74 68 69 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  this..*/.static 
2a860 4b 65 79 49 6e 66 6f 20 2a 6b 65 79 49 6e 66 6f  KeyInfo *keyInfo
2a861 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 50 61 72  FromExprList(Par
2a862 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
2a863 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20  List *pList){.  
2a864 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2a865 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
2a866 6e 45 78 70 72 3b 0a 20 20 4b 65 79 49 6e 66 6f  nExpr;.  KeyInfo
2a867 20 2a 70 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63   *pInfo;.  struc
2a868 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
2a869 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b  *pItem;.  int i;
2a86a 0a 0a 20 20 6e 45 78 70 72 20 3d 20 70 4c 69 73  ..  nExpr = pLis
2a86b 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 66  t->nExpr;.  pInf
2a86c 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  o = sqlite3DbMal
2a86d 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
2a86e 6f 66 28 2a 70 49 6e 66 6f 29 20 2b 20 6e 45 78  of(*pInfo) + nEx
2a86f 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  pr*(sizeof(CollS
2a870 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20 69 66 28  eq*)+1) );.  if(
2a871 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20 20 70 49   pInfo ){.    pI
2a872 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  nfo->aSortOrder 
2a873 3d 20 28 75 38 2a 29 26 70 49 6e 66 6f 2d 3e 61  = (u8*)&pInfo->a
2a874 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a 20 20 20  Coll[nExpr];.   
2a875 20 70 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d   pInfo->nField =
2a876 20 28 75 31 36 29 6e 45 78 70 72 3b 0a 20 20 20   (u16)nExpr;.   
2a877 20 70 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e   pInfo->enc = EN
2a878 43 28 64 62 29 3b 0a 20 20 20 20 70 49 6e 66 6f  C(db);.    pInfo
2a879 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 66  ->db = db;.    f
2a87a 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c  or(i=0, pItem=pL
2a87b 69 73 74 2d 3e 61 3b 20 69 3c 6e 45 78 70 72 3b  ist->a; i<nExpr;
2a87c 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
2a87d 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
2a87e 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 43 6f 6c  Coll;.      pCol
2a87f 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  l = sqlite3ExprC
2a880 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
2a881 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20  Item->pExpr);.  
2a882 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29      if( !pColl )
2a883 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20  {.        pColl 
2a884 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b  = db->pDfltColl;
2a885 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
2a886 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d  Info->aColl[i] =
2a887 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 49   pColl;.      pI
2a888 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  nfo->aSortOrder[
2a889 69 5d 20 3d 20 70 49 74 65 6d 2d 3e 73 6f 72 74  i] = pItem->sort
2a88a 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  Order;.    }.  }
2a88b 0a 20 20 72 65 74 75 72 6e 20 70 49 6e 66 6f 3b  .  return pInfo;
2a88c 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  .}.../*.** If th
2a88d 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 20 77 61 73  e inner loop was
2a88e 20 67 65 6e 65 72 61 74 65 64 20 75 73 69 6e 67   generated using
2a88f 20 61 20 6e 6f 6e 2d 6e 75 6c 6c 20 70 4f 72 64   a non-null pOrd
2a890 65 72 42 79 20 61 72 67 75 6d 65 6e 74 2c 0a 2a  erBy argument,.*
2a891 2a 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  * then the resul
2a892 74 73 20 77 65 72 65 20 70 6c 61 63 65 64 20 69  ts were placed i
2a893 6e 20 61 20 73 6f 72 74 65 72 2e 20 20 41 66 74  n a sorter.  Aft
2a894 65 72 20 74 68 65 20 6c 6f 6f 70 20 69 73 20 74  er the loop is t
2a895 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 77 65 20  erminated.** we 
2a896 6e 65 65 64 20 74 6f 20 72 75 6e 20 74 68 65 20  need to run the 
2a897 73 6f 72 74 65 72 20 61 6e 64 20 6f 75 74 70 75  sorter and outpu
2a898 74 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20  t the results.  
2a899 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  The following.**
2a89a 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
2a89b 65 73 20 74 68 65 20 63 6f 64 65 20 6e 65 65 64  es the code need
2a89c 65 64 20 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a  ed to do that..*
2a89d 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65  /.static void ge
2a89e 6e 65 72 61 74 65 53 6f 72 74 54 61 69 6c 28 0a  nerateSortTail(.
2a89f 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2a8a0 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2a8a1 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
2a8a2 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 2f 2a  ct *p,        /*
2a8a3 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
2a8a4 65 6d 65 6e 74 20 2a 2f 0a 20 20 56 64 62 65 20  ement */.  Vdbe 
2a8a5 2a 76 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  *v,          /* 
2a8a6 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
2a8a7 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a  to this VDBE */.
2a8a8 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20    int nColumn,  
2a8a9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2a8aa 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61   columns of data
2a8ab 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
2a8ac 20 2a 70 44 65 73 74 20 2f 2a 20 57 72 69 74 65   *pDest /* Write
2a8ad 20 74 68 65 20 73 6f 72 74 65 64 20 72 65 73 75   the sorted resu
2a8ae 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  lts here */.){. 
2a8af 20 69 6e 74 20 61 64 64 72 42 72 65 61 6b 20 3d   int addrBreak =
2a8b0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2a8b1 4c 61 62 65 6c 28 76 29 3b 20 20 20 20 20 2f 2a  Label(v);     /*
2a8b2 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 65 78   Jump here to ex
2a8b3 69 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  it loop */.  int
2a8b4 20 61 64 64 72 43 6f 6e 74 69 6e 75 65 20 3d 20   addrContinue = 
2a8b5 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2a8b6 61 62 65 6c 28 76 29 3b 20 20 2f 2a 20 4a 75 6d  abel(v);  /* Jum
2a8b7 70 20 68 65 72 65 20 66 6f 72 20 6e 65 78 74 20  p here for next 
2a8b8 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61  cycle */.  int a
2a8b9 64 64 72 3b 0a 20 20 69 6e 74 20 69 54 61 62 3b  ddr;.  int iTab;
2a8ba 0a 20 20 69 6e 74 20 70 73 65 75 64 6f 54 61 62  .  int pseudoTab
2a8bb 20 3d 20 30 3b 0a 20 20 45 78 70 72 4c 69 73 74   = 0;.  ExprList
2a8bc 20 2a 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e   *pOrderBy = p->
2a8bd 70 4f 72 64 65 72 42 79 3b 0a 0a 20 20 69 6e 74  pOrderBy;..  int
2a8be 20 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d 3e   eDest = pDest->
2a8bf 65 44 65 73 74 3b 0a 20 20 69 6e 74 20 69 50 61  eDest;.  int iPa
2a8c0 72 6d 20 3d 20 70 44 65 73 74 2d 3e 69 50 61 72  rm = pDest->iPar
2a8c1 6d 3b 0a 0a 20 20 69 6e 74 20 72 65 67 52 6f 77  m;..  int regRow
2a8c2 3b 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  ;.  int regRowid
2a8c3 3b 0a 0a 20 20 69 54 61 62 20 3d 20 70 4f 72 64  ;..  iTab = pOrd
2a8c4 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 3b 0a  erBy->iECursor;.
2a8c5 20 20 72 65 67 52 6f 77 20 3d 20 73 71 6c 69 74    regRow = sqlit
2a8c6 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2a8c7 72 73 65 29 3b 0a 20 20 69 66 28 20 65 44 65 73  rse);.  if( eDes
2a8c8 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c  t==SRT_Output ||
2a8c9 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f   eDest==SRT_Coro
2a8ca 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 70 73 65  utine ){.    pse
2a8cb 75 64 6f 54 61 62 20 3d 20 70 50 61 72 73 65 2d  udoTab = pParse-
2a8cc 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 73 71 6c  >nTab++;.    sql
2a8cd 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2a8ce 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c  , OP_OpenPseudo,
2a8cf 20 70 73 65 75 64 6f 54 61 62 2c 20 72 65 67 52   pseudoTab, regR
2a8d0 6f 77 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ow, nColumn);.  
2a8d1 20 20 72 65 67 52 6f 77 69 64 20 3d 20 30 3b 0a    regRowid = 0;.
2a8d2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 67    }else{.    reg
2a8d3 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47  Rowid = sqlite3G
2a8d4 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
2a8d5 29 3b 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20  );.  }.  addr = 
2a8d6 31 20 2b 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 + sqlite3VdbeA
2a8d7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74  ddOp2(v, OP_Sort
2a8d8 2c 20 69 54 61 62 2c 20 61 64 64 72 42 72 65 61  , iTab, addrBrea
2a8d9 6b 29 3b 0a 20 20 63 6f 64 65 4f 66 66 73 65 74  k);.  codeOffset
2a8da 28 76 2c 20 70 2c 20 61 64 64 72 43 6f 6e 74 69  (v, p, addrConti
2a8db 6e 75 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  nue);.  sqlite3V
2a8dc 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2a8dd 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c 20 70 4f  Column, iTab, pO
2a8de 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20 2b 20  rderBy->nExpr + 
2a8df 31 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 73 77  1, regRow);.  sw
2a8e0 69 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a 20  itch( eDest ){. 
2a8e1 20 20 20 63 61 73 65 20 53 52 54 5f 54 61 62 6c     case SRT_Tabl
2a8e2 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  e:.    case SRT_
2a8e3 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20  EphemTab: {.    
2a8e4 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65 73    testcase( eDes
2a8e5 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b 0a  t==SRT_Table );.
2a8e6 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2a8e7 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d  eDest==SRT_Ephem
2a8e8 54 61 62 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  Tab );.      sql
2a8e9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2a8ea 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69  , OP_NewRowid, i
2a8eb 50 61 72 6d 2c 20 72 65 67 52 6f 77 69 64 29 3b  Parm, regRowid);
2a8ec 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2a8ed 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
2a8ee 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 65  nsert, iParm, re
2a8ef 67 52 6f 77 2c 20 72 65 67 52 6f 77 69 64 29 3b  gRow, regRowid);
2a8f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2a8f1 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
2a8f2 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20  FLAG_APPEND);.  
2a8f3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2a8f4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2a8f5 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20  OMIT_SUBQUERY.  
2a8f6 20 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a 20    case SRT_Set: 
2a8f7 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
2a8f8 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20  nColumn==1 );.  
2a8f9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a8fa 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp4(v, OP_Make
2a8fb 52 65 63 6f 72 64 2c 20 72 65 67 52 6f 77 2c 20  Record, regRow, 
2a8fc 31 2c 20 72 65 67 52 6f 77 69 64 2c 20 26 70 2d  1, regRowid, &p-
2a8fd 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20  >affinity, 1);. 
2a8fe 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2a8ff 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
2a900 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 52  nge(pParse, regR
2a901 6f 77 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  ow, 1);.      sq
2a902 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a903 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
2a904 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 69 64   iParm, regRowid
2a905 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2a906 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
2a907 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20  RT_Mem: {.      
2a908 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d  assert( nColumn=
2a909 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =1 );.      sqli
2a90a 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
2a90b 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 2c 20  pParse, regRow, 
2a90c 69 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20  iParm, 1);.     
2a90d 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c   /* The LIMIT cl
2a90e 61 75 73 65 20 77 69 6c 6c 20 74 65 72 6d 69 6e  ause will termin
2a90f 61 74 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72  ate the loop for
2a910 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72 65   us */.      bre
2a911 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
2a912 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
2a913 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
2a914 20 20 20 61 73 73 65 72 74 28 20 65 44 65 73 74     assert( eDest
2a915 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c 20  ==SRT_Output || 
2a916 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75  eDest==SRT_Corou
2a917 74 69 6e 65 20 29 3b 20 0a 20 20 20 20 20 20 74  tine ); .      t
2a918 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d  estcase( eDest==
2a919 53 52 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20  SRT_Output );.  
2a91a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44      testcase( eD
2a91b 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69  est==SRT_Corouti
2a91c 6e 65 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  ne );.      for(
2a91d 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20  i=0; i<nColumn; 
2a91e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73  i++){.        as
2a91f 73 65 72 74 28 20 72 65 67 52 6f 77 21 3d 70 44  sert( regRow!=pD
2a920 65 73 74 2d 3e 69 4d 65 6d 2b 69 20 29 3b 0a 20  est->iMem+i );. 
2a921 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2a922 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
2a923 6f 6c 75 6d 6e 2c 20 70 73 65 75 64 6f 54 61 62  olumn, pseudoTab
2a924 2c 20 69 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d  , i, pDest->iMem
2a925 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  +i);.        if(
2a926 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   i==0 ){.       
2a927 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2a928 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
2a929 5f 43 4c 45 41 52 43 41 43 48 45 29 3b 0a 20 20  _CLEARCACHE);.  
2a92a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2a92b 20 20 20 20 20 20 69 66 28 20 65 44 65 73 74 3d        if( eDest=
2a92c 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20  =SRT_Output ){. 
2a92d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2a92e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2a92f 65 73 75 6c 74 52 6f 77 2c 20 70 44 65 73 74 2d  esultRow, pDest-
2a930 3e 69 4d 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  >iMem, nColumn);
2a931 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2a932 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
2a933 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
2a934 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e 43 6f  pDest->iMem, nCo
2a935 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c  lumn);.      }el
2a936 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
2a937 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2a938 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 74   OP_Yield, pDest
2a939 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20  ->iParm);.      
2a93a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
2a93b 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
2a93c 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2a93d 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 29  (pParse, regRow)
2a93e 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
2a93f 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2a940 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20  , regRowid);..  
2a941 2f 2a 20 4c 49 4d 49 54 20 68 61 73 20 62 65 65  /* LIMIT has bee
2a942 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79  n implemented by
2a943 20 74 68 65 20 70 75 73 68 4f 6e 74 6f 53 6f 72   the pushOntoSor
2a944 74 65 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 20  ter() routine.. 
2a945 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
2a946 3e 69 4c 69 6d 69 74 3d 3d 30 20 29 3b 0a 0a 20  >iLimit==0 );.. 
2a947 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f   /* The bottom o
2a948 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a  f the loop.  */.
2a949 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2a94a 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64  olveLabel(v, add
2a94b 72 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 73 71  rContinue);.  sq
2a94c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a94d 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62  v, OP_Next, iTab
2a94e 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69 74  , addr);.  sqlit
2a94f 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2a950 65 6c 28 76 2c 20 61 64 64 72 42 72 65 61 6b 29  el(v, addrBreak)
2a951 3b 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53  ;.  if( eDest==S
2a952 52 54 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44 65  RT_Output || eDe
2a953 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e  st==SRT_Coroutin
2a954 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
2a955 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2a956 5f 43 6c 6f 73 65 2c 20 70 73 65 75 64 6f 54 61  _Close, pseudoTa
2a957 62 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  b, 0);.  }.}../*
2a958 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
2a959 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67  nter to a string
2a95a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2a95b 27 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70  'declaration typ
2a95c 65 27 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78 70  e' of the.** exp
2a95d 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 54  ression pExpr. T
2a95e 68 65 20 73 74 72 69 6e 67 20 6d 61 79 20 62 65  he string may be
2a95f 20 74 72 65 61 74 65 64 20 61 73 20 73 74 61 74   treated as stat
2a960 69 63 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ic by the caller
2a961 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 63 6c  ..**.** The decl
2a962 61 72 61 74 69 6f 6e 20 74 79 70 65 20 69 73 20  aration type is 
2a963 74 68 65 20 65 78 61 63 74 20 64 61 74 61 74 79  the exact dataty
2a964 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 65 78  pe definition ex
2a965 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65  tracted from the
2a966 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 43 52 45  .** original CRE
2a967 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
2a968 65 6e 74 20 69 66 20 74 68 65 20 65 78 70 72 65  ent if the expre
2a969 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d  ssion is a colum
2a96a 6e 2e 20 54 68 65 0a 2a 2a 20 64 65 63 6c 61 72  n. The.** declar
2a96b 61 74 69 6f 6e 20 74 79 70 65 20 66 6f 72 20 61  ation type for a
2a96c 20 52 4f 57 49 44 20 66 69 65 6c 64 20 69 73 20   ROWID field is 
2a96d 49 4e 54 45 47 45 52 2e 20 45 78 61 63 74 6c 79  INTEGER. Exactly
2a96e 20 77 68 65 6e 20 61 6e 20 65 78 70 72 65 73 73   when an express
2a96f 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64  ion.** is consid
2a970 65 72 65 64 20 61 20 63 6f 6c 75 6d 6e 20 63 61  ered a column ca
2a971 6e 20 62 65 20 63 6f 6d 70 6c 65 78 20 69 6e 20  n be complex in 
2a972 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20  the presence of 
2a973 73 75 62 71 75 65 72 69 65 73 2e 20 54 68 65 0a  subqueries. The.
2a974 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78  ** result-set ex
2a975 70 72 65 73 73 69 6f 6e 20 69 6e 20 61 6c 6c 20  pression in all 
2a976 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2a977 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2a978 74 73 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 69 64  ts is .** consid
2a979 65 72 65 64 20 61 20 63 6f 6c 75 6d 6e 20 62 79  ered a column by
2a97a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
2a97b 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 63  **.**   SELECT c
2a97c 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0a 2a 2a 20  ol FROM tbl;.** 
2a97d 20 20 53 45 4c 45 43 54 20 28 53 45 4c 45 43 54    SELECT (SELECT
2a97e 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0a 2a   col FROM tbl;.*
2a97f 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c 45  *   SELECT (SELE
2a980 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 29  CT col FROM tbl)
2a981 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 61 62  ;.**   SELECT ab
2a982 63 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 63  c FROM (SELECT c
2a983 6f 6c 20 41 53 20 61 62 63 20 46 52 4f 4d 20 74  ol AS abc FROM t
2a984 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20  bl);.** .** The 
2a985 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
2a986 20 66 6f 72 20 61 6e 79 20 65 78 70 72 65 73 73   for any express
2a987 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 61  ion other than a
2a988 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 2e   column is NULL.
2a989 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
2a98a 20 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 54 79 70   char *columnTyp
2a98b 65 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  e(.  NameContext
2a98c 20 2a 70 4e 43 2c 20 0a 20 20 45 78 70 72 20 2a   *pNC, .  Expr *
2a98d 70 45 78 70 72 2c 0a 20 20 63 6f 6e 73 74 20 63  pExpr,.  const c
2a98e 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 44 62  har **pzOriginDb
2a98f 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
2a990 2a 70 7a 4f 72 69 67 69 6e 54 61 62 2c 0a 20 20  *pzOriginTab,.  
2a991 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f  const char **pzO
2a992 72 69 67 69 6e 43 6f 6c 0a 29 7b 0a 20 20 63 68  riginCol.){.  ch
2a993 61 72 20 63 6f 6e 73 74 20 2a 7a 54 79 70 65 20  ar const *zType 
2a994 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  = 0;.  char cons
2a995 74 20 2a 7a 4f 72 69 67 69 6e 44 62 20 3d 20 30  t *zOriginDb = 0
2a996 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
2a997 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 30 3b 0a  zOriginTab = 0;.
2a998 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f    char const *zO
2a999 72 69 67 69 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20  riginCol = 0;.  
2a99a 69 6e 74 20 6a 3b 0a 20 20 69 66 28 20 4e 45 56  int j;.  if( NEV
2a99b 45 52 28 70 45 78 70 72 3d 3d 30 29 20 7c 7c 20  ER(pExpr==0) || 
2a99c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3d 3d 30  pNC->pSrcList==0
2a99d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20   ) return 0;..  
2a99e 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f  switch( pExpr->o
2a99f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b  p ){.    case TK
2a9a0 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20  _AGG_COLUMN:.   
2a9a1 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a   case TK_COLUMN:
2a9a2 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20   {.      /* The 
2a9a3 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
2a9a4 63 6f 6c 75 6d 6e 2e 20 4c 6f 63 61 74 65 20 74  column. Locate t
2a9a5 68 65 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6c  he table the col
2a9a6 75 6d 6e 20 69 73 20 62 65 69 6e 67 0a 20 20 20  umn is being.   
2a9a7 20 20 20 2a 2a 20 65 78 74 72 61 63 74 65 64 20     ** extracted 
2a9a8 66 72 6f 6d 20 69 6e 20 4e 61 6d 65 43 6f 6e 74  from in NameCont
2a9a9 65 78 74 2e 70 53 72 63 4c 69 73 74 2e 20 54 68  ext.pSrcList. Th
2a9aa 69 73 20 74 61 62 6c 65 20 6d 61 79 20 62 65 20  is table may be 
2a9ab 72 65 61 6c 0a 20 20 20 20 20 20 2a 2a 20 64 61  real.      ** da
2a9ac 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
2a9ad 61 20 73 75 62 71 75 65 72 79 2e 0a 20 20 20 20  a subquery..    
2a9ae 20 20 2a 2f 0a 20 20 20 20 20 20 54 61 62 6c 65    */.      Table
2a9af 20 2a 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20   *pTab = 0;     
2a9b0 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
2a9b1 73 74 72 75 63 74 75 72 65 20 63 6f 6c 75 6d 6e  structure column
2a9b2 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72   is extracted fr
2a9b3 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65  om */.      Sele
2a9b4 63 74 20 2a 70 53 20 3d 20 30 3b 20 20 20 20 20  ct *pS = 0;     
2a9b5 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63          /* Selec
2a9b6 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20  t the column is 
2a9b7 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 2a  extracted from *
2a9b8 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  /.      int iCol
2a9b9 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d   = pExpr->iColum
2a9ba 6e 3b 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20  n;  /* Index of 
2a9bb 63 6f 6c 75 6d 6e 20 69 6e 20 70 54 61 62 20 2a  column in pTab *
2a9bc 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2a9bd 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2a9be 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  AGG_COLUMN );.  
2a9bf 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
2a9c0 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  xpr->op==TK_COLU
2a9c1 4d 4e 20 29 3b 0a 20 20 20 20 20 20 77 68 69 6c  MN );.      whil
2a9c2 65 28 20 70 4e 43 20 26 26 20 21 70 54 61 62 20  e( pNC && !pTab 
2a9c3 29 7b 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69  ){.        SrcLi
2a9c4 73 74 20 2a 70 54 61 62 4c 69 73 74 20 3d 20 70  st *pTabList = p
2a9c5 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20  NC->pSrcList;.  
2a9c6 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 6a 3c        for(j=0;j<
2a9c7 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 26  pTabList->nSrc &
2a9c8 26 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d  & pTabList->a[j]
2a9c9 2e 69 43 75 72 73 6f 72 21 3d 70 45 78 70 72 2d  .iCursor!=pExpr-
2a9ca 3e 69 54 61 62 6c 65 3b 6a 2b 2b 29 3b 0a 20 20  >iTable;j++);.  
2a9cb 20 20 20 20 20 20 69 66 28 20 6a 3c 70 54 61 62        if( j<pTab
2a9cc 4c 69 73 74 2d 3e 6e 53 72 63 20 29 7b 0a 20 20  List->nSrc ){.  
2a9cd 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70          pTab = p
2a9ce 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54  TabList->a[j].pT
2a9cf 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53  ab;.          pS
2a9d0 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a   = pTabList->a[j
2a9d1 5d 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20  ].pSelect;.     
2a9d2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2a9d3 20 20 20 20 70 4e 43 20 3d 20 70 4e 43 2d 3e 70      pNC = pNC->p
2a9d4 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Next;.        }.
2a9d5 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
2a9d6 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20  f( pTab==0 ){.  
2a9d7 20 20 20 20 20 20 2f 2a 20 41 74 20 6f 6e 65 20        /* At one 
2a9d8 74 69 6d 65 2c 20 63 6f 64 65 20 73 75 63 68 20  time, code such 
2a9d9 61 73 20 22 53 45 4c 45 43 54 20 6e 65 77 2e 78  as "SELECT new.x
2a9da 22 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67  " within a trigg
2a9db 65 72 20 77 6f 75 6c 64 0a 20 20 20 20 20 20 20  er would.       
2a9dc 20 2a 2a 20 63 61 75 73 65 20 74 68 69 73 20 63   ** cause this c
2a9dd 6f 6e 64 69 74 69 6f 6e 20 74 6f 20 72 75 6e 2e  ondition to run.
2a9de 20 20 53 69 6e 63 65 20 74 68 65 6e 2c 20 77 65    Since then, we
2a9df 20 68 61 76 65 20 72 65 73 74 72 75 63 74 75 72   have restructur
2a9e0 65 64 20 68 6f 77 0a 20 20 20 20 20 20 20 20 2a  ed how.        *
2a9e1 2a 20 74 72 69 67 67 65 72 20 63 6f 64 65 20 69  * trigger code i
2a9e2 73 20 67 65 6e 65 72 61 74 65 64 20 61 6e 64 20  s generated and 
2a9e3 73 6f 20 74 68 69 73 20 63 6f 6e 64 69 74 69 6f  so this conditio
2a9e4 6e 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 0a  n is no longer .
2a9e5 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 73 73 69          ** possi
2a9e6 62 6c 65 2e 20 48 6f 77 65 76 65 72 2c 20 69 74  ble. However, it
2a9e7 20 63 61 6e 20 73 74 69 6c 6c 20 62 65 20 74 72   can still be tr
2a9e8 75 65 20 66 6f 72 20 73 74 61 74 65 6d 65 6e 74  ue for statement
2a9e9 73 20 6c 69 6b 65 0a 20 20 20 20 20 20 20 20 2a  s like.        *
2a9ea 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  * the following:
2a9eb 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
2a9ec 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20      **   CREATE 
2a9ed 54 41 42 4c 45 20 74 31 28 63 6f 6c 20 49 4e 54  TABLE t1(col INT
2a9ee 45 47 45 52 29 3b 0a 20 20 20 20 20 20 20 20 2a  EGER);.        *
2a9ef 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c 45  *   SELECT (SELE
2a9f0 43 54 20 74 31 2e 63 6f 6c 29 20 46 52 4f 4d 20  CT t1.col) FROM 
2a9f1 46 52 4f 4d 20 74 31 3b 0a 20 20 20 20 20 20 20  FROM t1;.       
2a9f2 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 77   **.        ** w
2a9f3 68 65 6e 20 63 6f 6c 75 6d 6e 54 79 70 65 28 29  hen columnType()
2a9f4 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68   is called on th
2a9f5 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 74 31  e expression "t1
2a9f6 2e 63 6f 6c 22 20 69 6e 20 74 68 65 20 0a 20 20  .col" in the .  
2a9f7 20 20 20 20 20 20 2a 2a 20 73 75 62 2d 73 65 6c        ** sub-sel
2a9f8 65 63 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ect. In this cas
2a9f9 65 2c 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d  e, set the colum
2a9fa 6e 20 74 79 70 65 20 74 6f 20 4e 55 4c 4c 2c 20  n type to NULL, 
2a9fb 65 76 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  even.        ** 
2a9fc 74 68 6f 75 67 68 20 69 74 20 73 68 6f 75 6c 64  though it should
2a9fd 20 72 65 61 6c 6c 79 20 62 65 20 22 49 4e 54 45   really be "INTE
2a9fe 47 45 52 22 2e 0a 20 20 20 20 20 20 20 20 2a 2a  GER"..        **
2a9ff 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73  .        ** This
2aa00 20 69 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65   is not a proble
2aa01 6d 2c 20 61 73 20 74 68 65 20 63 6f 6c 75 6d 6e  m, as the column
2aa02 20 74 79 70 65 20 6f 66 20 22 74 31 2e 63 6f 6c   type of "t1.col
2aa03 22 20 69 73 20 6e 65 76 65 72 0a 20 20 20 20 20  " is never.     
2aa04 20 20 20 2a 2a 20 75 73 65 64 2e 20 57 68 65 6e     ** used. When
2aa05 20 63 6f 6c 75 6d 6e 54 79 70 65 28 29 20 69 73   columnType() is
2aa06 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 65   called on the e
2aa07 78 70 72 65 73 73 69 6f 6e 20 0a 20 20 20 20 20  xpression .     
2aa08 20 20 20 2a 2a 20 22 28 53 45 4c 45 43 54 20 74     ** "(SELECT t
2aa09 31 2e 63 6f 6c 29 22 2c 20 74 68 65 20 63 6f 72  1.col)", the cor
2aa0a 72 65 63 74 20 74 79 70 65 20 69 73 20 72 65 74  rect type is ret
2aa0b 75 72 6e 65 64 20 28 73 65 65 20 74 68 65 20 54  urned (see the T
2aa0c 4b 5f 53 45 4c 45 43 54 0a 20 20 20 20 20 20 20  K_SELECT.       
2aa0d 20 2a 2a 20 62 72 61 6e 63 68 20 62 65 6c 6f 77   ** branch below
2aa0e 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72  .  */.        br
2aa0f 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  eak;.      }..  
2aa10 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
2aa11 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 3d   && pExpr->pTab=
2aa12 3d 70 54 61 62 20 29 3b 0a 20 20 20 20 20 20 69  =pTab );.      i
2aa13 66 28 20 70 53 20 29 7b 0a 20 20 20 20 20 20 20  f( pS ){.       
2aa14 20 2f 2a 20 54 68 65 20 22 74 61 62 6c 65 22 20   /* The "table" 
2aa15 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 73 75  is actually a su
2aa16 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 20 76 69  b-select or a vi
2aa17 65 77 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ew in the FROM c
2aa18 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a  lause.        **
2aa19 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
2aa1a 74 61 74 65 6d 65 6e 74 2e 20 52 65 74 75 72 6e  tatement. Return
2aa1b 20 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e   the declaration
2aa1c 20 74 79 70 65 20 61 6e 64 20 6f 72 69 67 69 6e   type and origin
2aa1d 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61  .        ** data
2aa1e 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 2d   for the result-
2aa1f 73 65 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  set column of th
2aa20 65 20 73 75 62 2d 73 65 6c 65 63 74 2e 0a 20 20  e sub-select..  
2aa21 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2aa22 20 69 66 28 20 41 4c 57 41 59 53 28 69 43 6f 6c   if( ALWAYS(iCol
2aa23 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 53 2d 3e  >=0 && iCol<pS->
2aa24 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 20 29  pEList->nExpr) )
2aa25 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  {.          /* I
2aa26 66 20 69 43 6f 6c 20 69 73 20 6c 65 73 73 20 74  f iCol is less t
2aa27 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  han zero, then t
2aa28 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65  he expression re
2aa29 71 75 65 73 74 73 20 74 68 65 0a 20 20 20 20 20  quests the.     
2aa2a 20 20 20 20 20 2a 2a 20 72 6f 77 69 64 20 6f 66       ** rowid of
2aa2b 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74 20   the sub-select 
2aa2c 6f 72 20 76 69 65 77 2e 20 54 68 69 73 20 65 78  or view. This ex
2aa2d 70 72 65 73 73 69 6f 6e 20 69 73 20 6c 65 67 61  pression is lega
2aa2e 6c 20 28 73 65 65 20 0a 20 20 20 20 20 20 20 20  l (see .        
2aa2f 20 20 2a 2a 20 74 65 73 74 20 63 61 73 65 20 6d    ** test case m
2aa30 69 73 63 32 2e 32 2e 32 29 20 2d 20 69 74 20 61  isc2.2.2) - it a
2aa31 6c 77 61 79 73 20 65 76 61 6c 75 61 74 65 73 20  lways evaluates 
2aa32 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20  to NULL..       
2aa33 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
2aa34 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
2aa35 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20  .          Expr 
2aa36 2a 70 20 3d 20 70 53 2d 3e 70 45 4c 69 73 74 2d  *p = pS->pEList-
2aa37 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a  >a[iCol].pExpr;.
2aa38 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e 70 53            sNC.pS
2aa39 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70 53 72  rcList = pS->pSr
2aa3a 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43  c;.          sNC
2aa3b 2e 70 4e 65 78 74 20 3d 20 70 4e 43 3b 0a 20 20  .pNext = pNC;.  
2aa3c 20 20 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72          sNC.pPar
2aa3d 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
2aa3e 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70  ;.          zTyp
2aa3f 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
2aa40 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69 6e  sNC, p, &zOrigin
2aa41 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62 2c  Db, &zOriginTab,
2aa42 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20 0a   &zOriginCol); .
2aa43 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2aa44 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53  }else if( ALWAYS
2aa45 28 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 20  (pTab->pSchema) 
2aa46 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20  ){.        /* A 
2aa47 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  real table */.  
2aa48 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
2aa49 53 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  S );.        if(
2aa4a 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d   iCol<0 ) iCol =
2aa4b 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20   pTab->iPKey;.  
2aa4c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 43        assert( iC
2aa4d 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e  ol==-1 || (iCol>
2aa4e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d  =0 && iCol<pTab-
2aa4f 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20 20  >nCol) );.      
2aa50 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a    if( iCol<0 ){.
2aa51 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 20            zType 
2aa52 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20  = "INTEGER";.   
2aa53 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43 6f         zOriginCo
2aa54 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20  l = "rowid";.   
2aa55 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2aa56 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54        zType = pT
2aa57 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
2aa58 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Type;.          
2aa59 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 70 54 61  zOriginCol = pTa
2aa5a 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
2aa5b 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ame;.        }. 
2aa5c 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 54 61         zOriginTa
2aa5d 62 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  b = pTab->zName;
2aa5e 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 43  .        if( pNC
2aa5f 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 20 20  ->pParse ){.    
2aa60 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20        int iDb = 
2aa61 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2aa62 6e 64 65 78 28 70 4e 43 2d 3e 70 50 61 72 73 65  ndex(pNC->pParse
2aa63 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
2aa64 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ema);.          
2aa65 7a 4f 72 69 67 69 6e 44 62 20 3d 20 70 4e 43 2d  zOriginDb = pNC-
2aa66 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62  >pParse->db->aDb
2aa67 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  [iDb].zName;.   
2aa68 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2aa69 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2aa6a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
2aa6b 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
2aa6c 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43     case TK_SELEC
2aa6d 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  T: {.      /* Th
2aa6e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
2aa6f 61 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 52 65  a sub-select. Re
2aa70 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61  turn the declara
2aa71 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 0a 20 20  tion type and.  
2aa72 20 20 20 20 2a 2a 20 6f 72 69 67 69 6e 20 69 6e      ** origin in
2aa73 66 6f 20 66 6f 72 20 74 68 65 20 73 69 6e 67 6c  fo for the singl
2aa74 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  e column in the 
2aa75 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
2aa76 65 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a  e SELECT.      *
2aa77 2a 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  * statement..   
2aa78 20 20 20 2a 2f 0a 20 20 20 20 20 20 4e 61 6d 65     */.      Name
2aa79 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20  Context sNC;.   
2aa7a 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d 20     Select *pS = 
2aa7b 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
2aa7c 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 20  ;.      Expr *p 
2aa7d 3d 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  = pS->pEList->a[
2aa7e 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  0].pExpr;.      
2aa7f 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50  assert( ExprHasP
2aa80 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2aa81 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
2aa82 20 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69        sNC.pSrcLi
2aa83 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a 20  st = pS->pSrc;. 
2aa84 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d       sNC.pNext =
2aa85 20 70 4e 43 3b 0a 20 20 20 20 20 20 73 4e 43 2e   pNC;.      sNC.
2aa86 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
2aa87 61 72 73 65 3b 0a 20 20 20 20 20 20 7a 54 79 70  arse;.      zTyp
2aa88 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
2aa89 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69 6e  sNC, p, &zOrigin
2aa8a 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62 2c  Db, &zOriginTab,
2aa8b 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20 0a   &zOriginCol); .
2aa8c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2aa8d 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20   }.#endif.  }.  
2aa8e 0a 20 20 69 66 28 20 70 7a 4f 72 69 67 69 6e 44  .  if( pzOriginD
2aa8f 62 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  b ){.    assert(
2aa90 20 70 7a 4f 72 69 67 69 6e 54 61 62 20 26 26 20   pzOriginTab && 
2aa91 70 7a 4f 72 69 67 69 6e 43 6f 6c 20 29 3b 0a 20  pzOriginCol );. 
2aa92 20 20 20 2a 70 7a 4f 72 69 67 69 6e 44 62 20 3d     *pzOriginDb =
2aa93 20 7a 4f 72 69 67 69 6e 44 62 3b 0a 20 20 20 20   zOriginDb;.    
2aa94 2a 70 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 7a  *pzOriginTab = z
2aa95 4f 72 69 67 69 6e 54 61 62 3b 0a 20 20 20 20 2a  OriginTab;.    *
2aa96 70 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 7a 4f  pzOriginCol = zO
2aa97 72 69 67 69 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20  riginCol;.  }.  
2aa98 72 65 74 75 72 6e 20 7a 54 79 70 65 3b 0a 7d 0a  return zType;.}.
2aa99 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
2aa9a 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 74  code that will t
2aa9b 65 6c 6c 20 74 68 65 20 56 44 42 45 20 74 68 65  ell the VDBE the
2aa9c 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
2aa9d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  es of columns.**
2aa9e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
2aa9f 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  et..*/.static vo
2aaa0 69 64 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d  id generateColum
2aaa1 6e 54 79 70 65 73 28 0a 20 20 50 61 72 73 65 20  nTypes(.  Parse 
2aaa2 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
2aaa3 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   Parser context 
2aaa4 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
2aaa5 61 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74  abList,  /* List
2aaa6 20 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20   of tables */.  
2aaa7 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2aaa8 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
2aaa9 6e 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20  ns defining the 
2aaaa 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 29 7b  result set */.){
2aaab 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2aaac 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a 20 20  OMIT_DECLTYPE.  
2aaad 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
2aaae 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
2aaaf 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ;.  NameContext 
2aab0 73 4e 43 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c  sNC;.  sNC.pSrcL
2aab1 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a  ist = pTabList;.
2aab2 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
2aab3 50 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30  Parse;.  for(i=0
2aab4 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ; i<pEList->nExp
2aab5 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70  r; i++){.    Exp
2aab6 72 20 2a 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61  r *p = pEList->a
2aab7 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 63  [i].pExpr;.    c
2aab8 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65  onst char *zType
2aab9 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
2aaba 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
2aabb 54 41 44 41 54 41 0a 20 20 20 20 63 6f 6e 73 74  TADATA.    const
2aabc 20 63 68 61 72 20 2a 7a 4f 72 69 67 44 62 20 3d   char *zOrigDb =
2aabd 20 30 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   0;.    const ch
2aabe 61 72 20 2a 7a 4f 72 69 67 54 61 62 20 3d 20 30  ar *zOrigTab = 0
2aabf 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
2aac0 20 2a 7a 4f 72 69 67 43 6f 6c 20 3d 20 30 3b 0a   *zOrigCol = 0;.
2aac1 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75      zType = colu
2aac2 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
2aac3 26 7a 4f 72 69 67 44 62 2c 20 26 7a 4f 72 69 67  &zOrigDb, &zOrig
2aac4 54 61 62 2c 20 26 7a 4f 72 69 67 43 6f 6c 29 3b  Tab, &zOrigCol);
2aac5 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 76 64 62  ..    /* The vdb
2aac6 65 20 6d 75 73 74 20 6d 61 6b 65 20 69 74 73 20  e must make its 
2aac7 6f 77 6e 20 63 6f 70 79 20 6f 66 20 74 68 65 20  own copy of the 
2aac8 63 6f 6c 75 6d 6e 2d 74 79 70 65 20 61 6e 64 20  column-type and 
2aac9 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 63 6f  other .    ** co
2aaca 6c 75 6d 6e 20 73 70 65 63 69 66 69 63 20 73 74  lumn specific st
2aacb 72 69 6e 67 73 2c 20 69 6e 20 63 61 73 65 20 74  rings, in case t
2aacc 68 65 20 73 63 68 65 6d 61 20 69 73 20 72 65 73  he schema is res
2aacd 65 74 20 62 65 66 6f 72 65 20 74 68 69 73 0a 20  et before this. 
2aace 20 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61     ** virtual ma
2aacf 63 68 69 6e 65 20 69 73 20 64 65 6c 65 74 65 64  chine is deleted
2aad0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
2aad1 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2aad2 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
2aad3 5f 44 41 54 41 42 41 53 45 2c 20 7a 4f 72 69 67  _DATABASE, zOrig
2aad4 44 62 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  Db, SQLITE_TRANS
2aad5 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74  IENT);.    sqlit
2aad6 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2aad7 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 54  (v, i, COLNAME_T
2aad8 41 42 4c 45 2c 20 7a 4f 72 69 67 54 61 62 2c 20  ABLE, zOrigTab, 
2aad9 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
2aada 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2aadb 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2aadc 69 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d  i, COLNAME_COLUM
2aadd 4e 2c 20 7a 4f 72 69 67 43 6f 6c 2c 20 53 51 4c  N, zOrigCol, SQL
2aade 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
2aadf 23 65 6c 73 65 0a 20 20 20 20 7a 54 79 70 65 20  #else.    zType 
2aae0 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e  = columnType(&sN
2aae1 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  C, p, 0, 0, 0);.
2aae2 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74  #endif.    sqlit
2aae3 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2aae4 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44  (v, i, COLNAME_D
2aae5 45 43 4c 54 59 50 45 2c 20 7a 54 79 70 65 2c 20  ECLTYPE, zType, 
2aae6 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
2aae7 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  );.  }.#endif /*
2aae8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43   SQLITE_OMIT_DEC
2aae9 4c 54 59 50 45 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a  LTYPE */.}../*.*
2aaea 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2aaeb 74 68 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74  that will tell t
2aaec 68 65 20 56 44 42 45 20 74 68 65 20 6e 61 6d 65  he VDBE the name
2aaed 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  s of columns.** 
2aaee 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
2aaef 74 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61  t.  This informa
2aaf0 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
2aaf1 70 72 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61  provide the.** a
2aaf2 7a 43 6f 6c 5b 5d 20 76 61 6c 75 65 73 20 69 6e  zCol[] values in
2aaf3 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a   the callback..*
2aaf4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65  /.static void ge
2aaf5 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65  nerateColumnName
2aaf6 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  s(.  Parse *pPar
2aaf7 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73  se,      /* Pars
2aaf8 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
2aaf9 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
2aafa 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74  t,  /* List of t
2aafb 61 62 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c  ables */.  ExprL
2aafc 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f  ist *pEList    /
2aafd 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65  * Expressions de
2aafe 66 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c  fining the resul
2aaff 74 20 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 56 64  t set */.){.  Vd
2ab00 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2ab01 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 2c 20  pVdbe;.  int i, 
2ab02 6a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  j;.  sqlite3 *db
2ab03 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2ab04 20 69 6e 74 20 66 75 6c 6c 4e 61 6d 65 73 2c 20   int fullNames, 
2ab05 73 68 6f 72 74 4e 61 6d 65 73 3b 0a 0a 23 69 66  shortNames;..#if
2ab06 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2ab07 5f 45 58 50 4c 41 49 4e 0a 20 20 2f 2a 20 49 66  _EXPLAIN.  /* If
2ab08 20 74 68 69 73 20 69 73 20 61 6e 20 45 58 50 4c   this is an EXPL
2ab09 41 49 4e 2c 20 73 6b 69 70 20 74 68 69 73 20 73  AIN, skip this s
2ab0a 74 65 70 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  tep */.  if( pPa
2ab0b 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a  rse->explain ){.
2ab0c 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2ab0d 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 50  #endif..  if( pP
2ab0e 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65  arse->colNamesSe
2ab0f 74 20 7c 7c 20 4e 45 56 45 52 28 76 3d 3d 30 29  t || NEVER(v==0)
2ab10 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
2ab11 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  iled ) return;. 
2ab12 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65   pParse->colName
2ab13 73 53 65 74 20 3d 20 31 3b 0a 20 20 66 75 6c 6c  sSet = 1;.  full
2ab14 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66 6c 61  Names = (db->fla
2ab15 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75 6c 6c  gs & SQLITE_Full
2ab16 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a 20 20  ColNames)!=0;.  
2ab17 73 68 6f 72 74 4e 61 6d 65 73 20 3d 20 28 64 62  shortNames = (db
2ab18 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2ab19 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 29 21  _ShortColNames)!
2ab1a 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  =0;.  sqlite3Vdb
2ab1b 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 70  eSetNumCols(v, p
2ab1c 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20  EList->nExpr);. 
2ab1d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69   for(i=0; i<pELi
2ab1e 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
2ab1f 0a 20 20 20 20 45 78 70 72 20 2a 70 3b 0a 20 20  .    Expr *p;.  
2ab20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b    p = pEList->a[
2ab21 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66  i].pExpr;.    if
2ab22 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20 29 20  ( NEVER(p==0) ) 
2ab23 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66  continue;.    if
2ab24 28 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  ( pEList->a[i].z
2ab25 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 63 68  Name ){.      ch
2ab26 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 45 4c 69  ar *zName = pELi
2ab27 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a  st->a[i].zName;.
2ab28 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2ab29 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
2ab2a 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2ab2b 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 54 52  zName, SQLITE_TR
2ab2c 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d 65  ANSIENT);.    }e
2ab2d 6c 73 65 20 69 66 28 20 28 70 2d 3e 6f 70 3d 3d  lse if( (p->op==
2ab2e 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 2d 3e  TK_COLUMN || p->
2ab2f 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  op==TK_AGG_COLUM
2ab30 4e 29 20 26 26 20 70 54 61 62 4c 69 73 74 20 29  N) && pTabList )
2ab31 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  {.      Table *p
2ab32 54 61 62 3b 0a 20 20 20 20 20 20 63 68 61 72 20  Tab;.      char 
2ab33 2a 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 69 6e 74  *zCol;.      int
2ab34 20 69 43 6f 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75   iCol = p->iColu
2ab35 6d 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  mn;.      for(j=
2ab36 30 3b 20 41 4c 57 41 59 53 28 6a 3c 70 54 61 62  0; ALWAYS(j<pTab
2ab37 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 20 6a 2b 2b  List->nSrc); j++
2ab38 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
2ab39 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43  TabList->a[j].iC
2ab3a 75 72 73 6f 72 3d 3d 70 2d 3e 69 54 61 62 6c 65  ursor==p->iTable
2ab3b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2ab3c 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
2ab3d 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  j<pTabList->nSrc
2ab3e 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d   );.      pTab =
2ab3f 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e   pTabList->a[j].
2ab40 70 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20  pTab;.      if( 
2ab41 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20  iCol<0 ) iCol = 
2ab42 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20  pTab->iPKey;.   
2ab43 20 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3d     assert( iCol=
2ab44 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e 3d 30 20  =-1 || (iCol>=0 
2ab45 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  && iCol<pTab->nC
2ab46 6f 6c 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ol) );.      if(
2ab47 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20   iCol<0 ){.     
2ab48 20 20 20 7a 43 6f 6c 20 3d 20 22 72 6f 77 69 64     zCol = "rowid
2ab49 22 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  ";.      }else{.
2ab4a 20 20 20 20 20 20 20 20 7a 43 6f 6c 20 3d 20 70          zCol = p
2ab4b 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
2ab4c 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a 20  zName;.      }. 
2ab4d 20 20 20 20 20 69 66 28 20 21 73 68 6f 72 74 4e       if( !shortN
2ab4e 61 6d 65 73 20 26 26 20 21 66 75 6c 6c 4e 61 6d  ames && !fullNam
2ab4f 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  es ){.        sq
2ab50 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2ab51 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d  ame(v, i, COLNAM
2ab52 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20  E_NAME, .       
2ab53 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 53 74       sqlite3DbSt
2ab54 72 44 75 70 28 64 62 2c 20 70 45 4c 69 73 74 2d  rDup(db, pEList-
2ab55 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29 2c 20 53 51  >a[i].zSpan), SQ
2ab56 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
2ab57 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66       }else if( f
2ab58 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20  ullNames ){.    
2ab59 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20      char *zName 
2ab5a 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61  = 0;.        zNa
2ab5b 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
2ab5c 6e 74 66 28 64 62 2c 20 22 25 73 2e 25 73 22 2c  ntf(db, "%s.%s",
2ab5d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43   pTab->zName, zC
2ab5e 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ol);.        sql
2ab5f 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2ab60 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
2ab61 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 53 51  _NAME, zName, SQ
2ab62 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
2ab63 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2ab64 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2ab65 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20  etColName(v, i, 
2ab66 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 43  COLNAME_NAME, zC
2ab67 6f 6c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ol, SQLITE_TRANS
2ab68 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20  IENT);.      }. 
2ab69 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2ab6a 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2ab6b 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e  lName(v, i, COLN
2ab6c 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20  AME_NAME, .     
2ab6d 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 53 74       sqlite3DbSt
2ab6e 72 44 75 70 28 64 62 2c 20 70 45 4c 69 73 74 2d  rDup(db, pEList-
2ab6f 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29 2c 20 53 51  >a[i].zSpan), SQ
2ab70 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
2ab71 20 20 20 7d 0a 20 20 7d 0a 20 20 67 65 6e 65 72     }.  }.  gener
2ab72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 70  ateColumnTypes(p
2ab73 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
2ab74 20 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a 23 69 66   pEList);.}..#if
2ab75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2ab76 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
2ab77 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74  ./*.** Name of t
2ab78 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70  he connection op
2ab79 65 72 61 74 6f 72 2c 20 75 73 65 64 20 66 6f 72  erator, used for
2ab7a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e   error messages.
2ab7b 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
2ab7c 20 63 68 61 72 20 2a 73 65 6c 65 63 74 4f 70 4e   char *selectOpN
2ab7d 61 6d 65 28 69 6e 74 20 69 64 29 7b 0a 20 20 63  ame(int id){.  c
2ab7e 68 61 72 20 2a 7a 3b 0a 20 20 73 77 69 74 63 68  har *z;.  switch
2ab7f 28 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65  ( id ){.    case
2ab80 20 54 4b 5f 41 4c 4c 3a 20 20 20 20 20 20 20 7a   TK_ALL:       z
2ab81 20 3d 20 22 55 4e 49 4f 4e 20 41 4c 4c 22 3b 20   = "UNION ALL"; 
2ab82 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
2ab83 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a 20  e TK_INTERSECT: 
2ab84 7a 20 3d 20 22 49 4e 54 45 52 53 45 43 54 22 3b  z = "INTERSECT";
2ab85 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
2ab86 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 20 20 20  se TK_EXCEPT:   
2ab87 20 7a 20 3d 20 22 45 58 43 45 50 54 22 3b 20 20   z = "EXCEPT";  
2ab88 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
2ab89 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20  efault:         
2ab8a 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 22 3b 20 20    z = "UNION";  
2ab8b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a       break;.  }.
2ab8c 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65    return z;.}.#e
2ab8d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2ab8e 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
2ab8f 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 69  ECT */../*.** Gi
2ab90 76 65 6e 20 61 20 61 6e 20 65 78 70 72 65 73 73  ven a an express
2ab91 69 6f 6e 20 6c 69 73 74 20 28 77 68 69 63 68 20  ion list (which 
2ab92 69 73 20 72 65 61 6c 6c 79 20 74 68 65 20 6c 69  is really the li
2ab93 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  st of expression
2ab94 73 0a 2a 2a 20 74 68 61 74 20 66 6f 72 6d 20 74  s.** that form t
2ab95 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
2ab96 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2ab97 65 6e 74 29 20 63 6f 6d 70 75 74 65 20 61 70 70  ent) compute app
2ab98 72 6f 70 72 69 61 74 65 0a 2a 2a 20 63 6f 6c 75  ropriate.** colu
2ab99 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 61 20 74  mn names for a t
2ab9a 61 62 6c 65 20 74 68 61 74 20 77 6f 75 6c 64 20  able that would 
2ab9b 68 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73  hold the express
2ab9c 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ion list..**.** 
2ab9d 41 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  All column names
2ab9e 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 2e   will be unique.
2ab9f 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20  .**.** Only the 
2aba0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 72 65  column names are
2aba1 20 63 6f 6d 70 75 74 65 64 2e 20 20 43 6f 6c 75   computed.  Colu
2aba2 6d 6e 2e 7a 54 79 70 65 2c 20 43 6f 6c 75 6d 6e  mn.zType, Column
2aba3 2e 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61 6e 64 20 6f  .zColl,.** and o
2aba4 74 68 65 72 20 66 69 65 6c 64 73 20 6f 66 20 43  ther fields of C
2aba5 6f 6c 75 6d 6e 20 61 72 65 20 7a 65 72 6f 65 64  olumn are zeroed
2aba6 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
2aba7 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
2aba8 65 73 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72  ess.  If a memor
2aba9 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
2abaa 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 73 74  or occurs,.** st
2abab 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 2a 70 61 43  ore NULL in *paC
2abac 6f 6c 20 61 6e 64 20 30 20 69 6e 20 2a 70 6e 43  ol and 0 in *pnC
2abad 6f 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51  ol and return SQ
2abae 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73  LITE_NOMEM..*/.s
2abaf 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74  tatic int select
2abb0 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c  ColumnsFromExprL
2abb1 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ist(.  Parse *pP
2abb2 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
2abb3 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
2abb4 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
2abb5 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f  *pEList,       /
2abb6 2a 20 45 78 70 72 20 6c 69 73 74 20 66 72 6f 6d  * Expr list from
2abb7 20 77 68 69 63 68 20 74 6f 20 64 65 72 69 76 65   which to derive
2abb8 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f   column names */
2abb9 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 2c 20 20  .  int *pnCol,  
2abba 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
2abbb 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
2abbc 66 20 63 6f 6c 75 6d 6e 73 20 68 65 72 65 20 2a  f columns here *
2abbd 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 2a 70 61 43  /.  Column **paC
2abbe 6f 6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  ol          /* W
2abbf 72 69 74 65 20 74 68 65 20 6e 65 77 20 63 6f 6c  rite the new col
2abc0 75 6d 6e 20 6c 69 73 74 20 68 65 72 65 20 2a 2f  umn list here */
2abc1 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
2abc2 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2abc3 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
2abc4 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
2abc5 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
2abc6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2abc7 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
2abc8 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20  int cnt;        
2abc9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2abca 6e 64 65 78 20 61 64 64 65 64 20 74 6f 20 6d 61  ndex added to ma
2abcb 6b 65 20 74 68 65 20 6e 61 6d 65 20 75 6e 69 71  ke the name uniq
2abcc 75 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a  ue */.  Column *
2abcd 61 43 6f 6c 2c 20 2a 70 43 6f 6c 3b 20 20 20 20  aCol, *pCol;    
2abce 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
2abcf 6e 67 20 6f 76 65 72 20 72 65 73 75 6c 74 20 63  ng over result c
2abd0 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  olumns */.  int 
2abd1 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  nCol;           
2abd2 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2abd3 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
2abd4 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
2abd5 2f 0a 20 20 45 78 70 72 20 2a 70 3b 20 20 20 20  /.  Expr *p;    
2abd6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2abd7 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 66 6f  /* Expression fo
2abd8 72 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75 6c  r a single resul
2abd9 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68  t column */.  ch
2abda 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
2abdb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
2abdc 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e  umn name */.  in
2abdd 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  t nName;        
2abde 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
2abdf 65 20 6f 66 20 6e 61 6d 65 20 69 6e 20 7a 4e 61  e of name in zNa
2abe0 6d 65 5b 5d 20 2a 2f 0a 0a 20 20 2a 70 6e 43 6f  me[] */..  *pnCo
2abe1 6c 20 3d 20 6e 43 6f 6c 20 3d 20 70 45 4c 69 73  l = nCol = pELis
2abe2 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 43 6f 6c  t->nExpr;.  aCol
2abe3 20 3d 20 2a 70 61 43 6f 6c 20 3d 20 73 71 6c 69   = *paCol = sqli
2abe4 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2abe5 64 62 2c 20 73 69 7a 65 6f 66 28 61 43 6f 6c 5b  db, sizeof(aCol[
2abe6 30 5d 29 2a 6e 43 6f 6c 29 3b 0a 20 20 69 66 28  0])*nCol);.  if(
2abe7 20 61 43 6f 6c 3d 3d 30 20 29 20 72 65 74 75 72   aCol==0 ) retur
2abe8 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
2abe9 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d    for(i=0, pCol=
2abea 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  aCol; i<nCol; i+
2abeb 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  +, pCol++){.    
2abec 2f 2a 20 47 65 74 20 61 6e 20 61 70 70 72 6f 70  /* Get an approp
2abed 72 69 61 74 65 20 6e 61 6d 65 20 66 6f 72 20 74  riate name for t
2abee 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f  he column.    */
2abef 0a 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d  .    p = pEList-
2abf0 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
2abf1 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67   assert( p->pRig
2abf2 68 74 3d 3d 30 20 7c 7c 20 45 78 70 72 48 61 73  ht==0 || ExprHas
2abf3 50 72 6f 70 65 72 74 79 28 70 2d 3e 70 52 69 67  Property(p->pRig
2abf4 68 74 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  ht, EP_IntValue)
2abf5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2abf6 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e 75 2e  || p->pRight->u.
2abf7 7a 54 6f 6b 65 6e 3d 3d 30 20 7c 7c 20 70 2d 3e  zToken==0 || p->
2abf8 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e  pRight->u.zToken
2abf9 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 20 20 69 66  [0]!=0 );.    if
2abfa 28 20 28 7a 4e 61 6d 65 20 3d 20 70 45 4c 69 73  ( (zName = pELis
2abfb 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 21 3d  t->a[i].zName)!=
2abfc 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  0 ){.      /* If
2abfd 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74   the column cont
2abfe 61 69 6e 73 20 61 6e 20 22 41 53 20 3c 6e 61 6d  ains an "AS <nam
2abff 65 3e 22 20 70 68 72 61 73 65 2c 20 75 73 65 20  e>" phrase, use 
2ac00 3c 6e 61 6d 65 3e 20 61 73 20 74 68 65 20 6e 61  <name> as the na
2ac01 6d 65 20 2a 2f 0a 20 20 20 20 20 20 7a 4e 61 6d  me */.      zNam
2ac02 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
2ac03 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  Dup(db, zName);.
2ac04 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2ac05 20 45 78 70 72 20 2a 70 43 6f 6c 45 78 70 72 20   Expr *pColExpr 
2ac06 3d 20 70 3b 20 20 2f 2a 20 54 68 65 20 65 78 70  = p;  /* The exp
2ac07 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20  ression that is 
2ac08 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  the result colum
2ac09 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20  n name */.      
2ac0a 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
2ac0b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 61 73       /* Table as
2ac0c 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
2ac0d 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  is expression */
2ac0e 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 43  .      while( pC
2ac0f 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 44  olExpr->op==TK_D
2ac10 4f 54 20 29 20 70 43 6f 6c 45 78 70 72 20 3d 20  OT ) pColExpr = 
2ac11 70 43 6f 6c 45 78 70 72 2d 3e 70 52 69 67 68 74  pColExpr->pRight
2ac12 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c  ;.      if( pCol
2ac13 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Expr->op==TK_COL
2ac14 55 4d 4e 20 26 26 20 41 4c 57 41 59 53 28 70 43  UMN && ALWAYS(pC
2ac15 6f 6c 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 29  olExpr->pTab!=0)
2ac16 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46   ){.        /* F
2ac17 6f 72 20 63 6f 6c 75 6d 6e 73 20 75 73 65 20 74  or columns use t
2ac18 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 6e  he column name n
2ac19 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ame */.        i
2ac1a 6e 74 20 69 43 6f 6c 20 3d 20 70 43 6f 6c 45 78  nt iCol = pColEx
2ac1b 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  pr->iColumn;.   
2ac1c 20 20 20 20 20 70 54 61 62 20 3d 20 70 43 6f 6c       pTab = pCol
2ac1d 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Expr->pTab;.    
2ac1e 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29      if( iCol<0 )
2ac1f 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50   iCol = pTab->iP
2ac20 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61  Key;.        zNa
2ac21 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
2ac22 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 0a 20 20  ntf(db, "%s",.  
2ac23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
2ac24 43 6f 6c 3e 3d 30 20 3f 20 70 54 61 62 2d 3e 61  Col>=0 ? pTab->a
2ac25 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 20  Col[iCol].zName 
2ac26 3a 20 22 72 6f 77 69 64 22 29 3b 0a 20 20 20 20  : "rowid");.    
2ac27 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 6f 6c    }else if( pCol
2ac28 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20  Expr->op==TK_ID 
2ac29 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
2ac2a 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
2ac2b 72 74 79 28 70 43 6f 6c 45 78 70 72 2c 20 45 50  rty(pColExpr, EP
2ac2c 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20  _IntValue) );.  
2ac2d 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71        zName = sq
2ac2e 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2ac2f 20 22 25 73 22 2c 20 70 43 6f 6c 45 78 70 72 2d   "%s", pColExpr-
2ac30 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  >u.zToken);.    
2ac31 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2ac32 20 2f 2a 20 55 73 65 20 74 68 65 20 6f 72 69 67   /* Use the orig
2ac33 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65  inal text of the
2ac34 20 63 6f 6c 75 6d 6e 20 65 78 70 72 65 73 73 69   column expressi
2ac35 6f 6e 20 61 73 20 69 74 73 20 6e 61 6d 65 20 2a  on as its name *
2ac36 2f 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20  /.        zName 
2ac37 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2ac38 28 64 62 2c 20 22 25 73 22 2c 20 70 45 4c 69 73  (db, "%s", pELis
2ac39 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29 3b 0a  t->a[i].zSpan);.
2ac3a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2ac3b 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2ac3c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
2ac3d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2ac3e 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
2ac3f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
2ac40 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
2ac41 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69  he column name i
2ac42 73 20 75 6e 69 71 75 65 2e 20 20 49 66 20 74 68  s unique.  If th
2ac43 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 75 6e  e name is not un
2ac44 69 71 75 65 2c 0a 20 20 20 20 2a 2a 20 61 70 70  ique,.    ** app
2ac45 65 6e 64 20 61 20 69 6e 74 65 67 65 72 20 74 6f  end a integer to
2ac46 20 74 68 65 20 6e 61 6d 65 20 73 6f 20 74 68 61   the name so tha
2ac47 74 20 69 74 20 62 65 63 6f 6d 65 73 20 75 6e 69  t it becomes uni
2ac48 71 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  que..    */.    
2ac49 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
2ac4a 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a  trlen30(zName);.
2ac4b 20 20 20 20 66 6f 72 28 6a 3d 63 6e 74 3d 30 3b      for(j=cnt=0;
2ac4c 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20   j<i; j++){.    
2ac4d 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2ac4e 49 43 6d 70 28 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61  ICmp(aCol[j].zNa
2ac4f 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  me, zName)==0 ){
2ac50 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
2ac51 4e 65 77 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  NewName;.       
2ac52 20 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20   zName[nName] = 
2ac53 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 65 77 4e  0;.        zNewN
2ac54 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
2ac55 69 6e 74 66 28 64 62 2c 20 22 25 73 3a 25 64 22  intf(db, "%s:%d"
2ac56 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29 3b  , zName, ++cnt);
2ac57 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2ac58 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65  DbFree(db, zName
2ac59 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65  );.        zName
2ac5a 20 3d 20 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20   = zNewName;.   
2ac5b 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20 20       j = -1;.   
2ac5c 20 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d       if( zName==
2ac5d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
2ac5e 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 6f   }.    }.    pCo
2ac5f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65  l->zName = zName
2ac60 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e  ;.  }.  if( db->
2ac61 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2ac62 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 69      for(j=0; j<i
2ac63 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; j++){.      sq
2ac64 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2ac65 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3b 0a  aCol[j].zName);.
2ac66 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2ac67 33 44 62 46 72 65 65 28 64 62 2c 20 61 43 6f 6c  3DbFree(db, aCol
2ac68 29 3b 0a 20 20 20 20 2a 70 61 43 6f 6c 20 3d 20  );.    *paCol = 
2ac69 30 3b 0a 20 20 20 20 2a 70 6e 43 6f 6c 20 3d 20  0;.    *pnCol = 
2ac6a 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
2ac6b 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
2ac6c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2ac6d 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  OK;.}../*.** Add
2ac6e 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74   type and collat
2ac6f 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ion information 
2ac70 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 6c 69 73 74  to a column list
2ac71 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 61 20 53   based on.** a S
2ac72 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
2ac73 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 63 6f 6c 75  .** .** The colu
2ac74 6d 6e 20 6c 69 73 74 20 70 72 65 73 75 6d 61 62  mn list presumab
2ac75 6c 79 20 63 61 6d 65 20 66 72 6f 6d 20 73 65 6c  ly came from sel
2ac76 65 63 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 46 72  ectColumnNamesFr
2ac77 6f 6d 45 78 70 72 4c 69 73 74 28 29 2e 0a 2a 2a  omExprList()..**
2ac78 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74   The column list
2ac79 20 68 61 73 20 6f 6e 6c 79 20 6e 61 6d 65 73 2c   has only names,
2ac7a 20 6e 6f 74 20 74 79 70 65 73 20 6f 72 20 63 6f   not types or co
2ac7b 6c 6c 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 0a  llations.  This.
2ac7c 2a 2a 20 72 6f 75 74 69 6e 65 20 67 6f 65 73 20  ** routine goes 
2ac7d 74 68 72 6f 75 67 68 20 61 6e 64 20 61 64 64 73  through and adds
2ac7e 20 74 68 65 20 74 79 70 65 73 20 61 6e 64 20 63   the types and c
2ac7f 6f 6c 6c 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  ollations..**.**
2ac80 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
2ac81 71 75 69 72 65 73 20 74 68 61 74 20 61 6c 6c 20  quires that all 
2ac82 69 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20 74  identifiers in t
2ac83 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61  he SELECT.** sta
2ac84 74 65 6d 65 6e 74 20 62 65 20 72 65 73 6f 6c 76  tement be resolv
2ac85 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
2ac86 69 64 20 73 65 6c 65 63 74 41 64 64 43 6f 6c 75  id selectAddColu
2ac87 6d 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74 69  mnTypeAndCollati
2ac88 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  on(.  Parse *pPa
2ac89 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
2ac8a 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 73 20  arsing contexts 
2ac8b 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20 20  */.  int nCol,  
2ac8c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2ac8d 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2ac8e 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f  */.  Column *aCo
2ac8f 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  l,         /* Li
2ac90 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f  st of columns */
2ac91 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
2ac92 63 74 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45  ct       /* SELE
2ac93 43 54 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  CT used to deter
2ac94 6d 69 6e 65 20 74 79 70 65 73 20 61 6e 64 20 63  mine types and c
2ac95 6f 6c 6c 61 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a  ollations */.){.
2ac96 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2ac97 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 4e 61  pParse->db;.  Na
2ac98 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20  meContext sNC;. 
2ac99 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
2ac9a 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
2ac9b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 78 70 72  .  int i;.  Expr
2ac9c 20 2a 70 3b 0a 20 20 73 74 72 75 63 74 20 45 78   *p;.  struct Ex
2ac9d 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 3b 0a  prList_item *a;.
2ac9e 0a 20 20 61 73 73 65 72 74 28 20 70 53 65 6c 65  .  assert( pSele
2ac9f 63 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ct!=0 );.  asser
2aca0 74 28 20 28 70 53 65 6c 65 63 74 2d 3e 73 65 6c  t( (pSelect->sel
2aca1 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c  Flags & SF_Resol
2aca2 76 65 64 29 21 3d 30 20 29 3b 0a 20 20 61 73 73  ved)!=0 );.  ass
2aca3 65 72 74 28 20 6e 43 6f 6c 3d 3d 70 53 65 6c 65  ert( nCol==pSele
2aca4 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ct->pEList->nExp
2aca5 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
2aca6 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 64  ailed );.  if( d
2aca7 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2aca8 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73  ) return;.  mems
2aca9 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65  et(&sNC, 0, size
2acaa 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e  of(sNC));.  sNC.
2acab 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65  pSrcList = pSele
2acac 63 74 2d 3e 70 53 72 63 3b 0a 20 20 61 20 3d 20  ct->pSrc;.  a = 
2acad 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d  pSelect->pEList-
2acae 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  >a;.  for(i=0, p
2acaf 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c  Col=aCol; i<nCol
2acb0 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; i++, pCol++){.
2acb1 20 20 20 20 70 20 3d 20 61 5b 69 5d 2e 70 45 78      p = a[i].pEx
2acb2 70 72 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 54  pr;.    pCol->zT
2acb3 79 70 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ype = sqlite3DbS
2acb4 74 72 44 75 70 28 64 62 2c 20 63 6f 6c 75 6d 6e  trDup(db, column
2acb5 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 30 2c  Type(&sNC, p, 0,
2acb6 20 30 2c 20 30 29 29 3b 0a 20 20 20 20 70 43 6f   0, 0));.    pCo
2acb7 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73 71  l->affinity = sq
2acb8 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
2acb9 79 28 70 29 3b 0a 20 20 20 20 69 66 28 20 70 43  y(p);.    if( pC
2acba 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 30 20  ol->affinity==0 
2acbb 29 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  ) pCol->affinity
2acbc 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f   = SQLITE_AFF_NO
2acbd 4e 45 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20  NE;.    pColl = 
2acbe 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
2acbf 65 71 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20  eq(pParse, p);. 
2acc0 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a     if( pColl ){.
2acc1 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c        pCol->zCol
2acc2 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  l = sqlite3DbStr
2acc3 44 75 70 28 64 62 2c 20 70 43 6f 6c 6c 2d 3e 7a  Dup(db, pColl->z
2acc4 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Name);.    }.  }
2acc5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
2acc6 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
2acc7 6e 74 2c 20 67 65 6e 65 72 61 74 65 20 61 20 54  nt, generate a T
2acc8 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74  able structure t
2acc9 68 61 74 20 64 65 73 63 72 69 62 65 73 0a 2a 2a  hat describes.**
2acca 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2accb 6f 66 20 74 68 61 74 20 53 45 4c 45 43 54 2e 0a  of that SELECT..
2accc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2accd 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
2acce 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63  ResultSetOfSelec
2accf 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
2acd0 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
2acd1 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  ){.  Table *pTab
2acd2 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2acd3 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2acd4 69 6e 74 20 73 61 76 65 64 46 6c 61 67 73 3b 0a  int savedFlags;.
2acd5 0a 20 20 73 61 76 65 64 46 6c 61 67 73 20 3d 20  .  savedFlags = 
2acd6 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 64 62 2d  db->flags;.  db-
2acd7 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54  >flags &= ~SQLIT
2acd8 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 3b 0a  E_FullColNames;.
2acd9 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
2acda 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61  QLITE_ShortColNa
2acdb 6d 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  mes;.  sqlite3Se
2acdc 6c 65 63 74 50 72 65 70 28 70 50 61 72 73 65 2c  lectPrep(pParse,
2acdd 20 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20   pSelect, 0);.  
2acde 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
2acdf 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77   ) return 0;.  w
2ace0 68 69 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e 70  hile( pSelect->p
2ace1 50 72 69 6f 72 20 29 20 70 53 65 6c 65 63 74 20  Prior ) pSelect 
2ace2 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  = pSelect->pPrio
2ace3 72 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d  r;.  db->flags =
2ace4 20 73 61 76 65 64 46 6c 61 67 73 3b 0a 20 20 70   savedFlags;.  p
2ace5 54 61 62 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  Tab = sqlite3DbM
2ace6 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
2ace7 7a 65 6f 66 28 54 61 62 6c 65 29 20 29 3b 0a 20  zeof(Table) );. 
2ace8 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a   if( pTab==0 ){.
2ace9 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2acea 7d 0a 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74  }.  /* The sqlit
2aceb 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
2acec 65 63 74 28 29 20 69 73 20 6f 6e 6c 79 20 75 73  ect() is only us
2aced 65 64 20 6e 20 63 6f 6e 74 65 78 74 73 20 77 68  ed n contexts wh
2acee 65 72 65 20 6c 6f 6f 6b 61 73 69 64 65 0a 20 20  ere lookaside.  
2acef 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20  ** is disabled, 
2acf0 73 6f 20 77 65 20 6d 69 67 68 74 20 61 73 20 77  so we might as w
2acf1 65 6c 6c 20 68 61 72 64 2d 63 6f 64 65 20 70 54  ell hard-code pT
2acf2 61 62 2d 3e 64 62 4d 65 6d 20 74 6f 20 4e 55 4c  ab->dbMem to NUL
2acf3 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  L. */.  assert( 
2acf4 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
2acf5 6e 61 62 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 70  nabled==0 );.  p
2acf6 54 61 62 2d 3e 64 62 4d 65 6d 20 3d 20 30 3b 0a  Tab->dbMem = 0;.
2acf7 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31    pTab->nRef = 1
2acf8 3b 0a 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20  ;.  pTab->zName 
2acf9 3d 20 30 3b 0a 20 20 73 65 6c 65 63 74 43 6f 6c  = 0;.  selectCol
2acfa 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69 73 74  umnsFromExprList
2acfb 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
2acfc 2d 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d  ->pEList, &pTab-
2acfd 3e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43  >nCol, &pTab->aC
2acfe 6f 6c 29 3b 0a 20 20 73 65 6c 65 63 74 41 64 64  ol);.  selectAdd
2acff 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c  ColumnTypeAndCol
2ad00 6c 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  lation(pParse, p
2ad01 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54 61 62 2d  Tab->nCol, pTab-
2ad02 3e 61 43 6f 6c 2c 20 70 53 65 6c 65 63 74 29 3b  >aCol, pSelect);
2ad03 0a 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d  .  pTab->iPKey =
2ad04 20 2d 31 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d   -1;.  if( db->m
2ad05 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2ad06 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
2ad07 54 61 62 6c 65 28 70 54 61 62 29 3b 0a 20 20 20  Table(pTab);.   
2ad08 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2ad09 20 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a   return pTab;.}.
2ad0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 56 44 42  ./*.** Get a VDB
2ad0b 45 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20  E for the given 
2ad0c 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20  parser context. 
2ad0d 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6f 6e   Create a new on
2ad0e 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a  e if necessary..
2ad0f 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
2ad10 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55  ccurs, return NU
2ad11 4c 4c 20 61 6e 64 20 6c 65 61 76 65 20 61 20 6d  LL and leave a m
2ad12 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
2ad13 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2ad14 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65  ATE Vdbe *sqlite
2ad15 33 47 65 74 56 64 62 65 28 50 61 72 73 65 20 2a  3GetVdbe(Parse *
2ad16 70 50 61 72 73 65 29 7b 0a 20 20 56 64 62 65 20  pParse){.  Vdbe 
2ad17 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2ad18 62 65 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  be;.  if( v==0 )
2ad19 7b 0a 20 20 20 20 76 20 3d 20 70 50 61 72 73 65  {.    v = pParse
2ad1a 2d 3e 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65  ->pVdbe = sqlite
2ad1b 33 56 64 62 65 43 72 65 61 74 65 28 70 50 61 72  3VdbeCreate(pPar
2ad1c 73 65 2d 3e 64 62 29 3b 0a 23 69 66 6e 64 65 66  se->db);.#ifndef
2ad1d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
2ad1e 43 45 0a 20 20 20 20 69 66 28 20 76 20 29 7b 0a  CE.    if( v ){.
2ad1f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2ad20 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 54 72  eAddOp0(v, OP_Tr
2ad21 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ace);.    }.#end
2ad22 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  if.  }.  return 
2ad23 76 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  v;.}.../*.** Com
2ad24 70 75 74 65 20 74 68 65 20 69 4c 69 6d 69 74 20  pute the iLimit 
2ad25 61 6e 64 20 69 4f 66 66 73 65 74 20 66 69 65 6c  and iOffset fiel
2ad26 64 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ds of the SELECT
2ad27 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a   based on the.**
2ad28 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66 66   pLimit and pOff
2ad29 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e  set expressions.
2ad2a 20 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66    pLimit and pOf
2ad2b 66 73 65 74 20 68 6f 6c 64 20 74 68 65 20 65 78  fset hold the ex
2ad2c 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61  pressions.** tha
2ad2d 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20  t appear in the 
2ad2e 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61  original SQL sta
2ad2f 74 65 6d 65 6e 74 20 61 66 74 65 72 20 74 68 65  tement after the
2ad30 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
2ad31 54 0a 2a 2a 20 6b 65 79 77 6f 72 64 73 2e 20 20  T.** keywords.  
2ad32 4f 72 20 4e 55 4c 4c 20 69 66 20 74 68 6f 73 65  Or NULL if those
2ad33 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 6f 6d   keywords are om
2ad34 69 74 74 65 64 2e 20 69 4c 69 6d 69 74 20 61 6e  itted. iLimit an
2ad35 64 20 69 4f 66 66 73 65 74 20 0a 2a 2a 20 61 72  d iOffset .** ar
2ad36 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 6d 65  e the integer me
2ad37 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20 6e 75  mory register nu
2ad38 6d 62 65 72 73 20 66 6f 72 20 63 6f 75 6e 74 65  mbers for counte
2ad39 72 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75  rs used to compu
2ad3a 74 65 20 0a 2a 2a 20 74 68 65 20 6c 69 6d 69 74  te .** the limit
2ad3b 20 61 6e 64 20 6f 66 66 73 65 74 2e 20 20 49 66   and offset.  If
2ad3c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6c 69 6d   there is no lim
2ad3d 69 74 20 61 6e 64 2f 6f 72 20 6f 66 66 73 65 74  it and/or offset
2ad3e 2c 20 74 68 65 6e 20 0a 2a 2a 20 69 4c 69 6d 69  , then .** iLimi
2ad3f 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 61 72  t and iOffset ar
2ad40 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a  e negative..**.*
2ad41 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
2ad42 68 61 6e 67 65 73 20 74 68 65 20 76 61 6c 75 65  hanges the value
2ad43 73 20 6f 66 20 69 4c 69 6d 69 74 20 61 6e 64 20  s of iLimit and 
2ad44 69 4f 66 66 73 65 74 20 6f 6e 6c 79 20 69 66 0a  iOffset only if.
2ad45 2a 2a 20 61 20 6c 69 6d 69 74 20 6f 72 20 6f 66  ** a limit or of
2ad46 66 73 65 74 20 69 73 20 64 65 66 69 6e 65 64 20  fset is defined 
2ad47 62 79 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f  by pLimit and pO
2ad48 66 66 73 65 74 2e 20 20 69 4c 69 6d 69 74 20 61  ffset.  iLimit a
2ad49 6e 64 0a 2a 2a 20 69 4f 66 66 73 65 74 20 73 68  nd.** iOffset sh
2ad4a 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 70  ould have been p
2ad4b 72 65 73 65 74 20 74 6f 20 61 70 70 72 6f 70 72  reset to appropr
2ad4c 69 61 74 65 20 64 65 66 61 75 6c 74 20 76 61 6c  iate default val
2ad4d 75 65 73 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20  ues.** (usually 
2ad4e 62 75 74 20 6e 6f 74 20 61 6c 77 61 79 73 20 2d  but not always -
2ad4f 31 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c  1) prior to call
2ad50 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
2ad51 2e 0a 2a 2a 20 4f 6e 6c 79 20 69 66 20 70 4c 69  ..** Only if pLi
2ad52 6d 69 74 21 3d 30 20 6f 72 20 70 4f 66 66 73 65  mit!=0 or pOffse
2ad53 74 21 3d 30 20 64 6f 20 74 68 65 20 6c 69 6d 69  t!=0 do the limi
2ad54 74 20 72 65 67 69 73 74 65 72 73 20 67 65 74 0a  t registers get.
2ad55 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20 54  ** redefined.  T
2ad56 68 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65  he UNION ALL ope
2ad57 72 61 74 6f 72 20 75 73 65 73 20 74 68 69 73 20  rator uses this 
2ad58 70 72 6f 70 65 72 74 79 20 74 6f 20 66 6f 72 63  property to forc
2ad59 65 0a 2a 2a 20 74 68 65 20 72 65 75 73 65 20 6f  e.** the reuse o
2ad5a 66 20 74 68 65 20 73 61 6d 65 20 6c 69 6d 69 74  f the same limit
2ad5b 20 61 6e 64 20 6f 66 66 73 65 74 20 72 65 67 69   and offset regi
2ad5c 73 74 65 72 73 20 61 63 72 6f 73 73 20 6d 75 6c  sters across mul
2ad5d 74 69 70 6c 65 0a 2a 2a 20 53 45 4c 45 43 54 20  tiple.** SELECT 
2ad5e 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  statements..*/.s
2ad5f 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75  tatic void compu
2ad60 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73  teLimitRegisters
2ad61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2ad62 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69  Select *p, int i
2ad63 42 72 65 61 6b 29 7b 0a 20 20 56 64 62 65 20 2a  Break){.  Vdbe *
2ad64 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4c 69  v = 0;.  int iLi
2ad65 6d 69 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  mit = 0;.  int i
2ad66 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 61 64  Offset;.  int ad
2ad67 64 72 31 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c  dr1;.  if( p->iL
2ad68 69 6d 69 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a  imit ) return;..
2ad69 20 20 2f 2a 20 0a 20 20 2a 2a 20 22 4c 49 4d 49    /* .  ** "LIMI
2ad6a 54 20 2d 31 22 20 61 6c 77 61 79 73 20 73 68 6f  T -1" always sho
2ad6b 77 73 20 61 6c 6c 20 72 6f 77 73 2e 20 20 54 68  ws all rows.  Th
2ad6c 65 72 65 20 69 73 20 73 6f 6d 65 0a 20 20 2a 2a  ere is some.  **
2ad6d 20 63 6f 6e 74 72 61 76 65 72 73 79 20 61 62 6f   contraversy abo
2ad6e 75 74 20 77 68 61 74 20 74 68 65 20 63 6f 72 72  ut what the corr
2ad6f 65 63 74 20 62 65 68 61 76 69 6f 72 20 73 68 6f  ect behavior sho
2ad70 75 6c 64 20 62 65 2e 0a 20 20 2a 2a 20 54 68 65  uld be..  ** The
2ad71 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65   current impleme
2ad72 6e 74 61 74 69 6f 6e 20 69 6e 74 65 72 70 72 65  ntation interpre
2ad73 74 73 20 22 4c 49 4d 49 54 20 30 22 20 74 6f 20  ts "LIMIT 0" to 
2ad74 6d 65 61 6e 0a 20 20 2a 2a 20 6e 6f 20 72 6f 77  mean.  ** no row
2ad75 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  s..  */.  sqlite
2ad76 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
2ad77 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72  pParse);.  asser
2ad78 74 28 20 70 2d 3e 70 4f 66 66 73 65 74 3d 3d 30  t( p->pOffset==0
2ad79 20 7c 7c 20 70 2d 3e 70 4c 69 6d 69 74 21 3d 30   || p->pLimit!=0
2ad7a 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69   );.  if( p->pLi
2ad7b 6d 69 74 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4c  mit ){.    p->iL
2ad7c 69 6d 69 74 20 3d 20 69 4c 69 6d 69 74 20 3d 20  imit = iLimit = 
2ad7d 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2ad7e 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
2ad7f 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2ad80 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d      if( NEVER(v=
2ad81 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 20 20 2f  =0) ) return;  /
2ad82 2a 20 56 44 42 45 20 73 68 6f 75 6c 64 20 68 61  * VDBE should ha
2ad83 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
2ad84 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 20  allocated */.   
2ad85 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2ad86 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4c 69 6d  (pParse, p->pLim
2ad87 69 74 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  it, iLimit);.   
2ad88 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ad89 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
2ad8a 6e 74 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  nt, iLimit);.   
2ad8b 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2ad8c 20 22 4c 49 4d 49 54 20 63 6f 75 6e 74 65 72 22   "LIMIT counter"
2ad8d 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ));.    sqlite3V
2ad8e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2ad8f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 2c 20  IfZero, iLimit, 
2ad90 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 69 66 28  iBreak);.    if(
2ad91 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 7b 0a 20   p->pOffset ){. 
2ad92 20 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20       p->iOffset 
2ad93 3d 20 69 4f 66 66 73 65 74 20 3d 20 2b 2b 70 50  = iOffset = ++pP
2ad94 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
2ad95 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b    pParse->nMem++
2ad96 3b 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ;   /* Allocate 
2ad97 61 6e 20 65 78 74 72 61 20 72 65 67 69 73 74 65  an extra registe
2ad98 72 20 66 6f 72 20 6c 69 6d 69 74 2b 6f 66 66 73  r for limit+offs
2ad99 65 74 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  et */.      sqli
2ad9a 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
2ad9b 73 65 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20  se, p->pOffset, 
2ad9c 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20  iOffset);.      
2ad9d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ad9e 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e  1(v, OP_MustBeIn
2ad9f 74 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 20  t, iOffset);.   
2ada0 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2ada1 76 2c 20 22 4f 46 46 53 45 54 20 63 6f 75 6e 74  v, "OFFSET count
2ada2 65 72 22 29 29 3b 0a 20 20 20 20 20 20 61 64 64  er"));.      add
2ada3 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
2ada4 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50  AddOp1(v, OP_IfP
2ada5 6f 73 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20  os, iOffset);.  
2ada6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2ada7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2ada8 67 65 72 2c 20 30 2c 20 69 4f 66 66 73 65 74 29  ger, 0, iOffset)
2ada9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2adaa 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
2adab 64 64 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ddr1);.      sql
2adac 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2adad 2c 20 4f 50 5f 41 64 64 2c 20 69 4c 69 6d 69 74  , OP_Add, iLimit
2adae 2c 20 69 4f 66 66 73 65 74 2c 20 69 4f 66 66 73  , iOffset, iOffs
2adaf 65 74 2b 31 29 3b 0a 20 20 20 20 20 20 56 64 62  et+1);.      Vdb
2adb0 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49  eComment((v, "LI
2adb1 4d 49 54 2b 4f 46 46 53 45 54 22 29 29 3b 0a 20  MIT+OFFSET"));. 
2adb2 20 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c       addr1 = sql
2adb3 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2adb4 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4c 69 6d  , OP_IfPos, iLim
2adb5 69 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  it);.      sqlit
2adb6 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2adb7 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20  OP_Integer, -1, 
2adb8 69 4f 66 66 73 65 74 2b 31 29 3b 0a 20 20 20 20  iOffset+1);.    
2adb9 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2adba 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
2adbb 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69  .    }.  }.}..#i
2adbc 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2adbd 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
2adbe 54 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  T./*.** Return t
2adbf 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 63  he appropriate c
2adc0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2adc1 65 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74  e for the iCol-t
2adc2 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74  h column of.** t
2adc3 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f  he result set fo
2adc4 72 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 2d 73  r the compound-s
2adc5 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
2adc6 22 70 22 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c  "p".  Return NUL
2adc7 4c 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75  L if.** the colu
2adc8 6d 6e 20 68 61 73 20 6e 6f 20 64 65 66 61 75 6c  mn has no defaul
2adc9 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  t collating sequ
2adca 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ence..**.** The 
2adcb 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2adcc 63 65 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f  ce for the compo
2adcd 75 6e 64 20 73 65 6c 65 63 74 20 69 73 20 74 61  und select is ta
2adce 6b 65 6e 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  ken from the.** 
2adcf 6c 65 66 74 2d 6d 6f 73 74 20 74 65 72 6d 20 6f  left-most term o
2add0 66 20 74 68 65 20 73 65 6c 65 63 74 20 74 68 61  f the select tha
2add1 74 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e  t has a collatin
2add2 67 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2f 0a 73  g sequence..*/.s
2add3 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 6d  tatic CollSeq *m
2add4 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65  ultiSelectCollSe
2add5 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  q(Parse *pParse,
2add6 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20   Select *p, int 
2add7 69 43 6f 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  iCol){.  CollSeq
2add8 20 2a 70 52 65 74 3b 0a 20 20 69 66 28 20 70 2d   *pRet;.  if( p-
2add9 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 70  >pPrior ){.    p
2adda 52 65 74 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63  Ret = multiSelec
2addb 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  tCollSeq(pParse,
2addc 20 70 2d 3e 70 50 72 69 6f 72 2c 20 69 43 6f 6c   p->pPrior, iCol
2addd 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2adde 70 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pRet = 0;.  }.  
2addf 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20  assert( iCol>=0 
2ade0 29 3b 0a 20 20 69 66 28 20 70 52 65 74 3d 3d 30  );.  if( pRet==0
2ade1 20 26 26 20 69 43 6f 6c 3c 70 2d 3e 70 45 4c 69   && iCol<p->pELi
2ade2 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
2ade3 20 70 52 65 74 20 3d 20 73 71 6c 69 74 65 33 45   pRet = sqlite3E
2ade4 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
2ade5 65 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  e, p->pEList->a[
2ade6 69 43 6f 6c 5d 2e 70 45 78 70 72 29 3b 0a 20 20  iCol].pExpr);.  
2ade7 7d 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b  }.  return pRet;
2ade8 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
2ade9 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e  ITE_OMIT_COMPOUN
2adea 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 20  D_SELECT */../* 
2adeb 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63  Forward referenc
2adec 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  e */.static int 
2aded 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72  multiSelectOrder
2adee 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  By(.  Parse *pPa
2adef 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
2adf0 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2adf1 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20  /.  Select *p,  
2adf2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2adf3 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53   right-most of S
2adf4 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64  ELECTs to be cod
2adf5 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  ed */.  SelectDe
2adf6 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 2f 2a  st *pDest     /*
2adf7 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68   What to do with
2adf8 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a   query results *
2adf9 2f 0a 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 53  /.);...#ifndef S
2adfa 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
2adfb 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a  UND_SELECT./*.**
2adfc 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2adfd 20 63 61 6c 6c 65 64 20 74 6f 20 70 72 6f 63 65   called to proce
2adfe 73 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75  ss a compound qu
2adff 65 72 79 20 66 6f 72 6d 20 66 72 6f 6d 0a 2a 2a  ery form from.**
2ae00 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 65 70   two or more sep
2ae01 61 72 61 74 65 20 71 75 65 72 69 65 73 20 75 73  arate queries us
2ae02 69 6e 67 20 55 4e 49 4f 4e 2c 20 55 4e 49 4f 4e  ing UNION, UNION
2ae03 20 41 4c 4c 2c 20 45 58 43 45 50 54 2c 20 6f 72   ALL, EXCEPT, or
2ae04 0a 2a 2a 20 49 4e 54 45 52 53 45 43 54 0a 2a 2a  .** INTERSECT.**
2ae05 0a 2a 2a 20 22 70 22 20 70 6f 69 6e 74 73 20 74  .** "p" points t
2ae06 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  o the right-most
2ae07 20 6f 66 20 74 68 65 20 74 77 6f 20 71 75 65 72   of the two quer
2ae08 69 65 73 2e 20 20 74 68 65 20 71 75 65 72 79 20  ies.  the query 
2ae09 6f 6e 20 74 68 65 0a 2a 2a 20 6c 65 66 74 20 69  on the.** left i
2ae0a 73 20 70 2d 3e 70 50 72 69 6f 72 2e 20 20 54 68  s p->pPrior.  Th
2ae0b 65 20 6c 65 66 74 20 71 75 65 72 79 20 63 6f 75  e left query cou
2ae0c 6c 64 20 61 6c 73 6f 20 62 65 20 61 20 63 6f 6d  ld also be a com
2ae0d 70 6f 75 6e 64 20 71 75 65 72 79 0a 2a 2a 20 69  pound query.** i
2ae0e 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 69  n which case thi
2ae0f 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 62  s routine will b
2ae10 65 20 63 61 6c 6c 65 64 20 72 65 63 75 72 73 69  e called recursi
2ae11 76 65 6c 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  vely. .**.** The
2ae12 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
2ae13 74 6f 74 61 6c 20 71 75 65 72 79 20 61 72 65 20  total query are 
2ae14 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e  to be written in
2ae15 74 6f 20 61 20 64 65 73 74 69 6e 61 74 69 6f 6e  to a destination
2ae16 0a 2a 2a 20 6f 66 20 74 79 70 65 20 65 44 65 73  .** of type eDes
2ae17 74 20 77 69 74 68 20 70 61 72 61 6d 65 74 65 72  t with parameter
2ae18 20 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 45 78   iParm..**.** Ex
2ae19 61 6d 70 6c 65 20 31 3a 20 20 43 6f 6e 73 69 64  ample 1:  Consid
2ae1a 65 72 20 61 20 74 68 72 65 65 2d 77 61 79 20 63  er a three-way c
2ae1b 6f 6d 70 6f 75 6e 64 20 53 51 4c 20 73 74 61 74  ompound SQL stat
2ae1c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ement..**.**    
2ae1d 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 74   SELECT a FROM t
2ae1e 31 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20 62  1 UNION SELECT b
2ae1f 20 46 52 4f 4d 20 74 32 20 55 4e 49 4f 4e 20 53   FROM t2 UNION S
2ae20 45 4c 45 43 54 20 63 20 46 52 4f 4d 20 74 33 0a  ELECT c FROM t3.
2ae21 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 61 74 65  **.** This state
2ae22 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 20 75  ment is parsed u
2ae23 70 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  p as follows:.**
2ae24 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 63  .**     SELECT c
2ae25 20 46 52 4f 4d 20 74 33 0a 2a 2a 20 20 20 20 20   FROM t3.**     
2ae26 20 7c 0a 2a 2a 20 20 20 20 20 20 60 2d 2d 2d 2d   |.**      `----
2ae27 2d 3e 20 20 53 45 4c 45 43 54 20 62 20 46 52 4f  ->  SELECT b FRO
2ae28 4d 20 74 32 0a 2a 2a 20 20 20 20 20 20 20 20 20  M t2.**         
2ae29 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20         |.**     
2ae2a 20 20 20 20 20 20 20 20 20 20 20 60 2d 2d 2d 2d             `----
2ae2b 2d 2d 3e 20 20 53 45 4c 45 43 54 20 61 20 46 52  -->  SELECT a FR
2ae2c 4f 4d 20 74 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20  OM t1.**.** The 
2ae2d 61 72 72 6f 77 73 20 69 6e 20 74 68 65 20 64 69  arrows in the di
2ae2e 61 67 72 61 6d 20 61 62 6f 76 65 20 72 65 70 72  agram above repr
2ae2f 65 73 65 6e 74 20 74 68 65 20 53 65 6c 65 63 74  esent the Select
2ae30 2e 70 50 72 69 6f 72 20 70 6f 69 6e 74 65 72 2e  .pPrior pointer.
2ae31 0a 2a 2a 20 53 6f 20 69 66 20 74 68 69 73 20 72  .** So if this r
2ae32 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
2ae33 20 77 69 74 68 20 70 20 65 71 75 61 6c 20 74 6f   with p equal to
2ae34 20 74 68 65 20 74 33 20 71 75 65 72 79 2c 20 74   the t3 query, t
2ae35 68 65 6e 0a 2a 2a 20 70 50 72 69 6f 72 20 77 69  hen.** pPrior wi
2ae36 6c 6c 20 62 65 20 74 68 65 20 74 32 20 71 75 65  ll be the t2 que
2ae37 72 79 2e 20 20 70 2d 3e 6f 70 20 77 69 6c 6c 20  ry.  p->op will 
2ae38 62 65 20 54 4b 5f 55 4e 49 4f 4e 20 69 6e 20 74  be TK_UNION in t
2ae39 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
2ae3a 4e 6f 74 69 63 65 20 74 68 61 74 20 62 65 63 61  Notice that beca
2ae3b 75 73 65 20 6f 66 20 74 68 65 20 77 61 79 20 53  use of the way S
2ae3c 51 4c 69 74 65 20 70 61 72 73 65 73 20 63 6f 6d  QLite parses com
2ae3d 70 6f 75 6e 64 20 53 45 4c 45 43 54 73 2c 20 74  pound SELECTs, t
2ae3e 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c  he.** individual
2ae3f 20 73 65 6c 65 63 74 73 20 61 6c 77 61 79 73 20   selects always 
2ae40 67 72 6f 75 70 20 66 72 6f 6d 20 6c 65 66 74 20  group from left 
2ae41 74 6f 20 72 69 67 68 74 2e 0a 2a 2f 0a 73 74 61  to right..*/.sta
2ae42 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c  tic int multiSel
2ae43 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ect(.  Parse *pP
2ae44 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
2ae45 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2ae46 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
2ae47 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ae48 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20  e right-most of 
2ae49 53 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f  SELECTs to be co
2ae4a 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  ded */.  SelectD
2ae4b 65 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 2f  est *pDest     /
2ae4c 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74  * What to do wit
2ae4d 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20  h query results 
2ae4e 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
2ae4f 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a   SQLITE_OK;   /*
2ae50 20 53 75 63 63 65 73 73 20 63 6f 64 65 20 66 72   Success code fr
2ae51 6f 6d 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  om a subroutine 
2ae52 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72  */.  Select *pPr
2ae53 69 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e  ior;       /* An
2ae54 6f 74 68 65 72 20 53 45 4c 45 43 54 20 69 6d 6d  other SELECT imm
2ae55 65 64 69 61 74 65 6c 79 20 74 6f 20 6f 75 72 20  ediately to our 
2ae56 6c 65 66 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a  left */.  Vdbe *
2ae57 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
2ae58 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2ae59 20 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f   to this VDBE */
2ae5a 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
2ae5b 73 74 3b 20 20 20 20 20 20 2f 2a 20 41 6c 74 65  st;      /* Alte
2ae5c 72 6e 61 74 69 76 65 20 64 61 74 61 20 64 65 73  rnative data des
2ae5d 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 65  tination */.  Se
2ae5e 6c 65 63 74 20 2a 70 44 65 6c 65 74 65 20 3d 20  lect *pDelete = 
2ae5f 30 3b 20 20 2f 2a 20 43 68 61 69 6e 20 6f 66 20  0;  /* Chain of 
2ae60 73 69 6d 70 6c 65 20 73 65 6c 65 63 74 73 20 74  simple selects t
2ae61 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 73 71  o delete */.  sq
2ae62 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
2ae63 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2ae64 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20  connection */.. 
2ae65 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
2ae66 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20  ere is no ORDER 
2ae67 42 59 20 6f 72 20 4c 49 4d 49 54 20 63 6c 61 75  BY or LIMIT clau
2ae68 73 65 20 6f 6e 20 70 72 69 6f 72 20 53 45 4c 45  se on prior SELE
2ae69 43 54 73 2e 20 20 4f 6e 6c 79 0a 20 20 2a 2a 20  CTs.  Only.  ** 
2ae6a 74 68 65 20 6c 61 73 74 20 28 72 69 67 68 74 2d  the last (right-
2ae6b 6d 6f 73 74 29 20 53 45 4c 45 43 54 20 69 6e 20  most) SELECT in 
2ae6c 74 68 65 20 73 65 72 69 65 73 20 6d 61 79 20 68  the series may h
2ae6d 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20  ave an ORDER BY 
2ae6e 6f 72 20 4c 49 4d 49 54 2e 0a 20 20 2a 2f 0a 20  or LIMIT..  */. 
2ae6f 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d   assert( p && p-
2ae70 3e 70 50 72 69 6f 72 20 29 3b 20 20 2f 2a 20 43  >pPrior );  /* C
2ae71 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
2ae72 67 75 61 72 61 6e 74 65 65 73 20 74 68 69 73 20  guarantees this 
2ae73 6d 75 63 68 20 2a 2f 0a 20 20 64 62 20 3d 20 70  much */.  db = p
2ae74 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 72  Parse->db;.  pPr
2ae75 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b  ior = p->pPrior;
2ae76 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f  .  assert( pPrio
2ae77 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 21 3d 70  r->pRightmost!=p
2ae78 50 72 69 6f 72 20 29 3b 0a 20 20 61 73 73 65 72  Prior );.  asser
2ae79 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69 67 68  t( pPrior->pRigh
2ae7a 74 6d 6f 73 74 3d 3d 70 2d 3e 70 52 69 67 68 74  tmost==p->pRight
2ae7b 6d 6f 73 74 20 29 3b 0a 20 20 64 65 73 74 20 3d  most );.  dest =
2ae7c 20 2a 70 44 65 73 74 3b 0a 20 20 69 66 28 20 70   *pDest;.  if( p
2ae7d 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20  Prior->pOrderBy 
2ae7e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2ae7f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 4f  rorMsg(pParse,"O
2ae80 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73  RDER BY clause s
2ae81 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74 65 72  hould come after
2ae82 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65 22 2c   %s not before",
2ae83 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f 70 4e  .      selectOpN
2ae84 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20  ame(p->op));.   
2ae85 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74   rc = 1;.    got
2ae86 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
2ae87 6e 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  nd;.  }.  if( pP
2ae88 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a  rior->pLimit ){.
2ae89 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2ae8a 4d 73 67 28 70 50 61 72 73 65 2c 22 4c 49 4d 49  Msg(pParse,"LIMI
2ae8b 54 20 63 6c 61 75 73 65 20 73 68 6f 75 6c 64 20  T clause should 
2ae8c 63 6f 6d 65 20 61 66 74 65 72 20 25 73 20 6e 6f  come after %s no
2ae8d 74 20 62 65 66 6f 72 65 22 2c 0a 20 20 20 20 20  t before",.     
2ae8e 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d   selectOpName(p-
2ae8f 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  >op));.    rc = 
2ae90 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74  1;.    goto mult
2ae91 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
2ae92 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  }..  v = sqlite3
2ae93 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
2ae94 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
2ae95 29 3b 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20  );  /* The VDBE 
2ae96 61 6c 72 65 61 64 79 20 63 72 65 61 74 65 64 20  already created 
2ae97 62 79 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  by calling funct
2ae98 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 43 72 65  ion */..  /* Cre
2ae99 61 74 65 20 74 68 65 20 64 65 73 74 69 6e 61 74  ate the destinat
2ae9a 69 6f 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ion temporary ta
2ae9b 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72 79  ble if necessary
2ae9c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 74  .  */.  if( dest
2ae9d 2e 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65  .eDest==SRT_Ephe
2ae9e 6d 54 61 62 20 29 7b 0a 20 20 20 20 61 73 73 65  mTab ){.    asse
2ae9f 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b  rt( p->pEList );
2aea0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2aea1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65  AddOp2(v, OP_Ope
2aea2 6e 45 70 68 65 6d 65 72 61 6c 2c 20 64 65 73 74  nEphemeral, dest
2aea3 2e 69 50 61 72 6d 2c 20 70 2d 3e 70 45 4c 69 73  .iParm, p->pELis
2aea4 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 64  t->nExpr);.    d
2aea5 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f  est.eDest = SRT_
2aea6 54 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Table;.  }..  /*
2aea7 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 53   Make sure all S
2aea8 45 4c 45 43 54 73 20 69 6e 20 74 68 65 20 73 74  ELECTs in the st
2aea9 61 74 65 6d 65 6e 74 20 68 61 76 65 20 74 68 65  atement have the
2aeaa 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
2aeab 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 69 6e  elements.  ** in
2aeac 20 74 68 65 69 72 20 72 65 73 75 6c 74 20 73 65   their result se
2aead 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ts..  */.  asser
2aeae 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26 20  t( p->pEList && 
2aeaf 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74 20 29  pPrior->pEList )
2aeb0 3b 0a 20 20 69 66 28 20 70 2d 3e 70 45 4c 69 73  ;.  if( p->pELis
2aeb1 74 2d 3e 6e 45 78 70 72 21 3d 70 50 72 69 6f 72  t->nExpr!=pPrior
2aeb2 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  ->pEList->nExpr 
2aeb3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2aeb4 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2aeb5 53 45 4c 45 43 54 73 20 74 6f 20 74 68 65 20 6c  SELECTs to the l
2aeb6 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 6f 66  eft and right of
2aeb7 20 25 73 22 0a 20 20 20 20 20 20 22 20 64 6f 20   %s".      " do 
2aeb8 6e 6f 74 20 68 61 76 65 20 74 68 65 20 73 61 6d  not have the sam
2aeb9 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75  e number of resu
2aeba 6c 74 20 63 6f 6c 75 6d 6e 73 22 2c 20 73 65 6c  lt columns", sel
2aebb 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29  ectOpName(p->op)
2aebc 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20  );.    rc = 1;. 
2aebd 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65     goto multi_se
2aebe 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20  lect_end;.  }.. 
2aebf 20 2f 2a 20 43 6f 6d 70 6f 75 6e 64 20 53 45 4c   /* Compound SEL
2aec0 45 43 54 73 20 74 68 61 74 20 68 61 76 65 20 61  ECTs that have a
2aec1 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
2aec2 65 20 61 72 65 20 68 61 6e 64 6c 65 64 20 73 65  e are handled se
2aec3 70 61 72 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20  parately..  */. 
2aec4 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79   if( p->pOrderBy
2aec5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d   ){.    return m
2aec6 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42  ultiSelectOrderB
2aec7 79 28 70 50 61 72 73 65 2c 20 70 2c 20 70 44 65  y(pParse, p, pDe
2aec8 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47  st);.  }..  /* G
2aec9 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
2aeca 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69   the left and ri
2aecb 67 68 74 20 53 45 4c 45 43 54 20 73 74 61 74 65  ght SELECT state
2aecc 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 73 77  ments..  */.  sw
2aecd 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20  itch( p->op ){. 
2aece 20 20 20 63 61 73 65 20 54 4b 5f 41 4c 4c 3a 20     case TK_ALL: 
2aecf 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  {.      int addr
2aed0 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65   = 0;.      asse
2aed1 72 74 28 20 21 70 50 72 69 6f 72 2d 3e 70 4c 69  rt( !pPrior->pLi
2aed2 6d 69 74 20 29 3b 0a 20 20 20 20 20 20 70 50 72  mit );.      pPr
2aed3 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 2d  ior->pLimit = p-
2aed4 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70  >pLimit;.      p
2aed5 50 72 69 6f 72 2d 3e 70 4f 66 66 73 65 74 20 3d  Prior->pOffset =
2aed6 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20   p->pOffset;.   
2aed7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
2aed8 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 50  elect(pParse, pP
2aed9 72 69 6f 72 2c 20 26 64 65 73 74 29 3b 0a 20 20  rior, &dest);.  
2aeda 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
2aedb 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66  0;.      p->pOff
2aedc 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  set = 0;.      i
2aedd 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
2aede 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
2aedf 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  ct_end;.      }.
2aee0 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20        p->pPrior 
2aee1 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c  = 0;.      p->iL
2aee2 69 6d 69 74 20 3d 20 70 50 72 69 6f 72 2d 3e 69  imit = pPrior->i
2aee3 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e  Limit;.      p->
2aee4 69 4f 66 66 73 65 74 20 3d 20 70 50 72 69 6f 72  iOffset = pPrior
2aee5 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ->iOffset;.     
2aee6 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29   if( p->iLimit )
2aee7 7b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20 3d  {.        addr =
2aee8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2aee9 70 31 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c  p1(v, OP_IfZero,
2aeea 20 70 2d 3e 69 4c 69 6d 69 74 29 3b 0a 20 20 20   p->iLimit);.   
2aeeb 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2aeec 28 28 76 2c 20 22 4a 75 6d 70 20 61 68 65 61 64  ((v, "Jump ahead
2aeed 20 69 66 20 4c 49 4d 49 54 20 72 65 61 63 68 65   if LIMIT reache
2aeee 64 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  d"));.      }.  
2aeef 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2aef0 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2aef1 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20  , &dest);.      
2aef2 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51  testcase( rc!=SQ
2aef3 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20  LITE_OK );.     
2aef4 20 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e 70 50   pDelete = p->pP
2aef5 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70  rior;.      p->p
2aef6 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a  Prior = pPrior;.
2aef7 20 20 20 20 20 20 69 66 28 20 61 64 64 72 20 29        if( addr )
2aef8 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2aef9 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2aefa 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 7d 0a   addr);.      }.
2aefb 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2aefc 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45   }.    case TK_E
2aefd 58 43 45 50 54 3a 0a 20 20 20 20 63 61 73 65 20  XCEPT:.    case 
2aefe 54 4b 5f 55 4e 49 4f 4e 3a 20 7b 0a 20 20 20 20  TK_UNION: {.    
2aeff 20 20 69 6e 74 20 75 6e 69 6f 6e 54 61 62 3b 20    int unionTab; 
2af00 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
2af01 62 65 72 20 6f 66 20 74 68 65 20 74 65 6d 70 6f  ber of the tempo
2af02 72 61 72 79 20 74 61 62 6c 65 20 68 6f 6c 64 69  rary table holdi
2af03 6e 67 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 20  ng result */.   
2af04 20 20 20 75 38 20 6f 70 20 3d 20 30 3b 20 20 20     u8 op = 0;   
2af05 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
2af06 65 20 53 52 54 5f 20 6f 70 65 72 61 74 69 6f 6e  e SRT_ operation
2af07 73 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 73 65  s to apply to se
2af08 6c 66 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  lf */.      int 
2af09 70 72 69 6f 72 4f 70 3b 20 20 20 20 20 2f 2a 20  priorOp;     /* 
2af0a 54 68 65 20 53 52 54 5f 20 6f 70 65 72 61 74 69  The SRT_ operati
2af0b 6f 6e 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70  on to apply to p
2af0c 72 69 6f 72 20 73 65 6c 65 63 74 73 20 2a 2f 0a  rior selects */.
2af0d 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d        Expr *pLim
2af0e 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b 20 2f 2a  it, *pOffset; /*
2af0f 20 53 61 76 65 64 20 76 61 6c 75 65 73 20 6f 66   Saved values of
2af10 20 70 2d 3e 6e 4c 69 6d 69 74 20 61 6e 64 20 70   p->nLimit and p
2af11 2d 3e 6e 4f 66 66 73 65 74 20 2a 2f 0a 20 20 20  ->nOffset */.   
2af12 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
2af13 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 75 6e     SelectDest un
2af14 69 6f 6e 64 65 73 74 3b 0a 0a 20 20 20 20 20 20  iondest;..      
2af15 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d  testcase( p->op=
2af16 3d 54 4b 5f 45 58 43 45 50 54 20 29 3b 0a 20 20  =TK_EXCEPT );.  
2af17 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
2af18 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 3b  >op==TK_UNION );
2af19 0a 20 20 20 20 20 20 70 72 69 6f 72 4f 70 20 3d  .      priorOp =
2af1a 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a 20 20 20 20   SRT_Union;.    
2af1b 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74    if( dest.eDest
2af1c 3d 3d 70 72 69 6f 72 4f 70 20 26 26 20 41 4c 57  ==priorOp && ALW
2af1d 41 59 53 28 21 70 2d 3e 70 4c 69 6d 69 74 20 26  AYS(!p->pLimit &
2af1e 26 21 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b  &!p->pOffset) ){
2af1f 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 63  .        /* We c
2af20 61 6e 20 72 65 75 73 65 20 61 20 74 65 6d 70 6f  an reuse a tempo
2af21 72 61 72 79 20 74 61 62 6c 65 20 67 65 6e 65 72  rary table gener
2af22 61 74 65 64 20 62 79 20 61 20 53 45 4c 45 43 54  ated by a SELECT
2af23 20 74 6f 20 6f 75 72 0a 20 20 20 20 20 20 20 20   to our.        
2af24 2a 2a 20 72 69 67 68 74 2e 0a 20 20 20 20 20 20  ** right..      
2af25 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73    */.        ass
2af26 65 72 74 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f  ert( p->pRightmo
2af27 73 74 21 3d 70 20 29 3b 20 20 2f 2a 20 43 61 6e  st!=p );  /* Can
2af28 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 66 6f 72   only happen for
2af29 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e   leftward elemen
2af2a 74 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ts.             
2af2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af2c 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20          ** of a 
2af2d 33 2d 77 61 79 20 6f 72 20 6d 6f 72 65 20 63 6f  3-way or more co
2af2e 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20  mpound */.      
2af2f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4c 69    assert( p->pLi
2af30 6d 69 74 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f  mit==0 );      /
2af31 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e  * Not allowed on
2af32 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e   leftward elemen
2af33 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  ts */.        as
2af34 73 65 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74  sert( p->pOffset
2af35 3d 3d 30 20 29 3b 20 20 20 20 20 2f 2a 20 4e 6f  ==0 );     /* No
2af36 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 6c 65 66  t allowed on lef
2af37 74 77 61 72 64 20 65 6c 65 6d 65 6e 74 73 20 2a  tward elements *
2af38 2f 0a 20 20 20 20 20 20 20 20 75 6e 69 6f 6e 54  /.        unionT
2af39 61 62 20 3d 20 64 65 73 74 2e 69 50 61 72 6d 3b  ab = dest.iParm;
2af3a 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2af3b 20 20 20 20 20 20 2f 2a 20 57 65 20 77 69 6c 6c        /* We will
2af3c 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 20   need to create 
2af3d 6f 75 72 20 6f 77 6e 20 74 65 6d 70 6f 72 61 72  our own temporar
2af3e 79 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20  y table to hold 
2af3f 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  the.        ** i
2af40 6e 74 65 72 6d 65 64 69 61 74 65 20 72 65 73 75  ntermediate resu
2af41 6c 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  lts..        */.
2af42 20 20 20 20 20 20 20 20 75 6e 69 6f 6e 54 61 62          unionTab
2af43 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2af44 2b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  +;.        asser
2af45 74 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d  t( p->pOrderBy==
2af46 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 64 64  0 );.        add
2af47 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
2af48 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp2(v, OP_Open
2af49 45 70 68 65 6d 65 72 61 6c 2c 20 75 6e 69 6f 6e  Ephemeral, union
2af4a 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  Tab, 0);.       
2af4b 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 72   assert( p->addr
2af4c 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 3d 20 2d  OpenEphm[0] == -
2af4d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  1 );.        p->
2af4e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20  addrOpenEphm[0] 
2af4f 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 20 20  = addr;.        
2af50 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d 3e 73  p->pRightmost->s
2af51 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 55 73  elFlags |= SF_Us
2af52 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 20  esEphemeral;.   
2af53 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
2af54 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20  pEList );.      
2af55 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65  }..      /* Code
2af56 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
2af57 65 6d 65 6e 74 73 20 74 6f 20 6f 75 72 20 6c 65  ements to our le
2af58 66 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ft.      */.    
2af59 20 20 61 73 73 65 72 74 28 20 21 70 50 72 69 6f    assert( !pPrio
2af5a 72 2d 3e 70 4f 72 64 65 72 42 79 20 29 3b 0a 20  r->pOrderBy );. 
2af5b 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
2af5c 63 74 44 65 73 74 49 6e 69 74 28 26 75 6e 69 6f  ctDestInit(&unio
2af5d 6e 64 65 73 74 2c 20 70 72 69 6f 72 4f 70 2c 20  ndest, priorOp, 
2af5e 75 6e 69 6f 6e 54 61 62 29 3b 0a 20 20 20 20 20  unionTab);.     
2af5f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c   rc = sqlite3Sel
2af60 65 63 74 28 70 50 61 72 73 65 2c 20 70 50 72 69  ect(pParse, pPri
2af61 6f 72 2c 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b  or, &uniondest);
2af62 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
2af63 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  .        goto mu
2af64 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
2af65 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
2af66 2a 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65  * Code the curre
2af67 6e 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  nt SELECT statem
2af68 65 6e 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ent.      */.   
2af69 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b     if( p->op==TK
2af6a 5f 45 58 43 45 50 54 20 29 7b 0a 20 20 20 20 20  _EXCEPT ){.     
2af6b 20 20 20 6f 70 20 3d 20 53 52 54 5f 45 78 63 65     op = SRT_Exce
2af6c 70 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  pt;.      }else{
2af6d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2af6e 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e   p->op==TK_UNION
2af6f 20 29 3b 0a 20 20 20 20 20 20 20 20 6f 70 20 3d   );.        op =
2af70 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a 20 20 20 20   SRT_Union;.    
2af71 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 70 50 72    }.      p->pPr
2af72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ior = 0;.      p
2af73 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
2af74 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  t;.      p->pLim
2af75 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f  it = 0;.      pO
2af76 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73  ffset = p->pOffs
2af77 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66  et;.      p->pOf
2af78 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  fset = 0;.      
2af79 75 6e 69 6f 6e 64 65 73 74 2e 65 44 65 73 74 20  uniondest.eDest 
2af7a 3d 20 6f 70 3b 0a 20 20 20 20 20 20 72 63 20 3d  = op;.      rc =
2af7b 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
2af7c 50 61 72 73 65 2c 20 70 2c 20 26 75 6e 69 6f 6e  Parse, p, &union
2af7d 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73  dest);.      tes
2af7e 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54  tcase( rc!=SQLIT
2af7f 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 2f 2a  E_OK );.      /*
2af80 20 51 75 65 72 79 20 66 6c 61 74 74 65 6e 69 6e   Query flattenin
2af81 67 20 69 6e 20 73 71 6c 69 74 65 33 53 65 6c 65  g in sqlite3Sele
2af82 63 74 28 29 20 6d 69 67 68 74 20 72 65 66 69 6c  ct() might refil
2af83 6c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2e 0a 20  l p->pOrderBy.. 
2af84 20 20 20 20 20 2a 2a 20 42 65 20 73 75 72 65 20       ** Be sure 
2af85 74 6f 20 64 65 6c 65 74 65 20 70 2d 3e 70 4f 72  to delete p->pOr
2af86 64 65 72 42 79 2c 20 74 68 65 72 65 66 6f 72 65  derBy, therefore
2af87 2c 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d  , to avoid a mem
2af88 6f 72 79 20 6c 65 61 6b 2e 20 2a 2f 0a 20 20 20  ory leak. */.   
2af89 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2af8a 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  stDelete(db, p->
2af8b 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 20  pOrderBy);.     
2af8c 20 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e 70 50   pDelete = p->pP
2af8d 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70  rior;.      p->p
2af8e 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a  Prior = pPrior;.
2af8f 20 20 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42        p->pOrderB
2af90 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  y = 0;.      sql
2af91 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2af92 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20  b, p->pLimit);. 
2af93 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d       p->pLimit =
2af94 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70   pLimit;.      p
2af95 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66  ->pOffset = pOff
2af96 73 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c  set;.      p->iL
2af97 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  imit = 0;.      
2af98 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a  p->iOffset = 0;.
2af99 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72  .      /* Conver
2af9a 74 20 74 68 65 20 64 61 74 61 20 69 6e 20 74 68  t the data in th
2af9b 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
2af9c 65 20 69 6e 74 6f 20 77 68 61 74 65 76 65 72 20  e into whatever 
2af9d 66 6f 72 6d 0a 20 20 20 20 20 20 2a 2a 20 69 74  form.      ** it
2af9e 20 69 73 20 74 68 61 74 20 77 65 20 63 75 72 72   is that we curr
2af9f 65 6e 74 6c 79 20 6e 65 65 64 2e 0a 20 20 20 20  ently need..    
2afa0 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72    */.      asser
2afa1 74 28 20 75 6e 69 6f 6e 54 61 62 3d 3d 64 65 73  t( unionTab==des
2afa2 74 2e 69 50 61 72 6d 20 7c 7c 20 64 65 73 74 2e  t.iParm || dest.
2afa3 65 44 65 73 74 21 3d 70 72 69 6f 72 4f 70 20 29  eDest!=priorOp )
2afa4 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65 73 74  ;.      if( dest
2afa5 2e 65 44 65 73 74 21 3d 70 72 69 6f 72 4f 70 20  .eDest!=priorOp 
2afa6 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
2afa7 43 6f 6e 74 2c 20 69 42 72 65 61 6b 2c 20 69 53  Cont, iBreak, iS
2afa8 74 61 72 74 3b 0a 20 20 20 20 20 20 20 20 61 73  tart;.        as
2afa9 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20  sert( p->pEList 
2afaa 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64  );.        if( d
2afab 65 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f  est.eDest==SRT_O
2afac 75 74 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20  utput ){.       
2afad 20 20 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73     Select *pFirs
2afae 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20  t = p;.         
2afaf 20 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d 3e   while( pFirst->
2afb0 70 50 72 69 6f 72 20 29 20 70 46 69 72 73 74 20  pPrior ) pFirst 
2afb1 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  = pFirst->pPrior
2afb2 3b 0a 20 20 20 20 20 20 20 20 20 20 67 65 6e 65  ;.          gene
2afb3 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  rateColumnNames(
2afb4 70 50 61 72 73 65 2c 20 30 2c 20 70 46 69 72 73  pParse, 0, pFirs
2afb5 74 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  t->pEList);.    
2afb6 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 42      }.        iB
2afb7 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64  reak = sqlite3Vd
2afb8 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2afb9 20 20 20 20 20 20 20 20 69 43 6f 6e 74 20 3d 20          iCont = 
2afba 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2afbb 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20  abel(v);.       
2afbc 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67   computeLimitReg
2afbd 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70  isters(pParse, p
2afbe 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  , iBreak);.     
2afbf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2afc0 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
2afc1 64 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 69 42 72  d, unionTab, iBr
2afc2 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 69 53  eak);.        iS
2afc3 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64  tart = sqlite3Vd
2afc4 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2afc5 3b 0a 20 20 20 20 20 20 20 20 73 65 6c 65 63 74  ;.        select
2afc6 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65  InnerLoop(pParse
2afc7 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20  , p, p->pEList, 
2afc8 75 6e 69 6f 6e 54 61 62 2c 20 70 2d 3e 70 45 4c  unionTab, p->pEL
2afc9 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20  ist->nExpr,.    
2afca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2afcb 20 20 20 20 30 2c 20 2d 31 2c 20 26 64 65 73 74      0, -1, &dest
2afcc 2c 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29  , iCont, iBreak)
2afcd 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2afce 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2afcf 6c 28 76 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20  l(v, iCont);.   
2afd0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2afd1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
2afd2 74 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 69 53 74  t, unionTab, iSt
2afd3 61 72 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  art);.        sq
2afd4 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2afd5 4c 61 62 65 6c 28 76 2c 20 69 42 72 65 61 6b 29  Label(v, iBreak)
2afd6 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2afd7 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2afd8 50 5f 43 6c 6f 73 65 2c 20 75 6e 69 6f 6e 54 61  P_Close, unionTa
2afd9 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  b, 0);.      }. 
2afda 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2afdb 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 61  }.    default: a
2afdc 73 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 54 4b  ssert( p->op==TK
2afdd 5f 49 4e 54 45 52 53 45 43 54 20 29 3b 20 7b 0a  _INTERSECT ); {.
2afde 20 20 20 20 20 20 69 6e 74 20 74 61 62 31 2c 20        int tab1, 
2afdf 74 61 62 32 3b 0a 20 20 20 20 20 20 69 6e 74 20  tab2;.      int 
2afe0 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 2c 20 69  iCont, iBreak, i
2afe1 53 74 61 72 74 3b 0a 20 20 20 20 20 20 45 78 70  Start;.      Exp
2afe2 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66  r *pLimit, *pOff
2afe3 73 65 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 61  set;.      int a
2afe4 64 64 72 3b 0a 20 20 20 20 20 20 53 65 6c 65 63  ddr;.      Selec
2afe5 74 44 65 73 74 20 69 6e 74 65 72 73 65 63 74 64  tDest intersectd
2afe6 65 73 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  est;.      int r
2afe7 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 4e 54  1;..      /* INT
2afe8 45 52 53 45 43 54 20 69 73 20 64 69 66 66 65 72  ERSECT is differ
2afe9 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 6f 74 68  ent from the oth
2afea 65 72 73 20 73 69 6e 63 65 20 69 74 20 72 65 71  ers since it req
2afeb 75 69 72 65 73 0a 20 20 20 20 20 20 2a 2a 20 74  uires.      ** t
2afec 77 6f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  wo temporary tab
2afed 6c 65 73 2e 20 20 48 65 6e 63 65 20 69 74 20 68  les.  Hence it h
2afee 61 73 20 69 74 73 20 6f 77 6e 20 63 61 73 65 2e  as its own case.
2afef 20 20 42 65 67 69 6e 0a 20 20 20 20 20 20 2a 2a    Begin.      **
2aff0 20 62 79 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74   by allocating t
2aff1 68 65 20 74 61 62 6c 65 73 20 77 65 20 77 69 6c  he tables we wil
2aff2 6c 20 6e 65 65 64 2e 0a 20 20 20 20 20 20 2a 2f  l need..      */
2aff3 0a 20 20 20 20 20 20 74 61 62 31 20 3d 20 70 50  .      tab1 = pP
2aff4 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
2aff5 20 20 20 20 74 61 62 32 20 3d 20 70 50 61 72 73      tab2 = pPars
2aff6 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20  e->nTab++;.     
2aff7 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64   assert( p->pOrd
2aff8 65 72 42 79 3d 3d 30 20 29 3b 0a 0a 20 20 20 20  erBy==0 );..    
2aff9 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
2affa 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2affb 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
2affc 74 61 62 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  tab1, 0);.      
2affd 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 72 4f  assert( p->addrO
2affe 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 3d 20 2d 31  penEphm[0] == -1
2afff 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64   );.      p->add
2b000 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 61  rOpenEphm[0] = a
2b001 64 64 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 52  ddr;.      p->pR
2b002 69 67 68 74 6d 6f 73 74 2d 3e 73 65 6c 46 6c 61  ightmost->selFla
2b003 67 73 20 7c 3d 20 53 46 5f 55 73 65 73 45 70 68  gs |= SF_UsesEph
2b004 65 6d 65 72 61 6c 3b 0a 20 20 20 20 20 20 61 73  emeral;.      as
2b005 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20  sert( p->pEList 
2b006 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64  );..      /* Cod
2b007 65 20 74 68 65 20 53 45 4c 45 43 54 73 20 74 6f  e the SELECTs to
2b008 20 6f 75 72 20 6c 65 66 74 20 69 6e 74 6f 20 74   our left into t
2b009 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22  emporary table "
2b00a 74 61 62 31 22 2e 0a 20 20 20 20 20 20 2a 2f 0a  tab1"..      */.
2b00b 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
2b00c 65 63 74 44 65 73 74 49 6e 69 74 28 26 69 6e 74  ectDestInit(&int
2b00d 65 72 73 65 63 74 64 65 73 74 2c 20 53 52 54 5f  ersectdest, SRT_
2b00e 55 6e 69 6f 6e 2c 20 74 61 62 31 29 3b 0a 20 20  Union, tab1);.  
2b00f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2b010 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2b011 50 72 69 6f 72 2c 20 26 69 6e 74 65 72 73 65 63  Prior, &intersec
2b012 74 64 65 73 74 29 3b 0a 20 20 20 20 20 20 69 66  tdest);.      if
2b013 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
2b014 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63  goto multi_selec
2b015 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a  t_end;.      }..
2b016 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68        /* Code th
2b017 65 20 63 75 72 72 65 6e 74 20 53 45 4c 45 43 54  e current SELECT
2b018 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79 20   into temporary 
2b019 74 61 62 6c 65 20 22 74 61 62 32 22 0a 20 20 20  table "tab2".   
2b01a 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 64 64 72     */.      addr
2b01b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2b01c 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
2b01d 70 68 65 6d 65 72 61 6c 2c 20 74 61 62 32 2c 20  phemeral, tab2, 
2b01e 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  0);.      assert
2b01f 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  ( p->addrOpenEph
2b020 6d 5b 31 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20  m[1] == -1 );.  
2b021 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45      p->addrOpenE
2b022 70 68 6d 5b 31 5d 20 3d 20 61 64 64 72 3b 0a 20  phm[1] = addr;. 
2b023 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d       p->pPrior =
2b024 20 30 3b 0a 20 20 20 20 20 20 70 4c 69 6d 69 74   0;.      pLimit
2b025 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20   = p->pLimit;.  
2b026 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
2b027 30 3b 0a 20 20 20 20 20 20 70 4f 66 66 73 65 74  0;.      pOffset
2b028 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20   = p->pOffset;. 
2b029 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20       p->pOffset 
2b02a 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 65 72  = 0;.      inter
2b02b 73 65 63 74 64 65 73 74 2e 69 50 61 72 6d 20 3d  sectdest.iParm =
2b02c 20 74 61 62 32 3b 0a 20 20 20 20 20 20 72 63 20   tab2;.      rc 
2b02d 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  = sqlite3Select(
2b02e 70 50 61 72 73 65 2c 20 70 2c 20 26 69 6e 74 65  pParse, p, &inte
2b02f 72 73 65 63 74 64 65 73 74 29 3b 0a 20 20 20 20  rsectdest);.    
2b030 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d    testcase( rc!=
2b031 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20  SQLITE_OK );.   
2b032 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e     pDelete = p->
2b033 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d  pPrior;.      p-
2b034 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72  >pPrior = pPrior
2b035 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2b036 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
2b037 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20  >pLimit);.      
2b038 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d  p->pLimit = pLim
2b039 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66  it;.      p->pOf
2b03a 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a  fset = pOffset;.
2b03b 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  .      /* Genera
2b03c 74 65 20 63 6f 64 65 20 74 6f 20 74 61 6b 65 20  te code to take 
2b03d 74 68 65 20 69 6e 74 65 72 73 65 63 74 69 6f 6e  the intersection
2b03e 20 6f 66 20 74 68 65 20 74 77 6f 20 74 65 6d 70   of the two temp
2b03f 6f 72 61 72 79 0a 20 20 20 20 20 20 2a 2a 20 74  orary.      ** t
2b040 61 62 6c 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a  ables..      */.
2b041 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
2b042 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20  >pEList );.     
2b043 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d   if( dest.eDest=
2b044 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20  =SRT_Output ){. 
2b045 20 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70         Select *p
2b046 46 69 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 20  First = p;.     
2b047 20 20 20 77 68 69 6c 65 28 20 70 46 69 72 73 74     while( pFirst
2b048 2d 3e 70 50 72 69 6f 72 20 29 20 70 46 69 72 73  ->pPrior ) pFirs
2b049 74 20 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69  t = pFirst->pPri
2b04a 6f 72 3b 0a 20 20 20 20 20 20 20 20 67 65 6e 65  or;.        gene
2b04b 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  rateColumnNames(
2b04c 70 50 61 72 73 65 2c 20 30 2c 20 70 46 69 72 73  pParse, 0, pFirs
2b04d 74 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  t->pEList);.    
2b04e 20 20 7d 0a 20 20 20 20 20 20 69 42 72 65 61 6b    }.      iBreak
2b04f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2b050 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
2b051 20 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65    iCont = sqlite
2b052 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
2b053 29 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65  );.      compute
2b054 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
2b055 50 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b  Parse, p, iBreak
2b056 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b057 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b058 5f 52 65 77 69 6e 64 2c 20 74 61 62 31 2c 20 69  _Rewind, tab1, i
2b059 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 72 31  Break);.      r1
2b05a 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2b05b 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2b05c 20 20 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c      iStart = sql
2b05d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b05e 2c 20 4f 50 5f 52 6f 77 4b 65 79 2c 20 74 61 62  , OP_RowKey, tab
2b05f 31 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  1, r1);.      sq
2b060 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2b061 76 2c 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20  v, OP_NotFound, 
2b062 74 61 62 32 2c 20 69 43 6f 6e 74 2c 20 72 31 29  tab2, iCont, r1)
2b063 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
2b064 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2b065 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
2b066 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70   selectInnerLoop
2b067 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70  (pParse, p, p->p
2b068 45 4c 69 73 74 2c 20 74 61 62 31 2c 20 70 2d 3e  EList, tab1, p->
2b069 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20  pEList->nExpr,. 
2b06a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b06b 20 20 20 20 20 30 2c 20 2d 31 2c 20 26 64 65 73       0, -1, &des
2b06c 74 2c 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b  t, iCont, iBreak
2b06d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b06e 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2b06f 28 76 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20  (v, iCont);.    
2b070 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b071 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
2b072 74 61 62 31 2c 20 69 53 74 61 72 74 29 3b 0a 20  tab1, iStart);. 
2b073 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b074 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
2b075 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73  iBreak);.      s
2b076 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b077 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61  (v, OP_Close, ta
2b078 62 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  b2, 0);.      sq
2b079 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b07a 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62  v, OP_Close, tab
2b07b 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65  1, 0);.      bre
2b07c 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ak;.    }.  }.. 
2b07d 20 2f 2a 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c   /* Compute coll
2b07e 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
2b07f 75 73 65 64 20 62 79 20 0a 20 20 2a 2a 20 74 65  used by .  ** te
2b080 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 6e  mporary tables n
2b081 65 65 64 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65  eeded to impleme
2b082 6e 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  nt the compound 
2b083 73 65 6c 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74  select..  ** Att
2b084 61 63 68 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  ach the KeyInfo 
2b085 73 74 72 75 63 74 75 72 65 20 74 6f 20 61 6c 6c  structure to all
2b086 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2b087 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  s..  **.  ** Thi
2b088 73 20 73 65 63 74 69 6f 6e 20 69 73 20 72 75 6e  s section is run
2b089 20 62 79 20 74 68 65 20 72 69 67 68 74 2d 6d 6f   by the right-mo
2b08a 73 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  st SELECT statem
2b08b 65 6e 74 20 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53  ent only..  ** S
2b08c 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
2b08d 20 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6c 77   to the left alw
2b08e 61 79 73 20 73 6b 69 70 20 74 68 69 73 20 70 61  ays skip this pa
2b08f 72 74 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d  rt.  The right-m
2b090 6f 73 74 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20  ost.  ** SELECT 
2b091 6d 69 67 68 74 20 61 6c 73 6f 20 73 6b 69 70 20  might also skip 
2b092 74 68 69 73 20 70 61 72 74 20 69 66 20 69 74 20  this part if it 
2b093 68 61 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20  has no ORDER BY 
2b094 63 6c 61 75 73 65 20 61 6e 64 0a 20 20 2a 2a 20  clause and.  ** 
2b095 6e 6f 20 74 65 6d 70 20 74 61 62 6c 65 73 20 61  no temp tables a
2b096 72 65 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a  re required..  *
2b097 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c  /.  if( p->selFl
2b098 61 67 73 20 26 20 53 46 5f 55 73 65 73 45 70 68  ags & SF_UsesEph
2b099 65 6d 65 72 61 6c 20 29 7b 0a 20 20 20 20 69 6e  emeral ){.    in
2b09a 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
2b09b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2b09c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
2b09d 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
2b09e 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20  Info;           
2b09f 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65   /* Collating se
2b0a0 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 72  quence for the r
2b0a1 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20  esult set */.   
2b0a2 20 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20   Select *pLoop; 
2b0a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b0a4 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68  * For looping th
2b0a5 72 6f 75 67 68 20 53 45 4c 45 43 54 20 73 74 61  rough SELECT sta
2b0a6 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 43  tements */.    C
2b0a7 6f 6c 6c 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b  ollSeq **apColl;
2b0a8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b0a9 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f  For looping thro
2b0aa 75 67 68 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  ugh pKeyInfo->aC
2b0ab 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74  oll[] */.    int
2b0ac 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   nCol;          
2b0ad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2b0ae 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2b0af 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  in result set */
2b0b0 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ..    assert( p-
2b0b1 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70 20 29  >pRightmost==p )
2b0b2 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 2d 3e  ;.    nCol = p->
2b0b3 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20  pEList->nExpr;. 
2b0b4 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71     pKeyInfo = sq
2b0b5 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2b0b6 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20  o(db,.          
2b0b7 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a               siz
2b0b8 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 2b 6e  eof(*pKeyInfo)+n
2b0b9 43 6f 6c 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c  Col*(sizeof(Coll
2b0ba 53 65 71 2a 29 20 2b 20 31 29 29 3b 0a 20 20 20  Seq*) + 1));.   
2b0bb 20 69 66 28 20 21 70 4b 65 79 49 6e 66 6f 20 29   if( !pKeyInfo )
2b0bc 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
2b0bd 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
2b0be 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
2b0bf 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20  ct_end;.    }.. 
2b0c0 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63     pKeyInfo->enc
2b0c1 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20   = ENC(db);.    
2b0c2 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
2b0c3 20 3d 20 28 75 31 36 29 6e 43 6f 6c 3b 0a 0a 20   = (u16)nCol;.. 
2b0c4 20 20 20 66 6f 72 28 69 3d 30 2c 20 61 70 43 6f     for(i=0, apCo
2b0c5 6c 6c 3d 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  ll=pKeyInfo->aCo
2b0c6 6c 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c  ll; i<nCol; i++,
2b0c7 20 61 70 43 6f 6c 6c 2b 2b 29 7b 0a 20 20 20 20   apColl++){.    
2b0c8 20 20 2a 61 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74    *apColl = mult
2b0c9 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70  iSelectCollSeq(p
2b0ca 50 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20  Parse, p, i);.  
2b0cb 20 20 20 20 69 66 28 20 30 3d 3d 2a 61 70 43 6f      if( 0==*apCo
2b0cc 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 61  ll ){.        *a
2b0cd 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c  pColl = db->pDfl
2b0ce 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  tColl;.      }. 
2b0cf 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 70 4c     }..    for(pL
2b0d0 6f 6f 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c  oop=p; pLoop; pL
2b0d1 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f  oop=pLoop->pPrio
2b0d2 72 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  r){.      for(i=
2b0d3 30 3b 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<2; i++){.  
2b0d4 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d        int addr =
2b0d5 20 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e   pLoop->addrOpen
2b0d6 45 70 68 6d 5b 69 5d 3b 0a 20 20 20 20 20 20 20  Ephm[i];.       
2b0d7 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 20   if( addr<0 ){. 
2b0d8 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 5b           /* If [
2b0d9 30 5d 20 69 73 20 75 6e 75 73 65 64 20 74 68 65  0] is unused the
2b0da 6e 20 5b 31 5d 20 69 73 20 61 6c 73 6f 20 75 6e  n [1] is also un
2b0db 75 73 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e  used.  So we can
2b0dc 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6c  .          ** al
2b0dd 77 61 79 73 20 73 61 66 65 6c 79 20 61 62 6f 72  ways safely abor
2b0de 74 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65  t as soon as the
2b0df 20 66 69 72 73 74 20 75 6e 75 73 65 64 20 73 6c   first unused sl
2b0e0 6f 74 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20  ot is found */. 
2b0e1 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2b0e2 20 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e   pLoop->addrOpen
2b0e3 45 70 68 6d 5b 31 5d 3c 30 20 29 3b 0a 20 20 20  Ephm[1]<0 );.   
2b0e4 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b0e5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2b0e6 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2b0e7 65 50 32 28 76 2c 20 61 64 64 72 2c 20 6e 43 6f  eP2(v, addr, nCo
2b0e8 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  l);.        sqli
2b0e9 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
2b0ea 76 2c 20 61 64 64 72 2c 20 28 63 68 61 72 2a 29  v, addr, (char*)
2b0eb 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59  pKeyInfo, P4_KEY
2b0ec 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20 20 20 70  INFO);.        p
2b0ed 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70  Loop->addrOpenEp
2b0ee 68 6d 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20  hm[i] = -1;.    
2b0ef 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
2b0f0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2b0f1 70 4b 65 79 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a  pKeyInfo);.  }..
2b0f2 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
2b0f3 3a 0a 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20  :.  pDest->iMem 
2b0f4 3d 20 64 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 70  = dest.iMem;.  p
2b0f5 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 64 65 73  Dest->nMem = des
2b0f6 74 2e 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65  t.nMem;.  sqlite
2b0f7 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2b0f8 2c 20 70 44 65 6c 65 74 65 29 3b 0a 20 20 72 65  , pDelete);.  re
2b0f9 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
2b0fa 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2b0fb 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
2b0fc 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20   */../*.** Code 
2b0fd 61 6e 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75  an output subrou
2b0fe 74 69 6e 65 20 66 6f 72 20 61 20 63 6f 72 6f 75  tine for a corou
2b0ff 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tine implementat
2b100 69 6f 6e 20 6f 66 20 61 0a 2a 2a 20 53 45 4c 45  ion of a.** SELE
2b101 43 54 20 73 74 61 74 6d 65 6e 74 2e 0a 2a 2a 0a  CT statment..**.
2b102 2a 2a 20 54 68 65 20 64 61 74 61 20 74 6f 20 62  ** The data to b
2b103 65 20 6f 75 74 70 75 74 20 69 73 20 63 6f 6e 74  e output is cont
2b104 61 69 6e 65 64 20 69 6e 20 70 49 6e 2d 3e 69 4d  ained in pIn->iM
2b105 65 6d 2e 20 20 54 68 65 72 65 20 61 72 65 0a 2a  em.  There are.*
2b106 2a 20 70 49 6e 2d 3e 6e 4d 65 6d 20 63 6f 6c 75  * pIn->nMem colu
2b107 6d 6e 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74  mns to be output
2b108 2e 20 20 70 44 65 73 74 20 69 73 20 77 68 65 72  .  pDest is wher
2b109 65 20 74 68 65 20 6f 75 74 70 75 74 20 73 68 6f  e the output sho
2b10a 75 6c 64 0a 2a 2a 20 62 65 20 73 65 6e 74 2e 0a  uld.** be sent..
2b10b 2a 2a 0a 2a 2a 20 72 65 67 52 65 74 75 72 6e 20  **.** regReturn 
2b10c 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2b10d 20 74 68 65 20 72 65 67 69 73 74 65 72 20 68 6f   the register ho
2b10e 6c 64 69 6e 67 20 74 68 65 20 73 75 62 72 6f 75  lding the subrou
2b10f 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 61  tine.** return a
2b110 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  ddress..**.** If
2b111 20 72 65 67 50 72 65 76 3e 30 20 74 68 65 6e 20   regPrev>0 then 
2b112 69 74 20 69 73 20 61 20 74 68 65 20 66 69 72 73  it is a the firs
2b113 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 61 20  t register in a 
2b114 76 65 63 74 6f 72 20 74 68 61 74 0a 2a 2a 20 72  vector that.** r
2b115 65 63 6f 72 64 73 20 74 68 65 20 70 72 65 76 69  ecords the previ
2b116 6f 75 73 20 6f 75 74 70 75 74 2e 20 20 6d 65 6d  ous output.  mem
2b117 5b 72 65 67 50 72 65 76 5d 20 69 73 20 61 20 66  [regPrev] is a f
2b118 6c 61 67 20 74 68 61 74 20 69 73 20 66 61 6c 73  lag that is fals
2b119 65 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 68 61  e.** if there ha
2b11a 73 20 62 65 65 6e 20 6e 6f 20 70 72 65 76 69 6f  s been no previo
2b11b 75 73 20 6f 75 74 70 75 74 2e 20 20 49 66 20 72  us output.  If r
2b11c 65 67 50 72 65 76 3e 30 20 74 68 65 6e 20 63 6f  egPrev>0 then co
2b11d 64 65 20 69 73 0a 2a 2a 20 67 65 6e 65 72 61 74  de is.** generat
2b11e 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 64  ed to suppress d
2b11f 75 70 6c 69 63 61 74 65 73 2e 20 20 70 4b 65 79  uplicates.  pKey
2b120 49 6e 66 6f 20 69 73 20 75 73 65 64 20 66 6f 72  Info is used for
2b121 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 6b 65   comparing.** ke
2b122 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ys..**.** If the
2b123 20 4c 49 4d 49 54 20 66 6f 75 6e 64 20 69 6e 20   LIMIT found in 
2b124 70 2d 3e 69 4c 69 6d 69 74 20 69 73 20 72 65 61  p->iLimit is rea
2b125 63 68 65 64 2c 20 6a 75 6d 70 20 69 6d 6d 65 64  ched, jump immed
2b126 69 61 74 65 6c 79 20 74 6f 0a 2a 2a 20 69 42 72  iately to.** iBr
2b127 65 61 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  eak..*/.static i
2b128 6e 74 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75  nt generateOutpu
2b129 74 53 75 62 72 6f 75 74 69 6e 65 28 0a 20 20 50  tSubroutine(.  P
2b12a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2b12b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2b12c 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
2b12d 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
2b12e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45         /* The SE
2b12f 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
2b130 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a  /.  SelectDest *
2b131 70 49 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 43  pIn,        /* C
2b132 6f 72 6f 75 74 69 6e 65 20 73 75 70 70 6c 79 69  oroutine supplyi
2b133 6e 67 20 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c  ng data */.  Sel
2b134 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20  ectDest *pDest, 
2b135 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f       /* Where to
2b136 20 73 65 6e 64 20 74 68 65 20 64 61 74 61 20 2a   send the data *
2b137 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 74 75 72  /.  int regRetur
2b138 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  n,          /* T
2b139 68 65 20 72 65 74 75 72 6e 20 61 64 64 72 65 73  he return addres
2b13a 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  s register */.  
2b13b 69 6e 74 20 72 65 67 50 72 65 76 2c 20 20 20 20  int regPrev,    
2b13c 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69          /* Previ
2b13d 6f 75 73 20 72 65 73 75 6c 74 20 72 65 67 69 73  ous result regis
2b13e 74 65 72 2e 20 20 4e 6f 20 75 6e 69 71 75 65 6e  ter.  No uniquen
2b13f 65 73 73 20 69 66 20 30 20 2a 2f 0a 20 20 4b 65  ess if 0 */.  Ke
2b140 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c  yInfo *pKeyInfo,
2b141 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6d        /* For com
2b142 70 61 72 69 6e 67 20 77 69 74 68 20 70 72 65 76  paring with prev
2b143 69 6f 75 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20  ious entry */.  
2b144 69 6e 74 20 70 34 74 79 70 65 2c 20 20 20 20 20  int p4type,     
2b145 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2b146 34 20 74 79 70 65 20 66 6f 72 20 70 4b 65 79 49  4 type for pKeyI
2b147 6e 66 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72  nfo */.  int iBr
2b148 65 61 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  eak             
2b149 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66   /* Jump here if
2b14a 20 77 65 20 68 69 74 20 74 68 65 20 4c 49 4d 49   we hit the LIMI
2b14b 54 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  T */.){.  Vdbe *
2b14c 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2b14d 65 3b 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e  e;.  int iContin
2b14e 75 65 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a  ue;.  int addr;.
2b14f 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65  .  addr = sqlite
2b150 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2b151 28 76 29 3b 0a 20 20 69 43 6f 6e 74 69 6e 75 65  (v);.  iContinue
2b152 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2b153 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 2f  keLabel(v);..  /
2b154 2a 20 53 75 70 70 72 65 73 73 20 64 75 70 6c 69  * Suppress dupli
2b155 63 61 74 65 73 20 66 6f 72 20 55 4e 49 4f 4e 2c  cates for UNION,
2b156 20 45 58 43 45 50 54 2c 20 61 6e 64 20 49 4e 54   EXCEPT, and INT
2b157 45 52 53 45 43 54 20 0a 20 20 2a 2f 0a 20 20 69  ERSECT .  */.  i
2b158 66 28 20 72 65 67 50 72 65 76 20 29 7b 0a 20 20  f( regPrev ){.  
2b159 20 20 69 6e 74 20 6a 31 2c 20 6a 32 3b 0a 20 20    int j1, j2;.  
2b15a 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
2b15b 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
2b15c 66 4e 6f 74 2c 20 72 65 67 50 72 65 76 29 3b 0a  fNot, regPrev);.
2b15d 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33      j2 = sqlite3
2b15e 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2b15f 5f 43 6f 6d 70 61 72 65 2c 20 70 49 6e 2d 3e 69  _Compare, pIn->i
2b160 4d 65 6d 2c 20 72 65 67 50 72 65 76 2b 31 2c 20  Mem, regPrev+1, 
2b161 70 49 6e 2d 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20  pIn->nMem,.     
2b162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b163 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
2b164 70 4b 65 79 49 6e 66 6f 2c 20 70 34 74 79 70 65  pKeyInfo, p4type
2b165 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2b166 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a  beAddOp3(v, OP_J
2b167 75 6d 70 2c 20 6a 32 2b 32 2c 20 69 43 6f 6e 74  ump, j2+2, iCont
2b168 69 6e 75 65 2c 20 6a 32 2b 32 29 3b 0a 20 20 20  inue, j2+2);.   
2b169 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2b16a 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20  Here(v, j1);.   
2b16b 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2b16c 43 6f 70 79 28 70 50 61 72 73 65 2c 20 70 49 6e  Copy(pParse, pIn
2b16d 2d 3e 69 4d 65 6d 2c 20 72 65 67 50 72 65 76 2b  ->iMem, regPrev+
2b16e 31 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20  1, pIn->nMem);. 
2b16f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b170 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2b171 65 72 2c 20 31 2c 20 72 65 67 50 72 65 76 29 3b  er, 1, regPrev);
2b172 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73  .  }.  if( pPars
2b173 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
2b174 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  led ) return 0;.
2b175 0a 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20 74  .  /* Suppress t
2b176 68 65 20 74 68 65 20 66 69 72 73 74 20 4f 46 46  he the first OFF
2b177 53 45 54 20 65 6e 74 72 69 65 73 20 69 66 20 74  SET entries if t
2b178 68 65 72 65 20 69 73 20 61 6e 20 4f 46 46 53 45  here is an OFFSE
2b179 54 20 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20  T clause.  */.  
2b17a 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c  codeOffset(v, p,
2b17b 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 0a 20 20   iContinue);..  
2b17c 73 77 69 74 63 68 28 20 70 44 65 73 74 2d 3e 65  switch( pDest->e
2b17d 44 65 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20 53  Dest ){.    /* S
2b17e 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
2b17f 61 73 20 64 61 74 61 20 75 73 69 6e 67 20 61 20  as data using a 
2b180 75 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20  unique key..    
2b181 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
2b182 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20  Table:.    case 
2b183 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a  SRT_EphemTab: {.
2b184 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73        int r1 = s
2b185 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2b186 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2b187 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33  int r2 = sqlite3
2b188 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2b189 65 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  e);.      testca
2b18a 73 65 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74  se( pDest->eDest
2b18b 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20  ==SRT_Table );. 
2b18c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
2b18d 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54  Dest->eDest==SRT
2b18e 5f 45 70 68 65 6d 54 61 62 20 29 3b 0a 20 20 20  _EphemTab );.   
2b18f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b190 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp3(v, OP_MakeR
2b191 65 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d  ecord, pIn->iMem
2b192 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 2c 20 72 31 29  , pIn->nMem, r1)
2b193 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b194 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b195 4e 65 77 52 6f 77 69 64 2c 20 70 44 65 73 74 2d  NewRowid, pDest-
2b196 3e 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20  >iParm, r2);.   
2b197 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b198 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
2b199 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c  t, pDest->iParm,
2b19a 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20 20   r1, r2);.      
2b19b 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2b19c 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50  eP5(v, OPFLAG_AP
2b19d 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 73 71 6c  PEND);.      sql
2b19e 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2b19f 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a  eg(pParse, r2);.
2b1a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
2b1a1 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
2b1a2 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 62  se, r1);.      b
2b1a3 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  reak;.    }..#if
2b1a4 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2b1a5 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 2f 2a  _SUBQUERY.    /*
2b1a6 20 49 66 20 77 65 20 61 72 65 20 63 72 65 61 74   If we are creat
2b1a7 69 6e 67 20 61 20 73 65 74 20 66 6f 72 20 61 6e  ing a set for an
2b1a8 20 22 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43   "expr IN (SELEC
2b1a9 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63  T ...)" construc
2b1aa 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  t,.    ** then t
2b1ab 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 61  here should be a
2b1ac 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20   single item on 
2b1ad 74 68 65 20 73 74 61 63 6b 2e 20 20 57 72 69 74  the stack.  Writ
2b1ae 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 74  e this.    ** it
2b1af 65 6d 20 69 6e 74 6f 20 74 68 65 20 73 65 74 20  em into the set 
2b1b0 74 61 62 6c 65 20 77 69 74 68 20 62 6f 67 75 73  table with bogus
2b1b1 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20   data..    */.  
2b1b2 20 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a 20    case SRT_Set: 
2b1b3 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a  {.      int r1;.
2b1b4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
2b1b5 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20 20  n->nMem==1 );.  
2b1b6 20 20 20 20 70 2d 3e 61 66 66 69 6e 69 74 79 20      p->affinity 
2b1b7 3d 20 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  = .         sqli
2b1b8 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
2b1b9 74 79 28 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  ty(p->pEList->a[
2b1ba 30 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d  0].pExpr, pDest-
2b1bb 3e 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20 20  >affinity);.    
2b1bc 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
2b1bd 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2b1be 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b1bf 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2b1c0 4d 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d  MakeRecord, pIn-
2b1c1 3e 69 4d 65 6d 2c 20 31 2c 20 72 31 2c 20 26 70  >iMem, 1, r1, &p
2b1c2 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a  ->affinity, 1);.
2b1c3 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2b1c4 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
2b1c5 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e  ange(pParse, pIn
2b1c6 2d 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20  ->iMem, 1);.    
2b1c7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b1c8 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  Op2(v, OP_IdxIns
2b1c9 65 72 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72  ert, pDest->iPar
2b1ca 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  m, r1);.      sq
2b1cb 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2b1cc 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2b1cd 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2b1ce 20 20 7d 0a 0a 23 69 66 20 30 20 20 2f 2a 20 4e    }..#if 0  /* N
2b1cf 65 76 65 72 20 6f 63 63 75 72 73 20 6f 6e 20 61  ever occurs on a
2b1d0 6e 20 4f 52 44 45 52 20 42 59 20 71 75 65 72 79  n ORDER BY query
2b1d1 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e   */.    /* If an
2b1d2 79 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74  y row exist in t
2b1d3 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72  he result set, r
2b1d4 65 63 6f 72 64 20 74 68 61 74 20 66 61 63 74 20  ecord that fact 
2b1d5 61 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a  and abort..    *
2b1d6 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45  /.    case SRT_E
2b1d7 78 69 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73  xists: {.      s
2b1d8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b1d9 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2b1da 31 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29  1, pDest->iParm)
2b1db 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c  ;.      /* The L
2b1dc 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c  IMIT clause will
2b1dd 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c   terminate the l
2b1de 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20  oop for us */.  
2b1df 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2b1e0 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
2b1e1 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61  If this is a sca
2b1e2 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20  lar select that 
2b1e3 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78  is part of an ex
2b1e4 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20  pression, then. 
2b1e5 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20     ** store the 
2b1e6 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61  results in the a
2b1e7 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72  ppropriate memor
2b1e8 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b  y cell and break
2b1e9 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74   out.    ** of t
2b1ea 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20  he scan loop..  
2b1eb 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
2b1ec 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61  T_Mem: {.      a
2b1ed 73 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d  ssert( pIn->nMem
2b1ee 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==1 );.      sql
2b1ef 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
2b1f0 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d  (pParse, pIn->iM
2b1f1 65 6d 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  em, pDest->iParm
2b1f2 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  , 1);.      /* T
2b1f3 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
2b1f4 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66  will jump out of
2b1f5 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73   the loop for us
2b1f6 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   */.      break;
2b1f7 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
2b1f8 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
2b1f9 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
2b1fa 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73  ..    /* The res
2b1fb 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  ults are stored 
2b1fc 69 6e 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66  in a sequence of
2b1fd 20 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a   registers.    *
2b1fe 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 44  * starting at pD
2b1ff 65 73 74 2d 3e 69 4d 65 6d 2e 20 20 54 68 65 6e  est->iMem.  Then
2b200 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20   the co-routine 
2b201 79 69 65 6c 64 73 2e 0a 20 20 20 20 2a 2f 0a 20  yields..    */. 
2b202 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72 6f     case SRT_Coro
2b203 75 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20 20 69  utine: {.      i
2b204 66 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d  f( pDest->iMem==
2b205 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65  0 ){.        pDe
2b206 73 74 2d 3e 69 4d 65 6d 20 3d 20 73 71 6c 69 74  st->iMem = sqlit
2b207 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
2b208 50 61 72 73 65 2c 20 70 49 6e 2d 3e 6e 4d 65 6d  Parse, pIn->nMem
2b209 29 3b 0a 20 20 20 20 20 20 20 20 70 44 65 73 74  );.        pDest
2b20a 2d 3e 6e 4d 65 6d 20 3d 20 70 49 6e 2d 3e 6e 4d  ->nMem = pIn->nM
2b20b 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  em;.      }.    
2b20c 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2b20d 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 70 49  eMove(pParse, pI
2b20e 6e 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e  n->iMem, pDest->
2b20f 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 6e 4d 65  iMem, pDest->nMe
2b210 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  m);.      sqlite
2b211 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2b212 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e  P_Yield, pDest->
2b213 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 62 72  iParm);.      br
2b214 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
2b215 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68  /* If none of th
2b216 65 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 74 68  e above, then th
2b217 65 20 72 65 73 75 6c 74 20 64 65 73 74 69 6e 61  e result destina
2b218 74 69 6f 6e 20 6d 75 73 74 20 62 65 0a 20 20 20  tion must be.   
2b219 20 2a 2a 20 53 52 54 5f 4f 75 74 70 75 74 2e 20   ** SRT_Output. 
2b21a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2b21b 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 77 69   never called wi
2b21c 74 68 20 61 6e 79 20 6f 74 68 65 72 0a 20 20 20  th any other.   
2b21d 20 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20   ** destination 
2b21e 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f  other than the o
2b21f 6e 65 73 20 68 61 6e 64 6c 65 64 20 61 62 6f 76  nes handled abov
2b220 65 20 6f 72 20 53 52 54 5f 4f 75 74 70 75 74 2e  e or SRT_Output.
2b221 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46  .    **.    ** F
2b222 6f 72 20 53 52 54 5f 4f 75 74 70 75 74 2c 20 72  or SRT_Output, r
2b223 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65  esults are store
2b224 64 20 69 6e 20 61 20 73 65 71 75 65 6e 63 65 20  d in a sequence 
2b225 6f 66 20 72 65 67 69 73 74 65 72 73 2e 20 20 0a  of registers.  .
2b226 20 20 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20      ** Then the 
2b227 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20 6f 70 63  OP_ResultRow opc
2b228 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 63  ode is used to c
2b229 61 75 73 65 20 73 71 6c 69 74 65 33 5f 73 74 65  ause sqlite3_ste
2b22a 70 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65  p() to.    ** re
2b22b 74 75 72 6e 20 74 68 65 20 6e 65 78 74 20 72 6f  turn the next ro
2b22c 77 20 6f 66 20 72 65 73 75 6c 74 2e 0a 20 20 20  w of result..   
2b22d 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a   */.    default:
2b22e 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
2b22f 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
2b230 52 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20 20  RT_Output );.   
2b231 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b232 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
2b233 74 52 6f 77 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  tRow, pIn->iMem,
2b234 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20   pIn->nMem);.   
2b235 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2b236 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
2b237 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69  e(pParse, pIn->i
2b238 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b  Mem, pIn->nMem);
2b239 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2b23a 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75    }.  }..  /* Ju
2b23b 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  mp to the end of
2b23c 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65   the loop if the
2b23d 20 4c 49 4d 49 54 20 69 73 20 72 65 61 63 68 65   LIMIT is reache
2b23e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  d..  */.  if( p-
2b23f 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 73  >iLimit ){.    s
2b240 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b241 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70  (v, OP_AddImm, p
2b242 2d 3e 69 4c 69 6d 69 74 2c 20 2d 31 29 3b 0a 20  ->iLimit, -1);. 
2b243 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b244 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65 72  dOp2(v, OP_IfZer
2b245 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69 42  o, p->iLimit, iB
2b246 72 65 61 6b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  reak);.  }..  /*
2b247 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 73 75   Generate the su
2b248 62 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 0a  broutine return.
2b249 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
2b24a 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2b24b 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20  , iContinue);.  
2b24c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b24d 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20  1(v, OP_Return, 
2b24e 72 65 67 52 65 74 75 72 6e 29 3b 0a 0a 20 20 72  regReturn);..  r
2b24f 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f  eturn addr;.}../
2b250 2a 0a 2a 2a 20 41 6c 74 65 72 6e 61 74 69 76 65  *.** Alternative
2b251 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
2b252 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
2b253 66 6f 72 20 63 61 73 65 73 20 77 68 65 6e 20 74  for cases when t
2b254 68 65 72 65 0a 2a 2a 20 69 73 20 61 6e 20 4f 52  here.** is an OR
2b255 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a  DER BY clause..*
2b256 2a 0a 2a 2a 20 57 65 20 61 73 73 75 6d 65 20 61  *.** We assume a
2b257 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 66 6f   query of the fo
2b258 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 3a 0a 2a 2a  llowing form:.**
2b259 0a 2a 2a 20 20 20 20 20 20 3c 73 65 6c 65 63 74  .**      <select
2b25a 41 3e 20 20 3c 6f 70 65 72 61 74 6f 72 3e 20 20  A>  <operator>  
2b25b 3c 73 65 6c 65 63 74 42 3e 20 20 4f 52 44 45 52  <selectB>  ORDER
2b25c 20 42 59 20 3c 6f 72 64 65 72 62 79 6c 69 73 74   BY <orderbylist
2b25d 3e 0a 2a 2a 0a 2a 2a 20 3c 6f 70 65 72 61 74 6f  >.**.** <operato
2b25e 72 3e 20 69 73 20 6f 6e 65 20 6f 66 20 55 4e 49  r> is one of UNI
2b25f 4f 4e 20 41 4c 4c 2c 20 55 4e 49 4f 4e 2c 20 45  ON ALL, UNION, E
2b260 58 43 45 50 54 2c 20 6f 72 20 49 4e 54 45 52 53  XCEPT, or INTERS
2b261 45 43 54 2e 20 20 54 68 65 20 69 64 65 61 0a 2a  ECT.  The idea.*
2b262 2a 20 69 73 20 74 6f 20 63 6f 64 65 20 62 6f 74  * is to code bot
2b263 68 20 3c 73 65 6c 65 63 74 41 3e 20 61 6e 64 20  h <selectA> and 
2b264 3c 73 65 6c 65 63 74 42 3e 20 77 69 74 68 20 74  <selectB> with t
2b265 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2b266 73 65 20 61 73 0a 2a 2a 20 63 6f 2d 72 6f 75 74  se as.** co-rout
2b267 69 6e 65 73 2e 20 20 54 68 65 6e 20 72 75 6e 20  ines.  Then run 
2b268 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 73 20  the co-routines 
2b269 69 6e 20 70 61 72 61 6c 6c 65 6c 20 61 6e 64 20  in parallel and 
2b26a 6d 65 72 67 65 20 74 68 65 20 72 65 73 75 6c 74  merge the result
2b26b 73 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6f 75  s.** into the ou
2b26c 74 70 75 74 2e 20 20 49 6e 20 61 64 64 69 74 69  tput.  In additi
2b26d 6f 6e 20 74 6f 20 74 68 65 20 74 77 6f 20 63 6f  on to the two co
2b26e 72 6f 75 74 69 6e 65 73 20 28 63 61 6c 6c 65 64  routines (called
2b26f 20 73 65 6c 65 63 74 41 20 61 6e 64 0a 2a 2a 20   selectA and.** 
2b270 73 65 6c 65 63 74 42 29 20 74 68 65 72 65 20 61  selectB) there a
2b271 72 65 20 37 20 73 75 62 72 6f 75 74 69 6e 65 73  re 7 subroutines
2b272 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 75 74 41 3a  :.**.**    outA:
2b273 20 20 20 20 4d 6f 76 65 20 74 68 65 20 6f 75 74      Move the out
2b274 70 75 74 20 6f 66 20 74 68 65 20 73 65 6c 65 63  put of the selec
2b275 74 41 20 63 6f 72 6f 75 74 69 6e 65 20 69 6e 74  tA coroutine int
2b276 6f 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20  o the output.** 
2b277 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
2b278 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72  he compound quer
2b279 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 75 74 42  y..**.**    outB
2b27a 3a 20 20 20 20 4d 6f 76 65 20 74 68 65 20 6f 75  :    Move the ou
2b27b 74 70 75 74 20 6f 66 20 74 68 65 20 73 65 6c 65  tput of the sele
2b27c 63 74 42 20 63 6f 72 6f 75 74 69 6e 65 20 69 6e  ctB coroutine in
2b27d 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a  to the output.**
2b27e 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20               of 
2b27f 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65  the compound que
2b280 72 79 2e 20 20 28 4f 6e 6c 79 20 67 65 6e 65 72  ry.  (Only gener
2b281 61 74 65 64 20 66 6f 72 20 55 4e 49 4f 4e 20 61  ated for UNION a
2b282 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  nd.**           
2b283 20 20 55 4e 49 4f 4e 20 41 4c 4c 2e 20 20 45 58    UNION ALL.  EX
2b284 43 45 50 54 20 61 6e 64 20 49 4e 53 45 52 54 53  CEPT and INSERTS
2b285 45 43 54 20 6e 65 76 65 72 20 6f 75 74 70 75 74  ECT never output
2b286 20 61 20 72 6f 77 20 74 68 61 74 0a 2a 2a 20 20   a row that.**  
2b287 20 20 20 20 20 20 20 20 20 20 20 61 70 70 65 61             appea
2b288 72 73 20 6f 6e 6c 79 20 69 6e 20 42 2e 29 0a 2a  rs only in B.).*
2b289 2a 0a 2a 2a 20 20 20 20 41 6c 74 42 3a 20 20 20  *.**    AltB:   
2b28a 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   Called when the
2b28b 72 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20  re is data from 
2b28c 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20  both coroutines 
2b28d 61 6e 64 20 41 3c 42 2e 0a 2a 2a 0a 2a 2a 20 20  and A<B..**.**  
2b28e 20 20 41 65 71 42 3a 20 20 20 20 43 61 6c 6c 65    AeqB:    Calle
2b28f 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20  d when there is 
2b290 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63  data from both c
2b291 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3d  oroutines and A=
2b292 3d 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 67 74  =B..**.**    Agt
2b293 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65  B:    Called whe
2b294 6e 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20  n there is data 
2b295 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74  from both corout
2b296 69 6e 65 73 20 61 6e 64 20 41 3e 42 2e 0a 2a 2a  ines and A>B..**
2b297 0a 2a 2a 20 20 20 20 45 6f 66 41 3a 20 20 20 20  .**    EofA:    
2b298 43 61 6c 6c 65 64 20 77 68 65 6e 20 64 61 74 61  Called when data
2b299 20 69 73 20 65 78 68 61 75 73 74 65 64 20 66 72   is exhausted fr
2b29a 6f 6d 20 73 65 6c 65 63 74 41 2e 0a 2a 2a 0a 2a  om selectA..**.*
2b29b 2a 20 20 20 20 45 6f 66 42 3a 20 20 20 20 43 61  *    EofB:    Ca
2b29c 6c 6c 65 64 20 77 68 65 6e 20 64 61 74 61 20 69  lled when data i
2b29d 73 20 65 78 68 61 75 73 74 65 64 20 66 72 6f 6d  s exhausted from
2b29e 20 73 65 6c 65 63 74 42 2e 0a 2a 2a 0a 2a 2a 20   selectB..**.** 
2b29f 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  The implementati
2b2a0 6f 6e 20 6f 66 20 74 68 65 20 6c 61 74 74 65 72  on of the latter
2b2a1 20 66 69 76 65 20 73 75 62 72 6f 75 74 69 6e 65   five subroutine
2b2a2 73 20 64 65 70 65 6e 64 20 6f 6e 20 77 68 69 63  s depend on whic
2b2a3 68 20 0a 2a 2a 20 3c 6f 70 65 72 61 74 6f 72 3e  h .** <operator>
2b2a4 20 69 73 20 75 73 65 64 3a 0a 2a 2a 0a 2a 2a 0a   is used:.**.**.
2b2a5 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 55  **             U
2b2a6 4e 49 4f 4e 20 41 4c 4c 20 20 20 20 20 20 20 20  NION ALL        
2b2a7 20 55 4e 49 4f 4e 20 20 20 20 20 20 20 20 20 20   UNION          
2b2a8 20 20 45 58 43 45 50 54 20 20 20 20 20 20 20 20    EXCEPT        
2b2a9 20 20 49 4e 54 45 52 53 45 43 54 0a 2a 2a 20 20    INTERSECT.**  
2b2aa 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d          --------
2b2ab 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  -----  ---------
2b2ac 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d  --------  ------
2b2ad 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d  --------  ------
2b2ae 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
2b2af 20 41 6c 74 42 3a 20 20 20 6f 75 74 41 2c 20 6e   AltB:   outA, n
2b2b0 65 78 74 41 20 20 20 20 20 20 6f 75 74 41 2c 20  extA      outA, 
2b2b1 6e 65 78 74 41 20 20 20 20 20 20 20 6f 75 74 41  nextA       outA
2b2b2 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20  , nextA         
2b2b3 6e 65 78 74 41 0a 2a 2a 0a 2a 2a 20 20 20 41 65  nextA.**.**   Ae
2b2b4 71 42 3a 20 20 20 6f 75 74 41 2c 20 6e 65 78 74  qB:   outA, next
2b2b5 41 20 20 20 20 20 20 20 20 20 6e 65 78 74 41 20  A         nextA 
2b2b6 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74              next
2b2b7 41 20 20 20 20 20 20 20 20 20 6f 75 74 41 2c 20  A         outA, 
2b2b8 6e 65 78 74 41 0a 2a 2a 0a 2a 2a 20 20 20 41 67  nextA.**.**   Ag
2b2b9 74 42 3a 20 20 20 6f 75 74 42 2c 20 6e 65 78 74  tB:   outB, next
2b2ba 42 20 20 20 20 20 20 6f 75 74 42 2c 20 6e 65 78  B      outB, nex
2b2bb 74 42 20 20 20 20 20 20 20 20 20 20 6e 65 78 74  tB          next
2b2bc 42 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78  B            nex
2b2bd 74 42 0a 2a 2a 0a 2a 2a 20 20 20 45 6f 66 41 3a  tB.**.**   EofA:
2b2be 20 20 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20     outB, nextB  
2b2bf 20 20 20 20 6f 75 74 42 2c 20 6e 65 78 74 42 20      outB, nextB 
2b2c0 20 20 20 20 20 20 20 20 20 68 61 6c 74 20 20 20           halt   
2b2c1 20 20 20 20 20 20 20 20 20 20 68 61 6c 74 0a 2a            halt.*
2b2c2 2a 0a 2a 2a 20 20 20 45 6f 66 42 3a 20 20 20 6f  *.**   EofB:   o
2b2c3 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20  utA, nextA      
2b2c4 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
2b2c5 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20    outA, nextA   
2b2c6 20 20 20 20 20 20 68 61 6c 74 0a 2a 2a 0a 2a 2a        halt.**.**
2b2c7 20 49 6e 20 74 68 65 20 41 6c 74 42 2c 20 41 65   In the AltB, Ae
2b2c8 71 42 2c 20 61 6e 64 20 41 67 74 42 20 73 75 62  qB, and AgtB sub
2b2c9 72 6f 75 74 69 6e 65 73 2c 20 61 6e 20 45 4f 46  routines, an EOF
2b2ca 20 6f 6e 20 41 20 66 6f 6c 6c 6f 77 69 6e 67 20   on A following 
2b2cb 6e 65 78 74 41 0a 2a 2a 20 63 61 75 73 65 73 20  nextA.** causes 
2b2cc 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d  an immediate jum
2b2cd 70 20 74 6f 20 45 6f 66 41 20 61 6e 64 20 61 6e  p to EofA and an
2b2ce 20 45 4f 46 20 6f 6e 20 42 20 66 6f 6c 6c 6f 77   EOF on B follow
2b2cf 69 6e 67 20 6e 65 78 74 42 20 63 61 75 73 65 73  ing nextB causes
2b2d0 0a 2a 2a 20 61 6e 20 69 6d 6d 65 64 69 61 74 65  .** an immediate
2b2d1 20 6a 75 6d 70 20 74 6f 20 45 6f 66 42 2e 20 20   jump to EofB.  
2b2d2 57 69 74 68 69 6e 20 45 6f 66 41 20 61 6e 64 20  Within EofA and 
2b2d3 45 6f 66 42 2c 20 61 6e 64 20 45 4f 46 20 6f 6e  EofB, and EOF on
2b2d4 20 65 6e 74 72 79 20 6f 72 0a 2a 2a 20 66 6f 6c   entry or.** fol
2b2d5 6c 6f 77 69 6e 67 20 6e 65 78 74 58 20 63 61 75  lowing nextX cau
2b2d6 73 65 73 20 61 20 6a 75 6d 70 20 74 6f 20 74 68  ses a jump to th
2b2d7 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 65 6c  e end of the sel
2b2d8 65 63 74 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a  ect processing..
2b2d9 2a 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 74 65 20  **.** Duplicate 
2b2da 72 65 6d 6f 76 61 6c 20 69 6e 20 74 68 65 20 55  removal in the U
2b2db 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 61 6e  NION, EXCEPT, an
2b2dc 64 20 49 4e 54 45 52 53 45 43 54 20 63 61 73 65  d INTERSECT case
2b2dd 73 20 69 73 20 68 61 6e 64 6c 65 64 0a 2a 2a 20  s is handled.** 
2b2de 77 69 74 68 69 6e 20 74 68 65 20 6f 75 74 70 75  within the outpu
2b2df 74 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 54  t subroutine.  T
2b2e0 68 65 20 72 65 67 50 72 65 76 20 72 65 67 69 73  he regPrev regis
2b2e1 74 65 72 20 73 65 74 20 68 6f 6c 64 73 20 74 68  ter set holds th
2b2e2 65 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20  e previously.** 
2b2e3 6f 75 74 70 75 74 20 76 61 6c 75 65 2e 20 20 41  output value.  A
2b2e4 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d   comparison is m
2b2e5 61 64 65 20 61 67 61 69 6e 73 74 20 74 68 69 73  ade against this
2b2e6 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20 6f   value and the o
2b2e7 75 74 70 75 74 0a 2a 2a 20 69 73 20 73 6b 69 70  utput.** is skip
2b2e8 70 65 64 20 69 66 20 74 68 65 20 6e 65 78 74 20  ped if the next 
2b2e9 72 65 73 75 6c 74 73 20 77 6f 75 6c 64 20 62 65  results would be
2b2ea 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
2b2eb 20 70 72 65 76 69 6f 75 73 2e 0a 2a 2a 0a 2a 2a   previous..**.**
2b2ec 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
2b2ed 69 6f 6e 20 70 6c 61 6e 20 69 73 20 74 6f 20 69  ion plan is to i
2b2ee 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 74 77 6f  mplement the two
2b2ef 20 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20   coroutines and 
2b2f0 73 65 76 65 6e 0a 2a 2a 20 73 75 62 72 6f 75 74  seven.** subrout
2b2f1 69 6e 65 73 20 66 69 72 73 74 2c 20 74 68 65 6e  ines first, then
2b2f2 20 70 75 74 20 74 68 65 20 63 6f 6e 74 72 6f 6c   put the control
2b2f3 20 6c 6f 67 69 63 20 61 74 20 74 68 65 20 62 6f   logic at the bo
2b2f4 74 74 6f 6d 2e 20 20 4c 69 6b 65 20 74 68 69 73  ttom.  Like this
2b2f5 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
2b2f6 20 67 6f 74 6f 20 49 6e 69 74 0a 2a 2a 20 20 20   goto Init.**   
2b2f7 20 20 63 6f 41 3a 20 63 6f 72 6f 75 74 69 6e 65    coA: coroutine
2b2f8 20 66 6f 72 20 6c 65 66 74 20 71 75 65 72 79 20   for left query 
2b2f9 28 41 29 0a 2a 2a 20 20 20 20 20 63 6f 42 3a 20  (A).**     coB: 
2b2fa 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 72 69  coroutine for ri
2b2fb 67 68 74 20 71 75 65 72 79 20 28 42 29 0a 2a 2a  ght query (B).**
2b2fc 20 20 20 20 6f 75 74 41 3a 20 6f 75 74 70 75 74      outA: output
2b2fd 20 6f 6e 65 20 72 6f 77 20 6f 66 20 41 0a 2a 2a   one row of A.**
2b2fe 20 20 20 20 6f 75 74 42 3a 20 6f 75 74 70 75 74      outB: output
2b2ff 20 6f 6e 65 20 72 6f 77 20 6f 66 20 42 20 28 55   one row of B (U
2b300 4e 49 4f 4e 20 61 6e 64 20 55 4e 49 4f 4e 20 41  NION and UNION A
2b301 4c 4c 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 20 45  LL only).**    E
2b302 6f 66 41 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 45  ofA: ....**    E
2b303 6f 66 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41  ofB: ....**    A
2b304 6c 74 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41  ltB: ....**    A
2b305 65 71 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41  eqB: ....**    A
2b306 67 74 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 49  gtB: ....**    I
2b307 6e 69 74 3a 20 69 6e 69 74 69 61 6c 69 7a 65 20  nit: initialize 
2b308 63 6f 72 6f 75 74 69 6e 65 20 72 65 67 69 73 74  coroutine regist
2b309 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ers.**          
2b30a 79 69 65 6c 64 20 63 6f 41 0a 2a 2a 20 20 20 20  yield coA.**    
2b30b 20 20 20 20 20 20 69 66 20 65 6f 66 28 41 29 20        if eof(A) 
2b30c 67 6f 74 6f 20 45 6f 66 41 0a 2a 2a 20 20 20 20  goto EofA.**    
2b30d 20 20 20 20 20 20 79 69 65 6c 64 20 63 6f 42 0a        yield coB.
2b30e 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 65  **          if e
2b30f 6f 66 28 42 29 20 67 6f 74 6f 20 45 6f 66 42 0a  of(B) goto EofB.
2b310 2a 2a 20 20 20 20 43 6d 70 72 3a 20 43 6f 6d 70  **    Cmpr: Comp
2b311 61 72 65 20 41 2c 20 42 0a 2a 2a 20 20 20 20 20  are A, B.**     
2b312 20 20 20 20 20 4a 75 6d 70 20 41 6c 74 42 2c 20       Jump AltB, 
2b313 41 65 71 42 2c 20 41 67 74 42 0a 2a 2a 20 20 20  AeqB, AgtB.**   
2b314 20 20 45 6e 64 3a 20 2e 2e 2e 0a 2a 2a 0a 2a 2a    End: ....**.**
2b315 20 57 65 20 63 61 6c 6c 20 41 6c 74 42 2c 20 41   We call AltB, A
2b316 65 71 42 2c 20 41 67 74 42 2c 20 45 6f 66 41 2c  eqB, AgtB, EofA,
2b317 20 61 6e 64 20 45 6f 66 42 20 22 73 75 62 72 6f   and EofB "subro
2b318 75 74 69 6e 65 73 22 20 62 75 74 20 74 68 65 79  utines" but they
2b319 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 63 74 75   are not.** actu
2b31a 61 6c 6c 79 20 63 61 6c 6c 65 64 20 75 73 69 6e  ally called usin
2b31b 67 20 47 6f 73 75 62 20 61 6e 64 20 74 68 65 79  g Gosub and they
2b31c 20 64 6f 20 6e 6f 74 20 52 65 74 75 72 6e 2e 20   do not Return. 
2b31d 20 45 6f 66 41 20 61 6e 64 20 45 6f 66 42 20 6c   EofA and EofB l
2b31e 6f 6f 70 0a 2a 2a 20 75 6e 74 69 6c 20 61 6c 6c  oop.** until all
2b31f 20 64 61 74 61 20 69 73 20 65 78 68 61 75 73 74   data is exhaust
2b320 65 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  ed then jump to 
2b321 74 68 65 20 22 65 6e 64 22 20 6c 61 62 65 2e 20  the "end" labe. 
2b322 20 41 6c 74 42 2c 20 41 65 71 42 2c 0a 2a 2a 20   AltB, AeqB,.** 
2b323 61 6e 64 20 41 67 74 42 20 6a 75 6d 70 20 74 6f  and AgtB jump to
2b324 20 65 69 74 68 65 72 20 4c 32 20 6f 72 20 74 6f   either L2 or to
2b325 20 6f 6e 65 20 6f 66 20 45 6f 66 41 20 6f 72 20   one of EofA or 
2b326 45 6f 66 42 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  EofB..*/.#ifndef
2b327 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
2b328 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 73 74 61  POUND_SELECT.sta
2b329 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c  tic int multiSel
2b32a 65 63 74 4f 72 64 65 72 42 79 28 0a 20 20 50 61  ectOrderBy(.  Pa
2b32b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2b32c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2b32d 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
2b32e 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
2b32f 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d    /* The right-m
2b330 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74  ost of SELECTs t
2b331 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20  o be coded */.  
2b332 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
2b333 74 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f  t     /* What to
2b334 20 64 6f 20 77 69 74 68 20 71 75 65 72 79 20 72   do with query r
2b335 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69  esults */.){.  i
2b336 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
2b337 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2b338 6e 74 65 72 73 20 2a 2f 0a 20 20 53 65 6c 65 63  nters */.  Selec
2b339 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20  t *pPrior;      
2b33a 20 2f 2a 20 41 6e 6f 74 68 65 72 20 53 45 4c 45   /* Another SELE
2b33b 43 54 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  CT immediately t
2b33c 6f 20 6f 75 72 20 6c 65 66 74 20 2a 2f 0a 20 20  o our left */.  
2b33d 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
2b33e 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
2b33f 65 20 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56  e code to this V
2b340 44 42 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  DBE */.  SelectD
2b341 65 73 74 20 64 65 73 74 41 3b 20 20 20 20 20 2f  est destA;     /
2b342 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f  * Destination fo
2b343 72 20 63 6f 72 6f 75 74 69 6e 65 20 41 20 2a 2f  r coroutine A */
2b344 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
2b345 73 74 42 3b 20 20 20 20 20 2f 2a 20 44 65 73 74  stB;     /* Dest
2b346 69 6e 61 74 69 6f 6e 20 66 6f 72 20 63 6f 72 6f  ination for coro
2b347 75 74 69 6e 65 20 42 20 2a 2f 0a 20 20 69 6e 74  utine B */.  int
2b348 20 72 65 67 41 64 64 72 41 3b 20 20 20 20 20 20   regAddrA;      
2b349 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65     /* Address re
2b34a 67 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63  gister for selec
2b34b 74 2d 41 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f  t-A coroutine */
2b34c 0a 20 20 69 6e 74 20 72 65 67 45 6f 66 41 3b 20  .  int regEofA; 
2b34d 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
2b34e 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65   to indicate whe
2b34f 6e 20 73 65 6c 65 63 74 2d 41 20 69 73 20 63 6f  n select-A is co
2b350 6d 70 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20  mplete */.  int 
2b351 72 65 67 41 64 64 72 42 3b 20 20 20 20 20 20 20  regAddrB;       
2b352 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67    /* Address reg
2b353 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74  ister for select
2b354 2d 42 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a  -B coroutine */.
2b355 20 20 69 6e 74 20 72 65 67 45 6f 66 42 3b 20 20    int regEofB;  
2b356 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 20          /* Flag 
2b357 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e  to indicate when
2b358 20 73 65 6c 65 63 74 2d 42 20 69 73 20 63 6f 6d   select-B is com
2b359 70 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 61  plete */.  int a
2b35a 64 64 72 53 65 6c 65 63 74 41 3b 20 20 20 20 20  ddrSelectA;     
2b35b 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
2b35c 68 65 20 73 65 6c 65 63 74 2d 41 20 63 6f 72 6f  he select-A coro
2b35d 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61  utine */.  int a
2b35e 64 64 72 53 65 6c 65 63 74 42 3b 20 20 20 20 20  ddrSelectB;     
2b35f 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
2b360 68 65 20 73 65 6c 65 63 74 2d 42 20 63 6f 72 6f  he select-B coro
2b361 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72  utine */.  int r
2b362 65 67 4f 75 74 41 3b 20 20 20 20 20 20 20 20 20  egOutA;         
2b363 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67 69   /* Address regi
2b364 73 74 65 72 20 66 6f 72 20 74 68 65 20 6f 75 74  ster for the out
2b365 70 75 74 2d 41 20 73 75 62 72 6f 75 74 69 6e 65  put-A subroutine
2b366 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 75 74   */.  int regOut
2b367 42 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  B;          /* A
2b368 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20  ddress register 
2b369 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 2d 42  for the output-B
2b36a 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20   subroutine */. 
2b36b 20 69 6e 74 20 61 64 64 72 4f 75 74 41 3b 20 20   int addrOutA;  
2b36c 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
2b36d 73 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 2d  s of the output-
2b36e 41 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  A subroutine */.
2b36f 20 20 69 6e 74 20 61 64 64 72 4f 75 74 42 20 3d    int addrOutB =
2b370 20 30 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65   0;     /* Addre
2b371 73 73 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74  ss of the output
2b372 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f  -B subroutine */
2b373 0a 20 20 69 6e 74 20 61 64 64 72 45 6f 66 41 3b  .  int addrEofA;
2b374 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
2b375 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63  ess of the selec
2b376 74 2d 41 2d 65 78 68 61 75 73 74 65 64 20 73 75  t-A-exhausted su
2b377 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  broutine */.  in
2b378 74 20 61 64 64 72 45 6f 66 42 3b 20 20 20 20 20  t addrEofB;     
2b379 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
2b37a 66 20 74 68 65 20 73 65 6c 65 63 74 2d 42 2d 65  f the select-B-e
2b37b 78 68 61 75 73 74 65 64 20 73 75 62 72 6f 75 74  xhausted subrout
2b37c 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ine */.  int add
2b37d 72 41 6c 74 42 3b 20 20 20 20 20 20 20 20 20 2f  rAltB;         /
2b37e 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
2b37f 20 41 3c 42 20 73 75 62 72 6f 75 74 69 6e 65 20   A<B subroutine 
2b380 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 41 65 71  */.  int addrAeq
2b381 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  B;         /* Ad
2b382 64 72 65 73 73 20 6f 66 20 74 68 65 20 41 3d 3d  dress of the A==
2b383 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  B subroutine */.
2b384 20 20 69 6e 74 20 61 64 64 72 41 67 74 42 3b 20    int addrAgtB; 
2b385 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2b386 73 73 20 6f 66 20 74 68 65 20 41 3e 42 20 73 75  ss of the A>B su
2b387 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  broutine */.  in
2b388 74 20 72 65 67 4c 69 6d 69 74 41 3b 20 20 20 20  t regLimitA;    
2b389 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 72 65 67      /* Limit reg
2b38a 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74  ister for select
2b38b 2d 41 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c  -A */.  int regL
2b38c 69 6d 69 74 42 3b 20 20 20 20 20 20 20 20 2f 2a  imitB;        /*
2b38d 20 4c 69 6d 69 74 20 72 65 67 69 73 74 65 72 20   Limit register 
2b38e 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 2a 2f 0a  for select-A */.
2b38f 20 20 69 6e 74 20 72 65 67 50 72 65 76 3b 20 20    int regPrev;  
2b390 20 20 20 20 20 20 20 20 2f 2a 20 41 20 72 61 6e          /* A ran
2b391 67 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ge of registers 
2b392 74 6f 20 68 6f 6c 64 20 70 72 65 76 69 6f 75 73  to hold previous
2b393 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74   output */.  int
2b394 20 73 61 76 65 64 4c 69 6d 69 74 3b 20 20 20 20   savedLimit;    
2b395 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75     /* Saved valu
2b396 65 20 6f 66 20 70 2d 3e 69 4c 69 6d 69 74 20 2a  e of p->iLimit *
2b397 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 4f 66 66  /.  int savedOff
2b398 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 53 61 76  set;      /* Sav
2b399 65 64 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69  ed value of p->i
2b39a 4f 66 66 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20  Offset */.  int 
2b39b 6c 61 62 65 6c 43 6d 70 72 3b 20 20 20 20 20 20  labelCmpr;      
2b39c 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74    /* Label for t
2b39d 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
2b39e 6d 65 72 67 65 20 61 6c 67 6f 72 69 74 68 6d 20  merge algorithm 
2b39f 2a 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c 45 6e  */.  int labelEn
2b3a0 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61  d;         /* La
2b3a1 62 65 6c 20 66 6f 72 20 74 68 65 20 65 6e 64 20  bel for the end 
2b3a2 6f 66 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 53  of the overall S
2b3a3 45 4c 45 43 54 20 73 74 6d 74 20 2a 2f 0a 20 20  ELECT stmt */.  
2b3a4 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20  int j1;         
2b3a5 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 69 6e        /* Jump in
2b3a6 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61 74 20  structions that 
2b3a7 67 65 74 20 72 65 74 61 72 67 65 74 74 65 64 20  get retargetted 
2b3a8 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20  */.  int op;    
2b3a9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
2b3aa 65 20 6f 66 20 54 4b 5f 41 4c 4c 2c 20 54 4b 5f  e of TK_ALL, TK_
2b3ab 55 4e 49 4f 4e 2c 20 54 4b 5f 45 58 43 45 50 54  UNION, TK_EXCEPT
2b3ac 2c 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 2a  , TK_INTERSECT *
2b3ad 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  /.  KeyInfo *pKe
2b3ae 79 44 75 70 20 3d 20 30 3b 20 2f 2a 20 43 6f 6d  yDup = 0; /* Com
2b3af 70 61 72 69 73 6f 6e 20 69 6e 66 6f 72 6d 61 74  parison informat
2b3b0 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 61 74  ion for duplicat
2b3b1 65 20 72 65 6d 6f 76 61 6c 20 2a 2f 0a 20 20 4b  e removal */.  K
2b3b2 65 79 49 6e 66 6f 20 2a 70 4b 65 79 4d 65 72 67  eyInfo *pKeyMerg
2b3b3 65 3b 20 20 20 2f 2a 20 43 6f 6d 70 61 72 69 73  e;   /* Comparis
2b3b4 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  on information f
2b3b5 6f 72 20 6d 65 72 67 69 6e 67 20 72 6f 77 73 20  or merging rows 
2b3b6 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
2b3b7 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
2b3b8 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2b3b9 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  n */.  ExprList 
2b3ba 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 2f 2a 20  *pOrderBy;   /* 
2b3bb 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
2b3bc 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72  use */.  int nOr
2b3bd 64 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 2f  derBy;         /
2b3be 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
2b3bf 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42  s in the ORDER B
2b3c0 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  Y clause */.  in
2b3c1 74 20 2a 61 50 65 72 6d 75 74 65 3b 20 20 20 20  t *aPermute;    
2b3c2 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66      /* Mapping f
2b3c3 72 6f 6d 20 4f 52 44 45 52 20 42 59 20 74 65 72  rom ORDER BY ter
2b3c4 6d 73 20 74 6f 20 72 65 73 75 6c 74 20 73 65 74  ms to result set
2b3c5 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 0a 20 20 61   columns */..  a
2b3c6 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64 65 72  ssert( p->pOrder
2b3c7 42 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  By!=0 );.  asser
2b3c8 74 28 20 70 4b 65 79 44 75 70 3d 3d 30 20 29 3b  t( pKeyDup==0 );
2b3c9 20 2f 2a 20 22 4d 61 6e 61 67 65 64 22 20 63 6f   /* "Managed" co
2b3ca 64 65 20 6e 65 65 64 73 20 74 68 69 73 2e 20 20  de needs this.  
2b3cb 54 69 63 6b 65 74 20 23 33 33 38 32 2e 20 2a 2f  Ticket #3382. */
2b3cc 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
2b3cd 64 62 3b 0a 20 20 76 20 3d 20 70 50 61 72 73 65  db;.  v = pParse
2b3ce 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 65 72  ->pVdbe;.  asser
2b3cf 74 28 20 76 21 3d 30 20 29 3b 20 20 20 20 20 20  t( v!=0 );      
2b3d0 20 2f 2a 20 41 6c 72 65 61 64 79 20 74 68 72 6f   /* Already thro
2b3d1 77 6e 20 74 68 65 20 65 72 72 6f 72 20 69 66 20  wn the error if 
2b3d2 56 44 42 45 20 61 6c 6c 6f 63 20 66 61 69 6c 65  VDBE alloc faile
2b3d3 64 20 2a 2f 0a 20 20 6c 61 62 65 6c 45 6e 64 20  d */.  labelEnd 
2b3d4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2b3d5 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 6c 61 62  eLabel(v);.  lab
2b3d6 65 6c 43 6d 70 72 20 3d 20 73 71 6c 69 74 65 33  elCmpr = sqlite3
2b3d7 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
2b3d8 3b 0a 0a 0a 20 20 2f 2a 20 50 61 74 63 68 20 75  ;...  /* Patch u
2b3d9 70 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  p the ORDER BY c
2b3da 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20 6f 70 20  lause.  */.  op 
2b3db 3d 20 70 2d 3e 6f 70 3b 20 20 0a 20 20 70 50 72  = p->op;  .  pPr
2b3dc 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b  ior = p->pPrior;
2b3dd 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f  .  assert( pPrio
2b3de 72 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  r->pOrderBy==0 )
2b3df 3b 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70  ;.  pOrderBy = p
2b3e0 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 61 73  ->pOrderBy;.  as
2b3e1 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 20 29  sert( pOrderBy )
2b3e2 3b 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 70  ;.  nOrderBy = p
2b3e3 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a  OrderBy->nExpr;.
2b3e4 0a 20 20 2f 2a 20 46 6f 72 20 6f 70 65 72 61 74  .  /* For operat
2b3e5 6f 72 73 20 6f 74 68 65 72 20 74 68 61 6e 20 55  ors other than U
2b3e6 4e 49 4f 4e 20 41 4c 4c 20 77 65 20 68 61 76 65  NION ALL we have
2b3e7 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
2b3e8 61 74 0a 20 20 2a 2a 20 74 68 65 20 4f 52 44 45  at.  ** the ORDE
2b3e9 52 20 42 59 20 63 6c 61 75 73 65 20 63 6f 76 65  R BY clause cove
2b3ea 72 73 20 65 76 65 72 79 20 74 65 72 6d 20 6f 66  rs every term of
2b3eb 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
2b3ec 20 20 41 64 64 0a 20 20 2a 2a 20 74 65 72 6d 73    Add.  ** terms
2b3ed 20 74 6f 20 74 68 65 20 4f 52 44 45 52 20 42 59   to the ORDER BY
2b3ee 20 63 6c 61 75 73 65 20 61 73 20 6e 65 63 65 73   clause as neces
2b3ef 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  sary..  */.  if(
2b3f0 20 6f 70 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20   op!=TK_ALL ){. 
2b3f1 20 20 20 66 6f 72 28 69 3d 31 3b 20 64 62 2d 3e     for(i=1; db->
2b3f2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20  mallocFailed==0 
2b3f3 26 26 20 69 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d  && i<=p->pEList-
2b3f4 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
2b3f5 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
2b3f6 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
2b3f7 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20  .      for(j=0, 
2b3f8 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e  pItem=pOrderBy->
2b3f9 61 3b 20 6a 3c 6e 4f 72 64 65 72 42 79 3b 20 6a  a; j<nOrderBy; j
2b3fa 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
2b3fb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
2b3fc 74 65 6d 2d 3e 69 43 6f 6c 3e 30 20 29 3b 0a 20  tem->iCol>0 );. 
2b3fd 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
2b3fe 2d 3e 69 43 6f 6c 3d 3d 69 20 29 20 62 72 65 61  ->iCol==i ) brea
2b3ff 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
2b400 20 69 66 28 20 6a 3d 3d 6e 4f 72 64 65 72 42 79   if( j==nOrderBy
2b401 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72   ){.        Expr
2b402 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *pNew = sqlite3
2b403 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45  Expr(db, TK_INTE
2b404 47 45 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  GER, 0);.       
2b405 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72   if( pNew==0 ) r
2b406 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
2b407 45 4d 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  EM;.        pNew
2b408 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e  ->flags |= EP_In
2b409 74 56 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20  tValue;.        
2b40a 70 4e 65 77 2d 3e 75 2e 69 56 61 6c 75 65 20 3d  pNew->u.iValue =
2b40b 20 69 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 64   i;.        pOrd
2b40c 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78  erBy = sqlite3Ex
2b40d 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2b40e 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70  rse, pOrderBy, p
2b40f 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  New);.        pO
2b410 72 64 65 72 42 79 2d 3e 61 5b 6e 4f 72 64 65 72  rderBy->a[nOrder
2b411 42 79 2b 2b 5d 2e 69 43 6f 6c 20 3d 20 28 75 31  By++].iCol = (u1
2b412 36 29 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  6)i;.      }.   
2b413 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d   }.  }..  /* Com
2b414 70 75 74 65 20 74 68 65 20 63 6f 6d 70 61 72 69  pute the compari
2b415 73 6f 6e 20 70 65 72 6d 75 74 61 74 69 6f 6e 20  son permutation 
2b416 61 6e 64 20 6b 65 79 69 6e 66 6f 20 74 68 61 74  and keyinfo that
2b417 20 69 73 20 75 73 65 64 20 77 69 74 68 0a 20 20   is used with.  
2b418 2a 2a 20 74 68 65 20 70 65 72 6d 75 74 61 74 69  ** the permutati
2b419 6f 6e 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  on used to deter
2b41a 6d 69 6e 65 20 69 66 20 74 68 65 20 6e 65 78 74  mine if the next
2b41b 0a 20 20 2a 2a 20 72 6f 77 20 6f 66 20 72 65 73  .  ** row of res
2b41c 75 6c 74 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20  ults comes from 
2b41d 73 65 6c 65 63 74 41 20 6f 72 20 73 65 6c 65 63  selectA or selec
2b41e 74 42 2e 20 20 41 6c 73 6f 20 61 64 64 20 65 78  tB.  Also add ex
2b41f 70 6c 69 63 69 74 0a 20 20 2a 2a 20 63 6f 6c 6c  plicit.  ** coll
2b420 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 4f 52  ations to the OR
2b421 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74 65  DER BY clause te
2b422 72 6d 73 20 73 6f 20 74 68 61 74 20 77 68 65 6e  rms so that when
2b423 20 74 68 65 20 73 75 62 71 75 65 72 69 65 73 0a   the subqueries.
2b424 20 20 2a 2a 20 74 6f 20 74 68 65 20 72 69 67 68    ** to the righ
2b425 74 20 61 6e 64 20 74 68 65 20 6c 65 66 74 20 61  t and the left a
2b426 72 65 20 65 76 61 6c 75 61 74 65 64 2c 20 74 68  re evaluated, th
2b427 65 79 20 75 73 65 20 74 68 65 20 63 6f 72 72 65  ey use the corre
2b428 63 74 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f  ct.  ** collatio
2b429 6e 2e 0a 20 20 2a 2f 0a 20 20 61 50 65 72 6d 75  n..  */.  aPermu
2b42a 74 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  te = sqlite3DbMa
2b42b 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
2b42c 6f 66 28 69 6e 74 29 2a 6e 4f 72 64 65 72 42 79  of(int)*nOrderBy
2b42d 29 3b 0a 20 20 69 66 28 20 61 50 65 72 6d 75 74  );.  if( aPermut
2b42e 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  e ){.    struct 
2b42f 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
2b430 49 74 65 6d 3b 0a 20 20 20 20 66 6f 72 28 69 3d  Item;.    for(i=
2b431 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42  0, pItem=pOrderB
2b432 79 2d 3e 61 3b 20 69 3c 6e 4f 72 64 65 72 42 79  y->a; i<nOrderBy
2b433 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
2b434 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2b435 49 74 65 6d 2d 3e 69 43 6f 6c 3e 30 20 20 26 26  Item->iCol>0  &&
2b436 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3c 3d 70 2d   pItem->iCol<=p-
2b437 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  >pEList->nExpr )
2b438 3b 0a 20 20 20 20 20 20 61 50 65 72 6d 75 74 65  ;.      aPermute
2b439 5b 69 5d 20 3d 20 70 49 74 65 6d 2d 3e 69 43 6f  [i] = pItem->iCo
2b43a 6c 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  l - 1;.    }.   
2b43b 20 70 4b 65 79 4d 65 72 67 65 20 3d 0a 20 20 20   pKeyMerge =.   
2b43c 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c     sqlite3DbMall
2b43d 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
2b43e 28 2a 70 4b 65 79 4d 65 72 67 65 29 2b 6e 4f 72  (*pKeyMerge)+nOr
2b43f 64 65 72 42 79 2a 28 73 69 7a 65 6f 66 28 43 6f  derBy*(sizeof(Co
2b440 6c 6c 53 65 71 2a 29 2b 31 29 29 3b 0a 20 20 20  llSeq*)+1));.   
2b441 20 69 66 28 20 70 4b 65 79 4d 65 72 67 65 20 29   if( pKeyMerge )
2b442 7b 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67  {.      pKeyMerg
2b443 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20  e->aSortOrder = 
2b444 28 75 38 2a 29 26 70 4b 65 79 4d 65 72 67 65 2d  (u8*)&pKeyMerge-
2b445 3e 61 43 6f 6c 6c 5b 6e 4f 72 64 65 72 42 79 5d  >aColl[nOrderBy]
2b446 3b 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67  ;.      pKeyMerg
2b447 65 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36  e->nField = (u16
2b448 29 6e 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 20  )nOrderBy;.     
2b449 20 70 4b 65 79 4d 65 72 67 65 2d 3e 65 6e 63 20   pKeyMerge->enc 
2b44a 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 20  = ENC(db);.     
2b44b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64   for(i=0; i<nOrd
2b44c 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  erBy; i++){.    
2b44d 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
2b44e 6c 6c 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72  ll;.        Expr
2b44f 20 2a 70 54 65 72 6d 20 3d 20 70 4f 72 64 65 72   *pTerm = pOrder
2b450 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  By->a[i].pExpr;.
2b451 20 20 20 20 20 20 20 20 69 66 28 20 70 54 65 72          if( pTer
2b452 6d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  m->flags & EP_Ex
2b453 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20  pCollate ){.    
2b454 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 54        pColl = pT
2b455 65 72 6d 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20  erm->pColl;.    
2b456 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2b457 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 6d 75 6c       pColl = mul
2b458 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28  tiSelectCollSeq(
2b459 70 50 61 72 73 65 2c 20 70 2c 20 61 50 65 72 6d  pParse, p, aPerm
2b45a 75 74 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20  ute[i]);.       
2b45b 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20     pTerm->flags 
2b45c 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65  |= EP_ExpCollate
2b45d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65 72  ;.          pTer
2b45e 6d 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c  m->pColl = pColl
2b45f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2b460 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61      pKeyMerge->a
2b461 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b  Coll[i] = pColl;
2b462 0a 20 20 20 20 20 20 20 20 70 4b 65 79 4d 65 72  .        pKeyMer
2b463 67 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69  ge->aSortOrder[i
2b464 5d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  ] = pOrderBy->a[
2b465 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20  i].sortOrder;.  
2b466 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
2b467 6c 73 65 7b 0a 20 20 20 20 70 4b 65 79 4d 65 72  lse{.    pKeyMer
2b468 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  ge = 0;.  }..  /
2b469 2a 20 52 65 61 74 74 61 63 68 20 74 68 65 20 4f  * Reattach the O
2b46a 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74  RDER BY clause t
2b46b 6f 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20 2a  o the query..  *
2b46c 2f 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20  /.  p->pOrderBy 
2b46d 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 50  = pOrderBy;.  pP
2b46e 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20 3d  rior->pOrderBy =
2b46f 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2b470 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  Dup(pParse->db, 
2b471 70 4f 72 64 65 72 42 79 2c 20 30 29 3b 0a 0a 20  pOrderBy, 0);.. 
2b472 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 72   /* Allocate a r
2b473 61 6e 67 65 20 6f 66 20 74 65 6d 70 6f 72 61 72  ange of temporar
2b474 79 20 72 65 67 69 73 74 65 72 73 20 61 6e 64 20  y registers and 
2b475 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e 65 65 64  the KeyInfo need
2b476 65 64 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20  ed.  ** for the 
2b477 6c 6f 67 69 63 20 74 68 61 74 20 72 65 6d 6f 76  logic that remov
2b478 65 73 20 64 75 70 6c 69 63 61 74 65 20 72 65 73  es duplicate res
2b479 75 6c 74 20 72 6f 77 73 20 77 68 65 6e 20 74 68  ult rows when th
2b47a 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20  e.  ** operator 
2b47b 69 73 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54  is UNION, EXCEPT
2b47c 2c 20 6f 72 20 49 4e 54 45 52 53 45 43 54 20 28  , or INTERSECT (
2b47d 62 75 74 20 6e 6f 74 20 55 4e 49 4f 4e 20 41 4c  but not UNION AL
2b47e 4c 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f  L)..  */.  if( o
2b47f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20  p==TK_ALL ){.   
2b480 20 72 65 67 50 72 65 76 20 3d 20 30 3b 0a 20 20   regPrev = 0;.  
2b481 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e  }else{.    int n
2b482 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74  Expr = p->pEList
2b483 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73 73  ->nExpr;.    ass
2b484 65 72 74 28 20 6e 4f 72 64 65 72 42 79 3e 3d 6e  ert( nOrderBy>=n
2b485 45 78 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  Expr || db->mall
2b486 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
2b487 72 65 67 50 72 65 76 20 3d 20 73 71 6c 69 74 65  regPrev = sqlite
2b488 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
2b489 61 72 73 65 2c 20 6e 45 78 70 72 2b 31 29 3b 0a  arse, nExpr+1);.
2b48a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b48b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2b48c 67 65 72 2c 20 30 2c 20 72 65 67 50 72 65 76 29  ger, 0, regPrev)
2b48d 3b 0a 20 20 20 20 70 4b 65 79 44 75 70 20 3d 20  ;.    pKeyDup = 
2b48e 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2b48f 65 72 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20  ero(db,.        
2b490 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
2b491 28 2a 70 4b 65 79 44 75 70 29 20 2b 20 6e 45 78  (*pKeyDup) + nEx
2b492 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  pr*(sizeof(CollS
2b493 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20 20 20 69  eq*)+1) );.    i
2b494 66 28 20 70 4b 65 79 44 75 70 20 29 7b 0a 20 20  f( pKeyDup ){.  
2b495 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 61 53 6f      pKeyDup->aSo
2b496 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26  rtOrder = (u8*)&
2b497 70 4b 65 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 6e  pKeyDup->aColl[n
2b498 45 78 70 72 5d 3b 0a 20 20 20 20 20 20 70 4b 65  Expr];.      pKe
2b499 79 44 75 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 28  yDup->nField = (
2b49a 75 31 36 29 6e 45 78 70 72 3b 0a 20 20 20 20 20  u16)nExpr;.     
2b49b 20 70 4b 65 79 44 75 70 2d 3e 65 6e 63 20 3d 20   pKeyDup->enc = 
2b49c 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 20 20 66  ENC(db);.      f
2b49d 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b  or(i=0; i<nExpr;
2b49e 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70   i++){.        p
2b49f 4b 65 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 69 5d  KeyDup->aColl[i]
2b4a0 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f   = multiSelectCo
2b4a1 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2c  llSeq(pParse, p,
2b4a2 20 69 29 3b 0a 20 20 20 20 20 20 20 20 70 4b 65   i);.        pKe
2b4a3 79 44 75 70 2d 3e 61 53 6f 72 74 4f 72 64 65 72  yDup->aSortOrder
2b4a4 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  [i] = 0;.      }
2b4a5 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 0a 20 20 2f  .    }.  }. .  /
2b4a6 2a 20 53 65 70 61 72 61 74 65 20 74 68 65 20 6c  * Separate the l
2b4a7 65 66 74 20 61 6e 64 20 74 68 65 20 72 69 67 68  eft and the righ
2b4a8 74 20 71 75 65 72 79 20 66 72 6f 6d 20 6f 6e 65  t query from one
2b4a9 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2f 0a 20 20   another.  */.  
2b4aa 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20  p->pPrior = 0;. 
2b4ab 20 70 50 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d   pPrior->pRightm
2b4ac 6f 73 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  ost = 0;.  sqlit
2b4ad 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72  e3ResolveOrderGr
2b4ae 6f 75 70 42 79 28 70 50 61 72 73 65 2c 20 70 2c  oupBy(pParse, p,
2b4af 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f   p->pOrderBy, "O
2b4b0 52 44 45 52 22 29 3b 0a 20 20 69 66 28 20 70 50  RDER");.  if( pP
2b4b1 72 69 6f 72 2d 3e 70 50 72 69 6f 72 3d 3d 30 20  rior->pPrior==0 
2b4b2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  ){.    sqlite3Re
2b4b3 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
2b4b4 79 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72  y(pParse, pPrior
2b4b5 2c 20 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72  , pPrior->pOrder
2b4b6 42 79 2c 20 22 4f 52 44 45 52 22 29 3b 0a 20 20  By, "ORDER");.  
2b4b7 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20  }..  /* Compute 
2b4b8 74 68 65 20 6c 69 6d 69 74 20 72 65 67 69 73 74  the limit regist
2b4b9 65 72 73 20 2a 2f 0a 20 20 63 6f 6d 70 75 74 65  ers */.  compute
2b4ba 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
2b4bb 50 61 72 73 65 2c 20 70 2c 20 6c 61 62 65 6c 45  Parse, p, labelE
2b4bc 6e 64 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c  nd);.  if( p->iL
2b4bd 69 6d 69 74 20 26 26 20 6f 70 3d 3d 54 4b 5f 41  imit && op==TK_A
2b4be 4c 4c 20 29 7b 0a 20 20 20 20 72 65 67 4c 69 6d  LL ){.    regLim
2b4bf 69 74 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  itA = ++pParse->
2b4c0 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67 4c 69 6d  nMem;.    regLim
2b4c1 69 74 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  itB = ++pParse->
2b4c2 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
2b4c3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b4c4 50 5f 43 6f 70 79 2c 20 70 2d 3e 69 4f 66 66 73  P_Copy, p->iOffs
2b4c5 65 74 20 3f 20 70 2d 3e 69 4f 66 66 73 65 74 2b  et ? p->iOffset+
2b4c6 31 20 3a 20 70 2d 3e 69 4c 69 6d 69 74 2c 0a 20  1 : p->iLimit,. 
2b4c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4c9 20 72 65 67 4c 69 6d 69 74 41 29 3b 0a 20 20 20   regLimitA);.   
2b4ca 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b4cb 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72  p2(v, OP_Copy, r
2b4cc 65 67 4c 69 6d 69 74 41 2c 20 72 65 67 4c 69 6d  egLimitA, regLim
2b4cd 69 74 42 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  itB);.  }else{. 
2b4ce 20 20 20 72 65 67 4c 69 6d 69 74 41 20 3d 20 72     regLimitA = r
2b4cf 65 67 4c 69 6d 69 74 42 20 3d 20 30 3b 0a 20 20  egLimitB = 0;.  
2b4d0 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  }.  sqlite3ExprD
2b4d1 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69  elete(db, p->pLi
2b4d2 6d 69 74 29 3b 0a 20 20 70 2d 3e 70 4c 69 6d 69  mit);.  p->pLimi
2b4d3 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  t = 0;.  sqlite3
2b4d4 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2b4d5 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 20 20 70 2d  ->pOffset);.  p-
2b4d6 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 0a 20  >pOffset = 0;.. 
2b4d7 20 72 65 67 41 64 64 72 41 20 3d 20 2b 2b 70 50   regAddrA = ++pP
2b4d8 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65  arse->nMem;.  re
2b4d9 67 45 6f 66 41 20 3d 20 2b 2b 70 50 61 72 73 65  gEofA = ++pParse
2b4da 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 41 64 64  ->nMem;.  regAdd
2b4db 72 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  rB = ++pParse->n
2b4dc 4d 65 6d 3b 0a 20 20 72 65 67 45 6f 66 42 20 3d  Mem;.  regEofB =
2b4dd 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2b4de 0a 20 20 72 65 67 4f 75 74 41 20 3d 20 2b 2b 70  .  regOutA = ++p
2b4df 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72  Parse->nMem;.  r
2b4e0 65 67 4f 75 74 42 20 3d 20 2b 2b 70 50 61 72 73  egOutB = ++pPars
2b4e1 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74  e->nMem;.  sqlit
2b4e2 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
2b4e3 28 26 64 65 73 74 41 2c 20 53 52 54 5f 43 6f 72  (&destA, SRT_Cor
2b4e4 6f 75 74 69 6e 65 2c 20 72 65 67 41 64 64 72 41  outine, regAddrA
2b4e5 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
2b4e6 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
2b4e7 42 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  B, SRT_Coroutine
2b4e8 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 0a 20 20  , regAddrB);..  
2b4e9 2f 2a 20 4a 75 6d 70 20 70 61 73 74 20 74 68 65  /* Jump past the
2b4ea 20 76 61 72 69 6f 75 73 20 73 75 62 72 6f 75 74   various subrout
2b4eb 69 6e 65 73 20 61 6e 64 20 63 6f 72 6f 75 74 69  ines and corouti
2b4ec 6e 65 73 20 74 6f 20 74 68 65 20 6d 61 69 6e 0a  nes to the main.
2b4ed 20 20 2a 2a 20 6d 65 72 67 65 20 6c 6f 6f 70 0a    ** merge loop.
2b4ee 20 20 2a 2f 0a 20 20 6a 31 20 3d 20 73 71 6c 69    */.  j1 = sqli
2b4ef 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
2b4f0 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 61 64 64   OP_Goto);.  add
2b4f1 72 53 65 6c 65 63 74 41 20 3d 20 73 71 6c 69 74  rSelectA = sqlit
2b4f2 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2b4f3 72 28 76 29 3b 0a 0a 0a 20 20 2f 2a 20 47 65 6e  r(v);...  /* Gen
2b4f4 65 72 61 74 65 20 61 20 63 6f 72 6f 75 74 69 6e  erate a coroutin
2b4f5 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68  e to evaluate th
2b4f6 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2b4f7 6e 74 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 6c  nt to the.  ** l
2b4f8 65 66 74 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f  eft of the compo
2b4f9 75 6e 64 20 6f 70 65 72 61 74 6f 72 20 2d 20 74  und operator - t
2b4fa 68 65 20 22 41 22 20 73 65 6c 65 63 74 2e 0a 20  he "A" select.. 
2b4fb 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f   */.  VdbeNoopCo
2b4fc 6d 6d 65 6e 74 28 28 76 2c 20 22 42 65 67 69 6e  mment((v, "Begin
2b4fd 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c   coroutine for l
2b4fe 65 66 74 20 53 45 4c 45 43 54 22 29 29 3b 0a 20  eft SELECT"));. 
2b4ff 20 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 20   pPrior->iLimit 
2b500 3d 20 72 65 67 4c 69 6d 69 74 41 3b 0a 20 20 73  = regLimitA;.  s
2b501 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
2b502 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 64 65  rse, pPrior, &de
2b503 73 74 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  stA);.  sqlite3V
2b504 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b505 49 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 45  Integer, 1, regE
2b506 6f 66 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ofA);.  sqlite3V
2b507 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2b508 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29  Yield, regAddrA)
2b509 3b 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ;.  VdbeNoopComm
2b50a 65 6e 74 28 28 76 2c 20 22 45 6e 64 20 63 6f 72  ent((v, "End cor
2b50b 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20  outine for left 
2b50c 53 45 4c 45 43 54 22 29 29 3b 0a 0a 20 20 2f 2a  SELECT"));..  /*
2b50d 20 47 65 6e 65 72 61 74 65 20 61 20 63 6f 72 6f   Generate a coro
2b50e 75 74 69 6e 65 20 74 6f 20 65 76 61 6c 75 61 74  utine to evaluat
2b50f 65 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  e the SELECT sta
2b510 74 65 6d 65 6e 74 20 6f 6e 20 0a 20 20 2a 2a 20  tement on .  ** 
2b511 74 68 65 20 72 69 67 68 74 20 2d 20 74 68 65 20  the right - the 
2b512 22 42 22 20 73 65 6c 65 63 74 0a 20 20 2a 2f 0a  "B" select.  */.
2b513 20 20 61 64 64 72 53 65 6c 65 63 74 42 20 3d 20    addrSelectB = 
2b514 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2b515 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 56 64 62  ntAddr(v);.  Vdb
2b516 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
2b517 20 22 42 65 67 69 6e 20 63 6f 72 6f 75 74 69 6e   "Begin coroutin
2b518 65 20 66 6f 72 20 72 69 67 68 74 20 53 45 4c 45  e for right SELE
2b519 43 54 22 29 29 3b 0a 20 20 73 61 76 65 64 4c 69  CT"));.  savedLi
2b51a 6d 69 74 20 3d 20 70 2d 3e 69 4c 69 6d 69 74 3b  mit = p->iLimit;
2b51b 0a 20 20 73 61 76 65 64 4f 66 66 73 65 74 20 3d  .  savedOffset =
2b51c 20 70 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 70   p->iOffset;.  p
2b51d 2d 3e 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c 69  ->iLimit = regLi
2b51e 6d 69 74 42 3b 0a 20 20 70 2d 3e 69 4f 66 66 73  mitB;.  p->iOffs
2b51f 65 74 20 3d 20 30 3b 20 20 0a 20 20 73 71 6c 69  et = 0;  .  sqli
2b520 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
2b521 2c 20 70 2c 20 26 64 65 73 74 42 29 3b 0a 20 20  , p, &destB);.  
2b522 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 73 61 76 65  p->iLimit = save
2b523 64 4c 69 6d 69 74 3b 0a 20 20 70 2d 3e 69 4f 66  dLimit;.  p->iOf
2b524 66 73 65 74 20 3d 20 73 61 76 65 64 4f 66 66 73  fset = savedOffs
2b525 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  et;.  sqlite3Vdb
2b526 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2b527 74 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66  teger, 1, regEof
2b528 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  B);.  sqlite3Vdb
2b529 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69  eAddOp1(v, OP_Yi
2b52a 65 6c 64 2c 20 72 65 67 41 64 64 72 42 29 3b 0a  eld, regAddrB);.
2b52b 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
2b52c 74 28 28 76 2c 20 22 45 6e 64 20 63 6f 72 6f 75  t((v, "End corou
2b52d 74 69 6e 65 20 66 6f 72 20 72 69 67 68 74 20 53  tine for right S
2b52e 45 4c 45 43 54 22 29 29 3b 0a 0a 20 20 2f 2a 20  ELECT"));..  /* 
2b52f 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f  Generate a subro
2b530 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75  utine that outpu
2b531 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  ts the current r
2b532 6f 77 20 6f 66 20 74 68 65 20 41 0a 20 20 2a 2a  ow of the A.  **
2b533 20 73 65 6c 65 63 74 20 61 73 20 74 68 65 20 6e   select as the n
2b534 65 78 74 20 6f 75 74 70 75 74 20 72 6f 77 20 6f  ext output row o
2b535 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  f the compound s
2b536 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 56 64  elect..  */.  Vd
2b537 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2b538 2c 20 22 4f 75 74 70 75 74 20 72 6f 75 74 69 6e  , "Output routin
2b539 65 20 66 6f 72 20 41 22 29 29 3b 0a 20 20 61 64  e for A"));.  ad
2b53a 64 72 4f 75 74 41 20 3d 20 67 65 6e 65 72 61 74  drOutA = generat
2b53b 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74 69 6e  eOutputSubroutin
2b53c 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
2b53d 20 20 20 20 20 20 20 20 20 20 20 70 2c 20 26 64             p, &d
2b53e 65 73 74 41 2c 20 70 44 65 73 74 2c 20 72 65 67  estA, pDest, reg
2b53f 4f 75 74 41 2c 0a 20 20 20 20 20 20 20 20 20 20  OutA,.          
2b540 20 20 20 20 20 20 20 72 65 67 50 72 65 76 2c 20         regPrev, 
2b541 70 4b 65 79 44 75 70 2c 20 50 34 5f 4b 45 59 49  pKeyDup, P4_KEYI
2b542 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 6c 61 62  NFO_HANDOFF, lab
2b543 65 6c 45 6e 64 29 3b 0a 20 20 0a 20 20 2f 2a 20  elEnd);.  .  /* 
2b544 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f  Generate a subro
2b545 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75  utine that outpu
2b546 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  ts the current r
2b547 6f 77 20 6f 66 20 74 68 65 20 42 0a 20 20 2a 2a  ow of the B.  **
2b548 20 73 65 6c 65 63 74 20 61 73 20 74 68 65 20 6e   select as the n
2b549 65 78 74 20 6f 75 74 70 75 74 20 72 6f 77 20 6f  ext output row o
2b54a 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  f the compound s
2b54b 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  elect..  */.  if
2b54c 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20  ( op==TK_ALL || 
2b54d 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a  op==TK_UNION ){.
2b54e 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d      VdbeNoopComm
2b54f 65 6e 74 28 28 76 2c 20 22 4f 75 74 70 75 74 20  ent((v, "Output 
2b550 72 6f 75 74 69 6e 65 20 66 6f 72 20 42 22 29 29  routine for B"))
2b551 3b 0a 20 20 20 20 61 64 64 72 4f 75 74 42 20 3d  ;.    addrOutB =
2b552 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74 53   generateOutputS
2b553 75 62 72 6f 75 74 69 6e 65 28 70 50 61 72 73 65  ubroutine(pParse
2b554 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2b555 20 20 20 70 2c 20 26 64 65 73 74 42 2c 20 70 44     p, &destB, pD
2b556 65 73 74 2c 20 72 65 67 4f 75 74 42 2c 0a 20 20  est, regOutB,.  
2b557 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
2b558 65 67 50 72 65 76 2c 20 70 4b 65 79 44 75 70 2c  egPrev, pKeyDup,
2b559 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54   P4_KEYINFO_STAT
2b55a 49 43 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20  IC, labelEnd);. 
2b55b 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74   }..  /* Generat
2b55c 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74  e a subroutine t
2b55d 6f 20 72 75 6e 20 77 68 65 6e 20 74 68 65 20 72  o run when the r
2b55e 65 73 75 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65  esults from sele
2b55f 63 74 20 41 0a 20 20 2a 2a 20 61 72 65 20 65 78  ct A.  ** are ex
2b560 68 61 75 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79  hausted and only
2b561 20 64 61 74 61 20 69 6e 20 73 65 6c 65 63 74 20   data in select 
2b562 42 20 72 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a  B remains..  */.
2b563 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
2b564 74 28 28 76 2c 20 22 65 6f 66 2d 41 20 73 75 62  t((v, "eof-A sub
2b565 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 69 66  routine"));.  if
2b566 28 20 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20  ( op==TK_EXCEPT 
2b567 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53  || op==TK_INTERS
2b568 45 43 54 20 29 7b 0a 20 20 20 20 61 64 64 72 45  ECT ){.    addrE
2b569 6f 66 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ofA = sqlite3Vdb
2b56a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2b56b 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 45 6e 64 29  to, 0, labelEnd)
2b56c 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20  ;.  }else{  .   
2b56d 20 61 64 64 72 45 6f 66 41 20 3d 20 73 71 6c 69   addrEofA = sqli
2b56e 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b56f 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c   OP_If, regEofB,
2b570 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 20 20   labelEnd);.    
2b571 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b572 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2b573 65 67 4f 75 74 42 2c 20 61 64 64 72 4f 75 74 42  egOutB, addrOutB
2b574 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2b575 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
2b576 69 65 6c 64 2c 20 72 65 67 41 64 64 72 42 29 3b  ield, regAddrB);
2b577 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b578 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2b579 6f 2c 20 30 2c 20 61 64 64 72 45 6f 66 41 29 3b  o, 0, addrEofA);
2b57a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
2b57b 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65  ate a subroutine
2b57c 20 74 6f 20 72 75 6e 20 77 68 65 6e 20 74 68 65   to run when the
2b57d 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 73 65   results from se
2b57e 6c 65 63 74 20 42 0a 20 20 2a 2a 20 61 72 65 20  lect B.  ** are 
2b57f 65 78 68 61 75 73 74 65 64 20 61 6e 64 20 6f 6e  exhausted and on
2b580 6c 79 20 64 61 74 61 20 69 6e 20 73 65 6c 65 63  ly data in selec
2b581 74 20 41 20 72 65 6d 61 69 6e 73 2e 0a 20 20 2a  t A remains..  *
2b582 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49  /.  if( op==TK_I
2b583 4e 54 45 52 53 45 43 54 20 29 7b 0a 20 20 20 20  NTERSECT ){.    
2b584 61 64 64 72 45 6f 66 42 20 3d 20 61 64 64 72 45  addrEofB = addrE
2b585 6f 66 41 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a  ofA;.  }else{  .
2b586 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d      VdbeNoopComm
2b587 65 6e 74 28 28 76 2c 20 22 65 6f 66 2d 42 20 73  ent((v, "eof-B s
2b588 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20  ubroutine"));.  
2b589 20 20 61 64 64 72 45 6f 66 42 20 3d 20 73 71 6c    addrEofB = sql
2b58a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b58b 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 41  , OP_If, regEofA
2b58c 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 20  , labelEnd);.   
2b58d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b58e 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
2b58f 72 65 67 4f 75 74 41 2c 20 61 64 64 72 4f 75 74  regOutA, addrOut
2b590 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  A);.    sqlite3V
2b591 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2b592 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29  Yield, regAddrA)
2b593 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2b594 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2b595 74 6f 2c 20 30 2c 20 61 64 64 72 45 6f 66 42 29  to, 0, addrEofB)
2b596 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65  ;.  }..  /* Gene
2b597 72 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61 6e  rate code to han
2b598 64 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66 20  dle the case of 
2b599 41 3c 42 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e  A<B.  */.  VdbeN
2b59a 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
2b59b 41 2d 6c 74 2d 42 20 73 75 62 72 6f 75 74 69 6e  A-lt-B subroutin
2b59c 65 22 29 29 3b 0a 20 20 61 64 64 72 41 6c 74 42  e"));.  addrAltB
2b59d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2b59e 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
2b59f 2c 20 72 65 67 4f 75 74 41 2c 20 61 64 64 72 4f  , regOutA, addrO
2b5a0 75 74 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  utA);.  sqlite3V
2b5a1 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2b5a2 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29  Yield, regAddrA)
2b5a3 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2b5a4 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
2b5a5 72 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66  regEofA, addrEof
2b5a6 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  A);.  sqlite3Vdb
2b5a7 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2b5a8 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72  to, 0, labelCmpr
2b5a9 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  );..  /* Generat
2b5aa 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65  e code to handle
2b5ab 20 74 68 65 20 63 61 73 65 20 6f 66 20 41 3d 3d   the case of A==
2b5ac 42 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d  B.  */.  if( op=
2b5ad 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 61  =TK_ALL ){.    a
2b5ae 64 64 72 41 65 71 42 20 3d 20 61 64 64 72 41 6c  ddrAeqB = addrAl
2b5af 74 42 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  tB;.  }else if( 
2b5b0 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54  op==TK_INTERSECT
2b5b1 20 29 7b 0a 20 20 20 20 61 64 64 72 41 65 71 42   ){.    addrAeqB
2b5b2 20 3d 20 61 64 64 72 41 6c 74 42 3b 0a 20 20 20   = addrAltB;.   
2b5b3 20 61 64 64 72 41 6c 74 42 2b 2b 3b 0a 20 20 7d   addrAltB++;.  }
2b5b4 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 4e 6f  else{.    VdbeNo
2b5b5 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41  opComment((v, "A
2b5b6 2d 65 71 2d 42 20 73 75 62 72 6f 75 74 69 6e 65  -eq-B subroutine
2b5b7 22 29 29 3b 0a 20 20 20 20 61 64 64 72 41 65 71  "));.    addrAeq
2b5b8 42 20 3d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  B =.    sqlite3V
2b5b9 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2b5ba 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29  Yield, regAddrA)
2b5bb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2b5bc 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
2b5bd 2c 20 72 65 67 45 6f 66 41 2c 20 61 64 64 72 45  , regEofA, addrE
2b5be 6f 66 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ofA);.    sqlite
2b5bf 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b5c0 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c  P_Goto, 0, label
2b5c1 43 6d 70 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Cmpr);.  }..  /*
2b5c2 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2b5c3 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 63 61 73  o handle the cas
2b5c4 65 20 6f 66 20 41 3e 42 0a 20 20 2a 2f 0a 20 20  e of A>B.  */.  
2b5c5 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
2b5c6 28 76 2c 20 22 41 2d 67 74 2d 42 20 73 75 62 72  (v, "A-gt-B subr
2b5c7 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 61 64 64  outine"));.  add
2b5c8 72 41 67 74 42 20 3d 20 73 71 6c 69 74 65 33 56  rAgtB = sqlite3V
2b5c9 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2b5ca 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  );.  if( op==TK_
2b5cb 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 4e  ALL || op==TK_UN
2b5cc 49 4f 4e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ION ){.    sqlit
2b5cd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b5ce 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74  OP_Gosub, regOut
2b5cf 42 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a 20 20  B, addrOutB);.  
2b5d0 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
2b5d1 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
2b5d2 64 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20  d, regAddrB);.  
2b5d3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b5d4 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45  2(v, OP_If, regE
2b5d5 6f 66 42 2c 20 61 64 64 72 45 6f 66 42 29 3b 0a  ofB, addrEofB);.
2b5d6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b5d7 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
2b5d8 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a  0, labelCmpr);..
2b5d9 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 72    /* This code r
2b5da 75 6e 73 20 6f 6e 63 65 20 74 6f 20 69 6e 69 74  uns once to init
2b5db 69 61 6c 69 7a 65 20 65 76 65 72 79 74 68 69 6e  ialize everythin
2b5dc 67 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  g..  */.  sqlite
2b5dd 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2b5de 20 6a 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56   j1);.  sqlite3V
2b5df 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b5e0 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 45  Integer, 0, regE
2b5e1 6f 66 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ofA);.  sqlite3V
2b5e2 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b5e3 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 45  Integer, 0, regE
2b5e4 6f 66 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ofB);.  sqlite3V
2b5e5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b5e6 47 6f 73 75 62 2c 20 72 65 67 41 64 64 72 41 2c  Gosub, regAddrA,
2b5e7 20 61 64 64 72 53 65 6c 65 63 74 41 29 3b 0a 20   addrSelectA);. 
2b5e8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b5e9 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
2b5ea 72 65 67 41 64 64 72 42 2c 20 61 64 64 72 53 65  regAddrB, addrSe
2b5eb 6c 65 63 74 42 29 3b 0a 20 20 73 71 6c 69 74 65  lectB);.  sqlite
2b5ec 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b5ed 50 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c 20 61  P_If, regEofA, a
2b5ee 64 64 72 45 6f 66 41 29 3b 0a 20 20 73 71 6c 69  ddrEofA);.  sqli
2b5ef 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b5f0 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c   OP_If, regEofB,
2b5f1 20 61 64 64 72 45 6f 66 42 29 3b 0a 0a 20 20 2f   addrEofB);..  /
2b5f2 2a 20 49 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  * Implement the 
2b5f3 6d 61 69 6e 20 6d 65 72 67 65 20 6c 6f 6f 70 0a  main merge loop.
2b5f4 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
2b5f5 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2b5f6 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 20 20  , labelCmpr);.  
2b5f7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b5f8 34 28 76 2c 20 4f 50 5f 50 65 72 6d 75 74 61 74  4(v, OP_Permutat
2b5f9 69 6f 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63  ion, 0, 0, 0, (c
2b5fa 68 61 72 2a 29 61 50 65 72 6d 75 74 65 2c 20 50  har*)aPermute, P
2b5fb 34 5f 49 4e 54 41 52 52 41 59 29 3b 0a 20 20 73  4_INTARRAY);.  s
2b5fc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2b5fd 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20  (v, OP_Compare, 
2b5fe 64 65 73 74 41 2e 69 4d 65 6d 2c 20 64 65 73 74  destA.iMem, dest
2b5ff 42 2e 69 4d 65 6d 2c 20 6e 4f 72 64 65 72 42 79  B.iMem, nOrderBy
2b600 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2b601 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
2b602 2a 29 70 4b 65 79 4d 65 72 67 65 2c 20 50 34 5f  *)pKeyMerge, P4_
2b603 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
2b604 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2b605 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70  ddOp3(v, OP_Jump
2b606 2c 20 61 64 64 72 41 6c 74 42 2c 20 61 64 64 72  , addrAltB, addr
2b607 41 65 71 42 2c 20 61 64 64 72 41 67 74 42 29 3b  AeqB, addrAgtB);
2b608 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74  ..  /* Release t
2b609 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65  emporary registe
2b60a 72 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65  rs.  */.  if( re
2b60b 67 50 72 65 76 20 29 7b 0a 20 20 20 20 73 71 6c  gPrev ){.    sql
2b60c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2b60d 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
2b60e 50 72 65 76 2c 20 6e 4f 72 64 65 72 42 79 2b 31  Prev, nOrderBy+1
2b60f 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d  );.  }..  /* Jum
2b610 70 20 74 6f 20 74 68 65 20 74 68 69 73 20 70 6f  p to the this po
2b611 69 6e 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  int in order to 
2b612 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 71 75  terminate the qu
2b613 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ery..  */.  sqli
2b614 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2b615 62 65 6c 28 76 2c 20 6c 61 62 65 6c 45 6e 64 29  bel(v, labelEnd)
2b616 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  ;..  /* Set the 
2b617 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74  number of output
2b618 20 63 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20   columns.  */.  
2b619 69 66 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74  if( pDest->eDest
2b61a 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a  ==SRT_Output ){.
2b61b 20 20 20 20 53 65 6c 65 63 74 20 2a 70 46 69 72      Select *pFir
2b61c 73 74 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20  st = pPrior;.   
2b61d 20 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d 3e   while( pFirst->
2b61e 70 50 72 69 6f 72 20 29 20 70 46 69 72 73 74 20  pPrior ) pFirst 
2b61f 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  = pFirst->pPrior
2b620 3b 0a 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f  ;.    generateCo
2b621 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
2b622 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c  , 0, pFirst->pEL
2b623 69 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ist);.  }..  /* 
2b624 52 65 61 73 73 65 6d 62 6c 79 20 74 68 65 20 63  Reassembly the c
2b625 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 73 6f  ompound query so
2b626 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65   that it will be
2b627 20 66 72 65 65 64 20 63 6f 72 72 65 63 74 6c 79   freed correctly
2b628 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 63 61 6c  .  ** by the cal
2b629 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ling function */
2b62a 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72  .  if( p->pPrior
2b62b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
2b62c 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
2b62d 70 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a  p->pPrior);.  }.
2b62e 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50    p->pPrior = pP
2b62f 72 69 6f 72 3b 0a 0a 20 20 2f 2a 2a 2a 20 54 42  rior;..  /*** TB
2b630 44 3a 20 20 49 6e 73 65 72 74 20 73 75 62 72 6f  D:  Insert subro
2b631 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 6f 20 63  utine calls to c
2b632 6c 6f 73 65 20 63 75 72 73 6f 72 73 20 6f 6e 20  lose cursors on 
2b633 69 6e 63 6f 6d 70 6c 65 74 65 0a 20 20 2a 2a 2a  incomplete.  ***
2b634 2a 20 73 75 62 71 75 65 72 69 65 73 20 2a 2a 2a  * subqueries ***
2b635 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  */.  return SQLI
2b636 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
2b637 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2b638 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2b639 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  RY) || !defined(
2b63a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2b63b 29 0a 2f 2a 20 46 6f 72 77 61 72 64 20 44 65 63  )./* Forward Dec
2b63c 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 73 74 61  larations */.sta
2b63d 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 45 78  tic void substEx
2b63e 70 72 4c 69 73 74 28 73 71 6c 69 74 65 33 2a 2c  prList(sqlite3*,
2b63f 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c   ExprList*, int,
2b640 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61   ExprList*);.sta
2b641 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 53 65  tic void substSe
2b642 6c 65 63 74 28 73 71 6c 69 74 65 33 2a 2c 20 53  lect(sqlite3*, S
2b643 65 6c 65 63 74 20 2a 2c 20 69 6e 74 2c 20 45 78  elect *, int, Ex
2b644 70 72 4c 69 73 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a  prList *);../*.*
2b645 2a 20 53 63 61 6e 20 74 68 72 6f 75 67 68 20 74  * Scan through t
2b646 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  he expression pE
2b647 78 70 72 2e 20 20 52 65 70 6c 61 63 65 20 65 76  xpr.  Replace ev
2b648 65 72 79 20 72 65 66 65 72 65 6e 63 65 20 74 6f  ery reference to
2b649 0a 2a 2a 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20  .** a column in 
2b64a 74 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 54 61  table number iTa
2b64b 62 6c 65 20 77 69 74 68 20 61 20 63 6f 70 79 20  ble with a copy 
2b64c 6f 66 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74  of the iColumn-t
2b64d 68 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 70 45  h.** entry in pE
2b64e 4c 69 73 74 2e 20 20 28 42 75 74 20 6c 65 61 76  List.  (But leav
2b64f 65 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  e references to 
2b650 74 68 65 20 52 4f 57 49 44 20 63 6f 6c 75 6d 6e  the ROWID column
2b651 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 29   .** unchanged.)
2b652 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2b653 69 6e 65 20 69 73 20 70 61 72 74 20 6f 66 20 74  ine is part of t
2b654 68 65 20 66 6c 61 74 74 65 6e 69 6e 67 20 70 72  he flattening pr
2b655 6f 63 65 64 75 72 65 2e 20 20 41 20 73 75 62 71  ocedure.  A subq
2b656 75 65 72 79 0a 2a 2a 20 77 68 6f 73 65 20 72 65  uery.** whose re
2b657 73 75 6c 74 20 73 65 74 20 69 73 20 64 65 66 69  sult set is defi
2b658 6e 65 64 20 62 79 20 70 45 4c 69 73 74 20 61 70  ned by pEList ap
2b659 70 65 61 72 73 20 61 73 20 65 6e 74 72 79 20 69  pears as entry i
2b65a 6e 20 74 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c  n the.** FROM cl
2b65b 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
2b65c 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20 56   such that the V
2b65d 44 42 45 20 63 75 72 73 6f 72 20 61 73 73 69 67  DBE cursor assig
2b65e 6e 65 64 20 74 6f 20 74 68 61 74 0a 2a 2a 20 46  ned to that.** F
2b65f 4f 52 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 79  ORM clause entry
2b660 20 69 73 20 69 54 61 62 6c 65 2e 20 20 54 68 69   is iTable.  Thi
2b661 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20 74  s routine make t
2b662 68 65 20 6e 65 63 65 73 73 61 72 79 20 0a 2a 2a  he necessary .**
2b663 20 63 68 61 6e 67 65 73 20 74 6f 20 70 45 78 70   changes to pExp
2b664 72 20 73 6f 20 74 68 61 74 20 69 74 20 72 65 66  r so that it ref
2b665 65 72 73 20 64 69 72 65 63 74 6c 79 20 74 6f 20  ers directly to 
2b666 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65  the source table
2b667 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 62 71 75  .** of the subqu
2b668 65 72 79 20 72 61 74 68 65 72 20 74 68 65 20 72  ery rather the r
2b669 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
2b66a 20 73 75 62 71 75 65 72 79 2e 0a 2a 2f 0a 73 74   subquery..*/.st
2b66b 61 74 69 63 20 45 78 70 72 20 2a 73 75 62 73 74  atic Expr *subst
2b66c 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20  Expr(.  sqlite3 
2b66d 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 52  *db,        /* R
2b66e 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72  eport malloc err
2b66f 6f 72 73 20 74 6f 20 74 68 69 73 20 63 6f 6e 6e  ors to this conn
2b670 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72  ection */.  Expr
2b671 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
2b672 2f 2a 20 45 78 70 72 20 69 6e 20 77 68 69 63 68  /* Expr in which
2b673 20 73 75 62 73 74 69 74 75 74 69 6f 6e 20 6f 63   substitution oc
2b674 63 75 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54  curs */.  int iT
2b675 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  able,         /*
2b676 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73 75 62   Table to be sub
2b677 73 74 69 74 75 74 65 64 20 2a 2f 0a 20 20 45 78  stituted */.  Ex
2b678 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20  prList *pEList  
2b679 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20    /* Substitute 
2b67a 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 29  expressions */.)
2b67b 7b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  {.  if( pExpr==0
2b67c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
2b67d 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  f( pExpr->op==TK
2b67e 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45 78 70 72  _COLUMN && pExpr
2b67f 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65  ->iTable==iTable
2b680 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70   ){.    if( pExp
2b681 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a  r->iColumn<0 ){.
2b682 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20        pExpr->op 
2b683 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 7d  = TK_NULL;.    }
2b684 65 6c 73 65 7b 0a 20 20 20 20 20 20 45 78 70 72  else{.      Expr
2b685 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 61 73   *pNew;.      as
2b686 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20  sert( pEList!=0 
2b687 26 26 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  && pExpr->iColum
2b688 6e 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  n<pEList->nExpr 
2b689 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2b68a 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30   pExpr->pLeft==0
2b68b 20 26 26 20 70 45 78 70 72 2d 3e 70 52 69 67 68   && pExpr->pRigh
2b68c 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4e  t==0 );.      pN
2b68d 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
2b68e 44 75 70 28 64 62 2c 20 70 45 4c 69 73 74 2d 3e  Dup(db, pEList->
2b68f 61 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  a[pExpr->iColumn
2b690 5d 2e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 20  ].pExpr, 0);.   
2b691 20 20 20 69 66 28 20 70 4e 65 77 20 26 26 20 70     if( pNew && p
2b692 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0a 20  Expr->pColl ){. 
2b693 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f         pNew->pCo
2b694 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c  ll = pExpr->pCol
2b695 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  l;.      }.     
2b696 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2b697 74 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20  te(db, pExpr);. 
2b698 20 20 20 20 20 70 45 78 70 72 20 3d 20 70 4e 65       pExpr = pNe
2b699 77 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  w;.    }.  }else
2b69a 7b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 65  {.    pExpr->pLe
2b69b 66 74 20 3d 20 73 75 62 73 74 45 78 70 72 28 64  ft = substExpr(d
2b69c 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  b, pExpr->pLeft,
2b69d 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
2b69e 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69  ;.    pExpr->pRi
2b69f 67 68 74 20 3d 20 73 75 62 73 74 45 78 70 72 28  ght = substExpr(
2b6a0 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  db, pExpr->pRigh
2b6a1 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  t, iTable, pELis
2b6a2 74 29 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72  t);.    if( Expr
2b6a3 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2b6a4 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
2b6a5 20 29 7b 0a 20 20 20 20 20 20 73 75 62 73 74 53   ){.      substS
2b6a6 65 6c 65 63 74 28 64 62 2c 20 70 45 78 70 72 2d  elect(db, pExpr-
2b6a7 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 54 61 62  >x.pSelect, iTab
2b6a8 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20  le, pEList);.   
2b6a9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 75   }else{.      su
2b6aa 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20  bstExprList(db, 
2b6ab 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 20  pExpr->x.pList, 
2b6ac 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
2b6ad 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2b6ae 75 72 6e 20 70 45 78 70 72 3b 0a 7d 0a 73 74 61  urn pExpr;.}.sta
2b6af 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 45 78  tic void substEx
2b6b0 70 72 4c 69 73 74 28 0a 20 20 73 71 6c 69 74 65  prList(.  sqlite
2b6b1 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f  3 *db,         /
2b6b2 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20  * Report malloc 
2b6b3 65 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20  errors here */. 
2b6b4 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2b6b5 2c 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f  ,     /* List to
2b6b6 20 73 63 61 6e 20 61 6e 64 20 69 6e 20 77 68 69   scan and in whi
2b6b7 63 68 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74  ch to make subst
2b6b8 69 74 75 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20  itutes */.  int 
2b6b9 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  iTable,         
2b6ba 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20   /* Table to be 
2b6bb 73 75 62 73 74 69 74 75 74 65 64 20 2a 2f 0a 20  substituted */. 
2b6bc 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2b6bd 74 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74  t     /* Substit
2b6be 75 74 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b  ute values */.){
2b6bf 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
2b6c0 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
2b6c1 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  n;.  for(i=0; i<
2b6c2 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  pList->nExpr; i+
2b6c3 2b 29 7b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61  +){.    pList->a
2b6c4 5b 69 5d 2e 70 45 78 70 72 20 3d 20 73 75 62 73  [i].pExpr = subs
2b6c5 74 45 78 70 72 28 64 62 2c 20 70 4c 69 73 74 2d  tExpr(db, pList-
2b6c6 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 69 54 61  >a[i].pExpr, iTa
2b6c7 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2b6c8 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
2b6c9 73 75 62 73 74 53 65 6c 65 63 74 28 0a 20 20 73  substSelect(.  s
2b6ca 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2b6cb 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61      /* Report ma
2b6cc 6c 6c 6f 63 20 65 72 72 6f 72 73 20 68 65 72 65  lloc errors here
2b6cd 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
2b6ce 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 45             /* SE
2b6cf 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69  LECT statement i
2b6d0 6e 20 77 68 69 63 68 20 74 6f 20 6d 61 6b 65 20  n which to make 
2b6d1 73 75 62 73 74 69 74 75 74 69 6f 6e 73 20 2a 2f  substitutions */
2b6d2 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20  .  int iTable,  
2b6d3 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
2b6d4 20 74 6f 20 62 65 20 72 65 70 6c 61 63 65 64 20   to be replaced 
2b6d5 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2b6d6 45 4c 69 73 74 20 20 20 20 20 2f 2a 20 53 75 62  EList     /* Sub
2b6d7 73 74 69 74 75 74 65 20 76 61 6c 75 65 73 20 2a  stitute values *
2b6d8 2f 0a 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a  /.){.  SrcList *
2b6d9 70 53 72 63 3b 0a 20 20 73 74 72 75 63 74 20 53  pSrc;.  struct S
2b6da 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
2b6db 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  em;.  int i;.  i
2b6dc 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a  f( !p ) return;.
2b6dd 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
2b6de 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 69  db, p->pEList, i
2b6df 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
2b6e0 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
2b6e1 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c  db, p->pGroupBy,
2b6e2 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
2b6e3 3b 0a 20 20 73 75 62 73 74 45 78 70 72 4c 69 73  ;.  substExprLis
2b6e4 74 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42  t(db, p->pOrderB
2b6e5 79 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  y, iTable, pELis
2b6e6 74 29 3b 0a 20 20 70 2d 3e 70 48 61 76 69 6e 67  t);.  p->pHaving
2b6e7 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 2c   = substExpr(db,
2b6e8 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20 69 54 61   p->pHaving, iTa
2b6e9 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2b6ea 70 2d 3e 70 57 68 65 72 65 20 3d 20 73 75 62 73  p->pWhere = subs
2b6eb 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 57 68  tExpr(db, p->pWh
2b6ec 65 72 65 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  ere, iTable, pEL
2b6ed 69 73 74 29 3b 0a 20 20 73 75 62 73 74 53 65 6c  ist);.  substSel
2b6ee 65 63 74 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f  ect(db, p->pPrio
2b6ef 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  r, iTable, pELis
2b6f0 74 29 3b 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e  t);.  pSrc = p->
2b6f1 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  pSrc;.  assert( 
2b6f2 70 53 72 63 20 29 3b 20 20 2f 2a 20 45 76 65 6e  pSrc );  /* Even
2b6f3 20 66 6f 72 20 28 53 45 4c 45 43 54 20 31 29 20   for (SELECT 1) 
2b6f4 77 65 20 68 61 76 65 3a 20 70 53 72 63 21 3d 30  we have: pSrc!=0
2b6f5 20 62 75 74 20 70 53 72 63 2d 3e 6e 53 72 63 3d   but pSrc->nSrc=
2b6f6 3d 30 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41  =0 */.  if( ALWA
2b6f7 59 53 28 70 53 72 63 29 20 29 7b 0a 20 20 20 20  YS(pSrc) ){.    
2b6f8 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63  for(i=pSrc->nSrc
2b6f9 2c 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61 3b  , pItem=pSrc->a;
2b6fa 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d   i>0; i--, pItem
2b6fb 2b 2b 29 7b 0a 20 20 20 20 20 20 73 75 62 73 74  ++){.      subst
2b6fc 53 65 6c 65 63 74 28 64 62 2c 20 70 49 74 65 6d  Select(db, pItem
2b6fd 2d 3e 70 53 65 6c 65 63 74 2c 20 69 54 61 62 6c  ->pSelect, iTabl
2b6fe 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  e, pEList);.    
2b6ff 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  }.  }.}.#endif /
2b700 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
2b701 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
2b702 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
2b703 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 2a  ITE_OMIT_VIEW) *
2b704 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
2b705 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2b706 55 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65  UERY) || !define
2b707 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
2b708 45 57 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  EW)./*.** This r
2b709 6f 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73 20  outine attempts 
2b70a 74 6f 20 66 6c 61 74 74 65 6e 20 73 75 62 71 75  to flatten subqu
2b70b 65 72 69 65 73 20 69 6e 20 6f 72 64 65 72 20 74  eries in order t
2b70c 6f 20 73 70 65 65 64 0a 2a 2a 20 65 78 65 63 75  o speed.** execu
2b70d 74 69 6f 6e 2e 20 20 49 74 20 72 65 74 75 72 6e  tion.  It return
2b70e 73 20 31 20 69 66 20 69 74 20 6d 61 6b 65 73 20  s 1 if it makes 
2b70f 63 68 61 6e 67 65 73 20 61 6e 64 20 30 20 69 66  changes and 0 if
2b710 20 6e 6f 20 66 6c 61 74 74 65 6e 69 6e 67 0a 2a   no flattening.*
2b711 2a 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20  * occurs..**.** 
2b712 54 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68  To understand th
2b713 65 20 63 6f 6e 63 65 70 74 20 6f 66 20 66 6c 61  e concept of fla
2b714 74 74 65 6e 69 6e 67 2c 20 63 6f 6e 73 69 64 65  ttening, conside
2b715 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  r the following.
2b716 2a 2a 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20  ** query:.**.** 
2b717 20 20 20 20 53 45 4c 45 43 54 20 61 20 46 52 4f      SELECT a FRO
2b718 4d 20 28 53 45 4c 45 43 54 20 78 2b 79 20 41 53  M (SELECT x+y AS
2b719 20 61 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   a FROM t1 WHERE
2b71a 20 7a 3c 31 30 30 29 20 57 48 45 52 45 20 61 3e   z<100) WHERE a>
2b71b 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  5.**.** The defa
2b71c 75 6c 74 20 77 61 79 20 6f 66 20 69 6d 70 6c 65  ult way of imple
2b71d 6d 65 6e 74 69 6e 67 20 74 68 69 73 20 71 75 65  menting this que
2b71e 72 79 20 69 73 20 74 6f 20 65 78 65 63 75 74 65  ry is to execute
2b71f 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79   the.** subquery
2b720 20 66 69 72 73 74 20 61 6e 64 20 73 74 6f 72 65   first and store
2b721 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20   the results in 
2b722 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  a temporary tabl
2b723 65 2c 20 74 68 65 6e 0a 2a 2a 20 72 75 6e 20 74  e, then.** run t
2b724 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 6f  he outer query o
2b725 6e 20 74 68 61 74 20 74 65 6d 70 6f 72 61 72 79  n that temporary
2b726 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 72 65   table.  This re
2b727 71 75 69 72 65 73 20 74 77 6f 0a 2a 2a 20 70 61  quires two.** pa
2b728 73 73 65 73 20 6f 76 65 72 20 74 68 65 20 64 61  sses over the da
2b729 74 61 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65  ta.  Furthermore
2b72a 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 74 65  , because the te
2b72b 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 2a 2a  mporary table.**
2b72c 20 68 61 73 20 6e 6f 20 69 6e 64 69 63 65 73 2c   has no indices,
2b72d 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2b72e 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 20 71  e on the outer q
2b72f 75 65 72 79 20 63 61 6e 6e 6f 74 20 62 65 0a 2a  uery cannot be.*
2b730 2a 20 6f 70 74 69 6d 69 7a 65 64 2e 0a 2a 2a 0a  * optimized..**.
2b731 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2b732 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 77 72  attempts to rewr
2b733 69 74 65 20 71 75 65 72 69 65 73 20 73 75 63 68  ite queries such
2b734 20 61 73 20 74 68 65 20 61 62 6f 76 65 20 69 6e   as the above in
2b735 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 66  to.** a single f
2b736 6c 61 74 20 73 65 6c 65 63 74 2c 20 6c 69 6b 65  lat select, like
2b737 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
2b738 20 53 45 4c 45 43 54 20 78 2b 79 20 41 53 20 61   SELECT x+y AS a
2b739 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 7a   FROM t1 WHERE z
2b73a 3c 31 30 30 20 41 4e 44 20 61 3e 35 0a 2a 2a 0a  <100 AND a>5.**.
2b73b 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  ** The code gene
2b73c 72 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 73  rated for this s
2b73d 69 6d 70 69 66 69 63 61 74 69 6f 6e 20 67 69 76  impification giv
2b73e 65 73 20 74 68 65 20 73 61 6d 65 20 72 65 73 75  es the same resu
2b73f 6c 74 0a 2a 2a 20 62 75 74 20 6f 6e 6c 79 20 68  lt.** but only h
2b740 61 73 20 74 6f 20 73 63 61 6e 20 74 68 65 20 64  as to scan the d
2b741 61 74 61 20 6f 6e 63 65 2e 20 20 41 6e 64 20 62  ata once.  And b
2b742 65 63 61 75 73 65 20 69 6e 64 69 63 65 73 20 6d  ecause indices m
2b743 69 67 68 74 20 0a 2a 2a 20 65 78 69 73 74 20 6f  ight .** exist o
2b744 6e 20 74 68 65 20 74 61 62 6c 65 20 74 31 2c 20  n the table t1, 
2b745 61 20 63 6f 6d 70 6c 65 74 65 20 73 63 61 6e 20  a complete scan 
2b746 6f 66 20 74 68 65 20 64 61 74 61 20 6d 69 67 68  of the data migh
2b747 74 20 62 65 0a 2a 2a 20 61 76 6f 69 64 65 64 2e  t be.** avoided.
2b748 0a 2a 2a 0a 2a 2a 20 46 6c 61 74 74 65 6e 69 6e  .**.** Flattenin
2b749 67 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70  g is only attemp
2b74a 74 65 64 20 69 66 20 61 6c 6c 20 6f 66 20 74 68  ted if all of th
2b74b 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
2b74c 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31  true:.**.**   (1
2b74d 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2b74e 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71 75  and the outer qu
2b74f 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20  ery do not both 
2b750 75 73 65 20 61 67 67 72 65 67 61 74 65 73 2e 0a  use aggregates..
2b751 2a 2a 0a 2a 2a 20 20 20 28 32 29 20 20 54 68 65  **.**   (2)  The
2b752 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74   subquery is not
2b753 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 6f 72   an aggregate or
2b754 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2b755 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a   is not a join..
2b756 2a 2a 0a 2a 2a 20 20 20 28 33 29 20 20 54 68 65  **.**   (3)  The
2b757 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74   subquery is not
2b758 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   the right opera
2b759 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 74  nd of a left out
2b75a 65 72 20 6a 6f 69 6e 0a 2a 2a 20 20 20 20 20 20  er join.**      
2b75b 20 20 28 4f 72 69 67 69 6e 61 6c 6c 79 20 74 69    (Originally ti
2b75c 63 6b 65 74 20 23 33 30 36 2e 20 20 53 74 72 65  cket #306.  Stre
2b75d 6e 67 68 74 65 6e 65 64 20 62 79 20 74 69 63 6b  nghtened by tick
2b75e 65 74 20 23 33 33 30 30 29 0a 2a 2a 0a 2a 2a 20  et #3300).**.** 
2b75f 20 20 28 34 29 20 20 54 68 65 20 73 75 62 71 75    (4)  The subqu
2b760 65 72 79 20 69 73 20 6e 6f 74 20 44 49 53 54 49  ery is not DISTI
2b761 4e 43 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72  NCT or the outer
2b762 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20   query is not a 
2b763 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 35  join..**.**   (5
2b764 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2b765 69 73 20 6e 6f 74 20 44 49 53 54 49 4e 43 54 20  is not DISTINCT 
2b766 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
2b767 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 0a  ry does not use.
2b768 2a 2a 20 20 20 20 20 20 20 20 61 67 67 72 65 67  **        aggreg
2b769 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36  ates..**.**   (6
2b76a 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2b76b 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 67 67  does not use agg
2b76c 72 65 67 61 74 65 73 20 6f 72 20 74 68 65 20 6f  regates or the o
2b76d 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
2b76e 74 0a 2a 2a 20 20 20 20 20 20 20 20 44 49 53 54  t.**        DIST
2b76f 49 4e 43 54 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 37  INCT..**.**   (7
2b770 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2b771 68 61 73 20 61 20 46 52 4f 4d 20 63 6c 61 75 73  has a FROM claus
2b772 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 38 29 20 20  e..**.**   (8)  
2b773 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65  The subquery doe
2b774 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20  s not use LIMIT 
2b775 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
2b776 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e  ry is not a join
2b777 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 39 29 20 20 54  ..**.**   (9)  T
2b778 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73  he subquery does
2b779 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f   not use LIMIT o
2b77a 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
2b77b 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 0a 2a  y does not use.*
2b77c 2a 20 20 20 20 20 20 20 20 61 67 67 72 65 67 61  *        aggrega
2b77d 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 30 29  tes..**.**  (10)
2b77e 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64    The subquery d
2b77f 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 67 67 72  oes not use aggr
2b780 65 67 61 74 65 73 20 6f 72 20 74 68 65 20 6f 75  egates or the ou
2b781 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e  ter query does n
2b782 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 75 73 65  ot.**        use
2b783 20 4c 49 4d 49 54 2e 0a 2a 2a 0a 2a 2a 20 20 28   LIMIT..**.**  (
2b784 31 31 29 20 20 54 68 65 20 73 75 62 71 75 65 72  11)  The subquer
2b785 79 20 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20  y and the outer 
2b786 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74  query do not bot
2b787 68 20 68 61 76 65 20 4f 52 44 45 52 20 42 59 20  h have ORDER BY 
2b788 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  clauses..**.**  
2b789 28 31 32 29 20 20 4e 6f 74 20 69 6d 70 6c 65 6d  (12)  Not implem
2b78a 65 6e 74 65 64 2e 20 20 53 75 62 73 75 6d 65 64  ented.  Subsumed
2b78b 20 69 6e 74 6f 20 72 65 73 74 72 69 63 74 69 6f   into restrictio
2b78c 6e 20 28 33 29 2e 20 20 57 61 73 20 70 72 65 76  n (3).  Was prev
2b78d 69 6f 75 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20  iously.**       
2b78e 20 61 20 73 65 70 61 72 61 74 65 20 72 65 73 74   a separate rest
2b78f 72 69 63 74 69 6f 6e 20 64 65 72 69 76 69 6e 67  riction deriving
2b790 20 66 72 6f 6d 20 74 69 63 6b 65 74 20 23 33 35   from ticket #35
2b791 30 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 33 29 20 20  0..**.**  (13)  
2b792 54 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64  The subquery and
2b793 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 20   outer query do 
2b794 6e 6f 74 20 62 6f 74 68 20 75 73 65 20 4c 49 4d  not both use LIM
2b795 49 54 0a 2a 2a 0a 2a 2a 20 20 28 31 34 29 20 20  IT.**.**  (14)  
2b796 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65  The subquery doe
2b797 73 20 6e 6f 74 20 75 73 65 20 4f 46 46 53 45 54  s not use OFFSET
2b798 0a 2a 2a 0a 2a 2a 20 20 28 31 35 29 20 20 54 68  .**.**  (15)  Th
2b799 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73  e outer query is
2b79a 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 63   not part of a c
2b79b 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 6f  ompound select o
2b79c 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  r the.**        
2b79d 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
2b79e 74 20 68 61 76 65 20 62 6f 74 68 20 61 6e 20 4f  t have both an O
2b79f 52 44 45 52 20 42 59 20 61 6e 64 20 61 20 4c 49  RDER BY and a LI
2b7a0 4d 49 54 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 20  MIT clause..**  
2b7a1 20 20 20 20 20 20 28 53 65 65 20 74 69 63 6b 65        (See ticke
2b7a2 74 20 23 32 33 33 39 29 0a 2a 2a 0a 2a 2a 20 20  t #2339).**.**  
2b7a3 28 31 36 29 20 20 54 68 65 20 6f 75 74 65 72 20  (16)  The outer 
2b7a4 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 6e 20  query is not an 
2b7a5 61 67 67 72 65 67 61 74 65 20 6f 72 20 74 68 65  aggregate or the
2b7a6 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 0a 2a   subquery does.*
2b7a7 2a 20 20 20 20 20 20 20 20 6e 6f 74 20 63 6f 6e  *        not con
2b7a8 74 61 69 6e 20 4f 52 44 45 52 20 42 59 2e 20 20  tain ORDER BY.  
2b7a9 28 54 69 63 6b 65 74 20 23 32 39 34 32 29 20 20  (Ticket #2942)  
2b7aa 54 68 69 73 20 75 73 65 64 20 74 6f 20 6e 6f 74  This used to not
2b7ab 20 6d 61 74 74 65 72 0a 2a 2a 20 20 20 20 20 20   matter.**      
2b7ac 20 20 75 6e 74 69 6c 20 77 65 20 69 6e 74 72 6f    until we intro
2b7ad 64 75 63 65 64 20 74 68 65 20 67 72 6f 75 70 5f  duced the group_
2b7ae 63 6f 6e 63 61 74 28 29 20 66 75 6e 63 74 69 6f  concat() functio
2b7af 6e 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 28 31 37 29  n.  .**.**  (17)
2b7b0 20 20 54 68 65 20 73 75 62 2d 71 75 65 72 79 20    The sub-query 
2b7b1 69 73 20 6e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e  is not a compoun
2b7b2 64 20 73 65 6c 65 63 74 2c 20 6f 72 20 69 74 20  d select, or it 
2b7b3 69 73 20 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 0a  is a UNION ALL .
2b7b4 2a 2a 20 20 20 20 20 20 20 20 63 6f 6d 70 6f 75  **        compou
2b7b5 6e 64 20 63 6c 61 75 73 65 20 6d 61 64 65 20 75  nd clause made u
2b7b6 70 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 6e 6f  p entirely of no
2b7b7 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75 65 72  n-aggregate quer
2b7b8 69 65 73 2c 20 61 6e 64 20 0a 2a 2a 20 20 20 20  ies, and .**    
2b7b9 20 20 20 20 74 68 65 20 70 61 72 65 6e 74 20 71      the parent q
2b7ba 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  uery:.**.**     
2b7bb 20 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 69 74       * is not it
2b7bc 73 65 6c 66 20 70 61 72 74 20 6f 66 20 61 20 63  self part of a c
2b7bd 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 0a  ompound select,.
2b7be 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20 69 73  **          * is
2b7bf 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74   not an aggregat
2b7c0 65 20 6f 72 20 44 49 53 54 49 4e 43 54 20 71 75  e or DISTINCT qu
2b7c1 65 72 79 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20  ery, and.**     
2b7c2 20 20 20 20 20 2a 20 68 61 73 20 6e 6f 20 6f 74       * has no ot
2b7c3 68 65 72 20 74 61 62 6c 65 73 20 6f 72 20 73 75  her tables or su
2b7c4 62 2d 73 65 6c 65 63 74 73 20 69 6e 20 74 68 65  b-selects in the
2b7c5 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a   FROM clause..**
2b7c6 0a 2a 2a 20 20 20 20 20 20 20 20 54 68 65 20 70  .**        The p
2b7c7 61 72 65 6e 74 20 61 6e 64 20 73 75 62 2d 71 75  arent and sub-qu
2b7c8 65 72 79 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  ery may contain 
2b7c9 57 48 45 52 45 20 63 6c 61 75 73 65 73 2e 20 53  WHERE clauses. S
2b7ca 75 62 6a 65 63 74 20 74 6f 0a 2a 2a 20 20 20 20  ubject to.**    
2b7cb 20 20 20 20 72 75 6c 65 73 20 28 31 31 29 2c 20      rules (11), 
2b7cc 28 31 33 29 20 61 6e 64 20 28 31 34 29 2c 20 74  (13) and (14), t
2b7cd 68 65 79 20 6d 61 79 20 61 6c 73 6f 20 63 6f 6e  hey may also con
2b7ce 74 61 69 6e 20 4f 52 44 45 52 20 42 59 2c 0a 2a  tain ORDER BY,.*
2b7cf 2a 20 20 20 20 20 20 20 20 4c 49 4d 49 54 20 61  *        LIMIT a
2b7d0 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65  nd OFFSET clause
2b7d1 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 38 29 20 20  s..**.**  (18)  
2b7d2 49 66 20 74 68 65 20 73 75 62 2d 71 75 65 72 79  If the sub-query
2b7d3 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73   is a compound s
2b7d4 65 6c 65 63 74 2c 20 74 68 65 6e 20 61 6c 6c 20  elect, then all 
2b7d5 74 65 72 6d 73 20 6f 66 20 74 68 65 0a 2a 2a 20  terms of the.** 
2b7d6 20 20 20 20 20 20 20 4f 52 44 45 52 20 62 79 20         ORDER by 
2b7d7 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 70 61  clause of the pa
2b7d8 72 65 6e 74 20 6d 75 73 74 20 62 65 20 73 69 6d  rent must be sim
2b7d9 70 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20 74  ple references t
2b7da 6f 20 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f 6c  o .**        col
2b7db 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 75 62 2d  umns of the sub-
2b7dc 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 28 31  query..**.**  (1
2b7dd 39 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  9)  The subquery
2b7de 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49   does not use LI
2b7df 4d 49 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72  MIT or the outer
2b7e0 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a   query does not.
2b7e1 2a 2a 20 20 20 20 20 20 20 20 68 61 76 65 20 61  **        have a
2b7e2 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a   WHERE clause..*
2b7e3 2a 0a 2a 2a 20 20 28 32 30 29 20 20 49 66 20 74  *.**  (20)  If t
2b7e4 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 73 20  he sub-query is 
2b7e5 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  a compound selec
2b7e6 74 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 74 20  t, then it must 
2b7e7 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20  not use.**      
2b7e8 20 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c    an ORDER BY cl
2b7e9 61 75 73 65 2e 20 20 54 69 63 6b 65 74 20 23 33  ause.  Ticket #3
2b7ea 37 37 33 2e 20 20 57 65 20 63 6f 75 6c 64 20 72  773.  We could r
2b7eb 65 6c 61 78 20 74 68 69 73 20 63 6f 6e 73 74 72  elax this constr
2b7ec 61 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 73  aint.**        s
2b7ed 6f 6d 65 77 68 61 74 20 62 79 20 73 61 79 69 6e  omewhat by sayin
2b7ee 67 20 74 68 61 74 20 74 68 65 20 74 65 72 6d 73  g that the terms
2b7ef 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2b7f0 20 63 6c 61 75 73 65 20 6d 75 73 74 0a 2a 2a 20   clause must.** 
2b7f1 20 20 20 20 20 20 20 61 70 70 65 61 72 20 61 73         appear as
2b7f2 20 75 6e 6d 6f 64 69 66 69 65 64 20 72 65 73 75   unmodified resu
2b7f3 6c 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  lt columns in th
2b7f4 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 20 20  e outer query.  
2b7f5 42 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 68 61  But.**        ha
2b7f6 76 65 20 6f 74 68 65 72 20 6f 70 74 69 6d 69 7a  ve other optimiz
2b7f7 61 74 69 6f 6e 73 20 69 6e 20 6d 69 6e 64 20 74  ations in mind t
2b7f8 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68 61 74  o deal with that
2b7f9 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20   case..**.** In 
2b7fa 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68  this routine, th
2b7fb 65 20 22 70 22 20 70 61 72 61 6d 65 74 65 72 20  e "p" parameter 
2b7fc 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
2b7fd 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e  the outer query.
2b7fe 0a 2a 2a 20 54 68 65 20 73 75 62 71 75 65 72 79  .** The subquery
2b7ff 20 69 73 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 69   is p->pSrc->a[i
2b800 46 72 6f 6d 5d 2e 20 20 69 73 41 67 67 20 69 73  From].  isAgg is
2b801 20 74 72 75 65 20 69 66 20 74 68 65 20 6f 75 74   true if the out
2b802 65 72 20 71 75 65 72 79 0a 2a 2a 20 75 73 65 73  er query.** uses
2b803 20 61 67 67 72 65 67 61 74 65 73 20 61 6e 64 20   aggregates and 
2b804 73 75 62 71 75 65 72 79 49 73 41 67 67 20 69 73  subqueryIsAgg is
2b805 20 74 72 75 65 20 69 66 20 74 68 65 20 73 75 62   true if the sub
2b806 71 75 65 72 79 20 75 73 65 73 20 61 67 67 72 65  query uses aggre
2b807 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  gates..**.** If 
2b808 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6e 6f  flattening is no
2b809 74 20 61 74 74 65 6d 70 74 65 64 2c 20 74 68 69  t attempted, thi
2b80a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
2b80b 6f 2d 6f 70 20 61 6e 64 20 72 65 74 75 72 6e 73  o-op and returns
2b80c 20 30 2e 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65   0..** If flatte
2b80d 6e 69 6e 67 20 69 73 20 61 74 74 65 6d 70 74 65  ning is attempte
2b80e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  d this routine r
2b80f 65 74 75 72 6e 73 20 31 2e 0a 2a 2a 0a 2a 2a 20  eturns 1..**.** 
2b810 41 6c 6c 20 6f 66 20 74 68 65 20 65 78 70 72 65  All of the expre
2b811 73 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73 20 6d  ssion analysis m
2b812 75 73 74 20 6f 63 63 75 72 20 6f 6e 20 62 6f 74  ust occur on bot
2b813 68 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  h the outer quer
2b814 79 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 75 62  y and.** the sub
2b815 71 75 65 72 79 20 62 65 66 6f 72 65 20 74 68 69  query before thi
2b816 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 2e 0a  s routine runs..
2b817 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c  */.static int fl
2b818 61 74 74 65 6e 53 75 62 71 75 65 72 79 28 0a 20  attenSubquery(. 
2b819 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2b81a 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2b81b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
2b81c 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
2b81d 20 20 20 2f 2a 20 54 68 65 20 70 61 72 65 6e 74     /* The parent
2b81e 20 6f 72 20 6f 75 74 65 72 20 53 45 4c 45 43 54   or outer SELECT
2b81f 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2b820 69 6e 74 20 69 46 72 6f 6d 2c 20 20 20 20 20 20  int iFrom,      
2b821 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
2b822 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f 66   p->pSrc->a[] of
2b823 20 74 68 65 20 69 6e 6e 65 72 20 73 75 62 71 75   the inner subqu
2b824 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41  ery */.  int isA
2b825 67 67 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gg,           /*
2b826 20 54 72 75 65 20 69 66 20 6f 75 74 65 72 20 53   True if outer S
2b827 45 4c 45 43 54 20 75 73 65 73 20 61 67 67 72 65  ELECT uses aggre
2b828 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a  gate functions *
2b829 2f 0a 20 20 69 6e 74 20 73 75 62 71 75 65 72 79  /.  int subquery
2b82a 49 73 41 67 67 20 20 20 20 2f 2a 20 54 72 75 65  IsAgg    /* True
2b82b 20 69 66 20 74 68 65 20 73 75 62 71 75 65 72 79   if the subquery
2b82c 20 75 73 65 73 20 61 67 67 72 65 67 61 74 65 20   uses aggregate 
2b82d 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a  functions */.){.
2b82e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
2b82f 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74 20  avedAuthContext 
2b830 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43  = pParse->zAuthC
2b831 6f 6e 74 65 78 74 3b 0a 20 20 53 65 6c 65 63 74  ontext;.  Select
2b832 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 53 65 6c   *pParent;.  Sel
2b833 65 63 74 20 2a 70 53 75 62 3b 20 20 20 20 20 20  ect *pSub;      
2b834 20 2f 2a 20 54 68 65 20 69 6e 6e 65 72 20 71 75   /* The inner qu
2b835 65 72 79 20 6f 72 20 22 73 75 62 71 75 65 72 79  ery or "subquery
2b836 22 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  " */.  Select *p
2b837 53 75 62 31 3b 20 20 20 20 20 20 2f 2a 20 50 6f  Sub1;      /* Po
2b838 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 69 67  inter to the rig
2b839 68 74 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 6e  htmost select in
2b83a 20 73 75 62 2d 71 75 65 72 79 20 2a 2f 0a 20 20   sub-query */.  
2b83b 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20  SrcList *pSrc;  
2b83c 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20      /* The FROM 
2b83d 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75  clause of the ou
2b83e 74 65 72 20 71 75 65 72 79 20 2a 2f 0a 20 20 53  ter query */.  S
2b83f 72 63 4c 69 73 74 20 2a 70 53 75 62 53 72 63 3b  rcList *pSubSrc;
2b840 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63     /* The FROM c
2b841 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73 75 62  lause of the sub
2b842 71 75 65 72 79 20 2a 2f 0a 20 20 45 78 70 72 4c  query */.  ExprL
2b843 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 2f  ist *pList;    /
2b844 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74  * The result set
2b845 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75   of the outer qu
2b846 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61  ery */.  int iPa
2b847 72 65 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  rent;        /* 
2b848 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62  VDBE cursor numb
2b849 65 72 20 6f 66 20 74 68 65 20 70 53 75 62 20 72  er of the pSub r
2b84a 65 73 75 6c 74 20 73 65 74 20 74 65 6d 70 20 74  esult set temp t
2b84b 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  able */.  int i;
2b84c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b84d 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2b84e 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b  .  Expr *pWhere;
2b84f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b850 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
2b851 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72   clause */.  str
2b852 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2b853 20 2a 70 53 75 62 69 74 65 6d 3b 20 20 20 2f 2a   *pSubitem;   /*
2b854 20 54 68 65 20 73 75 62 71 75 65 72 79 20 2a 2f   The subquery */
2b855 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2b856 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
2b857 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
2b858 69 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73  if flattening is
2b859 20 70 65 72 6d 69 74 74 65 64 2e 20 20 52 65 74   permitted.  Ret
2b85a 75 72 6e 20 30 20 69 66 20 6e 6f 74 2e 0a 20 20  urn 0 if not..  
2b85b 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d  */.  assert( p!=
2b85c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2b85d 2d 3e 70 50 72 69 6f 72 3d 3d 30 20 29 3b 20 20  ->pPrior==0 );  
2b85e 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20 66 6c 61  /* Unable to fla
2b85f 74 74 65 6e 20 63 6f 6d 70 6f 75 6e 64 20 71 75  tten compound qu
2b860 65 72 69 65 73 20 2a 2f 0a 20 20 70 53 72 63 20  eries */.  pSrc 
2b861 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73  = p->pSrc;.  ass
2b862 65 72 74 28 20 70 53 72 63 20 26 26 20 69 46 72  ert( pSrc && iFr
2b863 6f 6d 3e 3d 30 20 26 26 20 69 46 72 6f 6d 3c 70  om>=0 && iFrom<p
2b864 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 70  Src->nSrc );.  p
2b865 53 75 62 69 74 65 6d 20 3d 20 26 70 53 72 63 2d  Subitem = &pSrc-
2b866 3e 61 5b 69 46 72 6f 6d 5d 3b 0a 20 20 69 50 61  >a[iFrom];.  iPa
2b867 72 65 6e 74 20 3d 20 70 53 75 62 69 74 65 6d 2d  rent = pSubitem-
2b868 3e 69 43 75 72 73 6f 72 3b 0a 20 20 70 53 75 62  >iCursor;.  pSub
2b869 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65   = pSubitem->pSe
2b86a 6c 65 63 74 3b 0a 20 20 61 73 73 65 72 74 28 20  lect;.  assert( 
2b86b 70 53 75 62 21 3d 30 20 29 3b 0a 20 20 69 66 28  pSub!=0 );.  if(
2b86c 20 69 73 41 67 67 20 26 26 20 73 75 62 71 75 65   isAgg && subque
2b86d 72 79 49 73 41 67 67 20 29 20 72 65 74 75 72 6e  ryIsAgg ) return
2b86e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2b86f 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2b870 6f 6e 20 28 31 29 20 20 2a 2f 0a 20 20 69 66 28  on (1)  */.  if(
2b871 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20 26   subqueryIsAgg &
2b872 26 20 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 29  & pSrc->nSrc>1 )
2b873 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
2b874 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2b875 6f 6e 20 28 32 29 20 20 2a 2f 0a 20 20 70 53 75  on (2)  */.  pSu
2b876 62 53 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72  bSrc = pSub->pSr
2b877 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 75  c;.  assert( pSu
2b878 62 53 72 63 20 29 3b 0a 20 20 2f 2a 20 50 72 69  bSrc );.  /* Pri
2b879 6f 72 20 74 6f 20 76 65 72 73 69 6f 6e 20 33 2e  or to version 3.
2b87a 31 2e 32 2c 20 77 68 65 6e 20 4c 49 4d 49 54 20  1.2, when LIMIT 
2b87b 61 6e 64 20 4f 46 46 53 45 54 20 68 61 64 20 74  and OFFSET had t
2b87c 6f 20 62 65 20 73 69 6d 70 6c 65 20 63 6f 6e 73  o be simple cons
2b87d 74 61 6e 74 73 2c 0a 20 20 2a 2a 20 6e 6f 74 20  tants,.  ** not 
2b87e 61 72 62 69 74 72 61 72 79 20 65 78 70 72 65 73  arbitrary expres
2b87f 73 73 69 6f 6e 73 2c 20 77 65 20 61 6c 6c 6f 77  ssions, we allow
2b880 65 64 20 73 6f 6d 65 20 63 6f 6d 62 69 6e 69 6e  ed some combinin
2b881 67 20 6f 66 20 4c 49 4d 49 54 20 61 6e 64 20 4f  g of LIMIT and O
2b882 46 46 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 75  FFSET.  ** becau
2b883 73 65 20 74 68 65 79 20 63 6f 75 6c 64 20 62 65  se they could be
2b884 20 63 6f 6d 70 75 74 65 64 20 61 74 20 63 6f 6d   computed at com
2b885 70 69 6c 65 2d 74 69 6d 65 2e 20 20 42 75 74 20  pile-time.  But 
2b886 77 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f  when LIMIT and O
2b887 46 46 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 6d  FFSET.  ** becam
2b888 65 20 61 72 62 69 74 72 61 72 79 20 65 78 70 72  e arbitrary expr
2b889 65 73 73 69 6f 6e 73 2c 20 77 65 20 77 65 72 65  essions, we were
2b88a 20 66 6f 72 63 65 64 20 74 6f 20 61 64 64 20 72   forced to add r
2b88b 65 73 74 72 69 63 74 69 6f 6e 73 20 28 31 33 29  estrictions (13)
2b88c 0a 20 20 2a 2a 20 61 6e 64 20 28 31 34 29 2e 20  .  ** and (14). 
2b88d 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70  */.  if( pSub->p
2b88e 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70 4c 69 6d  Limit && p->pLim
2b88f 69 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  it ) return 0;  
2b890 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2b891 65 73 74 72 69 63 74 69 6f 6e 20 28 31 33 29 20  estriction (13) 
2b892 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70  */.  if( pSub->p
2b893 4f 66 66 73 65 74 20 29 20 72 65 74 75 72 6e 20  Offset ) return 
2b894 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2b895 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2b896 65 73 74 72 69 63 74 69 6f 6e 20 28 31 34 29 20  estriction (14) 
2b897 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 52 69 67  */.  if( p->pRig
2b898 68 74 6d 6f 73 74 20 26 26 20 70 53 75 62 2d 3e  htmost && pSub->
2b899 70 4c 69 6d 69 74 20 26 26 20 70 53 75 62 2d 3e  pLimit && pSub->
2b89a 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2b89b 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
2b89c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b89d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b89e 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
2b89f 69 6f 6e 20 28 31 35 29 20 2a 2f 0a 20 20 7d 0a  ion (15) */.  }.
2b8a0 20 20 69 66 28 20 70 53 75 62 53 72 63 2d 3e 6e    if( pSubSrc->n
2b8a1 53 72 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Src==0 ) return 
2b8a2 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2b8a3 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
2b8a4 72 69 63 74 69 6f 6e 20 28 37 29 20 20 2a 2f 0a  riction (7)  */.
2b8a5 20 20 69 66 28 20 28 28 70 53 75 62 2d 3e 73 65    if( ((pSub->se
2b8a6 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74  lFlags & SF_Dist
2b8a7 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53 75 62  inct)!=0 || pSub
2b8a8 2d 3e 70 4c 69 6d 69 74 29 20 0a 20 20 20 20 20  ->pLimit) .     
2b8a9 20 20 20 20 26 26 20 28 70 53 72 63 2d 3e 6e 53      && (pSrc->nS
2b8aa 72 63 3e 31 20 7c 7c 20 69 73 41 67 67 29 20 29  rc>1 || isAgg) )
2b8ab 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  {          /* Re
2b8ac 73 74 72 69 63 74 69 6f 6e 73 20 28 34 29 28 35  strictions (4)(5
2b8ad 29 28 38 29 28 39 29 20 2a 2f 0a 20 20 20 20 20  )(8)(9) */.     
2b8ae 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
2b8af 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 73  .  }.  if( (p->s
2b8b0 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73  elFlags & SF_Dis
2b8b1 74 69 6e 63 74 29 21 3d 30 20 26 26 20 73 75 62  tinct)!=0 && sub
2b8b2 71 75 65 72 79 49 73 41 67 67 20 29 7b 0a 20 20  queryIsAgg ){.  
2b8b3 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20     return 0;    
2b8b4 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
2b8b5 69 6f 6e 20 28 36 29 20 20 2a 2f 0a 20 20 7d 0a  ion (6)  */.  }.
2b8b6 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42    if( p->pOrderB
2b8b7 79 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65  y && pSub->pOrde
2b8b8 72 42 79 20 29 7b 0a 20 20 20 20 20 72 65 74 75  rBy ){.     retu
2b8b9 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
2b8ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b8bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b8bc 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28  /* Restriction (
2b8bd 31 31 29 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  11) */.  }.  if(
2b8be 20 69 73 41 67 67 20 26 26 20 70 53 75 62 2d 3e   isAgg && pSub->
2b8bf 70 4f 72 64 65 72 42 79 20 29 20 72 65 74 75 72  pOrderBy ) retur
2b8c0 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
2b8c1 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2b8c2 6f 6e 20 28 31 36 29 20 2a 2f 0a 20 20 69 66 28  on (16) */.  if(
2b8c3 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26   pSub->pLimit &&
2b8c4 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74   p->pWhere ) ret
2b8c5 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
2b8c6 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2b8c7 6f 6e 20 28 31 39 29 20 2a 2f 0a 0a 20 20 2f 2a  on (19) */..  /*
2b8c8 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e   OBSOLETE COMMEN
2b8c9 54 20 31 3a 0a 20 20 2a 2a 20 52 65 73 74 72 69  T 1:.  ** Restri
2b8ca 63 74 69 6f 6e 20 33 3a 20 20 49 66 20 74 68 65  ction 3:  If the
2b8cb 20 73 75 62 71 75 65 72 79 20 69 73 20 61 20 6a   subquery is a j
2b8cc 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74  oin, make sure t
2b8cd 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 0a  he subquery is .
2b8ce 20 20 2a 2a 20 6e 6f 74 20 75 73 65 64 20 61 73    ** not used as
2b8cf 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   the right opera
2b8d0 6e 64 20 6f 66 20 61 6e 20 6f 75 74 65 72 20 6a  nd of an outer j
2b8d1 6f 69 6e 2e 20 20 45 78 61 6d 70 6c 65 73 20 6f  oin.  Examples o
2b8d2 66 20 77 68 79 20 74 68 69 73 0a 20 20 2a 2a 20  f why this.  ** 
2b8d3 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a  is not allowed:.
2b8d4 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20    **.  **       
2b8d5 20 20 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20    t1 LEFT OUTER 
2b8d6 4a 4f 49 4e 20 28 74 32 20 4a 4f 49 4e 20 74 33  JOIN (t2 JOIN t3
2b8d7 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77  ).  **.  ** If w
2b8d8 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61 62  e flatten the ab
2b8d9 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67 65  ove, we would ge
2b8da 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  t.  **.  **     
2b8db 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f 55 54      (t1 LEFT OUT
2b8dc 45 52 20 4a 4f 49 4e 20 74 32 29 20 4a 4f 49 4e  ER JOIN t2) JOIN
2b8dd 20 74 33 0a 20 20 2a 2a 0a 20 20 2a 2a 20 77 68   t3.  **.  ** wh
2b8de 69 63 68 20 69 73 20 6e 6f 74 20 61 74 20 61 6c  ich is not at al
2b8df 6c 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67  l the same thing
2b8e0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 42 53 4f  ..  **.  ** OBSO
2b8e1 4c 45 54 45 20 43 4f 4d 4d 45 4e 54 20 32 3a 0a  LETE COMMENT 2:.
2b8e2 20 20 2a 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    ** Restriction
2b8e3 20 31 32 3a 20 20 49 66 20 74 68 65 20 73 75 62   12:  If the sub
2b8e4 71 75 65 72 79 20 69 73 20 74 68 65 20 72 69 67  query is the rig
2b8e5 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20  ht operand of a 
2b8e6 6c 65 66 74 20 6f 75 74 65 72 0a 20 20 2a 2a 20  left outer.  ** 
2b8e7 6a 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20  join, make sure 
2b8e8 74 68 65 20 73 75 62 71 75 65 72 79 20 68 61 73  the subquery has
2b8e9 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65   no WHERE clause
2b8ea 2e 0a 20 20 2a 2a 20 41 6e 20 65 78 61 6d 70 6c  ..  ** An exampl
2b8eb 65 73 20 6f 66 20 77 68 79 20 74 68 69 73 20 69  es of why this i
2b8ec 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20  s not allowed:. 
2b8ed 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   **.  **        
2b8ee 20 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a   t1 LEFT OUTER J
2b8ef 4f 49 4e 20 28 53 45 4c 45 43 54 20 2a 20 46 52  OIN (SELECT * FR
2b8f0 4f 4d 20 74 32 20 57 48 45 52 45 20 74 32 2e 78  OM t2 WHERE t2.x
2b8f1 3e 30 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  >0).  **.  ** If
2b8f2 20 77 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20   we flatten the 
2b8f3 61 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20  above, we would 
2b8f4 67 65 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  get.  **.  **   
2b8f5 20 20 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f        (t1 LEFT O
2b8f6 55 54 45 52 20 4a 4f 49 4e 20 74 32 29 20 57 48  UTER JOIN t2) WH
2b8f7 45 52 45 20 74 32 2e 78 3e 30 0a 20 20 2a 2a 0a  ERE t2.x>0.  **.
2b8f8 20 20 2a 2a 20 42 75 74 20 74 68 65 20 74 32 2e    ** But the t2.
2b8f9 78 3e 30 20 74 65 73 74 20 77 69 6c 6c 20 61 6c  x>0 test will al
2b8fa 77 61 79 73 20 66 61 69 6c 20 6f 6e 20 61 20 4e  ways fail on a N
2b8fb 55 4c 4c 20 72 6f 77 20 6f 66 20 74 32 2c 20 77  ULL row of t2, w
2b8fc 68 69 63 68 0a 20 20 2a 2a 20 65 66 66 65 63 74  hich.  ** effect
2b8fd 69 76 65 6c 79 20 63 6f 6e 76 65 72 74 73 20 74  ively converts t
2b8fe 68 65 20 4f 55 54 45 52 20 4a 4f 49 4e 20 69 6e  he OUTER JOIN in
2b8ff 74 6f 20 61 6e 20 49 4e 4e 45 52 20 4a 4f 49 4e  to an INNER JOIN
2b900 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 48 49 53  ..  **.  ** THIS
2b901 20 4f 56 45 52 52 49 44 45 53 20 4f 42 53 4f 4c   OVERRIDES OBSOL
2b902 45 54 45 20 43 4f 4d 4d 45 4e 54 53 20 31 20 41  ETE COMMENTS 1 A
2b903 4e 44 20 32 20 41 42 4f 56 45 3a 0a 20 20 2a 2a  ND 2 ABOVE:.  **
2b904 20 54 69 63 6b 65 74 20 23 33 33 30 30 20 73 68   Ticket #3300 sh
2b905 6f 77 73 20 74 68 61 74 20 66 6c 61 74 74 65 6e  ows that flatten
2b906 69 6e 67 20 74 68 65 20 72 69 67 68 74 20 74 65  ing the right te
2b907 72 6d 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49  rm of a LEFT JOI
2b908 4e 0a 20 20 2a 2a 20 69 73 20 66 72 61 75 67 68  N.  ** is fraugh
2b909 74 20 77 69 74 68 20 64 61 6e 67 65 72 2e 20 20  t with danger.  
2b90a 42 65 73 74 20 74 6f 20 61 76 6f 69 64 20 74 68  Best to avoid th
2b90b 65 20 77 68 6f 6c 65 20 74 68 69 6e 67 2e 20 20  e whole thing.  
2b90c 49 66 20 74 68 65 0a 20 20 2a 2a 20 73 75 62 71  If the.  ** subq
2b90d 75 65 72 79 20 69 73 20 74 68 65 20 72 69 67 68  uery is the righ
2b90e 74 20 74 65 72 6d 20 6f 66 20 61 20 4c 45 46 54  t term of a LEFT
2b90f 20 4a 4f 49 4e 2c 20 74 68 65 6e 20 64 6f 20 6e   JOIN, then do n
2b910 6f 74 20 66 6c 61 74 74 65 6e 2e 0a 20 20 2a 2f  ot flatten..  */
2b911 0a 20 20 69 66 28 20 28 70 53 75 62 69 74 65 6d  .  if( (pSubitem
2b912 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f  ->jointype & JT_
2b913 4f 55 54 45 52 29 21 3d 30 20 29 7b 0a 20 20 20  OUTER)!=0 ){.   
2b914 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
2b915 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
2b916 20 31 37 3a 20 49 66 20 74 68 65 20 73 75 62 2d   17: If the sub-
2b917 71 75 65 72 79 20 69 73 20 61 20 63 6f 6d 70 6f  query is a compo
2b918 75 6e 64 20 53 45 4c 45 43 54 2c 20 74 68 65 6e  und SELECT, then
2b919 20 69 74 20 6d 75 73 74 0a 20 20 2a 2a 20 75 73   it must.  ** us
2b91a 65 20 6f 6e 6c 79 20 74 68 65 20 55 4e 49 4f 4e  e only the UNION
2b91b 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 2e 20 41   ALL operator. A
2b91c 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73  nd none of the s
2b91d 69 6d 70 6c 65 20 73 65 6c 65 63 74 20 71 75 65  imple select que
2b91e 72 69 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 6d  ries.  ** that m
2b91f 61 6b 65 20 75 70 20 74 68 65 20 63 6f 6d 70 6f  ake up the compo
2b920 75 6e 64 20 53 45 4c 45 43 54 20 61 72 65 20 61  und SELECT are a
2b921 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 61 67 67  llowed to be agg
2b922 72 65 67 61 74 65 20 6f 72 20 64 69 73 74 69 6e  regate or distin
2b923 63 74 0a 20 20 2a 2a 20 71 75 65 72 69 65 73 2e  ct.  ** queries.
2b924 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62  .  */.  if( pSub
2b925 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20  ->pPrior ){.    
2b926 69 66 28 20 70 53 75 62 2d 3e 70 4f 72 64 65 72  if( pSub->pOrder
2b927 42 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  By ){.      retu
2b928 72 6e 20 30 3b 20 20 2f 2a 20 52 65 73 74 72 69  rn 0;  /* Restri
2b929 63 74 69 6f 6e 20 32 30 20 2a 2f 0a 20 20 20 20  ction 20 */.    
2b92a 7d 0a 20 20 20 20 69 66 28 20 69 73 41 67 67 20  }.    if( isAgg 
2b92b 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  || (p->selFlags 
2b92c 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d  & SF_Distinct)!=
2b92d 30 20 7c 7c 20 70 53 72 63 2d 3e 6e 53 72 63 21  0 || pSrc->nSrc!
2b92e 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =1 ){.      retu
2b92f 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 0;.    }.    
2b930 66 6f 72 28 70 53 75 62 31 3d 70 53 75 62 3b 20  for(pSub1=pSub; 
2b931 70 53 75 62 31 3b 20 70 53 75 62 31 3d 70 53 75  pSub1; pSub1=pSu
2b932 62 31 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20  b1->pPrior){.   
2b933 20 20 20 74 65 73 74 63 61 73 65 28 20 28 70 53     testcase( (pS
2b934 75 62 31 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  ub1->selFlags & 
2b935 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f  (SF_Distinct|SF_
2b936 41 67 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f  Aggregate))==SF_
2b937 44 69 73 74 69 6e 63 74 20 29 3b 0a 20 20 20 20  Distinct );.    
2b938 20 20 74 65 73 74 63 61 73 65 28 20 28 70 53 75    testcase( (pSu
2b939 62 31 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28  b1->selFlags & (
2b93a 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41  SF_Distinct|SF_A
2b93b 67 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 41  ggregate))==SF_A
2b93c 67 67 72 65 67 61 74 65 20 29 3b 0a 20 20 20 20  ggregate );.    
2b93d 20 20 69 66 28 20 28 70 53 75 62 31 2d 3e 73 65    if( (pSub1->se
2b93e 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73  lFlags & (SF_Dis
2b93f 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61  tinct|SF_Aggrega
2b940 74 65 29 29 21 3d 30 0a 20 20 20 20 20 20 20 7c  te))!=0.       |
2b941 7c 20 28 70 53 75 62 31 2d 3e 70 50 72 69 6f 72  | (pSub1->pPrior
2b942 20 26 26 20 70 53 75 62 31 2d 3e 6f 70 21 3d 54   && pSub1->op!=T
2b943 4b 5f 41 4c 4c 29 20 0a 20 20 20 20 20 20 20 7c  K_ALL) .       |
2b944 7c 20 4e 45 56 45 52 28 70 53 75 62 31 2d 3e 70  | NEVER(pSub1->p
2b945 53 72 63 3d 3d 30 29 20 7c 7c 20 70 53 75 62 31  Src==0) || pSub1
2b946 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 0a  ->pSrc->nSrc!=1.
2b947 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
2b948 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
2b949 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
2b94a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31 38 2e   Restriction 18.
2b94b 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70   */.    if( p->p
2b94c 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
2b94d 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 66   int ii;.      f
2b94e 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 2d 3e 70  or(ii=0; ii<p->p
2b94f 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20  OrderBy->nExpr; 
2b950 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  ii++){.        i
2b951 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e  f( p->pOrderBy->
2b952 61 5b 69 69 5d 2e 69 43 6f 6c 3d 3d 30 20 29 20  a[ii].iCol==0 ) 
2b953 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2b954 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
2b955 2a 2a 2a 2a 2a 20 49 66 20 77 65 20 72 65 61 63  ***** If we reac
2b956 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 66 6c  h this point, fl
2b957 61 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d  attening is perm
2b958 69 74 74 65 64 2e 20 2a 2a 2a 2a 2a 2f 0a 0a 20  itted. *****/.. 
2b959 20 2f 2a 20 41 75 74 68 6f 72 69 7a 65 20 74 68   /* Authorize th
2b95a 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20  e subquery */.  
2b95b 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
2b95c 74 65 78 74 20 3d 20 70 53 75 62 69 74 65 6d 2d  text = pSubitem-
2b95d 3e 7a 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65  >zName;.  sqlite
2b95e 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
2b95f 65 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54  e, SQLITE_SELECT
2b960 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 70 50  , 0, 0, 0);.  pP
2b961 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
2b962 78 74 20 3d 20 7a 53 61 76 65 64 41 75 74 68 43  xt = zSavedAuthC
2b963 6f 6e 74 65 78 74 3b 0a 0a 20 20 2f 2a 20 49 66  ontext;..  /* If
2b964 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69   the sub-query i
2b965 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  s a compound SEL
2b966 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  ECT statement, t
2b967 68 65 6e 20 28 62 79 20 72 65 73 74 72 69 63 74  hen (by restrict
2b968 69 6f 6e 73 0a 20 20 2a 2a 20 31 37 20 61 6e 64  ions.  ** 17 and
2b969 20 31 38 20 61 62 6f 76 65 29 20 69 74 20 6d 75   18 above) it mu
2b96a 73 74 20 62 65 20 61 20 55 4e 49 4f 4e 20 41 4c  st be a UNION AL
2b96b 4c 20 61 6e 64 20 74 68 65 20 70 61 72 65 6e 74  L and the parent
2b96c 20 71 75 65 72 79 20 6d 75 73 74 20 0a 20 20 2a   query must .  *
2b96d 2a 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d  * be of the form
2b96e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
2b96f 53 45 4c 45 43 54 20 3c 65 78 70 72 2d 6c 69 73  SELECT <expr-lis
2b970 74 3e 20 46 52 4f 4d 20 28 3c 73 75 62 2d 71 75  t> FROM (<sub-qu
2b971 65 72 79 3e 29 20 3c 77 68 65 72 65 2d 63 6c 61  ery>) <where-cla
2b972 75 73 65 3e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  use> .  **.  ** 
2b973 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 79 20  followed by any 
2b974 4f 52 44 45 52 20 42 59 2c 20 4c 49 4d 49 54 20  ORDER BY, LIMIT 
2b975 61 6e 64 2f 6f 72 20 4f 46 46 53 45 54 20 63 6c  and/or OFFSET cl
2b976 61 75 73 65 73 2e 20 54 68 69 73 20 62 6c 6f 63  auses. This bloc
2b977 6b 0a 20 20 2a 2a 20 63 72 65 61 74 65 73 20 4e  k.  ** creates N
2b978 2d 31 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65  -1 copies of the
2b979 20 70 61 72 65 6e 74 20 71 75 65 72 79 20 77 69   parent query wi
2b97a 74 68 6f 75 74 20 61 6e 79 20 4f 52 44 45 52 20  thout any ORDER 
2b97b 42 59 2c 20 4c 49 4d 49 54 20 6f 72 20 0a 20 20  BY, LIMIT or .  
2b97c 2a 2a 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65  ** OFFSET clause
2b97d 73 20 61 6e 64 20 6a 6f 69 6e 73 20 74 68 65 6d  s and joins them
2b97e 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 68 61 6e   to the left-han
2b97f 64 2d 73 69 64 65 20 6f 66 20 74 68 65 20 6f 72  d-side of the or
2b980 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 75 73 69 6e  iginal.  ** usin
2b981 67 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72  g UNION ALL oper
2b982 61 74 6f 72 73 2e 20 49 6e 20 74 68 69 73 20 63  ators. In this c
2b983 61 73 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d  ase N is the num
2b984 62 65 72 20 6f 66 20 73 69 6d 70 6c 65 0a 20 20  ber of simple.  
2b985 2a 2a 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d  ** select statem
2b986 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70  ents in the comp
2b987 6f 75 6e 64 20 73 75 62 2d 71 75 65 72 79 2e 0a  ound sub-query..
2b988 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 61 6d 70 6c    **.  ** Exampl
2b989 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  e:.  **.  **    
2b98a 20 53 45 4c 45 43 54 20 61 2b 31 20 46 52 4f 4d   SELECT a+1 FROM
2b98b 20 28 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53   (.  **        S
2b98c 45 4c 45 43 54 20 78 20 46 52 4f 4d 20 74 61 62  ELECT x FROM tab
2b98d 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 55 4e 49  .  **        UNI
2b98e 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20  ON ALL.  **     
2b98f 20 20 20 53 45 4c 45 43 54 20 79 20 46 52 4f 4d     SELECT y FROM
2b990 20 74 61 62 0a 20 20 2a 2a 20 20 20 20 20 20 20   tab.  **       
2b991 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20   UNION ALL.  ** 
2b992 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 62         SELECT ab
2b993 73 28 7a 2a 32 29 20 46 52 4f 4d 20 74 61 62 32  s(z*2) FROM tab2
2b994 0a 20 20 2a 2a 20 20 20 20 20 29 20 57 48 45 52  .  **     ) WHER
2b995 45 20 61 21 3d 35 20 4f 52 44 45 52 20 42 59 20  E a!=5 ORDER BY 
2b996 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 72 61 6e  1.  **.  ** Tran
2b997 73 66 6f 72 6d 65 64 20 69 6e 74 6f 3a 0a 20 20  sformed into:.  
2b998 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45  **.  **     SELE
2b999 43 54 20 78 2b 31 20 46 52 4f 4d 20 74 61 62 20  CT x+1 FROM tab 
2b99a 57 48 45 52 45 20 78 2b 31 21 3d 35 0a 20 20 2a  WHERE x+1!=5.  *
2b99b 2a 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a  *     UNION ALL.
2b99c 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20    **     SELECT 
2b99d 79 2b 31 20 46 52 4f 4d 20 74 61 62 20 57 48 45  y+1 FROM tab WHE
2b99e 52 45 20 79 2b 31 21 3d 35 0a 20 20 2a 2a 20 20  RE y+1!=5.  **  
2b99f 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a     UNION ALL.  *
2b9a0 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 62 73  *     SELECT abs
2b9a1 28 7a 2a 32 29 2b 31 20 46 52 4f 4d 20 74 61 62  (z*2)+1 FROM tab
2b9a2 32 20 57 48 45 52 45 20 61 62 73 28 7a 2a 32 29  2 WHERE abs(z*2)
2b9a3 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 4f  +1!=5.  **     O
2b9a4 52 44 45 52 20 42 59 20 31 0a 20 20 2a 2a 0a 20  RDER BY 1.  **. 
2b9a5 20 2a 2a 20 57 65 20 63 61 6c 6c 20 74 68 69 73   ** We call this
2b9a6 20 74 68 65 20 22 63 6f 6d 70 6f 75 6e 64 2d 73   the "compound-s
2b9a7 75 62 71 75 65 72 79 20 66 6c 61 74 74 65 6e 69  ubquery flatteni
2b9a8 6e 67 22 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  ng"..  */.  for(
2b9a9 70 53 75 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f  pSub=pSub->pPrio
2b9aa 72 3b 20 70 53 75 62 3b 20 70 53 75 62 3d 70 53  r; pSub; pSub=pS
2b9ab 75 62 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20  ub->pPrior){.   
2b9ac 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20   Select *pNew;. 
2b9ad 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72     ExprList *pOr
2b9ae 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65  derBy = p->pOrde
2b9af 72 42 79 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  rBy;.    Expr *p
2b9b0 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
2b9b1 74 3b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70  t;.    Select *p
2b9b2 50 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f  Prior = p->pPrio
2b9b3 72 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72  r;.    p->pOrder
2b9b4 42 79 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70  By = 0;.    p->p
2b9b5 53 72 63 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e  Src = 0;.    p->
2b9b6 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pPrior = 0;.    
2b9b7 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  p->pLimit = 0;. 
2b9b8 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
2b9b9 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
2b9ba 2c 20 30 29 3b 0a 20 20 20 20 70 2d 3e 70 4c 69  , 0);.    p->pLi
2b9bb 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20  mit = pLimit;.  
2b9bc 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20    p->pOrderBy = 
2b9bd 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 70 2d  pOrderBy;.    p-
2b9be 3e 70 53 72 63 20 3d 20 70 53 72 63 3b 0a 20 20  >pSrc = pSrc;.  
2b9bf 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f 41 4c 4c    p->op = TK_ALL
2b9c0 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d  ;.    p->pRightm
2b9c1 6f 73 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  ost = 0;.    if(
2b9c2 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   pNew==0 ){.    
2b9c3 20 20 70 4e 65 77 20 3d 20 70 50 72 69 6f 72 3b    pNew = pPrior;
2b9c4 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2b9c5 20 20 70 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d    pNew->pPrior =
2b9c6 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70   pPrior;.      p
2b9c7 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20  New->pRightmost 
2b9c8 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  = 0;.    }.    p
2b9c9 2d 3e 70 50 72 69 6f 72 20 3d 20 70 4e 65 77 3b  ->pPrior = pNew;
2b9ca 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  .    if( db->mal
2b9cb 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
2b9cc 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 1;.  }..  /* 
2b9cd 42 65 67 69 6e 20 66 6c 61 74 74 65 6e 69 6e 67  Begin flattening
2b9ce 20 74 68 65 20 69 46 72 6f 6d 2d 74 68 20 65 6e   the iFrom-th en
2b9cf 74 72 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  try of the FROM 
2b9d0 63 6c 61 75 73 65 20 0a 20 20 2a 2a 20 69 6e 20  clause .  ** in 
2b9d1 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e  the outer query.
2b9d2 0a 20 20 2a 2f 0a 20 20 70 53 75 62 20 3d 20 70  .  */.  pSub = p
2b9d3 53 75 62 31 20 3d 20 70 53 75 62 69 74 65 6d 2d  Sub1 = pSubitem-
2b9d4 3e 70 53 65 6c 65 63 74 3b 0a 0a 20 20 2f 2a 20  >pSelect;..  /* 
2b9d5 44 65 6c 65 74 65 20 74 68 65 20 74 72 61 6e 73  Delete the trans
2b9d6 69 65 6e 74 20 74 61 62 6c 65 20 73 74 72 75 63  ient table struc
2b9d7 74 75 72 65 20 61 73 73 6f 63 69 61 74 65 64 20  ture associated 
2b9d8 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73 75  with the.  ** su
2b9d9 62 71 75 65 72 79 0a 20 20 2a 2f 0a 20 20 73 71  bquery.  */.  sq
2b9da 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2b9db 70 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62  pSubitem->zDatab
2b9dc 61 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ase);.  sqlite3D
2b9dd 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 74  bFree(db, pSubit
2b9de 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71  em->zName);.  sq
2b9df 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2b9e0 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73  pSubitem->zAlias
2b9e1 29 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a  );.  pSubitem->z
2b9e2 44 61 74 61 62 61 73 65 20 3d 20 30 3b 0a 20 20  Database = 0;.  
2b9e3 70 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 20  pSubitem->zName 
2b9e4 3d 20 30 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d  = 0;.  pSubitem-
2b9e5 3e 7a 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 70  >zAlias = 0;.  p
2b9e6 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74  Subitem->pSelect
2b9e7 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44 65 66 65   = 0;..  /* Defe
2b9e8 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 54  r deleting the T
2b9e9 61 62 6c 65 20 6f 62 6a 65 63 74 20 61 73 73 6f  able object asso
2b9ea 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 0a  ciated with the.
2b9eb 20 20 2a 2a 20 73 75 62 71 75 65 72 79 20 75 6e    ** subquery un
2b9ec 74 69 6c 20 63 6f 64 65 20 67 65 6e 65 72 61 74  til code generat
2b9ed 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 70  ion is.  ** comp
2b9ee 6c 65 74 65 2c 20 73 69 6e 63 65 20 74 68 65 72  lete, since ther
2b9ef 65 20 6d 61 79 20 73 74 69 6c 6c 20 65 78 69 73  e may still exis
2b9f0 74 20 45 78 70 72 2e 70 54 61 62 20 65 6e 74 72  t Expr.pTab entr
2b9f1 69 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 72 65  ies that.  ** re
2b9f2 66 65 72 20 74 6f 20 74 68 65 20 73 75 62 71 75  fer to the subqu
2b9f3 65 72 79 20 65 76 65 6e 20 61 66 74 65 72 20 66  ery even after f
2b9f4 6c 61 74 74 65 6e 69 6e 67 2e 20 20 54 69 63 6b  lattening.  Tick
2b9f5 65 74 20 23 33 33 34 36 2e 0a 20 20 2a 2a 0a 20  et #3346..  **. 
2b9f6 20 2a 2a 20 70 53 75 62 69 74 65 6d 2d 3e 70 54   ** pSubitem->pT
2b9f7 61 62 20 69 73 20 61 6c 77 61 79 73 20 6e 6f 6e  ab is always non
2b9f8 2d 4e 55 4c 4c 20 62 79 20 74 65 73 74 20 72 65  -NULL by test re
2b9f9 73 74 72 69 63 74 69 6f 6e 73 20 61 6e 64 20 74  strictions and t
2b9fa 65 73 74 73 20 61 62 6f 76 65 2e 0a 20 20 2a 2f  ests above..  */
2b9fb 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53  .  if( ALWAYS(pS
2b9fc 75 62 69 74 65 6d 2d 3e 70 54 61 62 21 3d 30 29  ubitem->pTab!=0)
2b9fd 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
2b9fe 54 61 62 54 6f 44 65 6c 20 3d 20 70 53 75 62 69  TabToDel = pSubi
2b9ff 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69  tem->pTab;.    i
2ba00 66 28 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52  f( pTabToDel->nR
2ba01 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 50  ef==1 ){.      P
2ba02 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20  arse *pToplevel 
2ba03 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  = sqlite3ParseTo
2ba04 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a  plevel(pParse);.
2ba05 20 20 20 20 20 20 70 54 61 62 54 6f 44 65 6c 2d        pTabToDel-
2ba06 3e 70 4e 65 78 74 5a 6f 6d 62 69 65 20 3d 20 70  >pNextZombie = p
2ba07 54 6f 70 6c 65 76 65 6c 2d 3e 70 5a 6f 6d 62 69  Toplevel->pZombi
2ba08 65 54 61 62 3b 0a 20 20 20 20 20 20 70 54 6f 70  eTab;.      pTop
2ba09 6c 65 76 65 6c 2d 3e 70 5a 6f 6d 62 69 65 54 61  level->pZombieTa
2ba0a 62 20 3d 20 70 54 61 62 54 6f 44 65 6c 3b 0a 20  b = pTabToDel;. 
2ba0b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2ba0c 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 2d  pTabToDel->nRef-
2ba0d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 75  -;.    }.    pSu
2ba0e 62 69 74 65 6d 2d 3e 70 54 61 62 20 3d 20 30 3b  bitem->pTab = 0;
2ba0f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66  .  }..  /* The f
2ba10 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 6f 70 20 72 75  ollowing loop ru
2ba11 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ns once for each
2ba12 20 74 65 72 6d 20 69 6e 20 61 20 63 6f 6d 70 6f   term in a compo
2ba13 75 6e 64 2d 73 75 62 71 75 65 72 79 0a 20 20 2a  und-subquery.  *
2ba14 2a 20 66 6c 61 74 74 65 6e 69 6e 67 20 28 61 73  * flattening (as
2ba15 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65   described above
2ba16 29 2e 20 20 49 66 20 77 65 20 61 72 65 20 64 6f  ).  If we are do
2ba17 69 6e 67 20 61 20 64 69 66 66 65 72 65 6e 74 20  ing a different 
2ba18 6b 69 6e 64 0a 20 20 2a 2a 20 6f 66 20 66 6c 61  kind.  ** of fla
2ba19 74 74 65 6e 69 6e 67 20 2d 20 61 20 66 6c 61 74  ttening - a flat
2ba1a 74 65 6e 69 6e 67 20 6f 74 68 65 72 20 74 68 61  tening other tha
2ba1b 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 2d 73 75 62  n a compound-sub
2ba1c 71 75 65 72 79 20 66 6c 61 74 74 65 6e 69 6e 67  query flattening
2ba1d 20 2d 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 69   -.  ** then thi
2ba1e 73 20 6c 6f 6f 70 20 6f 6e 6c 79 20 72 75 6e 73  s loop only runs
2ba1f 20 6f 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   once..  **.  **
2ba20 20 54 68 69 73 20 6c 6f 6f 70 20 6d 6f 76 65 73   This loop moves
2ba21 20 61 6c 6c 20 6f 66 20 74 68 65 20 46 52 4f 4d   all of the FROM
2ba22 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
2ba23 20 73 75 62 71 75 65 72 79 20 69 6e 74 6f 20 74   subquery into t
2ba24 68 65 0a 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d  he.  ** the FROM
2ba25 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f   clause of the o
2ba26 75 74 65 72 20 71 75 65 72 79 2e 20 20 42 65 66  uter query.  Bef
2ba27 6f 72 65 20 64 6f 69 6e 67 20 74 68 69 73 2c 20  ore doing this, 
2ba28 72 65 6d 65 6d 62 65 72 0a 20 20 2a 2a 20 74 68  remember.  ** th
2ba29 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  e cursor number 
2ba2a 66 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  for the original
2ba2b 20 6f 75 74 65 72 20 71 75 65 72 79 20 46 52 4f   outer query FRO
2ba2c 4d 20 65 6c 65 6d 65 6e 74 20 69 6e 0a 20 20 2a  M element in.  *
2ba2d 2a 20 69 50 61 72 65 6e 74 2e 20 20 54 68 65 20  * iParent.  The 
2ba2e 69 50 61 72 65 6e 74 20 63 75 72 73 6f 72 20 77  iParent cursor w
2ba2f 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73 65  ill never be use
2ba30 64 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63  d.  Subsequent c
2ba31 6f 64 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 73 63  ode.  ** will sc
2ba32 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c  an expressions l
2ba33 6f 6f 6b 69 6e 67 20 66 6f 72 20 69 50 61 72 65  ooking for iPare
2ba34 6e 74 20 72 65 66 65 72 65 6e 63 65 73 20 61 6e  nt references an
2ba35 64 20 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 74  d replace.  ** t
2ba36 68 6f 73 65 20 72 65 66 65 72 65 6e 63 65 73 20  hose references 
2ba37 77 69 74 68 20 65 78 70 72 65 73 73 69 6f 6e 73  with expressions
2ba38 20 74 68 61 74 20 72 65 73 6f 6c 76 65 20 74 6f   that resolve to
2ba39 20 74 68 65 20 73 75 62 71 75 65 72 79 20 46 52   the subquery FR
2ba3a 4f 4d 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 73  OM.  ** elements
2ba3b 20 77 65 20 61 72 65 20 6e 6f 77 20 63 6f 70 79   we are now copy
2ba3c 69 6e 67 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 66  ing in..  */.  f
2ba3d 6f 72 28 70 50 61 72 65 6e 74 3d 70 3b 20 70 50  or(pParent=p; pP
2ba3e 61 72 65 6e 74 3b 20 70 50 61 72 65 6e 74 3d 70  arent; pParent=p
2ba3f 50 61 72 65 6e 74 2d 3e 70 50 72 69 6f 72 2c 20  Parent->pPrior, 
2ba40 70 53 75 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f  pSub=pSub->pPrio
2ba41 72 29 7b 0a 20 20 20 20 69 6e 74 20 6e 53 75 62  r){.    int nSub
2ba42 53 72 63 3b 0a 20 20 20 20 75 38 20 6a 6f 69 6e  Src;.    u8 join
2ba43 74 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 70 53  type = 0;.    pS
2ba44 75 62 53 72 63 20 3d 20 70 53 75 62 2d 3e 70 53  ubSrc = pSub->pS
2ba45 72 63 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20  rc;     /* FROM 
2ba46 63 6c 61 75 73 65 20 6f 66 20 73 75 62 71 75 65  clause of subque
2ba47 72 79 20 2a 2f 0a 20 20 20 20 6e 53 75 62 53 72  ry */.    nSubSr
2ba48 63 20 3d 20 70 53 75 62 53 72 63 2d 3e 6e 53 72  c = pSubSrc->nSr
2ba49 63 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  c;  /* Number of
2ba4a 20 74 65 72 6d 73 20 69 6e 20 73 75 62 71 75 65   terms in subque
2ba4b 72 79 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  ry FROM clause *
2ba4c 2f 0a 20 20 20 20 70 53 72 63 20 3d 20 70 50 61  /.    pSrc = pPa
2ba4d 72 65 6e 74 2d 3e 70 53 72 63 3b 20 20 20 20 20  rent->pSrc;     
2ba4e 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  /* FROM clause o
2ba4f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  f the outer quer
2ba50 79 20 2a 2f 0a 0a 20 20 20 20 69 66 28 20 70 53  y */..    if( pS
2ba51 72 63 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  rc ){.      asse
2ba52 72 74 28 20 70 50 61 72 65 6e 74 3d 3d 70 20 29  rt( pParent==p )
2ba53 3b 20 20 2f 2a 20 46 69 72 73 74 20 74 69 6d 65  ;  /* First time
2ba54 20 74 68 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f   through the loo
2ba55 70 20 2a 2f 0a 20 20 20 20 20 20 6a 6f 69 6e 74  p */.      joint
2ba56 79 70 65 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  ype = pSubitem->
2ba57 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 7d 65  jointype;.    }e
2ba58 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
2ba59 74 28 20 70 50 61 72 65 6e 74 21 3d 70 20 29 3b  t( pParent!=p );
2ba5a 20 20 2f 2a 20 32 6e 64 20 61 6e 64 20 73 75 62    /* 2nd and sub
2ba5b 73 65 71 75 65 6e 74 20 74 69 6d 65 73 20 74 68  sequent times th
2ba5c 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a  rough the loop *
2ba5d 2f 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70  /.      pSrc = p
2ba5e 50 61 72 65 6e 74 2d 3e 70 53 72 63 20 3d 20 73  Parent->pSrc = s
2ba5f 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
2ba60 65 6e 64 28 64 62 2c 20 30 2c 20 30 2c 20 30 29  end(db, 0, 0, 0)
2ba61 3b 0a 20 20 20 20 20 20 69 66 28 20 70 53 72 63  ;.      if( pSrc
2ba62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ==0 ){.        a
2ba63 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f  ssert( db->mallo
2ba64 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20  cFailed );.     
2ba65 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ba66 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
2ba67 54 68 65 20 73 75 62 71 75 65 72 79 20 75 73 65  The subquery use
2ba68 73 20 61 20 73 69 6e 67 6c 65 20 73 6c 6f 74 20  s a single slot 
2ba69 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
2ba6a 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 0a  se of the outer.
2ba6b 20 20 20 20 2a 2a 20 71 75 65 72 79 2e 20 20 49      ** query.  I
2ba6c 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 68  f the subquery h
2ba6d 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  as more than one
2ba6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 69 74 73 20   element in its 
2ba6f 46 52 4f 4d 20 63 6c 61 75 73 65 2c 0a 20 20 20  FROM clause,.   
2ba70 20 2a 2a 20 74 68 65 6e 20 65 78 70 61 6e 64 20   ** then expand 
2ba71 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
2ba72 74 6f 20 6d 61 6b 65 20 73 70 61 63 65 20 66 6f  to make space fo
2ba73 72 20 69 74 20 74 6f 20 68 6f 6c 64 20 61 6c 6c  r it to hold all
2ba74 20 65 6c 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a   elements.    **
2ba75 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
2ba76 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2ba77 45 78 61 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a  Example:.    **.
2ba78 20 20 20 20 2a 2a 20 20 20 20 53 45 4c 45 43 54      **    SELECT
2ba79 20 2a 20 46 52 4f 4d 20 74 61 62 41 2c 20 28 53   * FROM tabA, (S
2ba7a 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 73 75 62  ELECT * FROM sub
2ba7b 31 2c 20 73 75 62 32 29 2c 20 74 61 62 42 3b 0a  1, sub2), tabB;.
2ba7c 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
2ba7d 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 68 61  e outer query ha
2ba7e 73 20 33 20 73 6c 6f 74 73 20 69 6e 20 69 74 73  s 3 slots in its
2ba7f 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 4f   FROM clause.  O
2ba80 6e 65 20 73 6c 6f 74 20 6f 66 20 74 68 65 0a 20  ne slot of the. 
2ba81 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72     ** outer quer
2ba82 79 20 28 74 68 65 20 6d 69 64 64 6c 65 20 73 6c  y (the middle sl
2ba83 6f 74 29 20 69 73 20 75 73 65 64 20 62 79 20 74  ot) is used by t
2ba84 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 54 68  he subquery.  Th
2ba85 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 62 6c  e next.    ** bl
2ba86 6f 63 6b 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c  ock of code will
2ba87 20 65 78 70 61 6e 64 20 74 68 65 20 6f 75 74 20   expand the out 
2ba88 71 75 65 72 79 20 74 6f 20 34 20 73 6c 6f 74 73  query to 4 slots
2ba89 2e 20 20 54 68 65 20 6d 69 64 64 6c 65 0a 20 20  .  The middle.  
2ba8a 20 20 2a 2a 20 73 6c 6f 74 20 69 73 20 65 78 70    ** slot is exp
2ba8b 61 6e 64 65 64 20 74 6f 20 74 77 6f 20 73 6c 6f  anded to two slo
2ba8c 74 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d  ts in order to m
2ba8d 61 6b 65 20 73 70 61 63 65 20 66 6f 72 20 74 68  ake space for th
2ba8e 65 0a 20 20 20 20 2a 2a 20 74 77 6f 20 65 6c 65  e.    ** two ele
2ba8f 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 46 52 4f  ments in the FRO
2ba90 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  M clause of the 
2ba91 73 75 62 71 75 65 72 79 2e 0a 20 20 20 20 2a 2f  subquery..    */
2ba92 0a 20 20 20 20 69 66 28 20 6e 53 75 62 53 72 63  .    if( nSubSrc
2ba93 3e 31 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  >1 ){.      pPar
2ba94 65 6e 74 2d 3e 70 53 72 63 20 3d 20 70 53 72 63  ent->pSrc = pSrc
2ba95 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
2ba96 74 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 53 72  tEnlarge(db, pSr
2ba97 63 2c 20 6e 53 75 62 53 72 63 2d 31 2c 69 46 72  c, nSubSrc-1,iFr
2ba98 6f 6d 2b 31 29 3b 0a 20 20 20 20 20 20 69 66 28  om+1);.      if(
2ba99 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2ba9a 64 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65  d ){.        bre
2ba9b 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2ba9c 7d 0a 0a 20 20 20 20 2f 2a 20 54 72 61 6e 73 66  }..    /* Transf
2ba9d 65 72 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  er the FROM clau
2ba9e 73 65 20 74 65 72 6d 73 20 66 72 6f 6d 20 74 68  se terms from th
2ba9f 65 20 73 75 62 71 75 65 72 79 20 69 6e 74 6f 20  e subquery into 
2baa0 74 68 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72  the.    ** outer
2baa1 20 71 75 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20   query..    */. 
2baa2 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53     for(i=0; i<nS
2baa3 75 62 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  ubSrc; i++){.   
2baa4 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74     sqlite3IdList
2baa5 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 2d  Delete(db, pSrc-
2baa6 3e 61 5b 69 2b 69 46 72 6f 6d 5d 2e 70 55 73 69  >a[i+iFrom].pUsi
2baa7 6e 67 29 3b 0a 20 20 20 20 20 20 70 53 72 63 2d  ng);.      pSrc-
2baa8 3e 61 5b 69 2b 69 46 72 6f 6d 5d 20 3d 20 70 53  >a[i+iFrom] = pS
2baa9 75 62 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20  ubSrc->a[i];.   
2baaa 20 20 20 6d 65 6d 73 65 74 28 26 70 53 75 62 53     memset(&pSubS
2baab 72 63 2d 3e 61 5b 69 5d 2c 20 30 2c 20 73 69 7a  rc->a[i], 0, siz
2baac 65 6f 66 28 70 53 75 62 53 72 63 2d 3e 61 5b 69  eof(pSubSrc->a[i
2baad 5d 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ]));.    }.    p
2baae 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 6a 6f  Src->a[iFrom].jo
2baaf 69 6e 74 79 70 65 20 3d 20 6a 6f 69 6e 74 79 70  intype = jointyp
2bab0 65 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77  e;.  .    /* Now
2bab1 20 62 65 67 69 6e 20 73 75 62 73 74 69 74 75 74   begin substitut
2bab2 69 6e 67 20 73 75 62 71 75 65 72 79 20 72 65 73  ing subquery res
2bab3 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69  ult set expressi
2bab4 6f 6e 73 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20  ons for .    ** 
2bab5 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
2bab6 65 20 69 50 61 72 65 6e 74 20 69 6e 20 74 68 65  e iParent in the
2bab7 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20   outer query..  
2bab8 20 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 45 78 61    ** .    ** Exa
2bab9 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  mple:.    **.   
2baba 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 61 2b 35   **   SELECT a+5
2babb 2c 20 62 2a 31 30 20 46 52 4f 4d 20 28 53 45 4c  , b*10 FROM (SEL
2babc 45 43 54 20 78 2a 33 20 41 53 20 61 2c 20 79 2b  ECT x*3 AS a, y+
2babd 31 30 20 41 53 20 62 20 46 52 4f 4d 20 74 31 29  10 AS b FROM t1)
2babe 20 57 48 45 52 45 20 61 3e 62 3b 0a 20 20 20 20   WHERE a>b;.    
2babf 2a 2a 20 20 20 5c 20 20 20 20 20 20 20 20 20 20  **   \          
2bac0 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f             \____
2bac1 5f 5f 5f 5f 5f 5f 5f 5f 5f 20 73 75 62 71 75 65  _________ subque
2bac2 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20  ry __________/  
2bac3 20 20 20 20 20 20 20 20 2f 0a 20 20 20 20 2a 2a          /.    **
2bac4 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f      \___________
2bac5 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20 6f 75 74 65 72  __________ outer
2bac6 20 71 75 65 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f   query _________
2bac7 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2bac8 5f 5f 5f 5f 5f 2f 0a 20 20 20 20 2a 2a 0a 20 20  _____/.    **.  
2bac9 20 20 2a 2a 20 57 65 20 6c 6f 6f 6b 20 61 74 20    ** We look at 
2baca 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  every expression
2bacb 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75   in the outer qu
2bacc 65 72 79 20 61 6e 64 20 65 76 65 72 79 20 70 6c  ery and every pl
2bacd 61 63 65 20 77 65 20 73 65 65 0a 20 20 20 20 2a  ace we see.    *
2bace 2a 20 22 61 22 20 77 65 20 73 75 62 73 74 69 74  * "a" we substit
2bacf 75 74 65 20 22 78 2a 33 22 20 61 6e 64 20 65 76  ute "x*3" and ev
2bad0 65 72 79 20 70 6c 61 63 65 20 77 65 20 73 65 65  ery place we see
2bad1 20 22 62 22 20 77 65 20 73 75 62 73 74 69 74 75   "b" we substitu
2bad2 74 65 20 22 79 2b 31 30 22 2e 0a 20 20 20 20 2a  te "y+10"..    *
2bad3 2f 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 50  /.    pList = pP
2bad4 61 72 65 6e 74 2d 3e 70 45 4c 69 73 74 3b 0a 20  arent->pEList;. 
2bad5 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c     for(i=0; i<pL
2bad6 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
2bad7 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73  {.      if( pLis
2bad8 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d 30  t->a[i].zName==0
2bad9 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   ){.        cons
2bada 74 20 63 68 61 72 20 2a 7a 53 70 61 6e 20 3d 20  t char *zSpan = 
2badb 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61  pList->a[i].zSpa
2badc 6e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 41  n;.        if( A
2badd 4c 57 41 59 53 28 7a 53 70 61 6e 29 20 29 7b 0a  LWAYS(zSpan) ){.
2bade 20 20 20 20 20 20 20 20 20 20 70 4c 69 73 74 2d            pList-
2badf 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71  >a[i].zName = sq
2bae0 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2bae1 2c 20 7a 53 70 61 6e 29 3b 0a 20 20 20 20 20 20  , zSpan);.      
2bae2 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2bae3 7d 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 4c  }.    substExprL
2bae4 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d  ist(db, pParent-
2bae5 3e 70 45 4c 69 73 74 2c 20 69 50 61 72 65 6e 74  >pEList, iParent
2bae6 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b  , pSub->pEList);
2bae7 0a 20 20 20 20 69 66 28 20 69 73 41 67 67 20 29  .    if( isAgg )
2bae8 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70  {.      substExp
2bae9 72 4c 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e  rList(db, pParen
2baea 74 2d 3e 70 47 72 6f 75 70 42 79 2c 20 69 50 61  t->pGroupBy, iPa
2baeb 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
2baec 73 74 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65  st);.      pPare
2baed 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 75  nt->pHaving = su
2baee 62 73 74 45 78 70 72 28 64 62 2c 20 70 50 61 72  bstExpr(db, pPar
2baef 65 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50  ent->pHaving, iP
2baf0 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c  arent, pSub->pEL
2baf1 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ist);.    }.    
2baf2 69 66 28 20 70 53 75 62 2d 3e 70 4f 72 64 65 72  if( pSub->pOrder
2baf3 42 79 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  By ){.      asse
2baf4 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72  rt( pParent->pOr
2baf5 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20  derBy==0 );.    
2baf6 20 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65    pParent->pOrde
2baf7 72 42 79 20 3d 20 70 53 75 62 2d 3e 70 4f 72 64  rBy = pSub->pOrd
2baf8 65 72 42 79 3b 0a 20 20 20 20 20 20 70 53 75 62  erBy;.      pSub
2baf9 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a  ->pOrderBy = 0;.
2bafa 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50      }else if( pP
2bafb 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20  arent->pOrderBy 
2bafc 29 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45 78  ){.      substEx
2bafd 70 72 4c 69 73 74 28 64 62 2c 20 70 50 61 72 65  prList(db, pPare
2bafe 6e 74 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 50  nt->pOrderBy, iP
2baff 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c  arent, pSub->pEL
2bb00 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ist);.    }.    
2bb01 69 66 28 20 70 53 75 62 2d 3e 70 57 68 65 72 65  if( pSub->pWhere
2bb02 20 29 7b 0a 20 20 20 20 20 20 70 57 68 65 72 65   ){.      pWhere
2bb03 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2bb04 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 57 68 65  p(db, pSub->pWhe
2bb05 72 65 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73  re, 0);.    }els
2bb06 65 7b 0a 20 20 20 20 20 20 70 57 68 65 72 65 20  e{.      pWhere 
2bb07 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
2bb08 66 28 20 73 75 62 71 75 65 72 79 49 73 41 67 67  f( subqueryIsAgg
2bb09 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
2bb0a 28 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69  ( pParent->pHavi
2bb0b 6e 67 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ng==0 );.      p
2bb0c 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20  Parent->pHaving 
2bb0d 3d 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72  = pParent->pWher
2bb0e 65 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  e;.      pParent
2bb0f 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65 72  ->pWhere = pWher
2bb10 65 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  e;.      pParent
2bb11 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62 73  ->pHaving = subs
2bb12 74 45 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e  tExpr(db, pParen
2bb13 74 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61 72  t->pHaving, iPar
2bb14 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73  ent, pSub->pELis
2bb15 74 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e  t);.      pParen
2bb16 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c  t->pHaving = sql
2bb17 69 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20  ite3ExprAnd(db, 
2bb18 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67  pParent->pHaving
2bb19 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2bb1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb1b 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2bb1c 44 75 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 48  Dup(db, pSub->pH
2bb1d 61 76 69 6e 67 2c 20 30 29 29 3b 0a 20 20 20 20  aving, 0));.    
2bb1e 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e    assert( pParen
2bb1f 74 2d 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20 29  t->pGroupBy==0 )
2bb20 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  ;.      pParent-
2bb21 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69  >pGroupBy = sqli
2bb22 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
2bb23 62 2c 20 70 53 75 62 2d 3e 70 47 72 6f 75 70 42  b, pSub->pGroupB
2bb24 79 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  y, 0);.    }else
2bb25 7b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  {.      pParent-
2bb26 3e 70 57 68 65 72 65 20 3d 20 73 75 62 73 74 45  >pWhere = substE
2bb27 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74 2d  xpr(db, pParent-
2bb28 3e 70 57 68 65 72 65 2c 20 69 50 61 72 65 6e 74  >pWhere, iParent
2bb29 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b  , pSub->pEList);
2bb2a 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
2bb2b 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  pWhere = sqlite3
2bb2c 45 78 70 72 41 6e 64 28 64 62 2c 20 70 50 61 72  ExprAnd(db, pPar
2bb2d 65 6e 74 2d 3e 70 57 68 65 72 65 2c 20 70 57 68  ent->pWhere, pWh
2bb2e 65 72 65 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  ere);.    }.  . 
2bb2f 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 74 74 65     /* The flatte
2bb30 6e 65 64 20 71 75 65 72 79 20 69 73 20 64 69 73  ned query is dis
2bb31 74 69 6e 63 74 20 69 66 20 65 69 74 68 65 72 20  tinct if either 
2bb32 74 68 65 20 69 6e 6e 65 72 20 6f 72 20 74 68 65  the inner or the
2bb33 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75  .    ** outer qu
2bb34 65 72 79 20 69 73 20 64 69 73 74 69 6e 63 74 2e  ery is distinct.
2bb35 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61   .    */.    pPa
2bb36 72 65 6e 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c  rent->selFlags |
2bb37 3d 20 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73  = pSub->selFlags
2bb38 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a   & SF_Distinct;.
2bb39 20 20 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a    .    /*.    **
2bb3a 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52 4f 4d   SELECT ... FROM
2bb3b 20 28 53 45 4c 45 43 54 20 2e 2e 2e 20 4c 49 4d   (SELECT ... LIM
2bb3c 49 54 20 61 20 4f 46 46 53 45 54 20 62 29 20 4c  IT a OFFSET b) L
2bb3d 49 4d 49 54 20 78 20 4f 46 46 53 45 54 20 79 3b  IMIT x OFFSET y;
2bb3e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f  .    **.    ** O
2bb3f 6e 65 20 69 73 20 74 65 6d 70 74 65 64 20 74 6f  ne is tempted to
2bb40 20 74 72 79 20 74 6f 20 61 64 64 20 61 20 61 6e   try to add a an
2bb41 64 20 62 20 74 6f 20 63 6f 6d 62 69 6e 65 20 74  d b to combine t
2bb42 68 65 20 6c 69 6d 69 74 73 2e 20 20 42 75 74 20  he limits.  But 
2bb43 74 68 69 73 0a 20 20 20 20 2a 2a 20 64 6f 65 73  this.    ** does
2bb44 20 6e 6f 74 20 77 6f 72 6b 20 69 66 20 65 69 74   not work if eit
2bb45 68 65 72 20 6c 69 6d 69 74 20 69 73 20 6e 65 67  her limit is neg
2bb46 61 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  ative..    */.  
2bb47 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d    if( pSub->pLim
2bb48 69 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  it ){.      pPar
2bb49 65 6e 74 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 53  ent->pLimit = pS
2bb4a 75 62 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20  ub->pLimit;.    
2bb4b 20 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 3d    pSub->pLimit =
2bb4c 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
2bb4d 20 2f 2a 20 46 69 6e 69 61 6c 6c 79 2c 20 64 65   /* Finially, de
2bb4e 6c 65 74 65 20 77 68 61 74 20 69 73 20 6c 65 66  lete what is lef
2bb4f 74 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72  t of the subquer
2bb50 79 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a  y and return.  *
2bb51 2a 20 73 75 63 63 65 73 73 2e 0a 20 20 2a 2f 0a  * success..  */.
2bb52 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2bb53 65 6c 65 74 65 28 64 62 2c 20 70 53 75 62 31 29  elete(db, pSub1)
2bb54 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  ;..  return 1;.}
2bb55 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
2bb56 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2bb57 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65  SUBQUERY) || !de
2bb58 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2bb59 54 5f 56 49 45 57 29 20 2a 2f 0a 0a 2f 2a 0a 2a  T_VIEW) */../*.*
2bb5a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 53 45  * Analyze the SE
2bb5b 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70  LECT statement p
2bb5c 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75  assed as an argu
2bb5d 6d 65 6e 74 20 74 6f 20 73 65 65 20 69 66 20 69  ment to see if i
2bb5e 74 0a 2a 2a 20 69 73 20 61 20 6d 69 6e 28 29 20  t.** is a min() 
2bb5f 6f 72 20 6d 61 78 28 29 20 71 75 65 72 79 2e 20  or max() query. 
2bb60 52 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44  Return WHERE_ORD
2bb61 45 52 42 59 5f 4d 49 4e 20 6f 72 20 57 48 45 52  ERBY_MIN or WHER
2bb62 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 69 66  E_ORDERBY_MAX if
2bb63 20 0a 2a 2a 20 69 74 20 69 73 2c 20 6f 72 20 30   .** it is, or 0
2bb64 20 6f 74 68 65 72 77 69 73 65 2e 20 41 74 20 70   otherwise. At p
2bb65 72 65 73 65 6e 74 2c 20 61 20 71 75 65 72 79 20  resent, a query 
2bb66 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f  is considered to
2bb67 20 62 65 0a 2a 2a 20 61 20 6d 69 6e 28 29 2f 6d   be.** a min()/m
2bb68 61 78 28 29 20 71 75 65 72 79 20 69 66 3a 0a 2a  ax() query if:.*
2bb69 2a 0a 2a 2a 20 20 20 31 2e 20 54 68 65 72 65 20  *.**   1. There 
2bb6a 69 73 20 61 20 73 69 6e 67 6c 65 20 6f 62 6a 65  is a single obje
2bb6b 63 74 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ct in the FROM c
2bb6c 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 32  lause..**.**   2
2bb6d 2e 20 54 68 65 72 65 20 69 73 20 61 20 73 69 6e  . There is a sin
2bb6e 67 6c 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  gle expression i
2bb6f 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
2bb70 2c 20 61 6e 64 20 69 74 20 69 73 0a 2a 2a 20 20  , and it is.**  
2bb71 20 20 20 20 65 69 74 68 65 72 20 6d 69 6e 28 78      either min(x
2bb72 29 20 6f 72 20 6d 61 78 28 78 29 2c 20 77 68 65  ) or max(x), whe
2bb73 72 65 20 78 20 69 73 20 61 20 63 6f 6c 75 6d 6e  re x is a column
2bb74 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2f 0a 73   reference..*/.s
2bb75 74 61 74 69 63 20 75 38 20 6d 69 6e 4d 61 78 51  tatic u8 minMaxQ
2bb76 75 65 72 79 28 53 65 6c 65 63 74 20 2a 70 29 7b  uery(Select *p){
2bb77 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a  .  Expr *pExpr;.
2bb78 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
2bb79 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a  st = p->pEList;.
2bb7a 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e  .  if( pEList->n
2bb7b 45 78 70 72 21 3d 31 20 29 20 72 65 74 75 72 6e  Expr!=1 ) return
2bb7c 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e   WHERE_ORDERBY_N
2bb7d 4f 52 4d 41 4c 3b 0a 20 20 70 45 78 70 72 20 3d  ORMAL;.  pExpr =
2bb7e 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
2bb7f 78 70 72 3b 0a 20 20 69 66 28 20 70 45 78 70 72  xpr;.  if( pExpr
2bb80 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e  ->op!=TK_AGG_FUN
2bb81 43 54 49 4f 4e 20 29 20 72 65 74 75 72 6e 20 30  CTION ) return 0
2bb82 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 45 78  ;.  if( NEVER(Ex
2bb83 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2bb84 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2bb85 74 29 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  t)) ) return 0;.
2bb86 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72    pEList = pExpr
2bb87 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 69 66 28  ->x.pList;.  if(
2bb88 20 70 45 4c 69 73 74 3d 3d 30 20 7c 7c 20 70 45   pEList==0 || pE
2bb89 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29  List->nExpr!=1 )
2bb8a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
2bb8b 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
2bb8c 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f  xpr->op!=TK_AGG_
2bb8d 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 20  COLUMN ) return 
2bb8e 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f  WHERE_ORDERBY_NO
2bb8f 52 4d 41 4c 3b 0a 20 20 61 73 73 65 72 74 28 20  RMAL;.  assert( 
2bb90 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2bb91 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
2bb92 6c 75 65 29 20 29 3b 0a 20 20 69 66 28 20 73 71  lue) );.  if( sq
2bb93 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 45 78  lite3StrICmp(pEx
2bb94 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d 69  pr->u.zToken,"mi
2bb95 6e 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  n")==0 ){.    re
2bb96 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52  turn WHERE_ORDER
2bb97 42 59 5f 4d 49 4e 3b 0a 20 20 7d 65 6c 73 65 20  BY_MIN;.  }else 
2bb98 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2bb99 6d 70 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  mp(pExpr->u.zTok
2bb9a 65 6e 2c 22 6d 61 78 22 29 3d 3d 30 20 29 7b 0a  en,"max")==0 ){.
2bb9b 20 20 20 20 72 65 74 75 72 6e 20 57 48 45 52 45      return WHERE
2bb9c 5f 4f 52 44 45 52 42 59 5f 4d 41 58 3b 0a 20 20  _ORDERBY_MAX;.  
2bb9d 7d 0a 20 20 72 65 74 75 72 6e 20 57 48 45 52 45  }.  return WHERE
2bb9e 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b  _ORDERBY_NORMAL;
2bb9f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65  .}../*.** The se
2bba0 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 70  lect statement p
2bba1 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
2bba2 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  st argument is a
2bba3 6e 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72  n aggregate quer
2bba4 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  y..** The second
2bba5 20 61 72 67 6d 65 6e 74 20 69 73 20 74 68 65 20   argment is the 
2bba6 61 73 73 6f 63 69 61 74 65 64 20 61 67 67 72 65  associated aggre
2bba7 67 61 74 65 2d 69 6e 66 6f 20 6f 62 6a 65 63 74  gate-info object
2bba8 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74  . This .** funct
2bba9 69 6f 6e 20 74 65 73 74 73 20 69 66 20 74 68 65  ion tests if the
2bbaa 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74 68   SELECT is of th
2bbab 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  e form:.**.**   
2bbac 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20  SELECT count(*) 
2bbad 46 52 4f 4d 20 3c 74 62 6c 3e 0a 2a 2a 0a 2a 2a  FROM <tbl>.**.**
2bbae 20 77 68 65 72 65 20 74 61 62 6c 65 20 69 73 20   where table is 
2bbaf 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  a database table
2bbb0 2c 20 6e 6f 74 20 61 20 73 75 62 2d 73 65 6c 65  , not a sub-sele
2bbb1 63 74 20 6f 72 20 76 69 65 77 2e 20 49 66 20 74  ct or view. If t
2bbb2 68 65 20 71 75 65 72 79 0a 2a 2a 20 64 6f 65 73  he query.** does
2bbb3 20 6d 61 74 63 68 20 74 68 69 73 20 70 61 74 74   match this patt
2bbb4 65 72 6e 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e  ern, then a poin
2bbb5 74 65 72 20 74 6f 20 74 68 65 20 54 61 62 6c 65  ter to the Table
2bbb6 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e   object represen
2bbb7 74 69 6e 67 0a 2a 2a 20 3c 74 62 6c 3e 20 69 73  ting.** <tbl> is
2bbb8 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
2bbb9 77 69 73 65 2c 20 30 20 69 73 20 72 65 74 75 72  wise, 0 is retur
2bbba 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54  ned..*/.static T
2bbbb 61 62 6c 65 20 2a 69 73 53 69 6d 70 6c 65 43 6f  able *isSimpleCo
2bbbc 75 6e 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 41  unt(Select *p, A
2bbbd 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
2bbbe 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  ){.  Table *pTab
2bbbf 3b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  ;.  Expr *pExpr;
2bbc0 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e  ..  assert( !p->
2bbc1 70 47 72 6f 75 70 42 79 20 29 3b 0a 0a 20 20 69  pGroupBy );..  i
2bbc2 66 28 20 70 2d 3e 70 57 68 65 72 65 20 7c 7c 20  f( p->pWhere || 
2bbc3 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
2bbc4 21 3d 31 20 0a 20 20 20 7c 7c 20 70 2d 3e 70 53  !=1 .   || p->pS
2bbc5 72 63 2d 3e 6e 53 72 63 21 3d 31 20 7c 7c 20 70  rc->nSrc!=1 || p
2bbc6 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65  ->pSrc->a[0].pSe
2bbc7 6c 65 63 74 0a 20 20 29 7b 0a 20 20 20 20 72 65  lect.  ){.    re
2bbc8 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 54  turn 0;.  }.  pT
2bbc9 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b  ab = p->pSrc->a[
2bbca 30 5d 2e 70 54 61 62 3b 0a 20 20 70 45 78 70 72  0].pTab;.  pExpr
2bbcb 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b   = p->pEList->a[
2bbcc 30 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 73 65  0].pExpr;.  asse
2bbcd 72 74 28 20 70 54 61 62 20 26 26 20 21 70 54 61  rt( pTab && !pTa
2bbce 62 2d 3e 70 53 65 6c 65 63 74 20 26 26 20 70 45  b->pSelect && pE
2bbcf 78 70 72 20 29 3b 0a 0a 20 20 69 66 28 20 49 73  xpr );..  if( Is
2bbd0 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20  Virtual(pTab) ) 
2bbd1 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
2bbd2 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47  pExpr->op!=TK_AG
2bbd3 47 5f 46 55 4e 43 54 49 4f 4e 20 29 20 72 65 74  G_FUNCTION ) ret
2bbd4 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 70 41  urn 0;.  if( (pA
2bbd5 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d  ggInfo->aFunc[0]
2bbd6 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 26 53 51  .pFunc->flags&SQ
2bbd7 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 29  LITE_FUNC_COUNT)
2bbd8 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
2bbd9 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61    if( pExpr->fla
2bbda 67 73 26 45 50 5f 44 69 73 74 69 6e 63 74 20 29  gs&EP_Distinct )
2bbdb 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 72 65   return 0;..  re
2bbdc 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a  turn pTab;.}../*
2bbdd 0a 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63  .** If the sourc
2bbde 65 2d 6c 69 73 74 20 69 74 65 6d 20 70 61 73 73  e-list item pass
2bbdf 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
2bbe0 74 20 77 61 73 20 61 75 67 6d 65 6e 74 65 64 20  t was augmented 
2bbe1 77 69 74 68 20 61 6e 0a 2a 2a 20 49 4e 44 45 58  with an.** INDEX
2bbe2 45 44 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68  ED BY clause, th
2bbe3 65 6e 20 74 72 79 20 74 6f 20 6c 6f 63 61 74 65  en try to locate
2bbe4 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 69   the specified i
2bbe5 6e 64 65 78 2e 20 49 66 20 74 68 65 72 65 0a 2a  ndex. If there.*
2bbe6 2a 20 77 61 73 20 73 75 63 68 20 61 20 63 6c 61  * was such a cla
2bbe7 75 73 65 20 61 6e 64 20 74 68 65 20 6e 61 6d 65  use and the name
2bbe8 64 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 62  d index cannot b
2bbe9 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20  e found, return 
2bbea 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  .** SQLITE_ERROR
2bbeb 20 61 6e 64 20 6c 65 61 76 65 20 61 6e 20 65 72   and leave an er
2bbec 72 6f 72 20 69 6e 20 70 50 61 72 73 65 2e 20 4f  ror in pParse. O
2bbed 74 68 65 72 77 69 73 65 2c 20 70 6f 70 75 6c 61  therwise, popula
2bbee 74 65 20 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 70 49  te .** pFrom->pI
2bbef 6e 64 65 78 20 61 6e 64 20 72 65 74 75 72 6e 20  ndex and return 
2bbf0 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51  SQLITE_OK..*/.SQ
2bbf1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2bbf2 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42   sqlite3IndexedB
2bbf3 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 70  yLookup(Parse *p
2bbf4 50 61 72 73 65 2c 20 73 74 72 75 63 74 20 53 72  Parse, struct Sr
2bbf5 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f  cList_item *pFro
2bbf6 6d 29 7b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d  m){.  if( pFrom-
2bbf7 3e 70 54 61 62 20 26 26 20 70 46 72 6f 6d 2d 3e  >pTab && pFrom->
2bbf8 7a 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 54 61  zIndex ){.    Ta
2bbf9 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f  ble *pTab = pFro
2bbfa 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 63 68 61  m->pTab;.    cha
2bbfb 72 20 2a 7a 49 6e 64 65 78 20 3d 20 70 46 72 6f  r *zIndex = pFro
2bbfc 6d 2d 3e 7a 49 6e 64 65 78 3b 0a 20 20 20 20 49  m->zIndex;.    I
2bbfd 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20  ndex *pIdx;.    
2bbfe 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
2bbff 49 6e 64 65 78 3b 20 0a 20 20 20 20 20 20 20 20  Index; .        
2bc00 70 49 64 78 20 26 26 20 73 71 6c 69 74 65 33 53  pIdx && sqlite3S
2bc01 74 72 49 43 6d 70 28 70 49 64 78 2d 3e 7a 4e 61  trICmp(pIdx->zNa
2bc02 6d 65 2c 20 7a 49 6e 64 65 78 29 3b 20 0a 20 20  me, zIndex); .  
2bc03 20 20 20 20 20 20 70 49 64 78 3d 70 49 64 78 2d        pIdx=pIdx-
2bc04 3e 70 4e 65 78 74 0a 20 20 20 20 29 3b 0a 20 20  >pNext.    );.  
2bc05 20 20 69 66 28 20 21 70 49 64 78 20 29 7b 0a 20    if( !pIdx ){. 
2bc06 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2bc07 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
2bc08 20 73 75 63 68 20 69 6e 64 65 78 3a 20 25 73 22   such index: %s"
2bc09 2c 20 7a 49 6e 64 65 78 2c 20 30 29 3b 0a 20 20  , zIndex, 0);.  
2bc0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2bc0b 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
2bc0c 20 20 20 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78     pFrom->pIndex
2bc0d 20 3d 20 70 49 64 78 3b 0a 20 20 7d 0a 20 20 72   = pIdx;.  }.  r
2bc0e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2bc0f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2bc10 6f 75 74 69 6e 65 20 69 73 20 61 20 57 61 6c 6b  outine is a Walk
2bc11 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  er callback for 
2bc12 22 65 78 70 61 6e 64 69 6e 67 22 20 61 20 53 45  "expanding" a SE
2bc13 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
2bc14 2a 2a 20 22 45 78 70 61 6e 64 69 6e 67 22 20 6d  ** "Expanding" m
2bc15 65 61 6e 73 20 74 6f 20 64 6f 20 74 68 65 20 66  eans to do the f
2bc16 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
2bc17 20 20 20 28 31 29 20 20 4d 61 6b 65 20 73 75 72     (1)  Make sur
2bc18 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
2bc19 6d 62 65 72 73 20 68 61 76 65 20 62 65 65 6e 20  mbers have been 
2bc1a 61 73 73 69 67 6e 65 64 20 74 6f 20 65 76 65 72  assigned to ever
2bc1b 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6c 65  y.**         ele
2bc1c 6d 65 6e 74 20 6f 66 20 74 68 65 20 46 52 4f 4d  ment of the FROM
2bc1d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20   clause..**.**  
2bc1e 20 20 28 32 29 20 20 46 69 6c 6c 20 69 6e 20 74    (2)  Fill in t
2bc1f 68 65 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d  he pTabList->a[]
2bc20 2e 70 54 61 62 20 66 69 65 6c 64 73 20 69 6e 20  .pTab fields in 
2bc21 74 68 65 20 53 72 63 4c 69 73 74 20 74 68 61 74  the SrcList that
2bc22 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 64 65 66   .**         def
2bc23 69 6e 65 73 20 46 52 4f 4d 20 63 6c 61 75 73 65  ines FROM clause
2bc24 2e 20 20 57 68 65 6e 20 76 69 65 77 73 20 61 70  .  When views ap
2bc25 70 65 61 72 20 69 6e 20 74 68 65 20 46 52 4f 4d  pear in the FROM
2bc26 20 63 6c 61 75 73 65 2c 0a 2a 2a 20 20 20 20 20   clause,.**     
2bc27 20 20 20 20 66 69 6c 6c 20 70 54 61 62 4c 69 73      fill pTabLis
2bc28 74 2d 3e 61 5b 5d 2e 70 53 65 6c 65 63 74 20 77  t->a[].pSelect w
2bc29 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ith a copy of th
2bc2a 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2bc2b 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 68  nt.**         th
2bc2c 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
2bc2d 65 20 76 69 65 77 2e 20 20 41 20 63 6f 70 79 20  e view.  A copy 
2bc2e 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 76  is made of the v
2bc2f 69 65 77 27 73 20 53 45 4c 45 43 54 0a 2a 2a 20  iew's SELECT.** 
2bc30 20 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e          statemen
2bc31 74 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e  t so that we can
2bc32 20 66 72 65 65 6c 79 20 6d 6f 64 69 66 79 20 6f   freely modify o
2bc33 72 20 64 65 6c 65 74 65 20 74 68 61 74 20 73 74  r delete that st
2bc34 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20  atement.**      
2bc35 20 20 20 77 69 74 68 6f 75 74 20 77 6f 72 72 79     without worry
2bc36 69 6e 67 20 61 62 6f 75 74 20 6d 65 73 73 69 6e  ing about messin
2bc37 67 20 75 70 20 74 68 65 20 70 72 65 73 69 73 74  g up the presist
2bc38 65 6e 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  ent representati
2bc39 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 66  on.**         of
2bc3a 20 74 68 65 20 76 69 65 77 2e 0a 2a 2a 0a 2a 2a   the view..**.**
2bc3b 20 20 20 20 28 33 29 20 20 41 64 64 20 74 65 72      (3)  Add ter
2bc3c 6d 73 20 74 6f 20 74 68 65 20 57 48 45 52 45 20  ms to the WHERE 
2bc3d 63 6c 61 75 73 65 20 74 6f 20 61 63 63 6f 6d 6f  clause to accomo
2bc3e 64 61 74 65 20 74 68 65 20 4e 41 54 55 52 41 4c  date the NATURAL
2bc3f 20 6b 65 79 77 6f 72 64 0a 2a 2a 20 20 20 20 20   keyword.**     
2bc40 20 20 20 20 6f 6e 20 6a 6f 69 6e 73 20 61 6e 64      on joins and
2bc41 20 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e   the ON and USIN
2bc42 47 20 63 6c 61 75 73 65 20 6f 66 20 6a 6f 69 6e  G clause of join
2bc43 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29 20  s..**.**    (4) 
2bc44 20 53 63 61 6e 20 74 68 65 20 6c 69 73 74 20 6f   Scan the list o
2bc45 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
2bc46 20 72 65 73 75 6c 74 20 73 65 74 20 28 70 45 4c   result set (pEL
2bc47 69 73 74 29 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20  ist) looking.** 
2bc48 20 20 20 20 20 20 20 20 66 6f 72 20 69 6e 73 74          for inst
2bc49 61 6e 63 65 73 20 6f 66 20 74 68 65 20 22 2a 22  ances of the "*"
2bc4a 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 74 68 65   operator or the
2bc4b 20 54 41 42 4c 45 2e 2a 20 6f 70 65 72 61 74 6f   TABLE.* operato
2bc4c 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 49 66  r..**         If
2bc4d 20 66 6f 75 6e 64 2c 20 65 78 70 61 6e 64 20 65   found, expand e
2bc4e 61 63 68 20 22 2a 22 20 74 6f 20 62 65 20 65 76  ach "*" to be ev
2bc4f 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 65 76  ery column in ev
2bc50 65 72 79 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  ery table.**    
2bc51 20 20 20 20 20 61 6e 64 20 54 41 42 4c 45 2e 2a       and TABLE.*
2bc52 20 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c   to be every col
2bc53 75 6d 6e 20 69 6e 20 54 41 42 4c 45 2e 0a 2a 2a  umn in TABLE..**
2bc54 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2bc55 65 6c 65 63 74 45 78 70 61 6e 64 65 72 28 57 61  electExpander(Wa
2bc56 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53  lker *pWalker, S
2bc57 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 50 61 72  elect *p){.  Par
2bc58 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 61  se *pParse = pWa
2bc59 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20  lker->pParse;.  
2bc5a 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 0a 20 20 53  int i, j, k;.  S
2bc5b 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
2bc5c 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ;.  ExprList *pE
2bc5d 4c 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 53  List;.  struct S
2bc5e 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72  rcList_item *pFr
2bc5f 6f 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  om;.  sqlite3 *d
2bc60 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2bc61 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2bc62 63 46 61 69 6c 65 64 20 20 29 7b 0a 20 20 20 20  cFailed  ){.    
2bc63 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2bc64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 45 56 45  ;.  }.  if( NEVE
2bc65 52 28 70 2d 3e 70 53 72 63 3d 3d 30 29 20 7c 7c  R(p->pSrc==0) ||
2bc66 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20   (p->selFlags & 
2bc67 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20  SF_Expanded)!=0 
2bc68 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52  ){.    return WR
2bc69 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70  C_Prune;.  }.  p
2bc6a 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
2bc6b 5f 45 78 70 61 6e 64 65 64 3b 0a 20 20 70 54 61  _Expanded;.  pTa
2bc6c 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b  bList = p->pSrc;
2bc6d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70  .  pEList = p->p
2bc6e 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 4d 61 6b  EList;..  /* Mak
2bc6f 65 20 73 75 72 65 20 63 75 72 73 6f 72 20 6e 75  e sure cursor nu
2bc70 6d 62 65 72 73 20 68 61 76 65 20 62 65 65 6e 20  mbers have been 
2bc71 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6c 6c 20  assigned to all 
2bc72 65 6e 74 72 69 65 73 20 69 6e 0a 20 20 2a 2a 20  entries in.  ** 
2bc73 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2bc74 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  of the SELECT st
2bc75 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20  atement..  */.  
2bc76 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73  sqlite3SrcListAs
2bc77 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72  signCursors(pPar
2bc78 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 0a  se, pTabList);..
2bc79 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 65 76 65    /* Look up eve
2bc7a 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 69  ry table named i
2bc7b 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
2bc7c 65 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2e  e of the select.
2bc7d 20 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 6e 74    If.  ** an ent
2bc7e 72 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  ry of the FROM c
2bc7f 6c 61 75 73 65 20 69 73 20 61 20 73 75 62 71 75  lause is a subqu
2bc80 65 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 61  ery instead of a
2bc81 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2c 0a   table or view,.
2bc82 20 20 2a 2a 20 74 68 65 6e 20 63 72 65 61 74 65    ** then create
2bc83 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62   a transient tab
2bc84 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  le structure to 
2bc85 64 65 73 63 72 69 62 65 20 74 68 65 20 73 75 62  describe the sub
2bc86 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f  query..  */.  fo
2bc87 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54 61  r(i=0, pFrom=pTa
2bc88 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61 62  bList->a; i<pTab
2bc89 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
2bc8a 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20 54   pFrom++){.    T
2bc8b 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20  able *pTab;.    
2bc8c 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 21  if( pFrom->pTab!
2bc8d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  =0 ){.      /* T
2bc8e 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 68 61  his statement ha
2bc8f 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70  s already been p
2bc90 72 65 70 61 72 65 64 2e 20 20 54 68 65 72 65 20  repared.  There 
2bc91 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20 20  is no need.     
2bc92 20 2a 2a 20 74 6f 20 67 6f 20 66 75 72 74 68 65   ** to go furthe
2bc93 72 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  r. */.      asse
2bc94 72 74 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20  rt( i==0 );.    
2bc95 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
2bc96 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ne;.    }.    if
2bc97 28 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3d 3d  ( pFrom->zName==
2bc98 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  0 ){.#ifndef SQL
2bc99 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2bc9a 59 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  Y.      Select *
2bc9b 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70 53  pSel = pFrom->pS
2bc9c 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 2f 2a 20  elect;.      /* 
2bc9d 41 20 73 75 62 2d 71 75 65 72 79 20 69 6e 20 74  A sub-query in t
2bc9e 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
2bc9f 66 20 61 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  f a SELECT */.  
2bca0 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c      assert( pSel
2bca1 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  !=0 );.      ass
2bca2 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62  ert( pFrom->pTab
2bca3 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
2bca4 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70  ite3WalkSelect(p
2bca5 57 61 6c 6b 65 72 2c 20 70 53 65 6c 29 3b 0a 20  Walker, pSel);. 
2bca6 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54 61 62       pFrom->pTab
2bca7 20 3d 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65   = pTab = sqlite
2bca8 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2bca9 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29  , sizeof(Table))
2bcaa 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ;.      if( pTab
2bcab 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
2bcac 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 70 54  _Abort;.      pT
2bcad 61 62 2d 3e 64 62 4d 65 6d 20 3d 20 64 62 2d 3e  ab->dbMem = db->
2bcae 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
2bcaf 65 64 20 3f 20 64 62 20 3a 20 30 3b 0a 20 20 20  ed ? db : 0;.   
2bcb0 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20     pTab->nRef = 
2bcb1 31 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  1;.      pTab->z
2bcb2 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  Name = sqlite3MP
2bcb3 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74  rintf(db, "sqlit
2bcb4 65 5f 73 75 62 71 75 65 72 79 5f 25 70 5f 22 2c  e_subquery_%p_",
2bcb5 20 28 76 6f 69 64 2a 29 70 54 61 62 29 3b 0a 20   (void*)pTab);. 
2bcb6 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 65 6c       while( pSel
2bcb7 2d 3e 70 50 72 69 6f 72 20 29 7b 20 70 53 65 6c  ->pPrior ){ pSel
2bcb8 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 3b   = pSel->pPrior;
2bcb9 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63 74 43   }.      selectC
2bcba 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69  olumnsFromExprLi
2bcbb 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d  st(pParse, pSel-
2bcbc 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d 3e  >pEList, &pTab->
2bcbd 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43 6f  nCol, &pTab->aCo
2bcbe 6c 29 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  l);.      pTab->
2bcbf 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 20 20  iPKey = -1;.    
2bcc0 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73    pTab->tabFlags
2bcc1 20 7c 3d 20 54 46 5f 45 70 68 65 6d 65 72 61 6c   |= TF_Ephemeral
2bcc2 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c  ;.#endif.    }el
2bcc3 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20  se{.      /* An 
2bcc4 6f 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20 6f  ordinary table o
2bcc5 72 20 76 69 65 77 20 6e 61 6d 65 20 69 6e 20 74  r view name in t
2bcc6 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
2bcc7 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
2bcc8 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29  pFrom->pTab==0 )
2bcc9 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70  ;.      pFrom->p
2bcca 54 61 62 20 3d 20 70 54 61 62 20 3d 20 0a 20 20  Tab = pTab = .  
2bccb 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c 6f 63        sqlite3Loc
2bccc 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
2bccd 30 2c 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 2c 70  0,pFrom->zName,p
2bcce 46 72 6f 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29  From->zDatabase)
2bccf 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ;.      if( pTab
2bcd0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
2bcd1 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 70 54  _Abort;.      pT
2bcd2 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 69 66 20  ab->nRef++;.#if 
2bcd3 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2bcd4 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
2bcd5 65 66 69 6e 65 64 20 28 53 51 4c 49 54 45 5f 4f  efined (SQLITE_O
2bcd6 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2bcd7 29 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ).      if( pTab
2bcd8 2d 3e 70 53 65 6c 65 63 74 20 7c 7c 20 49 73 56  ->pSelect || IsV
2bcd9 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
2bcda 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 72 65          /* We re
2bcdb 61 63 68 20 68 65 72 65 20 69 66 20 74 68 65 20  ach here if the 
2bcdc 6e 61 6d 65 64 20 74 61 62 6c 65 20 69 73 20 61  named table is a
2bcdd 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77 20 2a   really a view *
2bcde 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  /.        if( sq
2bcdf 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
2bce0 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
2bce1 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 57  pTab) ) return W
2bce2 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  RC_Abort;.      
2bce3 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d    assert( pFrom-
2bce4 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20  >pSelect==0 );. 
2bce5 20 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 53         pFrom->pS
2bce6 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53  elect = sqlite3S
2bce7 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54 61  electDup(db, pTa
2bce8 62 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a  b->pSelect, 0);.
2bce9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57          sqlite3W
2bcea 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65  alkSelect(pWalke
2bceb 72 2c 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63  r, pFrom->pSelec
2bcec 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  t);.      }.#end
2bced 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  if.    }..    /*
2bcee 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 64 65   Locate the inde
2bcef 78 20 6e 61 6d 65 64 20 62 79 20 74 68 65 20 49  x named by the I
2bcf0 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65  NDEXED BY clause
2bcf1 2c 20 69 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20  , if any. */.   
2bcf2 20 69 66 28 20 73 71 6c 69 74 65 33 49 6e 64 65   if( sqlite3Inde
2bcf3 78 65 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72  xedByLookup(pPar
2bcf4 73 65 2c 20 70 46 72 6f 6d 29 20 29 7b 0a 20 20  se, pFrom) ){.  
2bcf5 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
2bcf6 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  bort;.    }.  }.
2bcf7 0a 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 4e 41  .  /* Process NA
2bcf8 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 73 2c 20  TURAL keywords, 
2bcf9 61 6e 64 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  and ON and USING
2bcfa 20 63 6c 61 75 73 65 73 20 6f 66 20 6a 6f 69 6e   clauses of join
2bcfb 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  s..  */.  if( db
2bcfc 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
2bcfd 7c 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a  | sqliteProcessJ
2bcfe 6f 69 6e 28 70 50 61 72 73 65 2c 20 70 29 20 29  oin(pParse, p) )
2bcff 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  {.    return WRC
2bd00 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 2f  _Abort;.  }..  /
2bd01 2a 20 46 6f 72 20 65 76 65 72 79 20 22 2a 22 20  * For every "*" 
2bd02 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 74  that occurs in t
2bd03 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2c 20  he column list, 
2bd04 69 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73  insert the names
2bd05 20 6f 66 0a 20 20 2a 2a 20 61 6c 6c 20 63 6f 6c   of.  ** all col
2bd06 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c  umns in all tabl
2bd07 65 73 2e 20 20 41 6e 64 20 66 6f 72 20 65 76 65  es.  And for eve
2bd08 72 79 20 54 41 42 4c 45 2e 2a 20 69 6e 73 65 72  ry TABLE.* inser
2bd09 74 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a  t the names.  **
2bd0a 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20   of all columns 
2bd0b 69 6e 20 54 41 42 4c 45 2e 20 20 54 68 65 20 70  in TABLE.  The p
2bd0c 61 72 73 65 72 20 69 6e 73 65 72 74 65 64 20 61  arser inserted a
2bd0d 20 73 70 65 63 69 61 6c 20 65 78 70 72 65 73 73   special express
2bd0e 69 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 20 74 68  ion.  ** with th
2bd0f 65 20 54 4b 5f 41 4c 4c 20 6f 70 65 72 61 74 6f  e TK_ALL operato
2bd10 72 20 66 6f 72 20 65 61 63 68 20 22 2a 22 20 74  r for each "*" t
2bd11 68 61 74 20 69 74 20 66 6f 75 6e 64 20 69 6e 20  hat it found in 
2bd12 74 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e  the column list.
2bd13 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  ** The follow
2bd14 69 6e 67 20 63 6f 64 65 20 6a 75 73 74 20 68 61  ing code just ha
2bd15 73 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  s to locate the 
2bd16 54 4b 5f 41 4c 4c 20 65 78 70 72 65 73 73 69 6f  TK_ALL expressio
2bd17 6e 73 20 61 6e 64 20 65 78 70 61 6e 64 0a 20 20  ns and expand.  
2bd18 2a 2a 20 65 61 63 68 20 6f 6e 65 20 74 6f 20 74  ** each one to t
2bd19 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63  he list of all c
2bd1a 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61  olumns in all ta
2bd1b 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  bles..  **.  ** 
2bd1c 54 68 65 20 66 69 72 73 74 20 6c 6f 6f 70 20 6a  The first loop j
2bd1d 75 73 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65  ust checks to se
2bd1e 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  e if there are a
2bd1f 6e 79 20 22 2a 22 20 6f 70 65 72 61 74 6f 72 73  ny "*" operators
2bd20 0a 20 20 2a 2a 20 74 68 61 74 20 6e 65 65 64 20  .  ** that need 
2bd21 65 78 70 61 6e 64 69 6e 67 2e 0a 20 20 2a 2f 0a  expanding..  */.
2bd22 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c    for(k=0; k<pEL
2bd23 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29  ist->nExpr; k++)
2bd24 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d  {.    Expr *pE =
2bd25 20 70 45 4c 69 73 74 2d 3e 61 5b 6b 5d 2e 70 45   pEList->a[k].pE
2bd26 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45 2d  xpr;.    if( pE-
2bd27 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62 72  >op==TK_ALL ) br
2bd28 65 61 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28  eak;.    assert(
2bd29 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20   pE->op!=TK_DOT 
2bd2a 7c 7c 20 70 45 2d 3e 70 52 69 67 68 74 21 3d 30  || pE->pRight!=0
2bd2b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2bd2c 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c  pE->op!=TK_DOT |
2bd2d 7c 20 28 70 45 2d 3e 70 4c 65 66 74 21 3d 30 20  | (pE->pLeft!=0 
2bd2e 26 26 20 70 45 2d 3e 70 4c 65 66 74 2d 3e 6f 70  && pE->pLeft->op
2bd2f 3d 3d 54 4b 5f 49 44 29 20 29 3b 0a 20 20 20 20  ==TK_ID) );.    
2bd30 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44  if( pE->op==TK_D
2bd31 4f 54 20 26 26 20 70 45 2d 3e 70 52 69 67 68 74  OT && pE->pRight
2bd32 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62  ->op==TK_ALL ) b
2bd33 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  reak;.  }.  if( 
2bd34 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  k<pEList->nExpr 
2bd35 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  ){.    /*.    **
2bd36 20 49 66 20 77 65 20 67 65 74 20 68 65 72 65 20   If we get here 
2bd37 69 74 20 6d 65 61 6e 73 20 74 68 65 20 72 65 73  it means the res
2bd38 75 6c 74 20 73 65 74 20 63 6f 6e 74 61 69 6e 73  ult set contains
2bd39 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 22 2a 22   one or more "*"
2bd3a 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72  .    ** operator
2bd3b 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62  s that need to b
2bd3c 65 20 65 78 70 61 6e 64 65 64 2e 20 20 4c 6f 6f  e expanded.  Loo
2bd3d 70 20 74 68 72 6f 75 67 68 20 65 61 63 68 20 65  p through each e
2bd3e 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 2a 2a  xpression.    **
2bd3f 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
2bd40 65 74 20 61 6e 64 20 65 78 70 61 6e 64 20 74 68  et and expand th
2bd41 65 6d 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20  em one by one.. 
2bd42 20 20 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74     */.    struct
2bd43 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
2bd44 61 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20  a = pEList->a;. 
2bd45 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65     ExprList *pNe
2bd46 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 66  w = 0;.    int f
2bd47 6c 61 67 73 20 3d 20 70 50 61 72 73 65 2d 3e 64  lags = pParse->d
2bd48 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 20 20 69 6e  b->flags;.    in
2bd49 74 20 6c 6f 6e 67 4e 61 6d 65 73 20 3d 20 28 66  t longNames = (f
2bd4a 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75  lags & SQLITE_Fu
2bd4b 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 0a 20  llColNames)!=0. 
2bd4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bd4d 20 20 20 20 20 26 26 20 28 66 6c 61 67 73 20 26       && (flags &
2bd4e 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c   SQLITE_ShortCol
2bd4f 4e 61 6d 65 73 29 3d 3d 30 3b 0a 0a 20 20 20 20  Names)==0;..    
2bd50 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73  for(k=0; k<pELis
2bd51 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a  t->nExpr; k++){.
2bd52 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d        Expr *pE =
2bd53 20 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20 20 20   a[k].pExpr;.   
2bd54 20 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e 6f     assert( pE->o
2bd55 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d  p!=TK_DOT || pE-
2bd56 3e 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20  >pRight!=0 );.  
2bd57 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 21 3d      if( pE->op!=
2bd58 54 4b 5f 41 4c 4c 20 26 26 20 28 70 45 2d 3e 6f  TK_ALL && (pE->o
2bd59 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d  p!=TK_DOT || pE-
2bd5a 3e 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f  >pRight->op!=TK_
2bd5b 41 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ALL) ){.        
2bd5c 2f 2a 20 54 68 69 73 20 70 61 72 74 69 63 75 6c  /* This particul
2bd5d 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 64 6f  ar expression do
2bd5e 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
2bd5f 65 20 65 78 70 61 6e 64 65 64 2e 0a 20 20 20 20  e expanded..    
2bd60 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70      */.        p
2bd61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
2bd62 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2bd63 73 65 2c 20 70 4e 65 77 2c 20 61 5b 6b 5d 2e 70  se, pNew, a[k].p
2bd64 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 69  Expr);.        i
2bd65 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  f( pNew ){.     
2bd66 20 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65       pNew->a[pNe
2bd67 77 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a 4e 61 6d  w->nExpr-1].zNam
2bd68 65 20 3d 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 3b 0a  e = a[k].zName;.
2bd69 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e            pNew->
2bd6a 61 5b 70 4e 65 77 2d 3e 6e 45 78 70 72 2d 31 5d  a[pNew->nExpr-1]
2bd6b 2e 7a 53 70 61 6e 20 3d 20 61 5b 6b 5d 2e 7a 53  .zSpan = a[k].zS
2bd6c 70 61 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 61  pan;.          a
2bd6d 5b 6b 5d 2e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20  [k].zName = 0;. 
2bd6e 20 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 53           a[k].zS
2bd6f 70 61 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  pan = 0;.       
2bd70 20 7d 0a 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e   }.        a[k].
2bd71 70 45 78 70 72 20 3d 20 30 3b 0a 20 20 20 20 20  pExpr = 0;.     
2bd72 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2bd73 2f 2a 20 54 68 69 73 20 65 78 70 72 65 73 73 69  /* This expressi
2bd74 6f 6e 20 69 73 20 61 20 22 2a 22 20 6f 72 20 61  on is a "*" or a
2bd75 20 22 54 41 42 4c 45 2e 2a 22 20 61 6e 64 20 6e   "TABLE.*" and n
2bd76 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 20  eeds to be.     
2bd77 20 20 20 2a 2a 20 65 78 70 61 6e 64 65 64 2e 20     ** expanded. 
2bd78 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74  */.        int t
2bd79 61 62 6c 65 53 65 65 6e 20 3d 20 30 3b 20 20 20  ableSeen = 0;   
2bd7a 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 77     /* Set to 1 w
2bd7b 68 65 6e 20 54 41 42 4c 45 20 6d 61 74 63 68 65  hen TABLE matche
2bd7c 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61  s */.        cha
2bd7d 72 20 2a 7a 54 4e 61 6d 65 3b 20 20 20 20 20 20  r *zTName;      
2bd7e 20 20 20 20 20 20 2f 2a 20 74 65 78 74 20 6f 66        /* text of
2bd7f 20 6e 61 6d 65 20 6f 66 20 54 41 42 4c 45 20 2a   name of TABLE *
2bd80 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45  /.        if( pE
2bd81 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 7b 0a  ->op==TK_DOT ){.
2bd82 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2bd83 28 20 70 45 2d 3e 70 4c 65 66 74 21 3d 30 20 29  ( pE->pLeft!=0 )
2bd84 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
2bd85 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
2bd86 65 72 74 79 28 70 45 2d 3e 70 4c 65 66 74 2c 20  erty(pE->pLeft, 
2bd87 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a  EP_IntValue) );.
2bd88 20 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65            zTName
2bd89 20 3d 20 70 45 2d 3e 70 4c 65 66 74 2d 3e 75 2e   = pE->pLeft->u.
2bd8a 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  zToken;.        
2bd8b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2bd8c 20 7a 54 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20   zTName = 0;.   
2bd8d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66       }.        f
2bd8e 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54  or(i=0, pFrom=pT
2bd8f 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61  abList->a; i<pTa
2bd90 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
2bd91 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20  , pFrom++){.    
2bd92 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
2bd93 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b  b = pFrom->pTab;
2bd94 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
2bd95 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 46 72 6f  *zTabName = pFro
2bd96 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20  m->zAlias;.     
2bd97 20 20 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d       if( zTabNam
2bd98 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e==0 ){.        
2bd99 20 20 20 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70      zTabName = p
2bd9a 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
2bd9b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2bd9c 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2bd9d 46 61 69 6c 65 64 20 29 20 62 72 65 61 6b 3b 0a  Failed ) break;.
2bd9e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54            if( zT
2bd9f 4e 61 6d 65 20 26 26 20 73 71 6c 69 74 65 33 53  Name && sqlite3S
2bda0 74 72 49 43 6d 70 28 7a 54 4e 61 6d 65 2c 20 7a  trICmp(zTName, z
2bda1 54 61 62 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20  TabName)!=0 ){. 
2bda2 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69             conti
2bda3 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  nue;.          }
2bda4 0a 20 20 20 20 20 20 20 20 20 20 74 61 62 6c 65  .          table
2bda5 53 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20  Seen = 1;.      
2bda6 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
2bda7 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b  Tab->nCol; j++){
2bda8 0a 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70  .            Exp
2bda9 72 20 2a 70 45 78 70 72 2c 20 2a 70 52 69 67 68  r *pExpr, *pRigh
2bdaa 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  t;.            c
2bdab 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 54 61  har *zName = pTa
2bdac 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65  b->aCol[j].zName
2bdad 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  ;.            ch
2bdae 61 72 20 2a 7a 43 6f 6c 6e 61 6d 65 3b 20 20 2f  ar *zColname;  /
2bdaf 2a 20 54 68 65 20 63 6f 6d 70 75 74 65 64 20 63  * The computed c
2bdb0 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  olumn name */.  
2bdb1 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
2bdb2 7a 54 6f 46 72 65 65 3b 20 20 20 2f 2a 20 4d 61  zToFree;   /* Ma
2bdb3 6c 6c 6f 63 65 64 20 73 74 72 69 6e 67 20 74 68  lloced string th
2bdb4 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66  at needs to be f
2bdb5 72 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  reed */.        
2bdb6 20 20 20 20 54 6f 6b 65 6e 20 73 43 6f 6c 6e 61      Token sColna
2bdb7 6d 65 3b 20 20 2f 2a 20 43 6f 6d 70 75 74 65 64  me;  /* Computed
2bdb8 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20   column name as 
2bdb9 61 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 20 20  a token */..    
2bdba 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20          /* If a 
2bdbb 63 6f 6c 75 6d 6e 20 69 73 20 6d 61 72 6b 65 64  column is marked
2bdbc 20 61 73 20 27 68 69 64 64 65 6e 27 20 28 63 75   as 'hidden' (cu
2bdbd 72 72 65 6e 74 6c 79 20 6f 6e 6c 79 20 70 6f 73  rrently only pos
2bdbe 73 69 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20  sible.          
2bdbf 20 20 2a 2a 20 66 6f 72 20 76 69 72 74 75 61 6c    ** for virtual
2bdc0 20 74 61 62 6c 65 73 29 2c 20 64 6f 20 6e 6f 74   tables), do not
2bdc1 20 69 6e 63 6c 75 64 65 20 69 74 20 69 6e 20 74   include it in t
2bdc2 68 65 20 65 78 70 61 6e 64 65 64 0a 20 20 20 20  he expanded.    
2bdc3 20 20 20 20 20 20 20 20 2a 2a 20 72 65 73 75 6c          ** resul
2bdc4 74 2d 73 65 74 20 6c 69 73 74 2e 0a 20 20 20 20  t-set list..    
2bdc5 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2bdc6 20 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64         if( IsHid
2bdc7 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d  denColumn(&pTab-
2bdc8 3e 61 43 6f 6c 5b 6a 5d 29 20 29 7b 0a 20 20 20  >aCol[j]) ){.   
2bdc9 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
2bdca 74 28 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  t(IsVirtual(pTab
2bdcb 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ));.            
2bdcc 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
2bdcd 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
2bdce 20 20 20 20 20 20 20 69 66 28 20 69 3e 30 20 26         if( i>0 &
2bdcf 26 20 7a 54 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20  & zTName==0 ){. 
2bdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72               str
2bdd1 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2bdd2 20 2a 70 4c 65 66 74 20 3d 20 26 70 54 61 62 4c   *pLeft = &pTabL
2bdd3 69 73 74 2d 3e 61 5b 69 2d 31 5d 3b 0a 20 20 20  ist->a[i-1];.   
2bdd4 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28             if( (
2bdd5 70 4c 65 66 74 5b 31 5d 2e 6a 6f 69 6e 74 79 70  pLeft[1].jointyp
2bdd6 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 29 21  e & JT_NATURAL)!
2bdd7 3d 30 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  =0 &&.          
2bdd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
2bdd9 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66 74 2d  lumnIndex(pLeft-
2bdda 3e 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30  >pTab, zName)>=0
2bddb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2bddc 20 20 20 20 2f 2a 20 49 6e 20 61 20 4e 41 54 55      /* In a NATU
2bddd 52 41 4c 20 6a 6f 69 6e 2c 20 6f 6d 69 74 20 74  RAL join, omit t
2bdde 68 65 20 6a 6f 69 6e 20 63 6f 6c 75 6d 6e 73 20  he join columns 
2bddf 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 20 20  from the .      
2bde0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62            ** tab
2bde1 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20  le on the right 
2bde2 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
2bde3 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2bde4 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2bde5 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73             if( s
2bde6 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65  qlite3IdListInde
2bde7 78 28 70 4c 65 66 74 5b 31 5d 2e 70 55 73 69 6e  x(pLeft[1].pUsin
2bde8 67 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a  g, zName)>=0 ){.
2bde9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bdea 2f 2a 20 49 6e 20 61 20 6a 6f 69 6e 20 77 69 74  /* In a join wit
2bdeb 68 20 61 20 55 53 49 4e 47 20 63 6c 61 75 73 65  h a USING clause
2bdec 2c 20 6f 6d 69 74 20 63 6f 6c 75 6d 6e 73 20 69  , omit columns i
2bded 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  n the.          
2bdee 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 63        ** using c
2bdef 6c 61 75 73 65 20 66 72 6f 6d 20 74 68 65 20 74  lause from the t
2bdf0 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68  able on the righ
2bdf1 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  t. */.          
2bdf2 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
2bdf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
2bdf4 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2bdf5 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74            pRight
2bdf6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
2bdf7 62 2c 20 54 4b 5f 49 44 2c 20 7a 4e 61 6d 65 29  b, TK_ID, zName)
2bdf8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 43  ;.            zC
2bdf9 6f 6c 6e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a  olname = zName;.
2bdfa 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 6f 46              zToF
2bdfb 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ree = 0;.       
2bdfc 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d       if( longNam
2bdfd 65 73 20 7c 7c 20 70 54 61 62 4c 69 73 74 2d 3e  es || pTabList->
2bdfe 6e 53 72 63 3e 31 20 29 7b 0a 20 20 20 20 20 20  nSrc>1 ){.      
2bdff 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c          Expr *pL
2be00 65 66 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eft;.           
2be01 20 20 20 70 4c 65 66 74 20 3d 20 73 71 6c 69 74     pLeft = sqlit
2be02 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 44  e3Expr(db, TK_ID
2be03 2c 20 7a 54 61 62 4e 61 6d 65 29 3b 0a 20 20 20  , zTabName);.   
2be04 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
2be05 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2be06 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20  pParse, TK_DOT, 
2be07 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30  pLeft, pRight, 0
2be08 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
2be09 20 69 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20 29   if( longNames )
2be0a 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2be0b 20 20 7a 43 6f 6c 6e 61 6d 65 20 3d 20 73 71 6c    zColname = sql
2be0c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2be0d 22 25 73 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d  "%s.%s", zTabNam
2be0e 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  e, zName);.     
2be0f 20 20 20 20 20 20 20 20 20 20 20 7a 54 6f 46 72             zToFr
2be10 65 65 20 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0a 20  ee = zColname;. 
2be11 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
2be12 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
2be13 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2be14 70 45 78 70 72 20 3d 20 70 52 69 67 68 74 3b 0a  pExpr = pRight;.
2be15 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2be16 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d            pNew =
2be17 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2be18 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
2be19 4e 65 77 2c 20 70 45 78 70 72 29 3b 0a 20 20 20  New, pExpr);.   
2be1a 20 20 20 20 20 20 20 20 20 73 43 6f 6c 6e 61 6d           sColnam
2be1b 65 2e 7a 20 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0a  e.z = zColname;.
2be1c 20 20 20 20 20 20 20 20 20 20 20 20 73 43 6f 6c              sCol
2be1d 6e 61 6d 65 2e 6e 20 3d 20 73 71 6c 69 74 65 33  name.n = sqlite3
2be1e 53 74 72 6c 65 6e 33 30 28 7a 43 6f 6c 6e 61 6d  Strlen30(zColnam
2be1f 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
2be20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53  sqlite3ExprListS
2be21 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  etName(pParse, p
2be22 4e 65 77 2c 20 26 73 43 6f 6c 6e 61 6d 65 2c 20  New, &sColname, 
2be23 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
2be24 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2be25 2c 20 7a 54 6f 46 72 65 65 29 3b 0a 20 20 20 20  , zToFree);.    
2be26 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2be27 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 74  }.        if( !t
2be28 61 62 6c 65 53 65 65 6e 20 29 7b 0a 20 20 20 20  ableSeen ){.    
2be29 20 20 20 20 20 20 69 66 28 20 7a 54 4e 61 6d 65        if( zTName
2be2a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2be2b 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2be2c 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
2be2d 20 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 4e   table: %s", zTN
2be2e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ame);.          
2be2f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2be30 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2be31 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 74  sg(pParse, "no t
2be32 61 62 6c 65 73 20 73 70 65 63 69 66 69 65 64 22  ables specified"
2be33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
2be34 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2be35 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2be36 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
2be37 28 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  (db, pEList);.  
2be38 20 20 70 2d 3e 70 45 4c 69 73 74 20 3d 20 70 4e    p->pEList = pN
2be39 65 77 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49  ew;.  }.#if SQLI
2be3a 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20  TE_MAX_COLUMN.  
2be3b 69 66 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26  if( p->pEList &&
2be3c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
2be3d 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  r>db->aLimit[SQL
2be3e 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
2be3f 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
2be40 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2be41 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d   "too many colum
2be42 6e 73 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74  ns in result set
2be43 22 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ");.  }.#endif. 
2be44 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
2be45 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4e  inue;.}../*.** N
2be46 6f 2d 6f 70 20 72 6f 75 74 69 6e 65 20 66 6f 72  o-op routine for
2be47 20 74 68 65 20 70 61 72 73 65 2d 74 72 65 65 20   the parse-tree 
2be48 77 61 6c 6b 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68  walker..**.** Wh
2be49 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
2be4a 69 73 20 74 68 65 20 57 61 6c 6b 65 72 2e 78 45  is the Walker.xE
2be4b 78 70 72 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e  xprCallback then
2be4c 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
2be4d 73 0a 2a 2a 20 61 72 65 20 77 61 6c 6b 65 64 20  s.** are walked 
2be4e 77 69 74 68 6f 75 74 20 61 6e 79 20 61 63 74 69  without any acti
2be4f 6f 6e 73 20 62 65 69 6e 67 20 74 61 6b 65 6e 20  ons being taken 
2be50 61 74 20 65 61 63 68 20 6e 6f 64 65 2e 20 20 50  at each node.  P
2be51 72 65 73 75 6d 61 62 6c 79 2c 0a 2a 2a 20 77 68  resumably,.** wh
2be52 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
2be53 69 73 20 75 73 65 64 20 66 6f 72 20 57 61 6c 6b  is used for Walk
2be54 65 72 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b  er.xExprCallback
2be55 20 74 68 65 6e 20 0a 2a 2a 20 57 61 6c 6b 65 72   then .** Walker
2be56 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  .xSelectCallback
2be57 20 69 73 20 73 65 74 20 74 6f 20 64 6f 20 73 6f   is set to do so
2be58 6d 65 74 68 69 6e 67 20 75 73 65 66 75 6c 20 66  mething useful f
2be59 6f 72 20 65 76 65 72 79 20 0a 2a 2a 20 73 75 62  or every .** sub
2be5a 71 75 65 72 79 20 69 6e 20 74 68 65 20 70 61 72  query in the par
2be5b 73 65 72 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61  ser tree..*/.sta
2be5c 74 69 63 20 69 6e 74 20 65 78 70 72 57 61 6c 6b  tic int exprWalk
2be5d 4e 6f 6f 70 28 57 61 6c 6b 65 72 20 2a 4e 6f 74  Noop(Walker *Not
2be5e 55 73 65 64 2c 20 45 78 70 72 20 2a 4e 6f 74 55  Used, Expr *NotU
2be5f 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f  sed2){.  UNUSED_
2be60 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
2be61 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
2be62 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
2be63 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  inue;.}../*.** T
2be64 68 69 73 20 72 6f 75 74 69 6e 65 20 22 65 78 70  his routine "exp
2be65 61 6e 64 73 22 20 61 20 53 45 4c 45 43 54 20 73  ands" a SELECT s
2be66 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 61 6c 6c  tatement and all
2be67 20 6f 66 20 69 74 73 20 73 75 62 71 75 65 72 69   of its subqueri
2be68 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 64 64 69 74  es..** For addit
2be69 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
2be6a 6e 20 6f 6e 20 77 68 61 74 20 69 74 20 6d 65 61  n on what it mea
2be6b 6e 73 20 74 6f 20 22 65 78 70 61 6e 64 22 20 61  ns to "expand" a
2be6c 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65   SELECT.** state
2be6d 6d 65 6e 74 2c 20 73 65 65 20 74 68 65 20 63 6f  ment, see the co
2be6e 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 65 6c  mment on the sel
2be6f 65 63 74 45 78 70 61 6e 64 20 77 6f 72 6b 65 72  ectExpand worker
2be70 20 63 61 6c 6c 62 61 63 6b 20 61 62 6f 76 65 2e   callback above.
2be71 0a 2a 2a 0a 2a 2a 20 45 78 70 61 6e 64 69 6e 67  .**.** Expanding
2be72 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2be73 65 6e 74 20 69 73 20 74 68 65 20 66 69 72 73 74  ent is the first
2be74 20 73 74 65 70 20 69 6e 20 70 72 6f 63 65 73 73   step in process
2be75 69 6e 67 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20  ing a.** SELECT 
2be76 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20  statement.  The 
2be77 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2be78 20 6d 75 73 74 20 62 65 20 65 78 70 61 6e 64 65   must be expande
2be79 64 20 62 65 66 6f 72 65 0a 2a 2a 20 6e 61 6d 65  d before.** name
2be7a 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 70   resolution is p
2be7b 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20  erformed..**.** 
2be7c 49 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  If anything goes
2be7d 20 77 72 6f 6e 67 2c 20 61 6e 20 65 72 72 6f 72   wrong, an error
2be7e 20 6d 65 73 73 61 67 65 20 69 73 20 77 72 69 74   message is writ
2be7f 74 65 6e 20 69 6e 74 6f 20 70 50 61 72 73 65 2e  ten into pParse.
2be80 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  .** The calling 
2be81 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64 65 74  function can det
2be82 65 63 74 20 74 68 65 20 70 72 6f 62 6c 65 6d 20  ect the problem 
2be83 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 70 50  by looking at pP
2be84 61 72 73 65 2d 3e 6e 45 72 72 0a 2a 2a 20 61 6e  arse->nErr.** an
2be85 64 2f 6f 72 20 70 50 61 72 73 65 2d 3e 64 62 2d  d/or pParse->db-
2be86 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a  >mallocFailed..*
2be87 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
2be88 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e  lite3SelectExpan
2be89 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  d(Parse *pParse,
2be8a 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
2be8b 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20  ){.  Walker w;. 
2be8c 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
2be8d 63 6b 20 3d 20 73 65 6c 65 63 74 45 78 70 61 6e  ck = selectExpan
2be8e 64 65 72 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  der;.  w.xExprCa
2be8f 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72 57 61 6c  llback = exprWal
2be90 6b 4e 6f 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73  kNoop;.  w.pPars
2be91 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71  e = pParse;.  sq
2be92 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
2be93 26 77 2c 20 70 53 65 6c 65 63 74 29 3b 0a 7d 0a  &w, pSelect);.}.
2be94 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2be95 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 2f  _OMIT_SUBQUERY./
2be96 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 57  *.** This is a W
2be97 61 6c 6b 65 72 2e 78 53 65 6c 65 63 74 43 61 6c  alker.xSelectCal
2be98 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 66  lback callback f
2be99 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33 53 65  or the sqlite3Se
2be9a 6c 65 63 74 54 79 70 65 49 6e 66 6f 28 29 0a 2a  lectTypeInfo().*
2be9b 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  * interface..**.
2be9c 2a 2a 20 46 6f 72 20 65 61 63 68 20 46 52 4f 4d  ** For each FROM
2be9d 2d 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79  -clause subquery
2be9e 2c 20 61 64 64 20 43 6f 6c 75 6d 6e 2e 7a 54 79  , add Column.zTy
2be9f 70 65 20 61 6e 64 20 43 6f 6c 75 6d 6e 2e 7a 43  pe and Column.zC
2bea0 6f 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  oll.** informati
2bea1 6f 6e 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20  on to the Table 
2bea2 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 72  structure that r
2bea3 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 72 65  epresents the re
2bea4 73 75 6c 74 20 73 65 74 0a 2a 2a 20 6f 66 20 74  sult set.** of t
2bea5 68 61 74 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a  hat subquery..**
2bea6 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74  .** The Table st
2bea7 72 75 63 74 75 72 65 20 74 68 61 74 20 72 65 70  ructure that rep
2bea8 72 65 73 65 6e 74 73 20 74 68 65 20 72 65 73 75  resents the resu
2bea9 6c 74 20 73 65 74 20 77 61 73 20 63 6f 6e 73 74  lt set was const
2beaa 72 75 63 74 65 64 0a 2a 2a 20 62 79 20 73 65 6c  ructed.** by sel
2beab 65 63 74 45 78 70 61 6e 64 65 72 28 29 20 62 75  ectExpander() bu
2beac 74 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 63  t the type and c
2bead 6f 6c 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61  ollation informa
2beae 74 69 6f 6e 20 77 61 73 20 6f 6d 69 74 74 65 64  tion was omitted
2beaf 0a 2a 2a 20 61 74 20 74 68 61 74 20 70 6f 69 6e  .** at that poin
2beb0 74 20 62 65 63 61 75 73 65 20 69 64 65 6e 74 69  t because identi
2beb1 66 69 65 72 73 20 68 61 64 20 6e 6f 74 20 79 65  fiers had not ye
2beb2 74 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e  t been resolved.
2beb3 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
2beb4 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  e is called afte
2beb5 72 20 69 64 65 6e 74 69 66 69 65 72 20 72 65 73  r identifier res
2beb6 6f 6c 75 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  olution..*/.stat
2beb7 69 63 20 69 6e 74 20 73 65 6c 65 63 74 41 64 64  ic int selectAdd
2beb8 53 75 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f  SubqueryTypeInfo
2beb9 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
2beba 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
2bebb 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 20  Parse *pParse;. 
2bebc 20 69 6e 74 20 69 3b 0a 20 20 53 72 63 4c 69 73   int i;.  SrcLis
2bebd 74 20 2a 70 54 61 62 4c 69 73 74 3b 0a 20 20 73  t *pTabList;.  s
2bebe 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2bebf 65 6d 20 2a 70 46 72 6f 6d 3b 0a 0a 20 20 61 73  em *pFrom;..  as
2bec0 73 65 72 74 28 20 70 2d 3e 73 65 6c 46 6c 61 67  sert( p->selFlag
2bec1 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20  s & SF_Resolved 
2bec2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
2bec3 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 48  >selFlags & SF_H
2bec4 61 73 54 79 70 65 49 6e 66 6f 29 3d 3d 30 20 29  asTypeInfo)==0 )
2bec5 3b 0a 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20  ;.  p->selFlags 
2bec6 7c 3d 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66  |= SF_HasTypeInf
2bec7 6f 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57  o;.  pParse = pW
2bec8 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20  alker->pParse;. 
2bec9 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70   pTabList = p->p
2beca 53 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  Src;.  for(i=0, 
2becb 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e  pFrom=pTabList->
2becc 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  a; i<pTabList->n
2becd 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b  Src; i++, pFrom+
2bece 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  +){.    Table *p
2becf 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61  Tab = pFrom->pTa
2bed0 62 3b 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59  b;.    if( ALWAY
2bed1 53 28 70 54 61 62 21 3d 30 29 20 26 26 20 28 70  S(pTab!=0) && (p
2bed2 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
2bed3 54 46 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d 30  TF_Ephemeral)!=0
2bed4 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 73   ){.      /* A s
2bed5 75 62 2d 71 75 65 72 79 20 69 6e 20 74 68 65 20  ub-query in the 
2bed6 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61  FROM clause of a
2bed7 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20   SELECT */.     
2bed8 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 20 3d 20   Select *pSel = 
2bed9 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a  pFrom->pSelect;.
2beda 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
2bedb 65 6c 20 29 3b 0a 20 20 20 20 20 20 77 68 69 6c  el );.      whil
2bedc 65 28 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 20  e( pSel->pPrior 
2bedd 29 20 70 53 65 6c 20 3d 20 70 53 65 6c 2d 3e 70  ) pSel = pSel->p
2bede 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 73 65 6c  Prior;.      sel
2bedf 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  ectAddColumnType
2bee0 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61  AndCollation(pPa
2bee1 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c  rse, pTab->nCol,
2bee2 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65   pTab->aCol, pSe
2bee3 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  l);.    }.  }.  
2bee4 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
2bee5 6e 75 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  nue;.}.#endif...
2bee6 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2bee7 6e 65 20 61 64 64 73 20 64 61 74 61 74 79 70 65  ne adds datatype
2bee8 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73   and collating s
2bee9 65 71 75 65 6e 63 65 20 69 6e 66 6f 72 6d 61 74  equence informat
2beea 69 6f 6e 20 74 6f 0a 2a 2a 20 74 68 65 20 54 61  ion to.** the Ta
2beeb 62 6c 65 20 73 74 72 75 63 74 75 72 65 73 20 6f  ble structures o
2beec 66 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73  f all FROM-claus
2beed 65 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20  e subqueries in 
2beee 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74  a.** SELECT stat
2beef 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 55 73 65  ement..**.** Use
2bef0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 66   this routine af
2bef1 74 65 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74  ter name resolut
2bef2 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
2bef3 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63  oid sqlite3Selec
2bef4 74 41 64 64 54 79 70 65 49 6e 66 6f 28 50 61 72  tAddTypeInfo(Par
2bef5 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65  se *pParse, Sele
2bef6 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 23 69  ct *pSelect){.#i
2bef7 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2bef8 54 5f 53 55 42 51 55 45 52 59 0a 20 20 57 61 6c  T_SUBQUERY.  Wal
2bef9 6b 65 72 20 77 3b 0a 20 20 77 2e 78 53 65 6c 65  ker w;.  w.xSele
2befa 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c  ctCallback = sel
2befb 65 63 74 41 64 64 53 75 62 71 75 65 72 79 54 79  ectAddSubqueryTy
2befc 70 65 49 6e 66 6f 3b 0a 20 20 77 2e 78 45 78 70  peInfo;.  w.xExp
2befd 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72  rCallback = expr
2befe 57 61 6c 6b 4e 6f 6f 70 3b 0a 20 20 77 2e 70 50  WalkNoop;.  w.pP
2beff 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
2bf00 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
2bf01 63 74 28 26 77 2c 20 70 53 65 6c 65 63 74 29 3b  ct(&w, pSelect);
2bf02 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a  .#endif.}.../*.*
2bf03 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
2bf04 65 74 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20  ets of a SELECT 
2bf05 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 70 72  statement for pr
2bf06 6f 63 65 73 73 69 6e 67 2e 20 20 54 68 65 0a 2a  ocessing.  The.*
2bf07 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61  * following is a
2bf08 63 63 6f 6d 70 6c 69 73 68 65 64 3a 0a 2a 2a 0a  ccomplished:.**.
2bf09 2a 2a 20 20 20 20 20 2a 20 20 56 44 42 45 20 43  **     *  VDBE C
2bf0a 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72  ursor numbers ar
2bf0b 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6c  e assigned to al
2bf0c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20 74 65  l FROM-clause te
2bf0d 72 6d 73 2e 0a 2a 2a 20 20 20 20 20 2a 20 20 45  rms..**     *  E
2bf0e 70 68 65 6d 65 72 61 6c 20 54 61 62 6c 65 20 6f  phemeral Table o
2bf0f 62 6a 65 63 74 73 20 61 72 65 20 63 72 65 61 74  bjects are creat
2bf10 65 64 20 66 6f 72 20 61 6c 6c 20 46 52 4f 4d 2d  ed for all FROM-
2bf11 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 69 65  clause subquerie
2bf12 73 2e 0a 2a 2a 20 20 20 20 20 2a 20 20 4f 4e 20  s..**     *  ON 
2bf13 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65  and USING clause
2bf14 73 20 61 72 65 20 73 68 69 66 74 65 64 20 69 6e  s are shifted in
2bf15 74 6f 20 57 48 45 52 45 20 73 74 61 74 65 6d 65  to WHERE stateme
2bf16 6e 74 73 0a 2a 2a 20 20 20 20 20 2a 20 20 57 69  nts.**     *  Wi
2bf17 6c 64 63 61 72 64 73 20 22 2a 22 20 61 6e 64 20  ldcards "*" and 
2bf18 22 54 41 42 4c 45 2e 2a 22 20 69 6e 20 72 65 73  "TABLE.*" in res
2bf19 75 6c 74 20 73 65 74 73 20 61 72 65 20 65 78 70  ult sets are exp
2bf1a 61 6e 64 65 64 2e 0a 2a 2a 20 20 20 20 20 2a 20  anded..**     * 
2bf1b 20 49 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20   Identifiers in 
2bf1c 65 78 70 72 65 73 73 69 6f 6e 20 61 72 65 20 6d  expression are m
2bf1d 61 74 63 68 65 64 20 74 6f 20 74 61 62 6c 65 73  atched to tables
2bf1e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
2bf1f 74 69 6e 65 20 61 63 74 73 20 72 65 63 75 72 73  tine acts recurs
2bf20 69 76 65 6c 79 20 6f 6e 20 61 6c 6c 20 73 75 62  ively on all sub
2bf21 71 75 65 72 69 65 73 20 77 69 74 68 69 6e 20 74  queries within t
2bf22 68 65 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51  he SELECT..*/.SQ
2bf23 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2bf24 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50  d sqlite3SelectP
2bf25 72 65 70 28 0a 20 20 50 61 72 73 65 20 2a 70 50  rep(.  Parse *pP
2bf26 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  arse,         /*
2bf27 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
2bf28 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
2bf29 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
2bf2a 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
2bf2b 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f  atement being co
2bf2c 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  ded. */.  NameCo
2bf2d 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20  ntext *pOuterNC 
2bf2e 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74   /* Name context
2bf2f 20 66 6f 72 20 63 6f 6e 74 61 69 6e 65 72 20 2a   for container *
2bf30 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
2bf31 64 62 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28  db;.  if( NEVER(
2bf32 70 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a  p==0) ) return;.
2bf33 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
2bf34 62 3b 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46  b;.  if( p->selF
2bf35 6c 61 67 73 20 26 20 53 46 5f 48 61 73 54 79 70  lags & SF_HasTyp
2bf36 65 49 6e 66 6f 20 29 20 72 65 74 75 72 6e 3b 0a  eInfo ) return;.
2bf37 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45    sqlite3SelectE
2bf38 78 70 61 6e 64 28 70 50 61 72 73 65 2c 20 70 29  xpand(pParse, p)
2bf39 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
2bf3a 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
2bf3b 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
2bf3c 6e 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f  n;.  sqlite3Reso
2bf3d 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 70  lveSelectNames(p
2bf3e 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72  Parse, p, pOuter
2bf3f 4e 43 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  NC);.  if( pPars
2bf40 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
2bf41 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
2bf42 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 53  turn;.  sqlite3S
2bf43 65 6c 65 63 74 41 64 64 54 79 70 65 49 6e 66 6f  electAddTypeInfo
2bf44 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 7d 0a 0a  (pParse, p);.}..
2bf45 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20  /*.** Reset the 
2bf46 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75  aggregate accumu
2bf47 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  lator..**.** The
2bf48 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d   aggregate accum
2bf49 75 6c 61 74 6f 72 20 69 73 20 61 20 73 65 74 20  ulator is a set 
2bf4a 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  of memory cells 
2bf4b 74 68 61 74 20 68 6f 6c 64 0a 2a 2a 20 69 6e 74  that hold.** int
2bf4c 65 72 6d 65 64 69 61 74 65 20 72 65 73 75 6c 74  ermediate result
2bf4d 73 20 77 68 69 6c 65 20 63 61 6c 63 75 6c 61 74  s while calculat
2bf4e 69 6e 67 20 61 6e 20 61 67 67 72 65 67 61 74 65  ing an aggregate
2bf4f 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69  .  This.** routi
2bf50 6e 65 20 73 69 6d 70 6c 79 20 73 74 6f 72 65 73  ne simply stores
2bf51 20 4e 55 4c 4c 73 20 69 6e 20 61 6c 6c 20 6f 66   NULLs in all of
2bf52 20 74 68 6f 73 65 20 6d 65 6d 6f 72 79 20 63 65   those memory ce
2bf53 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  lls..*/.static v
2bf54 6f 69 64 20 72 65 73 65 74 41 63 63 75 6d 75 6c  oid resetAccumul
2bf55 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61 72  ator(Parse *pPar
2bf56 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  se, AggInfo *pAg
2bf57 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a  gInfo){.  Vdbe *
2bf58 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2bf59 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
2bf5a 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
2bf5b 63 20 2a 70 46 75 6e 63 3b 0a 20 20 69 66 28 20  c *pFunc;.  if( 
2bf5c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2b  pAggInfo->nFunc+
2bf5d 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d  pAggInfo->nColum
2bf5e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  n==0 ){.    retu
2bf5f 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  rn;.  }.  for(i=
2bf60 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e  0; i<pAggInfo->n
2bf61 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
2bf62 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2bf63 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
2bf64 30 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f  0, pAggInfo->aCo
2bf65 6c 5b 69 5d 2e 69 4d 65 6d 29 3b 0a 20 20 7d 0a  l[i].iMem);.  }.
2bf66 20 20 66 6f 72 28 70 46 75 6e 63 3d 70 41 67 67    for(pFunc=pAgg
2bf67 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c 20 69 3d 30  Info->aFunc, i=0
2bf68 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46  ; i<pAggInfo->nF
2bf69 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 75 6e 63 2b  unc; i++, pFunc+
2bf6a 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  +){.    sqlite3V
2bf6b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2bf6c 4e 75 6c 6c 2c 20 30 2c 20 70 46 75 6e 63 2d 3e  Null, 0, pFunc->
2bf6d 69 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70  iMem);.    if( p
2bf6e 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 3e  Func->iDistinct>
2bf6f 3d 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72  =0 ){.      Expr
2bf70 20 2a 70 45 20 3d 20 70 46 75 6e 63 2d 3e 70 45   *pE = pFunc->pE
2bf71 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  xpr;.      asser
2bf72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
2bf73 72 74 79 28 70 45 2c 20 45 50 5f 78 49 73 53 65  rty(pE, EP_xIsSe
2bf74 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 69  lect) );.      i
2bf75 66 28 20 70 45 2d 3e 78 2e 70 4c 69 73 74 3d 3d  f( pE->x.pList==
2bf76 30 20 7c 7c 20 70 45 2d 3e 78 2e 70 4c 69 73 74  0 || pE->x.pList
2bf77 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20  ->nExpr!=1 ){.  
2bf78 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2bf79 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 44  orMsg(pParse, "D
2bf7a 49 53 54 49 4e 43 54 20 61 67 67 72 65 67 61 74  ISTINCT aggregat
2bf7b 65 73 20 6d 75 73 74 20 68 61 76 65 20 65 78 61  es must have exa
2bf7c 63 74 6c 79 20 6f 6e 65 20 22 0a 20 20 20 20 20  ctly one ".     
2bf7d 20 20 20 20 20 20 22 61 72 67 75 6d 65 6e 74 22        "argument"
2bf7e 29 3b 0a 20 20 20 20 20 20 20 20 70 46 75 6e 63  );.        pFunc
2bf7f 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31  ->iDistinct = -1
2bf80 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2bf81 20 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a         KeyInfo *
2bf82 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e  pKeyInfo = keyIn
2bf83 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70  foFromExprList(p
2bf84 50 61 72 73 65 2c 20 70 45 2d 3e 78 2e 70 4c 69  Parse, pE->x.pLi
2bf85 73 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  st);.        sql
2bf86 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2bf87 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2bf88 61 6c 2c 20 70 46 75 6e 63 2d 3e 69 44 69 73 74  al, pFunc->iDist
2bf89 69 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20  inct, 0, 0,.    
2bf8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf8b 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
2bf8c 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
2bf8d 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20  O_HANDOFF);.    
2bf8e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
2bf8f 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
2bf90 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 69 7a 65  e OP_AggFinalize
2bf91 20 6f 70 63 6f 64 65 20 66 6f 72 20 65 76 65 72   opcode for ever
2bf92 79 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  y aggregate func
2bf93 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 41  tion.** in the A
2bf94 67 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  ggInfo structure
2bf95 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2bf96 20 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63   finalizeAggFunc
2bf97 74 69 6f 6e 73 28 50 61 72 73 65 20 2a 70 50 61  tions(Parse *pPa
2bf98 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41  rse, AggInfo *pA
2bf99 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20  ggInfo){.  Vdbe 
2bf9a 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2bf9b 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  be;.  int i;.  s
2bf9c 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75  truct AggInfo_fu
2bf9d 6e 63 20 2a 70 46 3b 0a 20 20 66 6f 72 28 69 3d  nc *pF;.  for(i=
2bf9e 30 2c 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e  0, pF=pAggInfo->
2bf9f 61 46 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66  aFunc; i<pAggInf
2bfa0 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70  o->nFunc; i++, p
2bfa1 46 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 4c 69  F++){.    ExprLi
2bfa2 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e  st *pList = pF->
2bfa3 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a  pExpr->x.pList;.
2bfa4 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2bfa5 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 46 2d  rHasProperty(pF-
2bfa6 3e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  >pExpr, EP_xIsSe
2bfa7 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 73 71 6c  lect) );.    sql
2bfa8 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2bfa9 2c 20 4f 50 5f 41 67 67 46 69 6e 61 6c 2c 20 70  , OP_AggFinal, p
2bfaa 46 2d 3e 69 4d 65 6d 2c 20 70 4c 69 73 74 20 3f  F->iMem, pList ?
2bfab 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20   pList->nExpr : 
2bfac 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  0, 0,.          
2bfad 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69              (voi
2bfae 64 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34  d*)pF->pFunc, P4
2bfaf 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 7d 0a 7d  _FUNCDEF);.  }.}
2bfb0 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74  ../*.** Update t
2bfb1 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 6d  he accumulator m
2bfb2 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20  emory cells for 
2bfb3 61 6e 20 61 67 67 72 65 67 61 74 65 20 62 61 73  an aggregate bas
2bfb4 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 75 72  ed on.** the cur
2bfb5 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69  rent cursor posi
2bfb6 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
2bfb7 76 6f 69 64 20 75 70 64 61 74 65 41 63 63 75 6d  void updateAccum
2bfb8 75 6c 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50  ulator(Parse *pP
2bfb9 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70  arse, AggInfo *p
2bfba 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65  AggInfo){.  Vdbe
2bfbb 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2bfbc 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  dbe;.  int i;.  
2bfbd 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66  struct AggInfo_f
2bfbe 75 6e 63 20 2a 70 46 3b 0a 20 20 73 74 72 75 63  unc *pF;.  struc
2bfbf 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70  t AggInfo_col *p
2bfc0 43 3b 0a 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e  C;..  pAggInfo->
2bfc1 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a  directMode = 1;.
2bfc2 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2bfc3 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
2bfc4 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70  .  for(i=0, pF=p
2bfc5 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20  AggInfo->aFunc; 
2bfc6 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e  i<pAggInfo->nFun
2bfc7 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20  c; i++, pF++){. 
2bfc8 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20     int nArg;.   
2bfc9 20 69 6e 74 20 61 64 64 72 4e 65 78 74 20 3d 20   int addrNext = 
2bfca 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 41 67  0;.    int regAg
2bfcb 67 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20  g;.    ExprList 
2bfcc 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78  *pList = pF->pEx
2bfcd 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20  pr->x.pList;.   
2bfce 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
2bfcf 73 50 72 6f 70 65 72 74 79 28 70 46 2d 3e 70 45  sProperty(pF->pE
2bfd0 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2bfd1 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  t) );.    if( pL
2bfd2 69 73 74 20 29 7b 0a 20 20 20 20 20 20 6e 41 72  ist ){.      nAr
2bfd3 67 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  g = pList->nExpr
2bfd4 3b 0a 20 20 20 20 20 20 72 65 67 41 67 67 20 3d  ;.      regAgg =
2bfd5 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2bfd6 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 41 72  ange(pParse, nAr
2bfd7 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
2bfd8 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
2bfd9 74 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  t(pParse, pList,
2bfda 20 72 65 67 41 67 67 2c 20 30 29 3b 0a 20 20 20   regAgg, 0);.   
2bfdb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 41   }else{.      nA
2bfdc 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65  rg = 0;.      re
2bfdd 67 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  gAgg = 0;.    }.
2bfde 20 20 20 20 69 66 28 20 70 46 2d 3e 69 44 69 73      if( pF->iDis
2bfdf 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20 20 20 20  tinct>=0 ){.    
2bfe0 20 20 61 64 64 72 4e 65 78 74 20 3d 20 73 71 6c    addrNext = sql
2bfe1 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2bfe2 6c 28 76 29 3b 0a 20 20 20 20 20 20 61 73 73 65  l(v);.      asse
2bfe3 72 74 28 20 6e 41 72 67 3d 3d 31 20 29 3b 0a 20  rt( nArg==1 );. 
2bfe4 20 20 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63       codeDistinc
2bfe5 74 28 70 50 61 72 73 65 2c 20 70 46 2d 3e 69 44  t(pParse, pF->iD
2bfe6 69 73 74 69 6e 63 74 2c 20 61 64 64 72 4e 65 78  istinct, addrNex
2bfe7 74 2c 20 31 2c 20 72 65 67 41 67 67 29 3b 0a 20  t, 1, regAgg);. 
2bfe8 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d     }.    if( pF-
2bfe9 3e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20  >pFunc->flags & 
2bfea 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44  SQLITE_FUNC_NEED
2bfeb 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 43 6f  COLL ){.      Co
2bfec 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30  llSeq *pColl = 0
2bfed 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  ;.      struct E
2bfee 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
2bfef 74 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a  tem;.      int j
2bff0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2bff1 70 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20  pList!=0 );  /* 
2bff2 70 4c 69 73 74 21 3d 30 20 69 66 20 70 46 2d 3e  pList!=0 if pF->
2bff3 70 46 75 6e 63 20 68 61 73 20 4e 45 45 44 43 4f  pFunc has NEEDCO
2bff4 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28  LL */.      for(
2bff5 6a 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74  j=0, pItem=pList
2bff6 2d 3e 61 3b 20 21 70 43 6f 6c 6c 20 26 26 20 6a  ->a; !pColl && j
2bff7 3c 6e 41 72 67 3b 20 6a 2b 2b 2c 20 70 49 74 65  <nArg; j++, pIte
2bff8 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 43  m++){.        pC
2bff9 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
2bffa 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
2bffb 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a   pItem->pExpr);.
2bffc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2bffd 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
2bffe 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72      pColl = pPar
2bfff 73 65 2d 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  se->db->pDfltCol
2c000 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  l;.      }.     
2c001 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c002 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71  p4(v, OP_CollSeq
2c003 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72  , 0, 0, 0, (char
2c004 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c   *)pColl, P4_COL
2c005 4c 53 45 51 29 3b 0a 20 20 20 20 7d 0a 20 20 20  LSEQ);.    }.   
2c006 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c007 70 34 28 76 2c 20 4f 50 5f 41 67 67 53 74 65 70  p4(v, OP_AggStep
2c008 2c 20 30 2c 20 72 65 67 41 67 67 2c 20 70 46 2d  , 0, regAgg, pF-
2c009 3e 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 20  >iMem,.         
2c00a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f               (vo
2c00b 69 64 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50  id*)pF->pFunc, P
2c00c 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20  4_FUNCDEF);.    
2c00d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2c00e 65 50 35 28 76 2c 20 28 75 38 29 6e 41 72 67 29  eP5(v, (u8)nArg)
2c00f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  ;.    sqlite3Rel
2c010 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
2c011 61 72 73 65 2c 20 72 65 67 41 67 67 2c 20 6e 41  arse, regAgg, nA
2c012 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rg);.    sqlite3
2c013 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
2c014 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
2c015 72 65 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20  regAgg, nArg);. 
2c016 20 20 20 69 66 28 20 61 64 64 72 4e 65 78 74 20     if( addrNext 
2c017 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2c018 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2c019 28 76 2c 20 61 64 64 72 4e 65 78 74 29 3b 0a 20  (v, addrNext);. 
2c01a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2c01b 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
2c01c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
2c01d 66 6f 72 28 69 3d 30 2c 20 70 43 3d 70 41 67 67  for(i=0, pC=pAgg
2c01e 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 20 69 3c 70 41  Info->aCol; i<pA
2c01f 67 67 49 6e 66 6f 2d 3e 6e 41 63 63 75 6d 75 6c  ggInfo->nAccumul
2c020 61 74 6f 72 3b 20 69 2b 2b 2c 20 70 43 2b 2b 29  ator; i++, pC++)
2c021 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
2c022 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 43  rCode(pParse, pC
2c023 2d 3e 70 45 78 70 72 2c 20 70 43 2d 3e 69 4d 65  ->pExpr, pC->iMe
2c024 6d 29 3b 0a 20 20 7d 0a 20 20 70 41 67 67 49 6e  m);.  }.  pAggIn
2c025 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d  fo->directMode =
2c026 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70   0;.  sqlite3Exp
2c027 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
2c028 73 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  se);.}../*.** Ge
2c029 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2c02a 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
2c02b 6d 65 6e 74 20 67 69 76 65 6e 20 69 6e 20 74 68  ment given in th
2c02c 65 20 70 20 61 72 67 75 6d 65 6e 74 2e 20 20 0a  e p argument.  .
2c02d 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  **.** The result
2c02e 73 20 61 72 65 20 64 69 73 74 72 69 62 75 74 65  s are distribute
2c02f 64 20 69 6e 20 76 61 72 69 6f 75 73 20 77 61 79  d in various way
2c030 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  s depending on t
2c031 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f  he.** contents o
2c032 66 20 74 68 65 20 53 65 6c 65 63 74 44 65 73 74  f the SelectDest
2c033 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74   structure point
2c034 65 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e  ed to by argumen
2c035 74 20 70 44 65 73 74 0a 2a 2a 20 61 73 20 66 6f  t pDest.** as fo
2c036 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  llows:.**.**    
2c037 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20 20 20   pDest->eDest   
2c038 20 52 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20 2d   Result.**     -
2c039 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d  -----------    -
2c03a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c03b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c03c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20  ----------.**   
2c03d 20 20 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20    SRT_Output    
2c03e 20 20 47 65 6e 65 72 61 74 65 20 61 20 72 6f 77    Generate a row
2c03f 20 6f 66 20 6f 75 74 70 75 74 20 28 75 73 69 6e   of output (usin
2c040 67 20 74 68 65 20 4f 50 5f 52 65 73 75 6c 74 52  g the OP_ResultR
2c041 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ow.**           
2c042 20 20 20 20 20 20 20 20 20 20 6f 70 63 6f 64 65            opcode
2c043 29 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  ) for each row i
2c044 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
2c045 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
2c046 4d 65 6d 20 20 20 20 20 20 20 20 20 4f 6e 6c 79  Mem         Only
2c047 20 76 61 6c 69 64 20 69 66 20 74 68 65 20 72 65   valid if the re
2c048 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67 6c 65  sult is a single
2c049 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 20 20 20 20   column..**     
2c04a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c04b 53 74 6f 72 65 20 74 68 65 20 66 69 72 73 74 20  Store the first 
2c04c 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 69  column of the fi
2c04d 72 73 74 20 72 65 73 75 6c 74 20 72 6f 77 0a 2a  rst result row.*
2c04e 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2c04f 20 20 20 20 20 20 69 6e 20 72 65 67 69 73 74 65        in registe
2c050 72 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 74  r pDest->iParm t
2c051 68 65 6e 20 61 62 61 6e 64 6f 6e 20 74 68 65 20  hen abandon the 
2c052 72 65 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  rest.**         
2c053 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
2c054 68 65 20 71 75 65 72 79 2e 20 20 54 68 69 73 20  he query.  This 
2c055 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 6d 70 6c  destination impl
2c056 69 65 73 20 22 4c 49 4d 49 54 20 31 22 2e 0a 2a  ies "LIMIT 1"..*
2c057 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 53 65 74  *.**     SRT_Set
2c058 20 20 20 20 20 20 20 20 20 54 68 65 20 72 65 73           The res
2c059 75 6c 74 20 6d 75 73 74 20 62 65 20 61 20 73 69  ult must be a si
2c05a 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e 20 20 53 74  ngle column.  St
2c05b 6f 72 65 20 65 61 63 68 0a 2a 2a 20 20 20 20 20  ore each.**     
2c05c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c05d 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 61 73  row of result as
2c05e 20 74 68 65 20 6b 65 79 20 69 6e 20 74 61 62 6c   the key in tabl
2c05f 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 20  e pDest->iParm. 
2c060 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2c061 20 20 20 20 20 20 20 20 41 70 70 6c 79 20 74 68          Apply th
2c062 65 20 61 66 66 69 6e 69 74 79 20 70 44 65 73 74  e affinity pDest
2c063 2d 3e 61 66 66 69 6e 69 74 79 20 62 65 66 6f 72  ->affinity befor
2c064 65 20 73 74 6f 72 69 6e 67 0a 2a 2a 20 20 20 20  e storing.**    
2c065 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c066 20 72 65 73 75 6c 74 73 2e 20 20 55 73 65 64 20   results.  Used 
2c067 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 49 4e  to implement "IN
2c068 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 0a   (SELECT ...)"..
2c069 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 55 6e  **.**     SRT_Un
2c06a 69 6f 6e 20 20 20 20 20 20 20 53 74 6f 72 65 20  ion       Store 
2c06b 72 65 73 75 6c 74 73 20 61 73 20 61 20 6b 65 79  results as a key
2c06c 20 69 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20   in a temporary 
2c06d 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61  table pDest->iPa
2c06e 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  rm..**.**     SR
2c06f 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 52 65  T_Except      Re
2c070 6d 6f 76 65 20 72 65 73 75 6c 74 73 20 66 72 6f  move results fro
2c071 6d 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  m the temporary 
2c072 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61  table pDest->iPa
2c073 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  rm..**.**     SR
2c074 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20 53 74  T_Table       St
2c075 6f 72 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74  ore results in t
2c076 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70  emporary table p
2c077 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 20  Dest->iParm..** 
2c078 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c079 20 20 20 20 54 68 69 73 20 69 73 20 6c 69 6b 65      This is like
2c07a 20 53 52 54 5f 45 70 68 65 6d 54 61 62 20 65 78   SRT_EphemTab ex
2c07b 63 65 70 74 20 74 68 61 74 20 74 68 65 20 74 61  cept that the ta
2c07c 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ble.**          
2c07d 20 20 20 20 20 20 20 20 20 20 20 69 73 20 61 73             is as
2c07e 73 75 6d 65 64 20 74 6f 20 61 6c 72 65 61 64 79  sumed to already
2c07f 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 0a 2a 2a 20   be open..**.** 
2c080 20 20 20 20 53 52 54 5f 45 70 68 65 6d 54 61 62      SRT_EphemTab
2c081 20 20 20 20 43 72 65 61 74 65 20 61 6e 20 74 65      Create an te
2c082 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70 44  mporary table pD
2c083 65 73 74 2d 3e 69 50 61 72 6d 20 61 6e 64 20 73  est->iParm and s
2c084 74 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  tore.**         
2c085 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
2c086 72 65 73 75 6c 74 20 74 68 65 72 65 2e 20 54 68  result there. Th
2c087 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74  e cursor is left
2c088 20 6f 70 65 6e 20 61 66 74 65 72 0a 2a 2a 20 20   open after.**  
2c089 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c08a 20 20 20 72 65 74 75 72 6e 69 6e 67 2e 20 20 54     returning.  T
2c08b 68 69 73 20 69 73 20 6c 69 6b 65 20 53 52 54 5f  his is like SRT_
2c08c 54 61 62 6c 65 20 65 78 63 65 70 74 20 74 68 61  Table except tha
2c08d 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
2c08e 20 20 20 20 20 20 20 20 20 74 68 69 73 20 64 65           this de
2c08f 73 74 69 6e 61 74 69 6f 6e 20 75 73 65 73 20 4f  stination uses O
2c090 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
2c091 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 20 20 20  to create.**    
2c092 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c093 20 74 68 65 20 74 61 62 6c 65 20 66 69 72 73 74   the table first
2c094 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
2c095 43 6f 72 6f 75 74 69 6e 65 20 20 20 47 65 6e 65  Coroutine   Gene
2c096 72 61 74 65 20 61 20 63 6f 2d 72 6f 75 74 69 6e  rate a co-routin
2c097 65 20 74 68 61 74 20 72 65 74 75 72 6e 73 20 61  e that returns a
2c098 20 6e 65 77 20 72 6f 77 20 6f 66 0a 2a 2a 20 20   new row of.**  
2c099 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c09a 20 20 20 72 65 73 75 6c 74 73 20 65 61 63 68 20     results each 
2c09b 74 69 6d 65 20 69 74 20 69 73 20 69 6e 76 6f 6b  time it is invok
2c09c 65 64 2e 20 20 54 68 65 20 65 6e 74 72 79 20 70  ed.  The entry p
2c09d 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  oint.**         
2c09e 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
2c09f 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73  he co-routine is
2c0a0 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
2c0a1 74 65 72 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  ter pDest->iParm
2c0a2 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
2c0a3 45 78 69 73 74 73 20 20 20 20 20 20 53 74 6f 72  Exists      Stor
2c0a4 65 20 61 20 31 20 69 6e 20 6d 65 6d 6f 72 79 20  e a 1 in memory 
2c0a5 63 65 6c 6c 20 70 44 65 73 74 2d 3e 69 50 61 72  cell pDest->iPar
2c0a6 6d 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 0a  m if the result.
2c0a7 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2c0a8 20 20 20 20 20 20 20 73 65 74 20 69 73 20 6e 6f         set is no
2c0a9 74 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 20  t empty..**.**  
2c0aa 20 20 20 53 52 54 5f 44 69 73 63 61 72 64 20 20     SRT_Discard  
2c0ab 20 20 20 54 68 72 6f 77 20 74 68 65 20 72 65 73     Throw the res
2c0ac 75 6c 74 73 20 61 77 61 79 2e 20 20 54 68 69 73  ults away.  This
2c0ad 20 69 73 20 75 73 65 64 20 62 79 20 53 45 4c 45   is used by SELE
2c0ae 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  CT.**           
2c0af 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d            statem
2c0b0 65 6e 74 73 20 77 69 74 68 69 6e 20 74 72 69 67  ents within trig
2c0b1 67 65 72 73 20 77 68 6f 73 65 20 6f 6e 6c 79 20  gers whose only 
2c0b2 70 75 72 70 6f 73 65 20 69 73 0a 2a 2a 20 20 20  purpose is.**   
2c0b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0b4 20 20 74 68 65 20 73 69 64 65 2d 65 66 66 65 63    the side-effec
2c0b5 74 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 2e  ts of functions.
2c0b6 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2c0b7 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
2c0b8 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
2c0b9 2e 20 20 49 66 20 61 6e 79 20 65 72 72 6f 72 73  .  If any errors
2c0ba 20 61 72 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65   are.** encounte
2c0bb 72 65 64 2c 20 74 68 65 6e 20 61 6e 20 61 70 70  red, then an app
2c0bc 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d  ropriate error m
2c0bd 65 73 73 61 67 65 20 69 73 20 6c 65 66 74 20 69  essage is left i
2c0be 6e 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72  n.** pParse->zEr
2c0bf 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rMsg..**.** This
2c0c0 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 4e 4f   routine does NO
2c0c1 54 20 66 72 65 65 20 74 68 65 20 53 65 6c 65 63  T free the Selec
2c0c2 74 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73  t structure pass
2c0c3 65 64 20 69 6e 2e 20 20 54 68 65 0a 2a 2a 20 63  ed in.  The.** c
2c0c4 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
2c0c5 6e 65 65 64 73 20 74 6f 20 64 6f 20 74 68 61 74  needs to do that
2c0c6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2c0c7 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
2c0c8 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a  elect(.  Parse *
2c0c9 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2c0ca 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
2c0cb 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
2c0cc 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
2c0cd 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
2c0ce 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
2c0cf 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 53 65 6c 65  coded. */.  Sele
2c0d0 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 20 20  ctDest *pDest   
2c0d1 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f     /* What to do
2c0d2 20 77 69 74 68 20 74 68 65 20 71 75 65 72 79 20   with the query 
2c0d3 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20  results */.){.  
2c0d4 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
2c0d5 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
2c0d6 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68 65  ounters */.  Whe
2c0d7 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20  reInfo *pWInfo; 
2c0d8 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 66 72      /* Return fr
2c0d9 6f 6d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  om sqlite3WhereB
2c0da 65 67 69 6e 28 29 20 2a 2f 0a 20 20 56 64 62 65  egin() */.  Vdbe
2c0db 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
2c0dc 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
2c0dd 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65 72 20  l machine under 
2c0de 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a  construction */.
2c0df 20 20 69 6e 74 20 69 73 41 67 67 3b 20 20 20 20    int isAgg;    
2c0e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2c0e1 20 66 6f 72 20 73 65 6c 65 63 74 20 6c 69 73 74   for select list
2c0e2 73 20 6c 69 6b 65 20 22 63 6f 75 6e 74 28 2a 29  s like "count(*)
2c0e3 22 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  " */.  ExprList 
2c0e4 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 2f 2a  *pEList;      /*
2c0e5 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   List of columns
2c0e6 20 74 6f 20 65 78 74 72 61 63 74 2e 20 2a 2f 0a   to extract. */.
2c0e7 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
2c0e8 69 73 74 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74  ist;     /* List
2c0e9 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 73 65   of tables to se
2c0ea 6c 65 63 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 45  lect from */.  E
2c0eb 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20  xpr *pWhere;    
2c0ec 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
2c0ed 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  RE clause.  May 
2c0ee 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70  be NULL */.  Exp
2c0ef 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b  rList *pOrderBy;
2c0f0 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
2c0f1 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79   BY clause.  May
2c0f2 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78   be NULL */.  Ex
2c0f3 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79  prList *pGroupBy
2c0f4 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55  ;    /* The GROU
2c0f5 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61  P BY clause.  Ma
2c0f6 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45  y be NULL */.  E
2c0f7 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20  xpr *pHaving;   
2c0f8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56        /* The HAV
2c0f9 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 4d 61 79  ING clause.  May
2c0fa 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e   be NULL */.  in
2c0fb 74 20 69 73 44 69 73 74 69 6e 63 74 3b 20 20 20  t isDistinct;   
2c0fc 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2c0fd 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79  the DISTINCT key
2c0fe 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20  word is present 
2c0ff 2a 2f 0a 20 20 69 6e 74 20 64 69 73 74 69 6e 63  */.  int distinc
2c100 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t;          /* T
2c101 61 62 6c 65 20 74 6f 20 75 73 65 20 66 6f 72 20  able to use for 
2c102 74 68 65 20 64 69 73 74 69 6e 63 74 20 73 65 74  the distinct set
2c103 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 31   */.  int rc = 1
2c104 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2c105 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  Value to return 
2c106 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69  from this functi
2c107 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  on */.  int addr
2c108 53 6f 72 74 49 6e 64 65 78 3b 20 20 20 20 20 2f  SortIndex;     /
2c109 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 6e 20  * Address of an 
2c10a 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
2c10b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a   instruction */.
2c10c 20 20 41 67 67 49 6e 66 6f 20 73 41 67 67 49 6e    AggInfo sAggIn
2c10d 66 6f 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f  fo;      /* Info
2c10e 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20  rmation used by 
2c10f 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65  aggregate querie
2c110 73 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 3b  s */.  int iEnd;
2c111 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c112 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
2c113 65 6e 64 20 6f 66 20 74 68 65 20 71 75 65 72 79  end of the query
2c114 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2c115 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
2c116 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
2c117 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 64 62  nection */..  db
2c118 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2c119 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 64 62 2d   if( p==0 || db-
2c11a 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
2c11b 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b   pParse->nErr ){
2c11c 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
2c11d 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
2c11e 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2c11f 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 2c  , SQLITE_SELECT,
2c120 20 30 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75   0, 0, 0) ) retu
2c121 72 6e 20 31 3b 0a 20 20 6d 65 6d 73 65 74 28 26  rn 1;.  memset(&
2c122 73 41 67 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a  sAggInfo, 0, siz
2c123 65 6f 66 28 73 41 67 67 49 6e 66 6f 29 29 3b 0a  eof(sAggInfo));.
2c124 0a 20 20 69 66 28 20 49 67 6e 6f 72 61 62 6c 65  .  if( Ignorable
2c125 4f 72 64 65 72 62 79 28 70 44 65 73 74 29 20 29  Orderby(pDest) )
2c126 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70 44 65  {.    assert(pDe
2c127 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45  st->eDest==SRT_E
2c128 78 69 73 74 73 20 7c 7c 20 70 44 65 73 74 2d 3e  xists || pDest->
2c129 65 44 65 73 74 3d 3d 53 52 54 5f 55 6e 69 6f 6e  eDest==SRT_Union
2c12a 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20   || .           
2c12b 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52  pDest->eDest==SR
2c12c 54 5f 45 78 63 65 70 74 20 7c 7c 20 70 44 65 73  T_Except || pDes
2c12d 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 44 69  t->eDest==SRT_Di
2c12e 73 63 61 72 64 29 3b 0a 20 20 20 20 2f 2a 20 49  scard);.    /* I
2c12f 66 20 4f 52 44 45 52 20 42 59 20 6d 61 6b 65 73  f ORDER BY makes
2c130 20 6e 6f 20 64 69 66 66 65 72 65 6e 63 65 20 69   no difference i
2c131 6e 20 74 68 65 20 6f 75 74 70 75 74 20 74 68 65  n the output the
2c132 6e 20 6e 65 69 74 68 65 72 20 64 6f 65 73 0a 20  n neither does. 
2c133 20 20 20 2a 2a 20 44 49 53 54 49 4e 43 54 20 73     ** DISTINCT s
2c134 6f 20 69 74 20 63 61 6e 20 62 65 20 72 65 6d 6f  o it can be remo
2c135 76 65 64 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20 20  ved too. */.    
2c136 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2c137 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 72  elete(db, p->pOr
2c138 64 65 72 42 79 29 3b 0a 20 20 20 20 70 2d 3e 70  derBy);.    p->p
2c139 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20  OrderBy = 0;.   
2c13a 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d 20   p->selFlags &= 
2c13b 7e 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20  ~SF_Distinct;.  
2c13c 7d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  }.  sqlite3Selec
2c13d 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c  tPrep(pParse, p,
2c13e 20 30 29 3b 0a 20 20 70 4f 72 64 65 72 42 79 20   0);.  pOrderBy 
2c13f 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20  = p->pOrderBy;. 
2c140 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70   pTabList = p->p
2c141 53 72 63 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20  Src;.  pEList = 
2c142 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28  p->pEList;.  if(
2c143 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
2c144 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2c145 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65  d ){.    goto se
2c146 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20  lect_end;.  }.  
2c147 69 73 41 67 67 20 3d 20 28 70 2d 3e 73 65 6c 46  isAgg = (p->selF
2c148 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 67  lags & SF_Aggreg
2c149 61 74 65 29 21 3d 30 3b 0a 20 20 61 73 73 65 72  ate)!=0;.  asser
2c14a 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
2c14b 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65  .  /* Begin gene
2c14c 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20 20 2a  rating code..  *
2c14d 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
2c14e 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2c14f 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74    if( v==0 ) got
2c150 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20  o select_end;.. 
2c151 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
2c152 65 20 66 6f 72 20 61 6c 6c 20 73 75 62 2d 71 75  e for all sub-qu
2c153 65 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f  eries in the FRO
2c154 4d 20 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 23 69  M clause.  */.#i
2c155 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2c156 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
2c157 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
2c158 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 20  ITE_OMIT_VIEW). 
2c159 20 66 6f 72 28 69 3d 30 3b 20 21 70 2d 3e 70 50   for(i=0; !p->pP
2c15a 72 69 6f 72 20 26 26 20 69 3c 70 54 61 62 4c 69  rior && i<pTabLi
2c15b 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a  st->nSrc; i++){.
2c15c 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
2c15d 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
2c15e 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d   &pTabList->a[i]
2c15f 3b 0a 20 20 20 20 53 65 6c 65 63 74 44 65 73 74  ;.    SelectDest
2c160 20 64 65 73 74 3b 0a 20 20 20 20 53 65 6c 65 63   dest;.    Selec
2c161 74 20 2a 70 53 75 62 20 3d 20 70 49 74 65 6d 2d  t *pSub = pItem-
2c162 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 69 6e  >pSelect;.    in
2c163 74 20 69 73 41 67 67 53 75 62 3b 0a 0a 20 20 20  t isAggSub;..   
2c164 20 69 66 28 20 70 53 75 62 3d 3d 30 20 7c 7c 20   if( pSub==0 || 
2c165 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74  pItem->isPopulat
2c166 65 64 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a  ed ) continue;..
2c167 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74      /* Increment
2c168 20 50 61 72 73 65 2e 6e 48 65 69 67 68 74 20 62   Parse.nHeight b
2c169 79 20 74 68 65 20 68 65 69 67 68 74 20 6f 66 20  y the height of 
2c16a 74 68 65 20 6c 61 72 67 65 73 74 20 65 78 70 72  the largest expr
2c16b 65 73 73 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 72  ession.    ** tr
2c16c 65 65 20 72 65 66 65 72 65 64 20 74 6f 20 62 79  ee refered to by
2c16d 20 74 68 69 73 2c 20 74 68 65 20 70 61 72 65 6e   this, the paren
2c16e 74 20 73 65 6c 65 63 74 2e 20 54 68 65 20 63 68  t select. The ch
2c16f 69 6c 64 20 73 65 6c 65 63 74 0a 20 20 20 20 2a  ild select.    *
2c170 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 65 78  * may contain ex
2c171 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73 20 6f  pression trees o
2c172 66 20 61 74 20 6d 6f 73 74 0a 20 20 20 20 2a 2a  f at most.    **
2c173 20 28 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50   (SQLITE_MAX_EXP
2c174 52 5f 44 45 50 54 48 2d 50 61 72 73 65 2e 6e 48  R_DEPTH-Parse.nH
2c175 65 69 67 68 74 29 20 68 65 69 67 68 74 2e 20 54  eight) height. T
2c176 68 69 73 20 69 73 20 61 20 62 69 74 0a 20 20 20  his is a bit.   
2c177 20 2a 2a 20 6d 6f 72 65 20 63 6f 6e 73 65 72 76   ** more conserv
2c178 61 74 69 76 65 20 74 68 61 6e 20 6e 65 63 65 73  ative than neces
2c179 73 61 72 79 2c 20 62 75 74 20 6d 75 63 68 20 65  sary, but much e
2c17a 61 73 69 65 72 20 74 68 61 6e 20 65 6e 66 6f 72  asier than enfor
2c17b 63 69 6e 67 0a 20 20 20 20 2a 2a 20 61 6e 20 65  cing.    ** an e
2c17c 78 61 63 74 20 6c 69 6d 69 74 2e 0a 20 20 20 20  xact limit..    
2c17d 2a 2f 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  */.    pParse->n
2c17e 48 65 69 67 68 74 20 2b 3d 20 73 71 6c 69 74 65  Height += sqlite
2c17f 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68  3SelectExprHeigh
2c180 74 28 70 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68  t(p);..    /* Ch
2c181 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
2c182 65 20 73 75 62 71 75 65 72 79 20 63 61 6e 20 62  e subquery can b
2c183 65 20 61 62 73 6f 72 62 65 64 20 69 6e 74 6f 20  e absorbed into 
2c184 74 68 65 20 70 61 72 65 6e 74 2e 20 2a 2f 0a 20  the parent. */. 
2c185 20 20 20 69 73 41 67 67 53 75 62 20 3d 20 28 70     isAggSub = (p
2c186 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  Sub->selFlags & 
2c187 53 46 5f 41 67 67 72 65 67 61 74 65 29 21 3d 30  SF_Aggregate)!=0
2c188 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 74 74 65  ;.    if( flatte
2c189 6e 53 75 62 71 75 65 72 79 28 70 50 61 72 73 65  nSubquery(pParse
2c18a 2c 20 70 2c 20 69 2c 20 69 73 41 67 67 2c 20 69  , p, i, isAgg, i
2c18b 73 41 67 67 53 75 62 29 20 29 7b 0a 20 20 20 20  sAggSub) ){.    
2c18c 20 20 69 66 28 20 69 73 41 67 67 53 75 62 20 29    if( isAggSub )
2c18d 7b 0a 20 20 20 20 20 20 20 20 69 73 41 67 67 20  {.        isAgg 
2c18e 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  = 1;.        p->
2c18f 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41  selFlags |= SF_A
2c190 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20 20 20  ggregate;.      
2c191 7d 0a 20 20 20 20 20 20 69 20 3d 20 2d 31 3b 0a  }.      i = -1;.
2c192 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2c193 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2c194 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52  stInit(&dest, SR
2c195 54 5f 45 70 68 65 6d 54 61 62 2c 20 70 49 74 65  T_EphemTab, pIte
2c196 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20  m->iCursor);.   
2c197 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d     assert( pItem
2c198 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3d 3d 30  ->isPopulated==0
2c199 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
2c19a 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
2c19b 70 53 75 62 2c 20 26 64 65 73 74 29 3b 0a 20 20  pSub, &dest);.  
2c19c 20 20 20 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70      pItem->isPop
2c19d 75 6c 61 74 65 64 20 3d 20 31 3b 0a 20 20 20 20  ulated = 1;.    
2c19e 7d 0a 20 20 20 20 69 66 28 20 2f 2a 70 50 61 72  }.    if( /*pPar
2c19f 73 65 2d 3e 6e 45 72 72 20 7c 7c 2a 2f 20 64 62  se->nErr ||*/ db
2c1a0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2c1a1 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c  {.      goto sel
2c1a2 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20  ect_end;.    }. 
2c1a3 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67     pParse->nHeig
2c1a4 68 74 20 2d 3d 20 73 71 6c 69 74 65 33 53 65 6c  ht -= sqlite3Sel
2c1a5 65 63 74 45 78 70 72 48 65 69 67 68 74 28 70 29  ectExprHeight(p)
2c1a6 3b 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d  ;.    pTabList =
2c1a7 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 69 66   p->pSrc;.    if
2c1a8 28 20 21 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65  ( !IgnorableOrde
2c1a9 72 62 79 28 70 44 65 73 74 29 20 29 7b 0a 20 20  rby(pDest) ){.  
2c1aa 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70      pOrderBy = p
2c1ab 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20  ->pOrderBy;.    
2c1ac 7d 0a 20 20 7d 0a 20 20 70 45 4c 69 73 74 20 3d  }.  }.  pEList =
2c1ad 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 23 65 6e 64   p->pEList;.#end
2c1ae 69 66 0a 20 20 70 57 68 65 72 65 20 3d 20 70 2d  if.  pWhere = p-
2c1af 3e 70 57 68 65 72 65 3b 0a 20 20 70 47 72 6f 75  >pWhere;.  pGrou
2c1b0 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42  pBy = p->pGroupB
2c1b1 79 3b 0a 20 20 70 48 61 76 69 6e 67 20 3d 20 70  y;.  pHaving = p
2c1b2 2d 3e 70 48 61 76 69 6e 67 3b 0a 20 20 69 73 44  ->pHaving;.  isD
2c1b3 69 73 74 69 6e 63 74 20 3d 20 28 70 2d 3e 73 65  istinct = (p->se
2c1b4 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74  lFlags & SF_Dist
2c1b5 69 6e 63 74 29 21 3d 30 3b 0a 0a 23 69 66 6e 64  inct)!=0;..#ifnd
2c1b6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
2c1b7 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 20  OMPOUND_SELECT. 
2c1b8 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
2c1b9 61 72 65 20 61 20 73 65 71 75 65 6e 63 65 20 6f  are a sequence o
2c1ba 66 20 71 75 65 72 69 65 73 2c 20 64 6f 20 74 68  f queries, do th
2c1bb 65 20 65 61 72 6c 69 65 72 20 6f 6e 65 73 20 66  e earlier ones f
2c1bc 69 72 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  irst..  */.  if(
2c1bd 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20   p->pPrior ){.  
2c1be 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 6d    if( p->pRightm
2c1bf 6f 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ost==0 ){.      
2c1c0 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 2c 20 2a  Select *pLoop, *
2c1c1 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20  pRight = 0;.    
2c1c2 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
2c1c3 20 20 20 20 20 69 6e 74 20 6d 78 53 65 6c 65 63       int mxSelec
2c1c4 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 4c 6f  t;.      for(pLo
2c1c5 6f 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f  op=p; pLoop; pLo
2c1c6 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72  op=pLoop->pPrior
2c1c7 2c 20 63 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20  , cnt++){.      
2c1c8 20 20 70 4c 6f 6f 70 2d 3e 70 52 69 67 68 74 6d    pLoop->pRightm
2c1c9 6f 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20  ost = p;.       
2c1ca 20 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 20 3d 20   pLoop->pNext = 
2c1cb 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
2c1cc 70 52 69 67 68 74 20 3d 20 70 4c 6f 6f 70 3b 0a  pRight = pLoop;.
2c1cd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 78        }.      mx
2c1ce 53 65 6c 65 63 74 20 3d 20 64 62 2d 3e 61 4c 69  Select = db->aLi
2c1cf 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
2c1d0 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
2c1d1 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 78 53  ];.      if( mxS
2c1d2 65 6c 65 63 74 20 26 26 20 63 6e 74 3e 6d 78 53  elect && cnt>mxS
2c1d3 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
2c1d4 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2c1d5 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
2c1d6 6e 79 20 74 65 72 6d 73 20 69 6e 20 63 6f 6d 70  ny terms in comp
2c1d7 6f 75 6e 64 20 53 45 4c 45 43 54 22 29 3b 0a 20  ound SELECT");. 
2c1d8 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
2c1d9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2c1da 20 20 20 72 65 74 75 72 6e 20 6d 75 6c 74 69 53     return multiS
2c1db 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c  elect(pParse, p,
2c1dc 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 23 65 6e   pDest);.  }.#en
2c1dd 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 72 69  dif..  /* If wri
2c1de 74 69 6e 67 20 74 6f 20 6d 65 6d 6f 72 79 20 6f  ting to memory o
2c1df 72 20 67 65 6e 65 72 61 74 69 6e 67 20 61 20 73  r generating a s
2c1e0 65 74 0a 20 20 2a 2a 20 6f 6e 6c 79 20 61 20 73  et.  ** only a s
2c1e1 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6d 61 79  ingle column may
2c1e2 20 62 65 20 6f 75 74 70 75 74 2e 0a 20 20 2a 2f   be output..  */
2c1e3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2c1e4 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20  OMIT_SUBQUERY.  
2c1e5 69 66 28 20 63 68 65 63 6b 46 6f 72 4d 75 6c 74  if( checkForMult
2c1e6 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72 72  iColumnSelectErr
2c1e7 6f 72 28 70 50 61 72 73 65 2c 20 70 44 65 73 74  or(pParse, pDest
2c1e8 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
2c1e9 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c   ){.    goto sel
2c1ea 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65 6e  ect_end;.  }.#en
2c1eb 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 6f 73  dif..  /* If pos
2c1ec 73 69 62 6c 65 2c 20 72 65 77 72 69 74 65 20 74  sible, rewrite t
2c1ed 68 65 20 71 75 65 72 79 20 74 6f 20 75 73 65 20  he query to use 
2c1ee 47 52 4f 55 50 20 42 59 20 69 6e 73 74 65 61 64  GROUP BY instead
2c1ef 20 6f 66 20 44 49 53 54 49 4e 43 54 2e 0a 20 20   of DISTINCT..  
2c1f0 2a 2a 20 47 52 4f 55 50 20 42 59 20 6d 69 67 68  ** GROUP BY migh
2c1f1 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2c 20  t use an index, 
2c1f2 44 49 53 54 49 4e 43 54 20 6e 65 76 65 72 20 64  DISTINCT never d
2c1f3 6f 65 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  oes..  */.  asse
2c1f4 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 3d  rt( p->pGroupBy=
2c1f5 3d 30 20 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61  =0 || (p->selFla
2c1f6 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74  gs & SF_Aggregat
2c1f7 65 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28  e)!=0 );.  if( (
2c1f8 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53  p->selFlags & (S
2c1f9 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67  F_Distinct|SF_Ag
2c1fa 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69  gregate))==SF_Di
2c1fb 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 70 2d  stinct ){.    p-
2c1fc 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69  >pGroupBy = sqli
2c1fd 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
2c1fe 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 30 29  b, p->pEList, 0)
2c1ff 3b 0a 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d  ;.    pGroupBy =
2c200 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20   p->pGroupBy;.  
2c201 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d    p->selFlags &=
2c202 20 7e 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20   ~SF_Distinct;. 
2c203 20 20 20 69 73 44 69 73 74 69 6e 63 74 20 3d 20     isDistinct = 
2c204 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  0;.  }..  /* If 
2c205 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45  there is an ORDE
2c206 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65  R BY clause, the
2c207 6e 20 74 68 69 73 20 73 6f 72 74 69 6e 67 0a 20  n this sorting. 
2c208 20 2a 2a 20 69 6e 64 65 78 20 6d 69 67 68 74 20   ** index might 
2c209 65 6e 64 20 75 70 20 62 65 69 6e 67 20 75 6e 75  end up being unu
2c20a 73 65 64 20 69 66 20 74 68 65 20 64 61 74 61 20  sed if the data 
2c20b 63 61 6e 20 62 65 20 0a 20 20 2a 2a 20 65 78 74  can be .  ** ext
2c20c 72 61 63 74 65 64 20 69 6e 20 70 72 65 2d 73 6f  racted in pre-so
2c20d 72 74 65 64 20 6f 72 64 65 72 2e 20 20 49 66 20  rted order.  If 
2c20e 74 68 61 74 20 69 73 20 74 68 65 20 63 61 73 65  that is the case
2c20f 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20  , then the.  ** 
2c210 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
2c211 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 69 6c   instruction wil
2c212 6c 20 62 65 20 63 68 61 6e 67 65 64 20 74 6f 20  l be changed to 
2c213 61 6e 20 4f 50 5f 4e 6f 6f 70 20 6f 6e 63 65 0a  an OP_Noop once.
2c214 20 20 2a 2a 20 77 65 20 66 69 67 75 72 65 20 6f    ** we figure o
2c215 75 74 20 74 68 61 74 20 74 68 65 20 73 6f 72 74  ut that the sort
2c216 69 6e 67 20 69 6e 64 65 78 20 69 73 20 6e 6f 74  ing index is not
2c217 20 6e 65 65 64 65 64 2e 20 20 54 68 65 20 61 64   needed.  The ad
2c218 64 72 53 6f 72 74 49 6e 64 65 78 0a 20 20 2a 2a  drSortIndex.  **
2c219 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65   variable is use
2c21a 64 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20  d to facilitate 
2c21b 74 68 61 74 20 63 68 61 6e 67 65 2e 0a 20 20 2a  that change..  *
2c21c 2f 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  /.  if( pOrderBy
2c21d 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20   ){.    KeyInfo 
2c21e 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 70  *pKeyInfo;.    p
2c21f 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66  KeyInfo = keyInf
2c220 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50  oFromExprList(pP
2c221 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 29 3b  arse, pOrderBy);
2c222 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69  .    pOrderBy->i
2c223 45 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65  ECursor = pParse
2c224 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 70 2d  ->nTab++;.    p-
2c225 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d  >addrOpenEphm[2]
2c226 20 3d 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78   = addrSortIndex
2c227 20 3d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   =.      sqlite3
2c228 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2c229 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 0a  _OpenEphemeral,.
2c22a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c22b 20 20 20 20 20 20 20 20 20 20 20 70 4f 72 64 65             pOrde
2c22c 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20 70  rBy->iECursor, p
2c22d 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 2b 32  OrderBy->nExpr+2
2c22e 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
2c22f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c230 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
2c231 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
2c232 4f 46 46 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  OFF);.  }else{. 
2c233 20 20 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78     addrSortIndex
2c234 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a   = -1;.  }..  /*
2c235 20 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 69   If the output i
2c236 73 20 64 65 73 74 69 6e 65 64 20 66 6f 72 20 61  s destined for a
2c237 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2c238 2c 20 6f 70 65 6e 20 74 68 61 74 20 74 61 62 6c  , open that tabl
2c239 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44  e..  */.  if( pD
2c23a 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2c23b 45 70 68 65 6d 54 61 62 20 29 7b 0a 20 20 20 20  EphemTab ){.    
2c23c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c23d 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  2(v, OP_OpenEphe
2c23e 6d 65 72 61 6c 2c 20 70 44 65 73 74 2d 3e 69 50  meral, pDest->iP
2c23f 61 72 6d 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78  arm, pEList->nEx
2c240 70 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  pr);.  }..  /* S
2c241 65 74 20 74 68 65 20 6c 69 6d 69 74 65 72 2e 0a  et the limiter..
2c242 20 20 2a 2f 0a 20 20 69 45 6e 64 20 3d 20 73 71    */.  iEnd = sq
2c243 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
2c244 65 6c 28 76 29 3b 0a 20 20 63 6f 6d 70 75 74 65  el(v);.  compute
2c245 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
2c246 50 61 72 73 65 2c 20 70 2c 20 69 45 6e 64 29 3b  Parse, p, iEnd);
2c247 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61 20 76 69  ..  /* Open a vi
2c248 72 74 75 61 6c 20 69 6e 64 65 78 20 74 6f 20 75  rtual index to u
2c249 73 65 20 66 6f 72 20 74 68 65 20 64 69 73 74 69  se for the disti
2c24a 6e 63 74 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20  nct set..  */.  
2c24b 69 66 28 20 69 73 44 69 73 74 69 6e 63 74 20 29  if( isDistinct )
2c24c 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70  {.    KeyInfo *p
2c24d 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 61 73 73  KeyInfo;.    ass
2c24e 65 72 74 28 20 69 73 41 67 67 20 7c 7c 20 70 47  ert( isAgg || pG
2c24f 72 6f 75 70 42 79 20 29 3b 0a 20 20 20 20 64 69  roupBy );.    di
2c250 73 74 69 6e 63 74 20 3d 20 70 50 61 72 73 65 2d  stinct = pParse-
2c251 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 70 4b 65  >nTab++;.    pKe
2c252 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46  yInfo = keyInfoF
2c253 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72  romExprList(pPar
2c254 73 65 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a  se, p->pEList);.
2c255 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c256 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
2c257 45 70 68 65 6d 65 72 61 6c 2c 20 64 69 73 74 69  Ephemeral, disti
2c258 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20  nct, 0, 0,.     
2c259 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c25a 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e     (char*)pKeyIn
2c25b 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  fo, P4_KEYINFO_H
2c25c 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 65 6c 73 65  ANDOFF);.  }else
2c25d 7b 0a 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d  {.    distinct =
2c25e 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41   -1;.  }..  /* A
2c25f 67 67 72 65 67 61 74 65 20 61 6e 64 20 6e 6f 6e  ggregate and non
2c260 2d 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69  -aggregate queri
2c261 65 73 20 61 72 65 20 68 61 6e 64 6c 65 64 20 64  es are handled d
2c262 69 66 66 65 72 65 6e 74 6c 79 20 2a 2f 0a 20 20  ifferently */.  
2c263 69 66 28 20 21 69 73 41 67 67 20 26 26 20 70 47  if( !isAgg && pG
2c264 72 6f 75 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20  roupBy==0 ){.   
2c265 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 69 73   /* This case is
2c266 20 66 6f 72 20 6e 6f 6e 2d 61 67 67 72 65 67 61   for non-aggrega
2c267 74 65 20 71 75 65 72 69 65 73 0a 20 20 20 20 2a  te queries.    *
2c268 2a 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61  * Begin the data
2c269 62 61 73 65 20 73 63 61 6e 0a 20 20 20 20 2a 2f  base scan.    */
2c26a 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71  .    pWInfo = sq
2c26b 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
2c26c 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
2c26d 2c 20 70 57 68 65 72 65 2c 20 26 70 4f 72 64 65  , pWhere, &pOrde
2c26e 72 42 79 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  rBy, 0);.    if(
2c26f 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74   pWInfo==0 ) got
2c270 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20  o select_end;.. 
2c271 20 20 20 2f 2a 20 49 66 20 73 6f 72 74 69 6e 67     /* If sorting
2c272 20 69 6e 64 65 78 20 74 68 61 74 20 77 61 73 20   index that was 
2c273 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 69  created by a pri
2c274 6f 72 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  or OP_OpenEpheme
2c275 72 61 6c 20 0a 20 20 20 20 2a 2a 20 69 6e 73 74  ral .    ** inst
2c276 72 75 63 74 69 6f 6e 20 65 6e 64 65 64 20 75 70  ruction ended up
2c277 20 6e 6f 74 20 62 65 69 6e 67 20 6e 65 65 64 65   not being neede
2c278 64 2c 20 74 68 65 6e 20 63 68 61 6e 67 65 20 74  d, then change t
2c279 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  he OP_OpenEpheme
2c27a 72 61 6c 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20  ral.    ** into 
2c27b 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 0a 20 20 20 20  an OP_Noop..    
2c27c 2a 2f 0a 20 20 20 20 69 66 28 20 61 64 64 72 53  */.    if( addrS
2c27d 6f 72 74 49 6e 64 65 78 3e 3d 30 20 26 26 20 70  ortIndex>=0 && p
2c27e 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20  OrderBy==0 ){.  
2c27f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2c280 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61  hangeToNoop(v, a
2c281 64 64 72 53 6f 72 74 49 6e 64 65 78 2c 20 31 29  ddrSortIndex, 1)
2c282 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f  ;.      p->addrO
2c283 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b  penEphm[2] = -1;
2c284 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 55  .    }..    /* U
2c285 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  se the standard 
2c286 69 6e 6e 65 72 20 6c 6f 6f 70 0a 20 20 20 20 2a  inner loop.    *
2c287 2f 0a 20 20 20 20 61 73 73 65 72 74 28 21 69 73  /.    assert(!is
2c288 44 69 73 74 69 6e 63 74 29 3b 0a 20 20 20 20 73  Distinct);.    s
2c289 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70  electInnerLoop(p
2c28a 50 61 72 73 65 2c 20 70 2c 20 70 45 4c 69 73 74  Parse, p, pEList
2c28b 2c 20 30 2c 20 30 2c 20 70 4f 72 64 65 72 42 79  , 0, 0, pOrderBy
2c28c 2c 20 2d 31 2c 20 70 44 65 73 74 2c 0a 20 20 20  , -1, pDest,.   
2c28d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c28e 20 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e   pWInfo->iContin
2c28f 75 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65  ue, pWInfo->iBre
2c290 61 6b 29 3b 0a 0a 20 20 20 20 2f 2a 20 45 6e 64  ak);..    /* End
2c291 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
2c292 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a  an loop..    */.
2c293 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65      sqlite3Where
2c294 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 7d  End(pWInfo);.  }
2c295 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69  else{.    /* Thi
2c296 73 20 69 73 20 74 68 65 20 70 72 6f 63 65 73 73  s is the process
2c297 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74  ing for aggregat
2c298 65 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 20  e queries */.   
2c299 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43   NameContext sNC
2c29a 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e  ;    /* Name con
2c29b 74 65 78 74 20 66 6f 72 20 70 72 6f 63 65 73 73  text for process
2c29c 69 6e 67 20 61 67 67 72 65 67 61 74 65 20 69 6e  ing aggregate in
2c29d 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  formation */.   
2c29e 20 69 6e 74 20 69 41 4d 65 6d 3b 20 20 20 20 20   int iAMem;     
2c29f 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d 65       /* First Me
2c2a0 6d 20 61 64 64 72 65 73 73 20 66 6f 72 20 73 74  m address for st
2c2a1 6f 72 69 6e 67 20 63 75 72 72 65 6e 74 20 47 52  oring current GR
2c2a2 4f 55 50 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e  OUP BY */.    in
2c2a3 74 20 69 42 4d 65 6d 3b 20 20 20 20 20 20 20 20  t iBMem;        
2c2a4 20 20 2f 2a 20 46 69 72 73 74 20 4d 65 6d 20 61    /* First Mem a
2c2a5 64 64 72 65 73 73 20 66 6f 72 20 70 72 65 76 69  ddress for previ
2c2a6 6f 75 73 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a  ous GROUP BY */.
2c2a7 20 20 20 20 69 6e 74 20 69 55 73 65 46 6c 61 67      int iUseFlag
2c2a8 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61  ;       /* Mem a
2c2a9 64 64 72 65 73 73 20 68 6f 6c 64 69 6e 67 20 66  ddress holding f
2c2aa 6c 61 67 20 69 6e 64 69 63 61 74 69 6e 67 20 74  lag indicating t
2c2ab 68 61 74 20 61 74 20 6c 65 61 73 74 0a 20 20 20  hat at least.   
2c2ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2ad 20 20 20 20 20 2a 2a 20 6f 6e 65 20 72 6f 77 20       ** one row 
2c2ae 6f 66 20 74 68 65 20 69 6e 70 75 74 20 74 6f 20  of the input to 
2c2af 74 68 65 20 61 67 67 72 65 67 61 74 6f 72 20 68  the aggregator h
2c2b0 61 73 20 62 65 65 6e 0a 20 20 20 20 20 20 20 20  as been.        
2c2b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2b2 2a 2a 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a  ** processed */.
2c2b3 20 20 20 20 69 6e 74 20 69 41 62 6f 72 74 46 6c      int iAbortFl
2c2b4 61 67 3b 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61  ag;     /* Mem a
2c2b5 64 64 72 65 73 73 20 77 68 69 63 68 20 63 61 75  ddress which cau
2c2b6 73 65 73 20 71 75 65 72 79 20 61 62 6f 72 74 20  ses query abort 
2c2b7 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20  if positive */. 
2c2b8 20 20 20 69 6e 74 20 67 72 6f 75 70 42 79 53 6f     int groupBySo
2c2b9 72 74 3b 20 20 20 20 2f 2a 20 52 6f 77 73 20 63  rt;    /* Rows c
2c2ba 6f 6d 65 20 66 72 6f 6d 20 73 6f 75 72 63 65 20  ome from source 
2c2bb 69 6e 20 47 52 4f 55 50 20 42 59 20 6f 72 64 65  in GROUP BY orde
2c2bc 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64 64  r */.    int add
2c2bd 72 45 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  rEnd;        /* 
2c2be 45 6e 64 20 6f 66 20 70 72 6f 63 65 73 73 69 6e  End of processin
2c2bf 67 20 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43  g for this SELEC
2c2c0 54 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 65 6d  T */..    /* Rem
2c2c1 6f 76 65 20 61 6e 79 20 61 6e 64 20 61 6c 6c 20  ove any and all 
2c2c2 61 6c 69 61 73 65 73 20 62 65 74 77 65 65 6e 20  aliases between 
2c2c3 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 61  the result set a
2c2c4 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 47 52  nd the.    ** GR
2c2c5 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 20  OUP BY clause.. 
2c2c6 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 47     */.    if( pG
2c2c7 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20  roupBy ){.      
2c2c8 69 6e 74 20 6b 3b 20 20 20 20 20 20 20 20 20 20  int k;          
2c2c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c2ca 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2c2cb 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78  .      struct Ex
2c2cc 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
2c2cd 65 6d 3b 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70  em;  /* For loop
2c2ce 69 6e 67 20 6f 76 65 72 20 65 78 70 72 65 73 73  ing over express
2c2cf 69 6f 6e 20 69 6e 20 61 20 6c 69 73 74 20 2a 2f  ion in a list */
2c2d0 0a 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 70 2d  ..      for(k=p-
2c2d1 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20  >pEList->nExpr, 
2c2d2 70 49 74 65 6d 3d 70 2d 3e 70 45 4c 69 73 74 2d  pItem=p->pEList-
2c2d3 3e 61 3b 20 6b 3e 30 3b 20 6b 2d 2d 2c 20 70 49  >a; k>0; k--, pI
2c2d4 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  tem++){.        
2c2d5 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20  pItem->iAlias = 
2c2d6 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
2c2d7 20 66 6f 72 28 6b 3d 70 47 72 6f 75 70 42 79 2d   for(k=pGroupBy-
2c2d8 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 47  >nExpr, pItem=pG
2c2d9 72 6f 75 70 42 79 2d 3e 61 3b 20 6b 3e 30 3b 20  roupBy->a; k>0; 
2c2da 6b 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  k--, pItem++){. 
2c2db 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 41         pItem->iA
2c2dc 6c 69 61 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  lias = 0;.      
2c2dd 7d 0a 20 20 20 20 7d 0a 0a 20 0a 20 20 20 20 2f  }.    }.. .    /
2c2de 2a 20 43 72 65 61 74 65 20 61 20 6c 61 62 65 6c  * Create a label
2c2df 20 74 6f 20 6a 75 6d 70 20 74 6f 20 77 68 65 6e   to jump to when
2c2e0 20 77 65 20 77 61 6e 74 20 74 6f 20 61 62 6f 72   we want to abor
2c2e1 74 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20  t the query */. 
2c2e2 20 20 20 61 64 64 72 45 6e 64 20 3d 20 73 71 6c     addrEnd = sql
2c2e3 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2c2e4 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f  l(v);..    /* Co
2c2e5 6e 76 65 72 74 20 54 4b 5f 43 4f 4c 55 4d 4e 20  nvert TK_COLUMN 
2c2e6 6e 6f 64 65 73 20 69 6e 74 6f 20 54 4b 5f 41 47  nodes into TK_AG
2c2e7 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 6d 61 6b  G_COLUMN and mak
2c2e8 65 20 65 6e 74 72 69 65 73 20 69 6e 0a 20 20 20  e entries in.   
2c2e9 20 2a 2a 20 73 41 67 67 49 6e 66 6f 20 66 6f 72   ** sAggInfo for
2c2ea 20 61 6c 6c 20 54 4b 5f 41 47 47 5f 46 55 4e 43   all TK_AGG_FUNC
2c2eb 54 49 4f 4e 20 6e 6f 64 65 73 20 69 6e 20 65 78  TION nodes in ex
2c2ec 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65  pressions of the
2c2ed 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 73  .    ** SELECT s
2c2ee 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f  tatement..    */
2c2ef 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43  .    memset(&sNC
2c2f0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29  , 0, sizeof(sNC)
2c2f1 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73  );.    sNC.pPars
2c2f2 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20  e = pParse;.    
2c2f3 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
2c2f4 54 61 62 4c 69 73 74 3b 0a 20 20 20 20 73 4e 43  TabList;.    sNC
2c2f5 2e 70 41 67 67 49 6e 66 6f 20 3d 20 26 73 41 67  .pAggInfo = &sAg
2c2f6 67 49 6e 66 6f 3b 0a 20 20 20 20 73 41 67 67 49  gInfo;.    sAggI
2c2f7 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75  nfo.nSortingColu
2c2f8 6d 6e 20 3d 20 70 47 72 6f 75 70 42 79 20 3f 20  mn = pGroupBy ? 
2c2f9 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2b  pGroupBy->nExpr+
2c2fa 31 20 3a 20 30 3b 0a 20 20 20 20 73 41 67 67 49  1 : 0;.    sAggI
2c2fb 6e 66 6f 2e 70 47 72 6f 75 70 42 79 20 3d 20 70  nfo.pGroupBy = p
2c2fc 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 73 71 6c  GroupBy;.    sql
2c2fd 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
2c2fe 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 70 45 4c  ggList(&sNC, pEL
2c2ff 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ist);.    sqlite
2c300 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c  3ExprAnalyzeAggL
2c301 69 73 74 28 26 73 4e 43 2c 20 70 4f 72 64 65 72  ist(&sNC, pOrder
2c302 42 79 29 3b 0a 20 20 20 20 69 66 28 20 70 48 61  By);.    if( pHa
2c303 76 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 73 71  ving ){.      sq
2c304 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
2c305 41 67 67 72 65 67 61 74 65 73 28 26 73 4e 43 2c  Aggregates(&sNC,
2c306 20 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 7d   pHaving);.    }
2c307 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e 41  .    sAggInfo.nA
2c308 63 63 75 6d 75 6c 61 74 6f 72 20 3d 20 73 41 67  ccumulator = sAg
2c309 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20  gInfo.nColumn;. 
2c30a 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41     for(i=0; i<sA
2c30b 67 67 49 6e 66 6f 2e 6e 46 75 6e 63 3b 20 69 2b  ggInfo.nFunc; i+
2c30c 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  +){.      assert
2c30d 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
2c30e 74 79 28 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e  ty(sAggInfo.aFun
2c30f 63 5b 69 5d 2e 70 45 78 70 72 2c 20 45 50 5f 78  c[i].pExpr, EP_x
2c310 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
2c311 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e     sqlite3ExprAn
2c312 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73 4e  alyzeAggList(&sN
2c313 43 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e  C, sAggInfo.aFun
2c314 63 5b 69 5d 2e 70 45 78 70 72 2d 3e 78 2e 70 4c  c[i].pExpr->x.pL
2c315 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ist);.    }.    
2c316 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2c317 69 6c 65 64 20 29 20 67 6f 74 6f 20 73 65 6c 65  iled ) goto sele
2c318 63 74 5f 65 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20  ct_end;..    /* 
2c319 50 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 61  Processing for a
2c31a 67 67 72 65 67 61 74 65 73 20 77 69 74 68 20 47  ggregates with G
2c31b 52 4f 55 50 20 42 59 20 69 73 20 76 65 72 79 20  ROUP BY is very 
2c31c 64 69 66 66 65 72 65 6e 74 20 61 6e 64 0a 20 20  different and.  
2c31d 20 20 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20 63    ** much more c
2c31e 6f 6d 70 6c 65 78 20 74 68 61 6e 20 61 67 67 72  omplex than aggr
2c31f 65 67 61 74 65 73 20 77 69 74 68 6f 75 74 20 61  egates without a
2c320 20 47 52 4f 55 50 20 42 59 2e 0a 20 20 20 20 2a   GROUP BY..    *
2c321 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70  /.    if( pGroup
2c322 42 79 20 29 7b 0a 20 20 20 20 20 20 4b 65 79 49  By ){.      KeyI
2c323 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20  nfo *pKeyInfo;  
2c324 2f 2a 20 4b 65 79 69 6e 67 20 69 6e 66 6f 72 6d  /* Keying inform
2c325 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 67 72  ation for the gr
2c326 6f 75 70 20 62 79 20 63 6c 61 75 73 65 20 2a 2f  oup by clause */
2c327 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 3b 20 20  .      int j1;  
2c328 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 2d             /* A-
2c329 76 73 2d 42 20 63 6f 6d 70 61 72 69 73 69 6f 6e  vs-B comparision
2c32a 20 6a 75 6d 70 20 2a 2f 0a 20 20 20 20 20 20 69   jump */.      i
2c32b 6e 74 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  nt addrOutputRow
2c32c 3b 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 73  ;  /* Start of s
2c32d 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6f  ubroutine that o
2c32e 75 74 70 75 74 73 20 61 20 72 65 73 75 6c 74 20  utputs a result 
2c32f 72 6f 77 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  row */.      int
2c330 20 72 65 67 4f 75 74 70 75 74 52 6f 77 3b 20 20   regOutputRow;  
2c331 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64 72 65   /* Return addre
2c332 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ss register for 
2c333 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e  output subroutin
2c334 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61  e */.      int a
2c335 64 64 72 53 65 74 41 62 6f 72 74 3b 20 20 20 2f  ddrSetAbort;   /
2c336 2a 20 53 65 74 20 74 68 65 20 61 62 6f 72 74 20  * Set the abort 
2c337 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e 20  flag and return 
2c338 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  */.      int add
2c339 72 54 6f 70 4f 66 4c 6f 6f 70 3b 20 20 2f 2a 20  rTopOfLoop;  /* 
2c33a 54 6f 70 20 6f 66 20 74 68 65 20 69 6e 70 75 74  Top of the input
2c33b 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 69   loop */.      i
2c33c 6e 74 20 61 64 64 72 53 6f 72 74 69 6e 67 49 64  nt addrSortingId
2c33d 78 3b 20 2f 2a 20 54 68 65 20 4f 50 5f 4f 70 65  x; /* The OP_Ope
2c33e 6e 45 70 68 65 6d 65 72 61 6c 20 66 6f 72 20 74  nEphemeral for t
2c33f 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  he sorting index
2c340 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
2c341 64 72 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a  drReset;      /*
2c342 20 53 75 62 72 6f 75 74 69 6e 65 20 66 6f 72 20   Subroutine for 
2c343 72 65 73 65 74 74 69 6e 67 20 74 68 65 20 61 63  resetting the ac
2c344 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20  cumulator */.   
2c345 20 20 20 69 6e 74 20 72 65 67 52 65 73 65 74 3b     int regReset;
2c346 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
2c347 20 61 64 64 72 65 73 73 20 72 65 67 69 73 74 65   address registe
2c348 72 20 66 6f 72 20 72 65 73 65 74 20 73 75 62 72  r for reset subr
2c349 6f 75 74 69 6e 65 20 2a 2f 0a 0a 20 20 20 20 20  outine */..     
2c34a 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
2c34b 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  a GROUP BY claus
2c34c 65 20 77 65 20 6d 69 67 68 74 20 6e 65 65 64 20  e we might need 
2c34d 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  a sorting index 
2c34e 74 6f 0a 20 20 20 20 20 20 2a 2a 20 69 6d 70 6c  to.      ** impl
2c34f 65 6d 65 6e 74 20 69 74 2e 20 20 41 6c 6c 6f 63  ement it.  Alloc
2c350 61 74 65 20 74 68 61 74 20 73 6f 72 74 69 6e 67  ate that sorting
2c351 20 69 6e 64 65 78 20 6e 6f 77 2e 20 20 49 66 20   index now.  If 
2c352 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 20 20 20  it turns out.   
2c353 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64 6f     ** that we do
2c354 20 6e 6f 74 20 6e 65 65 64 20 69 74 20 61 66 74   not need it aft
2c355 65 72 20 61 6c 6c 2c 20 74 68 65 20 4f 70 65 6e  er all, the Open
2c356 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75  Ephemeral instru
2c357 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 77  ction.      ** w
2c358 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72 74 65 64  ill be converted
2c359 20 69 6e 74 6f 20 61 20 4e 6f 6f 70 2e 20 20 0a   into a Noop.  .
2c35a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
2c35b 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49  AggInfo.sortingI
2c35c 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  dx = pParse->nTa
2c35d 62 2b 2b 3b 0a 20 20 20 20 20 20 70 4b 65 79 49  b++;.      pKeyI
2c35e 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f  nfo = keyInfoFro
2c35f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65  mExprList(pParse
2c360 2c 20 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20  , pGroupBy);.   
2c361 20 20 20 61 64 64 72 53 6f 72 74 69 6e 67 49 64     addrSortingId
2c362 78 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  x = sqlite3VdbeA
2c363 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
2c364 45 70 68 65 6d 65 72 61 6c 2c 20 0a 20 20 20 20  Ephemeral, .    
2c365 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 73        sAggInfo.s
2c366 6f 72 74 69 6e 67 49 64 78 2c 20 73 41 67 67 49  ortingIdx, sAggI
2c367 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75  nfo.nSortingColu
2c368 6d 6e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 30  mn, .          0
2c369 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66  , (char*)pKeyInf
2c36a 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  o, P4_KEYINFO_HA
2c36b 4e 44 4f 46 46 29 3b 0a 0a 20 20 20 20 20 20 2f  NDOFF);..      /
2c36c 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 6d 65 6d  * Initialize mem
2c36d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 75 73  ory locations us
2c36e 65 64 20 62 79 20 47 52 4f 55 50 20 42 59 20 61  ed by GROUP BY a
2c36f 67 67 72 65 67 61 74 65 20 70 72 6f 63 65 73 73  ggregate process
2c370 69 6e 67 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ing.      */.   
2c371 20 20 20 69 55 73 65 46 6c 61 67 20 3d 20 2b 2b     iUseFlag = ++
2c372 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2c373 20 20 20 20 69 41 62 6f 72 74 46 6c 61 67 20 3d      iAbortFlag =
2c374 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2c375 0a 20 20 20 20 20 20 72 65 67 4f 75 74 70 75 74  .      regOutput
2c376 52 6f 77 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Row = ++pParse->
2c377 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64 64 72  nMem;.      addr
2c378 4f 75 74 70 75 74 52 6f 77 20 3d 20 73 71 6c 69  OutputRow = sqli
2c379 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2c37a 28 76 29 3b 0a 20 20 20 20 20 20 72 65 67 52 65  (v);.      regRe
2c37b 73 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  set = ++pParse->
2c37c 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64 64 72  nMem;.      addr
2c37d 52 65 73 65 74 20 3d 20 73 71 6c 69 74 65 33 56  Reset = sqlite3V
2c37e 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
2c37f 0a 20 20 20 20 20 20 69 41 4d 65 6d 20 3d 20 70  .      iAMem = p
2c380 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b  Parse->nMem + 1;
2c381 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
2c382 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79 2d  Mem += pGroupBy-
2c383 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 69 42  >nExpr;.      iB
2c384 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Mem = pParse->nM
2c385 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 50  em + 1;.      pP
2c386 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47  arse->nMem += pG
2c387 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20  roupBy->nExpr;. 
2c388 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c389 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2c38a 65 67 65 72 2c 20 30 2c 20 69 41 62 6f 72 74 46  eger, 0, iAbortF
2c38b 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65  lag);.      Vdbe
2c38c 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63 6c 65  Comment((v, "cle
2c38d 61 72 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29  ar abort flag"))
2c38e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2c38f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c390 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 55 73 65  Integer, 0, iUse
2c391 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62  Flag);.      Vdb
2c392 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e  eComment((v, "in
2c393 64 69 63 61 74 65 20 61 63 63 75 6d 75 6c 61 74  dicate accumulat
2c394 6f 72 20 65 6d 70 74 79 22 29 29 3b 0a 0a 20 20  or empty"));..  
2c395 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 6c      /* Begin a l
2c396 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20 65 78  oop that will ex
2c397 74 72 61 63 74 20 61 6c 6c 20 73 6f 75 72 63 65  tract all source
2c398 20 72 6f 77 73 20 69 6e 20 47 52 4f 55 50 20 42   rows in GROUP B
2c399 59 20 6f 72 64 65 72 2e 0a 20 20 20 20 20 20 2a  Y order..      *
2c39a 2a 20 54 68 69 73 20 6d 69 67 68 74 20 69 6e 76  * This might inv
2c39b 6f 6c 76 65 20 74 77 6f 20 73 65 70 61 72 61 74  olve two separat
2c39c 65 20 6c 6f 6f 70 73 20 77 69 74 68 20 61 6e 20  e loops with an 
2c39d 4f 50 5f 53 6f 72 74 20 69 6e 20 62 65 74 77 65  OP_Sort in betwe
2c39e 65 6e 2c 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20  en, or.      ** 
2c39f 69 74 20 6d 69 67 68 74 20 62 65 20 61 20 73 69  it might be a si
2c3a0 6e 67 6c 65 20 6c 6f 6f 70 20 74 68 61 74 20 75  ngle loop that u
2c3a1 73 65 73 20 61 6e 20 69 6e 64 65 78 20 74 6f 20  ses an index to 
2c3a2 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74  extract informat
2c3a3 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20  ion.      ** in 
2c3a4 74 68 65 20 72 69 67 68 74 20 6f 72 64 65 72 20  the right order 
2c3a5 74 6f 20 62 65 67 69 6e 20 77 69 74 68 2e 0a 20  to begin with.. 
2c3a6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
2c3a7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c3a8 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
2c3a9 52 65 73 65 74 2c 20 61 64 64 72 52 65 73 65 74  Reset, addrReset
2c3aa 29 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f 20  );.      pWInfo 
2c3ab 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  = sqlite3WhereBe
2c3ac 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62  gin(pParse, pTab
2c3ad 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70  List, pWhere, &p
2c3ae 47 72 6f 75 70 42 79 2c 20 30 29 3b 0a 20 20 20  GroupBy, 0);.   
2c3af 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30     if( pWInfo==0
2c3b0 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65   ) goto select_e
2c3b1 6e 64 3b 0a 20 20 20 20 20 20 69 66 28 20 70 47  nd;.      if( pG
2c3b2 72 6f 75 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20  roupBy==0 ){.   
2c3b3 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 74 69       /* The opti
2c3b4 6d 69 7a 65 72 20 69 73 20 61 62 6c 65 20 74 6f  mizer is able to
2c3b5 20 64 65 6c 69 76 65 72 20 72 6f 77 73 20 69 6e   deliver rows in
2c3b6 20 67 72 6f 75 70 20 62 79 20 6f 72 64 65 72 20   group by order 
2c3b7 73 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65  so.        ** we
2c3b8 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20   do not have to 
2c3b9 73 6f 72 74 2e 20 20 54 68 65 20 4f 50 5f 4f 70  sort.  The OP_Op
2c3ba 65 6e 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  enEphemeral tabl
2c3bb 65 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20  e will be.      
2c3bc 20 20 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 20 6c    ** cancelled l
2c3bd 61 74 65 72 20 62 65 63 61 75 73 65 20 77 65 20  ater because we 
2c3be 73 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75 73  still need to us
2c3bf 65 20 74 68 65 20 70 4b 65 79 49 6e 66 6f 0a 20  e the pKeyInfo. 
2c3c0 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2c3c1 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e    pGroupBy = p->
2c3c2 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 20 20  pGroupBy;.      
2c3c3 20 20 67 72 6f 75 70 42 79 53 6f 72 74 20 3d 20    groupBySort = 
2c3c4 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  0;.      }else{.
2c3c5 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 73 20          /* Rows 
2c3c6 61 72 65 20 63 6f 6d 69 6e 67 20 6f 75 74 20 69  are coming out i
2c3c7 6e 20 75 6e 64 65 74 65 72 6d 69 6e 65 64 20 6f  n undetermined o
2c3c8 72 64 65 72 2e 20 20 57 65 20 68 61 76 65 20 74  rder.  We have t
2c3c9 6f 20 70 75 73 68 0a 20 20 20 20 20 20 20 20 2a  o push.        *
2c3ca 2a 20 65 61 63 68 20 72 6f 77 20 69 6e 74 6f 20  * each row into 
2c3cb 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c  a sorting index,
2c3cc 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 66   terminate the f
2c3cd 69 72 73 74 20 6c 6f 6f 70 2c 0a 20 20 20 20 20  irst loop,.     
2c3ce 20 20 20 2a 2a 20 74 68 65 6e 20 6c 6f 6f 70 20     ** then loop 
2c3cf 6f 76 65 72 20 74 68 65 20 73 6f 72 74 69 6e 67  over the sorting
2c3d0 20 69 6e 64 65 78 20 69 6e 20 6f 72 64 65 72 20   index in order 
2c3d1 74 6f 20 67 65 74 20 74 68 65 20 6f 75 74 70 75  to get the outpu
2c3d2 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20  t.        ** in 
2c3d3 73 6f 72 74 65 64 20 6f 72 64 65 72 0a 20 20 20  sorted order.   
2c3d4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2c3d5 69 6e 74 20 72 65 67 42 61 73 65 3b 0a 20 20 20  int regBase;.   
2c3d6 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 6f       int regReco
2c3d7 72 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  rd;.        int 
2c3d8 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e  nCol;.        in
2c3d9 74 20 6e 47 72 6f 75 70 42 79 3b 0a 0a 20 20 20  t nGroupBy;..   
2c3da 20 20 20 20 20 67 72 6f 75 70 42 79 53 6f 72 74       groupBySort
2c3db 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 6e 47   = 1;.        nG
2c3dc 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42  roupBy = pGroupB
2c3dd 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  y->nExpr;.      
2c3de 20 20 6e 43 6f 6c 20 3d 20 6e 47 72 6f 75 70 42    nCol = nGroupB
2c3df 79 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 6a  y + 1;.        j
2c3e0 20 3d 20 6e 47 72 6f 75 70 42 79 2b 31 3b 0a 20   = nGroupBy+1;. 
2c3e1 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
2c3e2 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75  i<sAggInfo.nColu
2c3e3 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
2c3e4 20 20 20 20 69 66 28 20 73 41 67 67 49 6e 66 6f      if( sAggInfo
2c3e5 2e 61 43 6f 6c 5b 69 5d 2e 69 53 6f 72 74 65 72  .aCol[i].iSorter
2c3e6 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20  Column>=j ){.   
2c3e7 20 20 20 20 20 20 20 20 20 6e 43 6f 6c 2b 2b 3b           nCol++;
2c3e8 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 2b 2b  .            j++
2c3e9 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2c3ea 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c3eb 72 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65  regBase = sqlite
2c3ec 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
2c3ed 61 72 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20  arse, nCol);.   
2c3ee 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2c3ef 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
2c3f0 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
2c3f1 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c  te3ExprCodeExprL
2c3f2 69 73 74 28 70 50 61 72 73 65 2c 20 70 47 72 6f  ist(pParse, pGro
2c3f3 75 70 42 79 2c 20 72 65 67 42 61 73 65 2c 20 30  upBy, regBase, 0
2c3f4 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2c3f5 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c3f6 4f 50 5f 53 65 71 75 65 6e 63 65 2c 20 73 41 67  OP_Sequence, sAg
2c3f7 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78  gInfo.sortingIdx
2c3f8 2c 72 65 67 42 61 73 65 2b 6e 47 72 6f 75 70 42  ,regBase+nGroupB
2c3f9 79 29 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20  y);.        j = 
2c3fa 6e 47 72 6f 75 70 42 79 2b 31 3b 0a 20 20 20 20  nGroupBy+1;.    
2c3fb 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73      for(i=0; i<s
2c3fc 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b  AggInfo.nColumn;
2c3fd 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
2c3fe 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
2c3ff 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 73 41 67  col *pCol = &sAg
2c400 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d 3b 0a 20  gInfo.aCol[i];. 
2c401 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f           if( pCo
2c402 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  l->iSorterColumn
2c403 3e 3d 6a 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=j ){.         
2c404 20 20 20 69 6e 74 20 72 31 20 3d 20 6a 20 2b 20     int r1 = j + 
2c405 72 65 67 42 61 73 65 3b 0a 20 20 20 20 20 20 20  regBase;.       
2c406 20 20 20 20 20 69 6e 74 20 72 32 3b 0a 0a 20 20       int r2;..  
2c407 20 20 20 20 20 20 20 20 20 20 72 32 20 3d 20 73            r2 = s
2c408 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65  qlite3ExprCodeGe
2c409 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20  tColumn(pParse, 
2c40a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c40b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c40c 70 43 6f 6c 2d 3e 70 54 61 62 2c 20 70 43 6f 6c  pCol->pTab, pCol
2c40d 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 2d  ->iColumn, pCol-
2c40e 3e 69 54 61 62 6c 65 2c 20 72 31 2c 20 30 29 3b  >iTable, r1, 0);
2c40f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
2c410 20 72 31 21 3d 72 32 20 29 7b 0a 20 20 20 20 20   r1!=r2 ){.     
2c411 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2c412 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c413 5f 53 43 6f 70 79 2c 20 72 32 2c 20 72 31 29 3b  _SCopy, r2, r1);
2c414 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
2c415 20 20 20 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a             j++;.
2c416 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2c417 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65      }.        re
2c418 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 65  gRecord = sqlite
2c419 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2c41a 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  se);.        sql
2c41b 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2c41c 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
2c41d 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2c 20   regBase, nCol, 
2c41e 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 20 20  regRecord);.    
2c41f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c420 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49  ddOp2(v, OP_IdxI
2c421 6e 73 65 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e  nsert, sAggInfo.
2c422 73 6f 72 74 69 6e 67 49 64 78 2c 20 72 65 67 52  sortingIdx, regR
2c423 65 63 6f 72 64 29 3b 0a 20 20 20 20 20 20 20 20  ecord);.        
2c424 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2c425 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
2c426 67 52 65 63 6f 72 64 29 3b 0a 20 20 20 20 20 20  gRecord);.      
2c427 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2c428 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
2c429 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 29  , regBase, nCol)
2c42a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c42b 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
2c42c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2c42d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c42e 4f 50 5f 53 6f 72 74 2c 20 73 41 67 67 49 6e 66  OP_Sort, sAggInf
2c42f 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 61 64  o.sortingIdx, ad
2c430 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 20 20  drEnd);.        
2c431 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2c432 22 47 52 4f 55 50 20 42 59 20 73 6f 72 74 22 29  "GROUP BY sort")
2c433 29 3b 0a 20 20 20 20 20 20 20 20 73 41 67 67 49  );.        sAggI
2c434 6e 66 6f 2e 75 73 65 53 6f 72 74 69 6e 67 49 64  nfo.useSortingId
2c435 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 73  x = 1;.        s
2c436 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
2c437 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
2c438 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
2c439 45 76 61 6c 75 61 74 65 20 74 68 65 20 63 75 72  Evaluate the cur
2c43a 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20 74 65  rent GROUP BY te
2c43b 72 6d 73 20 61 6e 64 20 73 74 6f 72 65 20 69 6e  rms and store in
2c43c 20 62 30 2c 20 62 31 2c 20 62 32 2e 2e 2e 0a 20   b0, b1, b2.... 
2c43d 20 20 20 20 20 2a 2a 20 28 62 30 20 69 73 20 6d       ** (b0 is m
2c43e 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 69  emory location i
2c43f 42 4d 65 6d 2b 30 2c 20 62 31 20 69 73 20 69 42  BMem+0, b1 is iB
2c440 4d 65 6d 2b 31 2c 20 61 6e 64 20 73 6f 20 66 6f  Mem+1, and so fo
2c441 72 74 68 29 0a 20 20 20 20 20 20 2a 2a 20 54 68  rth).      ** Th
2c442 65 6e 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63  en compare the c
2c443 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20  urrent GROUP BY 
2c444 74 65 72 6d 73 20 61 67 61 69 6e 73 74 20 74 68  terms against th
2c445 65 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73  e GROUP BY terms
2c446 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74  .      ** from t
2c447 68 65 20 70 72 65 76 69 6f 75 73 20 72 6f 77 20  he previous row 
2c448 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64  currently stored
2c449 20 69 6e 20 61 30 2c 20 61 31 2c 20 61 32 2e 2e   in a0, a1, a2..
2c44a 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2c44b 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 20 3d   addrTopOfLoop =
2c44c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
2c44d 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
2c44e 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2c44f 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
2c450 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  .      for(j=0; 
2c451 6a 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70  j<pGroupBy->nExp
2c452 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  r; j++){.       
2c453 20 69 66 28 20 67 72 6f 75 70 42 79 53 6f 72 74   if( groupBySort
2c454 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
2c455 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2c456 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 41  v, OP_Column, sA
2c457 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64  ggInfo.sortingId
2c458 78 2c 20 6a 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a  x, j, iBMem+j);.
2c459 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2c45a 20 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66           sAggInf
2c45b 6f 2e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 31  o.directMode = 1
2c45c 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2c45d 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
2c45e 73 65 2c 20 70 47 72 6f 75 70 42 79 2d 3e 61 5b  se, pGroupBy->a[
2c45f 6a 5d 2e 70 45 78 70 72 2c 20 69 42 4d 65 6d 2b  j].pExpr, iBMem+
2c460 6a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  j);.        }.  
2c461 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2c462 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2c463 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 69 41 4d   OP_Compare, iAM
2c464 65 6d 2c 20 69 42 4d 65 6d 2c 20 70 47 72 6f 75  em, iBMem, pGrou
2c465 70 42 79 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20  pBy->nExpr,.    
2c466 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c467 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
2c468 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
2c469 4f 29 3b 0a 20 20 20 20 20 20 6a 31 20 3d 20 73  O);.      j1 = s
2c46a 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2c46b 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
2c46c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c46d 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a 31  3(v, OP_Jump, j1
2c46e 2b 31 2c 20 30 2c 20 6a 31 2b 31 29 3b 0a 0a 20  +1, 0, j1+1);.. 
2c46f 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2c470 20 63 6f 64 65 20 74 68 61 74 20 72 75 6e 73 20   code that runs 
2c471 77 68 65 6e 65 76 65 72 20 74 68 65 20 47 52 4f  whenever the GRO
2c472 55 50 20 42 59 20 63 68 61 6e 67 65 73 2e 0a 20  UP BY changes.. 
2c473 20 20 20 20 20 2a 2a 20 43 68 61 6e 67 65 73 20       ** Changes 
2c474 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 20  in the GROUP BY 
2c475 61 72 65 20 64 65 74 65 63 74 65 64 20 62 79 20  are detected by 
2c476 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 6f 64  the previous cod
2c477 65 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 63 6b  e.      ** block
2c478 2e 20 20 49 66 20 74 68 65 72 65 20 77 65 72 65  .  If there were
2c479 20 6e 6f 20 63 68 61 6e 67 65 73 2c 20 74 68 69   no changes, thi
2c47a 73 20 62 6c 6f 63 6b 20 69 73 20 73 6b 69 70 70  s block is skipp
2c47b 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ed..      **.   
2c47c 20 20 20 2a 2a 20 54 68 69 73 20 63 6f 64 65 20     ** This code 
2c47d 63 6f 70 69 65 73 20 63 75 72 72 65 6e 74 20 67  copies current g
2c47e 72 6f 75 70 20 62 79 20 74 65 72 6d 73 20 69 6e  roup by terms in
2c47f 20 62 30 2c 62 31 2c 62 32 2c 2e 2e 2e 0a 20 20   b0,b1,b2,....  
2c480 20 20 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20 61      ** over to a
2c481 30 2c 61 31 2c 61 32 2e 20 20 49 74 20 74 68 65  0,a1,a2.  It the
2c482 6e 20 63 61 6c 6c 73 20 74 68 65 20 6f 75 74 70  n calls the outp
2c483 75 74 20 73 75 62 72 6f 75 74 69 6e 65 0a 20 20  ut subroutine.  
2c484 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74      ** and reset
2c485 73 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  s the aggregate 
2c486 61 63 63 75 6d 75 6c 61 74 6f 72 20 72 65 67 69  accumulator regi
2c487 73 74 65 72 73 20 69 6e 20 70 72 65 70 61 72 61  sters in prepara
2c488 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 66 6f  tion.      ** fo
2c489 72 20 74 68 65 20 6e 65 78 74 20 47 52 4f 55 50  r the next GROUP
2c48a 20 42 59 20 62 61 74 63 68 2e 0a 20 20 20 20 20   BY batch..     
2c48b 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2c48c 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50  3ExprCodeMove(pP
2c48d 61 72 73 65 2c 20 69 42 4d 65 6d 2c 20 69 41 4d  arse, iBMem, iAM
2c48e 65 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45  em, pGroupBy->nE
2c48f 78 70 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  xpr);.      sqli
2c490 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c491 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75   OP_Gosub, regOu
2c492 74 70 75 74 52 6f 77 2c 20 61 64 64 72 4f 75 74  tputRow, addrOut
2c493 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 56  putRow);.      V
2c494 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2c495 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 22 29  output one row")
2c496 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c497 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c498 5f 49 66 50 6f 73 2c 20 69 41 62 6f 72 74 46 6c  _IfPos, iAbortFl
2c499 61 67 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20  ag, addrEnd);.  
2c49a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2c49b 28 76 2c 20 22 63 68 65 63 6b 20 61 62 6f 72 74  (v, "check abort
2c49c 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20 20   flag"));.      
2c49d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c49e 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2c49f 65 67 52 65 73 65 74 2c 20 61 64 64 72 52 65 73  egReset, addrRes
2c4a0 65 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  et);.      VdbeC
2c4a1 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65 73 65  omment((v, "rese
2c4a2 74 20 61 63 63 75 6d 75 6c 61 74 6f 72 22 29 29  t accumulator"))
2c4a3 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61  ;..      /* Upda
2c4a4 74 65 20 74 68 65 20 61 67 67 72 65 67 61 74 65  te the aggregate
2c4a5 20 61 63 63 75 6d 75 6c 61 74 6f 72 73 20 62 61   accumulators ba
2c4a6 73 65 64 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65  sed on the conte
2c4a7 6e 74 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74  nt of.      ** t
2c4a8 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 20  he current row. 
2c4a9 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
2c4aa 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2c4ab 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20  e(v, j1);.      
2c4ac 75 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f  updateAccumulato
2c4ad 72 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49  r(pParse, &sAggI
2c4ae 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  nfo);.      sqli
2c4af 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c4b0 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
2c4b1 69 55 73 65 46 6c 61 67 29 3b 0a 20 20 20 20 20  iUseFlag);.     
2c4b2 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2c4b3 20 22 69 6e 64 69 63 61 74 65 20 64 61 74 61 20   "indicate data 
2c4b4 69 6e 20 61 63 63 75 6d 75 6c 61 74 6f 72 22 29  in accumulator")
2c4b5 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 45 6e 64  );..      /* End
2c4b6 20 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20 20   of the loop.   
2c4b7 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
2c4b8 67 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a 20  groupBySort ){. 
2c4b9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2c4ba 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2c4bb 65 78 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f  ext, sAggInfo.so
2c4bc 72 74 69 6e 67 49 64 78 2c 20 61 64 64 72 54 6f  rtingIdx, addrTo
2c4bd 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 20 20  pOfLoop);.      
2c4be 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
2c4bf 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70  qlite3WhereEnd(p
2c4c0 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20  WInfo);.        
2c4c1 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2c4c2 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 64 72 53  eToNoop(v, addrS
2c4c3 6f 72 74 69 6e 67 49 64 78 2c 20 31 29 3b 0a 20  ortingIdx, 1);. 
2c4c4 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
2c4c5 20 4f 75 74 70 75 74 20 74 68 65 20 66 69 6e 61   Output the fina
2c4c6 6c 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 0a  l row of result.
2c4c7 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
2c4c8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c4c9 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
2c4ca 67 4f 75 74 70 75 74 52 6f 77 2c 20 61 64 64 72  gOutputRow, addr
2c4cb 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20  OutputRow);.    
2c4cc 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2c4cd 2c 20 22 6f 75 74 70 75 74 20 66 69 6e 61 6c 20  , "output final 
2c4ce 72 6f 77 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f  row"));..      /
2c4cf 2a 20 4a 75 6d 70 20 6f 76 65 72 20 74 68 65 20  * Jump over the 
2c4d0 73 75 62 72 6f 75 74 69 6e 65 73 0a 20 20 20 20  subroutines.    
2c4d1 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
2c4d2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c4d3 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72  OP_Goto, 0, addr
2c4d4 45 6e 64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  End);..      /* 
2c4d5 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f  Generate a subro
2c4d6 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75  utine that outpu
2c4d7 74 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  ts a single row 
2c4d8 6f 66 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20  of the result.  
2c4d9 20 20 20 20 2a 2a 20 73 65 74 2e 20 20 54 68 69      ** set.  Thi
2c4da 73 20 73 75 62 72 6f 75 74 69 6e 65 20 66 69 72  s subroutine fir
2c4db 73 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20  st looks at the 
2c4dc 69 55 73 65 46 6c 61 67 2e 20 20 49 66 20 69 55  iUseFlag.  If iU
2c4dd 73 65 46 6c 61 67 0a 20 20 20 20 20 20 2a 2a 20  seFlag.      ** 
2c4de 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  is less than or 
2c4df 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74  equal to zero, t
2c4e0 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 69 73  he subroutine is
2c4e1 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 0a 20 20   a no-op.  If.  
2c4e2 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65      ** the proce
2c4e3 73 73 69 6e 67 20 63 61 6c 6c 73 20 66 6f 72 20  ssing calls for 
2c4e4 74 68 65 20 71 75 65 72 79 20 74 6f 20 61 62 6f  the query to abo
2c4e5 72 74 2c 20 74 68 69 73 20 73 75 62 72 6f 75 74  rt, this subrout
2c4e6 69 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63  ine.      ** inc
2c4e7 72 65 6d 65 6e 74 73 20 74 68 65 20 69 41 62 6f  rements the iAbo
2c4e8 72 74 46 6c 61 67 20 6d 65 6d 6f 72 79 20 6c 6f  rtFlag memory lo
2c4e9 63 61 74 69 6f 6e 20 62 65 66 6f 72 65 20 72 65  cation before re
2c4ea 74 75 72 6e 69 6e 67 20 69 6e 0a 20 20 20 20 20  turning in.     
2c4eb 20 2a 2a 20 6f 72 64 65 72 20 74 6f 20 73 69 67   ** order to sig
2c4ec 6e 61 6c 20 74 68 65 20 63 61 6c 6c 65 72 20 74  nal the caller t
2c4ed 6f 20 61 62 6f 72 74 2e 0a 20 20 20 20 20 20 2a  o abort..      *
2c4ee 2f 0a 20 20 20 20 20 20 61 64 64 72 53 65 74 41  /.      addrSetA
2c4ef 62 6f 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64  bort = sqlite3Vd
2c4f0 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2c4f1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2c4f2 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c4f3 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 41 62 6f  Integer, 1, iAbo
2c4f4 72 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56  rtFlag);.      V
2c4f5 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2c4f6 73 65 74 20 61 62 6f 72 74 20 66 6c 61 67 22 29  set abort flag")
2c4f7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c4f8 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2c4f9 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70  _Return, regOutp
2c4fa 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 73 71  utRow);.      sq
2c4fb 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2c4fc 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 4f 75 74  Label(v, addrOut
2c4fd 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 61  putRow);.      a
2c4fe 64 64 72 4f 75 74 70 75 74 52 6f 77 20 3d 20 73  ddrOutputRow = s
2c4ff 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2c500 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
2c501 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c502 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69  2(v, OP_IfPos, i
2c503 55 73 65 46 6c 61 67 2c 20 61 64 64 72 4f 75 74  UseFlag, addrOut
2c504 70 75 74 52 6f 77 2b 32 29 3b 0a 20 20 20 20 20  putRow+2);.     
2c505 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2c506 20 22 47 72 6f 75 70 62 79 20 72 65 73 75 6c 74   "Groupby result
2c507 20 67 65 6e 65 72 61 74 6f 72 20 65 6e 74 72 79   generator entry
2c508 20 70 6f 69 6e 74 22 29 29 3b 0a 20 20 20 20 20   point"));.     
2c509 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c50a 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c  p1(v, OP_Return,
2c50b 20 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a   regOutputRow);.
2c50c 20 20 20 20 20 20 66 69 6e 61 6c 69 7a 65 41 67        finalizeAg
2c50d 67 46 75 6e 63 74 69 6f 6e 73 28 70 50 61 72 73  gFunctions(pPars
2c50e 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20  e, &sAggInfo);. 
2c50f 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2c510 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
2c511 70 48 61 76 69 6e 67 2c 20 61 64 64 72 4f 75 74  pHaving, addrOut
2c512 70 75 74 52 6f 77 2b 31 2c 20 53 51 4c 49 54 45  putRow+1, SQLITE
2c513 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
2c514 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c      selectInnerL
2c515 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  oop(pParse, p, p
2c516 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20  ->pEList, 0, 0, 
2c517 70 4f 72 64 65 72 42 79 2c 0a 20 20 20 20 20 20  pOrderBy,.      
2c518 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c519 64 69 73 74 69 6e 63 74 2c 20 70 44 65 73 74 2c  distinct, pDest,
2c51a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c51b 20 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75         addrOutpu
2c51c 74 52 6f 77 2b 31 2c 20 61 64 64 72 53 65 74 41  tRow+1, addrSetA
2c51d 62 6f 72 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  bort);.      sql
2c51e 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2c51f 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67  , OP_Return, reg
2c520 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20  OutputRow);.    
2c521 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2c522 2c 20 22 65 6e 64 20 67 72 6f 75 70 62 79 20 72  , "end groupby r
2c523 65 73 75 6c 74 20 67 65 6e 65 72 61 74 6f 72 22  esult generator"
2c524 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65  ));..      /* Ge
2c525 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74  nerate a subrout
2c526 69 6e 65 20 74 68 61 74 20 77 69 6c 6c 20 72 65  ine that will re
2c527 73 65 74 20 74 68 65 20 67 72 6f 75 70 2d 62 79  set the group-by
2c528 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 20 20 20   accumulator.   
2c529 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
2c52a 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2c52b 62 65 6c 28 76 2c 20 61 64 64 72 52 65 73 65 74  bel(v, addrReset
2c52c 29 3b 0a 20 20 20 20 20 20 72 65 73 65 74 41 63  );.      resetAc
2c52d 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65  cumulator(pParse
2c52e 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
2c52f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c530 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75  ddOp1(v, OP_Retu
2c531 72 6e 2c 20 72 65 67 52 65 73 65 74 29 3b 0a 20  rn, regReset);. 
2c532 20 20 20 20 0a 20 20 20 20 7d 20 2f 2a 20 65 6e      .    } /* en
2c533 64 69 66 20 70 47 72 6f 75 70 42 79 2e 20 20 42  dif pGroupBy.  B
2c534 65 67 69 6e 20 61 67 67 72 65 67 61 74 65 20 71  egin aggregate q
2c535 75 65 72 69 65 73 20 77 69 74 68 6f 75 74 20 47  ueries without G
2c536 52 4f 55 50 20 42 59 3a 20 2a 2f 0a 20 20 20 20  ROUP BY: */.    
2c537 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 45 78 70  else {.      Exp
2c538 72 4c 69 73 74 20 2a 70 44 65 6c 20 3d 20 30 3b  rList *pDel = 0;
2c539 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2c53a 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a  OMIT_BTREECOUNT.
2c53b 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
2c53c 62 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 54  b;.      if( (pT
2c53d 61 62 20 3d 20 69 73 53 69 6d 70 6c 65 43 6f 75  ab = isSimpleCou
2c53e 6e 74 28 70 2c 20 26 73 41 67 67 49 6e 66 6f 29  nt(p, &sAggInfo)
2c53f 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
2c540 2f 2a 20 49 66 20 69 73 53 69 6d 70 6c 65 43 6f  /* If isSimpleCo
2c541 75 6e 74 28 29 20 72 65 74 75 72 6e 73 20 61 20  unt() returns a 
2c542 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 61 62  pointer to a Tab
2c543 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 74 68  le structure, th
2c544 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  en.        ** th
2c545 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
2c546 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  is of the form:.
2c547 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
2c548 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 63     **   SELECT c
2c549 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c 74 62  ount(*) FROM <tb
2c54a 6c 3e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  l>.        **.  
2c54b 20 20 20 20 20 20 2a 2a 20 77 68 65 72 65 20 74        ** where t
2c54c 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
2c54d 72 65 20 72 65 74 75 72 6e 65 64 20 72 65 70 72  re returned repr
2c54e 65 73 65 6e 74 73 20 74 61 62 6c 65 20 3c 74 62  esents table <tb
2c54f 6c 3e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  l>..        **. 
2c550 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 73         ** This s
2c551 74 61 74 65 6d 65 6e 74 20 69 73 20 73 6f 20 63  tatement is so c
2c552 6f 6d 6d 6f 6e 20 74 68 61 74 20 69 74 20 69 73  ommon that it is
2c553 20 6f 70 74 69 6d 69 7a 65 64 20 73 70 65 63 69   optimized speci
2c554 61 6c 6c 79 2e 20 54 68 65 0a 20 20 20 20 20 20  ally. The.      
2c555 20 20 2a 2a 20 4f 50 5f 43 6f 75 6e 74 20 69 6e    ** OP_Count in
2c556 73 74 72 75 63 74 69 6f 6e 20 69 73 20 65 78 65  struction is exe
2c557 63 75 74 65 64 20 65 69 74 68 65 72 20 6f 6e 20  cuted either on 
2c558 74 68 65 20 69 6e 74 6b 65 79 20 74 61 62 6c 65  the intkey table
2c559 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a   that.        **
2c55a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 61   contains the da
2c55b 74 61 20 66 6f 72 20 74 61 62 6c 65 20 3c 74 62  ta for table <tb
2c55c 6c 3e 20 6f 72 20 6f 6e 20 6f 6e 65 20 6f 66 20  l> or on one of 
2c55d 69 74 73 20 69 6e 64 65 78 65 73 2e 20 49 74 0a  its indexes. It.
2c55e 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 62 65          ** is be
2c55f 74 74 65 72 20 74 6f 20 65 78 65 63 75 74 65 20  tter to execute 
2c560 74 68 65 20 6f 70 20 6f 6e 20 61 6e 20 69 6e 64  the op on an ind
2c561 65 78 2c 20 61 73 20 69 6e 64 65 78 65 73 20 61  ex, as indexes a
2c562 72 65 20 61 6c 6d 6f 73 74 0a 20 20 20 20 20 20  re almost.      
2c563 20 20 2a 2a 20 61 6c 77 61 79 73 20 73 70 72 65    ** always spre
2c564 61 64 20 61 63 72 6f 73 73 20 6c 65 73 73 20 70  ad across less p
2c565 61 67 65 73 20 74 68 61 6e 20 74 68 65 69 72 20  ages than their 
2c566 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61  corresponding ta
2c567 62 6c 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f  bles..        */
2c568 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69  .        const i
2c569 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  nt iDb = sqlite3
2c56a 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
2c56b 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
2c56c 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
2c56d 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 43 73 72    const int iCsr
2c56e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2c56f 2b 3b 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72  +;     /* Cursor
2c570 20 74 6f 20 73 63 61 6e 20 62 2d 74 72 65 65 20   to scan b-tree 
2c571 2a 2f 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78  */.        Index
2c572 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
2c573 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c574 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
2c575 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  able */.        
2c576 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
2c577 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  o = 0;          
2c578 20 20 20 20 20 2f 2a 20 4b 65 79 69 6e 66 6f 20       /* Keyinfo 
2c579 66 6f 72 20 73 63 61 6e 6e 65 64 20 69 6e 64 65  for scanned inde
2c57a 78 20 2a 2f 0a 20 20 20 20 20 20 20 20 49 6e 64  x */.        Ind
2c57b 65 78 20 2a 70 42 65 73 74 20 3d 20 30 3b 20 20  ex *pBest = 0;  
2c57c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c57d 20 20 2f 2a 20 42 65 73 74 20 69 6e 64 65 78 20    /* Best index 
2c57e 66 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a  found so far */.
2c57f 20 20 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6f          int iRoo
2c580 74 20 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 20  t = pTab->tnum; 
2c581 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c582 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 73 63 61  Root page of sca
2c583 6e 6e 65 64 20 62 2d 74 72 65 65 20 2a 2f 0a 0a  nned b-tree */..
2c584 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
2c585 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
2c586 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20  pParse, iDb);.  
2c587 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62        sqlite3Tab
2c588 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
2c589 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
2c58a 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  0, pTab->zName);
2c58b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 65 61  ..        /* Sea
2c58c 72 63 68 20 66 6f 72 20 74 68 65 20 69 6e 64 65  rch for the inde
2c58d 78 20 74 68 61 74 20 68 61 73 20 74 68 65 20 6c  x that has the l
2c58e 65 61 73 74 20 61 6d 6f 75 6e 74 20 6f 66 20 63  east amount of c
2c58f 6f 6c 75 6d 6e 73 2e 20 49 66 0a 20 20 20 20 20  olumns. If.     
2c590 20 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 73     ** there is s
2c591 75 63 68 20 61 6e 20 69 6e 64 65 78 2c 20 61 6e  uch an index, an
2c592 64 20 69 74 20 68 61 73 20 6c 65 73 73 20 63 6f  d it has less co
2c593 6c 75 6d 6e 73 20 74 68 61 6e 20 74 68 65 20 74  lumns than the t
2c594 61 62 6c 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  able.        ** 
2c595 64 6f 65 73 2c 20 74 68 65 6e 20 77 65 20 63 61  does, then we ca
2c596 6e 20 61 73 73 75 6d 65 20 74 68 61 74 20 69 74  n assume that it
2c597 20 63 6f 6e 73 75 6d 65 73 20 6c 65 73 73 20 73   consumes less s
2c598 70 61 63 65 20 6f 6e 20 64 69 73 6b 20 61 6e 64  pace on disk and
2c599 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c  .        ** will
2c59a 20 74 68 65 72 65 66 6f 72 65 20 62 65 20 63 68   therefore be ch
2c59b 65 61 70 65 72 20 74 6f 20 73 63 61 6e 20 74 6f  eaper to scan to
2c59c 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 71   determine the q
2c59d 75 65 72 79 20 72 65 73 75 6c 74 2e 0a 20 20 20  uery result..   
2c59e 20 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20       ** In this 
2c59f 63 61 73 65 20 73 65 74 20 69 52 6f 6f 74 20 74  case set iRoot t
2c5a0 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  o the root page 
2c5a1 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e  number of the in
2c5a2 64 65 78 20 62 2d 74 72 65 65 0a 20 20 20 20 20  dex b-tree.     
2c5a3 20 20 20 2a 2a 20 61 6e 64 20 70 4b 65 79 49 6e     ** and pKeyIn
2c5a4 66 6f 20 74 6f 20 74 68 65 20 4b 65 79 49 6e 66  fo to the KeyInf
2c5a5 6f 20 73 74 72 75 63 74 75 72 65 20 72 65 71 75  o structure requ
2c5a6 69 72 65 64 20 74 6f 20 6e 61 76 69 67 61 74 65  ired to navigate
2c5a7 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
2c5a8 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20 2a  index..        *
2c5a9 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 20  *.        ** In 
2c5aa 70 72 61 63 74 69 63 65 20 74 68 65 20 4b 65 79  practice the Key
2c5ab 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 77  Info structure w
2c5ac 69 6c 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e  ill not be used.
2c5ad 20 49 74 20 69 73 20 6f 6e 6c 79 20 0a 20 20 20   It is only .   
2c5ae 20 20 20 20 20 2a 2a 20 70 61 73 73 65 64 20 74       ** passed t
2c5af 6f 20 6b 65 65 70 20 4f 50 5f 4f 70 65 6e 52 65  o keep OP_OpenRe
2c5b0 61 64 20 68 61 70 70 79 2e 0a 20 20 20 20 20 20  ad happy..      
2c5b1 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72    */.        for
2c5b2 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
2c5b3 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
2c5b4 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
2c5b5 20 20 20 20 20 20 20 69 66 28 20 21 70 42 65 73         if( !pBes
2c5b6 74 20 7c 7c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  t || pIdx->nColu
2c5b7 6d 6e 3c 70 42 65 73 74 2d 3e 6e 43 6f 6c 75 6d  mn<pBest->nColum
2c5b8 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  n ){.           
2c5b9 20 70 42 65 73 74 20 3d 20 70 49 64 78 3b 0a 20   pBest = pIdx;. 
2c5ba 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2c5bb 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
2c5bc 20 70 42 65 73 74 20 26 26 20 70 42 65 73 74 2d   pBest && pBest-
2c5bd 3e 6e 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e  >nColumn<pTab->n
2c5be 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Col ){.         
2c5bf 20 69 52 6f 6f 74 20 3d 20 70 42 65 73 74 2d 3e   iRoot = pBest->
2c5c0 74 6e 75 6d 3b 0a 20 20 20 20 20 20 20 20 20 20  tnum;.          
2c5c1 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74  pKeyInfo = sqlit
2c5c2 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70  e3IndexKeyinfo(p
2c5c3 50 61 72 73 65 2c 20 70 42 65 73 74 29 3b 0a 20  Parse, pBest);. 
2c5c4 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
2c5c5 20 20 2f 2a 20 4f 70 65 6e 20 61 20 72 65 61 64    /* Open a read
2c5c6 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 2c 20 65 78  -only cursor, ex
2c5c7 65 63 75 74 65 20 74 68 65 20 4f 50 5f 43 6f 75  ecute the OP_Cou
2c5c8 6e 74 2c 20 63 6c 6f 73 65 20 74 68 65 20 63 75  nt, close the cu
2c5c9 72 73 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20  rsor. */.       
2c5ca 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c5cb 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  p3(v, OP_OpenRea
2c5cc 64 2c 20 69 43 73 72 2c 20 69 52 6f 6f 74 2c 20  d, iCsr, iRoot, 
2c5cd 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 69 66  iDb);.        if
2c5ce 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20  ( pKeyInfo ){.  
2c5cf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2c5d0 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
2c5d1 31 2c 20 28 63 68 61 72 20 2a 29 70 4b 65 79 49  1, (char *)pKeyI
2c5d2 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  nfo, P4_KEYINFO_
2c5d3 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20  HANDOFF);.      
2c5d4 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
2c5d5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c5d6 20 4f 50 5f 43 6f 75 6e 74 2c 20 69 43 73 72 2c   OP_Count, iCsr,
2c5d7 20 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b   sAggInfo.aFunc[
2c5d8 30 5d 2e 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20  0].iMem);.      
2c5d9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c5da 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op1(v, OP_Close,
2c5db 20 69 43 73 72 29 3b 0a 20 20 20 20 20 20 7d 65   iCsr);.      }e
2c5dc 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  lse.#endif /* SQ
2c5dd 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43  LITE_OMIT_BTREEC
2c5de 4f 55 4e 54 20 2a 2f 0a 20 20 20 20 20 20 7b 0a  OUNT */.      {.
2c5df 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b          /* Check
2c5e0 20 69 66 20 74 68 65 20 71 75 65 72 79 20 69 73   if the query is
2c5e1 20 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 66   of one of the f
2c5e2 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a  ollowing forms:.
2c5e3 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
2c5e4 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d     **   SELECT m
2c5e5 69 6e 28 78 29 20 46 52 4f 4d 20 2e 2e 2e 0a 20  in(x) FROM .... 
2c5e6 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45         **   SELE
2c5e7 43 54 20 6d 61 78 28 78 29 20 46 52 4f 4d 20 2e  CT max(x) FROM .
2c5e8 2e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  ...        **.  
2c5e9 20 20 20 20 20 20 2a 2a 20 49 66 20 69 74 20 69        ** If it i
2c5ea 73 2c 20 74 68 65 6e 20 61 73 6b 20 74 68 65 20  s, then ask the 
2c5eb 63 6f 64 65 20 69 6e 20 77 68 65 72 65 2e 63 20  code in where.c 
2c5ec 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 73 6f  to attempt to so
2c5ed 72 74 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20  rt results.     
2c5ee 20 20 20 2a 2a 20 61 73 20 69 66 20 74 68 65 72     ** as if ther
2c5ef 65 20 77 61 73 20 61 6e 20 22 4f 52 44 45 52 20  e was an "ORDER 
2c5f0 4f 4e 20 78 22 20 6f 72 20 22 4f 52 44 45 52 20  ON x" or "ORDER 
2c5f1 4f 4e 20 78 20 44 45 53 43 22 20 63 6c 61 75 73  ON x DESC" claus
2c5f2 65 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 49  e. .        ** I
2c5f3 66 20 77 68 65 72 65 2e 63 20 69 73 20 61 62 6c  f where.c is abl
2c5f4 65 20 74 6f 20 70 72 6f 64 75 63 65 20 72 65 73  e to produce res
2c5f5 75 6c 74 73 20 73 6f 72 74 65 64 20 69 6e 20 74  ults sorted in t
2c5f6 68 69 73 20 6f 72 64 65 72 2c 20 74 68 65 6e 0a  his order, then.
2c5f7 20 20 20 20 20 20 20 20 2a 2a 20 61 64 64 20 76          ** add v
2c5f8 64 62 65 20 63 6f 64 65 20 74 6f 20 62 72 65 61  dbe code to brea
2c5f9 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 70 72 6f  k out of the pro
2c5fa 63 65 73 73 69 6e 67 20 6c 6f 6f 70 20 61 66 74  cessing loop aft
2c5fb 65 72 20 74 68 65 20 0a 20 20 20 20 20 20 20 20  er the .        
2c5fc 2a 2a 20 66 69 72 73 74 20 69 74 65 72 61 74 69  ** first iterati
2c5fd 6f 6e 20 28 73 69 6e 63 65 20 74 68 65 20 66 69  on (since the fi
2c5fe 72 73 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  rst iteration of
2c5ff 20 74 68 65 20 6c 6f 6f 70 20 69 73 20 0a 20 20   the loop is .  
2c600 20 20 20 20 20 20 2a 2a 20 67 75 61 72 61 6e 74        ** guarant
2c601 65 65 64 20 74 6f 20 6f 70 65 72 61 74 65 20 6f  eed to operate o
2c602 6e 20 74 68 65 20 72 6f 77 20 77 69 74 68 20 74  n the row with t
2c603 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 72 20 6d 61  he minimum or ma
2c604 78 69 6d 75 6d 20 0a 20 20 20 20 20 20 20 20 2a  ximum .        *
2c605 2a 20 76 61 6c 75 65 20 6f 66 20 78 2c 20 74 68  * value of x, th
2c606 65 20 6f 6e 6c 79 20 72 6f 77 20 72 65 71 75 69  e only row requi
2c607 72 65 64 29 2e 0a 20 20 20 20 20 20 20 20 2a 2a  red)..        **
2c608 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 73 70  .        ** A sp
2c609 65 63 69 61 6c 20 66 6c 61 67 20 6d 75 73 74 20  ecial flag must 
2c60a 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  be passed to sql
2c60b 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
2c60c 20 74 6f 20 73 6c 69 67 68 74 6c 79 0a 20 20 20   to slightly.   
2c60d 20 20 20 20 20 2a 2a 20 6d 6f 64 69 66 79 20 62       ** modify b
2c60e 65 68 61 76 69 6f 75 72 20 61 73 20 66 6f 6c 6c  ehaviour as foll
2c60f 6f 77 73 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ows:.        **.
2c610 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2b 20 49          **   + I
2c611 66 20 74 68 65 20 71 75 65 72 79 20 69 73 20 61  f the query is a
2c612 20 22 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 22   "SELECT min(x)"
2c613 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f 6f 70 20  , then the loop 
2c614 63 6f 64 65 64 20 62 79 0a 20 20 20 20 20 20 20  coded by.       
2c615 20 2a 2a 20 20 20 20 20 77 68 65 72 65 2e 63 20   **     where.c 
2c616 73 68 6f 75 6c 64 20 6e 6f 74 20 69 74 65 72 61  should not itera
2c617 74 65 20 6f 76 65 72 20 61 6e 79 20 76 61 6c 75  te over any valu
2c618 65 73 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 76  es with a NULL v
2c619 61 6c 75 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  alue.        ** 
2c61a 20 20 20 20 66 6f 72 20 78 2e 0a 20 20 20 20 20      for x..     
2c61b 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
2c61c 20 20 20 2b 20 54 68 65 20 6f 70 74 69 6d 69 7a     + The optimiz
2c61d 65 72 20 63 6f 64 65 20 69 6e 20 77 68 65 72 65  er code in where
2c61e 2e 63 20 28 74 68 65 20 74 68 69 6e 67 20 74 68  .c (the thing th
2c61f 61 74 20 64 65 63 69 64 65 73 20 77 68 69 63 68  at decides which
2c620 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20  .        **     
2c621 69 6e 64 65 78 20 6f 72 20 69 6e 64 69 63 65 73  index or indices
2c622 20 74 6f 20 75 73 65 29 20 73 68 6f 75 6c 64 20   to use) should 
2c623 70 6c 61 63 65 20 61 20 64 69 66 66 65 72 65 6e  place a differen
2c624 74 20 70 72 69 6f 72 69 74 79 20 6f 6e 20 0a 20  t priority on . 
2c625 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 73 61         **     sa
2c626 74 69 73 66 79 69 6e 67 20 74 68 65 20 27 4f 52  tisfying the 'OR
2c627 44 45 52 20 42 59 27 20 63 6c 61 75 73 65 20 74  DER BY' clause t
2c628 68 61 6e 20 69 74 20 64 6f 65 73 20 69 6e 20 6f  han it does in o
2c629 74 68 65 72 20 63 61 73 65 73 2e 0a 20 20 20 20  ther cases..    
2c62a 20 20 20 20 2a 2a 20 20 20 20 20 52 65 66 65 72      **     Refer
2c62b 20 74 6f 20 63 6f 64 65 20 61 6e 64 20 63 6f 6d   to code and com
2c62c 6d 65 6e 74 73 20 69 6e 20 77 68 65 72 65 2e 63  ments in where.c
2c62d 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 20 20   for details..  
2c62e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2c62f 20 45 78 70 72 4c 69 73 74 20 2a 70 4d 69 6e 4d   ExprList *pMinM
2c630 61 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ax = 0;.        
2c631 75 38 20 66 6c 61 67 20 3d 20 6d 69 6e 4d 61 78  u8 flag = minMax
2c632 51 75 65 72 79 28 70 29 3b 0a 20 20 20 20 20 20  Query(p);.      
2c633 20 20 69 66 28 20 66 6c 61 67 20 29 7b 0a 20 20    if( flag ){.  
2c634 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2c635 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2c636 28 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d  (p->pEList->a[0]
2c637 2e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  .pExpr, EP_xIsSe
2c638 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 20  lect) );.       
2c639 20 20 20 70 4d 69 6e 4d 61 78 20 3d 20 73 71 6c     pMinMax = sql
2c63a 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
2c63b 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61  db, p->pEList->a
2c63c 5b 30 5d 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69  [0].pExpr->x.pLi
2c63d 73 74 2c 30 29 3b 0a 20 20 20 20 20 20 20 20 20  st,0);.         
2c63e 20 70 44 65 6c 20 3d 20 70 4d 69 6e 4d 61 78 3b   pDel = pMinMax;
2c63f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
2c640 4d 69 6e 4d 61 78 20 26 26 20 21 64 62 2d 3e 6d  MinMax && !db->m
2c641 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2c642 20 20 20 20 20 20 20 20 20 20 20 70 4d 69 6e 4d             pMinM
2c643 61 78 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64  ax->a[0].sortOrd
2c644 65 72 20 3d 20 66 6c 61 67 21 3d 57 48 45 52 45  er = flag!=WHERE
2c645 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20 3f 31 3a  _ORDERBY_MIN ?1:
2c646 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  0;.            p
2c647 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e 70 45 78  MinMax->a[0].pEx
2c648 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55  pr->op = TK_COLU
2c649 4d 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  MN;.          }.
2c64a 20 20 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20          }.  .   
2c64b 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73       /* This cas
2c64c 65 20 72 75 6e 73 20 69 66 20 74 68 65 20 61 67  e runs if the ag
2c64d 67 72 65 67 61 74 65 20 68 61 73 20 6e 6f 20 47  gregate has no G
2c64e 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 20  ROUP BY clause. 
2c64f 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   The.        ** 
2c650 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 6d 75  processing is mu
2c651 63 68 20 73 69 6d 70 6c 65 72 20 73 69 6e 63 65  ch simpler since
2c652 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61   there is only a
2c653 20 73 69 6e 67 6c 65 20 72 6f 77 0a 20 20 20 20   single row.    
2c654 20 20 20 20 2a 2a 20 6f 66 20 6f 75 74 70 75 74      ** of output
2c655 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2c656 20 20 20 20 20 72 65 73 65 74 41 63 63 75 6d 75       resetAccumu
2c657 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73  lator(pParse, &s
2c658 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  AggInfo);.      
2c659 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74    pWInfo = sqlit
2c65a 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61  e3WhereBegin(pPa
2c65b 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
2c65c 57 68 65 72 65 2c 20 26 70 4d 69 6e 4d 61 78 2c  Where, &pMinMax,
2c65d 20 66 6c 61 67 29 3b 0a 20 20 20 20 20 20 20 20   flag);.        
2c65e 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 7b  if( pWInfo==0 ){
2c65f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2c660 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
2c661 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20  (db, pDel);.    
2c662 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63        goto selec
2c663 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d  t_end;.        }
2c664 0a 20 20 20 20 20 20 20 20 75 70 64 61 74 65 41  .        updateA
2c665 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73  ccumulator(pPars
2c666 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20  e, &sAggInfo);. 
2c667 20 20 20 20 20 20 20 69 66 28 20 21 70 4d 69 6e         if( !pMin
2c668 4d 61 78 20 26 26 20 66 6c 61 67 20 29 7b 0a 20  Max && flag ){. 
2c669 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2c66a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c66b 5f 47 6f 74 6f 2c 20 30 2c 20 70 57 49 6e 66 6f  _Goto, 0, pWInfo
2c66c 2d 3e 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  ->iBreak);.     
2c66d 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2c66e 28 28 76 2c 20 22 25 73 28 29 20 62 79 20 69 6e  ((v, "%s() by in
2c66f 64 65 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20  dex",.          
2c670 20 20 20 20 20 20 28 66 6c 61 67 3d 3d 57 48 45        (flag==WHE
2c671 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3f 22  RE_ORDERBY_MIN?"
2c672 6d 69 6e 22 3a 22 6d 61 78 22 29 29 29 3b 0a 20  min":"max")));. 
2c673 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2c674 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
2c675 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  (pWInfo);.      
2c676 20 20 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e    finalizeAggFun
2c677 63 74 69 6f 6e 73 28 70 50 61 72 73 65 2c 20 26  ctions(pParse, &
2c678 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20  sAggInfo);.     
2c679 20 7d 0a 0a 20 20 20 20 20 20 70 4f 72 64 65 72   }..      pOrder
2c67a 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  By = 0;.      sq
2c67b 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
2c67c 28 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e 67  (pParse, pHaving
2c67d 2c 20 61 64 64 72 45 6e 64 2c 20 53 51 4c 49 54  , addrEnd, SQLIT
2c67e 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
2c67f 20 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72       selectInner
2c680 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20  Loop(pParse, p, 
2c681 70 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c  p->pEList, 0, 0,
2c682 20 30 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20   0, -1, .       
2c683 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2c684 44 65 73 74 2c 20 61 64 64 72 45 6e 64 2c 20 61  Dest, addrEnd, a
2c685 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 73  ddrEnd);.      s
2c686 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
2c687 6c 65 74 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a  lete(db, pDel);.
2c688 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2c689 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2c68a 6c 28 76 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20  l(v, addrEnd);. 
2c68b 20 20 20 0a 20 20 7d 20 2f 2a 20 65 6e 64 69 66     .  } /* endif
2c68c 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79   aggregate query
2c68d 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   */..  /* If the
2c68e 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  re is an ORDER B
2c68f 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 77  Y clause, then w
2c690 65 20 6e 65 65 64 20 74 6f 20 73 6f 72 74 20 74  e need to sort t
2c691 68 65 20 72 65 73 75 6c 74 73 0a 20 20 2a 2a 20  he results.  ** 
2c692 61 6e 64 20 73 65 6e 64 20 74 68 65 6d 20 74 6f  and send them to
2c693 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e   the callback on
2c694 65 20 62 79 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20  e by one..  */. 
2c695 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
2c696 0a 20 20 20 20 67 65 6e 65 72 61 74 65 53 6f 72  .    generateSor
2c697 74 54 61 69 6c 28 70 50 61 72 73 65 2c 20 70 2c  tTail(pParse, p,
2c698 20 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   v, pEList->nExp
2c699 72 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a  r, pDest);.  }..
2c69a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
2c69b 6f 20 73 6b 69 70 20 74 68 69 73 20 71 75 65 72  o skip this quer
2c69c 79 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  y.  */.  sqlite3
2c69d 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2c69e 28 76 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a  (v, iEnd);..  /*
2c69f 20 54 68 65 20 53 45 4c 45 43 54 20 77 61 73 20   The SELECT was 
2c6a0 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 64  successfully cod
2c6a1 65 64 2e 20 20 20 53 65 74 20 74 68 65 20 72 65  ed.   Set the re
2c6a2 74 75 72 6e 20 63 6f 64 65 20 74 6f 20 30 0a 20  turn code to 0. 
2c6a3 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20   ** to indicate 
2c6a4 6e 6f 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a  no errors..  */.
2c6a5 20 20 72 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20    rc = 0;..  /* 
2c6a6 43 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 20 74 6f  Control jumps to
2c6a7 20 68 65 72 65 20 69 66 20 61 6e 20 65 72 72 6f   here if an erro
2c6a8 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
2c6a9 20 61 62 6f 76 65 2c 20 6f 72 20 75 70 6f 6e 0a   above, or upon.
2c6aa 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20    ** successful 
2c6ab 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 53 45  coding of the SE
2c6ac 4c 45 43 54 2e 0a 20 20 2a 2f 0a 73 65 6c 65 63  LECT..  */.selec
2c6ad 74 5f 65 6e 64 3a 0a 0a 20 20 2f 2a 20 49 64 65  t_end:..  /* Ide
2c6ae 6e 74 69 66 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ntify column nam
2c6af 65 73 20 69 66 20 72 65 73 75 6c 74 73 20 6f 66  es if results of
2c6b0 20 74 68 65 20 53 45 4c 45 43 54 20 61 72 65 20   the SELECT are 
2c6b1 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 20 20  to be output..  
2c6b2 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
2c6b3 49 54 45 5f 4f 4b 20 26 26 20 70 44 65 73 74 2d  ITE_OK && pDest-
2c6b4 3e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70  >eDest==SRT_Outp
2c6b5 75 74 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61  ut ){.    genera
2c6b6 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  teColumnNames(pP
2c6b7 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
2c6b8 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 0a 20 20  pEList);.  }..  
2c6b9 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2c6ba 2c 20 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 29  , sAggInfo.aCol)
2c6bb 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2c6bc 65 28 64 62 2c 20 73 41 67 67 49 6e 66 6f 2e 61  e(db, sAggInfo.a
2c6bd 46 75 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  Func);.  return 
2c6be 72 63 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e  rc;.}..#if defin
2c6bf 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
2c6c0 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./*.************
2c6c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c6c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c6c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c6c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c6c5 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ***.** The follo
2c6c6 77 69 6e 67 20 63 6f 64 65 20 69 73 20 75 73 65  wing code is use
2c6c7 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e  d for testing an
2c6c8 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79  d debugging only
2c6c9 2e 20 20 54 68 65 20 63 6f 64 65 0a 2a 2a 20 74  .  The code.** t
2c6ca 68 61 74 20 66 6f 6c 6c 6f 77 73 20 64 6f 65 73  hat follows does
2c6cb 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 6e   not appear in n
2c6cc 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2e 0a 2a 2a  ormal builds..**
2c6cd 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
2c6ce 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 70  es are used to p
2c6cf 72 69 6e 74 20 6f 75 74 20 74 68 65 20 63 6f 6e  rint out the con
2c6d0 74 65 6e 74 20 6f 66 20 61 6c 6c 20 6f 72 20 70  tent of all or p
2c6d1 61 72 74 20 6f 66 20 61 20 0a 2a 2a 20 70 61 72  art of a .** par
2c6d2 73 65 20 73 74 72 75 63 74 75 72 65 73 20 73 75  se structures su
2c6d3 63 68 20 61 73 20 53 65 6c 65 63 74 20 6f 72 20  ch as Select or 
2c6d4 45 78 70 72 2e 20 20 53 75 63 68 20 70 72 69 6e  Expr.  Such prin
2c6d5 74 6f 75 74 73 20 61 72 65 20 75 73 65 66 75 6c  touts are useful
2c6d6 0a 2a 2a 20 66 6f 72 20 68 65 6c 70 69 6e 67 20  .** for helping 
2c6d7 74 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 77 68  to understand wh
2c6d8 61 74 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20  at is happening 
2c6d9 69 6e 73 69 64 65 20 74 68 65 20 63 6f 64 65 20  inside the code 
2c6da 67 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 64 75 72  generator.** dur
2c6db 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f  ing the executio
2c6dc 6e 20 6f 66 20 63 6f 6d 70 6c 65 78 20 53 45 4c  n of complex SEL
2c6dd 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ECT statements..
2c6de 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
2c6df 69 6e 65 20 61 72 65 20 6e 6f 74 20 63 61 6c 6c  ine are not call
2c6e0 65 64 20 61 6e 79 77 68 65 72 65 20 66 72 6f 6d  ed anywhere from
2c6e1 20 77 69 74 68 69 6e 20 74 68 65 20 6e 6f 72 6d   within the norm
2c6e2 61 6c 0a 2a 2a 20 63 6f 64 65 20 62 61 73 65 2e  al.** code base.
2c6e3 20 20 54 68 65 6e 20 61 72 65 20 69 6e 74 65 6e    Then are inten
2c6e4 64 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64  ded to be called
2c6e5 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
2c6e6 20 64 65 62 75 67 67 65 72 0a 2a 2a 20 6f 72 20   debugger.** or 
2c6e7 66 72 6f 6d 20 74 65 6d 70 6f 72 61 72 79 20 22  from temporary "
2c6e8 70 72 69 6e 74 66 22 20 73 74 61 74 65 6d 65 6e  printf" statemen
2c6e9 74 73 20 69 6e 73 65 72 74 65 64 20 66 6f 72 20  ts inserted for 
2c6ea 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2f 0a 53 51  debugging..*/.SQ
2c6eb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2c6ec 64 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78  d sqlite3PrintEx
2c6ed 70 72 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69  pr(Expr *p){.  i
2c6ee 66 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  f( !ExprHasPrope
2c6ef 72 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c  rty(p, EP_IntVal
2c6f0 75 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b  ue) && p->u.zTok
2c6f1 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  en ){.    sqlite
2c6f2 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 28 25  3DebugPrintf("(%
2c6f3 73 22 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29  s", p->u.zToken)
2c6f4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
2c6f5 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2c6f6 66 28 22 28 25 64 22 2c 20 70 2d 3e 6f 70 29 3b  f("(%d", p->op);
2c6f7 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4c  .  }.  if( p->pL
2c6f8 65 66 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  eft ){.    sqlit
2c6f9 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
2c6fa 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ");.    sqlite3P
2c6fb 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 4c 65 66  rintExpr(p->pLef
2c6fc 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  t);.  }.  if( p-
2c6fd 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 73  >pRight ){.    s
2c6fe 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2c6ff 66 28 22 20 22 29 3b 0a 20 20 20 20 73 71 6c 69  f(" ");.    sqli
2c700 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e  te3PrintExpr(p->
2c701 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 73  pRight);.  }.  s
2c702 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2c703 66 28 22 29 22 29 3b 0a 7d 0a 53 51 4c 49 54 45  f(")");.}.SQLITE
2c704 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2c705 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 4c 69  lite3PrintExprLi
2c706 73 74 28 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  st(ExprList *pLi
2c707 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  st){.  int i;.  
2c708 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
2c709 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
2c70a 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45     sqlite3PrintE
2c70b 78 70 72 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  xpr(pList->a[i].
2c70c 70 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28 20  pExpr);.    if( 
2c70d 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31  i<pList->nExpr-1
2c70e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2c70f 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 2c 20  3DebugPrintf(", 
2c710 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ");.    }.  }.}.
2c711 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2c712 6f 69 64 20 73 71 6c 69 74 65 33 50 72 69 6e 74  oid sqlite3Print
2c713 53 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a 70  Select(Select *p
2c714 2c 20 69 6e 74 20 69 6e 64 65 6e 74 29 7b 0a 20  , int indent){. 
2c715 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2c716 6e 74 66 28 22 25 2a 73 53 45 4c 45 43 54 28 25  ntf("%*sSELECT(%
2c717 70 29 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22  p) ", indent, ""
2c718 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50  , p);.  sqlite3P
2c719 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e  rintExprList(p->
2c71a 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74  pEList);.  sqlit
2c71b 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c  e3DebugPrintf("\
2c71c 6e 22 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 53  n");.  if( p->pS
2c71d 72 63 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  rc ){.    char *
2c71e 7a 50 72 65 66 69 78 3b 0a 20 20 20 20 69 6e 74  zPrefix;.    int
2c71f 20 69 3b 0a 20 20 20 20 7a 50 72 65 66 69 78 20   i;.    zPrefix 
2c720 3d 20 22 46 52 4f 4d 22 3b 0a 20 20 20 20 66 6f  = "FROM";.    fo
2c721 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63  r(i=0; i<p->pSrc
2c722 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
2c723 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
2c724 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
2c725 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b   &p->pSrc->a[i];
2c726 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65  .      sqlite3De
2c727 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 22  bugPrintf("%*s "
2c728 2c 20 69 6e 64 65 6e 74 2b 36 2c 20 7a 50 72 65  , indent+6, zPre
2c729 66 69 78 29 3b 0a 20 20 20 20 20 20 7a 50 72 65  fix);.      zPre
2c72a 66 69 78 20 3d 20 22 22 3b 0a 20 20 20 20 20 20  fix = "";.      
2c72b 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65  if( pItem->pSele
2c72c 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ct ){.        sq
2c72d 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2c72e 28 22 28 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20  ("(\n");.       
2c72f 20 73 71 6c 69 74 65 33 50 72 69 6e 74 53 65 6c   sqlite3PrintSel
2c730 65 63 74 28 70 49 74 65 6d 2d 3e 70 53 65 6c 65  ect(pItem->pSele
2c731 63 74 2c 20 69 6e 64 65 6e 74 2b 31 30 29 3b 0a  ct, indent+10);.
2c732 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2c733 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 29  ebugPrintf("%*s)
2c734 22 2c 20 69 6e 64 65 6e 74 2b 38 2c 20 22 22 29  ", indent+8, "")
2c735 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
2c736 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29  ( pItem->zName )
2c737 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2c738 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 73  3DebugPrintf("%s
2c739 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  ", pItem->zName)
2c73a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c73b 69 66 28 20 70 49 74 65 6d 2d 3e 70 54 61 62 20  if( pItem->pTab 
2c73c 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2c73d 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 28  e3DebugPrintf("(
2c73e 74 61 62 6c 65 3a 20 25 73 29 22 2c 20 70 49 74  table: %s)", pIt
2c73f 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  em->pTab->zName)
2c740 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c741 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  if( pItem->zAlia
2c742 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  s ){.        sql
2c743 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2c744 22 20 41 53 20 25 73 22 2c 20 70 49 74 65 6d 2d  " AS %s", pItem-
2c745 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 20 20  >zAlias);.      
2c746 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3c 70 2d  }.      if( i<p-
2c747 3e 70 53 72 63 2d 3e 6e 53 72 63 2d 31 20 29 7b  >pSrc->nSrc-1 ){
2c748 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c749 44 65 62 75 67 50 72 69 6e 74 66 28 22 2c 22 29  DebugPrintf(",")
2c74a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c74b 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2c74c 74 66 28 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a  tf("\n");.    }.
2c74d 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 57 68    }.  if( p->pWh
2c74e 65 72 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ere ){.    sqlit
2c74f 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
2c750 2a 73 20 57 48 45 52 45 20 22 2c 20 69 6e 64 65  *s WHERE ", inde
2c751 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c  nt, "");.    sql
2c752 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d  ite3PrintExpr(p-
2c753 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71  >pWhere);.    sq
2c754 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2c755 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66  ("\n");.  }.  if
2c756 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b  ( p->pGroupBy ){
2c757 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
2c758 67 50 72 69 6e 74 66 28 22 25 2a 73 20 47 52 4f  gPrintf("%*s GRO
2c759 55 50 20 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c  UP BY ", indent,
2c75a 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   "");.    sqlite
2c75b 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70  3PrintExprList(p
2c75c 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20  ->pGroupBy);.   
2c75d 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2c75e 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20  ntf("\n");.  }. 
2c75f 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 20   if( p->pHaving 
2c760 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2c761 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 48  bugPrintf("%*s H
2c762 41 56 49 4e 47 20 22 2c 20 69 6e 64 65 6e 74 2c  AVING ", indent,
2c763 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   "");.    sqlite
2c764 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 48  3PrintExpr(p->pH
2c765 61 76 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69  aving);.    sqli
2c766 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2c767 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  \n");.  }.  if( 
2c768 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  p->pOrderBy ){. 
2c769 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2c76a 72 69 6e 74 66 28 22 25 2a 73 20 4f 52 44 45 52  rintf("%*s ORDER
2c76b 20 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22   BY ", indent, "
2c76c 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ");.    sqlite3P
2c76d 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e  rintExprList(p->
2c76e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 73  pOrderBy);.    s
2c76f 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2c770 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 7d 0a 2f  f("\n");.  }.}./
2c771 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 73 74 72  * End of the str
2c772 75 63 74 75 72 65 20 64 65 62 75 67 20 70 72 69  ucture debug pri
2c773 6e 74 69 6e 67 20 63 6f 64 65 0a 2a 2a 2a 2a 2a  nting code.*****
2c774 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c775 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c776 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c777 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c778 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66  ********/.#endif
2c779 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49   /* defined(SQLI
2c77a 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69  TE_TEST) || defi
2c77b 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
2c77c 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
2c77d 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 65 6c  ***** End of sel
2c77e 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ect.c **********
2c77f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c781 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2c782 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2c783 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   table.c *******
2c784 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c785 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c786 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
2c787 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
2c788 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2c789 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2c78a 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2c78b 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2c78c 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2c78d 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2c78e 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2c78f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2c790 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2c791 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2c792 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2c793 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2c794 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2c795 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
2c796 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2c797 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2c798 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2c799 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2c79a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c79b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c79c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c79d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
2c79e 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
2c79f 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67  ns the sqlite3_g
2c7a0 65 74 5f 74 61 62 6c 65 28 29 20 61 6e 64 20 73  et_table() and s
2c7a1 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
2c7a2 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  e().** interface
2c7a3 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 73   routines.  Thes
2c7a4 65 20 61 72 65 20 6a 75 73 74 20 77 72 61 70 70  e are just wrapp
2c7a5 65 72 73 20 61 72 6f 75 6e 64 20 74 68 65 20 6d  ers around the m
2c7a6 61 69 6e 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ain.** interface
2c7a7 20 72 6f 75 74 69 6e 65 20 6f 66 20 73 71 6c 69   routine of sqli
2c7a8 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a 0a 2a  te3_exec()..**.*
2c7a9 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
2c7aa 20 61 72 65 20 69 6e 20 61 20 73 65 70 61 72 61   are in a separa
2c7ab 74 65 20 66 69 6c 65 73 20 73 6f 20 74 68 61 74  te files so that
2c7ac 20 74 68 65 79 20 77 69 6c 6c 20 6e 6f 74 20 62   they will not b
2c7ad 65 20 6c 69 6e 6b 65 64 0a 2a 2a 20 69 66 20 74  e linked.** if t
2c7ae 68 65 79 20 61 72 65 20 6e 6f 74 20 75 73 65 64  hey are not used
2c7af 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74 61 62  ..**.** $Id: tab
2c7b0 6c 65 2e 63 2c 76 20 31 2e 34 30 20 32 30 30 39  le.c,v 1.40 2009
2c7b1 2f 30 34 2f 31 30 20 31 34 3a 32 38 3a 30 30 20  /04/10 14:28:00 
2c7b2 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69  drh Exp $.*/..#i
2c7b3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2c7b4 54 5f 47 45 54 5f 54 41 42 4c 45 0a 0a 2f 2a 0a  T_GET_TABLE../*.
2c7b5 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
2c7b6 65 20 69 73 20 75 73 65 64 20 74 6f 20 70 61 73  e is used to pas
2c7b7 73 20 64 61 74 61 20 66 72 6f 6d 20 73 71 6c 69  s data from sqli
2c7b8 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20  te3_get_table() 
2c7b9 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68  through.** to th
2c7ba 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
2c7bb 69 6f 6e 20 69 73 20 75 73 65 73 20 74 6f 20 62  ion is uses to b
2c7bc 75 69 6c 64 20 74 68 65 20 72 65 73 75 6c 74 2e  uild the result.
2c7bd 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
2c7be 63 74 20 54 61 62 52 65 73 75 6c 74 20 7b 0a 20  ct TabResult {. 
2c7bf 20 63 68 61 72 20 2a 2a 61 7a 52 65 73 75 6c 74   char **azResult
2c7c0 3b 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74  ;   /* Accumulat
2c7c1 65 64 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 63  ed output */.  c
2c7c2 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20  har *zErrMsg;   
2c7c3 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61    /* Error messa
2c7c4 67 65 20 74 65 78 74 2c 20 69 66 20 61 6e 20 65  ge text, if an e
2c7c5 72 72 6f 72 20 6f 63 63 75 72 73 20 2a 2f 0a 20  rror occurs */. 
2c7c6 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20   int nAlloc;    
2c7c7 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20 61 6c 6c      /* Slots all
2c7c8 6f 63 61 74 65 64 20 66 6f 72 20 61 7a 52 65 73  ocated for azRes
2c7c9 75 6c 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  ult[] */.  int n
2c7ca 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Row;          /*
2c7cb 20 4e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   Number of rows 
2c7cc 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f  in the result */
2c7cd 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20  .  int nColumn; 
2c7ce 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2c7cf 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
2c7d0 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 69 6e  e result */.  in
2c7d1 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20  t nData;        
2c7d2 20 2f 2a 20 53 6c 6f 74 73 20 75 73 65 64 20 69   /* Slots used i
2c7d3 6e 20 61 7a 52 65 73 75 6c 74 5b 5d 2e 20 20 28  n azResult[].  (
2c7d4 6e 52 6f 77 2b 31 29 2a 6e 43 6f 6c 75 6d 6e 20  nRow+1)*nColumn 
2c7d5 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
2c7d6 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
2c7d7 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69  n code from sqli
2c7d8 74 65 33 5f 65 78 65 63 28 29 20 2a 2f 0a 7d 20  te3_exec() */.} 
2c7d9 54 61 62 52 65 73 75 6c 74 3b 0a 0a 2f 2a 0a 2a  TabResult;../*.*
2c7da 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2c7db 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f  s called once fo
2c7dc 72 20 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68  r each row in th
2c7dd 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 2e 20  e result table. 
2c7de 20 49 74 73 20 6a 6f 62 0a 2a 2a 20 69 73 20 74   Its job.** is t
2c7df 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 54 61  o fill in the Ta
2c7e0 62 52 65 73 75 6c 74 20 73 74 72 75 63 74 75 72  bResult structur
2c7e1 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2c  e appropriately,
2c7e2 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 65 77 0a   allocating new.
2c7e3 2a 2a 20 6d 65 6d 6f 72 79 20 61 73 20 6e 65 63  ** memory as nec
2c7e4 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  essary..*/.stati
2c7e5 63 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65  c int sqlite3_ge
2c7e6 74 5f 74 61 62 6c 65 5f 63 62 28 76 6f 69 64 20  t_table_cb(void 
2c7e7 2a 70 41 72 67 2c 20 69 6e 74 20 6e 43 6f 6c 2c  *pArg, int nCol,
2c7e8 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68   char **argv, ch
2c7e9 61 72 20 2a 2a 63 6f 6c 76 29 7b 0a 20 20 54 61  ar **colv){.  Ta
2c7ea 62 52 65 73 75 6c 74 20 2a 70 20 3d 20 28 54 61  bResult *p = (Ta
2c7eb 62 52 65 73 75 6c 74 2a 29 70 41 72 67 3b 20 20  bResult*)pArg;  
2c7ec 2f 2a 20 52 65 73 75 6c 74 20 61 63 63 75 6d 75  /* Result accumu
2c7ed 6c 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  lator */.  int n
2c7ee 65 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  eed;            
2c7ef 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c7f0 53 6c 6f 74 73 20 6e 65 65 64 65 64 20 69 6e 20  Slots needed in 
2c7f1 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 5d 20 2a 2f  p->azResult[] */
2c7f2 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
2c7f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c7f4 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2c7f5 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a  nter */.  char *
2c7f6 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z;              
2c7f7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2c7f8 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f   single column o
2c7f9 66 20 72 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 2f  f result */..  /
2c7fa 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72  * Make sure ther
2c7fb 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63  e is enough spac
2c7fc 65 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74  e in p->azResult
2c7fd 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68   to hold everyth
2c7fe 69 6e 67 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64  ing.  ** we need
2c7ff 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 66 72 6f   to remember fro
2c800 6d 20 74 68 69 73 20 69 6e 76 6f 63 61 74 69 6f  m this invocatio
2c801 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63  n of the callbac
2c802 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  k..  */.  if( p-
2c803 3e 6e 52 6f 77 3d 3d 30 20 26 26 20 61 72 67 76  >nRow==0 && argv
2c804 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 65 64 20  !=0 ){.    need 
2c805 3d 20 6e 43 6f 6c 2a 32 3b 0a 20 20 7d 65 6c 73  = nCol*2;.  }els
2c806 65 7b 0a 20 20 20 20 6e 65 65 64 20 3d 20 6e 43  e{.    need = nC
2c807 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ol;.  }.  if( p-
2c808 3e 6e 44 61 74 61 20 2b 20 6e 65 65 64 20 3e 20  >nData + need > 
2c809 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20  p->nAlloc ){.   
2c80a 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b 0a 20   char **azNew;. 
2c80b 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70     p->nAlloc = p
2c80c 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 6e 65 65  ->nAlloc*2 + nee
2c80d 64 3b 0a 20 20 20 20 61 7a 4e 65 77 20 3d 20 73  d;.    azNew = s
2c80e 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 20  qlite3_realloc( 
2c80f 70 2d 3e 61 7a 52 65 73 75 6c 74 2c 20 73 69 7a  p->azResult, siz
2c810 65 6f 66 28 63 68 61 72 2a 29 2a 70 2d 3e 6e 41  eof(char*)*p->nA
2c811 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 69 66 28 20  lloc );.    if( 
2c812 61 7a 4e 65 77 3d 3d 30 20 29 20 67 6f 74 6f 20  azNew==0 ) goto 
2c813 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20  malloc_failed;. 
2c814 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 20 3d     p->azResult =
2c815 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 2f   azNew;.  }..  /
2c816 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65  * If this is the
2c817 20 66 69 72 73 74 20 72 6f 77 2c 20 74 68 65 6e   first row, then
2c818 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 78 74   generate an ext
2c819 72 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e  ra row containin
2c81a 67 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 73  g.  ** the names
2c81b 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 2e   of all columns.
2c81c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e  .  */.  if( p->n
2c81d 52 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d  Row==0 ){.    p-
2c81e 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c 3b  >nColumn = nCol;
2c81f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2c820 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
2c821 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70    z = sqlite3_mp
2c822 72 69 6e 74 66 28 22 25 73 22 2c 20 63 6f 6c 76  rintf("%s", colv
2c823 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [i]);.      if( 
2c824 7a 3d 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c  z==0 ) goto mall
2c825 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 20  oc_failed;.     
2c826 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e   p->azResult[p->
2c827 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a 20 20  nData++] = z;.  
2c828 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
2c829 70 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 6e 43 6f 6c  p->nColumn!=nCol
2c82a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2c82b 66 72 65 65 28 70 2d 3e 7a 45 72 72 4d 73 67 29  free(p->zErrMsg)
2c82c 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67  ;.    p->zErrMsg
2c82d 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
2c82e 74 66 28 0a 20 20 20 20 20 20 20 22 73 71 6c 69  tf(.       "sqli
2c82f 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20  te3_get_table() 
2c830 63 61 6c 6c 65 64 20 77 69 74 68 20 74 77 6f 20  called with two 
2c831 6f 72 20 6d 6f 72 65 20 69 6e 63 6f 6d 70 61 74  or more incompat
2c832 69 62 6c 65 20 71 75 65 72 69 65 73 22 0a 20 20  ible queries".  
2c833 20 20 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d    );.    p->rc =
2c834 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2c835 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
2c836 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72  ..  /* Copy over
2c837 20 74 68 65 20 72 6f 77 20 64 61 74 61 0a 20 20   the row data.  
2c838 2a 2f 0a 20 20 69 66 28 20 61 72 67 76 21 3d 30  */.  if( argv!=0
2c839 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
2c83a 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
2c83b 20 20 20 20 20 69 66 28 20 61 72 67 76 5b 69 5d       if( argv[i]
2c83c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ==0 ){.        z
2c83d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 0;.      }els
2c83e 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  e{.        int n
2c83f 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2c840 33 30 28 61 72 67 76 5b 69 5d 29 2b 31 3b 0a 20  30(argv[i])+1;. 
2c841 20 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74         z = sqlit
2c842 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a  e3_malloc( n );.
2c843 20 20 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30          if( z==0
2c844 20 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66   ) goto malloc_f
2c845 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 6d  ailed;.        m
2c846 65 6d 63 70 79 28 7a 2c 20 61 72 67 76 5b 69 5d  emcpy(z, argv[i]
2c847 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , n);.      }.  
2c848 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b      p->azResult[
2c849 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b  p->nData++] = z;
2c84a 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52  .    }.    p->nR
2c84b 6f 77 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ow++;.  }.  retu
2c84c 72 6e 20 30 3b 0a 0a 6d 61 6c 6c 6f 63 5f 66 61  rn 0;..malloc_fa
2c84d 69 6c 65 64 3a 0a 20 20 70 2d 3e 72 63 20 3d 20  iled:.  p->rc = 
2c84e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2c84f 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a  return 1;.}../*.
2c850 2a 2a 20 51 75 65 72 79 20 74 68 65 20 64 61 74  ** Query the dat
2c851 61 62 61 73 65 2e 20 20 42 75 74 20 69 6e 73 74  abase.  But inst
2c852 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20  ead of invoking 
2c853 61 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65  a callback for e
2c854 61 63 68 20 72 6f 77 2c 0a 2a 2a 20 6d 61 6c 6c  ach row,.** mall
2c855 6f 63 28 29 20 66 6f 72 20 73 70 61 63 65 20 74  oc() for space t
2c856 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c  o hold the resul
2c857 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  t and return the
2c858 20 65 6e 74 69 72 65 20 72 65 73 75 6c 74 73 0a   entire results.
2c859 2a 2a 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75  ** at the conclu
2c85a 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c  sion of the call
2c85b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75  ..**.** The resu
2c85c 6c 74 20 74 68 61 74 20 69 73 20 77 72 69 74 74  lt that is writt
2c85d 65 6e 20 74 6f 20 2a 2a 2a 70 61 7a 52 65 73 75  en to ***pazResu
2c85e 6c 74 20 69 73 20 68 65 6c 64 20 69 6e 20 6d 65  lt is held in me
2c85f 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  mory obtained.**
2c860 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20   from malloc(). 
2c861 20 42 75 74 20 74 68 65 20 63 61 6c 6c 65 72 20   But the caller 
2c862 63 61 6e 6e 6f 74 20 66 72 65 65 20 74 68 69 73  cannot free this
2c863 20 6d 65 6d 6f 72 79 20 64 69 72 65 63 74 6c 79   memory directly
2c864 2e 20 20 0a 2a 2a 20 49 6e 73 74 65 61 64 2c 20  .  .** Instead, 
2c865 74 68 65 20 65 6e 74 69 72 65 20 74 61 62 6c 65  the entire table
2c866 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65   should be passe
2c867 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65  d to sqlite3_fre
2c868 65 5f 74 61 62 6c 65 28 29 20 77 68 65 6e 0a 2a  e_table() when.*
2c869 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72  * the calling pr
2c86a 6f 63 65 64 75 72 65 20 69 73 20 66 69 6e 69 73  ocedure is finis
2c86b 68 65 64 20 75 73 69 6e 67 20 69 74 2e 0a 2a 2f  hed using it..*/
2c86c 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2c86d 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
2c86e 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
2c86f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2c870 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2c871 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 53 51   on which the SQ
2c872 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 20 20  L executes */.  
2c873 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
2c874 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
2c875 68 65 20 53 51 4c 20 74 6f 20 62 65 20 65 78 65  he SQL to be exe
2c876 63 75 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  cuted */.  char 
2c877 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20  ***pazResult,   
2c878 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
2c879 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
2c87a 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a   here */.  int *
2c87b 70 6e 52 6f 77 2c 20 20 20 20 20 20 20 20 20 20  pnRow,          
2c87c 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
2c87d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
2c87e 77 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ws in the result
2c87f 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a   here */.  int *
2c880 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20  pnColumn,       
2c881 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
2c882 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
2c883 6c 75 6d 6e 73 20 6f 66 20 72 65 73 75 6c 74 20  lumns of result 
2c884 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  here */.  char *
2c885 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20  *pzErrMsg       
2c886 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65        /* Write e
2c887 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65  rror messages he
2c888 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  re */.){.  int r
2c889 63 3b 0a 20 20 54 61 62 52 65 73 75 6c 74 20 72  c;.  TabResult r
2c88a 65 73 3b 0a 0a 20 20 2a 70 61 7a 52 65 73 75 6c  es;..  *pazResul
2c88b 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 6e 43  t = 0;.  if( pnC
2c88c 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d  olumn ) *pnColum
2c88d 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 6e 52  n = 0;.  if( pnR
2c88e 6f 77 20 29 20 2a 70 6e 52 6f 77 20 3d 20 30 3b  ow ) *pnRow = 0;
2c88f 0a 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20  .  if( pzErrMsg 
2c890 29 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ) *pzErrMsg = 0;
2c891 0a 20 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 3d  .  res.zErrMsg =
2c892 20 30 3b 0a 20 20 72 65 73 2e 6e 52 6f 77 20 3d   0;.  res.nRow =
2c893 20 30 3b 0a 20 20 72 65 73 2e 6e 43 6f 6c 75 6d   0;.  res.nColum
2c894 6e 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 44 61  n = 0;.  res.nDa
2c895 74 61 20 3d 20 31 3b 0a 20 20 72 65 73 2e 6e 41  ta = 1;.  res.nA
2c896 6c 6c 6f 63 20 3d 20 32 30 3b 0a 20 20 72 65 73  lloc = 20;.  res
2c897 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  .rc = SQLITE_OK;
2c898 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 20  .  res.azResult 
2c899 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
2c89a 28 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 72  (sizeof(char*)*r
2c89b 65 73 2e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 69  es.nAlloc );.  i
2c89c 66 28 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 3d  f( res.azResult=
2c89d 3d 30 20 29 7b 0a 20 20 20 20 20 64 62 2d 3e 65  =0 ){.     db->e
2c89e 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  rrCode = SQLITE_
2c89f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 72 65 74 75  NOMEM;.     retu
2c8a0 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2c8a1 0a 20 20 7d 0a 20 20 72 65 73 2e 61 7a 52 65 73  .  }.  res.azRes
2c8a2 75 6c 74 5b 30 5d 20 3d 20 30 3b 0a 20 20 72 63  ult[0] = 0;.  rc
2c8a3 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
2c8a4 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65  db, zSql, sqlite
2c8a5 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62 2c 20  3_get_table_cb, 
2c8a6 26 72 65 73 2c 20 70 7a 45 72 72 4d 73 67 29 3b  &res, pzErrMsg);
2c8a7 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
2c8a8 66 28 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30  f(res.azResult[0
2c8a9 5d 29 3e 3d 20 73 69 7a 65 6f 66 28 72 65 73 2e  ])>= sizeof(res.
2c8aa 6e 44 61 74 61 29 20 29 3b 0a 20 20 72 65 73 2e  nData) );.  res.
2c8ab 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 53 51  azResult[0] = SQ
2c8ac 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
2c8ad 72 65 73 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66  res.nData);.  if
2c8ae 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c  ( (rc&0xff)==SQL
2c8af 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20  ITE_ABORT ){.   
2c8b0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
2c8b1 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c  ble(&res.azResul
2c8b2 74 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 72  t[1]);.    if( r
2c8b3 65 73 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  es.zErrMsg ){.  
2c8b4 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67      if( pzErrMsg
2c8b5 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2c8b6 74 65 33 5f 66 72 65 65 28 2a 70 7a 45 72 72 4d  te3_free(*pzErrM
2c8b7 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a  sg);.        *pz
2c8b8 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
2c8b9 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 72 65  _mprintf("%s",re
2c8ba 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  s.zErrMsg);.    
2c8bb 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2c8bc 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72 72 4d  3_free(res.zErrM
2c8bd 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  sg);.    }.    d
2c8be 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 65 73  b->errCode = res
2c8bf 2e 72 63 3b 20 20 2f 2a 20 41 73 73 75 6d 65 20  .rc;  /* Assume 
2c8c0 33 32 2d 62 69 74 20 61 73 73 69 67 6e 6d 65 6e  32-bit assignmen
2c8c1 74 20 69 73 20 61 74 6f 6d 69 63 20 2a 2f 0a 20  t is atomic */. 
2c8c2 20 20 20 72 65 74 75 72 6e 20 72 65 73 2e 72 63     return res.rc
2c8c3 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
2c8c4 66 72 65 65 28 72 65 73 2e 7a 45 72 72 4d 73 67  free(res.zErrMsg
2c8c5 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2c8c6 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
2c8c7 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
2c8c8 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31  (&res.azResult[1
2c8c9 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  ]);.    return r
2c8ca 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73  c;.  }.  if( res
2c8cb 2e 6e 41 6c 6c 6f 63 3e 72 65 73 2e 6e 44 61 74  .nAlloc>res.nDat
2c8cc 61 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a  a ){.    char **
2c8cd 61 7a 4e 65 77 3b 0a 20 20 20 20 61 7a 4e 65 77  azNew;.    azNew
2c8ce 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
2c8cf 6f 63 28 20 72 65 73 2e 61 7a 52 65 73 75 6c 74  oc( res.azResult
2c8d0 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a  , sizeof(char*)*
2c8d1 72 65 73 2e 6e 44 61 74 61 20 29 3b 0a 20 20 20  res.nData );.   
2c8d2 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20 29 7b   if( azNew==0 ){
2c8d3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
2c8d4 72 65 65 5f 74 61 62 6c 65 28 26 72 65 73 2e 61  ree_table(&res.a
2c8d5 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20  zResult[1]);.   
2c8d6 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d     db->errCode =
2c8d7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2c8d8 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2c8d9 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
2c8da 20 20 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74      res.azResult
2c8db 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a 20 20   = azNew;.  }.  
2c8dc 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20 26 72 65  *pazResult = &re
2c8dd 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 3b 0a 20  s.azResult[1];. 
2c8de 20 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20 29 20   if( pnColumn ) 
2c8df 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20 72 65 73 2e  *pnColumn = res.
2c8e0 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28 20 70  nColumn;.  if( p
2c8e1 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20 3d 20  nRow ) *pnRow = 
2c8e2 72 65 73 2e 6e 52 6f 77 3b 0a 20 20 72 65 74 75  res.nRow;.  retu
2c8e3 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
2c8e4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 65  This routine fre
2c8e5 65 73 20 74 68 65 20 73 70 61 63 65 20 74 68 65  es the space the
2c8e6 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
2c8e7 6c 65 28 29 20 6d 61 6c 6c 6f 63 65 64 2e 0a 2a  le() malloced..*
2c8e8 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
2c8e9 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74  d sqlite3_free_t
2c8ea 61 62 6c 65 28 0a 20 20 63 68 61 72 20 2a 2a 61  able(.  char **a
2c8eb 7a 52 65 73 75 6c 74 20 20 20 20 20 20 20 20 20  zResult         
2c8ec 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 72 65 74     /* Result ret
2c8ed 75 72 6e 65 64 20 66 72 6f 6d 20 66 72 6f 6d 20  urned from from 
2c8ee 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
2c8ef 65 28 29 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  e() */.){.  if( 
2c8f0 61 7a 52 65 73 75 6c 74 20 29 7b 0a 20 20 20 20  azResult ){.    
2c8f1 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20 61 7a  int i, n;.    az
2c8f2 52 65 73 75 6c 74 2d 2d 3b 0a 20 20 20 20 61 73  Result--;.    as
2c8f3 73 65 72 74 28 20 61 7a 52 65 73 75 6c 74 21 3d  sert( azResult!=
2c8f4 30 20 29 3b 0a 20 20 20 20 6e 20 3d 20 53 51 4c  0 );.    n = SQL
2c8f5 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 61  ITE_PTR_TO_INT(a
2c8f6 7a 52 65 73 75 6c 74 5b 30 5d 29 3b 0a 20 20 20  zResult[0]);.   
2c8f7 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 3b 20 69   for(i=1; i<n; i
2c8f8 2b 2b 29 7b 20 69 66 28 20 61 7a 52 65 73 75 6c  ++){ if( azResul
2c8f9 74 5b 69 5d 20 29 20 73 71 6c 69 74 65 33 5f 66  t[i] ) sqlite3_f
2c8fa 72 65 65 28 61 7a 52 65 73 75 6c 74 5b 69 5d 29  ree(azResult[i])
2c8fb 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ; }.    sqlite3_
2c8fc 66 72 65 65 28 61 7a 52 65 73 75 6c 74 29 3b 0a  free(azResult);.
2c8fd 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a    }.}..#endif /*
2c8fe 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54   SQLITE_OMIT_GET
2c8ff 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _TABLE */../****
2c900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2c901 66 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a  f table.c ******
2c902 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c903 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c904 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2c905 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2c906 20 66 69 6c 65 20 74 72 69 67 67 65 72 2e 63 20   file trigger.c 
2c907 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c908 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c909 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
2c90a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2c90b 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2c90c 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2c90d 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2c90e 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2c90f 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2c910 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2c911 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2c912 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2c913 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2c914 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2c915 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2c916 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2c917 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
2c918 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2c919 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2c91a 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2c91b 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2c91c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c91d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c91e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c91f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
2c920 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74 72 69 67 67  **.** $Id: trigg
2c921 65 72 2e 63 2c 76 20 31 2e 31 34 33 20 32 30 30  er.c,v 1.143 200
2c922 39 2f 30 38 2f 31 30 20 30 33 3a 35 37 3a 35 38  9/08/10 03:57:58
2c923 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a   shane Exp $.*/.
2c924 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2c925 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a 0a  OMIT_TRIGGER./*.
2c926 2a 2a 20 44 65 6c 65 74 65 20 61 20 6c 69 6e 6b  ** Delete a link
2c927 65 64 20 6c 69 73 74 20 6f 66 20 54 72 69 67 67  ed list of Trigg
2c928 65 72 53 74 65 70 20 73 74 72 75 63 74 75 72 65  erStep structure
2c929 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
2c92a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2c92b 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74  3DeleteTriggerSt
2c92c 65 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ep(sqlite3 *db, 
2c92d 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72  TriggerStep *pTr
2c92e 69 67 67 65 72 53 74 65 70 29 7b 0a 20 20 77 68  iggerStep){.  wh
2c92f 69 6c 65 28 20 70 54 72 69 67 67 65 72 53 74 65  ile( pTriggerSte
2c930 70 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72  p ){.    Trigger
2c931 53 74 65 70 20 2a 20 70 54 6d 70 20 3d 20 70 54  Step * pTmp = pT
2c932 72 69 67 67 65 72 53 74 65 70 3b 0a 20 20 20 20  riggerStep;.    
2c933 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 70  pTriggerStep = p
2c934 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 4e 65  TriggerStep->pNe
2c935 78 74 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33  xt;..    sqlite3
2c936 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2c937 54 6d 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  Tmp->pWhere);.  
2c938 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2c939 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70  tDelete(db, pTmp
2c93a 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a 20 20  ->pExprList);.  
2c93b 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2c93c 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e  elete(db, pTmp->
2c93d 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71  pSelect);.    sq
2c93e 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
2c93f 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 49 64 4c  e(db, pTmp->pIdL
2c940 69 73 74 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74  ist);..    sqlit
2c941 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 6d  e3DbFree(db, pTm
2c942 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
2c943 20 47 69 76 65 6e 20 74 61 62 6c 65 20 70 54 61   Given table pTa
2c944 62 2c 20 72 65 74 75 72 6e 20 61 20 6c 69 73 74  b, return a list
2c945 20 6f 66 20 61 6c 6c 20 74 68 65 20 74 72 69 67   of all the trig
2c946 67 65 72 73 20 61 74 74 61 63 68 65 64 20 74 6f  gers attached to
2c947 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20   .** the table. 
2c948 54 68 65 20 6c 69 73 74 20 69 73 20 63 6f 6e 6e  The list is conn
2c949 65 63 74 65 64 20 62 79 20 54 72 69 67 67 65 72  ected by Trigger
2c94a 2e 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 73 2e  .pNext pointers.
2c94b 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  .**.** All of th
2c94c 65 20 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54  e triggers on pT
2c94d 61 62 20 74 68 61 74 20 61 72 65 20 69 6e 20 74  ab that are in t
2c94e 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
2c94f 20 61 73 20 70 54 61 62 0a 2a 2a 20 61 72 65 20   as pTab.** are 
2c950 61 6c 72 65 61 64 79 20 61 74 74 61 63 68 65 64  already attached
2c951 20 74 6f 20 70 54 61 62 2d 3e 70 54 72 69 67 67   to pTab->pTrigg
2c952 65 72 2e 20 20 42 75 74 20 74 68 65 72 65 20 6d  er.  But there m
2c953 69 67 68 74 20 62 65 20 61 64 64 69 74 69 6f 6e  ight be addition
2c954 61 6c 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 6f  al.** triggers o
2c955 6e 20 70 54 61 62 20 69 6e 20 74 68 65 20 54 45  n pTab in the TE
2c956 4d 50 20 73 63 68 65 6d 61 2e 20 20 54 68 69 73  MP schema.  This
2c957 20 72 6f 75 74 69 6e 65 20 70 72 65 70 65 6e 64   routine prepend
2c958 73 20 61 6c 6c 0a 2a 2a 20 54 45 4d 50 20 74 72  s all.** TEMP tr
2c959 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 20 74  iggers on pTab t
2c95a 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  o the beginning 
2c95b 6f 66 20 74 68 65 20 70 54 61 62 2d 3e 70 54 72  of the pTab->pTr
2c95c 69 67 67 65 72 20 6c 69 73 74 0a 2a 2a 20 61 6e  igger list.** an
2c95d 64 20 72 65 74 75 72 6e 73 20 74 68 65 20 63 6f  d returns the co
2c95e 6d 62 69 6e 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a  mbined list..**.
2c95f 2a 2a 20 54 6f 20 73 74 61 74 65 20 69 74 20 61  ** To state it a
2c960 6e 6f 74 68 65 72 20 77 61 79 3a 20 20 54 68 69  nother way:  Thi
2c961 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
2c962 73 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  s a list of all 
2c963 74 72 69 67 67 65 72 73 0a 2a 2a 20 74 68 61 74  triggers.** that
2c964 20 66 69 72 65 20 6f 66 66 20 6f 66 20 70 54 61   fire off of pTa
2c965 62 2e 20 20 54 68 65 20 6c 69 73 74 20 77 69 6c  b.  The list wil
2c966 6c 20 69 6e 63 6c 75 64 65 20 61 6e 79 20 54 45  l include any TE
2c967 4d 50 20 74 72 69 67 67 65 72 73 20 6f 6e 0a 2a  MP triggers on.*
2c968 2a 20 70 54 61 62 20 61 73 20 77 65 6c 6c 20 61  * pTab as well a
2c969 73 20 74 68 65 20 74 72 69 67 67 65 72 73 20 6c  s the triggers l
2c96a 69 73 65 64 20 69 6e 20 70 54 61 62 2d 3e 70 54  ised in pTab->pT
2c96b 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  rigger..*/.SQLIT
2c96c 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67 65  E_PRIVATE Trigge
2c96d 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65  r *sqlite3Trigge
2c96e 72 4c 69 73 74 28 50 61 72 73 65 20 2a 70 50 61  rList(Parse *pPa
2c96f 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
2c970 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a 20 63 6f  ){.  Schema * co
2c971 6e 73 74 20 70 54 6d 70 53 63 68 65 6d 61 20 3d  nst pTmpSchema =
2c972 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62   pParse->db->aDb
2c973 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 54  [1].pSchema;.  T
2c974 72 69 67 67 65 72 20 2a 70 4c 69 73 74 20 3d 20  rigger *pList = 
2c975 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2c976 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74      /* List of t
2c977 72 69 67 67 65 72 73 20 74 6f 20 72 65 74 75 72  riggers to retur
2c978 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 72  n */..  if( pPar
2c979 73 65 2d 3e 64 69 73 61 62 6c 65 54 72 69 67 67  se->disableTrigg
2c97a 65 72 73 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ers ){.    retur
2c97b 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  n 0;.  }..  if( 
2c97c 70 54 6d 70 53 63 68 65 6d 61 21 3d 70 54 61 62  pTmpSchema!=pTab
2c97d 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ->pSchema ){.   
2c97e 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20   HashElem *p;.  
2c97f 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61    for(p=sqliteHa
2c980 73 68 46 69 72 73 74 28 26 70 54 6d 70 53 63 68  shFirst(&pTmpSch
2c981 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 3b 20  ema->trigHash); 
2c982 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e  p; p=sqliteHashN
2c983 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20 54  ext(p)){.      T
2c984 72 69 67 67 65 72 20 2a 70 54 72 69 67 20 3d 20  rigger *pTrig = 
2c985 28 54 72 69 67 67 65 72 20 2a 29 73 71 6c 69 74  (Trigger *)sqlit
2c986 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 20 20  eHashData(p);.  
2c987 20 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70      if( pTrig->p
2c988 54 61 62 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d  TabSchema==pTab-
2c989 3e 70 53 63 68 65 6d 61 0a 20 20 20 20 20 20 20  >pSchema.       
2c98a 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  && 0==sqlite3Str
2c98b 49 43 6d 70 28 70 54 72 69 67 2d 3e 74 61 62 6c  ICmp(pTrig->tabl
2c98c 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20  e, pTab->zName) 
2c98d 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
2c98e 20 20 70 54 72 69 67 2d 3e 70 4e 65 78 74 20 3d    pTrig->pNext =
2c98f 20 28 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 20   (pList ? pList 
2c990 3a 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  : pTab->pTrigger
2c991 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74  );.        pList
2c992 20 3d 20 70 54 72 69 67 3b 0a 20 20 20 20 20 20   = pTrig;.      
2c993 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  }.    }.  }..  r
2c994 65 74 75 72 6e 20 28 70 4c 69 73 74 20 3f 20 70  eturn (pList ? p
2c995 4c 69 73 74 20 3a 20 70 54 61 62 2d 3e 70 54 72  List : pTab->pTr
2c996 69 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  igger);.}../*.**
2c997 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   This is called 
2c998 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68  by the parser wh
2c999 65 6e 20 69 74 20 73 65 65 73 20 61 20 43 52 45  en it sees a CRE
2c99a 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74  ATE TRIGGER stat
2c99b 65 6d 65 6e 74 0a 2a 2a 20 75 70 20 74 6f 20 74  ement.** up to t
2c99c 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 65 20  he point of the 
2c99d 42 45 47 49 4e 20 62 65 66 6f 72 65 20 74 68 65  BEGIN before the
2c99e 20 74 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73   trigger actions
2c99f 2e 20 20 41 20 54 72 69 67 67 65 72 0a 2a 2a 20  .  A Trigger.** 
2c9a0 73 74 72 75 63 74 75 72 65 20 69 73 20 67 65 6e  structure is gen
2c9a1 65 72 61 74 65 64 20 62 61 73 65 64 20 6f 6e 20  erated based on 
2c9a2 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
2c9a3 61 76 61 69 6c 61 62 6c 65 20 61 6e 64 20 73 74  available and st
2c9a4 6f 72 65 64 0a 2a 2a 20 69 6e 20 70 50 61 72 73  ored.** in pPars
2c9a5 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 2e 20  e->pNewTrigger. 
2c9a6 20 41 66 74 65 72 20 74 68 65 20 74 72 69 67 67   After the trigg
2c9a7 65 72 20 61 63 74 69 6f 6e 73 20 68 61 76 65 20  er actions have 
2c9a8 62 65 65 6e 20 70 61 72 73 65 64 2c 20 74 68 65  been parsed, the
2c9a9 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69 6e 69 73  .** sqlite3Finis
2c9aa 68 54 72 69 67 67 65 72 28 29 20 66 75 6e 63 74  hTrigger() funct
2c9ab 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ion is called to
2c9ac 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 74 72   complete the tr
2c9ad 69 67 67 65 72 0a 2a 2a 20 63 6f 6e 73 74 72 75  igger.** constru
2c9ae 63 74 69 6f 6e 20 70 72 6f 63 65 73 73 2e 0a 2a  ction process..*
2c9af 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2c9b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
2c9b1 69 6e 54 72 69 67 67 65 72 28 0a 20 20 50 61 72  inTrigger(.  Par
2c9b2 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2c9b3 20 2f 2a 20 54 68 65 20 70 61 72 73 65 20 63 6f   /* The parse co
2c9b4 6e 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45  ntext of the CRE
2c9b5 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74  ATE TRIGGER stat
2c9b6 65 6d 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ement */.  Token
2c9b7 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f   *pName1,      /
2c9b8 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
2c9b9 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 54  e trigger */.  T
2c9ba 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20  oken *pName2,   
2c9bb 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f     /* The name o
2c9bc 66 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f  f the trigger */
2c9bd 0a 20 20 69 6e 74 20 74 72 5f 74 6d 2c 20 20 20  .  int tr_tm,   
2c9be 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
2c9bf 20 54 4b 5f 42 45 46 4f 52 45 2c 20 54 4b 5f 41   TK_BEFORE, TK_A
2c9c0 46 54 45 52 2c 20 54 4b 5f 49 4e 53 54 45 41 44  FTER, TK_INSTEAD
2c9c1 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20   */.  int op,   
2c9c2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
2c9c3 20 6f 66 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54   of TK_INSERT, T
2c9c4 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 44 45 4c  K_UPDATE, TK_DEL
2c9c5 45 54 45 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20  ETE */.  IdList 
2c9c6 2a 70 43 6f 6c 75 6d 6e 73 2c 20 20 20 2f 2a 20  *pColumns,   /* 
2c9c7 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 69 66 20 74  column list if t
2c9c8 68 69 73 20 69 73 20 61 6e 20 55 50 44 41 54 45  his is an UPDATE
2c9c9 20 4f 46 20 74 72 69 67 67 65 72 20 2a 2f 0a 20   OF trigger */. 
2c9ca 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 6c 65   SrcList *pTable
2c9cb 4e 61 6d 65 2c 2f 2a 20 54 68 65 20 6e 61 6d 65  Name,/* The name
2c9cc 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2f 76 69   of the table/vi
2c9cd 65 77 20 74 68 65 20 74 72 69 67 67 65 72 20 61  ew the trigger a
2c9ce 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 45  pplies to */.  E
2c9cf 78 70 72 20 2a 70 57 68 65 6e 2c 20 20 20 20 20  xpr *pWhen,     
2c9d0 20 20 20 2f 2a 20 57 48 45 4e 20 63 6c 61 75 73     /* WHEN claus
2c9d1 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d  e */.  int isTem
2c9d2 70 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  p,         /* Tr
2c9d3 75 65 20 69 66 20 74 68 65 20 54 45 4d 50 4f 52  ue if the TEMPOR
2c9d4 41 52 59 20 6b 65 79 77 6f 72 64 20 69 73 20 70  ARY keyword is p
2c9d5 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  resent */.  int 
2c9d6 6e 6f 45 72 72 20 20 20 20 20 20 20 20 20 20 20  noErr           
2c9d7 2f 2a 20 53 75 70 70 72 65 73 73 20 65 72 72 6f  /* Suppress erro
2c9d8 72 73 20 69 66 20 74 68 65 20 74 72 69 67 67 65  rs if the trigge
2c9d9 72 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  r already exists
2c9da 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72   */.){.  Trigger
2c9db 20 2a 70 54 72 69 67 67 65 72 20 3d 20 30 3b 20   *pTrigger = 0; 
2c9dc 20 2f 2a 20 54 68 65 20 6e 65 77 20 74 72 69 67   /* The new trig
2c9dd 67 65 72 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ger */.  Table *
2c9de 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
2c9df 20 2f 2a 20 54 61 62 6c 65 20 74 68 61 74 20 74   /* Table that t
2c9e0 68 65 20 74 72 69 67 67 65 72 20 66 69 72 65 73  he trigger fires
2c9e1 20 6f 66 66 20 6f 66 20 2a 2f 0a 20 20 63 68 61   off of */.  cha
2c9e2 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20  r *zName = 0;   
2c9e3 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2c9e4 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20  the trigger */. 
2c9e5 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2c9e6 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 54  Parse->db;  /* T
2c9e7 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
2c9e8 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ection */.  int 
2c9e9 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
2c9ea 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
2c9eb 61 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ase to store the
2c9ec 20 74 72 69 67 67 65 72 20 69 6e 20 2a 2f 0a 20   trigger in */. 
2c9ed 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20   Token *pName;  
2c9ee 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2c9ef 75 6e 71 75 61 6c 69 66 69 65 64 20 64 62 20 6e  unqualified db n
2c9f0 61 6d 65 20 2a 2f 0a 20 20 44 62 46 69 78 65 72  ame */.  DbFixer
2c9f1 20 73 46 69 78 3b 20 20 20 20 20 20 20 20 20 20   sFix;          
2c9f2 20 2f 2a 20 53 74 61 74 65 20 76 65 63 74 6f 72   /* State vector
2c9f3 20 66 6f 72 20 74 68 65 20 44 42 20 66 69 78 65   for the DB fixe
2c9f4 72 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 44  r */.  int iTabD
2c9f5 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
2c9f6 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * Index of the d
2c9f7 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20  atabase holding 
2c9f8 70 54 61 62 20 2a 2f 0a 0a 20 20 61 73 73 65 72  pTab */..  asser
2c9f9 74 28 20 70 4e 61 6d 65 31 21 3d 30 20 29 3b 20  t( pName1!=0 ); 
2c9fa 20 20 2f 2a 20 70 4e 61 6d 65 31 2d 3e 7a 20 6d    /* pName1->z m
2c9fb 69 67 68 74 20 62 65 20 4e 55 4c 4c 2c 20 62 75  ight be NULL, bu
2c9fc 74 20 6e 6f 74 20 70 4e 61 6d 65 31 20 69 74 73  t not pName1 its
2c9fd 65 6c 66 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  elf */.  assert(
2c9fe 20 70 4e 61 6d 65 32 21 3d 30 20 29 3b 0a 20 20   pName2!=0 );.  
2c9ff 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 49  assert( op==TK_I
2ca00 4e 53 45 52 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f  NSERT || op==TK_
2ca01 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b  UPDATE || op==TK
2ca02 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 61 73 73  _DELETE );.  ass
2ca03 65 72 74 28 20 6f 70 3e 30 20 26 26 20 6f 70 3c  ert( op>0 && op<
2ca04 30 78 66 66 20 29 3b 0a 20 20 69 66 28 20 69 73  0xff );.  if( is
2ca05 54 65 6d 70 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Temp ){.    /* I
2ca06 66 20 54 45 4d 50 20 77 61 73 20 73 70 65 63 69  f TEMP was speci
2ca07 66 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20 74  fied, then the t
2ca08 72 69 67 67 65 72 20 6e 61 6d 65 20 6d 61 79 20  rigger name may 
2ca09 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65 64  not be qualified
2ca0a 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4e 61  . */.    if( pNa
2ca0b 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20  me2->n>0 ){.    
2ca0c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2ca0d 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70 6f  g(pParse, "tempo
2ca0e 72 61 72 79 20 74 72 69 67 67 65 72 20 6d 61 79  rary trigger may
2ca0f 20 6e 6f 74 20 68 61 76 65 20 71 75 61 6c 69 66   not have qualif
2ca10 69 65 64 20 6e 61 6d 65 22 29 3b 0a 20 20 20 20  ied name");.    
2ca11 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
2ca12 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20  leanup;.    }.  
2ca13 20 20 69 44 62 20 3d 20 31 3b 0a 20 20 20 20 70    iDb = 1;.    p
2ca14 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 31 3b 0a 20  Name = pName1;. 
2ca15 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46   }else{.    /* F
2ca16 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 64 62  igure out the db
2ca17 20 74 68 61 74 20 74 68 65 20 74 68 65 20 74 72   that the the tr
2ca18 69 67 67 65 72 20 77 69 6c 6c 20 62 65 20 63 72  igger will be cr
2ca19 65 61 74 65 64 20 69 6e 20 2a 2f 0a 20 20 20 20  eated in */.    
2ca1a 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f  iDb = sqlite3Two
2ca1b 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  PartName(pParse,
2ca1c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
2ca1d 20 26 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66   &pName);.    if
2ca1e 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 20  ( iDb<0 ){.     
2ca1f 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
2ca20 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d  eanup;.    }.  }
2ca21 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72  ..  /* If the tr
2ca22 69 67 67 65 72 20 6e 61 6d 65 20 77 61 73 20 75  igger name was u
2ca23 6e 71 75 61 6c 69 66 69 65 64 2c 20 61 6e 64 20  nqualified, and 
2ca24 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 74  the table is a t
2ca25 65 6d 70 20 74 61 62 6c 65 2c 0a 20 20 2a 2a 20  emp table,.  ** 
2ca26 74 68 65 6e 20 73 65 74 20 69 44 62 20 74 6f 20  then set iDb to 
2ca27 31 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  1 to create the 
2ca28 74 72 69 67 67 65 72 20 69 6e 20 74 68 65 20 74  trigger in the t
2ca29 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
2ca2a 65 2e 0a 20 20 2a 2a 20 49 66 20 73 71 6c 69 74  e..  ** If sqlit
2ca2b 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28  e3SrcListLookup(
2ca2c 29 20 72 65 74 75 72 6e 73 20 30 2c 20 69 6e 64  ) returns 0, ind
2ca2d 69 63 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c  icating the tabl
2ca2e 65 20 64 6f 65 73 20 6e 6f 74 0a 20 20 2a 2a 20  e does not.  ** 
2ca2f 65 78 69 73 74 2c 20 74 68 65 20 65 72 72 6f 72  exist, the error
2ca30 20 69 73 20 63 61 75 67 68 74 20 62 79 20 74 68   is caught by th
2ca31 65 20 62 6c 6f 63 6b 20 62 65 6c 6f 77 2e 0a 20  e block below.. 
2ca32 20 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62 6c   */.  if( !pTabl
2ca33 65 4e 61 6d 65 20 7c 7c 20 64 62 2d 3e 6d 61 6c  eName || db->mal
2ca34 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2ca35 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
2ca36 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 54 61  eanup;.  }.  pTa
2ca37 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  b = sqlite3SrcLi
2ca38 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c  stLookup(pParse,
2ca39 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20   pTableName);.  
2ca3a 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30  if( pName2->n==0
2ca3b 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61 62   && pTab && pTab
2ca3c 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61  ->pSchema==db->a
2ca3d 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 7b  Db[1].pSchema ){
2ca3e 0a 20 20 20 20 69 44 62 20 3d 20 31 3b 0a 20 20  .    iDb = 1;.  
2ca3f 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74  }..  /* Ensure t
2ca40 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d 61  he table name ma
2ca41 74 63 68 65 73 20 64 61 74 61 62 61 73 65 20 6e  tches database n
2ca42 61 6d 65 20 61 6e 64 20 74 68 61 74 20 74 68 65  ame and that the
2ca43 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 2a 2f   table exists */
2ca44 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2ca45 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 74  cFailed ) goto t
2ca46 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
2ca47 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
2ca48 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  Name->nSrc==1 );
2ca49 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
2ca4a 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61  xInit(&sFix, pPa
2ca4b 72 73 65 2c 20 69 44 62 2c 20 22 74 72 69 67 67  rse, iDb, "trigg
2ca4c 65 72 22 2c 20 70 4e 61 6d 65 29 20 26 26 20 0a  er", pName) && .
2ca4d 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 69 78        sqlite3Fix
2ca4e 53 72 63 4c 69 73 74 28 26 73 46 69 78 2c 20 70  SrcList(&sFix, p
2ca4f 54 61 62 6c 65 4e 61 6d 65 29 20 29 7b 0a 20 20  TableName) ){.  
2ca50 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
2ca51 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 54  leanup;.  }.  pT
2ca52 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  ab = sqlite3SrcL
2ca53 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65  istLookup(pParse
2ca54 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20  , pTableName);. 
2ca55 20 69 66 28 20 21 70 54 61 62 20 29 7b 0a 20 20   if( !pTab ){.  
2ca56 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 64    /* The table d
2ca57 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 2a  oes not exist. *
2ca58 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e  /.    if( db->in
2ca59 69 74 2e 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20  it.iDb==1 ){.   
2ca5a 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 33 38     /* Ticket #38
2ca5b 31 30 2e 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 72  10..      ** Nor
2ca5c 6d 61 6c 6c 79 2c 20 77 68 65 6e 65 76 65 72 20  mally, whenever 
2ca5d 61 20 74 61 62 6c 65 20 69 73 20 64 72 6f 70 70  a table is dropp
2ca5e 65 64 2c 20 61 6c 6c 20 61 73 73 6f 63 69 61 74  ed, all associat
2ca5f 65 64 20 74 72 69 67 67 65 72 73 20 61 72 65 0a  ed triggers are.
2ca60 20 20 20 20 20 20 2a 2a 20 64 72 6f 70 70 65 64        ** dropped
2ca61 20 74 6f 6f 2e 20 20 42 75 74 20 69 66 20 61 20   too.  But if a 
2ca62 54 45 4d 50 20 74 72 69 67 67 65 72 20 69 73 20  TEMP trigger is 
2ca63 63 72 65 61 74 65 64 20 6f 6e 20 61 20 6e 6f 6e  created on a non
2ca64 2d 54 45 4d 50 20 74 61 62 6c 65 0a 20 20 20 20  -TEMP table.    
2ca65 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 74 61 62    ** and the tab
2ca66 6c 65 20 69 73 20 64 72 6f 70 70 65 64 20 62 79  le is dropped by
2ca67 20 61 20 64 69 66 66 65 72 65 6e 74 20 64 61 74   a different dat
2ca68 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2ca69 2c 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 74  , the.      ** t
2ca6a 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20 76 69  rigger is not vi
2ca6b 73 69 62 6c 65 20 74 6f 20 74 68 65 20 64 61 74  sible to the dat
2ca6c 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2ca6d 20 74 68 61 74 20 64 6f 65 73 20 74 68 65 0a 20   that does the. 
2ca6e 20 20 20 20 20 2a 2a 20 64 72 6f 70 20 73 6f 20       ** drop so 
2ca6f 74 68 65 20 74 72 69 67 67 65 72 20 63 61 6e 6e  the trigger cann
2ca70 6f 74 20 62 65 20 64 72 6f 70 70 65 64 2e 20 20  ot be dropped.  
2ca71 54 68 69 73 20 72 65 73 75 6c 74 73 20 69 6e 20  This results in 
2ca72 61 6e 0a 20 20 20 20 20 20 2a 2a 20 22 6f 72 70  an.      ** "orp
2ca73 68 61 6e 65 64 20 74 72 69 67 67 65 72 22 20 2d  haned trigger" -
2ca74 20 61 20 74 72 69 67 67 65 72 20 77 68 6f 73 65   a trigger whose
2ca75 20 61 73 73 6f 63 69 61 74 65 64 20 74 61 62 6c   associated tabl
2ca76 65 20 69 73 20 6d 69 73 73 69 6e 67 2e 0a 20 20  e is missing..  
2ca77 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d      */.      db-
2ca78 3e 69 6e 69 74 2e 6f 72 70 68 61 6e 54 72 69 67  >init.orphanTrig
2ca79 67 65 72 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ger = 1;.    }. 
2ca7a 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
2ca7b 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
2ca7c 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2ca7d 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  b) ){.    sqlite
2ca7e 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2ca7f 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65  , "cannot create
2ca80 20 74 72 69 67 67 65 72 73 20 6f 6e 20 76 69 72   triggers on vir
2ca81 74 75 61 6c 20 74 61 62 6c 65 73 22 29 3b 0a 20  tual tables");. 
2ca82 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
2ca83 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
2ca84 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
2ca85 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69  e trigger name i
2ca86 73 20 6e 6f 74 20 72 65 73 65 72 76 65 64 20 61  s not reserved a
2ca87 6e 64 20 74 68 61 74 20 6e 6f 20 74 72 69 67 67  nd that no trigg
2ca88 65 72 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73  er of the.  ** s
2ca89 70 65 63 69 66 69 65 64 20 6e 61 6d 65 20 65 78  pecified name ex
2ca8a 69 73 74 73 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20  ists */.  zName 
2ca8b 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
2ca8c 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65  mToken(db, pName
2ca8d 29 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20  );.  if( !zName 
2ca8e 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  || SQLITE_OK!=sq
2ca8f 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74  lite3CheckObject
2ca90 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61  Name(pParse, zNa
2ca91 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  me) ){.    goto 
2ca92 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2ca93 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
2ca94 65 33 48 61 73 68 46 69 6e 64 28 26 28 64 62 2d  e3HashFind(&(db-
2ca95 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
2ca96 61 2d 3e 74 72 69 67 48 61 73 68 29 2c 0a 20 20  a->trigHash),.  
2ca97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca98 20 20 20 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74      zName, sqlit
2ca99 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
2ca9a 29 29 20 29 7b 0a 20 20 20 20 69 66 28 20 21 6e  )) ){.    if( !n
2ca9b 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71  oErr ){.      sq
2ca9c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2ca9d 61 72 73 65 2c 20 22 74 72 69 67 67 65 72 20 25  arse, "trigger %
2ca9e 54 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  T already exists
2ca9f 22 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 7d  ", pName);.    }
2caa0 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
2caa1 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  r_cleanup;.  }..
2caa2 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 72 65 61    /* Do not crea
2caa3 74 65 20 61 20 74 72 69 67 67 65 72 20 6f 6e 20  te a trigger on 
2caa4 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a  a system table *
2caa5 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
2caa6 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e  trNICmp(pTab->zN
2caa7 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20  ame, "sqlite_", 
2caa8 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  7)==0 ){.    sql
2caa9 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2caaa 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65  rse, "cannot cre
2caab 61 74 65 20 74 72 69 67 67 65 72 20 6f 6e 20 73  ate trigger on s
2caac 79 73 74 65 6d 20 74 61 62 6c 65 22 29 3b 0a 20  ystem table");. 
2caad 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b     pParse->nErr+
2caae 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  +;.    goto trig
2caaf 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
2cab0 0a 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44 20 6f  ..  /* INSTEAD o
2cab1 66 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6f  f triggers are o
2cab2 6e 6c 79 20 66 6f 72 20 76 69 65 77 73 20 61 6e  nly for views an
2cab3 64 20 76 69 65 77 73 20 6f 6e 6c 79 20 73 75 70  d views only sup
2cab4 70 6f 72 74 20 49 4e 53 54 45 41 44 0a 20 20 2a  port INSTEAD.  *
2cab5 2a 20 6f 66 20 74 72 69 67 67 65 72 73 2e 0a 20  * of triggers.. 
2cab6 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e   */.  if( pTab->
2cab7 70 53 65 6c 65 63 74 20 26 26 20 74 72 5f 74 6d  pSelect && tr_tm
2cab8 21 3d 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a  !=TK_INSTEAD ){.
2cab9 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2caba 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e  Msg(pParse, "can
2cabb 6e 6f 74 20 63 72 65 61 74 65 20 25 73 20 74 72  not create %s tr
2cabc 69 67 67 65 72 20 6f 6e 20 76 69 65 77 3a 20 25  igger on view: %
2cabd 53 22 2c 20 0a 20 20 20 20 20 20 20 20 28 74 72  S", .        (tr
2cabe 5f 74 6d 20 3d 3d 20 54 4b 5f 42 45 46 4f 52 45  _tm == TK_BEFORE
2cabf 29 3f 22 42 45 46 4f 52 45 22 3a 22 41 46 54 45  )?"BEFORE":"AFTE
2cac0 52 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20  R", pTableName, 
2cac1 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69  0);.    goto tri
2cac2 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
2cac3 7d 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e 70  }.  if( !pTab->p
2cac4 53 65 6c 65 63 74 20 26 26 20 74 72 5f 74 6d 3d  Select && tr_tm=
2cac5 3d 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20  =TK_INSTEAD ){. 
2cac6 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2cac7 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
2cac8 6f 74 20 63 72 65 61 74 65 20 49 4e 53 54 45 41  ot create INSTEA
2cac9 44 20 4f 46 22 0a 20 20 20 20 20 20 20 20 22 20  D OF".        " 
2caca 74 72 69 67 67 65 72 20 6f 6e 20 74 61 62 6c 65  trigger on table
2cacb 3a 20 25 53 22 2c 20 70 54 61 62 6c 65 4e 61 6d  : %S", pTableNam
2cacc 65 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20  e, 0);.    goto 
2cacd 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2cace 0a 20 20 7d 0a 20 20 69 54 61 62 44 62 20 3d 20  .  }.  iTabDb = 
2cacf 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2cad0 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70  ndex(db, pTab->p
2cad1 53 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65  Schema);..#ifnde
2cad2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
2cad3 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a  THORIZATION.  {.
2cad4 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53      int code = S
2cad5 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49  QLITE_CREATE_TRI
2cad6 47 47 45 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20  GGER;.    const 
2cad7 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e  char *zDb = db->
2cad8 61 44 62 5b 69 54 61 62 44 62 5d 2e 7a 4e 61 6d  aDb[iTabDb].zNam
2cad9 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e;.    const cha
2cada 72 20 2a 7a 44 62 54 72 69 67 20 3d 20 69 73 54  r *zDbTrig = isT
2cadb 65 6d 70 20 3f 20 64 62 2d 3e 61 44 62 5b 31 5d  emp ? db->aDb[1]
2cadc 2e 7a 4e 61 6d 65 20 3a 20 7a 44 62 3b 0a 20 20  .zName : zDb;.  
2cadd 20 20 69 66 28 20 69 54 61 62 44 62 3d 3d 31 20    if( iTabDb==1 
2cade 7c 7c 20 69 73 54 65 6d 70 20 29 20 63 6f 64 65  || isTemp ) code
2cadf 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45   = SQLITE_CREATE
2cae0 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 3b 0a 20  _TEMP_TRIGGER;. 
2cae1 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
2cae2 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2cae3 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 70 54 61  code, zName, pTa
2cae4 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 54 72 69  b->zName, zDbTri
2cae5 67 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  g) ){.      goto
2cae6 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
2cae7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2cae8 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2cae9 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2caea 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54  INSERT, SCHEMA_T
2caeb 41 42 4c 45 28 69 54 61 62 44 62 29 2c 30 2c 7a  ABLE(iTabDb),0,z
2caec 44 62 29 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  Db)){.      goto
2caed 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
2caee 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
2caef 69 66 0a 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44  if..  /* INSTEAD
2caf0 20 4f 46 20 74 72 69 67 67 65 72 73 20 63 61 6e   OF triggers can
2caf1 20 6f 6e 6c 79 20 61 70 70 65 61 72 20 6f 6e 20   only appear on 
2caf2 76 69 65 77 73 20 61 6e 64 20 42 45 46 4f 52 45  views and BEFORE
2caf3 20 74 72 69 67 67 65 72 73 0a 20 20 2a 2a 20 63   triggers.  ** c
2caf4 61 6e 6e 6f 74 20 61 70 70 65 61 72 20 6f 6e 20  annot appear on 
2caf5 76 69 65 77 73 2e 20 20 53 6f 20 77 65 20 6d 69  views.  So we mi
2caf6 67 68 74 20 61 73 20 77 65 6c 6c 20 74 72 61 6e  ght as well tran
2caf7 73 6c 61 74 65 20 65 76 65 72 79 0a 20 20 2a 2a  slate every.  **
2caf8 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67   INSTEAD OF trig
2caf9 67 65 72 20 69 6e 74 6f 20 61 20 42 45 46 4f 52  ger into a BEFOR
2cafa 45 20 74 72 69 67 67 65 72 2e 20 20 49 74 20 73  E trigger.  It s
2cafb 69 6d 70 6c 69 66 69 65 73 20 63 6f 64 65 0a 20  implifies code. 
2cafc 20 2a 2a 20 65 6c 73 65 77 68 65 72 65 2e 0a 20   ** elsewhere.. 
2cafd 20 2a 2f 0a 20 20 69 66 20 28 74 72 5f 74 6d 20   */.  if (tr_tm 
2cafe 3d 3d 20 54 4b 5f 49 4e 53 54 45 41 44 29 7b 0a  == TK_INSTEAD){.
2caff 20 20 20 20 74 72 5f 74 6d 20 3d 20 54 4b 5f 42      tr_tm = TK_B
2cb00 45 46 4f 52 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  EFORE;.  }..  /*
2cb01 20 42 75 69 6c 64 20 74 68 65 20 54 72 69 67 67   Build the Trigg
2cb02 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 70  er object */.  p
2cb03 54 72 69 67 67 65 72 20 3d 20 28 54 72 69 67 67  Trigger = (Trigg
2cb04 65 72 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  er*)sqlite3DbMal
2cb05 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
2cb06 6f 66 28 54 72 69 67 67 65 72 29 29 3b 0a 20 20  of(Trigger));.  
2cb07 69 66 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20  if( pTrigger==0 
2cb08 29 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63  ) goto trigger_c
2cb09 6c 65 61 6e 75 70 3b 0a 20 20 70 54 72 69 67 67  leanup;.  pTrigg
2cb0a 65 72 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d  er->zName = zNam
2cb0b 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a  e;.  zName = 0;.
2cb0c 20 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c    pTrigger->tabl
2cb0d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
2cb0e 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 4e 61  Dup(db, pTableNa
2cb0f 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 29 3b  me->a[0].zName);
2cb10 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63  .  pTrigger->pSc
2cb11 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69  hema = db->aDb[i
2cb12 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70  Db].pSchema;.  p
2cb13 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68  Trigger->pTabSch
2cb14 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68  ema = pTab->pSch
2cb15 65 6d 61 3b 0a 20 20 70 54 72 69 67 67 65 72 2d  ema;.  pTrigger-
2cb16 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20  >op = (u8)op;.  
2cb17 70 54 72 69 67 67 65 72 2d 3e 74 72 5f 74 6d 20  pTrigger->tr_tm 
2cb18 3d 20 74 72 5f 74 6d 3d 3d 54 4b 5f 42 45 46 4f  = tr_tm==TK_BEFO
2cb19 52 45 20 3f 20 54 52 49 47 47 45 52 5f 42 45 46  RE ? TRIGGER_BEF
2cb1a 4f 52 45 20 3a 20 54 52 49 47 47 45 52 5f 41 46  ORE : TRIGGER_AF
2cb1b 54 45 52 3b 0a 20 20 70 54 72 69 67 67 65 72 2d  TER;.  pTrigger-
2cb1c 3e 70 57 68 65 6e 20 3d 20 73 71 6c 69 74 65 33  >pWhen = sqlite3
2cb1d 45 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65  ExprDup(db, pWhe
2cb1e 6e 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43  n, EXPRDUP_REDUC
2cb1f 45 29 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e  E);.  pTrigger->
2cb20 70 43 6f 6c 75 6d 6e 73 20 3d 20 73 71 6c 69 74  pColumns = sqlit
2cb21 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20  e3IdListDup(db, 
2cb22 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 61 73 73  pColumns);.  ass
2cb23 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65  ert( pParse->pNe
2cb24 77 54 72 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20  wTrigger==0 );. 
2cb25 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
2cb26 67 67 65 72 20 3d 20 70 54 72 69 67 67 65 72 3b  gger = pTrigger;
2cb27 0a 0a 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  ..trigger_cleanu
2cb28 70 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  p:.  sqlite3DbFr
2cb29 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ee(db, zName);. 
2cb2a 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
2cb2b 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65  elete(db, pTable
2cb2c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
2cb2d 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  IdListDelete(db,
2cb2e 20 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71   pColumns);.  sq
2cb2f 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2cb30 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20 20 69 66  db, pWhen);.  if
2cb31 28 20 21 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  ( !pParse->pNewT
2cb32 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 73 71  rigger ){.    sq
2cb33 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
2cb34 65 72 28 64 62 2c 20 70 54 72 69 67 67 65 72 29  er(db, pTrigger)
2cb35 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
2cb36 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70  ssert( pParse->p
2cb37 4e 65 77 54 72 69 67 67 65 72 3d 3d 70 54 72 69  NewTrigger==pTri
2cb38 67 67 65 72 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  gger );.  }.}../
2cb39 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2cb3a 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  e is called afte
2cb3b 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 74 72 69  r all of the tri
2cb3c 67 67 65 72 20 61 63 74 69 6f 6e 73 20 68 61 76  gger actions hav
2cb3d 65 20 62 65 65 6e 20 70 61 72 73 65 64 0a 2a 2a  e been parsed.**
2cb3e 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6d   in order to com
2cb3f 70 6c 65 74 65 20 74 68 65 20 70 72 6f 63 65 73  plete the proces
2cb40 73 20 6f 66 20 62 75 69 6c 64 69 6e 67 20 74 68  s of building th
2cb41 65 20 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51  e trigger..*/.SQ
2cb42 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2cb43 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54  d sqlite3FinishT
2cb44 72 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20  rigger(.  Parse 
2cb45 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2cb46 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
2cb47 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ext */.  Trigger
2cb48 53 74 65 70 20 2a 70 53 74 65 70 4c 69 73 74 2c  Step *pStepList,
2cb49 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 65   /* The triggere
2cb4a 64 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 54  d program */.  T
2cb4b 6f 6b 65 6e 20 2a 70 41 6c 6c 20 20 20 20 20 20  oken *pAll      
2cb4c 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20         /* Token 
2cb4d 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
2cb4e 68 65 20 63 6f 6d 70 6c 65 74 65 20 43 52 45 41  he complete CREA
2cb4f 54 45 20 54 52 49 47 47 45 52 20 2a 2f 0a 29 7b  TE TRIGGER */.){
2cb50 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2cb51 67 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  g = pParse->pNew
2cb52 54 72 69 67 67 65 72 3b 20 20 20 20 2f 2a 20 54  Trigger;    /* T
2cb53 72 69 67 67 65 72 20 62 65 69 6e 67 20 66 69 6e  rigger being fin
2cb54 69 73 68 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  ished */.  char 
2cb55 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
2cb56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb57 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
2cb58 72 69 67 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69  rigger */.  sqli
2cb59 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2cb5a 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 20 20  ->db;           
2cb5b 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
2cb5c 62 61 73 65 20 2a 2f 0a 20 20 44 62 46 69 78 65  base */.  DbFixe
2cb5d 72 20 73 46 69 78 3b 0a 20 20 69 6e 74 20 69 44  r sFix;.  int iD
2cb5e 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
2cb5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb60 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
2cb61 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72  ontaining the tr
2cb62 69 67 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e  igger */.  Token
2cb63 20 6e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 20   nameToken;     
2cb64 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72        /* Trigger
2cb65 20 6e 61 6d 65 20 66 6f 72 20 65 72 72 6f 72 20   name for error 
2cb66 72 65 70 6f 72 74 69 6e 67 20 2a 2f 0a 0a 20 20  reporting */..  
2cb67 70 54 72 69 67 20 3d 20 70 50 61 72 73 65 2d 3e  pTrig = pParse->
2cb68 70 4e 65 77 54 72 69 67 67 65 72 3b 0a 20 20 70  pNewTrigger;.  p
2cb69 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2cb6a 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 4e 45  er = 0;.  if( NE
2cb6b 56 45 52 28 70 50 61 72 73 65 2d 3e 6e 45 72 72  VER(pParse->nErr
2cb6c 29 20 7c 7c 20 21 70 54 72 69 67 20 29 20 67 6f  ) || !pTrig ) go
2cb6d 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73 68  to triggerfinish
2cb6e 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7a 4e 61 6d  _cleanup;.  zNam
2cb6f 65 20 3d 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65  e = pTrig->zName
2cb70 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
2cb71 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
2cb72 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69 67  Parse->db, pTrig
2cb73 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 70 54  ->pSchema);.  pT
2cb74 72 69 67 2d 3e 73 74 65 70 5f 6c 69 73 74 20 3d  rig->step_list =
2cb75 20 70 53 74 65 70 4c 69 73 74 3b 0a 20 20 77 68   pStepList;.  wh
2cb76 69 6c 65 28 20 70 53 74 65 70 4c 69 73 74 20 29  ile( pStepList )
2cb77 7b 0a 20 20 20 20 70 53 74 65 70 4c 69 73 74 2d  {.    pStepList-
2cb78 3e 70 54 72 69 67 20 3d 20 70 54 72 69 67 3b 0a  >pTrig = pTrig;.
2cb79 20 20 20 20 70 53 74 65 70 4c 69 73 74 20 3d 20      pStepList = 
2cb7a 70 53 74 65 70 4c 69 73 74 2d 3e 70 4e 65 78 74  pStepList->pNext
2cb7b 3b 0a 20 20 7d 0a 20 20 6e 61 6d 65 54 6f 6b 65  ;.  }.  nameToke
2cb7c 6e 2e 7a 20 3d 20 70 54 72 69 67 2d 3e 7a 4e 61  n.z = pTrig->zNa
2cb7d 6d 65 3b 0a 20 20 6e 61 6d 65 54 6f 6b 65 6e 2e  me;.  nameToken.
2cb7e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
2cb7f 6e 33 30 28 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 29  n30(nameToken.z)
2cb80 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46  ;.  if( sqlite3F
2cb81 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50  ixInit(&sFix, pP
2cb82 61 72 73 65 2c 20 69 44 62 2c 20 22 74 72 69 67  arse, iDb, "trig
2cb83 67 65 72 22 2c 20 26 6e 61 6d 65 54 6f 6b 65 6e  ger", &nameToken
2cb84 29 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  ) .          && 
2cb85 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65  sqlite3FixTrigge
2cb86 72 53 74 65 70 28 26 73 46 69 78 2c 20 70 54 72  rStep(&sFix, pTr
2cb87 69 67 2d 3e 73 74 65 70 5f 6c 69 73 74 29 20 29  ig->step_list) )
2cb88 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67  {.    goto trigg
2cb89 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70  erfinish_cleanup
2cb8a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 77  ;.  }..  /* if w
2cb8b 65 20 61 72 65 20 6e 6f 74 20 69 6e 69 74 69 61  e are not initia
2cb8c 6c 69 7a 69 6e 67 2c 20 61 6e 64 20 74 68 69 73  lizing, and this
2cb8d 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20   trigger is not 
2cb8e 6f 6e 20 61 20 54 45 4d 50 20 74 61 62 6c 65 2c  on a TEMP table,
2cb8f 20 0a 20 20 2a 2a 20 62 75 69 6c 64 20 74 68 65   .  ** build the
2cb90 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 65   sqlite_master e
2cb91 6e 74 72 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ntry.  */.  if( 
2cb92 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29  !db->init.busy )
2cb93 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20  {.    Vdbe *v;. 
2cb94 20 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20     char *z;..   
2cb95 20 2f 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72   /* Make an entr
2cb96 79 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  y in the sqlite_
2cb97 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a  master table */.
2cb98 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
2cb99 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2cb9a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67      if( v==0 ) g
2cb9b 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73  oto triggerfinis
2cb9c 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 73  h_cleanup;.    s
2cb9d 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
2cb9e 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
2cb9f 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 7a  , 0, iDb);.    z
2cba0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
2cba1 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70  Dup(db, (char*)p
2cba2 41 6c 6c 2d 3e 7a 2c 20 70 41 6c 6c 2d 3e 6e 29  All->z, pAll->n)
2cba3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  ;.    sqlite3Nes
2cba4 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
2cba5 0a 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20  .       "INSERT 
2cba6 49 4e 54 4f 20 25 51 2e 25 73 20 56 41 4c 55 45  INTO %Q.%s VALUE
2cba7 53 28 27 74 72 69 67 67 65 72 27 2c 25 51 2c 25  S('trigger',%Q,%
2cba8 51 2c 30 2c 27 43 52 45 41 54 45 20 54 52 49 47  Q,0,'CREATE TRIG
2cba9 47 45 52 20 25 71 27 29 22 2c 0a 20 20 20 20 20  GER %q')",.     
2cbaa 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a    db->aDb[iDb].z
2cbab 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42  Name, SCHEMA_TAB
2cbac 4c 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65 2c 0a  LE(iDb), zName,.
2cbad 20 20 20 20 20 20 20 70 54 72 69 67 2d 3e 74 61         pTrig->ta
2cbae 62 6c 65 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c  ble, z);.    sql
2cbaf 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2cbb0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
2cbb1 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
2cbb2 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  e, iDb);.    sql
2cbb3 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2cbb4 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  , OP_ParseSchema
2cbb5 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 73 71 6c  , iDb, 0, 0, sql
2cbb6 69 74 65 33 4d 50 72 69 6e 74 66 28 0a 20 20 20  ite3MPrintf(.   
2cbb7 20 20 20 20 20 64 62 2c 20 22 74 79 70 65 3d 27       db, "type='
2cbb8 74 72 69 67 67 65 72 27 20 41 4e 44 20 6e 61 6d  trigger' AND nam
2cbb9 65 3d 27 25 71 27 22 2c 20 7a 4e 61 6d 65 29 2c  e='%q'", zName),
2cbba 20 50 34 5f 44 59 4e 41 4d 49 43 0a 20 20 20 20   P4_DYNAMIC.    
2cbbb 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 64 62  );.  }..  if( db
2cbbc 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
2cbbd 20 20 20 54 72 69 67 67 65 72 20 2a 70 4c 69 6e     Trigger *pLin
2cbbe 6b 20 3d 20 70 54 72 69 67 3b 0a 20 20 20 20 48  k = pTrig;.    H
2cbbf 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 64 62  ash *pHash = &db
2cbc0 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
2cbc1 6d 61 2d 3e 74 72 69 67 48 61 73 68 3b 0a 20 20  ma->trigHash;.  
2cbc2 20 20 70 54 72 69 67 20 3d 20 73 71 6c 69 74 65    pTrig = sqlite
2cbc3 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61 73  3HashInsert(pHas
2cbc4 68 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65  h, zName, sqlite
2cbc5 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
2cbc6 2c 20 70 54 72 69 67 29 3b 0a 20 20 20 20 69 66  , pTrig);.    if
2cbc7 28 20 70 54 72 69 67 20 29 7b 0a 20 20 20 20 20  ( pTrig ){.     
2cbc8 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2cbc9 64 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  d = 1;.    }else
2cbca 20 69 66 28 20 70 4c 69 6e 6b 2d 3e 70 53 63 68   if( pLink->pSch
2cbcb 65 6d 61 3d 3d 70 4c 69 6e 6b 2d 3e 70 54 61 62  ema==pLink->pTab
2cbcc 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20  Schema ){.      
2cbcd 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20  Table *pTab;.   
2cbce 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74     int n = sqlit
2cbcf 65 33 53 74 72 6c 65 6e 33 30 28 70 4c 69 6e 6b  e3Strlen30(pLink
2cbd0 2d 3e 74 61 62 6c 65 29 3b 0a 20 20 20 20 20 20  ->table);.      
2cbd1 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 48 61  pTab = sqlite3Ha
2cbd2 73 68 46 69 6e 64 28 26 70 4c 69 6e 6b 2d 3e 70  shFind(&pLink->p
2cbd3 54 61 62 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  TabSchema->tblHa
2cbd4 73 68 2c 20 70 4c 69 6e 6b 2d 3e 74 61 62 6c 65  sh, pLink->table
2cbd5 2c 20 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65  , n);.      asse
2cbd6 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20  rt( pTab!=0 );. 
2cbd7 20 20 20 20 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78       pLink->pNex
2cbd8 74 20 3d 20 70 54 61 62 2d 3e 70 54 72 69 67 67  t = pTab->pTrigg
2cbd9 65 72 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  er;.      pTab->
2cbda 70 54 72 69 67 67 65 72 20 3d 20 70 4c 69 6e 6b  pTrigger = pLink
2cbdb 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 74 72 69  ;.    }.  }..tri
2cbdc 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e  ggerfinish_clean
2cbdd 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 44 65 6c  up:.  sqlite3Del
2cbde 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70  eteTrigger(db, p
2cbdf 54 72 69 67 29 3b 0a 20 20 61 73 73 65 72 74 28  Trig);.  assert(
2cbe0 20 21 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72   !pParse->pNewTr
2cbe1 69 67 67 65 72 20 29 3b 0a 20 20 73 71 6c 69 74  igger );.  sqlit
2cbe2 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53  e3DeleteTriggerS
2cbe3 74 65 70 28 64 62 2c 20 70 53 74 65 70 4c 69 73  tep(db, pStepLis
2cbe4 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 75 72  t);.}../*.** Tur
2cbe5 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  n a SELECT state
2cbe6 6d 65 6e 74 20 28 74 68 61 74 20 74 68 65 20 70  ment (that the p
2cbe7 53 65 6c 65 63 74 20 70 61 72 61 6d 65 74 65 72  Select parameter
2cbe8 20 70 6f 69 6e 74 73 20 74 6f 29 20 69 6e 74 6f   points to) into
2cbe9 0a 2a 2a 20 61 20 74 72 69 67 67 65 72 20 73 74  .** a trigger st
2cbea 65 70 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  ep.  Return a po
2cbeb 69 6e 74 65 72 20 74 6f 20 61 20 54 72 69 67 67  inter to a Trigg
2cbec 65 72 53 74 65 70 20 73 74 72 75 63 74 75 72 65  erStep structure
2cbed 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73  ..**.** The pars
2cbee 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
2cbef 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20 66 69  utine when it fi
2cbf0 6e 64 73 20 61 20 53 45 4c 45 43 54 20 73 74 61  nds a SELECT sta
2cbf1 74 65 6d 65 6e 74 20 69 6e 0a 2a 2a 20 62 6f 64  tement in.** bod
2cbf2 79 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20  y of a TRIGGER. 
2cbf3 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
2cbf4 41 54 45 20 54 72 69 67 67 65 72 53 74 65 70 20  ATE TriggerStep 
2cbf5 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53  *sqlite3TriggerS
2cbf6 65 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65  electStep(sqlite
2cbf7 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70  3 *db, Select *p
2cbf8 53 65 6c 65 63 74 29 7b 0a 20 20 54 72 69 67 67  Select){.  Trigg
2cbf9 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72  erStep *pTrigger
2cbfa 53 74 65 70 20 3d 20 73 71 6c 69 74 65 33 44 62  Step = sqlite3Db
2cbfb 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2cbfc 69 7a 65 6f 66 28 54 72 69 67 67 65 72 53 74 65  izeof(TriggerSte
2cbfd 70 29 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67  p));.  if( pTrig
2cbfe 67 65 72 53 74 65 70 3d 3d 30 20 29 20 7b 0a 20  gerStep==0 ) {. 
2cbff 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2cc00 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65  Delete(db, pSele
2cc01 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ct);.    return 
2cc02 30 3b 0a 20 20 7d 0a 20 20 70 54 72 69 67 67 65  0;.  }.  pTrigge
2cc03 72 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 53  rStep->op = TK_S
2cc04 45 4c 45 43 54 3b 0a 20 20 70 54 72 69 67 67 65  ELECT;.  pTrigge
2cc05 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d  rStep->pSelect =
2cc06 20 70 53 65 6c 65 63 74 3b 0a 20 20 70 54 72 69   pSelect;.  pTri
2cc07 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66  ggerStep->orconf
2cc08 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b 0a 20   = OE_Default;. 
2cc09 20 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72   return pTrigger
2cc0a 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  Step;.}../*.** A
2cc0b 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 74 6f  llocate space to
2cc0c 20 68 6f 6c 64 20 61 20 6e 65 77 20 74 72 69 67   hold a new trig
2cc0d 67 65 72 20 73 74 65 70 2e 20 20 54 68 65 20 61  ger step.  The a
2cc0e 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 0a 2a  llocated space.*
2cc0f 2a 20 68 6f 6c 64 73 20 62 6f 74 68 20 74 68 65  * holds both the
2cc10 20 54 72 69 67 67 65 72 53 74 65 70 20 6f 62 6a   TriggerStep obj
2cc11 65 63 74 20 61 6e 64 20 74 68 65 20 54 72 69 67  ect and the Trig
2cc12 67 65 72 53 74 65 70 2e 74 61 72 67 65 74 2e 7a  gerStep.target.z
2cc13 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49   string..**.** I
2cc14 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f  f an OOM error o
2cc15 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72  ccurs, NULL is r
2cc16 65 74 75 72 6e 65 64 20 61 6e 64 20 64 62 2d 3e  eturned and db->
2cc17 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20  mallocFailed is 
2cc18 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54  set..*/.static T
2cc19 72 69 67 67 65 72 53 74 65 70 20 2a 74 72 69 67  riggerStep *trig
2cc1a 67 65 72 53 74 65 70 41 6c 6c 6f 63 61 74 65 28  gerStepAllocate(
2cc1b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2cc1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cc1d 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
2cc1e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 6f 70  ction */.  u8 op
2cc1f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2cc20 20 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65         /* Trigge
2cc21 72 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 54 6f  r opcode */.  To
2cc22 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20  ken *pName      
2cc23 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2cc24 20 74 61 72 67 65 74 20 6e 61 6d 65 20 2a 2f 0a   target name */.
2cc25 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  ){.  TriggerStep
2cc26 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a   *pTriggerStep;.
2cc27 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 20  .  pTriggerStep 
2cc28 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2cc29 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2cc2a 28 54 72 69 67 67 65 72 53 74 65 70 29 20 2b 20  (TriggerStep) + 
2cc2b 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 69 66 28  pName->n);.  if(
2cc2c 20 70 54 72 69 67 67 65 72 53 74 65 70 20 29 7b   pTriggerStep ){
2cc2d 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 28  .    char *z = (
2cc2e 63 68 61 72 2a 29 26 70 54 72 69 67 67 65 72 53  char*)&pTriggerS
2cc2f 74 65 70 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63  tep[1];.    memc
2cc30 70 79 28 7a 2c 20 70 4e 61 6d 65 2d 3e 7a 2c 20  py(z, pName->z, 
2cc31 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 70  pName->n);.    p
2cc32 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72  TriggerStep->tar
2cc33 67 65 74 2e 7a 20 3d 20 7a 3b 0a 20 20 20 20 70  get.z = z;.    p
2cc34 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72  TriggerStep->tar
2cc35 67 65 74 2e 6e 20 3d 20 70 4e 61 6d 65 2d 3e 6e  get.n = pName->n
2cc36 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  ;.    pTriggerSt
2cc37 65 70 2d 3e 6f 70 20 3d 20 6f 70 3b 0a 20 20 7d  ep->op = op;.  }
2cc38 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67  .  return pTrigg
2cc39 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  erStep;.}../*.**
2cc3a 20 42 75 69 6c 64 20 61 20 74 72 69 67 67 65 72   Build a trigger
2cc3b 20 73 74 65 70 20 6f 75 74 20 6f 66 20 61 6e 20   step out of an 
2cc3c 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
2cc3d 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
2cc3e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 65  ter.** to the ne
2cc3f 77 20 74 72 69 67 67 65 72 20 73 74 65 70 2e 0a  w trigger step..
2cc40 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  **.** The parser
2cc41 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2cc42 69 6e 65 20 77 68 65 6e 20 69 74 20 73 65 65 73  ine when it sees
2cc43 20 61 6e 20 49 4e 53 45 52 54 20 69 6e 73 69 64   an INSERT insid
2cc44 65 20 74 68 65 0a 2a 2a 20 62 6f 64 79 20 6f 66  e the.** body of
2cc45 20 61 20 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53   a trigger..*/.S
2cc46 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72  QLITE_PRIVATE Tr
2cc47 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74  iggerStep *sqlit
2cc48 65 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53  e3TriggerInsertS
2cc49 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  tep(.  sqlite3 *
2cc4a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  db,        /* Th
2cc4b 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
2cc4c 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ction */.  Token
2cc4d 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 2f   *pTableName,  /
2cc4e 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
2cc4f 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
2cc50 65 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 49 64  e insert */.  Id
2cc51 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20  List *pColumn,  
2cc52 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c    /* List of col
2cc53 75 6d 6e 73 20 69 6e 20 70 54 61 62 6c 65 4e 61  umns in pTableNa
2cc54 6d 65 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74  me to insert int
2cc55 6f 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  o */.  ExprList 
2cc56 2a 70 45 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68  *pEList,   /* Th
2cc57 65 20 56 41 4c 55 45 20 63 6c 61 75 73 65 3a 20  e VALUE clause: 
2cc58 61 20 6c 69 73 74 20 6f 66 20 76 61 6c 75 65 73  a list of values
2cc59 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20   to be inserted 
2cc5a 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
2cc5b 6c 65 63 74 2c 20 20 20 20 2f 2a 20 41 20 53 45  lect,    /* A SE
2cc5c 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
2cc5d 68 61 74 20 73 75 70 70 6c 69 65 73 20 76 61 6c  hat supplies val
2cc5e 75 65 73 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f  ues */.  u8 orco
2cc5f 6e 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nf           /* 
2cc60 54 68 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67  The conflict alg
2cc61 6f 72 69 74 68 6d 20 28 4f 45 5f 41 62 6f 72 74  orithm (OE_Abort
2cc62 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 65 74  , OE_Replace, et
2cc63 63 2e 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67  c.) */.){.  Trig
2cc64 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65  gerStep *pTrigge
2cc65 72 53 74 65 70 3b 0a 0a 20 20 61 73 73 65 72 74  rStep;..  assert
2cc66 28 70 45 4c 69 73 74 20 3d 3d 20 30 20 7c 7c 20  (pEList == 0 || 
2cc67 70 53 65 6c 65 63 74 20 3d 3d 20 30 29 3b 0a 20  pSelect == 0);. 
2cc68 20 61 73 73 65 72 74 28 70 45 4c 69 73 74 20 21   assert(pEList !
2cc69 3d 20 30 20 7c 7c 20 70 53 65 6c 65 63 74 20 21  = 0 || pSelect !
2cc6a 3d 20 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  = 0 || db->mallo
2cc6b 63 46 61 69 6c 65 64 29 3b 0a 0a 20 20 70 54 72  cFailed);..  pTr
2cc6c 69 67 67 65 72 53 74 65 70 20 3d 20 74 72 69 67  iggerStep = trig
2cc6d 67 65 72 53 74 65 70 41 6c 6c 6f 63 61 74 65 28  gerStepAllocate(
2cc6e 64 62 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 70  db, TK_INSERT, p
2cc6f 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66  TableName);.  if
2cc70 28 20 70 54 72 69 67 67 65 72 53 74 65 70 20 29  ( pTriggerStep )
2cc71 7b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  {.    pTriggerSt
2cc72 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71  ep->pSelect = sq
2cc73 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
2cc74 62 2c 20 70 53 65 6c 65 63 74 2c 20 45 58 50 52  b, pSelect, EXPR
2cc75 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
2cc76 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
2cc77 49 64 4c 69 73 74 20 3d 20 70 43 6f 6c 75 6d 6e  IdList = pColumn
2cc78 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  ;.    pTriggerSt
2cc79 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 20 3d 20  ep->pExprList = 
2cc7a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2cc7b 75 70 28 64 62 2c 20 70 45 4c 69 73 74 2c 20 45  up(db, pEList, E
2cc7c 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
2cc7d 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70      pTriggerStep
2cc7e 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e  ->orconf = orcon
2cc7f 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  f;.  }else{.    
2cc80 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
2cc81 65 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29  ete(db, pColumn)
2cc82 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
2cc83 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2cc84 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c  , pEList);.  sql
2cc85 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
2cc86 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 0a  (db, pSelect);..
2cc87 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67 65    return pTrigge
2cc88 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rStep;.}../*.** 
2cc89 43 6f 6e 73 74 72 75 63 74 20 61 20 74 72 69 67  Construct a trig
2cc8a 67 65 72 20 73 74 65 70 20 74 68 61 74 20 69 6d  ger step that im
2cc8b 70 6c 65 6d 65 6e 74 73 20 61 6e 20 55 50 44 41  plements an UPDA
2cc8c 54 45 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64  TE statement and
2cc8d 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   return.** a poi
2cc8e 6e 74 65 72 20 74 6f 20 74 68 61 74 20 74 72 69  nter to that tri
2cc8f 67 67 65 72 20 73 74 65 70 2e 20 20 54 68 65 20  gger step.  The 
2cc90 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
2cc91 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69  s routine when i
2cc92 74 0a 2a 2a 20 73 65 65 73 20 61 6e 20 55 50 44  t.** sees an UPD
2cc93 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  ATE statement in
2cc94 73 69 64 65 20 74 68 65 20 62 6f 64 79 20 6f 66  side the body of
2cc95 20 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45   a CREATE TRIGGE
2cc96 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  R..*/.SQLITE_PRI
2cc97 56 41 54 45 20 54 72 69 67 67 65 72 53 74 65 70  VATE TriggerStep
2cc98 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72   *sqlite3Trigger
2cc99 55 70 64 61 74 65 53 74 65 70 28 0a 20 20 73 71  UpdateStep(.  sq
2cc9a 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2cc9b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2cc9c 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2cc9d 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65  .  Token *pTable
2cc9e 4e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20  Name,   /* Name 
2cc9f 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
2cca0 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20  be updated */.  
2cca1 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2cca2 2c 20 20 20 20 2f 2a 20 54 68 65 20 53 45 54 20  ,    /* The SET 
2cca3 63 6c 61 75 73 65 3a 20 6c 69 73 74 20 6f 66 20  clause: list of 
2cca4 63 6f 6c 75 6d 6e 20 61 6e 64 20 6e 65 77 20 76  column and new v
2cca5 61 6c 75 65 73 20 2a 2f 0a 20 20 45 78 70 72 20  alues */.  Expr 
2cca6 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20  *pWhere,        
2cca7 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2cca8 75 73 65 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f  use */.  u8 orco
2cca9 6e 66 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  nf            /*
2ccaa 20 54 68 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c   The conflict al
2ccab 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f  gorithm. (OE_Abo
2ccac 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 65  rt, OE_Ignore, e
2ccad 74 63 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67  tc) */.){.  Trig
2ccae 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65  gerStep *pTrigge
2ccaf 72 53 74 65 70 3b 0a 0a 20 20 70 54 72 69 67 67  rStep;..  pTrigg
2ccb0 65 72 53 74 65 70 20 3d 20 74 72 69 67 67 65 72  erStep = trigger
2ccb1 53 74 65 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c  StepAllocate(db,
2ccb2 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 54 61 62   TK_UPDATE, pTab
2ccb3 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70  leName);.  if( p
2ccb4 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20  TriggerStep ){. 
2ccb5 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
2ccb6 3e 70 45 78 70 72 4c 69 73 74 20 3d 20 73 71 6c  >pExprList = sql
2ccb7 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
2ccb8 64 62 2c 20 70 45 4c 69 73 74 2c 20 45 58 50 52  db, pEList, EXPR
2ccb9 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
2ccba 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
2ccbb 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
2ccbc 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65 72  xprDup(db, pWher
2ccbd 65 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43  e, EXPRDUP_REDUC
2ccbe 45 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  E);.    pTrigger
2ccbf 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f  Step->orconf = o
2ccc0 72 63 6f 6e 66 3b 0a 20 20 7d 0a 20 20 73 71 6c  rconf;.  }.  sql
2ccc1 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2ccc2 74 65 28 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a  te(db, pEList);.
2ccc3 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2ccc4 65 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b  ete(db, pWhere);
2ccc5 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67  .  return pTrigg
2ccc6 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  erStep;.}../*.**
2ccc7 20 43 6f 6e 73 74 72 75 63 74 20 61 20 74 72 69   Construct a tri
2ccc8 67 67 65 72 20 73 74 65 70 20 74 68 61 74 20 69  gger step that i
2ccc9 6d 70 6c 65 6d 65 6e 74 73 20 61 20 44 45 4c 45  mplements a DELE
2ccca 54 45 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64  TE statement and
2cccb 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   return.** a poi
2cccc 6e 74 65 72 20 74 6f 20 74 68 61 74 20 74 72 69  nter to that tri
2cccd 67 67 65 72 20 73 74 65 70 2e 20 20 54 68 65 20  gger step.  The 
2ccce 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
2cccf 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69  s routine when i
2ccd0 74 0a 2a 2a 20 73 65 65 73 20 61 20 44 45 4c 45  t.** sees a DELE
2ccd1 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 73  TE statement ins
2ccd2 69 64 65 20 74 68 65 20 62 6f 64 79 20 6f 66 20  ide the body of 
2ccd3 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  a CREATE TRIGGER
2ccd4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2ccd5 41 54 45 20 54 72 69 67 67 65 72 53 74 65 70 20  ATE TriggerStep 
2ccd6 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 44  *sqlite3TriggerD
2ccd7 65 6c 65 74 65 53 74 65 70 28 0a 20 20 73 71 6c  eleteStep(.  sql
2ccd8 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2ccd9 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2ccda 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
2ccdb 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61   Token *pTableNa
2ccdc 6d 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  me,      /* The 
2ccdd 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68  table from which
2ccde 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65   rows are delete
2ccdf 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  d */.  Expr *pWh
2cce0 65 72 65 20 20 20 20 20 20 20 20 20 20 20 20 2f  ere            /
2cce1 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
2cce2 73 65 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67  se */.){.  Trigg
2cce3 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72  erStep *pTrigger
2cce4 53 74 65 70 3b 0a 0a 20 20 70 54 72 69 67 67 65  Step;..  pTrigge
2cce5 72 53 74 65 70 20 3d 20 74 72 69 67 67 65 72 53  rStep = triggerS
2cce6 74 65 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c 20  tepAllocate(db, 
2cce7 54 4b 5f 44 45 4c 45 54 45 2c 20 70 54 61 62 6c  TK_DELETE, pTabl
2cce8 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 54  eName);.  if( pT
2cce9 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20  riggerStep ){.  
2ccea 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
2cceb 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  pWhere = sqlite3
2ccec 45 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65  ExprDup(db, pWhe
2cced 72 65 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  re, EXPRDUP_REDU
2ccee 43 45 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65  CE);.    pTrigge
2ccef 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20  rStep->orconf = 
2ccf0 4f 45 5f 44 65 66 61 75 6c 74 3b 0a 20 20 7d 0a  OE_Default;.  }.
2ccf1 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2ccf2 65 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b  ete(db, pWhere);
2ccf3 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67  .  return pTrigg
2ccf4 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  erStep;.}../* .*
2ccf5 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 64 65  * Recursively de
2ccf6 6c 65 74 65 20 61 20 54 72 69 67 67 65 72 20 73  lete a Trigger s
2ccf7 74 72 75 63 74 75 72 65 0a 2a 2f 0a 53 51 4c 49  tructure.*/.SQLI
2ccf8 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2ccf9 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
2ccfa 67 67 65 72 28 73 71 6c 69 74 65 33 20 2a 64 62  gger(sqlite3 *db
2ccfb 2c 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67  , Trigger *pTrig
2ccfc 67 65 72 29 7b 0a 20 20 69 66 28 20 70 54 72 69  ger){.  if( pTri
2ccfd 67 67 65 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  gger==0 ) return
2ccfe 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74  ;.  sqlite3Delet
2ccff 65 54 72 69 67 67 65 72 53 74 65 70 28 64 62 2c  eTriggerStep(db,
2cd00 20 70 54 72 69 67 67 65 72 2d 3e 73 74 65 70 5f   pTrigger->step_
2cd01 6c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  list);.  sqlite3
2cd02 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69 67  DbFree(db, pTrig
2cd03 67 65 72 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73  ger->zName);.  s
2cd04 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2cd05 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65   pTrigger->table
2cd06 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2cd07 44 65 6c 65 74 65 28 64 62 2c 20 70 54 72 69 67  Delete(db, pTrig
2cd08 67 65 72 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 73  ger->pWhen);.  s
2cd09 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
2cd0a 74 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d  te(db, pTrigger-
2cd0b 3e 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71  >pColumns);.  sq
2cd0c 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2cd0d 70 54 72 69 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a  pTrigger);.}../*
2cd0e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2cd0f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 64  n is called to d
2cd10 72 6f 70 20 61 20 74 72 69 67 67 65 72 20 66 72  rop a trigger fr
2cd11 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
2cd12 73 63 68 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 54  schema. .**.** T
2cd13 68 69 73 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  his may be calle
2cd14 64 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20  d directly from 
2cd15 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 74  the parser and t
2cd16 68 65 72 65 66 6f 72 65 20 69 64 65 6e 74 69 66  herefore identif
2cd17 69 65 73 0a 2a 2a 20 74 68 65 20 74 72 69 67 67  ies.** the trigg
2cd18 65 72 20 62 79 20 6e 61 6d 65 2e 20 20 54 68 65  er by name.  The
2cd19 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
2cd1a 67 65 72 50 74 72 28 29 20 72 6f 75 74 69 6e 65  gerPtr() routine
2cd1b 20 64 6f 65 73 20 74 68 65 0a 2a 2a 20 73 61 6d   does the.** sam
2cd1c 65 20 6a 6f 62 20 61 73 20 74 68 69 73 20 72 6f  e job as this ro
2cd1d 75 74 69 6e 65 20 65 78 63 65 70 74 20 69 74 20  utine except it 
2cd1e 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20  takes a pointer 
2cd1f 74 6f 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a  to the trigger.*
2cd20 2a 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  * instead of the
2cd21 20 74 72 69 67 67 65 72 20 6e 61 6d 65 2e 0a 2a   trigger name..*
2cd22 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2cd23 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72  E void sqlite3Dr
2cd24 6f 70 54 72 69 67 67 65 72 28 50 61 72 73 65 20  opTrigger(Parse 
2cd25 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74  *pParse, SrcList
2cd26 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 6e 6f 45   *pName, int noE
2cd27 72 72 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a  rr){.  Trigger *
2cd28 70 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20  pTrigger = 0;.  
2cd29 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63  int i;.  const c
2cd2a 68 61 72 20 2a 7a 44 62 3b 0a 20 20 63 6f 6e 73  har *zDb;.  cons
2cd2b 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20  t char *zName;. 
2cd2c 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 73 71   int nName;.  sq
2cd2d 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2cd2e 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 64  se->db;..  if( d
2cd2f 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2cd30 29 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67  ) goto drop_trig
2cd31 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69  ger_cleanup;.  i
2cd32 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
2cd33 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
2cd34 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 67  pParse) ){.    g
2cd35 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65 72  oto drop_trigger
2cd36 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
2cd37 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e   assert( pName->
2cd38 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 44 62  nSrc==1 );.  zDb
2cd39 20 3d 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a   = pName->a[0].z
2cd3a 44 61 74 61 62 61 73 65 3b 0a 20 20 7a 4e 61 6d  Database;.  zNam
2cd3b 65 20 3d 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e  e = pName->a[0].
2cd3c 7a 4e 61 6d 65 3b 0a 20 20 6e 4e 61 6d 65 20 3d  zName;.  nName =
2cd3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2cd3e 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69  (zName);.  for(i
2cd3f 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c  =OMIT_TEMPDB; i<
2cd40 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
2cd41 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29     int j = (i<2)
2cd42 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20   ? i^1 : i;  /* 
2cd43 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f  Search TEMP befo
2cd44 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 69  re MAIN */.    i
2cd45 66 28 20 7a 44 62 20 26 26 20 73 71 6c 69 74 65  f( zDb && sqlite
2cd46 33 53 74 72 49 43 6d 70 28 64 62 2d 3e 61 44 62  3StrICmp(db->aDb
2cd47 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20  [j].zName, zDb) 
2cd48 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2cd49 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  pTrigger = sqlit
2cd4a 65 33 48 61 73 68 46 69 6e 64 28 26 28 64 62 2d  e3HashFind(&(db-
2cd4b 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d  >aDb[j].pSchema-
2cd4c 3e 74 72 69 67 48 61 73 68 29 2c 20 7a 4e 61 6d  >trigHash), zNam
2cd4d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69  e, nName);.    i
2cd4e 66 28 20 70 54 72 69 67 67 65 72 20 29 20 62 72  f( pTrigger ) br
2cd4f 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  eak;.  }.  if( !
2cd50 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
2cd51 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20  if( !noErr ){.  
2cd52 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2cd53 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
2cd54 73 75 63 68 20 74 72 69 67 67 65 72 3a 20 25 53  such trigger: %S
2cd55 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  ", pName, 0);.  
2cd56 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 64 72 6f    }.    goto dro
2cd57 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  p_trigger_cleanu
2cd58 70 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  p;.  }.  sqlite3
2cd59 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 70  DropTriggerPtr(p
2cd5a 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 29  Parse, pTrigger)
2cd5b 3b 0a 0a 64 72 6f 70 5f 74 72 69 67 67 65 72 5f  ;..drop_trigger_
2cd5c 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74  cleanup:.  sqlit
2cd5d 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
2cd5e 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f  db, pName);.}../
2cd5f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
2cd60 69 6e 74 65 72 20 74 6f 20 74 68 65 20 54 61 62  inter to the Tab
2cd61 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  le structure for
2cd62 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
2cd63 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20  a trigger.** is 
2cd64 73 65 74 20 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  set on..*/.stati
2cd65 63 20 54 61 62 6c 65 20 2a 74 61 62 6c 65 4f 66  c Table *tableOf
2cd66 54 72 69 67 67 65 72 28 54 72 69 67 67 65 72 20  Trigger(Trigger 
2cd67 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 69 6e  *pTrigger){.  in
2cd68 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  t n = sqlite3Str
2cd69 6c 65 6e 33 30 28 70 54 72 69 67 67 65 72 2d 3e  len30(pTrigger->
2cd6a 74 61 62 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e  table);.  return
2cd6b 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
2cd6c 28 26 70 54 72 69 67 67 65 72 2d 3e 70 54 61 62  (&pTrigger->pTab
2cd6d 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c  Schema->tblHash,
2cd6e 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65   pTrigger->table
2cd6f 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  , n);.}.../*.** 
2cd70 44 72 6f 70 20 61 20 74 72 69 67 67 65 72 20 67  Drop a trigger g
2cd71 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  iven a pointer t
2cd72 6f 20 74 68 61 74 20 74 72 69 67 67 65 72 2e 20  o that trigger. 
2cd73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2cd74 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
2cd75 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 50 61  ropTriggerPtr(Pa
2cd76 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 72 69  rse *pParse, Tri
2cd77 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 29 7b  gger *pTrigger){
2cd78 0a 20 20 54 61 62 6c 65 20 20 20 2a 70 54 61 62  .  Table   *pTab
2cd79 6c 65 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  le;.  Vdbe *v;. 
2cd7a 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2cd7b 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
2cd7c 20 69 44 62 3b 0a 0a 20 20 69 44 62 20 3d 20 73   iDb;..  iDb = s
2cd7d 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2cd7e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
2cd7f 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d  pTrigger->pSchem
2cd80 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  a);.  assert( iD
2cd81 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e  b>=0 && iDb<db->
2cd82 6e 44 62 20 29 3b 0a 20 20 70 54 61 62 6c 65 20  nDb );.  pTable 
2cd83 3d 20 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72  = tableOfTrigger
2cd84 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20 61 73  (pTrigger);.  as
2cd85 73 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a  sert( pTable );.
2cd86 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
2cd87 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 72 69 67  ->pSchema==pTrig
2cd88 67 65 72 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20  ger->pSchema || 
2cd89 69 44 62 3d 3d 31 20 29 3b 0a 23 69 66 6e 64 65  iDb==1 );.#ifnde
2cd8a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
2cd8b 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a  THORIZATION.  {.
2cd8c 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53      int code = S
2cd8d 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47  QLITE_DROP_TRIGG
2cd8e 45 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  ER;.    const ch
2cd8f 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
2cd90 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
2cd91 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2cd92 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c  ab = SCHEMA_TABL
2cd93 45 28 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20  E(iDb);.    if( 
2cd94 69 44 62 3d 3d 31 20 29 20 63 6f 64 65 20 3d 20  iDb==1 ) code = 
2cd95 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
2cd96 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20 69 66  _TRIGGER;.    if
2cd97 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
2cd98 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c  ck(pParse, code,
2cd99 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
2cd9a 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c  , pTable->zName,
2cd9b 20 7a 44 62 29 20 7c 7c 0a 20 20 20 20 20 20 73   zDb) ||.      s
2cd9c 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2cd9d 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44  pParse, SQLITE_D
2cd9e 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20  ELETE, zTab, 0, 
2cd9f 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 72 65  zDb) ){.      re
2cda0 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
2cda1 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e  #endif..  /* Gen
2cda2 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 65  erate code to de
2cda3 73 74 72 6f 79 20 74 68 65 20 64 61 74 61 62 61  stroy the databa
2cda4 73 65 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65  se record of the
2cda5 20 74 72 69 67 67 65 72 2e 0a 20 20 2a 2f 0a 20   trigger..  */. 
2cda6 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 21   assert( pTable!
2cda7 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 76 20 3d  =0 );.  if( (v =
2cda8 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2cda9 70 50 61 72 73 65 29 29 21 3d 30 20 29 7b 0a 20  pParse))!=0 ){. 
2cdaa 20 20 20 69 6e 74 20 62 61 73 65 3b 0a 20 20 20     int base;.   
2cdab 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64   static const Vd
2cdac 62 65 4f 70 4c 69 73 74 20 64 72 6f 70 54 72 69  beOpList dropTri
2cdad 67 67 65 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  gger[] = {.     
2cdae 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20   { OP_Rewind,   
2cdaf 20 20 30 2c 20 41 44 44 52 28 39 29 2c 20 20 30    0, ADDR(9),  0
2cdb0 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  },.      { OP_St
2cdb1 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 31 2c 20  ring8,    0, 1, 
2cdb2 20 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 31 20         0}, /* 1 
2cdb3 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f  */.      { OP_Co
2cdb4 6c 75 6d 6e 2c 20 20 20 20 20 30 2c 20 31 2c 20  lumn,     0, 1, 
2cdb5 20 20 20 20 20 20 20 32 7d 2c 0a 20 20 20 20 20         2},.     
2cdb6 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20 20 20 20   { OP_Ne,       
2cdb7 20 20 32 2c 20 41 44 44 52 28 38 29 2c 20 20 31    2, ADDR(8),  1
2cdb8 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  },.      { OP_St
2cdb9 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 31 2c 20  ring8,    0, 1, 
2cdba 20 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 34 3a         0}, /* 4:
2cdbb 20 22 74 72 69 67 67 65 72 22 20 2a 2f 0a 20 20   "trigger" */.  
2cdbc 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c      { OP_Column,
2cdbd 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20       0, 0,      
2cdbe 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    2},.      { OP
2cdbf 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32 2c 20  _Ne,         2, 
2cdc0 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20  ADDR(8),  1},.  
2cdc1 20 20 20 20 7b 20 4f 50 5f 44 65 6c 65 74 65 2c      { OP_Delete,
2cdc2 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20       0, 0,      
2cdc3 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    0},.      { OP
2cdc4 5f 4e 65 78 74 2c 20 20 20 20 20 20 20 30 2c 20  _Next,       0, 
2cdc5 41 44 44 52 28 31 29 2c 20 20 30 7d 2c 20 2f 2a  ADDR(1),  0}, /*
2cdc6 20 38 20 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20   8 */.    };..  
2cdc7 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
2cdc8 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
2cdc9 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20  rse, 0, iDb);.  
2cdca 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73    sqlite3OpenMas
2cdcb 74 65 72 54 61 62 6c 65 28 70 50 61 72 73 65 2c  terTable(pParse,
2cdcc 20 69 44 62 29 3b 0a 20 20 20 20 62 61 73 65 20   iDb);.    base 
2cdcd 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2cdce 4f 70 4c 69 73 74 28 76 2c 20 20 41 72 72 61 79  OpList(v,  Array
2cdcf 53 69 7a 65 28 64 72 6f 70 54 72 69 67 67 65 72  Size(dropTrigger
2cdd0 29 2c 20 64 72 6f 70 54 72 69 67 67 65 72 29 3b  ), dropTrigger);
2cdd1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2cdd2 43 68 61 6e 67 65 50 34 28 76 2c 20 62 61 73 65  ChangeP4(v, base
2cdd3 2b 31 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e  +1, pTrigger->zN
2cdd4 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  ame, 0);.    sql
2cdd5 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
2cdd6 28 76 2c 20 62 61 73 65 2b 34 2c 20 22 74 72 69  (v, base+4, "tri
2cdd7 67 67 65 72 22 2c 20 50 34 5f 53 54 41 54 49 43  gger", P4_STATIC
2cdd8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
2cdd9 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
2cdda 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  e, iDb);.    sql
2cddb 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2cddc 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30  , OP_Close, 0, 0
2cddd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2cdde 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44  beAddOp4(v, OP_D
2cddf 72 6f 70 54 72 69 67 67 65 72 2c 20 69 44 62 2c  ropTrigger, iDb,
2cde0 20 30 2c 20 30 2c 20 70 54 72 69 67 67 65 72 2d   0, 0, pTrigger-
2cde1 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
2cde2 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  if( pParse->nMem
2cde3 3c 33 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  <3 ){.      pPar
2cde4 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20  se->nMem = 3;.  
2cde5 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2cde6 20 52 65 6d 6f 76 65 20 61 20 74 72 69 67 67 65   Remove a trigge
2cde7 72 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  r from the hash 
2cde8 74 61 62 6c 65 73 20 6f 66 20 74 68 65 20 73 71  tables of the sq
2cde9 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 2e 0a 2a  lite* pointer..*
2cdea 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2cdeb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c   void sqlite3Unl
2cdec 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67  inkAndDeleteTrig
2cded 67 65 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ger(sqlite3 *db,
2cdee 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20   int iDb, const 
2cdef 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
2cdf0 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 28  Hash *pHash = &(
2cdf1 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
2cdf2 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 3b  hema->trigHash);
2cdf3 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2cdf4 67 67 65 72 3b 0a 20 20 70 54 72 69 67 67 65 72  gger;.  pTrigger
2cdf5 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e   = sqlite3HashIn
2cdf6 73 65 72 74 28 70 48 61 73 68 2c 20 7a 4e 61 6d  sert(pHash, zNam
2cdf7 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  e, sqlite3Strlen
2cdf8 33 30 28 7a 4e 61 6d 65 29 2c 20 30 29 3b 0a 20  30(zName), 0);. 
2cdf9 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 72 69   if( ALWAYS(pTri
2cdfa 67 67 65 72 29 20 29 7b 0a 20 20 20 20 69 66 28  gger) ){.    if(
2cdfb 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65   pTrigger->pSche
2cdfc 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d 3e 70 54  ma==pTrigger->pT
2cdfd 61 62 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20  abSchema ){.    
2cdfe 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2cdff 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70  tableOfTrigger(p
2ce00 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20  Trigger);.      
2ce01 54 72 69 67 67 65 72 20 2a 2a 70 70 3b 0a 20 20  Trigger **pp;.  
2ce02 20 20 20 20 66 6f 72 28 70 70 3d 26 70 54 61 62      for(pp=&pTab
2ce03 2d 3e 70 54 72 69 67 67 65 72 3b 20 2a 70 70 21  ->pTrigger; *pp!
2ce04 3d 70 54 72 69 67 67 65 72 3b 20 70 70 3d 26 28  =pTrigger; pp=&(
2ce05 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b 0a  (*pp)->pNext));.
2ce06 20 20 20 20 20 20 2a 70 70 20 3d 20 28 2a 70 70        *pp = (*pp
2ce07 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  )->pNext;.    }.
2ce08 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
2ce09 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 54 72  eTrigger(db, pTr
2ce0a 69 67 67 65 72 29 3b 0a 20 20 20 20 64 62 2d 3e  igger);.    db->
2ce0b 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
2ce0c 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
2ce0d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69   }.}../*.** pELi
2ce0e 73 74 20 69 73 20 74 68 65 20 53 45 54 20 63 6c  st is the SET cl
2ce0f 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 54  ause of an UPDAT
2ce10 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 45 61  E statement.  Ea
2ce11 63 68 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70  ch entry.** in p
2ce12 45 4c 69 73 74 20 69 73 20 6f 66 20 74 68 65 20  EList is of the 
2ce13 66 6f 72 6d 61 74 20 3c 69 64 3e 3d 3c 65 78 70  format <id>=<exp
2ce14 72 3e 2e 20 20 49 66 20 61 6e 79 20 6f 66 20 74  r>.  If any of t
2ce15 68 65 20 65 6e 74 72 69 65 73 0a 2a 2a 20 69 6e  he entries.** in
2ce16 20 70 45 4c 69 73 74 20 68 61 76 65 20 61 6e 20   pEList have an 
2ce17 3c 69 64 3e 20 77 68 69 63 68 20 6d 61 74 63 68  <id> which match
2ce18 65 73 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  es an identifier
2ce19 20 69 6e 20 70 49 64 4c 69 73 74 2c 0a 2a 2a 20   in pIdList,.** 
2ce1a 74 68 65 6e 20 72 65 74 75 72 6e 20 54 52 55 45  then return TRUE
2ce1b 2e 20 20 49 66 20 70 49 64 4c 69 73 74 3d 3d 4e  .  If pIdList==N
2ce1c 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20  ULL, then it is 
2ce1d 63 6f 6e 73 69 64 65 72 65 64 20 61 0a 2a 2a 20  considered a.** 
2ce1e 77 69 6c 64 63 61 72 64 20 74 68 61 74 20 6d 61  wildcard that ma
2ce1f 74 63 68 65 73 20 61 6e 79 74 68 69 6e 67 2e 20  tches anything. 
2ce20 20 4c 69 6b 65 77 69 73 65 20 69 66 20 70 45 4c   Likewise if pEL
2ce21 69 73 74 3d 3d 4e 55 4c 4c 20 74 68 65 6e 0a 2a  ist==NULL then.*
2ce22 2a 20 69 74 20 6d 61 74 63 68 65 73 20 61 6e 79  * it matches any
2ce23 74 68 69 6e 67 20 73 6f 20 61 6c 77 61 79 73 20  thing so always 
2ce24 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20 52 65  return true.  Re
2ce25 74 75 72 6e 20 66 61 6c 73 65 20 6f 6e 6c 79 0a  turn false only.
2ce26 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  ** if there is n
2ce27 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 61 74  o match..*/.stat
2ce28 69 63 20 69 6e 74 20 63 68 65 63 6b 43 6f 6c 75  ic int checkColu
2ce29 6d 6e 4f 76 65 72 6c 61 70 28 49 64 4c 69 73 74  mnOverlap(IdList
2ce2a 20 2a 70 49 64 4c 69 73 74 2c 20 45 78 70 72 4c   *pIdList, ExprL
2ce2b 69 73 74 20 2a 70 45 4c 69 73 74 29 7b 0a 20 20  ist *pEList){.  
2ce2c 69 6e 74 20 65 3b 0a 20 20 69 66 28 20 70 49 64  int e;.  if( pId
2ce2d 4c 69 73 74 3d 3d 30 20 7c 7c 20 4e 45 56 45 52  List==0 || NEVER
2ce2e 28 70 45 4c 69 73 74 3d 3d 30 29 20 29 20 72 65  (pEList==0) ) re
2ce2f 74 75 72 6e 20 31 3b 0a 20 20 66 6f 72 28 65 3d  turn 1;.  for(e=
2ce30 30 3b 20 65 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; e<pEList->nEx
2ce31 70 72 3b 20 65 2b 2b 29 7b 0a 20 20 20 20 69 66  pr; e++){.    if
2ce32 28 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49  ( sqlite3IdListI
2ce33 6e 64 65 78 28 70 49 64 4c 69 73 74 2c 20 70 45  ndex(pIdList, pE
2ce34 4c 69 73 74 2d 3e 61 5b 65 5d 2e 7a 4e 61 6d 65  List->a[e].zName
2ce35 29 3e 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  )>=0 ) return 1;
2ce36 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
2ce37 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72   .}../*.** Retur
2ce38 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  n a list of all 
2ce39 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 6c  triggers on tabl
2ce3a 65 20 70 54 61 62 20 69 66 20 74 68 65 72 65 20  e pTab if there 
2ce3b 65 78 69 73 74 73 20 61 74 20 6c 65 61 73 74 0a  exists at least.
2ce3c 2a 2a 20 6f 6e 65 20 74 72 69 67 67 65 72 20 74  ** one trigger t
2ce3d 68 61 74 20 6d 75 73 74 20 62 65 20 66 69 72 65  hat must be fire
2ce3e 64 20 77 68 65 6e 20 61 6e 20 6f 70 65 72 61 74  d when an operat
2ce3f 69 6f 6e 20 6f 66 20 74 79 70 65 20 27 6f 70 27  ion of type 'op'
2ce40 20 69 73 20 0a 2a 2a 20 70 65 72 66 6f 72 6d 65   is .** performe
2ce41 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  d on the table, 
2ce42 61 6e 64 2c 20 69 66 20 74 68 61 74 20 6f 70 65  and, if that ope
2ce43 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 55 50 44  ration is an UPD
2ce44 41 54 45 2c 20 69 66 20 61 74 0a 2a 2a 20 6c 65  ATE, if at.** le
2ce45 61 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 63  ast one of the c
2ce46 6f 6c 75 6d 6e 73 20 69 6e 20 70 43 68 61 6e 67  olumns in pChang
2ce47 65 73 20 69 73 20 62 65 69 6e 67 20 6d 6f 64 69  es is being modi
2ce48 66 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  fied..*/.SQLITE_
2ce49 50 52 49 56 41 54 45 20 54 72 69 67 67 65 72 20  PRIVATE Trigger 
2ce4a 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73  *sqlite3Triggers
2ce4b 45 78 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a  Exist(.  Parse *
2ce4c 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2ce4d 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
2ce4e 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  t */.  Table *pT
2ce4f 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ab,            /
2ce50 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 65 20  * The table the 
2ce51 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 72 69  contains the tri
2ce52 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6f  ggers */.  int o
2ce53 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
2ce54 20 20 20 2f 2a 20 6f 6e 65 20 6f 66 20 54 4b 5f     /* one of TK_
2ce55 44 45 4c 45 54 45 2c 20 54 4b 5f 49 4e 53 45 52  DELETE, TK_INSER
2ce56 54 2c 20 54 4b 5f 55 50 44 41 54 45 20 2a 2f 0a  T, TK_UPDATE */.
2ce57 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61    ExprList *pCha
2ce58 6e 67 65 73 2c 20 20 20 20 20 2f 2a 20 43 6f 6c  nges,     /* Col
2ce59 75 6d 6e 73 20 74 68 61 74 20 63 68 61 6e 67 65  umns that change
2ce5a 20 69 6e 20 61 6e 20 55 50 44 41 54 45 20 73 74   in an UPDATE st
2ce5b 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  atement */.  int
2ce5c 20 2a 70 4d 61 73 6b 20 20 20 20 20 20 20 20 20   *pMask         
2ce5d 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 73       /* OUT: Mas
2ce5e 6b 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46  k of TRIGGER_BEF
2ce5f 4f 52 45 7c 54 52 49 47 47 45 52 5f 41 46 54 45  ORE|TRIGGER_AFTE
2ce60 52 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6d 61  R */.){.  int ma
2ce61 73 6b 20 3d 20 30 3b 0a 20 20 54 72 69 67 67 65  sk = 0;.  Trigge
2ce62 72 20 2a 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  r *pList = sqlit
2ce63 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70 50  e3TriggerList(pP
2ce64 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 54  arse, pTab);.  T
2ce65 72 69 67 67 65 72 20 2a 70 3b 0a 20 20 61 73 73  rigger *p;.  ass
2ce66 65 72 74 28 20 70 4c 69 73 74 3d 3d 30 20 7c 7c  ert( pList==0 ||
2ce67 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
2ce68 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70  ==0 );.  for(p=p
2ce69 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  List; p; p=p->pN
2ce6a 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  ext){.    if( p-
2ce6b 3e 6f 70 3d 3d 6f 70 20 26 26 20 63 68 65 63 6b  >op==op && check
2ce6c 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70 28 70 2d  ColumnOverlap(p-
2ce6d 3e 70 43 6f 6c 75 6d 6e 73 2c 20 70 43 68 61 6e  >pColumns, pChan
2ce6e 67 65 73 29 20 29 7b 0a 20 20 20 20 20 20 6d 61  ges) ){.      ma
2ce6f 73 6b 20 7c 3d 20 70 2d 3e 74 72 5f 74 6d 3b 0a  sk |= p->tr_tm;.
2ce70 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
2ce71 70 4d 61 73 6b 20 29 7b 0a 20 20 20 20 2a 70 4d  pMask ){.    *pM
2ce72 61 73 6b 20 3d 20 6d 61 73 6b 3b 0a 20 20 7d 0a  ask = mask;.  }.
2ce73 20 20 72 65 74 75 72 6e 20 28 6d 61 73 6b 20 3f    return (mask ?
2ce74 20 70 4c 69 73 74 20 3a 20 30 29 3b 0a 7d 0a 0a   pList : 0);.}..
2ce75 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68  /*.** Convert th
2ce76 65 20 70 53 74 65 70 2d 3e 74 61 72 67 65 74 20  e pStep->target 
2ce77 74 6f 6b 65 6e 20 69 6e 74 6f 20 61 20 53 72 63  token into a Src
2ce78 4c 69 73 74 20 61 6e 64 20 72 65 74 75 72 6e 20  List and return 
2ce79 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
2ce7a 74 68 61 74 20 53 72 63 4c 69 73 74 2e 0a 2a 2a  that SrcList..**
2ce7b 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2ce7c 20 61 64 64 73 20 61 20 73 70 65 63 69 66 69 63   adds a specific
2ce7d 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20   database name, 
2ce7e 69 66 20 6e 65 65 64 65 64 2c 20 74 6f 20 74 68  if needed, to th
2ce7f 65 20 74 61 72 67 65 74 20 77 68 65 6e 0a 2a 2a  e target when.**
2ce80 20 66 6f 72 6d 69 6e 67 20 74 68 65 20 53 72 63   forming the Src
2ce81 4c 69 73 74 2e 20 20 54 68 69 73 20 70 72 65 76  List.  This prev
2ce82 65 6e 74 73 20 61 20 74 72 69 67 67 65 72 20 69  ents a trigger i
2ce83 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66  n one database f
2ce84 72 6f 6d 0a 2a 2a 20 72 65 66 65 72 72 69 6e 67  rom.** referring
2ce85 20 74 6f 20 61 20 74 61 72 67 65 74 20 69 6e 20   to a target in 
2ce86 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  another database
2ce87 2e 20 20 41 6e 20 65 78 63 65 70 74 69 6f 6e 20  .  An exception 
2ce88 69 73 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 74  is when the.** t
2ce89 72 69 67 67 65 72 20 69 73 20 69 6e 20 54 45 4d  rigger is in TEM
2ce8a 50 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  P in which case 
2ce8b 69 74 20 63 61 6e 20 72 65 66 65 72 20 74 6f 20  it can refer to 
2ce8c 61 6e 79 20 6f 74 68 65 72 20 64 61 74 61 62 61  any other databa
2ce8d 73 65 20 69 74 0a 2a 2a 20 77 61 6e 74 73 2e 0a  se it.** wants..
2ce8e 2a 2f 0a 73 74 61 74 69 63 20 53 72 63 4c 69 73  */.static SrcLis
2ce8f 74 20 2a 74 61 72 67 65 74 53 72 63 4c 69 73 74  t *targetSrcList
2ce90 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2ce91 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e,       /* The 
2ce92 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
2ce93 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  */.  TriggerStep
2ce94 20 2a 70 53 74 65 70 20 20 20 2f 2a 20 54 68 65   *pStep   /* The
2ce95 20 74 72 69 67 67 65 72 20 63 6f 6e 74 61 69 6e   trigger contain
2ce96 69 6e 67 20 74 68 65 20 74 61 72 67 65 74 20 74  ing the target t
2ce97 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  oken */.){.  int
2ce98 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
2ce99 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
2ce9a 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 75 73  e database to us
2ce9b 65 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  e */.  SrcList *
2ce9c 70 53 72 63 3b 20 20 20 20 20 20 20 2f 2a 20 53  pSrc;       /* S
2ce9d 72 63 4c 69 73 74 20 74 6f 20 62 65 20 72 65 74  rcList to be ret
2ce9e 75 72 6e 65 64 20 2a 2f 0a 0a 20 20 70 53 72 63  urned */..  pSrc
2ce9f 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
2cea0 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
2cea1 64 62 2c 20 30 2c 20 26 70 53 74 65 70 2d 3e 74  db, 0, &pStep->t
2cea2 61 72 67 65 74 2c 20 30 29 3b 0a 20 20 69 66 28  arget, 0);.  if(
2cea3 20 70 53 72 63 20 29 7b 0a 20 20 20 20 61 73 73   pSrc ){.    ass
2cea4 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3e  ert( pSrc->nSrc>
2cea5 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
2cea6 20 70 53 72 63 2d 3e 61 21 3d 30 20 29 3b 0a 20   pSrc->a!=0 );. 
2cea7 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
2cea8 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
2cea9 61 72 73 65 2d 3e 64 62 2c 20 70 53 74 65 70 2d  arse->db, pStep-
2ceaa 3e 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29  >pTrig->pSchema)
2ceab 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3d 3d 30  ;.    if( iDb==0
2ceac 20 7c 7c 20 69 44 62 3e 3d 32 20 29 7b 0a 20 20   || iDb>=2 ){.  
2cead 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
2ceae 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2ceaf 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3c      assert( iDb<
2ceb0 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6e 44 62 20  pParse->db->nDb 
2ceb1 29 3b 0a 20 20 20 20 20 20 70 53 72 63 2d 3e 61  );.      pSrc->a
2ceb2 5b 70 53 72 63 2d 3e 6e 53 72 63 2d 31 5d 2e 7a  [pSrc->nSrc-1].z
2ceb3 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74  Database = sqlit
2ceb4 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 64  e3DbStrDup(db, d
2ceb5 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2ceb6 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
2ceb7 72 65 74 75 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a  return pSrc;.}..
2ceb8 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 56  /*.** Generate V
2ceb9 44 42 45 20 63 6f 64 65 20 66 6f 72 20 74 68 65  DBE code for the
2ceba 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69   statements insi
2cebb 64 65 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61  de the body of a
2cebc 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 74 72 69 67   single .** trig
2cebd 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ger..*/.static i
2cebe 6e 74 20 63 6f 64 65 54 72 69 67 67 65 72 50 72  nt codeTriggerPr
2cebf 6f 67 72 61 6d 28 0a 20 20 50 61 72 73 65 20 2a  ogram(.  Parse *
2cec0 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2cec1 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
2cec2 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72   context */.  Tr
2cec3 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70  iggerStep *pStep
2cec4 4c 69 73 74 2c 20 20 20 2f 2a 20 4c 69 73 74 20  List,   /* List 
2cec5 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  of statements in
2cec6 73 69 64 65 20 74 68 65 20 74 72 69 67 67 65 72  side the trigger
2cec7 20 62 6f 64 79 20 2a 2f 0a 20 20 69 6e 74 20 6f   body */.  int o
2cec8 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20 20  rconf           
2cec9 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74       /* Conflict
2ceca 20 61 6c 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f   algorithm. (OE_
2cecb 41 62 6f 72 74 2c 20 65 74 63 29 20 2a 2f 20 20  Abort, etc) */  
2cecc 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65  .){.  TriggerSte
2cecd 70 20 2a 70 53 74 65 70 3b 0a 20 20 56 64 62 65  p *pStep;.  Vdbe
2cece 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2cecf 64 62 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  dbe;.  sqlite3 *
2ced0 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2ced1 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ..  assert( pPar
2ced2 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20  se->pTriggerTab 
2ced3 26 26 20 70 50 61 72 73 65 2d 3e 70 54 6f 70 6c  && pParse->pTopl
2ced4 65 76 65 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  evel );.  assert
2ced5 28 20 70 53 74 65 70 4c 69 73 74 20 29 3b 0a 20  ( pStepList );. 
2ced6 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
2ced7 0a 20 20 66 6f 72 28 70 53 74 65 70 3d 70 53 74  .  for(pStep=pSt
2ced8 65 70 4c 69 73 74 3b 20 70 53 74 65 70 3b 20 70  epList; pStep; p
2ced9 53 74 65 70 3d 70 53 74 65 70 2d 3e 70 4e 65 78  Step=pStep->pNex
2ceda 74 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72  t){.    /* Figur
2cedb 65 20 6f 75 74 20 74 68 65 20 4f 4e 20 43 4f 4e  e out the ON CON
2cedc 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 74 68 61  FLICT policy tha
2cedd 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66  t will be used f
2cede 6f 72 20 74 68 69 73 20 73 74 65 70 0a 20 20 20  or this step.   
2cedf 20 2a 2a 20 6f 66 20 74 68 65 20 74 72 69 67 67   ** of the trigg
2cee0 65 72 20 70 72 6f 67 72 61 6d 2e 20 49 66 20 74  er program. If t
2cee1 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  he statement tha
2cee2 74 20 63 61 75 73 65 64 20 74 68 69 73 20 74 72  t caused this tr
2cee3 69 67 67 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20  igger.    ** to 
2cee4 66 69 72 65 20 68 61 64 20 61 6e 20 65 78 70 6c  fire had an expl
2cee5 69 63 69 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54  icit ON CONFLICT
2cee6 2c 20 74 68 65 6e 20 75 73 65 20 69 74 2e 20 4f  , then use it. O
2cee7 74 68 65 72 77 69 73 65 2c 20 75 73 65 0a 20 20  therwise, use.  
2cee8 20 20 2a 2a 20 74 68 65 20 4f 4e 20 43 4f 4e 46    ** the ON CONF
2cee9 4c 49 43 54 20 70 6f 6c 69 63 79 20 74 68 61 74  LICT policy that
2ceea 20 77 61 73 20 73 70 65 63 69 66 69 65 64 20 61   was specified a
2ceeb 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 72  s part of the tr
2ceec 69 67 67 65 72 0a 20 20 20 20 2a 2a 20 73 74 65  igger.    ** ste
2ceed 70 20 73 74 61 74 65 6d 65 6e 74 2e 20 45 78 61  p statement. Exa
2ceee 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  mple:.    **.   
2ceef 20 2a 2a 20 20 20 43 52 45 41 54 45 20 54 52 49   **   CREATE TRI
2cef0 47 47 45 52 20 41 46 54 45 52 20 49 4e 53 45 52  GGER AFTER INSER
2cef1 54 20 4f 4e 20 74 31 20 42 45 47 49 4e 3b 0a 20  T ON t1 BEGIN;. 
2cef2 20 20 20 2a 2a 20 20 20 20 20 49 4e 53 45 52 54     **     INSERT
2cef3 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e 54 4f   OR REPLACE INTO
2cef4 20 74 32 20 56 41 4c 55 45 53 28 6e 65 77 2e 61   t2 VALUES(new.a
2cef5 2c 20 6e 65 77 2e 62 29 3b 0a 20 20 20 20 2a 2a  , new.b);.    **
2cef6 20 20 20 45 4e 44 3b 0a 20 20 20 20 2a 2a 0a 20     END;.    **. 
2cef7 20 20 20 2a 2a 20 20 20 49 4e 53 45 52 54 20 49     **   INSERT I
2cef8 4e 54 4f 20 74 31 20 2e 2e 2e 20 3b 20 20 20 20  NTO t1 ... ;    
2cef9 20 20 20 20 20 20 20 20 2d 2d 20 69 6e 73 65 72          -- inser
2cefa 74 20 69 6e 74 6f 20 74 32 20 75 73 65 73 20 52  t into t2 uses R
2cefb 45 50 4c 41 43 45 20 70 6f 6c 69 63 79 0a 20 20  EPLACE policy.  
2cefc 20 20 2a 2a 20 20 20 49 4e 53 45 52 54 20 4f 52    **   INSERT OR
2cefd 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 74 31 20   IGNORE INTO t1 
2cefe 2e 2e 2e 20 3b 20 20 2d 2d 20 69 6e 73 65 72 74  ... ;  -- insert
2ceff 20 69 6e 74 6f 20 74 32 20 75 73 65 73 20 49 47   into t2 uses IG
2cf00 4e 4f 52 45 20 70 6f 6c 69 63 79 0a 20 20 20 20  NORE policy.    
2cf01 2a 2f 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 65  */.    pParse->e
2cf02 4f 72 63 6f 6e 66 20 3d 20 28 6f 72 63 6f 6e 66  Orconf = (orconf
2cf03 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 29 3f 70 53  ==OE_Default)?pS
2cf04 74 65 70 2d 3e 6f 72 63 6f 6e 66 3a 28 75 38 29  tep->orconf:(u8)
2cf05 6f 72 63 6f 6e 66 3b 0a 0a 20 20 20 20 73 77 69  orconf;..    swi
2cf06 74 63 68 28 20 70 53 74 65 70 2d 3e 6f 70 20 29  tch( pStep->op )
2cf07 7b 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f  {.      case TK_
2cf08 55 50 44 41 54 45 3a 20 7b 0a 20 20 20 20 20 20  UPDATE: {.      
2cf09 20 20 73 71 6c 69 74 65 33 55 70 64 61 74 65 28    sqlite3Update(
2cf0a 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
2cf0b 20 20 20 74 61 72 67 65 74 53 72 63 4c 69 73 74     targetSrcList
2cf0c 28 70 50 61 72 73 65 2c 20 70 53 74 65 70 29 2c  (pParse, pStep),
2cf0d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2cf0e 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
2cf0f 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  , pStep->pExprLi
2cf10 73 74 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20  st, 0), .       
2cf11 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
2cf12 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 57 68  p(db, pStep->pWh
2cf13 65 72 65 2c 20 30 29 2c 20 0a 20 20 20 20 20 20  ere, 0), .      
2cf14 20 20 20 20 70 50 61 72 73 65 2d 3e 65 4f 72 63      pParse->eOrc
2cf15 6f 6e 66 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  onf.        );. 
2cf16 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2cf17 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
2cf18 20 54 4b 5f 49 4e 53 45 52 54 3a 20 7b 0a 20 20   TK_INSERT: {.  
2cf19 20 20 20 20 20 20 73 71 6c 69 74 65 33 49 6e 73        sqlite3Ins
2cf1a 65 72 74 28 70 50 61 72 73 65 2c 20 0a 20 20 20  ert(pParse, .   
2cf1b 20 20 20 20 20 20 20 74 61 72 67 65 74 53 72 63         targetSrc
2cf1c 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 74  List(pParse, pSt
2cf1d 65 70 29 2c 0a 20 20 20 20 20 20 20 20 20 20 73  ep),.          s
2cf1e 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
2cf1f 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 45 78  p(db, pStep->pEx
2cf20 70 72 4c 69 73 74 2c 20 30 29 2c 20 0a 20 20 20  prList, 0), .   
2cf21 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
2cf22 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53 74 65  lectDup(db, pSte
2cf23 70 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 2c 20  p->pSelect, 0), 
2cf24 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2cf25 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20  e3IdListDup(db, 
2cf26 70 53 74 65 70 2d 3e 70 49 64 4c 69 73 74 29 2c  pStep->pIdList),
2cf27 20 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72   .          pPar
2cf28 73 65 2d 3e 65 4f 72 63 6f 6e 66 0a 20 20 20 20  se->eOrconf.    
2cf29 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 62      );.        b
2cf2a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2cf2b 20 20 20 20 63 61 73 65 20 54 4b 5f 44 45 4c 45      case TK_DELE
2cf2c 54 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  TE: {.        sq
2cf2d 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28  lite3DeleteFrom(
2cf2e 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
2cf2f 20 20 20 74 61 72 67 65 74 53 72 63 4c 69 73 74     targetSrcList
2cf30 28 70 50 61 72 73 65 2c 20 70 53 74 65 70 29 2c  (pParse, pStep),
2cf31 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2cf32 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53  e3ExprDup(db, pS
2cf33 74 65 70 2d 3e 70 57 68 65 72 65 2c 20 30 29 0a  tep->pWhere, 0).
2cf34 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
2cf35 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2cf36 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  }.      default:
2cf37 20 61 73 73 65 72 74 28 20 70 53 74 65 70 2d 3e   assert( pStep->
2cf38 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b  op==TK_SELECT );
2cf39 20 7b 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63   {.        Selec
2cf3a 74 44 65 73 74 20 73 44 65 73 74 3b 0a 20 20 20  tDest sDest;.   
2cf3b 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65       Select *pSe
2cf3c 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
2cf3d 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53 74 65  lectDup(db, pSte
2cf3e 70 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a  p->pSelect, 0);.
2cf3f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
2cf40 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 73  electDestInit(&s
2cf41 44 65 73 74 2c 20 53 52 54 5f 44 69 73 63 61 72  Dest, SRT_Discar
2cf42 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  d, 0);.        s
2cf43 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
2cf44 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 73  rse, pSelect, &s
2cf45 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 73  Dest);.        s
2cf46 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2cf47 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  te(db, pSelect);
2cf48 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2cf49 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 0a 20        }.    } . 
2cf4a 20 20 20 69 66 28 20 70 53 74 65 70 2d 3e 6f 70     if( pStep->op
2cf4b 21 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20  !=TK_SELECT ){. 
2cf4c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2cf4d 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 52 65 73  AddOp0(v, OP_Res
2cf4e 65 74 43 6f 75 6e 74 29 3b 0a 20 20 20 20 7d 0a  etCount);.    }.
2cf4f 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b    }..  return 0;
2cf50 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
2cf51 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68  E_DEBUG./*.** Th
2cf52 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
2cf53 73 65 64 20 74 6f 20 61 64 64 20 56 64 62 65 43  sed to add VdbeC
2cf54 6f 6d 6d 65 6e 74 28 29 20 61 6e 6e 6f 74 61 74  omment() annotat
2cf55 69 6f 6e 73 20 74 6f 20 61 20 56 44 42 45 0a 2a  ions to a VDBE.*
2cf56 2a 20 70 72 6f 67 72 61 6d 2e 20 49 74 20 69 73  * program. It is
2cf57 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 70 72 6f   not used in pro
2cf58 64 75 63 74 69 6f 6e 20 63 6f 64 65 2c 20 6f 6e  duction code, on
2cf59 6c 79 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67  ly for debugging
2cf5a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
2cf5b 74 20 63 68 61 72 20 2a 6f 6e 45 72 72 6f 72 54  t char *onErrorT
2cf5c 65 78 74 28 69 6e 74 20 6f 6e 45 72 72 6f 72 29  ext(int onError)
2cf5d 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72  {.  switch( onEr
2cf5e 72 6f 72 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ror ){.    case 
2cf5f 4f 45 5f 41 62 6f 72 74 3a 20 20 20 20 72 65 74  OE_Abort:    ret
2cf60 75 72 6e 20 22 61 62 6f 72 74 22 3b 0a 20 20 20  urn "abort";.   
2cf61 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63   case OE_Rollbac
2cf62 6b 3a 20 72 65 74 75 72 6e 20 22 72 6f 6c 6c 62  k: return "rollb
2cf63 61 63 6b 22 3b 0a 20 20 20 20 63 61 73 65 20 4f  ack";.    case O
2cf64 45 5f 46 61 69 6c 3a 20 20 20 20 20 72 65 74 75  E_Fail:     retu
2cf65 72 6e 20 22 66 61 69 6c 22 3b 0a 20 20 20 20 63  rn "fail";.    c
2cf66 61 73 65 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20  ase OE_Replace: 
2cf67 20 72 65 74 75 72 6e 20 22 72 65 70 6c 61 63 65   return "replace
2cf68 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 49  ";.    case OE_I
2cf69 67 6e 6f 72 65 3a 20 20 20 72 65 74 75 72 6e 20  gnore:   return 
2cf6a 22 69 67 6e 6f 72 65 22 3b 0a 20 20 20 20 63 61  "ignore";.    ca
2cf6b 73 65 20 4f 45 5f 44 65 66 61 75 6c 74 3a 20 20  se OE_Default:  
2cf6c 72 65 74 75 72 6e 20 22 64 65 66 61 75 6c 74 22  return "default"
2cf6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22  ;.  }.  return "
2cf6e 6e 2f 61 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  n/a";.}.#endif..
2cf6f 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 63 6f 6e 74  /*.** Parse cont
2cf70 65 78 74 20 73 74 72 75 63 74 75 72 65 20 70 46  ext structure pF
2cf71 72 6f 6d 20 68 61 73 20 6a 75 73 74 20 62 65 65  rom has just bee
2cf72 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  n used to create
2cf73 20 61 20 73 75 62 2d 76 64 62 65 0a 2a 2a 20 28   a sub-vdbe.** (
2cf74 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 29  trigger program)
2cf75 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 68 61  . If an error ha
2cf76 73 20 6f 63 63 75 72 72 65 64 2c 20 74 72 61 6e  s occurred, tran
2cf77 73 66 65 72 20 65 72 72 6f 72 20 69 6e 66 6f 72  sfer error infor
2cf78 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 70  mation.** from p
2cf79 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 0a 2a 2f 0a  From to pTo..*/.
2cf7a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72 61 6e  static void tran
2cf7b 73 66 65 72 50 61 72 73 65 45 72 72 6f 72 28 50  sferParseError(P
2cf7c 61 72 73 65 20 2a 70 54 6f 2c 20 50 61 72 73 65  arse *pTo, Parse
2cf7d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65   *pFrom){.  asse
2cf7e 72 74 28 20 70 46 72 6f 6d 2d 3e 7a 45 72 72 4d  rt( pFrom->zErrM
2cf7f 73 67 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e  sg==0 || pFrom->
2cf80 6e 45 72 72 20 29 3b 0a 20 20 61 73 73 65 72 74  nErr );.  assert
2cf81 28 20 70 54 6f 2d 3e 7a 45 72 72 4d 73 67 3d 3d  ( pTo->zErrMsg==
2cf82 30 20 7c 7c 20 70 54 6f 2d 3e 6e 45 72 72 20 29  0 || pTo->nErr )
2cf83 3b 0a 20 20 69 66 28 20 70 54 6f 2d 3e 6e 45 72  ;.  if( pTo->nEr
2cf84 72 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 6f 2d  r==0 ){.    pTo-
2cf85 3e 7a 45 72 72 4d 73 67 20 3d 20 70 46 72 6f 6d  >zErrMsg = pFrom
2cf86 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70  ->zErrMsg;.    p
2cf87 54 6f 2d 3e 6e 45 72 72 20 3d 20 70 46 72 6f 6d  To->nErr = pFrom
2cf88 2d 3e 6e 45 72 72 3b 0a 20 20 7d 65 6c 73 65 7b  ->nErr;.  }else{
2cf89 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2cf8a 65 65 28 70 46 72 6f 6d 2d 3e 64 62 2c 20 70 46  ee(pFrom->db, pF
2cf8b 72 6f 6d 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  rom->zErrMsg);. 
2cf8c 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61   }.}../*.** Crea
2cf8d 74 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20  te and populate 
2cf8e 61 20 6e 65 77 20 54 72 69 67 67 65 72 50 72 67  a new TriggerPrg
2cf8f 20 6f 62 6a 65 63 74 20 77 69 74 68 20 61 20 73   object with a s
2cf90 75 62 2d 70 72 6f 67 72 61 6d 20 0a 2a 2a 20 69  ub-program .** i
2cf91 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 72 69 67  mplementing trig
2cf92 67 65 72 20 70 54 72 69 67 67 65 72 20 77 69 74  ger pTrigger wit
2cf93 68 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  h ON CONFLICT po
2cf94 6c 69 63 79 20 6f 72 63 6f 6e 66 2e 0a 2a 2f 0a  licy orconf..*/.
2cf95 73 74 61 74 69 63 20 54 72 69 67 67 65 72 50 72  static TriggerPr
2cf96 67 20 2a 63 6f 64 65 52 6f 77 54 72 69 67 67 65  g *codeRowTrigge
2cf97 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  r(.  Parse *pPar
2cf98 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 43 75 72  se,       /* Cur
2cf99 72 65 6e 74 20 70 61 72 73 65 20 63 6f 6e 74 65  rent parse conte
2cf9a 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20  xt */.  Trigger 
2cf9b 2a 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a 20  *pTrigger,   /* 
2cf9c 54 72 69 67 67 65 72 20 74 6f 20 63 6f 64 65 20  Trigger to code 
2cf9d 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2cf9e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2cf9f 20 74 61 62 6c 65 20 70 54 72 69 67 67 65 72 20   table pTrigger 
2cfa0 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 2a  is attached to *
2cfa1 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20  /.  int orconf  
2cfa2 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43           /* ON C
2cfa3 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 74  ONFLICT policy t
2cfa4 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72 20 70  o code trigger p
2cfa5 72 6f 67 72 61 6d 20 77 69 74 68 20 2a 2f 0a 29  rogram with */.)
2cfa6 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 20  {.  Parse *pTop 
2cfa7 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  = sqlite3ParseTo
2cfa8 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a  plevel(pParse);.
2cfa9 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2cfaa 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a  pParse->db;   /*
2cfab 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
2cfac 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67   */.  TriggerPrg
2cfad 20 2a 70 50 72 67 3b 20 20 20 20 20 20 20 20 20   *pPrg;         
2cfae 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
2cfaf 74 75 72 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  turn */.  Expr *
2cfb0 70 57 68 65 6e 20 3d 20 30 3b 20 20 20 20 20 20  pWhen = 0;      
2cfb1 20 20 20 20 20 20 2f 2a 20 44 75 70 6c 69 63 61        /* Duplica
2cfb2 74 65 20 6f 66 20 74 72 69 67 67 65 72 20 57 48  te of trigger WH
2cfb3 45 4e 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  EN expression */
2cfb4 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20  .  Vdbe *v;     
2cfb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cfb6 2a 20 54 65 6d 70 6f 72 61 72 79 20 56 4d 20 2a  * Temporary VM *
2cfb7 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
2cfb8 73 4e 43 3b 20 20 20 20 20 20 20 20 20 20 20 20  sNC;            
2cfb9 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
2cfba 66 6f 72 20 73 75 62 2d 76 64 62 65 20 2a 2f 0a  for sub-vdbe */.
2cfbb 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50    SubProgram *pP
2cfbc 72 6f 67 72 61 6d 20 3d 20 30 3b 20 20 20 2f 2a  rogram = 0;   /*
2cfbd 20 53 75 62 2d 76 64 62 65 20 66 6f 72 20 74 72   Sub-vdbe for tr
2cfbe 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f  igger program */
2cfbf 0a 20 20 50 61 72 73 65 20 2a 70 53 75 62 50 61  .  Parse *pSubPa
2cfc0 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  rse;           /
2cfc1 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
2cfc2 66 6f 72 20 73 75 62 2d 76 64 62 65 20 2a 2f 0a  for sub-vdbe */.
2cfc3 20 20 69 6e 74 20 69 45 6e 64 54 72 69 67 67 65    int iEndTrigge
2cfc4 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  r = 0;        /*
2cfc5 20 4c 61 62 65 6c 20 74 6f 20 6a 75 6d 70 20 74   Label to jump t
2cfc6 6f 20 69 66 20 57 48 45 4e 20 69 73 20 66 61 6c  o if WHEN is fal
2cfc7 73 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  se */..  assert(
2cfc8 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
2cfc9 3d 3d 30 20 7c 7c 20 70 54 61 62 3d 3d 74 61 62  ==0 || pTab==tab
2cfca 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54 72 69  leOfTrigger(pTri
2cfcb 67 67 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 41  gger) );..  /* A
2cfcc 6c 6c 6f 63 61 74 65 20 74 68 65 20 54 72 69 67  llocate the Trig
2cfcd 67 65 72 50 72 67 20 61 6e 64 20 53 75 62 50 72  gerPrg and SubPr
2cfce 6f 67 72 61 6d 20 6f 62 6a 65 63 74 73 2e 20 54  ogram objects. T
2cfcf 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  o ensure that th
2cfd0 65 79 0a 20 20 2a 2a 20 61 72 65 20 66 72 65 65  ey.  ** are free
2cfd1 64 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  d if an error oc
2cfd2 63 75 72 73 2c 20 6c 69 6e 6b 20 74 68 65 6d 20  curs, link them 
2cfd3 69 6e 74 6f 20 74 68 65 20 50 61 72 73 65 2e 70  into the Parse.p
2cfd4 54 72 69 67 67 65 72 50 72 67 20 0a 20 20 2a 2a  TriggerPrg .  **
2cfd5 20 6c 69 73 74 20 6f 66 20 74 68 65 20 74 6f 70   list of the top
2cfd6 2d 6c 65 76 65 6c 20 50 61 72 73 65 20 6f 62 6a  -level Parse obj
2cfd7 65 63 74 20 73 6f 6f 6e 65 72 20 72 61 74 68 65  ect sooner rathe
2cfd8 72 20 74 68 61 6e 20 6c 61 74 65 72 2e 20 20 2a  r than later.  *
2cfd9 2f 0a 20 20 70 50 72 67 20 3d 20 73 71 6c 69 74  /.  pPrg = sqlit
2cfda 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2cfdb 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65  b, sizeof(Trigge
2cfdc 72 50 72 67 29 29 3b 0a 20 20 69 66 28 20 21 70  rPrg));.  if( !p
2cfdd 50 72 67 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  Prg ) return 0;.
2cfde 20 20 70 50 72 67 2d 3e 70 4e 65 78 74 20 3d 20    pPrg->pNext = 
2cfdf 70 54 6f 70 2d 3e 70 54 72 69 67 67 65 72 50 72  pTop->pTriggerPr
2cfe0 67 3b 0a 20 20 70 54 6f 70 2d 3e 70 54 72 69 67  g;.  pTop->pTrig
2cfe1 67 65 72 50 72 67 20 3d 20 70 50 72 67 3b 0a 20  gerPrg = pPrg;. 
2cfe2 20 70 50 72 67 2d 3e 70 50 72 6f 67 72 61 6d 20   pPrg->pProgram 
2cfe3 3d 20 70 50 72 6f 67 72 61 6d 20 3d 20 73 71 6c  = pProgram = sql
2cfe4 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2cfe5 28 64 62 2c 20 73 69 7a 65 6f 66 28 53 75 62 50  (db, sizeof(SubP
2cfe6 72 6f 67 72 61 6d 29 29 3b 0a 20 20 69 66 28 20  rogram));.  if( 
2cfe7 21 70 50 72 6f 67 72 61 6d 20 29 20 72 65 74 75  !pProgram ) retu
2cfe8 72 6e 20 30 3b 0a 20 20 70 50 72 6f 67 72 61 6d  rn 0;.  pProgram
2cfe9 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 50  ->nRef = 1;.  pP
2cfea 72 67 2d 3e 70 54 72 69 67 67 65 72 20 3d 20 70  rg->pTrigger = p
2cfeb 54 72 69 67 67 65 72 3b 0a 20 20 70 50 72 67 2d  Trigger;.  pPrg-
2cfec 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66  >orconf = orconf
2cfed 3b 0a 20 20 70 50 72 67 2d 3e 6f 6c 64 6d 61 73  ;.  pPrg->oldmas
2cfee 6b 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b 0a  k = 0xffffffff;.
2cfef 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
2cff0 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 20 6e 65  nd populate a ne
2cff1 77 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  w Parse context 
2cff2 74 6f 20 75 73 65 20 66 6f 72 20 63 6f 64 69 6e  to use for codin
2cff3 67 20 74 68 65 20 0a 20 20 2a 2a 20 74 72 69 67  g the .  ** trig
2cff4 67 65 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e  ger sub-program.
2cff5 20 20 2a 2f 0a 20 20 70 53 75 62 50 61 72 73 65    */.  pSubParse
2cff6 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41   = sqlite3StackA
2cff7 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2cff8 65 6f 66 28 50 61 72 73 65 29 29 3b 0a 20 20 69  eof(Parse));.  i
2cff9 66 28 20 21 70 53 75 62 50 61 72 73 65 20 29 20  f( !pSubParse ) 
2cffa 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 65 6d 73  return 0;.  mems
2cffb 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65  et(&sNC, 0, size
2cffc 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e  of(sNC));.  sNC.
2cffd 70 50 61 72 73 65 20 3d 20 70 53 75 62 50 61 72  pParse = pSubPar
2cffe 73 65 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d  se;.  pSubParse-
2cfff 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 53 75 62  >db = db;.  pSub
2d000 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54  Parse->pTriggerT
2d001 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 70 53 75  ab = pTab;.  pSu
2d002 62 50 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65  bParse->pTopleve
2d003 6c 20 3d 20 70 54 6f 70 3b 0a 20 20 70 53 75 62  l = pTop;.  pSub
2d004 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
2d005 65 78 74 20 3d 20 70 54 72 69 67 67 65 72 2d 3e  ext = pTrigger->
2d006 7a 4e 61 6d 65 3b 0a 20 20 70 53 75 62 50 61 72  zName;.  pSubPar
2d007 73 65 2d 3e 65 54 72 69 67 67 65 72 4f 70 20 3d  se->eTriggerOp =
2d008 20 70 54 72 69 67 67 65 72 2d 3e 6f 70 3b 0a 0a   pTrigger->op;..
2d009 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
2d00a 56 64 62 65 28 70 53 75 62 50 61 72 73 65 29 3b  Vdbe(pSubParse);
2d00b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20  .  if( v ){.    
2d00c 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2d00d 22 53 74 61 72 74 3a 20 25 73 2e 25 73 20 28 25  "Start: %s.%s (%
2d00e 73 20 25 73 25 73 25 73 20 4f 4e 20 25 73 29 22  s %s%s%s ON %s)"
2d00f 2c 20 0a 20 20 20 20 20 20 70 54 72 69 67 67 65  , .      pTrigge
2d010 72 2d 3e 7a 4e 61 6d 65 2c 20 6f 6e 45 72 72 6f  r->zName, onErro
2d011 72 54 65 78 74 28 6f 72 63 6f 6e 66 29 2c 0a 20  rText(orconf),. 
2d012 20 20 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e       (pTrigger->
2d013 74 72 5f 74 6d 3d 3d 54 52 49 47 47 45 52 5f 42  tr_tm==TRIGGER_B
2d014 45 46 4f 52 45 20 3f 20 22 42 45 46 4f 52 45 22  EFORE ? "BEFORE"
2d015 20 3a 20 22 41 46 54 45 52 22 29 2c 0a 20 20 20   : "AFTER"),.   
2d016 20 20 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e       (pTrigger->
2d017 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 3f 20  op==TK_UPDATE ? 
2d018 22 55 50 44 41 54 45 22 20 3a 20 22 22 29 2c 0a  "UPDATE" : ""),.
2d019 20 20 20 20 20 20 20 20 28 70 54 72 69 67 67 65          (pTrigge
2d01a 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54  r->op==TK_INSERT
2d01b 20 3f 20 22 49 4e 53 45 52 54 22 20 3a 20 22 22   ? "INSERT" : ""
2d01c 29 2c 0a 20 20 20 20 20 20 20 20 28 70 54 72 69  ),.        (pTri
2d01d 67 67 65 72 2d 3e 6f 70 3d 3d 54 4b 5f 44 45 4c  gger->op==TK_DEL
2d01e 45 54 45 20 3f 20 22 44 45 4c 45 54 45 22 20 3a  ETE ? "DELETE" :
2d01f 20 22 22 29 2c 0a 20 20 20 20 20 20 70 54 61 62   ""),.      pTab
2d020 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 29 3b 0a  ->zName.    ));.
2d021 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2d022 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 20 73 71  MIT_TRACE.    sq
2d023 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2d024 34 28 76 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20  4(v, -1, .      
2d025 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2d026 62 2c 20 22 2d 2d 20 54 52 49 47 47 45 52 20 25  b, "-- TRIGGER %
2d027 73 22 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e  s", pTrigger->zN
2d028 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43  ame), P4_DYNAMIC
2d029 0a 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a 0a  .    );.#endif..
2d02a 20 20 20 20 2f 2a 20 49 66 20 6f 6e 65 20 77 61      /* If one wa
2d02b 73 20 73 70 65 63 69 66 69 65 64 2c 20 63 6f 64  s specified, cod
2d02c 65 20 74 68 65 20 57 48 45 4e 20 63 6c 61 75 73  e the WHEN claus
2d02d 65 2e 20 49 66 20 69 74 20 65 76 61 6c 75 61 74  e. If it evaluat
2d02e 65 73 20 74 6f 20 66 61 6c 73 65 0a 20 20 20 20  es to false.    
2d02f 2a 2a 20 28 6f 72 20 4e 55 4c 4c 29 20 74 68 65  ** (or NULL) the
2d030 20 73 75 62 2d 76 64 62 65 20 69 73 20 69 6d 6d   sub-vdbe is imm
2d031 65 64 69 61 74 65 6c 79 20 68 61 6c 74 65 64 20  ediately halted 
2d032 62 79 20 6a 75 6d 70 69 6e 67 20 74 6f 20 74 68  by jumping to th
2d033 65 20 0a 20 20 20 20 2a 2a 20 4f 50 5f 48 61 6c  e .    ** OP_Hal
2d034 74 20 69 6e 73 65 72 74 65 64 20 61 74 20 74 68  t inserted at th
2d035 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 72 6f  e end of the pro
2d036 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 20 20 69 66  gram.  */.    if
2d037 28 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68 65  ( pTrigger->pWhe
2d038 6e 20 29 7b 0a 20 20 20 20 20 20 70 57 68 65 6e  n ){.      pWhen
2d039 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2d03a 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e  p(db, pTrigger->
2d03b 70 57 68 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20  pWhen, 0);.     
2d03c 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
2d03d 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
2d03e 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 57  prNames(&sNC, pW
2d03f 68 65 6e 29 20 0a 20 20 20 20 20 20 20 26 26 20  hen) .       && 
2d040 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2d041 3d 3d 30 20 0a 20 20 20 20 20 20 29 7b 0a 20 20  ==0 .      ){.  
2d042 20 20 20 20 20 20 69 45 6e 64 54 72 69 67 67 65        iEndTrigge
2d043 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  r = sqlite3VdbeM
2d044 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
2d045 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2d046 49 66 46 61 6c 73 65 28 70 53 75 62 50 61 72 73  IfFalse(pSubPars
2d047 65 2c 20 70 57 68 65 6e 2c 20 69 45 6e 64 54 72  e, pWhen, iEndTr
2d048 69 67 67 65 72 2c 20 53 51 4c 49 54 45 5f 4a 55  igger, SQLITE_JU
2d049 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20  MPIFNULL);.     
2d04a 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2d04b 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2d04c 57 68 65 6e 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  When);.    }..  
2d04d 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 74 72    /* Code the tr
2d04e 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 69 6e  igger program in
2d04f 74 6f 20 74 68 65 20 73 75 62 2d 76 64 62 65 2e  to the sub-vdbe.
2d050 20 2a 2f 0a 20 20 20 20 63 6f 64 65 54 72 69 67   */.    codeTrig
2d051 67 65 72 50 72 6f 67 72 61 6d 28 70 53 75 62 50  gerProgram(pSubP
2d052 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2d 3e  arse, pTrigger->
2d053 73 74 65 70 5f 6c 69 73 74 2c 20 6f 72 63 6f 6e  step_list, orcon
2d054 66 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65  f);..    /* Inse
2d055 72 74 20 61 6e 20 4f 50 5f 48 61 6c 74 20 61 74  rt an OP_Halt at
2d056 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
2d057 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 2a 2f 0a  sub-program. */.
2d058 20 20 20 20 69 66 28 20 69 45 6e 64 54 72 69 67      if( iEndTrig
2d059 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ger ){.      sql
2d05a 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
2d05b 61 62 65 6c 28 76 2c 20 69 45 6e 64 54 72 69 67  abel(v, iEndTrig
2d05c 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ger);.    }.    
2d05d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d05e 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a 20  0(v, OP_Halt);. 
2d05f 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2d060 76 2c 20 22 45 6e 64 3a 20 25 73 2e 25 73 22 2c  v, "End: %s.%s",
2d061 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
2d062 2c 20 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f 72  , onErrorText(or
2d063 63 6f 6e 66 29 29 29 3b 0a 0a 20 20 20 20 74 72  conf)));..    tr
2d064 61 6e 73 66 65 72 50 61 72 73 65 45 72 72 6f 72  ansferParseError
2d065 28 70 50 61 72 73 65 2c 20 70 53 75 62 50 61 72  (pParse, pSubPar
2d066 73 65 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  se);.    if( db-
2d067 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30  >mallocFailed==0
2d068 20 29 7b 0a 20 20 20 20 20 20 70 50 72 6f 67 72   ){.      pProgr
2d069 61 6d 2d 3e 61 4f 70 20 3d 20 73 71 6c 69 74 65  am->aOp = sqlite
2d06a 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79  3VdbeTakeOpArray
2d06b 28 76 2c 20 26 70 50 72 6f 67 72 61 6d 2d 3e 6e  (v, &pProgram->n
2d06c 4f 70 2c 20 26 70 54 6f 70 2d 3e 6e 4d 61 78 41  Op, &pTop->nMaxA
2d06d 72 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  rg);.    }.    p
2d06e 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 20 3d 20  Program->nMem = 
2d06f 70 53 75 62 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b  pSubParse->nMem;
2d070 0a 20 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e 6e  .    pProgram->n
2d071 43 73 72 20 3d 20 70 53 75 62 50 61 72 73 65 2d  Csr = pSubParse-
2d072 3e 6e 54 61 62 3b 0a 20 20 20 20 70 50 72 6f 67  >nTab;.    pProg
2d073 72 61 6d 2d 3e 74 6f 6b 65 6e 20 3d 20 28 76 6f  ram->token = (vo
2d074 69 64 20 2a 29 70 54 72 69 67 67 65 72 3b 0a 20  id *)pTrigger;. 
2d075 20 20 20 70 50 72 67 2d 3e 6f 6c 64 6d 61 73 6b     pPrg->oldmask
2d076 20 3d 20 70 53 75 62 50 61 72 73 65 2d 3e 6f 6c   = pSubParse->ol
2d077 64 6d 61 73 6b 3b 0a 20 20 20 20 73 71 6c 69 74  dmask;.    sqlit
2d078 65 33 56 64 62 65 44 65 6c 65 74 65 28 76 29 3b  e3VdbeDelete(v);
2d079 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
2d07a 21 70 53 75 62 50 61 72 73 65 2d 3e 70 41 69 6e  !pSubParse->pAin
2d07b 63 20 20 20 20 20 20 20 26 26 20 21 70 53 75 62  c       && !pSub
2d07c 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61  Parse->pZombieTa
2d07d 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  b );.  assert( !
2d07e 70 53 75 62 50 61 72 73 65 2d 3e 70 54 72 69 67  pSubParse->pTrig
2d07f 67 65 72 50 72 67 20 26 26 20 21 70 53 75 62 50  gerPrg && !pSubP
2d080 61 72 73 65 2d 3e 6e 4d 61 78 41 72 67 20 29 3b  arse->nMaxArg );
2d081 0a 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46  .  sqlite3StackF
2d082 72 65 65 28 64 62 2c 20 70 53 75 62 50 61 72 73  ree(db, pSubPars
2d083 65 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 50  e);..  return pP
2d084 72 67 3b 0a 7d 0a 20 20 20 20 0a 2f 2a 0a 2a 2a  rg;.}.    ./*.**
2d085 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
2d086 72 20 74 6f 20 61 20 54 72 69 67 67 65 72 50 72  r to a TriggerPr
2d087 67 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e  g object contain
2d088 69 6e 67 20 74 68 65 20 73 75 62 2d 70 72 6f 67  ing the sub-prog
2d089 72 61 6d 20 66 6f 72 0a 2a 2a 20 74 72 69 67 67  ram for.** trigg
2d08a 65 72 20 70 54 72 69 67 67 65 72 20 77 69 74 68  er pTrigger with
2d08b 20 64 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46   default ON CONF
2d08c 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d 20 6f  LICT algorithm o
2d08d 72 63 6f 6e 66 2e 20 49 66 20 6e 6f 20 73 75 63  rconf. If no suc
2d08e 68 0a 2a 2a 20 54 72 69 67 67 65 72 50 72 67 20  h.** TriggerPrg 
2d08f 6f 62 6a 65 63 74 20 65 78 69 73 74 73 2c 20 61  object exists, a
2d090 20 6e 65 77 20 6f 62 6a 65 63 74 20 69 73 20 61   new object is a
2d091 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70  llocated and pop
2d092 75 6c 61 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a  ulated before.**
2d093 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e   being returned.
2d094 0a 2a 2f 0a 73 74 61 74 69 63 20 54 72 69 67 67  .*/.static Trigg
2d095 65 72 50 72 67 20 2a 67 65 74 52 6f 77 54 72 69  erPrg *getRowTri
2d096 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70  gger(.  Parse *p
2d097 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
2d098 43 75 72 72 65 6e 74 20 70 61 72 73 65 20 63 6f  Current parse co
2d099 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67  ntext */.  Trigg
2d09a 65 72 20 2a 70 54 72 69 67 67 65 72 2c 20 20 20  er *pTrigger,   
2d09b 2f 2a 20 54 72 69 67 67 65 72 20 74 6f 20 63 6f  /* Trigger to co
2d09c 64 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  de */.  Table *p
2d09d 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Tab,         /* 
2d09e 54 68 65 20 74 61 62 6c 65 20 74 72 69 67 67 65  The table trigge
2d09f 72 20 70 54 72 69 67 67 65 72 20 69 73 20 61 74  r pTrigger is at
2d0a0 74 61 63 68 65 64 20 74 6f 20 2a 2f 0a 20 20 69  tached to */.  i
2d0a1 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20  nt orconf       
2d0a2 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49      /* ON CONFLI
2d0a3 43 54 20 61 6c 67 6f 72 69 74 68 6d 2e 20 2a 2f  CT algorithm. */
2d0a4 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 52 6f  .){.  Parse *pRo
2d0a5 6f 74 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  ot = sqlite3Pars
2d0a6 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
2d0a7 29 3b 0a 20 20 54 72 69 67 67 65 72 50 72 67 20  );.  TriggerPrg 
2d0a8 2a 70 50 72 67 3b 0a 0a 20 20 61 73 73 65 72 74  *pPrg;..  assert
2d0a9 28 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d  ( pTrigger->zNam
2d0aa 65 3d 3d 30 20 7c 7c 20 70 54 61 62 3d 3d 74 61  e==0 || pTab==ta
2d0ab 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54 72  bleOfTrigger(pTr
2d0ac 69 67 67 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20  igger) );..  /* 
2d0ad 49 74 20 6d 61 79 20 62 65 20 74 68 61 74 20 74  It may be that t
2d0ae 68 69 73 20 74 72 69 67 67 65 72 20 68 61 73 20  his trigger has 
2d0af 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 64  already been cod
2d0b0 65 64 20 28 6f 72 20 69 73 20 69 6e 20 74 68 65  ed (or is in the
2d0b1 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66  .  ** process of
2d0b2 20 62 65 69 6e 67 20 63 6f 64 65 64 29 2e 20 49   being coded). I
2d0b3 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  f this is the ca
2d0b4 73 65 2c 20 74 68 65 6e 20 61 6e 20 65 6e 74 72  se, then an entr
2d0b5 79 20 77 69 74 68 0a 20 20 2a 2a 20 61 20 6d 61  y with.  ** a ma
2d0b6 74 63 68 69 6e 67 20 54 72 69 67 67 65 72 50 72  tching TriggerPr
2d0b7 67 2e 70 54 72 69 67 67 65 72 20 66 69 65 6c 64  g.pTrigger field
2d0b8 20 77 69 6c 6c 20 62 65 20 70 72 65 73 65 6e 74   will be present
2d0b9 20 73 6f 6d 65 77 68 65 72 65 0a 20 20 2a 2a 20   somewhere.  ** 
2d0ba 69 6e 20 74 68 65 20 50 61 72 73 65 2e 70 54 72  in the Parse.pTr
2d0bb 69 67 67 65 72 50 72 67 20 6c 69 73 74 2e 20 53  iggerPrg list. S
2d0bc 65 61 72 63 68 20 66 6f 72 20 73 75 63 68 20 61  earch for such a
2d0bd 6e 20 65 6e 74 72 79 2e 20 20 2a 2f 0a 20 20 66  n entry.  */.  f
2d0be 6f 72 28 70 50 72 67 3d 70 52 6f 6f 74 2d 3e 70  or(pPrg=pRoot->p
2d0bf 54 72 69 67 67 65 72 50 72 67 3b 20 0a 20 20 20  TriggerPrg; .   
2d0c0 20 20 20 70 50 72 67 20 26 26 20 28 70 50 72 67     pPrg && (pPrg
2d0c1 2d 3e 70 54 72 69 67 67 65 72 21 3d 70 54 72 69  ->pTrigger!=pTri
2d0c2 67 67 65 72 20 7c 7c 20 70 50 72 67 2d 3e 6f 72  gger || pPrg->or
2d0c3 63 6f 6e 66 21 3d 6f 72 63 6f 6e 66 29 3b 20 0a  conf!=orconf); .
2d0c4 20 20 20 20 20 20 70 50 72 67 3d 70 50 72 67 2d        pPrg=pPrg-
2d0c5 3e 70 4e 65 78 74 0a 20 20 29 3b 0a 0a 20 20 2f  >pNext.  );..  /
2d0c6 2a 20 49 66 20 61 6e 20 65 78 69 73 74 69 6e 67  * If an existing
2d0c7 20 54 72 69 67 67 65 72 50 72 67 20 63 6f 75 6c   TriggerPrg coul
2d0c8 64 20 6e 6f 74 20 62 65 20 6c 6f 63 61 74 65 64  d not be located
2d0c9 2c 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 6f  , create a new o
2d0ca 6e 65 2e 20 2a 2f 0a 20 20 69 66 28 20 21 70 50  ne. */.  if( !pP
2d0cb 72 67 20 29 7b 0a 20 20 20 20 70 50 72 67 20 3d  rg ){.    pPrg =
2d0cc 20 63 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28   codeRowTrigger(
2d0cd 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
2d0ce 2c 20 70 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b  , pTab, orconf);
2d0cf 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
2d0d0 50 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Prg;.}../*.** Ge
2d0d1 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2d0d2 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  the trigger prog
2d0d3 72 61 6d 20 61 73 73 6f 63 69 61 74 65 64 20 77  ram associated w
2d0d4 69 74 68 20 74 72 69 67 67 65 72 20 70 20 6f 6e  ith trigger p on
2d0d5 20 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 2e   .** table pTab.
2d0d6 20 54 68 65 20 72 65 67 2c 20 6f 72 63 6f 6e 66   The reg, orconf
2d0d7 20 61 6e 64 20 69 67 6e 6f 72 65 4a 75 6d 70 20   and ignoreJump 
2d0d8 70 61 72 61 6d 65 74 65 72 73 20 70 61 73 73 65  parameters passe
2d0d9 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e  d to this.** fun
2d0da 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 73 61  ction are the sa
2d0db 6d 65 20 61 73 20 74 68 6f 73 65 20 64 65 73 63  me as those desc
2d0dc 72 69 62 65 64 20 69 6e 20 74 68 65 20 68 65 61  ribed in the hea
2d0dd 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  der function for
2d0de 0a 2a 2a 20 73 71 6c 69 74 65 33 43 6f 64 65 52  .** sqlite3CodeR
2d0df 6f 77 54 72 69 67 67 65 72 28 29 0a 2a 2f 0a 53  owTrigger().*/.S
2d0e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2d0e1 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  id sqlite3CodeRo
2d0e2 77 54 72 69 67 67 65 72 44 69 72 65 63 74 28 0a  wTriggerDirect(.
2d0e3 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2d0e4 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
2d0e5 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69  context */.  Tri
2d0e6 67 67 65 72 20 2a 70 2c 20 20 20 20 20 20 20 20  gger *p,        
2d0e7 20 20 2f 2a 20 54 72 69 67 67 65 72 20 74 6f 20    /* Trigger to 
2d0e8 63 6f 64 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20  code */.  Table 
2d0e9 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f  *pTab,         /
2d0ea 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 63  * The table to c
2d0eb 6f 64 65 20 74 72 69 67 67 65 72 73 20 66 72 6f  ode triggers fro
2d0ec 6d 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 2c 20  m */.  int reg, 
2d0ed 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2d0ee 65 67 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e  eg array contain
2d0ef 69 6e 67 20 4f 4c 44 2e 2a 20 61 6e 64 20 4e 45  ing OLD.* and NE
2d0f0 57 2e 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  W.* values */.  
2d0f1 69 6e 74 20 6f 72 63 6f 6e 66 2c 20 20 20 20 20  int orconf,     
2d0f2 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c       /* ON CONFL
2d0f3 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20  ICT policy */.  
2d0f4 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70 20 20  int ignoreJump  
2d0f5 20 20 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74       /* Instruct
2d0f6 69 6f 6e 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66  ion to jump to f
2d0f7 6f 72 20 52 41 49 53 45 28 49 47 4e 4f 52 45 29  or RAISE(IGNORE)
2d0f8 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
2d0f9 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
2d0fa 65 28 70 50 61 72 73 65 29 3b 20 2f 2a 20 4d 61  e(pParse); /* Ma
2d0fb 69 6e 20 56 4d 20 2a 2f 0a 20 20 54 72 69 67 67  in VM */.  Trigg
2d0fc 65 72 50 72 67 20 2a 70 50 72 67 3b 0a 20 20 70  erPrg *pPrg;.  p
2d0fd 50 72 67 20 3d 20 67 65 74 52 6f 77 54 72 69 67  Prg = getRowTrig
2d0fe 67 65 72 28 70 50 61 72 73 65 2c 20 70 2c 20 70  ger(pParse, p, p
2d0ff 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20  Tab, orconf);.  
2d100 61 73 73 65 72 74 28 20 70 50 72 67 20 7c 7c 20  assert( pPrg || 
2d101 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
2d102 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
2d103 6f 63 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 2f  ocFailed );..  /
2d104 2a 20 43 6f 64 65 20 74 68 65 20 4f 50 5f 50 72  * Code the OP_Pr
2d105 6f 67 72 61 6d 20 6f 70 63 6f 64 65 20 69 6e 20  ogram opcode in 
2d106 74 68 65 20 70 61 72 65 6e 74 20 56 44 42 45 2e  the parent VDBE.
2d107 20 50 34 20 6f 66 20 74 68 65 20 4f 50 5f 50 72   P4 of the OP_Pr
2d108 6f 67 72 61 6d 20 0a 20 20 2a 2a 20 69 73 20 61  ogram .  ** is a
2d109 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2d10a 73 75 62 2d 76 64 62 65 20 63 6f 6e 74 61 69 6e  sub-vdbe contain
2d10b 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20  ing the trigger 
2d10c 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 69  program.  */.  i
2d10d 66 28 20 70 50 72 67 20 29 7b 0a 20 20 20 20 73  f( pPrg ){.    s
2d10e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2d10f 28 76 2c 20 4f 50 5f 50 72 6f 67 72 61 6d 2c 20  (v, OP_Program, 
2d110 72 65 67 2c 20 69 67 6e 6f 72 65 4a 75 6d 70 2c  reg, ignoreJump,
2d111 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29   ++pParse->nMem)
2d112 3b 0a 20 20 20 20 70 50 72 67 2d 3e 70 50 72 6f  ;.    pPrg->pPro
2d113 67 72 61 6d 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  gram->nRef++;.  
2d114 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2d115 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f  ngeP4(v, -1, (co
2d116 6e 73 74 20 63 68 61 72 20 2a 29 70 50 72 67 2d  nst char *)pPrg-
2d117 3e 70 50 72 6f 67 72 61 6d 2c 20 50 34 5f 53 55  >pProgram, P4_SU
2d118 42 50 52 4f 47 52 41 4d 29 3b 0a 20 20 20 20 56  BPROGRAM);.    V
2d119 64 62 65 43 6f 6d 6d 65 6e 74 28 0a 20 20 20 20  dbeComment(.    
2d11a 20 20 20 20 28 76 2c 20 22 43 61 6c 6c 3a 20 25      (v, "Call: %
2d11b 73 2e 25 73 22 2c 20 28 70 2d 3e 7a 4e 61 6d 65  s.%s", (p->zName
2d11c 3f 70 2d 3e 7a 4e 61 6d 65 3a 22 66 6b 65 79 22  ?p->zName:"fkey"
2d11d 29 2c 20 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f  ), onErrorText(o
2d11e 72 63 6f 6e 66 29 29 29 3b 0a 0a 20 20 20 20 2f  rconf)));..    /
2d11f 2a 20 53 65 74 20 74 68 65 20 50 35 20 6f 70 65  * Set the P5 ope
2d120 72 61 6e 64 20 6f 66 20 74 68 65 20 4f 50 5f 50  rand of the OP_P
2d121 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69  rogram instructi
2d122 6f 6e 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69  on to non-zero i
2d123 66 0a 20 20 20 20 2a 2a 20 72 65 63 75 72 73 69  f.    ** recursi
2d124 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  ve invocation of
2d125 20 74 68 69 73 20 74 72 69 67 67 65 72 20 70 72   this trigger pr
2d126 6f 67 72 61 6d 20 69 73 20 64 69 73 61 6c 6c 6f  ogram is disallo
2d127 77 65 64 2e 20 52 65 63 75 72 73 69 76 65 0a 20  wed. Recursive. 
2d128 20 20 20 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e     ** invocation
2d129 20 69 73 20 64 69 73 61 6c 6c 6f 77 65 64 20 69   is disallowed i
2d12a 66 20 28 61 29 20 74 68 65 20 73 75 62 2d 70 72  f (a) the sub-pr
2d12b 6f 67 72 61 6d 20 69 73 20 72 65 61 6c 6c 79 20  ogram is really 
2d12c 61 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a  a trigger,.    *
2d12d 2a 20 6e 6f 74 20 61 20 66 6f 72 65 69 67 6e 20  * not a foreign 
2d12e 6b 65 79 20 61 63 74 69 6f 6e 2c 20 61 6e 64 20  key action, and 
2d12f 28 62 29 20 74 68 65 20 66 6c 61 67 20 74 6f 20  (b) the flag to 
2d130 65 6e 61 62 6c 65 20 72 65 63 75 72 73 69 76 65  enable recursive
2d131 20 74 72 69 67 67 65 72 73 0a 20 20 20 20 2a 2a   triggers.    **
2d132 20 69 73 20 63 6c 65 61 72 2e 20 20 2a 2f 0a 20   is clear.  */. 
2d133 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2d134 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 28 70  angeP5(v, (u8)(p
2d135 2d 3e 7a 4e 61 6d 65 20 26 26 20 21 28 70 50 61  ->zName && !(pPa
2d136 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53  rse->db->flags&S
2d137 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67 65 72  QLITE_RecTrigger
2d138 73 29 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  s)));.  }.}../*.
2d139 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  ** This is calle
2d13a 64 20 74 6f 20 63 6f 64 65 20 74 68 65 20 72 65  d to code the re
2d13b 71 75 69 72 65 64 20 46 4f 52 20 45 41 43 48 20  quired FOR EACH 
2d13c 52 4f 57 20 74 72 69 67 67 65 72 73 20 66 6f 72  ROW triggers for
2d13d 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a   an operation.**
2d13e 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 2e 20   on table pTab. 
2d13f 54 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f  The operation to
2d140 20 63 6f 64 65 20 74 72 69 67 67 65 72 73 20 66   code triggers f
2d141 6f 72 20 28 49 4e 53 45 52 54 2c 20 55 50 44 41  or (INSERT, UPDA
2d142 54 45 20 6f 72 20 44 45 4c 45 54 45 29 0a 2a 2a  TE or DELETE).**
2d143 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65   is given by the
2d144 20 6f 70 20 70 61 72 61 6d 61 74 65 72 2e 20 54   op paramater. T
2d145 68 65 20 74 72 5f 74 6d 20 70 61 72 61 6d 65 74  he tr_tm paramet
2d146 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  er determines wh
2d147 65 74 68 65 72 20 74 68 65 0a 2a 2a 20 42 45 46  ether the.** BEF
2d148 4f 52 45 20 6f 72 20 41 46 54 45 52 20 74 72 69  ORE or AFTER tri
2d149 67 67 65 72 73 20 61 72 65 20 63 6f 64 65 64 2e  ggers are coded.
2d14a 20 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f   If the operatio
2d14b 6e 20 69 73 20 61 6e 20 55 50 44 41 54 45 2c 20  n is an UPDATE, 
2d14c 74 68 65 6e 0a 2a 2a 20 70 61 72 61 6d 65 74 65  then.** paramete
2d14d 72 20 70 43 68 61 6e 67 65 73 20 69 73 20 70 61  r pChanges is pa
2d14e 73 73 65 64 20 74 68 65 20 6c 69 73 74 20 6f 66  ssed the list of
2d14f 20 63 6f 6c 75 6d 6e 73 20 62 65 69 6e 67 20 6d   columns being m
2d150 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  odified..**.** I
2d151 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 74  f there are no t
2d152 72 69 67 67 65 72 73 20 74 68 61 74 20 66 69 72  riggers that fir
2d153 65 20 61 74 20 74 68 65 20 73 70 65 63 69 66 69  e at the specifi
2d154 65 64 20 74 69 6d 65 20 66 6f 72 20 74 68 65 20  ed time for the 
2d155 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 6f 70 65  specified.** ope
2d156 72 61 74 69 6f 6e 20 6f 6e 20 70 54 61 62 2c 20  ration on pTab, 
2d157 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
2d158 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
2d159 54 68 65 20 72 65 67 20 61 72 67 75 6d 65 6e 74  The reg argument
2d15a 20 69 73 20 74 68 65 20 61 64 64 72 65 73 73 20   is the address 
2d15b 6f 66 20 74 68 65 20 66 69 72 73 74 20 69 6e 20  of the first in 
2d15c 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 67 69  an array of regi
2d15d 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 63  sters .** that c
2d15e 6f 6e 74 61 69 6e 20 74 68 65 20 76 61 6c 75 65  ontain the value
2d15f 73 20 73 75 62 73 74 69 74 75 74 65 64 20 66 6f  s substituted fo
2d160 72 20 74 68 65 20 6e 65 77 2e 2a 20 61 6e 64 20  r the new.* and 
2d161 6f 6c 64 2e 2a 20 72 65 66 65 72 65 6e 63 65 73  old.* references
2d162 0a 2a 2a 20 69 6e 20 74 68 65 20 74 72 69 67 67  .** in the trigg
2d163 65 72 20 70 72 6f 67 72 61 6d 2e 20 49 66 20 4e  er program. If N
2d164 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
2d165 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 61 62  f columns in tab
2d166 6c 65 20 70 54 61 62 0a 2a 2a 20 28 61 20 63 6f  le pTab.** (a co
2d167 70 79 20 6f 66 20 70 54 61 62 2d 3e 6e 43 6f 6c  py of pTab->nCol
2d168 29 2c 20 74 68 65 6e 20 72 65 67 69 73 74 65 72  ), then register
2d169 73 20 61 72 65 20 70 6f 70 75 6c 61 74 65 64 20  s are populated 
2d16a 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
2d16b 2a 20 20 20 52 65 67 69 73 74 65 72 20 20 20 20  *   Register    
2d16c 20 20 20 43 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20     Contains.**  
2d16d 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
2d16e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d16f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d170 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 72 65 67  -------.**   reg
2d171 2b 30 20 20 20 20 20 20 20 20 20 20 4f 4c 44 2e  +0          OLD.
2d172 72 6f 77 69 64 0a 2a 2a 20 20 20 72 65 67 2b 31  rowid.**   reg+1
2d173 20 20 20 20 20 20 20 20 20 20 4f 4c 44 2e 2a 20            OLD.* 
2d174 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 2d 6d 6f  value of left-mo
2d175 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 54 61  st column of pTa
2d176 62 0a 2a 2a 20 20 20 2e 2e 2e 20 20 20 20 20 20  b.**   ...      
2d177 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 72        ....**   r
2d178 65 67 2b 4e 20 20 20 20 20 20 20 20 20 20 4f 4c  eg+N          OL
2d179 44 2e 2a 20 76 61 6c 75 65 20 6f 66 20 72 69 67  D.* value of rig
2d17a 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f  ht-most column o
2d17b 66 20 70 54 61 62 0a 2a 2a 20 20 20 72 65 67 2b  f pTab.**   reg+
2d17c 4e 2b 31 20 20 20 20 20 20 20 20 4e 45 57 2e 72  N+1        NEW.r
2d17d 6f 77 69 64 0a 2a 2a 20 20 20 72 65 67 2b 4e 2b  owid.**   reg+N+
2d17e 32 20 20 20 20 20 20 20 20 4f 4c 44 2e 2a 20 76  2        OLD.* v
2d17f 61 6c 75 65 20 6f 66 20 6c 65 66 74 2d 6d 6f 73  alue of left-mos
2d180 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 54 61 62  t column of pTab
2d181 0a 2a 2a 20 20 20 2e 2e 2e 20 20 20 20 20 20 20  .**   ...       
2d182 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 72 65       ....**   re
2d183 67 2b 4e 2b 4e 2b 31 20 20 20 20 20 20 4e 45 57  g+N+N+1      NEW
2d184 2e 2a 20 76 61 6c 75 65 20 6f 66 20 72 69 67 68  .* value of righ
2d185 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66  t-most column of
2d186 20 70 54 61 62 0a 2a 2a 0a 2a 2a 20 46 6f 72 20   pTab.**.** For 
2d187 4f 4e 20 44 45 4c 45 54 45 20 74 72 69 67 67 65  ON DELETE trigge
2d188 72 73 2c 20 74 68 65 20 72 65 67 69 73 74 65 72  rs, the register
2d189 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  s containing the
2d18a 20 4e 45 57 2e 2a 20 76 61 6c 75 65 73 20 77 69   NEW.* values wi
2d18b 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 62 65 20 61  ll.** never be a
2d18c 63 63 65 73 73 65 64 20 62 79 20 74 68 65 20 74  ccessed by the t
2d18d 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2c 20  rigger program, 
2d18e 73 6f 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20  so they are not 
2d18f 61 6c 6c 6f 63 61 74 65 64 20 6f 72 20 0a 2a 2a  allocated or .**
2d190 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20 74 68   populated by th
2d191 65 20 63 61 6c 6c 65 72 20 28 74 68 65 72 65 20  e caller (there 
2d192 69 73 20 6e 6f 20 64 61 74 61 20 74 6f 20 70 6f  is no data to po
2d193 70 75 6c 61 74 65 20 74 68 65 6d 20 77 69 74 68  pulate them with
2d194 20 61 6e 79 77 61 79 29 2e 20 0a 2a 2a 20 53 69   anyway). .** Si
2d195 6d 69 6c 61 72 6c 79 2c 20 66 6f 72 20 4f 4e 20  milarly, for ON 
2d196 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 73 20  INSERT triggers 
2d197 74 68 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65  the values store
2d198 64 20 69 6e 20 74 68 65 20 4f 4c 44 2e 2a 20 72  d in the OLD.* r
2d199 65 67 69 73 74 65 72 73 0a 2a 2a 20 61 72 65 20  egisters.** are 
2d19a 6e 65 76 65 72 20 61 63 63 65 73 73 65 64 2c 20  never accessed, 
2d19b 61 6e 64 20 73 6f 20 61 72 65 20 6e 6f 74 20 61  and so are not a
2d19c 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20  llocated by the 
2d19d 63 61 6c 6c 65 72 2e 20 53 6f 2c 20 66 6f 72 20  caller. So, for 
2d19e 61 6e 0a 2a 2a 20 4f 4e 20 49 4e 53 45 52 54 20  an.** ON INSERT 
2d19f 74 72 69 67 67 65 72 2c 20 74 68 65 20 76 61 6c  trigger, the val
2d1a0 75 65 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  ue passed to thi
2d1a1 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 70 61  s function as pa
2d1a2 72 61 6d 65 74 65 72 20 72 65 67 0a 2a 2a 20 69  rameter reg.** i
2d1a3 73 20 6e 6f 74 20 61 20 72 65 61 64 61 62 6c 65  s not a readable
2d1a4 20 72 65 67 69 73 74 65 72 2c 20 61 6c 74 68 6f   register, altho
2d1a5 75 67 68 20 72 65 67 69 73 74 65 72 73 20 28 72  ugh registers (r
2d1a6 65 67 2b 4e 29 20 74 68 72 6f 75 67 68 20 0a 2a  eg+N) through .*
2d1a7 2a 20 28 72 65 67 2b 4e 2b 4e 2b 31 29 20 61 72  * (reg+N+N+1) ar
2d1a8 65 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  e..**.** Paramet
2d1a9 65 72 20 6f 72 63 6f 6e 66 20 69 73 20 74 68 65  er orconf is the
2d1aa 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 6c 69 63   default conflic
2d1ab 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67  t resolution alg
2d1ac 6f 72 69 74 68 6d 20 66 6f 72 20 74 68 65 0a 2a  orithm for the.*
2d1ad 2a 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  * trigger progra
2d1ae 6d 20 74 6f 20 75 73 65 20 28 52 45 50 4c 41 43  m to use (REPLAC
2d1af 45 2c 20 49 47 4e 4f 52 45 20 65 74 63 2e 29 2e  E, IGNORE etc.).
2d1b0 20 50 61 72 61 6d 65 74 65 72 20 69 67 6e 6f 72   Parameter ignor
2d1b1 65 4a 75 6d 70 0a 2a 2a 20 69 73 20 74 68 65 20  eJump.** is the 
2d1b2 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74  instruction that
2d1b3 20 63 6f 6e 74 72 6f 6c 20 73 68 6f 75 6c 64 20   control should 
2d1b4 6a 75 6d 70 20 74 6f 20 69 66 20 61 20 74 72 69  jump to if a tri
2d1b5 67 67 65 72 20 70 72 6f 67 72 61 6d 0a 2a 2a 20  gger program.** 
2d1b6 72 61 69 73 65 73 20 61 6e 20 49 47 4e 4f 52 45  raises an IGNORE
2d1b7 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 53   exception..*/.S
2d1b8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2d1b9 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  id sqlite3CodeRo
2d1ba 77 54 72 69 67 67 65 72 28 0a 20 20 50 61 72 73  wTrigger(.  Pars
2d1bb 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2d1bc 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
2d1bd 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  t */.  Trigger *
2d1be 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a 20 4c  pTrigger,   /* L
2d1bf 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73 20  ist of triggers 
2d1c0 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20 2a 2f  on table pTab */
2d1c1 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20  .  int op,      
2d1c2 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
2d1c3 66 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f  f TK_UPDATE, TK_
2d1c4 49 4e 53 45 52 54 2c 20 54 4b 5f 44 45 4c 45 54  INSERT, TK_DELET
2d1c5 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  E */.  ExprList 
2d1c6 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 43  *pChanges,  /* C
2d1c7 68 61 6e 67 65 73 20 6c 69 73 74 20 66 6f 72 20  hanges list for 
2d1c8 61 6e 79 20 55 50 44 41 54 45 20 4f 46 20 74 72  any UPDATE OF tr
2d1c9 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20  iggers */.  int 
2d1ca 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20 20 20  tr_tm,          
2d1cb 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47 47   /* One of TRIGG
2d1cc 45 52 5f 42 45 46 4f 52 45 2c 20 54 52 49 47 47  ER_BEFORE, TRIGG
2d1cd 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20 54 61  ER_AFTER */.  Ta
2d1ce 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
2d1cf 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
2d1d0 74 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72 73  to code triggers
2d1d1 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 72   from */.  int r
2d1d2 65 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  eg,             
2d1d3 2f 2a 20 54 68 65 20 66 69 72 73 74 20 69 6e 20  /* The first in 
2d1d4 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 67 69  an array of regi
2d1d5 73 74 65 72 73 20 28 73 65 65 20 61 62 6f 76 65  sters (see above
2d1d6 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e  ) */.  int orcon
2d1d7 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  f,          /* O
2d1d8 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63  N CONFLICT polic
2d1d9 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72  y */.  int ignor
2d1da 65 4a 75 6d 70 20 20 20 20 20 20 20 2f 2a 20 49  eJump       /* I
2d1db 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6a 75  nstruction to ju
2d1dc 6d 70 20 74 6f 20 66 6f 72 20 52 41 49 53 45 28  mp to for RAISE(
2d1dd 49 47 4e 4f 52 45 29 20 2a 2f 0a 29 7b 0a 20 20  IGNORE) */.){.  
2d1de 54 72 69 67 67 65 72 20 2a 70 3b 20 20 20 20 20  Trigger *p;     
2d1df 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20       /* Used to 
2d1e0 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  iterate through 
2d1e1 70 54 72 69 67 67 65 72 20 6c 69 73 74 20 2a 2f  pTrigger list */
2d1e2 0a 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d  ..  assert( op==
2d1e3 54 4b 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d  TK_UPDATE || op=
2d1e4 3d 54 4b 5f 49 4e 53 45 52 54 20 7c 7c 20 6f 70  =TK_INSERT || op
2d1e5 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 29 3b 0a 20  ==TK_DELETE );. 
2d1e6 20 61 73 73 65 72 74 28 20 74 72 5f 74 6d 3d 3d   assert( tr_tm==
2d1e7 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 7c  TRIGGER_BEFORE |
2d1e8 7c 20 74 72 5f 74 6d 3d 3d 54 52 49 47 47 45 52  | tr_tm==TRIGGER
2d1e9 5f 41 46 54 45 52 20 29 3b 0a 20 20 61 73 73 65  _AFTER );.  asse
2d1ea 72 74 28 20 28 6f 70 3d 3d 54 4b 5f 55 50 44 41  rt( (op==TK_UPDA
2d1eb 54 45 29 3d 3d 28 70 43 68 61 6e 67 65 73 21 3d  TE)==(pChanges!=
2d1ec 30 29 20 29 3b 0a 0a 20 20 66 6f 72 28 70 3d 70  0) );..  for(p=p
2d1ed 54 72 69 67 67 65 72 3b 20 70 3b 20 70 3d 70 2d  Trigger; p; p=p-
2d1ee 3e 70 4e 65 78 74 29 7b 0a 0a 20 20 20 20 2f 2a  >pNext){..    /*
2d1ef 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   Sanity checking
2d1f0 3a 20 20 54 68 65 20 73 63 68 65 6d 61 20 66 6f  :  The schema fo
2d1f1 72 20 74 68 65 20 74 72 69 67 67 65 72 20 61 6e  r the trigger an
2d1f2 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  d for the table 
2d1f3 61 72 65 0a 20 20 20 20 2a 2a 20 61 6c 77 61 79  are.    ** alway
2d1f4 73 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20  s defined.  The 
2d1f5 74 72 69 67 67 65 72 20 6d 75 73 74 20 62 65 20  trigger must be 
2d1f6 69 6e 20 74 68 65 20 73 61 6d 65 20 73 63 68 65  in the same sche
2d1f7 6d 61 20 61 73 20 74 68 65 20 74 61 62 6c 65 0a  ma as the table.
2d1f8 20 20 20 20 2a 2a 20 6f 72 20 65 6c 73 65 20 69      ** or else i
2d1f9 74 20 6d 75 73 74 20 62 65 20 61 20 54 45 4d 50  t must be a TEMP
2d1fa 20 74 72 69 67 67 65 72 2e 20 2a 2f 0a 20 20 20   trigger. */.   
2d1fb 20 61 73 73 65 72 74 28 20 70 2d 3e 70 53 63 68   assert( p->pSch
2d1fc 65 6d 61 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  ema!=0 );.    as
2d1fd 73 65 72 74 28 20 70 2d 3e 70 54 61 62 53 63 68  sert( p->pTabSch
2d1fe 65 6d 61 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  ema!=0 );.    as
2d1ff 73 65 72 74 28 20 70 2d 3e 70 53 63 68 65 6d 61  sert( p->pSchema
2d200 3d 3d 70 2d 3e 70 54 61 62 53 63 68 65 6d 61 20  ==p->pTabSchema 
2d201 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e  .         || p->
2d202 70 53 63 68 65 6d 61 3d 3d 70 50 61 72 73 65 2d  pSchema==pParse-
2d203 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68  >db->aDb[1].pSch
2d204 65 6d 61 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  ema );..    /* D
2d205 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
2d206 20 77 65 20 73 68 6f 75 6c 64 20 63 6f 64 65 20   we should code 
2d207 74 68 69 73 20 74 72 69 67 67 65 72 20 2a 2f 0a  this trigger */.
2d208 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 6f      if( p->op==o
2d209 70 20 0a 20 20 20 20 20 26 26 20 70 2d 3e 74 72  p .     && p->tr
2d20a 5f 74 6d 3d 3d 74 72 5f 74 6d 20 0a 20 20 20 20  _tm==tr_tm .    
2d20b 20 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f   && checkColumnO
2d20c 76 65 72 6c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d  verlap(p->pColum
2d20d 6e 73 2c 20 70 43 68 61 6e 67 65 73 29 0a 20 20  ns, pChanges).  
2d20e 20 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74    ){.      sqlit
2d20f 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
2d210 44 69 72 65 63 74 28 70 50 61 72 73 65 2c 20 70  Direct(pParse, p
2d211 2c 20 70 54 61 62 2c 20 72 65 67 2c 20 6f 72 63  , pTab, reg, orc
2d212 6f 6e 66 2c 20 69 67 6e 6f 72 65 4a 75 6d 70 29  onf, ignoreJump)
2d213 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
2d214 2a 0a 2a 2a 20 54 72 69 67 67 65 72 73 20 66 69  *.** Triggers fi
2d215 72 65 64 20 62 79 20 55 50 44 41 54 45 20 6f 72  red by UPDATE or
2d216 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e   DELETE statemen
2d217 74 73 20 6d 61 79 20 61 63 63 65 73 73 20 76 61  ts may access va
2d218 6c 75 65 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69  lues stored.** i
2d219 6e 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65 75  n the old.* pseu
2d21a 64 6f 2d 74 61 62 6c 65 2e 20 54 68 69 73 20 66  do-table. This f
2d21b 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
2d21c 61 20 33 32 2d 62 69 74 20 62 69 74 6d 61 73 6b  a 32-bit bitmask
2d21d 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 77  .** indicating w
2d21e 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  hich columns of 
2d21f 74 68 65 20 6f 6c 64 2e 2a 20 74 61 62 6c 65 20  the old.* table 
2d220 61 63 74 75 61 6c 6c 79 20 61 72 65 20 75 73 65  actually are use
2d221 64 20 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73  d by.** triggers
2d222 2e 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  . This informati
2d223 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 20 62  on may be used b
2d224 79 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20  y the caller to 
2d225 61 76 6f 69 64 20 68 61 76 69 6e 67 0a 2a 2a 20  avoid having.** 
2d226 74 6f 20 6c 6f 61 64 20 74 68 65 20 65 6e 74 69  to load the enti
2d227 72 65 20 6f 6c 64 2e 2a 20 72 65 63 6f 72 64 20  re old.* record 
2d228 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 77 68 65 6e  into memory when
2d229 20 65 78 65 63 75 74 69 6e 67 20 61 6e 20 55 50   executing an UP
2d22a 44 41 54 45 0a 2a 2a 20 6f 72 20 44 45 4c 45 54  DATE.** or DELET
2d22b 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a  E command..**.**
2d22c 20 42 69 74 20 30 20 6f 66 20 74 68 65 20 72 65   Bit 0 of the re
2d22d 74 75 72 6e 65 64 20 6d 61 73 6b 20 69 73 20 73  turned mask is s
2d22e 65 74 20 69 66 20 74 68 65 20 6c 65 66 74 2d 6d  et if the left-m
2d22f 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  ost column of th
2d230 65 0a 2a 2a 20 74 61 62 6c 65 20 6d 61 79 20 62  e.** table may b
2d231 65 20 61 63 63 65 73 73 65 64 20 75 73 69 6e 67  e accessed using
2d232 20 61 6e 20 6f 6c 64 2e 3c 63 6f 6c 3e 20 72 65   an old.<col> re
2d233 66 65 72 65 6e 63 65 2e 20 42 69 74 20 31 20 69  ference. Bit 1 i
2d234 73 20 73 65 74 20 69 66 0a 2a 2a 20 74 68 65 20  s set if.** the 
2d235 73 65 63 6f 6e 64 20 6c 65 66 74 6d 6f 73 74 20  second leftmost 
2d236 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20  column value is 
2d237 72 65 71 75 69 72 65 64 2c 20 61 6e 64 20 73 6f  required, and so
2d238 20 6f 6e 2e 20 49 66 20 74 68 65 72 65 0a 2a 2a   on. If there.**
2d239 20 61 72 65 20 6d 6f 72 65 20 74 68 61 6e 20 33   are more than 3
2d23a 32 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  2 columns in the
2d23b 20 74 61 62 6c 65 2c 20 61 6e 64 20 61 74 20 6c   table, and at l
2d23c 65 61 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 20  east one of the 
2d23d 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 77 69 74 68 20  columns.** with 
2d23e 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 72  an index greater
2d23f 20 74 68 61 6e 20 33 32 20 6d 61 79 20 62 65 20   than 32 may be 
2d240 61 63 63 65 73 73 65 64 2c 20 30 78 66 66 66 66  accessed, 0xffff
2d241 66 66 66 66 20 69 73 20 72 65 74 75 72 6e 65 64  ffff is returned
2d242 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f  ..**.** It is no
2d243 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65  t possible to de
2d244 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 6f  termine if the o
2d245 6c 64 2e 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20  ld.rowid column 
2d246 69 73 20 61 63 63 65 73 73 65 64 0a 2a 2a 20 62  is accessed.** b
2d247 79 20 74 72 69 67 67 65 72 73 2e 20 54 68 65 20  y triggers. The 
2d248 63 61 6c 6c 65 72 20 6d 75 73 74 20 61 6c 77 61  caller must alwa
2d249 79 73 20 61 73 73 75 6d 65 20 74 68 61 74 20 69  ys assume that i
2d24a 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  t is..**.** Ther
2d24b 65 20 69 73 20 6e 6f 20 65 71 75 69 76 61 6c 65  e is no equivale
2d24c 6e 74 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  nt function for 
2d24d 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73  new.* references
2d24e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2d24f 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 54  ATE u32 sqlite3T
2d250 72 69 67 67 65 72 4f 6c 64 6d 61 73 6b 28 0a 20  riggerOldmask(. 
2d251 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2d252 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63        /* Parse c
2d253 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67  ontext */.  Trig
2d254 67 65 72 20 2a 70 54 72 69 67 67 65 72 2c 20 20  ger *pTrigger,  
2d255 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67   /* List of trig
2d256 67 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 54  gers on table pT
2d257 61 62 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ab */.  ExprList
2d258 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20   *pChanges,  /* 
2d259 43 68 61 6e 67 65 73 20 6c 69 73 74 20 66 6f 72  Changes list for
2d25a 20 61 6e 79 20 55 50 44 41 54 45 20 4f 46 20 74   any UPDATE OF t
2d25b 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 54 61 62  riggers */.  Tab
2d25c 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
2d25d 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74    /* The table t
2d25e 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72 73 20  o code triggers 
2d25f 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  from */.  int or
2d260 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20 20 2f  conf           /
2d261 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e  * Default ON CON
2d262 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66 6f 72  FLICT policy for
2d263 20 74 72 69 67 67 65 72 20 73 74 65 70 73 20 2a   trigger steps *
2d264 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74  /.){.  const int
2d265 20 6f 70 20 3d 20 70 43 68 61 6e 67 65 73 20 3f   op = pChanges ?
2d266 20 54 4b 5f 55 50 44 41 54 45 20 3a 20 54 4b 5f   TK_UPDATE : TK_
2d267 44 45 4c 45 54 45 3b 0a 20 20 75 33 32 20 6d 61  DELETE;.  u32 ma
2d268 73 6b 20 3d 20 30 3b 0a 20 20 54 72 69 67 67 65  sk = 0;.  Trigge
2d269 72 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 70 3d 70  r *p;..  for(p=p
2d26a 54 72 69 67 67 65 72 3b 20 70 3b 20 70 3d 70 2d  Trigger; p; p=p-
2d26b 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
2d26c 20 70 2d 3e 6f 70 3d 3d 6f 70 20 26 26 20 63 68   p->op==op && ch
2d26d 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70  eckColumnOverlap
2d26e 28 70 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 70 43 68  (p->pColumns,pCh
2d26f 61 6e 67 65 73 29 20 29 7b 0a 20 20 20 20 20 20  anges) ){.      
2d270 54 72 69 67 67 65 72 50 72 67 20 2a 70 50 72 67  TriggerPrg *pPrg
2d271 3b 0a 20 20 20 20 20 20 70 50 72 67 20 3d 20 67  ;.      pPrg = g
2d272 65 74 52 6f 77 54 72 69 67 67 65 72 28 70 50 61  etRowTrigger(pPa
2d273 72 73 65 2c 20 70 2c 20 70 54 61 62 2c 20 6f 72  rse, p, pTab, or
2d274 63 6f 6e 66 29 3b 0a 20 20 20 20 20 20 69 66 28  conf);.      if(
2d275 20 70 50 72 67 20 29 7b 0a 20 20 20 20 20 20 20   pPrg ){.       
2d276 20 6d 61 73 6b 20 7c 3d 20 70 50 72 67 2d 3e 6f   mask |= pPrg->o
2d277 6c 64 6d 61 73 6b 3b 0a 20 20 20 20 20 20 7d 0a  ldmask;.      }.
2d278 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
2d279 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 23 65 6e  urn mask;.}..#en
2d27a 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
2d27b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
2d27c 47 45 52 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  GER) */../******
2d27d 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
2d27e 74 72 69 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a  trigger.c ******
2d27f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d281 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2d282 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
2d283 69 6c 65 20 75 70 64 61 74 65 2e 63 20 2a 2a 2a  ile update.c ***
2d284 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d285 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d286 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
2d287 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
2d288 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
2d289 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
2d28a 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
2d28b 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
2d28c 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
2d28d 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
2d28e 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
2d28f 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
2d290 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
2d291 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
2d292 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
2d293 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
2d294 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
2d295 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
2d296 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
2d297 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
2d298 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
2d299 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
2d29a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d29b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d29c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d29d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
2d29e 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
2d29f 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75  tains C code rou
2d2a0 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63  tines that are c
2d2a1 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
2d2a2 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65  ser.** to handle
2d2a3 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
2d2a4 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75  ts..**.** $Id: u
2d2a5 70 64 61 74 65 2e 63 2c 76 20 31 2e 32 30 37 20  pdate.c,v 1.207 
2d2a6 32 30 30 39 2f 30 38 2f 30 38 20 31 38 3a 30 31  2009/08/08 18:01
2d2a7 3a 30 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :08 drh Exp $.*/
2d2a8 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2d2a9 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2d2aa 4c 45 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  LE./* Forward de
2d2ab 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61  claration */.sta
2d2ac 74 69 63 20 76 6f 69 64 20 75 70 64 61 74 65 56  tic void updateV
2d2ad 69 72 74 75 61 6c 54 61 62 6c 65 28 0a 20 20 50  irtualTable(.  P
2d2ae 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2d2af 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69      /* The parsi
2d2b0 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2d2b1 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20  SrcList *pSrc,  
2d2b2 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74       /* The virt
2d2b3 75 61 6c 20 74 61 62 6c 65 20 74 6f 20 62 65 20  ual table to be 
2d2b4 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20 20 54 61  modified */.  Ta
2d2b5 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
2d2b6 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
2d2b7 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70  l table */.  Exp
2d2b8 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c  rList *pChanges,
2d2b9 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73    /* The columns
2d2ba 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 74 68   to change in th
2d2bb 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  e UPDATE stateme
2d2bc 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52  nt */.  Expr *pR
2d2bd 6f 77 69 64 45 78 70 72 2c 20 20 20 20 2f 2a 20  owidExpr,    /* 
2d2be 45 78 70 72 65 73 73 69 6f 6e 20 75 73 65 64 20  Expression used 
2d2bf 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65  to recompute the
2d2c0 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20   rowid */.  int 
2d2c1 2a 61 58 52 65 66 2c 20 20 20 20 20 20 20 20 20  *aXRef,         
2d2c2 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d   /* Mapping from
2d2c3 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70 54 61 62   columns of pTab
2d2c4 20 74 6f 20 65 6e 74 72 69 65 73 20 69 6e 20 70   to entries in p
2d2c5 43 68 61 6e 67 65 73 20 2a 2f 0a 20 20 45 78 70  Changes */.  Exp
2d2c6 72 20 2a 70 57 68 65 72 65 20 20 20 20 20 20 20  r *pWhere       
2d2c7 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73    /* WHERE claus
2d2c8 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45 20  e of the UPDATE 
2d2c9 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 3b 0a  statement */.);.
2d2ca 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2d2cb 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2d2cc 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  LE */../*.** The
2d2cd 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63   most recently c
2d2ce 6f 64 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e  oded instruction
2d2cf 20 77 61 73 20 61 6e 20 4f 50 5f 43 6f 6c 75 6d   was an OP_Colum
2d2d0 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  n to retrieve th
2d2d1 65 0a 2a 2a 20 69 2d 74 68 20 63 6f 6c 75 6d 6e  e.** i-th column
2d2d2 20 6f 66 20 74 61 62 6c 65 20 70 54 61 62 2e 20   of table pTab. 
2d2d3 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
2d2d4 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 74  s the P4 paramet
2d2d5 65 72 20 6f 66 20 74 68 65 20 0a 2a 2a 20 4f 50  er of the .** OP
2d2d6 5f 43 6f 6c 75 6d 6e 20 74 6f 20 74 68 65 20 64  _Column to the d
2d2d7 65 66 61 75 6c 74 20 76 61 6c 75 65 2c 20 69 66  efault value, if
2d2d8 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   any..**.** The 
2d2d9 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66  default value of
2d2da 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 73 70 65   a column is spe
2d2db 63 69 66 69 65 64 20 62 79 20 61 20 44 45 46 41  cified by a DEFA
2d2dc 55 4c 54 20 63 6c 61 75 73 65 20 69 6e 20 74 68  ULT clause in th
2d2dd 65 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65 66  e .** column def
2d2de 69 6e 69 74 69 6f 6e 2e 20 54 68 69 73 20 77 61  inition. This wa
2d2df 73 20 65 69 74 68 65 72 20 73 75 70 70 6c 69 65  s either supplie
2d2e0 64 20 62 79 20 74 68 65 20 75 73 65 72 20 77 68  d by the user wh
2d2e1 65 6e 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20  en the table.** 
2d2e2 77 61 73 20 63 72 65 61 74 65 64 2c 20 6f 72 20  was created, or 
2d2e3 61 64 64 65 64 20 6c 61 74 65 72 20 74 6f 20 74  added later to t
2d2e4 68 65 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74  he table definit
2d2e5 69 6f 6e 20 62 79 20 61 6e 20 41 4c 54 45 52 20  ion by an ALTER 
2d2e6 54 41 42 4c 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64  TABLE.** command
2d2e7 2e 20 49 66 20 74 68 65 20 6c 61 74 74 65 72 2c  . If the latter,
2d2e8 20 74 68 65 6e 20 74 68 65 20 72 6f 77 2d 72 65   then the row-re
2d2e9 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61 62  cords in the tab
2d2ea 6c 65 20 62 74 72 65 65 20 6f 6e 20 64 69 73 6b  le btree on disk
2d2eb 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 63 6f 6e 74  .** may not cont
2d2ec 61 69 6e 20 61 20 76 61 6c 75 65 20 66 6f 72 20  ain a value for 
2d2ed 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74  the column and t
2d2ee 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
2d2ef 2c 20 74 61 6b 65 6e 0a 2a 2a 20 66 72 6f 6d 20  , taken.** from 
2d2f0 74 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72  the P4 parameter
2d2f1 20 6f 66 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d   of the OP_Colum
2d2f2 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 20 69  n instruction, i
2d2f3 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65  s returned inste
2d2f4 61 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f  ad..** If the fo
2d2f5 72 6d 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20 72  rmer, then all r
2d2f6 6f 77 2d 72 65 63 6f 72 64 73 20 61 72 65 20 67  ow-records are g
2d2f7 75 61 72 61 6e 74 65 65 64 20 74 6f 20 69 6e 63  uaranteed to inc
2d2f8 6c 75 64 65 20 61 20 76 61 6c 75 65 0a 2a 2a 20  lude a value.** 
2d2f9 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61  for the column a
2d2fa 6e 64 20 74 68 65 20 50 34 20 76 61 6c 75 65 20  nd the P4 value 
2d2fb 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e  is not required.
2d2fc 0a 2a 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 64 65  .**.** Column de
2d2fd 66 69 6e 69 74 69 6f 6e 73 20 63 72 65 61 74 65  finitions create
2d2fe 64 20 62 79 20 61 6e 20 41 4c 54 45 52 20 54 41  d by an ALTER TA
2d2ff 42 4c 45 20 63 6f 6d 6d 61 6e 64 20 6d 61 79 20  BLE command may 
2d300 6f 6e 6c 79 20 68 61 76 65 20 0a 2a 2a 20 6c 69  only have .** li
2d301 74 65 72 61 6c 20 64 65 66 61 75 6c 74 20 76 61  teral default va
2d302 6c 75 65 73 20 73 70 65 63 69 66 69 65 64 3a 20  lues specified: 
2d303 61 20 6e 75 6d 62 65 72 2c 20 6e 75 6c 6c 20 6f  a number, null o
2d304 72 20 61 20 73 74 72 69 6e 67 2e 20 28 49 66 20  r a string. (If 
2d305 61 20 6d 6f 72 65 0a 2a 2a 20 63 6f 6d 70 6c 69  a more.** compli
2d306 63 61 74 65 64 20 64 65 66 61 75 6c 74 20 65 78  cated default ex
2d307 70 72 65 73 73 69 6f 6e 20 76 61 6c 75 65 20 77  pression value w
2d308 61 73 20 70 72 6f 76 69 64 65 64 2c 20 69 74 20  as provided, it 
2d309 69 73 20 65 76 61 6c 75 61 74 65 64 20 0a 2a 2a  is evaluated .**
2d30a 20 77 68 65 6e 20 74 68 65 20 41 4c 54 45 52 20   when the ALTER 
2d30b 54 41 42 4c 45 20 69 73 20 65 78 65 63 75 74 65  TABLE is execute
2d30c 64 20 61 6e 64 20 6f 6e 65 20 6f 66 20 74 68 65  d and one of the
2d30d 20 6c 69 74 65 72 61 6c 20 76 61 6c 75 65 73 20   literal values 
2d30e 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20  written.** into 
2d30f 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  the sqlite_maste
2d310 72 20 74 61 62 6c 65 2e 29 0a 2a 2a 0a 2a 2a 20  r table.).**.** 
2d311 54 68 65 72 65 66 6f 72 65 2c 20 74 68 65 20 50  Therefore, the P
2d312 34 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f  4 parameter is o
2d313 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 20  nly required if 
2d314 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
2d315 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6c  e for.** the col
2d316 75 6d 6e 20 69 73 20 61 20 6c 69 74 65 72 61 6c  umn is a literal
2d317 20 6e 75 6d 62 65 72 2c 20 73 74 72 69 6e 67 20   number, string 
2d318 6f 72 20 6e 75 6c 6c 2e 20 54 68 65 20 73 71 6c  or null. The sql
2d319 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70  ite3ValueFromExp
2d31a 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  r().** function 
2d31b 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 74 72  is capable of tr
2d31c 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65 73 65  ansforming these
2d31d 20 74 79 70 65 73 20 6f 66 20 65 78 70 72 65 73   types of expres
2d31e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 73 71  sions into.** sq
2d31f 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
2d320 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61  cts..**.** If pa
2d321 72 61 6d 65 74 65 72 20 69 52 65 67 20 69 73 20  rameter iReg is 
2d322 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 63 6f  not negative, co
2d323 64 65 20 61 6e 20 4f 50 5f 52 65 61 6c 41 66 66  de an OP_RealAff
2d324 69 6e 69 74 79 20 69 6e 73 74 72 75 63 74 69 6f  inity instructio
2d325 6e 0a 2a 2a 20 6f 6e 20 72 65 67 69 73 74 65 72  n.** on register
2d326 20 69 52 65 67 2e 20 54 68 69 73 20 69 73 20 75   iReg. This is u
2d327 73 65 64 20 77 68 65 6e 20 61 6e 20 65 71 75 69  sed when an equi
2d328 76 61 6c 65 6e 74 20 69 6e 74 65 67 65 72 20 76  valent integer v
2d329 61 6c 75 65 20 69 73 20 0a 2a 2a 20 73 74 6f 72  alue is .** stor
2d32a 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61  ed in place of a
2d32b 6e 20 38 2d 62 79 74 65 20 66 6c 6f 61 74 69 6e  n 8-byte floatin
2d32c 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 6e  g point value in
2d32d 20 6f 72 64 65 72 20 74 6f 20 73 61 76 65 20 0a   order to save .
2d32e 2a 2a 20 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c  ** space..*/.SQL
2d32f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2d330 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
2d331 66 61 75 6c 74 28 56 64 62 65 20 2a 76 2c 20 54  fault(Vdbe *v, T
2d332 61 62 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20  able *pTab, int 
2d333 69 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 20  i, int iReg){.  
2d334 61 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 20  assert( pTab!=0 
2d335 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e  );.  if( !pTab->
2d336 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73  pSelect ){.    s
2d337 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
2d338 61 6c 75 65 3b 0a 20 20 20 20 75 38 20 65 6e 63  alue;.    u8 enc
2d339 20 3d 20 45 4e 43 28 73 71 6c 69 74 65 33 56 64   = ENC(sqlite3Vd
2d33a 62 65 44 62 28 76 29 29 3b 0a 20 20 20 20 43 6f  beDb(v));.    Co
2d33b 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54  lumn *pCol = &pT
2d33c 61 62 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20  ab->aCol[i];.   
2d33d 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2d33e 20 22 25 73 2e 25 73 22 2c 20 70 54 61 62 2d 3e   "%s.%s", pTab->
2d33f 7a 4e 61 6d 65 2c 20 70 43 6f 6c 2d 3e 7a 4e 61  zName, pCol->zNa
2d340 6d 65 29 29 3b 0a 20 20 20 20 61 73 73 65 72 74  me));.    assert
2d341 28 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  ( i<pTab->nCol )
2d342 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  ;.    sqlite3Val
2d343 75 65 46 72 6f 6d 45 78 70 72 28 73 71 6c 69 74  ueFromExpr(sqlit
2d344 65 33 56 64 62 65 44 62 28 76 29 2c 20 70 43 6f  e3VdbeDb(v), pCo
2d345 6c 2d 3e 70 44 66 6c 74 2c 20 65 6e 63 2c 20 0a  l->pDflt, enc, .
2d346 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d347 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 61           pCol->a
2d348 66 66 69 6e 69 74 79 2c 20 26 70 56 61 6c 75 65  ffinity, &pValue
2d349 29 3b 0a 20 20 20 20 69 66 28 20 70 56 61 6c 75  );.    if( pValu
2d34a 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
2d34b 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
2d34c 2c 20 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68 61  , -1, (const cha
2d34d 72 20 2a 29 70 56 61 6c 75 65 2c 20 50 34 5f 4d  r *)pValue, P4_M
2d34e 45 4d 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  EM);.    }.#ifnd
2d34f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
2d350 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
2d351 20 20 69 66 28 20 69 52 65 67 3e 3d 30 20 26 26    if( iReg>=0 &&
2d352 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 61   pTab->aCol[i].a
2d353 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
2d354 41 46 46 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20  AFF_REAL ){.    
2d355 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d356 4f 70 31 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66  Op1(v, OP_RealAf
2d357 66 69 6e 69 74 79 2c 20 69 52 65 67 29 3b 0a 20  finity, iReg);. 
2d358 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
2d359 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73  }../*.** Process
2d35a 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65   an UPDATE state
2d35b 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 55 50  ment..**.**   UP
2d35c 44 41 54 45 20 4f 52 20 49 47 4e 4f 52 45 20 74  DATE OR IGNORE t
2d35d 61 62 6c 65 5f 77 78 79 7a 20 53 45 54 20 61 3d  able_wxyz SET a=
2d35e 62 2c 20 63 3d 64 20 57 48 45 52 45 20 65 3c 35  b, c=d WHERE e<5
2d35f 20 41 4e 44 20 66 20 4e 4f 54 20 4e 55 4c 4c 3b   AND f NOT NULL;
2d360 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5c 5f 5f  .**          \__
2d361 5f 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f 5f 5f 5f 5f  _____/ \________
2d362 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 2f 20 20  /     \______/  
2d363 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f       \__________
2d364 5f 5f 5f 5f 5f 5f 2f 0a 2a 20 20 20 20 20 20 20  ______/.*       
2d365 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 20 20 70       onError   p
2d366 54 61 62 4c 69 73 74 20 20 20 20 20 20 70 43 68  TabList      pCh
2d367 61 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 20  anges           
2d368 20 20 70 57 68 65 72 65 0a 2a 2f 0a 53 51 4c 49    pWhere.*/.SQLI
2d369 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2d36a 73 71 6c 69 74 65 33 55 70 64 61 74 65 28 0a 20  sqlite3Update(. 
2d36b 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2d36c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2d36d 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2d36e 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
2d36f 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 54 68 65  List,     /* The
2d370 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 20   table in which 
2d371 77 65 20 73 68 6f 75 6c 64 20 63 68 61 6e 67 65  we should change
2d372 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70   things */.  Exp
2d373 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c  rList *pChanges,
2d374 20 20 20 20 2f 2a 20 54 68 69 6e 67 73 20 74 6f      /* Things to
2d375 20 62 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20   be changed */. 
2d376 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20   Expr *pWhere,  
2d377 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
2d378 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61  HERE clause.  Ma
2d379 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 69  y be null */.  i
2d37a 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20 20 20 20  nt onError      
2d37b 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20        /* How to 
2d37c 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e  handle constrain
2d37d 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20  t errors */.){. 
2d37e 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
2d37f 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2d380 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 54 61  counters */.  Ta
2d381 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
2d382 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
2d383 65 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20  e to be updated 
2d384 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20  */.  int addr = 
2d385 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56  0;          /* V
2d386 44 42 45 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  DBE instruction 
2d387 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 73  address of the s
2d388 74 61 72 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70  tart of the loop
2d389 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20   */.  WhereInfo 
2d38a 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20  *pWInfo;     /* 
2d38b 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
2d38c 74 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  t the WHERE clau
2d38d 73 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b  se */.  Vdbe *v;
2d38e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2d38f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 64 61  * The virtual da
2d390 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f  tabase engine */
2d391 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
2d392 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
2d393 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e   looping over in
2d394 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  dices */.  int n
2d395 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
2d396 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69    /* Number of i
2d397 6e 64 69 63 65 73 20 74 68 61 74 20 6e 65 65 64  ndices that need
2d398 20 75 70 64 61 74 69 6e 67 20 2a 2f 0a 20 20 69   updating */.  i
2d399 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20  nt iCur;        
2d39a 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75        /* VDBE Cu
2d39b 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 70  rsor number of p
2d39c 54 61 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  Tab */.  sqlite3
2d39d 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
2d39e 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2d39f 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69  structure */.  i
2d3a0 6e 74 20 2a 61 52 65 67 49 64 78 20 3d 20 30 3b  nt *aRegIdx = 0;
2d3a1 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 72 65 67        /* One reg
2d3a2 69 73 74 65 72 20 61 73 73 69 67 6e 65 64 20 74  ister assigned t
2d3a3 6f 20 65 61 63 68 20 69 6e 64 65 78 20 74 6f 20  o each index to 
2d3a4 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20  be updated */.  
2d3a5 69 6e 74 20 2a 61 58 52 65 66 20 3d 20 30 3b 20  int *aXRef = 0; 
2d3a6 20 20 20 20 20 20 20 2f 2a 20 61 58 52 65 66 5b         /* aXRef[
2d3a7 69 5d 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  i] is the index 
2d3a8 69 6e 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 5d  in pChanges->a[]
2d3a9 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20   of the.        
2d3aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d3ab 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f   ** an expressio
2d3ac 6e 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63  n for the i-th c
2d3ad 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62  olumn of the tab
2d3ae 6c 65 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  le..            
2d3af 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
2d3b0 61 58 52 65 66 5b 69 5d 3d 3d 2d 31 20 69 66 20  aXRef[i]==-1 if 
2d3b1 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20  the i-th column 
2d3b2 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2e 20  is not changed. 
2d3b3 2a 2f 0a 20 20 69 6e 74 20 63 68 6e 67 52 6f 77  */.  int chngRow
2d3b4 69 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  id;         /* T
2d3b5 72 75 65 20 69 66 20 74 68 65 20 72 65 63 6f 72  rue if the recor
2d3b6 64 20 6e 75 6d 62 65 72 20 69 73 20 62 65 69 6e  d number is bein
2d3b7 67 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 45  g changed */.  E
2d3b8 78 70 72 20 2a 70 52 6f 77 69 64 45 78 70 72 20  xpr *pRowidExpr 
2d3b9 3d 20 30 3b 20 20 2f 2a 20 45 78 70 72 65 73 73  = 0;  /* Express
2d3ba 69 6f 6e 20 64 65 66 69 6e 69 6e 67 20 74 68 65  ion defining the
2d3bb 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62   new record numb
2d3bc 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e  er */.  int open
2d3bd 41 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  All = 0;       /
2d3be 2a 20 54 72 75 65 20 69 66 20 61 6c 6c 20 69 6e  * True if all in
2d3bf 64 69 63 65 73 20 6e 65 65 64 20 74 6f 20 62 65  dices need to be
2d3c0 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 41 75 74   opened */.  Aut
2d3c1 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78  hContext sContex
2d3c2 74 3b 20 20 2f 2a 20 54 68 65 20 61 75 74 68 6f  t;  /* The autho
2d3c3 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74  rization context
2d3c4 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
2d3c5 74 20 73 4e 43 3b 20 20 20 20 20 20 20 2f 2a 20  t sNC;       /* 
2d3c6 54 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74  The name-context
2d3c7 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72   to resolve expr
2d3c8 65 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20  essions in */.  
2d3c9 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
2d3ca 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2d3cb 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  se containing th
2d3cc 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70  e table being up
2d3cd 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a  dated */.  int j
2d3ce 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
2d3cf 20 20 2f 2a 20 41 64 64 72 65 73 73 65 73 20 6f    /* Addresses o
2d3d0 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69  f jump instructi
2d3d1 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6b 4f  ons */.  int okO
2d3d2 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 20  nePass;         
2d3d3 2f 2a 20 54 72 75 65 20 66 6f 72 20 6f 6e 65 2d  /* True for one-
2d3d4 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 77  pass algorithm w
2d3d5 69 74 68 6f 75 74 20 74 68 65 20 46 49 46 4f 20  ithout the FIFO 
2d3d6 2a 2f 0a 20 20 69 6e 74 20 68 61 73 46 4b 3b 20  */.  int hasFK; 
2d3d7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2d3d8 72 75 65 20 69 66 20 66 6f 72 65 69 67 6e 20 6b  rue if foreign k
2d3d9 65 79 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73  ey processing is
2d3da 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 0a 23 69   required */..#i
2d3db 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2d3dc 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20  T_TRIGGER.  int 
2d3dd 69 73 56 69 65 77 3b 20 20 20 20 20 20 20 20 20  isView;         
2d3de 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 79 69           /* Tryi
2d3df 6e 67 20 74 6f 20 75 70 64 61 74 65 20 61 20 76  ng to update a v
2d3e0 69 65 77 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  iew */.  Trigger
2d3e1 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20   *pTrigger;     
2d3e2 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
2d3e3 20 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61   triggers on pTa
2d3e4 62 2c 20 69 66 20 72 65 71 75 69 72 65 64 20 2a  b, if required *
2d3e5 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52  /.#endif..  /* R
2d3e6 65 67 69 73 74 65 72 20 41 6c 6c 6f 63 61 74 69  egister Allocati
2d3e7 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ons */.  int reg
2d3e8 52 6f 77 43 6f 75 6e 74 20 3d 20 30 3b 20 20 20  RowCount = 0;   
2d3e9 2f 2a 20 41 20 63 6f 75 6e 74 20 6f 66 20 72 6f  /* A count of ro
2d3ea 77 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20  ws changed */.  
2d3eb 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77 69 64 3b  int regOldRowid;
2d3ec 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6c         /* The ol
2d3ed 64 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74  d rowid */.  int
2d3ee 20 72 65 67 4e 65 77 52 6f 77 69 64 3b 20 20 20   regNewRowid;   
2d3ef 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72      /* The new r
2d3f0 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65  owid */.  int re
2d3f1 67 4e 65 77 3b 0a 20 20 69 6e 74 20 72 65 67 4f  gNew;.  int regO
2d3f2 6c 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65  ld = 0;.  int re
2d3f3 67 52 6f 77 53 65 74 20 3d 20 30 3b 20 20 20 20  gRowSet = 0;    
2d3f4 20 2f 2a 20 52 6f 77 73 65 74 20 6f 66 20 72 6f   /* Rowset of ro
2d3f5 77 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 64  ws to be updated
2d3f6 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63   */.  int regRec
2d3f7 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2d3f8 52 65 67 69 73 74 65 72 20 75 73 65 64 20 66 6f  Register used fo
2d3f9 72 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f  r new table reco
2d3fa 72 64 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a  rd to insert */.
2d3fb 0a 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74  .  memset(&sCont
2d3fc 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  ext, 0, sizeof(s
2d3fd 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 64 62 20  Context));.  db 
2d3fe 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2d3ff 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
2d400 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
2d401 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
2d402 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2d403 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
2d404 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  TabList->nSrc==1
2d405 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65   );..  /* Locate
2d406 20 74 68 65 20 74 61 62 6c 65 20 77 68 69 63 68   the table which
2d407 20 77 65 20 77 61 6e 74 20 74 6f 20 75 70 64 61   we want to upda
2d408 74 65 2e 20 0a 20 20 2a 2f 0a 20 20 70 54 61 62  te. .  */.  pTab
2d409 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
2d40a 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20  tLookup(pParse, 
2d40b 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28  pTabList);.  if(
2d40c 20 70 54 61 62 3d 3d 30 20 29 20 67 6f 74 6f 20   pTab==0 ) goto 
2d40d 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
2d40e 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
2d40f 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
2d410 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
2d411 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a 20 46  Schema);..  /* F
2d412 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20  igure out if we 
2d413 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72  have any trigger
2d414 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62  s and if the tab
2d415 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 75 70  le being.  ** up
2d416 64 61 74 65 64 20 69 73 20 61 20 76 69 65 77 2e  dated is a view.
2d417 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
2d418 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
2d419 52 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 73  R.  pTrigger = s
2d41a 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78  qlite3TriggersEx
2d41b 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ist(pParse, pTab
2d41c 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68  , TK_UPDATE, pCh
2d41d 61 6e 67 65 73 2c 20 30 29 3b 0a 20 20 69 73 56  anges, 0);.  isV
2d41e 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c  iew = pTab->pSel
2d41f 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20  ect!=0;.#else.# 
2d420 64 65 66 69 6e 65 20 70 54 72 69 67 67 65 72 20  define pTrigger 
2d421 30 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69 65  0.# define isVie
2d422 77 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  w 0.#endif.#ifde
2d423 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2d424 45 57 0a 23 20 75 6e 64 65 66 20 69 73 56 69 65  EW.# undef isVie
2d425 77 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69 65  w.# define isVie
2d426 77 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66  w 0.#endif..  if
2d427 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  ( sqlite3ViewGet
2d428 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
2d429 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  se, pTab) ){.   
2d42a 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
2d42b 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20  anup;.  }.  if( 
2d42c 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c  sqlite3IsReadOnl
2d42d 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  y(pParse, pTab, 
2d42e 28 70 54 72 69 67 67 65 72 3f 31 3a 30 29 29 20  (pTrigger?1:0)) 
2d42f 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61  ){.    goto upda
2d430 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  te_cleanup;.  }.
2d431 20 20 61 58 52 65 66 20 3d 20 73 71 6c 69 74 65    aXRef = sqlite
2d432 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2d433 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2a 20 70   sizeof(int) * p
2d434 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 69  Tab->nCol );.  i
2d435 66 28 20 61 58 52 65 66 3d 3d 30 20 29 20 67 6f  f( aXRef==0 ) go
2d436 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
2d437 70 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  p;.  for(i=0; i<
2d438 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
2d439 20 61 58 52 65 66 5b 69 5d 20 3d 20 2d 31 3b 0a   aXRef[i] = -1;.
2d43a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
2d43b 20 63 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65   cursors for the
2d43c 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 74   main database t
2d43d 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 61 6c 6c  able and for all
2d43e 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 20 54   indices..  ** T
2d43f 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73  he index cursors
2d440 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 75 73   might not be us
2d441 65 64 2c 20 62 75 74 20 69 66 20 74 68 65 79 20  ed, but if they 
2d442 61 72 65 20 75 73 65 64 20 74 68 65 79 0a 20 20  are used they.  
2d443 2a 2a 20 6e 65 65 64 20 74 6f 20 6f 63 63 75 72  ** need to occur
2d444 20 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65   right after the
2d445 20 64 61 74 61 62 61 73 65 20 63 75 72 73 6f 72   database cursor
2d446 2e 20 20 53 6f 20 67 6f 20 61 68 65 61 64 20 61  .  So go ahead a
2d447 6e 64 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65  nd.  ** allocate
2d448 20 65 6e 6f 75 67 68 20 73 70 61 63 65 2c 20 6a   enough space, j
2d449 75 73 74 20 69 6e 20 63 61 73 65 2e 0a 20 20 2a  ust in case..  *
2d44a 2f 0a 20 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  /.  pTabList->a[
2d44b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 69 43 75  0].iCursor = iCu
2d44c 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
2d44d 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70  ++;.  for(pIdx=p
2d44e 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
2d44f 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
2d450 65 78 74 29 7b 0a 20 20 20 20 70 50 61 72 73 65  ext){.    pParse
2d451 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20  ->nTab++;.  }.. 
2d452 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
2d453 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20  he name-context 
2d454 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43  */.  memset(&sNC
2d455 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29  , 0, sizeof(sNC)
2d456 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20  );.  sNC.pParse 
2d457 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 4e 43 2e  = pParse;.  sNC.
2d458 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c  pSrcList = pTabL
2d459 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c  ist;..  /* Resol
2d45a 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  ve the column na
2d45b 6d 65 73 20 69 6e 20 61 6c 6c 20 74 68 65 20 65  mes in all the e
2d45c 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68  xpressions of th
2d45d 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 55 50  e.  ** of the UP
2d45e 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  DATE statement. 
2d45f 20 41 6c 73 6f 20 66 69 6e 64 20 74 68 65 20 63   Also find the c
2d460 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a 20 20 2a 2a  olumn index.  **
2d461 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   for each column
2d462 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 69   to be updated i
2d463 6e 20 74 68 65 20 70 43 68 61 6e 67 65 73 20 61  n the pChanges a
2d464 72 72 61 79 2e 20 20 46 6f 72 20 65 61 63 68 0a  rray.  For each.
2d465 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20 62    ** column to b
2d466 65 20 75 70 64 61 74 65 64 2c 20 6d 61 6b 65 20  e updated, make 
2d467 73 75 72 65 20 77 65 20 68 61 76 65 20 61 75 74  sure we have aut
2d468 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 63 68  horization to ch
2d469 61 6e 67 65 0a 20 20 2a 2a 20 74 68 61 74 20 63  ange.  ** that c
2d46a 6f 6c 75 6d 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68  olumn..  */.  ch
2d46b 6e 67 52 6f 77 69 64 20 3d 20 30 3b 0a 20 20 66  ngRowid = 0;.  f
2d46c 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 61 6e 67  or(i=0; i<pChang
2d46d 65 73 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  es->nExpr; i++){
2d46e 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2d46f 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
2d470 28 26 73 4e 43 2c 20 70 43 68 61 6e 67 65 73 2d  (&sNC, pChanges-
2d471 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a  >a[i].pExpr) ){.
2d472 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74        goto updat
2d473 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d  e_cleanup;.    }
2d474 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
2d475 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  pTab->nCol; j++)
2d476 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
2d477 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d  te3StrICmp(pTab-
2d478 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20  >aCol[j].zName, 
2d479 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a  pChanges->a[i].z
2d47a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Name)==0 ){.    
2d47b 20 20 20 20 69 66 28 20 6a 3d 3d 70 54 61 62 2d      if( j==pTab-
2d47c 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  >iPKey ){.      
2d47d 20 20 20 20 63 68 6e 67 52 6f 77 69 64 20 3d 20      chngRowid = 
2d47e 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 52 6f  1;.          pRo
2d47f 77 69 64 45 78 70 72 20 3d 20 70 43 68 61 6e 67  widExpr = pChang
2d480 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  es->a[i].pExpr;.
2d481 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2d482 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20 69 3b 0a    aXRef[j] = i;.
2d483 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d484 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2d485 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43   if( j>=pTab->nC
2d486 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ol ){.      if( 
2d487 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 70  sqlite3IsRowid(p
2d488 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e  Changes->a[i].zN
2d489 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ame) ){.        
2d48a 63 68 6e 67 52 6f 77 69 64 20 3d 20 31 3b 0a 20  chngRowid = 1;. 
2d48b 20 20 20 20 20 20 20 70 52 6f 77 69 64 45 78 70         pRowidExp
2d48c 72 20 3d 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b  r = pChanges->a[
2d48d 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
2d48e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
2d48f 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2d490 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
2d491 63 6f 6c 75 6d 6e 3a 20 25 73 22 2c 20 70 43 68  column: %s", pCh
2d492 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  anges->a[i].zNam
2d493 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  e);.        goto
2d494 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2d495 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
2d496 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2d497 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
2d498 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74  .    {.      int
2d499 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20   rc;.      rc = 
2d49a 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2d49b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2d49c 55 50 44 41 54 45 2c 20 70 54 61 62 2d 3e 7a 4e  UPDATE, pTab->zN
2d49d 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ame,.           
2d49e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d49f 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e  pTab->aCol[j].zN
2d4a0 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  ame, db->aDb[iDb
2d4a1 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ].zName);.      
2d4a2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
2d4a3 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 20 67  ENY ){.        g
2d4a4 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2d4a5 75 70 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  up;.      }else 
2d4a6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49  if( rc==SQLITE_I
2d4a7 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 20 20 20  GNORE ){.       
2d4a8 20 61 58 52 65 66 5b 6a 5d 20 3d 20 2d 31 3b 0a   aXRef[j] = -1;.
2d4a9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
2d4aa 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 68 61 73 46  ndif.  }..  hasF
2d4ab 4b 20 3d 20 73 71 6c 69 74 65 33 46 6b 52 65 71  K = sqlite3FkReq
2d4ac 75 69 72 65 64 28 70 50 61 72 73 65 2c 20 70 54  uired(pParse, pT
2d4ad 61 62 2c 20 61 58 52 65 66 2c 20 63 68 6e 67 52  ab, aXRef, chngR
2d4ae 6f 77 69 64 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c  owid);..  /* All
2d4af 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72  ocate memory for
2d4b0 20 74 68 65 20 61 72 72 61 79 20 61 52 65 67 49   the array aRegI
2d4b1 64 78 5b 5d 2e 20 20 54 68 65 72 65 20 69 73 20  dx[].  There is 
2d4b2 6f 6e 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65  one entry in the
2d4b3 0a 20 20 2a 2a 20 61 72 72 61 79 20 66 6f 72 20  .  ** array for 
2d4b4 65 61 63 68 20 69 6e 64 65 78 20 61 73 73 6f 63  each index assoc
2d4b5 69 61 74 65 64 20 77 69 74 68 20 74 61 62 6c 65  iated with table
2d4b6 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20   being updated. 
2d4b7 20 46 69 6c 6c 20 69 6e 0a 20 20 2a 2a 20 74 68   Fill in.  ** th
2d4b8 65 20 76 61 6c 75 65 20 77 69 74 68 20 61 20 72  e value with a r
2d4b9 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 66  egister number f
2d4ba 6f 72 20 69 6e 64 69 63 65 73 20 74 68 61 74 20  or indices that 
2d4bb 61 72 65 20 74 6f 20 62 65 20 75 73 65 64 0a 20  are to be used. 
2d4bc 20 2a 2a 20 61 6e 64 20 77 69 74 68 20 7a 65 72   ** and with zer
2d4bd 6f 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64  o for unused ind
2d4be 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ices..  */.  for
2d4bf 28 6e 49 64 78 3d 30 2c 20 70 49 64 78 3d 70 54  (nIdx=0, pIdx=pT
2d4c0 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
2d4c1 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
2d4c2 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a 20 20  xt, nIdx++){}.  
2d4c3 69 66 28 20 6e 49 64 78 3e 30 20 29 7b 0a 20 20  if( nIdx>0 ){.  
2d4c4 20 20 61 52 65 67 49 64 78 20 3d 20 73 71 6c 69    aRegIdx = sqli
2d4c5 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
2d4c6 62 2c 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 2a  b, sizeof(Index*
2d4c7 29 20 2a 20 6e 49 64 78 20 29 3b 0a 20 20 20 20  ) * nIdx );.    
2d4c8 69 66 28 20 61 52 65 67 49 64 78 3d 3d 30 20 29  if( aRegIdx==0 )
2d4c9 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
2d4ca 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  anup;.  }.  for(
2d4cb 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  j=0, pIdx=pTab->
2d4cc 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
2d4cd 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
2d4ce 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  j++){.    int re
2d4cf 67 3b 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52  g;.    if( chngR
2d4d0 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 72 65  owid ){.      re
2d4d1 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  g = ++pParse->nM
2d4d2 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  em;.    }else{. 
2d4d3 20 20 20 20 20 72 65 67 20 3d 20 30 3b 0a 20 20       reg = 0;.  
2d4d4 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2d4d5 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Idx->nColumn; i+
2d4d6 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
2d4d7 61 58 52 65 66 5b 70 49 64 78 2d 3e 61 69 43 6f  aXRef[pIdx->aiCo
2d4d8 6c 75 6d 6e 5b 69 5d 5d 3e 3d 30 20 29 7b 0a 20  lumn[i]]>=0 ){. 
2d4d9 20 20 20 20 20 20 20 20 20 72 65 67 20 3d 20 2b           reg = +
2d4da 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
2d4db 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2d4dc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2d4dd 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 52 65 67  }.    }.    aReg
2d4de 49 64 78 5b 6a 5d 20 3d 20 72 65 67 3b 0a 20 20  Idx[j] = reg;.  
2d4df 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65  }..  /* Begin ge
2d4e0 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 20 2a  nerating code. *
2d4e1 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
2d4e2 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2d4e3 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74    if( v==0 ) got
2d4e4 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
2d4e5 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
2d4e6 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69  nested==0 ) sqli
2d4e7 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e  te3VdbeCountChan
2d4e8 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65  ges(v);.  sqlite
2d4e9 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
2d4ea 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20  tion(pParse, 1, 
2d4eb 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  iDb);..#ifndef S
2d4ec 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2d4ed 41 4c 54 41 42 4c 45 0a 20 20 2f 2a 20 56 69 72  ALTABLE.  /* Vir
2d4ee 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 75 73 74  tual tables must
2d4ef 20 62 65 20 68 61 6e 64 6c 65 64 20 73 65 70 61   be handled sepa
2d4f0 72 61 74 65 6c 79 20 2a 2f 0a 20 20 69 66 28 20  rately */.  if( 
2d4f1 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
2d4f2 29 7b 0a 20 20 20 20 75 70 64 61 74 65 56 69 72  ){.    updateVir
2d4f3 74 75 61 6c 54 61 62 6c 65 28 70 50 61 72 73 65  tualTable(pParse
2d4f4 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 54 61 62  , pTabList, pTab
2d4f5 2c 20 70 43 68 61 6e 67 65 73 2c 20 70 52 6f 77  , pChanges, pRow
2d4f6 69 64 45 78 70 72 2c 20 61 58 52 65 66 2c 0a 20  idExpr, aXRef,. 
2d4f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d4f8 20 20 20 20 20 20 70 57 68 65 72 65 29 3b 0a 20        pWhere);. 
2d4f9 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20     pWhere = 0;. 
2d4fa 20 20 20 70 54 61 62 4c 69 73 74 20 3d 20 30 3b     pTabList = 0;
2d4fb 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65  .    goto update
2d4fc 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 23 65  _cleanup;.  }.#e
2d4fd 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  ndif..  /* Alloc
2d4fe 61 74 65 20 72 65 71 75 69 72 65 64 20 72 65 67  ate required reg
2d4ff 69 73 74 65 72 73 2e 20 2a 2f 0a 20 20 72 65 67  isters. */.  reg
2d500 4f 6c 64 52 6f 77 69 64 20 3d 20 72 65 67 4e 65  OldRowid = regNe
2d501 77 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73  wRowid = ++pPars
2d502 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 69 66 28 20 70  e->nMem;.  if( p
2d503 54 72 69 67 67 65 72 20 7c 7c 20 68 61 73 46 4b  Trigger || hasFK
2d504 20 29 7b 0a 20 20 20 20 72 65 67 4f 6c 64 20 3d   ){.    regOld =
2d505 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20   pParse->nMem + 
2d506 31 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  1;.    pParse->n
2d507 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f  Mem += pTab->nCo
2d508 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 68 6e  l;.  }.  if( chn
2d509 67 52 6f 77 69 64 20 7c 7c 20 70 54 72 69 67 67  gRowid || pTrigg
2d50a 65 72 20 7c 7c 20 68 61 73 46 4b 20 29 7b 0a 20  er || hasFK ){. 
2d50b 20 20 20 72 65 67 4e 65 77 52 6f 77 69 64 20 3d     regNewRowid =
2d50c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2d50d 0a 20 20 7d 0a 20 20 72 65 67 4e 65 77 20 3d 20  .  }.  regNew = 
2d50e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31  pParse->nMem + 1
2d50f 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ;.  pParse->nMem
2d510 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a   += pTab->nCol;.
2d511 20 20 72 65 67 52 65 63 20 3d 20 2b 2b 70 50 61    regRec = ++pPa
2d512 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 20 2f 2a  rse->nMem;..  /*
2d513 20 53 74 61 72 74 20 74 68 65 20 76 69 65 77 20   Start the view 
2d514 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 69 66  context. */.  if
2d515 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20  ( isView ){.    
2d516 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
2d517 78 74 50 75 73 68 28 70 50 61 72 73 65 2c 20 26  xtPush(pParse, &
2d518 73 43 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e  sContext, pTab->
2d519 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  zName);.  }..  /
2d51a 2a 20 49 66 20 77 65 20 61 72 65 20 74 72 79 69  * If we are tryi
2d51b 6e 67 20 74 6f 20 75 70 64 61 74 65 20 61 20 76  ng to update a v
2d51c 69 65 77 2c 20 72 65 61 6c 69 7a 65 20 74 68 61  iew, realize tha
2d51d 74 20 76 69 65 77 20 69 6e 74 6f 0a 20 20 2a 2a  t view into.  **
2d51e 20 61 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62   a ephemeral tab
2d51f 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65  le..  */.#if !de
2d520 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2d521 54 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 69  T_VIEW) && !defi
2d522 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2d523 54 52 49 47 47 45 52 29 0a 20 20 69 66 28 20 69  TRIGGER).  if( i
2d524 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c  sView ){.    sql
2d525 69 74 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56  ite3MaterializeV
2d526 69 65 77 28 70 50 61 72 73 65 2c 20 70 54 61 62  iew(pParse, pTab
2d527 2c 20 70 57 68 65 72 65 2c 20 69 43 75 72 29 3b  , pWhere, iCur);
2d528 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
2d529 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f  * Resolve the co
2d52a 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c  lumn names in al
2d52b 6c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  l the expression
2d52c 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 57 48  s in the.  ** WH
2d52d 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f  ERE clause..  */
2d52e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65  .  if( sqlite3Re
2d52f 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26  solveExprNames(&
2d530 73 4e 43 2c 20 70 57 68 65 72 65 29 20 29 7b 0a  sNC, pWhere) ){.
2d531 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
2d532 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
2d533 2f 2a 20 42 65 67 69 6e 20 74 68 65 20 64 61 74  /* Begin the dat
2d534 61 62 61 73 65 20 73 63 61 6e 0a 20 20 2a 2f 0a  abase scan.  */.
2d535 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d536 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
2d537 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b  0, regOldRowid);
2d538 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69  .  pWInfo = sqli
2d539 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50  te3WhereBegin(pP
2d53a 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
2d53b 70 57 68 65 72 65 2c 30 2c 20 57 48 45 52 45 5f  pWhere,0, WHERE_
2d53c 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 29  ONEPASS_DESIRED)
2d53d 3b 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d  ;.  if( pWInfo==
2d53e 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
2d53f 63 6c 65 61 6e 75 70 3b 0a 20 20 6f 6b 4f 6e 65  cleanup;.  okOne
2d540 50 61 73 73 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f  Pass = pWInfo->o
2d541 6b 4f 6e 65 50 61 73 73 3b 0a 0a 20 20 2f 2a 20  kOnePass;..  /* 
2d542 52 65 6d 65 6d 62 65 72 20 74 68 65 20 72 6f 77  Remember the row
2d543 69 64 20 6f 66 20 65 76 65 72 79 20 69 74 65 6d  id of every item
2d544 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a   to be updated..
2d545 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
2d546 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2d547 6f 77 69 64 2c 20 69 43 75 72 2c 20 72 65 67 4f  owid, iCur, regO
2d548 6c 64 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20  ldRowid);.  if( 
2d549 21 6f 6b 4f 6e 65 50 61 73 73 20 29 7b 0a 20 20  !okOnePass ){.  
2d54a 20 20 72 65 67 52 6f 77 53 65 74 20 3d 20 2b 2b    regRowSet = ++
2d54b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2d54c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d54d 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74  Op2(v, OP_RowSet
2d54e 41 64 64 2c 20 72 65 67 52 6f 77 53 65 74 2c 20  Add, regRowSet, 
2d54f 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20  regOldRowid);.  
2d550 7d 0a 0a 20 20 2f 2a 20 45 6e 64 20 74 68 65 20  }..  /* End the 
2d551 64 61 74 61 62 61 73 65 20 73 63 61 6e 20 6c 6f  database scan lo
2d552 6f 70 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  op..  */.  sqlit
2d553 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66  e3WhereEnd(pWInf
2d554 6f 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61  o);..  /* Initia
2d555 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f  lize the count o
2d556 66 20 75 70 64 61 74 65 64 20 72 6f 77 73 0a 20  f updated rows. 
2d557 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66   */.  if( (db->f
2d558 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f  lags & SQLITE_Co
2d559 75 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50 61  untRows) && !pPa
2d55a 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
2d55b 20 29 7b 0a 20 20 20 20 72 65 67 52 6f 77 43 6f   ){.    regRowCo
2d55c 75 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  unt = ++pParse->
2d55d 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
2d55e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2d55f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65  P_Integer, 0, re
2d560 67 52 6f 77 43 6f 75 6e 74 29 3b 0a 20 20 7d 0a  gRowCount);.  }.
2d561 0a 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29  .  if( !isView )
2d562 7b 0a 20 20 20 20 2f 2a 20 0a 20 20 20 20 2a 2a  {.    /* .    **
2d563 20 4f 70 65 6e 20 65 76 65 72 79 20 69 6e 64 65   Open every inde
2d564 78 20 74 68 61 74 20 6e 65 65 64 73 20 75 70 64  x that needs upd
2d565 61 74 69 6e 67 2e 20 20 4e 6f 74 65 20 74 68 61  ating.  Note tha
2d566 74 20 69 66 20 61 6e 79 0a 20 20 20 20 2a 2a 20  t if any.    ** 
2d567 69 6e 64 65 78 20 63 6f 75 6c 64 20 70 6f 74 65  index could pote
2d568 6e 74 69 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 61  ntially invoke a
2d569 20 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63   REPLACE conflic
2d56a 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 20 20  t resolution .  
2d56b 20 20 2a 2a 20 61 63 74 69 6f 6e 2c 20 74 68 65    ** action, the
2d56c 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 6f 70 65  n we need to ope
2d56d 6e 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 62 65  n all indices be
2d56e 63 61 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e  cause we might n
2d56f 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65  eed.    ** to be
2d570 20 64 65 6c 65 74 69 6e 67 20 73 6f 6d 65 20 72   deleting some r
2d571 65 63 6f 72 64 73 2e 0a 20 20 20 20 2a 2f 0a 20  ecords..    */. 
2d572 20 20 20 69 66 28 20 21 6f 6b 4f 6e 65 50 61 73     if( !okOnePas
2d573 73 20 29 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  s ) sqlite3OpenT
2d574 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75  able(pParse, iCu
2d575 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50  r, iDb, pTab, OP
2d576 5f 4f 70 65 6e 57 72 69 74 65 29 3b 20 0a 20 20  _OpenWrite); .  
2d577 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f    if( onError==O
2d578 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20  E_Replace ){.   
2d579 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a     openAll = 1;.
2d57a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2d57b 20 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b 0a 20 20   openAll = 0;.  
2d57c 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
2d57d 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
2d57e 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
2d57f 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t){.        if( 
2d580 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f  pIdx->onError==O
2d581 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20  E_Replace ){.   
2d582 20 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d         openAll =
2d583 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72   1;.          br
2d584 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
2d585 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2d586 20 66 6f 72 28 69 3d 30 2c 20 70 49 64 78 3d 70   for(i=0, pIdx=p
2d587 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
2d588 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
2d589 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ext, i++){.     
2d58a 20 69 66 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20   if( openAll || 
2d58b 61 52 65 67 49 64 78 5b 69 5d 3e 30 20 29 7b 0a  aRegIdx[i]>0 ){.
2d58c 20 20 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20          KeyInfo 
2d58d 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49  *pKey = sqlite3I
2d58e 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
2d58f 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  se, pIdx);.     
2d590 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2d591 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  dOp4(v, OP_OpenW
2d592 72 69 74 65 2c 20 69 43 75 72 2b 69 2b 31 2c 20  rite, iCur+i+1, 
2d593 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c  pIdx->tnum, iDb,
2d594 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2d595 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
2d596 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  Key, P4_KEYINFO_
2d597 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20  HANDOFF);.      
2d598 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
2d599 2d 3e 6e 54 61 62 3e 69 43 75 72 2b 69 2b 31 20  ->nTab>iCur+i+1 
2d59a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2d59b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 70 20 6f  .  }..  /* Top o
2d59c 66 20 74 68 65 20 75 70 64 61 74 65 20 6c 6f 6f  f the update loo
2d59d 70 20 2a 2f 0a 20 20 69 66 28 20 6f 6b 4f 6e 65  p */.  if( okOne
2d59e 50 61 73 73 20 29 7b 0a 20 20 20 20 69 6e 74 20  Pass ){.    int 
2d59f 61 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  a1 = sqlite3Vdbe
2d5a0 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
2d5a1 4e 75 6c 6c 2c 20 72 65 67 4f 6c 64 52 6f 77 69  Null, regOldRowi
2d5a2 64 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73  d);.    addr = s
2d5a3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
2d5a4 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20  (v, OP_Goto);.  
2d5a5 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2d5a6 70 48 65 72 65 28 76 2c 20 61 31 29 3b 0a 20 20  pHere(v, a1);.  
2d5a7 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 20  }else{.    addr 
2d5a8 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2d5a9 4f 70 33 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74  Op3(v, OP_RowSet
2d5aa 52 65 61 64 2c 20 72 65 67 52 6f 77 53 65 74 2c  Read, regRowSet,
2d5ab 20 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29   0, regOldRowid)
2d5ac 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
2d5ad 20 63 75 72 73 6f 72 20 69 43 75 72 20 70 6f 69   cursor iCur poi
2d5ae 6e 74 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64  nt to the record
2d5af 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 75   that is being u
2d5b0 70 64 61 74 65 64 2e 20 49 66 0a 20 20 2a 2a 20  pdated. If.  ** 
2d5b1 74 68 69 73 20 72 65 63 6f 72 64 20 64 6f 65 73  this record does
2d5b2 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 73   not exist for s
2d5b3 6f 6d 65 20 72 65 61 73 6f 6e 20 28 64 65 6c 65  ome reason (dele
2d5b4 74 65 64 20 62 79 20 61 20 74 72 69 67 67 65 72  ted by a trigger
2d5b5 2c 0a 20 20 2a 2a 20 66 6f 72 20 65 78 61 6d 70  ,.  ** for examp
2d5b6 6c 65 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  le, then jump to
2d5b7 20 74 68 65 20 6e 65 78 74 20 69 74 65 72 61 74   the next iterat
2d5b8 69 6f 6e 20 6f 66 20 74 68 65 20 52 6f 77 53 65  ion of the RowSe
2d5b9 74 20 6c 6f 6f 70 2e 20 20 2a 2f 0a 20 20 73 71  t loop.  */.  sq
2d5ba 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2d5bb 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
2d5bc 20 69 43 75 72 2c 20 61 64 64 72 2c 20 72 65 67   iCur, addr, reg
2d5bd 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a 20 20 2f 2a  OldRowid);..  /*
2d5be 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 6e   If the record n
2d5bf 75 6d 62 65 72 20 77 69 6c 6c 20 63 68 61 6e 67  umber will chang
2d5c0 65 2c 20 73 65 74 20 72 65 67 69 73 74 65 72 20  e, set register 
2d5c1 72 65 67 4e 65 77 52 6f 77 69 64 20 74 6f 0a 20  regNewRowid to. 
2d5c2 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 74 68 65 20   ** contain the 
2d5c3 6e 65 77 20 76 61 6c 75 65 2e 20 49 66 20 74 68  new value. If th
2d5c4 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
2d5c5 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 6d 6f 64  is not being mod
2d5c6 69 66 69 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e  ified,.  ** then
2d5c7 20 72 65 67 4e 65 77 52 6f 77 69 64 20 69 73 20   regNewRowid is 
2d5c8 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 74 65  the same registe
2d5c9 72 20 61 73 20 72 65 67 4f 6c 64 52 6f 77 69 64  r as regOldRowid
2d5ca 2c 20 77 68 69 63 68 20 69 73 0a 20 20 2a 2a 20  , which is.  ** 
2d5cb 61 6c 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65  already populate
2d5cc 64 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  d.  */.  assert(
2d5cd 20 63 68 6e 67 52 6f 77 69 64 20 7c 7c 20 70 54   chngRowid || pT
2d5ce 72 69 67 67 65 72 20 7c 7c 20 68 61 73 46 4b 20  rigger || hasFK 
2d5cf 7c 7c 20 72 65 67 4f 6c 64 52 6f 77 69 64 3d 3d  || regOldRowid==
2d5d0 72 65 67 4e 65 77 52 6f 77 69 64 20 29 3b 0a 20  regNewRowid );. 
2d5d1 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20 29   if( chngRowid )
2d5d2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
2d5d3 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 52  rCode(pParse, pR
2d5d4 6f 77 69 64 45 78 70 72 2c 20 72 65 67 4e 65 77  owidExpr, regNew
2d5d5 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69  Rowid);.    sqli
2d5d6 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2d5d7 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72   OP_MustBeInt, r
2d5d8 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20 7d  egNewRowid);.  }
2d5d9 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
2d5da 61 72 65 20 74 72 69 67 67 65 72 73 20 6f 6e 20  are triggers on 
2d5db 74 68 69 73 20 74 61 62 6c 65 2c 20 70 6f 70 75  this table, popu
2d5dc 6c 61 74 65 20 61 6e 20 61 72 72 61 79 20 6f 66  late an array of
2d5dd 20 72 65 67 69 73 74 65 72 73 20 0a 20 20 2a 2a   registers .  **
2d5de 20 77 69 74 68 20 74 68 65 20 72 65 71 75 69 72   with the requir
2d5df 65 64 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 20  ed old.* column 
2d5e0 64 61 74 61 2e 20 20 2a 2f 0a 20 20 69 66 28 20  data.  */.  if( 
2d5e1 68 61 73 46 4b 20 7c 7c 20 70 54 72 69 67 67 65  hasFK || pTrigge
2d5e2 72 20 29 7b 0a 20 20 20 20 75 33 32 20 6f 6c 64  r ){.    u32 old
2d5e3 6d 61 73 6b 20 3d 20 28 68 61 73 46 4b 20 3f 20  mask = (hasFK ? 
2d5e4 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b  sqlite3FkOldmask
2d5e5 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 3a  (pParse, pTab) :
2d5e6 20 30 29 3b 0a 20 20 20 20 6f 6c 64 6d 61 73 6b   0);.    oldmask
2d5e7 20 7c 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67   |= sqlite3Trigg
2d5e8 65 72 4f 6c 64 6d 61 73 6b 28 70 50 61 72 73 65  erOldmask(pParse
2d5e9 2c 20 70 54 72 69 67 67 65 72 2c 20 70 43 68 61  , pTrigger, pCha
2d5ea 6e 67 65 73 2c 20 70 54 61 62 2c 20 6f 6e 45 72  nges, pTab, onEr
2d5eb 72 6f 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  ror);.    for(i=
2d5ec 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; i<pTab->nCol;
2d5ed 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
2d5ee 20 61 58 52 65 66 5b 69 5d 3c 30 20 7c 7c 20 6f   aXRef[i]<0 || o
2d5ef 6c 64 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66 66  ldmask==0xffffff
2d5f0 66 66 20 7c 7c 20 28 6f 6c 64 6d 61 73 6b 20 26  ff || (oldmask &
2d5f1 20 28 31 3c 3c 69 29 29 20 29 7b 0a 20 20 20 20   (1<<i)) ){.    
2d5f2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2d5f3 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
2d5f4 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67  mn, iCur, i, reg
2d5f5 4f 6c 64 2b 69 29 3b 0a 20 20 20 20 20 20 20 20  Old+i);.        
2d5f6 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66  sqlite3ColumnDef
2d5f7 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 2c  ault(v, pTab, i,
2d5f8 20 72 65 67 4f 6c 64 2b 69 29 3b 0a 20 20 20 20   regOld+i);.    
2d5f9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2d5fa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d5fb 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
2d5fc 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0a 20 20 20  , regOld+i);.   
2d5fd 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
2d5fe 66 28 20 63 68 6e 67 52 6f 77 69 64 3d 3d 30 20  f( chngRowid==0 
2d5ff 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2d600 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2d601 5f 43 6f 70 79 2c 20 72 65 67 4f 6c 64 52 6f 77  _Copy, regOldRow
2d602 69 64 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29  id, regNewRowid)
2d603 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
2d604 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 61  * Populate the a
2d605 72 72 61 79 20 6f 66 20 72 65 67 69 73 74 65 72  rray of register
2d606 73 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 72  s beginning at r
2d607 65 67 4e 65 77 20 77 69 74 68 20 74 68 65 20 6e  egNew with the n
2d608 65 77 0a 20 20 2a 2a 20 72 6f 77 20 64 61 74 61  ew.  ** row data
2d609 2e 20 54 68 69 73 20 61 72 72 61 79 20 69 73 20  . This array is 
2d60a 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 63 6f  used to check co
2d60b 6e 73 74 61 69 6e 74 73 2c 20 63 72 65 61 74 65  nstaints, create
2d60c 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 74 61   the new.  ** ta
2d60d 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 72 65  ble and index re
2d60e 63 6f 72 64 73 2c 20 61 6e 64 20 61 73 20 74 68  cords, and as th
2d60f 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61 6e 79  e values for any
2d610 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65   new.* reference
2d611 73 0a 20 20 2a 2a 20 6d 61 64 65 20 62 79 20 74  s.  ** made by t
2d612 72 69 67 67 65 72 73 2e 20 20 2a 2f 0a 20 20 66  riggers.  */.  f
2d613 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
2d614 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
2d615 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( i==pTab->iPK
2d616 65 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ey ){.      sqli
2d617 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2d618 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
2d619 4e 65 77 2b 69 29 3b 0a 20 20 20 20 7d 65 6c 73  New+i);.    }els
2d61a 65 7b 0a 20 20 20 20 20 20 6a 20 3d 20 61 58 52  e{.      j = aXR
2d61b 65 66 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28  ef[i];.      if(
2d61c 20 6a 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20   j<0 ){.        
2d61d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d61e 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
2d61f 69 43 75 72 2c 20 69 2c 20 72 65 67 4e 65 77 2b  iCur, i, regNew+
2d620 69 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  i);.        sqli
2d621 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74  te3ColumnDefault
2d622 28 76 2c 20 70 54 61 62 2c 20 69 2c 20 72 65 67  (v, pTab, i, reg
2d623 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20 20 7d 65  New+i);.      }e
2d624 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
2d625 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
2d626 72 73 65 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61  rse, pChanges->a
2d627 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 4e 65  [j].pExpr, regNe
2d628 77 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  w+i);.      }.  
2d629 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69    }.  }..  /* Fi
2d62a 72 65 20 61 6e 79 20 42 45 46 4f 52 45 20 55 50  re any BEFORE UP
2d62b 44 41 54 45 20 74 72 69 67 67 65 72 73 2e 20 54  DATE triggers. T
2d62c 68 69 73 20 68 61 70 70 65 6e 73 20 62 65 66 6f  his happens befo
2d62d 72 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61  re constraints a
2d62e 72 65 0a 20 20 2a 2a 20 76 65 72 69 66 69 65 64  re.  ** verified
2d62f 2e 20 4f 6e 65 20 63 6f 75 6c 64 20 61 72 67 75  . One could argu
2d630 65 20 74 68 61 74 20 74 68 69 73 20 69 73 20 77  e that this is w
2d631 72 6f 6e 67 2e 20 20 2a 2f 0a 20 20 69 66 28 20  rong.  */.  if( 
2d632 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
2d633 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d634 32 28 76 2c 20 4f 50 5f 41 66 66 69 6e 69 74 79  2(v, OP_Affinity
2d635 2c 20 72 65 67 4e 65 77 2c 20 70 54 61 62 2d 3e  , regNew, pTab->
2d636 6e 43 6f 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  nCol);.    sqlit
2d637 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53  e3TableAffinityS
2d638 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 20 20 20  tr(v, pTab);.   
2d639 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
2d63a 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70  rigger(pParse, p
2d63b 54 72 69 67 67 65 72 2c 20 54 4b 5f 55 50 44 41  Trigger, TK_UPDA
2d63c 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 0a 20  TE, pChanges, . 
2d63d 20 20 20 20 20 20 20 54 52 49 47 47 45 52 5f 42         TRIGGER_B
2d63e 45 46 4f 52 45 2c 20 70 54 61 62 2c 20 72 65 67  EFORE, pTab, reg
2d63f 4f 6c 64 52 6f 77 69 64 2c 20 6f 6e 45 72 72 6f  OldRowid, onErro
2d640 72 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20 20 2f  r, addr);..    /
2d641 2a 20 54 68 65 20 72 6f 77 2d 74 72 69 67 67 65  * The row-trigge
2d642 72 20 6d 61 79 20 68 61 76 65 20 64 65 6c 65 74  r may have delet
2d643 65 64 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67  ed the row being
2d644 20 75 70 64 61 74 65 64 2e 20 49 6e 20 74 68 69   updated. In thi
2d645 73 0a 20 20 20 20 2a 2a 20 63 61 73 65 2c 20 6a  s.    ** case, j
2d646 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ump to the next 
2d647 72 6f 77 2e 20 4e 6f 20 75 70 64 61 74 65 73 20  row. No updates 
2d648 6f 72 20 41 46 54 45 52 20 74 72 69 67 67 65 72  or AFTER trigger
2d649 73 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 72 65  s are .    ** re
2d64a 71 75 69 72 65 64 2e 20 54 68 69 73 20 62 65 68  quired. This beh
2d64b 61 76 69 6f 75 72 20 2d 20 77 68 61 74 20 68 61  aviour - what ha
2d64c 70 70 65 6e 73 20 77 68 65 6e 20 74 68 65 20 72  ppens when the r
2d64d 6f 77 20 62 65 69 6e 67 20 75 70 64 61 74 65 64  ow being updated
2d64e 0a 20 20 20 20 2a 2a 20 69 73 20 64 65 6c 65 74  .    ** is delet
2d64f 65 64 20 6f 72 20 72 65 6e 61 6d 65 64 20 62 79  ed or renamed by
2d650 20 61 20 42 45 46 4f 52 45 20 74 72 69 67 67 65   a BEFORE trigge
2d651 72 20 2d 20 69 73 20 6c 65 66 74 20 75 6e 64 65  r - is left unde
2d652 66 69 6e 65 64 20 69 6e 20 74 68 65 0a 20 20 20  fined in the.   
2d653 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f   ** documentatio
2d654 6e 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  n.  */.    sqlit
2d655 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2d656 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43  OP_NotExists, iC
2d657 75 72 2c 20 61 64 64 72 2c 20 72 65 67 4f 6c 64  ur, addr, regOld
2d658 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 69  Rowid);.  }..  i
2d659 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 0a 20  f( !isView ){.. 
2d65a 20 20 20 2f 2a 20 44 6f 20 63 6f 6e 73 74 72 61     /* Do constra
2d65b 69 6e 74 20 63 68 65 63 6b 73 2e 20 2a 2f 0a 20  int checks. */. 
2d65c 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61     sqlite3Genera
2d65d 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
2d65e 6b 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ks(pParse, pTab,
2d65f 20 69 43 75 72 2c 20 72 65 67 4e 65 77 52 6f 77   iCur, regNewRow
2d660 69 64 2c 0a 20 20 20 20 20 20 20 20 61 52 65 67  id,.        aReg
2d661 49 64 78 2c 20 28 63 68 6e 67 52 6f 77 69 64 3f  Idx, (chngRowid?
2d662 72 65 67 4f 6c 64 52 6f 77 69 64 3a 30 29 2c 20  regOldRowid:0), 
2d663 31 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64 72  1, onError, addr
2d664 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f  , 0);..    /* Do
2d665 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 63   FK constraint c
2d666 68 65 63 6b 73 2e 20 2a 2f 0a 20 20 20 20 69 66  hecks. */.    if
2d667 28 20 68 61 73 46 4b 20 29 7b 0a 20 20 20 20 20  ( hasFK ){.     
2d668 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28   sqlite3FkCheck(
2d669 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 72 65  pParse, pTab, re
2d66a 67 4f 6c 64 52 6f 77 69 64 2c 20 30 29 3b 0a 20  gOldRowid, 0);. 
2d66b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c     }..    /* Del
2d66c 65 74 65 20 74 68 65 20 69 6e 64 65 78 20 65 6e  ete the index en
2d66d 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64  tries associated
2d66e 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e   with the curren
2d66f 74 20 72 65 63 6f 72 64 2e 20 20 2a 2f 0a 20 20  t record.  */.  
2d670 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
2d671 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
2d672 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20  otExists, iCur, 
2d673 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b  0, regOldRowid);
2d674 0a 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65  .    sqlite3Gene
2d675 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
2d676 74 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  te(pParse, pTab,
2d677 20 69 43 75 72 2c 20 61 52 65 67 49 64 78 29 3b   iCur, aRegIdx);
2d678 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 63 68  .  .    /* If ch
2d679 61 6e 67 69 6e 67 20 74 68 65 20 72 65 63 6f 72  anging the recor
2d67a 64 20 6e 75 6d 62 65 72 2c 20 64 65 6c 65 74 65  d number, delete
2d67b 20 74 68 65 20 6f 6c 64 20 72 65 63 6f 72 64 2e   the old record.
2d67c 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 61 73    */.    if( has
2d67d 46 4b 20 7c 7c 20 63 68 6e 67 52 6f 77 69 64 20  FK || chngRowid 
2d67e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2d67f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2d680 5f 44 65 6c 65 74 65 2c 20 69 43 75 72 2c 20 30  _Delete, iCur, 0
2d681 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2d682 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2d683 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20 20 69 66  (v, j1);..    if
2d684 28 20 68 61 73 46 4b 20 29 7b 0a 20 20 20 20 20  ( hasFK ){.     
2d685 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28   sqlite3FkCheck(
2d686 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c  pParse, pTab, 0,
2d687 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a 20   regNewRowid);. 
2d688 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
2d689 6e 73 65 72 74 20 74 68 65 20 6e 65 77 20 69 6e  nsert the new in
2d68a 64 65 78 20 65 6e 74 72 69 65 73 20 61 6e 64 20  dex entries and 
2d68b 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20  the new record. 
2d68c 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  */.    sqlite3Co
2d68d 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28  mpleteInsertion(
2d68e 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43  pParse, pTab, iC
2d68f 75 72 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 2c  ur, regNewRowid,
2d690 20 61 52 65 67 49 64 78 2c 20 31 2c 20 30 2c 20   aRegIdx, 1, 0, 
2d691 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 61  0);..    /* Do a
2d692 6e 79 20 4f 4e 20 43 41 53 43 41 44 45 2c 20 53  ny ON CASCADE, S
2d693 45 54 20 4e 55 4c 4c 20 6f 72 20 53 45 54 20 44  ET NULL or SET D
2d694 45 46 41 55 4c 54 20 6f 70 65 72 61 74 69 6f 6e  EFAULT operation
2d695 73 20 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20  s required to.  
2d696 20 20 2a 2a 20 68 61 6e 64 6c 65 20 72 6f 77 73    ** handle rows
2d697 20 28 70 6f 73 73 69 62 6c 79 20 69 6e 20 6f 74   (possibly in ot
2d698 68 65 72 20 74 61 62 6c 65 73 29 20 74 68 61 74  her tables) that
2d699 20 72 65 66 65 72 20 76 69 61 20 61 20 66 6f 72   refer via a for
2d69a 65 69 67 6e 20 6b 65 79 0a 20 20 20 20 2a 2a 20  eign key.    ** 
2d69b 74 6f 20 74 68 65 20 72 6f 77 20 6a 75 73 74 20  to the row just 
2d69c 75 70 64 61 74 65 64 2e 20 2a 2f 20 0a 20 20 20  updated. */ .   
2d69d 20 69 66 28 20 68 61 73 46 4b 20 29 7b 0a 20 20   if( hasFK ){.  
2d69e 20 20 20 20 73 71 6c 69 74 65 33 46 6b 41 63 74      sqlite3FkAct
2d69f 69 6f 6e 73 28 70 50 61 72 73 65 2c 20 70 54 61  ions(pParse, pTa
2d6a0 62 2c 20 70 43 68 61 6e 67 65 73 2c 20 72 65 67  b, pChanges, reg
2d6a1 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d  OldRowid);.    }
2d6a2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65  .  }..  /* Incre
2d6a3 6d 65 6e 74 20 74 68 65 20 72 6f 77 20 63 6f 75  ment the row cou
2d6a4 6e 74 65 72 20 0a 20 20 2a 2f 0a 20 20 69 66 28  nter .  */.  if(
2d6a5 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
2d6a6 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 20  LITE_CountRows) 
2d6a7 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54 72 69  && !pParse->pTri
2d6a8 67 67 65 72 54 61 62 29 7b 0a 20 20 20 20 73 71  ggerTab){.    sq
2d6a9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2d6aa 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65  v, OP_AddImm, re
2d6ab 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20  gRowCount, 1);. 
2d6ac 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 43 6f 64   }..  sqlite3Cod
2d6ad 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72  eRowTrigger(pPar
2d6ae 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b  se, pTrigger, TK
2d6af 5f 55 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65  _UPDATE, pChange
2d6b0 73 2c 20 0a 20 20 20 20 20 20 54 52 49 47 47 45  s, .      TRIGGE
2d6b1 52 5f 41 46 54 45 52 2c 20 70 54 61 62 2c 20 72  R_AFTER, pTab, r
2d6b2 65 67 4f 6c 64 52 6f 77 69 64 2c 20 6f 6e 45 72  egOldRowid, onEr
2d6b3 72 6f 72 2c 20 61 64 64 72 29 3b 0a 0a 20 20 2f  ror, addr);..  /
2d6b4 2a 20 52 65 70 65 61 74 20 74 68 65 20 61 62 6f  * Repeat the abo
2d6b5 76 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 74  ve with the next
2d6b6 20 72 65 63 6f 72 64 20 74 6f 20 62 65 20 75 70   record to be up
2d6b7 64 61 74 65 64 2c 20 75 6e 74 69 6c 0a 20 20 2a  dated, until.  *
2d6b8 2a 20 61 6c 6c 20 72 65 63 6f 72 64 20 73 65 6c  * all record sel
2d6b9 65 63 74 65 64 20 62 79 20 74 68 65 20 57 48 45  ected by the WHE
2d6ba 52 45 20 63 6c 61 75 73 65 20 68 61 76 65 20 62  RE clause have b
2d6bb 65 65 6e 20 75 70 64 61 74 65 64 2e 0a 20 20 2a  een updated..  *
2d6bc 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  /.  sqlite3VdbeA
2d6bd 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
2d6be 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20 20 73 71  , 0, addr);.  sq
2d6bf 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2d6c0 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 2f  e(v, addr);..  /
2d6c1 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c  * Close all tabl
2d6c2 65 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c  es */.  for(i=0,
2d6c3 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
2d6c4 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
2d6c5 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29  Idx->pNext, i++)
2d6c6 7b 0a 20 20 20 20 69 66 28 20 6f 70 65 6e 41 6c  {.    if( openAl
2d6c7 6c 20 7c 7c 20 61 52 65 67 49 64 78 5b 69 5d 3e  l || aRegIdx[i]>
2d6c8 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
2d6c9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2d6ca 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2b 69  OP_Close, iCur+i
2d6cb 2b 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  +1, 0);.    }.  
2d6cc 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
2d6cd 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
2d6ce 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a 0a 20 20  e, iCur, 0);..  
2d6cf 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 71  /* Update the sq
2d6d0 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
2d6d1 62 6c 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74  ble by storing t
2d6d2 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
2d6d3 65 0a 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 72  e.  ** maximum r
2d6d4 6f 77 69 64 20 63 6f 75 6e 74 65 72 20 76 61 6c  owid counter val
2d6d5 75 65 73 20 72 65 63 6f 72 64 65 64 20 77 68 69  ues recorded whi
2d6d6 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74  le inserting int
2d6d7 6f 0a 20 20 2a 2a 20 61 75 74 6f 69 6e 63 72 65  o.  ** autoincre
2d6d8 6d 65 6e 74 20 74 61 62 6c 65 73 2e 0a 20 20 2a  ment tables..  *
2d6d9 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  /.  if( pParse->
2d6da 6e 65 73 74 65 64 3d 3d 30 20 26 26 20 70 50 61  nested==0 && pPa
2d6db 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
2d6dc 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
2d6dd 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45  e3AutoincrementE
2d6de 6e 64 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a  nd(pParse);.  }.
2d6df 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72  .  /*.  ** Retur
2d6e0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
2d6e1 72 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 63  rows that were c
2d6e2 68 61 6e 67 65 64 2e 20 49 66 20 74 68 69 73 20  hanged. If this 
2d6e3 72 6f 75 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a  routine is .  **
2d6e4 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65   generating code
2d6e5 20 62 65 63 61 75 73 65 20 6f 66 20 61 20 63 61   because of a ca
2d6e6 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e 65 73  ll to sqlite3Nes
2d6e7 74 65 64 50 61 72 73 65 28 29 2c 20 64 6f 20 6e  tedParse(), do n
2d6e8 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74  ot.  ** invoke t
2d6e9 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
2d6ea 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tion..  */.  if(
2d6eb 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49   (db->flags&SQLI
2d6ec 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 20 26 26  TE_CountRows) &&
2d6ed 20 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67   !pParse->pTrigg
2d6ee 65 72 54 61 62 20 26 26 20 21 70 50 61 72 73 65  erTab && !pParse
2d6ef 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20  ->nested ){.    
2d6f0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d6f1 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
2d6f2 77 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20  w, regRowCount, 
2d6f3 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
2d6f4 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
2d6f5 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
2d6f6 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
2d6f7 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
2d6f8 45 2c 20 22 72 6f 77 73 20 75 70 64 61 74 65 64  E, "rows updated
2d6f9 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
2d6fa 29 3b 0a 20 20 7d 0a 0a 75 70 64 61 74 65 5f 63  );.  }..update_c
2d6fb 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65  leanup:.  sqlite
2d6fc 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28  3AuthContextPop(
2d6fd 26 73 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71  &sContext);.  sq
2d6fe 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2d6ff 61 52 65 67 49 64 78 29 3b 0a 20 20 73 71 6c 69  aRegIdx);.  sqli
2d700 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 58  te3DbFree(db, aX
2d701 52 65 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  Ref);.  sqlite3S
2d702 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
2d703 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71   pTabList);.  sq
2d704 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
2d705 65 74 65 28 64 62 2c 20 70 43 68 61 6e 67 65 73  ete(db, pChanges
2d706 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2d707 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 72  Delete(db, pWher
2d708 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  e);.  return;.}.
2d709 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73  /* Make sure "is
2d70a 56 69 65 77 22 20 61 6e 64 20 6f 74 68 65 72 20  View" and other 
2d70b 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20 61  macros defined a
2d70c 62 6f 76 65 20 61 72 65 20 75 6e 64 65 66 69 6e  bove are undefin
2d70d 65 64 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a  ed. Otherwise.**
2d70e 20 74 68 65 6c 79 20 6d 61 79 20 69 6e 74 65 72   thely may inter
2d70f 66 65 72 65 20 77 69 74 68 20 63 6f 6d 70 69 6c  fere with compil
2d710 61 74 69 6f 6e 20 6f 66 20 6f 74 68 65 72 20 66  ation of other f
2d711 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 69 73  unctions in this
2d712 20 66 69 6c 65 0a 2a 2a 20 28 6f 72 20 69 6e 20   file.** (or in 
2d713 61 6e 6f 74 68 65 72 20 66 69 6c 65 2c 20 69 66  another file, if
2d714 20 74 68 69 73 20 66 69 6c 65 20 62 65 63 6f 6d   this file becom
2d715 65 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 61  es part of the a
2d716 6d 61 6c 67 61 6d 61 74 69 6f 6e 29 2e 20 20 2a  malgamation).  *
2d717 2f 0a 23 69 66 64 65 66 20 69 73 56 69 65 77 0a  /.#ifdef isView.
2d718 20 23 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23   #undef isView.#
2d719 65 6e 64 69 66 0a 23 69 66 64 65 66 20 70 54 72  endif.#ifdef pTr
2d71a 69 67 67 65 72 0a 20 23 75 6e 64 65 66 20 70 54  igger. #undef pT
2d71b 72 69 67 67 65 72 0a 23 65 6e 64 69 66 0a 0a 23  rigger.#endif..#
2d71c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2d71d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2d71e 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
2d71f 6f 64 65 20 66 6f 72 20 61 6e 20 55 50 44 41 54  ode for an UPDAT
2d720 45 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74  E of a virtual t
2d721 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  able..**.** The 
2d722 73 74 72 61 74 65 67 79 20 69 73 20 74 68 61 74  strategy is that
2d723 20 77 65 20 63 72 65 61 74 65 20 61 6e 20 65 70   we create an ep
2d724 68 65 6d 65 72 69 61 6c 20 74 61 62 6c 65 20 74  hemerial table t
2d725 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  hat contains.** 
2d726 66 6f 72 20 65 61 63 68 20 72 6f 77 20 74 6f 20  for each row to 
2d727 62 65 20 63 68 61 6e 67 65 64 3a 0a 2a 2a 0a 2a  be changed:.**.*
2d728 2a 20 20 20 28 41 29 20 20 54 68 65 20 6f 72 69  *   (A)  The ori
2d729 67 69 6e 61 6c 20 72 6f 77 69 64 20 6f 66 20 74  ginal rowid of t
2d72a 68 61 74 20 72 6f 77 2e 0a 2a 2a 20 20 20 28 42  hat row..**   (B
2d72b 29 20 20 54 68 65 20 72 65 76 69 73 65 64 20 72  )  The revised r
2d72c 6f 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77  owid for the row
2d72d 2e 20 28 6e 6f 74 65 31 29 0a 2a 2a 20 20 20 28  . (note1).**   (
2d72e 43 29 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  C)  The content 
2d72f 6f 66 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20  of every column 
2d730 69 6e 20 74 68 65 20 72 6f 77 2e 0a 2a 2a 0a 2a  in the row..**.*
2d731 2a 20 54 68 65 6e 20 77 65 20 6c 6f 6f 70 20 6f  * Then we loop o
2d732 76 65 72 20 74 68 69 73 20 65 70 68 65 6d 65 72  ver this ephemer
2d733 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72  al table and for
2d734 20 65 61 63 68 20 72 6f 77 20 69 6e 0a 2a 2a 20   each row in.** 
2d735 74 68 65 20 65 70 68 65 72 6d 65 72 61 6c 20 74  the ephermeral t
2d736 61 62 6c 65 20 63 61 6c 6c 20 56 55 70 64 61 74  able call VUpdat
2d737 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 66 69  e..**.** When fi
2d738 6e 69 73 68 65 64 2c 20 64 72 6f 70 20 74 68 65  nished, drop the
2d739 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
2d73a 2e 0a 2a 2a 0a 2a 2a 20 28 6e 6f 74 65 31 29 20  ..**.** (note1) 
2d73b 41 63 74 75 61 6c 6c 79 2c 20 69 66 20 77 65 20  Actually, if we 
2d73c 6b 6e 6f 77 20 69 6e 20 61 64 76 61 6e 63 65 20  know in advance 
2d73d 74 68 61 74 20 28 41 29 20 69 73 20 61 6c 77 61  that (A) is alwa
2d73e 79 73 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 61  ys the same.** a
2d73f 73 20 28 42 29 20 77 65 20 6f 6e 6c 79 20 73 74  s (B) we only st
2d740 6f 72 65 20 28 41 29 2c 20 74 68 65 6e 20 64 75  ore (A), then du
2d741 70 6c 69 63 61 74 65 20 28 41 29 20 77 68 65 6e  plicate (A) when
2d742 20 70 75 6c 6c 69 6e 67 0a 2a 2a 20 69 74 20 6f   pulling.** it o
2d743 75 74 20 6f 66 20 74 68 65 20 65 70 68 65 6d 65  ut of the epheme
2d744 72 61 6c 20 74 61 62 6c 65 20 62 65 66 6f 72 65  ral table before
2d745 20 63 61 6c 6c 69 6e 67 20 56 55 70 64 61 74 65   calling VUpdate
2d746 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2d747 20 75 70 64 61 74 65 56 69 72 74 75 61 6c 54 61   updateVirtualTa
2d748 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ble(.  Parse *pP
2d749 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  arse,       /* T
2d74a 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
2d74b 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
2d74c 2a 70 53 72 63 2c 20 20 20 20 20 20 20 2f 2a 20  *pSrc,       /* 
2d74d 54 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  The virtual tabl
2d74e 65 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65 64  e to be modified
2d74f 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2d750 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b,         /* Th
2d751 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2d752 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2d753 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 54 68 65  Changes,  /* The
2d754 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 61 6e   columns to chan
2d755 67 65 20 69 6e 20 74 68 65 20 55 50 44 41 54 45  ge in the UPDATE
2d756 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2d757 45 78 70 72 20 2a 70 52 6f 77 69 64 2c 20 20 20  Expr *pRowid,   
2d758 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
2d759 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6f 6d  on used to recom
2d75a 70 75 74 65 20 74 68 65 20 72 6f 77 69 64 20 2a  pute the rowid *
2d75b 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 2c 20  /.  int *aXRef, 
2d75c 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70           /* Mapp
2d75d 69 6e 67 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73  ing from columns
2d75e 20 6f 66 20 70 54 61 62 20 74 6f 20 65 6e 74 72   of pTab to entr
2d75f 69 65 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20  ies in pChanges 
2d760 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
2d761 65 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45  e         /* WHE
2d762 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  RE clause of the
2d763 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
2d764 74 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  t */.){.  Vdbe *
2d765 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2d766 65 3b 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 6d  e;  /* Virtual m
2d767 61 63 68 69 6e 65 20 75 6e 64 65 72 20 63 6f 6e  achine under con
2d768 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 45  struction */.  E
2d769 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
2d76a 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  = 0;     /* The 
2d76b 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
2d76c 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2d76d 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  nt */.  Select *
2d76e 70 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20 20  pSelect = 0;    
2d76f 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
2d770 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45  statement */.  E
2d771 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20  xpr *pExpr;     
2d772 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
2d773 6f 72 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e  orary expression
2d774 20 2a 2f 0a 20 20 69 6e 74 20 65 70 68 65 6d 54   */.  int ephemT
2d775 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ab;             
2d776 2f 2a 20 54 61 62 6c 65 20 68 6f 6c 64 69 6e 67  /* Table holding
2d777 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
2d778 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69  he SELECT */.  i
2d779 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
2d77a 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2d77b 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
2d77c 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 20  t addr;         
2d77d 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2d77e 73 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f  ss of top of loo
2d77f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 3b  p */.  int iReg;
2d780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d781 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74   /* First regist
2d782 65 72 20 69 6e 20 73 65 74 20 70 61 73 73 65 64  er in set passed
2d783 20 74 6f 20 4f 50 5f 56 55 70 64 61 74 65 20 2a   to OP_VUpdate *
2d784 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2d785 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a  = pParse->db; /*
2d786 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2d787 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tion */.  const 
2d788 63 68 61 72 20 2a 70 56 54 61 62 20 3d 20 28 63  char *pVTab = (c
2d789 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74  onst char*)sqlit
2d78a 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20  e3GetVTable(db, 
2d78b 70 54 61 62 29 3b 0a 20 20 53 65 6c 65 63 74 44  pTab);.  SelectD
2d78c 65 73 74 20 64 65 73 74 3b 0a 0a 20 20 2f 2a 20  est dest;..  /* 
2d78d 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 53 45  Construct the SE
2d78e 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
2d78f 68 61 74 20 77 69 6c 6c 20 66 69 6e 64 20 74 68  hat will find th
2d790 65 20 6e 65 77 20 76 61 6c 75 65 73 20 66 6f 72  e new values for
2d791 0a 20 20 2a 2a 20 61 6c 6c 20 75 70 64 61 74 65  .  ** all update
2d792 64 20 72 6f 77 73 2e 20 0a 20 20 2a 2f 0a 20 20  d rows. .  */.  
2d793 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  pEList = sqlite3
2d794 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2d795 50 61 72 73 65 2c 20 30 2c 20 73 71 6c 69 74 65  Parse, 0, sqlite
2d796 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 44 2c  3Expr(db, TK_ID,
2d797 20 22 5f 72 6f 77 69 64 5f 22 29 29 3b 0a 20 20   "_rowid_"));.  
2d798 69 66 28 20 70 52 6f 77 69 64 20 29 7b 0a 20 20  if( pRowid ){.  
2d799 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74    pEList = sqlit
2d79a 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2d79b 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c  (pParse, pEList,
2d79c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2d79d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d79e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2d79f 75 70 28 64 62 2c 20 70 52 6f 77 69 64 2c 20 30  up(db, pRowid, 0
2d7a0 29 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ));.  }.  assert
2d7a1 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c 30 20  ( pTab->iPKey<0 
2d7a2 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
2d7a3 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
2d7a4 7b 0a 20 20 20 20 69 66 28 20 61 58 52 65 66 5b  {.    if( aXRef[
2d7a5 69 5d 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  i]>=0 ){.      p
2d7a6 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78  Expr = sqlite3Ex
2d7a7 70 72 44 75 70 28 64 62 2c 20 70 43 68 61 6e 67  prDup(db, pChang
2d7a8 65 73 2d 3e 61 5b 61 58 52 65 66 5b 69 5d 5d 2e  es->a[aXRef[i]].
2d7a9 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 7d  pExpr, 0);.    }
2d7aa 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70  else{.      pExp
2d7ab 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28  r = sqlite3Expr(
2d7ac 64 62 2c 20 54 4b 5f 49 44 2c 20 70 54 61 62 2d  db, TK_ID, pTab-
2d7ad 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b  >aCol[i].zName);
2d7ae 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 4c 69 73  .    }.    pELis
2d7af 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
2d7b0 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2d7b1 2c 20 70 45 4c 69 73 74 2c 20 70 45 78 70 72 29  , pEList, pExpr)
2d7b2 3b 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 20  ;.  }.  pSelect 
2d7b3 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e  = sqlite3SelectN
2d7b4 65 77 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73  ew(pParse, pELis
2d7b5 74 2c 20 70 53 72 63 2c 20 70 57 68 65 72 65 2c  t, pSrc, pWhere,
2d7b6 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2d7b7 30 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 72 65 61  0);.  .  /* Crea
2d7b8 74 65 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c  te the ephemeral
2d7b9 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
2d7ba 68 20 74 68 65 20 75 70 64 61 74 65 20 72 65 73  h the update res
2d7bb 75 6c 74 73 20 77 69 6c 6c 0a 20 20 2a 2a 20 62  ults will.  ** b
2d7bc 65 20 73 74 6f 72 65 64 2e 0a 20 20 2a 2f 0a 20  e stored..  */. 
2d7bd 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20   assert( v );.  
2d7be 65 70 68 65 6d 54 61 62 20 3d 20 70 50 61 72 73  ephemTab = pPars
2d7bf 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 73 71 6c  e->nTab++;.  sql
2d7c0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2d7c1 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2d7c2 61 6c 2c 20 65 70 68 65 6d 54 61 62 2c 20 70 54  al, ephemTab, pT
2d7c3 61 62 2d 3e 6e 43 6f 6c 2b 31 2b 28 70 52 6f 77  ab->nCol+1+(pRow
2d7c4 69 64 21 3d 30 29 29 3b 0a 0a 20 20 2f 2a 20 66  id!=0));..  /* f
2d7c5 69 6c 6c 20 74 68 65 20 65 70 68 65 6d 65 72 61  ill the ephemera
2d7c6 6c 20 74 61 62 6c 65 20 0a 20 20 2a 2f 0a 20 20  l table .  */.  
2d7c7 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
2d7c8 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54  tInit(&dest, SRT
2d7c9 5f 54 61 62 6c 65 2c 20 65 70 68 65 6d 54 61 62  _Table, ephemTab
2d7ca 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
2d7cb 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  ct(pParse, pSele
2d7cc 63 74 2c 20 26 64 65 73 74 29 3b 0a 0a 20 20 2f  ct, &dest);..  /
2d7cd 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2d7ce 74 6f 20 73 63 61 6e 20 74 68 65 20 65 70 68 65  to scan the ephe
2d7cf 6d 65 72 61 6c 20 74 61 62 6c 65 20 61 6e 64 20  meral table and 
2d7d0 63 61 6c 6c 20 56 55 70 64 61 74 65 2e 20 2a 2f  call VUpdate. */
2d7d1 0a 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72  .  iReg = ++pPar
2d7d2 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 70 50 61 72  se->nMem;.  pPar
2d7d3 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62  se->nMem += pTab
2d7d4 2d 3e 6e 43 6f 6c 2b 31 3b 0a 20 20 61 64 64 72  ->nCol+1;.  addr
2d7d5 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2d7d6 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
2d7d7 64 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b  d, ephemTab, 0);
2d7d8 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2d7d9 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
2d7da 6e 2c 20 20 65 70 68 65 6d 54 61 62 2c 20 30 2c  n,  ephemTab, 0,
2d7db 20 69 52 65 67 29 3b 0a 20 20 73 71 6c 69 74 65   iReg);.  sqlite
2d7dc 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2d7dd 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68 65 6d 54  P_Column, ephemT
2d7de 61 62 2c 20 28 70 52 6f 77 69 64 3f 31 3a 30 29  ab, (pRowid?1:0)
2d7df 2c 20 69 52 65 67 2b 31 29 3b 0a 20 20 66 6f 72  , iReg+1);.  for
2d7e0 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
2d7e1 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  ol; i++){.    sq
2d7e2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2d7e3 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70  v, OP_Column, ep
2d7e4 68 65 6d 54 61 62 2c 20 69 2b 31 2b 28 70 52 6f  hemTab, i+1+(pRo
2d7e5 77 69 64 21 3d 30 29 2c 20 69 52 65 67 2b 32 2b  wid!=0), iReg+2+
2d7e6 69 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  i);.  }.  sqlite
2d7e7 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c  3VtabMakeWritabl
2d7e8 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  e(pParse, pTab);
2d7e9 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2d7ea 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64 61  dOp4(v, OP_VUpda
2d7eb 74 65 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f  te, 0, pTab->nCo
2d7ec 6c 2b 32 2c 20 69 52 65 67 2c 20 70 56 54 61 62  l+2, iReg, pVTab
2d7ed 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 73 71  , P4_VTAB);.  sq
2d7ee 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50  lite3MayAbort(pP
2d7ef 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
2d7f0 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2d7f1 5f 4e 65 78 74 2c 20 65 70 68 65 6d 54 61 62 2c  _Next, ephemTab,
2d7f2 20 61 64 64 72 2b 31 29 3b 0a 20 20 73 71 6c 69   addr+1);.  sqli
2d7f3 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2d7f4 76 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69  v, addr);.  sqli
2d7f5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2d7f6 20 4f 50 5f 43 6c 6f 73 65 2c 20 65 70 68 65 6d   OP_Close, ephem
2d7f7 54 61 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 43  Tab, 0);..  /* C
2d7f8 6c 65 61 6e 75 70 20 2a 2f 0a 20 20 73 71 6c 69  leanup */.  sqli
2d7f9 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2d7fa 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 20 20 0a  db, pSelect);  .
2d7fb 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2d7fc 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2d7fd 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ABLE */../******
2d7fe 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
2d7ff 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  update.c *******
2d800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d801 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d802 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2d803 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
2d804 69 6c 65 20 76 61 63 75 75 6d 2e 63 20 2a 2a 2a  ile vacuum.c ***
2d805 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d806 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d807 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
2d808 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a  003 April 6.**.*
2d809 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2d80a 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2d80b 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2d80c 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2d80d 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2d80e 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2d80f 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2d810 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2d811 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2d812 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2d813 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
2d814 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
2d815 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
2d816 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2d817 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2d818 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2d819 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2d81a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2d81b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d81c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d81d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d81e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
2d81f 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
2d820 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d   code used to im
2d821 70 6c 65 6d 65 6e 74 20 74 68 65 20 56 41 43 55  plement the VACU
2d822 55 4d 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a  UM command..**.*
2d823 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f  * Most of the co
2d824 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
2d825 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62  may be omitted b
2d826 79 20 64 65 66 69 6e 69 6e 67 20 74 68 65 0a 2a  y defining the.*
2d827 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  * SQLITE_OMIT_VA
2d828 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a  CUUM macro..**.*
2d829 2a 20 24 49 64 3a 20 76 61 63 75 75 6d 2e 63 2c  * $Id: vacuum.c,
2d82a 76 20 31 2e 39 31 20 32 30 30 39 2f 30 37 2f 30  v 1.91 2009/07/0
2d82b 32 20 30 37 3a 34 37 3a 33 33 20 64 61 6e 69 65  2 07:47:33 danie
2d82c 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
2d82d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2d82e 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
2d82f 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
2d830 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
2d831 29 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  )./*.** Execute 
2d832 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61 73 65  zSql on database
2d833 20 64 62 2e 20 52 65 74 75 72 6e 20 61 6e 20 65   db. Return an e
2d834 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74  rror code..*/.st
2d835 61 74 69 63 20 69 6e 74 20 65 78 65 63 53 71 6c  atic int execSql
2d836 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
2d837 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b  nst char *zSql){
2d838 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2d839 2a 70 53 74 6d 74 3b 0a 20 20 56 56 41 5f 4f 4e  *pStmt;.  VVA_ON
2d83a 4c 59 28 20 69 6e 74 20 72 63 3b 20 29 0a 20 20  LY( int rc; ).  
2d83b 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20  if( !zSql ){.   
2d83c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2d83d 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OMEM;.  }.  if( 
2d83e 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
2d83f 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a  e3_prepare(db, z
2d840 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c  Sql, -1, &pStmt,
2d841 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   0) ){.    retur
2d842 6e 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  n sqlite3_errcod
2d843 65 28 64 62 29 3b 0a 20 20 7d 0a 20 20 56 56 41  e(db);.  }.  VVA
2d844 5f 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 71  _ONLY( rc = ) sq
2d845 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
2d846 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  );.  assert( rc!
2d847 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 3b 0a 20  =SQLITE_ROW );. 
2d848 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
2d849 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b  finalize(pStmt);
2d84a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  .}../*.** Execut
2d84b 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61  e zSql on databa
2d84c 73 65 20 64 62 2e 20 54 68 65 20 73 74 61 74 65  se db. The state
2d84d 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 65 78 61  ment returns exa
2d84e 63 74 6c 79 0a 2a 2a 20 6f 6e 65 20 63 6f 6c 75  ctly.** one colu
2d84f 6d 6e 2e 20 45 78 65 63 75 74 65 20 74 68 69 73  mn. Execute this
2d850 20 61 73 20 53 51 4c 20 6f 6e 20 74 68 65 20 73   as SQL on the s
2d851 61 6d 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  ame database..*/
2d852 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 65 63  .static int exec
2d853 45 78 65 63 53 71 6c 28 73 71 6c 69 74 65 33 20  ExecSql(sqlite3 
2d854 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
2d855 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65  *zSql){.  sqlite
2d856 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20  3_stmt *pStmt;. 
2d857 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d   int rc;..  rc =
2d858 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
2d859 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  (db, zSql, -1, &
2d85a 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28  pStmt, 0);.  if(
2d85b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2d85c 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 77   return rc;..  w
2d85d 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 52 4f 57  hile( SQLITE_ROW
2d85e 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  ==sqlite3_step(p
2d85f 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 72 63 20  Stmt) ){.    rc 
2d860 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20 28 63  = execSql(db, (c
2d861 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c  har*)sqlite3_col
2d862 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20  umn_text(pStmt, 
2d863 30 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  0));.    if( rc!
2d864 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2d865 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
2d866 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20 20  lize(pStmt);.   
2d867 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
2d868 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
2d869 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  n sqlite3_finali
2d86a 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a  ze(pStmt);.}../*
2d86b 0a 2a 2a 20 54 68 65 20 6e 6f 6e 2d 73 74 61 6e  .** The non-stan
2d86c 64 61 72 64 20 56 41 43 55 55 4d 20 63 6f 6d 6d  dard VACUUM comm
2d86d 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 63  and is used to c
2d86e 6c 65 61 6e 20 75 70 20 74 68 65 20 64 61 74 61  lean up the data
2d86f 62 61 73 65 2c 0a 2a 2a 20 63 6f 6c 6c 61 70 73  base,.** collaps
2d870 65 20 66 72 65 65 20 73 70 61 63 65 2c 20 65 74  e free space, et
2d871 63 2e 20 20 49 74 20 69 73 20 6d 6f 64 65 6c 6c  c.  It is modell
2d872 65 64 20 61 66 74 65 72 20 74 68 65 20 56 41 43  ed after the VAC
2d873 55 55 4d 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 69  UUM command.** i
2d874 6e 20 50 6f 73 74 67 72 65 53 51 4c 2e 0a 2a 2a  n PostgreSQL..**
2d875 0a 2a 2a 20 49 6e 20 76 65 72 73 69 6f 6e 20 31  .** In version 1
2d876 2e 30 2e 78 20 6f 66 20 53 51 4c 69 74 65 2c 20  .0.x of SQLite, 
2d877 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61  the VACUUM comma
2d878 6e 64 20 77 6f 75 6c 64 20 63 61 6c 6c 0a 2a 2a  nd would call.**
2d879 20 67 64 62 6d 5f 72 65 6f 72 67 61 6e 69 7a 65   gdbm_reorganize
2d87a 28 29 20 6f 6e 20 61 6c 6c 20 74 68 65 20 64 61  () on all the da
2d87b 74 61 62 61 73 65 20 74 61 62 6c 65 73 2e 20 20  tabase tables.  
2d87c 42 75 74 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a  But beginning.**
2d87d 20 77 69 74 68 20 32 2e 30 2e 30 2c 20 53 51 4c   with 2.0.0, SQL
2d87e 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73  ite no longer us
2d87f 65 73 20 47 44 42 4d 20 73 6f 20 74 68 69 73 20  es GDBM so this 
2d880 63 6f 6d 6d 61 6e 64 20 68 61 73 0a 2a 2a 20 62  command has.** b
2d881 65 63 6f 6d 65 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ecome a no-op..*
2d882 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2d883 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 63   void sqlite3Vac
2d884 75 75 6d 28 50 61 72 73 65 20 2a 70 50 61 72 73  uum(Parse *pPars
2d885 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  e){.  Vdbe *v = 
2d886 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2d887 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20  Parse);.  if( v 
2d888 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2d889 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 56  beAddOp2(v, OP_V
2d88a 61 63 75 75 6d 2c 20 30 2c 20 30 29 3b 0a 20 20  acuum, 0, 0);.  
2d88b 7d 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  }.  return;.}../
2d88c 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2d88d 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
2d88e 20 4f 50 5f 56 61 63 75 75 6d 20 6f 70 63 6f 64   OP_Vacuum opcod
2d88f 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e 0a 2a  e of the VDBE..*
2d890 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2d891 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 56   int sqlite3RunV
2d892 61 63 75 75 6d 28 63 68 61 72 20 2a 2a 70 7a 45  acuum(char **pzE
2d893 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 20 2a  rrMsg, sqlite3 *
2d894 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  db){.  int rc = 
2d895 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f  SQLITE_OK;     /
2d896 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72  * Return code fr
2d897 6f 6d 20 73 65 72 76 69 63 65 20 72 6f 75 74 69  om service routi
2d898 6e 65 73 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  nes */.  Btree *
2d899 70 4d 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20  pMain;          
2d89a 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2d89b 20 62 65 69 6e 67 20 76 61 63 75 75 6d 65 64 20   being vacuumed 
2d89c 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 54 65 6d  */.  Btree *pTem
2d89d 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p;           /* 
2d89e 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  The temporary da
2d89f 74 61 62 61 73 65 20 77 65 20 76 61 63 75 75 6d  tabase we vacuum
2d8a0 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20   into */.  char 
2d8a1 2a 7a 53 71 6c 20 3d 20 30 3b 20 20 20 20 20 20  *zSql = 0;      
2d8a2 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d     /* SQL statem
2d8a3 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61  ents */.  int sa
2d8a4 76 65 64 5f 66 6c 61 67 73 3b 20 20 20 20 20 20  ved_flags;      
2d8a5 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65    /* Saved value
2d8a6 20 6f 66 20 74 68 65 20 64 62 2d 3e 66 6c 61 67   of the db->flag
2d8a7 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64  s */.  int saved
2d8a8 5f 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f  _nChange;      /
2d8a9 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66  * Saved value of
2d8aa 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a   db->nChange */.
2d8ab 20 20 69 6e 74 20 73 61 76 65 64 5f 6e 54 6f 74    int saved_nTot
2d8ac 61 6c 43 68 61 6e 67 65 3b 20 2f 2a 20 53 61 76  alChange; /* Sav
2d8ad 65 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e  ed value of db->
2d8ae 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2a 2f 0a  nTotalChange */.
2d8af 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 20 20    Db *pDb = 0;  
2d8b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2d8b1 61 62 61 73 65 20 74 6f 20 64 65 74 61 63 68 20  abase to detach 
2d8b2 61 74 20 65 6e 64 20 6f 66 20 76 61 63 75 75 6d  at end of vacuum
2d8b3 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 65 6d 44   */.  int isMemD
2d8b4 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b;            /*
2d8b5 20 54 72 75 65 20 69 66 20 76 61 63 75 75 6d 69   True if vacuumi
2d8b6 6e 67 20 61 20 3a 6d 65 6d 6f 72 79 3a 20 64 61  ng a :memory: da
2d8b7 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  tabase */.  int 
2d8b8 6e 52 65 73 3b 0a 0a 20 20 69 66 28 20 21 64 62  nRes;..  if( !db
2d8b9 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
2d8ba 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
2d8bb 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64  ring(pzErrMsg, d
2d8bc 62 2c 20 22 63 61 6e 6e 6f 74 20 56 41 43 55 55  b, "cannot VACUU
2d8bd 4d 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20  M from within a 
2d8be 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20  transaction");. 
2d8bf 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2d8c0 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f  _ERROR;.  }..  /
2d8c1 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72 65  * Save the curre
2d8c2 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  nt value of the 
2d8c3 64 61 74 61 62 61 73 65 20 66 6c 61 67 73 20 73  database flags s
2d8c4 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65  o that it can be
2d8c5 20 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 64 20   .  ** restored 
2d8c6 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
2d8c7 2e 20 54 68 65 6e 20 73 65 74 20 74 68 65 20 77  . Then set the w
2d8c8 72 69 74 61 62 6c 65 2d 73 63 68 65 6d 61 20 66  ritable-schema f
2d8c9 6c 61 67 2c 20 61 6e 64 0a 20 20 2a 2a 20 64 69  lag, and.  ** di
2d8ca 73 61 62 6c 65 20 43 48 45 43 4b 20 61 6e 64 20  sable CHECK and 
2d8cb 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
2d8cc 74 72 61 69 6e 74 73 2e 20 20 2a 2f 0a 20 20 73  traints.  */.  s
2d8cd 61 76 65 64 5f 66 6c 61 67 73 20 3d 20 64 62 2d  aved_flags = db-
2d8ce 3e 66 6c 61 67 73 3b 0a 20 20 73 61 76 65 64 5f  >flags;.  saved_
2d8cf 6e 43 68 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 43  nChange = db->nC
2d8d0 68 61 6e 67 65 3b 0a 20 20 73 61 76 65 64 5f 6e  hange;.  saved_n
2d8d1 54 6f 74 61 6c 43 68 61 6e 67 65 20 3d 20 64 62  TotalChange = db
2d8d2 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a  ->nTotalChange;.
2d8d3 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
2d8d4 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d  QLITE_WriteSchem
2d8d5 61 20 7c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72  a | SQLITE_Ignor
2d8d6 65 43 68 65 63 6b 73 3b 0a 20 20 64 62 2d 3e 66  eChecks;.  db->f
2d8d7 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f  lags &= ~SQLITE_
2d8d8 46 6f 72 65 69 67 6e 4b 65 79 73 3b 0a 0a 20 20  ForeignKeys;..  
2d8d9 70 4d 61 69 6e 20 3d 20 64 62 2d 3e 61 44 62 5b  pMain = db->aDb[
2d8da 30 5d 2e 70 42 74 3b 0a 20 20 69 73 4d 65 6d 44  0].pBt;.  isMemD
2d8db 62 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  b = sqlite3Pager
2d8dc 49 73 4d 65 6d 64 62 28 73 71 6c 69 74 65 33 42  IsMemdb(sqlite3B
2d8dd 74 72 65 65 50 61 67 65 72 28 70 4d 61 69 6e 29  treePager(pMain)
2d8de 29 3b 0a 0a 20 20 2f 2a 20 41 74 74 61 63 68 20  );..  /* Attach 
2d8df 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
2d8e0 74 61 62 61 73 65 20 61 73 20 27 76 61 63 75 75  tabase as 'vacuu
2d8e1 6d 5f 64 62 27 2e 20 54 68 65 20 73 79 6e 63 68  m_db'. The synch
2d8e2 72 6f 6e 6f 75 73 20 70 72 61 67 6d 61 0a 20 20  ronous pragma.  
2d8e3 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 74 6f  ** can be set to
2d8e4 20 27 6f 66 66 27 20 66 6f 72 20 74 68 69 73 20   'off' for this 
2d8e5 66 69 6c 65 2c 20 61 73 20 69 74 20 69 73 20 6e  file, as it is n
2d8e6 6f 74 20 72 65 63 6f 76 65 72 65 64 20 69 66 20  ot recovered if 
2d8e7 61 20 63 72 61 73 68 0a 20 20 2a 2a 20 6f 63 63  a crash.  ** occ
2d8e8 75 72 73 20 61 6e 79 77 61 79 2e 20 54 68 65 20  urs anyway. The 
2d8e9 69 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65  integrity of the
2d8ea 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 61 69   database is mai
2d8eb 6e 74 61 69 6e 65 64 20 62 79 20 61 0a 20 20 2a  ntained by a.  *
2d8ec 2a 20 28 70 6f 73 73 69 62 6c 79 20 73 79 6e 63  * (possibly sync
2d8ed 68 72 6f 6e 6f 75 73 29 20 74 72 61 6e 73 61 63  hronous) transac
2d8ee 74 69 6f 6e 20 6f 70 65 6e 65 64 20 6f 6e 20 74  tion opened on t
2d8ef 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
2d8f0 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 73 71 6c   before.  ** sql
2d8f1 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c  ite3BtreeCopyFil
2d8f2 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20  e() is called.. 
2d8f3 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 6f 70 74 69   **.  ** An opti
2d8f4 6d 69 73 61 74 69 6f 6e 20 77 6f 75 6c 64 20 62  misation would b
2d8f5 65 20 74 6f 20 75 73 65 20 61 20 6e 6f 6e 2d 6a  e to use a non-j
2d8f6 6f 75 72 6e 61 6c 65 64 20 70 61 67 65 72 2e 0a  ournaled pager..
2d8f7 20 20 2a 2a 20 28 4c 61 74 65 72 3a 29 20 49 20    ** (Later:) I 
2d8f8 74 72 69 65 64 20 73 65 74 74 69 6e 67 20 22 50  tried setting "P
2d8f9 52 41 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e  RAGMA vacuum_db.
2d8fa 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46  journal_mode=OFF
2d8fb 22 20 62 75 74 0a 20 20 2a 2a 20 74 68 61 74 20  " but.  ** that 
2d8fc 61 63 74 75 61 6c 6c 79 20 6d 61 64 65 20 74 68  actually made th
2d8fd 65 20 56 41 43 55 55 4d 20 72 75 6e 20 73 6c 6f  e VACUUM run slo
2d8fe 77 65 72 2e 20 20 56 65 72 79 20 6c 69 74 74 6c  wer.  Very littl
2d8ff 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 0a 20 20  e journalling.  
2d900 2a 2a 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75  ** actually occu
2d901 72 73 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20  rs when doing a 
2d902 76 61 63 75 75 6d 20 73 69 6e 63 65 20 74 68 65  vacuum since the
2d903 20 76 61 63 75 75 6d 5f 64 62 20 69 73 20 69 6e   vacuum_db is in
2d904 69 74 69 61 6c 6c 79 0a 20 20 2a 2a 20 65 6d 70  itially.  ** emp
2d905 74 79 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6a 6f  ty.  Only the jo
2d906 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20  urnal header is 
2d907 77 72 69 74 74 65 6e 2e 20 20 41 70 70 61 72 65  written.  Appare
2d908 6e 74 6c 79 20 69 74 20 74 61 6b 65 73 20 6d 6f  ntly it takes mo
2d909 72 65 0a 20 20 2a 2a 20 74 69 6d 65 20 74 6f 20  re.  ** time to 
2d90a 70 61 72 73 65 20 61 6e 64 20 72 75 6e 20 74 68  parse and run th
2d90b 65 20 50 52 41 47 4d 41 20 74 6f 20 74 75 72 6e  e PRAGMA to turn
2d90c 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 6f 66 66   journalling off
2d90d 20 74 68 61 6e 20 69 74 20 64 6f 65 73 0a 20 20   than it does.  
2d90e 2a 2a 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  ** to write the 
2d90f 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
2d910 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 7a 53 71 6c  ile..  */.  zSql
2d911 20 3d 20 22 41 54 54 41 43 48 20 27 27 20 41 53   = "ATTACH '' AS
2d912 20 76 61 63 75 75 6d 5f 64 62 3b 22 3b 0a 20 20   vacuum_db;";.  
2d913 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c  rc = execSql(db,
2d914 20 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63   zSql);.  if( rc
2d915 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2d916 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2d917 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  ;.  pDb = &db->a
2d918 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 3b 0a 20  Db[db->nDb-1];. 
2d919 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28   assert( strcmp(
2d91a 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d  db->aDb[db->nDb-
2d91b 31 5d 2e 7a 4e 61 6d 65 2c 22 76 61 63 75 75 6d  1].zName,"vacuum
2d91c 5f 64 62 22 29 3d 3d 30 20 29 3b 0a 20 20 70 54  _db")==0 );.  pT
2d91d 65 6d 70 20 3d 20 64 62 2d 3e 61 44 62 5b 64 62  emp = db->aDb[db
2d91e 2d 3e 6e 44 62 2d 31 5d 2e 70 42 74 3b 0a 0a 20  ->nDb-1].pBt;.. 
2d91f 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20   /* The call to 
2d920 65 78 65 63 53 71 6c 28 29 20 74 6f 20 61 74 74  execSql() to att
2d921 61 63 68 20 74 68 65 20 74 65 6d 70 20 64 61 74  ach the temp dat
2d922 61 62 61 73 65 20 68 61 73 20 6c 65 66 74 20 74  abase has left t
2d923 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 6c 6f 63  he file.  ** loc
2d924 6b 65 64 20 28 61 73 20 74 68 65 72 65 20 77 61  ked (as there wa
2d925 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  s more than one 
2d926 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74  active statement
2d927 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61   when the transa
2d928 63 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 72 65  ction.  ** to re
2d929 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 77 61  ad the schema wa
2d92a 73 20 63 6f 6e 63 6c 75 64 65 64 2e 20 55 6e 6c  s concluded. Unl
2d92b 6f 63 6b 20 69 74 20 68 65 72 65 20 73 6f 20 74  ock it here so t
2d92c 68 61 74 20 74 68 69 73 20 64 6f 65 73 6e 27 74  hat this doesn't
2d92d 0a 20 20 2a 2a 20 63 61 75 73 65 20 70 72 6f 62  .  ** cause prob
2d92e 6c 65 6d 73 20 66 6f 72 20 74 68 65 20 63 61 6c  lems for the cal
2d92f 6c 20 74 6f 20 42 74 72 65 65 53 65 74 50 61 67  l to BtreeSetPag
2d930 65 53 69 7a 65 28 29 20 62 65 6c 6f 77 2e 20 20  eSize() below.  
2d931 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  */.  sqlite3Btre
2d932 65 43 6f 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0a  eCommit(pTemp);.
2d933 0a 20 20 6e 52 65 73 20 3d 20 73 71 6c 69 74 65  .  nRes = sqlite
2d934 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65  3BtreeGetReserve
2d935 28 70 4d 61 69 6e 29 3b 0a 0a 20 20 2f 2a 20 41  (pMain);..  /* A
2d936 20 56 41 43 55 55 4d 20 63 61 6e 6e 6f 74 20 63   VACUUM cannot c
2d937 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 73 69  hange the pagesi
2d938 7a 65 20 6f 66 20 61 6e 20 65 6e 63 72 79 70 74  ze of an encrypt
2d939 65 64 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a  ed database. */.
2d93a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41  #ifdef SQLITE_HA
2d93b 53 5f 43 4f 44 45 43 0a 20 20 69 66 28 20 64 62  S_CODEC.  if( db
2d93c 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20 29  ->nextPagesize )
2d93d 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69  {.    extern voi
2d93e 64 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65  d sqlite3CodecGe
2d93f 74 4b 65 79 28 73 71 6c 69 74 65 33 2a 2c 20 69  tKey(sqlite3*, i
2d940 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a  nt, void**, int*
2d941 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b  );.    int nKey;
2d942 0a 20 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b  .    char *zKey;
2d943 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65  .    sqlite3Code
2d944 63 47 65 74 4b 65 79 28 64 62 2c 20 30 2c 20 28  cGetKey(db, 0, (
2d945 76 6f 69 64 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e  void**)&zKey, &n
2d946 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 6e 4b  Key);.    if( nK
2d947 65 79 20 29 20 64 62 2d 3e 6e 65 78 74 50 61 67  ey ) db->nextPag
2d948 65 73 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a 23  esize = 0;.  }.#
2d949 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c  endif..  if( sql
2d94a 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65  ite3BtreeSetPage
2d94b 53 69 7a 65 28 70 54 65 6d 70 2c 20 73 71 6c 69  Size(pTemp, sqli
2d94c 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53  te3BtreeGetPageS
2d94d 69 7a 65 28 70 4d 61 69 6e 29 2c 20 6e 52 65 73  ize(pMain), nRes
2d94e 2c 20 30 29 0a 20 20 20 7c 7c 20 28 21 69 73 4d  , 0).   || (!isM
2d94f 65 6d 44 62 20 26 26 20 73 71 6c 69 74 65 33 42  emDb && sqlite3B
2d950 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28  treeSetPageSize(
2d951 70 54 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74 50  pTemp, db->nextP
2d952 61 67 65 73 69 7a 65 2c 20 6e 52 65 73 2c 20 30  agesize, nRes, 0
2d953 29 29 0a 20 20 20 7c 7c 20 4e 45 56 45 52 28 64  )).   || NEVER(d
2d954 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29  b->mallocFailed)
2d955 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  .  ){.    rc = S
2d956 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
2d957 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
2d958 75 75 6d 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  uum;.  }.  rc = 
2d959 65 78 65 63 53 71 6c 28 64 62 2c 20 22 50 52 41  execSql(db, "PRA
2d95a 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e 73 79  GMA vacuum_db.sy
2d95b 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 22 29 3b  nchronous=OFF");
2d95c 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2d95d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f  E_OK ){.    goto
2d95e 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2d95f 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
2d960 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
2d961 55 55 4d 0a 20 20 73 71 6c 69 74 65 33 42 74 72  UUM.  sqlite3Btr
2d962 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeSetAutoVacuum(
2d963 70 54 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74 41  pTemp, db->nextA
2d964 75 74 6f 76 61 63 3e 3d 30 20 3f 20 64 62 2d 3e  utovac>=0 ? db->
2d965 6e 65 78 74 41 75 74 6f 76 61 63 20 3a 0a 20 20  nextAutovac :.  
2d966 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d967 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d968 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2d969 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63 75  BtreeGetAutoVacu
2d96a 75 6d 28 70 4d 61 69 6e 29 29 3b 0a 23 65 6e 64  um(pMain));.#end
2d96b 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61  if..  /* Begin a
2d96c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a   transaction */.
2d96d 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64    rc = execSql(d
2d96e 62 2c 20 22 42 45 47 49 4e 20 45 58 43 4c 55 53  b, "BEGIN EXCLUS
2d96f 49 56 45 3b 22 29 3b 0a 20 20 69 66 28 20 72 63  IVE;");.  if( rc
2d970 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2d971 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2d972 3b 0a 0a 20 20 2f 2a 20 51 75 65 72 79 20 74 68  ;..  /* Query th
2d973 65 20 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20  e schema of the 
2d974 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 43  main database. C
2d975 72 65 61 74 65 20 61 20 6d 69 72 72 6f 72 20 73  reate a mirror s
2d976 63 68 65 6d 61 0a 20 20 2a 2a 20 69 6e 20 74 68  chema.  ** in th
2d977 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  e temporary data
2d978 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  base..  */.  rc 
2d979 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62  = execExecSql(db
2d97a 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54  , .      "SELECT
2d97b 20 27 43 52 45 41 54 45 20 54 41 42 4c 45 20 76   'CREATE TABLE v
2d97c 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75  acuum_db.' || su
2d97d 62 73 74 72 28 73 71 6c 2c 31 34 29 20 22 0a 20  bstr(sql,14) ". 
2d97e 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c       "  FROM sql
2d97f 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45  ite_master WHERE
2d980 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e   type='table' AN
2d981 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65 5f  D name!='sqlite_
2d982 73 65 71 75 65 6e 63 65 27 22 0a 20 20 20 20 20  sequence'".     
2d983 20 22 20 20 20 41 4e 44 20 72 6f 6f 74 70 61 67   "   AND rootpag
2d984 65 3e 30 22 0a 20 20 29 3b 0a 20 20 69 66 28 20  e>0".  );.  if( 
2d985 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2d986 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
2d987 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45  um;.  rc = execE
2d988 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20  xecSql(db, .    
2d989 20 20 22 53 45 4c 45 43 54 20 27 43 52 45 41 54    "SELECT 'CREAT
2d98a 45 20 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64  E INDEX vacuum_d
2d98b 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71  b.' || substr(sq
2d98c 6c 2c 31 34 29 22 0a 20 20 20 20 20 20 22 20 20  l,14)".      "  
2d98d 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74  FROM sqlite_mast
2d98e 65 72 20 57 48 45 52 45 20 73 71 6c 20 4c 49 4b  er WHERE sql LIK
2d98f 45 20 27 43 52 45 41 54 45 20 49 4e 44 45 58 20  E 'CREATE INDEX 
2d990 25 27 20 22 29 3b 0a 20 20 69 66 28 20 72 63 21  %' ");.  if( rc!
2d991 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
2d992 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
2d993 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65 63  .  rc = execExec
2d994 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22  Sql(db, .      "
2d995 53 45 4c 45 43 54 20 27 43 52 45 41 54 45 20 55  SELECT 'CREATE U
2d996 4e 49 51 55 45 20 49 4e 44 45 58 20 76 61 63 75  NIQUE INDEX vacu
2d997 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73 74  um_db.' || subst
2d998 72 28 73 71 6c 2c 32 31 29 20 22 0a 20 20 20 20  r(sql,21) ".    
2d999 20 20 22 20 20 46 52 4f 4d 20 73 71 6c 69 74 65    "  FROM sqlite
2d99a 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 73 71  _master WHERE sq
2d99b 6c 20 4c 49 4b 45 20 27 43 52 45 41 54 45 20 55  l LIKE 'CREATE U
2d99c 4e 49 51 55 45 20 49 4e 44 45 58 20 25 27 22 29  NIQUE INDEX %'")
2d99d 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2d99e 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
2d99f 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f  _of_vacuum;..  /
2d9a0 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  * Loop through t
2d9a1 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  he tables in the
2d9a2 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20   main database. 
2d9a3 46 6f 72 20 65 61 63 68 2c 20 64 6f 0a 20 20 2a  For each, do.  *
2d9a4 2a 20 61 6e 20 22 49 4e 53 45 52 54 20 49 4e 54  * an "INSERT INT
2d9a5 4f 20 76 61 63 75 75 6d 5f 64 62 2e 78 78 78 20  O vacuum_db.xxx 
2d9a6 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6d 61  SELECT * FROM ma
2d9a7 69 6e 2e 78 78 78 3b 22 20 74 6f 20 63 6f 70 79  in.xxx;" to copy
2d9a8 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  .  ** the conten
2d9a9 74 73 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72  ts to the tempor
2d9aa 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20  ary database..  
2d9ab 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78  */.  rc = execEx
2d9ac 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20  ecSql(db, .     
2d9ad 20 22 53 45 4c 45 43 54 20 27 49 4e 53 45 52 54   "SELECT 'INSERT
2d9ae 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e   INTO vacuum_db.
2d9af 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29  ' || quote(name)
2d9b0 20 22 0a 20 20 20 20 20 20 22 7c 7c 20 27 20 53   ".      "|| ' S
2d9b1 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6d 61 69  ELECT * FROM mai
2d9b2 6e 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d  n.' || quote(nam
2d9b3 65 29 20 7c 7c 20 27 3b 27 22 0a 20 20 20 20 20  e) || ';'".     
2d9b4 20 22 46 52 4f 4d 20 6d 61 69 6e 2e 73 71 6c 69   "FROM main.sqli
2d9b5 74 65 5f 6d 61 73 74 65 72 20 22 0a 20 20 20 20  te_master ".    
2d9b6 20 20 22 57 48 45 52 45 20 74 79 70 65 20 3d 20    "WHERE type = 
2d9b7 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65  'table' AND name
2d9b8 21 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  !='sqlite_sequen
2d9b9 63 65 27 20 22 0a 20 20 20 20 20 20 22 20 20 41  ce' ".      "  A
2d9ba 4e 44 20 72 6f 6f 74 70 61 67 65 3e 30 22 0a 0a  ND rootpage>0"..
2d9bb 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53    );.  if( rc!=S
2d9bc 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2d9bd 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a  end_of_vacuum;..
2d9be 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72 20 74    /* Copy over t
2d9bf 68 65 20 73 65 71 75 65 6e 63 65 20 74 61 62 6c  he sequence tabl
2d9c0 65 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78  e.  */.  rc = ex
2d9c1 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20  ecExecSql(db, . 
2d9c2 20 20 20 20 20 22 53 45 4c 45 43 54 20 27 44 45       "SELECT 'DE
2d9c3 4c 45 54 45 20 46 52 4f 4d 20 76 61 63 75 75 6d  LETE FROM vacuum
2d9c4 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e  _db.' || quote(n
2d9c5 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22 0a 20 20  ame) || ';' ".  
2d9c6 20 20 20 20 22 46 52 4f 4d 20 76 61 63 75 75 6d      "FROM vacuum
2d9c7 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65  _db.sqlite_maste
2d9c8 72 20 57 48 45 52 45 20 6e 61 6d 65 3d 27 73 71  r WHERE name='sq
2d9c9 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27 20 22  lite_sequence' "
2d9ca 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  .  );.  if( rc!=
2d9cb 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2d9cc 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2d9cd 20 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53    rc = execExecS
2d9ce 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53  ql(db, .      "S
2d9cf 45 4c 45 43 54 20 27 49 4e 53 45 52 54 20 49 4e  ELECT 'INSERT IN
2d9d0 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c  TO vacuum_db.' |
2d9d1 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 22 0a  | quote(name) ".
2d9d2 20 20 20 20 20 20 22 7c 7c 20 27 20 53 45 4c 45        "|| ' SELE
2d9d3 43 54 20 2a 20 46 52 4f 4d 20 6d 61 69 6e 2e 27  CT * FROM main.'
2d9d4 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20   || quote(name) 
2d9d5 7c 7c 20 27 3b 27 20 22 0a 20 20 20 20 20 20 22  || ';' ".      "
2d9d6 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73  FROM vacuum_db.s
2d9d7 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
2d9d8 52 45 20 6e 61 6d 65 3d 3d 27 73 71 6c 69 74 65  RE name=='sqlite
2d9d9 5f 73 65 71 75 65 6e 63 65 27 3b 22 0a 20 20 29  _sequence';".  )
2d9da 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2d9db 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
2d9dc 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 0a 20 20  _of_vacuum;...  
2d9dd 2f 2a 20 43 6f 70 79 20 74 68 65 20 74 72 69 67  /* Copy the trig
2d9de 67 65 72 73 2c 20 76 69 65 77 73 2c 20 61 6e 64  gers, views, and
2d9df 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
2d9e0 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61  from the main da
2d9e1 74 61 62 61 73 65 0a 20 20 2a 2a 20 6f 76 65 72  tabase.  ** over
2d9e2 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72   to the temporar
2d9e3 79 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e  y database.  Non
2d9e4 65 20 6f 66 20 74 68 65 73 65 20 6f 62 6a 65 63  e of these objec
2d9e5 74 73 20 68 61 73 20 61 6e 79 0a 20 20 2a 2a 20  ts has any.  ** 
2d9e6 61 73 73 6f 63 69 61 74 65 64 20 73 74 6f 72 61  associated stora
2d9e7 67 65 2c 20 73 6f 20 61 6c 6c 20 77 65 20 68 61  ge, so all we ha
2d9e8 76 65 20 74 6f 20 64 6f 20 69 73 20 63 6f 70 79  ve to do is copy
2d9e9 20 74 68 65 69 72 20 65 6e 74 72 69 65 73 0a 20   their entries. 
2d9ea 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 51 4c   ** from the SQL
2d9eb 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65  ITE_MASTER table
2d9ec 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78  ..  */.  rc = ex
2d9ed 65 63 53 71 6c 28 64 62 2c 0a 20 20 20 20 20 20  ecSql(db,.      
2d9ee 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63  "INSERT INTO vac
2d9ef 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61  uum_db.sqlite_ma
2d9f0 73 74 65 72 20 22 0a 20 20 20 20 20 20 22 20 20  ster ".      "  
2d9f1 53 45 4c 45 43 54 20 74 79 70 65 2c 20 6e 61 6d  SELECT type, nam
2d9f2 65 2c 20 74 62 6c 5f 6e 61 6d 65 2c 20 72 6f 6f  e, tbl_name, roo
2d9f3 74 70 61 67 65 2c 20 73 71 6c 22 0a 20 20 20 20  tpage, sql".    
2d9f4 20 20 22 20 20 20 20 46 52 4f 4d 20 6d 61 69 6e    "    FROM main
2d9f5 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 0a  .sqlite_master".
2d9f6 20 20 20 20 20 20 22 20 20 20 57 48 45 52 45 20        "   WHERE 
2d9f7 74 79 70 65 3d 27 76 69 65 77 27 20 4f 52 20 74  type='view' OR t
2d9f8 79 70 65 3d 27 74 72 69 67 67 65 72 27 22 0a 20  ype='trigger'". 
2d9f9 20 20 20 20 20 22 20 20 20 20 20 20 4f 52 20 28       "      OR (
2d9fa 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e 44  type='table' AND
2d9fb 20 72 6f 6f 74 70 61 67 65 3d 30 29 22 0a 20 20   rootpage=0)".  
2d9fc 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f  );.  if( rc ) go
2d9fd 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2d9fe 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20  ;..  /* At this 
2d9ff 70 6f 69 6e 74 2c 20 75 6e 6c 65 73 73 20 74 68  point, unless th
2da00 65 20 6d 61 69 6e 20 64 62 20 77 61 73 20 63 6f  e main db was co
2da01 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2c 20  mpletely empty, 
2da02 74 68 65 72 65 20 69 73 20 6e 6f 77 20 61 0a 20  there is now a. 
2da03 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
2da04 6f 70 65 6e 20 6f 6e 20 74 68 65 20 76 61 63 75  open on the vacu
2da05 75 6d 20 64 61 74 61 62 61 73 65 2c 20 62 75 74  um database, but
2da06 20 6e 6f 74 20 6f 6e 20 74 68 65 20 6d 61 69 6e   not on the main
2da07 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20   database..  ** 
2da08 4f 70 65 6e 20 61 20 62 74 72 65 65 20 6c 65 76  Open a btree lev
2da09 65 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  el transaction o
2da0a 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
2da0b 61 73 65 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73  ase. This allows
2da0c 20 61 0a 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20   a.  ** call to 
2da0d 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79  sqlite3BtreeCopy
2da0e 46 69 6c 65 28 29 2e 20 54 68 65 20 6d 61 69 6e  File(). The main
2da0f 20 64 61 74 61 62 61 73 65 20 62 74 72 65 65 20   database btree 
2da10 6c 65 76 65 6c 0a 20 20 2a 2a 20 74 72 61 6e 73  level.  ** trans
2da11 61 63 74 69 6f 6e 20 69 73 20 74 68 65 6e 20 63  action is then c
2da12 6f 6d 6d 69 74 74 65 64 2c 20 73 6f 20 74 68 65  ommitted, so the
2da13 20 53 51 4c 20 6c 65 76 65 6c 20 6e 65 76 65 72   SQL level never
2da14 20 6b 6e 6f 77 73 20 69 74 20 77 61 73 0a 20 20   knows it was.  
2da15 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72  ** opened for wr
2da16 69 74 69 6e 67 2e 20 54 68 69 73 20 77 61 79 2c  iting. This way,
2da17 20 74 68 65 20 53 51 4c 20 74 72 61 6e 73 61 63   the SQL transac
2da18 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65  tion used to cre
2da19 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 74 65 6d  ate the.  ** tem
2da1a 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20  porary database 
2da1b 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62  never needs to b
2da1c 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 20 20 2a  e committed..  *
2da1d 2f 0a 20 20 7b 0a 20 20 20 20 75 33 32 20 6d 65  /.  {.    u32 me
2da1e 74 61 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a  ta;.    int i;..
2da1f 20 20 20 20 2f 2a 20 54 68 69 73 20 61 72 72 61      /* This arra
2da20 79 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69  y determines whi
2da21 63 68 20 6d 65 74 61 20 6d 65 74 61 20 76 61 6c  ch meta meta val
2da22 75 65 73 20 61 72 65 20 70 72 65 73 65 72 76 65  ues are preserve
2da23 64 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  d in the.    ** 
2da24 76 61 63 75 75 6d 2e 20 20 45 76 65 6e 20 65 6e  vacuum.  Even en
2da25 74 72 69 65 73 20 61 72 65 20 74 68 65 20 6d 65  tries are the me
2da26 74 61 20 76 61 6c 75 65 20 6e 75 6d 62 65 72 20  ta value number 
2da27 61 6e 64 20 6f 64 64 20 65 6e 74 72 69 65 73 0a  and odd entries.
2da28 20 20 20 20 2a 2a 20 61 72 65 20 61 6e 20 69 6e      ** are an in
2da29 63 72 65 6d 65 6e 74 20 74 6f 20 61 70 70 6c 79  crement to apply
2da2a 20 74 6f 20 74 68 65 20 6d 65 74 61 20 76 61 6c   to the meta val
2da2b 75 65 20 61 66 74 65 72 20 74 68 65 20 76 61 63  ue after the vac
2da2c 75 75 6d 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20  uum..    ** The 
2da2d 69 6e 63 72 65 6d 65 6e 74 20 69 73 20 75 73 65  increment is use
2da2e 64 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68  d to increase th
2da2f 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  e schema cookie 
2da30 73 6f 20 74 68 61 74 20 6f 74 68 65 72 0a 20 20  so that other.  
2da31 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73    ** connections
2da32 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74   to the same dat
2da33 61 62 61 73 65 20 77 69 6c 6c 20 6b 6e 6f 77 20  abase will know 
2da34 74 6f 20 72 65 72 65 61 64 20 74 68 65 20 73 63  to reread the sc
2da35 68 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  hema..    */.   
2da36 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
2da37 73 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f 70  signed char aCop
2da38 79 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 42  y[] = {.       B
2da39 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53  TREE_SCHEMA_VERS
2da3a 49 4f 4e 2c 20 20 20 20 20 31 2c 20 20 2f 2a 20  ION,     1,  /* 
2da3b 41 64 64 20 6f 6e 65 20 74 6f 20 74 68 65 20 6f  Add one to the o
2da3c 6c 64 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  ld schema cookie
2da3d 20 2a 2f 0a 20 20 20 20 20 20 20 42 54 52 45 45   */.       BTREE
2da3e 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
2da3f 49 5a 45 2c 20 30 2c 20 20 2f 2a 20 50 72 65 73  IZE, 0,  /* Pres
2da40 65 72 76 65 20 74 68 65 20 64 65 66 61 75 6c 74  erve the default
2da41 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
2da42 20 2a 2f 0a 20 20 20 20 20 20 20 42 54 52 45 45   */.       BTREE
2da43 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2c 20  _TEXT_ENCODING, 
2da44 20 20 20 20 20 30 2c 20 20 2f 2a 20 50 72 65 73       0,  /* Pres
2da45 65 72 76 65 20 74 68 65 20 74 65 78 74 20 65 6e  erve the text en
2da46 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20  coding */.      
2da47 20 42 54 52 45 45 5f 55 53 45 52 5f 56 45 52 53   BTREE_USER_VERS
2da48 49 4f 4e 2c 20 20 20 20 20 20 20 30 2c 20 20 2f  ION,       0,  /
2da49 2a 20 50 72 65 73 65 72 76 65 20 74 68 65 20 75  * Preserve the u
2da4a 73 65 72 20 76 65 72 73 69 6f 6e 20 2a 2f 0a 20  ser version */. 
2da4b 20 20 20 7d 3b 0a 0a 20 20 20 20 61 73 73 65 72     };..    asser
2da4c 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42 74 72  t( 1==sqlite3Btr
2da4d 65 65 49 73 49 6e 54 72 61 6e 73 28 70 54 65 6d  eeIsInTrans(pTem
2da4e 70 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  p) );.    assert
2da4f 28 20 31 3d 3d 73 71 6c 69 74 65 33 42 74 72 65  ( 1==sqlite3Btre
2da50 65 49 73 49 6e 54 72 61 6e 73 28 70 4d 61 69 6e  eIsInTrans(pMain
2da51 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 70  ) );..    /* Cop
2da52 79 20 42 74 72 65 65 20 6d 65 74 61 20 76 61 6c  y Btree meta val
2da53 75 65 73 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  ues */.    for(i
2da54 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
2da55 61 43 6f 70 79 29 3b 20 69 2b 3d 32 29 7b 0a 20  aCopy); i+=2){. 
2da56 20 20 20 20 20 2f 2a 20 47 65 74 4d 65 74 61 28       /* GetMeta(
2da57 29 20 61 6e 64 20 55 70 64 61 74 65 4d 65 74 61  ) and UpdateMeta
2da58 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 69  () cannot fail i
2da59 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 62  n this context b
2da5a 65 63 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20  ecause.      ** 
2da5b 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20  we already have 
2da5c 70 61 67 65 20 31 20 6c 6f 61 64 65 64 20 69 6e  page 1 loaded in
2da5d 74 6f 20 63 61 63 68 65 20 61 6e 64 20 6d 61 72  to cache and mar
2da5e 6b 65 64 20 64 69 72 74 79 2e 20 2a 2f 0a 20 20  ked dirty. */.  
2da5f 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
2da60 47 65 74 4d 65 74 61 28 70 4d 61 69 6e 2c 20 61  GetMeta(pMain, a
2da61 43 6f 70 79 5b 69 5d 2c 20 26 6d 65 74 61 29 3b  Copy[i], &meta);
2da62 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
2da63 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65  te3BtreeUpdateMe
2da64 74 61 28 70 54 65 6d 70 2c 20 61 43 6f 70 79 5b  ta(pTemp, aCopy[
2da65 69 5d 2c 20 6d 65 74 61 2b 61 43 6f 70 79 5b 69  i], meta+aCopy[i
2da66 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +1]);.      if( 
2da67 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45  NEVER(rc!=SQLITE
2da68 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 65 6e 64 5f  _OK) ) goto end_
2da69 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 7d  of_vacuum;.    }
2da6a 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
2da6b 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28  e3BtreeCopyFile(
2da6c 70 4d 61 69 6e 2c 20 70 54 65 6d 70 29 3b 0a 20  pMain, pTemp);. 
2da6d 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2da6e 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
2da6f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 72  of_vacuum;.    r
2da70 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
2da71 43 6f 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0a 20  Commit(pTemp);. 
2da72 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2da73 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
2da74 6f 66 5f 76 61 63 75 75 6d 3b 0a 23 69 66 6e 64  of_vacuum;.#ifnd
2da75 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2da76 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 73 71  UTOVACUUM.    sq
2da77 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74  lite3BtreeSetAut
2da78 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e 2c 20 73  oVacuum(pMain, s
2da79 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75  qlite3BtreeGetAu
2da7a 74 6f 56 61 63 75 75 6d 28 70 54 65 6d 70 29 29  toVacuum(pTemp))
2da7b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20  ;.#endif.  }..  
2da7c 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
2da7d 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20  TE_OK );.  rc = 
2da7e 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50  sqlite3BtreeSetP
2da7f 61 67 65 53 69 7a 65 28 70 4d 61 69 6e 2c 20 73  ageSize(pMain, s
2da80 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
2da81 67 65 53 69 7a 65 28 70 54 65 6d 70 29 2c 20 6e  geSize(pTemp), n
2da82 52 65 73 2c 31 29 3b 0a 0a 65 6e 64 5f 6f 66 5f  Res,1);..end_of_
2da83 76 61 63 75 75 6d 3a 0a 20 20 2f 2a 20 52 65 73  vacuum:.  /* Res
2da84 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61  tore the origina
2da85 6c 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 66  l value of db->f
2da86 6c 61 67 73 20 2a 2f 0a 20 20 64 62 2d 3e 66 6c  lags */.  db->fl
2da87 61 67 73 20 3d 20 73 61 76 65 64 5f 66 6c 61 67  ags = saved_flag
2da88 73 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65  s;.  db->nChange
2da89 20 3d 20 73 61 76 65 64 5f 6e 43 68 61 6e 67 65   = saved_nChange
2da8a 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68  ;.  db->nTotalCh
2da8b 61 6e 67 65 20 3d 20 73 61 76 65 64 5f 6e 54 6f  ange = saved_nTo
2da8c 74 61 6c 43 68 61 6e 67 65 3b 0a 0a 20 20 2f 2a  talChange;..  /*
2da8d 20 43 75 72 72 65 6e 74 6c 79 20 74 68 65 72 65   Currently there
2da8e 20 69 73 20 61 6e 20 53 51 4c 20 6c 65 76 65 6c   is an SQL level
2da8f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
2da90 6e 20 6f 6e 20 74 68 65 20 76 61 63 75 75 6d 0a  n on the vacuum.
2da91 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 4e    ** database. N
2da92 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64  o locks are held
2da93 20 6f 6e 20 61 6e 79 20 6f 74 68 65 72 20 66 69   on any other fi
2da94 6c 65 73 20 28 73 69 6e 63 65 20 74 68 65 20 6d  les (since the m
2da95 61 69 6e 20 66 69 6c 65 0a 20 20 2a 2a 20 77 61  ain file.  ** wa
2da96 73 20 63 6f 6d 6d 69 74 74 65 64 20 61 74 20 74  s committed at t
2da97 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c 29 2e  he btree level).
2da98 20 53 6f 20 69 74 20 73 61 66 65 20 74 6f 20 65   So it safe to e
2da99 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  nd the transacti
2da9a 6f 6e 0a 20 20 2a 2a 20 62 79 20 6d 61 6e 75 61  on.  ** by manua
2da9b 6c 6c 79 20 73 65 74 74 69 6e 67 20 74 68 65 20  lly setting the 
2da9c 61 75 74 6f 43 6f 6d 6d 69 74 20 66 6c 61 67 20  autoCommit flag 
2da9d 74 6f 20 74 72 75 65 20 61 6e 64 20 64 65 74 61  to true and deta
2da9e 63 68 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 76  ching the.  ** v
2da9f 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 20  acuum database. 
2daa0 54 68 65 20 76 61 63 75 75 6d 5f 64 62 20 6a 6f  The vacuum_db jo
2daa1 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 65  urnal file is de
2daa2 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 70  leted when the p
2daa3 61 67 65 72 0a 20 20 2a 2a 20 69 73 20 63 6c 6f  ager.  ** is clo
2daa4 73 65 64 20 62 79 20 74 68 65 20 44 45 54 41 43  sed by the DETAC
2daa5 48 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61 75  H..  */.  db->au
2daa6 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 0a 20  toCommit = 1;.. 
2daa7 20 69 66 28 20 70 44 62 20 29 7b 0a 20 20 20 20   if( pDb ){.    
2daa8 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
2daa9 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20  e(pDb->pBt);.   
2daaa 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20   pDb->pBt = 0;. 
2daab 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20     pDb->pSchema 
2daac 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  = 0;.  }..  sqli
2daad 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
2daae 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 0a  Schema(db, 0);..
2daaf 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
2dab0 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45  endif  /* SQLITE
2dab1 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 26 26 20  _OMIT_VACUUM && 
2dab2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41  SQLITE_OMIT_ATTA
2dab3 43 48 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  CH */../********
2dab4 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 61  ****** End of va
2dab5 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  cuum.c *********
2dab6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dab7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dab8 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2dab9 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
2daba 65 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e vtab.c *******
2dabb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dabc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dabd 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
2dabe 36 20 4a 75 6e 65 20 31 30 0a 2a 2a 0a 2a 2a 20  6 June 10.**.** 
2dabf 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
2dac0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
2dac1 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
2dac2 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
2dac3 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
2dac4 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
2dac5 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
2dac6 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
2dac7 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
2dac8 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
2dac9 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
2daca 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
2dacb 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
2dacc 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
2dacd 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
2dace 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
2dacf 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
2dad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dad1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dad2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dad3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dad4 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
2dad5 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
2dad6 6f 64 65 20 75 73 65 64 20 74 6f 20 68 65 6c 70  ode used to help
2dad7 20 69 6d 70 6c 65 6d 65 6e 74 20 76 69 72 74 75   implement virtu
2dad8 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a  al tables..**.**
2dad9 20 24 49 64 3a 20 76 74 61 62 2e 63 2c 76 20 31   $Id: vtab.c,v 1
2dada 2e 39 34 20 32 30 30 39 2f 30 38 2f 30 38 20 31  .94 2009/08/08 1
2dadb 38 3a 30 31 3a 30 38 20 64 72 68 20 45 78 70 20  8:01:08 drh Exp 
2dadc 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  $.*/.#ifndef SQL
2dadd 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2dade 54 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  TABLE../*.** The
2dadf 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e   actual function
2dae0 20 74 68 61 74 20 64 6f 65 73 20 74 68 65 20 77   that does the w
2dae1 6f 72 6b 20 6f 66 20 63 72 65 61 74 69 6e 67 20  ork of creating 
2dae2 61 20 6e 65 77 20 6d 6f 64 75 6c 65 2e 0a 2a 2a  a new module..**
2dae3 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
2dae4 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 71  mplements the sq
2dae5 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
2dae6 75 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c  ule() and.** sql
2dae7 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
2dae8 6c 65 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63  le_v2() interfac
2dae9 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
2daea 74 20 63 72 65 61 74 65 4d 6f 64 75 6c 65 28 0a  t createModule(.
2daeb 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2daec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2daed 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e    /* Database in
2daee 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20 69 73   which module is
2daef 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f 0a 20   registered */. 
2daf0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2daf1 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  me,             
2daf2 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e 65   /* Name assigne
2daf3 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65  d to this module
2daf4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   */.  const sqli
2daf5 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
2daf6 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64 65 66  ule,  /* The def
2daf7 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d  inition of the m
2daf8 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20  odule */.  void 
2daf9 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20 20 20  *pAux,          
2dafa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
2dafb 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 66 6f  ntext pointer fo
2dafc 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65  r xCreate/xConne
2dafd 63 74 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  ct */.  void (*x
2dafe 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29  Destroy)(void *)
2daff 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c          /* Modul
2db00 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
2db01 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ction */.){.  in
2db02 74 20 72 63 2c 20 6e 4e 61 6d 65 3b 0a 20 20 4d  t rc, nName;.  M
2db03 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 0a 20 20  odule *pMod;..  
2db04 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
2db05 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
2db06 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    nName = sqlite
2db07 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
2db08 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64 75  ;.  pMod = (Modu
2db09 6c 65 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  le *)sqlite3DbMa
2db0a 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
2db0b 6f 66 28 4d 6f 64 75 6c 65 29 20 2b 20 6e 4e 61  of(Module) + nNa
2db0c 6d 65 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 70  me + 1);.  if( p
2db0d 4d 6f 64 20 29 7b 0a 20 20 20 20 4d 6f 64 75 6c  Mod ){.    Modul
2db0e 65 20 2a 70 44 65 6c 3b 0a 20 20 20 20 63 68 61  e *pDel;.    cha
2db0f 72 20 2a 7a 43 6f 70 79 20 3d 20 28 63 68 61 72  r *zCopy = (char
2db10 20 2a 29 28 26 70 4d 6f 64 5b 31 5d 29 3b 0a 20   *)(&pMod[1]);. 
2db11 20 20 20 6d 65 6d 63 70 79 28 7a 43 6f 70 79 2c     memcpy(zCopy,
2db12 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29   zName, nName+1)
2db13 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 7a 4e 61 6d  ;.    pMod->zNam
2db14 65 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20 20 70  e = zCopy;.    p
2db15 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70  Mod->pModule = p
2db16 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 4d 6f 64  Module;.    pMod
2db17 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a 20  ->pAux = pAux;. 
2db18 20 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f     pMod->xDestro
2db19 79 20 3d 20 78 44 65 73 74 72 6f 79 3b 0a 20 20  y = xDestroy;.  
2db1a 20 20 70 44 65 6c 20 3d 20 28 4d 6f 64 75 6c 65    pDel = (Module
2db1b 20 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49 6e   *)sqlite3HashIn
2db1c 73 65 72 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c  sert(&db->aModul
2db1d 65 2c 20 7a 43 6f 70 79 2c 20 6e 4e 61 6d 65 2c  e, zCopy, nName,
2db1e 20 28 76 6f 69 64 2a 29 70 4d 6f 64 29 3b 0a 20   (void*)pMod);. 
2db1f 20 20 20 69 66 28 20 70 44 65 6c 20 26 26 20 70     if( pDel && p
2db20 44 65 6c 2d 3e 78 44 65 73 74 72 6f 79 20 29 7b  Del->xDestroy ){
2db21 0a 20 20 20 20 20 20 70 44 65 6c 2d 3e 78 44 65  .      pDel->xDe
2db22 73 74 72 6f 79 28 70 44 65 6c 2d 3e 70 41 75 78  stroy(pDel->pAux
2db23 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2db24 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2db25 44 65 6c 29 3b 0a 20 20 20 20 69 66 28 20 70 44  Del);.    if( pD
2db26 65 6c 3d 3d 70 4d 6f 64 20 29 7b 0a 20 20 20 20  el==pMod ){.    
2db27 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2db28 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ed = 1;.    }.  
2db29 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
2db2a 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
2db2b 20 30 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   0);.  }else if(
2db2c 20 78 44 65 73 74 72 6f 79 20 29 7b 0a 20 20 20   xDestroy ){.   
2db2d 20 78 44 65 73 74 72 6f 79 28 70 41 75 78 29 3b   xDestroy(pAux);
2db2e 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69  .  }.  rc = sqli
2db2f 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 53  te3ApiExit(db, S
2db30 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 73 71 6c  QLITE_OK);.  sql
2db31 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2db32 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
2db33 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
2db34 0a 2a 2a 20 45 78 74 65 72 6e 61 6c 20 41 50 49  .** External API
2db35 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74   function used t
2db36 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 76  o create a new v
2db37 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 6f 64  irtual-table mod
2db38 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ule..*/.SQLITE_A
2db39 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
2db3a 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 0a 20 20  reate_module(.  
2db3b 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2db3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db3d 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e 20 77  /* Database in w
2db3e 68 69 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 72  hich module is r
2db3f 65 67 69 73 74 65 72 65 64 20 2a 2f 0a 20 20 63  egistered */.  c
2db40 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2db41 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2db42 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e 65 64 20  * Name assigned 
2db43 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a  to this module *
2db44 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  /.  const sqlite
2db45 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
2db46 65 2c 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e  e,  /* The defin
2db47 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64  ition of the mod
2db48 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ule */.  void *p
2db49 41 75 78 20 20 20 20 20 20 20 20 20 20 20 20 20  Aux             
2db4a 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74           /* Cont
2db4b 65 78 74 20 70 6f 69 6e 74 65 72 20 66 6f 72 20  ext pointer for 
2db4c 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74  xCreate/xConnect
2db4d 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20   */.){.  return 
2db4e 63 72 65 61 74 65 4d 6f 64 75 6c 65 28 64 62 2c  createModule(db,
2db4f 20 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c   zName, pModule,
2db50 20 70 41 75 78 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a   pAux, 0);.}../*
2db51 0a 2a 2a 20 45 78 74 65 72 6e 61 6c 20 41 50 49  .** External API
2db52 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74   function used t
2db53 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 76  o create a new v
2db54 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 6f 64  irtual-table mod
2db55 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ule..*/.SQLITE_A
2db56 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
2db57 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28  reate_module_v2(
2db58 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2db59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db5a 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69     /* Database i
2db5b 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20 69  n which module i
2db5c 73 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f 0a  s registered */.
2db5d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
2db5e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ame,            
2db5f 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e    /* Name assign
2db60 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c  ed to this modul
2db61 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c  e */.  const sql
2db62 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
2db63 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64 65  dule,  /* The de
2db64 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20  finition of the 
2db65 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64  module */.  void
2db66 20 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20 20   *pAux,         
2db67 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2db68 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 66  ontext pointer f
2db69 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e  or xCreate/xConn
2db6a 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ect */.  void (*
2db6b 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a  xDestroy)(void *
2db6c 29 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75  )        /* Modu
2db6d 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75  le destructor fu
2db6e 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 72  nction */.){.  r
2db6f 65 74 75 72 6e 20 63 72 65 61 74 65 4d 6f 64 75  eturn createModu
2db70 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d  le(db, zName, pM
2db71 6f 64 75 6c 65 2c 20 70 41 75 78 2c 20 78 44 65  odule, pAux, xDe
2db72 73 74 72 6f 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  stroy);.}../*.**
2db73 20 4c 6f 63 6b 20 74 68 65 20 76 69 72 74 75 61   Lock the virtua
2db74 6c 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74 20  l table so that 
2db75 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 69 73  it cannot be dis
2db76 63 6f 6e 6e 65 63 74 65 64 2e 0a 2a 2a 20 4c 6f  connected..** Lo
2db77 63 6b 73 20 6e 65 73 74 2e 20 20 45 76 65 72 79  cks nest.  Every
2db78 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 68 61 76   lock should hav
2db79 65 20 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  e a correspondin
2db7a 67 20 75 6e 6c 6f 63 6b 2e 0a 2a 2a 20 49 66 20  g unlock..** If 
2db7b 61 6e 20 75 6e 6c 6f 63 6b 20 69 73 20 6f 6d 69  an unlock is omi
2db7c 74 74 65 64 2c 20 72 65 73 6f 75 72 63 65 73 20  tted, resources 
2db7d 6c 65 61 6b 73 20 77 69 6c 6c 20 6f 63 63 75 72  leaks will occur
2db7e 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 64  .  .**.** If a d
2db7f 69 73 63 6f 6e 6e 65 63 74 20 69 73 20 61 74 74  isconnect is att
2db80 65 6d 70 74 65 64 20 77 68 69 6c 65 20 61 20 76  empted while a v
2db81 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20  irtual table is 
2db82 6c 6f 63 6b 65 64 2c 0a 2a 2a 20 74 68 65 20 64  locked,.** the d
2db83 69 73 63 6f 6e 6e 65 63 74 20 69 73 20 64 65 66  isconnect is def
2db84 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20  erred until all 
2db85 6c 6f 63 6b 73 20 68 61 76 65 20 62 65 65 6e 20  locks have been 
2db86 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49  removed..*/.SQLI
2db87 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2db88 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28  sqlite3VtabLock(
2db89 56 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b 0a  VTable *pVTab){.
2db8a 20 20 70 56 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b    pVTab->nRef++;
2db8b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 70 54 61 62 20  .}.../*.** pTab 
2db8c 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
2db8d 61 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  a Table structur
2db8e 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 61  e representing a
2db8f 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2e 0a   virtual-table..
2db90 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
2db91 74 65 72 20 74 6f 20 74 68 65 20 56 54 61 62 6c  ter to the VTabl
2db92 65 20 6f 62 6a 65 63 74 20 75 73 65 64 20 62 79  e object used by
2db93 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 74   connection db t
2db94 6f 20 61 63 63 65 73 73 20 0a 2a 2a 20 74 68 69  o access .** thi
2db95 73 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2c  s virtual-table,
2db96 20 69 66 20 6f 6e 65 20 68 61 73 20 62 65 65 6e   if one has been
2db97 20 63 72 65 61 74 65 64 2c 20 6f 72 20 4e 55 4c   created, or NUL
2db98 4c 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a  L otherwise..*/.
2db99 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
2db9a 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 47 65  Table *sqlite3Ge
2db9b 74 56 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20  tVTable(sqlite3 
2db9c 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  *db, Table *pTab
2db9d 29 7b 0a 20 20 56 54 61 62 6c 65 20 2a 70 56 74  ){.  VTable *pVt
2db9e 61 62 3b 0a 20 20 61 73 73 65 72 74 28 20 49 73  ab;.  assert( Is
2db9f 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b  Virtual(pTab) );
2dba0 0a 20 20 66 6f 72 28 70 56 74 61 62 3d 70 54 61  .  for(pVtab=pTa
2dba1 62 2d 3e 70 56 54 61 62 6c 65 3b 20 70 56 74 61  b->pVTable; pVta
2dba2 62 20 26 26 20 70 56 74 61 62 2d 3e 64 62 21 3d  b && pVtab->db!=
2dba3 64 62 3b 20 70 56 74 61 62 3d 70 56 74 61 62 2d  db; pVtab=pVtab-
2dba4 3e 70 4e 65 78 74 29 3b 0a 20 20 72 65 74 75 72  >pNext);.  retur
2dba5 6e 20 70 56 74 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a  n pVtab;.}../*.*
2dba6 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  * Decrement the 
2dba7 72 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 61 20 76  ref-count on a v
2dba8 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a  irtual table obj
2dba9 65 63 74 2e 20 57 68 65 6e 20 74 68 65 20 72 65  ect. When the re
2dbaa 66 2d 63 6f 75 6e 74 0a 2a 2a 20 72 65 61 63 68  f-count.** reach
2dbab 65 73 20 7a 65 72 6f 2c 20 63 61 6c 6c 20 74 68  es zero, call th
2dbac 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 28 29 20  e xDisconnect() 
2dbad 6d 65 74 68 6f 64 20 74 6f 20 64 65 6c 65 74 65  method to delete
2dbae 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a   the object..*/.
2dbaf 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2dbb0 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 55  oid sqlite3VtabU
2dbb1 6e 6c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 70 56  nlock(VTable *pV
2dbb2 54 61 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  Tab){.  sqlite3 
2dbb3 2a 64 62 20 3d 20 70 56 54 61 62 2d 3e 64 62 3b  *db = pVTab->db;
2dbb4 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 20 29  ..  assert( db )
2dbb5 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 54 61  ;.  assert( pVTa
2dbb6 62 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61  b->nRef>0 );.  a
2dbb7 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 61  ssert( sqlite3Sa
2dbb8 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29 20  fetyCheckOk(db) 
2dbb9 29 3b 0a 0a 20 20 70 56 54 61 62 2d 3e 6e 52 65  );..  pVTab->nRe
2dbba 66 2d 2d 3b 0a 20 20 69 66 28 20 70 56 54 61 62  f--;.  if( pVTab
2dbbb 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20  ->nRef==0 ){.   
2dbbc 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
2dbbd 20 3d 20 70 56 54 61 62 2d 3e 70 56 74 61 62 3b   = pVTab->pVtab;
2dbbe 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69  .    if( p ){.#i
2dbbf 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
2dbc0 47 0a 20 20 20 20 20 20 69 66 28 20 70 56 54 61  G.      if( pVTa
2dbc1 62 2d 3e 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  b->db->magic==SQ
2dbc2 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
2dbc3 29 7b 0a 20 20 20 20 20 20 20 20 28 76 6f 69 64  ){.        (void
2dbc4 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
2dbc5 66 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 70  f(db);.        p
2dbc6 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73 63  ->pModule->xDisc
2dbc7 6f 6e 6e 65 63 74 28 70 29 3b 0a 20 20 20 20 20  onnect(p);.     
2dbc8 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
2dbc9 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
2dbca 20 20 20 20 7d 20 65 6c 73 65 0a 23 65 6e 64 69      } else.#endi
2dbcb 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  f.      {.      
2dbcc 20 20 70 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44    p->pModule->xD
2dbcd 69 73 63 6f 6e 6e 65 63 74 28 70 29 3b 0a 20 20  isconnect(p);.  
2dbce 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2dbcf 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2dbd0 2c 20 70 56 54 61 62 29 3b 0a 20 20 7d 0a 7d 0a  , pVTab);.  }.}.
2dbd1 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 70 20 69  ./*.** Table p i
2dbd2 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  s a virtual tabl
2dbd3 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  e. This function
2dbd4 20 6d 6f 76 65 73 20 61 6c 6c 20 65 6c 65 6d 65   moves all eleme
2dbd5 6e 74 73 20 69 6e 20 74 68 65 0a 2a 2a 20 70 2d  nts in the.** p-
2dbd6 3e 70 56 54 61 62 6c 65 20 6c 69 73 74 20 74 6f  >pVTable list to
2dbd7 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69   the sqlite3.pDi
2dbd8 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 73 20 6f  sconnect lists o
2dbd9 66 20 74 68 65 69 72 20 61 73 73 6f 63 69 61 74  f their associat
2dbda 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  ed.** database c
2dbdb 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 62 65  onnections to be
2dbdc 20 64 69 73 63 6f 6e 6e 65 63 74 65 64 20 61 74   disconnected at
2dbdd 20 74 68 65 20 6e 65 78 74 20 6f 70 70 6f 72 74   the next opport
2dbde 75 6e 69 74 79 2e 20 0a 2a 2a 20 45 78 63 65 70  unity. .** Excep
2dbdf 74 2c 20 69 66 20 61 72 67 75 6d 65 6e 74 20 64  t, if argument d
2dbe0 62 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  b is not NULL, t
2dbe1 68 65 6e 20 74 68 65 20 65 6e 74 72 79 20 61 73  hen the entry as
2dbe2 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a  sociated with.**
2dbe3 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 69   connection db i
2dbe4 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 70 2d  s left in the p-
2dbe5 3e 70 56 54 61 62 6c 65 20 6c 69 73 74 2e 0a 2a  >pVTable list..*
2dbe6 2f 0a 73 74 61 74 69 63 20 56 54 61 62 6c 65 20  /.static VTable 
2dbe7 2a 76 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 41  *vtabDisconnectA
2dbe8 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ll(sqlite3 *db, 
2dbe9 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 56 54 61  Table *p){.  VTa
2dbea 62 6c 65 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20  ble *pRet = 0;. 
2dbeb 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65   VTable *pVTable
2dbec 20 3d 20 70 2d 3e 70 56 54 61 62 6c 65 3b 0a 20   = p->pVTable;. 
2dbed 20 70 2d 3e 70 56 54 61 62 6c 65 20 3d 20 30 3b   p->pVTable = 0;
2dbee 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68  ..  /* Assert th
2dbef 61 74 20 74 68 65 20 6d 75 74 65 78 20 28 69 66  at the mutex (if
2dbf0 20 61 6e 79 29 20 61 73 73 6f 63 69 61 74 65 64   any) associated
2dbf1 20 77 69 74 68 20 74 68 65 20 42 74 53 68 61 72   with the BtShar
2dbf2 65 64 20 64 61 74 61 62 61 73 65 20 0a 20 20 2a  ed database .  *
2dbf3 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  * that contains 
2dbf4 74 61 62 6c 65 20 70 20 69 73 20 68 65 6c 64 20  table p is held 
2dbf5 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 53  by the caller. S
2dbf6 65 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  ee header commen
2dbf7 74 73 20 0a 20 20 2a 2a 20 61 62 6f 76 65 20 66  ts .  ** above f
2dbf8 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 56  unction sqlite3V
2dbf9 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 29 20  tabUnlockList() 
2dbfa 66 6f 72 20 61 6e 20 65 78 70 6c 61 6e 61 74 69  for an explanati
2dbfb 6f 6e 20 6f 66 20 77 68 79 0a 20 20 2a 2a 20 74  on of why.  ** t
2dbfc 68 69 73 20 6d 61 6b 65 73 20 69 74 20 73 61 66  his makes it saf
2dbfd 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  e to access the 
2dbfe 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e  sqlite3.pDisconn
2dbff 65 63 74 20 6c 69 73 74 20 6f 66 20 61 6e 79 0a  ect list of any.
2dc00 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f    ** database co
2dc01 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 6d 61  nnection that ma
2dc02 79 20 68 61 76 65 20 61 6e 20 65 6e 74 72 79 20  y have an entry 
2dc03 69 6e 20 74 68 65 20 70 2d 3e 70 56 54 61 62 6c  in the p->pVTabl
2dc04 65 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20 61 73  e list.  */.  as
2dc05 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 0a 20  sert( db==0 ||. 
2dc06 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 48     sqlite3BtreeH
2dc07 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44  oldsMutex(db->aD
2dc08 62 5b 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  b[sqlite3SchemaT
2dc09 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53  oIndex(db, p->pS
2dc0a 63 68 65 6d 61 29 5d 2e 70 42 74 29 20 0a 20 20  chema)].pBt) .  
2dc0b 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 70 56 54  );..  while( pVT
2dc0c 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  able ){.    sqli
2dc0d 74 65 33 20 2a 64 62 32 20 3d 20 70 56 54 61 62  te3 *db2 = pVTab
2dc0e 6c 65 2d 3e 64 62 3b 0a 20 20 20 20 56 54 61 62  le->db;.    VTab
2dc0f 6c 65 20 2a 70 4e 65 78 74 20 3d 20 70 56 54 61  le *pNext = pVTa
2dc10 62 6c 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ble->pNext;.    
2dc11 61 73 73 65 72 74 28 20 64 62 32 20 29 3b 0a 20  assert( db2 );. 
2dc12 20 20 20 69 66 28 20 64 62 32 3d 3d 64 62 20 29     if( db2==db )
2dc13 7b 0a 20 20 20 20 20 20 70 52 65 74 20 3d 20 70  {.      pRet = p
2dc14 56 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 2d  VTable;.      p-
2dc15 3e 70 56 54 61 62 6c 65 20 3d 20 70 52 65 74 3b  >pVTable = pRet;
2dc16 0a 20 20 20 20 20 20 70 52 65 74 2d 3e 70 4e 65  .      pRet->pNe
2dc17 78 74 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  xt = 0;.    }els
2dc18 65 7b 0a 20 20 20 20 20 20 70 56 54 61 62 6c 65  e{.      pVTable
2dc19 2d 3e 70 4e 65 78 74 20 3d 20 64 62 32 2d 3e 70  ->pNext = db2->p
2dc1a 44 69 73 63 6f 6e 6e 65 63 74 3b 0a 20 20 20 20  Disconnect;.    
2dc1b 20 20 64 62 32 2d 3e 70 44 69 73 63 6f 6e 6e 65    db2->pDisconne
2dc1c 63 74 20 3d 20 70 56 54 61 62 6c 65 3b 0a 20 20  ct = pVTable;.  
2dc1d 20 20 7d 0a 20 20 20 20 70 56 54 61 62 6c 65 20    }.    pVTable 
2dc1e 3d 20 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20  = pNext;.  }..  
2dc1f 61 73 73 65 72 74 28 20 21 64 62 20 7c 7c 20 70  assert( !db || p
2dc20 52 65 74 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  Ret );.  return 
2dc21 70 52 65 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  pRet;.}.../*.** 
2dc22 44 69 73 63 6f 6e 6e 65 63 74 20 61 6c 6c 20 74  Disconnect all t
2dc23 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2dc24 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20   objects in the 
2dc25 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e  sqlite3.pDisconn
2dc26 65 63 74 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ect list..**.** 
2dc27 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61  This function ma
2dc28 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64  y only be called
2dc29 20 77 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65   when the mutexe
2dc2a 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2dc2b 68 20 61 6c 6c 0a 2a 2a 20 73 68 61 72 65 64 20  h all.** shared 
2dc2c 62 2d 74 72 65 65 20 64 61 74 61 62 61 73 65 73  b-tree databases
2dc2d 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 63 6f   opened using co
2dc2e 6e 6e 65 63 74 69 6f 6e 20 64 62 20 61 72 65 20  nnection db are 
2dc2f 68 65 6c 64 20 62 79 20 74 68 65 20 0a 2a 2a 20  held by the .** 
2dc30 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 69 73 20  caller. This is 
2dc31 64 6f 6e 65 20 74 6f 20 70 72 6f 74 65 63 74 20  done to protect 
2dc32 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73  the sqlite3.pDis
2dc33 63 6f 6e 6e 65 63 74 20 6c 69 73 74 2e 20 54 68  connect list. Th
2dc34 65 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 44 69  e.** sqlite3.pDi
2dc35 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20 69 73  sconnect list is
2dc36 20 61 63 63 65 73 73 65 64 20 6f 6e 6c 79 20 61   accessed only a
2dc37 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
2dc38 20 20 20 31 29 20 42 79 20 74 68 69 73 20 66 75     1) By this fu
2dc39 6e 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20  nction. In this 
2dc3a 63 61 73 65 2c 20 61 6c 6c 20 42 74 53 68 61 72  case, all BtShar
2dc3b 65 64 20 6d 75 74 65 78 65 73 20 61 6e 64 20 74  ed mutexes and t
2dc3c 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20  he mutex.**     
2dc3d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2dc3e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
2dc3f 6e 64 6c 65 20 69 74 73 65 6c 66 20 6d 75 73 74  ndle itself must
2dc40 20 62 65 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20   be held..**.** 
2dc41 20 20 32 29 20 42 79 20 66 75 6e 63 74 69 6f 6e    2) By function
2dc42 20 76 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 41   vtabDisconnectA
2dc43 6c 6c 28 29 2c 20 77 68 65 6e 20 69 74 20 61 64  ll(), when it ad
2dc44 64 73 20 61 20 56 54 61 62 6c 65 20 65 6e 74 72  ds a VTable entr
2dc45 79 20 74 6f 0a 2a 2a 20 20 20 20 20 20 74 68 65  y to.**      the
2dc46 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e   sqlite3.pDiscon
2dc47 6e 65 63 74 20 6c 69 73 74 2e 20 49 6e 20 74 68  nect list. In th
2dc48 69 73 20 63 61 73 65 20 65 69 74 68 65 72 20 74  is case either t
2dc49 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65  he BtShared mute
2dc4a 78 0a 2a 2a 20 20 20 20 20 20 61 73 73 6f 63 69  x.**      associ
2dc4b 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61  ated with the da
2dc4c 74 61 62 61 73 65 20 74 68 65 20 76 69 72 74 75  tabase the virtu
2dc4d 61 6c 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72  al table is stor
2dc4e 65 64 20 69 6e 20 69 73 20 68 65 6c 64 0a 2a 2a  ed in is held.**
2dc4f 20 20 20 20 20 20 6f 72 2c 20 69 66 20 74 68 65        or, if the
2dc50 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
2dc51 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 6e 6f  s stored in a no
2dc52 6e 2d 73 68 61 72 61 62 6c 65 20 64 61 74 61 62  n-sharable datab
2dc53 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20  ase, then.**    
2dc54 20 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68    the database h
2dc55 61 6e 64 6c 65 20 6d 75 74 65 78 20 69 73 20 68  andle mutex is h
2dc56 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 61 20  eld..**.** As a 
2dc57 72 65 73 75 6c 74 2c 20 61 20 73 71 6c 69 74 65  result, a sqlite
2dc58 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 63 61  3.pDisconnect ca
2dc59 6e 6e 6f 74 20 62 65 20 61 63 63 65 73 73 65 64  nnot be accessed
2dc5a 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 20   simultaneously 
2dc5b 0a 2a 2a 20 62 79 20 6d 75 6c 74 69 70 6c 65 20  .** by multiple 
2dc5c 74 68 72 65 61 64 73 2e 20 49 74 20 69 73 20 74  threads. It is t
2dc5d 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 53  hread-safe..*/.S
2dc5e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2dc5f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e  id sqlite3VtabUn
2dc60 6c 6f 63 6b 4c 69 73 74 28 73 71 6c 69 74 65 33  lockList(sqlite3
2dc61 20 2a 64 62 29 7b 0a 20 20 56 54 61 62 6c 65 20   *db){.  VTable 
2dc62 2a 70 20 3d 20 64 62 2d 3e 70 44 69 73 63 6f 6e  *p = db->pDiscon
2dc63 6e 65 63 74 3b 0a 20 20 64 62 2d 3e 70 44 69 73  nect;.  db->pDis
2dc64 63 6f 6e 6e 65 63 74 20 3d 20 30 3b 0a 0a 20 20  connect = 0;..  
2dc65 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
2dc66 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
2dc67 78 65 73 28 64 62 29 20 29 3b 0a 20 20 61 73 73  xes(db) );.  ass
2dc68 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
2dc69 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
2dc6a 78 29 20 29 3b 0a 0a 20 20 69 66 28 20 70 20 29  x) );..  if( p )
2dc6b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
2dc6c 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
2dc6d 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 64  ments(db);.    d
2dc6e 6f 20 7b 0a 20 20 20 20 20 20 56 54 61 62 6c 65  o {.      VTable
2dc6f 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65   *pNext = p->pNe
2dc70 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  xt;.      sqlite
2dc71 33 56 74 61 62 55 6e 6c 6f 63 6b 28 70 29 3b 0a  3VtabUnlock(p);.
2dc72 20 20 20 20 20 20 70 20 3d 20 70 4e 65 78 74 3b        p = pNext;
2dc73 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 70 20 29  .    }while( p )
2dc74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
2dc75 6c 65 61 72 20 61 6e 79 20 61 6e 64 20 61 6c 6c  lear any and all
2dc76 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 69   virtual-table i
2dc77 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
2dc78 74 68 65 20 54 61 62 6c 65 20 72 65 63 6f 72 64  the Table record
2dc79 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
2dc7a 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 66 6f 72  e is called, for
2dc7b 20 65 78 61 6d 70 6c 65 2c 20 6a 75 73 74 20 62   example, just b
2dc7c 65 66 6f 72 65 20 64 65 6c 65 74 69 6e 67 20 74  efore deleting t
2dc7d 68 65 20 54 61 62 6c 65 0a 2a 2a 20 72 65 63 6f  he Table.** reco
2dc7e 72 64 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20  rd..**.** Since 
2dc7f 69 74 20 69 73 20 61 20 76 69 72 74 75 61 6c 2d  it is a virtual-
2dc80 74 61 62 6c 65 2c 20 74 68 65 20 54 61 62 6c 65  table, the Table
2dc81 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
2dc82 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a  ins a pointer.**
2dc83 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20   to the head of 
2dc84 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  a linked list of
2dc85 20 56 54 61 62 6c 65 20 73 74 72 75 63 74 75 72   VTable structur
2dc86 65 73 2e 20 45 61 63 68 20 56 54 61 62 6c 65 20  es. Each VTable 
2dc87 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73  .** structure is
2dc88 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2dc89 20 61 20 73 69 6e 67 6c 65 20 73 71 6c 69 74 65   a single sqlite
2dc8a 33 2a 20 75 73 65 72 20 6f 66 20 74 68 65 20 73  3* user of the s
2dc8b 63 68 65 6d 61 2e 0a 2a 2a 20 54 68 65 20 72 65  chema..** The re
2dc8c 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66  ference count of
2dc8d 20 74 68 65 20 56 54 61 62 6c 65 20 73 74 72 75   the VTable stru
2dc8e 63 74 75 72 65 20 61 73 73 6f 63 69 61 74 65 64  cture associated
2dc8f 20 77 69 74 68 20 64 61 74 61 62 61 73 65 20 0a   with database .
2dc90 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62  ** connection db
2dc91 20 69 73 20 64 65 63 72 65 6d 65 6e 74 65 64 20   is decremented 
2dc92 69 6d 6d 65 64 69 61 74 65 6c 79 20 28 77 68 69  immediately (whi
2dc93 63 68 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 74  ch may lead to t
2dc94 68 65 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  he .** structure
2dc95 20 62 65 69 6e 67 20 78 44 69 73 63 6f 6e 6e 65   being xDisconne
2dc96 63 74 65 64 20 61 6e 64 20 66 72 65 65 29 2e 20  cted and free). 
2dc97 41 6e 79 20 6f 74 68 65 72 20 56 54 61 62 6c 65  Any other VTable
2dc98 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 69   structures.** i
2dc99 6e 20 74 68 65 20 6c 69 73 74 20 61 72 65 20 6d  n the list are m
2dc9a 6f 76 65 64 20 74 6f 20 74 68 65 20 73 71 6c 69  oved to the sqli
2dc9b 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20  te3.pDisconnect 
2dc9c 6c 69 73 74 20 6f 66 20 74 68 65 20 61 73 73 6f  list of the asso
2dc9d 63 69 61 74 65 64 20 0a 2a 2a 20 64 61 74 61 62  ciated .** datab
2dc9e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ase connection..
2dc9f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2dca0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2dca1 61 62 43 6c 65 61 72 28 54 61 62 6c 65 20 2a 70  abClear(Table *p
2dca2 29 7b 0a 20 20 76 74 61 62 44 69 73 63 6f 6e 6e  ){.  vtabDisconn
2dca3 65 63 74 41 6c 6c 28 30 2c 20 70 29 3b 0a 20 20  ectAll(0, p);.  
2dca4 69 66 28 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41  if( p->azModuleA
2dca5 72 67 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  rg ){.    int i;
2dca6 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2dca7 70 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 69  p->nModuleArg; i
2dca8 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
2dca9 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 4d 65  e3DbFree(p->dbMe
2dcaa 6d 2c 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  m, p->azModuleAr
2dcab 67 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20  g[i]);.    }.   
2dcac 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
2dcad 2d 3e 64 62 4d 65 6d 2c 20 70 2d 3e 61 7a 4d 6f  ->dbMem, p->azMo
2dcae 64 75 6c 65 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a  duleArg);.  }.}.
2dcaf 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
2dcb0 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74   module argument
2dcb1 20 74 6f 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f   to pTable->azMo
2dcb2 64 75 6c 65 41 72 67 5b 5d 2e 0a 2a 2a 20 54 68  duleArg[]..** Th
2dcb3 65 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20  e string is not 
2dcb4 63 6f 70 69 65 64 20 2d 20 74 68 65 20 70 6f 69  copied - the poi
2dcb5 6e 74 65 72 20 69 73 20 73 74 6f 72 65 64 2e 20  nter is stored. 
2dcb6 20 54 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 77   The.** string w
2dcb7 69 6c 6c 20 62 65 20 66 72 65 65 64 20 61 75 74  ill be freed aut
2dcb8 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20  omatically when 
2dcb9 74 68 65 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20  the table is.** 
2dcba 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74  deleted..*/.stat
2dcbb 69 63 20 76 6f 69 64 20 61 64 64 4d 6f 64 75 6c  ic void addModul
2dcbc 65 41 72 67 75 6d 65 6e 74 28 73 71 6c 69 74 65  eArgument(sqlite
2dcbd 33 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 54  3 *db, Table *pT
2dcbe 61 62 6c 65 2c 20 63 68 61 72 20 2a 7a 41 72 67  able, char *zArg
2dcbf 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 54 61  ){.  int i = pTa
2dcc0 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 2b  ble->nModuleArg+
2dcc1 2b 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 20  +;.  int nBytes 
2dcc2 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29  = sizeof(char *)
2dcc3 2a 28 31 2b 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64  *(1+pTable->nMod
2dcc4 75 6c 65 41 72 67 29 3b 0a 20 20 63 68 61 72 20  uleArg);.  char 
2dcc5 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 20  **azModuleArg;. 
2dcc6 20 61 7a 4d 6f 64 75 6c 65 41 72 67 20 3d 20 73   azModuleArg = s
2dcc7 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28  qlite3DbRealloc(
2dcc8 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f  db, pTable->azMo
2dcc9 64 75 6c 65 41 72 67 2c 20 6e 42 79 74 65 73 29  duleArg, nBytes)
2dcca 3b 0a 20 20 69 66 28 20 61 7a 4d 6f 64 75 6c 65  ;.  if( azModule
2dccb 41 72 67 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e  Arg==0 ){.    in
2dccc 74 20 6a 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30  t j;.    for(j=0
2dccd 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ; j<i; j++){.   
2dcce 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2dccf 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61 7a 4d  (db, pTable->azM
2dcd0 6f 64 75 6c 65 41 72 67 5b 6a 5d 29 3b 0a 20 20  oduleArg[j]);.  
2dcd1 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
2dcd2 62 46 72 65 65 28 64 62 2c 20 7a 41 72 67 29 3b  bFree(db, zArg);
2dcd3 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2dcd4 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61  ee(db, pTable->a
2dcd5 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20 20  zModuleArg);.   
2dcd6 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65   pTable->nModule
2dcd7 41 72 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  Arg = 0;.  }else
2dcd8 7b 0a 20 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72  {.    azModuleAr
2dcd9 67 5b 69 5d 20 3d 20 7a 41 72 67 3b 0a 20 20 20  g[i] = zArg;.   
2dcda 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 2b 31   azModuleArg[i+1
2dcdb 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61  ] = 0;.  }.  pTa
2dcdc 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  ble->azModuleArg
2dcdd 20 3d 20 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a   = azModuleArg;.
2dcde 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72  }../*.** The par
2dcdf 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
2dce0 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20 66  outine when it f
2dce1 69 72 73 74 20 73 65 65 73 20 61 20 43 52 45 41  irst sees a CREA
2dce2 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
2dce3 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  .** statement.  
2dce4 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20  The module name 
2dce5 68 61 73 20 62 65 65 6e 20 70 61 72 73 65 64 2c  has been parsed,
2dce6 20 62 75 74 20 74 68 65 20 6f 70 74 69 6f 6e 61   but the optiona
2dce7 6c 20 6c 69 73 74 0a 2a 2a 20 6f 66 20 70 61 72  l list.** of par
2dce8 61 6d 65 74 65 72 73 20 74 68 61 74 20 66 6f 6c  ameters that fol
2dce9 6c 6f 77 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e  low the module n
2dcea 61 6d 65 20 61 72 65 20 73 74 69 6c 6c 20 70 65  ame are still pe
2dceb 6e 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nding..*/.SQLITE
2dcec 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2dced 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 50 61  lite3VtabBeginPa
2dcee 72 73 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  rse(.  Parse *pP
2dcef 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
2dcf0 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2dcf1 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
2dcf2 65 31 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  e1,        /* Na
2dcf3 6d 65 20 6f 66 20 6e 65 77 20 74 61 62 6c 65 2c  me of new table,
2dcf4 20 6f 72 20 64 61 74 61 62 61 73 65 20 6e 61 6d   or database nam
2dcf5 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  e */.  Token *pN
2dcf6 61 6d 65 32 2c 20 20 20 20 20 20 20 20 2f 2a 20  ame2,        /* 
2dcf7 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74 61 62 6c  Name of new tabl
2dcf8 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54  e or NULL */.  T
2dcf9 6f 6b 65 6e 20 2a 70 4d 6f 64 75 6c 65 4e 61 6d  oken *pModuleNam
2dcfa 65 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  e    /* Name of 
2dcfb 74 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72 20 74  the module for t
2dcfc 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2dcfd 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62   */.){.  int iDb
2dcfe 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2dcff 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 74  * The database t
2dd00 68 65 20 74 61 62 6c 65 20 69 73 20 62 65 69 6e  he table is bein
2dd01 67 20 63 72 65 61 74 65 64 20 69 6e 20 2a 2f 0a  g created in */.
2dd02 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b    Table *pTable;
2dd03 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
2dd04 65 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ew virtual table
2dd05 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2dd06 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  b;          /* D
2dd07 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2dd08 6f 6e 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 33  on */..  sqlite3
2dd09 53 74 61 72 74 54 61 62 6c 65 28 70 50 61 72 73  StartTable(pPars
2dd0a 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65  e, pName1, pName
2dd0b 32 2c 20 30 2c 20 30 2c 20 31 2c 20 30 29 3b 0a  2, 0, 0, 1, 0);.
2dd0c 20 20 70 54 61 62 6c 65 20 3d 20 70 50 61 72 73    pTable = pPars
2dd0d 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20  e->pNewTable;.  
2dd0e 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 29 20  if( pTable==0 ) 
2dd0f 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
2dd10 28 20 30 3d 3d 70 54 61 62 6c 65 2d 3e 70 49 6e  ( 0==pTable->pIn
2dd11 64 65 78 20 29 3b 0a 0a 20 20 64 62 20 3d 20 70  dex );..  db = p
2dd12 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 44 62  Parse->db;.  iDb
2dd13 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2dd14 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
2dd15 6c 65 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  le->pSchema);.  
2dd16 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29  assert( iDb>=0 )
2dd17 3b 0a 0a 20 20 70 54 61 62 6c 65 2d 3e 74 61 62  ;..  pTable->tab
2dd18 46 6c 61 67 73 20 7c 3d 20 54 46 5f 56 69 72 74  Flags |= TF_Virt
2dd19 75 61 6c 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 6e  ual;.  pTable->n
2dd1a 4d 6f 64 75 6c 65 41 72 67 20 3d 20 30 3b 0a 20  ModuleArg = 0;. 
2dd1b 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65   addModuleArgume
2dd1c 6e 74 28 64 62 2c 20 70 54 61 62 6c 65 2c 20 73  nt(db, pTable, s
2dd1d 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
2dd1e 6b 65 6e 28 64 62 2c 20 70 4d 6f 64 75 6c 65 4e  ken(db, pModuleN
2dd1f 61 6d 65 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75  ame));.  addModu
2dd20 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70  leArgument(db, p
2dd21 54 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 44 62  Table, sqlite3Db
2dd22 53 74 72 44 75 70 28 64 62 2c 20 64 62 2d 3e 61  StrDup(db, db->a
2dd23 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 29 3b  Db[iDb].zName));
2dd24 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75  .  addModuleArgu
2dd25 6d 65 6e 74 28 64 62 2c 20 70 54 61 62 6c 65 2c  ment(db, pTable,
2dd26 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
2dd27 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61  (db, pTable->zNa
2dd28 6d 65 29 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  me));.  pParse->
2dd29 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28  sNameToken.n = (
2dd2a 69 6e 74 29 28 26 70 4d 6f 64 75 6c 65 4e 61 6d  int)(&pModuleNam
2dd2b 65 2d 3e 7a 5b 70 4d 6f 64 75 6c 65 4e 61 6d 65  e->z[pModuleName
2dd2c 2d 3e 6e 5d 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a  ->n] - pName1->z
2dd2d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
2dd2e 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
2dd2f 41 54 49 4f 4e 0a 20 20 2f 2a 20 43 72 65 61 74  ATION.  /* Creat
2dd30 69 6e 67 20 61 20 76 69 72 74 75 61 6c 20 74 61  ing a virtual ta
2dd31 62 6c 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  ble invokes the 
2dd32 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61  authorization ca
2dd33 6c 6c 62 61 63 6b 20 74 77 69 63 65 2e 0a 20 20  llback twice..  
2dd34 2a 2a 20 54 68 65 20 66 69 72 73 74 20 69 6e 76  ** The first inv
2dd35 6f 63 61 74 69 6f 6e 2c 20 74 6f 20 6f 62 74 61  ocation, to obta
2dd36 69 6e 20 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f  in permission to
2dd37 20 49 4e 53 45 52 54 20 61 20 72 6f 77 20 69 6e   INSERT a row in
2dd38 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69  to the.  ** sqli
2dd39 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2c  te_master table,
2dd3a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
2dd3b 6e 20 6d 61 64 65 20 62 79 20 73 71 6c 69 74 65  n made by sqlite
2dd3c 33 53 74 61 72 74 54 61 62 6c 65 28 29 2e 0a 20  3StartTable().. 
2dd3d 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63   ** The second c
2dd3e 61 6c 6c 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70  all, to obtain p
2dd3f 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 63 72 65  ermission to cre
2dd40 61 74 65 20 74 68 65 20 74 61 62 6c 65 2c 20 69  ate the table, i
2dd41 73 20 6d 61 64 65 20 6e 6f 77 2e 0a 20 20 2a 2f  s made now..  */
2dd42 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 61  .  if( pTable->a
2dd43 7a 4d 6f 64 75 6c 65 41 72 67 20 29 7b 0a 20 20  zModuleArg ){.  
2dd44 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65    sqlite3AuthChe
2dd45 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
2dd46 45 5f 43 52 45 41 54 45 5f 56 54 41 42 4c 45 2c  E_CREATE_VTABLE,
2dd47 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20   pTable->zName, 
2dd48 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61  .            pTa
2dd49 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  ble->azModuleArg
2dd4a 5b 30 5d 2c 20 70 50 61 72 73 65 2d 3e 64 62 2d  [0], pParse->db-
2dd4b 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
2dd4c 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  ;.  }.#endif.}..
2dd4d 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2dd4e 6e 65 20 74 61 6b 65 73 20 74 68 65 20 6d 6f 64  ne takes the mod
2dd4f 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 74 68 61  ule argument tha
2dd50 74 20 68 61 73 20 62 65 65 6e 20 61 63 63 75 6d  t has been accum
2dd51 75 6c 61 74 69 6e 67 0a 2a 2a 20 69 6e 20 70 50  ulating.** in pP
2dd52 61 72 73 65 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64  arse->zArg[] and
2dd53 20 61 70 70 65 6e 64 73 20 69 74 20 74 6f 20 74   appends it to t
2dd54 68 65 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d  he list of argum
2dd55 65 6e 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 76  ents on the.** v
2dd56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72  irtual table cur
2dd57 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
2dd58 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 70 50 61  struction in pPa
2dd59 72 73 65 2d 3e 70 54 61 62 6c 65 2e 0a 2a 2f 0a  rse->pTable..*/.
2dd5a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 41  static void addA
2dd5b 72 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 50 61  rgumentToVtab(Pa
2dd5c 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
2dd5d 69 66 28 20 70 50 61 72 73 65 2d 3e 73 41 72 67  if( pParse->sArg
2dd5e 2e 7a 20 26 26 20 41 4c 57 41 59 53 28 70 50 61  .z && ALWAYS(pPa
2dd5f 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 20  rse->pNewTable) 
2dd60 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
2dd61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68  r *z = (const ch
2dd62 61 72 2a 29 70 50 61 72 73 65 2d 3e 73 41 72 67  ar*)pParse->sArg
2dd63 2e 7a 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20  .z;.    int n = 
2dd64 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 6e 3b 0a  pParse->sArg.n;.
2dd65 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
2dd66 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2dd67 20 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d    addModuleArgum
2dd68 65 6e 74 28 64 62 2c 20 70 50 61 72 73 65 2d 3e  ent(db, pParse->
2dd69 70 4e 65 77 54 61 62 6c 65 2c 20 73 71 6c 69 74  pNewTable, sqlit
2dd6a 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
2dd6b 7a 2c 20 6e 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  z, n));.  }.}../
2dd6c 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20  *.** The parser 
2dd6d 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
2dd6e 6e 65 20 61 66 74 65 72 20 74 68 65 20 43 52 45  ne after the CRE
2dd6f 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
2dd70 45 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68  E statement.** h
2dd71 61 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74 65  as been complete
2dd72 6c 79 20 70 61 72 73 65 64 2e 0a 2a 2f 0a 53 51  ly parsed..*/.SQ
2dd73 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2dd74 64 20 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e  d sqlite3VtabFin
2dd75 69 73 68 50 61 72 73 65 28 50 61 72 73 65 20 2a  ishParse(Parse *
2dd76 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70  pParse, Token *p
2dd77 45 6e 64 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  End){.  Table *p
2dd78 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  Tab = pParse->pN
2dd79 65 77 54 61 62 6c 65 3b 20 20 2f 2a 20 54 68 65  ewTable;  /* The
2dd7a 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e   table being con
2dd7b 73 74 72 75 63 74 65 64 20 2a 2f 0a 20 20 73 71  structed */.  sq
2dd7c 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2dd7d 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20  se->db;         
2dd7e 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2dd7f 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20  connection */.. 
2dd80 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72   if( pTab==0 ) r
2dd81 65 74 75 72 6e 3b 0a 20 20 61 64 64 41 72 67 75  eturn;.  addArgu
2dd82 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73  mentToVtab(pPars
2dd83 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41  e);.  pParse->sA
2dd84 72 67 2e 7a 20 3d 20 30 3b 0a 20 20 69 66 28 20  rg.z = 0;.  if( 
2dd85 70 54 61 62 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67  pTab->nModuleArg
2dd86 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 0a  <1 ) return;.  .
2dd87 20 20 2f 2a 20 49 66 20 74 68 65 20 43 52 45 41    /* If the CREA
2dd88 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
2dd89 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65   statement is be
2dd8a 69 6e 67 20 65 6e 74 65 72 65 64 20 66 6f 72 20  ing entered for 
2dd8b 74 68 65 0a 20 20 2a 2a 20 66 69 72 73 74 20 74  the.  ** first t
2dd8c 69 6d 65 20 28 69 6e 20 6f 74 68 65 72 20 77 6f  ime (in other wo
2dd8d 72 64 73 20 69 66 20 74 68 65 20 76 69 72 74 75  rds if the virtu
2dd8e 61 6c 20 74 61 62 6c 65 20 69 73 20 61 63 74 75  al table is actu
2dd8f 61 6c 6c 79 20 62 65 69 6e 67 0a 20 20 2a 2a 20  ally being.  ** 
2dd90 63 72 65 61 74 65 64 20 6e 6f 77 20 69 6e 73 74  created now inst
2dd91 65 61 64 20 6f 66 20 6a 75 73 74 20 62 65 69 6e  ead of just bein
2dd92 67 20 72 65 61 64 20 6f 75 74 20 6f 66 20 73 71  g read out of sq
2dd93 6c 69 74 65 5f 6d 61 73 74 65 72 29 20 74 68 65  lite_master) the
2dd94 6e 0a 20 20 2a 2a 20 64 6f 20 61 64 64 69 74 69  n.  ** do additi
2dd95 6f 6e 61 6c 20 69 6e 69 74 69 61 6c 69 7a 61 74  onal initializat
2dd96 69 6f 6e 20 77 6f 72 6b 20 61 6e 64 20 73 74 6f  ion work and sto
2dd97 72 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  re the statement
2dd98 20 74 65 78 74 0a 20 20 2a 2a 20 69 6e 20 74 68   text.  ** in th
2dd99 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
2dd9a 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  table..  */.  if
2dd9b 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
2dd9c 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53   ){.    char *zS
2dd9d 74 6d 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  tmt;.    char *z
2dd9e 57 68 65 72 65 3b 0a 20 20 20 20 69 6e 74 20 69  Where;.    int i
2dd9f 44 62 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b  Db;.    Vdbe *v;
2dda0 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65  ..    /* Compute
2dda1 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65   the complete te
2dda2 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  xt of the CREATE
2dda3 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73   VIRTUAL TABLE s
2dda4 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20  tatement */.    
2dda5 69 66 28 20 70 45 6e 64 20 29 7b 0a 20 20 20 20  if( pEnd ){.    
2dda6 20 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54    pParse->sNameT
2dda7 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e 74 29 28 70  oken.n = (int)(p
2dda8 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 72 73 65 2d  End->z - pParse-
2dda9 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 20 2b  >sNameToken.z) +
2ddaa 20 70 45 6e 64 2d 3e 6e 3b 0a 20 20 20 20 7d 0a   pEnd->n;.    }.
2ddab 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69      zStmt = sqli
2ddac 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2ddad 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
2ddae 41 42 4c 45 20 25 54 22 2c 20 26 70 50 61 72 73  ABLE %T", &pPars
2ddaf 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 29 3b 0a  e->sNameToken);.
2ddb0 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f 74 20 66  .    /* A slot f
2ddb1 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 68 61  or the record ha
2ddb2 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61  s already been a
2ddb3 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20  llocated in the 
2ddb4 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4d  .    ** SQLITE_M
2ddb5 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 57 65  ASTER table.  We
2ddb6 20 6a 75 73 74 20 6e 65 65 64 20 74 6f 20 75 70   just need to up
2ddb7 64 61 74 65 20 74 68 61 74 20 73 6c 6f 74 20 77  date that slot w
2ddb8 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a 2a 20 74  ith all.    ** t
2ddb9 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77  he information w
2ddba 65 27 76 65 20 63 6f 6c 6c 65 63 74 65 64 2e 20  e've collected. 
2ddbb 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20   .    **.    ** 
2ddbc 54 68 65 20 56 4d 20 72 65 67 69 73 74 65 72 20  The VM register 
2ddbd 6e 75 6d 62 65 72 20 70 50 61 72 73 65 2d 3e 72  number pParse->r
2ddbe 65 67 52 6f 77 69 64 20 68 6f 6c 64 73 20 74 68  egRowid holds th
2ddbf 65 20 72 6f 77 69 64 20 6f 66 20 61 6e 0a 20 20  e rowid of an.  
2ddc0 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68    ** entry in th
2ddc1 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
2ddc2 74 61 62 6c 65 20 74 68 74 20 77 61 73 20 63 72  table tht was cr
2ddc3 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 76  eated for this v
2ddc4 74 61 62 0a 20 20 20 20 2a 2a 20 62 79 20 73 71  tab.    ** by sq
2ddc5 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
2ddc6 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 44  )..    */.    iD
2ddc7 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2ddc8 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
2ddc9 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
2ddca 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
2ddcb 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
2ddcc 20 20 22 55 50 44 41 54 45 20 25 51 2e 25 73 20    "UPDATE %Q.%s 
2ddcd 22 0a 20 20 20 20 20 20 20 20 20 22 53 45 54 20  ".         "SET 
2ddce 74 79 70 65 3d 27 74 61 62 6c 65 27 2c 20 6e 61  type='table', na
2ddcf 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 6d 65 3d  me=%Q, tbl_name=
2ddd0 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d 30 2c 20  %Q, rootpage=0, 
2ddd1 73 71 6c 3d 25 51 20 22 0a 20 20 20 20 20 20 20  sql=%Q ".       
2ddd2 22 57 48 45 52 45 20 72 6f 77 69 64 3d 23 25 64  "WHERE rowid=#%d
2ddd3 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62  ",.      db->aDb
2ddd4 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48  [iDb].zName, SCH
2ddd5 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a  EMA_TABLE(iDb),.
2ddd6 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
2ddd7 65 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  e,.      pTab->z
2ddd8 4e 61 6d 65 2c 0a 20 20 20 20 20 20 7a 53 74 6d  Name,.      zStm
2ddd9 74 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  t,.      pParse-
2ddda 3e 72 65 67 52 6f 77 69 64 0a 20 20 20 20 29 3b  >regRowid.    );
2dddb 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2dddc 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 20  ee(db, zStmt);. 
2dddd 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
2ddde 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
2dddf 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
2dde0 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
2dde1 44 62 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  Db);..    sqlite
2dde2 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2dde3 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 30 29 3b  P_Expire, 0, 0);
2dde4 0a 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73 71  .    zWhere = sq
2dde5 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2dde6 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70 54   "name='%q'", pT
2dde7 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
2dde8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2dde9 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68  4(v, OP_ParseSch
2ddea 65 6d 61 2c 20 69 44 62 2c 20 31 2c 20 30 2c 20  ema, iDb, 1, 0, 
2ddeb 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d  zWhere, P4_DYNAM
2ddec 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
2dded 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2ddee 5f 56 43 72 65 61 74 65 2c 20 69 44 62 2c 20 30  _VCreate, iDb, 0
2ddef 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20  , 0, .          
2ddf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2ddf1 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69  Tab->zName, sqli
2ddf2 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61 62  te3Strlen30(pTab
2ddf3 2d 3e 7a 4e 61 6d 65 29 20 2b 20 31 29 3b 0a 20  ->zName) + 1);. 
2ddf4 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61   }..  /* If we a
2ddf5 72 65 20 72 65 72 65 61 64 69 6e 67 20 74 68 65  re rereading the
2ddf6 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
2ddf7 61 62 6c 65 20 63 72 65 61 74 65 20 74 68 65 20  able create the 
2ddf8 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 72  in-memory.  ** r
2ddf9 65 63 6f 72 64 20 6f 66 20 74 68 65 20 74 61 62  ecord of the tab
2ddfa 6c 65 2e 20 54 68 65 20 78 43 6f 6e 6e 65 63 74  le. The xConnect
2ddfb 28 29 20 6d 65 74 68 6f 64 20 69 73 20 6e 6f 74  () method is not
2ddfc 20 63 61 6c 6c 65 64 20 75 6e 74 69 6c 0a 20 20   called until.  
2ddfd 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  ** the first tim
2ddfe 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  e the virtual ta
2ddff 62 6c 65 20 69 73 20 75 73 65 64 20 69 6e 20 61  ble is used in a
2de00 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  n SQL statement.
2de01 20 54 68 69 73 0a 20 20 2a 2a 20 61 6c 6c 6f 77   This.  ** allow
2de02 73 20 61 20 73 63 68 65 6d 61 20 74 68 61 74 20  s a schema that 
2de03 63 6f 6e 74 61 69 6e 73 20 76 69 72 74 75 61 6c  contains virtual
2de04 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 6c 6f   tables to be lo
2de05 61 64 65 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a  aded before.  **
2de06 20 74 68 65 20 72 65 71 75 69 72 65 64 20 76 69   the required vi
2de07 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c  rtual table impl
2de08 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 72 65 20  ementations are 
2de09 72 65 67 69 73 74 65 72 65 64 2e 20 20 2a 2f 0a  registered.  */.
2de0a 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 54 61 62    else {.    Tab
2de0b 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 53 63  le *pOld;.    Sc
2de0c 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20  hema *pSchema = 
2de0d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20  pTab->pSchema;. 
2de0e 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2de0f 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61  Name = pTab->zNa
2de10 6d 65 3b 0a 20 20 20 20 69 6e 74 20 6e 4e 61 6d  me;.    int nNam
2de11 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  e = sqlite3Strle
2de12 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  n30(zName);.    
2de13 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 48 61  pOld = sqlite3Ha
2de14 73 68 49 6e 73 65 72 74 28 26 70 53 63 68 65 6d  shInsert(&pSchem
2de15 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 4e 61 6d  a->tblHash, zNam
2de16 65 2c 20 6e 4e 61 6d 65 2c 20 70 54 61 62 29 3b  e, nName, pTab);
2de17 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 20 29 7b  .    if( pOld ){
2de18 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
2de19 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
2de1a 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 3d     assert( pTab=
2de1b 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20 4d 61 6c  =pOld );  /* Mal
2de1c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61  loc must have fa
2de1d 69 6c 65 64 20 69 6e 73 69 64 65 20 48 61 73 68  iled inside Hash
2de1e 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20 20 20 20  Insert() */.    
2de1f 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
2de20 20 20 20 20 70 53 63 68 65 6d 61 2d 3e 64 62 20      pSchema->db 
2de21 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2de22 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61    pParse->pNewTa
2de23 62 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  ble = 0;.  }.}..
2de24 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  /*.** The parser
2de25 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2de26 69 6e 65 20 77 68 65 6e 20 69 74 20 73 65 65 73  ine when it sees
2de27 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e   the first token
2de28 0a 2a 2a 20 6f 66 20 61 6e 20 61 72 67 75 6d 65  .** of an argume
2de29 6e 74 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65  nt to the module
2de2a 20 6e 61 6d 65 20 69 6e 20 61 20 43 52 45 41 54   name in a CREAT
2de2b 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  E VIRTUAL TABLE 
2de2c 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51  statement..*/.SQ
2de2d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2de2e 64 20 73 71 6c 69 74 65 33 56 74 61 62 41 72 67  d sqlite3VtabArg
2de2f 49 6e 69 74 28 50 61 72 73 65 20 2a 70 50 61 72  Init(Parse *pPar
2de30 73 65 29 7b 0a 20 20 61 64 64 41 72 67 75 6d 65  se){.  addArgume
2de31 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73 65 29  ntToVtab(pParse)
2de32 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41 72 67  ;.  pParse->sArg
2de33 2e 7a 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65  .z = 0;.  pParse
2de34 2d 3e 73 41 72 67 2e 6e 20 3d 20 30 3b 0a 7d 0a  ->sArg.n = 0;.}.
2de35 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65  ./*.** The parse
2de36 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
2de37 74 69 6e 65 20 66 6f 72 20 65 61 63 68 20 74 6f  tine for each to
2de38 6b 65 6e 20 61 66 74 65 72 20 74 68 65 20 66 69  ken after the fi
2de39 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20  rst token.** in 
2de3a 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  an argument to t
2de3b 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69  he module name i
2de3c 6e 20 61 20 43 52 45 41 54 45 20 56 49 52 54 55  n a CREATE VIRTU
2de3d 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  AL TABLE stateme
2de3e 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
2de3f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2de40 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28  e3VtabArgExtend(
2de41 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
2de42 6f 6b 65 6e 20 2a 70 29 7b 0a 20 20 54 6f 6b 65  oken *p){.  Toke
2de43 6e 20 2a 70 41 72 67 20 3d 20 26 70 50 61 72 73  n *pArg = &pPars
2de44 65 2d 3e 73 41 72 67 3b 0a 20 20 69 66 28 20 70  e->sArg;.  if( p
2de45 41 72 67 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20  Arg->z==0 ){.   
2de46 20 70 41 72 67 2d 3e 7a 20 3d 20 70 2d 3e 7a 3b   pArg->z = p->z;
2de47 0a 20 20 20 20 70 41 72 67 2d 3e 6e 20 3d 20 70  .    pArg->n = p
2de48 2d 3e 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ->n;.  }else{.  
2de49 20 20 61 73 73 65 72 74 28 70 41 72 67 2d 3e 7a    assert(pArg->z
2de4a 20 3c 20 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 41   < p->z);.    pA
2de4b 72 67 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 26 70  rg->n = (int)(&p
2de4c 2d 3e 7a 5b 70 2d 3e 6e 5d 20 2d 20 70 41 72 67  ->z[p->n] - pArg
2de4d 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ->z);.  }.}../*.
2de4e 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 76 69 72 74  ** Invoke a virt
2de4f 75 61 6c 20 74 61 62 6c 65 20 63 6f 6e 73 74 72  ual table constr
2de50 75 63 74 6f 72 20 28 65 69 74 68 65 72 20 78 43  uctor (either xC
2de51 72 65 61 74 65 20 6f 72 20 78 43 6f 6e 6e 65 63  reate or xConnec
2de52 74 29 2e 20 54 68 65 0a 2a 2a 20 70 6f 69 6e 74  t). The.** point
2de53 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  er to the functi
2de54 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 69 73 20  on to invoke is 
2de55 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 6f  passed as the fo
2de56 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 0a 2a  urth parameter.*
2de57 2a 20 74 6f 20 74 68 69 73 20 70 72 6f 63 65 64  * to this proced
2de58 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
2de59 6e 74 20 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74  nt vtabCallConst
2de5a 72 75 63 74 6f 72 28 0a 20 20 73 71 6c 69 74 65  ructor(.  sqlite
2de5b 33 20 2a 64 62 2c 20 0a 20 20 54 61 62 6c 65 20  3 *db, .  Table 
2de5c 2a 70 54 61 62 2c 0a 20 20 4d 6f 64 75 6c 65 20  *pTab,.  Module 
2de5d 2a 70 4d 6f 64 2c 0a 20 20 69 6e 74 20 28 2a 78  *pMod,.  int (*x
2de5e 43 6f 6e 73 74 72 75 63 74 29 28 73 71 6c 69 74  Construct)(sqlit
2de5f 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  e3*,void*,int,co
2de60 6e 73 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a 2c  nst char*const*,
2de61 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 2a 2c 63  sqlite3_vtab**,c
2de62 68 61 72 2a 2a 29 2c 0a 20 20 63 68 61 72 20 2a  har**),.  char *
2de63 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 56 54 61 62  *pzErr.){.  VTab
2de64 6c 65 20 2a 70 56 54 61 62 6c 65 3b 0a 20 20 69  le *pVTable;.  i
2de65 6e 74 20 72 63 3b 0a 20 20 63 6f 6e 73 74 20 63  nt rc;.  const c
2de66 68 61 72 20 2a 63 6f 6e 73 74 2a 61 7a 41 72 67  har *const*azArg
2de67 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
2de68 63 6f 6e 73 74 2a 29 70 54 61 62 2d 3e 61 7a 4d  const*)pTab->azM
2de69 6f 64 75 6c 65 41 72 67 3b 0a 20 20 69 6e 74 20  oduleArg;.  int 
2de6a 6e 41 72 67 20 3d 20 70 54 61 62 2d 3e 6e 4d 6f  nArg = pTab->nMo
2de6b 64 75 6c 65 41 72 67 3b 0a 20 20 63 68 61 72 20  duleArg;.  char 
2de6c 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 63 68 61  *zErr = 0;.  cha
2de6d 72 20 2a 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 3d  r *zModuleName =
2de6e 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2de6f 64 62 2c 20 22 25 73 22 2c 20 70 54 61 62 2d 3e  db, "%s", pTab->
2de70 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20 21  zName);..  if( !
2de71 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 29 7b 0a 20  zModuleName ){. 
2de72 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2de73 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 70  _NOMEM;.  }..  p
2de74 56 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  VTable = sqlite3
2de75 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
2de76 20 73 69 7a 65 6f 66 28 56 54 61 62 6c 65 29 29   sizeof(VTable))
2de77 3b 0a 20 20 69 66 28 20 21 70 56 54 61 62 6c 65  ;.  if( !pVTable
2de78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
2de79 62 46 72 65 65 28 64 62 2c 20 7a 4d 6f 64 75 6c  bFree(db, zModul
2de7a 65 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75  eName);.    retu
2de7b 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2de7c 0a 20 20 7d 0a 20 20 70 56 54 61 62 6c 65 2d 3e  .  }.  pVTable->
2de7d 64 62 20 3d 20 64 62 3b 0a 20 20 70 56 54 61 62  db = db;.  pVTab
2de7e 6c 65 2d 3e 70 4d 6f 64 20 3d 20 70 4d 6f 64 3b  le->pMod = pMod;
2de7f 0a 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ..  assert( !db-
2de80 3e 70 56 54 61 62 20 29 3b 0a 20 20 61 73 73 65  >pVTab );.  asse
2de81 72 74 28 20 78 43 6f 6e 73 74 72 75 63 74 20 29  rt( xConstruct )
2de82 3b 0a 20 20 64 62 2d 3e 70 56 54 61 62 20 3d 20  ;.  db->pVTab = 
2de83 70 54 61 62 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f  pTab;..  /* Invo
2de84 6b 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ke the virtual t
2de85 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72  able constructor
2de86 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69   */.  (void)sqli
2de87 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
2de88 3b 0a 20 20 72 63 20 3d 20 78 43 6f 6e 73 74 72  ;.  rc = xConstr
2de89 75 63 74 28 64 62 2c 20 70 4d 6f 64 2d 3e 70 41  uct(db, pMod->pA
2de8a 75 78 2c 20 6e 41 72 67 2c 20 61 7a 41 72 67 2c  ux, nArg, azArg,
2de8b 20 26 70 56 54 61 62 6c 65 2d 3e 70 56 74 61 62   &pVTable->pVtab
2de8c 2c 20 26 7a 45 72 72 29 3b 0a 20 20 28 76 6f 69  , &zErr);.  (voi
2de8d 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
2de8e 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d  n(db);.  if( rc=
2de8f 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 20  =SQLITE_NOMEM ) 
2de90 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2de91 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 53 51 4c   = 1;..  if( SQL
2de92 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20  ITE_OK!=rc ){.  
2de93 20 20 69 66 28 20 7a 45 72 72 3d 3d 30 20 29 7b    if( zErr==0 ){
2de94 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  .      *pzErr = 
2de95 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2de96 62 2c 20 22 76 74 61 62 6c 65 20 63 6f 6e 73 74  b, "vtable const
2de97 72 75 63 74 6f 72 20 66 61 69 6c 65 64 3a 20 25  ructor failed: %
2de98 73 22 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29  s", zModuleName)
2de99 3b 0a 20 20 20 20 7d 65 6c 73 65 20 7b 0a 20 20  ;.    }else {.  
2de9a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c      *pzErr = sql
2de9b 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2de9c 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 20  "%s", zErr);.   
2de9d 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2de9e 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  (db, zErr);.    
2de9f 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
2dea0 72 65 65 28 64 62 2c 20 70 56 54 61 62 6c 65 29  ree(db, pVTable)
2dea1 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 41 4c  ;.  }else if( AL
2dea2 57 41 59 53 28 70 56 54 61 62 6c 65 2d 3e 70 56  WAYS(pVTable->pV
2dea3 74 61 62 29 20 29 7b 0a 20 20 20 20 2f 2a 20 4a  tab) ){.    /* J
2dea4 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f 66 20  ustification of 
2dea5 41 4c 57 41 59 53 28 29 3a 20 20 41 20 63 6f 72  ALWAYS():  A cor
2dea6 72 65 63 74 20 76 74 61 62 20 63 6f 6e 73 74 72  rect vtab constr
2dea7 75 63 74 6f 72 20 6d 75 73 74 20 61 6c 6c 6f 63  uctor must alloc
2dea8 61 74 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 73  ate.    ** the s
2dea9 71 6c 69 74 65 33 5f 76 74 61 62 20 6f 62 6a 65  qlite3_vtab obje
2deaa 63 74 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  ct if successful
2deab 2e 20 20 2a 2f 0a 20 20 20 20 70 56 54 61 62 6c  .  */.    pVTabl
2deac 65 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c  e->pVtab->pModul
2dead 65 20 3d 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c  e = pMod->pModul
2deae 65 3b 0a 20 20 20 20 70 56 54 61 62 6c 65 2d 3e  e;.    pVTable->
2deaf 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 69 66  nRef = 1;.    if
2deb0 28 20 64 62 2d 3e 70 56 54 61 62 20 29 7b 0a 20  ( db->pVTab ){. 
2deb1 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2deb2 2a 7a 46 6f 72 6d 61 74 20 3d 20 22 76 74 61 62  *zFormat = "vtab
2deb3 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 64  le constructor d
2deb4 69 64 20 6e 6f 74 20 64 65 63 6c 61 72 65 20 73  id not declare s
2deb5 63 68 65 6d 61 3a 20 25 73 22 3b 0a 20 20 20 20  chema: %s";.    
2deb6 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
2deb7 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46  e3MPrintf(db, zF
2deb8 6f 72 6d 61 74 2c 20 70 54 61 62 2d 3e 7a 4e 61  ormat, pTab->zNa
2deb9 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  me);.      sqlit
2deba 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 70 56 54  e3VtabUnlock(pVT
2debb 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 72 63 20  able);.      rc 
2debc 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2debd 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2debe 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20   int iCol;.     
2debf 20 2f 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e   /* If everythin
2dec0 67 20 77 65 6e 74 20 61 63 63 6f 72 64 69 6e 67  g went according
2dec1 20 74 6f 20 70 6c 61 6e 2c 20 6c 69 6e 6b 20 74   to plan, link t
2dec2 68 65 20 6e 65 77 20 56 54 61 62 6c 65 20 73 74  he new VTable st
2dec3 72 75 63 74 75 72 65 0a 20 20 20 20 20 20 2a 2a  ructure.      **
2dec4 20 69 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64   into the linked
2dec5 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 20   list headed by 
2dec6 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 2e 20 54  pTab->pVTable. T
2dec7 68 65 6e 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68  hen loop through
2dec8 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 63   the .      ** c
2dec9 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61  olumns of the ta
2deca 62 6c 65 20 74 6f 20 73 65 65 20 69 66 20 61 6e  ble to see if an
2decb 79 20 6f 66 20 74 68 65 6d 20 63 6f 6e 74 61 69  y of them contai
2decc 6e 20 74 68 65 20 74 6f 6b 65 6e 20 22 68 69 64  n the token "hid
2decd 64 65 6e 22 2e 0a 20 20 20 20 20 20 2a 2a 20 49  den"..      ** I
2dece 66 20 73 6f 2c 20 73 65 74 20 74 68 65 20 43 6f  f so, set the Co
2decf 6c 75 6d 6e 2e 69 73 48 69 64 64 65 6e 20 66 6c  lumn.isHidden fl
2ded0 61 67 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68  ag and remove th
2ded1 65 20 74 6f 6b 65 6e 20 66 72 6f 6d 0a 20 20 20  e token from.   
2ded2 20 20 20 2a 2a 20 74 68 65 20 74 79 70 65 20 73     ** the type s
2ded3 74 72 69 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 20  tring.  */.     
2ded4 20 70 56 54 61 62 6c 65 2d 3e 70 4e 65 78 74 20   pVTable->pNext 
2ded5 3d 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 3b  = pTab->pVTable;
2ded6 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 56 54  .      pTab->pVT
2ded7 61 62 6c 65 20 3d 20 70 56 54 61 62 6c 65 3b 0a  able = pVTable;.
2ded8 0a 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d  .      for(iCol=
2ded9 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  0; iCol<pTab->nC
2deda 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20  ol; iCol++){.   
2dedb 20 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65       char *zType
2dedc 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   = pTab->aCol[iC
2dedd 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20 20 20  ol].zType;.     
2dede 20 20 20 69 6e 74 20 6e 54 79 70 65 3b 0a 20 20     int nType;.  
2dedf 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b        int i = 0;
2dee0 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 7a 54  .        if( !zT
2dee1 79 70 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ype ) continue;.
2dee2 20 20 20 20 20 20 20 20 6e 54 79 70 65 20 3d 20          nType = 
2dee3 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2dee4 7a 54 79 70 65 29 3b 0a 20 20 20 20 20 20 20 20  zType);.        
2dee5 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
2dee6 43 6d 70 28 22 68 69 64 64 65 6e 22 2c 20 7a 54  Cmp("hidden", zT
2dee7 79 70 65 2c 20 36 29 7c 7c 28 7a 54 79 70 65 5b  ype, 6)||(zType[
2dee8 36 5d 20 26 26 20 7a 54 79 70 65 5b 36 5d 21 3d  6] && zType[6]!=
2dee9 27 20 27 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ' ') ){.        
2deea 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 79    for(i=0; i<nTy
2deeb 70 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pe; i++){.      
2deec 20 20 20 20 20 20 69 66 28 20 28 30 3d 3d 73 71        if( (0==sq
2deed 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 22 20  lite3StrNICmp(" 
2deee 68 69 64 64 65 6e 22 2c 20 26 7a 54 79 70 65 5b  hidden", &zType[
2deef 69 5d 2c 20 37 29 29 0a 20 20 20 20 20 20 20 20  i], 7)).        
2def0 20 20 20 20 20 26 26 20 28 7a 54 79 70 65 5b 69       && (zType[i
2def1 2b 37 5d 3d 3d 27 5c 30 27 20 7c 7c 20 7a 54 79  +7]=='\0' || zTy
2def2 70 65 5b 69 2b 37 5d 3d 3d 27 20 27 29 0a 20 20  pe[i+7]==' ').  
2def3 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20            ){.   
2def4 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a             i++;.
2def5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
2def6 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
2def7 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
2def8 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2def9 20 69 66 28 20 69 3c 6e 54 79 70 65 20 29 7b 0a   if( i<nType ){.
2defa 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b            int j;
2defb 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e  .          int n
2defc 44 65 6c 20 3d 20 36 20 2b 20 28 7a 54 79 70 65  Del = 6 + (zType
2defd 5b 69 2b 36 5d 20 3f 20 31 20 3a 20 30 29 3b 0a  [i+6] ? 1 : 0);.
2defe 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d            for(j=
2deff 69 3b 20 28 6a 2b 6e 44 65 6c 29 3c 3d 6e 54 79  i; (j+nDel)<=nTy
2df00 70 65 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  pe; j++){.      
2df01 20 20 20 20 20 20 7a 54 79 70 65 5b 6a 5d 20 3d        zType[j] =
2df02 20 7a 54 79 70 65 5b 6a 2b 6e 44 65 6c 5d 3b 0a   zType[j+nDel];.
2df03 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2df04 20 20 20 20 20 20 69 66 28 20 7a 54 79 70 65 5b        if( zType[
2df05 69 5d 3d 3d 27 5c 30 27 20 26 26 20 69 3e 30 20  i]=='\0' && i>0 
2df06 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
2df07 73 73 65 72 74 28 7a 54 79 70 65 5b 69 2d 31 5d  ssert(zType[i-1]
2df08 3d 3d 27 20 27 29 3b 0a 20 20 20 20 20 20 20 20  ==' ');.        
2df09 20 20 20 20 7a 54 79 70 65 5b 69 2d 31 5d 20 3d      zType[i-1] =
2df0a 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20   '\0';.         
2df0b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 54 61   }.          pTa
2df0c 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73  b->aCol[iCol].is
2df0d 48 69 64 64 65 6e 20 3d 20 31 3b 0a 20 20 20 20  Hidden = 1;.    
2df0e 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2df0f 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74    }.  }..  sqlit
2df10 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 6f  e3DbFree(db, zMo
2df11 64 75 6c 65 4e 61 6d 65 29 3b 0a 20 20 64 62 2d  duleName);.  db-
2df12 3e 70 56 54 61 62 20 3d 20 30 3b 0a 20 20 72 65  >pVTab = 0;.  re
2df13 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2df14 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2df15 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68  is invoked by th
2df16 65 20 70 61 72 73 65 72 20 74 6f 20 63 61 6c 6c  e parser to call
2df17 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20   the xConnect() 
2df18 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65  method.** of the
2df19 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70   virtual table p
2df1a 54 61 62 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  Tab. If an error
2df1b 20 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f   occurs, an erro
2df1c 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
2df1d 65 64 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72  ed .** and an er
2df1e 72 6f 72 20 6c 65 66 74 20 69 6e 20 70 50 61 72  ror left in pPar
2df1f 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63  se..**.** This c
2df20 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  all is a no-op i
2df21 66 20 74 61 62 6c 65 20 70 54 61 62 20 69 73 20  f table pTab is 
2df22 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74 61  not a virtual ta
2df23 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
2df24 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2df25 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63  e3VtabCallConnec
2df26 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
2df27 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20   Table *pTab){. 
2df28 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2df29 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 63 6f 6e  Parse->db;.  con
2df2a 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 3b 0a 20  st char *zMod;. 
2df2b 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20   Module *pMod;. 
2df2c 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65   int rc;..  asse
2df2d 72 74 28 20 70 54 61 62 20 29 3b 0a 20 20 69 66  rt( pTab );.  if
2df2e 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67  ( (pTab->tabFlag
2df2f 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 3d  s & TF_Virtual)=
2df30 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 65 74  =0 || sqlite3Get
2df31 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29  VTable(db, pTab)
2df32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2df33 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
2df34 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 72   /* Locate the r
2df35 65 71 75 69 72 65 64 20 76 69 72 74 75 61 6c 20  equired virtual 
2df36 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  table module */.
2df37 20 20 7a 4d 6f 64 20 3d 20 70 54 61 62 2d 3e 61    zMod = pTab->a
2df38 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a 20  zModuleArg[0];. 
2df39 20 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 2a   pMod = (Module*
2df3a 29 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64  )sqlite3HashFind
2df3b 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a  (&db->aModule, z
2df3c 4d 6f 64 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  Mod, sqlite3Strl
2df3d 65 6e 33 30 28 7a 4d 6f 64 29 29 3b 0a 0a 20 20  en30(zMod));..  
2df3e 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a 20 20 20  if( !pMod ){.   
2df3f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f   const char *zMo
2df40 64 75 6c 65 20 3d 20 70 54 61 62 2d 3e 61 7a 4d  dule = pTab->azM
2df41 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a 20 20 20  oduleArg[0];.   
2df42 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2df43 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
2df44 68 20 6d 6f 64 75 6c 65 3a 20 25 73 22 2c 20 7a  h module: %s", z
2df45 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 72 63 20  Module);.    rc 
2df46 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2df47 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61    }else{.    cha
2df48 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 20  r *zErr = 0;.   
2df49 20 72 63 20 3d 20 76 74 61 62 43 61 6c 6c 43 6f   rc = vtabCallCo
2df4a 6e 73 74 72 75 63 74 6f 72 28 64 62 2c 20 70 54  nstructor(db, pT
2df4b 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d 3e  ab, pMod, pMod->
2df4c 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6e 6e 65 63  pModule->xConnec
2df4d 74 2c 20 26 7a 45 72 72 29 3b 0a 20 20 20 20 69  t, &zErr);.    i
2df4e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2df4f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2df50 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2df51 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20  , "%s", zErr);. 
2df52 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2df53 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29  DbFree(db, zErr)
2df54 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
2df55 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  rc;.}../*.** Add
2df56 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2df57 6c 65 20 70 56 54 61 62 20 74 6f 20 74 68 65 20  le pVTab to the 
2df58 61 72 72 61 79 20 73 71 6c 69 74 65 33 2e 61 56  array sqlite3.aV
2df59 54 72 61 6e 73 5b 5d 2e 0a 2a 2f 0a 73 74 61 74  Trans[]..*/.stat
2df5a 69 63 20 69 6e 74 20 61 64 64 54 6f 56 54 72 61  ic int addToVTra
2df5b 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ns(sqlite3 *db, 
2df5c 56 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b 0a  VTable *pVTab){.
2df5d 20 20 63 6f 6e 73 74 20 69 6e 74 20 41 52 52 41    const int ARRA
2df5e 59 5f 49 4e 43 52 20 3d 20 35 3b 0a 0a 20 20 2f  Y_INCR = 5;..  /
2df5f 2a 20 47 72 6f 77 20 74 68 65 20 73 71 6c 69 74  * Grow the sqlit
2df60 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79  e3.aVTrans array
2df61 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a   if required */.
2df62 20 20 69 66 28 20 28 64 62 2d 3e 6e 56 54 72 61    if( (db->nVTra
2df63 6e 73 25 41 52 52 41 59 5f 49 4e 43 52 29 3d 3d  ns%ARRAY_INCR)==
2df64 30 20 29 7b 0a 20 20 20 20 56 54 61 62 6c 65 20  0 ){.    VTable 
2df65 2a 2a 61 56 54 72 61 6e 73 3b 0a 20 20 20 20 69  **aVTrans;.    i
2df66 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65  nt nBytes = size
2df67 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  of(sqlite3_vtab 
2df68 2a 29 20 2a 20 28 64 62 2d 3e 6e 56 54 72 61 6e  *) * (db->nVTran
2df69 73 20 2b 20 41 52 52 41 59 5f 49 4e 43 52 29 3b  s + ARRAY_INCR);
2df6a 0a 20 20 20 20 61 56 54 72 61 6e 73 20 3d 20 73  .    aVTrans = s
2df6b 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28  qlite3DbRealloc(
2df6c 64 62 2c 20 28 76 6f 69 64 20 2a 29 64 62 2d 3e  db, (void *)db->
2df6d 61 56 54 72 61 6e 73 2c 20 6e 42 79 74 65 73 29  aVTrans, nBytes)
2df6e 3b 0a 20 20 20 20 69 66 28 20 21 61 56 54 72 61  ;.    if( !aVTra
2df6f 6e 73 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ns ){.      retu
2df70 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2df71 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65  .    }.    memse
2df72 74 28 26 61 56 54 72 61 6e 73 5b 64 62 2d 3e 6e  t(&aVTrans[db->n
2df73 56 54 72 61 6e 73 5d 2c 20 30 2c 20 73 69 7a 65  VTrans], 0, size
2df74 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  of(sqlite3_vtab 
2df75 2a 29 2a 41 52 52 41 59 5f 49 4e 43 52 29 3b 0a  *)*ARRAY_INCR);.
2df76 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20      db->aVTrans 
2df77 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 7d 0a 0a  = aVTrans;.  }..
2df78 20 20 2f 2a 20 41 64 64 20 70 56 74 61 62 20 74    /* Add pVtab t
2df79 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71 6c  o the end of sql
2df7a 69 74 65 33 2e 61 56 54 72 61 6e 73 20 2a 2f 0a  ite3.aVTrans */.
2df7b 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 64 62    db->aVTrans[db
2df7c 2d 3e 6e 56 54 72 61 6e 73 2b 2b 5d 20 3d 20 70  ->nVTrans++] = p
2df7d 56 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 56  VTab;.  sqlite3V
2df7e 74 61 62 4c 6f 63 6b 28 70 56 54 61 62 29 3b 0a  tabLock(pVTab);.
2df7f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2df80 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
2df81 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e  s function is in
2df82 76 6f 6b 65 64 20 62 79 20 74 68 65 20 76 64 62  voked by the vdb
2df83 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 43  e to call the xC
2df84 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20  reate method.** 
2df85 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
2df86 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62 20  able named zTab 
2df87 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e  in database iDb.
2df88 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72   .**.** If an er
2df89 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 7a 45  ror occurs, *pzE
2df8a 72 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  rr is set to poi
2df8b 6e 74 20 61 6e 20 61 6e 20 45 6e 67 6c 69 73 68  nt an an English
2df8c 20 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 64 65 73   language.** des
2df8d 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20  cription of the 
2df8e 65 72 72 6f 72 20 61 6e 64 20 61 6e 20 53 51 4c  error and an SQL
2df8f 49 54 45 5f 58 58 58 20 65 72 72 6f 72 20 63 6f  ITE_XXX error co
2df90 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
2df91 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  ** In this case 
2df92 74 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  the caller must 
2df93 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 72  call sqlite3DbFr
2df94 65 65 28 64 62 2c 20 29 20 6f 6e 20 2a 70 7a 45  ee(db, ) on *pzE
2df95 72 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rr..*/.SQLITE_PR
2df96 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2df97 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28  3VtabCallCreate(
2df98 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
2df99 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   iDb, const char
2df9a 20 2a 7a 54 61 62 2c 20 63 68 61 72 20 2a 2a 70   *zTab, char **p
2df9b 7a 45 72 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  zErr){.  int rc 
2df9c 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54  = SQLITE_OK;.  T
2df9d 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 4d 6f  able *pTab;.  Mo
2df9e 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 63 6f  dule *pMod;.  co
2df9f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 3b 0a  nst char *zMod;.
2dfa0 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
2dfa1 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
2dfa2 54 61 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  Tab, db->aDb[iDb
2dfa3 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65  ].zName);.  asse
2dfa4 72 74 28 20 70 54 61 62 20 26 26 20 28 70 54 61  rt( pTab && (pTa
2dfa5 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
2dfa6 5f 56 69 72 74 75 61 6c 29 21 3d 30 20 26 26 20  _Virtual)!=0 && 
2dfa7 21 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 20 29  !pTab->pVTable )
2dfa8 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  ;..  /* Locate t
2dfa9 68 65 20 72 65 71 75 69 72 65 64 20 76 69 72 74  he required virt
2dfaa 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
2dfab 20 2a 2f 0a 20 20 7a 4d 6f 64 20 3d 20 70 54 61   */.  zMod = pTa
2dfac 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30  b->azModuleArg[0
2dfad 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64  ];.  pMod = (Mod
2dfae 75 6c 65 2a 29 73 71 6c 69 74 65 33 48 61 73 68  ule*)sqlite3Hash
2dfaf 46 69 6e 64 28 26 64 62 2d 3e 61 4d 6f 64 75 6c  Find(&db->aModul
2dfb0 65 2c 20 7a 4d 6f 64 2c 20 73 71 6c 69 74 65 33  e, zMod, sqlite3
2dfb1 53 74 72 6c 65 6e 33 30 28 7a 4d 6f 64 29 29 3b  Strlen30(zMod));
2dfb2 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6d 6f  ..  /* If the mo
2dfb3 64 75 6c 65 20 68 61 73 20 62 65 65 6e 20 72 65  dule has been re
2dfb4 67 69 73 74 65 72 65 64 20 61 6e 64 20 69 6e 63  gistered and inc
2dfb5 6c 75 64 65 73 20 61 20 43 72 65 61 74 65 20 6d  ludes a Create m
2dfb6 65 74 68 6f 64 2c 20 0a 20 20 2a 2a 20 69 6e 76  ethod, .  ** inv
2dfb7 6f 6b 65 20 69 74 20 6e 6f 77 2e 20 49 66 20 74  oke it now. If t
2dfb8 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 6e 6f  he module has no
2dfb9 74 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  t been registere
2dfba 64 2c 20 72 65 74 75 72 6e 20 61 6e 20 0a 20 20  d, return an .  
2dfbb 2a 2a 20 65 72 72 6f 72 2e 20 4f 74 68 65 72 77  ** error. Otherw
2dfbc 69 73 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e  ise, do nothing.
2dfbd 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4d 6f  .  */.  if( !pMo
2dfbe 64 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20  d ){.    *pzErr 
2dfbf 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2dfc0 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 6d 6f  (db, "no such mo
2dfc1 64 75 6c 65 3a 20 25 73 22 2c 20 7a 4d 6f 64 29  dule: %s", zMod)
2dfc2 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
2dfc3 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65  E_ERROR;.  }else
2dfc4 7b 0a 20 20 20 20 72 63 20 3d 20 76 74 61 62 43  {.    rc = vtabC
2dfc5 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 64  allConstructor(d
2dfc6 62 2c 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70  b, pTab, pMod, p
2dfc7 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43  Mod->pModule->xC
2dfc8 72 65 61 74 65 2c 20 70 7a 45 72 72 29 3b 0a 20  reate, pzErr);. 
2dfc9 20 7d 0a 0a 20 20 2f 2a 20 4a 75 73 74 69 66 69   }..  /* Justifi
2dfca 63 61 74 69 6f 6e 20 6f 66 20 41 4c 57 41 59 53  cation of ALWAYS
2dfcb 28 29 3a 20 20 54 68 65 20 78 43 6f 6e 73 74 72  ():  The xConstr
2dfcc 75 63 74 6f 72 20 6d 65 74 68 6f 64 20 69 73 20  uctor method is 
2dfcd 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a  required to.  **
2dfce 20 63 72 65 61 74 65 20 61 20 76 61 6c 69 64 20   create a valid 
2dfcf 73 71 6c 69 74 65 33 5f 76 74 61 62 20 69 66 20  sqlite3_vtab if 
2dfd0 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  it returns SQLIT
2dfd1 45 5f 4f 4b 2e 20 2a 2f 0a 20 20 69 66 28 20 72  E_OK. */.  if( r
2dfd2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
2dfd3 41 4c 57 41 59 53 28 73 71 6c 69 74 65 33 47 65  ALWAYS(sqlite3Ge
2dfd4 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62  tVTable(db, pTab
2dfd5 29 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  )) ){.      rc =
2dfd6 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64 62 2c   addToVTrans(db,
2dfd7 20 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c   sqlite3GetVTabl
2dfd8 65 28 64 62 2c 20 70 54 61 62 29 29 3b 0a 20 20  e(db, pTab));.  
2dfd9 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
2dfda 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
2dfdb 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
2dfdc 6f 20 73 65 74 20 74 68 65 20 73 63 68 65 6d 61  o set the schema
2dfdd 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61   of a virtual ta
2dfde 62 6c 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79  ble.  It is only
2dfdf 0a 2a 2a 20 76 61 6c 69 64 20 74 6f 20 63 61 6c  .** valid to cal
2dfe0 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  l this function 
2dfe1 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20  from within the 
2dfe2 78 43 72 65 61 74 65 28 29 20 6f 72 20 78 43 6f  xCreate() or xCo
2dfe3 6e 6e 65 63 74 28 29 20 6f 66 20 61 0a 2a 2a 20  nnect() of a.** 
2dfe4 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
2dfe5 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dule..*/.SQLITE_
2dfe6 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2dfe7 64 65 63 6c 61 72 65 5f 76 74 61 62 28 73 71 6c  declare_vtab(sql
2dfe8 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
2dfe9 63 68 61 72 20 2a 7a 43 72 65 61 74 65 54 61 62  char *zCreateTab
2dfea 6c 65 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  le){.  Parse *pP
2dfeb 61 72 73 65 3b 0a 0a 20 20 69 6e 74 20 72 63 20  arse;..  int rc 
2dfec 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54  = SQLITE_OK;.  T
2dfed 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 63 68  able *pTab;.  ch
2dfee 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a 20  ar *zErr = 0;.. 
2dfef 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2dff0 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2dff1 0a 20 20 70 54 61 62 20 3d 20 64 62 2d 3e 70 56  .  pTab = db->pV
2dff2 54 61 62 3b 0a 20 20 69 66 28 20 21 70 54 61 62  Tab;.  if( !pTab
2dff3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2dff4 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
2dff5 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20 20  MISUSE, 0);.    
2dff6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
2dff7 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
2dff8 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2dff9 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
2dffa 61 73 73 65 72 74 28 20 28 70 54 61 62 2d 3e 74  assert( (pTab->t
2dffb 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72  abFlags & TF_Vir
2dffc 74 75 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 70  tual)!=0 );..  p
2dffd 50 61 72 73 65 20 3d 20 73 71 6c 69 74 65 33 53  Parse = sqlite3S
2dffe 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64 62  tackAllocZero(db
2dfff 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72 73 65  , sizeof(*pParse
2e000 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  ));.  if( pParse
2e001 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
2e002 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2e003 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 72 73  }else{.    pPars
2e004 65 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 20 3d  e->declareVtab =
2e005 20 31 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   1;.    pParse->
2e006 64 62 20 3d 20 64 62 3b 0a 20 20 0a 20 20 20 20  db = db;.  .    
2e007 69 66 28 20 0a 20 20 20 20 20 20 20 20 53 51 4c  if( .        SQL
2e008 49 54 45 5f 4f 4b 20 3d 3d 20 73 71 6c 69 74 65  ITE_OK == sqlite
2e009 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72 73  3RunParser(pPars
2e00a 65 2c 20 7a 43 72 65 61 74 65 54 61 62 6c 65 2c  e, zCreateTable,
2e00b 20 26 7a 45 72 72 29 20 26 26 20 0a 20 20 20 20   &zErr) && .    
2e00c 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77      pParse->pNew
2e00d 54 61 62 6c 65 20 26 26 20 0a 20 20 20 20 20 20  Table && .      
2e00e 20 20 21 70 50 61 72 73 65 2d 3e 70 4e 65 77 54    !pParse->pNewT
2e00f 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20 26 26  able->pSelect &&
2e010 20 0a 20 20 20 20 20 20 20 20 28 70 50 61 72 73   .        (pPars
2e011 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 74 61  e->pNewTable->ta
2e012 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
2e013 75 61 6c 29 3d 3d 30 0a 20 20 20 20 29 7b 0a 20  ual)==0.    ){. 
2e014 20 20 20 20 20 69 66 28 20 21 70 54 61 62 2d 3e       if( !pTab->
2e015 61 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  aCol ){.        
2e016 70 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 70 50 61  pTab->aCol = pPa
2e017 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e  rse->pNewTable->
2e018 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 54  aCol;.        pT
2e019 61 62 2d 3e 6e 43 6f 6c 20 3d 20 70 50 61 72 73  ab->nCol = pPars
2e01a 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e 43  e->pNewTable->nC
2e01b 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72  ol;.        pPar
2e01c 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e  se->pNewTable->n
2e01d 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  Col = 0;.       
2e01e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2e01f 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20  le->aCol = 0;.  
2e020 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 2d 3e      }.      db->
2e021 70 56 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 7d  pVTab = 0;.    }
2e022 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 73 71   else {.      sq
2e023 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
2e024 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 7a 45 72  QLITE_ERROR, zEr
2e025 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  r);.      sqlite
2e026 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
2e027 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
2e028 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
2e029 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 65  }.    pParse->de
2e02a 63 6c 61 72 65 56 74 61 62 20 3d 20 30 3b 0a 20  clareVtab = 0;. 
2e02b 20 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65   .    if( pParse
2e02c 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 20  ->pVdbe ){.     
2e02d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61   sqlite3VdbeFina
2e02e 6c 69 7a 65 28 70 50 61 72 73 65 2d 3e 70 56 64  lize(pParse->pVd
2e02f 62 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  be);.    }.    s
2e030 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
2e031 65 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  e(pParse->pNewTa
2e032 62 6c 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ble);.    sqlite
2e033 33 53 74 61 63 6b 46 72 65 65 28 64 62 2c 20 70  3StackFree(db, p
2e034 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 61  Parse);.  }..  a
2e035 73 73 65 72 74 28 20 28 72 63 26 30 78 66 66 29  ssert( (rc&0xff)
2e036 3d 3d 72 63 20 29 3b 0a 20 20 72 63 20 3d 20 73  ==rc );.  rc = s
2e037 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
2e038 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  , rc);.  sqlite3
2e039 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
2e03a 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
2e03b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
2e03c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2e03d 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 76  invoked by the v
2e03e 64 62 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  dbe to call the 
2e03f 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a  xDestroy method.
2e040 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  ** of the virtua
2e041 6c 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54  l table named zT
2e042 61 62 20 69 6e 20 64 61 74 61 62 61 73 65 20 69  ab in database i
2e043 44 62 2e 20 54 68 69 73 20 6f 63 63 75 72 73 0a  Db. This occurs.
2e044 2a 2a 20 77 68 65 6e 20 61 20 44 52 4f 50 20 54  ** when a DROP T
2e045 41 42 4c 45 20 69 73 20 6d 65 6e 74 69 6f 6e 65  ABLE is mentione
2e046 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61  d..**.** This ca
2e047 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  ll is a no-op if
2e048 20 7a 54 61 62 20 69 73 20 6e 6f 74 20 61 20 76   zTab is not a v
2e049 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f  irtual table..*/
2e04a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2e04b 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43  int sqlite3VtabC
2e04c 61 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c 69 74  allDestroy(sqlit
2e04d 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c  e3 *db, int iDb,
2e04e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
2e04f 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  b){.  int rc = S
2e050 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c  QLITE_OK;.  Tabl
2e051 65 20 2a 70 54 61 62 3b 0a 0a 20 20 70 54 61 62  e *pTab;..  pTab
2e052 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
2e053 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 64 62  ble(db, zTab, db
2e054 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
2e055 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  );.  if( ALWAYS(
2e056 70 54 61 62 21 3d 30 20 26 26 20 70 54 61 62 2d  pTab!=0 && pTab-
2e057 3e 70 56 54 61 62 6c 65 21 3d 30 29 20 29 7b 0a  >pVTable!=0) ){.
2e058 20 20 20 20 56 54 61 62 6c 65 20 2a 70 20 3d 20      VTable *p = 
2e059 76 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 41 6c  vtabDisconnectAl
2e05a 6c 28 64 62 2c 20 70 54 61 62 29 3b 0a 0a 20 20  l(db, pTab);..  
2e05b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61    rc = sqlite3Sa
2e05c 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
2e05d 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
2e05e 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 63  ITE_OK );.    rc
2e05f 20 3d 20 70 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64   = p->pMod->pMod
2e060 75 6c 65 2d 3e 78 44 65 73 74 72 6f 79 28 70 2d  ule->xDestroy(p-
2e061 3e 70 56 74 61 62 29 3b 0a 20 20 20 20 28 76 6f  >pVtab);.    (vo
2e062 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
2e063 4f 6e 28 64 62 29 3b 0a 0a 20 20 20 20 2f 2a 20  On(db);..    /* 
2e064 52 65 6d 6f 76 65 20 74 68 65 20 73 71 6c 69 74  Remove the sqlit
2e065 65 33 5f 76 74 61 62 2a 20 66 72 6f 6d 20 74 68  e3_vtab* from th
2e066 65 20 61 56 54 72 61 6e 73 5b 5d 20 61 72 72 61  e aVTrans[] arra
2e067 79 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65  y, if applicable
2e068 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
2e069 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2e06a 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d     assert( pTab-
2e06b 3e 70 56 54 61 62 6c 65 3d 3d 70 20 26 26 20 70  >pVTable==p && p
2e06c 2d 3e 70 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20  ->pNext==0 );.  
2e06d 20 20 20 20 70 2d 3e 70 56 74 61 62 20 3d 20 30      p->pVtab = 0
2e06e 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 56  ;.      pTab->pV
2e06f 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 20  Table = 0;.     
2e070 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f   sqlite3VtabUnlo
2e071 63 6b 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ck(p);.    }.  }
2e072 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
2e073 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
2e074 63 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 65 69  ction invokes ei
2e075 74 68 65 72 20 74 68 65 20 78 52 6f 6c 6c 62 61  ther the xRollba
2e076 63 6b 20 6f 72 20 78 43 6f 6d 6d 69 74 20 6d 65  ck or xCommit me
2e077 74 68 6f 64 0a 2a 2a 20 6f 66 20 65 61 63 68 20  thod.** of each 
2e078 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
2e079 61 62 6c 65 73 20 69 6e 20 74 68 65 20 73 71 6c  ables in the sql
2e07a 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72  ite3.aVTrans arr
2e07b 61 79 2e 20 54 68 65 20 6d 65 74 68 6f 64 0a 2a  ay. The method.*
2e07c 2a 20 63 61 6c 6c 65 64 20 69 73 20 69 64 65 6e  * called is iden
2e07d 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73 65  tified by the se
2e07e 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22  cond argument, "
2e07f 6f 66 66 73 65 74 22 2c 20 77 68 69 63 68 20 69  offset", which i
2e080 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20  s.** the offset 
2e081 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 20 74 6f  of the method to
2e082 20 63 61 6c 6c 20 69 6e 20 74 68 65 20 73 71 6c   call in the sql
2e083 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73 74 72 75  ite3_module stru
2e084 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cture..**.** The
2e085 20 61 72 72 61 79 20 69 73 20 63 6c 65 61 72 65   array is cleare
2e086 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67  d after invoking
2e087 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 2e 20   the callbacks. 
2e088 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2e089 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28 73 71  callFinaliser(sq
2e08a 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f  lite3 *db, int o
2e08b 66 66 73 65 74 29 7b 0a 20 20 69 6e 74 20 69 3b  ffset){.  int i;
2e08c 0a 20 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61  .  if( db->aVTra
2e08d 6e 73 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  ns ){.    for(i=
2e08e 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73  0; i<db->nVTrans
2e08f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 56 54  ; i++){.      VT
2e090 61 62 6c 65 20 2a 70 56 54 61 62 20 3d 20 64 62  able *pVTab = db
2e091 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3b 0a 20 20  ->aVTrans[i];.  
2e092 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
2e093 20 2a 70 20 3d 20 70 56 54 61 62 2d 3e 70 56 74   *p = pVTab->pVt
2e094 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20  ab;.      if( p 
2e095 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 28  ){.        int (
2e096 2a 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  *x)(sqlite3_vtab
2e097 20 2a 29 3b 0a 20 20 20 20 20 20 20 20 78 20 3d   *);.        x =
2e098 20 2a 28 69 6e 74 20 28 2a 2a 29 28 73 71 6c 69   *(int (**)(sqli
2e099 74 65 33 5f 76 74 61 62 20 2a 29 29 28 28 63 68  te3_vtab *))((ch
2e09a 61 72 20 2a 29 70 2d 3e 70 4d 6f 64 75 6c 65 20  ar *)p->pModule 
2e09b 2b 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 20  + offset);.     
2e09c 20 20 20 69 66 28 20 78 20 29 20 78 28 70 29 3b     if( x ) x(p);
2e09d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
2e09e 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
2e09f 28 70 56 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20  (pVTab);.    }. 
2e0a0 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2e0a1 28 64 62 2c 20 64 62 2d 3e 61 56 54 72 61 6e 73  (db, db->aVTrans
2e0a2 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 56 54 72 61  );.    db->nVTra
2e0a3 6e 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e  ns = 0;.    db->
2e0a4 61 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 7d  aVTrans = 0;.  }
2e0a5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65  .}../*.** Invoke
2e0a6 20 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f   the xSync metho
2e0a7 64 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c  d of all virtual
2e0a8 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 73   tables in the s
2e0a9 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 0a 2a  qlite3.aVTrans.*
2e0aa 2a 20 61 72 72 61 79 2e 20 52 65 74 75 72 6e 20  * array. Return 
2e0ab 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66  the error code f
2e0ac 6f 72 20 74 68 65 20 66 69 72 73 74 20 65 72 72  or the first err
2e0ad 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 2c 20  or that occurs, 
2e0ae 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  or.** SQLITE_OK 
2e0af 69 66 20 61 6c 6c 20 78 53 79 6e 63 20 6f 70 65  if all xSync ope
2e0b0 72 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63  rations are succ
2e0b1 65 73 73 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 53 65  essful..**.** Se
2e0b2 74 20 2a 70 7a 45 72 72 6d 73 67 20 74 6f 20 70  t *pzErrmsg to p
2e0b3 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
2e0b4 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
2e0b5 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 20 0a  released using .
2e0b6 2a 2a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  ** sqlite3DbFree
2e0b7 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e  () containing an
2e0b8 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20   error message, 
2e0b9 69 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c 61  if one is availa
2e0ba 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
2e0bb 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2e0bc 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69 74  e3VtabSync(sqlit
2e0bd 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 70  e3 *db, char **p
2e0be 7a 45 72 72 6d 73 67 29 7b 0a 20 20 69 6e 74 20  zErrmsg){.  int 
2e0bf 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  i;.  int rc = SQ
2e0c0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72  LITE_OK;.  int r
2e0c1 63 73 61 66 65 74 79 3b 0a 20 20 56 54 61 62 6c  csafety;.  VTabl
2e0c2 65 20 2a 2a 61 56 54 72 61 6e 73 20 3d 20 64 62  e **aVTrans = db
2e0c3 2d 3e 61 56 54 72 61 6e 73 3b 0a 0a 20 20 72 63  ->aVTrans;..  rc
2e0c4 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 79   = sqlite3Safety
2e0c5 4f 66 66 28 64 62 29 3b 0a 20 20 64 62 2d 3e 61  Off(db);.  db->a
2e0c6 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 66 6f  VTrans = 0;.  fo
2e0c7 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
2e0c8 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 56  E_OK && i<db->nV
2e0c9 54 72 61 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20  Trans; i++){.   
2e0ca 20 69 6e 74 20 28 2a 78 29 28 73 71 6c 69 74 65   int (*x)(sqlite
2e0cb 33 5f 76 74 61 62 20 2a 29 3b 0a 20 20 20 20 73  3_vtab *);.    s
2e0cc 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
2e0cd 61 62 20 3d 20 61 56 54 72 61 6e 73 5b 69 5d 2d  ab = aVTrans[i]-
2e0ce 3e 70 56 74 61 62 3b 0a 20 20 20 20 69 66 28 20  >pVtab;.    if( 
2e0cf 70 56 74 61 62 20 26 26 20 28 78 20 3d 20 70 56  pVtab && (x = pV
2e0d0 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 53  tab->pModule->xS
2e0d1 79 6e 63 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ync)!=0 ){.     
2e0d2 20 72 63 20 3d 20 78 28 70 56 74 61 62 29 3b 0a   rc = x(pVtab);.
2e0d3 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2e0d4 72 65 65 28 64 62 2c 20 2a 70 7a 45 72 72 6d 73  ree(db, *pzErrms
2e0d5 67 29 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72  g);.      *pzErr
2e0d6 6d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72  msg = pVtab->zEr
2e0d7 72 4d 73 67 3b 0a 20 20 20 20 20 20 70 56 74 61  rMsg;.      pVta
2e0d8 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  b->zErrMsg = 0;.
2e0d9 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62 2d 3e      }.  }.  db->
2e0da 61 56 54 72 61 6e 73 20 3d 20 61 56 54 72 61 6e  aVTrans = aVTran
2e0db 73 3b 0a 20 20 72 63 73 61 66 65 74 79 20 3d 20  s;.  rcsafety = 
2e0dc 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
2e0dd 64 62 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  db);..  if( rc==
2e0de 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2e0df 20 72 63 20 3d 20 72 63 73 61 66 65 74 79 3b 0a   rc = rcsafety;.
2e0e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
2e0e1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65  .}../*.** Invoke
2e0e2 20 74 68 65 20 78 52 6f 6c 6c 62 61 63 6b 20 6d   the xRollback m
2e0e3 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69 72  ethod of all vir
2e0e4 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 74  tual tables in t
2e0e5 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 61  he .** sqlite3.a
2e0e6 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20 54 68  VTrans array. Th
2e0e7 65 6e 20 63 6c 65 61 72 20 74 68 65 20 61 72 72  en clear the arr
2e0e8 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51  ay itself..*/.SQ
2e0e9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2e0ea 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c   sqlite3VtabRoll
2e0eb 62 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62  back(sqlite3 *db
2e0ec 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69 73  ){.  callFinalis
2e0ed 65 72 28 64 62 2c 20 6f 66 66 73 65 74 6f 66 28  er(db, offsetof(
2e0ee 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c 78  sqlite3_module,x
2e0ef 52 6f 6c 6c 62 61 63 6b 29 29 3b 0a 20 20 72 65  Rollback));.  re
2e0f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2e0f1 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20  }../*.** Invoke 
2e0f2 74 68 65 20 78 43 6f 6d 6d 69 74 20 6d 65 74 68  the xCommit meth
2e0f3 6f 64 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61  od of all virtua
2e0f4 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  l tables in the 
2e0f5 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 61 56 54 72  .** sqlite3.aVTr
2e0f6 61 6e 73 20 61 72 72 61 79 2e 20 54 68 65 6e 20  ans array. Then 
2e0f7 63 6c 65 61 72 20 74 68 65 20 61 72 72 61 79 20  clear the array 
2e0f8 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54  itself..*/.SQLIT
2e0f9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2e0fa 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28  lite3VtabCommit(
2e0fb 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
2e0fc 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28 64 62  callFinaliser(db
2e0fd 2c 20 6f 66 66 73 65 74 6f 66 28 73 71 6c 69 74  , offsetof(sqlit
2e0fe 65 33 5f 6d 6f 64 75 6c 65 2c 78 43 6f 6d 6d 69  e3_module,xCommi
2e0ff 74 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  t));.  return SQ
2e100 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
2e101 2a 20 49 66 20 74 68 65 20 76 69 72 74 75 61 6c  * If the virtual
2e102 20 74 61 62 6c 65 20 70 56 74 61 62 20 73 75 70   table pVtab sup
2e103 70 6f 72 74 73 20 74 68 65 20 74 72 61 6e 73 61  ports the transa
2e104 63 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 0a  ction interface.
2e105 2a 2a 20 28 78 42 65 67 69 6e 2f 78 52 6f 6c 6c  ** (xBegin/xRoll
2e106 62 61 63 6b 2f 78 43 6f 6d 6d 69 74 20 61 6e 64  back/xCommit and
2e107 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 78 53 79 6e   optionally xSyn
2e108 63 29 20 61 6e 64 20 61 20 74 72 61 6e 73 61 63  c) and a transac
2e109 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63  tion is.** not c
2e10a 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 69  urrently open, i
2e10b 6e 76 6f 6b 65 20 74 68 65 20 78 42 65 67 69 6e  nvoke the xBegin
2e10c 20 6d 65 74 68 6f 64 20 6e 6f 77 2e 0a 2a 2a 0a   method now..**.
2e10d 2a 2a 20 49 66 20 74 68 65 20 78 42 65 67 69 6e  ** If the xBegin
2e10e 20 63 61 6c 6c 20 69 73 20 73 75 63 63 65 73 73   call is success
2e10f 66 75 6c 2c 20 70 6c 61 63 65 20 74 68 65 20 73  ful, place the s
2e110 71 6c 69 74 65 33 5f 76 74 61 62 20 70 6f 69 6e  qlite3_vtab poin
2e111 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71  ter.** in the sq
2e112 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72  lite3.aVTrans ar
2e113 72 61 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ray..*/.SQLITE_P
2e114 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2e115 65 33 56 74 61 62 42 65 67 69 6e 28 73 71 6c 69  e3VtabBegin(sqli
2e116 74 65 33 20 2a 64 62 2c 20 56 54 61 62 6c 65 20  te3 *db, VTable 
2e117 2a 70 56 54 61 62 29 7b 0a 20 20 69 6e 74 20 72  *pVTab){.  int r
2e118 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
2e119 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
2e11a 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
2e11b 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61  .  /* Special ca
2e11c 73 65 3a 20 49 66 20 64 62 2d 3e 61 56 54 72 61  se: If db->aVTra
2e11d 6e 73 20 69 73 20 4e 55 4c 4c 20 61 6e 64 20 64  ns is NULL and d
2e11e 62 2d 3e 6e 56 54 72 61 6e 73 20 69 73 20 67 72  b->nVTrans is gr
2e11f 65 61 74 65 72 0a 20 20 2a 2a 20 74 68 61 6e 20  eater.  ** than 
2e120 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 69 73 20  zero, then this 
2e121 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e  function is bein
2e122 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  g called from wi
2e123 74 68 69 6e 20 61 0a 20 20 2a 2a 20 76 69 72 74  thin a.  ** virt
2e124 75 61 6c 20 6d 6f 64 75 6c 65 20 78 53 79 6e 63  ual module xSync
2e125 28 29 20 63 61 6c 6c 62 61 63 6b 2e 20 49 74 20  () callback. It 
2e126 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 77 72  is illegal to wr
2e127 69 74 65 20 74 6f 20 0a 20 20 2a 2a 20 76 69 72  ite to .  ** vir
2e128 74 75 61 6c 20 6d 6f 64 75 6c 65 20 74 61 62 6c  tual module tabl
2e129 65 73 20 69 6e 20 74 68 69 73 20 63 61 73 65 2c  es in this case,
2e12a 20 73 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54   so return SQLIT
2e12b 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20  E_LOCKED..  */. 
2e12c 20 69 66 28 20 73 71 6c 69 74 65 33 56 74 61 62   if( sqlite3Vtab
2e12d 49 6e 53 79 6e 63 28 64 62 29 20 29 7b 0a 20 20  InSync(db) ){.  
2e12e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2e12f 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a 20 20 69 66  LOCKED;.  }.  if
2e130 28 20 21 70 56 54 61 62 20 29 7b 0a 20 20 20 20  ( !pVTab ){.    
2e131 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2e132 3b 0a 20 20 7d 20 0a 20 20 70 4d 6f 64 75 6c 65  ;.  } .  pModule
2e133 20 3d 20 70 56 54 61 62 2d 3e 70 56 74 61 62 2d   = pVTab->pVtab-
2e134 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 69 66 28  >pModule;..  if(
2e135 20 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e   pModule->xBegin
2e136 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a   ){.    int i;..
2e137 0a 20 20 20 20 2f 2a 20 49 66 20 70 56 74 61 62  .    /* If pVtab
2e138 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
2e139 68 65 20 61 56 54 72 61 6e 73 20 61 72 72 61 79  he aVTrans array
2e13a 2c 20 72 65 74 75 72 6e 20 65 61 72 6c 79 20 2a  , return early *
2e13b 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
2e13c 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 3b 20 69 2b  <db->nVTrans; i+
2e13d 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62  +){.      if( db
2e13e 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3d 3d 70 56  ->aVTrans[i]==pV
2e13f 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 72  Tab ){.        r
2e140 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2e141 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2e142 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68      /* Invoke th
2e143 65 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20  e xBegin method 
2e144 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64  */.    rc = pMod
2e145 75 6c 65 2d 3e 78 42 65 67 69 6e 28 70 56 54 61  ule->xBegin(pVTa
2e146 62 2d 3e 70 56 74 61 62 29 3b 0a 20 20 20 20 69  b->pVtab);.    i
2e147 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2e148 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
2e149 64 64 54 6f 56 54 72 61 6e 73 28 64 62 2c 20 70  ddToVTrans(db, p
2e14a 56 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  VTab);.    }.  }
2e14b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2e14c 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  ./*.** The first
2e14d 20 70 61 72 61 6d 65 74 65 72 20 28 70 44 65 66   parameter (pDef
2e14e 29 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20  ) is a function 
2e14f 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
2e150 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70   The.** second p
2e151 61 72 61 6d 65 74 65 72 20 28 70 45 78 70 72 29  arameter (pExpr)
2e152 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72   is the first ar
2e153 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
2e154 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 70  unction..** If p
2e155 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e  Expr is a column
2e156 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61   in a virtual ta
2e157 62 6c 65 2c 20 74 68 65 6e 20 6c 65 74 20 74 68  ble, then let th
2e158 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62  e virtual.** tab
2e159 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
2e15a 6e 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74  n have an opport
2e15b 75 6e 69 74 79 20 74 6f 20 6f 76 65 72 6c 6f 61  unity to overloa
2e15c 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a  d the function..
2e15d 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2e15e 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 61 6c  ne is used to al
2e15f 6c 6f 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c  low virtual tabl
2e160 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
2e161 73 20 74 6f 0a 2a 2a 20 6f 76 65 72 6c 6f 61 64  s to.** overload
2e162 20 4d 41 54 43 48 2c 20 4c 49 4b 45 2c 20 47 4c   MATCH, LIKE, GL
2e163 4f 42 2c 20 61 6e 64 20 52 45 47 45 58 50 20 6f  OB, and REGEXP o
2e164 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  perators..**.** 
2e165 52 65 74 75 72 6e 20 65 69 74 68 65 72 20 74 68  Return either th
2e166 65 20 70 44 65 66 20 61 72 67 75 6d 65 6e 74 20  e pDef argument 
2e167 28 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 63  (indicating no c
2e168 68 61 6e 67 65 29 20 6f 72 20 61 20 0a 2a 2a 20  hange) or a .** 
2e169 6e 65 77 20 46 75 6e 63 44 65 66 20 73 74 72 75  new FuncDef stru
2e16a 63 74 75 72 65 20 74 68 61 74 20 69 73 20 6d 61  cture that is ma
2e16b 72 6b 65 64 20 61 73 20 65 70 68 65 6d 65 72 61  rked as ephemera
2e16c 6c 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53  l using the.** S
2e16d 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d  QLITE_FUNC_EPHEM
2e16e 20 66 6c 61 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45   flag..*/.SQLITE
2e16f 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66  _PRIVATE FuncDef
2e170 20 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65   *sqlite3VtabOve
2e171 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 0a 20  rloadFunction(. 
2e172 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
2e173 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
2e174 6e 65 63 74 69 6f 6e 20 66 6f 72 20 72 65 70 6f  nection for repo
2e175 72 74 69 6e 67 20 6d 61 6c 6c 6f 63 20 70 72 6f  rting malloc pro
2e176 62 6c 65 6d 73 20 2a 2f 0a 20 20 46 75 6e 63 44  blems */.  FuncD
2e177 65 66 20 2a 70 44 65 66 2c 20 20 2f 2a 20 46 75  ef *pDef,  /* Fu
2e178 6e 63 74 69 6f 6e 20 74 6f 20 70 6f 73 73 69 62  nction to possib
2e179 6c 79 20 6f 76 65 72 6c 6f 61 64 20 2a 2f 0a 20  ly overload */. 
2e17a 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20   int nArg,      
2e17b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
2e17c 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66  guments to the f
2e17d 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70  unction */.  Exp
2e17e 72 20 2a 70 45 78 70 72 20 20 20 20 20 2f 2a 20  r *pExpr     /* 
2e17f 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  First argument t
2e180 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  o the function *
2e181 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54  /.){.  Table *pT
2e182 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  ab;.  sqlite3_vt
2e183 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 71 6c  ab *pVtab;.  sql
2e184 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
2e185 64 3b 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  d;.  void (*xFun
2e186 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
2e187 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
2e188 76 61 6c 75 65 2a 2a 29 20 3d 20 30 3b 0a 20 20  value**) = 0;.  
2e189 76 6f 69 64 20 2a 70 41 72 67 20 3d 20 30 3b 0a  void *pArg = 0;.
2e18a 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 77 3b    FuncDef *pNew;
2e18b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20  .  int rc = 0;. 
2e18c 20 63 68 61 72 20 2a 7a 4c 6f 77 65 72 4e 61 6d   char *zLowerNam
2e18d 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  e;.  unsigned ch
2e18e 61 72 20 2a 7a 3b 0a 0a 0a 20 20 2f 2a 20 43 68  ar *z;...  /* Ch
2e18f 65 63 6b 20 74 6f 20 73 65 65 20 74 68 65 20 6c  eck to see the l
2e190 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20 61  eft operand is a
2e191 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72   column in a vir
2e192 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
2e193 69 66 28 20 4e 45 56 45 52 28 70 45 78 70 72 3d  if( NEVER(pExpr=
2e194 3d 30 29 20 29 20 72 65 74 75 72 6e 20 70 44 65  =0) ) return pDe
2e195 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  f;.  if( pExpr->
2e196 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20  op!=TK_COLUMN ) 
2e197 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70  return pDef;.  p
2e198 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 61  Tab = pExpr->pTa
2e199 62 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  b;.  if( NEVER(p
2e19a 54 61 62 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  Tab==0) ) return
2e19b 20 70 44 65 66 3b 0a 20 20 69 66 28 20 28 70 54   pDef;.  if( (pT
2e19c 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
2e19d 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 20 29 20  F_Virtual)==0 ) 
2e19e 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70  return pDef;.  p
2e19f 56 74 61 62 20 3d 20 73 71 6c 69 74 65 33 47 65  Vtab = sqlite3Ge
2e1a0 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62  tVTable(db, pTab
2e1a1 29 2d 3e 70 56 74 61 62 3b 0a 20 20 61 73 73 65  )->pVtab;.  asse
2e1a2 72 74 28 20 70 56 74 61 62 21 3d 30 20 29 3b 0a  rt( pVtab!=0 );.
2e1a3 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62 2d    assert( pVtab-
2e1a4 3e 70 4d 6f 64 75 6c 65 21 3d 30 20 29 3b 0a 20  >pModule!=0 );. 
2e1a5 20 70 4d 6f 64 20 3d 20 28 73 71 6c 69 74 65 33   pMod = (sqlite3
2e1a6 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d  _module *)pVtab-
2e1a7 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 66 28 20  >pModule;.  if( 
2e1a8 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74  pMod->xFindFunct
2e1a9 69 6f 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ion==0 ) return 
2e1aa 70 44 65 66 3b 0a 20 0a 20 20 2f 2a 20 43 61 6c  pDef;. .  /* Cal
2e1ab 6c 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63 74  l the xFindFunct
2e1ac 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68  ion method on th
2e1ad 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2e1ae 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 20  implementation. 
2e1af 20 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74 68   ** to see if th
2e1b0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
2e1b1 20 77 61 6e 74 73 20 74 6f 20 6f 76 65 72 6c 6f   wants to overlo
2e1b2 61 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ad this function
2e1b3 20 0a 20 20 2a 2f 0a 20 20 7a 4c 6f 77 65 72 4e   .  */.  zLowerN
2e1b4 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
2e1b5 74 72 44 75 70 28 64 62 2c 20 70 44 65 66 2d 3e  trDup(db, pDef->
2e1b6 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4c  zName);.  if( zL
2e1b7 6f 77 65 72 4e 61 6d 65 20 29 7b 0a 20 20 20 20  owerName ){.    
2e1b8 66 6f 72 28 7a 3d 28 75 6e 73 69 67 6e 65 64 20  for(z=(unsigned 
2e1b9 63 68 61 72 2a 29 7a 4c 6f 77 65 72 4e 61 6d 65  char*)zLowerName
2e1ba 3b 20 2a 7a 3b 20 7a 2b 2b 29 7b 0a 20 20 20 20  ; *z; z++){.    
2e1bb 20 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 55 70    *z = sqlite3Up
2e1bc 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a 5d 3b 0a  perToLower[*z];.
2e1bd 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70      }.    rc = p
2e1be 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74 69  Mod->xFindFuncti
2e1bf 6f 6e 28 70 56 74 61 62 2c 20 6e 41 72 67 2c 20  on(pVtab, nArg, 
2e1c0 7a 4c 6f 77 65 72 4e 61 6d 65 2c 20 26 78 46 75  zLowerName, &xFu
2e1c1 6e 63 2c 20 26 70 41 72 67 29 3b 0a 20 20 20 20  nc, &pArg);.    
2e1c2 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2e1c3 2c 20 7a 4c 6f 77 65 72 4e 61 6d 65 29 3b 0a 20  , zLowerName);. 
2e1c4 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29   }.  if( rc==0 )
2e1c5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 44 65  {.    return pDe
2e1c6 66 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65  f;.  }..  /* Cre
2e1c7 61 74 65 20 61 20 6e 65 77 20 65 70 68 65 6d 65  ate a new epheme
2e1c8 72 61 6c 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  ral function def
2e1c9 69 6e 69 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  inition for the 
2e1ca 6f 76 65 72 6c 6f 61 64 65 64 0a 20 20 2a 2a 20  overloaded.  ** 
2e1cb 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 4e  function */.  pN
2e1cc 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
2e1cd 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2e1ce 65 6f 66 28 2a 70 4e 65 77 29 0a 20 20 20 20 20  eof(*pNew).     
2e1cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e1d0 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69 74 65          + sqlite
2e1d1 33 53 74 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e  3Strlen30(pDef->
2e1d2 7a 4e 61 6d 65 29 20 2b 20 31 29 3b 0a 20 20 69  zName) + 1);.  i
2e1d3 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  f( pNew==0 ){.  
2e1d4 20 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20    return pDef;. 
2e1d5 20 7d 0a 20 20 2a 70 4e 65 77 20 3d 20 2a 70 44   }.  *pNew = *pD
2e1d6 65 66 3b 0a 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d  ef;.  pNew->zNam
2e1d7 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 4e 65  e = (char *)&pNe
2e1d8 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 28 70  w[1];.  memcpy(p
2e1d9 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66  New->zName, pDef
2e1da 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33  ->zName, sqlite3
2e1db 53 74 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a  Strlen30(pDef->z
2e1dc 4e 61 6d 65 29 2b 31 29 3b 0a 20 20 70 4e 65 77  Name)+1);.  pNew
2e1dd 2d 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b  ->xFunc = xFunc;
2e1de 0a 20 20 70 4e 65 77 2d 3e 70 55 73 65 72 44 61  .  pNew->pUserDa
2e1df 74 61 20 3d 20 70 41 72 67 3b 0a 20 20 70 4e 65  ta = pArg;.  pNe
2e1e0 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  w->flags |= SQLI
2e1e1 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 3b 0a 20  TE_FUNC_EPHEM;. 
2e1e2 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
2e1e3 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
2e1e4 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70   virtual table p
2e1e5 54 61 62 20 69 73 20 63 6f 6e 74 61 69 6e 65 64  Tab is contained
2e1e6 20 69 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e   in the pParse->
2e1e7 61 70 56 69 72 74 75 61 6c 4c 6f 63 6b 5b 5d 0a  apVirtualLock[].
2e1e8 2a 2a 20 61 72 72 61 79 20 73 6f 20 74 68 61 74  ** array so that
2e1e9 20 61 6e 20 4f 50 5f 56 42 65 67 69 6e 20 77 69   an OP_VBegin wi
2e1ea 6c 6c 20 67 65 74 20 67 65 6e 65 72 61 74 65 64  ll get generated
2e1eb 20 66 6f 72 20 69 74 2e 20 20 41 64 64 20 70 54   for it.  Add pT
2e1ec 61 62 20 74 6f 20 74 68 65 0a 2a 2a 20 61 72 72  ab to the.** arr
2e1ed 61 79 20 69 66 20 69 74 20 69 73 20 6d 69 73 73  ay if it is miss
2e1ee 69 6e 67 2e 20 20 49 66 20 70 54 61 62 20 69 73  ing.  If pTab is
2e1ef 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
2e1f0 61 72 72 61 79 2c 20 74 68 69 73 20 72 6f 75 74  array, this rout
2e1f1 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f  ine.** is a no-o
2e1f2 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  p..*/.SQLITE_PRI
2e1f3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2e1f4 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c  3VtabMakeWritabl
2e1f5 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
2e1f6 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20   Table *pTab){. 
2e1f7 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65   Parse *pTopleve
2e1f8 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65  l = sqlite3Parse
2e1f9 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29  Toplevel(pParse)
2e1fa 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  ;.  int i, n;.  
2e1fb 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62 4c 6f  Table **apVtabLo
2e1fc 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 49  ck;..  assert( I
2e1fd 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
2e1fe 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2e1ff 54 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74 61 62 4c  Toplevel->nVtabL
2e200 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  ock; i++){.    i
2e201 66 28 20 70 54 61 62 3d 3d 70 54 6f 70 6c 65 76  f( pTab==pToplev
2e202 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 69  el->apVtabLock[i
2e203 5d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a  ] ) return;.  }.
2e204 20 20 6e 20 3d 20 28 70 54 6f 70 6c 65 76 65 6c    n = (pToplevel
2e205 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b 31 29 2a 73  ->nVtabLock+1)*s
2e206 69 7a 65 6f 66 28 70 54 6f 70 6c 65 76 65 6c 2d  izeof(pToplevel-
2e207 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 30 5d 29 3b  >apVtabLock[0]);
2e208 0a 20 20 61 70 56 74 61 62 4c 6f 63 6b 20 3d 20  .  apVtabLock = 
2e209 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
2e20a 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61  pToplevel->apVta
2e20b 62 4c 6f 63 6b 2c 20 6e 29 3b 0a 20 20 69 66 28  bLock, n);.  if(
2e20c 20 61 70 56 74 61 62 4c 6f 63 6b 20 29 7b 0a 20   apVtabLock ){. 
2e20d 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70     pToplevel->ap
2e20e 56 74 61 62 4c 6f 63 6b 20 3d 20 61 70 56 74 61  VtabLock = apVta
2e20f 62 4c 6f 63 6b 3b 0a 20 20 20 20 70 54 6f 70 6c  bLock;.    pTopl
2e210 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b  evel->apVtabLock
2e211 5b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74 61  [pToplevel->nVta
2e212 62 4c 6f 63 6b 2b 2b 5d 20 3d 20 70 54 61 62 3b  bLock++] = pTab;
2e213 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54  .  }else{.    pT
2e214 6f 70 6c 65 76 65 6c 2d 3e 64 62 2d 3e 6d 61 6c  oplevel->db->mal
2e215 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2e216 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20   }.}..#endif /* 
2e217 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2e218 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a  UALTABLE */../**
2e219 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2e21a 20 6f 66 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a   of vtab.c *****
2e21b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e21c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e21d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2e21e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2e21f 69 6e 20 66 69 6c 65 20 77 68 65 72 65 2e 63 20  in file where.c 
2e220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e221 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e222 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2e223 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
2e224 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
2e225 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
2e226 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
2e227 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
2e228 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
2e229 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2e22a 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2e22b 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2e22c 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2e22d 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
2e22e 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2e22f 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2e230 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2e231 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2e232 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2e233 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2e234 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2e235 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
2e236 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e237 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e238 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e239 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e23a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  ***.** This modu
2e23b 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
2e23c 64 65 20 74 68 61 74 20 67 65 6e 65 72 61 74 65  de that generate
2e23d 73 20 56 44 42 45 20 63 6f 64 65 20 75 73 65 64  s VDBE code used
2e23e 20 74 6f 20 70 72 6f 63 65 73 73 0a 2a 2a 20 74   to process.** t
2e23f 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2e240 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  of SQL statement
2e241 73 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65 20  s.  This module 
2e242 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
2e243 6f 72 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67  or.** generating
2e244 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 6c   the code that l
2e245 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 61 20 74  oops through a t
2e246 61 62 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  able looking for
2e247 20 61 70 70 6c 69 63 61 62 6c 65 0a 2a 2a 20 72   applicable.** r
2e248 6f 77 73 2e 20 20 49 6e 64 69 63 65 73 20 61 72  ows.  Indices ar
2e249 65 20 73 65 6c 65 63 74 65 64 20 61 6e 64 20 75  e selected and u
2e24a 73 65 64 20 74 6f 20 73 70 65 65 64 20 74 68 65  sed to speed the
2e24b 20 73 65 61 72 63 68 20 77 68 65 6e 20 64 6f 69   search when doi
2e24c 6e 67 0a 2a 2a 20 73 6f 20 69 73 20 61 70 70 6c  ng.** so is appl
2e24d 69 63 61 62 6c 65 2e 20 20 42 65 63 61 75 73 65  icable.  Because
2e24e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   this module is 
2e24f 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
2e250 73 65 6c 65 63 74 69 6e 67 0a 2a 2a 20 69 6e 64  selecting.** ind
2e251 69 63 65 73 2c 20 79 6f 75 20 6d 69 67 68 74 20  ices, you might 
2e252 61 6c 73 6f 20 74 68 69 6e 6b 20 6f 66 20 74 68  also think of th
2e253 69 73 20 6d 6f 64 75 6c 65 20 61 73 20 74 68 65  is module as the
2e254 20 22 71 75 65 72 79 20 6f 70 74 69 6d 69 7a 65   "query optimize
2e255 72 22 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77  r"..**.** $Id: w
2e256 68 65 72 65 2e 63 2c 76 20 31 2e 34 31 31 20 32  here.c,v 1.411 2
2e257 30 30 39 2f 30 37 2f 33 31 20 30 36 3a 31 34 3a  009/07/31 06:14:
2e258 35 32 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  52 danielk1977 E
2e259 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
2e25a 72 61 63 65 20 6f 75 74 70 75 74 20 6d 61 63 72  race output macr
2e25b 6f 73 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  os.*/.#if define
2e25c 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c  d(SQLITE_TEST) |
2e25d 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
2e25e 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50  _DEBUG).SQLITE_P
2e25f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2e260 65 33 57 68 65 72 65 54 72 61 63 65 20 3d 20 30  e3WhereTrace = 0
2e261 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66  ;.#endif.#if def
2e262 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
2e263 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
2e264 49 54 45 5f 44 45 42 55 47 29 0a 23 20 64 65 66  ITE_DEBUG).# def
2e265 69 6e 65 20 57 48 45 52 45 54 52 41 43 45 28 58  ine WHERETRACE(X
2e266 29 20 20 69 66 28 73 71 6c 69 74 65 33 57 68 65  )  if(sqlite3Whe
2e267 72 65 54 72 61 63 65 29 20 73 71 6c 69 74 65 33  reTrace) sqlite3
2e268 44 65 62 75 67 50 72 69 6e 74 66 20 58 0a 23 65  DebugPrintf X.#e
2e269 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 57 48 45  lse.# define WHE
2e26a 52 45 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69  RETRACE(X).#endi
2e26b 66 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65  f../* Forward re
2e26c 66 65 72 65 6e 63 65 0a 2a 2f 0a 74 79 70 65 64  ference.*/.typed
2e26d 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 43  ef struct WhereC
2e26e 6c 61 75 73 65 20 57 68 65 72 65 43 6c 61 75 73  lause WhereClaus
2e26f 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
2e270 74 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 57  t WhereMaskSet W
2e271 68 65 72 65 4d 61 73 6b 53 65 74 3b 0a 74 79 70  hereMaskSet;.typ
2e272 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72  edef struct Wher
2e273 65 4f 72 49 6e 66 6f 20 57 68 65 72 65 4f 72 49  eOrInfo WhereOrI
2e274 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72  nfo;.typedef str
2e275 75 63 74 20 57 68 65 72 65 41 6e 64 49 6e 66 6f  uct WhereAndInfo
2e276 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 3b 0a 74   WhereAndInfo;.t
2e277 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68  ypedef struct Wh
2e278 65 72 65 43 6f 73 74 20 57 68 65 72 65 43 6f 73  ereCost WhereCos
2e279 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 71 75  t;../*.** The qu
2e27a 65 72 79 20 67 65 6e 65 72 61 74 6f 72 20 75 73  ery generator us
2e27b 65 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69  es an array of i
2e27c 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 69 73  nstances of this
2e27d 20 73 74 72 75 63 74 75 72 65 20 74 6f 0a 2a 2a   structure to.**
2e27e 20 68 65 6c 70 20 69 74 20 61 6e 61 6c 79 7a 65   help it analyze
2e27f 20 74 68 65 20 73 75 62 65 78 70 72 65 73 73 69   the subexpressi
2e280 6f 6e 73 20 6f 66 20 74 68 65 20 57 48 45 52 45  ons of the WHERE
2e281 20 63 6c 61 75 73 65 2e 20 20 45 61 63 68 20 57   clause.  Each W
2e282 48 45 52 45 0a 2a 2a 20 63 6c 61 75 73 65 20 73  HERE.** clause s
2e283 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  ubexpression is 
2e284 73 65 70 61 72 61 74 65 64 20 66 72 6f 6d 20 74  separated from t
2e285 68 65 20 6f 74 68 65 72 73 20 62 79 20 41 4e 44  he others by AND
2e286 20 6f 70 65 72 61 74 6f 72 73 2c 0a 2a 2a 20 75   operators,.** u
2e287 73 75 61 6c 6c 79 2c 20 6f 72 20 73 6f 6d 65 74  sually, or somet
2e288 69 6d 65 73 20 73 75 62 65 78 70 72 65 73 73 69  imes subexpressi
2e289 6f 6e 73 20 73 65 70 61 72 61 74 65 64 20 62 79  ons separated by
2e28a 20 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 57   OR..**.** All W
2e28b 68 65 72 65 54 65 72 6d 73 20 61 72 65 20 63 6f  hereTerms are co
2e28c 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 73  llected into a s
2e28d 69 6e 67 6c 65 20 57 68 65 72 65 43 6c 61 75 73  ingle WhereClaus
2e28e 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 0a 2a  e structure.  .*
2e28f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2e290 69 64 65 6e 74 69 74 79 20 68 6f 6c 64 73 3a 0a  identity holds:.
2e291 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 57 68 65  **.**        Whe
2e292 72 65 54 65 72 6d 2e 70 57 43 2d 3e 61 5b 57 68  reTerm.pWC->a[Wh
2e293 65 72 65 54 65 72 6d 2e 69 64 78 5d 20 3d 3d 20  ereTerm.idx] == 
2e294 57 68 65 72 65 54 65 72 6d 0a 2a 2a 0a 2a 2a 20  WhereTerm.**.** 
2e295 57 68 65 6e 20 61 20 74 65 72 6d 20 69 73 20 6f  When a term is o
2e296 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a  f the form:.**.*
2e297 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 58  *              X
2e298 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 0a 2a 2a 0a   <op> <expr>.**.
2e299 2a 2a 20 77 68 65 72 65 20 58 20 69 73 20 61 20  ** where X is a 
2e29a 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 6e 64 20  column name and 
2e29b 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f 66 20 63  <op> is one of c
2e29c 65 72 74 61 69 6e 20 6f 70 65 72 61 74 6f 72 73  ertain operators
2e29d 2c 0a 2a 2a 20 74 68 65 6e 20 57 68 65 72 65 54  ,.** then WhereT
2e29e 65 72 6d 2e 6c 65 66 74 43 75 72 73 6f 72 20 61  erm.leftCursor a
2e29f 6e 64 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 6c  nd WhereTerm.u.l
2e2a0 65 66 74 43 6f 6c 75 6d 6e 20 72 65 63 6f 72 64  eftColumn record
2e2a1 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 6e   the.** cursor n
2e2a2 75 6d 62 65 72 20 61 6e 64 20 63 6f 6c 75 6d 6e  umber and column
2e2a3 20 6e 75 6d 62 65 72 20 66 6f 72 20 58 2e 20 20   number for X.  
2e2a4 57 68 65 72 65 54 65 72 6d 2e 65 4f 70 65 72 61  WhereTerm.eOpera
2e2a5 74 6f 72 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74  tor records.** t
2e2a6 68 65 20 3c 6f 70 3e 20 75 73 69 6e 67 20 61 20  he <op> using a 
2e2a7 62 69 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67  bitmask encoding
2e2a8 20 64 65 66 69 6e 65 64 20 62 79 20 57 4f 5f 78   defined by WO_x
2e2a9 78 78 20 62 65 6c 6f 77 2e 20 20 54 68 65 0a 2a  xx below.  The.*
2e2aa 2a 20 75 73 65 20 6f 66 20 61 20 62 69 74 6d 61  * use of a bitma
2e2ab 73 6b 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20  sk encoding for 
2e2ac 74 68 65 20 6f 70 65 72 61 74 6f 72 20 61 6c 6c  the operator all
2e2ad 6f 77 73 20 75 73 20 74 6f 20 73 65 61 72 63 68  ows us to search
2e2ae 0a 2a 2a 20 71 75 69 63 6b 6c 79 20 66 6f 72 20  .** quickly for 
2e2af 74 65 72 6d 73 20 74 68 61 74 20 6d 61 74 63 68  terms that match
2e2b0 20 61 6e 79 20 6f 66 20 73 65 76 65 72 61 6c 20   any of several 
2e2b1 64 69 66 66 65 72 65 6e 74 20 6f 70 65 72 61 74  different operat
2e2b2 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 57 68 65  ors..**.** A Whe
2e2b3 72 65 54 65 72 6d 20 6d 69 67 68 74 20 61 6c 73  reTerm might als
2e2b4 6f 20 62 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65  o be two or more
2e2b5 20 73 75 62 74 65 72 6d 73 20 63 6f 6e 6e 65 63   subterms connec
2e2b6 74 65 64 20 62 79 20 4f 52 3a 0a 2a 2a 0a 2a 2a  ted by OR:.**.**
2e2b7 20 20 20 20 20 20 20 20 20 28 74 31 2e 58 20 3c           (t1.X <
2e2b8 6f 70 3e 20 3c 65 78 70 72 3e 29 20 4f 52 20 28  op> <expr>) OR (
2e2b9 74 31 2e 59 20 3c 6f 70 3e 20 3c 65 78 70 72 3e  t1.Y <op> <expr>
2e2ba 29 20 4f 52 20 2e 2e 2e 2e 0a 2a 2a 0a 2a 2a 20  ) OR .....**.** 
2e2bb 49 6e 20 74 68 69 73 20 73 65 63 6f 6e 64 20 63  In this second c
2e2bc 61 73 65 2c 20 77 74 46 6c 61 67 20 61 73 20 74  ase, wtFlag as t
2e2bd 68 65 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 73  he TERM_ORINFO s
2e2be 65 74 20 61 6e 64 20 65 4f 70 65 72 61 74 6f 72  et and eOperator
2e2bf 3d 3d 57 4f 5f 4f 52 0a 2a 2a 20 61 6e 64 20 74  ==WO_OR.** and t
2e2c0 68 65 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70  he WhereTerm.u.p
2e2c1 4f 72 49 6e 66 6f 20 66 69 65 6c 64 20 70 6f 69  OrInfo field poi
2e2c2 6e 74 73 20 74 6f 20 61 75 78 69 6c 69 61 72 79  nts to auxiliary
2e2c3 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
2e2c4 74 0a 2a 2a 20 69 73 20 63 6f 6c 6c 65 63 74 65  t.** is collecte
2e2c5 64 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 0a 2a  d about the.**.*
2e2c6 2a 20 49 66 20 61 20 74 65 72 6d 20 69 6e 20 74  * If a term in t
2e2c7 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2e2c8 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 65  does not match e
2e2c9 69 74 68 65 72 20 6f 66 20 74 68 65 20 74 77 6f  ither of the two
2e2ca 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 63 61 74   previous.** cat
2e2cb 65 67 6f 72 69 65 73 2c 20 74 68 65 6e 20 65 4f  egories, then eO
2e2cc 70 65 72 61 74 6f 72 3d 3d 30 2e 20 20 54 68 65  perator==0.  The
2e2cd 20 57 68 65 72 65 54 65 72 6d 2e 70 45 78 70 72   WhereTerm.pExpr
2e2ce 20 66 69 65 6c 64 20 69 73 20 73 74 69 6c 6c 20   field is still 
2e2cf 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 72  set.** to the or
2e2d0 69 67 69 6e 61 6c 20 73 75 62 65 78 70 72 65 73  iginal subexpres
2e2d1 73 69 6f 6e 20 63 6f 6e 74 65 6e 74 20 61 6e 64  sion content and
2e2d2 20 77 74 46 6c 61 67 73 20 69 73 20 73 65 74 20   wtFlags is set 
2e2d3 75 70 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79  up appropriately
2e2d4 0a 2a 2a 20 62 75 74 20 6e 6f 20 6f 74 68 65 72  .** but no other
2e2d5 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 57   fields in the W
2e2d6 68 65 72 65 54 65 72 6d 20 6f 62 6a 65 63 74 20  hereTerm object 
2e2d7 61 72 65 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a  are meaningful..
2e2d8 2a 2a 0a 2a 2a 20 57 68 65 6e 20 65 4f 70 65 72  **.** When eOper
2e2d9 61 74 6f 72 21 3d 30 2c 20 70 72 65 72 65 71 52  ator!=0, prereqR
2e2da 69 67 68 74 20 61 6e 64 20 70 72 65 72 65 71 41  ight and prereqA
2e2db 6c 6c 20 72 65 63 6f 72 64 20 73 65 74 73 20 6f  ll record sets o
2e2dc 66 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  f cursor numbers
2e2dd 2c 0a 2a 2a 20 62 75 74 20 74 68 65 79 20 64 6f  ,.** but they do
2e2de 20 73 6f 20 69 6e 64 69 72 65 63 74 6c 79 2e 20   so indirectly. 
2e2df 20 41 20 73 69 6e 67 6c 65 20 57 68 65 72 65 4d   A single WhereM
2e2e0 61 73 6b 53 65 74 20 73 74 72 75 63 74 75 72 65  askSet structure
2e2e1 20 74 72 61 6e 73 6c 61 74 65 73 0a 2a 2a 20 63   translates.** c
2e2e2 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 6e 74  ursor number int
2e2e3 6f 20 62 69 74 73 20 61 6e 64 20 74 68 65 20 74  o bits and the t
2e2e4 72 61 6e 73 6c 61 74 65 64 20 62 69 74 20 69 73  ranslated bit is
2e2e5 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70   stored in the p
2e2e6 72 65 72 65 71 0a 2a 2a 20 66 69 65 6c 64 73 2e  rereq.** fields.
2e2e7 20 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f    The translatio
2e2e8 6e 20 69 73 20 75 73 65 64 20 69 6e 20 6f 72 64  n is used in ord
2e2e9 65 72 20 74 6f 20 6d 61 78 69 6d 69 7a 65 20 74  er to maximize t
2e2ea 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
2e2eb 62 69 74 73 20 74 68 61 74 20 77 69 6c 6c 20 66  bits that will f
2e2ec 69 74 20 69 6e 20 61 20 42 69 74 6d 61 73 6b 2e  it in a Bitmask.
2e2ed 20 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f    The VDBE curso
2e2ee 72 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20  r numbers might 
2e2ef 62 65 0a 2a 2a 20 73 70 72 65 61 64 20 6f 75 74  be.** spread out
2e2f0 20 6f 76 65 72 20 74 68 65 20 6e 6f 6e 2d 6e 65   over the non-ne
2e2f1 67 61 74 69 76 65 20 69 6e 74 65 67 65 72 73 2e  gative integers.
2e2f2 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74    For example, t
2e2f3 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d  he cursor.** num
2e2f4 62 65 72 73 20 6d 69 67 68 74 20 62 65 20 33 2c  bers might be 3,
2e2f5 20 38 2c 20 39 2c 20 31 30 2c 20 32 30 2c 20 32   8, 9, 10, 20, 2
2e2f6 33 2c 20 34 31 2c 20 61 6e 64 20 34 35 2e 20 20  3, 41, and 45.  
2e2f7 54 68 65 20 57 68 65 72 65 4d 61 73 6b 53 65 74  The WhereMaskSet
2e2f8 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65 73 20 74  .** translates t
2e2f9 68 65 73 65 20 73 70 61 72 73 65 20 63 75 72 73  hese sparse curs
2e2fa 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20  or numbers into 
2e2fb 63 6f 6e 73 65 63 75 74 69 76 65 20 69 6e 74 65  consecutive inte
2e2fc 67 65 72 73 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e  gers.** beginnin
2e2fd 67 20 77 69 74 68 20 30 20 69 6e 20 6f 72 64 65  g with 0 in orde
2e2fe 72 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 62 65  r to make the be
2e2ff 73 74 20 70 6f 73 73 69 62 6c 65 20 75 73 65 20  st possible use 
2e300 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65  of the available
2e301 0a 2a 2a 20 62 69 74 73 20 69 6e 20 74 68 65 20  .** bits in the 
2e302 42 69 74 6d 61 73 6b 2e 20 20 53 6f 2c 20 69 6e  Bitmask.  So, in
2e303 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f   the example abo
2e304 76 65 2c 20 74 68 65 20 63 75 72 73 6f 72 20 6e  ve, the cursor n
2e305 75 6d 62 65 72 73 0a 2a 2a 20 77 6f 75 6c 64 20  umbers.** would 
2e306 62 65 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 69  be mapped into i
2e307 6e 74 65 67 65 72 73 20 30 20 74 68 72 6f 75 67  ntegers 0 throug
2e308 68 20 37 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  h 7..**.** The n
2e309 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69  umber of terms i
2e30a 6e 20 61 20 6a 6f 69 6e 20 69 73 20 6c 69 6d 69  n a join is limi
2e30b 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65  ted by the numbe
2e30c 72 20 6f 66 20 62 69 74 73 0a 2a 2a 20 69 6e 20  r of bits.** in 
2e30d 70 72 65 72 65 71 52 69 67 68 74 20 61 6e 64 20  prereqRight and 
2e30e 70 72 65 72 65 71 41 6c 6c 2e 20 20 54 68 65 20  prereqAll.  The 
2e30f 64 65 66 61 75 6c 74 20 69 73 20 36 34 20 62 69  default is 64 bi
2e310 74 73 2c 20 68 65 6e 63 65 20 53 51 4c 69 74 65  ts, hence SQLite
2e311 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 61 62 6c 65  .** is only able
2e312 20 74 6f 20 70 72 6f 63 65 73 73 20 6a 6f 69 6e   to process join
2e313 73 20 77 69 74 68 20 36 34 20 6f 72 20 66 65 77  s with 64 or few
2e314 65 72 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79  er tables..*/.ty
2e315 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65  pedef struct Whe
2e316 72 65 54 65 72 6d 20 57 68 65 72 65 54 65 72 6d  reTerm WhereTerm
2e317 3b 0a 73 74 72 75 63 74 20 57 68 65 72 65 54 65  ;.struct WhereTe
2e318 72 6d 20 7b 0a 20 20 45 78 70 72 20 2a 70 45 78  rm {.  Expr *pEx
2e319 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  pr;            /
2e31a 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
2e31b 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 74   subexpression t
2e31c 68 61 74 20 69 73 20 74 68 69 73 20 74 65 72 6d  hat is this term
2e31d 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e   */.  int iParen
2e31e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
2e31f 20 44 69 73 61 62 6c 65 20 70 57 43 2d 3e 61 5b   Disable pWC->a[
2e320 69 50 61 72 65 6e 74 5d 20 77 68 65 6e 20 74 68  iParent] when th
2e321 69 73 20 74 65 72 6d 20 64 69 73 61 62 6c 65 64  is term disabled
2e322 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 66 74 43 75   */.  int leftCu
2e323 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 2f 2a  rsor;         /*
2e324 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   Cursor number o
2e325 66 20 58 20 69 6e 20 22 58 20 3c 6f 70 3e 20 3c  f X in "X <op> <
2e326 65 78 70 72 3e 22 20 2a 2f 0a 20 20 75 6e 69 6f  expr>" */.  unio
2e327 6e 20 7b 0a 20 20 20 20 69 6e 74 20 6c 65 66 74  n {.    int left
2e328 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
2e329 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
2e32a 20 6f 66 20 58 20 69 6e 20 22 58 20 3c 6f 70 3e   of X in "X <op>
2e32b 20 3c 65 78 70 72 3e 22 20 2a 2f 0a 20 20 20 20   <expr>" */.    
2e32c 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a 70 4f 72  WhereOrInfo *pOr
2e32d 49 6e 66 6f 3b 20 20 20 2f 2a 20 45 78 74 72 61  Info;   /* Extra
2e32e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20   information if 
2e32f 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52  eOperator==WO_OR
2e330 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 41 6e 64   */.    WhereAnd
2e331 49 6e 66 6f 20 2a 70 41 6e 64 49 6e 66 6f 3b 20  Info *pAndInfo; 
2e332 2f 2a 20 45 78 74 72 61 20 69 6e 66 6f 72 6d 61  /* Extra informa
2e333 74 69 6f 6e 20 69 66 20 65 4f 70 65 72 61 74 6f  tion if eOperato
2e334 72 3d 3d 57 4f 5f 41 4e 44 20 2a 2f 0a 20 20 7d  r==WO_AND */.  }
2e335 20 75 3b 0a 20 20 75 31 36 20 65 4f 70 65 72 61   u;.  u16 eOpera
2e336 74 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  tor;          /*
2e337 20 41 20 57 4f 5f 78 78 20 76 61 6c 75 65 20 64   A WO_xx value d
2e338 65 73 63 72 69 62 69 6e 67 20 3c 6f 70 3e 20 2a  escribing <op> *
2e339 2f 0a 20 20 75 38 20 77 74 46 6c 61 67 73 3b 20  /.  u8 wtFlags; 
2e33a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2e33b 45 52 4d 5f 78 78 78 20 62 69 74 20 66 6c 61 67  ERM_xxx bit flag
2e33c 73 2e 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f  s.  See below */
2e33d 0a 20 20 75 38 20 6e 43 68 69 6c 64 3b 20 20 20  .  u8 nChild;   
2e33e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2e33f 6d 62 65 72 20 6f 66 20 63 68 69 6c 64 72 65 6e  mber of children
2e340 20 74 68 61 74 20 6d 75 73 74 20 64 69 73 61 62   that must disab
2e341 6c 65 20 75 73 20 2a 2f 0a 20 20 57 68 65 72 65  le us */.  Where
2e342 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20  Clause *pWC;    
2e343 20 20 20 2f 2a 20 54 68 65 20 63 6c 61 75 73 65     /* The clause
2e344 20 74 68 69 73 20 74 65 72 6d 20 69 73 20 70 61   this term is pa
2e345 72 74 20 6f 66 20 2a 2f 0a 20 20 42 69 74 6d 61  rt of */.  Bitma
2e346 73 6b 20 70 72 65 72 65 71 52 69 67 68 74 3b 20  sk prereqRight; 
2e347 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66     /* Bitmask of
2e348 20 74 61 62 6c 65 73 20 75 73 65 64 20 62 79 20   tables used by 
2e349 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 2a 2f  pExpr->pRight */
2e34a 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65  .  Bitmask prere
2e34b 71 41 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 42 69  qAll;      /* Bi
2e34c 74 6d 61 73 6b 20 6f 66 20 74 61 62 6c 65 73 20  tmask of tables 
2e34d 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 70 45  referenced by pE
2e34e 78 70 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  xpr */.};../*.**
2e34f 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   Allowed values 
2e350 6f 66 20 57 68 65 72 65 54 65 72 6d 2e 77 74 46  of WhereTerm.wtF
2e351 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  lags.*/.#define 
2e352 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 20 20 20  TERM_DYNAMIC    
2e353 30 78 30 31 20 20 20 2f 2a 20 4e 65 65 64 20 74  0x01   /* Need t
2e354 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 45 78  o call sqlite3Ex
2e355 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78  prDelete(db, pEx
2e356 70 72 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  pr) */.#define T
2e357 45 52 4d 5f 56 49 52 54 55 41 4c 20 20 20 20 30  ERM_VIRTUAL    0
2e358 78 30 32 20 20 20 2f 2a 20 41 64 64 65 64 20 62  x02   /* Added b
2e359 79 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e  y the optimizer.
2e35a 20 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20 2a 2f    Do not code */
2e35b 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 43 4f  .#define TERM_CO
2e35c 44 45 44 20 20 20 20 20 20 30 78 30 34 20 20 20  DED      0x04   
2e35d 2f 2a 20 54 68 69 73 20 74 65 72 6d 20 69 73 20  /* This term is 
2e35e 61 6c 72 65 61 64 79 20 63 6f 64 65 64 20 2a 2f  already coded */
2e35f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 43 4f  .#define TERM_CO
2e360 50 49 45 44 20 20 20 20 20 30 78 30 38 20 20 20  PIED     0x08   
2e361 2f 2a 20 48 61 73 20 61 20 63 68 69 6c 64 20 2a  /* Has a child *
2e362 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 4f  /.#define TERM_O
2e363 52 49 4e 46 4f 20 20 20 20 20 30 78 31 30 20 20  RINFO     0x10  
2e364 20 2f 2a 20 4e 65 65 64 20 74 6f 20 66 72 65 65   /* Need to free
2e365 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 2e 75   the WhereTerm.u
2e366 2e 70 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74 20  .pOrInfo object 
2e367 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f  */.#define TERM_
2e368 41 4e 44 49 4e 46 4f 20 20 20 20 30 78 32 30 20  ANDINFO    0x20 
2e369 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 66 72 65    /* Need to fre
2e36a 65 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 2e  e the WhereTerm.
2e36b 75 2e 70 41 6e 64 49 6e 66 6f 20 6f 62 6a 20 2a  u.pAndInfo obj *
2e36c 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 4f  /.#define TERM_O
2e36d 52 5f 4f 4b 20 20 20 20 20 20 30 78 34 30 20 20  R_OK      0x40  
2e36e 20 2f 2a 20 55 73 65 64 20 64 75 72 69 6e 67 20   /* Used during 
2e36f 4f 52 2d 63 6c 61 75 73 65 20 70 72 6f 63 65 73  OR-clause proces
2e370 73 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  sing */../*.** A
2e371 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
2e372 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
2e373 63 74 75 72 65 20 68 6f 6c 64 73 20 61 6c 6c 20  cture holds all 
2e374 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
2e375 74 20 61 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61  t a.** WHERE cla
2e376 75 73 65 2e 20 20 4d 6f 73 74 6c 79 20 74 68 69  use.  Mostly thi
2e377 73 20 69 73 20 61 20 63 6f 6e 74 61 69 6e 65 72  s is a container
2e378 20 66 6f 72 20 6f 6e 65 20 6f 72 20 6d 6f 72 65   for one or more
2e379 20 57 68 65 72 65 54 65 72 6d 73 2e 0a 2a 2f 0a   WhereTerms..*/.
2e37a 73 74 72 75 63 74 20 57 68 65 72 65 43 6c 61 75  struct WhereClau
2e37b 73 65 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  se {.  Parse *pP
2e37c 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  arse;           
2e37d 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
2e37e 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65  ntext */.  Where
2e37f 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2e380 74 3b 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f  t;  /* Mapping o
2e381 66 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 6e  f table cursor n
2e382 75 6d 62 65 72 73 20 74 6f 20 62 69 74 6d 61 73  umbers to bitmas
2e383 6b 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20  ks */.  Bitmask 
2e384 76 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20  vmask;          
2e385 20 2f 2a 20 42 69 74 6d 61 73 6b 20 69 64 65 6e   /* Bitmask iden
2e386 74 69 66 79 69 6e 67 20 76 69 72 74 75 61 6c 20  tifying virtual 
2e387 74 61 62 6c 65 20 63 75 72 73 6f 72 73 20 2a 2f  table cursors */
2e388 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20  .  u8 op;       
2e389 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
2e38a 70 6c 69 74 20 6f 70 65 72 61 74 6f 72 2e 20 20  plit operator.  
2e38b 54 4b 5f 41 4e 44 20 6f 72 20 54 4b 5f 4f 52 20  TK_AND or TK_OR 
2e38c 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20  */.  int nTerm; 
2e38d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e38e 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   Number of terms
2e38f 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6c 6f 74 3b   */.  int nSlot;
2e390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e391 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
2e392 69 65 73 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20 20  ies in a[] */.  
2e393 57 68 65 72 65 54 65 72 6d 20 2a 61 3b 20 20 20  WhereTerm *a;   
2e394 20 20 20 20 20 20 20 20 20 2f 2a 20 45 61 63 68           /* Each
2e395 20 61 5b 5d 20 64 65 73 63 72 69 62 65 73 20 61   a[] describes a
2e396 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45   term of the WHE
2e397 52 45 20 63 6c 75 61 73 65 20 2a 2f 0a 23 69 66  RE cluase */.#if
2e398 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
2e399 53 4d 41 4c 4c 5f 53 54 41 43 4b 29 0a 20 20 57  SMALL_STACK).  W
2e39a 68 65 72 65 54 65 72 6d 20 61 53 74 61 74 69 63  hereTerm aStatic
2e39b 5b 31 5d 3b 20 20 20 20 2f 2a 20 49 6e 69 74 69  [1];    /* Initi
2e39c 61 6c 20 73 74 61 74 69 63 20 73 70 61 63 65 20  al static space 
2e39d 66 6f 72 20 61 5b 5d 20 2a 2f 0a 23 65 6c 73 65  for a[] */.#else
2e39e 0a 20 20 57 68 65 72 65 54 65 72 6d 20 61 53 74  .  WhereTerm aSt
2e39f 61 74 69 63 5b 38 5d 3b 20 20 20 20 2f 2a 20 49  atic[8];    /* I
2e3a0 6e 69 74 69 61 6c 20 73 74 61 74 69 63 20 73 70  nitial static sp
2e3a1 61 63 65 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 23  ace for a[] */.#
2e3a2 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
2e3a3 41 20 57 68 65 72 65 54 65 72 6d 20 77 69 74 68  A WhereTerm with
2e3a4 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f   eOperator==WO_O
2e3a5 52 20 68 61 73 20 69 74 73 20 75 2e 70 4f 72 49  R has its u.pOrI
2e3a6 6e 66 6f 20 70 6f 69 6e 74 65 72 20 73 65 74 20  nfo pointer set 
2e3a7 74 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d 69 63 61  to.** a dynamica
2e3a8 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  lly allocated in
2e3a9 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
2e3aa 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
2e3ab 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65  e..*/.struct Whe
2e3ac 72 65 4f 72 49 6e 66 6f 20 7b 0a 20 20 57 68 65  reOrInfo {.  Whe
2e3ad 72 65 43 6c 61 75 73 65 20 77 63 3b 20 20 20 20  reClause wc;    
2e3ae 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f        /* Decompo
2e3af 73 69 74 69 6f 6e 20 69 6e 74 6f 20 73 75 62 74  sition into subt
2e3b0 65 72 6d 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73  erms */.  Bitmas
2e3b1 6b 20 69 6e 64 65 78 61 62 6c 65 3b 20 20 20 20  k indexable;    
2e3b2 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66     /* Bitmask of
2e3b3 20 61 6c 6c 20 69 6e 64 65 78 61 62 6c 65 20 74   all indexable t
2e3b4 61 62 6c 65 73 20 69 6e 20 74 68 65 20 63 6c 61  ables in the cla
2e3b5 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  use */.};../*.**
2e3b6 20 41 20 57 68 65 72 65 54 65 72 6d 20 77 69 74   A WhereTerm wit
2e3b7 68 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  h eOperator==WO_
2e3b8 41 4e 44 20 68 61 73 20 69 74 73 20 75 2e 70 41  AND has its u.pA
2e3b9 6e 64 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 73  ndInfo pointer s
2e3ba 65 74 20 74 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d  et to.** a dynam
2e3bb 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
2e3bc 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
2e3bd 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
2e3be 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ture..*/.struct 
2e3bf 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 7b 0a 20  WhereAndInfo {. 
2e3c0 20 57 68 65 72 65 43 6c 61 75 73 65 20 77 63 3b   WhereClause wc;
2e3c1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2e3c2 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 62   subexpression b
2e3c3 72 6f 6b 65 6e 20 6f 75 74 20 2a 2f 0a 7d 3b 0a  roken out */.};.
2e3c4 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
2e3c5 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
2e3c6 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65  ing structure ke
2e3c7 65 70 73 20 74 72 61 63 6b 20 6f 66 20 61 20 6d  eps track of a m
2e3c8 61 70 70 69 6e 67 0a 2a 2a 20 62 65 74 77 65 65  apping.** betwee
2e3c9 6e 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  n VDBE cursor nu
2e3ca 6d 62 65 72 73 20 61 6e 64 20 62 69 74 73 20 6f  mbers and bits o
2e3cb 66 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20 69  f the bitmasks i
2e3cc 6e 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a  n WhereTerm..**.
2e3cd 2a 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73  ** The VDBE curs
2e3ce 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 73  or numbers are s
2e3cf 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 20 63 6f  mall integers co
2e3d0 6e 74 61 69 6e 65 64 20 69 6e 20 0a 2a 2a 20 53  ntained in .** S
2e3d1 72 63 4c 69 73 74 5f 69 74 65 6d 2e 69 43 75 72  rcList_item.iCur
2e3d2 73 6f 72 20 61 6e 64 20 45 78 70 72 2e 69 54 61  sor and Expr.iTa
2e3d3 62 6c 65 20 66 69 65 6c 64 73 2e 20 20 46 6f 72  ble fields.  For
2e3d4 20 61 6e 79 20 67 69 76 65 6e 20 57 48 45 52 45   any given WHERE
2e3d5 20 0a 2a 2a 20 63 6c 61 75 73 65 2c 20 74 68 65   .** clause, the
2e3d6 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2e3d7 6d 69 67 68 74 20 6e 6f 74 20 62 65 67 69 6e 20  might not begin 
2e3d8 77 69 74 68 20 30 20 61 6e 64 20 74 68 65 79 20  with 0 and they 
2e3d9 6d 69 67 68 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e  might.** contain
2e3da 20 67 61 70 73 20 69 6e 20 74 68 65 20 6e 75 6d   gaps in the num
2e3db 62 65 72 69 6e 67 20 73 65 71 75 65 6e 63 65 2e  bering sequence.
2e3dc 20 20 42 75 74 20 77 65 20 77 61 6e 74 20 74 6f    But we want to
2e3dd 20 6d 61 6b 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a   make maximum.**
2e3de 20 75 73 65 20 6f 66 20 74 68 65 20 62 69 74 73   use of the bits
2e3df 20 69 6e 20 6f 75 72 20 62 69 74 6d 61 73 6b 73   in our bitmasks
2e3e0 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72  .  This structur
2e3e1 65 20 70 72 6f 76 69 64 65 73 20 61 20 6d 61 70  e provides a map
2e3e2 70 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  ping.** from the
2e3e3 20 73 70 61 72 73 65 20 63 75 72 73 6f 72 20 6e   sparse cursor n
2e3e4 75 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73  umbers into cons
2e3e5 65 63 75 74 69 76 65 20 69 6e 74 65 67 65 72 73  ecutive integers
2e3e6 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69   beginning.** wi
2e3e7 74 68 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 57  th 0..**.** If W
2e3e8 68 65 72 65 4d 61 73 6b 53 65 74 2e 69 78 5b 41  hereMaskSet.ix[A
2e3e9 5d 3d 3d 42 20 69 74 20 6d 65 61 6e 73 20 74 68  ]==B it means th
2e3ea 61 74 20 54 68 65 20 41 2d 74 68 20 62 69 74 20  at The A-th bit 
2e3eb 6f 66 20 61 20 42 69 74 6d 61 73 6b 0a 2a 2a 20  of a Bitmask.** 
2e3ec 63 6f 72 72 65 73 70 6f 6e 64 73 20 56 44 42 45  corresponds VDBE
2e3ed 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 42   cursor number B
2e3ee 2e 20 20 54 68 65 20 41 2d 74 68 20 62 69 74 20  .  The A-th bit 
2e3ef 6f 66 20 61 20 62 69 74 6d 61 73 6b 20 69 73 20  of a bitmask is 
2e3f0 31 3c 3c 41 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  1<<A..**.** For 
2e3f1 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20  example, if the 
2e3f2 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70  WHERE clause exp
2e3f3 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74 68 65  ression used the
2e3f4 73 65 20 56 44 42 45 0a 2a 2a 20 63 75 72 73 6f  se VDBE.** curso
2e3f5 72 73 3a 20 20 34 2c 20 35 2c 20 38 2c 20 32 39  rs:  4, 5, 8, 29
2e3f6 2c 20 35 37 2c 20 37 33 2e 20 20 54 68 65 6e 20  , 57, 73.  Then 
2e3f7 74 68 65 20 20 57 68 65 72 65 4d 61 73 6b 53 65  the  WhereMaskSe
2e3f8 74 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77  t structure.** w
2e3f9 6f 75 6c 64 20 6d 61 70 20 74 68 6f 73 65 20 63  ould map those c
2e3fa 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e  ursor numbers in
2e3fb 74 6f 20 62 69 74 73 20 30 20 74 68 72 6f 75 67  to bits 0 throug
2e3fc 68 20 35 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  h 5..**.** Note 
2e3fd 74 68 61 74 20 74 68 65 20 6d 61 70 70 69 6e 67  that the mapping
2e3fe 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
2e3ff 69 6c 79 20 6f 72 64 65 72 65 64 2e 20 20 49 6e  ily ordered.  In
2e400 20 74 68 65 20 65 78 61 6d 70 6c 65 0a 2a 2a 20   the example.** 
2e401 61 62 6f 76 65 2c 20 74 68 65 20 6d 61 70 70 69  above, the mappi
2e402 6e 67 20 6d 69 67 68 74 20 67 6f 20 6c 69 6b 65  ng might go like
2e403 20 74 68 69 73 3a 20 20 34 2d 3e 33 2c 20 35 2d   this:  4->3, 5-
2e404 3e 31 2c 20 38 2d 3e 32 2c 20 32 39 2d 3e 30 2c  >1, 8->2, 29->0,
2e405 0a 2a 2a 20 35 37 2d 3e 35 2c 20 37 33 2d 3e 34  .** 57->5, 73->4
2e406 2e 20 20 4f 72 20 6f 6e 65 20 6f 66 20 37 31 39  .  Or one of 719
2e407 20 6f 74 68 65 72 20 63 6f 6d 62 69 6e 61 74 69   other combinati
2e408 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 75 73 65  ons might be use
2e409 64 2e 20 49 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f  d. It.** does no
2e40a 74 20 72 65 61 6c 6c 79 20 6d 61 74 74 65 72 2e  t really matter.
2e40b 20 20 57 68 61 74 20 69 73 20 69 6d 70 6f 72 74    What is import
2e40c 61 6e 74 20 69 73 20 74 68 61 74 20 73 70 61 72  ant is that spar
2e40d 73 65 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d  se cursor.** num
2e40e 62 65 72 73 20 61 6c 6c 20 67 65 74 20 6d 61 70  bers all get map
2e40f 70 65 64 20 69 6e 74 6f 20 62 69 74 20 6e 75 6d  ped into bit num
2e410 62 65 72 73 20 74 68 61 74 20 62 65 67 69 6e 20  bers that begin 
2e411 77 69 74 68 20 30 20 61 6e 64 20 63 6f 6e 74 61  with 0 and conta
2e412 69 6e 0a 2a 2a 20 6e 6f 20 67 61 70 73 2e 0a 2a  in.** no gaps..*
2e413 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4d 61  /.struct WhereMa
2e414 73 6b 53 65 74 20 7b 0a 20 20 69 6e 74 20 6e 3b  skSet {.  int n;
2e415 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e416 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2e417 72 20 6f 66 20 61 73 73 69 67 6e 65 64 20 63 75  r of assigned cu
2e418 72 73 6f 72 20 76 61 6c 75 65 73 20 2a 2f 0a 20  rsor values */. 
2e419 20 69 6e 74 20 69 78 5b 42 4d 53 5d 3b 20 20 20   int ix[BMS];   
2e41a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e41b 2a 20 43 75 72 73 6f 72 20 61 73 73 69 67 6e 65  * Cursor assigne
2e41c 64 20 74 6f 20 65 61 63 68 20 62 69 74 20 2a 2f  d to each bit */
2e41d 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65  .};../*.** A Whe
2e41e 72 65 43 6f 73 74 20 6f 62 6a 65 63 74 20 72 65  reCost object re
2e41f 63 6f 72 64 73 20 61 20 6c 6f 6f 6b 75 70 20 73  cords a lookup s
2e420 74 72 61 74 65 67 79 20 61 6e 64 20 74 68 65 20  trategy and the 
2e421 65 73 74 69 6d 61 74 65 64 0a 2a 2a 20 63 6f 73  estimated.** cos
2e422 74 20 6f 66 20 70 75 72 73 75 69 6e 67 20 74 68  t of pursuing th
2e423 61 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2f 0a  at strategy..*/.
2e424 73 74 72 75 63 74 20 57 68 65 72 65 43 6f 73 74  struct WhereCost
2e425 20 7b 0a 20 20 57 68 65 72 65 50 6c 61 6e 20 70   {.  WherePlan p
2e426 6c 61 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c  lan;    /* The l
2e427 6f 6f 6b 75 70 20 73 74 72 61 74 65 67 79 20 2a  ookup strategy *
2e428 2f 0a 20 20 64 6f 75 62 6c 65 20 72 43 6f 73 74  /.  double rCost
2e429 3b 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 61 6c  ;      /* Overal
2e42a 6c 20 63 6f 73 74 20 6f 66 20 70 75 72 73 75 69  l cost of pursui
2e42b 6e 67 20 74 68 69 73 20 73 65 61 72 63 68 20 73  ng this search s
2e42c 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 64 6f 75  trategy */.  dou
2e42d 62 6c 65 20 6e 52 6f 77 3b 20 20 20 20 20 20 20  ble nRow;       
2e42e 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 6e 75 6d  /* Estimated num
2e42f 62 65 72 20 6f 66 20 6f 75 74 70 75 74 20 72 6f  ber of output ro
2e430 77 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20  ws */.  Bitmask 
2e431 75 73 65 64 3b 20 20 20 20 20 20 2f 2a 20 42 69  used;      /* Bi
2e432 74 6d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73  tmask of cursors
2e433 20 75 73 65 64 20 62 79 20 74 68 69 73 20 70 6c   used by this pl
2e434 61 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  an */.};../*.** 
2e435 42 69 74 6d 61 73 6b 73 20 66 6f 72 20 74 68 65  Bitmasks for the
2e436 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61 74 20   operators that 
2e437 69 6e 64 69 63 65 73 20 61 72 65 20 61 62 6c 65  indices are able
2e438 20 74 6f 20 65 78 70 6c 6f 69 74 2e 20 20 41 6e   to exploit.  An
2e439 0a 2a 2a 20 4f 52 2d 65 64 20 63 6f 6d 62 69 6e  .** OR-ed combin
2e43a 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 76  ation of these v
2e43b 61 6c 75 65 73 20 63 61 6e 20 62 65 20 75 73 65  alues can be use
2e43c 64 20 77 68 65 6e 20 73 65 61 72 63 68 69 6e 67  d when searching
2e43d 20 66 6f 72 0a 2a 2a 20 74 65 72 6d 73 20 69 6e   for.** terms in
2e43e 20 74 68 65 20 77 68 65 72 65 20 63 6c 61 75 73   the where claus
2e43f 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f  e..*/.#define WO
2e440 5f 49 4e 20 20 20 20 20 30 78 30 30 31 0a 23 64  _IN     0x001.#d
2e441 65 66 69 6e 65 20 57 4f 5f 45 51 20 20 20 20 20  efine WO_EQ     
2e442 30 78 30 30 32 0a 23 64 65 66 69 6e 65 20 57 4f  0x002.#define WO
2e443 5f 4c 54 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c  _LT     (WO_EQ<<
2e444 28 54 4b 5f 4c 54 2d 54 4b 5f 45 51 29 29 0a 23  (TK_LT-TK_EQ)).#
2e445 64 65 66 69 6e 65 20 57 4f 5f 4c 45 20 20 20 20  define WO_LE    
2e446 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 45 2d   (WO_EQ<<(TK_LE-
2e447 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20  TK_EQ)).#define 
2e448 57 4f 5f 47 54 20 20 20 20 20 28 57 4f 5f 45 51  WO_GT     (WO_EQ
2e449 3c 3c 28 54 4b 5f 47 54 2d 54 4b 5f 45 51 29 29  <<(TK_GT-TK_EQ))
2e44a 0a 23 64 65 66 69 6e 65 20 57 4f 5f 47 45 20 20  .#define WO_GE  
2e44b 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47     (WO_EQ<<(TK_G
2e44c 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e  E-TK_EQ)).#defin
2e44d 65 20 57 4f 5f 4d 41 54 43 48 20 20 30 78 30 34  e WO_MATCH  0x04
2e44e 30 0a 23 64 65 66 69 6e 65 20 57 4f 5f 49 53 4e  0.#define WO_ISN
2e44f 55 4c 4c 20 30 78 30 38 30 0a 23 64 65 66 69 6e  ULL 0x080.#defin
2e450 65 20 57 4f 5f 4f 52 20 20 20 20 20 30 78 31 30  e WO_OR     0x10
2e451 30 20 20 20 20 20 20 20 2f 2a 20 54 77 6f 20 6f  0       /* Two o
2e452 72 20 6d 6f 72 65 20 4f 52 2d 63 6f 6e 6e 65 63  r more OR-connec
2e453 74 65 64 20 74 65 72 6d 73 20 2a 2f 0a 23 64 65  ted terms */.#de
2e454 66 69 6e 65 20 57 4f 5f 41 4e 44 20 20 20 20 30  fine WO_AND    0
2e455 78 32 30 30 20 20 20 20 20 20 20 2f 2a 20 54 77  x200       /* Tw
2e456 6f 20 6f 72 20 6d 6f 72 65 20 41 4e 44 2d 63 6f  o or more AND-co
2e457 6e 6e 65 63 74 65 64 20 74 65 72 6d 73 20 2a 2f  nnected terms */
2e458 0a 0a 23 64 65 66 69 6e 65 20 57 4f 5f 41 4c 4c  ..#define WO_ALL
2e459 20 20 20 20 30 78 66 66 66 20 20 20 20 20 20 20      0xfff       
2e45a 2f 2a 20 4d 61 73 6b 20 6f 66 20 61 6c 6c 20 70  /* Mask of all p
2e45b 6f 73 73 69 62 6c 65 20 57 4f 5f 2a 20 76 61 6c  ossible WO_* val
2e45c 75 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  ues */.#define W
2e45d 4f 5f 53 49 4e 47 4c 45 20 30 78 30 66 66 20 20  O_SINGLE 0x0ff  
2e45e 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
2e45f 61 6c 6c 20 6e 6f 6e 2d 63 6f 6d 70 6f 75 6e 64  all non-compound
2e460 20 57 4f 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a   WO_* values */.
2e461 0a 2f 2a 0a 2a 2a 20 56 61 6c 75 65 20 66 6f 72  ./*.** Value for
2e462 20 77 73 46 6c 61 67 73 20 72 65 74 75 72 6e 65   wsFlags returne
2e463 64 20 62 79 20 62 65 73 74 49 6e 64 65 78 28 29  d by bestIndex()
2e464 20 61 6e 64 20 73 74 6f 72 65 64 20 69 6e 0a 2a   and stored in.*
2e465 2a 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73 46  * WhereLevel.wsF
2e466 6c 61 67 73 2e 20 20 54 68 65 73 65 20 66 6c 61  lags.  These fla
2e467 67 73 20 64 65 74 65 72 6d 69 6e 65 20 77 68 69  gs determine whi
2e468 63 68 20 73 65 61 72 63 68 0a 2a 2a 20 73 74 72  ch search.** str
2e469 61 74 65 67 69 65 73 20 61 72 65 20 61 70 70 72  ategies are appr
2e46a 6f 70 72 69 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54  opriate..**.** T
2e46b 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69  he least signifi
2e46c 63 61 6e 74 20 31 32 20 62 69 74 73 20 69 73 20  cant 12 bits is 
2e46d 72 65 73 65 72 76 65 64 20 61 73 20 61 20 6d 61  reserved as a ma
2e46e 73 6b 20 66 6f 72 20 57 4f 5f 20 76 61 6c 75 65  sk for WO_ value
2e46f 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 54 68 65 20  s above..** The 
2e470 57 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61  WhereLevel.wsFla
2e471 67 73 20 66 69 65 6c 64 20 69 73 20 75 73 75 61  gs field is usua
2e472 6c 6c 79 20 73 65 74 20 74 6f 20 57 4f 5f 49 4e  lly set to WO_IN
2e473 7c 57 4f 5f 45 51 7c 57 4f 5f 49 53 4e 55 4c 4c  |WO_EQ|WO_ISNULL
2e474 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20  ..** But if the 
2e475 74 61 62 6c 65 20 69 73 20 74 68 65 20 72 69 67  table is the rig
2e476 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6c 65  ht table of a le
2e477 66 74 20 6a 6f 69 6e 2c 20 57 68 65 72 65 4c 65  ft join, WhereLe
2e478 76 65 6c 2e 77 73 46 6c 61 67 73 0a 2a 2a 20 69  vel.wsFlags.** i
2e479 73 20 73 65 74 20 74 6f 20 57 4f 5f 49 4e 7c 57  s set to WO_IN|W
2e47a 4f 5f 45 51 2e 20 20 54 68 65 20 57 68 65 72 65  O_EQ.  The Where
2e47b 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73 20 66 69  Level.wsFlags fi
2e47c 65 6c 64 20 63 61 6e 20 74 68 65 6e 20 62 65 20  eld can then be 
2e47d 75 73 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 22  used as.** the "
2e47e 6f 70 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f  op" parameter to
2e47f 20 66 69 6e 64 54 65 72 6d 20 77 68 65 6e 20 77   findTerm when w
2e480 65 20 61 72 65 20 72 65 73 6f 6c 76 69 6e 67 20  e are resolving 
2e481 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2e482 69 6e 74 73 2e 0a 2a 2a 20 49 53 4e 55 4c 4c 20  ints..** ISNULL 
2e483 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 69 6c 6c  constraints will
2e484 20 74 68 65 6e 20 6e 6f 74 20 62 65 20 75 73 65   then not be use
2e485 64 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20 74  d on the right t
2e486 61 62 6c 65 20 6f 66 20 61 20 6c 65 66 74 0a 2a  able of a left.*
2e487 2a 20 6a 6f 69 6e 2e 20 20 54 69 63 6b 65 74 73  * join.  Tickets
2e488 20 23 32 31 37 37 20 61 6e 64 20 23 32 31 38 39   #2177 and #2189
2e489 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ..*/.#define WHE
2e48a 52 45 5f 52 4f 57 49 44 5f 45 51 20 20 20 20 20  RE_ROWID_EQ     
2e48b 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 72  0x00001000  /* r
2e48c 6f 77 69 64 3d 45 58 50 52 20 6f 72 20 72 6f 77  owid=EXPR or row
2e48d 69 64 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23  id IN (...) */.#
2e48e 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52 4f 57  define WHERE_ROW
2e48f 49 44 5f 52 41 4e 47 45 20 20 30 78 30 30 30 30  ID_RANGE  0x0000
2e490 32 30 30 30 20 20 2f 2a 20 72 6f 77 69 64 3c 45  2000  /* rowid<E
2e491 58 50 52 20 61 6e 64 2f 6f 72 20 72 6f 77 69 64  XPR and/or rowid
2e492 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66 69 6e 65  >EXPR */.#define
2e493 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51   WHERE_COLUMN_EQ
2e494 20 20 20 20 30 78 30 30 30 31 30 30 30 30 20 20      0x00010000  
2e495 2f 2a 20 78 3d 45 58 50 52 20 6f 72 20 78 20 49  /* x=EXPR or x I
2e496 4e 20 28 2e 2e 2e 29 20 6f 72 20 78 20 49 53 20  N (...) or x IS 
2e497 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NULL */.#define 
2e498 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e  WHERE_COLUMN_RAN
2e499 47 45 20 30 78 30 30 30 32 30 30 30 30 20 20 2f  GE 0x00020000  /
2e49a 2a 20 78 3c 45 58 50 52 20 61 6e 64 2f 6f 72 20  * x<EXPR and/or 
2e49b 78 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66 69 6e  x>EXPR */.#defin
2e49c 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49  e WHERE_COLUMN_I
2e49d 4e 20 20 20 20 30 78 30 30 30 34 30 30 30 30 20  N    0x00040000 
2e49e 20 2f 2a 20 78 20 49 4e 20 28 2e 2e 2e 29 20 2a   /* x IN (...) *
2e49f 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2e4a0 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 20 20 30 78 30  COLUMN_NULL  0x0
2e4a1 30 30 38 30 30 30 30 20 20 2f 2a 20 78 20 49 53  0080000  /* x IS
2e4a2 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65   NULL */.#define
2e4a3 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20 20   WHERE_INDEXED  
2e4a4 20 20 20 20 30 78 30 30 30 66 30 30 30 30 20 20      0x000f0000  
2e4a5 2f 2a 20 41 6e 79 74 68 69 6e 67 20 74 68 61 74  /* Anything that
2e4a6 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78 20 2a   uses an index *
2e4a7 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2e4a8 49 4e 5f 41 42 4c 45 20 20 20 20 20 20 30 78 30  IN_ABLE      0x0
2e4a9 30 30 66 31 30 30 30 20 20 2f 2a 20 41 62 6c 65  00f1000  /* Able
2e4aa 20 74 6f 20 73 75 70 70 6f 72 74 20 61 6e 20 49   to support an I
2e4ab 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 23 64  N operator */.#d
2e4ac 65 66 69 6e 65 20 57 48 45 52 45 5f 54 4f 50 5f  efine WHERE_TOP_
2e4ad 4c 49 4d 49 54 20 20 20 20 30 78 30 30 31 30 30  LIMIT    0x00100
2e4ae 30 30 30 20 20 2f 2a 20 78 3c 45 58 50 52 20 6f  000  /* x<EXPR o
2e4af 72 20 78 3c 3d 45 58 50 52 20 63 6f 6e 73 74 72  r x<=EXPR constr
2e4b0 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  aint */.#define 
2e4b1 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20  WHERE_BTM_LIMIT 
2e4b2 20 20 20 30 78 30 30 32 30 30 30 30 30 20 20 2f     0x00200000  /
2e4b3 2a 20 78 3e 45 58 50 52 20 6f 72 20 78 3e 3d 45  * x>EXPR or x>=E
2e4b4 58 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a  XPR constraint *
2e4b5 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2e4b6 49 44 58 5f 4f 4e 4c 59 20 20 20 20 20 30 78 30  IDX_ONLY     0x0
2e4b7 30 38 30 30 30 30 30 20 20 2f 2a 20 55 73 65 20  0800000  /* Use 
2e4b8 69 6e 64 65 78 20 6f 6e 6c 79 20 2d 20 6f 6d 69  index only - omi
2e4b9 74 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69  t table */.#defi
2e4ba 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  ne WHERE_ORDERBY
2e4bb 20 20 20 20 20 20 30 78 30 31 30 30 30 30 30 30        0x01000000
2e4bc 20 20 2f 2a 20 4f 75 74 70 75 74 20 77 69 6c 6c    /* Output will
2e4bd 20 61 70 70 65 61 72 20 69 6e 20 63 6f 72 72 65   appear in corre
2e4be 63 74 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66  ct order */.#def
2e4bf 69 6e 65 20 57 48 45 52 45 5f 52 45 56 45 52 53  ine WHERE_REVERS
2e4c0 45 20 20 20 20 20 20 30 78 30 32 30 30 30 30 30  E      0x0200000
2e4c1 30 20 20 2f 2a 20 53 63 61 6e 20 69 6e 20 72 65  0  /* Scan in re
2e4c2 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 23  verse order */.#
2e4c3 64 65 66 69 6e 65 20 57 48 45 52 45 5f 55 4e 49  define WHERE_UNI
2e4c4 51 55 45 20 20 20 20 20 20 20 30 78 30 34 30 30  QUE       0x0400
2e4c5 30 30 30 30 20 20 2f 2a 20 53 65 6c 65 63 74 73  0000  /* Selects
2e4c6 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e   no more than on
2e4c7 65 20 72 6f 77 20 2a 2f 0a 23 64 65 66 69 6e 65  e row */.#define
2e4c8 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41   WHERE_VIRTUALTA
2e4c9 42 4c 45 20 30 78 30 38 30 30 30 30 30 30 20 20  BLE 0x08000000  
2e4ca 2f 2a 20 55 73 65 20 76 69 72 74 75 61 6c 2d 74  /* Use virtual-t
2e4cb 61 62 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 20  able processing 
2e4cc 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
2e4cd 5f 4d 55 4c 54 49 5f 4f 52 20 20 20 20 20 30 78  _MULTI_OR     0x
2e4ce 31 30 30 30 30 30 30 30 20 20 2f 2a 20 4f 52 20  10000000  /* OR 
2e4cf 75 73 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 69  using multiple i
2e4d0 6e 64 69 63 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ndices */../*.**
2e4d1 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 70 72   Initialize a pr
2e4d2 65 61 6c 6c 6f 63 61 74 65 64 20 57 68 65 72 65  eallocated Where
2e4d3 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65  Clause structure
2e4d4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2e4d5 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74   whereClauseInit
2e4d6 28 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  (.  WhereClause 
2e4d7 2a 70 57 43 2c 20 20 20 20 20 20 20 20 2f 2a 20  *pWC,        /* 
2e4d8 54 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  The WhereClause 
2e4d9 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  to be initialize
2e4da 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50  d */.  Parse *pP
2e4db 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
2e4dc 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
2e4dd 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72  ontext */.  Wher
2e4de 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  eMaskSet *pMaskS
2e4df 65 74 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20  et   /* Mapping 
2e4e0 66 72 6f 6d 20 74 61 62 6c 65 20 63 75 72 73 6f  from table curso
2e4e1 72 20 6e 75 6d 62 65 72 73 20 74 6f 20 62 69 74  r numbers to bit
2e4e2 6d 61 73 6b 73 20 2a 2f 0a 29 7b 0a 20 20 70 57  masks */.){.  pW
2e4e3 43 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  C->pParse = pPar
2e4e4 73 65 3b 0a 20 20 70 57 43 2d 3e 70 4d 61 73 6b  se;.  pWC->pMask
2e4e5 53 65 74 20 3d 20 70 4d 61 73 6b 53 65 74 3b 0a  Set = pMaskSet;.
2e4e6 20 20 70 57 43 2d 3e 6e 54 65 72 6d 20 3d 20 30    pWC->nTerm = 0
2e4e7 3b 0a 20 20 70 57 43 2d 3e 6e 53 6c 6f 74 20 3d  ;.  pWC->nSlot =
2e4e8 20 41 72 72 61 79 53 69 7a 65 28 70 57 43 2d 3e   ArraySize(pWC->
2e4e9 61 53 74 61 74 69 63 29 3b 0a 20 20 70 57 43 2d  aStatic);.  pWC-
2e4ea 3e 61 20 3d 20 70 57 43 2d 3e 61 53 74 61 74 69  >a = pWC->aStati
2e4eb 63 3b 0a 20 20 70 57 43 2d 3e 76 6d 61 73 6b 20  c;.  pWC->vmask 
2e4ec 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61  = 0;.}../* Forwa
2e4ed 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a  rd reference */.
2e4ee 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72  static void wher
2e4ef 65 43 6c 61 75 73 65 43 6c 65 61 72 28 57 68 65  eClauseClear(Whe
2e4f0 72 65 43 6c 61 75 73 65 2a 29 3b 0a 0a 2f 2a 0a  reClause*);../*.
2e4f1 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c  ** Deallocate al
2e4f2 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  l memory associa
2e4f3 74 65 64 20 77 69 74 68 20 61 20 57 68 65 72 65  ted with a Where
2e4f4 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a 2a  OrInfo object..*
2e4f5 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68  /.static void wh
2e4f6 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65 74 65 28  ereOrInfoDelete(
2e4f7 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65  sqlite3 *db, Whe
2e4f8 72 65 4f 72 49 6e 66 6f 20 2a 70 29 7b 0a 20 20  reOrInfo *p){.  
2e4f9 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72  whereClauseClear
2e4fa 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73 71 6c 69  (&p->wc);.  sqli
2e4fb 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29  te3DbFree(db, p)
2e4fc 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c  ;.}../*.** Deall
2e4fd 6f 63 61 74 65 20 61 6c 6c 20 6d 65 6d 6f 72 79  ocate all memory
2e4fe 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2e4ff 20 61 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20   a WhereAndInfo 
2e500 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69  object..*/.stati
2e501 63 20 76 6f 69 64 20 77 68 65 72 65 41 6e 64 49  c void whereAndI
2e502 6e 66 6f 44 65 6c 65 74 65 28 73 71 6c 69 74 65  nfoDelete(sqlite
2e503 33 20 2a 64 62 2c 20 57 68 65 72 65 41 6e 64 49  3 *db, WhereAndI
2e504 6e 66 6f 20 2a 70 29 7b 0a 20 20 77 68 65 72 65  nfo *p){.  where
2e505 43 6c 61 75 73 65 43 6c 65 61 72 28 26 70 2d 3e  ClauseClear(&p->
2e506 77 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  wc);.  sqlite3Db
2e507 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a  Free(db, p);.}..
2e508 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  /*.** Deallocate
2e509 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 20 73   a WhereClause s
2e50a 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 57  tructure.  The W
2e50b 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63  hereClause struc
2e50c 74 75 72 65 0a 2a 2a 20 69 74 73 65 6c 66 20 69  ture.** itself i
2e50d 73 20 6e 6f 74 20 66 72 65 65 64 2e 20 20 54 68  s not freed.  Th
2e50e 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
2e50f 65 20 69 6e 76 65 72 73 65 20 6f 66 20 77 68 65  e inverse of whe
2e510 72 65 43 6c 61 75 73 65 49 6e 69 74 28 29 2e 0a  reClauseInit()..
2e511 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
2e512 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28  hereClauseClear(
2e513 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
2e514 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 57 68  ){.  int i;.  Wh
2e515 65 72 65 54 65 72 6d 20 2a 61 3b 0a 20 20 73 71  ereTerm *a;.  sq
2e516 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 57 43 2d  lite3 *db = pWC-
2e517 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 66  >pParse->db;.  f
2e518 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d 2d  or(i=pWC->nTerm-
2e519 31 2c 20 61 3d 70 57 43 2d 3e 61 3b 20 69 3e 3d  1, a=pWC->a; i>=
2e51a 30 3b 20 69 2d 2d 2c 20 61 2b 2b 29 7b 0a 20 20  0; i--, a++){.  
2e51b 20 20 69 66 28 20 61 2d 3e 77 74 46 6c 61 67 73    if( a->wtFlags
2e51c 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20   & TERM_DYNAMIC 
2e51d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2e51e 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 61  ExprDelete(db, a
2e51f 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a  ->pExpr);.    }.
2e520 20 20 20 20 69 66 28 20 61 2d 3e 77 74 46 6c 61      if( a->wtFla
2e521 67 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e 46 4f  gs & TERM_ORINFO
2e522 20 29 7b 0a 20 20 20 20 20 20 77 68 65 72 65 4f   ){.      whereO
2e523 72 49 6e 66 6f 44 65 6c 65 74 65 28 64 62 2c 20  rInfoDelete(db, 
2e524 61 2d 3e 75 2e 70 4f 72 49 6e 66 6f 29 3b 0a 20  a->u.pOrInfo);. 
2e525 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 2d 3e     }else if( a->
2e526 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 41  wtFlags & TERM_A
2e527 4e 44 49 4e 46 4f 20 29 7b 0a 20 20 20 20 20 20  NDINFO ){.      
2e528 77 68 65 72 65 41 6e 64 49 6e 66 6f 44 65 6c 65  whereAndInfoDele
2e529 74 65 28 64 62 2c 20 61 2d 3e 75 2e 70 41 6e 64  te(db, a->u.pAnd
2e52a 49 6e 66 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Info);.    }.  }
2e52b 0a 20 20 69 66 28 20 70 57 43 2d 3e 61 21 3d 70  .  if( pWC->a!=p
2e52c 57 43 2d 3e 61 53 74 61 74 69 63 20 29 7b 0a 20  WC->aStatic ){. 
2e52d 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2e52e 28 64 62 2c 20 70 57 43 2d 3e 61 29 3b 0a 20 20  (db, pWC->a);.  
2e52f 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  }.}../*.** Add a
2e530 20 73 69 6e 67 6c 65 20 6e 65 77 20 57 68 65 72   single new Wher
2e531 65 54 65 72 6d 20 65 6e 74 72 79 20 74 6f 20 74  eTerm entry to t
2e532 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 6f  he WhereClause o
2e533 62 6a 65 63 74 20 70 57 43 2e 0a 2a 2a 20 54 68  bject pWC..** Th
2e534 65 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d 20  e new WhereTerm 
2e535 6f 62 6a 65 63 74 20 69 73 20 63 6f 6e 73 74 72  object is constr
2e536 75 63 74 65 64 20 66 72 6f 6d 20 45 78 70 72 20  ucted from Expr 
2e537 70 20 61 6e 64 20 77 69 74 68 20 77 74 46 6c 61  p and with wtFla
2e538 67 73 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78  gs..** The index
2e539 20 69 6e 20 70 57 43 2d 3e 61 5b 5d 20 6f 66 20   in pWC->a[] of 
2e53a 74 68 65 20 6e 65 77 20 57 68 65 72 65 54 65 72  the new WhereTer
2e53b 6d 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  m is returned on
2e53c 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 30 20 69   success..** 0 i
2e53d 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  s returned if th
2e53e 65 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d 20  e new WhereTerm 
2e53f 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 64 64  could not be add
2e540 65 64 20 64 75 65 20 74 6f 20 61 20 6d 65 6d 6f  ed due to a memo
2e541 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  ry.** allocation
2e542 20 65 72 72 6f 72 2e 20 20 54 68 65 20 6d 65 6d   error.  The mem
2e543 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
2e544 61 69 6c 75 72 65 20 77 69 6c 6c 20 62 65 20 72  ailure will be r
2e545 65 63 6f 72 64 65 64 20 69 6e 0a 2a 2a 20 74 68  ecorded in.** th
2e546 65 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  e db->mallocFail
2e547 65 64 20 66 6c 61 67 20 73 6f 20 74 68 61 74 20  ed flag so that 
2e548 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 66 75 6e  higher-level fun
2e549 63 74 69 6f 6e 73 20 63 61 6e 20 64 65 74 65 63  ctions can detec
2e54a 74 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  t it..**.** This
2e54b 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 69 6e   routine will in
2e54c 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20  crease the size 
2e54d 6f 66 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20  of the pWC->a[] 
2e54e 61 72 72 61 79 20 61 73 20 6e 65 63 65 73 73 61  array as necessa
2e54f 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ry..**.** If the
2e550 20 77 74 46 6c 61 67 73 20 61 72 67 75 6d 65 6e   wtFlags argumen
2e551 74 20 69 6e 63 6c 75 64 65 73 20 54 45 52 4d 5f  t includes TERM_
2e552 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 72 65  DYNAMIC, then re
2e553 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a 2a 2a 20  sponsibility.** 
2e554 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20  for freeing the 
2e555 65 78 70 72 65 73 73 69 6f 6e 20 70 20 69 73 20  expression p is 
2e556 61 73 73 75 6d 65 64 20 62 79 20 74 68 65 20 57  assumed by the W
2e557 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65 63  hereClause objec
2e558 74 20 70 57 43 2e 0a 2a 2a 20 54 68 69 73 20 69  t pWC..** This i
2e559 73 20 74 72 75 65 20 65 76 65 6e 20 69 66 20 74  s true even if t
2e55a 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c  his routine fail
2e55b 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  s to allocate a 
2e55c 6e 65 77 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a  new WhereTerm..*
2e55d 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20 54  *.** WARNING:  T
2e55e 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68  his routine migh
2e55f 74 20 72 65 61 6c 6c 6f 63 61 74 65 20 74 68 65  t reallocate the
2e560 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 73   space used to s
2e561 74 6f 72 65 0a 2a 2a 20 57 68 65 72 65 54 65 72  tore.** WhereTer
2e562 6d 73 2e 20 20 41 6c 6c 20 70 6f 69 6e 74 65 72  ms.  All pointer
2e563 73 20 74 6f 20 57 68 65 72 65 54 65 72 6d 73 20  s to WhereTerms 
2e564 73 68 6f 75 6c 64 20 62 65 20 69 6e 76 61 6c 69  should be invali
2e565 64 61 74 65 64 20 61 66 74 65 72 0a 2a 2a 20 63  dated after.** c
2e566 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
2e567 69 6e 65 2e 20 20 53 75 63 68 20 70 6f 69 6e 74  ine.  Such point
2e568 65 72 73 20 6d 61 79 20 62 65 20 72 65 69 6e 69  ers may be reini
2e569 74 69 61 6c 69 7a 65 64 20 62 79 20 72 65 66 65  tialized by refe
2e56a 72 65 6e 63 69 6e 67 0a 2a 2a 20 74 68 65 20 70  rencing.** the p
2e56b 57 43 2d 3e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a  WC->a[] array..*
2e56c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 65  /.static int whe
2e56d 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 57  reClauseInsert(W
2e56e 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
2e56f 20 45 78 70 72 20 2a 70 2c 20 75 38 20 77 74 46   Expr *p, u8 wtF
2e570 6c 61 67 73 29 7b 0a 20 20 57 68 65 72 65 54 65  lags){.  WhereTe
2e571 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74  rm *pTerm;.  int
2e572 20 69 64 78 3b 0a 20 20 69 66 28 20 70 57 43 2d   idx;.  if( pWC-
2e573 3e 6e 54 65 72 6d 3e 3d 70 57 43 2d 3e 6e 53 6c  >nTerm>=pWC->nSl
2e574 6f 74 20 29 7b 0a 20 20 20 20 57 68 65 72 65 54  ot ){.    WhereT
2e575 65 72 6d 20 2a 70 4f 6c 64 20 3d 20 70 57 43 2d  erm *pOld = pWC-
2e576 3e 61 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  >a;.    sqlite3 
2e577 2a 64 62 20 3d 20 70 57 43 2d 3e 70 50 61 72 73  *db = pWC->pPars
2e578 65 2d 3e 64 62 3b 0a 20 20 20 20 70 57 43 2d 3e  e->db;.    pWC->
2e579 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  a = sqlite3DbMal
2e57a 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
2e57b 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43  f(pWC->a[0])*pWC
2e57c 2d 3e 6e 53 6c 6f 74 2a 32 20 29 3b 0a 20 20 20  ->nSlot*2 );.   
2e57d 20 69 66 28 20 70 57 43 2d 3e 61 3d 3d 30 20 29   if( pWC->a==0 )
2e57e 7b 0a 20 20 20 20 20 20 69 66 28 20 77 74 46 6c  {.      if( wtFl
2e57f 61 67 73 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d  ags & TERM_DYNAM
2e580 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  IC ){.        sq
2e581 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2e582 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a  db, p);.      }.
2e583 20 20 20 20 20 20 70 57 43 2d 3e 61 20 3d 20 70        pWC->a = p
2e584 4f 6c 64 3b 0a 20 20 20 20 20 20 72 65 74 75 72  Old;.      retur
2e585 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  n 0;.    }.    m
2e586 65 6d 63 70 79 28 70 57 43 2d 3e 61 2c 20 70 4f  emcpy(pWC->a, pO
2e587 6c 64 2c 20 73 69 7a 65 6f 66 28 70 57 43 2d 3e  ld, sizeof(pWC->
2e588 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e 54 65 72 6d  a[0])*pWC->nTerm
2e589 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 21  );.    if( pOld!
2e58a 3d 70 57 43 2d 3e 61 53 74 61 74 69 63 20 29 7b  =pWC->aStatic ){
2e58b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2e58c 46 72 65 65 28 64 62 2c 20 70 4f 6c 64 29 3b 0a  Free(db, pOld);.
2e58d 20 20 20 20 7d 0a 20 20 20 20 70 57 43 2d 3e 6e      }.    pWC->n
2e58e 53 6c 6f 74 20 3d 20 73 71 6c 69 74 65 33 44 62  Slot = sqlite3Db
2e58f 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70  MallocSize(db, p
2e590 57 43 2d 3e 61 29 2f 73 69 7a 65 6f 66 28 70 57  WC->a)/sizeof(pW
2e591 43 2d 3e 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20  C->a[0]);.  }.  
2e592 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  pTerm = &pWC->a[
2e593 69 64 78 20 3d 20 70 57 43 2d 3e 6e 54 65 72 6d  idx = pWC->nTerm
2e594 2b 2b 5d 3b 0a 20 20 70 54 65 72 6d 2d 3e 70 45  ++];.  pTerm->pE
2e595 78 70 72 20 3d 20 70 3b 0a 20 20 70 54 65 72 6d  xpr = p;.  pTerm
2e596 2d 3e 77 74 46 6c 61 67 73 20 3d 20 77 74 46 6c  ->wtFlags = wtFl
2e597 61 67 73 3b 0a 20 20 70 54 65 72 6d 2d 3e 70 57  ags;.  pTerm->pW
2e598 43 20 3d 20 70 57 43 3b 0a 20 20 70 54 65 72 6d  C = pWC;.  pTerm
2e599 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b 0a  ->iParent = -1;.
2e59a 20 20 72 65 74 75 72 6e 20 69 64 78 3b 0a 7d 0a    return idx;.}.
2e59b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2e59c 69 6e 65 20 69 64 65 6e 74 69 66 69 65 73 20 73  ine identifies s
2e59d 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  ubexpressions in
2e59e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2e59f 65 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20  e where.** each 
2e5a0 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73  subexpression is
2e5a1 20 73 65 70 61 72 61 74 65 64 20 62 79 20 74 68   separated by th
2e5a2 65 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 20 6f  e AND operator o
2e5a3 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20  r some other.** 
2e5a4 6f 70 65 72 61 74 6f 72 20 73 70 65 63 69 66 69  operator specifi
2e5a5 65 64 20 69 6e 20 74 68 65 20 6f 70 20 70 61 72  ed in the op par
2e5a6 61 6d 65 74 65 72 2e 20 20 54 68 65 20 57 68 65  ameter.  The Whe
2e5a7 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75  reClause structu
2e5a8 72 65 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20  re.** is filled 
2e5a9 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f  with pointers to
2e5aa 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 2e   subexpressions.
2e5ab 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a    For example:.*
2e5ac 2a 0a 2a 2a 20 20 20 20 57 48 45 52 45 20 20 61  *.**    WHERE  a
2e5ad 3d 3d 27 68 65 6c 6c 6f 27 20 41 4e 44 20 63 6f  =='hello' AND co
2e5ae 61 6c 65 73 63 65 28 62 2c 31 31 29 3c 31 30 20  alesce(b,11)<10 
2e5af 41 4e 44 20 28 63 2b 31 32 21 3d 64 20 4f 52 20  AND (c+12!=d OR 
2e5b0 63 3d 3d 32 32 29 0a 2a 2a 20 20 20 20 20 20 20  c==22).**       
2e5b1 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20      \________/  
2e5b2 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f     \____________
2e5b3 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f  ___/     \______
2e5b4 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20  __________/.**  
2e5b5 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 30            slot[0
2e5b6 5d 20 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f  ]            slo
2e5b7 74 5b 31 5d 20 20 20 20 20 20 20 20 20 20 20 20  t[1]            
2e5b8 20 20 20 73 6c 6f 74 5b 32 5d 0a 2a 2a 0a 2a 2a     slot[2].**.**
2e5b9 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 57 48   The original WH
2e5ba 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 70 45  ERE clause in pE
2e5bb 78 70 72 20 69 73 20 75 6e 61 6c 74 65 72 65 64  xpr is unaltered
2e5bc 2e 20 20 41 6c 6c 20 74 68 69 73 20 72 6f 75 74  .  All this rout
2e5bd 69 6e 65 0a 2a 2a 20 64 6f 65 73 20 69 73 20 6d  ine.** does is m
2e5be 61 6b 65 20 73 6c 6f 74 5b 5d 20 65 6e 74 72 69  ake slot[] entri
2e5bf 65 73 20 70 6f 69 6e 74 20 74 6f 20 73 75 62 73  es point to subs
2e5c0 74 72 75 63 74 75 72 65 20 77 69 74 68 69 6e 20  tructure within 
2e5c1 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  pExpr..**.** In 
2e5c2 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e  the previous sen
2e5c3 74 65 6e 63 65 20 61 6e 64 20 69 6e 20 74 68 65  tence and in the
2e5c4 20 64 69 61 67 72 61 6d 2c 20 22 73 6c 6f 74 5b   diagram, "slot[
2e5c5 5d 22 20 72 65 66 65 72 73 20 74 6f 0a 2a 2a 20  ]" refers to.** 
2e5c6 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 2e  the WhereClause.
2e5c7 61 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 65 20  a[] array.  The 
2e5c8 73 6c 6f 74 5b 5d 20 61 72 72 61 79 20 67 72 6f  slot[] array gro
2e5c9 77 73 20 61 73 20 6e 65 65 64 65 64 20 74 6f 20  ws as needed to 
2e5ca 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 6c 6c 20 74  contain.** all t
2e5cb 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52  erms of the WHER
2e5cc 45 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 61  E clause..*/.sta
2e5cd 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 53 70  tic void whereSp
2e5ce 6c 69 74 28 57 68 65 72 65 43 6c 61 75 73 65 20  lit(WhereClause 
2e5cf 2a 70 57 43 2c 20 45 78 70 72 20 2a 70 45 78 70  *pWC, Expr *pExp
2e5d0 72 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 70 57  r, int op){.  pW
2e5d1 43 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a  C->op = (u8)op;.
2e5d2 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29    if( pExpr==0 )
2e5d3 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70   return;.  if( p
2e5d4 45 78 70 72 2d 3e 6f 70 21 3d 6f 70 20 29 7b 0a  Expr->op!=op ){.
2e5d5 20 20 20 20 77 68 65 72 65 43 6c 61 75 73 65 49      whereClauseI
2e5d6 6e 73 65 72 74 28 70 57 43 2c 20 70 45 78 70 72  nsert(pWC, pExpr
2e5d7 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  , 0);.  }else{. 
2e5d8 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 70 57     whereSplit(pW
2e5d9 43 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  C, pExpr->pLeft,
2e5da 20 6f 70 29 3b 0a 20 20 20 20 77 68 65 72 65 53   op);.    whereS
2e5db 70 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72 2d  plit(pWC, pExpr-
2e5dc 3e 70 52 69 67 68 74 2c 20 6f 70 29 3b 0a 20 20  >pRight, op);.  
2e5dd 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  }.}../*.** Initi
2e5de 61 6c 69 7a 65 20 61 6e 20 65 78 70 72 65 73 73  alize an express
2e5df 69 6f 6e 20 6d 61 73 6b 20 73 65 74 20 28 61 20  ion mask set (a 
2e5e0 57 68 65 72 65 4d 61 73 6b 53 65 74 20 6f 62 6a  WhereMaskSet obj
2e5e1 65 63 74 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ect).*/.#define 
2e5e2 69 6e 69 74 4d 61 73 6b 53 65 74 28 50 29 20 20  initMaskSet(P)  
2e5e3 6d 65 6d 73 65 74 28 50 2c 20 30 2c 20 73 69 7a  memset(P, 0, siz
2e5e4 65 6f 66 28 2a 50 29 29 0a 0a 2f 2a 0a 2a 2a 20  eof(*P))../*.** 
2e5e5 52 65 74 75 72 6e 20 74 68 65 20 62 69 74 6d 61  Return the bitma
2e5e6 73 6b 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  sk for the given
2e5e7 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e 20   cursor number. 
2e5e8 20 52 65 74 75 72 6e 20 30 20 69 66 0a 2a 2a 20   Return 0 if.** 
2e5e9 69 43 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 69  iCursor is not i
2e5ea 6e 20 74 68 65 20 73 65 74 2e 0a 2a 2f 0a 73 74  n the set..*/.st
2e5eb 61 74 69 63 20 42 69 74 6d 61 73 6b 20 67 65 74  atic Bitmask get
2e5ec 4d 61 73 6b 28 57 68 65 72 65 4d 61 73 6b 53 65  Mask(WhereMaskSe
2e5ed 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 69 6e 74  t *pMaskSet, int
2e5ee 20 69 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74   iCursor){.  int
2e5ef 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   i;.  assert( pM
2e5f0 61 73 6b 53 65 74 2d 3e 6e 3c 3d 73 69 7a 65 6f  askSet->n<=sizeo
2e5f1 66 28 42 69 74 6d 61 73 6b 29 2a 38 20 29 3b 0a  f(Bitmask)*8 );.
2e5f2 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4d 61    for(i=0; i<pMa
2e5f3 73 6b 53 65 74 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a  skSet->n; i++){.
2e5f4 20 20 20 20 69 66 28 20 70 4d 61 73 6b 53 65 74      if( pMaskSet
2e5f5 2d 3e 69 78 5b 69 5d 3d 3d 69 43 75 72 73 6f 72  ->ix[i]==iCursor
2e5f6 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2e5f7 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 69   ((Bitmask)1)<<i
2e5f8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2e5f9 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
2e5fa 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6d 61   Create a new ma
2e5fb 73 6b 20 66 6f 72 20 63 75 72 73 6f 72 20 69 43  sk for cursor iC
2e5fc 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ursor..**.** The
2e5fd 72 65 20 69 73 20 6f 6e 65 20 63 75 72 73 6f 72  re is one cursor
2e5fe 20 70 65 72 20 74 61 62 6c 65 20 69 6e 20 74 68   per table in th
2e5ff 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20  e FROM clause.  
2e600 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  The number of.**
2e601 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46   tables in the F
2e602 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 6c 69  ROM clause is li
2e603 6d 69 74 65 64 20 62 79 20 61 20 74 65 73 74 20  mited by a test 
2e604 65 61 72 6c 79 20 69 6e 20 74 68 65 0a 2a 2a 20  early in the.** 
2e605 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
2e606 6e 28 29 20 72 6f 75 74 69 6e 65 2e 20 20 53 6f  n() routine.  So
2e607 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68   we know that th
2e608 65 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 5d  e pMaskSet->ix[]
2e609 0a 2a 2a 20 61 72 72 61 79 20 77 69 6c 6c 20 6e  .** array will n
2e60a 65 76 65 72 20 6f 76 65 72 66 6c 6f 77 2e 0a 2a  ever overflow..*
2e60b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 72  /.static void cr
2e60c 65 61 74 65 4d 61 73 6b 28 57 68 65 72 65 4d 61  eateMask(WhereMa
2e60d 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c  skSet *pMaskSet,
2e60e 20 69 6e 74 20 69 43 75 72 73 6f 72 29 7b 0a 20   int iCursor){. 
2e60f 20 61 73 73 65 72 74 28 20 70 4d 61 73 6b 53 65   assert( pMaskSe
2e610 74 2d 3e 6e 20 3c 20 41 72 72 61 79 53 69 7a 65  t->n < ArraySize
2e611 28 70 4d 61 73 6b 53 65 74 2d 3e 69 78 29 20 29  (pMaskSet->ix) )
2e612 3b 0a 20 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78  ;.  pMaskSet->ix
2e613 5b 70 4d 61 73 6b 53 65 74 2d 3e 6e 2b 2b 5d 20  [pMaskSet->n++] 
2e614 3d 20 69 43 75 72 73 6f 72 3b 0a 7d 0a 0a 2f 2a  = iCursor;.}../*
2e615 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2e616 20 77 61 6c 6b 73 20 28 72 65 63 75 72 73 69 76   walks (recursiv
2e617 65 6c 79 29 20 61 6e 20 65 78 70 72 65 73 73 69  ely) an expressi
2e618 6f 6e 20 74 72 65 65 20 61 6e 64 20 67 65 6e 65  on tree and gene
2e619 72 61 74 65 73 0a 2a 2a 20 61 20 62 69 74 6d 61  rates.** a bitma
2e61a 73 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68  sk indicating wh
2e61b 69 63 68 20 74 61 62 6c 65 73 20 61 72 65 20 75  ich tables are u
2e61c 73 65 64 20 69 6e 20 74 68 61 74 20 65 78 70 72  sed in that expr
2e61d 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a  ession.** tree..
2e61e 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66  **.** In order f
2e61f 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  or this routine 
2e620 74 6f 20 77 6f 72 6b 2c 20 74 68 65 20 63 61 6c  to work, the cal
2e621 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75  ling function mu
2e622 73 74 20 68 61 76 65 0a 2a 2a 20 70 72 65 76 69  st have.** previ
2e623 6f 75 73 6c 79 20 69 6e 76 6f 6b 65 64 20 73 71  ously invoked sq
2e624 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
2e625 4e 61 6d 65 73 28 29 20 6f 6e 20 74 68 65 20 65  Names() on the e
2e626 78 70 72 65 73 73 69 6f 6e 2e 20 20 53 65 65 0a  xpression.  See.
2e627 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 63 6f  ** the header co
2e628 6d 6d 65 6e 74 20 6f 6e 20 74 68 61 74 20 72 6f  mment on that ro
2e629 75 74 69 6e 65 20 66 6f 72 20 61 64 64 69 74 69  utine for additi
2e62a 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
2e62b 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  ..** The sqlite3
2e62c 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
2e62d 28 29 20 72 6f 75 74 69 6e 65 73 20 6c 6f 6f 6b  () routines look
2e62e 73 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61 6d  s for column nam
2e62f 65 73 20 61 6e 64 0a 2a 2a 20 73 65 74 73 20 74  es and.** sets t
2e630 68 65 69 72 20 6f 70 63 6f 64 65 73 20 74 6f 20  heir opcodes to 
2e631 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 74 68  TK_COLUMN and th
2e632 65 69 72 20 45 78 70 72 2e 69 54 61 62 6c 65 20  eir Expr.iTable 
2e633 66 69 65 6c 64 73 20 74 6f 0a 2a 2a 20 74 68 65  fields to.** the
2e634 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d   VDBE cursor num
2e635 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ber of the table
2e636 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
2e637 6a 75 73 74 20 68 61 73 20 74 6f 0a 2a 2a 20 74  just has to.** t
2e638 72 61 6e 73 6c 61 74 65 20 74 68 65 20 63 75 72  ranslate the cur
2e639 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f  sor numbers into
2e63a 20 62 69 74 6d 61 73 6b 20 76 61 6c 75 65 73 20   bitmask values 
2e63b 61 6e 64 20 4f 52 20 61 6c 6c 0a 2a 2a 20 74 68  and OR all.** th
2e63c 65 20 62 69 74 6d 61 73 6b 73 20 74 6f 67 65 74  e bitmasks toget
2e63d 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42  her..*/.static B
2e63e 69 74 6d 61 73 6b 20 65 78 70 72 4c 69 73 74 54  itmask exprListT
2e63f 61 62 6c 65 55 73 61 67 65 28 57 68 65 72 65 4d  ableUsage(WhereM
2e640 61 73 6b 53 65 74 2a 2c 20 45 78 70 72 4c 69 73  askSet*, ExprLis
2e641 74 2a 29 3b 0a 73 74 61 74 69 63 20 42 69 74 6d  t*);.static Bitm
2e642 61 73 6b 20 65 78 70 72 53 65 6c 65 63 74 54 61  ask exprSelectTa
2e643 62 6c 65 55 73 61 67 65 28 57 68 65 72 65 4d 61  bleUsage(WhereMa
2e644 73 6b 53 65 74 2a 2c 20 53 65 6c 65 63 74 2a 29  skSet*, Select*)
2e645 3b 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b  ;.static Bitmask
2e646 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
2e647 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d  WhereMaskSet *pM
2e648 61 73 6b 53 65 74 2c 20 45 78 70 72 20 2a 70 29  askSet, Expr *p)
2e649 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b  {.  Bitmask mask
2e64a 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 3d 3d 30   = 0;.  if( p==0
2e64b 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
2e64c 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  f( p->op==TK_COL
2e64d 55 4d 4e 20 29 7b 0a 20 20 20 20 6d 61 73 6b 20  UMN ){.    mask 
2e64e 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53  = getMask(pMaskS
2e64f 65 74 2c 20 70 2d 3e 69 54 61 62 6c 65 29 3b 0a  et, p->iTable);.
2e650 20 20 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b      return mask;
2e651 0a 20 20 7d 0a 20 20 6d 61 73 6b 20 3d 20 65 78  .  }.  mask = ex
2e652 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
2e653 73 6b 53 65 74 2c 20 70 2d 3e 70 52 69 67 68 74  skSet, p->pRight
2e654 29 3b 0a 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  );.  mask |= exp
2e655 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  rTableUsage(pMas
2e656 6b 53 65 74 2c 20 70 2d 3e 70 4c 65 66 74 29 3b  kSet, p->pLeft);
2e657 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72  .  if( ExprHasPr
2e658 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73  operty(p, EP_xIs
2e659 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 6d  Select) ){.    m
2e65a 61 73 6b 20 7c 3d 20 65 78 70 72 53 65 6c 65 63  ask |= exprSelec
2e65b 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  tTableUsage(pMas
2e65c 6b 53 65 74 2c 20 70 2d 3e 78 2e 70 53 65 6c 65  kSet, p->x.pSele
2e65d 63 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ct);.  }else{.  
2e65e 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69    mask |= exprLi
2e65f 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  stTableUsage(pMa
2e660 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70 4c 69 73  skSet, p->x.pLis
2e661 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  t);.  }.  return
2e662 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20   mask;.}.static 
2e663 42 69 74 6d 61 73 6b 20 65 78 70 72 4c 69 73 74  Bitmask exprList
2e664 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72 65  TableUsage(Where
2e665 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2e666 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  t, ExprList *pLi
2e667 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  st){.  int i;.  
2e668 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30  Bitmask mask = 0
2e669 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b  ;.  if( pList ){
2e66a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2e66b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  pList->nExpr; i+
2e66c 2b 29 7b 0a 20 20 20 20 20 20 6d 61 73 6b 20 7c  +){.      mask |
2e66d 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
2e66e 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c 69 73 74  (pMaskSet, pList
2e66f 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20  ->a[i].pExpr);. 
2e670 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2e671 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74 69 63  n mask;.}.static
2e672 20 42 69 74 6d 61 73 6b 20 65 78 70 72 53 65 6c   Bitmask exprSel
2e673 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 57 68  ectTableUsage(Wh
2e674 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2e675 6b 53 65 74 2c 20 53 65 6c 65 63 74 20 2a 70 53  kSet, Select *pS
2e676 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61 73  ){.  Bitmask mas
2e677 6b 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  k = 0;.  while( 
2e678 70 53 20 29 7b 0a 20 20 20 20 6d 61 73 6b 20 7c  pS ){.    mask |
2e679 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55  = exprListTableU
2e67a 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2e67b 53 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  S->pEList);.    
2e67c 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74  mask |= exprList
2e67d 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2e67e 53 65 74 2c 20 70 53 2d 3e 70 47 72 6f 75 70 42  Set, pS->pGroupB
2e67f 79 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20  y);.    mask |= 
2e680 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61  exprListTableUsa
2e681 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d  ge(pMaskSet, pS-
2e682 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20  >pOrderBy);.    
2e683 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c  mask |= exprTabl
2e684 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
2e685 20 70 53 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20   pS->pWhere);.  
2e686 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61    mask |= exprTa
2e687 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2e688 74 2c 20 70 53 2d 3e 70 48 61 76 69 6e 67 29 3b  t, pS->pHaving);
2e689 0a 20 20 20 20 70 53 20 3d 20 70 53 2d 3e 70 50  .    pS = pS->pP
2e68a 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rior;.  }.  retu
2e68b 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn mask;.}../*.*
2e68c 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
2e68d 20 74 68 65 20 67 69 76 65 6e 20 6f 70 65 72 61   the given opera
2e68e 74 6f 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  tor is one of th
2e68f 65 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61 74  e operators that
2e690 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 66   is.** allowed f
2e691 6f 72 20 61 6e 20 69 6e 64 65 78 61 62 6c 65 20  or an indexable 
2e692 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
2e693 6d 2e 20 20 54 68 65 20 61 6c 6c 6f 77 65 64 20  m.  The allowed 
2e694 6f 70 65 72 61 74 6f 72 73 20 61 72 65 0a 2a 2a  operators are.**
2e695 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3e 22 2c 20   "=", "<", ">", 
2e696 22 3c 3d 22 2c 20 22 3e 3d 22 2c 20 61 6e 64 20  "<=", ">=", and 
2e697 22 49 4e 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  "IN"..*/.static 
2e698 69 6e 74 20 61 6c 6c 6f 77 65 64 4f 70 28 69 6e  int allowedOp(in
2e699 74 20 6f 70 29 7b 0a 20 20 61 73 73 65 72 74 28  t op){.  assert(
2e69a 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20 26 26 20   TK_GT>TK_EQ && 
2e69b 54 4b 5f 47 54 3c 54 4b 5f 47 45 20 29 3b 0a 20  TK_GT<TK_GE );. 
2e69c 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3e 54   assert( TK_LT>T
2e69d 4b 5f 45 51 20 26 26 20 54 4b 5f 4c 54 3c 54 4b  K_EQ && TK_LT<TK
2e69e 5f 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _GE );.  assert(
2e69f 20 54 4b 5f 4c 45 3e 54 4b 5f 45 51 20 26 26 20   TK_LE>TK_EQ && 
2e6a0 54 4b 5f 4c 45 3c 54 4b 5f 47 45 20 29 3b 0a 20  TK_LE<TK_GE );. 
2e6a1 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d   assert( TK_GE==
2e6a2 54 4b 5f 45 51 2b 34 20 29 3b 0a 20 20 72 65 74  TK_EQ+4 );.  ret
2e6a3 75 72 6e 20 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c  urn op==TK_IN ||
2e6a4 20 28 6f 70 3e 3d 54 4b 5f 45 51 20 26 26 20 6f   (op>=TK_EQ && o
2e6a5 70 3c 3d 54 4b 5f 47 45 29 20 7c 7c 20 6f 70 3d  p<=TK_GE) || op=
2e6a6 3d 54 4b 5f 49 53 4e 55 4c 4c 3b 0a 7d 0a 0a 2f  =TK_ISNULL;.}../
2e6a7 2a 0a 2a 2a 20 53 77 61 70 20 74 77 6f 20 6f 62  *.** Swap two ob
2e6a8 6a 65 63 74 73 20 6f 66 20 74 79 70 65 20 54 59  jects of type TY
2e6a9 50 45 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  PE..*/.#define S
2e6aa 57 41 50 28 54 59 50 45 2c 41 2c 42 29 20 7b 54  WAP(TYPE,A,B) {T
2e6ab 59 50 45 20 74 3d 41 3b 20 41 3d 42 3b 20 42 3d  YPE t=A; A=B; B=
2e6ac 74 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 75  t;}../*.** Commu
2e6ad 74 65 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20  te a comparison 
2e6ae 6f 70 65 72 61 74 6f 72 2e 20 20 45 78 70 72 65  operator.  Expre
2e6af 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f  ssions of the fo
2e6b0 72 6d 20 22 58 20 6f 70 20 59 22 0a 2a 2a 20 61  rm "X op Y".** a
2e6b1 72 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  re converted int
2e6b2 6f 20 22 59 20 6f 70 20 58 22 2e 0a 2a 2a 0a 2a  o "Y op X"..**.*
2e6b3 2a 20 49 66 20 61 20 63 6f 6c 6c 61 74 69 6f 6e  * If a collation
2e6b4 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 73 73   sequence is ass
2e6b5 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 69 74  ociated with eit
2e6b6 68 65 72 20 74 68 65 20 6c 65 66 74 20 6f 72 20  her the left or 
2e6b7 72 69 67 68 74 0a 2a 2a 20 73 69 64 65 20 6f 66  right.** side of
2e6b8 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2c   the comparison,
2e6b9 20 69 74 20 72 65 6d 61 69 6e 73 20 61 73 73 6f   it remains asso
2e6ba 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
2e6bb 73 61 6d 65 20 73 69 64 65 20 61 66 74 65 72 0a  same side after.
2e6bc 2a 2a 20 74 68 65 20 63 6f 6d 6d 75 74 61 74 69  ** the commutati
2e6bd 6f 6e 2e 20 53 6f 20 22 59 20 63 6f 6c 6c 61 74  on. So "Y collat
2e6be 65 20 4e 4f 43 41 53 45 20 6f 70 20 58 22 20 62  e NOCASE op X" b
2e6bf 65 63 6f 6d 65 73 20 0a 2a 2a 20 22 58 20 63 6f  ecomes .** "X co
2e6c0 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f 70 20  llate NOCASE op 
2e6c1 59 22 2e 20 54 68 69 73 20 69 73 20 62 65 63 61  Y". This is beca
2e6c2 75 73 65 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f  use any collatio
2e6c3 6e 20 73 65 71 75 65 6e 63 65 20 6f 6e 0a 2a 2a  n sequence on.**
2e6c4 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20 73   the left hand s
2e6c5 69 64 65 20 6f 66 20 61 20 63 6f 6d 70 61 72 69  ide of a compari
2e6c6 73 6f 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e  son overrides an
2e6c7 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  y collation sequ
2e6c8 65 6e 63 65 20 0a 2a 2a 20 61 74 74 61 63 68 65  ence .** attache
2e6c9 64 20 74 6f 20 74 68 65 20 72 69 67 68 74 2e 20  d to the right. 
2e6ca 46 6f 72 20 74 68 65 20 73 61 6d 65 20 72 65 61  For the same rea
2e6cb 73 6f 6e 20 74 68 65 20 45 50 5f 45 78 70 43 6f  son the EP_ExpCo
2e6cc 6c 6c 61 74 65 20 66 6c 61 67 0a 2a 2a 20 69 73  llate flag.** is
2e6cd 20 6e 6f 74 20 63 6f 6d 6d 75 74 65 64 2e 0a 2a   not commuted..*
2e6ce 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78  /.static void ex
2e6cf 70 72 43 6f 6d 6d 75 74 65 28 50 61 72 73 65 20  prCommute(Parse 
2e6d0 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
2e6d1 45 78 70 72 29 7b 0a 20 20 75 31 36 20 65 78 70  Expr){.  u16 exp
2e6d2 52 69 67 68 74 20 3d 20 28 70 45 78 70 72 2d 3e  Right = (pExpr->
2e6d3 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20  pRight->flags & 
2e6d4 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 3b 0a  EP_ExpCollate);.
2e6d5 20 20 75 31 36 20 65 78 70 4c 65 66 74 20 3d 20    u16 expLeft = 
2e6d6 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66  (pExpr->pLeft->f
2e6d7 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
2e6d8 6c 61 74 65 29 3b 0a 20 20 61 73 73 65 72 74 28  late);.  assert(
2e6d9 20 61 6c 6c 6f 77 65 64 4f 70 28 70 45 78 70 72   allowedOp(pExpr
2e6da 2d 3e 6f 70 29 20 26 26 20 70 45 78 70 72 2d 3e  ->op) && pExpr->
2e6db 6f 70 21 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 70  op!=TK_IN );.  p
2e6dc 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 70 43  Expr->pRight->pC
2e6dd 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
2e6de 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
2e6df 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b   pExpr->pRight);
2e6e0 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d  .  pExpr->pLeft-
2e6e1 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33  >pColl = sqlite3
2e6e2 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
2e6e3 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
2e6e4 29 3b 0a 20 20 53 57 41 50 28 43 6f 6c 6c 53 65  );.  SWAP(CollSe
2e6e5 71 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68 74  q*,pExpr->pRight
2e6e6 2d 3e 70 43 6f 6c 6c 2c 70 45 78 70 72 2d 3e 70  ->pColl,pExpr->p
2e6e7 4c 65 66 74 2d 3e 70 43 6f 6c 6c 29 3b 0a 20 20  Left->pColl);.  
2e6e8 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66  pExpr->pRight->f
2e6e9 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d 3e 70  lags = (pExpr->p
2e6ea 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 7e  Right->flags & ~
2e6eb 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c  EP_ExpCollate) |
2e6ec 20 65 78 70 4c 65 66 74 3b 0a 20 20 70 45 78 70   expLeft;.  pExp
2e6ed 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20  r->pLeft->flags 
2e6ee 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d  = (pExpr->pLeft-
2e6ef 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f 45 78 70  >flags & ~EP_Exp
2e6f0 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78 70 52 69  Collate) | expRi
2e6f1 67 68 74 3b 0a 20 20 53 57 41 50 28 45 78 70 72  ght;.  SWAP(Expr
2e6f2 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  *,pExpr->pRight,
2e6f3 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20  pExpr->pLeft);. 
2e6f4 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3e 3d   if( pExpr->op>=
2e6f5 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20 61 73 73  TK_GT ){.    ass
2e6f6 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54 4b 5f 47  ert( TK_LT==TK_G
2e6f7 54 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72  T+2 );.    asser
2e6f8 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 4c 45 2b  t( TK_GE==TK_LE+
2e6f9 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  2 );.    assert(
2e6fa 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20 29 3b 0a   TK_GT>TK_EQ );.
2e6fb 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47      assert( TK_G
2e6fc 54 3c 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 61  T<TK_LE );.    a
2e6fd 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70  ssert( pExpr->op
2e6fe 3e 3d 54 4b 5f 47 54 20 26 26 20 70 45 78 70 72  >=TK_GT && pExpr
2e6ff 2d 3e 6f 70 3c 3d 54 4b 5f 47 45 20 29 3b 0a 20  ->op<=TK_GE );. 
2e700 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 28     pExpr->op = (
2e701 28 70 45 78 70 72 2d 3e 6f 70 2d 54 4b 5f 47 54  (pExpr->op-TK_GT
2e702 29 5e 32 29 2b 54 4b 5f 47 54 3b 0a 20 20 7d 0a  )^2)+TK_GT;.  }.
2e703 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61  }../*.** Transla
2e704 74 65 20 66 72 6f 6d 20 54 4b 5f 78 78 20 6f 70  te from TK_xx op
2e705 65 72 61 74 6f 72 20 74 6f 20 57 4f 5f 78 78 20  erator to WO_xx 
2e706 62 69 74 6d 61 73 6b 2e 0a 2a 2f 0a 73 74 61 74  bitmask..*/.stat
2e707 69 63 20 75 31 36 20 6f 70 65 72 61 74 6f 72 4d  ic u16 operatorM
2e708 61 73 6b 28 69 6e 74 20 6f 70 29 7b 0a 20 20 75  ask(int op){.  u
2e709 31 36 20 63 3b 0a 20 20 61 73 73 65 72 74 28 20  16 c;.  assert( 
2e70a 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20 29 3b  allowedOp(op) );
2e70b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e  .  if( op==TK_IN
2e70c 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49   ){.    c = WO_I
2e70d 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  N;.  }else if( o
2e70e 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a  p==TK_ISNULL ){.
2e70f 20 20 20 20 63 20 3d 20 57 4f 5f 49 53 4e 55 4c      c = WO_ISNUL
2e710 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  L;.  }else{.    
2e711 61 73 73 65 72 74 28 20 28 57 4f 5f 45 51 3c 3c  assert( (WO_EQ<<
2e712 28 6f 70 2d 54 4b 5f 45 51 29 29 20 3c 20 30 78  (op-TK_EQ)) < 0x
2e713 37 66 66 66 20 29 3b 0a 20 20 20 20 63 20 3d 20  7fff );.    c = 
2e714 28 75 31 36 29 28 57 4f 5f 45 51 3c 3c 28 6f 70  (u16)(WO_EQ<<(op
2e715 2d 54 4b 5f 45 51 29 29 3b 0a 20 20 7d 0a 20 20  -TK_EQ));.  }.  
2e716 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49  assert( op!=TK_I
2e717 53 4e 55 4c 4c 20 7c 7c 20 63 3d 3d 57 4f 5f 49  SNULL || c==WO_I
2e718 53 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72  SNULL );.  asser
2e719 74 28 20 6f 70 21 3d 54 4b 5f 49 4e 20 7c 7c 20  t( op!=TK_IN || 
2e71a 63 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 61 73  c==WO_IN );.  as
2e71b 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 45 51 20  sert( op!=TK_EQ 
2e71c 7c 7c 20 63 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20  || c==WO_EQ );. 
2e71d 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
2e71e 4c 54 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 54 20 29  LT || c==WO_LT )
2e71f 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  ;.  assert( op!=
2e720 54 4b 5f 4c 45 20 7c 7c 20 63 3d 3d 57 4f 5f 4c  TK_LE || c==WO_L
2e721 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  E );.  assert( o
2e722 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 63 3d 3d 57  p!=TK_GT || c==W
2e723 4f 5f 47 54 20 29 3b 0a 20 20 61 73 73 65 72 74  O_GT );.  assert
2e724 28 20 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20 63  ( op!=TK_GE || c
2e725 3d 3d 57 4f 5f 47 45 20 29 3b 0a 20 20 72 65 74  ==WO_GE );.  ret
2e726 75 72 6e 20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn c;.}../*.** 
2e727 53 65 61 72 63 68 20 66 6f 72 20 61 20 74 65 72  Search for a ter
2e728 6d 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  m in the WHERE c
2e729 6c 61 75 73 65 20 74 68 61 74 20 69 73 20 6f 66  lause that is of
2e72a 20 74 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f 70   the form "X <op
2e72b 3e 20 3c 65 78 70 72 3e 22 0a 2a 2a 20 77 68 65  > <expr>".** whe
2e72c 72 65 20 58 20 69 73 20 61 20 72 65 66 65 72 65  re X is a refere
2e72d 6e 63 65 20 74 6f 20 74 68 65 20 69 43 6f 6c 75  nce to the iColu
2e72e 6d 6e 20 6f 66 20 74 61 62 6c 65 20 69 43 75 72  mn of table iCur
2e72f 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e 65   and <op> is one
2e730 20 6f 66 0a 2a 2a 20 74 68 65 20 57 4f 5f 78 78   of.** the WO_xx
2e731 20 6f 70 65 72 61 74 6f 72 20 63 6f 64 65 73 20   operator codes 
2e732 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
2e733 20 6f 70 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a   op parameter..*
2e734 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
2e735 65 72 20 74 6f 20 74 68 65 20 74 65 72 6d 2e 20  er to the term. 
2e736 20 52 65 74 75 72 6e 20 30 20 69 66 20 6e 6f 74   Return 0 if not
2e737 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69   found..*/.stati
2e738 63 20 57 68 65 72 65 54 65 72 6d 20 2a 66 69 6e  c WhereTerm *fin
2e739 64 54 65 72 6d 28 0a 20 20 57 68 65 72 65 43 6c  dTerm(.  WhereCl
2e73a 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 2f  ause *pWC,     /
2e73b 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
2e73c 73 65 20 74 6f 20 62 65 20 73 65 61 72 63 68 65  se to be searche
2e73d 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c  d */.  int iCur,
2e73e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e73f 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  Cursor number of
2e740 20 4c 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69 43   LHS */.  int iC
2e741 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
2e742 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
2e743 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20 42 69 74   of LHS */.  Bit
2e744 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20  mask notReady,  
2e745 20 20 20 2f 2a 20 52 48 53 20 6d 75 73 74 20 6e     /* RHS must n
2e746 6f 74 20 6f 76 65 72 6c 61 70 20 77 69 74 68 20  ot overlap with 
2e747 74 68 69 73 20 6d 61 73 6b 20 2a 2f 0a 20 20 75  this mask */.  u
2e748 33 32 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20  32 op,          
2e749 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
2e74a 57 4f 5f 78 78 20 76 61 6c 75 65 73 20 64 65 73  WO_xx values des
2e74b 63 72 69 62 69 6e 67 20 6f 70 65 72 61 74 6f 72  cribing operator
2e74c 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
2e74d 78 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  x           /* M
2e74e 75 73 74 20 62 65 20 63 6f 6d 70 61 74 69 62 6c  ust be compatibl
2e74f 65 20 77 69 74 68 20 74 68 69 73 20 69 6e 64 65  e with this inde
2e750 78 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a  x, if not NULL *
2e751 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d  /.){.  WhereTerm
2e752 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6b   *pTerm;.  int k
2e753 3b 0a 20 20 61 73 73 65 72 74 28 20 69 43 75 72  ;.  assert( iCur
2e754 3e 3d 30 20 29 3b 0a 20 20 6f 70 20 26 3d 20 57  >=0 );.  op &= W
2e755 4f 5f 41 4c 4c 3b 0a 20 20 66 6f 72 28 70 54 65  O_ALL;.  for(pTe
2e756 72 6d 3d 70 57 43 2d 3e 61 2c 20 6b 3d 70 57 43  rm=pWC->a, k=pWC
2e757 2d 3e 6e 54 65 72 6d 3b 20 6b 3b 20 6b 2d 2d 2c  ->nTerm; k; k--,
2e758 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69   pTerm++){.    i
2e759 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75  f( pTerm->leftCu
2e75a 72 73 6f 72 3d 3d 69 43 75 72 0a 20 20 20 20 20  rsor==iCur.     
2e75b 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 70 72 65    && (pTerm->pre
2e75c 72 65 71 52 69 67 68 74 20 26 20 6e 6f 74 52 65  reqRight & notRe
2e75d 61 64 79 29 3d 3d 30 0a 20 20 20 20 20 20 20 26  ady)==0.       &
2e75e 26 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43  & pTerm->u.leftC
2e75f 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20  olumn==iColumn. 
2e760 20 20 20 20 20 20 26 26 20 28 70 54 65 72 6d 2d        && (pTerm-
2e761 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 6f 70 29  >eOperator & op)
2e762 21 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  !=0.    ){.     
2e763 20 69 66 28 20 70 49 64 78 20 26 26 20 70 54 65   if( pIdx && pTe
2e764 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57  rm->eOperator!=W
2e765 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  O_ISNULL ){.    
2e766 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70      Expr *pX = p
2e767 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Term->pExpr;.   
2e768 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43       CollSeq *pC
2e769 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 63 68 61  oll;.        cha
2e76a 72 20 69 64 78 61 66 66 3b 0a 20 20 20 20 20 20  r idxaff;.      
2e76b 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20    int j;.       
2e76c 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d   Parse *pParse =
2e76d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 0a 0a 20   pWC->pParse;.. 
2e76e 20 20 20 20 20 20 20 69 64 78 61 66 66 20 3d 20         idxaff = 
2e76f 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 61 43  pIdx->pTable->aC
2e770 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69  ol[iColumn].affi
2e771 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 69 66  nity;.        if
2e772 28 20 21 73 71 6c 69 74 65 33 49 6e 64 65 78 41  ( !sqlite3IndexA
2e773 66 66 69 6e 69 74 79 4f 6b 28 70 58 2c 20 69 64  ffinityOk(pX, id
2e774 78 61 66 66 29 20 29 20 63 6f 6e 74 69 6e 75 65  xaff) ) continue
2e775 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ;..        /* Fi
2e776 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f 6c  gure out the col
2e777 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2e778 72 65 71 75 69 72 65 64 20 66 72 6f 6d 20 61 6e  required from an
2e779 20 69 6e 64 65 78 20 66 6f 72 0a 20 20 20 20 20   index for.     
2e77a 20 20 20 2a 2a 20 69 74 20 74 6f 20 62 65 20 75     ** it to be u
2e77b 73 65 66 75 6c 20 66 6f 72 20 6f 70 74 69 6d 69  seful for optimi
2e77c 73 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 20  sing expression 
2e77d 70 58 2e 20 53 74 6f 72 65 20 74 68 69 73 0a 20  pX. Store this. 
2e77e 20 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 20         ** value 
2e77f 69 6e 20 76 61 72 69 61 62 6c 65 20 70 43 6f 6c  in variable pCol
2e780 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  l..        */.  
2e781 20 20 20 20 20 20 61 73 73 65 72 74 28 70 58 2d        assert(pX-
2e782 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 20  >pLeft);.       
2e783 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
2e784 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
2e785 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 58 2d  lSeq(pParse, pX-
2e786 3e 70 4c 65 66 74 2c 20 70 58 2d 3e 70 52 69 67  >pLeft, pX->pRig
2e787 68 74 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  ht);.        ass
2e788 65 72 74 28 70 43 6f 6c 6c 20 7c 7c 20 70 50 61  ert(pColl || pPa
2e789 72 73 65 2d 3e 6e 45 72 72 29 3b 0a 0a 20 20 20  rse->nErr);..   
2e78a 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 70 49       for(j=0; pI
2e78b 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 21  dx->aiColumn[j]!
2e78c 3d 69 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a  =iColumn; j++){.
2e78d 20 20 20 20 20 20 20 20 20 20 69 66 28 20 4e 45            if( NE
2e78e 56 45 52 28 6a 3e 3d 70 49 64 78 2d 3e 6e 43 6f  VER(j>=pIdx->nCo
2e78f 6c 75 6d 6e 29 20 29 20 72 65 74 75 72 6e 20 30  lumn) ) return 0
2e790 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2e791 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 26 26      if( pColl &&
2e792 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2e793 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 70 49  pColl->zName, pI
2e794 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6a 5d 29 20 29  dx->azColl[j]) )
2e795 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2e796 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   }.      return 
2e797 70 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d  pTerm;.    }.  }
2e798 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
2e799 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72  /* Forward refer
2e79a 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 76  ence */.static v
2e79b 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 28  oid exprAnalyze(
2e79c 53 72 63 4c 69 73 74 2a 2c 20 57 68 65 72 65 43  SrcList*, WhereC
2e79d 6c 61 75 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a 2f  lause*, int);../
2e79e 2a 0a 2a 2a 20 43 61 6c 6c 20 65 78 70 72 41 6e  *.** Call exprAn
2e79f 61 6c 79 7a 65 20 6f 6e 20 61 6c 6c 20 74 65 72  alyze on all ter
2e7a0 6d 73 20 69 6e 20 61 20 57 48 45 52 45 20 63 6c  ms in a WHERE cl
2e7a1 61 75 73 65 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2f  ause.  .**.**.*/
2e7a2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70  .static void exp
2e7a3 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 0a 20 20 53  rAnalyzeAll(.  S
2e7a4 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
2e7a5 2c 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46  ,       /* the F
2e7a6 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
2e7a7 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
2e7a8 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
2e7a9 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20  WHERE clause to 
2e7aa 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29  be analyzed */.)
2e7ab 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72  {.  int i;.  for
2e7ac 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d 2d 31 3b  (i=pWC->nTerm-1;
2e7ad 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20   i>=0; i--){.   
2e7ae 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 54 61   exprAnalyze(pTa
2e7af 62 4c 69 73 74 2c 20 70 57 43 2c 20 69 29 3b 0a  bList, pWC, i);.
2e7b0 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53    }.}..#ifndef S
2e7b1 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f  QLITE_OMIT_LIKE_
2e7b2 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 2f 2a 0a  OPTIMIZATION./*.
2e7b3 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  ** Check to see 
2e7b4 69 66 20 74 68 65 20 67 69 76 65 6e 20 65 78 70  if the given exp
2e7b5 72 65 73 73 69 6f 6e 20 69 73 20 61 20 4c 49 4b  ression is a LIK
2e7b6 45 20 6f 72 20 47 4c 4f 42 20 6f 70 65 72 61 74  E or GLOB operat
2e7b7 6f 72 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62  or that.** can b
2e7b8 65 20 6f 70 74 69 6d 69 7a 65 64 20 75 73 69 6e  e optimized usin
2e7b9 67 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  g inequality con
2e7ba 73 74 72 61 69 6e 74 73 2e 20 20 52 65 74 75 72  straints.  Retur
2e7bb 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73 0a  n TRUE if it is.
2e7bc 2a 2a 20 73 6f 20 61 6e 64 20 66 61 6c 73 65 20  ** so and false 
2e7bd 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e  if not..**.** In
2e7be 20 6f 72 64 65 72 20 66 6f 72 20 74 68 65 20 6f   order for the o
2e7bf 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 6f 70  perator to be op
2e7c0 74 69 6d 69 7a 69 62 6c 65 2c 20 74 68 65 20 52  timizible, the R
2e7c1 48 53 20 6d 75 73 74 20 62 65 20 61 20 73 74 72  HS must be a str
2e7c2 69 6e 67 0a 2a 2a 20 6c 69 74 65 72 61 6c 20 74  ing.** literal t
2e7c3 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 62 65 67  hat does not beg
2e7c4 69 6e 20 77 69 74 68 20 61 20 77 69 6c 64 63 61  in with a wildca
2e7c5 72 64 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20  rd.  .*/.static 
2e7c6 69 6e 74 20 69 73 4c 69 6b 65 4f 72 47 6c 6f 62  int isLikeOrGlob
2e7c7 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2e7c8 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  e,    /* Parsing
2e7c9 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61   and code genera
2e7ca 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  ting context */.
2e7cb 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20    Expr *pExpr,  
2e7cc 20 20 20 20 2f 2a 20 54 65 73 74 20 74 68 69 73      /* Test this
2e7cd 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
2e7ce 20 45 78 70 72 20 2a 2a 70 70 50 72 65 66 69 78   Expr **ppPrefix
2e7cf 2c 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f  ,  /* Pointer to
2e7d0 20 54 4b 5f 53 54 52 49 4e 47 20 65 78 70 72 65   TK_STRING expre
2e7d1 73 73 69 6f 6e 20 77 69 74 68 20 70 61 74 74 65  ssion with patte
2e7d2 72 6e 20 70 72 65 66 69 78 20 2a 2f 0a 20 20 69  rn prefix */.  i
2e7d3 6e 74 20 2a 70 69 73 43 6f 6d 70 6c 65 74 65 2c  nt *pisComplete,
2e7d4 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
2e7d5 6f 6e 6c 79 20 77 69 6c 64 63 61 72 64 20 69 73  only wildcard is
2e7d6 20 25 20 69 6e 20 74 68 65 20 6c 61 73 74 20 63   % in the last c
2e7d7 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 69 6e  haracter */.  in
2e7d8 74 20 2a 70 6e 6f 43 61 73 65 20 20 20 20 20 20  t *pnoCase      
2e7d9 2f 2a 20 54 72 75 65 20 69 66 20 75 70 70 65 72  /* True if upper
2e7da 63 61 73 65 20 69 73 20 65 71 75 69 76 61 6c 65  case is equivale
2e7db 6e 74 20 74 6f 20 6c 6f 77 65 72 63 61 73 65 20  nt to lowercase 
2e7dc 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  */.){.  const ch
2e7dd 61 72 20 2a 7a 20 3d 20 30 3b 20 20 20 20 20 20  ar *z = 0;      
2e7de 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 6e 20     /* String on 
2e7df 52 48 53 20 6f 66 20 4c 49 4b 45 20 6f 70 65 72  RHS of LIKE oper
2e7e0 61 74 6f 72 20 2a 2f 0a 20 20 45 78 70 72 20 2a  ator */.  Expr *
2e7e1 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 74 3b 20  pRight, *pLeft; 
2e7e2 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 61 6e       /* Right an
2e7e3 64 20 6c 65 66 74 20 73 69 7a 65 20 6f 66 20 4c  d left size of L
2e7e4 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a  IKE operator */.
2e7e5 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2e7e6 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
2e7e7 4c 69 73 74 20 6f 66 20 6f 70 65 72 61 6e 64 73  List of operands
2e7e8 20 74 6f 20 74 68 65 20 4c 49 4b 45 20 6f 70 65   to the LIKE ope
2e7e9 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 63  rator */.  int c
2e7ea 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2e7eb 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 68 61        /* One cha
2e7ec 72 61 63 74 65 72 20 69 6e 20 7a 5b 5d 20 2a 2f  racter in z[] */
2e7ed 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20  .  int cnt;     
2e7ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e7ef 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 77   Number of non-w
2e7f0 69 6c 64 63 61 72 64 20 70 72 65 66 69 78 20 63  ildcard prefix c
2e7f1 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 63  haracters */.  c
2e7f2 68 61 72 20 77 63 5b 33 5d 3b 20 20 20 20 20 20  har wc[3];      
2e7f3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 6c            /* Wil
2e7f4 64 63 61 72 64 20 63 68 61 72 61 63 74 65 72 73  dcard characters
2e7f5 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70   */.  CollSeq *p
2e7f6 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Coll;           
2e7f7 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65   /* Collating se
2e7f8 71 75 65 6e 63 65 20 66 6f 72 20 4c 48 53 20 2a  quence for LHS *
2e7f9 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2e7fa 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f  = pParse->db;  /
2e7fb 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
2e7fc 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  ction */.  sqlit
2e7fd 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d  e3_value *pVal =
2e7fe 20 30 3b 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20   0;.  int op;   
2e7ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e800 20 2f 2a 20 4f 70 63 6f 64 65 20 6f 66 20 70 52   /* Opcode of pR
2e801 69 67 68 74 20 2a 2f 0a 0a 20 20 69 66 28 20 21  ight */..  if( !
2e802 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e  sqlite3IsLikeFun
2e803 63 74 69 6f 6e 28 64 62 2c 20 70 45 78 70 72 2c  ction(db, pExpr,
2e804 20 70 6e 6f 43 61 73 65 2c 20 77 63 29 20 29 7b   pnoCase, wc) ){
2e805 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2e806 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   }.#ifdef SQLITE
2e807 5f 45 42 43 44 49 43 0a 20 20 69 66 28 20 2a 70  _EBCDIC.  if( *p
2e808 6e 6f 43 61 73 65 20 29 20 72 65 74 75 72 6e 20  noCase ) return 
2e809 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70 4c 69 73  0;.#endif.  pLis
2e80a 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2e80b 73 74 3b 0a 20 20 70 4c 65 66 74 20 3d 20 70 4c  st;.  pLeft = pL
2e80c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b  ist->a[1].pExpr;
2e80d 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70  .  if( pLeft->op
2e80e 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 73  !=TK_COLUMN || s
2e80f 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
2e810 74 79 28 70 4c 65 66 74 29 21 3d 53 51 4c 49 54  ty(pLeft)!=SQLIT
2e811 45 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a 20 20  E_AFF_TEXT ){.  
2e812 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 30 32 30 36    /* IMP: R-0206
2e813 35 2d 34 39 34 36 35 20 54 68 65 20 6c 65 66 74  5-49465 The left
2e814 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
2e815 65 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 6f  e LIKE or GLOB o
2e816 70 65 72 61 74 6f 72 20 6d 75 73 74 0a 20 20 20  perator must.   
2e817 20 2a 2a 20 62 65 20 74 68 65 20 6e 61 6d 65 20   ** be the name 
2e818 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20 63 6f  of an indexed co
2e819 6c 75 6d 6e 20 77 69 74 68 20 54 45 58 54 20 61  lumn with TEXT a
2e81a 66 66 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 20  ffinity. */.    
2e81b 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2e81c 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 69  assert( pLeft->i
2e81d 43 6f 6c 75 6d 6e 21 3d 28 2d 31 29 20 29 3b 20  Column!=(-1) ); 
2e81e 2f 2a 20 42 65 63 61 75 73 65 20 49 50 4b 20 6e  /* Because IPK n
2e81f 65 76 65 72 20 68 61 73 20 41 46 46 5f 54 45 58  ever has AFF_TEX
2e820 54 20 2a 2f 0a 20 20 70 43 6f 6c 6c 20 3d 20 73  T */.  pColl = s
2e821 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
2e822 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 29  q(pParse, pLeft)
2e823 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c  ;.  assert( pCol
2e824 6c 21 3d 30 20 29 3b 20 20 2f 2a 20 45 76 65 72  l!=0 );  /* Ever
2e825 79 20 6e 6f 6e 2d 49 50 4b 20 63 6f 6c 75 6d 6e  y non-IPK column
2e826 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67   has a collating
2e827 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 69   sequence */.  i
2e828 66 28 20 28 70 43 6f 6c 6c 2d 3e 74 79 70 65 21  f( (pColl->type!
2e829 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e  =SQLITE_COLL_BIN
2e82a 41 52 59 20 7c 7c 20 2a 70 6e 6f 43 61 73 65 29  ARY || *pnoCase)
2e82b 20 26 26 0a 20 20 20 20 20 20 28 70 43 6f 6c 6c   &&.      (pColl
2e82c 2d 3e 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 43  ->type!=SQLITE_C
2e82d 4f 4c 4c 5f 4e 4f 43 41 53 45 20 7c 7c 20 21 2a  OLL_NOCASE || !*
2e82e 70 6e 6f 43 61 73 65 29 20 29 7b 0a 20 20 20 20  pnoCase) ){.    
2e82f 2f 2a 20 49 4d 50 3a 20 52 2d 30 39 30 30 33 2d  /* IMP: R-09003-
2e830 33 32 30 34 36 20 46 6f 72 20 74 68 65 20 47 4c  32046 For the GL
2e831 4f 42 20 6f 70 65 72 61 74 6f 72 2c 20 74 68 65  OB operator, the
2e832 20 63 6f 6c 75 6d 6e 20 6d 75 73 74 20 75 73 65   column must use
2e833 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 65 66 61   the.    ** defa
2e834 75 6c 74 20 42 49 4e 41 52 59 20 63 6f 6c 6c 61  ult BINARY colla
2e835 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a 20  ting sequence.. 
2e836 20 20 20 2a 2a 20 49 4d 50 3a 20 52 2d 34 31 34     ** IMP: R-414
2e837 30 38 2d 32 38 33 30 36 20 46 6f 72 20 74 68 65  08-28306 For the
2e838 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2c 20   LIKE operator, 
2e839 69 66 20 63 61 73 65 5f 73 65 6e 73 69 74 69 76  if case_sensitiv
2e83a 65 5f 6c 69 6b 65 20 6d 6f 64 65 0a 20 20 20 20  e_like mode.    
2e83b 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64 20 74 68  ** is enabled th
2e83c 65 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6d 75  en the column mu
2e83d 73 74 20 75 73 65 20 74 68 65 20 64 65 66 61 75  st use the defau
2e83e 6c 74 20 42 49 4e 41 52 59 20 63 6f 6c 6c 61 74  lt BINARY collat
2e83f 69 6e 67 0a 20 20 20 20 2a 2a 20 73 65 71 75 65  ing.    ** seque
2e840 6e 63 65 2c 20 6f 72 20 69 66 20 63 61 73 65 5f  nce, or if case_
2e841 73 65 6e 73 69 74 69 76 65 5f 6c 69 6b 65 20 6d  sensitive_like m
2e842 6f 64 65 20 69 73 20 64 69 73 61 62 6c 65 64 20  ode is disabled 
2e843 74 68 65 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 0a  then the column.
2e844 20 20 20 20 2a 2a 20 6d 75 73 74 20 75 73 65 20      ** must use 
2e845 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 4e 4f 43  the built-in NOC
2e846 41 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ASE collating se
2e847 71 75 65 6e 63 65 2e 0a 20 20 20 20 2a 2f 0a 20  quence..    */. 
2e848 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2e849 0a 0a 20 20 70 52 69 67 68 74 20 3d 20 70 4c 69  ..  pRight = pLi
2e84a 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
2e84b 20 20 6f 70 20 3d 20 70 52 69 67 68 74 2d 3e 6f    op = pRight->o
2e84c 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  p;.  if( op==TK_
2e84d 52 45 47 49 53 54 45 52 20 29 7b 0a 20 20 20 20  REGISTER ){.    
2e84e 6f 70 20 3d 20 70 52 69 67 68 74 2d 3e 6f 70 32  op = pRight->op2
2e84f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f 70 3d 3d  ;.  }.  if( op==
2e850 54 4b 5f 56 41 52 49 41 42 4c 45 20 29 7b 0a 20  TK_VARIABLE ){. 
2e851 20 20 20 56 64 62 65 20 2a 70 52 65 70 72 65 70     Vdbe *pReprep
2e852 61 72 65 20 3d 20 70 50 61 72 73 65 2d 3e 70 52  are = pParse->pR
2e853 65 70 72 65 70 61 72 65 3b 0a 20 20 20 20 70 56  eprepare;.    pV
2e854 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  al = sqlite3Vdbe
2e855 47 65 74 56 61 6c 75 65 28 70 52 65 70 72 65 70  GetValue(pReprep
2e856 61 72 65 2c 20 70 52 69 67 68 74 2d 3e 69 43 6f  are, pRight->iCo
2e857 6c 75 6d 6e 2c 20 53 51 4c 49 54 45 5f 41 46 46  lumn, SQLITE_AFF
2e858 5f 4e 4f 4e 45 29 3b 0a 20 20 20 20 69 66 28 20  _NONE);.    if( 
2e859 70 56 61 6c 20 26 26 20 73 71 6c 69 74 65 33 5f  pVal && sqlite3_
2e85a 76 61 6c 75 65 5f 74 79 70 65 28 70 56 61 6c 29  value_type(pVal)
2e85b 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 29 7b  ==SQLITE_TEXT ){
2e85c 0a 20 20 20 20 20 20 7a 20 3d 20 28 63 68 61 72  .      z = (char
2e85d 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
2e85e 5f 74 65 78 74 28 70 56 61 6c 29 3b 0a 20 20 20  _text(pVal);.   
2e85f 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
2e860 62 65 53 65 74 56 61 72 6d 61 73 6b 28 70 50 61  beSetVarmask(pPa
2e861 72 73 65 2d 3e 70 56 64 62 65 2c 20 70 52 69 67  rse->pVdbe, pRig
2e862 68 74 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ht->iColumn);.  
2e863 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74    assert( pRight
2e864 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 52 49 41 42 4c  ->op==TK_VARIABL
2e865 45 20 7c 7c 20 70 52 69 67 68 74 2d 3e 6f 70 3d  E || pRight->op=
2e866 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a  =TK_REGISTER );.
2e867 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d    }else if( op==
2e868 54 4b 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 20  TK_STRING ){.   
2e869 20 7a 20 3d 20 70 52 69 67 68 74 2d 3e 75 2e 7a   z = pRight->u.z
2e86a 54 6f 6b 65 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  Token;.  }.  if(
2e86b 20 7a 20 29 7b 0a 20 20 20 20 63 6e 74 20 3d 20   z ){.    cnt = 
2e86c 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 63  0;.    while( (c
2e86d 3d 7a 5b 63 6e 74 5d 29 21 3d 30 20 26 26 20 63  =z[cnt])!=0 && c
2e86e 21 3d 77 63 5b 30 5d 20 26 26 20 63 21 3d 77 63  !=wc[0] && c!=wc
2e86f 5b 31 5d 20 26 26 20 63 21 3d 77 63 5b 32 5d 20  [1] && c!=wc[2] 
2e870 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a  ){.      cnt++;.
2e871 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6e      }.    if( cn
2e872 74 21 3d 30 20 26 26 20 63 21 3d 30 20 26 26 20  t!=0 && c!=0 && 
2e873 32 35 35 21 3d 28 75 38 29 7a 5b 63 6e 74 2d 31  255!=(u8)z[cnt-1
2e874 5d 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  ] ){.      Expr 
2e875 2a 70 50 72 65 66 69 78 3b 0a 20 20 20 20 20 20  *pPrefix;.      
2e876 2a 70 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 7a  *pisComplete = z
2e877 5b 63 6e 74 5d 3d 3d 77 63 5b 30 5d 20 26 26 20  [cnt]==wc[0] && 
2e878 7a 5b 63 6e 74 2b 31 5d 3d 3d 30 3b 0a 20 20 20  z[cnt+1]==0;.   
2e879 20 20 20 70 50 72 65 66 69 78 20 3d 20 73 71 6c     pPrefix = sql
2e87a 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
2e87b 53 54 52 49 4e 47 2c 20 7a 29 3b 0a 20 20 20 20  STRING, z);.    
2e87c 20 20 69 66 28 20 70 50 72 65 66 69 78 20 29 20    if( pPrefix ) 
2e87d 70 50 72 65 66 69 78 2d 3e 75 2e 7a 54 6f 6b 65  pPrefix->u.zToke
2e87e 6e 5b 63 6e 74 5d 20 3d 20 30 3b 0a 20 20 20 20  n[cnt] = 0;.    
2e87f 20 20 2a 70 70 50 72 65 66 69 78 20 3d 20 70 50    *ppPrefix = pP
2e880 72 65 66 69 78 3b 0a 20 20 20 20 20 20 69 66 28  refix;.      if(
2e881 20 6f 70 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45   op==TK_VARIABLE
2e882 20 29 7b 0a 20 20 20 20 20 20 20 20 56 64 62 65   ){.        Vdbe
2e883 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2e884 64 62 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  dbe;.        sql
2e885 69 74 65 33 56 64 62 65 53 65 74 56 61 72 6d 61  ite3VdbeSetVarma
2e886 73 6b 28 76 2c 20 70 52 69 67 68 74 2d 3e 69 43  sk(v, pRight->iC
2e887 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20  olumn);.        
2e888 69 66 28 20 2a 70 69 73 43 6f 6d 70 6c 65 74 65  if( *pisComplete
2e889 20 26 26 20 70 52 69 67 68 74 2d 3e 75 2e 7a 54   && pRight->u.zT
2e88a 6f 6b 65 6e 5b 31 5d 20 29 7b 0a 20 20 20 20 20  oken[1] ){.     
2e88b 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72       /* If the r
2e88c 68 73 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 65  hs of the LIKE e
2e88d 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 76  xpression is a v
2e88e 61 72 69 61 62 6c 65 2c 20 61 6e 64 20 74 68 65  ariable, and the
2e88f 20 63 75 72 72 65 6e 74 0a 20 20 20 20 20 20 20   current.       
2e890 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74     ** value of t
2e891 68 65 20 76 61 72 69 61 62 6c 65 20 6d 65 61 6e  he variable mean
2e892 73 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  s there is no ne
2e893 65 64 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65  ed to invoke the
2e894 20 4c 49 4b 45 0a 20 20 20 20 20 20 20 20 20 20   LIKE.          
2e895 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65  ** function, the
2e896 6e 20 6e 6f 20 4f 50 5f 56 61 72 69 61 62 6c 65  n no OP_Variable
2e897 20 77 69 6c 6c 20 62 65 20 61 64 64 65 64 20 74   will be added t
2e898 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 20  o the program.. 
2e899 20 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73           ** This
2e89a 20 63 61 75 73 65 73 20 70 72 6f 62 6c 65 6d 73   causes problems
2e89b 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33   for the sqlite3
2e89c 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
2e89d 6e 61 6d 65 28 29 0a 20 20 20 20 20 20 20 20 20  name().         
2e89e 20 2a 2a 20 41 50 49 2e 20 54 6f 20 77 6f 72 6b   ** API. To work
2e89f 61 72 6f 75 6e 64 20 74 68 65 6d 2c 20 61 64 64  around them, add
2e8a0 20 61 20 64 75 6d 6d 79 20 4f 50 5f 56 61 72 69   a dummy OP_Vari
2e8a1 61 62 6c 65 20 68 65 72 65 2e 0a 20 20 20 20 20  able here..     
2e8a2 20 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 20 20       */ .       
2e8a3 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69     int r1 = sqli
2e8a4 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2e8a5 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 20  arse);.         
2e8a6 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2e8a7 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
2e8a8 52 69 67 68 74 2c 20 72 31 29 3b 0a 20 20 20 20  Right, r1);.    
2e8a9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2e8aa 65 43 68 61 6e 67 65 50 33 28 76 2c 20 73 71 6c  eChangeP3(v, sql
2e8ab 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
2e8ac 64 64 72 28 76 29 2d 31 2c 20 30 29 3b 0a 20 20  ddr(v)-1, 0);.  
2e8ad 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
2e8ae 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2e8af 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
2e8b0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2e8b1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20   }else{.      z 
2e8b2 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  = 0;.    }.  }..
2e8b3 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
2e8b4 65 65 28 70 56 61 6c 29 3b 0a 20 20 72 65 74 75  ee(pVal);.  retu
2e8b5 72 6e 20 28 7a 21 3d 30 29 3b 0a 7d 0a 23 65 6e  rn (z!=0);.}.#en
2e8b6 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2e8b7 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41  IT_LIKE_OPTIMIZA
2e8b8 54 49 4f 4e 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65  TION */...#ifnde
2e8b9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2e8ba 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a  RTUALTABLE./*.**
2e8bb 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
2e8bc 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65   the given expre
2e8bd 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20  ssion is of the 
2e8be 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  form.**.**      
2e8bf 20 20 20 63 6f 6c 75 6d 6e 20 4d 41 54 43 48 20     column MATCH 
2e8c0 65 78 70 72 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74  expr.**.** If it
2e8c1 20 69 73 20 74 68 65 6e 20 72 65 74 75 72 6e 20   is then return 
2e8c2 54 52 55 45 2e 20 20 49 66 20 6e 6f 74 2c 20 72  TRUE.  If not, r
2e8c3 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2f 0a  eturn FALSE..*/.
2e8c4 73 74 61 74 69 63 20 69 6e 74 20 69 73 4d 61 74  static int isMat
2e8c5 63 68 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20 45 78  chOfColumn(.  Ex
2e8c6 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 2f  pr *pExpr      /
2e8c7 2a 20 54 65 73 74 20 74 68 69 73 20 65 78 70 72  * Test this expr
2e8c8 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 45  ession */.){.  E
2e8c9 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a  xprList *pList;.
2e8ca 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  .  if( pExpr->op
2e8cb 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 7b  !=TK_FUNCTION ){
2e8cc 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2e8cd 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
2e8ce 53 74 72 49 43 6d 70 28 70 45 78 70 72 2d 3e 75  StrICmp(pExpr->u
2e8cf 2e 7a 54 6f 6b 65 6e 2c 22 6d 61 74 63 68 22 29  .zToken,"match")
2e8d0 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=0 ){.    retur
2e8d1 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74  n 0;.  }.  pList
2e8d2 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   = pExpr->x.pLis
2e8d3 74 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e  t;.  if( pList->
2e8d4 6e 45 78 70 72 21 3d 32 20 29 7b 0a 20 20 20 20  nExpr!=2 ){.    
2e8d5 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2e8d6 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  if( pList->a[1].
2e8d7 70 45 78 70 72 2d 3e 6f 70 20 21 3d 20 54 4b 5f  pExpr->op != TK_
2e8d8 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 72 65  COLUMN ){.    re
2e8d9 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65  turn 0;.  }.  re
2e8da 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66  turn 1;.}.#endif
2e8db 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2e8dc 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
2e8dd 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 42  ./*.** If the pB
2e8de 61 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  ase expression o
2e8df 72 69 67 69 6e 61 74 65 64 20 69 6e 20 74 68 65  riginated in the
2e8e0 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
2e8e1 75 73 65 20 6f 66 0a 2a 2a 20 61 20 6a 6f 69 6e  use of.** a join
2e8e2 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20  , then transfer 
2e8e3 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
2e8e4 6d 61 72 6b 69 6e 67 73 20 6f 76 65 72 20 74 6f  markings over to
2e8e5 20 64 65 72 69 76 65 64 2e 0a 2a 2f 0a 73 74 61   derived..*/.sta
2e8e6 74 69 63 20 76 6f 69 64 20 74 72 61 6e 73 66 65  tic void transfe
2e8e7 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73 28 45 78  rJoinMarkings(Ex
2e8e8 70 72 20 2a 70 44 65 72 69 76 65 64 2c 20 45 78  pr *pDerived, Ex
2e8e9 70 72 20 2a 70 42 61 73 65 29 7b 0a 20 20 70 44  pr *pBase){.  pD
2e8ea 65 72 69 76 65 64 2d 3e 66 6c 61 67 73 20 7c 3d  erived->flags |=
2e8eb 20 70 42 61 73 65 2d 3e 66 6c 61 67 73 20 26 20   pBase->flags & 
2e8ec 45 50 5f 46 72 6f 6d 4a 6f 69 6e 3b 0a 20 20 70  EP_FromJoin;.  p
2e8ed 44 65 72 69 76 65 64 2d 3e 69 52 69 67 68 74 4a  Derived->iRightJ
2e8ee 6f 69 6e 54 61 62 6c 65 20 3d 20 70 42 61 73 65  oinTable = pBase
2e8ef 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c  ->iRightJoinTabl
2e8f0 65 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e  e;.}..#if !defin
2e8f1 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f  ed(SQLITE_OMIT_O
2e8f2 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20  R_OPTIMIZATION) 
2e8f3 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
2e8f4 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
2e8f5 29 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20  )./*.** Analyze 
2e8f6 61 20 74 65 72 6d 20 74 68 61 74 20 63 6f 6e 73  a term that cons
2e8f7 69 73 74 73 20 6f 66 20 74 77 6f 20 6f 72 20 6d  ists of two or m
2e8f8 6f 72 65 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64  ore OR-connected
2e8f9 0a 2a 2a 20 73 75 62 74 65 72 6d 73 2e 20 20 53  .** subterms.  S
2e8fa 6f 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  o in:.**.**     
2e8fb 2e 2e 2e 20 57 48 45 52 45 20 20 28 61 3d 35 29  ... WHERE  (a=5)
2e8fc 20 41 4e 44 20 28 62 3d 37 20 4f 52 20 63 3d 39   AND (b=7 OR c=9
2e8fd 20 4f 52 20 64 3d 31 33 29 20 41 4e 44 20 28 64   OR d=13) AND (d
2e8fe 3d 31 33 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  =13).**         
2e8ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e900 20 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e   ^^^^^^^^^^^^^^^
2e901 5e 5e 5e 5e 5e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ^^^^^.**.** This
2e902 20 72 6f 75 74 69 6e 65 20 61 6e 61 6c 79 7a 65   routine analyze
2e903 73 20 74 65 72 6d 73 20 73 75 63 68 20 61 73 20  s terms such as 
2e904 74 68 65 20 6d 69 64 64 6c 65 20 74 65 72 6d 20  the middle term 
2e905 69 6e 20 74 68 65 20 61 62 6f 76 65 20 65 78 61  in the above exa
2e906 6d 70 6c 65 2e 0a 2a 2a 20 41 20 57 68 65 72 65  mple..** A Where
2e907 4f 72 54 65 72 6d 20 6f 62 6a 65 63 74 20 69 73  OrTerm object is
2e908 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 61 74   computed and at
2e909 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 74 65  tached to the te
2e90a 72 6d 20 75 6e 64 65 72 0a 2a 2a 20 61 6e 61 6c  rm under.** anal
2e90b 79 73 69 73 2c 20 72 65 67 61 72 64 6c 65 73 73  ysis, regardless
2e90c 20 6f 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 20   of the outcome 
2e90d 6f 66 20 74 68 65 20 61 6e 61 6c 79 73 69 73 2e  of the analysis.
2e90e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20    Hence:.**.**  
2e90f 20 20 20 57 68 65 72 65 54 65 72 6d 2e 77 74 46     WhereTerm.wtF
2e910 6c 61 67 73 20 20 20 7c 3d 20 20 54 45 52 4d 5f  lags   |=  TERM_
2e911 4f 52 49 4e 46 4f 0a 2a 2a 20 20 20 20 20 57 68  ORINFO.**     Wh
2e912 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66  ereTerm.u.pOrInf
2e913 6f 20 20 3d 20 20 61 20 64 79 6e 61 6d 69 63 61  o  =  a dynamica
2e914 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 57 68  lly allocated Wh
2e915 65 72 65 4f 72 54 65 72 6d 20 6f 62 6a 65 63 74  ereOrTerm object
2e916 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 20  .**.** The term 
2e917 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 6d  being analyzed m
2e918 75 73 74 20 68 61 76 65 20 74 77 6f 20 6f 72 20  ust have two or 
2e919 6d 6f 72 65 20 6f 66 20 4f 52 2d 63 6f 6e 6e 65  more of OR-conne
2e91a 63 74 65 64 20 73 75 62 74 65 72 6d 73 2e 0a 2a  cted subterms..*
2e91b 2a 20 41 20 73 69 6e 67 6c 65 20 73 75 62 74 65  * A single subte
2e91c 72 6d 20 6d 69 67 68 74 20 62 65 20 61 20 73 65  rm might be a se
2e91d 74 20 6f 66 20 41 4e 44 2d 63 6f 6e 6e 65 63 74  t of AND-connect
2e91e 65 64 20 73 75 62 2d 73 75 62 74 65 72 6d 73 2e  ed sub-subterms.
2e91f 0a 2a 2a 20 45 78 61 6d 70 6c 65 73 20 6f 66 20  .** Examples of 
2e920 74 65 72 6d 73 20 75 6e 64 65 72 20 61 6e 61 6c  terms under anal
2e921 79 73 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ysis:.**.**     
2e922 28 41 29 20 20 20 20 20 74 31 2e 78 3d 74 32 2e  (A)     t1.x=t2.
2e923 79 20 4f 52 20 74 31 2e 78 3d 74 32 2e 7a 20 4f  y OR t1.x=t2.z O
2e924 52 20 74 31 2e 79 3d 31 35 20 4f 52 20 74 31 2e  R t1.y=15 OR t1.
2e925 7a 3d 74 33 2e 61 2b 35 0a 2a 2a 20 20 20 20 20  z=t3.a+5.**     
2e926 28 42 29 20 20 20 20 20 78 3d 65 78 70 72 31 20  (B)     x=expr1 
2e927 4f 52 20 65 78 70 72 32 3d 78 20 4f 52 20 78 3d  OR expr2=x OR x=
2e928 65 78 70 72 33 0a 2a 2a 20 20 20 20 20 28 43 29  expr3.**     (C)
2e929 20 20 20 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f       t1.x=t2.y O
2e92a 52 20 28 74 31 2e 78 3d 74 32 2e 7a 20 41 4e 44  R (t1.x=t2.z AND
2e92b 20 74 31 2e 79 3d 31 35 29 0a 2a 2a 20 20 20 20   t1.y=15).**    
2e92c 20 28 44 29 20 20 20 20 20 78 3d 65 78 70 72 31   (D)     x=expr1
2e92d 20 4f 52 20 28 79 3e 31 31 20 41 4e 44 20 79 3c   OR (y>11 AND y<
2e92e 32 32 20 41 4e 44 20 7a 20 4c 49 4b 45 20 27 2a  22 AND z LIKE '*
2e92f 68 65 6c 6c 6f 2a 27 29 0a 2a 2a 20 20 20 20 20  hello*').**     
2e930 28 45 29 20 20 20 20 20 28 70 2e 61 3d 31 20 41  (E)     (p.a=1 A
2e931 4e 44 20 71 2e 62 3d 32 20 41 4e 44 20 72 2e 63  ND q.b=2 AND r.c
2e932 3d 33 29 20 4f 52 20 28 70 2e 78 3d 34 20 41 4e  =3) OR (p.x=4 AN
2e933 44 20 71 2e 79 3d 35 20 41 4e 44 20 72 2e 7a 3d  D q.y=5 AND r.z=
2e934 36 29 0a 2a 2a 0a 2a 2a 20 43 41 53 45 20 31 3a  6).**.** CASE 1:
2e935 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75  .**.** If all su
2e936 62 74 65 72 6d 73 20 61 72 65 20 6f 66 20 74 68  bterms are of th
2e937 65 20 66 6f 72 6d 20 54 2e 43 3d 65 78 70 72 20  e form T.C=expr 
2e938 66 6f 72 20 73 6f 6d 65 20 73 69 6e 67 6c 65 20  for some single 
2e939 63 6f 6c 75 6d 6e 20 6f 66 20 43 0a 2a 2a 20 61  column of C.** a
2e93a 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 54 20   single table T 
2e93b 28 61 73 20 73 68 6f 77 6e 20 69 6e 20 65 78 61  (as shown in exa
2e93c 6d 70 6c 65 20 42 20 61 62 6f 76 65 29 20 74 68  mple B above) th
2e93d 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  en create a new 
2e93e 76 69 72 74 75 61 6c 0a 2a 2a 20 74 65 72 6d 20  virtual.** term 
2e93f 74 68 61 74 20 69 73 20 61 6e 20 65 71 75 69 76  that is an equiv
2e940 61 6c 65 6e 74 20 49 4e 20 65 78 70 72 65 73 73  alent IN express
2e941 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ion.  In other w
2e942 6f 72 64 73 2c 20 69 66 20 74 68 65 20 74 65 72  ords, if the ter
2e943 6d 0a 2a 2a 20 62 65 69 6e 67 20 61 6e 61 6c 79  m.** being analy
2e944 7a 65 64 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  zed is:.**.**   
2e945 20 20 20 78 20 3d 20 65 78 70 72 31 20 20 4f 52     x = expr1  OR
2e946 20 20 65 78 70 72 32 20 3d 20 78 20 20 4f 52 20    expr2 = x  OR 
2e947 20 78 20 3d 20 65 78 70 72 33 0a 2a 2a 0a 2a 2a   x = expr3.**.**
2e948 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 6e   then create a n
2e949 65 77 20 76 69 72 74 75 61 6c 20 74 65 72 6d 20  ew virtual term 
2e94a 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
2e94b 20 20 20 20 20 20 78 20 49 4e 20 28 65 78 70 72        x IN (expr
2e94c 31 2c 65 78 70 72 32 2c 65 78 70 72 33 29 0a 2a  1,expr2,expr3).*
2e94d 2a 0a 2a 2a 20 43 41 53 45 20 32 3a 0a 2a 2a 0a  *.** CASE 2:.**.
2e94e 2a 2a 20 49 66 20 61 6c 6c 20 73 75 62 74 65 72  ** If all subter
2e94f 6d 73 20 61 72 65 20 69 6e 64 65 78 61 62 6c 65  ms are indexable
2e950 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 61 62   by a single tab
2e951 6c 65 20 54 2c 20 74 68 65 6e 20 73 65 74 0a 2a  le T, then set.*
2e952 2a 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65  *.**     WhereTe
2e953 72 6d 2e 65 4f 70 65 72 61 74 6f 72 20 20 20 20  rm.eOperator    
2e954 20 20 20 20 20 20 20 20 20 20 3d 20 20 57 4f 5f            =  WO_
2e955 4f 52 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54  OR.**     WhereT
2e956 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 69  erm.u.pOrInfo->i
2e957 6e 64 65 78 61 62 6c 65 20 20 7c 3d 20 20 74 68  ndexable  |=  th
2e958 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  e cursor number 
2e959 66 6f 72 20 74 61 62 6c 65 20 54 0a 2a 2a 0a 2a  for table T.**.*
2e95a 2a 20 41 20 73 75 62 74 65 72 6d 20 69 73 20 22  * A subterm is "
2e95b 69 6e 64 65 78 61 62 6c 65 22 20 69 66 20 69 74  indexable" if it
2e95c 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a   is of the form.
2e95d 2a 2a 20 22 54 2e 43 20 3c 6f 70 3e 20 3c 65 78  ** "T.C <op> <ex
2e95e 70 72 3e 22 20 77 68 65 72 65 20 43 20 69 73 20  pr>" where C is 
2e95f 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61  any column of ta
2e960 62 6c 65 20 54 20 61 6e 64 20 0a 2a 2a 20 3c 6f  ble T and .** <o
2e961 70 3e 20 69 73 20 6f 6e 65 20 6f 66 20 22 3d 22  p> is one of "="
2e962 2c 20 22 3c 22 2c 20 22 3c 3d 22 2c 20 22 3e 22  , "<", "<=", ">"
2e963 2c 20 22 3e 3d 22 2c 20 22 49 53 20 4e 55 4c 4c  , ">=", "IS NULL
2e964 22 2c 20 6f 72 20 22 49 4e 22 2e 0a 2a 2a 20 41  ", or "IN"..** A
2e965 20 73 75 62 74 65 72 6d 20 69 73 20 61 6c 73 6f   subterm is also
2e966 20 69 6e 64 65 78 61 62 6c 65 20 69 66 20 69 74   indexable if it
2e967 20 69 73 20 61 6e 20 41 4e 44 20 6f 66 20 74 77   is an AND of tw
2e968 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73 75 62  o or more.** sub
2e969 73 75 62 74 65 72 6d 73 20 61 74 20 6c 65 61 73  subterms at leas
2e96a 74 20 6f 6e 65 20 6f 66 20 77 68 69 63 68 20 69  t one of which i
2e96b 73 20 69 6e 64 65 78 61 62 6c 65 2e 20 20 49 6e  s indexable.  In
2e96c 64 65 78 61 62 6c 65 20 41 4e 44 20 0a 2a 2a 20  dexable AND .** 
2e96d 73 75 62 74 65 72 6d 73 20 68 61 76 65 20 74 68  subterms have th
2e96e 65 69 72 20 65 4f 70 65 72 61 74 6f 72 20 73 65  eir eOperator se
2e96f 74 20 74 6f 20 57 4f 5f 41 4e 44 20 61 6e 64 20  t to WO_AND and 
2e970 74 68 65 79 20 68 61 76 65 0a 2a 2a 20 75 2e 70  they have.** u.p
2e971 41 6e 64 49 6e 66 6f 20 73 65 74 20 74 6f 20 61  AndInfo set to a
2e972 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   dynamically all
2e973 6f 63 61 74 65 64 20 57 68 65 72 65 41 6e 64 54  ocated WhereAndT
2e974 65 72 6d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  erm object..**.*
2e975 2a 20 46 72 6f 6d 20 61 6e 6f 74 68 65 72 20 70  * From another p
2e976 6f 69 6e 74 20 6f 66 20 76 69 65 77 2c 20 22 69  oint of view, "i
2e977 6e 64 65 78 61 62 6c 65 22 20 6d 65 61 6e 73 20  ndexable" means 
2e978 74 68 61 74 20 74 68 65 20 73 75 62 74 65 72 6d  that the subterm
2e979 20 63 6f 75 6c 64 0a 2a 2a 20 70 6f 74 65 6e 74   could.** potent
2e97a 69 61 6c 6c 79 20 62 65 20 75 73 65 64 20 77 69  ially be used wi
2e97b 74 68 20 61 6e 20 69 6e 64 65 78 20 69 66 20 61  th an index if a
2e97c 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e  n appropriate in
2e97d 64 65 78 20 65 78 69 73 74 73 2e 0a 2a 2a 20 54  dex exists..** T
2e97e 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64 6f 65  his analysis doe
2e97f 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 20 77  s not consider w
2e980 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
2e981 65 20 69 6e 64 65 78 20 65 78 69 73 74 73 3b 20  e index exists; 
2e982 74 68 61 74 0a 2a 2a 20 69 73 20 73 6f 6d 65 74  that.** is somet
2e983 68 69 6e 67 20 74 68 65 20 62 65 73 74 49 6e 64  hing the bestInd
2e984 65 78 28 29 20 72 6f 75 74 69 6e 65 20 77 69 6c  ex() routine wil
2e985 6c 20 64 65 74 65 72 6d 69 6e 65 2e 20 20 54 68  l determine.  Th
2e986 69 73 20 61 6e 61 6c 79 73 69 73 0a 2a 2a 20 6f  is analysis.** o
2e987 6e 6c 79 20 6c 6f 6f 6b 73 20 61 74 20 77 68 65  nly looks at whe
2e988 74 68 65 72 20 73 75 62 74 65 72 6d 73 20 61 70  ther subterms ap
2e989 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 69 6e  propriate for in
2e98a 64 65 78 69 6e 67 20 65 78 69 73 74 2e 0a 2a 2a  dexing exist..**
2e98b 0a 2a 2a 20 41 6c 6c 20 65 78 61 6d 70 6c 65 73  .** All examples
2e98c 20 41 20 74 68 72 6f 75 67 68 20 45 20 61 62 6f   A through E abo
2e98d 76 65 20 61 6c 6c 20 73 61 74 69 73 66 79 20 63  ve all satisfy c
2e98e 61 73 65 20 32 2e 20 20 42 75 74 20 69 66 20 61  ase 2.  But if a
2e98f 20 74 65 72 6d 0a 2a 2a 20 61 6c 73 6f 20 73 74   term.** also st
2e990 61 74 69 73 66 69 65 73 20 63 61 73 65 20 31 20  atisfies case 1 
2e991 28 73 75 63 68 20 61 73 20 42 29 20 77 65 20 6b  (such as B) we k
2e992 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6f 70 74  now that the opt
2e993 69 6d 69 7a 65 72 20 77 69 6c 6c 0a 2a 2a 20 61  imizer will.** a
2e994 6c 77 61 79 73 20 70 72 65 66 65 72 20 63 61 73  lways prefer cas
2e995 65 20 31 2c 20 73 6f 20 69 6e 20 74 68 61 74 20  e 1, so in that 
2e996 63 61 73 65 20 77 65 20 70 72 65 74 65 6e 64 20  case we pretend 
2e997 74 68 61 74 20 63 61 73 65 20 32 20 69 73 20 6e  that case 2 is n
2e998 6f 74 0a 2a 2a 20 73 61 74 69 73 66 69 65 64 2e  ot.** satisfied.
2e999 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20  .**.** It might 
2e99a 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
2e99b 20 6d 75 6c 74 69 70 6c 65 20 74 61 62 6c 65 73   multiple tables
2e99c 20 61 72 65 20 69 6e 64 65 78 61 62 6c 65 2e 20   are indexable. 
2e99d 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a   For example,.**
2e99e 20 28 45 29 20 61 62 6f 76 65 20 69 73 20 69 6e   (E) above is in
2e99f 64 65 78 61 62 6c 65 20 6f 6e 20 74 61 62 6c 65  dexable on table
2e9a0 73 20 50 2c 20 51 2c 20 61 6e 64 20 52 2e 0a 2a  s P, Q, and R..*
2e9a1 2a 0a 2a 2a 20 54 65 72 6d 73 20 74 68 61 74 20  *.** Terms that 
2e9a2 73 61 74 69 73 66 79 20 63 61 73 65 20 32 20 61  satisfy case 2 a
2e9a3 72 65 20 63 61 6e 64 69 64 61 74 65 73 20 66 6f  re candidates fo
2e9a4 72 20 6c 6f 6f 6b 75 70 20 62 79 20 75 73 69 6e  r lookup by usin
2e9a5 67 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 69 6e  g.** separate in
2e9a6 64 69 63 65 73 20 74 6f 20 66 69 6e 64 20 72 6f  dices to find ro
2e9a7 77 69 64 73 20 66 6f 72 20 65 61 63 68 20 73 75  wids for each su
2e9a8 62 74 65 72 6d 20 61 6e 64 20 63 6f 6d 70 6f 73  bterm and compos
2e9a9 69 6e 67 0a 2a 2a 20 74 68 65 20 75 6e 69 6f 6e  ing.** the union
2e9aa 20 6f 66 20 61 6c 6c 20 72 6f 77 69 64 73 20 75   of all rowids u
2e9ab 73 69 6e 67 20 61 20 52 6f 77 53 65 74 20 6f 62  sing a RowSet ob
2e9ac 6a 65 63 74 2e 20 20 54 68 69 73 20 69 73 20 73  ject.  This is s
2e9ad 69 6d 69 6c 61 72 0a 2a 2a 20 74 6f 20 22 62 69  imilar.** to "bi
2e9ae 74 6d 61 70 20 69 6e 64 69 63 65 73 22 20 69 6e  tmap indices" in
2e9af 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20   other database 
2e9b0 65 6e 67 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f  engines..**.** O
2e9b1 54 48 45 52 57 49 53 45 3a 0a 2a 2a 0a 2a 2a 20  THERWISE:.**.** 
2e9b2 49 66 20 6e 65 69 74 68 65 72 20 63 61 73 65 20  If neither case 
2e9b3 31 20 6e 6f 72 20 63 61 73 65 20 32 20 61 70 70  1 nor case 2 app
2e9b4 6c 79 2c 20 74 68 65 6e 20 6c 65 61 76 65 20 74  ly, then leave t
2e9b5 68 65 20 65 4f 70 65 72 61 74 6f 72 20 73 65 74  he eOperator set
2e9b6 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 20 20 54 68   to.** zero.  Th
2e9b7 69 73 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 75  is term is not u
2e9b8 73 65 66 75 6c 20 66 6f 72 20 73 65 61 72 63 68  seful for search
2e9b9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2e9ba 20 65 78 70 72 41 6e 61 6c 79 7a 65 4f 72 54 65   exprAnalyzeOrTe
2e9bb 72 6d 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  rm(.  SrcList *p
2e9bc 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
2e9bd 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* the FROM clau
2e9be 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  se */.  WhereCla
2e9bf 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20  use *pWC,       
2e9c0 20 20 2f 2a 20 74 68 65 20 63 6f 6d 70 6c 65 74    /* the complet
2e9c1 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2e9c2 2f 0a 20 20 69 6e 74 20 69 64 78 54 65 72 6d 20  /.  int idxTerm 
2e9c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e9c4 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 4f 52   Index of the OR
2e9c5 2d 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c  -term to be anal
2e9c6 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 50 61 72  yzed */.){.  Par
2e9c7 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 43  se *pParse = pWC
2e9c8 2d 3e 70 50 61 72 73 65 3b 20 20 20 20 20 20 20  ->pParse;       
2e9c9 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63       /* Parser c
2e9ca 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 73 71 6c 69  ontext */.  sqli
2e9cb 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2e9cc 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 20 20  ->db;           
2e9cd 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2e9ce 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
2e9cf 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
2e9d0 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
2e9d1 72 6d 5d 3b 20 20 20 20 2f 2a 20 54 68 65 20 74  rm];    /* The t
2e9d2 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  erm to be analyz
2e9d3 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  ed */.  Expr *pE
2e9d4 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  xpr = pTerm->pEx
2e9d5 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pr;             
2e9d6 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
2e9d7 6e 20 6f 66 20 74 68 65 20 74 65 72 6d 20 2a 2f  n of the term */
2e9d8 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20  .  WhereMaskSet 
2e9d9 2a 70 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d  *pMaskSet = pWC-
2e9da 3e 70 4d 61 73 6b 53 65 74 3b 20 2f 2a 20 54 61  >pMaskSet; /* Ta
2e9db 62 6c 65 20 75 73 65 20 6d 61 73 6b 73 20 2a 2f  ble use masks */
2e9dc 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
2e9dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e9de 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2e9df 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
2e9e0 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 4f   WhereClause *pO
2e9e1 72 57 63 3b 20 20 20 20 20 20 20 2f 2a 20 42 72  rWc;       /* Br
2e9e2 65 61 6b 75 70 20 6f 66 20 70 54 65 72 6d 20 69  eakup of pTerm i
2e9e3 6e 74 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a  nto subterms */.
2e9e4 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 72    WhereTerm *pOr
2e9e5 54 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a 20 41  Term;       /* A
2e9e6 20 53 75 62 2d 74 65 72 6d 20 77 69 74 68 69 6e   Sub-term within
2e9e7 20 74 68 65 20 70 4f 72 57 63 20 2a 2f 0a 20 20   the pOrWc */.  
2e9e8 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a 70 4f 72  WhereOrInfo *pOr
2e9e9 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 41 64 64  Info;     /* Add
2e9ea 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2e9eb 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77  ion associated w
2e9ec 69 74 68 20 70 54 65 72 6d 20 2a 2f 0a 20 20 42  ith pTerm */.  B
2e9ed 69 74 6d 61 73 6b 20 63 68 6e 67 54 6f 49 4e 3b  itmask chngToIN;
2e9ee 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2e9ef 65 73 20 74 68 61 74 20 6d 69 67 68 74 20 73 61  es that might sa
2e9f0 74 69 73 66 79 20 63 61 73 65 20 31 20 2a 2f 0a  tisfy case 1 */.
2e9f1 20 20 42 69 74 6d 61 73 6b 20 69 6e 64 65 78 61    Bitmask indexa
2e9f2 62 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ble;        /* T
2e9f3 61 62 6c 65 73 20 74 68 61 74 20 61 72 65 20 69  ables that are i
2e9f4 6e 64 65 78 61 62 6c 65 2c 20 73 61 74 69 73 66  ndexable, satisf
2e9f5 79 69 6e 67 20 63 61 73 65 20 32 20 2a 2f 0a 0a  ying case 2 */..
2e9f6 20 20 2f 2a 0a 20 20 2a 2a 20 42 72 65 61 6b 20    /*.  ** Break 
2e9f7 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20 69 6e  the OR clause in
2e9f8 74 6f 20 69 74 73 20 73 65 70 61 72 61 74 65 20  to its separate 
2e9f9 73 75 62 74 65 72 6d 73 2e 20 20 54 68 65 20 73  subterms.  The s
2e9fa 75 62 74 65 72 6d 73 20 61 72 65 0a 20 20 2a 2a  ubterms are.  **
2e9fb 20 73 74 6f 72 65 64 20 69 6e 20 61 20 57 68 65   stored in a Whe
2e9fc 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75  reClause structu
2e9fd 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 77 69  re containing wi
2e9fe 74 68 69 6e 20 74 68 65 20 57 68 65 72 65 4f 72  thin the WhereOr
2e9ff 49 6e 66 6f 0a 20 20 2a 2a 20 6f 62 6a 65 63 74  Info.  ** object
2ea00 20 74 68 61 74 20 69 73 20 61 74 74 61 63 68 65   that is attache
2ea01 64 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61  d to the origina
2ea02 6c 20 4f 52 20 63 6c 61 75 73 65 20 74 65 72 6d  l OR clause term
2ea03 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
2ea04 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73   (pTerm->wtFlags
2ea05 20 26 20 28 54 45 52 4d 5f 44 59 4e 41 4d 49 43   & (TERM_DYNAMIC
2ea06 7c 54 45 52 4d 5f 4f 52 49 4e 46 4f 7c 54 45 52  |TERM_ORINFO|TER
2ea07 4d 5f 41 4e 44 49 4e 46 4f 29 29 3d 3d 30 20 29  M_ANDINFO))==0 )
2ea08 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70  ;.  assert( pExp
2ea09 72 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a  r->op==TK_OR );.
2ea0a 20 20 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e    pTerm->u.pOrIn
2ea0b 66 6f 20 3d 20 70 4f 72 49 6e 66 6f 20 3d 20 73  fo = pOrInfo = s
2ea0c 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
2ea0d 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  ro(db, sizeof(*p
2ea0e 4f 72 49 6e 66 6f 29 29 3b 0a 20 20 69 66 28 20  OrInfo));.  if( 
2ea0f 70 4f 72 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74  pOrInfo==0 ) ret
2ea10 75 72 6e 3b 0a 20 20 70 54 65 72 6d 2d 3e 77 74  urn;.  pTerm->wt
2ea11 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 4f 52  Flags |= TERM_OR
2ea12 49 4e 46 4f 3b 0a 20 20 70 4f 72 57 63 20 3d 20  INFO;.  pOrWc = 
2ea13 26 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20  &pOrInfo->wc;.  
2ea14 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28  whereClauseInit(
2ea15 70 4f 72 57 63 2c 20 70 57 43 2d 3e 70 50 61 72  pOrWc, pWC->pPar
2ea16 73 65 2c 20 70 4d 61 73 6b 53 65 74 29 3b 0a 20  se, pMaskSet);. 
2ea17 20 77 68 65 72 65 53 70 6c 69 74 28 70 4f 72 57   whereSplit(pOrW
2ea18 63 2c 20 70 45 78 70 72 2c 20 54 4b 5f 4f 52 29  c, pExpr, TK_OR)
2ea19 3b 0a 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41  ;.  exprAnalyzeA
2ea1a 6c 6c 28 70 53 72 63 2c 20 70 4f 72 57 63 29 3b  ll(pSrc, pOrWc);
2ea1b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2ea1c 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
2ea1d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 57  ;.  assert( pOrW
2ea1e 63 2d 3e 6e 54 65 72 6d 3e 3d 32 20 29 3b 0a 0a  c->nTerm>=2 );..
2ea1f 20 20 2f 2a 0a 20 20 2a 2a 20 43 6f 6d 70 75 74    /*.  ** Comput
2ea20 65 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62  e the set of tab
2ea21 6c 65 73 20 74 68 61 74 20 6d 69 67 68 74 20 73  les that might s
2ea22 61 74 69 73 66 79 20 63 61 73 65 73 20 31 20 6f  atisfy cases 1 o
2ea23 72 20 32 2e 0a 20 20 2a 2f 0a 20 20 69 6e 64 65  r 2..  */.  inde
2ea24 78 61 62 6c 65 20 3d 20 7e 28 42 69 74 6d 61 73  xable = ~(Bitmas
2ea25 6b 29 30 3b 0a 20 20 63 68 6e 67 54 6f 49 4e 20  k)0;.  chngToIN 
2ea26 3d 20 7e 28 70 57 43 2d 3e 76 6d 61 73 6b 29 3b  = ~(pWC->vmask);
2ea27 0a 20 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e  .  for(i=pOrWc->
2ea28 6e 54 65 72 6d 2d 31 2c 20 70 4f 72 54 65 72 6d  nTerm-1, pOrTerm
2ea29 3d 70 4f 72 57 63 2d 3e 61 3b 20 69 3e 3d 30 20  =pOrWc->a; i>=0 
2ea2a 26 26 20 69 6e 64 65 78 61 62 6c 65 3b 20 69 2d  && indexable; i-
2ea2b 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20  -, pOrTerm++){. 
2ea2c 20 20 20 69 66 28 20 28 70 4f 72 54 65 72 6d 2d     if( (pOrTerm-
2ea2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f  >eOperator & WO_
2ea2e 53 49 4e 47 4c 45 29 3d 3d 30 20 29 7b 0a 20 20  SINGLE)==0 ){.  
2ea2f 20 20 20 20 57 68 65 72 65 41 6e 64 49 6e 66 6f      WhereAndInfo
2ea30 20 2a 70 41 6e 64 49 6e 66 6f 3b 0a 20 20 20 20   *pAndInfo;.    
2ea31 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72    assert( pOrTer
2ea32 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 30 20  m->eOperator==0 
2ea33 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2ea34 20 28 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61   (pOrTerm->wtFla
2ea35 67 73 20 26 20 28 54 45 52 4d 5f 41 4e 44 49 4e  gs & (TERM_ANDIN
2ea36 46 4f 7c 54 45 52 4d 5f 4f 52 49 4e 46 4f 29 29  FO|TERM_ORINFO))
2ea37 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 68 6e  ==0 );.      chn
2ea38 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20  gToIN = 0;.     
2ea39 20 70 41 6e 64 49 6e 66 6f 20 3d 20 73 71 6c 69   pAndInfo = sqli
2ea3a 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
2ea3b 62 2c 20 73 69 7a 65 6f 66 28 2a 70 41 6e 64 49  b, sizeof(*pAndI
2ea3c 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 69 66 28  nfo));.      if(
2ea3d 20 70 41 6e 64 49 6e 66 6f 20 29 7b 0a 20 20 20   pAndInfo ){.   
2ea3e 20 20 20 20 20 57 68 65 72 65 43 6c 61 75 73 65       WhereClause
2ea3f 20 2a 70 41 6e 64 57 43 3b 0a 20 20 20 20 20 20   *pAndWC;.      
2ea40 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 41 6e    WhereTerm *pAn
2ea41 64 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 69  dTerm;.        i
2ea42 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 42 69  nt j;.        Bi
2ea43 74 6d 61 73 6b 20 62 20 3d 20 30 3b 0a 20 20 20  tmask b = 0;.   
2ea44 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 75 2e       pOrTerm->u.
2ea45 70 41 6e 64 49 6e 66 6f 20 3d 20 70 41 6e 64 49  pAndInfo = pAndI
2ea46 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 70 4f 72  nfo;.        pOr
2ea47 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d  Term->wtFlags |=
2ea48 20 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 3b 0a 20   TERM_ANDINFO;. 
2ea49 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e         pOrTerm->
2ea4a 65 4f 70 65 72 61 74 6f 72 20 3d 20 57 4f 5f 41  eOperator = WO_A
2ea4b 4e 44 3b 0a 20 20 20 20 20 20 20 20 70 41 6e 64  ND;.        pAnd
2ea4c 57 43 20 3d 20 26 70 41 6e 64 49 6e 66 6f 2d 3e  WC = &pAndInfo->
2ea4d 77 63 3b 0a 20 20 20 20 20 20 20 20 77 68 65 72  wc;.        wher
2ea4e 65 43 6c 61 75 73 65 49 6e 69 74 28 70 41 6e 64  eClauseInit(pAnd
2ea4f 57 43 2c 20 70 57 43 2d 3e 70 50 61 72 73 65 2c  WC, pWC->pParse,
2ea50 20 70 4d 61 73 6b 53 65 74 29 3b 0a 20 20 20 20   pMaskSet);.    
2ea51 20 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 70      whereSplit(p
2ea52 41 6e 64 57 43 2c 20 70 4f 72 54 65 72 6d 2d 3e  AndWC, pOrTerm->
2ea53 70 45 78 70 72 2c 20 54 4b 5f 41 4e 44 29 3b 0a  pExpr, TK_AND);.
2ea54 20 20 20 20 20 20 20 20 65 78 70 72 41 6e 61 6c          exprAnal
2ea55 79 7a 65 41 6c 6c 28 70 53 72 63 2c 20 70 41 6e  yzeAll(pSrc, pAn
2ea56 64 57 43 29 3b 0a 20 20 20 20 20 20 20 20 74 65  dWC);.        te
2ea57 73 74 63 61 73 65 28 20 64 62 2d 3e 6d 61 6c 6c  stcase( db->mall
2ea58 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
2ea59 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c      if( !db->mal
2ea5a 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2ea5b 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20         for(j=0, 
2ea5c 70 41 6e 64 54 65 72 6d 3d 70 41 6e 64 57 43 2d  pAndTerm=pAndWC-
2ea5d 3e 61 3b 20 6a 3c 70 41 6e 64 57 43 2d 3e 6e 54  >a; j<pAndWC->nT
2ea5e 65 72 6d 3b 20 6a 2b 2b 2c 20 70 41 6e 64 54 65  erm; j++, pAndTe
2ea5f 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  rm++){.         
2ea60 20 20 20 61 73 73 65 72 74 28 20 70 41 6e 64 54     assert( pAndT
2ea61 65 72 6d 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20  erm->pExpr );.  
2ea62 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 6c            if( al
2ea63 6c 6f 77 65 64 4f 70 28 70 41 6e 64 54 65 72 6d  lowedOp(pAndTerm
2ea64 2d 3e 70 45 78 70 72 2d 3e 6f 70 29 20 29 7b 0a  ->pExpr->op) ){.
2ea65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 20                b 
2ea66 7c 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b  |= getMask(pMask
2ea67 53 65 74 2c 20 70 41 6e 64 54 65 72 6d 2d 3e 6c  Set, pAndTerm->l
2ea68 65 66 74 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  eftCursor);.    
2ea69 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2ea6a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
2ea6b 20 20 20 20 20 20 20 20 69 6e 64 65 78 61 62 6c          indexabl
2ea6c 65 20 26 3d 20 62 3b 0a 20 20 20 20 20 20 7d 0a  e &= b;.      }.
2ea6d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f      }else if( pO
2ea6e 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2ea6f 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 29 7b 0a   TERM_COPIED ){.
2ea70 20 20 20 20 20 20 2f 2a 20 53 6b 69 70 20 74 68        /* Skip th
2ea71 69 73 20 74 65 72 6d 20 66 6f 72 20 6e 6f 77 2e  is term for now.
2ea72 20 20 57 65 20 72 65 76 69 73 69 74 20 69 74 20    We revisit it 
2ea73 77 68 65 6e 20 77 65 20 70 72 6f 63 65 73 73 20  when we process 
2ea74 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72  the.      ** cor
2ea75 72 65 73 70 6f 6e 64 69 6e 67 20 54 45 52 4d 5f  responding TERM_
2ea76 56 49 52 54 55 41 4c 20 74 65 72 6d 20 2a 2f 0a  VIRTUAL term */.
2ea77 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2ea78 20 42 69 74 6d 61 73 6b 20 62 3b 0a 20 20 20 20   Bitmask b;.    
2ea79 20 20 62 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d    b = getMask(pM
2ea7a 61 73 6b 53 65 74 2c 20 70 4f 72 54 65 72 6d 2d  askSet, pOrTerm-
2ea7b 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b 0a 20 20  >leftCursor);.  
2ea7c 20 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d      if( pOrTerm-
2ea7d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2ea7e 56 49 52 54 55 41 4c 20 29 7b 0a 20 20 20 20 20  VIRTUAL ){.     
2ea7f 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f     WhereTerm *pO
2ea80 74 68 65 72 20 3d 20 26 70 4f 72 57 63 2d 3e 61  ther = &pOrWc->a
2ea81 5b 70 4f 72 54 65 72 6d 2d 3e 69 50 61 72 65 6e  [pOrTerm->iParen
2ea82 74 5d 3b 0a 20 20 20 20 20 20 20 20 62 20 7c 3d  t];.        b |=
2ea83 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65   getMask(pMaskSe
2ea84 74 2c 20 70 4f 74 68 65 72 2d 3e 6c 65 66 74 43  t, pOther->leftC
2ea85 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a  ursor);.      }.
2ea86 20 20 20 20 20 20 69 6e 64 65 78 61 62 6c 65 20        indexable 
2ea87 26 3d 20 62 3b 0a 20 20 20 20 20 20 69 66 28 20  &= b;.      if( 
2ea88 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
2ea89 6f 72 21 3d 57 4f 5f 45 51 20 29 7b 0a 20 20 20  or!=WO_EQ ){.   
2ea8a 20 20 20 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20       chngToIN = 
2ea8b 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  0;.      }else{.
2ea8c 20 20 20 20 20 20 20 20 63 68 6e 67 54 6f 49 4e          chngToIN
2ea8d 20 26 3d 20 62 3b 0a 20 20 20 20 20 20 7d 0a 20   &= b;.      }. 
2ea8e 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20     }.  }..  /*. 
2ea8f 20 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 73   ** Record the s
2ea90 65 74 20 6f 66 20 74 61 62 6c 65 73 20 74 68 61  et of tables tha
2ea91 74 20 73 61 74 69 73 66 79 20 63 61 73 65 20 32  t satisfy case 2
2ea92 2e 20 20 54 68 65 20 73 65 74 20 6d 69 67 68 74  .  The set might
2ea93 20 62 65 0a 20 20 2a 2a 20 65 6d 70 74 79 2e 0a   be.  ** empty..
2ea94 20 20 2a 2f 0a 20 20 70 4f 72 49 6e 66 6f 2d 3e    */.  pOrInfo->
2ea95 69 6e 64 65 78 61 62 6c 65 20 3d 20 69 6e 64 65  indexable = inde
2ea96 78 61 62 6c 65 3b 0a 20 20 70 54 65 72 6d 2d 3e  xable;.  pTerm->
2ea97 65 4f 70 65 72 61 74 6f 72 20 3d 20 69 6e 64 65  eOperator = inde
2ea98 78 61 62 6c 65 3d 3d 30 20 3f 20 30 20 3a 20 57  xable==0 ? 0 : W
2ea99 4f 5f 4f 52 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  O_OR;..  /*.  **
2ea9a 20 63 68 6e 67 54 6f 49 4e 20 68 6f 6c 64 73 20   chngToIN holds 
2ea9b 61 20 73 65 74 20 6f 66 20 74 61 62 6c 65 73 20  a set of tables 
2ea9c 74 68 61 74 20 2a 6d 69 67 68 74 2a 20 73 61 74  that *might* sat
2ea9d 69 73 66 79 20 63 61 73 65 20 31 2e 20 20 42 75  isfy case 1.  Bu
2ea9e 74 0a 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74  t.  ** we have t
2ea9f 6f 20 64 6f 20 73 6f 6d 65 20 61 64 64 69 74 69  o do some additi
2eaa0 6f 6e 61 6c 20 63 68 65 63 6b 69 6e 67 20 74 6f  onal checking to
2eaa1 20 73 65 65 20 69 66 20 63 61 73 65 20 31 20 72   see if case 1 r
2eaa2 65 61 6c 6c 79 0a 20 20 2a 2a 20 69 73 20 73 61  eally.  ** is sa
2eaa3 74 69 73 66 69 65 64 2e 0a 20 20 2a 2a 0a 20 20  tisfied..  **.  
2eaa4 2a 2a 20 63 68 6e 67 54 6f 49 4e 20 77 69 6c 6c  ** chngToIN will
2eaa5 20 68 6f 6c 64 20 65 69 74 68 65 72 20 30 2c 20   hold either 0, 
2eaa6 31 2c 20 6f 72 20 32 20 62 69 74 73 2e 20 20 54  1, or 2 bits.  T
2eaa7 68 65 20 30 2d 62 69 74 20 63 61 73 65 20 6d 65  he 0-bit case me
2eaa8 61 6e 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68  ans.  ** that th
2eaa9 65 72 65 20 69 73 20 6e 6f 20 70 6f 73 73 69 62  ere is no possib
2eaaa 69 6c 69 74 79 20 6f 66 20 74 72 61 6e 73 66 6f  ility of transfo
2eaab 72 6d 69 6e 67 20 74 68 65 20 4f 52 20 63 6c 61  rming the OR cla
2eaac 75 73 65 20 69 6e 74 6f 20 61 6e 0a 20 20 2a 2a  use into an.  **
2eaad 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 62 65 63   IN operator bec
2eaae 61 75 73 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ause one or more
2eaaf 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52   terms in the OR
2eab0 20 63 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 0a   clause contain.
2eab1 20 20 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f    ** something o
2eab2 74 68 65 72 20 74 68 61 6e 20 3d 3d 20 6f 6e 20  ther than == on 
2eab3 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  a column in the 
2eab4 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e 20 20 54  single table.  T
2eab5 68 65 20 31 2d 62 69 74 0a 20 20 2a 2a 20 63 61  he 1-bit.  ** ca
2eab6 73 65 20 6d 65 61 6e 73 20 74 68 61 74 20 65 76  se means that ev
2eab7 65 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ery term of the 
2eab8 4f 52 20 63 6c 61 75 73 65 20 69 73 20 6f 66 20  OR clause is of 
2eab9 74 68 65 20 66 6f 72 6d 0a 20 20 2a 2a 20 22 74  the form.  ** "t
2eaba 61 62 6c 65 2e 63 6f 6c 75 6d 6e 3d 65 78 70 72  able.column=expr
2eabb 22 20 66 6f 72 20 73 6f 6d 65 20 73 69 6e 67 6c  " for some singl
2eabc 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6f 6e  e table.  The on
2eabd 65 20 62 69 74 20 74 68 61 74 20 69 73 20 73 65  e bit that is se
2eabe 74 0a 20 20 2a 2a 20 77 69 6c 6c 20 63 6f 72 72  t.  ** will corr
2eabf 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 63 6f  espond to the co
2eac0 6d 6d 6f 6e 20 74 61 62 6c 65 2e 20 20 57 65 20  mmon table.  We 
2eac1 73 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 68  still need to ch
2eac2 65 63 6b 20 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a  eck to make.  **
2eac3 20 73 75 72 65 20 74 68 65 20 73 61 6d 65 20 63   sure the same c
2eac4 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20 6f 6e  olumn is used on
2eac5 20 61 6c 6c 20 74 65 72 6d 73 2e 20 20 54 68 65   all terms.  The
2eac6 20 32 2d 62 69 74 20 63 61 73 65 20 69 73 20 77   2-bit case is w
2eac7 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 61 6c 6c  hen.  ** the all
2eac8 20 74 65 72 6d 73 20 61 72 65 20 6f 66 20 74 68   terms are of th
2eac9 65 20 66 6f 72 6d 20 22 74 61 62 6c 65 31 2e 63  e form "table1.c
2eaca 6f 6c 75 6d 6e 3d 74 61 62 6c 65 32 2e 63 6f 6c  olumn=table2.col
2eacb 75 6d 6e 22 2e 20 20 49 74 0a 20 20 2a 2a 20 6d  umn".  It.  ** m
2eacc 69 67 68 74 20 62 65 20 70 6f 73 73 69 62 6c 65  ight be possible
2eacd 20 74 6f 20 66 6f 72 6d 20 61 6e 20 49 4e 20 6f   to form an IN o
2eace 70 65 72 61 74 6f 72 20 77 69 74 68 20 65 69 74  perator with eit
2eacf 68 65 72 20 74 61 62 6c 65 31 2e 63 6f 6c 75 6d  her table1.colum
2ead0 6e 0a 20 20 2a 2a 20 6f 72 20 74 61 62 6c 65 32  n.  ** or table2
2ead1 2e 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 4c  .column as the L
2ead2 48 53 20 69 66 20 65 69 74 68 65 72 20 69 73 20  HS if either is 
2ead3 63 6f 6d 6d 6f 6e 20 74 6f 20 65 76 65 72 79 20  common to every 
2ead4 74 65 72 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 65  term of.  ** the
2ead5 20 4f 52 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a   OR clause..  **
2ead6 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  .  ** Note that 
2ead7 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 72  terms of the for
2ead8 6d 20 22 74 61 62 6c 65 2e 63 6f 6c 75 6d 6e 31  m "table.column1
2ead9 3d 74 61 62 6c 65 2e 63 6f 6c 75 6d 6e 32 22 20  =table.column2" 
2eada 28 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 74  (the.  ** same t
2eadb 61 62 6c 65 20 6f 6e 20 62 6f 74 68 20 73 69 7a  able on both siz
2eadc 65 73 20 6f 66 20 74 68 65 20 3d 3d 29 20 63 61  es of the ==) ca
2eadd 6e 6e 6f 74 20 62 65 20 6f 70 74 69 6d 69 7a 65  nnot be optimize
2eade 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 68  d..  */.  if( ch
2eadf 6e 67 54 6f 49 4e 20 29 7b 0a 20 20 20 20 69 6e  ngToIN ){.    in
2eae0 74 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d  t okToChngToIN =
2eae1 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20   0;     /* True 
2eae2 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  if the conversio
2eae3 6e 20 74 6f 20 49 4e 20 69 73 20 76 61 6c 69 64  n to IN is valid
2eae4 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c   */.    int iCol
2eae5 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 20 20 20 20  umn = -1;       
2eae6 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65    /* Column inde
2eae7 78 20 6f 6e 20 6c 68 73 20 6f 66 20 49 4e 20 6f  x on lhs of IN o
2eae8 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69  perator */.    i
2eae9 6e 74 20 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b  nt iCursor = -1;
2eaea 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2eaeb 65 20 63 75 72 73 6f 72 20 63 6f 6d 6d 6f 6e 20  e cursor common 
2eaec 74 6f 20 61 6c 6c 20 74 65 72 6d 73 20 2a 2f 0a  to all terms */.
2eaed 20 20 20 20 69 6e 74 20 6a 20 3d 20 30 3b 20 20      int j = 0;  
2eaee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2eaef 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2eaf0 0a 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20  ..    /* Search 
2eaf1 66 6f 72 20 61 20 74 61 62 6c 65 20 61 6e 64 20  for a table and 
2eaf2 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65  column that appe
2eaf3 61 72 73 20 6f 6e 20 6f 6e 65 20 73 69 64 65 20  ars on one side 
2eaf4 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74  or the.    ** ot
2eaf5 68 65 72 20 6f 66 20 74 68 65 20 3d 3d 20 6f 70  her of the == op
2eaf6 65 72 61 74 6f 72 20 69 6e 20 65 76 65 72 79 20  erator in every 
2eaf7 73 75 62 74 65 72 6d 2e 20 20 54 68 61 74 20 74  subterm.  That t
2eaf8 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a  able and column.
2eaf9 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72      ** will be r
2eafa 65 63 6f 72 64 65 64 20 69 6e 20 69 43 75 72 73  ecorded in iCurs
2eafb 6f 72 20 61 6e 64 20 69 43 6f 6c 75 6d 6e 2e 20  or and iColumn. 
2eafc 20 54 68 65 72 65 20 6d 69 67 68 74 20 6e 6f 74   There might not
2eafd 20 62 65 20 61 6e 79 0a 20 20 20 20 2a 2a 20 73   be any.    ** s
2eafe 75 63 68 20 74 61 62 6c 65 20 61 6e 64 20 63 6f  uch table and co
2eaff 6c 75 6d 6e 2e 20 20 53 65 74 20 6f 6b 54 6f 43  lumn.  Set okToC
2eb00 68 6e 67 54 6f 49 4e 20 69 66 20 61 6e 20 61 70  hngToIN if an ap
2eb01 70 72 6f 70 72 69 61 74 65 20 74 61 62 6c 65 0a  propriate table.
2eb02 20 20 20 20 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d      ** and colum
2eb03 6e 20 69 73 20 66 6f 75 6e 64 20 62 75 74 20 6c  n is found but l
2eb04 65 61 76 65 20 6f 6b 54 6f 43 68 6e 67 54 6f 49  eave okToChngToI
2eb05 4e 20 66 61 6c 73 65 20 69 66 20 6e 6f 74 20 66  N false if not f
2eb06 6f 75 6e 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ound..    */.   
2eb07 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 32 20 26 26   for(j=0; j<2 &&
2eb08 20 21 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 3b 20   !okToChngToIN; 
2eb09 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 70 4f 72 54  j++){.      pOrT
2eb0a 65 72 6d 20 3d 20 70 4f 72 57 63 2d 3e 61 3b 0a  erm = pOrWc->a;.
2eb0b 20 20 20 20 20 20 66 6f 72 28 69 3d 70 4f 72 57        for(i=pOrW
2eb0c 63 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d 30  c->nTerm-1; i>=0
2eb0d 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b  ; i--, pOrTerm++
2eb0e 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
2eb0f 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65  t( pOrTerm->eOpe
2eb10 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a  rator==WO_EQ );.
2eb11 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d          pOrTerm-
2eb12 3e 77 74 46 6c 61 67 73 20 26 3d 20 7e 54 45 52  >wtFlags &= ~TER
2eb13 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  M_OR_OK;.       
2eb14 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65   if( pOrTerm->le
2eb15 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 73 6f  ftCursor==iCurso
2eb16 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  r ){.          /
2eb17 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 32 2d  * This is the 2-
2eb18 62 69 74 20 63 61 73 65 20 61 6e 64 20 77 65 20  bit case and we 
2eb19 61 72 65 20 6f 6e 20 74 68 65 20 73 65 63 6f 6e  are on the secon
2eb1a 64 20 69 74 65 72 61 74 69 6f 6e 20 61 6e 64 0a  d iteration and.
2eb1b 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 75 72            ** cur
2eb1c 72 65 6e 74 20 74 65 72 6d 20 69 73 20 66 72 6f  rent term is fro
2eb1d 6d 20 74 68 65 20 66 69 72 73 74 20 69 74 65 72  m the first iter
2eb1e 61 74 69 6f 6e 2e 20 20 53 6f 20 73 6b 69 70 20  ation.  So skip 
2eb1f 74 68 69 73 20 74 65 72 6d 2e 20 2a 2f 0a 20 20  this term. */.  
2eb20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2eb21 6a 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20  j==1 );.        
2eb22 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
2eb23 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
2eb24 28 20 28 63 68 6e 67 54 6f 49 4e 20 26 20 67 65  ( (chngToIN & ge
2eb25 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
2eb26 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  pOrTerm->leftCur
2eb27 73 6f 72 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  sor))==0 ){.    
2eb28 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 74 65        /* This te
2eb29 72 6d 20 6d 75 73 74 20 62 65 20 6f 66 20 74 68  rm must be of th
2eb2a 65 20 66 6f 72 6d 20 74 31 2e 61 3d 3d 74 32 2e  e form t1.a==t2.
2eb2b 62 20 77 68 65 72 65 20 74 32 20 69 73 20 69 6e  b where t2 is in
2eb2c 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a   the.          *
2eb2d 2a 20 63 68 6e 67 54 6f 49 4e 20 73 65 74 20 62  * chngToIN set b
2eb2e 75 74 20 74 31 20 69 73 20 6e 6f 74 2e 20 20 54  ut t1 is not.  T
2eb2f 68 69 73 20 74 65 72 6d 20 77 69 6c 6c 20 62 65  his term will be
2eb30 20 65 69 74 68 65 72 20 70 72 65 63 65 65 64 65   either preceede
2eb31 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f  d.          ** o
2eb32 72 20 66 6f 6c 6c 77 65 64 20 62 79 20 61 6e 20  r follwed by an 
2eb33 69 6e 76 65 72 74 65 64 20 63 6f 70 79 20 28 74  inverted copy (t
2eb34 32 2e 62 3d 3d 74 31 2e 61 29 2e 20 20 53 6b 69  2.b==t1.a).  Ski
2eb35 70 20 74 68 69 73 20 74 65 72 6d 20 0a 20 20 20  p this term .   
2eb36 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 75 73         ** and us
2eb37 65 20 69 74 73 20 69 6e 76 65 72 73 69 6f 6e 2e  e its inversion.
2eb38 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 74 65   */.          te
2eb39 73 74 63 61 73 65 28 20 70 4f 72 54 65 72 6d 2d  stcase( pOrTerm-
2eb3a 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2eb3b 43 4f 50 49 45 44 20 29 3b 0a 20 20 20 20 20 20  COPIED );.      
2eb3c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4f      testcase( pO
2eb3d 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2eb3e 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b   TERM_VIRTUAL );
2eb3f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
2eb40 74 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c  t( pOrTerm->wtFl
2eb41 61 67 73 20 26 20 28 54 45 52 4d 5f 43 4f 50 49  ags & (TERM_COPI
2eb42 45 44 7c 54 45 52 4d 5f 56 49 52 54 55 41 4c 29  ED|TERM_VIRTUAL)
2eb43 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f   );.          co
2eb44 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
2eb45 7d 0a 20 20 20 20 20 20 20 20 69 43 6f 6c 75 6d  }.        iColum
2eb46 6e 20 3d 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c  n = pOrTerm->u.l
2eb47 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  eftColumn;.     
2eb48 20 20 20 69 43 75 72 73 6f 72 20 3d 20 70 4f 72     iCursor = pOr
2eb49 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
2eb4a 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
2eb4b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2eb4c 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20 20 20  f( i<0 ){.      
2eb4d 20 20 2f 2a 20 4e 6f 20 63 61 6e 64 69 64 61 74    /* No candidat
2eb4e 65 20 74 61 62 6c 65 2b 63 6f 6c 75 6d 6e 20 77  e table+column w
2eb4f 61 73 20 66 6f 75 6e 64 2e 20 20 54 68 69 73 20  as found.  This 
2eb50 63 61 6e 20 6f 6e 6c 79 20 6f 63 63 75 72 0a 20  can only occur. 
2eb51 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65         ** on the
2eb52 20 73 65 63 6f 6e 64 20 69 74 65 72 61 74 69 6f   second iteratio
2eb53 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73  n */.        ass
2eb54 65 72 74 28 20 6a 3d 3d 31 20 29 3b 0a 20 20 20  ert( j==1 );.   
2eb55 20 20 20 20 20 61 73 73 65 72 74 28 20 28 63 68       assert( (ch
2eb56 6e 67 54 6f 49 4e 26 28 63 68 6e 67 54 6f 49 4e  ngToIN&(chngToIN
2eb57 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20  -1))==0 );.     
2eb58 20 20 20 61 73 73 65 72 74 28 20 63 68 6e 67 54     assert( chngT
2eb59 6f 49 4e 3d 3d 67 65 74 4d 61 73 6b 28 70 4d 61  oIN==getMask(pMa
2eb5a 73 6b 53 65 74 2c 20 69 43 75 72 73 6f 72 29 20  skSet, iCursor) 
2eb5b 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
2eb5c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2eb5d 74 65 73 74 63 61 73 65 28 20 6a 3d 3d 31 20 29  testcase( j==1 )
2eb5e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 68  ;..      /* We h
2eb5f 61 76 65 20 66 6f 75 6e 64 20 61 20 63 61 6e 64  ave found a cand
2eb60 69 64 61 74 65 20 74 61 62 6c 65 20 61 6e 64 20  idate table and 
2eb61 63 6f 6c 75 6d 6e 2e 20 20 43 68 65 63 6b 20 74  column.  Check t
2eb62 6f 20 73 65 65 20 69 66 20 74 68 61 74 0a 20 20  o see if that.  
2eb63 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64      ** table and
2eb64 20 63 6f 6c 75 6d 6e 20 69 73 20 63 6f 6d 6d 6f   column is commo
2eb65 6e 20 74 6f 20 65 76 65 72 79 20 74 65 72 6d 20  n to every term 
2eb66 69 6e 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65  in the OR clause
2eb67 20 2a 2f 0a 20 20 20 20 20 20 6f 6b 54 6f 43 68   */.      okToCh
2eb68 6e 67 54 6f 49 4e 20 3d 20 31 3b 0a 20 20 20 20  ngToIN = 1;.    
2eb69 20 20 66 6f 72 28 3b 20 69 3e 3d 30 20 26 26 20    for(; i>=0 && 
2eb6a 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 3b 20 69 2d  okToChngToIN; i-
2eb6b 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20  -, pOrTerm++){. 
2eb6c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2eb6d 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  OrTerm->eOperato
2eb6e 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20  r==WO_EQ );.    
2eb6f 20 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d      if( pOrTerm-
2eb70 3e 6c 65 66 74 43 75 72 73 6f 72 21 3d 69 43 75  >leftCursor!=iCu
2eb71 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20  rsor ){.        
2eb72 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61    pOrTerm->wtFla
2eb73 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f 4f  gs &= ~TERM_OR_O
2eb74 4b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  K;.        }else
2eb75 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 75 2e   if( pOrTerm->u.
2eb76 6c 65 66 74 43 6f 6c 75 6d 6e 21 3d 69 43 6f 6c  leftColumn!=iCol
2eb77 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20  umn ){.         
2eb78 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20   okToChngToIN = 
2eb79 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  0;.        }else
2eb7a 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
2eb7b 61 66 66 4c 65 66 74 2c 20 61 66 66 52 69 67 68  affLeft, affRigh
2eb7c 74 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;.          /* 
2eb7d 49 66 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  If the right-han
2eb7e 64 20 73 69 64 65 20 69 73 20 61 6c 73 6f 20 61  d side is also a
2eb7f 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68   column, then th
2eb80 65 20 61 66 66 69 6e 69 74 69 65 73 0a 20 20 20  e affinities.   
2eb81 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 62 6f 74         ** of bot
2eb82 68 20 72 69 67 68 74 20 61 6e 64 20 6c 65 66 74  h right and left
2eb83 20 73 69 64 65 73 20 6d 75 73 74 20 62 65 20 73   sides must be s
2eb84 75 63 68 20 74 68 61 74 20 6e 6f 20 74 79 70 65  uch that no type
2eb85 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  .          ** co
2eb86 6e 76 65 72 73 69 6f 6e 73 20 61 72 65 20 72 65  nversions are re
2eb87 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20 72 69  quired on the ri
2eb88 67 68 74 2e 20 20 28 54 69 63 6b 65 74 20 23 32  ght.  (Ticket #2
2eb89 32 34 39 29 0a 20 20 20 20 20 20 20 20 20 20 2a  249).          *
2eb8a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 66 66 52  /.          affR
2eb8b 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78  ight = sqlite3Ex
2eb8c 70 72 41 66 66 69 6e 69 74 79 28 70 4f 72 54 65  prAffinity(pOrTe
2eb8d 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68  rm->pExpr->pRigh
2eb8e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 66  t);.          af
2eb8f 66 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45  fLeft = sqlite3E
2eb90 78 70 72 41 66 66 69 6e 69 74 79 28 70 4f 72 54  xprAffinity(pOrT
2eb91 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66  erm->pExpr->pLef
2eb92 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  t);.          if
2eb93 28 20 61 66 66 52 69 67 68 74 21 3d 30 20 26 26  ( affRight!=0 &&
2eb94 20 61 66 66 52 69 67 68 74 21 3d 61 66 66 4c 65   affRight!=affLe
2eb95 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
2eb96 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d    okToChngToIN =
2eb97 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65   0;.          }e
2eb98 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
2eb99 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67   pOrTerm->wtFlag
2eb9a 73 20 7c 3d 20 54 45 52 4d 5f 4f 52 5f 4f 4b 3b  s |= TERM_OR_OK;
2eb9b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
2eb9c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2eb9d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20     }..    /* At 
2eb9e 74 68 69 73 20 70 6f 69 6e 74 2c 20 6f 6b 54 6f  this point, okTo
2eb9f 43 68 6e 67 54 6f 49 4e 20 69 73 20 74 72 75 65  ChngToIN is true
2eba0 20 69 66 20 6f 72 69 67 69 6e 61 6c 20 70 54 65   if original pTe
2eba1 72 6d 20 73 61 74 69 73 66 69 65 73 0a 20 20 20  rm satisfies.   
2eba2 20 2a 2a 20 63 61 73 65 20 31 2e 20 20 49 6e 20   ** case 1.  In 
2eba3 74 68 61 74 20 63 61 73 65 2c 20 63 6f 6e 73 74  that case, const
2eba4 72 75 63 74 20 61 20 6e 65 77 20 76 69 72 74 75  ruct a new virtu
2eba5 61 6c 20 74 65 72 6d 20 74 68 61 74 20 69 73 20  al term that is 
2eba6 0a 20 20 20 20 2a 2a 20 70 54 65 72 6d 20 63 6f  .    ** pTerm co
2eba7 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20  nverted into an 
2eba8 49 4e 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20  IN operator..   
2eba9 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 6b 54 6f   */.    if( okTo
2ebaa 43 68 6e 67 54 6f 49 4e 20 29 7b 0a 20 20 20 20  ChngToIN ){.    
2ebab 20 20 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20    Expr *pDup;   
2ebac 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74 72           /* A tr
2ebad 61 6e 73 69 65 6e 74 20 64 75 70 6c 69 63 61 74  ansient duplicat
2ebae 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  e expression */.
2ebaf 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a        ExprList *
2ebb0 70 4c 69 73 74 20 3d 20 30 3b 20 20 20 2f 2a 20  pList = 0;   /* 
2ebb1 54 68 65 20 52 48 53 20 6f 66 20 74 68 65 20 49  The RHS of the I
2ebb2 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  N operator */.  
2ebb3 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20      Expr *pLeft 
2ebb4 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  = 0;       /* Th
2ebb5 65 20 4c 48 53 20 6f 66 20 74 68 65 20 49 4e 20  e LHS of the IN 
2ebb6 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20  operator */.    
2ebb7 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 20 20 20    Expr *pNew;   
2ebb8 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2ebb9 63 6f 6d 70 6c 65 74 65 20 49 4e 20 6f 70 65 72  complete IN oper
2ebba 61 74 6f 72 20 2a 2f 0a 0a 20 20 20 20 20 20 66  ator */..      f
2ebbb 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72  or(i=pOrWc->nTer
2ebbc 6d 2d 31 2c 20 70 4f 72 54 65 72 6d 3d 70 4f 72  m-1, pOrTerm=pOr
2ebbd 57 63 2d 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d  Wc->a; i>=0; i--
2ebbe 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20  , pOrTerm++){.  
2ebbf 20 20 20 20 20 20 69 66 28 20 28 70 4f 72 54 65        if( (pOrTe
2ebc0 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
2ebc1 52 4d 5f 4f 52 5f 4f 4b 29 3d 3d 30 20 29 20 63  RM_OR_OK)==0 ) c
2ebc2 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
2ebc3 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d   assert( pOrTerm
2ebc4 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  ->eOperator==WO_
2ebc5 45 51 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  EQ );.        as
2ebc6 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 6c  sert( pOrTerm->l
2ebc7 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 73  eftCursor==iCurs
2ebc8 6f 72 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  or );.        as
2ebc9 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 75  sert( pOrTerm->u
2ebca 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f  .leftColumn==iCo
2ebcb 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 20 20 20 20  lumn );.        
2ebcc 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
2ebcd 70 72 44 75 70 28 64 62 2c 20 70 4f 72 54 65 72  prDup(db, pOrTer
2ebce 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74  m->pExpr->pRight
2ebcf 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 4c  , 0);.        pL
2ebd0 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
2ebd1 72 4c 69 73 74 41 70 70 65 6e 64 28 70 57 43 2d  rListAppend(pWC-
2ebd2 3e 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20  >pParse, pList, 
2ebd3 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 70  pDup);.        p
2ebd4 4c 65 66 74 20 3d 20 70 4f 72 54 65 72 6d 2d 3e  Left = pOrTerm->
2ebd5 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
2ebd6 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
2ebd7 72 74 28 20 70 4c 65 66 74 21 3d 30 20 29 3b 0a  rt( pLeft!=0 );.
2ebd8 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c        pDup = sql
2ebd9 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2ebda 70 4c 65 66 74 2c 20 30 29 3b 0a 20 20 20 20 20  pLeft, 0);.     
2ebdb 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 50   pNew = sqlite3P
2ebdc 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2ebdd 49 4e 2c 20 70 44 75 70 2c 20 30 2c 20 30 29 3b  IN, pDup, 0, 0);
2ebde 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20  .      if( pNew 
2ebdf 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
2ebe0 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 74  dxNew;.        t
2ebe1 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69  ransferJoinMarki
2ebe2 6e 67 73 28 70 4e 65 77 2c 20 70 45 78 70 72 29  ngs(pNew, pExpr)
2ebe3 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
2ebe4 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
2ebe5 74 79 28 70 4e 65 77 2c 20 45 50 5f 78 49 73 53  ty(pNew, EP_xIsS
2ebe6 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20  elect) );.      
2ebe7 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20    pNew->x.pList 
2ebe8 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20  = pList;.       
2ebe9 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43   idxNew = whereC
2ebea 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c  lauseInsert(pWC,
2ebeb 20 70 4e 65 77 2c 20 54 45 52 4d 5f 56 49 52 54   pNew, TERM_VIRT
2ebec 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43  UAL|TERM_DYNAMIC
2ebed 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
2ebee 61 73 65 28 20 69 64 78 4e 65 77 3d 3d 30 20 29  ase( idxNew==0 )
2ebef 3b 0a 20 20 20 20 20 20 20 20 65 78 70 72 41 6e  ;.        exprAn
2ebf0 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c  alyze(pSrc, pWC,
2ebf1 20 69 64 78 4e 65 77 29 3b 0a 20 20 20 20 20 20   idxNew);.      
2ebf2 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e    pTerm = &pWC->
2ebf3 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20  a[idxTerm];.    
2ebf4 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65      pWC->a[idxNe
2ebf5 77 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78  w].iParent = idx
2ebf6 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 54  Term;.        pT
2ebf7 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b  erm->nChild = 1;
2ebf8 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2ebf9 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2ebfa 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
2ebfb 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a  pList);.      }.
2ebfc 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70        pTerm->eOp
2ebfd 65 72 61 74 6f 72 20 3d 20 30 3b 20 20 2f 2a 20  erator = 0;  /* 
2ebfe 63 61 73 65 20 31 20 74 72 75 6d 70 73 20 63 61  case 1 trumps ca
2ebff 73 65 20 32 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  se 2 */.    }.  
2ec00 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  }.}.#endif /* !S
2ec01 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50  QLITE_OMIT_OR_OP
2ec02 54 49 4d 49 5a 41 54 49 4f 4e 20 26 26 20 21 53  TIMIZATION && !S
2ec03 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2ec04 45 52 59 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54  ERY */.../*.** T
2ec05 68 65 20 69 6e 70 75 74 20 74 6f 20 74 68 69 73  he input to this
2ec06 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 57   routine is an W
2ec07 68 65 72 65 54 65 72 6d 20 73 74 72 75 63 74 75  hereTerm structu
2ec08 72 65 20 77 69 74 68 20 6f 6e 6c 79 20 74 68 65  re with only the
2ec09 0a 2a 2a 20 22 70 45 78 70 72 22 20 66 69 65 6c  .** "pExpr" fiel
2ec0a 64 20 66 69 6c 6c 65 64 20 69 6e 2e 20 20 54 68  d filled in.  Th
2ec0b 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f  e job of this ro
2ec0c 75 74 69 6e 65 20 69 73 20 74 6f 20 61 6e 61 6c  utine is to anal
2ec0d 79 7a 65 20 74 68 65 0a 2a 2a 20 73 75 62 65 78  yze the.** subex
2ec0e 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 6f 70  pression and pop
2ec0f 75 6c 61 74 65 20 61 6c 6c 20 74 68 65 20 6f 74  ulate all the ot
2ec10 68 65 72 20 66 69 65 6c 64 73 20 6f 66 20 74 68  her fields of th
2ec11 65 20 57 68 65 72 65 54 65 72 6d 0a 2a 2a 20 73  e WhereTerm.** s
2ec12 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
2ec13 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
2ec14 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  n is of the form
2ec15 20 22 3c 65 78 70 72 3e 20 3c 6f 70 3e 20 58 22   "<expr> <op> X"
2ec16 20 69 74 20 67 65 74 73 20 63 6f 6d 6d 75 74 65   it gets commute
2ec17 64 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 6e  d.** to the stan
2ec18 64 61 72 64 20 66 6f 72 6d 20 6f 66 20 22 58 20  dard form of "X 
2ec19 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 2e 0a 2a 2a  <op> <expr>"..**
2ec1a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
2ec1b 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20  ssion is of the 
2ec1c 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 59 22 20  form "X <op> Y" 
2ec1d 77 68 65 72 65 20 62 6f 74 68 20 58 20 61 6e 64  where both X and
2ec1e 20 59 20 61 72 65 0a 2a 2a 20 63 6f 6c 75 6d 6e   Y are.** column
2ec1f 73 2c 20 74 68 65 6e 20 74 68 65 20 6f 72 69 67  s, then the orig
2ec20 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20  inal expression 
2ec21 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64  is unchanged and
2ec22 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 0a 2a   a new virtual.*
2ec23 2a 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f  * term of the fo
2ec24 72 6d 20 22 59 20 3c 6f 70 3e 20 58 22 20 69 73  rm "Y <op> X" is
2ec25 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 57 48   added to the WH
2ec26 45 52 45 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a  ERE clause and.*
2ec27 2a 20 61 6e 61 6c 79 7a 65 64 20 73 65 70 61 72  * analyzed separ
2ec28 61 74 65 6c 79 2e 20 20 54 68 65 20 6f 72 69 67  ately.  The orig
2ec29 69 6e 61 6c 20 74 65 72 6d 20 69 73 20 6d 61 72  inal term is mar
2ec2a 6b 65 64 20 77 69 74 68 20 54 45 52 4d 5f 43 4f  ked with TERM_CO
2ec2b 50 49 45 44 0a 2a 2a 20 61 6e 64 20 74 68 65 20  PIED.** and the 
2ec2c 6e 65 77 20 74 65 72 6d 20 69 73 20 6d 61 72 6b  new term is mark
2ec2d 65 64 20 77 69 74 68 20 54 45 52 4d 5f 44 59 4e  ed with TERM_DYN
2ec2e 41 4d 49 43 20 28 62 65 63 61 75 73 65 20 69 74  AMIC (because it
2ec2f 27 73 20 70 45 78 70 72 0a 2a 2a 20 6e 65 65 64  's pExpr.** need
2ec30 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 77 69  s to be freed wi
2ec31 74 68 20 74 68 65 20 57 68 65 72 65 43 6c 61 75  th the WhereClau
2ec32 73 65 29 20 61 6e 64 20 54 45 52 4d 5f 56 49 52  se) and TERM_VIR
2ec33 54 55 41 4c 20 28 62 65 63 61 75 73 65 20 69 74  TUAL (because it
2ec34 0a 2a 2a 20 69 73 20 61 20 63 6f 6d 6d 75 74 65  .** is a commute
2ec35 64 20 63 6f 70 79 20 6f 66 20 61 20 70 72 69 6f  d copy of a prio
2ec36 72 20 74 65 72 6d 2e 29 20 20 54 68 65 20 6f 72  r term.)  The or
2ec37 69 67 69 6e 61 6c 20 74 65 72 6d 20 68 61 73 20  iginal term has 
2ec38 6e 43 68 69 6c 64 3d 31 0a 2a 2a 20 61 6e 64 20  nChild=1.** and 
2ec39 74 68 65 20 63 6f 70 79 20 68 61 73 20 69 64 78  the copy has idx
2ec3a 50 61 72 65 6e 74 20 73 65 74 20 74 6f 20 74 68  Parent set to th
2ec3b 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6f  e index of the o
2ec3c 72 69 67 69 6e 61 6c 20 74 65 72 6d 2e 0a 2a 2f  riginal term..*/
2ec3d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70  .static void exp
2ec3e 72 41 6e 61 6c 79 7a 65 28 0a 20 20 53 72 63 4c  rAnalyze(.  SrcL
2ec3f 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
2ec40 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f        /* the FRO
2ec41 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68  M clause */.  Wh
2ec42 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2ec43 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 57          /* the W
2ec44 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2ec45 20 69 6e 74 20 69 64 78 54 65 72 6d 20 20 20 20   int idxTerm    
2ec46 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2ec47 64 65 78 20 6f 66 20 74 68 65 20 74 65 72 6d 20  dex of the term 
2ec48 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a  to be analyzed *
2ec49 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d  /.){.  WhereTerm
2ec4a 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *pTerm;        
2ec4b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
2ec4c 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  erm to be analyz
2ec4d 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73  ed */.  WhereMas
2ec4e 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20  kSet *pMaskSet; 
2ec4f 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20           /* Set 
2ec50 6f 66 20 74 61 62 6c 65 20 69 6e 64 65 78 20 6d  of table index m
2ec51 61 73 6b 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a  asks */.  Expr *
2ec52 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  pExpr;          
2ec53 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ec54 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
2ec55 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20  be analyzed */. 
2ec56 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 4c   Bitmask prereqL
2ec57 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  eft;            
2ec58 20 20 2f 2a 20 50 72 65 72 65 71 75 65 73 69 74    /* Prerequesit
2ec59 65 73 20 6f 66 20 74 68 65 20 70 45 78 70 72 2d  es of the pExpr-
2ec5a 3e 70 4c 65 66 74 20 2a 2f 0a 20 20 42 69 74 6d  >pLeft */.  Bitm
2ec5b 61 73 6b 20 70 72 65 72 65 71 41 6c 6c 3b 20 20  ask prereqAll;  
2ec5c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ec5d 50 72 65 72 65 71 75 65 73 69 74 65 73 20 6f 66  Prerequesites of
2ec5e 20 70 45 78 70 72 20 2a 2f 0a 20 20 42 69 74 6d   pExpr */.  Bitm
2ec5f 61 73 6b 20 65 78 74 72 61 52 69 67 68 74 20 3d  ask extraRight =
2ec60 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
2ec61 2a 2f 0a 20 20 45 78 70 72 20 2a 70 53 74 72 31  */.  Expr *pStr1
2ec62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2ec63 20 20 20 20 20 20 2f 2a 20 52 48 53 20 6f 66 20        /* RHS of 
2ec64 4c 49 4b 45 2f 47 4c 4f 42 20 6f 70 65 72 61 74  LIKE/GLOB operat
2ec65 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f  or */.  int isCo
2ec66 6d 70 6c 65 74 65 20 3d 20 30 3b 20 20 20 20 20  mplete = 0;     
2ec67 20 20 20 20 20 20 20 20 20 2f 2a 20 52 48 53 20           /* RHS 
2ec68 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 65 6e 64  of LIKE/GLOB end
2ec69 73 20 77 69 74 68 20 77 69 6c 64 63 61 72 64 20  s with wildcard 
2ec6a 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 61 73 65 20  */.  int noCase 
2ec6b 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2ec6c 20 20 20 20 20 20 2f 2a 20 4c 49 4b 45 2f 47 4c        /* LIKE/GL
2ec6d 4f 42 20 64 69 73 74 69 6e 67 75 69 73 68 65 73  OB distinguishes
2ec6e 20 63 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f   case */.  int o
2ec6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
2ec70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2ec71 6f 70 2d 6c 65 76 65 6c 20 6f 70 65 72 61 74 6f  op-level operato
2ec72 72 2e 20 20 70 45 78 70 72 2d 3e 6f 70 20 2a 2f  r.  pExpr->op */
2ec73 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2ec74 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 20   = pWC->pParse; 
2ec75 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2ec76 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 73 71 6c 69  ontext */.  sqli
2ec77 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2ec78 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20  ->db;        /* 
2ec79 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
2ec7a 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62  ion */..  if( db
2ec7b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2ec7c 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
2ec7d 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43  }.  pTerm = &pWC
2ec7e 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20  ->a[idxTerm];.  
2ec7f 70 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d 3e  pMaskSet = pWC->
2ec80 70 4d 61 73 6b 53 65 74 3b 0a 20 20 70 45 78 70  pMaskSet;.  pExp
2ec81 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  r = pTerm->pExpr
2ec82 3b 0a 20 20 70 72 65 72 65 71 4c 65 66 74 20 3d  ;.  prereqLeft =
2ec83 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
2ec84 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d  pMaskSet, pExpr-
2ec85 3e 70 4c 65 66 74 29 3b 0a 20 20 6f 70 20 3d 20  >pLeft);.  op = 
2ec86 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66 28  pExpr->op;.  if(
2ec87 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b 0a 20 20   op==TK_IN ){.  
2ec88 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
2ec89 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20  >pRight==0 );.  
2ec8a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
2ec8b 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
2ec8c 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
2ec8d 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65      pTerm->prere
2ec8e 71 52 69 67 68 74 20 3d 20 65 78 70 72 53 65 6c  qRight = exprSel
2ec8f 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  ectTableUsage(pM
2ec90 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 78  askSet, pExpr->x
2ec91 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 7d  .pSelect);.    }
2ec92 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 65 72  else{.      pTer
2ec93 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d  m->prereqRight =
2ec94 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73   exprListTableUs
2ec95 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45  age(pMaskSet, pE
2ec96 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20  xpr->x.pList);. 
2ec97 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
2ec98 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29   op==TK_ISNULL )
2ec99 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65  {.    pTerm->pre
2ec9a 72 65 71 52 69 67 68 74 20 3d 20 30 3b 0a 20 20  reqRight = 0;.  
2ec9b 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 65 72 6d  }else{.    pTerm
2ec9c 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20  ->prereqRight = 
2ec9d 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
2ec9e 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e  MaskSet, pExpr->
2ec9f 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 70  pRight);.  }.  p
2eca0 72 65 72 65 71 41 6c 6c 20 3d 20 65 78 70 72 54  rereqAll = exprT
2eca1 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2eca2 65 74 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66  et, pExpr);.  if
2eca3 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
2eca4 79 28 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d  y(pExpr, EP_From
2eca5 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 42 69 74  Join) ){.    Bit
2eca6 6d 61 73 6b 20 78 20 3d 20 67 65 74 4d 61 73 6b  mask x = getMask
2eca7 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
2eca8 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c  ->iRightJoinTabl
2eca9 65 29 3b 0a 20 20 20 20 70 72 65 72 65 71 41 6c  e);.    prereqAl
2ecaa 6c 20 7c 3d 20 78 3b 0a 20 20 20 20 65 78 74 72  l |= x;.    extr
2ecab 61 52 69 67 68 74 20 3d 20 78 2d 31 3b 20 20 2f  aRight = x-1;  /
2ecac 2a 20 4f 4e 20 63 6c 61 75 73 65 20 74 65 72 6d  * ON clause term
2ecad 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65  s may not be use
2ecae 64 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 0a  d with an index.
2ecaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ecb0 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 6c 65 66         ** on lef
2ecb1 74 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45 46  t table of a LEF
2ecb2 54 20 4a 4f 49 4e 2e 20 20 54 69 63 6b 65 74 20  T JOIN.  Ticket 
2ecb3 23 33 30 31 35 20 2a 2f 0a 20 20 7d 0a 20 20 70  #3015 */.  }.  p
2ecb4 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20  Term->prereqAll 
2ecb5 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20 70  = prereqAll;.  p
2ecb6 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
2ecb7 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d 3e   = -1;.  pTerm->
2ecb8 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b 0a 20 20  iParent = -1;.  
2ecb9 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2ecba 20 3d 20 30 3b 0a 20 20 69 66 28 20 61 6c 6c 6f   = 0;.  if( allo
2ecbb 77 65 64 4f 70 28 6f 70 29 20 26 26 20 28 70 54  wedOp(op) && (pT
2ecbc 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
2ecbd 20 26 20 70 72 65 72 65 71 4c 65 66 74 29 3d 3d   & prereqLeft)==
2ecbe 30 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  0 ){.    Expr *p
2ecbf 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Left = pExpr->pL
2ecc0 65 66 74 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  eft;.    Expr *p
2ecc1 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70  Right = pExpr->p
2ecc2 52 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20 70  Right;.    if( p
2ecc3 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Left->op==TK_COL
2ecc4 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 70 54 65  UMN ){.      pTe
2ecc5 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d  rm->leftCursor =
2ecc6 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a   pLeft->iTable;.
2ecc7 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 75 2e 6c        pTerm->u.l
2ecc8 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66  eftColumn = pLef
2ecc9 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  t->iColumn;.    
2ecca 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74    pTerm->eOperat
2eccb 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d 61 73  or = operatorMas
2eccc 6b 28 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20  k(op);.    }.   
2eccd 20 69 66 28 20 70 52 69 67 68 74 20 26 26 20 70   if( pRight && p
2ecce 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  Right->op==TK_CO
2eccf 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 57 68  LUMN ){.      Wh
2ecd0 65 72 65 54 65 72 6d 20 2a 70 4e 65 77 3b 0a 20  ereTerm *pNew;. 
2ecd1 20 20 20 20 20 45 78 70 72 20 2a 70 44 75 70 3b       Expr *pDup;
2ecd2 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d  .      if( pTerm
2ecd3 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3e 3d 30 20  ->leftCursor>=0 
2ecd4 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
2ecd5 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70  dxNew;.        p
2ecd6 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Dup = sqlite3Exp
2ecd7 72 44 75 70 28 64 62 2c 20 70 45 78 70 72 2c 20  rDup(db, pExpr, 
2ecd8 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
2ecd9 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2ecda 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
2ecdb 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2ecdc 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20  db, pDup);.     
2ecdd 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2ecde 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
2ecdf 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61  dxNew = whereCla
2ece0 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70  useInsert(pWC, p
2ece1 44 75 70 2c 20 54 45 52 4d 5f 56 49 52 54 55 41  Dup, TERM_VIRTUA
2ece2 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b  L|TERM_DYNAMIC);
2ece3 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 64 78  .        if( idx
2ece4 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  New==0 ) return;
2ece5 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20  .        pNew = 
2ece6 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b  &pWC->a[idxNew];
2ece7 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69  .        pNew->i
2ece8 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d  Parent = idxTerm
2ece9 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 20  ;.        pTerm 
2ecea 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72  = &pWC->a[idxTer
2eceb 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72  m];.        pTer
2ecec 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20  m->nChild = 1;. 
2eced 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77 74         pTerm->wt
2ecee 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f  Flags |= TERM_CO
2ecef 50 49 45 44 3b 0a 20 20 20 20 20 20 7d 65 6c 73  PIED;.      }els
2ecf0 65 7b 0a 20 20 20 20 20 20 20 20 70 44 75 70 20  e{.        pDup 
2ecf1 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  = pExpr;.       
2ecf2 20 70 4e 65 77 20 3d 20 70 54 65 72 6d 3b 0a 20   pNew = pTerm;. 
2ecf3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 78 70       }.      exp
2ecf4 72 43 6f 6d 6d 75 74 65 28 70 50 61 72 73 65 2c  rCommute(pParse,
2ecf5 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 70 4c   pDup);.      pL
2ecf6 65 66 74 20 3d 20 70 44 75 70 2d 3e 70 4c 65 66  eft = pDup->pLef
2ecf7 74 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c  t;.      pNew->l
2ecf8 65 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66  eftCursor = pLef
2ecf9 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20  t->iTable;.     
2ecfa 20 70 4e 65 77 2d 3e 75 2e 6c 65 66 74 43 6f 6c   pNew->u.leftCol
2ecfb 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f  umn = pLeft->iCo
2ecfc 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65 77  lumn;.      pNew
2ecfd 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20  ->prereqRight = 
2ecfe 70 72 65 72 65 71 4c 65 66 74 3b 0a 20 20 20 20  prereqLeft;.    
2ecff 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 41 6c    pNew->prereqAl
2ed00 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a 20  l = prereqAll;. 
2ed01 20 20 20 20 20 70 4e 65 77 2d 3e 65 4f 70 65 72       pNew->eOper
2ed02 61 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d  ator = operatorM
2ed03 61 73 6b 28 70 44 75 70 2d 3e 6f 70 29 3b 0a 20  ask(pDup->op);. 
2ed04 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 6e 64 65     }.  }..#ifnde
2ed05 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 45  f SQLITE_OMIT_BE
2ed06 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54 49  TWEEN_OPTIMIZATI
2ed07 4f 4e 0a 20 20 2f 2a 20 49 66 20 61 20 74 65 72  ON.  /* If a ter
2ed08 6d 20 69 73 20 74 68 65 20 42 45 54 57 45 45 4e  m is the BETWEEN
2ed09 20 6f 70 65 72 61 74 6f 72 2c 20 63 72 65 61 74   operator, creat
2ed0a 65 20 74 77 6f 20 6e 65 77 20 76 69 72 74 75 61  e two new virtua
2ed0b 6c 20 74 65 72 6d 73 0a 20 20 2a 2a 20 74 68 61  l terms.  ** tha
2ed0c 74 20 64 65 66 69 6e 65 20 74 68 65 20 72 61 6e  t define the ran
2ed0d 67 65 20 74 68 61 74 20 74 68 65 20 42 45 54 57  ge that the BETW
2ed0e 45 45 4e 20 69 6d 70 6c 65 6d 65 6e 74 73 2e 20  EEN implements. 
2ed0f 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20 20   For example:.  
2ed10 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 61 20 42  **.  **      a B
2ed11 45 54 57 45 45 4e 20 62 20 41 4e 44 20 63 0a 20  ETWEEN b AND c. 
2ed12 20 2a 2a 0a 20 20 2a 2a 20 69 73 20 63 6f 6e 76   **.  ** is conv
2ed13 65 72 74 65 64 20 69 6e 74 6f 3a 0a 20 20 2a 2a  erted into:.  **
2ed14 0a 20 20 2a 2a 20 20 20 20 20 20 28 61 20 42 45  .  **      (a BE
2ed15 54 57 45 45 4e 20 62 20 41 4e 44 20 63 29 20 41  TWEEN b AND c) A
2ed16 4e 44 20 28 61 3e 3d 62 29 20 41 4e 44 20 28 61  ND (a>=b) AND (a
2ed17 3c 3d 63 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  <=c).  **.  ** T
2ed18 68 65 20 74 77 6f 20 6e 65 77 20 74 65 72 6d 73  he two new terms
2ed19 20 61 72 65 20 61 64 64 65 64 20 6f 6e 74 6f 20   are added onto 
2ed1a 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57  the end of the W
2ed1b 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65 63  hereClause objec
2ed1c 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 65 77 20  t..  ** The new 
2ed1d 74 65 72 6d 73 20 61 72 65 20 22 64 79 6e 61 6d  terms are "dynam
2ed1e 69 63 22 20 61 6e 64 20 61 72 65 20 63 68 69 6c  ic" and are chil
2ed1f 64 72 65 6e 20 6f 66 20 74 68 65 20 6f 72 69 67  dren of the orig
2ed20 69 6e 61 6c 20 42 45 54 57 45 45 4e 0a 20 20 2a  inal BETWEEN.  *
2ed21 2a 20 74 65 72 6d 2e 20 20 54 68 61 74 20 6d 65  * term.  That me
2ed22 61 6e 73 20 74 68 61 74 20 69 66 20 74 68 65 20  ans that if the 
2ed23 42 45 54 57 45 45 4e 20 74 65 72 6d 20 69 73 20  BETWEEN term is 
2ed24 63 6f 64 65 64 2c 20 74 68 65 20 63 68 69 6c 64  coded, the child
2ed25 72 65 6e 20 61 72 65 0a 20 20 2a 2a 20 73 6b 69  ren are.  ** ski
2ed26 70 70 65 64 2e 20 20 4f 72 2c 20 69 66 20 74 68  pped.  Or, if th
2ed27 65 20 63 68 69 6c 64 72 65 6e 20 61 72 65 20 73  e children are s
2ed28 61 74 69 73 66 69 65 64 20 62 79 20 61 6e 20 69  atisfied by an i
2ed29 6e 64 65 78 2c 20 74 68 65 20 6f 72 69 67 69 6e  ndex, the origin
2ed2a 61 6c 0a 20 20 2a 2a 20 42 45 54 57 45 45 4e 20  al.  ** BETWEEN 
2ed2b 74 65 72 6d 20 69 73 20 73 6b 69 70 70 65 64 2e  term is skipped.
2ed2c 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28  .  */.  else if(
2ed2d 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 42   pExpr->op==TK_B
2ed2e 45 54 57 45 45 4e 20 26 26 20 70 57 43 2d 3e 6f  ETWEEN && pWC->o
2ed2f 70 3d 3d 54 4b 5f 41 4e 44 20 29 7b 0a 20 20 20  p==TK_AND ){.   
2ed30 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2ed31 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   = pExpr->x.pLis
2ed32 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  t;.    int i;.  
2ed33 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2ed34 38 20 6f 70 73 5b 5d 20 3d 20 7b 54 4b 5f 47 45  8 ops[] = {TK_GE
2ed35 2c 20 54 4b 5f 4c 45 7d 3b 0a 20 20 20 20 61 73  , TK_LE};.    as
2ed36 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29  sert( pList!=0 )
2ed37 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c  ;.    assert( pL
2ed38 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 32 20 29 3b  ist->nExpr==2 );
2ed39 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2ed3a 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45  2; i++){.      E
2ed3b 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a 20  xpr *pNewExpr;. 
2ed3c 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b       int idxNew;
2ed3d 0a 20 20 20 20 20 20 70 4e 65 77 45 78 70 72 20  .      pNewExpr 
2ed3e 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2ed3f 50 61 72 73 65 2c 20 6f 70 73 5b 69 5d 2c 20 0a  Parse, ops[i], .
2ed40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed41 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2ed42 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2ed43 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 30 29  pExpr->pLeft, 0)
2ed44 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2ed45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2ed46 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2ed47 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  , pList->a[i].pE
2ed48 78 70 72 2c 20 30 29 2c 20 30 29 3b 0a 20 20 20  xpr, 0), 0);.   
2ed49 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72     idxNew = wher
2ed4a 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57  eClauseInsert(pW
2ed4b 43 2c 20 70 4e 65 77 45 78 70 72 2c 20 54 45 52  C, pNewExpr, TER
2ed4c 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
2ed4d 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 74  YNAMIC);.      t
2ed4e 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 3d  estcase( idxNew=
2ed4f 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72  =0 );.      expr
2ed50 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57  Analyze(pSrc, pW
2ed51 43 2c 20 69 64 78 4e 65 77 29 3b 0a 20 20 20 20  C, idxNew);.    
2ed52 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e    pTerm = &pWC->
2ed53 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20  a[idxTerm];.    
2ed54 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d    pWC->a[idxNew]
2ed55 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65  .iParent = idxTe
2ed56 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54  rm;.    }.    pT
2ed57 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b  erm->nChild = 2;
2ed58 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
2ed59 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45  QLITE_OMIT_BETWE
2ed5a 45 4e 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  EN_OPTIMIZATION 
2ed5b 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  */..#if !defined
2ed5c 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f  (SQLITE_OMIT_OR_
2ed5d 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26  OPTIMIZATION) &&
2ed5e 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2ed5f 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a  _OMIT_SUBQUERY).
2ed60 20 20 2f 2a 20 41 6e 61 6c 79 7a 65 20 61 20 74    /* Analyze a t
2ed61 65 72 6d 20 74 68 61 74 20 69 73 20 63 6f 6d 70  erm that is comp
2ed62 6f 73 65 64 20 6f 66 20 74 77 6f 20 6f 72 20 6d  osed of two or m
2ed63 6f 72 65 20 73 75 62 74 65 72 6d 73 20 63 6f 6e  ore subterms con
2ed64 6e 65 63 74 65 64 20 62 79 0a 20 20 2a 2a 20 61  nected by.  ** a
2ed65 6e 20 4f 52 20 6f 70 65 72 61 74 6f 72 2e 0a 20  n OR operator.. 
2ed66 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 20 70   */.  else if( p
2ed67 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52 20  Expr->op==TK_OR 
2ed68 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
2ed69 57 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29  WC->op==TK_AND )
2ed6a 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a  ;.    exprAnalyz
2ed6b 65 4f 72 54 65 72 6d 28 70 53 72 63 2c 20 70 57  eOrTerm(pSrc, pW
2ed6c 43 2c 20 69 64 78 54 65 72 6d 29 3b 0a 20 20 20  C, idxTerm);.   
2ed6d 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
2ed6e 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 7d 0a 23  [idxTerm];.  }.#
2ed6f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2ed70 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2ed71 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  TION */..#ifndef
2ed72 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b   SQLITE_OMIT_LIK
2ed73 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20  E_OPTIMIZATION. 
2ed74 20 2f 2a 20 41 64 64 20 63 6f 6e 73 74 72 61 69   /* Add constrai
2ed75 6e 74 73 20 74 6f 20 72 65 64 75 63 65 20 74 68  nts to reduce th
2ed76 65 20 73 65 61 72 63 68 20 73 70 61 63 65 20 6f  e search space o
2ed77 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42  n a LIKE or GLOB
2ed78 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a  .  ** operator..
2ed79 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 6c 69 6b 65    **.  ** A like
2ed7a 20 70 61 74 74 65 72 6e 20 6f 66 20 74 68 65 20   pattern of the 
2ed7b 66 6f 72 6d 20 22 78 20 4c 49 4b 45 20 27 61 62  form "x LIKE 'ab
2ed7c 63 25 27 22 20 69 73 20 63 68 61 6e 67 65 64 20  c%'" is changed 
2ed7d 69 6e 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 73  into constraints
2ed7e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
2ed7f 20 20 20 20 78 3e 3d 27 61 62 63 27 20 41 4e 44      x>='abc' AND
2ed80 20 78 3c 27 61 62 64 27 20 41 4e 44 20 78 20 4c   x<'abd' AND x L
2ed81 49 4b 45 20 27 61 62 63 25 27 0a 20 20 2a 2a 0a  IKE 'abc%'.  **.
2ed82 20 20 2a 2a 20 54 68 65 20 6c 61 73 74 20 63 68    ** The last ch
2ed83 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 20 70  aracter of the p
2ed84 72 65 66 69 78 20 22 61 62 63 22 20 69 73 20 69  refix "abc" is i
2ed85 6e 63 72 65 6d 65 6e 74 65 64 20 74 6f 20 66 6f  ncremented to fo
2ed86 72 6d 20 74 68 65 0a 20 20 2a 2a 20 74 65 72 6d  rm the.  ** term
2ed87 69 6e 61 74 69 6f 6e 20 63 6f 6e 64 69 74 69 6f  ination conditio
2ed88 6e 20 22 61 62 64 22 2e 0a 20 20 2a 2f 0a 20 20  n "abd"..  */.  
2ed89 69 66 28 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f  if( pWC->op==TK_
2ed8a 41 4e 44 20 0a 20 20 20 26 26 20 69 73 4c 69 6b  AND .   && isLik
2ed8b 65 4f 72 47 6c 6f 62 28 70 50 61 72 73 65 2c 20  eOrGlob(pParse, 
2ed8c 70 45 78 70 72 2c 20 26 70 53 74 72 31 2c 20 26  pExpr, &pStr1, &
2ed8d 69 73 43 6f 6d 70 6c 65 74 65 2c 20 26 6e 6f 43  isComplete, &noC
2ed8e 61 73 65 29 0a 20 20 29 7b 0a 20 20 20 20 45 78  ase).  ){.    Ex
2ed8f 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20  pr *pLeft;      
2ed90 20 2f 2a 20 4c 48 53 20 6f 66 20 4c 49 4b 45 2f   /* LHS of LIKE/
2ed91 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 2a 2f  GLOB operator */
2ed92 0a 20 20 20 20 45 78 70 72 20 2a 70 53 74 72 32  .    Expr *pStr2
2ed93 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20  ;       /* Copy 
2ed94 6f 66 20 70 53 74 72 31 20 2d 20 52 48 53 20 6f  of pStr1 - RHS o
2ed95 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f 70 65 72  f LIKE/GLOB oper
2ed96 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72  ator */.    Expr
2ed97 20 2a 70 4e 65 77 45 78 70 72 31 3b 0a 20 20 20   *pNewExpr1;.   
2ed98 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 32   Expr *pNewExpr2
2ed99 3b 0a 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77  ;.    int idxNew
2ed9a 31 3b 0a 20 20 20 20 69 6e 74 20 69 64 78 4e 65  1;.    int idxNe
2ed9b 77 32 3b 0a 0a 20 20 20 20 70 4c 65 66 74 20 3d  w2;..    pLeft =
2ed9c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d   pExpr->x.pList-
2ed9d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[1].pExpr;.   
2ed9e 20 70 53 74 72 32 20 3d 20 73 71 6c 69 74 65 33   pStr2 = sqlite3
2ed9f 45 78 70 72 44 75 70 28 64 62 2c 20 70 53 74 72  ExprDup(db, pStr
2eda0 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 21  1, 0);.    if( !
2eda1 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2eda2 20 29 7b 0a 20 20 20 20 20 20 75 38 20 63 2c 20   ){.      u8 c, 
2eda3 2a 70 43 3b 20 20 20 20 20 20 20 2f 2a 20 4c 61  *pC;       /* La
2eda4 73 74 20 63 68 61 72 61 63 74 65 72 20 62 65 66  st character bef
2eda5 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 77 69  ore the first wi
2eda6 6c 64 63 61 72 64 20 2a 2f 0a 20 20 20 20 20 20  ldcard */.      
2eda7 70 43 20 3d 20 28 75 38 2a 29 26 70 53 74 72 32  pC = (u8*)&pStr2
2eda8 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 73 71 6c 69 74  ->u.zToken[sqlit
2eda9 65 33 53 74 72 6c 65 6e 33 30 28 70 53 74 72 32  e3Strlen30(pStr2
2edaa 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2d 31 5d 3b 0a  ->u.zToken)-1];.
2edab 20 20 20 20 20 20 63 20 3d 20 2a 70 43 3b 0a 20        c = *pC;. 
2edac 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73 65 20       if( noCase 
2edad 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
2edae 65 20 70 6f 69 6e 74 20 69 73 20 74 6f 20 69 6e  e point is to in
2edaf 63 72 65 6d 65 6e 74 20 74 68 65 20 6c 61 73 74  crement the last
2edb0 20 63 68 61 72 61 63 74 65 72 20 62 65 66 6f 72   character befor
2edb1 65 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20  e the first.    
2edb2 20 20 20 20 2a 2a 20 77 69 6c 64 63 61 72 64 2e      ** wildcard.
2edb3 20 20 42 75 74 20 69 66 20 77 65 20 69 6e 63 72    But if we incr
2edb4 65 6d 65 6e 74 20 27 40 27 2c 20 74 68 61 74 20  ement '@', that 
2edb5 77 69 6c 6c 20 70 75 73 68 20 69 74 20 69 6e 74  will push it int
2edb6 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  o the.        **
2edb7 20 61 6c 70 68 61 62 65 74 69 63 20 72 61 6e 67   alphabetic rang
2edb8 65 20 77 68 65 72 65 20 63 61 73 65 20 63 6f 6e  e where case con
2edb9 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c 20 6d 65  versions will me
2edba 73 73 20 75 70 20 74 68 65 20 0a 20 20 20 20 20  ss up the .     
2edbb 20 20 20 2a 2a 20 69 6e 65 71 75 61 6c 69 74 79     ** inequality
2edbc 2e 20 20 54 6f 20 61 76 6f 69 64 20 74 68 69 73  .  To avoid this
2edbd 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 6f 20 61  , make sure to a
2edbe 6c 73 6f 20 72 75 6e 20 74 68 65 20 66 75 6c 6c  lso run the full
2edbf 0a 20 20 20 20 20 20 20 20 2a 2a 20 4c 49 4b 45  .        ** LIKE
2edc0 20 6f 6e 20 61 6c 6c 20 63 61 6e 64 69 64 61 74   on all candidat
2edc1 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 62 79  e expressions by
2edc2 20 63 6c 65 61 72 69 6e 67 20 74 68 65 20 69 73   clearing the is
2edc3 43 6f 6d 70 6c 65 74 65 20 66 6c 61 67 0a 20 20  Complete flag.  
2edc4 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2edc5 20 69 66 28 20 63 3d 3d 27 41 27 2d 31 20 29 20   if( c=='A'-1 ) 
2edc6 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 30 3b 0a  isComplete = 0;.
2edc7 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c  .        c = sql
2edc8 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
2edc9 5b 63 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  [c];.      }.   
2edca 20 20 20 2a 70 43 20 3d 20 63 20 2b 20 31 3b 0a     *pC = c + 1;.
2edcb 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 45 78      }.    pNewEx
2edcc 70 72 31 20 3d 20 73 71 6c 69 74 65 33 50 45 78  pr1 = sqlite3PEx
2edcd 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 47 45  pr(pParse, TK_GE
2edce 2c 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  , sqlite3ExprDup
2edcf 28 64 62 2c 70 4c 65 66 74 2c 30 29 2c 70 53 74  (db,pLeft,0),pSt
2edd0 72 31 2c 30 29 3b 0a 20 20 20 20 69 64 78 4e 65  r1,0);.    idxNe
2edd1 77 31 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65  w1 = whereClause
2edd2 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77  Insert(pWC, pNew
2edd3 45 78 70 72 31 2c 20 54 45 52 4d 5f 56 49 52 54  Expr1, TERM_VIRT
2edd4 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43  UAL|TERM_DYNAMIC
2edd5 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2edd6 20 69 64 78 4e 65 77 31 3d 3d 30 20 29 3b 0a 20   idxNew1==0 );. 
2edd7 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70     exprAnalyze(p
2edd8 53 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77  Src, pWC, idxNew
2edd9 31 29 3b 0a 20 20 20 20 70 4e 65 77 45 78 70 72  1);.    pNewExpr
2edda 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2eddb 28 70 50 61 72 73 65 2c 20 54 4b 5f 4c 54 2c 20  (pParse, TK_LT, 
2eddc 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2eddd 62 2c 70 4c 65 66 74 2c 30 29 2c 70 53 74 72 32  b,pLeft,0),pStr2
2edde 2c 30 29 3b 0a 20 20 20 20 69 64 78 4e 65 77 32  ,0);.    idxNew2
2eddf 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e   = whereClauseIn
2ede0 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78  sert(pWC, pNewEx
2ede1 70 72 32 2c 20 54 45 52 4d 5f 56 49 52 54 55 41  pr2, TERM_VIRTUA
2ede2 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b  L|TERM_DYNAMIC);
2ede3 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69  .    testcase( i
2ede4 64 78 4e 65 77 32 3d 3d 30 20 29 3b 0a 20 20 20  dxNew2==0 );.   
2ede5 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72   exprAnalyze(pSr
2ede6 63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 32 29  c, pWC, idxNew2)
2ede7 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70  ;.    pTerm = &p
2ede8 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a  WC->a[idxTerm];.
2ede9 20 20 20 20 69 66 28 20 69 73 43 6f 6d 70 6c 65      if( isComple
2edea 74 65 20 29 7b 0a 20 20 20 20 20 20 70 57 43 2d  te ){.      pWC-
2edeb 3e 61 5b 69 64 78 4e 65 77 31 5d 2e 69 50 61 72  >a[idxNew1].iPar
2edec 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20  ent = idxTerm;. 
2eded 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e       pWC->a[idxN
2edee 65 77 32 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69  ew2].iParent = i
2edef 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 54  dxTerm;.      pT
2edf0 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b  erm->nChild = 2;
2edf1 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
2edf2 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2edf3 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49  _LIKE_OPTIMIZATI
2edf4 4f 4e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  ON */..#ifndef S
2edf5 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2edf6 41 4c 54 41 42 4c 45 0a 20 20 2f 2a 20 41 64 64  ALTABLE.  /* Add
2edf7 20 61 20 57 4f 5f 4d 41 54 43 48 20 61 75 78 69   a WO_MATCH auxi
2edf8 6c 69 61 72 79 20 74 65 72 6d 20 74 6f 20 74 68  liary term to th
2edf9 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 73 65 74  e constraint set
2edfa 20 69 66 20 74 68 65 0a 20 20 2a 2a 20 63 75 72   if the.  ** cur
2edfb 72 65 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 20  rent expression 
2edfc 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20  is of the form: 
2edfd 20 63 6f 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78   column MATCH ex
2edfe 70 72 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6e  pr..  ** This in
2edff 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65  formation is use
2ee00 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e  d by the xBestIn
2ee01 64 65 78 20 6d 65 74 68 6f 64 73 20 6f 66 0a 20  dex methods of. 
2ee02 20 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c   ** virtual tabl
2ee03 65 73 2e 20 20 54 68 65 20 6e 61 74 69 76 65 20  es.  The native 
2ee04 71 75 65 72 79 20 6f 70 74 69 6d 69 7a 65 72 20  query optimizer 
2ee05 64 6f 65 73 20 6e 6f 74 20 61 74 74 65 6d 70 74  does not attempt
2ee06 0a 20 20 2a 2a 20 74 6f 20 64 6f 20 61 6e 79 74  .  ** to do anyt
2ee07 68 69 6e 67 20 77 69 74 68 20 4d 41 54 43 48 20  hing with MATCH 
2ee08 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a  functions..  */.
2ee09 20 20 69 66 28 20 69 73 4d 61 74 63 68 4f 66 43    if( isMatchOfC
2ee0a 6f 6c 75 6d 6e 28 70 45 78 70 72 29 20 29 7b 0a  olumn(pExpr) ){.
2ee0b 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a      int idxNew;.
2ee0c 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74      Expr *pRight
2ee0d 2c 20 2a 70 4c 65 66 74 3b 0a 20 20 20 20 57 68  , *pLeft;.    Wh
2ee0e 65 72 65 54 65 72 6d 20 2a 70 4e 65 77 54 65 72  ereTerm *pNewTer
2ee0f 6d 3b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 70  m;.    Bitmask p
2ee10 72 65 72 65 71 43 6f 6c 75 6d 6e 2c 20 70 72 65  rereqColumn, pre
2ee11 72 65 71 45 78 70 72 3b 0a 0a 20 20 20 20 70 52  reqExpr;..    pR
2ee12 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e  ight = pExpr->x.
2ee13 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  pList->a[0].pExp
2ee14 72 3b 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70  r;.    pLeft = p
2ee15 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61  Expr->x.pList->a
2ee16 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70  [1].pExpr;.    p
2ee17 72 65 72 65 71 45 78 70 72 20 3d 20 65 78 70 72  rereqExpr = expr
2ee18 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2ee19 53 65 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20  Set, pRight);.  
2ee1a 20 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e 20 3d    prereqColumn =
2ee1b 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
2ee1c 70 4d 61 73 6b 53 65 74 2c 20 70 4c 65 66 74 29  pMaskSet, pLeft)
2ee1d 3b 0a 20 20 20 20 69 66 28 20 28 70 72 65 72 65  ;.    if( (prere
2ee1e 71 45 78 70 72 20 26 20 70 72 65 72 65 71 43 6f  qExpr & prereqCo
2ee1f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20  lumn)==0 ){.    
2ee20 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72    Expr *pNewExpr
2ee21 3b 0a 20 20 20 20 20 20 70 4e 65 77 45 78 70 72  ;.      pNewExpr
2ee22 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2ee23 70 50 61 72 73 65 2c 20 54 4b 5f 4d 41 54 43 48  pParse, TK_MATCH
2ee24 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2ee25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee26 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70 72 44   0, sqlite3ExprD
2ee27 75 70 28 64 62 2c 20 70 52 69 67 68 74 2c 20 30  up(db, pRight, 0
2ee28 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 64 78  ), 0);.      idx
2ee29 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73  New = whereClaus
2ee2a 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65  eInsert(pWC, pNe
2ee2b 77 45 78 70 72 2c 20 54 45 52 4d 5f 56 49 52 54  wExpr, TERM_VIRT
2ee2c 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43  UAL|TERM_DYNAMIC
2ee2d 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2ee2e 65 28 20 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0a  e( idxNew==0 );.
2ee2f 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 20 3d        pNewTerm =
2ee30 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d   &pWC->a[idxNew]
2ee31 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d  ;.      pNewTerm
2ee32 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20  ->prereqRight = 
2ee33 70 72 65 72 65 71 45 78 70 72 3b 0a 20 20 20 20  prereqExpr;.    
2ee34 20 20 70 4e 65 77 54 65 72 6d 2d 3e 6c 65 66 74    pNewTerm->left
2ee35 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e  Cursor = pLeft->
2ee36 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 4e  iTable;.      pN
2ee37 65 77 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f  ewTerm->u.leftCo
2ee38 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43  lumn = pLeft->iC
2ee39 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65  olumn;.      pNe
2ee3a 77 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  wTerm->eOperator
2ee3b 20 3d 20 57 4f 5f 4d 41 54 43 48 3b 0a 20 20 20   = WO_MATCH;.   
2ee3c 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 69 50 61     pNewTerm->iPa
2ee3d 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a  rent = idxTerm;.
2ee3e 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70        pTerm = &p
2ee3f 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a  WC->a[idxTerm];.
2ee40 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68        pTerm->nCh
2ee41 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  ild = 1;.      p
2ee42 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d  Term->wtFlags |=
2ee43 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a 20 20   TERM_COPIED;.  
2ee44 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72      pNewTerm->pr
2ee45 65 72 65 71 41 6c 6c 20 3d 20 70 54 65 72 6d 2d  ereqAll = pTerm-
2ee46 3e 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20 20 20  >prereqAll;.    
2ee47 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  }.  }.#endif /* 
2ee48 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2ee49 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 20 20 2f  UALTABLE */..  /
2ee4a 2a 20 50 72 65 76 65 6e 74 20 4f 4e 20 63 6c 61  * Prevent ON cla
2ee4b 75 73 65 20 74 65 72 6d 73 20 6f 66 20 61 20 4c  use terms of a L
2ee4c 45 46 54 20 4a 4f 49 4e 20 66 72 6f 6d 20 62 65  EFT JOIN from be
2ee4d 69 6e 67 20 75 73 65 64 20 74 6f 20 64 72 69 76  ing used to driv
2ee4e 65 0a 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20  e.  ** an index 
2ee4f 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20 74 68  for tables to th
2ee50 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20 6a 6f  e left of the jo
2ee51 69 6e 2e 0a 20 20 2a 2f 0a 20 20 70 54 65 72 6d  in..  */.  pTerm
2ee52 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 7c 3d  ->prereqRight |=
2ee53 20 65 78 74 72 61 52 69 67 68 74 3b 0a 7d 0a 0a   extraRight;.}..
2ee54 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  /*.** Return TRU
2ee55 45 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20  E if any of the 
2ee56 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 70  expressions in p
2ee57 4c 69 73 74 2d 3e 61 5b 69 46 69 72 73 74 2e 2e  List->a[iFirst..
2ee58 2e 5d 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 20  .] contain.** a 
2ee59 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e 79  reference to any
2ee5a 20 74 61 62 6c 65 20 6f 74 68 65 72 20 74 68 61   table other tha
2ee5b 6e 20 74 68 65 20 69 42 61 73 65 20 74 61 62 6c  n the iBase tabl
2ee5c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2ee5d 20 72 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72   referencesOther
2ee5e 54 61 62 6c 65 73 28 0a 20 20 45 78 70 72 4c 69  Tables(.  ExprLi
2ee5f 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20  st *pList,      
2ee60 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 65 78      /* Search ex
2ee61 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 73  pressions in ths
2ee62 20 6c 69 73 74 20 2a 2f 0a 20 20 57 68 65 72 65   list */.  Where
2ee63 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2ee64 74 2c 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20  t,   /* Mapping 
2ee65 66 72 6f 6d 20 74 61 62 6c 65 73 20 74 6f 20 62  from tables to b
2ee66 69 74 6d 61 70 73 20 2a 2f 0a 20 20 69 6e 74 20  itmaps */.  int 
2ee67 69 46 69 72 73 74 2c 20 20 20 20 20 20 20 20 20  iFirst,         
2ee68 20 20 20 20 20 20 2f 2a 20 42 65 20 73 65 61 72        /* Be sear
2ee69 63 68 69 6e 67 20 77 69 74 68 20 74 68 65 20 69  ching with the i
2ee6a 46 69 72 73 74 2d 74 68 20 65 78 70 72 65 73 73  First-th express
2ee6b 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 42 61  ion */.  int iBa
2ee6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  se              
2ee6d 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 72 65 66     /* Ignore ref
2ee6e 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20  erences to this 
2ee6f 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 42 69  table */.){.  Bi
2ee70 74 6d 61 73 6b 20 61 6c 6c 6f 77 65 64 20 3d 20  tmask allowed = 
2ee71 7e 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65  ~getMask(pMaskSe
2ee72 74 2c 20 69 42 61 73 65 29 3b 0a 20 20 77 68 69  t, iBase);.  whi
2ee73 6c 65 28 20 69 46 69 72 73 74 3c 70 4c 69 73 74  le( iFirst<pList
2ee74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 69  ->nExpr ){.    i
2ee75 66 28 20 28 65 78 70 72 54 61 62 6c 65 55 73 61  f( (exprTableUsa
2ee76 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c 69  ge(pMaskSet, pLi
2ee77 73 74 2d 3e 61 5b 69 46 69 72 73 74 2b 2b 5d 2e  st->a[iFirst++].
2ee78 70 45 78 70 72 29 26 61 6c 6c 6f 77 65 64 29 21  pExpr)&allowed)!
2ee79 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
2ee7a 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 1;.    }.  }.
2ee7b 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a    return 0;.}...
2ee7c 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2ee7d 6e 65 20 64 65 63 69 64 65 73 20 69 66 20 70 49  ne decides if pI
2ee7e 64 78 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  dx can be used t
2ee7f 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52  o satisfy the OR
2ee80 44 45 52 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65  DER BY.** clause
2ee81 2e 20 20 49 66 20 69 74 20 63 61 6e 2c 20 69 74  .  If it can, it
2ee82 20 72 65 74 75 72 6e 73 20 31 2e 20 20 49 66 20   returns 1.  If 
2ee83 70 49 64 78 20 63 61 6e 6e 6f 74 20 73 61 74 69  pIdx cannot sati
2ee84 73 66 79 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52  sfy the.** ORDER
2ee85 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 69 73   BY clause, this
2ee86 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
2ee87 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 4f 72 64 65 72   0..**.** pOrder
2ee88 42 79 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  By is an ORDER B
2ee89 59 20 63 6c 61 75 73 65 20 66 72 6f 6d 20 61 20  Y clause from a 
2ee8a 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2ee8b 2e 20 20 70 54 61 62 20 69 73 20 74 68 65 0a 2a  .  pTab is the.*
2ee8c 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74 61 62 6c  * left-most tabl
2ee8d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  e in the FROM cl
2ee8e 61 75 73 65 20 6f 66 20 74 68 61 74 20 73 61 6d  ause of that sam
2ee8f 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2ee90 6e 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 74 61  nt and.** the ta
2ee91 62 6c 65 20 68 61 73 20 61 20 63 75 72 73 6f 72  ble has a cursor
2ee92 20 6e 75 6d 62 65 72 20 6f 66 20 22 62 61 73 65   number of "base
2ee93 22 2e 20 20 70 49 64 78 20 69 73 20 61 6e 20 69  ".  pIdx is an i
2ee94 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a  ndex on pTab..**
2ee95 0a 2a 2a 20 6e 45 71 43 6f 6c 20 69 73 20 74 68  .** nEqCol is th
2ee96 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
2ee97 6d 6e 73 20 6f 66 20 70 49 64 78 20 74 68 61 74  mns of pIdx that
2ee98 20 61 72 65 20 75 73 65 64 20 61 73 20 65 71 75   are used as equ
2ee99 61 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73 74 72 61  ality.** constra
2ee9a 69 6e 74 73 2e 20 20 41 6e 79 20 6f 66 20 74 68  ints.  Any of th
2ee9b 65 73 65 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20  ese columns may 
2ee9c 62 65 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20  be missing from 
2ee9d 74 68 65 20 4f 52 44 45 52 20 42 59 0a 2a 2a 20  the ORDER BY.** 
2ee9e 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20 6d  clause and the m
2ee9f 61 74 63 68 20 63 61 6e 20 73 74 69 6c 6c 20 62  atch can still b
2eea0 65 20 61 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a  e a success..**.
2eea1 2a 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20  ** All terms of 
2eea2 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 68 61  the ORDER BY tha
2eea3 74 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20  t match against 
2eea4 74 68 65 20 69 6e 64 65 78 20 6d 75 73 74 20 62  the index must b
2eea5 65 20 65 69 74 68 65 72 0a 2a 2a 20 41 53 43 20  e either.** ASC 
2eea6 6f 72 20 44 45 53 43 2e 20 20 28 54 65 72 6d 73  or DESC.  (Terms
2eea7 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2eea8 20 63 6c 61 75 73 65 20 70 61 73 74 20 74 68 65   clause past the
2eea9 20 65 6e 64 20 6f 66 20 61 20 55 4e 49 51 55 45   end of a UNIQUE
2eeaa 0a 2a 2a 20 69 6e 64 65 78 20 64 6f 20 6e 6f 74  .** index do not
2eeab 20 6e 65 65 64 20 74 6f 20 73 61 74 69 73 66 79   need to satisfy
2eeac 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74   this constraint
2eead 2e 29 20 20 54 68 65 20 2a 70 62 52 65 76 20 76  .)  The *pbRev v
2eeae 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65 74 20 74  alue is.** set t
2eeaf 6f 20 31 20 69 66 20 74 68 65 20 4f 52 44 45 52  o 1 if the ORDER
2eeb0 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 6c   BY clause is al
2eeb1 6c 20 44 45 53 43 20 61 6e 64 20 69 74 20 69 73  l DESC and it is
2eeb2 20 73 65 74 20 74 6f 20 30 20 69 66 0a 2a 2a 20   set to 0 if.** 
2eeb3 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
2eeb4 75 73 65 20 69 73 20 61 6c 6c 20 41 53 43 2e 0a  use is all ASC..
2eeb5 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  */.static int is
2eeb6 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 0a 20 20  SortingIndex(.  
2eeb7 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2eeb8 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
2eeb9 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2eeba 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d  WhereMaskSet *pM
2eebb 61 73 6b 53 65 74 2c 20 2f 2a 20 4d 61 70 70 69  askSet, /* Mappi
2eebc 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 20 63 75  ng from table cu
2eebd 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 74 6f 20  rsor numbers to 
2eebe 62 69 74 6d 61 70 73 20 2a 2f 0a 20 20 49 6e 64  bitmaps */.  Ind
2eebf 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20  ex *pIdx,       
2eec0 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65       /* The inde
2eec1 78 20 77 65 20 61 72 65 20 74 65 73 74 69 6e 67  x we are testing
2eec2 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 2c 20   */.  int base, 
2eec3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2eec4 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
2eec5 6f 72 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  or the table to 
2eec6 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a 20 20 45  be sorted */.  E
2eec7 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
2eec8 79 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52  y,     /* The OR
2eec9 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
2eeca 0a 20 20 69 6e 74 20 6e 45 71 43 6f 6c 2c 20 20  .  int nEqCol,  
2eecb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2eecc 6d 62 65 72 20 6f 66 20 69 6e 64 65 78 20 63 6f  mber of index co
2eecd 6c 75 6d 6e 73 20 77 69 74 68 20 3d 3d 20 63 6f  lumns with == co
2eece 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69  nstraints */.  i
2eecf 6e 74 20 2a 70 62 52 65 76 20 20 20 20 20 20 20  nt *pbRev       
2eed0 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f         /* Set to
2eed1 20 31 20 69 66 20 4f 52 44 45 52 20 42 59 20 69   1 if ORDER BY i
2eed2 73 20 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 69  s DESC */.){.  i
2eed3 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
2eed4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2eed5 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
2eed6 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64  */.  int sortOrd
2eed7 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  er = 0;         
2eed8 20 20 20 20 20 2f 2a 20 58 4f 52 20 6f 66 20 69       /* XOR of i
2eed9 6e 64 65 78 20 61 6e 64 20 4f 52 44 45 52 20 42  ndex and ORDER B
2eeda 59 20 73 6f 72 74 20 64 69 72 65 63 74 69 6f 6e  Y sort direction
2eedb 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   */.  int nTerm;
2eedc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eedd 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2eede 6f 66 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  of ORDER BY term
2eedf 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78  s */.  struct Ex
2eee0 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 65  prList_item *pTe
2eee1 72 6d 3b 20 20 20 20 2f 2a 20 41 20 74 65 72 6d  rm;    /* A term
2eee2 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2eee3 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 71 6c   clause */.  sql
2eee4 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2eee5 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74  e->db;..  assert
2eee6 28 20 70 4f 72 64 65 72 42 79 21 3d 30 20 29 3b  ( pOrderBy!=0 );
2eee7 0a 20 20 6e 54 65 72 6d 20 3d 20 70 4f 72 64 65  .  nTerm = pOrde
2eee8 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 73  rBy->nExpr;.  as
2eee9 73 65 72 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b  sert( nTerm>0 );
2eeea 0a 0a 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20  ..  /* Argument 
2eeeb 70 49 64 78 20 6d 75 73 74 20 65 69 74 68 65 72  pIdx must either
2eeec 20 70 6f 69 6e 74 20 74 6f 20 61 20 27 72 65 61   point to a 'rea
2eeed 6c 27 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 73  l' named index s
2eeee 74 72 75 63 74 75 72 65 2c 20 0a 20 20 2a 2a 20  tructure, .  ** 
2eeef 6f 72 20 61 6e 20 69 6e 64 65 78 20 73 74 72 75  or an index stru
2eef0 63 74 75 72 65 20 61 6c 6c 6f 63 61 74 65 64 20  cture allocated 
2eef1 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 62 79 20  on the stack by 
2eef2 62 65 73 74 42 74 72 65 65 49 6e 64 65 78 28 29  bestBtreeIndex()
2eef3 20 74 6f 0a 20 20 2a 2a 20 72 65 70 72 65 73 65   to.  ** represe
2eef4 6e 74 20 74 68 65 20 72 6f 77 69 64 20 69 6e 64  nt the rowid ind
2eef5 65 78 20 74 68 61 74 20 69 73 20 70 61 72 74 20  ex that is part 
2eef6 6f 66 20 65 76 65 72 79 20 74 61 62 6c 65 2e 20  of every table. 
2eef7 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49   */.  assert( pI
2eef8 64 78 2d 3e 7a 4e 61 6d 65 20 7c 7c 20 28 70 49  dx->zName || (pI
2eef9 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 26  dx->nColumn==1 &
2eefa 26 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  & pIdx->aiColumn
2eefb 5b 30 5d 3d 3d 2d 31 29 20 29 3b 0a 0a 20 20 2f  [0]==-1) );..  /
2eefc 2a 20 4d 61 74 63 68 20 74 65 72 6d 73 20 6f 66  * Match terms of
2eefd 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2eefe 61 75 73 65 20 61 67 61 69 6e 73 74 20 63 6f 6c  ause against col
2eeff 75 6d 6e 73 20 6f 66 0a 20 20 2a 2a 20 74 68 65  umns of.  ** the
2ef00 20 69 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a   index..  **.  *
2ef01 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 6e 64 69  * Note that indi
2ef02 63 65 73 20 68 61 76 65 20 70 49 64 78 2d 3e 6e  ces have pIdx->n
2ef03 43 6f 6c 75 6d 6e 20 72 65 67 75 6c 61 72 20 63  Column regular c
2ef04 6f 6c 75 6d 6e 73 20 70 6c 75 73 0a 20 20 2a 2a  olumns plus.  **
2ef05 20 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20   one additional 
2ef06 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 6e  column containin
2ef07 67 20 74 68 65 20 72 6f 77 69 64 2e 20 20 54 68  g the rowid.  Th
2ef08 65 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 0a 20  e rowid column. 
2ef09 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 65 78   ** of the index
2ef0a 20 69 73 20 61 6c 73 6f 20 61 6c 6c 6f 77 65 64   is also allowed
2ef0b 20 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e 73   to match agains
2ef0c 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 20  t the ORDER BY. 
2ef0d 20 2a 2a 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f   ** clause..  */
2ef0e 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70 54  .  for(i=j=0, pT
2ef0f 65 72 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b  erm=pOrderBy->a;
2ef10 20 6a 3c 6e 54 65 72 6d 20 26 26 20 69 3c 3d 70   j<nTerm && i<=p
2ef11 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Idx->nColumn; i+
2ef12 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45  +){.    Expr *pE
2ef13 78 70 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  xpr;       /* Th
2ef14 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20  e expression of 
2ef15 74 68 65 20 4f 52 44 45 52 20 42 59 20 70 54 65  the ORDER BY pTe
2ef16 72 6d 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65  rm */.    CollSe
2ef17 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20  q *pColl;    /* 
2ef18 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  The collating se
2ef19 71 75 65 6e 63 65 20 6f 66 20 70 45 78 70 72 20  quence of pExpr 
2ef1a 2a 2f 0a 20 20 20 20 69 6e 74 20 74 65 72 6d 53  */.    int termS
2ef1b 6f 72 74 4f 72 64 65 72 3b 20 2f 2a 20 53 6f 72  ortOrder; /* Sor
2ef1c 74 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73  t order for this
2ef1d 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74   term */.    int
2ef1e 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   iColumn;       
2ef1f 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 6f 6c 75  /* The i-th colu
2ef20 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e  mn of the index.
2ef21 20 20 2d 31 20 66 6f 72 20 72 6f 77 69 64 20 2a    -1 for rowid *
2ef22 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f 72 74 4f  /.    int iSortO
2ef23 72 64 65 72 3b 20 20 20 20 2f 2a 20 31 20 66 6f  rder;    /* 1 fo
2ef24 72 20 44 45 53 43 2c 20 30 20 66 6f 72 20 41 53  r DESC, 0 for AS
2ef25 43 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 69 6e  C on the i-th in
2ef26 64 65 78 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20  dex term */.    
2ef27 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
2ef28 6c 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  l; /* Name of th
2ef29 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
2ef2a 65 6e 63 65 20 66 6f 72 20 69 2d 74 68 20 69 6e  ence for i-th in
2ef2b 64 65 78 20 74 65 72 6d 20 2a 2f 0a 0a 20 20 20  dex term */..   
2ef2c 20 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e   pExpr = pTerm->
2ef2d 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70  pExpr;.    if( p
2ef2e 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c  Expr->op!=TK_COL
2ef2f 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54  UMN || pExpr->iT
2ef30 61 62 6c 65 21 3d 62 61 73 65 20 29 7b 0a 20 20  able!=base ){.  
2ef31 20 20 20 20 2f 2a 20 43 61 6e 20 6e 6f 74 20 75      /* Can not u
2ef32 73 65 20 61 6e 20 69 6e 64 65 78 20 73 6f 72 74  se an index sort
2ef33 20 6f 6e 20 61 6e 79 74 68 69 6e 67 20 74 68 61   on anything tha
2ef34 74 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d  t is not a colum
2ef35 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a  n in the.      *
2ef36 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74 61 62 6c  * left-most tabl
2ef37 65 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  e of the FROM cl
2ef38 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 62 72  ause */.      br
2ef39 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  eak;.    }.    p
2ef3a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
2ef3b 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
2ef3c 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 69 66  , pExpr);.    if
2ef3d 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
2ef3e 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44    pColl = db->pD
2ef3f 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20  fltColl;.    }. 
2ef40 20 20 20 69 66 28 20 70 49 64 78 2d 3e 7a 4e 61     if( pIdx->zNa
2ef41 6d 65 20 26 26 20 69 3c 70 49 64 78 2d 3e 6e 43  me && i<pIdx->nC
2ef42 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 69  olumn ){.      i
2ef43 43 6f 6c 75 6d 6e 20 3d 20 70 49 64 78 2d 3e 61  Column = pIdx->a
2ef44 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20  iColumn[i];.    
2ef45 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d 70    if( iColumn==p
2ef46 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 69 50 4b  Idx->pTable->iPK
2ef47 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69 43  ey ){.        iC
2ef48 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20  olumn = -1;.    
2ef49 20 20 7d 0a 20 20 20 20 20 20 69 53 6f 72 74 4f    }.      iSortO
2ef4a 72 64 65 72 20 3d 20 70 49 64 78 2d 3e 61 53 6f  rder = pIdx->aSo
2ef4b 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 20 20  rtOrder[i];.    
2ef4c 20 20 7a 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e    zColl = pIdx->
2ef4d 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 7d  azColl[i];.    }
2ef4e 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 43 6f 6c  else{.      iCol
2ef4f 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  umn = -1;.      
2ef50 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 30 3b 0a  iSortOrder = 0;.
2ef51 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 43        zColl = pC
2ef52 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  oll->zName;.    
2ef53 7d 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d  }.    if( pExpr-
2ef54 3e 69 43 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75 6d  >iColumn!=iColum
2ef55 6e 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 49  n || sqlite3StrI
2ef56 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  Cmp(pColl->zName
2ef57 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20  , zColl) ){.    
2ef58 20 20 2f 2a 20 54 65 72 6d 20 6a 20 6f 66 20 74    /* Term j of t
2ef59 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2ef5a 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63  se does not matc
2ef5b 68 20 63 6f 6c 75 6d 6e 20 69 20 6f 66 20 74 68  h column i of th
2ef5c 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 20  e index */.     
2ef5d 20 69 66 28 20 69 3c 6e 45 71 43 6f 6c 20 29 7b   if( i<nEqCol ){
2ef5e 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61  .        /* If a
2ef5f 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 74  n index column t
2ef60 68 61 74 20 69 73 20 63 6f 6e 73 74 72 61 69 6e  hat is constrain
2ef61 65 64 20 62 79 20 3d 3d 20 66 61 69 6c 73 20 74  ed by == fails t
2ef62 6f 20 6d 61 74 63 68 20 61 6e 0a 20 20 20 20 20  o match an.     
2ef63 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20 74     ** ORDER BY t
2ef64 65 72 6d 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e  erm, that is OK.
2ef65 20 20 4a 75 73 74 20 69 67 6e 6f 72 65 20 74 68    Just ignore th
2ef66 61 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  at column of the
2ef67 20 69 6e 64 65 78 0a 20 20 20 20 20 20 20 20 2a   index.        *
2ef68 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e  /.        contin
2ef69 75 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  ue;.      }else 
2ef6a 69 66 28 20 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f  if( i==pIdx->nCo
2ef6b 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  lumn ){.        
2ef6c 2f 2a 20 49 6e 64 65 78 20 63 6f 6c 75 6d 6e 20  /* Index column 
2ef6d 69 20 69 73 20 74 68 65 20 72 6f 77 69 64 2e 20  i is the rowid. 
2ef6e 20 41 6c 6c 20 6f 74 68 65 72 20 74 65 72 6d 73   All other terms
2ef6f 20 6d 61 74 63 68 2e 20 2a 2f 0a 20 20 20 20 20   match. */.     
2ef70 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ef71 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
2ef72 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 6f  * If an index co
2ef73 6c 75 6d 6e 20 66 61 69 6c 73 20 74 6f 20 6d 61  lumn fails to ma
2ef74 74 63 68 20 61 6e 64 20 69 73 20 6e 6f 74 20 63  tch and is not c
2ef75 6f 6e 73 74 72 61 69 6e 65 64 20 62 79 20 3d 3d  onstrained by ==
2ef76 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  .        ** then
2ef77 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 6e 6f   the index canno
2ef78 74 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52  t satisfy the OR
2ef79 44 45 52 20 42 59 20 63 6f 6e 73 74 72 61 69 6e  DER BY constrain
2ef7a 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  t..        */.  
2ef7b 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
2ef7c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2ef7d 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e    assert( pIdx->
2ef7e 61 53 6f 72 74 4f 72 64 65 72 21 3d 30 20 7c 7c  aSortOrder!=0 ||
2ef7f 20 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 29 3b 0a   iColumn==-1 );.
2ef80 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72      assert( pTer
2ef81 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3d 3d 30 20  m->sortOrder==0 
2ef82 7c 7c 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72  || pTerm->sortOr
2ef83 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73  der==1 );.    as
2ef84 73 65 72 74 28 20 69 53 6f 72 74 4f 72 64 65 72  sert( iSortOrder
2ef85 3d 3d 30 20 7c 7c 20 69 53 6f 72 74 4f 72 64 65  ==0 || iSortOrde
2ef86 72 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65 72 6d  r==1 );.    term
2ef87 53 6f 72 74 4f 72 64 65 72 20 3d 20 69 53 6f 72  SortOrder = iSor
2ef88 74 4f 72 64 65 72 20 5e 20 70 54 65 72 6d 2d 3e  tOrder ^ pTerm->
2ef89 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 69  sortOrder;.    i
2ef8a 66 28 20 69 3e 6e 45 71 43 6f 6c 20 29 7b 0a 20  f( i>nEqCol ){. 
2ef8b 20 20 20 20 20 69 66 28 20 74 65 72 6d 53 6f 72       if( termSor
2ef8c 74 4f 72 64 65 72 21 3d 73 6f 72 74 4f 72 64 65  tOrder!=sortOrde
2ef8d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  r ){.        /* 
2ef8e 49 6e 64 69 63 65 73 20 63 61 6e 20 6f 6e 6c 79  Indices can only
2ef8f 20 62 65 20 75 73 65 64 20 69 66 20 61 6c 6c 20   be used if all 
2ef90 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 70  ORDER BY terms p
2ef91 61 73 74 20 74 68 65 0a 20 20 20 20 20 20 20 20  ast the.        
2ef92 2a 2a 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73  ** equality cons
2ef93 74 72 61 69 6e 74 73 20 61 72 65 20 61 6c 6c 20  traints are all 
2ef94 65 69 74 68 65 72 20 44 45 53 43 20 6f 72 20 41  either DESC or A
2ef95 53 43 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  SC. */.        r
2ef96 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
2ef97 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2ef98 20 20 73 6f 72 74 4f 72 64 65 72 20 3d 20 74 65    sortOrder = te
2ef99 72 6d 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20  rmSortOrder;.   
2ef9a 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20   }.    j++;.    
2ef9b 70 54 65 72 6d 2b 2b 3b 0a 20 20 20 20 69 66 28  pTerm++;.    if(
2ef9c 20 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 72   iColumn<0 && !r
2ef9d 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54 61  eferencesOtherTa
2ef9e 62 6c 65 73 28 70 4f 72 64 65 72 42 79 2c 20 70  bles(pOrderBy, p
2ef9f 4d 61 73 6b 53 65 74 2c 20 6a 2c 20 62 61 73 65  MaskSet, j, base
2efa0 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  ) ){.      /* If
2efa1 20 74 68 65 20 69 6e 64 65 78 65 64 20 63 6f 6c   the indexed col
2efa2 75 6d 6e 20 69 73 20 74 68 65 20 70 72 69 6d 61  umn is the prima
2efa3 72 79 20 6b 65 79 20 61 6e 64 20 65 76 65 72 79  ry key and every
2efa4 74 68 69 6e 67 20 6d 61 74 63 68 65 73 0a 20 20  thing matches.  
2efa5 20 20 20 20 2a 2a 20 73 6f 20 66 61 72 20 61 6e      ** so far an
2efa6 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 4f 52  d none of the OR
2efa7 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 6f 20  DER BY terms to 
2efa8 74 68 65 20 72 69 67 68 74 20 72 65 66 65 72 65  the right refere
2efa9 6e 63 65 20 6f 74 68 65 72 0a 20 20 20 20 20 20  nce other.      
2efaa 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  ** tables in the
2efab 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 77 65 20 61   join, then we a
2efac 72 65 20 61 73 73 75 72 65 64 20 74 68 61 74 20  re assured that 
2efad 74 68 65 20 69 6e 64 65 78 20 63 61 6e 20 62 65  the index can be
2efae 20 75 73 65 64 20 0a 20 20 20 20 20 20 2a 2a 20   used .      ** 
2efaf 74 6f 20 73 6f 72 74 20 62 65 63 61 75 73 65 20  to sort because 
2efb0 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20  the primary key 
2efb1 69 73 20 75 6e 69 71 75 65 20 61 6e 64 20 73 6f  is unique and so
2efb2 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 6f 74 68   none of the oth
2efb3 65 72 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75  er.      ** colu
2efb4 6d 6e 73 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e  mns will make an
2efb5 79 20 64 69 66 66 65 72 65 6e 63 65 0a 20 20 20  y difference.   
2efb6 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a 20 3d 20     */.      j = 
2efb7 6e 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d  nTerm;.    }.  }
2efb8 0a 0a 20 20 2a 70 62 52 65 76 20 3d 20 73 6f 72  ..  *pbRev = sor
2efb9 74 4f 72 64 65 72 21 3d 30 3b 0a 20 20 69 66 28  tOrder!=0;.  if(
2efba 20 6a 3e 3d 6e 54 65 72 6d 20 29 7b 0a 20 20 20   j>=nTerm ){.   
2efbb 20 2f 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66   /* All terms of
2efbc 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2efbd 61 75 73 65 20 61 72 65 20 63 6f 76 65 72 65 64  ause are covered
2efbe 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 20 73   by this index s
2efbf 6f 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 6e  o.    ** this in
2efc0 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 64 20  dex can be used 
2efc1 66 6f 72 20 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a  for sorting. */.
2efc2 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2efc3 7d 0a 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e  }.  if( pIdx->on
2efc4 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 26  Error!=OE_None &
2efc5 26 20 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75  & i==pIdx->nColu
2efc6 6d 6e 0a 20 20 20 20 20 20 26 26 20 21 72 65 66  mn.      && !ref
2efc7 65 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c  erencesOtherTabl
2efc8 65 73 28 70 4f 72 64 65 72 42 79 2c 20 70 4d 61  es(pOrderBy, pMa
2efc9 73 6b 53 65 74 2c 20 6a 2c 20 62 61 73 65 29 20  skSet, j, base) 
2efca 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 74 65  ){.    /* All te
2efcb 72 6d 73 20 6f 66 20 74 68 69 73 20 69 6e 64 65  rms of this inde
2efcc 78 20 6d 61 74 63 68 20 73 6f 6d 65 20 70 72 65  x match some pre
2efcd 66 69 78 20 6f 66 20 74 68 65 20 4f 52 44 45 52  fix of the ORDER
2efce 20 42 59 20 63 6c 61 75 73 65 0a 20 20 20 20 2a   BY clause.    *
2efcf 2a 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20  * and the index 
2efd0 69 73 20 55 4e 49 51 55 45 20 61 6e 64 20 6e 6f  is UNIQUE and no
2efd1 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20 74 61   terms on the ta
2efd2 69 6c 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  il of the ORDER 
2efd3 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73 65  BY.    ** clause
2efd4 20 72 65 66 65 72 65 6e 63 65 20 6f 74 68 65 72   reference other
2efd5 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69   tables in a joi
2efd6 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 61  n.  If this is a
2efd7 6c 6c 20 74 72 75 65 20 74 68 65 6e 0a 20 20 20  ll true then.   
2efd8 20 2a 2a 20 74 68 65 20 6f 72 64 65 72 20 62 79   ** the order by
2efd9 20 63 6c 61 75 73 65 20 69 73 20 73 75 70 65 72   clause is super
2efda 66 6c 75 6f 75 73 2e 20 2a 2f 0a 20 20 20 20 72  fluous. */.    r
2efdb 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72  eturn 1;.  }.  r
2efdc 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
2efdd 2a 20 50 72 65 70 61 72 65 20 61 20 63 72 75 64  * Prepare a crud
2efde 65 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68  e estimate of th
2efdf 65 20 6c 6f 67 61 72 69 74 68 6d 20 6f 66 20 74  e logarithm of t
2efe0 68 65 20 69 6e 70 75 74 20 76 61 6c 75 65 2e 0a  he input value..
2efe1 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6e  ** The results n
2efe2 65 65 64 20 6e 6f 74 20 62 65 20 65 78 61 63 74  eed not be exact
2efe3 2e 20 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20  .  This is only 
2efe4 75 73 65 64 20 66 6f 72 20 65 73 74 69 6d 61 74  used for estimat
2efe5 69 6e 67 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c  ing.** the total
2efe6 20 63 6f 73 74 20 6f 66 20 70 65 72 66 6f 72 6d   cost of perform
2efe7 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 20 77  ing operations w
2efe8 69 74 68 20 4f 28 6c 6f 67 4e 29 20 6f 72 20 4f  ith O(logN) or O
2efe9 28 4e 6c 6f 67 4e 29 0a 2a 2a 20 63 6f 6d 70 6c  (NlogN).** compl
2efea 65 78 69 74 79 2e 20 20 42 65 63 61 75 73 65 20  exity.  Because 
2efeb 4e 20 69 73 20 6a 75 73 74 20 61 20 67 75 65 73  N is just a gues
2efec 73 2c 20 69 74 20 69 73 20 6e 6f 20 67 72 65 61  s, it is no grea
2efed 74 20 74 72 61 67 65 64 79 20 69 66 0a 2a 2a 20  t tragedy if.** 
2efee 6c 6f 67 4e 20 69 73 20 61 20 6c 69 74 74 6c 65  logN is a little
2efef 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   off..*/.static 
2eff0 64 6f 75 62 6c 65 20 65 73 74 4c 6f 67 28 64 6f  double estLog(do
2eff1 75 62 6c 65 20 4e 29 7b 0a 20 20 64 6f 75 62 6c  uble N){.  doubl
2eff2 65 20 6c 6f 67 4e 20 3d 20 31 3b 0a 20 20 64 6f  e logN = 1;.  do
2eff3 75 62 6c 65 20 78 20 3d 20 31 30 3b 0a 20 20 77  uble x = 10;.  w
2eff4 68 69 6c 65 28 20 4e 3e 78 20 29 7b 0a 20 20 20  hile( N>x ){.   
2eff5 20 6c 6f 67 4e 20 2b 3d 20 31 3b 0a 20 20 20 20   logN += 1;.    
2eff6 78 20 2a 3d 20 31 30 3b 0a 20 20 7d 0a 20 20 72  x *= 10;.  }.  r
2eff7 65 74 75 72 6e 20 6c 6f 67 4e 3b 0a 7d 0a 0a 2f  eturn logN;.}../
2eff8 2a 0a 2a 2a 20 54 77 6f 20 72 6f 75 74 69 6e 65  *.** Two routine
2eff9 73 20 66 6f 72 20 70 72 69 6e 74 69 6e 67 20 74  s for printing t
2effa 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e  he content of an
2effb 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2effc 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  nfo.** structure
2effd 2e 20 20 55 73 65 64 20 66 6f 72 20 74 65 73 74  .  Used for test
2effe 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
2efff 67 20 6f 6e 6c 79 2e 20 20 49 66 20 6e 65 69 74  g only.  If neit
2f000 68 65 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45  her.** SQLITE_TE
2f001 53 54 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 42  ST or SQLITE_DEB
2f002 55 47 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20  UG are defined, 
2f003 74 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69  then these routi
2f004 6e 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 2d 6f 70  nes.** are no-op
2f005 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  s..*/.#if !defin
2f006 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2f007 49 52 54 55 41 4c 54 41 42 4c 45 29 20 26 26 20  IRTUALTABLE) && 
2f008 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
2f009 45 42 55 47 29 0a 73 74 61 74 69 63 20 76 6f 69  EBUG).static voi
2f00a 64 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50 55  d TRACE_IDX_INPU
2f00b 54 53 28 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  TS(sqlite3_index
2f00c 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74  _info *p){.  int
2f00d 20 69 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74   i;.  if( !sqlit
2f00e 65 33 57 68 65 72 65 54 72 61 63 65 20 29 20 72  e3WhereTrace ) r
2f00f 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30  eturn;.  for(i=0
2f010 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69  ; i<p->nConstrai
2f011 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  nt; i++){.    sq
2f012 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2f013 28 22 20 20 63 6f 6e 73 74 72 61 69 6e 74 5b 25  ("  constraint[%
2f014 64 5d 3a 20 63 6f 6c 3d 25 64 20 74 65 72 6d 69  d]: col=%d termi
2f015 64 3d 25 64 20 6f 70 3d 25 64 20 75 73 61 62 6c  d=%d op=%d usabl
2f016 65 64 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20  ed=%d\n",.      
2f017 20 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43   i,.       p->aC
2f018 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 69 43 6f  onstraint[i].iCo
2f019 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 70 2d 3e  lumn,.       p->
2f01a 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 69  aConstraint[i].i
2f01b 54 65 72 6d 4f 66 66 73 65 74 2c 0a 20 20 20 20  TermOffset,.    
2f01c 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e     p->aConstrain
2f01d 74 5b 69 5d 2e 6f 70 2c 0a 20 20 20 20 20 20 20  t[i].op,.       
2f01e 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  p->aConstraint[i
2f01f 5d 2e 75 73 61 62 6c 65 29 3b 0a 20 20 7d 0a 20  ].usable);.  }. 
2f020 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
2f021 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20  OrderBy; i++){. 
2f022 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2f023 72 69 6e 74 66 28 22 20 20 6f 72 64 65 72 62 79  rintf("  orderby
2f024 5b 25 64 5d 3a 20 63 6f 6c 3d 25 64 20 64 65 73  [%d]: col=%d des
2f025 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20  c=%d\n",.       
2f026 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 4f 72  i,.       p->aOr
2f027 64 65 72 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e  derBy[i].iColumn
2f028 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 4f 72 64  ,.       p->aOrd
2f029 65 72 42 79 5b 69 5d 2e 64 65 73 63 29 3b 0a 20  erBy[i].desc);. 
2f02a 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
2f02b 20 54 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55   TRACE_IDX_OUTPU
2f02c 54 53 28 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  TS(sqlite3_index
2f02d 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74  _info *p){.  int
2f02e 20 69 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74   i;.  if( !sqlit
2f02f 65 33 57 68 65 72 65 54 72 61 63 65 20 29 20 72  e3WhereTrace ) r
2f030 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30  eturn;.  for(i=0
2f031 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69  ; i<p->nConstrai
2f032 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  nt; i++){.    sq
2f033 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2f034 28 22 20 20 75 73 61 67 65 5b 25 64 5d 3a 20 61  ("  usage[%d]: a
2f035 72 67 76 49 64 78 3d 25 64 20 6f 6d 69 74 3d 25  rgvIdx=%d omit=%
2f036 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a  d\n",.       i,.
2f037 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74         p->aConst
2f038 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 61 72  raintUsage[i].ar
2f039 67 76 49 6e 64 65 78 2c 0a 20 20 20 20 20 20 20  gvIndex,.       
2f03a 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  p->aConstraintUs
2f03b 61 67 65 5b 69 5d 2e 6f 6d 69 74 29 3b 0a 20 20  age[i].omit);.  
2f03c 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67  }.  sqlite3Debug
2f03d 50 72 69 6e 74 66 28 22 20 20 69 64 78 4e 75 6d  Printf("  idxNum
2f03e 3d 25 64 5c 6e 22 2c 20 70 2d 3e 69 64 78 4e 75  =%d\n", p->idxNu
2f03f 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62  m);.  sqlite3Deb
2f040 75 67 50 72 69 6e 74 66 28 22 20 20 69 64 78 53  ugPrintf("  idxS
2f041 74 72 3d 25 73 5c 6e 22 2c 20 70 2d 3e 69 64 78  tr=%s\n", p->idx
2f042 53 74 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Str);.  sqlite3D
2f043 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 6f 72  ebugPrintf("  or
2f044 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3d 25 64  derByConsumed=%d
2f045 5c 6e 22 2c 20 70 2d 3e 6f 72 64 65 72 42 79 43  \n", p->orderByC
2f046 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 73 71 6c 69  onsumed);.  sqli
2f047 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2f048 20 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3d    estimatedCost=
2f049 25 67 5c 6e 22 2c 20 70 2d 3e 65 73 74 69 6d 61  %g\n", p->estima
2f04a 74 65 64 43 6f 73 74 29 3b 0a 7d 0a 23 65 6c 73  tedCost);.}.#els
2f04b 65 0a 23 64 65 66 69 6e 65 20 54 52 41 43 45 5f  e.#define TRACE_
2f04c 49 44 58 5f 49 4e 50 55 54 53 28 41 29 0a 23 64  IDX_INPUTS(A).#d
2f04d 65 66 69 6e 65 20 54 52 41 43 45 5f 49 44 58 5f  efine TRACE_IDX_
2f04e 4f 55 54 50 55 54 53 28 41 29 0a 23 65 6e 64 69  OUTPUTS(A).#endi
2f04f 66 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 71 75 69 72  f../* .** Requir
2f050 65 64 20 62 65 63 61 75 73 65 20 62 65 73 74 49  ed because bestI
2f051 6e 64 65 78 28 29 20 69 73 20 63 61 6c 6c 65 64  ndex() is called
2f052 20 62 79 20 62 65 73 74 4f 72 43 6c 61 75 73 65   by bestOrClause
2f053 49 6e 64 65 78 28 29 20 0a 2a 2f 0a 73 74 61 74  Index() .*/.stat
2f054 69 63 20 76 6f 69 64 20 62 65 73 74 49 6e 64 65  ic void bestInde
2f055 78 28 0a 20 20 20 20 50 61 72 73 65 2a 2c 20 57  x(.    Parse*, W
2f056 68 65 72 65 43 6c 61 75 73 65 2a 2c 20 73 74 72  hereClause*, str
2f057 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2f058 2a 2c 20 42 69 74 6d 61 73 6b 2c 20 45 78 70 72  *, Bitmask, Expr
2f059 4c 69 73 74 2a 2c 20 57 68 65 72 65 43 6f 73 74  List*, WhereCost
2f05a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  *);../*.** This 
2f05b 72 6f 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73  routine attempts
2f05c 20 74 6f 20 66 69 6e 64 20 61 6e 20 73 63 61 6e   to find an scan
2f05d 6e 69 6e 67 20 73 74 72 61 74 65 67 79 20 74 68  ning strategy th
2f05e 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 0a  at can be used .
2f05f 2a 2a 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 61  ** to optimize a
2f060 6e 20 27 4f 52 27 20 65 78 70 72 65 73 73 69 6f  n 'OR' expressio
2f061 6e 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f  n that is part o
2f062 66 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65  f a WHERE clause
2f063 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62  . .**.** The tab
2f064 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  le associated wi
2f065 74 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74  th FROM clause t
2f066 65 72 6d 20 70 53 72 63 20 6d 61 79 20 62 65 20  erm pSrc may be 
2f067 65 69 74 68 65 72 20 61 0a 2a 2a 20 72 65 67 75  either a.** regu
2f068 6c 61 72 20 42 2d 54 72 65 65 20 74 61 62 6c 65  lar B-Tree table
2f069 20 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61   or a virtual ta
2f06a 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
2f06b 6f 69 64 20 62 65 73 74 4f 72 43 6c 61 75 73 65  oid bestOrClause
2f06c 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a  Index(.  Parse *
2f06d 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2f06e 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2f06f 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2f070 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2f071 43 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  C,           /* 
2f072 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
2f073 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
2f074 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c  List_item *pSrc,
2f075 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
2f076 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61  ause term to sea
2f077 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  rch */.  Bitmask
2f078 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20   notReady,      
2f079 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
2f07a 63 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65  cursors that are
2f07b 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a   not available *
2f07c 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
2f07d 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20 20  rderBy,         
2f07e 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
2f07f 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72  clause */.  Wher
2f080 65 43 6f 73 74 20 2a 70 43 6f 73 74 20 20 20 20  eCost *pCost    
2f081 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73          /* Lowes
2f082 74 20 63 6f 73 74 20 71 75 65 72 79 20 70 6c 61  t cost query pla
2f083 6e 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20  n */.){.#ifndef 
2f084 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f  SQLITE_OMIT_OR_O
2f085 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 63 6f  PTIMIZATION.  co
2f086 6e 73 74 20 69 6e 74 20 69 43 75 72 20 3d 20 70  nst int iCur = p
2f087 53 72 63 2d 3e 69 43 75 72 73 6f 72 3b 20 20 20  Src->iCursor;   
2f088 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 6f 66  /* The cursor of
2f089 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
2f08a 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 63   accessed */.  c
2f08b 6f 6e 73 74 20 42 69 74 6d 61 73 6b 20 6d 61 73  onst Bitmask mas
2f08c 6b 53 72 63 20 3d 20 67 65 74 4d 61 73 6b 28 70  kSrc = getMask(p
2f08d 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 69 43  WC->pMaskSet, iC
2f08e 75 72 29 3b 20 20 2f 2a 20 42 69 74 6d 61 73 6b  ur);  /* Bitmask
2f08f 20 66 6f 72 20 70 53 72 63 20 2a 2f 0a 20 20 57   for pSrc */.  W
2f090 68 65 72 65 54 65 72 6d 20 2a 20 63 6f 6e 73 74  hereTerm * const
2f091 20 70 57 43 45 6e 64 20 3d 20 26 70 57 43 2d 3e   pWCEnd = &pWC->
2f092 61 5b 70 57 43 2d 3e 6e 54 65 72 6d 5d 3b 20 20  a[pWC->nTerm];  
2f093 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20        /* End of 
2f094 70 57 43 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 57 68  pWC->a[] */.  Wh
2f095 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20  ereTerm *pTerm; 
2f096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f097 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74 65 72 6d  /* A single term
2f098 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
2f099 61 75 73 65 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65  ause */..  /* Se
2f09a 61 72 63 68 20 74 68 65 20 57 48 45 52 45 20 63  arch the WHERE c
2f09b 6c 61 75 73 65 20 74 65 72 6d 73 20 66 6f 72 20  lause terms for 
2f09c 61 20 75 73 61 62 6c 65 20 57 4f 5f 4f 52 20 74  a usable WO_OR t
2f09d 65 72 6d 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 54  erm. */.  for(pT
2f09e 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 70 54 65 72  erm=pWC->a; pTer
2f09f 6d 3c 70 57 43 45 6e 64 3b 20 70 54 65 72 6d 2b  m<pWCEnd; pTerm+
2f0a0 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 72  +){.    if( pTer
2f0a1 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
2f0a2 5f 4f 52 20 0a 20 20 20 20 20 26 26 20 28 28 70  _OR .     && ((p
2f0a3 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20  Term->prereqAll 
2f0a4 26 20 7e 6d 61 73 6b 53 72 63 29 20 26 20 6e 6f  & ~maskSrc) & no
2f0a5 74 52 65 61 64 79 29 3d 3d 30 0a 20 20 20 20 20  tReady)==0.     
2f0a6 26 26 20 28 70 54 65 72 6d 2d 3e 75 2e 70 4f 72  && (pTerm->u.pOr
2f0a7 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20  Info->indexable 
2f0a8 26 20 6d 61 73 6b 53 72 63 29 21 3d 30 20 0a 20  & maskSrc)!=0 . 
2f0a9 20 20 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72     ){.      Wher
2f0aa 65 43 6c 61 75 73 65 20 2a 20 63 6f 6e 73 74 20  eClause * const 
2f0ab 70 4f 72 57 43 20 3d 20 26 70 54 65 72 6d 2d 3e  pOrWC = &pTerm->
2f0ac 75 2e 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20  u.pOrInfo->wc;. 
2f0ad 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a       WhereTerm *
2f0ae 20 63 6f 6e 73 74 20 70 4f 72 57 43 45 6e 64 20   const pOrWCEnd 
2f0af 3d 20 26 70 4f 72 57 43 2d 3e 61 5b 70 4f 72 57  = &pOrWC->a[pOrW
2f0b0 43 2d 3e 6e 54 65 72 6d 5d 3b 0a 20 20 20 20 20  C->nTerm];.     
2f0b1 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 72 54   WhereTerm *pOrT
2f0b2 65 72 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 66  erm;.      int f
2f0b3 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 4d 55 4c  lags = WHERE_MUL
2f0b4 54 49 5f 4f 52 3b 0a 20 20 20 20 20 20 64 6f 75  TI_OR;.      dou
2f0b5 62 6c 65 20 72 54 6f 74 61 6c 20 3d 20 30 3b 0a  ble rTotal = 0;.
2f0b6 20 20 20 20 20 20 64 6f 75 62 6c 65 20 6e 52 6f        double nRo
2f0b7 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 42 69 74  w = 0;.      Bit
2f0b8 6d 61 73 6b 20 75 73 65 64 20 3d 20 30 3b 0a 0a  mask used = 0;..
2f0b9 20 20 20 20 20 20 66 6f 72 28 70 4f 72 54 65 72        for(pOrTer
2f0ba 6d 3d 70 4f 72 57 43 2d 3e 61 3b 20 70 4f 72 54  m=pOrWC->a; pOrT
2f0bb 65 72 6d 3c 70 4f 72 57 43 45 6e 64 3b 20 70 4f  erm<pOrWCEnd; pO
2f0bc 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  rTerm++){.      
2f0bd 20 20 57 68 65 72 65 43 6f 73 74 20 73 54 65 72    WhereCost sTer
2f0be 6d 43 6f 73 74 3b 0a 20 20 20 20 20 20 20 20 57  mCost;.        W
2f0bf 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
2f0c0 4d 75 6c 74 69 2d 69 6e 64 65 78 20 4f 52 20 74  Multi-index OR t
2f0c1 65 73 74 69 6e 67 20 66 6f 72 20 74 65 72 6d 20  esting for term 
2f0c2 25 64 20 6f 66 20 25 64 2e 2e 2e 2e 5c 6e 22 2c  %d of %d....\n",
2f0c3 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 4f 72   .          (pOr
2f0c4 54 65 72 6d 20 2d 20 70 4f 72 57 43 2d 3e 61 29  Term - pOrWC->a)
2f0c5 2c 20 28 70 54 65 72 6d 20 2d 20 70 57 43 2d 3e  , (pTerm - pWC->
2f0c6 61 29 0a 20 20 20 20 20 20 20 20 29 29 3b 0a 20  a).        ));. 
2f0c7 20 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65         if( pOrTe
2f0c8 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
2f0c9 4f 5f 41 4e 44 20 29 7b 0a 20 20 20 20 20 20 20  O_AND ){.       
2f0ca 20 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a     WhereClause *
2f0cb 70 41 6e 64 57 43 20 3d 20 26 70 4f 72 54 65 72  pAndWC = &pOrTer
2f0cc 6d 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f 2d 3e 77  m->u.pAndInfo->w
2f0cd 63 3b 0a 20 20 20 20 20 20 20 20 20 20 62 65 73  c;.          bes
2f0ce 74 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  tIndex(pParse, p
2f0cf 41 6e 64 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74  AndWC, pSrc, not
2f0d0 52 65 61 64 79 2c 20 30 2c 20 26 73 54 65 72 6d  Ready, 0, &sTerm
2f0d1 43 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  Cost);.        }
2f0d2 65 6c 73 65 20 69 66 28 20 70 4f 72 54 65 72 6d  else if( pOrTerm
2f0d3 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43  ->leftCursor==iC
2f0d4 75 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ur ){.          
2f0d5 57 68 65 72 65 43 6c 61 75 73 65 20 74 65 6d 70  WhereClause temp
2f0d6 57 43 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65  WC;.          te
2f0d7 6d 70 57 43 2e 70 50 61 72 73 65 20 3d 20 70 57  mpWC.pParse = pW
2f0d8 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 20  C->pParse;.     
2f0d9 20 20 20 20 20 74 65 6d 70 57 43 2e 70 4d 61 73       tempWC.pMas
2f0da 6b 53 65 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73  kSet = pWC->pMas
2f0db 6b 53 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20  kSet;.          
2f0dc 74 65 6d 70 57 43 2e 6f 70 20 3d 20 54 4b 5f 41  tempWC.op = TK_A
2f0dd 4e 44 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65  ND;.          te
2f0de 6d 70 57 43 2e 61 20 3d 20 70 4f 72 54 65 72 6d  mpWC.a = pOrTerm
2f0df 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d 70  ;.          temp
2f0e0 57 43 2e 6e 54 65 72 6d 20 3d 20 31 3b 0a 20 20  WC.nTerm = 1;.  
2f0e1 20 20 20 20 20 20 20 20 62 65 73 74 49 6e 64 65          bestInde
2f0e2 78 28 70 50 61 72 73 65 2c 20 26 74 65 6d 70 57  x(pParse, &tempW
2f0e3 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52 65 61 64  C, pSrc, notRead
2f0e4 79 2c 20 30 2c 20 26 73 54 65 72 6d 43 6f 73 74  y, 0, &sTermCost
2f0e5 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
2f0e6 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74  {.          cont
2f0e7 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  inue;.        }.
2f0e8 20 20 20 20 20 20 20 20 72 54 6f 74 61 6c 20 2b          rTotal +
2f0e9 3d 20 73 54 65 72 6d 43 6f 73 74 2e 72 43 6f 73  = sTermCost.rCos
2f0ea 74 3b 0a 20 20 20 20 20 20 20 20 6e 52 6f 77 20  t;.        nRow 
2f0eb 2b 3d 20 73 54 65 72 6d 43 6f 73 74 2e 6e 52 6f  += sTermCost.nRo
2f0ec 77 3b 0a 20 20 20 20 20 20 20 20 75 73 65 64 20  w;.        used 
2f0ed 7c 3d 20 73 54 65 72 6d 43 6f 73 74 2e 75 73 65  |= sTermCost.use
2f0ee 64 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  d;.        if( r
2f0ef 54 6f 74 61 6c 3e 3d 70 43 6f 73 74 2d 3e 72 43  Total>=pCost->rC
2f0f0 6f 73 74 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ost ) break;.   
2f0f1 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49     }..      /* I
2f0f2 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52  f there is an OR
2f0f3 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 69  DER BY clause, i
2f0f4 6e 63 72 65 61 73 65 20 74 68 65 20 73 63 61 6e  ncrease the scan
2f0f5 20 63 6f 73 74 20 74 6f 20 61 63 63 6f 75 6e 74   cost to account
2f0f6 20 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74   .      ** for t
2f0f7 68 65 20 63 6f 73 74 20 6f 66 20 74 68 65 20 73  he cost of the s
2f0f8 6f 72 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  ort. */.      if
2f0f9 28 20 70 4f 72 64 65 72 42 79 21 3d 30 20 29 7b  ( pOrderBy!=0 ){
2f0fa 0a 20 20 20 20 20 20 20 20 72 54 6f 74 61 6c 20  .        rTotal 
2f0fb 2b 3d 20 6e 52 6f 77 2a 65 73 74 4c 6f 67 28 6e  += nRow*estLog(n
2f0fc 52 6f 77 29 3b 0a 20 20 20 20 20 20 20 20 57 48  Row);.        WH
2f0fd 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 73  ERETRACE(("... s
2f0fe 6f 72 74 69 6e 67 20 69 6e 63 72 65 61 73 65 73  orting increases
2f0ff 20 4f 52 20 63 6f 73 74 20 74 6f 20 25 2e 39 67   OR cost to %.9g
2f100 5c 6e 22 2c 20 72 54 6f 74 61 6c 29 29 3b 0a 20  \n", rTotal));. 
2f101 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
2f102 20 49 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20   If the cost of 
2f103 73 63 61 6e 6e 69 6e 67 20 75 73 69 6e 67 20 74  scanning using t
2f104 68 69 73 20 4f 52 20 74 65 72 6d 20 66 6f 72 20  his OR term for 
2f105 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 0a  optimization is.
2f106 20 20 20 20 20 20 2a 2a 20 6c 65 73 73 20 74 68        ** less th
2f107 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  an the current c
2f108 6f 73 74 20 73 74 6f 72 65 64 20 69 6e 20 70 43  ost stored in pC
2f109 6f 73 74 2c 20 72 65 70 6c 61 63 65 20 74 68 65  ost, replace the
2f10a 20 63 6f 6e 74 65 6e 74 73 0a 20 20 20 20 20 20   contents.      
2f10b 2a 2a 20 6f 66 20 70 43 6f 73 74 2e 20 2a 2f 0a  ** of pCost. */.
2f10c 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45        WHERETRACE
2f10d 28 28 22 2e 2e 2e 20 6d 75 6c 74 69 2d 69 6e 64  (("... multi-ind
2f10e 65 78 20 4f 52 20 63 6f 73 74 3d 25 2e 39 67 20  ex OR cost=%.9g 
2f10f 6e 72 6f 77 3d 25 2e 39 67 5c 6e 22 2c 20 72 54  nrow=%.9g\n", rT
2f110 6f 74 61 6c 2c 20 6e 52 6f 77 29 29 3b 0a 20 20  otal, nRow));.  
2f111 20 20 20 20 69 66 28 20 72 54 6f 74 61 6c 3c 70      if( rTotal<p
2f112 43 6f 73 74 2d 3e 72 43 6f 73 74 20 29 7b 0a 20  Cost->rCost ){. 
2f113 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 72 43         pCost->rC
2f114 6f 73 74 20 3d 20 72 54 6f 74 61 6c 3b 0a 20 20  ost = rTotal;.  
2f115 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 6e 52 6f        pCost->nRo
2f116 77 20 3d 20 6e 52 6f 77 3b 0a 20 20 20 20 20 20  w = nRow;.      
2f117 20 20 70 43 6f 73 74 2d 3e 75 73 65 64 20 3d 20    pCost->used = 
2f118 75 73 65 64 3b 0a 20 20 20 20 20 20 20 20 70 43  used;.        pC
2f119 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  ost->plan.wsFlag
2f11a 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20 20  s = flags;.     
2f11b 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75     pCost->plan.u
2f11c 2e 70 54 65 72 6d 20 3d 20 70 54 65 72 6d 3b 0a  .pTerm = pTerm;.
2f11d 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2f11e 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2f11f 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
2f120 49 5a 41 54 49 4f 4e 20 2a 2f 0a 7d 0a 0a 23 69  IZATION */.}..#i
2f121 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2f122 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
2f123 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e  *.** Allocate an
2f124 64 20 70 6f 70 75 6c 61 74 65 20 61 6e 20 73 71  d populate an sq
2f125 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2f126 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 69   structure. It i
2f127 73 20 74 68 65 20 0a 2a 2a 20 72 65 73 70 6f 6e  s the .** respon
2f128 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20  sibility of the 
2f129 63 61 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75  caller to eventu
2f12a 61 6c 6c 79 20 72 65 6c 65 61 73 65 20 74 68 65  ally release the
2f12b 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 62 79   structure.** by
2f12c 20 70 61 73 73 69 6e 67 20 74 68 65 20 70 6f 69   passing the poi
2f12d 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 62 79  nter returned by
2f12e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74   this function t
2f12f 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  o sqlite3_free()
2f130 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69  ..*/.static sqli
2f131 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
2f132 61 6c 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66  allocateIndexInf
2f133 6f 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  o(.  Parse *pPar
2f134 73 65 2c 20 0a 20 20 57 68 65 72 65 43 6c 61 75  se, .  WhereClau
2f135 73 65 20 2a 70 57 43 2c 0a 20 20 73 74 72 75 63  se *pWC,.  struc
2f136 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2f137 70 53 72 63 2c 0a 20 20 45 78 70 72 4c 69 73 74  pSrc,.  ExprList
2f138 20 2a 70 4f 72 64 65 72 42 79 0a 29 7b 0a 20 20   *pOrderBy.){.  
2f139 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 6e 74 20  int i, j;.  int 
2f13a 6e 54 65 72 6d 3b 0a 20 20 73 74 72 75 63 74 20  nTerm;.  struct 
2f13b 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
2f13c 6e 73 74 72 61 69 6e 74 20 2a 70 49 64 78 43 6f  nstraint *pIdxCo
2f13d 6e 73 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c  ns;.  struct sql
2f13e 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72  ite3_index_order
2f13f 62 79 20 2a 70 49 64 78 4f 72 64 65 72 42 79 3b  by *pIdxOrderBy;
2f140 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
2f141 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2f142 6e 74 5f 75 73 61 67 65 20 2a 70 55 73 61 67 65  nt_usage *pUsage
2f143 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  ;.  WhereTerm *p
2f144 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e 4f 72 64  Term;.  int nOrd
2f145 65 72 42 79 3b 0a 20 20 73 71 6c 69 74 65 33 5f  erBy;.  sqlite3_
2f146 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78  index_info *pIdx
2f147 49 6e 66 6f 3b 0a 0a 20 20 57 48 45 52 45 54 52  Info;..  WHERETR
2f148 41 43 45 28 28 22 52 65 63 6f 6d 70 75 74 69 6e  ACE(("Recomputin
2f149 67 20 69 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72  g index info for
2f14a 20 25 73 2e 2e 2e 5c 6e 22 2c 20 70 53 72 63 2d   %s...\n", pSrc-
2f14b 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a  >pTab->zName));.
2f14c 0a 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20  .  /* Count the 
2f14d 6e 75 6d 62 65 72 20 6f 66 20 70 6f 73 73 69 62  number of possib
2f14e 6c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  le WHERE clause 
2f14f 63 6f 6e 73 74 72 61 69 6e 74 73 20 72 65 66 65  constraints refe
2f150 72 72 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 74 68  rring.  ** to th
2f151 69 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  is virtual table
2f152 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6e 54 65 72   */.  for(i=nTer
2f153 6d 3d 30 2c 20 70 54 65 72 6d 3d 70 57 43 2d 3e  m=0, pTerm=pWC->
2f154 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b  a; i<pWC->nTerm;
2f155 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a   i++, pTerm++){.
2f156 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c      if( pTerm->l
2f157 65 66 74 43 75 72 73 6f 72 20 21 3d 20 70 53 72  eftCursor != pSr
2f158 63 2d 3e 69 43 75 72 73 6f 72 20 29 20 63 6f 6e  c->iCursor ) con
2f159 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 73 65 72  tinue;.    asser
2f15a 74 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72  t( (pTerm->eOper
2f15b 61 74 6f 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70  ator&(pTerm->eOp
2f15c 65 72 61 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b  erator-1))==0 );
2f15d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
2f15e 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
2f15f 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 74 65  =WO_IN );.    te
2f160 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65  stcase( pTerm->e
2f161 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e  Operator==WO_ISN
2f162 55 4c 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ULL );.    if( p
2f163 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2f164 26 20 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55  & (WO_IN|WO_ISNU
2f165 4c 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  LL) ) continue;.
2f166 20 20 20 20 6e 54 65 72 6d 2b 2b 3b 0a 20 20 7d      nTerm++;.  }
2f167 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 4f 52  ..  /* If the OR
2f168 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 63 6f  DER BY clause co
2f169 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 63 6f 6c 75  ntains only colu
2f16a 6d 6e 73 20 69 6e 20 74 68 65 20 63 75 72 72 65  mns in the curre
2f16b 6e 74 20 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c  nt .  ** virtual
2f16c 20 74 61 62 6c 65 20 74 68 65 6e 20 61 6c 6c 6f   table then allo
2f16d 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 74  cate space for t
2f16e 68 65 20 61 4f 72 64 65 72 42 79 20 70 61 72 74  he aOrderBy part
2f16f 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c   of.  ** the sql
2f170 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2f171 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2f 0a  structure..  */.
2f172 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a    nOrderBy = 0;.
2f173 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29    if( pOrderBy )
2f174 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
2f175 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  <pOrderBy->nExpr
2f176 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78  ; i++){.      Ex
2f177 70 72 20 2a 70 45 78 70 72 20 3d 20 70 4f 72 64  pr *pExpr = pOrd
2f178 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  erBy->a[i].pExpr
2f179 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  ;.      if( pExp
2f17a 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e  r->op!=TK_COLUMN
2f17b 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c   || pExpr->iTabl
2f17c 65 21 3d 70 53 72 63 2d 3e 69 43 75 72 73 6f 72  e!=pSrc->iCursor
2f17d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
2f17e 20 20 20 20 69 66 28 20 69 3d 3d 70 4f 72 64 65      if( i==pOrde
2f17f 72 42 79 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20  rBy->nExpr ){.  
2f180 20 20 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 70      nOrderBy = p
2f181 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a  OrderBy->nExpr;.
2f182 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2f183 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 71 6c  Allocate the sql
2f184 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2f185 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2f 0a 20  structure.  */. 
2f186 20 70 49 64 78 49 6e 66 6f 20 3d 20 73 71 6c 69   pIdxInfo = sqli
2f187 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2f188 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65  pParse->db, size
2f189 6f 66 28 2a 70 49 64 78 49 6e 66 6f 29 0a 20 20  of(*pIdxInfo).  
2f18a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f18b 20 20 20 20 20 20 20 20 20 2b 20 28 73 69 7a 65           + (size
2f18c 6f 66 28 2a 70 49 64 78 43 6f 6e 73 29 20 2b 20  of(*pIdxCons) + 
2f18d 73 69 7a 65 6f 66 28 2a 70 55 73 61 67 65 29 29  sizeof(*pUsage))
2f18e 2a 6e 54 65 72 6d 0a 20 20 20 20 20 20 20 20 20  *nTerm.         
2f18f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f190 20 20 2b 20 73 69 7a 65 6f 66 28 2a 70 49 64 78    + sizeof(*pIdx
2f191 4f 72 64 65 72 42 79 29 2a 6e 4f 72 64 65 72 42  OrderBy)*nOrderB
2f192 79 20 29 3b 0a 20 20 69 66 28 20 70 49 64 78 49  y );.  if( pIdxI
2f193 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  nfo==0 ){.    sq
2f194 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2f195 61 72 73 65 2c 20 22 6f 75 74 20 6f 66 20 6d 65  arse, "out of me
2f196 6d 6f 72 79 22 29 3b 0a 20 20 20 20 2f 2a 20 28  mory");.    /* (
2f197 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65  double)0 In case
2f198 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   of SQLITE_OMIT_
2f199 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e  FLOATING_POINT..
2f19a 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
2f19b 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69  0;.  }..  /* Ini
2f19c 74 69 61 6c 69 7a 65 20 74 68 65 20 73 74 72 75  tialize the stru
2f19d 63 74 75 72 65 2e 20 20 54 68 65 20 73 71 6c 69  cture.  The sqli
2f19e 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
2f19f 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
2f1a0 73 0a 20 20 2a 2a 20 6d 61 6e 79 20 66 69 65 6c  s.  ** many fiel
2f1a1 64 73 20 74 68 61 74 20 61 72 65 20 64 65 63 6c  ds that are decl
2f1a2 61 72 65 64 20 22 63 6f 6e 73 74 22 20 74 6f 20  ared "const" to 
2f1a3 70 72 65 76 65 6e 74 20 78 42 65 73 74 49 6e 64  prevent xBestInd
2f1a4 65 78 20 66 72 6f 6d 0a 20 20 2a 2a 20 63 68 61  ex from.  ** cha
2f1a5 6e 67 69 6e 67 20 74 68 65 6d 2e 20 20 57 65 20  nging them.  We 
2f1a6 68 61 76 65 20 74 6f 20 64 6f 20 73 6f 6d 65 20  have to do some 
2f1a7 66 75 6e 6b 79 20 63 61 73 74 69 6e 67 20 69 6e  funky casting in
2f1a8 20 6f 72 64 65 72 20 74 6f 0a 20 20 2a 2a 20 69   order to.  ** i
2f1a9 6e 69 74 69 61 6c 69 7a 65 20 74 68 6f 73 65 20  nitialize those 
2f1aa 66 69 65 6c 64 73 2e 0a 20 20 2a 2f 0a 20 20 70  fields..  */.  p
2f1ab 49 64 78 43 6f 6e 73 20 3d 20 28 73 74 72 75 63  IdxCons = (struc
2f1ac 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2f1ad 63 6f 6e 73 74 72 61 69 6e 74 2a 29 26 70 49 64  constraint*)&pId
2f1ae 78 49 6e 66 6f 5b 31 5d 3b 0a 20 20 70 49 64 78  xInfo[1];.  pIdx
2f1af 4f 72 64 65 72 42 79 20 3d 20 28 73 74 72 75 63  OrderBy = (struc
2f1b0 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2f1b1 6f 72 64 65 72 62 79 2a 29 26 70 49 64 78 43 6f  orderby*)&pIdxCo
2f1b2 6e 73 5b 6e 54 65 72 6d 5d 3b 0a 20 20 70 55 73  ns[nTerm];.  pUs
2f1b3 61 67 65 20 3d 20 28 73 74 72 75 63 74 20 73 71  age = (struct sq
2f1b4 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
2f1b5 74 72 61 69 6e 74 5f 75 73 61 67 65 2a 29 26 70  traint_usage*)&p
2f1b6 49 64 78 4f 72 64 65 72 42 79 5b 6e 4f 72 64 65  IdxOrderBy[nOrde
2f1b7 72 42 79 5d 3b 0a 20 20 2a 28 69 6e 74 2a 29 26  rBy];.  *(int*)&
2f1b8 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74  pIdxInfo->nConst
2f1b9 72 61 69 6e 74 20 3d 20 6e 54 65 72 6d 3b 0a 20  raint = nTerm;. 
2f1ba 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e 66   *(int*)&pIdxInf
2f1bb 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20 6e 4f  o->nOrderBy = nO
2f1bc 72 64 65 72 42 79 3b 0a 20 20 2a 28 73 74 72 75  rderBy;.  *(stru
2f1bd 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
2f1be 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70  _constraint**)&p
2f1bf 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
2f1c0 61 69 6e 74 20 3d 20 70 49 64 78 43 6f 6e 73 3b  aint = pIdxCons;
2f1c1 0a 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69  .  *(struct sqli
2f1c2 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62  te3_index_orderb
2f1c3 79 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61  y**)&pIdxInfo->a
2f1c4 4f 72 64 65 72 42 79 20 3d 20 70 49 64 78 4f 72  OrderBy = pIdxOr
2f1c5 64 65 72 42 79 3b 0a 20 20 2a 28 73 74 72 75 63  derBy;.  *(struc
2f1c6 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2f1c7 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
2f1c8 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43  **)&pIdxInfo->aC
2f1c9 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 20 3d  onstraintUsage =
2f1ca 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f1cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1ce 20 20 20 20 70 55 73 61 67 65 3b 0a 0a 20 20 66      pUsage;..  f
2f1cf 6f 72 28 69 3d 6a 3d 30 2c 20 70 54 65 72 6d 3d  or(i=j=0, pTerm=
2f1d0 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e  pWC->a; i<pWC->n
2f1d1 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d  Term; i++, pTerm
2f1d2 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65  ++){.    if( pTe
2f1d3 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 21  rm->leftCursor !
2f1d4 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20  = pSrc->iCursor 
2f1d5 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2f1d6 61 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e  assert( (pTerm->
2f1d7 65 4f 70 65 72 61 74 6f 72 26 28 70 54 65 72 6d  eOperator&(pTerm
2f1d8 2d 3e 65 4f 70 65 72 61 74 6f 72 2d 31 29 29 3d  ->eOperator-1))=
2f1d9 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  =0 );.    testca
2f1da 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  se( pTerm->eOper
2f1db 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20  ator==WO_IN );. 
2f1dc 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
2f1dd 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
2f1de 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20  O_ISNULL );.    
2f1df 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
2f1e0 61 74 6f 72 20 26 20 28 57 4f 5f 49 4e 7c 57 4f  ator & (WO_IN|WO
2f1e1 5f 49 53 4e 55 4c 4c 29 20 29 20 63 6f 6e 74 69  _ISNULL) ) conti
2f1e2 6e 75 65 3b 0a 20 20 20 20 70 49 64 78 43 6f 6e  nue;.    pIdxCon
2f1e3 73 5b 6a 5d 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70  s[j].iColumn = p
2f1e4 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75  Term->u.leftColu
2f1e5 6d 6e 3b 0a 20 20 20 20 70 49 64 78 43 6f 6e 73  mn;.    pIdxCons
2f1e6 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 20  [j].iTermOffset 
2f1e7 3d 20 69 3b 0a 20 20 20 20 70 49 64 78 43 6f 6e  = i;.    pIdxCon
2f1e8 73 5b 6a 5d 2e 6f 70 20 3d 20 28 75 38 29 70 54  s[j].op = (u8)pT
2f1e9 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3b 0a  erm->eOperator;.
2f1ea 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65 63      /* The direc
2f1eb 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 69 6e 20  t assignment in 
2f1ec 74 68 65 20 70 72 65 76 69 6f 75 73 20 6c 69 6e  the previous lin
2f1ed 65 20 69 73 20 70 6f 73 73 69 62 6c 65 20 6f 6e  e is possible on
2f1ee 6c 79 20 62 65 63 61 75 73 65 0a 20 20 20 20 2a  ly because.    *
2f1ef 2a 20 74 68 65 20 57 4f 5f 20 61 6e 64 20 53 51  * the WO_ and SQ
2f1f0 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
2f1f1 52 41 49 4e 54 5f 20 63 6f 64 65 73 20 61 72 65  RAINT_ codes are
2f1f2 20 69 64 65 6e 74 69 63 61 6c 2e 20 20 54 68 65   identical.  The
2f1f3 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  .    ** followin
2f1f4 67 20 61 73 73 65 72 74 73 20 76 65 72 69 66 79  g asserts verify
2f1f5 20 74 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20   this fact. */. 
2f1f6 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 45 51     assert( WO_EQ
2f1f7 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
2f1f8 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 29 3b 0a  ONSTRAINT_EQ );.
2f1f9 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 4c      assert( WO_L
2f1fa 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  T==SQLITE_INDEX_
2f1fb 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 29 3b  CONSTRAINT_LT );
2f1fc 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f  .    assert( WO_
2f1fd 4c 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58  LE==SQLITE_INDEX
2f1fe 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20 29  _CONSTRAINT_LE )
2f1ff 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f  ;.    assert( WO
2f200 5f 47 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  _GT==SQLITE_INDE
2f201 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20  X_CONSTRAINT_GT 
2f202 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 57  );.    assert( W
2f203 4f 5f 47 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  O_GE==SQLITE_IND
2f204 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45  EX_CONSTRAINT_GE
2f205 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2f206 57 4f 5f 4d 41 54 43 48 3d 3d 53 51 4c 49 54 45  WO_MATCH==SQLITE
2f207 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
2f208 54 5f 4d 41 54 43 48 20 29 3b 0a 20 20 20 20 61  T_MATCH );.    a
2f209 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 65 4f  ssert( pTerm->eO
2f20a 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 45 51  perator & (WO_EQ
2f20b 7c 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f  |WO_LT|WO_LE|WO_
2f20c 47 54 7c 57 4f 5f 47 45 7c 57 4f 5f 4d 41 54 43  GT|WO_GE|WO_MATC
2f20d 48 29 20 29 3b 0a 20 20 20 20 6a 2b 2b 3b 0a 20  H) );.    j++;. 
2f20e 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
2f20f 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a  nOrderBy; i++){.
2f210 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20      Expr *pExpr 
2f211 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d  = pOrderBy->a[i]
2f212 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 49 64 78  .pExpr;.    pIdx
2f213 4f 72 64 65 72 42 79 5b 69 5d 2e 69 43 6f 6c 75  OrderBy[i].iColu
2f214 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c  mn = pExpr->iCol
2f215 75 6d 6e 3b 0a 20 20 20 20 70 49 64 78 4f 72 64  umn;.    pIdxOrd
2f216 65 72 42 79 5b 69 5d 2e 64 65 73 63 20 3d 20 70  erBy[i].desc = p
2f217 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f  OrderBy->a[i].so
2f218 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 0a 20 20  rtOrder;.  }..  
2f219 72 65 74 75 72 6e 20 70 49 64 78 49 6e 66 6f 3b  return pIdxInfo;
2f21a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 61  .}../*.** The ta
2f21b 62 6c 65 20 6f 62 6a 65 63 74 20 72 65 66 65 72  ble object refer
2f21c 65 6e 63 65 20 70 61 73 73 65 64 20 61 73 20 74  ence passed as t
2f21d 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
2f21e 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
2f21f 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 72 65 70 72  ion.** must repr
2f220 65 73 65 6e 74 20 61 20 76 69 72 74 75 61 6c 20  esent a virtual 
2f221 74 61 62 6c 65 2e 20 54 68 69 73 20 66 75 6e 63  table. This func
2f222 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 74 68 65  tion invokes the
2f223 20 78 42 65 73 74 49 6e 64 65 78 28 29 0a 2a 2a   xBestIndex().**
2f224 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76   method of the v
2f225 69 72 74 75 61 6c 20 74 61 62 6c 65 20 77 69 74  irtual table wit
2f226 68 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  h the sqlite3_in
2f227 64 65 78 5f 69 6e 66 6f 20 70 6f 69 6e 74 65 72  dex_info pointer
2f228 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68   passed.** as th
2f229 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  e argument..**.*
2f22a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
2f22b 63 75 72 73 2c 20 70 50 61 72 73 65 20 69 73 20  curs, pParse is 
2f22c 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 61  populated with a
2f22d 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
2f22e 61 6e 64 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72  and a.** non-zer
2f22f 6f 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72  o value is retur
2f230 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ned. Otherwise, 
2f231 30 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  0 is returned an
2f232 64 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20  d the output.** 
2f233 70 61 72 74 20 6f 66 20 74 68 65 20 73 71 6c 69  part of the sqli
2f234 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
2f235 74 72 75 63 74 75 72 65 20 69 73 20 6c 65 66 74  tructure is left
2f236 20 70 6f 70 75 6c 61 74 65 64 2e 0a 2a 2a 0a 2a   populated..**.*
2f237 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
2f238 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
2f239 75 72 6e 65 64 2c 20 69 74 20 69 73 20 74 68 65  urned, it is the
2f23a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
2f23b 6f 66 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72  of the.** caller
2f23c 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 66   to eventually f
2f23d 72 65 65 20 70 2d 3e 69 64 78 53 74 72 20 69 66  ree p->idxStr if
2f23e 20 70 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64   p->needToFreeId
2f23f 78 53 74 72 20 69 6e 64 69 63 61 74 65 73 0a 2a  xStr indicates.*
2f240 2a 20 74 68 61 74 20 74 68 69 73 20 69 73 20 72  * that this is r
2f241 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74  equired..*/.stat
2f242 69 63 20 69 6e 74 20 76 74 61 62 42 65 73 74 49  ic int vtabBestI
2f243 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72  ndex(Parse *pPar
2f244 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  se, Table *pTab,
2f245 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2f246 6e 66 6f 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  nfo *p){.  sqlit
2f247 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d  e3_vtab *pVtab =
2f248 20 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c   sqlite3GetVTabl
2f249 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  e(pParse->db, pT
2f24a 61 62 29 2d 3e 70 56 74 61 62 3b 0a 20 20 69 6e  ab)->pVtab;.  in
2f24b 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  t i;.  int rc;..
2f24c 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
2f24d 61 66 65 74 79 4f 66 66 28 70 50 61 72 73 65 2d  afetyOff(pParse-
2f24e 3e 64 62 29 3b 0a 20 20 57 48 45 52 45 54 52 41  >db);.  WHERETRA
2f24f 43 45 28 28 22 78 42 65 73 74 49 6e 64 65 78 20  CE(("xBestIndex 
2f250 66 6f 72 20 25 73 5c 6e 22 2c 20 70 54 61 62 2d  for %s\n", pTab-
2f251 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 54 52 41 43  >zName));.  TRAC
2f252 45 5f 49 44 58 5f 49 4e 50 55 54 53 28 70 29 3b  E_IDX_INPUTS(p);
2f253 0a 20 20 72 63 20 3d 20 70 56 74 61 62 2d 3e 70  .  rc = pVtab->p
2f254 4d 6f 64 75 6c 65 2d 3e 78 42 65 73 74 49 6e 64  Module->xBestInd
2f255 65 78 28 70 56 74 61 62 2c 20 70 29 3b 0a 20 20  ex(pVtab, p);.  
2f256 54 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54  TRACE_IDX_OUTPUT
2f257 53 28 70 29 3b 0a 20 20 28 76 6f 69 64 29 73 71  S(p);.  (void)sq
2f258 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 50  lite3SafetyOn(pP
2f259 61 72 73 65 2d 3e 64 62 29 3b 0a 0a 20 20 69 66  arse->db);..  if
2f25a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2f25b 29 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ){.    if( rc==S
2f25c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
2f25d 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d       pParse->db-
2f25e 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2f25f 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  1;.    }else if(
2f260 20 21 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67   !pVtab->zErrMsg
2f261 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2f262 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2f263 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45  , "%s", sqlite3E
2f264 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 20 20  rrStr(rc));.    
2f265 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
2f266 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2f267 72 73 65 2c 20 22 25 73 22 2c 20 70 56 74 61 62  rse, "%s", pVtab
2f268 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
2f269 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  }.  }.  sqlite3D
2f26a 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62  bFree(pParse->db
2f26b 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  , pVtab->zErrMsg
2f26c 29 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72  );.  pVtab->zErr
2f26d 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 66 6f 72 28  Msg = 0;..  for(
2f26e 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74  i=0; i<p->nConst
2f26f 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20  raint; i++){.   
2f270 20 69 66 28 20 21 70 2d 3e 61 43 6f 6e 73 74 72   if( !p->aConstr
2f271 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c 65 20 26  aint[i].usable &
2f272 26 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  & p->aConstraint
2f273 55 73 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64  Usage[i].argvInd
2f274 65 78 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71  ex>0 ){.      sq
2f275 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2f276 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20  arse, .         
2f277 20 22 74 61 62 6c 65 20 25 73 3a 20 78 42 65 73   "table %s: xBes
2f278 74 49 6e 64 65 78 20 72 65 74 75 72 6e 65 64 20  tIndex returned 
2f279 61 6e 20 69 6e 76 61 6c 69 64 20 70 6c 61 6e 22  an invalid plan"
2f27a 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2f27b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
2f27c 75 72 6e 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  urn pParse->nErr
2f27d 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  ;.}.../*.** Comp
2f27e 75 74 65 20 74 68 65 20 62 65 73 74 20 69 6e 64  ute the best ind
2f27f 65 78 20 66 6f 72 20 61 20 76 69 72 74 75 61 6c  ex for a virtual
2f280 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   table..**.** Th
2f281 65 20 62 65 73 74 20 69 6e 64 65 78 20 69 73 20  e best index is 
2f282 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20  computed by the 
2f283 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f  xBestIndex metho
2f284 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  d of the virtual
2f285 0a 2a 2a 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  .** table module
2f286 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
2f287 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61  is really just a
2f288 20 77 72 61 70 70 65 72 20 74 68 61 74 20 73 65   wrapper that se
2f289 74 73 20 75 70 0a 2a 2a 20 74 68 65 20 73 71 6c  ts up.** the sql
2f28a 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2f28b 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 69  structure that i
2f28c 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 6d 75 6e  s used to commun
2f28d 69 63 61 74 65 20 77 69 74 68 0a 2a 2a 20 78 42  icate with.** xB
2f28e 65 73 74 49 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  estIndex..**.** 
2f28f 49 6e 20 61 20 6a 6f 69 6e 2c 20 74 68 69 73 20  In a join, this 
2f290 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 62 65  routine might be
2f291 20 63 61 6c 6c 65 64 20 6d 75 6c 74 69 70 6c 65   called multiple
2f292 20 74 69 6d 65 73 20 66 6f 72 20 74 68 65 0a 2a   times for the.*
2f293 2a 20 73 61 6d 65 20 76 69 72 74 75 61 6c 20 74  * same virtual t
2f294 61 62 6c 65 2e 20 20 54 68 65 20 73 71 6c 69 74  able.  The sqlit
2f295 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74  e3_index_info st
2f296 72 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74  ructure is creat
2f297 65 64 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61  ed.** and initia
2f298 6c 69 7a 65 64 20 6f 6e 20 74 68 65 20 66 69 72  lized on the fir
2f299 73 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 61 6e  st invocation an
2f29a 64 20 72 65 75 73 65 64 20 6f 6e 20 61 6c 6c 20  d reused on all 
2f29b 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e  subsequent.** in
2f29c 76 6f 63 61 74 69 6f 6e 73 2e 20 20 54 68 65 20  vocations.  The 
2f29d 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2f29e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
2f29f 61 6c 73 6f 20 75 73 65 64 20 77 68 65 6e 0a 2a  also used when.*
2f2a0 2a 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61  * code is genera
2f2a1 74 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  ted to access th
2f2a2 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
2f2a3 20 20 54 68 65 20 77 68 65 72 65 49 6e 66 6f 44    The whereInfoD
2f2a4 65 6c 65 74 65 28 29 20 0a 2a 2a 20 72 6f 75 74  elete() .** rout
2f2a5 69 6e 65 20 74 61 6b 65 73 20 63 61 72 65 20 6f  ine takes care o
2f2a6 66 20 66 72 65 65 69 6e 67 20 74 68 65 20 73 71  f freeing the sq
2f2a7 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2f2a8 20 73 74 72 75 63 74 75 72 65 20 61 66 74 65 72   structure after
2f2a9 0a 2a 2a 20 65 76 65 72 79 62 6f 64 79 20 68 61  .** everybody ha
2f2aa 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20  s finished with 
2f2ab 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  it..*/.static vo
2f2ac 69 64 20 62 65 73 74 56 69 72 74 75 61 6c 49 6e  id bestVirtualIn
2f2ad 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50  dex(.  Parse *pP
2f2ae 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
2f2af 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
2f2b0 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2f2b1 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2f2b2 70 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 20  pWC,            
2f2b3 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
2f2b4 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75  clause */.  stru
2f2b5 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2f2b6 2a 70 53 72 63 2c 20 20 20 20 20 20 2f 2a 20 54  *pSrc,      /* T
2f2b7 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74  he FROM clause t
2f2b8 65 72 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f  erm to search */
2f2b9 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65  .  Bitmask notRe
2f2ba 61 64 79 2c 20 20 20 20 20 20 20 20 20 20 20 20  ady,            
2f2bb 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75     /* Mask of cu
2f2bc 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e  rsors that are n
2f2bd 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a  ot available */.
2f2be 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
2f2bf 65 72 42 79 2c 20 20 20 20 20 20 20 20 20 20 20  erBy,           
2f2c0 20 20 2f 2a 20 54 68 65 20 6f 72 64 65 72 20 62    /* The order b
2f2c1 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68  y clause */.  Wh
2f2c2 65 72 65 43 6f 73 74 20 2a 70 43 6f 73 74 2c 20  ereCost *pCost, 
2f2c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2f2c4 20 4c 6f 77 65 73 74 20 63 6f 73 74 20 71 75 65   Lowest cost que
2f2c5 72 79 20 70 6c 61 6e 20 2a 2f 0a 20 20 73 71 6c  ry plan */.  sql
2f2c6 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2f2c7 2a 2a 70 70 49 64 78 49 6e 66 6f 20 20 2f 2a 20  **ppIdxInfo  /* 
2f2c8 49 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f  Index informatio
2f2c9 6e 20 70 61 73 73 65 64 20 74 6f 20 78 42 65 73  n passed to xBes
2f2ca 74 49 6e 64 65 78 20 2a 2f 0a 29 7b 0a 20 20 54  tIndex */.){.  T
2f2cb 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 53 72  able *pTab = pSr
2f2cc 63 2d 3e 70 54 61 62 3b 0a 20 20 73 71 6c 69 74  c->pTab;.  sqlit
2f2cd 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
2f2ce 49 64 78 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63  IdxInfo;.  struc
2f2cf 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2f2d0 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 49 64 78  constraint *pIdx
2f2d1 43 6f 6e 73 3b 0a 20 20 73 74 72 75 63 74 20 73  Cons;.  struct s
2f2d2 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
2f2d3 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 70  straint_usage *p
2f2d4 55 73 61 67 65 3b 0a 20 20 57 68 65 72 65 54 65  Usage;.  WhereTe
2f2d5 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74  rm *pTerm;.  int
2f2d6 20 69 2c 20 6a 3b 0a 20 20 69 6e 74 20 6e 4f 72   i, j;.  int nOr
2f2d7 64 65 72 42 79 3b 0a 0a 20 20 2f 2a 20 4d 61 6b  derBy;..  /* Mak
2f2d8 65 20 73 75 72 65 20 77 73 46 6c 61 67 73 20 69  e sure wsFlags i
2f2d9 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  s initialized to
2f2da 20 73 6f 6d 65 20 73 61 6e 65 20 76 61 6c 75 65   some sane value
2f2db 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
2f2dc 74 68 65 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63  the .  ** malloc
2f2dd 20 69 6e 20 61 6c 6c 6f 63 61 74 65 49 6e 64 65   in allocateInde
2f2de 78 49 6e 66 6f 28 29 20 66 61 69 6c 73 20 61 6e  xInfo() fails an
2f2df 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  d this function 
2f2e0 72 65 74 75 72 6e 73 20 6c 65 61 76 69 6e 67 0a  returns leaving.
2f2e1 20 20 2a 2a 20 77 73 46 6c 61 67 73 20 69 6e 20    ** wsFlags in 
2f2e2 61 6e 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  an uninitialized
2f2e3 20 73 74 61 74 65 2c 20 74 68 65 20 63 61 6c 6c   state, the call
2f2e4 65 72 20 6d 61 79 20 62 65 68 61 76 65 20 75 6e  er may behave un
2f2e5 70 72 65 64 69 63 74 61 62 6c 79 2e 0a 20 20 2a  predictably..  *
2f2e6 2f 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 73 74  /.  memset(pCost
2f2e7 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 6f  , 0, sizeof(*pCo
2f2e8 73 74 29 29 3b 0a 20 20 70 43 6f 73 74 2d 3e 70  st));.  pCost->p
2f2e9 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 57 48  lan.wsFlags = WH
2f2ea 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  ERE_VIRTUALTABLE
2f2eb 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73  ;..  /* If the s
2f2ec 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2f2ed 6f 20 73 74 72 75 63 74 75 72 65 20 68 61 73 20  o structure has 
2f2ee 6e 6f 74 20 62 65 65 6e 20 70 72 65 76 69 6f 75  not been previou
2f2ef 73 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  sly.  ** allocat
2f2f0 65 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  ed and initializ
2f2f1 65 64 2c 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74  ed, then allocat
2f2f2 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  e and initialize
2f2f3 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20   it now..  */.  
2f2f4 70 49 64 78 49 6e 66 6f 20 3d 20 2a 70 70 49 64  pIdxInfo = *ppId
2f2f5 78 49 6e 66 6f 3b 0a 20 20 69 66 28 20 70 49 64  xInfo;.  if( pId
2f2f6 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20  xInfo==0 ){.    
2f2f7 2a 70 70 49 64 78 49 6e 66 6f 20 3d 20 70 49 64  *ppIdxInfo = pId
2f2f8 78 49 6e 66 6f 20 3d 20 61 6c 6c 6f 63 61 74 65  xInfo = allocate
2f2f9 49 6e 64 65 78 49 6e 66 6f 28 70 50 61 72 73 65  IndexInfo(pParse
2f2fa 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 70 4f 72  , pWC, pSrc, pOr
2f2fb 64 65 72 42 79 29 3b 0a 20 20 7d 0a 20 20 69 66  derBy);.  }.  if
2f2fc 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b  ( pIdxInfo==0 ){
2f2fd 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
2f2fe 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70  ..  /* At this p
2f2ff 6f 69 6e 74 2c 20 74 68 65 20 73 71 6c 69 74 65  oint, the sqlite
2f300 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
2f301 75 63 74 75 72 65 20 74 68 61 74 20 70 49 64 78  ucture that pIdx
2f302 49 6e 66 6f 20 70 6f 69 6e 74 73 0a 20 20 2a 2a  Info points.  **
2f303 20 74 6f 20 77 69 6c 6c 20 68 61 76 65 20 62 65   to will have be
2f304 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20  en initialized, 
2f305 65 69 74 68 65 72 20 64 75 72 69 6e 67 20 74 68  either during th
2f306 65 20 63 75 72 72 65 6e 74 20 69 6e 76 6f 63 61  e current invoca
2f307 74 69 6f 6e 20 6f 72 0a 20 20 2a 2a 20 64 75 72  tion or.  ** dur
2f308 69 6e 67 20 73 6f 6d 65 20 70 72 69 6f 72 20 69  ing some prior i
2f309 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f 77 20  nvocation.  Now 
2f30a 77 65 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20  we just have to 
2f30b 63 75 73 74 6f 6d 69 7a 65 20 74 68 65 0a 20 20  customize the.  
2f30c 2a 2a 20 64 65 74 61 69 6c 73 20 6f 66 20 70 49  ** details of pI
2f30d 64 78 49 6e 66 6f 20 66 6f 72 20 74 68 65 20 63  dxInfo for the c
2f30e 75 72 72 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f  urrent invocatio
2f30f 6e 20 61 6e 64 20 70 61 73 73 20 69 74 20 74 6f  n and pass it to
2f310 0a 20 20 2a 2a 20 78 42 65 73 74 49 6e 64 65 78  .  ** xBestIndex
2f311 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  ..  */..  /* The
2f312 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 6d 75 73   module name mus
2f313 74 20 62 65 20 64 65 66 69 6e 65 64 2e 20 41 6c  t be defined. Al
2f314 73 6f 2c 20 62 79 20 74 68 69 73 20 70 6f 69 6e  so, by this poin
2f315 74 20 74 68 65 72 65 20 6d 75 73 74 0a 20 20 2a  t there must.  *
2f316 2a 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74  * be a pointer t
2f317 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61  o an sqlite3_vta
2f318 62 20 73 74 72 75 63 74 75 72 65 2e 20 4f 74 68  b structure. Oth
2f319 65 72 77 69 73 65 0a 20 20 2a 2a 20 73 71 6c 69  erwise.  ** sqli
2f31a 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
2f31b 4e 61 6d 65 73 28 29 20 77 6f 75 6c 64 20 68 61  Names() would ha
2f31c 76 65 20 70 69 63 6b 65 64 20 75 70 20 74 68 65  ve picked up the
2f31d 20 65 72 72 6f 72 2e 20 0a 20 20 2a 2f 0a 20 20   error. .  */.  
2f31e 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 61 7a  assert( pTab->az
2f31f 4d 6f 64 75 6c 65 41 72 67 20 26 26 20 70 54 61  ModuleArg && pTa
2f320 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30  b->azModuleArg[0
2f321 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ] );.  assert( s
2f322 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
2f323 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
2f324 29 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74  ) );..  /* Set t
2f325 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  he aConstraint[]
2f326 2e 75 73 61 62 6c 65 20 66 69 65 6c 64 73 20 61  .usable fields a
2f327 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6c  nd initialize al
2f328 6c 20 0a 20 20 2a 2a 20 6f 75 74 70 75 74 20 76  l .  ** output v
2f329 61 72 69 61 62 6c 65 73 20 74 6f 20 7a 65 72 6f  ariables to zero
2f32a 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 43 6f 6e  ..  **.  ** aCon
2f32b 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65  straint[].usable
2f32c 20 69 73 20 74 72 75 65 20 66 6f 72 20 63 6f 6e   is true for con
2f32d 73 74 72 61 69 6e 74 73 20 77 68 65 72 65 20 74  straints where t
2f32e 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 20 20  he right-hand.  
2f32f 2a 2a 20 73 69 64 65 20 63 6f 6e 74 61 69 6e 73  ** side contains
2f330 20 6f 6e 6c 79 20 72 65 66 65 72 65 6e 63 65 73   only references
2f331 20 74 6f 20 74 61 62 6c 65 73 20 74 6f 20 74 68   to tables to th
2f332 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20 63 75  e left of the cu
2f333 72 72 65 6e 74 0a 20 20 2a 2a 20 74 61 62 6c 65  rrent.  ** table
2f334 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
2f335 73 2c 20 69 66 20 74 68 65 20 63 6f 6e 73 74 72  s, if the constr
2f336 61 69 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66  aint is of the f
2f337 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  orm:.  **.  **  
2f338 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
2f339 3d 20 65 78 70 72 0a 20 20 2a 2a 0a 20 20 2a 2a  = expr.  **.  **
2f33a 20 61 6e 64 20 77 65 20 61 72 65 20 65 76 61 6c   and we are eval
2f33b 75 61 74 69 6e 67 20 61 20 6a 6f 69 6e 2c 20 74  uating a join, t
2f33c 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69  hen the constrai
2f33d 6e 74 20 6f 6e 20 63 6f 6c 75 6d 6e 20 69 73 20  nt on column is 
2f33e 0a 20 20 2a 2a 20 6f 6e 6c 79 20 76 61 6c 69 64  .  ** only valid
2f33f 20 69 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 72   if all tables r
2f340 65 66 65 72 65 6e 63 65 64 20 69 6e 20 65 78 70  eferenced in exp
2f341 72 20 6f 63 63 75 72 20 74 6f 20 74 68 65 20 6c  r occur to the l
2f342 65 66 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  eft.  ** of the 
2f343 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
2f344 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2a 0a 20 20   column..  **.  
2f345 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69  ** The aConstrai
2f346 6e 74 73 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74  nts[] array cont
2f347 61 69 6e 73 20 65 6e 74 72 69 65 73 20 66 6f 72  ains entries for
2f348 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73   all constraints
2f349 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 63 75 72  .  ** on the cur
2f34a 72 65 6e 74 20 74 61 62 6c 65 2e 20 20 54 68 61  rent table.  Tha
2f34b 74 20 77 61 79 20 77 65 20 6f 6e 6c 79 20 68 61  t way we only ha
2f34c 76 65 20 74 6f 20 63 6f 6d 70 75 74 65 20 69 74  ve to compute it
2f34d 20 6f 6e 63 65 0a 20 20 2a 2a 20 65 76 65 6e 20   once.  ** even 
2f34e 74 68 6f 75 67 68 20 77 65 20 6d 69 67 68 74 20  though we might 
2f34f 74 72 79 20 74 6f 20 70 69 63 6b 20 74 68 65 20  try to pick the 
2f350 62 65 73 74 20 69 6e 64 65 78 20 6d 75 6c 74 69  best index multi
2f351 70 6c 65 20 74 69 6d 65 73 2e 0a 20 20 2a 2a 20  ple times..  ** 
2f352 46 6f 72 20 65 61 63 68 20 61 74 74 65 6d 70 74  For each attempt
2f353 20 61 74 20 70 69 63 6b 69 6e 67 20 61 6e 20 69   at picking an i
2f354 6e 64 65 78 2c 20 74 68 65 20 6f 72 64 65 72 20  ndex, the order 
2f355 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  of tables in the
2f356 0a 20 20 2a 2a 20 6a 6f 69 6e 20 6d 69 67 68 74  .  ** join might
2f357 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 73 6f   be different so
2f358 20 77 65 20 68 61 76 65 20 74 6f 20 72 65 63 6f   we have to reco
2f359 6d 70 75 74 65 20 74 68 65 20 75 73 61 62 6c 65  mpute the usable
2f35a 20 66 6c 61 67 0a 20 20 2a 2a 20 65 61 63 68 20   flag.  ** each 
2f35b 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 70 49 64  time..  */.  pId
2f35c 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72 75 63 74  xCons = *(struct
2f35d 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
2f35e 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64  onstraint**)&pId
2f35f 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
2f360 6e 74 3b 0a 20 20 70 55 73 61 67 65 20 3d 20 70  nt;.  pUsage = p
2f361 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
2f362 61 69 6e 74 55 73 61 67 65 3b 0a 20 20 66 6f 72  aintUsage;.  for
2f363 28 69 3d 30 3b 20 69 3c 70 49 64 78 49 6e 66 6f  (i=0; i<pIdxInfo
2f364 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69  ->nConstraint; i
2f365 2b 2b 2c 20 70 49 64 78 43 6f 6e 73 2b 2b 29 7b  ++, pIdxCons++){
2f366 0a 20 20 20 20 6a 20 3d 20 70 49 64 78 43 6f 6e  .    j = pIdxCon
2f367 73 2d 3e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a  s->iTermOffset;.
2f368 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43      pTerm = &pWC
2f369 2d 3e 61 5b 6a 5d 3b 0a 20 20 20 20 70 49 64 78  ->a[j];.    pIdx
2f36a 43 6f 6e 73 2d 3e 75 73 61 62 6c 65 20 3d 20 28  Cons->usable = (
2f36b 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
2f36c 68 74 26 6e 6f 74 52 65 61 64 79 29 20 3f 20 30  ht&notReady) ? 0
2f36d 20 3a 20 31 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73   : 1;.  }.  mems
2f36e 65 74 28 70 55 73 61 67 65 2c 20 30 2c 20 73 69  et(pUsage, 0, si
2f36f 7a 65 6f 66 28 70 55 73 61 67 65 5b 30 5d 29 2a  zeof(pUsage[0])*
2f370 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74  pIdxInfo->nConst
2f371 72 61 69 6e 74 29 3b 0a 20 20 69 66 28 20 70 49  raint);.  if( pI
2f372 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72  dxInfo->needToFr
2f373 65 65 49 64 78 53 74 72 20 29 7b 0a 20 20 20 20  eeIdxStr ){.    
2f374 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 64  sqlite3_free(pId
2f375 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 29 3b 0a  xInfo->idxStr);.
2f376 20 20 7d 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e    }.  pIdxInfo->
2f377 69 64 78 53 74 72 20 3d 20 30 3b 0a 20 20 70 49  idxStr = 0;.  pI
2f378 64 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d  dxInfo->idxNum =
2f379 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e   0;.  pIdxInfo->
2f37a 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
2f37b 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f   = 0;.  pIdxInfo
2f37c 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65  ->orderByConsume
2f37d 64 20 3d 20 30 3b 0a 20 20 2f 2a 20 28 28 64 6f  d = 0;.  /* ((do
2f37e 75 62 6c 65 29 32 29 20 49 6e 20 63 61 73 65 20  uble)2) In case 
2f37f 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  of SQLITE_OMIT_F
2f380 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e  LOATING_POINT...
2f381 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e   */.  pIdxInfo->
2f382 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20  estimatedCost = 
2f383 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 2f  SQLITE_BIG_DBL /
2f384 20 28 28 64 6f 75 62 6c 65 29 32 29 3b 0a 20 20   ((double)2);.  
2f385 6e 4f 72 64 65 72 42 79 20 3d 20 70 49 64 78 49  nOrderBy = pIdxI
2f386 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 3b 0a 20  nfo->nOrderBy;. 
2f387 20 69 66 28 20 21 70 4f 72 64 65 72 42 79 20 29   if( !pOrderBy )
2f388 7b 0a 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e  {.    pIdxInfo->
2f389 6e 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20  nOrderBy = 0;.  
2f38a 7d 0a 0a 20 20 69 66 28 20 76 74 61 62 42 65 73  }..  if( vtabBes
2f38b 74 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  tIndex(pParse, p
2f38c 54 61 62 2c 20 70 49 64 78 49 6e 66 6f 29 20 29  Tab, pIdxInfo) )
2f38d 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
2f38e 7d 0a 0a 20 20 70 49 64 78 43 6f 6e 73 20 3d 20  }..  pIdxCons = 
2f38f 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  *(struct sqlite3
2f390 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
2f391 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61  t**)&pIdxInfo->a
2f392 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 66 6f  Constraint;.  fo
2f393 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 49 6e 66  r(i=0; i<pIdxInf
2f394 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20  o->nConstraint; 
2f395 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 55  i++){.    if( pU
2f396 73 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65  sage[i].argvInde
2f397 78 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 43 6f  x>0 ){.      pCo
2f398 73 74 2d 3e 75 73 65 64 20 7c 3d 20 70 57 43 2d  st->used |= pWC-
2f399 3e 61 5b 70 49 64 78 43 6f 6e 73 5b 69 5d 2e 69  >a[pIdxCons[i].i
2f39a 54 65 72 6d 4f 66 66 73 65 74 5d 2e 70 72 65 72  TermOffset].prer
2f39b 65 71 52 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20  eqRight;.    }. 
2f39c 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63 6f 73   }..  /* The cos
2f39d 74 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64  t is not allowed
2f39e 20 74 6f 20 62 65 20 6c 61 72 67 65 72 20 74 68   to be larger th
2f39f 61 6e 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42  an SQLITE_BIG_DB
2f3a0 4c 20 28 74 68 65 0a 20 20 2a 2a 20 69 6e 69 74  L (the.  ** init
2f3a1 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c 6f 77 65  al value of lowe
2f3a2 73 74 43 6f 73 74 20 69 6e 20 74 68 69 73 20 6c  stCost in this l
2f3a3 6f 6f 70 2e 20 49 66 20 69 74 20 69 73 2c 20 74  oop. If it is, t
2f3a4 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 28 63 6f  hen the.  ** (co
2f3a5 73 74 3c 6c 6f 77 65 73 74 43 6f 73 74 29 20 74  st<lowestCost) t
2f3a6 65 73 74 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e  est below will n
2f3a7 65 76 65 72 20 62 65 20 74 72 75 65 2e 0a 20 20  ever be true..  
2f3a8 2a 2a 20 0a 20 20 2a 2a 20 55 73 65 20 22 28 64  ** .  ** Use "(d
2f3a9 6f 75 62 6c 65 29 32 22 20 69 6e 73 74 65 61 64  ouble)2" instead
2f3aa 20 6f 66 20 22 32 2e 30 22 20 69 6e 20 63 61 73   of "2.0" in cas
2f3ab 65 20 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  e OMIT_FLOATING_
2f3ac 50 4f 49 4e 54 20 0a 20 20 2a 2a 20 69 73 20 64  POINT .  ** is d
2f3ad 65 66 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  efined..  */.  i
2f3ae 66 28 20 28 53 51 4c 49 54 45 5f 42 49 47 5f 44  f( (SQLITE_BIG_D
2f3af 42 4c 2f 28 28 64 6f 75 62 6c 65 29 32 29 29 3c  BL/((double)2))<
2f3b0 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61  pIdxInfo->estima
2f3b1 74 65 64 43 6f 73 74 20 29 7b 0a 20 20 20 20 70  tedCost ){.    p
2f3b2 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 28 53  Cost->rCost = (S
2f3b3 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2f 28 28  QLITE_BIG_DBL/((
2f3b4 64 6f 75 62 6c 65 29 32 29 29 3b 0a 20 20 7d 65  double)2));.  }e
2f3b5 6c 73 65 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e  lse{.    pCost->
2f3b6 72 43 6f 73 74 20 3d 20 70 49 64 78 49 6e 66 6f  rCost = pIdxInfo
2f3b7 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b  ->estimatedCost;
2f3b8 0a 20 20 7d 0a 20 20 70 43 6f 73 74 2d 3e 70 6c  .  }.  pCost->pl
2f3b9 61 6e 2e 75 2e 70 56 74 61 62 49 64 78 20 3d 20  an.u.pVtabIdx = 
2f3ba 70 49 64 78 49 6e 66 6f 3b 0a 20 20 69 66 28 20  pIdxInfo;.  if( 
2f3bb 70 49 64 78 49 6e 66 6f 2d 3e 6f 72 64 65 72 42  pIdxInfo->orderB
2f3bc 79 43 6f 6e 73 75 6d 65 64 20 29 7b 0a 20 20 20  yConsumed ){.   
2f3bd 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46   pCost->plan.wsF
2f3be 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 4f 52  lags |= WHERE_OR
2f3bf 44 45 52 42 59 3b 0a 20 20 7d 0a 20 20 70 43 6f  DERBY;.  }.  pCo
2f3c0 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20 3d 20 30  st->plan.nEq = 0
2f3c1 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f  ;.  pIdxInfo->nO
2f3c2 72 64 65 72 42 79 20 3d 20 6e 4f 72 64 65 72 42  rderBy = nOrderB
2f3c3 79 3b 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20  y;..  /* Try to 
2f3c4 66 69 6e 64 20 61 20 6d 6f 72 65 20 65 66 66 69  find a more effi
2f3c5 63 69 65 6e 74 20 61 63 63 65 73 73 20 70 61 74  cient access pat
2f3c6 74 65 72 6e 20 62 79 20 75 73 69 6e 67 20 6d 75  tern by using mu
2f3c7 6c 74 69 70 6c 65 20 69 6e 64 65 78 65 73 0a 20  ltiple indexes. 
2f3c8 20 2a 2a 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20   ** to optimize 
2f3c9 61 6e 20 4f 52 20 65 78 70 72 65 73 73 69 6f 6e  an OR expression
2f3ca 20 77 69 74 68 69 6e 20 74 68 65 20 57 48 45 52   within the WHER
2f3cb 45 20 63 6c 61 75 73 65 2e 20 0a 20 20 2a 2f 0a  E clause. .  */.
2f3cc 20 20 62 65 73 74 4f 72 43 6c 61 75 73 65 49 6e    bestOrClauseIn
2f3cd 64 65 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c  dex(pParse, pWC,
2f3ce 20 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c   pSrc, notReady,
2f3cf 20 70 4f 72 64 65 72 42 79 2c 20 70 43 6f 73 74   pOrderBy, pCost
2f3d0 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
2f3d1 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2f3d2 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a  ALTABLE */../*.*
2f3d3 2a 20 41 72 67 75 6d 65 6e 74 20 70 49 64 78 20  * Argument pIdx 
2f3d4 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
2f3d5 61 6e 20 69 6e 64 65 78 20 73 74 72 75 63 74 75  an index structu
2f3d6 72 65 20 74 68 61 74 20 68 61 73 20 61 6e 20 61  re that has an a
2f3d7 72 72 61 79 20 6f 66 0a 2a 2a 20 53 51 4c 49 54  rray of.** SQLIT
2f3d8 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20  E_INDEX_SAMPLES 
2f3d9 65 76 65 6e 6c 79 20 73 70 61 63 65 64 20 73 61  evenly spaced sa
2f3da 6d 70 6c 65 73 20 6f 66 20 74 68 65 20 66 69 72  mples of the fir
2f3db 73 74 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d  st indexed colum
2f3dc 6e 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 49  n.** stored in I
2f3dd 6e 64 65 78 2e 61 53 61 6d 70 6c 65 2e 20 54 68  ndex.aSample. Th
2f3de 65 20 64 6f 6d 61 69 6e 20 6f 66 20 76 61 6c 75  e domain of valu
2f3df 65 73 20 73 74 6f 72 65 64 20 69 6e 20 73 61 69  es stored in sai
2f3e0 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6d 61 79 20  d column.** may 
2f3e1 62 65 20 74 68 6f 75 67 68 74 20 6f 66 20 61 73  be thought of as
2f3e2 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 28 53   divided into (S
2f3e3 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
2f3e4 4c 45 53 2b 31 29 20 72 65 67 69 6f 6e 73 2e 0a  LES+1) regions..
2f3e5 2a 2a 20 52 65 67 69 6f 6e 20 30 20 63 6f 6e 74  ** Region 0 cont
2f3e6 61 69 6e 73 20 61 6c 6c 20 76 61 6c 75 65 73 20  ains all values 
2f3e7 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65  smaller than the
2f3e8 20 66 69 72 73 74 20 73 61 6d 70 6c 65 20 76 61   first sample va
2f3e9 6c 75 65 2e 20 52 65 67 69 6f 6e 0a 2a 2a 20 31  lue. Region.** 1
2f3ea 20 63 6f 6e 74 61 69 6e 73 20 76 61 6c 75 65 73   contains values
2f3eb 20 6c 61 72 67 65 72 20 74 68 61 6e 20 6f 72 20   larger than or 
2f3ec 65 71 75 61 6c 20 74 6f 20 74 68 65 20 76 61 6c  equal to the val
2f3ed 75 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ue of the first 
2f3ee 73 61 6d 70 6c 65 2c 0a 2a 2a 20 62 75 74 20 73  sample,.** but s
2f3ef 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20  maller than the 
2f3f0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73 65 63  value of the sec
2f3f1 6f 6e 64 2e 20 41 6e 64 20 73 6f 20 6f 6e 2e 0a  ond. And so on..
2f3f2 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73  **.** If success
2f3f3 66 75 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ful, this functi
2f3f4 6f 6e 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  on determines wh
2f3f5 69 63 68 20 6f 66 20 74 68 65 20 72 65 67 69 6f  ich of the regio
2f3f6 6e 73 20 76 61 6c 75 65 20 0a 2a 2a 20 70 56 61  ns value .** pVa
2f3f7 6c 20 6c 69 65 73 20 69 6e 2c 20 73 65 74 73 20  l lies in, sets 
2f3f8 2a 70 69 52 65 67 69 6f 6e 20 74 6f 20 74 68 65  *piRegion to the
2f3f9 20 72 65 67 69 6f 6e 20 69 6e 64 65 78 20 28 61   region index (a
2f3fa 20 76 61 6c 75 65 20 62 65 74 77 65 65 6e 20 30   value between 0
2f3fb 0a 2a 2a 20 61 6e 64 20 53 51 4c 49 54 45 5f 49  .** and SQLITE_I
2f3fc 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2b 31 2c 20  NDEX_SAMPLES+1, 
2f3fd 69 6e 63 6c 75 73 69 76 65 29 20 61 6e 64 20 72  inclusive) and r
2f3fe 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
2f3ff 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 61 6e 20 4f  ..** Or, if an O
2f400 4f 4d 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  OM occurs while 
2f401 63 6f 6e 76 65 72 74 69 6e 67 20 74 65 78 74 20  converting text 
2f402 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 65  values between e
2f403 6e 63 6f 64 69 6e 67 73 2c 0a 2a 2a 20 53 51 4c  ncodings,.** SQL
2f404 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74  ITE_NOMEM is ret
2f405 75 72 6e 65 64 20 61 6e 64 20 2a 70 69 52 65 67  urned and *piReg
2f406 69 6f 6e 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ion is undefined
2f407 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
2f408 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a  TE_ENABLE_STAT2.
2f409 73 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 65  static int where
2f40a 52 61 6e 67 65 52 65 67 69 6f 6e 28 0a 20 20 50  RangeRegion(.  P
2f40b 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2f40c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2f40d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2f40e 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  n */.  Index *pI
2f40f 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  dx,             
2f410 20 20 20 2f 2a 20 49 6e 64 65 78 20 74 6f 20 63     /* Index to c
2f411 6f 6e 73 69 64 65 72 20 64 6f 6d 61 69 6e 20 6f  onsider domain o
2f412 66 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  f */.  sqlite3_v
2f413 61 6c 75 65 20 2a 70 56 61 6c 2c 20 20 20 20 20  alue *pVal,     
2f414 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 63     /* Value to c
2f415 6f 6e 73 69 64 65 72 20 2a 2f 0a 20 20 69 6e 74  onsider */.  int
2f416 20 2a 70 69 52 65 67 69 6f 6e 20 20 20 20 20 20   *piRegion      
2f417 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
2f418 20 52 65 67 69 6f 6e 20 6f 66 20 64 6f 6d 61 69   Region of domai
2f419 6e 20 69 6e 20 77 68 69 63 68 20 76 61 6c 75 65  n in which value
2f41a 20 6c 69 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 66   lies */.){.  if
2f41b 28 20 41 4c 57 41 59 53 28 70 56 61 6c 29 20 29  ( ALWAYS(pVal) )
2f41c 7b 0a 20 20 20 20 49 6e 64 65 78 53 61 6d 70 6c  {.    IndexSampl
2f41d 65 20 2a 61 53 61 6d 70 6c 65 20 3d 20 70 49 64  e *aSample = pId
2f41e 78 2d 3e 61 53 61 6d 70 6c 65 3b 0a 20 20 20 20  x->aSample;.    
2f41f 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 20 20 69  int i = 0;.    i
2f420 6e 74 20 65 54 79 70 65 20 3d 20 73 71 6c 69 74  nt eType = sqlit
2f421 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 56  e3_value_type(pV
2f422 61 6c 29 3b 0a 0a 20 20 20 20 69 66 28 20 65 54  al);..    if( eT
2f423 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45  ype==SQLITE_INTE
2f424 47 45 52 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51  GER || eType==SQ
2f425 4c 49 54 45 5f 46 4c 4f 41 54 20 29 7b 0a 20 20  LITE_FLOAT ){.  
2f426 20 20 20 20 64 6f 75 62 6c 65 20 72 20 3d 20 73      double r = s
2f427 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
2f428 62 6c 65 28 70 56 61 6c 29 3b 0a 20 20 20 20 20  ble(pVal);.     
2f429 20 66 6f 72 28 69 3d 30 3b 20 69 3c 53 51 4c 49   for(i=0; i<SQLI
2f42a 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53  TE_INDEX_SAMPLES
2f42b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
2f42c 69 66 28 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65  if( aSample[i].e
2f42d 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c  Type==SQLITE_NUL
2f42e 4c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  L ) continue;.  
2f42f 20 20 20 20 20 20 69 66 28 20 61 53 61 6d 70 6c        if( aSampl
2f430 65 5b 69 5d 2e 65 54 79 70 65 3e 3d 53 51 4c 49  e[i].eType>=SQLI
2f431 54 45 5f 54 45 58 54 20 7c 7c 20 61 53 61 6d 70  TE_TEXT || aSamp
2f432 6c 65 5b 69 5d 2e 75 2e 72 3e 72 20 29 20 62 72  le[i].u.r>r ) br
2f433 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2f434 20 7d 65 6c 73 65 7b 20 0a 20 20 20 20 20 20 73   }else{ .      s
2f435 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2f436 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 43  rse->db;.      C
2f437 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
2f438 20 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a       const u8 *z
2f439 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 0a  ;.      int n;..
2f43a 20 20 20 20 20 20 2f 2a 20 70 56 61 6c 20 63 6f        /* pVal co
2f43b 6d 65 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  mes from sqlite3
2f43c 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 29 20  ValueFromExpr() 
2f43d 73 6f 20 74 68 65 20 74 79 70 65 20 63 61 6e 6e  so the type cann
2f43e 6f 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ot be NULL */.  
2f43f 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79 70      assert( eTyp
2f440 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 7c  e==SQLITE_TEXT |
2f441 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
2f442 42 4c 4f 42 20 29 3b 0a 0a 20 20 20 20 20 20 69  BLOB );..      i
2f443 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  f( eType==SQLITE
2f444 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 20  _BLOB ){.       
2f445 20 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 20 2a   z = (const u8 *
2f446 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  )sqlite3_value_b
2f447 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20 20 20 20  lob(pVal);.     
2f448 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70     pColl = db->p
2f449 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  DfltColl;.      
2f44a 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 6c 2d    assert( pColl-
2f44b 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  >enc==SQLITE_UTF
2f44c 38 20 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  8 );.      }else
2f44d 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20  {.        pColl 
2f44e 3d 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c  = sqlite3GetColl
2f44f 53 65 71 28 64 62 2c 20 53 51 4c 49 54 45 5f 55  Seq(db, SQLITE_U
2f450 54 46 38 2c 20 30 2c 20 2a 70 49 64 78 2d 3e 61  TF8, 0, *pIdx->a
2f451 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20  zColl);.        
2f452 69 66 28 20 70 43 6f 6c 6c 3d 3d 30 20 29 7b 0a  if( pColl==0 ){.
2f453 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2f454 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2f455 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61  , "no such colla
2f456 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25  tion sequence: %
2f457 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  s",.            
2f458 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70                *p
2f459 49 64 78 2d 3e 61 7a 43 6f 6c 6c 29 3b 0a 20 20  Idx->azColl);.  
2f45a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
2f45b 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
2f45c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a       }.        z
2f45d 20 3d 20 28 63 6f 6e 73 74 20 75 38 20 2a 29 73   = (const u8 *)s
2f45e 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
2f45f 70 56 61 6c 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63  pVal, pColl->enc
2f460 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  );.        if( !
2f461 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  z ){.          r
2f462 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
2f463 45 4d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  EM;.        }.  
2f464 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 20        assert( z 
2f465 26 26 20 70 43 6f 6c 6c 20 26 26 20 70 43 6f 6c  && pColl && pCol
2f466 6c 2d 3e 78 43 6d 70 20 29 3b 0a 20 20 20 20 20  l->xCmp );.     
2f467 20 7d 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c   }.      n = sql
2f468 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70  ite3ValueBytes(p
2f469 56 61 6c 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29  Val, pColl->enc)
2f46a 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  ;..      for(i=0
2f46b 3b 20 69 3c 53 51 4c 49 54 45 5f 49 4e 44 45 58  ; i<SQLITE_INDEX
2f46c 5f 53 41 4d 50 4c 45 53 3b 20 69 2b 2b 29 7b 0a  _SAMPLES; i++){.
2f46d 20 20 20 20 20 20 20 20 69 6e 74 20 72 3b 0a 20          int r;. 
2f46e 20 20 20 20 20 20 20 69 6e 74 20 65 53 61 6d 70         int eSamp
2f46f 6c 65 74 79 70 65 20 3d 20 61 53 61 6d 70 6c 65  letype = aSample
2f470 5b 69 5d 2e 65 54 79 70 65 3b 0a 20 20 20 20 20  [i].eType;.     
2f471 20 20 20 69 66 28 20 65 53 61 6d 70 6c 65 74 79     if( eSamplety
2f472 70 65 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  pe==SQLITE_NULL 
2f473 7c 7c 20 65 53 61 6d 70 6c 65 74 79 70 65 3c 65  || eSampletype<e
2f474 54 79 70 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  Type ) continue;
2f475 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 65 53  .        if( (eS
2f476 61 6d 70 6c 65 74 79 70 65 21 3d 65 54 79 70 65  ampletype!=eType
2f477 29 20 29 20 62 72 65 61 6b 3b 0a 23 69 66 6e 64  ) ) break;.#ifnd
2f478 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
2f479 54 46 31 36 0a 20 20 20 20 20 20 20 20 69 66 28  TF16.        if(
2f47a 20 70 43 6f 6c 6c 2d 3e 65 6e 63 21 3d 53 51 4c   pColl->enc!=SQL
2f47b 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20  ITE_UTF8 ){.    
2f47c 20 20 20 20 20 20 69 6e 74 20 6e 53 61 6d 70 6c        int nSampl
2f47d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61  e;.          cha
2f47e 72 20 2a 7a 53 61 6d 70 6c 65 20 3d 20 73 71 6c  r *zSample = sql
2f47f 69 74 65 33 55 74 66 38 74 6f 31 36 28 0a 20 20  ite3Utf8to16(.  
2f480 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2c 20              db, 
2f481 70 43 6f 6c 6c 2d 3e 65 6e 63 2c 20 61 53 61 6d  pColl->enc, aSam
2f482 70 6c 65 5b 69 5d 2e 75 2e 7a 2c 20 61 53 61 6d  ple[i].u.z, aSam
2f483 70 6c 65 5b 69 5d 2e 6e 42 79 74 65 2c 20 26 6e  ple[i].nByte, &n
2f484 53 61 6d 70 6c 65 0a 20 20 20 20 20 20 20 20 20  Sample.         
2f485 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66   );.          if
2f486 28 20 21 7a 53 61 6d 70 6c 65 20 29 7b 0a 20 20  ( !zSample ){.  
2f487 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2f488 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2f489 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ed );.          
2f48a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2f48b 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
2f48c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 20 3d   }.          r =
2f48d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f   pColl->xCmp(pCo
2f48e 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e 53 61 6d 70  ll->pUser, nSamp
2f48f 6c 65 2c 20 7a 53 61 6d 70 6c 65 2c 20 6e 2c 20  le, zSample, n, 
2f490 7a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  z);.          sq
2f491 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2f492 7a 53 61 6d 70 6c 65 29 3b 0a 20 20 20 20 20 20  zSample);.      
2f493 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
2f494 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20         {.       
2f495 20 20 20 72 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43     r = pColl->xC
2f496 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c  mp(pColl->pUser,
2f497 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 6e 42 79 74   aSample[i].nByt
2f498 65 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e  e, aSample[i].u.
2f499 7a 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 20 20  z, n, z);.      
2f49a 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2f49b 72 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  r>0 ) break;.   
2f49c 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
2f49d 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
2f49e 69 3c 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  i<=SQLITE_INDEX_
2f49f 53 41 4d 50 4c 45 53 20 29 3b 0a 20 20 20 20 2a  SAMPLES );.    *
2f4a0 70 69 52 65 67 69 6f 6e 20 3d 20 69 3b 0a 20 20  piRegion = i;.  
2f4a1 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
2f4a2 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 20  E_OK;.}.#endif  
2f4a3 20 2f 2a 20 23 69 66 64 65 66 20 53 51 4c 49 54   /* #ifdef SQLIT
2f4a4 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 2a  E_ENABLE_STAT2 *
2f4a5 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 65 78 70 72  /../*.** If expr
2f4a6 65 73 73 69 6f 6e 20 70 45 78 70 72 20 72 65 70  ession pExpr rep
2f4a7 72 65 73 65 6e 74 73 20 61 20 6c 69 74 65 72 61  resents a litera
2f4a8 6c 20 76 61 6c 75 65 2c 20 73 65 74 20 2a 70 70  l value, set *pp
2f4a9 20 74 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20   to point to.** 
2f4aa 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  an sqlite3_value
2f4ab 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
2f4ac 69 6e 69 6e 67 20 74 68 65 20 73 61 6d 65 20 76  ining the same v
2f4ad 61 6c 75 65 2c 20 77 69 74 68 20 61 66 66 69 6e  alue, with affin
2f4ae 69 74 79 0a 2a 2a 20 61 66 66 20 61 70 70 6c 69  ity.** aff appli
2f4af 65 64 20 74 6f 20 69 74 2c 20 62 65 66 6f 72 65  ed to it, before
2f4b0 20 72 65 74 75 72 6e 69 6e 67 2e 20 49 74 20 69   returning. It i
2f4b1 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69  s the responsibi
2f4b2 6c 69 74 79 20 6f 66 20 74 68 65 20 0a 2a 2a 20  lity of the .** 
2f4b3 63 61 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75  caller to eventu
2f4b4 61 6c 6c 79 20 72 65 6c 65 61 73 65 20 74 68 69  ally release thi
2f4b5 73 20 73 74 72 75 63 74 75 72 65 20 62 79 20 70  s structure by p
2f4b6 61 73 73 69 6e 67 20 69 74 20 74 6f 20 0a 2a 2a  assing it to .**
2f4b7 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
2f4b8 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  e()..**.** If th
2f4b9 65 20 63 75 72 72 65 6e 74 20 70 61 72 73 65 20  e current parse 
2f4ba 69 73 20 61 20 72 65 63 6f 6d 70 69 6c 65 20 28  is a recompile (
2f4bb 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65  sqlite3Reprepare
2f4bc 28 29 29 20 61 6e 64 20 70 45 78 70 72 0a 2a 2a  ()) and pExpr.**
2f4bd 20 69 73 20 61 6e 20 53 51 4c 20 76 61 72 69 61   is an SQL varia
2f4be 62 6c 65 20 74 68 61 74 20 63 75 72 72 65 6e 74  ble that current
2f4bf 6c 79 20 68 61 73 20 61 20 6e 6f 6e 2d 4e 55 4c  ly has a non-NUL
2f4c0 4c 20 76 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f  L value bound to
2f4c1 20 69 74 2c 0a 2a 2a 20 63 72 65 61 74 65 20 61   it,.** create a
2f4c2 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  n sqlite3_value 
2f4c3 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
2f4c4 6e 69 6e 67 20 74 68 69 73 20 76 61 6c 75 65 2c  ning this value,
2f4c5 20 61 67 61 69 6e 20 77 69 74 68 0a 2a 2a 20 61   again with.** a
2f4c6 66 66 69 6e 69 74 79 20 61 66 66 20 61 70 70 6c  ffinity aff appl
2f4c7 69 65 64 20 74 6f 20 69 74 2c 20 69 6e 73 74 65  ied to it, inste
2f4c8 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 65 69  ad..**.** If nei
2f4c9 74 68 65 72 20 6f 66 20 74 68 65 20 61 62 6f 76  ther of the abov
2f4ca 65 20 61 70 70 6c 79 2c 20 73 65 74 20 2a 70 70  e apply, set *pp
2f4cb 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   to NULL..**.** 
2f4cc 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
2f4cd 72 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  rs, return an er
2f4ce 72 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77  ror code. Otherw
2f4cf 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  ise, SQLITE_OK..
2f4d0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
2f4d1 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 73 74  _ENABLE_STAT2.st
2f4d2 61 74 69 63 20 69 6e 74 20 76 61 6c 75 65 46 72  atic int valueFr
2f4d3 6f 6d 45 78 70 72 28 0a 20 20 50 61 72 73 65 20  omExpr(.  Parse 
2f4d4 2a 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72  *pParse, .  Expr
2f4d5 20 2a 70 45 78 70 72 2c 20 0a 20 20 75 38 20 61   *pExpr, .  u8 a
2f4d6 66 66 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76  ff, .  sqlite3_v
2f4d7 61 6c 75 65 20 2a 2a 70 70 0a 29 7b 0a 20 20 2f  alue **pp.){.  /
2f4d8 2a 20 54 68 65 20 65 76 61 6c 43 6f 6e 73 74 45  * The evalConstE
2f4d9 78 70 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 77  xpr() function w
2f4da 69 6c 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79  ill have already
2f4db 20 63 6f 6e 76 65 72 74 65 64 20 61 6e 79 20 54   converted any T
2f4dc 4b 5f 56 41 52 49 41 42 4c 45 0a 20 20 2a 2a 20  K_VARIABLE.  ** 
2f4dd 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 76 6f 6c  expression invol
2f4de 76 65 64 20 69 6e 20 61 6e 20 63 6f 6d 70 61 72  ved in an compar
2f4df 69 73 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 52  ison into a TK_R
2f4e0 45 47 49 53 54 45 52 2e 20 2a 2f 0a 20 20 61 73  EGISTER. */.  as
2f4e1 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
2f4e2 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a  =TK_VARIABLE );.
2f4e3 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d    if( pExpr->op=
2f4e4 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 26 26 20  =TK_REGISTER && 
2f4e5 70 45 78 70 72 2d 3e 6f 70 32 3d 3d 54 4b 5f 56  pExpr->op2==TK_V
2f4e6 41 52 49 41 42 4c 45 20 29 7b 0a 20 20 20 20 69  ARIABLE ){.    i
2f4e7 6e 74 20 69 56 61 72 20 3d 20 70 45 78 70 72 2d  nt iVar = pExpr-
2f4e8 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71  >iColumn;.    sq
2f4e9 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 72 6d  lite3VdbeSetVarm
2f4ea 61 73 6b 28 70 50 61 72 73 65 2d 3e 70 56 64 62  ask(pParse->pVdb
2f4eb 65 2c 20 69 56 61 72 29 3b 0a 20 20 20 20 2a 70  e, iVar);.    *p
2f4ec 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47  p = sqlite3VdbeG
2f4ed 65 74 56 61 6c 75 65 28 70 50 61 72 73 65 2d 3e  etValue(pParse->
2f4ee 70 52 65 70 72 65 70 61 72 65 2c 20 69 56 61 72  pReprepare, iVar
2f4ef 2c 20 61 66 66 29 3b 0a 20 20 20 20 72 65 74 75  , aff);.    retu
2f4f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
2f4f1 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
2f4f2 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
2f4f3 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 45 78 70  pParse->db, pExp
2f4f4 72 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  r, SQLITE_UTF8, 
2f4f5 61 66 66 2c 20 70 70 29 3b 0a 7d 0a 23 65 6e 64  aff, pp);.}.#end
2f4f6 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  if../*.** This f
2f4f7 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
2f4f8 74 6f 20 65 73 74 69 6d 61 74 65 20 74 68 65 20  to estimate the 
2f4f9 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74  number of rows t
2f4fa 68 61 74 20 77 69 6c 6c 20 62 65 20 76 69 73 69  hat will be visi
2f4fb 74 65 64 0a 2a 2a 20 62 79 20 73 63 61 6e 6e 69  ted.** by scanni
2f4fc 6e 67 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 20  ng an index for 
2f4fd 61 20 72 61 6e 67 65 20 6f 66 20 76 61 6c 75 65  a range of value
2f4fe 73 2e 20 54 68 65 20 72 61 6e 67 65 20 6d 61 79  s. The range may
2f4ff 20 68 61 76 65 20 61 6e 20 75 70 70 65 72 0a 2a   have an upper.*
2f500 2a 20 62 6f 75 6e 64 2c 20 61 20 6c 6f 77 65 72  * bound, a lower
2f501 20 62 6f 75 6e 64 2c 20 6f 72 20 62 6f 74 68 2e   bound, or both.
2f502 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
2f503 65 20 74 65 72 6d 73 20 74 68 61 74 20 73 65 74  e terms that set
2f504 20 74 68 65 20 75 70 70 65 72 0a 2a 2a 20 61 6e   the upper.** an
2f505 64 20 6c 6f 77 65 72 20 62 6f 75 6e 64 73 20 61  d lower bounds a
2f506 72 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 62  re represented b
2f507 79 20 70 4c 6f 77 65 72 20 61 6e 64 20 70 55 70  y pLower and pUp
2f508 70 65 72 20 72 65 73 70 65 63 74 69 76 65 6c 79  per respectively
2f509 2e 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65  . For.** example
2f50a 2c 20 61 73 73 75 6d 69 6e 67 20 74 68 61 74 20  , assuming that 
2f50b 69 6e 64 65 78 20 70 20 69 73 20 6f 6e 20 74 31  index p is on t1
2f50c 28 61 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e  (a):.**.**   ...
2f50d 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61   FROM t1 WHERE a
2f50e 20 3e 20 3f 20 41 4e 44 20 61 20 3c 20 3f 20 2e   > ? AND a < ? .
2f50f 2e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ...**           
2f510 20 20 20 20 20 20 20 20 20 7c 5f 5f 5f 5f 5f 7c           |_____|
2f511 20 20 20 7c 5f 5f 5f 5f 5f 7c 0a 2a 2a 20 20 20     |_____|.**   
2f512 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f513 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 7c 0a      |         |.
2f514 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2f515 20 20 20 20 20 20 20 70 4c 6f 77 65 72 20 20 20         pLower   
2f516 20 70 55 70 70 65 72 0a 2a 2a 0a 2a 2a 20 49 66   pUpper.**.** If
2f517 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 75   either of the u
2f518 70 70 65 72 20 6f 72 20 6c 6f 77 65 72 20 62 6f  pper or lower bo
2f519 75 6e 64 20 69 73 20 6e 6f 74 20 70 72 65 73 65  und is not prese
2f51a 6e 74 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73  nt, then NULL is
2f51b 20 70 61 73 73 65 64 20 69 6e 0a 2a 2a 20 70 6c   passed in.** pl
2f51c 61 63 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65  ace of the corre
2f51d 73 70 6f 6e 64 69 6e 67 20 57 68 65 72 65 54 65  sponding WhereTe
2f51e 72 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45  rm..**.** The nE
2f51f 71 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 70  q parameter is p
2f520 61 73 73 65 64 20 74 68 65 20 69 6e 64 65 78 20  assed the index 
2f521 6f 66 20 74 68 65 20 69 6e 64 65 78 20 63 6f 6c  of the index col
2f522 75 6d 6e 20 73 75 62 6a 65 63 74 20 74 6f 20 74  umn subject to t
2f523 68 65 0a 2a 2a 20 72 61 6e 67 65 20 63 6f 6e 73  he.** range cons
2f524 74 72 61 69 6e 74 2e 20 4f 72 2c 20 65 71 75 69  traint. Or, equi
2f525 76 61 6c 65 6e 74 6c 79 2c 20 74 68 65 20 6e 75  valently, the nu
2f526 6d 62 65 72 20 6f 66 20 65 71 75 61 6c 69 74 79  mber of equality
2f527 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 2a 2a 20   constraints.** 
2f528 6f 70 74 69 6d 69 7a 65 64 20 62 79 20 74 68 65  optimized by the
2f529 20 70 72 6f 70 6f 73 65 64 20 69 6e 64 65 78 20   proposed index 
2f52a 73 63 61 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c  scan. For exampl
2f52b 65 2c 20 61 73 73 75 6d 69 6e 67 20 69 6e 64 65  e, assuming inde
2f52c 78 20 70 20 69 73 0a 2a 2a 20 6f 6e 20 74 31 28  x p is.** on t1(
2f52d 61 2c 20 62 29 2c 20 61 6e 64 20 74 68 65 20 53  a, b), and the S
2f52e 51 4c 20 71 75 65 72 79 20 69 73 3a 0a 2a 2a 0a  QL query is:.**.
2f52f 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f 4d 20 74 31  **   ... FROM t1
2f530 20 57 48 45 52 45 20 61 20 3d 20 3f 20 41 4e 44   WHERE a = ? AND
2f531 20 62 20 3e 20 3f 20 41 4e 44 20 62 20 3c 20 3f   b > ? AND b < ?
2f532 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20   ....**.** then 
2f533 6e 45 71 20 73 68 6f 75 6c 64 20 62 65 20 70 61  nEq should be pa
2f534 73 73 65 64 20 74 68 65 20 76 61 6c 75 65 20 31  ssed the value 1
2f535 20 28 61 73 20 74 68 65 20 72 61 6e 67 65 20 72   (as the range r
2f536 65 73 74 72 69 63 74 65 64 20 63 6f 6c 75 6d 6e  estricted column
2f537 2c 0a 2a 2a 20 62 2c 20 69 73 20 74 68 65 20 73  ,.** b, is the s
2f538 65 63 6f 6e 64 20 6c 65 66 74 2d 6d 6f 73 74 20  econd left-most 
2f539 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e  column of the in
2f53a 64 65 78 29 2e 20 4f 72 2c 20 69 66 20 74 68 65  dex). Or, if the
2f53b 20 71 75 65 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a   query is:.**.**
2f53c 20 20 20 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 57     ... FROM t1 W
2f53d 48 45 52 45 20 61 20 3e 20 3f 20 41 4e 44 20 61  HERE a > ? AND a
2f53e 20 3c 20 3f 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 74   < ? ....**.** t
2f53f 68 65 6e 20 6e 45 71 20 73 68 6f 75 6c 64 20 62  hen nEq should b
2f540 65 20 70 61 73 73 65 64 20 30 2e 0a 2a 2a 0a 2a  e passed 0..**.*
2f541 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76  * The returned v
2f542 61 6c 75 65 20 69 73 20 61 6e 20 69 6e 74 65 67  alue is an integ
2f543 65 72 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  er between 1 and
2f544 20 31 30 30 2c 20 69 6e 63 6c 75 73 69 76 65 2e   100, inclusive.
2f545 20 41 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c   A return.** val
2f546 75 65 20 6f 66 20 31 20 69 6e 64 69 63 61 74 65  ue of 1 indicate
2f547 73 20 74 68 61 74 20 74 68 65 20 70 72 6f 70 6f  s that the propo
2f548 73 65 64 20 72 61 6e 67 65 20 73 63 61 6e 20 69  sed range scan i
2f549 73 20 65 78 70 65 63 74 65 64 20 74 6f 20 76 69  s expected to vi
2f54a 73 69 74 0a 2a 2a 20 61 70 70 72 6f 78 69 6d 61  sit.** approxima
2f54b 74 65 6c 79 20 31 2f 31 30 30 74 68 20 28 31 25  tely 1/100th (1%
2f54c 29 20 6f 66 20 74 68 65 20 72 6f 77 73 20 73 65  ) of the rows se
2f54d 6c 65 63 74 65 64 20 62 79 20 74 68 65 20 6e 45  lected by the nE
2f54e 71 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20 63 6f  q equality.** co
2f54f 6e 73 74 72 61 69 6e 74 73 20 28 69 66 20 61 6e  nstraints (if an
2f550 79 29 2e 20 41 20 72 65 74 75 72 6e 20 76 61 6c  y). A return val
2f551 75 65 20 6f 66 20 31 30 30 20 69 6e 64 69 63 61  ue of 100 indica
2f552 74 65 73 20 74 68 61 74 20 69 74 20 69 73 20 65  tes that it is e
2f553 78 70 65 63 74 65 64 0a 2a 2a 20 74 68 61 74 20  xpected.** that 
2f554 74 68 65 20 72 61 6e 67 65 20 73 63 61 6e 20 77  the range scan w
2f555 69 6c 6c 20 76 69 73 69 74 20 65 76 65 72 79 20  ill visit every 
2f556 72 6f 77 20 28 31 30 30 25 29 20 73 65 6c 65 63  row (100%) selec
2f557 74 65 64 20 62 79 20 74 68 65 20 65 71 75 61 6c  ted by the equal
2f558 69 74 79 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e  ity.** constrain
2f559 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  ts..**.** In the
2f55a 20 61 62 73 65 6e 63 65 20 6f 66 20 73 71 6c 69   absence of sqli
2f55b 74 65 5f 73 74 61 74 32 20 41 4e 41 4c 59 5a 45  te_stat2 ANALYZE
2f55c 20 64 61 74 61 2c 20 65 61 63 68 20 72 61 6e 67   data, each rang
2f55d 65 20 69 6e 65 71 75 61 6c 69 74 79 0a 2a 2a 20  e inequality.** 
2f55e 72 65 64 75 63 65 73 20 74 68 65 20 73 65 61 72  reduces the sear
2f55f 63 68 20 73 70 61 63 65 20 62 79 20 32 2f 33 72  ch space by 2/3r
2f560 64 73 2e 20 20 48 65 6e 63 65 20 61 20 73 69 6e  ds.  Hence a sin
2f561 67 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 28  gle constraint (
2f562 78 3e 3f 29 0a 2a 2a 20 72 65 73 75 6c 74 73 20  x>?).** results 
2f563 69 6e 20 61 20 72 65 74 75 72 6e 20 6f 66 20 33  in a return of 3
2f564 33 20 61 6e 64 20 61 20 72 61 6e 67 65 20 63 6f  3 and a range co
2f565 6e 73 74 72 61 69 6e 74 20 28 78 3e 3f 20 41 4e  nstraint (x>? AN
2f566 44 20 78 3c 3f 29 20 72 65 73 75 6c 74 73 0a 2a  D x<?) results.*
2f567 2a 20 69 6e 20 61 20 72 65 74 75 72 6e 20 6f 66  * in a return of
2f568 20 31 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69   11..*/.static i
2f569 6e 74 20 77 68 65 72 65 52 61 6e 67 65 53 63 61  nt whereRangeSca
2f56a 6e 45 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70  nEst(.  Parse *p
2f56b 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
2f56c 50 61 72 73 69 6e 67 20 26 20 63 6f 64 65 20 67  Parsing & code g
2f56d 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78  enerating contex
2f56e 74 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 2c  t */.  Index *p,
2f56f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2f570 68 65 20 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e  he index contain
2f571 69 6e 67 20 74 68 65 20 72 61 6e 67 65 2d 63 6f  ing the range-co
2f572 6d 70 61 72 65 64 20 63 6f 6c 75 6d 6e 3b 20 22  mpared column; "
2f573 78 22 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 71 2c  x" */.  int nEq,
2f574 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f575 69 6e 64 65 78 20 69 6e 74 6f 20 70 2d 3e 61 43  index into p->aC
2f576 6f 6c 5b 5d 20 6f 66 20 74 68 65 20 72 61 6e 67  ol[] of the rang
2f577 65 2d 63 6f 6d 70 61 72 65 64 20 63 6f 6c 75 6d  e-compared colum
2f578 6e 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d  n */.  WhereTerm
2f579 20 2a 70 4c 6f 77 65 72 2c 20 20 20 2f 2a 20 4c   *pLower,   /* L
2f57a 6f 77 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68  ower bound on th
2f57b 65 20 72 61 6e 67 65 2e 20 65 78 3a 20 22 78 3e  e range. ex: "x>
2f57c 31 32 33 22 20 4d 69 67 68 74 20 62 65 20 4e 55  123" Might be NU
2f57d 4c 4c 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  LL */.  WhereTer
2f57e 6d 20 2a 70 55 70 70 65 72 2c 20 20 20 2f 2a 20  m *pUpper,   /* 
2f57f 55 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74  Upper bound on t
2f580 68 65 20 72 61 6e 67 65 2e 20 65 78 3a 20 22 78  he range. ex: "x
2f581 3c 34 35 35 22 20 4d 69 67 68 74 20 62 65 20 4e  <455" Might be N
2f582 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ULL */.  int *pi
2f583 45 73 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Est           /*
2f584 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 76 61 6c   OUT: Return val
2f585 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ue */.){.  int r
2f586 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
2f587 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
2f588 41 42 4c 45 5f 53 54 41 54 32 0a 0a 20 20 69 66  ABLE_STAT2..  if
2f589 28 20 6e 45 71 3d 3d 30 20 26 26 20 70 2d 3e 61  ( nEq==0 && p->a
2f58a 53 61 6d 70 6c 65 20 29 7b 0a 20 20 20 20 73 71  Sample ){.    sq
2f58b 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 4c 6f  lite3_value *pLo
2f58c 77 65 72 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20  werVal = 0;.    
2f58d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
2f58e 55 70 70 65 72 56 61 6c 20 3d 20 30 3b 0a 20 20  UpperVal = 0;.  
2f58f 20 20 69 6e 74 20 69 45 73 74 3b 0a 20 20 20 20    int iEst;.    
2f590 69 6e 74 20 69 4c 6f 77 65 72 20 3d 20 30 3b 0a  int iLower = 0;.
2f591 20 20 20 20 69 6e 74 20 69 55 70 70 65 72 20 3d      int iUpper =
2f592 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41   SQLITE_INDEX_SA
2f593 4d 50 4c 45 53 3b 0a 20 20 20 20 75 38 20 61 66  MPLES;.    u8 af
2f594 66 20 3d 20 70 2d 3e 70 54 61 62 6c 65 2d 3e 61  f = p->pTable->a
2f595 43 6f 6c 5b 70 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  Col[p->aiColumn[
2f596 30 5d 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 0a 20  0]].affinity;.. 
2f597 20 20 20 69 66 28 20 70 4c 6f 77 65 72 20 29 7b     if( pLower ){
2f598 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78  .      Expr *pEx
2f599 70 72 20 3d 20 70 4c 6f 77 65 72 2d 3e 70 45 78  pr = pLower->pEx
2f59a 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pr->pRight;.    
2f59b 20 20 72 63 20 3d 20 76 61 6c 75 65 46 72 6f 6d    rc = valueFrom
2f59c 45 78 70 72 28 70 50 61 72 73 65 2c 20 70 45 78  Expr(pParse, pEx
2f59d 70 72 2c 20 61 66 66 2c 20 26 70 4c 6f 77 65 72  pr, aff, &pLower
2f59e 56 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Val);.    }.    
2f59f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2f5a0 4b 20 26 26 20 70 55 70 70 65 72 20 29 7b 0a 20  K && pUpper ){. 
2f5a1 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72       Expr *pExpr
2f5a2 20 3d 20 70 55 70 70 65 72 2d 3e 70 45 78 70 72   = pUpper->pExpr
2f5a3 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ->pRight;.      
2f5a4 72 63 20 3d 20 76 61 6c 75 65 46 72 6f 6d 45 78  rc = valueFromEx
2f5a5 70 72 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  pr(pParse, pExpr
2f5a6 2c 20 61 66 66 2c 20 26 70 55 70 70 65 72 56 61  , aff, &pUpperVa
2f5a7 6c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  l);.    }..    i
2f5a8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2f5a9 20 7c 7c 20 28 70 4c 6f 77 65 72 56 61 6c 3d 3d   || (pLowerVal==
2f5aa 30 20 26 26 20 70 55 70 70 65 72 56 61 6c 3d 3d  0 && pUpperVal==
2f5ab 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  0) ){.      sqli
2f5ac 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 4c 6f  te3ValueFree(pLo
2f5ad 77 65 72 56 61 6c 29 3b 0a 20 20 20 20 20 20 73  werVal);.      s
2f5ae 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
2f5af 70 55 70 70 65 72 56 61 6c 29 3b 0a 20 20 20 20  pUpperVal);.    
2f5b0 20 20 67 6f 74 6f 20 72 61 6e 67 65 5f 65 73 74    goto range_est
2f5b1 5f 66 61 6c 6c 62 61 63 6b 3b 0a 20 20 20 20 7d  _fallback;.    }
2f5b2 65 6c 73 65 20 69 66 28 20 70 4c 6f 77 65 72 56  else if( pLowerV
2f5b3 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  al==0 ){.      r
2f5b4 63 20 3d 20 77 68 65 72 65 52 61 6e 67 65 52 65  c = whereRangeRe
2f5b5 67 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 2c 20  gion(pParse, p, 
2f5b6 70 55 70 70 65 72 56 61 6c 2c 20 26 69 55 70 70  pUpperVal, &iUpp
2f5b7 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  er);.      if( p
2f5b8 4c 6f 77 65 72 20 29 20 69 4c 6f 77 65 72 20 3d  Lower ) iLower =
2f5b9 20 69 55 70 70 65 72 2f 32 3b 0a 20 20 20 20 7d   iUpper/2;.    }
2f5ba 65 6c 73 65 20 69 66 28 20 70 55 70 70 65 72 56  else if( pUpperV
2f5bb 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  al==0 ){.      r
2f5bc 63 20 3d 20 77 68 65 72 65 52 61 6e 67 65 52 65  c = whereRangeRe
2f5bd 67 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 2c 20  gion(pParse, p, 
2f5be 70 4c 6f 77 65 72 56 61 6c 2c 20 26 69 4c 6f 77  pLowerVal, &iLow
2f5bf 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  er);.      if( p
2f5c0 55 70 70 65 72 20 29 20 69 55 70 70 65 72 20 3d  Upper ) iUpper =
2f5c1 20 28 69 4c 6f 77 65 72 20 2b 20 53 51 4c 49 54   (iLower + SQLIT
2f5c2 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20  E_INDEX_SAMPLES 
2f5c3 2b 20 31 29 2f 32 3b 0a 20 20 20 20 7d 65 6c 73  + 1)/2;.    }els
2f5c4 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 68  e{.      rc = wh
2f5c5 65 72 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 70  ereRangeRegion(p
2f5c6 50 61 72 73 65 2c 20 70 2c 20 70 55 70 70 65 72  Parse, p, pUpper
2f5c7 56 61 6c 2c 20 26 69 55 70 70 65 72 29 3b 0a 20  Val, &iUpper);. 
2f5c8 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
2f5c9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
2f5ca 20 20 72 63 20 3d 20 77 68 65 72 65 52 61 6e 67    rc = whereRang
2f5cb 65 52 65 67 69 6f 6e 28 70 50 61 72 73 65 2c 20  eRegion(pParse, 
2f5cc 70 2c 20 70 4c 6f 77 65 72 56 61 6c 2c 20 26 69  p, pLowerVal, &i
2f5cd 4c 6f 77 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  Lower);.      }.
2f5ce 20 20 20 20 7d 0a 0a 20 20 20 20 69 45 73 74 20      }..    iEst 
2f5cf 3d 20 69 55 70 70 65 72 20 2d 20 69 4c 6f 77 65  = iUpper - iLowe
2f5d0 72 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  r;.    testcase(
2f5d1 20 69 45 73 74 3d 3d 53 51 4c 49 54 45 5f 49 4e   iEst==SQLITE_IN
2f5d2 44 45 58 5f 53 41 4d 50 4c 45 53 20 29 3b 0a 20  DEX_SAMPLES );. 
2f5d3 20 20 20 61 73 73 65 72 74 28 20 69 45 73 74 3c     assert( iEst<
2f5d4 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41  =SQLITE_INDEX_SA
2f5d5 4d 50 4c 45 53 20 29 3b 0a 20 20 20 20 69 66 28  MPLES );.    if(
2f5d6 20 69 45 73 74 3c 31 20 29 7b 0a 20 20 20 20 20   iEst<1 ){.     
2f5d7 20 69 45 73 74 20 3d 20 31 3b 0a 20 20 20 20 7d   iEst = 1;.    }
2f5d8 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  ..    sqlite3Val
2f5d9 75 65 46 72 65 65 28 70 4c 6f 77 65 72 56 61 6c  ueFree(pLowerVal
2f5da 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61  );.    sqlite3Va
2f5db 6c 75 65 46 72 65 65 28 70 55 70 70 65 72 56 61  lueFree(pUpperVa
2f5dc 6c 29 3b 0a 20 20 20 20 2a 70 69 45 73 74 20 3d  l);.    *piEst =
2f5dd 20 28 69 45 73 74 20 2a 20 31 30 30 29 2f 53 51   (iEst * 100)/SQ
2f5de 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c  LITE_INDEX_SAMPL
2f5df 45 53 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  ES;.    return r
2f5e0 63 3b 0a 20 20 7d 0a 72 61 6e 67 65 5f 65 73 74  c;.  }.range_est
2f5e1 5f 66 61 6c 6c 62 61 63 6b 3a 0a 23 65 6c 73 65  _fallback:.#else
2f5e2 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
2f5e3 54 45 52 28 70 50 61 72 73 65 29 3b 0a 20 20 55  TER(pParse);.  U
2f5e4 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
2f5e5 70 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  p);.  UNUSED_PAR
2f5e6 41 4d 45 54 45 52 28 6e 45 71 29 3b 0a 23 65 6e  AMETER(nEq);.#en
2f5e7 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 4c  dif.  assert( pL
2f5e8 6f 77 65 72 20 7c 7c 20 70 55 70 70 65 72 20 29  ower || pUpper )
2f5e9 3b 0a 20 20 69 66 28 20 70 4c 6f 77 65 72 20 26  ;.  if( pLower &
2f5ea 26 20 70 55 70 70 65 72 20 29 7b 0a 20 20 20 20  & pUpper ){.    
2f5eb 2a 70 69 45 73 74 20 3d 20 31 31 3b 0a 20 20 7d  *piEst = 11;.  }
2f5ec 65 6c 73 65 7b 0a 20 20 20 20 2a 70 69 45 73 74  else{.    *piEst
2f5ed 20 3d 20 33 33 3b 0a 20 20 7d 0a 20 20 72 65 74   = 33;.  }.  ret
2f5ee 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
2f5ef 2a 20 46 69 6e 64 20 74 68 65 20 71 75 65 72 79  * Find the query
2f5f0 20 70 6c 61 6e 20 66 6f 72 20 61 63 63 65 73 73   plan for access
2f5f1 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72  ing a particular
2f5f2 20 74 61 62 6c 65 2e 20 20 57 72 69 74 65 20 74   table.  Write t
2f5f3 68 65 0a 2a 2a 20 62 65 73 74 20 71 75 65 72 79  he.** best query
2f5f4 20 70 6c 61 6e 20 61 6e 64 20 69 74 73 20 63 6f   plan and its co
2f5f5 73 74 20 69 6e 74 6f 20 74 68 65 20 57 68 65 72  st into the Wher
2f5f6 65 43 6f 73 74 20 6f 62 6a 65 63 74 20 73 75 70  eCost object sup
2f5f7 70 6c 69 65 64 20 61 73 20 74 68 65 0a 2a 2a 20  plied as the.** 
2f5f8 6c 61 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a  last parameter..
2f5f9 2a 2a 0a 2a 2a 20 54 68 65 20 6c 6f 77 65 73 74  **.** The lowest
2f5fa 20 63 6f 73 74 20 70 6c 61 6e 20 77 69 6e 73 2e   cost plan wins.
2f5fb 20 20 54 68 65 20 63 6f 73 74 20 69 73 20 61 6e    The cost is an
2f5fc 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65   estimate of the
2f5fd 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20 43 50   amount of.** CP
2f5fe 55 20 61 6e 64 20 64 69 73 6b 20 49 2f 4f 20 6e  U and disk I/O n
2f5ff 65 65 64 20 74 6f 20 70 72 6f 63 65 73 73 20 74  eed to process t
2f600 68 65 20 72 65 71 75 65 73 74 20 75 73 69 6e 67  he request using
2f601 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 70 6c   the selected pl
2f602 61 6e 2e 0a 2a 2a 20 46 61 63 74 6f 72 73 20 74  an..** Factors t
2f603 68 61 74 20 69 6e 66 6c 75 65 6e 63 65 20 63 6f  hat influence co
2f604 73 74 20 69 6e 63 6c 75 64 65 3a 0a 2a 2a 0a 2a  st include:.**.*
2f605 2a 20 20 20 20 2a 20 20 54 68 65 20 65 73 74 69  *    *  The esti
2f606 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  mated number of 
2f607 72 6f 77 73 20 74 68 61 74 20 77 69 6c 6c 20 62  rows that will b
2f608 65 20 72 65 74 72 69 65 76 65 64 2e 20 20 28 54  e retrieved.  (T
2f609 68 65 0a 2a 2a 20 20 20 20 20 20 20 66 65 77 65  he.**       fewe
2f60a 72 20 74 68 65 20 62 65 74 74 65 72 2e 29 0a 2a  r the better.).*
2f60b 2a 0a 2a 2a 20 20 20 20 2a 20 20 57 68 65 74 68  *.**    *  Wheth
2f60c 65 72 20 6f 72 20 6e 6f 74 20 73 6f 72 74 69 6e  er or not sortin
2f60d 67 20 6d 75 73 74 20 6f 63 63 75 72 2e 0a 2a 2a  g must occur..**
2f60e 0a 2a 2a 20 20 20 20 2a 20 20 57 68 65 74 68 65  .**    *  Whethe
2f60f 72 20 6f 72 20 6e 6f 74 20 74 68 65 72 65 20 6d  r or not there m
2f610 75 73 74 20 62 65 20 73 65 70 61 72 61 74 65 20  ust be separate 
2f611 6c 6f 6f 6b 75 70 73 20 69 6e 20 74 68 65 0a 2a  lookups in the.*
2f612 2a 20 20 20 20 20 20 20 69 6e 64 65 78 20 61 6e  *       index an
2f613 64 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 74 61  d in the main ta
2f614 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ble..**.** If th
2f615 65 72 65 20 77 61 73 20 61 6e 20 49 4e 44 45 58  ere was an INDEX
2f616 45 44 20 42 59 20 63 6c 61 75 73 65 20 28 70 53  ED BY clause (pS
2f617 72 63 2d 3e 70 49 6e 64 65 78 29 20 61 74 74 61  rc->pIndex) atta
2f618 63 68 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c  ched to the tabl
2f619 65 20 69 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 20  e in.** the SQL 
2f61a 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20  statement, then 
2f61b 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e  this function on
2f61c 6c 79 20 63 6f 6e 73 69 64 65 72 73 20 70 6c 61  ly considers pla
2f61d 6e 73 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a  ns using the .**
2f61e 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 49 66   named index. If
2f61f 20 6e 6f 20 73 75 63 68 20 70 6c 61 6e 20 69 73   no such plan is
2f620 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68 65   found, then the
2f621 20 72 65 74 75 72 6e 65 64 20 63 6f 73 74 20 69   returned cost i
2f622 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 49 47 5f  s.** SQLITE_BIG_
2f623 44 42 4c 2e 20 49 66 20 61 20 70 6c 61 6e 20 69  DBL. If a plan i
2f624 73 20 66 6f 75 6e 64 20 74 68 61 74 20 75 73 65  s found that use
2f625 73 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65  s the named inde
2f626 78 2c 20 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  x, .** then the 
2f627 63 6f 73 74 20 69 73 20 63 61 6c 63 75 6c 61 74  cost is calculat
2f628 65 64 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20  ed in the usual 
2f629 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  way..**.** If a 
2f62a 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75  NOT INDEXED clau
2f62b 73 65 20 28 70 53 72 63 2d 3e 6e 6f 74 49 6e 64  se (pSrc->notInd
2f62c 65 78 65 64 21 3d 30 29 20 77 61 73 20 61 74 74  exed!=0) was att
2f62d 61 63 68 65 64 20 74 6f 20 74 68 65 20 74 61 62  ached to the tab
2f62e 6c 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 53 45  le .** in the SE
2f62f 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20  LECT statement, 
2f630 74 68 65 6e 20 6e 6f 20 69 6e 64 65 78 65 73 20  then no indexes 
2f631 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 2e 20  are considered. 
2f632 48 6f 77 65 76 65 72 2c 20 74 68 65 20 0a 2a 2a  However, the .**
2f633 20 73 65 6c 65 63 74 65 64 20 70 6c 61 6e 20 6d   selected plan m
2f634 61 79 20 73 74 69 6c 6c 20 74 61 6b 65 20 61 64  ay still take ad
2f635 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 74  vantage of the t
2f636 61 62 6c 65 73 20 62 75 69 6c 74 2d 69 6e 20 72  ables built-in r
2f637 6f 77 69 64 0a 2a 2a 20 69 6e 64 65 78 2e 0a 2a  owid.** index..*
2f638 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65  /.static void be
2f639 73 74 42 74 72 65 65 49 6e 64 65 78 28 0a 20 20  stBtreeIndex(.  
2f63a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2f63b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2f63c 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
2f63d 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  xt */.  WhereCla
2f63e 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20  use *pWC,       
2f63f 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
2f640 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72   clause */.  str
2f641 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2f642 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65 20   *pSrc,  /* The 
2f643 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
2f644 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20 20   to search */.  
2f645 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
2f646 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ,           /* M
2f647 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20 74  ask of cursors t
2f648 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61 69  hat are not avai
2f649 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c  lable */.  ExprL
2f64a 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
2f64b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52         /* The OR
2f64c 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
2f64d 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a 70 43  .  WhereCost *pC
2f64e 6f 73 74 20 20 20 20 20 20 20 20 20 20 20 20 2f  ost            /
2f64f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74 20 71 75  * Lowest cost qu
2f650 65 72 79 20 70 6c 61 6e 20 2a 2f 0a 29 7b 0a 20  ery plan */.){. 
2f651 20 69 6e 74 20 69 43 75 72 20 3d 20 70 53 72 63   int iCur = pSrc
2f652 2d 3e 69 43 75 72 73 6f 72 3b 20 20 20 2f 2a 20  ->iCursor;   /* 
2f653 54 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68  The cursor of th
2f654 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63  e table to be ac
2f655 63 65 73 73 65 64 20 2a 2f 0a 20 20 49 6e 64 65  cessed */.  Inde
2f656 78 20 2a 70 50 72 6f 62 65 3b 20 20 20 20 20 20  x *pProbe;      
2f657 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e          /* An in
2f658 64 65 78 20 77 65 20 61 72 65 20 65 76 61 6c 75  dex we are evalu
2f659 61 74 69 6e 67 20 2a 2f 0a 20 20 49 6e 64 65 78  ating */.  Index
2f65a 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
2f65b 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
2f65c 66 20 70 50 72 6f 62 65 2c 20 6f 72 20 7a 65 72  f pProbe, or zer
2f65d 6f 20 66 6f 72 20 49 50 4b 20 69 6e 64 65 78 20  o for IPK index 
2f65e 2a 2f 0a 20 20 69 6e 74 20 65 71 54 65 72 6d 4d  */.  int eqTermM
2f65f 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ask;            
2f660 20 2f 2a 20 43 75 72 72 65 6e 74 20 6d 61 73 6b   /* Current mask
2f661 20 6f 66 20 76 61 6c 69 64 20 65 71 75 61 6c 69   of valid equali
2f662 74 79 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f 0a  ty operators */.
2f663 20 20 69 6e 74 20 69 64 78 45 71 54 65 72 6d 4d    int idxEqTermM
2f664 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ask;          /*
2f665 20 49 6e 64 65 78 20 6d 61 73 6b 20 6f 66 20 76   Index mask of v
2f666 61 6c 69 64 20 65 71 75 61 6c 69 74 79 20 6f 70  alid equality op
2f667 65 72 61 74 6f 72 73 20 2a 2f 0a 20 20 49 6e 64  erators */.  Ind
2f668 65 78 20 73 50 6b 3b 20 20 20 20 20 20 20 20 20  ex sPk;         
2f669 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 66 61           /* A fa
2f66a 6b 65 20 69 6e 64 65 78 20 6f 62 6a 65 63 74 20  ke index object 
2f66b 66 6f 72 20 74 68 65 20 70 72 69 6d 61 72 79 20  for the primary 
2f66c 6b 65 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  key */.  unsigne
2f66d 64 20 69 6e 74 20 61 69 52 6f 77 45 73 74 50 6b  d int aiRowEstPk
2f66e 5b 32 5d 3b 20 2f 2a 20 54 68 65 20 61 69 52 6f  [2]; /* The aiRo
2f66f 77 45 73 74 5b 5d 20 76 61 6c 75 65 20 66 6f 72  wEst[] value for
2f670 20 74 68 65 20 73 50 6b 20 69 6e 64 65 78 20 2a   the sPk index *
2f671 2f 0a 20 20 69 6e 74 20 61 69 43 6f 6c 75 6d 6e  /.  int aiColumn
2f672 50 6b 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20  Pk = -1;        
2f673 2f 2a 20 54 68 65 20 61 43 6f 6c 75 6d 6e 5b 5d  /* The aColumn[]
2f674 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73   value for the s
2f675 50 6b 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  Pk index */.  in
2f676 74 20 77 73 46 6c 61 67 4d 61 73 6b 3b 20 20 20  t wsFlagMask;   
2f677 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c            /* All
2f678 6f 77 65 64 20 66 6c 61 67 73 20 69 6e 20 70 43  owed flags in pC
2f679 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  ost->plan.wsFlag
2f67a 20 2a 2f 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61   */..  /* Initia
2f67b 6c 69 7a 65 20 74 68 65 20 63 6f 73 74 20 74 6f  lize the cost to
2f67c 20 61 20 77 6f 72 73 74 2d 63 61 73 65 20 76 61   a worst-case va
2f67d 6c 75 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  lue */.  memset(
2f67e 70 43 6f 73 74 2c 20 30 2c 20 73 69 7a 65 6f 66  pCost, 0, sizeof
2f67f 28 2a 70 43 6f 73 74 29 29 3b 0a 20 20 70 43 6f  (*pCost));.  pCo
2f680 73 74 2d 3e 72 43 6f 73 74 20 3d 20 53 51 4c 49  st->rCost = SQLI
2f681 54 45 5f 42 49 47 5f 44 42 4c 3b 0a 0a 20 20 2f  TE_BIG_DBL;..  /
2f682 2a 20 49 66 20 74 68 65 20 70 53 72 63 20 74 61  * If the pSrc ta
2f683 62 6c 65 20 69 73 20 74 68 65 20 72 69 67 68 74  ble is the right
2f684 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54   table of a LEFT
2f685 20 4a 4f 49 4e 20 74 68 65 6e 20 77 65 20 6d 61   JOIN then we ma
2f686 79 20 6e 6f 74 0a 20 20 2a 2a 20 75 73 65 20 61  y not.  ** use a
2f687 6e 20 69 6e 64 65 78 20 74 6f 20 73 61 74 69 73  n index to satis
2f688 66 79 20 49 53 20 4e 55 4c 4c 20 63 6f 6e 73 74  fy IS NULL const
2f689 72 61 69 6e 74 73 20 6f 6e 20 74 68 61 74 20 74  raints on that t
2f68a 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 20  able.  This is. 
2f68b 20 2a 2a 20 62 65 63 61 75 73 65 20 63 6f 6c 75   ** because colu
2f68c 6d 6e 73 20 6d 69 67 68 74 20 65 6e 64 20 75 70  mns might end up
2f68d 20 62 65 69 6e 67 20 4e 55 4c 4c 20 69 66 20 74   being NULL if t
2f68e 68 65 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f  he table does no
2f68f 74 20 6d 61 74 63 68 20 2d 0a 20 20 2a 2a 20 61  t match -.  ** a
2f690 20 63 69 72 63 75 6d 73 74 61 6e 63 65 20 77 68   circumstance wh
2f691 69 63 68 20 74 68 65 20 69 6e 64 65 78 20 63 61  ich the index ca
2f692 6e 6e 6f 74 20 68 65 6c 70 20 75 73 20 64 69 73  nnot help us dis
2f693 63 6f 76 65 72 2e 20 20 54 69 63 6b 65 74 20 23  cover.  Ticket #
2f694 32 31 37 37 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  2177..  */.  if(
2f695 20 70 53 72 63 2d 3e 6a 6f 69 6e 74 79 70 65 20   pSrc->jointype 
2f696 26 20 4a 54 5f 4c 45 46 54 20 29 7b 0a 20 20 20  & JT_LEFT ){.   
2f697 20 69 64 78 45 71 54 65 72 6d 4d 61 73 6b 20 3d   idxEqTermMask =
2f698 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 3b 0a 20 20   WO_EQ|WO_IN;.  
2f699 7d 65 6c 73 65 7b 0a 20 20 20 20 69 64 78 45 71  }else{.    idxEq
2f69a 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51  TermMask = WO_EQ
2f69b 7c 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c  |WO_IN|WO_ISNULL
2f69c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 53 72  ;.  }..  if( pSr
2f69d 63 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  c->pIndex ){.   
2f69e 20 2f 2a 20 41 6e 20 49 4e 44 45 58 45 44 20 42   /* An INDEXED B
2f69f 59 20 63 6c 61 75 73 65 20 73 70 65 63 69 66 69  Y clause specifi
2f6a0 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  es a particular 
2f6a1 69 6e 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a  index to use */.
2f6a2 20 20 20 20 70 49 64 78 20 3d 20 70 50 72 6f 62      pIdx = pProb
2f6a3 65 20 3d 20 70 53 72 63 2d 3e 70 49 6e 64 65 78  e = pSrc->pIndex
2f6a4 3b 0a 20 20 20 20 77 73 46 6c 61 67 4d 61 73 6b  ;.    wsFlagMask
2f6a5 20 3d 20 7e 28 57 48 45 52 45 5f 52 4f 57 49 44   = ~(WHERE_ROWID
2f6a6 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f  _EQ|WHERE_ROWID_
2f6a7 52 41 4e 47 45 29 3b 0a 20 20 20 20 65 71 54 65  RANGE);.    eqTe
2f6a8 72 6d 4d 61 73 6b 20 3d 20 69 64 78 45 71 54 65  rmMask = idxEqTe
2f6a9 72 6d 4d 61 73 6b 3b 0a 20 20 7d 65 6c 73 65 7b  rmMask;.  }else{
2f6aa 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73  .    /* There is
2f6ab 20 6e 6f 20 49 4e 44 45 58 45 44 20 42 59 20 63   no INDEXED BY c
2f6ac 6c 61 75 73 65 2e 20 20 43 72 65 61 74 65 20 61  lause.  Create a
2f6ad 20 66 61 6b 65 20 49 6e 64 65 78 20 6f 62 6a 65   fake Index obje
2f6ae 63 74 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 70  ct to.    ** rep
2f6af 72 65 73 65 6e 74 20 74 68 65 20 70 72 69 6d 61  resent the prima
2f6b0 72 79 20 6b 65 79 20 2a 2f 0a 20 20 20 20 49 6e  ry key */.    In
2f6b1 64 65 78 20 2a 70 46 69 72 73 74 3b 20 20 20 20  dex *pFirst;    
2f6b2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2f6b3 6e 79 20 6f 74 68 65 72 20 69 6e 64 65 78 20 6f  ny other index o
2f6b4 6e 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  n the table */. 
2f6b5 20 20 20 6d 65 6d 73 65 74 28 26 73 50 6b 2c 20     memset(&sPk, 
2f6b6 30 2c 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 29  0, sizeof(Index)
2f6b7 29 3b 0a 20 20 20 20 73 50 6b 2e 6e 43 6f 6c 75  );.    sPk.nColu
2f6b8 6d 6e 20 3d 20 31 3b 0a 20 20 20 20 73 50 6b 2e  mn = 1;.    sPk.
2f6b9 61 69 43 6f 6c 75 6d 6e 20 3d 20 26 61 69 43 6f  aiColumn = &aiCo
2f6ba 6c 75 6d 6e 50 6b 3b 0a 20 20 20 20 73 50 6b 2e  lumnPk;.    sPk.
2f6bb 61 69 52 6f 77 45 73 74 20 3d 20 61 69 52 6f 77  aiRowEst = aiRow
2f6bc 45 73 74 50 6b 3b 0a 20 20 20 20 61 69 52 6f 77  EstPk;.    aiRow
2f6bd 45 73 74 50 6b 5b 31 5d 20 3d 20 31 3b 0a 20 20  EstPk[1] = 1;.  
2f6be 20 20 73 50 6b 2e 6f 6e 45 72 72 6f 72 20 3d 20    sPk.onError = 
2f6bf 4f 45 5f 52 65 70 6c 61 63 65 3b 0a 20 20 20 20  OE_Replace;.    
2f6c0 73 50 6b 2e 70 54 61 62 6c 65 20 3d 20 70 53 72  sPk.pTable = pSr
2f6c1 63 2d 3e 70 54 61 62 3b 0a 20 20 20 20 70 46 69  c->pTab;.    pFi
2f6c2 72 73 74 20 3d 20 70 53 72 63 2d 3e 70 54 61 62  rst = pSrc->pTab
2f6c3 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 69 66  ->pIndex;.    if
2f6c4 28 20 70 53 72 63 2d 3e 6e 6f 74 49 6e 64 65 78  ( pSrc->notIndex
2f6c5 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ed==0 ){.      s
2f6c6 50 6b 2e 70 4e 65 78 74 20 3d 20 70 46 69 72 73  Pk.pNext = pFirs
2f6c7 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20  t;.    }.    /* 
2f6c8 54 68 65 20 61 69 52 6f 77 45 73 74 50 6b 5b 30  The aiRowEstPk[0
2f6c9 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65  ] is an estimate
2f6ca 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 6e 75   of the total nu
2f6cb 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
2f6cc 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65  the.    ** table
2f6cd 2e 20 20 47 65 74 20 74 68 69 73 20 69 6e 66 6f  .  Get this info
2f6ce 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65  rmation from the
2f6cf 20 41 4e 41 4c 59 5a 45 20 69 6e 66 6f 72 6d 61   ANALYZE informa
2f6d0 74 69 6f 6e 20 69 66 20 69 74 20 69 73 0a 20 20  tion if it is.  
2f6d1 20 20 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2e 20    ** available. 
2f6d2 20 49 66 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c   If not availabl
2f6d3 65 2c 20 61 73 73 75 6d 65 20 74 68 65 20 74 61  e, assume the ta
2f6d4 62 6c 65 20 31 20 6d 69 6c 6c 69 6f 6e 20 72 6f  ble 1 million ro
2f6d5 77 73 20 69 6e 20 73 69 7a 65 2e 0a 20 20 20 20  ws in size..    
2f6d6 2a 2f 0a 20 20 20 20 69 66 28 20 70 46 69 72 73  */.    if( pFirs
2f6d7 74 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t ){.      asser
2f6d8 74 28 20 70 46 69 72 73 74 2d 3e 61 69 52 6f 77  t( pFirst->aiRow
2f6d9 45 73 74 21 3d 30 20 29 3b 20 2f 2a 20 41 6c 6c  Est!=0 ); /* All
2f6da 6f 63 61 74 65 64 20 74 6f 67 65 74 68 65 72 20  ocated together 
2f6db 77 69 74 68 20 70 46 69 72 73 74 20 2a 2f 0a 20  with pFirst */. 
2f6dc 20 20 20 20 20 61 69 52 6f 77 45 73 74 50 6b 5b       aiRowEstPk[
2f6dd 30 5d 20 3d 20 70 46 69 72 73 74 2d 3e 61 69 52  0] = pFirst->aiR
2f6de 6f 77 45 73 74 5b 30 5d 3b 0a 20 20 20 20 7d 65  owEst[0];.    }e
2f6df 6c 73 65 7b 0a 20 20 20 20 20 20 61 69 52 6f 77  lse{.      aiRow
2f6e0 45 73 74 50 6b 5b 30 5d 20 3d 20 31 30 30 30 30  EstPk[0] = 10000
2f6e1 30 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  00;.    }.    pP
2f6e2 72 6f 62 65 20 3d 20 26 73 50 6b 3b 0a 20 20 20  robe = &sPk;.   
2f6e3 20 77 73 46 6c 61 67 4d 61 73 6b 20 3d 20 7e 28   wsFlagMask = ~(
2f6e4 0a 20 20 20 20 20 20 20 20 57 48 45 52 45 5f 43  .        WHERE_C
2f6e5 4f 4c 55 4d 4e 5f 49 4e 7c 57 48 45 52 45 5f 43  OLUMN_IN|WHERE_C
2f6e6 4f 4c 55 4d 4e 5f 45 51 7c 57 48 45 52 45 5f 43  OLUMN_EQ|WHERE_C
2f6e7 4f 4c 55 4d 4e 5f 4e 55 4c 4c 7c 57 48 45 52 45  OLUMN_NULL|WHERE
2f6e8 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 0a 20 20  _COLUMN_RANGE.  
2f6e9 20 20 29 3b 0a 20 20 20 20 65 71 54 65 72 6d 4d    );.    eqTermM
2f6ea 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49  ask = WO_EQ|WO_I
2f6eb 4e 3b 0a 20 20 20 20 70 49 64 78 20 3d 20 30 3b  N;.    pIdx = 0;
2f6ec 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20  .  }..  /* Loop 
2f6ed 6f 76 65 72 20 61 6c 6c 20 69 6e 64 69 63 65 73  over all indices
2f6ee 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 74 68 65   looking for the
2f6ef 20 62 65 73 74 20 6f 6e 65 20 74 6f 20 75 73 65   best one to use
2f6f0 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 3b 20 70 50  .  */.  for(; pP
2f6f1 72 6f 62 65 3b 20 70 49 64 78 3d 70 50 72 6f 62  robe; pIdx=pProb
2f6f2 65 3d 70 50 72 6f 62 65 2d 3e 70 4e 65 78 74 29  e=pProbe->pNext)
2f6f3 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69  {.    const unsi
2f6f4 67 6e 65 64 20 69 6e 74 20 2a 20 63 6f 6e 73 74  gned int * const
2f6f5 20 61 69 52 6f 77 45 73 74 20 3d 20 70 50 72 6f   aiRowEst = pPro
2f6f6 62 65 2d 3e 61 69 52 6f 77 45 73 74 3b 0a 20 20  be->aiRowEst;.  
2f6f7 20 20 64 6f 75 62 6c 65 20 63 6f 73 74 3b 20 20    double cost;  
2f6f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2f6f9 20 43 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 70   Cost of using p
2f6fa 50 72 6f 62 65 20 2a 2f 0a 20 20 20 20 64 6f 75  Probe */.    dou
2f6fb 62 6c 65 20 6e 52 6f 77 3b 20 20 20 20 20 20 20  ble nRow;       
2f6fc 20 20 20 20 20 20 20 20 20 2f 2a 20 45 73 74 69           /* Esti
2f6fd 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  mated number of 
2f6fe 72 6f 77 73 20 69 6e 20 72 65 73 75 6c 74 20 73  rows in result s
2f6ff 65 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65  et */.    int re
2f700 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
2f701 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
2f702 20 73 63 61 6e 20 69 6e 20 72 65 76 65 72 73 65   scan in reverse
2f703 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 69 6e   order */.    in
2f704 74 20 77 73 46 6c 61 67 73 20 3d 20 30 3b 0a 20  t wsFlags = 0;. 
2f705 20 20 20 42 69 74 6d 61 73 6b 20 75 73 65 64 20     Bitmask used 
2f706 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  = 0;..    /* The
2f707 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61   following varia
2f708 62 6c 65 73 20 61 72 65 20 70 6f 70 75 6c 61 74  bles are populat
2f709 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  ed based on the 
2f70a 70 72 6f 70 65 72 74 69 65 73 20 6f 66 0a 20 20  properties of.  
2f70b 20 20 2a 2a 20 73 63 61 6e 20 62 65 69 6e 67 20    ** scan being 
2f70c 65 76 61 6c 75 61 74 65 64 2e 20 54 68 65 79 20  evaluated. They 
2f70d 61 72 65 20 74 68 65 6e 20 75 73 65 64 20 74 6f  are then used to
2f70e 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 65   determine the e
2f70f 78 70 65 63 74 65 64 0a 20 20 20 20 2a 2a 20 63  xpected.    ** c
2f710 6f 73 74 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f  ost and number o
2f711 66 20 72 6f 77 73 20 72 65 74 75 72 6e 65 64 2e  f rows returned.
2f712 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2f713 6e 45 71 3a 20 0a 20 20 20 20 2a 2a 20 20 20 20  nEq: .    **    
2f714 4e 75 6d 62 65 72 20 6f 66 20 65 71 75 61 6c 69  Number of equali
2f715 74 79 20 74 65 72 6d 73 20 74 68 61 74 20 63 61  ty terms that ca
2f716 6e 20 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64  n be implemented
2f717 20 75 73 69 6e 67 20 74 68 65 20 69 6e 64 65 78   using the index
2f718 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2f719 20 6e 49 6e 4d 75 6c 3a 20 20 0a 20 20 20 20 2a   nInMul:  .    *
2f71a 2a 20 20 20 20 54 68 65 20 22 69 6e 2d 6d 75 6c  *    The "in-mul
2f71b 74 69 70 6c 69 65 72 22 2e 20 54 68 69 73 20 69  tiplier". This i
2f71c 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66  s an estimate of
2f71d 20 68 6f 77 20 6d 61 6e 79 20 73 65 65 6b 20 6f   how many seek o
2f71e 70 65 72 61 74 69 6f 6e 73 20 0a 20 20 20 20 2a  perations .    *
2f71f 2a 20 20 20 20 53 51 4c 69 74 65 20 6d 75 73 74  *    SQLite must
2f720 20 70 65 72 66 6f 72 6d 20 6f 6e 20 74 68 65 20   perform on the 
2f721 69 6e 64 65 78 20 69 6e 20 71 75 65 73 74 69 6f  index in questio
2f722 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  n. For example, 
2f723 69 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20  if the .    **  
2f724 20 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69    WHERE clause i
2f725 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s:.    **.    **
2f726 20 20 20 20 20 20 57 48 45 52 45 20 61 20 49 4e        WHERE a IN
2f727 20 28 31 2c 20 32 2c 20 33 29 20 41 4e 44 20 62   (1, 2, 3) AND b
2f728 20 49 4e 20 28 34 2c 20 35 2c 20 36 29 0a 20 20   IN (4, 5, 6).  
2f729 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 53    **.    **    S
2f72a 51 4c 69 74 65 20 6d 75 73 74 20 70 65 72 66 6f  QLite must perfo
2f72b 72 6d 20 39 20 6c 6f 6f 6b 75 70 73 20 6f 6e 20  rm 9 lookups on 
2f72c 61 6e 20 69 6e 64 65 78 20 6f 6e 20 28 61 2c 20  an index on (a, 
2f72d 62 29 2c 20 73 6f 20 6e 49 6e 4d 75 6c 20 69 73  b), so nInMul is
2f72e 20 0a 20 20 20 20 2a 2a 20 20 20 20 73 65 74 20   .    **    set 
2f72f 74 6f 20 39 2e 20 47 69 76 65 6e 20 74 68 65 20  to 9. Given the 
2f730 73 61 6d 65 20 73 63 68 65 6d 61 20 61 6e 64 20  same schema and 
2f731 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f  either of the fo
2f732 6c 6c 6f 77 69 6e 67 20 57 48 45 52 45 20 0a 20  llowing WHERE . 
2f733 20 20 20 2a 2a 20 20 20 20 63 6c 61 75 73 65 73     **    clauses
2f734 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
2f735 20 20 20 20 20 57 48 45 52 45 20 61 20 3d 20 20       WHERE a =  
2f736 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 57 48  1.    **      WH
2f737 45 52 45 20 61 20 3e 3d 20 32 0a 20 20 20 20 2a  ERE a >= 2.    *
2f738 2a 0a 20 20 20 20 2a 2a 20 20 20 20 6e 49 6e 4d  *.    **    nInM
2f739 75 6c 20 69 73 20 73 65 74 20 74 6f 20 31 2e 0a  ul is set to 1..
2f73a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2f73b 20 49 66 20 74 68 65 72 65 20 65 78 69 73 74 73   If there exists
2f73c 20 61 20 57 48 45 52 45 20 74 65 72 6d 20 6f 66   a WHERE term of
2f73d 20 74 68 65 20 66 6f 72 6d 20 22 78 20 49 4e 20   the form "x IN 
2f73e 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2c 20 74  (SELECT ...)", t
2f73f 68 65 6e 20 0a 20 20 20 20 2a 2a 20 20 20 20 74  hen .    **    t
2f740 68 65 20 73 75 62 2d 73 65 6c 65 63 74 20 69 73  he sub-select is
2f741 20 61 73 73 75 6d 65 64 20 74 6f 20 72 65 74 75   assumed to retu
2f742 72 6e 20 32 35 20 72 6f 77 73 20 66 6f 72 20 74  rn 25 rows for t
2f743 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 0a  he purposes of .
2f744 20 20 20 20 2a 2a 20 20 20 20 64 65 74 65 72 6d      **    determ
2f745 69 6e 69 6e 67 20 6e 49 6e 4d 75 6c 2e 0a 20 20  ining nInMul..  
2f746 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 62 49 6e    **.    **  bIn
2f747 45 73 74 3a 20 20 0a 20 20 20 20 2a 2a 20 20 20  Est:  .    **   
2f748 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 66 20   Set to true if 
2f749 74 68 65 72 65 20 77 61 73 20 61 74 20 6c 65 61  there was at lea
2f74a 73 74 20 6f 6e 65 20 22 78 20 49 4e 20 28 53 45  st one "x IN (SE
2f74b 4c 45 43 54 20 2e 2e 2e 29 22 20 74 65 72 6d 20  LECT ...)" term 
2f74c 75 73 65 64 20 0a 20 20 20 20 2a 2a 20 20 20 20  used .    **    
2f74d 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 74  in determining t
2f74e 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 49 6e 4d  he value of nInM
2f74f 75 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ul..    **.    *
2f750 2a 20 20 6e 42 6f 75 6e 64 3a 0a 20 20 20 20 2a  *  nBound:.    *
2f751 2a 20 20 20 20 41 6e 20 65 73 74 69 6d 61 74 65  *    An estimate
2f752 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f   on the amount o
2f753 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
2f754 20 6d 75 73 74 20 62 65 20 73 65 61 72 63 68 65   must be searche
2f755 64 2e 20 20 41 0a 20 20 20 20 2a 2a 20 20 20 20  d.  A.    **    
2f756 76 61 6c 75 65 20 6f 66 20 31 30 30 20 6d 65 61  value of 100 mea
2f757 6e 73 20 74 68 65 20 65 6e 74 69 72 65 20 74 61  ns the entire ta
2f758 62 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 2e  ble is searched.
2f759 20 20 52 61 6e 67 65 20 63 6f 6e 73 74 72 61 69    Range constrai
2f75a 6e 74 73 0a 20 20 20 20 2a 2a 20 20 20 20 6d 69  nts.    **    mi
2f75b 67 68 74 20 72 65 64 75 63 65 20 74 68 69 73 20  ght reduce this 
2f75c 74 6f 20 61 20 76 61 6c 75 65 20 6c 65 73 73 20  to a value less 
2f75d 74 68 61 6e 20 31 30 30 20 74 6f 20 69 6e 64 69  than 100 to indi
2f75e 63 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 0a 20  cate that only. 
2f75f 20 20 20 2a 2a 20 20 20 20 61 20 66 72 61 63 74     **    a fract
2f760 69 6f 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ion of the table
2f761 20 6e 65 65 64 73 20 73 65 61 72 63 68 69 6e 67   needs searching
2f762 2e 20 20 49 6e 20 74 68 65 20 61 62 73 65 6e 63  .  In the absenc
2f763 65 20 6f 66 0a 20 20 20 20 2a 2a 20 20 20 20 73  e of.    **    s
2f764 71 6c 69 74 65 5f 73 74 61 74 32 20 41 4e 41 4c  qlite_stat2 ANAL
2f765 59 5a 45 20 64 61 74 61 2c 20 61 20 73 69 6e 67  YZE data, a sing
2f766 6c 65 20 69 6e 65 71 75 61 6c 69 74 79 20 72 65  le inequality re
2f767 64 75 63 65 73 20 74 68 65 20 73 65 61 72 63 68  duces the search
2f768 0a 20 20 20 20 2a 2a 20 20 20 20 73 70 61 63 65  .    **    space
2f769 20 74 6f 20 31 2f 33 72 64 20 69 74 73 20 6f 72   to 1/3rd its or
2f76a 69 67 69 6e 61 6c 20 73 69 7a 65 2e 20 20 53 6f  iginal size.  So
2f76b 20 61 6e 20 78 3e 3f 20 63 6f 6e 73 74 72 61 69   an x>? constrai
2f76c 6e 74 20 72 65 64 75 63 65 73 0a 20 20 20 20 2a  nt reduces.    *
2f76d 2a 20 20 20 20 6e 42 6f 75 6e 64 20 74 6f 20 33  *    nBound to 3
2f76e 33 2e 20 20 54 77 6f 20 63 6f 6e 73 74 72 61 69  3.  Two constrai
2f76f 6e 74 73 20 28 78 3e 3f 20 41 4e 44 20 78 3c 3f  nts (x>? AND x<?
2f770 29 20 72 65 64 75 63 65 20 6e 42 6f 75 6e 64 20  ) reduce nBound 
2f771 74 6f 20 31 31 2e 0a 20 20 20 20 2a 2a 0a 20 20  to 11..    **.  
2f772 20 20 2a 2a 20 20 62 53 6f 72 74 3a 20 20 20 0a    **  bSort:   .
2f773 20 20 20 20 2a 2a 20 20 20 20 42 6f 6f 6c 65 61      **    Boolea
2f774 6e 2e 20 54 72 75 65 20 69 66 20 74 68 65 72 65  n. True if there
2f775 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20   is an ORDER BY 
2f776 63 6c 61 75 73 65 20 74 68 61 74 20 77 69 6c 6c  clause that will
2f777 20 72 65 71 75 69 72 65 20 61 6e 20 0a 20 20 20   require an .   
2f778 20 2a 2a 20 20 20 20 65 78 74 65 72 6e 61 6c 20   **    external 
2f779 73 6f 72 74 20 28 69 2e 65 2e 20 73 63 61 6e 6e  sort (i.e. scann
2f77a 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20 62 65  ing the index be
2f77b 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 77 69  ing evaluated wi
2f77c 6c 6c 20 6e 6f 74 20 0a 20 20 20 20 2a 2a 20 20  ll not .    **  
2f77d 20 20 63 6f 72 72 65 63 74 6c 79 20 6f 72 64 65    correctly orde
2f77e 72 20 72 65 63 6f 72 64 73 29 2e 0a 20 20 20 20  r records)..    
2f77f 2a 2a 0a 20 20 20 20 2a 2a 20 20 62 4c 6f 6f 6b  **.    **  bLook
2f780 75 70 3a 20 0a 20 20 20 20 2a 2a 20 20 20 20 42  up: .    **    B
2f781 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66 20  oolean. True if 
2f782 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 65  for each index e
2f783 6e 74 72 79 20 76 69 73 69 74 65 64 20 61 20 6c  ntry visited a l
2f784 6f 6f 6b 75 70 20 6f 6e 20 74 68 65 20 0a 20 20  ookup on the .  
2f785 20 20 2a 2a 20 20 20 20 63 6f 72 72 65 73 70 6f    **    correspo
2f786 6e 64 69 6e 67 20 74 61 62 6c 65 20 62 2d 74 72  nding table b-tr
2f787 65 65 20 69 73 20 72 65 71 75 69 72 65 64 2e 20  ee is required. 
2f788 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 66  This is always f
2f789 61 6c 73 65 20 0a 20 20 20 20 2a 2a 20 20 20 20  alse .    **    
2f78a 66 6f 72 20 74 68 65 20 72 6f 77 69 64 20 69 6e  for the rowid in
2f78b 64 65 78 2e 20 46 6f 72 20 6f 74 68 65 72 20 69  dex. For other i
2f78c 6e 64 65 78 65 73 2c 20 69 74 20 69 73 20 74 72  ndexes, it is tr
2f78d 75 65 20 75 6e 6c 65 73 73 20 61 6c 6c 20 74 68  ue unless all th
2f78e 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 63 6f 6c  e .    **    col
2f78f 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c  umns of the tabl
2f790 65 20 75 73 65 64 20 62 79 20 74 68 65 20 53 45  e used by the SE
2f791 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61  LECT statement a
2f792 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 0a 20  re present in . 
2f793 20 20 20 2a 2a 20 20 20 20 74 68 65 20 69 6e 64     **    the ind
2f794 65 78 20 28 73 75 63 68 20 61 6e 20 69 6e 64 65  ex (such an inde
2f795 78 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 20 64  x is sometimes d
2f796 65 73 63 72 69 62 65 64 20 61 73 20 61 20 63 6f  escribed as a co
2f797 76 65 72 69 6e 67 20 69 6e 64 65 78 29 2e 0a 20  vering index).. 
2f798 20 20 20 2a 2a 20 20 20 20 46 6f 72 20 65 78 61     **    For exa
2f799 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74 68 65 20  mple, given the 
2f79a 69 6e 64 65 78 20 6f 6e 20 28 61 2c 20 62 29 2c  index on (a, b),
2f79b 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 66 20 74   the second of t
2f79c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 20 20  he following .  
2f79d 20 20 2a 2a 20 20 20 20 74 77 6f 20 71 75 65 72    **    two quer
2f79e 69 65 73 20 72 65 71 75 69 72 65 73 20 74 61 62  ies requires tab
2f79f 6c 65 20 62 2d 74 72 65 65 20 6c 6f 6f 6b 75 70  le b-tree lookup
2f7a0 73 2c 20 62 75 74 20 74 68 65 20 66 69 72 73 74  s, but the first
2f7a1 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20 20 20 2a   does not..    *
2f7a2 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
2f7a3 20 20 20 20 20 53 45 4c 45 43 54 20 61 2c 20 62       SELECT a, b
2f7a4 20 20 20 20 46 52 4f 4d 20 74 62 6c 20 57 48 45      FROM tbl WHE
2f7a5 52 45 20 61 20 3d 20 31 3b 0a 20 20 20 20 2a 2a  RE a = 1;.    **
2f7a6 20 20 20 20 20 20 20 20 20 20 20 20 20 53 45 4c               SEL
2f7a7 45 43 54 20 61 2c 20 62 2c 20 63 20 46 52 4f 4d  ECT a, b, c FROM
2f7a8 20 74 62 6c 20 57 48 45 52 45 20 61 20 3d 20 31   tbl WHERE a = 1
2f7a9 3b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ;.    */.    int
2f7aa 20 6e 45 71 3b 0a 20 20 20 20 69 6e 74 20 62 49   nEq;.    int bI
2f7ab 6e 45 73 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e  nEst = 0;.    in
2f7ac 74 20 6e 49 6e 4d 75 6c 20 3d 20 31 3b 0a 20 20  t nInMul = 1;.  
2f7ad 20 20 69 6e 74 20 6e 42 6f 75 6e 64 20 3d 20 31    int nBound = 1
2f7ae 30 30 3b 0a 20 20 20 20 69 6e 74 20 62 53 6f 72  00;.    int bSor
2f7af 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 62  t = 0;.    int b
2f7b0 4c 6f 6f 6b 75 70 20 3d 20 30 3b 0a 0a 20 20 20  Lookup = 0;..   
2f7b1 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68   /* Determine th
2f7b2 65 20 76 61 6c 75 65 73 20 6f 66 20 6e 45 71 20  e values of nEq 
2f7b3 61 6e 64 20 6e 49 6e 4d 75 6c 20 2a 2f 0a 20 20  and nInMul */.  
2f7b4 20 20 66 6f 72 28 6e 45 71 3d 30 3b 20 6e 45 71    for(nEq=0; nEq
2f7b5 3c 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e  <pProbe->nColumn
2f7b6 3b 20 6e 45 71 2b 2b 29 7b 0a 20 20 20 20 20 20  ; nEq++){.      
2f7b7 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
2f7b8 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  ;           /* A
2f7b9 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 6f 66 20   single term of 
2f7ba 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2f7bb 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 20   */.      int j 
2f7bc 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75  = pProbe->aiColu
2f7bd 6d 6e 5b 6e 45 71 5d 3b 0a 20 20 20 20 20 20 70  mn[nEq];.      p
2f7be 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28  Term = findTerm(
2f7bf 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f  pWC, iCur, j, no
2f7c0 74 52 65 61 64 79 2c 20 65 71 54 65 72 6d 4d 61  tReady, eqTermMa
2f7c1 73 6b 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  sk, pIdx);.     
2f7c2 20 69 66 28 20 70 54 65 72 6d 3d 3d 30 20 29 20   if( pTerm==0 ) 
2f7c3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 77 73 46  break;.      wsF
2f7c4 6c 61 67 73 20 7c 3d 20 28 57 48 45 52 45 5f 43  lags |= (WHERE_C
2f7c5 4f 4c 55 4d 4e 5f 45 51 7c 57 48 45 52 45 5f 52  OLUMN_EQ|WHERE_R
2f7c6 4f 57 49 44 5f 45 51 29 3b 0a 20 20 20 20 20 20  OWID_EQ);.      
2f7c7 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
2f7c8 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 7b 0a  ator & WO_IN ){.
2f7c9 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45          Expr *pE
2f7ca 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  xpr = pTerm->pEx
2f7cb 70 72 3b 0a 20 20 20 20 20 20 20 20 77 73 46 6c  pr;.        wsFl
2f7cc 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c  ags |= WHERE_COL
2f7cd 55 4d 4e 5f 49 4e 3b 0a 20 20 20 20 20 20 20 20  UMN_IN;.        
2f7ce 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
2f7cf 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
2f7d0 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
2f7d1 20 20 20 20 20 20 6e 49 6e 4d 75 6c 20 2a 3d 20        nInMul *= 
2f7d2 32 35 3b 0a 20 20 20 20 20 20 20 20 20 20 62 49  25;.          bI
2f7d3 6e 45 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  nEst = 1;.      
2f7d4 20 20 7d 65 6c 73 65 20 69 66 28 20 70 45 78 70    }else if( pExp
2f7d5 72 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20  r->x.pList ){.  
2f7d6 20 20 20 20 20 20 20 20 6e 49 6e 4d 75 6c 20 2a          nInMul *
2f7d7 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2f7d8 2d 3e 6e 45 78 70 72 20 2b 20 31 3b 0a 20 20 20  ->nExpr + 1;.   
2f7d9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
2f7da 73 65 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f  se if( pTerm->eO
2f7db 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 53 4e  perator & WO_ISN
2f7dc 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 77  ULL ){.        w
2f7dd 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f  sFlags |= WHERE_
2f7de 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 3b 0a 20 20 20  COLUMN_NULL;.   
2f7df 20 20 20 7d 0a 20 20 20 20 20 20 75 73 65 64 20     }.      used 
2f7e0 7c 3d 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  |= pTerm->prereq
2f7e1 52 69 67 68 74 3b 0a 20 20 20 20 7d 0a 0a 20 20  Right;.    }..  
2f7e2 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74    /* Determine t
2f7e3 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 42 6f 75  he value of nBou
2f7e4 6e 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e  nd. */.    if( n
2f7e5 45 71 3c 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75  Eq<pProbe->nColu
2f7e6 6d 6e 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  mn ){.      int 
2f7e7 6a 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f  j = pProbe->aiCo
2f7e8 6c 75 6d 6e 5b 6e 45 71 5d 3b 0a 20 20 20 20 20  lumn[nEq];.     
2f7e9 20 69 66 28 20 66 69 6e 64 54 65 72 6d 28 70 57   if( findTerm(pW
2f7ea 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52  C, iCur, j, notR
2f7eb 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c  eady, WO_LT|WO_L
2f7ec 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 70  E|WO_GT|WO_GE, p
2f7ed 49 64 78 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Idx) ){.        
2f7ee 57 68 65 72 65 54 65 72 6d 20 2a 70 54 6f 70 20  WhereTerm *pTop 
2f7ef 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
2f7f0 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64  iCur, j, notRead
2f7f1 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20  y, WO_LT|WO_LE, 
2f7f2 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 57  pIdx);.        W
2f7f3 68 65 72 65 54 65 72 6d 20 2a 70 42 74 6d 20 3d  hereTerm *pBtm =
2f7f4 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2f7f5 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79  Cur, j, notReady
2f7f6 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 70  , WO_GT|WO_GE, p
2f7f7 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 77 68  Idx);.        wh
2f7f8 65 72 65 52 61 6e 67 65 53 63 61 6e 45 73 74 28  ereRangeScanEst(
2f7f9 70 50 61 72 73 65 2c 20 70 50 72 6f 62 65 2c 20  pParse, pProbe, 
2f7fa 6e 45 71 2c 20 70 42 74 6d 2c 20 70 54 6f 70 2c  nEq, pBtm, pTop,
2f7fb 20 26 6e 42 6f 75 6e 64 29 3b 0a 20 20 20 20 20   &nBound);.     
2f7fc 20 20 20 69 66 28 20 70 54 6f 70 20 29 7b 0a 20     if( pTop ){. 
2f7fd 20 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73           wsFlags
2f7fe 20 7c 3d 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49   |= WHERE_TOP_LI
2f7ff 4d 49 54 3b 0a 20 20 20 20 20 20 20 20 20 20 75  MIT;.          u
2f800 73 65 64 20 7c 3d 20 70 54 6f 70 2d 3e 70 72 65  sed |= pTop->pre
2f801 72 65 71 52 69 67 68 74 3b 0a 20 20 20 20 20 20  reqRight;.      
2f802 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2f803 70 42 74 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  pBtm ){.        
2f804 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45    wsFlags |= WHE
2f805 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a 20 20  RE_BTM_LIMIT;.  
2f806 20 20 20 20 20 20 20 20 75 73 65 64 20 7c 3d 20          used |= 
2f807 70 42 74 6d 2d 3e 70 72 65 72 65 71 52 69 67 68  pBtm->prereqRigh
2f808 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
2f809 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20       wsFlags |= 
2f80a 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41  (WHERE_COLUMN_RA
2f80b 4e 47 45 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f  NGE|WHERE_ROWID_
2f80c 52 41 4e 47 45 29 3b 0a 20 20 20 20 20 20 7d 0a  RANGE);.      }.
2f80d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50      }else if( pP
2f80e 72 6f 62 65 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  robe->onError!=O
2f80f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20  E_None ){.      
2f810 74 65 73 74 63 61 73 65 28 20 77 73 46 6c 61 67  testcase( wsFlag
2f811 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  s & WHERE_COLUMN
2f812 5f 49 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _IN );.      tes
2f813 74 63 61 73 65 28 20 77 73 46 6c 61 67 73 20 26  tcase( wsFlags &
2f814 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55   WHERE_COLUMN_NU
2f815 4c 4c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  LL );.      if( 
2f816 28 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52  (wsFlags & (WHER
2f817 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57 48 45 52  E_COLUMN_IN|WHER
2f818 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 29 29 3d  E_COLUMN_NULL))=
2f819 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 77 73  =0 ){.        ws
2f81a 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 55  Flags |= WHERE_U
2f81b 4e 49 51 55 45 3b 0a 20 20 20 20 20 20 7d 0a 20  NIQUE;.      }. 
2f81c 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
2f81d 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45  there is an ORDE
2f81e 52 20 42 59 20 63 6c 61 75 73 65 20 61 6e 64 20  R BY clause and 
2f81f 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20  the index being 
2f820 63 6f 6e 73 69 64 65 72 65 64 20 77 69 6c 6c 0a  considered will.
2f821 20 20 20 20 2a 2a 20 6e 61 74 75 72 61 6c 6c 79      ** naturally
2f822 20 73 63 61 6e 20 72 6f 77 73 20 69 6e 20 74 68   scan rows in th
2f823 65 20 72 65 71 75 69 72 65 64 20 6f 72 64 65 72  e required order
2f824 2c 20 73 65 74 20 74 68 65 20 61 70 70 72 6f 70  , set the approp
2f825 72 69 61 74 65 20 66 6c 61 67 73 0a 20 20 20 20  riate flags.    
2f826 2a 2a 20 69 6e 20 77 73 46 6c 61 67 73 2e 20 4f  ** in wsFlags. O
2f827 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
2f828 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  re is an ORDER B
2f829 59 20 63 6c 61 75 73 65 20 62 75 74 20 74 68 65  Y clause but the
2f82a 20 69 6e 64 65 78 0a 20 20 20 20 2a 2a 20 77 69   index.    ** wi
2f82b 6c 6c 20 73 63 61 6e 20 72 6f 77 73 20 69 6e 20  ll scan rows in 
2f82c 61 20 64 69 66 66 65 72 65 6e 74 20 6f 72 64 65  a different orde
2f82d 72 2c 20 73 65 74 20 74 68 65 20 62 53 6f 72 74  r, set the bSort
2f82e 20 76 61 72 69 61 62 6c 65 2e 20 20 2a 2f 0a 20   variable.  */. 
2f82f 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
2f830 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 77 73  ){.      if( (ws
2f831 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 43  Flags & (WHERE_C
2f832 4f 4c 55 4d 4e 5f 49 4e 7c 57 48 45 52 45 5f 43  OLUMN_IN|WHERE_C
2f833 4f 4c 55 4d 4e 5f 4e 55 4c 4c 29 29 3d 3d 30 0a  OLUMN_NULL))==0.
2f834 20 20 20 20 20 20 20 20 26 26 20 69 73 53 6f 72          && isSor
2f835 74 69 6e 67 49 6e 64 65 78 28 70 50 61 72 73 65  tingIndex(pParse
2f836 2c 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 70  ,pWC->pMaskSet,p
2f837 50 72 6f 62 65 2c 69 43 75 72 2c 70 4f 72 64 65  Probe,iCur,pOrde
2f838 72 42 79 2c 6e 45 71 2c 26 72 65 76 29 0a 20 20  rBy,nEq,&rev).  
2f839 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 77      ){.        w
2f83a 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f  sFlags |= WHERE_
2f83b 52 4f 57 49 44 5f 52 41 4e 47 45 7c 57 48 45 52  ROWID_RANGE|WHER
2f83c 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57  E_COLUMN_RANGE|W
2f83d 48 45 52 45 5f 4f 52 44 45 52 42 59 3b 0a 20 20  HERE_ORDERBY;.  
2f83e 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
2f83f 20 28 72 65 76 20 3f 20 57 48 45 52 45 5f 52 45   (rev ? WHERE_RE
2f840 56 45 52 53 45 20 3a 20 30 29 3b 0a 20 20 20 20  VERSE : 0);.    
2f841 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2f842 20 62 53 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20   bSort = 1;.    
2f843 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
2f844 2a 20 49 66 20 63 75 72 72 65 6e 74 6c 79 20 63  * If currently c
2f845 61 6c 63 75 6c 61 74 69 6e 67 20 74 68 65 20 63  alculating the c
2f846 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 61 6e 20  ost of using an 
2f847 69 6e 64 65 78 20 28 6e 6f 74 20 74 68 65 20 49  index (not the I
2f848 50 4b 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 29  PK.    ** index)
2f849 2c 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 61  , determine if a
2f84a 6c 6c 20 72 65 71 75 69 72 65 64 20 63 6f 6c 75  ll required colu
2f84b 6d 6e 20 64 61 74 61 20 6d 61 79 20 62 65 20 6f  mn data may be o
2f84c 62 74 61 69 6e 65 64 20 77 69 74 68 6f 75 74 20  btained without 
2f84d 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69 6e 67 20  .    ** seeking 
2f84e 74 6f 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  to entries in th
2f84f 65 20 6d 61 69 6e 20 74 61 62 6c 65 20 28 69 2e  e main table (i.
2f850 65 2e 20 69 66 20 74 68 65 20 69 6e 64 65 78 20  e. if the index 
2f851 69 73 20 61 20 63 6f 76 65 72 69 6e 67 0a 20 20  is a covering.  
2f852 20 20 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74    ** index for t
2f853 68 69 73 20 71 75 65 72 79 29 2e 20 49 66 20 69  his query). If i
2f854 74 20 69 73 2c 20 73 65 74 20 74 68 65 20 57 48  t is, set the WH
2f855 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 66 6c 61  ERE_IDX_ONLY fla
2f856 67 20 69 6e 0a 20 20 20 20 2a 2a 20 77 73 46 6c  g in.    ** wsFl
2f857 61 67 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ags. Otherwise, 
2f858 73 65 74 20 74 68 65 20 62 4c 6f 6f 6b 75 70 20  set the bLookup 
2f859 76 61 72 69 61 62 6c 65 20 74 6f 20 74 72 75 65  variable to true
2f85a 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 49  .  */.    if( pI
2f85b 64 78 20 26 26 20 77 73 46 6c 61 67 73 20 29 7b  dx && wsFlags ){
2f85c 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 6d  .      Bitmask m
2f85d 20 3d 20 70 53 72 63 2d 3e 63 6f 6c 55 73 65 64   = pSrc->colUsed
2f85e 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  ;.      int j;. 
2f85f 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
2f860 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a  pIdx->nColumn; j
2f861 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ++){.        int
2f862 20 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c   x = pIdx->aiCol
2f863 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20  umn[j];.        
2f864 69 66 28 20 78 3c 42 4d 53 2d 31 20 29 7b 0a 20  if( x<BMS-1 ){. 
2f865 20 20 20 20 20 20 20 20 20 6d 20 26 3d 20 7e 28           m &= ~(
2f866 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 78 29  ((Bitmask)1)<<x)
2f867 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2f868 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6d 3d    }.      if( m=
2f869 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 77 73  =0 ){.        ws
2f86a 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 49  Flags |= WHERE_I
2f86b 44 58 5f 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 7d  DX_ONLY;.      }
2f86c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 4c  else{.        bL
2f86d 6f 6f 6b 75 70 20 3d 20 31 3b 0a 20 20 20 20 20  ookup = 1;.     
2f86e 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
2f86f 2a 2a 2a 20 42 65 67 69 6e 20 61 64 64 69 6e 67  *** Begin adding
2f870 20 75 70 20 74 68 65 20 63 6f 73 74 20 6f 66 20   up the cost of 
2f871 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78  using this index
2f872 20 28 4e 65 65 64 73 20 69 6d 70 72 6f 76 65 6d   (Needs improvem
2f873 65 6e 74 73 29 0a 20 20 20 20 2a 2a 0a 20 20 20  ents).    **.   
2f874 20 2a 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65   ** Estimate the
2f875 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
2f876 6f 66 20 6f 75 74 70 75 74 2e 20 20 46 6f 72 20  of output.  For 
2f877 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 2c 0a  an IN operator,.
2f878 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 6c 65      ** do not le
2f879 74 20 74 68 65 20 65 73 74 69 6d 61 74 65 20 65  t the estimate e
2f87a 78 63 65 65 64 20 68 61 6c 66 20 74 68 65 20 72  xceed half the r
2f87b 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ows in the table
2f87c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 52 6f  ..    */.    nRo
2f87d 77 20 3d 20 28 64 6f 75 62 6c 65 29 28 61 69 52  w = (double)(aiR
2f87e 6f 77 45 73 74 5b 6e 45 71 5d 20 2a 20 6e 49 6e  owEst[nEq] * nIn
2f87f 4d 75 6c 29 3b 0a 20 20 20 20 69 66 28 20 62 49  Mul);.    if( bI
2f880 6e 45 73 74 20 26 26 20 6e 52 6f 77 2a 32 3e 61  nEst && nRow*2>a
2f881 69 52 6f 77 45 73 74 5b 30 5d 20 29 7b 0a 20 20  iRowEst[0] ){.  
2f882 20 20 20 20 6e 52 6f 77 20 3d 20 61 69 52 6f 77      nRow = aiRow
2f883 45 73 74 5b 30 5d 2f 32 3b 0a 20 20 20 20 20 20  Est[0]/2;.      
2f884 6e 49 6e 4d 75 6c 20 3d 20 28 69 6e 74 29 28 6e  nInMul = (int)(n
2f885 52 6f 77 20 2f 20 61 69 52 6f 77 45 73 74 5b 6e  Row / aiRowEst[n
2f886 45 71 5d 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  Eq]);.    }..   
2f887 20 2f 2a 20 41 73 73 75 6d 65 20 63 6f 6e 73 74   /* Assume const
2f888 61 6e 74 20 63 6f 73 74 20 74 6f 20 61 63 63 65  ant cost to acce
2f889 73 73 20 61 20 72 6f 77 20 61 6e 64 20 6c 6f 67  ss a row and log
2f88a 61 72 69 74 68 6d 69 63 20 63 6f 73 74 20 74 6f  arithmic cost to
2f88b 0a 20 20 20 20 2a 2a 20 64 6f 20 61 20 62 69 6e  .    ** do a bin
2f88c 61 72 79 20 73 65 61 72 63 68 2e 20 20 48 65 6e  ary search.  Hen
2f88d 63 65 2c 20 74 68 65 20 69 6e 69 74 69 61 6c 20  ce, the initial 
2f88e 63 6f 73 74 20 69 73 20 74 68 65 20 6e 75 6d 62  cost is the numb
2f88f 65 72 20 6f 66 20 6f 75 74 70 75 74 0a 20 20 20  er of output.   
2f890 20 2a 2a 20 72 6f 77 73 20 70 6c 75 73 20 6c 6f   ** rows plus lo
2f891 67 32 28 74 61 62 6c 65 2d 73 69 7a 65 29 20 74  g2(table-size) t
2f892 69 6d 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20  imes the number 
2f893 6f 66 20 62 69 6e 61 72 79 20 73 65 61 72 63 68  of binary search
2f894 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  es..    */.    c
2f895 6f 73 74 20 3d 20 6e 52 6f 77 20 2b 20 6e 49 6e  ost = nRow + nIn
2f896 4d 75 6c 2a 65 73 74 4c 6f 67 28 61 69 52 6f 77  Mul*estLog(aiRow
2f897 45 73 74 5b 30 5d 29 3b 0a 0a 20 20 20 20 2f 2a  Est[0]);..    /*
2f898 20 41 64 6a 75 73 74 20 74 68 65 20 6e 75 6d 62   Adjust the numb
2f899 65 72 20 6f 66 20 72 6f 77 73 20 61 6e 64 20 74  er of rows and t
2f89a 68 65 20 63 6f 73 74 20 64 6f 77 6e 77 61 72 64  he cost downward
2f89b 20 74 6f 20 72 65 66 6c 65 63 74 20 72 6f 77 73   to reflect rows
2f89c 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 61 72 65  .    ** that are
2f89d 20 65 78 63 6c 75 64 65 64 20 62 79 20 72 61 6e   excluded by ran
2f89e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a  ge constraints..
2f89f 20 20 20 20 2a 2f 0a 20 20 20 20 6e 52 6f 77 20      */.    nRow 
2f8a0 3d 20 28 6e 52 6f 77 20 2a 20 28 64 6f 75 62 6c  = (nRow * (doubl
2f8a1 65 29 6e 42 6f 75 6e 64 29 20 2f 20 28 64 6f 75  e)nBound) / (dou
2f8a2 62 6c 65 29 31 30 30 3b 0a 20 20 20 20 63 6f 73  ble)100;.    cos
2f8a3 74 20 3d 20 28 63 6f 73 74 20 2a 20 28 64 6f 75  t = (cost * (dou
2f8a4 62 6c 65 29 6e 42 6f 75 6e 64 29 20 2f 20 28 64  ble)nBound) / (d
2f8a5 6f 75 62 6c 65 29 31 30 30 3b 0a 0a 20 20 20 20  ouble)100;..    
2f8a6 2f 2a 20 41 64 64 20 69 6e 20 74 68 65 20 65 73  /* Add in the es
2f8a7 74 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20  timated cost of 
2f8a8 73 6f 72 74 69 6e 67 20 74 68 65 20 72 65 73 75  sorting the resu
2f8a9 6c 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  lt.    */.    if
2f8aa 28 20 62 53 6f 72 74 20 29 7b 0a 20 20 20 20 20  ( bSort ){.     
2f8ab 20 63 6f 73 74 20 2b 3d 20 63 6f 73 74 2a 65 73   cost += cost*es
2f8ac 74 4c 6f 67 28 63 6f 73 74 29 3b 0a 20 20 20 20  tLog(cost);.    
2f8ad 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6c 6c  }..    /* If all
2f8ae 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 61 6e   information can
2f8af 20 62 65 20 74 61 6b 65 6e 20 64 69 72 65 63 74   be taken direct
2f8b0 6c 79 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65  ly from the inde
2f8b1 78 2c 20 77 65 20 61 76 6f 69 64 0a 20 20 20 20  x, we avoid.    
2f8b2 2a 2a 20 64 6f 69 6e 67 20 74 61 62 6c 65 20 6c  ** doing table l
2f8b3 6f 6f 6b 75 70 73 2e 20 20 54 68 69 73 20 72 65  ookups.  This re
2f8b4 64 75 63 65 73 20 74 68 65 20 63 6f 73 74 20 62  duces the cost b
2f8b5 79 20 68 61 6c 66 2e 20 20 28 4e 6f 74 20 72 65  y half.  (Not re
2f8b6 61 6c 6c 79 20 2d 0a 20 20 20 20 2a 2a 20 74 68  ally -.    ** th
2f8b7 69 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66  is needs to be f
2f8b8 69 78 65 64 2e 29 0a 20 20 20 20 2a 2f 0a 20 20  ixed.).    */.  
2f8b9 20 20 69 66 28 20 70 49 64 78 20 26 26 20 62 4c    if( pIdx && bL
2f8ba 6f 6f 6b 75 70 3d 3d 30 20 29 7b 0a 20 20 20 20  ookup==0 ){.    
2f8bb 20 20 63 6f 73 74 20 2f 3d 20 28 64 6f 75 62 6c    cost /= (doubl
2f8bc 65 29 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  e)2;.    }.    /
2f8bd 2a 2a 2a 2a 20 43 6f 73 74 20 6f 66 20 75 73 69  **** Cost of usi
2f8be 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 68 61  ng this index ha
2f8bf 73 20 6e 6f 77 20 62 65 65 6e 20 63 6f 6d 70 75  s now been compu
2f8c0 74 65 64 20 2a 2a 2a 2a 2f 0a 0a 20 20 20 20 57  ted ****/..    W
2f8c1 48 45 52 45 54 52 41 43 45 28 28 0a 20 20 20 20  HERETRACE((.    
2f8c2 20 20 22 74 62 6c 3d 25 73 20 69 64 78 3d 25 73    "tbl=%s idx=%s
2f8c3 20 6e 45 71 3d 25 64 20 6e 49 6e 4d 75 6c 3d 25   nEq=%d nInMul=%
2f8c4 64 20 6e 42 6f 75 6e 64 3d 25 64 20 62 53 6f 72  d nBound=%d bSor
2f8c5 74 3d 25 64 20 62 4c 6f 6f 6b 75 70 3d 25 64 22  t=%d bLookup=%d"
2f8c6 0a 20 20 20 20 20 20 22 20 77 73 46 6c 61 67 73  .      " wsFlags
2f8c7 3d 25 64 20 20 20 28 6e 52 6f 77 3d 25 2e 32 66  =%d   (nRow=%.2f
2f8c8 20 63 6f 73 74 3d 25 2e 32 66 29 5c 6e 22 2c 0a   cost=%.2f)\n",.
2f8c9 20 20 20 20 20 20 70 53 72 63 2d 3e 70 54 61 62        pSrc->pTab
2f8ca 2d 3e 7a 4e 61 6d 65 2c 20 28 70 49 64 78 20 3f  ->zName, (pIdx ?
2f8cb 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20 3a 20 22   pIdx->zName : "
2f8cc 69 70 6b 22 29 2c 20 0a 20 20 20 20 20 20 6e 45  ipk"), .      nE
2f8cd 71 2c 20 6e 49 6e 4d 75 6c 2c 20 6e 42 6f 75 6e  q, nInMul, nBoun
2f8ce 64 2c 20 62 53 6f 72 74 2c 20 62 4c 6f 6f 6b 75  d, bSort, bLooku
2f8cf 70 2c 20 77 73 46 6c 61 67 73 2c 20 6e 52 6f 77  p, wsFlags, nRow
2f8d0 2c 20 63 6f 73 74 0a 20 20 20 20 29 29 3b 0a 0a  , cost.    ));..
2f8d1 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
2f8d2 6e 64 65 78 20 69 73 20 74 68 65 20 62 65 73 74  ndex is the best
2f8d3 20 77 65 20 68 61 76 65 20 73 65 65 6e 20 73 6f   we have seen so
2f8d4 20 66 61 72 2c 20 74 68 65 6e 20 72 65 63 6f 72   far, then recor
2f8d5 64 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 6e  d this.    ** in
2f8d6 64 65 78 20 61 6e 64 20 69 74 73 20 63 6f 73 74  dex and its cost
2f8d7 20 69 6e 20 74 68 65 20 70 43 6f 73 74 20 73 74   in the pCost st
2f8d8 72 75 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a  ructure..    */.
2f8d9 20 20 20 20 69 66 28 20 28 21 70 49 64 78 20 7c      if( (!pIdx |
2f8da 7c 20 77 73 46 6c 61 67 73 29 20 26 26 20 63 6f  | wsFlags) && co
2f8db 73 74 3c 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20  st<pCost->rCost 
2f8dc 29 7b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  ){.      pCost->
2f8dd 72 43 6f 73 74 20 3d 20 63 6f 73 74 3b 0a 20 20  rCost = cost;.  
2f8de 20 20 20 20 70 43 6f 73 74 2d 3e 6e 52 6f 77 20      pCost->nRow 
2f8df 3d 20 6e 52 6f 77 3b 0a 20 20 20 20 20 20 70 43  = nRow;.      pC
2f8e0 6f 73 74 2d 3e 75 73 65 64 20 3d 20 75 73 65 64  ost->used = used
2f8e1 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70  ;.      pCost->p
2f8e2 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 28 77  lan.wsFlags = (w
2f8e3 73 46 6c 61 67 73 26 77 73 46 6c 61 67 4d 61 73  sFlags&wsFlagMas
2f8e4 6b 29 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d  k);.      pCost-
2f8e5 3e 70 6c 61 6e 2e 6e 45 71 20 3d 20 6e 45 71 3b  >plan.nEq = nEq;
2f8e6 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c  .      pCost->pl
2f8e7 61 6e 2e 75 2e 70 49 64 78 20 3d 20 70 49 64 78  an.u.pIdx = pIdx
2f8e8 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2f8e9 49 66 20 74 68 65 72 65 20 77 61 73 20 61 6e 20  If there was an 
2f8ea 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
2f8eb 65 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 61  e, then only tha
2f8ec 74 20 6f 6e 65 20 69 6e 64 65 78 20 69 73 0a 20  t one index is. 
2f8ed 20 20 20 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64     ** considered
2f8ee 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 72  . */.    if( pSr
2f8ef 63 2d 3e 70 49 6e 64 65 78 20 29 20 62 72 65 61  c->pIndex ) brea
2f8f0 6b 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 65 74  k;..    /* Reset
2f8f1 20 6d 61 73 6b 73 20 66 6f 72 20 74 68 65 20 6e   masks for the n
2f8f2 65 78 74 20 69 6e 64 65 78 20 69 6e 20 74 68 65  ext index in the
2f8f3 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 77 73 46   loop */.    wsF
2f8f4 6c 61 67 4d 61 73 6b 20 3d 20 7e 28 57 48 45 52  lagMask = ~(WHER
2f8f5 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45  E_ROWID_EQ|WHERE
2f8f6 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a 20  _ROWID_RANGE);. 
2f8f7 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20     eqTermMask = 
2f8f8 69 64 78 45 71 54 65 72 6d 4d 61 73 6b 3b 0a 20  idxEqTermMask;. 
2f8f9 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72   }..  /* If ther
2f8fa 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59  e is no ORDER BY
2f8fb 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20   clause and the 
2f8fc 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72  SQLITE_ReverseOr
2f8fd 64 65 72 20 66 6c 61 67 0a 20 20 2a 2a 20 69 73  der flag.  ** is
2f8fe 20 73 65 74 2c 20 74 68 65 6e 20 72 65 76 65 72   set, then rever
2f8ff 73 65 20 74 68 65 20 6f 72 64 65 72 20 74 68 61  se the order tha
2f900 74 20 74 68 65 20 69 6e 64 65 78 20 77 69 6c 6c  t the index will
2f901 20 62 65 20 73 63 61 6e 6e 65 64 0a 20 20 2a 2a   be scanned.  **
2f902 20 69 6e 2e 20 54 68 69 73 20 69 73 20 75 73 65   in. This is use
2f903 64 20 66 6f 72 20 61 70 70 6c 69 63 61 74 69 6f  d for applicatio
2f904 6e 20 74 65 73 74 69 6e 67 2c 20 74 6f 20 68 65  n testing, to he
2f905 6c 70 20 66 69 6e 64 20 63 61 73 65 73 0a 20 20  lp find cases.  
2f906 2a 2a 20 77 68 65 72 65 20 61 70 70 6c 69 63 61  ** where applica
2f907 74 69 6f 6e 20 62 65 68 61 76 69 6f 75 72 20 64  tion behaviour d
2f908 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 28 75  epends on the (u
2f909 6e 64 65 66 69 6e 65 64 29 20 6f 72 64 65 72 20  ndefined) order 
2f90a 74 68 61 74 0a 20 20 2a 2a 20 53 51 4c 69 74 65  that.  ** SQLite
2f90b 20 6f 75 74 70 75 74 73 20 72 6f 77 73 20 69 6e   outputs rows in
2f90c 20 69 6e 20 74 68 65 20 61 62 73 65 6e 63 65 20   in the absence 
2f90d 6f 66 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  of an ORDER BY c
2f90e 6c 61 75 73 65 2e 20 20 2a 2f 0a 20 20 69 66 28  lause.  */.  if(
2f90f 20 21 70 4f 72 64 65 72 42 79 20 26 26 20 70 50   !pOrderBy && pP
2f910 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20  arse->db->flags 
2f911 26 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65  & SQLITE_Reverse
2f912 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 70 43 6f  Order ){.    pCo
2f913 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  st->plan.wsFlags
2f914 20 7c 3d 20 57 48 45 52 45 5f 52 45 56 45 52 53   |= WHERE_REVERS
2f915 45 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  E;.  }..  assert
2f916 28 20 70 4f 72 64 65 72 42 79 20 7c 7c 20 28 70  ( pOrderBy || (p
2f917 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  Cost->plan.wsFla
2f918 67 73 26 57 48 45 52 45 5f 4f 52 44 45 52 42 59  gs&WHERE_ORDERBY
2f919 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  )==0 );.  assert
2f91a 28 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e  ( pCost->plan.u.
2f91b 70 49 64 78 3d 3d 30 20 7c 7c 20 28 70 43 6f 73  pIdx==0 || (pCos
2f91c 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26  t->plan.wsFlags&
2f91d 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 29 3d  WHERE_ROWID_EQ)=
2f91e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
2f91f 70 53 72 63 2d 3e 70 49 6e 64 65 78 3d 3d 30 20  pSrc->pIndex==0 
2f920 0a 20 20 20 20 20 20 20 7c 7c 20 70 43 6f 73 74  .       || pCost
2f921 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 30  ->plan.u.pIdx==0
2f922 20 0a 20 20 20 20 20 20 20 7c 7c 20 70 43 6f 73   .       || pCos
2f923 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d  t->plan.u.pIdx==
2f924 70 53 72 63 2d 3e 70 49 6e 64 65 78 20 0a 20 20  pSrc->pIndex .  
2f925 29 3b 0a 0a 20 20 57 48 45 52 45 54 52 41 43 45  );..  WHERETRACE
2f926 28 28 22 62 65 73 74 20 69 6e 64 65 78 20 69 73  (("best index is
2f927 3a 20 25 73 5c 6e 22 2c 20 0a 20 20 20 20 28 70  : %s\n", .    (p
2f928 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64  Cost->plan.u.pId
2f929 78 20 3f 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e  x ? pCost->plan.
2f92a 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 20 3a 20  u.pIdx->zName : 
2f92b 22 69 70 6b 22 29 0a 20 20 29 29 3b 0a 20 20 0a  "ipk").  ));.  .
2f92c 20 20 62 65 73 74 4f 72 43 6c 61 75 73 65 49 6e    bestOrClauseIn
2f92d 64 65 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c  dex(pParse, pWC,
2f92e 20 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c   pSrc, notReady,
2f92f 20 70 4f 72 64 65 72 42 79 2c 20 70 43 6f 73 74   pOrderBy, pCost
2f930 29 3b 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e  );.  pCost->plan
2f931 2e 77 73 46 6c 61 67 73 20 7c 3d 20 65 71 54 65  .wsFlags |= eqTe
2f932 72 6d 4d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rmMask;.}../*.**
2f933 20 46 69 6e 64 20 74 68 65 20 71 75 65 72 79 20   Find the query 
2f934 70 6c 61 6e 20 66 6f 72 20 61 63 63 65 73 73 69  plan for accessi
2f935 6e 67 20 74 61 62 6c 65 20 70 53 72 63 2d 3e 70  ng table pSrc->p
2f936 54 61 62 2e 20 57 72 69 74 65 20 74 68 65 0a 2a  Tab. Write the.*
2f937 2a 20 62 65 73 74 20 71 75 65 72 79 20 70 6c 61  * best query pla
2f938 6e 20 61 6e 64 20 69 74 73 20 63 6f 73 74 20 69  n and its cost i
2f939 6e 74 6f 20 74 68 65 20 57 68 65 72 65 43 6f 73  nto the WhereCos
2f93a 74 20 6f 62 6a 65 63 74 20 73 75 70 70 6c 69 65  t object supplie
2f93b 64 20 0a 2a 2a 20 61 73 20 74 68 65 20 6c 61 73  d .** as the las
2f93c 74 20 70 61 72 61 6d 65 74 65 72 2e 20 54 68 69  t parameter. Thi
2f93d 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 63  s function may c
2f93e 61 6c 63 75 6c 61 74 65 20 74 68 65 20 63 6f 73  alculate the cos
2f93f 74 20 6f 66 0a 2a 2a 20 62 6f 74 68 20 72 65 61  t of.** both rea
2f940 6c 20 61 6e 64 20 76 69 72 74 75 61 6c 20 74 61  l and virtual ta
2f941 62 6c 65 20 73 63 61 6e 73 2e 0a 2a 2f 0a 73 74  ble scans..*/.st
2f942 61 74 69 63 20 76 6f 69 64 20 62 65 73 74 49 6e  atic void bestIn
2f943 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50  dex(.  Parse *pP
2f944 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
2f945 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
2f946 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57  g context */.  W
2f947 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
2f948 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2f949 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2f94a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
2f94b 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20  st_item *pSrc,  
2f94c 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* The FROM clau
2f94d 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63  se term to searc
2f94e 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e  h */.  Bitmask n
2f94f 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20 20 20  otReady,        
2f950 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75     /* Mask of cu
2f951 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e  rsors that are n
2f952 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a  ot available */.
2f953 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
2f954 65 72 42 79 2c 20 20 20 20 20 20 20 20 20 2f 2a  erBy,         /*
2f955 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
2f956 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43  ause */.  WhereC
2f957 6f 73 74 20 2a 70 43 6f 73 74 20 20 20 20 20 20  ost *pCost      
2f958 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20        /* Lowest 
2f959 63 6f 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20  cost query plan 
2f95a 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  */.){.#ifndef SQ
2f95b 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2f95c 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49 73 56  LTABLE.  if( IsV
2f95d 69 72 74 75 61 6c 28 70 53 72 63 2d 3e 70 54 61  irtual(pSrc->pTa
2f95e 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  b) ){.    sqlite
2f95f 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 20  3_index_info *p 
2f960 3d 20 30 3b 0a 20 20 20 20 62 65 73 74 56 69 72  = 0;.    bestVir
2f961 74 75 61 6c 49 6e 64 65 78 28 70 50 61 72 73 65  tualIndex(pParse
2f962 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74  , pWC, pSrc, not
2f963 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79 2c  Ready, pOrderBy,
2f964 20 70 43 6f 73 74 2c 20 26 70 29 3b 0a 20 20 20   pCost, &p);.   
2f965 20 69 66 28 20 70 2d 3e 6e 65 65 64 54 6f 46 72   if( p->needToFr
2f966 65 65 49 64 78 53 74 72 20 29 7b 0a 20 20 20 20  eeIdxStr ){.    
2f967 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
2f968 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20 7d  ->idxStr);.    }
2f969 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2f96a 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ee(pParse->db, p
2f96b 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  );.  }else.#endi
2f96c 66 0a 20 20 7b 0a 20 20 20 20 62 65 73 74 42 74  f.  {.    bestBt
2f96d 72 65 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c  reeIndex(pParse,
2f96e 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52   pWC, pSrc, notR
2f96f 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79 2c 20  eady, pOrderBy, 
2f970 70 43 6f 73 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  pCost);.  }.}../
2f971 2a 0a 2a 2a 20 44 69 73 61 62 6c 65 20 61 20 74  *.** Disable a t
2f972 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45 52 45  erm in the WHERE
2f973 20 63 6c 61 75 73 65 2e 20 20 45 78 63 65 70 74   clause.  Except
2f974 2c 20 64 6f 20 6e 6f 74 20 64 69 73 61 62 6c 65  , do not disable
2f975 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20 69 66 20   the term.** if 
2f976 69 74 20 63 6f 6e 74 72 6f 6c 73 20 61 20 4c 45  it controls a LE
2f977 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 61 6e  FT OUTER JOIN an
2f978 64 20 69 74 20 64 69 64 20 6e 6f 74 20 6f 72 69  d it did not ori
2f979 67 69 6e 61 74 65 20 69 6e 20 74 68 65 20 4f 4e  ginate in the ON
2f97a 0a 2a 2a 20 6f 72 20 55 53 49 4e 47 20 63 6c 61  .** or USING cla
2f97b 75 73 65 20 6f 66 20 74 68 61 74 20 6a 6f 69 6e  use of that join
2f97c 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 69 64 65 72  ..**.** Consider
2f97d 20 74 68 65 20 74 65 72 6d 20 74 32 2e 7a 3d 27   the term t2.z='
2f97e 6f 6b 27 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  ok' in the follo
2f97f 77 69 6e 67 20 71 75 65 72 69 65 73 3a 0a 2a 2a  wing queries:.**
2f980 0a 2a 2a 20 20 20 28 31 29 20 20 53 45 4c 45 43  .**   (1)  SELEC
2f981 54 20 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54  T * FROM t1 LEFT
2f982 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61   JOIN t2 ON t1.a
2f983 3d 74 32 2e 78 20 57 48 45 52 45 20 74 32 2e 7a  =t2.x WHERE t2.z
2f984 3d 27 6f 6b 27 0a 2a 2a 20 20 20 28 32 29 20 20  ='ok'.**   (2)  
2f985 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
2f986 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e   LEFT JOIN t2 ON
2f987 20 74 31 2e 61 3d 74 32 2e 78 20 41 4e 44 20 74   t1.a=t2.x AND t
2f988 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20 20 28 33  2.z='ok'.**   (3
2f989 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  )  SELECT * FROM
2f98a 20 74 31 2c 20 74 32 20 57 48 45 52 45 20 74 31   t1, t2 WHERE t1
2f98b 2e 61 3d 74 32 2e 78 20 41 4e 44 20 74 32 2e 7a  .a=t2.x AND t2.z
2f98c 3d 27 6f 6b 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ='ok'.**.** The 
2f98d 74 32 2e 7a 3d 27 6f 6b 27 20 69 73 20 64 69 73  t2.z='ok' is dis
2f98e 61 62 6c 65 64 20 69 6e 20 74 68 65 20 69 6e 20  abled in the in 
2f98f 28 32 29 20 62 65 63 61 75 73 65 20 69 74 20 6f  (2) because it o
2f990 72 69 67 69 6e 61 74 65 73 0a 2a 2a 20 69 6e 20  riginates.** in 
2f991 74 68 65 20 4f 4e 20 63 6c 61 75 73 65 2e 20 20  the ON clause.  
2f992 54 68 65 20 74 65 72 6d 20 69 73 20 64 69 73 61  The term is disa
2f993 62 6c 65 64 20 69 6e 20 28 33 29 20 62 65 63 61  bled in (3) beca
2f994 75 73 65 20 69 74 20 69 73 20 6e 6f 74 20 70 61  use it is not pa
2f995 72 74 0a 2a 2a 20 6f 66 20 61 20 4c 45 46 54 20  rt.** of a LEFT 
2f996 4f 55 54 45 52 20 4a 4f 49 4e 2e 20 20 49 6e 20  OUTER JOIN.  In 
2f997 28 31 29 2c 20 74 68 65 20 74 65 72 6d 20 69 73  (1), the term is
2f998 20 6e 6f 74 20 64 69 73 61 62 6c 65 64 2e 0a 2a   not disabled..*
2f999 2a 0a 2a 2a 20 44 69 73 61 62 6c 69 6e 67 20 61  *.** Disabling a
2f99a 20 74 65 72 6d 20 63 61 75 73 65 73 20 74 68 61   term causes tha
2f99b 74 20 74 65 72 6d 20 74 6f 20 6e 6f 74 20 62 65  t term to not be
2f99c 20 74 65 73 74 65 64 20 69 6e 20 74 68 65 20 69   tested in the i
2f99d 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a 20 6f 66 20  nner loop.** of 
2f99e 74 68 65 20 6a 6f 69 6e 2e 20 20 44 69 73 61 62  the join.  Disab
2f99f 6c 69 6e 67 20 69 73 20 61 6e 20 6f 70 74 69 6d  ling is an optim
2f9a0 69 7a 61 74 69 6f 6e 2e 20 20 57 68 65 6e 20 74  ization.  When t
2f9a1 65 72 6d 73 20 61 72 65 20 73 61 74 69 73 66 69  erms are satisfi
2f9a2 65 64 0a 2a 2a 20 62 79 20 69 6e 64 69 63 65 73  ed.** by indices
2f9a3 2c 20 77 65 20 64 69 73 61 62 6c 65 20 74 68 65  , we disable the
2f9a4 6d 20 74 6f 20 70 72 65 76 65 6e 74 20 72 65 64  m to prevent red
2f9a5 75 6e 64 61 6e 74 20 74 65 73 74 73 20 69 6e 20  undant tests in 
2f9a6 74 68 65 20 69 6e 6e 65 72 0a 2a 2a 20 6c 6f 6f  the inner.** loo
2f9a7 70 2e 20 20 57 65 20 77 6f 75 6c 64 20 67 65 74  p.  We would get
2f9a8 20 74 68 65 20 63 6f 72 72 65 63 74 20 72 65 73   the correct res
2f9a9 75 6c 74 73 20 69 66 20 6e 6f 74 68 69 6e 67 20  ults if nothing 
2f9aa 77 65 72 65 20 65 76 65 72 20 64 69 73 61 62 6c  were ever disabl
2f9ab 65 64 2c 0a 2a 2a 20 62 75 74 20 6a 6f 69 6e 73  ed,.** but joins
2f9ac 20 6d 69 67 68 74 20 72 75 6e 20 61 20 6c 69 74   might run a lit
2f9ad 74 6c 65 20 73 6c 6f 77 65 72 2e 20 20 54 68 65  tle slower.  The
2f9ae 20 74 72 69 63 6b 20 69 73 20 74 6f 20 64 69 73   trick is to dis
2f9af 61 62 6c 65 20 61 73 20 6d 75 63 68 0a 2a 2a 20  able as much.** 
2f9b0 61 73 20 77 65 20 63 61 6e 20 77 69 74 68 6f 75  as we can withou
2f9b1 74 20 64 69 73 61 62 6c 69 6e 67 20 74 6f 6f 20  t disabling too 
2f9b2 6d 75 63 68 2e 20 20 49 66 20 77 65 20 64 69 73  much.  If we dis
2f9b3 61 62 6c 65 64 20 69 6e 20 28 31 29 2c 20 77 65  abled in (1), we
2f9b4 27 64 20 67 65 74 0a 2a 2a 20 74 68 65 20 77 72  'd get.** the wr
2f9b5 6f 6e 67 20 61 6e 73 77 65 72 2e 20 20 53 65 65  ong answer.  See
2f9b6 20 74 69 63 6b 65 74 20 23 38 31 33 2e 0a 2a 2f   ticket #813..*/
2f9b7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 69 73  .static void dis
2f9b8 61 62 6c 65 54 65 72 6d 28 57 68 65 72 65 4c 65  ableTerm(WhereLe
2f9b9 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 57 68 65  vel *pLevel, Whe
2f9ba 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 29 7b 0a  reTerm *pTerm){.
2f9bb 20 20 69 66 28 20 70 54 65 72 6d 0a 20 20 20 20    if( pTerm.    
2f9bc 20 20 26 26 20 41 4c 57 41 59 53 28 28 70 54 65    && ALWAYS((pTe
2f9bd 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
2f9be 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 29 0a 20 20  RM_CODED)==0).  
2f9bf 20 20 20 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e      && (pLevel->
2f9c0 69 4c 65 66 74 4a 6f 69 6e 3d 3d 30 20 7c 7c 20  iLeftJoin==0 || 
2f9c1 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2f9c2 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 45 50  pTerm->pExpr, EP
2f9c3 5f 46 72 6f 6d 4a 6f 69 6e 29 29 0a 20 20 29 7b  _FromJoin)).  ){
2f9c4 0a 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c  .    pTerm->wtFl
2f9c5 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45  ags |= TERM_CODE
2f9c6 44 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  D;.    if( pTerm
2f9c7 2d 3e 69 50 61 72 65 6e 74 3e 3d 30 20 29 7b 0a  ->iParent>=0 ){.
2f9c8 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
2f9c9 2a 70 4f 74 68 65 72 20 3d 20 26 70 54 65 72 6d  *pOther = &pTerm
2f9ca 2d 3e 70 57 43 2d 3e 61 5b 70 54 65 72 6d 2d 3e  ->pWC->a[pTerm->
2f9cb 69 50 61 72 65 6e 74 5d 3b 0a 20 20 20 20 20 20  iParent];.      
2f9cc 69 66 28 20 28 2d 2d 70 4f 74 68 65 72 2d 3e 6e  if( (--pOther->n
2f9cd 43 68 69 6c 64 29 3d 3d 30 20 29 7b 0a 20 20 20  Child)==0 ){.   
2f9ce 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d       disableTerm
2f9cf 28 70 4c 65 76 65 6c 2c 20 70 4f 74 68 65 72 29  (pLevel, pOther)
2f9d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2f9d1 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64    }.}../*.** Cod
2f9d2 65 20 61 6e 20 4f 50 5f 41 66 66 69 6e 69 74 79  e an OP_Affinity
2f9d3 20 6f 70 63 6f 64 65 20 74 6f 20 61 70 70 6c 79   opcode to apply
2f9d4 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   the column affi
2f9d5 6e 69 74 79 20 73 74 72 69 6e 67 20 7a 41 66 66  nity string zAff
2f9d6 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 20 72 65 67  .** to the n reg
2f9d7 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20  isters starting 
2f9d8 61 74 20 62 61 73 65 2e 20 0a 2a 2a 0a 2a 2a 20  at base. .**.** 
2f9d9 42 75 66 66 65 72 20 7a 41 66 66 20 77 61 73 20  Buffer zAff was 
2f9da 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20  allocated using 
2f9db 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28  sqlite3DbMalloc(
2f9dc 29 2e 20 49 74 20 69 73 20 74 68 65 20 0a 2a 2a  ). It is the .**
2f9dd 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
2f9de 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  of this function
2f9df 20 74 6f 20 61 72 72 61 6e 67 65 20 66 6f 72 20   to arrange for 
2f9e0 69 74 20 74 6f 20 62 65 20 65 76 65 6e 74 75 61  it to be eventua
2f9e1 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20 75 73 69  lly.** freed usi
2f9e2 6e 67 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  ng sqlite3DbFree
2f9e3 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ()..*/.static vo
2f9e4 69 64 20 63 6f 64 65 41 70 70 6c 79 41 66 66 69  id codeApplyAffi
2f9e5 6e 69 74 79 28 50 61 72 73 65 20 2a 70 50 61 72  nity(Parse *pPar
2f9e6 73 65 2c 20 69 6e 74 20 62 61 73 65 2c 20 69 6e  se, int base, in
2f9e7 74 20 6e 2c 20 63 68 61 72 20 2a 7a 41 66 66 29  t n, char *zAff)
2f9e8 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
2f9e9 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61  arse->pVdbe;.  a
2f9ea 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20  ssert( v!=0 );. 
2f9eb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2f9ec 70 32 28 76 2c 20 4f 50 5f 41 66 66 69 6e 69 74  p2(v, OP_Affinit
2f9ed 79 2c 20 62 61 73 65 2c 20 6e 29 3b 0a 20 20 73  y, base, n);.  s
2f9ee 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2f9ef 50 34 28 76 2c 20 2d 31 2c 20 7a 41 66 66 2c 20  P4(v, -1, zAff, 
2f9f0 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 73  P4_DYNAMIC);.  s
2f9f1 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
2f9f2 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
2f9f3 61 72 73 65 2c 20 62 61 73 65 2c 20 6e 29 3b 0a  arse, base, n);.
2f9f4 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  }.../*.** Genera
2f9f5 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69  te code for a si
2f9f6 6e 67 6c 65 20 65 71 75 61 6c 69 74 79 20 74 65  ngle equality te
2f9f7 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  rm of the WHERE 
2f9f8 63 6c 61 75 73 65 2e 20 20 41 6e 20 65 71 75 61  clause.  An equa
2f9f9 6c 69 74 79 0a 2a 2a 20 74 65 72 6d 20 63 61 6e  lity.** term can
2f9fa 20 62 65 20 65 69 74 68 65 72 20 58 3d 65 78 70   be either X=exp
2f9fb 72 20 6f 72 20 58 20 49 4e 20 28 2e 2e 2e 29 2e  r or X IN (...).
2f9fc 20 20 20 70 54 65 72 6d 20 69 73 20 74 68 65 20     pTerm is the 
2f9fd 74 65 72 6d 20 74 6f 20 62 65 20 0a 2a 2a 20 63  term to be .** c
2f9fe 6f 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oded..**.** The 
2f9ff 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 66 6f  current value fo
2fa00 72 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  r the constraint
2fa01 20 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67 69   is left in regi
2fa02 73 74 65 72 20 69 52 65 67 2e 0a 2a 2a 0a 2a 2a  ster iReg..**.**
2fa03 20 46 6f 72 20 61 20 63 6f 6e 73 74 72 61 69 6e   For a constrain
2fa04 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 3d  t of the form X=
2fa05 65 78 70 72 2c 20 74 68 65 20 65 78 70 72 65 73  expr, the expres
2fa06 73 69 6f 6e 20 69 73 20 65 76 61 6c 75 61 74 65  sion is evaluate
2fa07 64 20 61 6e 64 20 69 74 73 0a 2a 2a 20 72 65 73  d and its.** res
2fa08 75 6c 74 20 69 73 20 6c 65 66 74 20 6f 6e 20 74  ult is left on t
2fa09 68 65 20 73 74 61 63 6b 2e 20 20 46 6f 72 20 63  he stack.  For c
2fa0a 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68  onstraints of th
2fa0b 65 20 66 6f 72 6d 20 58 20 49 4e 20 28 2e 2e 2e  e form X IN (...
2fa0c 29 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  ).** this routin
2fa0d 65 20 73 65 74 73 20 75 70 20 61 20 6c 6f 6f 70  e sets up a loop
2fa0e 20 74 68 61 74 20 77 69 6c 6c 20 69 74 65 72 61   that will itera
2fa0f 74 65 20 6f 76 65 72 20 61 6c 6c 20 76 61 6c 75  te over all valu
2fa10 65 73 20 6f 66 20 58 2e 0a 2a 2f 0a 73 74 61 74  es of X..*/.stat
2fa11 69 63 20 69 6e 74 20 63 6f 64 65 45 71 75 61 6c  ic int codeEqual
2fa12 69 74 79 54 65 72 6d 28 0a 20 20 50 61 72 73 65  ityTerm(.  Parse
2fa13 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f   *pParse,      /
2fa14 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
2fa15 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65  ntext */.  Where
2fa16 54 65 72 6d 20 2a 70 54 65 72 6d 2c 20 20 20 2f  Term *pTerm,   /
2fa17 2a 20 54 68 65 20 74 65 72 6d 20 6f 66 20 74 68  * The term of th
2fa18 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  e WHERE clause t
2fa19 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20  o be coded */.  
2fa1a 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
2fa1b 65 6c 2c 20 2f 2a 20 57 68 65 6e 20 6c 65 76 65  el, /* When leve
2fa1c 6c 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  l of the FROM cl
2fa1d 61 75 73 65 20 77 65 20 61 72 65 20 77 6f 72 6b  ause we are work
2fa1e 69 6e 67 20 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ing on */.  int 
2fa1f 69 54 61 72 67 65 74 20 20 20 20 20 20 20 20 20  iTarget         
2fa20 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 65  /* Attempt to le
2fa21 61 76 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74  ave results in t
2fa22 68 69 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  his register */.
2fa23 29 7b 0a 20 20 45 78 70 72 20 2a 70 58 20 3d 20  ){.  Expr *pX = 
2fa24 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
2fa25 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
2fa26 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
2fa27 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Reg;            
2fa28 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
2fa29 72 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74  r holding result
2fa2a 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  s */..  assert( 
2fa2b 69 54 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 69  iTarget>0 );.  i
2fa2c 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 45 51  f( pX->op==TK_EQ
2fa2d 20 29 7b 0a 20 20 20 20 69 52 65 67 20 3d 20 73   ){.    iReg = s
2fa2e 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
2fa2f 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 58 2d  rget(pParse, pX-
2fa30 3e 70 52 69 67 68 74 2c 20 69 54 61 72 67 65 74  >pRight, iTarget
2fa31 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
2fa32 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c  X->op==TK_ISNULL
2fa33 20 29 7b 0a 20 20 20 20 69 52 65 67 20 3d 20 69   ){.    iReg = i
2fa34 54 61 72 67 65 74 3b 0a 20 20 20 20 73 71 6c 69  Target;.    sqli
2fa35 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2fa36 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 65   OP_Null, 0, iRe
2fa37 67 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  g);.#ifndef SQLI
2fa38 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
2fa39 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
2fa3a 74 20 65 54 79 70 65 3b 0a 20 20 20 20 69 6e 74  t eType;.    int
2fa3b 20 69 54 61 62 3b 0a 20 20 20 20 73 74 72 75 63   iTab;.    struc
2fa3c 74 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a 0a  t InLoop *pIn;..
2fa3d 20 20 20 20 61 73 73 65 72 74 28 20 70 58 2d 3e      assert( pX->
2fa3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20  op==TK_IN );.   
2fa3f 20 69 52 65 67 20 3d 20 69 54 61 72 67 65 74 3b   iReg = iTarget;
2fa40 0a 20 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c  .    eType = sql
2fa41 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28  ite3FindInIndex(
2fa42 70 50 61 72 73 65 2c 20 70 58 2c 20 30 29 3b 0a  pParse, pX, 0);.
2fa43 20 20 20 20 69 54 61 62 20 3d 20 70 58 2d 3e 69      iTab = pX->i
2fa44 54 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74  Table;.    sqlit
2fa45 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2fa46 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62 2c  OP_Rewind, iTab,
2fa47 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   0);.    assert(
2fa48 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2fa49 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
2fa4a 5f 41 42 4c 45 20 29 3b 0a 20 20 20 20 69 66 28  _ABLE );.    if(
2fa4b 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49   pLevel->u.in.nI
2fa4c 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c  n==0 ){.      pL
2fa4d 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 20 3d 20  evel->addrNxt = 
2fa4e 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2fa4f 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 7d 0a 20  abel(v);.    }. 
2fa50 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e     pLevel->u.in.
2fa51 6e 49 6e 2b 2b 3b 0a 20 20 20 20 70 4c 65 76 65  nIn++;.    pLeve
2fa52 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 20  l->u.in.aInLoop 
2fa53 3d 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  =.       sqlite3
2fa54 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
2fa55 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 65 76  pParse->db, pLev
2fa56 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70  el->u.in.aInLoop
2fa57 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2fa58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa59 73 69 7a 65 6f 66 28 70 4c 65 76 65 6c 2d 3e 75  sizeof(pLevel->u
2fa5a 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 5b 30 5d 29 2a  .in.aInLoop[0])*
2fa5b 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e  pLevel->u.in.nIn
2fa5c 29 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 4c 65  );.    pIn = pLe
2fa5d 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f  vel->u.in.aInLoo
2fa5e 70 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 20 29  p;.    if( pIn )
2fa5f 7b 0a 20 20 20 20 20 20 70 49 6e 20 2b 3d 20 70  {.      pIn += p
2fa60 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 20  Level->u.in.nIn 
2fa61 2d 20 31 3b 0a 20 20 20 20 20 20 70 49 6e 2d 3e  - 1;.      pIn->
2fa62 69 43 75 72 20 3d 20 69 54 61 62 3b 0a 20 20 20  iCur = iTab;.   
2fa63 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e     if( eType==IN
2fa64 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a  _INDEX_ROWID ){.
2fa65 20 20 20 20 20 20 20 20 70 49 6e 2d 3e 61 64 64          pIn->add
2fa66 72 49 6e 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  rInTop = sqlite3
2fa67 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2fa68 5f 52 6f 77 69 64 2c 20 69 54 61 62 2c 20 69 52  _Rowid, iTab, iR
2fa69 65 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  eg);.      }else
2fa6a 7b 0a 20 20 20 20 20 20 20 20 70 49 6e 2d 3e 61  {.        pIn->a
2fa6b 64 64 72 49 6e 54 6f 70 20 3d 20 73 71 6c 69 74  ddrInTop = sqlit
2fa6c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2fa6d 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c  OP_Column, iTab,
2fa6e 20 30 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20   0, iReg);.     
2fa6f 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2fa70 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2fa71 5f 49 73 4e 75 6c 6c 2c 20 69 52 65 67 29 3b 0a  _IsNull, iReg);.
2fa72 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2fa73 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49   pLevel->u.in.nI
2fa74 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 23 65 6e  n = 0;.    }.#en
2fa75 64 69 66 0a 20 20 7d 0a 20 20 64 69 73 61 62 6c  dif.  }.  disabl
2fa76 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 54  eTerm(pLevel, pT
2fa77 65 72 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  erm);.  return i
2fa78 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Reg;.}../*.** Ge
2fa79 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
2fa7a 20 77 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 61   will evaluate a
2fa7b 6c 6c 20 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e  ll == and IN con
2fa7c 73 74 72 61 69 6e 74 73 20 66 6f 72 20 61 6e 0a  straints for an.
2fa7d 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65 20 76  ** index.  The v
2fa7e 61 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 63 6f  alues for all co
2fa7f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 6c 65  nstraints are le
2fa80 66 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e  ft on the stack.
2fa81 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
2fa82 6c 65 2c 20 63 6f 6e 73 69 64 65 72 20 74 61 62  le, consider tab
2fa83 6c 65 20 74 31 28 61 2c 62 2c 63 2c 64 2c 65 2c  le t1(a,b,c,d,e,
2fa84 66 29 20 77 69 74 68 20 69 6e 64 65 78 20 69 31  f) with index i1
2fa85 28 61 2c 62 2c 63 29 2e 0a 2a 2a 20 53 75 70 70  (a,b,c)..** Supp
2fa86 6f 73 65 20 74 68 65 20 57 48 45 52 45 20 63 6c  ose the WHERE cl
2fa87 61 75 73 65 20 69 73 20 74 68 69 73 3a 20 20 61  ause is this:  a
2fa88 3d 3d 35 20 41 4e 44 20 62 20 49 4e 20 28 31 2c  ==5 AND b IN (1,
2fa89 32 2c 33 29 20 41 4e 44 20 63 3e 35 20 41 4e 44  2,3) AND c>5 AND
2fa8a 20 63 3c 31 30 0a 2a 2a 20 54 68 65 20 69 6e 64   c<10.** The ind
2fa8b 65 78 20 68 61 73 20 61 73 20 6d 61 6e 79 20 61  ex has as many a
2fa8c 73 20 74 68 72 65 65 20 65 71 75 61 6c 69 74 79  s three equality
2fa8d 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 62 75   constraints, bu
2fa8e 74 20 69 6e 20 74 68 69 73 0a 2a 2a 20 65 78 61  t in this.** exa
2fa8f 6d 70 6c 65 2c 20 74 68 65 20 74 68 69 72 64 20  mple, the third 
2fa90 22 63 22 20 76 61 6c 75 65 20 69 73 20 61 6e 20  "c" value is an 
2fa91 69 6e 65 71 75 61 6c 69 74 79 2e 20 20 53 6f 20  inequality.  So 
2fa92 6f 6e 6c 79 20 74 77 6f 20 0a 2a 2a 20 63 6f 6e  only two .** con
2fa93 73 74 72 61 69 6e 74 73 20 61 72 65 20 63 6f 64  straints are cod
2fa94 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ed.  This routin
2fa95 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74 65 20  e will generate 
2fa96 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65  code to evaluate
2fa97 0a 2a 2a 20 61 3d 3d 35 20 61 6e 64 20 62 20 49  .** a==5 and b I
2fa98 4e 20 28 31 2c 32 2c 33 29 2e 20 20 54 68 65 20  N (1,2,3).  The 
2fa99 63 75 72 72 65 6e 74 20 76 61 6c 75 65 73 20 66  current values f
2fa9a 6f 72 20 61 20 61 6e 64 20 62 20 77 69 6c 6c 20  or a and b will 
2fa9b 62 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  be stored.** in 
2fa9c 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 69  consecutive regi
2fa9d 73 74 65 72 73 20 61 6e 64 20 74 68 65 20 69 6e  sters and the in
2fa9e 64 65 78 20 6f 66 20 74 68 65 20 66 69 72 73 74  dex of the first
2fa9f 20 72 65 67 69 73 74 65 72 20 69 73 20 72 65 74   register is ret
2faa0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  urned..**.** In 
2faa1 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76  the example abov
2faa2 65 20 6e 45 71 3d 3d 32 2e 20 20 42 75 74 20 74  e nEq==2.  But t
2faa3 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 20 77  his subroutine w
2faa4 6f 72 6b 73 20 66 6f 72 20 61 6e 79 20 76 61 6c  orks for any val
2faa5 75 65 0a 2a 2a 20 6f 66 20 6e 45 71 20 69 6e 63  ue.** of nEq inc
2faa6 6c 75 64 69 6e 67 20 30 2e 20 20 49 66 20 6e 45  luding 0.  If nE
2faa7 71 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69  q==0, this routi
2faa8 6e 65 20 69 73 20 6e 65 61 72 6c 79 20 61 20 6e  ne is nearly a n
2faa9 6f 2d 6f 70 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c  o-op..** The onl
2faaa 79 20 74 68 69 6e 67 20 69 74 20 64 6f 65 73 20  y thing it does 
2faab 69 73 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20  is allocate the 
2faac 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 6d 65 6d  pLevel->iMem mem
2faad 6f 72 79 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  ory cell..**.** 
2faae 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77  This routine alw
2faaf 61 79 73 20 61 6c 6c 6f 63 61 74 65 73 20 61 74  ays allocates at
2fab0 20 6c 65 61 73 74 20 6f 6e 65 20 6d 65 6d 6f 72   least one memor
2fab1 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 74 75 72  y cell and retur
2fab2 6e 73 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  ns.** the index 
2fab3 6f 66 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 63  of that memory c
2fab4 65 6c 6c 2e 20 54 68 65 20 63 6f 64 65 20 74 68  ell. The code th
2fab5 61 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 68 69 73  at.** calls this
2fab6 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 75 73   routine will us
2fab7 65 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 63 65  e that memory ce
2fab8 6c 6c 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ll to store the 
2fab9 74 65 72 6d 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6b  termination.** k
2faba 65 79 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ey value of the 
2fabb 6c 6f 6f 70 2e 20 20 49 66 20 6f 6e 65 20 6f 72  loop.  If one or
2fabc 20 6d 6f 72 65 20 49 4e 20 6f 70 65 72 61 74 6f   more IN operato
2fabd 72 73 20 61 70 70 65 61 72 2c 20 74 68 65 6e 0a  rs appear, then.
2fabe 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
2fabf 61 6c 6c 6f 63 61 74 65 73 20 61 6e 20 61 64 64  allocates an add
2fac0 69 74 69 6f 6e 61 6c 20 6e 45 71 20 6d 65 6d 6f  itional nEq memo
2fac1 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 69 6e 74  ry cells for int
2fac2 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 2e 0a 2a 2a  ernal.** use..**
2fac3 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72  .** Before retur
2fac4 6e 69 6e 67 2c 20 2a 70 7a 41 66 66 20 69 73 20  ning, *pzAff is 
2fac5 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  set to point to 
2fac6 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e  a buffer contain
2fac7 69 6e 67 20 61 0a 2a 2a 20 63 6f 70 79 20 6f 66  ing a.** copy of
2fac8 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   the column affi
2fac9 6e 69 74 79 20 73 74 72 69 6e 67 20 6f 66 20 74  nity string of t
2faca 68 65 20 69 6e 64 65 78 20 61 6c 6c 6f 63 61 74  he index allocat
2facb 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69  ed using.** sqli
2facc 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2e 20 45  te3DbMalloc(). E
2facd 78 63 65 70 74 2c 20 65 6e 74 72 69 65 73 20 69  xcept, entries i
2face 6e 20 74 68 65 20 63 6f 70 79 20 6f 66 20 74 68  n the copy of th
2facf 65 20 73 74 72 69 6e 67 20 61 73 73 6f 63 69 61  e string associa
2fad0 74 65 64 0a 2a 2a 20 77 69 74 68 20 65 71 75 61  ted.** with equa
2fad1 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2fad2 20 74 68 61 74 20 75 73 65 20 4e 4f 4e 45 20 61   that use NONE a
2fad3 66 66 69 6e 69 74 79 20 61 72 65 20 73 65 74 20  ffinity are set 
2fad4 74 6f 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46  to.** SQLITE_AFF
2fad5 5f 4e 4f 4e 45 2e 20 54 68 69 73 20 69 73 20 74  _NONE. This is t
2fad6 6f 20 64 65 61 6c 20 77 69 74 68 20 53 51 4c 20  o deal with SQL 
2fad7 73 75 63 68 20 61 73 20 74 68 65 20 66 6f 6c 6c  such as the foll
2fad8 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 43  owing:.**.**   C
2fad9 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61  REATE TABLE t1(a
2fada 20 54 45 58 54 20 50 52 49 4d 41 52 59 20 4b 45   TEXT PRIMARY KE
2fadb 59 2c 20 62 29 3b 0a 2a 2a 20 20 20 53 45 4c 45  Y, b);.**   SELE
2fadc 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 41  CT ... FROM t1 A
2fadd 53 20 74 32 2c 20 74 31 20 57 48 45 52 45 20 74  S t2, t1 WHERE t
2fade 31 2e 61 20 3d 20 74 32 2e 62 3b 0a 2a 2a 0a 2a  1.a = t2.b;.**.*
2fadf 2a 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65  * In the example
2fae0 20 61 62 6f 76 65 2c 20 74 68 65 20 69 6e 64 65   above, the inde
2fae1 78 20 6f 6e 20 74 31 28 61 29 20 68 61 73 20 54  x on t1(a) has T
2fae2 45 58 54 20 61 66 66 69 6e 69 74 79 2e 20 42 75  EXT affinity. Bu
2fae3 74 20 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 72  t since.** the r
2fae4 69 67 68 74 20 68 61 6e 64 20 73 69 64 65 20 6f  ight hand side o
2fae5 66 20 74 68 65 20 65 71 75 61 6c 69 74 79 20 63  f the equality c
2fae6 6f 6e 73 74 72 61 69 6e 74 20 28 74 32 2e 62 29  onstraint (t2.b)
2fae7 20 68 61 73 20 4e 4f 4e 45 20 61 66 66 69 6e 69   has NONE affini
2fae8 74 79 2c 0a 2a 2a 20 6e 6f 20 63 6f 6e 76 65 72  ty,.** no conver
2fae9 73 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 61  sion should be a
2faea 74 74 65 6d 70 74 65 64 20 62 65 66 6f 72 65 20  ttempted before 
2faeb 75 73 69 6e 67 20 61 20 74 32 2e 62 20 76 61 6c  using a t2.b val
2faec 75 65 20 61 73 20 70 61 72 74 20 6f 66 0a 2a 2a  ue as part of.**
2faed 20 61 20 6b 65 79 20 74 6f 20 73 65 61 72 63 68   a key to search
2faee 20 74 68 65 20 69 6e 64 65 78 2e 20 48 65 6e 63   the index. Henc
2faef 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  e the first byte
2faf0 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64   in the returned
2faf1 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 73 74 72   affinity.** str
2faf2 69 6e 67 20 69 6e 20 74 68 69 73 20 65 78 61 6d  ing in this exam
2faf3 70 6c 65 20 77 6f 75 6c 64 20 62 65 20 73 65 74  ple would be set
2faf4 20 74 6f 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e   to SQLITE_AFF_N
2faf5 4f 4e 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ONE..*/.static i
2faf6 6e 74 20 63 6f 64 65 41 6c 6c 45 71 75 61 6c 69  nt codeAllEquali
2faf7 74 79 54 65 72 6d 73 28 0a 20 20 50 61 72 73 65  tyTerms(.  Parse
2faf8 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2faf9 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2fafa 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65  ext */.  WhereLe
2fafb 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 20 20 2f  vel *pLevel,   /
2fafc 2a 20 57 68 69 63 68 20 6e 65 73 74 65 64 20 6c  * Which nested l
2fafd 6f 6f 70 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  oop of the FROM 
2fafe 77 65 20 61 72 65 20 63 6f 64 69 6e 67 20 2a 2f  we are coding */
2faff 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2fb00 70 57 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  pWC,     /* The 
2fb01 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2fb02 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
2fb03 64 79 2c 20 20 20 20 20 2f 2a 20 57 68 69 63 68  dy,     /* Which
2fb04 20 70 61 72 74 73 20 6f 66 20 46 52 4f 4d 20 68   parts of FROM h
2fb05 61 76 65 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  ave not yet been
2fb06 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   coded */.  int 
2fb07 6e 45 78 74 72 61 52 65 67 2c 20 20 20 20 20 20  nExtraReg,      
2fb08 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
2fb09 78 74 72 61 20 72 65 67 69 73 74 65 72 73 20 74  xtra registers t
2fb0a 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20  o allocate */.  
2fb0b 63 68 61 72 20 2a 2a 70 7a 41 66 66 20 20 20 20  char **pzAff    
2fb0c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65        /* OUT: Se
2fb0d 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 66  t to point to af
2fb0e 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 2a 2f  finity string */
2fb0f 0a 29 7b 0a 20 20 69 6e 74 20 6e 45 71 20 3d 20  .){.  int nEq = 
2fb10 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71  pLevel->plan.nEq
2fb11 3b 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65  ;   /* The numbe
2fb12 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f  r of == or IN co
2fb13 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 63 6f 64  nstraints to cod
2fb14 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d  e */.  Vdbe *v =
2fb15 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20   pParse->pVdbe; 
2fb16 20 20 20 20 20 2f 2a 20 54 68 65 20 76 6d 20 75       /* The vm u
2fb17 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
2fb18 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  n */.  Index *pI
2fb19 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
2fb1a 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65       /* The inde
2fb1b 78 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72  x being used for
2fb1c 20 74 68 69 73 20 6c 6f 6f 70 20 2a 2f 0a 20 20   this loop */.  
2fb1d 69 6e 74 20 69 43 75 72 20 3d 20 70 4c 65 76 65  int iCur = pLeve
2fb1e 6c 2d 3e 69 54 61 62 43 75 72 3b 20 20 20 2f 2a  l->iTabCur;   /*
2fb1f 20 54 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74   The cursor of t
2fb20 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 57 68  he table */.  Wh
2fb21 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20  ereTerm *pTerm; 
2fb22 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2fb23 20 73 69 6e 67 6c 65 20 63 6f 6e 73 74 72 61 69   single constrai
2fb24 6e 74 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74  nt term */.  int
2fb25 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
2fb26 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2fb27 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
2fb28 69 6e 74 20 72 65 67 42 61 73 65 3b 20 20 20 20  int regBase;    
2fb29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2fb2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72 20 2a   Base register *
2fb2b 2f 0a 20 20 69 6e 74 20 6e 52 65 67 3b 20 20 20  /.  int nReg;   
2fb2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb2d 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
2fb2e 65 67 69 73 74 65 72 73 20 74 6f 20 61 6c 6c 6f  egisters to allo
2fb2f 63 61 74 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  cate */.  char *
2fb30 7a 41 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  zAff;           
2fb31 20 20 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e          /* Affin
2fb32 69 74 79 20 73 74 72 69 6e 67 20 74 6f 20 72 65  ity string to re
2fb33 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68  turn */..  /* Th
2fb34 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 6f 6e 6c  is module is onl
2fb35 79 20 63 61 6c 6c 65 64 20 6f 6e 20 71 75 65 72  y called on quer
2fb36 79 20 70 6c 61 6e 73 20 74 68 61 74 20 75 73 65  y plans that use
2fb37 20 61 6e 20 69 6e 64 65 78 2e 20 2a 2f 0a 20 20   an index. */.  
2fb38 61 73 73 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e  assert( pLevel->
2fb39 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2fb3a 48 45 52 45 5f 49 4e 44 45 58 45 44 20 29 3b 0a  HERE_INDEXED );.
2fb3b 20 20 70 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d    pIdx = pLevel-
2fb3c 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 0a 20  >plan.u.pIdx;.. 
2fb3d 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
2fb3e 6f 77 20 6d 61 6e 79 20 6d 65 6d 6f 72 79 20 63  ow many memory c
2fb3f 65 6c 6c 73 20 77 65 20 77 69 6c 6c 20 6e 65 65  ells we will nee
2fb40 64 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20  d then allocate 
2fb41 74 68 65 6d 2e 0a 20 20 2a 2f 0a 20 20 72 65 67  them..  */.  reg
2fb42 42 61 73 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e  Base = pParse->n
2fb43 4d 65 6d 20 2b 20 31 3b 0a 20 20 6e 52 65 67 20  Mem + 1;.  nReg 
2fb44 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e  = pLevel->plan.n
2fb45 45 71 20 2b 20 6e 45 78 74 72 61 52 65 67 3b 0a  Eq + nExtraReg;.
2fb46 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
2fb47 3d 20 6e 52 65 67 3b 0a 0a 20 20 7a 41 66 66 20  = nReg;..  zAff 
2fb48 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
2fb49 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 71  p(pParse->db, sq
2fb4a 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69  lite3IndexAffini
2fb4b 74 79 53 74 72 28 76 2c 20 70 49 64 78 29 29 3b  tyStr(v, pIdx));
2fb4c 0a 20 20 69 66 28 20 21 7a 41 66 66 20 29 7b 0a  .  if( !zAff ){.
2fb4d 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e      pParse->db->
2fb4e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
2fb4f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 76 61 6c  ;.  }..  /* Eval
2fb50 75 61 74 65 20 74 68 65 20 65 71 75 61 6c 69 74  uate the equalit
2fb51 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20  y constraints.  
2fb52 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 64  */.  assert( pId
2fb53 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6e 45 71 20  x->nColumn>=nEq 
2fb54 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  );.  for(j=0; j<
2fb55 6e 45 71 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69  nEq; j++){.    i
2fb56 6e 74 20 72 31 3b 0a 20 20 20 20 69 6e 74 20 6b  nt r1;.    int k
2fb57 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d   = pIdx->aiColum
2fb58 6e 5b 6a 5d 3b 0a 20 20 20 20 70 54 65 72 6d 20  n[j];.    pTerm 
2fb59 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
2fb5a 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64  iCur, k, notRead
2fb5b 79 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  y, pLevel->plan.
2fb5c 77 73 46 6c 61 67 73 2c 20 70 49 64 78 29 3b 0a  wsFlags, pIdx);.
2fb5d 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 70 54      if( NEVER(pT
2fb5e 65 72 6d 3d 3d 30 29 20 29 20 62 72 65 61 6b 3b  erm==0) ) break;
2fb5f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54  .    assert( (pT
2fb60 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
2fb61 45 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 20 29 3b  ERM_CODED)==0 );
2fb62 0a 20 20 20 20 72 31 20 3d 20 63 6f 64 65 45 71  .    r1 = codeEq
2fb63 75 61 6c 69 74 79 54 65 72 6d 28 70 50 61 72 73  ualityTerm(pPars
2fb64 65 2c 20 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c  e, pTerm, pLevel
2fb65 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20  , regBase+j);.  
2fb66 20 20 69 66 28 20 72 31 21 3d 72 65 67 42 61 73    if( r1!=regBas
2fb67 65 2b 6a 20 29 7b 0a 20 20 20 20 20 20 69 66 28  e+j ){.      if(
2fb68 20 6e 52 65 67 3d 3d 31 20 29 7b 0a 20 20 20 20   nReg==1 ){.    
2fb69 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
2fb6a 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2fb6b 2c 20 72 65 67 42 61 73 65 29 3b 0a 20 20 20 20  , regBase);.    
2fb6c 20 20 20 20 72 65 67 42 61 73 65 20 3d 20 72 31      regBase = r1
2fb6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2fb6e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2fb6f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
2fb70 43 6f 70 79 2c 20 72 31 2c 20 72 65 67 42 61 73  Copy, r1, regBas
2fb71 65 2b 6a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  e+j);.      }.  
2fb72 20 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65    }.    testcase
2fb73 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  ( pTerm->eOperat
2fb74 6f 72 20 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29  or & WO_ISNULL )
2fb75 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2fb76 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2fb77 20 26 20 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20   & WO_IN );.    
2fb78 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65  if( (pTerm->eOpe
2fb79 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 53 4e 55  rator & (WO_ISNU
2fb7a 4c 4c 7c 57 4f 5f 49 4e 29 29 3d 3d 30 20 29 7b  LL|WO_IN))==0 ){
2fb7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2fb7c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2fb7d 73 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b 6a  sNull, regBase+j
2fb7e 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72  , pLevel->addrBr
2fb7f 6b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 41  k);.      if( zA
2fb80 66 66 20 0a 20 20 20 20 20 20 20 26 26 20 73 71  ff .       && sq
2fb81 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
2fb82 6e 69 74 79 28 70 54 65 72 6d 2d 3e 70 45 78 70  nity(pTerm->pExp
2fb83 72 2d 3e 70 52 69 67 68 74 2c 20 7a 41 66 66 5b  r->pRight, zAff[
2fb84 6a 5d 29 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  j])==SQLITE_AFF_
2fb85 4e 4f 4e 45 0a 20 20 20 20 20 20 29 7b 0a 20 20  NONE.      ){.  
2fb86 20 20 20 20 20 20 7a 41 66 66 5b 6a 5d 20 3d 20        zAff[j] = 
2fb87 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
2fb88 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2fb89 20 7d 0a 20 20 2a 70 7a 41 66 66 20 3d 20 7a 41   }.  *pzAff = zA
2fb8a 66 66 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 67  ff;.  return reg
2fb8b 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  Base;.}../*.** G
2fb8c 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
2fb8d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
2fb8e 65 20 69 4c 65 76 65 6c 2d 74 68 20 6c 6f 6f 70  e iLevel-th loop
2fb8f 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
2fb90 61 75 73 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  ause.** implemen
2fb91 74 61 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64  tation described
2fb92 20 62 79 20 70 57 49 6e 66 6f 2e 0a 2a 2f 0a 73   by pWInfo..*/.s
2fb93 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 63 6f  tatic Bitmask co
2fb94 64 65 4f 6e 65 4c 6f 6f 70 53 74 61 72 74 28 0a  deOneLoopStart(.
2fb95 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49    WhereInfo *pWI
2fb96 6e 66 6f 2c 20 20 20 2f 2a 20 43 6f 6d 70 6c 65  nfo,   /* Comple
2fb97 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  te information a
2fb98 62 6f 75 74 20 74 68 65 20 57 48 45 52 45 20 63  bout the WHERE c
2fb99 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lause */.  int i
2fb9a 4c 65 76 65 6c 2c 20 20 20 20 20 20 20 20 20 20  Level,          
2fb9b 2f 2a 20 57 68 69 63 68 20 6c 65 76 65 6c 20 6f  /* Which level o
2fb9c 66 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 20 73 68  f pWInfo->a[] sh
2fb9d 6f 75 6c 64 20 62 65 20 63 6f 64 65 64 20 2a 2f  ould be coded */
2fb9e 0a 20 20 75 31 36 20 77 63 74 72 6c 46 6c 61 67  .  u16 wctrlFlag
2fb9f 73 2c 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f  s,      /* One o
2fba0 66 20 74 68 65 20 57 48 45 52 45 5f 2a 20 66 6c  f the WHERE_* fl
2fba1 61 67 73 20 64 65 66 69 6e 65 64 20 69 6e 20 73  ags defined in s
2fba2 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2f 0a 20 20  qliteInt.h */.  
2fba3 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
2fba4 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 74 61       /* Which ta
2fba5 62 6c 65 73 20 61 72 65 20 63 75 72 72 65 6e 74  bles are current
2fba6 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a  ly available */.
2fba7 29 7b 0a 20 20 69 6e 74 20 6a 2c 20 6b 3b 20 20  ){.  int j, k;  
2fba8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2fba9 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
2fbaa 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20  int iCur;       
2fbab 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45       /* The VDBE
2fbac 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20   cursor for the 
2fbad 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61  table */.  int a
2fbae 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20 20 20  ddrNxt;         
2fbaf 2f 2a 20 57 68 65 72 65 20 74 6f 20 6a 75 6d 70  /* Where to jump
2fbb0 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
2fbb1 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20 63 61  h the next IN ca
2fbb2 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6d 69 74  se */.  int omit
2fbb3 54 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20  Table;       /* 
2fbb4 54 72 75 65 20 69 66 20 77 65 20 75 73 65 20 74  True if we use t
2fbb5 68 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2a 2f  he index only */
2fbb6 0a 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20  .  int bRev;    
2fbb7 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2fbb8 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 63  if we need to sc
2fbb9 61 6e 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72  an in reverse or
2fbba 64 65 72 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65  der */.  WhereLe
2fbbb 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 20 20 2f 2a  vel *pLevel;  /*
2fbbc 20 54 68 65 20 77 68 65 72 65 20 6c 65 76 65 6c   The where level
2fbbd 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a   to be coded */.
2fbbe 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
2fbbf 57 43 3b 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70  WC;    /* Decomp
2fbc0 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 65  osition of the e
2fbc1 6e 74 69 72 65 20 57 48 45 52 45 20 63 6c 61 75  ntire WHERE clau
2fbc2 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  se */.  WhereTer
2fbc3 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20  m *pTerm;       
2fbc4 20 20 20 20 20 20 20 20 2f 2a 20 41 20 57 48 45          /* A WHE
2fbc5 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20 2a  RE clause term *
2fbc6 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  /.  Parse *pPars
2fbc7 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
2fbc8 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2fbc9 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 56 64 62 65  ontext */.  Vdbe
2fbca 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
2fbcb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2fbcc 68 65 20 70 72 65 70 61 72 65 64 20 73 74 6d 74  he prepared stmt
2fbcd 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
2fbce 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  ions */.  struct
2fbcf 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
2fbd0 54 61 62 49 74 65 6d 3b 20 20 2f 2a 20 46 52 4f  TabItem;  /* FRO
2fbd1 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20 62 65  M clause term be
2fbd2 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69  ing coded */.  i
2fbd3 6e 74 20 61 64 64 72 42 72 6b 3b 20 20 20 20 20  nt addrBrk;     
2fbd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2fbd5 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62  * Jump here to b
2fbd6 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20  reak out of the 
2fbd7 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64  loop */.  int ad
2fbd8 64 72 43 6f 6e 74 3b 20 20 20 20 20 20 20 20 20  drCont;         
2fbd9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
2fbda 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e  p here to contin
2fbdb 75 65 20 77 69 74 68 20 6e 65 78 74 20 63 79 63  ue with next cyc
2fbdc 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 77  le */.  int iRow
2fbdd 69 64 52 65 67 20 3d 20 30 3b 20 20 20 20 20 20  idReg = 0;      
2fbde 20 20 2f 2a 20 52 6f 77 69 64 20 69 73 20 73 74    /* Rowid is st
2fbdf 6f 72 65 64 20 69 6e 20 74 68 69 73 20 72 65 67  ored in this reg
2fbe0 69 73 74 65 72 2c 20 69 66 20 6e 6f 74 20 7a 65  ister, if not ze
2fbe1 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 6c  ro */.  int iRel
2fbe2 65 61 73 65 52 65 67 20 3d 20 30 3b 20 20 20 20  easeReg = 0;    
2fbe3 20 20 2f 2a 20 54 65 6d 70 20 72 65 67 69 73 74    /* Temp regist
2fbe4 65 72 20 74 6f 20 66 72 65 65 20 62 65 66 6f 72  er to free befor
2fbe5 65 20 72 65 74 75 72 6e 69 6e 67 20 2a 2f 0a 0a  e returning */..
2fbe6 20 20 70 50 61 72 73 65 20 3d 20 70 57 49 6e 66    pParse = pWInf
2fbe7 6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20 76 20 3d  o->pParse;.  v =
2fbe8 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
2fbe9 20 20 70 57 43 20 3d 20 70 57 49 6e 66 6f 2d 3e    pWC = pWInfo->
2fbea 70 57 43 3b 0a 20 20 70 4c 65 76 65 6c 20 3d 20  pWC;.  pLevel = 
2fbeb 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 4c 65 76 65  &pWInfo->a[iLeve
2fbec 6c 5d 3b 0a 20 20 70 54 61 62 49 74 65 6d 20 3d  l];.  pTabItem =
2fbed 20 26 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69   &pWInfo->pTabLi
2fbee 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46  st->a[pLevel->iF
2fbef 72 6f 6d 5d 3b 0a 20 20 69 43 75 72 20 3d 20 70  rom];.  iCur = p
2fbf0 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  TabItem->iCursor
2fbf1 3b 0a 20 20 62 52 65 76 20 3d 20 28 70 4c 65 76  ;.  bRev = (pLev
2fbf2 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2fbf3 20 26 20 57 48 45 52 45 5f 52 45 56 45 52 53 45   & WHERE_REVERSE
2fbf4 29 21 3d 30 3b 0a 20 20 6f 6d 69 74 54 61 62 6c  )!=0;.  omitTabl
2fbf5 65 20 3d 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  e = (pLevel->pla
2fbf6 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2fbf7 45 5f 49 44 58 5f 4f 4e 4c 59 29 21 3d 30 20 0a  E_IDX_ONLY)!=0 .
2fbf8 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 77             && (w
2fbf9 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52  ctrlFlags & WHER
2fbfa 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45 29 3d 3d  E_FORCE_TABLE)==
2fbfb 30 3b 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20  0;..  /* Create 
2fbfc 6c 61 62 65 6c 73 20 66 6f 72 20 74 68 65 20 22  labels for the "
2fbfd 62 72 65 61 6b 22 20 61 6e 64 20 22 63 6f 6e 74  break" and "cont
2fbfe 69 6e 75 65 22 20 69 6e 73 74 72 75 63 74 69 6f  inue" instructio
2fbff 6e 73 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20  ns.  ** for the 
2fc00 63 75 72 72 65 6e 74 20 6c 6f 6f 70 2e 20 20 4a  current loop.  J
2fc01 75 6d 70 20 74 6f 20 61 64 64 72 42 72 6b 20 74  ump to addrBrk t
2fc02 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 61  o break out of a
2fc03 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 4a 75 6d 70   loop..  ** Jump
2fc04 20 74 6f 20 63 6f 6e 74 20 74 6f 20 67 6f 20 69   to cont to go i
2fc05 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 74 68  mmediately to th
2fc06 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e  e next iteration
2fc07 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 6f   of the.  ** loo
2fc08 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65  p..  **.  ** Whe
2fc09 6e 20 74 68 65 72 65 20 69 73 20 61 6e 20 49 4e  n there is an IN
2fc0a 20 6f 70 65 72 61 74 6f 72 2c 20 77 65 20 61 6c   operator, we al
2fc0b 73 6f 20 68 61 76 65 20 61 20 22 61 64 64 72 4e  so have a "addrN
2fc0c 78 74 22 20 6c 61 62 65 6c 20 74 68 61 74 0a 20  xt" label that. 
2fc0d 20 2a 2a 20 6d 65 61 6e 73 20 74 6f 20 63 6f 6e   ** means to con
2fc0e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e  tinue with the n
2fc0f 65 78 74 20 49 4e 20 76 61 6c 75 65 20 63 6f 6d  ext IN value com
2fc10 62 69 6e 61 74 69 6f 6e 2e 20 20 57 68 65 6e 0a  bination.  When.
2fc11 20 20 2a 2a 20 74 68 65 72 65 20 61 72 65 20 6e    ** there are n
2fc12 6f 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 69  o IN operators i
2fc13 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  n the constraint
2fc14 73 2c 20 74 68 65 20 22 61 64 64 72 4e 78 74 22  s, the "addrNxt"
2fc15 20 6c 61 62 65 6c 0a 20 20 2a 2a 20 69 73 20 74   label.  ** is t
2fc16 68 65 20 73 61 6d 65 20 61 73 20 22 61 64 64 72  he same as "addr
2fc17 42 72 6b 22 2e 0a 20 20 2a 2f 0a 20 20 61 64 64  Brk"..  */.  add
2fc18 72 42 72 6b 20 3d 20 70 4c 65 76 65 6c 2d 3e 61  rBrk = pLevel->a
2fc19 64 64 72 42 72 6b 20 3d 20 70 4c 65 76 65 6c 2d  ddrBrk = pLevel-
2fc1a 3e 61 64 64 72 4e 78 74 20 3d 20 73 71 6c 69 74  >addrNxt = sqlit
2fc1b 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2fc1c 76 29 3b 0a 20 20 61 64 64 72 43 6f 6e 74 20 3d  v);.  addrCont =
2fc1d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 43 6f 6e   pLevel->addrCon
2fc1e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  t = sqlite3VdbeM
2fc1f 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20  akeLabel(v);..  
2fc20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68  /* If this is th
2fc21 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66  e right table of
2fc22 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f   a LEFT OUTER JO
2fc23 49 4e 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64  IN, allocate and
2fc24 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65  .  ** initialize
2fc25 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74   a memory cell t
2fc26 68 61 74 20 72 65 63 6f 72 64 73 20 69 66 20 74  hat records if t
2fc27 68 69 73 20 74 61 62 6c 65 20 6d 61 74 63 68 65  his table matche
2fc28 73 20 61 6e 79 0a 20 20 2a 2a 20 72 6f 77 20 6f  s any.  ** row o
2fc29 66 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65  f the left table
2fc2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20   of the join..  
2fc2b 2a 2f 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  */.  if( pLevel-
2fc2c 3e 69 46 72 6f 6d 3e 30 20 26 26 20 28 70 54 61  >iFrom>0 && (pTa
2fc2d 62 49 74 65 6d 5b 30 5d 2e 6a 6f 69 6e 74 79 70  bItem[0].jointyp
2fc2e 65 20 26 20 4a 54 5f 4c 45 46 54 29 21 3d 30 20  e & JT_LEFT)!=0 
2fc2f 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69  ){.    pLevel->i
2fc30 4c 65 66 74 4a 6f 69 6e 20 3d 20 2b 2b 70 50 61  LeftJoin = ++pPa
2fc31 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73  rse->nMem;.    s
2fc32 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2fc33 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2fc34 30 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74  0, pLevel->iLeft
2fc35 4a 6f 69 6e 29 3b 0a 20 20 20 20 56 64 62 65 43  Join);.    VdbeC
2fc36 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e 69 74  omment((v, "init
2fc37 20 4c 45 46 54 20 4a 4f 49 4e 20 6e 6f 2d 6d 61   LEFT JOIN no-ma
2fc38 74 63 68 20 66 6c 61 67 22 29 29 3b 0a 20 20 7d  tch flag"));.  }
2fc39 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2fc3a 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2fc3b 4c 45 0a 20 20 69 66 28 20 20 28 70 4c 65 76 65  LE.  if(  (pLeve
2fc3c 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2fc3d 26 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  & WHERE_VIRTUALT
2fc3e 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20 20 20  ABLE)!=0 ){.    
2fc3f 2f 2a 20 43 61 73 65 20 30 3a 20 20 54 68 65 20  /* Case 0:  The 
2fc40 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 75  table is a virtu
2fc41 61 6c 2d 74 61 62 6c 65 2e 20 20 55 73 65 20 74  al-table.  Use t
2fc42 68 65 20 56 46 69 6c 74 65 72 20 61 6e 64 20 56  he VFilter and V
2fc43 4e 65 78 74 0a 20 20 20 20 2a 2a 20 20 20 20 20  Next.    **     
2fc44 20 20 20 20 20 74 6f 20 61 63 63 65 73 73 20 74       to access t
2fc45 68 65 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a  he data..    */.
2fc46 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20      int iReg;   
2fc47 2f 2a 20 50 33 20 56 61 6c 75 65 20 66 6f 72 20  /* P3 Value for 
2fc48 4f 50 5f 56 46 69 6c 74 65 72 20 2a 2f 0a 20 20  OP_VFilter */.  
2fc49 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
2fc4a 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 20 3d  info *pVtabIdx =
2fc4b 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
2fc4c 70 56 74 61 62 49 64 78 3b 0a 20 20 20 20 69 6e  pVtabIdx;.    in
2fc4d 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20  t nConstraint = 
2fc4e 70 56 74 61 62 49 64 78 2d 3e 6e 43 6f 6e 73 74  pVtabIdx->nConst
2fc4f 72 61 69 6e 74 3b 0a 20 20 20 20 73 74 72 75 63  raint;.    struc
2fc50 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2fc51 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
2fc52 20 2a 61 55 73 61 67 65 20 3d 0a 20 20 20 20 20   *aUsage =.     
2fc53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc55 20 20 20 20 20 20 20 20 20 20 20 70 56 74 61 62             pVtab
2fc56 49 64 78 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  Idx->aConstraint
2fc57 55 73 61 67 65 3b 0a 20 20 20 20 63 6f 6e 73 74  Usage;.    const
2fc58 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
2fc59 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
2fc5a 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 0a   *aConstraint =.
2fc5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc5e 70 56 74 61 62 49 64 78 2d 3e 61 43 6f 6e 73 74  pVtabIdx->aConst
2fc5f 72 61 69 6e 74 3b 0a 0a 20 20 20 20 69 52 65 67  raint;..    iReg
2fc60 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2fc61 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e  pRange(pParse, n
2fc62 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b 0a 20  Constraint+2);. 
2fc63 20 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c 3d 6e     for(j=1; j<=n
2fc64 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a 2b 2b 29  Constraint; j++)
2fc65 7b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b  {.      for(k=0;
2fc66 20 6b 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20   k<nConstraint; 
2fc67 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  k++){.        if
2fc68 28 20 61 55 73 61 67 65 5b 6b 5d 2e 61 72 67 76  ( aUsage[k].argv
2fc69 49 6e 64 65 78 3d 3d 6a 20 29 7b 0a 20 20 20 20  Index==j ){.    
2fc6a 20 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d 20        int iTerm 
2fc6b 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6b 5d  = aConstraint[k]
2fc6c 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20  .iTermOffset;.  
2fc6d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2fc6e 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
2fc6f 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d 2e 70 45  pWC->a[iTerm].pE
2fc70 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 69 52 65  xpr->pRight, iRe
2fc71 67 2b 6a 2b 31 29 3b 0a 20 20 20 20 20 20 20 20  g+j+1);.        
2fc72 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
2fc73 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
2fc74 20 69 66 28 20 6b 3d 3d 6e 43 6f 6e 73 74 72 61   if( k==nConstra
2fc75 69 6e 74 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  int ) break;.   
2fc76 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
2fc77 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2fc78 6e 74 65 67 65 72 2c 20 70 56 74 61 62 49 64 78  nteger, pVtabIdx
2fc79 2d 3e 69 64 78 4e 75 6d 2c 20 69 52 65 67 29 3b  ->idxNum, iReg);
2fc7a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2fc7b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2fc7c 65 67 65 72 2c 20 6a 2d 31 2c 20 69 52 65 67 2b  eger, j-1, iReg+
2fc7d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
2fc7e 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2fc7f 56 46 69 6c 74 65 72 2c 20 69 43 75 72 2c 20 61  VFilter, iCur, a
2fc80 64 64 72 42 72 6b 2c 20 69 52 65 67 2c 20 70 56  ddrBrk, iReg, pV
2fc81 74 61 62 49 64 78 2d 3e 69 64 78 53 74 72 2c 0a  tabIdx->idxStr,.
2fc82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc83 20 20 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e        pVtabIdx->
2fc84 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
2fc85 20 3f 20 50 34 5f 4d 50 52 49 4e 54 46 20 3a 20   ? P4_MPRINTF : 
2fc86 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
2fc87 70 56 74 61 62 49 64 78 2d 3e 6e 65 65 64 54 6f  pVtabIdx->needTo
2fc88 46 72 65 65 49 64 78 53 74 72 20 3d 20 30 3b 0a  FreeIdxStr = 0;.
2fc89 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e      for(j=0; j<n
2fc8a 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a 2b 2b 29  Constraint; j++)
2fc8b 7b 0a 20 20 20 20 20 20 69 66 28 20 61 55 73 61  {.      if( aUsa
2fc8c 67 65 5b 6a 5d 2e 6f 6d 69 74 20 29 7b 0a 20 20  ge[j].omit ){.  
2fc8d 20 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d 20        int iTerm 
2fc8e 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6a 5d  = aConstraint[j]
2fc8f 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20  .iTermOffset;.  
2fc90 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72        disableTer
2fc91 6d 28 70 4c 65 76 65 6c 2c 20 26 70 57 43 2d 3e  m(pLevel, &pWC->
2fc92 61 5b 69 54 65 72 6d 5d 29 3b 0a 20 20 20 20 20  a[iTerm]);.     
2fc93 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 65   }.    }.    pLe
2fc94 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 56 4e 65  vel->op = OP_VNe
2fc95 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  xt;.    pLevel->
2fc96 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 70  p1 = iCur;.    p
2fc97 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 71 6c 69  Level->p2 = sqli
2fc98 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2fc99 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr(v);.    sqlit
2fc9a 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
2fc9b 67 65 28 70 50 61 72 73 65 2c 20 69 52 65 67 2c  ge(pParse, iReg,
2fc9c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b   nConstraint+2);
2fc9d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
2fc9e 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
2fc9f 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
2fca0 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c    if( pLevel->pl
2fca1 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2fca2 52 45 5f 52 4f 57 49 44 5f 45 51 20 29 7b 0a 20  RE_ROWID_EQ ){. 
2fca3 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 57     /* Case 1:  W
2fca4 65 20 63 61 6e 20 64 69 72 65 63 74 6c 79 20 72  e can directly r
2fca5 65 66 65 72 65 6e 63 65 20 61 20 73 69 6e 67 6c  eference a singl
2fca6 65 20 72 6f 77 20 75 73 69 6e 67 20 61 6e 0a 20  e row using an. 
2fca7 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 65     **          e
2fca8 71 75 61 6c 69 74 79 20 63 6f 6d 70 61 72 69 73  quality comparis
2fca9 6f 6e 20 61 67 61 69 6e 73 74 20 74 68 65 20 52  on against the R
2fcaa 4f 57 49 44 20 66 69 65 6c 64 2e 20 20 4f 72 0a  OWID field.  Or.
2fcab 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
2fcac 77 65 20 72 65 66 65 72 65 6e 63 65 20 6d 75 6c  we reference mul
2fcad 74 69 70 6c 65 20 72 6f 77 73 20 75 73 69 6e 67  tiple rows using
2fcae 20 61 20 22 72 6f 77 69 64 20 49 4e 20 28 2e 2e   a "rowid IN (..
2fcaf 2e 29 22 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  .)".    **      
2fcb0 20 20 20 20 63 6f 6e 73 74 72 75 63 74 2e 0a 20      construct.. 
2fcb1 20 20 20 2a 2f 0a 20 20 20 20 69 52 65 6c 65 61     */.    iRelea
2fcb2 73 65 52 65 67 20 3d 20 73 71 6c 69 74 65 33 47  seReg = sqlite3G
2fcb3 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
2fcb4 29 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 66  );.    pTerm = f
2fcb5 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
2fcb6 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c  r, -1, notReady,
2fcb7 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 2c 20 30 29   WO_EQ|WO_IN, 0)
2fcb8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
2fcb9 65 72 6d 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  erm!=0 );.    as
2fcba 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 70 45 78  sert( pTerm->pEx
2fcbb 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  pr!=0 );.    ass
2fcbc 65 72 74 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74  ert( pTerm->left
2fcbd 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b 0a  Cursor==iCur );.
2fcbe 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69 74      assert( omit
2fcbf 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20  Table==0 );.    
2fcc0 69 52 6f 77 69 64 52 65 67 20 3d 20 63 6f 64 65  iRowidReg = code
2fcc1 45 71 75 61 6c 69 74 79 54 65 72 6d 28 70 50 61  EqualityTerm(pPa
2fcc2 72 73 65 2c 20 70 54 65 72 6d 2c 20 70 4c 65 76  rse, pTerm, pLev
2fcc3 65 6c 2c 20 69 52 65 6c 65 61 73 65 52 65 67 29  el, iReleaseReg)
2fcc4 3b 0a 20 20 20 20 61 64 64 72 4e 78 74 20 3d 20  ;.    addrNxt = 
2fcc5 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 3b  pLevel->addrNxt;
2fcc6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2fcc7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73  AddOp2(v, OP_Mus
2fcc8 74 42 65 49 6e 74 2c 20 69 52 6f 77 69 64 52 65  tBeInt, iRowidRe
2fcc9 67 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20 20 20  g, addrNxt);.   
2fcca 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2fccb 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
2fccc 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 4e 78  ts, iCur, addrNx
2fccd 74 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20  t, iRowidReg);. 
2fcce 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2fccf 63 68 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c  cheStore(pParse,
2fcd0 20 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69   iCur, -1, iRowi
2fcd1 64 52 65 67 29 3b 0a 20 20 20 20 56 64 62 65 43  dReg);.    VdbeC
2fcd2 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 70 6b 22 29  omment((v, "pk")
2fcd3 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f  );.    pLevel->o
2fcd4 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 7d  p = OP_Noop;.  }
2fcd5 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d  else if( pLevel-
2fcd6 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2fcd7 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47  WHERE_ROWID_RANG
2fcd8 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65  E ){.    /* Case
2fcd9 20 32 3a 20 20 57 65 20 68 61 76 65 20 61 6e 20   2:  We have an 
2fcda 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6d 70 61  inequality compa
2fcdb 72 69 73 6f 6e 20 61 67 61 69 6e 73 74 20 74 68  rison against th
2fcdc 65 20 52 4f 57 49 44 20 66 69 65 6c 64 2e 0a 20  e ROWID field.. 
2fcdd 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 74 65     */.    int te
2fcde 73 74 4f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a  stOp = OP_Noop;.
2fcdf 20 20 20 20 69 6e 74 20 73 74 61 72 74 3b 0a 20      int start;. 
2fce0 20 20 20 69 6e 74 20 6d 65 6d 45 6e 64 56 61 6c     int memEndVal
2fce1 75 65 20 3d 20 30 3b 0a 20 20 20 20 57 68 65 72  ue = 0;.    Wher
2fce2 65 54 65 72 6d 20 2a 70 53 74 61 72 74 2c 20 2a  eTerm *pStart, *
2fce3 70 45 6e 64 3b 0a 0a 20 20 20 20 61 73 73 65 72  pEnd;..    asser
2fce4 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20  t( omitTable==0 
2fce5 29 3b 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20  );.    pStart = 
2fce6 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
2fce7 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
2fce8 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 30  , WO_GT|WO_GE, 0
2fce9 29 3b 0a 20 20 20 20 70 45 6e 64 20 3d 20 66 69  );.    pEnd = fi
2fcea 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
2fceb 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20  , -1, notReady, 
2fcec 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 30 29 3b  WO_LT|WO_LE, 0);
2fced 0a 20 20 20 20 69 66 28 20 62 52 65 76 20 29 7b  .    if( bRev ){
2fcee 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 70  .      pTerm = p
2fcef 53 74 61 72 74 3b 0a 20 20 20 20 20 20 70 53 74  Start;.      pSt
2fcf0 61 72 74 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20  art = pEnd;.    
2fcf1 20 20 70 45 6e 64 20 3d 20 70 54 65 72 6d 3b 0a    pEnd = pTerm;.
2fcf2 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
2fcf3 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 45 78  tart ){.      Ex
2fcf4 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20 20 20  pr *pX;         
2fcf5 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65      /* The expre
2fcf6 73 73 69 6f 6e 20 74 68 61 74 20 64 65 66 69 6e  ssion that defin
2fcf7 65 73 20 74 68 65 20 73 74 61 72 74 20 62 6f 75  es the start bou
2fcf8 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  nd */.      int 
2fcf9 72 31 2c 20 72 54 65 6d 70 3b 20 20 20 20 20 20  r1, rTemp;      
2fcfa 20 20 2f 2a 20 52 65 67 69 73 74 65 72 73 20 66    /* Registers f
2fcfb 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 73  or holding the s
2fcfc 74 61 72 74 20 62 6f 75 6e 64 61 72 79 20 2a 2f  tart boundary */
2fcfd 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66  ..      /* The f
2fcfe 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e  ollowing constan
2fcff 74 20 6d 61 70 73 20 54 4b 5f 78 78 20 63 6f 64  t maps TK_xx cod
2fd00 65 73 20 69 6e 74 6f 20 63 6f 72 72 65 73 70 6f  es into correspo
2fd01 6e 64 69 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20  nding .      ** 
2fd02 73 65 65 6b 20 6f 70 63 6f 64 65 73 2e 20 20 49  seek opcodes.  I
2fd03 74 20 64 65 70 65 6e 64 73 20 6f 6e 20 61 20 70  t depends on a p
2fd04 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65 72 69  articular orderi
2fd05 6e 67 20 6f 66 20 54 4b 5f 78 78 0a 20 20 20 20  ng of TK_xx.    
2fd06 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74    */.      const
2fd07 20 75 38 20 61 4d 6f 76 65 4f 70 5b 5d 20 3d 20   u8 aMoveOp[] = 
2fd08 7b 0a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {.           /* 
2fd09 54 4b 5f 47 54 20 2a 2f 20 20 4f 50 5f 53 65 65  TK_GT */  OP_See
2fd0a 6b 47 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  kGt,.           
2fd0b 2f 2a 20 54 4b 5f 4c 45 20 2a 2f 20 20 4f 50 5f  /* TK_LE */  OP_
2fd0c 53 65 65 6b 4c 65 2c 0a 20 20 20 20 20 20 20 20  SeekLe,.        
2fd0d 20 20 20 2f 2a 20 54 4b 5f 4c 54 20 2a 2f 20 20     /* TK_LT */  
2fd0e 4f 50 5f 53 65 65 6b 4c 74 2c 0a 20 20 20 20 20  OP_SeekLt,.     
2fd0f 20 20 20 20 20 20 2f 2a 20 54 4b 5f 47 45 20 2a        /* TK_GE *
2fd10 2f 20 20 4f 50 5f 53 65 65 6b 47 65 0a 20 20 20  /  OP_SeekGe.   
2fd11 20 20 20 7d 3b 0a 20 20 20 20 20 20 61 73 73 65     };.      asse
2fd12 72 74 28 20 54 4b 5f 4c 45 3d 3d 54 4b 5f 47 54  rt( TK_LE==TK_GT
2fd13 2b 31 20 29 3b 20 20 20 20 20 20 2f 2a 20 4d 61  +1 );      /* Ma
2fd14 6b 65 20 73 75 72 65 20 74 68 65 20 6f 72 64 65  ke sure the orde
2fd15 72 69 6e 67 2e 2e 20 2a 2f 0a 20 20 20 20 20 20  ring.. */.      
2fd16 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54  assert( TK_LT==T
2fd17 4b 5f 47 54 2b 32 20 29 3b 20 20 20 20 20 20 2f  K_GT+2 );      /
2fd18 2a 20 20 2e 2e 2e 20 6f 66 20 74 68 65 20 54 4b  *  ... of the TK
2fd19 5f 78 78 20 76 61 6c 75 65 73 2e 2e 2e 20 2a 2f  _xx values... */
2fd1a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
2fd1b 4b 5f 47 45 3d 3d 54 4b 5f 47 54 2b 33 20 29 3b  K_GE==TK_GT+3 );
2fd1c 20 20 20 20 20 20 2f 2a 20 20 2e 2e 2e 20 69 73        /*  ... is
2fd1d 20 63 6f 72 72 65 63 63 74 2e 20 2a 2f 0a 0a 20   correcct. */.. 
2fd1e 20 20 20 20 20 70 58 20 3d 20 70 53 74 61 72 74       pX = pStart
2fd1f 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 61  ->pExpr;.      a
2fd20 73 73 65 72 74 28 20 70 58 21 3d 30 20 29 3b 0a  ssert( pX!=0 );.
2fd21 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
2fd22 74 61 72 74 2d 3e 6c 65 66 74 43 75 72 73 6f 72  tart->leftCursor
2fd23 3d 3d 69 43 75 72 20 29 3b 0a 20 20 20 20 20 20  ==iCur );.      
2fd24 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r1 = sqlite3Expr
2fd25 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
2fd26 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 26 72 54   pX->pRight, &rT
2fd27 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  emp);.      sqli
2fd28 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2fd29 20 61 4d 6f 76 65 4f 70 5b 70 58 2d 3e 6f 70 2d   aMoveOp[pX->op-
2fd2a 54 4b 5f 47 54 5d 2c 20 69 43 75 72 2c 20 61 64  TK_GT], iCur, ad
2fd2b 64 72 42 72 6b 2c 20 72 31 29 3b 0a 20 20 20 20  drBrk, r1);.    
2fd2c 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2fd2d 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20 20 20 20  , "pk"));.      
2fd2e 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2fd2f 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
2fd30 50 61 72 73 65 2c 20 72 31 2c 20 31 29 3b 0a 20  Parse, r1, 1);. 
2fd31 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
2fd32 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2fd33 65 2c 20 72 54 65 6d 70 29 3b 0a 20 20 20 20 20  e, rTemp);.     
2fd34 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
2fd35 76 65 6c 2c 20 70 53 74 61 72 74 29 3b 0a 20 20  vel, pStart);.  
2fd36 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
2fd37 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2fd38 28 76 2c 20 62 52 65 76 20 3f 20 4f 50 5f 4c 61  (v, bRev ? OP_La
2fd39 73 74 20 3a 20 4f 50 5f 52 65 77 69 6e 64 2c 20  st : OP_Rewind, 
2fd3a 69 43 75 72 2c 20 61 64 64 72 42 72 6b 29 3b 0a  iCur, addrBrk);.
2fd3b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 45      }.    if( pE
2fd3c 6e 64 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72  nd ){.      Expr
2fd3d 20 2a 70 58 3b 0a 20 20 20 20 20 20 70 58 20 3d   *pX;.      pX =
2fd3e 20 70 45 6e 64 2d 3e 70 45 78 70 72 3b 0a 20 20   pEnd->pExpr;.  
2fd3f 20 20 20 20 61 73 73 65 72 74 28 20 70 58 21 3d      assert( pX!=
2fd40 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
2fd41 74 28 20 70 45 6e 64 2d 3e 6c 65 66 74 43 75 72  t( pEnd->leftCur
2fd42 73 6f 72 3d 3d 69 43 75 72 20 29 3b 0a 20 20 20  sor==iCur );.   
2fd43 20 20 20 6d 65 6d 45 6e 64 56 61 6c 75 65 20 3d     memEndValue =
2fd44 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2fd45 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2fd46 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
2fd47 58 2d 3e 70 52 69 67 68 74 2c 20 6d 65 6d 45 6e  X->pRight, memEn
2fd48 64 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 69  dValue);.      i
2fd49 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54  f( pX->op==TK_LT
2fd4a 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 47   || pX->op==TK_G
2fd4b 54 20 29 7b 0a 20 20 20 20 20 20 20 20 74 65 73  T ){.        tes
2fd4c 74 4f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f  tOp = bRev ? OP_
2fd4d 4c 65 20 3a 20 4f 50 5f 47 65 3b 0a 20 20 20 20  Le : OP_Ge;.    
2fd4e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2fd4f 20 74 65 73 74 4f 70 20 3d 20 62 52 65 76 20 3f   testOp = bRev ?
2fd50 20 4f 50 5f 4c 74 20 3a 20 4f 50 5f 47 74 3b 0a   OP_Lt : OP_Gt;.
2fd51 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 69        }.      di
2fd52 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
2fd53 2c 20 70 45 6e 64 29 3b 0a 20 20 20 20 7d 0a 20  , pEnd);.    }. 
2fd54 20 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69 74     start = sqlit
2fd55 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2fd56 72 28 76 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c  r(v);.    pLevel
2fd57 2d 3e 6f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50  ->op = bRev ? OP
2fd58 5f 50 72 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b  _Prev : OP_Next;
2fd59 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20  .    pLevel->p1 
2fd5a 3d 20 69 43 75 72 3b 0a 20 20 20 20 70 4c 65 76  = iCur;.    pLev
2fd5b 65 6c 2d 3e 70 32 20 3d 20 73 74 61 72 74 3b 0a  el->p2 = start;.
2fd5c 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 35 20 3d      pLevel->p5 =
2fd5d 20 28 70 53 74 61 72 74 3d 3d 30 20 26 26 20 70   (pStart==0 && p
2fd5e 45 6e 64 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20 20  End==0) ?1:0;.  
2fd5f 20 20 69 66 28 20 74 65 73 74 4f 70 21 3d 4f 50    if( testOp!=OP
2fd60 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 69  _Noop ){.      i
2fd61 52 6f 77 69 64 52 65 67 20 3d 20 69 52 65 6c 65  RowidReg = iRele
2fd62 61 73 65 52 65 67 20 3d 20 73 71 6c 69 74 65 33  aseReg = sqlite3
2fd63 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2fd64 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
2fd65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2fd66 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 69  P_Rowid, iCur, i
2fd67 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 20  RowidReg);.     
2fd68 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2fd69 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69  eStore(pParse, i
2fd6a 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69 64 52  Cur, -1, iRowidR
2fd6b 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  eg);.      sqlit
2fd6c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2fd6d 74 65 73 74 4f 70 2c 20 6d 65 6d 45 6e 64 56 61  testOp, memEndVa
2fd6e 6c 75 65 2c 20 61 64 64 72 42 72 6b 2c 20 69 52  lue, addrBrk, iR
2fd6f 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 20 20  owidReg);.      
2fd70 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2fd71 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f 41 46  eP5(v, SQLITE_AF
2fd72 46 5f 4e 55 4d 45 52 49 43 20 7c 20 53 51 4c 49  F_NUMERIC | SQLI
2fd73 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
2fd74 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
2fd75 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
2fd76 73 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f  sFlags & (WHERE_
2fd77 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57 48 45  COLUMN_RANGE|WHE
2fd78 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 29 20 29 7b  RE_COLUMN_EQ) ){
2fd79 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 33 3a 20  .    /* Case 3: 
2fd7a 41 20 73 63 61 6e 20 75 73 69 6e 67 20 61 6e 20  A scan using an 
2fd7b 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20  index..    **.  
2fd7c 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68 65    **         The
2fd7d 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6d 61   WHERE clause ma
2fd7e 79 20 63 6f 6e 74 61 69 6e 20 7a 65 72 6f 20 6f  y contain zero o
2fd7f 72 20 6d 6f 72 65 20 65 71 75 61 6c 69 74 79 20  r more equality 
2fd80 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2fd81 74 65 72 6d 73 20 28 22 3d 3d 22 20 6f 72 20 22  terms ("==" or "
2fd82 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 29 20 74  IN" operators) t
2fd83 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65  hat refer to the
2fd84 20 4e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   N.    **       
2fd85 20 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75    left-most colu
2fd86 6d 6e 73 20 6f 66 20 74 68 65 20 69 6e 64 65 78  mns of the index
2fd87 2e 20 49 74 20 6d 61 79 20 61 6c 73 6f 20 63 6f  . It may also co
2fd88 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 20 20 20  ntain.    **    
2fd89 20 20 20 20 20 69 6e 65 71 75 61 6c 69 74 79 20       inequality 
2fd8a 63 6f 6e 73 74 72 61 69 6e 74 73 20 28 3e 2c 20  constraints (>, 
2fd8b 3c 2c 20 3e 3d 20 6f 72 20 3c 3d 29 20 6f 6e 20  <, >= or <=) on 
2fd8c 74 68 65 20 69 6e 64 65 78 65 64 0a 20 20 20 20  the indexed.    
2fd8d 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d  **         colum
2fd8e 6e 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74 65  n that immediate
2fd8f 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 4e  ly follows the N
2fd90 20 65 71 75 61 6c 69 74 69 65 73 2e 20 4f 6e 6c   equalities. Onl
2fd91 79 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  y .    **       
2fd92 20 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74    the right-most
2fd93 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 61   column can be a
2fd94 6e 20 69 6e 65 71 75 61 6c 69 74 79 20 2d 20 74  n inequality - t
2fd95 68 65 20 72 65 73 74 20 6d 75 73 74 0a 20 20 20  he rest must.   
2fd96 20 2a 2a 20 20 20 20 20 20 20 20 20 75 73 65 20   **         use 
2fd97 74 68 65 20 22 3d 3d 22 20 61 6e 64 20 22 49 4e  the "==" and "IN
2fd98 22 20 6f 70 65 72 61 74 6f 72 73 2e 20 46 6f 72  " operators. For
2fd99 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
2fd9a 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20   .    **        
2fd9b 20 69 6e 64 65 78 20 69 73 20 6f 6e 20 28 78 2c   index is on (x,
2fd9c 79 2c 7a 29 2c 20 74 68 65 6e 20 74 68 65 20 66  y,z), then the f
2fd9d 6f 6c 6c 6f 77 69 6e 67 20 63 6c 61 75 73 65 73  ollowing clauses
2fd9e 20 61 72 65 20 61 6c 6c 20 0a 20 20 20 20 2a 2a   are all .    **
2fd9f 20 20 20 20 20 20 20 20 20 6f 70 74 69 6d 69 7a           optimiz
2fda0 65 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ed:.    **.    *
2fda1 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 3d 35  *            x=5
2fda2 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2fda3 20 20 20 78 3d 35 20 41 4e 44 20 79 3d 31 30 0a     x=5 AND y=10.
2fda4 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
2fda5 20 20 78 3d 35 20 41 4e 44 20 79 3c 31 30 0a 20    x=5 AND y<10. 
2fda6 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
2fda7 20 78 3d 35 20 41 4e 44 20 79 3e 35 20 41 4e 44   x=5 AND y>5 AND
2fda8 20 79 3c 31 30 0a 20 20 20 20 2a 2a 20 20 20 20   y<10.    **    
2fda9 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
2fdaa 79 3d 35 20 41 4e 44 20 7a 3c 3d 31 30 0a 20 20  y=5 AND z<=10.  
2fdab 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
2fdac 20 20 20 20 54 68 65 20 7a 3c 31 30 20 74 65 72      The z<10 ter
2fdad 6d 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  m of the followi
2fdae 6e 67 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65  ng cannot be use
2fdaf 64 2c 20 6f 6e 6c 79 0a 20 20 20 20 2a 2a 20 20  d, only.    **  
2fdb0 20 20 20 20 20 20 20 74 68 65 20 78 3d 35 20 74         the x=5 t
2fdb1 65 72 6d 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  erm:.    **.    
2fdb2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 3d  **            x=
2fdb3 35 20 41 4e 44 20 7a 3c 31 30 0a 20 20 20 20 2a  5 AND z<10.    *
2fdb4 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
2fdb5 20 4e 20 6d 61 79 20 62 65 20 7a 65 72 6f 20 69   N may be zero i
2fdb6 66 20 74 68 65 72 65 20 61 72 65 20 69 6e 65 71  f there are ineq
2fdb7 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2fdb8 74 73 2e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ts..    **      
2fdb9 20 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20     If there are 
2fdba 6e 6f 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  no inequality co
2fdbb 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65 6e 20  nstraints, then 
2fdbc 4e 20 69 73 20 61 74 0a 20 20 20 20 2a 2a 20 20  N is at.    **  
2fdbd 20 20 20 20 20 20 20 6c 65 61 73 74 20 6f 6e 65         least one
2fdbe 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2fdbf 20 20 20 20 20 20 20 20 54 68 69 73 20 63 61 73          This cas
2fdc0 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 77  e is also used w
2fdc1 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hen there are no
2fdc2 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a 20 20   WHERE clause.  
2fdc3 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e    **         con
2fdc4 73 74 72 61 69 6e 74 73 20 62 75 74 20 61 6e 20  straints but an 
2fdc5 69 6e 64 65 78 20 69 73 20 73 65 6c 65 63 74 65  index is selecte
2fdc6 64 20 61 6e 79 77 61 79 2c 20 69 6e 20 6f 72 64  d anyway, in ord
2fdc7 65 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  er.    **       
2fdc8 20 20 74 6f 20 66 6f 72 63 65 20 74 68 65 20 6f    to force the o
2fdc9 75 74 70 75 74 20 6f 72 64 65 72 20 74 6f 20 63  utput order to c
2fdca 6f 6e 66 6f 72 6d 20 74 6f 20 61 6e 20 4f 52 44  onform to an ORD
2fdcb 45 52 20 42 59 2e 0a 20 20 20 20 2a 2f 20 20 0a  ER BY..    */  .
2fdcc 20 20 20 20 69 6e 74 20 61 53 74 61 72 74 4f 70      int aStartOp
2fdcd 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 30 2c 0a  [] = {.      0,.
2fdce 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 4f        0,.      O
2fdcf 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 20 20  P_Rewind,       
2fdd0 20 20 20 20 2f 2a 20 32 3a 20 28 21 73 74 61 72      /* 2: (!star
2fdd1 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26  t_constraints &&
2fdd2 20 73 74 61 72 74 45 71 20 26 26 20 20 21 62 52   startEq &&  !bR
2fdd3 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f  ev) */.      OP_
2fdd4 4c 61 73 74 2c 20 20 20 20 20 20 20 20 20 20 20  Last,           
2fdd5 20 20 2f 2a 20 33 3a 20 28 21 73 74 61 72 74 5f    /* 3: (!start_
2fdd6 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 20 73  constraints && s
2fdd7 74 61 72 74 45 71 20 26 26 20 20 20 62 52 65 76  tartEq &&   bRev
2fdd8 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65  ) */.      OP_Se
2fdd9 65 6b 47 74 2c 20 20 20 20 20 20 20 20 20 20 20  ekGt,           
2fdda 2f 2a 20 34 3a 20 28 73 74 61 72 74 5f 63 6f 6e  /* 4: (start_con
2fddb 73 74 72 61 69 6e 74 73 20 20 26 26 20 21 73 74  straints  && !st
2fddc 61 72 74 45 71 20 26 26 20 21 62 52 65 76 29 20  artEq && !bRev) 
2fddd 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 6b  */.      OP_Seek
2fdde 4c 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Lt,           /*
2fddf 20 35 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 74   5: (start_const
2fde0 72 61 69 6e 74 73 20 20 26 26 20 21 73 74 61 72  raints  && !star
2fde1 74 45 71 20 26 26 20 20 62 52 65 76 29 20 2a 2f  tEq &&  bRev) */
2fde2 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 6b 47 65  .      OP_SeekGe
2fde3 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 36  ,           /* 6
2fde4 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 61  : (start_constra
2fde5 69 6e 74 73 20 20 26 26 20 20 73 74 61 72 74 45  ints  &&  startE
2fde6 71 20 26 26 20 21 62 52 65 76 29 20 2a 2f 0a 20  q && !bRev) */. 
2fde7 20 20 20 20 20 4f 50 5f 53 65 65 6b 4c 65 20 20       OP_SeekLe  
2fde8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 37 3a 20            /* 7: 
2fde9 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e  (start_constrain
2fdea 74 73 20 20 26 26 20 20 73 74 61 72 74 45 71 20  ts  &&  startEq 
2fdeb 26 26 20 20 62 52 65 76 29 20 2a 2f 0a 20 20 20  &&  bRev) */.   
2fdec 20 7d 3b 0a 20 20 20 20 69 6e 74 20 61 45 6e 64   };.    int aEnd
2fded 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 4f  Op[] = {.      O
2fdee 50 5f 4e 6f 6f 70 2c 20 20 20 20 20 20 20 20 20  P_Noop,         
2fdef 20 20 20 20 2f 2a 20 30 3a 20 28 21 65 6e 64 5f      /* 0: (!end_
2fdf0 63 6f 6e 73 74 72 61 69 6e 74 73 29 20 2a 2f 0a  constraints) */.
2fdf1 20 20 20 20 20 20 4f 50 5f 49 64 78 47 45 2c 20        OP_IdxGE, 
2fdf2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a             /* 1:
2fdf3 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74   (end_constraint
2fdf4 73 20 26 26 20 21 62 52 65 76 29 20 2a 2f 0a 20  s && !bRev) */. 
2fdf5 20 20 20 20 20 4f 50 5f 49 64 78 4c 54 20 20 20       OP_IdxLT   
2fdf6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 3a 20            /* 2: 
2fdf7 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73  (end_constraints
2fdf8 20 26 26 20 62 52 65 76 29 20 2a 2f 0a 20 20 20   && bRev) */.   
2fdf9 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6e 45 71 20   };.    int nEq 
2fdfa 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e  = pLevel->plan.n
2fdfb 45 71 3b 0a 20 20 20 20 69 6e 74 20 69 73 4d 69  Eq;.    int isMi
2fdfc 6e 51 75 65 72 79 20 3d 20 30 3b 20 20 20 20 20  nQuery = 0;     
2fdfd 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20       /* If this 
2fdfe 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 65 64 20  is an optimized 
2fdff 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 2e 2e 20  SELECT min(x).. 
2fe00 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 42 61  */.    int regBa
2fe01 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
2fe02 20 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69      /* Base regi
2fe03 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6e  ster holding con
2fe04 73 74 72 61 69 6e 74 20 76 61 6c 75 65 73 20 2a  straint values *
2fe05 2f 0a 20 20 20 20 69 6e 74 20 72 31 3b 20 20 20  /.    int r1;   
2fe06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe07 20 20 20 2f 2a 20 54 65 6d 70 20 72 65 67 69 73     /* Temp regis
2fe08 74 65 72 20 2a 2f 0a 20 20 20 20 57 68 65 72 65  ter */.    Where
2fe09 54 65 72 6d 20 2a 70 52 61 6e 67 65 53 74 61 72  Term *pRangeStar
2fe0a 74 20 3d 20 30 3b 20 20 2f 2a 20 49 6e 65 71 75  t = 0;  /* Inequ
2fe0b 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2fe0c 20 61 74 20 72 61 6e 67 65 20 73 74 61 72 74 20   at range start 
2fe0d 2a 2f 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d  */.    WhereTerm
2fe0e 20 2a 70 52 61 6e 67 65 45 6e 64 20 3d 20 30 3b   *pRangeEnd = 0;
2fe0f 20 20 20 20 2f 2a 20 49 6e 65 71 75 61 6c 69 74      /* Inequalit
2fe10 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 74 20  y constraint at 
2fe11 72 61 6e 67 65 20 65 6e 64 20 2a 2f 0a 20 20 20  range end */.   
2fe12 20 69 6e 74 20 73 74 61 72 74 45 71 3b 20 20 20   int startEq;   
2fe13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2fe14 20 54 72 75 65 20 69 66 20 72 61 6e 67 65 20 73   True if range s
2fe15 74 61 72 74 20 75 73 65 73 20 3d 3d 2c 20 3e 3d  tart uses ==, >=
2fe16 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20 69 6e   or <= */.    in
2fe17 74 20 65 6e 64 45 71 3b 20 20 20 20 20 20 20 20  t endEq;        
2fe18 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
2fe19 75 65 20 69 66 20 72 61 6e 67 65 20 65 6e 64 20  ue if range end 
2fe1a 75 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72 20 3c  uses ==, >= or <
2fe1b 3d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 74 61  = */.    int sta
2fe1c 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3b 20  rt_constraints; 
2fe1d 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 6f        /* Start o
2fe1e 66 20 72 61 6e 67 65 20 69 73 20 63 6f 6e 73 74  f range is const
2fe1f 72 61 69 6e 65 64 20 2a 2f 0a 20 20 20 20 69 6e  rained */.    in
2fe20 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20  t nConstraint;  
2fe21 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2fe22 6d 62 65 72 20 6f 66 20 63 6f 6e 73 74 72 61 69  mber of constrai
2fe23 6e 74 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20  nt terms */.    
2fe24 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
2fe25 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65       /* The inde
2fe26 78 20 77 65 20 77 69 6c 6c 20 62 65 20 75 73 69  x we will be usi
2fe27 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 49  ng */.    int iI
2fe28 64 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 2f  dxCur;         /
2fe29 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
2fe2a 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20  r for the index 
2fe2b 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 78 74 72  */.    int nExtr
2fe2c 61 52 65 67 20 3d 20 30 3b 20 20 20 2f 2a 20 4e  aReg = 0;   /* N
2fe2d 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 72  umber of extra r
2fe2e 65 67 69 73 74 65 72 73 20 6e 65 65 64 65 64 20  egisters needed 
2fe2f 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 70 3b 20 20  */.    int op;  
2fe30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2fe31 6e 73 74 72 75 63 74 69 6f 6e 20 6f 70 63 6f 64  nstruction opcod
2fe32 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  e */.    char *z
2fe33 41 66 66 3b 0a 0a 20 20 20 20 70 49 64 78 20 3d  Aff;..    pIdx =
2fe34 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
2fe35 70 49 64 78 3b 0a 20 20 20 20 69 49 64 78 43 75  pIdx;.    iIdxCu
2fe36 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  r = pLevel->iIdx
2fe37 43 75 72 3b 0a 20 20 20 20 6b 20 3d 20 70 49 64  Cur;.    k = pId
2fe38 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d  x->aiColumn[nEq]
2fe39 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20  ;     /* Column 
2fe3a 66 6f 72 20 69 6e 65 71 75 61 6c 69 74 79 20 63  for inequality c
2fe3b 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 0a 20  onstraints */.. 
2fe3c 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6c 6f     /* If this lo
2fe3d 6f 70 20 73 61 74 69 73 66 69 65 73 20 61 20 73  op satisfies a s
2fe3e 6f 72 74 20 6f 72 64 65 72 20 28 70 4f 72 64 65  ort order (pOrde
2fe3f 72 42 79 29 20 72 65 71 75 65 73 74 20 74 68 61  rBy) request tha
2fe40 74 20 0a 20 20 20 20 2a 2a 20 77 61 73 20 70 61  t .    ** was pa
2fe41 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e  ssed to this fun
2fe42 63 74 69 6f 6e 20 74 6f 20 69 6d 70 6c 65 6d 65  ction to impleme
2fe43 6e 74 20 61 20 22 53 45 4c 45 43 54 20 6d 69 6e  nt a "SELECT min
2fe44 28 78 29 20 2e 2e 2e 22 20 0a 20 20 20 20 2a 2a  (x) ..." .    **
2fe45 20 71 75 65 72 79 2c 20 74 68 65 6e 20 74 68 65   query, then the
2fe46 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 6e 6c   caller will onl
2fe47 79 20 61 6c 6c 6f 77 20 74 68 65 20 6c 6f 6f 70  y allow the loop
2fe48 20 74 6f 20 72 75 6e 20 66 6f 72 0a 20 20 20 20   to run for.    
2fe49 2a 2a 20 61 20 73 69 6e 67 6c 65 20 69 74 65 72  ** a single iter
2fe4a 61 74 69 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e  ation. This mean
2fe4b 73 20 74 68 61 74 20 74 68 65 20 66 69 72 73 74  s that the first
2fe4c 20 72 6f 77 20 72 65 74 75 72 6e 65 64 0a 20 20   row returned.  
2fe4d 20 20 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20    ** should not 
2fe4e 68 61 76 65 20 61 20 4e 55 4c 4c 20 76 61 6c 75  have a NULL valu
2fe4f 65 20 73 74 6f 72 65 64 20 69 6e 20 27 78 27 2e  e stored in 'x'.
2fe50 20 49 66 20 63 6f 6c 75 6d 6e 20 27 78 27 20 69   If column 'x' i
2fe51 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 72  s.    ** the fir
2fe52 73 74 20 6f 6e 65 20 61 66 74 65 72 20 74 68 65  st one after the
2fe53 20 6e 45 71 20 65 71 75 61 6c 69 74 79 20 63 6f   nEq equality co
2fe54 6e 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68 65  nstraints in the
2fe55 20 69 6e 64 65 78 2c 0a 20 20 20 20 2a 2a 20 74   index,.    ** t
2fe56 68 69 73 20 72 65 71 75 69 72 65 73 20 73 6f 6d  his requires som
2fe57 65 20 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69  e special handli
2fe58 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ng..    */.    i
2fe59 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73 26 57  f( (wctrlFlags&W
2fe5a 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
2fe5b 29 21 3d 30 0a 20 20 20 20 20 26 26 20 28 70 4c  )!=0.     && (pL
2fe5c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2fe5d 67 73 26 57 48 45 52 45 5f 4f 52 44 45 52 42 59  gs&WHERE_ORDERBY
2fe5e 29 0a 20 20 20 20 20 26 26 20 28 70 49 64 78 2d  ).     && (pIdx-
2fe5f 3e 6e 43 6f 6c 75 6d 6e 3e 6e 45 71 29 0a 20 20  >nColumn>nEq).  
2fe60 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 61 73    ){.      /* as
2fe61 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d 3e  sert( pOrderBy->
2fe62 6e 45 78 70 72 3d 3d 31 20 29 3b 20 2a 2f 0a 20  nExpr==1 ); */. 
2fe63 20 20 20 20 20 2f 2a 20 61 73 73 65 72 74 28 20       /* assert( 
2fe64 70 4f 72 64 65 72 42 79 2d 3e 61 5b 30 5d 2e 70  pOrderBy->a[0].p
2fe65 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70  Expr->iColumn==p
2fe66 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45  Idx->aiColumn[nE
2fe67 71 5d 20 29 3b 20 2a 2f 0a 20 20 20 20 20 20 69  q] ); */.      i
2fe68 73 4d 69 6e 51 75 65 72 79 20 3d 20 31 3b 0a 20  sMinQuery = 1;. 
2fe69 20 20 20 20 20 6e 45 78 74 72 61 52 65 67 20 3d       nExtraReg =
2fe6a 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   1;.    }..    /
2fe6b 2a 20 46 69 6e 64 20 61 6e 79 20 69 6e 65 71 75  * Find any inequ
2fe6c 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2fe6d 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65 20 73   terms for the s
2fe6e 74 61 72 74 20 61 6e 64 20 65 6e 64 20 0a 20 20  tart and end .  
2fe6f 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 61 6e 67    ** of the rang
2fe70 65 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  e. .    */.    i
2fe71 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  f( pLevel->plan.
2fe72 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2fe73 54 4f 50 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20  TOP_LIMIT ){.   
2fe74 20 20 20 70 52 61 6e 67 65 45 6e 64 20 3d 20 66     pRangeEnd = f
2fe75 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
2fe76 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20  r, k, notReady, 
2fe77 28 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 29 2c 20 70  (WO_LT|WO_LE), p
2fe78 49 64 78 29 3b 0a 20 20 20 20 20 20 6e 45 78 74  Idx);.      nExt
2fe79 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d  raReg = 1;.    }
2fe7a 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
2fe7b 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2fe7c 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20  WHERE_BTM_LIMIT 
2fe7d 29 7b 0a 20 20 20 20 20 20 70 52 61 6e 67 65 53  ){.      pRangeS
2fe7e 74 61 72 74 20 3d 20 66 69 6e 64 54 65 72 6d 28  tart = findTerm(
2fe7f 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f  pWC, iCur, k, no
2fe80 74 52 65 61 64 79 2c 20 28 57 4f 5f 47 54 7c 57  tReady, (WO_GT|W
2fe81 4f 5f 47 45 29 2c 20 70 49 64 78 29 3b 0a 20 20  O_GE), pIdx);.  
2fe82 20 20 20 20 6e 45 78 74 72 61 52 65 67 20 3d 20      nExtraReg = 
2fe83 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  1;.    }..    /*
2fe84 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2fe85 6f 20 65 76 61 6c 75 61 74 65 20 61 6c 6c 20 63  o evaluate all c
2fe86 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20  onstraint terms 
2fe87 75 73 69 6e 67 20 3d 3d 20 6f 72 20 49 4e 0a 20  using == or IN. 
2fe88 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20     ** and store 
2fe89 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68  the values of th
2fe8a 6f 73 65 20 74 65 72 6d 73 20 69 6e 20 61 6e 20  ose terms in an 
2fe8b 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74 65  array of registe
2fe8c 72 73 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 69  rs.    ** starti
2fe8d 6e 67 20 61 74 20 72 65 67 42 61 73 65 2e 0a 20  ng at regBase.. 
2fe8e 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 42 61 73     */.    regBas
2fe8f 65 20 3d 20 63 6f 64 65 41 6c 6c 45 71 75 61 6c  e = codeAllEqual
2fe90 69 74 79 54 65 72 6d 73 28 0a 20 20 20 20 20 20  ityTerms(.      
2fe91 20 20 70 50 61 72 73 65 2c 20 70 4c 65 76 65 6c    pParse, pLevel
2fe92 2c 20 70 57 43 2c 20 6e 6f 74 52 65 61 64 79 2c  , pWC, notReady,
2fe93 20 6e 45 78 74 72 61 52 65 67 2c 20 26 7a 41 66   nExtraReg, &zAf
2fe94 66 0a 20 20 20 20 29 3b 0a 20 20 20 20 61 64 64  f.    );.    add
2fe95 72 4e 78 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61  rNxt = pLevel->a
2fe96 64 64 72 4e 78 74 3b 0a 0a 20 20 20 20 2f 2a 20  ddrNxt;..    /* 
2fe97 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20  If we are doing 
2fe98 61 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20  a reverse order 
2fe99 73 63 61 6e 20 6f 6e 20 61 6e 20 61 73 63 65 6e  scan on an ascen
2fe9a 64 69 6e 67 20 69 6e 64 65 78 2c 20 6f 72 0a 20  ding index, or. 
2fe9b 20 20 20 2a 2a 20 61 20 66 6f 72 77 61 72 64 20     ** a forward 
2fe9c 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e 20 61 20  order scan on a 
2fe9d 64 65 73 63 65 6e 64 69 6e 67 20 69 6e 64 65 78  descending index
2fe9e 2c 20 69 6e 74 65 72 63 68 61 6e 67 65 20 74 68  , interchange th
2fe9f 65 20 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 20  e .    ** start 
2fea0 61 6e 64 20 65 6e 64 20 74 65 72 6d 73 20 28 70  and end terms (p
2fea1 52 61 6e 67 65 53 74 61 72 74 20 61 6e 64 20 70  RangeStart and p
2fea2 52 61 6e 67 65 45 6e 64 29 2e 0a 20 20 20 20 2a  RangeEnd)..    *
2fea3 2f 0a 20 20 20 20 69 66 28 20 62 52 65 76 3d 3d  /.    if( bRev==
2fea4 28 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65  (pIdx->aSortOrde
2fea5 72 5b 6e 45 71 5d 3d 3d 53 51 4c 49 54 45 5f 53  r[nEq]==SQLITE_S
2fea6 4f 5f 41 53 43 29 20 29 7b 0a 20 20 20 20 20 20  O_ASC) ){.      
2fea7 53 57 41 50 28 57 68 65 72 65 54 65 72 6d 20 2a  SWAP(WhereTerm *
2fea8 2c 20 70 52 61 6e 67 65 45 6e 64 2c 20 70 52 61  , pRangeEnd, pRa
2fea9 6e 67 65 53 74 61 72 74 29 3b 0a 20 20 20 20 7d  ngeStart);.    }
2feaa 0a 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ..    testcase( 
2feab 70 52 61 6e 67 65 53 74 61 72 74 20 26 26 20 70  pRangeStart && p
2feac 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70 65  RangeStart->eOpe
2fead 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20 29 3b  rator & WO_LE );
2feae 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
2feaf 52 61 6e 67 65 53 74 61 72 74 20 26 26 20 70 52  RangeStart && pR
2feb0 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70 65 72  angeStart->eOper
2feb1 61 74 6f 72 20 26 20 57 4f 5f 47 45 20 29 3b 0a  ator & WO_GE );.
2feb2 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 52      testcase( pR
2feb3 61 6e 67 65 45 6e 64 20 26 26 20 70 52 61 6e 67  angeEnd && pRang
2feb4 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20  eEnd->eOperator 
2feb5 26 20 57 4f 5f 4c 45 20 29 3b 0a 20 20 20 20 74  & WO_LE );.    t
2feb6 65 73 74 63 61 73 65 28 20 70 52 61 6e 67 65 45  estcase( pRangeE
2feb7 6e 64 20 26 26 20 70 52 61 6e 67 65 45 6e 64 2d  nd && pRangeEnd-
2feb8 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f  >eOperator & WO_
2feb9 47 45 20 29 3b 0a 20 20 20 20 73 74 61 72 74 45  GE );.    startE
2feba 71 20 3d 20 21 70 52 61 6e 67 65 53 74 61 72 74  q = !pRangeStart
2febb 20 7c 7c 20 70 52 61 6e 67 65 53 74 61 72 74 2d   || pRangeStart-
2febc 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f  >eOperator & (WO
2febd 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a 20 20 20 20  _LE|WO_GE);.    
2febe 65 6e 64 45 71 20 3d 20 20 20 21 70 52 61 6e 67  endEq =   !pRang
2febf 65 45 6e 64 20 7c 7c 20 70 52 61 6e 67 65 45 6e  eEnd || pRangeEn
2fec0 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  d->eOperator & (
2fec1 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a 20 20  WO_LE|WO_GE);.  
2fec2 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69    start_constrai
2fec3 6e 74 73 20 3d 20 70 52 61 6e 67 65 53 74 61 72  nts = pRangeStar
2fec4 74 20 7c 7c 20 6e 45 71 3e 30 3b 0a 0a 20 20 20  t || nEq>0;..   
2fec5 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 69 6e 64   /* Seek the ind
2fec6 65 78 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  ex cursor to the
2fec7 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 61   start of the ra
2fec8 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 6e 43 6f 6e  nge. */.    nCon
2fec9 73 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b 0a 20  straint = nEq;. 
2feca 20 20 20 69 66 28 20 70 52 61 6e 67 65 53 74 61     if( pRangeSta
2fecb 72 74 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72  rt ){.      Expr
2fecc 20 2a 70 52 69 67 68 74 20 3d 20 70 52 61 6e 67   *pRight = pRang
2fecd 65 53 74 61 72 74 2d 3e 70 45 78 70 72 2d 3e 70  eStart->pExpr->p
2fece 52 69 67 68 74 3b 0a 20 20 20 20 20 20 73 71 6c  Right;.      sql
2fecf 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
2fed0 72 73 65 2c 20 70 52 69 67 68 74 2c 20 72 65 67  rse, pRight, reg
2fed1 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20  Base+nEq);.     
2fed2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2fed3 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  p2(v, OP_IsNull,
2fed4 20 72 65 67 42 61 73 65 2b 6e 45 71 2c 20 61 64   regBase+nEq, ad
2fed5 64 72 4e 78 74 29 3b 0a 20 20 20 20 20 20 69 66  drNxt);.      if
2fed6 28 20 7a 41 66 66 20 0a 20 20 20 20 20 20 20 26  ( zAff .       &
2fed7 26 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65  & sqlite3Compare
2fed8 41 66 66 69 6e 69 74 79 28 70 52 69 67 68 74 2c  Affinity(pRight,
2fed9 20 7a 41 66 66 5b 6e 43 6f 6e 73 74 72 61 69 6e   zAff[nConstrain
2feda 74 5d 29 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  t])==SQLITE_AFF_
2fedb 4e 4f 4e 45 0a 20 20 20 20 20 20 29 7b 0a 20 20  NONE.      ){.  
2fedc 20 20 20 20 20 20 2f 2a 20 53 69 6e 63 65 20 74        /* Since t
2fedd 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  he comparison is
2fede 20 74 6f 20 62 65 20 70 65 72 66 6f 72 6d 65 64   to be performed
2fedf 20 77 69 74 68 20 6e 6f 20 63 6f 6e 76 65 72 73   with no convers
2fee0 69 6f 6e 73 20 61 70 70 6c 69 65 64 0a 20 20 20  ions applied.   
2fee1 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 6f       ** to the o
2fee2 70 65 72 61 6e 64 73 2c 20 73 65 74 20 74 68 65  perands, set the
2fee3 20 61 66 66 69 6e 69 74 79 20 74 6f 20 61 70 70   affinity to app
2fee4 6c 79 20 74 6f 20 70 52 69 67 68 74 20 74 6f 20  ly to pRight to 
2fee5 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 51 4c 49  .        ** SQLI
2fee6 54 45 5f 41 46 46 5f 4e 4f 4e 45 2e 20 20 2a 2f  TE_AFF_NONE.  */
2fee7 0a 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6e 43  .        zAff[nC
2fee8 6f 6e 73 74 72 61 69 6e 74 5d 20 3d 20 53 51 4c  onstraint] = SQL
2fee9 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
2feea 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 6f 6e      }.      nCon
2feeb 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d  straint++;.    }
2feec 65 6c 73 65 20 69 66 28 20 69 73 4d 69 6e 51 75  else if( isMinQu
2feed 65 72 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ery ){.      sql
2feee 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2feef 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65  , OP_Null, 0, re
2fef0 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20  gBase+nEq);.    
2fef1 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b    nConstraint++;
2fef2 0a 20 20 20 20 20 20 73 74 61 72 74 45 71 20 3d  .      startEq =
2fef3 20 30 3b 0a 20 20 20 20 20 20 73 74 61 72 74 5f   0;.      start_
2fef4 63 6f 6e 73 74 72 61 69 6e 74 73 20 3d 20 31 3b  constraints = 1;
2fef5 0a 20 20 20 20 7d 0a 20 20 20 20 63 6f 64 65 41  .    }.    codeA
2fef6 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61  pplyAffinity(pPa
2fef7 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43  rse, regBase, nC
2fef8 6f 6e 73 74 72 61 69 6e 74 2c 20 7a 41 66 66 29  onstraint, zAff)
2fef9 3b 0a 20 20 20 20 6f 70 20 3d 20 61 53 74 61 72  ;.    op = aStar
2fefa 74 4f 70 5b 28 73 74 61 72 74 5f 63 6f 6e 73 74  tOp[(start_const
2fefb 72 61 69 6e 74 73 3c 3c 32 29 20 2b 20 28 73 74  raints<<2) + (st
2fefc 61 72 74 45 71 3c 3c 31 29 20 2b 20 62 52 65 76  artEq<<1) + bRev
2fefd 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f  ];.    assert( o
2fefe 70 21 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74  p!=0 );.    test
2feff 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 52 65 77  case( op==OP_Rew
2ff00 69 6e 64 20 29 3b 0a 20 20 20 20 74 65 73 74 63  ind );.    testc
2ff01 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4c 61 73 74  ase( op==OP_Last
2ff02 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2ff03 28 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b 47 74 20  ( op==OP_SeekGt 
2ff04 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2ff05 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29   op==OP_SeekGe )
2ff06 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2ff07 6f 70 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b  op==OP_SeekLe );
2ff08 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f  .    testcase( o
2ff09 70 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 29 3b 0a  p==OP_SeekLt );.
2ff0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2ff0b 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20 69 49 64  ddOp4(v, op, iId
2ff0c 78 43 75 72 2c 20 61 64 64 72 4e 78 74 2c 20 72  xCur, addrNxt, r
2ff0d 65 67 42 61 73 65 2c 20 0a 20 20 20 20 20 20 20  egBase, .       
2ff0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
2ff0f 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
2ff10 28 6e 43 6f 6e 73 74 72 61 69 6e 74 29 2c 20 50  (nConstraint), P
2ff11 34 5f 49 4e 54 33 32 29 3b 0a 0a 20 20 20 20 2f  4_INT32);..    /
2ff12 2a 20 4c 6f 61 64 20 74 68 65 20 76 61 6c 75 65  * Load the value
2ff13 20 66 6f 72 20 74 68 65 20 69 6e 65 71 75 61 6c   for the inequal
2ff14 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 61  ity constraint a
2ff15 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
2ff16 0a 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 28 69  .    ** range (i
2ff17 66 20 61 6e 79 29 2e 0a 20 20 20 20 2a 2f 0a 20  f any)..    */. 
2ff18 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d     nConstraint =
2ff19 20 6e 45 71 3b 0a 20 20 20 20 69 66 28 20 70 52   nEq;.    if( pR
2ff1a 61 6e 67 65 45 6e 64 20 29 7b 0a 20 20 20 20 20  angeEnd ){.     
2ff1b 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20   Expr *pRight = 
2ff1c 70 52 61 6e 67 65 45 6e 64 2d 3e 70 45 78 70 72  pRangeEnd->pExpr
2ff1d 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ->pRight;.      
2ff1e 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2ff1f 52 65 6d 6f 76 65 28 70 50 61 72 73 65 2c 20 72  Remove(pParse, r
2ff20 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20  egBase+nEq);.   
2ff21 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
2ff22 64 65 28 70 50 61 72 73 65 2c 20 70 52 69 67 68  de(pParse, pRigh
2ff23 74 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b  t, regBase+nEq);
2ff24 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2ff25 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2ff26 73 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b 6e  sNull, regBase+n
2ff27 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20 20  Eq, addrNxt);.  
2ff28 20 20 20 20 7a 41 66 66 20 3d 20 73 71 6c 69 74      zAff = sqlit
2ff29 65 33 44 62 53 74 72 44 75 70 28 70 50 61 72 73  e3DbStrDup(pPars
2ff2a 65 2d 3e 64 62 2c 20 7a 41 66 66 29 3b 0a 20 20  e->db, zAff);.  
2ff2b 20 20 20 20 69 66 28 20 7a 41 66 66 20 0a 20 20      if( zAff .  
2ff2c 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 43       && sqlite3C
2ff2d 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70  ompareAffinity(p
2ff2e 52 69 67 68 74 2c 20 7a 41 66 66 5b 6e 43 6f 6e  Right, zAff[nCon
2ff2f 73 74 72 61 69 6e 74 5d 29 3d 3d 53 51 4c 49 54  straint])==SQLIT
2ff30 45 5f 41 46 46 5f 4e 4f 4e 45 0a 20 20 20 20 20  E_AFF_NONE.     
2ff31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 53   ){.        /* S
2ff32 69 6e 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69  ince the compari
2ff33 73 6f 6e 20 69 73 20 74 6f 20 62 65 20 70 65 72  son is to be per
2ff34 66 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f 20 63  formed with no c
2ff35 6f 6e 76 65 72 73 69 6f 6e 73 20 61 70 70 6c 69  onversions appli
2ff36 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f  ed.        ** to
2ff37 20 74 68 65 20 6f 70 65 72 61 6e 64 73 2c 20 73   the operands, s
2ff38 65 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  et the affinity 
2ff39 74 6f 20 61 70 70 6c 79 20 74 6f 20 70 52 69 67  to apply to pRig
2ff3a 68 74 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a  ht to .        *
2ff3b 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  * SQLITE_AFF_NON
2ff3c 45 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a  E.  */.        z
2ff3d 41 66 66 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d  Aff[nConstraint]
2ff3e 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f   = SQLITE_AFF_NO
2ff3f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  NE;.      }.    
2ff40 20 20 63 6f 64 65 41 70 70 6c 79 41 66 66 69 6e    codeApplyAffin
2ff41 69 74 79 28 70 50 61 72 73 65 2c 20 72 65 67 42  ity(pParse, regB
2ff42 61 73 65 2c 20 6e 45 71 2b 31 2c 20 7a 41 66 66  ase, nEq+1, zAff
2ff43 29 3b 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74 72  );.      nConstr
2ff44 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20  aint++;.    }.. 
2ff45 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65     /* Top of the
2ff46 20 6c 6f 6f 70 20 62 6f 64 79 20 2a 2f 0a 20 20   loop body */.  
2ff47 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73    pLevel->p2 = s
2ff48 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2ff49 74 41 64 64 72 28 76 29 3b 0a 0a 20 20 20 20 2f  tAddr(v);..    /
2ff4a 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 69  * Check if the i
2ff4b 6e 64 65 78 20 63 75 72 73 6f 72 20 69 73 20 70  ndex cursor is p
2ff4c 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
2ff4d 68 65 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20  he range. */.   
2ff4e 20 6f 70 20 3d 20 61 45 6e 64 4f 70 5b 28 70 52   op = aEndOp[(pR
2ff4f 61 6e 67 65 45 6e 64 20 7c 7c 20 6e 45 71 29 20  angeEnd || nEq) 
2ff50 2a 20 28 31 20 2b 20 62 52 65 76 29 5d 3b 0a 20  * (1 + bRev)];. 
2ff51 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2ff52 3d 4f 50 5f 4e 6f 6f 70 20 29 3b 0a 20 20 20 20  =OP_Noop );.    
2ff53 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
2ff54 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 74 65  _IdxGE );.    te
2ff55 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 49  stcase( op==OP_I
2ff56 64 78 4c 54 20 29 3b 0a 20 20 20 20 69 66 28 20  dxLT );.    if( 
2ff57 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20  op!=OP_Noop ){. 
2ff58 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2ff59 41 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20 69 49  AddOp4(v, op, iI
2ff5a 64 78 43 75 72 2c 20 61 64 64 72 4e 78 74 2c 20  dxCur, addrNxt, 
2ff5b 72 65 67 42 61 73 65 2c 0a 20 20 20 20 20 20 20  regBase,.       
2ff5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff5d 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50   SQLITE_INT_TO_P
2ff5e 54 52 28 6e 43 6f 6e 73 74 72 61 69 6e 74 29 2c  TR(nConstraint),
2ff5f 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20   P4_INT32);.    
2ff60 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2ff61 6e 67 65 50 35 28 76 2c 20 65 6e 64 45 71 21 3d  ngeP5(v, endEq!=
2ff62 62 52 65 76 20 3f 31 3a 30 29 3b 0a 20 20 20 20  bRev ?1:0);.    
2ff63 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  }..    /* If the
2ff64 72 65 20 61 72 65 20 69 6e 65 71 75 61 6c 69 74  re are inequalit
2ff65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 63  y constraints, c
2ff66 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 76 61  heck that the va
2ff67 6c 75 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  lue.    ** of th
2ff68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74  e table column t
2ff69 68 61 74 20 74 68 65 20 69 6e 65 71 75 61 6c 69  hat the inequali
2ff6a 74 79 20 63 6f 6e 74 72 61 69 6e 73 20 69 73 20  ty contrains is 
2ff6b 6e 6f 74 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a  not NULL..    **
2ff6c 20 49 66 20 69 74 20 69 73 2c 20 6a 75 6d 70 20   If it is, jump 
2ff6d 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74 65 72  to the next iter
2ff6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f  ation of the loo
2ff6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 31  p..    */.    r1
2ff70 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2ff71 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2ff72 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 76    testcase( pLev
2ff73 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2ff74 20 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d   & WHERE_BTM_LIM
2ff75 49 54 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  IT );.    testca
2ff76 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  se( pLevel->plan
2ff77 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2ff78 5f 54 4f 50 5f 4c 49 4d 49 54 20 29 3b 0a 20 20  _TOP_LIMIT );.  
2ff79 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c    if( pLevel->pl
2ff7a 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 28 57 48  an.wsFlags & (WH
2ff7b 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 7c 57 48  ERE_BTM_LIMIT|WH
2ff7c 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 29 20 29  ERE_TOP_LIMIT) )
2ff7d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2ff7e 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2ff7f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c  Column, iIdxCur,
2ff80 20 6e 45 71 2c 20 72 31 29 3b 0a 20 20 20 20 20   nEq, r1);.     
2ff81 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ff82 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  p2(v, OP_IsNull,
2ff83 20 72 31 2c 20 61 64 64 72 43 6f 6e 74 29 3b 0a   r1, addrCont);.
2ff84 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2ff85 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2ff86 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 0a 20 20  pParse, r1);..  
2ff87 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 74 61    /* Seek the ta
2ff88 62 6c 65 20 63 75 72 73 6f 72 2c 20 69 66 20 72  ble cursor, if r
2ff89 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20 20 64  equired */.    d
2ff8a 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65  isableTerm(pLeve
2ff8b 6c 2c 20 70 52 61 6e 67 65 53 74 61 72 74 29 3b  l, pRangeStart);
2ff8c 0a 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d  .    disableTerm
2ff8d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e 67 65 45  (pLevel, pRangeE
2ff8e 6e 64 29 3b 0a 20 20 20 20 69 66 28 20 21 6f 6d  nd);.    if( !om
2ff8f 69 74 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  itTable ){.     
2ff90 20 69 52 6f 77 69 64 52 65 67 20 3d 20 69 52 65   iRowidReg = iRe
2ff91 6c 65 61 73 65 52 65 67 20 3d 20 73 71 6c 69 74  leaseReg = sqlit
2ff92 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2ff93 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  rse);.      sqli
2ff94 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2ff95 20 4f 50 5f 49 64 78 52 6f 77 69 64 2c 20 69 49   OP_IdxRowid, iI
2ff96 64 78 43 75 72 2c 20 69 52 6f 77 69 64 52 65 67  dxCur, iRowidReg
2ff97 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2ff98 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 70  ExprCacheStore(p
2ff99 50 61 72 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c  Parse, iCur, -1,
2ff9a 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20   iRowidReg);.   
2ff9b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2ff9c 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 65 6b 2c  dOp2(v, OP_Seek,
2ff9d 20 69 43 75 72 2c 20 69 52 6f 77 69 64 52 65 67   iCur, iRowidReg
2ff9e 29 3b 20 20 2f 2a 20 44 65 66 65 72 72 65 64 20  );  /* Deferred 
2ff9f 73 65 65 6b 20 2a 2f 0a 20 20 20 20 7d 0a 0a 20  seek */.    }.. 
2ffa0 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 74 68 65     /* Record the
2ffa1 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 75 73 65   instruction use
2ffa2 64 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74  d to terminate t
2ffa3 68 65 20 6c 6f 6f 70 2e 20 44 69 73 61 62 6c 65  he loop. Disable
2ffa4 20 0a 20 20 20 20 2a 2a 20 57 48 45 52 45 20 63   .    ** WHERE c
2ffa5 6c 61 75 73 65 20 74 65 72 6d 73 20 6d 61 64 65  lause terms made
2ffa6 20 72 65 64 75 6e 64 61 6e 74 20 62 79 20 74 68   redundant by th
2ffa7 65 20 69 6e 64 65 78 20 72 61 6e 67 65 20 73 63  e index range sc
2ffa8 61 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  an..    */.    p
2ffa9 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76  Level->op = bRev
2ffaa 20 3f 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f   ? OP_Prev : OP_
2ffab 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c  Next;.    pLevel
2ffac 2d 3e 70 31 20 3d 20 69 49 64 78 43 75 72 3b 0a  ->p1 = iIdxCur;.
2ffad 20 20 7d 65 6c 73 65 0a 0a 23 69 66 6e 64 65 66    }else..#ifndef
2ffae 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f   SQLITE_OMIT_OR_
2ffaf 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 69  OPTIMIZATION.  i
2ffb0 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  f( pLevel->plan.
2ffb1 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2ffb2 4d 55 4c 54 49 5f 4f 52 20 29 7b 0a 20 20 20 20  MULTI_OR ){.    
2ffb3 2f 2a 20 43 61 73 65 20 34 3a 20 20 54 77 6f 20  /* Case 4:  Two 
2ffb4 6f 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65  or more separate
2ffb5 6c 79 20 69 6e 64 65 78 65 64 20 74 65 72 6d 73  ly indexed terms
2ffb6 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 4f 52   connected by OR
2ffb7 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45  .    **.    ** E
2ffb8 78 61 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20  xample:.    **. 
2ffb9 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 54     **   CREATE T
2ffba 41 42 4c 45 20 74 31 28 61 2c 62 2c 63 2c 64 29  ABLE t1(a,b,c,d)
2ffbb 3b 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54  ;.    **   CREAT
2ffbc 45 20 49 4e 44 45 58 20 69 31 20 4f 4e 20 74 31  E INDEX i1 ON t1
2ffbd 28 61 29 3b 0a 20 20 20 20 2a 2a 20 20 20 43 52  (a);.    **   CR
2ffbe 45 41 54 45 20 49 4e 44 45 58 20 69 32 20 4f 4e  EATE INDEX i2 ON
2ffbf 20 74 31 28 62 29 3b 0a 20 20 20 20 2a 2a 20 20   t1(b);.    **  
2ffc0 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 33   CREATE INDEX i3
2ffc1 20 4f 4e 20 74 31 28 63 29 3b 0a 20 20 20 20 2a   ON t1(c);.    *
2ffc2 2a 0a 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43  *.    **   SELEC
2ffc3 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52  T * FROM t1 WHER
2ffc4 45 20 61 3d 35 20 4f 52 20 62 3d 37 20 4f 52 20  E a=5 OR b=7 OR 
2ffc5 28 63 3d 31 31 20 41 4e 44 20 64 3d 31 33 29 0a  (c=11 AND d=13).
2ffc6 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 6e      **.    ** In
2ffc7 20 74 68 65 20 65 78 61 6d 70 6c 65 2c 20 74 68   the example, th
2ffc8 65 72 65 20 61 72 65 20 74 68 72 65 65 20 69 6e  ere are three in
2ffc9 64 65 78 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e  dexed terms conn
2ffca 65 63 74 65 64 20 62 79 20 4f 52 2e 0a 20 20 20  ected by OR..   
2ffcb 20 2a 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74   ** The top of t
2ffcc 68 65 20 6c 6f 6f 70 20 6c 6f 6f 6b 73 20 6c 69  he loop looks li
2ffcd 6b 65 20 74 68 69 73 3a 0a 20 20 20 20 2a 2a 0a  ke this:.    **.
2ffce 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
2ffcf 4e 75 6c 6c 20 20 20 20 20 20 20 31 20 20 20 20  Null       1    
2ffd0 20 20 20 20 20 20 20 20 20 20 20 20 23 20 5a 65              # Ze
2ffd1 72 6f 20 74 68 65 20 72 6f 77 73 65 74 20 69 6e  ro the rowset in
2ffd2 20 72 65 67 20 31 0a 20 20 20 20 2a 2a 0a 20 20   reg 1.    **.  
2ffd3 20 20 2a 2a 20 54 68 65 6e 2c 20 66 6f 72 20 65    ** Then, for e
2ffd4 61 63 68 20 69 6e 64 65 78 65 64 20 74 65 72 6d  ach indexed term
2ffd5 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 2e  , the following.
2ffd6 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74   The arguments t
2ffd7 6f 0a 20 20 20 20 2a 2a 20 52 6f 77 53 65 74 54  o.    ** RowSetT
2ffd8 65 73 74 20 61 72 65 20 73 75 63 68 20 74 68 61  est are such tha
2ffd9 74 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74  t the rowid of t
2ffda 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 69  he current row i
2ffdb 73 20 69 6e 73 65 72 74 65 64 0a 20 20 20 20 2a  s inserted.    *
2ffdc 2a 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65  * into the RowSe
2ffdd 74 2e 20 49 66 20 69 74 20 69 73 20 61 6c 72 65  t. If it is alre
2ffde 61 64 79 20 70 72 65 73 65 6e 74 2c 20 63 6f 6e  ady present, con
2ffdf 74 72 6f 6c 20 73 6b 69 70 73 20 74 68 65 0a 20  trol skips the. 
2ffe0 20 20 20 2a 2a 20 47 6f 73 75 62 20 6f 70 63 6f     ** Gosub opco
2ffe1 64 65 20 61 6e 64 20 6a 75 6d 70 73 20 73 74 72  de and jumps str
2ffe2 61 69 67 68 74 20 74 6f 20 74 68 65 20 63 6f 64  aight to the cod
2ffe3 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 57  e generated by W
2ffe4 68 65 72 65 45 6e 64 28 29 2e 0a 20 20 20 20 2a  hereEnd()..    *
2ffe5 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
2ffe6 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
2ffe7 6e 28 3c 74 65 72 6d 3e 29 0a 20 20 20 20 2a 2a  n(<term>).    **
2ffe8 20 20 20 20 20 20 20 20 20 20 52 6f 77 53 65 74            RowSet
2ffe9 54 65 73 74 20 20 20 20 20 20 20 20 20 20 20 20  Test            
2ffea 20 20 20 20 20 20 23 20 49 6e 73 65 72 74 20 72        # Insert r
2ffeb 6f 77 69 64 20 69 6e 74 6f 20 72 6f 77 73 65 74  owid into rowset
2ffec 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2ffed 20 47 6f 73 75 62 20 20 20 20 20 20 32 20 41 0a   Gosub      2 A.
2ffee 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 73 71      **        sq
2ffef 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29 0a  lite3WhereEnd().
2fff0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f      **.    ** Fo
2fff1 6c 6c 6f 77 69 6e 67 20 74 68 65 20 61 62 6f 76  llowing the abov
2fff2 65 2c 20 63 6f 64 65 20 74 6f 20 74 65 72 6d 69  e, code to termi
2fff3 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20 4c  nate the loop. L
2fff4 61 62 65 6c 20 41 2c 20 74 68 65 20 74 61 72 67  abel A, the targ
2fff5 65 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  et.    ** of the
2fff6 20 47 6f 73 75 62 20 61 62 6f 76 65 2c 20 6a 75   Gosub above, ju
2fff7 6d 70 73 20 74 6f 20 74 68 65 20 69 6e 73 74 72  mps to the instr
2fff8 75 63 74 69 6f 6e 20 72 69 67 68 74 20 61 66 74  uction right aft
2fff9 65 72 20 74 68 65 20 47 6f 74 6f 2e 0a 20 20 20  er the Goto..   
2fffa 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
2fffb 20 20 20 20 4e 75 6c 6c 20 20 20 20 20 20 20 31      Null       1
2fffc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fffd 23 20 5a 65 72 6f 20 74 68 65 20 72 6f 77 73 65  # Zero the rowse
2fffe 74 20 69 6e 20 72 65 67 20 31 0a 20 20 20 20 2a  t in reg 1.    *
2ffff 2a 20 20 20 20 20 20 20 20 20 20 47 6f 74 6f 20  *          Goto 
30000 20 20 20 20 20 20 42 20 20 20 20 20 20 20 20 20        B         
30001 20 20 20 20 20 20 20 23 20 54 68 65 20 6c 6f 6f         # The loo
30002 70 20 69 73 20 66 69 6e 69 73 68 65 64 2e 0a 20  p is finished.. 
30003 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
30004 20 20 20 41 3a 20 3c 6c 6f 6f 70 20 62 6f 64 79     A: <loop body
30005 3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  >               
30006 20 20 23 20 52 65 74 75 72 6e 20 64 61 74 61 2c    # Return data,
30007 20 77 68 61 74 65 76 65 72 2e 0a 20 20 20 20 2a   whatever..    *
30008 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
30009 20 20 52 65 74 75 72 6e 20 20 20 20 20 32 20 20    Return     2  
3000a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23 20                # 
3000b 4a 75 6d 70 20 62 61 63 6b 20 74 6f 20 74 68 65  Jump back to the
3000c 20 47 6f 73 75 62 0a 20 20 20 20 2a 2a 0a 20 20   Gosub.    **.  
3000d 20 20 2a 2a 20 20 20 20 20 20 20 42 3a 20 3c 61    **       B: <a
3000e 66 74 65 72 20 74 68 65 20 6c 6f 6f 70 3e 0a 20  fter the loop>. 
3000f 20 20 20 2a 2a 0a 20 20 20 20 2a 2f 0a 20 20 20     **.    */.   
30010 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 4f   WhereClause *pO
30011 72 57 63 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f  rWc;    /* The O
30012 52 2d 63 6c 61 75 73 65 20 62 72 6f 6b 65 6e 20  R-clause broken 
30013 6f 75 74 20 69 6e 74 6f 20 73 75 62 74 65 72 6d  out into subterm
30014 73 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 54 65  s */.    WhereTe
30015 72 6d 20 2a 70 46 69 6e 61 6c 3b 20 20 20 20 20  rm *pFinal;     
30016 2f 2a 20 46 69 6e 61 6c 20 73 75 62 74 65 72 6d  /* Final subterm
30017 20 77 69 74 68 69 6e 20 74 68 65 20 4f 52 2d 63   within the OR-c
30018 6c 61 75 73 65 2e 20 2a 2f 0a 20 20 20 20 53 72  lause. */.    Sr
30019 63 4c 69 73 74 20 6f 6e 65 54 61 62 3b 20 20 20  cList oneTab;   
3001a 20 20 20 20 20 2f 2a 20 53 68 6f 72 74 65 6e 65       /* Shortene
3001b 64 20 74 61 62 6c 65 20 6c 69 73 74 20 2a 2f 0a  d table list */.
3001c 0a 20 20 20 20 69 6e 74 20 72 65 67 52 65 74 75  .    int regRetu
3001d 72 6e 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  rn = ++pParse->n
3001e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Mem;           /
3001f 2a 20 52 65 67 69 73 74 65 72 20 75 73 65 64 20  * Register used 
30020 77 69 74 68 20 4f 50 5f 47 6f 73 75 62 20 2a 2f  with OP_Gosub */
30021 0a 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 73  .    int regRows
30022 65 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  et = 0;         
30023 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
30024 2a 20 52 65 67 69 73 74 65 72 20 66 6f 72 20 52  * Register for R
30025 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a  owSet object */.
30026 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64      int regRowid
30027 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
30028 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30029 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
3002a 67 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 69  g rowid */.    i
3002b 6e 74 20 69 4c 6f 6f 70 42 6f 64 79 20 3d 20 73  nt iLoopBody = s
3002c 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
3002d 62 65 6c 28 76 29 3b 20 20 2f 2a 20 53 74 61 72  bel(v);  /* Star
3002e 74 20 6f 66 20 6c 6f 6f 70 20 62 6f 64 79 20 2a  t of loop body *
3002f 2f 0a 20 20 20 20 69 6e 74 20 69 52 65 74 49 6e  /.    int iRetIn
30030 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  it;             
30031 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30032 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 72 65  /* Address of re
30033 67 52 65 74 75 72 6e 20 69 6e 69 74 20 2a 2f 0a  gReturn init */.
30034 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 0a      int ii;.   .
30035 20 20 20 20 70 54 65 72 6d 20 3d 20 70 4c 65 76      pTerm = pLev
30036 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 54 65 72 6d  el->plan.u.pTerm
30037 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
30038 65 72 6d 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  erm!=0 );.    as
30039 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 65 4f 70  sert( pTerm->eOp
3003a 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 29 3b  erator==WO_OR );
3003b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54  .    assert( (pT
3003c 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
3003d 45 52 4d 5f 4f 52 49 4e 46 4f 29 21 3d 30 20 29  ERM_ORINFO)!=0 )
3003e 3b 0a 20 20 20 20 70 4f 72 57 63 20 3d 20 26 70  ;.    pOrWc = &p
3003f 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d  Term->u.pOrInfo-
30040 3e 77 63 3b 0a 20 20 20 20 70 46 69 6e 61 6c 20  >wc;.    pFinal 
30041 3d 20 26 70 4f 72 57 63 2d 3e 61 5b 70 4f 72 57  = &pOrWc->a[pOrW
30042 63 2d 3e 6e 54 65 72 6d 2d 31 5d 3b 0a 0a 20 20  c->nTerm-1];..  
30043 20 20 2f 2a 20 53 65 74 20 75 70 20 61 20 53 72    /* Set up a Sr
30044 63 4c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67  cList containing
30045 20 6a 75 73 74 20 74 68 65 20 74 61 62 6c 65 20   just the table 
30046 62 65 69 6e 67 20 73 63 61 6e 6e 65 64 20 62 79  being scanned by
30047 20 74 68 69 73 20 6c 6f 6f 70 2e 20 2a 2f 0a 20   this loop. */. 
30048 20 20 20 6f 6e 65 54 61 62 2e 6e 53 72 63 20 3d     oneTab.nSrc =
30049 20 31 3b 0a 20 20 20 20 6f 6e 65 54 61 62 2e 6e   1;.    oneTab.n
3004a 41 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 20 20 6f  Alloc = 1;.    o
3004b 6e 65 54 61 62 2e 61 5b 30 5d 20 3d 20 2a 70 54  neTab.a[0] = *pT
3004c 61 62 49 74 65 6d 3b 0a 0a 20 20 20 20 2f 2a 20  abItem;..    /* 
3004d 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 72  Initialize the r
3004e 6f 77 73 65 74 20 72 65 67 69 73 74 65 72 20 74  owset register t
3004f 6f 20 63 6f 6e 74 61 69 6e 20 4e 55 4c 4c 2e 20  o contain NULL. 
30050 41 6e 20 53 51 4c 20 4e 55 4c 4c 20 69 73 20 0a  An SQL NULL is .
30051 20 20 20 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e      ** equivalen
30052 74 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 72 6f  t to an empty ro
30053 77 73 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  wset..    **.   
30054 20 2a 2a 20 41 6c 73 6f 20 69 6e 69 74 69 61 6c   ** Also initial
30055 69 7a 65 20 72 65 67 52 65 74 75 72 6e 20 74 6f  ize regReturn to
30056 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 61 64 64   contain the add
30057 72 65 73 73 20 6f 66 20 74 68 65 20 69 6e 73 74  ress of the inst
30058 72 75 63 74 69 6f 6e 20 0a 20 20 20 20 2a 2a 20  ruction .    ** 
30059 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c  immediately foll
3005a 6f 77 69 6e 67 20 74 68 65 20 4f 50 5f 52 65 74  owing the OP_Ret
3005b 75 72 6e 20 61 74 20 74 68 65 20 62 6f 74 74 6f  urn at the botto
3005c 6d 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 54  m of the loop. T
3005d 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 72 65  his.    ** is re
3005e 71 75 69 72 65 64 20 69 6e 20 61 20 66 65 77 20  quired in a few 
3005f 6f 62 73 63 75 72 65 20 4c 45 46 54 20 4a 4f 49  obscure LEFT JOI
30060 4e 20 63 61 73 65 73 20 77 68 65 72 65 20 63 6f  N cases where co
30061 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a 20 20 20 20  ntrol jumps.    
30062 2a 2a 20 6f 76 65 72 20 74 68 65 20 74 6f 70 20  ** over the top 
30063 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 6e 74 6f  of the loop into
30064 20 74 68 65 20 62 6f 64 79 20 6f 66 20 69 74 2e   the body of it.
30065 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
30066 65 20 0a 20 20 20 20 2a 2a 20 63 6f 72 72 65 63  e .    ** correc
30067 74 20 72 65 73 70 6f 6e 73 65 20 66 6f 72 20 74  t response for t
30068 68 65 20 65 6e 64 2d 6f 66 2d 6c 6f 6f 70 20 63  he end-of-loop c
30069 6f 64 65 20 28 74 68 65 20 4f 50 5f 52 65 74 75  ode (the OP_Retu
3006a 72 6e 29 20 69 73 20 74 6f 20 0a 20 20 20 20 2a  rn) is to .    *
3006b 2a 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  * fall through t
3006c 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  o the next instr
3006d 75 63 74 69 6f 6e 2c 20 6a 75 73 74 20 61 73 20  uction, just as 
3006e 61 6e 20 4f 50 5f 4e 65 78 74 20 64 6f 65 73 20  an OP_Next does 
3006f 69 66 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 65 64  if.    ** called
30070 20 6f 6e 20 61 6e 20 75 6e 69 6e 69 74 69 61 6c   on an uninitial
30071 69 7a 65 64 20 63 75 72 73 6f 72 2e 0a 20 20 20  ized cursor..   
30072 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 77 63 74   */.    if( (wct
30073 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  rlFlags & WHERE_
30074 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3d 3d  DUPLICATES_OK)==
30075 30 20 29 7b 0a 20 20 20 20 20 20 72 65 67 52 6f  0 ){.      regRo
30076 77 73 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d  wset = ++pParse-
30077 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 72 65 67  >nMem;.      reg
30078 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65  Rowid = ++pParse
30079 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71  ->nMem;.      sq
3007a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
3007b 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
3007c 65 67 52 6f 77 73 65 74 29 3b 0a 20 20 20 20 7d  egRowset);.    }
3007d 0a 20 20 20 20 69 52 65 74 49 6e 69 74 20 3d 20  .    iRetInit = 
3007e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3007f 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
30080 20 30 2c 20 72 65 67 52 65 74 75 72 6e 29 3b 0a   0, regReturn);.
30081 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  .    for(ii=0; i
30082 69 3c 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 3b 20  i<pOrWc->nTerm; 
30083 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 57 68 65  ii++){.      Whe
30084 72 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 20  reTerm *pOrTerm 
30085 3d 20 26 70 4f 72 57 63 2d 3e 61 5b 69 69 5d 3b  = &pOrWc->a[ii];
30086 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65  .      if( pOrTe
30087 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d  rm->leftCursor==
30088 69 43 75 72 20 7c 7c 20 70 4f 72 54 65 72 6d 2d  iCur || pOrTerm-
30089 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41  >eOperator==WO_A
3008a 4e 44 20 29 7b 0a 20 20 20 20 20 20 20 20 57 68  ND ){.        Wh
3008b 65 72 65 49 6e 66 6f 20 2a 70 53 75 62 57 49 6e  ereInfo *pSubWIn
3008c 66 6f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  fo;          /* 
3008d 49 6e 66 6f 20 66 6f 72 20 73 69 6e 67 6c 65 20  Info for single 
3008e 4f 52 2d 74 65 72 6d 20 73 63 61 6e 20 2a 2f 0a  OR-term scan */.
3008f 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
30090 74 68 72 6f 75 67 68 20 74 61 62 6c 65 20 65 6e  through table en
30091 74 72 69 65 73 20 74 68 61 74 20 6d 61 74 63 68  tries that match
30092 20 74 65 72 6d 20 70 4f 72 54 65 72 6d 2e 20 2a   term pOrTerm. *
30093 2f 0a 20 20 20 20 20 20 20 20 70 53 75 62 57 49  /.        pSubWI
30094 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65  nfo = sqlite3Whe
30095 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20  reBegin(pParse, 
30096 26 6f 6e 65 54 61 62 2c 20 70 4f 72 54 65 72 6d  &oneTab, pOrTerm
30097 2d 3e 70 45 78 70 72 2c 20 30 2c 0a 20 20 20 20  ->pExpr, 0,.    
30098 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30099 20 20 20 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f      WHERE_OMIT_O
3009a 50 45 4e 20 7c 20 57 48 45 52 45 5f 4f 4d 49 54  PEN | WHERE_OMIT
3009b 5f 43 4c 4f 53 45 20 7c 20 57 48 45 52 45 5f 46  _CLOSE | WHERE_F
3009c 4f 52 43 45 5f 54 41 42 4c 45 29 3b 0a 20 20 20  ORCE_TABLE);.   
3009d 20 20 20 20 20 69 66 28 20 70 53 75 62 57 49 6e       if( pSubWIn
3009e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  fo ){.          
3009f 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20  if( (wctrlFlags 
300a0 26 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54  & WHERE_DUPLICAT
300a1 45 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0a 20 20 20  ES_OK)==0 ){.   
300a2 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 53 65           int iSe
300a3 74 20 3d 20 28 28 69 69 3d 3d 70 4f 72 57 63 2d  t = ((ii==pOrWc-
300a4 3e 6e 54 65 72 6d 2d 31 29 3f 2d 31 3a 69 69 29  >nTerm-1)?-1:ii)
300a5 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  ;.            in
300a6 74 20 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20  t r;.           
300a7 20 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72   r = sqlite3Expr
300a8 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50  CodeGetColumn(pP
300a9 61 72 73 65 2c 20 70 54 61 62 49 74 65 6d 2d 3e  arse, pTabItem->
300aa 70 54 61 62 2c 20 2d 31 2c 20 69 43 75 72 2c 20  pTab, -1, iCur, 
300ab 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
300ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
300ad 20 20 20 20 20 20 20 20 20 20 72 65 67 52 6f 77            regRow
300ae 69 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  id, 0);.        
300af 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
300b0 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 6f 77 53  ddOp4(v, OP_RowS
300b1 65 74 54 65 73 74 2c 20 72 65 67 52 6f 77 73 65  etTest, regRowse
300b2 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
300b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
300b4 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
300b5 65 6e 74 41 64 64 72 28 76 29 2b 32 2c 0a 20 20  entAddr(v)+2,.  
300b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
300b7 20 20 20 20 20 20 20 20 20 20 20 20 72 2c 20 53              r, S
300b8 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
300b9 28 69 53 65 74 29 2c 20 50 34 5f 49 4e 54 33 32  (iSet), P4_INT32
300ba 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
300bb 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
300bc 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
300bd 5f 47 6f 73 75 62 2c 20 72 65 67 52 65 74 75 72  _Gosub, regRetur
300be 6e 2c 20 69 4c 6f 6f 70 42 6f 64 79 29 3b 0a 0a  n, iLoopBody);..
300bf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e            /* Fin
300c0 69 73 68 20 74 68 65 20 6c 6f 6f 70 20 74 68 72  ish the loop thr
300c1 6f 75 67 68 20 74 61 62 6c 65 20 65 6e 74 72 69  ough table entri
300c2 65 73 20 74 68 61 74 20 6d 61 74 63 68 20 74 65  es that match te
300c3 72 6d 20 70 4f 72 54 65 72 6d 2e 20 2a 2f 0a 20  rm pOrTerm. */. 
300c4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
300c5 57 68 65 72 65 45 6e 64 28 70 53 75 62 57 49 6e  WhereEnd(pSubWIn
300c6 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  fo);.        }. 
300c7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
300c8 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
300c9 67 65 50 31 28 76 2c 20 69 52 65 74 49 6e 69 74  geP1(v, iRetInit
300ca 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  , sqlite3VdbeCur
300cb 72 65 6e 74 41 64 64 72 28 76 29 29 3b 0a 20 20  rentAddr(v));.  
300cc 20 20 2f 2a 20 73 71 6c 69 74 65 33 56 64 62 65    /* sqlite3Vdbe
300cd 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
300ce 6c 2c 20 30 2c 20 72 65 67 52 6f 77 73 65 74 29  l, 0, regRowset)
300cf 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  ; */.    sqlite3
300d0 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
300d1 5f 47 6f 74 6f 2c 20 30 2c 20 70 4c 65 76 65 6c  _Goto, 0, pLevel
300d2 2d 3e 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20  ->addrBrk);.    
300d3 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
300d4 76 65 4c 61 62 65 6c 28 76 2c 20 69 4c 6f 6f 70  veLabel(v, iLoop
300d5 42 6f 64 79 29 3b 0a 0a 20 20 20 20 70 4c 65 76  Body);..    pLev
300d6 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 52 65 74 75  el->op = OP_Retu
300d7 72 6e 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  rn;.    pLevel->
300d8 70 31 20 3d 20 72 65 67 52 65 74 75 72 6e 3b 0a  p1 = regReturn;.
300d9 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28      disableTerm(
300da 70 4c 65 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0a  pLevel, pTerm);.
300db 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
300dc 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52  * SQLITE_OMIT_OR
300dd 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
300de 0a 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 43 61 73  ..  {.    /* Cas
300df 65 20 35 3a 20 20 54 68 65 72 65 20 69 73 20 6e  e 5:  There is n
300e0 6f 20 75 73 61 62 6c 65 20 69 6e 64 65 78 2e 20  o usable index. 
300e1 20 57 65 20 6d 75 73 74 20 64 6f 20 61 20 63 6f   We must do a co
300e2 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 20 20  mplete.    **   
300e3 20 20 20 20 20 20 20 73 63 61 6e 20 6f 66 20 74         scan of t
300e4 68 65 20 65 6e 74 69 72 65 20 74 61 62 6c 65 2e  he entire table.
300e5 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 61 74  .    */.    stat
300e6 69 63 20 63 6f 6e 73 74 20 75 38 20 61 53 74 65  ic const u8 aSte
300e7 70 5b 5d 20 3d 20 7b 20 4f 50 5f 4e 65 78 74 2c  p[] = { OP_Next,
300e8 20 4f 50 5f 50 72 65 76 20 7d 3b 0a 20 20 20 20   OP_Prev };.    
300e9 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
300ea 61 53 74 61 72 74 5b 5d 20 3d 20 7b 20 4f 50 5f  aStart[] = { OP_
300eb 52 65 77 69 6e 64 2c 20 4f 50 5f 4c 61 73 74 20  Rewind, OP_Last 
300ec 7d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 62  };.    assert( b
300ed 52 65 76 3d 3d 30 20 7c 7c 20 62 52 65 76 3d 3d  Rev==0 || bRev==
300ee 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  1 );.    assert(
300ef 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b   omitTable==0 );
300f0 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20  .    pLevel->op 
300f1 3d 20 61 53 74 65 70 5b 62 52 65 76 5d 3b 0a 20  = aStep[bRev];. 
300f2 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20     pLevel->p1 = 
300f3 69 43 75 72 3b 0a 20 20 20 20 70 4c 65 76 65 6c  iCur;.    pLevel
300f4 2d 3e 70 32 20 3d 20 31 20 2b 20 73 71 6c 69 74  ->p2 = 1 + sqlit
300f5 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
300f6 61 53 74 61 72 74 5b 62 52 65 76 5d 2c 20 69 43  aStart[bRev], iC
300f7 75 72 2c 20 61 64 64 72 42 72 6b 29 3b 0a 20 20  ur, addrBrk);.  
300f8 20 20 70 4c 65 76 65 6c 2d 3e 70 35 20 3d 20 53    pLevel->p5 = S
300f9 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
300fa 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3b 0a  _FULLSCAN_STEP;.
300fb 20 20 7d 0a 20 20 6e 6f 74 52 65 61 64 79 20 26    }.  notReady &
300fc 3d 20 7e 67 65 74 4d 61 73 6b 28 70 57 43 2d 3e  = ~getMask(pWC->
300fd 70 4d 61 73 6b 53 65 74 2c 20 69 43 75 72 29 3b  pMaskSet, iCur);
300fe 0a 0a 20 20 2f 2a 20 49 6e 73 65 72 74 20 63 6f  ..  /* Insert co
300ff 64 65 20 74 6f 20 74 65 73 74 20 65 76 65 72 79  de to test every
30100 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 74   subexpression t
30101 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c  hat can be compl
30102 65 74 65 6c 79 0a 20 20 2a 2a 20 63 6f 6d 70 75  etely.  ** compu
30103 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 63 75  ted using the cu
30104 72 72 65 6e 74 20 73 65 74 20 6f 66 20 74 61 62  rrent set of tab
30105 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20  les..  */.  k = 
30106 30 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d 70  0;.  for(pTerm=p
30107 57 43 2d 3e 61 2c 20 6a 3d 70 57 43 2d 3e 6e 54  WC->a, j=pWC->nT
30108 65 72 6d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70  erm; j>0; j--, p
30109 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70  Term++){.    Exp
3010a 72 20 2a 70 45 3b 0a 20 20 20 20 74 65 73 74 63  r *pE;.    testc
3010b 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c  ase( pTerm->wtFl
3010c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55  ags & TERM_VIRTU
3010d 41 4c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  AL );.    testca
3010e 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61  se( pTerm->wtFla
3010f 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 20  gs & TERM_CODED 
30110 29 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  );.    if( pTerm
30111 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52  ->wtFlags & (TER
30112 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43  M_VIRTUAL|TERM_C
30113 4f 44 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65  ODED) ) continue
30114 3b 0a 20 20 20 20 69 66 28 20 28 70 54 65 72 6d  ;.    if( (pTerm
30115 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f  ->prereqAll & no
30116 74 52 65 61 64 79 29 21 3d 30 20 29 20 63 6f 6e  tReady)!=0 ) con
30117 74 69 6e 75 65 3b 0a 20 20 20 20 70 45 20 3d 20  tinue;.    pE = 
30118 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
30119 20 20 61 73 73 65 72 74 28 20 70 45 21 3d 30 20    assert( pE!=0 
3011a 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  );.    if( pLeve
3011b 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 26 26 20  l->iLeftJoin && 
3011c 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
3011d 28 70 45 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e  (pE, EP_FromJoin
3011e 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69  ) ){.      conti
3011f 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  nue;.    }.    s
30120 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
30121 65 28 70 50 61 72 73 65 2c 20 70 45 2c 20 61 64  e(pParse, pE, ad
30122 64 72 43 6f 6e 74 2c 20 53 51 4c 49 54 45 5f 4a  drCont, SQLITE_J
30123 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
30124 6b 20 3d 20 31 3b 0a 20 20 20 20 70 54 65 72 6d  k = 1;.    pTerm
30125 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52  ->wtFlags |= TER
30126 4d 5f 43 4f 44 45 44 3b 0a 20 20 7d 0a 0a 20 20  M_CODED;.  }..  
30127 2f 2a 20 46 6f 72 20 61 20 4c 45 46 54 20 4f 55  /* For a LEFT OU
30128 54 45 52 20 4a 4f 49 4e 2c 20 67 65 6e 65 72 61  TER JOIN, genera
30129 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
3012a 6c 20 72 65 63 6f 72 64 20 74 68 65 20 66 61 63  l record the fac
3012b 74 20 74 68 61 74 0a 20 20 2a 2a 20 61 74 20 6c  t that.  ** at l
3012c 65 61 73 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20  east one row of 
3012d 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
3012e 68 61 73 20 6d 61 74 63 68 65 64 20 74 68 65 20  has matched the 
3012f 6c 65 66 74 20 74 61 62 6c 65 2e 20 20 0a 20 20  left table.  .  
30130 2a 2f 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  */.  if( pLevel-
30131 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 7b 0a 20 20  >iLeftJoin ){.  
30132 20 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 46 69    pLevel->addrFi
30133 72 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  rst = sqlite3Vdb
30134 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
30135 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
30136 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
30137 65 67 65 72 2c 20 31 2c 20 70 4c 65 76 65 6c 2d  eger, 1, pLevel-
30138 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20  >iLeftJoin);.   
30139 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
3013a 20 22 72 65 63 6f 72 64 20 4c 45 46 54 20 4a 4f   "record LEFT JO
3013b 49 4e 20 68 69 74 22 29 29 3b 0a 20 20 20 20 73  IN hit"));.    s
3013c 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
3013d 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
3013e 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d    for(pTerm=pWC-
3013f 3e 61 2c 20 6a 3d 30 3b 20 6a 3c 70 57 43 2d 3e  >a, j=0; j<pWC->
30140 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70 54 65 72  nTerm; j++, pTer
30141 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 74 65 73 74  m++){.      test
30142 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46  case( pTerm->wtF
30143 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54  lags & TERM_VIRT
30144 55 41 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73  UAL );.      tes
30145 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 74  tcase( pTerm->wt
30146 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44  Flags & TERM_COD
30147 45 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ED );.      if( 
30148 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  pTerm->wtFlags &
30149 20 28 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54   (TERM_VIRTUAL|T
3014a 45 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f 6e  ERM_CODED) ) con
3014b 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28  tinue;.      if(
3014c 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41   (pTerm->prereqA
3014d 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79 29 21 3d  ll & notReady)!=
3014e 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
3014f 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72      assert( pTer
30150 6d 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20  m->pExpr );.    
30151 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
30152 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 65  alse(pParse, pTe
30153 72 6d 2d 3e 70 45 78 70 72 2c 20 61 64 64 72 43  rm->pExpr, addrC
30154 6f 6e 74 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  ont, SQLITE_JUMP
30155 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 70  IFNULL);.      p
30156 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d  Term->wtFlags |=
30157 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20   TERM_CODED;.   
30158 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
30159 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
3015a 50 61 72 73 65 2c 20 69 52 65 6c 65 61 73 65 52  Parse, iReleaseR
3015b 65 67 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6e  eg);..  return n
3015c 6f 74 52 65 61 64 79 3b 0a 7d 0a 0a 23 69 66 20  otReady;.}..#if 
3015d 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
3015e 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  EST)./*.** The f
3015f 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
30160 65 20 68 6f 6c 64 73 20 61 20 74 65 78 74 20 64  e holds a text d
30161 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 71 75  escription of qu
30162 65 72 79 20 70 6c 61 6e 20 67 65 6e 65 72 61 74  ery plan generat
30163 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 6f 73  ed.** by the mos
30164 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f  t recent call to
30165 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
30166 69 6e 28 29 2e 20 20 45 61 63 68 20 63 61 6c 6c  in().  Each call
30167 20 74 6f 20 57 68 65 72 65 42 65 67 69 6e 0a 2a   to WhereBegin.*
30168 2a 20 6f 76 65 72 77 72 69 74 65 73 20 74 68 65  * overwrites the
30169 20 70 72 65 76 69 6f 75 73 2e 20 20 54 68 69 73   previous.  This
3016a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
3016b 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
3016c 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c 79 73 69 73   and.** analysis
3016d 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45   only..*/.SQLITE
3016e 5f 41 50 49 20 63 68 61 72 20 73 71 6c 69 74 65  _API char sqlite
3016f 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 42 4d 53  3_query_plan[BMS
30170 2a 32 2a 34 30 5d 3b 20 20 2f 2a 20 54 65 78 74  *2*40];  /* Text
30171 20 6f 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a   of the join */.
30172 73 74 61 74 69 63 20 69 6e 74 20 6e 51 50 6c 61  static int nQPla
30173 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  n = 0;          
30174 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65      /* Next free
30175 20 73 6c 6f 77 20 69 6e 20 5f 71 75 65 72 79 5f   slow in _query_
30176 70 6c 61 6e 5b 5d 20 2a 2f 0a 0a 23 65 6e 64 69  plan[] */..#endi
30177 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54  f /* SQLITE_TEST
30178 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65   */.../*.** Free
30179 20 61 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72   a WhereInfo str
3017a 75 63 74 75 72 65 0a 2a 2f 0a 73 74 61 74 69 63  ucture.*/.static
3017b 20 76 6f 69 64 20 77 68 65 72 65 49 6e 66 6f 46   void whereInfoF
3017c 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ree(sqlite3 *db,
3017d 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e   WhereInfo *pWIn
3017e 66 6f 29 7b 0a 20 20 69 66 28 20 70 57 49 6e 66  fo){.  if( pWInf
3017f 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  o ){.    int i;.
30180 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
30181 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3b 20 69  WInfo->nLevel; i
30182 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
30183 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
30184 49 6e 66 6f 20 3d 20 70 57 49 6e 66 6f 2d 3e 61  Info = pWInfo->a
30185 5b 69 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20 20  [i].pIdxInfo;.  
30186 20 20 20 20 69 66 28 20 70 49 6e 66 6f 20 29 7b      if( pInfo ){
30187 0a 20 20 20 20 20 20 20 20 2f 2a 20 61 73 73 65  .        /* asse
30188 72 74 28 20 70 49 6e 66 6f 2d 3e 6e 65 65 64 54  rt( pInfo->needT
30189 6f 46 72 65 65 49 64 78 53 74 72 3d 3d 30 20 7c  oFreeIdxStr==0 |
3018a 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
3018b 65 64 20 29 3b 20 2a 2f 0a 20 20 20 20 20 20 20  ed ); */.       
3018c 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 65 65 64   if( pInfo->need
3018d 54 6f 46 72 65 65 49 64 78 53 74 72 20 29 7b 0a  ToFreeIdxStr ){.
3018e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3018f 33 5f 66 72 65 65 28 70 49 6e 66 6f 2d 3e 69 64  3_free(pInfo->id
30190 78 53 74 72 29 3b 0a 20 20 20 20 20 20 20 20 7d  xStr);.        }
30191 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
30192 44 62 46 72 65 65 28 64 62 2c 20 70 49 6e 66 6f  DbFree(db, pInfo
30193 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
30194 0a 20 20 20 20 77 68 65 72 65 43 6c 61 75 73 65  .    whereClause
30195 43 6c 65 61 72 28 70 57 49 6e 66 6f 2d 3e 70 57  Clear(pWInfo->pW
30196 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  C);.    sqlite3D
30197 62 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f  bFree(db, pWInfo
30198 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  );.  }.}.../*.**
30199 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 62 65   Generate the be
3019a 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 6c  ginning of the l
3019b 6f 6f 70 20 75 73 65 64 20 66 6f 72 20 57 48 45  oop used for WHE
3019c 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73  RE clause proces
3019d 73 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 72 65 74  sing..** The ret
3019e 75 72 6e 20 76 61 6c 75 65 20 69 73 20 61 20 70  urn value is a p
3019f 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 70 61  ointer to an opa
301a0 71 75 65 20 73 74 72 75 63 74 75 72 65 20 74 68  que structure th
301a1 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 69  at contains.** i
301a2 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65  nformation neede
301a3 64 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74  d to terminate t
301a4 68 65 20 6c 6f 6f 70 2e 20 20 4c 61 74 65 72 2c  he loop.  Later,
301a5 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75   the calling rou
301a6 74 69 6e 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 69  tine.** should i
301a7 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 57 68 65  nvoke sqlite3Whe
301a8 72 65 45 6e 64 28 29 20 77 69 74 68 20 74 68 65  reEnd() with the
301a9 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
301aa 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a   this function.*
301ab 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f  * in order to co
301ac 6d 70 6c 65 74 65 20 74 68 65 20 57 48 45 52 45  mplete the WHERE
301ad 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69   clause processi
301ae 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ng..**.** If an 
301af 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68  error occurs, th
301b0 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
301b1 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54  ns NULL..**.** T
301b2 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69 73  he basic idea is
301b3 20 74 6f 20 64 6f 20 61 20 6e 65 73 74 65 64 20   to do a nested 
301b4 6c 6f 6f 70 2c 20 6f 6e 65 20 6c 6f 6f 70 20 66  loop, one loop f
301b5 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e  or each table in
301b6 0a 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61  .** the FROM cla
301b7 75 73 65 20 6f 66 20 61 20 73 65 6c 65 63 74 2e  use of a select.
301b8 20 20 28 49 4e 53 45 52 54 20 61 6e 64 20 55 50    (INSERT and UP
301b9 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20  DATE statements 
301ba 61 72 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20  are the.** same 
301bb 61 73 20 61 20 53 45 4c 45 43 54 20 77 69 74 68  as a SELECT with
301bc 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 74   only a single t
301bd 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  able in the FROM
301be 20 63 6c 61 75 73 65 2e 29 20 20 46 6f 72 0a 2a   clause.)  For.*
301bf 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68  * example, if th
301c0 65 20 53 51 4c 20 69 73 20 74 68 69 73 3a 0a 2a  e SQL is this:.*
301c1 2a 0a 2a 2a 20 20 20 20 20 20 20 53 45 4c 45 43  *.**       SELEC
301c2 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32 2c  T * FROM t1, t2,
301c3 20 74 33 20 57 48 45 52 45 20 2e 2e 2e 3b 0a 2a   t3 WHERE ...;.*
301c4 2a 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 63 6f  *.** Then the co
301c5 64 65 20 67 65 6e 65 72 61 74 65 64 20 69 73 20  de generated is 
301c6 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 6c 69 6b  conceptually lik
301c7 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  e the following:
301c8 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 6f 72 65  .**.**      fore
301c9 61 63 68 20 72 6f 77 31 20 69 6e 20 74 31 20 64  ach row1 in t1 d
301ca 6f 20 20 20 20 20 20 20 5c 20 20 20 20 43 6f 64  o       \    Cod
301cb 65 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 20  e generated.**  
301cc 20 20 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f        foreach ro
301cd 77 32 20 69 6e 20 74 32 20 64 6f 20 20 20 20 20  w2 in t2 do     
301ce 20 7c 2d 2d 20 62 79 20 73 71 6c 69 74 65 33 57   |-- by sqlite3W
301cf 68 65 72 65 42 65 67 69 6e 28 29 0a 2a 2a 20 20  hereBegin().**  
301d0 20 20 20 20 20 20 20 20 66 6f 72 65 61 63 68 20          foreach 
301d1 72 6f 77 33 20 69 6e 20 74 33 20 64 6f 20 20 20  row3 in t3 do   
301d2 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  /.**            
301d3 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ....**          
301d4 65 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20  end             
301d5 20 20 20 20 20 20 20 20 5c 20 20 20 20 43 6f 64          \    Cod
301d6 65 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 20  e generated.**  
301d7 20 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20        end       
301d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
301d9 20 7c 2d 2d 20 62 79 20 73 71 6c 69 74 65 33 57   |-- by sqlite3W
301da 68 65 72 65 45 6e 64 28 29 0a 2a 2a 20 20 20 20  hereEnd().**    
301db 20 20 65 6e 64 20 20 20 20 20 20 20 20 20 20 20    end           
301dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0a                /.
301dd 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
301de 74 68 65 20 6c 6f 6f 70 73 20 6d 69 67 68 74 20  the loops might 
301df 6e 6f 74 20 62 65 20 6e 65 73 74 65 64 20 69 6e  not be nested in
301e0 20 74 68 65 20 6f 72 64 65 72 20 69 6e 20 77 68   the order in wh
301e1 69 63 68 20 74 68 65 79 0a 2a 2a 20 61 70 70 65  ich they.** appe
301e2 61 72 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ar in the FROM c
301e3 6c 61 75 73 65 20 69 66 20 61 20 64 69 66 66 65  lause if a diffe
301e4 72 65 6e 74 20 6f 72 64 65 72 20 69 73 20 62 65  rent order is be
301e5 74 74 65 72 20 61 62 6c 65 20 74 6f 20 6d 61 6b  tter able to mak
301e6 65 0a 2a 2a 20 75 73 65 20 6f 66 20 69 6e 64 69  e.** use of indi
301e7 63 65 73 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20  ces.  Note also 
301e8 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 49 4e  that when the IN
301e9 20 6f 70 65 72 61 74 6f 72 20 61 70 70 65 61 72   operator appear
301ea 73 20 69 6e 0a 2a 2a 20 74 68 65 20 57 48 45 52  s in.** the WHER
301eb 45 20 63 6c 61 75 73 65 2c 20 69 74 20 6d 69 67  E clause, it mig
301ec 68 74 20 72 65 73 75 6c 74 20 69 6e 20 61 64 64  ht result in add
301ed 69 74 69 6f 6e 61 6c 20 6e 65 73 74 65 64 20 6c  itional nested l
301ee 6f 6f 70 73 20 66 6f 72 0a 2a 2a 20 73 63 61 6e  oops for.** scan
301ef 6e 69 6e 67 20 74 68 72 6f 75 67 68 20 61 6c 6c  ning through all
301f0 20 76 61 6c 75 65 73 20 6f 6e 20 74 68 65 20 72   values on the r
301f1 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  ight-hand side o
301f2 66 20 74 68 65 20 49 4e 2e 0a 2a 2a 0a 2a 2a 20  f the IN..**.** 
301f3 54 68 65 72 65 20 61 72 65 20 42 74 72 65 65 20  There are Btree 
301f4 63 75 72 73 6f 72 73 20 61 73 73 6f 63 69 61 74  cursors associat
301f5 65 64 20 77 69 74 68 20 65 61 63 68 20 74 61 62  ed with each tab
301f6 6c 65 2e 20 20 74 31 20 75 73 65 73 20 63 75 72  le.  t1 uses cur
301f7 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 70 54  sor.** number pT
301f8 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75  abList->a[0].iCu
301f9 72 73 6f 72 2e 20 20 74 32 20 75 73 65 73 20 74  rsor.  t2 uses t
301fa 68 65 20 63 75 72 73 6f 72 20 70 54 61 62 4c 69  he cursor pTabLi
301fb 73 74 2d 3e 61 5b 31 5d 2e 69 43 75 72 73 6f 72  st->a[1].iCursor
301fc 2e 0a 2a 2a 20 41 6e 64 20 73 6f 20 66 6f 72 74  ..** And so fort
301fd 68 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  h.  This routine
301fe 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
301ff 74 6f 20 6f 70 65 6e 20 74 68 6f 73 65 20 56 44  to open those VD
30200 42 45 20 63 75 72 73 6f 72 73 0a 2a 2a 20 61 6e  BE cursors.** an
30201 64 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e  d sqlite3WhereEn
30202 64 28 29 20 67 65 6e 65 72 61 74 65 73 20 74 68  d() generates th
30203 65 20 63 6f 64 65 20 74 6f 20 63 6c 6f 73 65 20  e code to close 
30204 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  them..**.** The 
30205 63 6f 64 65 20 74 68 61 74 20 73 71 6c 69 74 65  code that sqlite
30206 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 67 65  3WhereBegin() ge
30207 6e 65 72 61 74 65 73 20 6c 65 61 76 65 73 20 74  nerates leaves t
30208 68 65 20 63 75 72 73 6f 72 73 20 6e 61 6d 65 64  he cursors named
30209 0a 2a 2a 20 69 6e 20 70 54 61 62 4c 69 73 74 20  .** in pTabList 
3020a 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 69  pointing at thei
3020b 72 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 6e  r appropriate en
3020c 74 72 69 65 73 2e 20 20 54 68 65 20 5b 2e 2e 2e  tries.  The [...
3020d 5d 20 63 6f 64 65 0a 2a 2a 20 63 61 6e 20 75 73  ] code.** can us
3020e 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61 6e 64 20  e OP_Column and 
3020f 4f 50 5f 52 6f 77 69 64 20 6f 70 63 6f 64 65 73  OP_Rowid opcodes
30210 20 6f 6e 20 74 68 65 73 65 20 63 75 72 73 6f 72   on these cursor
30211 73 20 74 6f 20 65 78 74 72 61 63 74 0a 2a 2a 20  s to extract.** 
30212 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 76 61  data from the va
30213 72 69 6f 75 73 20 74 61 62 6c 65 73 20 6f 66 20  rious tables of 
30214 74 68 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20  the loop..**.** 
30215 49 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  If the WHERE cla
30216 75 73 65 20 69 73 20 65 6d 70 74 79 2c 20 74 68  use is empty, th
30217 65 20 66 6f 72 65 61 63 68 20 6c 6f 6f 70 73 20  e foreach loops 
30218 6d 75 73 74 20 65 61 63 68 20 73 63 61 6e 20 74  must each scan t
30219 68 65 69 72 0a 2a 2a 20 65 6e 74 69 72 65 20 74  heir.** entire t
3021a 61 62 6c 65 73 2e 20 20 54 68 75 73 20 61 20 74  ables.  Thus a t
3021b 68 72 65 65 2d 77 61 79 20 6a 6f 69 6e 20 69 73  hree-way join is
3021c 20 61 6e 20 4f 28 4e 5e 33 29 20 6f 70 65 72 61   an O(N^3) opera
3021d 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 0a 2a 2a  tion.  But if.**
3021e 20 74 68 65 20 74 61 62 6c 65 73 20 68 61 76 65   the tables have
3021f 20 69 6e 64 69 63 65 73 20 61 6e 64 20 74 68 65   indices and the
30220 72 65 20 61 72 65 20 74 65 72 6d 73 20 69 6e 20  re are terms in 
30221 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
30222 20 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 20 74   that.** refer t
30223 6f 20 74 68 6f 73 65 20 69 6e 64 69 63 65 73 2c  o those indices,
30224 20 61 20 63 6f 6d 70 6c 65 74 65 20 74 61 62 6c   a complete tabl
30225 65 20 73 63 61 6e 20 63 61 6e 20 62 65 20 61 76  e scan can be av
30226 6f 69 64 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a  oided and the.**
30227 20 63 6f 64 65 20 77 69 6c 6c 20 72 75 6e 20 6d   code will run m
30228 75 63 68 20 66 61 73 74 65 72 2e 20 20 4d 6f 73  uch faster.  Mos
30229 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 20 6f 66  t of the work of
3022a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
3022b 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 74 6f 20   checking.** to 
3022c 73 65 65 20 69 66 20 74 68 65 72 65 20 61 72 65  see if there are
3022d 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 63 61   indices that ca
3022e 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 70 65  n be used to spe
3022f 65 64 20 75 70 20 74 68 65 20 6c 6f 6f 70 2e 0a  ed up the loop..
30230 2a 2a 0a 2a 2a 20 54 65 72 6d 73 20 6f 66 20 74  **.** Terms of t
30231 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
30232 61 72 65 20 61 6c 73 6f 20 75 73 65 64 20 74 6f  are also used to
30233 20 6c 69 6d 69 74 20 77 68 69 63 68 20 72 6f 77   limit which row
30234 73 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 6d 61  s actually.** ma
30235 6b 65 20 69 74 20 74 6f 20 74 68 65 20 22 2e 2e  ke it to the "..
30236 2e 22 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  ." in the middle
30237 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 41   of the loop.  A
30238 66 74 65 72 20 65 61 63 68 20 22 66 6f 72 65 61  fter each "forea
30239 63 68 22 2c 0a 2a 2a 20 74 65 72 6d 73 20 6f 66  ch",.** terms of
3023a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
3023b 65 20 74 68 61 74 20 75 73 65 20 6f 6e 6c 79 20  e that use only 
3023c 74 65 72 6d 73 20 69 6e 20 74 68 61 74 20 6c 6f  terms in that lo
3023d 6f 70 20 61 6e 64 20 6f 75 74 65 72 0a 2a 2a 20  op and outer.** 
3023e 6c 6f 6f 70 73 20 61 72 65 20 65 76 61 6c 75 61  loops are evalua
3023f 74 65 64 20 61 6e 64 20 69 66 20 66 61 6c 73 65  ted and if false
30240 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 20   a jump is made 
30241 61 72 6f 75 6e 64 20 61 6c 6c 20 73 75 62 73 65  around all subse
30242 71 75 65 6e 74 0a 2a 2a 20 69 6e 6e 65 72 20 6c  quent.** inner l
30243 6f 6f 70 73 20 28 6f 72 20 61 72 6f 75 6e 64 20  oops (or around 
30244 74 68 65 20 22 2e 2e 2e 22 20 69 66 20 74 68 65  the "..." if the
30245 20 74 65 73 74 20 6f 63 63 75 72 73 20 77 69 74   test occurs wit
30246 68 69 6e 20 74 68 65 20 69 6e 6e 65 72 2d 0a 2a  hin the inner-.*
30247 2a 20 6d 6f 73 74 20 6c 6f 6f 70 29 0a 2a 2a 0a  * most loop).**.
30248 2a 2a 20 4f 55 54 45 52 20 4a 4f 49 4e 53 0a 2a  ** OUTER JOINS.*
30249 2a 0a 2a 2a 20 41 6e 20 6f 75 74 65 72 20 6a 6f  *.** An outer jo
3024a 69 6e 20 6f 66 20 74 61 62 6c 65 73 20 74 31 20  in of tables t1 
3024b 61 6e 64 20 74 32 20 69 73 20 63 6f 6e 63 65 70  and t2 is concep
3024c 74 61 6c 6c 79 20 63 6f 64 65 64 20 61 73 20 66  tally coded as f
3024d 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
3024e 20 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e   foreach row1 in
3024f 20 74 31 20 64 6f 0a 2a 2a 20 20 20 20 20 20 66   t1 do.**      f
30250 6c 61 67 20 3d 20 30 0a 2a 2a 20 20 20 20 20 20  lag = 0.**      
30251 66 6f 72 65 61 63 68 20 72 6f 77 32 20 69 6e 20  foreach row2 in 
30252 74 32 20 64 6f 0a 2a 2a 20 20 20 20 20 20 20 20  t2 do.**        
30253 73 74 61 72 74 3a 0a 2a 2a 20 20 20 20 20 20 20  start:.**       
30254 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20 20     ....**       
30255 20 20 20 66 6c 61 67 20 3d 20 31 0a 2a 2a 20 20     flag = 1.**  
30256 20 20 20 20 65 6e 64 0a 2a 2a 20 20 20 20 20 20      end.**      
30257 69 66 20 66 6c 61 67 3d 3d 30 20 74 68 65 6e 0a  if flag==0 then.
30258 2a 2a 20 20 20 20 20 20 20 20 6d 6f 76 65 20 74  **        move t
30259 68 65 20 72 6f 77 32 20 63 75 72 73 6f 72 20 74  he row2 cursor t
3025a 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 0a 2a 2a 20  o a null row.** 
3025b 20 20 20 20 20 20 20 67 6f 74 6f 20 73 74 61 72         goto star
3025c 74 0a 2a 2a 20 20 20 20 20 20 66 69 0a 2a 2a 20  t.**      fi.** 
3025d 20 20 20 65 6e 64 0a 2a 2a 0a 2a 2a 20 4f 52 44     end.**.** ORD
3025e 45 52 20 42 59 20 43 4c 41 55 53 45 20 50 52 4f  ER BY CLAUSE PRO
3025f 43 45 53 53 49 4e 47 0a 2a 2a 0a 2a 2a 20 2a 70  CESSING.**.** *p
30260 70 4f 72 64 65 72 42 79 20 69 73 20 61 20 70 6f  pOrderBy is a po
30261 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4f 52 44  inter to the ORD
30262 45 52 20 42 59 20 63 6c 61 75 73 65 20 6f 66 20  ER BY clause of 
30263 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
30264 6e 74 2c 0a 2a 2a 20 69 66 20 74 68 65 72 65 20  nt,.** if there 
30265 69 73 20 6f 6e 65 2e 20 20 49 66 20 74 68 65 72  is one.  If ther
30266 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59  e is no ORDER BY
30267 20 63 6c 61 75 73 65 20 6f 72 20 69 66 20 74 68   clause or if th
30268 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73  is routine.** is
30269 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 6e 20   called from an 
3026a 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
3026b 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e   statement, then
3026c 20 70 70 4f 72 64 65 72 42 79 20 69 73 20 4e 55   ppOrderBy is NU
3026d 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  LL..**.** If an 
3026e 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65  index can be use
3026f 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 61  d so that the na
30270 74 75 72 61 6c 20 6f 75 74 70 75 74 20 6f 72 64  tural output ord
30271 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a  er of the table.
30272 2a 2a 20 73 63 61 6e 20 69 73 20 63 6f 72 72 65  ** scan is corre
30273 63 74 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52  ct for the ORDER
30274 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e   BY clause, then
30275 20 74 68 61 74 20 69 6e 64 65 78 20 69 73 20 75   that index is u
30276 73 65 64 20 61 6e 64 0a 2a 2a 20 2a 70 70 4f 72  sed and.** *ppOr
30277 64 65 72 42 79 20 69 73 20 73 65 74 20 74 6f 20  derBy is set to 
30278 4e 55 4c 4c 2e 20 20 54 68 69 73 20 69 73 20 61  NULL.  This is a
30279 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 74  n optimization t
3027a 68 61 74 20 70 72 65 76 65 6e 74 73 20 61 6e 0a  hat prevents an.
3027b 2a 2a 20 75 6e 6e 65 63 65 73 73 61 72 79 20 73  ** unnecessary s
3027c 6f 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c  ort of the resul
3027d 74 20 73 65 74 20 69 66 20 61 6e 20 69 6e 64 65  t set if an inde
3027e 78 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  x appropriate fo
3027f 72 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42  r the.** ORDER B
30280 59 20 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79  Y clause already
30281 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 49   exists..**.** I
30282 66 20 74 68 65 20 77 68 65 72 65 20 63 6c 61 75  f the where clau
30283 73 65 20 6c 6f 6f 70 73 20 63 61 6e 6e 6f 74 20  se loops cannot 
30284 62 65 20 61 72 72 61 6e 67 65 64 20 74 6f 20 70  be arranged to p
30285 72 6f 76 69 64 65 20 74 68 65 20 63 6f 72 72 65  rovide the corre
30286 63 74 0a 2a 2a 20 6f 75 74 70 75 74 20 6f 72 64  ct.** output ord
30287 65 72 2c 20 74 68 65 6e 20 74 68 65 20 2a 70 70  er, then the *pp
30288 4f 72 64 65 72 42 79 20 69 73 20 75 6e 63 68 61  OrderBy is uncha
30289 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nged..*/.SQLITE_
3028a 50 52 49 56 41 54 45 20 57 68 65 72 65 49 6e 66  PRIVATE WhereInf
3028b 6f 20 2a 73 71 6c 69 74 65 33 57 68 65 72 65 42  o *sqlite3WhereB
3028c 65 67 69 6e 28 0a 20 20 50 61 72 73 65 20 2a 70  egin(.  Parse *p
3028d 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
3028e 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
3028f 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ext */.  SrcList
30290 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 2f   *pTabList,    /
30291 2a 20 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  * A list of all 
30292 74 61 62 6c 65 73 20 74 6f 20 62 65 20 73 63 61  tables to be sca
30293 6e 6e 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a  nned */.  Expr *
30294 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20  pWhere,         
30295 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
30296 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
30297 74 20 2a 2a 70 70 4f 72 64 65 72 42 79 2c 20 2f  t **ppOrderBy, /
30298 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 20 63 6c  * An ORDER BY cl
30299 61 75 73 65 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f  ause, or NULL */
3029a 0a 20 20 75 31 36 20 77 63 74 72 6c 46 6c 61 67  .  u16 wctrlFlag
3029b 73 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20  s        /* One 
3029c 6f 66 20 74 68 65 20 57 48 45 52 45 5f 2a 20 66  of the WHERE_* f
3029d 6c 61 67 73 20 64 65 66 69 6e 65 64 20 69 6e 20  lags defined in 
3029e 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2f 0a 29  sqliteInt.h */.)
3029f 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
302a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
302a1 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
302a2 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 57 49 6e  /.  int nByteWIn
302a3 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  fo;            /
302a4 2a 20 4e 75 6d 2e 20 62 79 74 65 73 20 61 6c 6c  * Num. bytes all
302a5 6f 63 61 74 65 64 20 66 6f 72 20 57 68 65 72 65  ocated for Where
302a6 49 6e 66 6f 20 73 74 72 75 63 74 20 2a 2f 0a 20  Info struct */. 
302a7 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e   WhereInfo *pWIn
302a8 66 6f 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 57  fo;         /* W
302a9 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 72  ill become the r
302aa 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74  eturn value of t
302ab 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  his function */.
302ac 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
302ad 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 2f 2a 20  se->pVdbe;   /* 
302ae 54 68 65 20 76 69 72 74 75 61 6c 20 64 61 74 61  The virtual data
302af 62 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f 0a 20  base engine */. 
302b0 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
302b1 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  y;          /* C
302b2 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20  ursors that are 
302b3 6e 6f 74 20 79 65 74 20 70 6f 73 69 74 69 6f 6e  not yet position
302b4 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73  ed */.  WhereMas
302b5 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20  kSet *pMaskSet; 
302b6 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
302b7 73 69 6f 6e 20 6d 61 73 6b 20 73 65 74 20 2a 2f  sion mask set */
302b8 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
302b9 70 57 43 3b 20 20 20 20 20 20 20 20 20 20 20 20  pWC;            
302ba 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74     /* Decomposit
302bb 69 6f 6e 20 6f 66 20 74 68 65 20 57 48 45 52 45  ion of the WHERE
302bc 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72   clause */.  str
302bd 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
302be 20 2a 70 54 61 62 49 74 65 6d 3b 20 20 2f 2a 20   *pTabItem;  /* 
302bf 41 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66  A single entry f
302c0 72 6f 6d 20 70 54 61 62 4c 69 73 74 20 2a 2f 0a  rom pTabList */.
302c1 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c    WhereLevel *pL
302c2 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20  evel;           
302c3 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 6c 65    /* A single le
302c4 76 65 6c 20 69 6e 20 74 68 65 20 70 57 49 6e 66  vel in the pWInf
302c5 6f 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  o list */.  int 
302c6 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20  iFrom;          
302c7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
302c8 69 72 73 74 20 75 6e 75 73 65 64 20 46 52 4f 4d  irst unused FROM
302c9 20 63 6c 61 75 73 65 20 65 6c 65 6d 65 6e 74 20   clause element 
302ca 2a 2f 0a 20 20 69 6e 74 20 61 6e 64 46 6c 61 67  */.  int andFlag
302cb 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
302cc 2f 2a 20 41 4e 44 2d 65 64 20 63 6f 6d 62 69 6e  /* AND-ed combin
302cd 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 70 57 43  ation of all pWC
302ce 2d 3e 61 5b 5d 2e 77 74 46 6c 61 67 73 20 2a 2f  ->a[].wtFlags */
302cf 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
302d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
302d1 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
302d2 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68  tion */..  /* Th
302d3 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c  e number of tabl
302d4 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
302d5 6c 61 75 73 65 20 69 73 20 6c 69 6d 69 74 65 64  lause is limited
302d6 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
302d7 66 0a 20 20 2a 2a 20 62 69 74 73 20 69 6e 20 61  f.  ** bits in a
302d8 20 42 69 74 6d 61 73 6b 20 0a 20 20 2a 2f 0a 20   Bitmask .  */. 
302d9 20 69 66 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e   if( pTabList->n
302da 53 72 63 3e 42 4d 53 20 29 7b 0a 20 20 20 20 73  Src>BMS ){.    s
302db 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
302dc 50 61 72 73 65 2c 20 22 61 74 20 6d 6f 73 74 20  Parse, "at most 
302dd 25 64 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a  %d tables in a j
302de 6f 69 6e 22 2c 20 42 4d 53 29 3b 0a 20 20 20 20  oin", BMS);.    
302df 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
302e0 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64   /* Allocate and
302e1 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
302e2 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74  WhereInfo struct
302e3 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ure that will be
302e4 63 6f 6d 65 20 74 68 65 0a 20 20 2a 2a 20 72 65  come the.  ** re
302e5 74 75 72 6e 20 76 61 6c 75 65 2e 20 41 20 73 69  turn value. A si
302e6 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  ngle allocation 
302e7 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  is used to store
302e8 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 20   the WhereInfo. 
302e9 20 2a 2a 20 73 74 72 75 63 74 2c 20 74 68 65 20   ** struct, the 
302ea 63 6f 6e 74 65 6e 74 73 20 6f 66 20 57 68 65 72  contents of Wher
302eb 65 49 6e 66 6f 2e 61 5b 5d 2c 20 74 68 65 20 57  eInfo.a[], the W
302ec 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63  hereClause struc
302ed 74 75 72 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68  ture.  ** and th
302ee 65 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 73  e WhereMaskSet s
302ef 74 72 75 63 74 75 72 65 2e 20 53 69 6e 63 65 20  tructure. Since 
302f0 57 68 65 72 65 43 6c 61 75 73 65 20 63 6f 6e 74  WhereClause cont
302f1 61 69 6e 73 20 61 6e 20 38 2d 62 79 74 65 0a 20  ains an 8-byte. 
302f2 20 2a 2a 20 66 69 65 6c 64 20 28 74 79 70 65 20   ** field (type 
302f3 42 69 74 6d 61 73 6b 29 20 69 74 20 6d 75 73 74  Bitmask) it must
302f4 20 62 65 20 61 6c 69 67 6e 65 64 20 6f 6e 20 61   be aligned on a
302f5 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72  n 8-byte boundar
302f6 79 20 6f 6e 0a 20 20 2a 2a 20 73 6f 6d 65 20 61  y on.  ** some a
302f7 72 63 68 69 74 65 63 74 75 72 65 73 2e 20 48 65  rchitectures. He
302f8 6e 63 65 20 74 68 65 20 52 4f 55 4e 44 38 28 29  nce the ROUND8()
302f9 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 64   below..  */.  d
302fa 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
302fb 20 20 6e 42 79 74 65 57 49 6e 66 6f 20 3d 20 52    nByteWInfo = R
302fc 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 57 68 65  OUND8(sizeof(Whe
302fd 72 65 49 6e 66 6f 29 2b 28 70 54 61 62 4c 69 73  reInfo)+(pTabLis
302fe 74 2d 3e 6e 53 72 63 2d 31 29 2a 73 69 7a 65 6f  t->nSrc-1)*sizeo
302ff 66 28 57 68 65 72 65 4c 65 76 65 6c 29 29 3b 0a  f(WhereLevel));.
30300 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74    pWInfo = sqlit
30301 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
30302 62 2c 20 0a 20 20 20 20 20 20 6e 42 79 74 65 57  b, .      nByteW
30303 49 6e 66 6f 20 2b 20 0a 20 20 20 20 20 20 73 69  Info + .      si
30304 7a 65 6f 66 28 57 68 65 72 65 43 6c 61 75 73 65  zeof(WhereClause
30305 29 20 2b 0a 20 20 20 20 20 20 73 69 7a 65 6f 66  ) +.      sizeof
30306 28 57 68 65 72 65 4d 61 73 6b 53 65 74 29 0a 20  (WhereMaskSet). 
30307 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   );.  if( db->ma
30308 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
30309 20 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69    goto whereBegi
3030a 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 57  nError;.  }.  pW
3030b 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 20 3d 20 70  Info->nLevel = p
3030c 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 0a 20  TabList->nSrc;. 
3030d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 20   pWInfo->pParse 
3030e 3d 20 70 50 61 72 73 65 3b 0a 20 20 70 57 49 6e  = pParse;.  pWIn
3030f 66 6f 2d 3e 70 54 61 62 4c 69 73 74 20 3d 20 70  fo->pTabList = p
30310 54 61 62 4c 69 73 74 3b 0a 20 20 70 57 49 6e 66  TabList;.  pWInf
30311 6f 2d 3e 69 42 72 65 61 6b 20 3d 20 73 71 6c 69  o->iBreak = sqli
30312 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
30313 28 76 29 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70  (v);.  pWInfo->p
30314 57 43 20 3d 20 70 57 43 20 3d 20 28 57 68 65 72  WC = pWC = (Wher
30315 65 43 6c 61 75 73 65 20 2a 29 26 28 28 75 38 20  eClause *)&((u8 
30316 2a 29 70 57 49 6e 66 6f 29 5b 6e 42 79 74 65 57  *)pWInfo)[nByteW
30317 49 6e 66 6f 5d 3b 0a 20 20 70 57 49 6e 66 6f 2d  Info];.  pWInfo-
30318 3e 77 63 74 72 6c 46 6c 61 67 73 20 3d 20 77 63  >wctrlFlags = wc
30319 74 72 6c 46 6c 61 67 73 3b 0a 20 20 70 4d 61 73  trlFlags;.  pMas
3031a 6b 53 65 74 20 3d 20 28 57 68 65 72 65 4d 61 73  kSet = (WhereMas
3031b 6b 53 65 74 2a 29 26 70 57 43 5b 31 5d 3b 0a 0a  kSet*)&pWC[1];..
3031c 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20 57    /* Split the W
3031d 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e 74 6f  HERE clause into
3031e 20 73 65 70 61 72 61 74 65 20 73 75 62 65 78 70   separate subexp
3031f 72 65 73 73 69 6f 6e 73 20 77 68 65 72 65 20 65  ressions where e
30320 61 63 68 0a 20 20 2a 2a 20 73 75 62 65 78 70 72  ach.  ** subexpr
30321 65 73 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61  ession is separa
30322 74 65 64 20 62 79 20 61 6e 20 41 4e 44 20 6f 70  ted by an AND op
30323 65 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69  erator..  */.  i
30324 6e 69 74 4d 61 73 6b 53 65 74 28 70 4d 61 73 6b  nitMaskSet(pMask
30325 53 65 74 29 3b 0a 20 20 77 68 65 72 65 43 6c 61  Set);.  whereCla
30326 75 73 65 49 6e 69 74 28 70 57 43 2c 20 70 50 61  useInit(pWC, pPa
30327 72 73 65 2c 20 70 4d 61 73 6b 53 65 74 29 3b 0a  rse, pMaskSet);.
30328 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
30329 65 43 6f 6e 73 74 61 6e 74 73 28 70 50 61 72 73  eConstants(pPars
3032a 65 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 77 68  e, pWhere);.  wh
3032b 65 72 65 53 70 6c 69 74 28 70 57 43 2c 20 70 57  ereSplit(pWC, pW
3032c 68 65 72 65 2c 20 54 4b 5f 41 4e 44 29 3b 0a 20  here, TK_AND);. 
3032d 20 20 20 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c     .  /* Special
3032e 20 63 61 73 65 3a 20 61 20 57 48 45 52 45 20 63   case: a WHERE c
3032f 6c 61 75 73 65 20 74 68 61 74 20 69 73 20 63 6f  lause that is co
30330 6e 73 74 61 6e 74 2e 20 20 45 76 61 6c 75 61 74  nstant.  Evaluat
30331 65 20 74 68 65 0a 20 20 2a 2a 20 65 78 70 72 65  e the.  ** expre
30332 73 73 69 6f 6e 20 61 6e 64 20 65 69 74 68 65 72  ssion and either
30333 20 6a 75 6d 70 20 6f 76 65 72 20 61 6c 6c 20 6f   jump over all o
30334 66 20 74 68 65 20 63 6f 64 65 20 6f 72 20 66 61  f the code or fa
30335 6c 6c 20 74 68 72 75 2e 0a 20 20 2a 2f 0a 20 20  ll thru..  */.  
30336 69 66 28 20 70 57 68 65 72 65 20 26 26 20 28 70  if( pWhere && (p
30337 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 30  TabList->nSrc==0
30338 20 7c 7c 20 73 71 6c 69 74 65 33 45 78 70 72 49   || sqlite3ExprI
30339 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e  sConstantNotJoin
3033a 28 70 57 68 65 72 65 29 29 20 29 7b 0a 20 20 20  (pWhere)) ){.   
3033b 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
3033c 6c 73 65 28 70 50 61 72 73 65 2c 20 70 57 68 65  lse(pParse, pWhe
3033d 72 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65  re, pWInfo->iBre
3033e 61 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  ak, SQLITE_JUMPI
3033f 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 70 57 68 65  FNULL);.    pWhe
30340 72 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  re = 0;.  }..  /
30341 2a 20 41 73 73 69 67 6e 20 61 20 62 69 74 20 66  * Assign a bit f
30342 72 6f 6d 20 74 68 65 20 62 69 74 6d 61 73 6b 20  rom the bitmask 
30343 74 6f 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e  to every term in
30344 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
30345 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e  ..  **.  ** When
30346 20 61 73 73 69 67 6e 69 6e 67 20 62 69 74 6d 61   assigning bitma
30347 73 6b 20 76 61 6c 75 65 73 20 74 6f 20 46 52 4f  sk values to FRO
30348 4d 20 63 6c 61 75 73 65 20 63 75 72 73 6f 72 73  M clause cursors
30349 2c 20 69 74 20 6d 75 73 74 20 62 65 0a 20 20 2a  , it must be.  *
3034a 2a 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20  * the case that 
3034b 69 66 20 58 20 69 73 20 74 68 65 20 62 69 74 6d  if X is the bitm
3034c 61 73 6b 20 66 6f 72 20 74 68 65 20 4e 2d 74 68  ask for the N-th
3034d 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
3034e 6d 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20  m then.  ** the 
3034f 62 69 74 6d 61 73 6b 20 66 6f 72 20 61 6c 6c 20  bitmask for all 
30350 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
30351 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66  s to the left of
30352 20 74 68 65 20 4e 2d 74 68 20 74 65 72 6d 0a 20   the N-th term. 
30353 20 2a 2a 20 69 73 20 28 58 2d 31 29 2e 20 20 20   ** is (X-1).   
30354 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 66 72  An expression fr
30355 6f 6d 20 74 68 65 20 4f 4e 20 63 6c 61 75 73 65  om the ON clause
30356 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20   of a LEFT JOIN 
30357 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 69 74 73  can use.  ** its
30358 20 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e   Expr.iRightJoin
30359 54 61 62 6c 65 20 76 61 6c 75 65 20 74 6f 20 66  Table value to f
3035a 69 6e 64 20 74 68 65 20 62 69 74 6d 61 73 6b 20  ind the bitmask 
3035b 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61 62  of the right tab
3035c 6c 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6a  le.  ** of the j
3035d 6f 69 6e 2e 20 20 53 75 62 74 72 61 63 74 69 6e  oin.  Subtractin
3035e 67 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65 20 72  g one from the r
3035f 69 67 68 74 20 74 61 62 6c 65 20 62 69 74 6d 61  ight table bitma
30360 73 6b 20 67 69 76 65 73 20 61 0a 20 20 2a 2a 20  sk gives a.  ** 
30361 62 69 74 6d 61 73 6b 20 66 6f 72 20 61 6c 6c 20  bitmask for all 
30362 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c 65  tables to the le
30363 66 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20  ft of the join. 
30364 20 4b 6e 6f 77 69 6e 67 20 74 68 65 20 62 69 74   Knowing the bit
30365 6d 61 73 6b 0a 20 20 2a 2a 20 66 6f 72 20 61 6c  mask.  ** for al
30366 6c 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20  l tables to the 
30367 6c 65 66 74 20 6f 66 20 61 20 6c 65 66 74 20 6a  left of a left j
30368 6f 69 6e 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  oin is important
30369 2e 20 20 54 69 63 6b 65 74 20 23 33 30 31 35 2e  .  Ticket #3015.
3036a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f 6e 66 69  .  **.  ** Confi
3036b 67 75 72 65 20 74 68 65 20 57 68 65 72 65 43 6c  gure the WhereCl
3036c 61 75 73 65 2e 76 6d 61 73 6b 20 76 61 72 69 61  ause.vmask varia
3036d 62 6c 65 20 73 6f 20 74 68 61 74 20 62 69 74 73  ble so that bits
3036e 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64   that correspond
3036f 0a 20 20 2a 2a 20 74 6f 20 76 69 72 74 75 61 6c  .  ** to virtual
30370 20 74 61 62 6c 65 20 63 75 72 73 6f 72 73 20 61   table cursors a
30371 72 65 20 73 65 74 2e 20 54 68 69 73 20 69 73 20  re set. This is 
30372 75 73 65 64 20 74 6f 20 73 65 6c 65 63 74 69 76  used to selectiv
30373 65 6c 79 20 64 69 73 61 62 6c 65 20 0a 20 20 2a  ely disable .  *
30374 2a 20 74 68 65 20 4f 52 2d 74 6f 2d 49 4e 20 74  * the OR-to-IN t
30375 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  ransformation in
30376 20 65 78 70 72 41 6e 61 6c 79 7a 65 4f 72 54 65   exprAnalyzeOrTe
30377 72 6d 28 29 2e 20 49 74 20 69 73 20 6e 6f 74 20  rm(). It is not 
30378 68 65 6c 70 66 75 6c 20 0a 20 20 2a 2a 20 77 69  helpful .  ** wi
30379 74 68 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  th virtual table
3037a 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  s..  */.  assert
3037b 28 20 70 57 43 2d 3e 76 6d 61 73 6b 3d 3d 30 20  ( pWC->vmask==0 
3037c 26 26 20 70 4d 61 73 6b 53 65 74 2d 3e 6e 3d 3d  && pMaskSet->n==
3037d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  0 );.  for(i=0; 
3037e 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
3037f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 72 65 61  ; i++){.    crea
30380 74 65 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  teMask(pMaskSet,
30381 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e   pTabList->a[i].
30382 69 43 75 72 73 6f 72 29 3b 0a 23 69 66 6e 64 65  iCursor);.#ifnde
30383 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
30384 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69  RTUALTABLE.    i
30385 66 28 20 41 4c 57 41 59 53 28 70 54 61 62 4c 69  f( ALWAYS(pTabLi
30386 73 74 2d 3e 61 5b 69 5d 2e 70 54 61 62 29 20 26  st->a[i].pTab) &
30387 26 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  & IsVirtual(pTab
30388 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 54 61 62 29  List->a[i].pTab)
30389 20 29 7b 0a 20 20 20 20 20 20 70 57 43 2d 3e 76   ){.      pWC->v
3038a 6d 61 73 6b 20 7c 3d 20 28 28 42 69 74 6d 61 73  mask |= ((Bitmas
3038b 6b 29 31 20 3c 3c 20 69 29 3b 0a 20 20 20 20 7d  k)1 << i);.    }
3038c 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 69 66 6e  .#endif.  }.#ifn
3038d 64 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20  def NDEBUG.  {. 
3038e 20 20 20 42 69 74 6d 61 73 6b 20 74 6f 54 68 65     Bitmask toThe
3038f 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f  Left = 0;.    fo
30390 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73  r(i=0; i<pTabLis
30391 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  t->nSrc; i++){. 
30392 20 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 20 3d       Bitmask m =
30393 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65   getMask(pMaskSe
30394 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  t, pTabList->a[i
30395 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ].iCursor);.    
30396 20 20 61 73 73 65 72 74 28 20 28 6d 2d 31 29 3d    assert( (m-1)=
30397 3d 74 6f 54 68 65 4c 65 66 74 20 29 3b 0a 20 20  =toTheLeft );.  
30398 20 20 20 20 74 6f 54 68 65 4c 65 66 74 20 7c 3d      toTheLeft |=
30399 20 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65   m;.    }.  }.#e
3039a 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e 61 6c 79  ndif..  /* Analy
3039b 7a 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 73 75  ze all of the su
3039c 62 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 4e  bexpressions.  N
3039d 6f 74 65 20 74 68 61 74 20 65 78 70 72 41 6e 61  ote that exprAna
3039e 6c 79 7a 65 28 29 20 6d 69 67 68 74 0a 20 20 2a  lyze() might.  *
3039f 2a 20 61 64 64 20 6e 65 77 20 76 69 72 74 75 61  * add new virtua
303a0 6c 20 74 65 72 6d 73 20 6f 6e 74 6f 20 74 68 65  l terms onto the
303a1 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45 52   end of the WHER
303a2 45 20 63 6c 61 75 73 65 2e 20 20 57 65 20 64 6f  E clause.  We do
303a3 20 6e 6f 74 0a 20 20 2a 2a 20 77 61 6e 74 20 74   not.  ** want t
303a4 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 73 65 20  o analyze these 
303a5 76 69 72 74 75 61 6c 20 74 65 72 6d 73 2c 20 73  virtual terms, s
303a6 6f 20 73 74 61 72 74 20 61 6e 61 6c 79 7a 69 6e  o start analyzin
303a7 67 20 61 74 20 74 68 65 20 65 6e 64 0a 20 20 2a  g at the end.  *
303a8 2a 20 61 6e 64 20 77 6f 72 6b 20 66 6f 72 77 61  * and work forwa
303a9 72 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 61  rd so that the a
303aa 64 64 65 64 20 76 69 72 74 75 61 6c 20 74 65 72  dded virtual ter
303ab 6d 73 20 61 72 65 20 6e 65 76 65 72 20 70 72 6f  ms are never pro
303ac 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 65  cessed..  */.  e
303ad 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 54  xprAnalyzeAll(pT
303ae 61 62 4c 69 73 74 2c 20 70 57 43 29 3b 0a 20 20  abList, pWC);.  
303af 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
303b0 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
303b1 20 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72   whereBeginError
303b2 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 6f 73  ;.  }..  /* Chos
303b3 65 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78  e the best index
303b4 20 74 6f 20 75 73 65 20 66 6f 72 20 65 61 63 68   to use for each
303b5 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52   table in the FR
303b6 4f 4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a  OM clause..  **.
303b7 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 66    ** This loop f
303b8 69 6c 6c 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c  ills in the foll
303b9 6f 77 69 6e 67 20 66 69 65 6c 64 73 3a 0a 20 20  owing fields:.  
303ba 2a 2a 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f  **.  **   pWInfo
303bb 2d 3e 61 5b 5d 2e 70 49 64 78 20 20 20 20 20 20  ->a[].pIdx      
303bc 54 68 65 20 69 6e 64 65 78 20 74 6f 20 75 73 65  The index to use
303bd 20 66 6f 72 20 74 68 69 73 20 6c 65 76 65 6c 20   for this level 
303be 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 2a  of the loop..  *
303bf 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e  *   pWInfo->a[].
303c0 77 73 46 6c 61 67 73 20 20 20 57 48 45 52 45 5f  wsFlags   WHERE_
303c1 78 78 78 20 66 6c 61 67 73 20 61 73 73 6f 63 69  xxx flags associ
303c2 61 74 65 64 20 77 69 74 68 20 70 49 64 78 0a 20  ated with pIdx. 
303c3 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b   **   pWInfo->a[
303c4 5d 2e 6e 45 71 20 20 20 20 20 20 20 54 68 65 20  ].nEq       The 
303c5 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 61 6e 64  number of == and
303c6 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a   IN constraints.
303c7 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61    **   pWInfo->a
303c8 5b 5d 2e 69 46 72 6f 6d 20 20 20 20 20 57 68 69  [].iFrom     Whi
303c9 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 46  ch term of the F
303ca 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 62 65  ROM clause is be
303cb 69 6e 67 20 63 6f 64 65 64 0a 20 20 2a 2a 20 20  ing coded.  **  
303cc 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 54 61   pWInfo->a[].iTa
303cd 62 43 75 72 20 20 20 54 68 65 20 56 44 42 45 20  bCur   The VDBE 
303ce 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 64  cursor for the d
303cf 61 74 61 62 61 73 65 20 74 61 62 6c 65 0a 20 20  atabase table.  
303d0 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d  **   pWInfo->a[]
303d1 2e 69 49 64 78 43 75 72 20 20 20 54 68 65 20 56  .iIdxCur   The V
303d2 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74  DBE cursor for t
303d3 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 20 20  he index.  **   
303d4 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 54 65 72  pWInfo->a[].pTer
303d5 6d 20 20 20 20 20 57 68 65 6e 20 77 73 46 6c 61  m     When wsFla
303d6 67 73 3d 3d 57 4f 5f 4f 52 2c 20 74 68 65 20 4f  gs==WO_OR, the O
303d7 52 2d 63 6c 61 75 73 65 20 74 65 72 6d 0a 20 20  R-clause term.  
303d8 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f  **.  ** This loo
303d9 70 20 61 6c 73 6f 20 66 69 67 75 72 65 73 20 6f  p also figures o
303da 75 74 20 74 68 65 20 6e 65 73 74 69 6e 67 20 6f  ut the nesting o
303db 72 64 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69  rder of tables i
303dc 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20 2a 2a 20  n the FROM.  ** 
303dd 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6e  clause..  */.  n
303de 6f 74 52 65 61 64 79 20 3d 20 7e 28 42 69 74 6d  otReady = ~(Bitm
303df 61 73 6b 29 30 3b 0a 20 20 70 54 61 62 49 74 65  ask)0;.  pTabIte
303e0 6d 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 3b  m = pTabList->a;
303e1 0a 20 20 70 4c 65 76 65 6c 20 3d 20 70 57 49 6e  .  pLevel = pWIn
303e2 66 6f 2d 3e 61 3b 0a 20 20 61 6e 64 46 6c 61 67  fo->a;.  andFlag
303e3 73 20 3d 20 7e 30 3b 0a 20 20 57 48 45 52 45 54  s = ~0;.  WHERET
303e4 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d  RACE(("*** Optim
303e5 69 7a 65 72 20 53 74 61 72 74 20 2a 2a 2a 5c 6e  izer Start ***\n
303e6 22 29 29 3b 0a 20 20 66 6f 72 28 69 3d 69 46 72  "));.  for(i=iFr
303e7 6f 6d 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49  om=0, pLevel=pWI
303e8 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69  nfo->a; i<pTabLi
303e9 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70  st->nSrc; i++, p
303ea 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 57 68  Level++){.    Wh
303eb 65 72 65 43 6f 73 74 20 62 65 73 74 50 6c 61 6e  ereCost bestPlan
303ec 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73  ;         /* Mos
303ed 74 20 65 66 66 69 63 69 65 6e 74 20 70 6c 61 6e  t efficient plan
303ee 20 73 65 65 6e 20 73 6f 20 66 61 72 20 2a 2f 0a   seen so far */.
303ef 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b      Index *pIdx;
303f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303f1 2f 2a 20 49 6e 64 65 78 20 66 6f 72 20 46 52 4f  /* Index for FRO
303f2 4d 20 74 61 62 6c 65 20 61 74 20 70 54 61 62 49  M table at pTabI
303f3 74 65 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6a  tem */.    int j
303f4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
303f5 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
303f6 6f 70 69 6e 67 20 6f 76 65 72 20 46 52 4f 4d 20  oping over FROM 
303f7 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 69 6e  tables */.    in
303f8 74 20 62 65 73 74 4a 20 3d 20 2d 31 3b 20 20 20  t bestJ = -1;   
303f9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
303fa 20 76 61 6c 75 65 20 6f 66 20 6a 20 2a 2f 0a 20   value of j */. 
303fb 20 20 20 42 69 74 6d 61 73 6b 20 6d 3b 20 20 20     Bitmask m;   
303fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
303fd 2a 20 42 69 74 6d 61 73 6b 20 76 61 6c 75 65 20  * Bitmask value 
303fe 66 6f 72 20 6a 20 6f 72 20 62 65 73 74 4a 20 2a  for j or bestJ *
303ff 2f 0a 20 20 20 20 69 6e 74 20 69 73 4f 70 74 69  /.    int isOpti
30400 6d 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  mal;            
30401 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 66 6f    /* Iterator fo
30402 72 20 6f 70 74 69 6d 61 6c 2f 6e 6f 6e 2d 6f 70  r optimal/non-op
30403 74 69 6d 61 6c 20 73 65 61 72 63 68 20 2a 2f 0a  timal search */.
30404 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 62 65 73  .    memset(&bes
30405 74 50 6c 61 6e 2c 20 30 2c 20 73 69 7a 65 6f 66  tPlan, 0, sizeof
30406 28 62 65 73 74 50 6c 61 6e 29 29 3b 0a 20 20 20  (bestPlan));.   
30407 20 62 65 73 74 50 6c 61 6e 2e 72 43 6f 73 74 20   bestPlan.rCost 
30408 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  = SQLITE_BIG_DBL
30409 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74  ;..    /* Loop t
3040a 68 72 6f 75 67 68 20 74 68 65 20 72 65 6d 61 69  hrough the remai
3040b 6e 69 6e 67 20 65 6e 74 72 69 65 73 20 69 6e 20  ning entries in 
3040c 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
3040d 74 6f 20 66 69 6e 64 20 74 68 65 0a 20 20 20 20  to find the.    
3040e 2a 2a 20 6e 65 78 74 20 6e 65 73 74 65 64 20 6c  ** next nested l
3040f 6f 6f 70 2e 20 54 68 65 20 46 52 4f 4d 20 63 6c  oop. The FROM cl
30410 61 75 73 65 20 65 6e 74 72 69 65 73 20 6d 61 79  ause entries may
30411 20 62 65 20 69 74 65 72 61 74 65 64 20 74 68 72   be iterated thr
30412 6f 75 67 68 0a 20 20 20 20 2a 2a 20 65 69 74 68  ough.    ** eith
30413 65 72 20 6f 6e 63 65 20 6f 72 20 74 77 69 63 65  er once or twice
30414 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  . .    **.    **
30415 20 54 68 65 20 66 69 72 73 74 20 69 74 65 72 61   The first itera
30416 74 69 6f 6e 2c 20 77 68 69 63 68 20 69 73 20 61  tion, which is a
30417 6c 77 61 79 73 20 70 65 72 66 6f 72 6d 65 64 2c  lways performed,
30418 20 73 65 61 72 63 68 65 73 20 66 6f 72 20 74 68   searches for th
30419 65 0a 20 20 20 20 2a 2a 20 46 52 4f 4d 20 63 6c  e.    ** FROM cl
3041a 61 75 73 65 20 65 6e 74 72 79 20 74 68 61 74 20  ause entry that 
3041b 70 65 72 6d 69 74 73 20 74 68 65 20 6c 6f 77 65  permits the lowe
3041c 73 74 2d 63 6f 73 74 2c 20 22 6f 70 74 69 6d 61  st-cost, "optima
3041d 6c 22 20 73 63 61 6e 2e 20 49 6e 0a 20 20 20 20  l" scan. In.    
3041e 2a 2a 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20  ** this context 
3041f 61 6e 20 6f 70 74 69 6d 61 6c 20 73 63 61 6e 20  an optimal scan 
30420 69 73 20 6f 6e 65 20 74 68 61 74 20 75 73 65 73  is one that uses
30421 20 74 68 65 20 73 61 6d 65 20 73 74 72 61 74 65   the same strate
30422 67 79 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74 68  gy.    ** for th
30423 65 20 67 69 76 65 6e 20 46 52 4f 4d 20 63 6c 61  e given FROM cla
30424 75 73 65 20 65 6e 74 72 79 20 61 73 20 77 6f 75  use entry as wou
30425 6c 64 20 62 65 20 73 65 6c 65 63 74 65 64 20 69  ld be selected i
30426 66 20 74 68 65 20 65 6e 74 72 79 0a 20 20 20 20  f the entry.    
30427 2a 2a 20 77 65 72 65 20 75 73 65 64 20 61 73 20  ** were used as 
30428 74 68 65 20 69 6e 6e 65 72 6d 6f 73 74 20 6e 65  the innermost ne
30429 73 74 65 64 20 6c 6f 6f 70 2e 20 20 49 6e 20 6f  sted loop.  In o
3042a 74 68 65 72 20 77 6f 72 64 73 2c 20 61 20 74 61  ther words, a ta
3042b 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 63 68  ble.    ** is ch
3042c 6f 73 65 6e 20 73 75 63 68 20 74 68 61 74 20 74  osen such that t
3042d 68 65 20 63 6f 73 74 20 6f 66 20 72 75 6e 6e 69  he cost of runni
3042e 6e 67 20 74 68 61 74 20 74 61 62 6c 65 20 63 61  ng that table ca
3042f 6e 6e 6f 74 20 62 65 20 72 65 64 75 63 65 64 0a  nnot be reduced.
30430 20 20 20 20 2a 2a 20 62 79 20 77 61 69 74 69 6e      ** by waitin
30431 67 20 66 6f 72 20 6f 74 68 65 72 20 74 61 62 6c  g for other tabl
30432 65 73 20 74 6f 20 72 75 6e 20 66 69 72 73 74 2e  es to run first.
30433 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
30434 68 65 20 73 65 63 6f 6e 64 20 69 74 65 72 61 74  he second iterat
30435 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 70 65 72 66  ion is only perf
30436 6f 72 6d 65 64 20 69 66 20 6e 6f 20 6f 70 74 69  ormed if no opti
30437 6d 61 6c 20 73 63 61 6e 20 73 74 72 61 74 65 67  mal scan strateg
30438 69 65 73 0a 20 20 20 20 2a 2a 20 77 65 72 65 20  ies.    ** were 
30439 66 6f 75 6e 64 20 62 79 20 74 68 65 20 66 69 72  found by the fir
3043a 73 74 2e 20 54 68 69 73 20 69 74 65 72 61 74 69  st. This iterati
3043b 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 73 65  on is used to se
3043c 61 72 63 68 20 66 6f 72 20 74 68 65 0a 20 20 20  arch for the.   
3043d 20 2a 2a 20 6c 6f 77 65 73 74 20 63 6f 73 74 20   ** lowest cost 
3043e 73 63 61 6e 20 6f 76 65 72 61 6c 6c 2e 0a 20 20  scan overall..  
3043f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 50 72 65 76    **.    ** Prev
30440 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66  ious versions of
30441 20 53 51 4c 69 74 65 20 70 65 72 66 6f 72 6d 65   SQLite performe
30442 64 20 6f 6e 6c 79 20 74 68 65 20 73 65 63 6f 6e  d only the secon
30443 64 20 69 74 65 72 61 74 69 6f 6e 20 2d 0a 20 20  d iteration -.  
30444 20 20 2a 2a 20 74 68 65 20 6e 65 78 74 20 6f 75    ** the next ou
30445 74 65 72 6d 6f 73 74 20 6c 6f 6f 70 20 77 61 73  termost loop was
30446 20 61 6c 77 61 79 73 20 74 68 61 74 20 77 69 74   always that wit
30447 68 20 74 68 65 20 6c 6f 77 65 73 74 20 6f 76 65  h the lowest ove
30448 72 61 6c 6c 0a 20 20 20 20 2a 2a 20 63 6f 73 74  rall.    ** cost
30449 2e 20 48 6f 77 65 76 65 72 2c 20 74 68 69 73 20  . However, this 
3044a 6d 65 61 6e 74 20 74 68 61 74 20 53 51 4c 69 74  meant that SQLit
3044b 65 20 63 6f 75 6c 64 20 73 65 6c 65 63 74 20 74  e could select t
3044c 68 65 20 77 72 6f 6e 67 20 70 6c 61 6e 0a 20 20  he wrong plan.  
3044d 20 20 2a 2a 20 66 6f 72 20 73 63 72 69 70 74 73    ** for scripts
3044e 20 73 75 63 68 20 61 73 20 74 68 65 20 66 6f 6c   such as the fol
3044f 6c 6f 77 69 6e 67 3a 0a 20 20 20 20 2a 2a 20 20  lowing:.    **  
30450 20 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54   .    **   CREAT
30451 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 29  E TABLE t1(a, b)
30452 3b 20 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41  ; .    **   CREA
30453 54 45 20 54 41 42 4c 45 20 74 32 28 63 2c 20 64  TE TABLE t2(c, d
30454 29 3b 0a 20 20 20 20 2a 2a 20 20 20 53 45 4c 45  );.    **   SELE
30455 43 54 20 2a 20 46 52 4f 4d 20 74 32 2c 20 74 31  CT * FROM t2, t1
30456 20 57 48 45 52 45 20 74 32 2e 72 6f 77 69 64 20   WHERE t2.rowid 
30457 3d 20 74 31 2e 61 3b 0a 20 20 20 20 2a 2a 0a 20  = t1.a;.    **. 
30458 20 20 20 2a 2a 20 54 68 65 20 62 65 73 74 20 73     ** The best s
30459 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 69 74  trategy is to it
3045a 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 74 61  erate through ta
3045b 62 6c 65 20 74 31 20 66 69 72 73 74 2e 20 48 6f  ble t1 first. Ho
3045c 77 65 76 65 72 20 69 74 0a 20 20 20 20 2a 2a 20  wever it.    ** 
3045d 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
3045e 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 69  to determine thi
3045f 73 20 77 69 74 68 20 61 20 73 69 6d 70 6c 65 20  s with a simple 
30460 67 72 65 65 64 79 20 61 6c 67 6f 72 69 74 68 6d  greedy algorithm
30461 2e 0a 20 20 20 20 2a 2a 20 48 6f 77 65 76 65 72  ..    ** However
30462 2c 20 73 69 6e 63 65 20 74 68 65 20 63 6f 73 74  , since the cost
30463 20 6f 66 20 61 20 6c 69 6e 65 61 72 20 73 63 61   of a linear sca
30464 6e 20 74 68 72 6f 75 67 68 20 74 61 62 6c 65 20  n through table 
30465 74 32 20 69 73 20 74 68 65 20 73 61 6d 65 20 0a  t2 is the same .
30466 20 20 20 20 2a 2a 20 61 73 20 74 68 65 20 63 6f      ** as the co
30467 73 74 20 6f 66 20 61 20 6c 69 6e 65 61 72 20 73  st of a linear s
30468 63 61 6e 20 74 68 72 6f 75 67 68 20 74 61 62 6c  can through tabl
30469 65 20 74 31 2c 20 61 20 73 69 6d 70 6c 65 20 67  e t1, a simple g
3046a 72 65 65 64 79 20 0a 20 20 20 20 2a 2a 20 61 6c  reedy .    ** al
3046b 67 6f 72 69 74 68 6d 20 6d 61 79 20 63 68 6f 6f  gorithm may choo
3046c 73 65 20 74 6f 20 75 73 65 20 74 32 20 66 6f 72  se to use t2 for
3046d 20 74 68 65 20 6f 75 74 65 72 20 6c 6f 6f 70 2c   the outer loop,
3046e 20 77 68 69 63 68 20 69 73 20 61 20 6d 75 63 68   which is a much
3046f 0a 20 20 20 20 2a 2a 20 63 6f 73 74 6c 69 65 72  .    ** costlier
30470 20 61 70 70 72 6f 61 63 68 2e 0a 20 20 20 20 2a   approach..    *
30471 2f 0a 20 20 20 20 66 6f 72 28 69 73 4f 70 74 69  /.    for(isOpti
30472 6d 61 6c 3d 31 3b 20 69 73 4f 70 74 69 6d 61 6c  mal=1; isOptimal
30473 3e 3d 30 20 26 26 20 62 65 73 74 4a 3c 30 3b 20  >=0 && bestJ<0; 
30474 69 73 4f 70 74 69 6d 61 6c 2d 2d 29 7b 0a 20 20  isOptimal--){.  
30475 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b      Bitmask mask
30476 20 3d 20 28 69 73 4f 70 74 69 6d 61 6c 20 3f 20   = (isOptimal ? 
30477 30 20 3a 20 6e 6f 74 52 65 61 64 79 29 3b 0a 20  0 : notReady);. 
30478 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54       assert( (pT
30479 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 69 46 72  abList->nSrc-iFr
3047a 6f 6d 29 3e 31 20 7c 7c 20 69 73 4f 70 74 69 6d  om)>1 || isOptim
3047b 61 6c 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  al );.      for(
3047c 6a 3d 69 46 72 6f 6d 2c 20 70 54 61 62 49 74 65  j=iFrom, pTabIte
3047d 6d 3d 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a  m=&pTabList->a[j
3047e 5d 3b 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  ]; j<pTabList->n
3047f 53 72 63 3b 20 6a 2b 2b 2c 20 70 54 61 62 49 74  Src; j++, pTabIt
30480 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  em++){.        i
30481 6e 74 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 3b  nt doNotReorder;
30482 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
30483 68 69 73 20 74 61 62 6c 65 20 73 68 6f 75 6c 64  his table should
30484 20 6e 6f 74 20 62 65 20 72 65 6f 72 64 65 72 65   not be reordere
30485 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 57 68 65  d */.        Whe
30486 72 65 43 6f 73 74 20 73 43 6f 73 74 3b 20 20 20  reCost sCost;   
30487 20 20 2f 2a 20 43 6f 73 74 20 69 6e 66 6f 72 6d    /* Cost inform
30488 61 74 69 6f 6e 20 66 72 6f 6d 20 62 65 73 74 5b  ation from best[
30489 56 69 72 74 75 61 6c 5d 49 6e 64 65 78 28 29 20  Virtual]Index() 
3048a 2a 2f 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c  */.        ExprL
3048b 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20  ist *pOrderBy;  
3048c 2f 2a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  /* ORDER BY clau
3048d 73 65 20 66 6f 72 20 69 6e 64 65 78 20 74 6f 20  se for index to 
3048e 6f 70 74 69 6d 69 7a 65 20 2a 2f 0a 20 20 0a 20  optimize */.  . 
3048f 20 20 20 20 20 20 20 64 6f 4e 6f 74 52 65 6f 72         doNotReor
30490 64 65 72 20 3d 20 20 28 70 54 61 62 49 74 65 6d  der =  (pTabItem
30491 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a 54  ->jointype & (JT
30492 5f 4c 45 46 54 7c 4a 54 5f 43 52 4f 53 53 29 29  _LEFT|JT_CROSS))
30493 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  !=0;.        if(
30494 20 6a 21 3d 69 46 72 6f 6d 20 26 26 20 64 6f 4e   j!=iFrom && doN
30495 6f 74 52 65 6f 72 64 65 72 20 29 20 62 72 65 61  otReorder ) brea
30496 6b 3b 0a 20 20 20 20 20 20 20 20 6d 20 3d 20 67  k;.        m = g
30497 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  etMask(pMaskSet,
30498 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73   pTabItem->iCurs
30499 6f 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  or);.        if(
3049a 20 28 6d 20 26 20 6e 6f 74 52 65 61 64 79 29 3d   (m & notReady)=
3049b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
3049c 69 66 28 20 6a 3d 3d 69 46 72 6f 6d 20 29 20 69  if( j==iFrom ) i
3049d 46 72 6f 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20  From++;.        
3049e 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
3049f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4f      }.        pO
304a0 72 64 65 72 42 79 20 3d 20 28 28 69 3d 3d 30 20  rderBy = ((i==0 
304a1 26 26 20 70 70 4f 72 64 65 72 42 79 20 29 3f 2a  && ppOrderBy )?*
304a2 70 70 4f 72 64 65 72 42 79 3a 30 29 3b 0a 20 20  ppOrderBy:0);.  
304a3 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
304a4 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 20   pTabItem->pTab 
304a5 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
304a6 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
304a7 42 4c 45 0a 20 20 20 20 20 20 20 20 69 66 28 20  BLE.        if( 
304a8 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 49 74  IsVirtual(pTabIt
304a9 65 6d 2d 3e 70 54 61 62 29 20 29 7b 0a 20 20 20  em->pTab) ){.   
304aa 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
304ab 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 20 3d  ndex_info **pp =
304ac 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 6a 5d 2e 70   &pWInfo->a[j].p
304ad 49 64 78 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20  IdxInfo;.       
304ae 20 20 20 62 65 73 74 56 69 72 74 75 61 6c 49 6e     bestVirtualIn
304af 64 65 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c  dex(pParse, pWC,
304b0 20 70 54 61 62 49 74 65 6d 2c 20 6d 61 73 6b 2c   pTabItem, mask,
304b1 20 70 4f 72 64 65 72 42 79 2c 20 26 73 43 6f 73   pOrderBy, &sCos
304b2 74 2c 20 70 70 29 3b 0a 20 20 20 20 20 20 20 20  t, pp);.        
304b3 7d 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20  }else .#endif.  
304b4 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
304b5 20 20 62 65 73 74 42 74 72 65 65 49 6e 64 65 78    bestBtreeIndex
304b6 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 54  (pParse, pWC, pT
304b7 61 62 49 74 65 6d 2c 20 6d 61 73 6b 2c 20 70 4f  abItem, mask, pO
304b8 72 64 65 72 42 79 2c 20 26 73 43 6f 73 74 29 3b  rderBy, &sCost);
304b9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
304ba 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 74     assert( isOpt
304bb 69 6d 61 6c 20 7c 7c 20 28 73 43 6f 73 74 2e 75  imal || (sCost.u
304bc 73 65 64 26 6e 6f 74 52 65 61 64 79 29 3d 3d 30  sed&notReady)==0
304bd 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28   );..        if(
304be 20 28 73 43 6f 73 74 2e 75 73 65 64 26 6e 6f 74   (sCost.used&not
304bf 52 65 61 64 79 29 3d 3d 30 0a 20 20 20 20 20 20  Ready)==0.      
304c0 20 20 20 26 26 20 28 6a 3d 3d 69 46 72 6f 6d 20     && (j==iFrom 
304c1 7c 7c 20 73 43 6f 73 74 2e 72 43 6f 73 74 3c 62  || sCost.rCost<b
304c2 65 73 74 50 6c 61 6e 2e 72 43 6f 73 74 29 20 0a  estPlan.rCost) .
304c3 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
304c4 20 20 20 20 20 62 65 73 74 50 6c 61 6e 20 3d 20       bestPlan = 
304c5 73 43 6f 73 74 3b 0a 20 20 20 20 20 20 20 20 20  sCost;.         
304c6 20 62 65 73 74 4a 20 3d 20 6a 3b 0a 20 20 20 20   bestJ = j;.    
304c7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
304c8 28 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29  ( doNotReorder )
304c9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
304ca 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
304cb 28 20 62 65 73 74 4a 3e 3d 30 20 29 3b 0a 20 20  ( bestJ>=0 );.  
304cc 20 20 61 73 73 65 72 74 28 20 6e 6f 74 52 65 61    assert( notRea
304cd 64 79 20 26 20 67 65 74 4d 61 73 6b 28 70 4d 61  dy & getMask(pMa
304ce 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d  skSet, pTabList-
304cf 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75 72 73 6f  >a[bestJ].iCurso
304d0 72 29 20 29 3b 0a 20 20 20 20 57 48 45 52 45 54  r) );.    WHERET
304d1 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d  RACE(("*** Optim
304d2 69 7a 65 72 20 73 65 6c 65 63 74 73 20 74 61 62  izer selects tab
304d3 6c 65 20 25 64 20 66 6f 72 20 6c 6f 6f 70 20 25  le %d for loop %
304d4 64 5c 6e 22 2c 20 62 65 73 74 4a 2c 0a 20 20 20  d\n", bestJ,.   
304d5 20 20 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 70          pLevel-p
304d6 57 49 6e 66 6f 2d 3e 61 29 29 3b 0a 20 20 20 20  WInfo->a));.    
304d7 69 66 28 20 28 62 65 73 74 50 6c 61 6e 2e 70 6c  if( (bestPlan.pl
304d8 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
304d9 52 45 5f 4f 52 44 45 52 42 59 29 21 3d 30 20 29  RE_ORDERBY)!=0 )
304da 7b 0a 20 20 20 20 20 20 2a 70 70 4f 72 64 65 72  {.      *ppOrder
304db 42 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  By = 0;.    }.  
304dc 20 20 61 6e 64 46 6c 61 67 73 20 26 3d 20 62 65    andFlags &= be
304dd 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c  stPlan.plan.wsFl
304de 61 67 73 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  ags;.    pLevel-
304df 3e 70 6c 61 6e 20 3d 20 62 65 73 74 50 6c 61 6e  >plan = bestPlan
304e0 2e 70 6c 61 6e 3b 0a 20 20 20 20 69 66 28 20 62  .plan;.    if( b
304e1 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46  estPlan.plan.wsF
304e2 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44  lags & WHERE_IND
304e3 45 58 45 44 20 29 7b 0a 20 20 20 20 20 20 70 4c  EXED ){.      pL
304e4 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20  evel->iIdxCur = 
304e5 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
304e6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
304e7 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
304e8 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20   = -1;.    }.   
304e9 20 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e 67 65   notReady &= ~ge
304ea 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
304eb 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74  pTabList->a[best
304ec 4a 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20  J].iCursor);.   
304ed 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 20 3d   pLevel->iFrom =
304ee 20 28 75 38 29 62 65 73 74 4a 3b 0a 0a 20 20 20   (u8)bestJ;..   
304ef 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 69   /* Check that i
304f0 66 20 74 68 65 20 74 61 62 6c 65 20 73 63 61 6e  f the table scan
304f1 6e 65 64 20 62 79 20 74 68 69 73 20 6c 6f 6f 70  ned by this loop
304f2 20 69 74 65 72 61 74 69 6f 6e 20 68 61 64 20 61   iteration had a
304f3 6e 0a 20 20 20 20 2a 2a 20 49 4e 44 45 58 45 44  n.    ** INDEXED
304f4 20 42 59 20 63 6c 61 75 73 65 20 61 74 74 61 63   BY clause attac
304f5 68 65 64 20 74 6f 20 69 74 2c 20 74 68 61 74 20  hed to it, that 
304f6 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20  the named index 
304f7 69 73 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20  is being.    ** 
304f8 75 73 65 64 20 66 6f 72 20 74 68 65 20 73 63 61  used for the sca
304f9 6e 2e 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20  n. If not, then 
304fa 71 75 65 72 79 20 63 6f 6d 70 69 6c 61 74 69 6f  query compilatio
304fb 6e 20 68 61 73 20 66 61 69 6c 65 64 2e 0a 20 20  n has failed..  
304fc 20 20 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65    ** Return an e
304fd 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rror..    */.   
304fe 20 70 49 64 78 20 3d 20 70 54 61 62 4c 69 73 74   pIdx = pTabList
304ff 2d 3e 61 5b 62 65 73 74 4a 5d 2e 70 49 6e 64 65  ->a[bestJ].pInde
30500 78 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 20  x;.    if( pIdx 
30501 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 62 65  ){.      if( (be
30502 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c  stPlan.plan.wsFl
30503 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45  ags & WHERE_INDE
30504 58 45 44 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  XED)==0 ){.     
30505 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
30506 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
30507 6f 74 20 75 73 65 20 69 6e 64 65 78 3a 20 25 73  ot use index: %s
30508 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b  ", pIdx->zName);
30509 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 77 68  .        goto wh
3050a 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20  ereBeginError;. 
3050b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
3050c 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 49 4e 44      /* If an IND
3050d 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 20 69  EXED BY clause i
3050e 73 20 75 73 65 64 2c 20 74 68 65 20 62 65 73 74  s used, the best
3050f 49 6e 64 65 78 28 29 20 66 75 6e 63 74 69 6f 6e  Index() function
30510 20 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 67   is.        ** g
30511 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69 6e  uaranteed to fin
30512 64 20 74 68 65 20 69 6e 64 65 78 20 73 70 65 63  d the index spec
30513 69 66 69 65 64 20 69 6e 20 74 68 65 20 49 4e 44  ified in the IND
30514 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 0a 20  EXED BY clause. 
30515 20 20 20 20 20 20 20 2a 2a 20 69 66 20 69 74 20         ** if it 
30516 66 69 6e 64 20 61 6e 20 69 6e 64 65 78 20 61 74  find an index at
30517 20 61 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20   all. */.       
30518 20 61 73 73 65 72 74 28 20 62 65 73 74 50 6c 61   assert( bestPla
30519 6e 2e 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 70  n.plan.u.pIdx==p
3051a 49 64 78 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Idx );.      }. 
3051b 20 20 20 7d 0a 20 20 7d 0a 20 20 57 48 45 52 45     }.  }.  WHERE
3051c 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69  TRACE(("*** Opti
3051d 6d 69 7a 65 72 20 46 69 6e 69 73 68 65 64 20 2a  mizer Finished *
3051e 2a 2a 5c 6e 22 29 29 3b 0a 20 20 69 66 28 20 70  **\n"));.  if( p
3051f 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
30520 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
30521 29 7b 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72  ){.    goto wher
30522 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d  eBeginError;.  }
30523 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 6f  ..  /* If the to
30524 74 61 6c 20 71 75 65 72 79 20 6f 6e 6c 79 20 73  tal query only s
30525 65 6c 65 63 74 73 20 61 20 73 69 6e 67 6c 65 20  elects a single 
30526 72 6f 77 2c 20 74 68 65 6e 20 74 68 65 20 4f 52  row, then the OR
30527 44 45 52 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75  DER BY.  ** clau
30528 73 65 20 69 73 20 69 72 72 65 6c 65 76 61 6e 74  se is irrelevant
30529 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 61 6e  ..  */.  if( (an
3052a 64 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 55  dFlags & WHERE_U
3052b 4e 49 51 55 45 29 21 3d 30 20 26 26 20 70 70 4f  NIQUE)!=0 && ppO
3052c 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 2a 70  rderBy ){.    *p
3052d 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20  pOrderBy = 0;.  
3052e 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63  }..  /* If the c
3052f 61 6c 6c 65 72 20 69 73 20 61 6e 20 55 50 44 41  aller is an UPDA
30530 54 45 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61  TE or DELETE sta
30531 74 65 6d 65 6e 74 20 74 68 61 74 20 69 73 20 72  tement that is r
30532 65 71 75 65 73 74 69 6e 67 0a 20 20 2a 2a 20 74  equesting.  ** t
30533 6f 20 75 73 65 20 61 20 6f 6e 65 2d 70 61 73 73  o use a one-pass
30534 20 61 6c 67 6f 72 69 74 68 6d 2c 20 64 65 74 65   algorithm, dete
30535 72 6d 69 6e 65 20 69 66 20 74 68 69 73 20 69 73  rmine if this is
30536 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 20 20   appropriate..  
30537 2a 2a 20 54 68 65 20 6f 6e 65 2d 70 61 73 73 20  ** The one-pass 
30538 61 6c 67 6f 72 69 74 68 6d 20 6f 6e 6c 79 20 77  algorithm only w
30539 6f 72 6b 73 20 69 66 20 74 68 65 20 57 48 45 52  orks if the WHER
3053a 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61  E clause constra
3053b 69 6e 74 73 0a 20 20 2a 2a 20 74 68 65 20 73 74  ints.  ** the st
3053c 61 74 65 6d 65 6e 74 20 74 6f 20 75 70 64 61 74  atement to updat
3053d 65 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 2e 0a  e a single row..
3053e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28    */.  assert( (
3053f 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45  wctrlFlags & WHE
30540 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52  RE_ONEPASS_DESIR
30541 45 44 29 3d 3d 30 20 7c 7c 20 70 57 49 6e 66 6f  ED)==0 || pWInfo
30542 2d 3e 6e 4c 65 76 65 6c 3d 3d 31 20 29 3b 0a 20  ->nLevel==1 );. 
30543 20 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73   if( (wctrlFlags
30544 20 26 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53   & WHERE_ONEPASS
30545 5f 44 45 53 49 52 45 44 29 21 3d 30 20 26 26 20  _DESIRED)!=0 && 
30546 28 61 6e 64 46 6c 61 67 73 20 26 20 57 48 45 52  (andFlags & WHER
30547 45 5f 55 4e 49 51 55 45 29 21 3d 30 20 29 7b 0a  E_UNIQUE)!=0 ){.
30548 20 20 20 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e      pWInfo->okOn
30549 65 50 61 73 73 20 3d 20 31 3b 0a 20 20 20 20 70  ePass = 1;.    p
3054a 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e 70 6c 61 6e  WInfo->a[0].plan
3054b 2e 77 73 46 6c 61 67 73 20 26 3d 20 7e 57 48 45  .wsFlags &= ~WHE
3054c 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 20 20 7d  RE_IDX_ONLY;.  }
3054d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61 6c 6c 20  ..  /* Open all 
3054e 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 70 54  tables in the pT
3054f 61 62 4c 69 73 74 20 61 6e 64 20 61 6e 79 20 69  abList and any i
30550 6e 64 69 63 65 73 20 73 65 6c 65 63 74 65 64 20  ndices selected 
30551 66 6f 72 0a 20 20 2a 2a 20 73 65 61 72 63 68 69  for.  ** searchi
30552 6e 67 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e  ng those tables.
30553 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43  .  */.  sqlite3C
30554 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
30555 70 50 61 72 73 65 2c 20 2d 31 29 3b 20 2f 2a 20  pParse, -1); /* 
30556 49 6e 73 65 72 74 20 74 68 65 20 63 6f 6f 6b 69  Insert the cooki
30557 65 20 76 65 72 69 66 69 65 72 20 47 6f 74 6f 20  e verifier Goto 
30558 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c  */.  for(i=0, pL
30559 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20  evel=pWInfo->a; 
3055a 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
3055b 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29  ; i++, pLevel++)
3055c 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  {.    Table *pTa
3055d 62 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  b;     /* Table 
3055e 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69  to open */.    i
3055f 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
30560 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61  /* Index of data
30561 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
30562 74 61 62 6c 65 2f 69 6e 64 65 78 20 2a 2f 0a 0a  table/index */..
30563 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
30564 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20  MIT_EXPLAIN.    
30565 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c  if( pParse->expl
30566 61 69 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20  ain==2 ){.      
30567 63 68 61 72 20 2a 7a 4d 73 67 3b 0a 20 20 20 20  char *zMsg;.    
30568 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
30569 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26  _item *pItem = &
3056a 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76  pTabList->a[pLev
3056b 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20  el->iFrom];.    
3056c 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33    zMsg = sqlite3
3056d 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 54 41 42  MPrintf(db, "TAB
3056e 4c 45 20 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a  LE %s", pItem->z
3056f 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Name);.      if(
30570 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29   pItem->zAlias )
30571 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d  {.        zMsg =
30572 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66   sqlite3MAppendf
30573 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 41  (db, zMsg, "%s A
30574 53 20 25 73 22 2c 20 7a 4d 73 67 2c 20 70 49 74  S %s", zMsg, pIt
30575 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20  em->zAlias);.   
30576 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28     }.      if( (
30577 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
30578 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44  lags & WHERE_IND
30579 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20  EXED)!=0 ){.    
3057a 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74      zMsg = sqlit
3057b 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a  e3MAppendf(db, z
3057c 4d 73 67 2c 20 22 25 73 20 57 49 54 48 20 49 4e  Msg, "%s WITH IN
3057d 44 45 58 20 25 73 22 2c 0a 20 20 20 20 20 20 20  DEX %s",.       
3057e 20 20 20 20 7a 4d 73 67 2c 20 70 4c 65 76 65 6c      zMsg, pLevel
3057f 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a  ->plan.u.pIdx->z
30580 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Name);.      }el
30581 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70  se if( pLevel->p
30582 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
30583 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 29 7b 0a  ERE_MULTI_OR ){.
30584 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73          zMsg = s
30585 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64  qlite3MAppendf(d
30586 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 56 49 41  b, zMsg, "%s VIA
30587 20 4d 55 4c 54 49 2d 49 4e 44 45 58 20 55 4e 49   MULTI-INDEX UNI
30588 4f 4e 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20  ON", zMsg);.    
30589 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76    }else if( pLev
3058a 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
3058b 20 26 20 28 57 48 45 52 45 5f 52 4f 57 49 44 5f   & (WHERE_ROWID_
3058c 45 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52  EQ|WHERE_ROWID_R
3058d 41 4e 47 45 29 20 29 7b 0a 20 20 20 20 20 20 20  ANGE) ){.       
3058e 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
3058f 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67  Appendf(db, zMsg
30590 2c 20 22 25 73 20 55 53 49 4e 47 20 50 52 49 4d  , "%s USING PRIM
30591 41 52 59 20 4b 45 59 22 2c 20 7a 4d 73 67 29 3b  ARY KEY", zMsg);
30592 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  .      }.#ifndef
30593 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
30594 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20  TUALTABLE.      
30595 65 6c 73 65 20 69 66 28 20 28 70 4c 65 76 65 6c  else if( (pLevel
30596 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
30597 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41   WHERE_VIRTUALTA
30598 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  BLE)!=0 ){.     
30599 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78     sqlite3_index
3059a 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 20  _info *pVtabIdx 
3059b 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  = pLevel->plan.u
3059c 2e 70 56 74 61 62 49 64 78 3b 0a 20 20 20 20 20  .pVtabIdx;.     
3059d 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65     zMsg = sqlite
3059e 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d  3MAppendf(db, zM
3059f 73 67 2c 20 22 25 73 20 56 49 52 54 55 41 4c 20  sg, "%s VIRTUAL 
305a0 54 41 42 4c 45 20 49 4e 44 45 58 20 25 64 3a 25  TABLE INDEX %d:%
305a1 73 22 2c 20 7a 4d 73 67 2c 0a 20 20 20 20 20 20  s", zMsg,.      
305a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56                pV
305a3 74 61 62 49 64 78 2d 3e 69 64 78 4e 75 6d 2c 20  tabIdx->idxNum, 
305a4 70 56 74 61 62 49 64 78 2d 3e 69 64 78 53 74 72  pVtabIdx->idxStr
305a5 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
305a6 66 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 76  f.      if( pLev
305a7 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
305a8 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59   & WHERE_ORDERBY
305a9 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67   ){.        zMsg
305aa 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e   = sqlite3MAppen
305ab 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73  df(db, zMsg, "%s
305ac 20 4f 52 44 45 52 20 42 59 22 2c 20 7a 4d 73 67   ORDER BY", zMsg
305ad 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
305ae 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
305af 70 34 28 76 2c 20 4f 50 5f 45 78 70 6c 61 69 6e  p4(v, OP_Explain
305b0 2c 20 69 2c 20 70 4c 65 76 65 6c 2d 3e 69 46 72  , i, pLevel->iFr
305b1 6f 6d 2c 20 30 2c 20 7a 4d 73 67 2c 20 50 34 5f  om, 0, zMsg, P4_
305b2 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 0a  DYNAMIC);.    }.
305b3 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
305b4 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f  _OMIT_EXPLAIN */
305b5 0a 20 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20  .    pTabItem = 
305b6 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65  &pTabList->a[pLe
305b7 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20  vel->iFrom];.   
305b8 20 70 54 61 62 20 3d 20 70 54 61 62 49 74 65 6d   pTab = pTabItem
305b9 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 44 62 20  ->pTab;.    iDb 
305ba 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
305bb 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
305bc 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 69  >pSchema);.    i
305bd 66 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61  f( (pTab->tabFla
305be 67 73 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61  gs & TF_Ephemera
305bf 6c 29 21 3d 30 20 7c 7c 20 70 54 61 62 2d 3e 70  l)!=0 || pTab->p
305c0 53 65 6c 65 63 74 20 29 20 63 6f 6e 74 69 6e 75  Select ) continu
305c1 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e;.#ifndef SQLIT
305c2 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
305c3 42 4c 45 0a 20 20 20 20 69 66 28 20 28 70 4c 65  BLE.    if( (pLe
305c4 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
305c5 73 20 26 20 57 48 45 52 45 5f 56 49 52 54 55 41  s & WHERE_VIRTUA
305c6 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20  LTABLE)!=0 ){.  
305c7 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
305c8 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63  pVTab = (const c
305c9 68 61 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74  har *)sqlite3Get
305ca 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29  VTable(db, pTab)
305cb 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 75 72  ;.      int iCur
305cc 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75   = pTabItem->iCu
305cd 72 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69  rsor;.      sqli
305ce 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
305cf 20 4f 50 5f 56 4f 70 65 6e 2c 20 69 43 75 72 2c   OP_VOpen, iCur,
305d0 20 30 2c 20 30 2c 20 70 56 54 61 62 2c 20 50 34   0, 0, pVTab, P4
305d1 5f 56 54 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73  _VTAB);.    }els
305d2 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  e.#endif.    if(
305d3 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
305d4 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
305d5 44 58 5f 4f 4e 4c 59 29 3d 3d 30 0a 20 20 20 20  DX_ONLY)==0.    
305d6 20 20 20 20 20 26 26 20 28 77 63 74 72 6c 46 6c       && (wctrlFl
305d7 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4d 49 54  ags & WHERE_OMIT
305d8 5f 4f 50 45 4e 29 3d 3d 30 20 29 7b 0a 20 20 20  _OPEN)==0 ){.   
305d9 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 57 49 6e     int op = pWIn
305da 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 3f 20  fo->okOnePass ? 
305db 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 3a 20 4f  OP_OpenWrite : O
305dc 50 5f 4f 70 65 6e 52 65 61 64 3b 0a 20 20 20 20  P_OpenRead;.    
305dd 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
305de 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 49  le(pParse, pTabI
305df 74 65 6d 2d 3e 69 43 75 72 73 6f 72 2c 20 69 44  tem->iCursor, iD
305e0 62 2c 20 70 54 61 62 2c 20 6f 70 29 3b 0a 20 20  b, pTab, op);.  
305e1 20 20 20 20 69 66 28 20 21 70 57 49 6e 66 6f 2d      if( !pWInfo-
305e2 3e 6f 6b 4f 6e 65 50 61 73 73 20 26 26 20 70 54  >okOnePass && pT
305e3 61 62 2d 3e 6e 43 6f 6c 3c 42 4d 53 20 29 7b 0a  ab->nCol<BMS ){.
305e4 20 20 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20          Bitmask 
305e5 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 63 6f  b = pTabItem->co
305e6 6c 55 73 65 64 3b 0a 20 20 20 20 20 20 20 20 69  lUsed;.        i
305e7 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  nt n = 0;.      
305e8 20 20 66 6f 72 28 3b 20 62 3b 20 62 3d 62 3e 3e    for(; b; b=b>>
305e9 31 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 20 20  1, n++){}.      
305ea 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
305eb 6e 67 65 50 34 28 76 2c 20 73 71 6c 69 74 65 33  ngeP4(v, sqlite3
305ec 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
305ed 76 29 2d 31 2c 20 53 51 4c 49 54 45 5f 49 4e 54  v)-1, SQLITE_INT
305ee 5f 54 4f 5f 50 54 52 28 6e 29 2c 20 50 34 5f 49  _TO_PTR(n), P4_I
305ef 4e 54 33 32 29 3b 0a 20 20 20 20 20 20 20 20 61  NT32);.        a
305f0 73 73 65 72 74 28 20 6e 3c 3d 70 54 61 62 2d 3e  ssert( n<=pTab->
305f1 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20 7d 0a  nCol );.      }.
305f2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
305f3 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
305f4 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70  k(pParse, iDb, p
305f5 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54  Tab->tnum, 0, pT
305f6 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
305f7 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 54  }.    pLevel->iT
305f8 61 62 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d  abCur = pTabItem
305f9 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 69  ->iCursor;.    i
305fa 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
305fb 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
305fc 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a  _INDEXED)!=0 ){.
305fd 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 78        Index *pIx
305fe 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
305ff 75 2e 70 49 64 78 3b 0a 20 20 20 20 20 20 4b 65  u.pIdx;.      Ke
30600 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71  yInfo *pKey = sq
30601 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66  lite3IndexKeyinf
30602 6f 28 70 50 61 72 73 65 2c 20 70 49 78 29 3b 0a  o(pParse, pIx);.
30603 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 43 75        int iIdxCu
30604 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  r = pLevel->iIdx
30605 43 75 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  Cur;.      asser
30606 74 28 20 70 49 78 2d 3e 70 53 63 68 65 6d 61 3d  t( pIx->pSchema=
30607 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
30608 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
30609 69 49 64 78 43 75 72 3e 3d 30 20 29 3b 0a 20 20  iIdxCur>=0 );.  
3060a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3060b 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
3060c 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70  Read, iIdxCur, p
3060d 49 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20  Ix->tnum, iDb,. 
3060e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3060f 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
30610 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  ey, P4_KEYINFO_H
30611 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 56  ANDOFF);.      V
30612 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
30613 25 73 22 2c 20 70 49 78 2d 3e 7a 4e 61 6d 65 29  %s", pIx->zName)
30614 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
30615 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
30616 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62  hema(pParse, iDb
30617 29 3b 0a 20 20 7d 0a 20 20 70 57 49 6e 66 6f 2d  );.  }.  pWInfo-
30618 3e 69 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  >iTop = sqlite3V
30619 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
3061a 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  );..  /* Generat
3061b 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 6f  e the code to do
3061c 20 74 68 65 20 73 65 61 72 63 68 2e 20 20 45 61   the search.  Ea
3061d 63 68 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  ch iteration of 
3061e 74 68 65 20 66 6f 72 0a 20 20 2a 2a 20 6c 6f 6f  the for.  ** loo
3061f 70 20 62 65 6c 6f 77 20 67 65 6e 65 72 61 74 65  p below generate
30620 73 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e  s code for a sin
30621 67 6c 65 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20  gle nested loop 
30622 6f 66 20 74 68 65 20 56 4d 0a 20 20 2a 2a 20 70  of the VM.  ** p
30623 72 6f 67 72 61 6d 2e 0a 20 20 2a 2f 0a 20 20 6e  rogram..  */.  n
30624 6f 74 52 65 61 64 79 20 3d 20 7e 28 42 69 74 6d  otReady = ~(Bitm
30625 61 73 6b 29 30 3b 0a 20 20 66 6f 72 28 69 3d 30  ask)0;.  for(i=0
30626 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
30627 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f  rc; i++){.    no
30628 74 52 65 61 64 79 20 3d 20 63 6f 64 65 4f 6e 65  tReady = codeOne
30629 4c 6f 6f 70 53 74 61 72 74 28 70 57 49 6e 66 6f  LoopStart(pWInfo
3062a 2c 20 69 2c 20 77 63 74 72 6c 46 6c 61 67 73 2c  , i, wctrlFlags,
3062b 20 6e 6f 74 52 65 61 64 79 29 3b 0a 20 20 20 20   notReady);.    
3062c 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75  pWInfo->iContinu
3062d 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d  e = pWInfo->a[i]
3062e 2e 61 64 64 72 43 6f 6e 74 3b 0a 20 20 7d 0a 0a  .addrCont;.  }..
3062f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
30630 53 54 20 20 2f 2a 20 46 6f 72 20 74 65 73 74 69  ST  /* For testi
30631 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
30632 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 2f   use only */.  /
30633 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65 20  * Record in the 
30634 71 75 65 72 79 20 70 6c 61 6e 20 69 6e 66 6f 72  query plan infor
30635 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
30636 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 0a 20   current table. 
30637 20 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64 65   ** and the inde
30638 78 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  x used to access
30639 20 69 74 20 28 69 66 20 61 6e 79 29 2e 20 20 49   it (if any).  I
3063a 66 20 74 68 65 20 74 61 62 6c 65 20 69 74 73 65  f the table itse
3063b 6c 66 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 75  lf.  ** is not u
3063c 73 65 64 2c 20 69 74 73 20 6e 61 6d 65 20 69 73  sed, its name is
3063d 20 6a 75 73 74 20 27 7b 7d 27 2e 20 20 49 66 20   just '{}'.  If 
3063e 6e 6f 20 69 6e 64 65 78 20 69 73 20 75 73 65 64  no index is used
3063f 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  .  ** the index 
30640 69 73 20 6c 69 73 74 65 64 20 61 73 20 22 7b 7d  is listed as "{}
30641 22 2e 20 20 49 66 20 74 68 65 20 70 72 69 6d 61  ".  If the prima
30642 72 79 20 6b 65 79 20 69 73 20 75 73 65 64 20 74  ry key is used t
30643 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6e 61  he.  ** index na
30644 6d 65 20 69 73 20 27 2a 27 2e 0a 20 20 2a 2f 0a  me is '*'..  */.
30645 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
30646 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
30647 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a  ){.    char *z;.
30648 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 70      int n;.    p
30649 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d  Level = &pWInfo-
3064a 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 54 61 62 49  >a[i];.    pTabI
3064b 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  tem = &pTabList-
3064c 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
3064d 5d 3b 0a 20 20 20 20 7a 20 3d 20 70 54 61 62 49  ];.    z = pTabI
3064e 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20  tem->zAlias;.   
3064f 20 69 66 28 20 7a 3d 3d 30 20 29 20 7a 20 3d 20   if( z==0 ) z = 
30650 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2d 3e  pTabItem->pTab->
30651 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 20 3d 20 73  zName;.    n = s
30652 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
30653 29 3b 0a 20 20 20 20 69 66 28 20 6e 2b 6e 51 50  );.    if( n+nQP
30654 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c  lan < sizeof(sql
30655 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29  ite3_query_plan)
30656 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  -10 ){.      if(
30657 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
30658 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44  Flags & WHERE_ID
30659 58 5f 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20  X_ONLY ){.      
3065a 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65    memcpy(&sqlite
3065b 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50  3_query_plan[nQP
3065c 6c 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32 29 3b 0a  lan], "{}", 2);.
3065d 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b          nQPlan +
3065e 3d 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 2;.      }else
3065f 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
30660 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  (&sqlite3_query_
30661 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 7a 2c  plan[nQPlan], z,
30662 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50   n);.        nQP
30663 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20  lan += n;.      
30664 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
30665 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61  query_plan[nQPla
30666 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20  n++] = ' ';.    
30667 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  }.    testcase( 
30668 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
30669 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57  lags & WHERE_ROW
3066a 49 44 5f 45 51 20 29 3b 0a 20 20 20 20 74 65 73  ID_EQ );.    tes
3066b 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70  tcase( pLevel->p
3066c 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
3066d 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20  ERE_ROWID_RANGE 
3066e 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  );.    if( pLeve
3066f 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
30670 26 20 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45  & (WHERE_ROWID_E
30671 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41  Q|WHERE_ROWID_RA
30672 4e 47 45 29 20 29 7b 0a 20 20 20 20 20 20 6d 65  NGE) ){.      me
30673 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75  mcpy(&sqlite3_qu
30674 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
30675 2c 20 22 2a 20 22 2c 20 32 29 3b 0a 20 20 20 20  , "* ", 2);.    
30676 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 32 3b 0a 20    nQPlan += 2;. 
30677 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 4c     }else if( (pL
30678 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
30679 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58  gs & WHERE_INDEX
3067a 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ED)!=0 ){.      
3067b 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
3067c 6e 33 30 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  n30(pLevel->plan
3067d 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b  .u.pIdx->zName);
3067e 0a 20 20 20 20 20 20 69 66 28 20 6e 2b 6e 51 50  .      if( n+nQP
3067f 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c  lan < sizeof(sql
30680 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29  ite3_query_plan)
30681 2d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  -2 ){.        me
30682 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75  mcpy(&sqlite3_qu
30683 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
30684 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  , pLevel->plan.u
30685 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 6e 29  .pIdx->zName, n)
30686 3b 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e  ;.        nQPlan
30687 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 73   += n;.        s
30688 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
30689 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20  n[nQPlan++] = ' 
3068a 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ';.      }.    }
3068b 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63  else{.      memc
3068c 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72  py(&sqlite3_quer
3068d 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20  y_plan[nQPlan], 
3068e 22 7b 7d 20 22 2c 20 33 29 3b 0a 20 20 20 20 20  "{} ", 3);.     
3068f 20 6e 51 50 6c 61 6e 20 2b 3d 20 33 3b 0a 20 20   nQPlan += 3;.  
30690 20 20 7d 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28    }.  }.  while(
30691 20 6e 51 50 6c 61 6e 3e 30 20 26 26 20 73 71 6c   nQPlan>0 && sql
30692 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
30693 6e 51 50 6c 61 6e 2d 31 5d 3d 3d 27 20 27 20 29  nQPlan-1]==' ' )
30694 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 71 75  {.    sqlite3_qu
30695 65 72 79 5f 70 6c 61 6e 5b 2d 2d 6e 51 50 6c 61  ery_plan[--nQPla
30696 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  n] = 0;.  }.  sq
30697 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
30698 5b 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20 20  [nQPlan] = 0;.  
30699 6e 51 50 6c 61 6e 20 3d 20 30 3b 0a 23 65 6e 64  nQPlan = 0;.#end
3069a 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53  if /* SQLITE_TES
3069b 54 20 2f 2f 20 54 65 73 74 69 6e 67 20 61 6e 64  T // Testing and
3069c 20 64 65 62 75 67 67 69 6e 67 20 75 73 65 20 6f   debugging use o
3069d 6e 6c 79 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 63  nly */..  /* Rec
3069e 6f 72 64 20 74 68 65 20 63 6f 6e 74 69 6e 75 61  ord the continua
3069f 74 69 6f 6e 20 61 64 64 72 65 73 73 20 69 6e 20  tion address in 
306a0 74 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73 74  the WhereInfo st
306a1 72 75 63 74 75 72 65 2e 20 20 54 68 65 6e 0a 20  ructure.  Then. 
306a2 20 2a 2a 20 63 6c 65 61 6e 20 75 70 20 61 6e 64   ** clean up and
306a3 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20   return..  */.  
306a4 72 65 74 75 72 6e 20 70 57 49 6e 66 6f 3b 0a 0a  return pWInfo;..
306a5 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
306a6 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a  f malloc fails *
306a7 2f 0a 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f  /.whereBeginErro
306a8 72 3a 0a 20 20 77 68 65 72 65 49 6e 66 6f 46 72  r:.  whereInfoFr
306a9 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a  ee(db, pWInfo);.
306aa 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
306ab 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68  *.** Generate th
306ac 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45  e end of the WHE
306ad 52 45 20 6c 6f 6f 70 2e 20 20 53 65 65 20 63 6f  RE loop.  See co
306ae 6d 6d 65 6e 74 73 20 6f 6e 20 0a 2a 2a 20 73 71  mments on .** sq
306af 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
306b0 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  ) for additional
306b1 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
306b2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
306b3 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 72  void sqlite3Wher
306b4 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 20 2a  eEnd(WhereInfo *
306b5 70 57 49 6e 66 6f 29 7b 0a 20 20 50 61 72 73 65  pWInfo){.  Parse
306b6 20 2a 70 50 61 72 73 65 20 3d 20 70 57 49 6e 66   *pParse = pWInf
306b7 6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20 56 64 62  o->pParse;.  Vdb
306b8 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
306b9 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Vdbe;.  int i;. 
306ba 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65   WhereLevel *pLe
306bb 76 65 6c 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a  vel;.  SrcList *
306bc 70 54 61 62 4c 69 73 74 20 3d 20 70 57 49 6e 66  pTabList = pWInf
306bd 6f 2d 3e 70 54 61 62 4c 69 73 74 3b 0a 20 20 73  o->pTabList;.  s
306be 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
306bf 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 47  rse->db;..  /* G
306c0 65 6e 65 72 61 74 65 20 6c 6f 6f 70 20 74 65 72  enerate loop ter
306c1 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 2e 0a 20  mination code.. 
306c2 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70   */.  sqlite3Exp
306c3 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
306c4 73 65 29 3b 0a 20 20 66 6f 72 28 69 3d 70 54 61  se);.  for(i=pTa
306c5 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 3b 20 69  bList->nSrc-1; i
306c6 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70  >=0; i--){.    p
306c7 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d  Level = &pWInfo-
306c8 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 71 6c 69 74  >a[i];.    sqlit
306c9 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
306ca 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 61 64  el(v, pLevel->ad
306cb 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 69 66 28  drCont);.    if(
306cc 20 70 4c 65 76 65 6c 2d 3e 6f 70 21 3d 4f 50 5f   pLevel->op!=OP_
306cd 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 73 71  Noop ){.      sq
306ce 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
306cf 76 2c 20 70 4c 65 76 65 6c 2d 3e 6f 70 2c 20 70  v, pLevel->op, p
306d0 4c 65 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65  Level->p1, pLeve
306d1 6c 2d 3e 70 32 29 3b 0a 20 20 20 20 20 20 73 71  l->p2);.      sq
306d2 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
306d3 35 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 70 35 29  5(v, pLevel->p5)
306d4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
306d5 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
306d6 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 5f  lags & WHERE_IN_
306d7 41 42 4c 45 20 26 26 20 70 4c 65 76 65 6c 2d 3e  ABLE && pLevel->
306d8 75 2e 69 6e 2e 6e 49 6e 3e 30 20 29 7b 0a 20 20  u.in.nIn>0 ){.  
306d9 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f      struct InLoo
306da 70 20 2a 70 49 6e 3b 0a 20 20 20 20 20 20 69 6e  p *pIn;.      in
306db 74 20 6a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  t j;.      sqlit
306dc 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
306dd 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 61 64  el(v, pLevel->ad
306de 64 72 4e 78 74 29 3b 0a 20 20 20 20 20 20 66 6f  drNxt);.      fo
306df 72 28 6a 3d 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  r(j=pLevel->u.in
306e0 2e 6e 49 6e 2c 20 70 49 6e 3d 26 70 4c 65 76 65  .nIn, pIn=&pLeve
306e1 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 5b  l->u.in.aInLoop[
306e2 6a 2d 31 5d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20  j-1]; j>0; j--, 
306e3 70 49 6e 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20  pIn--){.        
306e4 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
306e5 65 72 65 28 76 2c 20 70 49 6e 2d 3e 61 64 64 72  ere(v, pIn->addr
306e6 49 6e 54 6f 70 2b 31 29 3b 0a 20 20 20 20 20 20  InTop+1);.      
306e7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
306e8 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
306e9 70 49 6e 2d 3e 69 43 75 72 2c 20 70 49 6e 2d 3e  pIn->iCur, pIn->
306ea 61 64 64 72 49 6e 54 6f 70 29 3b 0a 20 20 20 20  addrInTop);.    
306eb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
306ec 75 6d 70 48 65 72 65 28 76 2c 20 70 49 6e 2d 3e  umpHere(v, pIn->
306ed 61 64 64 72 49 6e 54 6f 70 2d 31 29 3b 0a 20 20  addrInTop-1);.  
306ee 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
306ef 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
306f0 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f  evel->u.in.aInLo
306f1 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  op);.    }.    s
306f2 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
306f3 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76 65 6c  eLabel(v, pLevel
306f4 2d 3e 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20  ->addrBrk);.    
306f5 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66  if( pLevel->iLef
306f6 74 4a 6f 69 6e 20 29 7b 0a 20 20 20 20 20 20 69  tJoin ){.      i
306f7 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 61  nt addr;.      a
306f8 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
306f9 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
306fa 50 6f 73 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65  Pos, pLevel->iLe
306fb 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20 20 20 73  ftJoin);.      s
306fc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
306fd 28 76 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20  (v, OP_NullRow, 
306fe 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69  pTabList->a[i].i
306ff 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69  Cursor);.      i
30700 66 28 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43  f( pLevel->iIdxC
30701 75 72 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ur>=0 ){.       
30702 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
30703 70 31 28 76 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77  p1(v, OP_NullRow
30704 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75  , pLevel->iIdxCu
30705 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
30706 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 6f 70    if( pLevel->op
30707 3d 3d 4f 50 5f 52 65 74 75 72 6e 20 29 7b 0a 20  ==OP_Return ){. 
30708 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
30709 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
3070a 6f 73 75 62 2c 20 70 4c 65 76 65 6c 2d 3e 70 31  osub, pLevel->p1
3070b 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 46 69  , pLevel->addrFi
3070c 72 73 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rst);.      }els
3070d 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
3070e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
3070f 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 4c 65 76  OP_Goto, 0, pLev
30710 65 6c 2d 3e 61 64 64 72 46 69 72 73 74 29 3b 0a  el->addrFirst);.
30711 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
30712 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
30713 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20  e(v, addr);.    
30714 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  }.  }..  /* The 
30715 22 62 72 65 61 6b 22 20 70 6f 69 6e 74 20 69 73  "break" point is
30716 20 68 65 72 65 2c 20 6a 75 73 74 20 70 61 73 74   here, just past
30717 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
30718 6f 75 74 65 72 20 6c 6f 6f 70 2e 0a 20 20 2a 2a  outer loop..  **
30719 20 53 65 74 20 69 74 2e 0a 20 20 2a 2f 0a 20 20   Set it..  */.  
3071a 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
3071b 76 65 4c 61 62 65 6c 28 76 2c 20 70 57 49 6e 66  veLabel(v, pWInf
3071c 6f 2d 3e 69 42 72 65 61 6b 29 3b 0a 0a 20 20 2f  o->iBreak);..  /
3071d 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 6f 66 20 74  * Close all of t
3071e 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  he cursors that 
3071f 77 65 72 65 20 6f 70 65 6e 65 64 20 62 79 20 73  were opened by s
30720 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
30721 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
30722 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d  , pLevel=pWInfo-
30723 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e  >a; i<pTabList->
30724 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65 76 65  nSrc; i++, pLeve
30725 6c 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74  l++){.    struct
30726 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
30727 54 61 62 49 74 65 6d 20 3d 20 26 70 54 61 62 4c  TabItem = &pTabL
30728 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69  ist->a[pLevel->i
30729 46 72 6f 6d 5d 3b 0a 20 20 20 20 54 61 62 6c 65  From];.    Table
3072a 20 2a 70 54 61 62 20 3d 20 70 54 61 62 49 74 65   *pTab = pTabIte
3072b 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 61 73 73  m->pTab;.    ass
3072c 65 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a  ert( pTab!=0 );.
3072d 20 20 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74      if( (pTab->t
3072e 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68  abFlags & TF_Eph
3072f 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c 20 70 54  emeral)!=0 || pT
30730 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 63 6f  ab->pSelect ) co
30731 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20  ntinue;.    if( 
30732 28 70 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c  (pWInfo->wctrlFl
30733 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4d 49 54  ags & WHERE_OMIT
30734 5f 43 4c 4f 53 45 29 3d 3d 30 20 29 7b 0a 20 20  _CLOSE)==0 ){.  
30735 20 20 20 20 69 66 28 20 21 70 57 49 6e 66 6f 2d      if( !pWInfo-
30736 3e 6f 6b 4f 6e 65 50 61 73 73 20 26 26 20 28 70  >okOnePass && (p
30737 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
30738 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f  ags & WHERE_IDX_
30739 4f 4e 4c 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ONLY)==0 ){.    
3073a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3073b 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
3073c 65 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75  e, pTabItem->iCu
3073d 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  rsor);.      }. 
3073e 20 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c       if( (pLevel
3073f 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
30740 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 29 21   WHERE_INDEXED)!
30741 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =0 ){.        sq
30742 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
30743 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 4c 65  v, OP_Close, pLe
30744 76 65 6c 2d 3e 69 49 64 78 43 75 72 29 3b 0a 20  vel->iIdxCur);. 
30745 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
30746 20 20 2f 2a 20 49 66 20 74 68 69 73 20 73 63 61    /* If this sca
30747 6e 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78 2c  n uses an index,
30748 20 6d 61 6b 65 20 63 6f 64 65 20 73 75 62 73 74   make code subst
30749 69 74 75 74 69 6f 6e 73 20 74 6f 20 72 65 61 64  itutions to read
3074a 20 64 61 74 61 0a 20 20 20 20 2a 2a 20 66 72 6f   data.    ** fro
3074b 6d 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 70  m the index in p
3074c 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
3074d 20 74 61 62 6c 65 2e 20 53 6f 6d 65 74 69 6d 65   table. Sometime
3074e 73 2c 20 74 68 69 73 20 6d 65 61 6e 73 0a 20 20  s, this means.  
3074f 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e    ** the table n
30750 65 65 64 20 6e 65 76 65 72 20 62 65 20 72 65 61  eed never be rea
30751 64 20 66 72 6f 6d 2e 20 54 68 69 73 20 69 73 20  d from. This is 
30752 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 6f  a performance bo
30753 6f 73 74 2c 0a 20 20 20 20 2a 2a 20 61 73 20 74  ost,.    ** as t
30754 68 65 20 76 64 62 65 20 6c 65 76 65 6c 20 77 61  he vdbe level wa
30755 69 74 73 20 75 6e 74 69 6c 20 74 68 65 20 74 61  its until the ta
30756 62 6c 65 20 69 73 20 72 65 61 64 20 62 65 66 6f  ble is read befo
30757 72 65 20 61 63 74 75 61 6c 6c 79 0a 20 20 20 20  re actually.    
30758 2a 2a 20 73 65 65 6b 69 6e 67 20 74 68 65 20 74  ** seeking the t
30759 61 62 6c 65 20 63 75 72 73 6f 72 20 74 6f 20 74  able cursor to t
3075a 68 65 20 72 65 63 6f 72 64 20 63 6f 72 72 65 73  he record corres
3075b 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 63  ponding to the c
3075c 75 72 72 65 6e 74 0a 20 20 20 20 2a 2a 20 70 6f  urrent.    ** po
3075d 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 69 6e  sition in the in
3075e 64 65 78 2e 0a 20 20 20 20 2a 2a 20 0a 20 20 20  dex..    ** .   
3075f 20 2a 2a 20 43 61 6c 6c 73 20 74 6f 20 74 68 65   ** Calls to the
30760 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
30761 69 6e 20 62 65 74 77 65 65 6e 20 73 71 6c 69 74  in between sqlit
30762 65 33 57 68 65 72 65 42 65 67 69 6e 20 61 6e 64  e3WhereBegin and
30763 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 57  .    ** sqlite3W
30764 68 65 72 65 45 6e 64 20 77 69 6c 6c 20 68 61 76  hereEnd will hav
30765 65 20 63 72 65 61 74 65 64 20 63 6f 64 65 20 74  e created code t
30766 68 61 74 20 72 65 66 65 72 65 6e 63 65 73 20 74  hat references t
30767 68 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20  he table.    ** 
30768 64 69 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20  directly.  This 
30769 6c 6f 6f 70 20 73 63 61 6e 73 20 61 6c 6c 20 74  loop scans all t
3076a 68 61 74 20 63 6f 64 65 20 6c 6f 6f 6b 69 6e 67  hat code looking
3076b 20 66 6f 72 20 6f 70 63 6f 64 65 73 0a 20 20 20   for opcodes.   
3076c 20 2a 2a 20 74 68 61 74 20 72 65 66 65 72 65 6e   ** that referen
3076d 63 65 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  ce the table and
3076e 20 63 6f 6e 76 65 72 74 73 20 74 68 65 6d 20 69   converts them i
3076f 6e 74 6f 20 6f 70 63 6f 64 65 73 20 74 68 61 74  nto opcodes that
30770 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63  .    ** referenc
30771 65 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20  e the index..   
30772 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 70 4c 65   */.    if( (pLe
30773 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
30774 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45  s & WHERE_INDEXE
30775 44 29 21 3d 30 20 26 26 20 21 64 62 2d 3e 6d 61  D)!=0 && !db->ma
30776 6c 6c 6f 63 46 61 69 6c 65 64 29 7b 0a 20 20 20  llocFailed){.   
30777 20 20 20 69 6e 74 20 6b 2c 20 6a 2c 20 6c 61 73     int k, j, las
30778 74 3b 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20  t;.      VdbeOp 
30779 2a 70 4f 70 3b 0a 20 20 20 20 20 20 49 6e 64 65  *pOp;.      Inde
3077a 78 20 2a 70 49 64 78 20 3d 20 70 4c 65 76 65 6c  x *pIdx = pLevel
3077b 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20  ->plan.u.pIdx;. 
3077c 20 20 20 20 20 69 6e 74 20 75 73 65 49 6e 64 65       int useInde
3077d 78 4f 6e 6c 79 20 3d 20 70 4c 65 76 65 6c 2d 3e  xOnly = pLevel->
3077e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
3077f 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 0a  HERE_IDX_ONLY;..
30780 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
30781 64 78 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  dx!=0 );.      p
30782 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  Op = sqlite3Vdbe
30783 47 65 74 4f 70 28 76 2c 20 70 57 49 6e 66 6f 2d  GetOp(v, pWInfo-
30784 3e 69 54 6f 70 29 3b 0a 20 20 20 20 20 20 6c 61  >iTop);.      la
30785 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  st = sqlite3Vdbe
30786 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
30787 20 20 20 20 20 20 66 6f 72 28 6b 3d 70 57 49 6e        for(k=pWIn
30788 66 6f 2d 3e 69 54 6f 70 3b 20 6b 3c 6c 61 73 74  fo->iTop; k<last
30789 3b 20 6b 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20  ; k++, pOp++){. 
3078a 20 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e         if( pOp->
3078b 70 31 21 3d 70 4c 65 76 65 6c 2d 3e 69 54 61 62  p1!=pLevel->iTab
3078c 43 75 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  Cur ) continue;.
3078d 20 20 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d          if( pOp-
3078e 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 75  >opcode==OP_Colu
3078f 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  mn ){.          
30790 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d  for(j=0; j<pIdx-
30791 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a  >nColumn; j++){.
30792 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
30793 70 4f 70 2d 3e 70 32 3d 3d 70 49 64 78 2d 3e 61  pOp->p2==pIdx->a
30794 69 43 6f 6c 75 6d 6e 5b 6a 5d 20 29 7b 0a 20 20  iColumn[j] ){.  
30795 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d              pOp-
30796 3e 70 32 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20  >p2 = j;.       
30797 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d         pOp->p1 =
30798 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
30799 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
3079a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
3079b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
3079c 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
3079d 74 28 21 75 73 65 49 6e 64 65 78 4f 6e 6c 79 20  t(!useIndexOnly 
3079e 7c 7c 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  || j<pIdx->nColu
3079f 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  mn);.        }el
307a0 73 65 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  se if( pOp->opco
307a1 64 65 3d 3d 4f 50 5f 52 6f 77 69 64 20 29 7b 0a  de==OP_Rowid ){.
307a2 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70            pOp->p
307a3 31 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  1 = pLevel->iIdx
307a4 43 75 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70  Cur;.          p
307a5 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f  Op->opcode = OP_
307a6 49 64 78 52 6f 77 69 64 3b 0a 20 20 20 20 20 20  IdxRowid;.      
307a7 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d    }else if( pOp-
307a8 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 75 6c 6c  >opcode==OP_Null
307a9 52 6f 77 20 26 26 20 75 73 65 49 6e 64 65 78 4f  Row && useIndexO
307aa 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20  nly ){.         
307ab 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f   pOp->opcode = O
307ac 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 20 20  P_Noop;.        
307ad 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
307ae 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 20    }..  /* Final 
307af 63 6c 65 61 6e 75 70 0a 20 20 2a 2f 0a 20 20 77  cleanup.  */.  w
307b0 68 65 72 65 49 6e 66 6f 46 72 65 65 28 64 62 2c  hereInfoFree(db,
307b1 20 70 57 49 6e 66 6f 29 3b 0a 20 20 72 65 74 75   pWInfo);.  retu
307b2 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  rn;.}../********
307b3 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 77 68  ****** End of wh
307b4 65 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ere.c **********
307b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
307b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
307b7 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
307b8 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
307b9 65 20 70 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a  e parse.c ******
307ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
307bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
307bc 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 72 69 76 65 72  *****/./* Driver
307bd 20 74 65 6d 70 6c 61 74 65 20 66 6f 72 20 74 68   template for th
307be 65 20 4c 45 4d 4f 4e 20 70 61 72 73 65 72 20 67  e LEMON parser g
307bf 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 65  enerator..** The
307c0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
307c1 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
307c2 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
307c3 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  .**.** This vers
307c4 69 6f 6e 20 6f 66 20 22 6c 65 6d 70 61 72 2e 63  ion of "lempar.c
307c5 22 20 69 73 20 6d 6f 64 69 66 69 65 64 2c 20 73  " is modified, s
307c6 6c 69 67 68 74 6c 79 2c 20 66 6f 72 20 75 73 65  lightly, for use
307c7 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 54   by SQLite..** T
307c8 68 65 20 6f 6e 6c 79 20 6d 6f 64 69 66 69 63 61  he only modifica
307c9 74 69 6f 6e 73 20 61 72 65 20 74 68 65 20 61 64  tions are the ad
307ca 64 69 74 69 6f 6e 20 6f 66 20 61 20 63 6f 75 70  dition of a coup
307cb 6c 65 20 6f 66 20 4e 45 56 45 52 28 29 0a 2a 2a  le of NEVER().**
307cc 20 6d 61 63 72 6f 73 20 74 6f 20 64 69 73 61 62   macros to disab
307cd 6c 65 20 74 65 73 74 73 20 74 68 61 74 20 61 72  le tests that ar
307ce 65 20 6e 65 65 64 65 64 20 69 6e 20 74 68 65 20  e needed in the 
307cf 63 61 73 65 20 6f 66 20 61 20 67 65 6e 65 72 61  case of a genera
307d0 6c 0a 2a 2a 20 4c 41 4c 52 28 31 29 20 67 72 61  l.** LALR(1) gra
307d1 6d 6d 61 72 20 62 75 74 20 77 68 69 63 68 20 61  mmar but which a
307d2 72 65 20 61 6c 77 61 79 73 20 66 61 6c 73 65 20  re always false 
307d3 69 6e 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66  in the.** specif
307d4 69 63 20 67 72 61 6d 6d 61 72 20 75 73 65 64 20  ic grammar used 
307d5 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 2f 2a  by SQLite..*/./*
307d6 20 46 69 72 73 74 20 6f 66 66 2c 20 63 6f 64 65   First off, code
307d7 20 69 73 20 69 6e 63 6c 75 64 65 64 20 74 68 61   is included tha
307d8 74 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 22 69  t follows the "i
307d9 6e 63 6c 75 64 65 22 20 64 65 63 6c 61 72 61 74  nclude" declarat
307da 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 69 6e  ion.** in the in
307db 70 75 74 20 67 72 61 6d 6d 61 72 20 66 69 6c 65  put grammar file
307dc 2e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 44 69 73  . */.../*.** Dis
307dd 61 62 6c 65 20 61 6c 6c 20 65 72 72 6f 72 20 72  able all error r
307de 65 63 6f 76 65 72 79 20 70 72 6f 63 65 73 73 69  ecovery processi
307df 6e 67 20 69 6e 20 74 68 65 20 70 61 72 73 65 72  ng in the parser
307e0 20 70 75 73 68 2d 64 6f 77 6e 0a 2a 2a 20 61 75   push-down.** au
307e1 74 6f 6d 61 74 6f 6e 2e 0a 2a 2f 0a 23 64 65 66  tomaton..*/.#def
307e2 69 6e 65 20 59 59 4e 4f 45 52 52 4f 52 52 45 43  ine YYNOERRORREC
307e3 4f 56 45 52 59 20 31 0a 0a 2f 2a 0a 2a 2a 20 4d  OVERY 1../*.** M
307e4 61 6b 65 20 79 79 74 65 73 74 63 61 73 65 28 29  ake yytestcase()
307e5 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 65 73   the same as tes
307e6 74 63 61 73 65 28 29 0a 2a 2f 0a 23 64 65 66 69  tcase().*/.#defi
307e7 6e 65 20 79 79 74 65 73 74 63 61 73 65 28 58 29  ne yytestcase(X)
307e8 20 74 65 73 74 63 61 73 65 28 58 29 0a 0a 2f 2a   testcase(X)../*
307e9 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
307ea 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
307eb 65 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74  e holds informat
307ec 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a  ion about the.**
307ed 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 6f 66   LIMIT clause of
307ee 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
307ef 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c  ent..*/.struct L
307f0 69 6d 69 74 56 61 6c 20 7b 0a 20 20 45 78 70 72  imitVal {.  Expr
307f1 20 2a 70 4c 69 6d 69 74 3b 20 20 20 20 2f 2a 20   *pLimit;    /* 
307f2 54 68 65 20 4c 49 4d 49 54 20 65 78 70 72 65 73  The LIMIT expres
307f3 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66 20 74  sion.  NULL if t
307f4 68 65 72 65 20 69 73 20 6e 6f 20 6c 69 6d 69 74  here is no limit
307f5 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66   */.  Expr *pOff
307f6 73 65 74 3b 20 20 20 2f 2a 20 54 68 65 20 4f 46  set;   /* The OF
307f7 46 53 45 54 20 65 78 70 72 65 73 73 69 6f 6e 2e  FSET expression.
307f8 20 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20    NULL if there 
307f9 69 73 20 6e 6f 6e 65 20 2a 2f 0a 7d 3b 0a 0a 2f  is none */.};../
307fa 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
307fb 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
307fc 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  re is used to st
307fd 6f 72 65 20 74 68 65 20 4c 49 4b 45 2c 0a 2a 2a  ore the LIKE,.**
307fe 20 47 4c 4f 42 2c 20 4e 4f 54 20 4c 49 4b 45 2c   GLOB, NOT LIKE,
307ff 20 61 6e 64 20 4e 4f 54 20 47 4c 4f 42 20 6f 70   and NOT GLOB op
30800 65 72 61 74 6f 72 73 2e 0a 2a 2f 0a 73 74 72 75  erators..*/.stru
30801 63 74 20 4c 69 6b 65 4f 70 20 7b 0a 20 20 54 6f  ct LikeOp {.  To
30802 6b 65 6e 20 65 4f 70 65 72 61 74 6f 72 3b 20 20  ken eOperator;  
30803 2f 2a 20 22 6c 69 6b 65 22 20 6f 72 20 22 67 6c  /* "like" or "gl
30804 6f 62 22 20 6f 72 20 22 72 65 67 65 78 70 22 20  ob" or "regexp" 
30805 2a 2f 0a 20 20 69 6e 74 20 6e 6f 74 3b 20 20 20  */.  int not;   
30806 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
30807 20 74 68 65 20 4e 4f 54 20 6b 65 79 77 6f 72 64   the NOT keyword
30808 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 7d   is present */.}
30809 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
3080a 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
3080b 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
3080c 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 76  describes the ev
3080d 65 6e 74 20 6f 66 20 61 0a 2a 2a 20 54 52 49 47  ent of a.** TRIG
3080e 47 45 52 2e 20 20 22 61 22 20 69 73 20 74 68 65  GER.  "a" is the
3080f 20 65 76 65 6e 74 20 74 79 70 65 2c 20 6f 6e 65   event type, one
30810 20 6f 66 20 54 4b 5f 55 50 44 41 54 45 2c 20 54   of TK_UPDATE, T
30811 4b 5f 49 4e 53 45 52 54 2c 0a 2a 2a 20 54 4b 5f  K_INSERT,.** TK_
30812 44 45 4c 45 54 45 2c 20 6f 72 20 54 4b 5f 49 4e  DELETE, or TK_IN
30813 53 54 45 41 44 2e 20 20 49 66 20 74 68 65 20 65  STEAD.  If the e
30814 76 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66  vent is of the f
30815 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 55  orm.**.**      U
30816 50 44 41 54 45 20 4f 4e 20 28 61 2c 62 2c 63 29  PDATE ON (a,b,c)
30817 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20  .**.** Then the 
30818 22 62 22 20 49 64 4c 69 73 74 20 72 65 63 6f 72  "b" IdList recor
30819 64 73 20 74 68 65 20 6c 69 73 74 20 22 61 2c 62  ds the list "a,b
3081a 2c 63 22 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54  ,c"..*/.struct T
3081b 72 69 67 45 76 65 6e 74 20 7b 20 69 6e 74 20 61  rigEvent { int a
3081c 3b 20 49 64 4c 69 73 74 20 2a 20 62 3b 20 7d 3b  ; IdList * b; };
3081d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
3081e 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
3081f 63 74 75 72 65 20 68 6f 6c 64 73 20 74 68 65 20  cture holds the 
30820 41 54 54 41 43 48 20 6b 65 79 20 61 6e 64 20 74  ATTACH key and t
30821 68 65 20 6b 65 79 20 74 79 70 65 2e 0a 2a 2f 0a  he key type..*/.
30822 73 74 72 75 63 74 20 41 74 74 61 63 68 4b 65 79  struct AttachKey
30823 20 7b 20 69 6e 74 20 74 79 70 65 3b 20 20 54 6f   { int type;  To
30824 6b 65 6e 20 6b 65 79 3b 20 7d 3b 0a 0a 0a 20 20  ken key; };...  
30825 2f 2a 20 54 68 69 73 20 69 73 20 61 20 75 74 69  /* This is a uti
30826 6c 69 74 79 20 72 6f 75 74 69 6e 65 20 75 73 65  lity routine use
30827 64 20 74 6f 20 73 65 74 20 74 68 65 20 45 78 70  d to set the Exp
30828 72 53 70 61 6e 2e 7a 53 74 61 72 74 20 61 6e 64  rSpan.zStart and
30829 0a 20 20 2a 2a 20 45 78 70 72 53 70 61 6e 2e 7a  .  ** ExprSpan.z
3082a 45 6e 64 20 76 61 6c 75 65 73 20 6f 66 20 70 4f  End values of pO
3082b 75 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 73  ut so that the s
3082c 70 61 6e 20 63 6f 76 65 72 73 20 74 68 65 20 63  pan covers the c
3082d 6f 6d 70 6c 65 74 65 0a 20 20 2a 2a 20 72 61 6e  omplete.  ** ran
3082e 67 65 20 6f 66 20 74 65 78 74 20 62 65 67 69 6e  ge of text begin
3082f 6e 69 6e 67 20 77 69 74 68 20 70 53 74 61 72 74  ning with pStart
30830 20 61 6e 64 20 67 6f 69 6e 67 20 74 6f 20 74 68   and going to th
30831 65 20 65 6e 64 20 6f 66 20 70 45 6e 64 2e 0a 20  e end of pEnd.. 
30832 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69   */.  static voi
30833 64 20 73 70 61 6e 53 65 74 28 45 78 70 72 53 70  d spanSet(ExprSp
30834 61 6e 20 2a 70 4f 75 74 2c 20 54 6f 6b 65 6e 20  an *pOut, Token 
30835 2a 70 53 74 61 72 74 2c 20 54 6f 6b 65 6e 20 2a  *pStart, Token *
30836 70 45 6e 64 29 7b 0a 20 20 20 20 70 4f 75 74 2d  pEnd){.    pOut-
30837 3e 7a 53 74 61 72 74 20 3d 20 70 53 74 61 72 74  >zStart = pStart
30838 2d 3e 7a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a  ->z;.    pOut->z
30839 45 6e 64 20 3d 20 26 70 45 6e 64 2d 3e 7a 5b 70  End = &pEnd->z[p
3083a 45 6e 64 2d 3e 6e 5d 3b 0a 20 20 7d 0a 0a 20 20  End->n];.  }..  
3083b 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 6e  /* Construct a n
3083c 65 77 20 45 78 70 72 20 6f 62 6a 65 63 74 20 66  ew Expr object f
3083d 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 69 64 65  rom a single ide
3083e 6e 74 69 66 69 65 72 2e 20 20 55 73 65 20 74 68  ntifier.  Use th
3083f 65 0a 20 20 2a 2a 20 6e 65 77 20 45 78 70 72 20  e.  ** new Expr 
30840 74 6f 20 70 6f 70 75 6c 61 74 65 20 70 4f 75 74  to populate pOut
30841 2e 20 20 53 65 74 20 74 68 65 20 73 70 61 6e 20  .  Set the span 
30842 6f 66 20 70 4f 75 74 20 74 6f 20 62 65 20 74 68  of pOut to be th
30843 65 20 69 64 65 6e 74 69 66 69 65 72 0a 20 20 2a  e identifier.  *
30844 2a 20 74 68 61 74 20 63 72 65 61 74 65 64 20 74  * that created t
30845 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20  he expression.. 
30846 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69   */.  static voi
30847 64 20 73 70 61 6e 45 78 70 72 28 45 78 70 72 53  d spanExpr(ExprS
30848 70 61 6e 20 2a 70 4f 75 74 2c 20 50 61 72 73 65  pan *pOut, Parse
30849 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 70   *pParse, int op
3084a 2c 20 54 6f 6b 65 6e 20 2a 70 56 61 6c 75 65 29  , Token *pValue)
3084b 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 70 45 78 70  {.    pOut->pExp
3084c 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
3084d 28 70 50 61 72 73 65 2c 20 6f 70 2c 20 30 2c 20  (pParse, op, 0, 
3084e 30 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20  0, pValue);.    
3084f 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20 70  pOut->zStart = p
30850 56 61 6c 75 65 2d 3e 7a 3b 0a 20 20 20 20 70 4f  Value->z;.    pO
30851 75 74 2d 3e 7a 45 6e 64 20 3d 20 26 70 56 61 6c  ut->zEnd = &pVal
30852 75 65 2d 3e 7a 5b 70 56 61 6c 75 65 2d 3e 6e 5d  ue->z[pValue->n]
30853 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73  ;.  }..  /* This
30854 20 72 6f 75 74 69 6e 65 20 63 6f 6e 73 74 72 75   routine constru
30855 63 74 73 20 61 20 62 69 6e 61 72 79 20 65 78 70  cts a binary exp
30856 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 6f 75 74  ression node out
30857 20 6f 66 20 74 77 6f 20 45 78 70 72 53 70 61 6e   of two ExprSpan
30858 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 73 20 61 6e  .  ** objects an
30859 64 20 75 73 65 73 20 74 68 65 20 72 65 73 75 6c  d uses the resul
3085a 74 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 61 20  t to populate a 
3085b 6e 65 77 20 45 78 70 72 53 70 61 6e 20 6f 62 6a  new ExprSpan obj
3085c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74  ect..  */.  stat
3085d 69 63 20 76 6f 69 64 20 73 70 61 6e 42 69 6e 61  ic void spanBina
3085e 72 79 45 78 70 72 28 0a 20 20 20 20 45 78 70 72  ryExpr(.    Expr
3085f 53 70 61 6e 20 2a 70 4f 75 74 2c 20 20 20 20 20  Span *pOut,     
30860 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73  /* Write the res
30861 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 20 20  ult here */.    
30862 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
30863 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69      /* The parsi
30864 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 45 72 72  ng context.  Err
30865 6f 72 73 20 61 63 63 75 6d 75 6c 61 74 65 20 68  ors accumulate h
30866 65 72 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f  ere */.    int o
30867 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p,             /
30868 2a 20 54 68 65 20 62 69 6e 61 72 79 20 6f 70 65  * The binary ope
30869 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 45 78  ration */.    Ex
3086a 70 72 53 70 61 6e 20 2a 70 4c 65 66 74 2c 20 20  prSpan *pLeft,  
3086b 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70    /* The left op
3086c 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 45 78 70  erand */.    Exp
3086d 72 53 70 61 6e 20 2a 70 52 69 67 68 74 20 20 20  rSpan *pRight   
3086e 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70   /* The right op
3086f 65 72 61 6e 64 20 2a 2f 0a 20 20 29 7b 0a 20 20  erand */.  ){.  
30870 20 20 70 4f 75 74 2d 3e 70 45 78 70 72 20 3d 20    pOut->pExpr = 
30871 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
30872 72 73 65 2c 20 6f 70 2c 20 70 4c 65 66 74 2d 3e  rse, op, pLeft->
30873 70 45 78 70 72 2c 20 70 52 69 67 68 74 2d 3e 70  pExpr, pRight->p
30874 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 70 4f  Expr, 0);.    pO
30875 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20 70 4c 65  ut->zStart = pLe
30876 66 74 2d 3e 7a 53 74 61 72 74 3b 0a 20 20 20 20  ft->zStart;.    
30877 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 70 52 69  pOut->zEnd = pRi
30878 67 68 74 2d 3e 7a 45 6e 64 3b 0a 20 20 7d 0a 0a  ght->zEnd;.  }..
30879 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 61    /* Construct a
3087a 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  n expression nod
3087b 65 20 66 6f 72 20 61 20 75 6e 61 72 79 20 70 6f  e for a unary po
3087c 73 74 66 69 78 20 6f 70 65 72 61 74 6f 72 0a 20  stfix operator. 
3087d 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69   */.  static voi
3087e 64 20 73 70 61 6e 55 6e 61 72 79 50 6f 73 74 66  d spanUnaryPostf
3087f 69 78 28 0a 20 20 20 20 45 78 70 72 53 70 61 6e  ix(.    ExprSpan
30880 20 2a 70 4f 75 74 2c 20 20 20 20 20 20 20 20 2f   *pOut,        /
30881 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20  * Write the new 
30882 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
30883 68 65 72 65 20 2a 2f 0a 20 20 20 20 50 61 72 73  here */.    Pars
30884 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
30885 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
30886 6e 74 65 78 74 20 74 6f 20 72 65 63 6f 72 64 20  ntext to record 
30887 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 69 6e  errors */.    in
30888 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
30889 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 65 72       /* The oper
3088a 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72  ator */.    Expr
3088b 53 70 61 6e 20 2a 70 4f 70 65 72 61 6e 64 2c 20  Span *pOperand, 
3088c 20 20 20 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e     /* The operan
3088d 64 20 2a 2f 0a 20 20 20 20 54 6f 6b 65 6e 20 2a  d */.    Token *
3088e 70 50 6f 73 74 4f 70 20 20 20 20 20 20 20 20 20  pPostOp         
3088f 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e 64 20 74  /* The operand t
30890 6f 6b 65 6e 20 66 6f 72 20 73 65 74 74 69 6e 67  oken for setting
30891 20 74 68 65 20 73 70 61 6e 20 2a 2f 0a 20 20 29   the span */.  )
30892 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 70 45 78 70  {.    pOut->pExp
30893 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
30894 28 70 50 61 72 73 65 2c 20 6f 70 2c 20 70 4f 70  (pParse, op, pOp
30895 65 72 61 6e 64 2d 3e 70 45 78 70 72 2c 20 30 2c  erand->pExpr, 0,
30896 20 30 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a   0);.    pOut->z
30897 53 74 61 72 74 20 3d 20 70 4f 70 65 72 61 6e 64  Start = pOperand
30898 2d 3e 7a 53 74 61 72 74 3b 0a 20 20 20 20 70 4f  ->zStart;.    pO
30899 75 74 2d 3e 7a 45 6e 64 20 3d 20 26 70 50 6f 73  ut->zEnd = &pPos
3089a 74 4f 70 2d 3e 7a 5b 70 50 6f 73 74 4f 70 2d 3e  tOp->z[pPostOp->
3089b 6e 5d 3b 0a 20 20 7d 20 20 20 20 20 20 20 20 20  n];.  }         
3089c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3089d 20 20 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75    ..  /* Constru
3089e 63 74 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ct an expression
3089f 20 6e 6f 64 65 20 66 6f 72 20 61 20 75 6e 61 72   node for a unar
308a0 79 20 70 72 65 66 69 78 20 6f 70 65 72 61 74 6f  y prefix operato
308a1 72 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  r.  */.  static 
308a2 76 6f 69 64 20 73 70 61 6e 55 6e 61 72 79 50 72  void spanUnaryPr
308a3 65 66 69 78 28 0a 20 20 20 20 45 78 70 72 53 70  efix(.    ExprSp
308a4 61 6e 20 2a 70 4f 75 74 2c 20 20 20 20 20 20 20  an *pOut,       
308a5 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65   /* Write the ne
308a6 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  w expression nod
308a7 65 20 68 65 72 65 20 2a 2f 0a 20 20 20 20 50 61  e here */.    Pa
308a8 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
308a9 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
308aa 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 63 6f 72  context to recor
308ab 64 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20  d errors */.    
308ac 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
308ad 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70         /* The op
308ae 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78  erator */.    Ex
308af 70 72 53 70 61 6e 20 2a 70 4f 70 65 72 61 6e 64  prSpan *pOperand
308b0 2c 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 65 72  ,    /* The oper
308b1 61 6e 64 20 2a 2f 0a 20 20 20 20 54 6f 6b 65 6e  and */.    Token
308b2 20 2a 70 50 72 65 4f 70 20 20 20 20 20 20 20 20   *pPreOp        
308b3 20 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e 64 20   /* The operand 
308b4 74 6f 6b 65 6e 20 66 6f 72 20 73 65 74 74 69 6e  token for settin
308b5 67 20 74 68 65 20 73 70 61 6e 20 2a 2f 0a 20 20  g the span */.  
308b6 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 70 45 78  ){.    pOut->pEx
308b7 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
308b8 72 28 70 50 61 72 73 65 2c 20 6f 70 2c 20 70 4f  r(pParse, op, pO
308b9 70 65 72 61 6e 64 2d 3e 70 45 78 70 72 2c 20 30  perand->pExpr, 0
308ba 2c 20 30 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  , 0);.    pOut->
308bb 7a 53 74 61 72 74 20 3d 20 70 50 72 65 4f 70 2d  zStart = pPreOp-
308bc 3e 7a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45  >z;.    pOut->zE
308bd 6e 64 20 3d 20 70 4f 70 65 72 61 6e 64 2d 3e 7a  nd = pOperand->z
308be 45 6e 64 3b 0a 20 20 7d 0a 2f 2a 20 4e 65 78 74  End;.  }./* Next
308bf 20 69 73 20 61 6c 6c 20 74 6f 6b 65 6e 20 76 61   is all token va
308c0 6c 75 65 73 2c 20 69 6e 20 61 20 66 6f 72 6d 20  lues, in a form 
308c1 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65  suitable for use
308c2 20 62 79 20 6d 61 6b 65 68 65 61 64 65 72 73 2e   by makeheaders.
308c3 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e  .** This section
308c4 20 77 69 6c 6c 20 62 65 20 6e 75 6c 6c 20 75 6e   will be null un
308c5 6c 65 73 73 20 6c 65 6d 6f 6e 20 69 73 20 72 75  less lemon is ru
308c6 6e 20 77 69 74 68 20 74 68 65 20 2d 6d 20 73 77  n with the -m sw
308c7 69 74 63 68 2e 0a 2a 2f 0a 2f 2a 20 0a 2a 2a 20  itch..*/./* .** 
308c8 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
308c9 28 61 6c 6c 20 67 65 6e 65 72 61 74 65 64 20 61  (all generated a
308ca 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20  utomatically by 
308cb 74 68 65 20 70 61 72 73 65 72 20 67 65 6e 65 72  the parser gener
308cc 61 74 6f 72 29 0a 2a 2a 20 73 70 65 63 69 66 79  ator).** specify
308cd 20 74 68 65 20 76 61 72 69 6f 75 73 20 6b 69 6e   the various kin
308ce 64 73 20 6f 66 20 74 6f 6b 65 6e 73 20 28 74 65  ds of tokens (te
308cf 72 6d 69 6e 61 6c 73 29 20 74 68 61 74 20 74 68  rminals) that th
308d0 65 20 70 61 72 73 65 72 0a 2a 2a 20 75 6e 64 65  e parser.** unde
308d1 72 73 74 61 6e 64 73 2e 20 0a 2a 2a 0a 2a 2a 20  rstands. .**.** 
308d2 45 61 63 68 20 73 79 6d 62 6f 6c 20 68 65 72 65  Each symbol here
308d3 20 69 73 20 61 20 74 65 72 6d 69 6e 61 6c 20 73   is a terminal s
308d4 79 6d 62 6f 6c 20 69 6e 20 74 68 65 20 67 72 61  ymbol in the gra
308d5 6d 6d 61 72 2e 0a 2a 2f 0a 2f 2a 20 4d 61 6b 65  mmar..*/./* Make
308d6 20 73 75 72 65 20 74 68 65 20 49 4e 54 45 52 46   sure the INTERF
308d7 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64 65 66  ACE macro is def
308d8 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ined..*/.#ifndef
308d9 20 49 4e 54 45 52 46 41 43 45 0a 23 20 64 65 66   INTERFACE.# def
308da 69 6e 65 20 49 4e 54 45 52 46 41 43 45 20 31 0a  ine INTERFACE 1.
308db 23 65 6e 64 69 66 0a 2f 2a 20 54 68 65 20 6e 65  #endif./* The ne
308dc 78 74 20 74 68 69 6e 67 20 69 6e 63 6c 75 64 65  xt thing include
308dd 64 20 69 73 20 73 65 72 69 65 73 20 6f 66 20 64  d is series of d
308de 65 66 69 6e 65 73 20 77 68 69 63 68 20 63 6f 6e  efines which con
308df 74 72 6f 6c 0a 2a 2a 20 76 61 72 69 6f 75 73 20  trol.** various 
308e0 61 73 70 65 63 74 73 20 6f 66 20 74 68 65 20 67  aspects of the g
308e1 65 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 2e  enerated parser.
308e2 0a 2a 2a 20 20 20 20 59 59 43 4f 44 45 54 59 50  .**    YYCODETYP
308e3 45 20 20 20 20 20 20 20 20 20 69 73 20 74 68 65  E         is the
308e4 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64 20   data type used 
308e5 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 72 6d  for storing term
308e6 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  inal.**         
308e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e                an
308e8 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75  d nonterminal nu
308e9 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67 6e 65  mbers.  "unsigne
308ea 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20 20 20  d char" is.**   
308eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
308ec 20 20 20 20 75 73 65 64 20 69 66 20 74 68 65 72      used if ther
308ed 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61 6e  e are fewer than
308ee 20 32 35 30 20 74 65 72 6d 69 6e 61 6c 73 0a 2a   250 terminals.*
308ef 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
308f0 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74          and nont
308f1 65 72 6d 69 6e 61 6c 73 2e 20 20 22 69 6e 74 22  erminals.  "int"
308f2 20 69 73 20 75 73 65 64 20 6f 74 68 65 72 77 69   is used otherwi
308f3 73 65 2e 0a 2a 2a 20 20 20 20 59 59 4e 4f 43 4f  se..**    YYNOCO
308f4 44 45 20 20 20 20 20 20 20 20 20 20 20 69 73 20  DE           is 
308f5 61 20 6e 75 6d 62 65 72 20 6f 66 20 74 79 70 65  a number of type
308f6 20 59 59 43 4f 44 45 54 59 50 45 20 77 68 69 63   YYCODETYPE whic
308f7 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a  h corresponds.**
308f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
308f9 20 20 20 20 20 20 20 74 6f 20 6e 6f 20 6c 65 67         to no leg
308fa 61 6c 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e  al terminal or n
308fb 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62 65  onterminal numbe
308fc 72 2e 20 20 54 68 69 73 0a 2a 2a 20 20 20 20 20  r.  This.**     
308fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
308fe 20 20 6e 75 6d 62 65 72 20 69 73 20 75 73 65 64    number is used
308ff 20 74 6f 20 66 69 6c 6c 20 69 6e 20 65 6d 70 74   to fill in empt
30900 79 20 73 6c 6f 74 73 20 6f 66 20 74 68 65 20 68  y slots of the h
30901 61 73 68 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ash .**         
30902 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 61                ta
30903 62 6c 65 2e 0a 2a 2a 20 20 20 20 59 59 46 41 4c  ble..**    YYFAL
30904 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20 49 66  LBACK         If
30905 20 64 65 66 69 6e 65 64 2c 20 74 68 69 73 20 69   defined, this i
30906 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 6f 6e  ndicates that on
30907 65 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73  e or more tokens
30908 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
30909 20 20 20 20 20 20 20 20 20 20 68 61 76 65 20 66            have f
3090a 61 6c 6c 2d 62 61 63 6b 20 76 61 6c 75 65 73 20  all-back values 
3090b 77 68 69 63 68 20 73 68 6f 75 6c 64 20 62 65 20  which should be 
3090c 75 73 65 64 20 69 66 20 74 68 65 0a 2a 2a 20 20  used if the.**  
3090d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3090e 20 20 20 20 20 6f 72 69 67 69 6e 61 6c 20 76 61       original va
3090f 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  lue of the token
30910 20 77 69 6c 6c 20 6e 6f 74 20 70 61 72 73 65 2e   will not parse.
30911 0a 2a 2a 20 20 20 20 59 59 41 43 54 49 4f 4e 54  .**    YYACTIONT
30912 59 50 45 20 20 20 20 20 20 20 69 73 20 74 68 65  YPE       is the
30913 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64 20   data type used 
30914 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 72 6d  for storing term
30915 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  inal.**         
30916 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e                an
30917 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75  d nonterminal nu
30918 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67 6e 65  mbers.  "unsigne
30919 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20 20 20  d char" is.**   
3091a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3091b 20 20 20 20 75 73 65 64 20 69 66 20 74 68 65 72      used if ther
3091c 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61 6e  e are fewer than
3091d 20 32 35 30 20 72 75 6c 65 73 20 61 6e 64 0a 2a   250 rules and.*
3091e 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3091f 20 20 20 20 20 20 20 20 73 74 61 74 65 73 20 63          states c
30920 6f 6d 62 69 6e 65 64 2e 20 20 22 69 6e 74 22 20  ombined.  "int" 
30921 69 73 20 75 73 65 64 20 6f 74 68 65 72 77 69 73  is used otherwis
30922 65 2e 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  e..**    sqlite3
30923 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20  ParserTOKENTYPE 
30924 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61 20      is the data 
30925 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 6d 69  type used for mi
30926 6e 6f 72 20 74 6f 6b 65 6e 73 20 67 69 76 65 6e  nor tokens given
30927 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
30928 20 20 20 20 20 20 20 20 20 20 20 64 69 72 65 63             direc
30929 74 6c 79 20 74 6f 20 74 68 65 20 70 61 72 73 65  tly to the parse
3092a 72 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65 6e  r from the token
3092b 69 7a 65 72 2e 0a 2a 2a 20 20 20 20 59 59 4d 49  izer..**    YYMI
3092c 4e 4f 52 54 59 50 45 20 20 20 20 20 20 20 20 69  NORTYPE        i
3092d 73 20 74 68 65 20 64 61 74 61 20 74 79 70 65 20  s the data type 
3092e 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 6d 69 6e  used for all min
3092f 6f 72 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 20 20 20  or tokens..**   
30930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30931 20 20 20 20 54 68 69 73 20 69 73 20 74 79 70 69      This is typi
30932 63 61 6c 6c 79 20 61 20 75 6e 69 6f 6e 20 6f 66  cally a union of
30933 20 6d 61 6e 79 20 74 79 70 65 73 2c 20 6f 6e 65   many types, one
30934 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
30935 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68 69               whi
30936 63 68 20 69 73 20 73 71 6c 69 74 65 33 50 61 72  ch is sqlite3Par
30937 73 65 72 54 4f 4b 45 4e 54 59 50 45 2e 20 20 54  serTOKENTYPE.  T
30938 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  he entry in the 
30939 75 6e 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  union.**        
3093a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
3093b 6f 72 20 62 61 73 65 20 74 6f 6b 65 6e 73 20 69  or base tokens i
3093c 73 20 63 61 6c 6c 65 64 20 22 79 79 30 22 2e 0a  s called "yy0"..
3093d 2a 2a 20 20 20 20 59 59 53 54 41 43 4b 44 45 50  **    YYSTACKDEP
3093e 54 48 20 20 20 20 20 20 20 69 73 20 74 68 65 20  TH       is the 
3093f 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66  maximum depth of
30940 20 74 68 65 20 70 61 72 73 65 72 27 73 20 73 74   the parser's st
30941 61 63 6b 2e 20 20 49 66 0a 2a 2a 20 20 20 20 20  ack.  If.**     
30942 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30943 20 20 7a 65 72 6f 20 74 68 65 20 73 74 61 63 6b    zero the stack
30944 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20   is dynamically 
30945 73 69 7a 65 64 20 75 73 69 6e 67 20 72 65 61 6c  sized using real
30946 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 73 71 6c 69  loc().**    sqli
30947 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 44 45  te3ParserARG_SDE
30948 43 4c 20 20 20 20 20 41 20 73 74 61 74 69 63 20  CL     A static 
30949 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61  variable declara
3094a 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 25 65 78  tion for the %ex
3094b 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  tra_argument.** 
3094c 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
3094d 41 52 47 5f 50 44 45 43 4c 20 20 20 20 20 41 20  ARG_PDECL     A 
3094e 70 61 72 61 6d 65 74 65 72 20 64 65 63 6c 61 72  parameter declar
3094f 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 25 65  ation for the %e
30950 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a 2a 2a  xtra_argument.**
30951 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
30952 72 41 52 47 5f 53 54 4f 52 45 20 20 20 20 20 43  rARG_STORE     C
30953 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 25 65 78  ode to store %ex
30954 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 69 6e 74  tra_argument int
30955 6f 20 79 79 70 50 61 72 73 65 72 0a 2a 2a 20 20  o yypParser.**  
30956 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
30957 52 47 5f 46 45 54 43 48 20 20 20 20 20 43 6f 64  RG_FETCH     Cod
30958 65 20 74 6f 20 65 78 74 72 61 63 74 20 25 65 78  e to extract %ex
30959 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 66 72 6f  tra_argument fro
3095a 6d 20 79 79 70 50 61 72 73 65 72 0a 2a 2a 20 20  m yypParser.**  
3095b 20 20 59 59 4e 53 54 41 54 45 20 20 20 20 20 20    YYNSTATE      
3095c 20 20 20 20 20 74 68 65 20 63 6f 6d 62 69 6e 65       the combine
3095d 64 20 6e 75 6d 62 65 72 20 6f 66 20 73 74 61 74  d number of stat
3095e 65 73 2e 0a 2a 2a 20 20 20 20 59 59 4e 52 55 4c  es..**    YYNRUL
3095f 45 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65  E            the
30960 20 6e 75 6d 62 65 72 20 6f 66 20 72 75 6c 65 73   number of rules
30961 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 0a   in the grammar.
30962 2a 2a 20 20 20 20 59 59 45 52 52 4f 52 53 59 4d  **    YYERRORSYM
30963 42 4f 4c 20 20 20 20 20 20 69 73 20 74 68 65 20  BOL      is the 
30964 63 6f 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  code number of t
30965 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2e  he error symbol.
30966 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 20 20 20 20    If not.**     
30967 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30968 20 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20    defined, then 
30969 64 6f 20 6e 6f 20 65 72 72 6f 72 20 70 72 6f 63  do no error proc
3096a 65 73 73 69 6e 67 2e 0a 2a 2f 0a 23 64 65 66 69  essing..*/.#defi
3096b 6e 65 20 59 59 43 4f 44 45 54 59 50 45 20 75 6e  ne YYCODETYPE un
3096c 73 69 67 6e 65 64 20 63 68 61 72 0a 23 64 65 66  signed char.#def
3096d 69 6e 65 20 59 59 4e 4f 43 4f 44 45 20 32 35 34  ine YYNOCODE 254
3096e 0a 23 64 65 66 69 6e 65 20 59 59 41 43 54 49 4f  .#define YYACTIO
3096f 4e 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73  NTYPE unsigned s
30970 68 6f 72 74 20 69 6e 74 0a 23 64 65 66 69 6e 65  hort int.#define
30971 20 59 59 57 49 4c 44 43 41 52 44 20 36 37 0a 23   YYWILDCARD 67.#
30972 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61  define sqlite3Pa
30973 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 54 6f  rserTOKENTYPE To
30974 6b 65 6e 0a 74 79 70 65 64 65 66 20 75 6e 69 6f  ken.typedef unio
30975 6e 20 7b 0a 20 20 69 6e 74 20 79 79 69 6e 69 74  n {.  int yyinit
30976 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  ;.  sqlite3Parse
30977 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 30 3b 0a  rTOKENTYPE yy0;.
30978 20 20 53 65 6c 65 63 74 2a 20 79 79 33 3b 0a 20    Select* yy3;. 
30979 20 45 78 70 72 4c 69 73 74 2a 20 79 79 31 34 3b   ExprList* yy14;
3097a 0a 20 20 53 72 63 4c 69 73 74 2a 20 79 79 36 35  .  SrcList* yy65
3097b 3b 0a 20 20 73 74 72 75 63 74 20 4c 69 6b 65 4f  ;.  struct LikeO
3097c 70 20 79 79 39 36 3b 0a 20 20 45 78 70 72 2a 20  p yy96;.  Expr* 
3097d 79 79 31 33 32 3b 0a 20 20 75 38 20 79 79 31 38  yy132;.  u8 yy18
3097e 36 3b 0a 20 20 69 6e 74 20 79 79 33 32 38 3b 0a  6;.  int yy328;.
3097f 20 20 45 78 70 72 53 70 61 6e 20 79 79 33 34 36    ExprSpan yy346
30980 3b 0a 20 20 73 74 72 75 63 74 20 54 72 69 67 45  ;.  struct TrigE
30981 76 65 6e 74 20 79 79 33 37 38 3b 0a 20 20 49 64  vent yy378;.  Id
30982 4c 69 73 74 2a 20 79 79 34 30 38 3b 0a 20 20 73  List* yy408;.  s
30983 74 72 75 63 74 20 7b 69 6e 74 20 76 61 6c 75 65  truct {int value
30984 3b 20 69 6e 74 20 6d 61 73 6b 3b 7d 20 79 79 34  ; int mask;} yy4
30985 32 39 3b 0a 20 20 54 72 69 67 67 65 72 53 74 65  29;.  TriggerSte
30986 70 2a 20 79 79 34 37 33 3b 0a 20 20 73 74 72 75  p* yy473;.  stru
30987 63 74 20 4c 69 6d 69 74 56 61 6c 20 79 79 34 37  ct LimitVal yy47
30988 36 3b 0a 7d 20 59 59 4d 49 4e 4f 52 54 59 50 45  6;.} YYMINORTYPE
30989 3b 0a 23 69 66 6e 64 65 66 20 59 59 53 54 41 43  ;.#ifndef YYSTAC
3098a 4b 44 45 50 54 48 0a 23 64 65 66 69 6e 65 20 59  KDEPTH.#define Y
3098b 59 53 54 41 43 4b 44 45 50 54 48 20 31 30 30 0a  YSTACKDEPTH 100.
3098c 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73  #endif.#define s
3098d 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
3098e 53 44 45 43 4c 20 50 61 72 73 65 20 2a 70 50 61  SDECL Parse *pPa
3098f 72 73 65 3b 0a 23 64 65 66 69 6e 65 20 73 71 6c  rse;.#define sql
30990 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 50 44  ite3ParserARG_PD
30991 45 43 4c 20 2c 50 61 72 73 65 20 2a 70 50 61 72  ECL ,Parse *pPar
30992 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  se.#define sqlit
30993 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43  e3ParserARG_FETC
30994 48 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20  H Parse *pParse 
30995 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50 61  = yypParser->pPa
30996 72 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  rse.#define sqli
30997 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f  te3ParserARG_STO
30998 52 45 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50  RE yypParser->pP
30999 61 72 73 65 20 3d 20 70 50 61 72 73 65 0a 23 64  arse = pParse.#d
3099a 65 66 69 6e 65 20 59 59 4e 53 54 41 54 45 20 36  efine YYNSTATE 6
3099b 32 39 0a 23 64 65 66 69 6e 65 20 59 59 4e 52 55  29.#define YYNRU
3099c 4c 45 20 33 32 39 0a 23 64 65 66 69 6e 65 20 59  LE 329.#define Y
3099d 59 46 41 4c 4c 42 41 43 4b 20 31 0a 23 64 65 66  YFALLBACK 1.#def
3099e 69 6e 65 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e  ine YY_NO_ACTION
3099f 20 20 20 20 20 20 28 59 59 4e 53 54 41 54 45 2b        (YYNSTATE+
309a0 59 59 4e 52 55 4c 45 2b 32 29 0a 23 64 65 66 69  YYNRULE+2).#defi
309a1 6e 65 20 59 59 5f 41 43 43 45 50 54 5f 41 43 54  ne YY_ACCEPT_ACT
309a2 49 4f 4e 20 20 28 59 59 4e 53 54 41 54 45 2b 59  ION  (YYNSTATE+Y
309a3 59 4e 52 55 4c 45 2b 31 29 0a 23 64 65 66 69 6e  YNRULE+1).#defin
309a4 65 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49 4f  e YY_ERROR_ACTIO
309a5 4e 20 20 20 28 59 59 4e 53 54 41 54 45 2b 59 59  N   (YYNSTATE+YY
309a6 4e 52 55 4c 45 29 0a 0a 2f 2a 20 54 68 65 20 79  NRULE)../* The y
309a7 79 7a 65 72 6f 6d 69 6e 6f 72 20 63 6f 6e 73 74  yzerominor const
309a8 61 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 69  ant is used to i
309a9 6e 69 74 69 61 6c 69 7a 65 20 69 6e 73 74 61 6e  nitialize instan
309aa 63 65 73 20 6f 66 0a 2a 2a 20 59 59 4d 49 4e 4f  ces of.** YYMINO
309ab 52 54 59 50 45 20 6f 62 6a 65 63 74 73 20 74 6f  RTYPE objects to
309ac 20 7a 65 72 6f 2e 20 2a 2f 0a 73 74 61 74 69 63   zero. */.static
309ad 20 63 6f 6e 73 74 20 59 59 4d 49 4e 4f 52 54 59   const YYMINORTY
309ae 50 45 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 20 3d  PE yyzerominor =
309af 20 7b 20 30 20 7d 3b 0a 0a 2f 2a 20 44 65 66 69   { 0 };../* Defi
309b0 6e 65 20 74 68 65 20 79 79 74 65 73 74 63 61 73  ne the yytestcas
309b1 65 28 29 20 6d 61 63 72 6f 20 74 6f 20 62 65 20  e() macro to be 
309b2 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 6e 6f  a no-op if is no
309b3 74 20 61 6c 72 65 61 64 79 20 64 65 66 69 6e 65  t already define
309b4 64 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 2e 0a  d.** otherwise..
309b5 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f  **.** Applicatio
309b6 6e 73 20 63 61 6e 20 63 68 6f 6f 73 65 20 74 6f  ns can choose to
309b7 20 64 65 66 69 6e 65 20 79 79 74 65 73 74 63 61   define yytestca
309b8 73 65 28 29 20 69 6e 20 74 68 65 20 25 69 6e 63  se() in the %inc
309b9 6c 75 64 65 20 73 65 63 74 69 6f 6e 0a 2a 2a 20  lude section.** 
309ba 74 6f 20 61 20 6d 61 63 72 6f 20 74 68 61 74 20  to a macro that 
309bb 63 61 6e 20 61 73 73 69 73 74 20 69 6e 20 76 65  can assist in ve
309bc 72 69 66 79 69 6e 67 20 63 6f 64 65 20 63 6f 76  rifying code cov
309bd 65 72 61 67 65 2e 20 20 46 6f 72 20 70 72 6f 64  erage.  For prod
309be 75 63 74 69 6f 6e 0a 2a 2a 20 63 6f 64 65 20 74  uction.** code t
309bf 68 65 20 79 79 74 65 73 74 63 61 73 65 28 29 20  he yytestcase() 
309c0 6d 61 63 72 6f 20 73 68 6f 75 6c 64 20 62 65 20  macro should be 
309c1 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 42 75 74  turned off.  But
309c2 20 69 74 20 69 73 20 75 73 65 66 75 6c 0a 2a 2a   it is useful.**
309c3 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f   for testing..*/
309c4 0a 23 69 66 6e 64 65 66 20 79 79 74 65 73 74 63  .#ifndef yytestc
309c5 61 73 65 0a 23 20 64 65 66 69 6e 65 20 79 79 74  ase.# define yyt
309c6 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69  estcase(X).#endi
309c7 66 0a 0a 0a 2f 2a 20 4e 65 78 74 20 61 72 65 20  f.../* Next are 
309c8 74 68 65 20 74 61 62 6c 65 73 20 75 73 65 64 20  the tables used 
309c9 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61  to determine wha
309ca 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65  t action to take
309cb 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a   based on the.**
309cc 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 61   current state a
309cd 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20 74 6f 6b  nd lookahead tok
309ce 65 6e 2e 20 20 54 68 65 73 65 20 74 61 62 6c 65  en.  These table
309cf 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d  s are used to im
309d0 70 6c 65 6d 65 6e 74 0a 2a 2a 20 66 75 6e 63 74  plement.** funct
309d1 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20 61  ions that take a
309d2 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20 61 6e   state number an
309d3 64 20 6c 6f 6f 6b 61 68 65 61 64 20 76 61 6c 75  d lookahead valu
309d4 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 0a  e and return an.
309d5 2a 2a 20 61 63 74 69 6f 6e 20 69 6e 74 65 67 65  ** action intege
309d6 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f  r.  .**.** Suppo
309d7 73 65 20 74 68 65 20 61 63 74 69 6f 6e 20 69 6e  se the action in
309d8 74 65 67 65 72 20 69 73 20 4e 2e 20 20 54 68 65  teger is N.  The
309d9 6e 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20  n the action is 
309da 64 65 74 65 72 6d 69 6e 65 64 20 61 73 0a 2a 2a  determined as.**
309db 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2a 20 20   follows.**.**  
309dc 20 30 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41   0 <= N < YYNSTA
309dd 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TE              
309de 20 20 20 20 53 68 69 66 74 20 4e 2e 20 20 54 68      Shift N.  Th
309df 61 74 20 69 73 2c 20 70 75 73 68 20 74 68 65 20  at is, push the 
309e0 6c 6f 6f 6b 61 68 65 61 64 0a 2a 2a 20 20 20 20  lookahead.**    
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 20 20 20 20 20 20 20                  
309e3 20 20 74 6f 6b 65 6e 20 6f 6e 74 6f 20 74 68 65    token onto the
309e4 20 73 74 61 63 6b 20 61 6e 64 20 67 6f 74 6f 20   stack and goto 
309e5 73 74 61 74 65 20 4e 2e 0a 2a 2a 0a 2a 2a 20 20  state N..**.**  
309e6 20 59 59 4e 53 54 41 54 45 20 3c 3d 20 4e 20 3c   YYNSTATE <= N <
309e7 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c   YYNSTATE+YYNRUL
309e8 45 20 20 20 52 65 64 75 63 65 20 62 79 20 72 75  E   Reduce by ru
309e9 6c 65 20 4e 2d 59 59 4e 53 54 41 54 45 2e 0a 2a  le N-YYNSTATE..*
309ea 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53  *.**   N == YYNS
309eb 54 41 54 45 2b 59 59 4e 52 55 4c 45 20 20 20 20  TATE+YYNRULE    
309ec 20 20 20 20 20 20 20 20 20 20 41 20 73 79 6e 74            A synt
309ed 61 78 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  ax error has occ
309ee 75 72 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e  urred..**.**   N
309ef 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e   == YYNSTATE+YYN
309f0 52 55 4c 45 2b 31 20 20 20 20 20 20 20 20 20 20  RULE+1          
309f1 20 20 54 68 65 20 70 61 72 73 65 72 20 61 63 63    The parser acc
309f2 65 70 74 73 20 69 74 73 20 69 6e 70 75 74 2e 0a  epts its input..
309f3 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e  **.**   N == YYN
309f4 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 32 20  STATE+YYNRULE+2 
309f5 20 20 20 20 20 20 20 20 20 20 20 4e 6f 20 73 75             No su
309f6 63 68 20 61 63 74 69 6f 6e 2e 20 20 44 65 6e 6f  ch action.  Deno
309f7 74 65 73 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20  tes unused.**   
309f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
309f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
309fa 20 20 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20     slots in the 
309fb 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 74 61 62 6c  yy_action[] tabl
309fc 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 74  e..**.** The act
309fd 69 6f 6e 20 74 61 62 6c 65 20 69 73 20 63 6f 6e  ion table is con
309fe 73 74 72 75 63 74 65 64 20 61 73 20 61 20 73 69  structed as a si
309ff 6e 67 6c 65 20 6c 61 72 67 65 20 74 61 62 6c 65  ngle large table
30a00 20 6e 61 6d 65 64 20 79 79 5f 61 63 74 69 6f 6e   named yy_action
30a01 5b 5d 2e 0a 2a 2a 20 47 69 76 65 6e 20 73 74 61  []..** Given sta
30a02 74 65 20 53 20 61 6e 64 20 6c 6f 6f 6b 61 68 65  te S and lookahe
30a03 61 64 20 58 2c 20 74 68 65 20 61 63 74 69 6f 6e  ad X, the action
30a04 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 73 0a   is computed as.
30a05 2a 2a 0a 2a 2a 20 20 20 20 20 20 79 79 5f 61 63  **.**      yy_ac
30a06 74 69 6f 6e 5b 20 79 79 5f 73 68 69 66 74 5f 6f  tion[ yy_shift_o
30a07 66 73 74 5b 53 5d 20 2b 20 58 20 5d 0a 2a 2a 0a  fst[S] + X ].**.
30a08 2a 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20  ** If the index 
30a09 76 61 6c 75 65 20 79 79 5f 73 68 69 66 74 5f 6f  value yy_shift_o
30a0a 66 73 74 5b 53 5d 2b 58 20 69 73 20 6f 75 74 20  fst[S]+X is out 
30a0b 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 20 74  of range or if t
30a0c 68 65 20 76 61 6c 75 65 0a 2a 2a 20 79 79 5f 6c  he value.** yy_l
30a0d 6f 6f 6b 61 68 65 61 64 5b 79 79 5f 73 68 69 66  ookahead[yy_shif
30a0e 74 5f 6f 66 73 74 5b 53 5d 2b 58 5d 20 69 73 20  t_ofst[S]+X] is 
30a0f 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 58 20 6f  not equal to X o
30a10 72 20 69 66 20 79 79 5f 73 68 69 66 74 5f 6f 66  r if yy_shift_of
30a11 73 74 5b 53 5d 0a 2a 2a 20 69 73 20 65 71 75 61  st[S].** is equa
30a12 6c 20 74 6f 20 59 59 5f 53 48 49 46 54 5f 55 53  l to YY_SHIFT_US
30a13 45 5f 44 46 4c 54 2c 20 69 74 20 6d 65 61 6e 73  E_DFLT, it means
30a14 20 74 68 61 74 20 74 68 65 20 61 63 74 69 6f 6e   that the action
30a15 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 74   is not in the t
30a16 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 61 74  able.** and that
30a17 20 79 79 5f 64 65 66 61 75 6c 74 5b 53 5d 20 73   yy_default[S] s
30a18 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e  hould be used in
30a19 73 74 65 61 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  stead.  .**.** T
30a1a 68 65 20 66 6f 72 6d 75 6c 61 20 61 62 6f 76 65  he formula above
30a1b 20 69 73 20 66 6f 72 20 63 6f 6d 70 75 74 69 6e   is for computin
30a1c 67 20 74 68 65 20 61 63 74 69 6f 6e 20 77 68 65  g the action whe
30a1d 6e 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20  n the lookahead 
30a1e 69 73 0a 2a 2a 20 61 20 74 65 72 6d 69 6e 61 6c  is.** a terminal
30a1f 20 73 79 6d 62 6f 6c 2e 20 20 49 66 20 74 68 65   symbol.  If the
30a20 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73 20 61 20   lookahead is a 
30a21 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 20 28 61 73  non-terminal (as
30a22 20 6f 63 63 75 72 73 20 61 66 74 65 72 0a 2a 2a   occurs after.**
30a23 20 61 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e   a reduce action
30a24 29 20 74 68 65 6e 20 74 68 65 20 79 79 5f 72 65  ) then the yy_re
30a25 64 75 63 65 5f 6f 66 73 74 5b 5d 20 61 72 72 61  duce_ofst[] arra
30a26 79 20 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61  y is used in pla
30a27 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 79 79 5f  ce of.** the yy_
30a28 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20 61 72 72  shift_ofst[] arr
30a29 61 79 20 61 6e 64 20 59 59 5f 52 45 44 55 43 45  ay and YY_REDUCE
30a2a 5f 55 53 45 5f 44 46 4c 54 20 69 73 20 75 73 65  _USE_DFLT is use
30a2b 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a  d in place of.**
30a2c 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46   YY_SHIFT_USE_DF
30a2d 4c 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  LT..**.** The fo
30a2e 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20  llowing are the 
30a2f 74 61 62 6c 65 73 20 67 65 6e 65 72 61 74 65 64  tables generated
30a30 20 69 6e 20 74 68 69 73 20 73 65 63 74 69 6f 6e   in this section
30a31 3a 0a 2a 2a 0a 2a 2a 20 20 79 79 5f 61 63 74 69  :.**.**  yy_acti
30a32 6f 6e 5b 5d 20 20 20 20 20 20 20 20 41 20 73 69  on[]        A si
30a33 6e 67 6c 65 20 74 61 62 6c 65 20 63 6f 6e 74 61  ngle table conta
30a34 69 6e 69 6e 67 20 61 6c 6c 20 61 63 74 69 6f 6e  ining all action
30a35 73 2e 0a 2a 2a 20 20 79 79 5f 6c 6f 6f 6b 61 68  s..**  yy_lookah
30a36 65 61 64 5b 5d 20 20 20 20 20 41 20 74 61 62 6c  ead[]     A tabl
30a37 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
30a38 20 6c 6f 6f 6b 61 68 65 61 64 20 66 6f 72 20 65   lookahead for e
30a39 61 63 68 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20  ach entry in.** 
30a3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30a3b 20 20 20 20 79 79 5f 61 63 74 69 6f 6e 2e 20 20      yy_action.  
30a3c 55 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 68  Used to detect h
30a3d 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a  ash collisions..
30a3e 2a 2a 20 20 79 79 5f 73 68 69 66 74 5f 6f 66 73  **  yy_shift_ofs
30a3f 74 5b 5d 20 20 20 20 46 6f 72 20 65 61 63 68 20  t[]    For each 
30a40 73 74 61 74 65 2c 20 74 68 65 20 6f 66 66 73 65  state, the offse
30a41 74 20 69 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e  t into yy_action
30a42 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20   for.**         
30a43 20 20 20 20 20 20 20 20 20 20 20 20 73 68 69 66              shif
30a44 74 69 6e 67 20 74 65 72 6d 69 6e 61 6c 73 2e 0a  ting terminals..
30a45 2a 2a 20 20 79 79 5f 72 65 64 75 63 65 5f 6f 66  **  yy_reduce_of
30a46 73 74 5b 5d 20 20 20 46 6f 72 20 65 61 63 68 20  st[]   For each 
30a47 73 74 61 74 65 2c 20 74 68 65 20 6f 66 66 73 65  state, the offse
30a48 74 20 69 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e  t into yy_action
30a49 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20   for.**         
30a4a 20 20 20 20 20 20 20 20 20 20 20 20 73 68 69 66              shif
30a4b 74 69 6e 67 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61  ting non-termina
30a4c 6c 73 20 61 66 74 65 72 20 61 20 72 65 64 75 63  ls after a reduc
30a4d 65 2e 0a 2a 2a 20 20 79 79 5f 64 65 66 61 75 6c  e..**  yy_defaul
30a4e 74 5b 5d 20 20 20 20 20 20 20 44 65 66 61 75 6c  t[]       Defaul
30a4f 74 20 61 63 74 69 6f 6e 20 66 6f 72 20 65 61 63  t action for eac
30a50 68 20 73 74 61 74 65 2e 0a 2a 2f 0a 23 64 65 66  h state..*/.#def
30a51 69 6e 65 20 59 59 5f 41 43 54 54 41 42 5f 43 4f  ine YY_ACTTAB_CO
30a52 55 4e 54 20 28 31 35 34 33 29 0a 73 74 61 74 69  UNT (1543).stati
30a53 63 20 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e  c const YYACTION
30a54 54 59 50 45 20 79 79 5f 61 63 74 69 6f 6e 5b 5d  TYPE yy_action[]
30a55 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a   = {. /*     0 *
30a56 2f 20 20 20 33 31 32 2c 20 20 20 34 39 2c 20 20  /   312,   49,  
30a57 35 35 34 2c 20 20 20 34 36 2c 20 20 31 34 37 2c  554,   46,  147,
30a58 20 20 31 37 32 2c 20 20 36 32 36 2c 20 20 35 39    172,  626,  59
30a59 36 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 0a 20  6,   55,   55,. 
30a5a 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 20 35  /*    10 */    5
30a5b 35 2c 20 20 20 35 35 2c 20 20 33 30 31 2c 20 20  5,   55,  301,  
30a5c 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
30a5d 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35     53,   52,   5
30a5e 32 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20 20 20  2,   51,. /*    
30a5f 32 30 20 2a 2f 20 20 20 20 35 31 2c 20 20 20 35  20 */    51,   5
30a60 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20  1,   50,  237,  
30a61 36 31 37 2c 20 20 36 31 36 2c 20 20 36 31 35 2c  617,  616,  615,
30a62 20 20 36 32 32 2c 20 20 36 32 31 2c 20 20 36 30    622,  621,  60
30a63 37 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20  7,. /*    30 */ 
30a64 20 20 35 38 39 2c 20 20 35 38 33 2c 20 20 20 34    589,  583,   4
30a65 38 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  8,   53,   53,  
30a66 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c   53,   53,   52,
30a67 20 20 20 35 32 2c 20 20 20 35 31 2c 0a 20 2f 2a     52,   51,. /*
30a68 20 20 20 20 34 30 20 2a 2f 20 20 20 20 35 31 2c      40 */    51,
30a69 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33     51,   50,  23
30a6a 37 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20  7,   51,   51,  
30a6b 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c   51,   50,  237,
30a6c 20 20 20 35 36 2c 0a 20 2f 2a 20 20 20 20 35 30     56,. /*    50
30a6d 20 2a 2f 20 20 20 20 35 37 2c 20 20 20 34 37 2c   */    57,   47,
30a6e 20 20 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38    581,  580,  58
30a6f 32 2c 20 20 35 38 32 2c 20 20 20 35 34 2c 20 20  2,  582,   54,  
30a70 20 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   54,   55,   55,
30a71 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20  . /*    60 */   
30a72 20 35 35 2c 20 20 20 35 35 2c 20 20 32 31 36 2c   55,   55,  216,
30a73 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
30a74 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20  3,   53,   52,  
30a75 20 35 32 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20   52,   51,. /*  
30a76 20 20 37 30 20 2a 2f 20 20 20 20 35 31 2c 20 20    70 */    51,  
30a77 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c   51,   50,  237,
30a78 20 20 33 31 32 2c 20 20 35 39 36 2c 20 20 20 34    312,  596,   4
30a79 39 2c 20 20 33 32 39 2c 20 20 20 34 36 2c 20 20  9,  329,   46,  
30a7a 31 34 37 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a  147,. /*    80 *
30a7b 2f 20 20 20 20 33 32 2c 20 20 20 35 33 2c 20 20  /    32,   53,  
30a7c 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
30a7d 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35     52,   52,   5
30a7e 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 0a 20  1,   51,   51,. 
30a7f 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 20 35  /*    90 */    5
30a80 30 2c 20 20 32 33 37 2c 20 20 33 32 39 2c 20 20  0,  237,  329,  
30a81 36 32 32 2c 20 20 36 32 31 2c 20 20 36 31 39 2c  622,  621,  619,
30a82 20 20 36 31 38 2c 20 20 31 36 36 2c 20 20 34 33    618,  166,  43
30a83 33 2c 20 20 35 34 37 2c 0a 20 2f 2a 20 20 20 31  3,  547,. /*   1
30a84 30 30 20 2a 2f 20 20 20 33 38 31 2c 20 20 33 37  00 */   381,  37
30a85 38 2c 20 20 33 37 37 2c 20 20 35 34 39 2c 20 20  8,  377,  549,  
30a86 35 38 39 2c 20 20 35 38 33 2c 20 20 33 38 39 2c  589,  583,  389,
30a87 20 20 34 39 30 2c 20 20 31 36 36 2c 20 20 20 35    490,  166,   5
30a88 38 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20  8,. /*   110 */ 
30a89 20 20 33 37 36 2c 20 20 33 38 31 2c 20 20 33 37    376,  381,  37
30a8a 38 2c 20 20 33 37 37 2c 20 20 33 39 30 2c 20 20  8,  377,  390,  
30a8b 32 39 39 2c 20 20 36 32 32 2c 20 20 36 32 31 2c  299,  622,  621,
30a8c 20 20 34 38 30 2c 20 20 20 36 37 2c 0a 20 2f 2a    480,   67,. /*
30a8d 20 20 20 31 32 30 20 2a 2f 20 20 20 36 37 30 2c     120 */   670,
30a8e 20 20 33 37 36 2c 20 20 36 32 30 2c 20 20 20 35    376,  620,   5
30a8f 36 2c 20 20 20 35 37 2c 20 20 20 34 37 2c 20 20  6,   57,   47,  
30a90 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c  581,  580,  582,
30a91 20 20 35 38 32 2c 0a 20 2f 2a 20 20 20 31 33 30    582,. /*   130
30a92 20 2a 2f 20 20 20 20 35 34 2c 20 20 20 35 34 2c   */    54,   54,
30a93 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
30a94 35 2c 20 20 20 35 35 2c 20 20 32 35 33 2c 20 20  5,   55,  253,  
30a95 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
30a96 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20  . /*   140 */   
30a97 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   53,   52,   52,
30a98 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
30a99 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20  1,   50,  237,  
30a9a 33 31 32 2c 20 20 34 30 38 2c 0a 20 2f 2a 20 20  312,  408,. /*  
30a9b 20 31 35 30 20 2a 2f 20 20 20 32 32 35 2c 20 20   150 */   225,  
30a9c 35 37 38 2c 20 20 35 37 38 2c 20 20 31 33 33 2c  578,  578,  133,
30a9d 20 20 31 37 37 2c 20 20 31 33 39 2c 20 20 32 38    177,  139,  28
30a9e 33 2c 20 20 33 38 34 2c 20 20 32 37 38 2c 20 20  3,  384,  278,  
30a9f 33 38 33 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a  383,. /*   160 *
30aa0 2f 20 20 20 31 36 39 2c 20 20 36 31 39 2c 20 20  /   169,  619,  
30aa1 36 31 38 2c 20 20 36 30 31 2c 20 20 31 39 37 2c  618,  601,  197,
30aa2 20 20 32 32 35 2c 20 20 32 37 34 2c 20 20 36 30    225,  274,  60
30aa3 32 2c 20 20 34 33 39 2c 20 20 31 34 36 2c 0a 20  2,  439,  146,. 
30aa4 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 31 33  /*   170 */   13
30aa5 39 2c 20 20 32 38 33 2c 20 20 33 38 34 2c 20 20  9,  283,  384,  
30aa6 32 37 38 2c 20 20 33 38 33 2c 20 20 31 36 39 2c  278,  383,  169,
30aa7 20 20 35 36 39 2c 20 20 32 33 35 2c 20 20 35 38    569,  235,  58
30aa8 39 2c 20 20 35 38 33 2c 0a 20 2f 2a 20 20 20 31  9,  583,. /*   1
30aa9 38 30 20 2a 2f 20 20 20 32 35 30 2c 20 20 32 37  80 */   250,  27
30aaa 34 2c 20 20 32 35 32 2c 20 20 36 32 30 2c 20 20  4,  252,  620,  
30aab 36 31 39 2c 20 20 36 31 38 2c 20 20 35 34 36 2c  619,  618,  546,
30aac 20 20 34 33 36 2c 20 20 34 34 30 2c 20 20 34 34    436,  440,  44
30aad 31 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20  1,. /*   190 */ 
30aae 20 20 31 36 38 2c 20 20 36 32 32 2c 20 20 36 32    168,  622,  62
30aaf 31 2c 20 20 35 34 37 2c 20 20 34 33 38 2c 20 20  1,  547,  438,  
30ab0 34 33 37 2c 20 20 31 39 32 2c 20 20 20 35 36 2c  437,  192,   56,
30ab1 20 20 20 35 37 2c 20 20 20 34 37 2c 0a 20 2f 2a     57,   47,. /*
30ab2 20 20 20 32 30 30 20 2a 2f 20 20 20 35 38 31 2c     200 */   581,
30ab3 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35 38    580,  582,  58
30ab4 32 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20  2,   54,   54,  
30ab5 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   55,   55,   55,
30ab6 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20 32 31 30     55,. /*   210
30ab7 20 2a 2f 20 20 20 20 20 36 2c 20 20 20 35 33 2c   */     6,   53,
30ab8 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
30ab9 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20  3,   52,   52,  
30aba 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c   51,   51,   51,
30abb 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20  . /*   220 */   
30abc 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32 2c   50,  237,  312,
30abd 20 20 32 38 32 2c 20 20 20 35 32 2c 20 20 20 35    282,   52,   5
30abe 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20  2,   51,   51,  
30abf 20 35 31 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20   51,   50,. /*  
30ac0 20 32 33 30 20 2a 2f 20 20 20 32 33 37 2c 20 20   230 */   237,  
30ac1 34 39 30 2c 20 20 31 38 33 2c 20 20 32 38 31 2c  490,  183,  281,
30ac2 20 20 35 34 37 2c 20 20 31 36 36 2c 20 20 34 33    547,  166,  43
30ac3 39 2c 20 20 35 36 35 2c 20 20 33 38 31 2c 20 20  9,  565,  381,  
30ac4 33 37 38 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a  378,. /*   240 *
30ac5 2f 20 20 20 33 37 37 2c 20 20 35 39 36 2c 20 20  /   377,  596,  
30ac6 36 30 36 2c 20 20 20 36 37 2c 20 20 33 32 37 2c  606,   67,  327,
30ac7 20 20 31 37 32 2c 20 20 36 32 30 2c 20 20 35 39    172,  620,  59
30ac8 36 2c 20 20 33 37 36 2c 20 20 34 34 32 2c 0a 20  6,  376,  442,. 
30ac9 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 32 33  /*   250 */   23
30aca 36 2c 20 20 36 32 30 2c 20 20 35 38 39 2c 20 20  6,  620,  589,  
30acb 35 38 33 2c 20 20 33 30 36 2c 20 20 34 32 33 2c  583,  306,  423,
30acc 20 20 34 34 30 2c 20 20 33 33 39 2c 20 20 32 35    440,  339,  25
30acd 31 2c 20 20 36 31 39 2c 0a 20 2f 2a 20 20 20 32  1,  619,. /*   2
30ace 36 30 20 2a 2f 20 20 20 36 31 38 2c 20 20 33 33  60 */   618,  33
30acf 31 2c 20 20 35 37 34 2c 20 20 35 37 33 2c 20 20  1,  574,  573,  
30ad0 20 20 37 2c 20 20 35 32 34 2c 20 20 31 39 34 2c    7,  524,  194,
30ad1 20 20 34 38 31 2c 20 20 20 31 36 2c 20 20 35 39    481,   16,  59
30ad2 34 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20  4,. /*   270 */ 
30ad3 20 20 31 38 39 2c 20 20 20 35 36 2c 20 20 20 35    189,   56,   5
30ad4 37 2c 20 20 20 34 37 2c 20 20 35 38 31 2c 20 20  7,   47,  581,  
30ad5 35 38 30 2c 20 20 35 38 32 2c 20 20 35 38 32 2c  580,  582,  582,
30ad6 20 20 20 35 34 2c 20 20 20 35 34 2c 0a 20 2f 2a     54,   54,. /*
30ad7 20 20 20 32 38 30 20 2a 2f 20 20 20 20 35 35 2c     280 */    55,
30ad8 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
30ad9 35 2c 20 20 35 34 35 2c 20 20 20 35 33 2c 20 20  5,  545,   53,  
30ada 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
30adb 20 20 20 35 32 2c 0a 20 2f 2a 20 20 20 32 39 30     52,. /*   290
30adc 20 2a 2f 20 20 20 20 35 32 2c 20 20 20 35 31 2c   */    52,   51,
30add 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
30ade 30 2c 20 20 32 33 37 2c 20 20 33 31 32 2c 20 20  0,  237,  312,  
30adf 34 31 30 2c 20 20 34 36 34 2c 20 20 34 32 31 2c  410,  464,  421,
30ae0 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20  . /*   300 */   
30ae1 35 39 32 2c 20 20 35 39 32 2c 20 20 35 39 32 2c  592,  592,  592,
30ae2 20 20 36 37 31 2c 20 20 31 34 36 2c 20 20 34 31    671,  146,  41
30ae3 30 2c 20 20 20 20 31 2c 20 20 32 30 35 2c 20 20  0,    1,  205,  
30ae4 34 31 30 2c 20 20 35 39 36 2c 0a 20 2f 2a 20 20  410,  596,. /*  
30ae5 20 33 31 30 20 2a 2f 20 20 20 36 32 32 2c 20 20   310 */   622,  
30ae6 36 32 31 2c 20 20 34 31 33 2c 20 20 34 32 30 2c  621,  413,  420,
30ae7 20 20 39 34 39 2c 20 20 35 39 36 2c 20 20 39 34    949,  596,  94
30ae8 39 2c 20 20 33 34 30 2c 20 20 32 33 36 2c 20 20  9,  340,  236,  
30ae9 35 33 30 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a  530,. /*   320 *
30aea 2f 20 20 20 34 31 33 2c 20 20 36 30 30 2c 20 20  /   413,  600,  
30aeb 20 37 34 2c 20 20 34 31 33 2c 20 20 32 33 36 2c   74,  413,  236,
30aec 20 20 35 35 32 2c 20 20 35 38 39 2c 20 20 35 38    552,  589,  58
30aed 33 2c 20 20 35 34 37 2c 20 20 36 30 30 2c 0a 20  3,  547,  600,. 
30aee 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 20 39  /*   330 */    9
30aef 35 2c 20 20 20 36 38 2c 20 20 36 30 30 2c 20 20  5,   68,  600,  
30af0 20 38 38 2c 20 20 35 35 31 2c 20 20 36 32 32 2c   88,  551,  622,
30af1 20 20 36 32 31 2c 20 20 34 36 35 2c 20 20 35 34    621,  465,  54
30af2 32 2c 20 20 20 33 38 2c 0a 20 2f 2a 20 20 20 33  2,   38,. /*   3
30af3 34 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 35 39  40 */    49,  59
30af4 39 2c 20 20 20 34 36 2c 20 20 31 34 37 2c 20 20  9,   46,  147,  
30af5 34 36 35 2c 20 20 20 35 36 2c 20 20 20 35 37 2c  465,   56,   57,
30af6 20 20 20 34 37 2c 20 20 35 38 31 2c 20 20 35 38     47,  581,  58
30af7 30 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20  0,. /*   350 */ 
30af8 20 20 35 38 32 2c 20 20 35 38 32 2c 20 20 20 35    582,  582,   5
30af9 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20  4,   54,   55,  
30afa 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   55,   55,   55,
30afb 20 20 34 32 34 2c 20 20 20 35 33 2c 0a 20 2f 2a    424,   53,. /*
30afc 20 20 20 33 36 30 20 2a 2f 20 20 20 20 35 33 2c     360 */    53,
30afd 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
30afe 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20  2,   52,   51,  
30aff 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c   51,   51,   50,
30b00 20 20 32 33 37 2c 0a 20 2f 2a 20 20 20 33 37 30    237,. /*   370
30b01 20 2a 2f 20 20 20 33 31 32 2c 20 20 33 39 37 2c   */   312,  397,
30b02 20 20 33 39 35 2c 20 20 32 33 32 2c 20 20 35 32    395,  232,  52
30b03 39 2c 20 20 35 37 37 2c 20 20 33 38 37 2c 20 20  9,  577,  387,  
30b04 35 33 33 2c 20 20 36 31 39 2c 20 20 36 31 38 2c  533,  619,  618,
30b05 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20  . /*   380 */   
30b06 36 30 35 2c 20 20 34 39 32 2c 20 20 35 36 30 2c  605,  492,  560,
30b07 20 20 35 38 38 2c 20 20 35 38 37 2c 20 20 33 35    588,  587,  35
30b08 30 2c 20 20 32 35 37 2c 20 20 36 32 32 2c 20 20  0,  257,  622,  
30b09 36 32 31 2c 20 20 34 39 35 2c 0a 20 2f 2a 20 20  621,  495,. /*  
30b0a 20 33 39 30 20 2a 2f 20 20 20 35 36 34 2c 20 20   390 */   564,  
30b0b 33 35 36 2c 20 20 33 35 30 2c 20 20 32 35 37 2c  356,  350,  257,
30b0c 20 20 20 34 39 2c 20 20 32 33 39 2c 20 20 20 34     49,  239,   4
30b0d 36 2c 20 20 31 34 37 2c 20 20 35 35 39 2c 20 20  6,  147,  559,  
30b0e 33 35 37 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a  357,. /*   400 *
30b0f 2f 20 20 20 35 38 39 2c 20 20 35 38 33 2c 20 20  /   589,  583,  
30b10 32 33 39 2c 20 20 36 31 39 2c 20 20 36 31 38 2c  239,  619,  618,
30b11 20 20 35 38 35 2c 20 20 35 38 34 2c 20 20 34 30    585,  584,  40
30b12 38 2c 20 20 32 35 38 2c 20 20 35 37 38 2c 0a 20  8,  258,  578,. 
30b13 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 35 37  /*   410 */   57
30b14 38 2c 20 20 36 37 32 2c 20 20 32 30 39 2c 20 20  8,  672,  209,  
30b15 20 33 35 2c 20 20 35 35 38 2c 20 20 32 35 38 2c   35,  558,  258,
30b16 20 20 34 30 31 2c 20 20 36 32 32 2c 20 20 36 32    401,  622,  62
30b17 31 2c 20 20 20 35 36 2c 0a 20 2f 2a 20 20 20 34  1,   56,. /*   4
30b18 32 30 20 2a 2f 20 20 20 20 35 37 2c 20 20 20 34  20 */    57,   4
30b19 37 2c 20 20 35 38 31 2c 20 20 35 38 30 2c 20 20  7,  581,  580,  
30b1a 35 38 32 2c 20 20 35 38 32 2c 20 20 20 35 34 2c  582,  582,   54,
30b1b 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20 20 35     54,   55,   5
30b1c 35 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20  5,. /*   430 */ 
30b1d 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 35 38     55,   55,  58
30b1e 36 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  6,   53,   53,  
30b1f 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c   53,   53,   52,
30b20 20 20 20 35 32 2c 20 20 20 35 31 2c 0a 20 2f 2a     52,   51,. /*
30b21 20 20 20 34 34 30 20 2a 2f 20 20 20 20 35 31 2c     440 */    51,
30b22 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33     51,   50,  23
30b23 37 2c 20 20 33 31 32 2c 20 20 35 36 30 2c 20 20  7,  312,  560,  
30b24 35 39 39 2c 20 20 34 31 30 2c 20 20 35 32 36 2c  599,  410,  526,
30b25 20 20 35 33 31 2c 0a 20 2f 2a 20 20 20 34 35 30    531,. /*   450
30b26 20 2a 2f 20 20 20 31 38 34 2c 20 20 35 31 34 2c   */   184,  514,
30b27 20 20 35 31 33 2c 20 20 34 37 34 2c 20 20 33 36    513,  474,  36
30b28 36 2c 20 20 36 31 39 2c 20 20 36 31 38 2c 20 20  6,  619,  618,  
30b29 35 37 36 2c 20 20 34 31 30 2c 20 20 20 36 35 2c  576,  410,   65,
30b2a 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20  . /*   460 */   
30b2b 31 37 36 2c 20 20 35 35 39 2c 20 20 34 31 33 2c  176,  559,  413,
30b2c 20 20 34 30 38 2c 20 20 33 31 31 2c 20 20 35 37    408,  311,  57
30b2d 38 2c 20 20 35 37 38 2c 20 20 35 36 37 2c 20 20  8,  578,  567,  
30b2e 34 39 31 2c 20 20 32 31 35 2c 0a 20 2f 2a 20 20  491,  215,. /*  
30b2f 20 34 37 30 20 2a 2f 20 20 20 33 35 32 2c 20 20   470 */   352,  
30b30 36 30 30 2c 20 20 20 39 34 2c 20 20 34 31 33 2c  600,   94,  413,
30b31 20 20 35 38 39 2c 20 20 35 38 33 2c 20 20 34 37    589,  583,  47
30b32 34 2c 20 20 35 35 38 2c 20 20 34 30 38 2c 20 20  4,  558,  408,  
30b33 35 31 38 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a  518,. /*   480 *
30b34 2f 20 20 20 35 37 38 2c 20 20 35 37 38 2c 20 20  /   578,  578,  
30b35 36 30 30 2c 20 20 20 39 35 2c 20 20 34 37 30 2c  600,   95,  470,
30b36 20 20 36 31 39 2c 20 20 36 31 38 2c 20 20 20 36    619,  618,   6
30b37 32 2c 20 20 34 32 30 2c 20 20 39 34 38 2c 0a 20  2,  420,  948,. 
30b38 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 35 31  /*   490 */   51
30b39 37 2c 20 20 39 34 38 2c 20 20 33 34 39 2c 20 20  7,  948,  349,  
30b3a 20 35 36 2c 20 20 20 35 37 2c 20 20 20 34 37 2c   56,   57,   47,
30b3b 20 20 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38    581,  580,  58
30b3c 32 2c 20 20 35 38 32 2c 0a 20 2f 2a 20 20 20 35  2,  582,. /*   5
30b3d 30 30 20 2a 2f 20 20 20 20 35 34 2c 20 20 20 35  00 */    54,   5
30b3e 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  4,   55,   55,  
30b3f 20 35 35 2c 20 20 20 35 35 2c 20 20 31 37 35 2c   55,   55,  175,
30b40 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
30b41 33 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20  3,. /*   510 */ 
30b42 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35     53,   52,   5
30b43 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20  2,   51,   51,  
30b44 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c   51,   50,  237,
30b45 20 20 33 31 32 2c 20 20 34 39 30 2c 0a 20 2f 2a    312,  490,. /*
30b46 20 20 20 35 32 30 20 2a 2f 20 20 20 31 35 37 2c     520 */   157,
30b47 20 20 34 31 30 2c 20 20 35 30 39 2c 20 20 32 39    410,  509,  29
30b48 32 2c 20 20 33 39 33 2c 20 20 33 37 33 2c 20 20  2,  393,  373,  
30b49 33 34 38 2c 20 20 34 31 30 2c 20 20 36 32 33 2c  348,  410,  623,
30b4a 20 20 34 31 30 2c 0a 20 2f 2a 20 20 20 35 33 30    410,. /*   530
30b4b 20 2a 2f 20 20 20 34 32 38 2c 20 20 20 36 37 2c   */   428,   67,
30b4c 20 20 36 31 31 2c 20 20 34 32 34 2c 20 20 36 32    611,  424,  62
30b4d 30 2c 20 20 34 31 30 2c 20 20 34 31 33 2c 20 20  0,  410,  413,  
30b4e 35 34 30 2c 20 20 34 30 38 2c 20 20 31 37 31 2c  540,  408,  171,
30b4f 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20  . /*   540 */   
30b50 35 37 38 2c 20 20 35 37 38 2c 20 20 34 31 33 2c  578,  578,  413,
30b51 20 20 36 32 30 2c 20 20 34 31 33 2c 20 20 36 30    620,  413,  60
30b52 30 2c 20 20 20 37 33 2c 20 20 36 32 30 2c 20 20  0,   73,  620,  
30b53 35 38 39 2c 20 20 35 38 33 2c 0a 20 2f 2a 20 20  589,  583,. /*  
30b54 20 35 35 30 20 2a 2f 20 20 20 34 31 33 2c 20 20   550 */   413,  
30b55 36 30 30 2c 20 20 20 38 30 2c 20 20 36 30 30 2c  600,   80,  600,
30b56 20 20 20 38 38 2c 20 20 32 33 38 2c 20 20 31 36     88,  238,  16
30b57 38 2c 20 20 33 30 36 2c 20 20 34 32 32 2c 20 20  8,  306,  422,  
30b58 36 30 30 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a  600,. /*   560 *
30b59 2f 20 20 20 20 38 30 2c 20 20 32 30 31 2c 20 20  /    80,  201,  
30b5a 20 31 38 2c 20 20 34 36 38 2c 20 20 34 30 36 2c   18,  468,  406,
30b5b 20 20 35 37 34 2c 20 20 35 37 33 2c 20 20 20 35    574,  573,   5
30b5c 36 2c 20 20 20 35 37 2c 20 20 20 34 37 2c 0a 20  6,   57,   47,. 
30b5d 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 35 38  /*   570 */   58
30b5e 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20  1,  580,  582,  
30b5f 35 38 32 2c 20 20 20 35 34 2c 20 20 20 35 34 2c  582,   54,   54,
30b60 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
30b61 35 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20 35  5,   55,. /*   5
30b62 38 30 20 2a 2f 20 20 20 35 37 39 2c 20 20 20 35  80 */   579,   5
30b63 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  3,   53,   53,  
30b64 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   53,   52,   52,
30b65 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
30b66 31 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20  1,. /*   590 */ 
30b67 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31     50,  237,  31
30b68 32 2c 20 20 20 34 34 2c 20 20 32 33 33 2c 20 20  2,   44,  233,  
30b69 35 39 39 2c 20 20 32 37 31 2c 20 20 33 32 30 2c  599,  271,  320,
30b6a 20 20 33 34 31 2c 20 20 34 37 32 2c 0a 20 2f 2a    341,  472,. /*
30b6b 20 20 20 36 30 30 20 2a 2f 20 20 20 34 31 30 2c     600 */   410,
30b6c 20 20 38 37 34 2c 20 20 34 32 31 2c 20 20 34 37    874,  421,  47
30b6d 33 2c 20 20 35 30 33 2c 20 20 33 31 39 2c 20 20  3,  503,  319,  
30b6e 34 31 30 2c 20 20 32 30 30 2c 20 20 31 34 34 2c  410,  200,  144,
30b6f 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 36 31 30     66,. /*   610
30b70 20 2a 2f 20 20 20 33 32 37 2c 20 20 34 38 33 2c   */   327,  483,
30b71 20 20 35 30 38 2c 20 20 35 39 36 2c 20 20 32 37    508,  596,  27
30b72 34 2c 20 20 34 31 33 2c 20 20 32 33 39 2c 20 20  4,  413,  239,  
30b73 33 36 34 2c 20 20 34 38 34 2c 20 20 33 38 32 2c  364,  484,  382,
30b74 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20  . /*   620 */   
30b75 20 33 30 2c 20 20 34 31 33 2c 20 20 35 38 39 2c   30,  413,  589,
30b76 20 20 35 38 33 2c 20 20 36 30 30 2c 20 20 20 36    583,  600,   6
30b77 39 2c 20 20 35 30 32 2c 20 20 32 33 36 2c 20 20  9,  502,  236,  
30b78 33 34 32 2c 20 20 35 37 35 2c 0a 20 2f 2a 20 20  342,  575,. /*  
30b79 20 36 33 30 20 2a 2f 20 20 20 36 30 30 2c 20 20   630 */   600,  
30b7a 20 39 37 2c 20 20 31 39 39 2c 20 20 31 39 38 2c   97,  199,  198,
30b7b 20 20 32 30 39 2c 20 20 39 35 39 2c 20 20 31 38    209,  959,  18
30b7c 36 2c 20 20 34 31 38 2c 20 20 20 20 32 2c 20 20  6,  418,    2,  
30b7d 35 36 36 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a  566,. /*   640 *
30b7e 2f 20 20 20 32 36 39 2c 20 20 20 35 36 2c 20 20  /   269,   56,  
30b7f 20 35 37 2c 20 20 20 34 37 2c 20 20 35 38 31 2c   57,   47,  581,
30b80 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35 38    580,  582,  58
30b81 32 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 0a 20  2,   54,   54,. 
30b82 2f 2a 20 20 20 36 35 30 20 2a 2f 20 20 20 20 35  /*   650 */    5
30b83 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  5,   55,   55,  
30b84 20 35 35 2c 20 20 34 31 30 2c 20 20 20 35 33 2c   55,  410,   53,
30b85 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
30b86 33 2c 20 20 20 35 32 2c 0a 20 2f 2a 20 20 20 36  3,   52,. /*   6
30b87 36 30 20 2a 2f 20 20 20 20 35 32 2c 20 20 20 35  60 */    52,   5
30b88 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20  1,   51,   51,  
30b89 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32 2c   50,  237,  312,
30b8a 20 20 32 36 33 2c 20 20 35 39 39 2c 20 20 34 31    263,  599,  41
30b8b 33 2c 0a 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20  3,. /*   670 */ 
30b8c 20 20 34 31 30 2c 20 20 20 32 31 2c 20 20 31 39    410,   21,  19
30b8d 30 2c 20 20 33 35 38 2c 20 20 34 31 30 2c 20 20  0,  358,  410,  
30b8e 33 32 36 2c 20 20 34 31 30 2c 20 20 32 30 32 2c  326,  410,  202,
30b8f 20 20 36 30 30 2c 20 20 31 30 30 2c 0a 20 2f 2a    600,  100,. /*
30b90 20 20 20 36 38 30 20 2a 2f 20 20 20 33 38 36 2c     680 */   386,
30b91 20 20 35 39 36 2c 20 20 36 32 30 2c 20 20 35 36    596,  620,  56
30b92 32 2c 20 20 32 36 35 2c 20 20 34 31 33 2c 20 20  2,  265,  413,  
30b93 32 36 37 2c 20 20 34 31 30 2c 20 20 36 32 30 2c  267,  410,  620,
30b94 20 20 34 31 33 2c 0a 20 2f 2a 20 20 20 36 39 30    413,. /*   690
30b95 20 2a 2f 20 20 20 35 36 33 2c 20 20 34 31 33 2c   */   563,  413,
30b96 20 20 33 35 32 2c 20 20 20 20 34 2c 20 20 36 30    352,    4,  60
30b97 30 2c 20 20 20 39 38 2c 20 20 35 38 39 2c 20 20  0,   98,  589,  
30b98 35 38 33 2c 20 20 36 30 30 2c 20 20 31 30 36 2c  583,  600,  106,
30b99 0a 20 2f 2a 20 20 20 37 30 30 20 2a 2f 20 20 20  . /*   700 */   
30b9a 36 30 30 2c 20 20 31 30 34 2c 20 20 34 31 33 2c  600,  104,  413,
30b9b 20 20 31 37 34 2c 20 20 36 30 31 2c 20 20 36 32    174,  601,  62
30b9c 39 2c 20 20 36 32 37 2c 20 20 33 33 33 2c 20 20  9,  627,  333,  
30b9d 36 30 32 2c 20 20 20 33 34 2c 0a 20 2f 2a 20 20  602,   34,. /*  
30b9e 20 37 31 30 20 2a 2f 20 20 20 33 33 37 2c 20 20   710 */   337,  
30b9f 36 30 30 2c 20 20 31 30 38 2c 20 20 35 36 31 2c  600,  108,  561,
30ba0 20 20 33 35 39 2c 20 20 20 35 36 2c 20 20 20 35    359,   56,   5
30ba1 37 2c 20 20 20 34 37 2c 20 20 35 38 31 2c 20 20  7,   47,  581,  
30ba2 35 38 30 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a  580,. /*   720 *
30ba3 2f 20 20 20 35 38 32 2c 20 20 35 38 32 2c 20 20  /   582,  582,  
30ba4 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c   54,   54,   55,
30ba5 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
30ba6 35 2c 20 20 34 31 30 2c 20 20 20 35 33 2c 0a 20  5,  410,   53,. 
30ba7 2f 2a 20 20 20 37 33 30 20 2a 2f 20 20 20 20 35  /*   730 */    5
30ba8 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  3,   53,   53,  
30ba9 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c   52,   52,   51,
30baa 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
30bab 30 2c 20 20 32 33 37 2c 0a 20 2f 2a 20 20 20 37  0,  237,. /*   7
30bac 34 30 20 2a 2f 20 20 20 33 31 32 2c 20 20 34 31  40 */   312,  41
30bad 30 2c 20 20 34 39 39 2c 20 20 34 31 33 2c 20 20  0,  499,  413,  
30bae 31 36 37 2c 20 20 35 36 37 2c 20 20 34 30 35 2c  167,  567,  405,
30baf 20 20 32 31 35 2c 20 20 35 30 34 2c 20 20 35 30    215,  504,  50
30bb0 35 2c 0a 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20  5,. /*   750 */ 
30bb1 20 20 33 31 36 2c 20 20 35 35 37 2c 20 20 36 30    316,  557,  60
30bb2 30 2c 20 20 31 30 39 2c 20 20 33 35 33 2c 20 20  0,  109,  353,  
30bb3 20 31 33 2c 20 20 34 31 33 2c 20 20 34 31 30 2c   13,  413,  410,
30bb4 20 20 20 31 32 2c 20 20 34 31 30 2c 0a 20 2f 2a     12,  410,. /*
30bb5 20 20 20 37 36 30 20 2a 2f 20 20 20 35 33 38 2c     760 */   538,
30bb6 20 20 34 31 30 2c 20 20 33 33 35 2c 20 20 33 35    410,  335,  35
30bb7 38 2c 20 20 32 32 33 2c 20 20 36 30 30 2c 20 20  8,  223,  600,  
30bb8 31 33 34 2c 20 20 35 37 31 2c 20 20 35 37 31 2c  134,  571,  571,
30bb9 20 20 36 32 30 2c 0a 20 2f 2a 20 20 20 37 37 30    620,. /*   770
30bba 20 2a 2f 20 20 20 35 38 39 2c 20 20 35 38 33 2c   */   589,  583,
30bbb 20 20 34 31 33 2c 20 20 20 32 30 2c 20 20 34 31    413,   20,  41
30bbc 33 2c 20 20 36 32 30 2c 20 20 34 31 33 2c 20 20  3,  620,  413,  
30bbd 32 37 32 2c 20 20 36 32 30 2c 20 20 31 36 37 2c  272,  620,  167,
30bbe 0a 20 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20 20  . /*   780 */   
30bbf 31 36 37 2c 20 20 36 30 30 2c 20 20 31 33 35 2c  167,  600,  135,
30bc0 20 20 36 30 30 2c 20 20 20 36 31 2c 20 20 36 30    600,   61,  60
30bc1 30 2c 20 20 31 30 35 2c 20 20 33 31 37 2c 20 20  0,  105,  317,  
30bc2 31 34 38 2c 20 20 20 35 36 2c 0a 20 2f 2a 20 20  148,   56,. /*  
30bc3 20 37 39 30 20 2a 2f 20 20 20 20 35 37 2c 20 20   790 */    57,  
30bc4 20 34 37 2c 20 20 35 38 31 2c 20 20 35 38 30 2c   47,  581,  580,
30bc5 20 20 35 38 32 2c 20 20 35 38 32 2c 20 20 20 35    582,  582,   5
30bc6 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20  4,   54,   55,  
30bc7 20 35 35 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a   55,. /*   800 *
30bc8 2f 20 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  /    55,   55,  
30bc9 34 31 30 2c 20 20 20 35 33 2c 20 20 20 35 33 2c  410,   53,   53,
30bca 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
30bcb 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 0a 20  2,   52,   51,. 
30bcc 2f 2a 20 20 20 38 31 30 20 2a 2f 20 20 20 20 35  /*   810 */    5
30bcd 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20  1,   51,   50,  
30bce 32 33 37 2c 20 20 33 31 32 2c 20 20 34 31 30 2c  237,  312,  410,
30bcf 20 20 32 37 35 2c 20 20 34 31 33 2c 20 20 34 31    275,  413,  41
30bd0 30 2c 20 20 32 37 35 2c 0a 20 2f 2a 20 20 20 38  0,  275,. /*   8
30bd1 32 30 20 2a 2f 20 20 20 32 37 35 2c 20 20 32 32  20 */   275,  22
30bd2 32 2c 20 20 34 31 30 2c 20 20 33 33 30 2c 20 20  2,  410,  330,  
30bd3 33 36 33 2c 20 20 35 34 34 2c 20 20 36 30 30 2c  363,  544,  600,
30bd4 20 20 31 30 33 2c 20 20 31 33 32 2c 20 20 33 36    103,  132,  36
30bd5 30 2c 0a 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20  0,. /*   830 */ 
30bd6 20 20 34 31 33 2c 20 20 36 32 30 2c 20 20 35 32    413,  620,  52
30bd7 32 2c 20 20 34 31 33 2c 20 20 36 32 30 2c 20 20  2,  413,  620,  
30bd8 36 32 30 2c 20 20 34 31 30 2c 20 20 34 31 33 2c  620,  410,  413,
30bd9 20 20 31 37 30 2c 20 20 36 30 30 2c 0a 20 2f 2a    170,  600,. /*
30bda 20 20 20 38 34 30 20 2a 2f 20 20 20 20 39 36 2c     840 */    96,
30bdb 20 20 36 30 33 2c 20 20 36 30 30 2c 20 20 31 30    603,  600,  10
30bdc 32 2c 20 20 35 38 39 2c 20 20 35 38 33 2c 20 20  2,  589,  583,  
30bdd 36 30 30 2c 20 20 20 37 37 2c 20 20 33 37 34 2c  600,   77,  374,
30bde 20 20 35 33 36 2c 0a 20 2f 2a 20 20 20 38 35 30    536,. /*   850
30bdf 20 2a 2f 20 20 20 31 36 37 2c 20 20 34 31 33 2c   */   167,  413,
30be0 20 20 31 34 33 2c 20 20 33 32 35 2c 20 20 32 35    143,  325,  25
30be1 36 2c 20 20 20 32 38 2c 20 20 32 32 34 2c 20 20  6,   28,  224,  
30be2 33 32 34 2c 20 20 35 31 31 2c 20 20 35 32 38 2c  324,  511,  528,
30be3 0a 20 2f 2a 20 20 20 38 36 30 20 2a 2f 20 20 20  . /*   860 */   
30be4 36 30 30 2c 20 20 20 39 39 2c 20 20 35 32 37 2c  600,   99,  527,
30be5 20 20 20 35 36 2c 20 20 20 35 37 2c 20 20 20 34     56,   57,   4
30be6 37 2c 20 20 35 38 31 2c 20 20 35 38 30 2c 20 20  7,  581,  580,  
30be7 35 38 32 2c 20 20 35 38 32 2c 0a 20 2f 2a 20 20  582,  582,. /*  
30be8 20 38 37 30 20 2a 2f 20 20 20 20 35 34 2c 20 20   870 */    54,  
30be9 20 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   54,   55,   55,
30bea 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 34 31     55,   55,  41
30beb 30 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  0,   53,   53,  
30bec 20 35 33 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a   53,. /*   880 *
30bed 2f 20 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20  /    53,   52,  
30bee 20 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c   52,   51,   51,
30bef 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33     51,   50,  23
30bf0 37 2c 20 20 33 31 32 2c 20 20 34 31 30 2c 0a 20  7,  312,  410,. 
30bf1 2f 2a 20 20 20 38 39 30 20 2a 2f 20 20 20 32 37  /*   890 */   27
30bf2 35 2c 20 20 34 31 33 2c 20 20 34 31 30 2c 20 20  5,  413,  410,  
30bf3 34 36 39 2c 20 20 32 37 35 2c 20 20 31 36 37 2c  469,  275,  167,
30bf4 20 20 34 35 38 2c 20 20 20 33 39 2c 20 20 31 37    458,   39,  17
30bf5 31 2c 20 20 20 33 37 2c 0a 20 2f 2a 20 20 20 39  1,   37,. /*   9
30bf6 30 30 20 2a 2f 20 20 20 36 30 30 2c 20 20 31 33  00 */   600,  13
30bf7 38 2c 20 20 32 31 34 2c 20 20 31 34 34 2c 20 20  8,  214,  144,  
30bf8 34 31 33 2c 20 20 36 32 30 2c 20 20 31 34 32 2c  413,  620,  142,
30bf9 20 20 34 31 33 2c 20 20 34 31 30 2c 20 20 36 32    413,  410,  62
30bfa 30 2c 0a 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20  0,. /*   910 */ 
30bfb 20 20 34 31 30 2c 20 20 33 35 38 2c 20 20 32 33    410,  358,  23
30bfc 39 2c 20 20 36 30 30 2c 20 20 31 33 37 2c 20 20  9,  600,  137,  
30bfd 32 33 30 2c 20 20 36 30 30 2c 20 20 31 33 36 2c  230,  600,  136,
30bfe 20 20 35 38 39 2c 20 20 35 38 33 2c 0a 20 2f 2a    589,  583,. /*
30bff 20 20 20 39 32 30 20 2a 2f 20 20 20 34 35 37 2c     920 */   457,
30c00 20 20 32 36 33 2c 20 20 20 32 33 2c 20 20 34 31    263,   23,  41
30c01 33 2c 20 20 33 35 31 2c 20 20 34 31 33 2c 20 20  3,  351,  413,  
30c02 36 32 30 2c 20 20 33 32 33 2c 20 20 34 34 35 2c  620,  323,  445,
30c03 20 20 35 30 31 2c 0a 20 2f 2a 20 20 20 39 33 30    501,. /*   930
30c04 20 2a 2f 20 20 20 20 32 33 2c 20 20 33 32 32 2c   */    23,  322,
30c05 20 20 36 30 30 2c 20 20 20 37 36 2c 20 20 36 30    600,   76,  60
30c06 30 2c 20 20 20 39 33 2c 20 20 36 32 30 2c 20 20  0,   93,  620,  
30c07 20 35 36 2c 20 20 20 34 35 2c 20 20 20 34 37 2c   56,   45,   47,
30c08 0a 20 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20 20  . /*   940 */   
30c09 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c  581,  580,  582,
30c0a 20 20 35 38 32 2c 20 20 20 35 34 2c 20 20 20 35    582,   54,   5
30c0b 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  4,   55,   55,  
30c0c 20 35 35 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20   55,   55,. /*  
30c0d 20 39 35 30 20 2a 2f 20 20 20 34 31 30 2c 20 20   950 */   410,  
30c0e 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
30c0f 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35     53,   52,   5
30c10 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20  2,   51,   51,  
30c11 20 35 31 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a   51,. /*   960 *
30c12 2f 20 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20  /    50,  237,  
30c13 33 31 32 2c 20 20 34 31 30 2c 20 20 32 36 32 2c  312,  410,  262,
30c14 20 20 34 31 33 2c 20 20 34 31 30 2c 20 20 34 32    413,  410,  42
30c15 36 2c 20 20 32 36 33 2c 20 20 33 30 38 2c 0a 20  6,  263,  308,. 
30c16 2f 2a 20 20 20 39 37 30 20 2a 2f 20 20 20 32 30  /*   970 */   20
30c17 33 2c 20 20 32 31 33 2c 20 20 32 31 32 2c 20 20  3,  213,  212,  
30c18 33 38 30 2c 20 20 36 30 30 2c 20 20 20 39 32 2c  380,  600,   92,
30c19 20 20 35 32 30 2c 20 20 35 31 39 2c 20 20 34 31    520,  519,  41
30c1a 33 2c 20 20 31 33 30 2c 0a 20 2f 2a 20 20 20 39  3,  130,. /*   9
30c1b 38 30 20 2a 2f 20 20 20 35 33 38 2c 20 20 34 31  80 */   538,  41
30c1c 33 2c 20 20 35 33 38 2c 20 20 36 32 30 2c 20 20  3,  538,  620,  
30c1d 34 31 30 2c 20 20 36 32 38 2c 20 20 20 20 32 2c  410,  628,    2,
30c1e 20 20 36 30 30 2c 20 20 20 37 35 2c 20 20 32 37    600,   75,  27
30c1f 33 2c 0a 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20  3,. /*   990 */ 
30c20 20 20 36 30 30 2c 20 20 20 39 31 2c 20 20 35 38    600,   91,  58
30c21 39 2c 20 20 35 38 33 2c 20 20 33 37 35 2c 20 20  9,  583,  375,  
30c22 36 32 30 2c 20 20 31 32 39 2c 20 20 36 32 30 2c  620,  129,  620,
30c23 20 20 20 32 37 2c 20 20 34 31 33 2c 0a 20 2f 2a     27,  413,. /*
30c24 20 20 31 30 30 30 20 2a 2f 20 20 20 34 32 35 2c    1000 */   425,
30c25 20 20 33 30 37 2c 20 20 32 32 31 2c 20 20 31 32    307,  221,  12
30c26 38 2c 20 20 35 39 39 2c 20 20 35 39 39 2c 20 20  8,  599,  599,  
30c27 35 39 39 2c 20 20 32 38 31 2c 20 20 36 30 30 2c  599,  281,  600,
30c28 20 20 20 39 30 2c 0a 20 2f 2a 20 20 31 30 31 30     90,. /*  1010
30c29 20 2a 2f 20 20 20 33 37 31 2c 20 20 34 35 32 2c   */   371,  452,
30c2a 20 20 20 35 37 2c 20 20 20 34 37 2c 20 20 35 38     57,   47,  58
30c2b 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20  1,  580,  582,  
30c2c 35 38 32 2c 20 20 20 35 34 2c 20 20 20 35 34 2c  582,   54,   54,
30c2d 0a 20 2f 2a 20 20 31 30 32 30 20 2a 2f 20 20 20  . /*  1020 */   
30c2e 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   55,   55,   55,
30c2f 20 20 20 35 35 2c 20 20 34 31 30 2c 20 20 20 35     55,  410,   5
30c30 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  3,   53,   53,  
30c31 20 35 33 2c 20 20 20 35 32 2c 0a 20 2f 2a 20 20   53,   52,. /*  
30c32 31 30 33 30 20 2a 2f 20 20 20 20 35 32 2c 20 20  1030 */    52,  
30c33 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c   51,   51,   51,
30c34 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31     50,  237,  31
30c35 32 2c 20 20 34 31 30 2c 20 20 32 36 33 2c 20 20  2,  410,  263,  
30c36 34 31 33 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a  413,. /*  1040 *
30c37 2f 20 20 20 34 31 30 2c 20 20 32 36 33 2c 20 20  /   410,  263,  
30c38 32 36 33 2c 20 20 33 36 35 2c 20 20 32 30 38 2c  263,  365,  208,
30c39 20 20 33 32 31 2c 20 20 32 30 36 2c 20 20 35 34    321,  206,  54
30c3a 32 2c 20 20 36 30 30 2c 20 20 31 30 31 2c 0a 20  2,  600,  101,. 
30c3b 2f 2a 20 20 31 30 35 30 20 2a 2f 20 20 20 20 35  /*  1050 */    5
30c3c 30 2c 20 20 32 33 37 2c 20 20 34 31 33 2c 20 20  0,  237,  413,  
30c3d 36 32 30 2c 20 20 36 31 30 2c 20 20 34 31 33 2c  620,  610,  413,
30c3e 20 20 36 32 30 2c 20 20 36 32 30 2c 20 20 34 31    620,  620,  41
30c3f 30 2c 20 20 35 34 32 2c 0a 20 2f 2a 20 20 31 30  0,  542,. /*  10
30c40 36 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 36 30  60 */   165,  60
30c41 30 2c 20 20 20 38 39 2c 20 20 31 38 38 2c 20 20  0,   89,  188,  
30c42 36 30 30 2c 20 20 20 38 37 2c 20 20 35 38 39 2c  600,   87,  589,
30c43 20 20 35 38 33 2c 20 20 34 37 38 2c 20 20 36 32    583,  478,  62
30c44 30 2c 0a 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20  0,. /*  1070 */ 
30c45 20 20 34 36 37 2c 20 20 35 31 39 2c 20 20 31 32    467,  519,  12
30c46 35 2c 20 20 34 31 33 2c 20 20 35 36 39 2c 20 20  5,  413,  569,  
30c47 32 33 35 2c 20 20 35 34 32 2c 20 20 33 36 37 2c  235,  542,  367,
30c48 20 20 35 39 39 2c 20 20 34 37 35 2c 0a 20 2f 2a    599,  475,. /*
30c49 20 20 31 30 38 30 20 2a 2f 20 20 20 35 39 39 2c    1080 */   599,
30c4a 20 20 34 35 30 2c 20 20 36 30 30 2c 20 20 20 38    450,  600,   8
30c4b 36 2c 20 20 34 34 39 2c 20 20 34 34 38 2c 20 20  6,  449,  448,  
30c4c 32 33 31 2c 20 20 20 34 37 2c 20 20 35 38 31 2c  231,   47,  581,
30c4d 20 20 35 38 30 2c 0a 20 2f 2a 20 20 31 30 39 30    580,. /*  1090
30c4e 20 2a 2f 20 20 20 35 38 32 2c 20 20 35 38 32 2c   */   582,  582,
30c4f 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35     54,   54,   5
30c50 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  5,   55,   55,  
30c51 20 35 35 2c 20 20 32 38 37 2c 20 20 20 35 33 2c   55,  287,   53,
30c52 0a 20 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20 20  . /*  1100 */   
30c53 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
30c54 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35     52,   52,   5
30c55 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20  1,   51,   51,  
30c56 20 35 30 2c 20 20 32 33 37 2c 0a 20 2f 2a 20 20   50,  237,. /*  
30c57 31 31 31 30 20 2a 2f 20 20 20 20 34 33 2c 20 20  1110 */    43,  
30c58 34 30 34 2c 20 20 34 31 30 2c 20 20 20 20 33 2c  404,  410,    3,
30c59 20 20 34 31 30 2c 20 20 32 38 35 2c 20 20 32 36    410,  285,  26
30c5a 30 2c 20 20 34 31 34 2c 20 20 36 32 31 2c 20 20  0,  414,  621,  
30c5b 32 36 33 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a  263,. /*  1120 *
30c5c 2f 20 20 20 36 30 39 2c 20 20 36 32 37 2c 20 20  /   609,  627,  
30c5d 33 33 33 2c 20 20 20 34 33 2c 20 20 34 30 34 2c  333,   43,  404,
30c5e 20 20 34 31 30 2c 20 20 20 20 33 2c 20 20 34 31    410,    3,  41
30c5f 33 2c 20 20 34 30 37 2c 20 20 34 31 33 2c 0a 20  3,  407,  413,. 
30c60 2f 2a 20 20 31 31 33 30 20 2a 2f 20 20 20 34 31  /*  1130 */   41
30c61 34 2c 20 20 36 32 31 2c 20 20 31 37 31 2c 20 20  4,  621,  171,  
30c62 32 36 33 2c 20 20 36 32 30 2c 20 20 36 32 30 2c  263,  620,  620,
30c63 20 20 36 30 30 2c 20 20 20 38 35 2c 20 20 36 30    600,   85,  60
30c64 30 2c 20 20 20 37 32 2c 0a 20 2f 2a 20 20 31 31  0,   72,. /*  11
30c65 34 30 20 2a 2f 20 20 20 34 31 33 2c 20 20 34 30  40 */   413,  40
30c66 37 2c 20 20 31 32 34 2c 20 20 31 34 30 2c 20 20  7,  124,  140,  
30c67 33 35 33 2c 20 20 36 30 34 2c 20 20 34 30 39 2c  353,  604,  409,
30c68 20 20 34 30 32 2c 20 20 36 32 30 2c 20 20 36 30    402,  620,  60
30c69 30 2c 0a 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20  0,. /*  1150 */ 
30c6a 20 20 20 37 31 2c 20 20 32 39 31 2c 20 20 34 37     71,  291,  47
30c6b 31 2c 20 20 34 39 35 2c 20 20 31 36 30 2c 20 20  1,  495,  160,  
30c6c 31 32 33 2c 20 20 35 39 33 2c 20 20 35 36 35 2c  123,  593,  565,
30c6d 20 20 36 32 30 2c 20 20 36 32 30 2c 0a 20 2f 2a    620,  620,. /*
30c6e 20 20 31 31 36 30 20 2a 2f 20 20 20 34 30 32 2c    1160 */   402,
30c6f 20 20 36 32 30 2c 20 20 32 32 30 2c 20 20 20 31    620,  220,   1
30c70 35 2c 20 20 34 36 33 2c 20 20 34 36 30 2c 20 20  5,  463,  460,  
30c71 36 32 30 2c 20 20 34 31 37 2c 20 20 36 32 35 2c  620,  417,  625,
30c72 20 20 31 35 39 2c 0a 20 2f 2a 20 20 31 31 37 30    159,. /*  1170
30c73 20 2a 2f 20 20 20 35 36 35 2c 20 20 36 32 30 2c   */   565,  620,
30c74 20 20 33 39 39 2c 20 20 32 34 30 2c 20 20 31 35    399,  240,  15
30c75 38 2c 20 20 31 32 36 2c 20 20 32 31 39 2c 20 20  8,  126,  219,  
30c76 20 34 30 2c 20 20 20 34 31 2c 20 20 35 33 32 2c   40,   41,  532,
30c77 0a 20 2f 2a 20 20 31 31 38 30 20 2a 2f 20 20 20  . /*  1180 */   
30c78 34 31 30 2c 20 20 32 30 37 2c 20 20 31 32 31 2c  410,  207,  121,
30c79 20 20 31 32 30 2c 20 20 20 34 32 2c 20 20 34 31    120,   42,  41
30c7a 32 2c 20 20 34 31 31 2c 20 20 36 32 30 2c 20 20  2,  411,  620,  
30c7b 32 36 33 2c 20 20 35 39 34 2c 0a 20 2f 2a 20 20  263,  594,. /*  
30c7c 31 31 39 30 20 2a 2f 20 20 20 20 34 30 2c 20 20  1190 */    40,  
30c7d 20 34 31 2c 20 20 35 35 36 2c 20 20 35 34 33 2c   41,  556,  543,
30c7e 20 20 20 32 35 2c 20 20 34 31 33 2c 20 20 20 31     25,  413,   1
30c7f 31 2c 20 20 20 34 32 2c 20 20 34 31 32 2c 20 20  1,   42,  412,  
30c80 34 31 31 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a  411,. /*  1200 *
30c81 2f 20 20 20 20 32 34 2c 20 20 31 31 38 2c 20 20  /    24,  118,  
30c82 35 39 34 2c 20 20 36 32 30 2c 20 20 36 30 30 2c  594,  620,  600,
30c83 20 20 20 38 34 2c 20 20 34 35 35 2c 20 20 36 32     84,  455,  62
30c84 30 2c 20 20 36 32 30 2c 20 20 20 34 33 2c 0a 20  0,  620,   43,. 
30c85 2f 2a 20 20 31 32 31 30 20 2a 2f 20 20 20 34 30  /*  1210 */   40
30c86 34 2c 20 20 32 31 38 2c 20 20 20 20 33 2c 20 20  4,  218,    3,  
30c87 35 33 39 2c 20 20 31 35 36 2c 20 20 35 39 39 2c  539,  156,  599,
30c88 20 20 34 31 34 2c 20 20 36 32 31 2c 20 20 31 31    414,  621,  11
30c89 33 2c 20 20 32 33 39 2c 0a 20 2f 2a 20 20 31 32  3,  239,. /*  12
30c8a 32 30 20 2a 2f 20 20 20 35 39 32 2c 20 20 35 39  20 */   592,  59
30c8b 32 2c 20 20 35 39 32 2c 20 20 35 39 31 2c 20 20  2,  592,  591,  
30c8c 35 39 30 2c 20 20 20 31 34 2c 20 20 31 35 35 2c  590,   14,  155,
30c8d 20 20 34 30 37 2c 20 20 36 32 30 2c 20 20 35 33    407,  620,  53
30c8e 37 2c 0a 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20  7,. /*  1230 */ 
30c8f 20 20 34 35 31 2c 20 20 32 34 37 2c 20 20 34 34    451,  247,  44
30c90 34 2c 20 20 35 39 32 2c 20 20 35 39 32 2c 20 20  4,  592,  592,  
30c91 35 39 32 2c 20 20 35 39 31 2c 20 20 35 39 30 2c  592,  591,  590,
30c92 20 20 20 31 34 2c 20 20 33 34 33 2c 0a 20 2f 2a     14,  343,. /*
30c93 20 20 31 32 34 30 20 2a 2f 20 20 20 34 31 30 2c    1240 */   410,
30c94 20 20 31 31 31 2c 20 20 34 31 30 2c 20 20 32 37    111,  410,  27
30c95 37 2c 20 20 36 32 30 2c 20 20 34 31 30 2c 20 20  7,  620,  410,  
30c96 34 30 32 2c 20 20 34 31 30 2c 20 20 35 30 37 2c  402,  410,  507,
30c97 20 20 31 31 30 2c 0a 20 2f 2a 20 20 31 32 35 30    110,. /*  1250
30c98 20 2a 2f 20 20 20 20 31 30 2c 20 20 20 36 34 2c   */    10,   64,
30c99 20 20 32 30 34 2c 20 20 33 33 36 2c 20 20 34 33    204,  336,  43
30c9a 35 2c 20 20 34 31 33 2c 20 20 35 36 35 2c 20 20  5,  413,  565,  
30c9b 34 31 33 2c 20 20 36 32 30 2c 20 20 32 37 36 2c  413,  620,  276,
30c9c 0a 20 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20 20  . /*  1260 */   
30c9d 34 31 33 2c 20 20 34 33 34 2c 20 20 34 31 33 2c  413,  434,  413,
30c9e 20 20 36 32 30 2c 20 20 36 30 30 2c 20 20 20 38    620,  600,   8
30c9f 33 2c 20 20 36 30 30 2c 20 20 20 39 35 2c 20 20  3,  600,   95,  
30ca0 33 33 34 2c 20 20 36 30 30 2c 0a 20 2f 2a 20 20  334,  600,. /*  
30ca1 31 32 37 30 20 2a 2f 20 20 20 20 38 32 2c 20 20  1270 */    82,  
30ca2 36 30 30 2c 20 20 20 38 31 2c 20 20 31 35 30 2c  600,   81,  150,
30ca3 20 20 36 32 30 2c 20 20 34 38 38 2c 20 20 20 34    620,  488,   4
30ca4 30 2c 20 20 20 34 31 2c 20 20 32 37 30 2c 20 20  0,   41,  270,  
30ca5 32 36 38 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a  268,. /*  1280 *
30ca6 2f 20 20 20 32 36 36 2c 20 20 31 39 31 2c 20 20  /   266,  191,  
30ca7 33 33 32 2c 20 20 20 34 32 2c 20 20 34 31 32 2c  332,   42,  412,
30ca8 20 20 34 31 31 2c 20 20 35 39 39 2c 20 20 34 31    411,  599,  41
30ca9 30 2c 20 20 35 39 34 2c 20 20 32 34 31 2c 0a 20  0,  594,  241,. 
30caa 2f 2a 20 20 31 32 39 30 20 2a 2f 20 20 20 36 32  /*  1290 */   62
30cab 30 2c 20 20 34 31 30 2c 20 20 32 36 34 2c 20 20  0,  410,  264,  
30cac 36 32 30 2c 20 20 36 32 30 2c 20 20 36 32 30 2c  620,  620,  620,
30cad 20 20 20 33 33 2c 20 20 34 30 34 2c 20 20 34 31     33,  404,  41
30cae 39 2c 20 20 20 20 33 2c 0a 20 2f 2a 20 20 31 33  9,    3,. /*  13
30caf 30 30 20 2a 2f 20 20 20 31 30 37 2c 20 20 32 32  00 */   107,  22
30cb0 39 2c 20 20 34 31 33 2c 20 20 34 31 34 2c 20 20  9,  413,  414,  
30cb1 36 32 31 2c 20 20 31 34 39 2c 20 20 34 31 33 2c  621,  149,  413,
30cb2 20 20 36 32 30 2c 20 20 33 39 37 2c 20 20 31 38    620,  397,  18
30cb3 31 2c 0a 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20  1,. /*  1310 */ 
30cb4 20 20 32 35 39 2c 20 20 36 30 30 2c 20 20 20 37    259,  600,   7
30cb5 30 2c 20 20 33 39 38 2c 20 20 34 30 37 2c 20 20  0,  398,  407,  
30cb6 36 30 30 2c 20 20 20 31 37 2c 20 20 33 31 35 2c  600,   17,  315,
30cb7 20 20 33 31 34 2c 20 20 35 39 32 2c 0a 20 2f 2a    314,  592,. /*
30cb8 20 20 31 33 32 30 20 2a 2f 20 20 20 35 39 32 2c    1320 */   592,
30cb9 20 20 35 39 32 2c 20 20 35 39 31 2c 20 20 35 39    592,  591,  59
30cba 30 2c 20 20 20 31 34 2c 20 20 36 32 30 2c 20 20  0,   14,  620,  
30cbb 31 32 37 2c 20 20 33 36 31 2c 20 20 36 32 34 2c  127,  361,  624,
30cbc 20 20 32 31 37 2c 0a 20 2f 2a 20 20 31 33 33 30    217,. /*  1330
30cbd 20 2a 2f 20 20 20 34 36 32 2c 20 20 34 36 31 2c   */   462,  461,
30cbe 20 20 33 35 34 2c 20 20 34 30 32 2c 20 20 33 30    354,  402,  30
30cbf 34 2c 20 20 33 30 33 2c 20 20 33 30 32 2c 20 20  4,  303,  302,  
30cc0 31 37 39 2c 20 20 33 30 30 2c 20 20 32 35 34 2c  179,  300,  254,
30cc1 0a 20 2f 2a 20 20 31 33 34 30 20 2a 2f 20 20 20  . /*  1340 */   
30cc2 36 31 34 2c 20 20 34 35 33 2c 20 20 36 32 30 2c  614,  453,  620,
30cc3 20 20 35 36 35 2c 20 20 34 35 34 2c 20 20 36 32    565,  454,  62
30cc4 30 2c 20 20 36 32 30 2c 20 20 36 32 30 2c 20 20  0,  620,  620,  
30cc5 36 31 33 2c 20 20 36 31 32 2c 0a 20 2f 2a 20 20  613,  612,. /*  
30cc6 31 33 35 30 20 2a 2f 20 20 20 34 34 33 2c 20 20  1350 */   443,  
30cc7 34 31 36 2c 20 20 31 38 30 2c 20 20 32 34 36 2c  416,  180,  246,
30cc8 20 20 36 32 30 2c 20 20 31 35 31 2c 20 20 34 31    620,  151,  41
30cc9 35 2c 20 20 32 34 35 2c 20 20 32 34 33 2c 20 20  5,  245,  243,  
30cca 36 32 30 2c 0a 20 2f 2a 20 20 31 33 36 30 20 2a  620,. /*  1360 *
30ccb 2f 20 20 20 31 37 38 2c 20 20 35 39 38 2c 20 20  /   178,  598,  
30ccc 32 34 32 2c 20 20 20 34 30 2c 20 20 20 34 31 2c  242,   40,   41,
30ccd 20 20 36 32 30 2c 20 20 32 34 34 2c 20 20 20 20    620,  244,    
30cce 38 2c 20 20 36 32 30 2c 20 20 32 33 39 2c 0a 20  8,  620,  239,. 
30ccf 2f 2a 20 20 31 33 37 30 20 2a 2f 20 20 20 20 34  /*  1370 */    4
30cd0 32 2c 20 20 34 31 32 2c 20 20 34 31 31 2c 20 20  2,  412,  411,  
30cd1 36 32 30 2c 20 20 34 31 30 2c 20 20 35 39 34 2c  620,  410,  594,
30cd2 20 20 34 31 30 2c 20 20 36 32 30 2c 20 20 20 36    410,  620,   6
30cd3 30 2c 20 20 31 35 33 2c 0a 20 2f 2a 20 20 31 33  0,  153,. /*  13
30cd4 38 30 20 2a 2f 20 20 20 34 32 39 2c 20 20 34 36  80 */   429,  46
30cd5 35 2c 20 20 36 32 32 2c 20 20 36 32 31 2c 20 20  5,  622,  621,  
30cd6 32 39 36 2c 20 20 31 35 34 2c 20 20 20 33 30 2c  296,  154,   30,
30cd7 20 20 31 34 35 2c 20 20 31 35 32 2c 20 20 34 31    145,  152,  41
30cd8 33 2c 0a 20 2f 2a 20 20 31 33 39 30 20 2a 2f 20  3,. /*  1390 */ 
30cd9 20 20 33 38 38 2c 20 20 34 31 33 2c 20 20 32 39    388,  413,  29
30cda 35 2c 20 20 33 39 34 2c 20 20 32 39 34 2c 20 20  5,  394,  294,  
30cdb 36 32 30 2c 20 20 20 33 31 2c 20 20 33 39 32 2c  620,   31,  392,
30cdc 20 20 36 30 30 2c 20 20 20 37 39 2c 0a 20 2f 2a    600,   79,. /*
30cdd 20 20 31 34 30 30 20 2a 2f 20 20 20 36 30 30 2c    1400 */   600,
30cde 20 20 20 37 38 2c 20 20 36 32 30 2c 20 20 32 39     78,  620,  29
30cdf 30 2c 20 20 33 39 36 2c 20 20 34 31 33 2c 20 20  0,  396,  413,  
30ce0 35 39 32 2c 20 20 35 39 32 2c 20 20 35 39 32 2c  592,  592,  592,
30ce1 20 20 35 39 31 2c 0a 20 2f 2a 20 20 31 34 31 30    591,. /*  1410
30ce2 20 2a 2f 20 20 20 35 39 30 2c 20 20 20 31 34 2c   */   590,   14,
30ce3 20 20 36 32 30 2c 20 20 32 39 33 2c 20 20 36 30    620,  293,  60
30ce4 30 2c 20 20 20 20 39 2c 20 20 35 39 37 2c 20 20  0,    9,  597,  
30ce5 20 35 39 2c 20 20 36 32 30 2c 20 20 20 33 36 2c   59,  620,   36,
30ce6 0a 20 2f 2a 20 20 31 34 32 30 20 2a 2f 20 20 20  . /*  1420 */   
30ce7 35 35 35 2c 20 20 31 37 33 2c 20 20 35 36 35 2c  555,  173,  565,
30ce8 20 20 32 33 34 2c 20 20 31 38 35 2c 20 20 32 38    234,  185,  28
30ce9 38 2c 20 20 20 32 39 2c 20 20 35 34 31 2c 20 20  8,   29,  541,  
30cea 33 39 31 2c 20 20 33 34 35 2c 0a 20 2f 2a 20 20  391,  345,. /*  
30ceb 31 34 33 30 20 2a 2f 20 20 20 32 34 38 2c 20 20  1430 */   248,  
30cec 32 38 36 2c 20 20 35 32 31 2c 20 20 35 33 35 2c  286,  521,  535,
30ced 20 20 33 31 33 2c 20 20 32 38 34 2c 20 20 33 38    313,  284,  38
30cee 35 2c 20 20 33 32 38 2c 20 20 35 33 34 2c 20 20  5,  328,  534,  
30cef 32 33 39 2c 0a 20 2f 2a 20 20 31 34 34 30 20 2a  239,. /*  1440 *
30cf0 2f 20 20 20 35 31 36 2c 20 20 35 31 35 2c 20 20  /   516,  515,  
30cf1 31 39 36 2c 20 20 31 39 35 2c 20 20 32 37 39 2c  196,  195,  279,
30cf2 20 20 33 31 30 2c 20 20 35 31 31 2c 20 20 35 31    310,  511,  51
30cf3 32 2c 20 20 35 31 30 2c 20 20 31 33 31 2c 0a 20  2,  510,  131,. 
30cf4 2f 2a 20 20 31 34 35 30 20 2a 2f 20 20 20 35 32  /*  1450 */   52
30cf5 34 2c 20 20 32 32 37 2c 20 20 32 35 38 2c 20 20  4,  227,  258,  
30cf6 32 32 38 2c 20 20 35 39 34 2c 20 20 33 30 39 2c  228,  594,  309,
30cf7 20 20 34 38 37 2c 20 20 34 38 36 2c 20 20 34 39    487,  486,  49
30cf8 33 2c 20 20 32 32 36 2c 0a 20 2f 2a 20 20 31 34  3,  226,. /*  14
30cf9 36 30 20 2a 2f 20 20 20 33 37 32 2c 20 20 34 38  60 */   372,  48
30cfa 35 2c 20 20 31 36 34 2c 20 20 33 33 38 2c 20 20  5,  164,  338,  
30cfb 34 37 39 2c 20 20 31 36 33 2c 20 20 33 36 38 2c  479,  163,  368,
30cfc 20 20 33 37 30 2c 20 20 31 36 32 2c 20 20 20 32    370,  162,   2
30cfd 36 2c 0a 20 2f 2a 20 20 31 34 37 30 20 2a 2f 20  6,. /*  1470 */ 
30cfe 20 20 32 31 31 2c 20 20 34 37 37 2c 20 20 32 36    211,  477,  26
30cff 31 2c 20 20 31 36 31 2c 20 20 31 34 31 2c 20 20  1,  161,  141,  
30d00 34 37 36 2c 20 20 33 36 32 2c 20 20 34 36 36 2c  476,  362,  466,
30d01 20 20 31 32 32 2c 20 20 31 38 37 2c 0a 20 2f 2a    122,  187,. /*
30d02 20 20 31 34 38 30 20 2a 2f 20 20 20 31 31 39 2c    1480 */   119,
30d03 20 20 34 35 36 2c 20 20 33 34 37 2c 20 20 31 31    456,  347,  11
30d04 37 2c 20 20 33 34 36 2c 20 20 35 39 32 2c 20 20  7,  346,  592,  
30d05 35 39 32 2c 20 20 35 39 32 2c 20 20 31 31 36 2c  592,  592,  116,
30d06 20 20 31 31 35 2c 0a 20 2f 2a 20 20 31 34 39 30    115,. /*  1490
30d07 20 2a 2f 20 20 20 31 31 34 2c 20 20 34 34 37 2c   */   114,  447,
30d08 20 20 31 31 32 2c 20 20 31 38 32 2c 20 20 33 31    112,  182,  31
30d09 38 2c 20 20 20 32 32 2c 20 20 34 33 32 2c 20 20  8,   22,  432,  
30d0a 20 31 39 2c 20 20 34 33 31 2c 20 20 34 33 30 2c   19,  431,  430,
30d0b 0a 20 2f 2a 20 20 31 35 30 30 20 2a 2f 20 20 20  . /*  1500 */   
30d0c 20 36 33 2c 20 20 34 32 37 2c 20 20 36 30 38 2c   63,  427,  608,
30d0d 20 20 31 39 33 2c 20 20 32 39 37 2c 20 20 35 39    193,  297,  59
30d0e 35 2c 20 20 35 37 32 2c 20 20 35 37 30 2c 20 20  5,  572,  570,  
30d0f 34 30 33 2c 20 20 35 35 33 2c 0a 20 2f 2a 20 20  403,  553,. /*  
30d10 31 35 31 30 20 2a 2f 20 20 20 35 35 30 2c 20 20  1510 */   550,  
30d11 32 38 39 2c 20 20 32 38 30 2c 20 20 35 30 38 2c  289,  280,  508,
30d12 20 20 34 39 38 2c 20 20 34 39 37 2c 20 20 34 39    498,  497,  49
30d13 36 2c 20 20 34 39 34 2c 20 20 33 37 39 2c 20 20  6,  494,  379,  
30d14 33 35 35 2c 0a 20 2f 2a 20 20 31 35 32 30 20 2a  355,. /*  1520 *
30d15 2f 20 20 20 34 35 39 2c 20 20 32 35 35 2c 20 20  /   459,  255,  
30d16 32 34 39 2c 20 20 33 34 34 2c 20 20 34 34 36 2c  249,  344,  446,
30d17 20 20 33 30 35 2c 20 20 20 20 35 2c 20 20 35 36    305,    5,  56
30d18 38 2c 20 20 35 34 38 2c 20 20 32 39 38 2c 0a 20  8,  548,  298,. 
30d19 2f 2a 20 20 31 35 33 30 20 2a 2f 20 20 20 32 39  /*  1530 */   29
30d1a 38 2c 20 20 32 31 30 2c 20 20 33 36 39 2c 20 20  8,  210,  369,  
30d1b 32 39 38 2c 20 20 34 30 30 2c 20 20 35 30 36 2c  298,  400,  506,
30d1c 20 20 35 30 30 2c 20 20 34 38 39 2c 20 20 35 32    500,  489,  52
30d1d 35 2c 20 20 35 32 33 2c 0a 20 2f 2a 20 20 31 35  5,  523,. /*  15
30d1e 34 30 20 2a 2f 20 20 20 34 38 32 2c 20 20 32 33  40 */   482,  23
30d1f 39 2c 20 20 32 33 37 2c 0a 7d 3b 0a 73 74 61 74  9,  237,.};.stat
30d20 69 63 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54  ic const YYCODET
30d21 59 50 45 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64  YPE yy_lookahead
30d22 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30  [] = {. /*     0
30d23 20 2a 2f 20 20 20 20 31 39 2c 20 20 32 32 32 2c   */    19,  222,
30d24 20 20 32 32 33 2c 20 20 32 32 34 2c 20 20 32 32    223,  224,  22
30d25 35 2c 20 20 20 32 34 2c 20 20 20 20 31 2c 20 20  5,   24,    1,  
30d26 20 32 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   26,   77,   78,
30d27 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20  . /*    10 */   
30d28 20 37 39 2c 20 20 20 38 30 2c 20 20 20 31 35 2c   79,   80,   15,
30d29 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
30d2a 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
30d2b 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20   87,   88,. /*  
30d2c 20 20 32 30 20 2a 2f 20 20 20 20 38 39 2c 20 20    20 */    89,  
30d2d 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   90,   91,   92,
30d2e 20 20 20 20 37 2c 20 20 20 20 38 2c 20 20 20 20      7,    8,    
30d2f 39 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  9,   26,   27,  
30d30 20 32 33 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a   23,. /*    30 *
30d31 2f 20 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  /    49,   50,  
30d32 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
30d33 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
30d34 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20  6,   87,   88,. 
30d35 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 20 38  /*    40 */    8
30d36 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20  9,   90,   91,  
30d37 20 39 32 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   92,   88,   89,
30d38 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
30d39 32 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 20  2,   68,. /*    
30d3a 35 30 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37  50 */    69,   7
30d3b 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
30d3c 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
30d3d 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
30d3e 38 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20  8,. /*    60 */ 
30d3f 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 32     79,   80,   2
30d40 32 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  2,   82,   83,  
30d41 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
30d42 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a     87,   88,. /*
30d43 20 20 20 20 37 30 20 2a 2f 20 20 20 20 38 39 2c      70 */    89,
30d44 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
30d45 32 2c 20 20 20 31 39 2c 20 20 20 39 34 2c 20 20  2,   19,   94,  
30d46 32 32 32 2c 20 20 20 31 39 2c 20 20 32 32 34 2c  222,   19,  224,
30d47 20 20 32 32 35 2c 0a 20 2f 2a 20 20 20 20 38 30    225,. /*    80
30d48 20 2a 2f 20 20 20 20 32 35 2c 20 20 20 38 32 2c   */    25,   82,
30d49 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
30d4a 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
30d4b 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
30d4c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20  . /*    90 */   
30d4d 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c   91,   92,   19,
30d4e 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 39     26,   27,   9
30d4f 34 2c 20 20 20 39 35 2c 20 20 20 39 36 2c 20 20  4,   95,   96,  
30d50 32 34 34 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20  244,   25,. /*  
30d51 20 31 30 30 20 2a 2f 20 20 20 20 39 39 2c 20 20   100 */    99,  
30d52 31 30 30 2c 20 20 31 30 31 2c 20 20 20 32 35 2c  100,  101,   25,
30d53 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 20 31     49,   50,   1
30d54 39 2c 20 20 31 35 30 2c 20 20 20 39 36 2c 20 20  9,  150,   96,  
30d55 20 35 34 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a   54,. /*   110 *
30d56 2f 20 20 20 31 30 39 2c 20 20 20 39 39 2c 20 20  /   109,   99,  
30d57 31 30 30 2c 20 20 31 30 31 2c 20 20 20 32 37 2c  100,  101,   27,
30d58 20 20 31 35 38 2c 20 20 20 32 36 2c 20 20 20 32    158,   26,   2
30d59 37 2c 20 20 31 36 31 2c 20 20 31 36 32 2c 0a 20  7,  161,  162,. 
30d5a 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 31 31  /*   120 */   11
30d5b 37 2c 20 20 31 30 39 2c 20 20 31 36 35 2c 20 20  7,  109,  165,  
30d5c 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
30d5d 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
30d5e 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 31  3,   74,. /*   1
30d5f 33 30 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37  30 */    75,   7
30d60 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  6,   77,   78,  
30d61 20 37 39 2c 20 20 20 38 30 2c 20 20 20 31 36 2c   79,   80,   16,
30d62 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
30d63 34 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20  4,. /*   140 */ 
30d64 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
30d65 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
30d66 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   90,   91,   92,
30d67 20 20 20 31 39 2c 20 20 31 31 31 2c 0a 20 2f 2a     19,  111,. /*
30d68 20 20 20 31 35 30 20 2a 2f 20 20 20 20 39 32 2c     150 */    92,
30d69 20 20 31 31 33 2c 20 20 31 31 34 2c 20 20 20 32    113,  114,   2
30d6a 34 2c 20 20 20 39 36 2c 20 20 20 39 37 2c 20 20  4,   96,   97,  
30d6b 20 39 38 2c 20 20 20 39 39 2c 20 20 31 30 30 2c   98,   99,  100,
30d6c 20 20 31 30 31 2c 0a 20 2f 2a 20 20 20 31 36 30    101,. /*   160
30d6d 20 2a 2f 20 20 20 31 30 32 2c 20 20 20 39 34 2c   */   102,   94,
30d6e 20 20 20 39 35 2c 20 20 31 31 32 2c 20 20 20 32     95,  112,   2
30d6f 35 2c 20 20 20 39 32 2c 20 20 31 30 38 2c 20 20  5,   92,  108,  
30d70 31 31 36 2c 20 20 31 35 30 2c 20 20 20 39 35 2c  116,  150,   95,
30d71 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20  . /*   170 */   
30d72 20 39 37 2c 20 20 20 39 38 2c 20 20 20 39 39 2c   97,   98,   99,
30d73 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20 31 30    100,  101,  10
30d74 32 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  2,   86,   87,  
30d75 20 34 39 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20   49,   50,. /*  
30d76 20 31 38 30 20 2a 2f 20 20 20 20 36 30 2c 20 20   180 */    60,  
30d77 31 30 38 2c 20 20 20 36 32 2c 20 20 31 36 35 2c  108,   62,  165,
30d78 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31 31     94,   95,  11
30d79 39 2c 20 20 20 39 37 2c 20 20 31 37 30 2c 20 20  9,   97,  170,  
30d7a 31 37 31 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a  171,. /*   190 *
30d7b 2f 20 20 20 20 35 30 2c 20 20 20 32 36 2c 20 20  /    50,   26,  
30d7c 20 32 37 2c 20 20 31 31 39 2c 20 20 31 30 34 2c   27,  119,  104,
30d7d 20 20 31 30 35 2c 20 20 31 31 38 2c 20 20 20 36    105,  118,   6
30d7e 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20  8,   69,   70,. 
30d7f 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 20 37  /*   200 */    7
30d80 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
30d81 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
30d82 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
30d83 39 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 32  9,   80,. /*   2
30d84 31 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 38  10 */    22,   8
30d85 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
30d86 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
30d87 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
30d88 30 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20  0,. /*   220 */ 
30d89 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31     91,   92,   1
30d8a 39 2c 20 20 20 39 38 2c 20 20 20 38 36 2c 20 20  9,   98,   86,  
30d8b 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
30d8c 20 20 20 39 30 2c 20 20 20 39 31 2c 0a 20 2f 2a     90,   91,. /*
30d8d 20 20 20 32 33 30 20 2a 2f 20 20 20 20 39 32 2c     230 */    92,
30d8e 20 20 31 35 30 2c 20 20 20 32 33 2c 20 20 31 30    150,   23,  10
30d8f 38 2c 20 20 20 32 35 2c 20 20 20 39 36 2c 20 20  8,   25,   96,  
30d90 31 35 30 2c 20 20 20 36 36 2c 20 20 20 39 39 2c  150,   66,   99,
30d91 20 20 31 30 30 2c 0a 20 2f 2a 20 20 20 32 34 30    100,. /*   240
30d92 20 2a 2f 20 20 20 31 30 31 2c 20 20 20 32 36 2c   */   101,   26,
30d93 20 20 31 36 31 2c 20 20 31 36 32 2c 20 20 31 30    161,  162,  10
30d94 34 2c 20 20 20 32 34 2c 20 20 31 36 35 2c 20 20  4,   24,  165,  
30d95 20 32 36 2c 20 20 31 30 39 2c 20 20 32 33 31 2c   26,  109,  231,
30d96 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20  . /*   250 */   
30d97 32 33 32 2c 20 20 31 36 35 2c 20 20 20 34 39 2c  232,  165,   49,
30d98 20 20 20 35 30 2c 20 20 20 32 32 2c 20 20 20 32     50,   22,   2
30d99 33 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20  3,  170,  171,  
30d9a 31 33 38 2c 20 20 20 39 34 2c 0a 20 2f 2a 20 20  138,   94,. /*  
30d9b 20 32 36 30 20 2a 2f 20 20 20 20 39 35 2c 20 20   260 */    95,  
30d9c 31 36 39 2c 20 20 31 37 30 2c 20 20 31 37 31 2c  169,  170,  171,
30d9d 20 20 20 37 36 2c 20 20 20 39 34 2c 20 20 31 38     76,   94,  18
30d9e 35 2c 20 20 31 38 36 2c 20 20 20 32 32 2c 20 20  5,  186,   22,  
30d9f 20 39 38 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a   98,. /*   270 *
30da0 2f 20 20 20 20 32 34 2c 20 20 20 36 38 2c 20 20  /    24,   68,  
30da1 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
30da2 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
30da3 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20  4,   75,   76,. 
30da4 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 20 20 37  /*   280 */    7
30da5 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
30da6 20 38 30 2c 20 20 31 31 39 2c 20 20 20 38 32 2c   80,  119,   82,
30da7 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
30da8 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20 32  5,   86,. /*   2
30da9 39 30 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38  90 */    87,   8
30daa 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  8,   89,   90,  
30dab 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c   91,   92,   19,
30dac 20 20 31 35 30 2c 20 20 20 31 31 2c 20 20 20 36    150,   11,   6
30dad 37 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20  7,. /*   300 */ 
30dae 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33    129,  130,  13
30daf 31 2c 20 20 31 31 37 2c 20 20 20 39 35 2c 20 20  1,  117,   95,  
30db0 31 35 30 2c 20 20 20 32 32 2c 20 20 31 36 30 2c  150,   22,  160,
30db1 20 20 31 35 30 2c 20 20 20 39 34 2c 0a 20 2f 2a    150,   94,. /*
30db2 20 20 20 33 31 30 20 2a 2f 20 20 20 20 32 36 2c     310 */    26,
30db3 20 20 20 32 37 2c 20 20 31 36 35 2c 20 20 20 32     27,  165,   2
30db4 32 2c 20 20 20 32 33 2c 20 20 20 39 34 2c 20 20  2,   23,   94,  
30db5 20 32 35 2c 20 20 32 33 31 2c 20 20 32 33 32 2c   25,  231,  232,
30db6 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20 33 32 30     23,. /*   320
30db7 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 37 34 2c   */   165,  174,
30db8 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20 32 33    175,  165,  23
30db9 32 2c 20 20 20 33 32 2c 20 20 20 34 39 2c 20 20  2,   32,   49,  
30dba 20 35 30 2c 20 20 31 31 39 2c 20 20 31 37 34 2c   50,  119,  174,
30dbb 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20  . /*   330 */   
30dbc 31 37 35 2c 20 20 20 32 32 2c 20 20 31 37 34 2c  175,   22,  174,
30dbd 20 20 31 37 35 2c 20 20 20 34 31 2c 20 20 20 32    175,   41,   2
30dbe 36 2c 20 20 20 32 37 2c 20 20 20 35 37 2c 20 20  6,   27,   57,  
30dbf 31 36 36 2c 20 20 31 33 36 2c 0a 20 2f 2a 20 20  166,  136,. /*  
30dc0 20 33 34 30 20 2a 2f 20 20 20 32 32 32 2c 20 20   340 */   222,  
30dc1 31 39 34 2c 20 20 32 32 34 2c 20 20 32 32 35 2c  194,  224,  225,
30dc2 20 20 20 35 37 2c 20 20 20 36 38 2c 20 20 20 36     57,   68,   6
30dc3 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  9,   70,   71,  
30dc4 20 37 32 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a   72,. /*   350 *
30dc5 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  /    73,   74,  
30dc6 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
30dc7 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
30dc8 30 2c 20 20 20 36 37 2c 20 20 20 38 32 2c 0a 20  0,   67,   82,. 
30dc9 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 20 20 38  /*   360 */    8
30dca 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
30dcb 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
30dcc 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39     89,   90,   9
30dcd 31 2c 20 20 20 39 32 2c 0a 20 2f 2a 20 20 20 33  1,   92,. /*   3
30dce 37 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 32 31  70 */    19,  21
30dcf 36 2c 20 20 32 31 34 2c 20 20 32 31 35 2c 20 20  6,  214,  215,  
30dd0 20 32 33 2c 20 20 20 32 33 2c 20 20 32 32 31 2c   23,   23,  221,
30dd1 20 20 32 30 35 2c 20 20 20 39 34 2c 20 20 20 39    205,   94,   9
30dd2 35 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20  5,. /*   380 */ 
30dd3 20 20 31 37 32 2c 20 20 31 37 33 2c 20 20 20 31    172,  173,   1
30dd4 32 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  2,   49,   50,  
30dd5 31 30 35 2c 20 20 31 30 36 2c 20 20 20 32 36 2c  105,  106,   26,
30dd6 20 20 20 32 37 2c 20 20 31 38 31 2c 0a 20 2f 2a     27,  181,. /*
30dd7 20 20 20 33 39 30 20 2a 2f 20 20 20 20 32 33 2c     390 */    23,
30dd8 20 20 20 31 39 2c 20 20 31 30 35 2c 20 20 31 30     19,  105,  10
30dd9 36 2c 20 20 32 32 32 2c 20 20 31 31 35 2c 20 20  6,  222,  115,  
30dda 32 32 34 2c 20 20 32 32 35 2c 20 20 20 32 38 2c  224,  225,   28,
30ddb 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20 34 30 30     27,. /*   400
30ddc 20 2a 2f 20 20 20 20 34 39 2c 20 20 20 35 30 2c   */    49,   50,
30ddd 20 20 31 31 35 2c 20 20 20 39 34 2c 20 20 20 39    115,   94,   9
30dde 35 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  5,   71,   72,  
30ddf 31 31 31 2c 20 20 31 32 38 2c 20 20 31 31 33 2c  111,  128,  113,
30de0 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20  . /*   410 */   
30de1 31 31 34 2c 20 20 31 31 37 2c 20 20 31 36 30 2c  114,  117,  160,
30de2 20 20 31 33 36 2c 20 20 20 34 34 2c 20 20 31 32    136,   44,  12
30de3 38 2c 20 20 20 34 36 2c 20 20 20 32 36 2c 20 20  8,   46,   26,  
30de4 20 32 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20   27,   68,. /*  
30de5 20 34 32 30 20 2a 2f 20 20 20 20 36 39 2c 20 20   420 */    69,  
30de6 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
30de7 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
30de8 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
30de9 20 37 38 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a   78,. /*   430 *
30dea 2f 20 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  /    79,   80,  
30deb 20 39 38 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   98,   82,   83,
30dec 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
30ded 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20  6,   87,   88,. 
30dee 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20 20 20 38  /*   440 */    8
30def 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20  9,   90,   91,  
30df0 20 39 32 2c 20 20 20 31 39 2c 20 20 20 31 32 2c   92,   19,   12,
30df1 20 20 31 39 34 2c 20 20 31 35 30 2c 20 20 20 32    194,  150,   2
30df2 33 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 34  3,   88,. /*   4
30df3 35 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 20 20  50 */    23,    
30df4 37 2c 20 20 20 20 38 2c 20 20 31 30 35 2c 20 20  7,    8,  105,  
30df5 31 30 36 2c 20 20 20 39 34 2c 20 20 20 39 35 2c  106,   94,   95,
30df6 20 20 20 32 33 2c 20 20 31 35 30 2c 20 20 20 32     23,  150,   2
30df7 35 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20  5,. /*   460 */ 
30df8 20 20 31 31 37 2c 20 20 20 32 38 2c 20 20 31 36    117,   28,  16
30df9 35 2c 20 20 31 31 31 2c 20 20 31 36 33 2c 20 20  5,  111,  163,  
30dfa 31 31 33 2c 20 20 31 31 34 2c 20 20 31 36 36 2c  113,  114,  166,
30dfb 20 20 31 36 37 2c 20 20 31 36 38 2c 0a 20 2f 2a    167,  168,. /*
30dfc 20 20 20 34 37 30 20 2a 2f 20 20 20 32 31 38 2c     470 */   218,
30dfd 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36    174,  175,  16
30dfe 35 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  5,   49,   50,  
30dff 31 32 38 2c 20 20 20 34 34 2c 20 20 31 31 31 2c  128,   44,  111,
30e00 20 20 20 34 36 2c 0a 20 2f 2a 20 20 20 34 38 30     46,. /*   480
30e01 20 2a 2f 20 20 20 31 31 33 2c 20 20 31 31 34 2c   */   113,  114,
30e02 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32    174,  175,   2
30e03 31 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20  1,   94,   95,  
30e04 32 33 35 2c 20 20 20 32 32 2c 20 20 20 32 33 2c  235,   22,   23,
30e05 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20  . /*   490 */   
30e06 20 35 37 2c 20 20 20 32 35 2c 20 20 32 34 30 2c   57,   25,  240,
30e07 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37     68,   69,   7
30e08 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
30e09 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20   73,   74,. /*  
30e0a 20 35 30 30 20 2a 2f 20 20 20 20 37 35 2c 20 20   500 */    75,  
30e0b 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
30e0c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31 31     79,   80,  11
30e0d 37 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  7,   82,   83,  
30e0e 20 38 34 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a   84,. /*   510 *
30e0f 2f 20 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  /    85,   86,  
30e10 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
30e11 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
30e12 32 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 0a 20  2,   19,  150,. 
30e13 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20 20 20 32  /*   520 */    2
30e14 35 2c 20 20 31 35 30 2c 20 20 20 32 33 2c 20 20  5,  150,   23,  
30e15 32 32 36 2c 20 20 32 31 36 2c 20 20 20 31 39 2c  226,  216,   19,
30e16 20 20 20 36 33 2c 20 20 31 35 30 2c 20 20 31 35     63,  150,  15
30e17 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 35  0,  150,. /*   5
30e18 33 30 20 2a 2f 20 20 20 31 36 31 2c 20 20 31 36  30 */   161,  16
30e19 32 2c 20 20 31 35 30 2c 20 20 20 36 37 2c 20 20  2,  150,   67,  
30e1a 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  165,  150,  165,
30e1b 20 20 20 32 33 2c 20 20 31 31 31 2c 20 20 20 32     23,  111,   2
30e1c 35 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20  5,. /*   540 */ 
30e1d 20 20 31 31 33 2c 20 20 31 31 34 2c 20 20 31 36    113,  114,  16
30e1e 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  5,  165,  165,  
30e1f 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c  174,  175,  165,
30e20 20 20 20 34 39 2c 20 20 20 35 30 2c 0a 20 2f 2a     49,   50,. /*
30e21 20 20 20 35 35 30 20 2a 2f 20 20 20 31 36 35 2c     550 */   165,
30e22 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 37    174,  175,  17
30e23 34 2c 20 20 31 37 35 2c 20 20 31 39 37 2c 20 20  4,  175,  197,  
30e24 20 35 30 2c 20 20 20 32 32 2c 20 20 20 32 33 2c   50,   22,   23,
30e25 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20 35 36 30    174,. /*   560
30e26 20 2a 2f 20 20 20 31 37 35 2c 20 20 31 36 30 2c   */   175,  160,
30e27 20 20 32 30 34 2c 20 20 31 30 30 2c 20 20 31 36    204,  100,  16
30e28 39 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20  9,  170,  171,  
30e29 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
30e2a 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20  . /*   570 */   
30e2b 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
30e2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
30e2d 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  6,   77,   78,  
30e2e 20 37 39 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20   79,   80,. /*  
30e2f 20 35 38 30 20 2a 2f 20 20 20 31 31 32 2c 20 20   580 */   112,  
30e30 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
30e31 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
30e32 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
30e33 20 39 30 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a   90,. /*   590 *
30e34 2f 20 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  /    91,   92,  
30e35 20 31 39 2c 20 20 20 32 32 2c 20 20 32 31 35 2c   19,   22,  215,
30e36 20 20 31 39 34 2c 20 20 20 32 33 2c 20 20 32 32    194,   23,  22
30e37 30 2c 20 20 20 39 37 2c 20 20 20 33 30 2c 0a 20  0,   97,   30,. 
30e38 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20 20 31 35  /*   600 */   15
30e39 30 2c 20 20 31 33 38 2c 20 20 20 36 37 2c 20 20  0,  138,   67,  
30e3a 20 33 34 2c 20 20 20 33 36 2c 20 20 32 32 30 2c   34,   36,  220,
30e3b 20 20 31 35 30 2c 20 20 32 30 36 2c 20 20 32 30    150,  206,  20
30e3c 37 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20 36  7,   22,. /*   6
30e3d 31 30 20 2a 2f 20 20 20 31 30 34 2c 20 20 31 38  10 */   104,  18
30e3e 31 2c 20 20 31 38 32 2c 20 20 20 32 36 2c 20 20  1,  182,   26,  
30e3f 31 30 38 2c 20 20 31 36 35 2c 20 20 31 31 35 2c  108,  165,  115,
30e40 20 20 20 34 38 2c 20 20 31 38 38 2c 20 20 20 35     48,  188,   5
30e41 31 2c 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20  1,. /*   620 */ 
30e42 20 20 31 32 35 2c 20 20 31 36 35 2c 20 20 20 34    125,  165,   4
30e43 39 2c 20 20 20 35 30 2c 20 20 31 37 34 2c 20 20  9,   50,  174,  
30e44 31 37 35 2c 20 20 20 35 38 2c 20 20 32 33 32 2c  175,   58,  232,
30e45 20 20 31 32 37 2c 20 20 20 32 33 2c 0a 20 2f 2a    127,   23,. /*
30e46 20 20 20 36 33 30 20 2a 2f 20 20 20 31 37 34 2c     630 */   174,
30e47 20 20 31 37 35 2c 20 20 31 30 35 2c 20 20 31 30    175,  105,  10
30e48 36 2c 20 20 31 36 30 2c 20 20 31 34 32 2c 20 20  6,  160,  142,  
30e49 31 34 33 2c 20 20 31 34 34 2c 20 20 31 34 35 2c  143,  144,  145,
30e4a 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20 36 34 30     23,. /*   640
30e4b 20 2a 2f 20 20 20 20 31 36 2c 20 20 20 36 38 2c   */    16,   68,
30e4c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37     69,   70,   7
30e4d 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
30e4e 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
30e4f 0a 20 2f 2a 20 20 20 36 35 30 20 2a 2f 20 20 20  . /*   650 */   
30e50 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
30e51 20 20 20 38 30 2c 20 20 31 35 30 2c 20 20 20 38     80,  150,   8
30e52 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
30e53 20 38 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20   85,   86,. /*  
30e54 20 36 36 30 20 2a 2f 20 20 20 20 38 37 2c 20 20   660 */    87,  
30e55 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
30e56 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31     91,   92,   1
30e57 39 2c 20 20 31 35 30 2c 20 20 31 39 34 2c 20 20  9,  150,  194,  
30e58 31 36 35 2c 0a 20 2f 2a 20 20 20 36 37 30 20 2a  165,. /*   670 *
30e59 2f 20 20 20 31 35 30 2c 20 20 20 32 34 2c 20 20  /   150,   24,  
30e5a 20 32 32 2c 20 20 31 35 30 2c 20 20 31 35 30 2c   22,  150,  150,
30e5b 20 20 31 30 37 2c 20 20 31 35 30 2c 20 20 20 32    107,  150,   2
30e5c 32 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20  2,  174,  175,. 
30e5d 2f 2a 20 20 20 36 38 30 20 2a 2f 20 20 20 20 38  /*   680 */    8
30e5e 38 2c 20 20 20 39 34 2c 20 20 31 36 35 2c 20 20  8,   94,  165,  
30e5f 20 32 33 2c 20 20 20 36 30 2c 20 20 31 36 35 2c   23,   60,  165,
30e60 20 20 20 36 32 2c 20 20 31 35 30 2c 20 20 31 36     62,  150,  16
30e61 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 36  5,  165,. /*   6
30e62 39 30 20 2a 2f 20 20 20 20 31 31 2c 20 20 31 36  90 */    11,  16
30e63 35 2c 20 20 32 31 38 2c 20 20 20 33 35 2c 20 20  5,  218,   35,  
30e64 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34 39 2c  174,  175,   49,
30e65 20 20 20 35 30 2c 20 20 31 37 34 2c 20 20 31 37     50,  174,  17
30e66 35 2c 0a 20 2f 2a 20 20 20 37 30 30 20 2a 2f 20  5,. /*   700 */ 
30e67 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36    174,  175,  16
30e68 35 2c 20 20 20 32 35 2c 20 20 31 31 32 2c 20 20  5,   25,  112,  
30e69 20 20 30 2c 20 20 20 20 31 2c 20 20 20 20 32 2c    0,    1,    2,
30e6a 20 20 31 31 36 2c 20 20 20 32 35 2c 0a 20 2f 2a    116,   25,. /*
30e6b 20 20 20 37 31 30 20 2a 2f 20 20 20 31 39 33 2c     710 */   193,
30e6c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32    174,  175,   2
30e6d 33 2c 20 20 32 34 30 2c 20 20 20 36 38 2c 20 20  3,  240,   68,  
30e6e 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
30e6f 20 20 20 37 32 2c 0a 20 2f 2a 20 20 20 37 32 30     72,. /*   720
30e70 20 2a 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c   */    73,   74,
30e71 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
30e72 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
30e73 20 38 30 2c 20 20 31 35 30 2c 20 20 20 38 32 2c   80,  150,   82,
30e74 0a 20 2f 2a 20 20 20 37 33 30 20 2a 2f 20 20 20  . /*   730 */   
30e75 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
30e76 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
30e77 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  8,   89,   90,  
30e78 20 39 31 2c 20 20 20 39 32 2c 0a 20 2f 2a 20 20   91,   92,. /*  
30e79 20 37 34 30 20 2a 2f 20 20 20 20 31 39 2c 20 20   740 */    19,  
30e7a 31 35 30 2c 20 20 20 32 33 2c 20 20 31 36 35 2c  150,   23,  165,
30e7b 20 20 20 32 35 2c 20 20 31 36 36 2c 20 20 31 36     25,  166,  16
30e7c 37 2c 20 20 31 36 38 2c 20 20 20 39 37 2c 20 20  7,  168,   97,  
30e7d 20 39 38 2c 0a 20 2f 2a 20 20 20 37 35 30 20 2a   98,. /*   750 *
30e7e 2f 20 20 20 31 35 35 2c 20 20 20 32 33 2c 20 20  /   155,   23,  
30e7f 31 37 34 2c 20 20 31 37 35 2c 20 20 31 35 30 2c  174,  175,  150,
30e80 20 20 20 32 35 2c 20 20 31 36 35 2c 20 20 31 35     25,  165,  15
30e81 30 2c 20 20 20 33 35 2c 20 20 31 35 30 2c 0a 20  0,   35,  150,. 
30e82 2f 2a 20 20 20 37 36 30 20 2a 2f 20 20 20 31 35  /*   760 */   15
30e83 30 2c 20 20 31 35 30 2c 20 20 32 34 35 2c 20 20  0,  150,  245,  
30e84 31 35 30 2c 20 20 32 34 31 2c 20 20 31 37 34 2c  150,  241,  174,
30e85 20 20 31 37 35 2c 20 20 31 32 39 2c 20 20 31 33    175,  129,  13
30e86 30 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 37  0,  165,. /*   7
30e87 37 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 20 35  70 */    49,   5
30e88 30 2c 20 20 31 36 35 2c 20 20 20 35 32 2c 20 20  0,  165,   52,  
30e89 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  165,  165,  165,
30e8a 20 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20 32     23,  165,   2
30e8b 35 2c 0a 20 2f 2a 20 20 20 37 38 30 20 2a 2f 20  5,. /*   780 */ 
30e8c 20 20 20 32 35 2c 20 20 31 37 34 2c 20 20 31 37     25,  174,  17
30e8d 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
30e8e 31 37 34 2c 20 20 31 37 35 2c 20 20 32 34 38 2c  174,  175,  248,
30e8f 20 20 32 34 39 2c 20 20 20 36 38 2c 0a 20 2f 2a    249,   68,. /*
30e90 20 20 20 37 39 30 20 2a 2f 20 20 20 20 36 39 2c     790 */    69,
30e91 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
30e92 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  2,   73,   74,  
30e93 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
30e94 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20 38 30 30     78,. /*   800
30e95 20 2a 2f 20 20 20 20 37 39 2c 20 20 20 38 30 2c   */    79,   80,
30e96 20 20 31 35 30 2c 20 20 20 38 32 2c 20 20 20 38    150,   82,   8
30e97 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
30e98 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
30e99 0a 20 2f 2a 20 20 20 38 31 30 20 2a 2f 20 20 20  . /*   810 */   
30e9a 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c   89,   90,   91,
30e9b 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31 35     92,   19,  15
30e9c 30 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  0,  150,  165,  
30e9d 31 35 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20  150,  150,. /*  
30e9e 20 38 32 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   820 */   150,  
30e9f 32 31 37 2c 20 20 31 35 30 2c 20 20 32 31 33 2c  217,  150,  213,
30ea0 20 20 32 32 39 2c 20 20 31 31 39 2c 20 20 31 37    229,  119,  17
30ea1 34 2c 20 20 31 37 35 2c 20 20 20 32 32 2c 20 20  4,  175,   22,  
30ea2 32 33 34 2c 0a 20 2f 2a 20 20 20 38 33 30 20 2a  234,. /*   830 *
30ea3 2f 20 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  /   165,  165,  
30ea4 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  165,  165,  165,
30ea5 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36    165,  150,  16
30ea6 35 2c 20 20 20 33 35 2c 20 20 31 37 34 2c 0a 20  5,   35,  174,. 
30ea7 2f 2a 20 20 20 38 34 30 20 2a 2f 20 20 20 31 37  /*   840 */   17
30ea8 35 2c 20 20 31 37 34 2c 20 20 31 37 34 2c 20 20  5,  174,  174,  
30ea9 31 37 35 2c 20 20 20 34 39 2c 20 20 20 35 30 2c  175,   49,   50,
30eaa 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32    174,  175,   2
30eab 33 2c 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20 38  3,   27,. /*   8
30eac 35 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 31 36  50 */    25,  16
30ead 35 2c 20 20 31 31 37 2c 20 20 31 38 37 2c 20 20  5,  117,  187,  
30eae 32 34 31 2c 20 20 20 32 32 2c 20 20 31 38 37 2c  241,   22,  187,
30eaf 20 20 31 38 37 2c 20 20 31 30 33 2c 20 20 20 32    187,  103,   2
30eb0 33 2c 0a 20 2f 2a 20 20 20 38 36 30 20 2a 2f 20  3,. /*   860 */ 
30eb1 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32    174,  175,   2
30eb2 33 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  3,   68,   69,  
30eb3 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
30eb4 20 20 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a     73,   74,. /*
30eb5 20 20 20 38 37 30 20 2a 2f 20 20 20 20 37 35 2c     870 */    75,
30eb6 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
30eb7 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  8,   79,   80,  
30eb8 31 35 30 2c 20 20 20 38 32 2c 20 20 20 38 33 2c  150,   82,   83,
30eb9 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20 38 38 30     84,. /*   880
30eba 20 2a 2f 20 20 20 20 38 35 2c 20 20 20 38 36 2c   */    85,   86,
30ebb 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
30ebc 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20  9,   90,   91,  
30ebd 20 39 32 2c 20 20 20 31 39 2c 20 20 31 35 30 2c   92,   19,  150,
30ebe 0a 20 2f 2a 20 20 20 38 39 30 20 2a 2f 20 20 20  . /*   890 */   
30ebf 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30 2c  150,  165,  150,
30ec0 20 20 20 32 33 2c 20 20 31 35 30 2c 20 20 20 32     23,  150,   2
30ec1 35 2c 20 20 20 32 33 2c 20 20 31 33 35 2c 20 20  5,   23,  135,  
30ec2 20 32 35 2c 20 20 31 33 37 2c 0a 20 2f 2a 20 20   25,  137,. /*  
30ec3 20 39 30 30 20 2a 2f 20 20 20 31 37 34 2c 20 20   900 */   174,  
30ec4 31 37 35 2c 20 20 32 30 36 2c 20 20 32 30 37 2c  175,  206,  207,
30ec5 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 20 33    165,  165,   3
30ec6 39 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20  9,  165,  150,  
30ec7 31 36 35 2c 0a 20 2f 2a 20 20 20 39 31 30 20 2a  165,. /*   910 *
30ec8 2f 20 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  /   150,  150,  
30ec9 31 31 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  115,  174,  175,
30eca 20 20 20 35 32 2c 20 20 31 37 34 2c 20 20 31 37     52,  174,  17
30ecb 35 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 0a 20  5,   49,   50,. 
30ecc 2f 2a 20 20 20 39 32 30 20 2a 2f 20 20 20 20 32  /*   920 */    2
30ecd 33 2c 20 20 31 35 30 2c 20 20 20 32 35 2c 20 20  3,  150,   25,  
30ece 31 36 35 2c 20 20 31 32 37 2c 20 20 31 36 35 2c  165,  127,  165,
30ecf 20 20 31 36 35 2c 20 20 31 38 37 2c 20 20 20 32    165,  187,   2
30ed0 33 2c 20 20 20 32 39 2c 0a 20 2f 2a 20 20 20 39  3,   29,. /*   9
30ed1 33 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 31 38  30 */    25,  18
30ed2 37 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  7,  174,  175,  
30ed3 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c  174,  175,  165,
30ed4 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37     68,   69,   7
30ed5 30 2c 0a 20 2f 2a 20 20 20 39 34 30 20 2a 2f 20  0,. /*   940 */ 
30ed6 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
30ed7 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  3,   74,   75,  
30ed8 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
30ed9 20 20 20 37 39 2c 20 20 20 38 30 2c 0a 20 2f 2a     79,   80,. /*
30eda 20 20 20 39 35 30 20 2a 2f 20 20 20 31 35 30 2c     950 */   150,
30edb 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
30edc 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
30edd 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
30ede 20 20 20 39 30 2c 0a 20 2f 2a 20 20 20 39 36 30     90,. /*   960
30edf 20 2a 2f 20 20 20 20 39 31 2c 20 20 20 39 32 2c   */    91,   92,
30ee0 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31 39     19,  150,  19
30ee1 33 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20  3,  165,  150,  
30ee2 20 32 33 2c 20 20 31 35 30 2c 20 20 20 32 35 2c   23,  150,   25,
30ee3 0a 20 2f 2a 20 20 20 39 37 30 20 2a 2f 20 20 20  . /*   970 */   
30ee4 31 36 30 2c 20 20 31 36 30 2c 20 20 31 36 30 2c  160,  160,  160,
30ee5 20 20 20 35 32 2c 20 20 31 37 34 2c 20 20 31 37     52,  174,  17
30ee6 35 2c 20 20 31 39 30 2c 20 20 31 39 31 2c 20 20  5,  190,  191,  
30ee7 31 36 35 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20  165,   22,. /*  
30ee8 20 39 38 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   980 */   150,  
30ee9 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  165,  150,  165,
30eea 20 20 31 35 30 2c 20 20 31 34 34 2c 20 20 31 34    150,  144,  14
30eeb 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
30eec 20 32 33 2c 0a 20 2f 2a 20 20 20 39 39 30 20 2a   23,. /*   990 *
30eed 2f 20 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  /   174,  175,  
30eee 20 34 39 2c 20 20 20 35 30 2c 20 20 20 35 32 2c   49,   50,   52,
30eef 20 20 31 36 35 2c 20 20 20 32 32 2c 20 20 31 36    165,   22,  16
30ef0 35 2c 20 20 20 32 32 2c 20 20 31 36 35 2c 0a 20  5,   22,  165,. 
30ef1 2f 2a 20 20 31 30 30 30 20 2a 2f 20 20 20 32 35  /*  1000 */   25
30ef2 30 2c 20 20 32 35 31 2c 20 20 32 34 31 2c 20 20  0,  251,  241,  
30ef3 20 32 32 2c 20 20 31 39 34 2c 20 20 31 39 34 2c   22,  194,  194,
30ef4 20 20 31 39 34 2c 20 20 31 30 38 2c 20 20 31 37    194,  108,  17
30ef5 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 31 30  4,  175,. /*  10
30ef6 31 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 31 39  10 */    19,  19
30ef7 33 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  3,   69,   70,  
30ef8 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
30ef9 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
30efa 36 2c 0a 20 2f 2a 20 20 31 30 32 30 20 2a 2f 20  6,. /*  1020 */ 
30efb 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
30efc 39 2c 20 20 20 38 30 2c 20 20 31 35 30 2c 20 20  9,   80,  150,  
30efd 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
30efe 20 20 20 38 35 2c 20 20 20 38 36 2c 0a 20 2f 2a     85,   86,. /*
30eff 20 20 31 30 33 30 20 2a 2f 20 20 20 20 38 37 2c    1030 */    87,
30f00 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
30f01 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  0,   91,   92,  
30f02 20 31 39 2c 20 20 31 35 30 2c 20 20 31 35 30 2c   19,  150,  150,
30f03 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 30 34 30    165,. /*  1040
30f04 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 35 30 2c   */   150,  150,
30f05 20 20 31 35 30 2c 20 20 32 31 33 2c 20 20 31 36    150,  213,  16
30f06 30 2c 20 20 32 31 33 2c 20 20 31 36 30 2c 20 20  0,  213,  160,  
30f07 31 36 36 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  166,  174,  175,
30f08 0a 20 2f 2a 20 20 31 30 35 30 20 2a 2f 20 20 20  . /*  1050 */   
30f09 20 39 31 2c 20 20 20 39 32 2c 20 20 31 36 35 2c   91,   92,  165,
30f0a 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36    165,  150,  16
30f0b 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  5,  165,  165,  
30f0c 31 35 30 2c 20 20 31 36 36 2c 0a 20 2f 2a 20 20  150,  166,. /*  
30f0d 31 30 36 30 20 2a 2f 20 20 20 31 30 32 2c 20 20  1060 */   102,  
30f0e 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32 34 2c  174,  175,   24,
30f0f 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34    174,  175,   4
30f10 39 2c 20 20 20 35 30 2c 20 20 20 32 30 2c 20 20  9,   50,   20,  
30f11 31 36 35 2c 0a 20 2f 2a 20 20 31 30 37 30 20 2a  165,. /*  1070 *
30f12 2f 20 20 20 31 39 30 2c 20 20 31 39 31 2c 20 20  /   190,  191,  
30f13 31 30 34 2c 20 20 31 36 35 2c 20 20 20 38 36 2c  104,  165,   86,
30f14 20 20 20 38 37 2c 20 20 31 36 36 2c 20 20 20 34     87,  166,   4
30f15 33 2c 20 20 31 39 34 2c 20 20 20 35 39 2c 0a 20  3,  194,   59,. 
30f16 2f 2a 20 20 31 30 38 30 20 2a 2f 20 20 20 31 39  /*  1080 */   19
30f17 34 2c 20 20 31 39 33 2c 20 20 31 37 34 2c 20 20  4,  193,  174,  
30f18 31 37 35 2c 20 20 31 39 33 2c 20 20 31 39 33 2c  175,  193,  193,
30f19 20 20 32 30 35 2c 20 20 20 37 30 2c 20 20 20 37    205,   70,   7
30f1a 31 2c 20 20 20 37 32 2c 0a 20 2f 2a 20 20 31 30  1,   72,. /*  10
30f1b 39 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 20 37  90 */    73,   7
30f1c 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
30f1d 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
30f1e 20 20 20 38 30 2c 20 20 32 30 35 2c 20 20 20 38     80,  205,   8
30f1f 32 2c 0a 20 2f 2a 20 20 31 31 30 30 20 2a 2f 20  2,. /*  1100 */ 
30f20 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
30f21 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
30f22 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
30f23 20 20 20 39 31 2c 20 20 20 39 32 2c 0a 20 2f 2a     91,   92,. /*
30f24 20 20 31 31 31 30 20 2a 2f 20 20 20 20 31 39 2c    1110 */    19,
30f25 20 20 20 32 30 2c 20 20 31 35 30 2c 20 20 20 32     20,  150,   2
30f26 32 2c 20 20 31 35 30 2c 20 20 32 30 35 2c 20 20  2,  150,  205,  
30f27 31 33 38 2c 20 20 20 32 36 2c 20 20 20 32 37 2c  138,   26,   27,
30f28 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 31 32 30    150,. /*  1120
30f29 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 20 31 2c   */   150,    1,
30f2a 20 20 20 20 32 2c 20 20 20 31 39 2c 20 20 20 32      2,   19,   2
30f2b 30 2c 20 20 31 35 30 2c 20 20 20 32 32 2c 20 20  0,  150,   22,  
30f2c 31 36 35 2c 20 20 20 33 37 2c 20 20 31 36 35 2c  165,   37,  165,
30f2d 0a 20 2f 2a 20 20 31 31 33 30 20 2a 2f 20 20 20  . /*  1130 */   
30f2e 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 35 2c   26,   27,   25,
30f2f 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 36    150,  165,  16
30f30 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
30f31 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20  174,  175,. /*  
30f32 31 31 34 30 20 2a 2f 20 20 20 31 36 35 2c 20 20  1140 */   165,  
30f33 20 33 37 2c 20 20 20 35 33 2c 20 20 31 35 30 2c   37,   53,  150,
30f34 20 20 31 35 30 2c 20 20 31 37 33 2c 20 20 31 35    150,  173,  15
30f35 30 2c 20 20 20 35 36 2c 20 20 31 36 35 2c 20 20  0,   56,  165,  
30f36 31 37 34 2c 0a 20 2f 2a 20 20 31 31 35 30 20 2a  174,. /*  1150 *
30f37 2f 20 20 20 31 37 35 2c 20 20 31 35 30 2c 20 20  /   175,  150,  
30f38 20 35 33 2c 20 20 31 38 31 2c 20 20 31 30 34 2c   53,  181,  104,
30f39 20 20 20 32 32 2c 20 20 31 35 30 2c 20 20 20 36     22,  150,   6
30f3a 36 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 0a 20  6,  165,  165,. 
30f3b 2f 2a 20 20 31 31 36 30 20 2a 2f 20 20 20 20 35  /*  1160 */    5
30f3c 36 2c 20 20 31 36 35 2c 20 20 31 39 33 2c 20 20  6,  165,  193,  
30f3d 20 20 35 2c 20 20 20 20 31 2c 20 20 20 32 37 2c    5,    1,   27,
30f3e 20 20 31 36 35 2c 20 20 31 34 36 2c 20 20 31 34    165,  146,  14
30f3f 37 2c 20 20 31 31 37 2c 0a 20 2f 2a 20 20 31 31  7,  117,. /*  11
30f40 37 30 20 2a 2f 20 20 20 20 36 36 2c 20 20 31 36  70 */    66,  16
30f41 35 2c 20 20 31 35 30 2c 20 20 31 35 32 2c 20 20  5,  150,  152,  
30f42 20 33 35 2c 20 20 31 35 34 2c 20 20 31 39 33 2c   35,  154,  193,
30f43 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
30f44 38 2c 0a 20 2f 2a 20 20 31 31 38 30 20 2a 2f 20  8,. /*  1180 */ 
30f45 20 20 31 35 30 2c 20 20 31 36 30 2c 20 20 31 30    150,  160,  10
30f46 37 2c 20 20 31 32 36 2c 20 20 20 39 33 2c 20 20  7,  126,   93,  
30f47 20 39 34 2c 20 20 20 39 35 2c 20 20 31 36 35 2c   94,   95,  165,
30f48 20 20 31 35 30 2c 20 20 20 39 38 2c 0a 20 2f 2a    150,   98,. /*
30f49 20 20 31 31 39 30 20 2a 2f 20 20 20 20 38 36 2c    1190 */    86,
30f4a 20 20 20 38 37 2c 20 20 31 35 30 2c 20 20 31 35     87,  150,  15
30f4b 30 2c 20 20 20 37 36 2c 20 20 31 36 35 2c 20 20  0,   76,  165,  
30f4c 20 32 32 2c 20 20 20 39 33 2c 20 20 20 39 34 2c   22,   93,   94,
30f4d 20 20 20 39 35 2c 0a 20 2f 2a 20 20 31 32 30 30     95,. /*  1200
30f4e 20 2a 2f 20 20 20 20 37 36 2c 20 20 31 31 38 2c   */    76,  118,
30f4f 20 20 20 39 38 2c 20 20 31 36 35 2c 20 20 31 37     98,  165,  17
30f50 34 2c 20 20 31 37 35 2c 20 20 20 20 31 2c 20 20  4,  175,    1,  
30f51 31 36 35 2c 20 20 31 36 35 2c 20 20 20 31 39 2c  165,  165,   19,
30f52 0a 20 2f 2a 20 20 31 32 31 30 20 2a 2f 20 20 20  . /*  1210 */   
30f53 20 32 30 2c 20 20 32 31 37 2c 20 20 20 32 32 2c   20,  217,   22,
30f54 20 20 31 35 30 2c 20 20 20 31 36 2c 20 20 31 39    150,   16,  19
30f55 34 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  4,   26,   27,  
30f56 31 31 38 2c 20 20 31 31 35 2c 0a 20 2f 2a 20 20  118,  115,. /*  
30f57 31 32 32 30 20 2a 2f 20 20 20 31 32 39 2c 20 20  1220 */   129,  
30f58 31 33 30 2c 20 20 31 33 31 2c 20 20 31 33 32 2c  130,  131,  132,
30f59 20 20 31 33 33 2c 20 20 31 33 34 2c 20 20 31 32    133,  134,  12
30f5a 30 2c 20 20 20 33 37 2c 20 20 31 36 35 2c 20 20  0,   37,  165,  
30f5b 31 35 30 2c 0a 20 2f 2a 20 20 31 32 33 30 20 2a  150,. /*  1230 *
30f5c 2f 20 20 20 20 32 30 2c 20 20 31 39 33 2c 20 20  /    20,  193,  
30f5d 31 32 37 2c 20 20 31 32 39 2c 20 20 31 33 30 2c  127,  129,  130,
30f5e 20 20 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33    131,  132,  13
30f5f 33 2c 20 20 31 33 34 2c 20 20 32 31 38 2c 0a 20  3,  134,  218,. 
30f60 2f 2a 20 20 31 32 34 30 20 2a 2f 20 20 20 31 35  /*  1240 */   15
30f61 30 2c 20 20 31 30 37 2c 20 20 31 35 30 2c 20 20  0,  107,  150,  
30f62 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30 2c  150,  165,  150,
30f63 20 20 20 35 36 2c 20 20 31 35 30 2c 20 20 31 35     56,  150,  15
30f64 30 2c 20 20 31 32 36 2c 0a 20 2f 2a 20 20 31 32  0,  126,. /*  12
30f65 35 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 31  50 */    22,   1
30f66 36 2c 20 20 31 36 30 2c 20 20 20 36 35 2c 20 20  6,  160,   65,  
30f67 20 32 33 2c 20 20 31 36 35 2c 20 20 20 36 36 2c   23,  165,   66,
30f68 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 35    165,  165,  15
30f69 30 2c 0a 20 2f 2a 20 20 31 32 36 30 20 2a 2f 20  0,. /*  1260 */ 
30f6a 20 20 31 36 35 2c 20 20 20 32 33 2c 20 20 31 36    165,   23,  16
30f6b 35 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20  5,  165,  174,  
30f6c 31 37 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  175,  174,  175,
30f6d 20 20 32 34 37 2c 20 20 31 37 34 2c 0a 20 2f 2a    247,  174,. /*
30f6e 20 20 31 32 37 30 20 2a 2f 20 20 20 31 37 35 2c    1270 */   175,
30f6f 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 31    174,  175,   1
30f70 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20  5,  165,  150,  
30f71 20 38 36 2c 20 20 20 38 37 2c 20 20 31 35 30 2c   86,   87,  150,
30f72 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 32 38 30    150,. /*  1280
30f73 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 32 32 2c   */   150,   22,
30f74 20 20 20 20 33 2c 20 20 20 39 33 2c 20 20 20 39      3,   93,   9
30f75 34 2c 20 20 20 39 35 2c 20 20 31 39 34 2c 20 20  4,   95,  194,  
30f76 31 35 30 2c 20 20 20 39 38 2c 20 20 31 34 30 2c  150,   98,  140,
30f77 0a 20 2f 2a 20 20 31 32 39 30 20 2a 2f 20 20 20  . /*  1290 */   
30f78 31 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  165,  150,  150,
30f79 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36    165,  165,  16
30f7a 35 2c 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20  5,   19,   20,  
30f7b 20 20 34 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20    4,   22,. /*  
30f7c 31 33 30 30 20 2a 2f 20 20 20 31 36 34 2c 20 20  1300 */   164,  
30f7d 31 38 30 2c 20 20 31 36 35 2c 20 20 20 32 36 2c  180,  165,   26,
30f7e 20 20 20 32 37 2c 20 20 32 34 39 2c 20 20 31 36     27,  249,  16
30f7f 35 2c 20 20 31 36 35 2c 20 20 32 31 36 2c 20 20  5,  165,  216,  
30f80 20 20 36 2c 0a 20 2f 2a 20 20 31 33 31 30 20 2a    6,. /*  1310 *
30f81 2f 20 20 20 31 35 30 2c 20 20 31 37 34 2c 20 20  /   150,  174,  
30f82 31 37 35 2c 20 20 32 32 31 2c 20 20 20 33 37 2c  175,  221,   37,
30f83 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 35    174,  175,  25
30f84 32 2c 20 20 32 35 32 2c 20 20 31 32 39 2c 0a 20  2,  252,  129,. 
30f85 2f 2a 20 20 31 33 32 30 20 2a 2f 20 20 20 31 33  /*  1320 */   13
30f86 30 2c 20 20 31 33 31 2c 20 20 31 33 32 2c 20 20  0,  131,  132,  
30f87 31 33 33 2c 20 20 31 33 34 2c 20 20 31 36 35 2c  133,  134,  165,
30f88 20 20 31 38 30 2c 20 20 31 35 30 2c 20 20 31 34    180,  150,  14
30f89 39 2c 20 20 20 20 35 2c 0a 20 2f 2a 20 20 31 33  9,    5,. /*  13
30f8a 33 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 35  30 */   150,  15
30f8b 30 2c 20 20 31 35 30 2c 20 20 20 35 36 2c 20 20  0,  150,   56,  
30f8c 20 31 30 2c 20 20 20 31 31 2c 20 20 20 31 32 2c   10,   11,   12,
30f8d 20 20 20 31 33 2c 20 20 20 31 34 2c 20 20 31 35     13,   14,  15
30f8e 30 2c 0a 20 2f 2a 20 20 31 33 34 30 20 2a 2f 20  0,. /*  1340 */ 
30f8f 20 20 31 34 39 2c 20 20 20 31 37 2c 20 20 31 36    149,   17,  16
30f90 35 2c 20 20 20 36 36 2c 20 20 31 35 30 2c 20 20  5,   66,  150,  
30f91 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  165,  165,  165,
30f92 20 20 31 34 39 2c 20 20 20 31 33 2c 0a 20 2f 2a    149,   13,. /*
30f93 20 20 31 33 35 30 20 2a 2f 20 20 20 31 35 30 2c    1350 */   150,
30f94 20 20 31 34 39 2c 20 20 31 35 31 2c 20 20 31 35    149,  151,  15
30f95 30 2c 20 20 31 36 35 2c 20 20 20 33 31 2c 20 20  0,  165,   31,  
30f96 31 35 39 2c 20 20 20 33 33 2c 20 20 31 35 30 2c  159,   33,  150,
30f97 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 33 36 30    165,. /*  1360
30f98 20 2a 2f 20 20 20 31 35 31 2c 20 20 31 39 34 2c   */   151,  194,
30f99 20 20 31 35 30 2c 20 20 20 38 36 2c 20 20 20 38    150,   86,   8
30f9a 37 2c 20 20 31 36 35 2c 20 20 20 34 32 2c 20 20  7,  165,   42,  
30f9b 20 32 35 2c 20 20 31 36 35 2c 20 20 31 31 35 2c   25,  165,  115,
30f9c 0a 20 2f 2a 20 20 31 33 37 30 20 2a 2f 20 20 20  . /*  1370 */   
30f9d 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c   93,   94,   95,
30f9e 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 39    165,  150,   9
30f9f 38 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  8,  150,  165,  
30fa0 20 32 32 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20   22,   55,. /*  
30fa1 31 33 38 30 20 2a 2f 20 20 20 31 35 30 2c 20 20  1380 */   150,  
30fa2 20 35 37 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   57,   26,   27,
30fa3 20 20 31 39 39 2c 20 20 20 36 31 2c 20 20 31 32    199,   61,  12
30fa4 35 2c 20 20 31 35 30 2c 20 20 20 36 34 2c 20 20  5,  150,   64,  
30fa5 31 36 35 2c 0a 20 2f 2a 20 20 31 33 39 30 20 2a  165,. /*  1390 *
30fa6 2f 20 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  /   150,  165,  
30fa7 32 30 30 2c 20 20 31 32 32 2c 20 20 32 30 31 2c  200,  122,  201,
30fa8 20 20 31 36 35 2c 20 20 31 32 33 2c 20 20 31 35    165,  123,  15
30fa9 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20  0,  174,  175,. 
30faa 2f 2a 20 20 31 34 30 30 20 2a 2f 20 20 20 31 37  /*  1400 */   17
30fab 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20  4,  175,  165,  
30fac 31 35 30 2c 20 20 31 32 31 2c 20 20 31 36 35 2c  150,  121,  165,
30fad 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33    129,  130,  13
30fae 31 2c 20 20 31 33 32 2c 0a 20 2f 2a 20 20 31 34  1,  132,. /*  14
30faf 31 30 20 2a 2f 20 20 20 31 33 33 2c 20 20 31 33  10 */   133,  13
30fb0 34 2c 20 20 31 36 35 2c 20 20 32 30 32 2c 20 20  4,  165,  202,  
30fb1 31 37 34 2c 20 20 31 37 35 2c 20 20 32 30 33 2c  174,  175,  203,
30fb2 20 20 31 32 34 2c 20 20 31 36 35 2c 20 20 31 33    124,  165,  13
30fb3 35 2c 0a 20 2f 2a 20 20 31 34 32 30 20 2a 2f 20  5,. /*  1420 */ 
30fb4 20 20 31 35 37 2c 20 20 31 31 37 2c 20 20 20 36    157,  117,   6
30fb5 36 2c 20 20 32 32 37 2c 20 20 31 35 37 2c 20 20  6,  227,  157,  
30fb6 32 31 30 2c 20 20 31 30 34 2c 20 20 32 31 31 2c  210,  104,  211,
30fb7 20 20 31 32 30 2c 20 20 31 30 35 2c 0a 20 2f 2a    120,  105,. /*
30fb8 20 20 31 34 33 30 20 2a 2f 20 20 20 31 30 36 2c    1430 */   106,
30fb9 20 20 32 31 30 2c 20 20 31 37 36 2c 20 20 32 31    210,  176,  21
30fba 31 2c 20 20 31 31 30 2c 20 20 32 31 30 2c 20 20  1,  110,  210,  
30fbb 31 30 34 2c 20 20 20 34 37 2c 20 20 32 31 31 2c  104,   47,  211,
30fbc 20 20 31 31 35 2c 0a 20 2f 2a 20 20 31 34 34 30    115,. /*  1440
30fbd 20 2a 2f 20 20 20 31 37 36 2c 20 20 31 38 34 2c   */   176,  184,
30fbe 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 31 37     86,   87,  17
30fbf 36 2c 20 20 31 37 39 2c 20 20 31 30 33 2c 20 20  6,  179,  103,  
30fc0 31 37 38 2c 20 20 31 37 36 2c 20 20 20 32 32 2c  178,  176,   22,
30fc1 0a 20 2f 2a 20 20 31 34 35 30 20 2a 2f 20 20 20  . /*  1450 */   
30fc2 20 39 34 2c 20 20 20 39 32 2c 20 20 31 32 38 2c   94,   92,  128,
30fc3 20 20 32 33 30 2c 20 20 20 39 38 2c 20 20 31 37    230,   98,  17
30fc4 39 2c 20 20 31 37 36 2c 20 20 31 37 36 2c 20 20  9,  176,  176,  
30fc5 31 38 34 2c 20 20 32 33 30 2c 0a 20 2f 2a 20 20  184,  230,. /*  
30fc6 31 34 36 30 20 2a 2f 20 20 20 20 31 38 2c 20 20  1460 */    18,  
30fc7 31 37 36 2c 20 20 31 35 36 2c 20 20 31 33 39 2c  176,  156,  139,
30fc8 20 20 31 35 37 2c 20 20 31 35 36 2c 20 20 20 34    157,  156,   4
30fc9 35 2c 20 20 31 35 37 2c 20 20 31 35 36 2c 20 20  5,  157,  156,  
30fca 31 33 35 2c 0a 20 2f 2a 20 20 31 34 37 30 20 2a  135,. /*  1470 *
30fcb 2f 20 20 20 31 35 37 2c 20 20 31 35 37 2c 20 20  /   157,  157,  
30fcc 32 33 38 2c 20 20 31 35 36 2c 20 20 20 36 38 2c  238,  156,   68,
30fcd 20 20 32 33 39 2c 20 20 31 35 37 2c 20 20 31 38    239,  157,  18
30fce 39 2c 20 20 31 38 39 2c 20 20 32 31 39 2c 0a 20  9,  189,  219,. 
30fcf 2f 2a 20 20 31 34 38 30 20 2a 2f 20 20 20 20 32  /*  1480 */    2
30fd0 32 2c 20 20 31 39 39 2c 20 20 31 35 37 2c 20 20  2,  199,  157,  
30fd1 31 39 32 2c 20 20 20 31 38 2c 20 20 31 32 39 2c  192,   18,  129,
30fd2 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31 39    130,  131,  19
30fd3 32 2c 20 20 31 39 32 2c 0a 20 2f 2a 20 20 31 34  2,  192,. /*  14
30fd4 39 30 20 2a 2f 20 20 20 31 39 32 2c 20 20 31 39  90 */   192,  19
30fd5 39 2c 20 20 31 38 39 2c 20 20 32 31 39 2c 20 20  9,  189,  219,  
30fd6 31 35 37 2c 20 20 32 34 33 2c 20 20 20 34 30 2c  157,  243,   40,
30fd7 20 20 32 34 33 2c 20 20 31 35 37 2c 20 20 31 35    243,  157,  15
30fd8 37 2c 0a 20 2f 2a 20 20 31 35 30 30 20 2a 2f 20  7,. /*  1500 */ 
30fd9 20 20 32 34 36 2c 20 20 20 33 38 2c 20 20 31 35    246,   38,  15
30fda 33 2c 20 20 31 39 36 2c 20 20 31 39 38 2c 20 20  3,  196,  198,  
30fdb 31 36 36 2c 20 20 32 33 33 2c 20 20 32 33 33 2c  166,  233,  233,
30fdc 20 20 32 32 38 2c 20 20 31 37 37 2c 0a 20 2f 2a    228,  177,. /*
30fdd 20 20 31 35 31 30 20 2a 2f 20 20 20 31 37 37 2c    1510 */   177,
30fde 20 20 32 30 39 2c 20 20 31 37 37 2c 20 20 31 38    209,  177,  18
30fdf 32 2c 20 20 31 37 37 2c 20 20 31 36 36 2c 20 20  2,  177,  166,  
30fe0 31 37 37 2c 20 20 31 36 36 2c 20 20 31 37 38 2c  177,  166,  178,
30fe1 20 20 32 34 32 2c 0a 20 2f 2a 20 20 31 35 32 30    242,. /*  1520
30fe2 20 2a 2f 20 20 20 31 39 39 2c 20 20 32 34 32 2c   */   199,  242,
30fe3 20 20 32 30 39 2c 20 20 32 30 39 2c 20 20 31 39    209,  209,  19
30fe4 39 2c 20 20 31 34 38 2c 20 20 31 39 36 2c 20 20  9,  148,  196,  
30fe5 31 36 36 2c 20 20 32 30 38 2c 20 20 31 39 35 2c  166,  208,  195,
30fe6 0a 20 2f 2a 20 20 31 35 33 30 20 2a 2f 20 20 20  . /*  1530 */   
30fe7 31 39 35 2c 20 20 32 33 36 2c 20 20 32 33 37 2c  195,  236,  237,
30fe8 20 20 31 39 35 2c 20 20 31 39 31 2c 20 20 31 38    195,  191,  18
30fe9 33 2c 20 20 31 38 33 2c 20 20 31 38 36 2c 20 20  3,  183,  186,  
30fea 31 37 34 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20  174,  174,. /*  
30feb 31 35 34 30 20 2a 2f 20 20 20 31 38 36 2c 20 20  1540 */   186,  
30fec 31 31 35 2c 20 20 20 39 32 2c 0a 7d 3b 0a 23 64  115,   92,.};.#d
30fed 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 55  efine YY_SHIFT_U
30fee 53 45 5f 44 46 4c 54 20 28 2d 37 30 29 0a 23 64  SE_DFLT (-70).#d
30fef 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 43  efine YY_SHIFT_C
30ff0 4f 55 4e 54 20 28 34 31 37 29 0a 23 64 65 66 69  OUNT (417).#defi
30ff1 6e 65 20 59 59 5f 53 48 49 46 54 5f 4d 49 4e 20  ne YY_SHIFT_MIN 
30ff2 20 20 28 2d 36 39 29 0a 23 64 65 66 69 6e 65 20    (-69).#define 
30ff3 59 59 5f 53 48 49 46 54 5f 4d 41 58 20 20 20 28  YY_SHIFT_MAX   (
30ff4 31 34 36 36 29 0a 73 74 61 74 69 63 20 63 6f 6e  1466).static con
30ff5 73 74 20 73 68 6f 72 74 20 79 79 5f 73 68 69 66  st short yy_shif
30ff6 74 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20 2f 2a  t_ofst[] = {. /*
30ff7 20 20 20 20 20 30 20 2a 2f 20 20 31 31 32 30 2c       0 */  1120,
30ff8 20 31 31 30 34 2c 20 31 33 32 34 2c 20 31 31 30   1104, 1324, 110
30ff9 34 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 20  4, 1190, 1190,  
30ffa 20 39 30 2c 20 20 20 39 30 2c 20 20 20 20 31 2c   90,   90,    1,
30ffb 20 20 2d 31 39 2c 0a 20 2f 2a 20 20 20 20 31 30    -19,. /*    10
30ffc 20 2a 2f 20 20 31 31 39 30 2c 20 31 31 39 30 2c   */  1190, 1190,
30ffd 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39   1190, 1190, 119
30ffe 30 2c 20 20 32 38 30 2c 20 20 33 39 31 2c 20 20  0,  280,  391,  
30fff 37 32 31 2c 20 31 30 39 31 2c 20 31 31 39 30 2c  721, 1091, 1190,
31000 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 31  . /*    20 */  1
31001 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c  190, 1190, 1190,
31002 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39   1190, 1190, 119
31003 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31  0, 1190, 1190, 1
31004 31 39 30 2c 20 31 31 39 30 2c 0a 20 2f 2a 20 20  190, 1190,. /*  
31005 20 20 33 30 20 2a 2f 20 20 31 31 39 30 2c 20 31    30 */  1190, 1
31006 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c  190, 1190, 1190,
31007 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39   1190, 1190, 119
31008 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31  0, 1190, 1190, 1
31009 31 39 30 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a  190,. /*    40 *
3100a 2f 20 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31  /  1190, 1190, 1
3100b 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c  190, 1190, 1190,
3100c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 32 37   1190, 1190, 127
3100d 37 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 0a 20  7, 1190, 1190,. 
3100e 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 31 31 39  /*    50 */  119
3100f 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31  0, 1190, 1190, 1
31010 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c  190, 1190, 1190,
31011 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39   1190, 1190, 119
31012 30 2c 20 31 31 39 30 2c 0a 20 2f 2a 20 20 20 20  0, 1190,. /*    
31013 36 30 20 2a 2f 20 20 31 31 39 30 2c 20 20 2d 34  60 */  1190,  -4
31014 39 2c 20 20 32 38 37 2c 20 20 33 39 31 2c 20 20  9,  287,  391,  
31015 33 39 31 2c 20 20 39 38 38 2c 20 20 39 38 38 2c  391,  988,  988,
31016 20 20 32 31 35 2c 20 31 34 32 36 2c 20 20 20 35    215, 1426,   5
31017 35 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20  5,. /*    70 */ 
31018 20 20 36 34 37 2c 20 20 35 37 33 2c 20 20 34 39    647,  573,  49
31019 39 2c 20 20 34 32 35 2c 20 20 33 35 31 2c 20 20  9,  425,  351,  
3101a 32 37 37 2c 20 20 32 30 33 2c 20 20 31 32 39 2c  277,  203,  129,
3101b 20 20 37 39 35 2c 20 20 37 39 35 2c 0a 20 2f 2a    795,  795,. /*
3101c 20 20 20 20 38 30 20 2a 2f 20 20 20 37 39 35 2c      80 */   795,
3101d 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39    795,  795,  79
3101e 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20  5,  795,  795,  
3101f 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c  795,  795,  795,
31020 20 20 37 39 35 2c 0a 20 2f 2a 20 20 20 20 39 30    795,. /*    90
31021 20 2a 2f 20 20 20 37 39 35 2c 20 20 37 39 35 2c   */   795,  795,
31022 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39    795,  795,  79
31023 35 2c 20 20 37 39 35 2c 20 20 38 36 39 2c 20 20  5,  795,  869,  
31024 37 39 35 2c 20 20 39 34 33 2c 20 31 30 31 37 2c  795,  943, 1017,
31025 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 31  . /*   100 */  1
31026 30 31 37 2c 20 20 2d 36 39 2c 20 20 2d 36 39 2c  017,  -69,  -69,
31027 20 20 2d 36 39 2c 20 20 2d 36 39 2c 20 20 20 2d    -69,  -69,   -
31028 31 2c 20 20 20 2d 31 2c 20 20 20 35 38 2c 20 20  1,   -1,   58,  
31029 31 33 38 2c 20 20 2d 34 34 2c 0a 20 2f 2a 20 20  138,  -44,. /*  
3102a 20 31 31 30 20 2a 2f 20 20 20 33 39 31 2c 20 20   110 */   391,  
3102b 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c  391,  391,  391,
3102c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39    391,  391,  39
3102d 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  1,  391,  391,  
3102e 33 39 31 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a  391,. /*   120 *
3102f 2f 20 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  /   391,  391,  
31030 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c  391,  391,  391,
31031 20 20 33 39 31 2c 20 20 34 36 33 2c 20 20 35 30    391,  463,  50
31032 36 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 0a 20  6,  391,  391,. 
31033 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 33 39  /*   130 */   39
31034 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  1,  391,  391,  
31035 32 31 35 2c 20 20 39 35 39 2c 20 31 34 35 30 2c  215,  959, 1450,
31036 20 20 2d 37 30 2c 20 20 2d 37 30 2c 20 20 2d 37    -70,  -70,  -7
31037 30 2c 20 31 33 35 36 2c 0a 20 2f 2a 20 20 20 31  0, 1356,. /*   1
31038 34 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 34 33  40 */    73,  43
31039 33 2c 20 20 34 33 33 2c 20 20 33 36 31 2c 20 20  3,  433,  361,  
3103a 33 30 39 2c 20 20 31 36 35 2c 20 20 20 36 37 2c  309,  165,   67,
3103b 20 20 32 38 34 2c 20 20 34 36 36 2c 20 20 32 39    284,  466,  29
3103c 31 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20  1,. /*   150 */ 
3103d 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39    391,  391,  39
3103e 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  1,  391,  391,  
3103f 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c  391,  391,  391,
31040 20 20 33 39 31 2c 20 20 33 39 31 2c 0a 20 2f 2a    391,  391,. /*
31041 20 20 20 31 36 30 20 2a 2f 20 20 20 33 39 31 2c     160 */   391,
31042 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39    391,  391,  39
31043 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  1,  391,  391,  
31044 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c  391,  391,  391,
31045 20 20 33 39 31 2c 0a 20 2f 2a 20 20 20 31 37 30    391,. /*   170
31046 20 2a 2f 20 20 20 33 39 31 2c 20 20 33 39 31 2c   */   391,  391,
31047 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39    391,  391,  39
31048 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  1,  391,  391,  
31049 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c  391,  391,  391,
3104a 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20  . /*   180 */   
3104b 33 39 31 2c 20 20 33 39 31 2c 20 20 35 30 31 2c  391,  391,  501,
3104c 20 20 32 32 31 2c 20 20 32 32 31 2c 20 20 32 32    221,  221,  22
3104d 31 2c 20 20 37 30 35 2c 20 20 37 39 37 2c 20 31  1,  705,  797, 1
3104e 34 32 36 2c 20 31 34 32 36 2c 0a 20 2f 2a 20 20  426, 1426,. /*  
3104f 20 31 39 30 20 2a 2f 20 20 31 34 32 36 2c 20 20   190 */  1426,  
31050 2d 37 30 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c  -70,  -70,  -70,
31051 20 20 31 33 39 2c 20 20 31 37 31 2c 20 20 31 37    139,  171,  17
31052 31 2c 20 20 20 31 32 2c 20 20 35 36 38 2c 20 20  1,   12,  568,  
31053 35 36 38 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a  568,. /*   200 *
31054 2f 20 20 20 32 30 39 2c 20 20 34 32 37 2c 20 20  /   209,  427,  
31055 33 37 30 2c 20 20 33 36 37 2c 20 20 33 35 32 2c  370,  367,  352,
31056 20 20 32 39 36 2c 20 20 20 33 38 2c 20 20 20 33    296,   38,   3
31057 38 2c 20 20 20 33 38 2c 20 20 20 33 38 2c 0a 20  8,   38,   38,. 
31058 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 33 34  /*   210 */   34
31059 38 2c 20 20 35 36 39 2c 20 20 20 33 38 2c 20 20  8,  569,   38,  
3105a 20 33 38 2c 20 20 20 37 34 2c 20 20 35 38 37 2c   38,   74,  587,
3105b 20 20 35 39 32 2c 20 20 20 31 37 2c 20 20 34 39    592,   17,  49
3105c 35 2c 20 20 20 38 37 2c 0a 20 2f 2a 20 20 20 32  5,   87,. /*   2
3105d 32 30 20 2a 2f 20 20 20 20 38 37 2c 20 20 33 37  20 */    87,  37
3105e 32 2c 20 20 34 39 35 2c 20 20 33 37 32 2c 20 20  2,  495,  372,  
3105f 37 35 35 2c 20 20 32 31 35 2c 20 20 32 39 33 2c  755,  215,  293,
31060 20 20 32 31 35 2c 20 20 32 39 33 2c 20 20 31 34    215,  293,  14
31061 30 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20  0,. /*   230 */ 
31062 20 20 32 39 33 2c 20 20 20 38 37 2c 20 20 32 39    293,   87,  29
31063 33 2c 20 20 32 39 33 2c 20 20 37 36 32 2c 20 20  3,  293,  762,  
31064 36 33 38 2c 20 20 36 33 38 2c 20 20 32 31 35 2c  638,  638,  215,
31065 20 20 20 37 38 2c 20 20 20 35 31 2c 0a 20 2f 2a     78,   51,. /*
31066 20 20 20 32 34 30 20 2a 2f 20 20 20 32 34 36 2c     240 */   246,
31067 20 31 34 36 33 2c 20 31 33 30 34 2c 20 31 33 30   1463, 1304, 130
31068 34 2c 20 31 34 35 36 2c 20 31 34 35 36 2c 20 31  4, 1456, 1456, 1
31069 33 30 34 2c 20 31 34 35 38 2c 20 31 34 30 36 2c  304, 1458, 1406,
3106a 20 31 32 36 31 2c 0a 20 2f 2a 20 20 20 32 35 30   1261,. /*   250
3106b 20 2a 2f 20 20 31 34 36 36 2c 20 31 34 36 36 2c   */  1466, 1466,
3106c 20 31 34 36 36 2c 20 31 34 36 36 2c 20 31 33 30   1466, 1466, 130
3106d 34 2c 20 31 32 36 31 2c 20 31 34 35 38 2c 20 31  4, 1261, 1458, 1
3106e 34 30 36 2c 20 31 34 30 36 2c 20 31 33 30 34 2c  406, 1406, 1304,
3106f 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 31  . /*   260 */  1
31070 34 34 32 2c 20 31 33 33 34 2c 20 31 34 32 31 2c  442, 1334, 1421,
31071 20 31 33 30 34 2c 20 31 33 30 34 2c 20 31 34 34   1304, 1304, 144
31072 32 2c 20 31 33 30 34 2c 20 31 34 34 32 2c 20 31  2, 1304, 1442, 1
31073 33 30 34 2c 20 31 34 34 32 2c 0a 20 2f 2a 20 20  304, 1442,. /*  
31074 20 32 37 30 20 2a 2f 20 20 31 34 32 37 2c 20 31   270 */  1427, 1
31075 33 33 32 2c 20 31 33 33 32 2c 20 31 33 33 32 2c  332, 1332, 1332,
31076 20 31 33 39 30 2c 20 31 33 35 39 2c 20 31 33 35   1390, 1359, 135
31077 39 2c 20 31 34 32 37 2c 20 31 33 33 32 2c 20 31  9, 1427, 1332, 1
31078 33 34 33 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a  343,. /*   280 *
31079 2f 20 20 31 33 33 32 2c 20 31 33 39 30 2c 20 31  /  1332, 1390, 1
3107a 33 33 32 2c 20 31 33 33 32 2c 20 31 33 30 38 2c  332, 1332, 1308,
3107b 20 31 33 32 32 2c 20 31 33 30 38 2c 20 31 33 32   1322, 1308, 132
3107c 32 2c 20 31 33 30 38 2c 20 31 33 32 32 2c 0a 20  2, 1308, 1322,. 
3107d 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31 33 30  /*   290 */  130
3107e 34 2c 20 31 33 30 34 2c 20 31 32 38 34 2c 20 31  4, 1304, 1284, 1
3107f 32 39 33 2c 20 31 32 38 33 2c 20 31 32 37 33 2c  293, 1283, 1273,
31080 20 31 32 37 31 2c 20 31 32 36 31 2c 20 31 32 35   1271, 1261, 125
31081 34 2c 20 31 33 34 32 2c 0a 20 2f 2a 20 20 20 33  4, 1342,. /*   3
31082 30 30 20 2a 2f 20 20 31 33 33 36 2c 20 31 33 33  00 */  1336, 133
31083 36 2c 20 31 33 30 33 2c 20 31 33 30 33 2c 20 31  6, 1303, 1303, 1
31084 33 30 33 2c 20 31 33 30 33 2c 20 20 2d 37 30 2c  303, 1303,  -70,
31085 20 20 2d 37 30 2c 20 20 2d 37 30 2c 20 20 2d 37    -70,  -70,  -7
31086 30 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20  0,. /*   310 */ 
31087 20 20 2d 37 30 2c 20 20 2d 37 30 2c 20 20 33 33    -70,  -70,  33
31088 34 2c 20 20 31 32 30 2c 20 20 35 33 35 2c 20 20  4,  120,  535,  
31089 32 33 32 2c 20 20 36 32 34 2c 20 20 39 34 34 2c  232,  624,  944,
3108a 20 20 31 38 38 2c 20 20 39 30 35 2c 0a 20 2f 2a    188,  905,. /*
3108b 20 20 20 33 32 30 20 2a 2f 20 20 20 38 39 37 2c     320 */   897,
3108c 20 20 38 37 33 2c 20 20 38 37 30 2c 20 20 38 32    873,  870,  82
3108d 35 2c 20 20 37 35 34 2c 20 20 37 31 39 2c 20 20  5,  754,  719,  
3108e 36 35 31 2c 20 20 35 32 37 2c 20 20 34 34 34 2c  651,  527,  444,
3108f 20 20 31 32 35 2c 0a 20 2f 2a 20 20 20 33 33 30    125,. /*   330
31090 20 2a 2f 20 20 20 35 31 34 2c 20 20 34 33 34 2c   */   514,  434,
31091 20 31 32 39 34 2c 20 31 32 37 39 2c 20 31 32 35   1294, 1279, 125
31092 39 2c 20 31 31 34 39 2c 20 31 32 35 38 2c 20 31  9, 1149, 1258, 1
31093 31 38 38 2c 20 31 32 33 35 2c 20 31 32 33 38 2c  188, 1235, 1238,
31094 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 31  . /*   340 */  1
31095 32 33 31 2c 20 31 31 30 35 2c 20 31 32 32 38 2c  231, 1105, 1228,
31096 20 31 31 32 33 2c 20 31 31 33 34 2c 20 31 31 30   1123, 1134, 110
31097 30 2c 20 31 32 31 30 2c 20 31 31 30 36 2c 20 31  0, 1210, 1106, 1
31098 31 39 38 2c 20 31 32 30 35 2c 0a 20 2f 2a 20 20  198, 1205,. /*  
31099 20 33 35 30 20 2a 2f 20 20 31 30 38 33 2c 20 31   350 */  1083, 1
3109a 31 37 34 2c 20 31 30 35 37 2c 20 31 31 32 34 2c  174, 1057, 1124,
3109b 20 31 31 31 38 2c 20 31 30 37 35 2c 20 31 31 33   1118, 1075, 113
3109c 38 2c 20 31 31 33 39 2c 20 31 30 35 32 2c 20 31  8, 1139, 1052, 1
3109d 31 36 33 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a  163,. /*   360 *
3109e 2f 20 20 31 31 35 38 2c 20 31 31 33 33 2c 20 31  /  1158, 1133, 1
3109f 30 35 30 2c 20 20 39 37 38 2c 20 31 30 39 39 2c  050,  978, 1099,
310a0 20 31 31 30 37 2c 20 31 30 38 39 2c 20 31 30 32   1107, 1089, 102
310a1 30 2c 20 31 30 33 34 2c 20 20 39 36 38 2c 0a 20  0, 1034,  968,. 
310a2 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 31 30 33  /*   370 */  103
310a3 39 2c 20 31 30 34 38 2c 20 20 39 39 31 2c 20 20  9, 1048,  991,  
310a4 38 39 39 2c 20 20 39 35 38 2c 20 20 39 38 31 2c  899,  958,  981,
310a5 20 20 39 34 32 2c 20 20 39 37 36 2c 20 20 39 37    942,  976,  97
310a6 34 2c 20 20 39 36 36 2c 0a 20 2f 2a 20 20 20 33  4,  966,. /*   3
310a7 38 30 20 2a 2f 20 20 20 39 35 37 2c 20 20 39 32  80 */   957,  92
310a8 31 2c 20 20 39 30 30 2c 20 20 38 33 33 2c 20 20  1,  900,  833,  
310a9 38 36 33 2c 20 20 38 36 37 2c 20 20 38 33 39 2c  863,  867,  839,
310aa 20 20 38 33 36 2c 20 20 37 33 35 2c 20 20 38 32    836,  735,  82
310ab 32 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20  2,. /*   390 */ 
310ac 20 20 38 30 33 2c 20 20 38 30 36 2c 20 20 37 30    803,  806,  70
310ad 36 2c 20 20 36 38 34 2c 20 20 37 32 33 2c 20 20  6,  684,  723,  
310ae 37 33 30 2c 20 20 36 35 38 2c 20 20 36 38 34 2c  730,  658,  684,
310af 20 20 37 32 38 2c 20 20 36 39 30 2c 0a 20 2f 2a    728,  690,. /*
310b0 20 20 20 34 30 30 20 2a 2f 20 20 20 36 37 38 2c     400 */   678,
310b1 20 20 36 36 30 2c 20 20 36 35 35 2c 20 20 36 37    660,  655,  67
310b2 39 2c 20 20 36 35 30 2c 20 20 36 31 36 2c 20 20  9,  650,  616,  
310b3 36 30 36 2c 20 20 35 37 31 2c 20 20 34 36 38 2c  606,  571,  468,
310b4 20 20 33 38 39 2c 0a 20 2f 2a 20 20 20 34 31 30    389,. /*   410
310b5 20 2a 2f 20 20 20 33 34 33 2c 20 20 32 39 34 2c   */   343,  294,
310b6 20 20 31 38 36 2c 20 20 20 20 33 2c 20 20 20 34    186,    3,   4
310b7 30 2c 20 20 20 20 36 2c 20 20 20 2d 33 2c 20 20  0,    6,   -3,  
310b8 20 20 35 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20    5,.};.#define 
310b9 59 59 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46  YY_REDUCE_USE_DF
310ba 4c 54 20 28 2d 32 32 32 29 0a 23 64 65 66 69 6e  LT (-222).#defin
310bb 65 20 59 59 5f 52 45 44 55 43 45 5f 43 4f 55 4e  e YY_REDUCE_COUN
310bc 54 20 28 33 31 31 29 0a 23 64 65 66 69 6e 65 20  T (311).#define 
310bd 59 59 5f 52 45 44 55 43 45 5f 4d 49 4e 20 20 20  YY_REDUCE_MIN   
310be 28 2d 32 32 31 29 0a 23 64 65 66 69 6e 65 20 59  (-221).#define Y
310bf 59 5f 52 45 44 55 43 45 5f 4d 41 58 20 20 20 28  Y_REDUCE_MAX   (
310c0 31 33 37 37 29 0a 73 74 61 74 69 63 20 63 6f 6e  1377).static con
310c1 73 74 20 73 68 6f 72 74 20 79 79 5f 72 65 64 75  st short yy_redu
310c2 63 65 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20 2f  ce_ofst[] = {. /
310c3 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 34 39 33  *     0 */   493
310c4 2c 20 31 30 39 32 2c 20 31 30 32 31 2c 20 20 31  , 1092, 1021,  1
310c5 34 37 2c 20 20 31 35 38 2c 20 20 31 35 35 2c 20  47,  158,  155, 
310c6 20 20 38 36 2c 20 20 20 31 38 2c 20 20 20 38 31    86,   18,   81
310c7 2c 20 20 31 37 32 2c 0a 20 2f 2a 20 20 20 20 31  ,  172,. /*    1
310c8 30 20 2a 2f 20 20 20 33 38 35 2c 20 20 33 37 37  0 */   385,  377
310c9 2c 20 20 33 30 38 2c 20 20 33 37 39 2c 20 20 32  ,  308,  379,  2
310ca 39 37 2c 20 20 32 35 32 2c 20 20 2d 34 33 2c 20  97,  252,  -43, 
310cb 2d 31 34 36 2c 20 31 32 34 30 2c 20 31 32 32 36  -146, 1240, 1226
310cc 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20  ,. /*    20 */  
310cd 31 32 32 34 2c 20 31 31 34 31 2c 20 31 31 33 37  1224, 1141, 1137
310ce 2c 20 31 30 39 37 2c 20 31 30 39 35 2c 20 31 30  , 1097, 1095, 10
310cf 39 30 2c 20 31 30 33 30 2c 20 20 39 37 35 2c 20  90, 1030,  975, 
310d0 20 39 36 34 2c 20 20 39 36 32 2c 0a 20 2f 2a 20   964,  962,. /* 
310d1 20 20 20 33 30 20 2a 2f 20 20 20 39 30 38 2c 20     30 */   908, 
310d2 20 38 39 30 2c 20 20 38 38 37 2c 20 20 38 37 34   890,  887,  874
310d3 2c 20 20 38 33 34 2c 20 20 38 31 36 2c 20 20 38  ,  834,  816,  8
310d4 31 33 2c 20 20 38 30 30 2c 20 20 37 36 30 2c 20  13,  800,  760, 
310d5 20 37 35 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20   758,. /*    40 
310d6 2a 2f 20 20 20 37 34 32 2c 20 20 37 33 39 2c 20  */   742,  739, 
310d7 20 37 32 36 2c 20 20 36 38 36 2c 20 20 36 37 32   726,  686,  672
310d8 2c 20 20 36 36 38 2c 20 20 36 36 35 2c 20 20 36  ,  668,  665,  6
310d9 35 32 2c 20 20 36 31 31 2c 20 20 36 30 39 2c 0a  52,  611,  609,.
310da 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 36   /*    50 */   6
310db 30 37 2c 20 20 35 39 31 2c 20 20 35 37 38 2c 20  07,  591,  578, 
310dc 20 35 33 37 2c 20 20 35 32 36 2c 20 20 35 32 34   537,  526,  524
310dd 2c 20 20 35 32 30 2c 20 20 35 30 34 2c 20 20 34  ,  520,  504,  4
310de 35 36 2c 20 20 34 35 30 2c 0a 20 2f 2a 20 20 20  56,  450,. /*   
310df 20 36 30 20 2a 2f 20 20 20 33 37 31 2c 20 2d 32   60 */   371, -2
310e0 32 31 2c 20 20 34 37 34 2c 20 20 33 36 39 2c 20  21,  474,  369, 
310e1 20 35 31 37 2c 20 20 33 39 35 2c 20 20 20 39 32   517,  395,   92
310e2 2c 20 20 33 30 31 2c 20 20 34 30 31 2c 20 20 31  ,  301,  401,  1
310e3 31 38 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f  18,. /*    70 */
310e4 20 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31     118,  118,  1
310e5 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20  18,  118,  118, 
310e6 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38   118,  118,  118
310e7 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 0a 20 2f  ,  118,  118,. /
310e8 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 31 31 38  *    80 */   118
310e9 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31  ,  118,  118,  1
310ea 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20  18,  118,  118, 
310eb 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38   118,  118,  118
310ec 2c 20 20 31 31 38 2c 0a 20 2f 2a 20 20 20 20 39  ,  118,. /*    9
310ed 30 20 2a 2f 20 20 20 31 31 38 2c 20 20 31 31 38  0 */   118,  118
310ee 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31  ,  118,  118,  1
310ef 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20  18,  118,  118, 
310f0 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38   118,  118,  118
310f1 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20  ,. /*   100 */  
310f2 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38   118,  118,  118
310f3 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31  ,  118,  118,  1
310f4 31 38 2c 20 20 31 31 38 2c 20 20 32 30 38 2c 20  18,  118,  208, 
310f5 20 31 31 38 2c 20 20 31 31 38 2c 0a 20 2f 2a 20   118,  118,. /* 
310f6 20 20 31 31 30 20 2a 2f 20 20 31 30 33 38 2c 20    110 */  1038, 
310f7 20 39 39 34 2c 20 20 39 38 33 2c 20 20 39 36 39   994,  983,  969
310f8 2c 20 20 38 39 32 2c 20 20 38 39 31 2c 20 20 38  ,  892,  891,  8
310f9 38 38 2c 20 20 38 31 38 2c 20 20 37 36 31 2c 20  88,  818,  761, 
310fa 20 38 33 32 2c 0a 20 2f 2a 20 20 20 31 32 30 20   832,. /*   120 
310fb 2a 2f 20 20 20 36 31 33 2c 20 20 36 30 34 2c 20  */   613,  604, 
310fc 20 35 32 33 2c 20 20 37 34 34 2c 20 20 38 33 30   523,  744,  830
310fd 2c 20 20 37 37 31 2c 20 20 35 39 35 2c 20 20 34  ,  771,  595,  4
310fe 33 30 2c 20 20 37 34 30 2c 20 20 36 37 30 2c 0a  30,  740,  670,.
310ff 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 36   /*   130 */   6
31100 36 39 2c 20 20 36 36 36 2c 20 20 36 31 30 2c 20  69,  666,  610, 
31101 20 35 37 39 2c 20 20 31 31 38 2c 20 20 31 31 38   579,  118,  118
31102 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31  ,  118,  118,  1
31103 31 38 2c 20 20 36 36 37 2c 0a 20 2f 2a 20 20 20  18,  667,. /*   
31104 31 34 30 20 2a 2f 20 20 20 39 37 32 2c 20 20 38  140 */   972,  8
31105 38 30 2c 20 20 37 38 36 2c 20 20 39 39 36 2c 20  80,  786,  996, 
31106 31 32 35 33 2c 20 31 32 34 37 2c 20 31 32 33 37  1253, 1247, 1237
31107 2c 20 31 30 30 31 2c 20 20 37 35 30 2c 20 20 37  , 1001,  750,  7
31108 35 30 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f  50,. /*   150 */
31109 20 20 31 32 33 30 2c 20 31 32 31 32 2c 20 31 32    1230, 1212, 12
3110a 30 38 2c 20 31 32 30 33 2c 20 31 32 30 30 2c 20  08, 1203, 1200, 
3110b 31 31 39 34 2c 20 31 31 38 39 2c 20 31 31 38 32  1194, 1189, 1182
3110c 2c 20 31 31 38 31 2c 20 31 31 38 30 2c 0a 20 2f  , 1181, 1180,. /
3110d 2a 20 20 20 31 36 30 20 2a 2f 20 20 31 31 37 37  *   160 */  1177
3110e 2c 20 31 31 36 30 2c 20 31 31 34 32 2c 20 31 31  , 1160, 1142, 11
3110f 33 30 2c 20 31 31 32 39 2c 20 31 31 32 38 2c 20  30, 1129, 1128, 
31110 31 31 32 35 2c 20 31 31 30 39 2c 20 31 30 39 38  1125, 1109, 1098
31111 2c 20 31 30 39 33 2c 0a 20 2f 2a 20 20 20 31 37  , 1093,. /*   17
31112 30 20 2a 2f 20 20 31 30 37 39 2c 20 31 30 36 33  0 */  1079, 1063
31113 2c 20 31 30 34 33 2c 20 31 30 34 32 2c 20 31 30  , 1043, 1042, 10
31114 32 32 2c 20 31 30 30 36 2c 20 20 39 39 36 2c 20  22, 1006,  996, 
31115 20 39 39 33 2c 20 20 39 37 30 2c 20 20 39 30 34   993,  970,  904
31116 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20  ,. /*   180 */  
31117 20 33 38 32 2c 20 20 33 37 38 2c 20 20 38 38 36   382,  378,  886
31118 2c 20 20 39 31 30 2c 20 20 38 39 33 2c 20 20 38  ,  910,  893,  8
31119 38 31 2c 20 20 38 34 31 2c 20 20 38 38 34 2c 20  81,  841,  884, 
3111a 20 38 31 32 2c 20 20 38 31 31 2c 0a 20 2f 2a 20   812,  811,. /* 
3111b 20 20 31 39 30 20 2a 2f 20 20 20 38 31 30 2c 20    190 */   810, 
3111c 20 35 33 39 2c 20 20 36 39 36 2c 20 20 33 35 38   539,  696,  358
3111d 2c 20 31 33 35 34 2c 20 31 33 36 35 2c 20 31 33  , 1354, 1365, 13
3111e 36 34 2c 20 31 33 35 31 2c 20 31 33 35 33 2c 20  64, 1351, 1353, 
3111f 31 33 35 32 2c 0a 20 2f 2a 20 20 20 32 30 30 20  1352,. /*   200 
31120 2a 2f 20 20 31 33 32 30 2c 20 31 33 33 38 2c 20  */  1320, 1338, 
31121 31 33 34 33 2c 20 31 33 33 38 2c 20 31 33 33 38  1343, 1338, 1338
31122 2c 20 31 33 33 38 2c 20 31 33 33 38 2c 20 31 33  , 1338, 1338, 13
31123 33 38 2c 20 31 33 33 38 2c 20 31 33 33 38 2c 0a  38, 1338, 1338,.
31124 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 31 32   /*   210 */  12
31125 39 35 2c 20 31 32 39 35 2c 20 31 33 33 35 2c 20  95, 1295, 1335, 
31126 31 33 33 34 2c 20 31 33 32 30 2c 20 31 33 36 31  1334, 1320, 1361
31127 2c 20 31 33 33 30 2c 20 31 33 37 37 2c 20 31 33  , 1330, 1377, 13
31128 32 35 2c 20 31 33 31 34 2c 0a 20 2f 2a 20 20 20  25, 1314,. /*   
31129 32 32 30 20 2a 2f 20 20 31 33 31 33 2c 20 31 32  220 */  1313, 12
3112a 37 39 2c 20 31 33 32 31 2c 20 31 32 37 37 2c 20  79, 1321, 1277, 
3112b 31 33 34 30 2c 20 31 33 35 31 2c 20 31 33 33 39  1340, 1351, 1339
3112c 2c 20 31 33 34 39 2c 20 31 33 33 37 2c 20 31 33  , 1349, 1337, 13
3112d 33 31 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f  31,. /*   230 */
3112e 20 20 31 33 33 35 2c 20 31 33 30 32 2c 20 31 33    1335, 1302, 13
3112f 33 33 2c 20 31 33 33 32 2c 20 31 32 38 30 2c 20  33, 1332, 1280, 
31130 31 32 37 34 2c 20 31 32 37 33 2c 20 31 33 33 39  1274, 1273, 1339
31131 2c 20 31 33 30 36 2c 20 31 33 30 37 2c 0a 20 2f  , 1306, 1307,. /
31132 2a 20 20 20 32 34 30 20 2a 2f 20 20 31 33 34 39  *   240 */  1349
31133 2c 20 31 32 35 34 2c 20 31 33 34 32 2c 20 31 33  , 1254, 1342, 13
31134 34 31 2c 20 31 32 35 34 2c 20 31 32 35 32 2c 20  41, 1254, 1252, 
31135 31 33 33 37 2c 20 31 32 37 34 2c 20 31 33 30 33  1337, 1274, 1303
31136 2c 20 31 32 39 32 2c 0a 20 2f 2a 20 20 20 32 35  , 1292,. /*   25
31137 30 20 2a 2f 20 20 31 32 39 38 2c 20 31 32 39 37  0 */  1298, 1297
31138 2c 20 31 32 39 36 2c 20 31 32 39 31 2c 20 31 33  , 1296, 1291, 13
31139 32 35 2c 20 31 32 38 32 2c 20 31 32 36 30 2c 20  25, 1282, 1260, 
3113a 31 32 38 39 2c 20 31 32 38 38 2c 20 31 33 31 39  1289, 1288, 1319
3113b 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20  ,. /*   260 */  
3113c 31 33 31 37 2c 20 31 32 33 36 2c 20 31 32 33 34  1317, 1236, 1234
3113d 2c 20 31 33 31 34 2c 20 31 33 31 33 2c 20 31 33  , 1314, 1313, 13
3113e 31 32 2c 20 31 33 31 30 2c 20 31 33 30 39 2c 20  12, 1310, 1309, 
3113f 31 33 30 37 2c 20 31 33 30 36 2c 0a 20 2f 2a 20  1307, 1306,. /* 
31140 20 20 32 37 30 20 2a 2f 20 20 31 32 37 36 2c 20    270 */  1276, 
31141 31 32 38 35 2c 20 31 32 38 31 2c 20 31 32 38 30  1285, 1281, 1280
31142 2c 20 31 32 37 34 2c 20 31 32 32 39 2c 20 31 32  , 1274, 1229, 12
31143 32 33 2c 20 31 32 36 36 2c 20 31 32 37 32 2c 20  23, 1266, 1272, 
31144 31 32 36 39 2c 0a 20 2f 2a 20 20 20 32 38 30 20  1269,. /*   280 
31145 2a 2f 20 20 31 32 36 38 2c 20 31 32 35 37 2c 20  */  1268, 1257, 
31146 31 32 36 34 2c 20 31 32 35 36 2c 20 31 32 32 37  1264, 1256, 1227
31147 2c 20 31 32 32 35 2c 20 31 32 32 32 2c 20 31 32  , 1225, 1222, 12
31148 32 31 2c 20 31 32 31 36 2c 20 31 32 31 35 2c 0a  21, 1216, 1215,.
31149 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31 32   /*   290 */  12
3114a 36 37 2c 20 31 32 36 33 2c 20 31 31 39 36 2c 20  67, 1263, 1196, 
3114b 31 32 31 33 2c 20 31 32 31 31 2c 20 31 31 39 33  1213, 1211, 1193
3114c 2c 20 31 31 39 32 2c 20 31 31 38 35 2c 20 31 31  , 1192, 1185, 11
3114d 36 37 2c 20 31 31 39 37 2c 0a 20 2f 2a 20 20 20  67, 1197,. /*   
3114e 33 30 30 20 2a 2f 20 20 31 32 30 39 2c 20 31 32  300 */  1209, 12
3114f 30 31 2c 20 31 32 30 32 2c 20 31 31 39 39 2c 20  01, 1202, 1199, 
31150 31 31 39 31 2c 20 31 31 37 39 2c 20 31 30 36 36  1191, 1179, 1066
31151 2c 20 31 30 36 35 2c 20 31 30 35 36 2c 20 31 31  , 1065, 1056, 11
31152 34 36 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f  46,. /*   310 */
31153 20 20 31 31 32 31 2c 20 31 31 33 36 2c 0a 7d 3b    1121, 1136,.};
31154 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59  .static const YY
31155 41 43 54 49 4f 4e 54 59 50 45 20 79 79 5f 64 65  ACTIONTYPE yy_de
31156 66 61 75 6c 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20  fault[] = {. /* 
31157 20 20 20 20 30 20 2a 2f 20 20 20 36 33 34 2c 20      0 */   634, 
31158 20 38 36 39 2c 20 20 39 35 38 2c 20 20 39 35 38   869,  958,  958
31159 2c 20 20 39 35 38 2c 20 20 38 36 39 2c 20 20 38  ,  958,  869,  8
3115a 39 38 2c 20 20 38 39 38 2c 20 20 39 35 38 2c 20  98,  898,  958, 
3115b 20 37 35 37 2c 0a 20 2f 2a 20 20 20 20 31 30 20   757,. /*    10 
3115c 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
3115d 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38 36 37   958,  958,  867
3115e 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
3115f 33 32 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  32,  958,  958,.
31160 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 39   /*    20 */   9
31161 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31162 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
31163 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31164 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
31165 20 33 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39   30 */   958,  9
31166 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31167 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
31168 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31169 35 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f  58,. /*    40 */
3116a 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
3116b 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
3116c 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
3116d 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
3116e 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 39 35 38  *    50 */   958
3116f 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31170 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31171 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
31172 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20 36  ,  958,. /*    6
31173 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 38 34 31  0 */   958,  841
31174 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31175 35 38 2c 20 20 38 39 38 2c 20 20 38 39 38 2c 20  58,  898,  898, 
31176 20 36 37 33 2c 20 20 37 36 31 2c 20 20 37 39 32   673,  761,  792
31177 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20  ,. /*    70 */  
31178 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
31179 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
3117a 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
3117b 20 39 33 31 2c 20 20 39 33 33 2c 0a 20 2f 2a 20   931,  933,. /* 
3117c 20 20 20 38 30 20 2a 2f 20 20 20 38 30 37 2c 20     80 */   807, 
3117d 20 38 30 36 2c 20 20 38 30 30 2c 20 20 37 39 39   806,  800,  799
3117e 2c 20 20 39 31 31 2c 20 20 37 37 32 2c 20 20 37  ,  911,  772,  7
3117f 39 37 2c 20 20 37 39 30 2c 20 20 37 38 33 2c 20  97,  790,  783, 
31180 20 37 39 34 2c 0a 20 2f 2a 20 20 20 20 39 30 20   794,. /*    90 
31181 2a 2f 20 20 20 38 37 30 2c 20 20 38 36 33 2c 20  */   870,  863, 
31182 20 38 36 34 2c 20 20 38 36 32 2c 20 20 38 36 36   864,  862,  866
31183 2c 20 20 38 37 31 2c 20 20 39 35 38 2c 20 20 37  ,  871,  958,  7
31184 39 33 2c 20 20 38 32 39 2c 20 20 38 34 37 2c 0a  93,  829,  847,.
31185 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 38   /*   100 */   8
31186 32 38 2c 20 20 38 34 36 2c 20 20 38 35 33 2c 20  28,  846,  853, 
31187 20 38 34 35 2c 20 20 38 33 31 2c 20 20 38 34 30   845,  831,  840
31188 2c 20 20 38 33 30 2c 20 20 36 36 35 2c 20 20 38  ,  830,  665,  8
31189 33 32 2c 20 20 38 33 33 2c 0a 20 2f 2a 20 20 20  32,  833,. /*   
3118a 31 31 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  110 */   958,  9
3118b 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
3118c 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
3118d 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
3118e 35 38 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f  58,. /*   120 */
3118f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
31190 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31191 20 39 35 38 2c 20 20 36 36 30 2c 20 20 37 32 36   958,  660,  726
31192 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
31193 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 39 35 38  *   130 */   958
31194 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31195 35 38 2c 20 20 38 33 34 2c 20 20 38 33 35 2c 20  58,  834,  835, 
31196 20 38 35 30 2c 20 20 38 34 39 2c 20 20 38 34 38   850,  849,  848
31197 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31 34  ,  958,. /*   14
31198 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
31199 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
3119a 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
3119b 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
3119c 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20  ,. /*   150 */  
3119d 20 39 35 38 2c 20 20 39 33 38 2c 20 20 39 33 36   958,  938,  936
3119e 2c 20 20 39 35 38 2c 20 20 38 38 32 2c 20 20 39  ,  958,  882,  9
3119f 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
311a0 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
311a1 20 20 31 36 30 20 2a 2f 20 20 20 39 35 38 2c 20    160 */   958, 
311a2 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
311a3 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
311a4 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
311a5 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31 37 30 20   958,. /*   170 
311a6 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
311a7 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
311a8 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
311a9 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  58,  958,  958,.
311aa 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 39   /*   180 */   9
311ab 35 38 2c 20 20 36 34 30 2c 20 20 39 35 38 2c 20  58,  640,  958, 
311ac 20 37 35 37 2c 20 20 37 35 37 2c 20 20 37 35 37   757,  757,  757
311ad 2c 20 20 36 33 34 2c 20 20 39 35 38 2c 20 20 39  ,  634,  958,  9
311ae 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
311af 31 39 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  190 */   958,  9
311b0 35 30 2c 20 20 37 36 31 2c 20 20 37 35 31 2c 20  50,  761,  751, 
311b1 20 37 31 37 2c 20 20 39 35 38 2c 20 20 39 35 38   717,  958,  958
311b2 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
311b3 35 38 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f  58,. /*   200 */
311b4 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
311b5 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
311b6 20 39 35 38 2c 20 20 38 30 32 2c 20 20 37 34 30   958,  802,  740
311b7 2c 20 20 39 32 31 2c 20 20 39 32 33 2c 0a 20 2f  ,  921,  923,. /
311b8 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 39 35 38  *   210 */   958
311b9 2c 20 20 39 30 34 2c 20 20 37 33 38 2c 20 20 36  ,  904,  738,  6
311ba 36 32 2c 20 20 37 35 39 2c 20 20 36 37 35 2c 20  62,  759,  675, 
311bb 20 37 34 39 2c 20 20 36 34 32 2c 20 20 37 39 36   749,  642,  796
311bc 2c 20 20 37 37 34 2c 0a 20 2f 2a 20 20 20 32 32  ,  774,. /*   22
311bd 30 20 2a 2f 20 20 20 37 37 34 2c 20 20 39 31 36  0 */   774,  916
311be 2c 20 20 37 39 36 2c 20 20 39 31 36 2c 20 20 36  ,  796,  916,  6
311bf 39 39 2c 20 20 39 35 38 2c 20 20 37 38 36 2c 20  99,  958,  786, 
311c0 20 39 35 38 2c 20 20 37 38 36 2c 20 20 36 39 36   958,  786,  696
311c1 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20  ,. /*   230 */  
311c2 20 37 38 36 2c 20 20 37 37 34 2c 20 20 37 38 36   786,  774,  786
311c3 2c 20 20 37 38 36 2c 20 20 38 36 35 2c 20 20 39  ,  786,  865,  9
311c4 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
311c5 20 37 35 38 2c 20 20 37 34 39 2c 0a 20 2f 2a 20   758,  749,. /* 
311c6 20 20 32 34 30 20 2a 2f 20 20 20 39 35 38 2c 20    240 */   958, 
311c7 20 39 34 33 2c 20 20 37 36 35 2c 20 20 37 36 35   943,  765,  765
311c8 2c 20 20 39 33 35 2c 20 20 39 33 35 2c 20 20 37  ,  935,  935,  7
311c9 36 35 2c 20 20 38 30 38 2c 20 20 37 33 30 2c 20  65,  808,  730, 
311ca 20 37 39 36 2c 0a 20 2f 2a 20 20 20 32 35 30 20   796,. /*   250 
311cb 2a 2f 20 20 20 37 33 37 2c 20 20 37 33 37 2c 20  */   737,  737, 
311cc 20 37 33 37 2c 20 20 37 33 37 2c 20 20 37 36 35   737,  737,  765
311cd 2c 20 20 37 39 36 2c 20 20 38 30 38 2c 20 20 37  ,  796,  808,  7
311ce 33 30 2c 20 20 37 33 30 2c 20 20 37 36 35 2c 0a  30,  730,  765,.
311cf 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 20 36   /*   260 */   6
311d0 35 37 2c 20 20 39 31 30 2c 20 20 39 30 38 2c 20  57,  910,  908, 
311d1 20 37 36 35 2c 20 20 37 36 35 2c 20 20 36 35 37   765,  765,  657
311d2 2c 20 20 37 36 35 2c 20 20 36 35 37 2c 20 20 37  ,  765,  657,  7
311d3 36 35 2c 20 20 36 35 37 2c 0a 20 2f 2a 20 20 20  65,  657,. /*   
311d4 32 37 30 20 2a 2f 20 20 20 38 37 35 2c 20 20 37  270 */   875,  7
311d5 32 38 2c 20 20 37 32 38 2c 20 20 37 32 38 2c 20  28,  728,  728, 
311d6 20 37 31 33 2c 20 20 38 37 39 2c 20 20 38 37 39   713,  879,  879
311d7 2c 20 20 38 37 35 2c 20 20 37 32 38 2c 20 20 36  ,  875,  728,  6
311d8 39 39 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f  99,. /*   280 */
311d9 20 20 20 37 32 38 2c 20 20 37 31 33 2c 20 20 37     728,  713,  7
311da 32 38 2c 20 20 37 32 38 2c 20 20 37 37 38 2c 20  28,  728,  778, 
311db 20 37 37 33 2c 20 20 37 37 38 2c 20 20 37 37 33   773,  778,  773
311dc 2c 20 20 37 37 38 2c 20 20 37 37 33 2c 0a 20 2f  ,  778,  773,. /
311dd 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 37 36 35  *   290 */   765
311de 2c 20 20 37 36 35 2c 20 20 39 35 38 2c 20 20 37  ,  765,  958,  7
311df 39 31 2c 20 20 37 37 39 2c 20 20 37 38 39 2c 20  91,  779,  789, 
311e0 20 37 38 37 2c 20 20 37 39 36 2c 20 20 39 35 38   787,  796,  958
311e1 2c 20 20 37 31 36 2c 0a 20 2f 2a 20 20 20 33 30  ,  716,. /*   30
311e2 30 20 2a 2f 20 20 20 36 35 30 2c 20 20 36 35 30  0 */   650,  650
311e3 2c 20 20 36 33 39 2c 20 20 36 33 39 2c 20 20 36  ,  639,  639,  6
311e4 33 39 2c 20 20 36 33 39 2c 20 20 39 35 35 2c 20  39,  639,  955, 
311e5 20 39 35 35 2c 20 20 39 35 30 2c 20 20 37 30 31   955,  950,  701
311e6 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20  ,. /*   310 */  
311e7 20 37 30 31 2c 20 20 36 38 33 2c 20 20 39 35 38   701,  683,  958
311e8 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
311e9 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
311ea 20 38 38 34 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   884,  958,. /* 
311eb 20 20 33 32 30 20 2a 2f 20 20 20 39 35 38 2c 20    320 */   958, 
311ec 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
311ed 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
311ee 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
311ef 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 33 30 20   958,. /*   330 
311f0 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
311f1 20 39 35 38 2c 20 20 36 33 35 2c 20 20 39 34 35   958,  635,  945
311f2 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
311f3 34 32 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  42,  958,  958,.
311f4 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20 39   /*   340 */   9
311f5 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
311f6 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
311f7 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
311f8 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
311f9 33 35 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  350 */   958,  9
311fa 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
311fb 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
311fc 2c 20 20 39 35 38 2c 20 20 39 31 34 2c 20 20 39  ,  958,  914,  9
311fd 35 38 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f  58,. /*   360 */
311fe 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
311ff 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31200 20 39 30 37 2c 20 20 39 30 36 2c 20 20 39 35 38   907,  906,  958
31201 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
31202 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 39 35 38  *   370 */   958
31203 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31204 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31205 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
31206 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 38  ,  958,. /*   38
31207 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
31208 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31209 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
3120a 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
3120b 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20  ,. /*   390 */  
3120c 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
3120d 2c 20 20 37 38 38 2c 20 20 39 35 38 2c 20 20 37  ,  788,  958,  7
3120e 38 30 2c 20 20 39 35 38 2c 20 20 38 36 38 2c 20  80,  958,  868, 
3120f 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
31210 20 20 34 30 30 20 2a 2f 20 20 20 39 35 38 2c 20    400 */   958, 
31211 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
31212 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31213 35 38 2c 20 20 39 35 38 2c 20 20 37 34 33 2c 20  58,  958,  743, 
31214 20 38 31 37 2c 0a 20 2f 2a 20 20 20 34 31 30 20   817,. /*   410 
31215 2a 2f 20 20 20 39 35 38 2c 20 20 38 31 36 2c 20  */   958,  816, 
31216 20 38 32 30 2c 20 20 38 31 35 2c 20 20 36 36 37   820,  815,  667
31217 2c 20 20 39 35 38 2c 20 20 36 34 38 2c 20 20 39  ,  958,  648,  9
31218 35 38 2c 20 20 36 33 31 2c 20 20 36 33 36 2c 0a  58,  631,  636,.
31219 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20 20 39   /*   420 */   9
3121a 35 34 2c 20 20 39 35 37 2c 20 20 39 35 36 2c 20  54,  957,  956, 
3121b 20 39 35 33 2c 20 20 39 35 32 2c 20 20 39 35 31   953,  952,  951
3121c 2c 20 20 39 34 36 2c 20 20 39 34 34 2c 20 20 39  ,  946,  944,  9
3121d 34 31 2c 20 20 39 34 30 2c 0a 20 2f 2a 20 20 20  41,  940,. /*   
3121e 34 33 30 20 2a 2f 20 20 20 39 33 39 2c 20 20 39  430 */   939,  9
3121f 33 37 2c 20 20 39 33 34 2c 20 20 39 33 30 2c 20  37,  934,  930, 
31220 20 38 38 38 2c 20 20 38 38 36 2c 20 20 38 39 33   888,  886,  893
31221 2c 20 20 38 39 32 2c 20 20 38 39 31 2c 20 20 38  ,  892,  891,  8
31222 39 30 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f  90,. /*   440 */
31223 20 20 20 38 38 39 2c 20 20 38 38 37 2c 20 20 38     889,  887,  8
31224 38 35 2c 20 20 38 38 33 2c 20 20 38 30 33 2c 20  85,  883,  803, 
31225 20 38 30 31 2c 20 20 37 39 38 2c 20 20 37 39 35   801,  798,  795
31226 2c 20 20 39 32 39 2c 20 20 38 38 31 2c 0a 20 2f  ,  929,  881,. /
31227 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 37 33 39  *   450 */   739
31228 2c 20 20 37 33 36 2c 20 20 37 33 35 2c 20 20 36  ,  736,  735,  6
31229 35 36 2c 20 20 39 34 37 2c 20 20 39 31 33 2c 20  56,  947,  913, 
3122a 20 39 32 32 2c 20 20 39 32 30 2c 20 20 38 30 39   922,  920,  809
3122b 2c 20 20 39 31 39 2c 0a 20 2f 2a 20 20 20 34 36  ,  919,. /*   46
3122c 30 20 2a 2f 20 20 20 39 31 38 2c 20 20 39 31 37  0 */   918,  917
3122d 2c 20 20 39 31 35 2c 20 20 39 31 32 2c 20 20 38  ,  915,  912,  8
3122e 39 39 2c 20 20 38 30 35 2c 20 20 38 30 34 2c 20  99,  805,  804, 
3122f 20 37 33 31 2c 20 20 38 37 33 2c 20 20 38 37 32   731,  873,  872
31230 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20  ,. /*   470 */  
31231 20 36 35 39 2c 20 20 39 30 33 2c 20 20 39 30 32   659,  903,  902
31232 2c 20 20 39 30 31 2c 20 20 39 30 35 2c 20 20 39  ,  901,  905,  9
31233 30 39 2c 20 20 39 30 30 2c 20 20 37 36 37 2c 20  09,  900,  767, 
31234 20 36 35 38 2c 20 20 36 35 35 2c 0a 20 2f 2a 20   658,  655,. /* 
31235 20 20 34 38 30 20 2a 2f 20 20 20 36 36 34 2c 20    480 */   664, 
31236 20 37 32 30 2c 20 20 37 31 39 2c 20 20 37 32 37   720,  719,  727
31237 2c 20 20 37 32 35 2c 20 20 37 32 34 2c 20 20 37  ,  725,  724,  7
31238 32 33 2c 20 20 37 32 32 2c 20 20 37 32 31 2c 20  23,  722,  721, 
31239 20 37 31 38 2c 0a 20 2f 2a 20 20 20 34 39 30 20   718,. /*   490 
3123a 2a 2f 20 20 20 36 36 36 2c 20 20 36 37 34 2c 20  */   666,  674, 
3123b 20 36 38 35 2c 20 20 37 31 32 2c 20 20 36 39 38   685,  712,  698
3123c 2c 20 20 36 39 37 2c 20 20 38 37 38 2c 20 20 38  ,  697,  878,  8
3123d 38 30 2c 20 20 38 37 37 2c 20 20 38 37 36 2c 0a  80,  877,  876,.
3123e 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20 20 37   /*   500 */   7
3123f 30 35 2c 20 20 37 31 30 2c 20 20 37 30 39 2c 20  05,  710,  709, 
31240 20 37 30 38 2c 20 20 37 30 37 2c 20 20 37 30 36   708,  707,  706
31241 2c 20 20 37 30 34 2c 20 20 37 30 33 2c 20 20 37  ,  704,  703,  7
31242 30 32 2c 20 20 36 39 35 2c 0a 20 2f 2a 20 20 20  02,  695,. /*   
31243 35 31 30 20 2a 2f 20 20 20 36 39 34 2c 20 20 37  510 */   694,  7
31244 30 30 2c 20 20 36 39 33 2c 20 20 37 31 35 2c 20  00,  693,  715, 
31245 20 37 31 34 2c 20 20 37 31 31 2c 20 20 36 39 32   714,  711,  692
31246 2c 20 20 37 33 34 2c 20 20 37 33 33 2c 20 20 37  ,  734,  733,  7
31247 33 32 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f  32,. /*   520 */
31248 20 20 20 37 32 39 2c 20 20 36 39 31 2c 20 20 36     729,  691,  6
31249 39 30 2c 20 20 36 38 39 2c 20 20 38 32 30 2c 20  90,  689,  820, 
3124a 20 36 38 38 2c 20 20 36 38 37 2c 20 20 38 32 36   688,  687,  826
3124b 2c 20 20 38 32 35 2c 20 20 38 31 33 2c 0a 20 2f  ,  825,  813,. /
3124c 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 38 35 37  *   530 */   857
3124d 2c 20 20 37 35 34 2c 20 20 37 35 33 2c 20 20 37  ,  754,  753,  7
3124e 35 32 2c 20 20 37 36 34 2c 20 20 37 36 33 2c 20  52,  764,  763, 
3124f 20 37 37 36 2c 20 20 37 37 35 2c 20 20 38 31 31   776,  775,  811
31250 2c 20 20 38 31 30 2c 0a 20 2f 2a 20 20 20 35 34  ,  810,. /*   54
31251 30 20 2a 2f 20 20 20 37 37 37 2c 20 20 37 36 32  0 */   777,  762
31252 2c 20 20 37 35 36 2c 20 20 37 35 35 2c 20 20 37  ,  756,  755,  7
31253 37 31 2c 20 20 37 37 30 2c 20 20 37 36 39 2c 20  71,  770,  769, 
31254 20 37 36 38 2c 20 20 37 36 30 2c 20 20 37 35 30   768,  760,  750
31255 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20  ,. /*   550 */  
31256 20 37 38 32 2c 20 20 37 38 35 2c 20 20 37 38 34   782,  785,  784
31257 2c 20 20 37 38 31 2c 20 20 38 34 32 2c 20 20 38  ,  781,  842,  8
31258 35 39 2c 20 20 37 36 36 2c 20 20 38 35 36 2c 20  59,  766,  856, 
31259 20 39 32 38 2c 20 20 39 32 37 2c 0a 20 2f 2a 20   928,  927,. /* 
3125a 20 20 35 36 30 20 2a 2f 20 20 20 39 32 36 2c 20    560 */   926, 
3125b 20 39 32 35 2c 20 20 39 32 34 2c 20 20 38 36 31   925,  924,  861
3125c 2c 20 20 38 36 30 2c 20 20 38 32 37 2c 20 20 38  ,  860,  827,  8
3125d 32 34 2c 20 20 36 37 38 2c 20 20 36 37 39 2c 20  24,  678,  679, 
3125e 20 38 39 37 2c 0a 20 2f 2a 20 20 20 35 37 30 20   897,. /*   570 
3125f 2a 2f 20 20 20 38 39 35 2c 20 20 38 39 36 2c 20  */   895,  896, 
31260 20 38 39 34 2c 20 20 36 38 31 2c 20 20 36 38 30   894,  681,  680
31261 2c 20 20 36 37 37 2c 20 20 36 37 36 2c 20 20 38  ,  677,  676,  8
31262 35 38 2c 20 20 37 34 35 2c 20 20 37 34 34 2c 0a  58,  745,  744,.
31263 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20 20 38   /*   580 */   8
31264 35 34 2c 20 20 38 35 31 2c 20 20 38 34 33 2c 20  54,  851,  843, 
31265 20 38 33 38 2c 20 20 38 35 35 2c 20 20 38 35 32   838,  855,  852
31266 2c 20 20 38 34 34 2c 20 20 38 33 39 2c 20 20 38  ,  844,  839,  8
31267 33 37 2c 20 20 38 33 36 2c 0a 20 2f 2a 20 20 20  37,  836,. /*   
31268 35 39 30 20 2a 2f 20 20 20 38 32 32 2c 20 20 38  590 */   822,  8
31269 32 31 2c 20 20 38 31 39 2c 20 20 38 31 38 2c 20  21,  819,  818, 
3126a 20 38 31 34 2c 20 20 38 32 33 2c 20 20 36 36 39   814,  823,  669
3126b 2c 20 20 37 34 36 2c 20 20 37 34 32 2c 20 20 37  ,  746,  742,  7
3126c 34 31 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f  41,. /*   600 */
3126d 20 20 20 38 31 32 2c 20 20 37 34 38 2c 20 20 37     812,  748,  7
3126e 34 37 2c 20 20 36 38 36 2c 20 20 36 38 34 2c 20  47,  686,  684, 
3126f 20 36 38 32 2c 20 20 36 36 33 2c 20 20 36 36 31   682,  663,  661
31270 2c 20 20 36 35 34 2c 20 20 36 35 32 2c 0a 20 2f  ,  654,  652,. /
31271 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 36 35 31  *   610 */   651
31272 2c 20 20 36 35 33 2c 20 20 36 34 39 2c 20 20 36  ,  653,  649,  6
31273 34 37 2c 20 20 36 34 36 2c 20 20 36 34 35 2c 20  47,  646,  645, 
31274 20 36 34 34 2c 20 20 36 34 33 2c 20 20 36 37 32   644,  643,  672
31275 2c 20 20 36 37 31 2c 0a 20 2f 2a 20 20 20 36 32  ,  671,. /*   62
31276 30 20 2a 2f 20 20 20 36 37 30 2c 20 20 36 36 38  0 */   670,  668
31277 2c 20 20 36 36 37 2c 20 20 36 34 31 2c 20 20 36  ,  667,  641,  6
31278 33 38 2c 20 20 36 33 37 2c 20 20 36 33 33 2c 20  38,  637,  633, 
31279 20 36 33 32 2c 20 20 36 33 30 2c 0a 7d 3b 0a 0a   632,  630,.};..
3127a 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 61 62 6c  /* The next tabl
3127b 65 20 6d 61 70 73 20 74 6f 6b 65 6e 73 20 69 6e  e maps tokens in
3127c 74 6f 20 66 61 6c 6c 62 61 63 6b 20 74 6f 6b 65  to fallback toke
3127d 6e 73 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72  ns.  If a constr
3127e 75 63 74 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20  uct.** like the 
3127f 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 20 0a 2a  following:.** .*
31280 2a 20 20 20 20 20 20 25 66 61 6c 6c 62 61 63 6b  *      %fallback
31281 20 49 44 20 58 20 59 20 5a 2e 0a 2a 2a 0a 2a 2a   ID X Y Z..**.**
31282 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65 20   appears in the 
31283 67 72 61 6d 6d 61 72 2c 20 74 68 65 6e 20 49 44  grammar, then ID
31284 20 62 65 63 6f 6d 65 73 20 61 20 66 61 6c 6c 62   becomes a fallb
31285 61 63 6b 20 74 6f 6b 65 6e 20 66 6f 72 20 58 2c  ack token for X,
31286 20 59 2c 0a 2a 2a 20 61 6e 64 20 5a 2e 20 20 57   Y,.** and Z.  W
31287 68 65 6e 65 76 65 72 20 6f 6e 65 20 6f 66 20 74  henever one of t
31288 68 65 20 74 6f 6b 65 6e 73 20 58 2c 20 59 2c 20  he tokens X, Y, 
31289 6f 72 20 5a 20 69 73 20 69 6e 70 75 74 20 74 6f  or Z is input to
3128a 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 62   the parser.** b
3128b 75 74 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 70  ut it does not p
3128c 61 72 73 65 2c 20 74 68 65 20 74 79 70 65 20 6f  arse, the type o
3128d 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 63  f the token is c
3128e 68 61 6e 67 65 64 20 74 6f 20 49 44 20 61 6e 64  hanged to ID and
3128f 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20 69 73  .** the parse is
31290 20 72 65 74 72 69 65 64 20 62 65 66 6f 72 65 20   retried before 
31291 61 6e 20 65 72 72 6f 72 20 69 73 20 74 68 72 6f  an error is thro
31292 77 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 59 59  wn..*/.#ifdef YY
31293 46 41 4c 4c 42 41 43 4b 0a 73 74 61 74 69 63 20  FALLBACK.static 
31294 63 6f 6e 73 74 20 59 59 43 4f 44 45 54 59 50 45  const YYCODETYPE
31295 20 79 79 46 61 6c 6c 62 61 63 6b 5b 5d 20 3d 20   yyFallback[] = 
31296 7b 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  {.    0,  /*    
31297 20 20 20 20 20 20 24 20 3d 3e 20 6e 6f 74 68 69        $ => nothi
31298 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
31299 20 20 20 20 20 20 20 53 45 4d 49 20 3d 3e 20 6e         SEMI => n
3129a 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c  othing */.   26,
3129b 20 20 2f 2a 20 20 20 20 45 58 50 4c 41 49 4e 20    /*    EXPLAIN 
3129c 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
3129d 20 2f 2a 20 20 20 20 20 20 51 55 45 52 59 20 3d   /*      QUERY =
3129e 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
3129f 2f 2a 20 20 20 20 20 20 20 50 4c 41 4e 20 3d 3e  /*       PLAN =>
312a0 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
312a1 2a 20 20 20 20 20 20 42 45 47 49 4e 20 3d 3e 20  *      BEGIN => 
312a2 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ID */.    0,  /*
312a3 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 3d 3e 20   TRANSACTION => 
312a4 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36  nothing */.   26
312a5 2c 20 20 2f 2a 20 20 20 44 45 46 45 52 52 45 44  ,  /*   DEFERRED
312a6 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
312a7 20 20 2f 2a 20 20 49 4d 4d 45 44 49 41 54 45 20    /*  IMMEDIATE 
312a8 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
312a9 20 2f 2a 20 20 45 58 43 4c 55 53 49 56 45 20 3d   /*  EXCLUSIVE =
312aa 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
312ab 2f 2a 20 20 20 20 20 43 4f 4d 4d 49 54 20 3d 3e  /*     COMMIT =>
312ac 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32   nothing */.   2
312ad 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 45 4e  6,  /*        EN
312ae 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  D => ID */.   26
312af 2c 20 20 2f 2a 20 20 20 52 4f 4c 4c 42 41 43 4b  ,  /*   ROLLBACK
312b0 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
312b1 20 20 2f 2a 20 20 53 41 56 45 50 4f 49 4e 54 20    /*  SAVEPOINT 
312b2 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
312b3 20 2f 2a 20 20 20 20 52 45 4c 45 41 53 45 20 3d   /*    RELEASE =
312b4 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
312b5 2f 2a 20 20 20 20 20 20 20 20 20 54 4f 20 3d 3e  /*         TO =>
312b6 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
312b7 30 2c 20 20 2f 2a 20 20 20 20 20 20 54 41 42 4c  0,  /*      TABL
312b8 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  E => nothing */.
312b9 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 43      0,  /*     C
312ba 52 45 41 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67  REATE => nothing
312bb 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
312bc 20 20 20 20 20 20 20 49 46 20 3d 3e 20 49 44 20         IF => ID 
312bd 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
312be 20 20 20 20 20 4e 4f 54 20 3d 3e 20 6e 6f 74 68       NOT => noth
312bf 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
312c0 2a 20 20 20 20 20 45 58 49 53 54 53 20 3d 3e 20  *     EXISTS => 
312c1 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36  nothing */.   26
312c2 2c 20 20 2f 2a 20 20 20 20 20 20 20 54 45 4d 50  ,  /*       TEMP
312c3 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c   => ID */.    0,
312c4 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4c 50 20    /*         LP 
312c5 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
312c6 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20    0,  /*        
312c7 20 52 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a   RP => nothing *
312c8 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
312c9 20 20 20 20 20 41 53 20 3d 3e 20 6e 6f 74 68 69       AS => nothi
312ca 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
312cb 20 20 20 20 20 20 43 4f 4d 4d 41 20 3d 3e 20 6e        COMMA => n
312cc 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
312cd 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49 44 20    /*         ID 
312ce 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
312cf 20 20 30 2c 20 20 2f 2a 20 20 20 20 49 4e 44 45    0,  /*    INDE
312d0 58 45 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  XED => nothing *
312d1 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
312d2 20 20 41 42 4f 52 54 20 3d 3e 20 49 44 20 2a 2f    ABORT => ID */
312d3 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
312d4 41 43 54 49 4f 4e 20 3d 3e 20 49 44 20 2a 2f 0a  ACTION => ID */.
312d5 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
312d6 41 46 54 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20  AFTER => ID */. 
312d7 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 41 4e 41    26,  /*    ANA
312d8 4c 59 5a 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  LYZE => ID */.  
312d9 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   26,  /*        
312da 41 53 43 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ASC => ID */.   
312db 32 36 2c 20 20 2f 2a 20 20 20 20 20 41 54 54 41  26,  /*     ATTA
312dc 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  CH => ID */.   2
312dd 36 2c 20 20 2f 2a 20 20 20 20 20 42 45 46 4f 52  6,  /*     BEFOR
312de 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  E => ID */.   26
312df 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 42 59  ,  /*         BY
312e0 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
312e1 20 20 2f 2a 20 20 20 20 43 41 53 43 41 44 45 20    /*    CASCADE 
312e2 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
312e3 20 2f 2a 20 20 20 20 20 20 20 43 41 53 54 20 3d   /*       CAST =
312e4 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
312e5 2f 2a 20 20 20 43 4f 4c 55 4d 4e 4b 57 20 3d 3e  /*   COLUMNKW =>
312e6 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
312e7 2a 20 20 20 43 4f 4e 46 4c 49 43 54 20 3d 3e 20  *   CONFLICT => 
312e8 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
312e9 20 20 20 44 41 54 41 42 41 53 45 20 3d 3e 20 49     DATABASE => I
312ea 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
312eb 20 20 20 20 20 20 44 45 53 43 20 3d 3e 20 49 44        DESC => ID
312ec 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
312ed 20 20 20 44 45 54 41 43 48 20 3d 3e 20 49 44 20     DETACH => ID 
312ee 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
312ef 20 20 20 20 45 41 43 48 20 3d 3e 20 49 44 20 2a      EACH => ID *
312f0 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
312f1 20 20 20 46 41 49 4c 20 3d 3e 20 49 44 20 2a 2f     FAIL => ID */
312f2 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
312f3 20 20 20 46 4f 52 20 3d 3e 20 49 44 20 2a 2f 0a     FOR => ID */.
312f4 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 49     26,  /*     I
312f5 47 4e 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20  GNORE => ID */. 
312f6 20 20 32 36 2c 20 20 2f 2a 20 20 49 4e 49 54 49    26,  /*  INITI
312f7 41 4c 4c 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  ALLY => ID */.  
312f8 20 32 36 2c 20 20 2f 2a 20 20 20 20 49 4e 53 54   26,  /*    INST
312f9 45 41 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  EAD => ID */.   
312fa 32 36 2c 20 20 2f 2a 20 20 20 20 4c 49 4b 45 5f  26,  /*    LIKE_
312fb 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  KW => ID */.   2
312fc 36 2c 20 20 2f 2a 20 20 20 20 20 20 4d 41 54 43  6,  /*      MATC
312fd 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  H => ID */.   26
312fe 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4e 4f  ,  /*         NO
312ff 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
31300 20 20 2f 2a 20 20 20 20 20 20 20 20 4b 45 59 20    /*        KEY 
31301 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
31302 20 2f 2a 20 20 20 20 20 20 20 20 20 4f 46 20 3d   /*         OF =
31303 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
31304 2f 2a 20 20 20 20 20 4f 46 46 53 45 54 20 3d 3e  /*     OFFSET =>
31305 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
31306 2a 20 20 20 20 20 50 52 41 47 4d 41 20 3d 3e 20  *     PRAGMA => 
31307 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
31308 20 20 20 20 20 20 52 41 49 53 45 20 3d 3e 20 49        RAISE => I
31309 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
3130a 20 20 20 52 45 50 4c 41 43 45 20 3d 3e 20 49 44     REPLACE => ID
3130b 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
3130c 20 52 45 53 54 52 49 43 54 20 3d 3e 20 49 44 20   RESTRICT => ID 
3130d 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
3130e 20 20 20 20 20 52 4f 57 20 3d 3e 20 49 44 20 2a       ROW => ID *
3130f 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
31310 54 52 49 47 47 45 52 20 3d 3e 20 49 44 20 2a 2f  TRIGGER => ID */
31311 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
31312 56 41 43 55 55 4d 20 3d 3e 20 49 44 20 2a 2f 0a  VACUUM => ID */.
31313 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
31314 20 56 49 45 57 20 3d 3e 20 49 44 20 2a 2f 0a 20   VIEW => ID */. 
31315 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 56 49 52    26,  /*    VIR
31316 54 55 41 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  TUAL => ID */.  
31317 20 32 36 2c 20 20 2f 2a 20 20 20 20 52 45 49 4e   26,  /*    REIN
31318 44 45 58 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  DEX => ID */.   
31319 32 36 2c 20 20 2f 2a 20 20 20 20 20 52 45 4e 41  26,  /*     RENA
3131a 4d 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ME => ID */.   2
3131b 36 2c 20 20 2f 2a 20 20 20 43 54 49 4d 45 5f 4b  6,  /*   CTIME_K
3131c 57 20 3d 3e 20 49 44 20 2a 2f 0a 7d 3b 0a 23 65  W => ID */.};.#e
3131d 6e 64 69 66 20 2f 2a 20 59 59 46 41 4c 4c 42 41  ndif /* YYFALLBA
3131e 43 4b 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 66 6f  CK */../* The fo
3131f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
31320 65 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 73  e represents a s
31321 69 6e 67 6c 65 20 65 6c 65 6d 65 6e 74 20 6f 66  ingle element of
31322 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 72 27 73   the.** parser's
31323 20 73 74 61 63 6b 2e 20 20 49 6e 66 6f 72 6d 61   stack.  Informa
31324 74 69 6f 6e 20 73 74 6f 72 65 64 20 69 6e 63 6c  tion stored incl
31325 75 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20  udes:.**.**   + 
31326 20 54 68 65 20 73 74 61 74 65 20 6e 75 6d 62 65   The state numbe
31327 72 20 66 6f 72 20 74 68 65 20 70 61 72 73 65 72  r for the parser
31328 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f   at this level o
31329 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a  f the stack..**.
3132a 2a 2a 20 20 20 2b 20 20 54 68 65 20 76 61 6c 75  **   +  The valu
3132b 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 73  e of the token s
3132c 74 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c 65  tored at this le
3132d 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b  vel of the stack
3132e 2e 0a 2a 2a 20 20 20 20 20 20 28 49 6e 20 6f 74  ..**      (In ot
3132f 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 22  her words, the "
31330 6d 61 6a 6f 72 22 20 74 6f 6b 65 6e 2e 29 0a 2a  major" token.).*
31331 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 73 65  *.**   +  The se
31332 6d 61 6e 74 69 63 20 76 61 6c 75 65 20 73 74 6f  mantic value sto
31333 72 65 64 20 61 74 20 74 68 69 73 20 6c 65 76 65  red at this leve
31334 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 20  l of the stack. 
31335 20 54 68 69 73 20 69 73 0a 2a 2a 20 20 20 20 20   This is.**     
31336 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   the information
31337 20 75 73 65 64 20 62 79 20 74 68 65 20 61 63 74   used by the act
31338 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 69 6e 20  ion routines in 
31339 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2a 20  the grammar..** 
3133a 20 20 20 20 20 49 74 20 69 73 20 73 6f 6d 65 74       It is somet
3133b 69 6d 65 73 20 63 61 6c 6c 65 64 20 74 68 65 20  imes called the 
3133c 22 6d 69 6e 6f 72 22 20 74 6f 6b 65 6e 2e 0a 2a  "minor" token..*
3133d 2f 0a 73 74 72 75 63 74 20 79 79 53 74 61 63 6b  /.struct yyStack
3133e 45 6e 74 72 79 20 7b 0a 20 20 59 59 41 43 54 49  Entry {.  YYACTI
3133f 4f 4e 54 59 50 45 20 73 74 61 74 65 6e 6f 3b 20  ONTYPE stateno; 
31340 20 2f 2a 20 54 68 65 20 73 74 61 74 65 2d 6e 75   /* The state-nu
31341 6d 62 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45  mber */.  YYCODE
31342 54 59 50 45 20 6d 61 6a 6f 72 3b 20 20 20 20 20  TYPE major;     
31343 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f   /* The major to
31344 6b 65 6e 20 76 61 6c 75 65 2e 20 20 54 68 69 73  ken value.  This
31345 20 69 73 20 74 68 65 20 63 6f 64 65 0a 20 20 20   is the code.   
31346 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31347 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
31348 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20 61 74  for the token at
31349 20 74 68 69 73 20 73 74 61 63 6b 20 6c 65 76 65   this stack leve
3134a 6c 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59  l */.  YYMINORTY
3134b 50 45 20 6d 69 6e 6f 72 3b 20 20 20 20 20 2f 2a  PE minor;     /*
3134c 20 54 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69   The user-suppli
3134d 65 64 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 76  ed minor token v
3134e 61 6c 75 65 2e 20 20 54 68 69 73 0a 20 20 20 20  alue.  This.    
3134f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31350 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 76       ** is the v
31351 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65  alue of the toke
31352 6e 20 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66  n  */.};.typedef
31353 20 73 74 72 75 63 74 20 79 79 53 74 61 63 6b 45   struct yyStackE
31354 6e 74 72 79 20 79 79 53 74 61 63 6b 45 6e 74 72  ntry yyStackEntr
31355 79 3b 0a 0a 2f 2a 20 54 68 65 20 73 74 61 74 65  y;../* The state
31356 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 20 69   of the parser i
31357 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 6e  s completely con
31358 74 61 69 6e 65 64 20 69 6e 20 61 6e 20 69 6e 73  tained in an ins
31359 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20  tance of.** the 
3135a 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
3135b 75 72 65 20 2a 2f 0a 73 74 72 75 63 74 20 79 79  ure */.struct yy
3135c 50 61 72 73 65 72 20 7b 0a 20 20 69 6e 74 20 79  Parser {.  int y
3135d 79 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20  yidx;           
3135e 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
3135f 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74  x of top element
31360 20 69 6e 20 73 74 61 63 6b 20 2a 2f 0a 23 69 66   in stack */.#if
31361 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54  def YYTRACKMAXST
31362 41 43 4b 44 45 50 54 48 0a 20 20 69 6e 74 20 79  ACKDEPTH.  int y
31363 79 69 64 78 4d 61 78 3b 20 20 20 20 20 20 20 20  yidxMax;        
31364 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
31365 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 79 79 69  mum value of yyi
31366 64 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69  dx */.#endif.  i
31367 6e 74 20 79 79 65 72 72 63 6e 74 3b 20 20 20 20  nt yyerrcnt;    
31368 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31369 53 68 69 66 74 73 20 6c 65 66 74 20 62 65 66 6f  Shifts left befo
3136a 72 65 20 6f 75 74 20 6f 66 20 74 68 65 20 65 72  re out of the er
3136b 72 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ror */.  sqlite3
3136c 50 61 72 73 65 72 41 52 47 5f 53 44 45 43 4c 20  ParserARG_SDECL 
3136d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3136e 2a 20 41 20 70 6c 61 63 65 20 74 6f 20 68 6f 6c  * A place to hol
3136f 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e  d %extra_argumen
31370 74 20 2a 2f 0a 23 69 66 20 59 59 53 54 41 43 4b  t */.#if YYSTACK
31371 44 45 50 54 48 3c 3d 30 0a 20 20 69 6e 74 20 79  DEPTH<=0.  int y
31372 79 73 74 6b 73 7a 3b 20 20 20 20 20 20 20 20 20  ystksz;         
31373 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
31374 65 6e 74 20 73 69 64 65 20 6f 66 20 74 68 65 20  ent side of the 
31375 73 74 61 63 6b 20 2a 2f 0a 20 20 79 79 53 74 61  stack */.  yySta
31376 63 6b 45 6e 74 72 79 20 2a 79 79 73 74 61 63 6b  ckEntry *yystack
31377 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
31378 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20 2a  parser's stack *
31379 2f 0a 23 65 6c 73 65 0a 20 20 79 79 53 74 61 63  /.#else.  yyStac
3137a 6b 45 6e 74 72 79 20 79 79 73 74 61 63 6b 5b 59  kEntry yystack[Y
3137b 59 53 54 41 43 4b 44 45 50 54 48 5d 3b 20 20 2f  YSTACKDEPTH];  /
3137c 2a 20 54 68 65 20 70 61 72 73 65 72 27 73 20 73  * The parser's s
3137d 74 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  tack */.#endif.}
3137e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
3137f 20 79 79 50 61 72 73 65 72 20 79 79 50 61 72 73   yyParser yyPars
31380 65 72 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  er;..#ifndef NDE
31381 42 55 47 0a 73 74 61 74 69 63 20 46 49 4c 45 20  BUG.static FILE 
31382 2a 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20 30  *yyTraceFILE = 0
31383 3b 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 79  ;.static char *y
31384 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 30  yTracePrompt = 0
31385 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  ;.#endif /* NDEB
31386 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e  UG */..#ifndef N
31387 44 45 42 55 47 0a 2f 2a 20 0a 2a 2a 20 54 75 72  DEBUG./* .** Tur
31388 6e 20 70 61 72 73 65 72 20 74 72 61 63 69 6e 67  n parser tracing
31389 20 6f 6e 20 62 79 20 67 69 76 69 6e 67 20 61 20   on by giving a 
3138a 73 74 72 65 61 6d 20 74 6f 20 77 68 69 63 68 20  stream to which 
3138b 74 6f 20 77 72 69 74 65 20 74 68 65 20 74 72 61  to write the tra
3138c 63 65 0a 2a 2a 20 61 6e 64 20 61 20 70 72 6f 6d  ce.** and a prom
3138d 70 74 20 74 6f 20 70 72 65 66 61 63 65 20 65 61  pt to preface ea
3138e 63 68 20 74 72 61 63 65 20 6d 65 73 73 61 67 65  ch trace message
3138f 2e 20 20 54 72 61 63 69 6e 67 20 69 73 20 74 75  .  Tracing is tu
31390 72 6e 65 64 20 6f 66 66 0a 2a 2a 20 62 79 20 6d  rned off.** by m
31391 61 6b 69 6e 67 20 65 69 74 68 65 72 20 61 72 67  aking either arg
31392 75 6d 65 6e 74 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a  ument NULL .**.*
31393 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c  * Inputs:.** <ul
31394 3e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 46 49 4c 45  >.** <li> A FILE
31395 2a 20 74 6f 20 77 68 69 63 68 20 74 72 61 63 65  * to which trace
31396 20 6f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 62   output should b
31397 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 20 20 20  e written..**   
31398 20 20 20 49 66 20 4e 55 4c 4c 2c 20 74 68 65 6e     If NULL, then
31399 20 74 72 61 63 69 6e 67 20 69 73 20 74 75 72 6e   tracing is turn
3139a 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 6c 69 3e 20  ed off..** <li> 
3139b 41 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 20  A prefix string 
3139c 77 72 69 74 74 65 6e 20 61 74 20 74 68 65 20 62  written at the b
3139d 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72  eginning of ever
3139e 79 0a 2a 2a 20 20 20 20 20 20 6c 69 6e 65 20 6f  y.**      line o
3139f 66 20 74 72 61 63 65 20 6f 75 74 70 75 74 2e 20  f trace output. 
313a0 20 49 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74   If NULL, then t
313a1 72 61 63 69 6e 67 20 69 73 0a 2a 2a 20 20 20 20  racing is.**    
313a2 20 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a    turned off..**
313a3 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74   </ul>.**.** Out
313a4 70 75 74 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a  puts:.** None..*
313a5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
313a6 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72   void sqlite3Par
313a7 73 65 72 54 72 61 63 65 28 46 49 4c 45 20 2a 54  serTrace(FILE *T
313a8 72 61 63 65 46 49 4c 45 2c 20 63 68 61 72 20 2a  raceFILE, char *
313a9 7a 54 72 61 63 65 50 72 6f 6d 70 74 29 7b 0a 20  zTracePrompt){. 
313aa 20 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20 54   yyTraceFILE = T
313ab 72 61 63 65 46 49 4c 45 3b 0a 20 20 79 79 54 72  raceFILE;.  yyTr
313ac 61 63 65 50 72 6f 6d 70 74 20 3d 20 7a 54 72 61  acePrompt = zTra
313ad 63 65 50 72 6f 6d 70 74 3b 0a 20 20 69 66 28 20  cePrompt;.  if( 
313ae 79 79 54 72 61 63 65 46 49 4c 45 3d 3d 30 20 29  yyTraceFILE==0 )
313af 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d   yyTracePrompt =
313b0 20 30 3b 0a 20 20 65 6c 73 65 20 69 66 28 20 79   0;.  else if( y
313b1 79 54 72 61 63 65 50 72 6f 6d 70 74 3d 3d 30 20  yTracePrompt==0 
313b2 29 20 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20  ) yyTraceFILE = 
313b3 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e  0;.}.#endif /* N
313b4 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65  DEBUG */..#ifnde
313b5 66 20 4e 44 45 42 55 47 0a 2f 2a 20 46 6f 72 20  f NDEBUG./* For 
313b6 74 72 61 63 69 6e 67 20 73 68 69 66 74 73 2c 20  tracing shifts, 
313b7 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c  the names of all
313b8 20 74 65 72 6d 69 6e 61 6c 73 20 61 6e 64 20 6e   terminals and n
313b9 6f 6e 74 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20 61  onterminals.** a
313ba 72 65 20 72 65 71 75 69 72 65 64 2e 20 20 54 68  re required.  Th
313bb 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  e following tabl
313bc 65 20 73 75 70 70 6c 69 65 73 20 74 68 65 73 65  e supplies these
313bd 20 6e 61 6d 65 73 20 2a 2f 0a 73 74 61 74 69 63   names */.static
313be 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
313bf 73 74 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 5d  st yyTokenName[]
313c0 20 3d 20 7b 20 0a 20 20 22 24 22 2c 20 20 20 20   = { .  "$",    
313c1 20 20 20 20 20 20 20 20 20 22 53 45 4d 49 22 2c           "SEMI",
313c2 20 20 20 20 20 20 20 20 20 20 22 45 58 50 4c 41            "EXPLA
313c3 49 4e 22 2c 20 20 20 20 20 20 20 22 51 55 45 52  IN",       "QUER
313c4 59 22 2c 20 20 20 20 20 20 20 0a 20 20 22 50 4c  Y",       .  "PL
313c5 41 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22 42  AN",          "B
313c6 45 47 49 4e 22 2c 20 20 20 20 20 20 20 20 20 22  EGIN",         "
313c7 54 52 41 4e 53 41 43 54 49 4f 4e 22 2c 20 20 20  TRANSACTION",   
313c8 22 44 45 46 45 52 52 45 44 22 2c 20 20 20 20 0a  "DEFERRED",    .
313c9 20 20 22 49 4d 4d 45 44 49 41 54 45 22 2c 20 20    "IMMEDIATE",  
313ca 20 20 20 22 45 58 43 4c 55 53 49 56 45 22 2c 20     "EXCLUSIVE", 
313cb 20 20 20 20 22 43 4f 4d 4d 49 54 22 2c 20 20 20      "COMMIT",   
313cc 20 20 20 20 20 22 45 4e 44 22 2c 20 20 20 20 20       "END",     
313cd 20 20 20 20 0a 20 20 22 52 4f 4c 4c 42 41 43 4b      .  "ROLLBACK
313ce 22 2c 20 20 20 20 20 20 22 53 41 56 45 50 4f 49  ",      "SAVEPOI
313cf 4e 54 22 2c 20 20 20 20 20 22 52 45 4c 45 41 53  NT",     "RELEAS
313d0 45 22 2c 20 20 20 20 20 20 20 22 54 4f 22 2c 20  E",       "TO", 
313d1 20 20 20 20 20 20 20 20 20 0a 20 20 22 54 41 42           .  "TAB
313d2 4c 45 22 2c 20 20 20 20 20 20 20 20 20 22 43 52  LE",         "CR
313d3 45 41 54 45 22 2c 20 20 20 20 20 20 20 20 22 49  EATE",        "I
313d4 46 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  F",            "
313d5 4e 4f 54 22 2c 20 20 20 20 20 20 20 20 20 0a 20  NOT",         . 
313d6 20 22 45 58 49 53 54 53 22 2c 20 20 20 20 20 20   "EXISTS",      
313d7 20 20 22 54 45 4d 50 22 2c 20 20 20 20 20 20 20    "TEMP",       
313d8 20 20 20 22 4c 50 22 2c 20 20 20 20 20 20 20 20     "LP",        
313d9 20 20 20 20 22 52 50 22 2c 20 20 20 20 20 20 20      "RP",       
313da 20 20 20 0a 20 20 22 41 53 22 2c 20 20 20 20 20     .  "AS",     
313db 20 20 20 20 20 20 20 22 43 4f 4d 4d 41 22 2c 20         "COMMA", 
313dc 20 20 20 20 20 20 20 20 22 49 44 22 2c 20 20 20          "ID",   
313dd 20 20 20 20 20 20 20 20 20 22 49 4e 44 45 58 45           "INDEXE
313de 44 22 2c 20 20 20 20 20 0a 20 20 22 41 42 4f 52  D",     .  "ABOR
313df 54 22 2c 20 20 20 20 20 20 20 20 20 22 41 43 54  T",         "ACT
313e0 49 4f 4e 22 2c 20 20 20 20 20 20 20 20 22 41 46  ION",        "AF
313e1 54 45 52 22 2c 20 20 20 20 20 20 20 20 20 22 41  TER",         "A
313e2 4e 41 4c 59 5a 45 22 2c 20 20 20 20 20 0a 20 20  NALYZE",     .  
313e3 22 41 53 43 22 2c 20 20 20 20 20 20 20 20 20 20  "ASC",          
313e4 20 22 41 54 54 41 43 48 22 2c 20 20 20 20 20 20   "ATTACH",      
313e5 20 20 22 42 45 46 4f 52 45 22 2c 20 20 20 20 20    "BEFORE",     
313e6 20 20 20 22 42 59 22 2c 20 20 20 20 20 20 20 20     "BY",        
313e7 20 20 0a 20 20 22 43 41 53 43 41 44 45 22 2c 20    .  "CASCADE", 
313e8 20 20 20 20 20 20 22 43 41 53 54 22 2c 20 20 20        "CAST",   
313e9 20 20 20 20 20 20 20 22 43 4f 4c 55 4d 4e 4b 57         "COLUMNKW
313ea 22 2c 20 20 20 20 20 20 22 43 4f 4e 46 4c 49 43  ",      "CONFLIC
313eb 54 22 2c 20 20 20 20 0a 20 20 22 44 41 54 41 42  T",    .  "DATAB
313ec 41 53 45 22 2c 20 20 20 20 20 20 22 44 45 53 43  ASE",      "DESC
313ed 22 2c 20 20 20 20 20 20 20 20 20 20 22 44 45 54  ",          "DET
313ee 41 43 48 22 2c 20 20 20 20 20 20 20 20 22 45 41  ACH",        "EA
313ef 43 48 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22  CH",        .  "
313f0 46 41 49 4c 22 2c 20 20 20 20 20 20 20 20 20 20  FAIL",          
313f1 22 46 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20  "FOR",          
313f2 20 22 49 47 4e 4f 52 45 22 2c 20 20 20 20 20 20   "IGNORE",      
313f3 20 20 22 49 4e 49 54 49 41 4c 4c 59 22 2c 20 20    "INITIALLY",  
313f4 20 0a 20 20 22 49 4e 53 54 45 41 44 22 2c 20 20   .  "INSTEAD",  
313f5 20 20 20 20 20 22 4c 49 4b 45 5f 4b 57 22 2c 20       "LIKE_KW", 
313f6 20 20 20 20 20 20 22 4d 41 54 43 48 22 2c 20 20        "MATCH",  
313f7 20 20 20 20 20 20 20 22 4e 4f 22 2c 20 20 20 20         "NO",    
313f8 20 20 20 20 20 20 0a 20 20 22 4b 45 59 22 2c 20        .  "KEY", 
313f9 20 20 20 20 20 20 20 20 20 20 22 4f 46 22 2c 20            "OF", 
313fa 20 20 20 20 20 20 20 20 20 20 20 22 4f 46 46 53             "OFFS
313fb 45 54 22 2c 20 20 20 20 20 20 20 20 22 50 52 41  ET",        "PRA
313fc 47 4d 41 22 2c 20 20 20 20 20 20 0a 20 20 22 52  GMA",      .  "R
313fd 41 49 53 45 22 2c 20 20 20 20 20 20 20 20 20 22  AISE",         "
313fe 52 45 50 4c 41 43 45 22 2c 20 20 20 20 20 20 20  REPLACE",       
313ff 22 52 45 53 54 52 49 43 54 22 2c 20 20 20 20 20  "RESTRICT",     
31400 20 22 52 4f 57 22 2c 20 20 20 20 20 20 20 20 20   "ROW",         
31401 0a 20 20 22 54 52 49 47 47 45 52 22 2c 20 20 20  .  "TRIGGER",   
31402 20 20 20 20 22 56 41 43 55 55 4d 22 2c 20 20 20      "VACUUM",   
31403 20 20 20 20 20 22 56 49 45 57 22 2c 20 20 20 20       "VIEW",    
31404 20 20 20 20 20 20 22 56 49 52 54 55 41 4c 22 2c        "VIRTUAL",
31405 20 20 20 20 20 0a 20 20 22 52 45 49 4e 44 45 58       .  "REINDEX
31406 22 2c 20 20 20 20 20 20 20 22 52 45 4e 41 4d 45  ",       "RENAME
31407 22 2c 20 20 20 20 20 20 20 20 22 43 54 49 4d 45  ",        "CTIME
31408 5f 4b 57 22 2c 20 20 20 20 20 20 22 41 4e 59 22  _KW",      "ANY"
31409 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22 4f 52  ,         .  "OR
3140a 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 41  ",            "A
3140b 4e 44 22 2c 20 20 20 20 20 20 20 20 20 20 20 22  ND",           "
3140c 49 53 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  IS",            
3140d 22 42 45 54 57 45 45 4e 22 2c 20 20 20 20 20 0a  "BETWEEN",     .
3140e 20 20 22 49 4e 22 2c 20 20 20 20 20 20 20 20 20    "IN",         
3140f 20 20 20 22 49 53 4e 55 4c 4c 22 2c 20 20 20 20     "ISNULL",    
31410 20 20 20 20 22 4e 4f 54 4e 55 4c 4c 22 2c 20 20      "NOTNULL",  
31411 20 20 20 20 20 22 4e 45 22 2c 20 20 20 20 20 20       "NE",      
31412 20 20 20 20 0a 20 20 22 45 51 22 2c 20 20 20 20      .  "EQ",    
31413 20 20 20 20 20 20 20 20 22 47 54 22 2c 20 20 20          "GT",   
31414 20 20 20 20 20 20 20 20 20 22 4c 45 22 2c 20 20           "LE",  
31415 20 20 20 20 20 20 20 20 20 20 22 4c 54 22 2c 20            "LT", 
31416 20 20 20 20 20 20 20 20 20 0a 20 20 22 47 45 22           .  "GE"
31417 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 45 53  ,            "ES
31418 43 41 50 45 22 2c 20 20 20 20 20 20 20 20 22 42  CAPE",        "B
31419 49 54 41 4e 44 22 2c 20 20 20 20 20 20 20 20 22  ITAND",        "
3141a 42 49 54 4f 52 22 2c 20 20 20 20 20 20 20 0a 20  BITOR",       . 
3141b 20 22 4c 53 48 49 46 54 22 2c 20 20 20 20 20 20   "LSHIFT",      
3141c 20 20 22 52 53 48 49 46 54 22 2c 20 20 20 20 20    "RSHIFT",     
3141d 20 20 20 22 50 4c 55 53 22 2c 20 20 20 20 20 20     "PLUS",      
3141e 20 20 20 20 22 4d 49 4e 55 53 22 2c 20 20 20 20      "MINUS",    
3141f 20 20 20 0a 20 20 22 53 54 41 52 22 2c 20 20 20     .  "STAR",   
31420 20 20 20 20 20 20 20 22 53 4c 41 53 48 22 2c 20         "SLASH", 
31421 20 20 20 20 20 20 20 20 22 52 45 4d 22 2c 20 20          "REM",  
31422 20 20 20 20 20 20 20 20 20 22 43 4f 4e 43 41 54           "CONCAT
31423 22 2c 20 20 20 20 20 20 0a 20 20 22 43 4f 4c 4c  ",      .  "COLL
31424 41 54 45 22 2c 20 20 20 20 20 20 20 22 42 49 54  ATE",       "BIT
31425 4e 4f 54 22 2c 20 20 20 20 20 20 20 20 22 53 54  NOT",        "ST
31426 52 49 4e 47 22 2c 20 20 20 20 20 20 20 20 22 4a  RING",        "J
31427 4f 49 4e 5f 4b 57 22 2c 20 20 20 20 20 0a 20 20  OIN_KW",     .  
31428 22 43 4f 4e 53 54 52 41 49 4e 54 22 2c 20 20 20  "CONSTRAINT",   
31429 20 22 44 45 46 41 55 4c 54 22 2c 20 20 20 20 20   "DEFAULT",     
3142a 20 20 22 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20    "NULL",       
3142b 20 20 20 22 50 52 49 4d 41 52 59 22 2c 20 20 20     "PRIMARY",   
3142c 20 20 0a 20 20 22 55 4e 49 51 55 45 22 2c 20 20    .  "UNIQUE",  
3142d 20 20 20 20 20 20 22 43 48 45 43 4b 22 2c 20 20        "CHECK",  
3142e 20 20 20 20 20 20 20 22 52 45 46 45 52 45 4e 43         "REFERENC
3142f 45 53 22 2c 20 20 20 20 22 41 55 54 4f 49 4e 43  ES",    "AUTOINC
31430 52 22 2c 20 20 20 20 0a 20 20 22 4f 4e 22 2c 20  R",    .  "ON", 
31431 20 20 20 20 20 20 20 20 20 20 20 22 44 45 4c 45             "DELE
31432 54 45 22 2c 20 20 20 20 20 20 20 20 22 55 50 44  TE",        "UPD
31433 41 54 45 22 2c 20 20 20 20 20 20 20 20 22 53 45  ATE",        "SE
31434 54 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22  T",         .  "
31435 44 45 46 45 52 52 41 42 4c 45 22 2c 20 20 20 20  DEFERRABLE",    
31436 22 46 4f 52 45 49 47 4e 22 2c 20 20 20 20 20 20  "FOREIGN",      
31437 20 22 44 52 4f 50 22 2c 20 20 20 20 20 20 20 20   "DROP",        
31438 20 20 22 55 4e 49 4f 4e 22 2c 20 20 20 20 20 20    "UNION",      
31439 20 0a 20 20 22 41 4c 4c 22 2c 20 20 20 20 20 20   .  "ALL",      
3143a 20 20 20 20 20 22 45 58 43 45 50 54 22 2c 20 20       "EXCEPT",  
3143b 20 20 20 20 20 20 22 49 4e 54 45 52 53 45 43 54        "INTERSECT
3143c 22 2c 20 20 20 20 20 22 53 45 4c 45 43 54 22 2c  ",     "SELECT",
3143d 20 20 20 20 20 20 0a 20 20 22 44 49 53 54 49 4e        .  "DISTIN
3143e 43 54 22 2c 20 20 20 20 20 20 22 44 4f 54 22 2c  CT",      "DOT",
3143f 20 20 20 20 20 20 20 20 20 20 20 22 46 52 4f 4d             "FROM
31440 22 2c 20 20 20 20 20 20 20 20 20 20 22 4a 4f 49  ",          "JOI
31441 4e 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 55  N",        .  "U
31442 53 49 4e 47 22 2c 20 20 20 20 20 20 20 20 20 22  SING",         "
31443 4f 52 44 45 52 22 2c 20 20 20 20 20 20 20 20 20  ORDER",         
31444 22 47 52 4f 55 50 22 2c 20 20 20 20 20 20 20 20  "GROUP",        
31445 20 22 48 41 56 49 4e 47 22 2c 20 20 20 20 20 20   "HAVING",      
31446 0a 20 20 22 4c 49 4d 49 54 22 2c 20 20 20 20 20  .  "LIMIT",     
31447 20 20 20 20 22 57 48 45 52 45 22 2c 20 20 20 20      "WHERE",    
31448 20 20 20 20 20 22 49 4e 54 4f 22 2c 20 20 20 20       "INTO",    
31449 20 20 20 20 20 20 22 56 41 4c 55 45 53 22 2c 20        "VALUES", 
3144a 20 20 20 20 20 0a 20 20 22 49 4e 53 45 52 54 22       .  "INSERT"
3144b 2c 20 20 20 20 20 20 20 20 22 49 4e 54 45 47 45  ,        "INTEGE
3144c 52 22 2c 20 20 20 20 20 20 20 22 46 4c 4f 41 54  R",       "FLOAT
3144d 22 2c 20 20 20 20 20 20 20 20 20 22 42 4c 4f 42  ",         "BLOB
3144e 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 52 45  ",        .  "RE
3144f 47 49 53 54 45 52 22 2c 20 20 20 20 20 20 22 56  GISTER",      "V
31450 41 52 49 41 42 4c 45 22 2c 20 20 20 20 20 20 22  ARIABLE",      "
31451 43 41 53 45 22 2c 20 20 20 20 20 20 20 20 20 20  CASE",          
31452 22 57 48 45 4e 22 2c 20 20 20 20 20 20 20 20 0a  "WHEN",        .
31453 20 20 22 54 48 45 4e 22 2c 20 20 20 20 20 20 20    "THEN",       
31454 20 20 20 22 45 4c 53 45 22 2c 20 20 20 20 20 20     "ELSE",      
31455 20 20 20 20 22 49 4e 44 45 58 22 2c 20 20 20 20      "INDEX",    
31456 20 20 20 20 20 22 41 4c 54 45 52 22 2c 20 20 20       "ALTER",   
31457 20 20 20 20 0a 20 20 22 41 44 44 22 2c 20 20 20      .  "ADD",   
31458 20 20 20 20 20 20 20 20 22 65 72 72 6f 72 22 2c          "error",
31459 20 20 20 20 20 20 20 20 20 22 69 6e 70 75 74 22           "input"
3145a 2c 20 20 20 20 20 20 20 20 20 22 63 6d 64 6c 69  ,         "cmdli
3145b 73 74 22 2c 20 20 20 20 20 0a 20 20 22 65 63 6d  st",     .  "ecm
3145c 64 22 2c 20 20 20 20 20 20 20 20 20 20 22 65 78  d",          "ex
3145d 70 6c 61 69 6e 22 2c 20 20 20 20 20 20 20 22 63  plain",       "c
3145e 6d 64 78 22 2c 20 20 20 20 20 20 20 20 20 20 22  mdx",          "
3145f 63 6d 64 22 2c 20 20 20 20 20 20 20 20 20 0a 20  cmd",         . 
31460 20 22 74 72 61 6e 73 74 79 70 65 22 2c 20 20 20   "transtype",   
31461 20 20 22 74 72 61 6e 73 5f 6f 70 74 22 2c 20 20    "trans_opt",  
31462 20 20 20 22 6e 6d 22 2c 20 20 20 20 20 20 20 20     "nm",        
31463 20 20 20 20 22 73 61 76 65 70 6f 69 6e 74 5f 6f      "savepoint_o
31464 70 74 22 2c 0a 20 20 22 63 72 65 61 74 65 5f 74  pt",.  "create_t
31465 61 62 6c 65 22 2c 20 20 22 63 72 65 61 74 65 5f  able",  "create_
31466 74 61 62 6c 65 5f 61 72 67 73 22 2c 20 20 22 63  table_args",  "c
31467 72 65 61 74 65 6b 77 22 2c 20 20 20 20 20 20 22  reatekw",      "
31468 74 65 6d 70 22 2c 20 20 20 20 20 20 20 20 0a 20  temp",        . 
31469 20 22 69 66 6e 6f 74 65 78 69 73 74 73 22 2c 20   "ifnotexists", 
3146a 20 20 22 64 62 6e 6d 22 2c 20 20 20 20 20 20 20    "dbnm",       
3146b 20 20 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 22 2c     "columnlist",
3146c 20 20 20 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70      "conslist_op
3146d 74 22 2c 0a 20 20 22 73 65 6c 65 63 74 22 2c 20  t",.  "select", 
3146e 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 22 2c         "column",
3146f 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 69          "columni
31470 64 22 2c 20 20 20 20 20 20 22 74 79 70 65 22 2c  d",      "type",
31471 20 20 20 20 20 20 20 20 0a 20 20 22 63 61 72 67          .  "carg
31472 6c 69 73 74 22 2c 20 20 20 20 20 20 22 69 64 22  list",      "id"
31473 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 69 64  ,            "id
31474 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 74  s",           "t
31475 79 70 65 74 6f 6b 65 6e 22 2c 20 20 20 0a 20 20  ypetoken",   .  
31476 22 74 79 70 65 6e 61 6d 65 22 2c 20 20 20 20 20  "typename",     
31477 20 22 73 69 67 6e 65 64 22 2c 20 20 20 20 20 20   "signed",      
31478 20 20 22 70 6c 75 73 5f 6e 75 6d 22 2c 20 20 20    "plus_num",   
31479 20 20 20 22 6d 69 6e 75 73 5f 6e 75 6d 22 2c 20     "minus_num", 
3147a 20 20 0a 20 20 22 63 61 72 67 22 2c 20 20 20 20    .  "carg",    
3147b 20 20 20 20 20 20 22 63 63 6f 6e 73 22 2c 20 20        "ccons",  
3147c 20 20 20 20 20 20 20 22 74 65 72 6d 22 2c 20 20         "term",  
3147d 20 20 20 20 20 20 20 20 22 65 78 70 72 22 2c 20          "expr", 
3147e 20 20 20 20 20 20 20 0a 20 20 22 6f 6e 63 6f 6e         .  "oncon
3147f 66 22 2c 20 20 20 20 20 20 20 20 22 73 6f 72 74  f",        "sort
31480 6f 72 64 65 72 22 2c 20 20 20 20 20 22 61 75 74  order",     "aut
31481 6f 69 6e 63 22 2c 20 20 20 20 20 20 20 22 69 64  oinc",       "id
31482 78 6c 69 73 74 5f 6f 70 74 22 2c 20 0a 20 20 22  xlist_opt", .  "
31483 72 65 66 61 72 67 73 22 2c 20 20 20 20 20 20 20  refargs",       
31484 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65  "defer_subclause
31485 22 2c 20 20 22 72 65 66 61 72 67 22 2c 20 20 20  ",  "refarg",   
31486 20 20 20 20 20 22 72 65 66 61 63 74 22 2c 20 20       "refact",  
31487 20 20 20 20 0a 20 20 22 69 6e 69 74 5f 64 65 66      .  "init_def
31488 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c  erred_pred_opt",
31489 20 20 22 63 6f 6e 73 6c 69 73 74 22 2c 20 20 20    "conslist",   
3148a 20 20 20 22 74 63 6f 6e 73 22 2c 20 20 20 20 20     "tcons",     
3148b 20 20 20 20 22 69 64 78 6c 69 73 74 22 2c 20 20      "idxlist",  
3148c 20 20 20 0a 20 20 22 64 65 66 65 72 5f 73 75 62     .  "defer_sub
3148d 63 6c 61 75 73 65 5f 6f 70 74 22 2c 20 20 22 6f  clause_opt",  "o
3148e 72 63 6f 6e 66 22 2c 20 20 20 20 20 20 20 20 22  rconf",        "
3148f 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 20 20 20  resolvetype",   
31490 22 72 61 69 73 65 74 79 70 65 22 2c 20 20 20 0a  "raisetype",   .
31491 20 20 22 69 66 65 78 69 73 74 73 22 2c 20 20 20    "ifexists",   
31492 20 20 20 22 66 75 6c 6c 6e 61 6d 65 22 2c 20 20     "fullname",  
31493 20 20 20 20 22 6f 6e 65 73 65 6c 65 63 74 22 2c      "oneselect",
31494 20 20 20 20 20 22 6d 75 6c 74 69 73 65 6c 65 63       "multiselec
31495 74 5f 6f 70 22 2c 0a 20 20 22 64 69 73 74 69 6e  t_op",.  "distin
31496 63 74 22 2c 20 20 20 20 20 20 22 73 65 6c 63 6f  ct",      "selco
31497 6c 6c 69 73 74 22 2c 20 20 20 20 22 66 72 6f 6d  llist",    "from
31498 22 2c 20 20 20 20 20 20 20 20 20 20 22 77 68 65  ",          "whe
31499 72 65 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 67  re_opt",   .  "g
3149a 72 6f 75 70 62 79 5f 6f 70 74 22 2c 20 20 20 22  roupby_opt",   "
3149b 68 61 76 69 6e 67 5f 6f 70 74 22 2c 20 20 20 20  having_opt",    
3149c 22 6f 72 64 65 72 62 79 5f 6f 70 74 22 2c 20 20  "orderby_opt",  
3149d 20 22 6c 69 6d 69 74 5f 6f 70 74 22 2c 20 20 20   "limit_opt",   
3149e 0a 20 20 22 73 63 6c 70 22 2c 20 20 20 20 20 20  .  "sclp",      
3149f 20 20 20 20 22 61 73 22 2c 20 20 20 20 20 20 20      "as",       
314a0 20 20 20 20 20 22 73 65 6c 74 61 62 6c 69 73 74       "seltablist
314a1 22 2c 20 20 20 20 22 73 74 6c 5f 70 72 65 66 69  ",    "stl_prefi
314a2 78 22 2c 20 20 0a 20 20 22 6a 6f 69 6e 6f 70 22  x",  .  "joinop"
314a3 2c 20 20 20 20 20 20 20 20 22 69 6e 64 65 78 65  ,        "indexe
314a4 64 5f 6f 70 74 22 2c 20 20 20 22 6f 6e 5f 6f 70  d_opt",   "on_op
314a5 74 22 2c 20 20 20 20 20 20 20 20 22 75 73 69 6e  t",        "usin
314a6 67 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 6a 6f  g_opt",   .  "jo
314a7 69 6e 6f 70 32 22 2c 20 20 20 20 20 20 20 22 69  inop2",       "i
314a8 6e 73 63 6f 6c 6c 69 73 74 22 2c 20 20 20 20 22  nscollist",    "
314a9 73 6f 72 74 6c 69 73 74 22 2c 20 20 20 20 20 20  sortlist",      
314aa 22 73 6f 72 74 69 74 65 6d 22 2c 20 20 20 20 0a  "sortitem",    .
314ab 20 20 22 6e 65 78 70 72 6c 69 73 74 22 2c 20 20    "nexprlist",  
314ac 20 20 20 22 73 65 74 6c 69 73 74 22 2c 20 20 20     "setlist",   
314ad 20 20 20 20 22 69 6e 73 65 72 74 5f 63 6d 64 22      "insert_cmd"
314ae 2c 20 20 20 20 22 69 6e 73 63 6f 6c 6c 69 73 74  ,    "inscollist
314af 5f 6f 70 74 22 2c 0a 20 20 22 69 74 65 6d 6c 69  _opt",.  "itemli
314b0 73 74 22 2c 20 20 20 20 20 20 22 65 78 70 72 6c  st",      "exprl
314b1 69 73 74 22 2c 20 20 20 20 20 20 22 6c 69 6b 65  ist",      "like
314b2 6f 70 22 2c 20 20 20 20 20 20 20 20 22 65 73 63  op",        "esc
314b3 61 70 65 22 2c 20 20 20 20 20 20 0a 20 20 22 62  ape",      .  "b
314b4 65 74 77 65 65 6e 5f 6f 70 22 2c 20 20 20 20 22  etween_op",    "
314b5 69 6e 5f 6f 70 22 2c 20 20 20 20 20 20 20 20 20  in_op",         
314b6 22 63 61 73 65 5f 6f 70 65 72 61 6e 64 22 2c 20  "case_operand", 
314b7 20 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74 22   "case_exprlist"
314b8 2c 0a 20 20 22 63 61 73 65 5f 65 6c 73 65 22 2c  ,.  "case_else",
314b9 20 20 20 20 20 22 75 6e 69 71 75 65 66 6c 61 67       "uniqueflag
314ba 22 2c 20 20 20 20 22 63 6f 6c 6c 61 74 65 22 2c  ",    "collate",
314bb 20 20 20 20 20 20 20 22 6e 6d 6e 75 6d 22 2c 20         "nmnum", 
314bc 20 20 20 20 20 20 0a 20 20 22 70 6c 75 73 5f 6f        .  "plus_o
314bd 70 74 22 2c 20 20 20 20 20 20 22 6e 75 6d 62 65  pt",      "numbe
314be 72 22 2c 20 20 20 20 20 20 20 20 22 74 72 69 67  r",        "trig
314bf 67 65 72 5f 64 65 63 6c 22 2c 20 20 22 74 72 69  ger_decl",  "tri
314c0 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 22 2c 0a  gger_cmd_list",.
314c1 20 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 22    "trigger_time"
314c2 2c 20 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e  ,  "trigger_even
314c3 74 22 2c 20 20 22 66 6f 72 65 61 63 68 5f 63 6c  t",  "foreach_cl
314c4 61 75 73 65 22 2c 20 20 22 77 68 65 6e 5f 63 6c  ause",  "when_cl
314c5 61 75 73 65 22 2c 20 0a 20 20 22 74 72 69 67 67  ause", .  "trigg
314c6 65 72 5f 63 6d 64 22 2c 20 20 20 22 74 72 6e 6d  er_cmd",   "trnm
314c7 22 2c 20 20 20 20 20 20 20 20 20 20 22 74 72 69  ",          "tri
314c8 64 78 62 79 22 2c 20 20 20 20 20 20 20 22 64 61  dxby",       "da
314c9 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 22 2c 0a  tabase_kw_opt",.
314ca 20 20 22 6b 65 79 5f 6f 70 74 22 2c 20 20 20 20    "key_opt",    
314cb 20 20 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66     "add_column_f
314cc 75 6c 6c 6e 61 6d 65 22 2c 20 20 22 6b 77 63 6f  ullname",  "kwco
314cd 6c 75 6d 6e 5f 6f 70 74 22 2c 20 20 22 63 72 65  lumn_opt",  "cre
314ce 61 74 65 5f 76 74 61 62 22 2c 20 0a 20 20 22 76  ate_vtab", .  "v
314cf 74 61 62 61 72 67 6c 69 73 74 22 2c 20 20 20 22  tabarglist",   "
314d0 76 74 61 62 61 72 67 22 2c 20 20 20 20 20 20 20  vtabarg",       
314d1 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c 20  "vtabargtoken", 
314d2 20 22 6c 70 22 2c 20 20 20 20 20 20 20 20 20 20   "lp",          
314d3 0a 20 20 22 61 6e 79 6c 69 73 74 22 2c 20 20 20  .  "anylist",   
314d4 20 20 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20    .};.#endif /* 
314d5 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64  NDEBUG */..#ifnd
314d6 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 46 6f 72  ef NDEBUG./* For
314d7 20 74 72 61 63 69 6e 67 20 72 65 64 75 63 65 20   tracing reduce 
314d8 61 63 74 69 6f 6e 73 2c 20 74 68 65 20 6e 61 6d  actions, the nam
314d9 65 73 20 6f 66 20 61 6c 6c 20 72 75 6c 65 73 20  es of all rules 
314da 61 72 65 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f  are required..*/
314db 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
314dc 61 72 20 2a 63 6f 6e 73 74 20 79 79 52 75 6c 65  ar *const yyRule
314dd 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20  Name[] = {. /*  
314de 20 30 20 2a 2f 20 22 69 6e 70 75 74 20 3a 3a 3d   0 */ "input ::=
314df 20 63 6d 64 6c 69 73 74 22 2c 0a 20 2f 2a 20 20   cmdlist",. /*  
314e0 20 31 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20 3a   1 */ "cmdlist :
314e1 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63 6d 64 22  := cmdlist ecmd"
314e2 2c 0a 20 2f 2a 20 20 20 32 20 2a 2f 20 22 63 6d  ,. /*   2 */ "cm
314e3 64 6c 69 73 74 20 3a 3a 3d 20 65 63 6d 64 22 2c  dlist ::= ecmd",
314e4 0a 20 2f 2a 20 20 20 33 20 2a 2f 20 22 65 63 6d  . /*   3 */ "ecm
314e5 64 20 3a 3a 3d 20 53 45 4d 49 22 2c 0a 20 2f 2a  d ::= SEMI",. /*
314e6 20 20 20 34 20 2a 2f 20 22 65 63 6d 64 20 3a 3a     4 */ "ecmd ::
314e7 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64 78 20 53  = explain cmdx S
314e8 45 4d 49 22 2c 0a 20 2f 2a 20 20 20 35 20 2a 2f  EMI",. /*   5 */
314e9 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 22 2c 0a   "explain ::=",.
314ea 20 2f 2a 20 20 20 36 20 2a 2f 20 22 65 78 70 6c   /*   6 */ "expl
314eb 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 22  ain ::= EXPLAIN"
314ec 2c 0a 20 2f 2a 20 20 20 37 20 2a 2f 20 22 65 78  ,. /*   7 */ "ex
314ed 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49  plain ::= EXPLAI
314ee 4e 20 51 55 45 52 59 20 50 4c 41 4e 22 2c 0a 20  N QUERY PLAN",. 
314ef 2f 2a 20 20 20 38 20 2a 2f 20 22 63 6d 64 78 20  /*   8 */ "cmdx 
314f0 3a 3a 3d 20 63 6d 64 22 2c 0a 20 2f 2a 20 20 20  ::= cmd",. /*   
314f1 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 42 45  9 */ "cmd ::= BE
314f2 47 49 4e 20 74 72 61 6e 73 74 79 70 65 20 74 72  GIN transtype tr
314f3 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31  ans_opt",. /*  1
314f4 30 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20  0 */ "trans_opt 
314f5 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 31 20 2a 2f  ::=",. /*  11 */
314f6 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20   "trans_opt ::= 
314f7 54 52 41 4e 53 41 43 54 49 4f 4e 22 2c 0a 20 2f  TRANSACTION",. /
314f8 2a 20 20 31 32 20 2a 2f 20 22 74 72 61 6e 73 5f  *  12 */ "trans_
314f9 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54  opt ::= TRANSACT
314fa 49 4f 4e 20 6e 6d 22 2c 0a 20 2f 2a 20 20 31 33  ION nm",. /*  13
314fb 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a   */ "transtype :
314fc 3a 3d 22 2c 0a 20 2f 2a 20 20 31 34 20 2a 2f 20  :=",. /*  14 */ 
314fd 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44  "transtype ::= D
314fe 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20 31  EFERRED",. /*  1
314ff 35 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20  5 */ "transtype 
31500 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45 22 2c 0a  ::= IMMEDIATE",.
31501 20 2f 2a 20 20 31 36 20 2a 2f 20 22 74 72 61 6e   /*  16 */ "tran
31502 73 74 79 70 65 20 3a 3a 3d 20 45 58 43 4c 55 53  stype ::= EXCLUS
31503 49 56 45 22 2c 0a 20 2f 2a 20 20 31 37 20 2a 2f  IVE",. /*  17 */
31504 20 22 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54   "cmd ::= COMMIT
31505 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a   trans_opt",. /*
31506 20 20 31 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d    18 */ "cmd ::=
31507 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74 22 2c   END trans_opt",
31508 0a 20 2f 2a 20 20 31 39 20 2a 2f 20 22 63 6d 64  . /*  19 */ "cmd
31509 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72   ::= ROLLBACK tr
3150a 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 32  ans_opt",. /*  2
3150b 30 20 2a 2f 20 22 73 61 76 65 70 6f 69 6e 74 5f  0 */ "savepoint_
3150c 6f 70 74 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e  opt ::= SAVEPOIN
3150d 54 22 2c 0a 20 2f 2a 20 20 32 31 20 2a 2f 20 22  T",. /*  21 */ "
3150e 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a  savepoint_opt ::
3150f 3d 22 2c 0a 20 2f 2a 20 20 32 32 20 2a 2f 20 22  =",. /*  22 */ "
31510 63 6d 64 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e  cmd ::= SAVEPOIN
31511 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 33 20 2a  T nm",. /*  23 *
31512 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 4c 45 41  / "cmd ::= RELEA
31513 53 45 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74  SE savepoint_opt
31514 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f   nm",. /*  24 */
31515 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41   "cmd ::= ROLLBA
31516 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20 54 4f 20  CK trans_opt TO 
31517 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d  savepoint_opt nm
31518 22 2c 0a 20 2f 2a 20 20 32 35 20 2a 2f 20 22 63  ",. /*  25 */ "c
31519 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61  md ::= create_ta
3151a 62 6c 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65  ble create_table
3151b 5f 61 72 67 73 22 2c 0a 20 2f 2a 20 20 32 36 20  _args",. /*  26 
3151c 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65  */ "create_table
3151d 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65   ::= createkw te
3151e 6d 70 20 54 41 42 4c 45 20 69 66 6e 6f 74 65 78  mp TABLE ifnotex
3151f 69 73 74 73 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20  ists nm dbnm",. 
31520 2f 2a 20 20 32 37 20 2a 2f 20 22 63 72 65 61 74  /*  27 */ "creat
31521 65 6b 77 20 3a 3a 3d 20 43 52 45 41 54 45 22 2c  ekw ::= CREATE",
31522 0a 20 2f 2a 20 20 32 38 20 2a 2f 20 22 69 66 6e  . /*  28 */ "ifn
31523 6f 74 65 78 69 73 74 73 20 3a 3a 3d 22 2c 0a 20  otexists ::=",. 
31524 2f 2a 20 20 32 39 20 2a 2f 20 22 69 66 6e 6f 74  /*  29 */ "ifnot
31525 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 4e 4f  exists ::= IF NO
31526 54 20 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20 20  T EXISTS",. /*  
31527 33 30 20 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d 20  30 */ "temp ::= 
31528 54 45 4d 50 22 2c 0a 20 2f 2a 20 20 33 31 20 2a  TEMP",. /*  31 *
31529 2f 20 22 74 65 6d 70 20 3a 3a 3d 22 2c 0a 20 2f  / "temp ::=",. /
3152a 2a 20 20 33 32 20 2a 2f 20 22 63 72 65 61 74 65  *  32 */ "create
3152b 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20  _table_args ::= 
3152c 4c 50 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f  LP columnlist co
3152d 6e 73 6c 69 73 74 5f 6f 70 74 20 52 50 22 2c 0a  nslist_opt RP",.
3152e 20 2f 2a 20 20 33 33 20 2a 2f 20 22 63 72 65 61   /*  33 */ "crea
3152f 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a  te_table_args ::
31530 3d 20 41 53 20 73 65 6c 65 63 74 22 2c 0a 20 2f  = AS select",. /
31531 2a 20 20 33 34 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  34 */ "column
31532 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c  list ::= columnl
31533 69 73 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e  ist COMMA column
31534 22 2c 0a 20 2f 2a 20 20 33 35 20 2a 2f 20 22 63  ",. /*  35 */ "c
31535 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f  olumnlist ::= co
31536 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 20 33 36 20 2a  lumn",. /*  36 *
31537 2f 20 22 63 6f 6c 75 6d 6e 20 3a 3a 3d 20 63 6f  / "column ::= co
31538 6c 75 6d 6e 69 64 20 74 79 70 65 20 63 61 72 67  lumnid type carg
31539 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 33 37 20 2a  list",. /*  37 *
3153a 2f 20 22 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20  / "columnid ::= 
3153b 6e 6d 22 2c 0a 20 2f 2a 20 20 33 38 20 2a 2f 20  nm",. /*  38 */ 
3153c 22 69 64 20 3a 3a 3d 20 49 44 22 2c 0a 20 2f 2a  "id ::= ID",. /*
3153d 20 20 33 39 20 2a 2f 20 22 69 64 20 3a 3a 3d 20    39 */ "id ::= 
3153e 49 4e 44 45 58 45 44 22 2c 0a 20 2f 2a 20 20 34  INDEXED",. /*  4
3153f 30 20 2a 2f 20 22 69 64 73 20 3a 3a 3d 20 49 44  0 */ "ids ::= ID
31540 7c 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 34  |STRING",. /*  4
31541 31 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 69 64 22  1 */ "nm ::= id"
31542 2c 0a 20 2f 2a 20 20 34 32 20 2a 2f 20 22 6e 6d  ,. /*  42 */ "nm
31543 20 3a 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f   ::= STRING",. /
31544 2a 20 20 34 33 20 2a 2f 20 22 6e 6d 20 3a 3a 3d  *  43 */ "nm ::=
31545 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 20   JOIN_KW",. /*  
31546 34 34 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 22  44 */ "type ::="
31547 2c 0a 20 2f 2a 20 20 34 35 20 2a 2f 20 22 74 79  ,. /*  45 */ "ty
31548 70 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e  pe ::= typetoken
31549 22 2c 0a 20 2f 2a 20 20 34 36 20 2a 2f 20 22 74  ",. /*  46 */ "t
3154a 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70  ypetoken ::= typ
3154b 65 6e 61 6d 65 22 2c 0a 20 2f 2a 20 20 34 37 20  ename",. /*  47 
3154c 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a  */ "typetoken ::
3154d 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69  = typename LP si
3154e 67 6e 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34  gned RP",. /*  4
3154f 38 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20  8 */ "typetoken 
31550 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20  ::= typename LP 
31551 73 69 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67  signed COMMA sig
31552 6e 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 39  ned RP",. /*  49
31553 20 2a 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a   */ "typename ::
31554 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 20 35 30 20  = ids",. /*  50 
31555 2a 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d  */ "typename ::=
31556 20 74 79 70 65 6e 61 6d 65 20 69 64 73 22 2c 0a   typename ids",.
31557 20 2f 2a 20 20 35 31 20 2a 2f 20 22 73 69 67 6e   /*  51 */ "sign
31558 65 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22  ed ::= plus_num"
31559 2c 0a 20 2f 2a 20 20 35 32 20 2a 2f 20 22 73 69  ,. /*  52 */ "si
3155a 67 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e  gned ::= minus_n
3155b 75 6d 22 2c 0a 20 2f 2a 20 20 35 33 20 2a 2f 20  um",. /*  53 */ 
3155c 22 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61  "carglist ::= ca
3155d 72 67 6c 69 73 74 20 63 61 72 67 22 2c 0a 20 2f  rglist carg",. /
3155e 2a 20 20 35 34 20 2a 2f 20 22 63 61 72 67 6c 69  *  54 */ "cargli
3155f 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 35 35  st ::=",. /*  55
31560 20 2a 2f 20 22 63 61 72 67 20 3a 3a 3d 20 43 4f   */ "carg ::= CO
31561 4e 53 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e  NSTRAINT nm ccon
31562 73 22 2c 0a 20 2f 2a 20 20 35 36 20 2a 2f 20 22  s",. /*  56 */ "
31563 63 61 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 22 2c  carg ::= ccons",
31564 0a 20 2f 2a 20 20 35 37 20 2a 2f 20 22 63 63 6f  . /*  57 */ "cco
31565 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74  ns ::= DEFAULT t
31566 65 72 6d 22 2c 0a 20 2f 2a 20 20 35 38 20 2a 2f  erm",. /*  58 */
31567 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41   "ccons ::= DEFA
31568 55 4c 54 20 4c 50 20 65 78 70 72 20 52 50 22 2c  ULT LP expr RP",
31569 0a 20 2f 2a 20 20 35 39 20 2a 2f 20 22 63 63 6f  . /*  59 */ "cco
3156a 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50  ns ::= DEFAULT P
3156b 4c 55 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 20  LUS term",. /*  
3156c 36 30 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  60 */ "ccons ::=
3156d 20 44 45 46 41 55 4c 54 20 4d 49 4e 55 53 20 74   DEFAULT MINUS t
3156e 65 72 6d 22 2c 0a 20 2f 2a 20 20 36 31 20 2a 2f  erm",. /*  61 */
3156f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41   "ccons ::= DEFA
31570 55 4c 54 20 69 64 22 2c 0a 20 2f 2a 20 20 36 32  ULT id",. /*  62
31571 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e   */ "ccons ::= N
31572 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a  ULL onconf",. /*
31573 20 20 36 33 20 2a 2f 20 22 63 63 6f 6e 73 20 3a    63 */ "ccons :
31574 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f  := NOT NULL onco
31575 6e 66 22 2c 0a 20 2f 2a 20 20 36 34 20 2a 2f 20  nf",. /*  64 */ 
31576 22 63 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41  "ccons ::= PRIMA
31577 52 59 20 4b 45 59 20 73 6f 72 74 6f 72 64 65 72  RY KEY sortorder
31578 20 6f 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63 22   onconf autoinc"
31579 2c 0a 20 2f 2a 20 20 36 35 20 2a 2f 20 22 63 63  ,. /*  65 */ "cc
3157a 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f  ons ::= UNIQUE o
3157b 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 36 20  nconf",. /*  66 
3157c 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43 48  */ "ccons ::= CH
3157d 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50 22 2c  ECK LP expr RP",
3157e 0a 20 2f 2a 20 20 36 37 20 2a 2f 20 22 63 63 6f  . /*  67 */ "cco
3157f 6e 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45  ns ::= REFERENCE
31580 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74  S nm idxlist_opt
31581 20 72 65 66 61 72 67 73 22 2c 0a 20 2f 2a 20 20   refargs",. /*  
31582 36 38 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  68 */ "ccons ::=
31583 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
31584 22 2c 0a 20 2f 2a 20 20 36 39 20 2a 2f 20 22 63  ",. /*  69 */ "c
31585 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45  cons ::= COLLATE
31586 20 69 64 73 22 2c 0a 20 2f 2a 20 20 37 30 20 2a   ids",. /*  70 *
31587 2f 20 22 61 75 74 6f 69 6e 63 20 3a 3a 3d 22 2c  / "autoinc ::=",
31588 0a 20 2f 2a 20 20 37 31 20 2a 2f 20 22 61 75 74  . /*  71 */ "aut
31589 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43  oinc ::= AUTOINC
3158a 52 22 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f 20 22  R",. /*  72 */ "
3158b 72 65 66 61 72 67 73 20 3a 3a 3d 22 2c 0a 20 2f  refargs ::=",. /
3158c 2a 20 20 37 33 20 2a 2f 20 22 72 65 66 61 72 67  *  73 */ "refarg
3158d 73 20 3a 3a 3d 20 72 65 66 61 72 67 73 20 72 65  s ::= refargs re
3158e 66 61 72 67 22 2c 0a 20 2f 2a 20 20 37 34 20 2a  farg",. /*  74 *
3158f 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41  / "refarg ::= MA
31590 54 43 48 20 6e 6d 22 2c 0a 20 2f 2a 20 20 37 35  TCH nm",. /*  75
31591 20 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20   */ "refarg ::= 
31592 4f 4e 20 44 45 4c 45 54 45 20 72 65 66 61 63 74  ON DELETE refact
31593 22 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f 20 22 72  ",. /*  76 */ "r
31594 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44  efarg ::= ON UPD
31595 41 54 45 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a  ATE refact",. /*
31596 20 20 37 37 20 2a 2f 20 22 72 65 66 61 63 74 20    77 */ "refact 
31597 3a 3a 3d 20 53 45 54 20 4e 55 4c 4c 22 2c 0a 20  ::= SET NULL",. 
31598 2f 2a 20 20 37 38 20 2a 2f 20 22 72 65 66 61 63  /*  78 */ "refac
31599 74 20 3a 3a 3d 20 53 45 54 20 44 45 46 41 55 4c  t ::= SET DEFAUL
3159a 54 22 2c 0a 20 2f 2a 20 20 37 39 20 2a 2f 20 22  T",. /*  79 */ "
3159b 72 65 66 61 63 74 20 3a 3a 3d 20 43 41 53 43 41  refact ::= CASCA
3159c 44 45 22 2c 0a 20 2f 2a 20 20 38 30 20 2a 2f 20  DE",. /*  80 */ 
3159d 22 72 65 66 61 63 74 20 3a 3a 3d 20 52 45 53 54  "refact ::= REST
3159e 52 49 43 54 22 2c 0a 20 2f 2a 20 20 38 31 20 2a  RICT",. /*  81 *
3159f 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 4e 4f  / "refact ::= NO
315a0 20 41 43 54 49 4f 4e 22 2c 0a 20 2f 2a 20 20 38   ACTION",. /*  8
315a1 32 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63  2 */ "defer_subc
315a2 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44 45  lause ::= NOT DE
315a3 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65  FERRABLE init_de
315a4 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22  ferred_pred_opt"
315a5 2c 0a 20 2f 2a 20 20 38 33 20 2a 2f 20 22 64 65  ,. /*  83 */ "de
315a6 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a  fer_subclause ::
315a7 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  = DEFERRABLE ini
315a8 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
315a9 6f 70 74 22 2c 0a 20 2f 2a 20 20 38 34 20 2a 2f  opt",. /*  84 */
315aa 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f   "init_deferred_
315ab 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  pred_opt ::=",. 
315ac 2f 2a 20 20 38 35 20 2a 2f 20 22 69 6e 69 74 5f  /*  85 */ "init_
315ad 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
315ae 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20  t ::= INITIALLY 
315af 44 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20  DEFERRED",. /*  
315b0 38 36 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65  86 */ "init_defe
315b1 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a  rred_pred_opt ::
315b2 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45  = INITIALLY IMME
315b3 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20 38 37 20  DIATE",. /*  87 
315b4 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74  */ "conslist_opt
315b5 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 38 38 20 2a   ::=",. /*  88 *
315b6 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20  / "conslist_opt 
315b7 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69  ::= COMMA consli
315b8 73 74 22 2c 0a 20 2f 2a 20 20 38 39 20 2a 2f 20  st",. /*  89 */ 
315b9 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f  "conslist ::= co
315ba 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20 74 63 6f  nslist COMMA tco
315bb 6e 73 22 2c 0a 20 2f 2a 20 20 39 30 20 2a 2f 20  ns",. /*  90 */ 
315bc 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f  "conslist ::= co
315bd 6e 73 6c 69 73 74 20 74 63 6f 6e 73 22 2c 0a 20  nslist tcons",. 
315be 2f 2a 20 20 39 31 20 2a 2f 20 22 63 6f 6e 73 6c  /*  91 */ "consl
315bf 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73 22 2c 0a  ist ::= tcons",.
315c0 20 2f 2a 20 20 39 32 20 2a 2f 20 22 74 63 6f 6e   /*  92 */ "tcon
315c1 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54  s ::= CONSTRAINT
315c2 20 6e 6d 22 2c 0a 20 2f 2a 20 20 39 33 20 2a 2f   nm",. /*  93 */
315c3 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d   "tcons ::= PRIM
315c4 41 52 59 20 4b 45 59 20 4c 50 20 69 64 78 6c 69  ARY KEY LP idxli
315c5 73 74 20 61 75 74 6f 69 6e 63 20 52 50 20 6f 6e  st autoinc RP on
315c6 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 34 20 2a  conf",. /*  94 *
315c7 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49  / "tcons ::= UNI
315c8 51 55 45 20 4c 50 20 69 64 78 6c 69 73 74 20 52  QUE LP idxlist R
315c9 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  P onconf",. /*  
315ca 39 35 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d  95 */ "tcons ::=
315cb 20 43 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52   CHECK LP expr R
315cc 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  P onconf",. /*  
315cd 39 36 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d  96 */ "tcons ::=
315ce 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50 20   FOREIGN KEY LP 
315cf 69 64 78 6c 69 73 74 20 52 50 20 52 45 46 45 52  idxlist RP REFER
315d0 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74  ENCES nm idxlist
315d1 5f 6f 70 74 20 72 65 66 61 72 67 73 20 64 65 66  _opt refargs def
315d2 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74  er_subclause_opt
315d3 22 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f 20 22 64  ",. /*  97 */ "d
315d4 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f  efer_subclause_o
315d5 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39 38  pt ::=",. /*  98
315d6 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c   */ "defer_subcl
315d7 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65 66  ause_opt ::= def
315d8 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a 20  er_subclause",. 
315d9 2f 2a 20 20 39 39 20 2a 2f 20 22 6f 6e 63 6f 6e  /*  99 */ "oncon
315da 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30 30 20  f ::=",. /* 100 
315db 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 4f  */ "onconf ::= O
315dc 4e 20 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f 6c  N CONFLICT resol
315dd 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31 30 31  vetype",. /* 101
315de 20 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d 22   */ "orconf ::="
315df 2c 0a 20 2f 2a 20 31 30 32 20 2a 2f 20 22 6f 72  ,. /* 102 */ "or
315e0 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72 65 73 6f  conf ::= OR reso
315e1 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31 30  lvetype",. /* 10
315e2 33 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70  3 */ "resolvetyp
315e3 65 20 3a 3a 3d 20 72 61 69 73 65 74 79 70 65 22  e ::= raisetype"
315e4 2c 0a 20 2f 2a 20 31 30 34 20 2a 2f 20 22 72 65  ,. /* 104 */ "re
315e5 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 49 47  solvetype ::= IG
315e6 4e 4f 52 45 22 2c 0a 20 2f 2a 20 31 30 35 20 2a  NORE",. /* 105 *
315e7 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a  / "resolvetype :
315e8 3a 3d 20 52 45 50 4c 41 43 45 22 2c 0a 20 2f 2a  := REPLACE",. /*
315e9 20 31 30 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   106 */ "cmd ::=
315ea 20 44 52 4f 50 20 54 41 42 4c 45 20 69 66 65 78   DROP TABLE ifex
315eb 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a  ists fullname",.
315ec 20 2f 2a 20 31 30 37 20 2a 2f 20 22 69 66 65 78   /* 107 */ "ifex
315ed 69 73 74 73 20 3a 3a 3d 20 49 46 20 45 58 49 53  ists ::= IF EXIS
315ee 54 53 22 2c 0a 20 2f 2a 20 31 30 38 20 2a 2f 20  TS",. /* 108 */ 
315ef 22 69 66 65 78 69 73 74 73 20 3a 3a 3d 22 2c 0a  "ifexists ::=",.
315f0 20 2f 2a 20 31 30 39 20 2a 2f 20 22 63 6d 64 20   /* 109 */ "cmd 
315f1 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d  ::= createkw tem
315f2 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78 69 73  p VIEW ifnotexis
315f3 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65  ts nm dbnm AS se
315f4 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31 30 20 2a  lect",. /* 110 *
315f5 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20  / "cmd ::= DROP 
315f6 56 49 45 57 20 69 66 65 78 69 73 74 73 20 66 75  VIEW ifexists fu
315f7 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31 31 31  llname",. /* 111
315f8 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 73 65 6c   */ "cmd ::= sel
315f9 65 63 74 22 2c 0a 20 2f 2a 20 31 31 32 20 2a 2f  ect",. /* 112 */
315fa 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20 6f 6e 65   "select ::= one
315fb 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31 33  select",. /* 113
315fc 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20   */ "select ::= 
315fd 73 65 6c 65 63 74 20 6d 75 6c 74 69 73 65 6c 65  select multisele
315fe 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63 74 22  ct_op oneselect"
315ff 2c 0a 20 2f 2a 20 31 31 34 20 2a 2f 20 22 6d 75  ,. /* 114 */ "mu
31600 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d  ltiselect_op ::=
31601 20 55 4e 49 4f 4e 22 2c 0a 20 2f 2a 20 31 31 35   UNION",. /* 115
31602 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74   */ "multiselect
31603 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c  _op ::= UNION AL
31604 4c 22 2c 0a 20 2f 2a 20 31 31 36 20 2a 2f 20 22  L",. /* 116 */ "
31605 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a  multiselect_op :
31606 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45 52 53  := EXCEPT|INTERS
31607 45 43 54 22 2c 0a 20 2f 2a 20 31 31 37 20 2a 2f  ECT",. /* 117 */
31608 20 22 6f 6e 65 73 65 6c 65 63 74 20 3a 3a 3d 20   "oneselect ::= 
31609 53 45 4c 45 43 54 20 64 69 73 74 69 6e 63 74 20  SELECT distinct 
3160a 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72 6f 6d 20  selcollist from 
3160b 77 68 65 72 65 5f 6f 70 74 20 67 72 6f 75 70 62  where_opt groupb
3160c 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f 6f 70 74  y_opt having_opt
3160d 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 6c 69 6d   orderby_opt lim
3160e 69 74 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 31 38  it_opt",. /* 118
3160f 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a   */ "distinct ::
31610 3d 20 44 49 53 54 49 4e 43 54 22 2c 0a 20 2f 2a  = DISTINCT",. /*
31611 20 31 31 39 20 2a 2f 20 22 64 69 73 74 69 6e 63   119 */ "distinc
31612 74 20 3a 3a 3d 20 41 4c 4c 22 2c 0a 20 2f 2a 20  t ::= ALL",. /* 
31613 31 32 30 20 2a 2f 20 22 64 69 73 74 69 6e 63 74  120 */ "distinct
31614 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 31 20 2a   ::=",. /* 121 *
31615 2f 20 22 73 63 6c 70 20 3a 3a 3d 20 73 65 6c 63  / "sclp ::= selc
31616 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 22 2c 0a 20  ollist COMMA",. 
31617 2f 2a 20 31 32 32 20 2a 2f 20 22 73 63 6c 70 20  /* 122 */ "sclp 
31618 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 33 20 2a 2f  ::=",. /* 123 */
31619 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d   "selcollist ::=
3161a 20 73 63 6c 70 20 65 78 70 72 20 61 73 22 2c 0a   sclp expr as",.
3161b 20 2f 2a 20 31 32 34 20 2a 2f 20 22 73 65 6c 63   /* 124 */ "selc
3161c 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20  ollist ::= sclp 
3161d 53 54 41 52 22 2c 0a 20 2f 2a 20 31 32 35 20 2a  STAR",. /* 125 *
3161e 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a  / "selcollist ::
3161f 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54  = sclp nm DOT ST
31620 41 52 22 2c 0a 20 2f 2a 20 31 32 36 20 2a 2f 20  AR",. /* 126 */ 
31621 22 61 73 20 3a 3a 3d 20 41 53 20 6e 6d 22 2c 0a  "as ::= AS nm",.
31622 20 2f 2a 20 31 32 37 20 2a 2f 20 22 61 73 20 3a   /* 127 */ "as :
31623 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 31 32 38  := ids",. /* 128
31624 20 2a 2f 20 22 61 73 20 3a 3a 3d 22 2c 0a 20 2f   */ "as ::=",. /
31625 2a 20 31 32 39 20 2a 2f 20 22 66 72 6f 6d 20 3a  * 129 */ "from :
31626 3a 3d 22 2c 0a 20 2f 2a 20 31 33 30 20 2a 2f 20  :=",. /* 130 */ 
31627 22 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73  "from ::= FROM s
31628 65 6c 74 61 62 6c 69 73 74 22 2c 0a 20 2f 2a 20  eltablist",. /* 
31629 31 33 31 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66  131 */ "stl_pref
3162a 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73  ix ::= seltablis
3162b 74 20 6a 6f 69 6e 6f 70 22 2c 0a 20 2f 2a 20 31  t joinop",. /* 1
3162c 33 32 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66 69  32 */ "stl_prefi
3162d 78 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 33 20  x ::=",. /* 133 
3162e 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 20 3a  */ "seltablist :
3162f 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d  := stl_prefix nm
31630 20 64 62 6e 6d 20 61 73 20 69 6e 64 65 78 65 64   dbnm as indexed
31631 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e  _opt on_opt usin
31632 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 33 34 20  g_opt",. /* 134 
31633 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 20 3a  */ "seltablist :
31634 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c 50  := stl_prefix LP
31635 20 73 65 6c 65 63 74 20 52 50 20 61 73 20 6f 6e   select RP as on
31636 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c  _opt using_opt",
31637 0a 20 2f 2a 20 31 33 35 20 2a 2f 20 22 73 65 6c  . /* 135 */ "sel
31638 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f  tablist ::= stl_
31639 70 72 65 66 69 78 20 4c 50 20 73 65 6c 74 61 62  prefix LP seltab
3163a 6c 69 73 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70  list RP as on_op
3163b 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f  t using_opt",. /
3163c 2a 20 31 33 36 20 2a 2f 20 22 64 62 6e 6d 20 3a  * 136 */ "dbnm :
3163d 3a 3d 22 2c 0a 20 2f 2a 20 31 33 37 20 2a 2f 20  :=",. /* 137 */ 
3163e 22 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d  "dbnm ::= DOT nm
3163f 22 2c 0a 20 2f 2a 20 31 33 38 20 2a 2f 20 22 66  ",. /* 138 */ "f
31640 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d 20 64  ullname ::= nm d
31641 62 6e 6d 22 2c 0a 20 2f 2a 20 31 33 39 20 2a 2f  bnm",. /* 139 */
31642 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d   "joinop ::= COM
31643 4d 41 7c 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34  MA|JOIN",. /* 14
31644 30 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d  0 */ "joinop ::=
31645 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49 4e 22 2c 0a   JOIN_KW JOIN",.
31646 20 2f 2a 20 31 34 31 20 2a 2f 20 22 6a 6f 69 6e   /* 141 */ "join
31647 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e  op ::= JOIN_KW n
31648 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 32  m JOIN",. /* 142
31649 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20   */ "joinop ::= 
3164a 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 6e 6d 20 4a 4f  JOIN_KW nm nm JO
3164b 49 4e 22 2c 0a 20 2f 2a 20 31 34 33 20 2a 2f 20  IN",. /* 143 */ 
3164c 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e 20 65  "on_opt ::= ON e
3164d 78 70 72 22 2c 0a 20 2f 2a 20 31 34 34 20 2a 2f  xpr",. /* 144 */
3164e 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20   "on_opt ::=",. 
3164f 2f 2a 20 31 34 35 20 2a 2f 20 22 69 6e 64 65 78  /* 145 */ "index
31650 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  ed_opt ::=",. /*
31651 20 31 34 36 20 2a 2f 20 22 69 6e 64 65 78 65 64   146 */ "indexed
31652 5f 6f 70 74 20 3a 3a 3d 20 49 4e 44 45 58 45 44  _opt ::= INDEXED
31653 20 42 59 20 6e 6d 22 2c 0a 20 2f 2a 20 31 34 37   BY nm",. /* 147
31654 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70 74   */ "indexed_opt
31655 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44   ::= NOT INDEXED
31656 22 2c 0a 20 2f 2a 20 31 34 38 20 2a 2f 20 22 75  ",. /* 148 */ "u
31657 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 55 53 49  sing_opt ::= USI
31658 4e 47 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74  NG LP inscollist
31659 20 52 50 22 2c 0a 20 2f 2a 20 31 34 39 20 2a 2f   RP",. /* 149 */
3165a 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 22   "using_opt ::="
3165b 2c 0a 20 2f 2a 20 31 35 30 20 2a 2f 20 22 6f 72  ,. /* 150 */ "or
3165c 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  derby_opt ::=",.
3165d 20 2f 2a 20 31 35 31 20 2a 2f 20 22 6f 72 64 65   /* 151 */ "orde
3165e 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 4f 52 44 45  rby_opt ::= ORDE
3165f 52 20 42 59 20 73 6f 72 74 6c 69 73 74 22 2c 0a  R BY sortlist",.
31660 20 2f 2a 20 31 35 32 20 2a 2f 20 22 73 6f 72 74   /* 152 */ "sort
31661 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 6c 69 73  list ::= sortlis
31662 74 20 43 4f 4d 4d 41 20 73 6f 72 74 69 74 65 6d  t COMMA sortitem
31663 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a   sortorder",. /*
31664 20 31 35 33 20 2a 2f 20 22 73 6f 72 74 6c 69 73   153 */ "sortlis
31665 74 20 3a 3a 3d 20 73 6f 72 74 69 74 65 6d 20 73  t ::= sortitem s
31666 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 31  ortorder",. /* 1
31667 35 34 20 2a 2f 20 22 73 6f 72 74 69 74 65 6d 20  54 */ "sortitem 
31668 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  ::= expr",. /* 1
31669 35 35 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65 72  55 */ "sortorder
3166a 20 3a 3a 3d 20 41 53 43 22 2c 0a 20 2f 2a 20 31   ::= ASC",. /* 1
3166b 35 36 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65 72  56 */ "sortorder
3166c 20 3a 3a 3d 20 44 45 53 43 22 2c 0a 20 2f 2a 20   ::= DESC",. /* 
3166d 31 35 37 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65  157 */ "sortorde
3166e 72 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 38 20  r ::=",. /* 158 
3166f 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70 74 20  */ "groupby_opt 
31670 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 39 20 2a 2f  ::=",. /* 159 */
31671 20 22 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a   "groupby_opt ::
31672 3d 20 47 52 4f 55 50 20 42 59 20 6e 65 78 70 72  = GROUP BY nexpr
31673 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 36 30 20 2a  list",. /* 160 *
31674 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a  / "having_opt ::
31675 3d 22 2c 0a 20 2f 2a 20 31 36 31 20 2a 2f 20 22  =",. /* 161 */ "
31676 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48  having_opt ::= H
31677 41 56 49 4e 47 20 65 78 70 72 22 2c 0a 20 2f 2a  AVING expr",. /*
31678 20 31 36 32 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f   162 */ "limit_o
31679 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 33  pt ::=",. /* 163
3167a 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a   */ "limit_opt :
3167b 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 22 2c 0a  := LIMIT expr",.
3167c 20 2f 2a 20 31 36 34 20 2a 2f 20 22 6c 69 6d 69   /* 164 */ "limi
3167d 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20  t_opt ::= LIMIT 
3167e 65 78 70 72 20 4f 46 46 53 45 54 20 65 78 70 72  expr OFFSET expr
3167f 22 2c 0a 20 2f 2a 20 31 36 35 20 2a 2f 20 22 6c  ",. /* 165 */ "l
31680 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d  imit_opt ::= LIM
31681 49 54 20 65 78 70 72 20 43 4f 4d 4d 41 20 65 78  IT expr COMMA ex
31682 70 72 22 2c 0a 20 2f 2a 20 31 36 36 20 2a 2f 20  pr",. /* 166 */ 
31683 22 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20  "cmd ::= DELETE 
31684 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20 69 6e  FROM fullname in
31685 64 65 78 65 64 5f 6f 70 74 20 77 68 65 72 65 5f  dexed_opt where_
31686 6f 70 74 22 2c 0a 20 2f 2a 20 31 36 37 20 2a 2f  opt",. /* 167 */
31687 20 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 22   "where_opt ::="
31688 2c 0a 20 2f 2a 20 31 36 38 20 2a 2f 20 22 77 68  ,. /* 168 */ "wh
31689 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 57 48 45 52  ere_opt ::= WHER
3168a 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 39  E expr",. /* 169
3168b 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 55 50 44   */ "cmd ::= UPD
3168c 41 54 45 20 6f 72 63 6f 6e 66 20 66 75 6c 6c 6e  ATE orconf fulln
3168d 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20  ame indexed_opt 
3168e 53 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72  SET setlist wher
3168f 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 37 30 20  e_opt",. /* 170 
31690 2a 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a 3d 20  */ "setlist ::= 
31691 73 65 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d  setlist COMMA nm
31692 20 45 51 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31   EQ expr",. /* 1
31693 37 31 20 2a 2f 20 22 73 65 74 6c 69 73 74 20 3a  71 */ "setlist :
31694 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a  := nm EQ expr",.
31695 20 2f 2a 20 31 37 32 20 2a 2f 20 22 63 6d 64 20   /* 172 */ "cmd 
31696 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49  ::= insert_cmd I
31697 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73  NTO fullname ins
31698 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55  collist_opt VALU
31699 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52  ES LP itemlist R
3169a 50 22 2c 0a 20 2f 2a 20 31 37 33 20 2a 2f 20 22  P",. /* 173 */ "
3169b 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
3169c 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
3169d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
3169e 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 37 34  select",. /* 174
3169f 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73   */ "cmd ::= ins
316a0 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c  ert_cmd INTO ful
316a1 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74  lname inscollist
316a2 5f 6f 70 74 20 44 45 46 41 55 4c 54 20 56 41 4c  _opt DEFAULT VAL
316a3 55 45 53 22 2c 0a 20 2f 2a 20 31 37 35 20 2a 2f  UES",. /* 175 */
316a4 20 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d   "insert_cmd ::=
316a5 20 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66 22 2c   INSERT orconf",
316a6 0a 20 2f 2a 20 31 37 36 20 2a 2f 20 22 69 6e 73  . /* 176 */ "ins
316a7 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c  ert_cmd ::= REPL
316a8 41 43 45 22 2c 0a 20 2f 2a 20 31 37 37 20 2a 2f  ACE",. /* 177 */
316a9 20 22 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69   "itemlist ::= i
316aa 74 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78  temlist COMMA ex
316ab 70 72 22 2c 0a 20 2f 2a 20 31 37 38 20 2a 2f 20  pr",. /* 178 */ 
316ac 22 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65 78  "itemlist ::= ex
316ad 70 72 22 2c 0a 20 2f 2a 20 31 37 39 20 2a 2f 20  pr",. /* 179 */ 
316ae 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20  "inscollist_opt 
316af 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 38 30 20 2a 2f  ::=",. /* 180 */
316b0 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74   "inscollist_opt
316b1 20 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c 69   ::= LP inscolli
316b2 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 38 31 20  st RP",. /* 181 
316b3 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20 3a  */ "inscollist :
316b4 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73 74 20 43 4f  := inscollist CO
316b5 4d 4d 41 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 32  MMA nm",. /* 182
316b6 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20   */ "inscollist 
316b7 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 33  ::= nm",. /* 183
316b8 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 74 65   */ "expr ::= te
316b9 72 6d 22 2c 0a 20 2f 2a 20 31 38 34 20 2a 2f 20  rm",. /* 184 */ 
316ba 22 65 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70  "expr ::= LP exp
316bb 72 20 52 50 22 2c 0a 20 2f 2a 20 31 38 35 20 2a  r RP",. /* 185 *
316bc 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c  / "term ::= NULL
316bd 22 2c 0a 20 2f 2a 20 31 38 36 20 2a 2f 20 22 65  ",. /* 186 */ "e
316be 78 70 72 20 3a 3a 3d 20 69 64 22 2c 0a 20 2f 2a  xpr ::= id",. /*
316bf 20 31 38 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a   187 */ "expr ::
316c0 3d 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20  = JOIN_KW",. /* 
316c1 31 38 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  188 */ "expr ::=
316c2 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a   nm DOT nm",. /*
316c3 20 31 38 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a   189 */ "expr ::
316c4 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f 54 20  = nm DOT nm DOT 
316c5 6e 6d 22 2c 0a 20 2f 2a 20 31 39 30 20 2a 2f 20  nm",. /* 190 */ 
316c6 22 74 65 72 6d 20 3a 3a 3d 20 49 4e 54 45 47 45  "term ::= INTEGE
316c7 52 7c 46 4c 4f 41 54 7c 42 4c 4f 42 22 2c 0a 20  R|FLOAT|BLOB",. 
316c8 2f 2a 20 31 39 31 20 2a 2f 20 22 74 65 72 6d 20  /* 191 */ "term 
316c9 3a 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a  ::= STRING",. /*
316ca 20 31 39 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a   192 */ "expr ::
316cb 3d 20 52 45 47 49 53 54 45 52 22 2c 0a 20 2f 2a  = REGISTER",. /*
316cc 20 31 39 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a   193 */ "expr ::
316cd 3d 20 56 41 52 49 41 42 4c 45 22 2c 0a 20 2f 2a  = VARIABLE",. /*
316ce 20 31 39 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a   194 */ "expr ::
316cf 3d 20 65 78 70 72 20 43 4f 4c 4c 41 54 45 20 69  = expr COLLATE i
316d0 64 73 22 2c 0a 20 2f 2a 20 31 39 35 20 2a 2f 20  ds",. /* 195 */ 
316d1 22 65 78 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c  "expr ::= CAST L
316d2 50 20 65 78 70 72 20 41 53 20 74 79 70 65 74 6f  P expr AS typeto
316d3 6b 65 6e 20 52 50 22 2c 0a 20 2f 2a 20 31 39 36  ken RP",. /* 196
316d4 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44   */ "expr ::= ID
316d5 20 4c 50 20 64 69 73 74 69 6e 63 74 20 65 78 70   LP distinct exp
316d6 72 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31  rlist RP",. /* 1
316d7 39 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  97 */ "expr ::= 
316d8 49 44 20 4c 50 20 53 54 41 52 20 52 50 22 2c 0a  ID LP STAR RP",.
316d9 20 2f 2a 20 31 39 38 20 2a 2f 20 22 74 65 72 6d   /* 198 */ "term
316da 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57 22 2c 0a   ::= CTIME_KW",.
316db 20 2f 2a 20 31 39 39 20 2a 2f 20 22 65 78 70 72   /* 199 */ "expr
316dc 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44 20 65 78   ::= expr AND ex
316dd 70 72 22 2c 0a 20 2f 2a 20 32 30 30 20 2a 2f 20  pr",. /* 200 */ 
316de 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4f  "expr ::= expr O
316df 52 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 31  R expr",. /* 201
316e0 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
316e1 70 72 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65  pr LT|GT|GE|LE e
316e2 78 70 72 22 2c 0a 20 2f 2a 20 32 30 32 20 2a 2f  xpr",. /* 202 */
316e3 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
316e4 45 51 7c 4e 45 20 65 78 70 72 22 2c 0a 20 2f 2a  EQ|NE expr",. /*
316e5 20 32 30 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a   203 */ "expr ::
316e6 3d 20 65 78 70 72 20 42 49 54 41 4e 44 7c 42 49  = expr BITAND|BI
316e7 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53 48 49 46  TOR|LSHIFT|RSHIF
316e8 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 34  T expr",. /* 204
316e9 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
316ea 70 72 20 50 4c 55 53 7c 4d 49 4e 55 53 20 65 78  pr PLUS|MINUS ex
316eb 70 72 22 2c 0a 20 2f 2a 20 32 30 35 20 2a 2f 20  pr",. /* 205 */ 
316ec 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 53  "expr ::= expr S
316ed 54 41 52 7c 53 4c 41 53 48 7c 52 45 4d 20 65 78  TAR|SLASH|REM ex
316ee 70 72 22 2c 0a 20 2f 2a 20 32 30 36 20 2a 2f 20  pr",. /* 206 */ 
316ef 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43  "expr ::= expr C
316f0 4f 4e 43 41 54 20 65 78 70 72 22 2c 0a 20 2f 2a  ONCAT expr",. /*
316f1 20 32 30 37 20 2a 2f 20 22 6c 69 6b 65 6f 70 20   207 */ "likeop 
316f2 3a 3a 3d 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20 2f  ::= LIKE_KW",. /
316f3 2a 20 32 30 38 20 2a 2f 20 22 6c 69 6b 65 6f 70  * 208 */ "likeop
316f4 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57   ::= NOT LIKE_KW
316f5 22 2c 0a 20 2f 2a 20 32 30 39 20 2a 2f 20 22 6c  ",. /* 209 */ "l
316f6 69 6b 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48 22  ikeop ::= MATCH"
316f7 2c 0a 20 2f 2a 20 32 31 30 20 2a 2f 20 22 6c 69  ,. /* 210 */ "li
316f8 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54  keop ::= NOT MAT
316f9 43 48 22 2c 0a 20 2f 2a 20 32 31 31 20 2a 2f 20  CH",. /* 211 */ 
316fa 22 65 73 63 61 70 65 20 3a 3a 3d 20 45 53 43 41  "escape ::= ESCA
316fb 50 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31  PE expr",. /* 21
316fc 32 20 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a 3d  2 */ "escape ::=
316fd 22 2c 0a 20 2f 2a 20 32 31 33 20 2a 2f 20 22 65  ",. /* 213 */ "e
316fe 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b  xpr ::= expr lik
316ff 65 6f 70 20 65 78 70 72 20 65 73 63 61 70 65 22  eop expr escape"
31700 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f 20 22 65 78  ,. /* 214 */ "ex
31701 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 4e 55  pr ::= expr ISNU
31702 4c 4c 7c 4e 4f 54 4e 55 4c 4c 22 2c 0a 20 2f 2a  LL|NOTNULL",. /*
31703 20 32 31 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a   215 */ "expr ::
31704 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c 4c 22  = expr NOT NULL"
31705 2c 0a 20 2f 2a 20 32 31 36 20 2a 2f 20 22 65 78  ,. /* 216 */ "ex
31706 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 65  pr ::= expr IS e
31707 78 70 72 22 2c 0a 20 2f 2a 20 32 31 37 20 2a 2f  xpr",. /* 217 */
31708 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
31709 49 53 20 4e 4f 54 20 65 78 70 72 22 2c 0a 20 2f  IS NOT expr",. /
3170a 2a 20 32 31 38 20 2a 2f 20 22 65 78 70 72 20 3a  * 218 */ "expr :
3170b 3a 3d 20 4e 4f 54 20 65 78 70 72 22 2c 0a 20 2f  := NOT expr",. /
3170c 2a 20 32 31 39 20 2a 2f 20 22 65 78 70 72 20 3a  * 219 */ "expr :
3170d 3a 3d 20 42 49 54 4e 4f 54 20 65 78 70 72 22 2c  := BITNOT expr",
3170e 0a 20 2f 2a 20 32 32 30 20 2a 2f 20 22 65 78 70  . /* 220 */ "exp
3170f 72 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78 70 72  r ::= MINUS expr
31710 22 2c 0a 20 2f 2a 20 32 32 31 20 2a 2f 20 22 65  ",. /* 221 */ "e
31711 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20 65 78 70  xpr ::= PLUS exp
31712 72 22 2c 0a 20 2f 2a 20 32 32 32 20 2a 2f 20 22  r",. /* 222 */ "
31713 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42  between_op ::= B
31714 45 54 57 45 45 4e 22 2c 0a 20 2f 2a 20 32 32 33  ETWEEN",. /* 223
31715 20 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f 70 20   */ "between_op 
31716 3a 3a 3d 20 4e 4f 54 20 42 45 54 57 45 45 4e 22  ::= NOT BETWEEN"
31717 2c 0a 20 2f 2a 20 32 32 34 20 2a 2f 20 22 65 78  ,. /* 224 */ "ex
31718 70 72 20 3a 3a 3d 20 65 78 70 72 20 62 65 74 77  pr ::= expr betw
31719 65 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44 20  een_op expr AND 
3171a 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 35 20 2a  expr",. /* 225 *
3171b 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 49 4e 22  / "in_op ::= IN"
3171c 2c 0a 20 2f 2a 20 32 32 36 20 2a 2f 20 22 69 6e  ,. /* 226 */ "in
3171d 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e 22 2c  _op ::= NOT IN",
3171e 0a 20 2f 2a 20 32 32 37 20 2a 2f 20 22 65 78 70  . /* 227 */ "exp
3171f 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70  r ::= expr in_op
31720 20 4c 50 20 65 78 70 72 6c 69 73 74 20 52 50 22   LP exprlist RP"
31721 2c 0a 20 2f 2a 20 32 32 38 20 2a 2f 20 22 65 78  ,. /* 228 */ "ex
31722 70 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65 63 74  pr ::= LP select
31723 20 52 50 22 2c 0a 20 2f 2a 20 32 32 39 20 2a 2f   RP",. /* 229 */
31724 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
31725 69 6e 5f 6f 70 20 4c 50 20 73 65 6c 65 63 74 20  in_op LP select 
31726 52 50 22 2c 0a 20 2f 2a 20 32 33 30 20 2a 2f 20  RP",. /* 230 */ 
31727 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69  "expr ::= expr i
31728 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20  n_op nm dbnm",. 
31729 2f 2a 20 32 33 31 20 2a 2f 20 22 65 78 70 72 20  /* 231 */ "expr 
3172a 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50 20 73 65  ::= EXISTS LP se
3172b 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 33  lect RP",. /* 23
3172c 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 43  2 */ "expr ::= C
3172d 41 53 45 20 63 61 73 65 5f 6f 70 65 72 61 6e 64  ASE case_operand
3172e 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 63   case_exprlist c
3172f 61 73 65 5f 65 6c 73 65 20 45 4e 44 22 2c 0a 20  ase_else END",. 
31730 2f 2a 20 32 33 33 20 2a 2f 20 22 63 61 73 65 5f  /* 233 */ "case_
31731 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 63 61 73  exprlist ::= cas
31732 65 5f 65 78 70 72 6c 69 73 74 20 57 48 45 4e 20  e_exprlist WHEN 
31733 65 78 70 72 20 54 48 45 4e 20 65 78 70 72 22 2c  expr THEN expr",
31734 0a 20 2f 2a 20 32 33 34 20 2a 2f 20 22 63 61 73  . /* 234 */ "cas
31735 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 57  e_exprlist ::= W
31736 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
31737 70 72 22 2c 0a 20 2f 2a 20 32 33 35 20 2a 2f 20  pr",. /* 235 */ 
31738 22 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 45  "case_else ::= E
31739 4c 53 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  LSE expr",. /* 2
3173a 33 36 20 2a 2f 20 22 63 61 73 65 5f 65 6c 73 65  36 */ "case_else
3173b 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 33 37 20 2a   ::=",. /* 237 *
3173c 2f 20 22 63 61 73 65 5f 6f 70 65 72 61 6e 64 20  / "case_operand 
3173d 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  ::= expr",. /* 2
3173e 33 38 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72  38 */ "case_oper
3173f 61 6e 64 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 33  and ::=",. /* 23
31740 39 20 2a 2f 20 22 65 78 70 72 6c 69 73 74 20 3a  9 */ "exprlist :
31741 3a 3d 20 6e 65 78 70 72 6c 69 73 74 22 2c 0a 20  := nexprlist",. 
31742 2f 2a 20 32 34 30 20 2a 2f 20 22 65 78 70 72 6c  /* 240 */ "exprl
31743 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34  ist ::=",. /* 24
31744 31 20 2a 2f 20 22 6e 65 78 70 72 6c 69 73 74 20  1 */ "nexprlist 
31745 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20 43 4f  ::= nexprlist CO
31746 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  MMA expr",. /* 2
31747 34 32 20 2a 2f 20 22 6e 65 78 70 72 6c 69 73 74  42 */ "nexprlist
31748 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20   ::= expr",. /* 
31749 32 34 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  243 */ "cmd ::= 
3174a 63 72 65 61 74 65 6b 77 20 75 6e 69 71 75 65 66  createkw uniquef
3174b 6c 61 67 20 49 4e 44 45 58 20 69 66 6e 6f 74 65  lag INDEX ifnote
3174c 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e  xists nm dbnm ON
3174d 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74 20 52   nm LP idxlist R
3174e 50 22 2c 0a 20 2f 2a 20 32 34 34 20 2a 2f 20 22  P",. /* 244 */ "
3174f 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 55  uniqueflag ::= U
31750 4e 49 51 55 45 22 2c 0a 20 2f 2a 20 32 34 35 20  NIQUE",. /* 245 
31751 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a  */ "uniqueflag :
31752 3a 3d 22 2c 0a 20 2f 2a 20 32 34 36 20 2a 2f 20  :=",. /* 246 */ 
31753 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  "idxlist_opt ::=
31754 22 2c 0a 20 2f 2a 20 32 34 37 20 2a 2f 20 22 69  ",. /* 247 */ "i
31755 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c  dxlist_opt ::= L
31756 50 20 69 64 78 6c 69 73 74 20 52 50 22 2c 0a 20  P idxlist RP",. 
31757 2f 2a 20 32 34 38 20 2a 2f 20 22 69 64 78 6c 69  /* 248 */ "idxli
31758 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74 20 43  st ::= idxlist C
31759 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74 65 20  OMMA nm collate 
3175a 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20  sortorder",. /* 
3175b 32 34 39 20 2a 2f 20 22 69 64 78 6c 69 73 74 20  249 */ "idxlist 
3175c 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73  ::= nm collate s
3175d 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 32  ortorder",. /* 2
3175e 35 30 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20 3a  50 */ "collate :
3175f 3a 3d 22 2c 0a 20 2f 2a 20 32 35 31 20 2a 2f 20  :=",. /* 251 */ 
31760 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c  "collate ::= COL
31761 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20 32  LATE ids",. /* 2
31762 35 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44  52 */ "cmd ::= D
31763 52 4f 50 20 49 4e 44 45 58 20 69 66 65 78 69 73  ROP INDEX ifexis
31764 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f  ts fullname",. /
31765 2a 20 32 35 33 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 253 */ "cmd ::
31766 3d 20 56 41 43 55 55 4d 22 2c 0a 20 2f 2a 20 32  = VACUUM",. /* 2
31767 35 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 56  54 */ "cmd ::= V
31768 41 43 55 55 4d 20 6e 6d 22 2c 0a 20 2f 2a 20 32  ACUUM nm",. /* 2
31769 35 35 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50  55 */ "cmd ::= P
3176a 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 22 2c 0a  RAGMA nm dbnm",.
3176b 20 2f 2a 20 32 35 36 20 2a 2f 20 22 63 6d 64 20   /* 256 */ "cmd 
3176c 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
3176d 6e 6d 20 45 51 20 6e 6d 6e 75 6d 22 2c 0a 20 2f  nm EQ nmnum",. /
3176e 2a 20 32 35 37 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 257 */ "cmd ::
3176f 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d  = PRAGMA nm dbnm
31770 20 4c 50 20 6e 6d 6e 75 6d 20 52 50 22 2c 0a 20   LP nmnum RP",. 
31771 2f 2a 20 32 35 38 20 2a 2f 20 22 63 6d 64 20 3a  /* 258 */ "cmd :
31772 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
31773 6d 20 45 51 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c  m EQ minus_num",
31774 0a 20 2f 2a 20 32 35 39 20 2a 2f 20 22 63 6d 64  . /* 259 */ "cmd
31775 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
31776 62 6e 6d 20 4c 50 20 6d 69 6e 75 73 5f 6e 75 6d  bnm LP minus_num
31777 20 52 50 22 2c 0a 20 2f 2a 20 32 36 30 20 2a 2f   RP",. /* 260 */
31778 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73   "nmnum ::= plus
31779 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 32 36 31 20 2a  _num",. /* 261 *
3177a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 22  / "nmnum ::= nm"
3177b 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f 20 22 6e 6d  ,. /* 262 */ "nm
3177c 6e 75 6d 20 3a 3a 3d 20 4f 4e 22 2c 0a 20 2f 2a  num ::= ON",. /*
3177d 20 32 36 33 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a   263 */ "nmnum :
3177e 3a 3d 20 44 45 4c 45 54 45 22 2c 0a 20 2f 2a 20  := DELETE",. /* 
3177f 32 36 34 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a  264 */ "nmnum ::
31780 3d 20 44 45 46 41 55 4c 54 22 2c 0a 20 2f 2a 20  = DEFAULT",. /* 
31781 32 36 35 20 2a 2f 20 22 70 6c 75 73 5f 6e 75 6d  265 */ "plus_num
31782 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74 20 6e 75   ::= plus_opt nu
31783 6d 62 65 72 22 2c 0a 20 2f 2a 20 32 36 36 20 2a  mber",. /* 266 *
31784 2f 20 22 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d  / "minus_num ::=
31785 20 4d 49 4e 55 53 20 6e 75 6d 62 65 72 22 2c 0a   MINUS number",.
31786 20 2f 2a 20 32 36 37 20 2a 2f 20 22 6e 75 6d 62   /* 267 */ "numb
31787 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46  er ::= INTEGER|F
31788 4c 4f 41 54 22 2c 0a 20 2f 2a 20 32 36 38 20 2a  LOAT",. /* 268 *
31789 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20  / "plus_opt ::= 
3178a 50 4c 55 53 22 2c 0a 20 2f 2a 20 32 36 39 20 2a  PLUS",. /* 269 *
3178b 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 22  / "plus_opt ::="
3178c 2c 0a 20 2f 2a 20 32 37 30 20 2a 2f 20 22 63 6d  ,. /* 270 */ "cm
3178d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74  d ::= createkw t
3178e 72 69 67 67 65 72 5f 64 65 63 6c 20 42 45 47 49  rigger_decl BEGI
3178f 4e 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69  N trigger_cmd_li
31790 73 74 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 37 31  st END",. /* 271
31791 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 64 65 63   */ "trigger_dec
31792 6c 20 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47  l ::= temp TRIGG
31793 45 52 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e  ER ifnotexists n
31794 6d 20 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74  m dbnm trigger_t
31795 69 6d 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e  ime trigger_even
31796 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f  t ON fullname fo
31797 72 65 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65  reach_clause whe
31798 6e 5f 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 32  n_clause",. /* 2
31799 37 32 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74  72 */ "trigger_t
3179a 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45 22 2c  ime ::= BEFORE",
3179b 0a 20 2f 2a 20 32 37 33 20 2a 2f 20 22 74 72 69  . /* 273 */ "tri
3179c 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41 46  gger_time ::= AF
3179d 54 45 52 22 2c 0a 20 2f 2a 20 32 37 34 20 2a 2f  TER",. /* 274 */
3179e 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a   "trigger_time :
3179f 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46 22 2c 0a  := INSTEAD OF",.
317a0 20 2f 2a 20 32 37 35 20 2a 2f 20 22 74 72 69 67   /* 275 */ "trig
317a1 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 22 2c 0a 20  ger_time ::=",. 
317a2 2f 2a 20 32 37 36 20 2a 2f 20 22 74 72 69 67 67  /* 276 */ "trigg
317a3 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c  er_event ::= DEL
317a4 45 54 45 7c 49 4e 53 45 52 54 22 2c 0a 20 2f 2a  ETE|INSERT",. /*
317a5 20 32 37 37 20 2a 2f 20 22 74 72 69 67 67 65 72   277 */ "trigger
317a6 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54  _event ::= UPDAT
317a7 45 22 2c 0a 20 2f 2a 20 32 37 38 20 2a 2f 20 22  E",. /* 278 */ "
317a8 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a  trigger_event ::
317a9 3d 20 55 50 44 41 54 45 20 4f 46 20 69 6e 73 63  = UPDATE OF insc
317aa 6f 6c 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 37 39  ollist",. /* 279
317ab 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c 61   */ "foreach_cla
317ac 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38  use ::=",. /* 28
317ad 30 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c  0 */ "foreach_cl
317ae 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43  ause ::= FOR EAC
317af 48 20 52 4f 57 22 2c 0a 20 2f 2a 20 32 38 31 20  H ROW",. /* 281 
317b0 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20  */ "when_clause 
317b1 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 32 20 2a 2f  ::=",. /* 282 */
317b2 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a   "when_clause ::
317b3 3d 20 57 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f  = WHEN expr",. /
317b4 2a 20 32 38 33 20 2a 2f 20 22 74 72 69 67 67 65  * 283 */ "trigge
317b5 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74  r_cmd_list ::= t
317b6 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20  rigger_cmd_list 
317b7 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49  trigger_cmd SEMI
317b8 22 2c 0a 20 2f 2a 20 32 38 34 20 2a 2f 20 22 74  ",. /* 284 */ "t
317b9 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20  rigger_cmd_list 
317ba 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64 20  ::= trigger_cmd 
317bb 53 45 4d 49 22 2c 0a 20 2f 2a 20 32 38 35 20 2a  SEMI",. /* 285 *
317bc 2f 20 22 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 22 2c  / "trnm ::= nm",
317bd 0a 20 2f 2a 20 32 38 36 20 2a 2f 20 22 74 72 6e  . /* 286 */ "trn
317be 6d 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 22  m ::= nm DOT nm"
317bf 2c 0a 20 2f 2a 20 32 38 37 20 2a 2f 20 22 74 72  ,. /* 287 */ "tr
317c0 69 64 78 62 79 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  idxby ::=",. /* 
317c1 32 38 38 20 2a 2f 20 22 74 72 69 64 78 62 79 20  288 */ "tridxby 
317c2 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59 20 6e  ::= INDEXED BY n
317c3 6d 22 2c 0a 20 2f 2a 20 32 38 39 20 2a 2f 20 22  m",. /* 289 */ "
317c4 74 72 69 64 78 62 79 20 3a 3a 3d 20 4e 4f 54 20  tridxby ::= NOT 
317c5 49 4e 44 45 58 45 44 22 2c 0a 20 2f 2a 20 32 39  INDEXED",. /* 29
317c6 30 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d  0 */ "trigger_cm
317c7 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63  d ::= UPDATE orc
317c8 6f 6e 66 20 74 72 6e 6d 20 74 72 69 64 78 62 79  onf trnm tridxby
317c9 20 53 45 54 20 73 65 74 6c 69 73 74 20 77 68 65   SET setlist whe
317ca 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 39 31  re_opt",. /* 291
317cb 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64   */ "trigger_cmd
317cc 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20   ::= insert_cmd 
317cd 49 4e 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f 6c  INTO trnm inscol
317ce 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20  list_opt VALUES 
317cf 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 22 2c  LP itemlist RP",
317d0 0a 20 2f 2a 20 32 39 32 20 2a 2f 20 22 74 72 69  . /* 292 */ "tri
317d1 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73  gger_cmd ::= ins
317d2 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72 6e  ert_cmd INTO trn
317d3 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  m inscollist_opt
317d4 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 32 39   select",. /* 29
317d5 33 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d  3 */ "trigger_cm
317d6 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f  d ::= DELETE FRO
317d7 4d 20 74 72 6e 6d 20 74 72 69 64 78 62 79 20 77  M trnm tridxby w
317d8 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32  here_opt",. /* 2
317d9 39 34 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  94 */ "trigger_c
317da 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 22 2c 0a  md ::= select",.
317db 20 2f 2a 20 32 39 35 20 2a 2f 20 22 65 78 70 72   /* 295 */ "expr
317dc 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 49 47   ::= RAISE LP IG
317dd 4e 4f 52 45 20 52 50 22 2c 0a 20 2f 2a 20 32 39  NORE RP",. /* 29
317de 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52  6 */ "expr ::= R
317df 41 49 53 45 20 4c 50 20 72 61 69 73 65 74 79 70  AISE LP raisetyp
317e0 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50 22 2c 0a  e COMMA nm RP",.
317e1 20 2f 2a 20 32 39 37 20 2a 2f 20 22 72 61 69 73   /* 297 */ "rais
317e2 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41  etype ::= ROLLBA
317e3 43 4b 22 2c 0a 20 2f 2a 20 32 39 38 20 2a 2f 20  CK",. /* 298 */ 
317e4 22 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 41  "raisetype ::= A
317e5 42 4f 52 54 22 2c 0a 20 2f 2a 20 32 39 39 20 2a  BORT",. /* 299 *
317e6 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a 3a 3d  / "raisetype ::=
317e7 20 46 41 49 4c 22 2c 0a 20 2f 2a 20 33 30 30 20   FAIL",. /* 300 
317e8 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50  */ "cmd ::= DROP
317e9 20 54 52 49 47 47 45 52 20 69 66 65 78 69 73 74   TRIGGER ifexist
317ea 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a  s fullname",. /*
317eb 20 33 30 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   301 */ "cmd ::=
317ec 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73 65   ATTACH database
317ed 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 41 53 20  _kw_opt expr AS 
317ee 65 78 70 72 20 6b 65 79 5f 6f 70 74 22 2c 0a 20  expr key_opt",. 
317ef 2f 2a 20 33 30 32 20 2a 2f 20 22 63 6d 64 20 3a  /* 302 */ "cmd :
317f0 3a 3d 20 44 45 54 41 43 48 20 64 61 74 61 62 61  := DETACH databa
317f1 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 22 2c  se_kw_opt expr",
317f2 0a 20 2f 2a 20 33 30 33 20 2a 2f 20 22 6b 65 79  . /* 303 */ "key
317f3 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33  _opt ::=",. /* 3
317f4 30 34 20 2a 2f 20 22 6b 65 79 5f 6f 70 74 20 3a  04 */ "key_opt :
317f5 3a 3d 20 4b 45 59 20 65 78 70 72 22 2c 0a 20 2f  := KEY expr",. /
317f6 2a 20 33 30 35 20 2a 2f 20 22 64 61 74 61 62 61  * 305 */ "databa
317f7 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44 41  se_kw_opt ::= DA
317f8 54 41 42 41 53 45 22 2c 0a 20 2f 2a 20 33 30 36  TABASE",. /* 306
317f9 20 2a 2f 20 22 64 61 74 61 62 61 73 65 5f 6b 77   */ "database_kw
317fa 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33  _opt ::=",. /* 3
317fb 30 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52  07 */ "cmd ::= R
317fc 45 49 4e 44 45 58 22 2c 0a 20 2f 2a 20 33 30 38  EINDEX",. /* 308
317fd 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49   */ "cmd ::= REI
317fe 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20  NDEX nm dbnm",. 
317ff 2f 2a 20 33 30 39 20 2a 2f 20 22 63 6d 64 20 3a  /* 309 */ "cmd :
31800 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c 0a 20 2f 2a  := ANALYZE",. /*
31801 20 33 31 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   310 */ "cmd ::=
31802 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d   ANALYZE nm dbnm
31803 22 2c 0a 20 2f 2a 20 33 31 31 20 2a 2f 20 22 63  ",. /* 311 */ "c
31804 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42  md ::= ALTER TAB
31805 4c 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41  LE fullname RENA
31806 4d 45 20 54 4f 20 6e 6d 22 2c 0a 20 2f 2a 20 33  ME TO nm",. /* 3
31807 31 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41  12 */ "cmd ::= A
31808 4c 54 45 52 20 54 41 42 4c 45 20 61 64 64 5f 63  LTER TABLE add_c
31809 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 41  olumn_fullname A
3180a 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20  DD kwcolumn_opt 
3180b 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 33 31 33  column",. /* 313
3180c 20 2a 2f 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f   */ "add_column_
3180d 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c  fullname ::= ful
3180e 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 33 31 34 20  lname",. /* 314 
3180f 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74  */ "kwcolumn_opt
31810 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 31 35 20 2a   ::=",. /* 315 *
31811 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20  / "kwcolumn_opt 
31812 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 22 2c 0a 20  ::= COLUMNKW",. 
31813 2f 2a 20 33 31 36 20 2a 2f 20 22 63 6d 64 20 3a  /* 316 */ "cmd :
31814 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62 22 2c  := create_vtab",
31815 0a 20 2f 2a 20 33 31 37 20 2a 2f 20 22 63 6d 64  . /* 317 */ "cmd
31816 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62   ::= create_vtab
31817 20 4c 50 20 76 74 61 62 61 72 67 6c 69 73 74 20   LP vtabarglist 
31818 52 50 22 2c 0a 20 2f 2a 20 33 31 38 20 2a 2f 20  RP",. /* 318 */ 
31819 22 63 72 65 61 74 65 5f 76 74 61 62 20 3a 3a 3d  "create_vtab ::=
3181a 20 63 72 65 61 74 65 6b 77 20 56 49 52 54 55 41   createkw VIRTUA
3181b 4c 20 54 41 42 4c 45 20 6e 6d 20 64 62 6e 6d 20  L TABLE nm dbnm 
3181c 55 53 49 4e 47 20 6e 6d 22 2c 0a 20 2f 2a 20 33  USING nm",. /* 3
3181d 31 39 20 2a 2f 20 22 76 74 61 62 61 72 67 6c 69  19 */ "vtabargli
3181e 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67 22 2c  st ::= vtabarg",
3181f 0a 20 2f 2a 20 33 32 30 20 2a 2f 20 22 76 74 61  . /* 320 */ "vta
31820 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61  barglist ::= vta
31821 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41 20 76  barglist COMMA v
31822 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20 33 32 31  tabarg",. /* 321
31823 20 2a 2f 20 22 76 74 61 62 61 72 67 20 3a 3a 3d   */ "vtabarg ::=
31824 22 2c 0a 20 2f 2a 20 33 32 32 20 2a 2f 20 22 76  ",. /* 322 */ "v
31825 74 61 62 61 72 67 20 3a 3a 3d 20 76 74 61 62 61  tabarg ::= vtaba
31826 72 67 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 22  rg vtabargtoken"
31827 2c 0a 20 2f 2a 20 33 32 33 20 2a 2f 20 22 76 74  ,. /* 323 */ "vt
31828 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41  abargtoken ::= A
31829 4e 59 22 2c 0a 20 2f 2a 20 33 32 34 20 2a 2f 20  NY",. /* 324 */ 
3182a 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a  "vtabargtoken ::
3182b 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52 50 22  = lp anylist RP"
3182c 2c 0a 20 2f 2a 20 33 32 35 20 2a 2f 20 22 6c 70  ,. /* 325 */ "lp
3182d 20 3a 3a 3d 20 4c 50 22 2c 0a 20 2f 2a 20 33 32   ::= LP",. /* 32
3182e 36 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20 3a 3a  6 */ "anylist ::
3182f 3d 22 2c 0a 20 2f 2a 20 33 32 37 20 2a 2f 20 22  =",. /* 327 */ "
31830 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c  anylist ::= anyl
31831 69 73 74 20 4c 50 20 61 6e 79 6c 69 73 74 20 52  ist LP anylist R
31832 50 22 2c 0a 20 2f 2a 20 33 32 38 20 2a 2f 20 22  P",. /* 328 */ "
31833 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c  anylist ::= anyl
31834 69 73 74 20 41 4e 59 22 2c 0a 7d 3b 0a 23 65 6e  ist ANY",.};.#en
31835 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
31836 0a 0a 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45  ...#if YYSTACKDE
31837 50 54 48 3c 3d 30 0a 2f 2a 0a 2a 2a 20 54 72 79  PTH<=0./*.** Try
31838 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65   to increase the
31839 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 61 72   size of the par
3183a 73 65 72 20 73 74 61 63 6b 2e 0a 2a 2f 0a 73 74  ser stack..*/.st
3183b 61 74 69 63 20 76 6f 69 64 20 79 79 47 72 6f 77  atic void yyGrow
3183c 53 74 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a  Stack(yyParser *
3183d 70 29 7b 0a 20 20 69 6e 74 20 6e 65 77 53 69 7a  p){.  int newSiz
3183e 65 3b 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72  e;.  yyStackEntr
3183f 79 20 2a 70 4e 65 77 3b 0a 0a 20 20 6e 65 77 53  y *pNew;..  newS
31840 69 7a 65 20 3d 20 70 2d 3e 79 79 73 74 6b 73 7a  ize = p->yystksz
31841 2a 32 20 2b 20 31 30 30 3b 0a 20 20 70 4e 65 77  *2 + 100;.  pNew
31842 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2d 3e 79 79   = realloc(p->yy
31843 73 74 61 63 6b 2c 20 6e 65 77 53 69 7a 65 2a 73  stack, newSize*s
31844 69 7a 65 6f 66 28 70 4e 65 77 5b 30 5d 29 29 3b  izeof(pNew[0]));
31845 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20  .  if( pNew ){. 
31846 20 20 20 70 2d 3e 79 79 73 74 61 63 6b 20 3d 20     p->yystack = 
31847 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 79 79 73  pNew;.    p->yys
31848 74 6b 73 7a 20 3d 20 6e 65 77 53 69 7a 65 3b 0a  tksz = newSize;.
31849 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
3184a 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49     if( yyTraceFI
3184b 4c 45 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69  LE ){.      fpri
3184c 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
3184d 22 25 73 53 74 61 63 6b 20 67 72 6f 77 73 20 74  "%sStack grows t
3184e 6f 20 25 64 20 65 6e 74 72 69 65 73 21 5c 6e 22  o %d entries!\n"
3184f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
31850 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 70  yyTracePrompt, p
31851 2d 3e 79 79 73 74 6b 73 7a 29 3b 0a 20 20 20 20  ->yystksz);.    
31852 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 23  }.#endif.  }.}.#
31853 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20 54 68  endif../* .** Th
31854 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f  is function allo
31855 63 61 74 65 73 20 61 20 6e 65 77 20 70 61 72 73  cates a new pars
31856 65 72 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20  er..** The only 
31857 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f  argument is a po
31858 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74  inter to a funct
31859 69 6f 6e 20 77 68 69 63 68 20 77 6f 72 6b 73 20  ion which works 
3185a 6c 69 6b 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 2e 0a  like.** malloc..
3185b 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a  **.** Inputs:.**
3185c 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
3185d 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20  e function used 
3185e 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  to allocate memo
3185f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74  ry..**.** Output
31860 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  s:.** A pointer 
31861 74 6f 20 61 20 70 61 72 73 65 72 2e 20 20 54 68  to a parser.  Th
31862 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73  is pointer is us
31863 65 64 20 69 6e 20 73 75 62 73 65 71 75 65 6e 74  ed in subsequent
31864 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c   calls.** to sql
31865 69 74 65 33 50 61 72 73 65 72 20 61 6e 64 20 73  ite3Parser and s
31866 71 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65  qlite3ParserFree
31867 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
31868 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
31869 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69  3ParserAlloc(voi
3186a 64 20 2a 28 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29  d *(*mallocProc)
3186b 28 73 69 7a 65 5f 74 29 29 7b 0a 20 20 79 79 50  (size_t)){.  yyP
3186c 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 3b 0a  arser *pParser;.
3186d 20 20 70 50 61 72 73 65 72 20 3d 20 28 79 79 50    pParser = (yyP
3186e 61 72 73 65 72 2a 29 28 2a 6d 61 6c 6c 6f 63 50  arser*)(*mallocP
3186f 72 6f 63 29 28 20 28 73 69 7a 65 5f 74 29 73 69  roc)( (size_t)si
31870 7a 65 6f 66 28 79 79 50 61 72 73 65 72 29 20 29  zeof(yyParser) )
31871 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 72 20  ;.  if( pParser 
31872 29 7b 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e  ){.    pParser->
31873 79 79 69 64 78 20 3d 20 2d 31 3b 0a 23 69 66 64  yyidx = -1;.#ifd
31874 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41  ef YYTRACKMAXSTA
31875 43 4b 44 45 50 54 48 0a 20 20 20 20 70 50 61 72  CKDEPTH.    pPar
31876 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20  ser->yyidxMax = 
31877 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 59 59  0;.#endif.#if YY
31878 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20  STACKDEPTH<=0.  
31879 20 20 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61    pParser->yysta
3187a 63 6b 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 70  ck = NULL;.    p
3187b 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20  Parser->yystksz 
3187c 3d 20 30 3b 0a 20 20 20 20 79 79 47 72 6f 77 53  = 0;.    yyGrowS
3187d 74 61 63 6b 28 70 50 61 72 73 65 72 29 3b 0a 23  tack(pParser);.#
3187e 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75  endif.  }.  retu
3187f 72 6e 20 70 50 61 72 73 65 72 3b 0a 7d 0a 0a 2f  rn pParser;.}../
31880 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
31881 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 73  function deletes
31882 20 74 68 65 20 76 61 6c 75 65 20 61 73 73 6f 63   the value assoc
31883 69 61 74 65 64 20 77 69 74 68 20 61 0a 2a 2a 20  iated with a.** 
31884 73 79 6d 62 6f 6c 2e 20 20 54 68 65 20 73 79 6d  symbol.  The sym
31885 62 6f 6c 20 63 61 6e 20 62 65 20 65 69 74 68 65  bol can be eithe
31886 72 20 61 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20  r a terminal or 
31887 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 0a 2a 2a 20  nonterminal..** 
31888 22 79 79 6d 61 6a 6f 72 22 20 69 73 20 74 68 65  "yymajor" is the
31889 20 73 79 6d 62 6f 6c 20 63 6f 64 65 2c 20 61 6e   symbol code, an
3188a 64 20 22 79 79 70 6d 69 6e 6f 72 22 20 69 73 20  d "yypminor" is 
3188b 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20  a pointer to.** 
3188c 74 68 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74  the value..*/.st
3188d 61 74 69 63 20 76 6f 69 64 20 79 79 5f 64 65 73  atic void yy_des
3188e 74 72 75 63 74 6f 72 28 0a 20 20 79 79 50 61 72  tructor(.  yyPar
3188f 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20  ser *yypParser, 
31890 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
31891 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45   */.  YYCODETYPE
31892 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20 2f 2a   yymajor,     /*
31893 20 54 79 70 65 20 63 6f 64 65 20 66 6f 72 20 6f   Type code for o
31894 62 6a 65 63 74 20 74 6f 20 64 65 73 74 72 6f 79  bject to destroy
31895 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50   */.  YYMINORTYP
31896 45 20 2a 79 79 70 6d 69 6e 6f 72 20 20 20 2f 2a  E *yypminor   /*
31897 20 54 68 65 20 6f 62 6a 65 63 74 20 74 6f 20 62   The object to b
31898 65 20 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 29  e destroyed */.)
31899 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  {.  sqlite3Parse
3189a 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20 73 77  rARG_FETCH;.  sw
3189b 69 74 63 68 28 20 79 79 6d 61 6a 6f 72 20 29 7b  itch( yymajor ){
3189c 0a 20 20 20 20 2f 2a 20 48 65 72 65 20 69 73 20  .    /* Here is 
3189d 69 6e 73 65 72 74 65 64 20 74 68 65 20 61 63 74  inserted the act
3189e 69 6f 6e 73 20 77 68 69 63 68 20 74 61 6b 65 20  ions which take 
3189f 70 6c 61 63 65 20 77 68 65 6e 20 61 0a 20 20 20  place when a.   
318a0 20 2a 2a 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20   ** terminal or 
318a1 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 20 69 73 20  non-terminal is 
318a2 64 65 73 74 72 6f 79 65 64 2e 20 20 54 68 69 73  destroyed.  This
318a3 20 63 61 6e 20 68 61 70 70 65 6e 0a 20 20 20 20   can happen.    
318a4 2a 2a 20 77 68 65 6e 20 74 68 65 20 73 79 6d 62  ** when the symb
318a5 6f 6c 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f  ol is popped fro
318a6 6d 20 74 68 65 20 73 74 61 63 6b 20 64 75 72 69  m the stack duri
318a7 6e 67 20 61 0a 20 20 20 20 2a 2a 20 72 65 64 75  ng a.    ** redu
318a8 63 65 20 6f 72 20 64 75 72 69 6e 67 20 65 72 72  ce or during err
318a9 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 72  or processing or
318aa 20 77 68 65 6e 20 61 20 70 61 72 73 65 72 20 69   when a parser i
318ab 73 20 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67 20  s .    ** being 
318ac 64 65 73 74 72 6f 79 65 64 20 62 65 66 6f 72 65  destroyed before
318ad 20 69 74 20 69 73 20 66 69 6e 69 73 68 65 64 20   it is finished 
318ae 70 61 72 73 69 6e 67 2e 0a 20 20 20 20 2a 2a 0a  parsing..    **.
318af 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 64 75 72      ** Note: dur
318b0 69 6e 67 20 61 20 72 65 64 75 63 65 2c 20 74 68  ing a reduce, th
318b1 65 20 6f 6e 6c 79 20 73 79 6d 62 6f 6c 73 20 64  e only symbols d
318b2 65 73 74 72 6f 79 65 64 20 61 72 65 20 74 68 6f  estroyed are tho
318b3 73 65 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 20  se.    ** which 
318b4 61 70 70 65 61 72 20 6f 6e 20 74 68 65 20 52 48  appear on the RH
318b5 53 20 6f 66 20 74 68 65 20 72 75 6c 65 2c 20 62  S of the rule, b
318b6 75 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74  ut which are not
318b7 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 69 6e 73   used.    ** ins
318b8 69 64 65 20 74 68 65 20 43 20 63 6f 64 65 2e 0a  ide the C code..
318b9 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
318ba 31 36 30 3a 20 2f 2a 20 73 65 6c 65 63 74 20 2a  160: /* select *
318bb 2f 0a 20 20 20 20 63 61 73 65 20 31 39 34 3a 20  /.    case 194: 
318bc 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a  /* oneselect */.
318bd 7b 0a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  {.sqlite3SelectD
318be 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
318bf 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33  , (yypminor->yy3
318c0 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61  ));.}.      brea
318c1 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 37 34 3a  k;.    case 174:
318c2 20 2f 2a 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20   /* term */.    
318c3 63 61 73 65 20 31 37 35 3a 20 2f 2a 20 65 78 70  case 175: /* exp
318c4 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32  r */.    case 22
318c5 33 3a 20 2f 2a 20 65 73 63 61 70 65 20 2a 2f 0a  3: /* escape */.
318c6 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c  {.sqlite3ExprDel
318c7 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
318c8 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 34 36  (yypminor->yy346
318c9 29 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20  ).pExpr);.}.    
318ca 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
318cb 65 20 31 37 39 3a 20 2f 2a 20 69 64 78 6c 69 73  e 179: /* idxlis
318cc 74 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73  t_opt */.    cas
318cd 65 20 31 38 37 3a 20 2f 2a 20 69 64 78 6c 69 73  e 187: /* idxlis
318ce 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39  t */.    case 19
318cf 37 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74  7: /* selcollist
318d0 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 30   */.    case 200
318d1 3a 20 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74  : /* groupby_opt
318d2 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 32   */.    case 202
318d3 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74  : /* orderby_opt
318d4 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 34   */.    case 204
318d5 3a 20 2f 2a 20 73 63 6c 70 20 2a 2f 0a 20 20 20  : /* sclp */.   
318d6 20 63 61 73 65 20 32 31 34 3a 20 2f 2a 20 73 6f   case 214: /* so
318d7 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61  rtlist */.    ca
318d8 73 65 20 32 31 36 3a 20 2f 2a 20 6e 65 78 70 72  se 216: /* nexpr
318d9 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  list */.    case
318da 20 32 31 37 3a 20 2f 2a 20 73 65 74 6c 69 73 74   217: /* setlist
318db 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 30   */.    case 220
318dc 3a 20 2f 2a 20 69 74 65 6d 6c 69 73 74 20 2a 2f  : /* itemlist */
318dd 0a 20 20 20 20 63 61 73 65 20 32 32 31 3a 20 2f  .    case 221: /
318de 2a 20 65 78 70 72 6c 69 73 74 20 2a 2f 0a 20 20  * exprlist */.  
318df 20 20 63 61 73 65 20 32 32 37 3a 20 2f 2a 20 63    case 227: /* c
318e0 61 73 65 5f 65 78 70 72 6c 69 73 74 20 2a 2f 0a  ase_exprlist */.
318e1 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  {.sqlite3ExprLis
318e2 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
318e3 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79  db, (yypminor->y
318e4 79 31 34 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62  y14));.}.      b
318e5 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31  reak;.    case 1
318e6 39 33 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20  93: /* fullname 
318e7 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39 38 3a  */.    case 198:
318e8 20 2f 2a 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20   /* from */.    
318e9 63 61 73 65 20 32 30 36 3a 20 2f 2a 20 73 65 6c  case 206: /* sel
318ea 74 61 62 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63  tablist */.    c
318eb 61 73 65 20 32 30 37 3a 20 2f 2a 20 73 74 6c 5f  ase 207: /* stl_
318ec 70 72 65 66 69 78 20 2a 2f 0a 7b 0a 73 71 6c 69  prefix */.{.sqli
318ed 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
318ee 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79  (pParse->db, (yy
318ef 70 6d 69 6e 6f 72 2d 3e 79 79 36 35 29 29 3b 0a  pminor->yy65));.
318f0 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
318f1 20 20 20 63 61 73 65 20 31 39 39 3a 20 2f 2a 20     case 199: /* 
318f2 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 20 20 20  where_opt */.   
318f3 20 63 61 73 65 20 32 30 31 3a 20 2f 2a 20 68 61   case 201: /* ha
318f4 76 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20 20 20  ving_opt */.    
318f5 63 61 73 65 20 32 31 30 3a 20 2f 2a 20 6f 6e 5f  case 210: /* on_
318f6 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
318f7 32 31 35 3a 20 2f 2a 20 73 6f 72 74 69 74 65 6d  215: /* sortitem
318f8 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 36   */.    case 226
318f9 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e  : /* case_operan
318fa 64 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32  d */.    case 22
318fb 38 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20  8: /* case_else 
318fc 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 33 39 3a  */.    case 239:
318fd 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65 20   /* when_clause 
318fe 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 34 34 3a  */.    case 244:
318ff 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b   /* key_opt */.{
31900 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65  .sqlite3ExprDele
31901 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28  te(pParse->db, (
31902 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 33 32 29  yypminor->yy132)
31903 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  );.}.      break
31904 3b 0a 20 20 20 20 63 61 73 65 20 32 31 31 3a 20  ;.    case 211: 
31905 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a  /* using_opt */.
31906 20 20 20 20 63 61 73 65 20 32 31 33 3a 20 2f 2a      case 213: /*
31907 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a 20   inscollist */. 
31908 20 20 20 63 61 73 65 20 32 31 39 3a 20 2f 2a 20     case 219: /* 
31909 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 2a  inscollist_opt *
3190a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64 4c 69 73  /.{.sqlite3IdLis
3190b 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
3190c 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79  db, (yypminor->y
3190d 79 34 30 38 29 29 3b 0a 7d 0a 20 20 20 20 20 20  y408));.}.      
3190e 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
3190f 32 33 35 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  235: /* trigger_
31910 63 6d 64 5f 6c 69 73 74 20 2a 2f 0a 20 20 20 20  cmd_list */.    
31911 63 61 73 65 20 32 34 30 3a 20 2f 2a 20 74 72 69  case 240: /* tri
31912 67 67 65 72 5f 63 6d 64 20 2a 2f 0a 7b 0a 73 71  gger_cmd */.{.sq
31913 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
31914 65 72 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64  erStep(pParse->d
31915 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  b, (yypminor->yy
31916 34 37 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62  473));.}.      b
31917 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32  reak;.    case 2
31918 33 37 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65  37: /* trigger_e
31919 76 65 6e 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65  vent */.{.sqlite
3191a 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 70 50  3IdListDelete(pP
3191b 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69  arse->db, (yypmi
3191c 6e 6f 72 2d 3e 79 79 33 37 38 29 2e 62 29 3b 0a  nor->yy378).b);.
3191d 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
3191e 20 20 20 64 65 66 61 75 6c 74 3a 20 20 62 72 65     default:  bre
3191f 61 6b 3b 20 20 20 2f 2a 20 49 66 20 6e 6f 20 64  ak;   /* If no d
31920 65 73 74 72 75 63 74 6f 72 20 61 63 74 69 6f 6e  estructor action
31921 20 73 70 65 63 69 66 69 65 64 3a 20 64 6f 20 6e   specified: do n
31922 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 7d 0a  othing */.  }.}.
31923 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 74 68 65 20 70  ./*.** Pop the p
31924 61 72 73 65 72 27 73 20 73 74 61 63 6b 20 6f 6e  arser's stack on
31925 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ce..**.** If the
31926 72 65 20 69 73 20 61 20 64 65 73 74 72 75 63 74  re is a destruct
31927 6f 72 20 72 6f 75 74 69 6e 65 20 61 73 73 6f 63  or routine assoc
31928 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74  iated with the t
31929 6f 6b 65 6e 20 77 68 69 63 68 0a 2a 2a 20 69 73  oken which.** is
3192a 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65   popped from the
3192b 20 73 74 61 63 6b 2c 20 74 68 65 6e 20 63 61 6c   stack, then cal
3192c 6c 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  l it..**.** Retu
3192d 72 6e 20 74 68 65 20 6d 61 6a 6f 72 20 74 6f 6b  rn the major tok
3192e 65 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  en number for th
3192f 65 20 73 79 6d 62 6f 6c 20 70 6f 70 70 65 64 2e  e symbol popped.
31930 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 79  .*/.static int y
31931 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61  y_pop_parser_sta
31932 63 6b 28 79 79 50 61 72 73 65 72 20 2a 70 50 61  ck(yyParser *pPa
31933 72 73 65 72 29 7b 0a 20 20 59 59 43 4f 44 45 54  rser){.  YYCODET
31934 59 50 45 20 79 79 6d 61 6a 6f 72 3b 0a 20 20 79  YPE yymajor;.  y
31935 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 74  yStackEntry *yyt
31936 6f 73 20 3d 20 26 70 50 61 72 73 65 72 2d 3e 79  os = &pParser->y
31937 79 73 74 61 63 6b 5b 70 50 61 72 73 65 72 2d 3e  ystack[pParser->
31938 79 79 69 64 78 5d 3b 0a 0a 20 20 2f 2a 20 54 68  yyidx];..  /* Th
31939 65 72 65 20 69 73 20 6e 6f 20 6d 65 63 68 61 6e  ere is no mechan
3193a 69 73 6d 20 62 79 20 77 68 69 63 68 20 74 68 65  ism by which the
3193b 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 63 61   parser stack ca
3193c 6e 20 62 65 20 70 6f 70 70 65 64 20 62 65 6c 6f  n be popped belo
3193d 77 0a 20 20 2a 2a 20 65 6d 70 74 79 20 69 6e 20  w.  ** empty in 
3193e 53 51 4c 69 74 65 2e 20 20 2a 2f 0a 20 20 69 66  SQLite.  */.  if
3193f 28 20 4e 45 56 45 52 28 70 50 61 72 73 65 72 2d  ( NEVER(pParser-
31940 3e 79 79 69 64 78 3c 30 29 20 29 20 72 65 74 75  >yyidx<0) ) retu
31941 72 6e 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e 44  rn 0;.#ifndef ND
31942 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61  EBUG.  if( yyTra
31943 63 65 46 49 4c 45 20 26 26 20 70 50 61 72 73 65  ceFILE && pParse
31944 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 7b 0a 20  r->yyidx>=0 ){. 
31945 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
31946 63 65 46 49 4c 45 2c 22 25 73 50 6f 70 70 69 6e  ceFILE,"%sPoppin
31947 67 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 79  g %s\n",.      y
31948 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 0a 20 20  yTracePrompt,.  
31949 20 20 20 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b      yyTokenName[
3194a 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 5d 29 3b 0a  yytos->major]);.
3194b 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 6d    }.#endif.  yym
3194c 61 6a 6f 72 20 3d 20 79 79 74 6f 73 2d 3e 6d 61  ajor = yytos->ma
3194d 6a 6f 72 3b 0a 20 20 79 79 5f 64 65 73 74 72 75  jor;.  yy_destru
3194e 63 74 6f 72 28 70 50 61 72 73 65 72 2c 20 79 79  ctor(pParser, yy
3194f 6d 61 6a 6f 72 2c 20 26 79 79 74 6f 73 2d 3e 6d  major, &yytos->m
31950 69 6e 6f 72 29 3b 0a 20 20 70 50 61 72 73 65 72  inor);.  pParser
31951 2d 3e 79 79 69 64 78 2d 2d 3b 0a 20 20 72 65 74  ->yyidx--;.  ret
31952 75 72 6e 20 79 79 6d 61 6a 6f 72 3b 0a 7d 0a 0a  urn yymajor;.}..
31953 2f 2a 20 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74  /* .** Deallocat
31954 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20 61 20  e and destroy a 
31955 70 61 72 73 65 72 2e 20 20 44 65 73 74 72 75 63  parser.  Destruc
31956 74 6f 72 73 20 61 72 65 20 61 6c 6c 20 63 61 6c  tors are all cal
31957 6c 65 64 20 66 6f 72 0a 2a 2a 20 61 6c 6c 20 73  led for.** all s
31958 74 61 63 6b 20 65 6c 65 6d 65 6e 74 73 20 62 65  tack elements be
31959 66 6f 72 65 20 73 68 75 74 74 69 6e 67 20 74 68  fore shutting th
3195a 65 20 70 61 72 73 65 72 20 64 6f 77 6e 2e 0a 2a  e parser down..*
3195b 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20  *.** Inputs:.** 
3195c 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 41 20  <ul>.** <li>  A 
3195d 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
3195e 61 72 73 65 72 2e 20 20 54 68 69 73 20 73 68 6f  arser.  This sho
3195f 75 6c 64 20 62 65 20 61 20 70 6f 69 6e 74 65 72  uld be a pointer
31960 0a 2a 2a 20 20 20 20 20 20 20 6f 62 74 61 69 6e  .**       obtain
31961 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 50  ed from sqlite3P
31962 61 72 73 65 72 41 6c 6c 6f 63 2e 0a 2a 2a 20 3c  arserAlloc..** <
31963 6c 69 3e 20 20 41 20 70 6f 69 6e 74 65 72 20 74  li>  A pointer t
31964 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  o a function use
31965 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d  d to reclaim mem
31966 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20  ory obtained.** 
31967 20 20 20 20 20 20 66 72 6f 6d 20 6d 61 6c 6c 6f        from mallo
31968 63 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f 0a 53  c..** </ul>.*/.S
31969 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3196a 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  id sqlite3Parser
3196b 46 72 65 65 28 0a 20 20 76 6f 69 64 20 2a 70 2c  Free(.  void *p,
3196c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3196d 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
3196e 72 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20  r to be deleted 
3196f 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 66 72 65 65  */.  void (*free
31970 50 72 6f 63 29 28 76 6f 69 64 2a 29 20 20 20 20  Proc)(void*)    
31971 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 75 73 65   /* Function use
31972 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d  d to reclaim mem
31973 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20 79 79 50 61  ory */.){.  yyPa
31974 72 73 65 72 20 2a 70 50 61 72 73 65 72 20 3d 20  rser *pParser = 
31975 28 79 79 50 61 72 73 65 72 2a 29 70 3b 0a 20 20  (yyParser*)p;.  
31976 2f 2a 20 49 6e 20 53 51 4c 69 74 65 2c 20 77 65  /* In SQLite, we
31977 20 6e 65 76 65 72 20 74 72 79 20 74 6f 20 64 65   never try to de
31978 73 74 72 6f 79 20 61 20 70 61 72 73 65 72 20 74  stroy a parser t
31979 68 61 74 20 77 61 73 20 6e 6f 74 20 73 75 63 63  hat was not succ
3197a 65 73 73 66 75 6c 6c 79 0a 20 20 2a 2a 20 63 72  essfully.  ** cr
3197b 65 61 74 65 64 20 69 6e 20 74 68 65 20 66 69 72  eated in the fir
3197c 73 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 69  st place. */.  i
3197d 66 28 20 4e 45 56 45 52 28 70 50 61 72 73 65 72  f( NEVER(pParser
3197e 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20  ==0) ) return;. 
3197f 20 77 68 69 6c 65 28 20 70 50 61 72 73 65 72 2d   while( pParser-
31980 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70  >yyidx>=0 ) yy_p
31981 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28  op_parser_stack(
31982 70 50 61 72 73 65 72 29 3b 0a 23 69 66 20 59 59  pParser);.#if YY
31983 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20  STACKDEPTH<=0.  
31984 66 72 65 65 28 70 50 61 72 73 65 72 2d 3e 79 79  free(pParser->yy
31985 73 74 61 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 20  stack);.#endif. 
31986 20 28 2a 66 72 65 65 50 72 6f 63 29 28 28 76 6f   (*freeProc)((vo
31987 69 64 2a 29 70 50 61 72 73 65 72 29 3b 0a 7d 0a  id*)pParser);.}.
31988 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
31989 65 20 70 65 61 6b 20 64 65 70 74 68 20 6f 66 20  e peak depth of 
3198a 74 68 65 20 73 74 61 63 6b 20 66 6f 72 20 61 20  the stack for a 
3198b 70 61 72 73 65 72 2e 0a 2a 2f 0a 23 69 66 64 65  parser..*/.#ifde
3198c 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43  f YYTRACKMAXSTAC
3198d 4b 44 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52  KDEPTH.SQLITE_PR
3198e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
3198f 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b  3ParserStackPeak
31990 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 79 79 50  (void *p){.  yyP
31991 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 20 3d  arser *pParser =
31992 20 28 79 79 50 61 72 73 65 72 2a 29 70 3b 0a 20   (yyParser*)p;. 
31993 20 72 65 74 75 72 6e 20 70 50 61 72 73 65 72 2d   return pParser-
31994 3e 79 79 69 64 78 4d 61 78 3b 0a 7d 0a 23 65 6e  >yyidxMax;.}.#en
31995 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20  dif../*.** Find 
31996 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
31997 61 63 74 69 6f 6e 20 66 6f 72 20 61 20 70 61 72  action for a par
31998 73 65 72 20 67 69 76 65 6e 20 74 68 65 20 74 65  ser given the te
31999 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61  rminal.** look-a
3199a 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f 6b  head token iLook
3199b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  Ahead..**.** If 
3199c 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74  the look-ahead t
3199d 6f 6b 65 6e 20 69 73 20 59 59 4e 4f 43 4f 44 45  oken is YYNOCODE
3199e 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 74 6f 20  , then check to 
3199f 73 65 65 20 69 66 20 74 68 65 20 61 63 74 69 6f  see if the actio
319a0 6e 20 69 73 0a 2a 2a 20 69 6e 64 65 70 65 6e 64  n is.** independ
319a1 65 6e 74 20 6f 66 20 74 68 65 20 6c 6f 6f 6b 2d  ent of the look-
319a2 61 68 65 61 64 2e 20 20 49 66 20 69 74 20 69 73  ahead.  If it is
319a3 2c 20 72 65 74 75 72 6e 20 74 68 65 20 61 63 74  , return the act
319a4 69 6f 6e 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a  ion, otherwise.*
319a5 2a 20 72 65 74 75 72 6e 20 59 59 5f 4e 4f 5f 41  * return YY_NO_A
319a6 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63  CTION..*/.static
319a7 20 69 6e 74 20 79 79 5f 66 69 6e 64 5f 73 68 69   int yy_find_shi
319a8 66 74 5f 61 63 74 69 6f 6e 28 0a 20 20 79 79 50  ft_action(.  yyP
319a9 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 2c 20  arser *pParser, 
319aa 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
319ab 72 73 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45  rser */.  YYCODE
319ac 54 59 50 45 20 69 4c 6f 6f 6b 41 68 65 61 64 20  TYPE iLookAhead 
319ad 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d      /* The look-
319ae 61 68 65 61 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29  ahead token */.)
319af 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
319b0 20 73 74 61 74 65 6e 6f 20 3d 20 70 50 61 72 73   stateno = pPars
319b1 65 72 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61 72  er->yystack[pPar
319b2 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74  ser->yyidx].stat
319b3 65 6e 6f 3b 0a 20 0a 20 20 69 66 28 20 73 74 61  eno;. .  if( sta
319b4 74 65 6e 6f 3e 59 59 5f 53 48 49 46 54 5f 43 4f  teno>YY_SHIFT_CO
319b5 55 4e 54 0a 20 20 20 7c 7c 20 28 69 20 3d 20 79  UNT.   || (i = y
319b6 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 73 74 61  y_shift_ofst[sta
319b7 74 65 6e 6f 5d 29 3d 3d 59 59 5f 53 48 49 46 54  teno])==YY_SHIFT
319b8 5f 55 53 45 5f 44 46 4c 54 20 29 7b 0a 20 20 20  _USE_DFLT ){.   
319b9 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75   return yy_defau
319ba 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d  lt[stateno];.  }
319bb 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 6f 6b  .  assert( iLook
319bc 41 68 65 61 64 21 3d 59 59 4e 4f 43 4f 44 45 20  Ahead!=YYNOCODE 
319bd 29 3b 0a 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b 41  );.  i += iLookA
319be 68 65 61 64 3b 0a 20 20 69 66 28 20 69 3c 30 20  head;.  if( i<0 
319bf 7c 7c 20 69 3e 3d 59 59 5f 41 43 54 54 41 42 5f  || i>=YY_ACTTAB_
319c0 43 4f 55 4e 54 20 7c 7c 20 79 79 5f 6c 6f 6f 6b  COUNT || yy_look
319c1 61 68 65 61 64 5b 69 5d 21 3d 69 4c 6f 6f 6b 41  ahead[i]!=iLookA
319c2 68 65 61 64 20 29 7b 0a 20 20 20 20 69 66 28 20  head ){.    if( 
319c3 69 4c 6f 6f 6b 41 68 65 61 64 3e 30 20 29 7b 0a  iLookAhead>0 ){.
319c4 23 69 66 64 65 66 20 59 59 46 41 4c 4c 42 41 43  #ifdef YYFALLBAC
319c5 4b 0a 20 20 20 20 20 20 59 59 43 4f 44 45 54 59  K.      YYCODETY
319c6 50 45 20 69 46 61 6c 6c 62 61 63 6b 3b 20 20 20  PE iFallback;   
319c7 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c           /* Fall
319c8 62 61 63 6b 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  back token */.  
319c9 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65      if( iLookAhe
319ca 61 64 3c 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c  ad<sizeof(yyFall
319cb 62 61 63 6b 29 2f 73 69 7a 65 6f 66 28 79 79 46  back)/sizeof(yyF
319cc 61 6c 6c 62 61 63 6b 5b 30 5d 29 0a 20 20 20 20  allback[0]).    
319cd 20 20 20 20 20 20 20 20 20 26 26 20 28 69 46 61           && (iFa
319ce 6c 6c 62 61 63 6b 20 3d 20 79 79 46 61 6c 6c 62  llback = yyFallb
319cf 61 63 6b 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 29  ack[iLookAhead])
319d0 21 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e  !=0 ){.#ifndef N
319d1 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 69 66  DEBUG.        if
319d2 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b  ( yyTraceFILE ){
319d3 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e  .          fprin
319d4 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20  tf(yyTraceFILE, 
319d5 22 25 73 46 41 4c 4c 42 41 43 4b 20 25 73 20 3d  "%sFALLBACK %s =
319d6 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20  > %s\n",.       
319d7 20 20 20 20 20 20 79 79 54 72 61 63 65 50 72 6f        yyTracePro
319d8 6d 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65  mpt, yyTokenName
319d9 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79 79  [iLookAhead], yy
319da 54 6f 6b 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c 62  TokenName[iFallb
319db 61 63 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d  ack]);.        }
319dc 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
319dd 72 65 74 75 72 6e 20 79 79 5f 66 69 6e 64 5f 73  return yy_find_s
319de 68 69 66 74 5f 61 63 74 69 6f 6e 28 70 50 61 72  hift_action(pPar
319df 73 65 72 2c 20 69 46 61 6c 6c 62 61 63 6b 29 3b  ser, iFallback);
319e0 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
319e1 23 69 66 64 65 66 20 59 59 57 49 4c 44 43 41 52  #ifdef YYWILDCAR
319e2 44 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  D.      {.      
319e3 20 20 69 6e 74 20 6a 20 3d 20 69 20 2d 20 69 4c    int j = i - iL
319e4 6f 6f 6b 41 68 65 61 64 20 2b 20 59 59 57 49 4c  ookAhead + YYWIL
319e5 44 43 41 52 44 3b 0a 20 20 20 20 20 20 20 20 69  DCARD;.        i
319e6 66 28 20 0a 23 69 66 20 59 59 5f 53 48 49 46 54  f( .#if YY_SHIFT
319e7 5f 4d 49 4e 2b 59 59 57 49 4c 44 43 41 52 44 3c  _MIN+YYWILDCARD<
319e8 30 0a 20 20 20 20 20 20 20 20 20 20 6a 3e 3d 30  0.          j>=0
319e9 20 26 26 0a 23 65 6e 64 69 66 0a 23 69 66 20 59   &&.#endif.#if Y
319ea 59 5f 53 48 49 46 54 5f 4d 41 58 2b 59 59 57 49  Y_SHIFT_MAX+YYWI
319eb 4c 44 43 41 52 44 3e 3d 59 59 5f 41 43 54 54 41  LDCARD>=YY_ACTTA
319ec 42 5f 43 4f 55 4e 54 0a 20 20 20 20 20 20 20 20  B_COUNT.        
319ed 20 20 6a 3c 59 59 5f 41 43 54 54 41 42 5f 43 4f    j<YY_ACTTAB_CO
319ee 55 4e 54 20 26 26 0a 23 65 6e 64 69 66 0a 20 20  UNT &&.#endif.  
319ef 20 20 20 20 20 20 20 20 79 79 5f 6c 6f 6f 6b 61          yy_looka
319f0 68 65 61 64 5b 6a 5d 3d 3d 59 59 57 49 4c 44 43  head[j]==YYWILDC
319f1 41 52 44 0a 20 20 20 20 20 20 20 20 29 7b 0a 23  ARD.        ){.#
319f2 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
319f3 20 20 20 20 20 20 20 20 69 66 28 20 79 79 54 72          if( yyTr
319f4 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20  aceFILE ){.     
319f5 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79         fprintf(y
319f6 79 54 72 61 63 65 46 49 4c 45 2c 20 22 25 73 57  yTraceFILE, "%sW
319f7 49 4c 44 43 41 52 44 20 25 73 20 3d 3e 20 25 73  ILDCARD %s => %s
319f8 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
319f9 20 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70      yyTracePromp
319fa 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69  t, yyTokenName[i
319fb 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f  LookAhead], yyTo
319fc 6b 65 6e 4e 61 6d 65 5b 59 59 57 49 4c 44 43 41  kenName[YYWILDCA
319fd 52 44 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  RD]);.          
319fe 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  }.#endif /* NDEB
319ff 55 47 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  UG */.          
31a00 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e  return yy_action
31a01 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  [j];.        }. 
31a02 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a       }.#endif /*
31a03 20 59 59 57 49 4c 44 43 41 52 44 20 2a 2f 0a 20   YYWILDCARD */. 
31a04 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
31a05 79 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65  yy_default[state
31a06 6e 6f 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  no];.  }else{.  
31a07 20 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69    return yy_acti
31a08 6f 6e 5b 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  on[i];.  }.}../*
31a09 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 61 70 70  .** Find the app
31a0a 72 6f 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20  ropriate action 
31a0b 66 6f 72 20 61 20 70 61 72 73 65 72 20 67 69 76  for a parser giv
31a0c 65 6e 20 74 68 65 20 6e 6f 6e 2d 74 65 72 6d 69  en the non-termi
31a0d 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61  nal.** look-ahea
31a0e 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65  d token iLookAhe
31a0f 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ad..**.** If the
31a10 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
31a11 6e 20 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74  n is YYNOCODE, t
31a12 68 65 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65  hen check to see
31a13 20 69 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69   if the action i
31a14 73 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74  s.** independent
31a15 20 6f 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65   of the look-ahe
31a16 61 64 2e 20 20 49 66 20 69 74 20 69 73 2c 20 72  ad.  If it is, r
31a17 65 74 75 72 6e 20 74 68 65 20 61 63 74 69 6f 6e  eturn the action
31a18 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72  , otherwise.** r
31a19 65 74 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49  eturn YY_NO_ACTI
31a1a 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ON..*/.static in
31a1b 74 20 79 79 5f 66 69 6e 64 5f 72 65 64 75 63 65  t yy_find_reduce
31a1c 5f 61 63 74 69 6f 6e 28 0a 20 20 69 6e 74 20 73  _action(.  int s
31a1d 74 61 74 65 6e 6f 2c 20 20 20 20 20 20 20 20 20  tateno,         
31a1e 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
31a1f 73 74 61 74 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  state number */.
31a20 20 20 59 59 43 4f 44 45 54 59 50 45 20 69 4c 6f    YYCODETYPE iLo
31a21 6f 6b 41 68 65 61 64 20 20 20 20 20 2f 2a 20 54  okAhead     /* T
31a22 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f  he look-ahead to
31a23 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ken */.){.  int 
31a24 69 3b 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f  i;.#ifdef YYERRO
31a25 52 53 59 4d 42 4f 4c 0a 20 20 69 66 28 20 73 74  RSYMBOL.  if( st
31a26 61 74 65 6e 6f 3e 59 59 5f 52 45 44 55 43 45 5f  ateno>YY_REDUCE_
31a27 43 4f 55 4e 54 20 29 7b 0a 20 20 20 20 72 65 74  COUNT ){.    ret
31a28 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b 73  urn yy_default[s
31a29 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 23 65 6c  tateno];.  }.#el
31a2a 73 65 0a 20 20 61 73 73 65 72 74 28 20 73 74 61  se.  assert( sta
31a2b 74 65 6e 6f 3c 3d 59 59 5f 52 45 44 55 43 45 5f  teno<=YY_REDUCE_
31a2c 43 4f 55 4e 54 20 29 3b 0a 23 65 6e 64 69 66 0a  COUNT );.#endif.
31a2d 20 20 69 20 3d 20 79 79 5f 72 65 64 75 63 65 5f    i = yy_reduce_
31a2e 6f 66 73 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20  ofst[stateno];. 
31a2f 20 61 73 73 65 72 74 28 20 69 21 3d 59 59 5f 52   assert( i!=YY_R
31a30 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54 20 29  EDUCE_USE_DFLT )
31a31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 6f  ;.  assert( iLoo
31a32 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43 4f 44 45  kAhead!=YYNOCODE
31a33 20 29 3b 0a 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b   );.  i += iLook
31a34 41 68 65 61 64 3b 0a 23 69 66 64 65 66 20 59 59  Ahead;.#ifdef YY
31a35 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 66  ERRORSYMBOL.  if
31a36 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59 59 5f 41  ( i<0 || i>=YY_A
31a37 43 54 54 41 42 5f 43 4f 55 4e 54 20 7c 7c 20 79  CTTAB_COUNT || y
31a38 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d  y_lookahead[i]!=
31a39 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20  iLookAhead ){.  
31a3a 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61    return yy_defa
31a3b 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20  ult[stateno];.  
31a3c 7d 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74  }.#else.  assert
31a3d 28 20 69 3e 3d 30 20 26 26 20 69 3c 59 59 5f 41  ( i>=0 && i<YY_A
31a3e 43 54 54 41 42 5f 43 4f 55 4e 54 20 29 3b 0a 20  CTTAB_COUNT );. 
31a3f 20 61 73 73 65 72 74 28 20 79 79 5f 6c 6f 6f 6b   assert( yy_look
31a40 61 68 65 61 64 5b 69 5d 3d 3d 69 4c 6f 6f 6b 41  ahead[i]==iLookA
31a41 68 65 61 64 20 29 3b 0a 23 65 6e 64 69 66 0a 20  head );.#endif. 
31a42 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f   return yy_actio
31a43 6e 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n[i];.}../*.** T
31a44 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
31a45 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69  tine is called i
31a46 66 20 74 68 65 20 73 74 61 63 6b 20 6f 76 65 72  f the stack over
31a47 66 6c 6f 77 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  flows..*/.static
31a48 20 76 6f 69 64 20 79 79 53 74 61 63 6b 4f 76 65   void yyStackOve
31a49 72 66 6c 6f 77 28 79 79 50 61 72 73 65 72 20 2a  rflow(yyParser *
31a4a 79 79 70 50 61 72 73 65 72 2c 20 59 59 4d 49 4e  yypParser, YYMIN
31a4b 4f 52 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f 72  ORTYPE *yypMinor
31a4c 29 7b 0a 20 20 20 73 71 6c 69 74 65 33 50 61 72  ){.   sqlite3Par
31a4d 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20  serARG_FETCH;.  
31a4e 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
31a4f 78 2d 2d 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  x--;.#ifndef NDE
31a50 42 55 47 0a 20 20 20 69 66 28 20 79 79 54 72 61  BUG.   if( yyTra
31a51 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 66  ceFILE ){.     f
31a52 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
31a53 4c 45 2c 22 25 73 53 74 61 63 6b 20 4f 76 65 72  LE,"%sStack Over
31a54 66 6c 6f 77 21 5c 6e 22 2c 79 79 54 72 61 63 65  flow!\n",yyTrace
31a55 50 72 6f 6d 70 74 29 3b 0a 20 20 20 7d 0a 23 65  Prompt);.   }.#e
31a56 6e 64 69 66 0a 20 20 20 77 68 69 6c 65 28 20 79  ndif.   while( y
31a57 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e  ypParser->yyidx>
31a58 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73  =0 ) yy_pop_pars
31a59 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73  er_stack(yypPars
31a5a 65 72 29 3b 0a 20 20 20 2f 2a 20 48 65 72 65 20  er);.   /* Here 
31a5b 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74 65 64  code is inserted
31a5c 20 77 68 69 63 68 20 77 69 6c 6c 20 65 78 65 63   which will exec
31a5d 75 74 65 20 69 66 20 74 68 65 20 70 61 72 73 65  ute if the parse
31a5e 72 0a 20 20 20 2a 2a 20 73 74 61 63 6b 20 65 76  r.   ** stack ev
31a5f 65 72 79 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f  ery overflows */
31a60 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
31a61 45 54 45 52 28 79 79 70 4d 69 6e 6f 72 29 3b 20  ETER(yypMinor); 
31a62 2f 2a 20 53 69 6c 65 6e 63 65 20 73 6f 6d 65 20  /* Silence some 
31a63 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67  compiler warning
31a64 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72  s */.  sqlite3Er
31a65 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
31a66 70 61 72 73 65 72 20 73 74 61 63 6b 20 6f 76 65  parser stack ove
31a67 72 66 6c 6f 77 22 29 3b 0a 20 20 70 50 61 72 73  rflow");.  pPars
31a68 65 2d 3e 70 61 72 73 65 45 72 72 6f 72 20 3d 20  e->parseError = 
31a69 31 3b 0a 20 20 20 73 71 6c 69 74 65 33 50 61 72  1;.   sqlite3Par
31a6a 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a  serARG_STORE; /*
31a6b 20 53 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e   Suppress warnin
31a6c 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25  g about unused %
31a6d 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76  extra_argument v
31a6e 61 72 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ar */.}../*.** P
31a6f 65 72 66 6f 72 6d 20 61 20 73 68 69 66 74 20 61  erform a shift a
31a70 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
31a71 20 76 6f 69 64 20 79 79 5f 73 68 69 66 74 28 0a   void yy_shift(.
31a72 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50    yyParser *yypP
31a73 61 72 73 65 72 2c 20 20 20 20 20 20 20 20 20 20  arser,          
31a74 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 74 6f  /* The parser to
31a75 20 62 65 20 73 68 69 66 74 65 64 20 2a 2f 0a 20   be shifted */. 
31a76 20 69 6e 74 20 79 79 4e 65 77 53 74 61 74 65 2c   int yyNewState,
31a77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
31a78 2a 20 54 68 65 20 6e 65 77 20 73 74 61 74 65 20  * The new state 
31a79 74 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20  to shift in */. 
31a7a 20 69 6e 74 20 79 79 4d 61 6a 6f 72 2c 20 20 20   int yyMajor,   
31a7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
31a7c 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65  * The major toke
31a7d 6e 20 74 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f  n to shift in */
31a7e 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a  .  YYMINORTYPE *
31a7f 79 79 70 4d 69 6e 6f 72 20 20 20 20 20 20 20 20  yypMinor        
31a80 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74   /* Pointer to t
31a81 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 74  he minor token t
31a82 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 29 7b  o shift in */.){
31a83 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  .  yyStackEntry 
31a84 2a 79 79 74 6f 73 3b 0a 20 20 79 79 70 50 61 72  *yytos;.  yypPar
31a85 73 65 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a 23 69  ser->yyidx++;.#i
31a86 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
31a87 54 41 43 4b 44 45 50 54 48 0a 20 20 69 66 28 20  TACKDEPTH.  if( 
31a88 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
31a89 3e 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64  >yypParser->yyid
31a8a 78 4d 61 78 20 29 7b 0a 20 20 20 20 79 79 70 50  xMax ){.    yypP
31a8b 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20  arser->yyidxMax 
31a8c 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  = yypParser->yyi
31a8d 64 78 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23  dx;.  }.#endif.#
31a8e 69 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 3e  if YYSTACKDEPTH>
31a8f 30 20 0a 20 20 69 66 28 20 79 79 70 50 61 72 73  0 .  if( yypPars
31a90 65 72 2d 3e 79 79 69 64 78 3e 3d 59 59 53 54 41  er->yyidx>=YYSTA
31a91 43 4b 44 45 50 54 48 20 29 7b 0a 20 20 20 20 79  CKDEPTH ){.    y
31a92 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79  yStackOverflow(y
31a93 79 70 50 61 72 73 65 72 2c 20 79 79 70 4d 69 6e  ypParser, yypMin
31a94 6f 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  or);.    return;
31a95 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28  .  }.#else.  if(
31a96 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
31a97 78 3e 3d 79 79 70 50 61 72 73 65 72 2d 3e 79 79  x>=yypParser->yy
31a98 73 74 6b 73 7a 20 29 7b 0a 20 20 20 20 79 79 47  stksz ){.    yyG
31a99 72 6f 77 53 74 61 63 6b 28 79 79 70 50 61 72 73  rowStack(yypPars
31a9a 65 72 29 3b 0a 20 20 20 20 69 66 28 20 79 79 70  er);.    if( yyp
31a9b 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79  Parser->yyidx>=y
31a9c 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73  ypParser->yystks
31a9d 7a 20 29 7b 0a 20 20 20 20 20 20 79 79 53 74 61  z ){.      yySta
31a9e 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61  ckOverflow(yypPa
31a9f 72 73 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b  rser, yypMinor);
31aa0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
31aa1 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
31aa2 20 20 79 79 74 6f 73 20 3d 20 26 79 79 70 50 61    yytos = &yypPa
31aa3 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79  rser->yystack[yy
31aa4 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b  pParser->yyidx];
31aa5 0a 20 20 79 79 74 6f 73 2d 3e 73 74 61 74 65 6e  .  yytos->staten
31aa6 6f 20 3d 20 28 59 59 41 43 54 49 4f 4e 54 59 50  o = (YYACTIONTYP
31aa7 45 29 79 79 4e 65 77 53 74 61 74 65 3b 0a 20 20  E)yyNewState;.  
31aa8 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 20 3d 20 28  yytos->major = (
31aa9 59 59 43 4f 44 45 54 59 50 45 29 79 79 4d 61 6a  YYCODETYPE)yyMaj
31aaa 6f 72 3b 0a 20 20 79 79 74 6f 73 2d 3e 6d 69 6e  or;.  yytos->min
31aab 6f 72 20 3d 20 2a 79 79 70 4d 69 6e 6f 72 3b 0a  or = *yypMinor;.
31aac 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
31aad 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
31aae 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79   && yypParser->y
31aaf 79 69 64 78 3e 30 20 29 7b 0a 20 20 20 20 69 6e  yidx>0 ){.    in
31ab0 74 20 69 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  t i;.    fprintf
31ab1 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73  (yyTraceFILE,"%s
31ab2 53 68 69 66 74 20 25 64 5c 6e 22 2c 79 79 54 72  Shift %d\n",yyTr
31ab3 61 63 65 50 72 6f 6d 70 74 2c 79 79 4e 65 77 53  acePrompt,yyNewS
31ab4 74 61 74 65 29 3b 0a 20 20 20 20 66 70 72 69 6e  tate);.    fprin
31ab5 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
31ab6 25 73 53 74 61 63 6b 3a 22 2c 79 79 54 72 61 63  %sStack:",yyTrac
31ab7 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20 66 6f  ePrompt);.    fo
31ab8 72 28 69 3d 31 3b 20 69 3c 3d 79 79 70 50 61 72  r(i=1; i<=yypPar
31ab9 73 65 72 2d 3e 79 79 69 64 78 3b 20 69 2b 2b 29  ser->yyidx; i++)
31aba 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79  .      fprintf(y
31abb 79 54 72 61 63 65 46 49 4c 45 2c 22 20 25 73 22  yTraceFILE," %s"
31abc 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 70  ,yyTokenName[yyp
31abd 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
31abe 69 5d 2e 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20 20  i].major]);.    
31abf 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
31ac0 49 4c 45 2c 22 5c 6e 22 29 3b 0a 20 20 7d 0a 23  ILE,"\n");.  }.#
31ac1 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 54 68 65 20  endif.}../* The 
31ac2 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20  following table 
31ac3 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
31ac4 74 69 6f 6e 20 61 62 6f 75 74 20 65 76 65 72 79  tion about every
31ac5 20 72 75 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73   rule that.** is
31ac6 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 68 65   used during the
31ac7 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74   reduce..*/.stat
31ac8 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
31ac9 7b 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 6c  {.  YYCODETYPE l
31aca 68 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  hs;         /* S
31acb 79 6d 62 6f 6c 20 6f 6e 20 74 68 65 20 6c 65 66  ymbol on the lef
31acc 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74  t-hand side of t
31acd 68 65 20 72 75 6c 65 20 2a 2f 0a 20 20 75 6e 73  he rule */.  uns
31ace 69 67 6e 65 64 20 63 68 61 72 20 6e 72 68 73 3b  igned char nrhs;
31acf 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
31ad0 66 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  f right-hand sid
31ad1 65 20 73 79 6d 62 6f 6c 73 20 69 6e 20 74 68 65  e symbols in the
31ad2 20 72 75 6c 65 20 2a 2f 0a 7d 20 79 79 52 75 6c   rule */.} yyRul
31ad3 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20  eInfo[] = {.  { 
31ad4 31 34 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  142, 1 },.  { 14
31ad5 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  3, 2 },.  { 143,
31ad6 20 31 20 7d 2c 0a 20 20 7b 20 31 34 34 2c 20 31   1 },.  { 144, 1
31ad7 20 7d 2c 0a 20 20 7b 20 31 34 34 2c 20 33 20 7d   },.  { 144, 3 }
31ad8 2c 0a 20 20 7b 20 31 34 35 2c 20 30 20 7d 2c 0a  ,.  { 145, 0 },.
31ad9 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20    { 145, 1 },.  
31ada 7b 20 31 34 35 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 145, 3 },.  { 
31adb 31 34 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  146, 1 },.  { 14
31adc 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 39 2c  7, 3 },.  { 149,
31add 20 30 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 31   0 },.  { 149, 1
31ade 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 32 20 7d   },.  { 149, 2 }
31adf 2c 0a 20 20 7b 20 31 34 38 2c 20 30 20 7d 2c 0a  ,.  { 148, 0 },.
31ae0 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20    { 148, 1 },.  
31ae1 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 148, 1 },.  { 
31ae2 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  148, 1 },.  { 14
31ae3 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 2 },.  { 147,
31ae4 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32   2 },.  { 147, 2
31ae5 20 7d 2c 0a 20 20 7b 20 31 35 31 2c 20 31 20 7d   },.  { 151, 1 }
31ae6 2c 0a 20 20 7b 20 31 35 31 2c 20 30 20 7d 2c 0a  ,.  { 151, 0 },.
31ae7 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20    { 147, 2 },.  
31ae8 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 147, 3 },.  { 
31ae9 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34  147, 5 },.  { 14
31aea 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 32 2c  7, 2 },.  { 152,
31aeb 20 36 20 7d 2c 0a 20 20 7b 20 31 35 34 2c 20 31   6 },.  { 154, 1
31aec 20 7d 2c 0a 20 20 7b 20 31 35 36 2c 20 30 20 7d   },.  { 156, 0 }
31aed 2c 0a 20 20 7b 20 31 35 36 2c 20 33 20 7d 2c 0a  ,.  { 156, 3 },.
31aee 20 20 7b 20 31 35 35 2c 20 31 20 7d 2c 0a 20 20    { 155, 1 },.  
31aef 7b 20 31 35 35 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 155, 0 },.  { 
31af0 31 35 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 35  153, 4 },.  { 15
31af1 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 38 2c  3, 2 },.  { 158,
31af2 20 33 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 31   3 },.  { 158, 1
31af3 20 7d 2c 0a 20 20 7b 20 31 36 31 2c 20 33 20 7d   },.  { 161, 3 }
31af4 2c 0a 20 20 7b 20 31 36 32 2c 20 31 20 7d 2c 0a  ,.  { 162, 1 },.
31af5 20 20 7b 20 31 36 35 2c 20 31 20 7d 2c 0a 20 20    { 165, 1 },.  
31af6 7b 20 31 36 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 165, 1 },.  { 
31af7 31 36 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35  166, 1 },.  { 15
31af8 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c  0, 1 },.  { 150,
31af9 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31   1 },.  { 150, 1
31afa 20 7d 2c 0a 20 20 7b 20 31 36 33 2c 20 30 20 7d   },.  { 163, 0 }
31afb 2c 0a 20 20 7b 20 31 36 33 2c 20 31 20 7d 2c 0a  ,.  { 163, 1 },.
31afc 20 20 7b 20 31 36 37 2c 20 31 20 7d 2c 0a 20 20    { 167, 1 },.  
31afd 7b 20 31 36 37 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 167, 4 },.  { 
31afe 31 36 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 36  167, 6 },.  { 16
31aff 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 38 2c  8, 1 },.  { 168,
31b00 20 32 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31   2 },.  { 169, 1
31b01 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d   },.  { 169, 1 }
31b02 2c 0a 20 20 7b 20 31 36 34 2c 20 32 20 7d 2c 0a  ,.  { 164, 2 },.
31b03 20 20 7b 20 31 36 34 2c 20 30 20 7d 2c 0a 20 20    { 164, 0 },.  
31b04 7b 20 31 37 32 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 172, 3 },.  { 
31b05 31 37 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  172, 1 },.  { 17
31b06 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  3, 2 },.  { 173,
31b07 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 33   4 },.  { 173, 3
31b08 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d   },.  { 173, 3 }
31b09 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a  ,.  { 173, 2 },.
31b0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20    { 173, 2 },.  
31b0b 7b 20 31 37 33 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 173, 3 },.  { 
31b0c 31 37 33 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37  173, 5 },.  { 17
31b0d 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  3, 2 },.  { 173,
31b0e 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34   4 },.  { 173, 4
31b0f 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 31 20 7d   },.  { 173, 1 }
31b10 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a  ,.  { 173, 2 },.
31b11 20 20 7b 20 31 37 38 2c 20 30 20 7d 2c 0a 20 20    { 178, 0 },.  
31b12 7b 20 31 37 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 178, 1 },.  { 
31b13 31 38 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38  180, 0 },.  { 18
31b14 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 32 2c  0, 2 },.  { 182,
31b15 20 32 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 33   2 },.  { 182, 3
31b16 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d   },.  { 182, 3 }
31b17 2c 0a 20 20 7b 20 31 38 33 2c 20 32 20 7d 2c 0a  ,.  { 183, 2 },.
31b18 20 20 7b 20 31 38 33 2c 20 32 20 7d 2c 0a 20 20    { 183, 2 },.  
31b19 7b 20 31 38 33 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 183, 1 },.  { 
31b1a 31 38 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38  183, 1 },.  { 18
31b1b 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 31 2c  3, 2 },.  { 181,
31b1c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 32   3 },.  { 181, 2
31b1d 20 7d 2c 0a 20 20 7b 20 31 38 34 2c 20 30 20 7d   },.  { 184, 0 }
31b1e 2c 0a 20 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a  ,.  { 184, 2 },.
31b1f 20 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20    { 184, 2 },.  
31b20 7b 20 31 35 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 159, 0 },.  { 
31b21 31 35 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38  159, 2 },.  { 18
31b22 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 35 2c  5, 3 },.  { 185,
31b23 20 32 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 31   2 },.  { 185, 1
31b24 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 32 20 7d   },.  { 186, 2 }
31b25 2c 0a 20 20 7b 20 31 38 36 2c 20 37 20 7d 2c 0a  ,.  { 186, 7 },.
31b26 20 20 7b 20 31 38 36 2c 20 35 20 7d 2c 0a 20 20    { 186, 5 },.  
31b27 7b 20 31 38 36 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 186, 5 },.  { 
31b28 31 38 36 2c 20 31 30 20 7d 2c 0a 20 20 7b 20 31  186, 10 },.  { 1
31b29 38 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 38  88, 0 },.  { 188
31b2a 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20  , 1 },.  { 176, 
31b2b 30 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 33 20  0 },.  { 176, 3 
31b2c 7d 2c 0a 20 20 7b 20 31 38 39 2c 20 30 20 7d 2c  },.  { 189, 0 },
31b2d 0a 20 20 7b 20 31 38 39 2c 20 32 20 7d 2c 0a 20  .  { 189, 2 },. 
31b2e 20 7b 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 190, 1 },.  {
31b2f 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   190, 1 },.  { 1
31b30 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37  90, 1 },.  { 147
31b31 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20  , 4 },.  { 192, 
31b32 32 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 30 20  2 },.  { 192, 0 
31b33 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 38 20 7d 2c  },.  { 147, 8 },
31b34 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20  .  { 147, 4 },. 
31b35 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b   { 147, 1 },.  {
31b36 20 31 36 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   160, 1 },.  { 1
31b37 36 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39 35  60, 3 },.  { 195
31b38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20  , 1 },.  { 195, 
31b39 32 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 31 20  2 },.  { 195, 1 
31b3a 7d 2c 0a 20 20 7b 20 31 39 34 2c 20 39 20 7d 2c  },.  { 194, 9 },
31b3b 0a 20 20 7b 20 31 39 36 2c 20 31 20 7d 2c 0a 20  .  { 196, 1 },. 
31b3c 20 7b 20 31 39 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 196, 1 },.  {
31b3d 20 31 39 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   196, 0 },.  { 2
31b3e 30 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 34  04, 2 },.  { 204
31b3f 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20  , 0 },.  { 197, 
31b40 33 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 32 20  3 },.  { 197, 2 
31b41 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 34 20 7d 2c  },.  { 197, 4 },
31b42 0a 20 20 7b 20 32 30 35 2c 20 32 20 7d 2c 0a 20  .  { 205, 2 },. 
31b43 20 7b 20 32 30 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 205, 1 },.  {
31b44 20 32 30 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   205, 0 },.  { 1
31b45 39 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38  98, 0 },.  { 198
31b46 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 37 2c 20  , 2 },.  { 207, 
31b47 32 20 7d 2c 0a 20 20 7b 20 32 30 37 2c 20 30 20  2 },.  { 207, 0 
31b48 7d 2c 0a 20 20 7b 20 32 30 36 2c 20 37 20 7d 2c  },.  { 206, 7 },
31b49 0a 20 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20  .  { 206, 7 },. 
31b4a 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b   { 206, 7 },.  {
31b4b 20 31 35 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   157, 0 },.  { 1
31b4c 35 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 33  57, 2 },.  { 193
31b4d 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20  , 2 },.  { 208, 
31b4e 31 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 32 20  1 },.  { 208, 2 
31b4f 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 33 20 7d 2c  },.  { 208, 3 },
31b50 0a 20 20 7b 20 32 30 38 2c 20 34 20 7d 2c 0a 20  .  { 208, 4 },. 
31b51 20 7b 20 32 31 30 2c 20 32 20 7d 2c 0a 20 20 7b   { 210, 2 },.  {
31b52 20 32 31 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   210, 0 },.  { 2
31b53 30 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 39  09, 0 },.  { 209
31b54 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20  , 3 },.  { 209, 
31b55 32 20 7d 2c 0a 20 20 7b 20 32 31 31 2c 20 34 20  2 },.  { 211, 4 
31b56 7d 2c 0a 20 20 7b 20 32 31 31 2c 20 30 20 7d 2c  },.  { 211, 0 },
31b57 0a 20 20 7b 20 32 30 32 2c 20 30 20 7d 2c 0a 20  .  { 202, 0 },. 
31b58 20 7b 20 32 30 32 2c 20 33 20 7d 2c 0a 20 20 7b   { 202, 3 },.  {
31b59 20 32 31 34 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   214, 4 },.  { 2
31b5a 31 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 35  14, 2 },.  { 215
31b5b 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20  , 1 },.  { 177, 
31b5c 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 31 20  1 },.  { 177, 1 
31b5d 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 30 20 7d 2c  },.  { 177, 0 },
31b5e 0a 20 20 7b 20 32 30 30 2c 20 30 20 7d 2c 0a 20  .  { 200, 0 },. 
31b5f 20 7b 20 32 30 30 2c 20 33 20 7d 2c 0a 20 20 7b   { 200, 3 },.  {
31b60 20 32 30 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   201, 0 },.  { 2
31b61 30 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 33  01, 2 },.  { 203
31b62 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20  , 0 },.  { 203, 
31b63 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 34 20  2 },.  { 203, 4 
31b64 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 34 20 7d 2c  },.  { 203, 4 },
31b65 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20  .  { 147, 5 },. 
31b66 20 7b 20 31 39 39 2c 20 30 20 7d 2c 0a 20 20 7b   { 199, 0 },.  {
31b67 20 31 39 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   199, 2 },.  { 1
31b68 34 37 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 31 37  47, 7 },.  { 217
31b69 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20  , 5 },.  { 217, 
31b6a 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 38 20  3 },.  { 147, 8 
31b6b 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c  },.  { 147, 5 },
31b6c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20  .  { 147, 6 },. 
31b6d 20 7b 20 32 31 38 2c 20 32 20 7d 2c 0a 20 20 7b   { 218, 2 },.  {
31b6e 20 32 31 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   218, 1 },.  { 2
31b6f 32 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32 30  20, 3 },.  { 220
31b70 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20  , 1 },.  { 219, 
31b71 30 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 33 20  0 },.  { 219, 3 
31b72 7d 2c 0a 20 20 7b 20 32 31 33 2c 20 33 20 7d 2c  },.  { 213, 3 },
31b73 0a 20 20 7b 20 32 31 33 2c 20 31 20 7d 2c 0a 20  .  { 213, 1 },. 
31b74 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 175, 1 },.  {
31b75 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
31b76 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35  74, 1 },.  { 175
31b77 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 1 },.  { 175, 
31b78 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20  1 },.  { 175, 3 
31b79 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c  },.  { 175, 5 },
31b7a 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20  .  { 174, 1 },. 
31b7b 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 174, 1 },.  {
31b7c 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   175, 1 },.  { 1
31b7d 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 1 },.  { 175
31b7e 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 3 },.  { 175, 
31b7f 36 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20  6 },.  { 175, 5 
31b80 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c  },.  { 175, 4 },
31b81 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20  .  { 174, 1 },. 
31b82 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
31b83 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
31b84 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
31b85 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 3 },.  { 175, 
31b86 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20  3 },.  { 175, 3 
31b87 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
31b88 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20  .  { 175, 3 },. 
31b89 20 7b 20 32 32 32 2c 20 31 20 7d 2c 0a 20 20 7b   { 222, 1 },.  {
31b8a 20 32 32 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   222, 2 },.  { 2
31b8b 32 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 32  22, 1 },.  { 222
31b8c 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 33 2c 20  , 2 },.  { 223, 
31b8d 32 20 7d 2c 0a 20 20 7b 20 32 32 33 2c 20 30 20  2 },.  { 223, 0 
31b8e 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c  },.  { 175, 4 },
31b8f 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20  .  { 175, 2 },. 
31b90 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
31b91 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
31b92 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 4 },.  { 175
31b93 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 2 },.  { 175, 
31b94 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20  2 },.  { 175, 2 
31b95 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c  },.  { 175, 2 },
31b96 0a 20 20 7b 20 32 32 34 2c 20 31 20 7d 2c 0a 20  .  { 224, 1 },. 
31b97 20 7b 20 32 32 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 224, 2 },.  {
31b98 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 32   175, 5 },.  { 2
31b99 32 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 35  25, 1 },.  { 225
31b9a 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 2 },.  { 175, 
31b9b 35 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20  5 },.  { 175, 3 
31b9c 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c  },.  { 175, 5 },
31b9d 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20  .  { 175, 4 },. 
31b9e 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b   { 175, 4 },.  {
31b9f 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 32   175, 5 },.  { 2
31ba0 32 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 37  27, 5 },.  { 227
31ba1 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 32 38 2c 20  , 4 },.  { 228, 
31ba2 32 20 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 30 20  2 },.  { 228, 0 
31ba3 7d 2c 0a 20 20 7b 20 32 32 36 2c 20 31 20 7d 2c  },.  { 226, 1 },
31ba4 0a 20 20 7b 20 32 32 36 2c 20 30 20 7d 2c 0a 20  .  { 226, 0 },. 
31ba5 20 7b 20 32 32 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 221, 1 },.  {
31ba6 20 32 32 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   221, 0 },.  { 2
31ba7 31 36 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 36  16, 3 },.  { 216
31ba8 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
31ba9 31 31 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20 31  11 },.  { 229, 1
31baa 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20 30 20 7d   },.  { 229, 0 }
31bab 2c 0a 20 20 7b 20 31 37 39 2c 20 30 20 7d 2c 0a  ,.  { 179, 0 },.
31bac 20 20 7b 20 31 37 39 2c 20 33 20 7d 2c 0a 20 20    { 179, 3 },.  
31bad 7b 20 31 38 37 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 187, 5 },.  { 
31bae 31 38 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33  187, 3 },.  { 23
31baf 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 30 2c  0, 0 },.  { 230,
31bb0 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34   2 },.  { 147, 4
31bb1 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d   },.  { 147, 1 }
31bb2 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a  ,.  { 147, 2 },.
31bb3 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20    { 147, 3 },.  
31bb4 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 147, 5 },.  { 
31bb5 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34  147, 6 },.  { 14
31bb6 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 5 },.  { 147,
31bb7 20 36 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31   6 },.  { 231, 1
31bb8 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d   },.  { 231, 1 }
31bb9 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a  ,.  { 231, 1 },.
31bba 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20    { 231, 1 },.  
31bbb 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 231, 1 },.  { 
31bbc 31 37 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  170, 2 },.  { 17
31bbd 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 33 2c  1, 2 },.  { 233,
31bbe 20 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 31   1 },.  { 232, 1
31bbf 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 30 20 7d   },.  { 232, 0 }
31bc0 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a  ,.  { 147, 5 },.
31bc1 20 20 7b 20 32 33 34 2c 20 31 31 20 7d 2c 0a 20    { 234, 11 },. 
31bc2 20 7b 20 32 33 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 236, 1 },.  {
31bc3 20 32 33 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   236, 1 },.  { 2
31bc4 33 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 36  36, 2 },.  { 236
31bc5 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20  , 0 },.  { 237, 
31bc6 31 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 31 20  1 },.  { 237, 1 
31bc7 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 33 20 7d 2c  },.  { 237, 3 },
31bc8 0a 20 20 7b 20 32 33 38 2c 20 30 20 7d 2c 0a 20  .  { 238, 0 },. 
31bc9 20 7b 20 32 33 38 2c 20 33 20 7d 2c 0a 20 20 7b   { 238, 3 },.  {
31bca 20 32 33 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   239, 0 },.  { 2
31bcb 33 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 35  39, 2 },.  { 235
31bcc 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20  , 3 },.  { 235, 
31bcd 32 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 31 20  2 },.  { 241, 1 
31bce 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 33 20 7d 2c  },.  { 241, 3 },
31bcf 0a 20 20 7b 20 32 34 32 2c 20 30 20 7d 2c 0a 20  .  { 242, 0 },. 
31bd0 20 7b 20 32 34 32 2c 20 33 20 7d 2c 0a 20 20 7b   { 242, 3 },.  {
31bd1 20 32 34 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   242, 2 },.  { 2
31bd2 34 30 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 34 30  40, 7 },.  { 240
31bd3 2c 20 38 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20  , 8 },.  { 240, 
31bd4 35 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 35 20  5 },.  { 240, 5 
31bd5 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 31 20 7d 2c  },.  { 240, 1 },
31bd6 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20  .  { 175, 4 },. 
31bd7 20 7b 20 31 37 35 2c 20 36 20 7d 2c 0a 20 20 7b   { 175, 6 },.  {
31bd8 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   191, 1 },.  { 1
31bd9 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 31  91, 1 },.  { 191
31bda 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
31bdb 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20  4 },.  { 147, 6 
31bdc 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c  },.  { 147, 3 },
31bdd 0a 20 20 7b 20 32 34 34 2c 20 30 20 7d 2c 0a 20  .  { 244, 0 },. 
31bde 20 7b 20 32 34 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 244, 2 },.  {
31bdf 20 32 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   243, 1 },.  { 2
31be0 34 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 37  43, 0 },.  { 147
31be1 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
31be2 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20  3 },.  { 147, 1 
31be3 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c  },.  { 147, 3 },
31be4 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20  .  { 147, 6 },. 
31be5 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b   { 147, 6 },.  {
31be6 20 32 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   245, 1 },.  { 2
31be7 34 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34 36  46, 0 },.  { 246
31be8 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
31be9 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20  1 },.  { 147, 4 
31bea 7d 2c 0a 20 20 7b 20 32 34 37 2c 20 37 20 7d 2c  },.  { 247, 7 },
31beb 0a 20 20 7b 20 32 34 38 2c 20 31 20 7d 2c 0a 20  .  { 248, 1 },. 
31bec 20 7b 20 32 34 38 2c 20 33 20 7d 2c 0a 20 20 7b   { 248, 3 },.  {
31bed 20 32 34 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   249, 0 },.  { 2
31bee 34 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 35 30  49, 2 },.  { 250
31bef 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 35 30 2c 20  , 1 },.  { 250, 
31bf0 33 20 7d 2c 0a 20 20 7b 20 32 35 31 2c 20 31 20  3 },.  { 251, 1 
31bf1 7d 2c 0a 20 20 7b 20 32 35 32 2c 20 30 20 7d 2c  },.  { 252, 0 },
31bf2 0a 20 20 7b 20 32 35 32 2c 20 34 20 7d 2c 0a 20  .  { 252, 4 },. 
31bf3 20 7b 20 32 35 32 2c 20 32 20 7d 2c 0a 7d 3b 0a   { 252, 2 },.};.
31bf4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f  .static void yy_
31bf5 61 63 63 65 70 74 28 79 79 50 61 72 73 65 72 2a  accept(yyParser*
31bf6 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 44  );  /* Forward D
31bf7 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f  eclaration */../
31bf8 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72  *.** Perform a r
31bf9 65 64 75 63 65 20 61 63 74 69 6f 6e 20 61 6e 64  educe action and
31bfa 20 74 68 65 20 73 68 69 66 74 20 74 68 61 74 20   the shift that 
31bfb 6d 75 73 74 20 69 6d 6d 65 64 69 61 74 65 6c 79  must immediately
31bfc 0a 2a 2a 20 66 6f 6c 6c 6f 77 20 74 68 65 20 72  .** follow the r
31bfd 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  educe..*/.static
31bfe 20 76 6f 69 64 20 79 79 5f 72 65 64 75 63 65 28   void yy_reduce(
31bff 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70  .  yyParser *yyp
31c00 50 61 72 73 65 72 2c 20 20 20 20 20 20 20 20 20  Parser,         
31c01 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
31c02 0a 20 20 69 6e 74 20 79 79 72 75 6c 65 6e 6f 20  .  int yyruleno 
31c03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c04 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65  /* Number of the
31c05 20 72 75 6c 65 20 62 79 20 77 68 69 63 68 20 74   rule by which t
31c06 6f 20 72 65 64 75 63 65 20 2a 2f 0a 29 7b 0a 20  o reduce */.){. 
31c07 20 69 6e 74 20 79 79 67 6f 74 6f 3b 20 20 20 20   int yygoto;    
31c08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c09 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 73 74 61   /* The next sta
31c0a 74 65 20 2a 2f 0a 20 20 69 6e 74 20 79 79 61 63  te */.  int yyac
31c0b 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
31c0c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
31c0d 65 78 74 20 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ext action */.  
31c0e 59 59 4d 49 4e 4f 52 54 59 50 45 20 79 79 67 6f  YYMINORTYPE yygo
31c0f 74 6f 6d 69 6e 6f 72 3b 20 20 20 20 20 20 20 20  tominor;        
31c10 2f 2a 20 54 68 65 20 4c 48 53 20 6f 66 20 74 68  /* The LHS of th
31c11 65 20 72 75 6c 65 20 72 65 64 75 63 65 64 20 2a  e rule reduced *
31c12 2f 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79  /.  yyStackEntry
31c13 20 2a 79 79 6d 73 70 3b 20 20 20 20 20 20 20 20   *yymsp;        
31c14 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f      /* The top o
31c15 66 20 74 68 65 20 70 61 72 73 65 72 27 73 20 73  f the parser's s
31c16 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 79 79  tack */.  int yy
31c17 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  size;           
31c18 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f            /* Amo
31c19 75 6e 74 20 74 6f 20 70 6f 70 20 74 68 65 20 73  unt to pop the s
31c1a 74 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65  tack */.  sqlite
31c1b 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
31c1c 3b 0a 20 20 79 79 6d 73 70 20 3d 20 26 79 79 70  ;.  yymsp = &yyp
31c1d 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
31c1e 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
31c1f 5d 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  ];.#ifndef NDEBU
31c20 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46  G.  if( yyTraceF
31c21 49 4c 45 20 26 26 20 79 79 72 75 6c 65 6e 6f 3e  ILE && yyruleno>
31c22 3d 30 20 0a 20 20 20 20 20 20 20 20 26 26 20 79  =0 .        && y
31c23 79 72 75 6c 65 6e 6f 3c 28 69 6e 74 29 28 73 69  yruleno<(int)(si
31c24 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65 29  zeof(yyRuleName)
31c25 2f 73 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61  /sizeof(yyRuleNa
31c26 6d 65 5b 30 5d 29 29 20 29 7b 0a 20 20 20 20 66  me[0])) ){.    f
31c27 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
31c28 4c 45 2c 20 22 25 73 52 65 64 75 63 65 20 5b 25  LE, "%sReduce [%
31c29 73 5d 2e 5c 6e 22 2c 20 79 79 54 72 61 63 65 50  s].\n", yyTraceP
31c2a 72 6f 6d 70 74 2c 0a 20 20 20 20 20 20 79 79 52  rompt,.      yyR
31c2b 75 6c 65 4e 61 6d 65 5b 79 79 72 75 6c 65 6e 6f  uleName[yyruleno
31c2c 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ]);.  }.#endif /
31c2d 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 20 20 2f  * NDEBUG */..  /
31c2e 2a 20 53 69 6c 65 6e 63 65 20 63 6f 6d 70 6c 61  * Silence compla
31c2f 69 6e 74 73 20 66 72 6f 6d 20 70 75 72 69 66 79  ints from purify
31c30 20 61 62 6f 75 74 20 79 79 67 6f 74 6f 6d 69 6e   about yygotomin
31c31 6f 72 20 62 65 69 6e 67 20 75 6e 69 6e 69 74 69  or being uniniti
31c32 61 6c 69 7a 65 64 0a 20 20 2a 2a 20 69 6e 20 73  alized.  ** in s
31c33 6f 6d 65 20 63 61 73 65 73 20 77 68 65 6e 20 69  ome cases when i
31c34 74 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f  t is copied into
31c35 20 74 68 65 20 73 74 61 63 6b 20 61 66 74 65 72   the stack after
31c36 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20   the following. 
31c37 20 2a 2a 20 73 77 69 74 63 68 2e 20 20 79 79 67   ** switch.  yyg
31c38 6f 74 6f 6d 69 6e 6f 72 20 69 73 20 75 6e 69 6e  otominor is unin
31c39 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 61  itialized when a
31c3a 20 72 75 6c 65 20 72 65 64 75 63 65 73 20 74 68   rule reduces th
31c3b 61 74 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74  at does.  ** not
31c3c 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f   set the value o
31c3d 66 20 69 74 73 20 6c 65 66 74 2d 68 61 6e 64 20  f its left-hand 
31c3e 73 69 64 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c  side nonterminal
31c3f 2e 20 20 4c 65 61 76 69 6e 67 20 74 68 65 0a 20  .  Leaving the. 
31c40 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65   ** value of the
31c41 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 75 6e 69   nonterminal uni
31c42 6e 69 74 69 61 6c 69 7a 65 64 20 69 73 20 75 74  nitialized is ut
31c43 74 65 72 6c 79 20 68 61 72 6d 6c 65 73 73 20 61  terly harmless a
31c44 73 20 6c 6f 6e 67 0a 20 20 2a 2a 20 61 73 20 74  s long.  ** as t
31c45 68 65 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65  he value is neve
31c46 72 20 75 73 65 64 2e 20 20 53 6f 20 72 65 61 6c  r used.  So real
31c47 6c 79 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e  ly the only thin
31c48 67 20 74 68 69 73 20 63 6f 64 65 0a 20 20 2a 2a  g this code.  **
31c49 20 61 63 63 6f 6d 70 6c 69 73 68 65 73 20 69 73   accomplishes is
31c4a 20 74 6f 20 71 75 69 65 74 65 6e 20 70 75 72 69   to quieten puri
31c4b 66 79 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  fy.  .  **.  ** 
31c4c 32 30 30 37 2d 30 31 2d 31 36 3a 20 20 54 68 65  2007-01-16:  The
31c4d 20 77 69 72 65 73 68 61 72 6b 20 70 72 6f 6a 65   wireshark proje
31c4e 63 74 20 28 77 77 77 2e 77 69 72 65 73 68 61 72  ct (www.wireshar
31c4f 6b 2e 6f 72 67 29 20 72 65 70 6f 72 74 73 20 74  k.org) reports t
31c50 68 61 74 0a 20 20 2a 2a 20 77 69 74 68 6f 75 74  hat.  ** without
31c51 20 74 68 69 73 20 63 6f 64 65 2c 20 74 68 65 69   this code, thei
31c52 72 20 70 61 72 73 65 72 20 73 65 67 66 61 75 6c  r parser segfaul
31c53 74 73 2e 20 20 49 27 6d 20 6e 6f 74 20 73 75 72  ts.  I'm not sur
31c54 65 20 77 68 61 74 20 74 68 65 72 65 0a 20 20 2a  e what there.  *
31c55 2a 20 70 61 72 73 65 72 20 69 73 20 64 6f 69 6e  * parser is doin
31c56 67 20 74 6f 20 6d 61 6b 65 20 74 68 69 73 20 68  g to make this h
31c57 61 70 70 65 6e 2e 20 20 54 68 69 73 20 69 73 20  appen.  This is 
31c58 74 68 65 20 73 65 63 6f 6e 64 20 62 75 67 20 72  the second bug r
31c59 65 70 6f 72 74 0a 20 20 2a 2a 20 66 72 6f 6d 20  eport.  ** from 
31c5a 77 69 72 65 73 68 61 72 6b 20 74 68 69 73 20 77  wireshark this w
31c5b 65 65 6b 2e 20 20 43 6c 65 61 72 6c 79 20 74 68  eek.  Clearly th
31c5c 65 79 20 61 72 65 20 73 74 72 65 73 73 69 6e 67  ey are stressing
31c5d 20 4c 65 6d 6f 6e 20 69 6e 20 77 61 79 73 0a 20   Lemon in ways. 
31c5e 20 2a 2a 20 74 68 61 74 20 69 74 20 68 61 73 20   ** that it has 
31c5f 6e 6f 74 20 62 65 65 6e 20 70 72 65 76 69 6f 75  not been previou
31c60 73 6c 79 20 73 74 72 65 73 73 65 64 2e 2e 2e 20  sly stressed... 
31c61 20 28 53 51 4c 69 74 65 20 74 69 63 6b 65 74 20   (SQLite ticket 
31c62 23 32 31 37 32 29 0a 20 20 2a 2f 0a 20 20 2f 2a  #2172).  */.  /*
31c63 6d 65 6d 73 65 74 28 26 79 79 67 6f 74 6f 6d 69  memset(&yygotomi
31c64 6e 6f 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 79  nor, 0, sizeof(y
31c65 79 67 6f 74 6f 6d 69 6e 6f 72 29 29 3b 2a 2f 0a  ygotominor));*/.
31c66 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 3d 20    yygotominor = 
31c67 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 0a 0a 20  yyzerominor;... 
31c68 20 73 77 69 74 63 68 28 20 79 79 72 75 6c 65 6e   switch( yyrulen
31c69 6f 20 29 7b 0a 20 20 2f 2a 20 42 65 67 69 6e 6e  o ){.  /* Beginn
31c6a 69 6e 67 20 68 65 72 65 20 61 72 65 20 74 68 65  ing here are the
31c6b 20 72 65 64 75 63 74 69 6f 6e 20 63 61 73 65 73   reduction cases
31c6c 2e 20 20 41 20 74 79 70 69 63 61 6c 20 65 78 61  .  A typical exa
31c6d 6d 70 6c 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77  mple.  ** follow
31c6e 73 3a 0a 20 20 2a 2a 20 20 20 63 61 73 65 20 30  s:.  **   case 0
31c6f 3a 0a 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c  :.  **  #line <l
31c70 69 6e 65 6e 6f 3e 20 3c 67 72 61 6d 6d 61 72 66  ineno> <grammarf
31c71 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20 20 7b 20  ile>.  **     { 
31c72 2e 2e 2e 20 7d 20 20 20 20 20 20 20 20 20 20 20  ... }           
31c73 2f 2f 20 55 73 65 72 20 73 75 70 70 6c 69 65 64  // User supplied
31c74 20 63 6f 64 65 0a 20 20 2a 2a 20 20 23 6c 69 6e   code.  **  #lin
31c75 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 74 68 69 73  e <lineno> <this
31c76 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20 20 62  file>.  **     b
31c77 72 65 61 6b 3b 0a 20 20 2a 2f 0a 20 20 20 20 20  reak;.  */.     
31c78 20 63 61 73 65 20 35 3a 20 2f 2a 20 65 78 70 6c   case 5: /* expl
31c79 61 69 6e 20 3a 3a 3d 20 2a 2f 0a 7b 20 73 71 6c  ain ::= */.{ sql
31c7a 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 70  ite3BeginParse(p
31c7b 50 61 72 73 65 2c 20 30 29 3b 20 7d 0a 20 20 20  Parse, 0); }.   
31c7c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31c7d 20 20 63 61 73 65 20 36 3a 20 2f 2a 20 65 78 70    case 6: /* exp
31c7e 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e  lain ::= EXPLAIN
31c7f 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65 67   */.{ sqlite3Beg
31c80 69 6e 50 61 72 73 65 28 70 50 61 72 73 65 2c 20  inParse(pParse, 
31c81 31 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  1); }.        br
31c82 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31c83 37 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a  7: /* explain ::
31c84 3d 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20  = EXPLAIN QUERY 
31c85 50 4c 41 4e 20 2a 2f 0a 7b 20 73 71 6c 69 74 65  PLAN */.{ sqlite
31c86 33 42 65 67 69 6e 50 61 72 73 65 28 70 50 61 72  3BeginParse(pPar
31c87 73 65 2c 20 32 29 3b 20 7d 0a 20 20 20 20 20 20  se, 2); }.      
31c88 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31c89 61 73 65 20 38 3a 20 2f 2a 20 63 6d 64 78 20 3a  ase 8: /* cmdx :
31c8a 3a 3d 20 63 6d 64 20 2a 2f 0a 7b 20 73 71 6c 69  := cmd */.{ sqli
31c8b 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28  te3FinishCoding(
31c8c 70 50 61 72 73 65 29 3b 20 7d 0a 20 20 20 20 20  pParse); }.     
31c8d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31c8e 63 61 73 65 20 39 3a 20 2f 2a 20 63 6d 64 20 3a  case 9: /* cmd :
31c8f 3a 3d 20 42 45 47 49 4e 20 74 72 61 6e 73 74 79  := BEGIN transty
31c90 70 65 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a  pe trans_opt */.
31c91 7b 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61  {sqlite3BeginTra
31c92 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73 65 2c  nsaction(pParse,
31c93 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
31c94 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20 20 20  .yy328);}.      
31c95 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31c96 61 73 65 20 31 33 3a 20 2f 2a 20 74 72 61 6e 73  ase 13: /* trans
31c97 74 79 70 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  type ::= */.{yyg
31c98 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d  otominor.yy328 =
31c99 20 54 4b 5f 44 45 46 45 52 52 45 44 3b 7d 0a 20   TK_DEFERRED;}. 
31c9a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31c9b 20 20 20 20 63 61 73 65 20 31 34 3a 20 2f 2a 20      case 14: /* 
31c9c 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44 45  transtype ::= DE
31c9d 46 45 52 52 45 44 20 2a 2f 0a 20 20 20 20 20 20  FERRED */.      
31c9e 63 61 73 65 20 31 35 3a 20 2f 2a 20 74 72 61 6e  case 15: /* tran
31c9f 73 74 79 70 65 20 3a 3a 3d 20 49 4d 4d 45 44 49  stype ::= IMMEDI
31ca0 41 54 45 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ATE */ yytestcas
31ca1 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 29 3b  e(yyruleno==15);
31ca2 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 3a 20  .      case 16: 
31ca3 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d  /* transtype ::=
31ca4 20 45 58 43 4c 55 53 49 56 45 20 2a 2f 20 79 79   EXCLUSIVE */ yy
31ca5 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31ca6 6f 3d 3d 31 36 29 3b 0a 20 20 20 20 20 20 63 61  o==16);.      ca
31ca7 73 65 20 31 31 34 3a 20 2f 2a 20 6d 75 6c 74 69  se 114: /* multi
31ca8 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e  select_op ::= UN
31ca9 49 4f 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ION */ yytestcas
31caa 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 34 29  e(yyruleno==114)
31cab 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 36  ;.      case 116
31cac 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74  : /* multiselect
31cad 5f 6f 70 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49  _op ::= EXCEPT|I
31cae 4e 54 45 52 53 45 43 54 20 2a 2f 20 79 79 74 65  NTERSECT */ yyte
31caf 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31cb0 3d 31 31 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =116);.{yygotomi
31cb1 6e 6f 72 2e 79 79 33 32 38 20 3d 20 79 79 6d 73  nor.yy328 = yyms
31cb2 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 7d 0a 20 20 20  p[0].major;}.   
31cb3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31cb4 20 20 63 61 73 65 20 31 37 3a 20 2f 2a 20 63 6d    case 17: /* cm
31cb5 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72 61  d ::= COMMIT tra
31cb6 6e 73 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20  ns_opt */.      
31cb7 63 61 73 65 20 31 38 3a 20 2f 2a 20 63 6d 64 20  case 18: /* cmd 
31cb8 3a 3a 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70  ::= END trans_op
31cb9 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  t */ yytestcase(
31cba 79 79 72 75 6c 65 6e 6f 3d 3d 31 38 29 3b 0a 7b  yyruleno==18);.{
31cbb 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61  sqlite3CommitTra
31cbc 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73 65 29  nsaction(pParse)
31cbd 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
31cbe 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 3a  ;.      case 19:
31cbf 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c   /* cmd ::= ROLL
31cc0 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20 2a  BACK trans_opt *
31cc1 2f 0a 7b 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61  /.{sqlite3Rollba
31cc2 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  ckTransaction(pP
31cc3 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20  arse);}.        
31cc4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31cc5 65 20 32 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d  e 22: /* cmd ::=
31cc6 20 53 41 56 45 50 4f 49 4e 54 20 6e 6d 20 2a 2f   SAVEPOINT nm */
31cc7 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 61 76 65  .{.  sqlite3Save
31cc8 70 6f 69 6e 74 28 70 50 61 72 73 65 2c 20 53 41  point(pParse, SA
31cc9 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 2c 20 26  VEPOINT_BEGIN, &
31cca 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31ccb 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
31ccc 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31ccd 20 32 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20   23: /* cmd ::= 
31cce 52 45 4c 45 41 53 45 20 73 61 76 65 70 6f 69 6e  RELEASE savepoin
31ccf 74 5f 6f 70 74 20 6e 6d 20 2a 2f 0a 7b 0a 20 20  t_opt nm */.{.  
31cd0 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74  sqlite3Savepoint
31cd1 28 70 50 61 72 73 65 2c 20 53 41 56 45 50 4f 49  (pParse, SAVEPOI
31cd2 4e 54 5f 52 45 4c 45 41 53 45 2c 20 26 79 79 6d  NT_RELEASE, &yym
31cd3 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
31cd4 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
31cd5 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34  k;.      case 24
31cd6 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f 4c  : /* cmd ::= ROL
31cd7 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20  LBACK trans_opt 
31cd8 54 4f 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74  TO savepoint_opt
31cd9 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74   nm */.{.  sqlit
31cda 65 33 53 61 76 65 70 6f 69 6e 74 28 70 50 61 72  e3Savepoint(pPar
31cdb 73 65 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f  se, SAVEPOINT_RO
31cdc 4c 4c 42 41 43 4b 2c 20 26 79 79 6d 73 70 5b 30  LLBACK, &yymsp[0
31cdd 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
31cde 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31cdf 20 20 20 20 20 63 61 73 65 20 32 36 3a 20 2f 2a       case 26: /*
31ce0 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a   create_table ::
31ce1 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20  = createkw temp 
31ce2 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74  TABLE ifnotexist
31ce3 73 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 0a 20  s nm dbnm */.{. 
31ce4 20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61    sqlite3StartTa
31ce5 62 6c 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73  ble(pParse,&yyms
31ce6 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
31ce7 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
31ce8 79 79 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  yy0,yymsp[-4].mi
31ce9 6e 6f 72 2e 79 79 33 32 38 2c 30 2c 30 2c 79 79  nor.yy328,0,0,yy
31cea 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
31ceb 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  328);.}.        
31cec 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31ced 65 20 32 37 3a 20 2f 2a 20 63 72 65 61 74 65 6b  e 27: /* createk
31cee 77 20 3a 3a 3d 20 43 52 45 41 54 45 20 2a 2f 0a  w ::= CREATE */.
31cef 7b 0a 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  {.  pParse->db->
31cf0 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
31cf1 65 64 20 3d 20 30 3b 0a 20 20 79 79 67 6f 74 6f  ed = 0;.  yygoto
31cf2 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73  minor.yy0 = yyms
31cf3 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a  p[0].minor.yy0;.
31cf4 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31cf5 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 3a 20  .      case 28: 
31cf6 2f 2a 20 69 66 6e 6f 74 65 78 69 73 74 73 20 3a  /* ifnotexists :
31cf7 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
31cf8 20 33 31 3a 20 2f 2a 20 74 65 6d 70 20 3a 3a 3d   31: /* temp ::=
31cf9 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31cfa 79 72 75 6c 65 6e 6f 3d 3d 33 31 29 3b 0a 20 20  yruleno==31);.  
31cfb 20 20 20 20 63 61 73 65 20 37 30 3a 20 2f 2a 20      case 70: /* 
31cfc 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 2a 2f 20 79  autoinc ::= */ y
31cfd 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31cfe 6e 6f 3d 3d 37 30 29 3b 0a 20 20 20 20 20 20 63  no==70);.      c
31cff 61 73 65 20 38 32 3a 20 2f 2a 20 64 65 66 65 72  ase 82: /* defer
31d00 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 4e  _subclause ::= N
31d01 4f 54 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e  OT DEFERRABLE in
31d02 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
31d03 5f 6f 70 74 20 2a 2f 20 79 79 74 65 73 74 63 61  _opt */ yytestca
31d04 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 32 29  se(yyruleno==82)
31d05 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 34 3a  ;.      case 84:
31d06 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72 72 65   /* init_deferre
31d07 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a  d_pred_opt ::= *
31d08 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31d09 75 6c 65 6e 6f 3d 3d 38 34 29 3b 0a 20 20 20 20  uleno==84);.    
31d0a 20 20 63 61 73 65 20 38 36 3a 20 2f 2a 20 69 6e    case 86: /* in
31d0b 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
31d0c 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c  _opt ::= INITIAL
31d0d 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 2a 2f 20  LY IMMEDIATE */ 
31d0e 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31d0f 65 6e 6f 3d 3d 38 36 29 3b 0a 20 20 20 20 20 20  eno==86);.      
31d10 63 61 73 65 20 39 37 3a 20 2f 2a 20 64 65 66 65  case 97: /* defe
31d11 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20  r_subclause_opt 
31d12 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
31d13 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 39 37 29 3b  e(yyruleno==97);
31d14 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 38 3a  .      case 108:
31d15 20 2f 2a 20 69 66 65 78 69 73 74 73 20 3a 3a 3d   /* ifexists ::=
31d16 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31d17 79 72 75 6c 65 6e 6f 3d 3d 31 30 38 29 3b 0a 20  yruleno==108);. 
31d18 20 20 20 20 20 63 61 73 65 20 31 31 39 3a 20 2f       case 119: /
31d19 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 41  * distinct ::= A
31d1a 4c 4c 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  LL */ yytestcase
31d1b 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 39 29 3b  (yyruleno==119);
31d1c 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 30 3a  .      case 120:
31d1d 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a 3d   /* distinct ::=
31d1e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31d1f 79 72 75 6c 65 6e 6f 3d 3d 31 32 30 29 3b 0a 20  yruleno==120);. 
31d20 20 20 20 20 20 63 61 73 65 20 32 32 32 3a 20 2f       case 222: /
31d21 2a 20 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d  * between_op ::=
31d22 20 42 45 54 57 45 45 4e 20 2a 2f 20 79 79 74 65   BETWEEN */ yyte
31d23 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31d24 3d 32 32 32 29 3b 0a 20 20 20 20 20 20 63 61 73  =222);.      cas
31d25 65 20 32 32 35 3a 20 2f 2a 20 69 6e 5f 6f 70 20  e 225: /* in_op 
31d26 3a 3a 3d 20 49 4e 20 2a 2f 20 79 79 74 65 73 74  ::= IN */ yytest
31d27 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
31d28 32 35 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  25);.{yygotomino
31d29 72 2e 79 79 33 32 38 20 3d 20 30 3b 7d 0a 20 20  r.yy328 = 0;}.  
31d2a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31d2b 20 20 20 63 61 73 65 20 32 39 3a 20 2f 2a 20 69     case 29: /* i
31d2c 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 49  fnotexists ::= I
31d2d 46 20 4e 4f 54 20 45 58 49 53 54 53 20 2a 2f 0a  F NOT EXISTS */.
31d2e 20 20 20 20 20 20 63 61 73 65 20 33 30 3a 20 2f        case 30: /
31d2f 2a 20 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 20  * temp ::= TEMP 
31d30 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31d31 72 75 6c 65 6e 6f 3d 3d 33 30 29 3b 0a 20 20 20  ruleno==30);.   
31d32 20 20 20 63 61 73 65 20 37 31 3a 20 2f 2a 20 61     case 71: /* a
31d33 75 74 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49  utoinc ::= AUTOI
31d34 4e 43 52 20 2a 2f 20 79 79 74 65 73 74 63 61 73  NCR */ yytestcas
31d35 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 37 31 29 3b  e(yyruleno==71);
31d36 0a 20 20 20 20 20 20 63 61 73 65 20 38 35 3a 20  .      case 85: 
31d37 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64  /* init_deferred
31d38 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e  _pred_opt ::= IN
31d39 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44  ITIALLY DEFERRED
31d3a 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31d3b 79 72 75 6c 65 6e 6f 3d 3d 38 35 29 3b 0a 20 20  yruleno==85);.  
31d3c 20 20 20 20 63 61 73 65 20 31 30 37 3a 20 2f 2a      case 107: /*
31d3d 20 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 49 46   ifexists ::= IF
31d3e 20 45 58 49 53 54 53 20 2a 2f 20 79 79 74 65 73   EXISTS */ yytes
31d3f 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31d40 31 30 37 29 3b 0a 20 20 20 20 20 20 63 61 73 65  107);.      case
31d41 20 31 31 38 3a 20 2f 2a 20 64 69 73 74 69 6e 63   118: /* distinc
31d42 74 20 3a 3a 3d 20 44 49 53 54 49 4e 43 54 20 2a  t ::= DISTINCT *
31d43 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31d44 75 6c 65 6e 6f 3d 3d 31 31 38 29 3b 0a 20 20 20  uleno==118);.   
31d45 20 20 20 63 61 73 65 20 32 32 33 3a 20 2f 2a 20     case 223: /* 
31d46 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e  between_op ::= N
31d47 4f 54 20 42 45 54 57 45 45 4e 20 2a 2f 20 79 79  OT BETWEEN */ yy
31d48 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31d49 6f 3d 3d 32 32 33 29 3b 0a 20 20 20 20 20 20 63  o==223);.      c
31d4a 61 73 65 20 32 32 36 3a 20 2f 2a 20 69 6e 5f 6f  ase 226: /* in_o
31d4b 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e 20 2a 2f 20  p ::= NOT IN */ 
31d4c 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31d4d 65 6e 6f 3d 3d 32 32 36 29 3b 0a 7b 79 79 67 6f  eno==226);.{yygo
31d4e 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
31d4f 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  1;}.        brea
31d50 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 32  k;.      case 32
31d51 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c  : /* create_tabl
31d52 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f  e_args ::= LP co
31d53 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73  lumnlist conslis
31d54 74 5f 6f 70 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  t_opt RP */.{.  
31d55 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28  sqlite3EndTable(
31d56 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31  pParse,&yymsp[-1
31d57 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
31d58 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
31d59 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
31d5a 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31d5b 33 33 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74 61  33: /* create_ta
31d5c 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20  ble_args ::= AS 
31d5d 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71  select */.{.  sq
31d5e 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50  lite3EndTable(pP
31d5f 61 72 73 65 2c 30 2c 30 2c 79 79 6d 73 70 5b 30  arse,0,0,yymsp[0
31d60 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20 20  ].minor.yy3);.  
31d61 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
31d62 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
31d63 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31d64 79 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y3);.}.        b
31d65 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31d66 20 33 36 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 20 3a   36: /* column :
31d67 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79 70 65  := columnid type
31d68 20 63 61 72 67 6c 69 73 74 20 2a 2f 0a 7b 0a 20   carglist */.{. 
31d69 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
31d6a 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  .z = yymsp[-2].m
31d6b 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79  inor.yy0.z;.  yy
31d6c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20  gotominor.yy0.n 
31d6d 3d 20 28 69 6e 74 29 28 70 50 61 72 73 65 2d 3e  = (int)(pParse->
31d6e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 2d 79 79 6d  sLastToken.z-yym
31d6f 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
31d70 2e 7a 29 20 2b 20 70 50 61 72 73 65 2d 3e 73 4c  .z) + pParse->sL
31d71 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a 20 20  astToken.n;.}.  
31d72 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31d73 20 20 20 63 61 73 65 20 33 37 3a 20 2f 2a 20 63     case 37: /* c
31d74 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 20 2a  olumnid ::= nm *
31d75 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 64 64  /.{.  sqlite3Add
31d76 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 26 79  Column(pParse,&y
31d77 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31d78 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  0);.  yygotomino
31d79 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d  r.yy0 = yymsp[0]
31d7a 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a 20 20  .minor.yy0;.}.  
31d7b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31d7c 20 20 20 63 61 73 65 20 33 38 3a 20 2f 2a 20 69     case 38: /* i
31d7d 64 20 3a 3a 3d 20 49 44 20 2a 2f 0a 20 20 20 20  d ::= ID */.    
31d7e 20 20 63 61 73 65 20 33 39 3a 20 2f 2a 20 69 64    case 39: /* id
31d7f 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 2a 2f 20   ::= INDEXED */ 
31d80 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31d81 65 6e 6f 3d 3d 33 39 29 3b 0a 20 20 20 20 20 20  eno==39);.      
31d82 63 61 73 65 20 34 30 3a 20 2f 2a 20 69 64 73 20  case 40: /* ids 
31d83 3a 3a 3d 20 49 44 7c 53 54 52 49 4e 47 20 2a 2f  ::= ID|STRING */
31d84 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31d85 6c 65 6e 6f 3d 3d 34 30 29 3b 0a 20 20 20 20 20  leno==40);.     
31d86 20 63 61 73 65 20 34 31 3a 20 2f 2a 20 6e 6d 20   case 41: /* nm 
31d87 3a 3a 3d 20 69 64 20 2a 2f 20 79 79 74 65 73 74  ::= id */ yytest
31d88 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34  case(yyruleno==4
31d89 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34  1);.      case 4
31d8a 32 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 53 54 52  2: /* nm ::= STR
31d8b 49 4e 47 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ING */ yytestcas
31d8c 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 32 29 3b  e(yyruleno==42);
31d8d 0a 20 20 20 20 20 20 63 61 73 65 20 34 33 3a 20  .      case 43: 
31d8e 2f 2a 20 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  /* nm ::= JOIN_K
31d8f 57 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  W */ yytestcase(
31d90 79 79 72 75 6c 65 6e 6f 3d 3d 34 33 29 3b 0a 20  yyruleno==43);. 
31d91 20 20 20 20 20 63 61 73 65 20 34 36 3a 20 2f 2a       case 46: /*
31d92 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74   typetoken ::= t
31d93 79 70 65 6e 61 6d 65 20 2a 2f 20 79 79 74 65 73  ypename */ yytes
31d94 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31d95 34 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  46);.      case 
31d96 34 39 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65 20  49: /* typename 
31d97 3a 3a 3d 20 69 64 73 20 2a 2f 20 79 79 74 65 73  ::= ids */ yytes
31d98 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31d99 34 39 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  49);.      case 
31d9a 31 32 36 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 41  126: /* as ::= A
31d9b 53 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61  S nm */ yytestca
31d9c 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 36  se(yyruleno==126
31d9d 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32  );.      case 12
31d9e 37 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 69 64 73  7: /* as ::= ids
31d9f 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31da0 79 72 75 6c 65 6e 6f 3d 3d 31 32 37 29 3b 0a 20  yruleno==127);. 
31da1 20 20 20 20 20 63 61 73 65 20 31 33 37 3a 20 2f       case 137: /
31da2 2a 20 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e  * dbnm ::= DOT n
31da3 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  m */ yytestcase(
31da4 79 79 72 75 6c 65 6e 6f 3d 3d 31 33 37 29 3b 0a  yyruleno==137);.
31da5 20 20 20 20 20 20 63 61 73 65 20 31 34 36 3a 20        case 146: 
31da6 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a  /* indexed_opt :
31da7 3a 3d 20 49 4e 44 45 58 45 44 20 42 59 20 6e 6d  := INDEXED BY nm
31da8 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31da9 79 72 75 6c 65 6e 6f 3d 3d 31 34 36 29 3b 0a 20  yruleno==146);. 
31daa 20 20 20 20 20 63 61 73 65 20 32 35 31 3a 20 2f       case 251: /
31dab 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f  * collate ::= CO
31dac 4c 4c 41 54 45 20 69 64 73 20 2a 2f 20 79 79 74  LLATE ids */ yyt
31dad 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31dae 3d 3d 32 35 31 29 3b 0a 20 20 20 20 20 20 63 61  ==251);.      ca
31daf 73 65 20 32 36 30 3a 20 2f 2a 20 6e 6d 6e 75 6d  se 260: /* nmnum
31db0 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f   ::= plus_num */
31db1 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31db2 6c 65 6e 6f 3d 3d 32 36 30 29 3b 0a 20 20 20 20  leno==260);.    
31db3 20 20 63 61 73 65 20 32 36 31 3a 20 2f 2a 20 6e    case 261: /* n
31db4 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f 20 79  mnum ::= nm */ y
31db5 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31db6 6e 6f 3d 3d 32 36 31 29 3b 0a 20 20 20 20 20 20  no==261);.      
31db7 63 61 73 65 20 32 36 32 3a 20 2f 2a 20 6e 6d 6e  case 262: /* nmn
31db8 75 6d 20 3a 3a 3d 20 4f 4e 20 2a 2f 20 79 79 74  um ::= ON */ yyt
31db9 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31dba 3d 3d 32 36 32 29 3b 0a 20 20 20 20 20 20 63 61  ==262);.      ca
31dbb 73 65 20 32 36 33 3a 20 2f 2a 20 6e 6d 6e 75 6d  se 263: /* nmnum
31dbc 20 3a 3a 3d 20 44 45 4c 45 54 45 20 2a 2f 20 79   ::= DELETE */ y
31dbd 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31dbe 6e 6f 3d 3d 32 36 33 29 3b 0a 20 20 20 20 20 20  no==263);.      
31dbf 63 61 73 65 20 32 36 34 3a 20 2f 2a 20 6e 6d 6e  case 264: /* nmn
31dc0 75 6d 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 2a  um ::= DEFAULT *
31dc1 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31dc2 75 6c 65 6e 6f 3d 3d 32 36 34 29 3b 0a 20 20 20  uleno==264);.   
31dc3 20 20 20 63 61 73 65 20 32 36 35 3a 20 2f 2a 20     case 265: /* 
31dc4 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d 20 70 6c 75  plus_num ::= plu
31dc5 73 5f 6f 70 74 20 6e 75 6d 62 65 72 20 2a 2f 20  s_opt number */ 
31dc6 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31dc7 65 6e 6f 3d 3d 32 36 35 29 3b 0a 20 20 20 20 20  eno==265);.     
31dc8 20 63 61 73 65 20 32 36 36 3a 20 2f 2a 20 6d 69   case 266: /* mi
31dc9 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d 49 4e 55  nus_num ::= MINU
31dca 53 20 6e 75 6d 62 65 72 20 2a 2f 20 79 79 74 65  S number */ yyte
31dcb 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31dcc 3d 32 36 36 29 3b 0a 20 20 20 20 20 20 63 61 73  =266);.      cas
31dcd 65 20 32 36 37 3a 20 2f 2a 20 6e 75 6d 62 65 72  e 267: /* number
31dce 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f   ::= INTEGER|FLO
31dcf 41 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  AT */ yytestcase
31dd0 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 37 29 3b  (yyruleno==267);
31dd1 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 35 3a  .      case 285:
31dd2 20 2f 2a 20 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 20   /* trnm ::= nm 
31dd3 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31dd4 72 75 6c 65 6e 6f 3d 3d 32 38 35 29 3b 0a 7b 79  ruleno==285);.{y
31dd5 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d  ygotominor.yy0 =
31dd6 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
31dd7 79 79 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  yy0;}.        br
31dd8 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31dd9 34 35 3a 20 2f 2a 20 74 79 70 65 20 3a 3a 3d 20  45: /* type ::= 
31dda 74 79 70 65 74 6f 6b 65 6e 20 2a 2f 0a 7b 73 71  typetoken */.{sq
31ddb 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79  lite3AddColumnTy
31ddc 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  pe(pParse,&yymsp
31ddd 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
31dde 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31ddf 20 20 20 20 20 20 63 61 73 65 20 34 37 3a 20 2f        case 47: /
31de0 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20  * typetoken ::= 
31de1 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e  typename LP sign
31de2 65 64 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67  ed RP */.{.  yyg
31de3 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d  otominor.yy0.z =
31de4 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
31de5 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f  .yy0.z;.  yygoto
31de6 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69  minor.yy0.n = (i
31de7 6e 74 29 28 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  nt)(&yymsp[0].mi
31de8 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
31de9 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20  0].minor.yy0.n] 
31dea 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  - yymsp[-3].mino
31deb 72 2e 79 79 30 2e 7a 29 3b 0a 7d 0a 20 20 20 20  r.yy0.z);.}.    
31dec 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31ded 20 63 61 73 65 20 34 38 3a 20 2f 2a 20 74 79 70   case 48: /* typ
31dee 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e  etoken ::= typen
31def 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 43 4f  ame LP signed CO
31df0 4d 4d 41 20 73 69 67 6e 65 64 20 52 50 20 2a 2f  MMA signed RP */
31df1 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
31df2 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d  .yy0.z = yymsp[-
31df3 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a  5].minor.yy0.z;.
31df4 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
31df5 30 2e 6e 20 3d 20 28 69 6e 74 29 28 26 79 79 6d  0.n = (int)(&yym
31df6 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
31df7 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  z[yymsp[0].minor
31df8 2e 79 79 30 2e 6e 5d 20 2d 20 79 79 6d 73 70 5b  .yy0.n] - yymsp[
31df9 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29  -5].minor.yy0.z)
31dfa 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
31dfb 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 30  k;.      case 50
31dfc 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a  : /* typename ::
31dfd 3d 20 74 79 70 65 6e 61 6d 65 20 69 64 73 20 2a  = typename ids *
31dfe 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
31dff 79 30 2e 7a 3d 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y0.z=yymsp[-1].m
31e00 69 6e 6f 72 2e 79 79 30 2e 7a 3b 20 79 79 67 6f  inor.yy0.z; yygo
31e01 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 79 79  tominor.yy0.n=yy
31e02 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
31e03 2e 6e 2b 28 69 6e 74 29 28 79 79 6d 73 70 5b 30  .n+(int)(yymsp[0
31e04 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 2d 79 79  ].minor.yy0.z-yy
31e05 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
31e06 30 2e 7a 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  0.z);}.        b
31e07 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31e08 20 35 37 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a   57: /* ccons ::
31e09 3d 20 44 45 46 41 55 4c 54 20 74 65 72 6d 20 2a  = DEFAULT term *
31e0a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 35 39 3a  /.      case 59:
31e0b 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45   /* ccons ::= DE
31e0c 46 41 55 4c 54 20 50 4c 55 53 20 74 65 72 6d 20  FAULT PLUS term 
31e0d 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31e0e 72 75 6c 65 6e 6f 3d 3d 35 39 29 3b 0a 7b 73 71  ruleno==59);.{sq
31e0f 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56  lite3AddDefaultV
31e10 61 6c 75 65 28 70 50 61 72 73 65 2c 26 79 79 6d  alue(pParse,&yym
31e11 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
31e12 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  6);}.        bre
31e13 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35  ak;.      case 5
31e14 38 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  8: /* ccons ::= 
31e15 44 45 46 41 55 4c 54 20 4c 50 20 65 78 70 72 20  DEFAULT LP expr 
31e16 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  RP */.{sqlite3Ad
31e17 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50  dDefaultValue(pP
31e18 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  arse,&yymsp[-1].
31e19 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 7d 0a 20  minor.yy346);}. 
31e1a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31e1b 20 20 20 20 63 61 73 65 20 36 30 3a 20 2f 2a 20      case 60: /* 
31e1c 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
31e1d 54 20 4d 49 4e 55 53 20 74 65 72 6d 20 2a 2f 0a  T MINUS term */.
31e1e 7b 0a 20 20 45 78 70 72 53 70 61 6e 20 76 3b 0a  {.  ExprSpan v;.
31e1f 20 20 76 2e 70 45 78 70 72 20 3d 20 73 71 6c 69    v.pExpr = sqli
31e20 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
31e21 20 54 4b 5f 55 4d 49 4e 55 53 2c 20 79 79 6d 73   TK_UMINUS, yyms
31e22 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  p[0].minor.yy346
31e23 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20  .pExpr, 0, 0);. 
31e24 20 76 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73   v.zStart = yyms
31e25 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-1].minor.yy0.
31e26 7a 3b 0a 20 20 76 2e 7a 45 6e 64 20 3d 20 79 79  z;.  v.zEnd = yy
31e27 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
31e28 34 36 2e 7a 45 6e 64 3b 0a 20 20 73 71 6c 69 74  46.zEnd;.  sqlit
31e29 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75  e3AddDefaultValu
31e2a 65 28 70 50 61 72 73 65 2c 26 76 29 3b 0a 7d 0a  e(pParse,&v);.}.
31e2b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31e2c 20 20 20 20 20 63 61 73 65 20 36 31 3a 20 2f 2a       case 61: /*
31e2d 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55   ccons ::= DEFAU
31e2e 4c 54 20 69 64 20 2a 2f 0a 7b 0a 20 20 45 78 70  LT id */.{.  Exp
31e2f 72 53 70 61 6e 20 76 3b 0a 20 20 73 70 61 6e 45  rSpan v;.  spanE
31e30 78 70 72 28 26 76 2c 20 70 50 61 72 73 65 2c 20  xpr(&v, pParse, 
31e31 54 4b 5f 53 54 52 49 4e 47 2c 20 26 79 79 6d 73  TK_STRING, &yyms
31e32 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
31e33 0a 20 20 73 71 6c 69 74 65 33 41 64 64 44 65 66  .  sqlite3AddDef
31e34 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65  aultValue(pParse
31e35 2c 26 76 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  ,&v);.}.        
31e36 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31e37 65 20 36 33 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 63: /* ccons :
31e38 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f  := NOT NULL onco
31e39 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  nf */.{sqlite3Ad
31e3a 64 4e 6f 74 4e 75 6c 6c 28 70 50 61 72 73 65 2c  dNotNull(pParse,
31e3b 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
31e3c 79 79 33 32 38 29 3b 7d 0a 20 20 20 20 20 20 20  yy328);}.       
31e3d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31e3e 73 65 20 36 34 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 64: /* ccons 
31e3f 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20  ::= PRIMARY KEY 
31e40 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63 6f 6e 66  sortorder onconf
31e41 20 61 75 74 6f 69 6e 63 20 2a 2f 0a 7b 73 71 6c   autoinc */.{sql
31e42 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
31e43 79 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70  y(pParse,0,yymsp
31e44 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-1].minor.yy328
31e45 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
31e46 79 79 33 32 38 2c 79 79 6d 73 70 5b 2d 32 5d 2e  yy328,yymsp[-2].
31e47 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20  minor.yy328);}. 
31e48 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31e49 20 20 20 20 63 61 73 65 20 36 35 3a 20 2f 2a 20      case 65: /* 
31e4a 63 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45  ccons ::= UNIQUE
31e4b 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69   onconf */.{sqli
31e4c 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70  te3CreateIndex(p
31e4d 50 61 72 73 65 2c 30 2c 30 2c 30 2c 30 2c 79 79  Parse,0,0,0,0,yy
31e4e 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
31e4f 32 38 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20  28,0,0,0,0);}.  
31e50 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31e51 20 20 20 63 61 73 65 20 36 36 3a 20 2f 2a 20 63     case 66: /* c
31e52 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c  cons ::= CHECK L
31e53 50 20 65 78 70 72 20 52 50 20 2a 2f 0a 7b 73 71  P expr RP */.{sq
31e54 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e  lite3AddCheckCon
31e55 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c 79  straint(pParse,y
31e56 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31e57 79 33 34 36 2e 70 45 78 70 72 29 3b 7d 0a 20 20  y346.pExpr);}.  
31e58 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31e59 20 20 20 63 61 73 65 20 36 37 3a 20 2f 2a 20 63     case 67: /* c
31e5a 63 6f 6e 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e  cons ::= REFEREN
31e5b 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f  CES nm idxlist_o
31e5c 70 74 20 72 65 66 61 72 67 73 20 2a 2f 0a 7b 73  pt refargs */.{s
31e5d 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 65  qlite3CreateFore
31e5e 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 30 2c  ignKey(pParse,0,
31e5f 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
31e60 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  .yy0,yymsp[-1].m
31e61 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b  inor.yy14,yymsp[
31e62 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  0].minor.yy328);
31e63 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31e64 0a 20 20 20 20 20 20 63 61 73 65 20 36 38 3a 20  .      case 68: 
31e65 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 64 65 66  /* ccons ::= def
31e66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 2a 2f 0a  er_subclause */.
31e67 7b 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72  {sqlite3DeferFor
31e68 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 79  eignKey(pParse,y
31e69 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31e6a 33 32 38 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  328);}.        b
31e6b 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31e6c 20 36 39 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a   69: /* ccons ::
31e6d 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f  = COLLATE ids */
31e6e 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c  .{sqlite3AddColl
31e6f 61 74 65 54 79 70 65 28 70 50 61 72 73 65 2c 20  ateType(pParse, 
31e70 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
31e71 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
31e72 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31e73 20 37 32 3a 20 2f 2a 20 72 65 66 61 72 67 73 20   72: /* refargs 
31e74 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  ::= */.{ yygotom
31e75 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f  inor.yy328 = OE_
31e76 4e 6f 6e 65 2a 30 78 30 31 30 31 3b 20 2f 2a 20  None*0x0101; /* 
31e77 45 56 3a 20 52 2d 31 39 38 30 33 2d 34 35 38 38  EV: R-19803-4588
31e78 34 20 2a 2f 7d 0a 20 20 20 20 20 20 20 20 62 72  4 */}.        br
31e79 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31e7a 37 33 3a 20 2f 2a 20 72 65 66 61 72 67 73 20 3a  73: /* refargs :
31e7b 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66 61 72  := refargs refar
31e7c 67 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  g */.{ yygotomin
31e7d 6f 72 2e 79 79 33 32 38 20 3d 20 28 79 79 6d 73  or.yy328 = (yyms
31e7e 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-1].minor.yy32
31e7f 38 20 26 20 7e 79 79 6d 73 70 5b 30 5d 2e 6d 69  8 & ~yymsp[0].mi
31e80 6e 6f 72 2e 79 79 34 32 39 2e 6d 61 73 6b 29 20  nor.yy429.mask) 
31e81 7c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  | yymsp[0].minor
31e82 2e 79 79 34 32 39 2e 76 61 6c 75 65 3b 20 7d 0a  .yy429.value; }.
31e83 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31e84 20 20 20 20 20 63 61 73 65 20 37 34 3a 20 2f 2a       case 74: /*
31e85 20 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43   refarg ::= MATC
31e86 48 20 6e 6d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  H nm */.{ yygoto
31e87 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 76 61 6c 75  minor.yy429.valu
31e88 65 20 3d 20 30 3b 20 20 20 20 20 79 79 67 6f 74  e = 0;     yygot
31e89 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 6d 61 73  ominor.yy429.mas
31e8a 6b 20 3d 20 30 78 30 30 30 30 30 30 3b 20 7d 0a  k = 0x000000; }.
31e8b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31e8c 20 20 20 20 20 63 61 73 65 20 37 35 3a 20 2f 2a       case 75: /*
31e8d 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 44   refarg ::= ON D
31e8e 45 4c 45 54 45 20 72 65 66 61 63 74 20 2a 2f 0a  ELETE refact */.
31e8f 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
31e90 34 32 39 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73  429.value = yyms
31e91 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
31e92 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  ;     yygotomino
31e93 72 2e 79 79 34 32 39 2e 6d 61 73 6b 20 3d 20 30  r.yy429.mask = 0
31e94 78 30 30 30 30 66 66 3b 20 7d 0a 20 20 20 20 20  x0000ff; }.     
31e95 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31e96 63 61 73 65 20 37 36 3a 20 2f 2a 20 72 65 66 61  case 76: /* refa
31e97 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45  rg ::= ON UPDATE
31e98 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67   refact */.{ yyg
31e99 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 76  otominor.yy429.v
31e9a 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  alue = yymsp[0].
31e9b 6d 69 6e 6f 72 2e 79 79 33 32 38 3c 3c 38 3b 20  minor.yy328<<8; 
31e9c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
31e9d 32 39 2e 6d 61 73 6b 20 3d 20 30 78 30 30 66 66  29.mask = 0x00ff
31e9e 30 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  00; }.        br
31e9f 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31ea0 37 37 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a  77: /* refact ::
31ea1 3d 20 53 45 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 20  = SET NULL */.{ 
31ea2 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
31ea3 38 20 3d 20 4f 45 5f 53 65 74 4e 75 6c 6c 3b 20  8 = OE_SetNull; 
31ea4 20 2f 2a 20 45 56 3a 20 52 2d 33 33 33 32 36 2d   /* EV: R-33326-
31ea5 34 35 32 35 32 20 2a 2f 7d 0a 20 20 20 20 20 20  45252 */}.      
31ea6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31ea7 61 73 65 20 37 38 3a 20 2f 2a 20 72 65 66 61 63  ase 78: /* refac
31ea8 74 20 3a 3a 3d 20 53 45 54 20 44 45 46 41 55 4c  t ::= SET DEFAUL
31ea9 54 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  T */.{ yygotomin
31eaa 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 53 65  or.yy328 = OE_Se
31eab 74 44 66 6c 74 3b 20 20 2f 2a 20 45 56 3a 20 52  tDflt;  /* EV: R
31eac 2d 33 33 33 32 36 2d 34 35 32 35 32 20 2a 2f 7d  -33326-45252 */}
31ead 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31eae 20 20 20 20 20 20 63 61 73 65 20 37 39 3a 20 2f        case 79: /
31eaf 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 43 41 53  * refact ::= CAS
31eb0 43 41 44 45 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  CADE */.{ yygoto
31eb1 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45  minor.yy328 = OE
31eb2 5f 43 61 73 63 61 64 65 3b 20 20 2f 2a 20 45 56  _Cascade;  /* EV
31eb3 3a 20 52 2d 33 33 33 32 36 2d 34 35 32 35 32 20  : R-33326-45252 
31eb4 2a 2f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  */}.        brea
31eb5 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 30  k;.      case 80
31eb6 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20  : /* refact ::= 
31eb7 52 45 53 54 52 49 43 54 20 2a 2f 0a 7b 20 79 79  RESTRICT */.{ yy
31eb8 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
31eb9 3d 20 4f 45 5f 52 65 73 74 72 69 63 74 3b 20 2f  = OE_Restrict; /
31eba 2a 20 45 56 3a 20 52 2d 33 33 33 32 36 2d 34 35  * EV: R-33326-45
31ebb 32 35 32 20 2a 2f 7d 0a 20 20 20 20 20 20 20 20  252 */}.        
31ebc 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31ebd 65 20 38 31 3a 20 2f 2a 20 72 65 66 61 63 74 20  e 81: /* refact 
31ebe 3a 3a 3d 20 4e 4f 20 41 43 54 49 4f 4e 20 2a 2f  ::= NO ACTION */
31ebf 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
31ec0 79 33 32 38 20 3d 20 4f 45 5f 4e 6f 6e 65 3b 20  y328 = OE_None; 
31ec1 20 20 20 20 2f 2a 20 45 56 3a 20 52 2d 33 33 33      /* EV: R-333
31ec2 32 36 2d 34 35 32 35 32 20 2a 2f 7d 0a 20 20 20  26-45252 */}.   
31ec3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31ec4 20 20 63 61 73 65 20 38 33 3a 20 2f 2a 20 64 65    case 83: /* de
31ec5 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a  fer_subclause ::
31ec6 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  = DEFERRABLE ini
31ec7 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
31ec8 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  opt */.      cas
31ec9 65 20 39 38 3a 20 2f 2a 20 64 65 66 65 72 5f 73  e 98: /* defer_s
31eca 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d  ubclause_opt ::=
31ecb 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
31ecc 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31ecd 79 72 75 6c 65 6e 6f 3d 3d 39 38 29 3b 0a 20 20  yruleno==98);.  
31ece 20 20 20 20 63 61 73 65 20 31 30 30 3a 20 2f 2a      case 100: /*
31ecf 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20 43   onconf ::= ON C
31ed0 4f 4e 46 4c 49 43 54 20 72 65 73 6f 6c 76 65 74  ONFLICT resolvet
31ed1 79 70 65 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ype */ yytestcas
31ed2 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 30 29  e(yyruleno==100)
31ed3 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 33  ;.      case 103
31ed4 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65  : /* resolvetype
31ed5 20 3a 3a 3d 20 72 61 69 73 65 74 79 70 65 20 2a   ::= raisetype *
31ed6 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31ed7 75 6c 65 6e 6f 3d 3d 31 30 33 29 3b 0a 7b 79 79  uleno==103);.{yy
31ed8 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
31ed9 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
31eda 2e 79 79 33 32 38 3b 7d 0a 20 20 20 20 20 20 20  .yy328;}.       
31edb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31edc 73 65 20 38 37 3a 20 2f 2a 20 63 6f 6e 73 6c 69  se 87: /* consli
31edd 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79  st_opt ::= */.{y
31ede 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e  ygotominor.yy0.n
31edf 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f   = 0; yygotomino
31ee0 72 2e 79 79 30 2e 7a 20 3d 20 30 3b 7d 0a 20 20  r.yy0.z = 0;}.  
31ee1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31ee2 20 20 20 63 61 73 65 20 38 38 3a 20 2f 2a 20 63     case 88: /* c
31ee3 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20  onslist_opt ::= 
31ee4 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69 73 74 20 2a  COMMA conslist *
31ee5 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
31ee6 79 30 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y0 = yymsp[-1].m
31ee7 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20 20  inor.yy0;}.     
31ee8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31ee9 63 61 73 65 20 39 33 3a 20 2f 2a 20 74 63 6f 6e  case 93: /* tcon
31eea 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45  s ::= PRIMARY KE
31eeb 59 20 4c 50 20 69 64 78 6c 69 73 74 20 61 75 74  Y LP idxlist aut
31eec 6f 69 6e 63 20 52 50 20 6f 6e 63 6f 6e 66 20 2a  oinc RP onconf *
31eed 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 50 72 69  /.{sqlite3AddPri
31eee 6d 61 72 79 4b 65 79 28 70 50 61 72 73 65 2c 79  maryKey(pParse,y
31eef 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
31ef0 79 31 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y14,yymsp[0].min
31ef1 6f 72 2e 79 79 33 32 38 2c 79 79 6d 73 70 5b 2d  or.yy328,yymsp[-
31ef2 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 30  2].minor.yy328,0
31ef3 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
31ef4 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 34  k;.      case 94
31ef5 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 55  : /* tcons ::= U
31ef6 4e 49 51 55 45 20 4c 50 20 69 64 78 6c 69 73 74  NIQUE LP idxlist
31ef7 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73   RP onconf */.{s
31ef8 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65  qlite3CreateInde
31ef9 78 28 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c 79  x(pParse,0,0,0,y
31efa 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
31efb 79 31 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y14,yymsp[0].min
31efc 6f 72 2e 79 79 33 32 38 2c 30 2c 30 2c 30 2c 30  or.yy328,0,0,0,0
31efd 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
31efe 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 35  k;.      case 95
31eff 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 43  : /* tcons ::= C
31f00 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50 20  HECK LP expr RP 
31f01 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74  onconf */.{sqlit
31f02 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72  e3AddCheckConstr
31f03 61 69 6e 74 28 70 50 61 72 73 65 2c 79 79 6d 73  aint(pParse,yyms
31f04 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-2].minor.yy34
31f05 36 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20  6.pExpr);}.     
31f06 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31f07 63 61 73 65 20 39 36 3a 20 2f 2a 20 74 63 6f 6e  case 96: /* tcon
31f08 73 20 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45  s ::= FOREIGN KE
31f09 59 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20  Y LP idxlist RP 
31f0a 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64  REFERENCES nm id
31f0b 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67  xlist_opt refarg
31f0c 73 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73  s defer_subclaus
31f0d 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20 73  e_opt */.{.    s
31f0e 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 65  qlite3CreateFore
31f0f 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 20 79  ignKey(pParse, y
31f10 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
31f11 79 31 34 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e  y14, &yymsp[-3].
31f12 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70  minor.yy0, yymsp
31f13 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-2].minor.yy14,
31f14 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
31f15 2e 79 79 33 32 38 29 3b 0a 20 20 20 20 73 71 6c  .yy328);.    sql
31f16 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
31f17 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73  Key(pParse, yyms
31f18 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
31f19 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
31f1a 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39  ak;.      case 9
31f1b 39 3a 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d  9: /* onconf ::=
31f1c 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
31f1d 2e 79 79 33 32 38 20 3d 20 4f 45 5f 44 65 66 61  .yy328 = OE_Defa
31f1e 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ult;}.        br
31f1f 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31f20 31 30 31 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a  101: /* orconf :
31f21 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  := */.{yygotomin
31f22 6f 72 2e 79 79 31 38 36 20 3d 20 4f 45 5f 44 65  or.yy186 = OE_De
31f23 66 61 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20 20  fault;}.        
31f24 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31f25 65 20 31 30 32 3a 20 2f 2a 20 6f 72 63 6f 6e 66  e 102: /* orconf
31f26 20 3a 3a 3d 20 4f 52 20 72 65 73 6f 6c 76 65 74   ::= OR resolvet
31f27 79 70 65 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ype */.{yygotomi
31f28 6e 6f 72 2e 79 79 31 38 36 20 3d 20 28 75 38 29  nor.yy186 = (u8)
31f29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31f2a 79 33 32 38 3b 7d 0a 20 20 20 20 20 20 20 20 62  y328;}.        b
31f2b 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31f2c 20 31 30 34 3a 20 2f 2a 20 72 65 73 6f 6c 76 65   104: /* resolve
31f2d 74 79 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45 20  type ::= IGNORE 
31f2e 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
31f2f 79 79 33 32 38 20 3d 20 4f 45 5f 49 67 6e 6f 72  yy328 = OE_Ignor
31f30 65 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  e;}.        brea
31f31 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  k;.      case 10
31f32 35 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70  5: /* resolvetyp
31f33 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f  e ::= REPLACE */
31f34 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
31f35 33 32 38 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65  328 = OE_Replace
31f36 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
31f37 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 36  ;.      case 106
31f38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f  : /* cmd ::= DRO
31f39 50 20 54 41 42 4c 45 20 69 66 65 78 69 73 74 73  P TABLE ifexists
31f3a 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20   fullname */.{. 
31f3b 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c   sqlite3DropTabl
31f3c 65 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  e(pParse, yymsp[
31f3d 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 30  0].minor.yy65, 0
31f3e 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
31f3f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20  r.yy328);.}.    
31f40 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31f41 20 63 61 73 65 20 31 30 39 3a 20 2f 2a 20 63 6d   case 109: /* cm
31f42 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74  d ::= createkw t
31f43 65 6d 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78  emp VIEW ifnotex
31f44 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20  ists nm dbnm AS 
31f45 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71  select */.{.  sq
31f46 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28  lite3CreateView(
31f47 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
31f48 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  7].minor.yy0, &y
31f49 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
31f4a 79 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y0, &yymsp[-2].m
31f4b 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b  inor.yy0, yymsp[
31f4c 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 79 79  0].minor.yy3, yy
31f4d 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
31f4e 33 32 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  328, yymsp[-4].m
31f4f 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20  inor.yy328);.}. 
31f50 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31f51 20 20 20 20 63 61 73 65 20 31 31 30 3a 20 2f 2a      case 110: /*
31f52 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56 49   cmd ::= DROP VI
31f53 45 57 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c  EW ifexists full
31f54 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  name */.{.  sqli
31f55 74 65 33 44 72 6f 70 54 61 62 6c 65 28 70 50 61  te3DropTable(pPa
31f56 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
31f57 6e 6f 72 2e 79 79 36 35 2c 20 31 2c 20 79 79 6d  nor.yy65, 1, yym
31f58 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
31f59 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  28);.}.        b
31f5a 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31f5b 20 31 31 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   111: /* cmd ::=
31f5c 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 53   select */.{.  S
31f5d 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 20 3d  electDest dest =
31f5e 20 7b 53 52 54 5f 4f 75 74 70 75 74 2c 20 30 2c   {SRT_Output, 0,
31f5f 20 30 2c 20 30 2c 20 30 7d 3b 0a 20 20 73 71 6c   0, 0, 0};.  sql
31f60 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
31f61 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  e, yymsp[0].mino
31f62 72 2e 79 79 33 2c 20 26 64 65 73 74 29 3b 0a 20  r.yy3, &dest);. 
31f63 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
31f64 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
31f65 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
31f66 79 79 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy3);.}.        
31f67 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31f68 65 20 31 31 32 3a 20 2f 2a 20 73 65 6c 65 63 74  e 112: /* select
31f69 20 3a 3a 3d 20 6f 6e 65 73 65 6c 65 63 74 20 2a   ::= oneselect *
31f6a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
31f6b 79 33 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  y3 = yymsp[0].mi
31f6c 6e 6f 72 2e 79 79 33 3b 7d 0a 20 20 20 20 20 20  nor.yy3;}.      
31f6d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31f6e 61 73 65 20 31 31 33 3a 20 2f 2a 20 73 65 6c 65  ase 113: /* sele
31f6f 63 74 20 3a 3a 3d 20 73 65 6c 65 63 74 20 6d 75  ct ::= select mu
31f70 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 6f 6e 65  ltiselect_op one
31f71 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 69 66  select */.{.  if
31f72 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ( yymsp[0].minor
31f73 2e 79 79 33 20 29 7b 0a 20 20 20 20 79 79 6d 73  .yy3 ){.    yyms
31f74 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2d 3e  p[0].minor.yy3->
31f75 6f 70 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 2d  op = (u8)yymsp[-
31f76 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a  1].minor.yy328;.
31f77 20 20 20 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e      yymsp[0].min
31f78 6f 72 2e 79 79 33 2d 3e 70 50 72 69 6f 72 20 3d  or.yy3->pPrior =
31f79 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
31f7a 2e 79 79 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  .yy3;.  }else{. 
31f7b 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
31f7c 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
31f7d 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  b, yymsp[-2].min
31f7e 6f 72 2e 79 79 33 29 3b 0a 20 20 7d 0a 20 20 79  or.yy3);.  }.  y
31f7f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 20 3d  ygotominor.yy3 =
31f80 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
31f81 79 79 33 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  yy3;.}.        b
31f82 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31f83 20 31 31 35 3a 20 2f 2a 20 6d 75 6c 74 69 73 65   115: /* multise
31f84 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f  lect_op ::= UNIO
31f85 4e 20 41 4c 4c 20 2a 2f 0a 7b 79 79 67 6f 74 6f  N ALL */.{yygoto
31f86 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54 4b  minor.yy328 = TK
31f87 5f 41 4c 4c 3b 7d 0a 20 20 20 20 20 20 20 20 62  _ALL;}.        b
31f88 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31f89 20 31 31 37 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65   117: /* onesele
31f8a 63 74 20 3a 3a 3d 20 53 45 4c 45 43 54 20 64 69  ct ::= SELECT di
31f8b 73 74 69 6e 63 74 20 73 65 6c 63 6f 6c 6c 69 73  stinct selcollis
31f8c 74 20 66 72 6f 6d 20 77 68 65 72 65 5f 6f 70 74  t from where_opt
31f8d 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 68 61 76   groupby_opt hav
31f8e 69 6e 67 5f 6f 70 74 20 6f 72 64 65 72 62 79 5f  ing_opt orderby_
31f8f 6f 70 74 20 6c 69 6d 69 74 5f 6f 70 74 20 2a 2f  opt limit_opt */
31f90 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
31f91 2e 79 79 33 20 3d 20 73 71 6c 69 74 65 33 53 65  .yy3 = sqlite3Se
31f92 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 79  lectNew(pParse,y
31f93 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
31f94 79 31 34 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  y14,yymsp[-5].mi
31f95 6e 6f 72 2e 79 79 36 35 2c 79 79 6d 73 70 5b 2d  nor.yy65,yymsp[-
31f96 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79  4].minor.yy132,y
31f97 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
31f98 79 31 34 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  y14,yymsp[-2].mi
31f99 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b  nor.yy132,yymsp[
31f9a 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79  -1].minor.yy14,y
31f9b 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
31f9c 79 33 32 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  y328,yymsp[0].mi
31f9d 6e 6f 72 2e 79 79 34 37 36 2e 70 4c 69 6d 69 74  nor.yy476.pLimit
31f9e 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
31f9f 79 79 34 37 36 2e 70 4f 66 66 73 65 74 29 3b 0a  yy476.pOffset);.
31fa0 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31fa1 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 31 3a  .      case 121:
31fa2 20 2f 2a 20 73 63 6c 70 20 3a 3a 3d 20 73 65 6c   /* sclp ::= sel
31fa3 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 2a 2f  collist COMMA */
31fa4 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 37 3a  .      case 247:
31fa5 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   /* idxlist_opt 
31fa6 3a 3a 3d 20 4c 50 20 69 64 78 6c 69 73 74 20 52  ::= LP idxlist R
31fa7 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  P */ yytestcase(
31fa8 79 79 72 75 6c 65 6e 6f 3d 3d 32 34 37 29 3b 0a  yyruleno==247);.
31fa9 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
31faa 34 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  4 = yymsp[-1].mi
31fab 6e 6f 72 2e 79 79 31 34 3b 7d 0a 20 20 20 20 20  nor.yy14;}.     
31fac 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31fad 63 61 73 65 20 31 32 32 3a 20 2f 2a 20 73 63 6c  case 122: /* scl
31fae 70 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  p ::= */.      c
31faf 61 73 65 20 31 35 30 3a 20 2f 2a 20 6f 72 64 65  ase 150: /* orde
31fb0 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79  rby_opt ::= */ y
31fb1 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31fb2 6e 6f 3d 3d 31 35 30 29 3b 0a 20 20 20 20 20 20  no==150);.      
31fb3 63 61 73 65 20 31 35 38 3a 20 2f 2a 20 67 72 6f  case 158: /* gro
31fb4 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  upby_opt ::= */ 
31fb5 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31fb6 65 6e 6f 3d 3d 31 35 38 29 3b 0a 20 20 20 20 20  eno==158);.     
31fb7 20 63 61 73 65 20 32 34 30 3a 20 2f 2a 20 65 78   case 240: /* ex
31fb8 70 72 6c 69 73 74 20 3a 3a 3d 20 2a 2f 20 79 79  prlist ::= */ yy
31fb9 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31fba 6f 3d 3d 32 34 30 29 3b 0a 20 20 20 20 20 20 63  o==240);.      c
31fbb 61 73 65 20 32 34 36 3a 20 2f 2a 20 69 64 78 6c  ase 246: /* idxl
31fbc 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79  ist_opt ::= */ y
31fbd 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31fbe 6e 6f 3d 3d 32 34 36 29 3b 0a 7b 79 79 67 6f 74  no==246);.{yygot
31fbf 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 30 3b  ominor.yy14 = 0;
31fc0 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31fc1 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 33 3a  .      case 123:
31fc2 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a   /* selcollist :
31fc3 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61 73 20  := sclp expr as 
31fc4 2a 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d 69  */.{.   yygotomi
31fc5 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
31fc6 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
31fc7 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
31fc8 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79  2].minor.yy14, y
31fc9 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31fca 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20  y346.pExpr);.   
31fcb 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  if( yymsp[0].min
31fcc 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 20 73 71 6c  or.yy0.n>0 ) sql
31fcd 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e  ite3ExprListSetN
31fce 61 6d 65 28 70 50 61 72 73 65 2c 20 79 79 67 6f  ame(pParse, yygo
31fcf 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79  tominor.yy14, &y
31fd0 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31fd1 30 2c 20 31 29 3b 0a 20 20 20 73 71 6c 69 74 65  0, 1);.   sqlite
31fd2 33 45 78 70 72 4c 69 73 74 53 65 74 53 70 61 6e  3ExprListSetSpan
31fd3 28 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69  (pParse,yygotomi
31fd4 6e 6f 72 2e 79 79 31 34 2c 26 79 79 6d 73 70 5b  nor.yy14,&yymsp[
31fd5 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29  -1].minor.yy346)
31fd6 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
31fd7 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32  k;.      case 12
31fd8 34 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74  4: /* selcollist
31fd9 20 3a 3a 3d 20 73 63 6c 70 20 53 54 41 52 20 2a   ::= sclp STAR *
31fda 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20  /.{.  Expr *p = 
31fdb 73 71 6c 69 74 65 33 45 78 70 72 28 70 50 61 72  sqlite3Expr(pPar
31fdc 73 65 2d 3e 64 62 2c 20 54 4b 5f 41 4c 4c 2c 20  se->db, TK_ALL, 
31fdd 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  0);.  yygotomino
31fde 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
31fdf 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
31fe0 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d  Parse, yymsp[-1]
31fe1 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 70 29 3b  .minor.yy14, p);
31fe2 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31fe3 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 35  ;.      case 125
31fe4 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20  : /* selcollist 
31fe5 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20  ::= sclp nm DOT 
31fe6 53 54 41 52 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  STAR */.{.  Expr
31fe7 20 2a 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74   *pRight = sqlit
31fe8 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
31fe9 54 4b 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20 26 79  TK_ALL, 0, 0, &y
31fea 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31feb 30 29 3b 0a 20 20 45 78 70 72 20 2a 70 4c 65 66  0);.  Expr *pLef
31fec 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  t = sqlite3PExpr
31fed 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
31fee 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  0, 0, &yymsp[-2]
31fef 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
31ff0 78 70 72 20 2a 70 44 6f 74 20 3d 20 73 71 6c 69  xpr *pDot = sqli
31ff1 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
31ff2 20 54 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 2c 20   TK_DOT, pLeft, 
31ff3 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 79 79  pRight, 0);.  yy
31ff4 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d  gotominor.yy14 =
31ff5 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
31ff6 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79  Append(pParse,yy
31ff7 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
31ff8 31 34 2c 20 70 44 6f 74 29 3b 0a 7d 0a 20 20 20  14, pDot);.}.   
31ff9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31ffa 20 20 63 61 73 65 20 31 32 38 3a 20 2f 2a 20 61    case 128: /* a
31ffb 73 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  s ::= */.{yygoto
31ffc 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b  minor.yy0.n = 0;
31ffd 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31ffe 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 39 3a  .      case 129:
31fff 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20 2a 2f 0a   /* from ::= */.
32000 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36  {yygotominor.yy6
32001 35 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  5 = sqlite3DbMal
32002 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e  locZero(pParse->
32003 64 62 2c 20 73 69 7a 65 6f 66 28 2a 79 79 67 6f  db, sizeof(*yygo
32004 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 29 29 3b 7d  tominor.yy65));}
32005 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32006 20 20 20 20 20 20 63 61 73 65 20 31 33 30 3a 20        case 130: 
32007 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d  /* from ::= FROM
32008 20 73 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a 7b   seltablist */.{
32009 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3200a 79 36 35 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  y65 = yymsp[0].m
3200b 69 6e 6f 72 2e 79 79 36 35 3b 0a 20 20 73 71 6c  inor.yy65;.  sql
3200c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74  ite3SrcListShift
3200d 4a 6f 69 6e 54 79 70 65 28 79 79 67 6f 74 6f 6d  JoinType(yygotom
3200e 69 6e 6f 72 2e 79 79 36 35 29 3b 0a 7d 0a 20 20  inor.yy65);.}.  
3200f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32010 20 20 20 63 61 73 65 20 31 33 31 3a 20 2f 2a 20     case 131: /* 
32011 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 20 73  stl_prefix ::= s
32012 65 6c 74 61 62 6c 69 73 74 20 6a 6f 69 6e 6f 70  eltablist joinop
32013 20 2a 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d   */.{.   yygotom
32014 69 6e 6f 72 2e 79 79 36 35 20 3d 20 79 79 6d 73  inor.yy65 = yyms
32015 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35  p[-1].minor.yy65
32016 3b 0a 20 20 20 69 66 28 20 41 4c 57 41 59 53 28  ;.   if( ALWAYS(
32017 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35  yygotominor.yy65
32018 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e   && yygotominor.
32019 79 79 36 35 2d 3e 6e 53 72 63 3e 30 29 20 29 20  yy65->nSrc>0) ) 
3201a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35  yygotominor.yy65
3201b 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ->a[yygotominor.
3201c 79 79 36 35 2d 3e 6e 53 72 63 2d 31 5d 2e 6a 6f  yy65->nSrc-1].jo
3201d 69 6e 74 79 70 65 20 3d 20 28 75 38 29 79 79 6d  intype = (u8)yym
3201e 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
3201f 38 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  8;.}.        bre
32020 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
32021 33 32 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69  32: /* stl_prefi
32022 78 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  x ::= */.{yygoto
32023 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 30 3b 7d  minor.yy65 = 0;}
32024 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32025 20 20 20 20 20 20 63 61 73 65 20 31 33 33 3a 20        case 133: 
32026 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a  /* seltablist ::
32027 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20  = stl_prefix nm 
32028 64 62 6e 6d 20 61 73 20 69 6e 64 65 78 65 64 5f  dbnm as indexed_
32029 6f 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67  opt on_opt using
3202a 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  _opt */.{.  yygo
3202b 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73  tominor.yy65 = s
3202c 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
3202d 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72  endFromTerm(pPar
3202e 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  se,yymsp[-6].min
3202f 6f 72 2e 79 79 36 35 2c 26 79 79 6d 73 70 5b 2d  or.yy65,&yymsp[-
32030 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  5].minor.yy0,&yy
32031 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
32032 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  0,&yymsp[-3].min
32033 6f 72 2e 79 79 30 2c 30 2c 79 79 6d 73 70 5b 2d  or.yy0,0,yymsp[-
32034 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79  1].minor.yy132,y
32035 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32036 34 30 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  408);.  sqlite3S
32037 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28  rcListIndexedBy(
32038 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
32039 6e 6f 72 2e 79 79 36 35 2c 20 26 79 79 6d 73 70  nor.yy65, &yymsp
3203a 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-2].minor.yy0);
3203b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3203c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 34  ;.      case 134
3203d 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20  : /* seltablist 
3203e 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c  ::= stl_prefix L
3203f 50 20 73 65 6c 65 63 74 20 52 50 20 61 73 20 6f  P select RP as o
32040 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20  n_opt using_opt 
32041 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  */.{.    yygotom
32042 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c 69  inor.yy65 = sqli
32043 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
32044 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73 65 2c  FromTerm(pParse,
32045 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
32046 79 79 36 35 2c 30 2c 30 2c 26 79 79 6d 73 70 5b  yy65,0,0,&yymsp[
32047 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79  -2].minor.yy0,yy
32048 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
32049 33 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  3,yymsp[-1].mino
3204a 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b 30 5d  r.yy132,yymsp[0]
3204b 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 29 3b 0a 20  .minor.yy408);. 
3204c 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
3204d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 35  ;.      case 135
3204e 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20  : /* seltablist 
3204f 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c  ::= stl_prefix L
32050 50 20 73 65 6c 74 61 62 6c 69 73 74 20 52 50 20  P seltablist RP 
32051 61 73 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f  as on_opt using_
32052 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20 69 66 28  opt */.{.    if(
32053 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72   yymsp[-6].minor
32054 2e 79 79 36 35 3d 3d 30 20 26 26 20 79 79 6d 73  .yy65==0 && yyms
32055 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-2].minor.yy0.
32056 6e 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b 2d 31  n==0 && yymsp[-1
32057 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 3d 3d 30  ].minor.yy132==0
32058 20 26 26 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   && yymsp[0].min
32059 6f 72 2e 79 79 34 30 38 3d 3d 30 20 29 7b 0a 20  or.yy408==0 ){. 
3205a 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72       yygotominor
3205b 2e 79 79 36 35 20 3d 20 79 79 6d 73 70 5b 2d 34  .yy65 = yymsp[-4
3205c 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 3b 0a 20 20  ].minor.yy65;.  
3205d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 53    }else{.      S
3205e 65 6c 65 63 74 20 2a 70 53 75 62 71 75 65 72 79  elect *pSubquery
3205f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ;.      sqlite3S
32060 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54  rcListShiftJoinT
32061 79 70 65 28 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  ype(yymsp[-4].mi
32062 6e 6f 72 2e 79 79 36 35 29 3b 0a 20 20 20 20 20  nor.yy65);.     
32063 20 70 53 75 62 71 75 65 72 79 20 3d 20 73 71 6c   pSubquery = sql
32064 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50  ite3SelectNew(pP
32065 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 34 5d  arse,0,yymsp[-4]
32066 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 30 2c 30 2c  .minor.yy65,0,0,
32067 30 2c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20 20  0,0,0,0,0);.    
32068 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
32069 36 35 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  65 = sqlite3SrcL
3206a 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72  istAppendFromTer
3206b 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  m(pParse,yymsp[-
3206c 36 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 30 2c  6].minor.yy65,0,
3206d 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  0,&yymsp[-2].min
3206e 6f 72 2e 79 79 30 2c 70 53 75 62 71 75 65 72 79  or.yy0,pSubquery
3206f 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
32070 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b 30 5d 2e  .yy132,yymsp[0].
32071 6d 69 6e 6f 72 2e 79 79 34 30 38 29 3b 0a 20 20  minor.yy408);.  
32072 20 20 7d 0a 20 20 7d 0a 20 20 20 20 20 20 20 20    }.  }.        
32073 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32074 65 20 31 33 36 3a 20 2f 2a 20 64 62 6e 6d 20 3a  e 136: /* dbnm :
32075 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
32076 20 31 34 35 3a 20 2f 2a 20 69 6e 64 65 78 65 64   145: /* indexed
32077 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  _opt ::= */ yyte
32078 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32079 3d 31 34 35 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =145);.{yygotomi
3207a 6e 6f 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79 67  nor.yy0.z=0; yyg
3207b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 30  otominor.yy0.n=0
3207c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3207d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 38  ;.      case 138
3207e 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 3a 3a  : /* fullname ::
3207f 3d 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 79 79  = nm dbnm */.{yy
32080 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d  gotominor.yy65 =
32081 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
32082 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  ppend(pParse->db
32083 2c 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  ,0,&yymsp[-1].mi
32084 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
32085 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
32086 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32087 20 20 20 20 63 61 73 65 20 31 33 39 3a 20 2f 2a      case 139: /*
32088 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d   joinop ::= COMM
32089 41 7c 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f  A|JOIN */.{ yygo
3208a 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
3208b 4a 54 5f 49 4e 4e 45 52 3b 20 7d 0a 20 20 20 20  JT_INNER; }.    
3208c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3208d 20 63 61 73 65 20 31 34 30 3a 20 2f 2a 20 6a 6f   case 140: /* jo
3208e 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57  inop ::= JOIN_KW
3208f 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74   JOIN */.{ yygot
32090 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 73  ominor.yy328 = s
32091 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 70  qlite3JoinType(p
32092 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d  Parse,&yymsp[-1]
32093 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 30 29 3b  .minor.yy0,0,0);
32094 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
32095 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 31  ;.      case 141
32096 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20  : /* joinop ::= 
32097 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 20  JOIN_KW nm JOIN 
32098 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
32099 2e 79 79 33 32 38 20 3d 20 73 71 6c 69 74 65 33  .yy328 = sqlite3
3209a 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c  JoinType(pParse,
3209b 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
3209c 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  .yy0,&yymsp[-1].
3209d 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 20 7d 0a  minor.yy0,0); }.
3209e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3209f 20 20 20 20 20 63 61 73 65 20 31 34 32 3a 20 2f       case 142: /
320a0 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49  * joinop ::= JOI
320a1 4e 5f 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 20  N_KW nm nm JOIN 
320a2 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
320a3 2e 79 79 33 32 38 20 3d 20 73 71 6c 69 74 65 33  .yy328 = sqlite3
320a4 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c  JoinType(pParse,
320a5 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
320a6 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  .yy0,&yymsp[-2].
320a7 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
320a8 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-1].minor.yy0);
320a9 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
320aa 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 33  ;.      case 143
320ab 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20  : /* on_opt ::= 
320ac 4f 4e 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  ON expr */.     
320ad 20 63 61 73 65 20 31 35 34 3a 20 2f 2a 20 73 6f   case 154: /* so
320ae 72 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72 20  rtitem ::= expr 
320af 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
320b0 72 75 6c 65 6e 6f 3d 3d 31 35 34 29 3b 0a 20 20  ruleno==154);.  
320b1 20 20 20 20 63 61 73 65 20 31 36 31 3a 20 2f 2a      case 161: /*
320b2 20 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20   having_opt ::= 
320b3 48 41 56 49 4e 47 20 65 78 70 72 20 2a 2f 20 79  HAVING expr */ y
320b4 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
320b5 6e 6f 3d 3d 31 36 31 29 3b 0a 20 20 20 20 20 20  no==161);.      
320b6 63 61 73 65 20 31 36 38 3a 20 2f 2a 20 77 68 65  case 168: /* whe
320b7 72 65 5f 6f 70 74 20 3a 3a 3d 20 57 48 45 52 45  re_opt ::= WHERE
320b8 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63   expr */ yytestc
320b9 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36  ase(yyruleno==16
320ba 38 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  8);.      case 2
320bb 33 35 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65  35: /* case_else
320bc 20 3a 3a 3d 20 45 4c 53 45 20 65 78 70 72 20 2a   ::= ELSE expr *
320bd 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
320be 75 6c 65 6e 6f 3d 3d 32 33 35 29 3b 0a 20 20 20  uleno==235);.   
320bf 20 20 20 63 61 73 65 20 32 33 37 3a 20 2f 2a 20     case 237: /* 
320c0 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d  case_operand ::=
320c1 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63   expr */ yytestc
320c2 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33  ase(yyruleno==23
320c3 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  7);.{yygotominor
320c4 2e 79 79 31 33 32 20 3d 20 79 79 6d 73 70 5b 30  .yy132 = yymsp[0
320c5 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
320c6 78 70 72 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  xpr;}.        br
320c7 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
320c8 31 34 34 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a  144: /* on_opt :
320c9 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
320ca 20 31 36 30 3a 20 2f 2a 20 68 61 76 69 6e 67 5f   160: /* having_
320cb 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
320cc 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
320cd 31 36 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65  160);.      case
320ce 20 31 36 37 3a 20 2f 2a 20 77 68 65 72 65 5f 6f   167: /* where_o
320cf 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
320d0 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
320d1 36 37 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  67);.      case 
320d2 32 33 36 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73  236: /* case_els
320d3 65 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  e ::= */ yytestc
320d4 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33  ase(yyruleno==23
320d5 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  6);.      case 2
320d6 33 38 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72  38: /* case_oper
320d7 61 6e 64 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  and ::= */ yytes
320d8 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
320d9 32 33 38 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  238);.{yygotomin
320da 6f 72 2e 79 79 31 33 32 20 3d 20 30 3b 7d 0a 20  or.yy132 = 0;}. 
320db 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
320dc 20 20 20 20 63 61 73 65 20 31 34 37 3a 20 2f 2a      case 147: /*
320dd 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d   indexed_opt ::=
320de 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 2a 2f 0a   NOT INDEXED */.
320df 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30  {yygotominor.yy0
320e0 2e 7a 3d 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  .z=0; yygotomino
320e1 72 2e 79 79 30 2e 6e 3d 31 3b 7d 0a 20 20 20 20  r.yy0.n=1;}.    
320e2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
320e3 20 63 61 73 65 20 31 34 38 3a 20 2f 2a 20 75 73   case 148: /* us
320e4 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 55 53 49 4e  ing_opt ::= USIN
320e5 47 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20  G LP inscollist 
320e6 52 50 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  RP */.      case
320e7 20 31 38 30 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c   180: /* inscoll
320e8 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69  ist_opt ::= LP i
320e9 6e 73 63 6f 6c 6c 69 73 74 20 52 50 20 2a 2f 20  nscollist RP */ 
320ea 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
320eb 65 6e 6f 3d 3d 31 38 30 29 3b 0a 7b 79 79 67 6f  eno==180);.{yygo
320ec 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d 20  tominor.yy408 = 
320ed 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
320ee 79 79 34 30 38 3b 7d 0a 20 20 20 20 20 20 20 20  yy408;}.        
320ef 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
320f0 65 20 31 34 39 3a 20 2f 2a 20 75 73 69 6e 67 5f  e 149: /* using_
320f1 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20  opt ::= */.     
320f2 20 63 61 73 65 20 31 37 39 3a 20 2f 2a 20 69 6e   case 179: /* in
320f3 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  scollist_opt ::=
320f4 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
320f5 79 72 75 6c 65 6e 6f 3d 3d 31 37 39 29 3b 0a 7b  yruleno==179);.{
320f6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30  yygotominor.yy40
320f7 38 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  8 = 0;}.        
320f8 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
320f9 65 20 31 35 31 3a 20 2f 2a 20 6f 72 64 65 72 62  e 151: /* orderb
320fa 79 5f 6f 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20  y_opt ::= ORDER 
320fb 42 59 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20  BY sortlist */. 
320fc 20 20 20 20 20 63 61 73 65 20 31 35 39 3a 20 2f       case 159: /
320fd 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a  * groupby_opt ::
320fe 3d 20 47 52 4f 55 50 20 42 59 20 6e 65 78 70 72  = GROUP BY nexpr
320ff 6c 69 73 74 20 2a 2f 20 79 79 74 65 73 74 63 61  list */ yytestca
32100 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 39  se(yyruleno==159
32101 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  );.      case 23
32102 39 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 3a  9: /* exprlist :
32103 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20 2a 2f 20  := nexprlist */ 
32104 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32105 65 6e 6f 3d 3d 32 33 39 29 3b 0a 7b 79 79 67 6f  eno==239);.{yygo
32106 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 79  tominor.yy14 = y
32107 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32108 31 34 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  14;}.        bre
32109 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
3210a 35 32 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74 20  52: /* sortlist 
3210b 3a 3a 3d 20 73 6f 72 74 6c 69 73 74 20 43 4f 4d  ::= sortlist COM
3210c 4d 41 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74  MA sortitem sort
3210d 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 79 79 67  order */.{.  yyg
3210e 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20  otominor.yy14 = 
3210f 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
32110 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d  ppend(pParse,yym
32111 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
32112 34 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  4,yymsp[-1].mino
32113 72 2e 79 79 31 33 32 29 3b 0a 20 20 69 66 28 20  r.yy132);.  if( 
32114 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
32115 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
32116 79 31 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e  y14->a[yygotomin
32117 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72 2d 31  or.yy14->nExpr-1
32118 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  ].sortOrder = (u
32119 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  8)yymsp[0].minor
3211a 2e 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20 20  .yy328;.}.      
3211b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3211c 61 73 65 20 31 35 33 3a 20 2f 2a 20 73 6f 72 74  ase 153: /* sort
3211d 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74 65  list ::= sortite
3211e 6d 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b  m sortorder */.{
3211f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
32120 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  y14 = sqlite3Exp
32121 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
32122 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  se,0,yymsp[-1].m
32123 69 6e 6f 72 2e 79 79 31 33 32 29 3b 0a 20 20 69  inor.yy132);.  i
32124 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
32125 79 31 34 20 26 26 20 41 4c 57 41 59 53 28 79 79  y14 && ALWAYS(yy
32126 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e  gotominor.yy14->
32127 61 29 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  a) ) yygotominor
32128 2e 79 79 31 34 2d 3e 61 5b 30 5d 2e 73 6f 72 74  .yy14->a[0].sort
32129 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73  Order = (u8)yyms
3212a 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
3212b 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
3212c 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  k;.      case 15
3212d 35 3a 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20  5: /* sortorder 
3212e 3a 3a 3d 20 41 53 43 20 2a 2f 0a 20 20 20 20 20  ::= ASC */.     
3212f 20 63 61 73 65 20 31 35 37 3a 20 2f 2a 20 73 6f   case 157: /* so
32130 72 74 6f 72 64 65 72 20 3a 3a 3d 20 2a 2f 20 79  rtorder ::= */ y
32131 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32132 6e 6f 3d 3d 31 35 37 29 3b 0a 7b 79 79 67 6f 74  no==157);.{yygot
32133 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 53  ominor.yy328 = S
32134 51 4c 49 54 45 5f 53 4f 5f 41 53 43 3b 7d 0a 20  QLITE_SO_ASC;}. 
32135 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32136 20 20 20 20 63 61 73 65 20 31 35 36 3a 20 2f 2a      case 156: /*
32137 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 44   sortorder ::= D
32138 45 53 43 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ESC */.{yygotomi
32139 6e 6f 72 2e 79 79 33 32 38 20 3d 20 53 51 4c 49  nor.yy328 = SQLI
3213a 54 45 5f 53 4f 5f 44 45 53 43 3b 7d 0a 20 20 20  TE_SO_DESC;}.   
3213b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3213c 20 20 63 61 73 65 20 31 36 32 3a 20 2f 2a 20 6c    case 162: /* l
3213d 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  imit_opt ::= */.
3213e 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
3213f 37 36 2e 70 4c 69 6d 69 74 20 3d 20 30 3b 20 79  76.pLimit = 0; y
32140 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36  ygotominor.yy476
32141 2e 70 4f 66 66 73 65 74 20 3d 20 30 3b 7d 0a 20  .pOffset = 0;}. 
32142 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32143 20 20 20 20 63 61 73 65 20 31 36 33 3a 20 2f 2a      case 163: /*
32144 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c   limit_opt ::= L
32145 49 4d 49 54 20 65 78 70 72 20 2a 2f 0a 7b 79 79  IMIT expr */.{yy
32146 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e  gotominor.yy476.
32147 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 30  pLimit = yymsp[0
32148 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
32149 78 70 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  xpr; yygotominor
3214a 2e 79 79 34 37 36 2e 70 4f 66 66 73 65 74 20 3d  .yy476.pOffset =
3214b 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
3214c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
3214d 36 34 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74  64: /* limit_opt
3214e 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20   ::= LIMIT expr 
3214f 4f 46 46 53 45 54 20 65 78 70 72 20 2a 2f 0a 7b  OFFSET expr */.{
32150 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37  yygotominor.yy47
32151 36 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70  6.pLimit = yymsp
32152 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-2].minor.yy346
32153 2e 70 45 78 70 72 3b 20 79 79 67 6f 74 6f 6d 69  .pExpr; yygotomi
32154 6e 6f 72 2e 79 79 34 37 36 2e 70 4f 66 66 73 65  nor.yy476.pOffse
32155 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  t = yymsp[0].min
32156 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 7d  or.yy346.pExpr;}
32157 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32158 20 20 20 20 20 20 63 61 73 65 20 31 36 35 3a 20        case 165: 
32159 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d  /* limit_opt ::=
3215a 20 4c 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d   LIMIT expr COMM
3215b 41 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74  A expr */.{yygot
3215c 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4f 66  ominor.yy476.pOf
3215d 66 73 65 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  fset = yymsp[-2]
3215e 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
3215f 70 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pr; yygotominor.
32160 79 79 34 37 36 2e 70 4c 69 6d 69 74 20 3d 20 79  yy476.pLimit = y
32161 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32162 33 34 36 2e 70 45 78 70 72 3b 7d 0a 20 20 20 20  346.pExpr;}.    
32163 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32164 20 63 61 73 65 20 31 36 36 3a 20 2f 2a 20 63 6d   case 166: /* cm
32165 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f  d ::= DELETE FRO
32166 4d 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78  M fullname index
32167 65 64 5f 6f 70 74 20 77 68 65 72 65 5f 6f 70 74  ed_opt where_opt
32168 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53   */.{.  sqlite3S
32169 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28  rcListIndexedBy(
3216a 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32  pParse, yymsp[-2
3216b 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 26 79  ].minor.yy65, &y
3216c 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3216d 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  y0);.  sqlite3De
3216e 6c 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65 2c  leteFrom(pParse,
3216f 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
32170 79 79 36 35 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy65,yymsp[0].mi
32171 6e 6f 72 2e 79 79 31 33 32 29 3b 0a 7d 0a 20 20  nor.yy132);.}.  
32172 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32173 20 20 20 63 61 73 65 20 31 36 39 3a 20 2f 2a 20     case 169: /* 
32174 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f  cmd ::= UPDATE o
32175 72 63 6f 6e 66 20 66 75 6c 6c 6e 61 6d 65 20 69  rconf fullname i
32176 6e 64 65 78 65 64 5f 6f 70 74 20 53 45 54 20 73  ndexed_opt SET s
32177 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74  etlist where_opt
32178 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53   */.{.  sqlite3S
32179 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28  rcListIndexedBy(
3217a 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 34  pParse, yymsp[-4
3217b 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 26 79  ].minor.yy65, &y
3217c 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
3217d 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  y0);.  sqlite3Ex
3217e 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74  prListCheckLengt
3217f 68 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  h(pParse,yymsp[-
32180 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 22 73  1].minor.yy14,"s
32181 65 74 20 6c 69 73 74 22 29 3b 20 0a 20 20 73 71  et list"); .  sq
32182 6c 69 74 65 33 55 70 64 61 74 65 28 70 50 61 72  lite3Update(pPar
32183 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  se,yymsp[-4].min
32184 6f 72 2e 79 79 36 35 2c 79 79 6d 73 70 5b 2d 31  or.yy65,yymsp[-1
32185 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d  ].minor.yy14,yym
32186 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33  sp[0].minor.yy13
32187 32 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  2,yymsp[-5].mino
32188 72 2e 79 79 31 38 36 29 3b 0a 7d 0a 20 20 20 20  r.yy186);.}.    
32189 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3218a 20 63 61 73 65 20 31 37 30 3a 20 2f 2a 20 73 65   case 170: /* se
3218b 74 6c 69 73 74 20 3a 3a 3d 20 73 65 74 6c 69 73  tlist ::= setlis
3218c 74 20 43 4f 4d 4d 41 20 6e 6d 20 45 51 20 65 78  t COMMA nm EQ ex
3218d 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pr */.{.  yygoto
3218e 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c  minor.yy14 = sql
3218f 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
32190 6e 64 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  nd(pParse, yymsp
32191 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-4].minor.yy14,
32192 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
32193 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20  yy346.pExpr);.  
32194 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53  sqlite3ExprListS
32195 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 79  etName(pParse, y
32196 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c  ygotominor.yy14,
32197 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f   &yymsp[-2].mino
32198 72 2e 79 79 30 2c 20 31 29 3b 0a 7d 0a 20 20 20  r.yy0, 1);.}.   
32199 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3219a 20 20 63 61 73 65 20 31 37 31 3a 20 2f 2a 20 73    case 171: /* s
3219b 65 74 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 45 51  etlist ::= nm EQ
3219c 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67   expr */.{.  yyg
3219d 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20  otominor.yy14 = 
3219e 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
3219f 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c  ppend(pParse, 0,
321a0 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
321a1 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20  yy346.pExpr);.  
321a2 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53  sqlite3ExprListS
321a3 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 79  etName(pParse, y
321a4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c  ygotominor.yy14,
321a5 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f   &yymsp[-2].mino
321a6 72 2e 79 79 30 2c 20 31 29 3b 0a 7d 0a 20 20 20  r.yy0, 1);.}.   
321a7 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
321a8 20 20 63 61 73 65 20 31 37 32 3a 20 2f 2a 20 63    case 172: /* c
321a9 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
321aa 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20  d INTO fullname 
321ab 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56  inscollist_opt V
321ac 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73  ALUES LP itemlis
321ad 74 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  t RP */.{sqlite3
321ae 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79  Insert(pParse, y
321af 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
321b0 79 36 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y65, yymsp[-1].m
321b1 69 6e 6f 72 2e 79 79 31 34 2c 20 30 2c 20 79 79  inor.yy14, 0, yy
321b2 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
321b3 34 30 38 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d  408, yymsp[-7].m
321b4 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20 20  inor.yy186);}.  
321b5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
321b6 20 20 20 63 61 73 65 20 31 37 33 3a 20 2f 2a 20     case 173: /* 
321b7 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
321b8 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
321b9 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
321ba 73 65 6c 65 63 74 20 2a 2f 0a 7b 73 71 6c 69 74  select */.{sqlit
321bb 65 33 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c  e3Insert(pParse,
321bc 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
321bd 2e 79 79 36 35 2c 20 30 2c 20 79 79 6d 73 70 5b  .yy65, 0, yymsp[
321be 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 79 79  0].minor.yy3, yy
321bf 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
321c0 34 30 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  408, yymsp[-4].m
321c1 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20 20  inor.yy186);}.  
321c2 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
321c3 20 20 20 63 61 73 65 20 31 37 34 3a 20 2f 2a 20     case 174: /* 
321c4 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
321c5 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
321c6 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
321c7 44 45 46 41 55 4c 54 20 56 41 4c 55 45 53 20 2a  DEFAULT VALUES *
321c8 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74  /.{sqlite3Insert
321c9 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
321ca 33 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 30  3].minor.yy65, 0
321cb 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  , 0, yymsp[-2].m
321cc 69 6e 6f 72 2e 79 79 34 30 38 2c 20 79 79 6d 73  inor.yy408, yyms
321cd 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38  p[-5].minor.yy18
321ce 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  6);}.        bre
321cf 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
321d0 37 35 3a 20 2f 2a 20 69 6e 73 65 72 74 5f 63 6d  75: /* insert_cm
321d1 64 20 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72 63  d ::= INSERT orc
321d2 6f 6e 66 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  onf */.{yygotomi
321d3 6e 6f 72 2e 79 79 31 38 36 20 3d 20 79 79 6d 73  nor.yy186 = yyms
321d4 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36  p[0].minor.yy186
321d5 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
321d6 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 36  ;.      case 176
321d7 3a 20 2f 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20  : /* insert_cmd 
321d8 3a 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a 7b  ::= REPLACE */.{
321d9 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38  yygotominor.yy18
321da 36 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d  6 = OE_Replace;}
321db 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
321dc 20 20 20 20 20 20 63 61 73 65 20 31 37 37 3a 20        case 177: 
321dd 2f 2a 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20  /* itemlist ::= 
321de 69 74 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65  itemlist COMMA e
321df 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
321e0 65 20 32 34 31 3a 20 2f 2a 20 6e 65 78 70 72 6c  e 241: /* nexprl
321e1 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73  ist ::= nexprlis
321e2 74 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 20  t COMMA expr */ 
321e3 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
321e4 65 6e 6f 3d 3d 32 34 31 29 3b 0a 7b 79 79 67 6f  eno==241);.{yygo
321e5 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73  tominor.yy14 = s
321e6 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
321e7 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
321e8 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-2].minor.yy14
321e9 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
321ea 79 79 33 34 36 2e 70 45 78 70 72 29 3b 7d 0a 20  yy346.pExpr);}. 
321eb 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
321ec 20 20 20 20 63 61 73 65 20 31 37 38 3a 20 2f 2a      case 178: /*
321ed 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65 78   itemlist ::= ex
321ee 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
321ef 20 32 34 32 3a 20 2f 2a 20 6e 65 78 70 72 6c 69   242: /* nexprli
321f0 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 20 79  st ::= expr */ y
321f1 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
321f2 6e 6f 3d 3d 32 34 32 29 3b 0a 7b 79 79 67 6f 74  no==242);.{yygot
321f3 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71  ominor.yy14 = sq
321f4 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
321f5 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 79 79 6d  end(pParse,0,yym
321f6 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
321f7 36 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20  6.pExpr);}.     
321f8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
321f9 63 61 73 65 20 31 38 31 3a 20 2f 2a 20 69 6e 73  case 181: /* ins
321fa 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 69 6e 73 63  collist ::= insc
321fb 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20  ollist COMMA nm 
321fc 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
321fd 79 79 34 30 38 20 3d 20 73 71 6c 69 74 65 33 49  yy408 = sqlite3I
321fe 64 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  dListAppend(pPar
321ff 73 65 2d 3e 64 62 2c 79 79 6d 73 70 5b 2d 32 5d  se->db,yymsp[-2]
32200 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 26 79 79  .minor.yy408,&yy
32201 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32202 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
32203 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38  k;.      case 18
32204 32 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74  2: /* inscollist
32205 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b 79 79 67 6f   ::= nm */.{yygo
32206 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d 20  tominor.yy408 = 
32207 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70  sqlite3IdListApp
32208 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30  end(pParse->db,0
32209 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
3220a 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
3220b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3220c 65 20 31 38 33 3a 20 2f 2a 20 65 78 70 72 20 3a  e 183: /* expr :
3220d 3a 3d 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20  := term */.     
3220e 20 63 61 73 65 20 32 31 31 3a 20 2f 2a 20 65 73   case 211: /* es
3220f 63 61 70 65 20 3a 3a 3d 20 45 53 43 41 50 45 20  cape ::= ESCAPE 
32210 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61  expr */ yytestca
32211 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 31 31  se(yyruleno==211
32212 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  );.{yygotominor.
32213 79 79 33 34 36 20 3d 20 79 79 6d 73 70 5b 30 5d  yy346 = yymsp[0]
32214 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 3b 7d 0a 20  .minor.yy346;}. 
32215 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32216 20 20 20 20 63 61 73 65 20 31 38 34 3a 20 2f 2a      case 184: /*
32217 20 65 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70   expr ::= LP exp
32218 72 20 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  r RP */.{yygotom
32219 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3221a 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
3221b 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 20  or.yy346.pExpr; 
3221c 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
3221d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73  inor.yy346,&yyms
3221e 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
3221f 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
32220 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
32221 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32222 20 31 38 35 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a   185: /* term ::
32223 3d 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20  = NULL */.      
32224 63 61 73 65 20 31 39 30 3a 20 2f 2a 20 74 65 72  case 190: /* ter
32225 6d 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c  m ::= INTEGER|FL
32226 4f 41 54 7c 42 4c 4f 42 20 2a 2f 20 79 79 74 65  OAT|BLOB */ yyte
32227 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32228 3d 31 39 30 29 3b 0a 20 20 20 20 20 20 63 61 73  =190);.      cas
32229 65 20 31 39 31 3a 20 2f 2a 20 74 65 72 6d 20 3a  e 191: /* term :
3222a 3a 3d 20 53 54 52 49 4e 47 20 2a 2f 20 79 79 74  := STRING */ yyt
3222b 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3222c 3d 3d 31 39 31 29 3b 0a 7b 73 70 61 6e 45 78 70  ==191);.{spanExp
3222d 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r(&yygotominor.y
3222e 79 33 34 36 2c 20 70 50 61 72 73 65 2c 20 79 79  y346, pParse, yy
3222f 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 20 26 79  msp[0].major, &y
32230 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32231 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
32232 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
32233 38 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  86: /* expr ::= 
32234 69 64 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  id */.      case
32235 20 31 38 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   187: /* expr ::
32236 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 20 79 79 74  = JOIN_KW */ yyt
32237 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32238 3d 3d 31 38 37 29 3b 0a 7b 73 70 61 6e 45 78 70  ==187);.{spanExp
32239 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r(&yygotominor.y
3223a 79 33 34 36 2c 20 70 50 61 72 73 65 2c 20 54 4b  y346, pParse, TK
3223b 5f 49 44 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  _ID, &yymsp[0].m
3223c 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
3223d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3223e 20 63 61 73 65 20 31 38 38 3a 20 2f 2a 20 65 78   case 188: /* ex
3223f 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d  pr ::= nm DOT nm
32240 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 74 65   */.{.  Expr *te
32241 6d 70 31 20 3d 20 73 71 6c 69 74 65 33 50 45 78  mp1 = sqlite3PEx
32242 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44  pr(pParse, TK_ID
32243 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d  , 0, 0, &yymsp[-
32244 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  2].minor.yy0);. 
32245 20 45 78 70 72 20 2a 74 65 6d 70 32 20 3d 20 73   Expr *temp2 = s
32246 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
32247 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
32248 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
32249 2e 79 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  .yy0);.  yygotom
3224a 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3224b 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
3224c 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20  pParse, TK_DOT, 
3224d 74 65 6d 70 31 2c 20 74 65 6d 70 32 2c 20 30 29  temp1, temp2, 0)
3224e 3b 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67  ;.  spanSet(&yyg
3224f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26  otominor.yy346,&
32250 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
32251 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
32252 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
32253 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32254 20 63 61 73 65 20 31 38 39 3a 20 2f 2a 20 65 78   case 189: /* ex
32255 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d  pr ::= nm DOT nm
32256 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45   DOT nm */.{.  E
32257 78 70 72 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c  xpr *temp1 = sql
32258 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32259 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
3225a 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
3225b 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65  yy0);.  Expr *te
3225c 6d 70 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78  mp2 = sqlite3PEx
3225d 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44  pr(pParse, TK_ID
3225e 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d  , 0, 0, &yymsp[-
3225f 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  2].minor.yy0);. 
32260 20 45 78 70 72 20 2a 74 65 6d 70 33 20 3d 20 73   Expr *temp3 = s
32261 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
32262 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
32263 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
32264 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74  .yy0);.  Expr *t
32265 65 6d 70 34 20 3d 20 73 71 6c 69 74 65 33 50 45  emp4 = sqlite3PE
32266 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
32267 4f 54 2c 20 74 65 6d 70 32 2c 20 74 65 6d 70 33  OT, temp2, temp3
32268 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  , 0);.  yygotomi
32269 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
3226a 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
3226b 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74  Parse, TK_DOT, t
3226c 65 6d 70 31 2c 20 74 65 6d 70 34 2c 20 30 29 3b  emp1, temp4, 0);
3226d 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f  .  spanSet(&yygo
3226e 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79  tominor.yy346,&y
3226f 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
32270 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
32271 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
32272 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32273 63 61 73 65 20 31 39 32 3a 20 2f 2a 20 65 78 70  case 192: /* exp
32274 72 20 3a 3a 3d 20 52 45 47 49 53 54 45 52 20 2a  r ::= REGISTER *
32275 2f 0a 7b 0a 20 20 2f 2a 20 57 68 65 6e 20 64 6f  /.{.  /* When do
32276 69 6e 67 20 61 20 6e 65 73 74 65 64 20 70 61 72  ing a nested par
32277 73 65 2c 20 6f 6e 65 20 63 61 6e 20 69 6e 63 6c  se, one can incl
32278 75 64 65 20 74 65 72 6d 73 20 69 6e 20 61 6e 20  ude terms in an 
32279 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20  expression.  ** 
3227a 74 68 61 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 74  that look like t
3227b 68 69 73 3a 20 20 20 23 31 20 23 32 20 2e 2e 2e  his:   #1 #2 ...
3227c 20 20 54 68 65 73 65 20 74 65 72 6d 73 20 72 65    These terms re
3227d 66 65 72 20 74 6f 20 72 65 67 69 73 74 65 72 73  fer to registers
3227e 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 76 69 72  .  ** in the vir
3227f 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 23  tual machine.  #
32280 4e 20 69 73 20 74 68 65 20 4e 2d 74 68 20 72 65  N is the N-th re
32281 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20 69 66 28  gister. */.  if(
32282 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
32283 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
32284 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
32285 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20  , "near \"%T\": 
32286 73 79 6e 74 61 78 20 65 72 72 6f 72 22 2c 20 26  syntax error", &
32287 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32288 79 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  y0);.    yygotom
32289 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3228a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
3228b 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
3228c 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
3228d 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
3228e 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 30  , TK_REGISTER, 0
3228f 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  , 0, &yymsp[0].m
32290 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20 69  inor.yy0);.    i
32291 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
32292 79 33 34 36 2e 70 45 78 70 72 20 29 20 73 71 6c  y346.pExpr ) sql
32293 69 74 65 33 47 65 74 49 6e 74 33 32 28 26 79 79  ite3GetInt32(&yy
32294 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32295 2e 7a 5b 31 5d 2c 20 26 79 79 67 6f 74 6f 6d 69  .z[1], &yygotomi
32296 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d  nor.yy346.pExpr-
32297 3e 69 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20  >iTable);.  }.  
32298 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
32299 69 6e 6f 72 2e 79 79 33 34 36 2c 20 26 79 79 6d  inor.yy346, &yym
3229a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
3229b 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
3229c 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
3229d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3229e 73 65 20 31 39 33 3a 20 2f 2a 20 65 78 70 72 20  se 193: /* expr 
3229f 3a 3a 3d 20 56 41 52 49 41 42 4c 45 20 2a 2f 0a  ::= VARIABLE */.
322a0 7b 0a 20 20 73 70 61 6e 45 78 70 72 28 26 79 79  {.  spanExpr(&yy
322a1 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
322a2 20 70 50 61 72 73 65 2c 20 54 4b 5f 56 41 52 49   pParse, TK_VARI
322a3 41 42 4c 45 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  ABLE, &yymsp[0].
322a4 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71  minor.yy0);.  sq
322a5 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56  lite3ExprAssignV
322a6 61 72 4e 75 6d 62 65 72 28 70 50 61 72 73 65 2c  arNumber(pParse,
322a7 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
322a8 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 73 70 61  46.pExpr);.  spa
322a9 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  nSet(&yygotomino
322aa 72 2e 79 79 33 34 36 2c 20 26 79 79 6d 73 70 5b  r.yy346, &yymsp[
322ab 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  0].minor.yy0, &y
322ac 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
322ad 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
322ae 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
322af 31 39 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  194: /* expr ::=
322b0 20 65 78 70 72 20 43 4f 4c 4c 41 54 45 20 69 64   expr COLLATE id
322b1 73 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  s */.{.  yygotom
322b2 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
322b3 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 53 65   = sqlite3ExprSe
322b4 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20 79 79  tColl(pParse, yy
322b5 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
322b6 33 34 36 2e 70 45 78 70 72 2c 20 26 79 79 6d 73  346.pExpr, &yyms
322b7 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
322b8 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
322b9 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79  y346.zStart = yy
322ba 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
322bb 33 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20 79 79  346.zStart;.  yy
322bc 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
322bd 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d  zEnd = &yymsp[0]
322be 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
322bf 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
322c0 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  n];.}.        br
322c1 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
322c2 31 39 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  195: /* expr ::=
322c3 20 43 41 53 54 20 4c 50 20 65 78 70 72 20 41 53   CAST LP expr AS
322c4 20 74 79 70 65 74 6f 6b 65 6e 20 52 50 20 2a 2f   typetoken RP */
322c5 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
322c6 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
322c7 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
322c8 73 65 2c 20 54 4b 5f 43 41 53 54 2c 20 79 79 6d  se, TK_CAST, yym
322c9 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-3].minor.yy3
322ca 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 26 79 79  46.pExpr, 0, &yy
322cb 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
322cc 30 29 3b 0a 20 20 73 70 61 6e 53 65 74 28 26 79  0);.  spanSet(&y
322cd 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
322ce 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  ,&yymsp[-5].mino
322cf 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
322d0 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
322d1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
322d2 20 20 20 63 61 73 65 20 31 39 36 3a 20 2f 2a 20     case 196: /* 
322d3 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 64  expr ::= ID LP d
322d4 69 73 74 69 6e 63 74 20 65 78 70 72 6c 69 73 74  istinct exprlist
322d5 20 52 50 20 2a 2f 0a 7b 0a 20 20 69 66 28 20 79   RP */.{.  if( y
322d6 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
322d7 79 31 34 20 26 26 20 79 79 6d 73 70 5b 2d 31 5d  y14 && yymsp[-1]
322d8 2e 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45 78  .minor.yy14->nEx
322d9 70 72 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61  pr>pParse->db->a
322da 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
322db 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 5d  IT_FUNCTION_ARG]
322dc 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
322dd 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
322de 22 74 6f 6f 20 6d 61 6e 79 20 61 72 67 75 6d 65  "too many argume
322df 6e 74 73 20 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  nts on function 
322e0 25 54 22 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e  %T", &yymsp[-4].
322e1 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a  minor.yy0);.  }.
322e2 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
322e3 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
322e4 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28  te3ExprFunction(
322e5 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31  pParse, yymsp[-1
322e6 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79  ].minor.yy14, &y
322e7 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
322e8 79 30 29 3b 0a 20 20 73 70 61 6e 53 65 74 28 26  y0);.  spanSet(&
322e9 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
322ea 36 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  6,&yymsp[-4].min
322eb 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
322ec 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69  .minor.yy0);.  i
322ed 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  f( yymsp[-2].min
322ee 6f 72 2e 79 79 33 32 38 20 26 26 20 79 79 67 6f  or.yy328 && yygo
322ef 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
322f0 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74  xpr ){.    yygot
322f1 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
322f2 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  pr->flags |= EP_
322f3 44 69 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 7d 0a  Distinct;.  }.}.
322f4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
322f5 20 20 20 20 20 63 61 73 65 20 31 39 37 3a 20 2f       case 197: /
322f6 2a 20 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50  * expr ::= ID LP
322f7 20 53 54 41 52 20 52 50 20 2a 2f 0a 7b 0a 20 20   STAR RP */.{.  
322f8 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
322f9 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
322fa 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50  3ExprFunction(pP
322fb 61 72 73 65 2c 20 30 2c 20 26 79 79 6d 73 70 5b  arse, 0, &yymsp[
322fc 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -3].minor.yy0);.
322fd 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74    spanSet(&yygot
322fe 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79  ominor.yy346,&yy
322ff 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
32300 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
32301 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
32302 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32303 61 73 65 20 31 39 38 3a 20 2f 2a 20 74 65 72 6d  ase 198: /* term
32304 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57 20 2a 2f   ::= CTIME_KW */
32305 0a 7b 0a 20 20 2f 2a 20 54 68 65 20 43 55 52 52  .{.  /* The CURR
32306 45 4e 54 5f 54 49 4d 45 2c 20 43 55 52 52 45 4e  ENT_TIME, CURREN
32307 54 5f 44 41 54 45 2c 20 61 6e 64 20 43 55 52 52  T_DATE, and CURR
32308 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50 20 76 61  ENT_TIMESTAMP va
32309 6c 75 65 73 20 61 72 65 0a 20 20 2a 2a 20 74 72  lues are.  ** tr
3230a 65 61 74 65 64 20 61 73 20 66 75 6e 63 74 69 6f  eated as functio
3230b 6e 73 20 74 68 61 74 20 72 65 74 75 72 6e 20 63  ns that return c
3230c 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 79 79  onstants */.  yy
3230d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
3230e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  pExpr = sqlite3E
3230f 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  xprFunction(pPar
32310 73 65 2c 20 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  se, 0,&yymsp[0].
32311 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66  minor.yy0);.  if
32312 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
32313 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20  346.pExpr ){.   
32314 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32315 34 36 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54  46.pExpr->op = T
32316 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3b 20 20 0a  K_CONST_FUNC;  .
32317 20 20 7d 0a 20 20 73 70 61 6e 53 65 74 28 26 79    }.  spanSet(&y
32318 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32319 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
3231a 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d  r.yy0, &yymsp[0]
3231b 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
3231c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3231d 20 20 20 20 63 61 73 65 20 31 39 39 3a 20 2f 2a      case 199: /*
3231e 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41   expr ::= expr A
3231f 4e 44 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  ND expr */.     
32320 20 63 61 73 65 20 32 30 30 3a 20 2f 2a 20 65 78   case 200: /* ex
32321 70 72 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65  pr ::= expr OR e
32322 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  xpr */ yytestcas
32323 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 30 29  e(yyruleno==200)
32324 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 31  ;.      case 201
32325 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
32326 70 72 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65  pr LT|GT|GE|LE e
32327 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  xpr */ yytestcas
32328 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 31 29  e(yyruleno==201)
32329 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 32  ;.      case 202
3232a 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
3232b 70 72 20 45 51 7c 4e 45 20 65 78 70 72 20 2a 2f  pr EQ|NE expr */
3232c 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3232d 6c 65 6e 6f 3d 3d 32 30 32 29 3b 0a 20 20 20 20  leno==202);.    
3232e 20 20 63 61 73 65 20 32 30 33 3a 20 2f 2a 20 65    case 203: /* e
3232f 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54  xpr ::= expr BIT
32330 41 4e 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54  AND|BITOR|LSHIFT
32331 7c 52 53 48 49 46 54 20 65 78 70 72 20 2a 2f 20  |RSHIFT expr */ 
32332 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32333 65 6e 6f 3d 3d 32 30 33 29 3b 0a 20 20 20 20 20  eno==203);.     
32334 20 63 61 73 65 20 32 30 34 3a 20 2f 2a 20 65 78   case 204: /* ex
32335 70 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53  pr ::= expr PLUS
32336 7c 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f 20 79  |MINUS expr */ y
32337 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32338 6e 6f 3d 3d 32 30 34 29 3b 0a 20 20 20 20 20 20  no==204);.      
32339 63 61 73 65 20 32 30 35 3a 20 2f 2a 20 65 78 70  case 205: /* exp
3233a 72 20 3a 3a 3d 20 65 78 70 72 20 53 54 41 52 7c  r ::= expr STAR|
3233b 53 4c 41 53 48 7c 52 45 4d 20 65 78 70 72 20 2a  SLASH|REM expr *
3233c 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3233d 75 6c 65 6e 6f 3d 3d 32 30 35 29 3b 0a 20 20 20  uleno==205);.   
3233e 20 20 20 63 61 73 65 20 32 30 36 3a 20 2f 2a 20     case 206: /* 
3233f 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f  expr ::= expr CO
32340 4e 43 41 54 20 65 78 70 72 20 2a 2f 20 79 79 74  NCAT expr */ yyt
32341 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32342 3d 3d 32 30 36 29 3b 0a 7b 73 70 61 6e 42 69 6e  ==206);.{spanBin
32343 61 72 79 45 78 70 72 28 26 79 79 67 6f 74 6f 6d  aryExpr(&yygotom
32344 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73  inor.yy346,pPars
32345 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f  e,yymsp[-1].majo
32346 72 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  r,&yymsp[-2].min
32347 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b  or.yy346,&yymsp[
32348 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b  0].minor.yy346);
32349 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3234a 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 37 3a  .      case 207:
3234b 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c   /* likeop ::= L
3234c 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  IKE_KW */.      
3234d 63 61 73 65 20 32 30 39 3a 20 2f 2a 20 6c 69 6b  case 209: /* lik
3234e 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48 20 2a 2f  eop ::= MATCH */
3234f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32350 6c 65 6e 6f 3d 3d 32 30 39 29 3b 0a 7b 79 79 67  leno==209);.{yyg
32351 6f 74 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 65 4f  otominor.yy96.eO
32352 70 65 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b  perator = yymsp[
32353 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79  0].minor.yy0; yy
32354 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e  gotominor.yy96.n
32355 6f 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  ot = 0;}.       
32356 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32357 73 65 20 32 30 38 3a 20 2f 2a 20 6c 69 6b 65 6f  se 208: /* likeo
32358 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b  p ::= NOT LIKE_K
32359 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  W */.      case 
3235a 32 31 30 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a  210: /* likeop :
3235b 3a 3d 20 4e 4f 54 20 4d 41 54 43 48 20 2a 2f 20  := NOT MATCH */ 
3235c 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3235d 65 6e 6f 3d 3d 32 31 30 29 3b 0a 7b 79 79 67 6f  eno==210);.{yygo
3235e 74 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 65 4f 70  tominor.yy96.eOp
3235f 65 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30  erator = yymsp[0
32360 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67  ].minor.yy0; yyg
32361 6f 74 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f  otominor.yy96.no
32362 74 20 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20 20  t = 1;}.        
32363 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32364 65 20 32 31 32 3a 20 2f 2a 20 65 73 63 61 70 65  e 212: /* escape
32365 20 3a 3a 3d 20 2a 2f 0a 7b 6d 65 6d 73 65 74 28   ::= */.{memset(
32366 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
32367 34 36 2c 30 2c 73 69 7a 65 6f 66 28 79 79 67 6f  46,0,sizeof(yygo
32368 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 29 29 3b  tominor.yy346));
32369 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3236a 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 33 3a  .      case 213:
3236b 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
3236c 72 20 6c 69 6b 65 6f 70 20 65 78 70 72 20 65 73  r likeop expr es
3236d 63 61 70 65 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  cape */.{.  Expr
3236e 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 20 20 70  List *pList;.  p
3236f 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
32370 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
32371 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 31 5d  rse,0, yymsp[-1]
32372 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
32373 70 72 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73  pr);.  pList = s
32374 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
32375 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73  pend(pParse,pLis
32376 74 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  t, yymsp[-3].min
32377 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b  or.yy346.pExpr);
32378 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e  .  if( yymsp[0].
32379 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
3237a 72 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d  r ){.    pList =
3237b 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3237c 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c  Append(pParse,pL
3237d 69 73 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  ist, yymsp[0].mi
3237e 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29  nor.yy346.pExpr)
3237f 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69  ;.  }.  yygotomi
32380 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
32381 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e  = sqlite3ExprFun
32382 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 4c  ction(pParse, pL
32383 69 73 74 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  ist, &yymsp[-2].
32384 6d 69 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65 72  minor.yy96.eOper
32385 61 74 6f 72 29 3b 0a 20 20 69 66 28 20 79 79 6d  ator);.  if( yym
32386 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 39  sp[-2].minor.yy9
32387 36 2e 6e 6f 74 20 29 20 79 79 67 6f 74 6f 6d 69  6.not ) yygotomi
32388 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
32389 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
3238a 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79  Parse, TK_NOT, y
3238b 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3238c 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20  .pExpr, 0, 0);. 
3238d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3238e 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73  46.zStart = yyms
3238f 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-3].minor.yy34
32390 36 2e 7a 53 74 61 72 74 3b 0a 20 20 79 79 67 6f  6.zStart;.  yygo
32391 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45  tominor.yy346.zE
32392 6e 64 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  nd = yymsp[-1].m
32393 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 3b  inor.yy346.zEnd;
32394 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e  .  if( yygotomin
32395 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29  or.yy346.pExpr )
32396 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32397 34 36 2e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  46.pExpr->flags 
32398 7c 3d 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 3b  |= EP_InfixFunc;
32399 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3239a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 34  ;.      case 214
3239b 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
3239c 70 72 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c  pr ISNULL|NOTNUL
3239d 4c 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79 50  L */.{spanUnaryP
3239e 6f 73 74 66 69 78 28 26 79 79 67 6f 74 6f 6d 69  ostfix(&yygotomi
3239f 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65  nor.yy346,pParse
323a0 2c 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c  ,yymsp[0].major,
323a1 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
323a2 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d  .yy346,&yymsp[0]
323a3 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
323a4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
323a5 20 20 20 63 61 73 65 20 32 31 35 3a 20 2f 2a 20     case 215: /* 
323a6 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4e 4f  expr ::= expr NO
323a7 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 73 70 61 6e 55  T NULL */.{spanU
323a8 6e 61 72 79 50 6f 73 74 66 69 78 28 26 79 79 67  naryPostfix(&yyg
323a9 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70  otominor.yy346,p
323aa 50 61 72 73 65 2c 54 4b 5f 4e 4f 54 4e 55 4c 4c  Parse,TK_NOTNULL
323ab 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
323ac 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 30  r.yy346,&yymsp[0
323ad 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
323ae 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
323af 20 20 20 20 63 61 73 65 20 32 31 36 3a 20 2f 2a      case 216: /*
323b0 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49   expr ::= expr I
323b1 53 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73 70  S expr */.{.  sp
323b2 61 6e 42 69 6e 61 72 79 45 78 70 72 28 26 79 79  anBinaryExpr(&yy
323b3 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
323b4 70 50 61 72 73 65 2c 54 4b 5f 49 53 2c 26 79 79  pParse,TK_IS,&yy
323b5 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
323b6 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  346,&yymsp[0].mi
323b7 6e 6f 72 2e 79 79 33 34 36 29 3b 0a 20 20 69 66  nor.yy346);.  if
323b8 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  ( pParse->db->ma
323b9 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 20 26  llocFailed==0  &
323ba 26 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  & yymsp[0].minor
323bb 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 6f 70  .yy346.pExpr->op
323bc 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20  ==TK_NULL ){.   
323bd 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
323be 34 36 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54  46.pExpr->op = T
323bf 4b 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a  K_ISNULL;.  }.}.
323c0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
323c1 20 20 20 20 20 63 61 73 65 20 32 31 37 3a 20 2f       case 217: /
323c2 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
323c3 49 53 20 4e 4f 54 20 65 78 70 72 20 2a 2f 0a 7b  IS NOT expr */.{
323c4 0a 20 20 73 70 61 6e 42 69 6e 61 72 79 45 78 70  .  spanBinaryExp
323c5 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r(&yygotominor.y
323c6 79 33 34 36 2c 70 50 61 72 73 65 2c 54 4b 5f 49  y346,pParse,TK_I
323c7 53 4e 4f 54 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e  SNOT,&yymsp[-3].
323c8 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d  minor.yy346,&yym
323c9 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
323ca 36 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  6);.  if( pParse
323cb 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
323cc 65 64 3d 3d 30 20 20 26 26 20 79 79 6d 73 70 5b  ed==0  && yymsp[
323cd 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  0].minor.yy346.p
323ce 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c  Expr->op==TK_NUL
323cf 4c 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  L ){.    yygotom
323d0 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
323d1 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 4f 54 4e 55 4c  ->op = TK_NOTNUL
323d2 4c 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20  L;.  }.}.       
323d3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
323d4 73 65 20 32 31 38 3a 20 2f 2a 20 65 78 70 72 20  se 218: /* expr 
323d5 3a 3a 3d 20 4e 4f 54 20 65 78 70 72 20 2a 2f 0a  ::= NOT expr */.
323d6 20 20 20 20 20 20 63 61 73 65 20 32 31 39 3a 20        case 219: 
323d7 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 42 49 54 4e  /* expr ::= BITN
323d8 4f 54 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  OT expr */ yytes
323d9 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
323da 32 31 39 29 3b 0a 7b 73 70 61 6e 55 6e 61 72 79  219);.{spanUnary
323db 50 72 65 66 69 78 28 26 79 79 67 6f 74 6f 6d 69  Prefix(&yygotomi
323dc 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65  nor.yy346,pParse
323dd 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72  ,yymsp[-1].major
323de 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
323df 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d 31  .yy346,&yymsp[-1
323e0 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
323e1 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
323e2 20 20 20 20 63 61 73 65 20 32 32 30 3a 20 2f 2a      case 220: /*
323e3 20 65 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53 20   expr ::= MINUS 
323e4 65 78 70 72 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61  expr */.{spanUna
323e5 72 79 50 72 65 66 69 78 28 26 79 79 67 6f 74 6f  ryPrefix(&yygoto
323e6 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72  minor.yy346,pPar
323e7 73 65 2c 54 4b 5f 55 4d 49 4e 55 53 2c 26 79 79  se,TK_UMINUS,&yy
323e8 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
323e9 34 36 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  46,&yymsp[-1].mi
323ea 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
323eb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
323ec 63 61 73 65 20 32 32 31 3a 20 2f 2a 20 65 78 70  case 221: /* exp
323ed 72 20 3a 3a 3d 20 50 4c 55 53 20 65 78 70 72 20  r ::= PLUS expr 
323ee 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79 50 72 65  */.{spanUnaryPre
323ef 66 69 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72  fix(&yygotominor
323f0 2e 79 79 33 34 36 2c 70 50 61 72 73 65 2c 54 4b  .yy346,pParse,TK
323f1 5f 55 50 4c 55 53 2c 26 79 79 6d 73 70 5b 30 5d  _UPLUS,&yymsp[0]
323f2 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79  .minor.yy346,&yy
323f3 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
323f4 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
323f5 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
323f6 32 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  24: /* expr ::= 
323f7 65 78 70 72 20 62 65 74 77 65 65 6e 5f 6f 70 20  expr between_op 
323f8 65 78 70 72 20 41 4e 44 20 65 78 70 72 20 2a 2f  expr AND expr */
323f9 0a 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  .{.  ExprList *p
323fa 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
323fb 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
323fc 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 32 5d  rse,0, yymsp[-2]
323fd 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
323fe 70 72 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73  pr);.  pList = s
323ff 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
32400 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73  pend(pParse,pLis
32401 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  t, yymsp[0].mino
32402 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
32403 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
32404 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
32405 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
32406 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20 79 79 6d   TK_BETWEEN, yym
32407 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-4].minor.yy3
32408 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  46.pExpr, 0, 0);
32409 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e  .  if( yygotomin
3240a 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29  or.yy346.pExpr )
3240b 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  {.    yygotomino
3240c 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 78  r.yy346.pExpr->x
3240d 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a  .pList = pList;.
3240e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
3240f 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
32410 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
32411 4c 69 73 74 29 3b 0a 20 20 7d 20 0a 20 20 69 66  List);.  } .  if
32412 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ( yymsp[-3].mino
32413 72 2e 79 79 33 32 38 20 29 20 79 79 67 6f 74 6f  r.yy328 ) yygoto
32414 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32415 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
32416 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c  (pParse, TK_NOT,
32417 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32418 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  46.pExpr, 0, 0);
32419 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3241a 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79  y346.zStart = yy
3241b 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
3241c 33 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20 79 79  346.zStart;.  yy
3241d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
3241e 7a 45 6e 64 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  zEnd = yymsp[0].
3241f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
32420 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
32421 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  k;.      case 22
32422 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  7: /* expr ::= e
32423 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 65 78 70  xpr in_op LP exp
32424 72 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  rlist RP */.{.  
32425 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
32426 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
32427 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
32428 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 34   TK_IN, yymsp[-4
32429 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
3242a 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xpr, 0, 0);.    
3242b 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
3242c 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20  yy346.pExpr ){. 
3242d 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72       yygotominor
3242e 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 78 2e  .yy346.pExpr->x.
3242f 70 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b 2d 31  pList = yymsp[-1
32430 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 3b 0a 20 20  ].minor.yy14;.  
32431 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
32432 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  etHeight(pParse,
32433 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32434 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d  46.pExpr);.    }
32435 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
32436 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
32437 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79  e(pParse->db, yy
32438 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
32439 31 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  14);.    }.    i
3243a 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  f( yymsp[-3].min
3243b 6f 72 2e 79 79 33 32 38 20 29 20 79 79 67 6f 74  or.yy328 ) yygot
3243c 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
3243d 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
3243e 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
3243f 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
32440 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29  346.pExpr, 0, 0)
32441 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  ;.    yygotomino
32442 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d  r.yy346.zStart =
32443 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
32444 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 3b 0a 20  .yy346.zStart;. 
32445 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
32446 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d  y346.zEnd = &yym
32447 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
32448 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  z[yymsp[0].minor
32449 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20  .yy0.n];.  }.   
3244a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3244b 20 20 63 61 73 65 20 32 32 38 3a 20 2f 2a 20 65    case 228: /* e
3244c 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65 63  xpr ::= LP selec
3244d 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 79 79  t RP */.{.    yy
3244e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
3244f 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
32450 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
32451 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c 20 30 29  SELECT, 0, 0, 0)
32452 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f  ;.    if( yygoto
32453 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32454 72 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74  r ){.      yygot
32455 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
32456 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20  pr->x.pSelect = 
32457 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
32458 79 79 33 3b 0a 20 20 20 20 20 20 45 78 70 72 53  yy3;.      ExprS
32459 65 74 50 72 6f 70 65 72 74 79 28 79 79 67 6f 74  etProperty(yygot
3245a 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
3245b 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
3245c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3245d 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
3245e 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
3245f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
32460 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
32461 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
32462 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
32463 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
32464 2e 79 79 33 29 3b 0a 20 20 20 20 7d 0a 20 20 20  .yy3);.    }.   
32465 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32466 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73  46.zStart = yyms
32467 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-2].minor.yy0.
32468 7a 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  z;.    yygotomin
32469 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20  or.yy346.zEnd = 
3246a 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3246b 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d  yy0.z[yymsp[0].m
3246c 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d  inor.yy0.n];.  }
3246d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3246e 20 20 20 20 20 20 63 61 73 65 20 32 32 39 3a 20        case 229: 
3246f 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
32470 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c 65 63 74   in_op LP select
32471 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67   RP */.{.    yyg
32472 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32473 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
32474 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
32475 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  N, yymsp[-4].min
32476 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
32477 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79  0, 0);.    if( y
32478 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32479 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20  .pExpr ){.      
3247a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3247b 36 2e 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  6.pExpr->x.pSele
3247c 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  ct = yymsp[-1].m
3247d 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 20 20 20 20  inor.yy3;.      
3247e 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28  ExprSetProperty(
3247f 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32480 36 2e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  6.pExpr, EP_xIsS
32481 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 73 71  elect);.      sq
32482 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
32483 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  ht(pParse, yygot
32484 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
32485 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  pr);.    }else{.
32486 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
32487 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  ectDelete(pParse
32488 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  ->db, yymsp[-1].
32489 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20 20 20 20  minor.yy3);.    
3248a 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b  }.    if( yymsp[
3248b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20  -3].minor.yy328 
3248c 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
3248d 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
3248e 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
3248f 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d   TK_NOT, yygotom
32490 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
32491 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 79 79 67  , 0, 0);.    yyg
32492 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
32493 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34  Start = yymsp[-4
32494 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  ].minor.yy346.zS
32495 74 61 72 74 3b 0a 20 20 20 20 79 79 67 6f 74 6f  tart;.    yygoto
32496 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
32497 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   = &yymsp[0].min
32498 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30  or.yy0.z[yymsp[0
32499 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a  ].minor.yy0.n];.
3249a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
3249b 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  k;.      case 23
3249c 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  0: /* expr ::= e
3249d 78 70 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e  xpr in_op nm dbn
3249e 6d 20 2a 2f 0a 7b 0a 20 20 20 20 53 72 63 4c 69  m */.{.    SrcLi
3249f 73 74 20 2a 70 53 72 63 20 3d 20 73 71 6c 69 74  st *pSrc = sqlit
324a0 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
324a1 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 26 79  pParse->db, 0,&y
324a2 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
324a3 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
324a4 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20 79 79 67  or.yy0);.    yyg
324a5 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
324a6 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
324a7 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
324a8 4e 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  N, yymsp[-3].min
324a9 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
324aa 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79  0, 0);.    if( y
324ab 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
324ac 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20  .pExpr ){.      
324ad 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
324ae 36 2e 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  6.pExpr->x.pSele
324af 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ct = sqlite3Sele
324b0 63 74 4e 65 77 28 70 50 61 72 73 65 2c 20 30 2c  ctNew(pParse, 0,
324b1 70 53 72 63 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30  pSrc,0,0,0,0,0,0
324b2 2c 30 29 3b 0a 20 20 20 20 20 20 45 78 70 72 53  ,0);.      ExprS
324b3 65 74 50 72 6f 70 65 72 74 79 28 79 79 67 6f 74  etProperty(yygot
324b4 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
324b5 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
324b6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
324b7 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
324b8 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
324b9 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
324ba 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
324bb 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
324bc 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
324bd 2c 20 70 53 72 63 29 3b 0a 20 20 20 20 7d 0a 20  , pSrc);.    }. 
324be 20 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32 5d     if( yymsp[-2]
324bf 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20 29 20 79  .minor.yy328 ) y
324c0 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
324c1 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
324c2 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
324c3 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  _NOT, yygotomino
324c4 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 30  r.yy346.pExpr, 0
324c5 2c 20 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f  , 0);.    yygoto
324c6 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61  minor.yy346.zSta
324c7 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  rt = yymsp[-3].m
324c8 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72  inor.yy346.zStar
324c9 74 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  t;.    yygotomin
324ca 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20  or.yy346.zEnd = 
324cb 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
324cc 79 30 2e 7a 20 3f 20 26 79 79 6d 73 70 5b 30 5d  y0.z ? &yymsp[0]
324cd 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
324ce 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
324cf 6e 5d 20 3a 20 26 79 79 6d 73 70 5b 2d 31 5d 2e  n] : &yymsp[-1].
324d0 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73  minor.yy0.z[yyms
324d1 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-1].minor.yy0.
324d2 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20  n];.  }.        
324d3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
324d4 65 20 32 33 31 3a 20 2f 2a 20 65 78 70 72 20 3a  e 231: /* expr :
324d5 3a 3d 20 45 58 49 53 54 53 20 4c 50 20 73 65 6c  := EXISTS LP sel
324d6 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20  ect RP */.{.    
324d7 45 78 70 72 20 2a 70 20 3d 20 79 79 67 6f 74 6f  Expr *p = yygoto
324d8 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
324d9 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
324da 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 58 49 53  (pParse, TK_EXIS
324db 54 53 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  TS, 0, 0, 0);.  
324dc 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20    if( p ){.     
324dd 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20   p->x.pSelect = 
324de 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
324df 79 79 33 3b 0a 20 20 20 20 20 20 45 78 70 72 53  yy3;.      ExprS
324e0 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  etProperty(p, EP
324e1 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a 20 20 20  _xIsSelect);.   
324e2 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65     sqlite3ExprSe
324e3 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  tHeight(pParse, 
324e4 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  p);.    }else{. 
324e5 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
324e6 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  ctDelete(pParse-
324e7 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  >db, yymsp[-1].m
324e8 69 6e 6f 72 2e 79 79 33 29 3b 0a 20 20 20 20 7d  inor.yy3);.    }
324e9 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
324ea 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20  .yy346.zStart = 
324eb 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
324ec 79 79 30 2e 7a 3b 0a 20 20 20 20 79 79 67 6f 74  yy0.z;.    yygot
324ed 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e  ominor.yy346.zEn
324ee 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  d = &yymsp[0].mi
324ef 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
324f0 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b  0].minor.yy0.n];
324f1 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  .  }.        bre
324f2 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
324f3 33 32 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  32: /* expr ::= 
324f4 43 41 53 45 20 63 61 73 65 5f 6f 70 65 72 61 6e  CASE case_operan
324f5 64 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20  d case_exprlist 
324f6 63 61 73 65 5f 65 6c 73 65 20 45 4e 44 20 2a 2f  case_else END */
324f7 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
324f8 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
324f9 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
324fa 73 65 2c 20 54 4b 5f 43 41 53 45 2c 20 79 79 6d  se, TK_CASE, yym
324fb 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
324fc 33 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  32, yymsp[-1].mi
324fd 6e 6f 72 2e 79 79 31 33 32 2c 20 30 29 3b 0a 20  nor.yy132, 0);. 
324fe 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
324ff 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a  .yy346.pExpr ){.
32500 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
32501 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 78 2e 70  yy346.pExpr->x.p
32502 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  List = yymsp[-2]
32503 2e 6d 69 6e 6f 72 2e 79 79 31 34 3b 0a 20 20 20  .minor.yy14;.   
32504 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
32505 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79  eight(pParse, yy
32506 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32507 70 45 78 70 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  pExpr);.  }else{
32508 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
32509 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
3250a 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 32 5d  e->db, yymsp[-2]
3250b 2e 6d 69 6e 6f 72 2e 79 79 31 34 29 3b 0a 20 20  .minor.yy14);.  
3250c 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  }.  yygotominor.
3250d 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79  yy346.zStart = y
3250e 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3250f 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  y0.z;.  yygotomi
32510 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d  nor.yy346.zEnd =
32511 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
32512 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e  .yy0.z[yymsp[0].
32513 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a  minor.yy0.n];.}.
32514 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32515 20 20 20 20 20 63 61 73 65 20 32 33 33 3a 20 2f       case 233: /
32516 2a 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20  * case_exprlist 
32517 3a 3a 3d 20 63 61 73 65 5f 65 78 70 72 6c 69 73  ::= case_exprlis
32518 74 20 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e  t WHEN expr THEN
32519 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67   expr */.{.  yyg
3251a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20  otominor.yy14 = 
3251b 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
3251c 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d  ppend(pParse,yym
3251d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
3251e 34 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  4, yymsp[-2].min
3251f 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b  or.yy346.pExpr);
32520 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
32521 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  y14 = sqlite3Exp
32522 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
32523 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  se,yygotominor.y
32524 79 31 34 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  y14, yymsp[0].mi
32525 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29  nor.yy346.pExpr)
32526 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
32527 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  k;.      case 23
32528 34 3a 20 2f 2a 20 63 61 73 65 5f 65 78 70 72 6c  4: /* case_exprl
32529 69 73 74 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70  ist ::= WHEN exp
3252a 72 20 54 48 45 4e 20 65 78 70 72 20 2a 2f 0a 7b  r THEN expr */.{
3252b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3252c 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  y14 = sqlite3Exp
3252d 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
3252e 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  se,0, yymsp[-2].
3252f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32530 72 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  r);.  yygotomino
32531 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
32532 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
32533 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f  Parse,yygotomino
32534 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b 30 5d  r.yy14, yymsp[0]
32535 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
32536 70 72 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  pr);.}.        b
32537 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32538 20 32 34 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   243: /* cmd ::=
32539 20 63 72 65 61 74 65 6b 77 20 75 6e 69 71 75 65   createkw unique
3253a 66 6c 61 67 20 49 4e 44 45 58 20 69 66 6e 6f 74  flag INDEX ifnot
3253b 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f  exists nm dbnm O
3253c 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74 20  N nm LP idxlist 
3253d 52 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  RP */.{.  sqlite
3253e 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61  3CreateIndex(pPa
3253f 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 36 5d 2e  rse, &yymsp[-6].
32540 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73  minor.yy0, &yyms
32541 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-5].minor.yy0,
32542 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
32543 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72         sqlite3Sr
32544 63 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  cListAppend(pPar
32545 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d 73 70 5b  se->db,0,&yymsp[
32546 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29  -3].minor.yy0,0)
32547 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
32548 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b 2d 39  r.yy14, yymsp[-9
32549 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 0a 20  ].minor.yy328,. 
3254a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3254b 20 20 20 20 20 26 79 79 6d 73 70 5b 2d 31 30 5d       &yymsp[-10]
3254c 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
3254d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
3254e 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 2c 20   SQLITE_SO_ASC, 
3254f 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e  yymsp[-7].minor.
32550 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20  yy328);.}.      
32551 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32552 61 73 65 20 32 34 34 3a 20 2f 2a 20 75 6e 69 71  ase 244: /* uniq
32553 75 65 66 6c 61 67 20 3a 3a 3d 20 55 4e 49 51 55  ueflag ::= UNIQU
32554 45 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  E */.      case 
32555 32 39 38 3a 20 2f 2a 20 72 61 69 73 65 74 79 70  298: /* raisetyp
32556 65 20 3a 3a 3d 20 41 42 4f 52 54 20 2a 2f 20 79  e ::= ABORT */ y
32557 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32558 6e 6f 3d 3d 32 39 38 29 3b 0a 7b 79 79 67 6f 74  no==298);.{yygot
32559 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f  ominor.yy328 = O
3255a 45 5f 41 62 6f 72 74 3b 7d 0a 20 20 20 20 20 20  E_Abort;}.      
3255b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3255c 61 73 65 20 32 34 35 3a 20 2f 2a 20 75 6e 69 71  ase 245: /* uniq
3255d 75 65 66 6c 61 67 20 3a 3a 3d 20 2a 2f 0a 7b 79  ueflag ::= */.{y
3255e 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
3255f 20 3d 20 4f 45 5f 4e 6f 6e 65 3b 7d 0a 20 20 20   = OE_None;}.   
32560 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32561 20 20 63 61 73 65 20 32 34 38 3a 20 2f 2a 20 69    case 248: /* i
32562 64 78 6c 69 73 74 20 3a 3a 3d 20 69 64 78 6c 69  dxlist ::= idxli
32563 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c  st COMMA nm coll
32564 61 74 65 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f  ate sortorder */
32565 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 30  .{.  Expr *p = 0
32566 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 31  ;.  if( yymsp[-1
32567 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20  ].minor.yy0.n>0 
32568 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  ){.    p = sqlit
32569 65 33 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64  e3Expr(pParse->d
3256a 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 29  b, TK_COLUMN, 0)
3256b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
3256c 72 53 65 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c  rSetColl(pParse,
3256d 20 70 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d   p, &yymsp[-1].m
3256e 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20  inor.yy0);.  }. 
3256f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
32570 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  4 = sqlite3ExprL
32571 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
32572 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
32573 2e 79 79 31 34 2c 20 70 29 3b 0a 20 20 73 71 6c  .yy14, p);.  sql
32574 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e  ite3ExprListSetN
32575 61 6d 65 28 70 50 61 72 73 65 2c 79 79 67 6f 74  ame(pParse,yygot
32576 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 26 79 79 6d  ominor.yy14,&yym
32577 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
32578 2c 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  ,1);.  sqlite3Ex
32579 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74  prListCheckLengt
3257a 68 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  h(pParse, yygoto
3257b 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 22 69 6e 64  minor.yy14, "ind
3257c 65 78 22 29 3b 0a 20 20 69 66 28 20 79 79 67 6f  ex");.  if( yygo
3257d 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 29 20 79  tominor.yy14 ) y
3257e 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d  ygotominor.yy14-
3257f 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  >a[yygotominor.y
32580 79 31 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f  y14->nExpr-1].so
32581 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79  rtOrder = (u8)yy
32582 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
32583 32 38 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  28;.}.        br
32584 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32585 32 34 39 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20  249: /* idxlist 
32586 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73  ::= nm collate s
32587 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20  ortorder */.{.  
32588 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20 69  Expr *p = 0;.  i
32589 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  f( yymsp[-1].min
3258a 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 7b 0a 20 20  or.yy0.n>0 ){.  
3258b 20 20 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78    p = sqlite3PEx
3258c 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 4f  pr(pParse, TK_CO
3258d 4c 55 4d 4e 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  LUMN, 0, 0, 0);.
3258e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
3258f 65 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20 70  etColl(pParse, p
32590 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  , &yymsp[-1].min
32591 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79  or.yy0);.  }.  y
32592 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20  ygotominor.yy14 
32593 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
32594 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
32595 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  , p);.  sqlite3E
32596 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70  xprListSetName(p
32597 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
32598 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d 73 70 5b  or.yy14, &yymsp[
32599 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 31  -2].minor.yy0, 1
3259a 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
3259b 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28  ListCheckLength(
3259c 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
3259d 6e 6f 72 2e 79 79 31 34 2c 20 22 69 6e 64 65 78  nor.yy14, "index
3259e 22 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f  ");.  if( yygoto
3259f 6d 69 6e 6f 72 2e 79 79 31 34 20 29 20 79 79 67  minor.yy14 ) yyg
325a0 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 61  otominor.yy14->a
325a1 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  [yygotominor.yy1
325a2 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74  4->nExpr-1].sort
325a3 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73  Order = (u8)yyms
325a4 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
325a5 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
325a6 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  k;.      case 25
325a7 30 3a 20 2f 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a  0: /* collate ::
325a8 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
325a9 72 2e 79 79 30 2e 7a 20 3d 20 30 3b 20 79 79 67  r.yy0.z = 0; yyg
325aa 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d  otominor.yy0.n =
325ab 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
325ac 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
325ad 35 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44  52: /* cmd ::= D
325ae 52 4f 50 20 49 4e 44 45 58 20 69 66 65 78 69 73  ROP INDEX ifexis
325af 74 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b  ts fullname */.{
325b0 73 71 6c 69 74 65 33 44 72 6f 70 49 6e 64 65 78  sqlite3DropIndex
325b1 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30  (pParse, yymsp[0
325b2 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 79 79  ].minor.yy65, yy
325b3 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
325b4 33 32 38 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  328);}.        b
325b5 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
325b6 20 32 35 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   253: /* cmd ::=
325b7 20 56 41 43 55 55 4d 20 2a 2f 0a 20 20 20 20 20   VACUUM */.     
325b8 20 63 61 73 65 20 32 35 34 3a 20 2f 2a 20 63 6d   case 254: /* cm
325b9 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d 20  d ::= VACUUM nm 
325ba 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
325bb 72 75 6c 65 6e 6f 3d 3d 32 35 34 29 3b 0a 7b 73  ruleno==254);.{s
325bc 71 6c 69 74 65 33 56 61 63 75 75 6d 28 70 50 61  qlite3Vacuum(pPa
325bd 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  rse);}.        b
325be 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
325bf 20 32 35 35 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   255: /* cmd ::=
325c0 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
325c1 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d  */.{sqlite3Pragm
325c2 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  a(pParse,&yymsp[
325c3 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -1].minor.yy0,&y
325c4 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
325c5 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20  0,0,0);}.       
325c6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
325c7 73 65 20 32 35 36 3a 20 2f 2a 20 63 6d 64 20 3a  se 256: /* cmd :
325c8 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
325c9 6d 20 45 51 20 6e 6d 6e 75 6d 20 2a 2f 0a 7b 73  m EQ nmnum */.{s
325ca 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61  qlite3Pragma(pPa
325cb 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  rse,&yymsp[-3].m
325cc 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
325cd 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -2].minor.yy0,&y
325ce 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
325cf 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  0,0);}.        b
325d0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
325d1 20 32 35 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   257: /* cmd ::=
325d2 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
325d3 4c 50 20 6e 6d 6e 75 6d 20 52 50 20 2a 2f 0a 7b  LP nmnum RP */.{
325d4 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50  sqlite3Pragma(pP
325d5 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e  arse,&yymsp[-4].
325d6 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
325d7 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-3].minor.yy0,&
325d8 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
325d9 79 79 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20  yy0,0);}.       
325da 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
325db 73 65 20 32 35 38 3a 20 2f 2a 20 63 6d 64 20 3a  se 258: /* cmd :
325dc 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
325dd 6d 20 45 51 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a  m EQ minus_num *
325de 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61  /.{sqlite3Pragma
325df 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
325e0 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  3].minor.yy0,&yy
325e1 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
325e2 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
325e3 72 2e 79 79 30 2c 31 29 3b 7d 0a 20 20 20 20 20  r.yy0,1);}.     
325e4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
325e5 63 61 73 65 20 32 35 39 3a 20 2f 2a 20 63 6d 64  case 259: /* cmd
325e6 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
325e7 62 6e 6d 20 4c 50 20 6d 69 6e 75 73 5f 6e 75 6d  bnm LP minus_num
325e8 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50   RP */.{sqlite3P
325e9 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79  ragma(pParse,&yy
325ea 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
325eb 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  0,&yymsp[-3].min
325ec 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31  or.yy0,&yymsp[-1
325ed 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 7d  ].minor.yy0,1);}
325ee 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
325ef 20 20 20 20 20 20 63 61 73 65 20 32 37 30 3a 20        case 270: 
325f0 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74  /* cmd ::= creat
325f1 65 6b 77 20 74 72 69 67 67 65 72 5f 64 65 63 6c  ekw trigger_decl
325f2 20 42 45 47 49 4e 20 74 72 69 67 67 65 72 5f 63   BEGIN trigger_c
325f3 6d 64 5f 6c 69 73 74 20 45 4e 44 20 2a 2f 0a 7b  md_list END */.{
325f4 0a 20 20 54 6f 6b 65 6e 20 61 6c 6c 3b 0a 20 20  .  Token all;.  
325f5 61 6c 6c 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 33  all.z = yymsp[-3
325f6 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20  ].minor.yy0.z;. 
325f7 20 61 6c 6c 2e 6e 20 3d 20 28 69 6e 74 29 28 79   all.n = (int)(y
325f8 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
325f9 30 2e 7a 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e  0.z - yymsp[-3].
325fa 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 20 2b 20 79  minor.yy0.z) + y
325fb 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
325fc 30 2e 6e 3b 0a 20 20 73 71 6c 69 74 65 33 46 69  0.n;.  sqlite3Fi
325fd 6e 69 73 68 54 72 69 67 67 65 72 28 70 50 61 72  nishTrigger(pPar
325fe 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se, yymsp[-1].mi
325ff 6e 6f 72 2e 79 79 34 37 33 2c 20 26 61 6c 6c 29  nor.yy473, &all)
32600 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
32601 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
32602 31 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 64 65  1: /* trigger_de
32603 63 6c 20 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47  cl ::= temp TRIG
32604 47 45 52 20 69 66 6e 6f 74 65 78 69 73 74 73 20  GER ifnotexists 
32605 6e 6d 20 64 62 6e 6d 20 74 72 69 67 67 65 72 5f  nm dbnm trigger_
32606 74 69 6d 65 20 74 72 69 67 67 65 72 5f 65 76 65  time trigger_eve
32607 6e 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66  nt ON fullname f
32608 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 77 68  oreach_clause wh
32609 65 6e 5f 63 6c 61 75 73 65 20 2a 2f 0a 7b 0a 20  en_clause */.{. 
3260a 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69   sqlite3BeginTri
3260b 67 67 65 72 28 70 50 61 72 73 65 2c 20 26 79 79  gger(pParse, &yy
3260c 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-7].minor.yy
3260d 30 2c 20 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69  0, &yymsp[-6].mi
3260e 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d  nor.yy0, yymsp[-
3260f 35 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 20  5].minor.yy328, 
32610 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
32611 79 79 33 37 38 2e 61 2c 20 79 79 6d 73 70 5b 2d  yy378.a, yymsp[-
32612 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 38 2e 62  4].minor.yy378.b
32613 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
32614 72 2e 79 79 36 35 2c 20 79 79 6d 73 70 5b 30 5d  r.yy65, yymsp[0]
32615 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 20 79 79  .minor.yy132, yy
32616 6d 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79  msp[-10].minor.y
32617 79 33 32 38 2c 20 79 79 6d 73 70 5b 2d 38 5d 2e  y328, yymsp[-8].
32618 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 20 20  minor.yy328);.  
32619 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20  yygotominor.yy0 
3261a 3d 20 28 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  = (yymsp[-6].min
3261b 6f 72 2e 79 79 30 2e 6e 3d 3d 30 3f 79 79 6d 73  or.yy0.n==0?yyms
3261c 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a  p[-7].minor.yy0:
3261d 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
3261e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
3261f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32620 65 20 32 37 32 3a 20 2f 2a 20 74 72 69 67 67 65  e 272: /* trigge
32621 72 5f 74 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52  r_time ::= BEFOR
32622 45 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  E */.      case 
32623 32 37 35 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  275: /* trigger_
32624 74 69 6d 65 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  time ::= */ yyte
32625 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32626 3d 32 37 35 29 3b 0a 7b 20 79 79 67 6f 74 6f 6d  =275);.{ yygotom
32627 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54 4b 5f  inor.yy328 = TK_
32628 42 45 46 4f 52 45 3b 20 7d 0a 20 20 20 20 20 20  BEFORE; }.      
32629 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3262a 61 73 65 20 32 37 33 3a 20 2f 2a 20 74 72 69 67  ase 273: /* trig
3262b 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41 46 54  ger_time ::= AFT
3262c 45 52 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  ER */.{ yygotomi
3262d 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54 4b 5f 41  nor.yy328 = TK_A
3262e 46 54 45 52 3b 20 20 7d 0a 20 20 20 20 20 20 20  FTER;  }.       
3262f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32630 73 65 20 32 37 34 3a 20 2f 2a 20 74 72 69 67 67  se 274: /* trigg
32631 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49 4e 53 54  er_time ::= INST
32632 45 41 44 20 4f 46 20 2a 2f 0a 7b 20 79 79 67 6f  EAD OF */.{ yygo
32633 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
32634 54 4b 5f 49 4e 53 54 45 41 44 3b 7d 0a 20 20 20  TK_INSTEAD;}.   
32635 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32636 20 20 63 61 73 65 20 32 37 36 3a 20 2f 2a 20 74    case 276: /* t
32637 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d  rigger_event ::=
32638 20 44 45 4c 45 54 45 7c 49 4e 53 45 52 54 20 2a   DELETE|INSERT *
32639 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 37  /.      case 277
3263a 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65  : /* trigger_eve
3263b 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 2a 2f  nt ::= UPDATE */
3263c 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3263d 6c 65 6e 6f 3d 3d 32 37 37 29 3b 0a 7b 79 79 67  leno==277);.{yyg
3263e 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 38 2e 61  otominor.yy378.a
3263f 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f   = yymsp[0].majo
32640 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r; yygotominor.y
32641 79 33 37 38 2e 62 20 3d 20 30 3b 7d 0a 20 20 20  y378.b = 0;}.   
32642 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32643 20 20 63 61 73 65 20 32 37 38 3a 20 2f 2a 20 74    case 278: /* t
32644 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d  rigger_event ::=
32645 20 55 50 44 41 54 45 20 4f 46 20 69 6e 73 63 6f   UPDATE OF insco
32646 6c 6c 69 73 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f  llist */.{yygoto
32647 6d 69 6e 6f 72 2e 79 79 33 37 38 2e 61 20 3d 20  minor.yy378.a = 
32648 54 4b 5f 55 50 44 41 54 45 3b 20 79 79 67 6f 74  TK_UPDATE; yygot
32649 6f 6d 69 6e 6f 72 2e 79 79 33 37 38 2e 62 20 3d  ominor.yy378.b =
3264a 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3264b 79 79 34 30 38 3b 7d 0a 20 20 20 20 20 20 20 20  yy408;}.        
3264c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3264d 65 20 32 38 31 3a 20 2f 2a 20 77 68 65 6e 5f 63  e 281: /* when_c
3264e 6c 61 75 73 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20  lause ::= */.   
3264f 20 20 20 63 61 73 65 20 33 30 33 3a 20 2f 2a 20     case 303: /* 
32650 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79  key_opt ::= */ y
32651 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32652 6e 6f 3d 3d 33 30 33 29 3b 0a 7b 20 79 79 67 6f  no==303);.{ yygo
32653 74 6f 6d 69 6e 6f 72 2e 79 79 31 33 32 20 3d 20  tominor.yy132 = 
32654 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0; }.        bre
32655 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
32656 38 32 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75  82: /* when_clau
32657 73 65 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72  se ::= WHEN expr
32658 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
32659 30 34 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a  04: /* key_opt :
3265a 3a 3d 20 4b 45 59 20 65 78 70 72 20 2a 2f 20 79  := KEY expr */ y
3265b 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3265c 6e 6f 3d 3d 33 30 34 29 3b 0a 7b 20 79 79 67 6f  no==304);.{ yygo
3265d 74 6f 6d 69 6e 6f 72 2e 79 79 31 33 32 20 3d 20  tominor.yy132 = 
3265e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3265f 79 33 34 36 2e 70 45 78 70 72 3b 20 7d 0a 20 20  y346.pExpr; }.  
32660 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32661 20 20 20 63 61 73 65 20 32 38 33 3a 20 2f 2a 20     case 283: /* 
32662 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
32663 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64   ::= trigger_cmd
32664 5f 6c 69 73 74 20 74 72 69 67 67 65 72 5f 63 6d  _list trigger_cm
32665 64 20 53 45 4d 49 20 2a 2f 0a 7b 0a 20 20 61 73  d SEMI */.{.  as
32666 73 65 72 74 28 20 79 79 6d 73 70 5b 2d 32 5d 2e  sert( yymsp[-2].
32667 6d 69 6e 6f 72 2e 79 79 34 37 33 21 3d 30 20 29  minor.yy473!=0 )
32668 3b 0a 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  ;.  yymsp[-2].mi
32669 6e 6f 72 2e 79 79 34 37 33 2d 3e 70 4c 61 73 74  nor.yy473->pLast
3266a 2d 3e 70 4e 65 78 74 20 3d 20 79 79 6d 73 70 5b  ->pNext = yymsp[
3266b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 3b  -1].minor.yy473;
3266c 0a 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  .  yymsp[-2].min
3266d 6f 72 2e 79 79 34 37 33 2d 3e 70 4c 61 73 74 20  or.yy473->pLast 
3266e 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
3266f 72 2e 79 79 34 37 33 3b 0a 20 20 79 79 67 6f 74  r.yy473;.  yygot
32670 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20 79  ominor.yy473 = y
32671 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32672 79 34 37 33 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y473;.}.        
32673 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32674 65 20 32 38 34 3a 20 2f 2a 20 74 72 69 67 67 65  e 284: /* trigge
32675 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74  r_cmd_list ::= t
32676 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 20  rigger_cmd SEMI 
32677 2a 2f 0a 7b 20 0a 20 20 61 73 73 65 72 74 28 20  */.{ .  assert( 
32678 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
32679 79 79 34 37 33 21 3d 30 20 29 3b 0a 20 20 79 79  yy473!=0 );.  yy
3267a 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3267b 34 37 33 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d  473->pLast = yym
3267c 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
3267d 37 33 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  73;.  yygotomino
3267e 72 2e 79 79 34 37 33 20 3d 20 79 79 6d 73 70 5b  r.yy473 = yymsp[
3267f 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 3b  -1].minor.yy473;
32680 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32681 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 36  ;.      case 286
32682 3a 20 2f 2a 20 74 72 6e 6d 20 3a 3a 3d 20 6e 6d  : /* trnm ::= nm
32683 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 79   DOT nm */.{.  y
32684 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d  ygotominor.yy0 =
32685 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
32686 79 79 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 72  yy0;.  sqlite3Er
32687 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
32688 20 20 20 20 20 20 20 20 22 71 75 61 6c 69 66 69          "qualifi
32689 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 61  ed table names a
3268a 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f  re not allowed o
3268b 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  n INSERT, UPDATE
3268c 2c 20 61 6e 64 20 44 45 4c 45 54 45 20 22 0a 20  , and DELETE ". 
3268d 20 20 20 20 20 20 20 22 73 74 61 74 65 6d 65 6e         "statemen
3268e 74 73 20 77 69 74 68 69 6e 20 74 72 69 67 67 65  ts within trigge
3268f 72 73 22 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  rs");.}.        
32690 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32691 65 20 32 38 38 3a 20 2f 2a 20 74 72 69 64 78 62  e 288: /* tridxb
32692 79 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59  y ::= INDEXED BY
32693 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74   nm */.{.  sqlit
32694 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
32695 65 2c 0a 20 20 20 20 20 20 20 20 22 74 68 65 20  e,.        "the 
32696 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
32697 65 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64  e is not allowed
32698 20 6f 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45   on UPDATE or DE
32699 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20  LETE statements 
3269a 22 0a 20 20 20 20 20 20 20 20 22 77 69 74 68 69  ".        "withi
3269b 6e 20 74 72 69 67 67 65 72 73 22 29 3b 0a 7d 0a  n triggers");.}.
3269c 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3269d 20 20 20 20 20 63 61 73 65 20 32 38 39 3a 20 2f       case 289: /
3269e 2a 20 74 72 69 64 78 62 79 20 3a 3a 3d 20 4e 4f  * tridxby ::= NO
3269f 54 20 49 4e 44 45 58 45 44 20 2a 2f 0a 7b 0a 20  T INDEXED */.{. 
326a0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
326a1 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
326a2 20 22 74 68 65 20 4e 4f 54 20 49 4e 44 45 58 45   "the NOT INDEXE
326a3 44 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20  D clause is not 
326a4 61 6c 6c 6f 77 65 64 20 6f 6e 20 55 50 44 41 54  allowed on UPDAT
326a5 45 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74  E or DELETE stat
326a6 65 6d 65 6e 74 73 20 22 0a 20 20 20 20 20 20 20  ements ".       
326a7 20 22 77 69 74 68 69 6e 20 74 72 69 67 67 65 72   "within trigger
326a8 73 22 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  s");.}.        b
326a9 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
326aa 20 32 39 30 3a 20 2f 2a 20 74 72 69 67 67 65 72   290: /* trigger
326ab 5f 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20  _cmd ::= UPDATE 
326ac 6f 72 63 6f 6e 66 20 74 72 6e 6d 20 74 72 69 64  orconf trnm trid
326ad 78 62 79 20 53 45 54 20 73 65 74 6c 69 73 74 20  xby SET setlist 
326ae 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 20 79  where_opt */.{ y
326af 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33  ygotominor.yy473
326b0 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
326b1 72 55 70 64 61 74 65 53 74 65 70 28 70 50 61 72  rUpdateStep(pPar
326b2 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d  se->db, &yymsp[-
326b3 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79  4].minor.yy0, yy
326b4 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
326b5 31 34 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  14, yymsp[0].min
326b6 6f 72 2e 79 79 31 33 32 2c 20 79 79 6d 73 70 5b  or.yy132, yymsp[
326b7 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 29  -5].minor.yy186)
326b8 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
326b9 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39  k;.      case 29
326ba 31 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d  1: /* trigger_cm
326bb 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
326bc 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f   INTO trnm insco
326bd 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53  llist_opt VALUES
326be 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 20   LP itemlist RP 
326bf 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
326c0 79 79 34 37 33 20 3d 20 73 71 6c 69 74 65 33 54  yy473 = sqlite3T
326c1 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70  riggerInsertStep
326c2 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79  (pParse->db, &yy
326c3 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
326c4 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  0, yymsp[-4].min
326c5 6f 72 2e 79 79 34 30 38 2c 20 79 79 6d 73 70 5b  or.yy408, yymsp[
326c6 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20  -1].minor.yy14, 
326c7 30 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  0, yymsp[-7].min
326c8 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20 20 20 20  or.yy186);}.    
326c9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
326ca 20 63 61 73 65 20 32 39 32 3a 20 2f 2a 20 74 72   case 292: /* tr
326cb 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e  igger_cmd ::= in
326cc 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72  sert_cmd INTO tr
326cd 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  nm inscollist_op
326ce 74 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 79 79 67  t select */.{yyg
326cf 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d  otominor.yy473 =
326d0 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 49   sqlite3TriggerI
326d1 6e 73 65 72 74 53 74 65 70 28 70 50 61 72 73 65  nsertStep(pParse
326d2 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  ->db, &yymsp[-2]
326d3 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73  .minor.yy0, yyms
326d4 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  p[-1].minor.yy40
326d5 38 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  8, 0, yymsp[0].m
326d6 69 6e 6f 72 2e 79 79 33 2c 20 79 79 6d 73 70 5b  inor.yy3, yymsp[
326d7 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 29  -4].minor.yy186)
326d8 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
326d9 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 33  ;.      case 293
326da 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64  : /* trigger_cmd
326db 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d   ::= DELETE FROM
326dc 20 74 72 6e 6d 20 74 72 69 64 78 62 79 20 77 68   trnm tridxby wh
326dd 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 79 79 67 6f  ere_opt */.{yygo
326de 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20  tominor.yy473 = 
326df 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 44 65  sqlite3TriggerDe
326e0 6c 65 74 65 53 74 65 70 28 70 50 61 72 73 65 2d  leteStep(pParse-
326e1 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  >db, &yymsp[-2].
326e2 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70  minor.yy0, yymsp
326e3 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 29  [0].minor.yy132)
326e4 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
326e5 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 34  ;.      case 294
326e6 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64  : /* trigger_cmd
326e7 20 3a 3a 3d 20 73 65 6c 65 63 74 20 2a 2f 0a 7b   ::= select */.{
326e8 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37  yygotominor.yy47
326e9 33 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67  3 = sqlite3Trigg
326ea 65 72 53 65 6c 65 63 74 53 74 65 70 28 70 50 61  erSelectStep(pPa
326eb 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 30  rse->db, yymsp[0
326ec 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 20 7d 0a  ].minor.yy3); }.
326ed 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
326ee 20 20 20 20 20 63 61 73 65 20 32 39 35 3a 20 2f       case 295: /
326ef 2a 20 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45  * expr ::= RAISE
326f0 20 4c 50 20 49 47 4e 4f 52 45 20 52 50 20 2a 2f   LP IGNORE RP */
326f1 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
326f2 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
326f3 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
326f4 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c  se, TK_RAISE, 0,
326f5 20 30 2c 20 30 29 3b 20 0a 20 20 69 66 28 20 79   0, 0); .  if( y
326f6 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
326f7 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 79 79  .pExpr ){.    yy
326f8 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
326f9 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20  pExpr->affinity 
326fa 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 0a 20 20 7d  = OE_Ignore;.  }
326fb 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
326fc 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79  y346.zStart = yy
326fd 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
326fe 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  0.z;.  yygotomin
326ff 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20  or.yy346.zEnd = 
32700 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
32701 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d  yy0.z[yymsp[0].m
32702 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a 20  inor.yy0.n];.}. 
32703 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32704 20 20 20 20 63 61 73 65 20 32 39 36 3a 20 2f 2a      case 296: /*
32705 20 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45 20   expr ::= RAISE 
32706 4c 50 20 72 61 69 73 65 74 79 70 65 20 43 4f 4d  LP raisetype COM
32707 4d 41 20 6e 6d 20 52 50 20 2a 2f 0a 7b 0a 20 20  MA nm RP */.{.  
32708 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32709 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
3270a 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3270b 4b 5f 52 41 49 53 45 2c 20 30 2c 20 30 2c 20 26  K_RAISE, 0, 0, &
3270c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3270d 79 79 30 29 3b 20 0a 20 20 69 66 28 20 79 79 67  yy0); .  if( yyg
3270e 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
3270f 45 78 70 72 20 29 20 7b 0a 20 20 20 20 79 79 67  Expr ) {.    yyg
32710 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32711 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d  Expr->affinity =
32712 20 28 63 68 61 72 29 79 79 6d 73 70 5b 2d 33 5d   (char)yymsp[-3]
32713 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 20 20  .minor.yy328;.  
32714 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  }.  yygotominor.
32715 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79  yy346.zStart = y
32716 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
32717 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  y0.z;.  yygotomi
32718 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d  nor.yy346.zEnd =
32719 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
3271a 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e  .yy0.z[yymsp[0].
3271b 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a  minor.yy0.n];.}.
3271c 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3271d 20 20 20 20 20 63 61 73 65 20 32 39 37 3a 20 2f       case 297: /
3271e 2a 20 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20  * raisetype ::= 
3271f 52 4f 4c 4c 42 41 43 4b 20 2a 2f 0a 7b 79 79 67  ROLLBACK */.{yyg
32720 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d  otominor.yy328 =
32721 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3b 7d 0a 20   OE_Rollback;}. 
32722 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32723 20 20 20 20 63 61 73 65 20 32 39 39 3a 20 2f 2a      case 299: /*
32724 20 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 46   raisetype ::= F
32725 41 49 4c 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  AIL */.{yygotomi
32726 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 46  nor.yy328 = OE_F
32727 61 69 6c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ail;}.        br
32728 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32729 33 30 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  300: /* cmd ::= 
3272a 44 52 4f 50 20 54 52 49 47 47 45 52 20 69 66 65  DROP TRIGGER ife
3272b 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20 2a  xists fullname *
3272c 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 72 6f  /.{.  sqlite3Dro
3272d 70 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  pTrigger(pParse,
3272e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3272f 79 36 35 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  y65,yymsp[-1].mi
32730 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20  nor.yy328);.}.  
32731 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32732 20 20 20 63 61 73 65 20 33 30 31 3a 20 2f 2a 20     case 301: /* 
32733 63 6d 64 20 3a 3a 3d 20 41 54 54 41 43 48 20 64  cmd ::= ATTACH d
32734 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65  atabase_kw_opt e
32735 78 70 72 20 41 53 20 65 78 70 72 20 6b 65 79 5f  xpr AS expr key_
32736 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  opt */.{.  sqlit
32737 65 33 41 74 74 61 63 68 28 70 50 61 72 73 65 2c  e3Attach(pParse,
32738 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
32739 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 79 79  .yy346.pExpr, yy
3273a 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3273b 33 34 36 2e 70 45 78 70 72 2c 20 79 79 6d 73 70  346.pExpr, yymsp
3273c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 29  [0].minor.yy132)
3273d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
3273e 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  k;.      case 30
3273f 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 45  2: /* cmd ::= DE
32740 54 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77  TACH database_kw
32741 5f 6f 70 74 20 65 78 70 72 20 2a 2f 0a 7b 0a 20  _opt expr */.{. 
32742 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 28 70   sqlite3Detach(p
32743 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
32744 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32745 72 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  r);.}.        br
32746 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32747 33 30 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  307: /* cmd ::= 
32748 52 45 49 4e 44 45 58 20 2a 2f 0a 7b 73 71 6c 69  REINDEX */.{sqli
32749 74 65 33 52 65 69 6e 64 65 78 28 70 50 61 72 73  te3Reindex(pPars
3274a 65 2c 20 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20  e, 0, 0);}.     
3274b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3274c 63 61 73 65 20 33 30 38 3a 20 2f 2a 20 63 6d 64  case 308: /* cmd
3274d 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 6e 6d 20   ::= REINDEX nm 
3274e 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  dbnm */.{sqlite3
3274f 52 65 69 6e 64 65 78 28 70 50 61 72 73 65 2c 20  Reindex(pParse, 
32750 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
32751 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  .yy0, &yymsp[0].
32752 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
32753 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32754 20 20 63 61 73 65 20 33 30 39 3a 20 2f 2a 20 63    case 309: /* c
32755 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 2a  md ::= ANALYZE *
32756 2f 0a 7b 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a  /.{sqlite3Analyz
32757 65 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b  e(pParse, 0, 0);
32758 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32759 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 30 3a  .      case 310:
3275a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c   /* cmd ::= ANAL
3275b 59 5a 45 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b  YZE nm dbnm */.{
3275c 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 70  sqlite3Analyze(p
3275d 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 31  Parse, &yymsp[-1
3275e 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
3275f 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32760 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
32761 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 31  k;.      case 31
32762 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4c  1: /* cmd ::= AL
32763 54 45 52 20 54 41 42 4c 45 20 66 75 6c 6c 6e 61  TER TABLE fullna
32764 6d 65 20 52 45 4e 41 4d 45 20 54 4f 20 6e 6d 20  me RENAME TO nm 
32765 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c  */.{.  sqlite3Al
32766 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 70  terRenameTable(p
32767 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e  Parse,yymsp[-3].
32768 6d 69 6e 6f 72 2e 79 79 36 35 2c 26 79 79 6d 73  minor.yy65,&yyms
32769 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
3276a 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3276b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 32  ;.      case 312
3276c 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4c 54  : /* cmd ::= ALT
3276d 45 52 20 54 41 42 4c 45 20 61 64 64 5f 63 6f 6c  ER TABLE add_col
3276e 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 41 44 44  umn_fullname ADD
3276f 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 63 6f   kwcolumn_opt co
32770 6c 75 6d 6e 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  lumn */.{.  sqli
32771 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64  te3AlterFinishAd
32772 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20  dColumn(pParse, 
32773 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
32774 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
32775 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32776 65 20 33 31 33 3a 20 2f 2a 20 61 64 64 5f 63 6f  e 313: /* add_co
32777 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a  lumn_fullname ::
32778 3d 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a  = fullname */.{.
32779 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6c 6f    pParse->db->lo
3277a 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
3277b 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 41   = 0;.  sqlite3A
3277c 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75  lterBeginAddColu
3277d 6d 6e 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  mn(pParse, yymsp
3277e 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 29 3b  [0].minor.yy65);
3277f 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32780 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 36  ;.      case 316
32781 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65  : /* cmd ::= cre
32782 61 74 65 5f 76 74 61 62 20 2a 2f 0a 7b 73 71 6c  ate_vtab */.{sql
32783 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 61  ite3VtabFinishPa
32784 72 73 65 28 70 50 61 72 73 65 2c 30 29 3b 7d 0a  rse(pParse,0);}.
32785 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32786 20 20 20 20 20 63 61 73 65 20 33 31 37 3a 20 2f       case 317: /
32787 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65  * cmd ::= create
32788 5f 76 74 61 62 20 4c 50 20 76 74 61 62 61 72 67  _vtab LP vtabarg
32789 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 73 71 6c 69  list RP */.{sqli
3278a 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 61 72  te3VtabFinishPar
3278b 73 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  se(pParse,&yymsp
3278c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
3278d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3278e 20 20 20 20 20 20 63 61 73 65 20 33 31 38 3a 20        case 318: 
3278f 2f 2a 20 63 72 65 61 74 65 5f 76 74 61 62 20 3a  /* create_vtab :
32790 3a 3d 20 63 72 65 61 74 65 6b 77 20 56 49 52 54  := createkw VIRT
32791 55 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64 62 6e  UAL TABLE nm dbn
32792 6d 20 55 53 49 4e 47 20 6e 6d 20 2a 2f 0a 7b 0a  m USING nm */.{.
32793 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 42      sqlite3VtabB
32794 65 67 69 6e 50 61 72 73 65 28 70 50 61 72 73 65  eginParse(pParse
32795 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  , &yymsp[-3].min
32796 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d  or.yy0, &yymsp[-
32797 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  2].minor.yy0, &y
32798 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32799 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
3279a 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3279b 33 32 31 3a 20 2f 2a 20 76 74 61 62 61 72 67 20  321: /* vtabarg 
3279c 3a 3a 3d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56  ::= */.{sqlite3V
3279d 74 61 62 41 72 67 49 6e 69 74 28 70 50 61 72 73  tabArgInit(pPars
3279e 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  e);}.        bre
3279f 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
327a0 32 33 3a 20 2f 2a 20 76 74 61 62 61 72 67 74 6f  23: /* vtabargto
327a1 6b 65 6e 20 3a 3a 3d 20 41 4e 59 20 2a 2f 0a 20  ken ::= ANY */. 
327a2 20 20 20 20 20 63 61 73 65 20 33 32 34 3a 20 2f       case 324: /
327a3 2a 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a  * vtabargtoken :
327a4 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52 50  := lp anylist RP
327a5 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
327a6 79 72 75 6c 65 6e 6f 3d 3d 33 32 34 29 3b 0a 20  yruleno==324);. 
327a7 20 20 20 20 20 63 61 73 65 20 33 32 35 3a 20 2f       case 325: /
327a8 2a 20 6c 70 20 3a 3a 3d 20 4c 50 20 2a 2f 20 79  * lp ::= LP */ y
327a9 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
327aa 6e 6f 3d 3d 33 32 35 29 3b 0a 7b 73 71 6c 69 74  no==325);.{sqlit
327ab 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28  e3VtabArgExtend(
327ac 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d  pParse,&yymsp[0]
327ad 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
327ae 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
327af 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
327b0 20 20 2f 2a 20 28 30 29 20 69 6e 70 75 74 20 3a    /* (0) input :
327b1 3a 3d 20 63 6d 64 6c 69 73 74 20 2a 2f 20 79 79  := cmdlist */ yy
327b2 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
327b3 6f 3d 3d 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20  o==0);.      /* 
327b4 28 31 29 20 63 6d 64 6c 69 73 74 20 3a 3a 3d 20  (1) cmdlist ::= 
327b5 63 6d 64 6c 69 73 74 20 65 63 6d 64 20 2a 2f 20  cmdlist ecmd */ 
327b6 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
327b7 65 6e 6f 3d 3d 31 29 3b 0a 20 20 20 20 20 20 2f  eno==1);.      /
327b8 2a 20 28 32 29 20 63 6d 64 6c 69 73 74 20 3a 3a  * (2) cmdlist ::
327b9 3d 20 65 63 6d 64 20 2a 2f 20 79 79 74 65 73 74  = ecmd */ yytest
327ba 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
327bb 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 29 20  );.      /* (3) 
327bc 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49 20 2a 2f  ecmd ::= SEMI */
327bd 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
327be 6c 65 6e 6f 3d 3d 33 29 3b 0a 20 20 20 20 20 20  leno==3);.      
327bf 2f 2a 20 28 34 29 20 65 63 6d 64 20 3a 3a 3d 20  /* (4) ecmd ::= 
327c0 65 78 70 6c 61 69 6e 20 63 6d 64 78 20 53 45 4d  explain cmdx SEM
327c1 49 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  I */ yytestcase(
327c2 79 79 72 75 6c 65 6e 6f 3d 3d 34 29 3b 0a 20 20  yyruleno==4);.  
327c3 20 20 20 20 2f 2a 20 28 31 30 29 20 74 72 61 6e      /* (10) tran
327c4 73 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  s_opt ::= */ yyt
327c5 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
327c6 3d 3d 31 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==10);.      /* 
327c7 28 31 31 29 20 74 72 61 6e 73 5f 6f 70 74 20 3a  (11) trans_opt :
327c8 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 2a  := TRANSACTION *
327c9 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
327ca 75 6c 65 6e 6f 3d 3d 31 31 29 3b 0a 20 20 20 20  uleno==11);.    
327cb 20 20 2f 2a 20 28 31 32 29 20 74 72 61 6e 73 5f    /* (12) trans_
327cc 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54  opt ::= TRANSACT
327cd 49 4f 4e 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74  ION nm */ yytest
327ce 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
327cf 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 30  2);.      /* (20
327d0 29 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20  ) savepoint_opt 
327d1 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20 2a 2f  ::= SAVEPOINT */
327d2 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
327d3 6c 65 6e 6f 3d 3d 32 30 29 3b 0a 20 20 20 20 20  leno==20);.     
327d4 20 2f 2a 20 28 32 31 29 20 73 61 76 65 70 6f 69   /* (21) savepoi
327d5 6e 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  nt_opt ::= */ yy
327d6 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
327d7 6f 3d 3d 32 31 29 3b 0a 20 20 20 20 20 20 2f 2a  o==21);.      /*
327d8 20 28 32 35 29 20 63 6d 64 20 3a 3a 3d 20 63 72   (25) cmd ::= cr
327d9 65 61 74 65 5f 74 61 62 6c 65 20 63 72 65 61 74  eate_table creat
327da 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 2a 2f 20  e_table_args */ 
327db 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
327dc 65 6e 6f 3d 3d 32 35 29 3b 0a 20 20 20 20 20 20  eno==25);.      
327dd 2f 2a 20 28 33 34 29 20 63 6f 6c 75 6d 6e 6c 69  /* (34) columnli
327de 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73  st ::= columnlis
327df 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 20 2a  t COMMA column *
327e0 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
327e1 75 6c 65 6e 6f 3d 3d 33 34 29 3b 0a 20 20 20 20  uleno==34);.    
327e2 20 20 2f 2a 20 28 33 35 29 20 63 6f 6c 75 6d 6e    /* (35) column
327e3 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 20  list ::= column 
327e4 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
327e5 72 75 6c 65 6e 6f 3d 3d 33 35 29 3b 0a 20 20 20  ruleno==35);.   
327e6 20 20 20 2f 2a 20 28 34 34 29 20 74 79 70 65 20     /* (44) type 
327e7 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
327e8 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 34 29 3b  e(yyruleno==44);
327e9 0a 20 20 20 20 20 20 2f 2a 20 28 35 31 29 20 73  .      /* (51) s
327ea 69 67 6e 65 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e  igned ::= plus_n
327eb 75 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  um */ yytestcase
327ec 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 31 29 3b 0a  (yyruleno==51);.
327ed 20 20 20 20 20 20 2f 2a 20 28 35 32 29 20 73 69        /* (52) si
327ee 67 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e  gned ::= minus_n
327ef 75 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  um */ yytestcase
327f0 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 32 29 3b 0a  (yyruleno==52);.
327f1 20 20 20 20 20 20 2f 2a 20 28 35 33 29 20 63 61        /* (53) ca
327f2 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67 6c  rglist ::= cargl
327f3 69 73 74 20 63 61 72 67 20 2a 2f 20 79 79 74 65  ist carg */ yyte
327f4 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
327f5 3d 35 33 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =53);.      /* (
327f6 35 34 29 20 63 61 72 67 6c 69 73 74 20 3a 3a 3d  54) carglist ::=
327f7 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
327f8 79 72 75 6c 65 6e 6f 3d 3d 35 34 29 3b 0a 20 20  yruleno==54);.  
327f9 20 20 20 20 2f 2a 20 28 35 35 29 20 63 61 72 67      /* (55) carg
327fa 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54 20   ::= CONSTRAINT 
327fb 6e 6d 20 63 63 6f 6e 73 20 2a 2f 20 79 79 74 65  nm ccons */ yyte
327fc 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
327fd 3d 35 35 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =55);.      /* (
327fe 35 36 29 20 63 61 72 67 20 3a 3a 3d 20 63 63 6f  56) carg ::= cco
327ff 6e 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  ns */ yytestcase
32800 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 36 29 3b 0a  (yyruleno==56);.
32801 20 20 20 20 20 20 2f 2a 20 28 36 32 29 20 63 63        /* (62) cc
32802 6f 6e 73 20 3a 3a 3d 20 4e 55 4c 4c 20 6f 6e 63  ons ::= NULL onc
32803 6f 6e 66 20 2a 2f 20 79 79 74 65 73 74 63 61 73  onf */ yytestcas
32804 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 36 32 29 3b  e(yyruleno==62);
32805 0a 20 20 20 20 20 20 2f 2a 20 28 38 39 29 20 63  .      /* (89) c
32806 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73  onslist ::= cons
32807 6c 69 73 74 20 43 4f 4d 4d 41 20 74 63 6f 6e 73  list COMMA tcons
32808 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32809 79 72 75 6c 65 6e 6f 3d 3d 38 39 29 3b 0a 20 20  yruleno==89);.  
3280a 20 20 20 20 2f 2a 20 28 39 30 29 20 63 6f 6e 73      /* (90) cons
3280b 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73  list ::= conslis
3280c 74 20 74 63 6f 6e 73 20 2a 2f 20 79 79 74 65 73  t tcons */ yytes
3280d 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3280e 39 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 39  90);.      /* (9
3280f 31 29 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20  1) conslist ::= 
32810 74 63 6f 6e 73 20 2a 2f 20 79 79 74 65 73 74 63  tcons */ yytestc
32811 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 39 31  ase(yyruleno==91
32812 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 39 32 29  );.      /* (92)
32813 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54   tcons ::= CONST
32814 52 41 49 4e 54 20 6e 6d 20 2a 2f 20 79 79 74 65  RAINT nm */ yyte
32815 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32816 3d 39 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =92);.      /* (
32817 32 36 38 29 20 70 6c 75 73 5f 6f 70 74 20 3a 3a  268) plus_opt ::
32818 3d 20 50 4c 55 53 20 2a 2f 20 79 79 74 65 73 74  = PLUS */ yytest
32819 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3281a 36 38 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32  68);.      /* (2
3281b 36 39 29 20 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d  69) plus_opt ::=
3281c 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3281d 79 72 75 6c 65 6e 6f 3d 3d 32 36 39 29 3b 0a 20  yruleno==269);. 
3281e 20 20 20 20 20 2f 2a 20 28 32 37 39 29 20 66 6f       /* (279) fo
3281f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d  reach_clause ::=
32820 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32821 79 72 75 6c 65 6e 6f 3d 3d 32 37 39 29 3b 0a 20  yruleno==279);. 
32822 20 20 20 20 20 2f 2a 20 28 32 38 30 29 20 66 6f       /* (280) fo
32823 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d  reach_clause ::=
32824 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 2a 2f   FOR EACH ROW */
32825 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32826 6c 65 6e 6f 3d 3d 32 38 30 29 3b 0a 20 20 20 20  leno==280);.    
32827 20 20 2f 2a 20 28 32 38 37 29 20 74 72 69 64 78    /* (287) tridx
32828 62 79 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  by ::= */ yytest
32829 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3282a 38 37 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33  87);.      /* (3
3282b 30 35 29 20 64 61 74 61 62 61 73 65 5f 6b 77 5f  05) database_kw_
3282c 6f 70 74 20 3a 3a 3d 20 44 41 54 41 42 41 53 45  opt ::= DATABASE
3282d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3282e 79 72 75 6c 65 6e 6f 3d 3d 33 30 35 29 3b 0a 20  yruleno==305);. 
3282f 20 20 20 20 20 2f 2a 20 28 33 30 36 29 20 64 61       /* (306) da
32830 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a  tabase_kw_opt ::
32831 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
32832 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 36 29 3b 0a  yyruleno==306);.
32833 20 20 20 20 20 20 2f 2a 20 28 33 31 34 29 20 6b        /* (314) k
32834 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20  wcolumn_opt ::= 
32835 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
32836 72 75 6c 65 6e 6f 3d 3d 33 31 34 29 3b 0a 20 20  ruleno==314);.  
32837 20 20 20 20 2f 2a 20 28 33 31 35 29 20 6b 77 63      /* (315) kwc
32838 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 43 4f  olumn_opt ::= CO
32839 4c 55 4d 4e 4b 57 20 2a 2f 20 79 79 74 65 73 74  LUMNKW */ yytest
3283a 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
3283b 31 35 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33  15);.      /* (3
3283c 31 39 29 20 76 74 61 62 61 72 67 6c 69 73 74 20  19) vtabarglist 
3283d 3a 3a 3d 20 76 74 61 62 61 72 67 20 2a 2f 20 79  ::= vtabarg */ y
3283e 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3283f 6e 6f 3d 3d 33 31 39 29 3b 0a 20 20 20 20 20 20  no==319);.      
32840 2f 2a 20 28 33 32 30 29 20 76 74 61 62 61 72 67  /* (320) vtabarg
32841 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67  list ::= vtabarg
32842 6c 69 73 74 20 43 4f 4d 4d 41 20 76 74 61 62 61  list COMMA vtaba
32843 72 67 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  rg */ yytestcase
32844 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 30 29 3b  (yyruleno==320);
32845 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 32 29 20  .      /* (322) 
32846 76 74 61 62 61 72 67 20 3a 3a 3d 20 76 74 61 62  vtabarg ::= vtab
32847 61 72 67 20 76 74 61 62 61 72 67 74 6f 6b 65 6e  arg vtabargtoken
32848 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32849 79 72 75 6c 65 6e 6f 3d 3d 33 32 32 29 3b 0a 20  yruleno==322);. 
3284a 20 20 20 20 20 2f 2a 20 28 33 32 36 29 20 61 6e       /* (326) an
3284b 79 6c 69 73 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  ylist ::= */ yyt
3284c 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3284d 3d 3d 33 32 36 29 3b 0a 20 20 20 20 20 20 2f 2a  ==326);.      /*
3284e 20 28 33 32 37 29 20 61 6e 79 6c 69 73 74 20 3a   (327) anylist :
3284f 3a 3d 20 61 6e 79 6c 69 73 74 20 4c 50 20 61 6e  := anylist LP an
32850 79 6c 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65  ylist RP */ yyte
32851 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32852 3d 33 32 37 29 3b 0a 20 20 20 20 20 20 2f 2a 20  =327);.      /* 
32853 28 33 32 38 29 20 61 6e 79 6c 69 73 74 20 3a 3a  (328) anylist ::
32854 3d 20 61 6e 79 6c 69 73 74 20 41 4e 59 20 2a 2f  = anylist ANY */
32855 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32856 6c 65 6e 6f 3d 3d 33 32 38 29 3b 0a 20 20 20 20  leno==328);.    
32857 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 3b 0a      break;.  };.
32858 20 20 79 79 67 6f 74 6f 20 3d 20 79 79 52 75 6c    yygoto = yyRul
32859 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e  eInfo[yyruleno].
3285a 6c 68 73 3b 0a 20 20 79 79 73 69 7a 65 20 3d 20  lhs;.  yysize = 
3285b 79 79 52 75 6c 65 49 6e 66 6f 5b 79 79 72 75 6c  yyRuleInfo[yyrul
3285c 65 6e 6f 5d 2e 6e 72 68 73 3b 0a 20 20 79 79 70  eno].nrhs;.  yyp
3285d 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 2d 3d  Parser->yyidx -=
3285e 20 79 79 73 69 7a 65 3b 0a 20 20 79 79 61 63 74   yysize;.  yyact
3285f 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65 64 75 63   = yy_find_reduc
32860 65 5f 61 63 74 69 6f 6e 28 79 79 6d 73 70 5b 2d  e_action(yymsp[-
32861 79 79 73 69 7a 65 5d 2e 73 74 61 74 65 6e 6f 2c  yysize].stateno,
32862 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 67 6f  (YYCODETYPE)yygo
32863 74 6f 29 3b 0a 20 20 69 66 28 20 79 79 61 63 74  to);.  if( yyact
32864 20 3c 20 59 59 4e 53 54 41 54 45 20 29 7b 0a 23   < YYNSTATE ){.#
32865 69 66 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  ifdef NDEBUG.   
32866 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 6e 6f   /* If we are no
32867 74 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20  t debugging and 
32868 74 68 65 20 72 65 64 75 63 65 20 61 63 74 69 6f  the reduce actio
32869 6e 20 70 6f 70 70 65 64 20 61 74 20 6c 65 61 73  n popped at leas
3286a 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6c 65  t.    ** one ele
3286b 6d 65 6e 74 20 6f 66 66 20 74 68 65 20 73 74 61  ment off the sta
3286c 63 6b 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20  ck, then we can 
3286d 70 75 73 68 20 74 68 65 20 6e 65 77 20 65 6c 65  push the new ele
3286e 6d 65 6e 74 20 62 61 63 6b 0a 20 20 20 20 2a 2a  ment back.    **
3286f 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20   onto the stack 
32870 68 65 72 65 2c 20 61 6e 64 20 73 6b 69 70 20 74  here, and skip t
32871 68 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f  he stack overflo
32872 77 20 74 65 73 74 20 69 6e 20 79 79 5f 73 68 69  w test in yy_shi
32873 66 74 28 29 2e 0a 20 20 20 20 2a 2a 20 54 68 61  ft()..    ** Tha
32874 74 20 67 69 76 65 73 20 61 20 73 69 67 6e 69 66  t gives a signif
32875 69 63 61 6e 74 20 73 70 65 65 64 20 69 6d 70 72  icant speed impr
32876 6f 76 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20  ovement. */.    
32877 69 66 28 20 79 79 73 69 7a 65 20 29 7b 0a 20 20  if( yysize ){.  
32878 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
32879 79 69 64 78 2b 2b 3b 0a 20 20 20 20 20 20 79 79  yidx++;.      yy
3287a 6d 73 70 20 2d 3d 20 79 79 73 69 7a 65 2d 31 3b  msp -= yysize-1;
3287b 0a 20 20 20 20 20 20 79 79 6d 73 70 2d 3e 73 74  .      yymsp->st
3287c 61 74 65 6e 6f 20 3d 20 28 59 59 41 43 54 49 4f  ateno = (YYACTIO
3287d 4e 54 59 50 45 29 79 79 61 63 74 3b 0a 20 20 20  NTYPE)yyact;.   
3287e 20 20 20 79 79 6d 73 70 2d 3e 6d 61 6a 6f 72 20     yymsp->major 
3287f 3d 20 28 59 59 43 4f 44 45 54 59 50 45 29 79 79  = (YYCODETYPE)yy
32880 67 6f 74 6f 3b 0a 20 20 20 20 20 20 79 79 6d 73  goto;.      yyms
32881 70 2d 3e 6d 69 6e 6f 72 20 3d 20 79 79 67 6f 74  p->minor = yygot
32882 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 7d 65 6c 73  ominor;.    }els
32883 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20  e.#endif.    {. 
32884 20 20 20 20 20 79 79 5f 73 68 69 66 74 28 79 79       yy_shift(yy
32885 70 50 61 72 73 65 72 2c 79 79 61 63 74 2c 79 79  pParser,yyact,yy
32886 67 6f 74 6f 2c 26 79 79 67 6f 74 6f 6d 69 6e 6f  goto,&yygotomino
32887 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  r);.    }.  }els
32888 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 79  e{.    assert( y
32889 79 61 63 74 20 3d 3d 20 59 59 4e 53 54 41 54 45  yact == YYNSTATE
3288a 20 2b 20 59 59 4e 52 55 4c 45 20 2b 20 31 20 29   + YYNRULE + 1 )
3288b 3b 0a 20 20 20 20 79 79 5f 61 63 63 65 70 74 28  ;.    yy_accept(
3288c 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 7d 0a  yypParser);.  }.
3288d 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
3288e 6c 6f 77 69 6e 67 20 63 6f 64 65 20 65 78 65 63  lowing code exec
3288f 75 74 65 73 20 77 68 65 6e 20 74 68 65 20 70 61  utes when the pa
32890 72 73 65 20 66 61 69 6c 73 0a 2a 2f 0a 23 69 66  rse fails.*/.#if
32891 6e 64 65 66 20 59 59 4e 4f 45 52 52 4f 52 52 45  ndef YYNOERRORRE
32892 43 4f 56 45 52 59 0a 73 74 61 74 69 63 20 76 6f  COVERY.static vo
32893 69 64 20 79 79 5f 70 61 72 73 65 5f 66 61 69 6c  id yy_parse_fail
32894 65 64 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a  ed(.  yyParser *
32895 79 79 70 50 61 72 73 65 72 20 20 20 20 20 20 20  yypParser       
32896 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
32897 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  r */.){.  sqlite
32898 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
32899 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
3289a 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49  .  if( yyTraceFI
3289b 4c 45 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  LE ){.    fprint
3289c 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
3289d 73 46 61 69 6c 21 5c 6e 22 2c 79 79 54 72 61 63  sFail!\n",yyTrac
3289e 65 50 72 6f 6d 70 74 29 3b 0a 20 20 7d 0a 23 65  ePrompt);.  }.#e
3289f 6e 64 69 66 0a 20 20 77 68 69 6c 65 28 20 79 79  ndif.  while( yy
328a0 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d  pParser->yyidx>=
328a1 30 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65  0 ) yy_pop_parse
328a2 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73 65  r_stack(yypParse
328a3 72 29 3b 0a 20 20 2f 2a 20 48 65 72 65 20 63 6f  r);.  /* Here co
328a4 64 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 77  de is inserted w
328a5 68 69 63 68 20 77 69 6c 6c 20 62 65 20 65 78 65  hich will be exe
328a6 63 75 74 65 64 20 77 68 65 6e 65 76 65 72 20 74  cuted whenever t
328a7 68 65 0a 20 20 2a 2a 20 70 61 72 73 65 72 20 66  he.  ** parser f
328a8 61 69 6c 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ails */.  sqlite
328a9 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45  3ParserARG_STORE
328aa 3b 20 2f 2a 20 53 75 70 70 72 65 73 73 20 77 61  ; /* Suppress wa
328ab 72 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e 75 73  rning about unus
328ac 65 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65  ed %extra_argume
328ad 6e 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 7d  nt variable */.}
328ae 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 4e 4f 45  .#endif /* YYNOE
328af 52 52 4f 52 52 45 43 4f 56 45 52 59 20 2a 2f 0a  RRORRECOVERY */.
328b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
328b1 77 69 6e 67 20 63 6f 64 65 20 65 78 65 63 75 74  wing code execut
328b2 65 73 20 77 68 65 6e 20 61 20 73 79 6e 74 61 78  es when a syntax
328b3 20 65 72 72 6f 72 20 66 69 72 73 74 20 6f 63 63   error first occ
328b4 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  urs..*/.static v
328b5 6f 69 64 20 79 79 5f 73 79 6e 74 61 78 5f 65 72  oid yy_syntax_er
328b6 72 6f 72 28 0a 20 20 79 79 50 61 72 73 65 72 20  ror(.  yyParser 
328b7 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20 20 20  *yypParser,     
328b8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
328b9 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20 79 79 6d  ser */.  int yym
328ba 61 6a 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20  ajor,           
328bb 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d          /* The m
328bc 61 6a 6f 72 20 74 79 70 65 20 6f 66 20 74 68 65  ajor type of the
328bd 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a   error token */.
328be 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79 79    YYMINORTYPE yy
328bf 6d 69 6e 6f 72 20 20 20 20 20 20 20 20 20 20 20  minor           
328c0 20 2f 2a 20 54 68 65 20 6d 69 6e 6f 72 20 74 79   /* The minor ty
328c1 70 65 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  pe of the error 
328c2 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71  token */.){.  sq
328c3 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46  lite3ParserARG_F
328c4 45 54 43 48 3b 0a 23 64 65 66 69 6e 65 20 54 4f  ETCH;.#define TO
328c5 4b 45 4e 20 28 79 79 6d 69 6e 6f 72 2e 79 79 30  KEN (yyminor.yy0
328c6 29 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  )..  UNUSED_PARA
328c7 4d 45 54 45 52 28 79 79 6d 61 6a 6f 72 29 3b 20  METER(yymajor); 
328c8 20 2f 2a 20 53 69 6c 65 6e 63 65 20 73 6f 6d 65   /* Silence some
328c9 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e   compiler warnin
328ca 67 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  gs */.  assert( 
328cb 54 4f 4b 45 4e 2e 7a 5b 30 5d 20 29 3b 20 20 2f  TOKEN.z[0] );  /
328cc 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  * The tokenizer 
328cd 61 6c 77 61 79 73 20 67 69 76 65 73 20 75 73 20  always gives us 
328ce 61 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 73 71 6c  a token */.  sql
328cf 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
328d0 72 73 65 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c  rse, "near \"%T\
328d1 22 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72 22  ": syntax error"
328d2 2c 20 26 54 4f 4b 45 4e 29 3b 0a 20 20 70 50 61  , &TOKEN);.  pPa
328d3 72 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72 20  rse->parseError 
328d4 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 50 61  = 1;.  sqlite3Pa
328d5 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f  rserARG_STORE; /
328d6 2a 20 53 75 70 70 72 65 73 73 20 77 61 72 6e 69  * Suppress warni
328d7 6e 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20  ng about unused 
328d8 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20  %extra_argument 
328d9 76 61 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f  variable */.}../
328da 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
328db 6e 67 20 69 73 20 65 78 65 63 75 74 65 64 20 77  ng is executed w
328dc 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 61  hen the parser a
328dd 63 63 65 70 74 73 0a 2a 2f 0a 73 74 61 74 69 63  ccepts.*/.static
328de 20 76 6f 69 64 20 79 79 5f 61 63 63 65 70 74 28   void yy_accept(
328df 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70  .  yyParser *yyp
328e0 50 61 72 73 65 72 20 20 20 20 20 20 20 20 20 20  Parser          
328e1 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
328e2 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61  /.){.  sqlite3Pa
328e3 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 23  rserARG_FETCH;.#
328e4 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
328e5 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
328e6 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79  ){.    fprintf(y
328e7 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 41 63  yTraceFILE,"%sAc
328e8 63 65 70 74 21 5c 6e 22 2c 79 79 54 72 61 63 65  cept!\n",yyTrace
328e9 50 72 6f 6d 70 74 29 3b 0a 20 20 7d 0a 23 65 6e  Prompt);.  }.#en
328ea 64 69 66 0a 20 20 77 68 69 6c 65 28 20 79 79 70  dif.  while( yyp
328eb 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30  Parser->yyidx>=0
328ec 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72   ) yy_pop_parser
328ed 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72  _stack(yypParser
328ee 29 3b 0a 20 20 2f 2a 20 48 65 72 65 20 63 6f 64  );.  /* Here cod
328ef 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 77 68  e is inserted wh
328f0 69 63 68 20 77 69 6c 6c 20 62 65 20 65 78 65 63  ich will be exec
328f1 75 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68  uted whenever th
328f2 65 0a 20 20 2a 2a 20 70 61 72 73 65 72 20 61 63  e.  ** parser ac
328f3 63 65 70 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74  cepts */.  sqlit
328f4 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52  e3ParserARG_STOR
328f5 45 3b 20 2f 2a 20 53 75 70 70 72 65 73 73 20 77  E; /* Suppress w
328f6 61 72 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e 75  arning about unu
328f7 73 65 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d  sed %extra_argum
328f8 65 6e 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  ent variable */.
328f9 7d 0a 0a 2f 2a 20 54 68 65 20 6d 61 69 6e 20 70  }../* The main p
328fa 61 72 73 65 72 20 70 72 6f 67 72 61 6d 2e 0a 2a  arser program..*
328fb 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
328fc 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65  ment is a pointe
328fd 72 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65  r to a structure
328fe 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
328ff 2a 20 22 73 71 6c 69 74 65 33 50 61 72 73 65 72  * "sqlite3Parser
32900 41 6c 6c 6f 63 22 20 77 68 69 63 68 20 64 65 73  Alloc" which des
32901 63 72 69 62 65 73 20 74 68 65 20 63 75 72 72 65  cribes the curre
32902 6e 74 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  nt state of the 
32903 70 61 72 73 65 72 2e 0a 2a 2a 20 54 68 65 20 73  parser..** The s
32904 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
32905 73 20 74 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65  s the major toke
32906 6e 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 74  n number.  The t
32907 68 69 72 64 20 69 73 0a 2a 2a 20 74 68 65 20 6d  hird is.** the m
32908 69 6e 6f 72 20 74 6f 6b 65 6e 2e 20 20 54 68 65  inor token.  The
32909 20 66 6f 75 72 74 68 20 6f 70 74 69 6f 6e 61 6c   fourth optional
3290a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 77 68 61   argument is wha
3290b 74 65 76 65 72 20 74 68 65 0a 2a 2a 20 75 73 65  tever the.** use
3290c 72 20 77 61 6e 74 73 20 28 61 6e 64 20 73 70 65  r wants (and spe
3290d 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 67 72  cified in the gr
3290e 61 6d 6d 61 72 29 20 61 6e 64 20 69 73 20 61 76  ammar) and is av
3290f 61 69 6c 61 62 6c 65 20 66 6f 72 0a 2a 2a 20 75  ailable for.** u
32910 73 65 20 62 79 20 74 68 65 20 61 63 74 69 6f 6e  se by the action
32911 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a   routines..**.**
32912 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e   Inputs:.** <ul>
32913 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70 6f 69 6e 74  .** <li> A point
32914 65 72 20 74 6f 20 74 68 65 20 70 61 72 73 65 72  er to the parser
32915 20 28 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75   (an opaque stru
32916 63 74 75 72 65 2e 29 0a 2a 2a 20 3c 6c 69 3e 20  cture.).** <li> 
32917 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20  The major token 
32918 6e 75 6d 62 65 72 2e 0a 2a 2a 20 3c 6c 69 3e 20  number..** <li> 
32919 54 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20  The minor token 
3291a 6e 75 6d 62 65 72 2e 0a 2a 2a 20 3c 6c 69 3e 20  number..** <li> 
3291b 41 6e 20 6f 70 74 69 6f 6e 20 61 72 67 75 6d 65  An option argume
3291c 6e 74 20 6f 66 20 61 20 67 72 61 6d 6d 61 72 2d  nt of a grammar-
3291d 73 70 65 63 69 66 69 65 64 20 74 79 70 65 2e 0a  specified type..
3291e 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f  ** </ul>.**.** O
3291f 75 74 70 75 74 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e  utputs:.** None.
32920 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
32921 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
32922 61 72 73 65 72 28 0a 20 20 76 6f 69 64 20 2a 79  arser(.  void *y
32923 79 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  yp,             
32924 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
32925 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20 79 79 6d  ser */.  int yym
32926 61 6a 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20  ajor,           
32927 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a        /* The maj
32928 6f 72 20 74 6f 6b 65 6e 20 63 6f 64 65 20 6e 75  or token code nu
32929 6d 62 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65  mber */.  sqlite
3292a 33 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45  3ParserTOKENTYPE
3292b 20 79 79 6d 69 6e 6f 72 20 20 20 20 20 20 20 2f   yyminor       /
3292c 2a 20 54 68 65 20 76 61 6c 75 65 20 66 6f 72 20  * The value for 
3292d 74 68 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 73  the token */.  s
3292e 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
3292f 50 44 45 43 4c 20 20 20 20 20 20 20 20 20 20 20  PDECL           
32930 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20      /* Optional 
32931 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20  %extra_argument 
32932 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 29 7b 0a  parameter */.){.
32933 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79 79    YYMINORTYPE yy
32934 6d 69 6e 6f 72 75 6e 69 6f 6e 3b 0a 20 20 69 6e  minorunion;.  in
32935 74 20 79 79 61 63 74 3b 20 20 20 20 20 20 20 20  t yyact;        
32936 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
32937 72 20 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 69  r action. */.  i
32938 6e 74 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 3b  nt yyendofinput;
32939 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
3293a 77 65 20 61 72 65 20 61 74 20 74 68 65 20 65 6e  we are at the en
3293b 64 20 6f 66 20 69 6e 70 75 74 20 2a 2f 0a 23 69  d of input */.#i
3293c 66 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42  fdef YYERRORSYMB
3293d 4f 4c 0a 20 20 69 6e 74 20 79 79 65 72 72 6f 72  OL.  int yyerror
3293e 68 69 74 20 3d 20 30 3b 20 20 20 2f 2a 20 54 72  hit = 0;   /* Tr
3293f 75 65 20 69 66 20 79 79 6d 61 6a 6f 72 20 68 61  ue if yymajor ha
32940 73 20 69 6e 76 6f 6b 65 64 20 61 6e 20 65 72 72  s invoked an err
32941 6f 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 79  or */.#endif.  y
32942 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
32943 65 72 3b 20 20 2f 2a 20 54 68 65 20 70 61 72 73  er;  /* The pars
32944 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 28 72 65 29  er */..  /* (re)
32945 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 70  initialize the p
32946 61 72 73 65 72 2c 20 69 66 20 6e 65 63 65 73 73  arser, if necess
32947 61 72 79 20 2a 2f 0a 20 20 79 79 70 50 61 72 73  ary */.  yypPars
32948 65 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29  er = (yyParser*)
32949 79 79 70 3b 0a 20 20 69 66 28 20 79 79 70 50 61  yyp;.  if( yypPa
3294a 72 73 65 72 2d 3e 79 79 69 64 78 3c 30 20 29 7b  rser->yyidx<0 ){
3294b 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54  .#if YYSTACKDEPT
3294c 48 3c 3d 30 0a 20 20 20 20 69 66 28 20 79 79 70  H<=0.    if( yyp
3294d 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20  Parser->yystksz 
3294e 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 6d  <=0 ){.      /*m
3294f 65 6d 73 65 74 28 26 79 79 6d 69 6e 6f 72 75 6e  emset(&yyminorun
32950 69 6f 6e 2c 20 30 2c 20 73 69 7a 65 6f 66 28 79  ion, 0, sizeof(y
32951 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 29 3b 2a 2f  yminorunion));*/
32952 0a 20 20 20 20 20 20 79 79 6d 69 6e 6f 72 75 6e  .      yyminorun
32953 69 6f 6e 20 3d 20 79 79 7a 65 72 6f 6d 69 6e 6f  ion = yyzeromino
32954 72 3b 0a 20 20 20 20 20 20 79 79 53 74 61 63 6b  r;.      yyStack
32955 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73  Overflow(yypPars
32956 65 72 2c 20 26 79 79 6d 69 6e 6f 72 75 6e 69 6f  er, &yyminorunio
32957 6e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  n);.      return
32958 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
32959 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
3295a 69 64 78 20 3d 20 30 3b 0a 20 20 20 20 79 79 70  idx = 0;.    yyp
3295b 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74  Parser->yyerrcnt
3295c 20 3d 20 2d 31 3b 0a 20 20 20 20 79 79 70 50 61   = -1;.    yypPa
3295d 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30 5d  rser->yystack[0]
3295e 2e 73 74 61 74 65 6e 6f 20 3d 20 30 3b 0a 20 20  .stateno = 0;.  
3295f 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73    yypParser->yys
32960 74 61 63 6b 5b 30 5d 2e 6d 61 6a 6f 72 20 3d 20  tack[0].major = 
32961 30 3b 0a 20 20 7d 0a 20 20 79 79 6d 69 6e 6f 72  0;.  }.  yyminor
32962 75 6e 69 6f 6e 2e 79 79 30 20 3d 20 79 79 6d 69  union.yy0 = yymi
32963 6e 6f 72 3b 0a 20 20 79 79 65 6e 64 6f 66 69 6e  nor;.  yyendofin
32964 70 75 74 20 3d 20 28 79 79 6d 61 6a 6f 72 3d 3d  put = (yymajor==
32965 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 72  0);.  sqlite3Par
32966 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 0a 0a 23  serARG_STORE;..#
32967 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
32968 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
32969 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79  ){.    fprintf(y
3296a 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 49 6e  yTraceFILE,"%sIn
3296b 70 75 74 20 25 73 5c 6e 22 2c 79 79 54 72 61 63  put %s\n",yyTrac
3296c 65 50 72 6f 6d 70 74 2c 79 79 54 6f 6b 65 6e 4e  ePrompt,yyTokenN
3296d 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29 3b 0a 20  ame[yymajor]);. 
3296e 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64 6f 7b   }.#endif..  do{
3296f 0a 20 20 20 20 79 79 61 63 74 20 3d 20 79 79 5f  .    yyact = yy_
32970 66 69 6e 64 5f 73 68 69 66 74 5f 61 63 74 69 6f  find_shift_actio
32971 6e 28 79 79 70 50 61 72 73 65 72 2c 28 59 59 43  n(yypParser,(YYC
32972 4f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f 72 29  ODETYPE)yymajor)
32973 3b 0a 20 20 20 20 69 66 28 20 79 79 61 63 74 3c  ;.    if( yyact<
32974 59 59 4e 53 54 41 54 45 20 29 7b 0a 20 20 20 20  YYNSTATE ){.    
32975 20 20 61 73 73 65 72 74 28 20 21 79 79 65 6e 64    assert( !yyend
32976 6f 66 69 6e 70 75 74 20 29 3b 20 20 2f 2a 20 49  ofinput );  /* I
32977 6d 70 6f 73 73 69 62 6c 65 20 74 6f 20 73 68 69  mpossible to shi
32978 66 74 20 74 68 65 20 24 20 74 6f 6b 65 6e 20 2a  ft the $ token *
32979 2f 0a 20 20 20 20 20 20 79 79 5f 73 68 69 66 74  /.      yy_shift
3297a 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74  (yypParser,yyact
3297b 2c 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f  ,yymajor,&yymino
3297c 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 79  runion);.      y
3297d 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63  ypParser->yyerrc
3297e 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 79 79 6d 61  nt--;.      yyma
3297f 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a  jor = YYNOCODE;.
32980 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 79 79      }else if( yy
32981 61 63 74 20 3c 20 59 59 4e 53 54 41 54 45 20 2b  act < YYNSTATE +
32982 20 59 59 4e 52 55 4c 45 20 29 7b 0a 20 20 20 20   YYNRULE ){.    
32983 20 20 79 79 5f 72 65 64 75 63 65 28 79 79 70 50    yy_reduce(yypP
32984 61 72 73 65 72 2c 79 79 61 63 74 2d 59 59 4e 53  arser,yyact-YYNS
32985 54 41 54 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65  TATE);.    }else
32986 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
32987 79 79 61 63 74 20 3d 3d 20 59 59 5f 45 52 52 4f  yyact == YY_ERRO
32988 52 5f 41 43 54 49 4f 4e 20 29 3b 0a 23 69 66 64  R_ACTION );.#ifd
32989 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c  ef YYERRORSYMBOL
3298a 0a 20 20 20 20 20 20 69 6e 74 20 79 79 6d 78 3b  .      int yymx;
3298b 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
3298c 4e 44 45 42 55 47 0a 20 20 20 20 20 20 69 66 28  NDEBUG.      if(
3298d 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a   yyTraceFILE ){.
3298e 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28          fprintf(
3298f 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53  yyTraceFILE,"%sS
32990 79 6e 74 61 78 20 45 72 72 6f 72 21 5c 6e 22 2c  yntax Error!\n",
32991 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a  yyTracePrompt);.
32992 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23        }.#endif.#
32993 69 66 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d  ifdef YYERRORSYM
32994 42 4f 4c 0a 20 20 20 20 20 20 2f 2a 20 41 20 73  BOL.      /* A s
32995 79 6e 74 61 78 20 65 72 72 6f 72 20 68 61 73 20  yntax error has 
32996 6f 63 63 75 72 72 65 64 2e 0a 20 20 20 20 20 20  occurred..      
32997 2a 2a 20 54 68 65 20 72 65 73 70 6f 6e 73 65 20  ** The response 
32998 74 6f 20 61 6e 20 65 72 72 6f 72 20 64 65 70 65  to an error depe
32999 6e 64 73 20 75 70 6f 6e 20 77 68 65 74 68 65 72  nds upon whether
3299a 20 6f 72 20 6e 6f 74 20 74 68 65 0a 20 20 20 20   or not the.    
3299b 20 20 2a 2a 20 67 72 61 6d 6d 61 72 20 64 65 66    ** grammar def
3299c 69 6e 65 73 20 61 6e 20 65 72 72 6f 72 20 74 6f  ines an error to
3299d 6b 65 6e 20 22 45 52 52 4f 52 22 2e 20 20 0a 20  ken "ERROR".  . 
3299e 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
3299f 20 54 68 69 73 20 69 73 20 77 68 61 74 20 77 65   This is what we
329a0 20 64 6f 20 69 66 20 74 68 65 20 67 72 61 6d 6d   do if the gramm
329a1 61 72 20 64 6f 65 73 20 64 65 66 69 6e 65 20 45  ar does define E
329a2 52 52 4f 52 3a 0a 20 20 20 20 20 20 2a 2a 0a 20  RROR:.      **. 
329a3 20 20 20 20 20 2a 2a 20 20 2a 20 43 61 6c 6c 20       **  * Call 
329a4 74 68 65 20 25 73 79 6e 74 61 78 5f 65 72 72 6f  the %syntax_erro
329a5 72 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20  r function..    
329a6 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a    **.      **  *
329a7 20 42 65 67 69 6e 20 70 6f 70 70 69 6e 67 20 74   Begin popping t
329a8 68 65 20 73 74 61 63 6b 20 75 6e 74 69 6c 20 77  he stack until w
329a9 65 20 65 6e 74 65 72 20 61 20 73 74 61 74 65 20  e enter a state 
329aa 77 68 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 20  where.      **  
329ab 20 20 69 74 20 69 73 20 6c 65 67 61 6c 20 74 6f    it is legal to
329ac 20 73 68 69 66 74 20 74 68 65 20 65 72 72 6f 72   shift the error
329ad 20 73 79 6d 62 6f 6c 2c 20 74 68 65 6e 20 73 68   symbol, then sh
329ae 69 66 74 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  ift.      **    
329af 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c  the error symbol
329b0 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
329b1 20 2a 2a 20 20 2a 20 53 65 74 20 74 68 65 20 65   **  * Set the e
329b2 72 72 6f 72 20 63 6f 75 6e 74 20 74 6f 20 74 68  rror count to th
329b3 72 65 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ree..      **.  
329b4 20 20 20 20 2a 2a 20 20 2a 20 42 65 67 69 6e 20      **  * Begin 
329b5 61 63 63 65 70 74 69 6e 67 20 61 6e 64 20 73 68  accepting and sh
329b6 69 66 74 69 6e 67 20 6e 65 77 20 74 6f 6b 65 6e  ifting new token
329b7 73 2e 20 20 4e 6f 20 6e 65 77 20 65 72 72 6f 72  s.  No new error
329b8 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 70 72 6f  .      **    pro
329b9 63 65 73 73 69 6e 67 20 77 69 6c 6c 20 6f 63 63  cessing will occ
329ba 75 72 20 75 6e 74 69 6c 20 74 68 72 65 65 20 74  ur until three t
329bb 6f 6b 65 6e 73 20 68 61 76 65 20 62 65 65 6e 0a  okens have been.
329bc 20 20 20 20 20 20 2a 2a 20 20 20 20 73 68 69 66        **    shif
329bd 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ted successfully
329be 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
329bf 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 79 79   */.      if( yy
329c0 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e  pParser->yyerrcn
329c1 74 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 79  t<0 ){.        y
329c2 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 79  y_syntax_error(y
329c3 79 70 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72  ypParser,yymajor
329c4 2c 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a  ,yyminorunion);.
329c5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79        }.      yy
329c6 6d 78 20 3d 20 79 79 70 50 61 72 73 65 72 2d 3e  mx = yypParser->
329c7 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65  yystack[yypParse
329c8 72 2d 3e 79 79 69 64 78 5d 2e 6d 61 6a 6f 72 3b  r->yyidx].major;
329c9 0a 20 20 20 20 20 20 69 66 28 20 79 79 6d 78 3d  .      if( yymx=
329ca 3d 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 7c  =YYERRORSYMBOL |
329cb 7c 20 79 79 65 72 72 6f 72 68 69 74 20 29 7b 0a  | yyerrorhit ){.
329cc 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
329cd 20 20 20 20 20 20 20 69 66 28 20 79 79 54 72 61         if( yyTra
329ce 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20  ceFILE ){.      
329cf 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
329d0 61 63 65 46 49 4c 45 2c 22 25 73 44 69 73 63 61  aceFILE,"%sDisca
329d1 72 64 20 69 6e 70 75 74 20 74 6f 6b 65 6e 20 25  rd input token %
329d2 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
329d3 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74     yyTracePrompt
329d4 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d  ,yyTokenName[yym
329d5 61 6a 6f 72 5d 29 3b 0a 20 20 20 20 20 20 20 20  ajor]);.        
329d6 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  }.#endif.       
329d7 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 79   yy_destructor(y
329d8 79 70 50 61 72 73 65 72 2c 20 28 59 59 43 4f 44  ypParser, (YYCOD
329d9 45 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79  ETYPE)yymajor,&y
329da 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
329db 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20        yymajor = 
329dc 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20  YYNOCODE;.      
329dd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
329de 77 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20 20  while(.         
329df 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
329e0 78 20 3e 3d 20 30 20 26 26 0a 20 20 20 20 20 20  x >= 0 &&.      
329e1 20 20 20 20 79 79 6d 78 20 21 3d 20 59 59 45 52      yymx != YYER
329e2 52 4f 52 53 59 4d 42 4f 4c 20 26 26 0a 20 20 20  RORSYMBOL &&.   
329e3 20 20 20 20 20 20 20 28 79 79 61 63 74 20 3d 20         (yyact = 
329e4 79 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61  yy_find_reduce_a
329e5 63 74 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 20  ction(.         
329e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 79                 y
329e7 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63  ypParser->yystac
329e8 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  k[yypParser->yyi
329e9 64 78 5d 2e 73 74 61 74 65 6e 6f 2c 0a 20 20 20  dx].stateno,.   
329ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329eb 20 20 20 20 20 59 59 45 52 52 4f 52 53 59 4d 42       YYERRORSYMB
329ec 4f 4c 29 29 20 3e 3d 20 59 59 4e 53 54 41 54 45  OL)) >= YYNSTATE
329ed 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20  .        ){.    
329ee 20 20 20 20 20 20 79 79 5f 70 6f 70 5f 70 61 72        yy_pop_par
329ef 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72  ser_stack(yypPar
329f0 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ser);.        }.
329f1 20 20 20 20 20 20 20 20 69 66 28 20 79 79 70 50          if( yypP
329f2 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3c 20 30  arser->yyidx < 0
329f3 20 7c 7c 20 79 79 6d 61 6a 6f 72 3d 3d 30 20 29   || yymajor==0 )
329f4 7b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 64  {.          yy_d
329f5 65 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72  estructor(yypPar
329f6 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45 29  ser,(YYCODETYPE)
329f7 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72  yymajor,&yyminor
329f8 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 20 20  union);.        
329f9 20 20 79 79 5f 70 61 72 73 65 5f 66 61 69 6c 65    yy_parse_faile
329fa 64 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20  d(yypParser);.  
329fb 20 20 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20          yymajor 
329fc 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20  = YYNOCODE;.    
329fd 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 79 79      }else if( yy
329fe 6d 78 21 3d 59 59 45 52 52 4f 52 53 59 4d 42 4f  mx!=YYERRORSYMBO
329ff 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 59  L ){.          Y
32a00 59 4d 49 4e 4f 52 54 59 50 45 20 75 32 3b 0a 20  YMINORTYPE u2;. 
32a01 20 20 20 20 20 20 20 20 20 75 32 2e 59 59 45 52           u2.YYER
32a02 52 53 59 4d 44 54 20 3d 20 30 3b 0a 20 20 20 20  RSYMDT = 0;.    
32a03 20 20 20 20 20 20 79 79 5f 73 68 69 66 74 28 79        yy_shift(y
32a04 79 70 50 61 72 73 65 72 2c 79 79 61 63 74 2c 59  ypParser,yyact,Y
32a05 59 45 52 52 4f 52 53 59 4d 42 4f 4c 2c 26 75 32  YERRORSYMBOL,&u2
32a06 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
32a07 20 20 20 7d 0a 20 20 20 20 20 20 79 79 70 50 61     }.      yypPa
32a08 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 20 3d  rser->yyerrcnt =
32a09 20 33 3b 0a 20 20 20 20 20 20 79 79 65 72 72 6f   3;.      yyerro
32a0a 72 68 69 74 20 3d 20 31 3b 0a 23 65 6c 69 66 20  rhit = 1;.#elif 
32a0b 64 65 66 69 6e 65 64 28 59 59 4e 4f 45 52 52 4f  defined(YYNOERRO
32a0c 52 52 45 43 4f 56 45 52 59 29 0a 20 20 20 20 20  RRECOVERY).     
32a0d 20 2f 2a 20 49 66 20 74 68 65 20 59 59 4e 4f 45   /* If the YYNOE
32a0e 52 52 4f 52 52 45 43 4f 56 45 52 59 20 6d 61 63  RRORRECOVERY mac
32a0f 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74  ro is defined, t
32a10 68 65 6e 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d  hen do not attem
32a11 70 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 64  pt to.      ** d
32a12 6f 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72  o any kind of er
32a13 72 6f 72 20 72 65 63 6f 76 65 72 79 2e 20 20 49  ror recovery.  I
32a14 6e 73 74 65 61 64 2c 20 73 69 6d 70 6c 79 20 69  nstead, simply i
32a15 6e 76 6f 6b 65 20 74 68 65 20 73 79 6e 74 61 78  nvoke the syntax
32a16 0a 20 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 20  .      ** error 
32a17 72 6f 75 74 69 6e 65 20 61 6e 64 20 63 6f 6e 74  routine and cont
32a18 69 6e 75 65 20 67 6f 69 6e 67 20 61 73 20 69 66  inue going as if
32a19 20 6e 6f 74 68 69 6e 67 20 68 61 64 20 68 61 70   nothing had hap
32a1a 70 65 6e 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a  pened..      **.
32a1b 20 20 20 20 20 20 2a 2a 20 41 70 70 6c 69 63 61        ** Applica
32a1c 74 69 6f 6e 73 20 63 61 6e 20 73 65 74 20 74 68  tions can set th
32a1d 69 73 20 6d 61 63 72 6f 20 28 66 6f 72 20 65 78  is macro (for ex
32a1e 61 6d 70 6c 65 20 69 6e 73 69 64 65 20 25 69 6e  ample inside %in
32a1f 63 6c 75 64 65 29 20 69 66 0a 20 20 20 20 20 20  clude) if.      
32a20 2a 2a 20 74 68 65 79 20 69 6e 74 65 6e 64 20 74  ** they intend t
32a21 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 70 61  o abandon the pa
32a22 72 73 65 20 75 70 6f 6e 20 74 68 65 20 66 69 72  rse upon the fir
32a23 73 74 20 73 79 6e 74 61 78 20 65 72 72 6f 72 20  st syntax error 
32a24 73 65 65 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  seen..      */. 
32a25 20 20 20 20 20 79 79 5f 73 79 6e 74 61 78 5f 65       yy_syntax_e
32a26 72 72 6f 72 28 79 79 70 50 61 72 73 65 72 2c 79  rror(yypParser,y
32a27 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e  ymajor,yyminorun
32a28 69 6f 6e 29 3b 0a 20 20 20 20 20 20 79 79 5f 64  ion);.      yy_d
32a29 65 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72  estructor(yypPar
32a2a 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45 29  ser,(YYCODETYPE)
32a2b 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72  yymajor,&yyminor
32a2c 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 79 79  union);.      yy
32a2d 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45  major = YYNOCODE
32a2e 3b 0a 20 20 20 20 20 20 0a 23 65 6c 73 65 20 20  ;.      .#else  
32a2f 2f 2a 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c  /* YYERRORSYMBOL
32a30 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20   is not defined 
32a31 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  */.      /* This
32a32 20 69 73 20 77 68 61 74 20 77 65 20 64 6f 20 69   is what we do i
32a33 66 20 74 68 65 20 67 72 61 6d 6d 61 72 20 64 6f  f the grammar do
32a34 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 45 52  es not define ER
32a35 52 4f 52 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ROR:.      **.  
32a36 20 20 20 20 2a 2a 20 20 2a 20 52 65 70 6f 72 74      **  * Report
32a37 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
32a38 65 2c 20 61 6e 64 20 74 68 72 6f 77 20 61 77 61  e, and throw awa
32a39 79 20 74 68 65 20 69 6e 70 75 74 20 74 6f 6b 65  y the input toke
32a3a 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  n..      **.    
32a3b 20 20 2a 2a 20 20 2a 20 49 66 20 74 68 65 20 69    **  * If the i
32a3c 6e 70 75 74 20 74 6f 6b 65 6e 20 69 73 20 24 2c  nput token is $,
32a3d 20 74 68 65 6e 20 66 61 69 6c 20 74 68 65 20 70   then fail the p
32a3e 61 72 73 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  arse..      **. 
32a3f 20 20 20 20 20 2a 2a 20 41 73 20 62 65 66 6f 72       ** As befor
32a40 65 2c 20 73 75 62 73 65 71 75 65 6e 74 20 65 72  e, subsequent er
32a41 72 6f 72 20 6d 65 73 73 61 67 65 73 20 61 72 65  ror messages are
32a42 20 73 75 70 70 72 65 73 73 65 64 20 75 6e 74 69   suppressed unti
32a43 6c 0a 20 20 20 20 20 20 2a 2a 20 74 68 72 65 65  l.      ** three
32a44 20 69 6e 70 75 74 20 74 6f 6b 65 6e 73 20 68 61   input tokens ha
32a45 76 65 20 62 65 65 6e 20 73 75 63 63 65 73 73 66  ve been successf
32a46 75 6c 6c 79 20 73 68 69 66 74 65 64 2e 0a 20 20  ully shifted..  
32a47 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
32a48 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72   yypParser->yyer
32a49 72 63 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 20  rcnt<=0 ){.     
32a4a 20 20 20 79 79 5f 73 79 6e 74 61 78 5f 65 72 72     yy_syntax_err
32a4b 6f 72 28 79 79 70 50 61 72 73 65 72 2c 79 79 6d  or(yypParser,yym
32a4c 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e 69 6f  ajor,yyminorunio
32a4d 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
32a4e 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65    yypParser->yye
32a4f 72 72 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 20  rrcnt = 3;.     
32a50 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 79   yy_destructor(y
32a51 79 70 50 61 72 73 65 72 2c 28 59 59 43 4f 44 45  ypParser,(YYCODE
32a52 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79 79  TYPE)yymajor,&yy
32a53 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
32a54 20 20 20 69 66 28 20 79 79 65 6e 64 6f 66 69 6e     if( yyendofin
32a55 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 79  put ){.        y
32a56 79 5f 70 61 72 73 65 5f 66 61 69 6c 65 64 28 79  y_parse_failed(y
32a57 79 70 50 61 72 73 65 72 29 3b 0a 20 20 20 20 20  ypParser);.     
32a58 20 7d 0a 20 20 20 20 20 20 79 79 6d 61 6a 6f 72   }.      yymajor
32a59 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 23 65 6e   = YYNOCODE;.#en
32a5a 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69  dif.    }.  }whi
32a5b 6c 65 28 20 79 79 6d 61 6a 6f 72 21 3d 59 59 4e  le( yymajor!=YYN
32a5c 4f 43 4f 44 45 20 26 26 20 79 79 70 50 61 72 73  OCODE && yypPars
32a5d 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 3b 0a  er->yyidx>=0 );.
32a5e 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a    return;.}../**
32a5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
32a60 20 6f 66 20 70 61 72 73 65 2e 63 20 2a 2a 2a 2a   of parse.c ****
32a61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
32a64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
32a65 69 6e 20 66 69 6c 65 20 74 6f 6b 65 6e 69 7a 65  in file tokenize
32a66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
32a67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
32a69 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
32a6a 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
32a6b 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
32a6c 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
32a6d 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
32a6e 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
32a6f 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
32a70 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
32a71 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
32a72 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
32a73 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
32a74 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
32a75 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
32a76 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
32a77 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
32a78 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
32a79 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
32a7a 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
32a7b 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
32a7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a80 2a 2a 2a 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e 69  ***.** An tokeni
32a81 7a 65 72 20 66 6f 72 20 53 51 4c 0a 2a 2a 0a 2a  zer for SQL.**.*
32a82 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
32a83 61 69 6e 73 20 43 20 63 6f 64 65 20 74 68 61 74  ains C code that
32a84 20 73 70 6c 69 74 73 20 61 6e 20 53 51 4c 20 69   splits an SQL i
32a85 6e 70 75 74 20 73 74 72 69 6e 67 20 75 70 20 69  nput string up i
32a86 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61  nto.** individua
32a87 6c 20 74 6f 6b 65 6e 73 20 61 6e 64 20 73 65 6e  l tokens and sen
32a88 64 73 20 74 68 6f 73 65 20 74 6f 6b 65 6e 73 20  ds those tokens 
32a89 6f 6e 65 2d 62 79 2d 6f 6e 65 20 6f 76 65 72 20  one-by-one over 
32a8a 74 6f 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 72  to the.** parser
32a8b 20 66 6f 72 20 61 6e 61 6c 79 73 69 73 2e 0a 2a   for analysis..*
32a8c 2a 0a 2a 2a 20 24 49 64 3a 20 74 6f 6b 65 6e 69  *.** $Id: tokeni
32a8d 7a 65 2e 63 2c 76 20 31 2e 31 36 33 20 32 30 30  ze.c,v 1.163 200
32a8e 39 2f 30 37 2f 30 33 20 32 32 3a 35 34 3a 33 37  9/07/03 22:54:37
32a8f 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
32a90 2a 0a 2a 2a 20 54 68 65 20 63 68 61 72 4d 61 70  *.** The charMap
32a91 28 29 20 6d 61 63 72 6f 20 6d 61 70 73 20 61 6c  () macro maps al
32a92 70 68 61 62 65 74 69 63 20 63 68 61 72 61 63 74  phabetic charact
32a93 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72 0a 2a  ers into their.*
32a94 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 43  * lower-case ASC
32a95 49 49 20 65 71 75 69 76 61 6c 65 6e 74 2e 20 20  II equivalent.  
32a96 4f 6e 20 41 53 43 49 49 20 6d 61 63 68 69 6e 65  On ASCII machine
32a97 73 2c 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a  s, this is just.
32a98 2a 2a 20 61 6e 20 75 70 70 65 72 2d 74 6f 2d 6c  ** an upper-to-l
32a99 6f 77 65 72 20 63 61 73 65 20 6d 61 70 2e 20 20  ower case map.  
32a9a 4f 6e 20 45 42 43 44 49 43 20 6d 61 63 68 69 6e  On EBCDIC machin
32a9b 65 73 20 77 65 20 61 6c 73 6f 20 6e 65 65 64 0a  es we also need.
32a9c 2a 2a 20 74 6f 20 61 64 6a 75 73 74 20 74 68 65  ** to adjust the
32a9d 20 65 6e 63 6f 64 69 6e 67 2e 20 20 4f 6e 6c 79   encoding.  Only
32a9e 20 61 6c 70 68 61 62 65 74 69 63 20 63 68 61 72   alphabetic char
32a9f 61 63 74 65 72 73 20 61 6e 64 20 75 6e 64 65 72  acters and under
32aa0 73 63 6f 72 65 73 0a 2a 2a 20 6e 65 65 64 20 74  scores.** need t
32aa1 6f 20 62 65 20 74 72 61 6e 73 6c 61 74 65 64 2e  o be translated.
32aa2 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
32aa3 45 5f 41 53 43 49 49 0a 23 20 64 65 66 69 6e 65  E_ASCII.# define
32aa4 20 63 68 61 72 4d 61 70 28 58 29 20 73 71 6c 69   charMap(X) sqli
32aa5 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
32aa6 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 58  (unsigned char)X
32aa7 5d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ].#endif.#ifdef 
32aa8 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 23 20  SQLITE_EBCDIC.# 
32aa9 64 65 66 69 6e 65 20 63 68 61 72 4d 61 70 28 58  define charMap(X
32aaa 29 20 65 62 63 64 69 63 54 6f 41 73 63 69 69 5b  ) ebcdicToAscii[
32aab 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 58  (unsigned char)X
32aac 5d 0a 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  ].const unsigned
32aad 20 63 68 61 72 20 65 62 63 64 69 63 54 6f 41 73   char ebcdicToAs
32aae 63 69 69 5b 5d 20 3d 20 7b 0a 2f 2a 20 30 20 20  cii[] = {./* 0  
32aaf 20 31 20 20 20 32 20 20 20 33 20 20 20 34 20 20   1   2   3   4  
32ab0 20 35 20 20 20 36 20 20 20 37 20 20 20 38 20 20   5   6   7   8  
32ab1 20 39 20 20 20 41 20 20 20 42 20 20 20 43 20 20   9   A   B   C  
32ab2 20 44 20 20 20 45 20 20 20 46 20 2a 2f 0a 20 20   D   E   F */.  
32ab3 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab4 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab5 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab6 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab7 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 30 2c 20   /* 0x */.   0, 
32ab8 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab9 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aba 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32abb 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32abc 31 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20  1x */.   0,  0, 
32abd 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32abe 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32abf 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ac0 20 30 2c 20 20 30 2c 20 20 2f 2a 20 32 78 20 2a   0,  0,  /* 2x *
32ac1 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  /.   0,  0,  0, 
32ac2 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ac3 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ac4 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ac5 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20   0,  /* 3x */.  
32ac6 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ac7 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ac8 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ac9 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aca 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 30 2c 20   /* 4x */.   0, 
32acb 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32acc 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32acd 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ace 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32acf 35 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20  5x */.   0,  0, 
32ad0 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ad1 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ad2 20 30 2c 20 20 30 2c 20 20 30 2c 20 39 35 2c 20   0,  0,  0, 95, 
32ad3 20 30 2c 20 20 30 2c 20 20 2f 2a 20 36 78 20 2a   0,  0,  /* 6x *
32ad4 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  /.   0,  0,  0, 
32ad5 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ad6 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ad7 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ad8 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20   0,  /* 7x */.  
32ad9 20 30 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31   0, 97, 98, 99,1
32ada 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 31  00,101,102,103,1
32adb 30 34 2c 31 30 35 2c 20 20 30 2c 20 20 30 2c 20  04,105,  0,  0, 
32adc 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32add 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 30 2c 31   /* 8x */.   0,1
32ade 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31  06,107,108,109,1
32adf 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31  10,111,112,113,1
32ae0 31 34 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  14,  0,  0,  0, 
32ae1 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32ae2 39 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 31  9x */.   0,  0,1
32ae3 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31  15,116,117,118,1
32ae4 31 39 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 20  19,120,121,122, 
32ae5 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ae6 20 30 2c 20 20 30 2c 20 20 2f 2a 20 41 78 20 2a   0,  0,  /* Ax *
32ae7 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  /.   0,  0,  0, 
32ae8 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ae9 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aea 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aeb 20 30 2c 20 20 2f 2a 20 42 78 20 2a 2f 0a 20 20   0,  /* Bx */.  
32aec 20 30 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31   0, 97, 98, 99,1
32aed 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 31  00,101,102,103,1
32aee 30 34 2c 31 30 35 2c 20 20 30 2c 20 20 30 2c 20  04,105,  0,  0, 
32aef 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32af0 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 30 2c 31   /* Cx */.   0,1
32af1 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31  06,107,108,109,1
32af2 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31  10,111,112,113,1
32af3 31 34 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  14,  0,  0,  0, 
32af4 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32af5 44 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 31  Dx */.   0,  0,1
32af6 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31  15,116,117,118,1
32af7 31 39 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 20  19,120,121,122, 
32af8 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32af9 20 30 2c 20 20 30 2c 20 20 2f 2a 20 45 78 20 2a   0,  0,  /* Ex *
32afa 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  /.   0,  0,  0, 
32afb 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32afc 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32afd 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32afe 20 30 2c 20 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b   0,  /* Fx */.};
32aff 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
32b00 68 65 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72  he sqlite3Keywor
32b01 64 43 6f 64 65 20 66 75 6e 63 74 69 6f 6e 20 6c  dCode function l
32b02 6f 6f 6b 73 20 75 70 20 61 6e 20 69 64 65 6e 74  ooks up an ident
32b03 69 66 69 65 72 20 74 6f 20 64 65 74 65 72 6d 69  ifier to determi
32b04 6e 65 20 69 66 0a 2a 2a 20 69 74 20 69 73 20 61  ne if.** it is a
32b05 20 6b 65 79 77 6f 72 64 2e 20 20 49 66 20 69 74   keyword.  If it
32b06 20 69 73 20 61 20 6b 65 79 77 6f 72 64 2c 20 74   is a keyword, t
32b07 68 65 20 74 6f 6b 65 6e 20 63 6f 64 65 20 6f 66  he token code of
32b08 20 74 68 61 74 20 6b 65 79 77 6f 72 64 20 69 73   that keyword is
32b09 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 20   .** returned.  
32b0a 49 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 20  If the input is 
32b0b 6e 6f 74 20 61 20 6b 65 79 77 6f 72 64 2c 20 54  not a keyword, T
32b0c 4b 5f 49 44 20 69 73 20 72 65 74 75 72 6e 65 64  K_ID is returned
32b0d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c  ..**.** The impl
32b0e 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
32b0f 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 67  is routine was g
32b10 65 6e 65 72 61 74 65 64 20 62 79 20 61 20 70 72  enerated by a pr
32b11 6f 67 72 61 6d 2c 0a 2a 2a 20 6d 6b 6b 65 79 77  ogram,.** mkkeyw
32b12 6f 72 64 68 61 73 68 2e 68 2c 20 6c 6f 63 61 74  ordhash.h, locat
32b13 65 64 20 69 6e 20 74 68 65 20 74 6f 6f 6c 20 73  ed in the tool s
32b14 75 62 64 69 72 65 63 74 6f 72 79 20 6f 66 20 74  ubdirectory of t
32b15 68 65 20 64 69 73 74 72 69 62 75 74 69 6f 6e 2e  he distribution.
32b16 0a 2a 2a 20 54 68 65 20 6f 75 74 70 75 74 20 6f  .** The output o
32b17 66 20 74 68 65 20 6d 6b 6b 65 79 77 6f 72 64 68  f the mkkeywordh
32b18 61 73 68 2e 63 20 70 72 6f 67 72 61 6d 20 69 73  ash.c program is
32b19 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20   written into a 
32b1a 66 69 6c 65 0a 2a 2a 20 6e 61 6d 65 64 20 6b 65  file.** named ke
32b1b 79 77 6f 72 64 68 61 73 68 2e 68 20 61 6e 64 20  ywordhash.h and 
32b1c 74 68 65 6e 20 69 6e 63 6c 75 64 65 64 20 69 6e  then included in
32b1d 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 66  to this source f
32b1e 69 6c 65 20 62 79 0a 2a 2a 20 74 68 65 20 23 69  ile by.** the #i
32b1f 6e 63 6c 75 64 65 20 62 65 6c 6f 77 2e 0a 2a 2f  nclude below..*/
32b20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
32b21 20 49 6e 63 6c 75 64 65 20 6b 65 79 77 6f 72 64   Include keyword
32b22 68 61 73 68 2e 68 20 69 6e 20 74 68 65 20 6d 69  hash.h in the mi
32b23 64 64 6c 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65  ddle of tokenize
32b24 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  .c ************/
32b25 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
32b26 20 42 65 67 69 6e 20 66 69 6c 65 20 6b 65 79 77   Begin file keyw
32b27 6f 72 64 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a  ordhash.h ******
32b28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32b29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
32b2a 0a 2f 2a 2a 2a 2a 2a 20 54 68 69 73 20 66 69 6c  ./***** This fil
32b2b 65 20 63 6f 6e 74 61 69 6e 73 20 61 75 74 6f 6d  e contains autom
32b2c 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74  atically generat
32b2d 65 64 20 63 6f 64 65 20 2a 2a 2a 2a 2a 2a 0a 2a  ed code ******.*
32b2e 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
32b2f 20 74 68 69 73 20 66 69 6c 65 20 68 61 73 20 62   this file has b
32b30 65 65 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  een automaticall
32b31 79 20 67 65 6e 65 72 61 74 65 64 20 62 79 0a 2a  y generated by.*
32b32 2a 0a 2a 2a 20 20 20 73 71 6c 69 74 65 2f 74 6f  *.**   sqlite/to
32b33 6f 6c 2f 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68  ol/mkkeywordhash
32b34 2e 63 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  .c.**.** The cod
32b35 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
32b36 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e 63  mplements a func
32b37 74 69 6f 6e 20 74 68 61 74 20 64 65 74 65 72 6d  tion that determ
32b38 69 6e 65 73 20 77 68 65 74 68 65 72 0a 2a 2a 20  ines whether.** 
32b39 6f 72 20 6e 6f 74 20 61 20 67 69 76 65 6e 20 69  or not a given i
32b3a 64 65 6e 74 69 66 69 65 72 20 69 73 20 72 65 61  dentifier is rea
32b3b 6c 6c 79 20 61 6e 20 53 51 4c 20 6b 65 79 77 6f  lly an SQL keywo
32b3c 72 64 2e 20 20 54 68 65 20 73 61 6d 65 20 74 68  rd.  The same th
32b3d 69 6e 67 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20  ing.** might be 
32b3e 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6d 6f 72 65  implemented more
32b3f 20 64 69 72 65 63 74 6c 79 20 75 73 69 6e 67 20   directly using 
32b40 61 20 68 61 6e 64 2d 77 72 69 74 74 65 6e 20 68  a hand-written h
32b41 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2a 20 42 75  ash table..** Bu
32b42 74 20 62 79 20 75 73 69 6e 67 20 74 68 69 73 20  t by using this 
32b43 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  automatically ge
32b44 6e 65 72 61 74 65 64 20 63 6f 64 65 2c 20 74 68  nerated code, th
32b45 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 63 6f  e size of the co
32b46 64 65 0a 2a 2a 20 69 73 20 73 75 62 73 74 61 6e  de.** is substan
32b47 74 69 61 6c 6c 79 20 72 65 64 75 63 65 64 2e 20  tially reduced. 
32b48 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61   This is importa
32b49 6e 74 20 66 6f 72 20 65 6d 62 65 64 64 65 64 20  nt for embedded 
32b4a 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20  applications.** 
32b4b 6f 6e 20 70 6c 61 74 66 6f 72 6d 73 20 77 69 74  on platforms wit
32b4c 68 20 6c 69 6d 69 74 65 64 20 6d 65 6d 6f 72 79  h limited memory
32b4d 2e 0a 2a 2f 0a 2f 2a 20 48 61 73 68 20 73 63 6f  ..*/./* Hash sco
32b4e 72 65 3a 20 31 37 35 20 2a 2f 0a 73 74 61 74 69  re: 175 */.stati
32b4f 63 20 69 6e 74 20 6b 65 79 77 6f 72 64 43 6f 64  c int keywordCod
32b50 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  e(const char *z,
32b51 20 69 6e 74 20 6e 29 7b 0a 20 20 2f 2a 20 7a 54   int n){.  /* zT
32b52 65 78 74 5b 5d 20 65 6e 63 6f 64 65 73 20 38 31  ext[] encodes 81
32b53 31 20 62 79 74 65 73 20 6f 66 20 6b 65 79 77 6f  1 bytes of keywo
32b54 72 64 73 20 69 6e 20 35 34 31 20 62 79 74 65 73  rds in 541 bytes
32b55 20 2a 2f 0a 20 20 2f 2a 20 20 20 52 45 49 4e 44   */.  /*   REIND
32b56 45 58 45 44 45 53 43 41 50 45 41 43 48 45 43 4b  EXEDESCAPEACHECK
32b57 45 59 42 45 46 4f 52 45 49 47 4e 4f 52 45 47 45  EYBEFOREIGNOREGE
32b58 58 50 4c 41 49 4e 53 54 45 41 44 44 41 54 41 42  XPLAINSTEADDATAB
32b59 41 53 45 4c 45 43 54 20 20 20 20 20 20 20 2a 2f  ASELECT       */
32b5a 0a 20 20 2f 2a 20 20 20 41 42 4c 45 46 54 48 45  .  /*   ABLEFTHE
32b5b 4e 44 45 46 45 52 52 41 42 4c 45 4c 53 45 58 43  NDEFERRABLELSEXC
32b5c 45 50 54 52 41 4e 53 41 43 54 49 4f 4e 41 54 55  EPTRANSACTIONATU
32b5d 52 41 4c 54 45 52 41 49 53 45 58 43 4c 55 53 49  RALTERAISEXCLUSI
32b5e 56 45 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  VE         */.  
32b5f 2f 2a 20 20 20 58 49 53 54 53 41 56 45 50 4f 49  /*   XISTSAVEPOI
32b60 4e 54 45 52 53 45 43 54 52 49 47 47 45 52 45 46  NTERSECTRIGGEREF
32b61 45 52 45 4e 43 45 53 43 4f 4e 53 54 52 41 49 4e  ERENCESCONSTRAIN
32b62 54 4f 46 46 53 45 54 45 4d 50 4f 52 41 52 59 20  TOFFSETEMPORARY 
32b63 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20          */.  /* 
32b64 20 20 55 4e 49 51 55 45 52 59 41 54 54 41 43 48    UNIQUERYATTACH
32b65 41 56 49 4e 47 52 4f 55 50 44 41 54 45 42 45 47  AVINGROUPDATEBEG
32b66 49 4e 4e 45 52 45 4c 45 41 53 45 42 45 54 57 45  INNERELEASEBETWE
32b67 45 4e 4f 54 4e 55 4c 4c 49 4b 45 20 20 20 20 20  ENOTNULLIKE     
32b68 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 43       */.  /*   C
32b69 41 53 43 41 44 45 4c 45 54 45 43 41 53 45 43 4f  ASCADELETECASECO
32b6a 4c 4c 41 54 45 43 52 45 41 54 45 43 55 52 52 45  LLATECREATECURRE
32b6b 4e 54 5f 44 41 54 45 44 45 54 41 43 48 49 4d 4d  NT_DATEDETACHIMM
32b6c 45 44 49 41 54 45 4a 4f 49 4e 20 20 20 20 20 20  EDIATEJOIN      
32b6d 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 53 45 52 54    */.  /*   SERT
32b6e 4d 41 54 43 48 50 4c 41 4e 41 4c 59 5a 45 50 52  MATCHPLANALYZEPR
32b6f 41 47 4d 41 42 4f 52 54 56 41 4c 55 45 53 56 49  AGMABORTVALUESVI
32b70 52 54 55 41 4c 49 4d 49 54 57 48 45 4e 57 48 45  RTUALIMITWHENWHE
32b71 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20 20 2a  RENAME         *
32b72 2f 0a 20 20 2f 2a 20 20 20 41 46 54 45 52 45 50  /.  /*   AFTEREP
32b73 4c 41 43 45 41 4e 44 45 46 41 55 4c 54 41 55 54  LACEANDEFAULTAUT
32b74 4f 49 4e 43 52 45 4d 45 4e 54 43 41 53 54 43 4f  OINCREMENTCASTCO
32b75 4c 55 4d 4e 43 4f 4d 4d 49 54 43 4f 4e 46 4c 49  LUMNCOMMITCONFLI
32b76 43 54 43 52 4f 53 53 20 20 20 20 20 2a 2f 0a 20  CTCROSS     */. 
32b77 20 2f 2a 20 20 20 43 55 52 52 45 4e 54 5f 54 49   /*   CURRENT_TI
32b78 4d 45 53 54 41 4d 50 52 49 4d 41 52 59 44 45 46  MESTAMPRIMARYDEF
32b79 45 52 52 45 44 49 53 54 49 4e 43 54 44 52 4f 50  ERREDISTINCTDROP
32b7a 46 41 49 4c 46 52 4f 4d 46 55 4c 4c 47 4c 4f 42  FAILFROMFULLGLOB
32b7b 59 49 46 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a  YIF      */.  /*
32b7c 20 20 20 49 53 4e 55 4c 4c 4f 52 44 45 52 45 53     ISNULLORDERES
32b7d 54 52 49 43 54 4f 55 54 45 52 49 47 48 54 52 4f  TRICTOUTERIGHTRO
32b7e 4c 4c 42 41 43 4b 52 4f 57 55 4e 49 4f 4e 55 53  LLBACKROWUNIONUS
32b7f 49 4e 47 56 41 43 55 55 4d 56 49 45 57 20 20 20  INGVACUUMVIEW   
32b80 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20        */.  /*   
32b81 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20  INITIALLY       
32b82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b85 20 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63     */.  static c
32b86 6f 6e 73 74 20 63 68 61 72 20 7a 54 65 78 74 5b  onst char zText[
32b87 35 34 30 5d 20 3d 20 7b 0a 20 20 20 20 27 52 27  540] = {.    'R'
32b88 2c 27 45 27 2c 27 49 27 2c 27 4e 27 2c 27 44 27  ,'E','I','N','D'
32b89 2c 27 45 27 2c 27 58 27 2c 27 45 27 2c 27 44 27  ,'E','X','E','D'
32b8a 2c 27 45 27 2c 27 53 27 2c 27 43 27 2c 27 41 27  ,'E','S','C','A'
32b8b 2c 27 50 27 2c 27 45 27 2c 27 41 27 2c 27 43 27  ,'P','E','A','C'
32b8c 2c 27 48 27 2c 0a 20 20 20 20 27 45 27 2c 27 43  ,'H',.    'E','C
32b8d 27 2c 27 4b 27 2c 27 45 27 2c 27 59 27 2c 27 42  ','K','E','Y','B
32b8e 27 2c 27 45 27 2c 27 46 27 2c 27 4f 27 2c 27 52  ','E','F','O','R
32b8f 27 2c 27 45 27 2c 27 49 27 2c 27 47 27 2c 27 4e  ','E','I','G','N
32b90 27 2c 27 4f 27 2c 27 52 27 2c 27 45 27 2c 27 47  ','O','R','E','G
32b91 27 2c 0a 20 20 20 20 27 45 27 2c 27 58 27 2c 27  ',.    'E','X','
32b92 50 27 2c 27 4c 27 2c 27 41 27 2c 27 49 27 2c 27  P','L','A','I','
32b93 4e 27 2c 27 53 27 2c 27 54 27 2c 27 45 27 2c 27  N','S','T','E','
32b94 41 27 2c 27 44 27 2c 27 44 27 2c 27 41 27 2c 27  A','D','D','A','
32b95 54 27 2c 27 41 27 2c 27 42 27 2c 27 41 27 2c 0a  T','A','B','A',.
32b96 20 20 20 20 27 53 27 2c 27 45 27 2c 27 4c 27 2c      'S','E','L',
32b97 27 45 27 2c 27 43 27 2c 27 54 27 2c 27 41 27 2c  'E','C','T','A',
32b98 27 42 27 2c 27 4c 27 2c 27 45 27 2c 27 46 27 2c  'B','L','E','F',
32b99 27 54 27 2c 27 48 27 2c 27 45 27 2c 27 4e 27 2c  'T','H','E','N',
32b9a 27 44 27 2c 27 45 27 2c 27 46 27 2c 0a 20 20 20  'D','E','F',.   
32b9b 20 27 45 27 2c 27 52 27 2c 27 52 27 2c 27 41 27   'E','R','R','A'
32b9c 2c 27 42 27 2c 27 4c 27 2c 27 45 27 2c 27 4c 27  ,'B','L','E','L'
32b9d 2c 27 53 27 2c 27 45 27 2c 27 58 27 2c 27 43 27  ,'S','E','X','C'
32b9e 2c 27 45 27 2c 27 50 27 2c 27 54 27 2c 27 52 27  ,'E','P','T','R'
32b9f 2c 27 41 27 2c 27 4e 27 2c 0a 20 20 20 20 27 53  ,'A','N',.    'S
32ba0 27 2c 27 41 27 2c 27 43 27 2c 27 54 27 2c 27 49  ','A','C','T','I
32ba1 27 2c 27 4f 27 2c 27 4e 27 2c 27 41 27 2c 27 54  ','O','N','A','T
32ba2 27 2c 27 55 27 2c 27 52 27 2c 27 41 27 2c 27 4c  ','U','R','A','L
32ba3 27 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 41  ','T','E','R','A
32ba4 27 2c 27 49 27 2c 0a 20 20 20 20 27 53 27 2c 27  ','I',.    'S','
32ba5 45 27 2c 27 58 27 2c 27 43 27 2c 27 4c 27 2c 27  E','X','C','L','
32ba6 55 27 2c 27 53 27 2c 27 49 27 2c 27 56 27 2c 27  U','S','I','V','
32ba7 45 27 2c 27 58 27 2c 27 49 27 2c 27 53 27 2c 27  E','X','I','S','
32ba8 54 27 2c 27 53 27 2c 27 41 27 2c 27 56 27 2c 27  T','S','A','V','
32ba9 45 27 2c 0a 20 20 20 20 27 50 27 2c 27 4f 27 2c  E',.    'P','O',
32baa 27 49 27 2c 27 4e 27 2c 27 54 27 2c 27 45 27 2c  'I','N','T','E',
32bab 27 52 27 2c 27 53 27 2c 27 45 27 2c 27 43 27 2c  'R','S','E','C',
32bac 27 54 27 2c 27 52 27 2c 27 49 27 2c 27 47 27 2c  'T','R','I','G',
32bad 27 47 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c  'G','E','R','E',
32bae 0a 20 20 20 20 27 46 27 2c 27 45 27 2c 27 52 27  .    'F','E','R'
32baf 2c 27 45 27 2c 27 4e 27 2c 27 43 27 2c 27 45 27  ,'E','N','C','E'
32bb0 2c 27 53 27 2c 27 43 27 2c 27 4f 27 2c 27 4e 27  ,'S','C','O','N'
32bb1 2c 27 53 27 2c 27 54 27 2c 27 52 27 2c 27 41 27  ,'S','T','R','A'
32bb2 2c 27 49 27 2c 27 4e 27 2c 27 54 27 2c 0a 20 20  ,'I','N','T',.  
32bb3 20 20 27 4f 27 2c 27 46 27 2c 27 46 27 2c 27 53    'O','F','F','S
32bb4 27 2c 27 45 27 2c 27 54 27 2c 27 45 27 2c 27 4d  ','E','T','E','M
32bb5 27 2c 27 50 27 2c 27 4f 27 2c 27 52 27 2c 27 41  ','P','O','R','A
32bb6 27 2c 27 52 27 2c 27 59 27 2c 27 55 27 2c 27 4e  ','R','Y','U','N
32bb7 27 2c 27 49 27 2c 27 51 27 2c 0a 20 20 20 20 27  ','I','Q',.    '
32bb8 55 27 2c 27 45 27 2c 27 52 27 2c 27 59 27 2c 27  U','E','R','Y','
32bb9 41 27 2c 27 54 27 2c 27 54 27 2c 27 41 27 2c 27  A','T','T','A','
32bba 43 27 2c 27 48 27 2c 27 41 27 2c 27 56 27 2c 27  C','H','A','V','
32bbb 49 27 2c 27 4e 27 2c 27 47 27 2c 27 52 27 2c 27  I','N','G','R','
32bbc 4f 27 2c 27 55 27 2c 0a 20 20 20 20 27 50 27 2c  O','U',.    'P',
32bbd 27 44 27 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c  'D','A','T','E',
32bbe 27 42 27 2c 27 45 27 2c 27 47 27 2c 27 49 27 2c  'B','E','G','I',
32bbf 27 4e 27 2c 27 4e 27 2c 27 45 27 2c 27 52 27 2c  'N','N','E','R',
32bc0 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27 41 27 2c  'E','L','E','A',
32bc1 27 53 27 2c 0a 20 20 20 20 27 45 27 2c 27 42 27  'S',.    'E','B'
32bc2 2c 27 45 27 2c 27 54 27 2c 27 57 27 2c 27 45 27  ,'E','T','W','E'
32bc3 2c 27 45 27 2c 27 4e 27 2c 27 4f 27 2c 27 54 27  ,'E','N','O','T'
32bc4 2c 27 4e 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27  ,'N','U','L','L'
32bc5 2c 27 49 27 2c 27 4b 27 2c 27 45 27 2c 27 43 27  ,'I','K','E','C'
32bc6 2c 0a 20 20 20 20 27 41 27 2c 27 53 27 2c 27 43  ,.    'A','S','C
32bc7 27 2c 27 41 27 2c 27 44 27 2c 27 45 27 2c 27 4c  ','A','D','E','L
32bc8 27 2c 27 45 27 2c 27 54 27 2c 27 45 27 2c 27 43  ','E','T','E','C
32bc9 27 2c 27 41 27 2c 27 53 27 2c 27 45 27 2c 27 43  ','A','S','E','C
32bca 27 2c 27 4f 27 2c 27 4c 27 2c 27 4c 27 2c 0a 20  ','O','L','L',. 
32bcb 20 20 20 27 41 27 2c 27 54 27 2c 27 45 27 2c 27     'A','T','E','
32bcc 43 27 2c 27 52 27 2c 27 45 27 2c 27 41 27 2c 27  C','R','E','A','
32bcd 54 27 2c 27 45 27 2c 27 43 27 2c 27 55 27 2c 27  T','E','C','U','
32bce 52 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27  R','R','E','N','
32bcf 54 27 2c 27 5f 27 2c 27 44 27 2c 0a 20 20 20 20  T','_','D',.    
32bd0 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 44 27 2c  'A','T','E','D',
32bd1 27 45 27 2c 27 54 27 2c 27 41 27 2c 27 43 27 2c  'E','T','A','C',
32bd2 27 48 27 2c 27 49 27 2c 27 4d 27 2c 27 4d 27 2c  'H','I','M','M',
32bd3 27 45 27 2c 27 44 27 2c 27 49 27 2c 27 41 27 2c  'E','D','I','A',
32bd4 27 54 27 2c 27 45 27 2c 0a 20 20 20 20 27 4a 27  'T','E',.    'J'
32bd5 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c 27 53 27  ,'O','I','N','S'
32bd6 2c 27 45 27 2c 27 52 27 2c 27 54 27 2c 27 4d 27  ,'E','R','T','M'
32bd7 2c 27 41 27 2c 27 54 27 2c 27 43 27 2c 27 48 27  ,'A','T','C','H'
32bd8 2c 27 50 27 2c 27 4c 27 2c 27 41 27 2c 27 4e 27  ,'P','L','A','N'
32bd9 2c 27 41 27 2c 0a 20 20 20 20 27 4c 27 2c 27 59  ,'A',.    'L','Y
32bda 27 2c 27 5a 27 2c 27 45 27 2c 27 50 27 2c 27 52  ','Z','E','P','R
32bdb 27 2c 27 41 27 2c 27 47 27 2c 27 4d 27 2c 27 41  ','A','G','M','A
32bdc 27 2c 27 42 27 2c 27 4f 27 2c 27 52 27 2c 27 54  ','B','O','R','T
32bdd 27 2c 27 56 27 2c 27 41 27 2c 27 4c 27 2c 27 55  ','V','A','L','U
32bde 27 2c 0a 20 20 20 20 27 45 27 2c 27 53 27 2c 27  ',.    'E','S','
32bdf 56 27 2c 27 49 27 2c 27 52 27 2c 27 54 27 2c 27  V','I','R','T','
32be0 55 27 2c 27 41 27 2c 27 4c 27 2c 27 49 27 2c 27  U','A','L','I','
32be1 4d 27 2c 27 49 27 2c 27 54 27 2c 27 57 27 2c 27  M','I','T','W','
32be2 48 27 2c 27 45 27 2c 27 4e 27 2c 27 57 27 2c 0a  H','E','N','W',.
32be3 20 20 20 20 27 48 27 2c 27 45 27 2c 27 52 27 2c      'H','E','R',
32be4 27 45 27 2c 27 4e 27 2c 27 41 27 2c 27 4d 27 2c  'E','N','A','M',
32be5 27 45 27 2c 27 41 27 2c 27 46 27 2c 27 54 27 2c  'E','A','F','T',
32be6 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 50 27 2c  'E','R','E','P',
32be7 27 4c 27 2c 27 41 27 2c 27 43 27 2c 0a 20 20 20  'L','A','C',.   
32be8 20 27 45 27 2c 27 41 27 2c 27 4e 27 2c 27 44 27   'E','A','N','D'
32be9 2c 27 45 27 2c 27 46 27 2c 27 41 27 2c 27 55 27  ,'E','F','A','U'
32bea 2c 27 4c 27 2c 27 54 27 2c 27 41 27 2c 27 55 27  ,'L','T','A','U'
32beb 2c 27 54 27 2c 27 4f 27 2c 27 49 27 2c 27 4e 27  ,'T','O','I','N'
32bec 2c 27 43 27 2c 27 52 27 2c 0a 20 20 20 20 27 45  ,'C','R',.    'E
32bed 27 2c 27 4d 27 2c 27 45 27 2c 27 4e 27 2c 27 54  ','M','E','N','T
32bee 27 2c 27 43 27 2c 27 41 27 2c 27 53 27 2c 27 54  ','C','A','S','T
32bef 27 2c 27 43 27 2c 27 4f 27 2c 27 4c 27 2c 27 55  ','C','O','L','U
32bf0 27 2c 27 4d 27 2c 27 4e 27 2c 27 43 27 2c 27 4f  ','M','N','C','O
32bf1 27 2c 27 4d 27 2c 0a 20 20 20 20 27 4d 27 2c 27  ','M',.    'M','
32bf2 49 27 2c 27 54 27 2c 27 43 27 2c 27 4f 27 2c 27  I','T','C','O','
32bf3 4e 27 2c 27 46 27 2c 27 4c 27 2c 27 49 27 2c 27  N','F','L','I','
32bf4 43 27 2c 27 54 27 2c 27 43 27 2c 27 52 27 2c 27  C','T','C','R','
32bf5 4f 27 2c 27 53 27 2c 27 53 27 2c 27 43 27 2c 27  O','S','S','C','
32bf6 55 27 2c 0a 20 20 20 20 27 52 27 2c 27 52 27 2c  U',.    'R','R',
32bf7 27 45 27 2c 27 4e 27 2c 27 54 27 2c 27 5f 27 2c  'E','N','T','_',
32bf8 27 54 27 2c 27 49 27 2c 27 4d 27 2c 27 45 27 2c  'T','I','M','E',
32bf9 27 53 27 2c 27 54 27 2c 27 41 27 2c 27 4d 27 2c  'S','T','A','M',
32bfa 27 50 27 2c 27 52 27 2c 27 49 27 2c 27 4d 27 2c  'P','R','I','M',
32bfb 0a 20 20 20 20 27 41 27 2c 27 52 27 2c 27 59 27  .    'A','R','Y'
32bfc 2c 27 44 27 2c 27 45 27 2c 27 46 27 2c 27 45 27  ,'D','E','F','E'
32bfd 2c 27 52 27 2c 27 52 27 2c 27 45 27 2c 27 44 27  ,'R','R','E','D'
32bfe 2c 27 49 27 2c 27 53 27 2c 27 54 27 2c 27 49 27  ,'I','S','T','I'
32bff 2c 27 4e 27 2c 27 43 27 2c 27 54 27 2c 0a 20 20  ,'N','C','T',.  
32c00 20 20 27 44 27 2c 27 52 27 2c 27 4f 27 2c 27 50    'D','R','O','P
32c01 27 2c 27 46 27 2c 27 41 27 2c 27 49 27 2c 27 4c  ','F','A','I','L
32c02 27 2c 27 46 27 2c 27 52 27 2c 27 4f 27 2c 27 4d  ','F','R','O','M
32c03 27 2c 27 46 27 2c 27 55 27 2c 27 4c 27 2c 27 4c  ','F','U','L','L
32c04 27 2c 27 47 27 2c 27 4c 27 2c 0a 20 20 20 20 27  ','G','L',.    '
32c05 4f 27 2c 27 42 27 2c 27 59 27 2c 27 49 27 2c 27  O','B','Y','I','
32c06 46 27 2c 27 49 27 2c 27 53 27 2c 27 4e 27 2c 27  F','I','S','N','
32c07 55 27 2c 27 4c 27 2c 27 4c 27 2c 27 4f 27 2c 27  U','L','L','O','
32c08 52 27 2c 27 44 27 2c 27 45 27 2c 27 52 27 2c 27  R','D','E','R','
32c09 45 27 2c 27 53 27 2c 0a 20 20 20 20 27 54 27 2c  E','S',.    'T',
32c0a 27 52 27 2c 27 49 27 2c 27 43 27 2c 27 54 27 2c  'R','I','C','T',
32c0b 27 4f 27 2c 27 55 27 2c 27 54 27 2c 27 45 27 2c  'O','U','T','E',
32c0c 27 52 27 2c 27 49 27 2c 27 47 27 2c 27 48 27 2c  'R','I','G','H',
32c0d 27 54 27 2c 27 52 27 2c 27 4f 27 2c 27 4c 27 2c  'T','R','O','L',
32c0e 27 4c 27 2c 0a 20 20 20 20 27 42 27 2c 27 41 27  'L',.    'B','A'
32c0f 2c 27 43 27 2c 27 4b 27 2c 27 52 27 2c 27 4f 27  ,'C','K','R','O'
32c10 2c 27 57 27 2c 27 55 27 2c 27 4e 27 2c 27 49 27  ,'W','U','N','I'
32c11 2c 27 4f 27 2c 27 4e 27 2c 27 55 27 2c 27 53 27  ,'O','N','U','S'
32c12 2c 27 49 27 2c 27 4e 27 2c 27 47 27 2c 27 56 27  ,'I','N','G','V'
32c13 2c 0a 20 20 20 20 27 41 27 2c 27 43 27 2c 27 55  ,.    'A','C','U
32c14 27 2c 27 55 27 2c 27 4d 27 2c 27 56 27 2c 27 49  ','U','M','V','I
32c15 27 2c 27 45 27 2c 27 57 27 2c 27 49 27 2c 27 4e  ','E','W','I','N
32c16 27 2c 27 49 27 2c 27 54 27 2c 27 49 27 2c 27 41  ','I','T','I','A
32c17 27 2c 27 4c 27 2c 27 4c 27 2c 27 59 27 2c 0a 20  ','L','L','Y',. 
32c18 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   };.  static con
32c19 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
32c1a 20 61 48 61 73 68 5b 31 32 37 5d 20 3d 20 7b 0a   aHash[127] = {.
32c1b 20 20 20 20 20 20 37 32 2c 20 31 30 31 2c 20 31        72, 101, 1
32c1c 31 34 2c 20 20 37 30 2c 20 20 20 30 2c 20 20 34  14,  70,   0,  4
32c1d 35 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 37 38  5,   0,   0,  78
32c1e 2c 20 20 20 30 2c 20 20 37 33 2c 20 20 20 30 2c  ,   0,  73,   0,
32c1f 20 20 20 30 2c 0a 20 20 20 20 20 20 34 32 2c 20     0,.      42, 
32c20 20 31 32 2c 20 20 37 34 2c 20 20 31 35 2c 20 20   12,  74,  15,  
32c21 20 30 2c 20 31 31 33 2c 20 20 38 31 2c 20 20 35   0, 113,  81,  5
32c22 30 2c 20 31 30 38 2c 20 20 20 30 2c 20 20 31 39  0, 108,   0,  19
32c23 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20  ,   0,   0,.    
32c24 20 31 31 38 2c 20 20 20 30 2c 20 31 31 36 2c 20   118,   0, 116, 
32c25 31 31 31 2c 20 20 20 30 2c 20 20 32 32 2c 20 20  111,   0,  22,  
32c26 38 39 2c 20 20 20 30 2c 20 20 20 39 2c 20 20 20  89,   0,   9,   
32c27 30 2c 20 20 20 30 2c 20 20 36 36 2c 20 20 36 37  0,   0,  66,  67
32c28 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 36 35 2c  ,.       0,  65,
32c29 20 20 20 36 2c 20 20 20 30 2c 20 20 34 38 2c 20     6,   0,  48, 
32c2a 20 38 36 2c 20 20 39 38 2c 20 20 20 30 2c 20 31   86,  98,   0, 1
32c2b 31 35 2c 20 20 39 37 2c 20 20 20 30 2c 20 20 20  15,  97,   0,   
32c2c 30 2c 20 20 34 34 2c 0a 20 20 20 20 20 20 20 30  0,  44,.       0
32c2d 2c 20 20 39 39 2c 20 20 32 34 2c 20 20 20 30 2c  ,  99,  24,   0,
32c2e 20 20 31 37 2c 20 20 20 30 2c 20 31 31 39 2c 20    17,   0, 119, 
32c2f 20 34 39 2c 20 20 32 33 2c 20 20 20 30 2c 20 20   49,  23,   0,  
32c30 20 35 2c 20 31 30 36 2c 20 20 32 35 2c 0a 20 20   5, 106,  25,.  
32c31 20 20 20 20 39 32 2c 20 20 20 30 2c 20 20 20 30      92,   0,   0
32c32 2c 20 31 32 31 2c 20 31 30 32 2c 20 20 35 36 2c  , 121, 102,  56,
32c33 20 31 32 30 2c 20 20 35 33 2c 20 20 32 38 2c 20   120,  53,  28, 
32c34 20 35 31 2c 20 20 20 30 2c 20 20 38 37 2c 20 20   51,   0,  87,  
32c35 20 30 2c 0a 20 20 20 20 20 20 39 36 2c 20 20 32   0,.      96,  2
32c36 36 2c 20 20 20 30 2c 20 20 39 35 2c 20 20 20 30  6,   0,  95,   0
32c37 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 39 31 2c  ,   0,   0,  91,
32c38 20 20 38 38 2c 20 20 39 33 2c 20 20 38 34 2c 20    88,  93,  84, 
32c39 31 30 35 2c 20 20 31 34 2c 0a 20 20 20 20 20 20  105,  14,.      
32c3a 33 39 2c 20 31 30 34 2c 20 20 20 30 2c 20 20 37  39, 104,   0,  7
32c3b 37 2c 20 20 20 30 2c 20 20 31 38 2c 20 20 38 35  7,   0,  18,  85
32c3c 2c 20 31 30 37 2c 20 20 33 32 2c 20 20 20 30 2c  , 107,  32,   0,
32c3d 20 31 31 37 2c 20 20 37 36 2c 20 31 30 39 2c 0a   117,  76, 109,.
32c3e 20 20 20 20 20 20 35 38 2c 20 20 34 36 2c 20 20        58,  46,  
32c3f 38 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 39  80,   0,   0,  9
32c40 30 2c 20 20 34 30 2c 20 20 20 30 2c 20 31 31 32  0,  40,   0, 112
32c41 2c 20 20 20 30 2c 20 20 33 36 2c 20 20 20 30 2c  ,   0,  36,   0,
32c42 20 20 20 30 2c 0a 20 20 20 20 20 20 32 39 2c 20     0,.      29, 
32c43 20 20 30 2c 20 20 38 32 2c 20 20 35 39 2c 20 20    0,  82,  59,  
32c44 36 30 2c 20 20 20 30 2c 20 20 32 30 2c 20 20 35  60,   0,  20,  5
32c45 37 2c 20 20 20 30 2c 20 20 35 32 2c 0a 20 20 7d  7,   0,  52,.  }
32c46 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
32c47 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61   unsigned char a
32c48 4e 65 78 74 5b 31 32 31 5d 20 3d 20 7b 0a 20 20  Next[121] = {.  
32c49 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30       0,   0,   0
32c4a 2c 20 20 20 30 2c 20 20 20 34 2c 20 20 20 30 2c  ,   0,   4,   0,
32c4b 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32c4c 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32c4d 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20   0,.       0,   
32c4e 32 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  2,   0,   0,   0
32c4f 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
32c50 20 20 31 33 2c 20 20 20 30 2c 20 20 20 30 2c 20    13,   0,   0, 
32c51 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20    0,   0,.      
32c52 20 30 2c 20 20 20 37 2c 20 20 20 30 2c 20 20 20   0,   7,   0,   
32c53 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
32c54 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
32c55 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a     0,   0,   0,.
32c56 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20         0,   0,  
32c57 20 30 2c 20 20 20 30 2c 20 20 33 33 2c 20 20 20   0,   0,  33,   
32c58 30 2c 20 20 32 31 2c 20 20 20 30 2c 20 20 20 30  0,  21,   0,   0
32c59 2c 20 20 20 30 2c 20 20 34 33 2c 20 20 20 33 2c  ,   0,  43,   3,
32c5a 20 20 34 37 2c 0a 20 20 20 20 20 20 20 30 2c 20    47,.       0, 
32c5b 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32c5c 33 30 2c 20 20 20 30 2c 20 20 35 34 2c 20 20 20  30,   0,  54,   
32c5d 30 2c 20 20 33 38 2c 20 20 20 30 2c 20 20 20 30  0,  38,   0,   0
32c5e 2c 20 20 20 30 2c 20 20 20 31 2c 0a 20 20 20 20  ,   0,   1,.    
32c5f 20 20 36 32 2c 20 20 20 30 2c 20 20 20 30 2c 20    62,   0,   0, 
32c60 20 36 33 2c 20 20 20 30 2c 20 20 34 31 2c 20 20   63,   0,  41,  
32c61 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
32c62 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
32c63 2c 0a 20 20 20 20 20 20 36 31 2c 20 20 20 30 2c  ,.      61,   0,
32c64 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32c65 20 33 31 2c 20 20 35 35 2c 20 20 31 36 2c 20 20   31,  55,  16,  
32c66 33 34 2c 20 20 31 30 2c 20 20 20 30 2c 20 20 20  34,  10,   0,   
32c67 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30  0,   0,.       0
32c68 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
32c69 20 20 31 31 2c 20 20 36 38 2c 20 20 37 35 2c 20    11,  68,  75, 
32c6a 20 20 30 2c 20 20 20 38 2c 20 20 20 30 2c 20 31    0,   8,   0, 1
32c6b 30 30 2c 20 20 39 34 2c 20 20 20 30 2c 0a 20 20  00,  94,   0,.  
32c6c 20 20 20 31 30 33 2c 20 20 20 30 2c 20 20 38 33     103,   0,  83
32c6d 2c 20 20 20 30 2c 20 20 37 31 2c 20 20 20 30 2c  ,   0,  71,   0,
32c6e 20 20 20 30 2c 20 31 31 30 2c 20 20 32 37 2c 20     0, 110,  27, 
32c6f 20 33 37 2c 20 20 36 39 2c 20 20 37 39 2c 20 20   37,  69,  79,  
32c70 20 30 2c 0a 20 20 20 20 20 20 33 35 2c 20 20 36   0,.      35,  6
32c71 34 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 7d  4,   0,   0,.  }
32c72 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
32c73 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61   unsigned char a
32c74 4c 65 6e 5b 31 32 31 5d 20 3d 20 7b 0a 20 20 20  Len[121] = {.   
32c75 20 20 20 20 37 2c 20 20 20 37 2c 20 20 20 35 2c      7,   7,   5,
32c76 20 20 20 34 2c 20 20 20 36 2c 20 20 20 34 2c 20     4,   6,   4, 
32c77 20 20 35 2c 20 20 20 33 2c 20 20 20 36 2c 20 20    5,   3,   6,  
32c78 20 37 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20   7,   3,   6,   
32c79 36 2c 0a 20 20 20 20 20 20 20 37 2c 20 20 20 37  6,.       7,   7
32c7a 2c 20 20 20 33 2c 20 20 20 38 2c 20 20 20 32 2c  ,   3,   8,   2,
32c7b 20 20 20 36 2c 20 20 20 35 2c 20 20 20 34 2c 20     6,   5,   4, 
32c7c 20 20 34 2c 20 20 20 33 2c 20 20 31 30 2c 20 20    4,   3,  10,  
32c7d 20 34 2c 20 20 20 36 2c 0a 20 20 20 20 20 20 31   4,   6,.      1
32c7e 31 2c 20 20 20 36 2c 20 20 20 32 2c 20 20 20 37  1,   6,   2,   7
32c7f 2c 20 20 20 35 2c 20 20 20 35 2c 20 20 20 39 2c  ,   5,   5,   9,
32c80 20 20 20 36 2c 20 20 20 39 2c 20 20 20 39 2c 20     6,   9,   9, 
32c81 20 20 37 2c 20 20 31 30 2c 20 20 31 30 2c 0a 20    7,  10,  10,. 
32c82 20 20 20 20 20 20 34 2c 20 20 20 36 2c 20 20 20        4,   6,   
32c83 32 2c 20 20 20 33 2c 20 20 20 39 2c 20 20 20 34  2,   3,   9,   4
32c84 2c 20 20 20 32 2c 20 20 20 36 2c 20 20 20 35 2c  ,   2,   6,   5,
32c85 20 20 20 36 2c 20 20 20 36 2c 20 20 20 35 2c 20     6,   6,   5, 
32c86 20 20 36 2c 0a 20 20 20 20 20 20 20 35 2c 20 20    6,.       5,  
32c87 20 35 2c 20 20 20 37 2c 20 20 20 37 2c 20 20 20   5,   7,   7,   
32c88 37 2c 20 20 20 33 2c 20 20 20 32 2c 20 20 20 34  7,   3,   2,   4
32c89 2c 20 20 20 34 2c 20 20 20 37 2c 20 20 20 33 2c  ,   4,   7,   3,
32c8a 20 20 20 36 2c 20 20 20 34 2c 0a 20 20 20 20 20     6,   4,.     
32c8b 20 20 37 2c 20 20 20 36 2c 20 20 31 32 2c 20 20    7,   6,  12,  
32c8c 20 36 2c 20 20 20 39 2c 20 20 20 34 2c 20 20 20   6,   9,   4,   
32c8d 36 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20 37  6,   5,   4,   7
32c8e 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c  ,   6,   5,   6,
32c8f 0a 20 20 20 20 20 20 20 37 2c 20 20 20 35 2c 20  .       7,   5, 
32c90 20 20 34 2c 20 20 20 35 2c 20 20 20 36 2c 20 20    4,   5,   6,  
32c91 20 35 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20   5,   7,   3,   
32c92 37 2c 20 20 31 33 2c 20 20 20 32 2c 20 20 20 32  7,  13,   2,   2
32c93 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20 36 2c  ,   4,.       6,
32c94 20 20 20 36 2c 20 20 20 38 2c 20 20 20 35 2c 20     6,   8,   5, 
32c95 20 31 37 2c 20 20 31 32 2c 20 20 20 37 2c 20 20   17,  12,   7,  
32c96 20 38 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20   8,   8,   2,   
32c97 34 2c 20 20 20 34 2c 20 20 20 34 2c 0a 20 20 20  4,   4,   4,.   
32c98 20 20 20 20 34 2c 20 20 20 34 2c 20 20 20 32 2c      4,   4,   2,
32c99 20 20 20 32 2c 20 20 20 36 2c 20 20 20 35 2c 20     2,   6,   5, 
32c9a 20 20 38 2c 20 20 20 35 2c 20 20 20 35 2c 20 20    8,   5,   5,  
32c9b 20 38 2c 20 20 20 33 2c 20 20 20 35 2c 20 20 20   8,   3,   5,   
32c9c 35 2c 0a 20 20 20 20 20 20 20 36 2c 20 20 20 34  5,.       6,   4
32c9d 2c 20 20 20 39 2c 20 20 20 33 2c 0a 20 20 7d 3b  ,   9,   3,.  };
32c9e 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
32c9f 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69  unsigned short i
32ca0 6e 74 20 61 4f 66 66 73 65 74 5b 31 32 31 5d 20  nt aOffset[121] 
32ca1 3d 20 7b 0a 20 20 20 20 20 20 20 30 2c 20 20 20  = {.       0,   
32ca2 32 2c 20 20 20 32 2c 20 20 20 38 2c 20 20 20 39  2,   2,   8,   9
32ca3 2c 20 20 31 34 2c 20 20 31 36 2c 20 20 32 30 2c  ,  14,  16,  20,
32ca4 20 20 32 33 2c 20 20 32 35 2c 20 20 32 35 2c 20    23,  25,  25, 
32ca5 20 32 39 2c 20 20 33 33 2c 0a 20 20 20 20 20 20   29,  33,.      
32ca6 33 36 2c 20 20 34 31 2c 20 20 34 36 2c 20 20 34  36,  41,  46,  4
32ca7 38 2c 20 20 35 33 2c 20 20 35 34 2c 20 20 35 39  8,  53,  54,  59
32ca8 2c 20 20 36 32 2c 20 20 36 35 2c 20 20 36 37 2c  ,  62,  65,  67,
32ca9 20 20 36 39 2c 20 20 37 38 2c 20 20 38 31 2c 0a    69,  78,  81,.
32caa 20 20 20 20 20 20 38 36 2c 20 20 39 31 2c 20 20        86,  91,  
32cab 39 35 2c 20 20 39 36 2c 20 31 30 31 2c 20 31 30  95,  96, 101, 10
32cac 35 2c 20 31 30 39 2c 20 31 31 37 2c 20 31 32 32  5, 109, 117, 122
32cad 2c 20 31 32 38 2c 20 31 33 36 2c 20 31 34 32 2c  , 128, 136, 142,
32cae 20 31 35 32 2c 0a 20 20 20 20 20 31 35 39 2c 20   152,.     159, 
32caf 31 36 32 2c 20 31 36 32 2c 20 31 36 35 2c 20 31  162, 162, 165, 1
32cb0 36 37 2c 20 31 36 37 2c 20 31 37 31 2c 20 31 37  67, 167, 171, 17
32cb1 36 2c 20 31 37 39 2c 20 31 38 34 2c 20 31 38 39  6, 179, 184, 189
32cb2 2c 20 31 39 34 2c 20 31 39 37 2c 0a 20 20 20 20  , 194, 197,.    
32cb3 20 32 30 33 2c 20 32 30 36 2c 20 32 31 30 2c 20   203, 206, 210, 
32cb4 32 31 37 2c 20 32 32 33 2c 20 32 32 33 2c 20 32  217, 223, 223, 2
32cb5 32 33 2c 20 32 32 36 2c 20 32 32 39 2c 20 32 33  23, 226, 229, 23
32cb6 33 2c 20 32 33 34 2c 20 32 33 38 2c 20 32 34 34  3, 234, 238, 244
32cb7 2c 0a 20 20 20 20 20 32 34 38 2c 20 32 35 35 2c  ,.     248, 255,
32cb8 20 32 36 31 2c 20 32 37 33 2c 20 32 37 39 2c 20   261, 273, 279, 
32cb9 32 38 38 2c 20 32 39 30 2c 20 32 39 36 2c 20 33  288, 290, 296, 3
32cba 30 31 2c 20 33 30 33 2c 20 33 31 30 2c 20 33 31  01, 303, 310, 31
32cbb 35 2c 20 33 32 30 2c 0a 20 20 20 20 20 33 32 36  5, 320,.     326
32cbc 2c 20 33 33 32 2c 20 33 33 37 2c 20 33 34 31 2c  , 332, 337, 341,
32cbd 20 33 34 34 2c 20 33 35 30 2c 20 33 35 34 2c 20   344, 350, 354, 
32cbe 33 36 31 2c 20 33 36 33 2c 20 33 37 30 2c 20 33  361, 363, 370, 3
32cbf 37 32 2c 20 33 37 34 2c 20 33 38 33 2c 0a 20 20  72, 374, 383,.  
32cc0 20 20 20 33 38 37 2c 20 33 39 33 2c 20 33 39 39     387, 393, 399
32cc1 2c 20 34 30 37 2c 20 34 31 32 2c 20 34 31 32 2c  , 407, 412, 412,
32cc2 20 34 32 38 2c 20 34 33 35 2c 20 34 34 32 2c 20   428, 435, 442, 
32cc3 34 34 33 2c 20 34 35 30 2c 20 34 35 34 2c 20 34  443, 450, 454, 4
32cc4 35 38 2c 0a 20 20 20 20 20 34 36 32 2c 20 34 36  58,.     462, 46
32cc5 36 2c 20 34 36 39 2c 20 34 37 31 2c 20 34 37 33  6, 469, 471, 473
32cc6 2c 20 34 37 39 2c 20 34 38 33 2c 20 34 39 31 2c  , 479, 483, 491,
32cc7 20 34 39 35 2c 20 35 30 30 2c 20 35 30 38 2c 20   495, 500, 508, 
32cc8 35 31 31 2c 20 35 31 36 2c 0a 20 20 20 20 20 35  511, 516,.     5
32cc9 32 31 2c 20 35 32 37 2c 20 35 33 31 2c 20 35 33  21, 527, 531, 53
32cca 36 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63  6,.  };.  static
32ccb 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
32ccc 63 68 61 72 20 61 43 6f 64 65 5b 31 32 31 5d 20  char aCode[121] 
32ccd 3d 20 7b 0a 20 20 20 20 54 4b 5f 52 45 49 4e 44  = {.    TK_REIND
32cce 45 58 2c 20 20 20 20 54 4b 5f 49 4e 44 45 58 45  EX,    TK_INDEXE
32ccf 44 2c 20 20 20 20 54 4b 5f 49 4e 44 45 58 2c 20  D,    TK_INDEX, 
32cd0 20 20 20 20 20 54 4b 5f 44 45 53 43 2c 20 20 20       TK_DESC,   
32cd1 20 20 20 20 54 4b 5f 45 53 43 41 50 45 2c 20 20      TK_ESCAPE,  
32cd2 20 20 20 0a 20 20 20 20 54 4b 5f 45 41 43 48 2c     .    TK_EACH,
32cd3 20 20 20 20 20 20 20 54 4b 5f 43 48 45 43 4b 2c         TK_CHECK,
32cd4 20 20 20 20 20 20 54 4b 5f 4b 45 59 2c 20 20 20        TK_KEY,   
32cd5 20 20 20 20 20 54 4b 5f 42 45 46 4f 52 45 2c 20       TK_BEFORE, 
32cd6 20 20 20 20 54 4b 5f 46 4f 52 45 49 47 4e 2c 20      TK_FOREIGN, 
32cd7 20 20 20 0a 20 20 20 20 54 4b 5f 46 4f 52 2c 20     .    TK_FOR, 
32cd8 20 20 20 20 20 20 20 54 4b 5f 49 47 4e 4f 52 45         TK_IGNORE
32cd9 2c 20 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57  ,     TK_LIKE_KW
32cda 2c 20 20 20 20 54 4b 5f 45 58 50 4c 41 49 4e 2c  ,    TK_EXPLAIN,
32cdb 20 20 20 20 54 4b 5f 49 4e 53 54 45 41 44 2c 20      TK_INSTEAD, 
32cdc 20 20 20 0a 20 20 20 20 54 4b 5f 41 44 44 2c 20     .    TK_ADD, 
32cdd 20 20 20 20 20 20 20 54 4b 5f 44 41 54 41 42 41         TK_DATABA
32cde 53 45 2c 20 20 20 54 4b 5f 41 53 2c 20 20 20 20  SE,   TK_AS,    
32cdf 20 20 20 20 20 54 4b 5f 53 45 4c 45 43 54 2c 20       TK_SELECT, 
32ce0 20 20 20 20 54 4b 5f 54 41 42 4c 45 2c 20 20 20      TK_TABLE,   
32ce1 20 20 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f     .    TK_JOIN_
32ce2 4b 57 2c 20 20 20 20 54 4b 5f 54 48 45 4e 2c 20  KW,    TK_THEN, 
32ce3 20 20 20 20 20 20 54 4b 5f 45 4e 44 2c 20 20 20        TK_END,   
32ce4 20 20 20 20 20 54 4b 5f 44 45 46 45 52 52 41 42       TK_DEFERRAB
32ce5 4c 45 2c 20 54 4b 5f 45 4c 53 45 2c 20 20 20 20  LE, TK_ELSE,    
32ce6 20 20 20 0a 20 20 20 20 54 4b 5f 45 58 43 45 50     .    TK_EXCEP
32ce7 54 2c 20 20 20 20 20 54 4b 5f 54 52 41 4e 53 41  T,     TK_TRANSA
32ce8 43 54 49 4f 4e 2c 54 4b 5f 41 43 54 49 4f 4e 2c  CTION,TK_ACTION,
32ce9 20 20 20 20 20 54 4b 5f 4f 4e 2c 20 20 20 20 20       TK_ON,     
32cea 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20      TK_JOIN_KW, 
32ceb 20 20 20 0a 20 20 20 20 54 4b 5f 41 4c 54 45 52     .    TK_ALTER
32cec 2c 20 20 20 20 20 20 54 4b 5f 52 41 49 53 45 2c  ,      TK_RAISE,
32ced 20 20 20 20 20 20 54 4b 5f 45 58 43 4c 55 53 49        TK_EXCLUSI
32cee 56 45 2c 20 20 54 4b 5f 45 58 49 53 54 53 2c 20  VE,  TK_EXISTS, 
32cef 20 20 20 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54      TK_SAVEPOINT
32cf0 2c 20 20 0a 20 20 20 20 54 4b 5f 49 4e 54 45 52  ,  .    TK_INTER
32cf1 53 45 43 54 2c 20 20 54 4b 5f 54 52 49 47 47 45  SECT,  TK_TRIGGE
32cf2 52 2c 20 20 20 20 54 4b 5f 52 45 46 45 52 45 4e  R,    TK_REFEREN
32cf3 43 45 53 2c 20 54 4b 5f 43 4f 4e 53 54 52 41 49  CES, TK_CONSTRAI
32cf4 4e 54 2c 20 54 4b 5f 49 4e 54 4f 2c 20 20 20 20  NT, TK_INTO,    
32cf5 20 20 20 0a 20 20 20 20 54 4b 5f 4f 46 46 53 45     .    TK_OFFSE
32cf6 54 2c 20 20 20 20 20 54 4b 5f 4f 46 2c 20 20 20  T,     TK_OF,   
32cf7 20 20 20 20 20 20 54 4b 5f 53 45 54 2c 20 20 20        TK_SET,   
32cf8 20 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20       TK_TEMP,   
32cf9 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20      TK_TEMP,    
32cfa 20 20 20 0a 20 20 20 20 54 4b 5f 4f 52 2c 20 20     .    TK_OR,  
32cfb 20 20 20 20 20 20 20 54 4b 5f 55 4e 49 51 55 45         TK_UNIQUE
32cfc 2c 20 20 20 20 20 54 4b 5f 51 55 45 52 59 2c 20  ,     TK_QUERY, 
32cfd 20 20 20 20 20 54 4b 5f 41 54 54 41 43 48 2c 20       TK_ATTACH, 
32cfe 20 20 20 20 54 4b 5f 48 41 56 49 4e 47 2c 20 20      TK_HAVING,  
32cff 20 20 20 0a 20 20 20 20 54 4b 5f 47 52 4f 55 50     .    TK_GROUP
32d00 2c 20 20 20 20 20 20 54 4b 5f 55 50 44 41 54 45  ,      TK_UPDATE
32d01 2c 20 20 20 20 20 54 4b 5f 42 45 47 49 4e 2c 20  ,     TK_BEGIN, 
32d02 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c       TK_JOIN_KW,
32d03 20 20 20 20 54 4b 5f 52 45 4c 45 41 53 45 2c 20      TK_RELEASE, 
32d04 20 20 20 0a 20 20 20 20 54 4b 5f 42 45 54 57 45     .    TK_BETWE
32d05 45 4e 2c 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c  EN,    TK_NOTNUL
32d06 4c 2c 20 20 20 20 54 4b 5f 4e 4f 54 2c 20 20 20  L,    TK_NOT,   
32d07 20 20 20 20 20 54 4b 5f 4e 4f 2c 20 20 20 20 20       TK_NO,     
32d08 20 20 20 20 54 4b 5f 4e 55 4c 4c 2c 20 20 20 20      TK_NULL,    
32d09 20 20 20 0a 20 20 20 20 54 4b 5f 4c 49 4b 45 5f     .    TK_LIKE_
32d0a 4b 57 2c 20 20 20 20 54 4b 5f 43 41 53 43 41 44  KW,    TK_CASCAD
32d0b 45 2c 20 20 20 20 54 4b 5f 41 53 43 2c 20 20 20  E,    TK_ASC,   
32d0c 20 20 20 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20       TK_DELETE, 
32d0d 20 20 20 20 54 4b 5f 43 41 53 45 2c 20 20 20 20      TK_CASE,    
32d0e 20 20 20 0a 20 20 20 20 54 4b 5f 43 4f 4c 4c 41     .    TK_COLLA
32d0f 54 45 2c 20 20 20 20 54 4b 5f 43 52 45 41 54 45  TE,    TK_CREATE
32d10 2c 20 20 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b  ,     TK_CTIME_K
32d11 57 2c 20 20 20 54 4b 5f 44 45 54 41 43 48 2c 20  W,   TK_DETACH, 
32d12 20 20 20 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45      TK_IMMEDIATE
32d13 2c 20 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e 2c  ,  .    TK_JOIN,
32d14 20 20 20 20 20 20 20 54 4b 5f 49 4e 53 45 52 54         TK_INSERT
32d15 2c 20 20 20 20 20 54 4b 5f 4d 41 54 43 48 2c 20  ,     TK_MATCH, 
32d16 20 20 20 20 20 54 4b 5f 50 4c 41 4e 2c 20 20 20       TK_PLAN,   
32d17 20 20 20 20 54 4b 5f 41 4e 41 4c 59 5a 45 2c 20      TK_ANALYZE, 
32d18 20 20 20 0a 20 20 20 20 54 4b 5f 50 52 41 47 4d     .    TK_PRAGM
32d19 41 2c 20 20 20 20 20 54 4b 5f 41 42 4f 52 54 2c  A,     TK_ABORT,
32d1a 20 20 20 20 20 20 54 4b 5f 56 41 4c 55 45 53 2c        TK_VALUES,
32d1b 20 20 20 20 20 54 4b 5f 56 49 52 54 55 41 4c 2c       TK_VIRTUAL,
32d1c 20 20 20 20 54 4b 5f 4c 49 4d 49 54 2c 20 20 20      TK_LIMIT,   
32d1d 20 20 20 0a 20 20 20 20 54 4b 5f 57 48 45 4e 2c     .    TK_WHEN,
32d1e 20 20 20 20 20 20 20 54 4b 5f 57 48 45 52 45 2c         TK_WHERE,
32d1f 20 20 20 20 20 20 54 4b 5f 52 45 4e 41 4d 45 2c        TK_RENAME,
32d20 20 20 20 20 20 54 4b 5f 41 46 54 45 52 2c 20 20       TK_AFTER,  
32d21 20 20 20 20 54 4b 5f 52 45 50 4c 41 43 45 2c 20      TK_REPLACE, 
32d22 20 20 20 0a 20 20 20 20 54 4b 5f 41 4e 44 2c 20     .    TK_AND, 
32d23 20 20 20 20 20 20 20 54 4b 5f 44 45 46 41 55 4c         TK_DEFAUL
32d24 54 2c 20 20 20 20 54 4b 5f 41 55 54 4f 49 4e 43  T,    TK_AUTOINC
32d25 52 2c 20 20 20 54 4b 5f 54 4f 2c 20 20 20 20 20  R,   TK_TO,     
32d26 20 20 20 20 54 4b 5f 49 4e 2c 20 20 20 20 20 20      TK_IN,      
32d27 20 20 20 0a 20 20 20 20 54 4b 5f 43 41 53 54 2c     .    TK_CAST,
32d28 20 20 20 20 20 20 20 54 4b 5f 43 4f 4c 55 4d 4e         TK_COLUMN
32d29 4b 57 2c 20 20 20 54 4b 5f 43 4f 4d 4d 49 54 2c  KW,   TK_COMMIT,
32d2a 20 20 20 20 20 54 4b 5f 43 4f 4e 46 4c 49 43 54       TK_CONFLICT
32d2b 2c 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20  ,   TK_JOIN_KW, 
32d2c 20 20 20 0a 20 20 20 20 54 4b 5f 43 54 49 4d 45     .    TK_CTIME
32d2d 5f 4b 57 2c 20 20 20 54 4b 5f 43 54 49 4d 45 5f  _KW,   TK_CTIME_
32d2e 4b 57 2c 20 20 20 54 4b 5f 50 52 49 4d 41 52 59  KW,   TK_PRIMARY
32d2f 2c 20 20 20 20 54 4b 5f 44 45 46 45 52 52 45 44  ,    TK_DEFERRED
32d30 2c 20 20 20 54 4b 5f 44 49 53 54 49 4e 43 54 2c  ,   TK_DISTINCT,
32d31 20 20 20 0a 20 20 20 20 54 4b 5f 49 53 2c 20 20     .    TK_IS,  
32d32 20 20 20 20 20 20 20 54 4b 5f 44 52 4f 50 2c 20         TK_DROP, 
32d33 20 20 20 20 20 20 54 4b 5f 46 41 49 4c 2c 20 20        TK_FAIL,  
32d34 20 20 20 20 20 54 4b 5f 46 52 4f 4d 2c 20 20 20       TK_FROM,   
32d35 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20      TK_JOIN_KW, 
32d36 20 20 20 0a 20 20 20 20 54 4b 5f 4c 49 4b 45 5f     .    TK_LIKE_
32d37 4b 57 2c 20 20 20 20 54 4b 5f 42 59 2c 20 20 20  KW,    TK_BY,   
32d38 20 20 20 20 20 20 54 4b 5f 49 46 2c 20 20 20 20        TK_IF,    
32d39 20 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20       TK_ISNULL, 
32d3a 20 20 20 20 54 4b 5f 4f 52 44 45 52 2c 20 20 20      TK_ORDER,   
32d3b 20 20 20 0a 20 20 20 20 54 4b 5f 52 45 53 54 52     .    TK_RESTR
32d3c 49 43 54 2c 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b  ICT,   TK_JOIN_K
32d3d 57 2c 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57  W,    TK_JOIN_KW
32d3e 2c 20 20 20 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b  ,    TK_ROLLBACK
32d3f 2c 20 20 20 54 4b 5f 52 4f 57 2c 20 20 20 20 20  ,   TK_ROW,     
32d40 20 20 20 0a 20 20 20 20 54 4b 5f 55 4e 49 4f 4e     .    TK_UNION
32d41 2c 20 20 20 20 20 20 54 4b 5f 55 53 49 4e 47 2c  ,      TK_USING,
32d42 20 20 20 20 20 20 54 4b 5f 56 41 43 55 55 4d 2c        TK_VACUUM,
32d43 20 20 20 20 20 54 4b 5f 56 49 45 57 2c 20 20 20       TK_VIEW,   
32d44 20 20 20 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59      TK_INITIALLY
32d45 2c 20 20 0a 20 20 20 20 54 4b 5f 41 4c 4c 2c 20  ,  .    TK_ALL, 
32d46 20 20 20 20 20 20 20 0a 20 20 7d 3b 0a 20 20 69         .  };.  i
32d47 6e 74 20 68 2c 20 69 3b 0a 20 20 69 66 28 20 6e  nt h, i;.  if( n
32d48 3c 32 20 29 20 72 65 74 75 72 6e 20 54 4b 5f 49  <2 ) return TK_I
32d49 44 3b 0a 20 20 68 20 3d 20 28 28 63 68 61 72 4d  D;.  h = ((charM
32d4a 61 70 28 7a 5b 30 5d 29 2a 34 29 20 5e 0a 20 20  ap(z[0])*4) ^.  
32d4b 20 20 20 20 28 63 68 61 72 4d 61 70 28 7a 5b 6e      (charMap(z[n
32d4c 2d 31 5d 29 2a 33 29 20 5e 0a 20 20 20 20 20 20  -1])*3) ^.      
32d4d 6e 29 20 25 20 31 32 37 3b 0a 20 20 66 6f 72 28  n) % 127;.  for(
32d4e 69 3d 28 28 69 6e 74 29 61 48 61 73 68 5b 68 5d  i=((int)aHash[h]
32d4f 29 2d 31 3b 20 69 3e 3d 30 3b 20 69 3d 28 28 69  )-1; i>=0; i=((i
32d50 6e 74 29 61 4e 65 78 74 5b 69 5d 29 2d 31 29 7b  nt)aNext[i])-1){
32d51 0a 20 20 20 20 69 66 28 20 61 4c 65 6e 5b 69 5d  .    if( aLen[i]
32d52 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33 53 74  ==n && sqlite3St
32d53 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b 61 4f  rNICmp(&zText[aO
32d54 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d  ffset[i]],z,n)==
32d55 30 20 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63  0 ){.      testc
32d56 61 73 65 28 20 69 3d 3d 30 20 29 3b 20 2f 2a 20  ase( i==0 ); /* 
32d57 52 45 49 4e 44 45 58 20 2a 2f 0a 20 20 20 20 20  REINDEX */.     
32d58 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20   testcase( i==1 
32d59 29 3b 20 2f 2a 20 49 4e 44 45 58 45 44 20 2a 2f  ); /* INDEXED */
32d5a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32d5b 20 69 3d 3d 32 20 29 3b 20 2f 2a 20 49 4e 44 45   i==2 ); /* INDE
32d5c 58 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  X */.      testc
32d5d 61 73 65 28 20 69 3d 3d 33 20 29 3b 20 2f 2a 20  ase( i==3 ); /* 
32d5e 44 45 53 43 20 2a 2f 0a 20 20 20 20 20 20 74 65  DESC */.      te
32d5f 73 74 63 61 73 65 28 20 69 3d 3d 34 20 29 3b 20  stcase( i==4 ); 
32d60 2f 2a 20 45 53 43 41 50 45 20 2a 2f 0a 20 20 20  /* ESCAPE */.   
32d61 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32d62 35 20 29 3b 20 2f 2a 20 45 41 43 48 20 2a 2f 0a  5 ); /* EACH */.
32d63 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32d64 69 3d 3d 36 20 29 3b 20 2f 2a 20 43 48 45 43 4b  i==6 ); /* CHECK
32d65 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32d66 73 65 28 20 69 3d 3d 37 20 29 3b 20 2f 2a 20 4b  se( i==7 ); /* K
32d67 45 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  EY */.      test
32d68 63 61 73 65 28 20 69 3d 3d 38 20 29 3b 20 2f 2a  case( i==8 ); /*
32d69 20 42 45 46 4f 52 45 20 2a 2f 0a 20 20 20 20 20   BEFORE */.     
32d6a 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 20   testcase( i==9 
32d6b 29 3b 20 2f 2a 20 46 4f 52 45 49 47 4e 20 2a 2f  ); /* FOREIGN */
32d6c 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32d6d 20 69 3d 3d 31 30 20 29 3b 20 2f 2a 20 46 4f 52   i==10 ); /* FOR
32d6e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32d6f 73 65 28 20 69 3d 3d 31 31 20 29 3b 20 2f 2a 20  se( i==11 ); /* 
32d70 49 47 4e 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20  IGNORE */.      
32d71 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 32 20  testcase( i==12 
32d72 29 3b 20 2f 2a 20 52 45 47 45 58 50 20 2a 2f 0a  ); /* REGEXP */.
32d73 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32d74 69 3d 3d 31 33 20 29 3b 20 2f 2a 20 45 58 50 4c  i==13 ); /* EXPL
32d75 41 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  AIN */.      tes
32d76 74 63 61 73 65 28 20 69 3d 3d 31 34 20 29 3b 20  tcase( i==14 ); 
32d77 2f 2a 20 49 4e 53 54 45 41 44 20 2a 2f 0a 20 20  /* INSTEAD */.  
32d78 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32d79 3d 31 35 20 29 3b 20 2f 2a 20 41 44 44 20 2a 2f  =15 ); /* ADD */
32d7a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32d7b 20 69 3d 3d 31 36 20 29 3b 20 2f 2a 20 44 41 54   i==16 ); /* DAT
32d7c 41 42 41 53 45 20 2a 2f 0a 20 20 20 20 20 20 74  ABASE */.      t
32d7d 65 73 74 63 61 73 65 28 20 69 3d 3d 31 37 20 29  estcase( i==17 )
32d7e 3b 20 2f 2a 20 41 53 20 2a 2f 0a 20 20 20 20 20  ; /* AS */.     
32d7f 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 38   testcase( i==18
32d80 20 29 3b 20 2f 2a 20 53 45 4c 45 43 54 20 2a 2f   ); /* SELECT */
32d81 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32d82 20 69 3d 3d 31 39 20 29 3b 20 2f 2a 20 54 41 42   i==19 ); /* TAB
32d83 4c 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  LE */.      test
32d84 63 61 73 65 28 20 69 3d 3d 32 30 20 29 3b 20 2f  case( i==20 ); /
32d85 2a 20 4c 45 46 54 20 2a 2f 0a 20 20 20 20 20 20  * LEFT */.      
32d86 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 31 20  testcase( i==21 
32d87 29 3b 20 2f 2a 20 54 48 45 4e 20 2a 2f 0a 20 20  ); /* THEN */.  
32d88 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32d89 3d 32 32 20 29 3b 20 2f 2a 20 45 4e 44 20 2a 2f  =22 ); /* END */
32d8a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32d8b 20 69 3d 3d 32 33 20 29 3b 20 2f 2a 20 44 45 46   i==23 ); /* DEF
32d8c 45 52 52 41 42 4c 45 20 2a 2f 0a 20 20 20 20 20  ERRABLE */.     
32d8d 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 34   testcase( i==24
32d8e 20 29 3b 20 2f 2a 20 45 4c 53 45 20 2a 2f 0a 20   ); /* ELSE */. 
32d8f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32d90 3d 3d 32 35 20 29 3b 20 2f 2a 20 45 58 43 45 50  ==25 ); /* EXCEP
32d91 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
32d92 61 73 65 28 20 69 3d 3d 32 36 20 29 3b 20 2f 2a  ase( i==26 ); /*
32d93 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 2a 2f 0a   TRANSACTION */.
32d94 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32d95 69 3d 3d 32 37 20 29 3b 20 2f 2a 20 41 43 54 49  i==27 ); /* ACTI
32d96 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ON */.      test
32d97 63 61 73 65 28 20 69 3d 3d 32 38 20 29 3b 20 2f  case( i==28 ); /
32d98 2a 20 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  * ON */.      te
32d99 73 74 63 61 73 65 28 20 69 3d 3d 32 39 20 29 3b  stcase( i==29 );
32d9a 20 2f 2a 20 4e 41 54 55 52 41 4c 20 2a 2f 0a 20   /* NATURAL */. 
32d9b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32d9c 3d 3d 33 30 20 29 3b 20 2f 2a 20 41 4c 54 45 52  ==30 ); /* ALTER
32d9d 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32d9e 73 65 28 20 69 3d 3d 33 31 20 29 3b 20 2f 2a 20  se( i==31 ); /* 
32d9f 52 41 49 53 45 20 2a 2f 0a 20 20 20 20 20 20 74  RAISE */.      t
32da0 65 73 74 63 61 73 65 28 20 69 3d 3d 33 32 20 29  estcase( i==32 )
32da1 3b 20 2f 2a 20 45 58 43 4c 55 53 49 56 45 20 2a  ; /* EXCLUSIVE *
32da2 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32da3 28 20 69 3d 3d 33 33 20 29 3b 20 2f 2a 20 45 58  ( i==33 ); /* EX
32da4 49 53 54 53 20 2a 2f 0a 20 20 20 20 20 20 74 65  ISTS */.      te
32da5 73 74 63 61 73 65 28 20 69 3d 3d 33 34 20 29 3b  stcase( i==34 );
32da6 20 2f 2a 20 53 41 56 45 50 4f 49 4e 54 20 2a 2f   /* SAVEPOINT */
32da7 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32da8 20 69 3d 3d 33 35 20 29 3b 20 2f 2a 20 49 4e 54   i==35 ); /* INT
32da9 45 52 53 45 43 54 20 2a 2f 0a 20 20 20 20 20 20  ERSECT */.      
32daa 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 36 20  testcase( i==36 
32dab 29 3b 20 2f 2a 20 54 52 49 47 47 45 52 20 2a 2f  ); /* TRIGGER */
32dac 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32dad 20 69 3d 3d 33 37 20 29 3b 20 2f 2a 20 52 45 46   i==37 ); /* REF
32dae 45 52 45 4e 43 45 53 20 2a 2f 0a 20 20 20 20 20  ERENCES */.     
32daf 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 38   testcase( i==38
32db0 20 29 3b 20 2f 2a 20 43 4f 4e 53 54 52 41 49 4e   ); /* CONSTRAIN
32db1 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
32db2 61 73 65 28 20 69 3d 3d 33 39 20 29 3b 20 2f 2a  ase( i==39 ); /*
32db3 20 49 4e 54 4f 20 2a 2f 0a 20 20 20 20 20 20 74   INTO */.      t
32db4 65 73 74 63 61 73 65 28 20 69 3d 3d 34 30 20 29  estcase( i==40 )
32db5 3b 20 2f 2a 20 4f 46 46 53 45 54 20 2a 2f 0a 20  ; /* OFFSET */. 
32db6 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32db7 3d 3d 34 31 20 29 3b 20 2f 2a 20 4f 46 20 2a 2f  ==41 ); /* OF */
32db8 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32db9 20 69 3d 3d 34 32 20 29 3b 20 2f 2a 20 53 45 54   i==42 ); /* SET
32dba 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32dbb 73 65 28 20 69 3d 3d 34 33 20 29 3b 20 2f 2a 20  se( i==43 ); /* 
32dbc 54 45 4d 50 4f 52 41 52 59 20 2a 2f 0a 20 20 20  TEMPORARY */.   
32dbd 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32dbe 34 34 20 29 3b 20 2f 2a 20 54 45 4d 50 20 2a 2f  44 ); /* TEMP */
32dbf 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32dc0 20 69 3d 3d 34 35 20 29 3b 20 2f 2a 20 4f 52 20   i==45 ); /* OR 
32dc1 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32dc2 65 28 20 69 3d 3d 34 36 20 29 3b 20 2f 2a 20 55  e( i==46 ); /* U
32dc3 4e 49 51 55 45 20 2a 2f 0a 20 20 20 20 20 20 74  NIQUE */.      t
32dc4 65 73 74 63 61 73 65 28 20 69 3d 3d 34 37 20 29  estcase( i==47 )
32dc5 3b 20 2f 2a 20 51 55 45 52 59 20 2a 2f 0a 20 20  ; /* QUERY */.  
32dc6 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32dc7 3d 34 38 20 29 3b 20 2f 2a 20 41 54 54 41 43 48  =48 ); /* ATTACH
32dc8 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32dc9 73 65 28 20 69 3d 3d 34 39 20 29 3b 20 2f 2a 20  se( i==49 ); /* 
32dca 48 41 56 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20  HAVING */.      
32dcb 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 30 20  testcase( i==50 
32dcc 29 3b 20 2f 2a 20 47 52 4f 55 50 20 2a 2f 0a 20  ); /* GROUP */. 
32dcd 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32dce 3d 3d 35 31 20 29 3b 20 2f 2a 20 55 50 44 41 54  ==51 ); /* UPDAT
32dcf 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
32dd0 61 73 65 28 20 69 3d 3d 35 32 20 29 3b 20 2f 2a  ase( i==52 ); /*
32dd1 20 42 45 47 49 4e 20 2a 2f 0a 20 20 20 20 20 20   BEGIN */.      
32dd2 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 33 20  testcase( i==53 
32dd3 29 3b 20 2f 2a 20 49 4e 4e 45 52 20 2a 2f 0a 20  ); /* INNER */. 
32dd4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32dd5 3d 3d 35 34 20 29 3b 20 2f 2a 20 52 45 4c 45 41  ==54 ); /* RELEA
32dd6 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  SE */.      test
32dd7 63 61 73 65 28 20 69 3d 3d 35 35 20 29 3b 20 2f  case( i==55 ); /
32dd8 2a 20 42 45 54 57 45 45 4e 20 2a 2f 0a 20 20 20  * BETWEEN */.   
32dd9 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32dda 35 36 20 29 3b 20 2f 2a 20 4e 4f 54 4e 55 4c 4c  56 ); /* NOTNULL
32ddb 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32ddc 73 65 28 20 69 3d 3d 35 37 20 29 3b 20 2f 2a 20  se( i==57 ); /* 
32ddd 4e 4f 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  NOT */.      tes
32dde 74 63 61 73 65 28 20 69 3d 3d 35 38 20 29 3b 20  tcase( i==58 ); 
32ddf 2f 2a 20 4e 4f 20 2a 2f 0a 20 20 20 20 20 20 74  /* NO */.      t
32de0 65 73 74 63 61 73 65 28 20 69 3d 3d 35 39 20 29  estcase( i==59 )
32de1 3b 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20  ; /* NULL */.   
32de2 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32de3 36 30 20 29 3b 20 2f 2a 20 4c 49 4b 45 20 2a 2f  60 ); /* LIKE */
32de4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32de5 20 69 3d 3d 36 31 20 29 3b 20 2f 2a 20 43 41 53   i==61 ); /* CAS
32de6 43 41 44 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  CADE */.      te
32de7 73 74 63 61 73 65 28 20 69 3d 3d 36 32 20 29 3b  stcase( i==62 );
32de8 20 2f 2a 20 41 53 43 20 2a 2f 0a 20 20 20 20 20   /* ASC */.     
32de9 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 33   testcase( i==63
32dea 20 29 3b 20 2f 2a 20 44 45 4c 45 54 45 20 2a 2f   ); /* DELETE */
32deb 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32dec 20 69 3d 3d 36 34 20 29 3b 20 2f 2a 20 43 41 53   i==64 ); /* CAS
32ded 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
32dee 61 73 65 28 20 69 3d 3d 36 35 20 29 3b 20 2f 2a  ase( i==65 ); /*
32def 20 43 4f 4c 4c 41 54 45 20 2a 2f 0a 20 20 20 20   COLLATE */.    
32df0 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
32df1 36 20 29 3b 20 2f 2a 20 43 52 45 41 54 45 20 2a  6 ); /* CREATE *
32df2 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32df3 28 20 69 3d 3d 36 37 20 29 3b 20 2f 2a 20 43 55  ( i==67 ); /* CU
32df4 52 52 45 4e 54 5f 44 41 54 45 20 2a 2f 0a 20 20  RRENT_DATE */.  
32df5 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32df6 3d 36 38 20 29 3b 20 2f 2a 20 44 45 54 41 43 48  =68 ); /* DETACH
32df7 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32df8 73 65 28 20 69 3d 3d 36 39 20 29 3b 20 2f 2a 20  se( i==69 ); /* 
32df9 49 4d 4d 45 44 49 41 54 45 20 2a 2f 0a 20 20 20  IMMEDIATE */.   
32dfa 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32dfb 37 30 20 29 3b 20 2f 2a 20 4a 4f 49 4e 20 2a 2f  70 ); /* JOIN */
32dfc 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32dfd 20 69 3d 3d 37 31 20 29 3b 20 2f 2a 20 49 4e 53   i==71 ); /* INS
32dfe 45 52 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ERT */.      tes
32dff 74 63 61 73 65 28 20 69 3d 3d 37 32 20 29 3b 20  tcase( i==72 ); 
32e00 2f 2a 20 4d 41 54 43 48 20 2a 2f 0a 20 20 20 20  /* MATCH */.    
32e01 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
32e02 33 20 29 3b 20 2f 2a 20 50 4c 41 4e 20 2a 2f 0a  3 ); /* PLAN */.
32e03 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32e04 69 3d 3d 37 34 20 29 3b 20 2f 2a 20 41 4e 41 4c  i==74 ); /* ANAL
32e05 59 5a 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  YZE */.      tes
32e06 74 63 61 73 65 28 20 69 3d 3d 37 35 20 29 3b 20  tcase( i==75 ); 
32e07 2f 2a 20 50 52 41 47 4d 41 20 2a 2f 0a 20 20 20  /* PRAGMA */.   
32e08 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32e09 37 36 20 29 3b 20 2f 2a 20 41 42 4f 52 54 20 2a  76 ); /* ABORT *
32e0a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32e0b 28 20 69 3d 3d 37 37 20 29 3b 20 2f 2a 20 56 41  ( i==77 ); /* VA
32e0c 4c 55 45 53 20 2a 2f 0a 20 20 20 20 20 20 74 65  LUES */.      te
32e0d 73 74 63 61 73 65 28 20 69 3d 3d 37 38 20 29 3b  stcase( i==78 );
32e0e 20 2f 2a 20 56 49 52 54 55 41 4c 20 2a 2f 0a 20   /* VIRTUAL */. 
32e0f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32e10 3d 3d 37 39 20 29 3b 20 2f 2a 20 4c 49 4d 49 54  ==79 ); /* LIMIT
32e11 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32e12 73 65 28 20 69 3d 3d 38 30 20 29 3b 20 2f 2a 20  se( i==80 ); /* 
32e13 57 48 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  WHEN */.      te
32e14 73 74 63 61 73 65 28 20 69 3d 3d 38 31 20 29 3b  stcase( i==81 );
32e15 20 2f 2a 20 57 48 45 52 45 20 2a 2f 0a 20 20 20   /* WHERE */.   
32e16 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32e17 38 32 20 29 3b 20 2f 2a 20 52 45 4e 41 4d 45 20  82 ); /* RENAME 
32e18 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32e19 65 28 20 69 3d 3d 38 33 20 29 3b 20 2f 2a 20 41  e( i==83 ); /* A
32e1a 46 54 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65  FTER */.      te
32e1b 73 74 63 61 73 65 28 20 69 3d 3d 38 34 20 29 3b  stcase( i==84 );
32e1c 20 2f 2a 20 52 45 50 4c 41 43 45 20 2a 2f 0a 20   /* REPLACE */. 
32e1d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32e1e 3d 3d 38 35 20 29 3b 20 2f 2a 20 41 4e 44 20 2a  ==85 ); /* AND *
32e1f 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32e20 28 20 69 3d 3d 38 36 20 29 3b 20 2f 2a 20 44 45  ( i==86 ); /* DE
32e21 46 41 55 4c 54 20 2a 2f 0a 20 20 20 20 20 20 74  FAULT */.      t
32e22 65 73 74 63 61 73 65 28 20 69 3d 3d 38 37 20 29  estcase( i==87 )
32e23 3b 20 2f 2a 20 41 55 54 4f 49 4e 43 52 45 4d 45  ; /* AUTOINCREME
32e24 4e 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  NT */.      test
32e25 63 61 73 65 28 20 69 3d 3d 38 38 20 29 3b 20 2f  case( i==88 ); /
32e26 2a 20 54 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65  * TO */.      te
32e27 73 74 63 61 73 65 28 20 69 3d 3d 38 39 20 29 3b  stcase( i==89 );
32e28 20 2f 2a 20 49 4e 20 2a 2f 0a 20 20 20 20 20 20   /* IN */.      
32e29 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 30 20  testcase( i==90 
32e2a 29 3b 20 2f 2a 20 43 41 53 54 20 2a 2f 0a 20 20  ); /* CAST */.  
32e2b 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32e2c 3d 39 31 20 29 3b 20 2f 2a 20 43 4f 4c 55 4d 4e  =91 ); /* COLUMN
32e2d 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32e2e 73 65 28 20 69 3d 3d 39 32 20 29 3b 20 2f 2a 20  se( i==92 ); /* 
32e2f 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20 20 20 20 20  COMMIT */.      
32e30 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 33 20  testcase( i==93 
32e31 29 3b 20 2f 2a 20 43 4f 4e 46 4c 49 43 54 20 2a  ); /* CONFLICT *
32e32 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32e33 28 20 69 3d 3d 39 34 20 29 3b 20 2f 2a 20 43 52  ( i==94 ); /* CR
32e34 4f 53 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  OSS */.      tes
32e35 74 63 61 73 65 28 20 69 3d 3d 39 35 20 29 3b 20  tcase( i==95 ); 
32e36 2f 2a 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 53  /* CURRENT_TIMES
32e37 54 41 4d 50 20 2a 2f 0a 20 20 20 20 20 20 74 65  TAMP */.      te
32e38 73 74 63 61 73 65 28 20 69 3d 3d 39 36 20 29 3b  stcase( i==96 );
32e39 20 2f 2a 20 43 55 52 52 45 4e 54 5f 54 49 4d 45   /* CURRENT_TIME
32e3a 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32e3b 73 65 28 20 69 3d 3d 39 37 20 29 3b 20 2f 2a 20  se( i==97 ); /* 
32e3c 50 52 49 4d 41 52 59 20 2a 2f 0a 20 20 20 20 20  PRIMARY */.     
32e3d 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 38   testcase( i==98
32e3e 20 29 3b 20 2f 2a 20 44 45 46 45 52 52 45 44 20   ); /* DEFERRED 
32e3f 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32e40 65 28 20 69 3d 3d 39 39 20 29 3b 20 2f 2a 20 44  e( i==99 ); /* D
32e41 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 20 20 20  ISTINCT */.     
32e42 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
32e43 30 20 29 3b 20 2f 2a 20 49 53 20 2a 2f 0a 20 20  0 ); /* IS */.  
32e44 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32e45 3d 31 30 31 20 29 3b 20 2f 2a 20 44 52 4f 50 20  =101 ); /* DROP 
32e46 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32e47 65 28 20 69 3d 3d 31 30 32 20 29 3b 20 2f 2a 20  e( i==102 ); /* 
32e48 46 41 49 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65  FAIL */.      te
32e49 73 74 63 61 73 65 28 20 69 3d 3d 31 30 33 20 29  stcase( i==103 )
32e4a 3b 20 2f 2a 20 46 52 4f 4d 20 2a 2f 0a 20 20 20  ; /* FROM */.   
32e4b 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32e4c 31 30 34 20 29 3b 20 2f 2a 20 46 55 4c 4c 20 2a  104 ); /* FULL *
32e4d 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32e4e 28 20 69 3d 3d 31 30 35 20 29 3b 20 2f 2a 20 47  ( i==105 ); /* G
32e4f 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  LOB */.      tes
32e50 74 63 61 73 65 28 20 69 3d 3d 31 30 36 20 29 3b  tcase( i==106 );
32e51 20 2f 2a 20 42 59 20 2a 2f 0a 20 20 20 20 20 20   /* BY */.      
32e52 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 37  testcase( i==107
32e53 20 29 3b 20 2f 2a 20 49 46 20 2a 2f 0a 20 20 20   ); /* IF */.   
32e54 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32e55 31 30 38 20 29 3b 20 2f 2a 20 49 53 4e 55 4c 4c  108 ); /* ISNULL
32e56 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32e57 73 65 28 20 69 3d 3d 31 30 39 20 29 3b 20 2f 2a  se( i==109 ); /*
32e58 20 4f 52 44 45 52 20 2a 2f 0a 20 20 20 20 20 20   ORDER */.      
32e59 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 30  testcase( i==110
32e5a 20 29 3b 20 2f 2a 20 52 45 53 54 52 49 43 54 20   ); /* RESTRICT 
32e5b 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32e5c 65 28 20 69 3d 3d 31 31 31 20 29 3b 20 2f 2a 20  e( i==111 ); /* 
32e5d 4f 55 54 45 52 20 2a 2f 0a 20 20 20 20 20 20 74  OUTER */.      t
32e5e 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 32 20  estcase( i==112 
32e5f 29 3b 20 2f 2a 20 52 49 47 48 54 20 2a 2f 0a 20  ); /* RIGHT */. 
32e60 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32e61 3d 3d 31 31 33 20 29 3b 20 2f 2a 20 52 4f 4c 4c  ==113 ); /* ROLL
32e62 42 41 43 4b 20 2a 2f 0a 20 20 20 20 20 20 74 65  BACK */.      te
32e63 73 74 63 61 73 65 28 20 69 3d 3d 31 31 34 20 29  stcase( i==114 )
32e64 3b 20 2f 2a 20 52 4f 57 20 2a 2f 0a 20 20 20 20  ; /* ROW */.    
32e65 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
32e66 31 35 20 29 3b 20 2f 2a 20 55 4e 49 4f 4e 20 2a  15 ); /* UNION *
32e67 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32e68 28 20 69 3d 3d 31 31 36 20 29 3b 20 2f 2a 20 55  ( i==116 ); /* U
32e69 53 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74 65  SING */.      te
32e6a 73 74 63 61 73 65 28 20 69 3d 3d 31 31 37 20 29  stcase( i==117 )
32e6b 3b 20 2f 2a 20 56 41 43 55 55 4d 20 2a 2f 0a 20  ; /* VACUUM */. 
32e6c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32e6d 3d 3d 31 31 38 20 29 3b 20 2f 2a 20 56 49 45 57  ==118 ); /* VIEW
32e6e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32e6f 73 65 28 20 69 3d 3d 31 31 39 20 29 3b 20 2f 2a  se( i==119 ); /*
32e70 20 49 4e 49 54 49 41 4c 4c 59 20 2a 2f 0a 20 20   INITIALLY */.  
32e71 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32e72 3d 31 32 30 20 29 3b 20 2f 2a 20 41 4c 4c 20 2a  =120 ); /* ALL *
32e73 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 61  /.      return a
32e74 43 6f 64 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20  Code[i];.    }. 
32e75 20 7d 0a 20 20 72 65 74 75 72 6e 20 54 4b 5f 49   }.  return TK_I
32e76 44 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  D;.}.SQLITE_PRIV
32e77 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b  ATE int sqlite3K
32e78 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74  eywordCode(const
32e79 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
32e7a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74  z, int n){.  ret
32e7b 75 72 6e 20 6b 65 79 77 6f 72 64 43 6f 64 65 28  urn keywordCode(
32e7c 28 63 68 61 72 2a 29 7a 2c 20 6e 29 3b 0a 7d 0a  (char*)z, n);.}.
32e7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
32e7e 20 45 6e 64 20 6f 66 20 6b 65 79 77 6f 72 64 68   End of keywordh
32e7f 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.h **********
32e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
32e82 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
32e83 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
32e84 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
32e85 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a   tokenize.c ****
32e86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
32e87 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 58 20 69 73  .../*.** If X is
32e88 20 61 20 63 68 61 72 61 63 74 65 72 20 74 68 61   a character tha
32e89 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e  t can be used in
32e8a 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74   an identifier t
32e8b 68 65 6e 0a 2a 2a 20 49 64 43 68 61 72 28 58 29  hen.** IdChar(X)
32e8c 20 77 69 6c 6c 20 62 65 20 74 72 75 65 2e 20 20   will be true.  
32e8d 4f 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20  Otherwise it is 
32e8e 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  false..**.** For
32e8f 20 41 53 43 49 49 2c 20 61 6e 79 20 63 68 61 72   ASCII, any char
32e90 61 63 74 65 72 20 77 69 74 68 20 74 68 65 20 68  acter with the h
32e91 69 67 68 2d 6f 72 64 65 72 20 62 69 74 20 73 65  igh-order bit se
32e92 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20  t is.** allowed 
32e93 69 6e 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  in an identifier
32e94 2e 20 20 46 6f 72 20 37 2d 62 69 74 20 63 68 61  .  For 7-bit cha
32e95 72 61 63 74 65 72 73 2c 20 0a 2a 2a 20 73 71 6c  racters, .** sql
32e96 69 74 65 33 49 73 49 64 43 68 61 72 5b 58 5d 20  ite3IsIdChar[X] 
32e97 6d 75 73 74 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a  must be 1..**.**
32e98 20 46 6f 72 20 45 42 43 44 49 43 2c 20 74 68 65   For EBCDIC, the
32e99 20 72 75 6c 65 73 20 61 72 65 20 6d 6f 72 65 20   rules are more 
32e9a 63 6f 6d 70 6c 65 78 20 62 75 74 20 68 61 76 65  complex but have
32e9b 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 65 6e 64   the same.** end
32e9c 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54   result..**.** T
32e9d 69 63 6b 65 74 20 23 31 30 36 36 2e 20 20 74 68  icket #1066.  th
32e9e 65 20 53 51 4c 20 73 74 61 6e 64 61 72 64 20 64  e SQL standard d
32e9f 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 27 24  oes not allow '$
32ea0 27 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 69 64 64  ' in the.** midd
32ea1 6c 65 20 6f 66 20 69 64 65 6e 74 66 69 65 72 73  le of identfiers
32ea2 2e 20 20 42 75 74 20 6d 61 6e 79 20 53 51 4c 20  .  But many SQL 
32ea3 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
32ea4 64 6f 2e 20 0a 2a 2a 20 53 51 4c 69 74 65 20 77  do. .** SQLite w
32ea5 69 6c 6c 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e  ill allow '$' in
32ea6 20 69 64 65 6e 74 69 66 69 65 72 73 20 66 6f 72   identifiers for
32ea7 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a   compatibility..
32ea8 2a 2a 20 42 75 74 20 74 68 65 20 66 65 61 74 75  ** But the featu
32ea9 72 65 20 69 73 20 75 6e 64 6f 63 75 6d 65 6e 74  re is undocument
32eaa 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ed..*/.#ifdef SQ
32eab 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54  LITE_ASCII.SQLIT
32eac 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
32ead 63 68 61 72 20 73 71 6c 69 74 65 33 49 73 41 73  char sqlite3IsAs
32eae 63 69 69 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a  ciiIdChar[] = {.
32eaf 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33 20 78  /* x0 x1 x2 x3 x
32eb0 34 20 78 35 20 78 36 20 78 37 20 78 38 20 78 39  4 x5 x6 x7 x8 x9
32eb1 20 78 41 20 78 42 20 78 43 20 78 44 20 78 45 20   xA xB xC xD xE 
32eb2 78 46 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20  xF */.    0, 0, 
32eb3 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30  0, 0, 1, 0, 0, 0
32eb4 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
32eb5 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 32 78   0, 0, 0,  /* 2x
32eb6 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
32eb7 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32eb8 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 1, 0, 0, 0, 0
32eb9 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78 20 2a  , 0, 0,  /* 3x *
32eba 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
32ebb 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32ebc 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32ebd 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a  1, 1,  /* 4x */.
32ebe 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20      1, 1, 1, 1, 
32ebf 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32ec0 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
32ec1 20 31 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20   1,  /* 5x */.  
32ec2 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
32ec3 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32ec4 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32ec5 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20  ,  /* 6x */.    
32ec6 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32ec7 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32ec8 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
32ec9 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64 65   /* 7x */.};.#de
32eca 66 69 6e 65 20 49 64 43 68 61 72 28 43 29 20 20  fine IdChar(C)  
32ecb 28 28 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30  (((c=C)&0x80)!=0
32ecc 20 7c 7c 20 28 63 3e 30 78 31 66 20 26 26 20 73   || (c>0x1f && s
32ecd 71 6c 69 74 65 33 49 73 41 73 63 69 69 49 64 43  qlite3IsAsciiIdC
32ece 68 61 72 5b 63 2d 30 78 32 30 5d 29 29 0a 23 65  har[c-0x20])).#e
32ecf 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
32ed0 54 45 5f 45 42 43 44 49 43 0a 53 51 4c 49 54 45  TE_EBCDIC.SQLITE
32ed1 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
32ed2 68 61 72 20 73 71 6c 69 74 65 33 49 73 45 62 63  har sqlite3IsEbc
32ed3 64 69 63 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a  dicIdChar[] = {.
32ed4 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33 20 78  /* x0 x1 x2 x3 x
32ed5 34 20 78 35 20 78 36 20 78 37 20 78 38 20 78 39  4 x5 x6 x7 x8 x9
32ed6 20 78 41 20 78 42 20 78 43 20 78 44 20 78 45 20   xA xB xC xD xE 
32ed7 78 46 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20  xF */.    0, 0, 
32ed8 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32ed9 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
32eda 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 34 78   0, 0, 0,  /* 4x
32edb 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
32edc 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32edd 31 2c 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20 30  1, 1, 0, 1, 0, 0
32ede 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 35 78 20 2a  , 0, 0,  /* 5x *
32edf 2f 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31  /.    0, 0, 1, 1
32ee0 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32ee1 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20   1, 0, 0, 0, 1, 
32ee2 30 2c 20 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a  0, 0,  /* 6x */.
32ee3 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
32ee4 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
32ee5 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
32ee6 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20   0,  /* 7x */.  
32ee7 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
32ee8 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32ee9 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30  0, 0, 1, 1, 1, 0
32eea 2c 20 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20  ,  /* 8x */.    
32eeb 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
32eec 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
32eed 20 30 2c 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20   0, 1, 0, 1, 0, 
32eee 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 2c   /* 9x */.    1,
32eef 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
32ef0 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
32ef1 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f  , 1, 1, 1, 0,  /
32ef2 2a 20 41 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30  * Ax */.    0, 0
32ef3 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
32ef4 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
32ef5 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
32ef6 42 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  Bx */.    0, 1, 
32ef7 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32ef8 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c  , 1, 1, 0, 1, 1,
32ef9 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 43 78   1, 1, 1,  /* Cx
32efa 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
32efb 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32efc 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
32efd 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 44 78 20 2a  , 1, 1,  /* Dx *
32efe 2f 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31  /.    0, 0, 1, 1
32eff 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32f00 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 0, 1, 1, 1, 
32f01 31 2c 20 31 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a  1, 1,  /* Ex */.
32f02 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20      1, 1, 1, 1, 
32f03 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32f04 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
32f05 20 30 2c 20 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b   0,  /* Fx */.};
32f06 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61 72 28  .#define IdChar(
32f07 43 29 20 20 28 28 28 63 3d 43 29 3e 3d 30 78 34  C)  (((c=C)>=0x4
32f08 32 20 26 26 20 73 71 6c 69 74 65 33 49 73 45 62  2 && sqlite3IsEb
32f09 63 64 69 63 49 64 43 68 61 72 5b 63 2d 30 78 34  cdicIdChar[c-0x4
32f0a 30 5d 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  0])).#endif.../*
32f0b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c  .** Return the l
32f0c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b  ength of the tok
32f0d 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 61  en that begins a
32f0e 74 20 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74 6f 72  t z[0]. .** Stor
32f0f 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 79 70 65  e the token type
32f10 20 69 6e 20 2a 74 6f 6b 65 6e 54 79 70 65 20 62   in *tokenType b
32f11 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
32f12 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
32f13 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
32f14 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73  tToken(const uns
32f15 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69  igned char *z, i
32f16 6e 74 20 2a 74 6f 6b 65 6e 54 79 70 65 29 7b 0a  nt *tokenType){.
32f17 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 73 77    int i, c;.  sw
32f18 69 74 63 68 28 20 2a 7a 20 29 7b 0a 20 20 20 20  itch( *z ){.    
32f19 63 61 73 65 20 27 20 27 3a 20 63 61 73 65 20 27  case ' ': case '
32f1a 5c 74 27 3a 20 63 61 73 65 20 27 5c 6e 27 3a 20  \t': case '\n': 
32f1b 63 61 73 65 20 27 5c 66 27 3a 20 63 61 73 65 20  case '\f': case 
32f1c 27 5c 72 27 3a 20 7b 0a 20 20 20 20 20 20 74 65  '\r': {.      te
32f1d 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 20  stcase( z[0]==' 
32f1e 27 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  ' );.      testc
32f1f 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 5c 74 27 20  ase( z[0]=='\t' 
32f20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
32f21 65 28 20 7a 5b 30 5d 3d 3d 27 5c 6e 27 20 29 3b  e( z[0]=='\n' );
32f22 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32f23 20 7a 5b 30 5d 3d 3d 27 5c 66 27 20 29 3b 0a 20   z[0]=='\f' );. 
32f24 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
32f25 5b 30 5d 3d 3d 27 5c 72 27 20 29 3b 0a 20 20 20  [0]=='\r' );.   
32f26 20 20 20 66 6f 72 28 69 3d 31 3b 20 73 71 6c 69     for(i=1; sqli
32f27 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29  te3Isspace(z[i])
32f28 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a  ; i++){}.      *
32f29 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53  tokenType = TK_S
32f2a 50 41 43 45 3b 0a 20 20 20 20 20 20 72 65 74 75  PACE;.      retu
32f2b 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn i;.    }.    
32f2c 63 61 73 65 20 27 2d 27 3a 20 7b 0a 20 20 20 20  case '-': {.    
32f2d 20 20 69 66 28 20 7a 5b 31 5d 3d 3d 27 2d 27 20    if( z[1]=='-' 
32f2e 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ){.        for(i
32f2f 3d 32 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20  =2; (c=z[i])!=0 
32f30 26 26 20 63 21 3d 27 5c 6e 27 3b 20 69 2b 2b 29  && c!='\n'; i++)
32f31 7b 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  {}.        *toke
32f32 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45  nType = TK_SPACE
32f33 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
32f34 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   i;.      }.    
32f35 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
32f36 4b 5f 4d 49 4e 55 53 3b 0a 20 20 20 20 20 20 72  K_MINUS;.      r
32f37 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
32f38 20 20 20 63 61 73 65 20 27 28 27 3a 20 7b 0a 20     case '(': {. 
32f39 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
32f3a 3d 20 54 4b 5f 4c 50 3b 0a 20 20 20 20 20 20 72  = TK_LP;.      r
32f3b 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
32f3c 20 20 20 63 61 73 65 20 27 29 27 3a 20 7b 0a 20     case ')': {. 
32f3d 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
32f3e 3d 20 54 4b 5f 52 50 3b 0a 20 20 20 20 20 20 72  = TK_RP;.      r
32f3f 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
32f40 20 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 0a 20     case ';': {. 
32f41 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
32f42 3d 20 54 4b 5f 53 45 4d 49 3b 0a 20 20 20 20 20  = TK_SEMI;.     
32f43 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
32f44 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 20 7b  .    case '+': {
32f45 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
32f46 65 20 3d 20 54 4b 5f 50 4c 55 53 3b 0a 20 20 20  e = TK_PLUS;.   
32f47 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
32f48 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2a 27 3a   }.    case '*':
32f49 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
32f4a 79 70 65 20 3d 20 54 4b 5f 53 54 41 52 3b 0a 20  ype = TK_STAR;. 
32f4b 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
32f4c 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2f     }.    case '/
32f4d 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a  ': {.      if( z
32f4e 5b 31 5d 21 3d 27 2a 27 20 7c 7c 20 7a 5b 32 5d  [1]!='*' || z[2]
32f4f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  ==0 ){.        *
32f50 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53  tokenType = TK_S
32f51 4c 41 53 48 3b 0a 20 20 20 20 20 20 20 20 72 65  LASH;.        re
32f52 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
32f53 20 20 20 20 20 20 66 6f 72 28 69 3d 33 2c 20 63        for(i=3, c
32f54 3d 7a 5b 32 5d 3b 20 28 63 21 3d 27 2a 27 20 7c  =z[2]; (c!='*' |
32f55 7c 20 7a 5b 69 5d 21 3d 27 2f 27 29 20 26 26 20  | z[i]!='/') && 
32f56 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b  (c=z[i])!=0; i++
32f57 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 63 20  ){}.      if( c 
32f58 29 20 69 2b 2b 3b 0a 20 20 20 20 20 20 2a 74 6f  ) i++;.      *to
32f59 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41  kenType = TK_SPA
32f5a 43 45 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  CE;.      return
32f5b 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   i;.    }.    ca
32f5c 73 65 20 27 25 27 3a 20 7b 0a 20 20 20 20 20 20  se '%': {.      
32f5d 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
32f5e 52 45 4d 3b 0a 20 20 20 20 20 20 72 65 74 75 72  REM;.      retur
32f5f 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
32f60 61 73 65 20 27 3d 27 3a 20 7b 0a 20 20 20 20 20  ase '=': {.     
32f61 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
32f62 5f 45 51 3b 0a 20 20 20 20 20 20 72 65 74 75 72  _EQ;.      retur
32f63 6e 20 31 20 2b 20 28 7a 5b 31 5d 3d 3d 27 3d 27  n 1 + (z[1]=='='
32f64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  );.    }.    cas
32f65 65 20 27 3c 27 3a 20 7b 0a 20 20 20 20 20 20 69  e '<': {.      i
32f66 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27  f( (c=z[1])=='='
32f67 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
32f68 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 45 3b 0a  enType = TK_LE;.
32f69 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
32f6a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
32f6b 28 20 63 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20  ( c=='>' ){.    
32f6c 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
32f6d 20 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 20 20   TK_NE;.        
32f6e 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20  return 2;.      
32f6f 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3c 27  }else if( c=='<'
32f70 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
32f71 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 53 48 49  enType = TK_LSHI
32f72 46 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  FT;.        retu
32f73 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rn 2;.      }els
32f74 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  e{.        *toke
32f75 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 54 3b 0a 20  nType = TK_LT;. 
32f76 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
32f77 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
32f78 20 20 20 63 61 73 65 20 27 3e 27 3a 20 7b 0a 20     case '>': {. 
32f79 20 20 20 20 20 69 66 28 20 28 63 3d 7a 5b 31 5d       if( (c=z[1]
32f7a 29 3d 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20 20  )=='=' ){.      
32f7b 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
32f7c 4b 5f 47 45 3b 0a 20 20 20 20 20 20 20 20 72 65  K_GE;.        re
32f7d 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65  turn 2;.      }e
32f7e 6c 73 65 20 69 66 28 20 63 3d 3d 27 3e 27 20 29  lse if( c=='>' )
32f7f 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
32f80 54 79 70 65 20 3d 20 54 4b 5f 52 53 48 49 46 54  Type = TK_RSHIFT
32f81 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
32f82 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   2;.      }else{
32f83 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
32f84 79 70 65 20 3d 20 54 4b 5f 47 54 3b 0a 20 20 20  ype = TK_GT;.   
32f85 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
32f86 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
32f87 20 63 61 73 65 20 27 21 27 3a 20 7b 0a 20 20 20   case '!': {.   
32f88 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 3d 27     if( z[1]!='='
32f89 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
32f8a 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
32f8b 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74  GAL;.        ret
32f8c 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c  urn 2;.      }el
32f8d 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  se{.        *tok
32f8e 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a  enType = TK_NE;.
32f8f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
32f90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
32f91 20 20 20 20 63 61 73 65 20 27 7c 27 3a 20 7b 0a      case '|': {.
32f92 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d        if( z[1]!=
32f93 27 7c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  '|' ){.        *
32f94 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42  tokenType = TK_B
32f95 49 54 4f 52 3b 0a 20 20 20 20 20 20 20 20 72 65  ITOR;.        re
32f96 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65  turn 1;.      }e
32f97 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  lse{.        *to
32f98 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4e  kenType = TK_CON
32f99 43 41 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74  CAT;.        ret
32f9a 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 2;.      }. 
32f9b 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2c     }.    case ',
32f9c 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
32f9d 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d 41  nType = TK_COMMA
32f9e 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
32f9f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
32fa0 20 27 26 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74   '&': {.      *t
32fa1 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49  okenType = TK_BI
32fa2 54 41 4e 44 3b 0a 20 20 20 20 20 20 72 65 74 75  TAND;.      retu
32fa3 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
32fa4 63 61 73 65 20 27 7e 27 3a 20 7b 0a 20 20 20 20  case '~': {.    
32fa5 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
32fa6 4b 5f 42 49 54 4e 4f 54 3b 0a 20 20 20 20 20 20  K_BITNOT;.      
32fa7 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
32fa8 20 20 20 20 63 61 73 65 20 27 60 27 3a 0a 20 20      case '`':.  
32fa9 20 20 63 61 73 65 20 27 5c 27 27 3a 0a 20 20 20    case '\'':.   
32faa 20 63 61 73 65 20 27 22 27 3a 20 7b 0a 20 20 20   case '"': {.   
32fab 20 20 20 69 6e 74 20 64 65 6c 69 6d 20 3d 20 7a     int delim = z
32fac 5b 30 5d 3b 0a 20 20 20 20 20 20 74 65 73 74 63  [0];.      testc
32fad 61 73 65 28 20 64 65 6c 69 6d 3d 3d 27 60 27 20  ase( delim=='`' 
32fae 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
32faf 65 28 20 64 65 6c 69 6d 3d 3d 27 5c 27 27 20 29  e( delim=='\'' )
32fb0 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
32fb1 28 20 64 65 6c 69 6d 3d 3d 27 22 27 20 29 3b 0a  ( delim=='"' );.
32fb2 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 28        for(i=1; (
32fb3 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  c=z[i])!=0; i++)
32fb4 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d  {.        if( c=
32fb5 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20  =delim ){.      
32fb6 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d      if( z[i+1]==
32fb7 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20 20  delim ){.       
32fb8 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
32fb9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
32fba 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32fbb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
32fbc 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
32fbd 20 20 69 66 28 20 63 3d 3d 27 5c 27 27 20 29 7b    if( c=='\'' ){
32fbe 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
32fbf 79 70 65 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b  ype = TK_STRING;
32fc0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
32fc1 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  i+1;.      }else
32fc2 20 69 66 28 20 63 21 3d 30 20 29 7b 0a 20 20 20   if( c!=0 ){.   
32fc3 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
32fc4 3d 20 54 4b 5f 49 44 3b 0a 20 20 20 20 20 20 20  = TK_ID;.       
32fc5 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20   return i+1;.   
32fc6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
32fc7 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
32fc8 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20  K_ILLEGAL;.     
32fc9 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
32fca 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63     }.    }.    c
32fcb 61 73 65 20 27 2e 27 3a 20 7b 0a 23 69 66 6e 64  ase '.': {.#ifnd
32fcc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
32fcd 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
32fce 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
32fcf 49 73 64 69 67 69 74 28 7a 5b 31 5d 29 20 29 0a  Isdigit(z[1]) ).
32fd0 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20  #endif.      {. 
32fd1 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
32fd2 65 20 3d 20 54 4b 5f 44 4f 54 3b 0a 20 20 20 20  e = TK_DOT;.    
32fd3 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
32fd4 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49      }.      /* I
32fd5 66 20 74 68 65 20 6e 65 78 74 20 63 68 61 72 61  f the next chara
32fd6 63 74 65 72 20 69 73 20 61 20 64 69 67 69 74 2c  cter is a digit,
32fd7 20 74 68 69 73 20 69 73 20 61 20 66 6c 6f 61 74   this is a float
32fd8 69 6e 67 20 70 6f 69 6e 74 0a 20 20 20 20 20 20  ing point.      
32fd9 2a 2a 20 6e 75 6d 62 65 72 20 74 68 61 74 20 62  ** number that b
32fda 65 67 69 6e 73 20 77 69 74 68 20 22 2e 22 2e 20  egins with ".". 
32fdb 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20   Fall thru into 
32fdc 74 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f  the next case */
32fdd 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
32fde 27 30 27 3a 20 63 61 73 65 20 27 31 27 3a 20 63  '0': case '1': c
32fdf 61 73 65 20 27 32 27 3a 20 63 61 73 65 20 27 33  ase '2': case '3
32fe0 27 3a 20 63 61 73 65 20 27 34 27 3a 0a 20 20 20  ': case '4':.   
32fe1 20 63 61 73 65 20 27 35 27 3a 20 63 61 73 65 20   case '5': case 
32fe2 27 36 27 3a 20 63 61 73 65 20 27 37 27 3a 20 63  '6': case '7': c
32fe3 61 73 65 20 27 38 27 3a 20 63 61 73 65 20 27 39  ase '8': case '9
32fe4 27 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  ': {.      testc
32fe5 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29  ase( z[0]=='0' )
32fe6 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30  ;  testcase( z[0
32fe7 5d 3d 3d 27 31 27 20 29 3b 20 20 74 65 73 74 63  ]=='1' );  testc
32fe8 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 32 27 20 29  ase( z[0]=='2' )
32fe9 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
32fea 28 20 7a 5b 30 5d 3d 3d 27 33 27 20 29 3b 20 20  ( z[0]=='3' );  
32feb 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
32fec 27 34 27 20 29 3b 20 20 74 65 73 74 63 61 73 65  '4' );  testcase
32fed 28 20 7a 5b 30 5d 3d 3d 27 35 27 20 29 3b 0a 20  ( z[0]=='5' );. 
32fee 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
32fef 5b 30 5d 3d 3d 27 36 27 20 29 3b 20 20 74 65 73  [0]=='6' );  tes
32ff0 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 37 27  tcase( z[0]=='7'
32ff1 20 29 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a   );  testcase( z
32ff2 5b 30 5d 3d 3d 27 38 27 20 29 3b 0a 20 20 20 20  [0]=='8' );.    
32ff3 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
32ff4 3d 3d 27 39 27 20 29 3b 0a 20 20 20 20 20 20 2a  =='9' );.      *
32ff5 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
32ff6 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 66 6f  NTEGER;.      fo
32ff7 72 28 69 3d 30 3b 20 73 71 6c 69 74 65 33 49 73  r(i=0; sqlite3Is
32ff8 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b  digit(z[i]); i++
32ff9 29 7b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ){}.#ifndef SQLI
32ffa 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
32ffb 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28  _POINT.      if(
32ffc 20 7a 5b 69 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20   z[i]=='.' ){.  
32ffd 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
32ffe 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65     while( sqlite
32fff 33 49 73 64 69 67 69 74 28 7a 5b 69 5d 29 20 29  3Isdigit(z[i]) )
33000 7b 20 69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  { i++; }.       
33001 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
33002 5f 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 7d 0a  _FLOAT;.      }.
33003 20 20 20 20 20 20 69 66 28 20 28 7a 5b 69 5d 3d        if( (z[i]=
33004 3d 27 65 27 20 7c 7c 20 7a 5b 69 5d 3d 3d 27 45  ='e' || z[i]=='E
33005 27 29 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  ') &&.          
33006 20 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69   ( sqlite3Isdigi
33007 74 28 7a 5b 69 2b 31 5d 29 20 0a 20 20 20 20 20  t(z[i+1]) .     
33008 20 20 20 20 20 20 20 7c 7c 20 28 28 7a 5b 69 2b         || ((z[i+
33009 31 5d 3d 3d 27 2b 27 20 7c 7c 20 7a 5b 69 2b 31  1]=='+' || z[i+1
3300a 5d 3d 3d 27 2d 27 29 20 26 26 20 73 71 6c 69 74  ]=='-') && sqlit
3300b 65 33 49 73 64 69 67 69 74 28 7a 5b 69 2b 32 5d  e3Isdigit(z[i+2]
3300c 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 29 0a  )).           ).
3300d 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
3300e 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20   i += 2;.       
3300f 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
33010 73 64 69 67 69 74 28 7a 5b 69 5d 29 20 29 7b 20  sdigit(z[i]) ){ 
33011 69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 2a  i++; }.        *
33012 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 46  tokenType = TK_F
33013 4c 4f 41 54 3b 0a 20 20 20 20 20 20 7d 0a 23 65  LOAT;.      }.#e
33014 6e 64 69 66 0a 20 20 20 20 20 20 77 68 69 6c 65  ndif.      while
33015 28 20 49 64 43 68 61 72 28 7a 5b 69 5d 29 20 29  ( IdChar(z[i]) )
33016 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
33017 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41  Type = TK_ILLEGA
33018 4c 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a  L;.        i++;.
33019 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
3301a 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20  turn i;.    }.  
3301b 20 20 63 61 73 65 20 27 5b 27 3a 20 7b 0a 20 20    case '[': {.  
3301c 20 20 20 20 66 6f 72 28 69 3d 31 2c 20 63 3d 7a      for(i=1, c=z
3301d 5b 30 5d 3b 20 63 21 3d 27 5d 27 20 26 26 20 28  [0]; c!=']' && (
3301e 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  c=z[i])!=0; i++)
3301f 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  {}.      *tokenT
33020 79 70 65 20 3d 20 63 3d 3d 27 5d 27 20 3f 20 54  ype = c==']' ? T
33021 4b 5f 49 44 20 3a 20 54 4b 5f 49 4c 4c 45 47 41  K_ID : TK_ILLEGA
33022 4c 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  L;.      return 
33023 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  i;.    }.    cas
33024 65 20 27 3f 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '?': {.      *
33025 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56  tokenType = TK_V
33026 41 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20 66  ARIABLE;.      f
33027 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74 65 33 49  or(i=1; sqlite3I
33028 73 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b  sdigit(z[i]); i+
33029 2b 29 7b 7d 0a 20 20 20 20 20 20 72 65 74 75 72  +){}.      retur
3302a 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n i;.    }.    c
3302b 61 73 65 20 27 23 27 3a 20 7b 0a 20 20 20 20 20  ase '#': {.     
3302c 20 66 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74 65   for(i=1; sqlite
3302d 33 49 73 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20  3Isdigit(z[i]); 
3302e 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 69 66 28  i++){}.      if(
3302f 20 69 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20   i>1 ){.        
33030 2f 2a 20 50 61 72 61 6d 65 74 65 72 73 20 6f 66  /* Parameters of
33031 20 74 68 65 20 66 6f 72 6d 20 23 4e 4e 4e 20 28   the form #NNN (
33032 77 68 65 72 65 20 4e 4e 4e 20 69 73 20 61 20 6e  where NNN is a n
33033 75 6d 62 65 72 29 20 61 72 65 20 75 73 65 64 0a  umber) are used.
33034 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 72          ** inter
33035 6e 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33  nally by sqlite3
33036 4e 65 73 74 65 64 50 61 72 73 65 2e 20 20 2a 2f  NestedParse.  */
33037 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
33038 79 70 65 20 3d 20 54 4b 5f 52 45 47 49 53 54 45  ype = TK_REGISTE
33039 52 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  R;.        retur
3303a 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n i;.      }.   
3303b 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75     /* Fall throu
3303c 67 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74  gh into the next
3303d 20 63 61 73 65 20 69 66 20 74 68 65 20 27 23 27   case if the '#'
3303e 20 69 73 20 6e 6f 74 20 66 6f 6c 6c 6f 77 65 64   is not followed
3303f 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 61 20 64   by.      ** a d
33040 69 67 69 74 2e 20 54 72 79 20 74 6f 20 6d 61 74  igit. Try to mat
33041 63 68 20 23 41 41 41 41 20 77 68 65 72 65 20 41  ch #AAAA where A
33042 41 41 41 20 69 73 20 61 20 70 61 72 61 6d 65 74  AAA is a paramet
33043 65 72 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20  er name. */.    
33044 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
33045 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42  _OMIT_TCL_VARIAB
33046 4c 45 0a 20 20 20 20 63 61 73 65 20 27 24 27 3a  LE.    case '$':
33047 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
33048 20 27 40 27 3a 20 20 2f 2a 20 46 6f 72 20 63 6f   '@':  /* For co
33049 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68  mpatibility with
3304a 20 4d 53 20 53 51 4c 20 53 65 72 76 65 72 20 2a   MS SQL Server *
3304b 2f 0a 20 20 20 20 63 61 73 65 20 27 3a 27 3a 20  /.    case ':': 
3304c 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  {.      int n = 
3304d 30 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  0;.      testcas
3304e 65 28 20 7a 5b 30 5d 3d 3d 27 24 27 20 29 3b 20  e( z[0]=='$' ); 
3304f 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
33050 3d 27 40 27 20 29 3b 20 20 74 65 73 74 63 61 73  ='@' );  testcas
33051 65 28 20 7a 5b 30 5d 3d 3d 27 3a 27 20 29 3b 0a  e( z[0]==':' );.
33052 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
33053 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c 45 3b 0a   = TK_VARIABLE;.
33054 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 28        for(i=1; (
33055 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  c=z[i])!=0; i++)
33056 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 64  {.        if( Id
33057 43 68 61 72 28 63 29 20 29 7b 0a 20 20 20 20 20  Char(c) ){.     
33058 20 20 20 20 20 6e 2b 2b 3b 0a 23 69 66 6e 64 65       n++;.#ifnde
33059 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43  f SQLITE_OMIT_TC
3305a 4c 5f 56 41 52 49 41 42 4c 45 0a 20 20 20 20 20  L_VARIABLE.     
3305b 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d     }else if( c==
3305c 27 28 27 20 26 26 20 6e 3e 30 20 29 7b 0a 20 20  '(' && n>0 ){.  
3305d 20 20 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20          do{.    
3305e 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
3305f 20 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 28         }while( (
33060 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20 21 73  c=z[i])!=0 && !s
33061 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 63 29  qlite3Isspace(c)
33062 20 26 26 20 63 21 3d 27 29 27 20 29 3b 0a 20 20   && c!=')' );.  
33063 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27          if( c=='
33064 29 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  )' ){.          
33065 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
33066 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
33067 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
33068 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
33069 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3306a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3306b 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27    }else if( c=='
3306c 3a 27 20 26 26 20 7a 5b 69 2b 31 5d 3d 3d 27 3a  :' && z[i+1]==':
3306d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ' ){.          i
3306e 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ++;.#endif.     
3306f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
33070 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
33071 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
33072 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20 2a 74     if( n==0 ) *t
33073 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c  okenType = TK_IL
33074 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 72 65 74  LEGAL;.      ret
33075 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 23 69 66  urn i;.    }.#if
33076 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
33077 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20  _BLOB_LITERAL.  
33078 20 20 63 61 73 65 20 27 78 27 3a 20 63 61 73 65    case 'x': case
33079 20 27 58 27 3a 20 7b 0a 20 20 20 20 20 20 74 65   'X': {.      te
3307a 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 78  stcase( z[0]=='x
3307b 27 20 29 3b 20 74 65 73 74 63 61 73 65 28 20 7a  ' ); testcase( z
3307c 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20  [0]=='X' );.    
3307d 20 20 69 66 28 20 7a 5b 31 5d 3d 3d 27 5c 27 27    if( z[1]=='\''
3307e 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
3307f 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 4c 4f 42  enType = TK_BLOB
33080 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
33081 32 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26  2; (c=z[i])!=0 &
33082 26 20 63 21 3d 27 5c 27 27 3b 20 69 2b 2b 29 7b  & c!='\''; i++){
33083 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
33084 73 71 6c 69 74 65 33 49 73 78 64 69 67 69 74 28  sqlite3Isxdigit(
33085 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  c) ){.          
33086 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
33087 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20  K_ILLEGAL;.     
33088 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
33089 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 25 32  .        if( i%2
3308a 20 7c 7c 20 21 63 20 29 20 2a 74 6f 6b 65 6e 54   || !c ) *tokenT
3308b 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c  ype = TK_ILLEGAL
3308c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 20  ;.        if( c 
3308d 29 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 72  ) i++;.        r
3308e 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d  eturn i;.      }
3308f 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77  .      /* Otherw
33090 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  ise fall through
33091 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73   to the next cas
33092 65 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69  e */.    }.#endi
33093 66 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  f.    default: {
33094 0a 20 20 20 20 20 20 69 66 28 20 21 49 64 43 68  .      if( !IdCh
33095 61 72 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20  ar(*z) ){.      
33096 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
33097 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  .      for(i=1; 
33098 49 64 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b  IdChar(z[i]); i+
33099 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65  +){}.      *toke
3309a 6e 54 79 70 65 20 3d 20 6b 65 79 77 6f 72 64 43  nType = keywordC
3309b 6f 64 65 28 28 63 68 61 72 2a 29 7a 2c 20 69 29  ode((char*)z, i)
3309c 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ;.      return i
3309d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74  ;.    }.  }.  *t
3309e 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c  okenType = TK_IL
3309f 4c 45 47 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20  LEGAL;.  return 
330a0 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20  1;.}../*.** Run 
330a1 74 68 65 20 70 61 72 73 65 72 20 6f 6e 20 74 68  the parser on th
330a2 65 20 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69  e given SQL stri
330a3 6e 67 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  ng.  The parser 
330a4 73 74 72 75 63 74 75 72 65 20 69 73 0a 2a 2a 20  structure is.** 
330a5 70 61 73 73 65 64 20 69 6e 2e 20 20 41 6e 20 53  passed in.  An S
330a6 51 4c 49 54 45 5f 20 73 74 61 74 75 73 20 63 6f  QLITE_ status co
330a7 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  de is returned. 
330a8 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
330a9 75 72 73 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 61  urs.** then an a
330aa 6e 64 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  nd attempt is ma
330ab 64 65 20 74 6f 20 77 72 69 74 65 20 61 6e 20 65  de to write an e
330ac 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74  rror message int
330ad 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6f 62 74  o .** memory obt
330ae 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
330af 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  e3_malloc() and 
330b0 74 6f 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73  to make *pzErrMs
330b1 67 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a  g point to that.
330b2 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ** error message
330b3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
330b4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
330b5 75 6e 50 61 72 73 65 72 28 50 61 72 73 65 20 2a  unParser(Parse *
330b6 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
330b7 61 72 20 2a 7a 53 71 6c 2c 20 63 68 61 72 20 2a  ar *zSql, char *
330b8 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e  *pzErrMsg){.  in
330b9 74 20 6e 45 72 72 20 3d 20 30 3b 20 20 20 20 20  t nErr = 0;     
330ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
330bb 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   Number of error
330bc 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f  s encountered */
330bd 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
330be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
330bf 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
330c0 65 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45  er */.  void *pE
330c1 6e 67 69 6e 65 3b 20 20 20 20 20 20 20 20 20 20  ngine;          
330c2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c          /* The L
330c3 45 4d 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 4c  EMON-generated L
330c4 41 4c 52 28 31 29 20 70 61 72 73 65 72 20 2a 2f  ALR(1) parser */
330c5 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 54 79 70 65  .  int tokenType
330c6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
330c7 20 20 20 2f 2a 20 74 79 70 65 20 6f 66 20 74 68     /* type of th
330c8 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a  e next token */.
330c9 20 20 69 6e 74 20 6c 61 73 74 54 6f 6b 65 6e 50    int lastTokenP
330ca 61 72 73 65 64 20 3d 20 2d 31 3b 20 20 20 20 20  arsed = -1;     
330cb 20 20 2f 2a 20 74 79 70 65 20 6f 66 20 74 68 65    /* type of the
330cc 20 70 72 65 76 69 6f 75 73 20 74 6f 6b 65 6e 20   previous token 
330cd 2a 2f 0a 20 20 75 38 20 65 6e 61 62 6c 65 4c 6f  */.  u8 enableLo
330ce 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20  okaside;        
330cf 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61       /* Saved va
330d0 6c 75 65 20 6f 66 20 64 62 2d 3e 6c 6f 6f 6b 61  lue of db->looka
330d1 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 2a 2f  side.bEnabled */
330d2 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
330d3 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20   pParse->db;    
330d4 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
330d5 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
330d6 0a 20 20 69 6e 74 20 6d 78 53 71 6c 4c 65 6e 3b  .  int mxSqlLen;
330d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
330d8 20 20 20 2f 2a 20 4d 61 78 20 6c 65 6e 67 74 68     /* Max length
330d9 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 72 69 6e   of an SQL strin
330da 67 20 2a 2f 0a 0a 0a 20 20 6d 78 53 71 6c 4c 65  g */...  mxSqlLe
330db 6e 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  n = db->aLimit[S
330dc 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f  QLITE_LIMIT_SQL_
330dd 4c 45 4e 47 54 48 5d 3b 0a 20 20 69 66 28 20 64  LENGTH];.  if( d
330de 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
330df 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 75  ==0 ){.    db->u
330e0 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
330e1 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73  = 0;.  }.  pPars
330e2 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  e->rc = SQLITE_O
330e3 4b 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 54 61  K;.  pParse->zTa
330e4 69 6c 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 20 3d  il = zSql;.  i =
330e5 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 7a   0;.  assert( pz
330e6 45 72 72 4d 73 67 21 3d 30 20 29 3b 0a 20 20 70  ErrMsg!=0 );.  p
330e7 45 6e 67 69 6e 65 20 3d 20 73 71 6c 69 74 65 33  Engine = sqlite3
330e8 50 61 72 73 65 72 41 6c 6c 6f 63 28 28 76 6f 69  ParserAlloc((voi
330e9 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29 73 71  d*(*)(size_t))sq
330ea 6c 69 74 65 33 4d 61 6c 6c 6f 63 29 3b 0a 20 20  lite3Malloc);.  
330eb 69 66 28 20 70 45 6e 67 69 6e 65 3d 3d 30 20 29  if( pEngine==0 )
330ec 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
330ed 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
330ee 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
330ef 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  MEM;.  }.  asser
330f0 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  t( pParse->pNewT
330f1 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73  able==0 );.  ass
330f2 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65  ert( pParse->pNe
330f3 77 54 72 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20  wTrigger==0 );. 
330f4 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
330f5 3e 6e 56 61 72 3d 3d 30 20 29 3b 0a 20 20 61 73  >nVar==0 );.  as
330f6 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56  sert( pParse->nV
330f7 61 72 45 78 70 72 3d 3d 30 20 29 3b 0a 20 20 61  arExpr==0 );.  a
330f8 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e  ssert( pParse->n
330f9 56 61 72 45 78 70 72 41 6c 6c 6f 63 3d 3d 30 20  VarExprAlloc==0 
330fa 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
330fb 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 3d 3d  rse->apVarExpr==
330fc 30 20 29 3b 0a 20 20 65 6e 61 62 6c 65 4c 6f 6f  0 );.  enableLoo
330fd 6b 61 73 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f  kaside = db->loo
330fe 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b  kaside.bEnabled;
330ff 0a 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61  .  if( db->looka
33100 73 69 64 65 2e 70 53 74 61 72 74 20 29 20 64 62  side.pStart ) db
33101 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
33102 62 6c 65 64 20 3d 20 31 3b 0a 20 20 77 68 69 6c  bled = 1;.  whil
33103 65 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  e( !db->mallocFa
33104 69 6c 65 64 20 26 26 20 7a 53 71 6c 5b 69 5d 21  iled && zSql[i]!
33105 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
33106 28 20 69 3e 3d 30 20 29 3b 0a 20 20 20 20 70 50  ( i>=0 );.    pP
33107 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
33108 2e 7a 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20  .z = &zSql[i];. 
33109 20 20 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74     pParse->sLast
3310a 54 6f 6b 65 6e 2e 6e 20 3d 20 73 71 6c 69 74 65  Token.n = sqlite
3310b 33 47 65 74 54 6f 6b 65 6e 28 28 75 6e 73 69 67  3GetToken((unsig
3310c 6e 65 64 20 63 68 61 72 2a 29 26 7a 53 71 6c 5b  ned char*)&zSql[
3310d 69 5d 2c 26 74 6f 6b 65 6e 54 79 70 65 29 3b 0a  i],&tokenType);.
3310e 20 20 20 20 69 20 2b 3d 20 70 50 61 72 73 65 2d      i += pParse-
3310f 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 20  >sLastToken.n;. 
33110 20 20 20 69 66 28 20 69 3e 6d 78 53 71 6c 4c 65     if( i>mxSqlLe
33111 6e 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  n ){.      pPars
33112 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 54  e->rc = SQLITE_T
33113 4f 4f 42 49 47 3b 0a 20 20 20 20 20 20 62 72 65  OOBIG;.      bre
33114 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 77  ak;.    }.    sw
33115 69 74 63 68 28 20 74 6f 6b 65 6e 54 79 70 65 20  itch( tokenType 
33116 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b  ){.      case TK
33117 5f 53 50 41 43 45 3a 20 7b 0a 20 20 20 20 20 20  _SPACE: {.      
33118 20 20 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49    if( db->u1.isI
33119 6e 74 65 72 72 75 70 74 65 64 20 29 7b 0a 20 20  nterrupted ){.  
3311a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
3311b 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
3311c 22 69 6e 74 65 72 72 75 70 74 22 29 3b 0a 20 20  "interrupt");.  
3311d 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
3311e 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  rc = SQLITE_INTE
3311f 52 52 55 50 54 3b 0a 20 20 20 20 20 20 20 20 20  RRUPT;.         
33120 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72 73   goto abort_pars
33121 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
33122 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33123 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54    }.      case T
33124 4b 5f 49 4c 4c 45 47 41 4c 3a 20 7b 0a 20 20 20  K_ILLEGAL: {.   
33125 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
33126 65 65 28 64 62 2c 20 2a 70 7a 45 72 72 4d 73 67  ee(db, *pzErrMsg
33127 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a 45 72  );.        *pzEr
33128 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50  rMsg = sqlite3MP
33129 72 69 6e 74 66 28 64 62 2c 20 22 75 6e 72 65 63  rintf(db, "unrec
3312a 6f 67 6e 69 7a 65 64 20 74 6f 6b 65 6e 3a 20 5c  ognized token: \
3312b 22 25 54 5c 22 22 2c 0a 20 20 20 20 20 20 20 20  "%T\"",.        
3312c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3312d 26 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f  &pParse->sLastTo
3312e 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 45  ken);.        nE
3312f 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 67 6f  rr++;.        go
33130 74 6f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a  to abort_parse;.
33131 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
33132 73 65 20 54 4b 5f 53 45 4d 49 3a 20 7b 0a 20 20  se TK_SEMI: {.  
33133 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 54        pParse->zT
33134 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a  ail = &zSql[i];.
33135 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20          /* Fall 
33136 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 64 65  thru into the de
33137 66 61 75 6c 74 20 63 61 73 65 20 2a 2f 0a 20 20  fault case */.  
33138 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61      }.      defa
33139 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 73  ult: {.        s
3313a 71 6c 69 74 65 33 50 61 72 73 65 72 28 70 45 6e  qlite3Parser(pEn
3313b 67 69 6e 65 2c 20 74 6f 6b 65 6e 54 79 70 65 2c  gine, tokenType,
3313c 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
3313d 6b 65 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20 20  ken, pParse);.  
3313e 20 20 20 20 20 20 6c 61 73 74 54 6f 6b 65 6e 50        lastTokenP
3313f 61 72 73 65 64 20 3d 20 74 6f 6b 65 6e 54 79 70  arsed = tokenTyp
33140 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  e;.        if( p
33141 50 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54  Parse->rc!=SQLIT
33142 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
33143 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72    goto abort_par
33144 73 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  se;.        }.  
33145 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
33146 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 61     }.    }.  }.a
33147 62 6f 72 74 5f 70 61 72 73 65 3a 0a 20 20 69 66  bort_parse:.  if
33148 28 20 7a 53 71 6c 5b 69 5d 3d 3d 30 20 26 26 20  ( zSql[i]==0 && 
33149 6e 45 72 72 3d 3d 30 20 26 26 20 70 50 61 72 73  nErr==0 && pPars
3314a 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e->rc==SQLITE_OK
3314b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 61 73 74   ){.    if( last
3314c 54 6f 6b 65 6e 50 61 72 73 65 64 21 3d 54 4b 5f  TokenParsed!=TK_
3314d 53 45 4d 49 20 29 7b 0a 20 20 20 20 20 20 73 71  SEMI ){.      sq
3314e 6c 69 74 65 33 50 61 72 73 65 72 28 70 45 6e 67  lite3Parser(pEng
3314f 69 6e 65 2c 20 54 4b 5f 53 45 4d 49 2c 20 70 50  ine, TK_SEMI, pP
33150 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
33151 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  , pParse);.     
33152 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d   pParse->zTail =
33153 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 7d   &zSql[i];.    }
33154 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73  .    sqlite3Pars
33155 65 72 28 70 45 6e 67 69 6e 65 2c 20 30 2c 20 70  er(pEngine, 0, p
33156 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
33157 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a  n, pParse);.  }.
33158 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41  #ifdef YYTRACKMA
33159 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20 73 71  XSTACKDEPTH.  sq
3315a 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53  lite3StatusSet(S
3315b 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52  QLITE_STATUS_PAR
3315c 53 45 52 5f 53 54 41 43 4b 2c 0a 20 20 20 20 20  SER_STACK,.     
3315d 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 53 74   sqlite3ParserSt
3315e 61 63 6b 50 65 61 6b 28 70 45 6e 67 69 6e 65 29  ackPeak(pEngine)
3315f 0a 20 20 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  .  );.#endif /* 
33160 59 59 44 45 42 55 47 20 2a 2f 0a 20 20 73 71 6c  YYDEBUG */.  sql
33161 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28 70  ite3ParserFree(p
33162 45 6e 67 69 6e 65 2c 20 73 71 6c 69 74 65 33 5f  Engine, sqlite3_
33163 66 72 65 65 29 3b 0a 20 20 64 62 2d 3e 6c 6f 6f  free);.  db->loo
33164 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
33165 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64  = enableLookasid
33166 65 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  e;.  if( db->mal
33167 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
33168 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
33169 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
3316a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63    if( pParse->rc
3316b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  !=SQLITE_OK && p
3316c 50 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54  Parse->rc!=SQLIT
3316d 45 5f 44 4f 4e 45 20 26 26 20 70 50 61 72 73 65  E_DONE && pParse
3316e 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29 7b 0a  ->zErrMsg==0 ){.
3316f 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
33170 72 69 6e 67 28 26 70 50 61 72 73 65 2d 3e 7a 45  ring(&pParse->zE
33171 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c  rrMsg, db, "%s",
33172 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 70   sqlite3ErrStr(p
33173 50 61 72 73 65 2d 3e 72 63 29 29 3b 0a 20 20 7d  Parse->rc));.  }
33174 0a 20 20 61 73 73 65 72 74 28 20 70 7a 45 72 72  .  assert( pzErr
33175 4d 73 67 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  Msg!=0 );.  if( 
33176 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
33177 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73 67  ){.    *pzErrMsg
33178 20 3d 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d   = pParse->zErrM
33179 73 67 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  sg;.    pParse->
3317a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
3317b 20 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 69   nErr++;.  }.  i
3317c 66 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65  f( pParse->pVdbe
3317d 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   && pParse->nErr
3317e 3e 30 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65  >0 && pParse->ne
3317f 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73  sted==0 ){.    s
33180 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
33181 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b  (pParse->pVdbe);
33182 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 56 64  .    pParse->pVd
33183 62 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e  be = 0;.  }.#ifn
33184 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
33185 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69  SHARED_CACHE.  i
33186 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  f( pParse->neste
33187 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  d==0 ){.    sqli
33188 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50  te3DbFree(db, pP
33189 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  arse->aTableLock
3318a 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61  );.    pParse->a
3318b 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20  TableLock = 0;. 
3318c 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c     pParse->nTabl
3318d 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 23  eLock = 0;.  }.#
3318e 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
3318f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
33190 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33  LTABLE.  sqlite3
33191 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73  DbFree(db, pPars
33192 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 29 3b 0a  e->apVtabLock);.
33193 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 21 49  #endif..  if( !I
33194 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
33195 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
33196 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56  pParse->declareV
33197 74 61 62 20 66 6c 61 67 20 69 73 20 73 65 74 2c  tab flag is set,
33198 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 61   do not delete a
33199 6e 79 20 74 61 62 6c 65 20 0a 20 20 20 20 2a 2a  ny table .    **
3319a 20 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74   structure built
3319b 20 75 70 20 69 6e 20 70 50 61 72 73 65 2d 3e 70   up in pParse->p
3319c 4e 65 77 54 61 62 6c 65 2e 20 54 68 65 20 63 61  NewTable. The ca
3319d 6c 6c 69 6e 67 20 63 6f 64 65 20 28 73 65 65 20  lling code (see 
3319e 76 74 61 62 2e 63 29 0a 20 20 20 20 2a 2a 20 77  vtab.c).    ** w
3319f 69 6c 6c 20 74 61 6b 65 20 72 65 73 70 6f 6e 73  ill take respons
331a0 69 62 69 6c 69 74 79 20 66 6f 72 20 66 72 65 65  ibility for free
331a1 69 6e 67 20 74 68 65 20 54 61 62 6c 65 20 73 74  ing the Table st
331a2 72 75 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a  ructure..    */.
331a3 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
331a4 65 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70  eTable(pParse->p
331a5 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 0a  NewTable);.  }..
331a6 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
331a7 72 69 67 67 65 72 28 64 62 2c 20 70 50 61 72 73  rigger(db, pPars
331a8 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 29 3b  e->pNewTrigger);
331a9 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
331aa 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56  (db, pParse->apV
331ab 61 72 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74  arExpr);.  sqlit
331ac 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61  e3DbFree(db, pPa
331ad 72 73 65 2d 3e 61 41 6c 69 61 73 29 3b 0a 20 20  rse->aAlias);.  
331ae 77 68 69 6c 65 28 20 70 50 61 72 73 65 2d 3e 70  while( pParse->p
331af 41 69 6e 63 20 29 7b 0a 20 20 20 20 41 75 74 6f  Ainc ){.    Auto
331b0 69 6e 63 49 6e 66 6f 20 2a 70 20 3d 20 70 50 61  incInfo *p = pPa
331b1 72 73 65 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20  rse->pAinc;.    
331b2 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 20 3d 20  pParse->pAinc = 
331b3 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71  p->pNext;.    sq
331b4 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
331b5 70 29 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28  p);.  }.  while(
331b6 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65   pParse->pZombie
331b7 54 61 62 20 29 7b 0a 20 20 20 20 54 61 62 6c 65  Tab ){.    Table
331b8 20 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 5a   *p = pParse->pZ
331b9 6f 6d 62 69 65 54 61 62 3b 0a 20 20 20 20 70 50  ombieTab;.    pP
331ba 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62  arse->pZombieTab
331bb 20 3d 20 70 2d 3e 70 4e 65 78 74 5a 6f 6d 62 69   = p->pNextZombi
331bc 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  e;.    sqlite3De
331bd 6c 65 74 65 54 61 62 6c 65 28 70 29 3b 0a 20 20  leteTable(p);.  
331be 7d 0a 20 20 69 66 28 20 6e 45 72 72 3e 30 20 26  }.  if( nErr>0 &
331bf 26 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51  & pParse->rc==SQ
331c0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
331c1 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
331c2 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
331c3 72 65 74 75 72 6e 20 6e 45 72 72 3b 0a 7d 0a 0a  return nErr;.}..
331c4 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
331c5 45 6e 64 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e  End of tokenize.
331c6 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
331c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
331c9 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
331ca 42 65 67 69 6e 20 66 69 6c 65 20 63 6f 6d 70 6c  Begin file compl
331cb 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ete.c **********
331cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
331ce 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
331cf 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
331d0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
331d1 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
331d2 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
331d3 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
331d4 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
331d5 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
331d6 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
331d7 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
331d8 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
331d9 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
331da 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
331db 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
331dc 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
331dd 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
331de 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
331df 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
331e0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
331e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331e5 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20 74 6f 6b  ******.** An tok
331e6 65 6e 69 7a 65 72 20 66 6f 72 20 53 51 4c 0a 2a  enizer for SQL.*
331e7 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
331e8 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 74  ontains C code t
331e9 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
331ea 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  he sqlite3_compl
331eb 65 74 65 28 29 20 41 50 49 2e 0a 2a 2a 20 54 68  ete() API..** Th
331ec 69 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  is code used to 
331ed 62 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 74  be part of the t
331ee 6f 6b 65 6e 69 7a 65 72 2e 63 20 73 6f 75 72 63  okenizer.c sourc
331ef 65 20 66 69 6c 65 2e 20 20 42 75 74 20 62 79 0a  e file.  But by.
331f0 2a 2a 20 73 65 70 61 72 61 74 69 6e 67 20 69 74  ** separating it
331f1 20 6f 75 74 2c 20 74 68 65 20 63 6f 64 65 20 77   out, the code w
331f2 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63  ill be automatic
331f3 61 6c 6c 79 20 6f 6d 69 74 74 65 64 20 66 72 6f  ally omitted fro
331f4 6d 0a 2a 2a 20 73 74 61 74 69 63 20 6c 69 6e 6b  m.** static link
331f5 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 75 73  s that do not us
331f6 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  e it..**.** $Id:
331f7 20 63 6f 6d 70 6c 65 74 65 2e 63 2c 76 20 31 2e   complete.c,v 1.
331f8 38 20 32 30 30 39 2f 30 34 2f 32 38 20 30 34 3a  8 2009/04/28 04:
331f9 34 36 3a 34 32 20 64 72 68 20 45 78 70 20 24 0a  46:42 drh Exp $.
331fa 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
331fb 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a  E_OMIT_COMPLETE.
331fc 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 64  ./*.** This is d
331fd 65 66 69 6e 65 64 20 69 6e 20 74 6f 6b 65 6e 69  efined in tokeni
331fe 7a 65 2e 63 2e 20 20 57 65 20 6a 75 73 74 20 68  ze.c.  We just h
331ff 61 76 65 20 74 6f 20 69 6d 70 6f 72 74 20 74 68  ave to import th
33200 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f  e definition..*/
33201 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
33202 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 23 69 66  AMALGAMATION.#if
33203 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49  def SQLITE_ASCII
33204 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
33205 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74  const char sqlit
33206 65 33 49 73 41 73 63 69 69 49 64 43 68 61 72 5b  e3IsAsciiIdChar[
33207 5d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61  ];.#define IdCha
33208 72 28 43 29 20 20 28 28 28 63 3d 43 29 26 30 78  r(C)  (((c=C)&0x
33209 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e 30 78 31  80)!=0 || (c>0x1
3320a 66 20 26 26 20 73 71 6c 69 74 65 33 49 73 41 73  f && sqlite3IsAs
3320b 63 69 69 49 64 43 68 61 72 5b 63 2d 30 78 32 30  ciiIdChar[c-0x20
3320c 5d 29 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  ])).#endif.#ifde
3320d 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a  f SQLITE_EBCDIC.
3320e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
3320f 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65  onst char sqlite
33210 33 49 73 45 62 63 64 69 63 49 64 43 68 61 72 5b  3IsEbcdicIdChar[
33211 5d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61  ];.#define IdCha
33212 72 28 43 29 20 20 28 28 28 63 3d 43 29 3e 3d 30  r(C)  (((c=C)>=0
33213 78 34 32 20 26 26 20 73 71 6c 69 74 65 33 49 73  x42 && sqlite3Is
33214 45 62 63 64 69 63 49 64 43 68 61 72 5b 63 2d 30  EbcdicIdChar[c-0
33215 78 34 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23 65  x40])).#endif.#e
33216 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41  ndif /* SQLITE_A
33217 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a  MALGAMATION */..
33218 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e 20 74 79 70  ./*.** Token typ
33219 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73  es used by the s
3321a 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
3321b 29 20 72 6f 75 74 69 6e 65 2e 20 20 53 65 65 20  ) routine.  See 
3321c 74 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 63 6f  the header.** co
3321d 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 61 74 20 70  mments on that p
3321e 72 6f 63 65 64 75 72 65 20 66 6f 72 20 61 64 64  rocedure for add
3321f 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
33220 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ion..*/.#define 
33221 74 6b 53 45 4d 49 20 20 20 20 30 0a 23 64 65 66  tkSEMI    0.#def
33222 69 6e 65 20 74 6b 57 53 20 20 20 20 20 20 31 0a  ine tkWS      1.
33223 23 64 65 66 69 6e 65 20 74 6b 4f 54 48 45 52 20  #define tkOTHER 
33224 20 20 32 0a 23 64 65 66 69 6e 65 20 74 6b 45 58    2.#define tkEX
33225 50 4c 41 49 4e 20 33 0a 23 64 65 66 69 6e 65 20  PLAIN 3.#define 
33226 74 6b 43 52 45 41 54 45 20 20 34 0a 23 64 65 66  tkCREATE  4.#def
33227 69 6e 65 20 74 6b 54 45 4d 50 20 20 20 20 35 0a  ine tkTEMP    5.
33228 23 64 65 66 69 6e 65 20 74 6b 54 52 49 47 47 45  #define tkTRIGGE
33229 52 20 36 0a 23 64 65 66 69 6e 65 20 74 6b 45 4e  R 6.#define tkEN
3322a 44 20 20 20 20 20 37 0a 0a 2f 2a 0a 2a 2a 20 52  D     7../*.** R
3322b 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
3322c 65 20 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69  e given SQL stri
3322d 6e 67 20 65 6e 64 73 20 69 6e 20 61 20 73 65 6d  ng ends in a sem
3322e 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 70  icolon..**.** Sp
3322f 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 20 69  ecial handling i
33230 73 20 72 65 71 75 69 72 65 20 66 6f 72 20 43 52  s require for CR
33231 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61  EATE TRIGGER sta
33232 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 57 68 65 6e  tements..** When
33233 65 76 65 72 20 74 68 65 20 43 52 45 41 54 45 20  ever the CREATE 
33234 54 52 49 47 47 45 52 20 6b 65 79 77 6f 72 64 73  TRIGGER keywords
33235 20 61 72 65 20 73 65 65 6e 2c 20 74 68 65 20 73   are seen, the s
33236 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6d 75 73 74  tatement.** must
33237 20 65 6e 64 20 77 69 74 68 20 22 3b 45 4e 44 3b   end with ";END;
33238 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d  "..**.** This im
33239 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65  plementation use
3323a 73 20 61 20 73 74 61 74 65 20 6d 61 63 68 69 6e  s a state machin
3323b 65 20 77 69 74 68 20 37 20 73 74 61 74 65 73 3a  e with 7 states:
3323c 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29 20 53 54 41  .**.**   (0) STA
3323d 52 54 20 20 20 20 20 41 74 20 74 68 65 20 62 65  RT     At the be
3323e 67 69 6e 6e 69 6e 67 20 6f 72 20 65 6e 64 20 6f  ginning or end o
3323f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  f an SQL stateme
33240 6e 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  nt.  This routin
33241 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
33242 20 20 20 20 20 72 65 74 75 72 6e 73 20 31 20 69       returns 1 i
33243 66 20 69 74 20 65 6e 64 73 20 69 6e 20 74 68 65  f it ends in the
33244 20 53 54 41 52 54 20 73 74 61 74 65 20 61 6e 64   START state and
33245 20 30 20 69 66 20 69 74 20 65 6e 64 73 0a 2a 2a   0 if it ends.**
33246 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33247 20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 73 74   in any other st
33248 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29  ate..**.**   (1)
33249 20 4e 4f 52 4d 41 4c 20 20 20 20 57 65 20 61 72   NORMAL    We ar
3324a 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  e in the middle 
3324b 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 77 68 69  of statement whi
3324c 63 68 20 65 6e 64 73 20 77 69 74 68 20 61 20 73  ch ends with a s
3324d 69 6e 67 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20  ingle.**        
3324e 20 20 20 20 20 20 20 20 20 73 65 6d 69 63 6f 6c           semicol
3324f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20  on..**.**   (2) 
33250 45 58 50 4c 41 49 4e 20 20 20 54 68 65 20 6b 65  EXPLAIN   The ke
33251 79 77 6f 72 64 20 45 58 50 4c 41 49 4e 20 68 61  yword EXPLAIN ha
33252 73 20 62 65 65 6e 20 73 65 65 6e 20 61 74 20 74  s been seen at t
33253 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
33254 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
33255 20 20 20 20 61 20 73 74 61 74 65 6d 65 6e 74 2e      a statement.
33256 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20 43 52 45  .**.**   (3) CRE
33257 41 54 45 20 20 20 20 54 68 65 20 6b 65 79 77 6f  ATE    The keywo
33258 72 64 20 43 52 45 41 54 45 20 68 61 73 20 62 65  rd CREATE has be
33259 65 6e 20 73 65 65 6e 20 61 74 20 74 68 65 20 62  en seen at the b
3325a 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 0a 2a 2a  eginning of a.**
3325b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3325c 20 73 74 61 74 65 6d 65 6e 74 2c 20 70 6f 73 73   statement, poss
3325d 69 62 6c 79 20 70 72 65 63 65 65 64 65 64 20 62  ibly preceeded b
3325e 79 20 45 58 50 4c 41 49 4e 20 61 6e 64 2f 6f 72  y EXPLAIN and/or
3325f 20 66 6f 6c 6c 6f 77 65 64 20 62 79 0a 2a 2a 20   followed by.** 
33260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33261 54 45 4d 50 20 6f 72 20 54 45 4d 50 4f 52 41 52  TEMP or TEMPORAR
33262 59 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 54 52  Y.**.**   (4) TR
33263 49 47 47 45 52 20 20 20 57 65 20 61 72 65 20 69  IGGER   We are i
33264 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
33265 61 20 74 72 69 67 67 65 72 20 64 65 66 69 6e 69  a trigger defini
33266 74 69 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62  tion that must b
33267 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
33268 20 20 20 20 20 65 6e 64 65 64 20 62 79 20 61 20       ended by a 
33269 73 65 6d 69 63 6f 6c 6f 6e 2c 20 74 68 65 20 6b  semicolon, the k
3326a 65 79 77 6f 72 64 20 45 4e 44 2c 20 61 6e 64 20  eyword END, and 
3326b 61 6e 6f 74 68 65 72 20 73 65 6d 69 63 6f 6c 6f  another semicolo
3326c 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 53  n..**.**   (5) S
3326d 45 4d 49 20 20 20 20 20 20 57 65 27 76 65 20 73  EMI      We've s
3326e 65 65 6e 20 74 68 65 20 66 69 72 73 74 20 73 65  een the first se
3326f 6d 69 63 6f 6c 6f 6e 20 69 6e 20 74 68 65 20 22  micolon in the "
33270 3b 45 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75  ;END;" that occu
33271 72 73 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20  rs at.**        
33272 20 20 20 20 20 20 20 20 20 74 68 65 20 65 6e 64           the end
33273 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 64 65   of a trigger de
33274 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  finition..**.** 
33275 20 20 28 36 29 20 45 4e 44 20 20 20 20 20 20 20    (6) END       
33276 57 65 27 76 65 20 73 65 65 6e 20 74 68 65 20 22  We've seen the "
33277 3b 45 4e 44 22 20 6f 66 20 74 68 65 20 22 3b 45  ;END" of the ";E
33278 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75 72 73  ND;" that occurs
33279 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 20   at the end.**  
3327a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
3327b 66 20 61 20 74 72 69 67 67 65 72 20 64 69 66 69  f a trigger difi
3327c 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 72  nition..**.** Tr
3327d 61 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65  ansitions betwee
3327e 6e 20 73 74 61 74 65 73 20 61 62 6f 76 65 20 61  n states above a
3327f 72 65 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  re determined by
33280 20 74 6f 6b 65 6e 73 20 65 78 74 72 61 63 74 65   tokens extracte
33281 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e  d.** from the in
33282 70 75 74 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  put.  The follow
33283 69 6e 67 20 74 6f 6b 65 6e 73 20 61 72 65 20 73  ing tokens are s
33284 69 67 6e 69 66 69 63 61 6e 74 3a 0a 2a 2a 0a 2a  ignificant:.**.*
33285 2a 20 20 20 28 30 29 20 74 6b 53 45 4d 49 20 20  *   (0) tkSEMI  
33286 20 20 20 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 2e      A semicolon.
33287 0a 2a 2a 20 20 20 28 31 29 20 74 6b 57 53 20 20  .**   (1) tkWS  
33288 20 20 20 20 20 20 57 68 69 74 65 73 70 61 63 65        Whitespace
33289 0a 2a 2a 20 20 20 28 32 29 20 74 6b 4f 54 48 45  .**   (2) tkOTHE
3328a 52 20 20 20 20 20 41 6e 79 20 6f 74 68 65 72 20  R     Any other 
3328b 53 51 4c 20 74 6f 6b 65 6e 2e 0a 2a 2a 20 20 20  SQL token..**   
3328c 28 33 29 20 74 6b 45 58 50 4c 41 49 4e 20 20 20  (3) tkEXPLAIN   
3328d 54 68 65 20 22 65 78 70 6c 61 69 6e 22 20 6b 65  The "explain" ke
3328e 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 34 29 20  yword..**   (4) 
3328f 74 6b 43 52 45 41 54 45 20 20 20 20 54 68 65 20  tkCREATE    The 
33290 22 63 72 65 61 74 65 22 20 6b 65 79 77 6f 72 64  "create" keyword
33291 2e 0a 2a 2a 20 20 20 28 35 29 20 74 6b 54 45 4d  ..**   (5) tkTEM
33292 50 20 20 20 20 20 20 54 68 65 20 22 74 65 6d 70  P      The "temp
33293 22 20 6f 72 20 22 74 65 6d 70 6f 72 61 72 79 22  " or "temporary"
33294 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28   keyword..**   (
33295 36 29 20 74 6b 54 52 49 47 47 45 52 20 20 20 54  6) tkTRIGGER   T
33296 68 65 20 22 74 72 69 67 67 65 72 22 20 6b 65 79  he "trigger" key
33297 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 37 29 20 74  word..**   (7) t
33298 6b 45 4e 44 20 20 20 20 20 20 20 54 68 65 20 22  kEND       The "
33299 65 6e 64 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a  end" keyword..**
3329a 0a 2a 2a 20 57 68 69 74 65 73 70 61 63 65 20 6e  .** Whitespace n
3329b 65 76 65 72 20 63 61 75 73 65 73 20 61 20 73 74  ever causes a st
3329c 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 61  ate transition a
3329d 6e 64 20 69 73 20 61 6c 77 61 79 73 20 69 67 6e  nd is always ign
3329e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77  ored..**.** If w
3329f 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 53  e compile with S
332a0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
332a1 45 52 2c 20 61 6c 6c 20 6f 66 20 74 68 65 20 63  ER, all of the c
332a2 6f 6d 70 75 74 61 74 69 6f 6e 20 6e 65 65 64 65  omputation neede
332a3 64 0a 2a 2a 20 74 6f 20 72 65 63 6f 67 6e 69 7a  d.** to recogniz
332a4 65 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74  e the end of a t
332a5 72 69 67 67 65 72 20 63 61 6e 20 62 65 20 6f 6d  rigger can be om
332a6 69 74 74 65 64 2e 20 20 41 6c 6c 20 77 65 20 68  itted.  All we h
332a7 61 76 65 20 74 6f 20 64 6f 0a 2a 2a 20 69 73 20  ave to do.** is 
332a8 6c 6f 6f 6b 20 66 6f 72 20 61 20 73 65 6d 69 63  look for a semic
332a9 6f 6c 6f 6e 20 74 68 61 74 20 69 73 20 6e 6f 74  olon that is not
332aa 20 70 61 72 74 20 6f 66 20 61 6e 20 73 74 72 69   part of an stri
332ab 6e 67 20 6f 72 20 63 6f 6d 6d 65 6e 74 2e 0a 2a  ng or comment..*
332ac 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
332ad 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
332ae 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  e(const char *zS
332af 71 6c 29 7b 0a 20 20 75 38 20 73 74 61 74 65 20  ql){.  u8 state 
332b0 3d 20 30 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e  = 0;   /* Curren
332b1 74 20 73 74 61 74 65 2c 20 75 73 69 6e 67 20 6e  t state, using n
332b2 75 6d 62 65 72 73 20 64 65 66 69 6e 65 64 20 69  umbers defined i
332b3 6e 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74  n header comment
332b4 20 2a 2f 0a 20 20 75 38 20 74 6f 6b 65 6e 3b 20   */.  u8 token; 
332b5 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f        /* Value o
332b6 66 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  f the next token
332b7 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
332b8 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
332b9 0a 20 20 2f 2a 20 41 20 63 6f 6d 70 6c 65 78 20  .  /* A complex 
332ba 73 74 61 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e  statement machin
332bb 65 20 75 73 65 64 20 74 6f 20 64 65 74 65 63 74  e used to detect
332bc 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 43 52   the end of a CR
332bd 45 41 54 45 20 54 52 49 47 47 45 52 0a 20 20 2a  EATE TRIGGER.  *
332be 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  * statement.  Th
332bf 69 73 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c  is is the normal
332c0 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 73 74   case..  */.  st
332c1 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 74 72  atic const u8 tr
332c2 61 6e 73 5b 37 5d 5b 38 5d 20 3d 20 7b 0a 20 20  ans[7][8] = {.  
332c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332c4 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20     /* Token:    
332c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332c7 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
332c8 20 20 20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20      /* State:   
332c9 20 20 20 20 2a 2a 20 20 53 45 4d 49 20 20 57 53      **  SEMI  WS
332ca 20 20 4f 54 48 45 52 20 45 58 50 4c 41 49 4e 20    OTHER EXPLAIN 
332cb 20 43 52 45 41 54 45 20 20 54 45 4d 50 20 20 54   CREATE  TEMP  T
332cc 52 49 47 47 45 52 20 20 45 4e 44 20 20 2a 2f 0a  RIGGER  END  */.
332cd 20 20 20 20 20 2f 2a 20 30 20 20 20 53 54 41 52       /* 0   STAR
332ce 54 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 30  T: */ {    0,  0
332cf 2c 20 20 20 20 20 31 2c 20 20 20 20 20 20 32 2c  ,     1,      2,
332d0 20 20 20 20 20 20 33 2c 20 20 20 20 31 2c 20 20        3,    1,  
332d1 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c       1,   1,  },
332d2 0a 20 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d  .     /* 1  NORM
332d3 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20  AL: */ {    0,  
332d4 31 2c 20 20 20 20 20 31 2c 20 20 20 20 20 20 31  1,     1,      1
332d5 2c 20 20 20 20 20 20 31 2c 20 20 20 20 31 2c 20  ,      1,    1, 
332d6 20 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d        1,   1,  }
332d7 2c 0a 20 20 20 20 20 2f 2a 20 32 20 45 58 50 4c  ,.     /* 2 EXPL
332d8 41 49 4e 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20  AIN: */ {    0, 
332d9 20 32 2c 20 20 20 20 20 32 2c 20 20 20 20 20 20   2,     2,      
332da 31 2c 20 20 20 20 20 20 33 2c 20 20 20 20 31 2c  1,      3,    1,
332db 20 20 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20         1,   1,  
332dc 7d 2c 0a 20 20 20 20 20 2f 2a 20 33 20 20 43 52  },.     /* 3  CR
332dd 45 41 54 45 3a 20 2a 2f 20 7b 20 20 20 20 30 2c  EATE: */ {    0,
332de 20 20 33 2c 20 20 20 20 20 31 2c 20 20 20 20 20    3,     1,     
332df 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20 33   1,      1,    3
332e0 2c 20 20 20 20 20 20 20 34 2c 20 20 20 31 2c 20  ,       4,   1, 
332e1 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 34 20 54 52   },.     /* 4 TR
332e2 49 47 47 45 52 3a 20 2a 2f 20 7b 20 20 20 20 35  IGGER: */ {    5
332e3 2c 20 20 34 2c 20 20 20 20 20 34 2c 20 20 20 20  ,  4,     4,    
332e4 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20    4,      4,    
332e5 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20 34 2c  4,       4,   4,
332e6 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 35 20 20    },.     /* 5  
332e7 20 20 53 45 4d 49 3a 20 2a 2f 20 7b 20 20 20 20    SEMI: */ {    
332e8 35 2c 20 20 35 2c 20 20 20 20 20 34 2c 20 20 20  5,  5,     4,   
332e9 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20     4,      4,   
332ea 20 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20 36   4,       4,   6
332eb 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 36 20  ,  },.     /* 6 
332ec 20 20 20 20 45 4e 44 3a 20 2a 2f 20 7b 20 20 20      END: */ {   
332ed 20 30 2c 20 20 36 2c 20 20 20 20 20 34 2c 20 20   0,  6,     4,  
332ee 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20      4,      4,  
332ef 20 20 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20    4,       4,   
332f0 34 2c 20 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6c 73  4,  },.  };.#els
332f1 65 0a 20 20 2f 2a 20 49 66 20 74 72 69 67 67 65  e.  /* If trigge
332f2 72 73 20 61 72 65 20 6e 6f 74 20 73 75 70 70 6f  rs are not suppo
332f3 72 65 64 20 62 79 20 74 68 69 73 20 63 6f 6d 70  red by this comp
332f4 69 6c 65 20 74 68 65 6e 20 74 68 65 20 73 74 61  ile then the sta
332f5 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e 65 0a 20  tement machine. 
332f6 20 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65   ** used to dete
332f7 63 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20  ct the end of a 
332f8 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6d 75 63  statement is muc
332f9 68 20 73 69 6d 70 6c 69 65 72 0a 20 20 2a 2f 0a  h simplier.  */.
332fa 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
332fb 38 20 74 72 61 6e 73 5b 32 5d 5b 33 5d 20 3d 20  8 trans[2][3] = 
332fc 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
332fd 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a         /* Token:
332fe 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
332ff 20 20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20 20     /* State:    
33300 20 20 20 2a 2a 20 20 53 45 4d 49 20 20 57 53 20     **  SEMI  WS 
33301 20 4f 54 48 45 52 20 2a 2f 0a 20 20 20 20 20 2f   OTHER */.     /
33302 2a 20 30 20 20 20 53 54 41 52 54 3a 20 2a 2f 20  * 0   START: */ 
33303 7b 20 20 20 20 30 2c 20 20 30 2c 20 20 20 20 20  {    0,  0,     
33304 31 2c 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31 20  1, },.     /* 1 
33305 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20   NORMAL: */ {   
33306 20 30 2c 20 20 31 2c 20 20 20 20 20 31 2c 20 7d   0,  1,     1, }
33307 2c 0a 20 20 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a  ,.  };.#endif /*
33308 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
33309 47 47 45 52 20 2a 2f 0a 0a 20 20 77 68 69 6c 65  GGER */..  while
3330a 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 73  ( *zSql ){.    s
3330b 77 69 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b 0a  witch( *zSql ){.
3330c 20 20 20 20 20 20 63 61 73 65 20 27 3b 27 3a 20        case ';': 
3330d 7b 20 20 2f 2a 20 41 20 73 65 6d 69 63 6f 6c 6f  {  /* A semicolo
3330e 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f 6b  n */.        tok
3330f 65 6e 20 3d 20 74 6b 53 45 4d 49 3b 0a 20 20 20  en = tkSEMI;.   
33310 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33311 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27    }.      case '
33312 20 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27   ':.      case '
33313 5c 72 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20  \r':.      case 
33314 27 5c 74 27 3a 0a 20 20 20 20 20 20 63 61 73 65  '\t':.      case
33315 20 27 5c 6e 27 3a 0a 20 20 20 20 20 20 63 61 73   '\n':.      cas
33316 65 20 27 5c 66 27 3a 20 7b 20 20 2f 2a 20 57 68  e '\f': {  /* Wh
33317 69 74 65 20 73 70 61 63 65 20 69 73 20 69 67 6e  ite space is ign
33318 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  ored */.        
33319 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20  token = tkWS;.  
3331a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3331b 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
3331c 27 2f 27 3a 20 7b 20 20 20 2f 2a 20 43 2d 73 74  '/': {   /* C-st
3331d 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a  yle comments */.
3331e 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c          if( zSql
3331f 5b 31 5d 21 3d 27 2a 27 20 29 7b 0a 20 20 20 20  [1]!='*' ){.    
33320 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
33321 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20  OTHER;.         
33322 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
33323 7d 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 20 2b  }.        zSql +
33324 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 77 68 69  = 2;.        whi
33325 6c 65 28 20 7a 53 71 6c 5b 30 5d 20 26 26 20 28  le( zSql[0] && (
33326 7a 53 71 6c 5b 30 5d 21 3d 27 2a 27 20 7c 7c 20  zSql[0]!='*' || 
33327 7a 53 71 6c 5b 31 5d 21 3d 27 2f 27 29 20 29 7b  zSql[1]!='/') ){
33328 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20   zSql++; }.     
33329 20 20 20 69 66 28 20 7a 53 71 6c 5b 30 5d 3d 3d     if( zSql[0]==
3332a 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
3332b 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20        zSql++;.  
3332c 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
3332d 57 53 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  WS;.        brea
3332e 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
3332f 20 63 61 73 65 20 27 2d 27 3a 20 7b 20 20 20 2f   case '-': {   /
33330 2a 20 53 51 4c 2d 73 74 79 6c 65 20 63 6f 6d 6d  * SQL-style comm
33331 65 6e 74 73 20 66 72 6f 6d 20 22 2d 2d 22 20 74  ents from "--" t
33332 6f 20 65 6e 64 20 6f 66 20 6c 69 6e 65 20 2a 2f  o end of line */
33333 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 71  .        if( zSq
33334 6c 5b 31 5d 21 3d 27 2d 27 20 29 7b 0a 20 20 20  l[1]!='-' ){.   
33335 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
33336 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
33337 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
33338 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65   }.        while
33339 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c  ( *zSql && *zSql
3333a 21 3d 27 5c 6e 27 20 29 7b 20 7a 53 71 6c 2b 2b  !='\n' ){ zSql++
3333b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
3333c 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72  *zSql==0 ) retur
3333d 6e 20 73 74 61 74 65 3d 3d 30 3b 0a 20 20 20 20  n state==0;.    
3333e 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53      token = tkWS
3333f 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
33340 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
33341 61 73 65 20 27 5b 27 3a 20 7b 20 20 20 2f 2a 20  ase '[': {   /* 
33342 4d 69 63 72 6f 73 6f 66 74 2d 73 74 79 6c 65 20  Microsoft-style 
33343 69 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20 5b  identifiers in [
33344 2e 2e 2e 5d 20 2a 2f 0a 20 20 20 20 20 20 20 20  ...] */.        
33345 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20  zSql++;.        
33346 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20  while( *zSql && 
33347 2a 7a 53 71 6c 21 3d 27 5d 27 20 29 7b 20 7a 53  *zSql!=']' ){ zS
33348 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20  ql++; }.        
33349 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72  if( *zSql==0 ) r
3334a 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20  eturn 0;.       
3334b 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
3334c 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
3334d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
3334e 61 73 65 20 27 60 27 3a 20 20 20 20 20 2f 2a 20  ase '`':     /* 
3334f 47 72 61 76 65 2d 61 63 63 65 6e 74 20 71 75 6f  Grave-accent quo
33350 74 65 64 20 73 79 6d 62 6f 6c 73 20 75 73 65 64  ted symbols used
33351 20 62 79 20 4d 79 53 51 4c 20 2a 2f 0a 20 20 20   by MySQL */.   
33352 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20 20     case '"':    
33353 20 2f 2a 20 73 69 6e 67 6c 65 2d 20 61 6e 64 20   /* single- and 
33354 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74  double-quoted st
33355 72 69 6e 67 73 20 2a 2f 0a 20 20 20 20 20 20 63  rings */.      c
33356 61 73 65 20 27 5c 27 27 3a 20 7b 0a 20 20 20 20  ase '\'': {.    
33357 20 20 20 20 69 6e 74 20 63 20 3d 20 2a 7a 53 71      int c = *zSq
33358 6c 3b 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b  l;.        zSql+
33359 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  +;.        while
3335a 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c  ( *zSql && *zSql
3335b 21 3d 63 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d  !=c ){ zSql++; }
3335c 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53  .        if( *zS
3335d 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ql==0 ) return 0
3335e 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20  ;.        token 
3335f 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20  = tkOTHER;.     
33360 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
33361 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  }.      default:
33362 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63   {.        int c
33363 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 64  ;.        if( Id
33364 43 68 61 72 28 28 75 38 29 2a 7a 53 71 6c 29 20  Char((u8)*zSql) 
33365 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
33366 4b 65 79 77 6f 72 64 73 20 61 6e 64 20 75 6e 71  Keywords and unq
33367 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65 72  uoted identifier
33368 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69  s */.          i
33369 6e 74 20 6e 49 64 3b 0a 20 20 20 20 20 20 20 20  nt nId;.        
3336a 20 20 66 6f 72 28 6e 49 64 3d 31 3b 20 49 64 43    for(nId=1; IdC
3336b 68 61 72 28 7a 53 71 6c 5b 6e 49 64 5d 29 3b 20  har(zSql[nId]); 
3336c 6e 49 64 2b 2b 29 7b 7d 0a 23 69 66 64 65 66 20  nId++){}.#ifdef 
3336d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
3336e 47 45 52 0a 20 20 20 20 20 20 20 20 20 20 74 6f  GER.          to
3336f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 23  ken = tkOTHER;.#
33370 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 73  else.          s
33371 77 69 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b 0a  witch( *zSql ){.
33372 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
33373 20 27 63 27 3a 20 63 61 73 65 20 27 43 27 3a 20   'c': case 'C': 
33374 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
33375 69 66 28 20 6e 49 64 3d 3d 36 20 26 26 20 73 71  if( nId==6 && sq
33376 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53  lite3StrNICmp(zS
33377 71 6c 2c 20 22 63 72 65 61 74 65 22 2c 20 36 29  ql, "create", 6)
33378 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
33379 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
3337a 6b 43 52 45 41 54 45 3b 0a 20 20 20 20 20 20 20  kCREATE;.       
3337b 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
3337c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
3337d 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20  ken = tkOTHER;. 
3337e 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
3337f 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
33380 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
33381 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 61  }.            ca
33382 73 65 20 27 74 27 3a 20 63 61 73 65 20 27 54 27  se 't': case 'T'
33383 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
33384 20 20 69 66 28 20 6e 49 64 3d 3d 37 20 26 26 20    if( nId==7 && 
33385 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
33386 7a 53 71 6c 2c 20 22 74 72 69 67 67 65 72 22 2c  zSql, "trigger",
33387 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   7)==0 ){.      
33388 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
33389 3d 20 74 6b 54 52 49 47 47 45 52 3b 0a 20 20 20  = tkTRIGGER;.   
3338a 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
3338b 20 69 66 28 20 6e 49 64 3d 3d 34 20 26 26 20 73   if( nId==4 && s
3338c 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
3338d 53 71 6c 2c 20 22 74 65 6d 70 22 2c 20 34 29 3d  Sql, "temp", 4)=
3338e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
3338f 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
33390 54 45 4d 50 3b 0a 20 20 20 20 20 20 20 20 20 20  TEMP;.          
33391 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49      }else if( nI
33392 64 3d 3d 39 20 26 26 20 73 71 6c 69 74 65 33 53  d==9 && sqlite3S
33393 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74  trNICmp(zSql, "t
33394 65 6d 70 6f 72 61 72 79 22 2c 20 39 29 3d 3d 30  emporary", 9)==0
33395 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
33396 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 45      token = tkTE
33397 4d 50 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  MP;.            
33398 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
33399 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
3339a 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20   tkOTHER;.      
3339b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3339c 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3339d 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
3339e 20 20 20 20 20 20 20 20 20 63 61 73 65 20 27 65           case 'e
3339f 27 3a 20 20 63 61 73 65 20 27 45 27 3a 20 7b 0a  ':  case 'E': {.
333a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
333a1 28 20 6e 49 64 3d 3d 33 20 26 26 20 73 71 6c 69  ( nId==3 && sqli
333a2 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c  te3StrNICmp(zSql
333a3 2c 20 22 65 6e 64 22 2c 20 33 29 3d 3d 30 20 29  , "end", 3)==0 )
333a4 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
333a5 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 45 4e 44 3b    token = tkEND;
333a6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
333a7 65 6c 73 65 0a 23 69 66 6e 64 65 66 20 53 51 4c  else.#ifndef SQL
333a8 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
333a9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
333aa 66 28 20 6e 49 64 3d 3d 37 20 26 26 20 73 71 6c  f( nId==7 && sql
333ab 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71  ite3StrNICmp(zSq
333ac 6c 2c 20 22 65 78 70 6c 61 69 6e 22 2c 20 37 29  l, "explain", 7)
333ad 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
333ae 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
333af 6b 45 58 50 4c 41 49 4e 3b 0a 20 20 20 20 20 20  kEXPLAIN;.      
333b0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65          }else.#e
333b1 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 20  ndif.           
333b2 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20     {.           
333b3 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
333b4 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20  THER;.          
333b5 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
333b6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
333b7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
333b8 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
333b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
333ba 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20  ken = tkOTHER;. 
333bb 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
333bc 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
333bd 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65  }.          }.#e
333be 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
333bf 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 20  MIT_TRIGGER */. 
333c0 20 20 20 20 20 20 20 20 20 7a 53 71 6c 20 2b 3d           zSql +=
333c1 20 6e 49 64 2d 31 3b 0a 20 20 20 20 20 20 20 20   nId-1;.        
333c2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
333c3 20 2f 2a 20 4f 70 65 72 61 74 6f 72 73 20 61 6e   /* Operators an
333c4 64 20 73 70 65 63 69 61 6c 20 73 79 6d 62 6f 6c  d special symbol
333c5 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 74  s */.          t
333c6 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
333c7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
333c8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
333c9 0a 20 20 20 20 7d 0a 20 20 20 20 73 74 61 74 65  .    }.    state
333ca 20 3d 20 74 72 61 6e 73 5b 73 74 61 74 65 5d 5b   = trans[state][
333cb 74 6f 6b 65 6e 5d 3b 0a 20 20 20 20 7a 53 71 6c  token];.    zSql
333cc 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ++;.  }.  return
333cd 20 73 74 61 74 65 3d 3d 30 3b 0a 7d 0a 0a 23 69   state==0;.}..#i
333ce 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
333cf 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68  T_UTF16./*.** Th
333d0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
333d1 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71  e same as the sq
333d2 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29  lite3_complete()
333d3 20 72 6f 75 74 69 6e 65 20 64 65 73 63 72 69 62   routine describ
333d4 65 64 0a 2a 2a 20 61 62 6f 76 65 2c 20 65 78 63  ed.** above, exc
333d5 65 70 74 20 74 68 61 74 20 74 68 65 20 70 61 72  ept that the par
333d6 61 6d 65 74 65 72 20 69 73 20 72 65 71 75 69 72  ameter is requir
333d7 65 64 20 74 6f 20 62 65 20 55 54 46 2d 31 36 20  ed to be UTF-16 
333d8 65 6e 63 6f 64 65 64 2c 20 6e 6f 74 0a 2a 2a 20  encoded, not.** 
333d9 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45  UTF-8..*/.SQLITE
333da 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
333db 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73  _complete16(cons
333dc 74 20 76 6f 69 64 20 2a 7a 53 71 6c 29 7b 0a 20  t void *zSql){. 
333dd 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
333de 70 56 61 6c 3b 0a 20 20 63 68 61 72 20 63 6f 6e  pVal;.  char con
333df 73 74 20 2a 7a 53 71 6c 38 3b 0a 20 20 69 6e 74  st *zSql8;.  int
333e0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
333e1 45 4d 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  EM;..#ifndef SQL
333e2 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
333e3 54 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  T.  rc = sqlite3
333e4 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20  _initialize();. 
333e5 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
333e6 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 70 56   rc;.#endif.  pV
333e7 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  al = sqlite3Valu
333e8 65 4e 65 77 28 30 29 3b 0a 20 20 73 71 6c 69 74  eNew(0);.  sqlit
333e9 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56  e3ValueSetStr(pV
333ea 61 6c 2c 20 2d 31 2c 20 7a 53 71 6c 2c 20 53 51  al, -1, zSql, SQ
333eb 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
333ec 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
333ed 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69  ;.  zSql8 = sqli
333ee 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
333ef 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
333f0 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a  .  if( zSql8 ){.
333f1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
333f2 5f 63 6f 6d 70 6c 65 74 65 28 7a 53 71 6c 38 29  _complete(zSql8)
333f3 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
333f4 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
333f5 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
333f6 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a  alueFree(pVal);.
333f7 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
333f8 41 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a  ApiExit(0, rc);.
333f9 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
333fa 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
333fb 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
333fc 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 20  E_OMIT_COMPLETE 
333fd 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
333fe 2a 2a 2a 20 45 6e 64 20 6f 66 20 63 6f 6d 70 6c  *** End of compl
333ff 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ete.c **********
33400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33401 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33402 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
33403 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d  *** Begin file m
33404 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ain.c **********
33405 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33406 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33407 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
33408 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
33409 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
3340a 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
3340b 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
3340c 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
3340d 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
3340e 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
3340f 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
33410 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
33411 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
33412 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
33413 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
33414 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
33415 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
33416 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
33417 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
33418 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
33419 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
3341a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
3341b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3341c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3341d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3341e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61  **********.** Ma
3341f 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  in file for the 
33420 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
33421 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   The routines in
33422 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d   this file.** im
33423 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67  plement the prog
33424 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65  rammer interface
33425 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e   to the library.
33426 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a    Routines in.**
33427 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72 65   other files are
33428 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
33429 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20  e by SQLite and 
3342a 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
3342b 20 61 63 63 65 73 73 65 64 20 62 79 20 75 73 65   accessed by use
3342c 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  rs of the librar
3342d 79 2e 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  y..*/..#ifdef SQ
3342e 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
3342f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
33430 20 49 6e 63 6c 75 64 65 20 66 74 73 33 2e 68 20   Include fts3.h 
33431 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
33432 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
33433 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
33434 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
33435 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
33436 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
33437 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33438 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
33439 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20  ./*.** 2006 Oct 
3343a 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  10.**.** The aut
3343b 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
3343c 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
3343d 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
3343e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
3343f 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
33440 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
33441 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
33442 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
33443 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
33444 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
33445 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
33446 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
33447 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
33448 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
33449 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
3344a 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
3344b 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
3344c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3344d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3344e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3344f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33450 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
33451 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73  s header file is
33452 20 75 73 65 64 20 62 79 20 70 72 6f 67 72 61 6d   used by program
33453 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 6c  s that want to l
33454 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 65 0a  ink against the.
33455 2a 2a 20 46 54 53 33 20 6c 69 62 72 61 72 79 2e  ** FTS3 library.
33456 20 20 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73    All it does is
33457 20 64 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c   declare the sql
33458 69 74 65 33 46 74 73 33 49 6e 69 74 28 29 20 69  ite3Fts3Init() i
33459 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69  nterface..*/..#i
3345a 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b  f 0.extern "C" {
3345b 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70  .#endif  /* __cp
3345c 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49  lusplus */..SQLI
3345d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3345e 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 73  qlite3Fts3Init(s
3345f 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69  qlite3 *db);..#i
33460 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e  f 0.}  /* extern
33461 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20   "C" */.#endif  
33462 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a  /* __cplusplus *
33463 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
33464 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 2e 68  ** End of fts3.h
33465 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
33466 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33467 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33468 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
33469 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
3346a 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
3346b 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a  in main.c ******
3346c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3346d 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  */.#endif.#ifdef
3346e 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52   SQLITE_ENABLE_R
3346f 54 52 45 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  TREE./**********
33470 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 72 74 72  **** Include rtr
33471 65 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  ee.h in the midd
33472 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a  le of main.c ***
33473 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33474 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
33475 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
33476 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  rtree.h ********
33477 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33478 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33479 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
3347a 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65  May 26.**.** The
3347b 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
3347c 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
3347d 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
3347e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
3347f 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
33480 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
33481 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
33482 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
33483 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
33484 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
33485 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
33486 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
33487 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
33488 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
33489 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
3348a 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
3348b 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
3348c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3348d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3348e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3348f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
33491 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
33492 65 20 69 73 20 75 73 65 64 20 62 79 20 70 72 6f  e is used by pro
33493 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20  grams that want 
33494 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20  to link against 
33495 74 68 65 0a 2a 2a 20 52 54 52 45 45 20 6c 69 62  the.** RTREE lib
33496 72 61 72 79 2e 20 20 41 6c 6c 20 69 74 20 64 6f  rary.  All it do
33497 65 73 20 69 73 20 64 65 63 6c 61 72 65 20 74 68  es is declare th
33498 65 20 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e  e sqlite3RtreeIn
33499 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a  it() interface..
3349a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74 65 72 6e  */..#if 0.extern
3349b 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f   "C" {.#endif  /
3349c 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f  * __cplusplus */
3349d 0a 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 74 72 65   int sqlite3Rtre
3349f 65 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64  eInit(sqlite3 *d
334a0 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a  b);..#if 0.}  /*
334a1 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23   extern "C" */.#
334a2 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75  endif  /* __cplu
334a3 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  splus */../*****
334a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
334a5 20 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a   rtree.h *******
334a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334a8 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
334a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
334aa 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
334ab 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63  ft off in main.c
334ac 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
334ad 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66  ********/.#endif
334ae 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
334af 4e 41 42 4c 45 5f 49 43 55 0a 2f 2a 2a 2a 2a 2a  NABLE_ICU./*****
334b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
334b1 65 20 73 71 6c 69 74 65 69 63 75 2e 68 20 69 6e  e sqliteicu.h in
334b2 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d   the middle of m
334b3 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ain.c **********
334b4 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
334b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
334b6 66 69 6c 65 20 73 71 6c 69 74 65 69 63 75 2e 68  file sqliteicu.h
334b7 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
334b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334b9 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
334ba 32 30 30 38 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a  2008 May 26.**.*
334bb 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
334bc 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
334bd 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
334be 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
334bf 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
334c0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
334c1 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
334c2 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
334c3 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
334c4 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
334c5 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
334c6 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
334c7 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
334c8 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
334c9 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
334ca 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
334cb 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
334cc 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
334cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
334d1 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
334d2 72 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 62  r file is used b
334d3 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20  y programs that 
334d4 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61  want to link aga
334d5 69 6e 73 74 20 74 68 65 0a 2a 2a 20 49 43 55 20  inst the.** ICU 
334d6 65 78 74 65 6e 73 69 6f 6e 2e 20 20 41 6c 6c 20  extension.  All 
334d7 69 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61  it does is decla
334d8 72 65 20 74 68 65 20 73 71 6c 69 74 65 33 49 63  re the sqlite3Ic
334d9 75 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63  uInit() interfac
334da 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74  e..*/..#if 0.ext
334db 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66  ern "C" {.#endif
334dc 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73    /* __cplusplus
334dd 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
334de 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
334df 63 75 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a  cuInit(sqlite3 *
334e0 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f  db);..#if 0.}  /
334e1 2a 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a  * extern "C" */.
334e2 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c  #endif  /* __cpl
334e3 75 73 70 6c 75 73 20 2a 2f 0a 0a 0a 2f 2a 2a 2a  usplus */.../***
334e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
334e5 6f 66 20 73 71 6c 69 74 65 69 63 75 2e 68 20 2a  of sqliteicu.h *
334e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
334e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
334ea 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
334eb 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e  left off in main
334ec 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
334ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64  **********/.#end
334ee 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 65  if../*.** The ve
334ef 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62  rsion of the lib
334f0 72 61 72 79 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  rary.*/.#ifndef 
334f1 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
334f2 49 4f 4e 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  ION.SQLITE_API c
334f3 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65  onst char sqlite
334f4 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 51  3_version[] = SQ
334f5 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 23 65  LITE_VERSION;.#e
334f6 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20  ndif.SQLITE_API 
334f7 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
334f8 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76  te3_libversion(v
334f9 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 73 71 6c  oid){ return sql
334fa 69 74 65 33 5f 76 65 72 73 69 6f 6e 3b 20 7d 0a  ite3_version; }.
334fb 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
334fc 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73   char *sqlite3_s
334fd 6f 75 72 63 65 69 64 28 76 6f 69 64 29 7b 20 72  ourceid(void){ r
334fe 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 53 4f 55  eturn SQLITE_SOU
334ff 52 43 45 5f 49 44 3b 20 7d 0a 53 51 4c 49 54 45  RCE_ID; }.SQLITE
33500 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
33501 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62  _libversion_numb
33502 65 72 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  er(void){ return
33503 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f   SQLITE_VERSION_
33504 4e 55 4d 42 45 52 3b 20 7d 0a 53 51 4c 49 54 45  NUMBER; }.SQLITE
33505 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
33506 5f 74 68 72 65 61 64 73 61 66 65 28 76 6f 69 64  _threadsafe(void
33507 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ){ return SQLITE
33508 5f 54 48 52 45 41 44 53 41 46 45 3b 20 7d 0a 0a  _THREADSAFE; }..
33509 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
3350a 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20  ITE_OMIT_TRACE) 
3350b 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
3350c 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45  E_ENABLE_IOTRACE
3350d 29 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66  )./*.** If the f
3350e 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f  ollowing functio
3350f 6e 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74  n pointer is not
33510 20 4e 55 4c 4c 20 61 6e 64 20 69 66 0a 2a 2a 20   NULL and if.** 
33511 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f  SQLITE_ENABLE_IO
33512 54 52 41 43 45 20 69 73 20 65 6e 61 62 6c 65 64  TRACE is enabled
33513 2c 20 74 68 65 6e 20 6d 65 73 73 61 67 65 73 20  , then messages 
33514 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 49 2f  describing.** I/
33515 4f 20 61 63 74 69 76 65 20 61 72 65 20 77 72 69  O active are wri
33516 74 74 65 6e 20 75 73 69 6e 67 20 74 68 69 73 20  tten using this 
33517 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65  function.  These
33518 20 6d 65 73 73 61 67 65 73 0a 2a 2a 20 61 72 65   messages.** are
33519 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 64 65   intended for de
3351a 62 75 67 67 69 6e 67 20 61 63 74 69 76 69 74 79  bugging activity
3351b 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45   only..*/.SQLITE
3351c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a  _PRIVATE void (*
3351d 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29 28  sqlite3IoTrace)(
3351e 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e  const char*, ...
3351f 29 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  ) = 0;.#endif../
33520 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c  *.** If the foll
33521 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72  owing global var
33522 69 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20  iable points to 
33523 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69  a string which i
33524 73 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66  s the.** name of
33525 20 61 20 64 69 72 65 63 74 6f 72 79 2c 20 74 68   a directory, th
33526 65 6e 20 74 68 61 74 20 64 69 72 65 63 74 6f 72  en that director
33527 79 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74  y will be used t
33528 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70 6f  o store.** tempo
33529 72 61 72 79 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a  rary files..**.*
3352a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 22  * See also the "
3352b 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72  PRAGMA temp_stor
3352c 65 5f 64 69 72 65 63 74 6f 72 79 22 20 53 51 4c  e_directory" SQL
3352d 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c   command..*/.SQL
3352e 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71  ITE_API char *sq
3352f 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
33530 74 6f 72 79 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a  tory = 0;../*.**
33531 20 49 6e 69 74 69 61 6c 69 7a 65 20 53 51 4c 69   Initialize SQLi
33532 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  te.  .**.** This
33533 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65   routine must be
33534 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e 69 74 69   called to initi
33535 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79  alize the memory
33536 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20   allocation,.** 
33537 56 46 53 2c 20 61 6e 64 20 6d 75 74 65 78 20 73  VFS, and mutex s
33538 75 62 73 79 73 74 65 6d 73 20 70 72 69 6f 72 20  ubsystems prior 
33539 74 6f 20 64 6f 69 6e 67 20 61 6e 79 20 73 65 72  to doing any ser
3353a 69 6f 75 73 20 77 6f 72 6b 20 77 69 74 68 0a 2a  ious work with.*
3353b 2a 20 53 51 4c 69 74 65 2e 20 20 42 75 74 20 61  * SQLite.  But a
3353c 73 20 6c 6f 6e 67 20 61 73 20 79 6f 75 20 64 6f  s long as you do
3353d 20 6e 6f 74 20 63 6f 6d 70 69 6c 65 20 77 69 74   not compile wit
3353e 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  h SQLITE_OMIT_AU
3353f 54 4f 49 4e 49 54 0a 2a 2a 20 74 68 69 73 20 72  TOINIT.** this r
33540 6f 75 74 69 6e 65 20 77 69 6c 6c 20 62 65 20 63  outine will be c
33541 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61  alled automatica
33542 6c 6c 79 20 62 79 20 6b 65 79 20 72 6f 75 74 69  lly by key routi
33543 6e 65 73 20 73 75 63 68 20 61 73 0a 2a 2a 20 73  nes such as.** s
33544 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 20 20  qlite3_open().  
33545 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
33546 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 65  ine is a no-op e
33547 78 63 65 70 74 20 6f 6e 20 69 74 73 20 76 65 72  xcept on its ver
33548 79 20 66 69 72 73 74 20 63 61 6c 6c 20 66 6f 72  y first call for
33549 20 74 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a   the process,.**
3354a 20 6f 72 20 66 6f 72 20 74 68 65 20 66 69 72 73   or for the firs
3354b 74 20 63 61 6c 6c 20 61 66 74 65 72 20 61 20 63  t call after a c
3354c 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73  all to sqlite3_s
3354d 68 75 74 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54  hutdown..**.** T
3354e 68 65 20 66 69 72 73 74 20 74 68 72 65 61 64 20  he first thread 
3354f 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75  to call this rou
33550 74 69 6e 65 20 72 75 6e 73 20 74 68 65 20 69 6e  tine runs the in
33551 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 74 6f 0a  itialization to.
33552 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 20 20  ** completion.  
33553 49 66 20 73 75 62 73 65 71 75 65 6e 74 20 74 68  If subsequent th
33554 72 65 61 64 73 20 63 61 6c 6c 20 74 68 69 73 20  reads call this 
33555 72 6f 75 74 69 6e 65 20 62 65 66 6f 72 65 20 74  routine before t
33556 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 68 72 65  he first.** thre
33557 61 64 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  ad has finished 
33558 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  the initializati
33559 6f 6e 20 70 72 6f 63 65 73 73 2c 20 74 68 65 6e  on process, then
3355a 20 74 68 65 20 73 75 62 73 65 71 75 65 6e 74 0a   the subsequent.
3355b 2a 2a 20 74 68 72 65 61 64 73 20 6d 75 73 74 20  ** threads must 
3355c 62 6c 6f 63 6b 20 75 6e 74 69 6c 20 74 68 65 20  block until the 
3355d 66 69 72 73 74 20 74 68 72 65 61 64 20 66 69 6e  first thread fin
3355e 69 73 68 65 73 20 77 69 74 68 20 74 68 65 20 69  ishes with the i
3355f 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a  nitialization..*
33560 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
33561 68 72 65 61 64 20 6d 69 67 68 74 20 63 61 6c 6c  hread might call
33562 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
33563 63 75 72 73 69 76 65 6c 79 2e 20 20 52 65 63 75  cursively.  Recu
33564 72 73 69 76 65 0a 2a 2a 20 63 61 6c 6c 73 20 74  rsive.** calls t
33565 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73  o this routine s
33566 68 6f 75 6c 64 20 6e 6f 74 20 62 6c 6f 63 6b 2c  hould not block,
33567 20 6f 66 20 63 6f 75 72 73 65 2e 20 20 4f 74 68   of course.  Oth
33568 65 72 77 69 73 65 20 74 68 65 0a 2a 2a 20 69 6e  erwise the.** in
33569 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72 6f  itialization pro
3356a 63 65 73 73 20 77 6f 75 6c 64 20 6e 65 76 65 72  cess would never
3356b 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a   complete..**.**
3356c 20 4c 65 74 20 58 20 62 65 20 74 68 65 20 66 69   Let X be the fi
3356d 72 73 74 20 74 68 72 65 61 64 20 74 6f 20 65 6e  rst thread to en
3356e 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
3356f 2e 20 20 4c 65 74 20 59 20 62 65 20 73 6f 6d 65  .  Let Y be some
33570 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64   other.** thread
33571 2e 20 20 54 68 65 6e 20 77 68 69 6c 65 20 74 68  .  Then while th
33572 65 20 69 6e 69 74 69 61 6c 20 69 6e 76 6f 63 61  e initial invoca
33573 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75  tion of this rou
33574 74 69 6e 65 20 62 79 20 58 20 69 73 0a 2a 2a 20  tine by X is.** 
33575 69 6e 63 6f 6d 70 6c 65 74 65 2c 20 69 74 20 69  incomplete, it i
33576 73 20 72 65 71 75 69 72 65 64 20 74 68 61 74 3a  s required that:
33577 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 43 61 6c  .**.**    *  Cal
33578 6c 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ls to this routi
33579 6e 65 20 66 72 6f 6d 20 59 20 6d 75 73 74 20 62  ne from Y must b
3357a 6c 6f 63 6b 20 75 6e 74 69 6c 20 74 68 65 20 6f  lock until the o
3357b 75 74 65 72 2d 6d 6f 73 74 0a 2a 2a 20 20 20 20  uter-most.**    
3357c 20 20 20 63 61 6c 6c 20 62 79 20 58 20 63 6f 6d     call by X com
3357d 70 6c 65 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  pletes..**.**   
3357e 20 2a 20 20 52 65 63 75 72 73 69 76 65 20 63 61   *  Recursive ca
3357f 6c 6c 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74  lls to this rout
33580 69 6e 65 20 66 72 6f 6d 20 74 68 72 65 61 64 20  ine from thread 
33581 58 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69 61  X return immedia
33582 74 65 6c 79 0a 2a 2a 20 20 20 20 20 20 20 77 69  tely.**       wi
33583 74 68 6f 75 74 20 62 6c 6f 63 6b 69 6e 67 2e 0a  thout blocking..
33584 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
33585 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  t sqlite3_initia
33586 6c 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 73 71  lize(void){.  sq
33587 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61  lite3_mutex *pMa
33588 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  ster;           
33589 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
3358a 65 20 6d 61 69 6e 20 73 74 61 74 69 63 20 6d 75  e main static mu
3358b 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  tex */.  int rc;
3358c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3358d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3358e 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
3358f 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20  code */..#ifdef 
33590 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a  SQLITE_OMIT_WSD.
33591 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 77    rc = sqlite3_w
33592 73 64 5f 69 6e 69 74 28 34 30 39 36 2c 20 32 34  sd_init(4096, 24
33593 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
33594 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
33595 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e  turn rc;.  }.#en
33596 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 53 51 4c  dif..  /* If SQL
33597 69 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 63  ite is already c
33598 6f 6d 70 6c 65 74 65 6c 79 20 69 6e 69 74 69 61  ompletely initia
33599 6c 69 7a 65 64 2c 20 74 68 65 6e 20 74 68 69 73  lized, then this
3359a 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 73 71   call.  ** to sq
3359b 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
3359c 28 29 20 73 68 6f 75 6c 64 20 62 65 20 61 20 6e  () should be a n
3359d 6f 2d 6f 70 2e 20 20 42 75 74 20 74 68 65 20 69  o-op.  But the i
3359e 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 20 20  nitialization.  
3359f 2a 2a 20 6d 75 73 74 20 62 65 20 63 6f 6d 70 6c  ** must be compl
335a0 65 74 65 2e 20 20 53 6f 20 69 73 49 6e 69 74 20  ete.  So isInit 
335a1 6d 75 73 74 20 6e 6f 74 20 62 65 20 73 65 74 20  must not be set 
335a2 75 6e 74 69 6c 20 74 68 65 20 76 65 72 79 20 65  until the very e
335a3 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73 20  nd.  ** of this 
335a4 72 6f 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20  routine..  */.  
335a5 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
335a6 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 29  lConfig.isInit )
335a7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
335a8 4b 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  K;..  /* Make su
335a9 72 65 20 74 68 65 20 6d 75 74 65 78 20 73 75 62  re the mutex sub
335aa 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61  system is initia
335ab 6c 69 7a 65 64 2e 20 20 49 66 20 75 6e 61 62 6c  lized.  If unabl
335ac 65 20 74 6f 20 0a 20 20 2a 2a 20 69 6e 69 74 69  e to .  ** initi
335ad 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20  alize the mutex 
335ae 73 75 62 73 79 73 74 65 6d 2c 20 72 65 74 75 72  subsystem, retur
335af 6e 20 65 61 72 6c 79 20 77 69 74 68 20 74 68 65  n early with the
335b0 20 65 72 72 6f 72 2e 0a 20 20 2a 2a 20 49 66 20   error..  ** If 
335b1 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 73 6f  the system is so
335b2 20 73 69 63 6b 20 74 68 61 74 20 77 65 20 61 72   sick that we ar
335b3 65 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f  e unable to allo
335b4 63 61 74 65 20 61 20 6d 75 74 65 78 2c 0a 20 20  cate a mutex,.  
335b5 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20  ** there is not 
335b6 6d 75 63 68 20 53 51 4c 69 74 65 20 69 73 20 67  much SQLite is g
335b7 6f 69 6e 67 20 74 6f 20 62 65 20 61 62 6c 65 20  oing to be able 
335b8 74 6f 20 64 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  to do..  **.  **
335b9 20 54 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   The mutex subsy
335ba 73 74 65 6d 20 6d 75 73 74 20 74 61 6b 65 20 63  stem must take c
335bb 61 72 65 20 6f 66 20 73 65 72 69 61 6c 69 7a 69  are of serializi
335bc 6e 67 20 69 74 73 20 6f 77 6e 0a 20 20 2a 2a 20  ng its own.  ** 
335bd 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a  initialization..
335be 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69    */.  rc = sqli
335bf 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a  te3MutexInit();.
335c0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
335c1 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74  n rc;..  /* Init
335c2 69 61 6c 69 7a 65 20 74 68 65 20 6d 61 6c 6c 6f  ialize the mallo
335c3 63 28 29 20 73 79 73 74 65 6d 20 61 6e 64 20 74  c() system and t
335c4 68 65 20 72 65 63 75 72 73 69 76 65 20 70 49 6e  he recursive pIn
335c5 69 74 4d 75 74 65 78 20 6d 75 74 65 78 2e 0a 20  itMutex mutex.. 
335c6 20 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69   ** This operati
335c7 6f 6e 20 69 73 20 70 72 6f 74 65 63 74 65 64 20  on is protected 
335c8 62 79 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41  by the STATIC_MA
335c9 53 54 45 52 20 6d 75 74 65 78 2e 20 20 4e 6f 74  STER mutex.  Not
335ca 65 20 74 68 61 74 0a 20 20 2a 2a 20 4d 75 74 65  e that.  ** Mute
335cb 78 41 6c 6c 6f 63 28 29 20 69 73 20 63 61 6c 6c  xAlloc() is call
335cc 65 64 20 66 6f 72 20 61 20 73 74 61 74 69 63 20  ed for a static 
335cd 6d 75 74 65 78 20 70 72 69 6f 72 20 74 6f 20 69  mutex prior to i
335ce 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a  nitializing the.
335cf 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 73 75 62 73    ** malloc subs
335d0 79 73 74 65 6d 20 2d 20 74 68 69 73 20 69 6d 70  ystem - this imp
335d1 6c 69 65 73 20 74 68 61 74 20 74 68 65 20 61 6c  lies that the al
335d2 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20 73 74  location of a st
335d3 61 74 69 63 0a 20 20 2a 2a 20 6d 75 74 65 78 20  atic.  ** mutex 
335d4 6d 75 73 74 20 6e 6f 74 20 72 65 71 75 69 72 65  must not require
335d5 20 73 75 70 70 6f 72 74 20 66 72 6f 6d 20 74 68   support from th
335d6 65 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74  e malloc subsyst
335d7 65 6d 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 74  em..  */.  pMast
335d8 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  er = sqlite3Mute
335d9 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
335da 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
335db 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  R);.  sqlite3_mu
335dc 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65  tex_enter(pMaste
335dd 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f  r);.  sqlite3Glo
335de 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65  balConfig.isMute
335df 78 49 6e 69 74 20 3d 20 31 3b 0a 20 20 69 66 28  xInit = 1;.  if(
335e0 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   !sqlite3GlobalC
335e1 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e  onfig.isMallocIn
335e2 69 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  it ){.    rc = s
335e3 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74  qlite3MallocInit
335e4 28 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  ();.  }.  if( rc
335e5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
335e6 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
335e7 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49  Config.isMallocI
335e8 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 69 66 28  nit = 1;.    if(
335e9 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   !sqlite3GlobalC
335ea 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
335eb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
335ec 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49  3GlobalConfig.pI
335ed 6e 69 74 4d 75 74 65 78 20 3d 0a 20 20 20 20 20  nitMutex =.     
335ee 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 75 74        sqlite3Mut
335ef 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
335f0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b  UTEX_RECURSIVE);
335f1 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
335f2 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
335f3 43 6f 72 65 4d 75 74 65 78 20 26 26 20 21 73 71  CoreMutex && !sq
335f4 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
335f5 67 2e 70 49 6e 69 74 4d 75 74 65 78 20 29 7b 0a  g.pInitMutex ){.
335f6 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
335f7 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
335f8 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69   }.    }.  }.  i
335f9 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
335fa 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   ){.    sqlite3G
335fb 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66  lobalConfig.nRef
335fc 49 6e 69 74 4d 75 74 65 78 2b 2b 3b 0a 20 20 7d  InitMutex++;.  }
335fd 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
335fe 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b  _leave(pMaster);
335ff 0a 0a 20 20 2f 2a 20 49 66 20 72 63 20 69 73 20  ..  /* If rc is 
33600 6e 6f 74 20 53 51 4c 49 54 45 5f 4f 4b 20 61 74  not SQLITE_OK at
33601 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65   this point, the
33602 6e 20 65 69 74 68 65 72 20 74 68 65 20 6d 61 6c  n either the mal
33603 6c 6f 63 0a 20 20 2a 2a 20 73 75 62 73 79 73 74  loc.  ** subsyst
33604 65 6d 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  em could not be 
33605 69 6e 69 74 69 61 6c 69 7a 65 64 20 6f 72 20 74  initialized or t
33606 68 65 20 73 79 73 74 65 6d 20 66 61 69 6c 65 64  he system failed
33607 20 74 6f 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a   to allocate.  *
33608 2a 20 74 68 65 20 70 49 6e 69 74 4d 75 74 65 78  * the pInitMutex
33609 20 6d 75 74 65 78 2e 20 52 65 74 75 72 6e 20 61   mutex. Return a
3360a 6e 20 65 72 72 6f 72 20 69 6e 20 65 69 74 68 65  n error in eithe
3360b 72 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 69 66  r case.  */.  if
3360c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3360d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
3360e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 74  ;.  }..  /* Do t
3360f 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 69  he rest of the i
33610 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 6e  nitialization un
33611 64 65 72 20 74 68 65 20 72 65 63 75 72 73 69 76  der the recursiv
33612 65 20 6d 75 74 65 78 20 73 6f 0a 20 20 2a 2a 20  e mutex so.  ** 
33613 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20  that we will be 
33614 61 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 72  able to handle r
33615 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 69  ecursive calls i
33616 6e 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  nto.  ** sqlite3
33617 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20  _initialize().  
33618 54 68 65 20 72 65 63 75 72 73 69 76 65 20 63 61  The recursive ca
33619 6c 6c 73 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6d  lls normally com
3361a 65 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20 73  e through.  ** s
3361b 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
3361c 20 77 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73   when it invokes
3361d 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67   sqlite3_vfs_reg
3361e 69 73 74 65 72 28 29 2c 20 62 75 74 20 6f 74 68  ister(), but oth
3361f 65 72 0a 20 20 2a 2a 20 72 65 63 75 72 73 69 76  er.  ** recursiv
33620 65 20 63 61 6c 6c 73 20 6d 69 67 68 74 20 61 6c  e calls might al
33621 73 6f 20 62 65 20 70 6f 73 73 69 62 6c 65 2e 0a  so be possible..
33622 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d    */.  sqlite3_m
33623 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
33624 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
33625 49 6e 69 74 4d 75 74 65 78 29 3b 0a 20 20 69 66  InitMutex);.  if
33626 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
33627 6f 6e 66 69 67 2e 69 73 49 6e 69 74 3d 3d 30 20  onfig.isInit==0 
33628 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  && sqlite3Global
33629 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73  Config.inProgres
3362a 73 3d 3d 30 20 29 7b 0a 20 20 20 20 46 75 6e 63  s==0 ){.    Func
3362b 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d  DefHash *pHash =
3362c 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66   &GLOBAL(FuncDef
3362d 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f  Hash, sqlite3Glo
3362e 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20  balFunctions);. 
3362f 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
33630 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73  Config.inProgres
33631 73 20 3d 20 31 3b 0a 20 20 20 20 6d 65 6d 73 65  s = 1;.    memse
33632 74 28 70 48 61 73 68 2c 20 30 2c 20 73 69 7a 65  t(pHash, 0, size
33633 6f 66 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  of(sqlite3Global
33634 46 75 6e 63 74 69 6f 6e 73 29 29 3b 0a 20 20 20  Functions));.   
33635 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
33636 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28  GlobalFunctions(
33637 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
33638 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
33639 73 50 43 61 63 68 65 49 6e 69 74 3d 3d 30 20 29  sPCacheInit==0 )
3363a 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
3363b 69 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61  ite3PcacheInitia
3363c 6c 69 7a 65 28 29 3b 0a 20 20 20 20 7d 0a 20 20  lize();.    }.  
3363d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3363e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
3363f 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
33640 2e 69 73 50 43 61 63 68 65 49 6e 69 74 20 3d 20  .isPCacheInit = 
33641 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  1;.      rc = sq
33642 6c 69 74 65 33 4f 73 49 6e 69 74 28 29 3b 0a 20  lite3OsInit();. 
33643 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
33644 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
33645 20 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68      sqlite3PCach
33646 65 42 75 66 66 65 72 53 65 74 75 70 28 20 73 71  eBufferSetup( sq
33647 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33648 67 2e 70 50 61 67 65 2c 20 0a 20 20 20 20 20 20  g.pPage, .      
33649 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3364a 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 2c 20  lConfig.szPage, 
3364b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3364c 66 69 67 2e 6e 50 61 67 65 29 3b 0a 20 20 20 20  fig.nPage);.    
3364d 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3364e 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 3d 20 31  onfig.isInit = 1
3364f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
33650 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33651 69 6e 50 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a  inProgress = 0;.
33652 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
33653 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
33654 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49  3GlobalConfig.pI
33655 6e 69 74 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a  nitMutex);..  /*
33656 20 47 6f 20 62 61 63 6b 20 75 6e 64 65 72 20 74   Go back under t
33657 68 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 20  he static mutex 
33658 61 6e 64 20 63 6c 65 61 6e 20 75 70 20 74 68 65  and clean up the
33659 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a 2a 20   recursive.  ** 
3365a 6d 75 74 65 78 20 74 6f 20 70 72 65 76 65 6e 74  mutex to prevent
3365b 20 61 20 72 65 73 6f 75 72 63 65 20 6c 65 61 6b   a resource leak
3365c 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
3365d 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61  _mutex_enter(pMa
3365e 73 74 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  ster);.  sqlite3
3365f 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65  GlobalConfig.nRe
33660 66 49 6e 69 74 4d 75 74 65 78 2d 2d 3b 0a 20 20  fInitMutex--;.  
33661 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
33662 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74  lConfig.nRefInit
33663 4d 75 74 65 78 3c 3d 30 20 29 7b 0a 20 20 20 20  Mutex<=0 ){.    
33664 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 47  assert( sqlite3G
33665 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66  lobalConfig.nRef
33666 49 6e 69 74 4d 75 74 65 78 3d 3d 30 20 29 3b 0a  InitMutex==0 );.
33667 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
33668 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 47 6c  x_free(sqlite3Gl
33669 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74  obalConfig.pInit
3366a 4d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69  Mutex);.    sqli
3366b 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3366c 70 49 6e 69 74 4d 75 74 65 78 20 3d 20 30 3b 0a  pInitMutex = 0;.
3366d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
3366e 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65  tex_leave(pMaste
3366f 72 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f  r);..  /* The fo
33670 6c 6c 6f 77 69 6e 67 20 69 73 20 6a 75 73 74 20  llowing is just 
33671 61 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20 74  a sanity check t
33672 6f 20 6d 61 6b 65 20 73 75 72 65 20 53 51 4c 69  o make sure SQLi
33673 74 65 20 68 61 73 0a 20 20 2a 2a 20 62 65 65 6e  te has.  ** been
33674 20 63 6f 6d 70 69 6c 65 64 20 63 6f 72 72 65 63   compiled correc
33675 74 6c 79 2e 20 20 49 74 20 69 73 20 69 6d 70 6f  tly.  It is impo
33676 72 74 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 69  rtant to run thi
33677 73 20 63 6f 64 65 2c 20 62 75 74 0a 20 20 2a 2a  s code, but.  **
33678 20 77 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 74   we don't want t
33679 6f 20 72 75 6e 20 69 74 20 74 6f 6f 20 6f 66 74  o run it too oft
3367a 65 6e 20 61 6e 64 20 73 6f 61 6b 20 75 70 20 43  en and soak up C
3367b 50 55 20 63 79 63 6c 65 73 20 66 6f 72 20 6e 6f  PU cycles for no
3367c 0a 20 20 2a 2a 20 72 65 61 73 6f 6e 2e 20 20 53  .  ** reason.  S
3367d 6f 20 77 65 20 72 75 6e 20 69 74 20 6f 6e 63 65  o we run it once
3367e 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69   during initiali
3367f 7a 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66  zation..  */.#if
33680 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 69 66 6e  ndef NDEBUG.#ifn
33681 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
33682 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20  FLOATING_POINT. 
33683 20 2f 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e   /* This section
33684 20 6f 66 20 63 6f 64 65 27 73 20 6f 6e 6c 79 20   of code's only 
33685 22 6f 75 74 70 75 74 22 20 69 73 20 76 69 61 20  "output" is via 
33686 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
33687 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 20 28 20 72  nts. */.  if ( r
33688 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
33689 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 28 75      u64 x = (((u
3368a 36 34 29 31 29 3c 3c 36 33 29 2d 31 3b 0a 20 20  64)1)<<63)-1;.  
3368b 20 20 64 6f 75 62 6c 65 20 79 3b 0a 20 20 20 20    double y;.    
3368c 61 73 73 65 72 74 28 73 69 7a 65 6f 66 28 78 29  assert(sizeof(x)
3368d 3d 3d 38 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ==8);.    assert
3368e 28 73 69 7a 65 6f 66 28 78 29 3d 3d 73 69 7a 65  (sizeof(x)==size
3368f 6f 66 28 79 29 29 3b 0a 20 20 20 20 6d 65 6d 63  of(y));.    memc
33690 70 79 28 26 79 2c 20 26 78 2c 20 38 29 3b 0a 20  py(&y, &x, 8);. 
33691 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
33692 65 33 49 73 4e 61 4e 28 79 29 20 29 3b 0a 20 20  e3IsNaN(y) );.  
33693 7d 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  }.#endif.#endif.
33694 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
33695 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20  ./*.** Undo the 
33696 65 66 66 65 63 74 73 20 6f 66 20 73 71 6c 69 74  effects of sqlit
33697 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e  e3_initialize().
33698 20 20 4d 75 73 74 20 6e 6f 74 20 62 65 20 63 61    Must not be ca
33699 6c 6c 65 64 20 77 68 69 6c 65 0a 2a 2a 20 74 68  lled while.** th
3369a 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64  ere are outstand
3369b 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ing database con
3369c 6e 65 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f  nections or memo
3369d 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f  ry allocations o
3369e 72 0a 2a 2a 20 77 68 69 6c 65 20 61 6e 79 20 70  r.** while any p
3369f 61 72 74 20 6f 66 20 53 51 4c 69 74 65 20 69 73  art of SQLite is
336a0 20 6f 74 68 65 72 77 69 73 65 20 69 6e 20 75 73   otherwise in us
336a1 65 20 69 6e 20 61 6e 79 20 74 68 72 65 61 64 2e  e in any thread.
336a2 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
336a3 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73  e is not threads
336a4 61 66 65 2e 20 20 42 75 74 20 69 74 20 69 73 20  afe.  But it is 
336a5 73 61 66 65 20 74 6f 20 69 6e 76 6f 6b 65 20 74  safe to invoke t
336a6 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6f  his routine.** o
336a7 6e 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73  n when SQLite is
336a8 20 61 6c 72 65 61 64 79 20 73 68 75 74 20 64 6f   already shut do
336a9 77 6e 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69  wn.  If SQLite i
336aa 73 20 61 6c 72 65 61 64 79 20 73 68 75 74 20 64  s already shut d
336ab 6f 77 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73  own.** when this
336ac 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f   routine is invo
336ad 6b 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 72  ked, then this r
336ae 6f 75 74 69 6e 65 20 69 73 20 61 20 68 61 72 6d  outine is a harm
336af 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53  less no-op..*/.S
336b0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
336b1 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76  lite3_shutdown(v
336b2 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69  oid){.  if( sqli
336b3 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
336b4 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 73 71  isInit ){.    sq
336b5 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 3b 0a  lite3_os_end();.
336b6 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65      sqlite3_rese
336b7 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e  t_auto_extension
336b8 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  ();.    sqlite3G
336b9 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e  lobalConfig.isIn
336ba 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  it = 0;.  }.  if
336bb 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
336bc 6f 6e 66 69 67 2e 69 73 50 43 61 63 68 65 49 6e  onfig.isPCacheIn
336bd 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  it ){.    sqlite
336be 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28  3PcacheShutdown(
336bf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  );.    sqlite3Gl
336c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61  obalConfig.isPCa
336c1 63 68 65 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d  cheInit = 0;.  }
336c2 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
336c3 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c  obalConfig.isMal
336c4 6c 6f 63 49 6e 69 74 20 29 7b 0a 20 20 20 20 73  locInit ){.    s
336c5 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28  qlite3MallocEnd(
336c6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  );.    sqlite3Gl
336c7 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c  obalConfig.isMal
336c8 6c 6f 63 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d  locInit = 0;.  }
336c9 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
336ca 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 75 74  obalConfig.isMut
336cb 65 78 49 6e 69 74 20 29 7b 0a 20 20 20 20 73 71  exInit ){.    sq
336cc 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 29 3b  lite3MutexEnd();
336cd 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
336ce 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78  alConfig.isMutex
336cf 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  Init = 0;.  }.. 
336d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
336d1 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  K;.}../*.** This
336d2 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70 70 6c   API allows appl
336d3 69 63 61 74 69 6f 6e 73 20 74 6f 20 6d 6f 64 69  ications to modi
336d4 66 79 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f  fy the global co
336d5 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 66 0a 2a  nfiguration of.*
336d6 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  * the SQLite lib
336d7 72 61 72 79 20 61 74 20 72 75 6e 2d 74 69 6d 65  rary at run-time
336d8 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
336d9 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79  tine should only
336da 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   be called when 
336db 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74  there are no out
336dc 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 64 61 74 61  standing.** data
336dd 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
336de 20 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63   or memory alloc
336df 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 72 6f  ations.  This ro
336e0 75 74 69 6e 65 20 69 73 20 6e 6f 74 0a 2a 2a 20  utine is not.** 
336e1 74 68 72 65 61 64 73 61 66 65 2e 20 20 46 61 69  threadsafe.  Fai
336e2 6c 75 72 65 20 74 6f 20 68 65 65 64 20 74 68 65  lure to heed the
336e3 73 65 20 77 61 72 6e 69 6e 67 73 20 63 61 6e 20  se warnings can 
336e4 6c 65 61 64 20 74 6f 20 75 6e 70 72 65 64 69 63  lead to unpredic
336e5 74 61 62 6c 65 0a 2a 2a 20 62 65 68 61 76 69 6f  table.** behavio
336e6 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
336e7 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e   int sqlite3_con
336e8 66 69 67 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29  fig(int op, ...)
336e9 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
336ea 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
336eb 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 73 71 6c 69  E_OK;..  /* sqli
336ec 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 73 68 61  te3_config() sha
336ed 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ll return SQLITE
336ee 5f 4d 49 53 55 53 45 20 69 66 20 69 74 20 69 73  _MISUSE if it is
336ef 20 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 0a 20   invoked while. 
336f0 20 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c   ** the SQLite l
336f1 69 62 72 61 72 79 20 69 73 20 69 6e 20 75 73 65  ibrary is in use
336f2 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74  . */.  if( sqlit
336f3 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
336f4 73 49 6e 69 74 20 29 20 72 65 74 75 72 6e 20 53  sInit ) return S
336f5 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 0a 20  QLITE_MISUSE;.. 
336f6 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70   va_start(ap, op
336f7 29 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20  );.  switch( op 
336f8 29 7b 0a 0a 20 20 20 20 2f 2a 20 4d 75 74 65 78  ){..    /* Mutex
336f9 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
336fa 70 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 20  ptions are only 
336fb 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 20 74  available in a t
336fc 68 72 65 61 64 73 61 66 65 0a 20 20 20 20 2a 2a  hreadsafe.    **
336fd 20 63 6f 6d 70 69 6c 65 2e 20 0a 20 20 20 20 2a   compile. .    *
336fe 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  /.#if SQLITE_THR
336ff 45 41 44 53 41 46 45 0a 20 20 20 20 63 61 73 65  EADSAFE.    case
33700 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53   SQLITE_CONFIG_S
33701 49 4e 47 4c 45 54 48 52 45 41 44 3a 20 7b 0a 20  INGLETHREAD: {. 
33702 20 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20       /* Disable 
33703 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a  all mutexing */.
33704 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
33705 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
33706 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20  utex = 0;.      
33707 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33708 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d  fig.bFullMutex =
33709 20 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   0;.      break;
3370a 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
3370b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
3370c 4c 54 49 54 48 52 45 41 44 3a 20 7b 0a 20 20 20  LTITHREAD: {.   
3370d 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 6d 75     /* Disable mu
3370e 74 65 78 69 6e 67 20 6f 66 20 64 61 74 61 62 61  texing of databa
3370f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a  se connections *
33710 2f 0a 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c  /.      /* Enabl
33711 65 20 6d 75 74 65 78 69 6e 67 20 6f 66 20 63 6f  e mutexing of co
33712 72 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72  re data structur
33713 65 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  es */.      sqli
33714 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33715 62 43 6f 72 65 4d 75 74 65 78 20 3d 20 31 3b 0a  bCoreMutex = 1;.
33716 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
33717 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d  balConfig.bFullM
33718 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20  utex = 0;.      
33719 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
3371a 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
3371b 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 3a 20  FIG_SERIALIZED: 
3371c 7b 0a 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c  {.      /* Enabl
3371d 65 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a  e all mutexing *
3371e 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
3371f 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72  lobalConfig.bCor
33720 65 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20 20 20  eMutex = 1;.    
33721 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
33722 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78  onfig.bFullMutex
33723 20 3d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61   = 1;.      brea
33724 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
33725 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
33726 4d 55 54 45 58 3a 20 7b 0a 20 20 20 20 20 20 2f  MUTEX: {.      /
33727 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74  * Specify an alt
33728 65 72 6e 61 74 69 76 65 20 6d 75 74 65 78 20 69  ernative mutex i
33729 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f  mplementation */
3372a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
3372b 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
3372c 20 3d 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73   = *va_arg(ap, s
3372d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
3372e 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20 20 62 72  hods*);.      br
3372f 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
33730 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
33731 47 5f 47 45 54 4d 55 54 45 58 3a 20 7b 0a 20 20  G_GETMUTEX: {.  
33732 20 20 20 20 2f 2a 20 52 65 74 72 69 65 76 65 20      /* Retrieve 
33733 74 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65  the current mute
33734 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
33735 20 2a 2f 0a 20 20 20 20 20 20 2a 76 61 5f 61 72   */.      *va_ar
33736 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d 75  g(ap, sqlite3_mu
33737 74 65 78 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20  tex_methods*) = 
33738 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33739 66 69 67 2e 6d 75 74 65 78 3b 0a 20 20 20 20 20  fig.mutex;.     
3373a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
3373b 6e 64 69 66 0a 0a 0a 20 20 20 20 63 61 73 65 20  ndif...    case 
3373c 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41  SQLITE_CONFIG_MA
3373d 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  LLOC: {.      /*
3373e 20 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65   Specify an alte
3373f 72 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 20 69  rnative malloc i
33740 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f  mplementation */
33741 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
33742 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a  obalConfig.m = *
33743 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74  va_arg(ap, sqlit
33744 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 29  e3_mem_methods*)
33745 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
33746 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
33747 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d  LITE_CONFIG_GETM
33748 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f  ALLOC: {.      /
33749 2a 20 52 65 74 72 69 65 76 65 20 74 68 65 20 63  * Retrieve the c
3374a 75 72 72 65 6e 74 20 6d 61 6c 6c 6f 63 28 29 20  urrent malloc() 
3374b 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
3374c 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  /.      if( sqli
3374d 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3374e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 20 73  m.xMalloc==0 ) s
3374f 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61  qlite3MemSetDefa
33750 75 6c 74 28 29 3b 0a 20 20 20 20 20 20 2a 76 61  ult();.      *va
33751 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33  _arg(ap, sqlite3
33752 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 20 3d  _mem_methods*) =
33753 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
33754 6e 66 69 67 2e 6d 3b 0a 20 20 20 20 20 20 62 72  nfig.m;.      br
33755 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
33756 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
33757 47 5f 4d 45 4d 53 54 41 54 55 53 3a 20 7b 0a 20  G_MEMSTATUS: {. 
33758 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6f       /* Enable o
33759 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 6d 61  r disable the ma
3375a 6c 6c 6f 63 20 73 74 61 74 75 73 20 63 6f 6c 6c  lloc status coll
3375b 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  ection */.      
3375c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3375d 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 3d 20 76  fig.bMemstat = v
3375e 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
3375f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
33760 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
33761 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43  TE_CONFIG_SCRATC
33762 48 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65  H: {.      /* De
33763 73 69 67 6e 61 74 65 20 61 20 62 75 66 66 65 72  signate a buffer
33764 20 66 6f 72 20 73 63 72 61 74 63 68 20 6d 65 6d   for scratch mem
33765 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20  ory space */.   
33766 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
33767 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20  Config.pScratch 
33768 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69  = va_arg(ap, voi
33769 64 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  d*);.      sqlit
3376a 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
3376b 7a 53 63 72 61 74 63 68 20 3d 20 76 61 5f 61 72  zScratch = va_ar
3376c 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
3376d 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3376e 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20 3d  onfig.nScratch =
3376f 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
33770 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
33771 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
33772 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45  LITE_CONFIG_PAGE
33773 43 41 43 48 45 3a 20 7b 0a 20 20 20 20 20 20 2f  CACHE: {.      /
33774 2a 20 44 65 73 69 67 6e 61 74 65 20 61 20 62 75  * Designate a bu
33775 66 66 65 72 20 66 6f 72 20 70 61 67 65 20 63 61  ffer for page ca
33776 63 68 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65  che memory space
33777 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
33778 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50  3GlobalConfig.pP
33779 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  age = va_arg(ap,
3377a 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 73   void*);.      s
3377b 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3377c 69 67 2e 73 7a 50 61 67 65 20 3d 20 76 61 5f 61  ig.szPage = va_a
3377d 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
3377e 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
3377f 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 3d 20 76  Config.nPage = v
33780 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
33781 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
33782 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c   }..    case SQL
33783 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48  ITE_CONFIG_PCACH
33784 45 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53 70  E: {.      /* Sp
33785 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61  ecify an alterna
33786 74 69 76 65 20 70 61 67 65 20 63 61 63 68 65 20  tive page cache 
33787 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
33788 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
33789 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
3378a 68 65 20 3d 20 2a 76 61 5f 61 72 67 28 61 70 2c  he = *va_arg(ap,
3378b 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
3378c 6d 65 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20  methods*);.     
3378d 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
3378e 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
3378f 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 3a  ONFIG_GETPCACHE:
33790 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c   {.      if( sql
33791 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
33792 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 3d 3d 30  .pcache.xInit==0
33793 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
33794 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61  te3PCacheSetDefa
33795 75 6c 74 28 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ult();.      }. 
33796 20 20 20 20 20 2a 76 61 5f 61 72 67 28 61 70 2c       *va_arg(ap,
33797 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
33798 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69  methods*) = sqli
33799 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3379a 70 63 61 63 68 65 3b 0a 20 20 20 20 20 20 62 72  pcache;.      br
3379b 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20  eak;.    }..#if 
3379c 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
3379d 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 29 20 7c  NABLE_MEMSYS3) |
3379e 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
3379f 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 29  _ENABLE_MEMSYS5)
337a0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
337a1 5f 43 4f 4e 46 49 47 5f 48 45 41 50 3a 20 7b 0a  _CONFIG_HEAP: {.
337a2 20 20 20 20 20 20 2f 2a 20 44 65 73 69 67 6e 61        /* Designa
337a3 74 65 20 61 20 62 75 66 66 65 72 20 66 6f 72 20  te a buffer for 
337a4 68 65 61 70 20 6d 65 6d 6f 72 79 20 73 70 61 63  heap memory spac
337a5 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  e */.      sqlit
337a6 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
337a7 48 65 61 70 20 3d 20 76 61 5f 61 72 67 28 61 70  Heap = va_arg(ap
337a8 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20  , void*);.      
337a9 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
337aa 66 69 67 2e 6e 48 65 61 70 20 3d 20 76 61 5f 61  fig.nHeap = va_a
337ab 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
337ac 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
337ad 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 20 3d 20 76  Config.mnReq = v
337ae 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
337af 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
337b0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
337b1 48 65 61 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Heap==0 ){.     
337b2 20 20 20 2f 2a 20 49 66 20 74 68 65 20 68 65 61     /* If the hea
337b3 70 20 70 6f 69 6e 74 65 72 20 69 73 20 4e 55 4c  p pointer is NUL
337b4 4c 2c 20 74 68 65 6e 20 72 65 73 74 6f 72 65 20  L, then restore 
337b5 74 68 65 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65  the malloc imple
337b6 6d 65 6e 74 61 74 69 6f 6e 0a 20 20 20 20 20 20  mentation.      
337b7 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 4e 55 4c    ** back to NUL
337b8 4c 20 70 6f 69 6e 74 65 72 73 20 74 6f 6f 2e 20  L pointers too. 
337b9 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65   This will cause
337ba 20 74 68 65 20 6d 61 6c 6c 6f 63 20 74 6f 20 67   the malloc to g
337bb 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 61 63  o.        ** bac
337bc 6b 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c 74  k to its default
337bd 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
337be 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 69 6e 69  when sqlite3_ini
337bf 74 69 61 6c 69 7a 65 28 29 20 69 73 0a 20 20 20  tialize() is.   
337c0 20 20 20 20 20 2a 2a 20 72 75 6e 2e 0a 20 20 20       ** run..   
337c1 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
337c2 6d 65 6d 73 65 74 28 26 73 71 6c 69 74 65 33 47  memset(&sqlite3G
337c3 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2c 20 30  lobalConfig.m, 0
337c4 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  , sizeof(sqlite3
337c5 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 29 29  GlobalConfig.m))
337c6 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
337c7 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68 65         /* The he
337c8 61 70 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f  ap pointer is no
337c9 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 6e 73  t NULL, then ins
337ca 74 61 6c 6c 20 6f 6e 65 20 6f 66 20 74 68 65 0a  tall one of the.
337cb 20 20 20 20 20 20 20 20 2a 2a 20 6d 65 6d 35 2e          ** mem5.
337cc 63 2f 6d 65 6d 33 2e 63 20 6d 65 74 68 6f 64 73  c/mem3.c methods
337cd 2e 20 49 66 20 6e 65 69 74 68 65 72 20 45 4e 41  . If neither ENA
337ce 42 4c 45 5f 4d 45 4d 53 59 53 33 20 6e 6f 72 0a  BLE_MEMSYS3 nor.
337cf 20 20 20 20 20 20 20 20 2a 2a 20 45 4e 41 42 4c          ** ENABL
337d0 45 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66  E_MEMSYS5 is def
337d1 69 6e 65 64 2c 20 72 65 74 75 72 6e 20 61 6e 20  ined, return an 
337d2 65 72 72 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a  error..        *
337d3 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
337d4 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 0a 20  ENABLE_MEMSYS3. 
337d5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c         sqlite3Gl
337d6 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a  obalConfig.m = *
337d7 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d  sqlite3MemGetMem
337d8 73 79 73 33 28 29 3b 0a 23 65 6e 64 69 66 0a 23  sys3();.#endif.#
337d9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
337da 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 20 20 20 20  BLE_MEMSYS5.    
337db 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
337dc 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 73 71 6c  lConfig.m = *sql
337dd 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73  ite3MemGetMemsys
337de 35 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  5();.#endif.    
337df 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
337e0 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
337e1 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
337e2 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3a  ONFIG_LOOKASIDE:
337e3 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
337e4 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 4c  GlobalConfig.szL
337e5 6f 6f 6b 61 73 69 64 65 20 3d 20 76 61 5f 61 72  ookaside = va_ar
337e6 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
337e7 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
337e8 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b 61 73 69 64 65  onfig.nLookaside
337e9 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
337ea 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t);.      break;
337eb 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64 65 66 61  .    }..    defa
337ec 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  ult: {.      rc 
337ed 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
337ee 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
337ef 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28   }.  }.  va_end(
337f0 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ap);.  return rc
337f1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75  ;.}../*.** Set u
337f2 70 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20  p the lookaside 
337f3 62 75 66 66 65 72 73 20 66 6f 72 20 61 20 64 61  buffers for a da
337f4 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
337f5 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  n..** Return SQL
337f6 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
337f7 73 2e 20 20 0a 2a 2a 20 49 66 20 6c 6f 6f 6b 61  s.  .** If looka
337f8 73 69 64 65 20 69 73 20 61 6c 72 65 61 64 79 20  side is already 
337f9 61 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20 53  active, return S
337fa 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a  QLITE_BUSY..**.*
337fb 2a 20 54 68 65 20 73 7a 20 70 61 72 61 6d 65 74  * The sz paramet
337fc 65 72 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  er is the number
337fd 20 6f 66 20 62 79 74 65 73 20 69 6e 20 65 61 63   of bytes in eac
337fe 68 20 6c 6f 6f 6b 61 73 69 64 65 20 73 6c 6f 74  h lookaside slot
337ff 2e 0a 2a 2a 20 54 68 65 20 63 6e 74 20 70 61 72  ..** The cnt par
33800 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e 75  ameter is the nu
33801 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 2e 20 20  mber of slots.  
33802 49 66 20 70 53 74 61 72 74 20 69 73 20 4e 55 4c  If pStart is NUL
33803 4c 20 74 68 65 0a 2a 2a 20 73 70 61 63 65 20 66  L the.** space f
33804 6f 72 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65  or the lookaside
33805 20 6d 65 6d 6f 72 79 20 69 73 20 6f 62 74 61 69   memory is obtai
33806 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
33807 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66  _malloc()..** If
33808 20 70 53 74 61 72 74 20 69 73 20 6e 6f 74 20 4e   pStart is not N
33809 55 4c 4c 20 74 68 65 6e 20 69 74 20 69 73 20 73  ULL then it is s
3380a 7a 2a 63 6e 74 20 62 79 74 65 73 20 6f 66 20 6d  z*cnt bytes of m
3380b 65 6d 6f 72 79 20 74 6f 20 75 73 65 20 66 6f 72  emory to use for
3380c 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64  .** the lookasid
3380d 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61  e memory..*/.sta
3380e 74 69 63 20 69 6e 74 20 73 65 74 75 70 4c 6f 6f  tic int setupLoo
3380f 6b 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a  kaside(sqlite3 *
33810 64 62 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  db, void *pBuf, 
33811 69 6e 74 20 73 7a 2c 20 69 6e 74 20 63 6e 74 29  int sz, int cnt)
33812 7b 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74  {.  void *pStart
33813 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b  ;.  if( db->look
33814 61 73 69 64 65 2e 6e 4f 75 74 20 29 7b 0a 20 20  aside.nOut ){.  
33815 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
33816 42 55 53 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46  BUSY;.  }.  /* F
33817 72 65 65 20 61 6e 79 20 65 78 69 73 74 69 6e 67  ree any existing
33818 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65   lookaside buffe
33819 72 20 66 6f 72 20 74 68 69 73 20 68 61 6e 64 6c  r for this handl
3381a 65 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 6c  e before.  ** al
3381b 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f  locating a new o
3381c 6e 65 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 68  ne so we don't h
3381d 61 76 65 20 74 6f 20 68 61 76 65 20 73 70 61 63  ave to have spac
3381e 65 20 66 6f 72 20 0a 20 20 2a 2a 20 62 6f 74 68  e for .  ** both
3381f 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
33820 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  e..  */.  if( db
33821 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c  ->lookaside.bMal
33822 6c 6f 63 65 64 20 29 7b 0a 20 20 20 20 73 71 6c  loced ){.    sql
33823 69 74 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f  ite3_free(db->lo
33824 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74 29 3b  okaside.pStart);
33825 0a 20 20 7d 0a 20 20 2f 2a 20 54 68 65 20 73 69  .  }.  /* The si
33826 7a 65 20 6f 66 20 61 20 6c 6f 6f 6b 61 73 69 64  ze of a lookasid
33827 65 20 73 6c 6f 74 20 6e 65 65 64 73 20 74 6f 20  e slot needs to 
33828 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 61  be larger than a
33829 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f   pointer.  ** to
3382a 20 62 65 20 75 73 65 66 75 6c 2e 0a 20 20 2a 2f   be useful..  */
3382b 0a 20 20 69 66 28 20 73 7a 3c 3d 28 69 6e 74 29  .  if( sz<=(int)
3382c 73 69 7a 65 6f 66 28 4c 6f 6f 6b 61 73 69 64 65  sizeof(Lookaside
3382d 53 6c 6f 74 2a 29 20 29 20 73 7a 20 3d 20 30 3b  Slot*) ) sz = 0;
3382e 0a 20 20 69 66 28 20 63 6e 74 3c 30 20 29 20 63  .  if( cnt<0 ) c
3382f 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 7a  nt = 0;.  if( sz
33830 3d 3d 30 20 7c 7c 20 63 6e 74 3d 3d 30 20 29 7b  ==0 || cnt==0 ){
33831 0a 20 20 20 20 73 7a 20 3d 20 30 3b 0a 20 20 20  .    sz = 0;.   
33832 20 70 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 7d   pStart = 0;.  }
33833 65 6c 73 65 20 69 66 28 20 70 42 75 66 3d 3d 30  else if( pBuf==0
33834 20 29 7b 0a 20 20 20 20 73 7a 20 3d 20 52 4f 55   ){.    sz = ROU
33835 4e 44 38 28 73 7a 29 3b 0a 20 20 20 20 73 71 6c  ND8(sz);.    sql
33836 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
33837 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 70 53 74  alloc();.    pSt
33838 61 72 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  art = sqlite3Mal
33839 6c 6f 63 28 20 73 7a 2a 63 6e 74 20 29 3b 0a 20  loc( sz*cnt );. 
3383a 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e     sqlite3EndBen
3383b 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d  ignMalloc();.  }
3383c 65 6c 73 65 7b 0a 20 20 20 20 73 7a 20 3d 20 52  else{.    sz = R
3383d 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 0a 20  OUNDDOWN8(sz);. 
3383e 20 20 20 70 53 74 61 72 74 20 3d 20 70 42 75 66     pStart = pBuf
3383f 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 6c 6f 6f 6b  ;.  }.  db->look
33840 61 73 69 64 65 2e 70 53 74 61 72 74 20 3d 20 70  aside.pStart = p
33841 53 74 61 72 74 3b 0a 20 20 64 62 2d 3e 6c 6f 6f  Start;.  db->loo
33842 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 30  kaside.pFree = 0
33843 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  ;.  db->lookasid
33844 65 2e 73 7a 20 3d 20 28 75 31 36 29 73 7a 3b 0a  e.sz = (u16)sz;.
33845 20 20 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a    if( pStart ){.
33846 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4c      int i;.    L
33847 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 3b  ookasideSlot *p;
33848 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 20  .    assert( sz 
33849 3e 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 4c 6f  > (int)sizeof(Lo
3384a 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 20 29 3b  okasideSlot*) );
3384b 0a 20 20 20 20 70 20 3d 20 28 4c 6f 6f 6b 61 73  .    p = (Lookas
3384c 69 64 65 53 6c 6f 74 2a 29 70 53 74 61 72 74 3b  ideSlot*)pStart;
3384d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 2d 31  .    for(i=cnt-1
3384e 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20  ; i>=0; i--){.  
3384f 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64      p->pNext = d
33850 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72  b->lookaside.pFr
33851 65 65 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f  ee;.      db->lo
33852 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20  okaside.pFree = 
33853 70 3b 0a 20 20 20 20 20 20 70 20 3d 20 28 4c 6f  p;.      p = (Lo
33854 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 26 28 28  okasideSlot*)&((
33855 75 38 2a 29 70 29 5b 73 7a 5d 3b 0a 20 20 20 20  u8*)p)[sz];.    
33856 7d 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  }.    db->lookas
33857 69 64 65 2e 70 45 6e 64 20 3d 20 70 3b 0a 20 20  ide.pEnd = p;.  
33858 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
33859 62 45 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 20 20  bEnabled = 1;.  
3385a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
3385b 62 4d 61 6c 6c 6f 63 65 64 20 3d 20 70 42 75 66  bMalloced = pBuf
3385c 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 7d 65 6c 73  ==0 ?1:0;.  }els
3385d 65 7b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61  e{.    db->looka
3385e 73 69 64 65 2e 70 45 6e 64 20 3d 20 30 3b 0a 20  side.pEnd = 0;. 
3385f 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
33860 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20  .bEnabled = 0;. 
33861 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
33862 2e 62 4d 61 6c 6c 6f 63 65 64 20 3d 20 30 3b 0a  .bMalloced = 0;.
33863 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
33864 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
33865 20 52 65 74 75 72 6e 20 74 68 65 20 6d 75 74 65   Return the mute
33866 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
33867 68 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  h a database con
33868 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  nection..*/.SQLI
33869 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d  TE_API sqlite3_m
3386a 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64 62  utex *sqlite3_db
3386b 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33 20 2a  _mutex(sqlite3 *
3386c 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62  db){.  return db
3386d 2d 3e 6d 75 74 65 78 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->mutex;.}../*.*
3386e 2a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  * Configuration 
3386f 73 65 74 74 69 6e 67 73 20 66 6f 72 20 61 6e 20  settings for an 
33870 69 6e 64 69 76 69 64 75 61 6c 20 64 61 74 61 62  individual datab
33871 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
33872 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
33873 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66   sqlite3_db_conf
33874 69 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ig(sqlite3 *db, 
33875 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20  int op, ...){.  
33876 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e  va_list ap;.  in
33877 74 20 72 63 3b 0a 20 20 76 61 5f 73 74 61 72 74  t rc;.  va_start
33878 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69 74  (ap, op);.  swit
33879 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61  ch( op ){.    ca
3387a 73 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46  se SQLITE_DBCONF
3387b 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b 0a  IG_LOOKASIDE: {.
3387c 20 20 20 20 20 20 76 6f 69 64 20 2a 70 42 75 66        void *pBuf
3387d 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
3387e 69 64 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  id*);.      int 
3387f 73 7a 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  sz = va_arg(ap, 
33880 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  int);.      int 
33881 63 6e 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  cnt = va_arg(ap,
33882 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 72 63 20   int);.      rc 
33883 3d 20 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65  = setupLookaside
33884 28 64 62 2c 20 70 42 75 66 2c 20 73 7a 2c 20 63  (db, pBuf, sz, c
33885 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  nt);.      break
33886 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
33887 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  ult: {.      rc 
33888 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
33889 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3388a 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28   }.  }.  va_end(
3388b 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ap);.  return rc
3388c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ;.}.../*.** Retu
3388d 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 62  rn true if the b
3388e 75 66 66 65 72 20 7a 5b 30 2e 2e 6e 2d 31 5d 20  uffer z[0..n-1] 
3388f 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 73 70 61  contains all spa
33890 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ces..*/.static i
33891 6e 74 20 61 6c 6c 53 70 61 63 65 73 28 63 6f 6e  nt allSpaces(con
33892 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
33893 6e 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30  n){.  while( n>0
33894 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 20 27 20   && z[n-1]==' ' 
33895 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 72 65 74 75  ){ n--; }.  retu
33896 72 6e 20 6e 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn n==0;.}../*.*
33897 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 64 65  * This is the de
33898 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20  fault collating 
33899 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 22  function named "
3389a 42 49 4e 41 52 59 22 20 77 68 69 63 68 20 69 73  BINARY" which is
3389b 20 61 6c 77 61 79 73 0a 2a 2a 20 61 76 61 69 6c   always.** avail
3389c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  able..**.** If t
3389d 68 65 20 70 61 64 46 6c 61 67 20 61 72 67 75 6d  he padFlag argum
3389e 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  ent is not NULL 
3389f 74 68 65 6e 20 73 70 61 63 65 20 70 61 64 64 69  then space paddi
338a0 6e 67 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a  ng at the end.**
338a1 20 6f 66 20 73 74 72 69 6e 67 73 20 69 73 20 69   of strings is i
338a2 67 6e 6f 72 65 64 2e 20 20 54 68 69 73 20 69 6d  gnored.  This im
338a3 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 52 54 52  plements the RTR
338a4 49 4d 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 2a 2f  IM collation..*/
338a5 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69 6e 43  .static int binC
338a6 6f 6c 6c 46 75 6e 63 28 0a 20 20 76 6f 69 64 20  ollFunc(.  void 
338a7 2a 70 61 64 46 6c 61 67 2c 0a 20 20 69 6e 74 20  *padFlag,.  int 
338a8 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69  nKey1, const voi
338a9 64 20 2a 70 4b 65 79 31 2c 0a 20 20 69 6e 74 20  d *pKey1,.  int 
338aa 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f 69  nKey2, const voi
338ab 64 20 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 69 6e  d *pKey2.){.  in
338ac 74 20 72 63 2c 20 6e 3b 0a 20 20 6e 20 3d 20 6e  t rc, n;.  n = n
338ad 4b 65 79 31 3c 6e 4b 65 79 32 20 3f 20 6e 4b 65  Key1<nKey2 ? nKe
338ae 79 31 20 3a 20 6e 4b 65 79 32 3b 0a 20 20 72 63  y1 : nKey2;.  rc
338af 20 3d 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31 2c   = memcmp(pKey1,
338b0 20 70 4b 65 79 32 2c 20 6e 29 3b 0a 20 20 69 66   pKey2, n);.  if
338b1 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ( rc==0 ){.    i
338b2 66 28 20 70 61 64 46 6c 61 67 0a 20 20 20 20 20  f( padFlag.     
338b3 26 26 20 61 6c 6c 53 70 61 63 65 73 28 28 28 63  && allSpaces(((c
338b4 68 61 72 2a 29 70 4b 65 79 31 29 2b 6e 2c 20 6e  har*)pKey1)+n, n
338b5 4b 65 79 31 2d 6e 29 0a 20 20 20 20 20 26 26 20  Key1-n).     && 
338b6 61 6c 6c 53 70 61 63 65 73 28 28 28 63 68 61 72  allSpaces(((char
338b7 2a 29 70 4b 65 79 32 29 2b 6e 2c 20 6e 4b 65 79  *)pKey2)+n, nKey
338b8 32 2d 6e 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  2-n).    ){.    
338b9 20 20 2f 2a 20 4c 65 61 76 65 20 72 63 20 75 6e    /* Leave rc un
338ba 63 68 61 6e 67 65 64 20 61 74 20 30 20 2a 2f 0a  changed at 0 */.
338bb 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
338bc 20 72 63 20 3d 20 6e 4b 65 79 31 20 2d 20 6e 4b   rc = nKey1 - nK
338bd 65 79 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ey2;.    }.  }. 
338be 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
338bf 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 62 75 69  *.** Another bui
338c0 6c 74 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e 67 20  lt-in collating 
338c1 73 65 71 75 65 6e 63 65 3a 20 4e 4f 43 41 53 45  sequence: NOCASE
338c2 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f  . .**.** This co
338c3 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
338c4 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20   is intended to 
338c5 62 65 20 75 73 65 64 20 66 6f 72 20 22 63 61 73  be used for "cas
338c6 65 20 69 6e 64 65 70 65 6e 64 61 6e 74 0a 2a 2a  e independant.**
338c7 20 63 6f 6d 70 61 72 69 73 6f 6e 22 2e 20 53 51   comparison". SQ
338c8 4c 69 74 65 27 73 20 6b 6e 6f 77 6c 65 64 67 65  Lite's knowledge
338c9 20 6f 66 20 75 70 70 65 72 20 61 6e 64 20 6c 6f   of upper and lo
338ca 77 65 72 20 63 61 73 65 20 65 71 75 69 76 61 6c  wer case equival
338cb 65 6e 74 73 0a 2a 2a 20 65 78 74 65 6e 64 73 20  ents.** extends 
338cc 6f 6e 6c 79 20 74 6f 20 74 68 65 20 32 36 20 63  only to the 26 c
338cd 68 61 72 61 63 74 65 72 73 20 75 73 65 64 20 69  haracters used i
338ce 6e 20 74 68 65 20 45 6e 67 6c 69 73 68 20 6c 61  n the English la
338cf 6e 67 75 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 74  nguage..**.** At
338d0 20 74 68 65 20 6d 6f 6d 65 6e 74 20 74 68 65 72   the moment ther
338d1 65 20 69 73 20 6f 6e 6c 79 20 61 20 55 54 46 2d  e is only a UTF-
338d2 38 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  8 implementation
338d3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
338d4 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67 46  nocaseCollatingF
338d5 75 6e 63 28 0a 20 20 76 6f 69 64 20 2a 4e 6f 74  unc(.  void *Not
338d6 55 73 65 64 2c 0a 20 20 69 6e 74 20 6e 4b 65 79  Used,.  int nKey
338d7 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  1, const void *p
338d8 4b 65 79 31 2c 0a 20 20 69 6e 74 20 6e 4b 65 79  Key1,.  int nKey
338d9 32 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  2, const void *p
338da 4b 65 79 32 0a 29 7b 0a 20 20 69 6e 74 20 72 20  Key2.){.  int r 
338db 3d 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  = sqlite3StrNICm
338dc 70 28 0a 20 20 20 20 20 20 28 63 6f 6e 73 74 20  p(.      (const 
338dd 63 68 61 72 20 2a 29 70 4b 65 79 31 2c 20 28 63  char *)pKey1, (c
338de 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79  onst char *)pKey
338df 32 2c 20 28 6e 4b 65 79 31 3c 6e 4b 65 79 32 29  2, (nKey1<nKey2)
338e0 3f 6e 4b 65 79 31 3a 6e 4b 65 79 32 29 3b 0a 20  ?nKey1:nKey2);. 
338e1 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
338e2 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66  R(NotUsed);.  if
338e3 28 20 30 3d 3d 72 20 29 7b 0a 20 20 20 20 72 20  ( 0==r ){.    r 
338e4 3d 20 6e 4b 65 79 31 2d 6e 4b 65 79 32 3b 0a 20  = nKey1-nKey2;. 
338e5 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d   }.  return r;.}
338e6 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
338e7 68 65 20 52 4f 57 49 44 20 6f 66 20 74 68 65 20  he ROWID of the 
338e8 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 73 65  most recent inse
338e9 72 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  rt.*/.SQLITE_API
338ea 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71   sqlite_int64 sq
338eb 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
338ec 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 20  t_rowid(sqlite3 
338ed 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64  *db){.  return d
338ee 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 7d 0a  b->lastRowid;.}.
338ef 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
338f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e  e number of chan
338f1 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20  ges in the most 
338f2 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73  recent call to s
338f3 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a  qlite3_exec()..*
338f4 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
338f5 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
338f6 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
338f7 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e 43 68 61   return db->nCha
338f8 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  nge;.}../*.** Re
338f9 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
338fa 6f 66 20 63 68 61 6e 67 65 73 20 73 69 6e 63 65  of changes since
338fb 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
338fc 6e 64 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 2e  ndle was opened.
338fd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
338fe 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  nt sqlite3_total
338ff 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33  _changes(sqlite3
33900 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20   *db){.  return 
33901 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65  db->nTotalChange
33902 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
33903 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f   all open savepo
33904 69 6e 74 73 2e 20 54 68 69 73 20 66 75 6e 63 74  ints. This funct
33905 69 6f 6e 20 6f 6e 6c 79 20 6d 61 6e 69 70 75 6c  ion only manipul
33906 61 74 65 73 20 66 69 65 6c 64 73 20 6f 66 20 74  ates fields of t
33907 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68  he.** database h
33908 61 6e 64 6c 65 20 6f 62 6a 65 63 74 2c 20 69 74  andle object, it
33909 20 64 6f 65 73 20 6e 6f 74 20 63 6c 6f 73 65 20   does not close 
3390a 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20 74  any savepoints t
3390b 68 61 74 20 6d 61 79 20 62 65 20 6f 70 65 6e 0a  hat may be open.
3390c 2a 2a 20 61 74 20 74 68 65 20 62 2d 74 72 65 65  ** at the b-tree
3390d 2f 70 61 67 65 72 20 6c 65 76 65 6c 2e 0a 2a 2f  /pager level..*/
3390e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3390f 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73  void sqlite3Clos
33910 65 53 61 76 65 70 6f 69 6e 74 73 28 73 71 6c 69  eSavepoints(sqli
33911 74 65 33 20 2a 64 62 29 7b 0a 20 20 77 68 69 6c  te3 *db){.  whil
33912 65 28 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e  e( db->pSavepoin
33913 74 20 29 7b 0a 20 20 20 20 53 61 76 65 70 6f 69  t ){.    Savepoi
33914 6e 74 20 2a 70 54 6d 70 20 3d 20 64 62 2d 3e 70  nt *pTmp = db->p
33915 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 64  Savepoint;.    d
33916 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20  b->pSavepoint = 
33917 70 54 6d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  pTmp->pNext;.   
33918 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
33919 62 2c 20 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 20  b, pTmp);.  }.  
3391a 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d  db->nSavepoint =
3391b 20 30 3b 0a 20 20 64 62 2d 3e 6e 53 74 61 74 65   0;.  db->nState
3391c 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e  ment = 0;.  db->
3391d 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76  isTransactionSav
3391e 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f  epoint = 0;.}../
3391f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 65 78  *.** Close an ex
33920 69 73 74 69 6e 67 20 53 51 4c 69 74 65 20 64 61  isting SQLite da
33921 74 61 62 61 73 65 0a 2a 2f 0a 53 51 4c 49 54 45  tabase.*/.SQLITE
33922 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
33923 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a  _close(sqlite3 *
33924 64 62 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  db){.  HashElem 
33925 2a 69 3b 0a 20 20 69 6e 74 20 6a 3b 0a 0a 20 20  *i;.  int j;..  
33926 69 66 28 20 21 64 62 20 29 7b 0a 20 20 20 20 72  if( !db ){.    r
33927 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
33928 0a 20 20 7d 0a 20 20 69 66 28 20 21 73 71 6c 69  .  }.  if( !sqli
33929 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69  te3SafetyCheckSi
3392a 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20  ckOrOk(db) ){.  
3392b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3392c 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71  MISUSE;.  }.  sq
3392d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
3392e 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20  r(db->mutex);.. 
3392f 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
33930 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
33931 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74  0);..  /* If a t
33932 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70  ransaction is op
33933 65 6e 2c 20 74 68 65 20 52 65 73 65 74 49 6e 74  en, the ResetInt
33934 65 72 6e 61 6c 53 63 68 65 6d 61 28 29 20 63 61  ernalSchema() ca
33935 6c 6c 20 61 62 6f 76 65 0a 20 20 2a 2a 20 77 69  ll above.  ** wi
33936 6c 6c 20 6e 6f 74 20 68 61 76 65 20 63 61 6c 6c  ll not have call
33937 65 64 20 74 68 65 20 78 44 69 73 63 6f 6e 6e 65  ed the xDisconne
33938 63 74 28 29 20 6d 65 74 68 6f 64 20 6f 6e 20 61  ct() method on a
33939 6e 79 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20  ny virtual.  ** 
3393a 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 62  tables in the db
3393b 2d 3e 61 56 54 72 61 6e 73 5b 5d 20 61 72 72 61  ->aVTrans[] arra
3393c 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  y. The following
3393d 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c   sqlite3VtabRoll
3393e 62 61 63 6b 28 29 0a 20 20 2a 2a 20 63 61 6c 6c  back().  ** call
3393f 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 20 57 65 20   will do so. We 
33940 6e 65 65 64 20 74 6f 20 64 6f 20 74 68 69 73 20  need to do this 
33941 62 65 66 6f 72 65 20 74 68 65 20 63 68 65 63 6b  before the check
33942 20 66 6f 72 20 61 63 74 69 76 65 0a 20 20 2a 2a   for active.  **
33943 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
33944 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 76 2d  below, as the v-
33945 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
33946 74 69 6f 6e 20 6d 61 79 20 62 65 20 73 74 6f 72  tion may be stor
33947 69 6e 67 0a 20 20 2a 2a 20 73 6f 6d 65 20 70 72  ing.  ** some pr
33948 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
33949 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20  s internally..  
3394a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62  */.  sqlite3Vtab
3394b 52 6f 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 0a 20  Rollback(db);.. 
3394c 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
3394d 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67   any outstanding
3394e 20 56 4d 73 2c 20 72 65 74 75 72 6e 20 53 51 4c   VMs, return SQL
3394f 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 69  ITE_BUSY. */.  i
33950 66 28 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a  f( db->pVdbe ){.
33951 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
33952 28 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59  (db, SQLITE_BUSY
33953 2c 20 0a 20 20 20 20 20 20 20 20 22 75 6e 61 62  , .        "unab
33954 6c 65 20 74 6f 20 63 6c 6f 73 65 20 64 75 65 20  le to close due 
33955 74 6f 20 75 6e 66 69 6e 61 6c 69 73 65 64 20 73  to unfinalised s
33956 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20  tatements");.   
33957 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
33958 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
33959 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
3395a 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 61  TE_BUSY;.  }.  a
3395b 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 61  ssert( sqlite3Sa
3395c 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f  fetyCheckSickOrO
3395d 6b 28 64 62 29 20 29 3b 0a 0a 20 20 66 6f 72 28  k(db) );..  for(
3395e 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20  j=0; j<db->nDb; 
3395f 6a 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20  j++){.    Btree 
33960 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 6a  *pBt = db->aDb[j
33961 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70  ].pBt;.    if( p
33962 42 74 20 26 26 20 73 71 6c 69 74 65 33 42 74 72  Bt && sqlite3Btr
33963 65 65 49 73 49 6e 42 61 63 6b 75 70 28 70 42 74  eeIsInBackup(pBt
33964 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
33965 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
33966 54 45 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20  TE_BUSY, .      
33967 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 63      "unable to c
33968 6c 6f 73 65 20 64 75 65 20 74 6f 20 75 6e 66 69  lose due to unfi
33969 6e 69 73 68 65 64 20 62 61 63 6b 75 70 20 6f 70  nished backup op
3396a 65 72 61 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20  eration");.     
3396b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
3396c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
3396d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
3396e 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
3396f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20  .  }..  /* Free 
33970 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  any outstanding 
33971 53 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74  Savepoint struct
33972 75 72 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  ures. */.  sqlit
33973 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74  e3CloseSavepoint
33974 73 28 64 62 29 3b 0a 0a 20 20 66 6f 72 28 6a 3d  s(db);..  for(j=
33975 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a 2b  0; j<db->nDb; j+
33976 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44  +){.    struct D
33977 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  b *pDb = &db->aD
33978 62 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 20 70 44  b[j];.    if( pD
33979 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20  b->pBt ){.      
3397a 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
3397b 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20  e(pDb->pBt);.   
3397c 20 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b     pDb->pBt = 0;
3397d 0a 20 20 20 20 20 20 69 66 28 20 6a 21 3d 31 20  .      if( j!=1 
3397e 29 7b 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e  ){.        pDb->
3397f 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 20  pSchema = 0;.   
33980 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
33981 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
33982 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
33983 30 29 3b 0a 0a 20 20 2f 2a 20 54 65 6c 6c 20 74  0);..  /* Tell t
33984 68 65 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66  he code in notif
33985 79 2e 63 20 74 68 61 74 20 74 68 65 20 63 6f 6e  y.c that the con
33986 6e 65 63 74 69 6f 6e 20 6e 6f 20 6c 6f 6e 67 65  nection no longe
33987 72 20 68 6f 6c 64 73 20 61 6e 79 0a 20 20 2a 2a  r holds any.  **
33988 20 6c 6f 63 6b 73 20 61 6e 64 20 64 6f 65 73 20   locks and does 
33989 6e 6f 74 20 72 65 71 75 69 72 65 20 61 6e 79 20  not require any 
3398a 66 75 72 74 68 65 72 20 75 6e 6c 6f 63 6b 2d 6e  further unlock-n
3398b 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 73 2e  otify callbacks.
3398c 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43  .  */.  sqlite3C
3398d 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28  onnectionClosed(
3398e 64 62 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  db);..  assert( 
3398f 64 62 2d 3e 6e 44 62 3c 3d 32 20 29 3b 0a 20 20  db->nDb<=2 );.  
33990 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 3d  assert( db->aDb=
33991 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29  =db->aDbStatic )
33992 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41  ;.  for(j=0; j<A
33993 72 72 61 79 53 69 7a 65 28 64 62 2d 3e 61 46 75  rraySize(db->aFu
33994 6e 63 2e 61 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20  nc.a); j++){.   
33995 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 2c   FuncDef *pNext,
33996 20 2a 70 48 61 73 68 2c 20 2a 70 3b 0a 20 20 20   *pHash, *p;.   
33997 20 66 6f 72 28 70 3d 64 62 2d 3e 61 46 75 6e 63   for(p=db->aFunc
33998 2e 61 5b 6a 5d 3b 20 70 3b 20 70 3d 70 48 61 73  .a[j]; p; p=pHas
33999 68 29 7b 0a 20 20 20 20 20 20 70 48 61 73 68 20  h){.      pHash 
3399a 3d 20 70 2d 3e 70 48 61 73 68 3b 0a 20 20 20 20  = p->pHash;.    
3399b 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20    while( p ){.  
3399c 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d        pNext = p-
3399d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
3399e 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
3399f 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 70 20  , p);.        p 
339a0 3d 20 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  = pNext;.      }
339a1 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72  .    }.  }.  for
339a2 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (i=sqliteHashFir
339a3 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71  st(&db->aCollSeq
339a4 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74 65 48 61  ); i; i=sqliteHa
339a5 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20  shNext(i)){.    
339a6 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d  CollSeq *pColl =
339a7 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73 71 6c 69   (CollSeq *)sqli
339a8 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20  teHashData(i);. 
339a9 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 61 6e 79     /* Invoke any
339aa 20 64 65 73 74 72 75 63 74 6f 72 73 20 72 65 67   destructors reg
339ab 69 73 74 65 72 65 64 20 66 6f 72 20 63 6f 6c 6c  istered for coll
339ac 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75  ation sequence u
339ad 73 65 72 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20  ser data. */.   
339ae 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b 20 6a   for(j=0; j<3; j
339af 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
339b0 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 20 29 7b 0a  Coll[j].xDel ){.
339b1 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 5b 6a 5d          pColl[j]
339b2 2e 78 44 65 6c 28 70 43 6f 6c 6c 5b 6a 5d 2e 70  .xDel(pColl[j].p
339b3 55 73 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  User);.      }. 
339b4 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
339b5 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 6c  DbFree(db, pColl
339b6 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
339b7 48 61 73 68 43 6c 65 61 72 28 26 64 62 2d 3e 61  HashClear(&db->a
339b8 43 6f 6c 6c 53 65 71 29 3b 0a 23 69 66 6e 64 65  CollSeq);.#ifnde
339b9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
339ba 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 66 6f 72  RTUALTABLE.  for
339bb 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (i=sqliteHashFir
339bc 73 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29  st(&db->aModule)
339bd 3b 20 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73  ; i; i=sqliteHas
339be 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 4d  hNext(i)){.    M
339bf 6f 64 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 28 4d  odule *pMod = (M
339c0 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 48 61  odule *)sqliteHa
339c1 73 68 44 61 74 61 28 69 29 3b 0a 20 20 20 20 69  shData(i);.    i
339c2 66 28 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f  f( pMod->xDestro
339c3 79 20 29 7b 0a 20 20 20 20 20 20 70 4d 6f 64 2d  y ){.      pMod-
339c4 3e 78 44 65 73 74 72 6f 79 28 70 4d 6f 64 2d 3e  >xDestroy(pMod->
339c5 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pAux);.    }.   
339c6 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
339c7 62 2c 20 70 4d 6f 64 29 3b 0a 20 20 7d 0a 20 20  b, pMod);.  }.  
339c8 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
339c9 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 0a  (&db->aModule);.
339ca 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65  #endif..  sqlite
339cb 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
339cc 45 5f 4f 4b 2c 20 30 29 3b 20 2f 2a 20 44 65 61  E_OK, 0); /* Dea
339cd 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 63 61 63  llocates any cac
339ce 68 65 64 20 65 72 72 6f 72 20 73 74 72 69 6e 67  hed error string
339cf 73 2e 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  s. */.  if( db->
339d0 70 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  pErr ){.    sqli
339d1 74 65 33 56 61 6c 75 65 46 72 65 65 28 64 62 2d  te3ValueFree(db-
339d2 3e 70 45 72 72 29 3b 0a 20 20 7d 0a 20 20 73 71  >pErr);.  }.  sq
339d3 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73  lite3CloseExtens
339d4 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20 64 62 2d  ions(db);..  db-
339d5 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
339d6 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 0a 20 20  MAGIC_ERROR;..  
339d7 2f 2a 20 54 68 65 20 74 65 6d 70 2d 64 61 74 61  /* The temp-data
339d8 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 61  base schema is a
339d9 6c 6c 6f 63 61 74 65 64 20 64 69 66 66 65 72 65  llocated differe
339da 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 6f 74  ntly from the ot
339db 68 65 72 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20  her schema.  ** 
339dc 6f 62 6a 65 63 74 73 20 28 75 73 69 6e 67 20 73  objects (using s
339dd 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 64 69  qliteMalloc() di
339de 72 65 63 74 6c 79 2c 20 69 6e 73 74 65 61 64 20  rectly, instead 
339df 6f 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  of sqlite3BtreeS
339e0 63 68 65 6d 61 28 29 29 2e 0a 20 20 2a 2a 20 53  chema())..  ** S
339e1 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65  o it needs to be
339e2 20 66 72 65 65 64 20 68 65 72 65 2e 20 54 6f 64   freed here. Tod
339e3 6f 3a 20 57 68 79 20 6e 6f 74 20 72 6f 6c 6c 20  o: Why not roll 
339e4 74 68 65 20 74 65 6d 70 20 73 63 68 65 6d 61 20  the temp schema 
339e5 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61  into.  ** the sa
339e6 6d 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  me sqliteMalloc(
339e7 29 20 61 73 20 74 68 65 20 6f 6e 65 20 74 68 61  ) as the one tha
339e8 74 20 61 6c 6c 6f 63 61 74 65 73 20 74 68 65 20  t allocates the 
339e9 64 61 74 61 62 61 73 65 20 0a 20 20 2a 2a 20 73  database .  ** s
339ea 74 72 75 63 74 75 72 65 3f 0a 20 20 2a 2f 0a 20  tructure?.  */. 
339eb 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
339ec 62 2c 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  b, db->aDb[1].pS
339ed 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65  chema);.  sqlite
339ee 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
339ef 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e  ->mutex);.  db->
339f0 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d  magic = SQLITE_M
339f1 41 47 49 43 5f 43 4c 4f 53 45 44 3b 0a 20 20 73  AGIC_CLOSED;.  s
339f2 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65  qlite3_mutex_fre
339f3 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
339f4 61 73 73 65 72 74 28 20 64 62 2d 3e 6c 6f 6f 6b  assert( db->look
339f5 61 73 69 64 65 2e 6e 4f 75 74 3d 3d 30 20 29 3b  aside.nOut==0 );
339f6 20 20 2f 2a 20 46 61 69 6c 73 20 6f 6e 20 61 20    /* Fails on a 
339f7 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
339f8 20 6c 65 61 6b 20 2a 2f 0a 20 20 69 66 28 20 64   leak */.  if( d
339f9 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61  b->lookaside.bMa
339fa 6c 6c 6f 63 65 64 20 29 7b 0a 20 20 20 20 73 71  lloced ){.    sq
339fb 6c 69 74 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c  lite3_free(db->l
339fc 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74 29  ookaside.pStart)
339fd 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
339fe 66 72 65 65 28 64 62 29 3b 0a 20 20 72 65 74 75  free(db);.  retu
339ff 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
33a00 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20  ./*.** Rollback 
33a01 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  all database fil
33a02 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
33a03 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
33a04 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71  e3RollbackAll(sq
33a05 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e  lite3 *db){.  in
33a06 74 20 69 3b 0a 20 20 69 6e 74 20 69 6e 54 72 61  t i;.  int inTra
33a07 6e 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  ns = 0;.  assert
33a08 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
33a09 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
33a0a 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  );.  sqlite3Begi
33a0b 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
33a0c 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
33a0d 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
33a0e 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   if( db->aDb[i].
33a0f 70 42 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28  pBt ){.      if(
33a10 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
33a11 6e 54 72 61 6e 73 28 64 62 2d 3e 61 44 62 5b 69  nTrans(db->aDb[i
33a12 5d 2e 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  ].pBt) ){.      
33a13 20 20 69 6e 54 72 61 6e 73 20 3d 20 31 3b 0a 20    inTrans = 1;. 
33a14 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
33a15 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63  ite3BtreeRollbac
33a16 6b 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  k(db->aDb[i].pBt
33a17 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62  );.      db->aDb
33a18 5b 69 5d 2e 69 6e 54 72 61 6e 73 20 3d 20 30 3b  [i].inTrans = 0;
33a19 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
33a1a 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b  ite3VtabRollback
33a1b 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  (db);.  sqlite3E
33a1c 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
33a1d 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61  ;..  if( db->fla
33a1e 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  gs&SQLITE_Intern
33a1f 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 73  Changes ){.    s
33a20 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
33a21 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
33a22 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  b);.    sqlite3R
33a23 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
33a24 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a  ma(db, 0);.  }..
33a25 20 20 2f 2a 20 41 6e 79 20 64 65 66 65 72 72 65    /* Any deferre
33a26 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  d constraint vio
33a27 6c 61 74 69 6f 6e 73 20 68 61 76 65 20 6e 6f 77  lations have now
33a28 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 20   been resolved. 
33a29 2a 2f 0a 20 20 64 62 2d 3e 6e 44 65 66 65 72 72  */.  db->nDeferr
33a2a 65 64 43 6f 6e 73 20 3d 20 30 3b 0a 0a 20 20 2f  edCons = 0;..  /
33a2b 2a 20 49 66 20 6f 6e 65 20 68 61 73 20 62 65 65  * If one has bee
33a2c 6e 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 69 6e  n configured, in
33a2d 76 6f 6b 65 20 74 68 65 20 72 6f 6c 6c 62 61 63  voke the rollbac
33a2e 6b 2d 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20  k-hook callback 
33a2f 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 52 6f  */.  if( db->xRo
33a30 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 20 26  llbackCallback &
33a31 26 20 28 69 6e 54 72 61 6e 73 20 7c 7c 20 21 64  & (inTrans || !d
33a32 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 29 20 29  b->autoCommit) )
33a33 7b 0a 20 20 20 20 64 62 2d 3e 78 52 6f 6c 6c 62  {.    db->xRollb
33a34 61 63 6b 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e  ackCallback(db->
33a35 70 52 6f 6c 6c 62 61 63 6b 41 72 67 29 3b 0a 20  pRollbackArg);. 
33a36 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
33a37 72 6e 20 61 20 73 74 61 74 69 63 20 73 74 72 69  rn a static stri
33a38 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62 65  ng that describe
33a39 73 20 74 68 65 20 6b 69 6e 64 20 6f 66 20 65 72  s the kind of er
33a3a 72 6f 72 20 73 70 65 63 69 66 69 65 64 20 69 6e  ror specified in
33a3b 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74   the.** argument
33a3c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
33a3d 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
33a3e 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 69 6e  sqlite3ErrStr(in
33a3f 74 20 72 63 29 7b 0a 20 20 73 74 61 74 69 63 20  t rc){.  static 
33a40 63 6f 6e 73 74 20 63 68 61 72 2a 20 63 6f 6e 73  const char* cons
33a41 74 20 61 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20 20  t aMsg[] = {.   
33a42 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20   /* SQLITE_OK   
33a43 20 20 20 20 20 20 20 2a 2f 20 22 6e 6f 74 20 61         */ "not a
33a44 6e 20 65 72 72 6f 72 22 2c 0a 20 20 20 20 2f 2a  n error",.    /*
33a45 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20 20   SQLITE_ERROR   
33a46 20 20 20 20 2a 2f 20 22 53 51 4c 20 6c 6f 67 69      */ "SQL logi
33a47 63 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 69  c error or missi
33a48 6e 67 20 64 61 74 61 62 61 73 65 22 2c 0a 20 20  ng database",.  
33a49 20 20 2f 2a 20 53 51 4c 49 54 45 5f 49 4e 54 45    /* SQLITE_INTE
33a4a 52 4e 41 4c 20 20 20 20 2a 2f 20 30 2c 0a 20 20  RNAL    */ 0,.  
33a4b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 50 45 52 4d    /* SQLITE_PERM
33a4c 20 20 20 20 20 20 20 20 2a 2f 20 22 61 63 63 65          */ "acce
33a4d 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 64 65  ss permission de
33a4e 6e 69 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51  nied",.    /* SQ
33a4f 4c 49 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20  LITE_ABORT      
33a50 20 2a 2f 20 22 63 61 6c 6c 62 61 63 6b 20 72 65   */ "callback re
33a51 71 75 65 73 74 65 64 20 71 75 65 72 79 20 61 62  quested query ab
33a52 6f 72 74 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ort",.    /* SQL
33a53 49 54 45 5f 42 55 53 59 20 20 20 20 20 20 20 20  ITE_BUSY        
33a54 2a 2f 20 22 64 61 74 61 62 61 73 65 20 69 73 20  */ "database is 
33a55 6c 6f 63 6b 65 64 22 2c 0a 20 20 20 20 2f 2a 20  locked",.    /* 
33a56 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20  SQLITE_LOCKED   
33a57 20 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20     */ "database 
33a58 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 22  table is locked"
33a59 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33a5a 4e 4f 4d 45 4d 20 20 20 20 20 20 20 2a 2f 20 22  NOMEM       */ "
33a5b 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 0a  out of memory",.
33a5c 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 52 45      /* SQLITE_RE
33a5d 41 44 4f 4e 4c 59 20 20 20 20 2a 2f 20 22 61 74  ADONLY    */ "at
33a5e 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61  tempt to write a
33a5f 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 62 61   readonly databa
33a60 73 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  se",.    /* SQLI
33a61 54 45 5f 49 4e 54 45 52 52 55 50 54 20 20 20 2a  TE_INTERRUPT   *
33a62 2f 20 22 69 6e 74 65 72 72 75 70 74 65 64 22 2c  / "interrupted",
33a63 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 49  .    /* SQLITE_I
33a64 4f 45 52 52 20 20 20 20 20 20 20 2a 2f 20 22 64  OERR       */ "d
33a65 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 22 2c 0a  isk I/O error",.
33a66 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f      /* SQLITE_CO
33a67 52 52 55 50 54 20 20 20 20 20 2a 2f 20 22 64 61  RRUPT     */ "da
33a68 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67  tabase disk imag
33a69 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 22 2c  e is malformed",
33a6a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e  .    /* SQLITE_N
33a6b 4f 54 46 4f 55 4e 44 20 20 20 20 2a 2f 20 30 2c  OTFOUND    */ 0,
33a6c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46  .    /* SQLITE_F
33a6d 55 4c 4c 20 20 20 20 20 20 20 20 2a 2f 20 22 64  ULL        */ "d
33a6e 61 74 61 62 61 73 65 20 6f 72 20 64 69 73 6b 20  atabase or disk 
33a6f 69 73 20 66 75 6c 6c 22 2c 0a 20 20 20 20 2f 2a  is full",.    /*
33a70 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
33a71 20 20 20 20 2a 2f 20 22 75 6e 61 62 6c 65 20 74      */ "unable t
33a72 6f 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20  o open database 
33a73 66 69 6c 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51  file",.    /* SQ
33a74 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20  LITE_PROTOCOL   
33a75 20 2a 2f 20 30 2c 0a 20 20 20 20 2f 2a 20 53 51   */ 0,.    /* SQ
33a76 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20 20  LITE_EMPTY      
33a77 20 2a 2f 20 22 74 61 62 6c 65 20 63 6f 6e 74 61   */ "table conta
33a78 69 6e 73 20 6e 6f 20 64 61 74 61 22 2c 0a 20 20  ins no data",.  
33a79 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 43 48 45    /* SQLITE_SCHE
33a7a 4d 41 20 20 20 20 20 20 2a 2f 20 22 64 61 74 61  MA      */ "data
33a7b 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20  base schema has 
33a7c 63 68 61 6e 67 65 64 22 2c 0a 20 20 20 20 2f 2a  changed",.    /*
33a7d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 20   SQLITE_TOOBIG  
33a7e 20 20 20 20 2a 2f 20 22 73 74 72 69 6e 67 20 6f      */ "string o
33a7f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 2c  r blob too big",
33a80 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43  .    /* SQLITE_C
33a81 4f 4e 53 54 52 41 49 4e 54 20 20 2a 2f 20 22 63  ONSTRAINT  */ "c
33a82 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
33a83 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
33a84 5f 4d 49 53 4d 41 54 43 48 20 20 20 20 2a 2f 20  _MISMATCH    */ 
33a85 22 64 61 74 61 74 79 70 65 20 6d 69 73 6d 61 74  "datatype mismat
33a86 63 68 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  ch",.    /* SQLI
33a87 54 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20 2a  TE_MISUSE      *
33a88 2f 20 22 6c 69 62 72 61 72 79 20 72 6f 75 74 69  / "library routi
33a89 6e 65 20 63 61 6c 6c 65 64 20 6f 75 74 20 6f 66  ne called out of
33a8a 20 73 65 71 75 65 6e 63 65 22 2c 0a 20 20 20 20   sequence",.    
33a8b 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20  /* SQLITE_NOLFS 
33a8c 20 20 20 20 20 20 2a 2f 20 22 6c 61 72 67 65 20        */ "large 
33a8d 66 69 6c 65 20 73 75 70 70 6f 72 74 20 69 73 20  file support is 
33a8e 64 69 73 61 62 6c 65 64 22 2c 0a 20 20 20 20 2f  disabled",.    /
33a8f 2a 20 53 51 4c 49 54 45 5f 41 55 54 48 20 20 20  * SQLITE_AUTH   
33a90 20 20 20 20 20 2a 2f 20 22 61 75 74 68 6f 72 69       */ "authori
33a91 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 22 2c 0a  zation denied",.
33a92 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 4f      /* SQLITE_FO
33a93 52 4d 41 54 20 20 20 20 20 20 2a 2f 20 22 61 75  RMAT      */ "au
33a94 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
33a95 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 22 2c 0a   format error",.
33a96 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 52 41      /* SQLITE_RA
33a97 4e 47 45 20 20 20 20 20 20 20 2a 2f 20 22 62 69  NGE       */ "bi
33a98 6e 64 20 6f 72 20 63 6f 6c 75 6d 6e 20 69 6e 64  nd or column ind
33a99 65 78 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 22  ex out of range"
33a9a 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33a9b 4e 4f 54 41 44 42 20 20 20 20 20 20 2a 2f 20 22  NOTADB      */ "
33a9c 66 69 6c 65 20 69 73 20 65 6e 63 72 79 70 74 65  file is encrypte
33a9d 64 20 6f 72 20 69 73 20 6e 6f 74 20 61 20 64 61  d or is not a da
33a9e 74 61 62 61 73 65 22 2c 0a 20 20 7d 3b 0a 20 20  tabase",.  };.  
33a9f 72 63 20 26 3d 20 30 78 66 66 3b 0a 20 20 69 66  rc &= 0xff;.  if
33aa0 28 20 41 4c 57 41 59 53 28 72 63 3e 3d 30 29 20  ( ALWAYS(rc>=0) 
33aa1 26 26 20 72 63 3c 28 69 6e 74 29 28 73 69 7a 65  && rc<(int)(size
33aa2 6f 66 28 61 4d 73 67 29 2f 73 69 7a 65 6f 66 28  of(aMsg)/sizeof(
33aa3 61 4d 73 67 5b 30 5d 29 29 20 26 26 20 61 4d 73  aMsg[0])) && aMs
33aa4 67 5b 72 63 5d 21 3d 30 20 29 7b 0a 20 20 20 20  g[rc]!=0 ){.    
33aa5 72 65 74 75 72 6e 20 61 4d 73 67 5b 72 63 5d 3b  return aMsg[rc];
33aa6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
33aa7 74 75 72 6e 20 22 75 6e 6b 6e 6f 77 6e 20 65 72  turn "unknown er
33aa8 72 6f 72 22 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ror";.  }.}../*.
33aa9 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
33aaa 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 62 75 73  implements a bus
33aab 79 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20  y callback that 
33aac 73 6c 65 65 70 73 20 61 6e 64 20 74 72 69 65 73  sleeps and tries
33aad 0a 2a 2a 20 61 67 61 69 6e 20 75 6e 74 69 6c 20  .** again until 
33aae 61 20 74 69 6d 65 6f 75 74 20 76 61 6c 75 65 20  a timeout value 
33aaf 69 73 20 72 65 61 63 68 65 64 2e 20 20 54 68 65  is reached.  The
33ab0 20 74 69 6d 65 6f 75 74 20 76 61 6c 75 65 20 69   timeout value i
33ab1 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20  s.** an integer 
33ab2 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73  number of millis
33ab3 65 63 6f 6e 64 73 20 70 61 73 73 65 64 20 69 6e  econds passed in
33ab4 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   as the first.**
33ab5 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74   argument..*/.st
33ab6 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 44  atic int sqliteD
33ab7 65 66 61 75 6c 74 42 75 73 79 43 61 6c 6c 62 61  efaultBusyCallba
33ab8 63 6b 28 0a 20 76 6f 69 64 20 2a 70 74 72 2c 20  ck(. void *ptr, 
33ab9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33aba 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
33abb 74 69 6f 6e 20 2a 2f 0a 20 69 6e 74 20 63 6f 75  tion */. int cou
33abc 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
33abd 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
33abe 69 6d 65 73 20 74 61 62 6c 65 20 68 61 73 20 62  imes table has b
33abf 65 65 6e 20 62 75 73 79 20 2a 2f 0a 29 7b 0a 23  een busy */.){.#
33ac0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
33ac1 20 7c 7c 20 28 64 65 66 69 6e 65 64 28 48 41 56   || (defined(HAV
33ac2 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 56  E_USLEEP) && HAV
33ac3 45 5f 55 53 4c 45 45 50 29 0a 20 20 73 74 61 74  E_USLEEP).  stat
33ac4 69 63 20 63 6f 6e 73 74 20 75 38 20 64 65 6c 61  ic const u8 dela
33ac5 79 73 5b 5d 20 3d 0a 20 20 20 20 20 7b 20 31 2c  ys[] =.     { 1,
33ac6 20 32 2c 20 35 2c 20 31 30 2c 20 31 35 2c 20 32   2, 5, 10, 15, 2
33ac7 30 2c 20 32 35 2c 20 32 35 2c 20 20 32 35 2c 20  0, 25, 25,  25, 
33ac8 20 35 30 2c 20 20 35 30 2c 20 31 30 30 20 7d 3b   50,  50, 100 };
33ac9 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
33aca 75 38 20 74 6f 74 61 6c 73 5b 5d 20 3d 0a 20 20  u8 totals[] =.  
33acb 20 20 20 7b 20 30 2c 20 31 2c 20 33 2c 20 20 38     { 0, 1, 3,  8
33acc 2c 20 31 38 2c 20 33 33 2c 20 35 33 2c 20 37 38  , 18, 33, 53, 78
33acd 2c 20 31 30 33 2c 20 31 32 38 2c 20 31 37 38 2c  , 103, 128, 178,
33ace 20 32 32 38 20 7d 3b 0a 23 20 64 65 66 69 6e 65   228 };.# define
33acf 20 4e 44 45 4c 41 59 20 28 73 69 7a 65 6f 66 28   NDELAY (sizeof(
33ad0 64 65 6c 61 79 73 29 2f 73 69 7a 65 6f 66 28 64  delays)/sizeof(d
33ad1 65 6c 61 79 73 5b 30 5d 29 29 0a 20 20 73 71 6c  elays[0])).  sql
33ad2 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69  ite3 *db = (sqli
33ad3 74 65 33 20 2a 29 70 74 72 3b 0a 20 20 69 6e 74  te3 *)ptr;.  int
33ad4 20 74 69 6d 65 6f 75 74 20 3d 20 64 62 2d 3e 62   timeout = db->b
33ad5 75 73 79 54 69 6d 65 6f 75 74 3b 0a 20 20 69 6e  usyTimeout;.  in
33ad6 74 20 64 65 6c 61 79 2c 20 70 72 69 6f 72 3b 0a  t delay, prior;.
33ad7 0a 20 20 61 73 73 65 72 74 28 20 63 6f 75 6e 74  .  assert( count
33ad8 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 63 6f 75  >=0 );.  if( cou
33ad9 6e 74 20 3c 20 4e 44 45 4c 41 59 20 29 7b 0a 20  nt < NDELAY ){. 
33ada 20 20 20 64 65 6c 61 79 20 3d 20 64 65 6c 61 79     delay = delay
33adb 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 20 20 70 72  s[count];.    pr
33adc 69 6f 72 20 3d 20 74 6f 74 61 6c 73 5b 63 6f 75  ior = totals[cou
33add 6e 74 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  nt];.  }else{.  
33ade 20 20 64 65 6c 61 79 20 3d 20 64 65 6c 61 79 73    delay = delays
33adf 5b 4e 44 45 4c 41 59 2d 31 5d 3b 0a 20 20 20 20  [NDELAY-1];.    
33ae0 70 72 69 6f 72 20 3d 20 74 6f 74 61 6c 73 5b 4e  prior = totals[N
33ae1 44 45 4c 41 59 2d 31 5d 20 2b 20 64 65 6c 61 79  DELAY-1] + delay
33ae2 2a 28 63 6f 75 6e 74 2d 28 4e 44 45 4c 41 59 2d  *(count-(NDELAY-
33ae3 31 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  1));.  }.  if( p
33ae4 72 69 6f 72 20 2b 20 64 65 6c 61 79 20 3e 20 74  rior + delay > t
33ae5 69 6d 65 6f 75 74 20 29 7b 0a 20 20 20 20 64 65  imeout ){.    de
33ae6 6c 61 79 20 3d 20 74 69 6d 65 6f 75 74 20 2d 20  lay = timeout - 
33ae7 70 72 69 6f 72 3b 0a 20 20 20 20 69 66 28 20 64  prior;.    if( d
33ae8 65 6c 61 79 3c 3d 30 20 29 20 72 65 74 75 72 6e  elay<=0 ) return
33ae9 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
33aea 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e 70 56 66  3OsSleep(db->pVf
33aeb 73 2c 20 64 65 6c 61 79 2a 31 30 30 30 29 3b 0a  s, delay*1000);.
33aec 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6c 73    return 1;.#els
33aed 65 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  e.  sqlite3 *db 
33aee 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72  = (sqlite3 *)ptr
33aef 3b 0a 20 20 69 6e 74 20 74 69 6d 65 6f 75 74 20  ;.  int timeout 
33af0 3d 20 28 28 73 71 6c 69 74 65 33 20 2a 29 70 74  = ((sqlite3 *)pt
33af1 72 29 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74 3b  r)->busyTimeout;
33af2 0a 20 20 69 66 28 20 28 63 6f 75 6e 74 2b 31 29  .  if( (count+1)
33af3 2a 31 30 30 30 20 3e 20 74 69 6d 65 6f 75 74 20  *1000 > timeout 
33af4 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
33af5 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73  .  }.  sqlite3Os
33af6 53 6c 65 65 70 28 64 62 2d 3e 70 56 66 73 2c 20  Sleep(db->pVfs, 
33af7 31 30 30 30 30 30 30 29 3b 0a 20 20 72 65 74 75  1000000);.  retu
33af8 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  rn 1;.#endif.}..
33af9 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
33afa 20 67 69 76 65 6e 20 62 75 73 79 20 68 61 6e 64   given busy hand
33afb 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ler..**.** This 
33afc 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
33afd 64 20 77 68 65 6e 20 61 6e 20 6f 70 65 72 61 74  d when an operat
33afe 69 6f 6e 20 66 61 69 6c 65 64 20 77 69 74 68 20  ion failed with 
33aff 61 20 6c 6f 63 6b 2e 0a 2a 2a 20 49 66 20 74 68  a lock..** If th
33b00 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
33b01 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  ns non-zero, the
33b02 20 6c 6f 63 6b 20 69 73 20 72 65 74 72 69 65 64   lock is retried
33b03 2e 20 20 49 66 20 69 74 0a 2a 2a 20 72 65 74 75  .  If it.** retu
33b04 72 6e 73 20 30 2c 20 74 68 65 20 6f 70 65 72 61  rns 0, the opera
33b05 74 69 6f 6e 20 61 62 6f 72 74 73 20 77 69 74 68  tion aborts with
33b06 20 61 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20   an SQLITE_BUSY 
33b07 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
33b08 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
33b09 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61  ite3InvokeBusyHa
33b0a 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65  ndler(BusyHandle
33b0b 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  r *p){.  int rc;
33b0c 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d  .  if( NEVER(p==
33b0d 30 29 20 7c 7c 20 70 2d 3e 78 46 75 6e 63 3d 3d  0) || p->xFunc==
33b0e 30 20 7c 7c 20 70 2d 3e 6e 42 75 73 79 3c 30 20  0 || p->nBusy<0 
33b0f 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 63  ) return 0;.  rc
33b10 20 3d 20 70 2d 3e 78 46 75 6e 63 28 70 2d 3e 70   = p->xFunc(p->p
33b11 41 72 67 2c 20 70 2d 3e 6e 42 75 73 79 29 3b 0a  Arg, p->nBusy);.
33b12 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20    if( rc==0 ){. 
33b13 20 20 20 70 2d 3e 6e 42 75 73 79 20 3d 20 2d 31     p->nBusy = -1
33b14 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
33b15 2d 3e 6e 42 75 73 79 2b 2b 3b 0a 20 20 7d 0a 20  ->nBusy++;.  }. 
33b16 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a   return rc; .}..
33b17 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
33b18 6e 65 20 73 65 74 73 20 74 68 65 20 62 75 73 79  ne sets the busy
33b19 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e   callback for an
33b1a 20 53 71 6c 69 74 65 20 64 61 74 61 62 61 73 65   Sqlite database
33b1b 20 74 6f 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e   to the.** given
33b1c 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
33b1d 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76 65  on with the give
33b1e 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53  n argument..*/.S
33b1f 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
33b20 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
33b21 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  er(.  sqlite3 *d
33b22 62 2c 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79  b,.  int (*xBusy
33b23 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 0a 20 20  )(void*,int),.  
33b24 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20  void *pArg.){.  
33b25 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
33b26 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
33b27 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65    db->busyHandle
33b28 72 2e 78 46 75 6e 63 20 3d 20 78 42 75 73 79 3b  r.xFunc = xBusy;
33b29 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c  .  db->busyHandl
33b2a 65 72 2e 70 41 72 67 20 3d 20 70 41 72 67 3b 0a  er.pArg = pArg;.
33b2b 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65    db->busyHandle
33b2c 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20 73  r.nBusy = 0;.  s
33b2d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
33b2e 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
33b2f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
33b30 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  K;.}..#ifndef SQ
33b31 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45  LITE_OMIT_PROGRE
33b32 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 2f 2a 0a 2a  SS_CALLBACK./*.*
33b33 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
33b34 65 74 73 20 74 68 65 20 70 72 6f 67 72 65 73 73  ets the progress
33b35 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e   callback for an
33b36 20 53 71 6c 69 74 65 20 64 61 74 61 62 61 73 65   Sqlite database
33b37 20 74 6f 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e   to the.** given
33b38 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
33b39 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76 65  on with the give
33b3a 6e 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 20  n argument. The 
33b3b 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
33b3c 6b 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 69 6e 76  k will.** be inv
33b3d 6f 6b 65 64 20 65 76 65 72 79 20 6e 4f 70 73 20  oked every nOps 
33b3e 6f 70 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49  opcodes..*/.SQLI
33b3f 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
33b40 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e  te3_progress_han
33b41 64 6c 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20  dler(.  sqlite3 
33b42 2a 64 62 2c 20 0a 20 20 69 6e 74 20 6e 4f 70 73  *db, .  int nOps
33b43 2c 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 72  ,.  int (*xProgr
33b44 65 73 73 29 28 76 6f 69 64 2a 29 2c 20 0a 20 20  ess)(void*), .  
33b45 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20  void *pArg.){.  
33b46 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
33b47 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
33b48 20 20 69 66 28 20 6e 4f 70 73 3e 30 20 29 7b 0a    if( nOps>0 ){.
33b49 20 20 20 20 64 62 2d 3e 78 50 72 6f 67 72 65 73      db->xProgres
33b4a 73 20 3d 20 78 50 72 6f 67 72 65 73 73 3b 0a 20  s = xProgress;. 
33b4b 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73     db->nProgress
33b4c 4f 70 73 20 3d 20 6e 4f 70 73 3b 0a 20 20 20 20  Ops = nOps;.    
33b4d 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67  db->pProgressArg
33b4e 20 3d 20 70 41 72 67 3b 0a 20 20 7d 65 6c 73 65   = pArg;.  }else
33b4f 7b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 67 72  {.    db->xProgr
33b50 65 73 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  ess = 0;.    db-
33b51 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20  >nProgressOps = 
33b52 30 3b 0a 20 20 20 20 64 62 2d 3e 70 50 72 6f 67  0;.    db->pProg
33b53 72 65 73 73 41 72 67 20 3d 20 30 3b 0a 20 20 7d  ressArg = 0;.  }
33b54 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
33b55 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
33b56 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  );.}.#endif.../*
33b57 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
33b58 20 69 6e 73 74 61 6c 6c 73 20 61 20 64 65 66 61   installs a defa
33b59 75 6c 74 20 62 75 73 79 20 68 61 6e 64 6c 65 72  ult busy handler
33b5a 20 74 68 61 74 20 77 61 69 74 73 20 66 6f 72 20   that waits for 
33b5b 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 65 64  the.** specified
33b5c 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69   number of milli
33b5d 73 65 63 6f 6e 64 73 20 62 65 66 6f 72 65 20 72  seconds before r
33b5e 65 74 75 72 6e 69 6e 67 20 30 2e 0a 2a 2f 0a 53  eturning 0..*/.S
33b5f 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
33b60 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f  lite3_busy_timeo
33b61 75 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ut(sqlite3 *db, 
33b62 69 6e 74 20 6d 73 29 7b 0a 20 20 69 66 28 20 6d  int ms){.  if( m
33b63 73 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 62  s>0 ){.    db->b
33b64 75 73 79 54 69 6d 65 6f 75 74 20 3d 20 6d 73 3b  usyTimeout = ms;
33b65 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 75 73  .    sqlite3_bus
33b66 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20 73 71  y_handler(db, sq
33b67 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73 79 43  liteDefaultBusyC
33b68 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64 2a 29  allback, (void*)
33b69 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  db);.  }else{.  
33b6a 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68    sqlite3_busy_h
33b6b 61 6e 64 6c 65 72 28 64 62 2c 20 30 2c 20 30 29  andler(db, 0, 0)
33b6c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
33b6d 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
33b6e 2a 2a 20 43 61 75 73 65 20 61 6e 79 20 70 65 6e  ** Cause any pen
33b6f 64 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 74  ding operation t
33b70 6f 20 73 74 6f 70 20 61 74 20 69 74 73 20 65 61  o stop at its ea
33b71 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75 6e 69  rliest opportuni
33b72 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ty..*/.SQLITE_AP
33b73 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 69  I void sqlite3_i
33b74 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65 33  nterrupt(sqlite3
33b75 20 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 75 31 2e   *db){.  db->u1.
33b76 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20  isInterrupted = 
33b77 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  1;.}.../*.** Thi
33b78 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65 78  s function is ex
33b79 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 61  actly the same a
33b7a 73 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  s sqlite3_create
33b7b 5f 66 75 6e 63 74 69 6f 6e 28 29 2c 20 65 78 63  _function(), exc
33b7c 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 74 20 69  ept.** that it i
33b7d 73 20 64 65 73 69 67 6e 65 64 20 74 6f 20 62 65  s designed to be
33b7e 20 63 61 6c 6c 65 64 20 62 79 20 69 6e 74 65 72   called by inter
33b7f 6e 61 6c 20 63 6f 64 65 2e 20 54 68 65 20 64 69  nal code. The di
33b80 66 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a 20 74  fference is.** t
33b81 68 61 74 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28  hat if a malloc(
33b82 29 20 66 61 69 6c 73 20 69 6e 20 73 71 6c 69 74  ) fails in sqlit
33b83 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
33b84 6f 6e 28 29 2c 20 61 6e 20 65 72 72 6f 72 20 63  on(), an error c
33b85 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  ode.** is return
33b86 65 64 20 61 6e 64 20 74 68 65 20 6d 61 6c 6c 6f  ed and the mallo
33b87 63 46 61 69 6c 65 64 20 66 6c 61 67 20 63 6c 65  cFailed flag cle
33b88 61 72 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45  ared. .*/.SQLITE
33b89 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
33b8a 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 0a  ite3CreateFunc(.
33b8b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
33b8c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75   const char *zFu
33b8d 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e  nctionName,.  in
33b8e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 6e  t nArg,.  int en
33b8f 63 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72  c,.  void *pUser
33b90 44 61 74 61 2c 0a 20 20 76 6f 69 64 20 28 2a 78  Data,.  void (*x
33b91 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
33b92 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
33b93 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20  e3_value **),.  
33b94 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
33b95 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
33b96 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
33b97 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78   **),.  void (*x
33b98 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
33b99 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 46 75  ontext*).){.  Fu
33b9a 6e 63 44 65 66 20 2a 70 3b 0a 20 20 69 6e 74 20  ncDef *p;.  int 
33b9b 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73 65 72 74  nName;..  assert
33b9c 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
33b9d 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
33b9e 29 3b 0a 20 20 69 66 28 20 7a 46 75 6e 63 74 69  );.  if( zFuncti
33b9f 6f 6e 4e 61 6d 65 3d 3d 30 20 7c 7c 0a 20 20 20  onName==0 ||.   
33ba0 20 20 20 28 78 46 75 6e 63 20 26 26 20 28 78 46     (xFunc && (xF
33ba1 69 6e 61 6c 20 7c 7c 20 78 53 74 65 70 29 29 20  inal || xStep)) 
33ba2 7c 7c 20 0a 20 20 20 20 20 20 28 21 78 46 75 6e  || .      (!xFun
33ba3 63 20 26 26 20 28 78 46 69 6e 61 6c 20 26 26 20  c && (xFinal && 
33ba4 21 78 53 74 65 70 29 29 20 7c 7c 0a 20 20 20 20  !xStep)) ||.    
33ba5 20 20 28 21 78 46 75 6e 63 20 26 26 20 28 21 78    (!xFunc && (!x
33ba6 46 69 6e 61 6c 20 26 26 20 78 53 74 65 70 29 29  Final && xStep))
33ba7 20 7c 7c 0a 20 20 20 20 20 20 28 6e 41 72 67 3c   ||.      (nArg<
33ba8 2d 31 20 7c 7c 20 6e 41 72 67 3e 53 51 4c 49 54  -1 || nArg>SQLIT
33ba9 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41  E_MAX_FUNCTION_A
33baa 52 47 29 20 7c 7c 0a 20 20 20 20 20 20 28 32 35  RG) ||.      (25
33bab 35 3c 28 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  5<(nName = sqlit
33bac 65 33 53 74 72 6c 65 6e 33 30 28 20 7a 46 75 6e  e3Strlen30( zFun
33bad 63 74 69 6f 6e 4e 61 6d 65 29 29 29 20 29 7b 0a  ctionName))) ){.
33bae 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
33baf 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
33bb0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
33bb1 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a 20  OMIT_UTF16.  /* 
33bb2 49 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20  If SQLITE_UTF16 
33bb3 69 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20  is specified as 
33bb4 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 74 79 70  the encoding typ
33bb5 65 2c 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 69  e, transform thi
33bb6 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66  s.  ** to one of
33bb7 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20   SQLITE_UTF16LE 
33bb8 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  or SQLITE_UTF16B
33bb9 45 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a  E using the.  **
33bba 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
33bbb 49 56 45 20 6d 61 63 72 6f 2e 20 53 51 4c 49 54  IVE macro. SQLIT
33bbc 45 5f 55 54 46 31 36 20 69 73 20 6e 6f 74 20 75  E_UTF16 is not u
33bbd 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a  sed internally..
33bbe 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 53 51 4c    **.  ** If SQL
33bbf 49 54 45 5f 41 4e 59 20 69 73 20 73 70 65 63 69  ITE_ANY is speci
33bc0 66 69 65 64 2c 20 61 64 64 20 74 68 72 65 65 20  fied, add three 
33bc1 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  versions of the 
33bc2 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f  function.  ** to
33bc3 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e   the hash table.
33bc4 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 6e 63 3d  .  */.  if( enc=
33bc5 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20 29 7b  =SQLITE_UTF16 ){
33bc6 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c 49 54  .    enc = SQLIT
33bc7 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20  E_UTF16NATIVE;. 
33bc8 20 7d 65 6c 73 65 20 69 66 28 20 65 6e 63 3d 3d   }else if( enc==
33bc9 53 51 4c 49 54 45 5f 41 4e 59 20 29 7b 0a 20 20  SQLITE_ANY ){.  
33bca 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63    int rc;.    rc
33bcb 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65   = sqlite3Create
33bcc 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69  Func(db, zFuncti
33bcd 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51  onName, nArg, SQ
33bce 4c 49 54 45 5f 55 54 46 38 2c 0a 20 20 20 20 20  LITE_UTF8,.     
33bcf 20 20 20 20 70 55 73 65 72 44 61 74 61 2c 20 78      pUserData, x
33bd0 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69  Func, xStep, xFi
33bd1 6e 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63  nal);.    if( rc
33bd2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
33bd3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
33bd4 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
33bd5 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e  zFunctionName, n
33bd6 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Arg, SQLITE_UTF1
33bd7 36 4c 45 2c 0a 20 20 20 20 20 20 20 20 20 20 70  6LE,.          p
33bd8 55 73 65 72 44 61 74 61 2c 20 78 46 75 6e 63 2c  UserData, xFunc,
33bd9 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b   xStep, xFinal);
33bda 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
33bdb 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
33bdc 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
33bdd 0a 20 20 20 20 7d 0a 20 20 20 20 65 6e 63 20 3d  .    }.    enc =
33bde 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b   SQLITE_UTF16BE;
33bdf 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 65 6e 63  .  }.#else.  enc
33be0 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
33be1 23 65 6e 64 69 66 0a 20 20 0a 20 20 2f 2a 20 43  #endif.  .  /* C
33be2 68 65 63 6b 20 69 66 20 61 6e 20 65 78 69 73 74  heck if an exist
33be3 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ing function is 
33be4 62 65 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e  being overridden
33be5 20 6f 72 20 64 65 6c 65 74 65 64 2e 20 49 66 20   or deleted. If 
33be6 73 6f 2c 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  so,.  ** and the
33be7 72 65 20 61 72 65 20 61 63 74 69 76 65 20 56 4d  re are active VM
33be8 73 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 53  s, then return S
33be9 51 4c 49 54 45 5f 42 55 53 59 2e 20 49 66 20 61  QLITE_BUSY. If a
33bea 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69   function.  ** i
33beb 73 20 62 65 69 6e 67 20 6f 76 65 72 72 69 64 64  s being overridd
33bec 65 6e 2f 64 65 6c 65 74 65 64 20 62 75 74 20 74  en/deleted but t
33bed 68 65 72 65 20 61 72 65 20 6e 6f 20 61 63 74 69  here are no acti
33bee 76 65 20 56 4d 73 2c 20 61 6c 6c 6f 77 20 74 68  ve VMs, allow th
33bef 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e  e.  ** operation
33bf0 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 62 75 74   to continue but
33bf1 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20   invalidate all 
33bf2 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  precompiled stat
33bf3 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70  ements..  */.  p
33bf4 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
33bf5 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63  nction(db, zFunc
33bf6 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c  tionName, nName,
33bf7 20 6e 41 72 67 2c 20 28 75 38 29 65 6e 63 2c 20   nArg, (u8)enc, 
33bf8 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70  0);.  if( p && p
33bf9 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 65 6e 63 20  ->iPrefEnc==enc 
33bfa 26 26 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67  && p->nArg==nArg
33bfb 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   ){.    if( db->
33bfc 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 7b  activeVdbeCnt ){
33bfd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
33bfe 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42  ror(db, SQLITE_B
33bff 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20 22 75  USY, .        "u
33c00 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 2f  nable to delete/
33c01 6d 6f 64 69 66 79 20 75 73 65 72 2d 66 75 6e 63  modify user-func
33c02 74 69 6f 6e 20 64 75 65 20 74 6f 20 61 63 74 69  tion due to acti
33c03 76 65 20 73 74 61 74 65 6d 65 6e 74 73 22 29 3b  ve statements");
33c04 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
33c05 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
33c06 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
33c07 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
33c08 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
33c09 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
33c0a 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
33c0b 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  b);.    }.  }.. 
33c0c 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64   p = sqlite3Find
33c0d 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75  Function(db, zFu
33c0e 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d  nctionName, nNam
33c0f 65 2c 20 6e 41 72 67 2c 20 28 75 38 29 65 6e 63  e, nArg, (u8)enc
33c10 2c 20 31 29 3b 0a 20 20 61 73 73 65 72 74 28 70  , 1);.  assert(p
33c11 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
33c12 69 6c 65 64 29 3b 0a 20 20 69 66 28 20 21 70 20  iled);.  if( !p 
33c13 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
33c14 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
33c15 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a    p->flags = 0;.
33c16 20 20 70 2d 3e 78 46 75 6e 63 20 3d 20 78 46 75    p->xFunc = xFu
33c17 6e 63 3b 0a 20 20 70 2d 3e 78 53 74 65 70 20 3d  nc;.  p->xStep =
33c18 20 78 53 74 65 70 3b 0a 20 20 70 2d 3e 78 46 69   xStep;.  p->xFi
33c19 6e 61 6c 69 7a 65 20 3d 20 78 46 69 6e 61 6c 3b  nalize = xFinal;
33c1a 0a 20 20 70 2d 3e 70 55 73 65 72 44 61 74 61 20  .  p->pUserData 
33c1b 3d 20 70 55 73 65 72 44 61 74 61 3b 0a 20 20 70  = pUserData;.  p
33c1c 2d 3e 6e 41 72 67 20 3d 20 28 75 31 36 29 6e 41  ->nArg = (u16)nA
33c1d 72 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  rg;.  return SQL
33c1e 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
33c1f 20 43 72 65 61 74 65 20 6e 65 77 20 75 73 65 72   Create new user
33c20 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 53   functions..*/.S
33c21 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
33c22 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
33c23 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33  ction(.  sqlite3
33c24 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68   *db,.  const ch
33c25 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  ar *zFunctionNam
33c26 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20  e,.  int nArg,. 
33c27 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69 64   int enc,.  void
33c28 20 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46   *p,.  void (*xF
33c29 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
33c2a 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
33c2b 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76  3_value **),.  v
33c2c 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
33c2d 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
33c2e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
33c2f 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46  **),.  void (*xF
33c30 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f  inal)(sqlite3_co
33c31 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 69 6e 74  ntext*).){.  int
33c32 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d   rc;.  sqlite3_m
33c33 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
33c34 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 73 71  utex);.  rc = sq
33c35 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28  lite3CreateFunc(
33c36 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  db, zFunctionNam
33c37 65 2c 20 6e 41 72 67 2c 20 65 6e 63 2c 20 70 2c  e, nArg, enc, p,
33c38 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78   xFunc, xStep, x
33c39 46 69 6e 61 6c 29 3b 0a 20 20 72 63 20 3d 20 73  Final);.  rc = s
33c3a 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
33c3b 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  , rc);.  sqlite3
33c3c 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
33c3d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
33c3e 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  n rc;.}..#ifndef
33c3f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
33c40 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  16.SQLITE_API in
33c41 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
33c42 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20 73  _function16(.  s
33c43 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f  qlite3 *db,.  co
33c44 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 74  nst void *zFunct
33c45 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e  ionName,.  int n
33c46 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74  Arg,.  int eText
33c47 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 2c 0a  Rep,.  void *p,.
33c48 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28    void (*xFunc)(
33c49 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
33c4a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
33c4b 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  ue**),.  void (*
33c4c 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63  xStep)(sqlite3_c
33c4d 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
33c4e 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20  te3_value**),.  
33c4f 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73  void (*xFinal)(s
33c50 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
33c51 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
33c52 63 68 61 72 20 2a 7a 46 75 6e 63 38 3b 0a 20 20  char *zFunc8;.  
33c53 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
33c54 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
33c55 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
33c56 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
33c57 20 7a 46 75 6e 63 38 20 3d 20 73 71 6c 69 74 65   zFunc8 = sqlite
33c58 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 46  3Utf16to8(db, zF
33c59 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 2d 31 29  unctionName, -1)
33c5a 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
33c5b 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a  CreateFunc(db, z
33c5c 46 75 6e 63 38 2c 20 6e 41 72 67 2c 20 65 54 65  Func8, nArg, eTe
33c5d 78 74 52 65 70 2c 20 70 2c 20 78 46 75 6e 63 2c  xtRep, p, xFunc,
33c5e 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b   xStep, xFinal);
33c5f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
33c60 28 64 62 2c 20 7a 46 75 6e 63 38 29 3b 0a 20 20  (db, zFunc8);.  
33c61 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
33c62 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73  xit(db, rc);.  s
33c63 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
33c64 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
33c65 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
33c66 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 63  ndif.../*.** Dec
33c67 6c 61 72 65 20 74 68 61 74 20 61 20 66 75 6e 63  lare that a func
33c68 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 6f 76  tion has been ov
33c69 65 72 6c 6f 61 64 65 64 20 62 79 20 61 20 76 69  erloaded by a vi
33c6a 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a  rtual table..**.
33c6b 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69  ** If the functi
33c6c 6f 6e 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  on already exist
33c6d 73 20 61 73 20 61 20 72 65 67 75 6c 61 72 20 67  s as a regular g
33c6e 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 2c 20  lobal function, 
33c6f 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75  then.** this rou
33c70 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
33c71 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f    If the functio
33c72 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  n does not exist
33c73 2c 20 74 68 65 6e 20 63 72 65 61 74 65 0a 2a 2a  , then create.**
33c74 20 61 20 6e 65 77 20 6f 6e 65 20 74 68 61 74 20   a new one that 
33c75 61 6c 77 61 79 73 20 74 68 72 6f 77 73 20 61 20  always throws a 
33c76 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 2e 20  run-time error. 
33c77 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 76 69 72   .**.** When vir
33c78 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 74 65  tual tables inte
33c79 6e 64 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e  nd to provide an
33c7a 20 6f 76 65 72 6c 6f 61 64 65 64 20 66 75 6e 63   overloaded func
33c7b 74 69 6f 6e 2c 20 74 68 65 79 0a 2a 2a 20 73 68  tion, they.** sh
33c7c 6f 75 6c 64 20 63 61 6c 6c 20 74 68 69 73 20 72  ould call this r
33c7d 6f 75 74 69 6e 65 20 74 6f 20 6d 61 6b 65 20 73  outine to make s
33c7e 75 72 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 66  ure the global f
33c7f 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 2e 0a  unction exists..
33c80 2a 2a 20 41 20 67 6c 6f 62 61 6c 20 66 75 6e 63  ** A global func
33c81 74 69 6f 6e 20 6d 75 73 74 20 65 78 69 73 74 20  tion must exist 
33c82 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 6e 61 6d  in order for nam
33c83 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 74 6f 20  e resolution to 
33c84 77 6f 72 6b 0a 2a 2a 20 70 72 6f 70 65 72 6c 79  work.** properly
33c85 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
33c86 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72  int sqlite3_over
33c87 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 0a 20  load_function(. 
33c88 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
33c89 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
33c8a 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 0a 29 7b  e,.  int nArg.){
33c8b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73  .  int nName = s
33c8c 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
33c8d 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b  Name);.  int rc;
33c8e 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
33c8f 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
33c90 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
33c91 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c  FindFunction(db,
33c92 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e   zName, nName, n
33c93 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Arg, SQLITE_UTF8
33c94 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73  , 0)==0 ){.    s
33c95 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
33c96 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41 72 67  (db, zName, nArg
33c97 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a 20  , SQLITE_UTF8,. 
33c98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33c99 20 20 20 20 20 30 2c 20 73 71 6c 69 74 65 33 49       0, sqlite3I
33c9a 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 2c 20  nvalidFunction, 
33c9b 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 63 20  0, 0);.  }.  rc 
33c9c 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
33c9d 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 3b  (db, SQLITE_OK);
33c9e 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
33c9f 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
33ca0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
33ca1 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
33ca2 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 0a  E_OMIT_TRACE./*.
33ca3 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 74 72  ** Register a tr
33ca4 61 63 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  ace function.  T
33ca5 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74 68 65  he pArg from the
33ca6 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69   previously regi
33ca7 73 74 65 72 65 64 20 74 72 61 63 65 0a 2a 2a 20  stered trace.** 
33ca8 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a  is returned.  .*
33ca9 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 74 72 61 63  *.** A NULL trac
33caa 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e 73  e function means
33cab 20 74 68 61 74 20 6e 6f 20 74 72 61 63 69 6e 67   that no tracing
33cac 20 69 73 20 65 78 65 63 75 74 65 73 2e 20 20 41   is executes.  A
33cad 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74 72 61   non-NULL.** tra
33cae 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ce is a pointer 
33caf 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68  to a function th
33cb0 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74  at is invoked at
33cb1 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 61   the start of ea
33cb2 63 68 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d  ch.** SQL statem
33cb3 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ent..*/.SQLITE_A
33cb4 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
33cb5 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33 20 2a  _trace(sqlite3 *
33cb6 64 62 2c 20 76 6f 69 64 20 28 2a 78 54 72 61 63  db, void (*xTrac
33cb7 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
33cb8 68 61 72 2a 29 2c 20 76 6f 69 64 20 2a 70 41 72  har*), void *pAr
33cb9 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64  g){.  void *pOld
33cba 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
33cbb 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
33cbc 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d  x);.  pOld = db-
33cbd 3e 70 54 72 61 63 65 41 72 67 3b 0a 20 20 64 62  >pTraceArg;.  db
33cbe 2d 3e 78 54 72 61 63 65 20 3d 20 78 54 72 61 63  ->xTrace = xTrac
33cbf 65 3b 0a 20 20 64 62 2d 3e 70 54 72 61 63 65 41  e;.  db->pTraceA
33cc0 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c  rg = pArg;.  sql
33cc1 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
33cc2 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
33cc3 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 2f 2a  eturn pOld;.}./*
33cc4 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 70  .** Register a p
33cc5 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 2e  rofile function.
33cc6 20 20 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20    The pArg from 
33cc7 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72  the previously r
33cc8 65 67 69 73 74 65 72 65 64 20 0a 2a 2a 20 70 72  egistered .** pr
33cc9 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 69  ofile function i
33cca 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a 2a  s returned.  .**
33ccb 0a 2a 2a 20 41 20 4e 55 4c 4c 20 70 72 6f 66 69  .** A NULL profi
33ccc 6c 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e  le function mean
33ccd 73 20 74 68 61 74 20 6e 6f 20 70 72 6f 66 69 6c  s that no profil
33cce 69 6e 67 20 69 73 20 65 78 65 63 75 74 65 73 2e  ing is executes.
33ccf 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20    A non-NULL.** 
33cd0 70 72 6f 66 69 6c 65 20 69 73 20 61 20 70 6f 69  profile is a poi
33cd1 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69  nter to a functi
33cd2 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b  on that is invok
33cd3 65 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75  ed at the conclu
33cd4 73 69 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63 68 20  sion of.** each 
33cd5 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68  SQL statement th
33cd6 61 74 20 69 73 20 72 75 6e 2e 0a 2a 2f 0a 53 51  at is run..*/.SQ
33cd7 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
33cd8 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 0a  qlite3_profile(.
33cd9 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
33cda 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65   void (*xProfile
33cdb 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68  )(void*,const ch
33cdc 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36  ar*,sqlite_uint6
33cdd 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  4),.  void *pArg
33cde 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64  .){.  void *pOld
33cdf 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
33ce0 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
33ce1 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d  x);.  pOld = db-
33ce2 3e 70 50 72 6f 66 69 6c 65 41 72 67 3b 0a 20 20  >pProfileArg;.  
33ce3 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 3d 20 78  db->xProfile = x
33ce4 50 72 6f 66 69 6c 65 3b 0a 20 20 64 62 2d 3e 70  Profile;.  db->p
33ce5 50 72 6f 66 69 6c 65 41 72 67 20 3d 20 70 41 72  ProfileArg = pAr
33ce6 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  g;.  sqlite3_mut
33ce7 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
33ce8 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f  ex);.  return pO
33ce9 6c 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ld;.}.#endif /* 
33cea 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
33ceb 45 20 2a 2f 0a 0a 2f 2a 2a 2a 20 45 58 50 45 52  E */../*** EXPER
33cec 49 4d 45 4e 54 41 4c 20 2a 2a 2a 0a 2a 2a 0a 2a  IMENTAL ***.**.*
33ced 2a 20 52 65 67 69 73 74 65 72 20 61 20 66 75 6e  * Register a fun
33cee 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f  ction to be invo
33cef 6b 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73  ked when a trans
33cf0 61 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 2e  action comments.
33cf1 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 76 6f 6b  .** If the invok
33cf2 65 64 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  ed function retu
33cf3 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
33cf4 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 62 65  en the commit be
33cf5 63 6f 6d 65 73 20 61 0a 2a 2a 20 72 6f 6c 6c 62  comes a.** rollb
33cf6 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ack..*/.SQLITE_A
33cf7 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
33cf8 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 0a 20 20  _commit_hook(.  
33cf9 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
33cfa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74            /* Att
33cfb 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20  ach the hook to 
33cfc 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
33cfd 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 62 61  .  int (*xCallba
33cfe 63 6b 29 28 76 6f 69 64 2a 29 2c 20 20 2f 2a 20  ck)(void*),  /* 
33cff 46 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f  Function to invo
33d00 6b 65 20 6f 6e 20 65 61 63 68 20 63 6f 6d 6d 69  ke on each commi
33d01 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72  t */.  void *pAr
33d02 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
33d03 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
33d04 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
33d05 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b  ){.  void *pOld;
33d06 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
33d07 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
33d08 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e  );.  pOld = db->
33d09 70 43 6f 6d 6d 69 74 41 72 67 3b 0a 20 20 64 62  pCommitArg;.  db
33d0a 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63  ->xCommitCallbac
33d0b 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20  k = xCallback;. 
33d0c 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 20   db->pCommitArg 
33d0d 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  = pArg;.  sqlite
33d0e 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
33d0f 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
33d10 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pOld;.}../*.*
33d11 2a 20 52 65 67 69 73 74 65 72 20 61 20 63 61 6c  * Register a cal
33d12 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f  lback to be invo
33d13 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20 61 20  ked each time a 
33d14 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c 0a  row is updated,.
33d15 2a 2a 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64  ** inserted or d
33d16 65 6c 65 74 65 64 20 75 73 69 6e 67 20 74 68 69  eleted using thi
33d17 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  s database conne
33d18 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
33d19 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
33d1a 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a  e3_update_hook(.
33d1b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
33d1c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
33d1d 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74  ttach the hook t
33d1e 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
33d1f 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c  */.  void (*xCal
33d20 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74  lback)(void*,int
33d21 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68  ,char const *,ch
33d22 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74  ar const *,sqlit
33d23 65 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64  e_int64),.  void
33d24 20 2a 70 41 72 67 20 20 20 20 20 20 20 20 20 20   *pArg          
33d25 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
33d26 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  t to the functio
33d27 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a  n */.){.  void *
33d28 70 52 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pRet;.  sqlite3_
33d29 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
33d2a 6d 75 74 65 78 29 3b 0a 20 20 70 52 65 74 20 3d  mutex);.  pRet =
33d2b 20 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67 3b   db->pUpdateArg;
33d2c 0a 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61  .  db->xUpdateCa
33d2d 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61  llback = xCallba
33d2e 63 6b 3b 0a 20 20 64 62 2d 3e 70 55 70 64 61 74  ck;.  db->pUpdat
33d2f 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73  eArg = pArg;.  s
33d30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
33d31 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
33d32 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a   return pRet;.}.
33d33 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
33d34 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65  a callback to be
33d35 20 69 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69   invoked each ti
33d36 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  me a transaction
33d37 20 69 73 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61   is rolled.** ba
33d38 63 6b 20 62 79 20 74 68 69 73 20 64 61 74 61 62  ck by this datab
33d39 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ase connection..
33d3a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
33d3b 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c  id *sqlite3_roll
33d3c 62 61 63 6b 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c  back_hook(.  sql
33d3d 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
33d3e 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68         /* Attach
33d3f 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69   the hook to thi
33d40 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  s database */.  
33d41 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b  void (*xCallback
33d42 29 28 76 6f 69 64 2a 29 2c 20 2f 2a 20 43 61 6c  )(void*), /* Cal
33d43 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a  lback function *
33d44 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20 20  /.  void *pArg  
33d45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33d46 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   Argument to the
33d47 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a   function */.){.
33d48 20 20 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20 20    void *pRet;.  
33d49 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
33d4a 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
33d4b 20 20 70 52 65 74 20 3d 20 64 62 2d 3e 70 52 6f    pRet = db->pRo
33d4c 6c 6c 62 61 63 6b 41 72 67 3b 0a 20 20 64 62 2d  llbackArg;.  db-
33d4d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61  >xRollbackCallba
33d4e 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a  ck = xCallback;.
33d4f 20 20 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41    db->pRollbackA
33d50 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c  rg = pArg;.  sql
33d51 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
33d52 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
33d53 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f  eturn pRet;.}../
33d54 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
33d55 6f 6e 20 72 65 74 75 72 6e 73 20 74 72 75 65 20  on returns true 
33d56 69 66 20 6d 61 69 6e 2d 6d 65 6d 6f 72 79 20 73  if main-memory s
33d57 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e  hould be used in
33d58 73 74 65 61 64 20 6f 66 0a 2a 2a 20 61 20 74 65  stead of.** a te
33d59 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 66 6f 72  mporary file for
33d5a 20 74 72 61 6e 73 69 65 6e 74 20 70 61 67 65 72   transient pager
33d5b 20 66 69 6c 65 73 20 61 6e 64 20 73 74 61 74 65   files and state
33d5c 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2e 0a 2a  ment journals..*
33d5d 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  * The value retu
33d5e 72 6e 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20  rned depends on 
33d5f 74 68 65 20 76 61 6c 75 65 20 6f 66 20 64 62 2d  the value of db-
33d60 3e 74 65 6d 70 5f 73 74 6f 72 65 20 28 72 75 6e  >temp_store (run
33d61 74 69 6d 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65  time.** paramete
33d62 72 29 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 69  r) and the compi
33d63 6c 65 20 74 69 6d 65 20 76 61 6c 75 65 20 6f 66  le time value of
33d64 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
33d65 52 45 2e 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c 6f  RE. The.** follo
33d66 77 69 6e 67 20 74 61 62 6c 65 20 64 65 73 63 72  wing table descr
33d67 69 62 65 73 20 74 68 65 20 72 65 6c 61 74 69 6f  ibes the relatio
33d68 6e 73 68 69 70 20 62 65 74 77 65 65 6e 20 74 68  nship between th
33d69 65 73 65 20 74 77 6f 20 76 61 6c 75 65 73 0a 2a  ese two values.*
33d6a 2a 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74  * and this funct
33d6b 69 6f 6e 73 20 72 65 74 75 72 6e 20 76 61 6c 75  ions return valu
33d6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54  e..**.**   SQLIT
33d6d 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 20 20 20  E_TEMP_STORE    
33d6e 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20   db->temp_store 
33d6f 20 20 20 20 4c 6f 63 61 74 69 6f 6e 20 6f 66 20      Location of 
33d70 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
33d71 73 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  se.**   --------
33d72 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d  ---------     --
33d73 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20  ------------    
33d74 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
33d75 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
33d76 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20 20  **   0          
33d77 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 20             any  
33d78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
33d79 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e 20  le      (return 
33d7a 30 29 0a 2a 2a 20 20 20 31 20 20 20 20 20 20 20  0).**   1       
33d7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
33d7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d7d 20 66 69 6c 65 20 20 20 20 20 20 28 72 65 74 75   file      (retu
33d7e 72 6e 20 30 29 0a 2a 2a 20 20 20 31 20 20 20 20  rn 0).**   1    
33d7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d80 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20   2              
33d81 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72      memory    (r
33d82 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20 20 31 20  eturn 1).**   1 
33d83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d84 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
33d85 20 20 20 20 20 20 20 66 69 6c 65 20 20 20 20 20         file     
33d86 20 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20 20   (return 0).**  
33d87 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20   2              
33d88 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20         1        
33d89 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 20 20            file  
33d8a 20 20 20 20 28 72 65 74 75 72 6e 20 30 29 0a 2a      (return 0).*
33d8b 2a 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20  *   2           
33d8c 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20            2     
33d8d 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d               mem
33d8e 6f 72 79 20 20 20 20 28 72 65 74 75 72 6e 20 31  ory    (return 1
33d8f 29 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20 20  ).**   2        
33d90 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20               0  
33d91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d92 6d 65 6d 6f 72 79 20 20 20 20 28 72 65 74 75 72  memory    (retur
33d93 6e 20 31 29 0a 2a 2a 20 20 20 33 20 20 20 20 20  n 1).**   3     
33d94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d95 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20  any             
33d96 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65     memory    (re
33d97 74 75 72 6e 20 31 29 0a 2a 2f 0a 53 51 4c 49 54  turn 1).*/.SQLIT
33d98 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
33d99 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72  lite3TempInMemor
33d9a 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 20  y(const sqlite3 
33d9b 2a 64 62 29 7b 0a 23 69 66 20 53 51 4c 49 54 45  *db){.#if SQLITE
33d9c 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 0a 20  _TEMP_STORE==1. 
33d9d 20 72 65 74 75 72 6e 20 28 20 64 62 2d 3e 74 65   return ( db->te
33d9e 6d 70 5f 73 74 6f 72 65 3d 3d 32 20 29 3b 0a 23  mp_store==2 );.#
33d9f 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
33da0 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 0a 20  _TEMP_STORE==2. 
33da1 20 72 65 74 75 72 6e 20 28 20 64 62 2d 3e 74 65   return ( db->te
33da2 6d 70 5f 73 74 6f 72 65 21 3d 31 20 29 3b 0a 23  mp_store!=1 );.#
33da3 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
33da4 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 33 0a 20  _TEMP_STORE==3. 
33da5 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69   return 1;.#endi
33da6 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45 4d  f.#if SQLITE_TEM
33da7 50 5f 53 54 4f 52 45 3c 31 20 7c 7c 20 53 51 4c  P_STORE<1 || SQL
33da8 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3e 33  ITE_TEMP_STORE>3
33da9 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e  .  return 0;.#en
33daa 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  dif.}../*.** Thi
33dab 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
33dac 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  led to create a 
33dad 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20  connection to a 
33dae 64 61 74 61 62 61 73 65 20 42 54 72 65 65 0a 2a  database BTree.*
33daf 2a 20 64 72 69 76 65 72 2e 20 20 49 66 20 7a 46  * driver.  If zF
33db0 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e  ilename is the n
33db1 61 6d 65 20 6f 66 20 61 20 66 69 6c 65 2c 20 74  ame of a file, t
33db2 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20 69 73  hen that file is
33db3 0a 2a 2a 20 6f 70 65 6e 65 64 20 61 6e 64 20 75  .** opened and u
33db4 73 65 64 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61  sed.  If zFilena
33db5 6d 65 20 69 73 20 74 68 65 20 6d 61 67 69 63 20  me is the magic 
33db6 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20  name ":memory:" 
33db7 74 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61  then.** the data
33db8 62 61 73 65 20 69 73 20 73 74 6f 72 65 64 20 69  base is stored i
33db9 6e 20 6d 65 6d 6f 72 79 20 28 61 6e 64 20 69 73  n memory (and is
33dba 20 74 68 75 73 20 66 6f 72 67 6f 74 74 65 6e 20   thus forgotten 
33dbb 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 74 68  as soon as.** th
33dbc 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
33dbd 63 6c 6f 73 65 64 2e 29 20 20 49 66 20 7a 46 69  closed.)  If zFi
33dbe 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74  lename is NULL t
33dbf 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
33dc0 0a 2a 2a 20 69 73 20 61 20 22 76 69 72 74 75 61  .** is a "virtua
33dc1 6c 22 20 64 61 74 61 62 61 73 65 20 66 6f 72 20  l" database for 
33dc2 74 72 61 6e 73 69 65 6e 74 20 75 73 65 20 6f 6e  transient use on
33dc3 6c 79 20 61 6e 64 20 69 73 20 64 65 6c 65 74 65  ly and is delete
33dc4 64 20 61 73 0a 2a 2a 20 73 6f 6f 6e 20 61 73 20  d as.** soon as 
33dc5 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  the connection i
33dc6 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  s closed..**.** 
33dc7 41 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61  A virtual databa
33dc8 73 65 20 63 61 6e 20 62 65 20 65 69 74 68 65 72  se can be either
33dc9 20 61 20 64 69 73 6b 20 66 69 6c 65 20 28 74 68   a disk file (th
33dca 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  at is automatica
33dcb 6c 6c 79 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77  lly.** deleted w
33dcc 68 65 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20  hen the file is 
33dcd 63 6c 6f 73 65 64 29 20 6f 72 20 69 74 20 61 6e  closed) or it an
33dce 20 62 65 20 68 65 6c 64 20 65 6e 74 69 72 65 6c   be held entirel
33dcf 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20  y in memory..** 
33dd0 54 68 65 20 73 71 6c 69 74 65 33 54 65 6d 70 49  The sqlite3TempI
33dd1 6e 4d 65 6d 6f 72 79 28 29 20 66 75 6e 63 74 69  nMemory() functi
33dd2 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 64 65  on is used to de
33dd3 74 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 0a 2a  termine which..*
33dd4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
33dd5 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
33dd6 65 46 61 63 74 6f 72 79 28 0a 20 20 73 71 6c 69  eFactory(.  sqli
33dd7 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
33dd8 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61        /* Main da
33dd9 74 61 62 61 73 65 20 77 68 65 6e 20 6f 70 65 6e  tabase when open
33dda 69 6e 67 20 61 75 78 20 6f 74 68 65 72 77 69 73  ing aux otherwis
33ddb 65 20 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  e 0 */.  const c
33ddc 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
33ddd 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
33dde 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e  e file containin
33ddf 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74 61  g the BTree data
33de0 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6d  base */.  int om
33de1 69 74 4a 6f 75 72 6e 61 6c 2c 20 20 20 20 20 20  itJournal,      
33de2 20 20 20 20 2f 2a 20 69 66 20 54 52 55 45 20 74      /* if TRUE t
33de3 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 6f 75 72 6e  hen do not journ
33de4 61 6c 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a  al this file */.
33de5 20 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 20 20    int nCache,   
33de6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
33de7 6f 77 20 6d 61 6e 79 20 70 61 67 65 73 20 69 6e  ow many pages in
33de8 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20   the page cache 
33de9 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67  */.  int vfsFlag
33dea 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  s,             /
33deb 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74  * Flags passed t
33dec 68 72 6f 75 67 68 20 74 6f 20 76 66 73 4f 70 65  hrough to vfsOpe
33ded 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70  n */.  Btree **p
33dee 70 42 74 72 65 65 20 20 20 20 20 20 20 20 20 20  pBtree          
33def 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e   /* Pointer to n
33df0 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20  ew Btree object 
33df1 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
33df2 29 7b 0a 20 20 69 6e 74 20 62 74 46 6c 61 67 73  ){.  int btFlags
33df3 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a   = 0;.  int rc;.
33df4 20 20 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c    .  assert( sql
33df5 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
33df6 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
33df7 61 73 73 65 72 74 28 20 70 70 42 74 72 65 65 20  assert( ppBtree 
33df8 21 3d 20 30 29 3b 0a 20 20 69 66 28 20 6f 6d 69  != 0);.  if( omi
33df9 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  tJournal ){.    
33dfa 62 74 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45  btFlags |= BTREE
33dfb 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a 20  _OMIT_JOURNAL;. 
33dfc 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61   }.  if( db->fla
33dfd 67 73 20 26 20 53 51 4c 49 54 45 5f 4e 6f 52 65  gs & SQLITE_NoRe
33dfe 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 62 74  adlock ){.    bt
33dff 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4e  Flags |= BTREE_N
33e00 4f 5f 52 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d 0a  O_READLOCK;.  }.
33e01 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
33e02 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 69  MIT_MEMORYDB.  i
33e03 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20  f( zFilename==0 
33e04 26 26 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e  && sqlite3TempIn
33e05 4d 65 6d 6f 72 79 28 64 62 29 20 29 7b 0a 20 20  Memory(db) ){.  
33e06 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a    zFilename = ":
33e07 6d 65 6d 6f 72 79 3a 22 3b 0a 20 20 7d 0a 23 65  memory:";.  }.#e
33e08 6e 64 69 66 0a 0a 20 20 69 66 28 20 28 76 66 73  ndif..  if( (vfs
33e09 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  Flags & SQLITE_O
33e0a 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20  PEN_MAIN_DB)!=0 
33e0b 26 26 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30  && (zFilename==0
33e0c 20 7c 7c 20 2a 7a 46 69 6c 65 6e 61 6d 65 3d 3d   || *zFilename==
33e0d 30 29 20 29 7b 0a 20 20 20 20 76 66 73 46 6c 61  0) ){.    vfsFla
33e0e 67 73 20 3d 20 28 76 66 73 46 6c 61 67 73 20 26  gs = (vfsFlags &
33e0f 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41   ~SQLITE_OPEN_MA
33e10 49 4e 5f 44 42 29 20 7c 20 53 51 4c 49 54 45 5f  IN_DB) | SQLITE_
33e11 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20 20  OPEN_TEMP_DB;.  
33e12 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
33e13 42 74 72 65 65 4f 70 65 6e 28 7a 46 69 6c 65 6e  BtreeOpen(zFilen
33e14 61 6d 65 2c 20 28 73 71 6c 69 74 65 33 20 2a 29  ame, (sqlite3 *)
33e15 64 62 2c 20 70 70 42 74 72 65 65 2c 20 62 74 46  db, ppBtree, btF
33e16 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 73 29 3b  lags, vfsFlags);
33e17 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 42 2d  ..  /* If the B-
33e18 54 72 65 65 20 77 61 73 20 73 75 63 63 65 73 73  Tree was success
33e19 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 73 65  fully opened, se
33e1a 74 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68  t the pager-cach
33e1b 65 20 73 69 7a 65 20 74 6f 20 74 68 65 0a 20 20  e size to the.  
33e1c 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  ** default value
33e1d 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65  . Except, if the
33e1e 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 65 4f 70   call to BtreeOp
33e1f 65 6e 28 29 20 72 65 74 75 72 6e 65 64 20 61 20  en() returned a 
33e20 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 6f 70 65 6e  handle.  ** open
33e21 20 6f 6e 20 61 6e 20 65 78 69 73 74 69 6e 67 20   on an existing 
33e22 73 68 61 72 65 64 20 70 61 67 65 72 2d 63 61 63  shared pager-cac
33e23 68 65 2c 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67  he, do not chang
33e24 65 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68  e the pager-cach
33e25 65 20 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20  e .  ** size..  
33e26 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
33e27 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c  ITE_OK && 0==sql
33e28 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28  ite3BtreeSchema(
33e29 2a 70 70 42 74 72 65 65 2c 20 30 2c 20 30 29 20  *ppBtree, 0, 0) 
33e2a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
33e2b 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28  reeSetCacheSize(
33e2c 2a 70 70 42 74 72 65 65 2c 20 6e 43 61 63 68 65  *ppBtree, nCache
33e2d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
33e2e 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
33e2f 75 72 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  urn UTF-8 encode
33e30 64 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61  d English langua
33e31 67 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f  ge explanation o
33e32 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
33e33 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53  t.** error..*/.S
33e34 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
33e35 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 65 72  char *sqlite3_er
33e36 72 6d 73 67 28 73 71 6c 69 74 65 33 20 2a 64 62  rmsg(sqlite3 *db
33e37 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
33e38 2a 7a 3b 0a 20 20 69 66 28 20 21 64 62 20 29 7b  *z;.  if( !db ){
33e39 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
33e3a 74 65 33 45 72 72 53 74 72 28 53 51 4c 49 54 45  te3ErrStr(SQLITE
33e3b 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69  _NOMEM);.  }.  i
33e3c 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
33e3d 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
33e3e 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
33e3f 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 53   sqlite3ErrStr(S
33e40 51 4c 49 54 45 5f 4d 49 53 55 53 45 29 3b 0a 20  QLITE_MISUSE);. 
33e41 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
33e42 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
33e43 65 78 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  ex);.  if( db->m
33e44 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
33e45 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 45 72     z = sqlite3Er
33e46 72 53 74 72 28 53 51 4c 49 54 45 5f 4e 4f 4d 45  rStr(SQLITE_NOME
33e47 4d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  M);.  }else{.   
33e48 20 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69   z = (char*)sqli
33e49 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 64  te3_value_text(d
33e4a 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20 61 73  b->pErr);.    as
33e4b 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
33e4c 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 69  cFailed );.    i
33e4d 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( z==0 ){.     
33e4e 20 7a 20 3d 20 73 71 6c 69 74 65 33 45 72 72 53   z = sqlite3ErrS
33e4f 74 72 28 64 62 2d 3e 65 72 72 43 6f 64 65 29 3b  tr(db->errCode);
33e50 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
33e51 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
33e52 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
33e53 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 6e  eturn z;.}..#ifn
33e54 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
33e55 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 74 75  UTF16./*.** Retu
33e56 72 6e 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  rn UTF-16 encode
33e57 64 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61  d English langua
33e58 67 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f  ge explanation o
33e59 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
33e5a 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53  t.** error..*/.S
33e5b 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
33e5c 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 65 72  void *sqlite3_er
33e5d 72 6d 73 67 31 36 28 73 71 6c 69 74 65 33 20 2a  rmsg16(sqlite3 *
33e5e 64 62 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  db){.  static co
33e5f 6e 73 74 20 75 31 36 20 6f 75 74 4f 66 4d 65 6d  nst u16 outOfMem
33e60 5b 5d 20 3d 20 7b 0a 20 20 20 20 27 6f 27 2c 20  [] = {.    'o', 
33e61 27 75 27 2c 20 27 74 27 2c 20 27 20 27 2c 20 27  'u', 't', ' ', '
33e62 6f 27 2c 20 27 66 27 2c 20 27 20 27 2c 20 27 6d  o', 'f', ' ', 'm
33e63 27 2c 20 27 65 27 2c 20 27 6d 27 2c 20 27 6f 27  ', 'e', 'm', 'o'
33e64 2c 20 27 72 27 2c 20 27 79 27 2c 20 30 0a 20 20  , 'r', 'y', 0.  
33e65 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
33e66 74 20 75 31 36 20 6d 69 73 75 73 65 5b 5d 20 3d  t u16 misuse[] =
33e67 20 7b 0a 20 20 20 20 27 6c 27 2c 20 27 69 27 2c   {.    'l', 'i',
33e68 20 27 62 27 2c 20 27 72 27 2c 20 27 61 27 2c 20   'b', 'r', 'a', 
33e69 27 72 27 2c 20 27 79 27 2c 20 27 20 27 2c 20 0a  'r', 'y', ' ', .
33e6a 20 20 20 20 27 72 27 2c 20 27 6f 27 2c 20 27 75      'r', 'o', 'u
33e6b 27 2c 20 27 74 27 2c 20 27 69 27 2c 20 27 6e 27  ', 't', 'i', 'n'
33e6c 2c 20 27 65 27 2c 20 27 20 27 2c 20 0a 20 20 20  , 'e', ' ', .   
33e6d 20 27 63 27 2c 20 27 61 27 2c 20 27 6c 27 2c 20   'c', 'a', 'l', 
33e6e 27 6c 27 2c 20 27 65 27 2c 20 27 64 27 2c 20 27  'l', 'e', 'd', '
33e6f 20 27 2c 20 0a 20 20 20 20 27 6f 27 2c 20 27 75   ', .    'o', 'u
33e70 27 2c 20 27 74 27 2c 20 27 20 27 2c 20 0a 20 20  ', 't', ' ', .  
33e71 20 20 27 6f 27 2c 20 27 66 27 2c 20 27 20 27 2c    'o', 'f', ' ',
33e72 20 0a 20 20 20 20 27 73 27 2c 20 27 65 27 2c 20   .    's', 'e', 
33e73 27 71 27 2c 20 27 75 27 2c 20 27 65 27 2c 20 27  'q', 'u', 'e', '
33e74 6e 27 2c 20 27 63 27 2c 20 27 65 27 2c 20 30 0a  n', 'c', 'e', 0.
33e75 20 20 7d 3b 0a 0a 20 20 63 6f 6e 73 74 20 76 6f    };..  const vo
33e76 69 64 20 2a 7a 3b 0a 20 20 69 66 28 20 21 64 62  id *z;.  if( !db
33e77 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
33e78 76 6f 69 64 20 2a 29 6f 75 74 4f 66 4d 65 6d 3b  void *)outOfMem;
33e79 0a 20 20 7d 0a 20 20 69 66 28 20 21 73 71 6c 69  .  }.  if( !sqli
33e7a 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69  te3SafetyCheckSi
33e7b 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20  ckOrOk(db) ){.  
33e7c 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a    return (void *
33e7d 29 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 73  )misuse;.  }.  s
33e7e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
33e7f 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
33e80 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
33e81 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 7a 20 3d  ailed ){.    z =
33e82 20 28 76 6f 69 64 20 2a 29 6f 75 74 4f 66 4d 65   (void *)outOfMe
33e83 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  m;.  }else{.    
33e84 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  z = sqlite3_valu
33e85 65 5f 74 65 78 74 31 36 28 64 62 2d 3e 70 45 72  e_text16(db->pEr
33e86 72 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30  r);.    if( z==0
33e87 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
33e88 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d  3ValueSetStr(db-
33e89 3e 70 45 72 72 2c 20 2d 31 2c 20 73 71 6c 69 74  >pErr, -1, sqlit
33e8a 65 33 45 72 72 53 74 72 28 64 62 2d 3e 65 72 72  e3ErrStr(db->err
33e8b 43 6f 64 65 29 2c 0a 20 20 20 20 20 20 20 20 20  Code),.         
33e8c 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53    SQLITE_UTF8, S
33e8d 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
33e8e 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33       z = sqlite3
33e8f 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 64 62  _value_text16(db
33e90 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20  ->pErr);.    }. 
33e91 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28 29     /* A malloc()
33e92 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65 64   may have failed
33e93 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c   within the call
33e94 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   to sqlite3_valu
33e95 65 5f 74 65 78 74 31 36 28 29 0a 20 20 20 20 2a  e_text16().    *
33e96 2a 20 61 62 6f 76 65 2e 20 49 66 20 74 68 69 73  * above. If this
33e97 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
33e98 65 6e 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c 6f  en the db->mallo
33e99 63 46 61 69 6c 65 64 20 66 6c 61 67 20 6e 65 65  cFailed flag nee
33e9a 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20  ds to.    ** be 
33e9b 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20 72  cleared before r
33e9c 65 74 75 72 6e 69 6e 67 2e 20 44 6f 20 74 68 69  eturning. Do thi
33e9d 73 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74  s directly, inst
33e9e 65 61 64 20 6f 66 20 76 69 61 0a 20 20 20 20 2a  ead of via.    *
33e9f 2a 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  * sqlite3ApiExit
33ea0 28 29 2c 20 74 6f 20 61 76 6f 69 64 20 73 65 74  (), to avoid set
33ea1 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73  ting the databas
33ea2 65 20 68 61 6e 64 6c 65 20 65 72 72 6f 72 20 6d  e handle error m
33ea3 65 73 73 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20  essage..    */. 
33ea4 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
33ea5 6c 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73  led = 0;.  }.  s
33ea6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
33ea7 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
33ea8 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65 6e   return z;.}.#en
33ea9 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
33eaa 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a  IT_UTF16 */../*.
33eab 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 6f  ** Return the mo
33eac 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20  st recent error 
33ead 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62  code generated b
33eae 79 20 61 6e 20 53 51 4c 69 74 65 20 72 6f 75 74  y an SQLite rout
33eaf 69 6e 65 2e 20 49 66 20 4e 55 4c 4c 20 69 73 0a  ine. If NULL is.
33eb0 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  ** passed to thi
33eb1 73 20 66 75 6e 63 74 69 6f 6e 2c 20 77 65 20 61  s function, we a
33eb2 73 73 75 6d 65 20 61 20 6d 61 6c 6c 6f 63 28 29  ssume a malloc()
33eb3 20 66 61 69 6c 65 64 20 64 75 72 69 6e 67 20 73   failed during s
33eb4 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a  qlite3_open()..*
33eb5 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
33eb6 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
33eb7 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
33eb8 20 69 66 28 20 64 62 20 26 26 20 21 73 71 6c 69   if( db && !sqli
33eb9 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69  te3SafetyCheckSi
33eba 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20  ckOrOk(db) ){.  
33ebb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
33ebc 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66  MISUSE;.  }.  if
33ebd 28 20 21 64 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c  ( !db || db->mal
33ebe 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
33ebf 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
33ec0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  OMEM;.  }.  retu
33ec1 72 6e 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 26  rn db->errCode &
33ec2 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a   db->errMask;.}.
33ec3 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
33ec4 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
33ec5 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20  errcode(sqlite3 
33ec6 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 20 26  *db){.  if( db &
33ec7 26 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  & !sqlite3Safety
33ec8 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62  CheckSickOrOk(db
33ec9 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
33eca 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
33ecb 20 7d 0a 20 20 69 66 28 20 21 64 62 20 7c 7c 20   }.  if( !db || 
33ecc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
33ecd 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
33ece 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
33ecf 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 65 72  .  return db->er
33ed0 72 43 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rCode;.}../*.** 
33ed1 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 6f 6c  Create a new col
33ed2 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  lating function 
33ed3 66 6f 72 20 64 61 74 61 62 61 73 65 20 22 64 62  for database "db
33ed4 22 2e 20 20 54 68 65 20 6e 61 6d 65 20 69 73 20  ".  The name is 
33ed5 7a 4e 61 6d 65 0a 2a 2a 20 61 6e 64 20 74 68 65  zName.** and the
33ed6 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 65 6e 63   encoding is enc
33ed7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
33ed8 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
33ed9 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 0a  .  sqlite3* db,.
33eda 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
33edb 61 6d 65 2c 20 0a 20 20 75 38 20 65 6e 63 2c 0a  ame, .  u8 enc,.
33edc 20 20 75 38 20 63 6f 6c 6c 54 79 70 65 2c 0a 20    u8 collType,. 
33edd 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69   void* pCtx,.  i
33ede 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f  nt(*xCompare)(vo
33edf 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
33ee0 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
33ee1 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44  id*),.  void(*xD
33ee2 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20  el)(void*).){.  
33ee3 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a  CollSeq *pColl;.
33ee4 20 20 69 6e 74 20 65 6e 63 32 3b 0a 20 20 69 6e    int enc2;.  in
33ee5 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  t nName = sqlite
33ee6 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
33ee7 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73  ;.  .  assert( s
33ee8 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
33ee9 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
33eea 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f  .  /* If SQLITE_
33eeb 55 54 46 31 36 20 69 73 20 73 70 65 63 69 66 69  UTF16 is specifi
33eec 65 64 20 61 73 20 74 68 65 20 65 6e 63 6f 64 69  ed as the encodi
33eed 6e 67 20 74 79 70 65 2c 20 74 72 61 6e 73 66 6f  ng type, transfo
33eee 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74 6f 20  rm this.  ** to 
33eef 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54  one of SQLITE_UT
33ef0 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f  F16LE or SQLITE_
33ef1 55 54 46 31 36 42 45 20 75 73 69 6e 67 20 74 68  UTF16BE using th
33ef2 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55 54  e.  ** SQLITE_UT
33ef3 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72 6f 2e  F16NATIVE macro.
33ef4 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73   SQLITE_UTF16 is
33ef5 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72 6e   not used intern
33ef6 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e 63  ally..  */.  enc
33ef7 32 20 3d 20 65 6e 63 3b 0a 20 20 74 65 73 74 63  2 = enc;.  testc
33ef8 61 73 65 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54  ase( enc2==SQLIT
33ef9 45 5f 55 54 46 31 36 20 29 3b 0a 20 20 74 65 73  E_UTF16 );.  tes
33efa 74 63 61 73 65 28 20 65 6e 63 32 3d 3d 53 51 4c  tcase( enc2==SQL
33efb 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45  ITE_UTF16_ALIGNE
33efc 44 20 29 3b 0a 20 20 69 66 28 20 65 6e 63 32 3d  D );.  if( enc2=
33efd 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20 7c 7c  =SQLITE_UTF16 ||
33efe 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55 54   enc2==SQLITE_UT
33eff 46 31 36 5f 41 4c 49 47 4e 45 44 20 29 7b 0a 20  F16_ALIGNED ){. 
33f00 20 20 20 65 6e 63 32 20 3d 20 53 51 4c 49 54 45     enc2 = SQLITE
33f01 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20  _UTF16NATIVE;.  
33f02 7d 0a 20 20 69 66 28 20 65 6e 63 32 3c 53 51 4c  }.  if( enc2<SQL
33f03 49 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63 32  ITE_UTF8 || enc2
33f04 3e 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  >SQLITE_UTF16BE 
33f05 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
33f06 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
33f07 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20  ..  /* Check if 
33f08 74 68 69 73 20 63 61 6c 6c 20 69 73 20 72 65 6d  this call is rem
33f09 6f 76 69 6e 67 20 6f 72 20 72 65 70 6c 61 63 69  oving or replaci
33f0a 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 63  ng an existing c
33f0b 6f 6c 6c 61 74 69 6f 6e 20 0a 20 20 2a 2a 20 73  ollation .  ** s
33f0c 65 71 75 65 6e 63 65 2e 20 49 66 20 73 6f 2c 20  equence. If so, 
33f0d 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 63  and there are ac
33f0e 74 69 76 65 20 56 4d 73 2c 20 72 65 74 75 72 6e  tive VMs, return
33f0f 20 62 75 73 79 2e 20 49 66 20 74 68 65 72 65 0a   busy. If there.
33f10 20 20 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69    ** are no acti
33f11 76 65 20 56 4d 73 2c 20 69 6e 76 61 6c 69 64 61  ve VMs, invalida
33f12 74 65 20 61 6e 79 20 70 72 65 2d 63 6f 6d 70 69  te any pre-compi
33f13 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  led statements..
33f14 20 20 2a 2f 0a 20 20 70 43 6f 6c 6c 20 3d 20 73    */.  pColl = s
33f15 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
33f16 71 28 64 62 2c 20 28 75 38 29 65 6e 63 32 2c 20  q(db, (u8)enc2, 
33f17 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 69 66 28  zName, 0);.  if(
33f18 20 70 43 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d   pColl && pColl-
33f19 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 69 66 28  >xCmp ){.    if(
33f1a 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
33f1b 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  nt ){.      sqli
33f1c 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
33f1d 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20 20 20  ITE_BUSY, .     
33f1e 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 64 65     "unable to de
33f1f 6c 65 74 65 2f 6d 6f 64 69 66 79 20 63 6f 6c 6c  lete/modify coll
33f20 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 64  ation sequence d
33f21 75 65 20 74 6f 20 61 63 74 69 76 65 20 73 74 61  ue to active sta
33f22 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 20  tements");.     
33f23 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
33f24 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  USY;.    }.    s
33f25 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
33f26 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
33f27 62 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63  b);..    /* If c
33f28 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
33f29 65 20 70 43 6f 6c 6c 20 77 61 73 20 63 72 65 61  e pColl was crea
33f2a 74 65 64 20 64 69 72 65 63 74 6c 79 20 62 79 20  ted directly by 
33f2b 61 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a  a call to.    **
33f2c 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
33f2d 63 6f 6c 6c 61 74 69 6f 6e 2c 20 61 6e 64 20 6e  collation, and n
33f2e 6f 74 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  ot generated by 
33f2f 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 29 2c 0a  synthCollSeq(),.
33f30 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 79 20      ** then any 
33f31 63 6f 70 69 65 73 20 6d 61 64 65 20 62 79 20 73  copies made by s
33f32 79 6e 74 68 43 6f 6c 6c 53 65 71 28 29 20 6e 65  ynthCollSeq() ne
33f33 65 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64  ed to be invalid
33f34 61 74 65 64 2e 0a 20 20 20 20 2a 2a 20 41 6c 73  ated..    ** Als
33f35 6f 2c 20 63 6f 6c 6c 61 74 69 6f 6e 20 64 65 73  o, collation des
33f36 74 72 75 63 74 6f 72 20 2d 20 43 6f 6c 6c 53 65  tructor - CollSe
33f37 71 2e 78 44 65 6c 28 29 20 2d 20 66 75 6e 63 74  q.xDel() - funct
33f38 69 6f 6e 20 6d 61 79 20 6e 65 65 64 0a 20 20 20  ion may need.   
33f39 20 2a 2a 20 74 6f 20 62 65 20 63 61 6c 6c 65 64   ** to be called
33f3a 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 69 66  ..    */ .    if
33f3b 28 20 28 70 43 6f 6c 6c 2d 3e 65 6e 63 20 26 20  ( (pColl->enc & 
33f3c 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  ~SQLITE_UTF16_AL
33f3d 49 47 4e 45 44 29 3d 3d 65 6e 63 32 20 29 7b 0a  IGNED)==enc2 ){.
33f3e 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 61        CollSeq *a
33f3f 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48 61  Coll = sqlite3Ha
33f40 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c  shFind(&db->aCol
33f41 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  lSeq, zName, nNa
33f42 6d 65 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a  me);.      int j
33f43 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ;.      for(j=0;
33f44 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20   j<3; j++){.    
33f45 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 20 3d      CollSeq *p =
33f46 20 26 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20   &aColl[j];.    
33f47 20 20 20 20 69 66 28 20 70 2d 3e 65 6e 63 3d 3d      if( p->enc==
33f48 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20  pColl->enc ){.  
33f49 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 78          if( p->x
33f4a 44 65 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Del ){.         
33f4b 20 20 20 70 2d 3e 78 44 65 6c 28 70 2d 3e 70 55     p->xDel(p->pU
33f4c 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ser);.          
33f4d 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 78  }.          p->x
33f4e 43 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  Cmp = 0;.       
33f4f 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
33f50 0a 20 20 7d 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20  .  }..  pColl = 
33f51 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
33f52 65 71 28 64 62 2c 20 28 75 38 29 65 6e 63 32 2c  eq(db, (u8)enc2,
33f53 20 7a 4e 61 6d 65 2c 20 31 29 3b 0a 20 20 69 66   zName, 1);.  if
33f54 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70  ( pColl ){.    p
33f55 43 6f 6c 6c 2d 3e 78 43 6d 70 20 3d 20 78 43 6f  Coll->xCmp = xCo
33f56 6d 70 61 72 65 3b 0a 20 20 20 20 70 43 6f 6c 6c  mpare;.    pColl
33f57 2d 3e 70 55 73 65 72 20 3d 20 70 43 74 78 3b 0a  ->pUser = pCtx;.
33f58 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20      pColl->xDel 
33f59 3d 20 78 44 65 6c 3b 0a 20 20 20 20 70 43 6f 6c  = xDel;.    pCol
33f5a 6c 2d 3e 65 6e 63 20 3d 20 28 75 38 29 28 65 6e  l->enc = (u8)(en
33f5b 63 32 20 7c 20 28 65 6e 63 20 26 20 53 51 4c 49  c2 | (enc & SQLI
33f5c 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
33f5d 29 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 74  ));.    pColl->t
33f5e 79 70 65 20 3d 20 63 6f 6c 6c 54 79 70 65 3b 0a  ype = collType;.
33f5f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72    }.  sqlite3Err
33f60 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  or(db, SQLITE_OK
33f61 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  , 0);.  return S
33f62 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
33f63 0a 2a 2a 20 54 68 69 73 20 61 72 72 61 79 20 64  .** This array d
33f64 65 66 69 6e 65 73 20 68 61 72 64 20 75 70 70 65  efines hard uppe
33f65 72 20 62 6f 75 6e 64 73 20 6f 6e 20 6c 69 6d 69  r bounds on limi
33f66 74 20 76 61 6c 75 65 73 2e 20 20 54 68 65 0a 2a  t values.  The.*
33f67 2a 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 6d 75  * initializer mu
33f68 73 74 20 62 65 20 6b 65 70 74 20 69 6e 20 73 79  st be kept in sy
33f69 6e 63 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  nc with the SQLI
33f6a 54 45 5f 4c 49 4d 49 54 5f 2a 0a 2a 2a 20 23 64  TE_LIMIT_*.** #d
33f6b 65 66 69 6e 65 73 20 69 6e 20 73 71 6c 69 74 65  efines in sqlite
33f6c 33 2e 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  3.h..*/.static c
33f6d 6f 6e 73 74 20 69 6e 74 20 61 48 61 72 64 4c 69  onst int aHardLi
33f6e 6d 69 74 5b 5d 20 3d 20 7b 0a 20 20 53 51 4c 49  mit[] = {.  SQLI
33f6f 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2c 0a 20  TE_MAX_LENGTH,. 
33f70 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f   SQLITE_MAX_SQL_
33f71 4c 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54 45  LENGTH,.  SQLITE
33f72 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 0a 20 20 53  _MAX_COLUMN,.  S
33f73 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
33f74 45 50 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  EPTH,.  SQLITE_M
33f75 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  AX_COMPOUND_SELE
33f76 43 54 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58  CT,.  SQLITE_MAX
33f77 5f 56 44 42 45 5f 4f 50 2c 0a 20 20 53 51 4c 49  _VDBE_OP,.  SQLI
33f78 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
33f79 41 52 47 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  ARG,.  SQLITE_MA
33f7a 58 5f 41 54 54 41 43 48 45 44 2c 0a 20 20 53 51  X_ATTACHED,.  SQ
33f7b 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41  LITE_MAX_LIKE_PA
33f7c 54 54 45 52 4e 5f 4c 45 4e 47 54 48 2c 0a 20 20  TTERN_LENGTH,.  
33f7d 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
33f7e 42 4c 45 5f 4e 55 4d 42 45 52 2c 0a 20 20 53 51  BLE_NUMBER,.  SQ
33f7f 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52  LITE_MAX_TRIGGER
33f80 5f 44 45 50 54 48 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a  _DEPTH,.};../*.*
33f81 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
33f82 68 61 72 64 20 6c 69 6d 69 74 73 20 61 72 65 20  hard limits are 
33f83 73 65 74 20 74 6f 20 72 65 61 73 6f 6e 61 62 6c  set to reasonabl
33f84 65 20 76 61 6c 75 65 73 0a 2a 2f 0a 23 69 66 20  e values.*/.#if 
33f85 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
33f86 48 3c 31 30 30 0a 23 20 65 72 72 6f 72 20 53 51  H<100.# error SQ
33f87 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20  LITE_MAX_LENGTH 
33f88 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74  must be at least
33f89 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 20   100.#endif.#if 
33f8a 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
33f8b 45 4e 47 54 48 3c 31 30 30 0a 23 20 65 72 72 6f  ENGTH<100.# erro
33f8c 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c  r SQLITE_MAX_SQL
33f8d 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65 20  _LENGTH must be 
33f8e 61 74 20 6c 65 61 73 74 20 31 30 30 0a 23 65 6e  at least 100.#en
33f8f 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
33f90 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 3e 53 51  AX_SQL_LENGTH>SQ
33f91 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a  LITE_MAX_LENGTH.
33f92 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d  # error SQLITE_M
33f93 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 6d 75  AX_SQL_LENGTH mu
33f94 73 74 20 6e 6f 74 20 62 65 20 67 72 65 61 74 65  st not be greate
33f95 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d 41  r than SQLITE_MA
33f96 58 5f 4c 45 4e 47 54 48 0a 23 65 6e 64 69 66 0a  X_LENGTH.#endif.
33f97 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43  #if SQLITE_MAX_C
33f98 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 3c 32  OMPOUND_SELECT<2
33f99 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
33f9a 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
33f9b 45 43 54 20 6d 75 73 74 20 62 65 20 61 74 20 6c  ECT must be at l
33f9c 65 61 73 74 20 32 0a 23 65 6e 64 69 66 0a 23 69  east 2.#endif.#i
33f9d 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42  f SQLITE_MAX_VDB
33f9e 45 5f 4f 50 3c 34 30 0a 23 20 65 72 72 6f 72 20  E_OP<40.# error 
33f9f 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f  SQLITE_MAX_VDBE_
33fa0 4f 50 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65  OP must be at le
33fa1 61 73 74 20 34 30 0a 23 65 6e 64 69 66 0a 23 69  ast 40.#endif.#i
33fa2 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e  f SQLITE_MAX_FUN
33fa3 43 54 49 4f 4e 5f 41 52 47 3c 30 20 7c 7c 20 53  CTION_ARG<0 || S
33fa4 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49  QLITE_MAX_FUNCTI
33fa5 4f 4e 5f 41 52 47 3e 31 30 30 30 0a 23 20 65 72  ON_ARG>1000.# er
33fa6 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46  ror SQLITE_MAX_F
33fa7 55 4e 43 54 49 4f 4e 5f 41 52 47 20 6d 75 73 74  UNCTION_ARG must
33fa8 20 62 65 20 62 65 74 77 65 65 6e 20 30 20 61 6e   be between 0 an
33fa9 64 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 23 69  d 1000.#endif.#i
33faa 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54  f SQLITE_MAX_ATT
33fab 41 43 48 45 44 3c 30 20 7c 7c 20 53 51 4c 49 54  ACHED<0 || SQLIT
33fac 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 3e 33  E_MAX_ATTACHED>3
33fad 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45  0.# error SQLITE
33fae 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 20 6d 75  _MAX_ATTACHED mu
33faf 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 30 20  st be between 0 
33fb0 61 6e 64 20 33 30 0a 23 65 6e 64 69 66 0a 23 69  and 30.#endif.#i
33fb1 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  f SQLITE_MAX_LIK
33fb2 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
33fb3 3c 31 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  <1.# error SQLIT
33fb4 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45  E_MAX_LIKE_PATTE
33fb5 52 4e 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 62  RN_LENGTH must b
33fb6 65 20 61 74 20 6c 65 61 73 74 20 31 0a 23 65 6e  e at least 1.#en
33fb7 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
33fb8 41 58 5f 43 4f 4c 55 4d 4e 3e 33 32 37 36 37 0a  AX_COLUMN>32767.
33fb9 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d  # error SQLITE_M
33fba 41 58 5f 43 4f 4c 55 4d 4e 20 6d 75 73 74 20 6e  AX_COLUMN must n
33fbb 6f 74 20 65 78 63 65 65 64 20 33 32 37 36 37 0a  ot exceed 32767.
33fbc 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
33fbd 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45  E_MAX_TRIGGER_DE
33fbe 50 54 48 3c 31 0a 23 20 65 72 72 6f 72 20 53 51  PTH<1.# error SQ
33fbf 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52  LITE_MAX_TRIGGER
33fc0 5f 44 45 50 54 48 20 6d 75 73 74 20 62 65 20 61  _DEPTH must be a
33fc1 74 20 6c 65 61 73 74 20 31 0a 23 65 6e 64 69 66  t least 1.#endif
33fc2 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  .../*.** Change 
33fc3 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 6c  the value of a l
33fc4 69 6d 69 74 2e 20 20 52 65 70 6f 72 74 20 74 68  imit.  Report th
33fc5 65 20 6f 6c 64 20 76 61 6c 75 65 2e 0a 2a 2a 20  e old value..** 
33fc6 49 66 20 61 6e 20 69 6e 76 61 6c 69 64 20 6c 69  If an invalid li
33fc7 6d 69 74 20 69 6e 64 65 78 20 69 73 20 73 75 70  mit index is sup
33fc8 70 6c 69 65 64 2c 20 72 65 70 6f 72 74 20 2d 31  plied, report -1
33fc9 2e 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61  ..** Make no cha
33fca 6e 67 65 73 20 62 75 74 20 73 74 69 6c 6c 20 72  nges but still r
33fcb 65 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76 61  eport the old va
33fcc 6c 75 65 20 69 66 20 74 68 65 0a 2a 2a 20 6e 65  lue if the.** ne
33fcd 77 20 6c 69 6d 69 74 20 69 73 20 6e 65 67 61 74  w limit is negat
33fce 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77  ive..**.** A new
33fcf 20 6c 6f 77 65 72 20 6c 69 6d 69 74 20 64 6f 65   lower limit doe
33fd0 73 20 6e 6f 74 20 73 68 72 69 6e 6b 20 65 78 69  s not shrink exi
33fd1 73 74 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 73  sting constructs
33fd2 2e 0a 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 70  ..** It merely p
33fd3 72 65 76 65 6e 74 73 20 6e 65 77 20 63 6f 6e 73  revents new cons
33fd4 74 72 75 63 74 73 20 74 68 61 74 20 65 78 63 65  tructs that exce
33fd5 65 64 20 74 68 65 20 6c 69 6d 69 74 0a 2a 2a 20  ed the limit.** 
33fd6 66 72 6f 6d 20 66 6f 72 6d 69 6e 67 2e 0a 2a 2f  from forming..*/
33fd7 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
33fd8 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71  sqlite3_limit(sq
33fd9 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6c  lite3 *db, int l
33fda 69 6d 69 74 49 64 2c 20 69 6e 74 20 6e 65 77 4c  imitId, int newL
33fdb 69 6d 69 74 29 7b 0a 20 20 69 6e 74 20 6f 6c 64  imit){.  int old
33fdc 4c 69 6d 69 74 3b 0a 20 20 69 66 28 20 6c 69 6d  Limit;.  if( lim
33fdd 69 74 49 64 3c 30 20 7c 7c 20 6c 69 6d 69 74 49  itId<0 || limitI
33fde 64 3e 3d 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49  d>=SQLITE_N_LIMI
33fdf 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  T ){.    return 
33fe0 2d 31 3b 0a 20 20 7d 0a 20 20 6f 6c 64 4c 69 6d  -1;.  }.  oldLim
33fe1 69 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b  it = db->aLimit[
33fe2 6c 69 6d 69 74 49 64 5d 3b 0a 20 20 69 66 28 20  limitId];.  if( 
33fe3 6e 65 77 4c 69 6d 69 74 3e 3d 30 20 29 7b 0a 20  newLimit>=0 ){. 
33fe4 20 20 20 69 66 28 20 6e 65 77 4c 69 6d 69 74 3e     if( newLimit>
33fe5 61 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69 74  aHardLimit[limit
33fe6 49 64 5d 20 29 7b 0a 20 20 20 20 20 20 6e 65 77  Id] ){.      new
33fe7 4c 69 6d 69 74 20 3d 20 61 48 61 72 64 4c 69 6d  Limit = aHardLim
33fe8 69 74 5b 6c 69 6d 69 74 49 64 5d 3b 0a 20 20 20  it[limitId];.   
33fe9 20 7d 0a 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69   }.    db->aLimi
33fea 74 5b 6c 69 6d 69 74 49 64 5d 20 3d 20 6e 65 77  t[limitId] = new
33feb 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74  Limit;.  }.  ret
33fec 75 72 6e 20 6f 6c 64 4c 69 6d 69 74 3b 0a 7d 0a  urn oldLimit;.}.
33fed 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
33fee 69 6e 65 20 64 6f 65 73 20 74 68 65 20 77 6f 72  ine does the wor
33fef 6b 20 6f 66 20 6f 70 65 6e 69 6e 67 20 61 20 64  k of opening a d
33ff0 61 74 61 62 61 73 65 20 6f 6e 20 62 65 68 61 6c  atabase on behal
33ff1 66 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  f of.** sqlite3_
33ff2 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74  open() and sqlit
33ff3 65 33 5f 6f 70 65 6e 31 36 28 29 2e 20 54 68 65  e3_open16(). The
33ff4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61   database filena
33ff5 6d 65 20 22 7a 46 69 6c 65 6e 61 6d 65 22 20 20  me "zFilename"  
33ff6 0a 2a 2a 20 69 73 20 55 54 46 2d 38 20 65 6e 63  .** is UTF-8 enc
33ff7 6f 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  oded..*/.static 
33ff8 69 6e 74 20 6f 70 65 6e 44 61 74 61 62 61 73 65  int openDatabase
33ff9 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
33ffa 7a 46 69 6c 65 6e 61 6d 65 2c 20 2f 2a 20 44 61  zFilename, /* Da
33ffb 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  tabase filename 
33ffc 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f  UTF-8 encoded */
33ffd 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44  .  sqlite3 **ppD
33ffe 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  b,        /* OUT
33fff 3a 20 52 65 74 75 72 6e 65 64 20 64 61 74 61 62  : Returned datab
34000 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
34001 75 6e 73 69 67 6e 65 64 20 66 6c 61 67 73 2c 20  unsigned flags, 
34002 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74         /* Operat
34003 69 6f 6e 61 6c 20 66 6c 61 67 73 20 2a 2f 0a 20  ional flags */. 
34004 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66   const char *zVf
34005 73 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  s       /* Name 
34006 6f 66 20 74 68 65 20 56 46 53 20 74 6f 20 75 73  of the VFS to us
34007 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  e */.){.  sqlite
34008 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b  3 *db;.  int rc;
34009 0a 20 20 69 6e 74 20 69 73 54 68 72 65 61 64 73  .  int isThreads
3400a 61 66 65 3b 0a 0a 20 20 2a 70 70 44 62 20 3d 20  afe;..  *ppDb = 
3400b 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  0;.#ifndef SQLIT
3400c 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
3400d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69    rc = sqlite3_i
3400e 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69  nitialize();.  i
3400f 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
34010 63 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28  c;.#endif..  if(
34011 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
34012 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 3d  nfig.bCoreMutex=
34013 3d 30 20 29 7b 0a 20 20 20 20 69 73 54 68 72 65  =0 ){.    isThre
34014 61 64 73 61 66 65 20 3d 20 30 3b 0a 20 20 7d 65  adsafe = 0;.  }e
34015 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
34016 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55  SQLITE_OPEN_NOMU
34017 54 45 58 20 29 7b 0a 20 20 20 20 69 73 54 68 72  TEX ){.    isThr
34018 65 61 64 73 61 66 65 20 3d 20 30 3b 0a 20 20 7d  eadsafe = 0;.  }
34019 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
3401a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c   SQLITE_OPEN_FUL
3401b 4c 4d 55 54 45 58 20 29 7b 0a 20 20 20 20 69 73  LMUTEX ){.    is
3401c 54 68 72 65 61 64 73 61 66 65 20 3d 20 31 3b 0a  Threadsafe = 1;.
3401d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 73 54    }else{.    isT
3401e 68 72 65 61 64 73 61 66 65 20 3d 20 73 71 6c 69  hreadsafe = sqli
3401f 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
34020 62 46 75 6c 6c 4d 75 74 65 78 3b 0a 20 20 7d 0a  bFullMutex;.  }.
34021 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51    if( flags & SQ
34022 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54  LITE_OPEN_PRIVAT
34023 45 43 41 43 48 45 20 29 7b 0a 20 20 20 20 66 6c  ECACHE ){.    fl
34024 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4f  ags &= ~SQLITE_O
34025 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 3b  PEN_SHAREDCACHE;
34026 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c  .  }else if( sql
34027 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
34028 2e 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62  .sharedCacheEnab
34029 6c 65 64 20 29 7b 0a 20 20 20 20 66 6c 61 67 73  led ){.    flags
3402a 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   |= SQLITE_OPEN_
3402b 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d  SHAREDCACHE;.  }
3402c 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 68 61  ..  /* Remove ha
3402d 72 6d 66 75 6c 20 62 69 74 73 20 66 72 6f 6d 20  rmful bits from 
3402e 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
3402f 74 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ter.  **.  ** Th
34030 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f  e SQLITE_OPEN_NO
34031 4d 55 54 45 58 20 61 6e 64 20 53 51 4c 49 54 45  MUTEX and SQLITE
34032 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20  _OPEN_FULLMUTEX 
34033 66 6c 61 67 73 20 77 65 72 65 0a 20 20 2a 2a 20  flags were.  ** 
34034 64 65 61 6c 74 20 77 69 74 68 20 69 6e 20 74 68  dealt with in th
34035 65 20 70 72 65 76 69 6f 75 73 20 63 6f 64 65 20  e previous code 
34036 62 6c 6f 63 6b 2e 20 20 42 65 73 69 64 65 73 20  block.  Besides 
34037 74 68 65 73 65 2c 20 74 68 65 20 6f 6e 6c 79 0a  these, the only.
34038 20 20 2a 2a 20 76 61 6c 69 64 20 69 6e 70 75 74    ** valid input
34039 20 66 6c 61 67 73 20 66 6f 72 20 73 71 6c 69 74   flags for sqlit
3403a 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 61 72 65  e3_open_v2() are
3403b 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
3403c 44 4f 4e 4c 59 2c 0a 20 20 2a 2a 20 53 51 4c 49  DONLY,.  ** SQLI
3403d 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
3403e 45 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 50  E, and SQLITE_OP
3403f 45 4e 5f 43 52 45 41 54 45 2e 20 20 53 69 6c 65  EN_CREATE.  Sile
34040 6e 74 6c 79 20 6d 61 73 6b 0a 20 20 2a 2a 20 6f  ntly mask.  ** o
34041 66 66 20 61 6c 6c 20 6f 74 68 65 72 20 66 6c 61  ff all other fla
34042 67 73 2e 0a 20 20 2a 2f 0a 20 20 66 6c 61 67 73  gs..  */.  flags
34043 20 26 3d 20 20 7e 28 20 53 51 4c 49 54 45 5f 4f   &=  ~( SQLITE_O
34044 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
34045 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20  E |.            
34046 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45     SQLITE_OPEN_E
34047 58 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20  XCLUSIVE |.     
34048 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
34049 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 7c 0a  _OPEN_MAIN_DB |.
3404a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
3404b 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
3404c 44 42 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20  DB | .          
3404d 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
3404e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20 7c 20  _TRANSIENT_DB | 
3404f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34050 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
34051 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20  _JOURNAL | .    
34052 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
34053 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52  E_OPEN_TEMP_JOUR
34054 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20 20 20 20  NAL | .         
34055 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
34056 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 7c 20 0a  N_SUBJOURNAL | .
34057 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
34058 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45  QLITE_OPEN_MASTE
34059 52 5f 4a 4f 55 52 4e 41 4c 20 7c 0a 20 20 20 20  R_JOURNAL |.    
3405a 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
3405b 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 7c  E_OPEN_NOMUTEX |
3405c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3405d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c  SQLITE_OPEN_FULL
3405e 4d 55 54 45 58 0a 20 20 20 20 20 20 20 20 20 20  MUTEX.          
3405f 20 20 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f     );..  /* Allo
34060 63 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 20  cate the sqlite 
34061 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 2a  data structure *
34062 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33  /.  db = sqlite3
34063 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65  MallocZero( size
34064 6f 66 28 73 71 6c 69 74 65 33 29 20 29 3b 0a 20  of(sqlite3) );. 
34065 20 69 66 28 20 64 62 3d 3d 30 20 29 20 67 6f 74   if( db==0 ) got
34066 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20  o opendb_out;.  
34067 69 66 28 20 69 73 54 68 72 65 61 64 73 61 66 65  if( isThreadsafe
34068 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 75 74 65   ){.    db->mute
34069 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  x = sqlite3Mutex
3406a 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
3406b 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20  EX_RECURSIVE);. 
3406c 20 20 20 69 66 28 20 64 62 2d 3e 6d 75 74 65 78     if( db->mutex
3406d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
3406e 69 74 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 20  ite3_free(db);. 
3406f 20 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20 20       db = 0;.   
34070 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f     goto opendb_o
34071 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ut;.    }.  }.  
34072 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
34073 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
34074 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d 20    db->errMask = 
34075 30 78 66 66 3b 0a 20 20 64 62 2d 3e 6e 44 62 20  0xff;.  db->nDb 
34076 3d 20 32 3b 0a 20 20 64 62 2d 3e 6d 61 67 69 63  = 2;.  db->magic
34077 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f   = SQLITE_MAGIC_
34078 42 55 53 59 3b 0a 20 20 64 62 2d 3e 61 44 62 20  BUSY;.  db->aDb 
34079 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b  = db->aDbStatic;
3407a 0a 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ..  assert( size
3407b 6f 66 28 64 62 2d 3e 61 4c 69 6d 69 74 29 3d 3d  of(db->aLimit)==
3407c 73 69 7a 65 6f 66 28 61 48 61 72 64 4c 69 6d 69  sizeof(aHardLimi
3407d 74 29 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28 64  t) );.  memcpy(d
3407e 62 2d 3e 61 4c 69 6d 69 74 2c 20 61 48 61 72 64  b->aLimit, aHard
3407f 4c 69 6d 69 74 2c 20 73 69 7a 65 6f 66 28 64 62  Limit, sizeof(db
34080 2d 3e 61 4c 69 6d 69 74 29 29 3b 0a 20 20 64 62  ->aLimit));.  db
34081 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31  ->autoCommit = 1
34082 3b 0a 20 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f  ;.  db->nextAuto
34083 76 61 63 20 3d 20 2d 31 3b 0a 20 20 64 62 2d 3e  vac = -1;.  db->
34084 6e 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20 30  nextPagesize = 0
34085 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d  ;.  db->flags |=
34086 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c   SQLITE_ShortCol
34087 4e 61 6d 65 73 0a 23 69 66 20 53 51 4c 49 54 45  Names.#if SQLITE
34088 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f  _DEFAULT_FILE_FO
34089 52 4d 41 54 3c 34 0a 20 20 20 20 20 20 20 20 20  RMAT<4.         
3408a 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45          | SQLITE
3408b 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 0a 23  _LegacyFileFmt.#
3408c 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
3408d 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 41 44 5f  ITE_ENABLE_LOAD_
3408e 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 20 20  EXTENSION.      
3408f 20 20 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c             | SQL
34090 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
34091 6e 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  n.#endif.#if SQL
34092 49 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55  ITE_DEFAULT_RECU
34093 52 53 49 56 45 5f 54 52 49 47 47 45 52 53 0a 20  RSIVE_TRIGGERS. 
34094 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34095 7c 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67  | SQLITE_RecTrig
34096 67 65 72 73 0a 23 65 6e 64 69 66 0a 20 20 20 20  gers.#endif.    
34097 20 20 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73    ;.  sqlite3Has
34098 68 49 6e 69 74 28 26 64 62 2d 3e 61 43 6f 6c 6c  hInit(&db->aColl
34099 53 65 71 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  Seq);.#ifndef SQ
3409a 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
3409b 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33  LTABLE.  sqlite3
3409c 48 61 73 68 49 6e 69 74 28 26 64 62 2d 3e 61 4d  HashInit(&db->aM
3409d 6f 64 75 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 0a  odule);.#endif..
3409e 20 20 64 62 2d 3e 70 56 66 73 20 3d 20 73 71 6c    db->pVfs = sql
3409f 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 7a 56  ite3_vfs_find(zV
340a0 66 73 29 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e  fs);.  if( !db->
340a1 70 56 66 73 20 29 7b 0a 20 20 20 20 72 63 20 3d  pVfs ){.    rc =
340a2 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
340a3 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
340a4 64 62 2c 20 72 63 2c 20 22 6e 6f 20 73 75 63 68  db, rc, "no such
340a5 20 76 66 73 3a 20 25 73 22 2c 20 7a 56 66 73 29   vfs: %s", zVfs)
340a6 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64  ;.    goto opend
340a7 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  b_out;.  }..  /*
340a8 20 41 64 64 20 74 68 65 20 64 65 66 61 75 6c 74   Add the default
340a9 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
340aa 6e 63 65 20 42 49 4e 41 52 59 2e 20 42 49 4e 41  nce BINARY. BINA
340ab 52 59 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74  RY works for bot
340ac 68 20 55 54 46 2d 38 0a 20 20 2a 2a 20 61 6e 64  h UTF-8.  ** and
340ad 20 55 54 46 2d 31 36 2c 20 73 6f 20 61 64 64 20   UTF-16, so add 
340ae 61 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 65 61  a version for ea
340af 63 68 20 74 6f 20 61 76 6f 69 64 20 61 6e 79 20  ch to avoid any 
340b0 75 6e 6e 65 63 65 73 73 61 72 79 0a 20 20 2a 2a  unnecessary.  **
340b1 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 54 68   conversions. Th
340b2 65 20 6f 6e 6c 79 20 65 72 72 6f 72 20 74 68 61  e only error tha
340b3 74 20 63 61 6e 20 6f 63 63 75 72 20 68 65 72 65  t can occur here
340b4 20 69 73 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66   is a malloc() f
340b5 61 69 6c 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 63  ailure..  */.  c
340b6 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
340b7 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c  b, "BINARY", SQL
340b8 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
340b9 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 2c 20 30 2c  _COLL_BINARY, 0,
340ba 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
340bb 20 20 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20     binCollFunc, 
340bc 30 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c  0);.  createColl
340bd 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52  ation(db, "BINAR
340be 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  Y", SQLITE_UTF16
340bf 42 45 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  BE, SQLITE_COLL_
340c0 42 49 4e 41 52 59 2c 20 30 2c 0a 20 20 20 20 20  BINARY, 0,.     
340c1 20 20 20 20 20 20 20 20 20 20 20 20 20 62 69 6e               bin
340c2 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20  CollFunc, 0);.  
340c3 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
340c4 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51  db, "BINARY", SQ
340c5 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 53 51  LITE_UTF16LE, SQ
340c6 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59  LITE_COLL_BINARY
340c7 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
340c8 20 20 20 20 20 20 20 62 69 6e 43 6f 6c 6c 46 75         binCollFu
340c9 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65 61 74 65  nc, 0);.  create
340ca 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 52  Collation(db, "R
340cb 54 52 49 4d 22 2c 20 53 51 4c 49 54 45 5f 55 54  TRIM", SQLITE_UT
340cc 46 38 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  F8, SQLITE_COLL_
340cd 55 53 45 52 2c 20 28 76 6f 69 64 2a 29 31 2c 0a  USER, (void*)1,.
340ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
340cf 20 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30    binCollFunc, 0
340d0 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
340d1 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
340d2 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74   goto opendb_out
340d3 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 44 66 6c  ;.  }.  db->pDfl
340d4 74 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46  tColl = sqlite3F
340d5 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53  indCollSeq(db, S
340d6 51 4c 49 54 45 5f 55 54 46 38 2c 20 22 42 49 4e  QLITE_UTF8, "BIN
340d7 41 52 59 22 2c 20 30 29 3b 0a 20 20 61 73 73 65  ARY", 0);.  asse
340d8 72 74 28 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  rt( db->pDfltCol
340d9 6c 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c  l!=0 );..  /* Al
340da 73 6f 20 61 64 64 20 61 20 55 54 46 2d 38 20 63  so add a UTF-8 c
340db 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 65 20  ase-insensitive 
340dc 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
340dd 63 65 2e 20 2a 2f 0a 20 20 63 72 65 61 74 65 43  ce. */.  createC
340de 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 4e 4f  ollation(db, "NO
340df 43 41 53 45 22 2c 20 53 51 4c 49 54 45 5f 55 54  CASE", SQLITE_UT
340e0 46 38 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  F8, SQLITE_COLL_
340e1 4e 4f 43 41 53 45 2c 20 30 2c 0a 20 20 20 20 20  NOCASE, 0,.     
340e2 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 6f 63               noc
340e3 61 73 65 43 6f 6c 6c 61 74 69 6e 67 46 75 6e 63  aseCollatingFunc
340e4 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e  , 0);..  /* Open
340e5 20 74 68 65 20 62 61 63 6b 65 6e 64 20 64 61 74   the backend dat
340e6 61 62 61 73 65 20 64 72 69 76 65 72 20 2a 2f 0a  abase driver */.
340e7 20 20 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20    db->openFlags 
340e8 3d 20 66 6c 61 67 73 3b 0a 20 20 72 63 20 3d 20  = flags;.  rc = 
340e9 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74  sqlite3BtreeFact
340ea 6f 72 79 28 64 62 2c 20 7a 46 69 6c 65 6e 61 6d  ory(db, zFilenam
340eb 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46  e, 0, SQLITE_DEF
340ec 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c  AULT_CACHE_SIZE,
340ed 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
340ee 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6c 61               fla
340ef 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs | SQLITE_OPEN
340f0 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20 20  _MAIN_DB,.      
340f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
340f2 20 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 30 5d       &db->aDb[0]
340f3 2e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63 21  .pBt);.  if( rc!
340f4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
340f5 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
340f6 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a  _IOERR_NOMEM ){.
340f7 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
340f8 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
340f9 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
340fa 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20  db, rc, 0);.    
340fb 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b  goto opendb_out;
340fc 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 5b 30  .  }.  db->aDb[0
340fd 5d 2e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69  ].pSchema = sqli
340fe 74 65 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c  te3SchemaGet(db,
340ff 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29   db->aDb[0].pBt)
34100 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  ;.  db->aDb[1].p
34101 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33  Schema = sqlite3
34102 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 30 29  SchemaGet(db, 0)
34103 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 64 65 66  ;...  /* The def
34104 61 75 6c 74 20 73 61 66 65 74 79 5f 6c 65 76 65  ault safety_leve
34105 6c 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  l for the main d
34106 61 74 61 62 61 73 65 20 69 73 20 27 66 75 6c 6c  atabase is 'full
34107 27 3b 20 66 6f 72 20 74 68 65 20 74 65 6d 70 0a  '; for the temp.
34108 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 74    ** database it
34109 20 69 73 20 27 4e 4f 4e 45 27 2e 20 54 68 69 73   is 'NONE'. This
3410a 20 6d 61 74 63 68 65 73 20 74 68 65 20 70 61 67   matches the pag
3410b 65 72 20 6c 61 79 65 72 20 64 65 66 61 75 6c 74  er layer default
3410c 73 2e 20 20 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e  s.  .  */.  db->
3410d 61 44 62 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 22  aDb[0].zName = "
3410e 6d 61 69 6e 22 3b 0a 20 20 64 62 2d 3e 61 44 62  main";.  db->aDb
3410f 5b 30 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65 6c  [0].safety_level
34110 20 3d 20 33 3b 0a 20 20 64 62 2d 3e 61 44 62 5b   = 3;.  db->aDb[
34111 31 5d 2e 7a 4e 61 6d 65 20 3d 20 22 74 65 6d 70  1].zName = "temp
34112 22 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e  ";.  db->aDb[1].
34113 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 31  safety_level = 1
34114 3b 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  ;..  db->magic =
34115 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
34116 45 4e 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  EN;.  if( db->ma
34117 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
34118 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75    goto opendb_ou
34119 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 67  t;.  }..  /* Reg
3411a 69 73 74 65 72 20 61 6c 6c 20 62 75 69 6c 74 2d  ister all built-
3411b 69 6e 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62 75  in functions, bu
3411c 74 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74  t do not attempt
3411d 20 74 6f 20 72 65 61 64 20 74 68 65 0a 20 20 2a   to read the.  *
3411e 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  * database schem
3411f 61 20 79 65 74 2e 20 54 68 69 73 20 69 73 20 64  a yet. This is d
34120 65 6c 61 79 65 64 20 75 6e 74 69 6c 20 74 68 65  elayed until the
34121 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 65 20   first time the 
34122 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 69 73  database.  ** is
34123 20 61 63 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a   accessed..  */.
34124 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
34125 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29  b, SQLITE_OK, 0)
34126 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73  ;.  sqlite3Regis
34127 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69  terBuiltinFuncti
34128 6f 6e 73 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4c  ons(db);..  /* L
34129 6f 61 64 20 61 75 74 6f 6d 61 74 69 63 20 65 78  oad automatic ex
3412a 74 65 6e 73 69 6f 6e 73 20 2d 20 65 78 74 65 6e  tensions - exten
3412b 73 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20  sions that have 
3412c 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 0a  been registered.
3412d 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73    ** using the s
3412e 71 6c 69 74 65 33 5f 61 75 74 6f 6d 61 74 69 63  qlite3_automatic
3412f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 41 50 49  _extension() API
34130 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
34131 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  AutoLoadExtensio
34132 6e 73 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73  ns(db);.  rc = s
34133 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64  qlite3_errcode(d
34134 62 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  b);.  if( rc!=SQ
34135 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67  LITE_OK ){.    g
34136 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a  oto opendb_out;.
34137 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49    }..#ifdef SQLI
34138 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 31 0a 20  TE_ENABLE_FTS1. 
34139 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
3413a 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 65 78  Failed ){.    ex
3413b 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
3413c 46 74 73 31 49 6e 69 74 28 73 71 6c 69 74 65 33  Fts1Init(sqlite3
3413d 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  *);.    rc = sql
3413e 69 74 65 33 46 74 73 31 49 6e 69 74 28 64 62 29  ite3Fts1Init(db)
3413f 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
34140 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
34141 4c 45 5f 46 54 53 32 0a 20 20 69 66 28 20 21 64  LE_FTS2.  if( !d
34142 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
34143 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
34144 20 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69   ){.    extern i
34145 6e 74 20 73 71 6c 69 74 65 33 46 74 73 32 49 6e  nt sqlite3Fts2In
34146 69 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20  it(sqlite3*);.  
34147 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74    rc = sqlite3Ft
34148 73 32 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a  s2Init(db);.  }.
34149 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
3414a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
3414b 33 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c  3.  if( !db->mal
3414c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d  locFailed && rc=
3414d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3414e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74    rc = sqlite3Ft
3414f 73 33 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a  s3Init(db);.  }.
34150 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
34151 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55  QLITE_ENABLE_ICU
34152 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c  .  if( !db->mall
34153 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d  ocFailed && rc==
34154 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
34155 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 63 75   rc = sqlite3Icu
34156 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65  Init(db);.  }.#e
34157 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
34158 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45  ITE_ENABLE_RTREE
34159 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c  .  if( !db->mall
3415a 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d  ocFailed && rc==
3415b 53 51 4c 49 54 45 5f 4f 4b 29 7b 0a 20 20 20 20  SQLITE_OK){.    
3415c 72 63 20 3d 20 73 71 6c 69 74 65 33 52 74 72 65  rc = sqlite3Rtre
3415d 65 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23  eInit(db);.  }.#
3415e 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
3415f 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29  Error(db, rc, 0)
34160 3b 0a 0a 20 20 2f 2a 20 2d 44 53 51 4c 49 54 45  ;..  /* -DSQLITE
34161 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47  _DEFAULT_LOCKING
34162 5f 4d 4f 44 45 3d 31 20 6d 61 6b 65 73 20 45 58  _MODE=1 makes EX
34163 43 4c 55 53 49 56 45 20 74 68 65 20 64 65 66 61  CLUSIVE the defa
34164 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a  ult locking.  **
34165 20 6d 6f 64 65 2e 20 20 2d 44 53 51 4c 49 54 45   mode.  -DSQLITE
34166 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47  _DEFAULT_LOCKING
34167 5f 4d 4f 44 45 3d 30 20 6d 61 6b 65 20 4e 4f 52  _MODE=0 make NOR
34168 4d 41 4c 20 74 68 65 20 64 65 66 61 75 6c 74 20  MAL the default 
34169 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f 64  locking.  ** mod
3416a 65 2e 20 20 44 6f 69 6e 67 20 6e 6f 74 68 69 6e  e.  Doing nothin
3416b 67 20 61 74 20 61 6c 6c 20 61 6c 73 6f 20 6d 61  g at all also ma
3416c 6b 65 73 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64  kes NORMAL the d
3416d 65 66 61 75 6c 74 2e 0a 20 20 2a 2f 0a 23 69 66  efault..  */.#if
3416e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
3416f 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 0a  LT_LOCKING_MODE.
34170 20 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f    db->dfltLockMo
34171 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  de = SQLITE_DEFA
34172 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45  ULT_LOCKING_MODE
34173 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ;.  sqlite3Pager
34174 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 73 71 6c 69  LockingMode(sqli
34175 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64 62  te3BtreePager(db
34176 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 2c 0a 20  ->aDb[0].pBt),. 
34177 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34178 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
34179 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f  DEFAULT_LOCKING_
3417a 4d 4f 44 45 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  MODE);.#endif.. 
3417b 20 2f 2a 20 45 6e 61 62 6c 65 20 74 68 65 20 6c   /* Enable the l
3417c 6f 6f 6b 61 73 69 64 65 2d 6d 61 6c 6c 6f 63 20  ookaside-malloc 
3417d 73 75 62 73 79 73 74 65 6d 20 2a 2f 0a 20 20 73  subsystem */.  s
3417e 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 64 62  etupLookaside(db
3417f 2c 20 30 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62  , 0, sqlite3Glob
34180 61 6c 43 6f 6e 66 69 67 2e 73 7a 4c 6f 6f 6b 61  alConfig.szLooka
34181 73 69 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20  side,.          
34182 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
34183 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
34184 67 2e 6e 4c 6f 6f 6b 61 73 69 64 65 29 3b 0a 0a  g.nLookaside);..
34185 6f 70 65 6e 64 62 5f 6f 75 74 3a 0a 20 20 69 66  opendb_out:.  if
34186 28 20 64 62 20 29 7b 0a 20 20 20 20 61 73 73 65  ( db ){.    asse
34187 72 74 28 20 64 62 2d 3e 6d 75 74 65 78 21 3d 30  rt( db->mutex!=0
34188 20 7c 7c 20 69 73 54 68 72 65 61 64 73 61 66 65   || isThreadsafe
34189 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c  ==0 || sqlite3Gl
3418a 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c  obalConfig.bFull
3418b 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20  Mutex==0 );.    
3418c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
3418d 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
3418e 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
3418f 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29 3b 0a  e3_errcode(db);.
34190 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
34191 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71  _NOMEM ){.    sq
34192 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 3b  lite3_close(db);
34193 0a 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20 7d  .    db = 0;.  }
34194 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c  else if( rc!=SQL
34195 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 64 62  ITE_OK ){.    db
34196 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45  ->magic = SQLITE
34197 5f 4d 41 47 49 43 5f 53 49 43 4b 3b 0a 20 20 7d  _MAGIC_SICK;.  }
34198 0a 20 20 2a 70 70 44 62 20 3d 20 64 62 3b 0a 20  .  *ppDb = db;. 
34199 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41   return sqlite3A
3419a 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d  piExit(0, rc);.}
3419b 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e  ../*.** Open a n
3419c 65 77 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  ew database hand
3419d 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
3419e 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70  I int sqlite3_op
3419f 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  en(.  const char
341a0 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a 20 20   *zFilename, .  
341a1 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 0a  sqlite3 **ppDb .
341a2 29 7b 0a 20 20 72 65 74 75 72 6e 20 6f 70 65 6e  ){.  return open
341a3 44 61 74 61 62 61 73 65 28 7a 46 69 6c 65 6e 61  Database(zFilena
341a4 6d 65 2c 20 70 70 44 62 2c 0a 20 20 20 20 20 20  me, ppDb,.      
341a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
341a6 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
341a7 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f  WRITE | SQLITE_O
341a8 50 45 4e 5f 43 52 45 41 54 45 2c 20 30 29 3b 0a  PEN_CREATE, 0);.
341a9 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
341aa 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
341ab 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
341ac 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44  filename,   /* D
341ad 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
341ae 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71   (UTF-8) */.  sq
341af 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20  lite3 **ppDb,   
341b0 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51        /* OUT: SQ
341b1 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a  Lite db handle *
341b2 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
341b3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
341b4 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  lags */.  const 
341b5 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20  char *zVfs      
341b6 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46 53    /* Name of VFS
341b7 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 2a   module to use *
341b8 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 6f 70  /.){.  return op
341b9 65 6e 44 61 74 61 62 61 73 65 28 66 69 6c 65 6e  enDatabase(filen
341ba 61 6d 65 2c 20 70 70 44 62 2c 20 66 6c 61 67 73  ame, ppDb, flags
341bb 2c 20 7a 56 66 73 29 3b 0a 7d 0a 0a 23 69 66 6e  , zVfs);.}..#ifn
341bc 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
341bd 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  UTF16./*.** Open
341be 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
341bf 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  handle..*/.SQLIT
341c0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
341c1 33 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73  3_open16(.  cons
341c2 74 20 76 6f 69 64 20 2a 7a 46 69 6c 65 6e 61 6d  t void *zFilenam
341c3 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a  e, .  sqlite3 **
341c4 70 70 44 62 0a 29 7b 0a 20 20 63 68 61 72 20 63  ppDb.){.  char c
341c5 6f 6e 73 74 20 2a 7a 46 69 6c 65 6e 61 6d 65 38  onst *zFilename8
341c6 3b 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65  ;   /* zFilename
341c7 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d   encoded in UTF-
341c8 38 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46  8 instead of UTF
341c9 2d 31 36 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -16 */.  sqlite3
341ca 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20  _value *pVal;.  
341cb 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
341cc 74 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a  t( zFilename );.
341cd 20 20 61 73 73 65 72 74 28 20 70 70 44 62 20 29    assert( ppDb )
341ce 3b 0a 20 20 2a 70 70 44 62 20 3d 20 30 3b 0a 23  ;.  *ppDb = 0;.#
341cf 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
341d0 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63  IT_AUTOINIT.  rc
341d1 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69   = sqlite3_initi
341d2 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72  alize();.  if( r
341d3 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23  c ) return rc;.#
341d4 65 6e 64 69 66 0a 20 20 70 56 61 6c 20 3d 20 73  endif.  pVal = s
341d5 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 30  qlite3ValueNew(0
341d6 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75  );.  sqlite3Valu
341d7 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31  eSetStr(pVal, -1
341d8 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 53 51 4c  , zFilename, SQL
341d9 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c  ITE_UTF16NATIVE,
341da 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
341db 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 38 20 3d 20  .  zFilename8 = 
341dc 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
341dd 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54  (pVal, SQLITE_UT
341de 46 38 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65  F8);.  if( zFile
341df 6e 61 6d 65 38 20 29 7b 0a 20 20 20 20 72 63 20  name8 ){.    rc 
341e0 3d 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 7a  = openDatabase(z
341e1 46 69 6c 65 6e 61 6d 65 38 2c 20 70 70 44 62 2c  Filename8, ppDb,
341e2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
341e3 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
341e4 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53  EN_READWRITE | S
341e5 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
341e6 45 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72  E, 0);.    asser
341e7 74 28 20 2a 70 70 44 62 20 7c 7c 20 72 63 3d 3d  t( *ppDb || rc==
341e8 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
341e9 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
341ea 54 45 5f 4f 4b 20 26 26 20 21 44 62 48 61 73 50  TE_OK && !DbHasP
341eb 72 6f 70 65 72 74 79 28 2a 70 70 44 62 2c 20 30  roperty(*ppDb, 0
341ec 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  , DB_SchemaLoade
341ed 64 29 20 29 7b 0a 20 20 20 20 20 20 45 4e 43 28  d) ){.      ENC(
341ee 2a 70 70 44 62 29 20 3d 20 53 51 4c 49 54 45 5f  *ppDb) = SQLITE_
341ef 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20  UTF16NATIVE;.   
341f0 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
341f1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
341f2 4d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  M;.  }.  sqlite3
341f3 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b  ValueFree(pVal);
341f4 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
341f5 65 33 41 70 69 45 78 69 74 28 30 2c 20 72 63 29  e3ApiExit(0, rc)
341f6 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
341f7 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
341f8 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  */../*.** Regist
341f9 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69  er a new collati
341fa 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68  on sequence with
341fb 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
341fc 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49  ndle db..*/.SQLI
341fd 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
341fe 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
341ff 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  ion(.  sqlite3* 
34200 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  db, .  const cha
34201 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74  r *zName, .  int
34202 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70   enc, .  void* p
34203 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d  Ctx,.  int(*xCom
34204 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  pare)(void*,int,
34205 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
34206 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a  const void*).){.
34207 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
34208 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
34209 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
3420a 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
3420b 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 63 20  cFailed );.  rc 
3420c 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f  = createCollatio
3420d 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 28 75 38  n(db, zName, (u8
3420e 29 65 6e 63 2c 20 53 51 4c 49 54 45 5f 43 4f 4c  )enc, SQLITE_COL
3420f 4c 5f 55 53 45 52 2c 20 70 43 74 78 2c 20 78 43  L_USER, pCtx, xC
34210 6f 6d 70 61 72 65 2c 20 30 29 3b 0a 20 20 72 63  ompare, 0);.  rc
34211 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
34212 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
34213 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
34214 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
34215 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
34216 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e 65  ** Register a ne
34217 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  w collation sequ
34218 65 6e 63 65 20 77 69 74 68 20 74 68 65 20 64 61  ence with the da
34219 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62  tabase handle db
3421a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3421b 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
3421c 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28  te_collation_v2(
3421d 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20  .  sqlite3* db, 
3421e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3421f 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63  Name, .  int enc
34220 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c  , .  void* pCtx,
34221 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65  .  int(*xCompare
34222 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
34223 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
34224 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64  t void*),.  void
34225 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29  (*xDel)(void*).)
34226 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
34227 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
34228 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
34229 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c  assert( !db->mal
3422a 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72  locFailed );.  r
3422b 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74  c = createCollat
3422c 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 28  ion(db, zName, (
3422d 75 38 29 65 6e 63 2c 20 53 51 4c 49 54 45 5f 43  u8)enc, SQLITE_C
3422e 4f 4c 4c 5f 55 53 45 52 2c 20 70 43 74 78 2c 20  OLL_USER, pCtx, 
3422f 78 43 6f 6d 70 61 72 65 2c 20 78 44 65 6c 29 3b  xCompare, xDel);
34230 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
34231 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a  piExit(db, rc);.
34232 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
34233 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
34234 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
34235 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
34236 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a  _OMIT_UTF16./*.*
34237 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e 65 77  * Register a new
34238 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
34239 6e 63 65 20 77 69 74 68 20 74 68 65 20 64 61 74  nce with the dat
3423a 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e  abase handle db.
3423b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
3423c 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
3423d 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20  e_collation16(. 
3423e 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20   sqlite3* db, . 
3423f 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61   const void *zNa
34240 6d 65 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a  me,.  int enc, .
34241 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20    void* pCtx,.  
34242 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76  int(*xCompare)(v
34243 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
34244 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
34245 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72  oid*).){.  int r
34246 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
34247 20 63 68 61 72 20 2a 7a 4e 61 6d 65 38 3b 0a 20   char *zName8;. 
34248 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
34249 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
3424a 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e  .  assert( !db->
3424b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
3424c 20 20 7a 4e 61 6d 65 38 20 3d 20 73 71 6c 69 74    zName8 = sqlit
3424d 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a  e3Utf16to8(db, z
3424e 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 69 66 28  Name, -1);.  if(
3424f 20 7a 4e 61 6d 65 38 20 29 7b 0a 20 20 20 20 72   zName8 ){.    r
34250 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74  c = createCollat
34251 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 38 2c 20  ion(db, zName8, 
34252 28 75 38 29 65 6e 63 2c 20 53 51 4c 49 54 45 5f  (u8)enc, SQLITE_
34253 43 4f 4c 4c 5f 55 53 45 52 2c 20 70 43 74 78 2c  COLL_USER, pCtx,
34254 20 78 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0a 20   xCompare, 0);. 
34255 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
34256 28 64 62 2c 20 7a 4e 61 6d 65 38 29 3b 0a 20 20  (db, zName8);.  
34257 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
34258 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
34259 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3425a 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
3425b 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
3425c 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
3425d 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
3425e 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
3425f 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71   a collation seq
34260 75 65 6e 63 65 20 66 61 63 74 6f 72 79 20 63 61  uence factory ca
34261 6c 6c 62 61 63 6b 20 77 69 74 68 20 74 68 65 20  llback with the 
34262 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a  database handle.
34263 2a 2a 20 64 62 2e 20 52 65 70 6c 61 63 65 20 61  ** db. Replace a
34264 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e  ny previously in
34265 73 74 61 6c 6c 65 64 20 63 6f 6c 6c 61 74 69 6f  stalled collatio
34266 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f  n sequence facto
34267 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ry..*/.SQLITE_AP
34268 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
34269 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 0a  llation_needed(.
3426a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a    sqlite3 *db, .
3426b 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65    void *pCollNee
3426c 64 65 64 41 72 67 2c 20 0a 20 20 76 6f 69 64 28  dedArg, .  void(
3426d 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f  *xCollNeeded)(vo
3426e 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
3426f 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
34270 63 68 61 72 2a 29 0a 29 7b 0a 20 20 73 71 6c 69  char*).){.  sqli
34271 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
34272 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62  db->mutex);.  db
34273 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20  ->xCollNeeded = 
34274 78 43 6f 6c 6c 4e 65 65 64 65 64 3b 0a 20 20 64  xCollNeeded;.  d
34275 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36  b->xCollNeeded16
34276 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c   = 0;.  db->pCol
34277 6c 4e 65 65 64 65 64 41 72 67 20 3d 20 70 43 6f  lNeededArg = pCo
34278 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 73  llNeededArg;.  s
34279 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3427a 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
3427b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3427c 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  K;.}..#ifndef SQ
3427d 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
3427e 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
3427f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
34280 6e 63 65 20 66 61 63 74 6f 72 79 20 63 61 6c 6c  nce factory call
34281 62 61 63 6b 20 77 69 74 68 20 74 68 65 20 64 61  back with the da
34282 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a  tabase handle.**
34283 20 64 62 2e 20 52 65 70 6c 61 63 65 20 61 6e 79   db. Replace any
34284 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 74   previously inst
34285 61 6c 6c 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  alled collation 
34286 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79  sequence factory
34287 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
34288 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  int sqlite3_coll
34289 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 0a  ation_needed16(.
3428a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a    sqlite3 *db, .
3428b 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65    void *pCollNee
3428c 64 65 64 41 72 67 2c 20 0a 20 20 76 6f 69 64 28  dedArg, .  void(
3428d 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28  *xCollNeeded16)(
3428e 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69  void*,sqlite3*,i
3428f 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73  nt eTextRep,cons
34290 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 71  t void*).){.  sq
34291 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
34292 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
34293 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20  db->xCollNeeded 
34294 3d 20 30 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c  = 0;.  db->xColl
34295 4e 65 65 64 65 64 31 36 20 3d 20 78 43 6f 6c 6c  Needed16 = xColl
34296 4e 65 65 64 65 64 31 36 3b 0a 20 20 64 62 2d 3e  Needed16;.  db->
34297 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20 3d  pCollNeededArg =
34298 20 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b   pCollNeededArg;
34299 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3429a 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
3429b 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
3429c 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20  TE_OK;.}.#endif 
3429d 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
3429e 54 46 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  TF16 */..#ifndef
3429f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 4c 4f   SQLITE_OMIT_GLO
342a0 42 41 4c 52 45 43 4f 56 45 52 0a 23 69 66 6e 64  BALRECOVER.#ifnd
342a1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
342a2 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20  EPRECATED./*.** 
342a3 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
342a4 20 6e 6f 77 20 61 6e 20 61 6e 61 63 68 72 6f 6e   now an anachron
342a5 69 73 6d 2e 20 49 74 20 75 73 65 64 20 74 6f 20  ism. It used to 
342a6 62 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f 76  be used to recov
342a7 65 72 20 66 72 6f 6d 20 61 0a 2a 2a 20 6d 61 6c  er from a.** mal
342a8 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 2c 20 62  loc() failure, b
342a9 75 74 20 53 51 4c 69 74 65 20 6e 6f 77 20 64 6f  ut SQLite now do
342aa 65 73 20 74 68 69 73 20 61 75 74 6f 6d 61 74 69  es this automati
342ab 63 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cally..*/.SQLITE
342ac 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
342ad 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28  _global_recover(
342ae 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20  void){.  return 
342af 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
342b0 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  dif.#endif../*.*
342b1 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20 77 68  * Test to see wh
342b2 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
342b3 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
342b4 74 69 6f 6e 20 69 73 20 69 6e 20 61 75 74 6f 63  tion is in autoc
342b5 6f 6d 6d 69 74 0a 2a 2a 20 6d 6f 64 65 2e 20 20  ommit.** mode.  
342b6 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69  Return TRUE if i
342b7 74 20 69 73 20 61 6e 64 20 46 41 4c 53 45 20 69  t is and FALSE i
342b8 66 20 6e 6f 74 2e 20 20 41 75 74 6f 63 6f 6d 6d  f not.  Autocomm
342b9 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 0a 2a 2a  it mode is on.**
342ba 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20 41 75   by default.  Au
342bb 74 6f 63 6f 6d 6d 69 74 20 69 73 20 64 69 73 61  tocommit is disa
342bc 62 6c 65 64 20 62 79 20 61 20 42 45 47 49 4e 20  bled by a BEGIN 
342bd 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65  statement and re
342be 65 6e 61 62 6c 65 64 0a 2a 2a 20 62 79 20 74 68  enabled.** by th
342bf 65 20 6e 65 78 74 20 43 4f 4d 4d 49 54 20 6f 72  e next COMMIT or
342c0 20 52 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a   ROLLBACK..**.**
342c1 2a 2a 2a 2a 2a 20 54 48 49 53 20 49 53 20 41 4e  ***** THIS IS AN
342c2 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 41 50   EXPERIMENTAL AP
342c3 49 20 41 4e 44 20 49 53 20 53 55 42 4a 45 43 54  I AND IS SUBJECT
342c4 20 54 4f 20 43 48 41 4e 47 45 20 2a 2a 2a 2a 2a   TO CHANGE *****
342c5 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  *.*/.SQLITE_API 
342c6 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  int sqlite3_get_
342c7 61 75 74 6f 63 6f 6d 6d 69 74 28 73 71 6c 69 74  autocommit(sqlit
342c8 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72  e3 *db){.  retur
342c9 6e 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74  n db->autoCommit
342ca 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
342cb 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54  TE_DEBUG./*.** T
342cc 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
342cd 74 69 6e 65 20 69 73 20 73 75 62 74 69 74 75 74  tine is subtitut
342ce 65 64 20 66 6f 72 20 63 6f 6e 73 74 61 6e 74 20  ed for constant 
342cf 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69  SQLITE_CORRUPT i
342d0 6e 0a 2a 2a 20 64 65 62 75 67 67 69 6e 67 20 62  n.** debugging b
342d1 75 69 6c 64 73 2e 20 20 54 68 69 73 20 70 72 6f  uilds.  This pro
342d2 76 69 64 65 73 20 61 20 77 61 79 20 74 6f 20 73  vides a way to s
342d3 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e 74 20  et a breakpoint 
342d4 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 63 6f 72 72  for when.** corr
342d5 75 70 74 69 6f 6e 20 69 73 20 66 69 72 73 74 20  uption is first 
342d6 64 65 74 65 63 74 65 64 2e 0a 2a 2f 0a 53 51 4c  detected..*/.SQL
342d7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
342d8 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76  sqlite3Corrupt(v
342d9 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 53  oid){.  return S
342da 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 7d  QLITE_CORRUPT;.}
342db 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
342dc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
342dd 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 54 68  RECATED./*.** Th
342de 69 73 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65  is is a convenie
342df 6e 63 65 20 72 6f 75 74 69 6e 65 20 74 68 61 74  nce routine that
342e0 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61 74   makes sure that
342e1 20 61 6c 6c 20 74 68 72 65 61 64 2d 73 70 65 63   all thread-spec
342e2 69 66 69 63 0a 2a 2a 20 64 61 74 61 20 66 6f 72  ific.** data for
342e3 20 74 68 69 73 20 74 68 72 65 61 64 20 68 61 73   this thread has
342e4 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65   been deallocate
342e5 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  d..**.** SQLite 
342e6 6e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73 20 74  no longer uses t
342e7 68 72 65 61 64 2d 73 70 65 63 69 66 69 63 20 64  hread-specific d
342e8 61 74 61 20 73 6f 20 74 68 69 73 20 72 6f 75 74  ata so this rout
342e9 69 6e 65 20 69 73 20 6e 6f 77 20 61 0a 2a 2a 20  ine is now a.** 
342ea 6e 6f 2d 6f 70 2e 20 20 49 74 20 69 73 20 72 65  no-op.  It is re
342eb 74 61 69 6e 65 64 20 66 6f 72 20 68 69 73 74 6f  tained for histo
342ec 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c  rical compatibil
342ed 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ity..*/.SQLITE_A
342ee 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
342ef 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76  thread_cleanup(v
342f0 6f 69 64 29 7b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  oid){.}.#endif..
342f1 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6d 65 74  /*.** Return met
342f2 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  a information ab
342f3 6f 75 74 20 61 20 73 70 65 63 69 66 69 63 20 63  out a specific c
342f4 6f 6c 75 6d 6e 20 6f 66 20 61 20 64 61 74 61 62  olumn of a datab
342f5 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 53 65  ase table..** Se
342f6 65 20 63 6f 6d 6d 65 6e 74 20 69 6e 20 73 71 6c  e comment in sql
342f7 69 74 65 33 2e 68 20 28 73 71 6c 69 74 65 2e 68  ite3.h (sqlite.h
342f8 2e 69 6e 29 20 66 6f 72 20 64 65 74 61 69 6c 73  .in) for details
342f9 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
342fa 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
342fb 5f 4d 45 54 41 44 41 54 41 0a 53 51 4c 49 54 45  _METADATA.SQLITE
342fc 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
342fd 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  _table_column_me
342fe 74 61 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65  tadata(.  sqlite
342ff 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
34300 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74        /* Connect
34301 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ion handle */.  
34302 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e  const char *zDbN
34303 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 44  ame,        /* D
34304 61 74 61 62 61 73 65 20 6e 61 6d 65 20 6f 72 20  atabase name or 
34305 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  NULL */.  const 
34306 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65  char *zTableName
34307 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e  ,     /* Table n
34308 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ame */.  const c
34309 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65  har *zColumnName
3430a 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e  ,    /* Column n
3430b 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f  ame */.  char co
3430c 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54 79 70 65  nst **pzDataType
3430d 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20  ,    /* OUTPUT: 
3430e 44 65 63 6c 61 72 65 64 20 64 61 74 61 20 74 79  Declared data ty
3430f 70 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e  pe */.  char con
34310 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20  st **pzCollSeq, 
34311 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 43      /* OUTPUT: C
34312 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
34313 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20  e name */.  int 
34314 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 20 20 20  *pNotNull,      
34315 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55          /* OUTPU
34316 54 3a 20 54 72 75 65 20 69 66 20 4e 4f 54 20 4e  T: True if NOT N
34317 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 65  ULL constraint e
34318 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  xists */.  int *
34319 70 50 72 69 6d 61 72 79 4b 65 79 2c 20 20 20 20  pPrimaryKey,    
3431a 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54         /* OUTPUT
3431b 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e  : True if column
3431c 20 70 61 72 74 20 6f 66 20 50 4b 20 2a 2f 0a 20   part of PK */. 
3431d 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e 63 20 20   int *pAutoinc  
3431e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3431f 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20  OUTPUT: True if 
34320 63 6f 6c 75 6d 6e 20 69 73 20 61 75 74 6f 2d 69  column is auto-i
34321 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20  ncrement */.){. 
34322 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20   int rc;.  char 
34323 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  *zErrMsg = 0;.  
34324 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b  Table *pTab = 0;
34325 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20  .  Column *pCol 
34326 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b  = 0;.  int iCol;
34327 0a 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ..  char const *
34328 7a 44 61 74 61 54 79 70 65 20 3d 20 30 3b 0a 20  zDataType = 0;. 
34329 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f   char const *zCo
3432a 6c 6c 53 65 71 20 3d 20 30 3b 0a 20 20 69 6e 74  llSeq = 0;.  int
3432b 20 6e 6f 74 6e 75 6c 6c 20 3d 20 30 3b 0a 20 20   notnull = 0;.  
3432c 69 6e 74 20 70 72 69 6d 61 72 79 6b 65 79 20 3d  int primarykey =
3432d 20 30 3b 0a 20 20 69 6e 74 20 61 75 74 6f 69 6e   0;.  int autoin
3432e 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 45 6e 73  c = 0;..  /* Ens
3432f 75 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ure the database
34330 20 73 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e   schema has been
34331 20 6c 6f 61 64 65 64 20 2a 2f 0a 20 20 73 71 6c   loaded */.  sql
34332 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
34333 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 28  (db->mutex);.  (
34334 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
34335 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69  tyOn(db);.  sqli
34336 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
34337 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  (db);.  rc = sql
34338 69 74 65 33 49 6e 69 74 28 64 62 2c 20 26 7a 45  ite3Init(db, &zE
34339 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 53 51  rrMsg);.  if( SQ
3433a 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20  LITE_OK!=rc ){. 
3433b 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75     goto error_ou
3433c 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63  t;.  }..  /* Loc
3433d 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 69 6e  ate the table in
3433e 20 71 75 65 73 74 69 6f 6e 20 2a 2f 0a 20 20 70   question */.  p
3433f 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Tab = sqlite3Fin
34340 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 6c  dTable(db, zTabl
34341 65 4e 61 6d 65 2c 20 7a 44 62 4e 61 6d 65 29 3b  eName, zDbName);
34342 0a 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c 20  .  if( !pTab || 
34343 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b  pTab->pSelect ){
34344 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20  .    pTab = 0;. 
34345 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75     goto error_ou
34346 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e  t;.  }..  /* Fin
34347 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72  d the column for
34348 20 77 68 69 63 68 20 69 6e 66 6f 20 69 73 20 72   which info is r
34349 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 69 66  equested */.  if
3434a 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64  ( sqlite3IsRowid
3434b 28 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 20 29 7b  (zColumnName) ){
3434c 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 62  .    iCol = pTab
3434d 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 69 66 28  ->iPKey;.    if(
3434e 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20 20   iCol>=0 ){.    
3434f 20 20 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e    pCol = &pTab->
34350 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20  aCol[iCol];.    
34351 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66  }.  }else{.    f
34352 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c  or(iCol=0; iCol<
34353 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c  pTab->nCol; iCol
34354 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 20  ++){.      pCol 
34355 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43  = &pTab->aCol[iC
34356 6f 6c 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 30  ol];.      if( 0
34357 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
34358 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43  (pCol->zName, zC
34359 6f 6c 75 6d 6e 4e 61 6d 65 29 20 29 7b 0a 20 20  olumnName) ){.  
3435a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3435b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
3435c 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e  f( iCol==pTab->n
3435d 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 70 54 61  Col ){.      pTa
3435e 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f 74  b = 0;.      got
3435f 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20  o error_out;.   
34360 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65   }.  }..  /* The
34361 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
34362 20 73 74 6f 72 65 73 20 74 68 65 20 6d 65 74 61   stores the meta
34363 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
34364 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e  t will be return
34365 65 64 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 63  ed.  ** to the c
34366 61 6c 6c 65 72 20 69 6e 20 6c 6f 63 61 6c 20 76  aller in local v
34367 61 72 69 61 62 6c 65 73 20 7a 44 61 74 61 54 79  ariables zDataTy
34368 70 65 2c 20 7a 43 6f 6c 6c 53 65 71 2c 20 6e 6f  pe, zCollSeq, no
34369 74 6e 75 6c 6c 2c 20 70 72 69 6d 61 72 79 6b 65  tnull, primaryke
3436a 79 0a 20 20 2a 2a 20 61 6e 64 20 61 75 74 6f 69  y.  ** and autoi
3436b 6e 63 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e  nc. At this poin
3436c 74 20 74 68 65 72 65 20 61 72 65 20 74 77 6f 20  t there are two 
3436d 70 6f 73 73 69 62 69 6c 69 74 69 65 73 3a 0a 20  possibilities:. 
3436e 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 20 20 31 2e   ** .  **     1.
3436f 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20 63   The specified c
34370 6f 6c 75 6d 6e 20 6e 61 6d 65 20 77 61 73 20 72  olumn name was r
34371 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f 72 20  owid", "oid" or 
34372 22 5f 72 6f 77 69 64 5f 22 20 0a 20 20 2a 2a 20  "_rowid_" .  ** 
34373 20 20 20 20 20 20 20 61 6e 64 20 74 68 65 72 65         and there
34374 20 69 73 20 6e 6f 20 65 78 70 6c 69 63 69 74 6c   is no explicitl
34375 79 20 64 65 63 6c 61 72 65 64 20 49 50 4b 20 63  y declared IPK c
34376 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2a 0a 20 20 2a  olumn. .  **.  *
34377 2a 20 20 20 20 20 32 2e 20 54 68 65 20 74 61 62  *     2. The tab
34378 6c 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77  le is not a view
34379 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20   and the column 
3437a 6e 61 6d 65 20 69 64 65 6e 74 69 66 69 65 64 20  name identified 
3437b 61 6e 20 0a 20 20 2a 2a 20 20 20 20 20 20 20 20  an .  **        
3437c 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61  explicitly decla
3437d 72 65 64 20 63 6f 6c 75 6d 6e 2e 20 43 6f 70 79  red column. Copy
3437e 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f   meta informatio
3437f 6e 20 66 72 6f 6d 20 2a 70 43 6f 6c 2e 0a 20 20  n from *pCol..  
34380 2a 2f 20 0a 20 20 69 66 28 20 70 43 6f 6c 20 29  */ .  if( pCol )
34381 7b 0a 20 20 20 20 7a 44 61 74 61 54 79 70 65 20  {.    zDataType 
34382 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b 0a 20  = pCol->zType;. 
34383 20 20 20 7a 43 6f 6c 6c 53 65 71 20 3d 20 70 43     zCollSeq = pC
34384 6f 6c 2d 3e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 6e  ol->zColl;.    n
34385 6f 74 6e 75 6c 6c 20 3d 20 70 43 6f 6c 2d 3e 6e  otnull = pCol->n
34386 6f 74 4e 75 6c 6c 21 3d 30 3b 0a 20 20 20 20 70  otNull!=0;.    p
34387 72 69 6d 61 72 79 6b 65 79 20 20 3d 20 70 43 6f  rimarykey  = pCo
34388 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 21 3d 30 3b  l->isPrimKey!=0;
34389 0a 20 20 20 20 61 75 74 6f 69 6e 63 20 3d 20 70  .    autoinc = p
3438a 54 61 62 2d 3e 69 50 4b 65 79 3d 3d 69 43 6f 6c  Tab->iPKey==iCol
3438b 20 26 26 20 28 70 54 61 62 2d 3e 74 61 62 46 6c   && (pTab->tabFl
3438c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63  ags & TF_Autoinc
3438d 72 65 6d 65 6e 74 29 21 3d 30 3b 0a 20 20 7d 65  rement)!=0;.  }e
3438e 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 54 79  lse{.    zDataTy
3438f 70 65 20 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a  pe = "INTEGER";.
34390 20 20 20 20 70 72 69 6d 61 72 79 6b 65 79 20 3d      primarykey =
34391 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 7a   1;.  }.  if( !z
34392 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20 20 7a  CollSeq ){.    z
34393 43 6f 6c 6c 53 65 71 20 3d 20 22 42 49 4e 41 52  CollSeq = "BINAR
34394 59 22 3b 0a 20 20 7d 0a 0a 65 72 72 6f 72 5f 6f  Y";.  }..error_o
34395 75 74 3a 0a 20 20 73 71 6c 69 74 65 33 42 74 72  ut:.  sqlite3Btr
34396 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
34397 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
34398 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20  afetyOff(db);.. 
34399 20 2f 2a 20 57 68 65 74 68 65 72 20 74 68 65 20   /* Whether the 
3439a 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 73 75  function call su
3439b 63 63 65 65 64 65 64 20 6f 72 20 66 61 69 6c 65  cceeded or faile
3439c 64 2c 20 73 65 74 20 74 68 65 20 6f 75 74 70 75  d, set the outpu
3439d 74 20 70 61 72 61 6d 65 74 65 72 73 0a 20 20 2a  t parameters.  *
3439e 2a 20 74 6f 20 77 68 61 74 65 76 65 72 20 74 68  * to whatever th
3439f 65 69 72 20 6c 6f 63 61 6c 20 63 6f 75 6e 74 65  eir local counte
343a0 72 70 61 72 74 73 20 63 6f 6e 74 61 69 6e 2e 20  rparts contain. 
343a1 49 66 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20  If an error did 
343a2 6f 63 63 75 72 2c 0a 20 20 2a 2a 20 74 68 69 73  occur,.  ** this
343a3 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20   has the effect 
343a4 6f 66 20 7a 65 72 6f 69 6e 67 20 61 6c 6c 20 6f  of zeroing all o
343a5 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 73  utput parameters
343a6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 7a 44  ..  */.  if( pzD
343a7 61 74 61 54 79 70 65 20 29 20 2a 70 7a 44 61 74  ataType ) *pzDat
343a8 61 54 79 70 65 20 3d 20 7a 44 61 74 61 54 79 70  aType = zDataTyp
343a9 65 3b 0a 20 20 69 66 28 20 70 7a 43 6f 6c 6c 53  e;.  if( pzCollS
343aa 65 71 20 29 20 2a 70 7a 43 6f 6c 6c 53 65 71 20  eq ) *pzCollSeq 
343ab 3d 20 7a 43 6f 6c 6c 53 65 71 3b 0a 20 20 69 66  = zCollSeq;.  if
343ac 28 20 70 4e 6f 74 4e 75 6c 6c 20 29 20 2a 70 4e  ( pNotNull ) *pN
343ad 6f 74 4e 75 6c 6c 20 3d 20 6e 6f 74 6e 75 6c 6c  otNull = notnull
343ae 3b 0a 20 20 69 66 28 20 70 50 72 69 6d 61 72 79  ;.  if( pPrimary
343af 4b 65 79 20 29 20 2a 70 50 72 69 6d 61 72 79 4b  Key ) *pPrimaryK
343b0 65 79 20 3d 20 70 72 69 6d 61 72 79 6b 65 79 3b  ey = primarykey;
343b1 0a 20 20 69 66 28 20 70 41 75 74 6f 69 6e 63 20  .  if( pAutoinc 
343b2 29 20 2a 70 41 75 74 6f 69 6e 63 20 3d 20 61 75  ) *pAutoinc = au
343b3 74 6f 69 6e 63 3b 0a 0a 20 20 69 66 28 20 53 51  toinc;..  if( SQ
343b4 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 21  LITE_OK==rc && !
343b5 70 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69  pTab ){.    sqli
343b6 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45  te3DbFree(db, zE
343b7 72 72 4d 73 67 29 3b 0a 20 20 20 20 7a 45 72 72  rrMsg);.    zErr
343b8 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Msg = sqlite3MPr
343b9 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 75 63  intf(db, "no suc
343ba 68 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 3a 20  h table column: 
343bb 25 73 2e 25 73 22 2c 20 7a 54 61 62 6c 65 4e 61  %s.%s", zTableNa
343bc 6d 65 2c 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c  me,.        zCol
343bd 75 6d 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 72 63  umnName);.    rc
343be 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
343bf 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72  .  }.  sqlite3Er
343c0 72 6f 72 28 64 62 2c 20 72 63 2c 20 28 7a 45 72  ror(db, rc, (zEr
343c1 72 4d 73 67 3f 22 25 73 22 3a 30 29 2c 20 7a 45  rMsg?"%s":0), zE
343c2 72 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65  rrMsg);.  sqlite
343c3 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
343c4 4d 73 67 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  Msg);.  rc = sql
343c5 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
343c6 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
343c7 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
343c8 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
343c9 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  rc;.}.#endif../*
343ca 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20  .** Sleep for a 
343cb 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52  little while.  R
343cc 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74  eturn the amount
343cd 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a   of time slept..
343ce 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
343cf 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28  t sqlite3_sleep(
343d0 69 6e 74 20 6d 73 29 7b 0a 20 20 73 71 6c 69 74  int ms){.  sqlit
343d1 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 0a 20 20  e3_vfs *pVfs;.  
343d2 69 6e 74 20 72 63 3b 0a 20 20 70 56 66 73 20 3d  int rc;.  pVfs =
343d3 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e   sqlite3_vfs_fin
343d4 64 28 30 29 3b 0a 20 20 69 66 28 20 70 56 66 73  d(0);.  if( pVfs
343d5 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
343d6 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74  .  /* This funct
343d7 69 6f 6e 20 77 6f 72 6b 73 20 69 6e 20 6d 69 6c  ion works in mil
343d8 6c 69 73 65 63 6f 6e 64 73 2c 20 62 75 74 20 74  liseconds, but t
343d9 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 4f 73  he underlying Os
343da 53 6c 65 65 70 28 29 20 0a 20 20 2a 2a 20 41 50  Sleep() .  ** AP
343db 49 20 75 73 65 73 20 6d 69 63 72 6f 73 65 63 6f  I uses microseco
343dc 6e 64 73 2e 20 48 65 6e 63 65 20 74 68 65 20 31  nds. Hence the 1
343dd 30 30 30 27 73 2e 0a 20 20 2a 2f 0a 20 20 72 63  000's..  */.  rc
343de 20 3d 20 28 73 71 6c 69 74 65 33 4f 73 53 6c 65   = (sqlite3OsSle
343df 65 70 28 70 56 66 73 2c 20 31 30 30 30 2a 6d 73  ep(pVfs, 1000*ms
343e0 29 2f 31 30 30 30 29 3b 0a 20 20 72 65 74 75 72  )/1000);.  retur
343e1 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  n rc;.}../*.** E
343e2 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65  nable or disable
343e3 20 74 68 65 20 65 78 74 65 6e 64 65 64 20 72 65   the extended re
343e4 73 75 6c 74 20 63 6f 64 65 73 2e 0a 2a 2f 0a 53  sult codes..*/.S
343e5 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
343e6 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72  lite3_extended_r
343e7 65 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69  esult_codes(sqli
343e8 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f  te3 *db, int ono
343e9 66 66 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ff){.  sqlite3_m
343ea 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
343eb 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 65 72 72  utex);.  db->err
343ec 4d 61 73 6b 20 3d 20 6f 6e 6f 66 66 20 3f 20 30  Mask = onoff ? 0
343ed 78 66 66 66 66 66 66 66 66 20 3a 20 30 78 66 66  xffffffff : 0xff
343ee 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
343ef 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
343f0 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
343f1 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
343f2 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69 6c   Invoke the xFil
343f3 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20  eControl method 
343f4 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  on a particular 
343f5 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c  database..*/.SQL
343f6 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
343f7 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  te3_file_control
343f8 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
343f9 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d  nst char *zDbNam
343fa 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20  e, int op, void 
343fb 2a 70 41 72 67 29 7b 0a 20 20 69 6e 74 20 72 63  *pArg){.  int rc
343fc 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
343fd 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 73 71  .  int iDb;.  sq
343fe 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
343ff 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
34400 69 66 28 20 7a 44 62 4e 61 6d 65 3d 3d 30 20 29  if( zDbName==0 )
34401 7b 0a 20 20 20 20 69 44 62 20 3d 20 30 3b 0a 20  {.    iDb = 0;. 
34402 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28   }else{.    for(
34403 69 44 62 3d 30 3b 20 69 44 62 3c 64 62 2d 3e 6e  iDb=0; iDb<db->n
34404 44 62 3b 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20  Db; iDb++){.    
34405 20 20 69 66 28 20 73 74 72 63 6d 70 28 64 62 2d    if( strcmp(db-
34406 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
34407 20 7a 44 62 4e 61 6d 65 29 3d 3d 30 20 29 20 62   zDbName)==0 ) b
34408 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
34409 20 20 69 66 28 20 69 44 62 3c 64 62 2d 3e 6e 44    if( iDb<db->nD
3440a 62 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  b ){.    Btree *
3440b 70 42 74 72 65 65 20 3d 20 64 62 2d 3e 61 44 62  pBtree = db->aDb
3440c 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20 69  [iDb].pBt;.    i
3440d 66 28 20 70 42 74 72 65 65 20 29 7b 0a 20 20 20  f( pBtree ){.   
3440e 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72     Pager *pPager
3440f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
34410 66 69 6c 65 20 2a 66 64 3b 0a 20 20 20 20 20 20  file *fd;.      
34411 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
34412 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 20  r(pBtree);.     
34413 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65   pPager = sqlite
34414 33 42 74 72 65 65 50 61 67 65 72 28 70 42 74 72  3BtreePager(pBtr
34415 65 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ee);.      asser
34416 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a  t( pPager!=0 );.
34417 20 20 20 20 20 20 66 64 20 3d 20 73 71 6c 69 74        fd = sqlit
34418 65 33 50 61 67 65 72 46 69 6c 65 28 70 50 61 67  e3PagerFile(pPag
34419 65 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  er);.      asser
3441a 74 28 20 66 64 21 3d 30 20 29 3b 0a 20 20 20 20  t( fd!=0 );.    
3441b 20 20 69 66 28 20 66 64 2d 3e 70 4d 65 74 68 6f    if( fd->pMetho
3441c 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  ds ){.        rc
3441d 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
3441e 43 6f 6e 74 72 6f 6c 28 66 64 2c 20 6f 70 2c 20  Control(fd, op, 
3441f 70 41 72 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20  pArg);.      }. 
34420 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
34421 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a  eLeave(pBtree);.
34422 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
34423 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
34424 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
34425 74 75 72 6e 20 72 63 3b 20 20 20 0a 7d 0a 0a 2f  turn rc;   .}../
34426 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 20 74  *.** Interface t
34427 6f 20 74 68 65 20 74 65 73 74 69 6e 67 20 6c 6f  o the testing lo
34428 67 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  gic..*/.SQLITE_A
34429 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74  PI int sqlite3_t
3442a 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20  est_control(int 
3442b 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 69 6e 74 20  op, ...){.  int 
3442c 72 63 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20  rc = 0;.#ifndef 
3442d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
3442e 54 49 4e 5f 54 45 53 54 0a 20 20 76 61 5f 6c 69  TIN_TEST.  va_li
3442f 73 74 20 61 70 3b 0a 20 20 76 61 5f 73 74 61 72  st ap;.  va_star
34430 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69  t(ap, op);.  swi
34431 74 63 68 28 20 6f 70 20 29 7b 0a 0a 20 20 20 20  tch( op ){..    
34432 2f 2a 0a 20 20 20 20 2a 2a 20 53 61 76 65 20 74  /*.    ** Save t
34433 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
34434 20 6f 66 20 74 68 65 20 50 52 4e 47 2e 0a 20 20   of the PRNG..  
34435 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51    */.    case SQ
34436 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52  LITE_TESTCTRL_PR
34437 4e 47 5f 53 41 56 45 3a 20 7b 0a 20 20 20 20 20  NG_SAVE: {.     
34438 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65   sqlite3PrngSave
34439 53 74 61 74 65 28 29 3b 0a 20 20 20 20 20 20 62  State();.      b
3443a 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
3443b 20 2f 2a 0a 20 20 20 20 2a 2a 20 52 65 73 74 6f   /*.    ** Resto
3443c 72 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  re the state of 
3443d 74 68 65 20 50 52 4e 47 20 74 6f 20 74 68 65 20  the PRNG to the 
3443e 6c 61 73 74 20 73 74 61 74 65 20 73 61 76 65 64  last state saved
3443f 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 50 52   using.    ** PR
34440 4e 47 5f 53 41 56 45 2e 20 20 49 66 20 50 52 4e  NG_SAVE.  If PRN
34441 47 5f 53 41 56 45 20 68 61 73 20 6e 65 76 65 72  G_SAVE has never
34442 20 62 65 66 6f 72 65 20 62 65 65 6e 20 63 61 6c   before been cal
34443 6c 65 64 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a  led, then.    **
34444 20 74 68 69 73 20 76 65 72 62 20 61 63 74 73 20   this verb acts 
34445 6c 69 6b 65 20 50 52 4e 47 5f 52 45 53 45 54 2e  like PRNG_RESET.
34446 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
34447 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
34448 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45 3a 20 7b  _PRNG_RESTORE: {
34449 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 72  .      sqlite3Pr
3444a 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 29  ngRestoreState()
3444b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
3444c 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
3444d 20 2a 2a 20 52 65 73 65 74 20 74 68 65 20 50 52   ** Reset the PR
3444e 4e 47 20 62 61 63 6b 20 74 6f 20 69 74 73 20 75  NG back to its u
3444f 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 74 61  ninitialized sta
34450 74 65 2e 20 20 54 68 65 20 6e 65 78 74 20 63 61  te.  The next ca
34451 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c  ll.    ** to sql
34452 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
34453 29 20 77 69 6c 6c 20 72 65 73 65 65 64 20 74 68  ) will reseed th
34454 65 20 50 52 4e 47 20 75 73 69 6e 67 20 61 20 73  e PRNG using a s
34455 69 6e 67 6c 65 20 63 61 6c 6c 0a 20 20 20 20 2a  ingle call.    *
34456 2a 20 74 6f 20 74 68 65 20 78 52 61 6e 64 6f 6d  * to the xRandom
34457 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 74  ness method of t
34458 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 2e 0a  he default VFS..
34459 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
3445a 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3445b 50 52 4e 47 5f 52 45 53 45 54 3a 20 7b 0a 20 20  PRNG_RESET: {.  
3445c 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67 52      sqlite3PrngR
3445d 65 73 65 74 53 74 61 74 65 28 29 3b 0a 20 20 20  esetState();.   
3445e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
3445f 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20  .    /*.    **  
34460 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
34461 74 72 6f 6c 28 42 49 54 56 45 43 5f 54 45 53 54  trol(BITVEC_TEST
34462 2c 20 73 69 7a 65 2c 20 70 72 6f 67 72 61 6d 29  , size, program)
34463 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 52  .    **.    ** R
34464 75 6e 20 61 20 74 65 73 74 20 61 67 61 69 6e 73  un a test agains
34465 74 20 61 20 42 69 74 76 65 63 20 6f 62 6a 65 63  t a Bitvec objec
34466 74 20 6f 66 20 73 69 7a 65 2e 20 20 54 68 65 20  t of size.  The 
34467 70 72 6f 67 72 61 6d 20 61 72 67 75 6d 65 6e 74  program argument
34468 0a 20 20 20 20 2a 2a 20 69 73 20 61 6e 20 61 72  .    ** is an ar
34469 72 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20  ray of integers 
3446a 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65  that defines the
3446b 20 74 65 73 74 2e 20 20 52 65 74 75 72 6e 20 2d   test.  Return -
3446c 31 20 6f 6e 20 61 0a 20 20 20 20 2a 2a 20 6d 65  1 on a.    ** me
3446d 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
3446e 65 72 72 6f 72 2c 20 30 20 6f 6e 20 73 75 63 63  error, 0 on succ
3446f 65 73 73 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f  ess, or non-zero
34470 20 66 6f 72 20 61 6e 20 65 72 72 6f 72 2e 0a 20   for an error.. 
34471 20 20 20 2a 2a 20 53 65 65 20 74 68 65 20 73 71     ** See the sq
34472 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74  lite3BitvecBuilt
34473 69 6e 54 65 73 74 28 29 20 66 6f 72 20 61 64 64  inTest() for add
34474 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
34475 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
34476 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
34477 43 54 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54  CTRL_BITVEC_TEST
34478 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a  : {.      int sz
34479 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
3447a 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 2a 61  t);.      int *a
3447b 50 72 6f 67 20 3d 20 76 61 5f 61 72 67 28 61 70  Prog = va_arg(ap
3447c 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 20 20 72  , int*);.      r
3447d 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  c = sqlite3Bitve
3447e 63 42 75 69 6c 74 69 6e 54 65 73 74 28 73 7a 2c  cBuiltinTest(sz,
3447f 20 61 50 72 6f 67 29 3b 0a 20 20 20 20 20 20 62   aProg);.      b
34480 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
34481 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69   /*.    **  sqli
34482 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
34483 28 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48  (BENIGN_MALLOC_H
34484 4f 4f 4b 53 2c 20 78 42 65 67 69 6e 2c 20 78 45  OOKS, xBegin, xE
34485 6e 64 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  nd).    **.    *
34486 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6f 6b 73  * Register hooks
34487 20 74 6f 20 63 61 6c 6c 20 74 6f 20 69 6e 64 69   to call to indi
34488 63 61 74 65 20 77 68 69 63 68 20 6d 61 6c 6c 6f  cate which mallo
34489 63 28 29 20 66 61 69 6c 75 72 65 73 20 0a 20 20  c() failures .  
3448a 20 20 2a 2a 20 61 72 65 20 62 65 6e 69 67 6e 2e    ** are benign.
3448b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
3448c 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
3448d 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48  _BENIGN_MALLOC_H
3448e 4f 4f 4b 53 3a 20 7b 0a 20 20 20 20 20 20 74 79  OOKS: {.      ty
3448f 70 65 64 65 66 20 76 6f 69 64 20 28 2a 76 6f 69  pedef void (*voi
34490 64 5f 66 75 6e 63 74 69 6f 6e 29 28 76 6f 69 64  d_function)(void
34491 29 3b 0a 20 20 20 20 20 20 76 6f 69 64 5f 66 75  );.      void_fu
34492 6e 63 74 69 6f 6e 20 78 42 65 6e 69 67 6e 42 65  nction xBenignBe
34493 67 69 6e 3b 0a 20 20 20 20 20 20 76 6f 69 64 5f  gin;.      void_
34494 66 75 6e 63 74 69 6f 6e 20 78 42 65 6e 69 67 6e  function xBenign
34495 45 6e 64 3b 0a 20 20 20 20 20 20 78 42 65 6e 69  End;.      xBeni
34496 67 6e 42 65 67 69 6e 20 3d 20 76 61 5f 61 72 67  gnBegin = va_arg
34497 28 61 70 2c 20 76 6f 69 64 5f 66 75 6e 63 74 69  (ap, void_functi
34498 6f 6e 29 3b 0a 20 20 20 20 20 20 78 42 65 6e 69  on);.      xBeni
34499 67 6e 45 6e 64 20 3d 20 76 61 5f 61 72 67 28 61  gnEnd = va_arg(a
3449a 70 2c 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e  p, void_function
3449b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3449c 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b  BenignMallocHook
3449d 73 28 78 42 65 6e 69 67 6e 42 65 67 69 6e 2c 20  s(xBenignBegin, 
3449e 78 42 65 6e 69 67 6e 45 6e 64 29 3b 0a 20 20 20  xBenignEnd);.   
3449f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
344a0 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20  .    /*.    **  
344a1 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
344a2 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54  trol(SQLITE_TEST
344a3 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 54  CTRL_PENDING_BYT
344a4 45 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  E, unsigned int 
344a5 58 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  X).    **.    **
344a6 20 53 65 74 20 74 68 65 20 50 45 4e 44 49 4e 47   Set the PENDING
344a7 20 62 79 74 65 20 74 6f 20 74 68 65 20 76 61 6c   byte to the val
344a8 75 65 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65  ue in the argume
344a9 6e 74 2c 20 69 66 20 58 3e 30 2e 0a 20 20 20 20  nt, if X>0..    
344aa 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67  ** Make no chang
344ab 65 73 20 69 66 20 58 3d 3d 30 2e 20 20 52 65 74  es if X==0.  Ret
344ac 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66  urn the value of
344ad 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74   the pending byt
344ae 65 0a 20 20 20 20 2a 2a 20 61 73 20 69 74 20 65  e.    ** as it e
344af 78 69 73 74 69 6e 67 20 62 65 66 6f 72 65 20 74  xisting before t
344b0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20  his routine was 
344b1 63 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2a 0a 20  called..    **. 
344b2 20 20 20 2a 2a 20 49 4d 50 4f 52 54 41 4e 54 3a     ** IMPORTANT:
344b3 20 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 50    Changing the P
344b4 45 4e 44 49 4e 47 20 62 79 74 65 20 66 72 6f 6d  ENDING byte from
344b5 20 30 78 34 30 30 30 30 30 30 30 20 72 65 73 75   0x40000000 resu
344b6 6c 74 73 20 69 6e 0a 20 20 20 20 2a 2a 20 61 6e  lts in.    ** an
344b7 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 64 61   incompatible da
344b8 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 6d  tabase file form
344b9 61 74 2e 20 20 43 68 61 6e 67 69 6e 67 20 74 68  at.  Changing th
344ba 65 20 50 45 4e 44 49 4e 47 20 62 79 74 65 0a 20  e PENDING byte. 
344bb 20 20 20 2a 2a 20 77 68 69 6c 65 20 61 6e 79 20     ** while any 
344bc 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
344bd 69 6f 6e 20 69 73 20 6f 70 65 6e 20 72 65 73 75  ion is open resu
344be 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64  lts in undefined
344bf 20 61 6e 64 0a 20 20 20 20 2a 2a 20 64 69 6c 65   and.    ** dile
344c0 74 65 72 69 6f 75 73 20 62 65 68 61 76 69 6f 72  terious behavior
344c1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
344c2 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
344c3 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 54 45 3a 20  L_PENDING_BYTE: 
344c4 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
344c5 20 69 6e 74 20 6e 65 77 56 61 6c 20 3d 20 76 61   int newVal = va
344c6 5f 61 72 67 28 61 70 2c 20 75 6e 73 69 67 6e 65  _arg(ap, unsigne
344c7 64 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 72 63  d int);.      rc
344c8 20 3d 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e   = sqlite3Pendin
344c9 67 42 79 74 65 3b 0a 20 20 20 20 20 20 69 66 28  gByte;.      if(
344ca 20 6e 65 77 56 61 6c 20 29 20 73 71 6c 69 74 65   newVal ) sqlite
344cb 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d 20 6e  3PendingByte = n
344cc 65 77 56 61 6c 3b 0a 20 20 20 20 20 20 62 72 65  ewVal;.      bre
344cd 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ak;.    }..    /
344ce 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65  *.    **  sqlite
344cf 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53  3_test_control(S
344d0 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41  QLITE_TESTCTRL_A
344d1 53 53 45 52 54 2c 20 69 6e 74 20 58 29 0a 20 20  SSERT, int X).  
344d2 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73    **.    ** This
344d3 20 61 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 73   action provides
344d4 20 61 20 72 75 6e 2d 74 69 6d 65 20 74 65 73 74   a run-time test
344d5 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72 20   to see whether 
344d6 6f 72 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 61 73  or not.    ** as
344d7 73 65 72 74 28 29 20 77 61 73 20 65 6e 61 62 6c  sert() was enabl
344d8 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  ed at compile-ti
344d9 6d 65 2e 20 20 49 66 20 58 20 69 73 20 74 72 75  me.  If X is tru
344da 65 20 61 6e 64 20 61 73 73 65 72 74 28 29 0a 20  e and assert(). 
344db 20 20 20 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64     ** is enabled
344dc 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72  , then the retur
344dd 6e 20 76 61 6c 75 65 20 69 73 20 74 72 75 65 2e  n value is true.
344de 20 20 49 66 20 58 20 69 73 20 74 72 75 65 20 61    If X is true a
344df 6e 64 0a 20 20 20 20 2a 2a 20 61 73 73 65 72 74  nd.    ** assert
344e0 28 29 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20  () is disabled, 
344e1 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20  then the return 
344e2 76 61 6c 75 65 20 69 73 20 7a 65 72 6f 2e 20 20  value is zero.  
344e3 49 66 20 58 20 69 73 0a 20 20 20 20 2a 2a 20 66  If X is.    ** f
344e4 61 6c 73 65 20 61 6e 64 20 61 73 73 65 72 74 28  alse and assert(
344e5 29 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68  ) is enabled, th
344e6 65 6e 20 74 68 65 20 61 73 73 65 72 74 69 6f 6e  en the assertion
344e7 20 66 69 72 65 73 20 61 6e 64 20 74 68 65 0a 20   fires and the. 
344e8 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 62     ** process ab
344e9 6f 72 74 73 2e 20 20 49 66 20 58 20 69 73 20 66  orts.  If X is f
344ea 61 6c 73 65 20 61 6e 64 20 61 73 73 65 72 74 28  alse and assert(
344eb 29 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74  ) is disabled, t
344ec 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 72  hen the.    ** r
344ed 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 7a  eturn value is z
344ee 65 72 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ero..    */.    
344ef 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
344f0 43 54 52 4c 5f 41 53 53 45 52 54 3a 20 7b 0a 20  CTRL_ASSERT: {. 
344f1 20 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e       volatile in
344f2 74 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 61  t x = 0;.      a
344f3 73 73 65 72 74 28 20 28 78 20 3d 20 76 61 5f 61  ssert( (x = va_a
344f4 72 67 28 61 70 2c 69 6e 74 29 29 21 3d 30 20 29  rg(ap,int))!=0 )
344f5 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 78 3b 0a  ;.      rc = x;.
344f6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
344f7 20 7d 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20   }...    /*.    
344f8 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74  **  sqlite3_test
344f9 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f  _control(SQLITE_
344fa 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59 53 2c  TESTCTRL_ALWAYS,
344fb 20 69 6e 74 20 58 29 0a 20 20 20 20 2a 2a 0a 20   int X).    **. 
344fc 20 20 20 2a 2a 20 54 68 69 73 20 61 63 74 69 6f     ** This actio
344fd 6e 20 70 72 6f 76 69 64 65 73 20 61 20 72 75 6e  n provides a run
344fe 2d 74 69 6d 65 20 74 65 73 74 20 74 6f 20 73 65  -time test to se
344ff 65 20 68 6f 77 20 74 68 65 20 41 4c 57 41 59 53  e how the ALWAYS
34500 20 61 6e 64 0a 20 20 20 20 2a 2a 20 4e 45 56 45   and.    ** NEVE
34501 52 20 6d 61 63 72 6f 73 20 77 65 72 65 20 64 65  R macros were de
34502 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  fined at compile
34503 2d 74 69 6d 65 2e 0a 20 20 20 20 2a 2a 0a 20 20  -time..    **.  
34504 20 20 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20    ** The return 
34505 76 61 6c 75 65 20 69 73 20 41 4c 57 41 59 53 28  value is ALWAYS(
34506 58 29 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20  X).  .    **.   
34507 20 2a 2a 20 54 68 65 20 72 65 63 6f 6d 6d 65 6e   ** The recommen
34508 64 65 64 20 74 65 73 74 20 69 73 20 58 3d 3d 32  ded test is X==2
34509 2e 20 20 49 66 20 74 68 65 20 72 65 74 75 72 6e  .  If the return
3450a 20 76 61 6c 75 65 20 69 73 20 32 2c 20 74 68 61   value is 2, tha
3450b 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 41  t means.    ** A
3450c 4c 57 41 59 53 28 29 20 61 6e 64 20 4e 45 56 45  LWAYS() and NEVE
3450d 52 28 29 20 61 72 65 20 62 6f 74 68 20 6e 6f 2d  R() are both no-
3450e 6f 70 20 70 61 73 73 2d 74 68 72 6f 75 67 68 20  op pass-through 
3450f 6d 61 63 72 6f 73 2c 20 77 68 69 63 68 20 69 73  macros, which is
34510 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 65 66 61   the.    ** defa
34511 75 6c 74 20 73 65 74 74 69 6e 67 2e 20 20 49 66  ult setting.  If
34512 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
34513 65 20 69 73 20 31 2c 20 74 68 65 6e 20 41 4c 57  e is 1, then ALW
34514 41 59 53 28 29 20 69 73 20 65 69 74 68 65 72 0a  AYS() is either.
34515 20 20 20 20 2a 2a 20 68 61 72 64 2d 63 6f 64 65      ** hard-code
34516 64 20 74 6f 20 74 72 75 65 20 6f 72 20 65 6c 73  d to true or els
34517 65 20 69 74 20 61 73 73 65 72 74 73 20 69 66 20  e it asserts if 
34518 69 74 73 20 61 72 67 75 6d 65 6e 74 20 69 73 20  its argument is 
34519 66 61 6c 73 65 2e 0a 20 20 20 20 2a 2a 20 54 68  false..    ** Th
3451a 65 20 66 69 72 73 74 20 62 65 68 61 76 69 6f 72  e first behavior
3451b 20 28 68 61 72 64 2d 63 6f 64 65 64 20 74 6f 20   (hard-coded to 
3451c 74 72 75 65 29 20 69 73 20 74 68 65 20 63 61 73  true) is the cas
3451d 65 20 69 66 0a 20 20 20 20 2a 2a 20 53 51 4c 49  e if.    ** SQLI
3451e 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45  TE_TESTCTRL_ASSE
3451f 52 54 20 73 68 6f 77 73 20 74 68 61 74 20 61 73  RT shows that as
34520 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c  sert() is disabl
34521 65 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  ed and the secon
34522 64 0a 20 20 20 20 2a 2a 20 62 65 68 61 76 69 6f  d.    ** behavio
34523 72 20 28 61 73 73 65 72 74 20 69 66 20 74 68 65  r (assert if the
34524 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c 57   argument to ALW
34525 41 59 53 28 29 20 69 73 20 66 61 6c 73 65 29 20  AYS() is false) 
34526 69 73 20 74 68 65 20 63 61 73 65 20 69 66 0a 20  is the case if. 
34527 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53     ** SQLITE_TES
34528 54 43 54 52 4c 5f 41 53 53 45 52 54 20 73 68 6f  TCTRL_ASSERT sho
34529 77 73 20 74 68 61 74 20 61 73 73 65 72 74 28 29  ws that assert()
3452a 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 20 20 20   is enabled..   
3452b 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72   **.    ** The r
3452c 75 6e 2d 74 69 6d 65 20 74 65 73 74 20 70 72 6f  un-time test pro
3452d 63 65 64 75 72 65 20 6d 69 67 68 74 20 6c 6f 6f  cedure might loo
3452e 6b 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65  k something like
3452f 20 74 68 69 73 3a 0a 20 20 20 20 2a 2a 0a 20 20   this:.    **.  
34530 20 20 2a 2a 20 20 20 20 69 66 28 20 73 71 6c 69    **    if( sqli
34531 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
34532 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c  (SQLITE_TESTCTRL
34533 5f 41 4c 57 41 59 53 2c 20 32 29 3d 3d 32 20 29  _ALWAYS, 2)==2 )
34534 7b 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2f 2f  {.    **      //
34535 20 41 4c 57 41 59 53 28 29 20 61 6e 64 20 4e 45   ALWAYS() and NE
34536 56 45 52 28 29 20 61 72 65 20 6e 6f 2d 6f 70 20  VER() are no-op 
34537 70 61 73 73 2d 74 68 72 6f 75 67 68 20 6d 61 63  pass-through mac
34538 72 6f 73 0a 20 20 20 20 2a 2a 20 20 20 20 7d 65  ros.    **    }e
34539 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f  lse if( sqlite3_
3453a 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c  test_control(SQL
3453b 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53  ITE_TESTCTRL_ASS
3453c 45 52 54 2c 20 31 29 20 29 7b 0a 20 20 20 20 2a  ERT, 1) ){.    *
3453d 2a 20 20 20 20 20 20 2f 2f 20 41 4c 57 41 59 53  *      // ALWAYS
3453e 28 78 29 20 61 73 73 65 72 74 73 20 74 68 61 74  (x) asserts that
3453f 20 78 20 69 73 20 74 72 75 65 2e 20 4e 45 56 45   x is true. NEVE
34540 52 28 78 29 20 61 73 73 65 72 74 73 20 78 20 69  R(x) asserts x i
34541 73 20 66 61 6c 73 65 2e 0a 20 20 20 20 2a 2a 20  s false..    ** 
34542 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 2a     }else{.    **
34543 20 20 20 20 20 20 2f 2f 20 41 4c 57 41 59 53 28        // ALWAYS(
34544 78 29 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74  x) is a constant
34545 20 31 2e 20 20 4e 45 56 45 52 28 78 29 20 69 73   1.  NEVER(x) is
34546 20 61 20 63 6f 6e 73 74 61 6e 74 20 30 2e 0a 20   a constant 0.. 
34547 20 20 20 2a 2a 20 20 20 20 7d 0a 20 20 20 20 2a     **    }.    *
34548 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
34549 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59  E_TESTCTRL_ALWAY
3454a 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 78  S: {.      int x
3454b 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   = va_arg(ap,int
3454c 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 41 4c  );.      rc = AL
3454d 57 41 59 53 28 78 29 3b 0a 20 20 20 20 20 20 62  WAYS(x);.      b
3454e 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
3454f 20 2f 2a 20 20 20 73 71 6c 69 74 65 33 5f 74 65   /*   sqlite3_te
34550 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54  st_control(SQLIT
34551 45 5f 54 45 53 54 43 54 52 4c 5f 52 45 53 45 52  E_TESTCTRL_RESER
34552 56 45 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  VE, sqlite3 *db,
34553 20 69 6e 74 20 4e 29 0a 20 20 20 20 2a 2a 0a 20   int N).    **. 
34554 20 20 20 2a 2a 20 53 65 74 20 74 68 65 20 6e 52     ** Set the nR
34555 65 73 65 72 76 65 20 73 69 7a 65 20 74 6f 20 4e  eserve size to N
34556 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   for the main da
34557 74 61 62 61 73 65 20 6f 6e 20 74 68 65 20 64 61  tabase on the da
34558 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 63 6f  tabase.    ** co
34559 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 0a 20 20 20  nnection db..   
3455a 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c   */.    case SQL
3455b 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 52 45 53  ITE_TESTCTRL_RES
3455c 45 52 56 45 3a 20 7b 0a 20 20 20 20 20 20 73 71  ERVE: {.      sq
3455d 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 61 5f 61  lite3 *db = va_a
3455e 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 2a 29  rg(ap, sqlite3*)
3455f 3b 0a 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20  ;.      int x = 
34560 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a  va_arg(ap,int);.
34561 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
34562 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
34563 74 65 78 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  tex);.      sqli
34564 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53  te3BtreeSetPageS
34565 69 7a 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  ize(db->aDb[0].p
34566 42 74 2c 20 30 2c 20 78 2c 20 30 29 3b 0a 20 20  Bt, 0, x, 0);.  
34567 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
34568 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
34569 78 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  x);.      break;
3456a 0a 20 20 20 20 7d 0a 0a 20 20 7d 0a 20 20 76 61  .    }..  }.  va
3456b 5f 65 6e 64 28 61 70 29 3b 0a 23 65 6e 64 69 66  _end(ap);.#endif
3456c 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
3456d 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a  BUILTIN_TEST */.
3456e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3456f 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34570 45 6e 64 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a  End of main.c **
34571 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34572 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34573 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34574 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34575 42 65 67 69 6e 20 66 69 6c 65 20 6e 6f 74 69 66  Begin file notif
34576 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  y.c ************
34577 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34578 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34579 2f 2a 0a 2a 2a 20 32 30 30 39 20 4d 61 72 63 68  /*.** 2009 March
3457a 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   3.**.** The aut
3457b 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
3457c 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
3457d 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
3457e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
3457f 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
34580 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
34581 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
34582 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
34583 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
34584 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
34585 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
34586 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
34587 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
34588 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
34589 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
3458a 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
3458b 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
3458c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3458d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3458e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3458f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34590 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
34591 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69  e contains the i
34592 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
34593 20 74 68 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c   the sqlite3_unl
34594 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 0a 2a 2a 20  ock_notify().** 
34595 41 50 49 20 6d 65 74 68 6f 64 20 61 6e 64 20 69  API method and i
34596 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 66 75  ts associated fu
34597 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a  nctionality..**.
34598 2a 2a 20 24 49 64 3a 20 6e 6f 74 69 66 79 2e 63  ** $Id: notify.c
34599 2c 76 20 31 2e 34 20 32 30 30 39 2f 30 34 2f 30  ,v 1.4 2009/04/0
3459a 37 20 32 32 3a 30 36 3a 35 37 20 64 72 68 20 45  7 22:06:57 drh E
3459b 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 4f 6d 69 74  xp $.*/../* Omit
3459c 20 74 68 69 73 20 65 6e 74 69 72 65 20 66 69 6c   this entire fil
3459d 65 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  e if SQLITE_ENAB
3459e 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59  LE_UNLOCK_NOTIFY
3459f 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e   is not defined.
345a0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
345a1 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f  E_ENABLE_UNLOCK_
345a2 4e 4f 54 49 46 59 0a 0a 2f 2a 0a 2a 2a 20 50 75  NOTIFY../*.** Pu
345a3 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 73 3a  blic interfaces:
345a4 0a 2a 2a 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33  .**.**   sqlite3
345a5 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65  ConnectionBlocke
345a6 64 28 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33  d().**   sqlite3
345a7 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b  ConnectionUnlock
345a8 65 64 28 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65  ed().**   sqlite
345a9 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65  3ConnectionClose
345aa 64 28 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33  d().**   sqlite3
345ab 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
345ac 0a 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 61 73 73  .*/..#define ass
345ad 65 72 74 4d 75 74 65 78 48 65 6c 64 28 29 20 5c  ertMutexHeld() \
345ae 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
345af 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71  e3_mutex_held(sq
345b0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
345b1 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
345b2 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29 0a 0a  TIC_MASTER)) )..
345b3 2f 2a 0a 2a 2a 20 48 65 61 64 20 6f 66 20 61 20  /*.** Head of a 
345b4 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61  linked list of a
345b5 6c 6c 20 73 71 6c 69 74 65 33 20 6f 62 6a 65 63  ll sqlite3 objec
345b6 74 73 20 63 72 65 61 74 65 64 20 62 79 20 74 68  ts created by th
345b7 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 66 6f  is process.** fo
345b8 72 20 77 68 69 63 68 20 65 69 74 68 65 72 20 73  r which either s
345b9 71 6c 69 74 65 33 2e 70 42 6c 6f 63 6b 69 6e 67  qlite3.pBlocking
345ba 43 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 73 71  Connection or sq
345bb 6c 69 74 65 33 2e 70 55 6e 6c 6f 63 6b 43 6f 6e  lite3.pUnlockCon
345bc 6e 65 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f  nection.** is no
345bd 74 20 4e 55 4c 4c 2e 20 54 68 69 73 20 76 61 72  t NULL. This var
345be 69 61 62 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 61  iable may only a
345bf 63 63 65 73 73 65 64 20 77 68 69 6c 65 20 74 68  ccessed while th
345c0 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a  e STATIC_MASTER.
345c1 2a 2a 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64  ** mutex is held
345c2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69  ..*/.static sqli
345c3 74 65 33 20 2a 53 51 4c 49 54 45 5f 57 53 44 20  te3 *SQLITE_WSD 
345c4 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69  sqlite3BlockedLi
345c5 73 74 20 3d 20 30 3b 0a 0a 23 69 66 6e 64 65 66  st = 0;..#ifndef
345c6 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68   NDEBUG./*.** Th
345c7 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
345c8 20 63 6f 6d 70 6c 65 78 20 61 73 73 65 72 74 28   complex assert(
345c9 29 20 74 68 61 74 20 76 65 72 69 66 69 65 73 20  ) that verifies 
345ca 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a  the following .*
345cb 2a 20 70 72 6f 70 65 72 74 69 65 73 20 6f 66 20  * properties of 
345cc 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  the blocked conn
345cd 65 63 74 69 6f 6e 73 20 6c 69 73 74 3a 0a 2a 2a  ections list:.**
345ce 0a 2a 2a 20 20 20 31 29 20 45 61 63 68 20 65 6e  .**   1) Each en
345cf 74 72 79 20 69 6e 20 74 68 65 20 6c 69 73 74 20  try in the list 
345d0 68 61 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 76  has a non-NULL v
345d1 61 6c 75 65 20 66 6f 72 20 65 69 74 68 65 72 20  alue for either 
345d2 0a 2a 2a 20 20 20 20 20 20 70 55 6e 6c 6f 63 6b  .**      pUnlock
345d3 43 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 42  Connection or pB
345d4 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
345d5 6e 2c 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 0a 2a  n, or both..**.*
345d6 2a 20 20 20 32 29 20 41 6c 6c 20 65 6e 74 72 69  *   2) All entri
345d7 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 74  es in the list t
345d8 68 61 74 20 73 68 61 72 65 20 61 20 63 6f 6d 6d  hat share a comm
345d9 6f 6e 20 76 61 6c 75 65 20 66 6f 72 20 0a 2a 2a  on value for .**
345da 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74        xUnlockNot
345db 69 66 79 20 61 72 65 20 67 72 6f 75 70 65 64 20  ify are grouped 
345dc 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20  together..**.** 
345dd 20 20 33 29 20 49 66 20 74 68 65 20 61 72 67 75    3) If the argu
345de 6d 65 6e 74 20 64 62 20 69 73 20 6e 6f 74 20 4e  ment db is not N
345df 55 4c 4c 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f  ULL, then none o
345e0 66 20 74 68 65 20 65 6e 74 72 69 65 73 20 69 6e  f the entries in
345e1 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 62 6c 6f   the.**      blo
345e2 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
345e3 20 6c 69 73 74 20 68 61 76 65 20 70 55 6e 6c 6f   list have pUnlo
345e4 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20  ckConnection or 
345e5 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
345e6 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 73 65 74 20  ion.**      set 
345e7 74 6f 20 64 62 2e 20 54 68 69 73 20 69 73 20 75  to db. This is u
345e8 73 65 64 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67  sed when closing
345e9 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 0a   connection db..
345ea 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
345eb 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69  heckListProperti
345ec 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  es(sqlite3 *db){
345ed 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 3b 0a 20  .  sqlite3 *p;. 
345ee 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 42 6c   for(p=sqlite3Bl
345ef 6f 63 6b 65 64 4c 69 73 74 3b 20 70 3b 20 70 3d  ockedList; p; p=
345f0 70 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 29  p->pNextBlocked)
345f1 7b 0a 20 20 20 20 69 6e 74 20 73 65 65 6e 20 3d  {.    int seen =
345f2 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20   0;.    sqlite3 
345f3 2a 70 32 3b 0a 0a 20 20 20 20 2f 2a 20 56 65 72  *p2;..    /* Ver
345f4 69 66 79 20 70 72 6f 70 65 72 74 79 20 28 31 29  ify property (1)
345f5 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
345f6 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  p->pUnlockConnec
345f7 74 69 6f 6e 20 7c 7c 20 70 2d 3e 70 42 6c 6f 63  tion || p->pBloc
345f8 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 29  kingConnection )
345f9 3b 0a 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79  ;..    /* Verify
345fa 20 70 72 6f 70 65 72 74 79 20 28 32 29 20 2a 2f   property (2) */
345fb 0a 20 20 20 20 66 6f 72 28 70 32 3d 73 71 6c 69  .    for(p2=sqli
345fc 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20  te3BlockedList; 
345fd 70 32 21 3d 70 3b 20 70 32 3d 70 32 2d 3e 70 4e  p2!=p; p2=p2->pN
345fe 65 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20  extBlocked){.   
345ff 20 20 20 69 66 28 20 70 32 2d 3e 78 55 6e 6c 6f     if( p2->xUnlo
34600 63 6b 4e 6f 74 69 66 79 3d 3d 70 2d 3e 78 55 6e  ckNotify==p->xUn
34601 6c 6f 63 6b 4e 6f 74 69 66 79 20 29 20 73 65 65  lockNotify ) see
34602 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 61 73 73  n = 1;.      ass
34603 65 72 74 28 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b  ert( p2->xUnlock
34604 4e 6f 74 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f  Notify==p->xUnlo
34605 63 6b 4e 6f 74 69 66 79 20 7c 7c 20 21 73 65 65  ckNotify || !see
34606 6e 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  n );.      asser
34607 74 28 20 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70  t( db==0 || p->p
34608 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
34609 21 3d 64 62 20 29 3b 0a 20 20 20 20 20 20 61 73  !=db );.      as
3460a 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 70  sert( db==0 || p
3460b 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  ->pBlockingConne
3460c 63 74 69 6f 6e 21 3d 64 62 20 29 3b 0a 20 20 20  ction!=db );.   
3460d 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23   }.  }.}.#else.#
3460e 20 64 65 66 69 6e 65 20 63 68 65 63 6b 4c 69 73   define checkLis
3460f 74 50 72 6f 70 65 72 74 69 65 73 28 78 29 0a 23  tProperties(x).#
34610 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d  endif../*.** Rem
34611 6f 76 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64  ove connection d
34612 62 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b  b from the block
34613 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c  ed connections l
34614 69 73 74 2e 20 49 66 20 63 6f 6e 6e 65 63 74 69  ist. If connecti
34615 6f 6e 0a 2a 2a 20 64 62 20 69 73 20 6e 6f 74 20  on.** db is not 
34616 63 75 72 72 65 6e 74 6c 79 20 61 20 70 61 72 74  currently a part
34617 20 6f 66 20 74 68 65 20 6c 69 73 74 2c 20 74 68   of the list, th
34618 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
34619 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
3461a 63 20 76 6f 69 64 20 72 65 6d 6f 76 65 46 72 6f  c void removeFro
3461b 6d 42 6c 6f 63 6b 65 64 4c 69 73 74 28 73 71 6c  mBlockedList(sql
3461c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c  ite3 *db){.  sql
3461d 69 74 65 33 20 2a 2a 70 70 3b 0a 20 20 61 73 73  ite3 **pp;.  ass
3461e 65 72 74 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a  ertMutexHeld();.
3461f 20 20 66 6f 72 28 70 70 3d 26 73 71 6c 69 74 65    for(pp=&sqlite
34620 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 2a 70  3BlockedList; *p
34621 70 3b 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e  p; pp = &(*pp)->
34622 70 4e 65 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20  pNextBlocked){. 
34623 20 20 20 69 66 28 20 2a 70 70 3d 3d 64 62 20 29     if( *pp==db )
34624 7b 0a 20 20 20 20 20 20 2a 70 70 20 3d 20 28 2a  {.      *pp = (*
34625 70 70 29 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65  pp)->pNextBlocke
34626 64 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  d;.      break;.
34627 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
34628 2a 2a 20 41 64 64 20 63 6f 6e 6e 65 63 74 69 6f  ** Add connectio
34629 6e 20 64 62 20 74 6f 20 74 68 65 20 62 6c 6f 63  n db to the bloc
3462a 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ked connections 
3462b 6c 69 73 74 2e 20 49 74 20 69 73 20 61 73 73 75  list. It is assu
3462c 6d 65 64 0a 2a 2a 20 74 68 61 74 20 69 74 20 69  med.** that it i
3462d 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20  s not already a 
3462e 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73 74  part of the list
3462f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
34630 20 61 64 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73   addToBlockedLis
34631 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  t(sqlite3 *db){.
34632 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 3b 0a    sqlite3 **pp;.
34633 20 20 61 73 73 65 72 74 4d 75 74 65 78 48 65 6c    assertMutexHel
34634 64 28 29 3b 0a 20 20 66 6f 72 28 0a 20 20 20 20  d();.  for(.    
34635 70 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f 63 6b  pp=&sqlite3Block
34636 65 64 4c 69 73 74 3b 20 0a 20 20 20 20 2a 70 70  edList; .    *pp
34637 20 26 26 20 28 2a 70 70 29 2d 3e 78 55 6e 6c 6f   && (*pp)->xUnlo
34638 63 6b 4e 6f 74 69 66 79 21 3d 64 62 2d 3e 78 55  ckNotify!=db->xU
34639 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3b 20 0a 20 20  nlockNotify; .  
3463a 20 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 4e 65    pp=&(*pp)->pNe
3463b 78 74 42 6c 6f 63 6b 65 64 0a 20 20 29 3b 0a 20  xtBlocked.  );. 
3463c 20 64 62 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65   db->pNextBlocke
3463d 64 20 3d 20 2a 70 70 3b 0a 20 20 2a 70 70 20 3d  d = *pp;.  *pp =
3463e 20 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62   db;.}../*.** Ob
3463f 74 61 69 6e 20 74 68 65 20 53 54 41 54 49 43 5f  tain the STATIC_
34640 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 0a 2a 2f  MASTER mutex..*/
34641 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 6e 74  .static void ent
34642 65 72 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20  erMutex(void){. 
34643 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
34644 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65  nter(sqlite3Mute
34645 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
34646 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
34647 52 29 29 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74  R));.  checkList
34648 50 72 6f 70 65 72 74 69 65 73 28 30 29 3b 0a 7d  Properties(0);.}
34649 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
3464a 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45  the STATIC_MASTE
3464b 52 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74  R mutex..*/.stat
3464c 69 63 20 76 6f 69 64 20 6c 65 61 76 65 4d 75 74  ic void leaveMut
3464d 65 78 28 76 6f 69 64 29 7b 0a 20 20 61 73 73 65  ex(void){.  asse
3464e 72 74 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a 20  rtMutexHeld();. 
3464f 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72   checkListProper
34650 74 69 65 73 28 30 29 3b 0a 20 20 73 71 6c 69 74  ties(0);.  sqlit
34651 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
34652 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
34653 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
34654 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d  ATIC_MASTER));.}
34655 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
34656 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
34657 79 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a  y callback..**.*
34658 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64  * This is called
34659 20 61 66 74 65 72 20 63 6f 6e 6e 65 63 74 69 6f   after connectio
3465a 6e 20 22 64 62 22 20 68 61 73 20 61 74 74 65 6d  n "db" has attem
3465b 70 74 65 64 20 73 6f 6d 65 20 6f 70 65 72 61 74  pted some operat
3465c 69 6f 6e 0a 2a 2a 20 62 75 74 20 68 61 73 20 72  ion.** but has r
3465d 65 63 65 69 76 65 64 20 61 6e 20 53 51 4c 49 54  eceived an SQLIT
3465e 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 20 62  E_LOCKED error b
3465f 65 63 61 75 73 65 20 61 6e 6f 74 68 65 72 20 63  ecause another c
34660 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 28 63 61  onnection.** (ca
34661 6c 6c 20 69 74 20 70 4f 74 68 65 72 29 20 69 6e  ll it pOther) in
34662 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73   the same proces
34663 73 20 77 61 73 20 62 75 73 79 20 75 73 69 6e 67  s was busy using
34664 20 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64   the same shared
34665 0a 2a 2a 20 63 61 63 68 65 2e 20 20 70 4f 74 68  .** cache.  pOth
34666 65 72 20 69 73 20 66 6f 75 6e 64 20 62 79 20 6c  er is found by l
34667 6f 6f 6b 69 6e 67 20 61 74 20 64 62 2d 3e 70 42  ooking at db->pB
34668 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
34669 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  n..**.** If ther
3466a 65 20 69 73 20 6e 6f 20 62 6c 6f 63 6b 69 6e 67  e is no blocking
3466b 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65   connection, the
3466c 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76   callback is inv
3466d 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  oked immediately
3466e 2c 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73  ,.** before this
3466f 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
34670 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4f 74 68 65  ..**.** If pOthe
34671 72 20 69 73 20 61 6c 72 65 61 64 79 20 62 6c 6f  r is already blo
34672 63 6b 65 64 20 6f 6e 20 64 62 2c 20 74 68 65 6e  cked on db, then
34673 20 72 65 70 6f 72 74 20 53 51 4c 49 54 45 5f 4c   report SQLITE_L
34674 4f 43 4b 45 44 2c 20 74 6f 20 69 6e 64 69 63 61  OCKED, to indica
34675 74 65 0a 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b  te.** a deadlock
34676 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
34677 65 2c 20 6d 61 6b 65 20 61 72 72 61 6e 67 65 6d  e, make arrangem
34678 65 6e 74 73 20 74 6f 20 69 6e 76 6f 6b 65 20 78  ents to invoke x
34679 4e 6f 74 69 66 79 20 77 68 65 6e 20 70 4f 74 68  Notify when pOth
3467a 65 72 20 64 72 6f 70 73 0a 2a 2a 20 69 74 73 20  er drops.** its 
3467b 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 45 61 63  locks..**.** Eac
3467c 68 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72  h call to this r
3467d 6f 75 74 69 6e 65 20 6f 76 65 72 72 69 64 65 73  outine overrides
3467e 20 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c 62   any prior callb
3467f 61 63 6b 73 20 72 65 67 69 73 74 65 72 65 64 0a  acks registered.
34680 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 22  ** on the same "
34681 64 62 22 2e 20 20 49 66 20 78 4e 6f 74 69 66 79  db".  If xNotify
34682 3d 3d 30 20 74 68 65 6e 20 61 6e 79 20 70 72 69  ==0 then any pri
34683 6f 72 20 63 61 6c 6c 62 61 63 6b 73 20 61 72 65  or callbacks are
34684 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20   immediately.** 
34685 63 61 6e 63 65 6c 6c 65 64 2e 0a 2a 2f 0a 53 51  cancelled..*/.SQ
34686 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
34687 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69  ite3_unlock_noti
34688 66 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  fy(.  sqlite3 *d
34689 62 2c 0a 20 20 76 6f 69 64 20 28 2a 78 4e 6f 74  b,.  void (*xNot
3468a 69 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e  ify)(void **, in
3468b 74 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  t),.  void *pArg
3468c 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
3468d 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c  QLITE_OK;..  sql
3468e 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
3468f 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 65  (db->mutex);.  e
34690 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20  nterMutex();..  
34691 69 66 28 20 78 4e 6f 74 69 66 79 3d 3d 30 20 29  if( xNotify==0 )
34692 7b 0a 20 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d  {.    removeFrom
34693 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b  BlockedList(db);
34694 0a 20 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b  .    db->pUnlock
34695 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a  Connection = 0;.
34696 20 20 20 20 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e      db->xUnlockN
34697 6f 74 69 66 79 20 3d 20 30 3b 0a 20 20 20 20 64  otify = 0;.    d
34698 62 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 20 3d 20  b->pUnlockArg = 
34699 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 30  0;.  }else if( 0
3469a 3d 3d 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43  ==db->pBlockingC
3469b 6f 6e 6e 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20  onnection ){.   
3469c 20 2f 2a 20 54 68 65 20 62 6c 6f 63 6b 69 6e 67   /* The blocking
3469d 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73   transaction has
3469e 20 62 65 65 6e 20 63 6f 6e 63 6c 75 64 65 64 2e   been concluded.
3469f 20 4f 72 20 74 68 65 72 65 20 6e 65 76 65 72 20   Or there never 
346a0 77 61 73 20 61 20 0a 20 20 20 20 2a 2a 20 62 6c  was a .    ** bl
346a1 6f 63 6b 69 6e 67 20 74 72 61 6e 73 61 63 74 69  ocking transacti
346a2 6f 6e 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61  on. In either ca
346a3 73 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 6e  se, invoke the n
346a4 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 0a 20  otify callback. 
346a5 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c     ** immediatel
346a6 79 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 78  y. .    */.    x
346a7 4e 6f 74 69 66 79 28 26 70 41 72 67 2c 20 31 29  Notify(&pArg, 1)
346a8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
346a9 71 6c 69 74 65 33 20 2a 70 3b 0a 0a 20 20 20 20  qlite3 *p;..    
346aa 66 6f 72 28 70 3d 64 62 2d 3e 70 42 6c 6f 63 6b  for(p=db->pBlock
346ab 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 70  ingConnection; p
346ac 20 26 26 20 70 21 3d 64 62 3b 20 70 3d 70 2d 3e   && p!=db; p=p->
346ad 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
346ae 6e 29 7b 7d 0a 20 20 20 20 69 66 28 20 70 20 29  n){}.    if( p )
346af 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
346b0 49 54 45 5f 4c 4f 43 4b 45 44 3b 20 20 20 20 20  ITE_LOCKED;     
346b1 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 61 64           /* Dead
346b2 6c 6f 63 6b 20 64 65 74 65 63 74 65 64 2e 20 2a  lock detected. *
346b3 2f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  /.    }else{.   
346b4 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f     db->pUnlockCo
346b5 6e 6e 65 63 74 69 6f 6e 20 3d 20 64 62 2d 3e 70  nnection = db->p
346b6 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
346b7 6f 6e 3b 0a 20 20 20 20 20 20 64 62 2d 3e 78 55  on;.      db->xU
346b8 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 78 4e  nlockNotify = xN
346b9 6f 74 69 66 79 3b 0a 20 20 20 20 20 20 64 62 2d  otify;.      db-
346ba 3e 70 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 70 41  >pUnlockArg = pA
346bb 72 67 3b 0a 20 20 20 20 20 20 72 65 6d 6f 76 65  rg;.      remove
346bc 46 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73 74 28  FromBlockedList(
346bd 64 62 29 3b 0a 20 20 20 20 20 20 61 64 64 54 6f  db);.      addTo
346be 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b  BlockedList(db);
346bf 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6c 65  .    }.  }..  le
346c0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 61 73  aveMutex();.  as
346c1 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
346c2 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 73 71 6c  cFailed );.  sql
346c3 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
346c4 2c 20 28 72 63 3f 22 64 61 74 61 62 61 73 65 20  , (rc?"database 
346c5 69 73 20 64 65 61 64 6c 6f 63 6b 65 64 22 3a 30  is deadlocked":0
346c6 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ));.  sqlite3_mu
346c7 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
346c8 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
346c9 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
346ca 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
346cb 6c 65 64 20 77 68 69 6c 65 20 73 74 65 70 70 69  led while steppi
346cc 6e 67 20 6f 72 20 70 72 65 70 61 72 69 6e 67 20  ng or preparing 
346cd 61 20 73 74 61 74 65 6d 65 6e 74 20 0a 2a 2a 20  a statement .** 
346ce 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
346cf 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 20 54  connection db. T
346d0 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c  he operation wil
346d1 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  l return SQLITE_
346d2 4c 4f 43 4b 45 44 0a 2a 2a 20 74 6f 20 74 68 65  LOCKED.** to the
346d3 20 75 73 65 72 20 62 65 63 61 75 73 65 20 69 74   user because it
346d4 20 72 65 71 75 69 72 65 73 20 61 20 6c 6f 63 6b   requires a lock
346d5 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62   that will not b
346d6 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 75  e available.** u
346d7 6e 74 69 6c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ntil connection 
346d8 70 42 6c 6f 63 6b 65 72 20 63 6f 6e 63 6c 75 64  pBlocker conclud
346d9 65 73 20 69 74 73 20 63 75 72 72 65 6e 74 20 74  es its current t
346da 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  ransaction..*/.S
346db 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
346dc 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63  id sqlite3Connec
346dd 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c 69  tionBlocked(sqli
346de 74 65 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33  te3 *db, sqlite3
346df 20 2a 70 42 6c 6f 63 6b 65 72 29 7b 0a 20 20 65   *pBlocker){.  e
346e0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69  nterMutex();.  i
346e1 66 28 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67  f( db->pBlocking
346e2 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 26 26  Connection==0 &&
346e3 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e   db->pUnlockConn
346e4 65 63 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20  ection==0 ){.   
346e5 20 61 64 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73   addToBlockedLis
346e6 74 28 64 62 29 3b 0a 20 20 7d 0a 20 20 64 62 2d  t(db);.  }.  db-
346e7 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  >pBlockingConnec
346e8 74 69 6f 6e 20 3d 20 70 42 6c 6f 63 6b 65 72 3b  tion = pBlocker;
346e9 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b  .  leaveMutex();
346ea 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
346eb 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
346ec 64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 74 72  d when.** the tr
346ed 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64  ansaction opened
346ee 20 62 79 20 64 61 74 61 62 61 73 65 20 64 62 20   by database db 
346ef 68 61 73 20 6a 75 73 74 20 66 69 6e 69 73 68 65  has just finishe
346f0 64 2e 20 4c 6f 63 6b 73 20 68 65 6c 64 20 0a 2a  d. Locks held .*
346f1 2a 20 62 79 20 64 61 74 61 62 61 73 65 20 63 6f  * by database co
346f2 6e 6e 65 63 74 69 6f 6e 20 64 62 20 68 61 76 65  nnection db have
346f3 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a   been released..
346f4 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
346f5 69 6f 6e 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67  ion loops throug
346f6 68 20 65 61 63 68 20 65 6e 74 72 79 20 69 6e 20  h each entry in 
346f7 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  the blocked conn
346f8 65 63 74 69 6f 6e 73 0a 2a 2a 20 6c 69 73 74 20  ections.** list 
346f9 61 6e 64 20 64 6f 65 73 20 74 68 65 20 66 6f 6c  and does the fol
346fa 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
346fb 31 29 20 49 66 20 74 68 65 20 73 71 6c 69 74 65  1) If the sqlite
346fc 33 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  3.pBlockingConne
346fd 63 74 69 6f 6e 20 6d 65 6d 62 65 72 20 6f 66 20  ction member of 
346fe 61 20 6c 69 73 74 20 65 6e 74 72 79 20 69 73 0a  a list entry is.
346ff 2a 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 64  **      set to d
34700 62 2c 20 74 68 65 6e 20 73 65 74 20 70 42 6c 6f  b, then set pBlo
34701 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d  ckingConnection=
34702 30 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66  0..**.**   2) If
34703 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 55 6e   the sqlite3.pUn
34704 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d  lockConnection m
34705 65 6d 62 65 72 20 6f 66 20 61 20 6c 69 73 74 20  ember of a list 
34706 65 6e 74 72 79 20 69 73 0a 2a 2a 20 20 20 20 20  entry is.**     
34707 20 73 65 74 20 74 6f 20 64 62 2c 20 74 68 65 6e   set to db, then
34708 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 6e 66   invoke the conf
34709 69 67 75 72 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f  igured unlock-no
3470a 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 61 6e  tify callback an
3470b 64 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 70 55  d.**      set pU
3470c 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d  nlockConnection=
3470d 30 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 49 66  0..**.**   3) If
3470e 20 74 68 65 20 74 77 6f 20 73 74 65 70 73 20 61   the two steps a
3470f 62 6f 76 65 20 6d 65 61 6e 20 74 68 61 74 20 70  bove mean that p
34710 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
34711 6f 6e 3d 3d 30 20 61 6e 64 0a 2a 2a 20 20 20 20  on==0 and.**    
34712 20 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74    pUnlockConnect
34713 69 6f 6e 3d 3d 30 2c 20 72 65 6d 6f 76 65 20 74  ion==0, remove t
34714 68 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  he entry from th
34715 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  e blocked connec
34716 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 6c 69  tions.**      li
34717 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  st..*/.SQLITE_PR
34718 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
34719 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f  e3ConnectionUnlo
3471a 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62  cked(sqlite3 *db
3471b 29 7b 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e 6c  ){.  void (*xUnl
3471c 6f 63 6b 4e 6f 74 69 66 79 29 28 76 6f 69 64 20  ockNotify)(void 
3471d 2a 2a 2c 20 69 6e 74 29 20 3d 20 30 3b 20 2f 2a  **, int) = 0; /*
3471e 20 55 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   Unlock-notify c
3471f 62 20 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20  b to invoke */. 
34720 20 69 6e 74 20 6e 41 72 67 20 3d 20 30 3b 20 20   int nArg = 0;  
34721 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34722 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
34723 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
34724 6e 20 61 41 72 67 5b 5d 20 2a 2f 0a 20 20 73 71  n aArg[] */.  sq
34725 6c 69 74 65 33 20 2a 2a 70 70 3b 20 20 20 20 20  lite3 **pp;     
34726 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34727 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
34728 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  or variable */. 
34729 20 76 6f 69 64 20 2a 2a 61 41 72 67 3b 20 20 20   void **aArg;   
3472a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
3472b 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
3472c 75 6e 6c 6f 63 6b 20 63 61 6c 6c 62 61 63 6b 20  unlock callback 
3472d 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 44 79 6e  */.  void **aDyn
3472e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
3472f 2f 2a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 61  /* Dynamically a
34730 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66  llocated space f
34731 6f 72 20 61 41 72 67 5b 5d 20 2a 2f 0a 20 20 76  or aArg[] */.  v
34732 6f 69 64 20 2a 61 53 74 61 74 69 63 5b 31 36 5d  oid *aStatic[16]
34733 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61  ;         /* Sta
34734 72 74 65 72 20 73 70 61 63 65 20 66 6f 72 20 61  rter space for a
34735 41 72 67 5b 5d 2e 20 20 4e 6f 20 6d 61 6c 6c 6f  Arg[].  No mallo
34736 63 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 0a 20  c required */.. 
34737 20 61 41 72 67 20 3d 20 61 53 74 61 74 69 63 3b   aArg = aStatic;
34738 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b  .  enterMutex();
34739 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 74 65           /* Ente
3473a 72 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20  r STATIC_MASTER 
3473b 6d 75 74 65 78 20 2a 2f 0a 0a 20 20 2f 2a 20 54  mutex */..  /* T
3473c 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e  his loop runs on
3473d 63 65 20 66 6f 72 20 65 61 63 68 20 65 6e 74 72  ce for each entr
3473e 79 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 65 64  y in the blocked
3473f 2d 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73  -connections lis
34740 74 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 70 3d 26  t. */.  for(pp=&
34741 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69  sqlite3BlockedLi
34742 73 74 3b 20 2a 70 70 3b 20 2f 2a 20 6e 6f 2d 6f  st; *pp; /* no-o
34743 70 20 2a 2f 20 29 7b 0a 20 20 20 20 73 71 6c 69  p */ ){.    sqli
34744 74 65 33 20 2a 70 20 3d 20 2a 70 70 3b 0a 0a 20  te3 *p = *pp;.. 
34745 20 20 20 2f 2a 20 53 74 65 70 20 31 2e 20 2a 2f     /* Step 1. */
34746 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 42 6c 6f  .    if( p->pBlo
34747 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d  ckingConnection=
34748 3d 64 62 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  =db ){.      p->
34749 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
3474a 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a  ion = 0;.    }..
3474b 20 20 20 20 2f 2a 20 53 74 65 70 20 32 2e 20 2a      /* Step 2. *
3474c 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 55 6e  /.    if( p->pUn
3474d 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  lockConnection==
3474e 64 62 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  db ){.      asse
3474f 72 74 28 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f  rt( p->xUnlockNo
34750 74 69 66 79 20 29 3b 0a 20 20 20 20 20 20 69 66  tify );.      if
34751 28 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69  ( p->xUnlockNoti
34752 66 79 21 3d 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  fy!=xUnlockNotif
34753 79 20 26 26 20 6e 41 72 67 21 3d 30 20 29 7b 0a  y && nArg!=0 ){.
34754 20 20 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e          xUnlockN
34755 6f 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67  otify(aArg, nArg
34756 29 3b 0a 20 20 20 20 20 20 20 20 6e 41 72 67 20  );.        nArg 
34757 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  = 0;.      }..  
34758 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
34759 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
3475a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61 41        assert( aA
3475b 72 67 3d 3d 61 44 79 6e 20 7c 7c 20 28 61 44 79  rg==aDyn || (aDy
3475c 6e 3d 3d 30 20 26 26 20 61 41 72 67 3d 3d 61 53  n==0 && aArg==aS
3475d 74 61 74 69 63 29 20 29 3b 0a 20 20 20 20 20 20  tatic) );.      
3475e 61 73 73 65 72 74 28 20 6e 41 72 67 3c 3d 28 69  assert( nArg<=(i
3475f 6e 74 29 41 72 72 61 79 53 69 7a 65 28 61 53 74  nt)ArraySize(aSt
34760 61 74 69 63 29 20 7c 7c 20 61 41 72 67 3d 3d 61  atic) || aArg==a
34761 44 79 6e 20 29 3b 0a 20 20 20 20 20 20 69 66 28  Dyn );.      if(
34762 20 28 21 61 44 79 6e 20 26 26 20 6e 41 72 67 3d   (!aDyn && nArg=
34763 3d 28 69 6e 74 29 41 72 72 61 79 53 69 7a 65 28  =(int)ArraySize(
34764 61 53 74 61 74 69 63 29 29 0a 20 20 20 20 20 20  aStatic)).      
34765 20 7c 7c 20 28 61 44 79 6e 20 26 26 20 6e 41 72   || (aDyn && nAr
34766 67 3d 3d 28 69 6e 74 29 28 73 71 6c 69 74 65 33  g==(int)(sqlite3
34767 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c  DbMallocSize(db,
34768 20 61 44 79 6e 29 2f 73 69 7a 65 6f 66 28 76 6f   aDyn)/sizeof(vo
34769 69 64 2a 29 29 29 0a 20 20 20 20 20 20 29 7b 0a  id*))).      ){.
3476a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61          /* The a
3476b 41 72 67 5b 5d 20 61 72 72 61 79 20 6e 65 65 64  Arg[] array need
3476c 73 20 74 6f 20 67 72 6f 77 2e 20 2a 2f 0a 20 20  s to grow. */.  
3476d 20 20 20 20 20 20 76 6f 69 64 20 2a 2a 70 4e 65        void **pNe
3476e 77 20 3d 20 28 76 6f 69 64 20 2a 2a 29 73 71 6c  w = (void **)sql
3476f 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 41 72 67 2a  ite3Malloc(nArg*
34770 73 69 7a 65 6f 66 28 76 6f 69 64 20 2a 29 2a 32  sizeof(void *)*2
34771 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
34772 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20  New ){.         
34773 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 61 41   memcpy(pNew, aA
34774 72 67 2c 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28  rg, nArg*sizeof(
34775 76 6f 69 64 20 2a 29 29 3b 0a 20 20 20 20 20 20  void *));.      
34776 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
34777 28 61 44 79 6e 29 3b 0a 20 20 20 20 20 20 20 20  (aDyn);.        
34778 20 20 61 44 79 6e 20 3d 20 61 41 72 67 20 3d 20    aDyn = aArg = 
34779 70 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 7d 65  pNew;.        }e
3477a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
3477b 2a 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68  * This occurs wh
3477c 65 6e 20 74 68 65 20 61 72 72 61 79 20 6f 66 20  en the array of 
3477d 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73  context pointers
3477e 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 20 20   that need to.  
3477f 20 20 20 20 20 20 20 20 2a 2a 20 62 65 20 70 61          ** be pa
34780 73 73 65 64 20 74 6f 20 74 68 65 20 75 6e 6c 6f  ssed to the unlo
34781 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
34782 63 6b 20 69 73 20 6c 61 72 67 65 72 20 74 68 61  ck is larger tha
34783 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  n the.          
34784 2a 2a 20 61 53 74 61 74 69 63 5b 5d 20 61 72 72  ** aStatic[] arr
34785 61 79 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20  ay allocated on 
34786 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20 74 68  the stack and th
34787 65 20 61 74 74 65 6d 70 74 20 74 6f 20 0a 20 20  e attempt to .  
34788 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63          ** alloc
34789 61 74 65 20 61 20 6c 61 72 67 65 72 20 61 72 72  ate a larger arr
3478a 61 79 20 66 72 6f 6d 20 74 68 65 20 68 65 61 70  ay from the heap
3478b 20 68 61 73 20 66 61 69 6c 65 64 2e 0a 20 20 20   has failed..   
3478c 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
3478d 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61      ** This is a
3478e 20 64 69 66 66 69 63 75 6c 74 20 73 69 74 75 61   difficult situa
3478f 74 69 6f 6e 20 74 6f 20 68 61 6e 64 6c 65 2e 20  tion to handle. 
34790 52 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72  Returning an err
34791 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  or.          ** 
34792 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c  code to the call
34793 65 72 20 69 73 20 69 6e 73 75 66 66 69 63 69 65  er is insufficie
34794 6e 74 2c 20 61 73 20 65 76 65 6e 20 69 66 20 61  nt, as even if a
34795 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a 20 20 20  n error code.   
34796 20 20 20 20 20 20 20 2a 2a 20 69 73 20 72 65 74         ** is ret
34797 75 72 6e 65 64 20 74 68 65 20 74 72 61 6e 73 61  urned the transa
34798 63 74 69 6f 6e 20 6f 6e 20 63 6f 6e 6e 65 63 74  ction on connect
34799 69 6f 6e 20 64 62 20 77 69 6c 6c 20 73 74 69 6c  ion db will stil
3479a 6c 20 62 65 0a 20 20 20 20 20 20 20 20 20 20 2a  l be.          *
3479b 2a 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 65  * closed and the
3479c 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
3479d 61 6c 6c 62 61 63 6b 73 20 6f 6e 20 62 6c 6f 63  allbacks on bloc
3479e 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a  ked connections.
3479f 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c            ** wil
347a0 6c 20 67 6f 20 75 6e 69 73 73 75 65 64 2e 20 54  l go unissued. T
347a1 68 69 73 20 6d 69 67 68 74 20 63 61 75 73 65 20  his might cause 
347a2 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
347a3 74 6f 20 77 61 69 74 0a 20 20 20 20 20 20 20 20  to wait.        
347a4 20 20 2a 2a 20 69 6e 64 65 66 69 6e 69 74 65 6c    ** indefinitel
347a5 79 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d  y for an unlock-
347a6 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20  notify callback 
347a7 74 68 61 74 20 77 69 6c 6c 20 6e 65 76 65 72 20  that will never 
347a8 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 72  .          ** ar
347a9 72 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20 20  rive..          
347aa 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  **.          ** 
347ab 49 6e 73 74 65 61 64 2c 20 69 6e 76 6f 6b 65 20  Instead, invoke 
347ac 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  the unlock-notif
347ad 79 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20  y callback with 
347ae 74 68 65 20 63 6f 6e 74 65 78 74 0a 20 20 20 20  the context.    
347af 20 20 20 20 20 20 2a 2a 20 61 72 72 61 79 20 61        ** array a
347b0 6c 72 65 61 64 79 20 61 63 63 75 6d 75 6c 61 74  lready accumulat
347b1 65 64 2e 20 57 65 20 63 61 6e 20 74 68 65 6e 20  ed. We can then 
347b2 63 6c 65 61 72 20 74 68 65 20 61 72 72 61 79 20  clear the array 
347b3 61 6e 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  and.          **
347b4 20 62 65 67 69 6e 20 61 63 63 75 6d 75 6c 61 74   begin accumulat
347b5 69 6e 67 20 61 6e 79 20 66 75 72 74 68 65 72 20  ing any further 
347b6 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73  context pointers
347b7 20 77 69 74 68 6f 75 74 20 0a 20 20 20 20 20 20   without .      
347b8 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67      ** requiring
347b9 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 61 6c 6c   any dynamic all
347ba 6f 63 61 74 69 6f 6e 2e 20 54 68 69 73 20 69 73  ocation. This is
347bb 20 73 75 62 2d 6f 70 74 69 6d 61 6c 20 62 65 63   sub-optimal bec
347bc 61 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 2a  ause.          *
347bd 2a 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  * it means that 
347be 69 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65 20 63  instead of one c
347bf 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 61 20 6c  allback with a l
347c0 61 72 67 65 20 61 72 72 61 79 20 6f 66 0a 20 20  arge array of.  
347c1 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 65          ** conte
347c2 78 74 20 70 6f 69 6e 74 65 72 73 20 74 68 65 20  xt pointers the 
347c3 61 70 70 6c 69 63 61 74 69 6f 6e 20 77 69 6c 6c  application will
347c4 20 72 65 63 65 69 76 65 20 74 77 6f 20 6f 72 20   receive two or 
347c5 6d 6f 72 65 0a 20 20 20 20 20 20 20 20 20 20 2a  more.          *
347c6 2a 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 74 68  * callbacks with
347c7 20 73 6d 61 6c 6c 65 72 20 61 72 72 61 79 73 20   smaller arrays 
347c8 6f 66 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74  of context point
347c9 65 72 73 2c 20 77 68 69 63 68 20 77 69 6c 6c 0a  ers, which will.
347ca 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 64            ** red
347cb 75 63 65 20 74 68 65 20 61 70 70 6c 69 63 61 74  uce the applicat
347cc 69 6f 6e 73 20 61 62 69 6c 69 74 79 20 74 6f 20  ions ability to 
347cd 70 72 69 6f 72 69 74 69 7a 65 20 6d 75 6c 74 69  prioritize multi
347ce 70 6c 65 20 0a 20 20 20 20 20 20 20 20 20 20 2a  ple .          *
347cf 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 42  * connections. B
347d0 75 74 20 69 74 20 69 73 20 74 68 65 20 62 65 73  ut it is the bes
347d1 74 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 6f  t that can be do
347d2 6e 65 20 75 6e 64 65 72 20 74 68 65 0a 20 20 20  ne under the.   
347d3 20 20 20 20 20 20 20 2a 2a 20 63 69 72 63 75 6d         ** circum
347d4 73 74 61 6e 63 65 73 2e 0a 20 20 20 20 20 20 20  stances..       
347d5 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
347d6 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28 61 41  xUnlockNotify(aA
347d7 72 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 20  rg, nArg);.     
347d8 20 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a 20       nArg = 0;. 
347d9 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
347da 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e  .      sqlite3En
347db 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
347dc 0a 0a 20 20 20 20 20 20 61 41 72 67 5b 6e 41 72  ..      aArg[nAr
347dd 67 2b 2b 5d 20 3d 20 70 2d 3e 70 55 6e 6c 6f 63  g++] = p->pUnloc
347de 6b 41 72 67 3b 0a 20 20 20 20 20 20 78 55 6e 6c  kArg;.      xUnl
347df 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 70 2d 3e 78  ockNotify = p->x
347e0 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3b 0a 20 20  UnlockNotify;.  
347e1 20 20 20 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f      p->pUnlockCo
347e2 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20 20  nnection = 0;.  
347e3 20 20 20 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f      p->xUnlockNo
347e4 74 69 66 79 20 3d 20 30 3b 0a 20 20 20 20 20 20  tify = 0;.      
347e5 70 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 20 3d 20  p->pUnlockArg = 
347e6 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  0;.    }..    /*
347e7 20 53 74 65 70 20 33 2e 20 2a 2f 0a 20 20 20 20   Step 3. */.    
347e8 69 66 28 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67  if( p->pBlocking
347e9 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 26 26  Connection==0 &&
347ea 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65   p->pUnlockConne
347eb 63 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  ction==0 ){.    
347ec 20 20 2f 2a 20 52 65 6d 6f 76 65 20 63 6f 6e 6e    /* Remove conn
347ed 65 63 74 69 6f 6e 20 70 20 66 72 6f 6d 20 74 68  ection p from th
347ee 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  e blocked connec
347ef 74 69 6f 6e 73 20 6c 69 73 74 2e 20 2a 2f 0a 20  tions list. */. 
347f0 20 20 20 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e       *pp = p->pN
347f1 65 78 74 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20  extBlocked;.    
347f2 20 20 70 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65    p->pNextBlocke
347f3 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  d = 0;.    }else
347f4 7b 0a 20 20 20 20 20 20 70 70 20 3d 20 26 70 2d  {.      pp = &p-
347f5 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 0a 20  >pNextBlocked;. 
347f6 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
347f7 6e 41 72 67 21 3d 30 20 29 7b 0a 20 20 20 20 78  nArg!=0 ){.    x
347f8 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28 61 41 72  UnlockNotify(aAr
347f9 67 2c 20 6e 41 72 67 29 3b 0a 20 20 7d 0a 20 20  g, nArg);.  }.  
347fa 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 44 79  sqlite3_free(aDy
347fb 6e 29 3b 0a 20 20 6c 65 61 76 65 4d 75 74 65 78  n);.  leaveMutex
347fc 28 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ();         /* L
347fd 65 61 76 65 20 53 54 41 54 49 43 5f 4d 41 53 54  eave STATIC_MAST
347fe 45 52 20 6d 75 74 65 78 20 2a 2f 0a 7d 0a 0a 2f  ER mutex */.}../
347ff 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c  *.** This is cal
34800 6c 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74  led when the dat
34801 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
34802 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72   passed as an ar
34803 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 62 65  gument is .** be
34804 69 6e 67 20 63 6c 6f 73 65 64 2e 20 54 68 65 20  ing closed. The 
34805 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 72 65  connection is re
34806 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 62  moved from the b
34807 6c 6f 63 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a  locked list..*/.
34808 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
34809 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  oid sqlite3Conne
3480a 63 74 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69  ctionClosed(sqli
3480b 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69  te3 *db){.  sqli
3480c 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c  te3ConnectionUnl
3480d 6f 63 6b 65 64 28 64 62 29 3b 0a 20 20 65 6e 74  ocked(db);.  ent
3480e 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 6d  erMutex();.  rem
3480f 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69  oveFromBlockedLi
34810 73 74 28 64 62 29 3b 0a 20 20 63 68 65 63 6b 4c  st(db);.  checkL
34811 69 73 74 50 72 6f 70 65 72 74 69 65 73 28 64 62  istProperties(db
34812 29 3b 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28  );.  leaveMutex(
34813 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  );.}.#endif../**
34814 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
34815 20 6f 66 20 6e 6f 74 69 66 79 2e 63 20 2a 2a 2a   of notify.c ***
34816 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34817 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34818 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
34819 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
3481a 69 6e 20 66 69 6c 65 20 66 74 73 33 2e 63 20 2a  in file fts3.c *
3481b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3481c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3481d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
3481e 2a 2a 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a  ** 2006 Oct 10.*
3481f 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
34820 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
34821 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
34822 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
34823 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
34824 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
34825 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
34826 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
34827 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
34828 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
34829 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
3482a 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
3482b 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
3482c 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
3482d 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
3482e 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
3482f 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
34830 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
34831 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34832 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34833 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34834 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34835 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  **.**.** This is
34836 20 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c   an SQLite modul
34837 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 66  e implementing f
34838 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 2e  ull-text search.
34839 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63  .*/../*.** The c
3483a 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
3483b 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65   is only compile
3483c 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  d if:.**.**     
3483d 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c  * The FTS3 modul
3483e 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74  e is being built
3483f 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e   as an extension
34840 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68  .**       (in wh
34841 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f  ich case SQLITE_
34842 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69  CORE is not defi
34843 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20  ned), or.**.**  
34844 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f     * The FTS3 mo
34845 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75  dule is being bu
34846 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72  ilt into the cor
34847 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51  e of.**       SQ
34848 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63  Lite (in which c
34849 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ase SQLITE_ENABL
3484a 45 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65  E_FTS3 is define
3484b 64 29 2e 0a 2a 2f 0a 0a 2f 2a 20 54 4f 44 4f 28  d)..*/../* TODO(
3484c 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20  shess) Consider 
3484d 65 78 70 6f 72 74 69 6e 67 20 74 68 69 73 20 63  exporting this c
3484e 6f 6d 6d 65 6e 74 20 74 6f 20 61 6e 20 48 54 4d  omment to an HTM
3484f 4c 20 66 69 6c 65 20 6f 72 20 74 68 65 0a 2a 2a  L file or the.**
34850 20 77 69 6b 69 2e 0a 2a 2f 0a 2f 2a 20 54 68 65   wiki..*/./* The
34851 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78   full-text index
34852 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20   is stored in a 
34853 73 65 72 69 65 73 20 6f 66 20 62 2b 74 72 65 65  series of b+tree
34854 20 28 2d 6c 69 6b 65 29 0a 2a 2a 20 73 74 72 75   (-like).** stru
34855 63 74 75 72 65 73 20 63 61 6c 6c 65 64 20 73 65  ctures called se
34856 67 6d 65 6e 74 73 20 77 68 69 63 68 20 6d 61 70  gments which map
34857 20 74 65 72 6d 73 20 74 6f 20 64 6f 63 6c 69 73   terms to doclis
34858 74 73 2e 20 20 54 68 65 0a 2a 2a 20 73 74 72 75  ts.  The.** stru
34859 63 74 75 72 65 73 20 61 72 65 20 6c 69 6b 65 20  ctures are like 
3485a 62 2b 74 72 65 65 73 20 69 6e 20 6c 61 79 6f 75  b+trees in layou
3485b 74 2c 20 62 75 74 20 61 72 65 20 63 6f 6e 73 74  t, but are const
3485c 72 75 63 74 65 64 20 66 72 6f 6d 20 74 68 65 0a  ructed from the.
3485d 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 20 69 6e 20  ** bottom up in 
3485e 6f 70 74 69 6d 61 6c 20 66 61 73 68 69 6f 6e 20  optimal fashion 
3485f 61 6e 64 20 61 72 65 20 6e 6f 74 20 75 70 64 61  and are not upda
34860 74 61 62 6c 65 2e 20 20 53 69 6e 63 65 20 74 72  table.  Since tr
34861 65 65 73 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74  ees.** are built
34862 20 66 72 6f 6d 20 74 68 65 20 62 6f 74 74 6f 6d   from the bottom
34863 20 75 70 2c 20 74 68 69 6e 67 73 20 77 69 6c 6c   up, things will
34864 20 62 65 20 64 65 73 63 72 69 62 65 64 20 66 72   be described fr
34865 6f 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74 6f 6d  om the.** bottom
34866 20 75 70 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20   up..**.**.**** 
34867 56 61 72 69 6e 74 73 20 2a 2a 2a 2a 0a 2a 2a 20  Varints ****.** 
34868 54 68 65 20 62 61 73 69 63 20 75 6e 69 74 20 6f  The basic unit o
34869 66 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 20  f encoding is a 
3486a 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
3486b 69 6e 74 65 67 65 72 20 63 61 6c 6c 65 64 20 61  integer called a
3486c 0a 2a 2a 20 76 61 72 69 6e 74 2e 20 20 57 65 20  .** varint.  We 
3486d 65 6e 63 6f 64 65 20 76 61 72 69 61 62 6c 65 2d  encode variable-
3486e 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 20  length integers 
3486f 69 6e 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  in little-endian
34870 20 6f 72 64 65 72 0a 2a 2a 20 75 73 69 6e 67 20   order.** using 
34871 73 65 76 65 6e 20 62 69 74 73 20 2a 20 70 65 72  seven bits * per
34872 20 62 79 74 65 20 61 73 20 66 6f 6c 6c 6f 77 73   byte as follows
34873 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20  :.**.** KEY:.** 
34874 20 20 20 20 20 20 20 20 41 20 3d 20 30 78 78 78          A = 0xxx
34875 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20 6f  xxxx    7 bits o
34876 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66  f data and one f
34877 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20  lag bit.**      
34878 20 20 20 42 20 3d 20 31 78 78 78 78 78 78 78 20     B = 1xxxxxxx 
34879 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 74     7 bits of dat
3487a 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62  a and one flag b
3487b 69 74 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73  it.**.**  7 bits
3487c 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20   - A.** 14 bits 
3487d 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20  - BA.** 21 bits 
3487e 2d 20 42 42 41 0a 2a 2a 20 61 6e 64 20 73 6f 20  - BBA.** and so 
3487f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  on..**.** This i
34880 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 68  s identical to h
34881 6f 77 20 73 71 6c 69 74 65 20 65 6e 63 6f 64 65  ow sqlite encode
34882 73 20 76 61 72 69 6e 74 73 20 28 73 65 65 20 75  s varints (see u
34883 74 69 6c 2e 63 29 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a  til.c)..**.**.**
34884 2a 2a 20 44 6f 63 75 6d 65 6e 74 20 6c 69 73 74  ** Document list
34885 73 20 2a 2a 2a 2a 0a 2a 2a 20 41 20 64 6f 63 6c  s ****.** A docl
34886 69 73 74 20 28 64 6f 63 75 6d 65 6e 74 20 6c 69  ist (document li
34887 73 74 29 20 68 6f 6c 64 73 20 61 20 64 6f 63 69  st) holds a doci
34888 64 2d 73 6f 72 74 65 64 20 6c 69 73 74 20 6f 66  d-sorted list of
34889 20 68 69 74 73 20 66 6f 72 20 61 0a 2a 2a 20 67   hits for a.** g
3488a 69 76 65 6e 20 74 65 72 6d 2e 20 20 44 6f 63 6c  iven term.  Docl
3488b 69 73 74 73 20 68 6f 6c 64 20 64 6f 63 69 64 73  ists hold docids
3488c 2c 20 61 6e 64 20 63 61 6e 20 6f 70 74 69 6f 6e  , and can option
3488d 61 6c 6c 79 20 61 73 73 6f 63 69 61 74 65 0a 2a  ally associate.*
3488e 2a 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e  * token position
3488f 73 20 61 6e 64 20 6f 66 66 73 65 74 73 20 77 69  s and offsets wi
34890 74 68 20 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a  th docids..**.**
34891 20 41 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f   A DL_POSITIONS_
34892 4f 46 46 53 45 54 53 20 64 6f 63 6c 69 73 74 20  OFFSETS doclist 
34893 69 73 20 73 74 6f 72 65 64 20 6c 69 6b 65 20 74  is stored like t
34894 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 61 72 72 61 79  his:.**.** array
34895 20 7b 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 64   {.**   varint d
34896 6f 63 69 64 3b 0a 2a 2a 20 20 20 61 72 72 61 79  ocid;.**   array
34897 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
34898 20 20 28 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74    (position list
34899 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 30 29 0a 2a   for column 0).*
3489a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 70 6f 73  *     varint pos
3489b 69 74 69 6f 6e 3b 20 20 20 20 20 28 64 65 6c 74  ition;     (delt
3489c 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20  a from previous 
3489d 70 6f 73 69 74 69 6f 6e 20 70 6c 75 73 20 50 4f  position plus PO
3489e 53 5f 42 41 53 45 29 0a 2a 2a 20 20 20 20 20 76  S_BASE).**     v
3489f 61 72 69 6e 74 20 73 74 61 72 74 4f 66 66 73 65  arint startOffse
348a0 74 3b 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20  t;  (delta from 
348a1 70 72 65 76 69 6f 75 73 20 73 74 61 72 74 4f 66  previous startOf
348a2 66 73 65 74 29 0a 2a 2a 20 20 20 20 20 76 61 72  fset).**     var
348a3 69 6e 74 20 65 6e 64 4f 66 66 73 65 74 3b 20 20  int endOffset;  
348a4 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 73 74    (delta from st
348a5 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20 20  artOffset).**   
348a6 7d 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a  }.**   array {.*
348a7 2a 20 20 20 20 20 76 61 72 69 6e 74 20 50 4f 53  *     varint POS
348a8 5f 43 4f 4c 55 4d 4e 3b 20 20 20 28 6d 61 72 6b  _COLUMN;   (mark
348a9 73 20 73 74 61 72 74 20 6f 66 20 70 6f 73 69 74  s start of posit
348aa 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 6e 65 77  ion list for new
348ab 20 63 6f 6c 75 6d 6e 29 0a 2a 2a 20 20 20 20 20   column).**     
348ac 76 61 72 69 6e 74 20 63 6f 6c 75 6d 6e 3b 20 20  varint column;  
348ad 20 20 20 20 20 28 69 6e 64 65 78 20 6f 66 20 6e       (index of n
348ae 65 77 20 63 6f 6c 75 6d 6e 29 0a 2a 2a 20 20 20  ew column).**   
348af 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20    array {.**    
348b0 20 20 20 76 61 72 69 6e 74 20 70 6f 73 69 74 69     varint positi
348b1 6f 6e 3b 20 20 20 28 64 65 6c 74 61 20 66 72 6f  on;   (delta fro
348b2 6d 20 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74  m previous posit
348b3 69 6f 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53  ion plus POS_BAS
348b4 45 29 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69  E).**       vari
348b5 6e 74 20 73 74 61 72 74 4f 66 66 73 65 74 3b 28  nt startOffset;(
348b6 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69  delta from previ
348b7 6f 75 73 20 73 74 61 72 74 4f 66 66 73 65 74 29  ous startOffset)
348b8 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69 6e 74  .**       varint
348b9 20 65 6e 64 4f 66 66 73 65 74 3b 20 20 28 64 65   endOffset;  (de
348ba 6c 74 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66  lta from startOf
348bb 66 73 65 74 29 0a 2a 2a 20 20 20 20 20 7d 0a 2a  fset).**     }.*
348bc 2a 20 20 20 7d 0a 2a 2a 20 20 20 76 61 72 69 6e  *   }.**   varin
348bd 74 20 50 4f 53 5f 45 4e 44 3b 20 20 20 20 20 20  t POS_END;      
348be 20 20 28 6d 61 72 6b 73 20 65 6e 64 20 6f 66 20    (marks end of 
348bf 70 6f 73 69 74 69 6f 6e 73 20 66 6f 72 20 74 68  positions for th
348c0 69 73 20 64 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20  is document..** 
348c1 7d 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 61 72  }.**.** Here, ar
348c2 72 61 79 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20  ray { X } means 
348c3 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63  zero or more occ
348c4 75 72 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61  urrences of X, a
348c5 64 6a 61 63 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65  djacent in.** me
348c6 6d 6f 72 79 2e 20 20 41 20 22 70 6f 73 69 74 69  mory.  A "positi
348c7 6f 6e 22 20 69 73 20 61 6e 20 69 6e 64 65 78 20  on" is an index 
348c8 6f 66 20 61 20 74 6f 6b 65 6e 20 69 6e 20 74 68  of a token in th
348c9 65 20 74 6f 6b 65 6e 20 73 74 72 65 61 6d 0a 2a  e token stream.*
348ca 2a 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74  * generated by t
348cb 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2c 20 77 68  he tokenizer, wh
348cc 69 6c 65 20 61 6e 20 22 6f 66 66 73 65 74 22 20  ile an "offset" 
348cd 69 73 20 61 20 62 79 74 65 20 6f 66 66 73 65 74  is a byte offset
348ce 2c 0a 2a 2a 20 62 6f 74 68 20 62 61 73 65 64 20  ,.** both based 
348cf 61 74 20 30 2e 20 20 4e 6f 74 65 20 74 68 61 74  at 0.  Note that
348d0 20 50 4f 53 5f 45 4e 44 20 61 6e 64 20 50 4f 53   POS_END and POS
348d1 5f 43 4f 4c 55 4d 4e 20 6f 63 63 75 72 20 69 6e  _COLUMN occur in
348d2 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6c 6f 67   the.** same log
348d3 69 63 61 6c 20 70 6c 61 63 65 20 61 73 20 74 68  ical place as th
348d4 65 20 70 6f 73 69 74 69 6f 6e 20 65 6c 65 6d 65  e position eleme
348d5 6e 74 2c 20 61 6e 64 20 61 63 74 20 61 73 20 73  nt, and act as s
348d6 65 6e 74 69 6e 61 6c 73 0a 2a 2a 20 65 6e 64 69  entinals.** endi
348d7 6e 67 20 61 20 70 6f 73 69 74 69 6f 6e 20 6c 69  ng a position li
348d8 73 74 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20  st array..**.** 
348d9 41 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 64  A DL_POSITIONS d
348da 6f 63 6c 69 73 74 20 6f 6d 69 74 73 20 74 68 65  oclist omits the
348db 20 73 74 61 72 74 4f 66 66 73 65 74 20 61 6e 64   startOffset and
348dc 20 65 6e 64 4f 66 66 73 65 74 0a 2a 2a 20 69 6e   endOffset.** in
348dd 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 20 44 4c  formation.  A DL
348de 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 20  _DOCIDS doclist 
348df 6f 6d 69 74 73 20 62 6f 74 68 20 74 68 65 20 70  omits both the p
348e0 6f 73 69 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 6f  osition and.** o
348e1 66 66 73 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f  ffset informatio
348e2 6e 2c 20 62 65 63 6f 6d 69 6e 67 20 61 6e 20 61  n, becoming an a
348e3 72 72 61 79 20 6f 66 20 76 61 72 69 6e 74 2d 65  rray of varint-e
348e4 6e 63 6f 64 65 64 20 64 6f 63 69 64 73 2e 0a 2a  ncoded docids..*
348e5 2a 0a 2a 2a 20 4f 6e 2d 64 69 73 6b 20 64 61 74  *.** On-disk dat
348e6 61 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 74  a is stored as t
348e7 79 70 65 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  ype DL_DEFAULT, 
348e8 73 6f 20 77 65 20 64 6f 6e 27 74 20 73 65 72 69  so we don't seri
348e9 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 74 79 70  alize.** the typ
348ea 65 2e 20 20 44 75 65 20 74 6f 20 68 6f 77 20 64  e.  Due to how d
348eb 65 6c 65 74 69 6f 6e 20 69 73 20 69 6d 70 6c 65  eletion is imple
348ec 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 20 73 65  mented in the se
348ed 67 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 73 79  gmentation.** sy
348ee 73 74 65 6d 2c 20 6f 6e 2d 64 69 73 6b 20 64 6f  stem, on-disk do
348ef 63 6c 69 73 74 73 20 4d 55 53 54 20 73 74 6f 72  clists MUST stor
348f0 65 20 61 74 20 6c 65 61 73 74 20 70 6f 73 69 74  e at least posit
348f1 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a  ions..**.**.****
348f2 20 53 65 67 6d 65 6e 74 20 6c 65 61 66 20 6e 6f   Segment leaf no
348f3 64 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d  des ****.** Segm
348f4 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 73  ent leaf nodes s
348f5 74 6f 72 65 20 74 65 72 6d 73 20 61 6e 64 20 64  tore terms and d
348f6 6f 63 6c 69 73 74 73 2c 20 6f 72 64 65 72 65 64  oclists, ordered
348f7 20 62 79 20 74 65 72 6d 2e 20 20 4c 65 61 66 0a   by term.  Leaf.
348f8 2a 2a 20 6e 6f 64 65 73 20 61 72 65 20 77 72 69  ** nodes are wri
348f9 74 74 65 6e 20 75 73 69 6e 67 20 4c 65 61 66 57  tten using LeafW
348fa 72 69 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20  riter, and read 
348fb 75 73 69 6e 67 20 4c 65 61 66 52 65 61 64 65 72  using LeafReader
348fc 20 28 74 6f 0a 2a 2a 20 69 74 65 72 61 74 65 20   (to.** iterate 
348fd 74 68 72 6f 75 67 68 20 61 20 73 69 6e 67 6c 65  through a single
348fe 20 6c 65 61 66 20 6e 6f 64 65 27 73 20 64 61 74   leaf node's dat
348ff 61 29 20 61 6e 64 20 4c 65 61 76 65 73 52 65 61  a) and LeavesRea
34900 64 65 72 20 28 74 6f 0a 2a 2a 20 69 74 65 72 61  der (to.** itera
34901 74 65 20 74 68 72 6f 75 67 68 20 61 20 73 65 67  te through a seg
34902 6d 65 6e 74 27 73 20 65 6e 74 69 72 65 20 6c 65  ment's entire le
34903 61 66 20 6c 61 79 65 72 29 2e 20 20 4c 65 61 66  af layer).  Leaf
34904 20 6e 6f 64 65 73 20 68 61 76 65 0a 2a 2a 20 74   nodes have.** t
34905 68 65 20 66 6f 72 6d 61 74 3a 0a 2a 2a 0a 2a 2a  he format:.**.**
34906 20 76 61 72 69 6e 74 20 69 48 65 69 67 68 74 3b   varint iHeight;
34907 20 20 20 20 20 20 20 20 20 20 20 20 20 28 68 65               (he
34908 69 67 68 74 20 66 72 6f 6d 20 6c 65 61 66 20 6c  ight from leaf l
34909 65 76 65 6c 2c 20 61 6c 77 61 79 73 20 30 29 0a  evel, always 0).
3490a 2a 2a 20 76 61 72 69 6e 74 20 6e 54 65 72 6d 3b  ** varint nTerm;
3490b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
3490c 6c 65 6e 67 74 68 20 6f 66 20 66 69 72 73 74 20  length of first 
3490d 74 65 72 6d 29 0a 2a 2a 20 63 68 61 72 20 70 54  term).** char pT
3490e 65 72 6d 5b 6e 54 65 72 6d 5d 3b 20 20 20 20 20  erm[nTerm];     
3490f 20 20 20 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66       (content of
34910 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20   first term).** 
34911 76 61 72 69 6e 74 20 6e 44 6f 63 6c 69 73 74 3b  varint nDoclist;
34912 20 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e              (len
34913 67 74 68 20 6f 66 20 74 65 72 6d 27 73 20 61 73  gth of term's as
34914 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73 74  sociated doclist
34915 29 0a 2a 2a 20 63 68 61 72 20 70 44 6f 63 6c 69  ).** char pDocli
34916 73 74 5b 6e 44 6f 63 6c 69 73 74 5d 3b 20 20 20  st[nDoclist];   
34917 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20 64 6f 63   (content of doc
34918 6c 69 73 74 29 0a 2a 2a 20 61 72 72 61 79 20 7b  list).** array {
34919 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3491a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3491b 28 66 75 72 74 68 65 72 20 74 65 72 6d 73 20 61  (further terms a
3491c 72 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64  re delta-encoded
3491d 29 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 50  ).**   varint nP
3491e 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  refix;          
3491f 20 28 6c 65 6e 67 74 68 20 6f 66 20 70 72 65 66   (length of pref
34920 69 78 20 73 68 61 72 65 64 20 77 69 74 68 20 70  ix shared with p
34921 72 65 76 69 6f 75 73 20 74 65 72 6d 29 0a 2a 2a  revious term).**
34922 20 20 20 76 61 72 69 6e 74 20 6e 53 75 66 66 69     varint nSuffi
34923 78 3b 20 20 20 20 20 20 20 20 20 20 20 28 6c 65  x;           (le
34924 6e 67 74 68 20 6f 66 20 75 6e 73 68 61 72 65 64  ngth of unshared
34925 20 73 75 66 66 69 78 29 0a 2a 2a 20 20 20 63 68   suffix).**   ch
34926 61 72 20 70 54 65 72 6d 53 75 66 66 69 78 5b 6e  ar pTermSuffix[n
34927 53 75 66 66 69 78 5d 3b 28 75 6e 73 68 61 72 65  Suffix];(unshare
34928 64 20 73 75 66 66 69 78 20 6f 66 20 6e 65 78 74  d suffix of next
34929 20 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69   term).**   vari
3492a 6e 74 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20  nt nDoclist;    
3492b 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
3492c 20 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61 74   term's associat
3492d 65 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 20  ed doclist).**  
3492e 20 63 68 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e   char pDoclist[n
3492f 44 6f 63 6c 69 73 74 5d 3b 20 20 28 63 6f 6e 74  Doclist];  (cont
34930 65 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29 0a  ent of doclist).
34931 2a 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c  ** }.**.** Here,
34932 20 61 72 72 61 79 20 7b 20 58 20 7d 20 6d 65 61   array { X } mea
34933 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  ns zero or more 
34934 6f 63 63 75 72 72 65 6e 63 65 73 20 6f 66 20 58  occurrences of X
34935 2c 20 61 64 6a 61 63 65 6e 74 20 69 6e 0a 2a 2a  , adjacent in.**
34936 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4c   memory..**.** L
34937 65 61 66 20 6e 6f 64 65 73 20 61 72 65 20 62 72  eaf nodes are br
34938 6f 6b 65 6e 20 69 6e 74 6f 20 62 6c 6f 63 6b 73  oken into blocks
34939 20 77 68 69 63 68 20 61 72 65 20 73 74 6f 72 65   which are store
3493a 64 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79 20 69  d contiguously i
3493b 6e 0a 2a 2a 20 74 68 65 20 25 5f 73 65 67 6d 65  n.** the %_segme
3493c 6e 74 73 20 74 61 62 6c 65 20 69 6e 20 73 6f 72  nts table in sor
3493d 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 69 73  ted order.  This
3493e 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e   means that when
3493f 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 61   the end.** of a
34940 20 6e 6f 64 65 20 69 73 20 72 65 61 63 68 65 64   node is reached
34941 2c 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20  , the next term 
34942 69 73 20 69 6e 20 74 68 65 20 6e 6f 64 65 20 77  is in the node w
34943 69 74 68 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20  ith the next.** 
34944 67 72 65 61 74 65 72 20 6e 6f 64 65 20 69 64 2e  greater node id.
34945 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20  .**.** New data 
34946 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20  is spilled to a 
34947 6e 65 77 20 6c 65 61 66 20 6e 6f 64 65 20 77 68  new leaf node wh
34948 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e  en the current n
34949 6f 64 65 0a 2a 2a 20 65 78 63 65 65 64 73 20 4c  ode.** exceeds L
3494a 45 41 46 5f 4d 41 58 20 62 79 74 65 73 20 28 64  EAF_MAX bytes (d
3494b 65 66 61 75 6c 74 20 32 30 34 38 29 2e 20 20 4e  efault 2048).  N
3494c 65 77 20 64 61 74 61 20 77 68 69 63 68 20 69 74  ew data which it
3494d 73 65 6c 66 20 69 73 0a 2a 2a 20 6c 61 72 67 65  self is.** large
3494e 72 20 74 68 61 6e 20 53 54 41 4e 44 41 4c 4f 4e  r than STANDALON
3494f 45 5f 4d 49 4e 20 28 64 65 66 61 75 6c 74 20 31  E_MIN (default 1
34950 30 32 34 29 20 69 73 20 70 6c 61 63 65 64 20 69  024) is placed i
34951 6e 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a  n a standalone.*
34952 2a 20 6e 6f 64 65 20 28 61 20 6c 65 61 66 20 6e  * node (a leaf n
34953 6f 64 65 20 77 69 74 68 20 61 20 73 69 6e 67 6c  ode with a singl
34954 65 20 74 65 72 6d 20 61 6e 64 20 64 6f 63 6c 69  e term and docli
34955 73 74 29 2e 20 20 54 68 65 20 67 6f 61 6c 20 6f  st).  The goal o
34956 66 0a 2a 2a 20 74 68 65 73 65 20 73 65 74 74 69  f.** these setti
34957 6e 67 73 20 69 73 20 74 6f 20 70 61 63 6b 20 74  ngs is to pack t
34958 6f 67 65 74 68 65 72 20 67 72 6f 75 70 73 20 6f  ogether groups o
34959 66 20 73 6d 61 6c 6c 20 64 6f 63 6c 69 73 74 73  f small doclists
3495a 20 77 68 69 6c 65 0a 2a 2a 20 6d 61 6b 69 6e 67   while.** making
3495b 20 69 74 20 65 66 66 69 63 69 65 6e 74 20 74 6f   it efficient to
3495c 20 64 69 72 65 63 74 6c 79 20 61 63 63 65 73 73   directly access
3495d 20 6c 61 72 67 65 20 64 6f 63 6c 69 73 74 73 2e   large doclists.
3495e 20 20 54 68 65 0a 2a 2a 20 61 73 73 75 6d 70 74    The.** assumpt
3495f 69 6f 6e 20 69 73 20 74 68 61 74 20 6c 61 72 67  ion is that larg
34960 65 20 64 6f 63 6c 69 73 74 73 20 72 65 70 72 65  e doclists repre
34961 73 65 6e 74 20 74 65 72 6d 73 20 77 68 69 63 68  sent terms which
34962 20 61 72 65 20 6d 6f 72 65 0a 2a 2a 20 6c 69 6b   are more.** lik
34963 65 6c 79 20 74 6f 20 62 65 20 71 75 65 72 79 20  ely to be query 
34964 74 61 72 67 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  targets..**.** T
34965 4f 44 4f 28 73 68 65 73 73 29 20 49 74 20 6d 61  ODO(shess) It ma
34966 79 20 62 65 20 75 73 65 66 75 6c 20 66 6f 72 20  y be useful for 
34967 62 6c 6f 63 6b 69 6e 67 20 64 65 63 69 73 69 6f  blocking decisio
34968 6e 73 20 74 6f 20 62 65 20 6d 6f 72 65 0a 2a 2a  ns to be more.**
34969 20 64 79 6e 61 6d 69 63 2e 20 20 46 6f 72 20 69   dynamic.  For i
3496a 6e 73 74 61 6e 63 65 2c 20 69 74 20 6d 61 79 20  nstance, it may 
3496b 6d 61 6b 65 20 6d 6f 72 65 20 73 65 6e 73 65 20  make more sense 
3496c 74 6f 20 68 61 76 65 20 61 20 32 2e 35 6b 20 6c  to have a 2.5k l
3496d 65 61 66 0a 2a 2a 20 6e 6f 64 65 20 72 61 74 68  eaf.** node rath
3496e 65 72 20 74 68 61 6e 20 73 70 6c 69 74 74 69 6e  er than splittin
3496f 67 20 69 6e 74 6f 20 32 6b 20 61 6e 64 20 2e 35  g into 2k and .5
34970 6b 20 6e 6f 64 65 73 2e 20 20 4d 79 20 69 6e 74  k nodes.  My int
34971 75 69 74 69 6f 6e 20 69 73 0a 2a 2a 20 74 68 61  uition is.** tha
34972 74 20 74 68 69 73 20 6d 69 67 68 74 20 65 78 74  t this might ext
34973 65 6e 64 20 74 68 72 6f 75 67 68 20 32 78 20 6f  end through 2x o
34974 72 20 34 78 20 74 68 65 20 70 61 67 65 73 69 7a  r 4x the pagesiz
34975 65 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65  e..**.**.**** Se
34976 67 6d 65 6e 74 20 69 6e 74 65 72 69 6f 72 20 6e  gment interior n
34977 6f 64 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 65 67  odes ****.** Seg
34978 6d 65 6e 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f  ment interior no
34979 64 65 73 20 73 74 6f 72 65 20 62 6c 6f 63 6b 69  des store blocki
3497a 64 73 20 66 6f 72 20 73 75 62 74 72 65 65 20 6e  ds for subtree n
3497b 6f 64 65 73 20 61 6e 64 20 74 65 72 6d 73 0a 2a  odes and terms.*
3497c 2a 20 74 6f 20 64 65 73 63 72 69 62 65 20 77 68  * to describe wh
3497d 61 74 20 64 61 74 61 20 69 73 20 73 74 6f 72 65  at data is store
3497e 64 20 62 79 20 74 68 65 20 65 61 63 68 20 73 75  d by the each su
3497f 62 74 72 65 65 2e 20 20 49 6e 74 65 72 69 6f 72  btree.  Interior
34980 0a 2a 2a 20 6e 6f 64 65 73 20 61 72 65 20 77 72  .** nodes are wr
34981 69 74 74 65 6e 20 75 73 69 6e 67 20 49 6e 74 65  itten using Inte
34982 72 69 6f 72 57 72 69 74 65 72 2c 20 61 6e 64 20  riorWriter, and 
34983 72 65 61 64 20 75 73 69 6e 67 0a 2a 2a 20 49 6e  read using.** In
34984 74 65 72 69 6f 72 52 65 61 64 65 72 2e 20 20 49  teriorReader.  I
34985 6e 74 65 72 69 6f 72 57 72 69 74 65 72 73 20 61  nteriorWriters a
34986 72 65 20 63 72 65 61 74 65 64 20 61 73 20 6e 65  re created as ne
34987 65 64 65 64 20 77 68 65 6e 0a 2a 2a 20 53 65 67  eded when.** Seg
34988 6d 65 6e 74 57 72 69 74 65 72 20 63 72 65 61 74  mentWriter creat
34989 65 73 20 6e 65 77 20 6c 65 61 66 20 6e 6f 64 65  es new leaf node
3498a 73 2c 20 6f 72 20 77 68 65 6e 20 61 6e 20 69 6e  s, or when an in
3498b 74 65 72 69 6f 72 20 6e 6f 64 65 0a 2a 2a 20 69  terior node.** i
3498c 74 73 65 6c 66 20 67 72 6f 77 73 20 74 6f 6f 20  tself grows too 
3498d 62 69 67 20 61 6e 64 20 6d 75 73 74 20 62 65 20  big and must be 
3498e 73 70 6c 69 74 2e 20 20 54 68 65 20 66 6f 72 6d  split.  The form
3498f 61 74 20 6f 66 20 69 6e 74 65 72 69 6f 72 0a 2a  at of interior.*
34990 2a 20 6e 6f 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 76  * nodes:.**.** v
34991 61 72 69 6e 74 20 69 48 65 69 67 68 74 3b 20 20  arint iHeight;  
34992 20 20 20 20 20 20 20 20 20 28 68 65 69 67 68 74           (height
34993 20 66 72 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c   from leaf level
34994 2c 20 61 6c 77 61 79 73 20 3e 30 29 0a 2a 2a 20  , always >0).** 
34995 76 61 72 69 6e 74 20 69 42 6c 6f 63 6b 69 64 3b  varint iBlockid;
34996 20 20 20 20 20 20 20 20 20 20 28 62 6c 6f 63 6b            (block
34997 20 69 64 20 6f 66 20 6e 6f 64 65 27 73 20 6c 65   id of node's le
34998 66 74 6d 6f 73 74 20 73 75 62 74 72 65 65 29 0a  ftmost subtree).
34999 2a 2a 20 6f 70 74 69 6f 6e 61 6c 20 7b 0a 2a 2a  ** optional {.**
3499a 20 20 20 76 61 72 69 6e 74 20 6e 54 65 72 6d 3b     varint nTerm;
3499b 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67             (leng
3499c 74 68 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d  th of first term
3499d 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 54 65 72  ).**   char pTer
3499e 6d 5b 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20 28  m[nTerm];      (
3499f 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74  content of first
349a0 20 74 65 72 6d 29 0a 2a 2a 20 20 20 61 72 72 61   term).**   arra
349a1 79 20 7b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  y {.**          
349a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
349a3 20 20 20 20 20 20 28 66 75 72 74 68 65 72 20 74        (further t
349a4 65 72 6d 73 20 61 72 65 20 64 65 6c 74 61 2d 65  erms are delta-e
349a5 6e 63 6f 64 65 64 29 0a 2a 2a 20 20 20 20 20 76  ncoded).**     v
349a6 61 72 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20  arint nPrefix;  
349a7 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
349a8 68 20 6f 66 20 73 68 61 72 65 64 20 70 72 65 66  h of shared pref
349a9 69 78 20 77 69 74 68 20 70 72 65 76 69 6f 75 73  ix with previous
349aa 20 74 65 72 6d 29 0a 2a 2a 20 20 20 20 20 76 61   term).**     va
349ab 72 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20  rint nSuffix;   
349ac 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68           (length
349ad 20 6f 66 20 75 6e 73 68 61 72 65 64 20 73 75 66   of unshared suf
349ae 66 69 78 29 0a 2a 2a 20 20 20 20 20 63 68 61 72  fix).**     char
349af 20 70 54 65 72 6d 53 75 66 66 69 78 5b 6e 53 75   pTermSuffix[nSu
349b0 66 66 69 78 5d 3b 20 28 75 6e 73 68 61 72 65 64  ffix]; (unshared
349b1 20 73 75 66 66 69 78 20 6f 66 20 6e 65 78 74 20   suffix of next 
349b2 74 65 72 6d 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20  term).**   }.** 
349b3 7d 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 6f 70  }.**.** Here, op
349b4 74 69 6f 6e 61 6c 20 7b 20 58 20 7d 20 6d 65 61  tional { X } mea
349b5 6e 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 65  ns an optional e
349b6 6c 65 6d 65 6e 74 2c 20 77 68 69 6c 65 20 61 72  lement, while ar
349b7 72 61 79 20 7b 20 58 20 7d 0a 2a 2a 20 6d 65 61  ray { X }.** mea
349b8 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  ns zero or more 
349b9 6f 63 63 75 72 72 65 6e 63 65 73 20 6f 66 20 58  occurrences of X
349ba 2c 20 61 64 6a 61 63 65 6e 74 20 69 6e 20 6d 65  , adjacent in me
349bb 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69  mory..**.** An i
349bc 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 65 6e 63  nterior node enc
349bd 6f 64 65 73 20 6e 20 74 65 72 6d 73 20 73 65 70  odes n terms sep
349be 61 72 61 74 69 6e 67 20 6e 2b 31 20 73 75 62 74  arating n+1 subt
349bf 72 65 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 75  rees.  The.** su
349c0 62 74 72 65 65 20 62 6c 6f 63 6b 73 20 61 72 65  btree blocks are
349c1 20 63 6f 6e 74 69 67 75 6f 75 73 2c 20 73 6f 20   contiguous, so 
349c2 6f 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 73  only the first s
349c3 75 62 74 72 65 65 27 73 20 62 6c 6f 63 6b 69 64  ubtree's blockid
349c4 0a 2a 2a 20 69 73 20 65 6e 63 6f 64 65 64 2e 20  .** is encoded. 
349c5 20 54 68 65 20 73 75 62 74 72 65 65 20 61 74 20   The subtree at 
349c6 69 42 6c 6f 63 6b 69 64 20 77 69 6c 6c 20 63 6f  iBlockid will co
349c7 6e 74 61 69 6e 20 61 6c 6c 20 74 65 72 6d 73 20  ntain all terms 
349c8 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65  less.** than the
349c9 20 66 69 72 73 74 20 74 65 72 6d 20 65 6e 63 6f   first term enco
349ca 64 65 64 20 28 6f 72 20 61 6c 6c 20 74 65 72 6d  ded (or all term
349cb 73 20 69 66 20 6e 6f 20 74 65 72 6d 20 69 73 20  s if no term is 
349cc 65 6e 63 6f 64 65 64 29 2e 0a 2a 2a 20 4f 74 68  encoded)..** Oth
349cd 65 72 77 69 73 65 2c 20 66 6f 72 20 74 65 72 6d  erwise, for term
349ce 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
349cf 72 20 65 71 75 61 6c 20 74 6f 20 70 54 65 72 6d  r equal to pTerm
349d0 5b 69 5d 20 62 75 74 20 6c 65 73 73 0a 2a 2a 20  [i] but less.** 
349d1 74 68 61 6e 20 70 54 65 72 6d 5b 69 2b 31 5d 2c  than pTerm[i+1],
349d2 20 74 68 65 20 73 75 62 74 72 65 65 20 66 6f 72   the subtree for
349d3 20 74 68 61 74 20 74 65 72 6d 20 77 69 6c 6c 20   that term will 
349d4 62 65 20 72 6f 6f 74 65 64 20 61 74 0a 2a 2a 20  be rooted at.** 
349d5 69 42 6c 6f 63 6b 69 64 2b 69 2e 20 20 49 6e 74  iBlockid+i.  Int
349d6 65 72 69 6f 72 20 6e 6f 64 65 73 20 6f 6e 6c 79  erior nodes only
349d7 20 73 74 6f 72 65 20 65 6e 6f 75 67 68 20 74 65   store enough te
349d8 72 6d 20 64 61 74 61 20 74 6f 0a 2a 2a 20 64 69  rm data to.** di
349d9 73 74 69 6e 67 75 69 73 68 20 61 64 6a 61 63 65  stinguish adjace
349da 6e 74 20 63 68 69 6c 64 72 65 6e 20 28 69 66 20  nt children (if 
349db 74 68 65 20 72 69 67 68 74 6d 6f 73 74 20 74 65  the rightmost te
349dc 72 6d 20 6f 66 20 74 68 65 20 6c 65 66 74 0a 2a  rm of the left.*
349dd 2a 20 63 68 69 6c 64 20 69 73 20 22 73 6f 6d 65  * child is "some
349de 74 68 69 6e 67 22 2c 20 61 6e 64 20 74 68 65 20  thing", and the 
349df 6c 65 66 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66  leftmost term of
349e0 20 74 68 65 20 72 69 67 68 74 20 63 68 69 6c 64   the right child
349e1 20 69 73 0a 2a 2a 20 22 77 69 63 6b 65 64 22 2c   is.** "wicked",
349e2 20 6f 6e 6c 79 20 22 77 22 20 69 73 20 73 74 6f   only "w" is sto
349e3 72 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20  red)..**.** New 
349e4 64 61 74 61 20 69 73 20 73 70 69 6c 6c 65 64 20  data is spilled 
349e5 74 6f 20 61 20 6e 65 77 20 69 6e 74 65 72 69 6f  to a new interio
349e6 72 20 6e 6f 64 65 20 61 74 20 74 68 65 20 73 61  r node at the sa
349e7 6d 65 20 68 65 69 67 68 74 20 77 68 65 6e 0a 2a  me height when.*
349e8 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f  * the current no
349e9 64 65 20 65 78 63 65 65 64 73 20 49 4e 54 45 52  de exceeds INTER
349ea 49 4f 52 5f 4d 41 58 20 62 79 74 65 73 20 28 64  IOR_MAX bytes (d
349eb 65 66 61 75 6c 74 20 32 30 34 38 29 2e 0a 2a 2a  efault 2048)..**
349ec 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45   INTERIOR_MIN_TE
349ed 52 4d 53 20 28 64 65 66 61 75 6c 74 20 37 29 20  RMS (default 7) 
349ee 6b 65 65 70 73 20 6c 61 72 67 65 20 74 65 72 6d  keeps large term
349ef 73 20 66 72 6f 6d 20 6d 6f 6e 6f 70 6f 6c 69 7a  s from monopoliz
349f0 69 6e 67 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20  ing.** interior 
349f1 6e 6f 64 65 73 20 61 6e 64 20 6d 61 6b 69 6e 67  nodes and making
349f2 20 74 68 65 20 74 72 65 65 20 74 6f 6f 20 73 6b   the tree too sk
349f3 69 6e 6e 79 2e 20 20 54 68 65 20 69 6e 74 65 72  inny.  The inter
349f4 69 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 61 74 20  ior nodes.** at 
349f5 61 20 67 69 76 65 6e 20 68 65 69 67 68 74 20 61  a given height a
349f6 72 65 20 6e 61 74 75 72 61 6c 6c 79 20 74 72 61  re naturally tra
349f7 63 6b 65 64 20 62 79 20 69 6e 74 65 72 69 6f 72  cked by interior
349f8 20 6e 6f 64 65 73 20 61 74 0a 2a 2a 20 68 65 69   nodes at.** hei
349f9 67 68 74 2b 31 2c 20 61 6e 64 20 73 6f 20 6f 6e  ght+1, and so on
349fa 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67  ..**.**.**** Seg
349fb 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 2a  ment directory *
349fc 2a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 67 6d 65  ***.** The segme
349fd 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69 6e 20  nt directory in 
349fe 74 61 62 6c 65 20 25 5f 73 65 67 64 69 72 20 73  table %_segdir s
349ff 74 6f 72 65 73 20 6d 65 74 61 2d 69 6e 66 6f 72  tores meta-infor
34a00 6d 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 6d 65  mation for.** me
34a01 72 67 69 6e 67 20 61 6e 64 20 64 65 6c 65 74 69  rging and deleti
34a02 6e 67 20 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64  ng segments, and
34a03 20 61 6c 73 6f 20 74 68 65 20 72 6f 6f 74 20 6e   also the root n
34a04 6f 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 65  ode of the.** se
34a05 67 6d 65 6e 74 27 73 20 74 72 65 65 2e 0a 2a 2a  gment's tree..**
34a06 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 6e 6f 64  .** The root nod
34a07 65 20 69 73 20 74 68 65 20 74 6f 70 20 6e 6f 64  e is the top nod
34a08 65 20 6f 66 20 74 68 65 20 73 65 67 6d 65 6e 74  e of the segment
34a09 27 73 20 74 72 65 65 20 61 66 74 65 72 20 65 6e  's tree after en
34a0a 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 65 20 65 6e  coding.** the en
34a0b 74 69 72 65 20 73 65 67 6d 65 6e 74 2c 20 72 65  tire segment, re
34a0c 73 74 72 69 63 74 65 64 20 74 6f 20 52 4f 4f 54  stricted to ROOT
34a0d 5f 4d 41 58 20 62 79 74 65 73 20 28 64 65 66 61  _MAX bytes (defa
34a0e 75 6c 74 20 31 30 32 34 29 2e 0a 2a 2a 20 54 68  ult 1024)..** Th
34a0f 69 73 20 63 6f 75 6c 64 20 62 65 20 65 69 74 68  is could be eith
34a10 65 72 20 61 20 6c 65 61 66 20 6e 6f 64 65 20 6f  er a leaf node o
34a11 72 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  r an interior no
34a12 64 65 2e 20 20 49 66 20 74 68 65 20 74 6f 70 0a  de.  If the top.
34a13 2a 2a 20 6e 6f 64 65 20 72 65 71 75 69 72 65 73  ** node requires
34a14 20 6d 6f 72 65 20 74 68 61 6e 20 52 4f 4f 54 5f   more than ROOT_
34a15 4d 41 58 20 62 79 74 65 73 2c 20 69 74 20 69 73  MAX bytes, it is
34a16 20 66 6c 75 73 68 65 64 20 74 6f 20 25 5f 73 65   flushed to %_se
34a17 67 6d 65 6e 74 73 0a 2a 2a 20 61 6e 64 20 61 20  gments.** and a 
34a18 6e 65 77 20 72 6f 6f 74 20 69 6e 74 65 72 69 6f  new root interio
34a19 72 20 6e 6f 64 65 20 69 73 20 67 65 6e 65 72 61  r node is genera
34a1a 74 65 64 20 28 77 68 69 63 68 20 73 68 6f 75 6c  ted (which shoul
34a1b 64 20 61 6c 77 61 79 73 20 66 69 74 0a 2a 2a 20  d always fit.** 
34a1c 77 69 74 68 69 6e 20 52 4f 4f 54 5f 4d 41 58 20  within ROOT_MAX 
34a1d 62 65 63 61 75 73 65 20 69 74 20 6f 6e 6c 79 20  because it only 
34a1e 6e 65 65 64 73 20 73 70 61 63 65 20 66 6f 72 20  needs space for 
34a1f 32 20 76 61 72 69 6e 74 73 2c 20 74 68 65 0a 2a  2 varints, the.*
34a20 2a 20 68 65 69 67 68 74 20 61 6e 64 20 74 68 65  * height and the
34a21 20 62 6c 6f 63 6b 69 64 20 6f 66 20 74 68 65 20   blockid of the 
34a22 70 72 65 76 69 6f 75 73 20 72 6f 6f 74 29 2e 0a  previous root)..
34a23 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 74 61 2d 69  **.** The meta-i
34a24 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68  nformation in th
34a25 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74  e segment direct
34a26 6f 72 79 20 69 73 3a 0a 2a 2a 20 20 20 6c 65 76  ory is:.**   lev
34a27 65 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  el              
34a28 20 2d 20 73 65 67 6d 65 6e 74 20 6c 65 76 65 6c   - segment level
34a29 20 28 73 65 65 20 62 65 6c 6f 77 29 0a 2a 2a 20   (see below).** 
34a2a 20 20 69 64 78 20 20 20 20 20 20 20 20 20 20 20    idx           
34a2b 20 20 20 20 20 20 2d 20 69 6e 64 65 78 20 77 69        - index wi
34a2c 74 68 69 6e 20 6c 65 76 65 6c 0a 2a 2a 20 20 20  thin level.**   
34a2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34a2e 20 20 20 20 2d 20 28 6c 65 76 65 6c 2c 69 64 78      - (level,idx
34a2f 20 75 6e 69 71 75 65 6c 79 20 69 64 65 6e 74 69   uniquely identi
34a30 66 79 20 61 20 73 65 67 6d 65 6e 74 29 0a 2a 2a  fy a segment).**
34a31 20 20 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20 20     start_block  
34a32 20 20 20 20 20 20 20 2d 20 66 69 72 73 74 20 6c         - first l
34a33 65 61 66 20 6e 6f 64 65 0a 2a 2a 20 20 20 6c 65  eaf node.**   le
34a34 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 20  aves_end_block  
34a35 20 20 2d 20 6c 61 73 74 20 6c 65 61 66 20 6e 6f    - last leaf no
34a36 64 65 0a 2a 2a 20 20 20 65 6e 64 5f 62 6c 6f 63  de.**   end_bloc
34a37 6b 20 20 20 20 20 20 20 20 20 20 20 2d 20 6c 61  k           - la
34a38 73 74 20 62 6c 6f 63 6b 20 28 69 6e 63 6c 75 64  st block (includ
34a39 69 6e 67 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  ing interior nod
34a3a 65 73 29 0a 2a 2a 20 20 20 72 6f 6f 74 20 20 20  es).**   root   
34a3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 63               - c
34a3c 6f 6e 74 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20  ontents of root 
34a3d 6e 6f 64 65 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  node.**.** If th
34a3e 65 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20 61  e root node is a
34a3f 20 6c 65 61 66 20 6e 6f 64 65 2c 20 74 68 65 6e   leaf node, then
34a40 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 0a 2a 2a   start_block,.**
34a41 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63   leaves_end_bloc
34a42 6b 2c 20 61 6e 64 20 65 6e 64 5f 62 6c 6f 63 6b  k, and end_block
34a43 20 61 72 65 20 61 6c 6c 20 30 2e 0a 2a 2a 0a 2a   are all 0..**.*
34a44 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 6d  *.**** Segment m
34a45 65 72 67 69 6e 67 20 2a 2a 2a 2a 0a 2a 2a 20 54  erging ****.** T
34a46 6f 20 61 6d 6f 72 74 69 7a 65 20 75 70 64 61 74  o amortize updat
34a47 65 20 63 6f 73 74 73 2c 20 73 65 67 6d 65 6e 74  e costs, segment
34a48 73 20 61 72 65 20 67 72 6f 75 70 65 64 20 69 6e  s are grouped in
34a49 74 6f 20 6c 65 76 65 6c 73 20 61 6e 64 0a 2a 2a  to levels and.**
34a4a 20 6d 65 72 67 65 64 20 69 6e 20 62 61 74 63 68   merged in batch
34a4b 65 73 2e 20 20 45 61 63 68 20 69 6e 63 72 65 61  es.  Each increa
34a4c 73 65 20 69 6e 20 6c 65 76 65 6c 20 72 65 70 72  se in level repr
34a4d 65 73 65 6e 74 73 20 65 78 70 6f 6e 65 6e 74 69  esents exponenti
34a4e 61 6c 6c 79 0a 2a 2a 20 6d 6f 72 65 20 64 6f 63  ally.** more doc
34a4f 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 65  uments..**.** Ne
34a50 77 20 64 6f 63 75 6d 65 6e 74 73 20 28 61 63 74  w documents (act
34a51 75 61 6c 6c 79 2c 20 64 6f 63 75 6d 65 6e 74 20  ually, document 
34a52 75 70 64 61 74 65 73 29 20 61 72 65 20 74 6f 6b  updates) are tok
34a53 65 6e 69 7a 65 64 20 61 6e 64 0a 2a 2a 20 77 72  enized and.** wr
34a54 69 74 74 65 6e 20 69 6e 64 69 76 69 64 75 61 6c  itten individual
34a55 6c 79 20 28 75 73 69 6e 67 20 4c 65 61 66 57 72  ly (using LeafWr
34a56 69 74 65 72 29 20 74 6f 20 61 20 6c 65 76 65 6c  iter) to a level
34a57 20 30 20 73 65 67 6d 65 6e 74 2c 20 77 69 74 68   0 segment, with
34a58 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67  .** incrementing
34a59 20 69 64 78 2e 20 20 57 68 65 6e 20 69 64 78 20   idx.  When idx 
34a5a 72 65 61 63 68 65 73 20 4d 45 52 47 45 5f 43 4f  reaches MERGE_CO
34a5b 55 4e 54 20 28 64 65 66 61 75 6c 74 20 31 36 29  UNT (default 16)
34a5c 2c 20 61 6c 6c 0a 2a 2a 20 6c 65 76 65 6c 20 30  , all.** level 0
34a5d 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20 6d 65   segments are me
34a5e 72 67 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67  rged into a sing
34a5f 6c 65 20 6c 65 76 65 6c 20 31 20 73 65 67 6d 65  le level 1 segme
34a60 6e 74 2e 20 20 4c 65 76 65 6c 20 31 0a 2a 2a 20  nt.  Level 1.** 
34a61 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6c 69 6b  is populated lik
34a62 65 20 6c 65 76 65 6c 20 30 2c 20 61 6e 64 20 65  e level 0, and e
34a63 76 65 6e 74 75 61 6c 6c 79 20 4d 45 52 47 45 5f  ventually MERGE_
34a64 43 4f 55 4e 54 20 6c 65 76 65 6c 20 31 0a 2a 2a  COUNT level 1.**
34a65 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20 6d 65   segments are me
34a66 72 67 65 64 20 74 6f 20 61 20 73 69 6e 67 6c 65  rged to a single
34a67 20 6c 65 76 65 6c 20 32 20 73 65 67 6d 65 6e 74   level 2 segment
34a68 20 28 72 65 70 72 65 73 65 6e 74 69 6e 67 0a 2a   (representing.*
34a69 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54 5e 32 20  * MERGE_COUNT^2 
34a6a 75 70 64 61 74 65 73 29 2c 20 61 6e 64 20 73 6f  updates), and so
34a6b 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 67   on..**.** A seg
34a6c 6d 65 6e 74 20 6d 65 72 67 65 20 74 72 61 76 65  ment merge trave
34a6d 72 73 65 73 20 61 6c 6c 20 73 65 67 6d 65 6e 74  rses all segment
34a6e 73 20 61 74 20 61 20 67 69 76 65 6e 20 6c 65 76  s at a given lev
34a6f 65 6c 20 69 6e 0a 2a 2a 20 70 61 72 61 6c 6c 65  el in.** paralle
34a70 6c 2c 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 20  l, performing a 
34a71 73 74 72 61 69 67 68 74 66 6f 72 77 61 72 64 20  straightforward 
34a72 73 6f 72 74 65 64 20 6d 65 72 67 65 2e 20 20 53  sorted merge.  S
34a73 69 6e 63 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20  ince segment.** 
34a74 6c 65 61 66 20 6e 6f 64 65 73 20 61 72 65 20 77  leaf nodes are w
34a75 72 69 74 74 65 6e 20 69 6e 20 74 6f 20 74 68 65  ritten in to the
34a76 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c   %_segments tabl
34a77 65 20 69 6e 20 6f 72 64 65 72 2c 20 74 68 69 73  e in order, this
34a78 0a 2a 2a 20 6d 65 72 67 65 20 74 72 61 76 65 72  .** merge traver
34a79 73 65 73 20 74 68 65 20 75 6e 64 65 72 6c 79 69  ses the underlyi
34a7a 6e 67 20 73 71 6c 69 74 65 20 64 69 73 6b 20 73  ng sqlite disk s
34a7b 74 72 75 63 74 75 72 65 73 20 65 66 66 69 63 69  tructures effici
34a7c 65 6e 74 6c 79 2e 0a 2a 2a 20 41 66 74 65 72 20  ently..** After 
34a7d 74 68 65 20 6d 65 72 67 65 2c 20 61 6c 6c 20 73  the merge, all s
34a7e 65 67 6d 65 6e 74 20 62 6c 6f 63 6b 73 20 66 72  egment blocks fr
34a7f 6f 6d 20 74 68 65 20 6d 65 72 67 65 64 20 6c 65  om the merged le
34a80 76 65 6c 20 61 72 65 0a 2a 2a 20 64 65 6c 65 74  vel are.** delet
34a81 65 64 2e 0a 2a 2a 0a 2a 2a 20 4d 45 52 47 45 5f  ed..**.** MERGE_
34a82 43 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20 68  COUNT controls h
34a83 6f 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72 67  ow often we merg
34a84 65 20 73 65 67 6d 65 6e 74 73 2e 20 20 31 36 20  e segments.  16 
34a85 73 65 65 6d 73 20 74 6f 20 62 65 0a 2a 2a 20 73  seems to be.** s
34a86 6f 6d 65 77 68 61 74 20 6f 66 20 61 20 73 77 65  omewhat of a swe
34a87 65 74 20 73 70 6f 74 20 66 6f 72 20 69 6e 73 65  et spot for inse
34a88 72 74 69 6f 6e 20 70 65 72 66 6f 72 6d 61 6e 63  rtion performanc
34a89 65 2e 20 20 33 32 20 61 6e 64 20 36 34 20 73 68  e.  32 and 64 sh
34a8a 6f 77 0a 2a 2a 20 76 65 72 79 20 73 69 6d 69 6c  ow.** very simil
34a8b 61 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6e  ar performance n
34a8c 75 6d 62 65 72 73 20 74 6f 20 31 36 20 6f 6e 20  umbers to 16 on 
34a8d 69 6e 73 65 72 74 69 6f 6e 2c 20 74 68 6f 75 67  insertion, thoug
34a8e 68 20 74 68 65 79 27 72 65 0a 2a 2a 20 61 20 74  h they're.** a t
34a8f 69 6e 79 20 62 69 74 20 73 6c 6f 77 65 72 20 28  iny bit slower (
34a90 70 65 72 68 61 70 73 20 64 75 65 20 74 6f 20 6d  perhaps due to m
34a91 6f 72 65 20 6f 76 65 72 68 65 61 64 20 69 6e 20  ore overhead in 
34a92 6d 65 72 67 65 2d 74 69 6d 65 0a 2a 2a 20 73 6f  merge-time.** so
34a93 72 74 69 6e 67 29 2e 20 20 38 20 69 73 20 61 62  rting).  8 is ab
34a94 6f 75 74 20 32 30 25 20 73 6c 6f 77 65 72 20 74  out 20% slower t
34a95 68 61 6e 20 31 36 2c 20 34 20 61 62 6f 75 74 20  han 16, 4 about 
34a96 35 30 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 0a  50% slower than.
34a97 2a 2a 20 31 36 2c 20 32 20 61 62 6f 75 74 20 36  ** 16, 2 about 6
34a98 36 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 31  6% slower than 1
34a99 36 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 71 75 65 72  6..**.** At quer
34a9a 79 20 74 69 6d 65 2c 20 68 69 67 68 20 4d 45 52  y time, high MER
34a9b 47 45 5f 43 4f 55 4e 54 20 69 6e 63 72 65 61 73  GE_COUNT increas
34a9c 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  es the number of
34a9d 20 73 65 67 6d 65 6e 74 73 0a 2a 2a 20 77 68 69   segments.** whi
34a9e 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 73 63  ch need to be sc
34a9f 61 6e 6e 65 64 20 61 6e 64 20 6d 65 72 67 65 64  anned and merged
34aa0 2e 20 20 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c  .  For instance,
34aa1 20 77 69 74 68 20 31 30 30 6b 20 64 6f 63 73 0a   with 100k docs.
34aa2 2a 2a 20 69 6e 73 65 72 74 65 64 3a 0a 2a 2a 0a  ** inserted:.**.
34aa3 2a 2a 20 20 20 20 4d 45 52 47 45 5f 43 4f 55 4e  **    MERGE_COUN
34aa4 54 20 20 20 73 65 67 6d 65 6e 74 73 0a 2a 2a 20  T   segments.** 
34aa5 20 20 20 20 20 20 31 36 20 20 20 20 20 20 20 20        16        
34aa6 20 20 20 32 35 0a 2a 2a 20 20 20 20 20 20 20 20     25.**        
34aa7 38 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 2a  8           12.*
34aa8 2a 20 20 20 20 20 20 20 20 34 20 20 20 20 20 20  *        4      
34aa9 20 20 20 20 20 31 30 0a 2a 2a 20 20 20 20 20 20       10.**      
34aaa 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 36    2            6
34aab 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 70 70 65  .**.** This appe
34aac 61 72 73 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79  ars to have only
34aad 20 61 20 6d 6f 64 65 72 61 74 65 20 69 6d 70 61   a moderate impa
34aae 63 74 20 6f 6e 20 71 75 65 72 69 65 73 20 66 6f  ct on queries fo
34aaf 72 20 76 65 72 79 0a 2a 2a 20 66 72 65 71 75 65  r very.** freque
34ab0 6e 74 20 74 65 72 6d 73 20 28 77 68 69 63 68 20  nt terms (which 
34ab1 61 72 65 20 73 6f 6d 65 77 68 61 74 20 64 6f 6d  are somewhat dom
34ab2 69 6e 61 74 65 64 20 62 79 20 73 65 67 6d 65 6e  inated by segmen
34ab3 74 20 6d 65 72 67 65 0a 2a 2a 20 63 6f 73 74 73  t merge.** costs
34ab4 29 2c 20 61 6e 64 20 69 6e 66 72 65 71 75 65 6e  ), and infrequen
34ab5 74 20 61 6e 64 20 6e 6f 6e 2d 65 78 69 73 74 65  t and non-existe
34ab6 6e 74 20 74 65 72 6d 73 20 73 74 69 6c 6c 20 73  nt terms still s
34ab7 65 65 6d 20 74 6f 20 62 65 20 66 61 73 74 0a 2a  eem to be fast.*
34ab8 2a 20 65 76 65 6e 20 77 69 74 68 20 6d 61 6e 79  * even with many
34ab9 20 73 65 67 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a   segments..**.**
34aba 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 61   TODO(shess) Tha
34abb 74 20 73 61 69 64 2c 20 69 74 20 77 6f 75 6c 64  t said, it would
34abc 20 62 65 20 6e 69 63 65 20 74 6f 20 68 61 76 65   be nice to have
34abd 20 61 20 62 65 74 74 65 72 20 71 75 65 72 79 2d   a better query-
34abe 73 69 64 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  side.** argument
34abf 20 66 6f 72 20 4d 45 52 47 45 5f 43 4f 55 4e 54   for MERGE_COUNT
34ac0 20 6f 66 20 31 36 2e 20 20 41 6c 73 6f 2c 20 69   of 16.  Also, i
34ac1 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2f 6c 69  t is possible/li
34ac2 6b 65 6c 79 20 74 68 61 74 0a 2a 2a 20 6f 70 74  kely that.** opt
34ac3 69 6d 69 7a 61 74 69 6f 6e 73 20 74 6f 20 74 68  imizations to th
34ac4 69 6e 67 73 20 6c 69 6b 65 20 64 6f 63 6c 69 73  ings like doclis
34ac5 74 20 6d 65 72 67 69 6e 67 20 77 69 6c 6c 20 73  t merging will s
34ac6 77 69 6e 67 20 74 68 65 20 73 77 65 65 74 0a 2a  wing the sweet.*
34ac7 2a 20 73 70 6f 74 20 61 72 6f 75 6e 64 2e 0a 2a  * spot around..*
34ac8 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 48 61 6e  *.**.**.**** Han
34ac9 64 6c 69 6e 67 20 6f 66 20 64 65 6c 65 74 69 6f  dling of deletio
34aca 6e 73 20 61 6e 64 20 75 70 64 61 74 65 73 20 2a  ns and updates *
34acb 2a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 77 65 27  ***.** Since we'
34acc 72 65 20 75 73 69 6e 67 20 61 20 73 65 67 6d 65  re using a segme
34acd 6e 74 65 64 20 73 74 72 75 63 74 75 72 65 2c 20  nted structure, 
34ace 77 69 74 68 20 6e 6f 20 64 6f 63 69 64 2d 6f 72  with no docid-or
34acf 69 65 6e 74 65 64 0a 2a 2a 20 69 6e 64 65 78 20  iented.** index 
34ad0 69 6e 74 6f 20 74 68 65 20 74 65 72 6d 20 69 6e  into the term in
34ad1 64 65 78 2c 20 77 65 20 63 6c 65 61 72 6c 79 20  dex, we clearly 
34ad2 63 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 75 70  cannot simply up
34ad3 64 61 74 65 20 74 68 65 20 74 65 72 6d 0a 2a 2a  date the term.**
34ad4 20 69 6e 64 65 78 20 77 68 65 6e 20 61 20 64 6f   index when a do
34ad5 63 75 6d 65 6e 74 20 69 73 20 64 65 6c 65 74 65  cument is delete
34ad6 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 46  d or updated.  F
34ad7 6f 72 20 64 65 6c 65 74 69 6f 6e 73 2c 20 77 65  or deletions, we
34ad8 0a 2a 2a 20 77 72 69 74 65 20 61 6e 20 65 6d 70  .** write an emp
34ad9 74 79 20 64 6f 63 6c 69 73 74 20 28 76 61 72 69  ty doclist (vari
34ada 6e 74 28 64 6f 63 69 64 29 20 76 61 72 69 6e 74  nt(docid) varint
34adb 28 50 4f 53 5f 45 4e 44 29 29 2c 20 66 6f 72 20  (POS_END)), for 
34adc 75 70 64 61 74 65 73 0a 2a 2a 20 77 65 20 73 69  updates.** we si
34add 6d 70 6c 79 20 77 72 69 74 65 20 74 68 65 20 6e  mply write the n
34ade 65 77 20 64 6f 63 6c 69 73 74 2e 20 20 53 65 67  ew doclist.  Seg
34adf 6d 65 6e 74 20 6d 65 72 67 65 73 20 6f 76 65 72  ment merges over
34ae0 77 72 69 74 65 20 6f 6c 64 65 72 0a 2a 2a 20 64  write older.** d
34ae1 61 74 61 20 66 6f 72 20 61 20 70 61 72 74 69 63  ata for a partic
34ae2 75 6c 61 72 20 64 6f 63 69 64 20 77 69 74 68 20  ular docid with 
34ae3 6e 65 77 65 72 20 64 61 74 61 2c 20 73 6f 20 64  newer data, so d
34ae4 65 6c 65 74 65 73 20 6f 72 20 75 70 64 61 74 65  eletes or update
34ae5 73 0a 2a 2a 20 77 69 6c 6c 20 65 76 65 6e 74 75  s.** will eventu
34ae6 61 6c 6c 79 20 6f 76 65 72 74 61 6b 65 20 74 68  ally overtake th
34ae7 65 20 65 61 72 6c 69 65 72 20 64 61 74 61 20 61  e earlier data a
34ae8 6e 64 20 6b 6e 6f 63 6b 20 69 74 20 6f 75 74 2e  nd knock it out.
34ae9 20 20 54 68 65 0a 2a 2a 20 71 75 65 72 79 20 6c    The.** query l
34aea 6f 67 69 63 20 6c 69 6b 65 77 69 73 65 20 6d 65  ogic likewise me
34aeb 72 67 65 73 20 64 6f 63 6c 69 73 74 73 20 73 6f  rges doclists so
34aec 20 74 68 61 74 20 6e 65 77 65 72 20 64 61 74 61   that newer data
34aed 20 6b 6e 6f 63 6b 73 20 6f 75 74 0a 2a 2a 20 6f   knocks out.** o
34aee 6c 64 65 72 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a  lder data..**.**
34aef 20 54 4f 44 4f 28 73 68 65 73 73 29 20 50 72 6f   TODO(shess) Pro
34af0 76 69 64 65 20 61 20 56 41 43 55 55 4d 20 74 79  vide a VACUUM ty
34af1 70 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  pe operation to 
34af2 63 6c 65 61 72 20 6f 75 74 20 61 6c 6c 0a 2a 2a  clear out all.**
34af3 20 64 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20 64   deletions and d
34af4 75 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 54 68  uplications.  Th
34af5 69 73 20 77 6f 75 6c 64 20 62 61 73 69 63 61 6c  is would basical
34af6 6c 79 20 62 65 20 61 20 66 6f 72 63 65 64 20 6d  ly be a forced m
34af7 65 72 67 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 73  erge.** into a s
34af8 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74 2e 0a 2a  ingle segment..*
34af9 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
34afa 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
34afb 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
34afc 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 23 69 66  NABLE_FTS3)..#if
34afd 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
34afe 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 26 26 20  ENABLE_FTS3) && 
34aff 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
34b00 43 4f 52 45 29 0a 23 20 64 65 66 69 6e 65 20 53  CORE).# define S
34b01 51 4c 49 54 45 5f 43 4f 52 45 20 31 0a 23 65 6e  QLITE_CORE 1.#en
34b02 64 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif.../*********
34b03 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74  ***** Include ft
34b04 73 33 5f 65 78 70 72 2e 68 20 69 6e 20 74 68 65  s3_expr.h in the
34b05 20 6d 69 64 64 6c 65 20 6f 66 20 66 74 73 33 2e   middle of fts3.
34b06 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
34b07 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
34b08 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
34b09 20 66 74 73 33 5f 65 78 70 72 2e 68 20 2a 2a 2a   fts3_expr.h ***
34b0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b0c 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
34b0d 20 4e 6f 76 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68   Nov 28.**.** Th
34b0e 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
34b0f 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
34b10 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
34b11 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
34b12 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
34b13 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
34b14 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
34b15 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
34b16 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
34b17 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
34b18 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
34b19 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
34b1a 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
34b1b 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
34b1c 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
34b1d 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
34b1e 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
34b1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
34b24 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
34b25 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73 33 5f  ** Include fts3_
34b26 74 6f 6b 65 6e 69 7a 65 72 2e 68 20 69 6e 20 74  tokenizer.h in t
34b27 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 66 74 73  he middle of fts
34b28 33 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a  3_expr.h *******
34b29 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
34b2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74  ** Begin file ft
34b2b 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 68 20 2a  s3_tokenizer.h *
34b2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b2e 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75  */./*.** 2006 Ju
34b2f 6c 79 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ly 10.**.** The 
34b30 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
34b31 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
34b32 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 0a  is source code..
34b33 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
34b34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
34b38 44 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65  Defines the inte
34b39 72 66 61 63 65 20 74 6f 20 74 6f 6b 65 6e 69 7a  rface to tokeniz
34b3a 65 72 73 20 75 73 65 64 20 62 79 20 66 75 6c 6c  ers used by full
34b3b 74 65 78 74 2d 73 65 61 72 63 68 2e 20 20 54 68  text-search.  Th
34b3c 65 72 65 0a 2a 2a 20 61 72 65 20 74 68 72 65 65  ere.** are three
34b3d 20 62 61 73 69 63 20 63 6f 6d 70 6f 6e 65 6e 74   basic component
34b3e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33  s:.**.** sqlite3
34b3f 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
34b40 65 20 69 73 20 61 20 73 69 6e 67 6c 65 74 6f 6e  e is a singleton
34b41 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 74 6f   defining the to
34b42 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6e 74 65 72  kenizer.** inter
34b43 66 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20  face functions. 
34b44 20 54 68 69 73 20 69 73 20 65 73 73 65 6e 74 69   This is essenti
34b45 61 6c 6c 79 20 74 68 65 20 63 6c 61 73 73 20 73  ally the class s
34b46 74 72 75 63 74 75 72 65 20 66 6f 72 0a 2a 2a 20  tructure for.** 
34b47 74 6f 6b 65 6e 69 7a 65 72 73 2e 0a 2a 2a 0a 2a  tokenizers..**.*
34b48 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  * sqlite3_tokeni
34b49 7a 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 64  zer is used to d
34b4a 65 66 69 6e 65 20 61 20 70 61 72 74 69 63 75 6c  efine a particul
34b4b 61 72 20 74 6f 6b 65 6e 69 7a 65 72 2c 20 70 65  ar tokenizer, pe
34b4c 72 68 61 70 73 0a 2a 2a 20 69 6e 63 6c 75 64 69  rhaps.** includi
34b4d 6e 67 20 63 75 73 74 6f 6d 69 7a 61 74 69 6f 6e  ng customization
34b4e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 64 65 66   information def
34b4f 69 6e 65 64 20 61 74 20 63 72 65 61 74 69 6f 6e  ined at creation
34b50 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c   time..**.** sql
34b51 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
34b52 75 72 73 6f 72 20 69 73 20 67 65 6e 65 72 61 74  ursor is generat
34b53 65 64 20 62 79 20 61 20 74 6f 6b 65 6e 69 7a 65  ed by a tokenize
34b54 72 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a  r to generate.**
34b55 20 74 6f 6b 65 6e 73 20 66 72 6f 6d 20 61 20 70   tokens from a p
34b56 61 72 74 69 63 75 6c 61 72 20 69 6e 70 75 74 2e  articular input.
34b57 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 46 54 53  .*/.#ifndef _FTS
34b58 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48 5f 0a 23  3_TOKENIZER_H_.#
34b59 64 65 66 69 6e 65 20 5f 46 54 53 33 5f 54 4f 4b  define _FTS3_TOK
34b5a 45 4e 49 5a 45 52 5f 48 5f 0a 0a 2f 2a 20 54 4f  ENIZER_H_../* TO
34b5b 44 4f 28 73 68 65 73 73 29 20 4f 6e 6c 79 20 75  DO(shess) Only u
34b5c 73 65 64 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f  sed for SQLITE_O
34b5d 4b 20 61 6e 64 20 53 51 4c 49 54 45 5f 44 4f 4e  K and SQLITE_DON
34b5e 45 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a  E at this time..
34b5f 2a 2a 20 49 66 20 74 6f 6b 65 6e 69 7a 65 72 73  ** If tokenizers
34b60 20 61 72 65 20 74 6f 20 62 65 20 61 6c 6c 6f 77   are to be allow
34b61 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74  ed to call sqlit
34b62 65 33 5f 2a 28 29 20 66 75 6e 63 74 69 6f 6e 73  e3_*() functions
34b63 2c 20 74 68 65 6e 0a 2a 2a 20 77 65 20 77 69 6c  , then.** we wil
34b64 6c 20 6e 65 65 64 20 61 20 77 61 79 20 74 6f 20  l need a way to 
34b65 72 65 67 69 73 74 65 72 20 74 68 65 20 41 50 49  register the API
34b66 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a   consistently..*
34b67 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75  /../*.** Structu
34b68 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  res used by the 
34b69 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 74 65 72 66  tokenizer interf
34b6a 61 63 65 2e 20 57 68 65 6e 20 61 20 6e 65 77 20  ace. When a new 
34b6b 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6d 70  tokenizer.** imp
34b6c 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 72  lementation is r
34b6d 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 63  egistered, the c
34b6e 61 6c 6c 65 72 20 70 72 6f 76 69 64 65 73 20 61  aller provides a
34b6f 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61   pointer to.** a
34b70 6e 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  n sqlite3_tokeni
34b71 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 74 61  zer_module conta
34b72 69 6e 69 6e 67 20 70 6f 69 6e 74 65 72 73 20 74  ining pointers t
34b73 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a  o the callback.*
34b74 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  * functions that
34b75 20 6d 61 6b 65 20 75 70 20 61 6e 20 69 6d 70 6c   make up an impl
34b76 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  ementation..**.*
34b77 2a 20 57 68 65 6e 20 61 6e 20 66 74 73 33 20 74  * When an fts3 t
34b78 61 62 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c  able is created,
34b79 20 69 74 20 70 61 73 73 65 73 20 61 6e 79 20 61   it passes any a
34b7a 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20  rguments passed 
34b7b 74 6f 0a 2a 2a 20 74 68 65 20 74 6f 6b 65 6e 69  to.** the tokeni
34b7c 7a 65 72 20 63 6c 61 75 73 65 20 6f 66 20 74 68  zer clause of th
34b7d 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c  e CREATE VIRTUAL
34b7e 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
34b7f 20 74 6f 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74   to the.** sqlit
34b80 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
34b81 75 6c 65 2e 78 43 72 65 61 74 65 28 29 20 66 75  ule.xCreate() fu
34b82 6e 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 65  nction of the re
34b83 71 75 65 73 74 65 64 20 74 6f 6b 65 6e 69 7a 65  quested tokenize
34b84 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  r.** implementat
34b85 69 6f 6e 2e 20 54 68 65 20 78 43 72 65 61 74 65  ion. The xCreate
34b86 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 74  () function in t
34b87 75 72 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 0a  urn returns an .
34b88 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ** sqlite3_token
34b89 69 7a 65 72 20 73 74 72 75 63 74 75 72 65 20 72  izer structure r
34b8a 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
34b8b 73 70 65 63 69 66 69 63 20 74 6f 6b 65 6e 69 7a  specific tokeniz
34b8c 65 72 20 74 6f 0a 2a 2a 20 62 65 20 75 73 65 64  er to.** be used
34b8d 20 66 6f 72 20 74 68 65 20 66 74 73 33 20 74 61   for the fts3 ta
34b8e 62 6c 65 20 28 63 75 73 74 6f 6d 69 7a 65 64 20  ble (customized 
34b8f 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  by the tokenizer
34b90 20 63 6c 61 75 73 65 20 61 72 67 75 6d 65 6e 74   clause argument
34b91 73 29 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 74 6f 6b  s)..**.** To tok
34b92 65 6e 69 7a 65 20 61 6e 20 69 6e 70 75 74 20 62  enize an input b
34b93 75 66 66 65 72 2c 20 74 68 65 20 73 71 6c 69 74  uffer, the sqlit
34b94 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
34b95 75 6c 65 2e 78 4f 70 65 6e 28 29 0a 2a 2a 20 6d  ule.xOpen().** m
34b96 65 74 68 6f 64 20 69 73 20 63 61 6c 6c 65 64 2e  ethod is called.
34b97 20 49 74 20 72 65 74 75 72 6e 73 20 61 6e 20 73   It returns an s
34b98 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34b99 5f 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 0a 2a  _cursor object.*
34b9a 2a 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73  * that may be us
34b9b 65 64 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 20 61  ed to tokenize a
34b9c 20 73 70 65 63 69 66 69 63 20 69 6e 70 75 74 20   specific input 
34b9d 62 75 66 66 65 72 20 62 61 73 65 64 20 6f 6e 0a  buffer based on.
34b9e 2a 2a 20 74 68 65 20 74 6f 6b 65 6e 69 7a 61 74  ** the tokenizat
34b9f 69 6f 6e 20 72 75 6c 65 73 20 73 75 70 70 6c 69  ion rules suppli
34ba0 65 64 20 62 79 20 61 20 73 70 65 63 69 66 69 63  ed by a specific
34ba1 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
34ba2 65 72 0a 2a 2a 20 6f 62 6a 65 63 74 2e 0a 2a 2f  er.** object..*/
34ba3 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
34ba4 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
34ba5 72 5f 6d 6f 64 75 6c 65 20 73 71 6c 69 74 65 33  r_module sqlite3
34ba6 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
34ba7 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
34ba8 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
34ba9 7a 65 72 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  zer sqlite3_toke
34baa 6e 69 7a 65 72 3b 0a 74 79 70 65 64 65 66 20 73  nizer;.typedef s
34bab 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f  truct sqlite3_to
34bac 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 73  kenizer_cursor s
34bad 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34bae 5f 63 75 72 73 6f 72 3b 0a 0a 73 74 72 75 63 74  _cursor;..struct
34baf 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
34bb0 65 72 5f 6d 6f 64 75 6c 65 20 7b 0a 0a 20 20 2f  er_module {..  /
34bb1 2a 0a 20 20 2a 2a 20 53 74 72 75 63 74 75 72 65  *.  ** Structure
34bb2 20 76 65 72 73 69 6f 6e 2e 20 53 68 6f 75 6c 64   version. Should
34bb3 20 61 6c 77 61 79 73 20 62 65 20 73 65 74 20 74   always be set t
34bb4 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  o 0..  */.  int 
34bb5 69 56 65 72 73 69 6f 6e 3b 0a 0a 20 20 2f 2a 0a  iVersion;..  /*.
34bb6 20 20 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65    ** Create a ne
34bb7 77 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 54 68 65  w tokenizer. The
34bb8 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 61   values in the a
34bb9 72 67 76 5b 5d 20 61 72 72 61 79 20 61 72 65 20  rgv[] array are 
34bba 74 68 65 0a 20 20 2a 2a 20 61 72 67 75 6d 65 6e  the.  ** argumen
34bbb 74 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65  ts passed to the
34bbc 20 22 74 6f 6b 65 6e 69 7a 65 72 22 20 63 6c 61   "tokenizer" cla
34bbd 75 73 65 20 6f 66 20 74 68 65 20 43 52 45 41 54  use of the CREAT
34bbe 45 20 56 49 52 54 55 41 4c 0a 20 20 2a 2a 20 54  E VIRTUAL.  ** T
34bbf 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 74  ABLE statement t
34bc0 68 61 74 20 63 72 65 61 74 65 64 20 74 68 65 20  hat created the 
34bc1 66 74 73 33 20 74 61 62 6c 65 2e 20 46 6f 72 20  fts3 table. For 
34bc2 65 78 61 6d 70 6c 65 2c 20 69 66 0a 20 20 2a 2a  example, if.  **
34bc3 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53   the following S
34bc4 51 4c 20 69 73 20 65 78 65 63 75 74 65 64 3a 0a  QL is executed:.
34bc5 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 43 52 45 41    **.  **   CREA
34bc6 54 45 20 2e 2e 20 55 53 49 4e 47 20 66 74 73 33  TE .. USING fts3
34bc7 28 20 2e 2e 2e 20 2c 20 74 6f 6b 65 6e 69 7a 65  ( ... , tokenize
34bc8 72 20 3c 74 6f 6b 65 6e 69 7a 65 72 2d 6e 61 6d  r <tokenizer-nam
34bc9 65 3e 20 61 72 67 31 20 61 72 67 32 29 0a 20 20  e> arg1 arg2).  
34bca 2a 2a 0a 20 20 2a 2a 20 74 68 65 6e 20 61 72 67  **.  ** then arg
34bcb 63 20 69 73 20 73 65 74 20 74 6f 20 32 2c 20 61  c is set to 2, a
34bcc 6e 64 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72  nd the argv[] ar
34bcd 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69  ray contains poi
34bce 6e 74 65 72 73 0a 20 20 2a 2a 20 74 6f 20 74 68  nters.  ** to th
34bcf 65 20 73 74 72 69 6e 67 73 20 22 61 72 67 31 22  e strings "arg1"
34bd0 20 61 6e 64 20 22 61 72 67 32 22 2e 0a 20 20 2a   and "arg2"..  *
34bd1 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6d 65 74 68  *.  ** This meth
34bd2 6f 64 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e  od should return
34bd3 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f   either SQLITE_O
34bd4 4b 20 28 30 29 2c 20 6f 72 20 61 6e 20 53 51 4c  K (0), or an SQL
34bd5 69 74 65 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20  ite error .  ** 
34bd6 63 6f 64 65 2e 20 49 66 20 53 51 4c 49 54 45 5f  code. If SQLITE_
34bd7 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  OK is returned, 
34bd8 74 68 65 6e 20 2a 70 70 54 6f 6b 65 6e 69 7a 65  then *ppTokenize
34bd9 72 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 0a  r should be set.
34bda 20 20 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 61 74    ** to point at
34bdb 20 74 68 65 20 6e 65 77 6c 79 20 63 72 65 61 74   the newly creat
34bdc 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 73 74 72  ed tokenizer str
34bdd 75 63 74 75 72 65 2e 20 54 68 65 20 67 65 6e 65  ucture. The gene
34bde 72 69 63 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  ric.  ** sqlite3
34bdf 5f 74 6f 6b 65 6e 69 7a 65 72 2e 70 4d 6f 64 75  _tokenizer.pModu
34be0 6c 65 20 76 61 72 69 61 62 6c 65 20 73 68 6f 75  le variable shou
34be1 6c 64 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61  ld not be initia
34be2 6c 69 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68  lised by.  ** th
34be3 69 73 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 65  is callback. The
34be4 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 64 6f 20   caller will do 
34be5 73 6f 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28  so..  */.  int (
34be6 2a 78 43 72 65 61 74 65 29 28 0a 20 20 20 20 69  *xCreate)(.    i
34be7 6e 74 20 61 72 67 63 2c 20 20 20 20 20 20 20 20  nt argc,        
34be8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34be9 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72     /* Size of ar
34bea 67 76 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20  gv array */.    
34beb 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
34bec 74 2a 61 72 67 76 2c 20 20 20 20 20 20 20 20 20  t*argv,         
34bed 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
34bee 20 61 72 67 75 6d 65 6e 74 20 73 74 72 69 6e 67   argument string
34bef 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  s */.    sqlite3
34bf0 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54  _tokenizer **ppT
34bf1 6f 6b 65 6e 69 7a 65 72 20 20 20 20 20 2f 2a 20  okenizer     /* 
34bf2 4f 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b  OUT: Created tok
34bf3 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 29 3b 0a 0a  enizer */.  );..
34bf4 20 20 2f 2a 0a 20 20 2a 2a 20 44 65 73 74 72 6f    /*.  ** Destro
34bf5 79 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 6f  y an existing to
34bf6 6b 65 6e 69 7a 65 72 2e 20 54 68 65 20 66 74 73  kenizer. The fts
34bf7 33 20 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20 74  3 module calls t
34bf8 68 69 73 20 6d 65 74 68 6f 64 0a 20 20 2a 2a 20  his method.  ** 
34bf9 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72  exactly once for
34bfa 20 65 61 63 68 20 73 75 63 63 65 73 73 66 75 6c   each successful
34bfb 20 63 61 6c 6c 20 74 6f 20 78 43 72 65 61 74 65   call to xCreate
34bfc 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28  ()..  */.  int (
34bfd 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74  *xDestroy)(sqlit
34bfe 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
34bff 6f 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20 20 2f 2a  okenizer);..  /*
34c00 0a 20 20 2a 2a 20 43 72 65 61 74 65 20 61 20 74  .  ** Create a t
34c01 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20  okenizer cursor 
34c02 74 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 6e 20 69  to tokenize an i
34c03 6e 70 75 74 20 62 75 66 66 65 72 2e 20 54 68 65  nput buffer. The
34c04 20 63 61 6c 6c 65 72 0a 20 20 2a 2a 20 69 73 20   caller.  ** is 
34c05 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
34c06 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68  ensuring that th
34c07 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 72  e input buffer r
34c08 65 6d 61 69 6e 73 20 76 61 6c 69 64 0a 20 20 2a  emains valid.  *
34c09 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 75 72 73  * until the curs
34c0a 6f 72 20 69 73 20 63 6c 6f 73 65 64 20 28 75 73  or is closed (us
34c0b 69 6e 67 20 74 68 65 20 78 43 6c 6f 73 65 28 29  ing the xClose()
34c0c 20 6d 65 74 68 6f 64 29 2e 20 0a 20 20 2a 2f 0a   method). .  */.
34c0d 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 0a    int (*xOpen)(.
34c0e 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65      sqlite3_toke
34c0f 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
34c10 72 2c 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  r,       /* Toke
34c11 6e 69 7a 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  nizer object */.
34c12 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
34c13 70 49 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74  pInput, int nByt
34c14 65 73 2c 20 20 20 20 20 20 2f 2a 20 49 6e 70 75  es,      /* Inpu
34c15 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20  t buffer */.    
34c16 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
34c17 72 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72  r_cursor **ppCur
34c18 73 6f 72 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65  sor  /* OUT: Cre
34c19 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 63  ated tokenizer c
34c1a 75 72 73 6f 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20  ursor */.  );.. 
34c1b 20 2f 2a 0a 20 20 2a 2a 20 44 65 73 74 72 6f 79   /*.  ** Destroy
34c1c 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 6f 6b   an existing tok
34c1d 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 2e 20 54  enizer cursor. T
34c1e 68 65 20 66 74 73 33 20 6d 6f 64 75 6c 65 20 63  he fts3 module c
34c1f 61 6c 6c 73 20 74 68 69 73 20 0a 20 20 2a 2a 20  alls this .  ** 
34c20 6d 65 74 68 6f 64 20 65 78 61 63 74 6c 79 20 6f  method exactly o
34c21 6e 63 65 20 66 6f 72 20 65 61 63 68 20 73 75 63  nce for each suc
34c22 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20  cessful call to 
34c23 78 4f 70 65 6e 28 29 2e 0a 20 20 2a 2f 0a 20 20  xOpen()..  */.  
34c24 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 73 71  int (*xClose)(sq
34c25 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
34c26 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  cursor *pCursor)
34c27 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74  ;..  /*.  ** Ret
34c28 72 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 74  rieve the next t
34c29 6f 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 74 6f  oken from the to
34c2a 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 70  kenizer cursor p
34c2b 43 75 72 73 6f 72 2e 20 54 68 69 73 0a 20 20 2a  Cursor. This.  *
34c2c 2a 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20  * method should 
34c2d 65 69 74 68 65 72 20 72 65 74 75 72 6e 20 53 51  either return SQ
34c2e 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 73 65 74 20  LITE_OK and set 
34c2f 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68  the values of th
34c30 65 0a 20 20 2a 2a 20 22 4f 55 54 22 20 76 61 72  e.  ** "OUT" var
34c31 69 61 62 6c 65 73 20 69 64 65 6e 74 69 66 69 65  iables identifie
34c32 64 20 62 65 6c 6f 77 2c 20 6f 72 20 53 51 4c 49  d below, or SQLI
34c33 54 45 5f 44 4f 4e 45 20 74 6f 20 69 6e 64 69 63  TE_DONE to indic
34c34 61 74 65 20 74 68 61 74 0a 20 20 2a 2a 20 74 68  ate that.  ** th
34c35 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66  e end of the buf
34c36 66 65 72 20 68 61 73 20 62 65 65 6e 20 72 65 61  fer has been rea
34c37 63 68 65 64 2c 20 6f 72 20 61 6e 20 53 51 4c 69  ched, or an SQLi
34c38 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 20  te error code.. 
34c39 20 2a 2a 0a 20 20 2a 2a 20 2a 70 70 54 6f 6b 65   **.  ** *ppToke
34c3a 6e 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20  n should be set 
34c3b 74 6f 20 70 6f 69 6e 74 20 61 74 20 61 20 62 75  to point at a bu
34c3c 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
34c3d 74 68 65 20 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c  the .  ** normal
34c3e 69 7a 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  ized version of 
34c3f 74 68 65 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20  the token (i.e. 
34c40 61 66 74 65 72 20 61 6e 79 20 63 61 73 65 2d 66  after any case-f
34c41 6f 6c 64 69 6e 67 20 61 6e 64 2f 6f 72 0a 20 20  olding and/or.  
34c42 2a 2a 20 73 74 65 6d 6d 69 6e 67 20 68 61 73 20  ** stemming has 
34c43 62 65 65 6e 20 70 65 72 66 6f 72 6d 65 64 29 2e  been performed).
34c44 20 2a 70 6e 42 79 74 65 73 20 73 68 6f 75 6c 64   *pnBytes should
34c45 20 62 65 20 73 65 74 20 74 6f 20 74 68 65 20 6c   be set to the l
34c46 65 6e 67 74 68 0a 20 20 2a 2a 20 6f 66 20 74 68  ength.  ** of th
34c47 69 73 20 62 75 66 66 65 72 20 69 6e 20 62 79 74  is buffer in byt
34c48 65 73 2e 20 54 68 65 20 69 6e 70 75 74 20 74 65  es. The input te
34c49 78 74 20 74 68 61 74 20 67 65 6e 65 72 61 74 65  xt that generate
34c4a 64 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 0a 20  d the token is. 
34c4b 20 2a 2a 20 69 64 65 6e 74 69 66 69 65 64 20 62   ** identified b
34c4c 79 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65  y the byte offse
34c4d 74 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a  ts returned in *
34c4e 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 61 6e  piStartOffset an
34c4f 64 0a 20 20 2a 2a 20 2a 70 69 45 6e 64 4f 66 66  d.  ** *piEndOff
34c50 73 65 74 2e 20 2a 70 69 53 74 61 72 74 4f 66 66  set. *piStartOff
34c51 73 65 74 20 73 68 6f 75 6c 64 20 62 65 20 73 65  set should be se
34c52 74 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f  t to the index o
34c53 66 20 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a  f the first.  **
34c54 20 62 79 74 65 20 6f 66 20 74 68 65 20 74 6f 6b   byte of the tok
34c55 65 6e 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20  en in the input 
34c56 62 75 66 66 65 72 2e 20 2a 70 69 45 6e 64 4f 66  buffer. *piEndOf
34c57 66 73 65 74 20 73 68 6f 75 6c 64 20 62 65 20 73  fset should be s
34c58 65 74 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 69  et.  ** to the i
34c59 6e 64 65 78 20 6f 66 20 74 68 65 20 66 69 72 73  ndex of the firs
34c5a 74 20 62 79 74 65 20 6a 75 73 74 20 70 61 73 74  t byte just past
34c5b 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
34c5c 74 6f 6b 65 6e 20 69 6e 0a 20 20 2a 2a 20 74 68  token in.  ** th
34c5d 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 2e 0a  e input buffer..
34c5e 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 62 75    **.  ** The bu
34c5f 66 66 65 72 20 2a 70 70 54 6f 6b 65 6e 20 69 73  ffer *ppToken is
34c60 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74   set to point at
34c61 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 74   is managed by t
34c62 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 20 20 2a  he tokenizer.  *
34c63 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
34c64 2e 20 49 74 20 69 73 20 6f 6e 6c 79 20 72 65 71  . It is only req
34c65 75 69 72 65 64 20 74 6f 20 62 65 20 76 61 6c 69  uired to be vali
34c66 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74  d until the next
34c67 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 78 4e   call.  ** to xN
34c68 65 78 74 28 29 20 6f 72 20 78 43 6c 6f 73 65 28  ext() or xClose(
34c69 29 2e 20 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54 4f  ). .  */.  /* TO
34c6a 44 4f 28 73 68 65 73 73 29 20 63 75 72 72 65 6e  DO(shess) curren
34c6b 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  t implementation
34c6c 20 72 65 71 75 69 72 65 73 20 70 49 6e 70 75 74   requires pInput
34c6d 20 74 6f 20 62 65 0a 20 20 2a 2a 20 6e 75 6c 2d   to be.  ** nul-
34c6e 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 69  terminated.  Thi
34c6f 73 20 73 68 6f 75 6c 64 20 65 69 74 68 65 72 20  s should either 
34c70 62 65 20 66 69 78 65 64 2c 20 6f 72 20 70 49 6e  be fixed, or pIn
34c71 70 75 74 2f 6e 42 79 74 65 73 0a 20 20 2a 2a 20  put/nBytes.  ** 
34c72 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e 76 65 72  should be conver
34c73 74 65 64 20 74 6f 20 7a 49 6e 70 75 74 2e 0a 20  ted to zInput.. 
34c74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78   */.  int (*xNex
34c75 74 29 28 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  t)(.    sqlite3_
34c76 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
34c77 20 2a 70 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20   *pCursor,   /* 
34c78 54 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72  Tokenizer cursor
34c79 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   */.    const ch
34c7a 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 69 6e  ar **ppToken, in
34c7b 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 2f 2a 20  t *pnBytes,  /* 
34c7c 4f 55 54 3a 20 4e 6f 72 6d 61 6c 69 7a 65 64 20  OUT: Normalized 
34c7d 74 65 78 74 20 66 6f 72 20 74 6f 6b 65 6e 20 2a  text for token *
34c7e 2f 0a 20 20 20 20 69 6e 74 20 2a 70 69 53 74 61  /.    int *piSta
34c7f 72 74 4f 66 66 73 65 74 2c 20 20 2f 2a 20 4f 55  rtOffset,  /* OU
34c80 54 3a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f  T: Byte offset o
34c81 66 20 74 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74  f token in input
34c82 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 69   buffer */.    i
34c83 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c  nt *piEndOffset,
34c84 20 20 20 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65      /* OUT: Byte
34c85 20 6f 66 66 73 65 74 20 6f 66 20 65 6e 64 20 6f   offset of end o
34c86 66 20 74 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74  f token in input
34c87 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 69   buffer */.    i
34c88 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20  nt *piPosition  
34c89 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62      /* OUT: Numb
34c8a 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 72 65 74  er of tokens ret
34c8b 75 72 6e 65 64 20 62 65 66 6f 72 65 20 74 68 69  urned before thi
34c8c 73 20 6f 6e 65 20 2a 2f 0a 20 20 29 3b 0a 7d 3b  s one */.  );.};
34c8d 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ..struct sqlite3
34c8e 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 63  _tokenizer {.  c
34c8f 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
34c90 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
34c91 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20  Module;  /* The 
34c92 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20  module for this 
34c93 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 2f  tokenizer */.  /
34c94 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c  * Tokenizer impl
34c95 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c  ementations will
34c96 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61   typically add a
34c97 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
34c98 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 73   */.};..struct s
34c99 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34c9a 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69  _cursor {.  sqli
34c9b 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
34c9c 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20  Tokenizer;      
34c9d 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 66 6f   /* Tokenizer fo
34c9e 72 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 2a  r this cursor. *
34c9f 2f 0a 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72  /.  /* Tokenizer
34ca0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
34ca1 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
34ca2 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  add additional f
34ca3 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 23 65 6e  ields */.};..#en
34ca4 64 69 66 20 2f 2a 20 5f 46 54 53 33 5f 54 4f 4b  dif /* _FTS3_TOK
34ca5 45 4e 49 5a 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a  ENIZER_H_ */../*
34ca6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
34ca7 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69  d of fts3_tokeni
34ca8 7a 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  zer.h **********
34ca9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34caa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
34cab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
34cac 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
34cad 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 66 74  e left off in ft
34cae 73 33 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a  s3_expr.h ******
34caf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
34cb0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
34cb1 6e 67 20 64 65 73 63 72 69 62 65 73 20 74 68 65  ng describes the
34cb2 20 73 79 6e 74 61 78 20 73 75 70 70 6f 72 74 65   syntax supporte
34cb3 64 20 62 79 20 74 68 65 20 66 74 73 33 20 4d 41  d by the fts3 MA
34cb4 54 43 48 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20  TCH.** operator 
34cb5 69 6e 20 61 20 73 69 6d 69 6c 61 72 20 66 6f 72  in a similar for
34cb6 6d 61 74 20 74 6f 20 74 68 61 74 20 75 73 65 64  mat to that used
34cb7 20 62 79 20 74 68 65 20 6c 65 6d 6f 6e 20 70 61   by the lemon pa
34cb8 72 73 65 72 0a 2a 2a 20 67 65 6e 65 72 61 74 6f  rser.** generato
34cb9 72 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 64  r. This module d
34cba 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 63 74 75  oes not use actu
34cbb 61 6c 6c 79 20 6c 65 6d 6f 6e 2c 20 69 74 20 75  ally lemon, it u
34cbc 73 65 73 20 61 0a 2a 2a 20 63 75 73 74 6f 6d 20  ses a.** custom 
34cbd 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20  parser..**.**   
34cbe 71 75 65 72 79 20 3a 3a 3d 20 61 6e 64 65 78 70  query ::= andexp
34cbf 72 20 28 4f 52 20 61 6e 64 65 78 70 72 29 2a 2e  r (OR andexpr)*.
34cc0 0a 2a 2a 0a 2a 2a 20 20 20 61 6e 64 65 78 70 72  .**.**   andexpr
34cc1 20 3a 3a 3d 20 6e 6f 74 65 78 70 72 20 28 41 4e   ::= notexpr (AN
34cc2 44 3f 20 6e 6f 74 65 78 70 72 29 2a 2e 0a 2a 2a  D? notexpr)*..**
34cc3 0a 2a 2a 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a  .**   notexpr ::
34cc4 3d 20 6e 65 61 72 65 78 70 72 20 28 4e 4f 54 20  = nearexpr (NOT 
34cc5 6e 65 61 72 65 78 70 72 7c 2d 54 4f 4b 45 4e 29  nearexpr|-TOKEN)
34cc6 2a 2e 0a 2a 2a 20 20 20 6e 6f 74 65 78 70 72 20  *..**   notexpr 
34cc7 3a 3a 3d 20 4c 50 20 71 75 65 72 79 20 52 50 2e  ::= LP query RP.
34cc8 0a 2a 2a 0a 2a 2a 20 20 20 6e 65 61 72 65 78 70  .**.**   nearexp
34cc9 72 20 3a 3a 3d 20 70 68 72 61 73 65 20 28 4e 45  r ::= phrase (NE
34cca 41 52 20 64 69 73 74 61 6e 63 65 5f 6f 70 74 20  AR distance_opt 
34ccb 6e 65 61 72 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a  nearexpr)*..**.*
34ccc 2a 20 20 20 64 69 73 74 61 6e 63 65 5f 6f 70 74  *   distance_opt
34ccd 20 3a 3a 3d 20 2e 0a 2a 2a 20 20 20 64 69 73 74   ::= ..**   dist
34cce 61 6e 63 65 5f 6f 70 74 20 3a 3a 3d 20 2f 20 49  ance_opt ::= / I
34ccf 4e 54 45 47 45 52 2e 0a 2a 2a 0a 2a 2a 20 20 20  NTEGER..**.**   
34cd0 70 68 72 61 73 65 20 3a 3a 3d 20 54 4f 4b 45 4e  phrase ::= TOKEN
34cd1 2e 0a 2a 2a 20 20 20 70 68 72 61 73 65 20 3a 3a  ..**   phrase ::
34cd2 3d 20 43 4f 4c 55 4d 4e 3a 54 4f 4b 45 4e 2e 0a  = COLUMN:TOKEN..
34cd3 2a 2a 20 20 20 70 68 72 61 73 65 20 3a 3a 3d 20  **   phrase ::= 
34cd4 22 54 4f 4b 45 4e 20 54 4f 4b 45 4e 20 54 4f 4b  "TOKEN TOKEN TOK
34cd5 45 4e 2e 2e 2e 22 2e 0a 2a 2f 0a 0a 74 79 70 65  EN..."..*/..type
34cd6 64 65 66 20 73 74 72 75 63 74 20 46 74 73 33 45  def struct Fts3E
34cd7 78 70 72 20 46 74 73 33 45 78 70 72 3b 0a 74 79  xpr Fts3Expr;.ty
34cd8 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74 73  pedef struct Fts
34cd9 33 50 68 72 61 73 65 20 46 74 73 33 50 68 72 61  3Phrase Fts3Phra
34cda 73 65 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 70 68  se;../*.** A "ph
34cdb 72 61 73 65 22 20 69 73 20 61 20 73 65 71 75 65  rase" is a seque
34cdc 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f  nce of one or mo
34cdd 72 65 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 6d  re tokens that m
34cde 75 73 74 20 6d 61 74 63 68 20 69 6e 0a 2a 2a 20  ust match in.** 
34cdf 73 65 71 75 65 6e 63 65 2e 20 20 41 20 73 69 6e  sequence.  A sin
34ce0 67 6c 65 20 74 6f 6b 65 6e 20 69 73 20 74 68 65  gle token is the
34ce1 20 62 61 73 65 20 63 61 73 65 20 61 6e 64 20 74   base case and t
34ce2 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 63  he most common c
34ce3 61 73 65 2e 0a 2a 2a 20 46 6f 72 20 61 20 73 65  ase..** For a se
34ce4 71 75 65 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 73  quence of tokens
34ce5 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 22 2e   contained in ".
34ce6 2e 2e 22 2c 20 6e 54 6f 6b 65 6e 20 77 69 6c 6c  ..", nToken will
34ce7 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a   be the number.*
34ce8 2a 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 74  * of tokens in t
34ce9 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74  he string..*/.st
34cea 72 75 63 74 20 46 74 73 33 50 68 72 61 73 65 20  ruct Fts3Phrase 
34ceb 7b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20  {.  int nToken; 
34cec 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
34ced 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20  er of tokens in 
34cee 74 68 65 20 70 68 72 61 73 65 20 2a 2f 0a 20 20  the phrase */.  
34cef 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
34cf0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
34cf1 20 63 6f 6c 75 6d 6e 20 74 68 69 73 20 70 68 72   column this phr
34cf2 61 73 65 20 6d 75 73 74 20 6d 61 74 63 68 20 2a  ase must match *
34cf3 2f 0a 20 20 69 6e 74 20 69 73 4e 6f 74 3b 20 20  /.  int isNot;  
34cf4 20 20 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61           /* Phra
34cf5 73 65 20 70 72 65 66 69 78 65 64 20 62 79 20 75  se prefixed by u
34cf6 6e 61 72 79 20 6e 6f 74 20 28 2d 29 20 6f 70 65  nary not (-) ope
34cf7 72 61 74 6f 72 20 2a 2f 0a 20 20 73 74 72 75 63  rator */.  struc
34cf8 74 20 50 68 72 61 73 65 54 6f 6b 65 6e 20 7b 0a  t PhraseToken {.
34cf9 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20      char *z;    
34cfa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
34cfb 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 2a  t of the token *
34cfc 2f 0a 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 20  /.    int n;    
34cfd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
34cfe 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
34cff 6e 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64  n buffer pointed
34d00 20 74 6f 20 62 79 20 7a 20 2a 2f 0a 20 20 20 20   to by z */.    
34d01 69 6e 74 20 69 73 50 72 65 66 69 78 3b 20 20 20  int isPrefix;   
34d02 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
34d03 20 74 6f 6b 65 6e 20 65 6e 64 73 20 69 6e 20 77   token ends in w
34d04 69 74 68 20 61 20 22 2a 22 20 63 68 61 72 61 63  ith a "*" charac
34d05 74 65 72 20 2a 2f 0a 20 20 7d 20 61 54 6f 6b 65  ter */.  } aToke
34d06 6e 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a  n[1];         /*
34d07 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65   One entry for e
34d08 61 63 68 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  ach token in the
34d09 20 70 68 72 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f   phrase */.};../
34d0a 2a 0a 2a 2a 20 41 20 74 72 65 65 20 6f 66 20 74  *.** A tree of t
34d0b 68 65 73 65 20 6f 62 6a 65 63 74 73 20 66 6f 72  hese objects for
34d0c 6d 73 20 74 68 65 20 52 48 53 20 6f 66 20 61 20  ms the RHS of a 
34d0d 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e 0a  MATCH operator..
34d0e 2a 2f 0a 73 74 72 75 63 74 20 46 74 73 33 45 78  */.struct Fts3Ex
34d0f 70 72 20 7b 0a 20 20 69 6e 74 20 65 54 79 70 65  pr {.  int eType
34d10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
34d11 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20    /* One of the 
34d12 46 54 53 51 55 45 52 59 5f 58 58 58 20 76 61 6c  FTSQUERY_XXX val
34d13 75 65 73 20 64 65 66 69 6e 65 64 20 62 65 6c 6f  ues defined belo
34d14 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 61 72  w */.  int nNear
34d15 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
34d16 20 20 2f 2a 20 56 61 6c 69 64 20 69 66 20 65 54    /* Valid if eT
34d17 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45  ype==FTSQUERY_NE
34d18 41 52 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72  AR */.  Fts3Expr
34d19 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20 20   *pParent;      
34d1a 20 20 20 2f 2a 20 70 50 61 72 65 6e 74 2d 3e 70     /* pParent->p
34d1b 4c 65 66 74 3d 3d 74 68 69 73 20 6f 72 20 70 50  Left==this or pP
34d1c 61 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 74  arent->pRight==t
34d1d 68 69 73 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  his */.  Fts3Exp
34d1e 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 20  r *pLeft;       
34d1f 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72      /* Left oper
34d20 61 6e 64 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  and */.  Fts3Exp
34d21 72 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20  r *pRight;      
34d22 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65      /* Right ope
34d23 72 61 6e 64 20 2a 2f 0a 20 20 46 74 73 33 50 68  rand */.  Fts3Ph
34d24 72 61 73 65 20 2a 70 50 68 72 61 73 65 3b 20 20  rase *pPhrase;  
34d25 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 69 66       /* Valid if
34d26 20 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59   eType==FTSQUERY
34d27 5f 50 48 52 41 53 45 20 2a 2f 0a 7d 3b 0a 0a 53  _PHRASE */.};..S
34d28 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
34d29 74 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70  t sqlite3Fts3Exp
34d2a 72 50 61 72 73 65 28 73 71 6c 69 74 65 33 5f 74  rParse(sqlite3_t
34d2b 6f 6b 65 6e 69 7a 65 72 20 2a 2c 20 63 68 61 72  okenizer *, char
34d2c 20 2a 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 0a   **, int, int, .
34d2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34d2e 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
34d2f 68 61 72 20 2a 2c 20 69 6e 74 2c 20 46 74 73 33  har *, int, Fts3
34d30 45 78 70 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45  Expr **);.SQLITE
34d31 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
34d32 6c 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65  lite3Fts3ExprFre
34d33 65 28 46 74 73 33 45 78 70 72 20 2a 29 3b 0a 0a  e(Fts3Expr *);..
34d34 2f 2a 0a 2a 2a 20 43 61 6e 64 69 64 61 74 65 20  /*.** Candidate 
34d35 76 61 6c 75 65 73 20 66 6f 72 20 46 74 73 33 51  values for Fts3Q
34d36 75 65 72 79 2e 65 54 79 70 65 2e 20 4e 6f 74 65  uery.eType. Note
34d37 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20   that the order 
34d38 6f 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  of the first.** 
34d39 66 6f 75 72 20 76 61 6c 75 65 73 20 69 73 20 69  four values is i
34d3a 6e 20 6f 72 64 65 72 20 6f 66 20 70 72 65 63 65  n order of prece
34d3b 64 65 6e 63 65 20 77 68 65 6e 20 70 61 72 73 69  dence when parsi
34d3c 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20  ng expressions. 
34d3d 46 6f 72 20 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c  For .** example,
34d3e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
34d3f 2a 2a 0a 2a 2a 20 20 20 22 61 20 4f 52 20 62 20  **.**   "a OR b 
34d40 41 4e 44 20 63 20 4e 4f 54 20 64 20 4e 45 41 52  AND c NOT d NEAR
34d41 20 65 22 0a 2a 2a 0a 2a 2a 20 69 73 20 65 71 75   e".**.** is equ
34d42 69 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a  ivalent to:.**.*
34d43 2a 20 20 20 22 61 20 4f 52 20 28 62 20 41 4e 44  *   "a OR (b AND
34d44 20 28 63 20 4e 4f 54 20 28 64 20 4e 45 41 52 20   (c NOT (d NEAR 
34d45 65 29 29 29 22 0a 2a 2f 0a 23 64 65 66 69 6e 65  e)))".*/.#define
34d46 20 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 20   FTSQUERY_NEAR  
34d47 20 31 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55   1.#define FTSQU
34d48 45 52 59 5f 4e 4f 54 20 20 20 20 32 0a 23 64 65  ERY_NOT    2.#de
34d49 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f 41 4e  fine FTSQUERY_AN
34d4a 44 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 46  D    3.#define F
34d4b 54 53 51 55 45 52 59 5f 4f 52 20 20 20 20 20 34  TSQUERY_OR     4
34d4c 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45 52  .#define FTSQUER
34d4d 59 5f 50 48 52 41 53 45 20 35 0a 0a 23 69 66 64  Y_PHRASE 5..#ifd
34d4e 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
34d4f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
34d50 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  id sqlite3Fts3Ex
34d51 70 72 49 6e 69 74 54 65 73 74 49 6e 74 65 72 66  prInitTestInterf
34d52 61 63 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ace(sqlite3 *db)
34d53 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  ;.#endif../*****
34d54 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
34d55 20 66 74 73 33 5f 65 78 70 72 2e 68 20 2a 2a 2a   fts3_expr.h ***
34d56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d58 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
34d59 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
34d5a 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
34d5b 66 74 20 6f 66 66 20 69 6e 20 66 74 73 33 2e 63  ft off in fts3.c
34d5c 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
34d5d 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
34d5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
34d5f 65 20 66 74 73 33 5f 68 61 73 68 2e 68 20 69 6e  e fts3_hash.h in
34d60 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 66   the middle of f
34d61 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ts3.c **********
34d62 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
34d63 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
34d64 66 69 6c 65 20 66 74 73 33 5f 68 61 73 68 2e 68  file fts3_hash.h
34d65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
34d66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d67 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
34d68 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32  2001 September 2
34d69 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
34d6a 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
34d6b 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
34d6c 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
34d6d 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
34d6e 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
34d6f 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
34d70 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
34d71 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
34d72 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
34d73 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
34d74 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
34d75 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
34d76 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
34d77 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
34d78 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
34d79 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
34d7a 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
34d7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d7f 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
34d80 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20  header file for 
34d81 74 68 65 20 67 65 6e 65 72 69 63 20 68 61 73 68  the generic hash
34d82 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 61  -table implemena
34d83 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69 6e 20  tion.** used in 
34d84 53 51 4c 69 74 65 2e 20 20 57 65 27 76 65 20 6d  SQLite.  We've m
34d85 6f 64 69 66 69 65 64 20 69 74 20 73 6c 69 67 68  odified it sligh
34d86 74 6c 79 20 74 6f 20 73 65 72 76 65 20 61 73 20  tly to serve as 
34d87 61 20 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a 20  a standalone.** 
34d88 68 61 73 68 20 74 61 62 6c 65 20 69 6d 70 6c 65  hash table imple
34d89 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74 68  mentation for th
34d8a 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65  e full-text inde
34d8b 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a  xing module..**.
34d8c 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 46 54 53 33  */.#ifndef _FTS3
34d8d 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e 65  _HASH_H_.#define
34d8e 20 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 0a 0a   _FTS3_HASH_H_..
34d8f 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
34d90 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63  rations of struc
34d91 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65  tures. */.typede
34d92 66 20 73 74 72 75 63 74 20 66 74 73 33 48 61 73  f struct fts3Has
34d93 68 20 66 74 73 33 48 61 73 68 3b 0a 74 79 70 65  h fts3Hash;.type
34d94 64 65 66 20 73 74 72 75 63 74 20 66 74 73 33 48  def struct fts3H
34d95 61 73 68 45 6c 65 6d 20 66 74 73 33 48 61 73 68  ashElem fts3Hash
34d96 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70  Elem;../* A comp
34d97 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 20  lete hash table 
34d98 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
34d99 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
34d9a 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68  structure..** Th
34d9b 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74  e internals of t
34d9c 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72  his structure ar
34d9d 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65  e intended to be
34d9e 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e   opaque -- clien
34d9f 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64  t.** code should
34da0 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
34da1 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 79  access or modify
34da2 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74   the fields of t
34da3 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  his structure.**
34da4 20 64 69 72 65 63 74 6c 79 2e 20 20 43 68 61 6e   directly.  Chan
34da5 67 65 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ge this structur
34da6 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e 67 20  e only by using 
34da7 74 68 65 20 72 6f 75 74 69 6e 65 73 20 62 65 6c  the routines bel
34da8 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20  ow..** However, 
34da9 6d 61 6e 79 20 6f 66 20 74 68 65 20 22 70 72 6f  many of the "pro
34daa 63 65 64 75 72 65 73 22 20 61 6e 64 20 22 66 75  cedures" and "fu
34dab 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64  nctions" for mod
34dac 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63  ifying and.** ac
34dad 63 65 73 73 69 6e 67 20 74 68 69 73 20 73 74 72  cessing this str
34dae 75 63 74 75 72 65 20 61 72 65 20 72 65 61 6c 6c  ucture are reall
34daf 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77 65 20  y macros, so we 
34db0 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b  can't really mak
34db1 65 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74  e.** this struct
34db2 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2f 0a 73  ure opaque..*/.s
34db3 74 72 75 63 74 20 66 74 73 33 48 61 73 68 20 7b  truct fts3Hash {
34db4 0a 20 20 63 68 61 72 20 6b 65 79 43 6c 61 73 73  .  char keyClass
34db5 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 41  ;          /* HA
34db6 53 48 5f 49 4e 54 2c 20 5f 50 4f 49 4e 54 45 52  SH_INT, _POINTER
34db7 2c 20 5f 53 54 52 49 4e 47 2c 20 5f 42 49 4e 41  , _STRING, _BINA
34db8 52 59 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 70  RY */.  char cop
34db9 79 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  yKey;           
34dba 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 70 79 20  /* True if copy 
34dbb 6f 66 20 6b 65 79 20 6d 61 64 65 20 6f 6e 20 69  of key made on i
34dbc 6e 73 65 72 74 20 2a 2f 0a 20 20 69 6e 74 20 63  nsert */.  int c
34dbd 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  ount;           
34dbe 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
34dbf 65 6e 74 72 69 65 73 20 69 6e 20 74 68 69 73 20  entries in this 
34dc0 74 61 62 6c 65 20 2a 2f 0a 20 20 66 74 73 33 48  table */.  fts3H
34dc1 61 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20  ashElem *first; 
34dc2 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20     /* The first 
34dc3 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61  element of the a
34dc4 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 74  rray */.  int ht
34dc5 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  size;           
34dc6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
34dc7 75 63 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61  uckets in the ha
34dc8 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74  sh table */.  st
34dc9 72 75 63 74 20 5f 66 74 73 33 68 74 20 7b 20 20  ruct _fts3ht {  
34dca 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73        /* the has
34dcb 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69  h table */.    i
34dcc 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20  nt count;       
34dcd 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
34dce 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74  r of entries wit
34dcf 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20  h this hash */. 
34dd0 20 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20     fts3HashElem 
34dd1 2a 63 68 61 69 6e 3b 20 20 20 20 20 2f 2a 20 50  *chain;     /* P
34dd2 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20  ointer to first 
34dd3 65 6e 74 72 79 20 77 69 74 68 20 74 68 69 73 20  entry with this 
34dd4 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b  hash */.  } *ht;
34dd5 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65  .};../* Each ele
34dd6 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73 68  ment in the hash
34dd7 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73   table is an ins
34dd8 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
34dd9 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63  lowing .** struc
34dda 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65  ture.  All eleme
34ddb 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f  nts are stored o
34ddc 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c  n a single doubl
34ddd 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a  y-linked list..*
34dde 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73  *.** Again, this
34ddf 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e   structure is in
34de0 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61  tended to be opa
34de1 71 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27  que, but it can'
34de2 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f  t really.** be o
34de3 70 61 71 75 65 20 62 65 63 61 75 73 65 20 69 74  paque because it
34de4 20 69 73 20 75 73 65 64 20 62 79 20 6d 61 63 72   is used by macr
34de5 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 66 74  os..*/.struct ft
34de6 73 33 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20 66  s3HashElem {.  f
34de7 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78  ts3HashElem *nex
34de8 74 2c 20 2a 70 72 65 76 3b 20 2f 2a 20 4e 65 78  t, *prev; /* Nex
34de9 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20 65  t and previous e
34dea 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 74  lements in the t
34deb 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  able */.  void *
34dec 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  data;           
34ded 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61 73 73       /* Data ass
34dee 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69  ociated with thi
34def 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 76  s element */.  v
34df0 6f 69 64 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e  oid *pKey; int n
34df1 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 4b 65 79  Key;      /* Key
34df2 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
34df3 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f   this element */
34df4 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65  .};../*.** There
34df5 20 61 72 65 20 32 20 64 69 66 66 65 72 65 6e 74   are 2 different
34df6 20 6d 6f 64 65 73 20 6f 66 20 6f 70 65 72 61 74   modes of operat
34df7 69 6f 6e 20 66 6f 72 20 61 20 68 61 73 68 20 74  ion for a hash t
34df8 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 54  able:.**.**   FT
34df9 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 20  S3_HASH_STRING  
34dfa 20 20 20 20 20 20 70 4b 65 79 20 70 6f 69 6e 74        pKey point
34dfb 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 74 68  s to a string th
34dfc 61 74 20 69 73 20 6e 4b 65 79 20 62 79 74 65 73  at is nKey bytes
34dfd 20 6c 6f 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20   long.**        
34dfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34dff 20 20 20 28 69 6e 63 6c 75 64 69 6e 67 20 74 68     (including th
34e00 65 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 6f  e null-terminato
34e01 72 2c 20 69 66 20 61 6e 79 29 2e 20 20 43 61 73  r, if any).  Cas
34e02 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
34e03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
34e04 73 20 72 65 73 70 65 63 74 65 64 20 69 6e 20 63  s respected in c
34e05 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a  omparisons..**.*
34e06 2a 20 20 20 46 54 53 33 5f 48 41 53 48 5f 42 49  *   FTS3_HASH_BI
34e07 4e 41 52 59 20 20 20 20 20 20 20 20 70 4b 65 79  NARY        pKey
34e08 20 70 6f 69 6e 74 73 20 74 6f 20 62 69 6e 61 72   points to binar
34e09 79 20 64 61 74 61 20 6e 4b 65 79 20 62 79 74 65  y data nKey byte
34e0a 73 20 6c 6f 6e 67 2e 20 0a 2a 2a 20 20 20 20 20  s long. .**     
34e0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e0c 20 20 20 20 20 20 6d 65 6d 63 6d 70 28 29 20 69        memcmp() i
34e0d 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72  s used to compar
34e0e 65 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a 20 41 20  e keys..**.** A 
34e0f 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20  copy of the key 
34e10 69 73 20 6d 61 64 65 20 69 66 20 74 68 65 20 63  is made if the c
34e11 6f 70 79 4b 65 79 20 70 61 72 61 6d 65 74 65 72  opyKey parameter
34e12 20 74 6f 20 66 74 73 33 48 61 73 68 49 6e 69 74   to fts3HashInit
34e13 20 69 73 20 31 2e 20 20 0a 2a 2f 0a 23 64 65 66   is 1.  .*/.#def
34e14 69 6e 65 20 46 54 53 33 5f 48 41 53 48 5f 53 54  ine FTS3_HASH_ST
34e15 52 49 4e 47 20 20 20 20 31 0a 23 64 65 66 69 6e  RING    1.#defin
34e16 65 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41  e FTS3_HASH_BINA
34e17 52 59 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 41  RY    2../*.** A
34e18 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e 20  ccess routines. 
34e19 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73 65   To delete, inse
34e1a 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  rt a NULL pointe
34e1b 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
34e1c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
34e1d 33 46 74 73 33 48 61 73 68 49 6e 69 74 28 66 74  3Fts3HashInit(ft
34e1e 73 33 48 61 73 68 2a 2c 20 69 6e 74 20 6b 65 79  s3Hash*, int key
34e1f 74 79 70 65 2c 20 69 6e 74 20 63 6f 70 79 4b 65  type, int copyKe
34e20 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  y);.SQLITE_PRIVA
34e21 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
34e22 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 66  Fts3HashInsert(f
34e23 74 73 33 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20  ts3Hash*, const 
34e24 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
34e25 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61 74  nKey, void *pDat
34e26 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  a);.SQLITE_PRIVA
34e27 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
34e28 46 74 73 33 48 61 73 68 46 69 6e 64 28 63 6f 6e  Fts3HashFind(con
34e29 73 74 20 66 74 73 33 48 61 73 68 2a 2c 20 63 6f  st fts3Hash*, co
34e2a 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
34e2b 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54  int nKey);.SQLIT
34e2c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
34e2d 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c  qlite3Fts3HashCl
34e2e 65 61 72 28 66 74 73 33 48 61 73 68 2a 29 3b 0a  ear(fts3Hash*);.
34e2f 0a 2f 2a 0a 2a 2a 20 53 68 6f 72 74 68 61 6e 64  ./*.** Shorthand
34e30 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f   for the functio
34e31 6e 73 20 61 62 6f 76 65 0a 2a 2f 0a 23 64 65 66  ns above.*/.#def
34e32 69 6e 65 20 66 74 73 33 48 61 73 68 49 6e 69 74  ine fts3HashInit
34e33 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61     sqlite3Fts3Ha
34e34 73 68 49 6e 69 74 0a 23 64 65 66 69 6e 65 20 66  shInit.#define f
34e35 74 73 33 48 61 73 68 49 6e 73 65 72 74 20 73 71  ts3HashInsert sq
34e36 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73  lite3Fts3HashIns
34e37 65 72 74 0a 23 64 65 66 69 6e 65 20 66 74 73 33  ert.#define fts3
34e38 48 61 73 68 46 69 6e 64 20 20 20 73 71 6c 69 74  HashFind   sqlit
34e39 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 0a 23  e3Fts3HashFind.#
34e3a 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 43  define fts3HashC
34e3b 6c 65 61 72 20 20 73 71 6c 69 74 65 33 46 74 73  lear  sqlite3Fts
34e3c 33 48 61 73 68 43 6c 65 61 72 0a 0a 2f 2a 0a 2a  3HashClear../*.*
34e3d 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f  * Macros for loo
34e3e 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c  ping over all el
34e3f 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68  ements of a hash
34e40 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69   table.  The idi
34e41 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68  om is.** like th
34e42 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73 33  is:.**.**   fts3
34e43 48 61 73 68 20 68 3b 0a 2a 2a 20 20 20 66 74 73  Hash h;.**   fts
34e44 33 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a  3HashElem *p;.**
34e45 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28     ....**   for(
34e46 70 3d 66 74 73 33 48 61 73 68 46 69 72 73 74 28  p=fts3HashFirst(
34e47 26 68 29 3b 20 70 3b 20 70 3d 66 74 73 33 48 61  &h); p; p=fts3Ha
34e48 73 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a 20 20  shNext(p)){.**  
34e49 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75 72 65     SomeStructure
34e4a 20 2a 70 44 61 74 61 20 3d 20 66 74 73 33 48 61   *pData = fts3Ha
34e4b 73 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20  shData(p);.**   
34e4c 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e    // do somethin
34e4d 67 20 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20  g with pData.** 
34e4e 20 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66    }.*/.#define f
34e4f 74 73 33 48 61 73 68 46 69 72 73 74 28 48 29 20  ts3HashFirst(H) 
34e50 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64   ((H)->first).#d
34e51 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 4e 65  efine fts3HashNe
34e52 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e 6e 65  xt(E)   ((E)->ne
34e53 78 74 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33  xt).#define fts3
34e54 48 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28  HashData(E)   ((
34e55 45 29 2d 3e 64 61 74 61 29 0a 23 64 65 66 69 6e  E)->data).#defin
34e56 65 20 66 74 73 33 48 61 73 68 4b 65 79 28 45 29  e fts3HashKey(E)
34e57 20 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 0a      ((E)->pKey).
34e58 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
34e59 4b 65 79 73 69 7a 65 28 45 29 20 28 28 45 29 2d  Keysize(E) ((E)-
34e5a 3e 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a 2a 20 4e 75  >nKey)../*.** Nu
34e5b 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
34e5c 69 6e 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a  in a hash table.
34e5d 2a 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  */.#define fts3H
34e5e 61 73 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48  ashCount(H)  ((H
34e5f 29 2d 3e 63 6f 75 6e 74 29 0a 0a 23 65 6e 64 69  )->count)..#endi
34e60 66 20 2f 2a 20 5f 46 54 53 33 5f 48 41 53 48 5f  f /* _FTS3_HASH_
34e61 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  H_ */../********
34e62 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
34e63 73 33 5f 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a  s3_hash.h ******
34e64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e66 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
34e67 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
34e68 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
34e69 6f 66 66 20 69 6e 20 66 74 73 33 2e 63 20 2a 2a  off in fts3.c **
34e6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e6b 2a 2a 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  *****/.#ifndef S
34e6c 51 4c 49 54 45 5f 43 4f 52 45 20 0a 20 20 53 51  QLITE_CORE .  SQ
34e6d 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49  LITE_EXTENSION_I
34e6e 4e 49 54 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  NIT1.#endif.../*
34e6f 20 54 4f 44 4f 28 73 68 65 73 73 29 20 4d 41 4e   TODO(shess) MAN
34e70 2c 20 74 68 69 73 20 74 68 69 6e 67 20 6e 65 65  , this thing nee
34e71 64 73 20 73 6f 6d 65 20 72 65 66 61 63 74 6f 72  ds some refactor
34e72 69 6e 67 2e 20 20 41 74 20 6d 69 6e 69 6d 75 6d  ing.  At minimum
34e73 2c 20 69 74 0a 2a 2a 20 77 6f 75 6c 64 20 62 65  , it.** would be
34e74 20 6e 69 63 65 20 74 6f 20 6f 72 64 65 72 20 74   nice to order t
34e75 68 65 20 66 69 6c 65 20 62 65 74 74 65 72 2c 20  he file better, 
34e76 70 65 72 68 61 70 73 20 73 6f 6d 65 74 68 69 6e  perhaps somethin
34e77 67 20 61 6c 6f 6e 67 20 74 68 65 0a 2a 2a 20 6c  g along the.** l
34e78 69 6e 65 73 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20  ines of:.**.**  
34e79 2d 20 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69  - utility functi
34e7a 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20  ons.**  - table 
34e7b 73 65 74 75 70 20 66 75 6e 63 74 69 6f 6e 73 0a  setup functions.
34e7c 2a 2a 20 20 2d 20 74 61 62 6c 65 20 75 70 64 61  **  - table upda
34e7d 74 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20  te functions.** 
34e7e 20 2d 20 74 61 62 6c 65 20 71 75 65 72 79 20 66   - table query f
34e7f 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 50  unctions.**.** P
34e80 75 74 20 74 68 65 20 71 75 65 72 79 20 66 75 6e  ut the query fun
34e81 63 74 69 6f 6e 73 20 6c 61 73 74 20 62 65 63 61  ctions last beca
34e82 75 73 65 20 74 68 65 79 27 72 65 20 6c 69 6b 65  use they're like
34e83 6c 79 20 74 6f 20 72 65 66 65 72 65 6e 63 65 0a  ly to reference.
34e84 2a 2a 20 74 79 70 65 64 65 66 73 20 6f 72 20 66  ** typedefs or f
34e85 75 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68  unctions from th
34e86 65 20 74 61 62 6c 65 20 75 70 64 61 74 65 20 73  e table update s
34e87 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 0a 23 69 66 20  ection..*/..#if 
34e88 30 0a 23 20 64 65 66 69 6e 65 20 46 54 53 54 52  0.# define FTSTR
34e89 41 43 45 28 41 29 20 20 70 72 69 6e 74 66 20 41  ACE(A)  printf A
34e8a 3b 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29  ; fflush(stdout)
34e8b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
34e8c 46 54 53 54 52 41 43 45 28 41 29 0a 23 65 6e 64  FTSTRACE(A).#end
34e8d 69 66 0a 0a 2f 2a 20 49 74 20 69 73 20 6e 6f 74  if../* It is not
34e8e 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 69 73   safe to call is
34e8f 73 70 61 63 65 28 29 2c 20 74 6f 6c 6f 77 65 72  space(), tolower
34e90 28 29 2c 20 6f 72 20 69 73 61 6c 6e 75 6d 28 29  (), or isalnum()
34e91 20 6f 6e 0a 2a 2a 20 68 69 2d 62 69 74 2d 73 65   on.** hi-bit-se
34e92 74 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54  t characters.  T
34e93 68 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20  his is the same 
34e94 73 6f 6c 75 74 69 6f 6e 20 75 73 65 64 20 69 6e  solution used in
34e95 20 74 68 65 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65   the.** tokenize
34e96 72 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  r..*/./* TODO(sh
34e97 65 73 73 29 20 54 68 65 20 73 6e 69 70 70 65 74  ess) The snippet
34e98 2d 67 65 6e 65 72 61 74 69 6f 6e 20 63 6f 64 65  -generation code
34e99 20 73 68 6f 75 6c 64 20 62 65 20 75 73 69 6e 67   should be using
34e9a 20 74 68 65 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65   the.** tokenize
34e9b 72 2d 67 65 6e 65 72 61 74 65 64 20 74 6f 6b 65  r-generated toke
34e9c 6e 73 20 72 61 74 68 65 72 20 74 68 61 6e 20 64  ns rather than d
34e9d 6f 69 6e 67 20 69 74 73 20 6f 77 6e 20 6c 6f 63  oing its own loc
34e9e 61 6c 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 61 74 69  al.** tokenizati
34e9f 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  on..*/./* TODO(s
34ea0 68 65 73 73 29 20 49 73 20 5f 5f 69 73 61 73 63  hess) Is __isasc
34ea1 69 69 28 29 20 61 20 70 6f 72 74 61 62 6c 65 20  ii() a portable 
34ea2 76 65 72 73 69 6f 6e 20 6f 66 20 28 63 26 30 78  version of (c&0x
34ea3 38 30 29 3d 3d 30 3f 20 2a 2f 0a 73 74 61 74 69  80)==0? */.stati
34ea4 63 20 69 6e 74 20 73 61 66 65 5f 69 73 73 70 61  c int safe_isspa
34ea5 63 65 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65  ce(char c){.  re
34ea6 74 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30  turn (c&0x80)==0
34ea7 20 3f 20 69 73 73 70 61 63 65 28 63 29 20 3a 20   ? isspace(c) : 
34ea8 30 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  0;.}.static int 
34ea9 73 61 66 65 5f 74 6f 6c 6f 77 65 72 28 63 68 61  safe_tolower(cha
34eaa 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  r c){.  return (
34eab 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 74 6f 6c  c&0x80)==0 ? tol
34eac 6f 77 65 72 28 63 29 20 3a 20 63 3b 0a 7d 0a 73  ower(c) : c;.}.s
34ead 74 61 74 69 63 20 69 6e 74 20 73 61 66 65 5f 69  tatic int safe_i
34eae 73 61 6c 6e 75 6d 28 63 68 61 72 20 63 29 7b 0a  salnum(char c){.
34eaf 20 20 72 65 74 75 72 6e 20 28 63 26 30 78 38 30    return (c&0x80
34eb0 29 3d 3d 30 20 3f 20 69 73 61 6c 6e 75 6d 28 63  )==0 ? isalnum(c
34eb1 29 20 3a 20 30 3b 0a 7d 0a 0a 74 79 70 65 64 65  ) : 0;.}..typede
34eb2 66 20 65 6e 75 6d 20 44 6f 63 4c 69 73 74 54 79  f enum DocListTy
34eb3 70 65 20 7b 0a 20 20 44 4c 5f 44 4f 43 49 44 53  pe {.  DL_DOCIDS
34eb4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
34eb5 2a 20 64 6f 63 69 64 73 20 6f 6e 6c 79 20 2a 2f  * docids only */
34eb6 0a 20 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c  .  DL_POSITIONS,
34eb7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 6f             /* do
34eb8 63 69 64 73 20 2b 20 70 6f 73 69 74 69 6f 6e 73  cids + positions
34eb9 20 2a 2f 0a 20 20 44 4c 5f 50 4f 53 49 54 49 4f   */.  DL_POSITIO
34eba 4e 53 5f 4f 46 46 53 45 54 53 20 20 20 20 2f 2a  NS_OFFSETS    /*
34ebb 20 64 6f 63 69 64 73 20 2b 20 70 6f 73 69 74 69   docids + positi
34ebc 6f 6e 73 20 2b 20 6f 66 66 73 65 74 73 20 2a 2f  ons + offsets */
34ebd 0a 7d 20 44 6f 63 4c 69 73 74 54 79 70 65 3b 0a  .} DocListType;.
34ebe 0a 2f 2a 0a 2a 2a 20 42 79 20 64 65 66 61 75 6c  ./*.** By defaul
34ebf 74 2c 20 6f 6e 6c 79 20 70 6f 73 69 74 69 6f 6e  t, only position
34ec0 73 20 61 6e 64 20 6e 6f 74 20 6f 66 66 73 65 74  s and not offset
34ec1 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  s are stored in 
34ec2 74 68 65 20 64 6f 63 6c 69 73 74 73 2e 0a 2a 2a  the doclists..**
34ec3 20 54 6f 20 63 68 61 6e 67 65 20 74 68 69 73 20   To change this 
34ec4 73 6f 20 74 68 61 74 20 6f 66 66 73 65 74 73 20  so that offsets 
34ec5 61 72 65 20 73 74 6f 72 65 64 20 74 6f 6f 2c 20  are stored too, 
34ec6 63 6f 6d 70 69 6c 65 20 77 69 74 68 0a 2a 2a 0a  compile with.**.
34ec7 2a 2a 20 20 20 20 20 20 20 20 20 20 2d 44 44 4c  **          -DDL
34ec8 5f 44 45 46 41 55 4c 54 3d 44 4c 5f 50 4f 53 49  _DEFAULT=DL_POSI
34ec9 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 0a 2a 2a  TIONS_OFFSETS.**
34eca 0a 2a 2a 20 49 66 20 44 4c 5f 44 45 46 41 55 4c  .** If DL_DEFAUL
34ecb 54 20 69 73 20 73 65 74 20 74 6f 20 44 4c 5f 44  T is set to DL_D
34ecc 4f 43 49 44 53 2c 20 79 6f 75 72 20 74 61 62 6c  OCIDS, your tabl
34ecd 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 69 6e  e can only be in
34ece 73 65 72 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 28  serted.** into (
34ecf 6e 6f 20 64 65 6c 65 74 65 73 20 6f 72 20 75 70  no deletes or up
34ed0 64 61 74 65 73 29 2e 0a 2a 2f 0a 23 69 66 6e 64  dates)..*/.#ifnd
34ed1 65 66 20 44 4c 5f 44 45 46 41 55 4c 54 0a 23 20  ef DL_DEFAULT.# 
34ed2 64 65 66 69 6e 65 20 44 4c 5f 44 45 46 41 55 4c  define DL_DEFAUL
34ed3 54 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 0a 23  T DL_POSITIONS.#
34ed4 65 6e 64 69 66 0a 0a 65 6e 75 6d 20 7b 0a 20 20  endif..enum {.  
34ed5 50 4f 53 5f 45 4e 44 20 3d 20 30 2c 20 20 20 20  POS_END = 0,    
34ed6 20 20 20 20 2f 2a 20 65 6e 64 20 6f 66 20 74 68      /* end of th
34ed7 69 73 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74  is position list
34ed8 20 2a 2f 0a 20 20 50 4f 53 5f 43 4f 4c 55 4d 4e   */.  POS_COLUMN
34ed9 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 6c  ,         /* fol
34eda 6c 6f 77 65 64 20 62 79 20 6e 65 77 20 63 6f 6c  lowed by new col
34edb 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  umn number */.  
34edc 50 4f 53 5f 42 41 53 45 0a 7d 3b 0a 0a 2f 2a 20  POS_BASE.};../* 
34edd 4d 45 52 47 45 5f 43 4f 55 4e 54 20 63 6f 6e 74  MERGE_COUNT cont
34ede 72 6f 6c 73 20 68 6f 77 20 6f 66 74 65 6e 20 77  rols how often w
34edf 65 20 6d 65 72 67 65 20 73 65 67 6d 65 6e 74 73  e merge segments
34ee0 20 28 73 65 65 20 63 6f 6d 6d 65 6e 74 20 61 74   (see comment at
34ee1 0a 2a 2a 20 74 6f 70 20 6f 66 20 66 69 6c 65 29  .** top of file)
34ee2 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 52  ..*/.#define MER
34ee3 47 45 5f 43 4f 55 4e 54 20 31 36 0a 0a 2f 2a 20  GE_COUNT 16../* 
34ee4 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e  utility function
34ee5 73 20 2a 2f 0a 0a 2f 2a 20 43 4c 45 41 52 28 29  s */../* CLEAR()
34ee6 20 61 6e 64 20 53 43 52 41 4d 42 4c 45 28 29 20   and SCRAMBLE() 
34ee7 61 62 73 74 72 61 63 74 20 6d 65 6d 73 65 74 28  abstract memset(
34ee8 29 20 6f 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  ) on a pointer t
34ee9 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 72 65  o a single.** re
34eea 63 6f 72 64 20 74 6f 20 70 72 65 76 65 6e 74 20  cord to prevent 
34eeb 65 72 72 6f 72 73 20 6f 66 20 74 68 65 20 66 6f  errors of the fo
34eec 72 6d 3a 0a 2a 2a 0a 2a 2a 20 6d 79 5f 66 75 6e  rm:.**.** my_fun
34eed 63 74 69 6f 6e 28 53 6f 6d 65 54 79 70 65 20 2a  ction(SomeType *
34eee 62 29 7b 0a 2a 2a 20 20 20 6d 65 6d 73 65 74 28  b){.**   memset(
34eef 62 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28  b, '\0', sizeof(
34ef0 62 29 29 3b 20 20 2f 2f 20 73 69 7a 65 6f 66 28  b));  // sizeof(
34ef1 62 29 21 3d 73 69 7a 65 6f 66 28 2a 62 29 0a 2a  b)!=sizeof(*b).*
34ef2 2a 20 7d 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  * }.*/./* TODO(s
34ef3 68 65 73 73 29 20 4f 62 76 69 6f 75 73 20 63 61  hess) Obvious ca
34ef4 6e 64 69 64 61 74 65 73 20 66 6f 72 20 61 20 68  ndidates for a h
34ef5 65 61 64 65 72 20 66 69 6c 65 2e 20 2a 2f 0a 23  eader file. */.#
34ef6 64 65 66 69 6e 65 20 43 4c 45 41 52 28 62 29 20  define CLEAR(b) 
34ef7 6d 65 6d 73 65 74 28 62 2c 20 27 5c 30 27 2c 20  memset(b, '\0', 
34ef8 73 69 7a 65 6f 66 28 2a 28 62 29 29 29 0a 0a 23  sizeof(*(b)))..#
34ef9 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 20  ifndef NDEBUG.# 
34efa 20 64 65 66 69 6e 65 20 53 43 52 41 4d 42 4c 45   define SCRAMBLE
34efb 28 62 29 20 6d 65 6d 73 65 74 28 62 2c 20 30 78  (b) memset(b, 0x
34efc 35 35 2c 20 73 69 7a 65 6f 66 28 2a 28 62 29 29  55, sizeof(*(b))
34efd 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  ).#else.#  defin
34efe 65 20 53 43 52 41 4d 42 4c 45 28 62 29 0a 23 65  e SCRAMBLE(b).#e
34eff 6e 64 69 66 0a 0a 2f 2a 20 57 65 20 6d 61 79 20  ndif../* We may 
34f00 6e 65 65 64 20 75 70 20 74 6f 20 56 41 52 49 4e  need up to VARIN
34f01 54 5f 4d 41 58 20 62 79 74 65 73 20 74 6f 20 73  T_MAX bytes to s
34f02 74 6f 72 65 20 61 6e 20 65 6e 63 6f 64 65 64 20  tore an encoded 
34f03 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 20  64-bit integer. 
34f04 2a 2f 0a 23 64 65 66 69 6e 65 20 56 41 52 49 4e  */.#define VARIN
34f05 54 5f 4d 41 58 20 31 30 0a 0a 2f 2a 20 57 72 69  T_MAX 10../* Wri
34f06 74 65 20 61 20 36 34 2d 62 69 74 20 76 61 72 69  te a 64-bit vari
34f07 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
34f08 67 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74  ger to memory st
34f09 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a  arting at p[0]..
34f0a 20 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66   * The length of
34f0b 20 64 61 74 61 20 77 72 69 74 74 65 6e 20 77 69   data written wi
34f0c 6c 6c 20 62 65 20 62 65 74 77 65 65 6e 20 31 20  ll be between 1 
34f0d 61 6e 64 20 56 41 52 49 4e 54 5f 4d 41 58 20 62  and VARINT_MAX b
34f0e 79 74 65 73 2e 0a 20 2a 20 54 68 65 20 6e 75 6d  ytes.. * The num
34f0f 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 69  ber of bytes wri
34f10 74 74 65 6e 20 69 73 20 72 65 74 75 72 6e 65 64  tten is returned
34f11 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
34f12 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 68  fts3PutVarint(ch
34f13 61 72 20 2a 70 2c 20 73 71 6c 69 74 65 5f 69 6e  ar *p, sqlite_in
34f14 74 36 34 20 76 29 7b 0a 20 20 75 6e 73 69 67 6e  t64 v){.  unsign
34f15 65 64 20 63 68 61 72 20 2a 71 20 3d 20 28 75 6e  ed char *q = (un
34f16 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 70  signed char *) p
34f17 3b 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  ;.  sqlite_uint6
34f18 34 20 76 75 20 3d 20 76 3b 0a 20 20 64 6f 7b 0a  4 vu = v;.  do{.
34f19 20 20 20 20 2a 71 2b 2b 20 3d 20 28 75 6e 73 69      *q++ = (unsi
34f1a 67 6e 65 64 20 63 68 61 72 29 20 28 28 76 75 20  gned char) ((vu 
34f1b 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 29 3b  & 0x7f) | 0x80);
34f1c 0a 20 20 20 20 76 75 20 3e 3e 3d 20 37 3b 0a 20  .    vu >>= 7;. 
34f1d 20 7d 77 68 69 6c 65 28 20 76 75 21 3d 30 20 29   }while( vu!=0 )
34f1e 3b 0a 20 20 71 5b 2d 31 5d 20 26 3d 20 30 78 37  ;.  q[-1] &= 0x7
34f1f 66 3b 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20  f;  /* turn off 
34f20 68 69 67 68 20 62 69 74 20 69 6e 20 66 69 6e 61  high bit in fina
34f21 6c 20 62 79 74 65 20 2a 2f 0a 20 20 61 73 73 65  l byte */.  asse
34f22 72 74 28 20 71 20 2d 20 28 75 6e 73 69 67 6e 65  rt( q - (unsigne
34f23 64 20 63 68 61 72 20 2a 29 70 20 3c 3d 20 56 41  d char *)p <= VA
34f24 52 49 4e 54 5f 4d 41 58 20 29 3b 0a 20 20 72 65  RINT_MAX );.  re
34f25 74 75 72 6e 20 28 69 6e 74 29 20 28 71 20 2d 20  turn (int) (q - 
34f26 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
34f27 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 61 64 20  )p);.}../* Read 
34f28 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c  a 64-bit variabl
34f29 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
34f2a 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61   from memory sta
34f2b 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 20  rting at p[0].. 
34f2c 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
34f2d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61  ber of bytes rea
34f2e 64 2c 20 6f 72 20 30 20 6f 6e 20 65 72 72 6f 72  d, or 0 on error
34f2f 2e 0a 20 2a 20 54 68 65 20 76 61 6c 75 65 20 69  .. * The value i
34f30 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 20  s stored in *v. 
34f31 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
34f32 73 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73  s3GetVarint(cons
34f33 74 20 63 68 61 72 20 2a 70 2c 20 73 71 6c 69 74  t char *p, sqlit
34f34 65 5f 69 6e 74 36 34 20 2a 76 29 7b 0a 20 20 63  e_int64 *v){.  c
34f35 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
34f36 61 72 20 2a 71 20 3d 20 28 63 6f 6e 73 74 20 75  ar *q = (const u
34f37 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
34f38 70 3b 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74  p;.  sqlite_uint
34f39 36 34 20 78 20 3d 20 30 2c 20 79 20 3d 20 31 3b  64 x = 0, y = 1;
34f3a 0a 20 20 77 68 69 6c 65 28 20 28 2a 71 20 26 20  .  while( (*q & 
34f3b 30 78 38 30 29 20 3d 3d 20 30 78 38 30 20 29 7b  0x80) == 0x80 ){
34f3c 0a 20 20 20 20 78 20 2b 3d 20 79 20 2a 20 28 2a  .    x += y * (*
34f3d 71 2b 2b 20 26 20 30 78 37 66 29 3b 0a 20 20 20  q++ & 0x7f);.   
34f3e 20 79 20 3c 3c 3d 20 37 3b 0a 20 20 20 20 69 66   y <<= 7;.    if
34f3f 28 20 71 20 2d 20 28 75 6e 73 69 67 6e 65 64 20  ( q - (unsigned 
34f40 63 68 61 72 20 2a 29 70 20 3e 3d 20 56 41 52 49  char *)p >= VARI
34f41 4e 54 5f 4d 41 58 20 29 7b 20 20 2f 2a 20 62 61  NT_MAX ){  /* ba
34f42 64 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 20 20  d data */.      
34f43 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20  assert( 0 );.   
34f44 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
34f45 20 7d 0a 20 20 7d 0a 20 20 78 20 2b 3d 20 79 20   }.  }.  x += y 
34f46 2a 20 28 2a 71 2b 2b 29 3b 0a 20 20 2a 76 20 3d  * (*q++);.  *v =
34f47 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 20   (sqlite_int64) 
34f48 78 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74  x;.  return (int
34f49 29 20 28 71 20 2d 20 28 75 6e 73 69 67 6e 65 64  ) (q - (unsigned
34f4a 20 63 68 61 72 20 2a 29 70 29 3b 0a 7d 0a 0a 73   char *)p);.}..s
34f4b 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 47 65  tatic int fts3Ge
34f4c 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20  tVarint32(const 
34f4d 63 68 61 72 20 2a 70 2c 20 69 6e 74 20 2a 70 69  char *p, int *pi
34f4e 29 7b 0a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ){. sqlite_int64
34f4f 20 69 3b 0a 20 69 6e 74 20 72 65 74 20 3d 20 66   i;. int ret = f
34f50 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20  ts3GetVarint(p, 
34f51 26 69 29 3b 0a 20 2a 70 69 20 3d 20 28 69 6e 74  &i);. *pi = (int
34f52 29 20 69 3b 0a 20 61 73 73 65 72 74 28 20 2a 70  ) i;. assert( *p
34f53 69 3d 3d 69 20 29 3b 0a 20 72 65 74 75 72 6e 20  i==i );. return 
34f54 72 65 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ret;.}../*******
34f55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
34f59 20 44 61 74 61 42 75 66 66 65 72 20 69 73 20 75   DataBuffer is u
34f5a 73 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74 20 64  sed to collect d
34f5b 61 74 61 20 69 6e 74 6f 20 61 20 62 75 66 66 65  ata into a buffe
34f5c 72 20 69 6e 20 70 69 65 63 65 6d 65 61 6c 0a 2a  r in piecemeal.*
34f5d 2a 20 66 61 73 68 69 6f 6e 2e 20 20 49 74 20 69  * fashion.  It i
34f5e 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 75 73  mplements the us
34f5f 75 61 6c 20 64 69 73 74 69 6e 63 74 69 6f 6e 20  ual distinction 
34f60 62 65 74 77 65 65 6e 20 61 6d 6f 75 6e 74 20 6f  between amount o
34f61 66 0a 2a 2a 20 64 61 74 61 20 63 75 72 72 65 6e  f.** data curren
34f62 74 6c 79 20 73 74 6f 72 65 64 20 28 6e 44 61 74  tly stored (nDat
34f63 61 29 20 61 6e 64 20 62 75 66 66 65 72 20 63 61  a) and buffer ca
34f64 70 61 63 69 74 79 20 28 6e 43 61 70 61 63 69 74  pacity (nCapacit
34f65 79 29 2e 0a 2a 2a 0a 2a 2a 20 64 61 74 61 42 75  y)..**.** dataBu
34f66 66 66 65 72 49 6e 69 74 20 2d 20 63 72 65 61 74  fferInit - creat
34f67 65 20 61 20 62 75 66 66 65 72 20 77 69 74 68 20  e a buffer with 
34f68 67 69 76 65 6e 20 69 6e 69 74 69 61 6c 20 63 61  given initial ca
34f69 70 61 63 69 74 79 2e 0a 2a 2a 20 64 61 74 61 42  pacity..** dataB
34f6a 75 66 66 65 72 52 65 73 65 74 20 2d 20 66 6f 72  ufferReset - for
34f6b 67 65 74 20 62 75 66 66 65 72 27 73 20 64 61 74  get buffer's dat
34f6c 61 2c 20 72 65 74 61 69 6e 69 6e 67 20 63 61 70  a, retaining cap
34f6d 61 63 69 74 79 2e 0a 2a 2a 20 64 61 74 61 42 75  acity..** dataBu
34f6e 66 66 65 72 44 65 73 74 72 6f 79 20 2d 20 66 72  fferDestroy - fr
34f6f 65 65 20 62 75 66 66 65 72 27 73 20 64 61 74 61  ee buffer's data
34f70 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 53  ..** dataBufferS
34f71 77 61 70 20 2d 20 73 77 61 70 20 63 6f 6e 74 65  wap - swap conte
34f72 6e 74 73 20 6f 66 20 74 77 6f 20 62 75 66 66 65  nts of two buffe
34f73 72 73 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65  rs..** dataBuffe
34f74 72 45 78 70 61 6e 64 20 2d 20 65 78 70 61 6e 64  rExpand - expand
34f75 20 63 61 70 61 63 69 74 79 20 77 69 74 68 6f 75   capacity withou
34f76 74 20 61 64 64 69 6e 67 20 64 61 74 61 2e 0a 2a  t adding data..*
34f77 2a 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65  * dataBufferAppe
34f78 6e 64 20 2d 20 61 70 70 65 6e 64 20 64 61 74 61  nd - append data
34f79 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 41  ..** dataBufferA
34f7a 70 70 65 6e 64 32 20 2d 20 61 70 70 65 6e 64 20  ppend2 - append 
34f7b 74 77 6f 20 70 69 65 63 65 73 20 6f 66 20 64 61  two pieces of da
34f7c 74 61 20 61 74 20 6f 6e 63 65 2e 0a 2a 2a 20 64  ta at once..** d
34f7d 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65  ataBufferReplace
34f7e 20 2d 20 72 65 70 6c 61 63 65 20 62 75 66 66 65   - replace buffe
34f7f 72 27 73 20 64 61 74 61 2e 0a 2a 2f 0a 74 79 70  r's data..*/.typ
34f80 65 64 65 66 20 73 74 72 75 63 74 20 44 61 74 61  edef struct Data
34f81 42 75 66 66 65 72 20 7b 0a 20 20 63 68 61 72 20  Buffer {.  char 
34f82 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  *pData;         
34f83 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d   /* Pointer to m
34f84 61 6c 6c 6f 63 27 65 64 20 62 75 66 66 65 72 2e  alloc'ed buffer.
34f85 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61 70 61 63   */.  int nCapac
34f86 69 74 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  ity;        /* S
34f87 69 7a 65 20 6f 66 20 70 44 61 74 61 20 62 75 66  ize of pData buf
34f88 66 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 44  fer. */.  int nD
34f89 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
34f8a 2f 2a 20 45 6e 64 20 6f 66 20 64 61 74 61 20 6c  /* End of data l
34f8b 6f 61 64 65 64 20 69 6e 74 6f 20 70 44 61 74 61  oaded into pData
34f8c 2e 20 2a 2f 0a 7d 20 44 61 74 61 42 75 66 66 65  . */.} DataBuffe
34f8d 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
34f8e 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 44  dataBufferInit(D
34f8f 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66  ataBuffer *pBuff
34f90 65 72 2c 20 69 6e 74 20 6e 43 61 70 61 63 69 74  er, int nCapacit
34f91 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6e 43  y){.  assert( nC
34f92 61 70 61 63 69 74 79 3e 3d 30 20 29 3b 0a 20 20  apacity>=0 );.  
34f93 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 20 3d  pBuffer->nData =
34f94 20 30 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e   0;.  pBuffer->n
34f95 43 61 70 61 63 69 74 79 20 3d 20 6e 43 61 70 61  Capacity = nCapa
34f96 63 69 74 79 3b 0a 20 20 70 42 75 66 66 65 72 2d  city;.  pBuffer-
34f97 3e 70 44 61 74 61 20 3d 20 6e 43 61 70 61 63 69  >pData = nCapaci
34f98 74 79 3d 3d 30 20 3f 20 4e 55 4c 4c 20 3a 20 73  ty==0 ? NULL : s
34f99 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43  qlite3_malloc(nC
34f9a 61 70 61 63 69 74 79 29 3b 0a 7d 0a 73 74 61 74  apacity);.}.stat
34f9b 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66  ic void dataBuff
34f9c 65 72 52 65 73 65 74 28 44 61 74 61 42 75 66 66  erReset(DataBuff
34f9d 65 72 20 2a 70 42 75 66 66 65 72 29 7b 0a 20 20  er *pBuffer){.  
34f9e 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 20 3d  pBuffer->nData =
34f9f 20 30 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69   0;.}.static voi
34fa0 64 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74  d dataBufferDest
34fa1 72 6f 79 28 44 61 74 61 42 75 66 66 65 72 20 2a  roy(DataBuffer *
34fa2 70 42 75 66 66 65 72 29 7b 0a 20 20 69 66 28 20  pBuffer){.  if( 
34fa3 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 21 3d  pBuffer->pData!=
34fa4 4e 55 4c 4c 20 29 20 73 71 6c 69 74 65 33 5f 66  NULL ) sqlite3_f
34fa5 72 65 65 28 70 42 75 66 66 65 72 2d 3e 70 44 61  ree(pBuffer->pDa
34fa6 74 61 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28  ta);.  SCRAMBLE(
34fa7 70 42 75 66 66 65 72 29 3b 0a 7d 0a 73 74 61 74  pBuffer);.}.stat
34fa8 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66  ic void dataBuff
34fa9 65 72 53 77 61 70 28 44 61 74 61 42 75 66 66 65  erSwap(DataBuffe
34faa 72 20 2a 70 42 75 66 66 65 72 31 2c 20 44 61 74  r *pBuffer1, Dat
34fab 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
34fac 32 29 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72  2){.  DataBuffer
34fad 20 74 6d 70 20 3d 20 2a 70 42 75 66 66 65 72 31   tmp = *pBuffer1
34fae 3b 0a 20 20 2a 70 42 75 66 66 65 72 31 20 3d 20  ;.  *pBuffer1 = 
34faf 2a 70 42 75 66 66 65 72 32 3b 0a 20 20 2a 70 42  *pBuffer2;.  *pB
34fb0 75 66 66 65 72 32 20 3d 20 74 6d 70 3b 0a 7d 0a  uffer2 = tmp;.}.
34fb1 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61  static void data
34fb2 42 75 66 66 65 72 45 78 70 61 6e 64 28 44 61 74  BufferExpand(Dat
34fb3 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
34fb4 2c 20 69 6e 74 20 6e 41 64 64 43 61 70 61 63 69  , int nAddCapaci
34fb5 74 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6e  ty){.  assert( n
34fb6 41 64 64 43 61 70 61 63 69 74 79 3e 30 20 29 3b  AddCapacity>0 );
34fb7 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .  /* TODO(shess
34fb8 29 20 43 6f 6e 73 69 64 65 72 20 65 78 70 61 6e  ) Consider expan
34fb9 64 69 6e 67 20 6d 6f 72 65 20 61 67 67 72 65 73  ding more aggres
34fba 73 69 76 65 6c 79 2e 20 20 4e 6f 74 65 20 74 68  sively.  Note th
34fbb 61 74 20 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65  at the.  ** unde
34fbc 72 6c 79 69 6e 67 20 6d 61 6c 6c 6f 63 20 69 6d  rlying malloc im
34fbd 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 61 79  plementation may
34fbe 20 74 61 6b 65 20 63 61 72 65 20 6f 66 20 73 75   take care of su
34fbf 63 68 20 74 68 69 6e 67 73 20 66 6f 72 0a 20 20  ch things for.  
34fc0 2a 2a 20 75 73 20 61 6c 72 65 61 64 79 2e 0a 20  ** us already.. 
34fc1 20 2a 2f 0a 20 20 69 66 28 20 70 42 75 66 66 65   */.  if( pBuffe
34fc2 72 2d 3e 6e 44 61 74 61 2b 6e 41 64 64 43 61 70  r->nData+nAddCap
34fc3 61 63 69 74 79 3e 70 42 75 66 66 65 72 2d 3e 6e  acity>pBuffer->n
34fc4 43 61 70 61 63 69 74 79 20 29 7b 0a 20 20 20 20  Capacity ){.    
34fc5 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69  pBuffer->nCapaci
34fc6 74 79 20 3d 20 70 42 75 66 66 65 72 2d 3e 6e 44  ty = pBuffer->nD
34fc7 61 74 61 2b 6e 41 64 64 43 61 70 61 63 69 74 79  ata+nAddCapacity
34fc8 3b 0a 20 20 20 20 70 42 75 66 66 65 72 2d 3e 70  ;.    pBuffer->p
34fc9 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 72  Data = sqlite3_r
34fca 65 61 6c 6c 6f 63 28 70 42 75 66 66 65 72 2d 3e  ealloc(pBuffer->
34fcb 70 44 61 74 61 2c 20 70 42 75 66 66 65 72 2d 3e  pData, pBuffer->
34fcc 6e 43 61 70 61 63 69 74 79 29 3b 0a 20 20 7d 0a  nCapacity);.  }.
34fcd 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61  }.static void da
34fce 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 44  taBufferAppend(D
34fcf 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66  ataBuffer *pBuff
34fd0 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
34fd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fd2 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 6f   const char *pSo
34fd3 75 72 63 65 2c 20 69 6e 74 20 6e 53 6f 75 72 63  urce, int nSourc
34fd4 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6e 53  e){.  assert( nS
34fd5 6f 75 72 63 65 3e 30 20 26 26 20 70 53 6f 75 72  ource>0 && pSour
34fd6 63 65 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 64 61  ce!=NULL );.  da
34fd7 74 61 42 75 66 66 65 72 45 78 70 61 6e 64 28 70  taBufferExpand(p
34fd8 42 75 66 66 65 72 2c 20 6e 53 6f 75 72 63 65 29  Buffer, nSource)
34fd9 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 75 66 66  ;.  memcpy(pBuff
34fda 65 72 2d 3e 70 44 61 74 61 2b 70 42 75 66 66 65  er->pData+pBuffe
34fdb 72 2d 3e 6e 44 61 74 61 2c 20 70 53 6f 75 72 63  r->nData, pSourc
34fdc 65 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 20 20 70  e, nSource);.  p
34fdd 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 20 2b 3d  Buffer->nData +=
34fde 20 6e 53 6f 75 72 63 65 3b 0a 7d 0a 73 74 61 74   nSource;.}.stat
34fdf 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66  ic void dataBuff
34fe0 65 72 41 70 70 65 6e 64 32 28 44 61 74 61 42 75  erAppend2(DataBu
34fe1 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20  ffer *pBuffer,. 
34fe2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fe3 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
34fe4 73 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63 65  st char *pSource
34fe5 31 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65 31 2c  1, int nSource1,
34fe6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34fe7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
34fe8 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 6f 75 72  onst char *pSour
34fe9 63 65 32 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65  ce2, int nSource
34fea 32 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6e 53  2){.  assert( nS
34feb 6f 75 72 63 65 31 3e 30 20 26 26 20 70 53 6f 75  ource1>0 && pSou
34fec 72 63 65 31 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20  rce1!=NULL );.  
34fed 61 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65 32  assert( nSource2
34fee 3e 30 20 26 26 20 70 53 6f 75 72 63 65 32 21 3d  >0 && pSource2!=
34fef 4e 55 4c 4c 20 29 3b 0a 20 20 64 61 74 61 42 75  NULL );.  dataBu
34ff0 66 66 65 72 45 78 70 61 6e 64 28 70 42 75 66 66  fferExpand(pBuff
34ff1 65 72 2c 20 6e 53 6f 75 72 63 65 31 2b 6e 53 6f  er, nSource1+nSo
34ff2 75 72 63 65 32 29 3b 0a 20 20 6d 65 6d 63 70 79  urce2);.  memcpy
34ff3 28 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 2b  (pBuffer->pData+
34ff4 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2c 20  pBuffer->nData, 
34ff5 70 53 6f 75 72 63 65 31 2c 20 6e 53 6f 75 72 63  pSource1, nSourc
34ff6 65 31 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42  e1);.  memcpy(pB
34ff7 75 66 66 65 72 2d 3e 70 44 61 74 61 2b 70 42 75  uffer->pData+pBu
34ff8 66 66 65 72 2d 3e 6e 44 61 74 61 2b 6e 53 6f 75  ffer->nData+nSou
34ff9 72 63 65 31 2c 20 70 53 6f 75 72 63 65 32 2c 20  rce1, pSource2, 
34ffa 6e 53 6f 75 72 63 65 32 29 3b 0a 20 20 70 42 75  nSource2);.  pBu
34ffb 66 66 65 72 2d 3e 6e 44 61 74 61 20 2b 3d 20 6e  ffer->nData += n
34ffc 53 6f 75 72 63 65 31 2b 6e 53 6f 75 72 63 65 32  Source1+nSource2
34ffd 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
34ffe 64 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63  dataBufferReplac
34fff 65 28 44 61 74 61 42 75 66 66 65 72 20 2a 70 42  e(DataBuffer *pB
35000 75 66 66 65 72 2c 0a 20 20 20 20 20 20 20 20 20  uffer,.         
35001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35002 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
35003 2a 70 53 6f 75 72 63 65 2c 20 69 6e 74 20 6e 53  *pSource, int nS
35004 6f 75 72 63 65 29 7b 0a 20 20 64 61 74 61 42 75  ource){.  dataBu
35005 66 66 65 72 52 65 73 65 74 28 70 42 75 66 66 65  fferReset(pBuffe
35006 72 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72  r);.  dataBuffer
35007 41 70 70 65 6e 64 28 70 42 75 66 66 65 72 2c 20  Append(pBuffer, 
35008 70 53 6f 75 72 63 65 2c 20 6e 53 6f 75 72 63 65  pSource, nSource
35009 29 3b 0a 7d 0a 0a 2f 2a 20 53 74 72 69 6e 67 42  );.}../* StringB
3500a 75 66 66 65 72 20 69 73 20 61 20 6e 75 6c 6c 2d  uffer is a null-
3500b 74 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69  terminated versi
3500c 6f 6e 20 6f 66 20 44 61 74 61 42 75 66 66 65 72  on of DataBuffer
3500d 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  . */.typedef str
3500e 75 63 74 20 53 74 72 69 6e 67 42 75 66 66 65 72  uct StringBuffer
3500f 20 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72 20   {.  DataBuffer 
35010 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b;            /*
35011 20 49 6e 63 6c 75 64 65 73 20 6e 75 6c 6c 20 74   Includes null t
35012 65 72 6d 69 6e 61 74 6f 72 2e 20 2a 2f 0a 7d 20  erminator. */.} 
35013 53 74 72 69 6e 67 42 75 66 66 65 72 3b 0a 0a 73  StringBuffer;..s
35014 74 61 74 69 63 20 76 6f 69 64 20 69 6e 69 74 53  tatic void initS
35015 74 72 69 6e 67 42 75 66 66 65 72 28 53 74 72 69  tringBuffer(Stri
35016 6e 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a 20  ngBuffer *sb){. 
35017 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
35018 26 73 62 2d 3e 62 2c 20 31 30 30 29 3b 0a 20 20  &sb->b, 100);.  
35019 64 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63  dataBufferReplac
3501a 65 28 26 73 62 2d 3e 62 2c 20 22 22 2c 20 31 29  e(&sb->b, "", 1)
3501b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  ;.}.static int s
3501c 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
3501d 68 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a  h(StringBuffer *
3501e 73 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 62  sb){.  return sb
3501f 2d 3e 62 2e 6e 44 61 74 61 2d 31 3b 0a 7d 0a 73  ->b.nData-1;.}.s
35020 74 61 74 69 63 20 63 68 61 72 20 2a 73 74 72 69  tatic char *stri
35021 6e 67 42 75 66 66 65 72 44 61 74 61 28 53 74 72  ngBufferData(Str
35022 69 6e 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a  ingBuffer *sb){.
35023 20 20 72 65 74 75 72 6e 20 73 62 2d 3e 62 2e 70    return sb->b.p
35024 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20 76  Data;.}.static v
35025 6f 69 64 20 73 74 72 69 6e 67 42 75 66 66 65 72  oid stringBuffer
35026 44 65 73 74 72 6f 79 28 53 74 72 69 6e 67 42 75  Destroy(StringBu
35027 66 66 65 72 20 2a 73 62 29 7b 0a 20 20 64 61 74  ffer *sb){.  dat
35028 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
35029 73 62 2d 3e 62 29 3b 0a 7d 0a 0a 73 74 61 74 69  sb->b);.}..stati
3502a 63 20 76 6f 69 64 20 6e 61 70 70 65 6e 64 28 53  c void nappend(S
3502b 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62 2c  tringBuffer *sb,
3502c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 72   const char *zFr
3502d 6f 6d 2c 20 69 6e 74 20 6e 46 72 6f 6d 29 7b 0a  om, int nFrom){.
3502e 20 20 61 73 73 65 72 74 28 20 73 62 2d 3e 62 2e    assert( sb->b.
3502f 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 69 66 28  nData>0 );.  if(
35030 20 6e 46 72 6f 6d 3e 30 20 29 7b 0a 20 20 20 20   nFrom>0 ){.    
35031 73 62 2d 3e 62 2e 6e 44 61 74 61 2d 2d 3b 0a 20  sb->b.nData--;. 
35032 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
35033 65 6e 64 32 28 26 73 62 2d 3e 62 2c 20 7a 46 72  end2(&sb->b, zFr
35034 6f 6d 2c 20 6e 46 72 6f 6d 2c 20 22 22 2c 20 31  om, nFrom, "", 1
35035 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20  );.  }.}.static 
35036 76 6f 69 64 20 61 70 70 65 6e 64 28 53 74 72 69  void append(Stri
35037 6e 67 42 75 66 66 65 72 20 2a 73 62 2c 20 63 6f  ngBuffer *sb, co
35038 6e 73 74 20 63 68 61 72 20 2a 7a 46 72 6f 6d 29  nst char *zFrom)
35039 7b 0a 20 20 6e 61 70 70 65 6e 64 28 73 62 2c 20  {.  nappend(sb, 
3503a 7a 46 72 6f 6d 2c 20 73 74 72 6c 65 6e 28 7a 46  zFrom, strlen(zF
3503b 72 6f 6d 29 29 3b 0a 7d 0a 0a 2f 2a 20 41 70 70  rom));.}../* App
3503c 65 6e 64 20 61 20 6c 69 73 74 20 6f 66 20 73 74  end a list of st
3503d 72 69 6e 67 73 20 73 65 70 61 72 61 74 65 64 20  rings separated 
3503e 62 79 20 63 6f 6d 6d 61 73 2e 20 2a 2f 0a 73 74  by commas. */.st
3503f 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64  atic void append
35040 4c 69 73 74 28 53 74 72 69 6e 67 42 75 66 66 65  List(StringBuffe
35041 72 20 2a 73 62 2c 20 69 6e 74 20 6e 53 74 72 69  r *sb, int nStri
35042 6e 67 2c 20 63 68 61 72 20 2a 2a 61 7a 53 74 72  ng, char **azStr
35043 69 6e 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ing){.  int i;. 
35044 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 74 72   for(i=0; i<nStr
35045 69 6e 67 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 69  ing; ++i){.    i
35046 66 28 20 69 3e 30 20 29 20 61 70 70 65 6e 64 28  f( i>0 ) append(
35047 73 62 2c 20 22 2c 20 22 29 3b 0a 20 20 20 20 61  sb, ", ");.    a
35048 70 70 65 6e 64 28 73 62 2c 20 61 7a 53 74 72 69  ppend(sb, azStri
35049 6e 67 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 73  ng[i]);.  }.}..s
3504a 74 61 74 69 63 20 69 6e 74 20 65 6e 64 73 49 6e  tatic int endsIn
3504b 57 68 69 74 65 53 70 61 63 65 28 53 74 72 69 6e  WhiteSpace(Strin
3504c 67 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20 72  gBuffer *p){.  r
3504d 65 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66 66  eturn stringBuff
3504e 65 72 4c 65 6e 67 74 68 28 70 29 3e 30 20 26 26  erLength(p)>0 &&
3504f 0a 20 20 20 20 73 61 66 65 5f 69 73 73 70 61 63  .    safe_isspac
35050 65 28 73 74 72 69 6e 67 42 75 66 66 65 72 44 61  e(stringBufferDa
35051 74 61 28 70 29 5b 73 74 72 69 6e 67 42 75 66 66  ta(p)[stringBuff
35052 65 72 4c 65 6e 67 74 68 28 70 29 2d 31 5d 29 3b  erLength(p)-1]);
35053 0a 7d 0a 0a 2f 2a 20 49 66 20 74 68 65 20 53 74  .}../* If the St
35054 72 69 6e 67 42 75 66 66 65 72 20 65 6e 64 73 20  ringBuffer ends 
35055 69 6e 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68  in something oth
35056 65 72 20 74 68 61 6e 20 77 68 69 74 65 20 73 70  er than white sp
35057 61 63 65 2c 20 61 64 64 20 61 0a 2a 2a 20 73 69  ace, add a.** si
35058 6e 67 6c 65 20 73 70 61 63 65 20 63 68 61 72 61  ngle space chara
35059 63 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 2e  cter to the end.
3505a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3505b 61 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65  appendWhiteSpace
3505c 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 70  (StringBuffer *p
3505d 29 7b 0a 20 20 69 66 28 20 73 74 72 69 6e 67 42  ){.  if( stringB
3505e 75 66 66 65 72 4c 65 6e 67 74 68 28 70 29 3d 3d  ufferLength(p)==
3505f 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
35060 28 20 21 65 6e 64 73 49 6e 57 68 69 74 65 53 70  ( !endsInWhiteSp
35061 61 63 65 28 70 29 20 29 20 61 70 70 65 6e 64 28  ace(p) ) append(
35062 70 2c 20 22 20 22 29 3b 0a 7d 0a 0a 2f 2a 20 52  p, " ");.}../* R
35063 65 6d 6f 76 65 20 77 68 69 74 65 20 73 70 61 63  emove white spac
35064 65 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f  e from the end o
35065 66 20 74 68 65 20 53 74 72 69 6e 67 42 75 66 66  f the StringBuff
35066 65 72 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  er */.static voi
35067 64 20 74 72 69 6d 57 68 69 74 65 53 70 61 63 65  d trimWhiteSpace
35068 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 70  (StringBuffer *p
35069 29 7b 0a 20 20 77 68 69 6c 65 28 20 65 6e 64 73  ){.  while( ends
3506a 49 6e 57 68 69 74 65 53 70 61 63 65 28 70 29 20  InWhiteSpace(p) 
3506b 29 7b 0a 20 20 20 20 70 2d 3e 62 2e 70 44 61 74  ){.    p->b.pDat
3506c 61 5b 2d 2d 70 2d 3e 62 2e 6e 44 61 74 61 2d 31  a[--p->b.nData-1
3506d 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 7d 0a 7d 0a  ] = '\0';.  }.}.
3506e 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3506f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35071 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35072 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 4c 52 65 61 64  *****/./* DLRead
35073 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  er is used to re
35074 61 64 20 64 6f 63 75 6d 65 6e 74 20 65 6c 65 6d  ad document elem
35075 65 6e 74 73 20 66 72 6f 6d 20 61 20 64 6f 63 6c  ents from a docl
35076 69 73 74 2e 20 20 54 68 65 0a 2a 2a 20 63 75 72  ist.  The.** cur
35077 72 65 6e 74 20 64 6f 63 69 64 20 69 73 20 63 61  rent docid is ca
35078 63 68 65 64 2c 20 73 6f 20 64 6c 72 44 6f 63 69  ched, so dlrDoci
35079 64 28 29 20 69 73 20 66 61 73 74 2e 20 20 44 4c  d() is fast.  DL
3507a 52 65 61 64 65 72 20 64 6f 65 73 20 6e 6f 74 0a  Reader does not.
3507b 2a 2a 20 6f 77 6e 20 74 68 65 20 64 6f 63 6c 69  ** own the docli
3507c 73 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a  st buffer..**.**
3507d 20 64 6c 72 41 74 45 6e 64 20 2d 20 74 72 75 65   dlrAtEnd - true
3507e 20 69 66 20 74 68 65 72 65 27 73 20 6e 6f 20 6d   if there's no m
3507f 6f 72 65 20 64 61 74 61 20 74 6f 20 72 65 61 64  ore data to read
35080 2e 0a 2a 2a 20 64 6c 72 44 6f 63 69 64 20 2d 20  ..** dlrDocid - 
35081 64 6f 63 69 64 20 6f 66 20 63 75 72 72 65 6e 74  docid of current
35082 20 64 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c   document..** dl
35083 72 44 6f 63 44 61 74 61 20 2d 20 64 6f 63 6c 69  rDocData - docli
35084 73 74 20 64 61 74 61 20 66 6f 72 20 63 75 72 72  st data for curr
35085 65 6e 74 20 64 6f 63 75 6d 65 6e 74 20 28 69 6e  ent document (in
35086 63 6c 75 64 69 6e 67 20 64 6f 63 69 64 29 2e 0a  cluding docid)..
35087 2a 2a 20 64 6c 72 44 6f 63 44 61 74 61 42 79 74  ** dlrDocDataByt
35088 65 73 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 73  es - length of s
35089 61 6d 65 2e 0a 2a 2a 20 64 6c 72 41 6c 6c 44 61  ame..** dlrAllDa
3508a 74 61 42 79 74 65 73 20 2d 20 6c 65 6e 67 74 68  taBytes - length
3508b 20 6f 66 20 61 6c 6c 20 72 65 6d 61 69 6e 69 6e   of all remainin
3508c 67 20 64 61 74 61 2e 0a 2a 2a 20 64 6c 72 50 6f  g data..** dlrPo
3508d 73 44 61 74 61 20 2d 20 70 6f 73 69 74 69 6f 6e  sData - position
3508e 20 64 61 74 61 20 66 6f 72 20 63 75 72 72 65 6e   data for curren
3508f 74 20 64 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20 64  t document..** d
35090 6c 72 50 6f 73 44 61 74 61 4c 65 6e 20 2d 20 6c  lrPosDataLen - l
35091 65 6e 67 74 68 20 6f 66 20 70 6f 73 20 64 61 74  ength of pos dat
35092 61 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64 6f  a for current do
35093 63 75 6d 65 6e 74 20 28 69 6e 63 6c 20 50 4f 53  cument (incl POS
35094 5f 45 4e 44 29 2e 0a 2a 2a 20 64 6c 72 53 74 65  _END)..** dlrSte
35095 70 20 2d 20 73 74 65 70 20 74 6f 20 63 75 72 72  p - step to curr
35096 65 6e 74 20 64 6f 63 75 6d 65 6e 74 2e 0a 2a 2a  ent document..**
35097 20 64 6c 72 49 6e 69 74 20 2d 20 69 6e 69 74 69   dlrInit - initi
35098 61 6c 20 66 6f 72 20 64 6f 63 6c 69 73 74 20 6f  al for doclist o
35099 66 20 67 69 76 65 6e 20 74 79 70 65 20 61 67 61  f given type aga
3509a 69 6e 73 74 20 67 69 76 65 6e 20 64 61 74 61 2e  inst given data.
3509b 0a 2a 2a 20 64 6c 72 44 65 73 74 72 6f 79 20 2d  .** dlrDestroy -
3509c 20 63 6c 65 61 6e 20 75 70 2e 0a 2a 2a 0a 2a 2a   clean up..**.**
3509d 20 45 78 70 65 63 74 65 64 20 75 73 61 67 65 20   Expected usage 
3509e 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b  is something lik
3509f 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 44 4c 52 65 61  e:.**.**   DLRea
350a0 64 65 72 20 72 65 61 64 65 72 3b 0a 2a 2a 20 20  der reader;.**  
350a1 20 64 6c 72 49 6e 69 74 28 26 72 65 61 64 65 72   dlrInit(&reader
350a2 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b  , pData, nData);
350a3 0a 2a 2a 20 20 20 77 68 69 6c 65 28 20 21 64 6c  .**   while( !dl
350a4 72 41 74 45 6e 64 28 26 72 65 61 64 65 72 29 20  rAtEnd(&reader) 
350a5 29 7b 0a 2a 2a 20 20 20 20 20 2f 2f 20 63 61 6c  ){.**     // cal
350a6 6c 73 20 74 6f 20 64 6c 72 44 6f 63 69 64 28 29  ls to dlrDocid()
350a7 20 61 6e 64 20 6b 69 6e 2e 0a 2a 2a 20 20 20 20   and kin..**    
350a8 20 64 6c 72 53 74 65 70 28 26 72 65 61 64 65 72   dlrStep(&reader
350a9 29 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 64  );.**   }.**   d
350aa 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65  lrDestroy(&reade
350ab 72 29 3b 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  r);.*/.typedef s
350ac 74 72 75 63 74 20 44 4c 52 65 61 64 65 72 20 7b  truct DLReader {
350ad 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69  .  DocListType i
350ae 54 79 70 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68  Type;.  const ch
350af 61 72 20 2a 70 44 61 74 61 3b 0a 20 20 69 6e 74  ar *pData;.  int
350b0 20 6e 44 61 74 61 3b 0a 0a 20 20 73 71 6c 69 74   nData;..  sqlit
350b1 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 3b 0a  e_int64 iDocid;.
350b2 20 20 69 6e 74 20 6e 45 6c 65 6d 65 6e 74 3b 0a    int nElement;.
350b3 7d 20 44 4c 52 65 61 64 65 72 3b 0a 0a 73 74 61  } DLReader;..sta
350b4 74 69 63 20 69 6e 74 20 64 6c 72 41 74 45 6e 64  tic int dlrAtEnd
350b5 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64  (DLReader *pRead
350b6 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  er){.  assert( p
350b7 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3e 3d 30  Reader->nData>=0
350b8 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
350b9 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 3b 0a  ader->nData==0;.
350ba 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f  }.static sqlite_
350bb 69 6e 74 36 34 20 64 6c 72 44 6f 63 69 64 28 44  int64 dlrDocid(D
350bc 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
350bd 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c  ){.  assert( !dl
350be 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
350bf 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
350c0 64 65 72 2d 3e 69 44 6f 63 69 64 3b 0a 7d 0a 73  der->iDocid;.}.s
350c1 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
350c2 20 2a 64 6c 72 44 6f 63 44 61 74 61 28 44 4c 52   *dlrDocData(DLR
350c3 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
350c4 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41  .  assert( !dlrA
350c5 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
350c6 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
350c7 72 2d 3e 70 44 61 74 61 3b 0a 7d 0a 73 74 61 74  r->pData;.}.stat
350c8 69 63 20 69 6e 74 20 64 6c 72 44 6f 63 44 61 74  ic int dlrDocDat
350c9 61 42 79 74 65 73 28 44 4c 52 65 61 64 65 72 20  aBytes(DLReader 
350ca 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
350cb 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70  ert( !dlrAtEnd(p
350cc 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74  Reader) );.  ret
350cd 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c  urn pReader->nEl
350ce 65 6d 65 6e 74 3b 0a 7d 0a 73 74 61 74 69 63 20  ement;.}.static 
350cf 69 6e 74 20 64 6c 72 41 6c 6c 44 61 74 61 42 79  int dlrAllDataBy
350d0 74 65 73 28 44 4c 52 65 61 64 65 72 20 2a 70 52  tes(DLReader *pR
350d1 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
350d2 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61  ( !dlrAtEnd(pRea
350d3 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
350d4 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3b   pReader->nData;
350d5 0a 7d 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .}./* TODO(shess
350d6 29 20 43 6f 6e 73 69 64 65 72 20 61 64 64 69 6e  ) Consider addin
350d7 67 20 61 20 66 69 65 6c 64 20 74 6f 20 74 72 61  g a field to tra
350d8 63 6b 20 69 44 6f 63 69 64 20 76 61 72 69 6e 74  ck iDocid varint
350d9 20 6c 65 6e 67 74 68 0a 2a 2a 20 74 6f 20 6d 61   length.** to ma
350da 6b 65 20 74 68 65 73 65 20 74 77 6f 20 66 75 6e  ke these two fun
350db 63 74 69 6f 6e 73 20 66 61 73 74 65 72 2e 20 20  ctions faster.  
350dc 54 68 69 73 20 6d 69 67 68 74 20 6d 61 74 74 65  This might matte
350dd 72 20 28 61 20 74 69 6e 79 20 62 69 74 29 0a 2a  r (a tiny bit).*
350de 2a 20 66 6f 72 20 71 75 65 72 69 65 73 2e 0a 2a  * for queries..*
350df 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
350e0 68 61 72 20 2a 64 6c 72 50 6f 73 44 61 74 61 28  har *dlrPosData(
350e1 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  DLReader *pReade
350e2 72 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  r){.  sqlite_int
350e3 36 34 20 69 44 75 6d 6d 79 3b 0a 20 20 69 6e 74  64 iDummy;.  int
350e4 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
350e5 6e 74 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74  nt(pReader->pDat
350e6 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61  a, &iDummy);.  a
350e7 73 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64  ssert( !dlrAtEnd
350e8 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72  (pReader) );.  r
350e9 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 70  eturn pReader->p
350ea 44 61 74 61 2b 6e 3b 0a 7d 0a 73 74 61 74 69 63  Data+n;.}.static
350eb 20 69 6e 74 20 64 6c 72 50 6f 73 44 61 74 61 4c   int dlrPosDataL
350ec 65 6e 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65  en(DLReader *pRe
350ed 61 64 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 5f  ader){.  sqlite_
350ee 69 6e 74 36 34 20 69 44 75 6d 6d 79 3b 0a 20 20  int64 iDummy;.  
350ef 69 6e 74 20 6e 20 3d 20 66 74 73 33 47 65 74 56  int n = fts3GetV
350f0 61 72 69 6e 74 28 70 52 65 61 64 65 72 2d 3e 70  arint(pReader->p
350f1 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
350f2 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41 74    assert( !dlrAt
350f3 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
350f4 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
350f5 2d 3e 6e 45 6c 65 6d 65 6e 74 2d 6e 3b 0a 7d 0a  ->nElement-n;.}.
350f6 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 72 53  static void dlrS
350f7 74 65 70 28 44 4c 52 65 61 64 65 72 20 2a 70 52  tep(DLReader *pR
350f8 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
350f9 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61  ( !dlrAtEnd(pRea
350fa 64 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 53 6b  der) );..  /* Sk
350fb 69 70 20 70 61 73 74 20 63 75 72 72 65 6e 74 20  ip past current 
350fc 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e 74 2e  doclist element.
350fd 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 52   */.  assert( pR
350fe 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3c  eader->nElement<
350ff 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  =pReader->nData 
35100 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44  );.  pReader->pD
35101 61 74 61 20 2b 3d 20 70 52 65 61 64 65 72 2d 3e  ata += pReader->
35102 6e 45 6c 65 6d 65 6e 74 3b 0a 20 20 70 52 65 61  nElement;.  pRea
35103 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 70 52  der->nData -= pR
35104 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b  eader->nElement;
35105 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
35106 69 73 20 6d 6f 72 65 20 64 61 74 61 2c 20 72 65  is more data, re
35107 61 64 20 74 68 65 20 6e 65 78 74 20 64 6f 63 6c  ad the next docl
35108 69 73 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a  ist element. */.
35109 20 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 6e    if( pReader->n
3510a 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 73  Data!=0 ){.    s
3510b 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
3510c 69 64 44 65 6c 74 61 3b 0a 20 20 20 20 69 6e 74  idDelta;.    int
3510d 20 69 44 75 6d 6d 79 2c 20 6e 20 3d 20 66 74 73   iDummy, n = fts
3510e 33 47 65 74 56 61 72 69 6e 74 28 70 52 65 61 64  3GetVarint(pRead
3510f 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 44 6f 63  er->pData, &iDoc
35110 69 64 44 65 6c 74 61 29 3b 0a 20 20 20 20 70 52  idDelta);.    pR
35111 65 61 64 65 72 2d 3e 69 44 6f 63 69 64 20 2b 3d  eader->iDocid +=
35112 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a 20 20   iDocidDelta;.  
35113 20 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 69    if( pReader->i
35114 54 79 70 65 3e 3d 44 4c 5f 50 4f 53 49 54 49 4f  Type>=DL_POSITIO
35115 4e 53 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  NS ){.      asse
35116 72 74 28 20 6e 3c 70 52 65 61 64 65 72 2d 3e 6e  rt( n<pReader->n
35117 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20 77 68  Data );.      wh
35118 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 20 20  ile( 1 ){.      
35119 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
3511a 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
3511b 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79  pData+n, &iDummy
3511c 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
3511d 74 28 20 6e 3c 3d 70 52 65 61 64 65 72 2d 3e 6e  t( n<=pReader->n
3511e 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20 20 20  Data );.        
3511f 69 66 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f  if( iDummy==POS_
35120 45 4e 44 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  END ) break;.   
35121 20 20 20 20 20 69 66 28 20 69 44 75 6d 6d 79 3d       if( iDummy=
35122 3d 50 4f 53 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20  =POS_COLUMN ){. 
35123 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74           n += ft
35124 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
35125 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20  eader->pData+n, 
35126 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  &iDummy);.      
35127 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70 52      assert( n<pR
35128 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a  eader->nData );.
35129 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
3512a 28 20 70 52 65 61 64 65 72 2d 3e 69 54 79 70 65  ( pReader->iType
3512b 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  ==DL_POSITIONS_O
3512c 46 46 53 45 54 53 20 29 7b 0a 20 20 20 20 20 20  FFSETS ){.      
3512d 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
3512e 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
3512f 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d  ->pData+n, &iDum
35130 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e  my);.          n
35131 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
35132 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
35133 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  ta+n, &iDummy);.
35134 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
35135 28 20 6e 3c 70 52 65 61 64 65 72 2d 3e 6e 44 61  ( n<pReader->nDa
35136 74 61 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ta );.        }.
35137 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
35138 20 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d    pReader->nElem
35139 65 6e 74 20 3d 20 6e 3b 0a 20 20 20 20 61 73 73  ent = n;.    ass
3513a 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e 6e 45  ert( pReader->nE
3513b 6c 65 6d 65 6e 74 3c 3d 70 52 65 61 64 65 72 2d  lement<=pReader-
3513c 3e 6e 44 61 74 61 20 29 3b 0a 20 20 7d 0a 7d 0a  >nData );.  }.}.
3513d 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 72 49  static void dlrI
3513e 6e 69 74 28 44 4c 52 65 61 64 65 72 20 2a 70 52  nit(DLReader *pR
3513f 65 61 64 65 72 2c 20 44 6f 63 4c 69 73 74 54 79  eader, DocListTy
35140 70 65 20 69 54 79 70 65 2c 0a 20 20 20 20 20 20  pe iType,.      
35141 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
35142 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c  nst char *pData,
35143 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 61   int nData){.  a
35144 73 73 65 72 74 28 20 70 44 61 74 61 21 3d 4e 55  ssert( pData!=NU
35145 4c 4c 20 26 26 20 6e 44 61 74 61 21 3d 30 20 29  LL && nData!=0 )
35146 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 54 79  ;.  pReader->iTy
35147 70 65 20 3d 20 69 54 79 70 65 3b 0a 20 20 70 52  pe = iType;.  pR
35148 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 70  eader->pData = p
35149 44 61 74 61 3b 0a 20 20 70 52 65 61 64 65 72 2d  Data;.  pReader-
3514a 3e 6e 44 61 74 61 20 3d 20 6e 44 61 74 61 3b 0a  >nData = nData;.
3514b 20 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d    pReader->nElem
3514c 65 6e 74 20 3d 20 30 3b 0a 20 20 70 52 65 61 64  ent = 0;.  pRead
3514d 65 72 2d 3e 69 44 6f 63 69 64 20 3d 20 30 3b 0a  er->iDocid = 0;.
3514e 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 66  .  /* Load the f
3514f 69 72 73 74 20 65 6c 65 6d 65 6e 74 27 73 20 64  irst element's d
35150 61 74 61 2e 20 20 54 68 65 72 65 20 6d 75 73 74  ata.  There must
35151 20 62 65 20 61 20 66 69 72 73 74 20 65 6c 65 6d   be a first elem
35152 65 6e 74 2e 20 2a 2f 0a 20 20 64 6c 72 53 74 65  ent. */.  dlrSte
35153 70 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74  p(pReader);.}.st
35154 61 74 69 63 20 76 6f 69 64 20 64 6c 72 44 65 73  atic void dlrDes
35155 74 72 6f 79 28 44 4c 52 65 61 64 65 72 20 2a 70  troy(DLReader *p
35156 52 65 61 64 65 72 29 7b 0a 20 20 53 43 52 41 4d  Reader){.  SCRAM
35157 42 4c 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a  BLE(pReader);.}.
35158 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
35159 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
3515a 68 65 20 64 6f 63 6c 69 73 74 20 63 61 6e 20 62  he doclist can b
3515b 65 20 76 61 6c 69 64 6c 79 20 64 65 63 6f 64 65  e validly decode
3515c 64 2e 20 20 41 6c 73 6f 20 72 65 74 75 72 6e 73  d.  Also returns
3515d 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 6f 63   the.** last doc
3515e 69 64 20 66 6f 75 6e 64 20 62 65 63 61 75 73 65  id found because
3515f 20 69 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e   it is convenien
35160 74 20 69 6e 20 6f 74 68 65 72 20 61 73 73 65 72  t in other asser
35161 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 44 4c 57  tions for.** DLW
35162 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  riter..*/.static
35163 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 56 61 6c   void docListVal
35164 69 64 61 74 65 28 44 6f 63 4c 69 73 74 54 79 70  idate(DocListTyp
35165 65 20 69 54 79 70 65 2c 20 63 6f 6e 73 74 20 63  e iType, const c
35166 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
35167 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
35168 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35169 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
3516a 2a 70 4c 61 73 74 44 6f 63 69 64 29 7b 0a 20 20  *pLastDocid){.  
3516b 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 50 72  sqlite_int64 iPr
3516c 65 76 44 6f 63 69 64 20 3d 20 30 3b 0a 20 20 61  evDocid = 0;.  a
3516d 73 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29  ssert( nData>0 )
3516e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74  ;.  assert( pDat
3516f 61 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  a!=0 );.  assert
35170 28 20 70 44 61 74 61 2b 6e 44 61 74 61 3e 70 44  ( pData+nData>pD
35171 61 74 61 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  ata );.  while( 
35172 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20  nData!=0 ){.    
35173 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
35174 63 69 64 44 65 6c 74 61 3b 0a 20 20 20 20 69 6e  cidDelta;.    in
35175 74 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72  t n = fts3GetVar
35176 69 6e 74 28 70 44 61 74 61 2c 20 26 69 44 6f 63  int(pData, &iDoc
35177 69 64 44 65 6c 74 61 29 3b 0a 20 20 20 20 69 50  idDelta);.    iP
35178 72 65 76 44 6f 63 69 64 20 2b 3d 20 69 44 6f 63  revDocid += iDoc
35179 69 64 44 65 6c 74 61 3b 0a 20 20 20 20 69 66 28  idDelta;.    if(
3517a 20 69 54 79 70 65 3e 44 4c 5f 44 4f 43 49 44 53   iType>DL_DOCIDS
3517b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44   ){.      int iD
3517c 75 6d 6d 79 3b 0a 20 20 20 20 20 20 77 68 69 6c  ummy;.      whil
3517d 65 28 20 31 20 29 7b 0a 20 20 20 20 20 20 20 20  e( 1 ){.        
3517e 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69  n += fts3GetVari
3517f 6e 74 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69  nt32(pData+n, &i
35180 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20  Dummy);.        
35181 69 66 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f  if( iDummy==POS_
35182 45 4e 44 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  END ) break;.   
35183 20 20 20 20 20 69 66 28 20 69 44 75 6d 6d 79 3d       if( iDummy=
35184 3d 50 4f 53 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20  =POS_COLUMN ){. 
35185 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74           n += ft
35186 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
35187 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b  ata+n, &iDummy);
35188 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
35189 66 28 20 69 54 79 70 65 3e 44 4c 5f 50 4f 53 49  f( iType>DL_POSI
3518a 54 49 4f 4e 53 20 29 7b 0a 20 20 20 20 20 20 20  TIONS ){.       
3518b 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56     n += fts3GetV
3518c 61 72 69 6e 74 33 32 28 70 44 61 74 61 2b 6e 2c  arint32(pData+n,
3518d 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20   &iDummy);.     
3518e 20 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65       n += fts3Ge
3518f 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2b  tVarint32(pData+
35190 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20  n, &iDummy);.   
35191 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
35192 73 73 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61 20  ssert( n<=nData 
35193 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
35194 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 3d  .    assert( n<=
35195 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44 61  nData );.    pDa
35196 74 61 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 44 61  ta += n;.    nDa
35197 74 61 20 2d 3d 20 6e 3b 0a 20 20 7d 0a 20 20 69  ta -= n;.  }.  i
35198 66 28 20 70 4c 61 73 74 44 6f 63 69 64 20 29 20  f( pLastDocid ) 
35199 2a 70 4c 61 73 74 44 6f 63 69 64 20 3d 20 69 50  *pLastDocid = iP
3519a 72 65 76 44 6f 63 69 64 3b 0a 7d 0a 23 64 65 66  revDocid;.}.#def
3519b 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44  ine ASSERT_VALID
3519c 5f 44 4f 43 4c 49 53 54 28 69 2c 20 70 2c 20 6e  _DOCLIST(i, p, n
3519d 2c 20 6f 29 20 64 6f 63 4c 69 73 74 56 61 6c 69  , o) docListVali
3519e 64 61 74 65 28 69 2c 20 70 2c 20 6e 2c 20 6f 29  date(i, p, n, o)
3519f 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 41  .#else.#define A
351a0 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c  SSERT_VALID_DOCL
351a1 49 53 54 28 69 2c 20 70 2c 20 6e 2c 20 6f 29 20  IST(i, p, n, o) 
351a2 61 73 73 65 72 74 28 20 31 20 29 0a 23 65 6e 64  assert( 1 ).#end
351a3 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
351a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
351a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
351a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
351a7 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 4c 57  ********/./* DLW
351a8 72 69 74 65 72 20 69 73 20 75 73 65 64 20 74 6f  riter is used to
351a9 20 77 72 69 74 65 20 64 6f 63 6c 69 73 74 20 64   write doclist d
351aa 61 74 61 20 74 6f 20 61 20 44 61 74 61 42 75 66  ata to a DataBuf
351ab 66 65 72 2e 20 20 44 4c 57 72 69 74 65 72 0a 2a  fer.  DLWriter.*
351ac 2a 20 61 6c 77 61 79 73 20 61 70 70 65 6e 64 73  * always appends
351ad 20 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 61   to the buffer a
351ae 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6f 77 6e 20  nd does not own 
351af 69 74 2e 0a 2a 2a 0a 2a 2a 20 64 6c 77 49 6e 69  it..**.** dlwIni
351b0 74 20 2d 20 69 6e 69 74 69 61 6c 69 7a 65 20 74  t - initialize t
351b1 6f 20 77 72 69 74 65 20 61 20 67 69 76 65 6e 20  o write a given 
351b2 74 79 70 65 20 64 6f 63 6c 69 73 74 74 6f 20 61  type doclistto a
351b3 20 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 44   buffer..** dlwD
351b4 65 73 74 72 6f 79 20 2d 20 63 6c 65 61 72 20 74  estroy - clear t
351b5 68 65 20 77 72 69 74 65 72 27 73 20 6d 65 6d 6f  he writer's memo
351b6 72 79 2e 20 20 44 6f 65 73 20 6e 6f 74 20 66 72  ry.  Does not fr
351b7 65 65 20 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c  ee buffer..** dl
351b8 77 41 70 70 65 6e 64 20 2d 20 61 70 70 65 6e 64  wAppend - append
351b9 20 72 61 77 20 64 6f 63 6c 69 73 74 20 64 61 74   raw doclist dat
351ba 61 20 74 6f 20 62 75 66 66 65 72 2e 0a 2a 2a 20  a to buffer..** 
351bb 64 6c 77 43 6f 70 79 20 2d 20 63 6f 70 79 20 6e  dlwCopy - copy n
351bc 65 78 74 20 64 6f 63 6c 69 73 74 20 66 72 6f 6d  ext doclist from
351bd 20 72 65 61 64 65 72 20 74 6f 20 77 72 69 74 65   reader to write
351be 72 2e 0a 2a 2a 20 64 6c 77 41 64 64 20 2d 20 63  r..** dlwAdd - c
351bf 6f 6e 73 74 72 75 63 74 20 64 6f 63 6c 69 73 74  onstruct doclist
351c0 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 70 70   element and app
351c1 65 6e 64 20 74 6f 20 62 75 66 66 65 72 2e 0a 2a  end to buffer..*
351c2 2a 20 20 20 20 4f 6e 6c 79 20 61 70 70 6c 79 20  *    Only apply 
351c3 64 6c 77 41 64 64 28 29 20 74 6f 20 44 4c 5f 44  dlwAdd() to DL_D
351c4 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 73 20 28  OCIDS doclists (
351c5 65 6c 73 65 20 75 73 65 20 50 4c 57 72 69 74 65  else use PLWrite
351c6 72 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  r)..*/.typedef s
351c7 74 72 75 63 74 20 44 4c 57 72 69 74 65 72 20 7b  truct DLWriter {
351c8 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69  .  DocListType i
351c9 54 79 70 65 3b 0a 20 20 44 61 74 61 42 75 66 66  Type;.  DataBuff
351ca 65 72 20 2a 62 3b 0a 20 20 73 71 6c 69 74 65 5f  er *b;.  sqlite_
351cb 69 6e 74 36 34 20 69 50 72 65 76 44 6f 63 69 64  int64 iPrevDocid
351cc 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
351cd 0a 20 20 69 6e 74 20 68 61 73 5f 69 50 72 65 76  .  int has_iPrev
351ce 44 6f 63 69 64 3b 0a 23 65 6e 64 69 66 0a 7d 20  Docid;.#endif.} 
351cf 44 4c 57 72 69 74 65 72 3b 0a 0a 73 74 61 74 69  DLWriter;..stati
351d0 63 20 76 6f 69 64 20 64 6c 77 49 6e 69 74 28 44  c void dlwInit(D
351d1 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  LWriter *pWriter
351d2 2c 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54  , DocListType iT
351d3 79 70 65 2c 20 44 61 74 61 42 75 66 66 65 72 20  ype, DataBuffer 
351d4 2a 62 29 7b 0a 20 20 70 57 72 69 74 65 72 2d 3e  *b){.  pWriter->
351d5 62 20 3d 20 62 3b 0a 20 20 70 57 72 69 74 65 72  b = b;.  pWriter
351d6 2d 3e 69 54 79 70 65 20 3d 20 69 54 79 70 65 3b  ->iType = iType;
351d7 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72 65  .  pWriter->iPre
351d8 76 44 6f 63 69 64 20 3d 20 30 3b 0a 23 69 66 6e  vDocid = 0;.#ifn
351d9 64 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57 72  def NDEBUG.  pWr
351da 69 74 65 72 2d 3e 68 61 73 5f 69 50 72 65 76 44  iter->has_iPrevD
351db 6f 63 69 64 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ocid = 0;.#endif
351dc 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  .}.static void d
351dd 6c 77 44 65 73 74 72 6f 79 28 44 4c 57 72 69 74  lwDestroy(DLWrit
351de 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20  er *pWriter){.  
351df 53 43 52 41 4d 42 4c 45 28 70 57 72 69 74 65 72  SCRAMBLE(pWriter
351e0 29 3b 0a 7d 0a 2f 2a 20 69 46 69 72 73 74 44 6f  );.}./* iFirstDo
351e1 63 69 64 20 69 73 20 74 68 65 20 66 69 72 73 74  cid is the first
351e2 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20 64 6f   docid in the do
351e3 63 6c 69 73 74 20 69 6e 20 70 44 61 74 61 2e 20  clist in pData. 
351e4 20 49 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64   It is.** needed
351e5 20 62 65 63 61 75 73 65 20 70 44 61 74 61 20 6d   because pData m
351e6 61 79 20 70 6f 69 6e 74 20 77 69 74 68 69 6e 20  ay point within 
351e7 61 20 6c 61 72 67 65 72 20 64 6f 63 6c 69 73 74  a larger doclist
351e8 2c 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 63 61  , in which.** ca
351e9 73 65 20 74 68 65 20 66 69 72 73 74 20 69 74 65  se the first ite
351ea 6d 20 77 6f 75 6c 64 20 62 65 20 64 65 6c 74 61  m would be delta
351eb 2d 65 6e 63 6f 64 65 64 2e 0a 2a 2a 0a 2a 2a 20  -encoded..**.** 
351ec 69 4c 61 73 74 44 6f 63 69 64 20 69 73 20 74 68  iLastDocid is th
351ed 65 20 66 69 6e 61 6c 20 64 6f 63 69 64 20 69 6e  e final docid in
351ee 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69 6e 20   the doclist in 
351ef 70 44 61 74 61 2e 20 20 49 74 20 69 73 0a 2a 2a  pData.  It is.**
351f0 20 6e 65 65 64 65 64 20 74 6f 20 63 72 65 61 74   needed to creat
351f1 65 20 74 68 65 20 6e 65 77 20 69 50 72 65 76 44  e the new iPrevD
351f2 6f 63 69 64 20 66 6f 72 20 66 75 74 75 72 65 20  ocid for future 
351f3 64 65 6c 74 61 2d 65 6e 63 6f 64 69 6e 67 2e 20  delta-encoding. 
351f4 20 54 68 65 0a 2a 2a 20 63 6f 64 65 20 63 6f 75   The.** code cou
351f5 6c 64 20 64 65 63 6f 64 65 20 74 68 65 20 70 61  ld decode the pa
351f6 73 73 65 64 20 64 6f 63 6c 69 73 74 20 74 6f 20  ssed doclist to 
351f7 72 65 63 72 65 61 74 65 20 69 4c 61 73 74 44 6f  recreate iLastDo
351f8 63 69 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20  cid, but.** the 
351f9 6f 6e 6c 79 20 63 75 72 72 65 6e 74 20 75 73 65  only current use
351fa 72 20 28 64 6f 63 4c 69 73 74 4d 65 72 67 65 29  r (docListMerge)
351fb 20 61 6c 72 65 61 64 79 20 68 61 73 20 64 65 63   already has dec
351fc 6f 64 65 64 20 74 68 69 73 0a 2a 2a 20 69 6e 66  oded this.** inf
351fd 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20  ormation..*/./* 
351fe 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73  TODO(shess) This
351ff 20 68 61 73 20 62 65 63 6f 6d 65 20 6a 75 73 74   has become just
35200 20 61 20 68 65 6c 70 65 72 20 66 6f 72 20 64 6f   a helper for do
35201 63 4c 69 73 74 4d 65 72 67 65 2e 0a 2a 2a 20 43  cListMerge..** C
35202 6f 6e 73 69 64 65 72 20 61 20 72 65 66 61 63 74  onsider a refact
35203 6f 72 20 74 6f 20 6d 61 6b 65 20 74 68 69 73 20  or to make this 
35204 63 6c 65 61 6e 65 72 2e 0a 2a 2f 0a 73 74 61 74  cleaner..*/.stat
35205 69 63 20 76 6f 69 64 20 64 6c 77 41 70 70 65 6e  ic void dlwAppen
35206 64 28 44 4c 57 72 69 74 65 72 20 2a 70 57 72 69  d(DLWriter *pWri
35207 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ter,.           
35208 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
35209 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
3520a 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20  t nData,.       
3520b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
3520c 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 46 69 72  qlite_int64 iFir
3520d 73 74 44 6f 63 69 64 2c 20 73 71 6c 69 74 65 5f  stDocid, sqlite_
3520e 69 6e 74 36 34 20 69 4c 61 73 74 44 6f 63 69 64  int64 iLastDocid
3520f 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  ){.  sqlite_int6
35210 34 20 69 44 6f 63 69 64 20 3d 20 30 3b 0a 20 20  4 iDocid = 0;.  
35211 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
35212 58 5d 3b 0a 20 20 69 6e 74 20 6e 46 69 72 73 74  X];.  int nFirst
35213 4f 6c 64 2c 20 6e 46 69 72 73 74 4e 65 77 3b 20  Old, nFirstNew; 
35214 20 20 20 20 2f 2a 20 4f 6c 64 20 61 6e 64 20 6e      /* Old and n
35215 65 77 20 76 61 72 69 6e 74 20 6c 65 6e 20 6f 66  ew varint len of
35216 20 66 69 72 73 74 20 64 6f 63 69 64 2e 20 2a 2f   first docid. */
35217 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
35218 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
35219 4c 61 73 74 44 6f 63 69 64 44 65 6c 74 61 3b 0a  LastDocidDelta;.
3521a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63  #endif..  /* Rec
3521b 6f 64 65 20 74 68 65 20 69 6e 69 74 69 61 6c 20  ode the initial 
3521c 64 6f 63 69 64 20 61 73 20 64 65 6c 74 61 20 66  docid as delta f
3521d 72 6f 6d 20 69 50 72 65 76 44 6f 63 69 64 2e 20  rom iPrevDocid. 
3521e 2a 2f 0a 20 20 6e 46 69 72 73 74 4f 6c 64 20 3d  */.  nFirstOld =
3521f 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70   fts3GetVarint(p
35220 44 61 74 61 2c 20 26 69 44 6f 63 69 64 29 3b 0a  Data, &iDocid);.
35221 20 20 61 73 73 65 72 74 28 20 6e 46 69 72 73 74    assert( nFirst
35222 4f 6c 64 3c 6e 44 61 74 61 20 7c 7c 20 28 6e 46  Old<nData || (nF
35223 69 72 73 74 4f 6c 64 3d 3d 6e 44 61 74 61 20 26  irstOld==nData &
35224 26 20 70 57 72 69 74 65 72 2d 3e 69 54 79 70 65  & pWriter->iType
35225 3d 3d 44 4c 5f 44 4f 43 49 44 53 29 20 29 3b 0a  ==DL_DOCIDS) );.
35226 20 20 6e 46 69 72 73 74 4e 65 77 20 3d 20 66 74    nFirstNew = ft
35227 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 69  s3PutVarint(c, i
35228 46 69 72 73 74 44 6f 63 69 64 2d 70 57 72 69 74  FirstDocid-pWrit
35229 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 29 3b  er->iPrevDocid);
3522a 0a 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68  ..  /* Verify th
3522b 61 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20  at the incoming 
3522c 64 6f 63 6c 69 73 74 20 69 73 20 76 61 6c 69 64  doclist is valid
3522d 20 41 4e 44 20 74 68 61 74 20 69 74 20 65 6e 64   AND that it end
3522e 73 20 77 69 74 68 0a 20 20 2a 2a 20 74 68 65 20  s with.  ** the 
3522f 65 78 70 65 63 74 65 64 20 64 6f 63 69 64 2e 20  expected docid. 
35230 20 54 68 69 73 20 69 73 20 65 73 73 65 6e 74 69   This is essenti
35231 61 6c 20 62 65 63 61 75 73 65 20 77 65 27 6c 6c  al because we'll
35232 20 74 72 75 73 74 20 74 68 69 73 0a 20 20 2a 2a   trust this.  **
35233 20 64 6f 63 69 64 20 69 6e 20 66 75 74 75 72 65   docid in future
35234 20 64 65 6c 74 61 2d 65 6e 63 6f 64 69 6e 67 2e   delta-encoding.
35235 0a 20 20 2a 2f 0a 20 20 41 53 53 45 52 54 5f 56  .  */.  ASSERT_V
35236 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 70 57 72  ALID_DOCLIST(pWr
35237 69 74 65 72 2d 3e 69 54 79 70 65 2c 20 70 44 61  iter->iType, pDa
35238 74 61 2c 20 6e 44 61 74 61 2c 20 26 69 4c 61 73  ta, nData, &iLas
35239 74 44 6f 63 69 64 44 65 6c 74 61 29 3b 0a 20 20  tDocidDelta);.  
3523a 61 73 73 65 72 74 28 20 69 4c 61 73 74 44 6f 63  assert( iLastDoc
3523b 69 64 3d 3d 69 46 69 72 73 74 44 6f 63 69 64 2d  id==iFirstDocid-
3523c 69 44 6f 63 69 64 2b 69 4c 61 73 74 44 6f 63 69  iDocid+iLastDoci
3523d 64 44 65 6c 74 61 20 29 3b 0a 0a 20 20 2f 2a 20  dDelta );..  /* 
3523e 41 70 70 65 6e 64 20 72 65 63 6f 64 65 64 20 69  Append recoded i
3523f 6e 69 74 69 61 6c 20 64 6f 63 69 64 20 61 6e 64  nitial docid and
35240 20 65 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65   everything else
35241 2e 20 20 52 65 73 74 20 6f 66 20 64 6f 63 69 64  .  Rest of docid
35242 73 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61  s.  ** should ha
35243 76 65 20 62 65 65 6e 20 64 65 6c 74 61 2d 65 6e  ve been delta-en
35244 63 6f 64 65 64 20 66 72 6f 6d 20 70 72 65 76 69  coded from previ
35245 6f 75 73 20 69 6e 69 74 69 61 6c 20 64 6f 63 69  ous initial doci
35246 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 46  d..  */.  if( nF
35247 69 72 73 74 4f 6c 64 3c 6e 44 61 74 61 20 29 7b  irstOld<nData ){
35248 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
35249 70 70 65 6e 64 32 28 70 57 72 69 74 65 72 2d 3e  ppend2(pWriter->
3524a 62 2c 20 63 2c 20 6e 46 69 72 73 74 4e 65 77 2c  b, c, nFirstNew,
3524b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3524c 20 20 20 20 20 20 20 70 44 61 74 61 2b 6e 46 69         pData+nFi
3524d 72 73 74 4f 6c 64 2c 20 6e 44 61 74 61 2d 6e 46  rstOld, nData-nF
3524e 69 72 73 74 4f 6c 64 29 3b 0a 20 20 7d 65 6c 73  irstOld);.  }els
3524f 65 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  e{.    dataBuffe
35250 72 41 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d  rAppend(pWriter-
35251 3e 62 2c 20 63 2c 20 6e 46 69 72 73 74 4e 65 77  >b, c, nFirstNew
35252 29 3b 0a 20 20 7d 0a 20 20 70 57 72 69 74 65 72  );.  }.  pWriter
35253 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69  ->iPrevDocid = i
35254 4c 61 73 74 44 6f 63 69 64 3b 0a 7d 0a 73 74 61  LastDocid;.}.sta
35255 74 69 63 20 76 6f 69 64 20 64 6c 77 43 6f 70 79  tic void dlwCopy
35256 28 44 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74  (DLWriter *pWrit
35257 65 72 2c 20 44 4c 52 65 61 64 65 72 20 2a 70 52  er, DLReader *pR
35258 65 61 64 65 72 29 7b 0a 20 20 64 6c 77 41 70 70  eader){.  dlwApp
35259 65 6e 64 28 70 57 72 69 74 65 72 2c 20 64 6c 72  end(pWriter, dlr
3525a 44 6f 63 44 61 74 61 28 70 52 65 61 64 65 72 29  DocData(pReader)
3525b 2c 20 64 6c 72 44 6f 63 44 61 74 61 42 79 74 65  , dlrDocDataByte
3525c 73 28 70 52 65 61 64 65 72 29 2c 0a 20 20 20 20  s(pReader),.    
3525d 20 20 20 20 20 20 20 20 64 6c 72 44 6f 63 69 64          dlrDocid
3525e 28 70 52 65 61 64 65 72 29 2c 20 64 6c 72 44 6f  (pReader), dlrDo
3525f 63 69 64 28 70 52 65 61 64 65 72 29 29 3b 0a 7d  cid(pReader));.}
35260 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 77  .static void dlw
35261 41 64 64 28 44 4c 57 72 69 74 65 72 20 2a 70 57  Add(DLWriter *pW
35262 72 69 74 65 72 2c 20 73 71 6c 69 74 65 5f 69 6e  riter, sqlite_in
35263 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 63  t64 iDocid){.  c
35264 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58  har c[VARINT_MAX
35265 5d 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74 73  ];.  int n = fts
35266 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 69 44  3PutVarint(c, iD
35267 6f 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 69 50  ocid-pWriter->iP
35268 72 65 76 44 6f 63 69 64 29 3b 0a 0a 20 20 2f 2a  revDocid);..  /*
35269 20 44 6f 63 69 64 73 20 6d 75 73 74 20 61 73 63   Docids must asc
3526a 65 6e 64 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  end. */.  assert
3526b 28 20 21 70 57 72 69 74 65 72 2d 3e 68 61 73 5f  ( !pWriter->has_
3526c 69 50 72 65 76 44 6f 63 69 64 20 7c 7c 20 69 44  iPrevDocid || iD
3526d 6f 63 69 64 3e 70 57 72 69 74 65 72 2d 3e 69 50  ocid>pWriter->iP
3526e 72 65 76 44 6f 63 69 64 20 29 3b 0a 20 20 61 73  revDocid );.  as
3526f 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 69  sert( pWriter->i
35270 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44 53 20  Type==DL_DOCIDS 
35271 29 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72  );..  dataBuffer
35272 41 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e  Append(pWriter->
35273 62 2c 20 63 2c 20 6e 29 3b 0a 20 20 70 57 72 69  b, c, n);.  pWri
35274 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 20  ter->iPrevDocid 
35275 3d 20 69 44 6f 63 69 64 3b 0a 23 69 66 6e 64 65  = iDocid;.#ifnde
35276 66 20 4e 44 45 42 55 47 0a 20 20 70 57 72 69 74  f NDEBUG.  pWrit
35277 65 72 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63  er->has_iPrevDoc
35278 69 64 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 7d  id = 1;.#endif.}
35279 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
3527a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3527b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3527c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3527d 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50 4c 52 65 61  ******/./* PLRea
3527e 64 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 72  der is used to r
3527f 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20  ead data from a 
35280 64 6f 63 75 6d 65 6e 74 27 73 20 70 6f 73 69 74  document's posit
35281 69 6f 6e 20 6c 69 73 74 2e 20 20 41 73 0a 2a 2a  ion list.  As.**
35282 20 74 68 65 20 63 61 6c 6c 65 72 20 73 74 65 70   the caller step
35283 73 20 74 68 72 6f 75 67 68 20 74 68 65 20 6c 69  s through the li
35284 73 74 2c 20 64 61 74 61 20 69 73 20 63 61 63 68  st, data is cach
35285 65 64 20 73 6f 20 74 68 61 74 20 76 61 72 69 6e  ed so that varin
35286 74 73 0a 2a 2a 20 6f 6e 6c 79 20 6e 65 65 64 20  ts.** only need 
35287 74 6f 20 62 65 20 64 65 63 6f 64 65 64 20 6f 6e  to be decoded on
35288 63 65 2e 0a 2a 2a 0a 2a 2a 20 70 6c 72 49 6e 69  ce..**.** plrIni
35289 74 2c 20 70 6c 72 44 65 73 74 72 6f 79 20 2d 20  t, plrDestroy - 
3528a 63 72 65 61 74 65 2f 64 65 73 74 72 6f 79 20 61  create/destroy a
3528b 20 72 65 61 64 65 72 2e 0a 2a 2a 20 70 6c 72 43   reader..** plrC
3528c 6f 6c 75 6d 6e 2c 20 70 6c 72 50 6f 73 69 74 69  olumn, plrPositi
3528d 6f 6e 2c 20 70 6c 72 53 74 61 72 74 4f 66 66 73  on, plrStartOffs
3528e 65 74 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74  et, plrEndOffset
3528f 20 2d 20 61 63 63 65 73 73 6f 72 73 0a 2a 2a 20   - accessors.** 
35290 70 6c 72 41 74 45 6e 64 20 2d 20 61 74 20 65 6e  plrAtEnd - at en
35291 64 20 6f 66 20 73 74 72 65 61 6d 2c 20 6f 6e 6c  d of stream, onl
35292 79 20 63 61 6c 6c 20 70 6c 72 44 65 73 74 72 6f  y call plrDestro
35293 79 20 6f 6e 63 65 20 74 72 75 65 2e 0a 2a 2a 20  y once true..** 
35294 70 6c 72 53 74 65 70 20 2d 20 73 74 65 70 20 74  plrStep - step t
35295 6f 20 74 68 65 20 6e 65 78 74 20 65 6c 65 6d 65  o the next eleme
35296 6e 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  nt..*/.typedef s
35297 74 72 75 63 74 20 50 4c 52 65 61 64 65 72 20 7b  truct PLReader {
35298 0a 20 20 2f 2a 20 54 68 65 73 65 20 72 65 66 65  .  /* These refe
35299 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70 6f  r to the next po
3529a 73 69 74 69 6f 6e 27 73 20 64 61 74 61 2e 20 20  sition's data.  
3529b 6e 44 61 74 61 20 77 69 6c 6c 20 72 65 61 63 68  nData will reach
3529c 20 30 20 77 68 65 6e 0a 20 20 2a 2a 20 72 65 61   0 when.  ** rea
3529d 64 69 6e 67 20 74 68 65 20 6c 61 73 74 20 70 6f  ding the last po
3529e 73 69 74 69 6f 6e 2c 20 73 6f 20 70 6c 72 53 74  sition, so plrSt
3529f 65 70 28 29 20 73 69 67 6e 61 6c 73 20 45 4f 46  ep() signals EOF
352a0 20 62 79 20 73 65 74 74 69 6e 67 0a 20 20 2a 2a   by setting.  **
352a1 20 70 44 61 74 61 20 74 6f 20 4e 55 4c 4c 2e 0a   pData to NULL..
352a2 20 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61    */.  const cha
352a3 72 20 2a 70 44 61 74 61 3b 0a 20 20 69 6e 74 20  r *pData;.  int 
352a4 6e 44 61 74 61 3b 0a 0a 20 20 44 6f 63 4c 69 73  nData;..  DocLis
352a5 74 54 79 70 65 20 69 54 79 70 65 3b 0a 20 20 69  tType iType;.  i
352a6 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  nt iColumn;     
352a7 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20      /* the last 
352a8 63 6f 6c 75 6d 6e 20 72 65 61 64 20 2a 2f 0a 20  column read */. 
352a9 20 69 6e 74 20 69 50 6f 73 69 74 69 6f 6e 3b 20   int iPosition; 
352aa 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73        /* the las
352ab 74 20 70 6f 73 69 74 69 6f 6e 20 72 65 61 64 20  t position read 
352ac 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 72 74 4f  */.  int iStartO
352ad 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 74 68 65  ffset;    /* the
352ae 20 6c 61 73 74 20 73 74 61 72 74 20 6f 66 66 73   last start offs
352af 65 74 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74  et read */.  int
352b0 20 69 45 6e 64 4f 66 66 73 65 74 3b 20 20 20 20   iEndOffset;    
352b1 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 65 6e    /* the last en
352b2 64 20 6f 66 66 73 65 74 20 72 65 61 64 20 2a 2f  d offset read */
352b3 0a 7d 20 50 4c 52 65 61 64 65 72 3b 0a 0a 73 74  .} PLReader;..st
352b4 61 74 69 63 20 69 6e 74 20 70 6c 72 41 74 45 6e  atic int plrAtEn
352b5 64 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61  d(PLReader *pRea
352b6 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  der){.  return p
352b7 52 65 61 64 65 72 2d 3e 70 44 61 74 61 3d 3d 4e  Reader->pData==N
352b8 55 4c 4c 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  ULL;.}.static in
352b9 74 20 70 6c 72 43 6f 6c 75 6d 6e 28 50 4c 52 65  t plrColumn(PLRe
352ba 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
352bb 20 20 61 73 73 65 72 74 28 20 21 70 6c 72 41 74    assert( !plrAt
352bc 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
352bd 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
352be 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 7d 0a 73 74 61  ->iColumn;.}.sta
352bf 74 69 63 20 69 6e 74 20 70 6c 72 50 6f 73 69 74  tic int plrPosit
352c0 69 6f 6e 28 50 4c 52 65 61 64 65 72 20 2a 70 52  ion(PLReader *pR
352c1 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
352c2 28 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65 61  ( !plrAtEnd(pRea
352c3 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
352c4 20 70 52 65 61 64 65 72 2d 3e 69 50 6f 73 69 74   pReader->iPosit
352c5 69 6f 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  ion;.}.static in
352c6 74 20 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74  t plrStartOffset
352c7 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64  (PLReader *pRead
352c8 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
352c9 70 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65 72  plrAtEnd(pReader
352ca 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52  ) );.  return pR
352cb 65 61 64 65 72 2d 3e 69 53 74 61 72 74 4f 66 66  eader->iStartOff
352cc 73 65 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  set;.}.static in
352cd 74 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 50  t plrEndOffset(P
352ce 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
352cf 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c  ){.  assert( !pl
352d0 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
352d1 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
352d2 64 65 72 2d 3e 69 45 6e 64 4f 66 66 73 65 74 3b  der->iEndOffset;
352d3 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  .}.static void p
352d4 6c 72 53 74 65 70 28 50 4c 52 65 61 64 65 72 20  lrStep(PLReader 
352d5 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e 74  *pReader){.  int
352d6 20 69 2c 20 6e 3b 0a 0a 20 20 61 73 73 65 72 74   i, n;..  assert
352d7 28 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65 61  ( !plrAtEnd(pRea
352d8 64 65 72 29 20 29 3b 0a 0a 20 20 69 66 28 20 70  der) );..  if( p
352d9 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d 30  Reader->nData==0
352da 20 29 7b 0a 20 20 20 20 70 52 65 61 64 65 72 2d   ){.    pReader-
352db 3e 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a 20  >pData = NULL;. 
352dc 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
352dd 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
352de 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70  int32(pReader->p
352df 44 61 74 61 2c 20 26 69 29 3b 0a 20 20 69 66 28  Data, &i);.  if(
352e0 20 69 3d 3d 50 4f 53 5f 43 4f 4c 55 4d 4e 20 29   i==POS_COLUMN )
352e1 7b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47  {.    n += fts3G
352e2 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
352e3 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 70 52  er->pData+n, &pR
352e4 65 61 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e 29 3b  eader->iColumn);
352e5 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 50  .    pReader->iP
352e6 6f 73 69 74 69 6f 6e 20 3d 20 30 3b 0a 20 20 20  osition = 0;.   
352e7 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72 74   pReader->iStart
352e8 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20  Offset = 0;.    
352e9 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69  n += fts3GetVari
352ea 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
352eb 61 74 61 2b 6e 2c 20 26 69 29 3b 0a 20 20 7d 0a  ata+n, &i);.  }.
352ec 20 20 2f 2a 20 53 68 6f 75 6c 64 20 6e 65 76 65    /* Should neve
352ed 72 20 73 65 65 20 61 64 6a 61 63 65 6e 74 20 63  r see adjacent c
352ee 6f 6c 75 6d 6e 20 63 68 61 6e 67 65 73 2e 20 2a  olumn changes. *
352ef 2f 0a 20 20 61 73 73 65 72 74 28 20 69 21 3d 50  /.  assert( i!=P
352f0 4f 53 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 0a 20 20  OS_COLUMN );..  
352f1 69 66 28 20 69 3d 3d 50 4f 53 5f 45 4e 44 20 29  if( i==POS_END )
352f2 7b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e  {.    pReader->n
352f3 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 70 52  Data = 0;.    pR
352f4 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 4e  eader->pData = N
352f5 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ULL;.    return;
352f6 0a 20 20 7d 0a 0a 20 20 70 52 65 61 64 65 72 2d  .  }..  pReader-
352f7 3e 69 50 6f 73 69 74 69 6f 6e 20 2b 3d 20 69 2d  >iPosition += i-
352f8 50 4f 53 5f 42 41 53 45 3b 0a 20 20 69 66 28 20  POS_BASE;.  if( 
352f9 70 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3d 3d  pReader->iType==
352fa 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46  DL_POSITIONS_OFF
352fb 53 45 54 53 20 29 7b 0a 20 20 20 20 6e 20 2b 3d  SETS ){.    n +=
352fc 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
352fd 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b  (pReader->pData+
352fe 6e 2c 20 26 69 29 3b 0a 20 20 20 20 70 52 65 61  n, &i);.    pRea
352ff 64 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65  der->iStartOffse
35300 74 20 2b 3d 20 69 3b 0a 20 20 20 20 6e 20 2b 3d  t += i;.    n +=
35301 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
35302 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b  (pReader->pData+
35303 6e 2c 20 26 69 29 3b 0a 20 20 20 20 70 52 65 61  n, &i);.    pRea
35304 64 65 72 2d 3e 69 45 6e 64 4f 66 66 73 65 74 20  der->iEndOffset 
35305 3d 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72  = pReader->iStar
35306 74 4f 66 66 73 65 74 2b 69 3b 0a 20 20 7d 0a 20  tOffset+i;.  }. 
35307 20 61 73 73 65 72 74 28 20 6e 3c 3d 70 52 65 61   assert( n<=pRea
35308 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
35309 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b  pReader->pData +
3530a 3d 20 6e 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  = n;.  pReader->
3530b 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 7d 0a 0a 73  nData -= n;.}..s
3530c 74 61 74 69 63 20 76 6f 69 64 20 70 6c 72 49 6e  tatic void plrIn
3530d 69 74 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65  it(PLReader *pRe
3530e 61 64 65 72 2c 20 44 4c 52 65 61 64 65 72 20 2a  ader, DLReader *
3530f 70 44 4c 52 65 61 64 65 72 29 7b 0a 20 20 70 52  pDLReader){.  pR
35310 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 64  eader->pData = d
35311 6c 72 50 6f 73 44 61 74 61 28 70 44 4c 52 65 61  lrPosData(pDLRea
35312 64 65 72 29 3b 0a 20 20 70 52 65 61 64 65 72 2d  der);.  pReader-
35313 3e 6e 44 61 74 61 20 3d 20 64 6c 72 50 6f 73 44  >nData = dlrPosD
35314 61 74 61 4c 65 6e 28 70 44 4c 52 65 61 64 65 72  ataLen(pDLReader
35315 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 54  );.  pReader->iT
35316 79 70 65 20 3d 20 70 44 4c 52 65 61 64 65 72 2d  ype = pDLReader-
35317 3e 69 54 79 70 65 3b 0a 20 20 70 52 65 61 64 65  >iType;.  pReade
35318 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a  r->iColumn = 0;.
35319 20 20 70 52 65 61 64 65 72 2d 3e 69 50 6f 73 69    pReader->iPosi
3531a 74 69 6f 6e 20 3d 20 30 3b 0a 20 20 70 52 65 61  tion = 0;.  pRea
3531b 64 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65  der->iStartOffse
3531c 74 20 3d 20 30 3b 0a 20 20 70 52 65 61 64 65 72  t = 0;.  pReader
3531d 2d 3e 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 30  ->iEndOffset = 0
3531e 3b 0a 20 20 70 6c 72 53 74 65 70 28 70 52 65 61  ;.  plrStep(pRea
3531f 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76  der);.}.static v
35320 6f 69 64 20 70 6c 72 44 65 73 74 72 6f 79 28 50  oid plrDestroy(P
35321 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
35322 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52  ){.  SCRAMBLE(pR
35323 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  eader);.}../****
35324 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35325 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35326 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35327 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
35328 0a 2f 2a 20 50 4c 57 72 69 74 65 72 20 69 73 20  ./* PLWriter is 
35329 75 73 65 64 20 69 6e 20 63 6f 6e 73 74 72 75 63  used in construc
3532a 74 69 6e 67 20 61 20 64 6f 63 75 6d 65 6e 74 27  ting a document'
3532b 73 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 2e  s position list.
3532c 20 20 41 73 20 61 0a 2a 2a 20 63 6f 6e 76 65 6e    As a.** conven
3532d 69 65 6e 63 65 2c 20 69 66 20 69 54 79 70 65 20  ience, if iType 
3532e 69 73 20 44 4c 5f 44 4f 43 49 44 53 2c 20 50 4c  is DL_DOCIDS, PL
3532f 57 72 69 74 65 72 20 62 65 63 6f 6d 65 73 20 61  Writer becomes a
35330 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 50 4c 57 72 69   no-op..** PLWri
35331 74 65 72 20 77 72 69 74 65 73 20 74 6f 20 74 68  ter writes to th
35332 65 20 61 73 73 6f 63 69 61 74 65 64 20 44 4c 57  e associated DLW
35333 72 69 74 65 72 27 73 20 62 75 66 66 65 72 2e 0a  riter's buffer..
35334 2a 2a 0a 2a 2a 20 70 6c 77 49 6e 69 74 20 2d 20  **.** plwInit - 
35335 69 6e 69 74 20 66 6f 72 20 77 72 69 74 69 6e 67  init for writing
35336 20 61 20 64 6f 63 75 6d 65 6e 74 27 73 20 70 6f   a document's po
35337 73 6c 69 73 74 2e 0a 2a 2a 20 70 6c 77 44 65 73  slist..** plwDes
35338 74 72 6f 79 20 2d 20 63 6c 65 61 72 20 61 20 77  troy - clear a w
35339 72 69 74 65 72 2e 0a 2a 2a 20 70 6c 77 41 64 64  riter..** plwAdd
3533a 20 2d 20 61 70 70 65 6e 64 20 70 6f 73 69 74 69   - append positi
3533b 6f 6e 20 61 6e 64 20 6f 66 66 73 65 74 20 69 6e  on and offset in
3533c 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 70 6c  formation..** pl
3533d 77 43 6f 70 79 20 2d 20 63 6f 70 79 20 6e 65 78  wCopy - copy nex
3533e 74 20 70 6f 73 69 74 69 6f 6e 27 73 20 64 61 74  t position's dat
3533f 61 20 66 72 6f 6d 20 72 65 61 64 65 72 20 74 6f  a from reader to
35340 20 77 72 69 74 65 72 2e 0a 2a 2a 20 70 6c 77 54   writer..** plwT
35341 65 72 6d 69 6e 61 74 65 20 2d 20 61 64 64 20 61  erminate - add a
35342 6e 79 20 6e 65 63 65 73 73 61 72 79 20 64 6f 63  ny necessary doc
35343 6c 69 73 74 20 74 65 72 6d 69 6e 61 74 6f 72 2e  list terminator.
35344 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 70  .**.** Calling p
35345 6c 77 41 64 64 28 29 20 61 66 74 65 72 20 70 6c  lwAdd() after pl
35346 77 54 65 72 6d 69 6e 61 74 65 28 29 20 6d 61 79  wTerminate() may
35347 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72   result in a cor
35348 72 75 70 74 0a 2a 2a 20 64 6f 63 6c 69 73 74 2e  rupt.** doclist.
35349 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
3534a 73 29 20 55 6e 74 69 6c 20 77 65 27 76 65 20 77  s) Until we've w
3534b 72 69 74 74 65 6e 20 74 68 65 20 73 65 63 6f 6e  ritten the secon
3534c 64 20 69 74 65 6d 2c 20 77 65 20 63 61 6e 20 63  d item, we can c
3534d 61 63 68 65 20 74 68 65 0a 2a 2a 20 66 69 72 73  ache the.** firs
3534e 74 20 69 74 65 6d 27 73 20 69 6e 66 6f 72 6d 61  t item's informa
3534f 74 69 6f 6e 2e 20 20 54 68 65 6e 20 77 65 27 64  tion.  Then we'd
35350 20 68 61 76 65 20 74 68 72 65 65 20 73 74 61 74   have three stat
35351 65 73 3a 0a 2a 2a 0a 2a 2a 20 2d 20 69 6e 69 74  es:.**.** - init
35352 69 61 6c 69 7a 65 64 20 77 69 74 68 20 64 6f 63  ialized with doc
35353 69 64 2c 20 6e 6f 20 70 6f 73 69 74 69 6f 6e 73  id, no positions
35354 2e 0a 2a 2a 20 2d 20 64 6f 63 69 64 20 61 6e 64  ..** - docid and
35355 20 6f 6e 65 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a   one position..*
35356 2a 20 2d 20 64 6f 63 69 64 20 61 6e 64 20 6d 75  * - docid and mu
35357 6c 74 69 70 6c 65 20 70 6f 73 69 74 69 6f 6e 73  ltiple positions
35358 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65  ..**.** Only the
35359 20 6c 61 73 74 20 73 74 61 74 65 20 6e 65 65 64   last state need
3535a 73 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 77 72  s to actually wr
3535b 69 74 65 20 74 6f 20 64 6c 77 2d 3e 62 2c 20 77  ite to dlw->b, w
3535c 68 69 63 68 20 77 6f 75 6c 64 0a 2a 2a 20 62 65  hich would.** be
3535d 20 61 6e 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20   an improvement 
3535e 69 6e 20 74 68 65 20 44 4c 43 6f 6c 6c 65 63 74  in the DLCollect
3535f 6f 72 20 63 61 73 65 2e 0a 2a 2f 0a 74 79 70 65  or case..*/.type
35360 64 65 66 20 73 74 72 75 63 74 20 50 4c 57 72 69  def struct PLWri
35361 74 65 72 20 7b 0a 20 20 44 4c 57 72 69 74 65 72  ter {.  DLWriter
35362 20 2a 64 6c 77 3b 0a 0a 20 20 69 6e 74 20 69 43   *dlw;..  int iC
35363 6f 6c 75 6d 6e 3b 20 20 20 20 2f 2a 20 74 68 65  olumn;    /* the
35364 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 77 72 69   last column wri
35365 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 50  tten */.  int iP
35366 6f 73 3b 20 20 20 20 20 20 20 2f 2a 20 74 68 65  os;       /* the
35367 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 20 77   last position w
35368 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  ritten */.  int 
35369 69 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 74  iOffset;    /* t
3536a 68 65 20 6c 61 73 74 20 73 74 61 72 74 20 6f 66  he last start of
3536b 66 73 65 74 20 77 72 69 74 74 65 6e 20 2a 2f 0a  fset written */.
3536c 7d 20 50 4c 57 72 69 74 65 72 3b 0a 0a 2f 2a 20  } PLWriter;../* 
3536d 54 4f 44 4f 28 73 68 65 73 73 29 20 49 6e 20 74  TODO(shess) In t
3536e 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68  he case where th
3536f 65 20 70 61 72 65 6e 74 20 69 73 20 72 65 61 64  e parent is read
35370 69 6e 67 20 74 68 65 73 65 20 76 61 6c 75 65 73  ing these values
35371 0a 2a 2a 20 66 72 6f 6d 20 61 20 50 4c 52 65 61  .** from a PLRea
35372 64 65 72 2c 20 77 65 20 63 6f 75 6c 64 20 6f 70  der, we could op
35373 74 69 6d 69 7a 65 20 74 6f 20 61 20 63 6f 70 79  timize to a copy
35374 20 69 66 20 74 68 61 74 20 50 4c 52 65 61 64 65   if that PLReade
35375 72 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d  r has.** the sam
35376 65 20 74 79 70 65 20 61 73 20 70 57 72 69 74 65  e type as pWrite
35377 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
35378 64 20 70 6c 77 41 64 64 28 50 4c 57 72 69 74 65  d plwAdd(PLWrite
35379 72 20 2a 70 57 72 69 74 65 72 2c 20 69 6e 74 20  r *pWriter, int 
3537a 69 43 6f 6c 75 6d 6e 2c 20 69 6e 74 20 69 50 6f  iColumn, int iPo
3537b 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
3537c 20 20 20 20 20 20 69 6e 74 20 69 53 74 61 72 74        int iStart
3537d 4f 66 66 73 65 74 2c 20 69 6e 74 20 69 45 6e 64  Offset, int iEnd
3537e 4f 66 66 73 65 74 29 7b 0a 20 20 2f 2a 20 57 6f  Offset){.  /* Wo
3537f 72 73 74 2d 63 61 73 65 20 73 70 61 63 65 20 66  rst-case space f
35380 6f 72 20 50 4f 53 5f 43 4f 4c 55 4d 4e 2c 20 69  or POS_COLUMN, i
35381 43 6f 6c 75 6d 6e 2c 20 69 50 6f 73 44 65 6c 74  Column, iPosDelt
35382 61 2c 0a 20 20 2a 2a 20 69 53 74 61 72 74 4f 66  a,.  ** iStartOf
35383 66 73 65 74 44 65 6c 74 61 2c 20 61 6e 64 20 69  fsetDelta, and i
35384 45 6e 64 4f 66 66 73 65 74 44 65 6c 74 61 2e 0a  EndOffsetDelta..
35385 20 20 2a 2f 0a 20 20 63 68 61 72 20 63 5b 35 2a    */.  char c[5*
35386 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
35387 6e 74 20 6e 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  nt n = 0;..  /* 
35388 42 61 6e 20 70 6c 77 41 64 64 28 29 20 61 66 74  Ban plwAdd() aft
35389 65 72 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28  er plwTerminate(
3538a 29 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  ). */.  assert( 
3538b 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 21 3d 2d  pWriter->iPos!=-
3538c 31 20 29 3b 0a 0a 20 20 69 66 28 20 70 57 72 69  1 );..  if( pWri
3538d 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3d  ter->dlw->iType=
3538e 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72 65 74  =DL_DOCIDS ) ret
3538f 75 72 6e 3b 0a 0a 20 20 69 66 28 20 69 43 6f 6c  urn;..  if( iCol
35390 75 6d 6e 21 3d 70 57 72 69 74 65 72 2d 3e 69 43  umn!=pWriter->iC
35391 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 6e 20 2b  olumn ){.    n +
35392 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
35393 63 2b 6e 2c 20 50 4f 53 5f 43 4f 4c 55 4d 4e 29  c+n, POS_COLUMN)
35394 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50  ;.    n += fts3P
35395 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69 43  utVarint(c+n, iC
35396 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 70 57 72 69  olumn);.    pWri
35397 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69  ter->iColumn = i
35398 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 57 72 69  Column;.    pWri
35399 74 65 72 2d 3e 69 50 6f 73 20 3d 20 30 3b 0a 20  ter->iPos = 0;. 
3539a 20 20 20 70 57 72 69 74 65 72 2d 3e 69 4f 66 66     pWriter->iOff
3539b 73 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61  set = 0;.  }.  a
3539c 73 73 65 72 74 28 20 69 50 6f 73 3e 3d 70 57 72  ssert( iPos>=pWr
3539d 69 74 65 72 2d 3e 69 50 6f 73 20 29 3b 0a 20 20  iter->iPos );.  
3539e 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
3539f 6e 74 28 63 2b 6e 2c 20 50 4f 53 5f 42 41 53 45  nt(c+n, POS_BASE
353a0 2b 28 69 50 6f 73 2d 70 57 72 69 74 65 72 2d 3e  +(iPos-pWriter->
353a1 69 50 6f 73 29 29 3b 0a 20 20 70 57 72 69 74 65  iPos));.  pWrite
353a2 72 2d 3e 69 50 6f 73 20 3d 20 69 50 6f 73 3b 0a  r->iPos = iPos;.
353a3 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 64    if( pWriter->d
353a4 6c 77 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f  lw->iType==DL_PO
353a5 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
353a6 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ){.    assert( i
353a7 53 74 61 72 74 4f 66 66 73 65 74 3e 3d 70 57 72  StartOffset>=pWr
353a8 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 29 3b  iter->iOffset );
353a9 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  .    n += fts3Pu
353aa 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69 53 74  tVarint(c+n, iSt
353ab 61 72 74 4f 66 66 73 65 74 2d 70 57 72 69 74 65  artOffset-pWrite
353ac 72 2d 3e 69 4f 66 66 73 65 74 29 3b 0a 20 20 20  r->iOffset);.   
353ad 20 70 57 72 69 74 65 72 2d 3e 69 4f 66 66 73 65   pWriter->iOffse
353ae 74 20 3d 20 69 53 74 61 72 74 4f 66 66 73 65 74  t = iStartOffset
353af 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 45  ;.    assert( iE
353b0 6e 64 4f 66 66 73 65 74 3e 3d 69 53 74 61 72 74  ndOffset>=iStart
353b1 4f 66 66 73 65 74 20 29 3b 0a 20 20 20 20 6e 20  Offset );.    n 
353b2 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74  += fts3PutVarint
353b3 28 63 2b 6e 2c 20 69 45 6e 64 4f 66 66 73 65 74  (c+n, iEndOffset
353b4 2d 69 53 74 61 72 74 4f 66 66 73 65 74 29 3b 0a  -iStartOffset);.
353b5 20 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72    }.  dataBuffer
353b6 41 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e  Append(pWriter->
353b7 64 6c 77 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a 7d  dlw->b, c, n);.}
353b8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77  .static void plw
353b9 43 6f 70 79 28 50 4c 57 72 69 74 65 72 20 2a 70  Copy(PLWriter *p
353ba 57 72 69 74 65 72 2c 20 50 4c 52 65 61 64 65 72  Writer, PLReader
353bb 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 70 6c   *pReader){.  pl
353bc 77 41 64 64 28 70 57 72 69 74 65 72 2c 20 70 6c  wAdd(pWriter, pl
353bd 72 43 6f 6c 75 6d 6e 28 70 52 65 61 64 65 72 29  rColumn(pReader)
353be 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52  , plrPosition(pR
353bf 65 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20  eader),.        
353c0 20 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28   plrStartOffset(
353c1 70 52 65 61 64 65 72 29 2c 20 70 6c 72 45 6e 64  pReader), plrEnd
353c2 4f 66 66 73 65 74 28 70 52 65 61 64 65 72 29 29  Offset(pReader))
353c3 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
353c4 70 6c 77 49 6e 69 74 28 50 4c 57 72 69 74 65 72  plwInit(PLWriter
353c5 20 2a 70 57 72 69 74 65 72 2c 20 44 4c 57 72 69   *pWriter, DLWri
353c6 74 65 72 20 2a 64 6c 77 2c 20 73 71 6c 69 74 65  ter *dlw, sqlite
353c7 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a  _int64 iDocid){.
353c8 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f    char c[VARINT_
353c9 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a  MAX];.  int n;..
353ca 20 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 20 3d    pWriter->dlw =
353cb 20 64 6c 77 3b 0a 0a 20 20 2f 2a 20 44 6f 63 69   dlw;..  /* Doci
353cc 64 73 20 6d 75 73 74 20 61 73 63 65 6e 64 2e 20  ds must ascend. 
353cd 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 70 57  */.  assert( !pW
353ce 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 68 61 73 5f  riter->dlw->has_
353cf 69 50 72 65 76 44 6f 63 69 64 20 7c 7c 20 69 44  iPrevDocid || iD
353d0 6f 63 69 64 3e 70 57 72 69 74 65 72 2d 3e 64 6c  ocid>pWriter->dl
353d1 77 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 29 3b  w->iPrevDocid );
353d2 0a 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61  .  n = fts3PutVa
353d3 72 69 6e 74 28 63 2c 20 69 44 6f 63 69 64 2d 70  rint(c, iDocid-p
353d4 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50 72  Writer->dlw->iPr
353d5 65 76 44 6f 63 69 64 29 3b 0a 20 20 64 61 74 61  evDocid);.  data
353d6 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57 72  BufferAppend(pWr
353d7 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c 20 63 2c  iter->dlw->b, c,
353d8 20 6e 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e   n);.  pWriter->
353d9 64 6c 77 2d 3e 69 50 72 65 76 44 6f 63 69 64 20  dlw->iPrevDocid 
353da 3d 20 69 44 6f 63 69 64 3b 0a 23 69 66 6e 64 65  = iDocid;.#ifnde
353db 66 20 4e 44 45 42 55 47 0a 20 20 70 57 72 69 74  f NDEBUG.  pWrit
353dc 65 72 2d 3e 64 6c 77 2d 3e 68 61 73 5f 69 50 72  er->dlw->has_iPr
353dd 65 76 44 6f 63 69 64 20 3d 20 31 3b 0a 23 65 6e  evDocid = 1;.#en
353de 64 69 66 0a 0a 20 20 70 57 72 69 74 65 72 2d 3e  dif..  pWriter->
353df 69 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 70  iColumn = 0;.  p
353e0 57 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d 20 30  Writer->iPos = 0
353e1 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 4f 66  ;.  pWriter->iOf
353e2 66 73 65 74 20 3d 20 30 3b 0a 7d 0a 2f 2a 20 54  fset = 0;.}./* T
353e3 4f 44 4f 28 73 68 65 73 73 29 20 53 68 6f 75 6c  ODO(shess) Shoul
353e4 64 20 70 6c 77 44 65 73 74 72 6f 79 28 29 20 61  d plwDestroy() a
353e5 6c 73 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68  lso terminate th
353e6 65 20 64 6f 63 6c 69 73 74 3f 20 20 42 75 74 0a  e doclist?  But.
353e7 2a 2a 20 74 68 65 6e 20 70 6c 77 44 65 73 74 72  ** then plwDestr
353e8 6f 79 28 29 20 77 6f 75 6c 64 20 6e 6f 20 6c 6f  oy() would no lo
353e9 6e 67 65 72 20 62 65 20 6a 75 73 74 20 61 20 64  nger be just a d
353ea 65 73 74 72 75 63 74 6f 72 2c 20 69 74 20 77 6f  estructor, it wo
353eb 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 64  uld.** also be d
353ec 6f 69 6e 67 20 77 6f 72 6b 2c 20 77 68 69 63 68  oing work, which
353ed 20 69 73 6e 27 74 20 63 6f 6e 73 69 73 74 65 6e   isn't consisten
353ee 74 20 77 69 74 68 20 74 68 65 20 6f 76 65 72 61  t with the overa
353ef 6c 6c 20 69 64 69 6f 6d 2e 0a 2a 2a 20 41 6e 6f  ll idiom..** Ano
353f0 74 68 65 72 20 6f 70 74 69 6f 6e 20 77 6f 75 6c  ther option woul
353f1 64 20 62 65 20 66 6f 72 20 70 6c 77 41 64 64 28  d be for plwAdd(
353f2 29 20 74 6f 20 61 6c 77 61 79 73 20 61 70 70 65  ) to always appe
353f3 6e 64 20 61 6e 79 20 6e 65 63 65 73 73 61 72 79  nd any necessary
353f4 0a 2a 2a 20 74 65 72 6d 69 6e 61 74 6f 72 2c 20  .** terminator, 
353f5 73 6f 20 74 68 61 74 20 74 68 65 20 6f 75 74 70  so that the outp
353f6 75 74 20 69 73 20 61 6c 77 61 79 73 20 63 6f 72  ut is always cor
353f7 72 65 63 74 2e 20 20 42 75 74 20 74 68 61 74 20  rect.  But that 
353f8 77 6f 75 6c 64 0a 2a 2a 20 61 64 64 20 69 6e 63  would.** add inc
353f9 72 65 6d 65 6e 74 61 6c 20 77 6f 72 6b 20 74 6f  remental work to
353fa 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65   the common case
353fb 20 77 69 74 68 20 74 68 65 20 6f 6e 6c 79 20 62   with the only b
353fc 65 6e 65 66 69 74 20 62 65 69 6e 67 0a 2a 2a 20  enefit being.** 
353fd 41 50 49 20 65 6c 65 67 61 6e 63 65 2e 20 20 50  API elegance.  P
353fe 75 6e 74 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2f 0a  unt for now..*/.
353ff 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 54  static void plwT
35400 65 72 6d 69 6e 61 74 65 28 50 4c 57 72 69 74 65  erminate(PLWrite
35401 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 69  r *pWriter){.  i
35402 66 28 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d  f( pWriter->dlw-
35403 3e 69 54 79 70 65 3e 44 4c 5f 44 4f 43 49 44 53  >iType>DL_DOCIDS
35404 20 29 7b 0a 20 20 20 20 63 68 61 72 20 63 5b 56   ){.    char c[V
35405 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 20 20  ARINT_MAX];.    
35406 69 6e 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56  int n = fts3PutV
35407 61 72 69 6e 74 28 63 2c 20 50 4f 53 5f 45 4e 44  arint(c, POS_END
35408 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  );.    dataBuffe
35409 72 41 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d  rAppend(pWriter-
3540a 3e 64 6c 77 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a  >dlw->b, c, n);.
3540b 20 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42    }.#ifndef NDEB
3540c 55 47 0a 20 20 2f 2a 20 4d 61 72 6b 20 61 73 20  UG.  /* Mark as 
3540d 74 65 72 6d 69 6e 61 74 65 64 20 66 6f 72 20 61  terminated for a
3540e 73 73 65 72 74 20 69 6e 20 70 6c 77 41 64 64 28  ssert in plwAdd(
3540f 29 2e 20 2a 2f 0a 20 20 70 57 72 69 74 65 72 2d  ). */.  pWriter-
35410 3e 69 50 6f 73 20 3d 20 2d 31 3b 0a 23 65 6e 64  >iPos = -1;.#end
35411 69 66 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  if.}.static void
35412 20 70 6c 77 44 65 73 74 72 6f 79 28 50 4c 57 72   plwDestroy(PLWr
35413 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
35414 20 20 53 43 52 41 4d 42 4c 45 28 70 57 72 69 74    SCRAMBLE(pWrit
35415 65 72 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  er);.}../*******
35416 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35417 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35418 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35419 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
3541a 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 77 72 61   DLCollector wra
3541b 70 73 20 50 4c 57 72 69 74 65 72 20 61 6e 64 20  ps PLWriter and 
3541c 44 4c 57 72 69 74 65 72 20 74 6f 20 70 72 6f 76  DLWriter to prov
3541d 69 64 65 20 61 0a 2a 2a 20 64 79 6e 61 6d 69 63  ide a.** dynamic
3541e 61 6c 6c 79 2d 61 6c 6c 6f 63 61 74 65 64 20 64  ally-allocated d
3541f 6f 63 6c 69 73 74 20 61 72 65 61 20 74 6f 20 75  oclist area to u
35420 73 65 20 64 75 72 69 6e 67 20 74 6f 6b 65 6e 69  se during tokeni
35421 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 64 6c  zation..**.** dl
35422 63 4e 65 77 20 2d 20 6d 61 6c 6c 6f 63 20 75 70  cNew - malloc up
35423 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20   and initialize 
35424 61 20 63 6f 6c 6c 65 63 74 6f 72 2e 0a 2a 2a 20  a collector..** 
35425 64 6c 63 44 65 6c 65 74 65 20 2d 20 64 65 73 74  dlcDelete - dest
35426 72 6f 79 20 61 20 63 6f 6c 6c 65 63 74 6f 72 20  roy a collector 
35427 61 6e 64 20 61 6c 6c 20 63 6f 6e 74 61 69 6e 65  and all containe
35428 64 20 69 74 65 6d 73 2e 0a 2a 2a 20 64 6c 63 41  d items..** dlcA
35429 64 64 50 6f 73 20 2d 20 61 70 70 65 6e 64 20 70  ddPos - append p
3542a 6f 73 69 74 69 6f 6e 20 61 6e 64 20 6f 66 66 73  osition and offs
3542b 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  et information..
3542c 2a 2a 20 64 6c 63 41 64 64 44 6f 63 6c 69 73 74  ** dlcAddDoclist
3542d 20 2d 20 61 64 64 20 74 68 65 20 63 6f 6c 6c 65   - add the colle
3542e 63 74 65 64 20 64 6f 63 6c 69 73 74 20 74 6f 20  cted doclist to 
3542f 74 68 65 20 67 69 76 65 6e 20 62 75 66 66 65 72  the given buffer
35430 2e 0a 2a 2a 20 64 6c 63 4e 65 78 74 20 2d 20 74  ..** dlcNext - t
35431 65 72 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72  erminate the cur
35432 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74 20 61 6e  rent document an
35433 64 20 6f 70 65 6e 20 61 6e 6f 74 68 65 72 2e 0a  d open another..
35434 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
35435 74 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 7b 0a  t DLCollector {.
35436 20 20 44 61 74 61 42 75 66 66 65 72 20 62 3b 0a    DataBuffer b;.
35437 20 20 44 4c 57 72 69 74 65 72 20 64 6c 77 3b 0a    DLWriter dlw;.
35438 20 20 50 4c 57 72 69 74 65 72 20 70 6c 77 3b 0a    PLWriter plw;.
35439 7d 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 3b 0a 0a  } DLCollector;..
3543a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54  /* TODO(shess) T
3543b 68 69 73 20 63 6f 75 6c 64 20 61 6c 73 6f 20 62  his could also b
3543c 65 20 64 6f 6e 65 20 62 79 20 63 61 6c 6c 69 6e  e done by callin
3543d 67 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 29  g plwTerminate()
3543e 20 61 6e 64 0a 2a 2a 20 64 61 74 61 42 75 66 66   and.** dataBuff
3543f 65 72 41 70 70 65 6e 64 28 29 2e 20 20 49 20 74  erAppend().  I t
35440 72 69 65 64 20 74 68 61 74 2c 20 65 78 70 65 63  ried that, expec
35441 74 69 6e 67 20 6e 6f 6d 69 6e 61 6c 20 70 65 72  ting nominal per
35442 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 64 69 66 66  formance.** diff
35443 65 72 65 6e 63 65 73 2c 20 62 75 74 20 69 74 20  erences, but it 
35444 73 65 65 6d 65 64 20 74 6f 20 70 72 65 74 74 79  seemed to pretty
35445 20 72 65 6c 69 61 62 6c 79 20 62 65 20 77 6f 72   reliably be wor
35446 74 68 20 31 25 20 74 6f 20 63 6f 64 65 0a 2a 2a  th 1% to code.**
35447 20 69 74 20 74 68 69 73 20 77 61 79 2e 20 20 49   it this way.  I
35448 20 73 75 73 70 65 63 74 20 69 74 20 69 73 20 74   suspect it is t
35449 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 6d  he incremental m
3544a 61 6c 6c 6f 63 20 6f 76 65 72 68 65 61 64 20 28  alloc overhead (
3544b 73 6f 6d 65 0a 2a 2a 20 70 65 72 63 65 6e 74 61  some.** percenta
3544c 67 65 20 6f 66 20 74 68 65 20 70 6c 77 54 65 72  ge of the plwTer
3544d 6d 69 6e 61 74 65 28 29 20 63 61 6c 6c 73 20 77  minate() calls w
3544e 69 6c 6c 20 63 61 75 73 65 20 61 20 72 65 61 6c  ill cause a real
3544f 6c 6f 63 29 2c 20 73 6f 0a 2a 2a 20 74 68 69 73  loc), so.** this
35450 20 6d 69 67 68 74 20 62 65 20 77 6f 72 74 68 20   might be worth 
35451 72 65 76 69 73 69 74 69 6e 67 20 69 66 20 74 68  revisiting if th
35452 65 20 44 61 74 61 42 75 66 66 65 72 20 69 6d 70  e DataBuffer imp
35453 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 63  lementation.** c
35454 68 61 6e 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  hanges..*/.stati
35455 63 20 76 6f 69 64 20 64 6c 63 41 64 64 44 6f 63  c void dlcAddDoc
35456 6c 69 73 74 28 44 4c 43 6f 6c 6c 65 63 74 6f 72  list(DLCollector
35457 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 2c 20 44 61   *pCollector, Da
35458 74 61 42 75 66 66 65 72 20 2a 62 29 7b 0a 20 20  taBuffer *b){.  
35459 69 66 28 20 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  if( pCollector->
3545a 64 6c 77 2e 69 54 79 70 65 3e 44 4c 5f 44 4f 43  dlw.iType>DL_DOC
3545b 49 44 53 20 29 7b 0a 20 20 20 20 63 68 61 72 20  IDS ){.    char 
3545c 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  c[VARINT_MAX];. 
3545d 20 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33 50     int n = fts3P
3545e 75 74 56 61 72 69 6e 74 28 63 2c 20 50 4f 53 5f  utVarint(c, POS_
3545f 45 4e 44 29 3b 0a 20 20 20 20 64 61 74 61 42 75  END);.    dataBu
35460 66 66 65 72 41 70 70 65 6e 64 32 28 62 2c 20 70  fferAppend2(b, p
35461 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 70 44 61  Collector->b.pDa
35462 74 61 2c 20 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  ta, pCollector->
35463 62 2e 6e 44 61 74 61 2c 20 63 2c 20 6e 29 3b 0a  b.nData, c, n);.
35464 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 61 74    }else{.    dat
35465 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 62 2c  aBufferAppend(b,
35466 20 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 70   pCollector->b.p
35467 44 61 74 61 2c 20 70 43 6f 6c 6c 65 63 74 6f 72  Data, pCollector
35468 2d 3e 62 2e 6e 44 61 74 61 29 3b 0a 20 20 7d 0a  ->b.nData);.  }.
35469 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c  }.static void dl
3546a 63 4e 65 78 74 28 44 4c 43 6f 6c 6c 65 63 74 6f  cNext(DLCollecto
3546b 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 2c 20 73  r *pCollector, s
3546c 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
3546d 69 64 29 7b 0a 20 20 70 6c 77 54 65 72 6d 69 6e  id){.  plwTermin
3546e 61 74 65 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d  ate(&pCollector-
3546f 3e 70 6c 77 29 3b 0a 20 20 70 6c 77 44 65 73 74  >plw);.  plwDest
35470 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d  roy(&pCollector-
35471 3e 70 6c 77 29 3b 0a 20 20 70 6c 77 49 6e 69 74  >plw);.  plwInit
35472 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c  (&pCollector->pl
35473 77 2c 20 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  w, &pCollector->
35474 64 6c 77 2c 20 69 44 6f 63 69 64 29 3b 0a 7d 0a  dlw, iDocid);.}.
35475 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 63 41  static void dlcA
35476 64 64 50 6f 73 28 44 4c 43 6f 6c 6c 65 63 74 6f  ddPos(DLCollecto
35477 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 2c 20 69  r *pCollector, i
35478 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69 6e 74 20  nt iColumn, int 
35479 69 50 6f 73 2c 0a 20 20 20 20 20 20 20 20 20 20  iPos,.          
3547a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
3547b 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69 6e  iStartOffset, in
3547c 74 20 69 45 6e 64 4f 66 66 73 65 74 29 7b 0a 20  t iEndOffset){. 
3547d 20 70 6c 77 41 64 64 28 26 70 43 6f 6c 6c 65 63   plwAdd(&pCollec
3547e 74 6f 72 2d 3e 70 6c 77 2c 20 69 43 6f 6c 75 6d  tor->plw, iColum
3547f 6e 2c 20 69 50 6f 73 2c 20 69 53 74 61 72 74 4f  n, iPos, iStartO
35480 66 66 73 65 74 2c 20 69 45 6e 64 4f 66 66 73 65  ffset, iEndOffse
35481 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 44 4c  t);.}..static DL
35482 43 6f 6c 6c 65 63 74 6f 72 20 2a 64 6c 63 4e 65  Collector *dlcNe
35483 77 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  w(sqlite_int64 i
35484 44 6f 63 69 64 2c 20 44 6f 63 4c 69 73 74 54 79  Docid, DocListTy
35485 70 65 20 69 54 79 70 65 29 7b 0a 20 20 44 4c 43  pe iType){.  DLC
35486 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65  ollector *pColle
35487 63 74 6f 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  ctor = sqlite3_m
35488 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 44 4c 43  alloc(sizeof(DLC
35489 6f 6c 6c 65 63 74 6f 72 29 29 3b 0a 20 20 64 61  ollector));.  da
3548a 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 43  taBufferInit(&pC
3548b 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2c 20 30 29 3b  ollector->b, 0);
3548c 0a 20 20 64 6c 77 49 6e 69 74 28 26 70 43 6f 6c  .  dlwInit(&pCol
3548d 6c 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 54 79  lector->dlw, iTy
3548e 70 65 2c 20 26 70 43 6f 6c 6c 65 63 74 6f 72 2d  pe, &pCollector-
3548f 3e 62 29 3b 0a 20 20 70 6c 77 49 6e 69 74 28 26  >b);.  plwInit(&
35490 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 2c  pCollector->plw,
35491 20 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c   &pCollector->dl
35492 77 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 72 65  w, iDocid);.  re
35493 74 75 72 6e 20 70 43 6f 6c 6c 65 63 74 6f 72 3b  turn pCollector;
35494 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  .}.static void d
35495 6c 63 44 65 6c 65 74 65 28 44 4c 43 6f 6c 6c 65  lcDelete(DLColle
35496 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72  ctor *pCollector
35497 29 7b 0a 20 20 70 6c 77 44 65 73 74 72 6f 79 28  ){.  plwDestroy(
35498 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77  &pCollector->plw
35499 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28  );.  dlwDestroy(
3549a 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77  &pCollector->dlw
3549b 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44  );.  dataBufferD
3549c 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74  estroy(&pCollect
3549d 6f 72 2d 3e 62 29 3b 0a 20 20 53 43 52 41 4d 42  or->b);.  SCRAMB
3549e 4c 45 28 70 43 6f 6c 6c 65 63 74 6f 72 29 3b 0a  LE(pCollector);.
3549f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
354a0 43 6f 6c 6c 65 63 74 6f 72 29 3b 0a 7d 0a 0a 0a  Collector);.}...
354a1 2f 2a 20 43 6f 70 79 20 74 68 65 20 64 6f 63 6c  /* Copy the docl
354a2 69 73 74 20 64 61 74 61 20 6f 66 20 69 54 79 70  ist data of iTyp
354a3 65 20 69 6e 20 70 44 61 74 61 2f 6e 44 61 74 61  e in pData/nData
354a4 20 69 6e 74 6f 20 2a 6f 75 74 2c 20 74 72 69 6d   into *out, trim
354a5 6d 69 6e 67 0a 2a 2a 20 75 6e 6e 65 63 65 73 73  ming.** unnecess
354a6 61 72 79 20 64 61 74 61 20 61 73 20 77 65 20 67  ary data as we g
354a7 6f 2e 20 20 4f 6e 6c 79 20 63 6f 6c 75 6d 6e 73  o.  Only columns
354a8 20 6d 61 74 63 68 69 6e 67 20 69 43 6f 6c 75 6d   matching iColum
354a9 6e 20 61 72 65 0a 2a 2a 20 63 6f 70 69 65 64 2c  n are.** copied,
354aa 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 63 6f 70   all columns cop
354ab 69 65 64 20 69 66 20 69 43 6f 6c 75 6d 6e 20 69  ied if iColumn i
354ac 73 20 2d 31 2e 20 20 45 6c 65 6d 65 6e 74 73 20  s -1.  Elements 
354ad 77 69 74 68 20 6e 6f 0a 2a 2a 20 6d 61 74 63 68  with no.** match
354ae 69 6e 67 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20  ing columns are 
354af 64 72 6f 70 70 65 64 2e 20 20 54 68 65 20 6f 75  dropped.  The ou
354b0 74 70 75 74 20 69 73 20 61 6e 20 69 4f 75 74 54  tput is an iOutT
354b1 79 70 65 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a  ype doclist..*/.
354b2 2f 2a 20 4e 4f 54 45 28 73 68 65 73 73 29 20 54  /* NOTE(shess) T
354b3 68 69 73 20 63 6f 64 65 20 69 73 20 6f 6e 6c 79  his code is only
354b4 20 76 61 6c 69 64 20 61 66 74 65 72 20 61 6c 6c   valid after all
354b5 20 64 6f 63 6c 69 73 74 73 20 61 72 65 20 6d 65   doclists are me
354b6 72 67 65 64 2e 0a 2a 2a 20 49 66 20 74 68 69 73  rged..** If this
354b7 20 69 73 20 72 75 6e 20 62 65 66 6f 72 65 20 6d   is run before m
354b8 65 72 67 65 73 2c 20 74 68 65 6e 20 64 6f 63 6c  erges, then docl
354b9 69 73 74 20 69 74 65 6d 73 20 77 68 69 63 68 20  ist items which 
354ba 72 65 70 72 65 73 65 6e 74 0a 2a 2a 20 64 65 6c  represent.** del
354bb 65 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 74 72  etion will be tr
354bc 69 6d 6d 65 64 2c 20 61 6e 64 20 77 69 6c 6c 20  immed, and will 
354bd 74 68 75 73 20 6e 6f 74 20 65 66 66 65 63 74 20  thus not effect 
354be 61 20 64 65 6c 65 74 69 6f 6e 0a 2a 2a 20 64 75  a deletion.** du
354bf 72 69 6e 67 20 74 68 65 20 6d 65 72 67 65 2e 0a  ring the merge..
354c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
354c1 6f 63 4c 69 73 74 54 72 69 6d 28 44 6f 63 4c 69  ocListTrim(DocLi
354c2 73 74 54 79 70 65 20 69 54 79 70 65 2c 20 63 6f  stType iType, co
354c3 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c  nst char *pData,
354c4 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20   int nData,.    
354c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
354c6 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c      int iColumn,
354c7 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 4f 75   DocListType iOu
354c8 74 54 79 70 65 2c 20 44 61 74 61 42 75 66 66 65  tType, DataBuffe
354c9 72 20 2a 6f 75 74 29 7b 0a 20 20 44 4c 52 65 61  r *out){.  DLRea
354ca 64 65 72 20 64 6c 52 65 61 64 65 72 3b 0a 20 20  der dlReader;.  
354cb 44 4c 57 72 69 74 65 72 20 64 6c 57 72 69 74 65  DLWriter dlWrite
354cc 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 4f  r;..  assert( iO
354cd 75 74 54 79 70 65 3c 3d 69 54 79 70 65 20 29 3b  utType<=iType );
354ce 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52  ..  dlrInit(&dlR
354cf 65 61 64 65 72 2c 20 69 54 79 70 65 2c 20 70 44  eader, iType, pD
354d0 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20 20 64  ata, nData);.  d
354d1 6c 77 49 6e 69 74 28 26 64 6c 57 72 69 74 65 72  lwInit(&dlWriter
354d2 2c 20 69 4f 75 74 54 79 70 65 2c 20 6f 75 74 29  , iOutType, out)
354d3 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72  ;..  while( !dlr
354d4 41 74 45 6e 64 28 26 64 6c 52 65 61 64 65 72 29  AtEnd(&dlReader)
354d5 20 29 7b 0a 20 20 20 20 50 4c 52 65 61 64 65 72   ){.    PLReader
354d6 20 70 6c 52 65 61 64 65 72 3b 0a 20 20 20 20 50   plReader;.    P
354d7 4c 57 72 69 74 65 72 20 70 6c 57 72 69 74 65 72  LWriter plWriter
354d8 3b 0a 20 20 20 20 69 6e 74 20 6d 61 74 63 68 20  ;.    int match 
354d9 3d 20 30 3b 0a 0a 20 20 20 20 70 6c 72 49 6e 69  = 0;..    plrIni
354da 74 28 26 70 6c 52 65 61 64 65 72 2c 20 26 64 6c  t(&plReader, &dl
354db 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 77 68  Reader);..    wh
354dc 69 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28 26  ile( !plrAtEnd(&
354dd 70 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20  plReader) ){.   
354de 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d     if( iColumn==
354df 2d 31 20 7c 7c 20 70 6c 72 43 6f 6c 75 6d 6e 28  -1 || plrColumn(
354e0 26 70 6c 52 65 61 64 65 72 29 3d 3d 69 43 6f 6c  &plReader)==iCol
354e1 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 69  umn ){.        i
354e2 66 28 20 21 6d 61 74 63 68 20 29 7b 0a 20 20 20  f( !match ){.   
354e3 20 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28 26         plwInit(&
354e4 70 6c 57 72 69 74 65 72 2c 20 26 64 6c 57 72 69  plWriter, &dlWri
354e5 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 64  ter, dlrDocid(&d
354e6 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20 20  lReader));.     
354e7 20 20 20 20 20 6d 61 74 63 68 20 3d 20 31 3b 0a       match = 1;.
354e8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
354e9 20 20 70 6c 77 41 64 64 28 26 70 6c 57 72 69 74    plwAdd(&plWrit
354ea 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70  er, plrColumn(&p
354eb 6c 52 65 61 64 65 72 29 2c 20 70 6c 72 50 6f 73  lReader), plrPos
354ec 69 74 69 6f 6e 28 26 70 6c 52 65 61 64 65 72 29  ition(&plReader)
354ed 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
354ee 20 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28   plrStartOffset(
354ef 26 70 6c 52 65 61 64 65 72 29 2c 20 70 6c 72 45  &plReader), plrE
354f0 6e 64 4f 66 66 73 65 74 28 26 70 6c 52 65 61 64  ndOffset(&plRead
354f1 65 72 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  er));.      }.  
354f2 20 20 20 20 70 6c 72 53 74 65 70 28 26 70 6c 52      plrStep(&plR
354f3 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  eader);.    }.  
354f4 20 20 69 66 28 20 6d 61 74 63 68 20 29 7b 0a 20    if( match ){. 
354f5 20 20 20 20 20 70 6c 77 54 65 72 6d 69 6e 61 74       plwTerminat
354f6 65 28 26 70 6c 57 72 69 74 65 72 29 3b 0a 20 20  e(&plWriter);.  
354f7 20 20 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26      plwDestroy(&
354f8 70 6c 57 72 69 74 65 72 29 3b 0a 20 20 20 20 7d  plWriter);.    }
354f9 0a 0a 20 20 20 20 70 6c 72 44 65 73 74 72 6f 79  ..    plrDestroy
354fa 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 20 20 20  (&plReader);.   
354fb 20 64 6c 72 53 74 65 70 28 26 64 6c 52 65 61 64   dlrStep(&dlRead
354fc 65 72 29 3b 0a 20 20 7d 0a 20 20 64 6c 77 44 65  er);.  }.  dlwDe
354fd 73 74 72 6f 79 28 26 64 6c 57 72 69 74 65 72 29  stroy(&dlWriter)
354fe 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ;.  dlrDestroy(&
354ff 64 6c 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a  dlReader);.}../*
35500 20 55 73 65 64 20 62 79 20 64 6f 63 4c 69 73 74   Used by docList
35501 4d 65 72 67 65 28 29 20 74 6f 20 6b 65 65 70 20  Merge() to keep 
35502 64 6f 63 6c 69 73 74 73 20 69 6e 20 74 68 65 20  doclists in the 
35503 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20  ascending order 
35504 62 79 0a 2a 2a 20 64 6f 63 69 64 2c 20 74 68 65  by.** docid, the
35505 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
35506 72 20 62 79 20 61 67 65 20 28 73 6f 20 74 68 65  r by age (so the
35507 20 6e 65 77 65 73 74 20 63 6f 6d 65 73 20 66 69   newest comes fi
35508 72 73 74 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  rst)..*/.typedef
35509 20 73 74 72 75 63 74 20 4f 72 64 65 72 65 64 44   struct OrderedD
3550a 4c 52 65 61 64 65 72 20 7b 0a 20 20 44 4c 52 65  LReader {.  DLRe
3550b 61 64 65 72 20 2a 70 52 65 61 64 65 72 3b 0a 0a  ader *pReader;..
3550c 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
3550d 20 49 66 20 77 65 20 61 73 73 75 6d 65 20 74 68   If we assume th
3550e 61 74 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 20  at docListMerge 
3550f 70 52 65 61 64 65 72 73 20 69 73 20 6f 72 64 65  pReaders is orde
35510 72 65 64 20 62 79 0a 20 20 2a 2a 20 61 67 65 20  red by.  ** age 
35511 28 77 68 69 63 68 20 77 65 20 64 6f 29 2c 20 74  (which we do), t
35512 68 65 6e 20 77 65 20 63 6f 75 6c 64 20 75 73 65  hen we could use
35513 20 70 52 65 61 64 65 72 20 63 6f 6d 70 61 72 69   pReader compari
35514 73 6f 6e 73 20 74 6f 20 62 72 65 61 6b 0a 20 20  sons to break.  
35515 2a 2a 20 74 69 65 73 2e 0a 20 20 2a 2f 0a 20 20  ** ties..  */.  
35516 69 6e 74 20 69 64 78 3b 0a 7d 20 4f 72 64 65 72  int idx;.} Order
35517 65 64 44 4c 52 65 61 64 65 72 3b 0a 0a 2f 2a 20  edDLReader;../* 
35518 4f 72 64 65 72 20 65 6f 66 20 74 6f 20 65 6e 64  Order eof to end
35519 2c 20 74 68 65 6e 20 62 79 20 64 6f 63 69 64 20  , then by docid 
3551a 61 73 63 2c 20 69 64 78 20 64 65 73 63 2e 20 2a  asc, idx desc. *
3551b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 72 64  /.static int ord
3551c 65 72 65 64 44 4c 52 65 61 64 65 72 43 6d 70 28  eredDLReaderCmp(
3551d 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 20  OrderedDLReader 
3551e 2a 72 31 2c 20 4f 72 64 65 72 65 64 44 4c 52 65  *r1, OrderedDLRe
3551f 61 64 65 72 20 2a 72 32 29 7b 0a 20 20 69 66 28  ader *r2){.  if(
35520 20 64 6c 72 41 74 45 6e 64 28 72 31 2d 3e 70 52   dlrAtEnd(r1->pR
35521 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 69 66  eader) ){.    if
35522 28 20 64 6c 72 41 74 45 6e 64 28 72 32 2d 3e 70  ( dlrAtEnd(r2->p
35523 52 65 61 64 65 72 29 20 29 20 72 65 74 75 72 6e  Reader) ) return
35524 20 30 3b 20 20 2f 2a 20 42 6f 74 68 20 61 74 45   0;  /* Both atE
35525 6e 64 28 29 2e 20 2a 2f 0a 20 20 20 20 72 65 74  nd(). */.    ret
35526 75 72 6e 20 31 3b 20 20 20 20 20 20 20 20 20 20  urn 1;          
35527 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35528 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 72 31 20 61      /* Only r1 a
35529 74 45 6e 64 28 29 2e 20 2a 2f 0a 20 20 7d 0a 20  tEnd(). */.  }. 
3552a 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 72 32   if( dlrAtEnd(r2
3552b 2d 3e 70 52 65 61 64 65 72 29 20 29 20 72 65 74  ->pReader) ) ret
3552c 75 72 6e 20 2d 31 3b 20 20 20 2f 2a 20 4f 6e 6c  urn -1;   /* Onl
3552d 79 20 72 32 20 61 74 45 6e 64 28 29 2e 20 2a 2f  y r2 atEnd(). */
3552e 0a 0a 20 20 69 66 28 20 64 6c 72 44 6f 63 69 64  ..  if( dlrDocid
3552f 28 72 31 2d 3e 70 52 65 61 64 65 72 29 3c 64 6c  (r1->pReader)<dl
35530 72 44 6f 63 69 64 28 72 32 2d 3e 70 52 65 61 64  rDocid(r2->pRead
35531 65 72 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  er) ) return -1;
35532 0a 20 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  .  if( dlrDocid(
35533 72 31 2d 3e 70 52 65 61 64 65 72 29 3e 64 6c 72  r1->pReader)>dlr
35534 44 6f 63 69 64 28 72 32 2d 3e 70 52 65 61 64 65  Docid(r2->pReade
35535 72 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a  r) ) return 1;..
35536 20 20 2f 2a 20 44 65 73 63 65 6e 64 69 6e 67 20    /* Descending 
35537 6f 6e 20 69 64 78 2e 20 2a 2f 0a 20 20 72 65 74  on idx. */.  ret
35538 75 72 6e 20 72 32 2d 3e 69 64 78 2d 72 31 2d 3e  urn r2->idx-r1->
35539 69 64 78 3b 0a 7d 0a 0a 2f 2a 20 42 75 62 62 6c  idx;.}../* Bubbl
3553a 65 20 70 5b 30 5d 20 74 6f 20 61 70 70 72 6f 70  e p[0] to approp
3553b 72 69 61 74 65 20 70 6c 61 63 65 20 69 6e 20 70  riate place in p
3553c 5b 31 2e 2e 6e 2d 31 5d 2e 20 20 41 73 73 75 6d  [1..n-1].  Assum
3553d 65 73 20 74 68 61 74 0a 2a 2a 20 70 5b 31 2e 2e  es that.** p[1..
3553e 6e 2d 31 5d 20 69 73 20 61 6c 72 65 61 64 79 20  n-1] is already 
3553f 73 6f 72 74 65 64 2e 0a 2a 2f 0a 2f 2a 20 54 4f  sorted..*/./* TO
35540 44 4f 28 73 68 65 73 73 29 20 49 73 20 74 68 69  DO(shess) Is thi
35541 73 20 66 72 65 71 75 65 6e 74 20 65 6e 6f 75 67  s frequent enoug
35542 68 20 74 6f 20 77 61 72 72 61 6e 74 20 61 20 62  h to warrant a b
35543 69 6e 61 72 79 20 73 65 61 72 63 68 3f 0a 2a 2a  inary search?.**
35544 20 42 65 66 6f 72 65 20 69 6d 70 6c 65 6d 65 6e   Before implemen
35545 74 69 6e 67 20 74 68 61 74 2c 20 69 6e 73 74 72  ting that, instr
35546 75 6d 65 6e 74 20 74 68 65 20 63 6f 64 65 20 74  ument the code t
35547 6f 20 63 68 65 63 6b 2e 20 20 49 6e 20 6d 6f 73  o check.  In mos
35548 74 0a 2a 2a 20 63 75 72 72 65 6e 74 20 75 73 61  t.** current usa
35549 67 65 2c 20 49 20 65 78 70 65 63 74 20 74 68 61  ge, I expect tha
3554a 74 20 70 5b 30 5d 20 77 69 6c 6c 20 62 65 20 6c  t p[0] will be l
3554b 65 73 73 20 74 68 61 6e 20 70 5b 31 5d 20 61 20  ess than p[1] a 
3554c 76 65 72 79 0a 2a 2a 20 68 69 67 68 20 70 72 6f  very.** high pro
3554d 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 74  portion of the t
3554e 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ime..*/.static v
3554f 6f 69 64 20 6f 72 64 65 72 65 64 44 4c 52 65 61  oid orderedDLRea
35550 64 65 72 52 65 6f 72 64 65 72 28 4f 72 64 65 72  derReorder(Order
35551 65 64 44 4c 52 65 61 64 65 72 20 2a 70 2c 20 69  edDLReader *p, i
35552 6e 74 20 6e 29 7b 0a 20 20 77 68 69 6c 65 28 20  nt n){.  while( 
35553 6e 3e 31 20 26 26 20 6f 72 64 65 72 65 64 44 4c  n>1 && orderedDL
35554 52 65 61 64 65 72 43 6d 70 28 70 2c 20 70 2b 31  ReaderCmp(p, p+1
35555 29 3e 30 20 29 7b 0a 20 20 20 20 4f 72 64 65 72  )>0 ){.    Order
35556 65 64 44 4c 52 65 61 64 65 72 20 74 6d 70 20 3d  edDLReader tmp =
35557 20 70 5b 30 5d 3b 0a 20 20 20 20 70 5b 30 5d 20   p[0];.    p[0] 
35558 3d 20 70 5b 31 5d 3b 0a 20 20 20 20 70 5b 31 5d  = p[1];.    p[1]
35559 20 3d 20 74 6d 70 3b 0a 20 20 20 20 6e 2d 2d 3b   = tmp;.    n--;
3555a 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 7d 0a  .    p++;.  }.}.
3555b 0a 2f 2a 20 47 69 76 65 6e 20 61 6e 20 61 72 72  ./* Given an arr
3555c 61 79 20 6f 66 20 64 6f 63 6c 69 73 74 20 72 65  ay of doclist re
3555d 61 64 65 72 73 2c 20 6d 65 72 67 65 20 74 68 65  aders, merge the
3555e 69 72 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65  ir doclist eleme
3555f 6e 74 73 0a 2a 2a 20 69 6e 74 6f 20 6f 75 74 20  nts.** into out 
35560 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20  in sorted order 
35561 28 62 79 20 64 6f 63 69 64 29 2c 20 64 72 6f 70  (by docid), drop
35562 70 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 66 72  ping elements fr
35563 6f 6d 20 6f 6c 64 65 72 0a 2a 2a 20 72 65 61 64  om older.** read
35564 65 72 73 20 77 68 65 6e 20 74 68 65 72 65 20 69  ers when there i
35565 73 20 61 20 64 75 70 6c 69 63 61 74 65 20 64 6f  s a duplicate do
35566 63 69 64 2e 20 20 70 52 65 61 64 65 72 73 20 69  cid.  pReaders i
35567 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0a  s assumed to be.
35568 2a 2a 20 6f 72 64 65 72 65 64 20 62 79 20 61 67  ** ordered by ag
35569 65 2c 20 6f 6c 64 65 73 74 20 66 69 72 73 74 2e  e, oldest first.
3556a 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
3556b 73 29 20 6e 52 65 61 64 65 72 73 20 6d 75 73 74  s) nReaders must
3556c 20 62 65 20 3c 3d 20 4d 45 52 47 45 5f 43 4f 55   be <= MERGE_COU
3556d 4e 54 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64  NT.  This should
3556e 20 70 72 6f 62 61 62 6c 79 0a 2a 2a 20 62 65 20   probably.** be 
3556f 66 69 78 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  fixed..*/.static
35570 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 4d 65 72   void docListMer
35571 67 65 28 44 61 74 61 42 75 66 66 65 72 20 2a 6f  ge(DataBuffer *o
35572 75 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ut,.            
35573 20 20 20 20 20 20 20 20 20 20 20 20 20 44 4c 52               DLR
35574 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 73 2c  eader *pReaders,
35575 20 69 6e 74 20 6e 52 65 61 64 65 72 73 29 7b 0a   int nReaders){.
35576 20 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65    OrderedDLReade
35577 72 20 72 65 61 64 65 72 73 5b 4d 45 52 47 45 5f  r readers[MERGE_
35578 43 4f 55 4e 54 5d 3b 0a 20 20 44 4c 57 72 69 74  COUNT];.  DLWrit
35579 65 72 20 77 72 69 74 65 72 3b 0a 20 20 69 6e 74  er writer;.  int
3557a 20 69 2c 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 63   i, n;.  const c
3557b 68 61 72 20 2a 70 53 74 61 72 74 20 3d 20 30 3b  har *pStart = 0;
3557c 0a 20 20 69 6e 74 20 6e 53 74 61 72 74 20 3d 20  .  int nStart = 
3557d 30 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  0;.  sqlite_int6
3557e 34 20 69 46 69 72 73 74 44 6f 63 69 64 20 3d 20  4 iFirstDocid = 
3557f 30 2c 20 69 4c 61 73 74 44 6f 63 69 64 20 3d 20  0, iLastDocid = 
35580 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52  0;..  assert( nR
35581 65 61 64 65 72 73 3e 30 20 29 3b 0a 20 20 69 66  eaders>0 );.  if
35582 28 20 6e 52 65 61 64 65 72 73 3d 3d 31 20 29 7b  ( nReaders==1 ){
35583 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
35584 70 70 65 6e 64 28 6f 75 74 2c 20 64 6c 72 44 6f  ppend(out, dlrDo
35585 63 44 61 74 61 28 70 52 65 61 64 65 72 73 29 2c  cData(pReaders),
35586 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73   dlrAllDataBytes
35587 28 70 52 65 61 64 65 72 73 29 29 3b 0a 20 20 20  (pReaders));.   
35588 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
35589 61 73 73 65 72 74 28 20 6e 52 65 61 64 65 72 73  assert( nReaders
3558a 3c 3d 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b  <=MERGE_COUNT );
3558b 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28  .  n = 0;.  for(
3558c 69 3d 30 3b 20 69 3c 6e 52 65 61 64 65 72 73 3b  i=0; i<nReaders;
3558d 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72   i++){.    asser
3558e 74 28 20 70 52 65 61 64 65 72 73 5b 69 5d 2e 69  t( pReaders[i].i
3558f 54 79 70 65 3d 3d 70 52 65 61 64 65 72 73 5b 30  Type==pReaders[0
35590 5d 2e 69 54 79 70 65 20 29 3b 0a 20 20 20 20 72  ].iType );.    r
35591 65 61 64 65 72 73 5b 69 5d 2e 70 52 65 61 64 65  eaders[i].pReade
35592 72 20 3d 20 70 52 65 61 64 65 72 73 2b 69 3b 0a  r = pReaders+i;.
35593 20 20 20 20 72 65 61 64 65 72 73 5b 69 5d 2e 69      readers[i].i
35594 64 78 20 3d 20 69 3b 0a 20 20 20 20 6e 20 2b 3d  dx = i;.    n +=
35595 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73   dlrAllDataBytes
35596 28 26 70 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a  (&pReaders[i]);.
35597 20 20 7d 0a 20 20 2f 2a 20 43 6f 6e 73 65 72 76    }.  /* Conserv
35598 61 74 69 76 65 6c 79 20 73 69 7a 65 20 6f 75 74  atively size out
35599 70 75 74 20 74 6f 20 73 75 6d 20 6f 66 20 69 6e  put to sum of in
3559a 70 75 74 73 2e 20 20 4f 75 74 70 75 74 20 73 68  puts.  Output sh
3559b 6f 75 6c 64 20 65 6e 64 0a 20 20 2a 2a 20 75 70  ould end.  ** up
3559c 20 73 74 72 69 63 74 6c 79 20 73 6d 61 6c 6c 65   strictly smalle
3559d 72 20 74 68 61 6e 20 69 6e 70 75 74 2e 0a 20 20  r than input..  
3559e 2a 2f 0a 20 20 64 61 74 61 42 75 66 66 65 72 45  */.  dataBufferE
3559f 78 70 61 6e 64 28 6f 75 74 2c 20 6e 29 3b 0a 0a  xpand(out, n);..
355a0 20 20 2f 2a 20 47 65 74 20 74 68 65 20 72 65 61    /* Get the rea
355a1 64 65 72 73 20 69 6e 74 6f 20 73 6f 72 74 65 64  ders into sorted
355a2 20 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 77 68 69   order. */.  whi
355a3 6c 65 28 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20  le( i-->0 ){.   
355a4 20 6f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72   orderedDLReader
355a5 52 65 6f 72 64 65 72 28 72 65 61 64 65 72 73 2b  Reorder(readers+
355a6 69 2c 20 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a  i, nReaders-i);.
355a7 20 20 7d 0a 0a 20 20 64 6c 77 49 6e 69 74 28 26    }..  dlwInit(&
355a8 77 72 69 74 65 72 2c 20 70 52 65 61 64 65 72 73  writer, pReaders
355a9 5b 30 5d 2e 69 54 79 70 65 2c 20 6f 75 74 29 3b  [0].iType, out);
355aa 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74  .  while( !dlrAt
355ab 45 6e 64 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  End(readers[0].p
355ac 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 73  Reader) ){.    s
355ad 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
355ae 69 64 20 3d 20 64 6c 72 44 6f 63 69 64 28 72 65  id = dlrDocid(re
355af 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72  aders[0].pReader
355b0 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  );..    /* If th
355b1 69 73 20 69 73 20 61 20 63 6f 6e 74 69 6e 75 61  is is a continua
355b2 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72 72  tion of the curr
355b3 65 6e 74 20 62 75 66 66 65 72 20 74 6f 20 63 6f  ent buffer to co
355b4 70 79 2c 20 65 78 74 65 6e 64 0a 20 20 20 20 2a  py, extend.    *
355b5 2a 20 74 68 61 74 20 62 75 66 66 65 72 2e 20 20  * that buffer.  
355b6 6d 65 6d 63 70 79 28 29 20 73 65 65 6d 73 20 74  memcpy() seems t
355b7 6f 20 62 65 20 6d 6f 72 65 20 65 66 66 69 63 69  o be more effici
355b8 65 6e 74 20 69 66 20 69 74 20 68 61 73 20 61 0a  ent if it has a.
355b9 20 20 20 20 2a 2a 20 6c 6f 74 73 20 6f 66 20 64      ** lots of d
355ba 61 74 61 20 74 6f 20 63 6f 70 79 2e 0a 20 20 20  ata to copy..   
355bb 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 6c 72 44   */.    if( dlrD
355bc 6f 63 44 61 74 61 28 72 65 61 64 65 72 73 5b 30  ocData(readers[0
355bd 5d 2e 70 52 65 61 64 65 72 29 3d 3d 70 53 74 61  ].pReader)==pSta
355be 72 74 2b 6e 53 74 61 72 74 20 29 7b 0a 20 20 20  rt+nStart ){.   
355bf 20 20 20 6e 53 74 61 72 74 20 2b 3d 20 64 6c 72     nStart += dlr
355c0 44 6f 63 44 61 74 61 42 79 74 65 73 28 72 65 61  DocDataBytes(rea
355c1 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29  ders[0].pReader)
355c2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
355c3 20 20 20 69 66 28 20 70 53 74 61 72 74 21 3d 30     if( pStart!=0
355c4 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6c 77 41   ){.        dlwA
355c5 70 70 65 6e 64 28 26 77 72 69 74 65 72 2c 20 70  ppend(&writer, p
355c6 53 74 61 72 74 2c 20 6e 53 74 61 72 74 2c 20 69  Start, nStart, i
355c7 46 69 72 73 74 44 6f 63 69 64 2c 20 69 4c 61 73  FirstDocid, iLas
355c8 74 44 6f 63 69 64 29 3b 0a 20 20 20 20 20 20 7d  tDocid);.      }
355c9 0a 20 20 20 20 20 20 70 53 74 61 72 74 20 3d 20  .      pStart = 
355ca 64 6c 72 44 6f 63 44 61 74 61 28 72 65 61 64 65  dlrDocData(reade
355cb 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a  rs[0].pReader);.
355cc 20 20 20 20 20 20 6e 53 74 61 72 74 20 3d 20 64        nStart = d
355cd 6c 72 44 6f 63 44 61 74 61 42 79 74 65 73 28 72  lrDocDataBytes(r
355ce 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65  eaders[0].pReade
355cf 72 29 3b 0a 20 20 20 20 20 20 69 46 69 72 73 74  r);.      iFirst
355d0 44 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a  Docid = iDocid;.
355d1 20 20 20 20 7d 0a 20 20 20 20 69 4c 61 73 74 44      }.    iLastD
355d2 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a 20  ocid = iDocid;. 
355d3 20 20 20 64 6c 72 53 74 65 70 28 72 65 61 64 65     dlrStep(reade
355d4 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a  rs[0].pReader);.
355d5 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c  .    /* Drop all
355d6 20 6f 66 20 74 68 65 20 6f 6c 64 65 72 20 65 6c   of the older el
355d7 65 6d 65 6e 74 73 20 77 69 74 68 20 74 68 65 20  ements with the 
355d8 73 61 6d 65 20 64 6f 63 69 64 2e 20 2a 2f 0a 20  same docid. */. 
355d9 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 52     for(i=1; i<nR
355da 65 61 64 65 72 73 20 26 26 0a 20 20 20 20 20 20  eaders &&.      
355db 20 20 20 20 20 20 20 21 64 6c 72 41 74 45 6e 64         !dlrAtEnd
355dc 28 72 65 61 64 65 72 73 5b 69 5d 2e 70 52 65 61  (readers[i].pRea
355dd 64 65 72 29 20 26 26 0a 20 20 20 20 20 20 20 20  der) &&.        
355de 20 20 20 20 20 64 6c 72 44 6f 63 69 64 28 72 65       dlrDocid(re
355df 61 64 65 72 73 5b 69 5d 2e 70 52 65 61 64 65 72  aders[i].pReader
355e0 29 3d 3d 69 44 6f 63 69 64 3b 20 69 2b 2b 29 7b  )==iDocid; i++){
355e1 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 72  .      dlrStep(r
355e2 65 61 64 65 72 73 5b 69 5d 2e 70 52 65 61 64 65  eaders[i].pReade
355e3 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  r);.    }..    /
355e4 2a 20 47 65 74 20 74 68 65 20 72 65 61 64 65 72  * Get the reader
355e5 73 20 62 61 63 6b 20 69 6e 74 6f 20 6f 72 64 65  s back into orde
355e6 72 2e 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  r. */.    while(
355e7 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20   i-->0 ){.      
355e8 6f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 52  orderedDLReaderR
355e9 65 6f 72 64 65 72 28 72 65 61 64 65 72 73 2b 69  eorder(readers+i
355ea 2c 20 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a 20  , nReaders-i);. 
355eb 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43     }.  }..  /* C
355ec 6f 70 79 20 6f 76 65 72 20 61 6e 79 20 72 65 6d  opy over any rem
355ed 61 69 6e 69 6e 67 20 65 6c 65 6d 65 6e 74 73 2e  aining elements.
355ee 20 2a 2f 0a 20 20 69 66 28 20 6e 53 74 61 72 74   */.  if( nStart
355ef 3e 30 20 29 20 64 6c 77 41 70 70 65 6e 64 28 26  >0 ) dlwAppend(&
355f0 77 72 69 74 65 72 2c 20 70 53 74 61 72 74 2c 20  writer, pStart, 
355f1 6e 53 74 61 72 74 2c 20 69 46 69 72 73 74 44 6f  nStart, iFirstDo
355f2 63 69 64 2c 20 69 4c 61 73 74 44 6f 63 69 64 29  cid, iLastDocid)
355f3 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26  ;.  dlwDestroy(&
355f4 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 48  writer);.}../* H
355f5 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66  elper function f
355f6 6f 72 20 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28  or posListUnion(
355f7 29 2e 20 20 43 6f 6d 70 61 72 65 73 20 74 68 65  ).  Compares the
355f8 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f   current positio
355f9 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6c 65 66  n.** between lef
355fa 74 20 61 6e 64 20 72 69 67 68 74 2c 20 72 65 74  t and right, ret
355fb 75 72 6e 69 6e 67 20 61 73 20 73 74 61 6e 64 61  urning as standa
355fc 72 64 20 43 20 69 64 69 6f 6d 20 6f 66 20 3c 30  rd C idiom of <0
355fd 20 69 66 0a 2a 2a 20 6c 65 66 74 3c 72 69 67 68   if.** left<righ
355fe 74 2c 20 3e 30 20 69 66 20 6c 65 66 74 3e 72 69  t, >0 if left>ri
355ff 67 68 74 2c 20 61 6e 64 20 30 20 69 66 20 6c 65  ght, and 0 if le
35600 66 74 3d 3d 72 69 67 68 74 2e 20 20 22 45 6e 64  ft==right.  "End
35601 22 20 61 6c 77 61 79 73 0a 2a 2a 20 63 6f 6d 70  " always.** comp
35602 61 72 65 73 20 67 72 65 61 74 65 72 2e 0a 2a 2f  ares greater..*/
35603 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 73 4c  .static int posL
35604 69 73 74 43 6d 70 28 50 4c 52 65 61 64 65 72 20  istCmp(PLReader 
35605 2a 70 4c 65 66 74 2c 20 50 4c 52 65 61 64 65 72  *pLeft, PLReader
35606 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61 73 73   *pRight){.  ass
35607 65 72 74 28 20 70 4c 65 66 74 2d 3e 69 54 79 70  ert( pLeft->iTyp
35608 65 3d 3d 70 52 69 67 68 74 2d 3e 69 54 79 70 65  e==pRight->iType
35609 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 2d   );.  if( pLeft-
3560a 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44  >iType==DL_DOCID
3560b 53 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  S ) return 0;.. 
3560c 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28 70 4c   if( plrAtEnd(pL
3560d 65 66 74 29 20 29 20 72 65 74 75 72 6e 20 70 6c  eft) ) return pl
3560e 72 41 74 45 6e 64 28 70 52 69 67 68 74 29 20 3f  rAtEnd(pRight) ?
3560f 20 30 20 3a 20 31 3b 0a 20 20 69 66 28 20 70 6c   0 : 1;.  if( pl
35610 72 41 74 45 6e 64 28 70 52 69 67 68 74 29 20 29  rAtEnd(pRight) )
35611 20 72 65 74 75 72 6e 20 2d 31 3b 0a 0a 20 20 69   return -1;..  i
35612 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65  f( plrColumn(pLe
35613 66 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 70 52  ft)<plrColumn(pR
35614 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d  ight) ) return -
35615 31 3b 0a 20 20 69 66 28 20 70 6c 72 43 6f 6c 75  1;.  if( plrColu
35616 6d 6e 28 70 4c 65 66 74 29 3e 70 6c 72 43 6f 6c  mn(pLeft)>plrCol
35617 75 6d 6e 28 70 52 69 67 68 74 29 20 29 20 72 65  umn(pRight) ) re
35618 74 75 72 6e 20 31 3b 0a 0a 20 20 69 66 28 20 70  turn 1;..  if( p
35619 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66 74  lrPosition(pLeft
3561a 29 3c 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52  )<plrPosition(pR
3561b 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d  ight) ) return -
3561c 31 3b 0a 20 20 69 66 28 20 70 6c 72 50 6f 73 69  1;.  if( plrPosi
3561d 74 69 6f 6e 28 70 4c 65 66 74 29 3e 70 6c 72 50  tion(pLeft)>plrP
3561e 6f 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29 20  osition(pRight) 
3561f 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66  ) return 1;.  if
35620 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d  ( pLeft->iType==
35621 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 20 72  DL_POSITIONS ) r
35622 65 74 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20  eturn 0;..  if( 
35623 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70  plrStartOffset(p
35624 4c 65 66 74 29 3c 70 6c 72 53 74 61 72 74 4f 66  Left)<plrStartOf
35625 66 73 65 74 28 70 52 69 67 68 74 29 20 29 20 72  fset(pRight) ) r
35626 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20  eturn -1;.  if( 
35627 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70  plrStartOffset(p
35628 4c 65 66 74 29 3e 70 6c 72 53 74 61 72 74 4f 66  Left)>plrStartOf
35629 66 73 65 74 28 70 52 69 67 68 74 29 20 29 20 72  fset(pRight) ) r
3562a 65 74 75 72 6e 20 31 3b 0a 0a 20 20 69 66 28 20  eturn 1;..  if( 
3562b 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 4c 65  plrEndOffset(pLe
3562c 66 74 29 3c 70 6c 72 45 6e 64 4f 66 66 73 65 74  ft)<plrEndOffset
3562d 28 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72  (pRight) ) retur
3562e 6e 20 2d 31 3b 0a 20 20 69 66 28 20 70 6c 72 45  n -1;.  if( plrE
3562f 6e 64 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3e  ndOffset(pLeft)>
35630 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 52 69  plrEndOffset(pRi
35631 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b  ght) ) return 1;
35632 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ..  return 0;.}.
35633 0a 2f 2a 20 57 72 69 74 65 20 74 68 65 20 75 6e  ./* Write the un
35634 69 6f 6e 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20  ion of position 
35635 6c 69 73 74 73 20 69 6e 20 70 4c 65 66 74 20 61  lists in pLeft a
35636 6e 64 20 70 52 69 67 68 74 20 74 6f 20 70 4f 75  nd pRight to pOu
35637 74 2e 0a 2a 2a 20 22 55 6e 69 6f 6e 22 20 69 6e  t..** "Union" in
35638 20 74 68 69 73 20 63 61 73 65 20 6d 65 61 6e 69   this case meani
35639 6e 67 20 22 41 6c 6c 20 75 6e 69 71 75 65 20 70  ng "All unique p
3563a 6f 73 69 74 69 6f 6e 20 74 75 70 6c 65 73 22 2e  osition tuples".
3563b 20 20 53 68 6f 75 6c 64 0a 2a 2a 20 77 6f 72 6b    Should.** work
3563c 20 77 69 74 68 20 61 6e 79 20 64 6f 63 6c 69 73   with any doclis
3563d 74 20 74 79 70 65 2c 20 74 68 6f 75 67 68 20 62  t type, though b
3563e 6f 74 68 20 69 6e 70 75 74 73 20 61 6e 64 20 74  oth inputs and t
3563f 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 73 68 6f  he output.** sho
35640 75 6c 64 20 62 65 20 74 68 65 20 73 61 6d 65 20  uld be the same 
35641 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  type..*/.static 
35642 76 6f 69 64 20 70 6f 73 4c 69 73 74 55 6e 69 6f  void posListUnio
35643 6e 28 44 4c 52 65 61 64 65 72 20 2a 70 4c 65 66  n(DLReader *pLef
35644 74 2c 20 44 4c 52 65 61 64 65 72 20 2a 70 52 69  t, DLReader *pRi
35645 67 68 74 2c 20 44 4c 57 72 69 74 65 72 20 2a 70  ght, DLWriter *p
35646 4f 75 74 29 7b 0a 20 20 50 4c 52 65 61 64 65 72  Out){.  PLReader
35647 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20   left, right;.  
35648 50 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b  PLWriter writer;
35649 0a 0a 20 20 61 73 73 65 72 74 28 20 64 6c 72 44  ..  assert( dlrD
3564a 6f 63 69 64 28 70 4c 65 66 74 29 3d 3d 64 6c 72  ocid(pLeft)==dlr
3564b 44 6f 63 69 64 28 70 52 69 67 68 74 29 20 29 3b  Docid(pRight) );
3564c 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74  .  assert( pLeft
3564d 2d 3e 69 54 79 70 65 3d 3d 70 52 69 67 68 74 2d  ->iType==pRight-
3564e 3e 69 54 79 70 65 20 29 3b 0a 20 20 61 73 73 65  >iType );.  asse
3564f 72 74 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65  rt( pLeft->iType
35650 3d 3d 70 4f 75 74 2d 3e 69 54 79 70 65 20 29 3b  ==pOut->iType );
35651 0a 0a 20 20 70 6c 72 49 6e 69 74 28 26 6c 65 66  ..  plrInit(&lef
35652 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 70 6c 72  t, pLeft);.  plr
35653 49 6e 69 74 28 26 72 69 67 68 74 2c 20 70 52 69  Init(&right, pRi
35654 67 68 74 29 3b 0a 20 20 70 6c 77 49 6e 69 74 28  ght);.  plwInit(
35655 26 77 72 69 74 65 72 2c 20 70 4f 75 74 2c 20 64  &writer, pOut, d
35656 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29 29 3b  lrDocid(pLeft));
35657 0a 0a 20 20 77 68 69 6c 65 28 20 21 70 6c 72 41  ..  while( !plrA
35658 74 45 6e 64 28 26 6c 65 66 74 29 20 7c 7c 20 21  tEnd(&left) || !
35659 70 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29  plrAtEnd(&right)
3565a 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 20 3d 20   ){.    int c = 
3565b 70 6f 73 4c 69 73 74 43 6d 70 28 26 6c 65 66 74  posListCmp(&left
3565c 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 69  , &right);.    i
3565d 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 20 20  f( c<0 ){.      
3565e 70 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c  plwCopy(&writer,
3565f 20 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 70   &left);.      p
35660 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20  lrStep(&left);. 
35661 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3e 30     }else if( c>0
35662 20 29 7b 0a 20 20 20 20 20 20 70 6c 77 43 6f 70   ){.      plwCop
35663 79 28 26 77 72 69 74 65 72 2c 20 26 72 69 67 68  y(&writer, &righ
35664 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65  t);.      plrSte
35665 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
35666 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 6c 77 43  else{.      plwC
35667 6f 70 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65  opy(&writer, &le
35668 66 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53 74  ft);.      plrSt
35669 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20  ep(&left);.     
3566a 20 70 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   plrStep(&right)
3566b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70  ;.    }.  }..  p
3566c 6c 77 54 65 72 6d 69 6e 61 74 65 28 26 77 72 69  lwTerminate(&wri
3566d 74 65 72 29 3b 0a 20 20 70 6c 77 44 65 73 74 72  ter);.  plwDestr
3566e 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 70  oy(&writer);.  p
3566f 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  lrDestroy(&left)
35670 3b 0a 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26  ;.  plrDestroy(&
35671 72 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 20 57 72  right);.}../* Wr
35672 69 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66  ite the union of
35673 20 64 6f 63 6c 69 73 74 73 20 69 6e 20 70 4c 65   doclists in pLe
35674 66 74 20 61 6e 64 20 70 52 69 67 68 74 20 74 6f  ft and pRight to
35675 20 70 4f 75 74 2e 20 20 46 6f 72 0a 2a 2a 20 64   pOut.  For.** d
35676 6f 63 69 64 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20  ocids in common 
35677 62 65 74 77 65 65 6e 20 74 68 65 20 69 6e 70 75  between the inpu
35678 74 73 2c 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66  ts, the union of
35679 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 0a 2a 2a   the position.**
3567a 20 6c 69 73 74 73 20 69 73 20 77 72 69 74 74 65   lists is writte
3567b 6e 2e 20 20 49 6e 70 75 74 73 20 61 6e 64 20 6f  n.  Inputs and o
3567c 75 74 70 75 74 73 20 61 72 65 20 61 6c 77 61 79  utputs are alway
3567d 73 20 74 79 70 65 20 44 4c 5f 44 45 46 41 55 4c  s type DL_DEFAUL
3567e 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  T..*/.static voi
3567f 64 20 64 6f 63 4c 69 73 74 55 6e 69 6f 6e 28 0a  d docListUnion(.
35680 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c    const char *pL
35681 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a  eft, int nLeft,.
35682 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
35683 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67 68 74  ight, int nRight
35684 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  ,.  DataBuffer *
35685 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69  pOut      /* Wri
35686 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20  te the combined 
35687 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  doclist here */.
35688 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65  ){.  DLReader le
35689 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57  ft, right;.  DLW
3568a 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20  riter writer;.. 
3568b 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 29 7b   if( nLeft==0 ){
3568c 0a 20 20 20 20 69 66 28 20 6e 52 69 67 68 74 21  .    if( nRight!
3568d 3d 30 29 20 64 61 74 61 42 75 66 66 65 72 41 70  =0) dataBufferAp
3568e 70 65 6e 64 28 70 4f 75 74 2c 20 70 52 69 67 68  pend(pOut, pRigh
3568f 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 20 20  t, nRight);.    
35690 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66  return;.  }.  if
35691 28 20 6e 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20  ( nRight==0 ){. 
35692 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
35693 65 6e 64 28 70 4f 75 74 2c 20 70 4c 65 66 74 2c  end(pOut, pLeft,
35694 20 6e 4c 65 66 74 29 3b 0a 20 20 20 20 72 65 74   nLeft);.    ret
35695 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 49  urn;.  }..  dlrI
35696 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 45  nit(&left, DL_DE
35697 46 41 55 4c 54 2c 20 70 4c 65 66 74 2c 20 6e 4c  FAULT, pLeft, nL
35698 65 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28  eft);.  dlrInit(
35699 26 72 69 67 68 74 2c 20 44 4c 5f 44 45 46 41 55  &right, DL_DEFAU
3569a 4c 54 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67  LT, pRight, nRig
3569b 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26  ht);.  dlwInit(&
3569c 77 72 69 74 65 72 2c 20 44 4c 5f 44 45 46 41 55  writer, DL_DEFAU
3569d 4c 54 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 77 68  LT, pOut);..  wh
3569e 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26  ile( !dlrAtEnd(&
3569f 6c 65 66 74 29 20 7c 7c 20 21 64 6c 72 41 74 45  left) || !dlrAtE
356a0 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20  nd(&right) ){.  
356a1 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26    if( dlrAtEnd(&
356a2 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  right) ){.      
356a3 64 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c  dlwCopy(&writer,
356a4 20 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64   &left);.      d
356a5 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20  lrStep(&left);. 
356a6 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72     }else if( dlr
356a7 41 74 45 6e 64 28 26 6c 65 66 74 29 20 29 7b 0a  AtEnd(&left) ){.
356a8 20 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77        dlwCopy(&w
356a9 72 69 74 65 72 2c 20 26 72 69 67 68 74 29 3b 0a  riter, &right);.
356aa 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72        dlrStep(&r
356ab 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ight);.    }else
356ac 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 6c   if( dlrDocid(&l
356ad 65 66 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 72  eft)<dlrDocid(&r
356ae 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64  ight) ){.      d
356af 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20  lwCopy(&writer, 
356b0 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64 6c  &left);.      dl
356b1 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
356b2 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44    }else if( dlrD
356b3 6f 63 69 64 28 26 6c 65 66 74 29 3e 64 6c 72 44  ocid(&left)>dlrD
356b4 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a  ocid(&right) ){.
356b5 20 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77        dlwCopy(&w
356b6 72 69 74 65 72 2c 20 26 72 69 67 68 74 29 3b 0a  riter, &right);.
356b7 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72        dlrStep(&r
356b8 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ight);.    }else
356b9 7b 0a 20 20 20 20 20 20 70 6f 73 4c 69 73 74 55  {.      posListU
356ba 6e 69 6f 6e 28 26 6c 65 66 74 2c 20 26 72 69 67  nion(&left, &rig
356bb 68 74 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20  ht, &writer);.  
356bc 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66      dlrStep(&lef
356bd 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  t);.      dlrSte
356be 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
356bf 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65 73 74 72  .  }..  dlrDestr
356c0 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 64 6c 72  oy(&left);.  dlr
356c1 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b  Destroy(&right);
356c2 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26 77  .  dlwDestroy(&w
356c3 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  riter);.}../* .*
356c4 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
356c5 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74 20  is used as part 
356c6 6f 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  of the implement
356c7 61 74 69 6f 6e 20 6f 66 20 70 68 72 61 73 65 20  ation of phrase 
356c8 61 6e 64 0a 2a 2a 20 4e 45 41 52 20 6d 61 74 63  and.** NEAR matc
356c9 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 70 4c 65 66  hing..**.** pLef
356ca 74 20 61 6e 64 20 70 52 69 67 68 74 20 61 72 65  t and pRight are
356cb 20 44 4c 52 65 61 64 65 72 73 20 70 6f 73 69 74   DLReaders posit
356cc 69 6f 6e 65 64 20 74 6f 20 74 68 65 20 73 61 6d  ioned to the sam
356cd 65 20 64 6f 63 69 64 20 69 6e 0a 2a 2a 20 6c 69  e docid in.** li
356ce 73 74 73 20 6f 66 20 74 79 70 65 20 44 4c 5f 50  sts of type DL_P
356cf 4f 53 49 54 49 4f 4e 2e 20 54 68 69 73 20 66 75  OSITION. This fu
356d0 6e 63 74 69 6f 6e 20 77 72 69 74 65 73 20 61 6e  nction writes an
356d1 20 65 6e 74 72 79 20 74 6f 20 74 68 65 0a 2a 2a   entry to the.**
356d2 20 44 4c 57 72 69 74 65 72 20 70 4f 75 74 20 66   DLWriter pOut f
356d3 6f 72 20 65 61 63 68 20 70 6f 73 69 74 69 6f 6e  or each position
356d4 20 69 6e 20 70 52 69 67 68 74 20 74 68 61 74 20   in pRight that 
356d5 69 73 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20  is less than.** 
356d6 28 6e 4e 65 61 72 2b 31 29 20 67 72 65 61 74 65  (nNear+1) greate
356d7 72 20 28 62 75 74 20 6e 6f 74 20 65 71 75 61 6c  r (but not equal
356d8 20 74 6f 20 6f 72 20 73 6d 61 6c 6c 65 72 29 20   to or smaller) 
356d9 74 68 61 6e 20 61 20 70 6f 73 69 74 69 6f 6e 20  than a position 
356da 0a 2a 2a 20 69 6e 20 70 4c 65 66 74 2e 20 46 6f  .** in pLeft. Fo
356db 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 6e 4e  r example, if nN
356dc 65 61 72 20 69 73 20 30 2c 20 61 6e 64 20 74 68  ear is 0, and th
356dd 65 20 70 6f 73 69 74 69 6f 6e 73 20 63 6f 6e 74  e positions cont
356de 61 69 6e 65 64 0a 2a 2a 20 62 79 20 70 4c 65 66  ained.** by pLef
356df 74 20 61 6e 64 20 70 52 69 67 68 74 20 61 72 65  t and pRight are
356e0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 4c 65 66 74  :.**.**    pLeft
356e1 3a 20 20 35 20 31 30 20 31 35 20 32 30 0a 2a 2a  :  5 10 15 20.**
356e2 20 20 20 20 70 52 69 67 68 74 3a 20 36 20 20 39      pRight: 6  9
356e3 20 31 37 20 32 31 0a 2a 2a 0a 2a 2a 20 74 68 65   17 21.**.** the
356e4 6e 20 74 68 65 20 64 6f 63 69 64 20 69 73 20 61  n the docid is a
356e5 64 64 65 64 20 74 6f 20 70 4f 75 74 2e 20 49 66  dded to pOut. If
356e6 20 70 4f 75 74 20 69 73 20 6f 66 20 74 79 70 65   pOut is of type
356e7 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 0a 2a   DL_POSITIONS,.*
356e8 2a 20 74 68 65 6e 20 61 20 70 6f 73 69 74 69 6f  * then a positio
356e9 6e 69 64 73 20 22 36 22 20 61 6e 64 20 22 32 31  nids "6" and "21
356ea 22 20 61 72 65 20 61 6c 73 6f 20 61 64 64 65 64  " are also added
356eb 20 74 6f 20 70 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20   to pOut..**.** 
356ec 49 66 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75 6d  If boolean argum
356ed 65 6e 74 20 69 73 53 61 76 65 4c 65 66 74 20 69  ent isSaveLeft i
356ee 73 20 74 72 75 65 2c 20 74 68 65 6e 20 70 6f 73  s true, then pos
356ef 69 74 69 6f 6e 69 64 73 20 61 72 65 20 63 6f 70  itionids are cop
356f0 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 4c 65 66  ied.** from pLef
356f1 74 20 69 6e 73 74 65 61 64 20 6f 66 20 70 52 69  t instead of pRi
356f2 67 68 74 2e 20 49 6e 20 74 68 65 20 65 78 61 6d  ght. In the exam
356f3 70 6c 65 20 61 62 6f 76 65 2c 20 74 68 65 20 70  ple above, the p
356f4 6f 73 69 74 69 6f 6e 73 20 22 35 22 0a 2a 2a 20  ositions "5".** 
356f5 61 6e 64 20 22 32 30 22 20 77 6f 75 6c 64 20 62  and "20" would b
356f6 65 20 61 64 64 65 64 20 69 6e 73 74 65 61 64 20  e added instead 
356f7 6f 66 20 22 36 22 20 61 6e 64 20 22 32 31 22 2e  of "6" and "21".
356f8 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
356f9 70 6f 73 4c 69 73 74 50 68 72 61 73 65 4d 65 72  posListPhraseMer
356fa 67 65 28 0a 20 20 44 4c 52 65 61 64 65 72 20 2a  ge(.  DLReader *
356fb 70 4c 65 66 74 2c 20 0a 20 20 44 4c 52 65 61 64  pLeft, .  DLRead
356fc 65 72 20 2a 70 52 69 67 68 74 2c 0a 20 20 69 6e  er *pRight,.  in
356fd 74 20 6e 4e 65 61 72 2c 0a 20 20 69 6e 74 20 69  t nNear,.  int i
356fe 73 53 61 76 65 4c 65 66 74 2c 0a 20 20 44 4c 57  sSaveLeft,.  DLW
356ff 72 69 74 65 72 20 2a 70 4f 75 74 0a 29 7b 0a 20  riter *pOut.){. 
35700 20 50 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20   PLReader left, 
35701 72 69 67 68 74 3b 0a 20 20 50 4c 57 72 69 74 65  right;.  PLWrite
35702 72 20 77 72 69 74 65 72 3b 0a 20 20 69 6e 74 20  r writer;.  int 
35703 6d 61 74 63 68 20 3d 20 30 3b 0a 0a 20 20 61 73  match = 0;..  as
35704 73 65 72 74 28 20 64 6c 72 44 6f 63 69 64 28 70  sert( dlrDocid(p
35705 4c 65 66 74 29 3d 3d 64 6c 72 44 6f 63 69 64 28  Left)==dlrDocid(
35706 70 52 69 67 68 74 29 20 29 3b 0a 20 20 61 73 73  pRight) );.  ass
35707 65 72 74 28 20 70 4f 75 74 2d 3e 69 54 79 70 65  ert( pOut->iType
35708 21 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  !=DL_POSITIONS_O
35709 46 46 53 45 54 53 20 29 3b 0a 0a 20 20 70 6c 72  FFSETS );..  plr
3570a 49 6e 69 74 28 26 6c 65 66 74 2c 20 70 4c 65 66  Init(&left, pLef
3570b 74 29 3b 0a 20 20 70 6c 72 49 6e 69 74 28 26 72  t);.  plrInit(&r
3570c 69 67 68 74 2c 20 70 52 69 67 68 74 29 3b 0a 0a  ight, pRight);..
3570d 20 20 77 68 69 6c 65 28 20 21 70 6c 72 41 74 45    while( !plrAtE
3570e 6e 64 28 26 6c 65 66 74 29 20 26 26 20 21 70 6c  nd(&left) && !pl
3570f 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 29  rAtEnd(&right) )
35710 7b 0a 20 20 20 20 69 66 28 20 70 6c 72 43 6f 6c  {.    if( plrCol
35711 75 6d 6e 28 26 6c 65 66 74 29 3c 70 6c 72 43 6f  umn(&left)<plrCo
35712 6c 75 6d 6e 28 26 72 69 67 68 74 29 20 29 7b 0a  lumn(&right) ){.
35713 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c        plrStep(&l
35714 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  eft);.    }else 
35715 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 6c  if( plrColumn(&l
35716 65 66 74 29 3e 70 6c 72 43 6f 6c 75 6d 6e 28 26  eft)>plrColumn(&
35717 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  right) ){.      
35718 70 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  plrStep(&right);
35719 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
3571a 6c 72 50 6f 73 69 74 69 6f 6e 28 26 6c 65 66 74  lrPosition(&left
3571b 29 3e 3d 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26  )>=plrPosition(&
3571c 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  right) ){.      
3571d 70 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  plrStep(&right);
3571e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
3571f 20 20 69 66 28 20 28 70 6c 72 50 6f 73 69 74 69    if( (plrPositi
35720 6f 6e 28 26 72 69 67 68 74 29 2d 70 6c 72 50 6f  on(&right)-plrPo
35721 73 69 74 69 6f 6e 28 26 6c 65 66 74 29 29 3c 3d  sition(&left))<=
35722 28 6e 4e 65 61 72 2b 31 29 20 29 7b 0a 20 20 20  (nNear+1) ){.   
35723 20 20 20 20 20 69 66 28 20 21 6d 61 74 63 68 20       if( !match 
35724 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77  ){.          plw
35725 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 70 4f  Init(&writer, pO
35726 75 74 2c 20 64 6c 72 44 6f 63 69 64 28 70 4c 65  ut, dlrDocid(pLe
35727 66 74 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ft));.          
35728 6d 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 20  match = 1;.     
35729 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
3572a 20 21 69 73 53 61 76 65 4c 65 66 74 20 29 7b 0a   !isSaveLeft ){.
3572b 20 20 20 20 20 20 20 20 20 20 70 6c 77 41 64 64            plwAdd
3572c 28 26 77 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c  (&writer, plrCol
3572d 75 6d 6e 28 26 72 69 67 68 74 29 2c 20 70 6c 72  umn(&right), plr
3572e 50 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74 29  Position(&right)
3572f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
35730 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
35731 20 20 70 6c 77 41 64 64 28 26 77 72 69 74 65 72    plwAdd(&writer
35732 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65 66  , plrColumn(&lef
35733 74 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28  t), plrPosition(
35734 26 6c 65 66 74 29 2c 20 30 2c 20 30 29 3b 0a 20  &left), 0, 0);. 
35735 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
35736 20 70 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   plrStep(&right)
35737 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
35738 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26         plrStep(&
35739 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  left);.      }. 
3573a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
3573b 6d 61 74 63 68 20 29 7b 0a 20 20 20 20 70 6c 77  match ){.    plw
3573c 54 65 72 6d 69 6e 61 74 65 28 26 77 72 69 74 65  Terminate(&write
3573d 72 29 3b 0a 20 20 20 20 70 6c 77 44 65 73 74 72  r);.    plwDestr
3573e 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 7d  oy(&writer);.  }
3573f 0a 0a 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26  ..  plrDestroy(&
35740 6c 65 66 74 29 3b 0a 20 20 70 6c 72 44 65 73 74  left);.  plrDest
35741 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 7d 0a 0a  roy(&right);.}..
35742 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68  /*.** Compare th
35743 65 20 76 61 6c 75 65 73 20 70 6f 69 6e 74 65 64  e values pointed
35744 20 74 6f 20 62 79 20 74 68 65 20 50 4c 52 65 61   to by the PLRea
35745 64 65 72 73 20 70 61 73 73 65 64 20 61 73 20 61  ders passed as a
35746 72 67 75 6d 65 6e 74 73 2e 20 0a 2a 2a 20 52 65  rguments. .** Re
35747 74 75 72 6e 20 2d 31 20 69 66 20 74 68 65 20 76  turn -1 if the v
35748 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  alue pointed to 
35749 62 79 20 70 4c 65 66 74 20 69 73 20 63 6f 6e 73  by pLeft is cons
3574a 69 64 65 72 65 64 20 6c 65 73 73 20 74 68 61 6e  idered less than
3574b 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 70 6f  .** the value po
3574c 69 6e 74 65 64 20 74 6f 20 62 79 20 70 52 69 67  inted to by pRig
3574d 68 74 2c 20 2b 31 20 69 66 20 69 74 20 69 73 20  ht, +1 if it is 
3574e 63 6f 6e 73 69 64 65 72 65 64 20 67 72 65 61 74  considered great
3574f 65 72 0a 2a 2a 20 74 68 61 6e 20 69 74 2c 20 6f  er.** than it, o
35750 72 20 30 20 69 66 20 69 74 20 69 73 20 65 71 75  r 0 if it is equ
35751 61 6c 2e 20 69 2e 65 2e 0a 2a 2a 0a 2a 2a 20 20  al. i.e..**.**  
35752 20 20 20 28 2a 70 4c 65 66 74 20 2d 20 2a 70 52     (*pLeft - *pR
35753 69 67 68 74 29 0a 2a 2a 0a 2a 2a 20 41 20 50 4c  ight).**.** A PL
35754 52 65 61 64 65 72 20 74 68 61 74 20 69 73 20 69  Reader that is i
35755 6e 20 74 68 65 20 45 4f 46 20 63 6f 6e 64 69 74  n the EOF condit
35756 69 6f 6e 20 69 73 20 63 6f 6e 73 69 64 65 72 65  ion is considere
35757 64 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a  d greater than.*
35758 2a 20 61 6e 79 20 6f 74 68 65 72 2e 20 49 66 20  * any other. If 
35759 6e 65 69 74 68 65 72 20 61 72 67 75 6d 65 6e 74  neither argument
3575a 20 69 73 20 69 6e 20 45 4f 46 20 73 74 61 74 65   is in EOF state
3575b 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
3575c 75 65 20 6f 66 0a 2a 2a 20 70 6c 72 43 6f 6c 75  ue of.** plrColu
3575d 6d 6e 28 29 20 69 73 20 75 73 65 64 2e 20 49 66  mn() is used. If
3575e 20 74 68 65 20 70 6c 72 43 6f 6c 75 6d 6e 28 29   the plrColumn()
3575f 20 76 61 6c 75 65 73 20 61 72 65 20 65 71 75 61   values are equa
35760 6c 2c 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 61 72  l, the.** compar
35761 69 73 6f 6e 20 69 73 20 6f 6e 20 74 68 65 20 62  ison is on the b
35762 61 73 69 73 20 6f 66 20 70 6c 72 50 6f 73 69 74  asis of plrPosit
35763 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ion()..*/.static
35764 20 69 6e 74 20 70 6c 72 43 6f 6d 70 61 72 65 28   int plrCompare(
35765 50 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74 2c  PLReader *pLeft,
35766 20 50 4c 52 65 61 64 65 72 20 2a 70 52 69 67 68   PLReader *pRigh
35767 74 29 7b 0a 20 20 61 73 73 65 72 74 28 21 70 6c  t){.  assert(!pl
35768 72 41 74 45 6e 64 28 70 4c 65 66 74 29 20 7c 7c  rAtEnd(pLeft) ||
35769 20 21 70 6c 72 41 74 45 6e 64 28 70 52 69 67 68   !plrAtEnd(pRigh
3576a 74 29 29 3b 0a 0a 20 20 69 66 28 20 70 6c 72 41  t));..  if( plrA
3576b 74 45 6e 64 28 70 52 69 67 68 74 29 20 7c 7c 20  tEnd(pRight) || 
3576c 70 6c 72 41 74 45 6e 64 28 70 4c 65 66 74 29 20  plrAtEnd(pLeft) 
3576d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70  ){.    return (p
3576e 6c 72 41 74 45 6e 64 28 70 52 69 67 68 74 29 20  lrAtEnd(pRight) 
3576f 3f 20 2d 31 20 3a 20 31 29 3b 0a 20 20 7d 0a 20  ? -1 : 1);.  }. 
35770 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 70   if( plrColumn(p
35771 4c 65 66 74 29 21 3d 70 6c 72 43 6f 6c 75 6d 6e  Left)!=plrColumn
35772 28 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20  (pRight) ){.    
35773 72 65 74 75 72 6e 20 28 28 70 6c 72 43 6f 6c 75  return ((plrColu
35774 6d 6e 28 70 4c 65 66 74 29 3c 70 6c 72 43 6f 6c  mn(pLeft)<plrCol
35775 75 6d 6e 28 70 52 69 67 68 74 29 29 20 3f 20 2d  umn(pRight)) ? -
35776 31 20 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66  1 : 1);.  }.  if
35777 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c  ( plrPosition(pL
35778 65 66 74 29 21 3d 70 6c 72 50 6f 73 69 74 69 6f  eft)!=plrPositio
35779 6e 28 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20  n(pRight) ){.   
3577a 20 72 65 74 75 72 6e 20 28 28 70 6c 72 50 6f 73   return ((plrPos
3577b 69 74 69 6f 6e 28 70 4c 65 66 74 29 3c 70 6c 72  ition(pLeft)<plr
3577c 50 6f 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29  Position(pRight)
3577d 29 20 3f 20 2d 31 20 3a 20 31 29 3b 0a 20 20 7d  ) ? -1 : 1);.  }
3577e 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
3577f 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f 20 64  /* We have two d
35780 6f 63 6c 69 73 74 73 20 77 69 74 68 20 70 6f 73  oclists with pos
35781 69 74 69 6f 6e 73 3a 20 20 70 4c 65 66 74 20 61  itions:  pLeft a
35782 6e 64 20 70 52 69 67 68 74 2e 20 44 65 70 65 6e  nd pRight. Depen
35783 64 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 76  ding.** on the v
35784 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 4e 65 61  alue of the nNea
35785 72 20 70 61 72 61 6d 65 74 65 72 2c 20 70 65 72  r parameter, per
35786 66 6f 72 6d 20 65 69 74 68 65 72 20 61 20 70 68  form either a ph
35787 72 61 73 65 0a 2a 2a 20 69 6e 74 65 72 73 65 63  rase.** intersec
35788 74 69 6f 6e 20 28 69 66 20 6e 4e 65 61 72 3d 3d  tion (if nNear==
35789 30 29 20 6f 72 20 61 20 4e 45 41 52 20 69 6e 74  0) or a NEAR int
3578a 65 72 73 65 63 74 69 6f 6e 20 28 69 66 20 6e 4e  ersection (if nN
3578b 65 61 72 3e 30 29 0a 2a 2a 20 61 6e 64 20 77 72  ear>0).** and wr
3578c 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ite the results 
3578d 69 6e 74 6f 20 70 4f 75 74 2e 0a 2a 2a 0a 2a 2a  into pOut..**.**
3578e 20 41 20 70 68 72 61 73 65 20 69 6e 74 65 72 73   A phrase inters
3578f 65 63 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 61  ection means tha
35790 74 20 74 77 6f 20 64 6f 63 75 6d 65 6e 74 73 20  t two documents 
35791 6f 6e 6c 79 20 6d 61 74 63 68 0a 2a 2a 20 69 66  only match.** if
35792 20 70 4c 65 66 74 2e 69 50 6f 73 2b 31 3d 3d 70   pLeft.iPos+1==p
35793 52 69 67 68 74 2e 69 50 6f 73 2e 0a 2a 2a 0a 2a  Right.iPos..**.*
35794 2a 20 41 20 4e 45 41 52 20 69 6e 74 65 72 73 65  * A NEAR interse
35795 63 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 61 74  ction means that
35796 20 74 77 6f 20 64 6f 63 75 6d 65 6e 74 73 20 6f   two documents o
35797 6e 6c 79 20 6d 61 74 63 68 20 69 66 20 0a 2a 2a  nly match if .**
35798 20 28 61 62 73 28 70 4c 65 66 74 2e 69 50 6f 73   (abs(pLeft.iPos
35799 2d 70 52 69 67 68 74 2e 69 50 6f 73 29 3c 6e 4e  -pRight.iPos)<nN
3579a 65 61 72 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ear)..**.** If a
3579b 20 4e 45 41 52 20 69 6e 74 65 72 73 65 63 74 69   NEAR intersecti
3579c 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  on is requested,
3579d 20 74 68 65 6e 20 74 68 65 20 6e 50 68 72 61 73   then the nPhras
3579e 65 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c  e argument shoul
3579f 64 0a 2a 2a 20 62 65 20 70 61 73 73 65 64 20 74  d.** be passed t
357a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b  he number of tok
357a1 65 6e 73 20 69 6e 20 74 68 65 20 74 77 6f 20 6f  ens in the two o
357a2 70 65 72 61 6e 64 73 20 74 6f 20 74 68 65 20 4e  perands to the N
357a3 45 41 52 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20  EAR operator.** 
357a4 63 6f 6d 62 69 6e 65 64 2e 20 46 6f 72 20 65 78  combined. For ex
357a5 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ample:.**.**    
357a6 20 20 20 51 75 65 72 79 20 73 79 6e 74 61 78 20     Query syntax 
357a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 50                nP
357a8 68 72 61 73 65 0a 2a 2a 20 20 20 20 20 20 2d 2d  hrase.**      --
357a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
357aa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
357ab 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 22 41 20 42  --.**       "A B
357ac 20 43 22 20 4e 45 41 52 20 22 44 20 45 22 20 20   C" NEAR "D E"  
357ad 20 20 20 20 20 20 20 35 0a 2a 2a 20 20 20 20 20         5.**     
357ae 20 20 41 20 4e 45 41 52 20 42 20 20 20 20 20 20    A NEAR B      
357af 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 2a               2.*
357b0 2a 0a 2a 2a 20 69 54 79 70 65 20 63 6f 6e 74 72  *.** iType contr
357b1 6f 6c 73 20 74 68 65 20 74 79 70 65 20 6f 66 20  ols the type of 
357b2 64 61 74 61 20 77 72 69 74 74 65 6e 20 74 6f 20  data written to 
357b3 70 4f 75 74 2e 20 20 49 66 20 69 54 79 70 65 20  pOut.  If iType 
357b4 69 73 0a 2a 2a 20 44 4c 5f 50 4f 53 49 54 49 4f  is.** DL_POSITIO
357b5 4e 53 2c 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  NS, the position
357b6 73 20 61 72 65 20 74 68 6f 73 65 20 66 72 6f 6d  s are those from
357b7 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 73 74 61 74   pRight..*/.stat
357b8 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 50  ic void docListP
357b9 68 72 61 73 65 4d 65 72 67 65 28 0a 20 20 63 6f  hraseMerge(.  co
357ba 6e 73 74 20 63 68 61 72 20 2a 70 4c 65 66 74 2c  nst char *pLeft,
357bb 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f   int nLeft,.  co
357bc 6e 73 74 20 63 68 61 72 20 2a 70 52 69 67 68 74  nst char *pRight
357bd 2c 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20  , int nRight,.  
357be 69 6e 74 20 6e 4e 65 61 72 2c 20 20 20 20 20 20  int nNear,      
357bf 20 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 61        /* 0 for a
357c0 20 70 68 72 61 73 65 20 6d 65 72 67 65 2c 20 6e   phrase merge, n
357c1 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 61 20 4e 45  on-zero for a NE
357c2 41 52 20 6d 65 72 67 65 20 2a 2f 0a 20 20 69 6e  AR merge */.  in
357c3 74 20 6e 50 68 72 61 73 65 2c 20 20 20 20 20 20  t nPhrase,      
357c4 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
357c5 20 74 6f 6b 65 6e 73 20 69 6e 20 6c 65 66 74 2b   tokens in left+
357c6 72 69 67 68 74 20 6f 70 65 72 61 6e 64 73 20 74  right operands t
357c7 6f 20 4e 45 41 52 20 2a 2f 0a 20 20 44 6f 63 4c  o NEAR */.  DocL
357c8 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 20 20  istType iType,  
357c9 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 64 6f 63    /* Type of doc
357ca 6c 69 73 74 20 74 6f 20 77 72 69 74 65 20 74 6f  list to write to
357cb 20 70 4f 75 74 20 2a 2f 0a 20 20 44 61 74 61 42   pOut */.  DataB
357cc 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20  uffer *pOut     
357cd 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f   /* Write the co
357ce 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68  mbined doclist h
357cf 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65  ere */.){.  DLRe
357d0 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74  ader left, right
357d1 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69  ;.  DLWriter wri
357d2 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66  ter;..  if( nLef
357d3 74 3d 3d 30 20 7c 7c 20 6e 52 69 67 68 74 3d 3d  t==0 || nRight==
357d4 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61  0 ) return;..  a
357d5 73 73 65 72 74 28 20 69 54 79 70 65 21 3d 44 4c  ssert( iType!=DL
357d6 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45  _POSITIONS_OFFSE
357d7 54 53 20 29 3b 0a 0a 20 20 64 6c 72 49 6e 69 74  TS );..  dlrInit
357d8 28 26 6c 65 66 74 2c 20 44 4c 5f 50 4f 53 49 54  (&left, DL_POSIT
357d9 49 4f 4e 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65  IONS, pLeft, nLe
357da 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26  ft);.  dlrInit(&
357db 72 69 67 68 74 2c 20 44 4c 5f 50 4f 53 49 54 49  right, DL_POSITI
357dc 4f 4e 53 2c 20 70 52 69 67 68 74 2c 20 6e 52 69  ONS, pRight, nRi
357dd 67 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28  ght);.  dlwInit(
357de 26 77 72 69 74 65 72 2c 20 69 54 79 70 65 2c 20  &writer, iType, 
357df 70 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28  pOut);..  while(
357e0 20 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74   !dlrAtEnd(&left
357e1 29 20 26 26 20 21 64 6c 72 41 74 45 6e 64 28 26  ) && !dlrAtEnd(&
357e2 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66  right) ){.    if
357e3 28 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74  ( dlrDocid(&left
357e4 29 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68  )<dlrDocid(&righ
357e5 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53  t) ){.      dlrS
357e6 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  tep(&left);.    
357e7 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63  }else if( dlrDoc
357e8 69 64 28 26 72 69 67 68 74 29 3c 64 6c 72 44 6f  id(&right)<dlrDo
357e9 63 69 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20  cid(&left) ){.  
357ea 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67      dlrStep(&rig
357eb 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ht);.    }else{.
357ec 20 20 20 20 20 20 69 66 28 20 6e 4e 65 61 72 3d        if( nNear=
357ed 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 6f  =0 ){.        po
357ee 73 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65  sListPhraseMerge
357ef 28 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20  (&left, &right, 
357f0 30 2c 20 30 2c 20 26 77 72 69 74 65 72 29 3b 0a  0, 0, &writer);.
357f1 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
357f2 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73       /* This cas
357f3 65 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 77  e occurs when tw
357f4 6f 20 74 65 72 6d 73 20 28 73 69 6d 70 6c 65 20  o terms (simple 
357f5 74 65 72 6d 73 20 6f 72 20 70 68 72 61 73 65 73  terms or phrases
357f6 29 20 61 72 65 0a 20 20 20 20 20 20 20 20 20 2a  ) are.         *
357f7 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 61 20   connected by a 
357f8 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 2c 20 73  NEAR operator, s
357f9 70 61 6e 20 28 6e 4e 65 61 72 2b 31 29 2e 20 69  pan (nNear+1). i
357fa 2e 65 2e 0a 20 20 20 20 20 20 20 20 20 2a 0a 20  .e..         *. 
357fb 20 20 20 20 20 20 20 20 2a 20 20 20 20 20 27 22          *     '"
357fc 74 65 72 72 69 62 6c 65 20 63 6f 6d 70 61 6e 79  terrible company
357fd 22 20 4e 45 41 52 20 77 69 64 67 65 74 27 0a 20  " NEAR widget'. 
357fe 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
357ff 20 20 20 44 61 74 61 42 75 66 66 65 72 20 6f 6e     DataBuffer on
35800 65 20 3d 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 20  e = {0, 0, 0};. 
35801 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65         DataBuffe
35802 72 20 74 77 6f 20 3d 20 7b 30 2c 20 30 2c 20 30  r two = {0, 0, 0
35803 7d 3b 0a 0a 20 20 20 20 20 20 20 20 44 4c 57 72  };..        DLWr
35804 69 74 65 72 20 64 6c 77 72 69 74 65 72 32 3b 0a  iter dlwriter2;.
35805 20 20 20 20 20 20 20 20 44 4c 52 65 61 64 65 72          DLReader
35806 20 64 72 31 20 3d 20 7b 30 2c 20 30 2c 20 30 2c   dr1 = {0, 0, 0,
35807 20 30 2c 20 30 7d 3b 20 0a 20 20 20 20 20 20 20   0, 0}; .       
35808 20 44 4c 52 65 61 64 65 72 20 64 72 32 20 3d 20   DLReader dr2 = 
35809 7b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b  {0, 0, 0, 0, 0};
3580a 0a 0a 20 20 20 20 20 20 20 20 64 6c 77 49 6e 69  ..        dlwIni
3580b 74 28 26 64 6c 77 72 69 74 65 72 32 2c 20 69 54  t(&dlwriter2, iT
3580c 79 70 65 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 20  ype, &one);.    
3580d 20 20 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73      posListPhras
3580e 65 4d 65 72 67 65 28 26 72 69 67 68 74 2c 20 26  eMerge(&right, &
3580f 6c 65 66 74 2c 20 6e 4e 65 61 72 2d 33 2b 6e 50  left, nNear-3+nP
35810 68 72 61 73 65 2c 20 31 2c 20 26 64 6c 77 72 69  hrase, 1, &dlwri
35811 74 65 72 32 29 3b 0a 20 20 20 20 20 20 20 20 64  ter2);.        d
35812 6c 77 49 6e 69 74 28 26 64 6c 77 72 69 74 65 72  lwInit(&dlwriter
35813 32 2c 20 69 54 79 70 65 2c 20 26 74 77 6f 29 3b  2, iType, &two);
35814 0a 20 20 20 20 20 20 20 20 70 6f 73 4c 69 73 74  .        posList
35815 50 68 72 61 73 65 4d 65 72 67 65 28 26 6c 65 66  PhraseMerge(&lef
35816 74 2c 20 26 72 69 67 68 74 2c 20 6e 4e 65 61 72  t, &right, nNear
35817 2d 31 2c 20 30 2c 20 26 64 6c 77 72 69 74 65 72  -1, 0, &dlwriter
35818 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28  2);..        if(
35819 20 6f 6e 65 2e 6e 44 61 74 61 29 20 64 6c 72 49   one.nData) dlrI
3581a 6e 69 74 28 26 64 72 31 2c 20 69 54 79 70 65 2c  nit(&dr1, iType,
3581b 20 6f 6e 65 2e 70 44 61 74 61 2c 20 6f 6e 65 2e   one.pData, one.
3581c 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 20 20  nData);.        
3581d 69 66 28 20 74 77 6f 2e 6e 44 61 74 61 29 20 64  if( two.nData) d
3581e 6c 72 49 6e 69 74 28 26 64 72 32 2c 20 69 54 79  lrInit(&dr2, iTy
3581f 70 65 2c 20 74 77 6f 2e 70 44 61 74 61 2c 20 74  pe, two.pData, t
35820 77 6f 2e 6e 44 61 74 61 29 3b 0a 0a 20 20 20 20  wo.nData);..    
35821 20 20 20 20 69 66 28 20 21 64 6c 72 41 74 45 6e      if( !dlrAtEn
35822 64 28 26 64 72 31 29 20 7c 7c 20 21 64 6c 72 41  d(&dr1) || !dlrA
35823 74 45 6e 64 28 26 64 72 32 29 20 29 7b 0a 20 20  tEnd(&dr2) ){.  
35824 20 20 20 20 20 20 20 20 50 4c 52 65 61 64 65 72          PLReader
35825 20 70 72 31 20 3d 20 7b 30 7d 3b 0a 20 20 20 20   pr1 = {0};.    
35826 20 20 20 20 20 20 50 4c 52 65 61 64 65 72 20 70        PLReader p
35827 72 32 20 3d 20 7b 30 7d 3b 0a 0a 20 20 20 20 20  r2 = {0};..     
35828 20 20 20 20 20 50 4c 57 72 69 74 65 72 20 70 6c       PLWriter pl
35829 77 72 69 74 65 72 3b 0a 20 20 20 20 20 20 20 20  writer;.        
3582a 20 20 70 6c 77 49 6e 69 74 28 26 70 6c 77 72 69    plwInit(&plwri
3582b 74 65 72 2c 20 26 77 72 69 74 65 72 2c 20 64 6c  ter, &writer, dl
3582c 72 44 6f 63 69 64 28 64 6c 72 41 74 45 6e 64 28  rDocid(dlrAtEnd(
3582d 26 64 72 31 29 3f 26 64 72 32 3a 26 64 72 31 29  &dr1)?&dr2:&dr1)
3582e 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 69 66  );..          if
3582f 28 20 6f 6e 65 2e 6e 44 61 74 61 20 29 20 70 6c  ( one.nData ) pl
35830 72 49 6e 69 74 28 26 70 72 31 2c 20 26 64 72 31  rInit(&pr1, &dr1
35831 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
35832 20 74 77 6f 2e 6e 44 61 74 61 20 29 20 70 6c 72   two.nData ) plr
35833 49 6e 69 74 28 26 70 72 32 2c 20 26 64 72 32 29  Init(&pr2, &dr2)
35834 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  ;.          whil
35835 65 28 20 21 70 6c 72 41 74 45 6e 64 28 26 70 72  e( !plrAtEnd(&pr
35836 31 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e 64 28  1) || !plrAtEnd(
35837 26 70 72 32 29 20 29 7b 0a 20 20 20 20 20 20 20  &pr2) ){.       
35838 20 20 20 20 20 69 6e 74 20 69 43 6f 6d 70 61 72       int iCompar
35839 65 20 3d 20 70 6c 72 43 6f 6d 70 61 72 65 28 26  e = plrCompare(&
3583a 70 72 31 2c 20 26 70 72 32 29 3b 0a 20 20 20 20  pr1, &pr2);.    
3583b 20 20 20 20 20 20 20 20 73 77 69 74 63 68 28 20          switch( 
3583c 69 43 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20  iCompare ){.    
3583d 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 2d            case -
3583e 31 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1:.             
3583f 20 20 20 70 6c 77 43 6f 70 79 28 26 70 6c 77 72     plwCopy(&plwr
35840 69 74 65 72 2c 20 26 70 72 31 29 3b 0a 20 20 20  iter, &pr1);.   
35841 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72               plr
35842 53 74 65 70 28 26 70 72 31 29 3b 0a 20 20 20 20  Step(&pr1);.    
35843 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
35844 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
35845 20 63 61 73 65 20 31 3a 0a 20 20 20 20 20 20 20   case 1:.       
35846 20 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70 79           plwCopy
35847 28 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72 32  (&plwriter, &pr2
35848 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
35849 20 20 20 70 6c 72 53 74 65 70 28 26 70 72 32 29     plrStep(&pr2)
3584a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
3584b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
3584c 20 20 20 20 20 20 20 63 61 73 65 20 30 3a 0a 20         case 0:. 
3584d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
3584e 6c 77 43 6f 70 79 28 26 70 6c 77 72 69 74 65 72  lwCopy(&plwriter
3584f 2c 20 26 70 72 31 29 3b 0a 20 20 20 20 20 20 20  , &pr1);.       
35850 20 20 20 20 20 20 20 20 20 70 6c 72 53 74 65 70           plrStep
35851 28 26 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20  (&pr1);.        
35852 20 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28          plrStep(
35853 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20  &pr2);.         
35854 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
35855 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
35856 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
35857 20 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26    plwTerminate(&
35858 70 6c 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20  plwriter);.     
35859 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 61 74     }.        dat
3585a 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
3585b 6f 6e 65 29 3b 0a 20 20 20 20 20 20 20 20 64 61  one);.        da
3585c 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
3585d 26 74 77 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 20  &two);.      }. 
3585e 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65       dlrStep(&le
3585f 66 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  ft);.      dlrSt
35860 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
35861 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65 73 74  }.  }..  dlrDest
35862 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 64 6c  roy(&left);.  dl
35863 72 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29  rDestroy(&right)
35864 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26  ;.  dlwDestroy(&
35865 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 57  writer);.}../* W
35866 65 20 68 61 76 65 20 74 77 6f 20 44 4c 5f 44 4f  e have two DL_DO
35867 43 49 44 53 20 64 6f 63 6c 69 73 74 73 3a 20 20  CIDS doclists:  
35868 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74  pLeft and pRight
35869 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 69  ..** Write the i
3586a 6e 74 65 72 73 65 63 74 69 6f 6e 20 6f 66 20 74  ntersection of t
3586b 68 65 73 65 20 74 77 6f 20 64 6f 63 6c 69 73 74  hese two doclist
3586c 73 20 69 6e 74 6f 20 70 4f 75 74 20 61 73 20 61  s into pOut as a
3586d 0a 2a 2a 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f  .** DL_DOCIDS do
3586e 63 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  clist..*/.static
3586f 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 41 6e 64   void docListAnd
35870 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63  Merge(.  const c
35871 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20  har *pLeft, int 
35872 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63  nLeft,.  const c
35873 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74  har *pRight, int
35874 20 6e 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42   nRight,.  DataB
35875 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20  uffer *pOut     
35876 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f   /* Write the co
35877 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68  mbined doclist h
35878 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65  ere */.){.  DLRe
35879 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74  ader left, right
3587a 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69  ;.  DLWriter wri
3587b 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66  ter;..  if( nLef
3587c 74 3d 3d 30 20 7c 7c 20 6e 52 69 67 68 74 3d 3d  t==0 || nRight==
3587d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 64  0 ) return;..  d
3587e 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c  lrInit(&left, DL
3587f 5f 44 4f 43 49 44 53 2c 20 70 4c 65 66 74 2c 20  _DOCIDS, pLeft, 
35880 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69  nLeft);.  dlrIni
35881 74 28 26 72 69 67 68 74 2c 20 44 4c 5f 44 4f 43  t(&right, DL_DOC
35882 49 44 53 2c 20 70 52 69 67 68 74 2c 20 6e 52 69  IDS, pRight, nRi
35883 67 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28  ght);.  dlwInit(
35884 26 77 72 69 74 65 72 2c 20 44 4c 5f 44 4f 43 49  &writer, DL_DOCI
35885 44 53 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 77 68  DS, pOut);..  wh
35886 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26  ile( !dlrAtEnd(&
35887 6c 65 66 74 29 20 26 26 20 21 64 6c 72 41 74 45  left) && !dlrAtE
35888 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20  nd(&right) ){.  
35889 20 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26    if( dlrDocid(&
3588a 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69 64 28 26  left)<dlrDocid(&
3588b 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  right) ){.      
3588c 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a  dlrStep(&left);.
3588d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c      }else if( dl
3588e 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 3c 64  rDocid(&right)<d
3588f 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 20 29  lrDocid(&left) )
35890 7b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  {.      dlrStep(
35891 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c  &right);.    }el
35892 73 65 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64  se{.      dlwAdd
35893 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63  (&writer, dlrDoc
35894 69 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20 20  id(&left));.    
35895 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29    dlrStep(&left)
35896 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
35897 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20  &right);.    }. 
35898 20 7d 0a 0a 20 20 64 6c 72 44 65 73 74 72 6f 79   }..  dlrDestroy
35899 28 26 6c 65 66 74 29 3b 0a 20 20 64 6c 72 44 65  (&left);.  dlrDe
3589a 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 20  stroy(&right);. 
3589b 20 64 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69   dlwDestroy(&wri
3589c 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68  ter);.}../* We h
3589d 61 76 65 20 74 77 6f 20 44 4c 5f 44 4f 43 49 44  ave two DL_DOCID
3589e 53 20 64 6f 63 6c 69 73 74 73 3a 20 20 70 4c 65  S doclists:  pLe
3589f 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a  ft and pRight..*
358a0 2a 20 57 72 69 74 65 20 74 68 65 20 75 6e 69 6f  * Write the unio
358a1 6e 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 64  n of these two d
358a2 6f 63 6c 69 73 74 73 20 69 6e 74 6f 20 70 4f 75  oclists into pOu
358a3 74 20 61 73 20 61 0a 2a 2a 20 44 4c 5f 44 4f 43  t as a.** DL_DOC
358a4 49 44 53 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a  IDS doclist..*/.
358a5 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c  static void docL
358a6 69 73 74 4f 72 4d 65 72 67 65 28 0a 20 20 63 6f  istOrMerge(.  co
358a7 6e 73 74 20 63 68 61 72 20 2a 70 4c 65 66 74 2c  nst char *pLeft,
358a8 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f   int nLeft,.  co
358a9 6e 73 74 20 63 68 61 72 20 2a 70 52 69 67 68 74  nst char *pRight
358aa 2c 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20  , int nRight,.  
358ab 44 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75 74  DataBuffer *pOut
358ac 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
358ad 68 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c  he combined docl
358ae 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ist here */.){. 
358af 20 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20   DLReader left, 
358b0 72 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74 65  right;.  DLWrite
358b1 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66 28  r writer;..  if(
358b2 20 6e 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20   nLeft==0 ){.   
358b3 20 69 66 28 20 6e 52 69 67 68 74 21 3d 30 20 29   if( nRight!=0 )
358b4 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
358b5 64 28 70 4f 75 74 2c 20 70 52 69 67 68 74 2c 20  d(pOut, pRight, 
358b6 6e 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74  nRight);.    ret
358b7 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  urn;.  }.  if( n
358b8 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20  Right==0 ){.    
358b9 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
358ba 28 70 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e 4c  (pOut, pLeft, nL
358bb 65 66 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  eft);.    return
358bc 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69 74  ;.  }..  dlrInit
358bd 28 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49 44  (&left, DL_DOCID
358be 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29  S, pLeft, nLeft)
358bf 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67  ;.  dlrInit(&rig
358c0 68 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70  ht, DL_DOCIDS, p
358c1 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a  Right, nRight);.
358c2 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65    dlwInit(&write
358c3 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4f  r, DL_DOCIDS, pO
358c4 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21  ut);..  while( !
358c5 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20  dlrAtEnd(&left) 
358c6 7c 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69  || !dlrAtEnd(&ri
358c7 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20  ght) ){.    if( 
358c8 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29  dlrAtEnd(&right)
358c9 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64   ){.      dlwAdd
358ca 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63  (&writer, dlrDoc
358cb 69 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20 20  id(&left));.    
358cc 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29    dlrStep(&left)
358cd 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
358ce 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20  dlrAtEnd(&left) 
358cf 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64 28  ){.      dlwAdd(
358d0 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69  &writer, dlrDoci
358d1 64 28 26 72 69 67 68 74 29 29 3b 0a 20 20 20 20  d(&right));.    
358d2 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74    dlrStep(&right
358d3 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
358d4 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
358d5 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74  <dlrDocid(&right
358d6 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64  ) ){.      dlwAd
358d7 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f  d(&writer, dlrDo
358d8 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20  cid(&left));.   
358d9 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74     dlrStep(&left
358da 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
358db 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74   dlrDocid(&right
358dc 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74  )<dlrDocid(&left
358dd 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64  ) ){.      dlwAd
358de 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f  d(&writer, dlrDo
358df 63 69 64 28 26 72 69 67 68 74 29 29 3b 0a 20 20  cid(&right));.  
358e0 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67      dlrStep(&rig
358e1 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ht);.    }else{.
358e2 20 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72        dlwAdd(&wr
358e3 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26  iter, dlrDocid(&
358e4 6c 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c  left));.      dl
358e5 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
358e6 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67      dlrStep(&rig
358e7 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ht);.    }.  }..
358e8 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65    dlrDestroy(&le
358e9 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f  ft);.  dlrDestro
358ea 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77  y(&right);.  dlw
358eb 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
358ec 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20  ;.}../* We have 
358ed 74 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f  two DL_DOCIDS do
358ee 63 6c 69 73 74 73 3a 20 20 70 4c 65 66 74 20 61  clists:  pLeft a
358ef 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72  nd pRight..** Wr
358f0 69 74 65 20 69 6e 74 6f 20 70 4f 75 74 20 61 73  ite into pOut as
358f1 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69   DL_DOCIDS docli
358f2 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c  st containing al
358f3 6c 20 64 6f 63 75 6d 65 6e 74 73 20 74 68 61 74  l documents that
358f4 0a 2a 2a 20 6f 63 63 75 72 20 69 6e 20 70 4c 65  .** occur in pLe
358f5 66 74 20 62 75 74 20 6e 6f 74 20 69 6e 20 70 52  ft but not in pR
358f6 69 67 68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ight..*/.static 
358f7 76 6f 69 64 20 64 6f 63 4c 69 73 74 45 78 63 65  void docListExce
358f8 70 74 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74  ptMerge(.  const
358f9 20 63 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e   char *pLeft, in
358fa 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74  t nLeft,.  const
358fb 20 63 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69   char *pRight, i
358fc 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 44 61 74  nt nRight,.  Dat
358fd 61 42 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20  aBuffer *pOut   
358fe 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
358ff 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74  combined doclist
35900 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c   here */.){.  DL
35901 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67  Reader left, rig
35902 68 74 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77  ht;.  DLWriter w
35903 72 69 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c  riter;..  if( nL
35904 65 66 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  eft==0 ) return;
35905 0a 20 20 69 66 28 20 6e 52 69 67 68 74 3d 3d 30  .  if( nRight==0
35906 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66   ){.    dataBuff
35907 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 70  erAppend(pOut, p
35908 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20  Left, nLeft);.  
35909 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
3590a 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20   dlrInit(&left, 
3590b 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4c 65 66 74  DL_DOCIDS, pLeft
3590c 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72 49  , nLeft);.  dlrI
3590d 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c 5f 44  nit(&right, DL_D
3590e 4f 43 49 44 53 2c 20 70 52 69 67 68 74 2c 20 6e  OCIDS, pRight, n
3590f 52 69 67 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69  Right);.  dlwIni
35910 74 28 26 77 72 69 74 65 72 2c 20 44 4c 5f 44 4f  t(&writer, DL_DO
35911 43 49 44 53 2c 20 70 4f 75 74 29 3b 0a 0a 20 20  CIDS, pOut);..  
35912 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64  while( !dlrAtEnd
35913 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 77  (&left) ){.    w
35914 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28  hile( !dlrAtEnd(
35915 26 72 69 67 68 74 29 20 26 26 20 64 6c 72 44 6f  &right) && dlrDo
35916 63 69 64 28 26 72 69 67 68 74 29 3c 64 6c 72 44  cid(&right)<dlrD
35917 6f 63 69 64 28 26 6c 65 66 74 29 20 29 7b 0a 20  ocid(&left) ){. 
35918 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69       dlrStep(&ri
35919 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ght);.    }.    
3591a 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26 72 69  if( dlrAtEnd(&ri
3591b 67 68 74 29 20 7c 7c 20 64 6c 72 44 6f 63 69 64  ght) || dlrDocid
3591c 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69 64  (&left)<dlrDocid
3591d 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20  (&right) ){.    
3591e 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72    dlwAdd(&writer
3591f 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74  , dlrDocid(&left
35920 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 6c  ));.    }.    dl
35921 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
35922 7d 0a 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28  }..  dlrDestroy(
35923 26 6c 65 66 74 29 3b 0a 20 20 64 6c 72 44 65 73  &left);.  dlrDes
35924 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20  troy(&right);.  
35925 64 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74  dlwDestroy(&writ
35926 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63  er);.}..static c
35927 68 61 72 20 2a 73 74 72 69 6e 67 5f 64 75 70 5f  har *string_dup_
35928 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 2c  n(const char *s,
35929 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20   int n){.  char 
3592a 2a 73 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  *str = sqlite3_m
3592b 61 6c 6c 6f 63 28 6e 20 2b 20 31 29 3b 0a 20 20  alloc(n + 1);.  
3592c 6d 65 6d 63 70 79 28 73 74 72 2c 20 73 2c 20 6e  memcpy(str, s, n
3592d 29 3b 0a 20 20 73 74 72 5b 6e 5d 20 3d 20 27 5c  );.  str[n] = '\
3592e 30 27 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72  0';.  return str
3592f 3b 0a 7d 0a 0a 2f 2a 20 44 75 70 6c 69 63 61 74  ;.}../* Duplicat
35930 65 20 61 20 73 74 72 69 6e 67 3b 20 74 68 65 20  e a string; the 
35931 63 61 6c 6c 65 72 20 6d 75 73 74 20 66 72 65 65  caller must free
35932 28 29 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  () the returned 
35933 73 74 72 69 6e 67 2e 0a 20 2a 20 28 57 65 20 64  string.. * (We d
35934 6f 6e 27 74 20 75 73 65 20 73 74 72 64 75 70 28  on't use strdup(
35935 29 20 73 69 6e 63 65 20 69 74 20 69 73 20 6e 6f  ) since it is no
35936 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 73 74  t part of the st
35937 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79  andard C library
35938 20 61 6e 64 0a 20 2a 20 6d 61 79 20 6e 6f 74 20   and. * may not 
35939 62 65 20 61 76 61 69 6c 61 62 6c 65 20 65 76 65  be available eve
3593a 72 79 77 68 65 72 65 2e 29 20 2a 2f 0a 73 74 61  rywhere.) */.sta
3593b 74 69 63 20 63 68 61 72 20 2a 73 74 72 69 6e 67  tic char *string
3593c 5f 64 75 70 28 63 6f 6e 73 74 20 63 68 61 72 20  _dup(const char 
3593d 2a 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 74  *s){.  return st
3593e 72 69 6e 67 5f 64 75 70 5f 6e 28 73 2c 20 73 74  ring_dup_n(s, st
3593f 72 6c 65 6e 28 73 29 29 3b 0a 7d 0a 0a 2f 2a 20  rlen(s));.}../* 
35940 46 6f 72 6d 61 74 20 61 20 73 74 72 69 6e 67 2c  Format a string,
35941 20 72 65 70 6c 61 63 69 6e 67 20 65 61 63 68 20   replacing each 
35942 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 20 74 68  occurrence of th
35943 65 20 25 20 63 68 61 72 61 63 74 65 72 20 77 69  e % character wi
35944 74 68 0a 20 2a 20 7a 44 62 2e 7a 4e 61 6d 65 2e  th. * zDb.zName.
35945 20 20 54 68 69 73 20 6d 61 79 20 62 65 20 6d 6f    This may be mo
35946 72 65 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 68  re convenient th
35947 61 6e 20 73 71 6c 69 74 65 5f 6d 70 72 69 6e 74  an sqlite_mprint
35948 66 28 29 0a 20 2a 20 77 68 65 6e 20 6f 6e 65 20  f(). * when one 
35949 73 74 72 69 6e 67 20 69 73 20 75 73 65 64 20 72  string is used r
3594a 65 70 65 61 74 65 64 6c 79 20 69 6e 20 61 20 66  epeatedly in a f
3594b 6f 72 6d 61 74 20 73 74 72 69 6e 67 2e 0a 20 2a  ormat string.. *
3594c 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   The caller must
3594d 20 66 72 65 65 28 29 20 74 68 65 20 72 65 74 75   free() the retu
3594e 72 6e 65 64 20 73 74 72 69 6e 67 2e 20 2a 2f 0a  rned string. */.
3594f 73 74 61 74 69 63 20 63 68 61 72 20 2a 73 74 72  static char *str
35950 69 6e 67 5f 66 6f 72 6d 61 74 28 63 6f 6e 73 74  ing_format(const
35951 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a   char *zFormat,.
35952 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35953 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
35954 20 63 68 61 72 20 2a 7a 44 62 2c 20 63 6f 6e 73   char *zDb, cons
35955 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a  t char *zName){.
35956 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 3b    const char *p;
35957 0a 20 20 73 69 7a 65 5f 74 20 6c 65 6e 20 3d 20  .  size_t len = 
35958 30 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 44 62 20  0;.  size_t nDb 
35959 3d 20 73 74 72 6c 65 6e 28 7a 44 62 29 3b 0a 20  = strlen(zDb);. 
3595a 20 73 69 7a 65 5f 74 20 6e 4e 61 6d 65 20 3d 20   size_t nName = 
3595b 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20  strlen(zName);. 
3595c 20 73 69 7a 65 5f 74 20 6e 46 75 6c 6c 54 61 62   size_t nFullTab
3595d 6c 65 4e 61 6d 65 20 3d 20 6e 44 62 2b 31 2b 6e  leName = nDb+1+n
3595e 4e 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 72 65  Name;.  char *re
3595f 73 75 6c 74 3b 0a 20 20 63 68 61 72 20 2a 72 3b  sult;.  char *r;
35960 0a 0a 20 20 2f 2a 20 66 69 72 73 74 20 63 6f 6d  ..  /* first com
35961 70 75 74 65 20 6c 65 6e 67 74 68 20 6e 65 65 64  pute length need
35962 65 64 20 2a 2f 0a 20 20 66 6f 72 28 70 20 3d 20  ed */.  for(p = 
35963 7a 46 6f 72 6d 61 74 20 3b 20 2a 70 20 3b 20 2b  zFormat ; *p ; +
35964 2b 70 29 7b 0a 20 20 20 20 6c 65 6e 20 2b 3d 20  +p){.    len += 
35965 28 2a 70 3d 3d 27 25 27 20 3f 20 6e 46 75 6c 6c  (*p=='%' ? nFull
35966 54 61 62 6c 65 4e 61 6d 65 20 3a 20 31 29 3b 0a  TableName : 1);.
35967 20 20 7d 0a 20 20 6c 65 6e 20 2b 3d 20 31 3b 20    }.  len += 1; 
35968 20 2f 2a 20 66 6f 72 20 6e 75 6c 6c 20 74 65 72   /* for null ter
35969 6d 69 6e 61 74 6f 72 20 2a 2f 0a 0a 20 20 72 20  minator */..  r 
3596a 3d 20 72 65 73 75 6c 74 20 3d 20 73 71 6c 69 74  = result = sqlit
3596b 65 33 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 29 3b 0a  e3_malloc(len);.
3596c 20 20 66 6f 72 28 70 20 3d 20 7a 46 6f 72 6d 61    for(p = zForma
3596d 74 3b 20 2a 70 3b 20 2b 2b 70 29 7b 0a 20 20 20  t; *p; ++p){.   
3596e 20 69 66 28 20 2a 70 3d 3d 27 25 27 20 29 7b 0a   if( *p=='%' ){.
3596f 20 20 20 20 20 20 6d 65 6d 63 70 79 28 72 2c 20        memcpy(r, 
35970 7a 44 62 2c 20 6e 44 62 29 3b 0a 20 20 20 20 20  zDb, nDb);.     
35971 20 72 20 2b 3d 20 6e 44 62 3b 0a 20 20 20 20 20   r += nDb;.     
35972 20 2a 72 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20   *r++ = '.';.   
35973 20 20 20 6d 65 6d 63 70 79 28 72 2c 20 7a 4e 61     memcpy(r, zNa
35974 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
35975 20 20 72 20 2b 3d 20 6e 4e 61 6d 65 3b 0a 20 20    r += nName;.  
35976 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
35977 20 2a 72 2b 2b 20 3d 20 2a 70 3b 0a 20 20 20 20   *r++ = *p;.    
35978 7d 0a 20 20 7d 0a 20 20 2a 72 2b 2b 20 3d 20 27  }.  }.  *r++ = '
35979 5c 30 27 3b 0a 20 20 61 73 73 65 72 74 28 20 72  \0';.  assert( r
3597a 20 3d 3d 20 72 65 73 75 6c 74 20 2b 20 6c 65 6e   == result + len
3597b 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73   );.  return res
3597c 75 6c 74 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  ult;.}..static i
3597d 6e 74 20 73 71 6c 5f 65 78 65 63 28 73 71 6c 69  nt sql_exec(sqli
3597e 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
3597f 68 61 72 20 2a 7a 44 62 2c 20 63 6f 6e 73 74 20  har *zDb, const 
35980 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20  char *zName,.   
35981 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35982 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
35983 72 6d 61 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a  rmat){.  char *z
35984 43 6f 6d 6d 61 6e 64 20 3d 20 73 74 72 69 6e 67  Command = string
35985 5f 66 6f 72 6d 61 74 28 7a 46 6f 72 6d 61 74 2c  _format(zFormat,
35986 20 7a 44 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   zDb, zName);.  
35987 69 6e 74 20 72 63 3b 0a 20 20 46 54 53 54 52 41  int rc;.  FTSTRA
35988 43 45 28 28 22 46 54 53 33 20 73 71 6c 3a 20 25  CE(("FTS3 sql: %
35989 73 5c 6e 22 2c 20 7a 43 6f 6d 6d 61 6e 64 29 29  s\n", zCommand))
3598a 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
3598b 5f 65 78 65 63 28 64 62 2c 20 7a 43 6f 6d 6d 61  _exec(db, zComma
3598c 6e 64 2c 20 4e 55 4c 4c 2c 20 30 2c 20 4e 55 4c  nd, NULL, 0, NUL
3598d 4c 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  L);.  sqlite3_fr
3598e 65 65 28 7a 43 6f 6d 6d 61 6e 64 29 3b 0a 20 20  ee(zCommand);.  
3598f 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74  return rc;.}..st
35990 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 70 72 65  atic int sql_pre
35991 70 61 72 65 28 73 71 6c 69 74 65 33 20 2a 64 62  pare(sqlite3 *db
35992 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
35993 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
35994 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Name,.          
35995 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
35996 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
35997 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  mt, const char *
35998 7a 46 6f 72 6d 61 74 29 7b 0a 20 20 63 68 61 72  zFormat){.  char
35999 20 2a 7a 43 6f 6d 6d 61 6e 64 20 3d 20 73 74 72   *zCommand = str
3599a 69 6e 67 5f 66 6f 72 6d 61 74 28 7a 46 6f 72 6d  ing_format(zForm
3599b 61 74 2c 20 7a 44 62 2c 20 7a 4e 61 6d 65 29 3b  at, zDb, zName);
3599c 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 46 54 53  .  int rc;.  FTS
3599d 54 52 41 43 45 28 28 22 46 54 53 33 20 70 72 65  TRACE(("FTS3 pre
3599e 70 61 72 65 3a 20 25 73 5c 6e 22 2c 20 7a 43 6f  pare: %s\n", zCo
3599f 6d 6d 61 6e 64 29 29 3b 0a 20 20 72 63 20 3d 20  mmand));.  rc = 
359a0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
359a1 76 32 28 64 62 2c 20 7a 43 6f 6d 6d 61 6e 64 2c  v2(db, zCommand,
359a2 20 2d 31 2c 20 70 70 53 74 6d 74 2c 20 4e 55 4c   -1, ppStmt, NUL
359a3 4c 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  L);.  sqlite3_fr
359a4 65 65 28 7a 43 6f 6d 6d 61 6e 64 29 3b 0a 20 20  ee(zCommand);.  
359a5 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
359a6 20 65 6e 64 20 75 74 69 6c 69 74 79 20 66 75 6e   end utility fun
359a7 63 74 69 6f 6e 73 20 2a 2f 0a 0a 2f 2a 20 46 6f  ctions */../* Fo
359a8 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 20  rward reference 
359a9 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
359aa 74 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t fulltext_vtab 
359ab 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 3b 0a 0a  fulltext_vtab;..
359ac 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
359ad 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
359ae 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  ng structure kee
359af 70 73 20 74 72 61 63 6b 20 6f 66 20 67 65 6e 65  ps track of gene
359b0 72 61 74 65 64 0a 2a 2a 20 6d 61 74 63 68 69 6e  rated.** matchin
359b1 67 2d 77 6f 72 64 20 6f 66 66 73 65 74 20 69 6e  g-word offset in
359b2 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 73 6e  formation and sn
359b3 69 70 70 65 74 73 2e 0a 2a 2f 0a 74 79 70 65 64  ippets..*/.typed
359b4 65 66 20 73 74 72 75 63 74 20 53 6e 69 70 70 65  ef struct Snippe
359b5 74 20 7b 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68  t {.  int nMatch
359b6 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e  ;     /* Total n
359b7 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 65 73  umber of matches
359b8 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63   */.  int nAlloc
359b9 3b 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 61  ;     /* Space a
359ba 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 4d 61  llocated for aMa
359bb 74 63 68 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63  tch[] */.  struc
359bc 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 7b  t snippetMatch {
359bd 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f   /* One entry fo
359be 72 20 65 61 63 68 20 6d 61 74 63 68 69 6e 67 20  r each matching 
359bf 74 65 72 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72  term */.    char
359c0 20 73 6e 53 74 61 74 75 73 3b 20 20 20 20 20 20   snStatus;      
359c1 20 2f 2a 20 53 74 61 74 75 73 20 66 6c 61 67 20   /* Status flag 
359c2 66 6f 72 20 75 73 65 20 77 68 69 6c 65 20 63 6f  for use while co
359c3 6e 73 74 72 75 63 74 69 6e 67 20 73 6e 69 70 70  nstructing snipp
359c4 65 74 73 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74  ets */.    short
359c5 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20   int iCol;      
359c6 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 74 68  /* The column th
359c7 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
359c8 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 73 68 6f  match */.    sho
359c9 72 74 20 69 6e 74 20 69 54 65 72 6d 3b 20 20 20  rt int iTerm;   
359ca 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 69    /* The index i
359cb 6e 20 51 75 65 72 79 2e 70 54 65 72 6d 73 5b 5d  n Query.pTerms[]
359cc 20 6f 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67   of the matching
359cd 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74   term */.    int
359ce 20 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20   iToken;        
359cf 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 6f    /* The index o
359d0 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 64  f the matching d
359d1 6f 63 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f  ocument token */
359d2 0a 20 20 20 20 73 68 6f 72 74 20 69 6e 74 20 6e  .    short int n
359d3 42 79 74 65 3b 20 20 20 20 20 2f 2a 20 4e 75 6d  Byte;     /* Num
359d4 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
359d5 74 68 65 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20  the term */.    
359d6 69 6e 74 20 69 53 74 61 72 74 3b 20 20 20 20 20  int iStart;     
359d7 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 66 66 73       /* The offs
359d8 65 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  et to the first 
359d9 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65  character of the
359da 20 74 65 72 6d 20 2a 2f 0a 20 20 7d 20 2a 61 4d   term */.  } *aM
359db 61 74 63 68 3b 20 20 20 20 20 20 2f 2a 20 50 6f  atch;      /* Po
359dc 69 6e 74 73 20 74 6f 20 73 70 61 63 65 20 6f 62  ints to space ob
359dd 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
359de 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f  oc */.  char *zO
359df 66 66 73 65 74 3b 20 20 2f 2a 20 54 65 78 74 20  ffset;  /* Text 
359e0 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 61 4d 61  rendering of aMa
359e1 74 63 68 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  tch[] */.  int n
359e2 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 73 74  Offset;    /* st
359e3 72 6c 65 6e 28 7a 4f 66 66 73 65 74 29 20 2a 2f  rlen(zOffset) */
359e4 0a 20 20 63 68 61 72 20 2a 7a 53 6e 69 70 70 65  .  char *zSnippe
359e5 74 3b 20 2f 2a 20 53 6e 69 70 70 65 74 20 74 65  t; /* Snippet te
359e6 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6e 69  xt */.  int nSni
359e7 70 70 65 74 3b 20 20 20 2f 2a 20 73 74 72 6c 65  ppet;   /* strle
359e8 6e 28 7a 53 6e 69 70 70 65 74 29 20 2a 2f 0a 7d  n(zSnippet) */.}
359e9 20 53 6e 69 70 70 65 74 3b 0a 0a 0a 74 79 70 65   Snippet;...type
359ea 64 65 66 20 65 6e 75 6d 20 51 75 65 72 79 54 79  def enum QueryTy
359eb 70 65 20 7b 0a 20 20 51 55 45 52 59 5f 47 45 4e  pe {.  QUERY_GEN
359ec 45 52 49 43 2c 20 20 20 2f 2a 20 74 61 62 6c 65  ERIC,   /* table
359ed 20 73 63 61 6e 20 2a 2f 0a 20 20 51 55 45 52 59   scan */.  QUERY
359ee 5f 44 4f 43 49 44 2c 20 20 20 20 20 2f 2a 20 6c  _DOCID,     /* l
359ef 6f 6f 6b 75 70 20 62 79 20 64 6f 63 69 64 20 2a  ookup by docid *
359f0 2f 0a 20 20 51 55 45 52 59 5f 46 55 4c 4c 54 45  /.  QUERY_FULLTE
359f1 58 54 20 20 20 2f 2a 20 51 55 45 52 59 5f 46 55  XT   /* QUERY_FU
359f2 4c 4c 54 45 58 54 20 2b 20 5b 69 5d 20 69 73 20  LLTEXT + [i] is 
359f3 61 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72  a full-text sear
359f4 63 68 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 69 2a  ch for column i*
359f5 2f 0a 7d 20 51 75 65 72 79 54 79 70 65 3b 0a 0a  /.} QueryType;..
359f6 74 79 70 65 64 65 66 20 65 6e 75 6d 20 66 75 6c  typedef enum ful
359f7 6c 74 65 78 74 5f 73 74 61 74 65 6d 65 6e 74 20  ltext_statement 
359f8 7b 0a 20 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45  {.  CONTENT_INSE
359f9 52 54 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45  RT_STMT,.  CONTE
359fa 4e 54 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 0a  NT_SELECT_STMT,.
359fb 20 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45    CONTENT_UPDATE
359fc 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54  _STMT,.  CONTENT
359fd 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 20 20  _DELETE_STMT,.  
359fe 43 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53 5f 53  CONTENT_EXISTS_S
359ff 54 4d 54 2c 0a 0a 20 20 42 4c 4f 43 4b 5f 49 4e  TMT,..  BLOCK_IN
35a00 53 45 52 54 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f  SERT_STMT,.  BLO
35a01 43 4b 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 0a  CK_SELECT_STMT,.
35a02 20 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 53    BLOCK_DELETE_S
35a03 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 44 45 4c  TMT,.  BLOCK_DEL
35a04 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 0a 20  ETE_ALL_STMT,.. 
35a05 20 53 45 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45   SEGDIR_MAX_INDE
35a06 58 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52  X_STMT,.  SEGDIR
35a07 5f 53 45 54 5f 53 54 4d 54 2c 0a 20 20 53 45 47  _SET_STMT,.  SEG
35a08 44 49 52 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c  DIR_SELECT_LEVEL
35a09 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f  _STMT,.  SEGDIR_
35a0a 53 50 41 4e 5f 53 54 4d 54 2c 0a 20 20 53 45 47  SPAN_STMT,.  SEG
35a0b 44 49 52 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c  DIR_DELETE_STMT,
35a0c 0a 20 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54  .  SEGDIR_SELECT
35a0d 5f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 0a 20  _SEGMENT_STMT,. 
35a0e 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 41   SEGDIR_SELECT_A
35a0f 4c 4c 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49  LL_STMT,.  SEGDI
35a10 52 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d  R_DELETE_ALL_STM
35a11 54 2c 0a 20 20 53 45 47 44 49 52 5f 43 4f 55 4e  T,.  SEGDIR_COUN
35a12 54 5f 53 54 4d 54 2c 0a 0a 20 20 4d 41 58 5f 53  T_STMT,..  MAX_S
35a13 54 4d 54 20 20 20 20 20 20 20 20 20 20 20 20 20  TMT             
35a14 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 77 61 79          /* Alway
35a15 73 20 61 74 20 65 6e 64 21 20 2a 2f 0a 7d 20 66  s at end! */.} f
35a16 75 6c 6c 74 65 78 74 5f 73 74 61 74 65 6d 65 6e  ulltext_statemen
35a17 74 3b 0a 0a 2f 2a 20 54 68 65 73 65 20 6d 75 73  t;../* These mus
35a18 74 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 20  t exactly match 
35a19 74 68 65 20 65 6e 75 6d 20 61 62 6f 76 65 2e 20  the enum above. 
35a1a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  */./* TODO(shess
35a1b 29 3a 20 49 73 20 74 68 65 72 65 20 73 6f 6d 65  ): Is there some
35a1c 20 72 69 73 6b 20 74 68 61 74 20 61 20 73 74 61   risk that a sta
35a1d 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 65 20 75  tement will be u
35a1e 73 65 64 20 69 6e 20 74 77 6f 0a 2a 2a 20 63 75  sed in two.** cu
35a1f 72 73 6f 72 73 20 61 74 20 6f 6e 63 65 2c 20 65  rsors at once, e
35a20 2e 67 2e 20 20 69 66 20 61 20 71 75 65 72 79 20  .g.  if a query 
35a21 6a 6f 69 6e 73 20 61 20 76 69 72 74 75 61 6c 20  joins a virtual 
35a22 74 61 62 6c 65 20 74 6f 20 69 74 73 65 6c 66 3f  table to itself?
35a23 0a 2a 2a 20 49 66 20 73 6f 20 70 65 72 68 61 70  .** If so perhap
35a24 73 20 77 65 20 73 68 6f 75 6c 64 20 6d 6f 76 65  s we should move
35a25 20 73 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 74   some of these t
35a26 6f 20 74 68 65 20 63 75 72 73 6f 72 20 6f 62 6a  o the cursor obj
35a27 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ect..*/.static c
35a28 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
35a29 20 66 75 6c 6c 74 65 78 74 5f 7a 53 74 61 74 65   fulltext_zState
35a2a 6d 65 6e 74 5b 4d 41 58 5f 53 54 4d 54 5d 20 3d  ment[MAX_STMT] =
35a2b 20 7b 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f   {.  /* CONTENT_
35a2c 49 4e 53 45 52 54 20 2a 2f 20 4e 55 4c 4c 2c 20  INSERT */ NULL, 
35a2d 20 2f 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e   /* generated in
35a2e 20 63 6f 6e 74 65 6e 74 49 6e 73 65 72 74 53 74   contentInsertSt
35a2f 61 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f  atement() */.  /
35a30 2a 20 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54  * CONTENT_SELECT
35a31 20 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67 65   */ NULL,  /* ge
35a32 6e 65 72 61 74 65 64 20 69 6e 20 63 6f 6e 74 65  nerated in conte
35a33 6e 74 53 65 6c 65 63 74 53 74 61 74 65 6d 65 6e  ntSelectStatemen
35a34 74 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e 54  t() */.  /* CONT
35a35 45 4e 54 5f 55 50 44 41 54 45 20 2a 2f 20 4e 55  ENT_UPDATE */ NU
35a36 4c 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61 74 65  LL,  /* generate
35a37 64 20 69 6e 20 63 6f 6e 74 65 6e 74 55 70 64 61  d in contentUpda
35a38 74 65 53 74 61 74 65 6d 65 6e 74 28 29 20 2a 2f  teStatement() */
35a39 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 44 45  .  /* CONTENT_DE
35a3a 4c 45 54 45 20 2a 2f 20 22 64 65 6c 65 74 65 20  LETE */ "delete 
35a3b 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77  from %_content w
35a3c 68 65 72 65 20 64 6f 63 69 64 20 3d 20 3f 22 2c  here docid = ?",
35a3d 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 45 58  .  /* CONTENT_EX
35a3e 49 53 54 53 20 2a 2f 20 22 73 65 6c 65 63 74 20  ISTS */ "select 
35a3f 64 6f 63 69 64 20 66 72 6f 6d 20 25 5f 63 6f 6e  docid from %_con
35a40 74 65 6e 74 20 6c 69 6d 69 74 20 31 22 2c 0a 0a  tent limit 1",..
35a41 20 20 2f 2a 20 42 4c 4f 43 4b 5f 49 4e 53 45 52    /* BLOCK_INSER
35a42 54 20 2a 2f 0a 20 20 22 69 6e 73 65 72 74 20 69  T */.  "insert i
35a43 6e 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 20 28  nto %_segments (
35a44 62 6c 6f 63 6b 69 64 2c 20 62 6c 6f 63 6b 29 20  blockid, block) 
35a45 76 61 6c 75 65 73 20 28 6e 75 6c 6c 2c 20 3f 29  values (null, ?)
35a46 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f 53 45  ",.  /* BLOCK_SE
35a47 4c 45 43 54 20 2a 2f 20 22 73 65 6c 65 63 74 20  LECT */ "select 
35a48 62 6c 6f 63 6b 20 66 72 6f 6d 20 25 5f 73 65 67  block from %_seg
35a49 6d 65 6e 74 73 20 77 68 65 72 65 20 62 6c 6f 63  ments where bloc
35a4a 6b 69 64 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 42  kid = ?",.  /* B
35a4b 4c 4f 43 4b 5f 44 45 4c 45 54 45 20 2a 2f 20 22  LOCK_DELETE */ "
35a4c 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65  delete from %_se
35a4d 67 6d 65 6e 74 73 20 77 68 65 72 65 20 62 6c 6f  gments where blo
35a4e 63 6b 69 64 20 62 65 74 77 65 65 6e 20 3f 20 61  ckid between ? a
35a4f 6e 64 20 3f 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43  nd ?",.  /* BLOC
35a50 4b 5f 44 45 4c 45 54 45 5f 41 4c 4c 20 2a 2f 20  K_DELETE_ALL */ 
35a51 22 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73  "delete from %_s
35a52 65 67 6d 65 6e 74 73 22 2c 0a 0a 20 20 2f 2a 20  egments",..  /* 
35a53 53 45 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58  SEGDIR_MAX_INDEX
35a54 20 2a 2f 20 22 73 65 6c 65 63 74 20 6d 61 78 28   */ "select max(
35a55 69 64 78 29 20 66 72 6f 6d 20 25 5f 73 65 67 64  idx) from %_segd
35a56 69 72 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d  ir where level =
35a57 20 3f 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52   ?",.  /* SEGDIR
35a58 5f 53 45 54 20 2a 2f 20 22 69 6e 73 65 72 74 20  _SET */ "insert 
35a59 69 6e 74 6f 20 25 5f 73 65 67 64 69 72 20 76 61  into %_segdir va
35a5a 6c 75 65 73 20 28 3f 2c 20 3f 2c 20 3f 2c 20 3f  lues (?, ?, ?, ?
35a5b 2c 20 3f 2c 20 3f 29 22 2c 0a 20 20 2f 2a 20 53  , ?, ?)",.  /* S
35a5c 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 4c 45 56  EGDIR_SELECT_LEV
35a5d 45 4c 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20  EL */.  "select 
35a5e 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61  start_block, lea
35a5f 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72  ves_end_block, r
35a60 6f 6f 74 20 66 72 6f 6d 20 25 5f 73 65 67 64 69  oot from %_segdi
35a61 72 20 22 0a 20 20 22 20 77 68 65 72 65 20 6c 65  r ".  " where le
35a62 76 65 6c 20 3d 20 3f 20 6f 72 64 65 72 20 62 79  vel = ? order by
35a63 20 69 64 78 22 2c 0a 20 20 2f 2a 20 53 45 47 44   idx",.  /* SEGD
35a64 49 52 5f 53 50 41 4e 20 2a 2f 0a 20 20 22 73 65  IR_SPAN */.  "se
35a65 6c 65 63 74 20 6d 69 6e 28 73 74 61 72 74 5f 62  lect min(start_b
35a66 6c 6f 63 6b 29 2c 20 6d 61 78 28 65 6e 64 5f 62  lock), max(end_b
35a67 6c 6f 63 6b 29 20 66 72 6f 6d 20 25 5f 73 65 67  lock) from %_seg
35a68 64 69 72 20 22 0a 20 20 22 20 77 68 65 72 65 20  dir ".  " where 
35a69 6c 65 76 65 6c 20 3d 20 3f 20 61 6e 64 20 73 74  level = ? and st
35a6a 61 72 74 5f 62 6c 6f 63 6b 20 3c 3e 20 30 22 2c  art_block <> 0",
35a6b 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 44 45 4c  .  /* SEGDIR_DEL
35a6c 45 54 45 20 2a 2f 20 22 64 65 6c 65 74 65 20 66  ETE */ "delete f
35a6d 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 77 68 65  rom %_segdir whe
35a6e 72 65 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a 0a  re level = ?",..
35a6f 20 20 2f 2a 20 4e 4f 54 45 28 73 68 65 73 73 29    /* NOTE(shess)
35a70 3a 20 54 68 65 20 66 69 72 73 74 20 74 68 72 65  : The first thre
35a71 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65  e results of the
35a72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 0a 20   following two. 
35a73 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 6d   ** statements m
35a74 75 73 74 20 6d 61 74 63 68 2e 0a 20 20 2a 2f 0a  ust match..  */.
35a75 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 45 4c 45    /* SEGDIR_SELE
35a76 43 54 5f 53 45 47 4d 45 4e 54 20 2a 2f 0a 20 20  CT_SEGMENT */.  
35a77 22 73 65 6c 65 63 74 20 73 74 61 72 74 5f 62 6c  "select start_bl
35a78 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f  ock, leaves_end_
35a79 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d  block, root from
35a7a 20 25 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20   %_segdir ".  " 
35a7b 77 68 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 20  where level = ? 
35a7c 61 6e 64 20 69 64 78 20 3d 20 3f 22 2c 0a 20 20  and idx = ?",.  
35a7d 2f 2a 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54  /* SEGDIR_SELECT
35a7e 5f 41 4c 4c 20 2a 2f 0a 20 20 22 73 65 6c 65 63  _ALL */.  "selec
35a7f 74 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c  t start_block, l
35a80 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c  eaves_end_block,
35a81 20 72 6f 6f 74 20 66 72 6f 6d 20 25 5f 73 65 67   root from %_seg
35a82 64 69 72 20 22 0a 20 20 22 20 6f 72 64 65 72 20  dir ".  " order 
35a83 62 79 20 6c 65 76 65 6c 20 64 65 73 63 2c 20 69  by level desc, i
35a84 64 78 20 61 73 63 22 2c 0a 20 20 2f 2a 20 53 45  dx asc",.  /* SE
35a85 47 44 49 52 5f 44 45 4c 45 54 45 5f 41 4c 4c 20  GDIR_DELETE_ALL 
35a86 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20  */ "delete from 
35a87 25 5f 73 65 67 64 69 72 22 2c 0a 20 20 2f 2a 20  %_segdir",.  /* 
35a88 53 45 47 44 49 52 5f 43 4f 55 4e 54 20 2a 2f 20  SEGDIR_COUNT */ 
35a89 22 73 65 6c 65 63 74 20 63 6f 75 6e 74 28 2a 29  "select count(*)
35a8a 2c 20 69 66 6e 75 6c 6c 28 6d 61 78 28 6c 65 76  , ifnull(max(lev
35a8b 65 6c 29 2c 30 29 20 66 72 6f 6d 20 25 5f 73 65  el),0) from %_se
35a8c 67 64 69 72 22 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  gdir",.};../*.**
35a8d 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f   A connection to
35a8e 20 61 20 66 75 6c 6c 74 65 78 74 20 69 6e 64 65   a fulltext inde
35a8f 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  x is an instance
35a90 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
35a91 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20  g.** structure. 
35a92 20 54 68 65 20 78 43 72 65 61 74 65 20 61 6e 64   The xCreate and
35a93 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64   xConnect method
35a94 73 20 63 72 65 61 74 65 20 61 6e 20 69 6e 73 74  s create an inst
35a95 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ance.** of this 
35a96 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 78 44  structure and xD
35a97 65 73 74 72 6f 79 20 61 6e 64 20 78 44 69 73 63  estroy and xDisc
35a98 6f 6e 6e 65 63 74 20 66 72 65 65 20 74 68 61 74  onnect free that
35a99 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2a 20 41 6c   instance..** Al
35a9a 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 20  l other methods 
35a9b 72 65 63 65 69 76 65 20 61 20 70 6f 69 6e 74 65  receive a pointe
35a9c 72 20 74 6f 20 74 68 65 20 73 74 72 75 63 74 75  r to the structu
35a9d 72 65 20 61 73 20 6f 6e 65 20 6f 66 20 74 68 65  re as one of the
35a9e 69 72 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e  ir.** arguments.
35a9f 0a 2a 2f 0a 73 74 72 75 63 74 20 66 75 6c 6c 74  .*/.struct fullt
35aa0 65 78 74 5f 76 74 61 62 20 7b 0a 20 20 73 71 6c  ext_vtab {.  sql
35aa1 69 74 65 33 5f 76 74 61 62 20 62 61 73 65 3b 20  ite3_vtab base; 
35aa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35aa3 20 42 61 73 65 20 63 6c 61 73 73 20 75 73 65 64   Base class used
35aa4 20 62 79 20 53 51 4c 69 74 65 20 63 6f 72 65 20   by SQLite core 
35aa5 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
35aa6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35aa7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
35aa8 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
35aa9 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
35aaa 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20   *zDb;          
35aab 20 20 20 20 20 20 20 2f 2a 20 6c 6f 67 69 63 61         /* logica
35aac 6c 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  l database name 
35aad 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
35aae 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
35aaf 20 20 20 20 20 20 2f 2a 20 76 69 72 74 75 61 6c        /* virtual
35ab0 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   table name */. 
35ab1 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20   int nColumn;   
35ab2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ab3 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63    /* number of c
35ab4 6f 6c 75 6d 6e 73 20 69 6e 20 76 69 72 74 75 61  olumns in virtua
35ab5 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61  l table */.  cha
35ab6 72 20 2a 2a 61 7a 43 6f 6c 75 6d 6e 3b 20 20 20  r **azColumn;   
35ab7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35ab8 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20   column names.  
35ab9 6d 61 6c 6c 6f 63 65 64 20 2a 2f 0a 20 20 63 68  malloced */.  ch
35aba 61 72 20 2a 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f  ar **azContentCo
35abb 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  lumn;          /
35abc 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69  * column names i
35abd 6e 20 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b  n content table;
35abe 20 6d 61 6c 6c 6f 63 65 64 20 2a 2f 0a 20 20 73   malloced */.  s
35abf 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
35ac0 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20   *pTokenizer;   
35ac1 2f 2a 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72  /* tokenizer for
35ac2 20 69 6e 73 65 72 74 73 20 61 6e 64 20 71 75 65   inserts and que
35ac3 72 69 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 50 72  ries */..  /* Pr
35ac4 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d  ecompiled statem
35ac5 65 6e 74 73 20 77 68 69 63 68 20 77 65 20 6b 65  ents which we ke
35ac6 65 70 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68  ep as long as th
35ac7 65 20 74 61 62 6c 65 20 69 73 0a 20 20 2a 2a 20  e table is.  ** 
35ac8 6f 70 65 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  open..  */.  sql
35ac9 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 75 6c 6c  ite3_stmt *pFull
35aca 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 4d  textStatements[M
35acb 41 58 5f 53 54 4d 54 5d 3b 0a 0a 20 20 2f 2a 20  AX_STMT];..  /* 
35acc 50 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  Precompiled stat
35acd 65 6d 65 6e 74 73 20 75 73 65 64 20 66 6f 72 20  ements used for 
35ace 73 65 67 6d 65 6e 74 20 6d 65 72 67 65 73 2e 20  segment merges. 
35acf 20 57 65 20 72 75 6e 20 61 0a 20 20 2a 2a 20 73   We run a.  ** s
35ad0 65 70 61 72 61 74 65 20 73 65 6c 65 63 74 20 61  eparate select a
35ad1 63 72 6f 73 73 20 74 68 65 20 6c 65 61 66 20 6c  cross the leaf l
35ad2 65 76 65 6c 20 6f 66 20 65 61 63 68 20 74 72 65  evel of each tre
35ad3 65 20 62 65 69 6e 67 20 6d 65 72 67 65 64 2e 0a  e being merged..
35ad4 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73    */.  sqlite3_s
35ad5 74 6d 74 20 2a 70 4c 65 61 66 53 65 6c 65 63 74  tmt *pLeafSelect
35ad6 53 74 6d 74 73 5b 4d 45 52 47 45 5f 43 4f 55 4e  Stmts[MERGE_COUN
35ad7 54 5d 3b 0a 20 20 2f 2a 20 54 68 65 20 73 74 61  T];.  /* The sta
35ad8 74 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20 70  tement used to p
35ad9 72 65 70 61 72 65 20 70 4c 65 61 66 53 65 6c 65  repare pLeafSele
35ada 63 74 53 74 6d 74 73 2e 20 2a 2f 0a 23 64 65 66  ctStmts. */.#def
35adb 69 6e 65 20 4c 45 41 46 5f 53 45 4c 45 43 54 20  ine LEAF_SELECT 
35adc 5c 0a 20 20 22 73 65 6c 65 63 74 20 62 6c 6f 63  \.  "select bloc
35add 6b 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74  k from %_segment
35ade 73 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20  s where blockid 
35adf 62 65 74 77 65 65 6e 20 3f 20 61 6e 64 20 3f 20  between ? and ? 
35ae0 6f 72 64 65 72 20 62 79 20 62 6c 6f 63 6b 69 64  order by blockid
35ae1 22 0a 0a 20 20 2f 2a 20 54 68 65 73 65 20 62 75  "..  /* These bu
35ae2 66 66 65 72 20 70 65 6e 64 69 6e 67 20 69 6e 64  ffer pending ind
35ae3 65 78 20 75 70 64 61 74 65 73 20 64 75 72 69 6e  ex updates durin
35ae4 67 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a  g transactions..
35ae5 20 20 2a 2a 20 6e 50 65 6e 64 69 6e 67 44 61 74    ** nPendingDat
35ae6 61 20 65 73 74 69 6d 61 74 65 73 20 74 68 65 20  a estimates the 
35ae7 6d 65 6d 6f 72 79 20 73 69 7a 65 20 6f 66 20 74  memory size of t
35ae8 68 65 20 70 65 6e 64 69 6e 67 20 64 61 74 61 2e  he pending data.
35ae9 20 20 49 74 0a 20 20 2a 2a 20 64 6f 65 73 6e 27    It.  ** doesn'
35aea 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 68 61  t include the ha
35aeb 73 68 2d 62 75 63 6b 65 74 20 6f 76 65 72 68 65  sh-bucket overhe
35aec 61 64 2c 20 6e 6f 72 20 61 6e 79 20 6d 61 6c 6c  ad, nor any mall
35aed 6f 63 0a 20 20 2a 2a 20 6f 76 65 72 68 65 61 64  oc.  ** overhead
35aee 2e 20 20 57 68 65 6e 20 6e 50 65 6e 64 69 6e 67  .  When nPending
35aef 44 61 74 61 20 65 78 63 65 65 64 73 20 6b 50 65  Data exceeds kPe
35af0 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c 64 2c 20  ndingThreshold, 
35af1 74 68 65 0a 20 20 2a 2a 20 62 75 66 66 65 72 20  the.  ** buffer 
35af2 69 73 20 66 6c 75 73 68 65 64 20 65 76 65 6e 20  is flushed even 
35af3 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73  before the trans
35af4 61 63 74 69 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20  action closes.. 
35af5 20 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   ** pendingTerms
35af6 20 73 74 6f 72 65 73 20 74 68 65 20 64 61 74 61   stores the data
35af7 2c 20 61 6e 64 20 69 73 20 6f 6e 6c 79 20 76 61  , and is only va
35af8 6c 69 64 20 77 68 65 6e 20 6e 50 65 6e 64 69 6e  lid when nPendin
35af9 67 44 61 74 61 0a 20 20 2a 2a 20 69 73 20 3e 3d  gData.  ** is >=
35afa 30 20 28 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c  0 (nPendingData<
35afb 30 20 6d 65 61 6e 73 20 70 65 6e 64 69 6e 67 54  0 means pendingT
35afc 65 72 6d 73 20 68 61 73 20 6e 6f 74 20 62 65 65  erms has not bee
35afd 6e 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a  n.  ** initializ
35afe 65 64 29 2e 20 20 69 50 72 65 76 44 6f 63 69 64  ed).  iPrevDocid
35aff 20 69 73 20 74 68 65 20 6c 61 73 74 20 64 6f 63   is the last doc
35b00 69 64 20 77 72 69 74 74 65 6e 2c 20 75 73 65 64  id written, used
35b01 20 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 63 65   to make.  ** ce
35b02 72 74 61 69 6e 20 77 65 27 72 65 20 69 6e 73 65  rtain we're inse
35b03 72 74 69 6e 67 20 69 6e 20 73 6f 72 74 65 64 20  rting in sorted 
35b04 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 6e  order..  */.  in
35b05 74 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 3b 0a  t nPendingData;.
35b06 23 64 65 66 69 6e 65 20 6b 50 65 6e 64 69 6e 67  #define kPending
35b07 54 68 72 65 73 68 6f 6c 64 20 28 31 2a 31 30 32  Threshold (1*102
35b08 34 2a 31 30 32 34 29 0a 20 20 73 71 6c 69 74 65  4*1024).  sqlite
35b09 5f 69 6e 74 36 34 20 69 50 72 65 76 44 6f 63 69  _int64 iPrevDoci
35b0a 64 3b 0a 20 20 66 74 73 33 48 61 73 68 20 70 65  d;.  fts3Hash pe
35b0b 6e 64 69 6e 67 54 65 72 6d 73 3b 0a 7d 3b 0a 0a  ndingTerms;.};..
35b0c 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63  /*.** When the c
35b0d 6f 72 65 20 77 61 6e 74 73 20 74 6f 20 64 6f 20  ore wants to do 
35b0e 61 20 71 75 65 72 79 2c 20 69 74 20 63 72 65 61  a query, it crea
35b0f 74 65 20 61 20 63 75 72 73 6f 72 20 75 73 69 6e  te a cursor usin
35b10 67 20 61 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78  g a.** call to x
35b11 4f 70 65 6e 2e 20 20 54 68 69 73 20 73 74 72 75  Open.  This stru
35b12 63 74 75 72 65 20 69 73 20 61 6e 20 69 6e 73 74  cture is an inst
35b13 61 6e 63 65 20 6f 66 20 61 20 63 75 72 73 6f 72  ance of a cursor
35b14 2e 20 20 49 74 0a 2a 2a 20 69 73 20 64 65 73 74  .  It.** is dest
35b15 72 6f 79 65 64 20 62 79 20 78 43 6c 6f 73 65 2e  royed by xClose.
35b16 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
35b17 63 74 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  ct fulltext_curs
35b18 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  or {.  sqlite3_v
35b19 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  tab_cursor base;
35b1a 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20          /* Base 
35b1b 63 6c 61 73 73 20 75 73 65 64 20 62 79 20 53 51  class used by SQ
35b1c 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 20 20 51  Lite core */.  Q
35b1d 75 65 72 79 54 79 70 65 20 69 43 75 72 73 6f 72  ueryType iCursor
35b1e 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  Type;           
35b1f 2f 2a 20 43 6f 70 79 20 6f 66 20 73 71 6c 69 74  /* Copy of sqlit
35b20 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2e 69 64  e3_index_info.id
35b21 78 4e 75 6d 20 2a 2f 0a 20 20 73 71 6c 69 74 65  xNum */.  sqlite
35b22 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20  3_stmt *pStmt;  
35b23 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72             /* Pr
35b24 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
35b25 20 69 6e 20 75 73 65 20 62 79 20 74 68 65 20 63   in use by the c
35b26 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 65  ursor */.  int e
35b27 6f 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  of;             
35b28 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
35b29 72 75 65 20 69 66 20 61 74 20 45 6e 64 20 4f 66  rue if at End Of
35b2a 20 52 65 73 75 6c 74 73 20 2a 2f 0a 20 20 46 74   Results */.  Ft
35b2b 73 33 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20  s3Expr *pExpr;  
35b2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35b2d 2a 20 50 61 72 73 65 64 20 4d 41 54 43 48 20 71  * Parsed MATCH q
35b2e 75 65 72 79 20 73 74 72 69 6e 67 20 2a 2f 0a 20  uery string */. 
35b2f 20 53 6e 69 70 70 65 74 20 73 6e 69 70 70 65 74   Snippet snippet
35b30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35b31 20 20 2f 2a 20 43 61 63 68 65 64 20 73 6e 69 70    /* Cached snip
35b32 70 65 74 20 66 6f 72 20 74 68 65 20 63 75 72 72  pet for the curr
35b33 65 6e 74 20 72 6f 77 20 2a 2f 0a 20 20 69 6e 74  ent row */.  int
35b34 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   iColumn;       
35b35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35b36 20 43 6f 6c 75 6d 6e 20 62 65 69 6e 67 20 73 65   Column being se
35b37 61 72 63 68 65 64 20 2a 2f 0a 20 20 44 61 74 61  arched */.  Data
35b38 42 75 66 66 65 72 20 72 65 73 75 6c 74 3b 20 20  Buffer result;  
35b39 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35b3a 44 6f 63 6c 69 73 74 20 72 65 73 75 6c 74 73 20  Doclist results 
35b3b 66 72 6f 6d 20 66 75 6c 6c 74 65 78 74 51 75 65  from fulltextQue
35b3c 72 79 20 2a 2f 0a 20 20 44 4c 52 65 61 64 65 72  ry */.  DLReader
35b3d 20 72 65 61 64 65 72 3b 20 20 20 20 20 20 20 20   reader;        
35b3e 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
35b3f 6c 74 20 72 65 61 64 65 72 20 69 66 20 72 65 73  lt reader if res
35b40 75 6c 74 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f  ult not empty */
35b41 0a 7d 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .} fulltext_curs
35b42 6f 72 3b 0a 0a 73 74 61 74 69 63 20 66 75 6c 6c  or;..static full
35b43 74 65 78 74 5f 76 74 61 62 20 2a 63 75 72 73 6f  text_vtab *curso
35b44 72 5f 76 74 61 62 28 66 75 6c 6c 74 65 78 74 5f  r_vtab(fulltext_
35b45 63 75 72 73 6f 72 20 2a 63 29 7b 0a 20 20 72 65  cursor *c){.  re
35b46 74 75 72 6e 20 28 66 75 6c 6c 74 65 78 74 5f 76  turn (fulltext_v
35b47 74 61 62 20 2a 29 20 63 2d 3e 62 61 73 65 2e 70  tab *) c->base.p
35b48 56 74 61 62 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  Vtab;.}..static 
35b49 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
35b4a 64 75 6c 65 20 66 74 73 33 4d 6f 64 75 6c 65 3b  dule fts3Module;
35b4b 20 20 20 2f 2a 20 66 6f 72 77 61 72 64 20 64 65     /* forward de
35b4c 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a  claration */../*
35b4d 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d 69   Return a dynami
35b4e 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20  cally generated 
35b4f 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68 65  statement of the
35b50 20 66 6f 72 6d 0a 20 2a 20 20 20 69 6e 73 65 72   form. *   inser
35b51 74 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65 6e 74  t into %_content
35b52 20 28 64 6f 63 69 64 2c 20 2e 2e 2e 29 20 76 61   (docid, ...) va
35b53 6c 75 65 73 20 28 3f 2c 20 2e 2e 2e 29 0a 20 2a  lues (?, ...). *
35b54 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
35b55 68 61 72 20 2a 63 6f 6e 74 65 6e 74 49 6e 73 65  har *contentInse
35b56 72 74 53 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c  rtStatement(full
35b57 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20  text_vtab *v){. 
35b58 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62   StringBuffer sb
35b59 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69 6e  ;.  int i;..  in
35b5a 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26  itStringBuffer(&
35b5b 73 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73  sb);.  append(&s
35b5c 62 2c 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20  b, "insert into 
35b5d 25 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64  %_content (docid
35b5e 2c 20 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69  , ");.  appendLi
35b5f 73 74 28 26 73 62 2c 20 76 2d 3e 6e 43 6f 6c 75  st(&sb, v->nColu
35b60 6d 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74  mn, v->azContent
35b61 43 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70 65 6e  Column);.  appen
35b62 64 28 26 73 62 2c 20 22 29 20 76 61 6c 75 65 73  d(&sb, ") values
35b63 20 28 3f 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30   (?");.  for(i=0
35b64 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  ; i<v->nColumn; 
35b65 2b 2b 69 29 0a 20 20 20 20 61 70 70 65 6e 64 28  ++i).    append(
35b66 26 73 62 2c 20 22 2c 20 3f 22 29 3b 0a 20 20 61  &sb, ", ?");.  a
35b67 70 70 65 6e 64 28 26 73 62 2c 20 22 29 22 29 3b  ppend(&sb, ")");
35b68 0a 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67  .  return string
35b69 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b  BufferData(&sb);
35b6a 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20  .}../* Return a 
35b6b 64 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65  dynamically gene
35b6c 72 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20  rated statement 
35b6d 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20  of the form. *  
35b6e 20 73 65 6c 65 63 74 20 3c 63 6f 6e 74 65 6e 74   select <content
35b6f 20 63 6f 6c 75 6d 6e 73 3e 20 66 72 6f 6d 20 25   columns> from %
35b70 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20 64  _content where d
35b71 6f 63 69 64 20 3d 20 3f 0a 20 2a 2f 0a 73 74 61  ocid = ?. */.sta
35b72 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
35b73 63 6f 6e 74 65 6e 74 53 65 6c 65 63 74 53 74 61  contentSelectSta
35b74 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  tement(fulltext_
35b75 76 74 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69  vtab *v){.  Stri
35b76 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69  ngBuffer sb;.  i
35b77 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28  nitStringBuffer(
35b78 26 73 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26  &sb);.  append(&
35b79 73 62 2c 20 22 53 45 4c 45 43 54 20 22 29 3b 0a  sb, "SELECT ");.
35b7a 20 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73 62    appendList(&sb
35b7b 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d  , v->nColumn, v-
35b7c 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  >azContentColumn
35b7d 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c  );.  append(&sb,
35b7e 20 22 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e   " FROM %_conten
35b7f 74 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d 20  t WHERE docid = 
35b80 3f 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 74  ?");.  return st
35b81 72 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 26  ringBufferData(&
35b82 73 62 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72  sb);.}../* Retur
35b83 6e 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20  n a dynamically 
35b84 67 65 6e 65 72 61 74 65 64 20 73 74 61 74 65 6d  generated statem
35b85 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a  ent of the form.
35b86 20 2a 20 20 20 75 70 64 61 74 65 20 25 5f 63 6f   *   update %_co
35b87 6e 74 65 6e 74 20 73 65 74 20 5b 63 6f 6c 5f 30  ntent set [col_0
35b88 5d 20 3d 20 3f 2c 20 5b 63 6f 6c 5f 31 5d 20 3d  ] = ?, [col_1] =
35b89 20 3f 2c 20 2e 2e 2e 0a 20 2a 20 20 20 20 20 20   ?, .... *      
35b8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68                wh
35b8b 65 72 65 20 64 6f 63 69 64 20 3d 20 3f 0a 20 2a  ere docid = ?. *
35b8c 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
35b8d 68 61 72 20 2a 63 6f 6e 74 65 6e 74 55 70 64 61  har *contentUpda
35b8e 74 65 53 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c  teStatement(full
35b8f 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20  text_vtab *v){. 
35b90 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62   StringBuffer sb
35b91 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69 6e  ;.  int i;..  in
35b92 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26  itStringBuffer(&
35b93 73 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73  sb);.  append(&s
35b94 62 2c 20 22 75 70 64 61 74 65 20 25 5f 63 6f 6e  b, "update %_con
35b95 74 65 6e 74 20 73 65 74 20 22 29 3b 0a 20 20 66  tent set ");.  f
35b96 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f  or(i=0; i<v->nCo
35b97 6c 75 6d 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20 20  lumn; ++i) {.   
35b98 20 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20   if( i>0 ){.    
35b99 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 2c    append(&sb, ",
35b9a 20 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61   ");.    }.    a
35b9b 70 70 65 6e 64 28 26 73 62 2c 20 76 2d 3e 61 7a  ppend(&sb, v->az
35b9c 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d  ContentColumn[i]
35b9d 29 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73  );.    append(&s
35b9e 62 2c 20 22 20 3d 20 3f 22 29 3b 0a 20 20 7d 0a  b, " = ?");.  }.
35b9f 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20    append(&sb, " 
35ba0 77 68 65 72 65 20 64 6f 63 69 64 20 3d 20 3f 22  where docid = ?"
35ba1 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72 69  );.  return stri
35ba2 6e 67 42 75 66 66 65 72 44 61 74 61 28 26 73 62  ngBufferData(&sb
35ba3 29 3b 0a 7d 0a 0a 2f 2a 20 50 75 74 73 20 61 20  );.}../* Puts a 
35ba4 66 72 65 73 68 6c 79 2d 70 72 65 70 61 72 65 64  freshly-prepared
35ba5 20 73 74 61 74 65 6d 65 6e 74 20 64 65 74 65 72   statement deter
35ba6 6d 69 6e 65 64 20 62 79 20 69 53 74 6d 74 20 69  mined by iStmt i
35ba7 6e 20 2a 70 70 53 74 6d 74 2e 0a 2a 2a 20 49 66  n *ppStmt..** If
35ba8 20 74 68 65 20 69 6e 64 69 63 61 74 65 64 20 73   the indicated s
35ba9 74 61 74 65 6d 65 6e 74 20 68 61 73 20 6e 65 76  tatement has nev
35baa 65 72 20 62 65 65 6e 20 70 72 65 70 61 72 65 64  er been prepared
35bab 2c 20 69 74 20 69 73 20 70 72 65 70 61 72 65 64  , it is prepared
35bac 0a 2a 2a 20 61 6e 64 20 63 61 63 68 65 64 2c 20  .** and cached, 
35bad 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 63 61  otherwise the ca
35bae 63 68 65 64 20 76 65 72 73 69 6f 6e 20 69 73 20  ched version is 
35baf 72 65 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  reset..*/.static
35bb0 20 69 6e 74 20 73 71 6c 5f 67 65 74 5f 73 74 61   int sql_get_sta
35bb1 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  tement(fulltext_
35bb2 76 74 61 62 20 2a 76 2c 20 66 75 6c 6c 74 65 78  vtab *v, fulltex
35bb3 74 5f 73 74 61 74 65 6d 65 6e 74 20 69 53 74 6d  t_statement iStm
35bb4 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
35bb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bb6 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70  sqlite3_stmt **p
35bb7 70 53 74 6d 74 29 7b 0a 20 20 61 73 73 65 72 74  pStmt){.  assert
35bb8 28 20 69 53 74 6d 74 3c 4d 41 58 5f 53 54 4d 54  ( iStmt<MAX_STMT
35bb9 20 29 3b 0a 20 20 69 66 28 20 76 2d 3e 70 46 75   );.  if( v->pFu
35bba 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73  lltextStatements
35bbb 5b 69 53 74 6d 74 5d 3d 3d 4e 55 4c 4c 20 29 7b  [iStmt]==NULL ){
35bbc 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
35bbd 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 69 6e 74 20  *zStmt;.    int 
35bbe 72 63 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20  rc;.    switch( 
35bbf 69 53 74 6d 74 20 29 7b 0a 20 20 20 20 20 20 63  iStmt ){.      c
35bc0 61 73 65 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45  ase CONTENT_INSE
35bc1 52 54 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20 20  RT_STMT:.       
35bc2 20 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74   zStmt = content
35bc3 49 6e 73 65 72 74 53 74 61 74 65 6d 65 6e 74 28  InsertStatement(
35bc4 76 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  v); break;.     
35bc5 20 63 61 73 65 20 43 4f 4e 54 45 4e 54 5f 53 45   case CONTENT_SE
35bc6 4c 45 43 54 5f 53 54 4d 54 3a 0a 20 20 20 20 20  LECT_STMT:.     
35bc7 20 20 20 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65     zStmt = conte
35bc8 6e 74 53 65 6c 65 63 74 53 74 61 74 65 6d 65 6e  ntSelectStatemen
35bc9 74 28 76 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20  t(v); break;.   
35bca 20 20 20 63 61 73 65 20 43 4f 4e 54 45 4e 54 5f     case CONTENT_
35bcb 55 50 44 41 54 45 5f 53 54 4d 54 3a 0a 20 20 20  UPDATE_STMT:.   
35bcc 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 6f 6e       zStmt = con
35bcd 74 65 6e 74 55 70 64 61 74 65 53 74 61 74 65 6d  tentUpdateStatem
35bce 65 6e 74 28 76 29 3b 20 62 72 65 61 6b 3b 0a 20  ent(v); break;. 
35bcf 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20       default:.  
35bd0 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 66 75        zStmt = fu
35bd1 6c 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e  lltext_zStatemen
35bd2 74 5b 69 53 74 6d 74 5d 3b 0a 20 20 20 20 7d 0a  t[iStmt];.    }.
35bd3 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 70 72 65      rc = sql_pre
35bd4 70 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a  pare(v->db, v->z
35bd5 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20 26 76  Db, v->zName, &v
35bd6 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65  ->pFulltextState
35bd7 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 2c 0a 20 20  ments[iStmt],.  
35bd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bd9 20 20 20 20 20 20 20 7a 53 74 6d 74 29 3b 0a 20         zStmt);. 
35bda 20 20 20 69 66 28 20 7a 53 74 6d 74 20 21 3d 20     if( zStmt != 
35bdb 66 75 6c 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d  fulltext_zStatem
35bdc 65 6e 74 5b 69 53 74 6d 74 5d 29 20 73 71 6c 69  ent[iStmt]) sqli
35bdd 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a  te3_free((void *
35bde 29 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 69 66  ) zStmt);.    if
35bdf 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35be0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
35be1 20 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20   else {.    int 
35be2 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
35be3 65 74 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53  et(v->pFulltextS
35be4 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d  tatements[iStmt]
35be5 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
35be6 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35be7 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70  n rc;.  }..  *pp
35be8 53 74 6d 74 20 3d 20 76 2d 3e 70 46 75 6c 6c 74  Stmt = v->pFullt
35be9 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53  extStatements[iS
35bea 74 6d 74 5d 3b 0a 20 20 72 65 74 75 72 6e 20 53  tmt];.  return S
35beb 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
35bec 4c 69 6b 65 20 73 71 6c 69 74 65 33 5f 73 74 65  Like sqlite3_ste
35bed 70 28 29 2c 20 62 75 74 20 63 6f 6e 76 65 72 74  p(), but convert
35bee 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 74 6f 20   SQLITE_DONE to 
35bef 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 0a 2a 2a  SQLITE_OK and.**
35bf0 20 53 51 4c 49 54 45 5f 52 4f 57 20 74 6f 20 53   SQLITE_ROW to S
35bf1 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 55 73  QLITE_ERROR.  Us
35bf2 65 66 75 6c 20 66 6f 72 20 73 74 61 74 65 6d 65  eful for stateme
35bf3 6e 74 73 20 6c 69 6b 65 20 55 50 44 41 54 45 2c  nts like UPDATE,
35bf4 0a 2a 2a 20 77 68 65 72 65 20 77 65 20 65 78 70  .** where we exp
35bf5 65 63 74 20 6e 6f 20 72 65 73 75 6c 74 73 2e 0a  ect no results..
35bf6 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
35bf7 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 71  l_single_step(sq
35bf8 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 29 7b 0a  lite3_stmt *s){.
35bf9 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
35bfa 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 72 65  e3_step(s);.  re
35bfb 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45  turn (rc==SQLITE
35bfc 5f 44 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f  _DONE) ? SQLITE_
35bfd 4f 4b 20 3a 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4c  OK : rc;.}../* L
35bfe 69 6b 65 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  ike sql_get_stat
35bff 65 6d 65 6e 74 28 29 2c 20 62 75 74 20 66 6f 72  ement(), but for
35c00 20 73 70 65 63 69 61 6c 20 72 65 70 6c 69 63 61   special replica
35c01 74 65 64 20 4c 45 41 46 5f 53 45 4c 45 43 54 0a  ted LEAF_SELECT.
35c02 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  ** statements.  
35c03 69 64 78 20 2d 31 20 69 73 20 61 20 73 70 65 63  idx -1 is a spec
35c04 69 61 6c 20 63 61 73 65 20 66 6f 72 20 61 6e 20  ial case for an 
35c05 75 6e 63 61 63 68 65 64 20 76 65 72 73 69 6f 6e  uncached version
35c06 20 6f 66 0a 2a 2a 20 74 68 65 20 73 74 61 74 65   of.** the state
35c07 6d 65 6e 74 20 28 75 73 65 64 20 69 6e 20 74 68  ment (used in th
35c08 65 20 6f 70 74 69 6d 69 7a 65 20 69 6d 70 6c 65  e optimize imple
35c09 6d 65 6e 74 61 74 69 6f 6e 29 2e 0a 2a 2f 0a 2f  mentation)..*/./
35c0a 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57 72  * TODO(shess) Wr
35c0b 69 74 65 20 76 65 72 73 69 6f 6e 20 66 6f 72 20  ite version for 
35c0c 67 65 6e 65 72 69 63 20 73 74 61 74 65 6d 65 6e  generic statemen
35c0d 74 73 20 61 6e 64 20 74 68 65 6e 20 73 68 61 72  ts and then shar
35c0e 65 0a 2a 2a 20 74 68 61 74 20 62 65 74 77 65 65  e.** that betwee
35c0f 6e 20 74 68 65 20 63 61 63 68 65 64 2d 73 74 61  n the cached-sta
35c10 74 65 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e 73  tement functions
35c11 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35c12 73 71 6c 5f 67 65 74 5f 6c 65 61 66 5f 73 74 61  sql_get_leaf_sta
35c13 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  tement(fulltext_
35c14 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 64 78  vtab *v, int idx
35c15 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35c16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c17 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74      sqlite3_stmt
35c18 20 2a 2a 70 70 53 74 6d 74 29 7b 0a 20 20 61 73   **ppStmt){.  as
35c19 73 65 72 74 28 20 69 64 78 3e 3d 2d 31 20 26 26  sert( idx>=-1 &&
35c1a 20 69 64 78 3c 4d 45 52 47 45 5f 43 4f 55 4e 54   idx<MERGE_COUNT
35c1b 20 29 3b 0a 20 20 69 66 28 20 69 64 78 3d 3d 2d   );.  if( idx==-
35c1c 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  1 ){.    return 
35c1d 73 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64  sql_prepare(v->d
35c1e 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e  b, v->zDb, v->zN
35c1f 61 6d 65 2c 20 70 70 53 74 6d 74 2c 20 4c 45 41  ame, ppStmt, LEA
35c20 46 5f 53 45 4c 45 43 54 29 3b 0a 20 20 7d 65 6c  F_SELECT);.  }el
35c21 73 65 20 69 66 28 20 76 2d 3e 70 4c 65 61 66 53  se if( v->pLeafS
35c22 65 6c 65 63 74 53 74 6d 74 73 5b 69 64 78 5d 3d  electStmts[idx]=
35c23 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 6e 74  =NULL ){.    int
35c24 20 72 63 20 3d 20 73 71 6c 5f 70 72 65 70 61 72   rc = sql_prepar
35c25 65 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c  e(v->db, v->zDb,
35c26 20 76 2d 3e 7a 4e 61 6d 65 2c 20 26 76 2d 3e 70   v->zName, &v->p
35c27 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b  LeafSelectStmts[
35c28 69 64 78 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  idx],.          
35c29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c                 L
35c2a 45 41 46 5f 53 45 4c 45 43 54 29 3b 0a 20 20 20  EAF_SELECT);.   
35c2b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35c2c 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35c2d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
35c2e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65   rc = sqlite3_re
35c2f 73 65 74 28 76 2d 3e 70 4c 65 61 66 53 65 6c 65  set(v->pLeafSele
35c30 63 74 53 74 6d 74 73 5b 69 64 78 5d 29 3b 0a 20  ctStmts[idx]);. 
35c31 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
35c32 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
35c33 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 53 74 6d 74  ;.  }..  *ppStmt
35c34 20 3d 20 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63   = v->pLeafSelec
35c35 74 53 74 6d 74 73 5b 69 64 78 5d 3b 0a 20 20 72  tStmts[idx];.  r
35c36 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
35c37 0a 7d 0a 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e  .}../* insert in
35c38 74 6f 20 25 5f 63 6f 6e 74 65 6e 74 20 28 64 6f  to %_content (do
35c39 63 69 64 2c 20 2e 2e 2e 29 20 76 61 6c 75 65 73  cid, ...) values
35c3a 20 28 5b 64 6f 63 69 64 5d 2c 20 5b 70 56 61 6c   ([docid], [pVal
35c3b 75 65 73 5d 29 0a 2a 2a 20 49 66 20 74 68 65 20  ues]).** If the 
35c3c 64 6f 63 69 64 20 63 6f 6e 74 61 69 6e 73 20 53  docid contains S
35c3d 51 4c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20  QL NULL, then a 
35c3e 75 6e 69 71 75 65 20 64 6f 63 69 64 20 77 69 6c  unique docid wil
35c3f 6c 20 62 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65  l be.** generate
35c40 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
35c41 20 63 6f 6e 74 65 6e 74 5f 69 6e 73 65 72 74 28   content_insert(
35c42 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
35c43 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
35c44 2a 64 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20  *docid,.        
35c45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c46 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
35c47 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 73 71  **pValues){.  sq
35c48 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
35c49 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63   int i;.  int rc
35c4a 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
35c4b 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f  ment(v, CONTENT_
35c4c 49 4e 53 45 52 54 5f 53 54 4d 54 2c 20 26 73 29  INSERT_STMT, &s)
35c4d 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35c4e 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35c4f 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
35c50 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 2c  e3_bind_value(s,
35c51 20 31 2c 20 64 6f 63 69 64 29 3b 0a 20 20 69 66   1, docid);.  if
35c52 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35c53 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
35c54 66 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43  for(i=0; i<v->nC
35c55 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20  olumn; ++i){.   
35c56 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
35c57 6e 64 5f 76 61 6c 75 65 28 73 2c 20 32 2b 69 2c  nd_value(s, 2+i,
35c58 20 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20   pValues[i]);.  
35c59 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35c5a 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35c5b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73  .  }..  return s
35c5c 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73  ql_single_step(s
35c5d 29 3b 0a 7d 0a 0a 2f 2a 20 75 70 64 61 74 65 20  );.}../* update 
35c5e 25 5f 63 6f 6e 74 65 6e 74 20 73 65 74 20 63 6f  %_content set co
35c5f 6c 30 20 3d 20 70 56 61 6c 75 65 73 5b 30 5d 2c  l0 = pValues[0],
35c60 20 63 6f 6c 31 20 3d 20 70 56 61 6c 75 65 73 5b   col1 = pValues[
35c61 31 5d 2c 20 2e 2e 2e 0a 20 2a 20 20 20 20 20 20  1], .... *      
35c62 20 20 20 20 20 20 20 20 20 20 20 20 77 68 65 72              wher
35c63 65 20 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63 69  e docid = [iDoci
35c64 64 5d 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d] */.static int
35c65 20 63 6f 6e 74 65 6e 74 5f 75 70 64 61 74 65 28   content_update(
35c66 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
35c67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
35c68 2a 2a 70 56 61 6c 75 65 73 2c 0a 20 20 20 20 20  **pValues,.     
35c69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c6a 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
35c6b 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c  4 iDocid){.  sql
35c6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20  ite3_stmt *s;.  
35c6d 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20  int i;.  int rc 
35c6e 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
35c6f 65 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 55  ent(v, CONTENT_U
35c70 50 44 41 54 45 5f 53 54 4d 54 2c 20 26 73 29 3b  PDATE_STMT, &s);
35c71 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35c72 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
35c73 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
35c74 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29  v->nColumn; ++i)
35c75 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
35c76 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 2c  e3_bind_value(s,
35c77 20 31 2b 69 2c 20 70 56 61 6c 75 65 73 5b 69 5d   1+i, pValues[i]
35c78 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
35c79 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35c7a 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  n rc;.  }..  rc 
35c7b 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
35c7c 6e 74 36 34 28 73 2c 20 31 2b 76 2d 3e 6e 43 6f  nt64(s, 1+v->nCo
35c7d 6c 75 6d 6e 2c 20 69 44 6f 63 69 64 29 3b 0a 20  lumn, iDocid);. 
35c7e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35c7f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35c80 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69  .  return sql_si
35c81 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a  ngle_step(s);.}.
35c82 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65  .static void fre
35c83 65 53 74 72 69 6e 67 41 72 72 61 79 28 69 6e 74  eStringArray(int
35c84 20 6e 53 74 72 69 6e 67 2c 20 63 6f 6e 73 74 20   nString, const 
35c85 63 68 61 72 20 2a 2a 70 53 74 72 69 6e 67 29 7b  char **pString){
35c86 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72  .  int i;..  for
35c87 20 28 69 3d 30 20 3b 20 69 20 3c 20 6e 53 74 72   (i=0 ; i < nStr
35c88 69 6e 67 20 3b 20 2b 2b 69 29 20 7b 0a 20 20 20  ing ; ++i) {.   
35c89 20 69 66 28 20 70 53 74 72 69 6e 67 5b 69 5d 21   if( pString[i]!
35c8a 3d 4e 55 4c 4c 20 29 20 73 71 6c 69 74 65 33 5f  =NULL ) sqlite3_
35c8b 66 72 65 65 28 28 76 6f 69 64 20 2a 29 20 70 53  free((void *) pS
35c8c 74 72 69 6e 67 5b 69 5d 29 3b 0a 20 20 7d 0a 20  tring[i]);.  }. 
35c8d 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28 76   sqlite3_free((v
35c8e 6f 69 64 20 2a 29 20 70 53 74 72 69 6e 67 29 3b  oid *) pString);
35c8f 0a 7d 0a 0a 2f 2a 20 73 65 6c 65 63 74 20 2a 20  .}../* select * 
35c90 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77  from %_content w
35c91 68 65 72 65 20 64 6f 63 69 64 20 3d 20 5b 69 44  here docid = [iD
35c92 6f 63 69 64 5d 0a 20 2a 20 54 68 65 20 63 61 6c  ocid]. * The cal
35c93 6c 65 72 20 6d 75 73 74 20 64 65 6c 65 74 65 20  ler must delete 
35c94 74 68 65 20 72 65 74 75 72 6e 65 64 20 61 72 72  the returned arr
35c95 61 79 20 61 6e 64 20 61 6c 6c 20 73 74 72 69 6e  ay and all strin
35c96 67 73 20 69 6e 20 69 74 2e 0a 20 2a 20 6e 75 6c  gs in it.. * nul
35c97 6c 20 66 69 65 6c 64 73 20 77 69 6c 6c 20 62 65  l fields will be
35c98 20 4e 55 4c 4c 20 69 6e 20 74 68 65 20 72 65 74   NULL in the ret
35c99 75 72 6e 65 64 20 61 72 72 61 79 2e 0a 20 2a 0a  urned array.. *.
35c9a 20 2a 20 54 4f 44 4f 3a 20 50 65 72 68 61 70 73   * TODO: Perhaps
35c9b 20 77 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72   we should retur
35c9c 6e 20 70 6f 69 6e 74 65 72 2f 6c 65 6e 67 74 68  n pointer/length
35c9d 20 73 74 72 69 6e 67 73 20 68 65 72 65 20 66 6f   strings here fo
35c9e 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 0a 20 2a  r consistency. *
35c9f 20 77 69 74 68 20 6f 74 68 65 72 20 63 6f 64 65   with other code
35ca0 20 77 68 69 63 68 20 75 73 65 73 20 70 6f 69 6e   which uses poin
35ca1 74 65 72 2f 6c 65 6e 67 74 68 2e 20 2a 2f 0a 73  ter/length. */.s
35ca2 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e  tatic int conten
35ca3 74 5f 73 65 6c 65 63 74 28 66 75 6c 6c 74 65 78  t_select(fulltex
35ca4 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
35ca5 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 0a  e_int64 iDocid,.
35ca6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ca7 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
35ca8 63 68 61 72 20 2a 2a 2a 70 56 61 6c 75 65 73 29  char ***pValues)
35ca9 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
35caa 20 2a 73 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61   *s;.  const cha
35cab 72 20 2a 2a 76 61 6c 75 65 73 3b 0a 20 20 69 6e  r **values;.  in
35cac 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  t i;.  int rc;..
35cad 20 20 2a 70 56 61 6c 75 65 73 20 3d 20 4e 55 4c    *pValues = NUL
35cae 4c 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67  L;..  rc = sql_g
35caf 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
35cb0 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 5f 53  CONTENT_SELECT_S
35cb1 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
35cb2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35cb3 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
35cb4 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
35cb5 69 6e 74 36 34 28 73 2c 20 31 2c 20 69 44 6f 63  int64(s, 1, iDoc
35cb6 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
35cb7 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35cb8 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
35cb9 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
35cba 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35cbb 52 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b  ROW ) return rc;
35cbc 0a 0a 20 20 76 61 6c 75 65 73 20 3d 20 28 63 6f  ..  values = (co
35cbd 6e 73 74 20 63 68 61 72 20 2a 2a 29 20 73 71 6c  nst char **) sql
35cbe 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 76 2d 3e 6e  ite3_malloc(v->n
35cbf 43 6f 6c 75 6d 6e 20 2a 20 73 69 7a 65 6f 66 28  Column * sizeof(
35cc0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 29 3b 0a  const char *));.
35cc1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e    for(i=0; i<v->
35cc2 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20  nColumn; ++i){. 
35cc3 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63     if( sqlite3_c
35cc4 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 2c 20 69 29  olumn_type(s, i)
35cc5 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b  ==SQLITE_NULL ){
35cc6 0a 20 20 20 20 20 20 76 61 6c 75 65 73 5b 69 5d  .      values[i]
35cc7 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 65 6c   = NULL;.    }el
35cc8 73 65 7b 0a 20 20 20 20 20 20 76 61 6c 75 65 73  se{.      values
35cc9 5b 69 5d 20 3d 20 73 74 72 69 6e 67 5f 64 75 70  [i] = string_dup
35cca 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f  ((char*)sqlite3_
35ccb 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 2c 20 69  column_text(s, i
35ccc 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ));.    }.  }.. 
35ccd 20 2f 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e   /* We expect on
35cce 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20  ly one row.  We 
35ccf 6d 75 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f  must execute ano
35cd0 74 68 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65  ther sqlite3_ste
35cd1 70 28 29 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70  p().   * to comp
35cd2 6c 65 74 65 20 74 68 65 20 69 74 65 72 61 74 69  lete the iterati
35cd3 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68  on; otherwise th
35cd4 65 20 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d  e table will rem
35cd5 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20  ain locked. */. 
35cd6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
35cd7 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d  ep(s);.  if( rc=
35cd8 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
35cd9 20 20 20 20 2a 70 56 61 6c 75 65 73 20 3d 20 76      *pValues = v
35cda 61 6c 75 65 73 3b 0a 20 20 20 20 72 65 74 75 72  alues;.    retur
35cdb 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
35cdc 0a 0a 20 20 66 72 65 65 53 74 72 69 6e 67 41 72  ..  freeStringAr
35cdd 72 61 79 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20  ray(v->nColumn, 
35cde 76 61 6c 75 65 73 29 3b 0a 20 20 72 65 74 75 72  values);.  retur
35cdf 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 64 65 6c 65  n rc;.}../* dele
35ce0 74 65 20 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e  te from %_conten
35ce1 74 20 77 68 65 72 65 20 64 6f 63 69 64 20 3d 20  t where docid = 
35ce2 5b 69 44 6f 63 69 64 20 5d 20 2a 2f 0a 73 74 61  [iDocid ] */.sta
35ce3 74 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f  tic int content_
35ce4 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f  delete(fulltext_
35ce5 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f  vtab *v, sqlite_
35ce6 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20  int64 iDocid){. 
35ce7 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
35ce8 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
35ce9 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
35cea 2c 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45  , CONTENT_DELETE
35ceb 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
35cec 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35ced 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
35cee 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
35cef 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69 44  d_int64(s, 1, iD
35cf0 6f 63 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ocid);.  if( rc!
35cf1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
35cf2 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72  urn rc;..  retur
35cf3 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65  n sql_single_ste
35cf4 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75  p(s);.}../* Retu
35cf5 72 6e 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 69  rns SQLITE_ROW i
35cf6 66 20 61 6e 79 20 72 6f 77 73 20 65 78 69 73 74  f any rows exist
35cf7 20 69 6e 20 25 5f 63 6f 6e 74 65 6e 74 2c 20 53   in %_content, S
35cf8 51 4c 49 54 45 5f 44 4f 4e 45 20 69 66 0a 2a 2a  QLITE_DONE if.**
35cf9 20 6e 6f 20 72 6f 77 73 20 65 78 69 73 74 2c 20   no rows exist, 
35cfa 61 6e 64 20 61 6e 79 20 65 72 72 6f 72 20 69 6e  and any error in
35cfb 20 63 61 73 65 20 6f 66 20 66 61 69 6c 75 72 65   case of failure
35cfc 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35cfd 63 6f 6e 74 65 6e 74 5f 65 78 69 73 74 73 28 66  content_exists(f
35cfe 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29  ulltext_vtab *v)
35cff 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
35d00 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20   *s;.  int rc = 
35d01 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
35d02 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 45 58 49  t(v, CONTENT_EXI
35d03 53 54 53 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  STS_STMT, &s);. 
35d04 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35d05 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35d06 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
35d07 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72  step(s);.  if( r
35d08 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
35d09 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
35d0a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20   We expect only 
35d0b 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73  one row.  We mus
35d0c 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65  t execute anothe
35d0d 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  r sqlite3_step()
35d0e 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74  .   * to complet
35d0f 65 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b  e the iteration;
35d10 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74   otherwise the t
35d11 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e  able will remain
35d12 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63   locked. */.  rc
35d13 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
35d14 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  s);.  if( rc==SQ
35d15 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75  LITE_DONE ) retu
35d16 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20  rn SQLITE_ROW;. 
35d17 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
35d18 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ROW ) return SQL
35d19 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 72 65 74  ITE_ERROR;.  ret
35d1a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 69 6e  urn rc;.}../* in
35d1b 73 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67 6d  sert into %_segm
35d1c 65 6e 74 73 20 76 61 6c 75 65 73 20 28 5b 70 44  ents values ([pD
35d1d 61 74 61 5d 29 0a 2a 2a 20 20 20 72 65 74 75 72  ata]).**   retur
35d1e 6e 73 20 61 73 73 69 67 6e 65 64 20 62 6c 6f 63  ns assigned bloc
35d1f 6b 69 64 20 69 6e 20 2a 70 69 42 6c 6f 63 6b 69  kid in *piBlocki
35d20 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  d.*/.static int 
35d21 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 66 75 6c  block_insert(ful
35d22 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 63  ltext_vtab *v, c
35d23 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
35d24 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20  , int nData,.   
35d25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d26 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
35d27 34 20 2a 70 69 42 6c 6f 63 6b 69 64 29 7b 0a 20  4 *piBlockid){. 
35d28 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
35d29 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
35d2a 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
35d2b 2c 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 5f 53  , BLOCK_INSERT_S
35d2c 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
35d2d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35d2e 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
35d2f 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
35d30 62 6c 6f 62 28 73 2c 20 31 2c 20 70 44 61 74 61  blob(s, 1, pData
35d31 2c 20 6e 44 61 74 61 2c 20 53 51 4c 49 54 45 5f  , nData, SQLITE_
35d32 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20 72  STATIC);.  if( r
35d33 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
35d34 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
35d35 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
35d36 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
35d37 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e  ITE_ROW ) return
35d38 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
35d39 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35d3a 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63  DONE ) return rc
35d3b 3b 0a 0a 20 20 2f 2a 20 62 6c 6f 63 6b 69 64 20  ;..  /* blockid 
35d3c 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 61 6c 69  column is an ali
35d3d 61 73 20 66 6f 72 20 72 6f 77 69 64 2e 20 2a 2f  as for rowid. */
35d3e 0a 20 20 2a 70 69 42 6c 6f 63 6b 69 64 20 3d 20  .  *piBlockid = 
35d3f 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
35d40 65 72 74 5f 72 6f 77 69 64 28 76 2d 3e 64 62 29  ert_rowid(v->db)
35d41 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
35d42 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 64 65 6c 65  E_OK;.}../* dele
35d43 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e  te from %_segmen
35d44 74 73 0a 2a 2a 20 20 20 77 68 65 72 65 20 62 6c  ts.**   where bl
35d45 6f 63 6b 69 64 20 62 65 74 77 65 65 6e 20 5b 69  ockid between [i
35d46 53 74 61 72 74 42 6c 6f 63 6b 69 64 5d 20 61 6e  StartBlockid] an
35d47 64 20 5b 69 45 6e 64 42 6c 6f 63 6b 69 64 5d 0a  d [iEndBlockid].
35d48 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 73 20 74 68  **.** Deletes th
35d49 65 20 72 61 6e 67 65 20 6f 66 20 62 6c 6f 63 6b  e range of block
35d4a 73 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20 75 73  s, inclusive, us
35d4b 65 64 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65  ed to delete the
35d4c 20 62 6c 6f 63 6b 73 0a 2a 2a 20 77 68 69 63 68   blocks.** which
35d4d 20 66 6f 72 6d 20 61 20 73 65 67 6d 65 6e 74 2e   form a segment.
35d4e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
35d4f 6c 6f 63 6b 5f 64 65 6c 65 74 65 28 66 75 6c 6c  lock_delete(full
35d50 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20  text_vtab *v,.  
35d51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d52 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
35d53 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  64 iStartBlockid
35d54 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
35d55 45 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 73  EndBlockid){.  s
35d56 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
35d57 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
35d58 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
35d59 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 53 54 4d  BLOCK_DELETE_STM
35d5a 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
35d5b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35d5c 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
35d5d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
35d5e 74 36 34 28 73 2c 20 31 2c 20 69 53 74 61 72 74  t64(s, 1, iStart
35d5f 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20  Blockid);.  if( 
35d60 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35d61 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
35d62 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
35d63 69 6e 74 36 34 28 73 2c 20 32 2c 20 69 45 6e 64  int64(s, 2, iEnd
35d64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20  Blockid);.  if( 
35d65 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35d66 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65  return rc;..  re
35d67 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f  turn sql_single_
35d68 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 52  step(s);.}../* R
35d69 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 52 4f  eturns SQLITE_RO
35d6a 57 20 77 69 74 68 20 2a 70 69 64 78 20 73 65 74  W with *pidx set
35d6b 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   to the maximum 
35d6c 73 65 67 6d 65 6e 74 20 69 64 78 20 66 6f 75 6e  segment idx foun
35d6d 64 0a 2a 2a 20 61 74 20 69 4c 65 76 65 6c 2e 20  d.** at iLevel. 
35d6e 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   Returns SQLITE_
35d6f 44 4f 4e 45 20 69 66 20 74 68 65 72 65 20 61 72  DONE if there ar
35d70 65 20 6e 6f 20 73 65 67 6d 65 6e 74 73 20 61 74  e no segments at
35d71 0a 2a 2a 20 69 4c 65 76 65 6c 2e 20 20 4f 74 68  .** iLevel.  Oth
35d72 65 72 77 69 73 65 20 72 65 74 75 72 6e 73 20 61  erwise returns a
35d73 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74  n error..*/.stat
35d74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f 6d 61  ic int segdir_ma
35d75 78 5f 69 6e 64 65 78 28 66 75 6c 6c 74 65 78 74  x_index(fulltext
35d76 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
35d77 65 76 65 6c 2c 20 69 6e 74 20 2a 70 69 64 78 29  evel, int *pidx)
35d78 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
35d79 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20   *s;.  int rc = 
35d7a 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
35d7b 74 28 76 2c 20 53 45 47 44 49 52 5f 4d 41 58 5f  t(v, SEGDIR_MAX_
35d7c 49 4e 44 45 58 5f 53 54 4d 54 2c 20 26 73 29 3b  INDEX_STMT, &s);
35d7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35d7e 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
35d7f 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
35d80 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c  3_bind_int(s, 1,
35d81 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20   iLevel);.  if( 
35d82 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35d83 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
35d84 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
35d85 73 29 3b 0a 20 20 2f 2a 20 53 68 6f 75 6c 64 20  s);.  /* Should 
35d86 61 6c 77 61 79 73 20 67 65 74 20 61 74 20 6c 65  always get at le
35d87 61 73 74 20 6f 6e 65 20 72 6f 77 20 64 75 65 20  ast one row due 
35d88 74 6f 20 68 6f 77 20 6d 61 78 28 29 20 77 6f 72  to how max() wor
35d89 6b 73 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  ks. */.  if( rc=
35d8a 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72  =SQLITE_DONE ) r
35d8b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
35d8c 45 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  E;.  if( rc!=SQL
35d8d 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e  ITE_ROW ) return
35d8e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20   rc;..  /* NULL 
35d8f 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65  means that there
35d90 20 77 65 72 65 20 6e 6f 20 69 6e 70 75 74 73 20   were no inputs 
35d91 74 6f 20 6d 61 78 28 29 2e 20 2a 2f 0a 20 20 69  to max(). */.  i
35d92 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d  f( SQLITE_NULL==
35d93 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
35d94 79 70 65 28 73 2c 20 30 29 20 29 7b 0a 20 20 20  ype(s, 0) ){.   
35d95 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
35d96 65 70 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72  ep(s);.    if( r
35d97 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c==SQLITE_ROW ) 
35d98 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
35d99 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ROR;.    return 
35d9a 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 64 78  rc;.  }..  *pidx
35d9b 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
35d9c 6e 5f 69 6e 74 28 73 2c 20 30 29 3b 0a 0a 20 20  n_int(s, 0);..  
35d9d 2f 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c  /* We expect onl
35d9e 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d  y one row.  We m
35d9f 75 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74  ust execute anot
35da0 68 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70  her sqlite3_step
35da1 28 29 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c  ().   * to compl
35da2 65 74 65 20 74 68 65 20 69 74 65 72 61 74 69 6f  ete the iteratio
35da3 6e 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68 65  n; otherwise the
35da4 20 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61   table will rema
35da5 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20  in locked. */.  
35da6 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
35da7 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  p(s);.  if( rc==
35da8 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74  SQLITE_ROW ) ret
35da9 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
35daa 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35dab 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e  TE_DONE ) return
35dac 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51   rc;.  return SQ
35dad 4c 49 54 45 5f 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20  LITE_ROW;.}../* 
35dae 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 73 65  insert into %_se
35daf 67 64 69 72 20 76 61 6c 75 65 73 20 28 0a 2a 2a  gdir values (.**
35db0 20 20 20 5b 69 4c 65 76 65 6c 5d 2c 20 5b 69 64     [iLevel], [id
35db1 78 5d 2c 0a 2a 2a 20 20 20 5b 69 53 74 61 72 74  x],.**   [iStart
35db2 42 6c 6f 63 6b 69 64 5d 2c 20 5b 69 4c 65 61 76  Blockid], [iLeav
35db3 65 73 45 6e 64 42 6c 6f 63 6b 69 64 5d 2c 20 5b  esEndBlockid], [
35db4 69 45 6e 64 42 6c 6f 63 6b 69 64 5d 2c 0a 2a 2a  iEndBlockid],.**
35db5 20 20 20 5b 70 52 6f 6f 74 44 61 74 61 5d 0a 2a     [pRootData].*
35db6 2a 20 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  * ).*/.static in
35db7 74 20 73 65 67 64 69 72 5f 73 65 74 28 66 75 6c  t segdir_set(ful
35db8 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
35db9 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 69  nt iLevel, int i
35dba 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  dx,.            
35dbb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
35dbc 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f  _int64 iStartBlo
35dbd 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  ckid,.          
35dbe 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
35dbf 74 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73  te_int64 iLeaves
35dc0 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20  EndBlockid,.    
35dc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35dc2 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
35dc3 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20  EndBlockid,.    
35dc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35dc5 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
35dc6 6f 6f 74 44 61 74 61 2c 20 69 6e 74 20 6e 52 6f  ootData, int nRo
35dc7 6f 74 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74  otData){.  sqlit
35dc8 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e  e3_stmt *s;.  in
35dc9 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73  t rc = sql_get_s
35dca 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44  tatement(v, SEGD
35dcb 49 52 5f 53 45 54 5f 53 54 4d 54 2c 20 26 73 29  IR_SET_STMT, &s)
35dcc 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35dcd 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35dce 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
35dcf 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31  e3_bind_int(s, 1
35dd0 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28  , iLevel);.  if(
35dd1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35dd2 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
35dd3 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
35dd4 5f 69 6e 74 28 73 2c 20 32 2c 20 69 64 78 29 3b  _int(s, 2, idx);
35dd5 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35dd6 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
35dd7 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
35dd8 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20  3_bind_int64(s, 
35dd9 33 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  3, iStartBlockid
35dda 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
35ddb 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
35ddc 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
35ddd 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
35dde 2c 20 34 2c 20 69 4c 65 61 76 65 73 45 6e 64 42  , 4, iLeavesEndB
35ddf 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72  lockid);.  if( r
35de0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
35de1 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
35de2 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
35de3 6e 74 36 34 28 73 2c 20 35 2c 20 69 45 6e 64 42  nt64(s, 5, iEndB
35de4 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72  lockid);.  if( r
35de5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
35de6 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
35de7 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62  = sqlite3_bind_b
35de8 6c 6f 62 28 73 2c 20 36 2c 20 70 52 6f 6f 74 44  lob(s, 6, pRootD
35de9 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 20  ata, nRootData, 
35dea 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
35deb 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35dec 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35ded 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73  ..  return sql_s
35dee 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d  ingle_step(s);.}
35def 0a 0a 2f 2a 20 51 75 65 72 69 65 73 20 25 5f 73  ../* Queries %_s
35df0 65 67 64 69 72 20 66 6f 72 20 74 68 65 20 62 6c  egdir for the bl
35df1 6f 63 6b 20 73 70 61 6e 20 6f 66 20 74 68 65 20  ock span of the 
35df2 73 65 67 6d 65 6e 74 73 20 69 6e 20 6c 65 76 65  segments in leve
35df3 6c 0a 2a 2a 20 69 4c 65 76 65 6c 2e 20 20 52 65  l.** iLevel.  Re
35df4 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e  turns SQLITE_DON
35df5 45 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e  E if there are n
35df6 6f 20 62 6c 6f 63 6b 73 20 66 6f 72 20 69 4c 65  o blocks for iLe
35df7 76 65 6c 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 52  vel,.** SQLITE_R
35df8 4f 57 20 69 66 20 74 68 65 72 65 20 61 72 65 20  OW if there are 
35df9 62 6c 6f 63 6b 73 2c 20 65 6c 73 65 20 61 6e 20  blocks, else an 
35dfa 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  error..*/.static
35dfb 20 69 6e 74 20 73 65 67 64 69 72 5f 73 70 61 6e   int segdir_span
35dfc 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
35dfd 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c 0a 20  v, int iLevel,. 
35dfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35dff 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
35e00 36 34 20 2a 70 69 53 74 61 72 74 42 6c 6f 63 6b  64 *piStartBlock
35e01 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  id,.            
35e02 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
35e03 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42 6c  e_int64 *piEndBl
35e04 6f 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  ockid){.  sqlite
35e05 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
35e06 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
35e07 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49  atement(v, SEGDI
35e08 52 5f 53 50 41 4e 5f 53 54 4d 54 2c 20 26 73 29  R_SPAN_STMT, &s)
35e09 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35e0a 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35e0b 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
35e0c 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31  e3_bind_int(s, 1
35e0d 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28  , iLevel);.  if(
35e0e 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35e0f 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
35e10 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
35e11 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  (s);.  if( rc==S
35e12 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74  QLITE_DONE ) ret
35e13 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
35e14 20 20 2f 2a 20 53 68 6f 75 6c 64 20 6e 65 76 65    /* Should neve
35e15 72 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 69 66  r happen */.  if
35e16 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
35e17 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
35e18 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73   /* This happens
35e19 20 69 66 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73   if all segments
35e1a 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 61   at this level a
35e1b 72 65 20 65 6e 74 69 72 65 6c 79 20 69 6e 6c 69  re entirely inli
35e1c 6e 65 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c  ne. */.  if( SQL
35e1d 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65  ITE_NULL==sqlite
35e1e 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 2c  3_column_type(s,
35e1f 20 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65   0) ){.    /* We
35e20 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65   expect only one
35e21 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65   row.  We must e
35e22 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73  xecute another s
35e23 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20  qlite3_step().  
35e24 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65     * to complete
35e25 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20   the iteration; 
35e26 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61  otherwise the ta
35e27 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20  ble will remain 
35e28 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 69  locked. */.    i
35e29 6e 74 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33  nt rc2 = sqlite3
35e2a 5f 73 74 65 70 28 73 29 3b 0a 20 20 20 20 69 66  _step(s);.    if
35e2b 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 52 4f  ( rc2==SQLITE_RO
35e2c 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  W ) return SQLIT
35e2d 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74  E_ERROR;.    ret
35e2e 75 72 6e 20 72 63 32 3b 0a 20 20 7d 0a 0a 20 20  urn rc2;.  }..  
35e2f 2a 70 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20  *piStartBlockid 
35e30 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
35e31 5f 69 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20  _int64(s, 0);.  
35e32 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20  *piEndBlockid = 
35e33 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
35e34 6e 74 36 34 28 73 2c 20 31 29 3b 0a 0a 20 20 2f  nt64(s, 1);..  /
35e35 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79  * We expect only
35e36 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75   one row.  We mu
35e37 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68  st execute anoth
35e38 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  er sqlite3_step(
35e39 29 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65  ).   * to comple
35e3a 74 65 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e  te the iteration
35e3b 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20  ; otherwise the 
35e3c 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69  table will remai
35e3d 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72  n locked. */.  r
35e3e 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
35e3f 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  (s);.  if( rc==S
35e40 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
35e41 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
35e42 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35e43 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  E_DONE ) return 
35e44 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  rc;.  return SQL
35e45 49 54 45 5f 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20 44  ITE_ROW;.}../* D
35e46 65 6c 65 74 65 20 74 68 65 20 73 65 67 6d 65 6e  elete the segmen
35e47 74 20 62 6c 6f 63 6b 73 20 61 6e 64 20 73 65 67  t blocks and seg
35e48 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 72  ment directory r
35e49 65 63 6f 72 64 73 20 66 6f 72 20 61 6c 6c 0a 2a  ecords for all.*
35e4a 2a 20 73 65 67 6d 65 6e 74 73 20 61 74 20 69 4c  * segments at iL
35e4b 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  evel..*/.static 
35e4c 69 6e 74 20 73 65 67 64 69 72 5f 64 65 6c 65 74  int segdir_delet
35e4d 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
35e4e 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29 7b  *v, int iLevel){
35e4f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
35e50 2a 73 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  *s;.  sqlite_int
35e51 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  64 iStartBlockid
35e52 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 0a 20  , iEndBlockid;. 
35e53 20 69 6e 74 20 72 63 20 3d 20 73 65 67 64 69 72   int rc = segdir
35e54 5f 73 70 61 6e 28 76 2c 20 69 4c 65 76 65 6c 2c  _span(v, iLevel,
35e55 20 26 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c   &iStartBlockid,
35e56 20 26 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a   &iEndBlockid);.
35e57 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35e58 5f 52 4f 57 20 26 26 20 72 63 21 3d 53 51 4c 49  _ROW && rc!=SQLI
35e59 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e  TE_DONE ) return
35e5a 20 72 63 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d   rc;..  if( rc==
35e5b 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
35e5c 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 64 65 6c    rc = block_del
35e5d 65 74 65 28 76 2c 20 69 53 74 61 72 74 42 6c 6f  ete(v, iStartBlo
35e5e 63 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69  ckid, iEndBlocki
35e5f 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  d);.    if( rc!=
35e60 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
35e61 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
35e62 20 44 65 6c 65 74 65 20 74 68 65 20 73 65 67 6d   Delete the segm
35e63 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69 74  ent directory it
35e64 73 65 6c 66 2e 20 2a 2f 0a 20 20 72 63 20 3d 20  self. */.  rc = 
35e65 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
35e66 74 28 76 2c 20 53 45 47 44 49 52 5f 44 45 4c 45  t(v, SEGDIR_DELE
35e67 54 45 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  TE_STMT, &s);.  
35e68 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35e69 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
35e6a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
35e6b 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20  ind_int64(s, 1, 
35e6c 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 72  iLevel);.  if( r
35e6d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
35e6e 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74  eturn rc;..  ret
35e6f 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73  urn sql_single_s
35e70 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 44 65  tep(s);.}../* De
35e71 6c 65 74 65 20 65 6e 74 69 72 65 20 66 74 73 20  lete entire fts 
35e72 69 6e 64 65 78 2c 20 53 51 4c 49 54 45 5f 4f 4b  index, SQLITE_OK
35e73 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 72 65 6c   on success, rel
35e74 65 76 61 6e 74 20 65 72 72 6f 72 20 6f 6e 0a 2a  evant error on.*
35e75 2a 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74  * failure..*/.st
35e76 61 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f  atic int segdir_
35e77 64 65 6c 65 74 65 5f 61 6c 6c 28 66 75 6c 6c 74  delete_all(fullt
35e78 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20  ext_vtab *v){.  
35e79 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
35e7a 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f  .  int rc = sql_
35e7b 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
35e7c 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f 41   SEGDIR_DELETE_A
35e7d 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  LL_STMT, &s);.  
35e7e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35e7f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
35e80 20 20 72 63 20 3d 20 73 71 6c 5f 73 69 6e 67 6c    rc = sql_singl
35e81 65 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28  e_step(s);.  if(
35e82 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35e83 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
35e84 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  c = sql_get_stat
35e85 65 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 44  ement(v, BLOCK_D
35e86 45 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 20  ELETE_ALL_STMT, 
35e87 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
35e88 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35e89 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20  n rc;..  return 
35e8a 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28  sql_single_step(
35e8b 73 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e  s);.}../* Return
35e8c 73 20 53 51 4c 49 54 45 5f 4f 4b 20 77 69 74 68  s SQLITE_OK with
35e8d 20 2a 70 6e 53 65 67 6d 65 6e 74 73 20 73 65 74   *pnSegments set
35e8e 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
35e8f 66 20 65 6e 74 72 69 65 73 20 69 6e 0a 2a 2a 20  f entries in.** 
35e90 25 5f 73 65 67 64 69 72 20 61 6e 64 20 2a 70 69  %_segdir and *pi
35e91 4d 61 78 4c 65 76 65 6c 20 73 65 74 20 74 6f 20  MaxLevel set to 
35e92 74 68 65 20 68 69 67 68 65 73 74 20 6c 65 76 65  the highest leve
35e93 6c 20 77 68 69 63 68 20 68 61 73 20 61 0a 2a 2a  l which has a.**
35e94 20 73 65 67 6d 65 6e 74 2e 20 20 4f 74 68 65 72   segment.  Other
35e95 77 69 73 65 20 72 65 74 75 72 6e 73 20 74 68 65  wise returns the
35e96 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 77 68   SQLite error wh
35e97 69 63 68 20 63 61 75 73 65 64 20 66 61 69 6c 75  ich caused failu
35e98 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  re..*/.static in
35e99 74 20 73 65 67 64 69 72 5f 63 6f 75 6e 74 28 66  t segdir_count(f
35e9a 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
35e9b 20 69 6e 74 20 2a 70 6e 53 65 67 6d 65 6e 74 73   int *pnSegments
35e9c 2c 20 69 6e 74 20 2a 70 69 4d 61 78 4c 65 76 65  , int *piMaxLeve
35e9d 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  l){.  sqlite3_st
35e9e 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20  mt *s;.  int rc 
35e9f 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
35ea0 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 43 4f  ent(v, SEGDIR_CO
35ea1 55 4e 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  UNT_STMT, &s);. 
35ea2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35ea3 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35ea4 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
35ea5 73 74 65 70 28 73 29 3b 0a 20 20 2f 2a 20 54 4f  step(s);.  /* TO
35ea6 44 4f 28 73 68 65 73 73 29 3a 20 54 68 69 73 20  DO(shess): This 
35ea7 63 61 73 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  case should not 
35ea8 62 65 20 70 6f 73 73 69 62 6c 65 3f 20 20 53 68  be possible?  Sh
35ea9 6f 75 6c 64 20 73 74 72 6f 6e 67 65 72 0a 20 20  ould stronger.  
35eaa 2a 2a 20 6d 65 61 73 75 72 65 73 20 62 65 20 74  ** measures be t
35eab 61 6b 65 6e 20 69 66 20 69 74 20 68 61 70 70 65  aken if it happe
35eac 6e 73 3f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  ns?.  */.  if( r
35ead 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c==SQLITE_DONE )
35eae 7b 0a 20 20 20 20 2a 70 6e 53 65 67 6d 65 6e 74  {.    *pnSegment
35eaf 73 20 3d 20 30 3b 0a 20 20 20 20 2a 70 69 4d 61  s = 0;.    *piMa
35eb0 78 4c 65 76 65 6c 20 3d 20 30 3b 0a 20 20 20 20  xLevel = 0;.    
35eb1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
35eb2 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d  ;.  }.  if( rc!=
35eb3 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74  SQLITE_ROW ) ret
35eb4 75 72 6e 20 72 63 3b 0a 0a 20 20 2a 70 6e 53 65  urn rc;..  *pnSe
35eb5 67 6d 65 6e 74 73 20 3d 20 73 71 6c 69 74 65 33  gments = sqlite3
35eb6 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 30  _column_int(s, 0
35eb7 29 3b 0a 20 20 2a 70 69 4d 61 78 4c 65 76 65 6c  );.  *piMaxLevel
35eb8 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
35eb9 6e 5f 69 6e 74 28 73 2c 20 31 29 3b 0a 0a 20 20  n_int(s, 1);..  
35eba 2f 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c  /* We expect onl
35ebb 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d  y one row.  We m
35ebc 75 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74  ust execute anot
35ebd 68 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70  her sqlite3_step
35ebe 28 29 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c  ().   * to compl
35ebf 65 74 65 20 74 68 65 20 69 74 65 72 61 74 69 6f  ete the iteratio
35ec0 6e 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68 65  n; otherwise the
35ec1 20 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61   table will rema
35ec2 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20  in locked. */.  
35ec3 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
35ec4 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  p(s);.  if( rc==
35ec5 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65  SQLITE_DONE ) re
35ec6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
35ec7 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
35ec8 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51  _ROW ) return SQ
35ec9 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 72 65  LITE_ERROR;.  re
35eca 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54  turn rc;.}../* T
35ecb 4f 44 4f 28 73 68 65 73 73 29 20 63 6c 65 61 72  ODO(shess) clear
35ecc 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 29 20 69  PendingTerms() i
35ecd 73 20 66 61 72 20 64 6f 77 6e 20 74 68 65 20 66  s far down the f
35ece 69 6c 65 20 62 65 63 61 75 73 65 0a 2a 2a 20 77  ile because.** w
35ecf 72 69 74 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28  riteZeroSegment(
35ed0 29 20 69 73 20 66 61 72 20 64 6f 77 6e 20 74 68  ) is far down th
35ed1 65 20 66 69 6c 65 20 62 65 63 61 75 73 65 20 4c  e file because L
35ed2 65 61 66 57 72 69 74 65 72 20 69 73 20 66 61 72  eafWriter is far
35ed3 0a 2a 2a 20 64 6f 77 6e 20 74 68 65 20 66 69 6c  .** down the fil
35ed4 65 2e 20 20 43 6f 6e 73 69 64 65 72 20 72 65 66  e.  Consider ref
35ed5 61 63 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 64  actoring the cod
35ed6 65 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 6e 6f  e to move the no
35ed7 6e 2d 76 74 61 62 0a 2a 2a 20 63 6f 64 65 20 61  n-vtab.** code a
35ed8 62 6f 76 65 20 74 68 65 20 76 74 61 62 20 63 6f  bove the vtab co
35ed9 64 65 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f  de so that we do
35eda 6e 27 74 20 6e 65 65 64 20 74 68 69 73 20 66 6f  n't need this fo
35edb 72 77 61 72 64 0a 2a 2a 20 72 65 66 65 72 65 6e  rward.** referen
35edc 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
35edd 74 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65  t clearPendingTe
35ede 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rms(fulltext_vta
35edf 62 20 2a 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72  b *v);../*.** Fr
35ee0 65 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73  ee the memory us
35ee1 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20  ed to contain a 
35ee2 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 73 74  fulltext_vtab st
35ee3 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74  ructure..*/.stat
35ee4 69 63 20 76 6f 69 64 20 66 75 6c 6c 74 65 78 74  ic void fulltext
35ee5 5f 76 74 61 62 5f 64 65 73 74 72 6f 79 28 66 75  _vtab_destroy(fu
35ee6 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b  lltext_vtab *v){
35ee7 0a 20 20 69 6e 74 20 69 53 74 6d 74 2c 20 69 3b  .  int iStmt, i;
35ee8 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46  ..  FTSTRACE(("F
35ee9 54 53 33 20 44 65 73 74 72 6f 79 20 25 70 5c 6e  TS3 Destroy %p\n
35eea 22 2c 20 76 29 29 3b 0a 20 20 66 6f 72 28 20 69  ", v));.  for( i
35eeb 53 74 6d 74 3d 30 3b 20 69 53 74 6d 74 3c 4d 41  Stmt=0; iStmt<MA
35eec 58 5f 53 54 4d 54 3b 20 69 53 74 6d 74 2b 2b 20  X_STMT; iStmt++ 
35eed 29 7b 0a 20 20 20 20 69 66 28 20 76 2d 3e 70 46  ){.    if( v->pF
35eee 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74  ulltextStatement
35eef 73 5b 69 53 74 6d 74 5d 21 3d 4e 55 4c 4c 20 29  s[iStmt]!=NULL )
35ef0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
35ef1 66 69 6e 61 6c 69 7a 65 28 76 2d 3e 70 46 75 6c  finalize(v->pFul
35ef2 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b  ltextStatements[
35ef3 69 53 74 6d 74 5d 29 3b 0a 20 20 20 20 20 20 76  iStmt]);.      v
35ef4 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65  ->pFulltextState
35ef5 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 20 3d 20 4e  ments[iStmt] = N
35ef6 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ULL;.    }.  }..
35ef7 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 4d 45    for( i=0; i<ME
35ef8 52 47 45 5f 43 4f 55 4e 54 3b 20 69 2b 2b 20 29  RGE_COUNT; i++ )
35ef9 7b 0a 20 20 20 20 69 66 28 20 76 2d 3e 70 4c 65  {.    if( v->pLe
35efa 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 5d  afSelectStmts[i]
35efb 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  !=NULL ){.      
35efc 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
35efd 28 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53  (v->pLeafSelectS
35efe 74 6d 74 73 5b 69 5d 29 3b 0a 20 20 20 20 20 20  tmts[i]);.      
35eff 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74  v->pLeafSelectSt
35f00 6d 74 73 5b 69 5d 20 3d 20 4e 55 4c 4c 3b 0a 20  mts[i] = NULL;. 
35f01 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
35f02 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 21 3d 4e  v->pTokenizer!=N
35f03 55 4c 4c 20 29 7b 0a 20 20 20 20 76 2d 3e 70 54  ULL ){.    v->pT
35f04 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
35f05 65 2d 3e 78 44 65 73 74 72 6f 79 28 76 2d 3e 70  e->xDestroy(v->p
35f06 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 20 20  Tokenizer);.    
35f07 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  v->pTokenizer = 
35f08 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 63 6c 65  NULL;.  }..  cle
35f09 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  arPendingTerms(v
35f0a 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
35f0b 65 65 28 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 29 3b  ee(v->azColumn);
35f0c 0a 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 20  .  for(i = 0; i 
35f0d 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b  < v->nColumn; ++
35f0e 69 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  i) {.    sqlite3
35f0f 5f 66 72 65 65 28 76 2d 3e 61 7a 43 6f 6e 74 65  _free(v->azConte
35f10 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20  ntColumn[i]);.  
35f11 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  }.  sqlite3_free
35f12 28 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c  (v->azContentCol
35f13 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  umn);.  sqlite3_
35f14 66 72 65 65 28 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  free(v);.}../*.*
35f15 2a 20 54 6f 6b 65 6e 20 74 79 70 65 73 20 66 6f  * Token types fo
35f16 72 20 70 61 72 73 69 6e 67 20 74 68 65 20 61 72  r parsing the ar
35f17 67 75 6d 65 6e 74 73 20 74 6f 20 78 43 6f 6e 6e  guments to xConn
35f18 65 63 74 20 6f 72 20 78 43 72 65 61 74 65 2e 0a  ect or xCreate..
35f19 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e  */.#define TOKEN
35f1a 5f 45 4f 46 20 20 20 20 20 20 20 20 20 30 20 20  _EOF         0  
35f1b 20 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69 6c 65    /* End of file
35f1c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45   */.#define TOKE
35f1d 4e 5f 53 50 41 43 45 20 20 20 20 20 20 20 31 20  N_SPACE       1 
35f1e 20 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f     /* Any kind o
35f1f 66 20 77 68 69 74 65 73 70 61 63 65 20 2a 2f 0a  f whitespace */.
35f20 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 49 44  #define TOKEN_ID
35f21 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20 2f            2    /
35f22 2a 20 41 6e 20 69 64 65 6e 74 69 66 69 65 72 20  * An identifier 
35f23 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e  */.#define TOKEN
35f24 5f 53 54 52 49 4e 47 20 20 20 20 20 20 33 20 20  _STRING      3  
35f25 20 20 2f 2a 20 41 20 73 74 72 69 6e 67 20 6c 69    /* A string li
35f26 74 65 72 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65  teral */.#define
35f27 20 54 4f 4b 45 4e 5f 50 55 4e 43 54 20 20 20 20   TOKEN_PUNCT    
35f28 20 20 20 34 20 20 20 20 2f 2a 20 41 20 73 69 6e     4    /* A sin
35f29 67 6c 65 20 70 75 6e 63 74 75 61 74 69 6f 6e 20  gle punctuation 
35f2a 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 0a 2f 2a  character */../*
35f2b 0a 2a 2a 20 49 66 20 58 20 69 73 20 61 20 63 68  .** If X is a ch
35f2c 61 72 61 63 74 65 72 20 74 68 61 74 20 63 61 6e  aracter that can
35f2d 20 62 65 20 75 73 65 64 20 69 6e 20 61 6e 20 69   be used in an i
35f2e 64 65 6e 74 69 66 69 65 72 20 74 68 65 6e 0a 2a  dentifier then.*
35f2f 2a 20 66 74 73 49 64 43 68 61 72 28 58 29 20 77  * ftsIdChar(X) w
35f30 69 6c 6c 20 62 65 20 74 72 75 65 2e 20 20 4f 74  ill be true.  Ot
35f31 68 65 72 77 69 73 65 20 69 74 20 69 73 20 66 61  herwise it is fa
35f32 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41  lse..**.** For A
35f33 53 43 49 49 2c 20 61 6e 79 20 63 68 61 72 61 63  SCII, any charac
35f34 74 65 72 20 77 69 74 68 20 74 68 65 20 68 69 67  ter with the hig
35f35 68 2d 6f 72 64 65 72 20 62 69 74 20 73 65 74 20  h-order bit set 
35f36 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 69 6e  is.** allowed in
35f37 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 2e 20   an identifier. 
35f38 20 46 6f 72 20 37 2d 62 69 74 20 63 68 61 72 61   For 7-bit chara
35f39 63 74 65 72 73 2c 20 0a 2a 2a 20 69 73 46 74 73  cters, .** isFts
35f3a 49 64 43 68 61 72 5b 58 5d 20 6d 75 73 74 20 62  IdChar[X] must b
35f3b 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65  e 1..**.** Ticke
35f3c 74 20 23 31 30 36 36 2e 20 20 74 68 65 20 53 51  t #1066.  the SQ
35f3d 4c 20 73 74 61 6e 64 61 72 64 20 64 6f 65 73 20  L standard does 
35f3e 6e 6f 74 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e  not allow '$' in
35f3f 20 74 68 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f   the.** middle o
35f40 66 20 69 64 65 6e 74 66 69 65 72 73 2e 20 20 42  f identfiers.  B
35f41 75 74 20 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c  ut many SQL impl
35f42 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20  ementations do. 
35f43 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
35f44 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 69 64 65  allow '$' in ide
35f45 6e 74 69 66 69 65 72 73 20 66 6f 72 20 63 6f 6d  ntifiers for com
35f46 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42  patibility..** B
35f47 75 74 20 74 68 65 20 66 65 61 74 75 72 65 20 69  ut the feature i
35f48 73 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a  s undocumented..
35f49 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
35f4a 63 68 61 72 20 69 73 46 74 73 49 64 43 68 61 72  char isFtsIdChar
35f4b 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20  [] = {./* x0 x1 
35f4c 78 32 20 78 33 20 78 34 20 78 35 20 78 36 20 78  x2 x3 x4 x5 x6 x
35f4d 37 20 78 38 20 78 39 20 78 41 20 78 42 20 78 43  7 x8 x9 xA xB xC
35f4e 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20 20   xD xE xF */.   
35f4f 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20   0, 0, 0, 0, 1, 
35f50 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
35f51 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
35f52 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 31    /* 2x */.    1
35f53 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
35f54 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
35f55 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20  0, 0, 0, 0, 0,  
35f56 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20  /* 3x */.    0, 
35f57 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
35f58 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
35f59 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a   1, 1, 1, 1,  /*
35f5a 20 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c   4x */.    1, 1,
35f5b 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
35f5c 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
35f5d 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35  , 0, 0, 1,  /* 5
35f5e 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31  x */.    0, 1, 1
35f5f 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
35f60 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
35f61 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20  1, 1, 1,  /* 6x 
35f62 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20  */.    1, 1, 1, 
35f63 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
35f64 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
35f65 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f   0, 0,  /* 7x */
35f66 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 66 74 73 49  .};.#define ftsI
35f67 64 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43  dChar(C)  (((c=C
35f68 29 26 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63  )&0x80)!=0 || (c
35f69 3e 30 78 31 66 20 26 26 20 69 73 46 74 73 49 64  >0x1f && isFtsId
35f6a 43 68 61 72 5b 63 2d 30 78 32 30 5d 29 29 0a 0a  Char[c-0x20]))..
35f6b 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
35f6c 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
35f6d 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e  token that begin
35f6e 73 20 61 74 20 7a 5b 30 5d 2e 20 0a 2a 2a 20 53  s at z[0]. .** S
35f6f 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20 74  tore the token t
35f70 79 70 65 20 69 6e 20 2a 74 6f 6b 65 6e 54 79 70  ype in *tokenTyp
35f71 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  e before returni
35f72 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
35f73 74 20 66 74 73 47 65 74 54 6f 6b 65 6e 28 63 6f  t ftsGetToken(co
35f74 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
35f75 20 2a 74 6f 6b 65 6e 54 79 70 65 29 7b 0a 20 20   *tokenType){.  
35f76 69 6e 74 20 69 2c 20 63 3b 0a 20 20 73 77 69 74  int i, c;.  swit
35f77 63 68 28 20 2a 7a 20 29 7b 0a 20 20 20 20 63 61  ch( *z ){.    ca
35f78 73 65 20 30 3a 20 7b 0a 20 20 20 20 20 20 2a 74  se 0: {.      *t
35f79 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e  okenType = TOKEN
35f7a 5f 45 4f 46 3b 0a 20 20 20 20 20 20 72 65 74 75  _EOF;.      retu
35f7b 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 0;.    }.    
35f7c 63 61 73 65 20 27 20 27 3a 20 63 61 73 65 20 27  case ' ': case '
35f7d 5c 74 27 3a 20 63 61 73 65 20 27 5c 6e 27 3a 20  \t': case '\n': 
35f7e 63 61 73 65 20 27 5c 66 27 3a 20 63 61 73 65 20  case '\f': case 
35f7f 27 5c 72 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f  '\r': {.      fo
35f80 72 28 69 3d 31 3b 20 73 61 66 65 5f 69 73 73 70  r(i=1; safe_issp
35f81 61 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b  ace(z[i]); i++){
35f82 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  }.      *tokenTy
35f83 70 65 20 3d 20 54 4f 4b 45 4e 5f 53 50 41 43 45  pe = TOKEN_SPACE
35f84 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ;.      return i
35f85 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
35f86 20 27 60 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '`':.    case '
35f87 5c 27 27 3a 0a 20 20 20 20 63 61 73 65 20 27 22  \'':.    case '"
35f88 27 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64  ': {.      int d
35f89 65 6c 69 6d 20 3d 20 7a 5b 30 5d 3b 0a 20 20 20  elim = z[0];.   
35f8a 20 20 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a     for(i=1; (c=z
35f8b 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20  [i])!=0; i++){. 
35f8c 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 64 65         if( c==de
35f8d 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  lim ){.         
35f8e 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c   if( z[i+1]==del
35f8f 69 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  im ){.          
35f90 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
35f91 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
35f92 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
35f93 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
35f94 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a  .      }.      *
35f95 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45  tokenType = TOKE
35f96 4e 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 20 20  N_STRING;.      
35f97 72 65 74 75 72 6e 20 69 20 2b 20 28 63 21 3d 30  return i + (c!=0
35f98 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  );.    }.    cas
35f99 65 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66  e '[': {.      f
35f9a 6f 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20  or(i=1, c=z[0]; 
35f9b 63 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69  c!=']' && (c=z[i
35f9c 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20  ])!=0; i++){}.  
35f9d 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
35f9e 20 54 4f 4b 45 4e 5f 49 44 3b 0a 20 20 20 20 20   TOKEN_ID;.     
35f9f 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d   return i;.    }
35fa0 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
35fa1 20 20 20 20 20 20 69 66 28 20 21 66 74 73 49 64        if( !ftsId
35fa2 43 68 61 72 28 2a 7a 29 20 29 7b 0a 20 20 20 20  Char(*z) ){.    
35fa3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
35fa4 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31   }.      for(i=1
35fa5 3b 20 66 74 73 49 64 43 68 61 72 28 7a 5b 69 5d  ; ftsIdChar(z[i]
35fa6 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20  ); i++){}.      
35fa7 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b  *tokenType = TOK
35fa8 45 4e 5f 49 44 3b 0a 20 20 20 20 20 20 72 65 74  EN_ID;.      ret
35fa9 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn i;.    }.  }
35faa 0a 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20  .  *tokenType = 
35fab 54 4f 4b 45 4e 5f 50 55 4e 43 54 3b 0a 20 20 72  TOKEN_PUNCT;.  r
35fac 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
35fad 2a 20 41 20 74 6f 6b 65 6e 20 65 78 74 72 61 63  * A token extrac
35fae 74 65 64 20 66 72 6f 6d 20 61 20 73 74 72 69 6e  ted from a strin
35faf 67 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  g is an instance
35fb0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
35fb1 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a  g.** structure..
35fb2 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
35fb3 74 20 46 74 73 54 6f 6b 65 6e 20 7b 0a 20 20 63  t FtsToken {.  c
35fb4 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20 20 20  onst char *z;   
35fb5 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
35fb6 6f 20 74 6f 6b 65 6e 20 74 65 78 74 2e 20 20 4e  o token text.  N
35fb7 6f 74 20 27 5c 30 30 30 27 20 74 65 72 6d 69 6e  ot '\000' termin
35fb8 61 74 65 64 20 2a 2f 0a 20 20 73 68 6f 72 74 20  ated */.  short 
35fb9 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 2f  int n;         /
35fba 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  * Length of the 
35fbb 74 6f 6b 65 6e 20 74 65 78 74 20 69 6e 20 62 79  token text in by
35fbc 74 65 73 2e 20 2a 2f 0a 7d 20 46 74 73 54 6f 6b  tes. */.} FtsTok
35fbd 65 6e 3b 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  en;../*.** Given
35fbe 20 61 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20   a input string 
35fbf 28 77 68 69 63 68 20 69 73 20 72 65 61 6c 6c 79  (which is really
35fc0 20 6f 6e 65 20 6f 66 20 74 68 65 20 61 72 67 76   one of the argv
35fc1 5b 5d 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a  [] parameters.**
35fc2 20 70 61 73 73 65 64 20 69 6e 74 6f 20 78 43 6f   passed into xCo
35fc3 6e 6e 65 63 74 20 6f 72 20 78 43 72 65 61 74 65  nnect or xCreate
35fc4 29 20 73 70 6c 69 74 20 74 68 65 20 73 74 72 69  ) split the stri
35fc5 6e 67 20 75 70 20 69 6e 74 6f 20 74 6f 6b 65 6e  ng up into token
35fc6 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20  s..** Return an 
35fc7 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72  array of pointer
35fc8 73 20 74 6f 20 27 5c 30 30 30 27 20 74 65 72 6d  s to '\000' term
35fc9 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 2c 20  inated strings, 
35fca 6f 6e 65 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f  one string.** fo
35fcb 72 20 65 61 63 68 20 6e 6f 6e 2d 77 68 69 74 65  r each non-white
35fcc 73 70 61 63 65 20 74 6f 6b 65 6e 2e 0a 2a 2a 0a  space token..**.
35fcd 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
35fce 61 72 72 61 79 20 69 73 20 74 65 72 6d 69 6e 61  array is termina
35fcf 74 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20  ted by a single 
35fd0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  NULL pointer..**
35fd1 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  .** Space to hol
35fd2 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61  d the returned a
35fd3 72 72 61 79 20 69 73 20 6f 62 74 61 69 6e 65 64  rray is obtained
35fd4 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 0a 2a   from a single.*
35fd5 2a 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 73 68 6f  * malloc and sho
35fd6 75 6c 64 20 62 65 20 66 72 65 65 64 20 62 79 20  uld be freed by 
35fd7 70 61 73 73 69 6e 67 20 74 68 65 20 72 65 74 75  passing the retu
35fd8 72 6e 20 76 61 6c 75 65 20 74 6f 20 66 72 65 65  rn value to free
35fd9 28 29 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 69 76  ()..** The indiv
35fda 69 64 75 61 6c 20 73 74 72 69 6e 67 73 20 77 69  idual strings wi
35fdb 74 68 69 6e 20 74 68 65 20 74 6f 6b 65 6e 20 6c  thin the token l
35fdc 69 73 74 20 61 72 65 20 61 6c 6c 20 61 20 70 61  ist are all a pa
35fdd 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 73 69 6e  rt of.** the sin
35fde 67 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  gle memory alloc
35fdf 61 74 69 6f 6e 20 61 6e 64 20 77 69 6c 6c 20 61  ation and will a
35fe0 6c 6c 20 62 65 20 66 72 65 65 64 20 61 74 20 6f  ll be freed at o
35fe1 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  nce..*/.static c
35fe2 68 61 72 20 2a 2a 74 6f 6b 65 6e 69 7a 65 53 74  har **tokenizeSt
35fe3 72 69 6e 67 28 63 6f 6e 73 74 20 63 68 61 72 20  ring(const char 
35fe4 2a 7a 2c 20 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e  *z, int *pnToken
35fe5 29 7b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20  ){.  int nToken 
35fe6 3d 20 30 3b 0a 20 20 46 74 73 54 6f 6b 65 6e 20  = 0;.  FtsToken 
35fe7 2a 61 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65  *aToken = sqlite
35fe8 33 5f 6d 61 6c 6c 6f 63 28 20 73 74 72 6c 65 6e  3_malloc( strlen
35fe9 28 7a 29 20 2a 20 73 69 7a 65 6f 66 28 61 54 6f  (z) * sizeof(aTo
35fea 6b 65 6e 5b 30 5d 29 20 29 3b 0a 20 20 69 6e 74  ken[0]) );.  int
35feb 20 6e 20 3d 20 31 3b 0a 20 20 69 6e 74 20 65 2c   n = 1;.  int e,
35fec 20 69 3b 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53   i;.  int totalS
35fed 69 7a 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  ize = 0;.  char 
35fee 2a 2a 61 7a 54 6f 6b 65 6e 3b 0a 20 20 63 68 61  **azToken;.  cha
35fef 72 20 2a 7a 43 6f 70 79 3b 0a 20 20 77 68 69 6c  r *zCopy;.  whil
35ff0 65 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 6e 20  e( n>0 ){.    n 
35ff1 3d 20 66 74 73 47 65 74 54 6f 6b 65 6e 28 7a 2c  = ftsGetToken(z,
35ff2 20 26 65 29 3b 0a 20 20 20 20 69 66 28 20 65 21   &e);.    if( e!
35ff3 3d 54 4f 4b 45 4e 5f 53 50 41 43 45 20 29 7b 0a  =TOKEN_SPACE ){.
35ff4 20 20 20 20 20 20 61 54 6f 6b 65 6e 5b 6e 54 6f        aToken[nTo
35ff5 6b 65 6e 5d 2e 7a 20 3d 20 7a 3b 0a 20 20 20 20  ken].z = z;.    
35ff6 20 20 61 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d    aToken[nToken]
35ff7 2e 6e 20 3d 20 6e 3b 0a 20 20 20 20 20 20 6e 54  .n = n;.      nT
35ff8 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 74 6f  oken++;.      to
35ff9 74 61 6c 53 69 7a 65 20 2b 3d 20 6e 2b 31 3b 0a  talSize += n+1;.
35ffa 20 20 20 20 7d 0a 20 20 20 20 7a 20 2b 3d 20 6e      }.    z += n
35ffb 3b 0a 20 20 7d 0a 20 20 61 7a 54 6f 6b 65 6e 20  ;.  }.  azToken 
35ffc 3d 20 28 63 68 61 72 2a 2a 29 73 71 6c 69 74 65  = (char**)sqlite
35ffd 33 5f 6d 61 6c 6c 6f 63 28 20 6e 54 6f 6b 65 6e  3_malloc( nToken
35ffe 2a 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 20 2b  *sizeof(char*) +
35fff 20 74 6f 74 61 6c 53 69 7a 65 20 29 3b 0a 20 20   totalSize );.  
36000 7a 43 6f 70 79 20 3d 20 28 63 68 61 72 2a 29 26  zCopy = (char*)&
36001 61 7a 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 3b  azToken[nToken];
36002 0a 20 20 6e 54 6f 6b 65 6e 2d 2d 3b 0a 20 20 66  .  nToken--;.  f
36003 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 6f 6b 65 6e  or(i=0; i<nToken
36004 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 7a 54 6f  ; i++){.    azTo
36005 6b 65 6e 5b 69 5d 20 3d 20 7a 43 6f 70 79 3b 0a  ken[i] = zCopy;.
36006 20 20 20 20 6e 20 3d 20 61 54 6f 6b 65 6e 5b 69      n = aToken[i
36007 5d 2e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  ].n;.    memcpy(
36008 7a 43 6f 70 79 2c 20 61 54 6f 6b 65 6e 5b 69 5d  zCopy, aToken[i]
36009 2e 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 43 6f 70  .z, n);.    zCop
3600a 79 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 43  y[n] = 0;.    zC
3600b 6f 70 79 20 2b 3d 20 6e 2b 31 3b 0a 20 20 7d 0a  opy += n+1;.  }.
3600c 20 20 61 7a 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e    azToken[nToken
3600d 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  ] = 0;.  sqlite3
3600e 5f 66 72 65 65 28 61 54 6f 6b 65 6e 29 3b 0a 20  _free(aToken);. 
3600f 20 2a 70 6e 54 6f 6b 65 6e 20 3d 20 6e 54 6f 6b   *pnToken = nTok
36010 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20 61 7a 54  en;.  return azT
36011 6f 6b 65 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  oken;.}../*.** C
36012 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 74  onvert an SQL-st
36013 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69 6e  yle quoted strin
36014 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20  g into a normal 
36015 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 69  string by removi
36016 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 20  ng.** the quote 
36017 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 65  characters.  The
36018 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 64   conversion is d
36019 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49  one in-place.  I
3601a 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64  f the.** input d
3601b 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69  oes not begin wi
3601c 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72 61  th a quote chara
3601d 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20  cter, then this 
3601e 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20  routine.** is a 
3601f 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 61  no-op..**.** Exa
36020 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  mples:.**.**    
36021 20 22 61 62 63 22 20 20 20 62 65 63 6f 6d 65 73   "abc"   becomes
36022 20 20 20 61 62 63 0a 2a 2a 20 20 20 20 20 27 78     abc.**     'x
36023 79 7a 27 20 20 20 62 65 63 6f 6d 65 73 20 20 20  yz'   becomes   
36024 78 79 7a 0a 2a 2a 20 20 20 20 20 5b 70 71 72 5d  xyz.**     [pqr]
36025 20 20 20 62 65 63 6f 6d 65 73 20 20 20 70 71 72     becomes   pqr
36026 0a 2a 2a 20 20 20 20 20 60 6d 6e 6f 60 20 20 20  .**     `mno`   
36027 62 65 63 6f 6d 65 73 20 20 20 6d 6e 6f 0a 2a 2f  becomes   mno.*/
36028 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 71  .static void deq
36029 75 6f 74 65 53 74 72 69 6e 67 28 63 68 61 72 20  uoteString(char 
3602a 2a 7a 29 7b 0a 20 20 69 6e 74 20 71 75 6f 74 65  *z){.  int quote
3602b 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  ;.  int i, j;.  
3602c 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72  if( z==0 ) retur
3602d 6e 3b 0a 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30  n;.  quote = z[0
3602e 5d 3b 0a 20 20 73 77 69 74 63 68 28 20 71 75 6f  ];.  switch( quo
3602f 74 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27  te ){.    case '
36030 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20  \'':  break;.   
36031 20 63 61 73 65 20 27 22 27 3a 20 20 20 62 72 65   case '"':   bre
36032 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 60 27  ak;.    case '`'
36033 3a 20 20 20 62 72 65 61 6b 3b 20 20 20 20 20 20  :   break;      
36034 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
36035 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74 69 62 69   MySQL compatibi
36036 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63 61 73 65  lity */.    case
36037 20 27 5b 27 3a 20 20 20 71 75 6f 74 65 20 3d 20   '[':   quote = 
36038 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a  ']';  break;  /*
36039 20 46 6f 72 20 4d 53 20 53 71 6c 53 65 72 76 65   For MS SqlServe
3603a 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  r compatibility 
3603b 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  */.    default: 
3603c 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
3603d 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b 20 7a   for(i=1, j=0; z
3603e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  [i]; i++){.    i
3603f 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29  f( z[i]==quote )
36040 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b  {.      if( z[i+
36041 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20  1]==quote ){.   
36042 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75       z[j++] = qu
36043 6f 74 65 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b  ote;.        i++
36044 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
36045 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20         z[j++] = 
36046 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  0;.        break
36047 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
36048 6c 73 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b  lse{.      z[j++
36049 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a  ] = z[i];.    }.
3604a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
3604b 20 69 6e 70 75 74 20 61 7a 49 6e 20 69 73 20 61   input azIn is a
3604c 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64   NULL-terminated
3604d 20 6c 69 73 74 20 6f 66 20 74 6f 6b 65 6e 73 2e   list of tokens.
3604e 20 20 52 65 6d 6f 76 65 20 74 68 65 20 66 69 72    Remove the fir
3604f 73 74 0a 2a 2a 20 74 6f 6b 65 6e 20 61 6e 64 20  st.** token and 
36050 61 6c 6c 20 70 75 6e 63 74 75 61 74 69 6f 6e 20  all punctuation 
36051 74 6f 6b 65 6e 73 2e 20 20 52 65 6d 6f 76 65 20  tokens.  Remove 
36052 74 68 65 20 71 75 6f 74 65 73 20 66 72 6f 6d 0a  the quotes from.
36053 2a 2a 20 61 72 6f 75 6e 64 20 73 74 72 69 6e 67  ** around string
36054 20 6c 69 74 65 72 61 6c 20 74 6f 6b 65 6e 73 2e   literal tokens.
36055 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a  .**.** Example:.
36056 2a 2a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a  **.**     input:
36057 20 20 20 20 20 20 74 6f 6b 65 6e 69 7a 65 20 63        tokenize c
36058 68 69 6e 65 73 65 20 28 20 27 73 69 6d 70 6c 69  hinese ( 'simpli
36059 66 65 64 27 20 2c 20 27 6d 69 78 65 64 27 20 29  fed' , 'mixed' )
3605a 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20  .**     output: 
3605b 20 20 20 20 63 68 69 6e 65 73 65 20 73 69 6d 70      chinese simp
3605c 6c 69 66 65 64 20 6d 69 78 65 64 0a 2a 2a 0a 2a  lifed mixed.**.*
3605d 2a 20 41 6e 6f 74 68 65 72 20 65 78 61 6d 70 6c  * Another exampl
3605e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 69 6e 70  e:.**.**     inp
3605f 75 74 3a 20 20 20 20 20 20 64 65 6c 69 6d 69 74  ut:      delimit
36060 65 72 73 20 28 20 27 5b 27 20 2c 20 27 5d 27 20  ers ( '[' , ']' 
36061 2c 20 27 2e 2e 2e 27 20 29 0a 2a 2a 20 20 20 20  , '...' ).**    
36062 20 6f 75 74 70 75 74 3a 20 20 20 20 20 5b 20 5d   output:     [ ]
36063 20 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76   ....*/.static v
36064 6f 69 64 20 74 6f 6b 65 6e 4c 69 73 74 54 6f 49  oid tokenListToI
36065 64 4c 69 73 74 28 63 68 61 72 20 2a 2a 61 7a 49  dList(char **azI
36066 6e 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  n){.  int i, j;.
36067 20 20 69 66 28 20 61 7a 49 6e 20 29 7b 0a 20 20    if( azIn ){.  
36068 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 2d 31 3b    for(i=0, j=-1;
36069 20 61 7a 49 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a   azIn[i]; i++){.
3606a 20 20 20 20 20 20 69 66 28 20 73 61 66 65 5f 69        if( safe_i
3606b 73 61 6c 6e 75 6d 28 61 7a 49 6e 5b 69 5d 5b 30  salnum(azIn[i][0
3606c 5d 29 20 7c 7c 20 61 7a 49 6e 5b 69 5d 5b 31 5d  ]) || azIn[i][1]
3606d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 65 71 75   ){.        dequ
3606e 6f 74 65 53 74 72 69 6e 67 28 61 7a 49 6e 5b 69  oteString(azIn[i
3606f 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]);.        if( 
36070 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  j>=0 ){.        
36071 20 20 61 7a 49 6e 5b 6a 5d 20 3d 20 61 7a 49 6e    azIn[j] = azIn
36072 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  [i];.        }. 
36073 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20         j++;.    
36074 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 7a    }.    }.    az
36075 49 6e 5b 6a 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d  In[j] = 0;.  }.}
36076 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  .../*.** Find th
36077 65 20 66 69 72 73 74 20 61 6c 70 68 61 6e 75 6d  e first alphanum
36078 65 72 69 63 20 74 6f 6b 65 6e 20 69 6e 20 74 68  eric token in th
36079 65 20 73 74 72 69 6e 67 20 7a 49 6e 2e 20 20 4e  e string zIn.  N
3607a 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 0a 2a 2a  ull-terminate.**
3607b 20 74 68 69 73 20 74 6f 6b 65 6e 2e 20 20 52 65   this token.  Re
3607c 6d 6f 76 65 20 61 6e 79 20 71 75 6f 74 61 74 69  move any quotati
3607d 6f 6e 20 6d 61 72 6b 73 2e 20 20 41 6e 64 20 72  on marks.  And r
3607e 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
3607f 74 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  to.** the result
36080 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
36081 20 2a 66 69 72 73 74 54 6f 6b 65 6e 28 63 68 61   *firstToken(cha
36082 72 20 2a 7a 49 6e 2c 20 63 68 61 72 20 2a 2a 70  r *zIn, char **p
36083 7a 54 61 69 6c 29 7b 0a 20 20 69 6e 74 20 6e 2c  zTail){.  int n,
36084 20 74 74 79 70 65 3b 0a 20 20 77 68 69 6c 65 28   ttype;.  while(
36085 31 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 47  1){.    n = ftsG
36086 65 74 54 6f 6b 65 6e 28 7a 49 6e 2c 20 26 74 74  etToken(zIn, &tt
36087 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 74 74  ype);.    if( tt
36088 79 70 65 3d 3d 54 4f 4b 45 4e 5f 53 50 41 43 45  ype==TOKEN_SPACE
36089 20 29 7b 0a 20 20 20 20 20 20 7a 49 6e 20 2b 3d   ){.      zIn +=
3608a 20 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   n;.    }else if
3608b 28 20 74 74 79 70 65 3d 3d 54 4f 4b 45 4e 5f 45  ( ttype==TOKEN_E
3608c 4f 46 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 54  OF ){.      *pzT
3608d 61 69 6c 20 3d 20 7a 49 6e 3b 0a 20 20 20 20 20  ail = zIn;.     
3608e 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
3608f 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 49 6e 5b  else{.      zIn[
36090 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a 70  n] = 0;.      *p
36091 7a 54 61 69 6c 20 3d 20 26 7a 49 6e 5b 31 5d 3b  zTail = &zIn[1];
36092 0a 20 20 20 20 20 20 64 65 71 75 6f 74 65 53 74  .      dequoteSt
36093 72 69 6e 67 28 7a 49 6e 29 3b 0a 20 20 20 20 20  ring(zIn);.     
36094 20 72 65 74 75 72 6e 20 7a 49 6e 3b 0a 20 20 20   return zIn;.   
36095 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45   }.  }.  /*NOTRE
36096 41 43 48 45 44 2a 2f 0a 7d 0a 0a 2f 2a 20 52 65  ACHED*/.}../* Re
36097 74 75 72 6e 20 74 72 75 65 20 69 66 2e 2e 2e 0a  turn true if....
36098 2a 2a 0a 2a 2a 20 20 20 2a 20 20 73 20 62 65 67  **.**   *  s beg
36099 69 6e 73 20 77 69 74 68 20 74 68 65 20 73 74 72  ins with the str
3609a 69 6e 67 20 74 2c 20 69 67 6e 6f 72 69 6e 67 20  ing t, ignoring 
3609b 63 61 73 65 0a 2a 2a 20 20 20 2a 20 20 73 20 69  case.**   *  s i
3609c 73 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 74 0a  s longer than t.
3609d 2a 2a 20 20 20 2a 20 20 54 68 65 20 66 69 72 73  **   *  The firs
3609e 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 73  t character of s
3609f 20 62 65 79 6f 6e 64 20 74 20 69 73 20 6e 6f 74   beyond t is not
360a0 20 61 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 0a   a alphanumeric.
360a1 2a 2a 20 0a 2a 2a 20 49 67 6e 6f 72 65 20 6c 65  ** .** Ignore le
360a2 61 64 69 6e 67 20 73 70 61 63 65 20 69 6e 20 2a  ading space in *
360a3 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 70 75 74 20  s..**.** To put 
360a4 69 74 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20  it another way, 
360a5 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  return true if t
360a6 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20 6f  he first token o
360a7 66 0a 2a 2a 20 73 5b 5d 20 69 73 20 74 5b 5d 2e  f.** s[] is t[].
360a8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
360a9 74 61 72 74 73 57 69 74 68 28 63 6f 6e 73 74 20  tartsWith(const 
360aa 63 68 61 72 20 2a 73 2c 20 63 6f 6e 73 74 20 63  char *s, const c
360ab 68 61 72 20 2a 74 29 7b 0a 20 20 77 68 69 6c 65  har *t){.  while
360ac 28 20 73 61 66 65 5f 69 73 73 70 61 63 65 28 2a  ( safe_isspace(*
360ad 73 29 20 29 7b 20 73 2b 2b 3b 20 7d 0a 20 20 77  s) ){ s++; }.  w
360ae 68 69 6c 65 28 20 2a 74 20 29 7b 0a 20 20 20 20  hile( *t ){.    
360af 69 66 28 20 73 61 66 65 5f 74 6f 6c 6f 77 65 72  if( safe_tolower
360b0 28 2a 73 2b 2b 29 21 3d 73 61 66 65 5f 74 6f 6c  (*s++)!=safe_tol
360b1 6f 77 65 72 28 2a 74 2b 2b 29 20 29 20 72 65 74  ower(*t++) ) ret
360b2 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 0;.  }.  ret
360b3 75 72 6e 20 2a 73 21 3d 27 5f 27 20 26 26 20 21  urn *s!='_' && !
360b4 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28 2a 73 29  safe_isalnum(*s)
360b5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  ;.}../*.** An in
360b6 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
360b7 74 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73  tructure defines
360b8 20 74 68 65 20 22 73 70 65 63 22 20 6f 66 20 61   the "spec" of a
360b9 0a 2a 2a 20 66 75 6c 6c 20 74 65 78 74 20 69 6e  .** full text in
360ba 64 65 78 2e 20 20 54 68 69 73 20 73 74 72 75 63  dex.  This struc
360bb 74 75 72 65 20 69 73 20 70 6f 70 75 6c 61 74 65  ture is populate
360bc 64 20 62 79 20 70 61 72 73 65 53 70 65 63 0a 2a  d by parseSpec.*
360bd 2a 20 61 6e 64 20 75 73 65 20 62 79 20 66 75 6c  * and use by ful
360be 6c 74 65 78 74 43 6f 6e 6e 65 63 74 20 61 6e 64  ltextConnect and
360bf 20 66 75 6c 6c 74 65 78 74 43 72 65 61 74 65 2e   fulltextCreate.
360c0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
360c1 63 74 20 54 61 62 6c 65 53 70 65 63 20 7b 0a 20  ct TableSpec {. 
360c2 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
360c3 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 67  ;         /* Log
360c4 69 63 61 6c 20 64 61 74 61 62 61 73 65 20 6e 61  ical database na
360c5 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  me */.  const ch
360c6 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
360c7 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
360c8 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20  full-text index 
360c9 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e  */.  int nColumn
360ca 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
360cb 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
360cc 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  ns to be indexed
360cd 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43   */.  char **azC
360ce 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f  olumn;         /
360cf 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 61 6d 65 73  * Original names
360d0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62   of columns to b
360d1 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 63  e indexed */.  c
360d2 68 61 72 20 2a 2a 61 7a 43 6f 6e 74 65 6e 74 43  har **azContentC
360d3 6f 6c 75 6d 6e 3b 20 20 2f 2a 20 43 6f 6c 75 6d  olumn;  /* Colum
360d4 6e 20 6e 61 6d 65 73 20 66 6f 72 20 25 5f 63 6f  n names for %_co
360d5 6e 74 65 6e 74 20 2a 2f 0a 20 20 63 68 61 72 20  ntent */.  char 
360d6 2a 2a 61 7a 54 6f 6b 65 6e 69 7a 65 72 3b 20 20  **azTokenizer;  
360d7 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
360d8 6f 6b 65 6e 69 7a 65 72 20 61 6e 64 20 69 74 73  okenizer and its
360d9 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 7d 20   arguments */.} 
360da 54 61 62 6c 65 53 70 65 63 3b 0a 0a 2f 2a 0a 2a  TableSpec;../*.*
360db 2a 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6f 66  * Reclaim all of
360dc 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65 64   the memory used
360dd 20 62 79 20 61 20 54 61 62 6c 65 53 70 65 63 0a   by a TableSpec.
360de 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
360df 6c 65 61 72 54 61 62 6c 65 53 70 65 63 28 54 61  learTableSpec(Ta
360e0 62 6c 65 53 70 65 63 20 2a 70 29 20 7b 0a 20 20  bleSpec *p) {.  
360e1 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
360e2 61 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c  azColumn);.  sql
360e3 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 43  ite3_free(p->azC
360e4 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20  ontentColumn);. 
360e5 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d   sqlite3_free(p-
360e6 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 7d  >azTokenizer);.}
360e7 0a 0a 2f 2a 20 50 61 72 73 65 20 61 20 43 52 45  ../* Parse a CRE
360e8 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
360e9 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 77 68 69  E statement, whi
360ea 63 68 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68  ch looks like th
360eb 69 73 3a 0a 20 2a 0a 20 2a 20 43 52 45 41 54 45  is:. *. * CREATE
360ec 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 65   VIRTUAL TABLE e
360ed 6d 61 69 6c 0a 20 2a 20 20 20 20 20 20 20 20 55  mail. *        U
360ee 53 49 4e 47 20 66 74 73 33 28 73 75 62 6a 65 63  SING fts3(subjec
360ef 74 2c 20 62 6f 64 79 2c 20 74 6f 6b 65 6e 69 7a  t, body, tokeniz
360f0 65 20 6d 79 74 6f 6b 65 6e 69 7a 65 72 28 6d 79  e mytokenizer(my
360f1 61 72 67 29 29 0a 20 2a 0a 20 2a 20 57 65 20 72  arg)). *. * We r
360f2 65 74 75 72 6e 20 70 61 72 73 65 64 20 69 6e 66  eturn parsed inf
360f3 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 54 61  ormation in a Ta
360f4 62 6c 65 53 70 65 63 20 73 74 72 75 63 74 75 72  bleSpec structur
360f5 65 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 61 74 69  e.. * . */.stati
360f6 63 20 69 6e 74 20 70 61 72 73 65 53 70 65 63 28  c int parseSpec(
360f7 54 61 62 6c 65 53 70 65 63 20 2a 70 53 70 65 63  TableSpec *pSpec
360f8 2c 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73  , int argc, cons
360f9 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72  t char *const*ar
360fa 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  gv,.            
360fb 20 20 20 20 20 20 20 20 20 63 68 61 72 2a 2a 70           char**p
360fc 7a 45 72 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20  zErr){.  int i, 
360fd 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a  n;.  char *z, *z
360fe 44 75 6d 6d 79 3b 0a 20 20 63 68 61 72 20 2a 2a  Dummy;.  char **
360ff 61 7a 41 72 67 3b 0a 20 20 63 6f 6e 73 74 20 63  azArg;.  const c
36100 68 61 72 20 2a 7a 54 6f 6b 65 6e 69 7a 65 72 20  har *zTokenizer 
36101 3d 20 30 3b 20 20 20 20 2f 2a 20 61 72 67 76 5b  = 0;    /* argv[
36102 5d 20 65 6e 74 72 79 20 64 65 73 63 72 69 62 69  ] entry describi
36103 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  ng the tokenizer
36104 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 61   */..  assert( a
36105 72 67 63 3e 3d 33 20 29 3b 0a 20 20 2f 2a 20 43  rgc>=3 );.  /* C
36106 75 72 72 65 6e 74 20 69 6e 74 65 72 66 61 63 65  urrent interface
36107 3a 0a 20 20 2a 2a 20 61 72 67 76 5b 30 5d 20 2d  :.  ** argv[0] -
36108 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a 20 20 2a   module name.  *
36109 2a 20 61 72 67 76 5b 31 5d 20 2d 20 64 61 74 61  * argv[1] - data
3610a 62 61 73 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61  base name.  ** a
3610b 72 67 76 5b 32 5d 20 2d 20 74 61 62 6c 65 20 6e  rgv[2] - table n
3610c 61 6d 65 0a 20 20 2a 2a 20 61 72 67 76 5b 33 2e  ame.  ** argv[3.
3610d 2e 5d 20 2d 20 63 6f 6c 75 6d 6e 73 2c 20 6f 70  .] - columns, op
3610e 74 69 6f 6e 61 6c 6c 79 20 66 6f 6c 6c 6f 77 65  tionally followe
3610f 64 20 62 79 20 74 6f 6b 65 6e 69 7a 65 72 20 73  d by tokenizer s
36110 70 65 63 69 66 69 63 61 74 69 6f 6e 0a 20 20 2a  pecification.  *
36111 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e  *             an
36112 64 20 73 6e 69 70 70 65 74 20 64 65 6c 69 6d 69  d snippet delimi
36113 74 65 72 73 20 73 70 65 63 69 66 69 63 61 74 69  ters specificati
36114 6f 6e 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 4d  on..  */..  /* M
36115 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
36116 65 20 63 6f 6d 70 6c 65 74 65 20 61 72 67 76 5b  e complete argv[
36117 5d 5b 5d 20 61 72 72 61 79 20 69 6e 20 61 20 73  ][] array in a s
36118 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  ingle allocation
36119 2e 0a 20 20 2a 2a 20 54 68 65 20 61 72 67 76 5b  ..  ** The argv[
3611a 5d 5b 5d 20 61 72 72 61 79 20 69 73 20 72 65 61  ][] array is rea
3611b 64 2d 6f 6e 6c 79 20 61 6e 64 20 74 72 61 6e 73  d-only and trans
3611c 69 65 6e 74 2e 20 20 57 65 20 63 61 6e 20 77 72  ient.  We can wr
3611d 69 74 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20  ite to the.  ** 
3611e 63 6f 70 79 20 69 6e 20 6f 72 64 65 72 20 74 6f  copy in order to
3611f 20 6d 6f 64 69 66 79 20 74 68 69 6e 67 73 20 61   modify things a
36120 6e 64 20 74 68 65 20 63 6f 70 79 20 69 73 20 70  nd the copy is p
36121 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a  ersistent..  */.
36122 20 20 43 4c 45 41 52 28 70 53 70 65 63 29 3b 0a    CLEAR(pSpec);.
36123 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 69 3c 61    for(i=n=0; i<a
36124 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e  rgc; i++){.    n
36125 20 2b 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b   += strlen(argv[
36126 69 5d 29 20 2b 20 31 3b 0a 20 20 7d 0a 20 20 61  i]) + 1;.  }.  a
36127 7a 41 72 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zArg = sqlite3_m
36128 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 63 68  alloc( sizeof(ch
36129 61 72 2a 29 2a 61 72 67 63 20 2b 20 6e 20 29 3b  ar*)*argc + n );
3612a 0a 20 20 69 66 28 20 61 7a 41 72 67 3d 3d 30 20  .  if( azArg==0 
3612b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
3612c 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
3612d 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 61 7a    z = (char*)&az
3612e 41 72 67 5b 61 72 67 63 5d 3b 0a 20 20 66 6f 72  Arg[argc];.  for
3612f 28 69 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b  (i=0; i<argc; i+
36130 2b 29 7b 0a 20 20 20 20 61 7a 41 72 67 5b 69 5d  +){.    azArg[i]
36131 20 3d 20 7a 3b 0a 20 20 20 20 73 74 72 63 70 79   = z;.    strcpy
36132 28 7a 2c 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20  (z, argv[i]);.  
36133 20 20 7a 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 29    z += strlen(z)
36134 2b 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 64  +1;.  }..  /* Id
36135 65 6e 74 69 66 79 20 74 68 65 20 63 6f 6c 75 6d  entify the colum
36136 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74 68 65 20  n names and the 
36137 74 6f 6b 65 6e 69 7a 65 72 20 61 6e 64 20 64 65  tokenizer and de
36138 6c 69 6d 69 74 65 72 20 61 72 67 75 6d 65 6e 74  limiter argument
36139 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 61 72  s.  ** in the ar
3613a 67 76 5b 5d 5b 5d 20 61 72 72 61 79 2e 0a 20 20  gv[][] array..  
3613b 2a 2f 0a 20 20 70 53 70 65 63 2d 3e 7a 44 62 20  */.  pSpec->zDb 
3613c 3d 20 61 7a 41 72 67 5b 31 5d 3b 0a 20 20 70 53  = azArg[1];.  pS
3613d 70 65 63 2d 3e 7a 4e 61 6d 65 20 3d 20 61 7a 41  pec->zName = azA
3613e 72 67 5b 32 5d 3b 0a 20 20 70 53 70 65 63 2d 3e  rg[2];.  pSpec->
3613f 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 70  nColumn = 0;.  p
36140 53 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d  Spec->azColumn =
36141 20 61 7a 41 72 67 3b 0a 20 20 7a 54 6f 6b 65 6e   azArg;.  zToken
36142 69 7a 65 72 20 3d 20 22 74 6f 6b 65 6e 69 7a 65  izer = "tokenize
36143 20 73 69 6d 70 6c 65 22 3b 0a 20 20 66 6f 72 28   simple";.  for(
36144 69 3d 33 3b 20 69 3c 61 72 67 63 3b 20 2b 2b 69  i=3; i<argc; ++i
36145 29 7b 0a 20 20 20 20 69 66 28 20 73 74 61 72 74  ){.    if( start
36146 73 57 69 74 68 28 61 7a 41 72 67 5b 69 5d 2c 22  sWith(azArg[i],"
36147 74 6f 6b 65 6e 69 7a 65 22 29 20 29 7b 0a 20 20  tokenize") ){.  
36148 20 20 20 20 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d      zTokenizer =
36149 20 61 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 20 7d   azArg[i];.    }
3614a 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20  else{.      z = 
3614b 61 7a 41 72 67 5b 70 53 70 65 63 2d 3e 6e 43 6f  azArg[pSpec->nCo
3614c 6c 75 6d 6e 5d 20 3d 20 66 69 72 73 74 54 6f 6b  lumn] = firstTok
3614d 65 6e 28 61 7a 41 72 67 5b 69 5d 2c 20 26 7a 44  en(azArg[i], &zD
3614e 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 70 53 70  ummy);.      pSp
3614f 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20  ec->nColumn++;. 
36150 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
36151 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30  Spec->nColumn==0
36152 20 29 7b 0a 20 20 20 20 61 7a 41 72 67 5b 30 5d   ){.    azArg[0]
36153 20 3d 20 22 63 6f 6e 74 65 6e 74 22 3b 0a 20 20   = "content";.  
36154 20 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e    pSpec->nColumn
36155 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a   = 1;.  }..  /*.
36156 20 20 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 74    ** Construct t
36157 68 65 20 6c 69 73 74 20 6f 66 20 63 6f 6e 74 65  he list of conte
36158 6e 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e  nt column names.
36159 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20  .  **.  ** Each 
3615a 63 6f 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e 20 6e  content column n
3615b 61 6d 65 20 77 69 6c 6c 20 62 65 20 6f 66 20 74  ame will be of t
3615c 68 65 20 66 6f 72 6d 20 63 4e 4e 41 41 41 41 0a  he form cNNAAAA.
3615d 20 20 2a 2a 20 77 68 65 72 65 20 4e 4e 20 69 73    ** where NN is
3615e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62   the column numb
3615f 65 72 20 61 6e 64 20 41 41 41 41 20 69 73 20 74  er and AAAA is t
36160 68 65 20 73 61 6e 69 74 69 7a 65 64 0a 20 20 2a  he sanitized.  *
36161 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 20 20  * column name.  
36162 22 73 61 6e 69 74 69 7a 65 64 22 20 6d 65 61 6e  "sanitized" mean
36163 73 20 74 68 61 74 20 73 70 65 63 69 61 6c 20 63  s that special c
36164 68 61 72 61 63 74 65 72 73 20 61 72 65 0a 20 20  haracters are.  
36165 2a 2a 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  ** converted to 
36166 22 5f 22 2e 20 20 54 68 65 20 63 4e 4e 20 70 72  "_".  The cNN pr
36167 65 66 69 78 20 67 75 61 72 61 6e 74 65 65 73 20  efix guarantees 
36168 74 68 61 74 20 61 6c 6c 20 63 6f 6c 75 6d 6e 0a  that all column.
36169 20 20 2a 2a 20 6e 61 6d 65 73 20 61 72 65 20 75    ** names are u
3616a 6e 69 71 75 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  nique..  **.  **
3616b 20 54 68 65 20 41 41 41 41 20 73 75 66 66 69 78   The AAAA suffix
3616c 20 69 73 20 6e 6f 74 20 73 74 72 69 63 74 6c 79   is not strictly
3616d 20 6e 65 63 65 73 73 61 72 79 2e 20 20 49 74 20   necessary.  It 
3616e 69 73 20 69 6e 63 6c 75 64 65 64 0a 20 20 2a 2a  is included.  **
3616f 20 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 6e 69   for the conveni
36170 65 6e 63 65 20 6f 66 20 70 65 6f 70 6c 65 20 77  ence of people w
36171 68 6f 20 6d 69 67 68 74 20 65 78 61 6d 69 6e 65  ho might examine
36172 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 0a 20   the generated. 
36173 20 2a 2a 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61   ** %_content ta
36174 62 6c 65 20 61 6e 64 20 77 6f 6e 64 65 72 20 77  ble and wonder w
36175 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20  hat the columns 
36176 61 72 65 20 75 73 65 64 20 66 6f 72 2e 0a 20 20  are used for..  
36177 2a 2f 0a 20 20 70 53 70 65 63 2d 3e 61 7a 43 6f  */.  pSpec->azCo
36178 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20 73 71  ntentColumn = sq
36179 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 70 53  lite3_malloc( pS
3617a 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 2a 20 73  pec->nColumn * s
3617b 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 20 29 3b  izeof(char *) );
3617c 0a 20 20 69 66 28 20 70 53 70 65 63 2d 3e 61 7a  .  if( pSpec->az
3617d 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3d 3d 30  ContentColumn==0
3617e 20 29 7b 0a 20 20 20 20 63 6c 65 61 72 54 61 62   ){.    clearTab
3617f 6c 65 53 70 65 63 28 70 53 70 65 63 29 3b 0a 20  leSpec(pSpec);. 
36180 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
36181 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 66 6f  _NOMEM;.  }.  fo
36182 72 28 69 3d 30 3b 20 69 3c 70 53 70 65 63 2d 3e  r(i=0; i<pSpec->
36183 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
36184 20 20 20 63 68 61 72 20 2a 70 3b 0a 20 20 20 20     char *p;.    
36185 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74  pSpec->azContent
36186 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 73 71 6c 69  Column[i] = sqli
36187 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 63 25 64  te3_mprintf("c%d
36188 25 73 22 2c 20 69 2c 20 61 7a 41 72 67 5b 69 5d  %s", i, azArg[i]
36189 29 3b 0a 20 20 20 20 66 6f 72 20 28 70 20 3d 20  );.    for (p = 
3618a 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74  pSpec->azContent
3618b 43 6f 6c 75 6d 6e 5b 69 5d 3b 20 2a 70 20 3b 20  Column[i]; *p ; 
3618c 2b 2b 70 29 20 7b 0a 20 20 20 20 20 20 69 66 28  ++p) {.      if(
3618d 20 21 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28 2a   !safe_isalnum(*
3618e 70 29 20 29 20 2a 70 20 3d 20 27 5f 27 3b 0a 20  p) ) *p = '_';. 
3618f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20     }.  }..  /*. 
36190 20 2a 2a 20 50 61 72 73 65 20 74 68 65 20 74 6f   ** Parse the to
36191 6b 65 6e 69 7a 65 72 20 73 70 65 63 69 66 69 63  kenizer specific
36192 61 74 69 6f 6e 20 73 74 72 69 6e 67 2e 0a 20 20  ation string..  
36193 2a 2f 0a 20 20 70 53 70 65 63 2d 3e 61 7a 54 6f  */.  pSpec->azTo
36194 6b 65 6e 69 7a 65 72 20 3d 20 74 6f 6b 65 6e 69  kenizer = tokeni
36195 7a 65 53 74 72 69 6e 67 28 7a 54 6f 6b 65 6e 69  zeString(zTokeni
36196 7a 65 72 2c 20 26 6e 29 3b 0a 20 20 74 6f 6b 65  zer, &n);.  toke
36197 6e 4c 69 73 74 54 6f 49 64 4c 69 73 74 28 70 53  nListToIdList(pS
36198 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72  pec->azTokenizer
36199 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  );..  return SQL
3619a 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3619b 20 47 65 6e 65 72 61 74 65 20 61 20 43 52 45 41   Generate a CREA
3619c 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
3619d 6e 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65  nt that describe
3619e 73 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 0a  s the schema of.
3619f 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ** the virtual t
361a0 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 61 20  able.  Return a 
361a1 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20  pointer to this 
361a2 73 63 68 65 6d 61 20 73 74 72 69 6e 67 2e 0a 2a  schema string..*
361a3 2a 0a 2a 2a 20 53 70 61 63 65 20 69 73 20 6f 62  *.** Space is ob
361a4 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
361a5 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e  te3_mprintf() an
361a6 64 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65  d should be free
361a7 64 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74  d.** using sqlit
361a8 65 33 5f 66 72 65 65 28 29 2e 0a 2a 2f 0a 73 74  e3_free()..*/.st
361a9 61 74 69 63 20 63 68 61 72 20 2a 66 75 6c 6c 74  atic char *fullt
361aa 65 78 74 53 63 68 65 6d 61 28 0a 20 20 69 6e 74  extSchema(.  int
361ab 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20   nColumn,       
361ac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
361ad 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
361ae 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
361af 2a 63 6f 6e 73 74 2a 20 61 7a 43 6f 6c 75 6d 6e  *const* azColumn
361b0 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f  ,  /* List of co
361b1 6c 75 6d 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  lumns */.  const
361b2 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d   char *zTableNam
361b3 65 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  e        /* Name
361b4 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
361b5 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63  .){.  int i;.  c
361b6 68 61 72 20 2a 7a 53 63 68 65 6d 61 2c 20 2a 7a  har *zSchema, *z
361b7 4e 65 78 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68  Next;.  const ch
361b8 61 72 20 2a 7a 53 65 70 20 3d 20 22 28 22 3b 0a  ar *zSep = "(";.
361b9 20 20 7a 53 63 68 65 6d 61 20 3d 20 73 71 6c 69    zSchema = sqli
361ba 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 43 52 45  te3_mprintf("CRE
361bb 41 54 45 20 54 41 42 4c 45 20 78 22 29 3b 0a 20  ATE TABLE x");. 
361bc 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
361bd 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 7a  umn; i++){.    z
361be 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Next = sqlite3_m
361bf 70 72 69 6e 74 66 28 22 25 73 25 73 25 51 22 2c  printf("%s%s%Q",
361c0 20 7a 53 63 68 65 6d 61 2c 20 7a 53 65 70 2c 20   zSchema, zSep, 
361c1 61 7a 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20  azColumn[i]);.  
361c2 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
361c3 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7a 53 63  Schema);.    zSc
361c4 68 65 6d 61 20 3d 20 7a 4e 65 78 74 3b 0a 20 20  hema = zNext;.  
361c5 20 20 7a 53 65 70 20 3d 20 22 2c 22 3b 0a 20 20    zSep = ",";.  
361c6 7d 0a 20 20 7a 4e 65 78 74 20 3d 20 73 71 6c 69  }.  zNext = sqli
361c7 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 2c  te3_mprintf("%s,
361c8 25 51 20 48 49 44 44 45 4e 22 2c 20 7a 53 63 68  %Q HIDDEN", zSch
361c9 65 6d 61 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 29  ema, zTableName)
361ca 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
361cb 28 7a 53 63 68 65 6d 61 29 3b 0a 20 20 7a 53 63  (zSchema);.  zSc
361cc 68 65 6d 61 20 3d 20 7a 4e 65 78 74 3b 0a 20 20  hema = zNext;.  
361cd 7a 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f  zNext = sqlite3_
361ce 6d 70 72 69 6e 74 66 28 22 25 73 2c 64 6f 63 69  mprintf("%s,doci
361cf 64 20 48 49 44 44 45 4e 29 22 2c 20 7a 53 63 68  d HIDDEN)", zSch
361d0 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ema);.  sqlite3_
361d1 66 72 65 65 28 7a 53 63 68 65 6d 61 29 3b 0a 20  free(zSchema);. 
361d2 20 72 65 74 75 72 6e 20 7a 4e 65 78 74 3b 0a 7d   return zNext;.}
361d3 0a 0a 2f 2a 0a 2a 2a 20 42 75 69 6c 64 20 61 20  ../*.** Build a 
361d4 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 74 61 62  new sqlite3_vtab
361d5 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
361d6 77 69 6c 6c 20 64 65 73 63 72 69 62 65 20 74 68  will describe th
361d7 65 0a 2a 2a 20 66 75 6c 6c 74 65 78 74 20 69 6e  e.** fulltext in
361d8 64 65 78 20 64 65 66 69 6e 65 64 20 62 79 20 73  dex defined by s
361d9 70 65 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  pec..*/.static i
361da 6e 74 20 63 6f 6e 73 74 72 75 63 74 56 74 61 62  nt constructVtab
361db 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
361dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
361dd 20 54 68 65 20 53 51 4c 69 74 65 20 64 61 74 61   The SQLite data
361de 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
361df 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70  */.  fts3Hash *p
361e0 48 61 73 68 2c 20 20 20 20 20 20 20 20 20 20 2f  Hash,          /
361e1 2a 20 48 61 73 68 20 74 61 62 6c 65 20 63 6f 6e  * Hash table con
361e2 74 61 69 6e 69 6e 67 20 74 6f 6b 65 6e 69 7a 65  taining tokenize
361e3 72 73 20 2a 2f 0a 20 20 54 61 62 6c 65 53 70 65  rs */.  TableSpe
361e4 63 20 2a 73 70 65 63 2c 20 20 20 20 20 20 20 20  c *spec,        
361e5 20 20 2f 2a 20 50 61 72 73 65 64 20 73 70 65 63    /* Parsed spec
361e6 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
361e7 6d 20 70 61 72 73 65 53 70 65 63 28 29 20 2a 2f  m parseSpec() */
361e8 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
361e9 2a 2a 70 70 56 54 61 62 2c 20 20 20 20 2f 2a 20  **ppVTab,    /* 
361ea 57 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74  Write the result
361eb 69 6e 67 20 76 74 61 62 20 73 74 72 75 63 74 75  ing vtab structu
361ec 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61  re here */.  cha
361ed 72 20 2a 2a 70 7a 45 72 72 20 20 20 20 20 20 20  r **pzErr       
361ee 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
361ef 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67  any error messag
361f0 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  e here */.){.  i
361f1 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 0a  nt rc;.  int n;.
361f2 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
361f3 2a 76 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20  *v = 0;.  const 
361f4 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
361f5 72 5f 6d 6f 64 75 6c 65 20 2a 6d 20 3d 20 4e 55  r_module *m = NU
361f6 4c 4c 3b 0a 20 20 63 68 61 72 20 2a 73 63 68 65  LL;.  char *sche
361f7 6d 61 3b 0a 0a 20 20 63 68 61 72 20 63 6f 6e 73  ma;..  char cons
361f8 74 20 2a 7a 54 6f 6b 3b 20 20 20 20 20 20 20 20  t *zTok;        
361f9 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 6f 6b 65   /* Name of toke
361fa 6e 69 7a 65 72 20 74 6f 20 75 73 65 20 66 6f 72  nizer to use for
361fb 20 74 68 69 73 20 66 74 73 20 74 61 62 6c 65 20   this fts table 
361fc 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 3b 20 20  */.  int nTok;  
361fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
361fe 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 54 6f 6b  * Length of zTok
361ff 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6e 75 6c 20  , including nul 
36200 74 65 72 6d 69 6e 61 74 6f 72 20 2a 2f 0a 0a 20  terminator */.. 
36201 20 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76   v = (fulltext_v
36202 74 61 62 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d  tab *) sqlite3_m
36203 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66 75 6c  alloc(sizeof(ful
36204 6c 74 65 78 74 5f 76 74 61 62 29 29 3b 0a 20 20  ltext_vtab));.  
36205 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72  if( v==0 ) retur
36206 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
36207 20 20 43 4c 45 41 52 28 76 29 3b 0a 20 20 2f 2a    CLEAR(v);.  /*
36208 20 73 71 6c 69 74 65 20 77 69 6c 6c 20 69 6e 69   sqlite will ini
36209 74 69 61 6c 69 7a 65 20 76 2d 3e 62 61 73 65 20  tialize v->base 
3620a 2a 2f 0a 20 20 76 2d 3e 64 62 20 3d 20 64 62 3b  */.  v->db = db;
3620b 0a 20 20 76 2d 3e 7a 44 62 20 3d 20 73 70 65 63  .  v->zDb = spec
3620c 2d 3e 7a 44 62 3b 20 20 20 20 20 20 20 2f 2a 20  ->zDb;       /* 
3620d 46 72 65 65 64 20 77 68 65 6e 20 61 7a 43 6f 6c  Freed when azCol
3620e 75 6d 6e 20 69 73 20 66 72 65 65 64 20 2a 2f 0a  umn is freed */.
3620f 20 20 76 2d 3e 7a 4e 61 6d 65 20 3d 20 73 70 65    v->zName = spe
36210 63 2d 3e 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 46  c->zName;   /* F
36211 72 65 65 64 20 77 68 65 6e 20 61 7a 43 6f 6c 75  reed when azColu
36212 6d 6e 20 69 73 20 66 72 65 65 64 20 2a 2f 0a 20  mn is freed */. 
36213 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 73 70   v->nColumn = sp
36214 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 76  ec->nColumn;.  v
36215 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
36216 6e 20 3d 20 73 70 65 63 2d 3e 61 7a 43 6f 6e 74  n = spec->azCont
36217 65 6e 74 43 6f 6c 75 6d 6e 3b 0a 20 20 73 70 65  entColumn;.  spe
36218 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75  c->azContentColu
36219 6d 6e 20 3d 20 30 3b 0a 20 20 76 2d 3e 61 7a 43  mn = 0;.  v->azC
3621a 6f 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e 61 7a  olumn = spec->az
3621b 43 6f 6c 75 6d 6e 3b 0a 20 20 73 70 65 63 2d 3e  Column;.  spec->
3621c 61 7a 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 0a 20  azColumn = 0;.. 
3621d 20 69 66 28 20 73 70 65 63 2d 3e 61 7a 54 6f 6b   if( spec->azTok
3621e 65 6e 69 7a 65 72 3d 3d 30 20 29 7b 0a 20 20 20  enizer==0 ){.   
3621f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
36220 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 7a 54 6f  OMEM;.  }..  zTo
36221 6b 20 3d 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65  k = spec->azToke
36222 6e 69 7a 65 72 5b 30 5d 3b 20 0a 20 20 69 66 28  nizer[0]; .  if(
36223 20 21 7a 54 6f 6b 20 29 7b 0a 20 20 20 20 7a 54   !zTok ){.    zT
36224 6f 6b 20 3d 20 22 73 69 6d 70 6c 65 22 3b 0a 20  ok = "simple";. 
36225 20 7d 0a 20 20 6e 54 6f 6b 20 3d 20 73 74 72 6c   }.  nTok = strl
36226 65 6e 28 7a 54 6f 6b 29 2b 31 3b 0a 0a 20 20 6d  en(zTok)+1;..  m
36227 20 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65   = (sqlite3_toke
36228 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 29 73  nizer_module *)s
36229 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
3622a 6e 64 28 70 48 61 73 68 2c 20 7a 54 6f 6b 2c 20  nd(pHash, zTok, 
3622b 6e 54 6f 6b 29 3b 0a 20 20 69 66 28 20 21 6d 20  nTok);.  if( !m 
3622c 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  ){.    *pzErr = 
3622d 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
3622e 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a  "unknown tokeniz
3622f 65 72 3a 20 25 73 22 2c 20 73 70 65 63 2d 3e 61  er: %s", spec->a
36230 7a 54 6f 6b 65 6e 69 7a 65 72 5b 30 5d 29 3b 0a  zTokenizer[0]);.
36231 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
36232 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20  ERROR;.    goto 
36233 65 72 72 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28  err;.  }..  for(
36234 6e 3d 30 3b 20 73 70 65 63 2d 3e 61 7a 54 6f 6b  n=0; spec->azTok
36235 65 6e 69 7a 65 72 5b 6e 5d 3b 20 6e 2b 2b 29 7b  enizer[n]; n++){
36236 7d 0a 20 20 69 66 28 20 6e 20 29 7b 0a 20 20 20  }.  if( n ){.   
36237 20 72 63 20 3d 20 6d 2d 3e 78 43 72 65 61 74 65   rc = m->xCreate
36238 28 6e 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68 61  (n-1, (const cha
36239 72 2a 63 6f 6e 73 74 2a 29 26 73 70 65 63 2d 3e  r*const*)&spec->
3623a 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b 31 5d 2c 0a  azTokenizer[1],.
3623b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3623c 20 20 20 20 26 76 2d 3e 70 54 6f 6b 65 6e 69 7a      &v->pTokeniz
3623d 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  er);.  }else{.  
3623e 20 20 72 63 20 3d 20 6d 2d 3e 78 43 72 65 61 74    rc = m->xCreat
3623f 65 28 30 2c 20 30 2c 20 26 76 2d 3e 70 54 6f 6b  e(0, 0, &v->pTok
36240 65 6e 69 7a 65 72 29 3b 0a 20 20 7d 0a 20 20 69  enizer);.  }.  i
36241 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
36242 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 76   ) goto err;.  v
36243 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d  ->pTokenizer->pM
36244 6f 64 75 6c 65 20 3d 20 6d 3b 0a 0a 20 20 2f 2a  odule = m;..  /*
36245 20 54 4f 44 4f 3a 20 76 65 72 69 66 79 20 74 68   TODO: verify th
36246 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 62  e existence of b
36247 61 63 6b 69 6e 67 20 74 61 62 6c 65 73 20 66 6f  acking tables fo
36248 6f 5f 63 6f 6e 74 65 6e 74 2c 20 66 6f 6f 5f 74  o_content, foo_t
36249 65 72 6d 20 2a 2f 0a 0a 20 20 73 63 68 65 6d 61  erm */..  schema
3624a 20 3d 20 66 75 6c 6c 74 65 78 74 53 63 68 65 6d   = fulltextSchem
3624b 61 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 28 63  a(v->nColumn, (c
3624c 6f 6e 73 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a  onst char*const*
3624d 29 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 2c 0a 20 20  )v->azColumn,.  
3624e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3624f 20 20 20 20 20 20 20 20 73 70 65 63 2d 3e 7a 4e          spec->zN
36250 61 6d 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  ame);.  rc = sql
36251 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
36252 62 28 64 62 2c 20 73 63 68 65 6d 61 29 3b 0a 20  b(db, schema);. 
36253 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73 63   sqlite3_free(sc
36254 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 72 63 21  hema);.  if( rc!
36255 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
36256 6f 20 65 72 72 3b 0a 0a 20 20 6d 65 6d 73 65 74  o err;..  memset
36257 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61  (v->pFulltextSta
36258 74 65 6d 65 6e 74 73 2c 20 30 2c 20 73 69 7a 65  tements, 0, size
36259 6f 66 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53  of(v->pFulltextS
3625a 74 61 74 65 6d 65 6e 74 73 29 29 3b 0a 0a 20 20  tatements));..  
3625b 2f 2a 20 49 6e 64 69 63 61 74 65 20 74 68 61 74  /* Indicate that
3625c 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20 6e   the buffer is n
3625d 6f 74 20 6c 69 76 65 2e 20 2a 2f 0a 20 20 76 2d  ot live. */.  v-
3625e 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 3d 20  >nPendingData = 
3625f 2d 31 3b 0a 0a 20 20 2a 70 70 56 54 61 62 20 3d  -1;..  *ppVTab =
36260 20 26 76 2d 3e 62 61 73 65 3b 0a 20 20 46 54 53   &v->base;.  FTS
36261 54 52 41 43 45 28 28 22 46 54 53 33 20 43 6f 6e  TRACE(("FTS3 Con
36262 6e 65 63 74 20 25 70 5c 6e 22 2c 20 76 29 29 3b  nect %p\n", v));
36263 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  ..  return rc;..
36264 65 72 72 3a 0a 20 20 66 75 6c 6c 74 65 78 74 5f  err:.  fulltext_
36265 76 74 61 62 5f 64 65 73 74 72 6f 79 28 76 29 3b  vtab_destroy(v);
36266 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
36267 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
36268 74 65 78 74 43 6f 6e 6e 65 63 74 28 0a 20 20 73  textConnect(.  s
36269 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f  qlite3 *db,.  vo
3626a 69 64 20 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20  id *pAux,.  int 
3626b 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
3626c 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20   *const*argv,.  
3626d 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
3626e 70 56 54 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a  pVTab,.  char **
3626f 70 7a 45 72 72 0a 29 7b 0a 20 20 54 61 62 6c 65  pzErr.){.  Table
36270 53 70 65 63 20 73 70 65 63 3b 0a 20 20 69 6e 74  Spec spec;.  int
36271 20 72 63 20 3d 20 70 61 72 73 65 53 70 65 63 28   rc = parseSpec(
36272 26 73 70 65 63 2c 20 61 72 67 63 2c 20 61 72 67  &spec, argc, arg
36273 76 2c 20 70 7a 45 72 72 29 3b 0a 20 20 69 66 28  v, pzErr);.  if(
36274 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
36275 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
36276 63 20 3d 20 63 6f 6e 73 74 72 75 63 74 56 74 61  c = constructVta
36277 62 28 64 62 2c 20 28 66 74 73 33 48 61 73 68 20  b(db, (fts3Hash 
36278 2a 29 70 41 75 78 2c 20 26 73 70 65 63 2c 20 70  *)pAux, &spec, p
36279 70 56 54 61 62 2c 20 70 7a 45 72 72 29 3b 0a 20  pVTab, pzErr);. 
3627a 20 63 6c 65 61 72 54 61 62 6c 65 53 70 65 63 28   clearTableSpec(
3627b 26 73 70 65 63 29 3b 0a 20 20 72 65 74 75 72 6e  &spec);.  return
3627c 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 25   rc;.}../* The %
3627d 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 68  _content table h
3627e 6f 6c 64 73 20 74 68 65 20 74 65 78 74 20 6f 66  olds the text of
3627f 20 65 61 63 68 20 64 6f 63 75 6d 65 6e 74 2c 20   each document, 
36280 77 69 74 68 0a 2a 2a 20 74 68 65 20 64 6f 63 69  with.** the doci
36281 64 20 63 6f 6c 75 6d 6e 20 65 78 70 6f 73 65 64  d column exposed
36282 20 61 73 20 74 68 65 20 53 51 4c 69 74 65 20 72   as the SQLite r
36283 6f 77 69 64 20 66 6f 72 20 74 68 65 20 74 61 62  owid for the tab
36284 6c 65 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  le..*/./* TODO(s
36285 68 65 73 73 29 20 54 68 69 73 20 63 6f 6d 6d 65  hess) This comme
36286 6e 74 20 6e 65 65 64 73 20 65 6c 61 62 6f 72 61  nt needs elabora
36287 74 69 6f 6e 20 74 6f 20 6d 61 74 63 68 20 74 68  tion to match th
36288 65 20 75 70 64 61 74 65 64 0a 2a 2a 20 63 6f 64  e updated.** cod
36289 65 2e 20 20 57 6f 72 6b 20 69 74 20 69 6e 74 6f  e.  Work it into
3628a 20 74 68 65 20 74 6f 70 2d 6f 66 2d 66 69 6c 65   the top-of-file
3628b 20 63 6f 6d 6d 65 6e 74 20 61 74 20 74 68 61 74   comment at that
3628c 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   time..*/.static
3628d 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 72 65   int fulltextCre
3628e 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ate(sqlite3 *db,
3628f 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20   void *pAux,.   
36290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36291 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c         int argc,
36292 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f   const char * co
36293 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20 20 20 20  nst *argv,.     
36294 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36295 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
36296 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72  b **ppVTab, char
36297 20 2a 2a 70 7a 45 72 72 29 7b 0a 20 20 69 6e 74   **pzErr){.  int
36298 20 72 63 3b 0a 20 20 54 61 62 6c 65 53 70 65 63   rc;.  TableSpec
36299 20 73 70 65 63 3b 0a 20 20 53 74 72 69 6e 67 42   spec;.  StringB
3629a 75 66 66 65 72 20 73 63 68 65 6d 61 3b 0a 20 20  uffer schema;.  
3629b 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
3629c 43 72 65 61 74 65 5c 6e 22 29 29 3b 0a 0a 20 20  Create\n"));..  
3629d 72 63 20 3d 20 70 61 72 73 65 53 70 65 63 28 26  rc = parseSpec(&
3629e 73 70 65 63 2c 20 61 72 67 63 2c 20 61 72 67 76  spec, argc, argv
3629f 2c 20 70 7a 45 72 72 29 3b 0a 20 20 69 66 28 20  , pzErr);.  if( 
362a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
362a1 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 6e  return rc;..  in
362a2 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26  itStringBuffer(&
362a3 73 63 68 65 6d 61 29 3b 0a 20 20 61 70 70 65 6e  schema);.  appen
362a4 64 28 26 73 63 68 65 6d 61 2c 20 22 43 52 45 41  d(&schema, "CREA
362a5 54 45 20 54 41 42 4c 45 20 25 5f 63 6f 6e 74 65  TE TABLE %_conte
362a6 6e 74 28 22 29 3b 0a 20 20 61 70 70 65 6e 64 28  nt(");.  append(
362a7 26 73 63 68 65 6d 61 2c 20 22 20 20 64 6f 63 69  &schema, "  doci
362a8 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  d INTEGER PRIMAR
362a9 59 20 4b 45 59 2c 22 29 3b 0a 20 20 61 70 70 65  Y KEY,");.  appe
362aa 6e 64 4c 69 73 74 28 26 73 63 68 65 6d 61 2c 20  ndList(&schema, 
362ab 73 70 65 63 2e 6e 43 6f 6c 75 6d 6e 2c 20 73 70  spec.nColumn, sp
362ac 65 63 2e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75  ec.azContentColu
362ad 6d 6e 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73  mn);.  append(&s
362ae 63 68 65 6d 61 2c 20 22 29 22 29 3b 0a 20 20 72  chema, ")");.  r
362af 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 64 62 2c  c = sql_exec(db,
362b0 20 73 70 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e   spec.zDb, spec.
362b1 7a 4e 61 6d 65 2c 20 73 74 72 69 6e 67 42 75 66  zName, stringBuf
362b2 66 65 72 44 61 74 61 28 26 73 63 68 65 6d 61 29  ferData(&schema)
362b3 29 3b 0a 20 20 73 74 72 69 6e 67 42 75 66 66 65  );.  stringBuffe
362b4 72 44 65 73 74 72 6f 79 28 26 73 63 68 65 6d 61  rDestroy(&schema
362b5 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
362b6 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f 75  ITE_OK ) goto ou
362b7 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65  t;..  rc = sql_e
362b8 78 65 63 28 64 62 2c 20 73 70 65 63 2e 7a 44 62  xec(db, spec.zDb
362b9 2c 20 73 70 65 63 2e 7a 4e 61 6d 65 2c 0a 20 20  , spec.zName,.  
362ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63                "c
362bb 72 65 61 74 65 20 74 61 62 6c 65 20 25 5f 73 65  reate table %_se
362bc 67 6d 65 6e 74 73 28 22 0a 20 20 20 20 20 20 20  gments(".       
362bd 20 20 20 20 20 20 20 20 20 22 20 20 62 6c 6f 63           "  bloc
362be 6b 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d  kid INTEGER PRIM
362bf 41 52 59 20 4b 45 59 2c 22 0a 20 20 20 20 20 20  ARY KEY,".      
362c0 20 20 20 20 20 20 20 20 20 20 22 20 20 62 6c 6f            "  blo
362c1 63 6b 20 62 6c 6f 62 22 0a 20 20 20 20 20 20 20  ck blob".       
362c2 20 20 20 20 20 20 20 20 20 22 29 3b 22 0a 20 20           ");".  
362c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 3b                );
362c4 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
362c5 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b  E_OK ) goto out;
362c6 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78 65  ..  rc = sql_exe
362c7 63 28 64 62 2c 20 73 70 65 63 2e 7a 44 62 2c 20  c(db, spec.zDb, 
362c8 73 70 65 63 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20  spec.zName,.    
362c9 20 20 20 20 20 20 20 20 20 20 20 20 22 63 72 65              "cre
362ca 61 74 65 20 74 61 62 6c 65 20 25 5f 73 65 67 64  ate table %_segd
362cb 69 72 28 22 0a 20 20 20 20 20 20 20 20 20 20 20  ir(".           
362cc 20 20 20 20 20 22 20 20 6c 65 76 65 6c 20 69 6e       "  level in
362cd 74 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20  teger,".        
362ce 20 20 20 20 20 20 20 20 22 20 20 69 64 78 20 69          "  idx i
362cf 6e 74 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20  nteger,".       
362d0 20 20 20 20 20 20 20 20 20 22 20 20 73 74 61 72           "  star
362d1 74 5f 62 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c  t_block integer,
362d2 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
362d3 20 20 22 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f    "  leaves_end_
362d4 62 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a  block integer,".
362d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362d6 22 20 20 65 6e 64 5f 62 6c 6f 63 6b 20 69 6e 74  "  end_block int
362d7 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20  eger,".         
362d8 20 20 20 20 20 20 20 22 20 20 72 6f 6f 74 20 62         "  root b
362d9 6c 6f 62 2c 22 0a 20 20 20 20 20 20 20 20 20 20  lob,".          
362da 20 20 20 20 20 20 22 20 20 70 72 69 6d 61 72 79        "  primary
362db 20 6b 65 79 28 6c 65 76 65 6c 2c 20 69 64 78 29   key(level, idx)
362dc 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
362dd 20 20 22 29 3b 22 29 3b 0a 20 20 69 66 28 20 72    ");");.  if( r
362de 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
362df 6f 74 6f 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d  oto out;..  rc =
362e0 20 63 6f 6e 73 74 72 75 63 74 56 74 61 62 28 64   constructVtab(d
362e1 62 2c 20 28 66 74 73 33 48 61 73 68 20 2a 29 70  b, (fts3Hash *)p
362e2 41 75 78 2c 20 26 73 70 65 63 2c 20 70 70 56 54  Aux, &spec, ppVT
362e3 61 62 2c 20 70 7a 45 72 72 29 3b 0a 0a 6f 75 74  ab, pzErr);..out
362e4 3a 0a 20 20 63 6c 65 61 72 54 61 62 6c 65 53 70  :.  clearTableSp
362e5 65 63 28 26 73 70 65 63 29 3b 0a 20 20 72 65 74  ec(&spec);.  ret
362e6 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 44 65  urn rc;.}../* De
362e7 63 69 64 65 20 68 6f 77 20 74 6f 20 68 61 6e 64  cide how to hand
362e8 6c 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2e  le an SQL query.
362e9 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66   */.static int f
362ea 75 6c 6c 74 65 78 74 42 65 73 74 49 6e 64 65 78  ulltextBestIndex
362eb 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
362ec 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e  VTab, sqlite3_in
362ed 64 65 78 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 29  dex_info *pInfo)
362ee 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  {.  fulltext_vta
362ef 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74  b *v = (fulltext
362f0 5f 76 74 61 62 20 2a 29 70 56 54 61 62 3b 0a 20  _vtab *)pVTab;. 
362f1 20 69 6e 74 20 69 3b 0a 20 20 46 54 53 54 52 41   int i;.  FTSTRA
362f2 43 45 28 28 22 46 54 53 33 20 42 65 73 74 49 6e  CE(("FTS3 BestIn
362f3 64 65 78 5c 6e 22 29 29 3b 0a 0a 20 20 66 6f 72  dex\n"));..  for
362f4 28 69 3d 30 3b 20 69 3c 70 49 6e 66 6f 2d 3e 6e  (i=0; i<pInfo->n
362f5 43 6f 6e 73 74 72 61 69 6e 74 3b 20 2b 2b 69 29  Constraint; ++i)
362f6 7b 0a 20 20 20 20 63 6f 6e 73 74 20 73 74 72 75  {.    const stru
362f7 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
362f8 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 43 6f  _constraint *pCo
362f9 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 70 43  nstraint;.    pC
362fa 6f 6e 73 74 72 61 69 6e 74 20 3d 20 26 70 49 6e  onstraint = &pIn
362fb 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  fo->aConstraint[
362fc 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6e  i];.    if( pCon
362fd 73 74 72 61 69 6e 74 2d 3e 75 73 61 62 6c 65 20  straint->usable 
362fe 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70  ) {.      if( (p
362ff 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c  Constraint->iCol
36300 75 6d 6e 3d 3d 2d 31 20 7c 7c 20 70 43 6f 6e 73  umn==-1 || pCons
36301 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3d  traint->iColumn=
36302 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 20 26  =v->nColumn+1) &
36303 26 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6e  &.          pCon
36304 73 74 72 61 69 6e 74 2d 3e 6f 70 3d 3d 53 51 4c  straint->op==SQL
36305 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
36306 41 49 4e 54 5f 45 51 20 29 7b 0a 20 20 20 20 20  AINT_EQ ){.     
36307 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d     pInfo->idxNum
36308 20 3d 20 51 55 45 52 59 5f 44 4f 43 49 44 3b 20   = QUERY_DOCID; 
36309 20 20 20 20 20 2f 2a 20 6c 6f 6f 6b 75 70 20 62       /* lookup b
3630a 79 20 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 20  y docid */.     
3630b 20 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54     FTSTRACE(("FT
3630c 53 33 20 51 55 45 52 59 5f 44 4f 43 49 44 5c 6e  S3 QUERY_DOCID\n
3630d 22 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73  "));.      } els
3630e 65 20 69 66 28 20 70 43 6f 6e 73 74 72 61 69 6e  e if( pConstrain
3630f 74 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26  t->iColumn>=0 &&
36310 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43   pConstraint->iC
36311 6f 6c 75 6d 6e 3c 3d 76 2d 3e 6e 43 6f 6c 75 6d  olumn<=v->nColum
36312 6e 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  n &&.           
36313 20 20 20 20 20 20 70 43 6f 6e 73 74 72 61 69 6e        pConstrain
36314 74 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e  t->op==SQLITE_IN
36315 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d  DEX_CONSTRAINT_M
36316 41 54 43 48 20 29 7b 0a 20 20 20 20 20 20 20 20  ATCH ){.        
36317 2f 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61  /* full-text sea
36318 72 63 68 20 2a 2f 0a 20 20 20 20 20 20 20 20 70  rch */.        p
36319 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51  Info->idxNum = Q
3631a 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 2b 20  UERY_FULLTEXT + 
3631b 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f  pConstraint->iCo
3631c 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 46 54  lumn;.        FT
3631d 53 54 52 41 43 45 28 28 22 46 54 53 33 20 51 55  STRACE(("FTS3 QU
3631e 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 25 64 5c  ERY_FULLTEXT %d\
3631f 6e 22 2c 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d  n", pConstraint-
36320 3e 69 43 6f 6c 75 6d 6e 29 29 3b 0a 20 20 20 20  >iColumn));.    
36321 20 20 7d 20 65 6c 73 65 20 63 6f 6e 74 69 6e 75    } else continu
36322 65 3b 0a 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d  e;..      pInfo-
36323 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67  >aConstraintUsag
36324 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d  e[i].argvIndex =
36325 20 31 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d   1;.      pInfo-
36326 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67  >aConstraintUsag
36327 65 5b 69 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 0a  e[i].omit = 1;..
36328 20 20 20 20 20 20 2f 2a 20 41 6e 20 61 72 62 69        /* An arbi
36329 74 72 61 72 79 20 76 61 6c 75 65 20 66 6f 72 20  trary value for 
3632a 6e 6f 77 2e 0a 20 20 20 20 20 20 20 2a 20 54 4f  now..       * TO
3632b 44 4f 3a 20 50 65 72 68 61 70 73 20 64 6f 63 69  DO: Perhaps doci
3632c 64 20 6d 61 74 63 68 65 73 20 73 68 6f 75 6c 64  d matches should
3632d 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64 20 63   be considered c
3632e 68 65 61 70 65 72 20 74 68 61 6e 0a 20 20 20 20  heaper than.    
3632f 20 20 20 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73     * full-text s
36330 65 61 72 63 68 65 73 2e 20 2a 2f 0a 20 20 20 20  earches. */.    
36331 20 20 70 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74    pInfo->estimat
36332 65 64 43 6f 73 74 20 3d 20 31 2e 30 3b 20 20 20  edCost = 1.0;   
36333 0a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
36334 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
36335 20 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 69 64 78    }.  pInfo->idx
36336 4e 75 6d 20 3d 20 51 55 45 52 59 5f 47 45 4e 45  Num = QUERY_GENE
36337 52 49 43 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  RIC;.  return SQ
36338 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74  LITE_OK;.}..stat
36339 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 44  ic int fulltextD
3633a 69 73 63 6f 6e 6e 65 63 74 28 73 71 6c 69 74 65  isconnect(sqlite
3633b 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 7b 0a  3_vtab *pVTab){.
3633c 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
3633d 33 20 44 69 73 63 6f 6e 6e 65 63 74 20 25 70 5c  3 Disconnect %p\
3633e 6e 22 2c 20 70 56 54 61 62 29 29 3b 0a 20 20 66  n", pVTab));.  f
3633f 75 6c 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73  ulltext_vtab_des
36340 74 72 6f 79 28 28 66 75 6c 6c 74 65 78 74 5f 76  troy((fulltext_v
36341 74 61 62 20 2a 29 70 56 54 61 62 29 3b 0a 20 20  tab *)pVTab);.  
36342 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
36343 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
36344 66 75 6c 6c 74 65 78 74 44 65 73 74 72 6f 79 28  fulltextDestroy(
36345 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
36346 54 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  Tab){.  fulltext
36347 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c  _vtab *v = (full
36348 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56 54 61  text_vtab *)pVTa
36349 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  b;.  int rc;..  
3634a 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
3634b 44 65 73 74 72 6f 79 20 25 70 5c 6e 22 2c 20 70  Destroy %p\n", p
3634c 56 54 61 62 29 29 3b 0a 20 20 72 63 20 3d 20 73  VTab));.  rc = s
3634d 71 6c 5f 65 78 65 63 28 76 2d 3e 64 62 2c 20 76  ql_exec(v->db, v
3634e 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c  ->zDb, v->zName,
3634f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36350 20 22 64 72 6f 70 20 74 61 62 6c 65 20 69 66 20   "drop table if 
36351 65 78 69 73 74 73 20 25 5f 63 6f 6e 74 65 6e 74  exists %_content
36352 3b 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ;".             
36353 20 20 20 22 64 72 6f 70 20 74 61 62 6c 65 20 69     "drop table i
36354 66 20 65 78 69 73 74 73 20 25 5f 73 65 67 6d 65  f exists %_segme
36355 6e 74 73 3b 22 0a 20 20 20 20 20 20 20 20 20 20  nts;".          
36356 20 20 20 20 20 20 22 64 72 6f 70 20 74 61 62 6c        "drop tabl
36357 65 20 69 66 20 65 78 69 73 74 73 20 25 5f 73 65  e if exists %_se
36358 67 64 69 72 3b 22 0a 20 20 20 20 20 20 20 20 20  gdir;".         
36359 20 20 20 20 20 20 20 29 3b 0a 20 20 69 66 28 20         );.  if( 
3635a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3635b 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 66 75  return rc;..  fu
3635c 6c 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74  lltext_vtab_dest
3635d 72 6f 79 28 28 66 75 6c 6c 74 65 78 74 5f 76 74  roy((fulltext_vt
3635e 61 62 20 2a 29 70 56 54 61 62 29 3b 0a 20 20 72  ab *)pVTab);.  r
3635f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
36360 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .}..static int f
36361 75 6c 6c 74 65 78 74 4f 70 65 6e 28 73 71 6c 69  ulltextOpen(sqli
36362 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c  te3_vtab *pVTab,
36363 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
36364 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29  rsor **ppCursor)
36365 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72  {.  fulltext_cur
36366 73 6f 72 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28  sor *c;..  c = (
36367 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
36368 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
36369 63 28 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65 78  c(sizeof(fulltex
3636a 74 5f 63 75 72 73 6f 72 29 29 3b 0a 20 20 69 66  t_cursor));.  if
3636b 28 20 63 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( c ){.    memse
3636c 74 28 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66  t(c, 0, sizeof(f
3636d 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 29 29  ulltext_cursor))
3636e 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 20  ;.    /* sqlite 
3636f 77 69 6c 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20  will initialize 
36370 63 2d 3e 62 61 73 65 20 2a 2f 0a 20 20 20 20 2a  c->base */.    *
36371 70 70 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62  ppCursor = &c->b
36372 61 73 65 3b 0a 20 20 20 20 46 54 53 54 52 41 43  ase;.    FTSTRAC
36373 45 28 28 22 46 54 53 33 20 4f 70 65 6e 20 25 70  E(("FTS3 Open %p
36374 3a 20 25 70 5c 6e 22 2c 20 70 56 54 61 62 2c 20  : %p\n", pVTab, 
36375 63 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  c));.    return 
36376 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
36377 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  se{.    return S
36378 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
36379 0a 7d 0a 0a 2f 2a 20 46 72 65 65 20 61 6c 6c 20  .}../* Free all 
3637a 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 61 6c  of the dynamical
3637b 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d  ly allocated mem
3637c 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 0a  ory held by the.
3637d 2a 2a 20 53 6e 69 70 70 65 74 0a 2a 2f 0a 73 74  ** Snippet.*/.st
3637e 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65  atic void snippe
3637f 74 43 6c 65 61 72 28 53 6e 69 70 70 65 74 20 2a  tClear(Snippet *
36380 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  p){.  sqlite3_fr
36381 65 65 28 70 2d 3e 61 4d 61 74 63 68 29 3b 0a 20  ee(p->aMatch);. 
36382 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d   sqlite3_free(p-
36383 3e 7a 4f 66 66 73 65 74 29 3b 0a 20 20 73 71 6c  >zOffset);.  sql
36384 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 53 6e  ite3_free(p->zSn
36385 69 70 70 65 74 29 3b 0a 20 20 43 4c 45 41 52 28  ippet);.  CLEAR(
36386 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70  p);.}../*.** App
36387 65 6e 64 20 61 20 73 69 6e 67 6c 65 20 65 6e 74  end a single ent
36388 72 79 20 74 6f 20 74 68 65 20 70 2d 3e 61 4d 61  ry to the p->aMa
36389 74 63 68 5b 5d 20 6c 6f 67 2e 0a 2a 2f 0a 73 74  tch[] log..*/.st
3638a 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65  atic void snippe
3638b 74 41 70 70 65 6e 64 4d 61 74 63 68 28 0a 20 20  tAppendMatch(.  
3638c 53 6e 69 70 70 65 74 20 2a 70 2c 20 20 20 20 20  Snippet *p,     
3638d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 70 70            /* App
3638e 65 6e 64 20 74 68 65 20 65 6e 74 72 79 20 74 6f  end the entry to
3638f 20 74 68 69 73 20 73 6e 69 70 70 65 74 20 2a 2f   this snippet */
36390 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 69 6e 74  .  int iCol, int
36391 20 69 54 65 72 6d 2c 20 20 20 20 20 20 2f 2a 20   iTerm,      /* 
36392 54 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 71  The column and q
36393 75 65 72 79 20 74 65 72 6d 20 2a 2f 0a 20 20 69  uery term */.  i
36394 6e 74 20 69 54 6f 6b 65 6e 2c 20 20 20 20 20 20  nt iToken,      
36395 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 74 63           /* Matc
36396 68 69 6e 67 20 74 6f 6b 65 6e 20 69 6e 20 64 6f  hing token in do
36397 63 75 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  cument */.  int 
36398 69 53 74 61 72 74 2c 20 69 6e 74 20 6e 42 79 74  iStart, int nByt
36399 65 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20  e     /* Offset 
3639a 61 6e 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20  and size of the 
3639b 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e  match */.){.  in
3639c 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 73 6e  t i;.  struct sn
3639d 69 70 70 65 74 4d 61 74 63 68 20 2a 70 4d 61 74  ippetMatch *pMat
3639e 63 68 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4d 61  ch;.  if( p->nMa
3639f 74 63 68 2b 31 3e 3d 70 2d 3e 6e 41 6c 6c 6f 63  tch+1>=p->nAlloc
363a0 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f   ){.    p->nAllo
363a1 63 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20  c = p->nAlloc*2 
363a2 2b 20 31 30 3b 0a 20 20 20 20 70 2d 3e 61 4d 61  + 10;.    p->aMa
363a3 74 63 68 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  tch = sqlite3_re
363a4 61 6c 6c 6f 63 28 70 2d 3e 61 4d 61 74 63 68 2c  alloc(p->aMatch,
363a5 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 73 69 7a 65 6f   p->nAlloc*sizeo
363a6 66 28 70 2d 3e 61 4d 61 74 63 68 5b 30 5d 29 20  f(p->aMatch[0]) 
363a7 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 4d  );.    if( p->aM
363a8 61 74 63 68 3d 3d 30 20 29 7b 0a 20 20 20 20 20  atch==0 ){.     
363a9 20 70 2d 3e 6e 4d 61 74 63 68 20 3d 20 30 3b 0a   p->nMatch = 0;.
363aa 20 20 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20        p->nAlloc 
363ab 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 0;.      retur
363ac 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  n;.    }.  }.  i
363ad 20 3d 20 70 2d 3e 6e 4d 61 74 63 68 2b 2b 3b 0a   = p->nMatch++;.
363ae 20 20 70 4d 61 74 63 68 20 3d 20 26 70 2d 3e 61    pMatch = &p->a
363af 4d 61 74 63 68 5b 69 5d 3b 0a 20 20 70 4d 61 74  Match[i];.  pMat
363b0 63 68 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b  ch->iCol = iCol;
363b1 0a 20 20 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d  .  pMatch->iTerm
363b2 20 3d 20 69 54 65 72 6d 3b 0a 20 20 70 4d 61 74   = iTerm;.  pMat
363b3 63 68 2d 3e 69 54 6f 6b 65 6e 20 3d 20 69 54 6f  ch->iToken = iTo
363b4 6b 65 6e 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69  ken;.  pMatch->i
363b5 53 74 61 72 74 20 3d 20 69 53 74 61 72 74 3b 0a  Start = iStart;.
363b6 20 20 70 4d 61 74 63 68 2d 3e 6e 42 79 74 65 20    pMatch->nByte 
363b7 3d 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  = nByte;.}../*.*
363b8 2a 20 53 69 7a 69 6e 67 20 69 6e 66 6f 72 6d 61  * Sizing informa
363b9 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 63 69 72  tion for the cir
363ba 63 75 6c 61 72 20 62 75 66 66 65 72 20 75 73 65  cular buffer use
363bb 64 20 69 6e 20 73 6e 69 70 70 65 74 4f 66 66 73  d in snippetOffs
363bc 65 74 73 4f 66 43 6f 6c 75 6d 6e 28 29 0a 2a 2f  etsOfColumn().*/
363bd 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 52 4f  .#define FTS3_RO
363be 54 4f 52 5f 53 5a 20 20 20 28 33 32 29 0a 23 64  TOR_SZ   (32).#d
363bf 65 66 69 6e 65 20 46 54 53 33 5f 52 4f 54 4f 52  efine FTS3_ROTOR
363c0 5f 4d 41 53 4b 20 28 46 54 53 33 5f 52 4f 54 4f  _MASK (FTS3_ROTO
363c1 52 5f 53 5a 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 46  R_SZ-1)../*.** F
363c2 75 6e 63 74 69 6f 6e 20 74 6f 20 69 74 65 72 61  unction to itera
363c3 74 65 20 74 68 72 6f 75 67 68 20 74 68 65 20 74  te through the t
363c4 6f 6b 65 6e 73 20 6f 66 20 61 20 63 6f 6d 70 69  okens of a compi
363c5 6c 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a  led expression..
363c6 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20 73 6b  **.** Except, sk
363c7 69 70 20 61 6c 6c 20 74 6f 6b 65 6e 73 20 6f 6e  ip all tokens on
363c8 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   the right-hand 
363c9 73 69 64 65 20 6f 66 20 61 20 4e 4f 54 20 6f 70  side of a NOT op
363ca 65 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20  erator..** This 
363cb 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
363cc 20 74 6f 20 66 69 6e 64 20 74 6f 6b 65 6e 73 20   to find tokens 
363cd 61 73 20 70 61 72 74 20 6f 66 20 73 6e 69 70 70  as part of snipp
363ce 65 74 20 61 6e 64 20 6f 66 66 73 65 74 0a 2a 2a  et and offset.**
363cf 20 67 65 6e 65 72 61 74 69 6f 6e 20 61 6e 64 20   generation and 
363d0 77 65 20 64 6f 20 6e 74 20 77 61 6e 74 20 73 6e  we do nt want sn
363d1 69 70 70 65 74 73 20 61 6e 64 20 6f 66 66 73 65  ippets and offse
363d2 74 73 20 74 6f 20 72 65 70 6f 72 74 20 6d 61 74  ts to report mat
363d3 63 68 65 73 0a 2a 2a 20 66 6f 72 20 74 6f 6b 65  ches.** for toke
363d4 6e 73 20 6f 6e 20 74 68 65 20 52 48 53 20 6f 66  ns on the RHS of
363d5 20 61 20 4e 4f 54 2e 0a 2a 2f 0a 73 74 61 74 69   a NOT..*/.stati
363d6 63 20 69 6e 74 20 66 74 73 33 4e 65 78 74 45 78  c int fts3NextEx
363d7 70 72 54 6f 6b 65 6e 28 46 74 73 33 45 78 70 72  prToken(Fts3Expr
363d8 20 2a 2a 70 70 45 78 70 72 2c 20 69 6e 74 20 2a   **ppExpr, int *
363d9 70 69 54 6f 6b 65 6e 29 7b 0a 20 20 46 74 73 33  piToken){.  Fts3
363da 45 78 70 72 20 2a 70 20 3d 20 2a 70 70 45 78 70  Expr *p = *ppExp
363db 72 3b 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 20  r;.  int iToken 
363dc 3d 20 2a 70 69 54 6f 6b 65 6e 3b 0a 20 20 69 66  = *piToken;.  if
363dd 28 20 69 54 6f 6b 65 6e 3c 30 20 29 7b 0a 20 20  ( iToken<0 ){.  
363de 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73    /* In this cas
363df 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  e the expression
363e0 20 70 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6f   p is the root o
363e1 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  f an expression 
363e2 74 72 65 65 2e 0a 20 20 20 20 2a 2a 20 4d 6f 76  tree..    ** Mov
363e3 65 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 74  e to the first t
363e4 6f 6b 65 6e 20 69 6e 20 74 68 65 20 65 78 70 72  oken in the expr
363e5 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 20 20 20  ession tree..   
363e6 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 70   */.    while( p
363e7 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20  ->pLeft ){.     
363e8 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20   p = p->pLeft;. 
363e9 20 20 20 7d 0a 20 20 20 20 69 54 6f 6b 65 6e 20     }.    iToken 
363ea 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
363eb 20 20 61 73 73 65 72 74 28 70 20 26 26 20 70 2d    assert(p && p-
363ec 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
363ed 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 20 20 69  _PHRASE );.    i
363ee 66 28 20 69 54 6f 6b 65 6e 3c 28 70 2d 3e 70 50  f( iToken<(p->pP
363ef 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 2d 31 29  hrase->nToken-1)
363f0 20 29 7b 0a 20 20 20 20 20 20 69 54 6f 6b 65 6e   ){.      iToken
363f1 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ++;.    }else{. 
363f2 20 20 20 20 20 69 54 6f 6b 65 6e 20 3d 20 30 3b       iToken = 0;
363f3 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d  .      while( p-
363f4 3e 70 50 61 72 65 6e 74 20 26 26 20 70 2d 3e 70  >pParent && p->p
363f5 50 61 72 65 6e 74 2d 3e 70 4c 65 66 74 21 3d 70  Parent->pLeft!=p
363f6 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
363f7 72 74 28 20 70 2d 3e 70 50 61 72 65 6e 74 2d 3e  rt( p->pParent->
363f8 70 52 69 67 68 74 3d 3d 70 20 29 3b 0a 20 20 20  pRight==p );.   
363f9 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 61 72       p = p->pPar
363fa 65 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ent;.      }.   
363fb 20 20 20 70 20 3d 20 70 2d 3e 70 50 61 72 65 6e     p = p->pParen
363fc 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29  t;.      if( p )
363fd 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
363fe 28 20 70 2d 3e 70 52 69 67 68 74 21 3d 30 20 29  ( p->pRight!=0 )
363ff 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d  ;.        p = p-
36400 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  >pRight;.       
36401 20 77 68 69 6c 65 28 20 70 2d 3e 70 4c 65 66 74   while( p->pLeft
36402 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 20   ){.          p 
36403 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  = p->pLeft;.    
36404 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
36405 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 70 45 78    }.  }..  *ppEx
36406 70 72 20 3d 20 70 3b 0a 20 20 2a 70 69 54 6f 6b  pr = p;.  *piTok
36407 65 6e 20 3d 20 69 54 6f 6b 65 6e 3b 0a 20 20 72  en = iToken;.  r
36408 65 74 75 72 6e 20 70 3f 31 3a 30 3b 0a 7d 0a 0a  eturn p?1:0;.}..
36409 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  /*.** Return TRU
3640a 45 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73  E if the express
3640b 69 6f 6e 20 6e 6f 64 65 20 70 45 78 70 72 20 69  ion node pExpr i
3640c 73 20 6c 6f 63 61 74 65 64 20 62 65 6e 65 61 74  s located beneat
3640d 68 20 74 68 65 0a 2a 2a 20 52 48 53 20 6f 66 20  h the.** RHS of 
3640e 61 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e 0a  a NOT operator..
3640f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
36410 73 33 45 78 70 72 42 65 6e 65 61 74 68 4e 6f 74  s3ExprBeneathNot
36411 28 46 74 73 33 45 78 70 72 20 2a 70 29 7b 0a 20  (Fts3Expr *p){. 
36412 20 46 74 73 33 45 78 70 72 20 2a 70 50 61 72 65   Fts3Expr *pPare
36413 6e 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29  nt;.  while( p )
36414 7b 0a 20 20 20 20 70 50 61 72 65 6e 74 20 3d 20  {.    pParent = 
36415 70 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20  p->pParent;.    
36416 69 66 28 20 70 50 61 72 65 6e 74 20 26 26 20 70  if( pParent && p
36417 50 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d 3d 46  Parent->eType==F
36418 54 53 51 55 45 52 59 5f 4e 4f 54 20 26 26 20 70  TSQUERY_NOT && p
36419 50 61 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d  Parent->pRight==
3641a 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  p ){.      retur
3641b 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  n 1;.    }.    p
3641c 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a   = pParent;.  }.
3641d 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
3641e 2a 0a 2a 2a 20 41 64 64 20 65 6e 74 72 69 65 73  *.** Add entries
3641f 20 74 6f 20 70 53 6e 69 70 70 65 74 2d 3e 61 4d   to pSnippet->aM
36420 61 74 63 68 5b 5d 20 66 6f 72 20 65 76 65 72 79  atch[] for every
36421 20 6d 61 74 63 68 20 74 68 61 74 20 6f 63 63 75   match that occu
36422 72 73 20 61 67 61 69 6e 73 74 0a 2a 2a 20 64 6f  rs against.** do
36423 63 75 6d 65 6e 74 20 7a 44 6f 63 5b 30 2e 2e 6e  cument zDoc[0..n
36424 44 6f 63 2d 31 5d 20 77 68 69 63 68 20 69 73 20  Doc-1] which is 
36425 73 74 6f 72 65 64 20 69 6e 20 63 6f 6c 75 6d 6e  stored in column
36426 20 69 43 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61   iColumn..*/.sta
36427 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74  tic void snippet
36428 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28  OffsetsOfColumn(
36429 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
3642a 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20  or *pCur,       
3642b 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c 74 65 73    /* The fulltes
3642c 74 20 73 65 61 72 63 68 20 63 75 72 73 6f 72 20  t search cursor 
3642d 2a 2f 0a 20 20 53 6e 69 70 70 65 74 20 2a 70 53  */.  Snippet *pS
3642e 6e 69 70 70 65 74 2c 20 20 20 20 20 20 20 20 20  nippet,         
3642f 20 20 20 20 2f 2a 20 54 68 65 20 53 6e 69 70 70      /* The Snipp
36430 65 74 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20  et object to be 
36431 66 69 6c 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69  filled in */.  i
36432 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20  nt iColumn,     
36433 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36434 20 49 6e 64 65 78 20 6f 66 20 66 75 6c 6c 74 65   Index of fullte
36435 78 74 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  xt table column 
36436 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
36437 2a 7a 44 6f 63 2c 20 20 20 20 20 20 20 20 20 20  *zDoc,          
36438 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74      /* Text of t
36439 68 65 20 66 75 6c 6c 74 65 78 74 20 74 61 62 6c  he fulltext tabl
3643a 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e  e column */.  in
3643b 74 20 6e 44 6f 63 20 20 20 20 20 20 20 20 20 20  t nDoc          
3643c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3643d 4c 65 6e 67 74 68 20 6f 66 20 7a 44 6f 63 20 69  Length of zDoc i
3643e 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20  n bytes */.){.  
3643f 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
36440 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
36441 70 54 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68  pTModule;  /* Th
36442 65 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  e tokenizer modu
36443 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  le */.  sqlite3_
36444 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
36445 6e 69 7a 65 72 3b 20 20 20 20 20 20 20 20 20 20  nizer;          
36446 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66     /* The specif
36447 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a  ic tokenizer */.
36448 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
36449 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 54 43 75  zer_cursor *pTCu
3644a 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  rsor;        /* 
3644b 54 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72  Tokenizer cursor
3644c 20 2a 2f 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76   */.  fulltext_v
3644d 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20 20  tab *pVtab;     
3644e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
3644f 65 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65  e full text inde
36450 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  x */.  int nColu
36451 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
36452 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
36453 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
36454 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20 2a 2f   in the index */
36455 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20  .  int i, j;    
36456 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36457 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
36458 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e  counters */.  in
36459 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
3645a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3645b 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
3645c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
3645d 69 6e 74 20 6d 61 74 63 68 2c 20 70 72 65 76 4d  int match, prevM
3645e 61 74 63 68 3b 20 20 20 20 20 20 20 2f 2a 20 50  atch;       /* P
3645f 68 72 61 73 65 20 73 65 61 72 63 68 20 62 69 74  hrase search bit
36460 6d 61 73 6b 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  masks */.  const
36461 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20   char *zToken;  
36462 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36463 2f 2a 20 4e 65 78 74 20 74 6f 6b 65 6e 20 66 72  /* Next token fr
36464 6f 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  om the tokenizer
36465 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e   */.  int nToken
36466 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36467 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
36468 7a 65 20 6f 66 20 7a 54 6f 6b 65 6e 20 2a 2f 0a  ze of zToken */.
36469 20 20 69 6e 74 20 69 42 65 67 69 6e 2c 20 69 45    int iBegin, iE
3646a 6e 64 2c 20 69 50 6f 73 3b 20 20 20 20 20 20 20  nd, iPos;       
3646b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
3646c 73 20 6f 66 20 62 65 67 69 6e 6e 69 6e 67 20 61  s of beginning a
3646d 6e 64 20 65 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20  nd end */..  /* 
3646e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
3646f 72 69 61 62 6c 65 73 20 6b 65 65 70 20 61 20 63  riables keep a c
36470 69 72 63 75 6c 61 72 20 62 75 66 66 65 72 20 6f  ircular buffer o
36471 66 20 74 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20  f the last.  ** 
36472 66 65 77 20 74 6f 6b 65 6e 73 20 2a 2f 0a 20 20  few tokens */.  
36473 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 52 6f  unsigned int iRo
36474 74 6f 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  tor = 0;        
36475 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
36476 20 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a   current token *
36477 2f 0a 20 20 69 6e 74 20 69 52 6f 74 6f 72 42 65  /.  int iRotorBe
36478 67 69 6e 5b 46 54 53 33 5f 52 4f 54 4f 52 5f 53  gin[FTS3_ROTOR_S
36479 5a 5d 3b 20 20 20 20 20 20 2f 2a 20 42 65 67 69  Z];      /* Begi
3647a 6e 6e 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  nning offset of 
3647b 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  token */.  int i
3647c 52 6f 74 6f 72 4c 65 6e 5b 46 54 53 33 5f 52 4f  RotorLen[FTS3_RO
3647d 54 4f 52 5f 53 5a 5d 3b 20 20 20 20 20 20 20 20  TOR_SZ];        
3647e 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 6f 6b  /* Length of tok
3647f 65 6e 20 2a 2f 0a 0a 20 20 70 56 74 61 62 20 3d  en */..  pVtab =
36480 20 63 75 72 73 6f 72 5f 76 74 61 62 28 70 43 75   cursor_vtab(pCu
36481 72 29 3b 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20  r);.  nColumn = 
36482 70 56 74 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a  pVtab->nColumn;.
36483 20 20 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70    pTokenizer = p
36484 56 74 61 62 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  Vtab->pTokenizer
36485 3b 0a 20 20 70 54 4d 6f 64 75 6c 65 20 3d 20 70  ;.  pTModule = p
36486 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75  Tokenizer->pModu
36487 6c 65 3b 0a 20 20 72 63 20 3d 20 70 54 4d 6f 64  le;.  rc = pTMod
36488 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65  ule->xOpen(pToke
36489 6e 69 7a 65 72 2c 20 7a 44 6f 63 2c 20 6e 44 6f  nizer, zDoc, nDo
3648a 63 2c 20 26 70 54 43 75 72 73 6f 72 29 3b 0a 20  c, &pTCursor);. 
3648b 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
3648c 3b 0a 20 20 70 54 43 75 72 73 6f 72 2d 3e 70 54  ;.  pTCursor->pT
3648d 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65  okenizer = pToke
3648e 6e 69 7a 65 72 3b 0a 0a 20 20 70 72 65 76 4d 61  nizer;..  prevMa
3648f 74 63 68 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65  tch = 0;.  while
36490 28 20 21 70 54 4d 6f 64 75 6c 65 2d 3e 78 4e 65  ( !pTModule->xNe
36491 78 74 28 70 54 43 75 72 73 6f 72 2c 20 26 7a 54  xt(pTCursor, &zT
36492 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26  oken, &nToken, &
36493 69 42 65 67 69 6e 2c 20 26 69 45 6e 64 2c 20 26  iBegin, &iEnd, &
36494 69 50 6f 73 29 20 29 7b 0a 20 20 20 20 46 74 73  iPos) ){.    Fts
36495 33 45 78 70 72 20 2a 70 49 74 65 72 20 3d 20 70  3Expr *pIter = p
36496 43 75 72 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  Cur->pExpr;.    
36497 69 6e 74 20 69 49 74 65 72 20 3d 20 2d 31 3b 0a  int iIter = -1;.
36498 20 20 20 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b      iRotorBegin[
36499 69 52 6f 74 6f 72 26 46 54 53 33 5f 52 4f 54 4f  iRotor&FTS3_ROTO
3649a 52 5f 4d 41 53 4b 5d 20 3d 20 69 42 65 67 69 6e  R_MASK] = iBegin
3649b 3b 0a 20 20 20 20 69 52 6f 74 6f 72 4c 65 6e 5b  ;.    iRotorLen[
3649c 69 52 6f 74 6f 72 26 46 54 53 33 5f 52 4f 54 4f  iRotor&FTS3_ROTO
3649d 52 5f 4d 41 53 4b 5d 20 3d 20 69 45 6e 64 2d 69  R_MASK] = iEnd-i
3649e 42 65 67 69 6e 3b 0a 20 20 20 20 6d 61 74 63 68  Begin;.    match
3649f 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d   = 0;.    for(i=
364a0 30 3b 20 69 3c 28 46 54 53 33 5f 52 4f 54 4f 52  0; i<(FTS3_ROTOR
364a1 5f 53 5a 2d 31 29 20 26 26 20 66 74 73 33 4e 65  _SZ-1) && fts3Ne
364a2 78 74 45 78 70 72 54 6f 6b 65 6e 28 26 70 49 74  xtExprToken(&pIt
364a3 65 72 2c 20 26 69 49 74 65 72 29 3b 20 69 2b 2b  er, &iIter); i++
364a4 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 68  ){.      int nPh
364a5 72 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  rase;           
364a6 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
364a7 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20  er of tokens in 
364a8 63 75 72 72 65 6e 74 20 70 68 72 61 73 65 20 2a  current phrase *
364a9 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 50  /.      struct P
364aa 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70 54 6f 6b  hraseToken *pTok
364ab 65 6e 3b 20 20 20 20 20 2f 2a 20 43 75 72 72 65  en;     /* Curre
364ac 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20  nt token */.    
364ad 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20    int iCol;     
364ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
364af 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65    /* Column inde
364b0 78 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 20  x */..      if( 
364b1 66 74 73 33 45 78 70 72 42 65 6e 65 61 74 68 4e  fts3ExprBeneathN
364b2 6f 74 28 70 49 74 65 72 29 20 29 20 63 6f 6e 74  ot(pIter) ) cont
364b3 69 6e 75 65 3b 0a 20 20 20 20 20 20 6e 50 68 72  inue;.      nPhr
364b4 61 73 65 20 3d 20 70 49 74 65 72 2d 3e 70 50 68  ase = pIter->pPh
364b5 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20  rase->nToken;.  
364b6 20 20 20 20 70 54 6f 6b 65 6e 20 3d 20 26 70 49      pToken = &pI
364b7 74 65 72 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54  ter->pPhrase->aT
364b8 6f 6b 65 6e 5b 69 49 74 65 72 5d 3b 0a 20 20 20  oken[iIter];.   
364b9 20 20 20 69 43 6f 6c 20 3d 20 70 49 74 65 72 2d     iCol = pIter-
364ba 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d  >pPhrase->iColum
364bb 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f  n;.      if( iCo
364bc 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 6e 43 6f  l>=0 && iCol<nCo
364bd 6c 75 6d 6e 20 26 26 20 69 43 6f 6c 21 3d 69 43  lumn && iCol!=iC
364be 6f 6c 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65  olumn ) continue
364bf 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b  ;.      if( pTok
364c0 65 6e 2d 3e 6e 3e 6e 54 6f 6b 65 6e 20 29 20 63  en->n>nToken ) c
364c1 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69  ontinue;.      i
364c2 66 28 20 21 70 54 6f 6b 65 6e 2d 3e 69 73 50 72  f( !pToken->isPr
364c3 65 66 69 78 20 26 26 20 70 54 6f 6b 65 6e 2d 3e  efix && pToken->
364c4 6e 3c 6e 54 6f 6b 65 6e 20 29 20 63 6f 6e 74 69  n<nToken ) conti
364c5 6e 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72  nue;.      asser
364c6 74 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3c 3d 6e 54  t( pToken->n<=nT
364c7 6f 6b 65 6e 20 29 3b 0a 20 20 20 20 20 20 69 66  oken );.      if
364c8 28 20 6d 65 6d 63 6d 70 28 70 54 6f 6b 65 6e 2d  ( memcmp(pToken-
364c9 3e 7a 2c 20 7a 54 6f 6b 65 6e 2c 20 70 54 6f 6b  >z, zToken, pTok
364ca 65 6e 2d 3e 6e 29 20 29 20 63 6f 6e 74 69 6e 75  en->n) ) continu
364cb 65 3b 0a 20 20 20 20 20 20 69 66 28 20 69 49 74  e;.      if( iIt
364cc 65 72 3e 30 20 26 26 20 28 70 72 65 76 4d 61 74  er>0 && (prevMat
364cd 63 68 20 26 20 28 31 3c 3c 69 29 29 3d 3d 30 20  ch & (1<<i))==0 
364ce 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
364cf 20 20 6d 61 74 63 68 20 7c 3d 20 31 3c 3c 69 3b    match |= 1<<i;
364d0 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 28 46  .      if( i==(F
364d1 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d 32 29 20  TS3_ROTOR_SZ-2) 
364d2 7c 7c 20 6e 50 68 72 61 73 65 3d 3d 69 49 74 65  || nPhrase==iIte
364d3 72 2b 31 20 29 7b 0a 20 20 20 20 20 20 20 20 66  r+1 ){.        f
364d4 6f 72 28 6a 3d 6e 50 68 72 61 73 65 2d 31 3b 20  or(j=nPhrase-1; 
364d5 6a 3e 3d 30 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20  j>=0; j--){.    
364d6 20 20 20 20 20 20 69 6e 74 20 6b 20 3d 20 28 69        int k = (i
364d7 52 6f 74 6f 72 2d 6a 29 20 26 20 46 54 53 33 5f  Rotor-j) & FTS3_
364d8 52 4f 54 4f 52 5f 4d 41 53 4b 3b 0a 20 20 20 20  ROTOR_MASK;.    
364d9 20 20 20 20 20 20 73 6e 69 70 70 65 74 41 70 70        snippetApp
364da 65 6e 64 4d 61 74 63 68 28 70 53 6e 69 70 70 65  endMatch(pSnippe
364db 74 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 2d 6a 2c  t, iColumn, i-j,
364dc 20 69 50 6f 73 2d 6a 2c 0a 20 20 20 20 20 20 20   iPos-j,.       
364dd 20 20 20 20 20 20 20 20 20 69 52 6f 74 6f 72 42           iRotorB
364de 65 67 69 6e 5b 6b 5d 2c 20 69 52 6f 74 6f 72 4c  egin[k], iRotorL
364df 65 6e 5b 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20  en[k]);.        
364e0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
364e1 20 20 20 20 70 72 65 76 4d 61 74 63 68 20 3d 20      prevMatch = 
364e2 6d 61 74 63 68 3c 3c 31 3b 0a 20 20 20 20 69 52  match<<1;.    iR
364e3 6f 74 6f 72 2b 2b 3b 0a 20 20 7d 0a 20 20 70 54  otor++;.  }.  pT
364e4 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70  Module->xClose(p
364e5 54 43 75 72 73 6f 72 29 3b 20 20 0a 7d 0a 0a 2f  TCursor);  .}../
364e6 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 65 6e 74 72  *.** Remove entr
364e7 69 65 73 20 66 72 6f 6d 20 74 68 65 20 70 53 6e  ies from the pSn
364e8 69 70 70 65 74 20 73 74 72 75 63 74 75 72 65 20  ippet structure 
364e9 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 74  to account for t
364ea 68 65 20 4e 45 41 52 0a 2a 2a 20 6f 70 65 72 61  he NEAR.** opera
364eb 74 6f 72 2e 20 57 68 65 6e 20 74 68 69 73 20 69  tor. When this i
364ec 73 20 63 61 6c 6c 65 64 2c 20 70 53 6e 69 70 70  s called, pSnipp
364ed 65 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  et contains the 
364ee 6c 69 73 74 20 6f 66 20 74 6f 6b 65 6e 20 0a 2a  list of token .*
364ef 2a 20 6f 66 66 73 65 74 73 20 70 72 6f 64 75 63  * offsets produc
364f0 65 64 20 62 79 20 74 72 65 61 74 69 6e 67 20 61  ed by treating a
364f1 6c 6c 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72  ll NEAR operator
364f2 73 20 61 73 20 41 4e 44 20 6f 70 65 72 61 74 6f  s as AND operato
364f3 72 73 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  rs..** This func
364f4 74 69 6f 6e 20 72 65 6d 6f 76 65 73 20 61 6e 79  tion removes any
364f5 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 73 68   entries that sh
364f6 6f 75 6c 64 20 6e 6f 74 20 62 65 20 70 72 65 73  ould not be pres
364f7 65 6e 74 20 61 66 74 65 72 0a 2a 2a 20 61 63 63  ent after.** acc
364f8 6f 75 6e 74 69 6e 67 20 66 6f 72 20 74 68 65 20  ounting for the 
364f9 4e 45 41 52 20 72 65 73 74 72 69 63 74 69 6f 6e  NEAR restriction
364fa 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  . For example, i
364fb 66 20 74 68 65 20 71 75 65 72 69 65 64 0a 2a 2a  f the queried.**
364fc 20 64 6f 63 75 6d 65 6e 74 20 69 73 3a 0a 2a 2a   document is:.**
364fd 0a 2a 2a 20 20 20 20 20 22 41 20 42 20 43 20 44  .**     "A B C D
364fe 20 45 20 41 22 0a 2a 2a 0a 2a 2a 20 61 6e 64 20   E A".**.** and 
364ff 74 68 65 20 71 75 65 72 79 20 69 73 3a 0a 2a 2a  the query is:.**
36500 20 0a 2a 2a 20 20 20 20 20 41 20 4e 45 41 52 2f   .**     A NEAR/
36501 30 20 45 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 77  0 E.**.** then w
36502 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
36503 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20  n is called the 
36504 53 6e 69 70 70 65 74 20 63 6f 6e 74 61 69 6e 73  Snippet contains
36505 20 74 6f 6b 65 6e 20 6f 66 66 73 65 74 73 0a 2a   token offsets.*
36506 2a 20 30 2c 20 34 20 61 6e 64 20 35 2e 20 54 68  * 0, 4 and 5. Th
36507 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f  is function remo
36508 76 65 73 20 74 68 65 20 22 30 22 20 65 6e 74 72  ves the "0" entr
36509 79 20 28 62 65 63 61 75 73 65 20 74 68 65 20 66  y (because the f
3650a 69 72 73 74 20 41 0a 2a 2a 20 69 73 20 6e 6f 74  irst A.** is not
3650b 20 6e 65 61 72 20 65 6e 6f 75 67 68 20 74 6f 20   near enough to 
3650c 61 6e 20 45 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  an E)..**.** Whe
3650d 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
3650e 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 76  is called, the v
3650f 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  alue pointed to 
36510 62 79 20 70 61 72 61 6d 65 74 65 72 20 70 69 4c  by parameter piL
36511 65 66 74 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e  eft is.** the in
36512 74 65 67 65 72 20 69 64 20 6f 66 20 74 68 65 20  teger id of the 
36513 6c 65 66 74 2d 6d 6f 73 74 20 74 6f 6b 65 6e 20  left-most token 
36514 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
36515 6e 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79  n tree headed by
36516 0a 2a 2a 20 70 45 78 70 72 2e 20 54 68 69 73 20  .** pExpr. This 
36517 66 75 6e 63 74 69 6f 6e 20 69 6e 63 72 65 6d 65  function increme
36518 6e 74 73 20 2a 70 69 4c 65 66 74 20 62 79 20 74  nts *piLeft by t
36519 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
3651a 6f 66 20 74 6f 6b 65 6e 73 0a 2a 2a 20 69 6e 20  of tokens.** in 
3651b 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
3651c 72 65 65 20 68 65 61 64 65 64 20 62 79 20 70 45  ree headed by pE
3651d 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  xpr..**.** Retur
3651e 6e 20 31 20 69 66 20 61 6e 79 20 74 72 69 6d 6d  n 1 if any trimm
3651f 69 6e 67 20 6f 63 63 75 72 73 2e 20 20 52 65 74  ing occurs.  Ret
36520 75 72 6e 20 30 20 69 66 20 6e 6f 20 74 72 69 6d  urn 0 if no trim
36521 6d 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64  ming is required
36522 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36523 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65  trimSnippetOffse
36524 74 73 28 0a 20 20 46 74 73 33 45 78 70 72 20 2a  ts(.  Fts3Expr *
36525 70 45 78 70 72 2c 20 20 20 20 20 20 2f 2a 20 54  pExpr,      /* T
36526 68 65 20 73 65 61 72 63 68 20 65 78 70 72 65 73  he search expres
36527 73 69 6f 6e 20 2a 2f 0a 20 20 53 6e 69 70 70 65  sion */.  Snippe
36528 74 20 2a 70 53 6e 69 70 70 65 74 2c 20 20 20 20  t *pSnippet,    
36529 2f 2a 20 54 68 65 20 73 65 74 20 6f 66 20 73 6e  /* The set of sn
3652a 69 70 70 65 74 20 6f 66 66 73 65 74 73 20 74 6f  ippet offsets to
3652b 20 62 65 20 74 72 69 6d 6d 65 64 20 2a 2f 0a 20   be trimmed */. 
3652c 20 69 6e 74 20 2a 70 69 4c 65 66 74 20 20 20 20   int *piLeft    
3652d 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
3652e 6f 66 20 6c 65 66 74 2d 6d 6f 73 74 20 74 6f 6b  of left-most tok
3652f 65 6e 20 69 6e 20 70 45 78 70 72 20 2a 2f 0a 29  en in pExpr */.)
36530 7b 0a 20 20 69 66 28 20 70 45 78 70 72 20 29 7b  {.  if( pExpr ){
36531 0a 20 20 20 20 69 66 28 20 74 72 69 6d 53 6e 69  .    if( trimSni
36532 70 70 65 74 4f 66 66 73 65 74 73 28 70 45 78 70  ppetOffsets(pExp
36533 72 2d 3e 70 4c 65 66 74 2c 20 70 53 6e 69 70 70  r->pLeft, pSnipp
36534 65 74 2c 20 70 69 4c 65 66 74 29 20 29 7b 0a 20  et, piLeft) ){. 
36535 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
36536 20 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68     }..    switch
36537 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65 20 29  ( pExpr->eType )
36538 7b 0a 20 20 20 20 20 20 63 61 73 65 20 46 54 53  {.      case FTS
36539 51 55 45 52 59 5f 50 48 52 41 53 45 3a 0a 20 20  QUERY_PHRASE:.  
3653a 20 20 20 20 20 20 2a 70 69 4c 65 66 74 20 2b 3d        *piLeft +=
3653b 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73 65 2d   pExpr->pPhrase-
3653c 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20  >nToken;.       
3653d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3653e 73 65 20 46 54 53 51 55 45 52 59 5f 4e 45 41 52  se FTSQUERY_NEAR
3653f 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  : {.        /* T
36540 68 65 20 72 69 67 68 74 2d 68 61 6e 64 2d 73 69  he right-hand-si
36541 64 65 20 6f 66 20 61 20 4e 45 41 52 20 6f 70 65  de of a NEAR ope
36542 72 61 74 6f 72 20 69 73 20 61 6c 77 61 79 73 20  rator is always 
36543 61 20 70 68 72 61 73 65 2e 20 54 68 65 0a 20 20  a phrase. The.  
36544 20 20 20 20 20 20 2a 2a 20 6c 65 66 74 2d 68 61        ** left-ha
36545 6e 64 2d 73 69 64 65 20 69 73 20 65 69 74 68 65  nd-side is eithe
36546 72 20 61 20 70 68 72 61 73 65 20 6f 72 20 61 6e  r a phrase or an
36547 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
36548 20 74 68 61 74 20 69 73 20 0a 20 20 20 20 20 20   that is .      
36549 20 20 2a 2a 20 69 74 73 65 6c 66 20 68 65 61 64    ** itself head
3654a 65 64 20 62 79 20 61 20 4e 45 41 52 20 6f 70 65  ed by a NEAR ope
3654b 72 61 74 6f 72 2e 20 54 68 65 20 66 6f 6c 6c 6f  rator. The follo
3654c 77 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  wing initializat
3654d 69 6f 6e 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  ions.        ** 
3654e 73 65 74 20 6c 6f 63 61 6c 20 76 61 72 69 61 62  set local variab
3654f 6c 65 20 69 4c 65 66 74 20 74 6f 20 74 68 65 20  le iLeft to the 
36550 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 6f 66 20  token number of 
36551 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 20 20  the left-most.  
36552 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69        ** token i
36553 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  n the right-hand
36554 20 70 68 72 61 73 65 2c 20 61 6e 64 20 69 52 69   phrase, and iRi
36555 67 68 74 20 74 6f 20 74 68 65 20 72 69 67 68 74  ght to the right
36556 20 6d 6f 73 74 0a 20 20 20 20 20 20 20 20 2a 2a   most.        **
36557 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73 61   token in the sa
36558 6d 65 20 70 68 72 61 73 65 2e 20 46 6f 72 20 65  me phrase. For e
36559 78 61 6d 70 6c 65 2c 20 69 66 20 77 65 20 68 61  xample, if we ha
3655a 64 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  d:.        **.  
3655b 20 20 20 20 20 20 2a 2a 20 20 20 20 20 3c 63 6f        **     <co
3655c 6c 3e 20 4d 41 54 43 48 20 27 22 61 62 63 20 64  l> MATCH '"abc d
3655d 65 66 22 20 4e 45 41 52 2f 32 20 22 67 68 69 20  ef" NEAR/2 "ghi 
3655e 6a 6b 6c 22 27 0a 20 20 20 20 20 20 20 20 2a 2a  jkl"'.        **
3655f 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  .        ** then
36560 20 69 4c 65 66 74 20 77 69 6c 6c 20 62 65 20 73   iLeft will be s
36561 65 74 20 74 6f 20 32 20 28 74 6f 6b 65 6e 20 6e  et to 2 (token n
36562 75 6d 62 65 72 20 6f 66 20 67 68 69 29 20 61 6e  umber of ghi) an
36563 64 20 6e 54 6f 6b 65 6e 20 77 69 6c 6c 0a 20 20  d nToken will.  
36564 20 20 20 20 20 20 2a 2a 20 62 65 20 73 65 74 20        ** be set 
36565 74 6f 20 34 2e 0a 20 20 20 20 20 20 20 20 2a 2f  to 4..        */
36566 0a 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70  .        Fts3Exp
36567 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72  r *pLeft = pExpr
36568 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20  ->pLeft;.       
36569 20 46 74 73 33 45 78 70 72 20 2a 70 52 69 67 68   Fts3Expr *pRigh
3656a 74 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68  t = pExpr->pRigh
3656b 74 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  t;.        int i
3656c 4c 65 66 74 20 3d 20 2a 70 69 4c 65 66 74 3b 0a  Left = *piLeft;.
3656d 20 20 20 20 20 20 20 20 69 6e 74 20 6e 4e 65 61          int nNea
3656e 72 20 3d 20 70 45 78 70 72 2d 3e 6e 4e 65 61 72  r = pExpr->nNear
3656f 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 54  ;.        int nT
36570 6f 6b 65 6e 20 3d 20 70 52 69 67 68 74 2d 3e 70  oken = pRight->p
36571 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a  Phrase->nToken;.
36572 20 20 20 20 20 20 20 20 69 6e 74 20 6a 6a 2c 20          int jj, 
36573 69 69 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ii;.        if( 
36574 70 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d 46 54  pLeft->eType==FT
36575 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20  SQUERY_NEAR ){. 
36576 20 20 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d           pLeft =
36577 20 70 4c 65 66 74 2d 3e 70 52 69 67 68 74 3b 0a   pLeft->pRight;.
36578 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
36579 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74    assert( pRight
3657a 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
3657b 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 20 20  Y_PHRASE );.    
3657c 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66      assert( pLef
3657d 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  t->eType==FTSQUE
3657e 52 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 20  RY_PHRASE );.   
3657f 20 20 20 20 20 6e 54 6f 6b 65 6e 20 2b 3d 20 70       nToken += p
36580 4c 65 66 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  Left->pPhrase->n
36581 54 6f 6b 65 6e 3b 0a 0a 20 20 20 20 20 20 20 20  Token;..        
36582 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 53 6e  for(ii=0; ii<pSn
36583 69 70 70 65 74 2d 3e 6e 4d 61 74 63 68 3b 20 69  ippet->nMatch; i
36584 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
36585 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61  struct snippetMa
36586 74 63 68 20 2a 70 20 3d 20 26 70 53 6e 69 70 70  tch *p = &pSnipp
36587 65 74 2d 3e 61 4d 61 74 63 68 5b 69 69 5d 3b 0a  et->aMatch[ii];.
36588 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d            if( p-
36589 3e 69 54 65 72 6d 3d 3d 69 4c 65 66 74 20 29 7b  >iTerm==iLeft ){
3658a 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
3658b 20 69 73 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 20   isOk = 0;.     
3658c 20 20 20 20 20 20 20 2f 2a 20 53 6e 69 70 70 65         /* Snippe
3658d 74 20 69 69 20 69 73 20 61 6e 20 6f 63 63 75 72  t ii is an occur
3658e 65 6e 63 65 20 6f 66 20 71 75 65 72 79 20 74 65  ence of query te
3658f 72 6d 20 69 4c 65 66 74 20 69 6e 20 74 68 65 20  rm iLeft in the 
36590 64 6f 63 75 6d 65 6e 74 2e 0a 20 20 20 20 20 20  document..      
36591 20 20 20 20 20 20 2a 2a 20 49 74 20 6f 63 63 75        ** It occu
36592 72 73 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 28  rs at position (
36593 70 2d 3e 69 54 6f 6b 65 6e 29 20 6f 66 20 74 68  p->iToken) of th
36594 65 20 64 6f 63 75 6d 65 6e 74 2e 20 57 65 20 6e  e document. We n
36595 6f 77 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  ow.            *
36596 2a 20 73 65 61 72 63 68 20 66 6f 72 20 61 6e 20  * search for an 
36597 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 6f 6b 65  instance of toke
36598 6e 20 28 69 4c 65 66 74 2d 31 29 20 73 6f 6d 65  n (iLeft-1) some
36599 77 68 65 72 65 20 69 6e 20 74 68 65 20 0a 20 20  where in the .  
3659a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e            ** ran
3659b 67 65 20 28 70 2d 3e 69 54 6f 6b 65 6e 20 2d 20  ge (p->iToken - 
3659c 6e 4e 65 61 72 29 2e 2e 2e 28 70 2d 3e 69 54 6f  nNear)...(p->iTo
3659d 6b 65 6e 20 2b 20 6e 4e 65 61 72 20 2b 20 6e 54  ken + nNear + nT
3659e 6f 6b 65 6e 29 20 77 69 74 68 69 6e 20 0a 20 20  oken) within .  
3659f 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65            ** the
365a0 20 73 65 74 20 6f 66 20 73 6e 69 70 70 65 74 4d   set of snippetM
365a1 61 74 63 68 20 73 74 72 75 63 74 75 72 65 73 2e  atch structures.
365a2 20 49 66 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64   If one is found
365a3 2c 20 70 72 6f 63 65 65 64 2e 20 0a 20 20 20 20  , proceed. .    
365a4 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 6f 6e          ** If on
365a5 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  e cannot be foun
365a6 64 2c 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 73  d, then remove s
365a7 6e 69 70 70 65 74 73 20 69 69 2e 2e 28 69 69 2b  nippets ii..(ii+
365a8 4e 2d 31 29 20 0a 20 20 20 20 20 20 20 20 20 20  N-1) .          
365a9 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6d 61    ** from the ma
365aa 74 63 68 69 6e 67 20 73 6e 69 70 70 65 74 73 2c  tching snippets,
365ab 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20   where N is the 
365ac 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73  number of tokens
365ad 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a   .            **
365ae 20 69 6e 20 70 68 72 61 73 65 20 70 52 69 67 68   in phrase pRigh
365af 74 2d 3e 70 50 68 72 61 73 65 2e 0a 20 20 20 20  t->pPhrase..    
365b0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
365b1 20 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b         for(jj=0;
365b2 20 69 73 4f 6b 3d 3d 30 20 26 26 20 6a 6a 3c 70   isOk==0 && jj<p
365b3 53 6e 69 70 70 65 74 2d 3e 6e 4d 61 74 63 68 3b  Snippet->nMatch;
365b4 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   jj++){.        
365b5 20 20 20 20 20 20 73 74 72 75 63 74 20 73 6e 69        struct sni
365b6 70 70 65 74 4d 61 74 63 68 20 2a 70 32 20 3d 20  ppetMatch *p2 = 
365b7 26 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63  &pSnippet->aMatc
365b8 68 5b 6a 6a 5d 3b 0a 20 20 20 20 20 20 20 20 20  h[jj];.         
365b9 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54 65       if( p2->iTe
365ba 72 6d 3d 3d 28 69 4c 65 66 74 2d 31 29 20 29 7b  rm==(iLeft-1) ){
365bb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
365bc 20 69 66 28 20 70 32 2d 3e 69 54 6f 6b 65 6e 3e   if( p2->iToken>
365bd 3d 28 70 2d 3e 69 54 6f 6b 65 6e 2d 6e 4e 65 61  =(p->iToken-nNea
365be 72 2d 31 29 20 0a 20 20 20 20 20 20 20 20 20 20  r-1) .          
365bf 20 20 20 20 20 20 20 26 26 20 70 32 2d 3e 69 54         && p2->iT
365c0 6f 6b 65 6e 3c 28 70 2d 3e 69 54 6f 6b 65 6e 2b  oken<(p->iToken+
365c1 6e 4e 65 61 72 2b 6e 54 6f 6b 65 6e 29 20 0a 20  nNear+nToken) . 
365c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29                 )
365c3 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
365c4 20 20 20 20 69 73 4f 6b 20 3d 20 31 3b 0a 20 20      isOk = 1;.  
365c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
365c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
365c7 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
365c8 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69            if( !i
365c9 73 4f 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  sOk ){.         
365ca 20 20 20 20 20 69 6e 74 20 6b 6b 3b 0a 20 20 20       int kk;.   
365cb 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6b             for(k
365cc 6b 3d 30 3b 20 6b 6b 3c 70 52 69 67 68 74 2d 3e  k=0; kk<pRight->
365cd 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b  pPhrase->nToken;
365ce 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   kk++){.        
365cf 20 20 20 20 20 20 20 20 70 53 6e 69 70 70 65 74          pSnippet
365d0 2d 3e 61 4d 61 74 63 68 5b 6b 6b 2b 69 69 5d 2e  ->aMatch[kk+ii].
365d1 69 54 65 72 6d 20 3d 20 2d 32 3b 0a 20 20 20 20  iTerm = -2;.    
365d2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
365d3 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
365d4 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
365d5 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
365d6 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69          if( p->i
365d7 54 65 72 6d 3d 3d 28 69 4c 65 66 74 2d 31 29 20  Term==(iLeft-1) 
365d8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
365d9 6e 74 20 69 73 4f 6b 20 3d 20 30 3b 0a 20 20 20  nt isOk = 0;.   
365da 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d           for(jj=
365db 30 3b 20 69 73 4f 6b 3d 3d 30 20 26 26 20 6a 6a  0; isOk==0 && jj
365dc 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61 74 63  <pSnippet->nMatc
365dd 68 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  h; jj++){.      
365de 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 73          struct s
365df 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70 32 20  nippetMatch *p2 
365e0 3d 20 26 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61  = &pSnippet->aMa
365e1 74 63 68 5b 6a 6a 5d 3b 0a 20 20 20 20 20 20 20  tch[jj];.       
365e2 20 20 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69         if( p2->i
365e3 54 65 72 6d 3d 3d 69 4c 65 66 74 20 29 7b 0a 20  Term==iLeft ){. 
365e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
365e5 66 28 20 70 32 2d 3e 69 54 6f 6b 65 6e 3c 3d 28  f( p2->iToken<=(
365e6 70 2d 3e 69 54 6f 6b 65 6e 2b 6e 4e 65 61 72 2b  p->iToken+nNear+
365e7 31 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  1) .            
365e8 20 20 20 20 20 26 26 20 70 32 2d 3e 69 54 6f 6b       && p2->iTok
365e9 65 6e 3e 28 70 2d 3e 69 54 6f 6b 65 6e 2d 6e 4e  en>(p->iToken-nN
365ea 65 61 72 2d 6e 54 6f 6b 65 6e 29 20 0a 20 20 20  ear-nToken) .   
365eb 20 20 20 20 20 20 20 20 20 20 20 20 20 29 7b 0a               ){.
365ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
365ed 20 20 69 73 4f 6b 20 3d 20 31 3b 0a 20 20 20 20    isOk = 1;.    
365ee 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
365ef 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
365f0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
365f1 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 4f          if( !isO
365f2 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  k ){.           
365f3 20 20 20 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20     int kk;.     
365f4 20 20 20 20 20 20 20 20 20 66 6f 72 28 6b 6b 3d           for(kk=
365f5 30 3b 20 6b 6b 3c 70 4c 65 66 74 2d 3e 70 50 68  0; kk<pLeft->pPh
365f6 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 6b 6b  rase->nToken; kk
365f7 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
365f8 20 20 20 20 20 70 53 6e 69 70 70 65 74 2d 3e 61       pSnippet->a
365f9 4d 61 74 63 68 5b 69 69 2d 6b 6b 5d 2e 69 54 65  Match[ii-kk].iTe
365fa 72 6d 20 3d 20 2d 32 3b 0a 20 20 20 20 20 20 20  rm = -2;.       
365fb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
365fc 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
365fd 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
365fe 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
365ff 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65     }.        bre
36600 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
36601 7d 0a 0a 20 20 20 20 69 66 28 20 74 72 69 6d 53  }..    if( trimS
36602 6e 69 70 70 65 74 4f 66 66 73 65 74 73 28 70 45  nippetOffsets(pE
36603 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 70 53 6e  xpr->pRight, pSn
36604 69 70 70 65 74 2c 20 70 69 4c 65 66 74 29 20 29  ippet, piLeft) )
36605 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
36606 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
36607 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
36608 20 43 6f 6d 70 75 74 65 20 61 6c 6c 20 6f 66 66   Compute all off
36609 73 65 74 73 20 66 6f 72 20 74 68 65 20 63 75 72  sets for the cur
3660a 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
3660b 71 75 65 72 79 2e 20 20 0a 2a 2a 20 49 66 20 74  query.  .** If t
3660c 68 65 20 6f 66 66 73 65 74 73 20 68 61 76 65 20  he offsets have 
3660d 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d  already been com
3660e 70 75 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74  puted, this rout
3660f 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
36610 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
36611 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73  nippetAllOffsets
36612 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72  (fulltext_cursor
36613 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 43 6f 6c   *p){.  int nCol
36614 75 6d 6e 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 75  umn;.  int iColu
36615 6d 6e 2c 20 69 3b 0a 20 20 69 6e 74 20 69 46 69  mn, i;.  int iFi
36616 72 73 74 2c 20 69 4c 61 73 74 3b 0a 20 20 69 6e  rst, iLast;.  in
36617 74 20 69 54 65 72 6d 20 3d 20 30 3b 0a 20 20 66  t iTerm = 0;.  f
36618 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 46  ulltext_vtab *pF
36619 74 73 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62  ts = cursor_vtab
3661a 28 70 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 73  (p);..  if( p->s
3661b 6e 69 70 70 65 74 2e 6e 4d 61 74 63 68 20 7c 7c  nippet.nMatch ||
3661c 20 70 2d 3e 70 45 78 70 72 3d 3d 30 20 29 7b 0a   p->pExpr==0 ){.
3661d 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
3661e 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 46 74 73    nColumn = pFts
3661f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 43 6f  ->nColumn;.  iCo
36620 6c 75 6d 6e 20 3d 20 28 70 2d 3e 69 43 75 72 73  lumn = (p->iCurs
36621 6f 72 54 79 70 65 20 2d 20 51 55 45 52 59 5f 46  orType - QUERY_F
36622 55 4c 4c 54 45 58 54 29 3b 0a 20 20 69 66 28 20  ULLTEXT);.  if( 
36623 69 43 6f 6c 75 6d 6e 3c 30 20 7c 7c 20 69 43 6f  iColumn<0 || iCo
36624 6c 75 6d 6e 3e 3d 6e 43 6f 6c 75 6d 6e 20 29 7b  lumn>=nColumn ){
36625 0a 20 20 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72  .    /* Look for
36626 20 6d 61 74 63 68 65 73 20 6f 76 65 72 20 61 6c   matches over al
36627 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  l columns of the
36628 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78   full-text index
36629 20 2a 2f 0a 20 20 20 20 69 46 69 72 73 74 20 3d   */.    iFirst =
3662a 20 30 3b 0a 20 20 20 20 69 4c 61 73 74 20 3d 20   0;.    iLast = 
3662b 6e 43 6f 6c 75 6d 6e 2d 31 3b 0a 20 20 7d 65 6c  nColumn-1;.  }el
3662c 73 65 7b 0a 20 20 20 20 2f 2a 20 4c 6f 6f 6b 20  se{.    /* Look 
3662d 66 6f 72 20 6d 61 74 63 68 65 73 20 69 6e 20 74  for matches in t
3662e 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f  he iColumn-th co
3662f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65  lumn of the inde
36630 78 20 6f 6e 6c 79 20 2a 2f 0a 20 20 20 20 69 46  x only */.    iF
36631 69 72 73 74 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a  irst = iColumn;.
36632 20 20 20 20 69 4c 61 73 74 20 3d 20 69 43 6f 6c      iLast = iCol
36633 75 6d 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  umn;.  }.  for(i
36634 3d 69 46 69 72 73 74 3b 20 69 3c 3d 69 4c 61 73  =iFirst; i<=iLas
36635 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e  t; i++){.    con
36636 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a 20  st char *zDoc;. 
36637 20 20 20 69 6e 74 20 6e 44 6f 63 3b 0a 20 20 20     int nDoc;.   
36638 20 7a 44 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63   zDoc = (const c
36639 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c  har*)sqlite3_col
3663a 75 6d 6e 5f 74 65 78 74 28 70 2d 3e 70 53 74 6d  umn_text(p->pStm
3663b 74 2c 20 69 2b 31 29 3b 0a 20 20 20 20 6e 44 6f  t, i+1);.    nDo
3663c 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  c = sqlite3_colu
3663d 6d 6e 5f 62 79 74 65 73 28 70 2d 3e 70 53 74 6d  mn_bytes(p->pStm
3663e 74 2c 20 69 2b 31 29 3b 0a 20 20 20 20 73 6e 69  t, i+1);.    sni
3663f 70 70 65 74 4f 66 66 73 65 74 73 4f 66 43 6f 6c  ppetOffsetsOfCol
36640 75 6d 6e 28 70 2c 20 26 70 2d 3e 73 6e 69 70 70  umn(p, &p->snipp
36641 65 74 2c 20 69 2c 20 7a 44 6f 63 2c 20 6e 44 6f  et, i, zDoc, nDo
36642 63 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65  c);.  }..  while
36643 28 20 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66  ( trimSnippetOff
36644 73 65 74 73 28 70 2d 3e 70 45 78 70 72 2c 20 26  sets(p->pExpr, &
36645 70 2d 3e 73 6e 69 70 70 65 74 2c 20 26 69 54 65  p->snippet, &iTe
36646 72 6d 29 20 29 7b 0a 20 20 20 20 69 54 65 72 6d  rm) ){.    iTerm
36647 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 0;.  }.}../*.
36648 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 69  ** Convert the i
36649 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68  nformation in th
3664a 65 20 61 4d 61 74 63 68 5b 5d 20 61 72 72 61 79  e aMatch[] array
3664b 20 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 0a   of the snippet.
3664c 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 74 72 69  ** into the stri
3664d 6e 67 20 7a 4f 66 66 73 65 74 5b 30 2e 2e 6e 4f  ng zOffset[0..nO
3664e 66 66 73 65 74 2d 31 5d 2e 20 54 68 69 73 20 73  ffset-1]. This s
3664f 74 72 69 6e 67 20 69 73 20 75 73 65 64 20 61 73  tring is used as
36650 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 20 6f  .** the return o
36651 66 20 74 68 65 20 53 51 4c 20 6f 66 66 73 65 74  f the SQL offset
36652 73 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  s() function..*/
36653 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69  .static void sni
36654 70 70 65 74 4f 66 66 73 65 74 54 65 78 74 28 53  ppetOffsetText(S
36655 6e 69 70 70 65 74 20 2a 70 29 7b 0a 20 20 69 6e  nippet *p){.  in
36656 74 20 69 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d  t i;.  int cnt =
36657 20 30 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66   0;.  StringBuff
36658 65 72 20 73 62 3b 0a 20 20 63 68 61 72 20 7a 42  er sb;.  char zB
36659 75 66 5b 32 30 30 5d 3b 0a 20 20 69 66 28 20 70  uf[200];.  if( p
3665a 2d 3e 7a 4f 66 66 73 65 74 20 29 20 72 65 74 75  ->zOffset ) retu
3665b 72 6e 3b 0a 20 20 69 6e 69 74 53 74 72 69 6e 67  rn;.  initString
3665c 42 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20 66  Buffer(&sb);.  f
3665d 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d 61  or(i=0; i<p->nMa
3665e 74 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  tch; i++){.    s
3665f 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74  truct snippetMat
36660 63 68 20 2a 70 4d 61 74 63 68 20 3d 20 26 70 2d  ch *pMatch = &p-
36661 3e 61 4d 61 74 63 68 5b 69 5d 3b 0a 20 20 20 20  >aMatch[i];.    
36662 69 66 28 20 70 4d 61 74 63 68 2d 3e 69 54 65 72  if( pMatch->iTer
36663 6d 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  m>=0 ){.      /*
36664 20 49 66 20 73 6e 69 70 70 65 74 4d 61 74 63 68   If snippetMatch
36665 2e 69 54 65 72 6d 20 69 73 20 6c 65 73 73 20 74  .iTerm is less t
36666 68 61 6e 20 30 2c 20 74 68 65 6e 20 74 68 65 20  han 0, then the 
36667 6d 61 74 63 68 20 77 61 73 20 0a 20 20 20 20 20  match was .     
36668 20 2a 2a 20 64 69 73 63 61 72 64 65 64 20 61 73   ** discarded as
36669 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73 73   part of process
3666a 69 6e 67 20 74 68 65 20 4e 45 41 52 20 6f 70 65  ing the NEAR ope
3666b 72 61 74 6f 72 20 28 73 65 65 20 74 68 65 20 0a  rator (see the .
3666c 20 20 20 20 20 20 2a 2a 20 74 72 69 6d 53 6e 69        ** trimSni
3666d 70 70 65 74 4f 66 66 73 65 74 73 46 6f 72 4e 65  ppetOffsetsForNe
3666e 61 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f  ar() function fo
3666f 72 20 64 65 74 61 69 6c 73 29 2e 20 49 67 6e 6f  r details). Igno
36670 72 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 74 20  re .      ** it 
36671 69 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20  in this case.   
36672 20 20 20 2a 2f 0a 20 20 20 20 20 20 7a 42 75 66     */.      zBuf
36673 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20  [0] = ' ';.     
36674 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
36675 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d 31  f(sizeof(zBuf)-1
36676 2c 20 26 7a 42 75 66 5b 63 6e 74 3e 30 5d 2c 20  , &zBuf[cnt>0], 
36677 22 25 64 20 25 64 20 25 64 20 25 64 22 2c 0a 20  "%d %d %d %d",. 
36678 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 2d           pMatch-
36679 3e 69 43 6f 6c 2c 20 70 4d 61 74 63 68 2d 3e 69  >iCol, pMatch->i
3667a 54 65 72 6d 2c 20 70 4d 61 74 63 68 2d 3e 69 53  Term, pMatch->iS
3667b 74 61 72 74 2c 20 70 4d 61 74 63 68 2d 3e 6e 42  tart, pMatch->nB
3667c 79 74 65 29 3b 0a 20 20 20 20 20 20 61 70 70 65  yte);.      appe
3667d 6e 64 28 26 73 62 2c 20 7a 42 75 66 29 3b 0a 20  nd(&sb, zBuf);. 
3667e 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20       cnt++;.    
3667f 7d 0a 20 20 7d 0a 20 20 70 2d 3e 7a 4f 66 66 73  }.  }.  p->zOffs
36680 65 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65  et = stringBuffe
36681 72 44 61 74 61 28 26 73 62 29 3b 0a 20 20 70 2d  rData(&sb);.  p-
36682 3e 6e 4f 66 66 73 65 74 20 3d 20 73 74 72 69 6e  >nOffset = strin
36683 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28 26 73  gBufferLength(&s
36684 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 7a 44 6f  b);.}../*.** zDo
36685 63 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 69 73 20  c[0..nDoc-1] is 
36686 70 68 72 61 73 65 20 6f 66 20 74 65 78 74 2e 20  phrase of text. 
36687 20 61 4d 61 74 63 68 5b 30 2e 2e 6e 4d 61 74 63   aMatch[0..nMatc
36688 68 2d 31 5d 20 61 72 65 20 61 20 73 65 74 0a 2a  h-1] are a set.*
36689 2a 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 77 6f  * of matching wo
3668a 72 64 73 20 73 6f 6d 65 20 6f 66 20 77 68 69 63  rds some of whic
3668b 68 20 6d 69 67 68 74 20 62 65 20 69 6e 20 7a 44  h might be in zD
3668c 6f 63 2e 20 20 7a 44 6f 63 20 69 73 20 63 6f 6c  oc.  zDoc is col
3668d 75 6d 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 43  umn.** number iC
3668e 6f 6c 2e 0a 2a 2a 0a 2a 2a 20 69 42 72 65 61 6b  ol..**.** iBreak
3668f 20 69 73 20 73 75 67 67 65 73 74 65 64 20 73 70   is suggested sp
36690 6f 74 20 69 6e 20 7a 44 6f 63 20 77 68 65 72 65  ot in zDoc where
36691 20 77 65 20 63 6f 75 6c 64 20 62 65 67 69 6e 20   we could begin 
36692 6f 72 20 65 6e 64 20 61 6e 0a 2a 2a 20 65 78 63  or end an.** exc
36693 65 72 70 74 2e 20 20 52 65 74 75 72 6e 20 61 20  erpt.  Return a 
36694 76 61 6c 75 65 20 73 69 6d 69 6c 61 72 20 74 6f  value similar to
36695 20 69 42 72 65 61 6b 20 62 75 74 20 70 6f 73 73   iBreak but poss
36696 69 62 6c 79 20 61 64 6a 75 73 74 65 64 0a 2a 2a  ibly adjusted.**
36697 20 74 6f 20 62 65 20 61 20 6c 69 74 74 6c 65 20   to be a little 
36698 6c 65 66 74 20 6f 72 20 72 69 67 68 74 20 73 6f  left or right so
36699 20 74 68 61 74 20 74 68 65 20 62 72 65 61 6b 20   that the break 
3669a 70 6f 69 6e 74 20 69 73 20 62 65 74 74 65 72 2e  point is better.
3669b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
3669c 6f 72 64 42 6f 75 6e 64 61 72 79 28 0a 20 20 69  ordBoundary(.  i
3669d 6e 74 20 69 42 72 65 61 6b 2c 20 20 20 20 20 20  nt iBreak,      
3669e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3669f 54 68 65 20 73 75 67 67 65 73 74 65 64 20 62 72  The suggested br
366a0 65 61 6b 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 63  eak point */.  c
366a1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 2c  onst char *zDoc,
366a2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
366a3 44 6f 63 75 6d 65 6e 74 20 74 65 78 74 20 2a 2f  Document text */
366a4 0a 20 20 69 6e 74 20 6e 44 6f 63 2c 20 20 20 20  .  int nDoc,    
366a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
366a6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
366a7 74 65 73 20 69 6e 20 7a 44 6f 63 5b 5d 20 2a 2f  tes in zDoc[] */
366a8 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65  .  struct snippe
366a9 74 4d 61 74 63 68 20 2a 61 4d 61 74 63 68 2c 20  tMatch *aMatch, 
366aa 20 2f 2a 20 4d 61 74 63 68 69 6e 67 20 77 6f 72   /* Matching wor
366ab 64 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 74  ds */.  int nMat
366ac 63 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ch,             
366ad 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
366ae 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 4d  of entries in aM
366af 61 74 63 68 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  atch[] */.  int 
366b0 69 43 6f 6c 20 20 20 20 20 20 20 20 20 20 20 20  iCol            
366b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
366b2 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66   column number f
366b3 6f 72 20 7a 44 6f 63 5b 5d 20 2a 2f 0a 29 7b 0a  or zDoc[] */.){.
366b4 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 69    int i;.  if( i
366b5 42 72 65 61 6b 3c 3d 31 30 20 29 7b 0a 20 20 20  Break<=10 ){.   
366b6 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
366b7 20 69 66 28 20 69 42 72 65 61 6b 3e 3d 6e 44 6f   if( iBreak>=nDo
366b8 63 2d 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75  c-10 ){.    retu
366b9 72 6e 20 6e 44 6f 63 3b 0a 20 20 7d 0a 20 20 66  rn nDoc;.  }.  f
366ba 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68  or(i=0; i<nMatch
366bb 20 26 26 20 61 4d 61 74 63 68 5b 69 5d 2e 69 43   && aMatch[i].iC
366bc 6f 6c 3c 69 43 6f 6c 3b 20 69 2b 2b 29 7b 7d 0a  ol<iCol; i++){}.
366bd 20 20 77 68 69 6c 65 28 20 69 3c 6e 4d 61 74 63    while( i<nMatc
366be 68 20 26 26 20 61 4d 61 74 63 68 5b 69 5d 2e 69  h && aMatch[i].i
366bf 53 74 61 72 74 2b 61 4d 61 74 63 68 5b 69 5d 2e  Start+aMatch[i].
366c0 6e 42 79 74 65 3c 69 42 72 65 61 6b 20 29 7b 20  nByte<iBreak ){ 
366c1 69 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 69 3c 6e  i++; }.  if( i<n
366c2 4d 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66 28  Match ){.    if(
366c3 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72   aMatch[i].iStar
366c4 74 3c 69 42 72 65 61 6b 2b 31 30 20 29 7b 0a 20  t<iBreak+10 ){. 
366c5 20 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61 74       return aMat
366c6 63 68 5b 69 5d 2e 69 53 74 61 72 74 3b 0a 20 20  ch[i].iStart;.  
366c7 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 30 20    }.    if( i>0 
366c8 26 26 20 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 69  && aMatch[i-1].i
366c9 53 74 61 72 74 2b 61 4d 61 74 63 68 5b 69 2d 31  Start+aMatch[i-1
366ca 5d 2e 6e 42 79 74 65 3e 3d 69 42 72 65 61 6b 20  ].nByte>=iBreak 
366cb 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
366cc 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 69 53 74 61  aMatch[i-1].iSta
366cd 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rt;.    }.  }.  
366ce 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 31 30 3b 20  for(i=1; i<=10; 
366cf 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 61  i++){.    if( sa
366d0 66 65 5f 69 73 73 70 61 63 65 28 7a 44 6f 63 5b  fe_isspace(zDoc[
366d1 69 42 72 65 61 6b 2d 69 5d 29 20 29 7b 0a 20 20  iBreak-i]) ){.  
366d2 20 20 20 20 72 65 74 75 72 6e 20 69 42 72 65 61      return iBrea
366d3 6b 20 2d 20 69 20 2b 20 31 3b 0a 20 20 20 20 7d  k - i + 1;.    }
366d4 0a 20 20 20 20 69 66 28 20 73 61 66 65 5f 69 73  .    if( safe_is
366d5 73 70 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61  space(zDoc[iBrea
366d6 6b 2b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72  k+i]) ){.      r
366d7 65 74 75 72 6e 20 69 42 72 65 61 6b 20 2b 20 69  eturn iBreak + i
366d8 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   + 1;.    }.  }.
366d9 20 20 72 65 74 75 72 6e 20 69 42 72 65 61 6b 3b    return iBreak;
366da 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  .}..../*.** Allo
366db 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 53  wed values for S
366dc 6e 69 70 70 65 74 2e 61 4d 61 74 63 68 5b 5d 2e  nippet.aMatch[].
366dd 73 6e 53 74 61 74 75 73 0a 2a 2f 0a 23 64 65 66  snStatus.*/.#def
366de 69 6e 65 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f  ine SNIPPET_IGNO
366df 52 45 20 20 30 20 20 20 2f 2a 20 49 74 20 69 73  RE  0   /* It is
366e0 20 6f 6b 20 74 6f 20 6f 6d 69 74 20 74 68 69 73   ok to omit this
366e1 20 6d 61 74 63 68 20 66 72 6f 6d 20 74 68 65 20   match from the 
366e2 73 6e 69 70 70 65 74 20 2a 2f 0a 23 64 65 66 69  snippet */.#defi
366e3 6e 65 20 53 4e 49 50 50 45 54 5f 44 45 53 49 52  ne SNIPPET_DESIR
366e4 45 44 20 31 20 20 20 2f 2a 20 57 65 20 77 61 6e  ED 1   /* We wan
366e5 74 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 69  t to include thi
366e6 73 20 6d 61 74 63 68 20 69 6e 20 74 68 65 20 73  s match in the s
366e7 6e 69 70 70 65 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  nippet */../*.**
366e8 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65   Generate the te
366e9 78 74 20 6f 66 20 61 20 73 6e 69 70 70 65 74 2e  xt of a snippet.
366ea 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
366eb 73 6e 69 70 70 65 74 54 65 78 74 28 0a 20 20 66  snippetText(.  f
366ec 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
366ed 70 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 54 68  pCursor,   /* Th
366ee 65 20 63 75 72 73 6f 72 20 77 65 20 6e 65 65 64  e cursor we need
366ef 20 74 68 65 20 73 6e 69 70 70 65 74 20 66 6f 72   the snippet for
366f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
366f1 20 2a 7a 53 74 61 72 74 4d 61 72 6b 2c 20 20 20   *zStartMark,   
366f2 20 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f 20 61    /* Markup to a
366f3 70 70 65 61 72 20 62 65 66 6f 72 65 20 65 61 63  ppear before eac
366f4 68 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f 6e  h match */.  con
366f5 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 4d 61 72  st char *zEndMar
366f6 6b 2c 20 20 20 20 20 20 20 2f 2a 20 4d 61 72 6b  k,       /* Mark
366f7 75 70 20 74 6f 20 61 70 70 65 61 72 20 61 66 74  up to appear aft
366f8 65 72 20 65 61 63 68 20 6d 61 74 63 68 20 2a 2f  er each match */
366f9 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
366fa 45 6c 6c 69 70 73 69 73 20 20 20 20 20 20 20 2f  Ellipsis       /
366fb 2a 20 45 6c 6c 69 70 73 69 73 20 6d 61 72 6b 20  * Ellipsis mark 
366fc 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  */.){.  int i, j
366fd 3b 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70  ;.  struct snipp
366fe 65 74 4d 61 74 63 68 20 2a 61 4d 61 74 63 68 3b  etMatch *aMatch;
366ff 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 0a 20  .  int nMatch;. 
36700 20 69 6e 74 20 6e 44 65 73 69 72 65 64 3b 0a 20   int nDesired;. 
36701 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62   StringBuffer sb
36702 3b 0a 20 20 69 6e 74 20 74 61 69 6c 43 6f 6c 3b  ;.  int tailCol;
36703 0a 20 20 69 6e 74 20 74 61 69 6c 4f 66 66 73 65  .  int tailOffse
36704 74 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20  t;.  int iCol;. 
36705 20 69 6e 74 20 6e 44 6f 63 3b 0a 20 20 63 6f 6e   int nDoc;.  con
36706 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a 20  st char *zDoc;. 
36707 20 69 6e 74 20 69 53 74 61 72 74 2c 20 69 45 6e   int iStart, iEn
36708 64 3b 0a 20 20 69 6e 74 20 74 61 69 6c 45 6c 6c  d;.  int tailEll
36709 69 70 73 69 73 20 3d 20 30 3b 0a 20 20 69 6e 74  ipsis = 0;.  int
3670a 20 69 4d 61 74 63 68 3b 0a 20 20 0a 0a 20 20 73   iMatch;.  ..  s
3670b 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 72  qlite3_free(pCur
3670c 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 53 6e  sor->snippet.zSn
3670d 69 70 70 65 74 29 3b 0a 20 20 70 43 75 72 73 6f  ippet);.  pCurso
3670e 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 53 6e 69 70  r->snippet.zSnip
3670f 70 65 74 20 3d 20 30 3b 0a 20 20 61 4d 61 74 63  pet = 0;.  aMatc
36710 68 20 3d 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69  h = pCursor->sni
36711 70 70 65 74 2e 61 4d 61 74 63 68 3b 0a 20 20 6e  ppet.aMatch;.  n
36712 4d 61 74 63 68 20 3d 20 70 43 75 72 73 6f 72 2d  Match = pCursor-
36713 3e 73 6e 69 70 70 65 74 2e 6e 4d 61 74 63 68 3b  >snippet.nMatch;
36714 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66  .  initStringBuf
36715 66 65 72 28 26 73 62 29 3b 0a 0a 20 20 66 6f 72  fer(&sb);..  for
36716 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 3b 20  (i=0; i<nMatch; 
36717 69 2b 2b 29 7b 0a 20 20 20 20 61 4d 61 74 63 68  i++){.    aMatch
36718 5b 69 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20 53  [i].snStatus = S
36719 4e 49 50 50 45 54 5f 49 47 4e 4f 52 45 3b 0a 20  NIPPET_IGNORE;. 
3671a 20 7d 0a 20 20 6e 44 65 73 69 72 65 64 20 3d 20   }.  nDesired = 
3671b 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
3671c 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 3b 20 69  FTS3_ROTOR_SZ; i
3671d 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 30  ++){.    for(j=0
3671e 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a 2b 2b 29  ; j<nMatch; j++)
3671f 7b 0a 20 20 20 20 20 20 69 66 28 20 61 4d 61 74  {.      if( aMat
36720 63 68 5b 6a 5d 2e 69 54 65 72 6d 3d 3d 69 20 29  ch[j].iTerm==i )
36721 7b 0a 20 20 20 20 20 20 20 20 61 4d 61 74 63 68  {.        aMatch
36722 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20 53  [j].snStatus = S
36723 4e 49 50 50 45 54 5f 44 45 53 49 52 45 44 3b 0a  NIPPET_DESIRED;.
36724 20 20 20 20 20 20 20 20 6e 44 65 73 69 72 65 64          nDesired
36725 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  ++;.        brea
36726 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
36727 0a 20 20 7d 0a 0a 20 20 69 4d 61 74 63 68 20 3d  .  }..  iMatch =
36728 20 30 3b 0a 20 20 74 61 69 6c 43 6f 6c 20 3d 20   0;.  tailCol = 
36729 2d 31 3b 0a 20 20 74 61 69 6c 4f 66 66 73 65 74  -1;.  tailOffset
3672a 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b   = 0;.  for(i=0;
3672b 20 69 3c 6e 4d 61 74 63 68 20 26 26 20 6e 44 65   i<nMatch && nDe
3672c 73 69 72 65 64 3e 30 3b 20 69 2b 2b 29 7b 0a 20  sired>0; i++){. 
3672d 20 20 20 69 66 28 20 61 4d 61 74 63 68 5b 69 5d     if( aMatch[i]
3672e 2e 73 6e 53 74 61 74 75 73 21 3d 53 4e 49 50 50  .snStatus!=SNIPP
3672f 45 54 5f 44 45 53 49 52 45 44 20 29 20 63 6f 6e  ET_DESIRED ) con
36730 74 69 6e 75 65 3b 0a 20 20 20 20 6e 44 65 73 69  tinue;.    nDesi
36731 72 65 64 2d 2d 3b 0a 20 20 20 20 69 43 6f 6c 20  red--;.    iCol 
36732 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 43 6f 6c  = aMatch[i].iCol
36733 3b 0a 20 20 20 20 7a 44 6f 63 20 3d 20 28 63 6f  ;.    zDoc = (co
36734 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65  nst char*)sqlite
36735 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 43  3_column_text(pC
36736 75 72 73 6f 72 2d 3e 70 53 74 6d 74 2c 20 69 43  ursor->pStmt, iC
36737 6f 6c 2b 31 29 3b 0a 20 20 20 20 6e 44 6f 63 20  ol+1);.    nDoc 
36738 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
36739 5f 62 79 74 65 73 28 70 43 75 72 73 6f 72 2d 3e  _bytes(pCursor->
3673a 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29 3b 0a  pStmt, iCol+1);.
3673b 20 20 20 20 69 53 74 61 72 74 20 3d 20 61 4d 61      iStart = aMa
3673c 74 63 68 5b 69 5d 2e 69 53 74 61 72 74 20 2d 20  tch[i].iStart - 
3673d 34 30 3b 0a 20 20 20 20 69 53 74 61 72 74 20 3d  40;.    iStart =
3673e 20 77 6f 72 64 42 6f 75 6e 64 61 72 79 28 69 53   wordBoundary(iS
3673f 74 61 72 74 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63  tart, zDoc, nDoc
36740 2c 20 61 4d 61 74 63 68 2c 20 6e 4d 61 74 63 68  , aMatch, nMatch
36741 2c 20 69 43 6f 6c 29 3b 0a 20 20 20 20 69 66 28  , iCol);.    if(
36742 20 69 53 74 61 72 74 3c 3d 31 30 20 29 7b 0a 20   iStart<=10 ){. 
36743 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 30 3b       iStart = 0;
36744 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
36745 43 6f 6c 3d 3d 74 61 69 6c 43 6f 6c 20 26 26 20  Col==tailCol && 
36746 69 53 74 61 72 74 3c 3d 74 61 69 6c 4f 66 66 73  iStart<=tailOffs
36747 65 74 2b 32 30 20 29 7b 0a 20 20 20 20 20 20 69  et+20 ){.      i
36748 53 74 61 72 74 20 3d 20 74 61 69 6c 4f 66 66 73  Start = tailOffs
36749 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  et;.    }.    if
3674a 28 20 28 69 43 6f 6c 21 3d 74 61 69 6c 43 6f 6c  ( (iCol!=tailCol
3674b 20 26 26 20 74 61 69 6c 43 6f 6c 3e 3d 30 29 20   && tailCol>=0) 
3674c 7c 7c 20 69 53 74 61 72 74 21 3d 74 61 69 6c 4f  || iStart!=tailO
3674d 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 74  ffset ){.      t
3674e 72 69 6d 57 68 69 74 65 53 70 61 63 65 28 26 73  rimWhiteSpace(&s
3674f 62 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e 64  b);.      append
36750 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b  WhiteSpace(&sb);
36751 0a 20 20 20 20 20 20 61 70 70 65 6e 64 28 26 73  .      append(&s
36752 62 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20  b, zEllipsis);. 
36753 20 20 20 20 20 61 70 70 65 6e 64 57 68 69 74 65       appendWhite
36754 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20  Space(&sb);.    
36755 7d 0a 20 20 20 20 69 45 6e 64 20 3d 20 61 4d 61  }.    iEnd = aMa
36756 74 63 68 5b 69 5d 2e 69 53 74 61 72 74 20 2b 20  tch[i].iStart + 
36757 61 4d 61 74 63 68 5b 69 5d 2e 6e 42 79 74 65 20  aMatch[i].nByte 
36758 2b 20 34 30 3b 0a 20 20 20 20 69 45 6e 64 20 3d  + 40;.    iEnd =
36759 20 77 6f 72 64 42 6f 75 6e 64 61 72 79 28 69 45   wordBoundary(iE
3675a 6e 64 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20  nd, zDoc, nDoc, 
3675b 61 4d 61 74 63 68 2c 20 6e 4d 61 74 63 68 2c 20  aMatch, nMatch, 
3675c 69 43 6f 6c 29 3b 0a 20 20 20 20 69 66 28 20 69  iCol);.    if( i
3675d 45 6e 64 3e 3d 6e 44 6f 63 2d 31 30 20 29 7b 0a  End>=nDoc-10 ){.
3675e 20 20 20 20 20 20 69 45 6e 64 20 3d 20 6e 44 6f        iEnd = nDo
3675f 63 3b 0a 20 20 20 20 20 20 74 61 69 6c 45 6c 6c  c;.      tailEll
36760 69 70 73 69 73 20 3d 20 30 3b 0a 20 20 20 20 7d  ipsis = 0;.    }
36761 65 6c 73 65 7b 0a 20 20 20 20 20 20 74 61 69 6c  else{.      tail
36762 45 6c 6c 69 70 73 69 73 20 3d 20 31 3b 0a 20 20  Ellipsis = 1;.  
36763 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 69    }.    while( i
36764 4d 61 74 63 68 3c 6e 4d 61 74 63 68 20 26 26 20  Match<nMatch && 
36765 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69  aMatch[iMatch].i
36766 43 6f 6c 3c 69 43 6f 6c 20 29 7b 20 69 4d 61 74  Col<iCol ){ iMat
36767 63 68 2b 2b 3b 20 7d 0a 20 20 20 20 77 68 69 6c  ch++; }.    whil
36768 65 28 20 69 53 74 61 72 74 3c 69 45 6e 64 20 29  e( iStart<iEnd )
36769 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69  {.      while( i
3676a 4d 61 74 63 68 3c 6e 4d 61 74 63 68 20 26 26 20  Match<nMatch && 
3676b 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69  aMatch[iMatch].i
3676c 53 74 61 72 74 3c 69 53 74 61 72 74 0a 20 20 20  Start<iStart.   
3676d 20 20 20 20 20 20 20 20 20 20 26 26 20 61 4d 61            && aMa
3676e 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 43 6f 6c  tch[iMatch].iCol
3676f 3c 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  <=iCol ){.      
36770 20 20 69 4d 61 74 63 68 2b 2b 3b 0a 20 20 20 20    iMatch++;.    
36771 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 4d    }.      if( iM
36772 61 74 63 68 3c 6e 4d 61 74 63 68 20 26 26 20 61  atch<nMatch && a
36773 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 53  Match[iMatch].iS
36774 74 61 72 74 3c 69 45 6e 64 0a 20 20 20 20 20 20  tart<iEnd.      
36775 20 20 20 20 20 20 20 26 26 20 61 4d 61 74 63 68         && aMatch
36776 5b 69 4d 61 74 63 68 5d 2e 69 43 6f 6c 3d 3d 69  [iMatch].iCol==i
36777 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  Col ){.        n
36778 61 70 70 65 6e 64 28 26 73 62 2c 20 26 7a 44 6f  append(&sb, &zDo
36779 63 5b 69 53 74 61 72 74 5d 2c 20 61 4d 61 74 63  c[iStart], aMatc
3677a 68 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74  h[iMatch].iStart
3677b 20 2d 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20   - iStart);.    
3677c 20 20 20 20 69 53 74 61 72 74 20 3d 20 61 4d 61      iStart = aMa
3677d 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61  tch[iMatch].iSta
3677e 72 74 3b 0a 20 20 20 20 20 20 20 20 61 70 70 65  rt;.        appe
3677f 6e 64 28 26 73 62 2c 20 7a 53 74 61 72 74 4d 61  nd(&sb, zStartMa
36780 72 6b 29 3b 0a 20 20 20 20 20 20 20 20 6e 61 70  rk);.        nap
36781 70 65 6e 64 28 26 73 62 2c 20 26 7a 44 6f 63 5b  pend(&sb, &zDoc[
36782 69 53 74 61 72 74 5d 2c 20 61 4d 61 74 63 68 5b  iStart], aMatch[
36783 69 4d 61 74 63 68 5d 2e 6e 42 79 74 65 29 3b 0a  iMatch].nByte);.
36784 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 28 26          append(&
36785 73 62 2c 20 7a 45 6e 64 4d 61 72 6b 29 3b 0a 20  sb, zEndMark);. 
36786 20 20 20 20 20 20 20 69 53 74 61 72 74 20 2b 3d         iStart +=
36787 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e   aMatch[iMatch].
36788 6e 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 66  nByte;.        f
36789 6f 72 28 6a 3d 69 4d 61 74 63 68 2b 31 3b 20 6a  or(j=iMatch+1; j
3678a 3c 6e 4d 61 74 63 68 3b 20 6a 2b 2b 29 7b 0a 20  <nMatch; j++){. 
3678b 20 20 20 20 20 20 20 20 20 69 66 28 20 61 4d 61           if( aMa
3678c 74 63 68 5b 6a 5d 2e 69 54 65 72 6d 3d 3d 61 4d  tch[j].iTerm==aM
3678d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 54 65  atch[iMatch].iTe
3678e 72 6d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  rm.             
3678f 20 26 26 20 61 4d 61 74 63 68 5b 6a 5d 2e 73 6e   && aMatch[j].sn
36790 53 74 61 74 75 73 3d 3d 53 4e 49 50 50 45 54 5f  Status==SNIPPET_
36791 44 45 53 49 52 45 44 20 29 7b 0a 20 20 20 20 20  DESIRED ){.     
36792 20 20 20 20 20 20 20 6e 44 65 73 69 72 65 64 2d         nDesired-
36793 2d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  -;.            a
36794 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75  Match[j].snStatu
36795 73 20 3d 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f  s = SNIPPET_IGNO
36796 52 45 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  RE;.          }.
36797 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
36798 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e  }else{.        n
36799 61 70 70 65 6e 64 28 26 73 62 2c 20 26 7a 44 6f  append(&sb, &zDo
3679a 63 5b 69 53 74 61 72 74 5d 2c 20 69 45 6e 64 20  c[iStart], iEnd 
3679b 2d 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20  - iStart);.     
3679c 20 20 20 69 53 74 61 72 74 20 3d 20 69 45 6e 64     iStart = iEnd
3679d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
3679e 20 20 20 20 74 61 69 6c 43 6f 6c 20 3d 20 69 43      tailCol = iC
3679f 6f 6c 3b 0a 20 20 20 20 74 61 69 6c 4f 66 66 73  ol;.    tailOffs
367a0 65 74 20 3d 20 69 45 6e 64 3b 0a 20 20 7d 0a 20  et = iEnd;.  }. 
367a1 20 74 72 69 6d 57 68 69 74 65 53 70 61 63 65 28   trimWhiteSpace(
367a2 26 73 62 29 3b 0a 20 20 69 66 28 20 74 61 69 6c  &sb);.  if( tail
367a3 45 6c 6c 69 70 73 69 73 20 29 7b 0a 20 20 20 20  Ellipsis ){.    
367a4 61 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65  appendWhiteSpace
367a5 28 26 73 62 29 3b 0a 20 20 20 20 61 70 70 65 6e  (&sb);.    appen
367a6 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73 69 73  d(&sb, zEllipsis
367a7 29 3b 0a 20 20 7d 0a 20 20 70 43 75 72 73 6f 72  );.  }.  pCursor
367a8 2d 3e 73 6e 69 70 70 65 74 2e 7a 53 6e 69 70 70  ->snippet.zSnipp
367a9 65 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65  et = stringBuffe
367aa 72 44 61 74 61 28 26 73 62 29 3b 0a 20 20 70 43  rData(&sb);.  pC
367ab 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 6e  ursor->snippet.n
367ac 53 6e 69 70 70 65 74 20 3d 20 73 74 72 69 6e 67  Snippet = string
367ad 42 75 66 66 65 72 4c 65 6e 67 74 68 28 26 73 62  BufferLength(&sb
367ae 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  );.}.../*.** Clo
367af 73 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20  se the cursor.  
367b0 46 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  For additional i
367b1 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 65 65 20 74  nformation see t
367b2 68 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  he documentation
367b3 0a 2a 2a 20 6f 6e 20 74 68 65 20 78 43 6c 6f 73  .** on the xClos
367b4 65 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  e method of the 
367b5 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  virtual table in
367b6 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 73 74 61 74  terface..*/.stat
367b7 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43  ic int fulltextC
367b8 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 74 61  lose(sqlite3_vta
367b9 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  b_cursor *pCurso
367ba 72 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63  r){.  fulltext_c
367bb 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c  ursor *c = (full
367bc 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 70  text_cursor *) p
367bd 43 75 72 73 6f 72 3b 0a 20 20 46 54 53 54 52 41  Cursor;.  FTSTRA
367be 43 45 28 28 22 46 54 53 33 20 43 6c 6f 73 65 20  CE(("FTS3 Close 
367bf 25 70 5c 6e 22 2c 20 63 29 29 3b 0a 20 20 73 71  %p\n", c));.  sq
367c0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 63  lite3_finalize(c
367c1 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69  ->pStmt);.  sqli
367c2 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28  te3Fts3ExprFree(
367c3 63 2d 3e 70 45 78 70 72 29 3b 0a 20 20 73 6e 69  c->pExpr);.  sni
367c4 70 70 65 74 43 6c 65 61 72 28 26 63 2d 3e 73 6e  ppetClear(&c->sn
367c5 69 70 70 65 74 29 3b 0a 20 20 69 66 28 20 63 2d  ippet);.  if( c-
367c6 3e 72 65 73 75 6c 74 2e 6e 44 61 74 61 21 3d 30  >result.nData!=0
367c7 20 29 7b 0a 20 20 20 20 64 6c 72 44 65 73 74 72   ){.    dlrDestr
367c8 6f 79 28 26 63 2d 3e 72 65 61 64 65 72 29 3b 0a  oy(&c->reader);.
367c9 20 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72    }.  dataBuffer
367ca 44 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 73 75  Destroy(&c->resu
367cb 6c 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  lt);.  sqlite3_f
367cc 72 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e  ree(c);.  return
367cd 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73   SQLITE_OK;.}..s
367ce 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
367cf 78 74 4e 65 78 74 28 73 71 6c 69 74 65 33 5f 76  xtNext(sqlite3_v
367d0 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  tab_cursor *pCur
367d1 73 6f 72 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  sor){.  fulltext
367d2 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75  _cursor *c = (fu
367d3 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29  lltext_cursor *)
367d4 20 70 43 75 72 73 6f 72 3b 0a 20 20 69 6e 74 20   pCursor;.  int 
367d5 72 63 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28  rc;..  FTSTRACE(
367d6 28 22 46 54 53 33 20 4e 65 78 74 20 25 70 5c 6e  ("FTS3 Next %p\n
367d7 22 2c 20 70 43 75 72 73 6f 72 29 29 3b 0a 20 20  ", pCursor));.  
367d8 73 6e 69 70 70 65 74 43 6c 65 61 72 28 26 63 2d  snippetClear(&c-
367d9 3e 73 6e 69 70 70 65 74 29 3b 0a 20 20 69 66 28  >snippet);.  if(
367da 20 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20   c->iCursorType 
367db 3c 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54  < QUERY_FULLTEXT
367dc 20 29 7b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28   ){.    /* TODO(
367dd 73 68 65 73 73 29 20 48 61 6e 64 6c 65 20 53 51  shess) Handle SQ
367de 4c 49 54 45 5f 53 43 48 45 4d 41 20 41 4e 44 20  LITE_SCHEMA AND 
367df 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a  SQLITE_BUSY. */.
367e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
367e1 5f 73 74 65 70 28 63 2d 3e 70 53 74 6d 74 29 3b  _step(c->pStmt);
367e2 0a 20 20 20 20 73 77 69 74 63 68 28 20 72 63 20  .    switch( rc 
367e3 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51  ){.      case SQ
367e4 4c 49 54 45 5f 52 4f 57 3a 0a 20 20 20 20 20 20  LITE_ROW:.      
367e5 20 20 63 2d 3e 65 6f 66 20 3d 20 30 3b 0a 20 20    c->eof = 0;.  
367e6 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
367e7 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 63 61  ITE_OK;.      ca
367e8 73 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a  se SQLITE_DONE:.
367e9 20 20 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d          c->eof =
367ea 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   1;.        retu
367eb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
367ec 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
367ed 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b       c->eof = 1;
367ee 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
367ef 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c  rc;.    }.  } el
367f0 73 65 20 7b 20 20 2f 2a 20 66 75 6c 6c 2d 74 65  se {  /* full-te
367f1 78 74 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20  xt query */.    
367f2 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
367f3 65 74 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  et(c->pStmt);.  
367f4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
367f5 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
367f6 0a 0a 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73  ..    if( c->res
367f7 75 6c 74 2e 6e 44 61 74 61 3d 3d 30 20 7c 7c 20  ult.nData==0 || 
367f8 64 6c 72 41 74 45 6e 64 28 26 63 2d 3e 72 65 61  dlrAtEnd(&c->rea
367f9 64 65 72 29 20 29 7b 0a 20 20 20 20 20 20 63 2d  der) ){.      c-
367fa 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  >eof = 1;.      
367fb 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
367fc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
367fd 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
367fe 74 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 31 2c  t64(c->pStmt, 1,
367ff 20 64 6c 72 44 6f 63 69 64 28 26 63 2d 3e 72 65   dlrDocid(&c->re
36800 61 64 65 72 29 29 3b 0a 20 20 20 20 64 6c 72 53  ader));.    dlrS
36801 74 65 70 28 26 63 2d 3e 72 65 61 64 65 72 29 3b  tep(&c->reader);
36802 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
36803 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
36804 72 63 3b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28  rc;.    /* TODO(
36805 73 68 65 73 73 29 20 48 61 6e 64 6c 65 20 53 51  shess) Handle SQ
36806 4c 49 54 45 5f 53 43 48 45 4d 41 20 41 4e 44 20  LITE_SCHEMA AND 
36807 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a  SQLITE_BUSY. */.
36808 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
36809 5f 73 74 65 70 28 63 2d 3e 70 53 74 6d 74 29 3b  _step(c->pStmt);
3680a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
3680b 49 54 45 5f 52 4f 57 20 29 7b 20 20 20 2f 2a 20  ITE_ROW ){   /* 
3680c 74 68 65 20 63 61 73 65 20 77 65 20 65 78 70 65  the case we expe
3680d 63 74 20 2a 2f 0a 20 20 20 20 20 20 63 2d 3e 65  ct */.      c->e
3680e 6f 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65  of = 0;.      re
3680f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
36810 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 61 6e 20      }.    /* an 
36811 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 3b 20  error occurred; 
36812 61 62 6f 72 74 20 2a 2f 0a 20 20 20 20 72 65 74  abort */.    ret
36813 75 72 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  urn rc==SQLITE_D
36814 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 45 52 52  ONE ? SQLITE_ERR
36815 4f 52 20 3a 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a  OR : rc;.  }.}..
36816 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
36817 49 66 20 77 65 20 70 75 73 68 65 64 20 4c 65 61  If we pushed Lea
36818 66 52 65 61 64 65 72 20 74 6f 20 74 68 65 20 74  fReader to the t
36819 6f 70 20 6f 66 20 74 68 65 20 66 69 6c 65 2c 20  op of the file, 
3681a 6f 72 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72  or to.** another
3681b 20 66 69 6c 65 2c 20 74 65 72 6d 5f 73 65 6c 65   file, term_sele
3681c 63 74 28 29 20 63 6f 75 6c 64 20 62 65 20 70 75  ct() could be pu
3681d 73 68 65 64 20 61 62 6f 76 65 0a 2a 2a 20 64 6f  shed above.** do
3681e 63 4c 69 73 74 4f 66 54 65 72 6d 28 29 2e 0a 2a  cListOfTerm()..*
3681f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 72  /.static int ter
36820 6d 53 65 6c 65 63 74 28 66 75 6c 6c 74 65 78 74  mSelect(fulltext
36821 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 43  _vtab *v, int iC
36822 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 20 20  olumn,.         
36823 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
36824 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
36825 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
36826 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
36827 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44                 D
36828 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65  ocListType iType
36829 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75  , DataBuffer *ou
3682a 74 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75  t);../* .** Retu
3682b 72 6e 20 61 20 44 6f 63 4c 69 73 74 20 63 6f 72  rn a DocList cor
3682c 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68  responding to th
3682d 65 20 70 68 72 61 73 65 20 2a 70 50 68 72 61 73  e phrase *pPhras
3682e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73  e..**.** The res
3682f 75 6c 74 69 6e 67 20 44 4c 5f 44 4f 43 49 44 53  ulting DL_DOCIDS
36830 20 64 6f 63 6c 69 73 74 20 69 73 20 73 74 6f 72   doclist is stor
36831 65 64 20 69 6e 20 70 52 65 73 75 6c 74 2c 20 77  ed in pResult, w
36832 68 69 63 68 20 69 73 0a 2a 2a 20 6f 76 65 72 77  hich is.** overw
36833 72 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69  ritten..*/.stati
36834 63 20 69 6e 74 20 64 6f 63 4c 69 73 74 4f 66 50  c int docListOfP
36835 68 72 61 73 65 28 0a 20 20 66 75 6c 6c 74 65 78  hrase(.  fulltex
36836 74 5f 76 74 61 62 20 2a 70 54 61 62 2c 20 20 20  t_vtab *pTab,   
36837 2f 2a 20 54 68 65 20 66 75 6c 6c 20 74 65 78 74  /* The full text
36838 20 69 6e 64 65 78 20 2a 2f 0a 20 20 46 74 73 33   index */.  Fts3
36839 50 68 72 61 73 65 20 2a 70 50 68 72 61 73 65 2c  Phrase *pPhrase,
3683a 20 20 20 2f 2a 20 50 68 72 61 73 65 20 74 6f 20     /* Phrase to 
3683b 72 65 74 75 72 6e 20 61 20 64 6f 63 6c 69 73 74  return a doclist
3683c 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
3683d 6f 20 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79  o */.  DocListTy
3683e 70 65 20 65 4c 69 73 74 54 79 70 65 2c 20 2f 2a  pe eListType, /*
3683f 20 45 69 74 68 65 72 20 44 4c 5f 44 4f 43 49 44   Either DL_DOCID
36840 53 20 6f 72 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  S or DL_POSITION
36841 53 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65  S */.  DataBuffe
36842 72 20 2a 70 52 65 73 75 6c 74 20 20 20 20 2f 2a  r *pResult    /*
36843 20 57 72 69 74 65 20 74 68 65 20 72 65 73 75 6c   Write the resul
36844 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  t here */.){.  i
36845 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20  nt ii;.  int rc 
36846 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
36847 6e 74 20 69 43 6f 6c 20 3d 20 70 50 68 72 61 73  nt iCol = pPhras
36848 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 44 6f  e->iColumn;.  Do
36849 63 4c 69 73 74 54 79 70 65 20 65 54 79 70 65 20  cListType eType 
3684a 3d 20 65 4c 69 73 74 54 79 70 65 3b 0a 20 20 61  = eListType;.  a
3684b 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d 44 4c  ssert( eType==DL
3684c 5f 50 4f 53 49 54 49 4f 4e 53 20 7c 7c 20 65 54  _POSITIONS || eT
3684d 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 29  ype==DL_DOCIDS )
3684e 3b 0a 20 20 69 66 28 20 70 50 68 72 61 73 65 2d  ;.  if( pPhrase-
3684f 3e 6e 54 6f 6b 65 6e 3e 31 20 29 7b 0a 20 20 20  >nToken>1 ){.   
36850 20 65 54 79 70 65 20 3d 20 44 4c 5f 50 4f 53 49   eType = DL_POSI
36851 54 49 4f 4e 53 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TIONS;.  }..  /*
36852 20 54 68 69 73 20 63 6f 64 65 20 73 68 6f 75 6c   This code shoul
36853 64 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c 65  d never be calle
36854 64 20 77 69 74 68 20 62 75 66 66 65 72 65 64 20  d with buffered 
36855 75 70 64 61 74 65 73 2e 20 2a 2f 0a 20 20 61 73  updates. */.  as
36856 73 65 72 74 28 20 70 54 61 62 2d 3e 6e 50 65 6e  sert( pTab->nPen
36857 64 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a 0a 20  dingData<0 );.. 
36858 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53   for(ii=0; rc==S
36859 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 70  QLITE_OK && ii<p
3685a 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20  Phrase->nToken; 
3685b 69 69 2b 2b 29 7b 0a 20 20 20 20 44 61 74 61 42  ii++){.    DataB
3685c 75 66 66 65 72 20 74 6d 70 3b 0a 20 20 20 20 73  uffer tmp;.    s
3685d 74 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65  truct PhraseToke
3685e 6e 20 2a 70 20 3d 20 26 70 50 68 72 61 73 65 2d  n *p = &pPhrase-
3685f 3e 61 54 6f 6b 65 6e 5b 69 69 5d 3b 0a 20 20 20  >aToken[ii];.   
36860 20 72 63 20 3d 20 74 65 72 6d 53 65 6c 65 63 74   rc = termSelect
36861 28 70 54 61 62 2c 20 69 43 6f 6c 2c 20 70 2d 3e  (pTab, iCol, p->
36862 7a 2c 20 70 2d 3e 6e 2c 20 70 2d 3e 69 73 50 72  z, p->n, p->isPr
36863 65 66 69 78 2c 20 65 54 79 70 65 2c 20 26 74 6d  efix, eType, &tm
36864 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  p);.    if( rc==
36865 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
36866 20 20 20 69 66 28 20 69 69 3d 3d 30 20 29 7b 0a     if( ii==0 ){.
36867 20 20 20 20 20 20 20 20 2a 70 52 65 73 75 6c 74          *pResult
36868 20 3d 20 74 6d 70 3b 0a 20 20 20 20 20 20 7d 65   = tmp;.      }e
36869 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 44 61 74  lse{.        Dat
3686a 61 42 75 66 66 65 72 20 72 65 73 20 3d 20 2a 70  aBuffer res = *p
3686b 52 65 73 75 6c 74 3b 0a 20 20 20 20 20 20 20 20  Result;.        
3686c 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 70  dataBufferInit(p
3686d 52 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20 20 20  Result, 0);.    
3686e 20 20 20 20 69 66 28 20 69 69 3d 3d 28 70 50 68      if( ii==(pPh
3686f 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 2d 31 29 20  rase->nToken-1) 
36870 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65 54 79  ){.          eTy
36871 70 65 20 3d 20 65 4c 69 73 74 54 79 70 65 3b 0a  pe = eListType;.
36872 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
36873 20 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65 4d    docListPhraseM
36874 65 72 67 65 28 0a 20 20 20 20 20 20 20 20 20 20  erge(.          
36875 72 65 73 2e 70 44 61 74 61 2c 20 72 65 73 2e 6e  res.pData, res.n
36876 44 61 74 61 2c 20 74 6d 70 2e 70 44 61 74 61 2c  Data, tmp.pData,
36877 20 74 6d 70 2e 6e 44 61 74 61 2c 20 30 2c 20 30   tmp.nData, 0, 0
36878 2c 20 65 54 79 70 65 2c 20 70 52 65 73 75 6c 74  , eType, pResult
36879 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
3687a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
3687b 73 74 72 6f 79 28 26 72 65 73 29 3b 0a 20 20 20  stroy(&res);.   
3687c 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44       dataBufferD
3687d 65 73 74 72 6f 79 28 26 74 6d 70 29 3b 0a 20 20  estroy(&tmp);.  
3687e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
3687f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
36880 0a 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74 65 20  ./*.** Evaluate 
36881 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 65 78  the full-text ex
36882 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61  pression pExpr a
36883 67 61 69 6e 73 74 20 66 74 73 33 20 74 61 62 6c  gainst fts3 tabl
36884 65 20 70 54 61 62 2e 20 57 72 69 74 65 0a 2a 2a  e pTab. Write.**
36885 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 74   the results int
36886 6f 20 70 52 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  o pRes..*/.stati
36887 63 20 69 6e 74 20 65 76 61 6c 46 74 73 33 45 78  c int evalFts3Ex
36888 70 72 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76  pr(.  fulltext_v
36889 74 61 62 20 2a 70 54 61 62 2c 20 20 20 20 20 20  tab *pTab,      
3688a 20 20 20 20 20 2f 2a 20 46 74 73 33 20 56 69 72       /* Fts3 Vir
3688b 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63  tual table objec
3688c 74 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20  t */.  Fts3Expr 
3688d 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  *pExpr,         
3688e 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 64 20        /* Parsed 
3688f 66 74 73 33 20 65 78 70 72 65 73 73 69 6f 6e 20  fts3 expression 
36890 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  */.  DataBuffer 
36891 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20  *pRes           
36892 20 20 20 20 2f 2a 20 4f 55 54 3a 20 57 72 69 74      /* OUT: Writ
36893 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65  e results of the
36894 20 65 78 70 72 65 73 73 69 6f 6e 20 68 65 72 65   expression here
36895 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
36896 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
36897 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
36898 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e  e output buffer.
36899 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 65   If this is an e
3689a 6d 70 74 79 20 71 75 65 72 79 20 28 70 45 78 70  mpty query (pExp
3689b 72 3d 3d 30 29 2c 20 0a 20 20 2a 2a 20 74 68 69  r==0), .  ** thi
3689c 73 20 69 73 20 61 6c 6c 20 74 68 61 74 20 6e 65  s is all that ne
3689d 65 64 73 20 74 6f 20 62 65 20 64 6f 6e 65 2e 20  eds to be done. 
3689e 45 6d 70 74 79 20 71 75 65 72 69 65 73 20 70 72  Empty queries pr
3689f 6f 64 75 63 65 20 65 6d 70 74 79 20 0a 20 20 2a  oduce empty .  *
368a0 2a 20 72 65 73 75 6c 74 20 73 65 74 73 2e 0a 20  * result sets.. 
368a1 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66 65 72   */.  dataBuffer
368a2 49 6e 69 74 28 70 52 65 73 2c 20 30 29 3b 0a 0a  Init(pRes, 0);..
368a3 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20    if( pExpr ){. 
368a4 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 65 54     if( pExpr->eT
368a5 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
368a6 52 41 53 45 20 29 7b 0a 20 20 20 20 20 20 44 6f  RASE ){.      Do
368a7 63 4c 69 73 74 54 79 70 65 20 65 54 79 70 65 20  cListType eType 
368a8 3d 20 44 4c 5f 44 4f 43 49 44 53 3b 0a 20 20 20  = DL_DOCIDS;.   
368a9 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 50     if( pExpr->pP
368aa 61 72 65 6e 74 20 26 26 20 70 45 78 70 72 2d 3e  arent && pExpr->
368ab 70 50 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d 3d  pParent->eType==
368ac 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b  FTSQUERY_NEAR ){
368ad 0a 20 20 20 20 20 20 20 20 65 54 79 70 65 20 3d  .        eType =
368ae 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 3b 0a 20   DL_POSITIONS;. 
368af 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
368b0 3d 20 64 6f 63 4c 69 73 74 4f 66 50 68 72 61 73  = docListOfPhras
368b1 65 28 70 54 61 62 2c 20 70 45 78 70 72 2d 3e 70  e(pTab, pExpr->p
368b2 50 68 72 61 73 65 2c 20 65 54 79 70 65 2c 20 70  Phrase, eType, p
368b3 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Res);.    }else{
368b4 0a 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65  .      DataBuffe
368b5 72 20 6c 68 73 3b 0a 20 20 20 20 20 20 44 61 74  r lhs;.      Dat
368b6 61 42 75 66 66 65 72 20 72 68 73 3b 0a 0a 20 20  aBuffer rhs;..  
368b7 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e      dataBufferIn
368b8 69 74 28 26 72 68 73 2c 20 30 29 3b 0a 20 20 20  it(&rhs, 0);.   
368b9 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
368ba 3d 3d 28 72 63 20 3d 20 65 76 61 6c 46 74 73 33  ==(rc = evalFts3
368bb 45 78 70 72 28 70 54 61 62 2c 20 70 45 78 70 72  Expr(pTab, pExpr
368bc 2d 3e 70 4c 65 66 74 2c 20 26 6c 68 73 29 29 20  ->pLeft, &lhs)) 
368bd 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54  .       && SQLIT
368be 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 65 76 61 6c  E_OK==(rc = eval
368bf 46 74 73 33 45 78 70 72 28 70 54 61 62 2c 20 70  Fts3Expr(pTab, p
368c0 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72  Expr->pRight, &r
368c1 68 73 29 29 20 0a 20 20 20 20 20 20 29 7b 0a 20  hs)) .      ){. 
368c2 20 20 20 20 20 20 20 73 77 69 74 63 68 28 20 70         switch( p
368c3 45 78 70 72 2d 3e 65 54 79 70 65 20 29 7b 0a 20  Expr->eType ){. 
368c4 20 20 20 20 20 20 20 20 20 63 61 73 65 20 46 54           case FT
368c5 53 51 55 45 52 59 5f 4e 45 41 52 3a 20 7b 0a 20  SQUERY_NEAR: {. 
368c6 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e             int n
368c7 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 20  Token;.         
368c8 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c 65     Fts3Expr *pLe
368c9 66 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ft;.            
368ca 44 6f 63 4c 69 73 74 54 79 70 65 20 65 54 79 70  DocListType eTyp
368cb 65 20 3d 20 44 4c 5f 44 4f 43 49 44 53 3b 0a 20  e = DL_DOCIDS;. 
368cc 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
368cd 45 78 70 72 2d 3e 70 50 61 72 65 6e 74 20 26 26  Expr->pParent &&
368ce 20 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74 2d   pExpr->pParent-
368cf 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
368d0 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20 20 20  _NEAR ){.       
368d1 20 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 44         eType = D
368d2 4c 5f 50 4f 53 49 54 49 4f 4e 53 3b 0a 20 20 20  L_POSITIONS;.   
368d3 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
368d4 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70         pLeft = p
368d5 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20  Expr->pLeft;.   
368d6 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
368d7 70 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d 46 54  pLeft->eType==FT
368d8 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 20 0a  SQUERY_NEAR ){ .
368d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4c                pL
368da 65 66 74 3d 70 4c 65 66 74 2d 3e 70 52 69 67 68  eft=pLeft->pRigh
368db 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  t;.            }
368dc 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
368dd 65 72 74 28 20 70 45 78 70 72 2d 3e 70 52 69 67  ert( pExpr->pRig
368de 68 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  ht->eType==FTSQU
368df 45 52 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20  ERY_PHRASE );.  
368e0 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
368e1 28 20 70 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d  ( pLeft->eType==
368e2 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
368e3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e  );.            n
368e4 54 6f 6b 65 6e 20 3d 20 70 4c 65 66 74 2d 3e 70  Token = pLeft->p
368e5 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 20 2b  Phrase->nToken +
368e6 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e   pExpr->pRight->
368e7 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b  pPhrase->nToken;
368e8 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 6f 63  .            doc
368e9 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28  ListPhraseMerge(
368ea 6c 68 73 2e 70 44 61 74 61 2c 20 6c 68 73 2e 6e  lhs.pData, lhs.n
368eb 44 61 74 61 2c 20 72 68 73 2e 70 44 61 74 61 2c  Data, rhs.pData,
368ec 20 72 68 73 2e 6e 44 61 74 61 2c 20 0a 20 20 20   rhs.nData, .   
368ed 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
368ee 70 72 2d 3e 6e 4e 65 61 72 2b 31 2c 20 6e 54 6f  pr->nNear+1, nTo
368ef 6b 65 6e 2c 20 65 54 79 70 65 2c 20 70 52 65 73  ken, eType, pRes
368f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a  .            );.
368f1 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
368f2 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
368f3 20 20 20 20 20 20 20 20 20 63 61 73 65 20 46 54           case FT
368f4 53 51 55 45 52 59 5f 4e 4f 54 3a 20 7b 0a 20 20  SQUERY_NOT: {.  
368f5 20 20 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73            docLis
368f6 74 45 78 63 65 70 74 4d 65 72 67 65 28 6c 68 73  tExceptMerge(lhs
368f7 2e 70 44 61 74 61 2c 20 6c 68 73 2e 6e 44 61 74  .pData, lhs.nDat
368f8 61 2c 20 72 68 73 2e 70 44 61 74 61 2c 20 72 68  a, rhs.pData, rh
368f9 73 2e 6e 44 61 74 61 2c 70 52 65 73 29 3b 0a 20  s.nData,pRes);. 
368fa 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
368fb 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
368fc 20 20 20 20 20 20 20 20 63 61 73 65 20 46 54 53          case FTS
368fd 51 55 45 52 59 5f 41 4e 44 3a 20 7b 0a 20 20 20  QUERY_AND: {.   
368fe 20 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74           docList
368ff 41 6e 64 4d 65 72 67 65 28 6c 68 73 2e 70 44 61  AndMerge(lhs.pDa
36900 74 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c 20 72  ta, lhs.nData, r
36901 68 73 2e 70 44 61 74 61 2c 20 72 68 73 2e 6e 44  hs.pData, rhs.nD
36902 61 74 61 2c 20 70 52 65 73 29 3b 0a 20 20 20 20  ata, pRes);.    
36903 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
36904 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
36905 20 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45       case FTSQUE
36906 52 59 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20 20  RY_OR: {.       
36907 20 20 20 20 20 64 6f 63 4c 69 73 74 4f 72 4d 65       docListOrMe
36908 72 67 65 28 6c 68 73 2e 70 44 61 74 61 2c 20 6c  rge(lhs.pData, l
36909 68 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e 70 44  hs.nData, rhs.pD
3690a 61 74 61 2c 20 72 68 73 2e 6e 44 61 74 61 2c 20  ata, rhs.nData, 
3690b 70 52 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20  pRes);.         
3690c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3690d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
3690e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 61        }.      da
3690f 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
36910 26 6c 68 73 29 3b 0a 20 20 20 20 20 20 64 61 74  &lhs);.      dat
36911 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
36912 72 68 73 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rhs);.    }.  }.
36913 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
36914 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
36915 52 65 66 61 63 74 6f 72 20 74 68 65 20 63 6f 64  Refactor the cod
36916 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 69 73  e to remove this
36917 20 66 6f 72 77 61 72 64 20 64 65 63 6c 2e 20 2a   forward decl. *
36918 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 75  /.static int flu
36919 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66  shPendingTerms(f
3691a 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29  ulltext_vtab *v)
3691b 3b 0a 0a 2f 2a 20 50 65 72 66 6f 72 6d 20 61 20  ;../* Perform a 
3691c 66 75 6c 6c 2d 74 65 78 74 20 71 75 65 72 79 20  full-text query 
3691d 75 73 69 6e 67 20 74 68 65 20 73 65 61 72 63 68  using the search
3691e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 0a 2a   expression in.*
3691f 2a 20 7a 49 6e 70 75 74 5b 30 2e 2e 6e 49 6e 70  * zInput[0..nInp
36920 75 74 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20 61  ut-1].  Return a
36921 20 6c 69 73 74 20 6f 66 20 6d 61 74 63 68 69 6e   list of matchin
36922 67 20 64 6f 63 75 6d 65 6e 74 73 0a 2a 2a 20 69  g documents.** i
36923 6e 20 70 52 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a  n pResult..**.**
36924 20 51 75 65 72 69 65 73 20 6d 75 73 74 20 6d 61   Queries must ma
36925 74 63 68 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 75  tch column iColu
36926 6d 6e 2e 20 20 4f 72 20 69 66 20 69 43 6f 6c 75  mn.  Or if iColu
36927 6d 6e 3e 3d 6e 43 6f 6c 75 6d 6e 0a 2a 2a 20 74  mn>=nColumn.** t
36928 68 65 79 20 61 72 65 20 61 6c 6c 6f 77 65 64 20  hey are allowed 
36929 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74  to match against
3692a 20 61 6e 79 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a   any column..*/.
3692b 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
3692c 65 78 74 51 75 65 72 79 28 0a 20 20 66 75 6c 6c  extQuery(.  full
3692d 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 20 20  text_vtab *v,   
3692e 20 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c 20 74     /* The full t
3692f 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  ext index */.  i
36930 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20  nt iColumn,     
36931 20 20 20 20 20 20 2f 2a 20 4d 61 74 63 68 20 61        /* Match a
36932 67 61 69 6e 73 74 20 74 68 69 73 20 63 6f 6c 75  gainst this colu
36933 6d 6e 20 62 79 20 64 65 66 61 75 6c 74 20 2a 2f  mn by default */
36934 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
36935 49 6e 70 75 74 2c 20 20 20 20 2f 2a 20 54 68 65  Input,    /* The
36936 20 71 75 65 72 79 20 73 74 72 69 6e 67 20 2a 2f   query string */
36937 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20  .  int nInput,  
36938 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
36939 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
3693a 7a 49 6e 70 75 74 5b 5d 20 2a 2f 0a 20 20 44 61  zInput[] */.  Da
3693b 74 61 42 75 66 66 65 72 20 2a 70 52 65 73 75 6c  taBuffer *pResul
3693c 74 2c 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68  t,   /* Write th
3693d 65 20 72 65 73 75 6c 74 20 64 6f 63 6c 69 73 74  e result doclist
3693e 20 68 65 72 65 20 2a 2f 0a 20 20 46 74 73 33 45   here */.  Fts3E
3693f 78 70 72 20 2a 2a 70 70 45 78 70 72 20 20 20 20  xpr **ppExpr    
36940 20 20 20 20 2f 2a 20 50 75 74 20 70 61 72 73 65      /* Put parse
36941 64 20 71 75 65 72 79 20 73 74 72 69 6e 67 20 68  d query string h
36942 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
36943 72 63 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73  rc;..  /* TODO(s
36944 68 65 73 73 29 20 49 6e 73 74 65 61 64 20 6f 66  hess) Instead of
36945 20 66 6c 75 73 68 69 6e 67 20 70 65 6e 64 69 6e   flushing pendin
36946 67 54 65 72 6d 73 2c 20 77 65 20 63 6f 75 6c 64  gTerms, we could
36947 20 71 75 65 72 79 20 66 6f 72 0a 20 20 2a 2a 20   query for.  ** 
36948 74 68 65 20 72 65 6c 65 76 61 6e 74 20 74 65 72  the relevant ter
36949 6d 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65 20  m and merge the 
3694a 64 6f 63 6c 69 73 74 20 69 6e 74 6f 20 77 68 61  doclist into wha
3694b 74 20 77 65 20 72 65 63 65 69 76 65 20 66 72 6f  t we receive fro
3694c 6d 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62  m.  ** the datab
3694d 61 73 65 2e 20 20 57 61 69 74 20 61 6e 64 20 73  ase.  Wait and s
3694e 65 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ee if this is a 
3694f 63 6f 6d 6d 6f 6e 20 69 73 73 75 65 2c 20 66 69  common issue, fi
36950 72 73 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  rst..  **.  ** A
36951 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20 6e 6f 74   good reason not
36952 20 74 6f 20 66 6c 75 73 68 20 69 73 20 74 6f 20   to flush is to 
36953 6e 6f 74 20 67 65 6e 65 72 61 74 65 20 75 70 64  not generate upd
36954 61 74 65 2d 72 65 6c 61 74 65 64 0a 20 20 2a 2a  ate-related.  **
36955 20 65 72 72 6f 72 20 63 6f 64 65 73 20 66 72 6f   error codes fro
36956 6d 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 0a 20 20  m here..  */..  
36957 2f 2a 20 46 6c 75 73 68 20 61 6e 79 20 62 75 66  /* Flush any buf
36958 66 65 72 65 64 20 75 70 64 61 74 65 73 20 62 65  fered updates be
36959 66 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20 74  fore executing t
3695a 68 65 20 71 75 65 72 79 2e 20 2a 2f 0a 20 20 72  he query. */.  r
3695b 63 20 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67  c = flushPending
3695c 54 65 72 6d 73 28 76 29 3b 0a 20 20 69 66 28 20  Terms(v);.  if( 
3695d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
3695e 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
3695f 20 20 7d 0a 0a 20 20 2f 2a 20 50 61 72 73 65 20    }..  /* Parse 
36960 74 68 65 20 71 75 65 72 79 20 70 61 73 73 65 64  the query passed
36961 20 74 6f 20 74 68 65 20 4d 41 54 43 48 20 6f 70   to the MATCH op
36962 65 72 61 74 6f 72 2e 20 2a 2f 0a 20 20 72 63 20  erator. */.  rc 
36963 3d 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70  = sqlite3Fts3Exp
36964 72 50 61 72 73 65 28 76 2d 3e 70 54 6f 6b 65 6e  rParse(v->pToken
36965 69 7a 65 72 2c 20 0a 20 20 20 20 20 20 76 2d 3e  izer, .      v->
36966 61 7a 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 6e 43 6f  azColumn, v->nCo
36967 6c 75 6d 6e 2c 20 69 43 6f 6c 75 6d 6e 2c 20 7a  lumn, iColumn, z
36968 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 70  Input, nInput, p
36969 70 45 78 70 72 0a 20 20 29 3b 0a 20 20 69 66 28  pExpr.  );.  if(
3696a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3696b 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d  {.    assert( 0=
3696c 3d 28 2a 70 70 45 78 70 72 29 20 29 3b 0a 20 20  =(*ppExpr) );.  
3696d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
3696e 0a 0a 20 20 72 65 74 75 72 6e 20 65 76 61 6c 46  ..  return evalF
3696f 74 73 33 45 78 70 72 28 76 2c 20 2a 70 70 45 78  ts3Expr(v, *ppEx
36970 70 72 2c 20 70 52 65 73 75 6c 74 29 3b 0a 7d 0a  pr, pResult);.}.
36971 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ./*.** This is t
36972 68 65 20 78 46 69 6c 74 65 72 20 69 6e 74 65 72  he xFilter inter
36973 66 61 63 65 20 66 6f 72 20 74 68 65 20 76 69 72  face for the vir
36974 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 53 65 65  tual table.  See
36975 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20  .** the virtual 
36976 74 61 62 6c 65 20 78 46 69 6c 74 65 72 20 6d 65  table xFilter me
36977 74 68 6f 64 20 64 6f 63 75 6d 65 6e 74 61 74 69  thod documentati
36978 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  on for additiona
36979 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  l.** information
3697a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e 75  ..**.** If idxNu
3697b 6d 3d 3d 51 55 45 52 59 5f 47 45 4e 45 52 49 43  m==QUERY_GENERIC
3697c 20 74 68 65 6e 20 64 6f 20 61 20 66 75 6c 6c 20   then do a full 
3697d 74 61 62 6c 65 20 73 63 61 6e 20 61 67 61 69 6e  table scan again
3697e 73 74 0a 2a 2a 20 74 68 65 20 25 5f 63 6f 6e 74  st.** the %_cont
3697f 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ent table..**.**
36980 20 49 66 20 69 64 78 4e 75 6d 3d 3d 51 55 45 52   If idxNum==QUER
36981 59 5f 44 4f 43 49 44 20 74 68 65 6e 20 64 6f 20  Y_DOCID then do 
36982 61 20 64 6f 63 69 64 20 6c 6f 6f 6b 75 70 20 66  a docid lookup f
36983 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72  or a single entr
36984 79 0a 2a 2a 20 69 6e 20 74 68 65 20 25 5f 63 6f  y.** in the %_co
36985 6e 74 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a  ntent table..**.
36986 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3e 3d 51 55  ** If idxNum>=QU
36987 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 74 68 65  ERY_FULLTEXT the
36988 6e 20 75 73 65 20 74 68 65 20 66 75 6c 6c 20 74  n use the full t
36989 65 78 74 20 69 6e 64 65 78 2e 20 20 54 68 65 0a  ext index.  The.
3698a 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65  ** column on the
3698b 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20   left-hand side 
3698c 6f 66 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65  of the MATCH ope
3698d 72 61 74 6f 72 20 69 73 20 63 6f 6c 75 6d 6e 0a  rator is column.
3698e 2a 2a 20 6e 75 6d 62 65 72 20 69 64 78 4e 75 6d  ** number idxNum
3698f 2d 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 2c  -QUERY_FULLTEXT,
36990 20 30 20 69 6e 64 65 78 65 64 2e 20 20 61 72 67   0 indexed.  arg
36991 76 5b 30 5d 20 69 73 20 74 68 65 20 72 69 67 68  v[0] is the righ
36992 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 64 65 20 6f  t-hand.** side o
36993 66 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72  f the MATCH oper
36994 61 74 6f 72 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f  ator..*/./* TODO
36995 28 73 68 65 73 73 29 20 55 70 67 72 61 64 65 20  (shess) Upgrade 
36996 74 68 65 20 63 75 72 73 6f 72 20 69 6e 69 74 69  the cursor initi
36997 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 20 64 65  alization and de
36998 73 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20  struction to.** 
36999 61 63 63 6f 75 6e 74 20 66 6f 72 20 66 75 6c 6c  account for full
3699a 74 65 78 74 46 69 6c 74 65 72 28 29 20 62 65 69  textFilter() bei
3699b 6e 67 20 63 61 6c 6c 65 64 20 6d 75 6c 74 69 70  ng called multip
3699c 6c 65 20 74 69 6d 65 73 20 6f 6e 20 74 68 65 0a  le times on the.
3699d 2a 2a 20 73 61 6d 65 20 63 75 72 73 6f 72 2e 20  ** same cursor. 
3699e 20 54 68 65 20 63 75 72 72 65 6e 74 20 73 6f 6c   The current sol
3699f 75 74 69 6f 6e 20 69 73 20 76 65 72 79 20 66 72  ution is very fr
369a0 61 67 69 6c 65 2e 20 20 41 70 70 6c 79 20 66 69  agile.  Apply fi
369a1 78 20 74 6f 0a 2a 2a 20 66 74 73 33 20 61 73 20  x to.** fts3 as 
369a2 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a 2f 0a  appropriate..*/.
369a3 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
369a4 65 78 74 46 69 6c 74 65 72 28 0a 20 20 73 71 6c  extFilter(.  sql
369a5 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
369a6 20 2a 70 43 75 72 73 6f 72 2c 20 20 20 20 20 2f   *pCursor,     /
369a7 2a 20 54 68 65 20 63 75 72 73 6f 72 20 75 73 65  * The cursor use
369a8 64 20 66 6f 72 20 74 68 69 73 20 71 75 65 72 79  d for this query
369a9 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d   */.  int idxNum
369aa 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64  , const char *id
369ab 78 53 74 72 2c 20 20 20 2f 2a 20 57 68 69 63 68  xStr,   /* Which
369ac 20 69 6e 64 65 78 69 6e 67 20 73 63 68 65 6d 65   indexing scheme
369ad 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74   to use */.  int
369ae 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
369af 61 6c 75 65 20 2a 2a 61 72 67 76 20 20 20 20 2f  alue **argv    /
369b0 2a 20 41 72 67 75 6d 65 6e 74 73 20 66 6f 72 20  * Arguments for 
369b1 74 68 65 20 69 6e 64 65 78 69 6e 67 20 73 63 68  the indexing sch
369b2 65 6d 65 20 2a 2f 0a 29 7b 0a 20 20 66 75 6c 6c  eme */.){.  full
369b3 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d  text_cursor *c =
369b4 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f   (fulltext_curso
369b5 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
369b6 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
369b7 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28 63   = cursor_vtab(c
369b8 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  );.  int rc;..  
369b9 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
369ba 46 69 6c 74 65 72 20 25 70 5c 6e 22 2c 70 43 75  Filter %p\n",pCu
369bb 72 73 6f 72 29 29 3b 0a 0a 20 20 2f 2a 20 49 66  rsor));..  /* If
369bc 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20   the cursor has 
369bd 61 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  a statement that
369be 20 77 61 73 20 6e 6f 74 20 70 72 65 70 61 72 65   was not prepare
369bf 64 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 0a 20  d according to. 
369c0 20 2a 2a 20 69 64 78 4e 75 6d 2c 20 63 6c 65 61   ** idxNum, clea
369c1 72 20 69 74 2e 20 20 49 20 62 65 6c 69 65 76 65  r it.  I believe
369c2 20 61 6c 6c 20 63 61 6c 6c 73 20 74 6f 20 66 75   all calls to fu
369c3 6c 6c 74 65 78 74 46 69 6c 74 65 72 20 77 69 74  lltextFilter wit
369c4 68 20 61 0a 20 20 2a 2a 20 67 69 76 65 6e 20 63  h a.  ** given c
369c5 75 72 73 6f 72 20 77 69 6c 6c 20 68 61 76 65 20  ursor will have 
369c6 74 68 65 20 73 61 6d 65 20 69 64 78 4e 75 6d 20  the same idxNum 
369c7 2c 20 62 75 74 20 69 6e 20 74 68 69 73 20 63 61  , but in this ca
369c8 73 65 20 69 74 27 73 0a 20 20 2a 2a 20 65 61 73  se it's.  ** eas
369c9 79 20 74 6f 20 62 65 20 73 61 66 65 2e 0a 20 20  y to be safe..  
369ca 2a 2f 0a 20 20 69 66 28 20 63 2d 3e 70 53 74 6d  */.  if( c->pStm
369cb 74 20 26 26 20 63 2d 3e 69 43 75 72 73 6f 72 54  t && c->iCursorT
369cc 79 70 65 21 3d 69 64 78 4e 75 6d 20 29 7b 0a 20  ype!=idxNum ){. 
369cd 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c     sqlite3_final
369ce 69 7a 65 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20  ize(c->pStmt);. 
369cf 20 20 20 63 2d 3e 70 53 74 6d 74 20 3d 20 4e 55     c->pStmt = NU
369d0 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65  LL;.  }..  /* Ge
369d1 74 20 61 20 66 72 65 73 68 20 73 74 61 74 65 6d  t a fresh statem
369d2 65 6e 74 20 61 70 70 72 6f 70 72 69 61 74 65 20  ent appropriate 
369d3 74 6f 20 69 64 78 4e 75 6d 2e 20 2a 2f 0a 20 20  to idxNum. */.  
369d4 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20  /* TODO(shess): 
369d5 41 64 64 20 61 20 70 72 65 70 61 72 65 64 2d 73  Add a prepared-s
369d6 74 61 74 65 6d 65 6e 74 20 63 61 63 68 65 20 69  tatement cache i
369d7 6e 20 74 68 65 20 76 74 20 73 74 72 75 63 74 75  n the vt structu
369d8 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 63  re..  ** The cac
369d9 68 65 20 6d 75 73 74 20 68 61 6e 64 6c 65 20 6d  he must handle m
369da 75 6c 74 69 70 6c 65 20 6f 70 65 6e 20 63 75 72  ultiple open cur
369db 73 6f 72 73 2e 20 20 45 61 73 69 65 72 20 74 6f  sors.  Easier to
369dc 20 63 61 63 68 65 20 74 68 65 0a 20 20 2a 2a 20   cache the.  ** 
369dd 73 74 61 74 65 6d 65 6e 74 20 76 61 72 69 61 6e  statement varian
369de 74 73 20 61 74 20 74 68 65 20 76 74 20 74 6f 20  ts at the vt to 
369df 72 65 64 75 63 65 20 6d 61 6c 6c 6f 63 2f 72 65  reduce malloc/re
369e0 61 6c 6c 6f 63 2f 66 72 65 65 20 68 65 72 65 2e  alloc/free here.
369e1 0a 20 20 2a 2a 20 4f 72 20 77 65 20 63 6f 75 6c  .  ** Or we coul
369e2 64 20 68 61 76 65 20 61 20 53 74 72 69 6e 67 42  d have a StringB
369e3 75 66 66 65 72 20 76 61 72 69 61 6e 74 20 77 68  uffer variant wh
369e4 69 63 68 20 61 6c 6c 6f 77 65 64 20 73 74 61 63  ich allowed stac
369e5 6b 0a 20 20 2a 2a 20 63 6f 6e 73 74 72 75 63 74  k.  ** construct
369e6 69 6f 6e 20 66 6f 72 20 73 6d 61 6c 6c 20 76 61  ion for small va
369e7 6c 75 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  lues..  */.  if(
369e8 20 21 63 2d 3e 70 53 74 6d 74 20 29 7b 0a 20 20   !c->pStmt ){.  
369e9 20 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73    StringBuffer s
369ea 62 3b 0a 20 20 20 20 69 6e 69 74 53 74 72 69 6e  b;.    initStrin
369eb 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20  gBuffer(&sb);.  
369ec 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 53    append(&sb, "S
369ed 45 4c 45 43 54 20 64 6f 63 69 64 2c 20 22 29 3b  ELECT docid, ");
369ee 0a 20 20 20 20 61 70 70 65 6e 64 4c 69 73 74 28  .    appendList(
369ef 26 73 62 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c  &sb, v->nColumn,
369f0 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c   v->azContentCol
369f1 75 6d 6e 29 3b 0a 20 20 20 20 61 70 70 65 6e 64  umn);.    append
369f2 28 26 73 62 2c 20 22 20 46 52 4f 4d 20 25 5f 63  (&sb, " FROM %_c
369f3 6f 6e 74 65 6e 74 22 29 3b 0a 20 20 20 20 69 66  ontent");.    if
369f4 28 20 69 64 78 4e 75 6d 21 3d 51 55 45 52 59 5f  ( idxNum!=QUERY_
369f5 47 45 4e 45 52 49 43 20 29 20 61 70 70 65 6e 64  GENERIC ) append
369f6 28 26 73 62 2c 20 22 20 57 48 45 52 45 20 64 6f  (&sb, " WHERE do
369f7 63 69 64 20 3d 20 3f 22 29 3b 0a 20 20 20 20 72  cid = ?");.    r
369f8 63 20 3d 20 73 71 6c 5f 70 72 65 70 61 72 65 28  c = sql_prepare(
369f9 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76  v->db, v->zDb, v
369fa 2d 3e 7a 4e 61 6d 65 2c 20 26 63 2d 3e 70 53 74  ->zName, &c->pSt
369fb 6d 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  mt,.            
369fc 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 42           stringB
369fd 75 66 66 65 72 44 61 74 61 28 26 73 62 29 29 3b  ufferData(&sb));
369fe 0a 20 20 20 20 73 74 72 69 6e 67 42 75 66 66 65  .    stringBuffe
369ff 72 44 65 73 74 72 6f 79 28 26 73 62 29 3b 0a 20  rDestroy(&sb);. 
36a00 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
36a01 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
36a02 3b 0a 20 20 20 20 63 2d 3e 69 43 75 72 73 6f 72  ;.    c->iCursor
36a03 54 79 70 65 20 3d 20 69 64 78 4e 75 6d 3b 0a 20  Type = idxNum;. 
36a04 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
36a05 74 65 33 5f 72 65 73 65 74 28 63 2d 3e 70 53 74  te3_reset(c->pSt
36a06 6d 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  mt);.    assert(
36a07 20 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 3d   c->iCursorType=
36a08 3d 69 64 78 4e 75 6d 20 29 3b 0a 20 20 7d 0a 0a  =idxNum );.  }..
36a09 20 20 73 77 69 74 63 68 28 20 69 64 78 4e 75 6d    switch( idxNum
36a0a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 51 55 45   ){.    case QUE
36a0b 52 59 5f 47 45 4e 45 52 49 43 3a 0a 20 20 20 20  RY_GENERIC:.    
36a0c 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 63 61    break;..    ca
36a0d 73 65 20 51 55 45 52 59 5f 44 4f 43 49 44 3a 0a  se QUERY_DOCID:.
36a0e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
36a0f 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 63 2d  e3_bind_int64(c-
36a10 3e 70 53 74 6d 74 2c 20 31 2c 20 73 71 6c 69 74  >pStmt, 1, sqlit
36a11 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61  e3_value_int64(a
36a12 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20  rgv[0]));.      
36a13 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
36a14 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
36a15 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20       break;..   
36a16 20 64 65 66 61 75 6c 74 3a 20 20 20 2f 2a 20 66   default:   /* f
36a17 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 20  ull-text search 
36a18 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69  */.    {.      i
36a19 6e 74 20 69 43 6f 6c 20 3d 20 69 64 78 4e 75 6d  nt iCol = idxNum
36a1a 2d 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 3b  -QUERY_FULLTEXT;
36a1b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
36a1c 72 20 2a 7a 51 75 65 72 79 20 3d 20 28 63 6f 6e  r *zQuery = (con
36a1d 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
36a1e 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
36a1f 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73  v[0]);.      ass
36a20 65 72 74 28 20 69 64 78 4e 75 6d 3c 3d 51 55 45  ert( idxNum<=QUE
36a21 52 59 5f 46 55 4c 4c 54 45 58 54 2b 76 2d 3e 6e  RY_FULLTEXT+v->n
36a22 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 61  Column);.      a
36a23 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29  ssert( argc==1 )
36a24 3b 0a 20 20 20 20 20 20 69 66 28 20 63 2d 3e 72  ;.      if( c->r
36a25 65 73 75 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29  esult.nData!=0 )
36a26 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
36a27 73 20 63 61 73 65 20 68 61 70 70 65 6e 73 20 69  s case happens i
36a28 66 20 74 68 65 20 73 61 6d 65 20 63 75 72 73 6f  f the same curso
36a29 72 20 69 73 20 75 73 65 64 20 72 65 70 65 61 74  r is used repeat
36a2a 65 64 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20  edly. */.       
36a2b 20 64 6c 72 44 65 73 74 72 6f 79 28 26 63 2d 3e   dlrDestroy(&c->
36a2c 72 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20 20  reader);.       
36a2d 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
36a2e 28 26 63 2d 3e 72 65 73 75 6c 74 29 3b 0a 20 20  (&c->result);.  
36a2f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
36a30 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69     dataBufferIni
36a31 74 28 26 63 2d 3e 72 65 73 75 6c 74 2c 20 30 29  t(&c->result, 0)
36a32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
36a33 72 63 20 3d 20 66 75 6c 6c 74 65 78 74 51 75 65  rc = fulltextQue
36a34 72 79 28 76 2c 20 69 43 6f 6c 2c 20 7a 51 75 65  ry(v, iCol, zQue
36a35 72 79 2c 20 2d 31 2c 20 26 63 2d 3e 72 65 73 75  ry, -1, &c->resu
36a36 6c 74 2c 20 26 63 2d 3e 70 45 78 70 72 29 3b 0a  lt, &c->pExpr);.
36a37 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
36a38 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
36a39 20 72 63 3b 0a 20 20 20 20 20 20 69 66 28 20 63   rc;.      if( c
36a3a 2d 3e 72 65 73 75 6c 74 2e 6e 44 61 74 61 21 3d  ->result.nData!=
36a3b 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6c 72  0 ){.        dlr
36a3c 49 6e 69 74 28 26 63 2d 3e 72 65 61 64 65 72 2c  Init(&c->reader,
36a3d 20 44 4c 5f 44 4f 43 49 44 53 2c 20 63 2d 3e 72   DL_DOCIDS, c->r
36a3e 65 73 75 6c 74 2e 70 44 61 74 61 2c 20 63 2d 3e  esult.pData, c->
36a3f 72 65 73 75 6c 74 2e 6e 44 61 74 61 29 3b 0a 20  result.nData);. 
36a40 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
36a41 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ak;.    }.  }.. 
36a42 20 72 65 74 75 72 6e 20 66 75 6c 6c 74 65 78 74   return fulltext
36a43 4e 65 78 74 28 70 43 75 72 73 6f 72 29 3b 0a 7d  Next(pCursor);.}
36a44 0a 0a 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65  ../* This is the
36a45 20 78 45 6f 66 20 6d 65 74 68 6f 64 20 6f 66 20   xEof method of 
36a46 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
36a47 65 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63  e.  The SQLite c
36a48 6f 72 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 68 69  ore.** calls thi
36a49 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 69 6e  s routine to fin
36a4a 64 20 6f 75 74 20 69 66 20 69 74 20 68 61 73 20  d out if it has 
36a4b 72 65 61 63 68 65 64 20 74 68 65 20 65 6e 64 20  reached the end 
36a4c 6f 66 0a 2a 2a 20 61 20 71 75 65 72 79 27 73 20  of.** a query's 
36a4d 72 65 73 75 6c 74 73 20 73 65 74 2e 0a 2a 2f 0a  results set..*/.
36a4e 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
36a4f 65 78 74 45 6f 66 28 73 71 6c 69 74 65 33 5f 76  extEof(sqlite3_v
36a50 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  tab_cursor *pCur
36a51 73 6f 72 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  sor){.  fulltext
36a52 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75  _cursor *c = (fu
36a53 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29  lltext_cursor *)
36a54 20 70 43 75 72 73 6f 72 3b 0a 20 20 72 65 74 75   pCursor;.  retu
36a55 72 6e 20 63 2d 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a  rn c->eof;.}../*
36a56 20 54 68 69 73 20 69 73 20 74 68 65 20 78 43 6f   This is the xCo
36a57 6c 75 6d 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74  lumn method of t
36a58 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
36a59 2e 20 20 54 68 65 20 53 51 4c 69 74 65 0a 2a 2a  .  The SQLite.**
36a5a 20 63 6f 72 65 20 63 61 6c 6c 73 20 74 68 69 73   core calls this
36a5b 20 6d 65 74 68 6f 64 20 64 75 72 69 6e 67 20 61   method during a
36a5c 20 71 75 65 72 79 20 77 68 65 6e 20 69 74 20 6e   query when it n
36a5d 65 65 64 73 20 74 68 65 20 76 61 6c 75 65 0a 2a  eeds the value.*
36a5e 2a 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72  * of a column fr
36a5f 6f 6d 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  om the virtual t
36a60 61 62 6c 65 2e 20 20 54 68 69 73 20 6d 65 74 68  able.  This meth
36a61 6f 64 20 6e 65 65 64 73 20 74 6f 20 75 73 65 0a  od needs to use.
36a62 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 71  ** one of the sq
36a63 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 2a 28 29  lite3_result_*()
36a64 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 73 74 6f   routines to sto
36a65 72 65 20 74 68 65 20 72 65 71 75 65 73 74 65 64  re the requested
36a66 0a 2a 2a 20 76 61 6c 75 65 20 62 61 63 6b 20 69  .** value back i
36a67 6e 20 74 68 65 20 70 43 6f 6e 74 65 78 74 2e 0a  n the pContext..
36a68 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  */.static int fu
36a69 6c 6c 74 65 78 74 43 6f 6c 75 6d 6e 28 73 71 6c  lltextColumn(sql
36a6a 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
36a6b 20 2a 70 43 75 72 73 6f 72 2c 0a 20 20 20 20 20   *pCursor,.     
36a6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36a6d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e       sqlite3_con
36a6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20  text *pContext, 
36a6f 69 6e 74 20 69 64 78 43 6f 6c 29 7b 0a 20 20 66  int idxCol){.  f
36a70 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
36a71 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75  c = (fulltext_cu
36a72 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b  rsor *) pCursor;
36a73 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
36a74 20 2a 76 20 3d 20 63 75 72 73 6f 72 5f 76 74 61   *v = cursor_vta
36a75 62 28 63 29 3b 0a 0a 20 20 69 66 28 20 69 64 78  b(c);..  if( idx
36a76 43 6f 6c 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  Col<v->nColumn )
36a77 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  {.    sqlite3_va
36a78 6c 75 65 20 2a 70 56 61 6c 20 3d 20 73 71 6c 69  lue *pVal = sqli
36a79 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65  te3_column_value
36a7a 28 63 2d 3e 70 53 74 6d 74 2c 20 69 64 78 43 6f  (c->pStmt, idxCo
36a7b 6c 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  l+1);.    sqlite
36a7c 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 70  3_result_value(p
36a7d 43 6f 6e 74 65 78 74 2c 20 70 56 61 6c 29 3b 0a  Context, pVal);.
36a7e 20 20 7d 65 6c 73 65 20 69 66 28 20 69 64 78 43    }else if( idxC
36a7f 6f 6c 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  ol==v->nColumn )
36a80 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 74  {.    /* The ext
36a81 72 61 20 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20  ra column whose 
36a82 6e 61 6d 65 20 69 73 20 74 68 65 20 73 61 6d 65  name is the same
36a83 20 61 73 20 74 68 65 20 74 61 62 6c 65 2e 0a 20   as the table.. 
36a84 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 61 20 62     ** Return a b
36a85 6c 6f 62 20 77 68 69 63 68 20 69 73 20 61 20 70  lob which is a p
36a86 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 75  ointer to the cu
36a87 72 73 6f 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20  rsor.    */.    
36a88 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
36a89 6c 6f 62 28 70 43 6f 6e 74 65 78 74 2c 20 26 63  lob(pContext, &c
36a8a 2c 20 73 69 7a 65 6f 66 28 63 29 2c 20 53 51 4c  , sizeof(c), SQL
36a8b 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
36a8c 20 20 7d 65 6c 73 65 20 69 66 28 20 69 64 78 43    }else if( idxC
36a8d 6f 6c 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31  ol==v->nColumn+1
36a8e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 64   ){.    /* The d
36a8f 6f 63 69 64 20 63 6f 6c 75 6d 6e 2c 20 77 68 69  ocid column, whi
36a90 63 68 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66  ch is an alias f
36a91 6f 72 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 20  or rowid. */.   
36a92 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
36a93 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63  pVal = sqlite3_c
36a94 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 63 2d 3e 70  olumn_value(c->p
36a95 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20 73 71  Stmt, 0);.    sq
36a96 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
36a97 75 65 28 70 43 6f 6e 74 65 78 74 2c 20 70 56 61  ue(pContext, pVa
36a98 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  l);.  }.  return
36a99 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
36a9a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 78 52  * This is the xR
36a9b 6f 77 69 64 20 6d 65 74 68 6f 64 2e 20 20 54 68  owid method.  Th
36a9c 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 63 61  e SQLite core ca
36a9d 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
36a9e 20 74 6f 0a 2a 2a 20 72 65 74 72 69 65 76 65 20   to.** retrieve 
36a9f 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68  the rowid for th
36aa0 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
36aa1 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
36aa2 20 20 66 74 73 33 0a 2a 2a 20 65 78 70 6f 73 65    fts3.** expose
36aa3 73 20 25 5f 63 6f 6e 74 65 6e 74 2e 64 6f 63 69  s %_content.doci
36aa4 64 20 61 73 20 74 68 65 20 72 6f 77 69 64 20 66  d as the rowid f
36aa5 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  or the virtual t
36aa6 61 62 6c 65 2e 20 20 54 68 65 0a 2a 2a 20 72 6f  able.  The.** ro
36aa7 77 69 64 20 73 68 6f 75 6c 64 20 62 65 20 77 72  wid should be wr
36aa8 69 74 74 65 6e 20 74 6f 20 2a 70 52 6f 77 69 64  itten to *pRowid
36aa9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36aaa 66 75 6c 6c 74 65 78 74 52 6f 77 69 64 28 73 71  fulltextRowid(sq
36aab 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
36aac 72 20 2a 70 43 75 72 73 6f 72 2c 20 73 71 6c 69  r *pCursor, sqli
36aad 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77 69 64  te_int64 *pRowid
36aae 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  ){.  fulltext_cu
36aaf 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74  rsor *c = (fullt
36ab0 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43  ext_cursor *) pC
36ab1 75 72 73 6f 72 3b 0a 0a 20 20 2a 70 52 6f 77 69  ursor;..  *pRowi
36ab2 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
36ab3 6d 6e 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74 6d  mn_int64(c->pStm
36ab4 74 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  t, 0);.  return 
36ab5 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
36ab6 20 41 64 64 20 61 6c 6c 20 74 65 72 6d 73 20 69   Add all terms i
36ab7 6e 20 5b 7a 54 65 78 74 5d 20 74 6f 20 70 65 6e  n [zText] to pen
36ab8 64 69 6e 67 54 65 72 6d 73 20 74 61 62 6c 65 2e  dingTerms table.
36ab9 20 20 49 66 20 5b 69 43 6f 6c 75 6d 6e 5d 20 3e    If [iColumn] >
36aba 20 30 2c 0a 2a 2a 20 77 65 20 61 6c 73 6f 20 73   0,.** we also s
36abb 74 6f 72 65 20 70 6f 73 69 74 69 6f 6e 73 20 61  tore positions a
36abc 6e 64 20 6f 66 66 73 65 74 73 20 69 6e 20 74 68  nd offsets in th
36abd 65 20 68 61 73 68 20 74 61 62 6c 65 20 75 73 69  e hash table usi
36abe 6e 67 20 74 68 61 74 0a 2a 2a 20 63 6f 6c 75 6d  ng that.** colum
36abf 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  n number..*/.sta
36ac0 74 69 63 20 69 6e 74 20 62 75 69 6c 64 54 65 72  tic int buildTer
36ac1 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ms(fulltext_vtab
36ac2 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36   *v, sqlite_int6
36ac3 34 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20 20  4 iDocid,.      
36ac4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ac5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 78  const char *zTex
36ac6 74 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 29 7b  t, int iColumn){
36ac7 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
36ac8 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
36ac9 20 3d 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72   = v->pTokenizer
36aca 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ;.  sqlite3_toke
36acb 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
36acc 75 72 73 6f 72 3b 0a 20 20 63 6f 6e 73 74 20 63  ursor;.  const c
36acd 68 61 72 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 69  har *pToken;.  i
36ace 6e 74 20 6e 54 6f 6b 65 6e 42 79 74 65 73 3b 0a  nt nTokenBytes;.
36acf 20 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73    int iStartOffs
36ad0 65 74 2c 20 69 45 6e 64 4f 66 66 73 65 74 2c 20  et, iEndOffset, 
36ad1 69 50 6f 73 69 74 69 6f 6e 3b 0a 20 20 69 6e 74  iPosition;.  int
36ad2 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 70 54 6f   rc;..  rc = pTo
36ad3 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65  kenizer->pModule
36ad4 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a  ->xOpen(pTokeniz
36ad5 65 72 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 26  er, zText, -1, &
36ad6 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20  pCursor);.  if( 
36ad7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
36ad8 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 70 43  return rc;..  pC
36ad9 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65  ursor->pTokenize
36ada 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a  r = pTokenizer;.
36adb 20 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f    while( SQLITE_
36adc 4f 4b 3d 3d 28 72 63 3d 70 54 6f 6b 65 6e 69 7a  OK==(rc=pTokeniz
36add 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65  er->pModule->xNe
36ade 78 74 28 70 43 75 72 73 6f 72 2c 0a 20 20 20 20  xt(pCursor,.    
36adf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ae1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
36ae2 70 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 42  pToken, &nTokenB
36ae3 79 74 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20  ytes,.          
36ae4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ae5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ae6 20 20 20 20 20 20 20 20 20 26 69 53 74 61 72 74           &iStart
36ae7 4f 66 66 73 65 74 2c 20 26 69 45 6e 64 4f 66 66  Offset, &iEndOff
36ae8 73 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  set,.           
36ae9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36aea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36aeb 20 20 20 20 20 20 20 20 26 69 50 6f 73 69 74 69          &iPositi
36aec 6f 6e 29 29 20 29 7b 0a 20 20 20 20 44 4c 43 6f  on)) ){.    DLCo
36aed 6c 6c 65 63 74 6f 72 20 2a 70 3b 0a 20 20 20 20  llector *p;.    
36aee 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20  int nData;      
36aef 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36af0 53 69 7a 65 20 6f 66 20 64 6f 63 6c 69 73 74 20  Size of doclist 
36af1 62 65 66 6f 72 65 20 6f 75 72 20 75 70 64 61 74  before our updat
36af2 65 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 50 6f  e. */..    /* Po
36af3 73 69 74 69 6f 6e 73 20 63 61 6e 27 74 20 62 65  sitions can't be
36af4 20 6e 65 67 61 74 69 76 65 3b 20 77 65 20 75 73   negative; we us
36af5 65 20 2d 31 20 61 73 20 61 20 74 65 72 6d 69 6e  e -1 as a termin
36af6 61 74 6f 72 0a 20 20 20 20 20 2a 20 69 6e 74 65  ator.     * inte
36af7 72 6e 61 6c 6c 79 2e 20 20 54 6f 6b 65 6e 20 63  rnally.  Token c
36af8 61 6e 27 74 20 62 65 20 4e 55 4c 4c 20 6f 72 20  an't be NULL or 
36af9 65 6d 70 74 79 2e 20 2a 2f 0a 20 20 20 20 69 66  empty. */.    if
36afa 28 20 69 50 6f 73 69 74 69 6f 6e 3c 30 20 7c 7c  ( iPosition<0 ||
36afb 20 70 54 6f 6b 65 6e 20 3d 3d 20 4e 55 4c 4c 20   pToken == NULL 
36afc 7c 7c 20 6e 54 6f 6b 65 6e 42 79 74 65 73 20 3d  || nTokenBytes =
36afd 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  = 0 ){.      rc 
36afe 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
36aff 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
36b00 20 7d 0a 0a 20 20 20 20 70 20 3d 20 66 74 73 33   }..    p = fts3
36b01 48 61 73 68 46 69 6e 64 28 26 76 2d 3e 70 65 6e  HashFind(&v->pen
36b02 64 69 6e 67 54 65 72 6d 73 2c 20 70 54 6f 6b 65  dingTerms, pToke
36b03 6e 2c 20 6e 54 6f 6b 65 6e 42 79 74 65 73 29 3b  n, nTokenBytes);
36b04 0a 20 20 20 20 69 66 28 20 70 3d 3d 4e 55 4c 4c  .    if( p==NULL
36b05 20 29 7b 0a 20 20 20 20 20 20 6e 44 61 74 61 20   ){.      nData 
36b06 3d 20 30 3b 0a 20 20 20 20 20 20 70 20 3d 20 64  = 0;.      p = d
36b07 6c 63 4e 65 77 28 69 44 6f 63 69 64 2c 20 44 4c  lcNew(iDocid, DL
36b08 5f 44 45 46 41 55 4c 54 29 3b 0a 20 20 20 20 20  _DEFAULT);.     
36b09 20 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 28   fts3HashInsert(
36b0a 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73  &v->pendingTerms
36b0b 2c 20 70 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  , pToken, nToken
36b0c 42 79 74 65 73 2c 20 70 29 3b 0a 0a 20 20 20 20  Bytes, p);..    
36b0d 20 20 2f 2a 20 4f 76 65 72 68 65 61 64 20 66 6f    /* Overhead fo
36b0e 72 20 6f 75 72 20 68 61 73 68 20 74 61 62 6c 65  r our hash table
36b0f 20 65 6e 74 72 79 2c 20 74 68 65 20 6b 65 79 2c   entry, the key,
36b10 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 2e 20   and the value. 
36b11 2a 2f 0a 20 20 20 20 20 20 76 2d 3e 6e 50 65 6e  */.      v->nPen
36b12 64 69 6e 67 44 61 74 61 20 2b 3d 20 73 69 7a 65  dingData += size
36b13 6f 66 28 73 74 72 75 63 74 20 66 74 73 33 48 61  of(struct fts3Ha
36b14 73 68 45 6c 65 6d 29 2b 73 69 7a 65 6f 66 28 2a  shElem)+sizeof(*
36b15 70 29 2b 6e 54 6f 6b 65 6e 42 79 74 65 73 3b 0a  p)+nTokenBytes;.
36b16 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
36b17 20 6e 44 61 74 61 20 3d 20 70 2d 3e 62 2e 6e 44   nData = p->b.nD
36b18 61 74 61 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ata;.      if( p
36b19 2d 3e 64 6c 77 2e 69 50 72 65 76 44 6f 63 69 64  ->dlw.iPrevDocid
36b1a 21 3d 69 44 6f 63 69 64 20 29 20 64 6c 63 4e 65  !=iDocid ) dlcNe
36b1b 78 74 28 70 2c 20 69 44 6f 63 69 64 29 3b 0a 20  xt(p, iDocid);. 
36b1c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f     }.    if( iCo
36b1d 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20  lumn>=0 ){.     
36b1e 20 64 6c 63 41 64 64 50 6f 73 28 70 2c 20 69 43   dlcAddPos(p, iC
36b1f 6f 6c 75 6d 6e 2c 20 69 50 6f 73 69 74 69 6f 6e  olumn, iPosition
36b20 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  , iStartOffset, 
36b21 69 45 6e 64 4f 66 66 73 65 74 29 3b 0a 20 20 20  iEndOffset);.   
36b22 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 63 63 75 6d   }..    /* Accum
36b23 75 6c 61 74 65 20 64 61 74 61 20 61 64 64 65 64  ulate data added
36b24 20 62 79 20 64 6c 63 4e 65 77 20 6f 72 20 64 6c   by dlcNew or dl
36b25 63 4e 65 78 74 2c 20 61 6e 64 20 64 6c 63 41 64  cNext, and dlcAd
36b26 64 50 6f 73 2e 20 2a 2f 0a 20 20 20 20 76 2d 3e  dPos. */.    v->
36b27 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 2b 3d 20  nPendingData += 
36b28 70 2d 3e 62 2e 6e 44 61 74 61 2d 6e 44 61 74 61  p->b.nData-nData
36b29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 4f 44 4f  ;.  }..  /* TODO
36b2a 28 73 68 65 73 73 29 20 43 68 65 63 6b 20 72 65  (shess) Check re
36b2b 74 75 72 6e 3f 20 20 53 68 6f 75 6c 64 20 74 68  turn?  Should th
36b2c 69 73 20 62 65 20 61 62 6c 65 20 74 6f 20 63 61  is be able to ca
36b2d 75 73 65 20 65 72 72 6f 72 73 20 61 74 0a 20 20  use errors at.  
36b2e 2a 2a 20 74 68 69 73 20 70 6f 69 6e 74 3f 20 20  ** this point?  
36b2f 41 63 74 75 61 6c 6c 79 2c 20 73 61 6d 65 20 71  Actually, same q
36b30 75 65 73 74 69 6f 6e 20 61 62 6f 75 74 20 73 71  uestion about sq
36b31 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
36b32 2c 0a 20 20 2a 2a 20 74 68 6f 75 67 68 20 6f 6e  ,.  ** though on
36b33 65 20 63 6f 75 6c 64 20 61 72 67 75 65 20 74 68  e could argue th
36b34 61 74 20 66 61 69 6c 75 72 65 20 74 68 65 72 65  at failure there
36b35 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
36b36 64 61 74 61 20 69 73 0a 20 20 2a 2a 20 6e 6f 74  data is.  ** not
36b37 20 64 75 72 61 62 6c 65 2e 20 20 2a 70 6f 6e 64   durable.  *pond
36b38 65 72 2a 0a 20 20 2a 2f 0a 20 20 70 54 6f 6b 65  er*.  */.  pToke
36b39 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e  nizer->pModule->
36b3a 78 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29 3b  xClose(pCursor);
36b3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 44 4f  .  if( SQLITE_DO
36b3c 4e 45 20 3d 3d 20 72 63 20 29 20 72 65 74 75 72  NE == rc ) retur
36b3d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72  n SQLITE_OK;.  r
36b3e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
36b3f 41 64 64 20 64 6f 63 6c 69 73 74 73 20 66 6f 72  Add doclists for
36b40 20 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 5b 70   all terms in [p
36b41 56 61 6c 75 65 73 5d 20 74 6f 20 70 65 6e 64 69  Values] to pendi
36b42 6e 67 54 65 72 6d 73 20 74 61 62 6c 65 2e 20 2a  ngTerms table. *
36b43 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 73  /.static int ins
36b44 65 72 74 54 65 72 6d 73 28 66 75 6c 6c 74 65 78  ertTerms(fulltex
36b45 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
36b46 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 0a  e_int64 iDocid,.
36b47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b48 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76         sqlite3_v
36b49 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 29 7b  alue **pValues){
36b4a 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
36b4b 69 20 3d 20 30 3b 20 69 20 3c 20 76 2d 3e 6e 43  i = 0; i < v->nC
36b4c 6f 6c 75 6d 6e 20 3b 20 2b 2b 69 29 7b 0a 20 20  olumn ; ++i){.  
36b4d 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20    char *zText = 
36b4e 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76  (char*)sqlite3_v
36b4f 61 6c 75 65 5f 74 65 78 74 28 70 56 61 6c 75 65  alue_text(pValue
36b50 73 5b 69 5d 29 3b 0a 20 20 20 20 69 6e 74 20 72  s[i]);.    int r
36b51 63 20 3d 20 62 75 69 6c 64 54 65 72 6d 73 28 76  c = buildTerms(v
36b52 2c 20 69 44 6f 63 69 64 2c 20 7a 54 65 78 74 2c  , iDocid, zText,
36b53 20 69 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21   i);.    if( rc!
36b54 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
36b55 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65  urn rc;.  }.  re
36b56 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
36b57 7d 0a 0a 2f 2a 20 41 64 64 20 65 6d 70 74 79 20  }../* Add empty 
36b58 64 6f 63 6c 69 73 74 73 20 66 6f 72 20 61 6c 6c  doclists for all
36b59 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 67 69   terms in the gi
36b5a 76 65 6e 20 72 6f 77 27 73 20 63 6f 6e 74 65 6e  ven row's conten
36b5b 74 20 74 6f 0a 2a 2a 20 70 65 6e 64 69 6e 67 54  t to.** pendingT
36b5c 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  erms..*/.static 
36b5d 69 6e 74 20 64 65 6c 65 74 65 54 65 72 6d 73 28  int deleteTerms(
36b5e 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
36b5f 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
36b60 44 6f 63 69 64 29 7b 0a 20 20 63 6f 6e 73 74 20  Docid){.  const 
36b61 63 68 61 72 20 2a 2a 70 56 61 6c 75 65 73 3b 0a  char **pValues;.
36b62 20 20 69 6e 74 20 69 2c 20 72 63 3b 0a 0a 20 20    int i, rc;..  
36b63 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 53  /* TODO(shess) S
36b64 68 6f 75 6c 64 20 77 65 20 61 6c 6c 6f 77 20 73  hould we allow s
36b65 75 63 68 20 74 61 62 6c 65 73 20 61 74 20 61 6c  uch tables at al
36b66 6c 3f 20 2a 2f 0a 20 20 69 66 28 20 44 4c 5f 44  l? */.  if( DL_D
36b67 45 46 41 55 4c 54 3d 3d 44 4c 5f 44 4f 43 49 44  EFAULT==DL_DOCID
36b68 53 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  S ) return SQLIT
36b69 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 72 63 20 3d  E_ERROR;..  rc =
36b6a 20 63 6f 6e 74 65 6e 74 5f 73 65 6c 65 63 74 28   content_select(
36b6b 76 2c 20 69 44 6f 63 69 64 2c 20 26 70 56 61 6c  v, iDocid, &pVal
36b6c 75 65 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ues);.  if( rc!=
36b6d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
36b6e 72 6e 20 72 63 3b 0a 0a 20 20 66 6f 72 28 69 20  rn rc;..  for(i 
36b6f 3d 20 30 20 3b 20 69 20 3c 20 76 2d 3e 6e 43 6f  = 0 ; i < v->nCo
36b70 6c 75 6d 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20 20  lumn; ++i) {.   
36b71 20 72 63 20 3d 20 62 75 69 6c 64 54 65 72 6d 73   rc = buildTerms
36b72 28 76 2c 20 69 44 6f 63 69 64 2c 20 70 56 61 6c  (v, iDocid, pVal
36b73 75 65 73 5b 69 5d 2c 20 2d 31 29 3b 0a 20 20 20  ues[i], -1);.   
36b74 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
36b75 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  OK ) break;.  }.
36b76 0a 20 20 66 72 65 65 53 74 72 69 6e 67 41 72 72  .  freeStringArr
36b77 61 79 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 70  ay(v->nColumn, p
36b78 56 61 6c 75 65 73 29 3b 0a 20 20 72 65 74 75 72  Values);.  retur
36b79 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
36b7a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 52  /* TODO(shess) R
36b7b 65 66 61 63 74 6f 72 20 74 68 65 20 63 6f 64 65  efactor the code
36b7c 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 69 73 20   to remove this 
36b7d 66 6f 72 77 61 72 64 20 64 65 63 6c 2e 20 2a 2f  forward decl. */
36b7e 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 69 74  .static int init
36b7f 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c  PendingTerms(ful
36b80 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
36b81 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
36b82 69 64 29 3b 0a 0a 2f 2a 20 49 6e 73 65 72 74 20  id);../* Insert 
36b83 61 20 72 6f 77 20 69 6e 74 6f 20 74 68 65 20 25  a row into the %
36b84 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20  _content table; 
36b85 73 65 74 20 2a 70 69 44 6f 63 69 64 20 74 6f 20  set *piDocid to 
36b86 62 65 20 74 68 65 20 49 44 20 6f 66 20 74 68 65  be the ID of the
36b87 0a 2a 2a 20 6e 65 77 20 72 6f 77 2e 20 20 41 64  .** new row.  Ad
36b88 64 20 64 6f 63 6c 69 73 74 73 20 66 6f 72 20 74  d doclists for t
36b89 65 72 6d 73 20 74 6f 20 70 65 6e 64 69 6e 67 54  erms to pendingT
36b8a 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  erms..*/.static 
36b8b 69 6e 74 20 69 6e 64 65 78 5f 69 6e 73 65 72 74  int index_insert
36b8c 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
36b8d 76 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  v, sqlite3_value
36b8e 20 2a 70 52 65 71 75 65 73 74 44 6f 63 69 64 2c   *pRequestDocid,
36b8f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36b90 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
36b91 5f 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73  _value **pValues
36b92 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a  , sqlite_int64 *
36b93 70 69 44 6f 63 69 64 29 7b 0a 20 20 69 6e 74 20  piDocid){.  int 
36b94 72 63 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 74  rc;..  rc = cont
36b95 65 6e 74 5f 69 6e 73 65 72 74 28 76 2c 20 70 52  ent_insert(v, pR
36b96 65 71 75 65 73 74 44 6f 63 69 64 2c 20 70 56 61  equestDocid, pVa
36b97 6c 75 65 73 29 3b 20 20 2f 2a 20 65 78 65 63 75  lues);  /* execu
36b98 74 65 20 61 6e 20 53 51 4c 20 49 4e 53 45 52 54  te an SQL INSERT
36b99 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51   */.  if( rc!=SQ
36b9a 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
36b9b 20 72 63 3b 0a 0a 20 20 2f 2a 20 64 6f 63 69 64   rc;..  /* docid
36b9c 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 61 6c   column is an al
36b9d 69 61 73 20 66 6f 72 20 72 6f 77 69 64 2e 20 2a  ias for rowid. *
36b9e 2f 0a 20 20 2a 70 69 44 6f 63 69 64 20 3d 20 73  /.  *piDocid = s
36b9f 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
36ba0 72 74 5f 72 6f 77 69 64 28 76 2d 3e 64 62 29 3b  rt_rowid(v->db);
36ba1 0a 20 20 72 63 20 3d 20 69 6e 69 74 50 65 6e 64  .  rc = initPend
36ba2 69 6e 67 54 65 72 6d 73 28 76 2c 20 2a 70 69 44  ingTerms(v, *piD
36ba3 6f 63 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ocid);.  if( rc!
36ba4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
36ba5 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72  urn rc;..  retur
36ba6 6e 20 69 6e 73 65 72 74 54 65 72 6d 73 28 76 2c  n insertTerms(v,
36ba7 20 2a 70 69 44 6f 63 69 64 2c 20 70 56 61 6c 75   *piDocid, pValu
36ba8 65 73 29 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74  es);.}../* Delet
36ba9 65 20 61 20 72 6f 77 20 66 72 6f 6d 20 74 68 65  e a row from the
36baa 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65   %_content table
36bab 3b 20 61 64 64 20 65 6d 70 74 79 20 64 6f 63 6c  ; add empty docl
36bac 69 73 74 73 20 66 6f 72 20 74 65 72 6d 73 0a 2a  ists for terms.*
36bad 2a 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d  * to pendingTerm
36bae 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
36baf 20 69 6e 64 65 78 5f 64 65 6c 65 74 65 28 66 75   index_delete(fu
36bb0 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
36bb1 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 6f  sqlite_int64 iRo
36bb2 77 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 69  w){.  int rc = i
36bb3 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d 73 28  nitPendingTerms(
36bb4 76 2c 20 69 52 6f 77 29 3b 0a 20 20 69 66 28 20  v, iRow);.  if( 
36bb5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
36bb6 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
36bb7 20 3d 20 64 65 6c 65 74 65 54 65 72 6d 73 28 76   = deleteTerms(v
36bb8 2c 20 69 52 6f 77 29 3b 0a 20 20 69 66 28 20 72  , iRow);.  if( r
36bb9 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
36bba 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74  eturn rc;..  ret
36bbb 75 72 6e 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 65  urn content_dele
36bbc 74 65 28 76 2c 20 69 52 6f 77 29 3b 20 20 2f 2a  te(v, iRow);  /*
36bbd 20 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20   execute an SQL 
36bbe 44 45 4c 45 54 45 20 2a 2f 0a 7d 0a 0a 2f 2a 20  DELETE */.}../* 
36bbf 55 70 64 61 74 65 20 61 20 72 6f 77 20 69 6e 20  Update a row in 
36bc0 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61  the %_content ta
36bc1 62 6c 65 3b 20 61 64 64 20 64 65 6c 65 74 65 20  ble; add delete 
36bc2 64 6f 63 6c 69 73 74 73 20 74 6f 0a 2a 2a 20 70  doclists to.** p
36bc3 65 6e 64 69 6e 67 54 65 72 6d 73 20 66 6f 72 20  endingTerms for 
36bc4 6f 6c 64 20 74 65 72 6d 73 20 6e 6f 74 20 69 6e  old terms not in
36bc5 20 74 68 65 20 6e 65 77 20 64 61 74 61 2c 20 61   the new data, a
36bc6 64 64 20 69 6e 73 65 72 74 20 64 6f 63 6c 69 73  dd insert doclis
36bc7 74 73 0a 2a 2a 20 74 6f 20 70 65 6e 64 69 6e 67  ts.** to pending
36bc8 54 65 72 6d 73 20 66 6f 72 20 74 65 72 6d 73 20  Terms for terms 
36bc9 69 6e 20 74 68 65 20 6e 65 77 20 64 61 74 61 2e  in the new data.
36bca 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
36bcb 6e 64 65 78 5f 75 70 64 61 74 65 28 66 75 6c 6c  ndex_update(full
36bcc 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71  text_vtab *v, sq
36bcd 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c  lite_int64 iRow,
36bce 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36bcf 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
36bd0 5f 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73  _value **pValues
36bd1 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 69 6e  ){.  int rc = in
36bd2 69 74 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  itPendingTerms(v
36bd3 2c 20 69 52 6f 77 29 3b 0a 20 20 69 66 28 20 72  , iRow);.  if( r
36bd4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
36bd5 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
36bd6 47 65 6e 65 72 61 74 65 20 61 6e 20 65 6d 70 74  Generate an empt
36bd7 79 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 65 61  y doclist for ea
36bd8 63 68 20 74 65 72 6d 20 74 68 61 74 20 70 72 65  ch term that pre
36bd9 76 69 6f 75 73 6c 79 20 61 70 70 65 61 72 65 64  viously appeared
36bda 20 69 6e 20 74 68 69 73 0a 20 20 20 2a 20 72 6f   in this.   * ro
36bdb 77 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 64 65 6c  w. */.  rc = del
36bdc 65 74 65 54 65 72 6d 73 28 76 2c 20 69 52 6f 77  eteTerms(v, iRow
36bdd 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36bde 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
36bdf 72 63 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 74  rc;..  rc = cont
36be0 65 6e 74 5f 75 70 64 61 74 65 28 76 2c 20 70 56  ent_update(v, pV
36be1 61 6c 75 65 73 2c 20 69 52 6f 77 29 3b 20 20 2f  alues, iRow);  /
36be2 2a 20 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c  * execute an SQL
36be3 20 55 50 44 41 54 45 20 2a 2f 0a 20 20 69 66 28   UPDATE */.  if(
36be4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
36be5 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
36be6 2a 20 4e 6f 77 20 61 64 64 20 70 6f 73 69 74 69  * Now add positi
36be7 6f 6e 73 20 66 6f 72 20 74 65 72 6d 73 20 77 68  ons for terms wh
36be8 69 63 68 20 61 70 70 65 61 72 20 69 6e 20 74 68  ich appear in th
36be9 65 20 75 70 64 61 74 65 64 20 72 6f 77 2e 20 2a  e updated row. *
36bea 2f 0a 20 20 72 65 74 75 72 6e 20 69 6e 73 65 72  /.  return inser
36beb 74 54 65 72 6d 73 28 76 2c 20 69 52 6f 77 2c 20  tTerms(v, iRow, 
36bec 70 56 61 6c 75 65 73 29 3b 0a 7d 0a 0a 2f 2a 2a  pValues);.}../**
36bed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36bee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36bef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36bf1 2a 2f 0a 2f 2a 20 49 6e 74 65 72 69 6f 72 57 72  */./* InteriorWr
36bf2 69 74 65 72 20 69 73 20 75 73 65 64 20 74 6f 20  iter is used to 
36bf3 63 6f 6c 6c 65 63 74 20 74 65 72 6d 73 20 61 6e  collect terms an
36bf4 64 20 62 6c 6f 63 6b 20 72 65 66 65 72 65 6e 63  d block referenc
36bf5 65 73 20 69 6e 74 6f 0a 2a 2a 20 69 6e 74 65 72  es into.** inter
36bf6 69 6f 72 20 6e 6f 64 65 73 20 69 6e 20 25 5f 73  ior nodes in %_s
36bf7 65 67 6d 65 6e 74 73 2e 20 20 53 65 65 20 63 6f  egments.  See co
36bf8 6d 6d 65 6e 74 61 72 79 20 61 74 20 74 6f 70 20  mmentary at top 
36bf9 6f 66 20 66 69 6c 65 20 66 6f 72 0a 2a 2a 20 66  of file for.** f
36bfa 6f 72 6d 61 74 2e 0a 2a 2f 0a 0a 2f 2a 20 48 6f  ormat..*/../* Ho
36bfb 77 20 6c 61 72 67 65 20 69 6e 74 65 72 69 6f 72  w large interior
36bfc 20 6e 6f 64 65 73 20 63 61 6e 20 67 72 6f 77 2e   nodes can grow.
36bfd 20 2a 2f 0a 23 64 65 66 69 6e 65 20 49 4e 54 45   */.#define INTE
36bfe 52 49 4f 52 5f 4d 41 58 20 32 30 34 38 0a 0a 2f  RIOR_MAX 2048../
36bff 2a 20 4d 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72  * Minimum number
36c00 20 6f 66 20 74 65 72 6d 73 20 70 65 72 20 69 6e   of terms per in
36c01 74 65 72 69 6f 72 20 6e 6f 64 65 20 28 65 78 63  terior node (exc
36c02 65 70 74 20 74 68 65 20 72 6f 6f 74 29 2e 20 54  ept the root). T
36c03 68 69 73 0a 2a 2a 20 70 72 65 76 65 6e 74 73 20  his.** prevents 
36c04 6c 61 72 67 65 20 74 65 72 6d 73 20 66 72 6f 6d  large terms from
36c05 20 6d 61 6b 69 6e 67 20 74 68 65 20 74 72 65 65   making the tree
36c06 20 74 6f 6f 20 73 6b 69 6e 6e 79 20 2d 20 6d 75   too skinny - mu
36c07 73 74 20 62 65 20 3e 30 0a 2a 2a 20 73 6f 20 74  st be >0.** so t
36c08 68 61 74 20 74 68 65 20 74 72 65 65 20 61 6c 77  hat the tree alw
36c09 61 79 73 20 6d 61 6b 65 73 20 70 72 6f 67 72 65  ays makes progre
36c0a 73 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74  ss.  Note that t
36c0b 68 65 20 6d 69 6e 20 74 72 65 65 0a 2a 2a 20 66  he min tree.** f
36c0c 61 6e 6f 75 74 20 77 69 6c 6c 20 62 65 20 49 4e  anout will be IN
36c0d 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53  TERIOR_MIN_TERMS
36c0e 2b 31 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 49  +1..*/.#define I
36c0f 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d  NTERIOR_MIN_TERM
36c10 53 20 37 0a 23 69 66 20 49 4e 54 45 52 49 4f 52  S 7.#if INTERIOR
36c11 5f 4d 49 4e 5f 54 45 52 4d 53 3c 31 0a 23 20 65  _MIN_TERMS<1.# e
36c12 72 72 6f 72 20 49 4e 54 45 52 49 4f 52 5f 4d 49  rror INTERIOR_MI
36c13 4e 5f 54 45 52 4d 53 20 6d 75 73 74 20 62 65 20  N_TERMS must be 
36c14 67 72 65 61 74 65 72 20 74 68 61 6e 20 30 2e 0a  greater than 0..
36c15 23 65 6e 64 69 66 0a 0a 2f 2a 20 52 4f 4f 54 5f  #endif../* ROOT_
36c16 4d 41 58 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77  MAX controls how
36c17 20 6d 75 63 68 20 64 61 74 61 20 69 73 20 73 74   much data is st
36c18 6f 72 65 64 20 69 6e 6c 69 6e 65 20 69 6e 20 74  ored inline in t
36c19 68 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 64 69  he segment.** di
36c1a 72 65 63 74 6f 72 79 2e 0a 2a 2f 0a 2f 2a 20 54  rectory..*/./* T
36c1b 4f 44 4f 28 73 68 65 73 73 29 20 50 75 73 68 20  ODO(shess) Push 
36c1c 52 4f 4f 54 5f 4d 41 58 20 64 6f 77 6e 20 74 6f  ROOT_MAX down to
36c1d 20 77 68 6f 65 76 65 72 20 69 73 20 77 72 69 74   whoever is writ
36c1e 69 6e 67 20 74 68 69 6e 67 73 2e 20 20 49 74 27  ing things.  It'
36c1f 73 0a 2a 2a 20 6f 6e 6c 79 20 68 65 72 65 20 73  s.** only here s
36c20 6f 20 74 68 61 74 20 69 6e 74 65 72 69 6f 72 57  o that interiorW
36c21 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 29 20  riterRootInfo() 
36c22 61 6e 64 20 6c 65 61 66 57 72 69 74 65 72 52 6f  and leafWriterRo
36c23 6f 74 49 6e 66 6f 28 29 0a 2a 2a 20 63 61 6e 20  otInfo().** can 
36c24 62 6f 74 68 20 73 65 65 20 69 74 2c 20 62 75 74  both see it, but
36c25 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20 70   if the caller p
36c26 61 73 73 65 64 20 69 74 20 69 6e 2c 20 77 65 20  assed it in, we 
36c27 77 6f 75 6c 64 6e 27 74 20 65 76 65 6e 0a 2a 2a  wouldn't even.**
36c28 20 6e 65 65 64 20 61 20 64 65 66 69 6e 65 2e 0a   need a define..
36c29 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 4f 54 5f  */.#define ROOT_
36c2a 4d 41 58 20 31 30 32 34 0a 23 69 66 20 52 4f 4f  MAX 1024.#if ROO
36c2b 54 5f 4d 41 58 3c 56 41 52 49 4e 54 5f 4d 41 58  T_MAX<VARINT_MAX
36c2c 2a 32 0a 23 20 65 72 72 6f 72 20 52 4f 4f 54 5f  *2.# error ROOT_
36c2d 4d 41 58 20 6d 75 73 74 20 68 61 76 65 20 65 6e  MAX must have en
36c2e 6f 75 67 68 20 73 70 61 63 65 20 66 6f 72 20 61  ough space for a
36c2f 20 68 65 61 64 65 72 2e 0a 23 65 6e 64 69 66 0a   header..#endif.
36c30 0a 2f 2a 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63  ./* InteriorBloc
36c31 6b 20 73 74 6f 72 65 73 20 61 20 6c 69 6e 6b 65  k stores a linke
36c32 64 2d 6c 69 73 74 20 6f 66 20 69 6e 74 65 72 69  d-list of interi
36c33 6f 72 20 62 6c 6f 63 6b 73 20 77 68 69 6c 65 20  or blocks while 
36c34 61 20 6c 6f 77 65 72 0a 2a 2a 20 6c 61 79 65 72  a lower.** layer
36c35 20 69 73 20 62 65 69 6e 67 20 63 6f 6e 73 74 72   is being constr
36c36 75 63 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65  ucted..*/.typede
36c37 66 20 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f  f struct Interio
36c38 72 42 6c 6f 63 6b 20 7b 0a 20 20 44 61 74 61 42  rBlock {.  DataB
36c39 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20 20 20  uffer term;     
36c3a 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 6d 6f 73        /* Leftmos
36c3b 74 20 74 65 72 6d 20 69 6e 20 62 6c 6f 63 6b 27  t term in block'
36c3c 73 20 73 75 62 74 72 65 65 2e 20 2a 2f 0a 20 20  s subtree. */.  
36c3d 44 61 74 61 42 75 66 66 65 72 20 64 61 74 61 3b  DataBuffer data;
36c3e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63             /* Ac
36c3f 63 75 6d 75 6c 61 74 65 64 20 64 61 74 61 20 66  cumulated data f
36c40 6f 72 20 74 68 65 20 62 6c 6f 63 6b 2e 20 2a 2f  or the block. */
36c41 0a 20 20 73 74 72 75 63 74 20 49 6e 74 65 72 69  .  struct Interi
36c42 6f 72 42 6c 6f 63 6b 20 2a 6e 65 78 74 3b 0a 7d  orBlock *next;.}
36c43 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 3b 0a   InteriorBlock;.
36c44 0a 73 74 61 74 69 63 20 49 6e 74 65 72 69 6f 72  .static Interior
36c45 42 6c 6f 63 6b 20 2a 69 6e 74 65 72 69 6f 72 42  Block *interiorB
36c46 6c 6f 63 6b 4e 65 77 28 69 6e 74 20 69 48 65 69  lockNew(int iHei
36c47 67 68 74 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  ght, sqlite_int6
36c48 34 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 0a 20  4 iChildBlock,. 
36c49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c4b 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
36c4c 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
36c4d 72 6d 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 42  rm){.  InteriorB
36c4e 6c 6f 63 6b 20 2a 62 6c 6f 63 6b 20 3d 20 73 71  lock *block = sq
36c4f 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
36c50 65 6f 66 28 49 6e 74 65 72 69 6f 72 42 6c 6f 63  eof(InteriorBloc
36c51 6b 29 29 3b 0a 20 20 63 68 61 72 20 63 5b 56 41  k));.  char c[VA
36c52 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49 4e 54 5f  RINT_MAX+VARINT_
36c53 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a  MAX];.  int n;..
36c54 20 20 69 66 28 20 62 6c 6f 63 6b 20 29 7b 0a 20    if( block ){. 
36c55 20 20 20 6d 65 6d 73 65 74 28 62 6c 6f 63 6b 2c     memset(block,
36c56 20 30 2c 20 73 69 7a 65 6f 66 28 2a 62 6c 6f 63   0, sizeof(*bloc
36c57 6b 29 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66  k));.    dataBuf
36c58 66 65 72 49 6e 69 74 28 26 62 6c 6f 63 6b 2d 3e  ferInit(&block->
36c59 74 65 72 6d 2c 20 30 29 3b 0a 20 20 20 20 64 61  term, 0);.    da
36c5a 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28  taBufferReplace(
36c5b 26 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2c 20 70 54  &block->term, pT
36c5c 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20  erm, nTerm);..  
36c5d 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72    n = fts3PutVar
36c5e 69 6e 74 28 63 2c 20 69 48 65 69 67 68 74 29 3b  int(c, iHeight);
36c5f 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  .    n += fts3Pu
36c60 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69 43 68  tVarint(c+n, iCh
36c61 69 6c 64 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 64  ildBlock);.    d
36c62 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 62  ataBufferInit(&b
36c63 6c 6f 63 6b 2d 3e 64 61 74 61 2c 20 49 4e 54 45  lock->data, INTE
36c64 52 49 4f 52 5f 4d 41 58 29 3b 0a 20 20 20 20 64  RIOR_MAX);.    d
36c65 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65  ataBufferReplace
36c66 28 26 62 6c 6f 63 6b 2d 3e 64 61 74 61 2c 20 63  (&block->data, c
36c67 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , n);.  }.  retu
36c68 72 6e 20 62 6c 6f 63 6b 3b 0a 7d 0a 0a 23 69 66  rn block;.}..#if
36c69 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56  ndef NDEBUG./* V
36c6a 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64  erify that the d
36c6b 61 74 61 20 69 73 20 72 65 61 64 61 62 6c 65 20  ata is readable 
36c6c 61 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e  as an interior n
36c6d 6f 64 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76  ode. */.static v
36c6e 6f 69 64 20 69 6e 74 65 72 69 6f 72 42 6c 6f 63  oid interiorBloc
36c6f 6b 56 61 6c 69 64 61 74 65 28 49 6e 74 65 72 69  kValidate(Interi
36c70 6f 72 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b 29  orBlock *pBlock)
36c71 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
36c72 70 44 61 74 61 20 3d 20 70 42 6c 6f 63 6b 2d 3e  pData = pBlock->
36c73 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20 69 6e  data.pData;.  in
36c74 74 20 6e 44 61 74 61 20 3d 20 70 42 6c 6f 63 6b  t nData = pBlock
36c75 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 20 20  ->data.nData;.  
36c76 69 6e 74 20 6e 2c 20 69 44 75 6d 6d 79 3b 0a 20  int n, iDummy;. 
36c77 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 42   sqlite_int64 iB
36c78 6c 6f 63 6b 69 64 3b 0a 0a 20 20 61 73 73 65 72  lockid;..  asser
36c79 74 28 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20  t( nData>0 );.  
36c7a 61 73 73 65 72 74 28 20 70 44 61 74 61 21 3d 30  assert( pData!=0
36c7b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   );.  assert( pD
36c7c 61 74 61 2b 6e 44 61 74 61 3e 70 44 61 74 61 20  ata+nData>pData 
36c7d 29 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 6c 65  );..  /* Must le
36c7e 61 64 20 77 69 74 68 20 68 65 69 67 68 74 20 6f  ad with height o
36c7f 66 20 6e 6f 64 65 20 61 73 20 61 20 76 61 72 69  f node as a vari
36c80 6e 74 28 6e 29 2c 20 6e 3e 30 20 2a 2f 0a 20 20  nt(n), n>0 */.  
36c81 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
36c82 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d  t32(pData, &iDum
36c83 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  my);.  assert( n
36c84 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
36c85 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73  iDummy>0 );.  as
36c86 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29 3b  sert( n<nData );
36c87 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20  .  pData += n;. 
36c88 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 0a 20 20   nData -= n;..  
36c89 2f 2a 20 4d 75 73 74 20 63 6f 6e 74 61 69 6e 20  /* Must contain 
36c8a 69 42 6c 6f 63 6b 69 64 2e 20 2a 2f 0a 20 20 6e  iBlockid. */.  n
36c8b 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
36c8c 28 70 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b 69  (pData, &iBlocki
36c8d 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e  d);.  assert( n>
36c8e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
36c8f 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 70 44 61  <=nData );.  pDa
36c90 74 61 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74 61  ta += n;.  nData
36c91 20 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 5a 65 72   -= n;..  /* Zer
36c92 6f 20 6f 72 20 6d 6f 72 65 20 74 65 72 6d 73 20  o or more terms 
36c93 6f 66 20 70 6f 73 69 74 69 76 65 20 6c 65 6e 67  of positive leng
36c94 74 68 20 2a 2f 0a 20 20 69 66 28 20 6e 44 61 74  th */.  if( nDat
36c95 61 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46  a!=0 ){.    /* F
36c96 69 72 73 74 20 74 65 72 6d 20 69 73 20 6e 6f 74  irst term is not
36c97 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 20   delta-encoded. 
36c98 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47  */.    n = fts3G
36c99 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
36c9a 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &iDummy);.    
36c9b 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20  assert( n>0 );. 
36c9c 20 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d     assert( iDumm
36c9d 79 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  y>0 );.    asser
36c9e 74 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 29 3b 0a  t( n+iDummy>0);.
36c9f 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44      assert( n+iD
36ca0 75 6d 6d 79 3c 3d 6e 44 61 74 61 20 29 3b 0a 20  ummy<=nData );. 
36ca1 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44     pData += n+iD
36ca2 75 6d 6d 79 3b 0a 20 20 20 20 6e 44 61 74 61 20  ummy;.    nData 
36ca3 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20  -= n+iDummy;..  
36ca4 20 20 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 74    /* Following t
36ca5 65 72 6d 73 20 64 65 6c 74 61 2d 65 6e 63 6f 64  erms delta-encod
36ca6 65 64 2e 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65  ed. */.    while
36ca7 28 20 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20  ( nData!=0 ){.  
36ca8 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
36ca9 20 73 68 61 72 65 64 20 70 72 65 66 69 78 2e 20   shared prefix. 
36caa 2a 2f 0a 20 20 20 20 20 20 6e 20 3d 20 66 74 73  */.      n = fts
36cab 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61  3GetVarint32(pDa
36cac 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  ta, &iDummy);.  
36cad 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20      assert( n>0 
36cae 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
36caf 20 69 44 75 6d 6d 79 3e 3d 30 20 29 3b 0a 20 20   iDummy>=0 );.  
36cb0 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 6e 44      assert( n<nD
36cb1 61 74 61 20 29 3b 0a 20 20 20 20 20 20 70 44 61  ata );.      pDa
36cb2 74 61 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 6e  ta += n;.      n
36cb3 44 61 74 61 20 2d 3d 20 6e 3b 0a 0a 20 20 20 20  Data -= n;..    
36cb4 20 20 2f 2a 20 4c 65 6e 67 74 68 20 61 6e 64 20    /* Length and 
36cb5 64 61 74 61 20 6f 66 20 64 69 73 74 69 6e 63 74  data of distinct
36cb6 20 73 75 66 66 69 78 2e 20 2a 2f 0a 20 20 20 20   suffix. */.    
36cb7 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
36cb8 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
36cb9 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 61 73 73  ummy);.      ass
36cba 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20  ert( n>0 );.    
36cbb 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
36cbc 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  >0 );.      asse
36cbd 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 29 3b  rt( n+iDummy>0);
36cbe 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
36cbf 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74 61 20 29  +iDummy<=nData )
36cc0 3b 0a 20 20 20 20 20 20 70 44 61 74 61 20 2b 3d  ;.      pData +=
36cc1 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 20   n+iDummy;.     
36cc2 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d   nData -= n+iDum
36cc3 6d 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  my;.    }.  }.}.
36cc4 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f 56  #define ASSERT_V
36cc5 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c  ALID_INTERIOR_BL
36cc6 4f 43 4b 28 78 29 20 69 6e 74 65 72 69 6f 72 42  OCK(x) interiorB
36cc7 6c 6f 63 6b 56 61 6c 69 64 61 74 65 28 78 29 0a  lockValidate(x).
36cc8 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 41 53  #else.#define AS
36cc9 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52  SERT_VALID_INTER
36cca 49 4f 52 5f 42 4c 4f 43 4b 28 78 29 20 61 73 73  IOR_BLOCK(x) ass
36ccb 65 72 74 28 20 31 20 29 0a 23 65 6e 64 69 66 0a  ert( 1 ).#endif.
36ccc 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
36ccd 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 7b  InteriorWriter {
36cce 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 3b 20  .  int iHeight; 
36ccf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36cd0 20 20 2f 2a 20 66 72 6f 6d 20 30 20 61 74 20 6c    /* from 0 at l
36cd1 65 61 76 65 73 2e 20 2a 2f 0a 20 20 49 6e 74 65  eaves. */.  Inte
36cd2 72 69 6f 72 42 6c 6f 63 6b 20 2a 66 69 72 73 74  riorBlock *first
36cd3 2c 20 2a 6c 61 73 74 3b 0a 20 20 73 74 72 75 63  , *last;.  struc
36cd4 74 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72  t InteriorWriter
36cd5 20 2a 70 61 72 65 6e 74 57 72 69 74 65 72 3b 0a   *parentWriter;.
36cd6 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65  .  DataBuffer te
36cd7 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
36cd8 20 20 2f 2a 20 4c 61 73 74 20 74 65 72 6d 20 77    /* Last term w
36cd9 72 69 74 74 65 6e 20 74 6f 20 62 6c 6f 63 6b 20  ritten to block 
36cda 22 6c 61 73 74 22 2e 20 2a 2f 0a 20 20 73 71 6c  "last". */.  sql
36cdb 69 74 65 5f 69 6e 74 36 34 20 69 4f 70 65 6e 69  ite_int64 iOpeni
36cdc 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 3b 20 2f 2a  ngChildBlock; /*
36cdd 20 46 69 72 73 74 20 63 68 69 6c 64 20 62 6c 6f   First child blo
36cde 63 6b 20 69 6e 20 62 6c 6f 63 6b 20 22 6c 61 73  ck in block "las
36cdf 74 22 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  t". */.#ifndef N
36ce0 44 45 42 55 47 0a 20 20 73 71 6c 69 74 65 5f 69  DEBUG.  sqlite_i
36ce1 6e 74 36 34 20 69 4c 61 73 74 43 68 69 6c 64 42  nt64 iLastChildB
36ce2 6c 6f 63 6b 3b 20 20 2f 2a 20 66 6f 72 20 63 6f  lock;  /* for co
36ce3 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b 73  nsistency checks
36ce4 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 20 49 6e  . */.#endif.} In
36ce5 74 65 72 69 6f 72 57 72 69 74 65 72 3b 0a 0a 2f  teriorWriter;../
36ce6 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 20  * Initialize an 
36ce7 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 77 68  interior node wh
36ce8 65 72 65 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d  ere pTerm[nTerm]
36ce9 20 6d 61 72 6b 73 20 74 68 65 20 6c 65 66 74 6d   marks the leftm
36cea 6f 73 74 0a 2a 2a 20 74 65 72 6d 20 69 6e 20 74  ost.** term in t
36ceb 68 65 20 74 72 65 65 2e 20 20 69 43 68 69 6c 64  he tree.  iChild
36cec 42 6c 6f 63 6b 20 69 73 20 74 68 65 20 6c 65 66  Block is the lef
36ced 74 6d 6f 73 74 20 63 68 69 6c 64 20 62 6c 6f 63  tmost child bloc
36cee 6b 20 61 74 20 74 68 65 0a 2a 2a 20 6e 65 78 74  k at the.** next
36cef 20 6c 65 76 65 6c 20 64 6f 77 6e 20 74 68 65 20   level down the 
36cf0 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tree..*/.static 
36cf1 76 6f 69 64 20 69 6e 74 65 72 69 6f 72 57 72 69  void interiorWri
36cf2 74 65 72 49 6e 69 74 28 69 6e 74 20 69 48 65 69  terInit(int iHei
36cf3 67 68 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ght, const char 
36cf4 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
36cf5 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m,.             
36cf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36cf7 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
36cf8 43 68 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20 20 20  ChildBlock,.    
36cf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36cfa 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 72             Inter
36cfb 69 6f 72 57 72 69 74 65 72 20 2a 70 57 72 69 74  iorWriter *pWrit
36cfc 65 72 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 42  er){.  InteriorB
36cfd 6c 6f 63 6b 20 2a 62 6c 6f 63 6b 3b 0a 20 20 61  lock *block;.  a
36cfe 73 73 65 72 74 28 20 69 48 65 69 67 68 74 3e 30  ssert( iHeight>0
36cff 20 29 3b 0a 20 20 43 4c 45 41 52 28 70 57 72 69   );.  CLEAR(pWri
36d00 74 65 72 29 3b 0a 0a 20 20 70 57 72 69 74 65 72  ter);..  pWriter
36d01 2d 3e 69 48 65 69 67 68 74 20 3d 20 69 48 65 69  ->iHeight = iHei
36d02 67 68 74 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e  ght;.  pWriter->
36d03 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f  iOpeningChildBlo
36d04 63 6b 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b  ck = iChildBlock
36d05 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
36d06 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 4c 61 73  .  pWriter->iLas
36d07 74 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20 69 43  tChildBlock = iC
36d08 68 69 6c 64 42 6c 6f 63 6b 3b 0a 23 65 6e 64 69  hildBlock;.#endi
36d09 66 0a 20 20 62 6c 6f 63 6b 20 3d 20 69 6e 74 65  f.  block = inte
36d0a 72 69 6f 72 42 6c 6f 63 6b 4e 65 77 28 69 48 65  riorBlockNew(iHe
36d0b 69 67 68 74 2c 20 69 43 68 69 6c 64 42 6c 6f 63  ight, iChildBloc
36d0c 6b 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29  k, pTerm, nTerm)
36d0d 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 6c 61 73  ;.  pWriter->las
36d0e 74 20 3d 20 70 57 72 69 74 65 72 2d 3e 66 69 72  t = pWriter->fir
36d0f 73 74 20 3d 20 62 6c 6f 63 6b 3b 0a 20 20 41 53  st = block;.  AS
36d10 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52  SERT_VALID_INTER
36d11 49 4f 52 5f 42 4c 4f 43 4b 28 70 57 72 69 74 65  IOR_BLOCK(pWrite
36d12 72 2d 3e 6c 61 73 74 29 3b 0a 20 20 64 61 74 61  r->last);.  data
36d13 42 75 66 66 65 72 49 6e 69 74 28 26 70 57 72 69  BufferInit(&pWri
36d14 74 65 72 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a 7d  ter->term, 0);.}
36d15 0a 0a 2f 2a 20 41 70 70 65 6e 64 20 74 68 65 20  ../* Append the 
36d16 63 68 69 6c 64 20 6e 6f 64 65 20 72 6f 6f 74 65  child node roote
36d17 64 20 61 74 20 69 43 68 69 6c 64 42 6c 6f 63 6b  d at iChildBlock
36d18 20 74 6f 20 74 68 65 20 69 6e 74 65 72 69 6f 72   to the interior
36d19 20 6e 6f 64 65 2c 0a 2a 2a 20 77 69 74 68 20 70   node,.** with p
36d1a 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 61 73 20 74  Term[nTerm] as t
36d1b 68 65 20 6c 65 66 74 6d 6f 73 74 20 74 65 72 6d  he leftmost term
36d1c 20 69 6e 20 69 43 68 69 6c 64 42 6c 6f 63 6b 27   in iChildBlock'
36d1d 73 20 73 75 62 74 72 65 65 2e 0a 2a 2f 0a 73 74  s subtree..*/.st
36d1e 61 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69  atic void interi
36d1f 6f 72 57 72 69 74 65 72 41 70 70 65 6e 64 28 49  orWriterAppend(I
36d20 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 2a 70  nteriorWriter *p
36d21 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20  Writer,.        
36d22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d23 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
36d24 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20  har *pTerm, int 
36d25 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20 20  nTerm,.         
36d26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d27 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
36d28 6e 74 36 34 20 69 43 68 69 6c 64 42 6c 6f 63 6b  nt64 iChildBlock
36d29 29 7b 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49  ){.  char c[VARI
36d2a 4e 54 5f 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41  NT_MAX+VARINT_MA
36d2b 58 5d 3b 0a 20 20 69 6e 74 20 6e 2c 20 6e 50 72  X];.  int n, nPr
36d2c 65 66 69 78 20 3d 20 30 3b 0a 0a 20 20 41 53 53  efix = 0;..  ASS
36d2d 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49  ERT_VALID_INTERI
36d2e 4f 52 5f 42 4c 4f 43 4b 28 70 57 72 69 74 65 72  OR_BLOCK(pWriter
36d2f 2d 3e 6c 61 73 74 29 3b 0a 0a 20 20 2f 2a 20 54  ->last);..  /* T
36d30 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 77 72  he first term wr
36d31 69 74 74 65 6e 20 69 6e 74 6f 20 61 6e 20 69 6e  itten into an in
36d32 74 65 72 69 6f 72 20 6e 6f 64 65 20 69 73 20 61  terior node is a
36d33 63 74 75 61 6c 6c 79 0a 20 20 2a 2a 20 61 73 73  ctually.  ** ass
36d34 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
36d35 20 73 65 63 6f 6e 64 20 63 68 69 6c 64 20 61 64   second child ad
36d36 64 65 64 20 28 74 68 65 20 66 69 72 73 74 20 63  ded (the first c
36d37 68 69 6c 64 20 77 61 73 20 61 64 64 65 64 0a 20  hild was added. 
36d38 20 2a 2a 20 69 6e 20 69 6e 74 65 72 69 6f 72 57   ** in interiorW
36d39 72 69 74 65 72 49 6e 69 74 2c 20 6f 72 20 69 6e  riterInit, or in
36d3a 20 74 68 65 20 69 66 20 63 6c 61 75 73 65 20 61   the if clause a
36d3b 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  t the bottom of 
36d3c 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69  this.  ** functi
36d3d 6f 6e 29 2e 20 20 54 68 61 74 20 74 65 72 6d 20  on).  That term 
36d3e 67 65 74 73 20 65 6e 63 6f 64 65 64 20 73 74 72  gets encoded str
36d3f 61 69 67 68 74 20 75 70 2c 20 77 69 74 68 20 6e  aight up, with n
36d40 50 72 65 66 69 78 20 6c 65 66 74 0a 20 20 2a 2a  Prefix left.  **
36d41 20 61 74 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66   at 0..  */.  if
36d42 28 20 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e  ( pWriter->term.
36d43 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20  nData==0 ){.    
36d44 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  n = fts3PutVarin
36d45 74 28 63 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 7d  t(c, nTerm);.  }
36d46 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28  else{.    while(
36d47 20 6e 50 72 65 66 69 78 3c 70 57 72 69 74 65 72   nPrefix<pWriter
36d48 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 26 26 0a  ->term.nData &&.
36d49 20 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d             pTerm
36d4a 5b 6e 50 72 65 66 69 78 5d 3d 3d 70 57 72 69 74  [nPrefix]==pWrit
36d4b 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 5b 6e  er->term.pData[n
36d4c 50 72 65 66 69 78 5d 20 29 7b 0a 20 20 20 20 20  Prefix] ){.     
36d4d 20 6e 50 72 65 66 69 78 2b 2b 3b 0a 20 20 20 20   nPrefix++;.    
36d4e 7d 0a 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50  }..    n = fts3P
36d4f 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 50 72 65  utVarint(c, nPre
36d50 66 69 78 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66  fix);.    n += f
36d51 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e  ts3PutVarint(c+n
36d52 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78 29  , nTerm-nPrefix)
36d53 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 4e  ;.  }..#ifndef N
36d54 44 45 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d  DEBUG.  pWriter-
36d55 3e 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b  >iLastChildBlock
36d56 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73  ++;.#endif.  ass
36d57 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 69 4c  ert( pWriter->iL
36d58 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b 3d 3d 69  astChildBlock==i
36d59 43 68 69 6c 64 42 6c 6f 63 6b 20 29 3b 0a 0a 20  ChildBlock );.. 
36d5a 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 74 6f 20   /* Overflow to 
36d5b 61 20 6e 65 77 20 62 6c 6f 63 6b 20 69 66 20 74  a new block if t
36d5c 68 65 20 6e 65 77 20 74 65 72 6d 20 6d 61 6b 65  he new term make
36d5d 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 62 6c  s the current bl
36d5e 6f 63 6b 0a 20 20 2a 2a 20 74 6f 6f 20 62 69 67  ock.  ** too big
36d5f 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  , and the curren
36d60 74 20 62 6c 6f 63 6b 20 61 6c 72 65 61 64 79 20  t block already 
36d61 68 61 73 20 65 6e 6f 75 67 68 20 74 65 72 6d 73  has enough terms
36d62 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 72  ..  */.  if( pWr
36d63 69 74 65 72 2d 3e 6c 61 73 74 2d 3e 64 61 74 61  iter->last->data
36d64 2e 6e 44 61 74 61 2b 6e 2b 6e 54 65 72 6d 2d 6e  .nData+n+nTerm-n
36d65 50 72 65 66 69 78 3e 49 4e 54 45 52 49 4f 52 5f  Prefix>INTERIOR_
36d66 4d 41 58 20 26 26 0a 20 20 20 20 20 20 69 43 68  MAX &&.      iCh
36d67 69 6c 64 42 6c 6f 63 6b 2d 70 57 72 69 74 65 72  ildBlock-pWriter
36d68 2d 3e 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42  ->iOpeningChildB
36d69 6c 6f 63 6b 3e 49 4e 54 45 52 49 4f 52 5f 4d 49  lock>INTERIOR_MI
36d6a 4e 5f 54 45 52 4d 53 20 29 7b 0a 20 20 20 20 70  N_TERMS ){.    p
36d6b 57 72 69 74 65 72 2d 3e 6c 61 73 74 2d 3e 6e 65  Writer->last->ne
36d6c 78 74 20 3d 20 69 6e 74 65 72 69 6f 72 42 6c 6f  xt = interiorBlo
36d6d 63 6b 4e 65 77 28 70 57 72 69 74 65 72 2d 3e 69  ckNew(pWriter->i
36d6e 48 65 69 67 68 74 2c 20 69 43 68 69 6c 64 42 6c  Height, iChildBl
36d6f 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ock,.           
36d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d72 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20  pTerm, nTerm);. 
36d73 20 20 20 70 57 72 69 74 65 72 2d 3e 6c 61 73 74     pWriter->last
36d74 20 3d 20 70 57 72 69 74 65 72 2d 3e 6c 61 73 74   = pWriter->last
36d75 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 70 57 72 69  ->next;.    pWri
36d76 74 65 72 2d 3e 69 4f 70 65 6e 69 6e 67 43 68 69  ter->iOpeningChi
36d77 6c 64 42 6c 6f 63 6b 20 3d 20 69 43 68 69 6c 64  ldBlock = iChild
36d78 42 6c 6f 63 6b 3b 0a 20 20 20 20 64 61 74 61 42  Block;.    dataB
36d79 75 66 66 65 72 52 65 73 65 74 28 26 70 57 72 69  ufferReset(&pWri
36d7a 74 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 7d 65  ter->term);.  }e
36d7b 6c 73 65 7b 0a 20 20 20 20 64 61 74 61 42 75 66  lse{.    dataBuf
36d7c 66 65 72 41 70 70 65 6e 64 32 28 26 70 57 72 69  ferAppend2(&pWri
36d7d 74 65 72 2d 3e 6c 61 73 74 2d 3e 64 61 74 61 2c  ter->last->data,
36d7e 20 63 2c 20 6e 2c 0a 20 20 20 20 20 20 20 20 20   c, n,.         
36d7f 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 65               pTe
36d80 72 6d 2b 6e 50 72 65 66 69 78 2c 20 6e 54 65 72  rm+nPrefix, nTer
36d81 6d 2d 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20  m-nPrefix);.    
36d82 64 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63  dataBufferReplac
36d83 65 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d  e(&pWriter->term
36d84 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , pTerm, nTerm);
36d85 0a 20 20 7d 0a 20 20 41 53 53 45 52 54 5f 56 41  .  }.  ASSERT_VA
36d86 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f  LID_INTERIOR_BLO
36d87 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61 73 74  CK(pWriter->last
36d88 29 3b 0a 7d 0a 0a 2f 2a 20 46 72 65 65 20 74 68  );.}../* Free th
36d89 65 20 73 70 61 63 65 20 75 73 65 64 20 62 79 20  e space used by 
36d8a 70 57 72 69 74 65 72 2c 20 69 6e 63 6c 75 64 69  pWriter, includi
36d8b 6e 67 20 74 68 65 20 6c 69 6e 6b 65 64 2d 6c 69  ng the linked-li
36d8c 73 74 20 6f 66 0a 2a 2a 20 49 6e 74 65 72 69 6f  st of.** Interio
36d8d 72 42 6c 6f 63 6b 73 2c 20 61 6e 64 20 70 61 72  rBlocks, and par
36d8e 65 6e 74 57 72 69 74 65 72 2c 20 69 66 20 70 72  entWriter, if pr
36d8f 65 73 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  esent..*/.static
36d90 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 57 72 69   int interiorWri
36d91 74 65 72 44 65 73 74 72 6f 79 28 49 6e 74 65 72  terDestroy(Inter
36d92 69 6f 72 57 72 69 74 65 72 20 2a 70 57 72 69 74  iorWriter *pWrit
36d93 65 72 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 42  er){.  InteriorB
36d94 6c 6f 63 6b 20 2a 62 6c 6f 63 6b 20 3d 20 70 57  lock *block = pW
36d95 72 69 74 65 72 2d 3e 66 69 72 73 74 3b 0a 0a 20  riter->first;.. 
36d96 20 77 68 69 6c 65 28 20 62 6c 6f 63 6b 21 3d 4e   while( block!=N
36d97 55 4c 4c 20 29 7b 0a 20 20 20 20 49 6e 74 65 72  ULL ){.    Inter
36d98 69 6f 72 42 6c 6f 63 6b 20 2a 62 20 3d 20 62 6c  iorBlock *b = bl
36d99 6f 63 6b 3b 0a 20 20 20 20 62 6c 6f 63 6b 20 3d  ock;.    block =
36d9a 20 62 6c 6f 63 6b 2d 3e 6e 65 78 74 3b 0a 20 20   block->next;.  
36d9b 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
36d9c 72 6f 79 28 26 62 2d 3e 74 65 72 6d 29 3b 0a 20  roy(&b->term);. 
36d9d 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
36d9e 74 72 6f 79 28 26 62 2d 3e 64 61 74 61 29 3b 0a  troy(&b->data);.
36d9f 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
36da0 28 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  (b);.  }.  if( p
36da1 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72  Writer->parentWr
36da2 69 74 65 72 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20  iter!=NULL ){.  
36da3 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72    interiorWriter
36da4 44 65 73 74 72 6f 79 28 70 57 72 69 74 65 72 2d  Destroy(pWriter-
36da5 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29 3b 0a  >parentWriter);.
36da6 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
36da7 28 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74  (pWriter->parent
36da8 57 72 69 74 65 72 29 3b 0a 20 20 7d 0a 20 20 64  Writer);.  }.  d
36da9 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
36daa 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 29  (&pWriter->term)
36dab 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 57 72  ;.  SCRAMBLE(pWr
36dac 69 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  iter);.  return 
36dad 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
36dae 20 49 66 20 70 57 72 69 74 65 72 20 63 61 6e 20   If pWriter can 
36daf 66 69 74 20 65 6e 74 69 72 65 6c 79 20 69 6e 20  fit entirely in 
36db0 52 4f 4f 54 5f 4d 41 58 2c 20 72 65 74 75 72 6e  ROOT_MAX, return
36db1 20 69 74 20 61 73 20 74 68 65 20 72 6f 6f 74 20   it as the root 
36db2 69 6e 66 6f 0a 2a 2a 20 64 69 72 65 63 74 6c 79  info.** directly
36db3 2c 20 6c 65 61 76 69 6e 67 20 2a 70 69 45 6e 64  , leaving *piEnd
36db4 42 6c 6f 63 6b 69 64 20 75 6e 63 68 61 6e 67 65  Blockid unchange
36db5 64 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 66  d.  Otherwise, f
36db6 6c 75 73 68 0a 2a 2a 20 70 57 72 69 74 65 72 20  lush.** pWriter 
36db7 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 62  to %_segments, b
36db8 75 69 6c 64 69 6e 67 20 61 20 6e 65 77 20 6c 61  uilding a new la
36db9 79 65 72 20 6f 66 20 69 6e 74 65 72 69 6f 72 20  yer of interior 
36dba 6e 6f 64 65 73 2c 20 61 6e 64 0a 2a 2a 20 72 65  nodes, and.** re
36dbb 63 75 72 73 69 76 65 6c 79 20 61 73 6b 20 66 6f  cursively ask fo
36dbc 72 20 74 68 65 69 72 20 72 6f 6f 74 20 69 6e 74  r their root int
36dbd 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  o..*/.static int
36dbe 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 52   interiorWriterR
36dbf 6f 6f 74 49 6e 66 6f 28 66 75 6c 6c 74 65 78 74  ootInfo(fulltext
36dc0 5f 76 74 61 62 20 2a 76 2c 20 49 6e 74 65 72 69  _vtab *v, Interi
36dc1 6f 72 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  orWriter *pWrite
36dc2 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
36dc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36dc4 20 20 20 20 20 63 68 61 72 20 2a 2a 70 70 52 6f       char **ppRo
36dc5 6f 74 49 6e 66 6f 2c 20 69 6e 74 20 2a 70 6e 52  otInfo, int *pnR
36dc6 6f 6f 74 49 6e 66 6f 2c 0a 20 20 20 20 20 20 20  ootInfo,.       
36dc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36dc8 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
36dc9 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42 6c  e_int64 *piEndBl
36dca 6f 63 6b 69 64 29 7b 0a 20 20 49 6e 74 65 72 69  ockid){.  Interi
36dcb 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63 6b 20 3d  orBlock *block =
36dcc 20 70 57 72 69 74 65 72 2d 3e 66 69 72 73 74 3b   pWriter->first;
36dcd 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
36dce 69 42 6c 6f 63 6b 69 64 20 3d 20 30 3b 0a 20 20  iBlockid = 0;.  
36dcf 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 66  int rc;..  /* If
36dd0 20 77 65 20 63 61 6e 20 66 69 74 20 74 68 65 20   we can fit the 
36dd1 73 65 67 6d 65 6e 74 20 69 6e 6c 69 6e 65 20 2a  segment inline *
36dd2 2f 0a 20 20 69 66 28 20 62 6c 6f 63 6b 3d 3d 70  /.  if( block==p
36dd3 57 72 69 74 65 72 2d 3e 6c 61 73 74 20 26 26 20  Writer->last && 
36dd4 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74  block->data.nDat
36dd5 61 3c 52 4f 4f 54 5f 4d 41 58 20 29 7b 0a 20 20  a<ROOT_MAX ){.  
36dd6 20 20 2a 70 70 52 6f 6f 74 49 6e 66 6f 20 3d 20    *ppRootInfo = 
36dd7 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70 44 61 74  block->data.pDat
36dd8 61 3b 0a 20 20 20 20 2a 70 6e 52 6f 6f 74 49 6e  a;.    *pnRootIn
36dd9 66 6f 20 3d 20 62 6c 6f 63 6b 2d 3e 64 61 74 61  fo = block->data
36dda 2e 6e 44 61 74 61 3b 0a 20 20 20 20 72 65 74 75  .nData;.    retu
36ddb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
36ddc 7d 0a 0a 20 20 2f 2a 20 46 6c 75 73 68 20 74 68  }..  /* Flush th
36ddd 65 20 66 69 72 73 74 20 62 6c 6f 63 6b 20 74 6f  e first block to
36dde 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64   %_segments, and
36ddf 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 6c 65   create a new le
36de0 76 65 6c 20 6f 66 0a 20 20 2a 2a 20 69 6e 74 65  vel of.  ** inte
36de1 72 69 6f 72 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a  rior node..  */.
36de2 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49    ASSERT_VALID_I
36de3 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 62 6c  NTERIOR_BLOCK(bl
36de4 6f 63 6b 29 3b 0a 20 20 72 63 20 3d 20 62 6c 6f  ock);.  rc = blo
36de5 63 6b 5f 69 6e 73 65 72 74 28 76 2c 20 62 6c 6f  ck_insert(v, blo
36de6 63 6b 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20  ck->data.pData, 
36de7 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74  block->data.nDat
36de8 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20  a, &iBlockid);. 
36de9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
36dea 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
36deb 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20    *piEndBlockid 
36dec 3d 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20 70  = iBlockid;..  p
36ded 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72  Writer->parentWr
36dee 69 74 65 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  iter = sqlite3_m
36def 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 57  alloc(sizeof(*pW
36df0 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69  riter->parentWri
36df1 74 65 72 29 29 3b 0a 20 20 69 6e 74 65 72 69 6f  ter));.  interio
36df2 72 57 72 69 74 65 72 49 6e 69 74 28 70 57 72 69  rWriterInit(pWri
36df3 74 65 72 2d 3e 69 48 65 69 67 68 74 2b 31 2c 0a  ter->iHeight+1,.
36df4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36df5 20 20 20 20 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d       block->term
36df6 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 74  .pData, block->t
36df7 65 72 6d 2e 6e 44 61 74 61 2c 0a 20 20 20 20 20  erm.nData,.     
36df8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36df9 69 42 6c 6f 63 6b 69 64 2c 20 70 57 72 69 74 65  iBlockid, pWrite
36dfa 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29  r->parentWriter)
36dfb 3b 0a 0a 20 20 2f 2a 20 46 6c 75 73 68 20 61 64  ;..  /* Flush ad
36dfc 64 69 74 69 6f 6e 61 6c 20 62 6c 6f 63 6b 73 20  ditional blocks 
36dfd 61 6e 64 20 61 70 70 65 6e 64 20 74 6f 20 74 68  and append to th
36dfe 65 20 68 69 67 68 65 72 20 69 6e 74 65 72 69 6f  e higher interio
36dff 72 0a 20 20 2a 2a 20 6e 6f 64 65 2e 0a 20 20 2a  r.  ** node..  *
36e00 2f 0a 20 20 66 6f 72 28 62 6c 6f 63 6b 3d 62 6c  /.  for(block=bl
36e01 6f 63 6b 2d 3e 6e 65 78 74 3b 20 62 6c 6f 63 6b  ock->next; block
36e02 21 3d 4e 55 4c 4c 3b 20 62 6c 6f 63 6b 3d 62 6c  !=NULL; block=bl
36e03 6f 63 6b 2d 3e 6e 65 78 74 29 7b 0a 20 20 20 20  ock->next){.    
36e04 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54  ASSERT_VALID_INT
36e05 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 62 6c 6f 63  ERIOR_BLOCK(bloc
36e06 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f  k);.    rc = blo
36e07 63 6b 5f 69 6e 73 65 72 74 28 76 2c 20 62 6c 6f  ck_insert(v, blo
36e08 63 6b 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20  ck->data.pData, 
36e09 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74  block->data.nDat
36e0a 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20  a, &iBlockid);. 
36e0b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
36e0c 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
36e0d 3b 0a 20 20 20 20 2a 70 69 45 6e 64 42 6c 6f 63  ;.    *piEndBloc
36e0e 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64 3b 0a  kid = iBlockid;.
36e0f 0a 20 20 20 20 69 6e 74 65 72 69 6f 72 57 72 69  .    interiorWri
36e10 74 65 72 41 70 70 65 6e 64 28 70 57 72 69 74 65  terAppend(pWrite
36e11 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 2c  r->parentWriter,
36e12 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36e13 20 20 20 20 20 20 20 20 20 20 62 6c 6f 63 6b 2d            block-
36e14 3e 74 65 72 6d 2e 70 44 61 74 61 2c 20 62 6c 6f  >term.pData, blo
36e15 63 6b 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 2c 20  ck->term.nData, 
36e16 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 7d 0a 0a  iBlockid);.  }..
36e17 20 20 2f 2a 20 50 61 72 65 6e 74 20 6e 6f 64 65    /* Parent node
36e18 20 67 65 74 73 20 74 68 65 20 63 68 61 6e 63 65   gets the chance
36e19 20 74 6f 20 62 65 20 74 68 65 20 72 6f 6f 74 2e   to be the root.
36e1a 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 69 6e 74   */.  return int
36e1b 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49  eriorWriterRootI
36e1c 6e 66 6f 28 76 2c 20 70 57 72 69 74 65 72 2d 3e  nfo(v, pWriter->
36e1d 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a 20 20  parentWriter,.  
36e1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70                pp
36e20 52 6f 6f 74 49 6e 66 6f 2c 20 70 6e 52 6f 6f 74  RootInfo, pnRoot
36e21 49 6e 66 6f 2c 20 70 69 45 6e 64 42 6c 6f 63 6b  Info, piEndBlock
36e22 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  id);.}../*******
36e23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 49 6e  *********/./* In
36e27 74 65 72 69 6f 72 52 65 61 64 65 72 20 69 73 20  teriorReader is 
36e28 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f 66 66  used to read off
36e29 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d 20 61   the data from a
36e2a 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 0a  n interior node.
36e2b 2a 2a 20 28 73 65 65 20 63 6f 6d 6d 65 6e 74 20  ** (see comment 
36e2c 61 74 20 74 6f 70 20 6f 66 20 66 69 6c 65 20 66  at top of file f
36e2d 6f 72 20 74 68 65 20 66 6f 72 6d 61 74 29 2e 0a  or the format)..
36e2e 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
36e2f 74 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72  t InteriorReader
36e30 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20   {.  const char 
36e31 2a 70 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44  *pData;.  int nD
36e32 61 74 61 3b 0a 0a 20 20 44 61 74 61 42 75 66 66  ata;..  DataBuff
36e33 65 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20  er term;        
36e34 20 20 2f 2a 20 70 72 65 76 69 6f 75 73 20 74 65    /* previous te
36e35 72 6d 2c 20 66 6f 72 20 64 65 63 6f 64 69 6e 67  rm, for decoding
36e36 20 74 65 72 6d 20 64 65 6c 74 61 2e 20 2a 2f 0a   term delta. */.
36e37 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
36e38 69 42 6c 6f 63 6b 69 64 3b 0a 7d 20 49 6e 74 65  iBlockid;.} Inte
36e39 72 69 6f 72 52 65 61 64 65 72 3b 0a 0a 73 74 61  riorReader;..sta
36e3a 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f  tic void interio
36e3b 72 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 49  rReaderDestroy(I
36e3c 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70  nteriorReader *p
36e3d 52 65 61 64 65 72 29 7b 0a 20 20 64 61 74 61 42  Reader){.  dataB
36e3e 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 52  ufferDestroy(&pR
36e3f 65 61 64 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20  eader->term);.  
36e40 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72  SCRAMBLE(pReader
36e41 29 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  );.}../* TODO(sh
36e42 65 73 73 29 20 54 68 65 20 61 73 73 65 72 74 69  ess) The asserti
36e43 6f 6e 73 20 61 72 65 20 67 72 65 61 74 2c 20 62  ons are great, b
36e44 75 74 20 77 68 61 74 20 69 66 20 77 65 27 72 65  ut what if we're
36e45 20 69 6e 20 4e 44 45 42 55 47 0a 2a 2a 20 61 6e   in NDEBUG.** an
36e46 64 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65 6d  d the blob is em
36e47 70 74 79 20 6f 72 20 6f 74 68 65 72 77 69 73 65  pty or otherwise
36e48 20 63 6f 6e 74 61 69 6e 73 20 73 75 73 70 65 63   contains suspec
36e49 74 20 64 61 74 61 3f 0a 2a 2f 0a 73 74 61 74 69  t data?.*/.stati
36e4a 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f 72 52  c void interiorR
36e4b 65 61 64 65 72 49 6e 69 74 28 63 6f 6e 73 74 20  eaderInit(const 
36e4c 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74  char *pData, int
36e4d 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20   nData,.        
36e4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e4f 20 20 20 20 20 20 20 49 6e 74 65 72 69 6f 72 52         InteriorR
36e50 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
36e51 0a 20 20 69 6e 74 20 6e 2c 20 6e 54 65 72 6d 3b  .  int n, nTerm;
36e52 0a 0a 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61  ..  /* Require a
36e53 74 20 6c 65 61 73 74 20 74 68 65 20 6c 65 61 64  t least the lead
36e54 69 6e 67 20 66 6c 61 67 20 62 79 74 65 20 2a 2f  ing flag byte */
36e55 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61  .  assert( nData
36e56 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
36e57 70 44 61 74 61 5b 30 5d 21 3d 27 5c 30 27 20 29  pData[0]!='\0' )
36e58 3b 0a 0a 20 20 43 4c 45 41 52 28 70 52 65 61 64  ;..  CLEAR(pRead
36e59 65 72 29 3b 0a 0a 20 20 2f 2a 20 44 65 63 6f 64  er);..  /* Decod
36e5a 65 20 74 68 65 20 62 61 73 65 20 62 6c 6f 63 6b  e the base block
36e5b 69 64 2c 20 61 6e 64 20 73 65 74 20 74 68 65 20  id, and set the 
36e5c 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69  cursor to the fi
36e5d 72 73 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 6e  rst term. */.  n
36e5e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
36e5f 28 70 44 61 74 61 2b 31 2c 20 26 70 52 65 61 64  (pData+1, &pRead
36e60 65 72 2d 3e 69 42 6c 6f 63 6b 69 64 29 3b 0a 20  er->iBlockid);. 
36e61 20 61 73 73 65 72 74 28 20 31 2b 6e 3c 3d 6e 44   assert( 1+n<=nD
36e62 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72  ata );.  pReader
36e63 2d 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 2b  ->pData = pData+
36e64 31 2b 6e 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  1+n;.  pReader->
36e65 6e 44 61 74 61 20 3d 20 6e 44 61 74 61 2d 28 31  nData = nData-(1
36e66 2b 6e 29 3b 0a 0a 20 20 2f 2a 20 41 20 73 69 6e  +n);..  /* A sin
36e67 67 6c 65 2d 63 68 69 6c 64 20 69 6e 74 65 72 69  gle-child interi
36e68 6f 72 20 6e 6f 64 65 20 28 73 75 63 68 20 61 73  or node (such as
36e69 20 77 68 65 6e 20 61 20 6c 65 61 66 20 6e 6f 64   when a leaf nod
36e6a 65 20 77 61 73 20 74 6f 6f 0a 20 20 2a 2a 20 6c  e was too.  ** l
36e6b 61 72 67 65 20 66 6f 72 20 74 68 65 20 73 65 67  arge for the seg
36e6c 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 29 20  ment directory) 
36e6d 77 6f 6e 27 74 20 68 61 76 65 20 61 6e 79 20 74  won't have any t
36e6e 65 72 6d 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72  erms..  ** Other
36e6f 77 69 73 65 2c 20 64 65 63 6f 64 65 20 74 68 65  wise, decode the
36e70 20 66 69 72 73 74 20 74 65 72 6d 2e 0a 20 20 2a   first term..  *
36e71 2f 0a 20 20 69 66 28 20 70 52 65 61 64 65 72 2d  /.  if( pReader-
36e72 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  >nData==0 ){.   
36e73 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
36e74 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20  &pReader->term, 
36e75 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  0);.  }else{.   
36e76 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
36e77 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
36e78 61 74 61 2c 20 26 6e 54 65 72 6d 29 3b 0a 20 20  ata, &nTerm);.  
36e79 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
36e7a 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c  (&pReader->term,
36e7b 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74   nTerm);.    dat
36e7c 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 26  aBufferReplace(&
36e7d 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70  pReader->term, p
36e7e 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
36e7f 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 61 73 73   nTerm);.    ass
36e80 65 72 74 28 20 6e 2b 6e 54 65 72 6d 3c 3d 70 52  ert( n+nTerm<=pR
36e81 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a  eader->nData );.
36e82 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61      pReader->pDa
36e83 74 61 20 2b 3d 20 6e 2b 6e 54 65 72 6d 3b 0a 20  ta += n+nTerm;. 
36e84 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74     pReader->nDat
36e85 61 20 2d 3d 20 6e 2b 6e 54 65 72 6d 3b 0a 20 20  a -= n+nTerm;.  
36e86 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  }.}..static int 
36e87 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74  interiorReaderAt
36e88 45 6e 64 28 49 6e 74 65 72 69 6f 72 52 65 61 64  End(InteriorRead
36e89 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
36e8a 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
36e8b 74 65 72 6d 2e 6e 44 61 74 61 3d 3d 30 3b 0a 7d  term.nData==0;.}
36e8c 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f  ..static sqlite_
36e8d 69 6e 74 36 34 20 69 6e 74 65 72 69 6f 72 52 65  int64 interiorRe
36e8e 61 64 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b  aderCurrentBlock
36e8f 69 64 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65  id(InteriorReade
36e90 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72  r *pReader){.  r
36e91 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69  eturn pReader->i
36e92 42 6c 6f 63 6b 69 64 3b 0a 7d 0a 0a 73 74 61 74  Blockid;.}..stat
36e93 69 63 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 52  ic int interiorR
36e94 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 49  eaderTermBytes(I
36e95 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70  nteriorReader *p
36e96 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
36e97 74 28 20 21 69 6e 74 65 72 69 6f 72 52 65 61 64  t( !interiorRead
36e98 65 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29  erAtEnd(pReader)
36e99 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
36e9a 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  ader->term.nData
36e9b 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.}.static const
36e9c 20 63 68 61 72 20 2a 69 6e 74 65 72 69 6f 72 52   char *interiorR
36e9d 65 61 64 65 72 54 65 72 6d 28 49 6e 74 65 72 69  eaderTerm(Interi
36e9e 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
36e9f 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 69  r){.  assert( !i
36ea0 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74 45  nteriorReaderAtE
36ea1 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20  nd(pReader) );. 
36ea2 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
36ea3 3e 74 65 72 6d 2e 70 44 61 74 61 3b 0a 7d 0a 0a  >term.pData;.}..
36ea4 2f 2a 20 53 74 65 70 20 66 6f 72 77 61 72 64 20  /* Step forward 
36ea5 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d  to the next term
36ea6 20 69 6e 20 74 68 65 20 6e 6f 64 65 2e 20 2a 2f   in the node. */
36ea7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74  .static void int
36ea8 65 72 69 6f 72 52 65 61 64 65 72 53 74 65 70 28  eriorReaderStep(
36ea9 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a  InteriorReader *
36eaa 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
36eab 72 74 28 20 21 69 6e 74 65 72 69 6f 72 52 65 61  rt( !interiorRea
36eac 64 65 72 41 74 45 6e 64 28 70 52 65 61 64 65 72  derAtEnd(pReader
36ead 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ) );..  /* If th
36eae 65 20 6c 61 73 74 20 74 65 72 6d 20 68 61 73 20  e last term has 
36eaf 62 65 65 6e 20 72 65 61 64 2c 20 73 69 67 6e 61  been read, signa
36eb0 6c 20 65 6f 66 2c 20 65 6c 73 65 20 63 6f 6e 73  l eof, else cons
36eb1 74 72 75 63 74 20 74 68 65 0a 20 20 2a 2a 20 6e  truct the.  ** n
36eb2 65 78 74 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20  ext term..  */. 
36eb3 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 44   if( pReader->nD
36eb4 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61  ata==0 ){.    da
36eb5 74 61 42 75 66 66 65 72 52 65 73 65 74 28 26 70  taBufferReset(&p
36eb6 52 65 61 64 65 72 2d 3e 74 65 72 6d 29 3b 0a 20  Reader->term);. 
36eb7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
36eb8 6e 2c 20 6e 50 72 65 66 69 78 2c 20 6e 53 75 66  n, nPrefix, nSuf
36eb9 66 69 78 3b 0a 0a 20 20 20 20 6e 20 3d 20 66 74  fix;..    n = ft
36eba 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
36ebb 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e  eader->pData, &n
36ebc 50 72 65 66 69 78 29 3b 0a 20 20 20 20 6e 20 2b  Prefix);.    n +
36ebd 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
36ebe 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
36ebf 2b 6e 2c 20 26 6e 53 75 66 66 69 78 29 3b 0a 0a  +n, &nSuffix);..
36ec0 20 20 20 20 2f 2a 20 54 72 75 6e 63 61 74 65 20      /* Truncate 
36ec1 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d  the current term
36ec2 20 61 6e 64 20 61 70 70 65 6e 64 20 73 75 66 66   and append suff
36ec3 69 78 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20  ix data. */.    
36ec4 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
36ec5 61 74 61 20 3d 20 6e 50 72 65 66 69 78 3b 0a 20  ata = nPrefix;. 
36ec6 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
36ec7 65 6e 64 28 26 70 52 65 61 64 65 72 2d 3e 74 65  end(&pReader->te
36ec8 72 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70 44 61  rm, pReader->pDa
36ec9 74 61 2b 6e 2c 20 6e 53 75 66 66 69 78 29 3b 0a  ta+n, nSuffix);.
36eca 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 6e  .    assert( n+n
36ecb 53 75 66 66 69 78 3c 3d 70 52 65 61 64 65 72 2d  Suffix<=pReader-
36ecc 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 52  >nData );.    pR
36ecd 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20  eader->pData += 
36ece 6e 2b 6e 53 75 66 66 69 78 3b 0a 20 20 20 20 70  n+nSuffix;.    p
36ecf 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d  Reader->nData -=
36ed0 20 6e 2b 6e 53 75 66 66 69 78 3b 0a 20 20 7d 0a   n+nSuffix;.  }.
36ed1 20 20 70 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63    pReader->iBloc
36ed2 6b 69 64 2b 2b 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6d  kid++;.}../* Com
36ed3 70 61 72 65 20 74 68 65 20 63 75 72 72 65 6e 74  pare the current
36ed4 20 74 65 72 6d 20 74 6f 20 70 54 65 72 6d 5b 6e   term to pTerm[n
36ed5 54 65 72 6d 5d 2c 20 72 65 74 75 72 6e 69 6e 67  Term], returning
36ed6 20 73 74 72 63 6d 70 2d 73 74 79 6c 65 0a 2a 2a   strcmp-style.**
36ed7 20 72 65 73 75 6c 74 73 2e 20 20 49 66 20 69 73   results.  If is
36ed8 50 72 65 66 69 78 2c 20 65 71 75 61 6c 69 74 79  Prefix, equality
36ed9 20 6d 65 61 6e 73 20 65 71 75 61 6c 20 74 68 72   means equal thr
36eda 6f 75 67 68 20 6e 54 65 72 6d 20 62 79 74 65 73  ough nTerm bytes
36edb 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36edc 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65  interiorReaderTe
36edd 72 6d 43 6d 70 28 49 6e 74 65 72 69 6f 72 52 65  rmCmp(InteriorRe
36ede 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 0a 20  ader *pReader,. 
36edf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ee1 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
36ee2 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
36ee3 74 20 69 73 50 72 65 66 69 78 29 7b 0a 20 20 63  t isPrefix){.  c
36ee4 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 65 61 64  onst char *pRead
36ee5 65 72 54 65 72 6d 20 3d 20 69 6e 74 65 72 69 6f  erTerm = interio
36ee6 72 52 65 61 64 65 72 54 65 72 6d 28 70 52 65 61  rReaderTerm(pRea
36ee7 64 65 72 29 3b 0a 20 20 69 6e 74 20 6e 52 65 61  der);.  int nRea
36ee8 64 65 72 54 65 72 6d 20 3d 20 69 6e 74 65 72 69  derTerm = interi
36ee9 6f 72 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  orReaderTermByte
36eea 73 28 70 52 65 61 64 65 72 29 3b 0a 20 20 69 6e  s(pReader);.  in
36eeb 74 20 63 2c 20 6e 20 3d 20 6e 52 65 61 64 65 72  t c, n = nReader
36eec 54 65 72 6d 3c 6e 54 65 72 6d 20 3f 20 6e 52 65  Term<nTerm ? nRe
36eed 61 64 65 72 54 65 72 6d 20 3a 20 6e 54 65 72 6d  aderTerm : nTerm
36eee 3b 0a 0a 20 20 69 66 28 20 6e 3d 3d 30 20 29 7b  ;..  if( n==0 ){
36eef 0a 20 20 20 20 69 66 28 20 6e 52 65 61 64 65 72  .    if( nReader
36ef0 54 65 72 6d 3e 30 20 29 20 72 65 74 75 72 6e 20  Term>0 ) return 
36ef1 2d 31 3b 0a 20 20 20 20 69 66 28 20 6e 54 65 72  -1;.    if( nTer
36ef2 6d 3e 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  m>0 ) return 1;.
36ef3 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
36ef4 7d 0a 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28  }..  c = memcmp(
36ef5 70 52 65 61 64 65 72 54 65 72 6d 2c 20 70 54 65  pReaderTerm, pTe
36ef6 72 6d 2c 20 6e 29 3b 0a 20 20 69 66 28 20 63 21  rm, n);.  if( c!
36ef7 3d 30 20 29 20 72 65 74 75 72 6e 20 63 3b 0a 20  =0 ) return c;. 
36ef8 20 69 66 28 20 69 73 50 72 65 66 69 78 20 26 26   if( isPrefix &&
36ef9 20 6e 3d 3d 6e 54 65 72 6d 20 29 20 72 65 74 75   n==nTerm ) retu
36efa 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6e  rn 0;.  return n
36efb 52 65 61 64 65 72 54 65 72 6d 20 2d 20 6e 54 65  ReaderTerm - nTe
36efc 72 6d 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  rm;.}../********
36efd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36efe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36eff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36f00 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61  ********/./* Lea
36f01 66 57 72 69 74 65 72 20 69 73 20 75 73 65 64 20  fWriter is used 
36f02 74 6f 20 63 6f 6c 6c 65 63 74 20 74 65 72 6d 73  to collect terms
36f03 20 61 6e 64 20 61 73 73 6f 63 69 61 74 65 64 20   and associated 
36f04 64 6f 63 6c 69 73 74 20 64 61 74 61 0a 2a 2a 20  doclist data.** 
36f05 69 6e 74 6f 20 6c 65 61 66 20 62 6c 6f 63 6b 73  into leaf blocks
36f06 20 69 6e 20 25 5f 73 65 67 6d 65 6e 74 73 20 28   in %_segments (
36f07 73 65 65 20 74 6f 70 20 6f 66 20 66 69 6c 65 20  see top of file 
36f08 66 6f 72 20 66 6f 72 6d 61 74 20 69 6e 66 6f 29  for format info)
36f09 2e 0a 2a 2a 20 45 78 70 65 63 74 65 64 20 75 73  ..** Expected us
36f0a 61 67 65 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 4c 65  age is:.**.** Le
36f0b 61 66 57 72 69 74 65 72 20 77 72 69 74 65 72 3b  afWriter writer;
36f0c 0a 2a 2a 20 6c 65 61 66 57 72 69 74 65 72 49 6e  .** leafWriterIn
36f0d 69 74 28 30 2c 20 30 2c 20 26 77 72 69 74 65 72  it(0, 0, &writer
36f0e 29 3b 0a 2a 2a 20 77 68 69 6c 65 28 20 73 6f 72  );.** while( sor
36f0f 74 65 64 5f 74 65 72 6d 73 5f 6c 65 66 74 5f 74  ted_terms_left_t
36f10 6f 5f 70 72 6f 63 65 73 73 20 29 7b 0a 2a 2a 20  o_process ){.** 
36f11 20 20 2f 2f 20 64 61 74 61 20 69 73 20 64 6f 63    // data is doc
36f12 6c 69 73 74 20 64 61 74 61 20 66 6f 72 20 74 68  list data for th
36f13 61 74 20 74 65 72 6d 2e 0a 2a 2a 20 20 20 72 63  at term..**   rc
36f14 20 3d 20 6c 65 61 66 57 72 69 74 65 72 53 74 65   = leafWriterSte
36f15 70 28 76 2c 20 26 77 72 69 74 65 72 2c 20 70 54  p(v, &writer, pT
36f16 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 70 44 61 74  erm, nTerm, pDat
36f17 61 2c 20 6e 44 61 74 61 29 3b 0a 2a 2a 20 20 20  a, nData);.**   
36f18 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
36f19 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 2a 2a  K ) goto err;.**
36f1a 20 7d 0a 2a 2a 20 72 63 20 3d 20 6c 65 61 66 57   }.** rc = leafW
36f1b 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76 2c  riterFinalize(v,
36f1c 20 26 77 72 69 74 65 72 29 3b 0a 2a 2a 65 72 72   &writer);.**err
36f1d 3a 0a 2a 2a 20 6c 65 61 66 57 72 69 74 65 72 44  :.** leafWriterD
36f1e 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
36f1f 0a 2a 2a 20 72 65 74 75 72 6e 20 72 63 3b 0a 2a  .** return rc;.*
36f20 2a 0a 2a 2a 20 6c 65 61 66 57 72 69 74 65 72 53  *.** leafWriterS
36f21 74 65 70 28 29 20 6d 61 79 20 77 72 69 74 65 20  tep() may write 
36f22 61 20 63 6f 6c 6c 65 63 74 65 64 20 6c 65 61 66  a collected leaf
36f23 20 6f 75 74 20 74 6f 20 25 5f 73 65 67 6d 65 6e   out to %_segmen
36f24 74 73 2e 0a 2a 2a 20 6c 65 61 66 57 72 69 74 65  ts..** leafWrite
36f25 72 46 69 6e 61 6c 69 7a 65 28 29 20 66 69 6e 69  rFinalize() fini
36f26 73 68 65 73 20 77 72 69 74 69 6e 67 20 61 6e 79  shes writing any
36f27 20 62 75 66 66 65 72 65 64 20 64 61 74 61 20 61   buffered data a
36f28 6e 64 20 73 74 6f 72 65 73 0a 2a 2a 20 61 20 72  nd stores.** a r
36f29 6f 6f 74 20 6e 6f 64 65 20 69 6e 20 25 5f 73 65  oot node in %_se
36f2a 67 64 69 72 2e 20 20 6c 65 61 66 57 72 69 74 65  gdir.  leafWrite
36f2b 72 44 65 73 74 72 6f 79 28 29 20 66 72 65 65 73  rDestroy() frees
36f2c 20 61 6c 6c 20 62 75 66 66 65 72 73 20 61 6e 64   all buffers and
36f2d 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 57 72 69 74  .** InteriorWrit
36f2e 65 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 73  ers allocated as
36f2f 20 70 61 72 74 20 6f 66 20 77 72 69 74 69 6e 67   part of writing
36f30 20 74 68 69 73 20 73 65 67 6d 65 6e 74 2e 0a 2a   this segment..*
36f31 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  *.** TODO(shess)
36f32 20 44 6f 63 75 6d 65 6e 74 20 6c 65 61 66 57 72   Document leafWr
36f33 69 74 65 72 53 74 65 70 4d 65 72 67 65 28 29 2e  iterStepMerge().
36f34 0a 2a 2f 0a 0a 2f 2a 20 50 75 74 20 74 65 72 6d  .*/../* Put term
36f35 73 20 77 69 74 68 20 64 61 74 61 20 74 68 69 73  s with data this
36f36 20 62 69 67 20 69 6e 20 74 68 65 69 72 20 6f 77   big in their ow
36f37 6e 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 23 64 65 66  n block. */.#def
36f38 69 6e 65 20 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d  ine STANDALONE_M
36f39 49 4e 20 31 30 32 34 0a 0a 2f 2a 20 4b 65 65 70  IN 1024../* Keep
36f3a 20 6c 65 61 66 20 62 6c 6f 63 6b 73 20 62 65 6c   leaf blocks bel
36f3b 6f 77 20 74 68 69 73 20 73 69 7a 65 2e 20 2a 2f  ow this size. */
36f3c 0a 23 64 65 66 69 6e 65 20 4c 45 41 46 5f 4d 41  .#define LEAF_MA
36f3d 58 20 32 30 34 38 0a 0a 74 79 70 65 64 65 66 20  X 2048..typedef 
36f3e 73 74 72 75 63 74 20 4c 65 61 66 57 72 69 74 65  struct LeafWrite
36f3f 72 20 7b 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c  r {.  int iLevel
36f40 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 73  ;.  int idx;.  s
36f41 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61  qlite_int64 iSta
36f42 72 74 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 2f  rtBlockid;     /
36f43 2a 20 6e 65 65 64 65 64 20 74 6f 20 63 72 65 61  * needed to crea
36f44 74 65 20 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f  te the root info
36f45 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74   */.  sqlite_int
36f46 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 20  64 iEndBlockid; 
36f47 20 20 20 20 20 20 2f 2a 20 77 68 65 6e 20 77 65        /* when we
36f48 27 72 65 20 64 6f 6e 65 20 77 72 69 74 69 6e 67  're done writing
36f49 2e 20 2a 2f 0a 0a 20 20 44 61 74 61 42 75 66 66  . */..  DataBuff
36f4a 65 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20  er term;        
36f4b 20 20 20 20 20 20 20 20 2f 2a 20 70 72 65 76 69          /* previ
36f4c 6f 75 73 20 65 6e 63 6f 64 65 64 20 74 65 72 6d  ous encoded term
36f4d 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
36f4e 20 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20   data;          
36f4f 20 20 20 20 20 20 2f 2a 20 65 6e 63 6f 64 69 6e        /* encodin
36f50 67 20 62 75 66 66 65 72 20 2a 2f 0a 0a 20 20 2f  g buffer */..  /
36f51 2a 20 62 79 74 65 73 20 6f 66 20 66 69 72 73 74  * bytes of first
36f52 20 74 65 72 6d 20 69 6e 20 74 68 65 20 63 75 72   term in the cur
36f53 72 65 6e 74 20 6e 6f 64 65 20 77 68 69 63 68 20  rent node which 
36f54 64 69 73 74 69 6e 67 75 69 73 68 65 73 20 74 68  distinguishes th
36f55 61 74 0a 20 20 2a 2a 20 74 65 72 6d 20 66 72 6f  at.  ** term fro
36f56 6d 20 74 68 65 20 6c 61 73 74 20 74 65 72 6d 20  m the last term 
36f57 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  of the previous 
36f58 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74  node..  */.  int
36f59 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a   nTermDistinct;.
36f5a 0a 20 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65  .  InteriorWrite
36f5b 72 20 70 61 72 65 6e 74 57 72 69 74 65 72 3b 20  r parentWriter; 
36f5c 20 20 20 2f 2a 20 69 66 20 77 65 20 6f 76 65 72     /* if we over
36f5d 66 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 68 61  flow */.  int ha
36f5e 73 5f 70 61 72 65 6e 74 3b 0a 7d 20 4c 65 61 66  s_parent;.} Leaf
36f5f 57 72 69 74 65 72 3b 0a 0a 73 74 61 74 69 63 20  Writer;..static 
36f60 76 6f 69 64 20 6c 65 61 66 57 72 69 74 65 72 49  void leafWriterI
36f61 6e 69 74 28 69 6e 74 20 69 4c 65 76 65 6c 2c 20  nit(int iLevel, 
36f62 69 6e 74 20 69 64 78 2c 20 4c 65 61 66 57 72 69  int idx, LeafWri
36f63 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20  ter *pWriter){. 
36f64 20 43 4c 45 41 52 28 70 57 72 69 74 65 72 29 3b   CLEAR(pWriter);
36f65 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 4c 65 76  .  pWriter->iLev
36f66 65 6c 20 3d 20 69 4c 65 76 65 6c 3b 0a 20 20 70  el = iLevel;.  p
36f67 57 72 69 74 65 72 2d 3e 69 64 78 20 3d 20 69 64  Writer->idx = id
36f68 78 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72  x;..  dataBuffer
36f69 49 6e 69 74 28 26 70 57 72 69 74 65 72 2d 3e 74  Init(&pWriter->t
36f6a 65 72 6d 2c 20 33 32 29 3b 0a 0a 20 20 2f 2a 20  erm, 32);..  /* 
36f6b 53 74 61 72 74 20 6f 75 74 20 77 69 74 68 20 61  Start out with a
36f6c 20 72 65 61 73 6f 6e 61 62 6c 79 20 73 69 7a 65   reasonably size
36f6d 64 20 62 6c 6f 63 6b 2c 20 74 68 6f 75 67 68 20  d block, though 
36f6e 69 74 20 63 61 6e 20 67 72 6f 77 2e 20 2a 2f 0a  it can grow. */.
36f6f 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
36f70 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c  (&pWriter->data,
36f71 20 4c 45 41 46 5f 4d 41 58 29 3b 0a 7d 0a 0a 23   LEAF_MAX);.}..#
36f72 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
36f73 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65   Verify that the
36f74 20 64 61 74 61 20 69 73 20 72 65 61 64 61 62 6c   data is readabl
36f75 65 20 61 73 20 61 20 6c 65 61 66 20 6e 6f 64 65  e as a leaf node
36f76 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  . */.static void
36f77 20 6c 65 61 66 4e 6f 64 65 56 61 6c 69 64 61 74   leafNodeValidat
36f78 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  e(const char *pD
36f79 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b  ata, int nData){
36f7a 0a 20 20 69 6e 74 20 6e 2c 20 69 44 75 6d 6d 79  .  int n, iDummy
36f7b 3b 0a 0a 20 20 69 66 28 20 6e 44 61 74 61 3d 3d  ;..  if( nData==
36f7c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  0 ) return;.  as
36f7d 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29 3b  sert( nData>0 );
36f7e 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
36f7f 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
36f80 20 70 44 61 74 61 2b 6e 44 61 74 61 3e 70 44 61   pData+nData>pDa
36f81 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74  ta );..  /* Must
36f82 20 6c 65 61 64 20 77 69 74 68 20 61 20 76 61 72   lead with a var
36f83 69 6e 74 28 30 29 20 2a 2f 0a 20 20 6e 20 3d 20  int(0) */.  n = 
36f84 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
36f85 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b  pData, &iDummy);
36f86 0a 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d  .  assert( iDumm
36f87 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  y==0 );.  assert
36f88 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ( n>0 );.  asser
36f89 74 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20  t( n<nData );.  
36f8a 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 6e 44  pData += n;.  nD
36f8b 61 74 61 20 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a 20  ata -= n;..  /* 
36f8c 4c 65 61 64 69 6e 67 20 74 65 72 6d 20 6c 65 6e  Leading term len
36f8d 67 74 68 20 61 6e 64 20 64 61 74 61 20 6d 75 73  gth and data mus
36f8e 74 20 66 69 74 20 69 6e 20 62 75 66 66 65 72 2e  t fit in buffer.
36f8f 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65   */.  n = fts3Ge
36f90 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c  tVarint32(pData,
36f91 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73   &iDummy);.  ass
36f92 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73  ert( n>0 );.  as
36f93 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29  sert( iDummy>0 )
36f94 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44  ;.  assert( n+iD
36f95 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73 65  ummy>0 );.  asse
36f96 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 6e 44 61  rt( n+iDummy<nDa
36f97 74 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d  ta );.  pData +=
36f98 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 6e 44 61   n+iDummy;.  nDa
36f99 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a  ta -= n+iDummy;.
36f9a 0a 20 20 2f 2a 20 4c 65 61 64 69 6e 67 20 74 65  .  /* Leading te
36f9b 72 6d 27 73 20 64 6f 63 6c 69 73 74 20 6c 65 6e  rm's doclist len
36f9c 67 74 68 20 61 6e 64 20 64 61 74 61 20 6d 75 73  gth and data mus
36f9d 74 20 66 69 74 2e 20 2a 2f 0a 20 20 6e 20 3d 20  t fit. */.  n = 
36f9e 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
36f9f 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b  pData, &iDummy);
36fa0 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29  .  assert( n>0 )
36fa1 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 75 6d  ;.  assert( iDum
36fa2 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  my>0 );.  assert
36fa3 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  ( n+iDummy>0 );.
36fa4 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
36fa5 6d 79 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 41  my<=nData );.  A
36fa6 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c  SSERT_VALID_DOCL
36fa7 49 53 54 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20  IST(DL_DEFAULT, 
36fa8 70 44 61 74 61 2b 6e 2c 20 69 44 75 6d 6d 79 2c  pData+n, iDummy,
36fa9 20 4e 55 4c 4c 29 3b 0a 20 20 70 44 61 74 61 20   NULL);.  pData 
36faa 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 6e  += n+iDummy;.  n
36fab 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79  Data -= n+iDummy
36fac 3b 0a 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74  ;..  /* Verify t
36fad 68 61 74 20 74 72 61 69 6c 69 6e 67 20 74 65 72  hat trailing ter
36fae 6d 73 20 61 6e 64 20 64 6f 63 6c 69 73 74 73 20  ms and doclists 
36faf 61 6c 73 6f 20 61 72 65 20 72 65 61 64 61 62 6c  also are readabl
36fb0 65 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e  e. */.  while( n
36fb1 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 6e  Data!=0 ){.    n
36fb2 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
36fb3 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d  32(pData, &iDumm
36fb4 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  y);.    assert( 
36fb5 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  n>0 );.    asser
36fb6 74 28 20 69 44 75 6d 6d 79 3e 3d 30 20 29 3b 0a  t( iDummy>=0 );.
36fb7 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 6e 44      assert( n<nD
36fb8 61 74 61 20 29 3b 0a 20 20 20 20 70 44 61 74 61  ata );.    pData
36fb9 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 44 61 74 61   += n;.    nData
36fba 20 2d 3d 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 66   -= n;.    n = f
36fbb 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
36fbc 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
36fbd 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20      assert( n>0 
36fbe 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
36fbf 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61  Dummy>0 );.    a
36fc0 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e  ssert( n+iDummy>
36fc1 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
36fc2 20 6e 2b 69 44 75 6d 6d 79 3c 6e 44 61 74 61 20   n+iDummy<nData 
36fc3 29 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20  );.    pData += 
36fc4 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 6e 44  n+iDummy;.    nD
36fc5 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata -= n+iDummy;
36fc6 0a 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65  ..    n = fts3Ge
36fc7 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c  tVarint32(pData,
36fc8 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61   &iDummy);.    a
36fc9 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
36fca 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
36fcb 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
36fcc 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  ( n+iDummy>0 );.
36fcd 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44      assert( n+iD
36fce 75 6d 6d 79 3c 3d 6e 44 61 74 61 20 29 3b 0a 20  ummy<=nData );. 
36fcf 20 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f     ASSERT_VALID_
36fd0 44 4f 43 4c 49 53 54 28 44 4c 5f 44 45 46 41 55  DOCLIST(DL_DEFAU
36fd1 4c 54 2c 20 70 44 61 74 61 2b 6e 2c 20 69 44 75  LT, pData+n, iDu
36fd2 6d 6d 79 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20  mmy, NULL);.    
36fd3 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d  pData += n+iDumm
36fd4 79 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20  y;.    nData -= 
36fd5 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 7d 0a 7d 0a  n+iDummy;.  }.}.
36fd6 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f 56  #define ASSERT_V
36fd7 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70  ALID_LEAF_NODE(p
36fd8 2c 20 6e 29 20 6c 65 61 66 4e 6f 64 65 56 61 6c  , n) leafNodeVal
36fd9 69 64 61 74 65 28 70 2c 20 6e 29 0a 23 65 6c 73  idate(p, n).#els
36fda 65 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54  e.#define ASSERT
36fdb 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f 44 45  _VALID_LEAF_NODE
36fdc 28 70 2c 20 6e 29 20 61 73 73 65 72 74 28 20 31  (p, n) assert( 1
36fdd 20 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6c   ).#endif../* Fl
36fde 75 73 68 20 74 68 65 20 63 75 72 72 65 6e 74 20  ush the current 
36fdf 6c 65 61 66 20 6e 6f 64 65 20 74 6f 20 25 5f 73  leaf node to %_s
36fe0 65 67 6d 65 6e 74 73 2c 20 61 6e 64 20 61 64 64  egments, and add
36fe1 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 69 6e  ing the resultin
36fe2 67 0a 2a 2a 20 62 6c 6f 63 6b 69 64 20 61 6e 64  g.** blockid and
36fe3 20 74 68 65 20 73 74 61 72 74 69 6e 67 20 74 65   the starting te
36fe4 72 6d 20 74 6f 20 74 68 65 20 69 6e 74 65 72 69  rm to the interi
36fe5 6f 72 20 6e 6f 64 65 20 77 68 69 63 68 20 77 69  or node which wi
36fe6 6c 6c 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 69 74  ll.** contain it
36fe7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36fe8 6c 65 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e  leafWriterIntern
36fe9 61 6c 46 6c 75 73 68 28 66 75 6c 6c 74 65 78 74  alFlush(fulltext
36fea 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72  _vtab *v, LeafWr
36feb 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20  iter *pWriter,. 
36fec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36fed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36fee 20 20 69 6e 74 20 69 44 61 74 61 2c 20 69 6e 74    int iData, int
36fef 20 6e 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74   nData){.  sqlit
36ff0 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64  e_int64 iBlockid
36ff1 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68   = 0;.  const ch
36ff2 61 72 20 2a 70 53 74 61 72 74 69 6e 67 54 65 72  ar *pStartingTer
36ff3 6d 3b 0a 20 20 69 6e 74 20 6e 53 74 61 72 74 69  m;.  int nStarti
36ff4 6e 67 54 65 72 6d 2c 20 72 63 2c 20 6e 3b 0a 0a  ngTerm, rc, n;..
36ff5 20 20 2f 2a 20 4d 75 73 74 20 68 61 76 65 20 74    /* Must have t
36ff6 68 65 20 6c 65 61 64 69 6e 67 20 76 61 72 69 6e  he leading varin
36ff7 74 28 30 29 20 66 6c 61 67 2c 20 70 6c 75 73 20  t(0) flag, plus 
36ff8 61 74 20 6c 65 61 73 74 20 73 6f 6d 65 0a 20 20  at least some.  
36ff9 2a 2a 20 76 61 6c 69 64 2d 6c 6f 6f 6b 69 6e 67  ** valid-looking
36ffa 20 64 61 74 61 2e 0a 20 20 2a 2f 0a 20 20 61 73   data..  */.  as
36ffb 73 65 72 74 28 20 6e 44 61 74 61 3e 32 20 29 3b  sert( nData>2 );
36ffc 0a 20 20 61 73 73 65 72 74 28 20 69 44 61 74 61  .  assert( iData
36ffd 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  >=0 );.  assert(
36ffe 20 69 44 61 74 61 2b 6e 44 61 74 61 3c 3d 70 57   iData+nData<=pW
36fff 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
37000 61 20 29 3b 0a 20 20 41 53 53 45 52 54 5f 56 41  a );.  ASSERT_VA
37001 4c 49 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 57  LID_LEAF_NODE(pW
37002 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
37003 61 2b 69 44 61 74 61 2c 20 6e 44 61 74 61 29 3b  a+iData, nData);
37004 0a 0a 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69  ..  rc = block_i
37005 6e 73 65 72 74 28 76 2c 20 70 57 72 69 74 65 72  nsert(v, pWriter
37006 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61  ->data.pData+iDa
37007 74 61 2c 20 6e 44 61 74 61 2c 20 26 69 42 6c 6f  ta, nData, &iBlo
37008 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ckid);.  if( rc!
37009 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3700a 75 72 6e 20 72 63 3b 0a 20 20 61 73 73 65 72 74  urn rc;.  assert
3700b 28 20 69 42 6c 6f 63 6b 69 64 21 3d 30 20 29 3b  ( iBlockid!=0 );
3700c 0a 0a 20 20 2f 2a 20 52 65 63 6f 6e 73 74 72 75  ..  /* Reconstru
3700d 63 74 20 74 68 65 20 66 69 72 73 74 20 74 65 72  ct the first ter
3700e 6d 20 69 6e 20 74 68 65 20 6c 65 61 66 20 66 6f  m in the leaf fo
3700f 72 20 70 75 72 70 6f 73 65 73 20 6f 66 20 62 75  r purposes of bu
37010 69 6c 64 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  ilding.  ** the 
37011 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 0a 20  interior node.. 
37012 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65   */.  n = fts3Ge
37013 74 56 61 72 69 6e 74 33 32 28 70 57 72 69 74 65  tVarint32(pWrite
37014 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44  r->data.pData+iD
37015 61 74 61 2b 31 2c 20 26 6e 53 74 61 72 74 69 6e  ata+1, &nStartin
37016 67 54 65 72 6d 29 3b 0a 20 20 70 53 74 61 72 74  gTerm);.  pStart
37017 69 6e 67 54 65 72 6d 20 3d 20 70 57 72 69 74 65  ingTerm = pWrite
37018 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44  r->data.pData+iD
37019 61 74 61 2b 31 2b 6e 3b 0a 20 20 61 73 73 65 72  ata+1+n;.  asser
3701a 74 28 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  t( pWriter->data
3701b 2e 6e 44 61 74 61 3e 69 44 61 74 61 2b 31 2b 6e  .nData>iData+1+n
3701c 2b 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 20 29  +nStartingTerm )
3701d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69  ;.  assert( pWri
3701e 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e  ter->nTermDistin
3701f 63 74 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ct>0 );.  assert
37020 28 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d  ( pWriter->nTerm
37021 44 69 73 74 69 6e 63 74 3c 3d 6e 53 74 61 72 74  Distinct<=nStart
37022 69 6e 67 54 65 72 6d 20 29 3b 0a 20 20 6e 53 74  ingTerm );.  nSt
37023 61 72 74 69 6e 67 54 65 72 6d 20 3d 20 70 57 72  artingTerm = pWr
37024 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69  iter->nTermDisti
37025 6e 63 74 3b 0a 0a 20 20 69 66 28 20 70 57 72 69  nct;..  if( pWri
37026 74 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20  ter->has_parent 
37027 29 7b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72 57  ){.    interiorW
37028 72 69 74 65 72 41 70 70 65 6e 64 28 26 70 57 72  riterAppend(&pWr
37029 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74  iter->parentWrit
3702a 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
3702b 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 74               pSt
3702c 61 72 74 69 6e 67 54 65 72 6d 2c 20 6e 53 74 61  artingTerm, nSta
3702d 72 74 69 6e 67 54 65 72 6d 2c 20 69 42 6c 6f 63  rtingTerm, iBloc
3702e 6b 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  kid);.  }else{. 
3702f 20 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65     interiorWrite
37030 72 49 6e 69 74 28 31 2c 20 70 53 74 61 72 74 69  rInit(1, pStarti
37031 6e 67 54 65 72 6d 2c 20 6e 53 74 61 72 74 69 6e  ngTerm, nStartin
37032 67 54 65 72 6d 2c 20 69 42 6c 6f 63 6b 69 64 2c  gTerm, iBlockid,
37033 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37034 20 20 20 20 20 20 20 20 26 70 57 72 69 74 65 72          &pWriter
37035 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29 3b  ->parentWriter);
37036 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 68 61  .    pWriter->ha
37037 73 5f 70 61 72 65 6e 74 20 3d 20 31 3b 0a 20 20  s_parent = 1;.  
37038 7d 0a 0a 20 20 2f 2a 20 54 72 61 63 6b 20 74 68  }..  /* Track th
37039 65 20 73 70 61 6e 20 6f 66 20 74 68 69 73 20 73  e span of this s
3703a 65 67 6d 65 6e 74 27 73 20 6c 65 61 66 20 6e 6f  egment's leaf no
3703b 64 65 73 2e 20 2a 2f 0a 20 20 69 66 28 20 70 57  des. */.  if( pW
3703c 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b  riter->iEndBlock
3703d 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 70 57 72  id==0 ){.    pWr
3703e 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69  iter->iEndBlocki
3703f 64 20 3d 20 70 57 72 69 74 65 72 2d 3e 69 53 74  d = pWriter->iSt
37040 61 72 74 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c  artBlockid = iBl
37041 6f 63 6b 69 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ockid;.  }else{.
37042 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 45 6e      pWriter->iEn
37043 64 42 6c 6f 63 6b 69 64 2b 2b 3b 0a 20 20 20 20  dBlockid++;.    
37044 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 69 64  assert( iBlockid
37045 3d 3d 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42  ==pWriter->iEndB
37046 6c 6f 63 6b 69 64 20 29 3b 0a 20 20 7d 0a 0a 20  lockid );.  }.. 
37047 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
37048 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  K;.}.static int 
37049 6c 65 61 66 57 72 69 74 65 72 46 6c 75 73 68 28  leafWriterFlush(
3704a 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
3704b 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57  , LeafWriter *pW
3704c 72 69 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  riter){.  int rc
3704d 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74   = leafWriterInt
3704e 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57  ernalFlush(v, pW
3704f 72 69 74 65 72 2c 20 30 2c 20 70 57 72 69 74 65  riter, 0, pWrite
37050 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 29 3b 0a  r->data.nData);.
37051 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
37052 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
37053 0a 0a 20 20 2f 2a 20 52 65 2d 69 6e 69 74 69 61  ..  /* Re-initia
37054 6c 69 7a 65 20 74 68 65 20 6f 75 74 70 75 74 20  lize the output 
37055 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 64 61 74  buffer. */.  dat
37056 61 42 75 66 66 65 72 52 65 73 65 74 28 26 70 57  aBufferReset(&pW
37057 72 69 74 65 72 2d 3e 64 61 74 61 29 3b 0a 0a 20  riter->data);.. 
37058 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
37059 4b 3b 0a 7d 0a 0a 2f 2a 20 46 65 74 63 68 20 74  K;.}../* Fetch t
3705a 68 65 20 72 6f 6f 74 20 69 6e 66 6f 20 66 6f 72  he root info for
3705b 20 74 68 65 20 73 65 67 6d 65 6e 74 2e 20 20 49   the segment.  I
3705c 66 20 74 68 65 20 65 6e 74 69 72 65 20 6c 65 61  f the entire lea
3705d 66 20 66 69 74 73 0a 2a 2a 20 77 69 74 68 69 6e  f fits.** within
3705e 20 52 4f 4f 54 5f 4d 41 58 2c 20 74 68 65 6e 20   ROOT_MAX, then 
3705f 69 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  it will be retur
37060 6e 65 64 20 64 69 72 65 63 74 6c 79 2c 20 6f 74  ned directly, ot
37061 68 65 72 77 69 73 65 20 69 74 0a 2a 2a 20 77 69  herwise it.** wi
37062 6c 6c 20 62 65 20 66 6c 75 73 68 65 64 20 61 6e  ll be flushed an
37063 64 20 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f 20  d the root info 
37064 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64  will be returned
37065 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 69 6e 74   from the.** int
37066 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 2a 70 69  erior node.  *pi
37067 45 6e 64 42 6c 6f 63 6b 69 64 20 69 73 20 73 65  EndBlockid is se
37068 74 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 69 64  t to the blockid
37069 20 6f 66 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20   of the last.** 
3706a 69 6e 74 65 72 69 6f 72 20 6f 72 20 6c 65 61 66  interior or leaf
3706b 20 6e 6f 64 65 20 77 72 69 74 74 65 6e 20 74 6f   node written to
3706c 20 64 69 73 6b 20 28 30 20 69 66 20 6e 6f 6e 65   disk (0 if none
3706d 20 61 72 65 20 77 72 69 74 74 65 6e 20 61 74 0a   are written at.
3706e 2a 2a 20 61 6c 6c 29 2e 0a 2a 2f 0a 73 74 61 74  ** all)..*/.stat
3706f 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65  ic int leafWrite
37070 72 52 6f 6f 74 49 6e 66 6f 28 66 75 6c 6c 74 65  rRootInfo(fullte
37071 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 66  xt_vtab *v, Leaf
37072 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
37073 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37074 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
37075 68 61 72 20 2a 2a 70 70 52 6f 6f 74 49 6e 66 6f  har **ppRootInfo
37076 2c 20 69 6e 74 20 2a 70 6e 52 6f 6f 74 49 6e 66  , int *pnRootInf
37077 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  o,.             
37078 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37079 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
3707a 69 45 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20  iEndBlockid){.  
3707b 2f 2a 20 77 65 20 63 61 6e 20 66 69 74 20 74 68  /* we can fit th
3707c 65 20 73 65 67 6d 65 6e 74 20 65 6e 74 69 72 65  e segment entire
3707d 6c 79 20 69 6e 6c 69 6e 65 20 2a 2f 0a 20 20 69  ly inline */.  i
3707e 66 28 20 21 70 57 72 69 74 65 72 2d 3e 68 61 73  f( !pWriter->has
3707f 5f 70 61 72 65 6e 74 20 26 26 20 70 57 72 69 74  _parent && pWrit
37080 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3c 52  er->data.nData<R
37081 4f 4f 54 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2a  OOT_MAX ){.    *
37082 70 70 52 6f 6f 74 49 6e 66 6f 20 3d 20 70 57 72  ppRootInfo = pWr
37083 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
37084 3b 0a 20 20 20 20 2a 70 6e 52 6f 6f 74 49 6e 66  ;.    *pnRootInf
37085 6f 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74  o = pWriter->dat
37086 61 2e 6e 44 61 74 61 3b 0a 20 20 20 20 2a 70 69  a.nData;.    *pi
37087 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 30 3b 0a  EndBlockid = 0;.
37088 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
37089 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
3708a 46 6c 75 73 68 20 72 65 6d 61 69 6e 69 6e 67 20  Flush remaining 
3708b 6c 65 61 66 20 64 61 74 61 2e 20 2a 2f 0a 20 20  leaf data. */.  
3708c 69 66 28 20 70 57 72 69 74 65 72 2d 3e 64 61 74  if( pWriter->dat
3708d 61 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20  a.nData>0 ){.   
3708e 20 69 6e 74 20 72 63 20 3d 20 6c 65 61 66 57 72   int rc = leafWr
3708f 69 74 65 72 46 6c 75 73 68 28 76 2c 20 70 57 72  iterFlush(v, pWr
37090 69 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72  iter);.    if( r
37091 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
37092 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
37093 20 2f 2a 20 57 65 20 6d 75 73 74 20 68 61 76 65   /* We must have
37094 20 66 6c 75 73 68 65 64 20 61 20 6c 65 61 66 20   flushed a leaf 
37095 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74 2e 20 2a  at some point. *
37096 2f 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69  /.  assert( pWri
37097 74 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20  ter->has_parent 
37098 29 3b 0a 0a 20 20 2f 2a 20 54 65 6e 61 74 69 76  );..  /* Tenativ
37099 65 6c 79 20 73 65 74 20 74 68 65 20 65 6e 64 20  ely set the end 
3709a 6c 65 61 66 20 62 6c 6f 63 6b 69 64 20 61 73 20  leaf blockid as 
3709b 74 68 65 20 65 6e 64 20 62 6c 6f 63 6b 69 64 2e  the end blockid.
3709c 20 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 69 6e    If the.  ** in
3709d 74 65 72 69 6f 72 20 6e 6f 64 65 20 63 61 6e 20  terior node can 
3709e 62 65 20 72 65 74 75 72 6e 65 64 20 69 6e 6c 69  be returned inli
3709f 6e 65 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65  ne, this will be
370a0 20 74 68 65 20 66 69 6e 61 6c 0a 20 20 2a 2a 20   the final.  ** 
370a1 62 6c 6f 63 6b 69 64 2c 20 6f 74 68 65 72 77 69  blockid, otherwi
370a2 73 65 20 69 74 20 77 69 6c 6c 20 62 65 20 6f 76  se it will be ov
370a3 65 72 77 72 69 74 74 65 6e 20 62 79 0a 20 20 2a  erwritten by.  *
370a4 2a 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  * interiorWriter
370a5 52 6f 6f 74 49 6e 66 6f 28 29 2e 0a 20 20 2a 2f  RootInfo()..  */
370a6 0a 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  .  *piEndBlockid
370a7 20 3d 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64   = pWriter->iEnd
370a8 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20 72 65 74 75  Blockid;..  retu
370a9 72 6e 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65  rn interiorWrite
370aa 72 52 6f 6f 74 49 6e 66 6f 28 76 2c 20 26 70 57  rRootInfo(v, &pW
370ab 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69  riter->parentWri
370ac 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ter,.           
370ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370ae 20 20 20 20 20 70 70 52 6f 6f 74 49 6e 66 6f 2c       ppRootInfo,
370af 20 70 6e 52 6f 6f 74 49 6e 66 6f 2c 20 70 69 45   pnRootInfo, piE
370b0 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 7d 0a 0a 2f  ndBlockid);.}../
370b1 2a 20 43 6f 6c 6c 65 63 74 20 74 68 65 20 72 6f  * Collect the ro
370b2 6f 74 49 6e 66 6f 20 64 61 74 61 20 61 6e 64 20  otInfo data and 
370b3 73 74 6f 72 65 20 69 74 20 69 6e 74 6f 20 74 68  store it into th
370b4 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74  e segment direct
370b5 6f 72 79 2e 0a 2a 2a 20 54 68 69 73 20 68 61 73  ory..** This has
370b6 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 66   the effect of f
370b7 6c 75 73 68 69 6e 67 20 74 68 65 20 73 65 67 6d  lushing the segm
370b8 65 6e 74 27 73 20 6c 65 61 66 20 64 61 74 61 20  ent's leaf data 
370b9 74 6f 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e 74 73  to.** %_segments
370ba 2c 20 61 6e 64 20 61 6c 73 6f 20 66 6c 75 73 68  , and also flush
370bb 69 6e 67 20 61 6e 79 20 69 6e 74 65 72 69 6f 72  ing any interior
370bc 20 6e 6f 64 65 73 20 74 6f 20 25 5f 73 65 67 6d   nodes to %_segm
370bd 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ents..*/.static 
370be 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 46 69  int leafWriterFi
370bf 6e 61 6c 69 7a 65 28 66 75 6c 6c 74 65 78 74 5f  nalize(fulltext_
370c0 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69  vtab *v, LeafWri
370c1 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20  ter *pWriter){. 
370c2 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45   sqlite_int64 iE
370c3 6e 64 42 6c 6f 63 6b 69 64 3b 0a 20 20 63 68 61  ndBlockid;.  cha
370c4 72 20 2a 70 52 6f 6f 74 49 6e 66 6f 3b 0a 20 20  r *pRootInfo;.  
370c5 69 6e 74 20 72 63 2c 20 6e 52 6f 6f 74 49 6e 66  int rc, nRootInf
370c6 6f 3b 0a 0a 20 20 72 63 20 3d 20 6c 65 61 66 57  o;..  rc = leafW
370c7 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 76 2c  riterRootInfo(v,
370c8 20 70 57 72 69 74 65 72 2c 20 26 70 52 6f 6f 74   pWriter, &pRoot
370c9 49 6e 66 6f 2c 20 26 6e 52 6f 6f 74 49 6e 66 6f  Info, &nRootInfo
370ca 2c 20 26 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b  , &iEndBlockid);
370cb 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
370cc 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
370cd 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74 20 62 6f  ;..  /* Don't bo
370ce 74 68 65 72 20 73 74 6f 72 69 6e 67 20 61 6e 20  ther storing an 
370cf 65 6e 74 69 72 65 6c 79 20 65 6d 70 74 79 20 73  entirely empty s
370d0 65 67 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 66 28  egment. */.  if(
370d1 20 69 45 6e 64 42 6c 6f 63 6b 69 64 3d 3d 30 20   iEndBlockid==0 
370d2 26 26 20 6e 52 6f 6f 74 49 6e 66 6f 3d 3d 30 20  && nRootInfo==0 
370d3 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
370d4 4f 4b 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 65  OK;..  return se
370d5 67 64 69 72 5f 73 65 74 28 76 2c 20 70 57 72 69  gdir_set(v, pWri
370d6 74 65 72 2d 3e 69 4c 65 76 65 6c 2c 20 70 57 72  ter->iLevel, pWr
370d7 69 74 65 72 2d 3e 69 64 78 2c 0a 20 20 20 20 20  iter->idx,.     
370d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
370d9 57 72 69 74 65 72 2d 3e 69 53 74 61 72 74 42 6c  Writer->iStartBl
370da 6f 63 6b 69 64 2c 20 70 57 72 69 74 65 72 2d 3e  ockid, pWriter->
370db 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20  iEndBlockid,.   
370dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370dd 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20 70 52   iEndBlockid, pR
370de 6f 6f 74 49 6e 66 6f 2c 20 6e 52 6f 6f 74 49 6e  ootInfo, nRootIn
370df 66 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  fo);.}..static v
370e0 6f 69 64 20 6c 65 61 66 57 72 69 74 65 72 44 65  oid leafWriterDe
370e1 73 74 72 6f 79 28 4c 65 61 66 57 72 69 74 65 72  stroy(LeafWriter
370e2 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 69 66   *pWriter){.  if
370e3 28 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70  ( pWriter->has_p
370e4 61 72 65 6e 74 20 29 20 69 6e 74 65 72 69 6f 72  arent ) interior
370e5 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 26 70  WriterDestroy(&p
370e6 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72  Writer->parentWr
370e7 69 74 65 72 29 3b 0a 20 20 64 61 74 61 42 75 66  iter);.  dataBuf
370e8 66 65 72 44 65 73 74 72 6f 79 28 26 70 57 72 69  ferDestroy(&pWri
370e9 74 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 64 61  ter->term);.  da
370ea 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
370eb 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 29 3b  &pWriter->data);
370ec 0a 7d 0a 0a 2f 2a 20 45 6e 63 6f 64 65 20 61 20  .}../* Encode a 
370ed 74 65 72 6d 20 69 6e 74 6f 20 74 68 65 20 6c 65  term into the le
370ee 61 66 57 72 69 74 65 72 2c 20 64 65 6c 74 61 2d  afWriter, delta-
370ef 65 6e 63 6f 64 69 6e 67 20 61 73 20 61 70 70 72  encoding as appr
370f0 6f 70 72 69 61 74 65 2e 0a 2a 2a 20 52 65 74 75  opriate..** Retu
370f1 72 6e 73 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  rns the length o
370f2 66 20 74 68 65 20 6e 65 77 20 74 65 72 6d 20 77  f the new term w
370f3 68 69 63 68 20 64 69 73 74 69 6e 67 75 69 73 68  hich distinguish
370f4 65 73 20 69 74 20 66 72 6f 6d 20 74 68 65 0a 2a  es it from the.*
370f5 2a 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d 2c  * previous term,
370f6 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73   which can be us
370f7 65 64 20 74 6f 20 73 65 74 20 6e 54 65 72 6d 44  ed to set nTermD
370f8 69 73 74 69 6e 63 74 20 77 68 65 6e 20 61 20 6e  istinct when a n
370f9 6f 64 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 20  ode.** boundary 
370fa 69 73 20 63 72 6f 73 73 65 64 2e 0a 2a 2f 0a 73  is crossed..*/.s
370fb 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72  tatic int leafWr
370fc 69 74 65 72 45 6e 63 6f 64 65 54 65 72 6d 28 4c  iterEncodeTerm(L
370fd 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74  eafWriter *pWrit
370fe 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
370ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37100 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
37101 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
37102 29 7b 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49  ){.  char c[VARI
37103 4e 54 5f 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41  NT_MAX+VARINT_MA
37104 58 5d 3b 0a 20 20 69 6e 74 20 6e 2c 20 6e 50 72  X];.  int n, nPr
37105 65 66 69 78 20 3d 20 30 3b 0a 0a 20 20 61 73 73  efix = 0;..  ass
37106 65 72 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b 0a  ert( nTerm>0 );.
37107 20 20 77 68 69 6c 65 28 20 6e 50 72 65 66 69 78    while( nPrefix
37108 3c 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 6e  <pWriter->term.n
37109 44 61 74 61 20 26 26 0a 20 20 20 20 20 20 20 20  Data &&.        
3710a 20 70 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 3d   pTerm[nPrefix]=
3710b 3d 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 70  =pWriter->term.p
3710c 44 61 74 61 5b 6e 50 72 65 66 69 78 5d 20 29 7b  Data[nPrefix] ){
3710d 0a 20 20 20 20 6e 50 72 65 66 69 78 2b 2b 3b 0a  .    nPrefix++;.
3710e 20 20 20 20 2f 2a 20 46 61 69 6c 69 6e 67 20 74      /* Failing t
3710f 68 69 73 20 69 6d 70 6c 69 65 73 20 74 68 61 74  his implies that
37110 20 74 68 65 20 74 65 72 6d 73 20 77 65 72 65 6e   the terms weren
37111 27 74 20 69 6e 20 6f 72 64 65 72 2e 20 2a 2f 0a  't in order. */.
37112 20 20 20 20 61 73 73 65 72 74 28 20 6e 50 72 65      assert( nPre
37113 66 69 78 3c 6e 54 65 72 6d 20 29 3b 0a 20 20 7d  fix<nTerm );.  }
37114 0a 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  ..  if( pWriter-
37115 3e 64 61 74 61 2e 6e 44 61 74 61 3d 3d 30 20 29  >data.nData==0 )
37116 7b 0a 20 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20  {.    /* Encode 
37117 74 68 65 20 6e 6f 64 65 20 68 65 61 64 65 72 20  the node header 
37118 61 6e 64 20 6c 65 61 64 69 6e 67 20 74 65 72 6d  and leading term
37119 20 61 73 3a 0a 20 20 20 20 2a 2a 20 20 76 61 72   as:.    **  var
3711a 69 6e 74 28 30 29 0a 20 20 20 20 2a 2a 20 20 76  int(0).    **  v
3711b 61 72 69 6e 74 28 6e 54 65 72 6d 29 0a 20 20 20  arint(nTerm).   
3711c 20 2a 2a 20 20 63 68 61 72 20 70 54 65 72 6d 5b   **  char pTerm[
3711d 6e 54 65 72 6d 5d 0a 20 20 20 20 2a 2f 0a 20 20  nTerm].    */.  
3711e 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72    n = fts3PutVar
3711f 69 6e 74 28 63 2c 20 27 5c 30 27 29 3b 0a 20 20  int(c, '\0');.  
37120 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61    n += fts3PutVa
37121 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 29  rint(c+n, nTerm)
37122 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
37123 41 70 70 65 6e 64 32 28 26 70 57 72 69 74 65 72  Append2(&pWriter
37124 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 2c 20 70 54  ->data, c, n, pT
37125 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 7d  erm, nTerm);.  }
37126 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 44 65 6c  else{.    /* Del
37127 74 61 2d 65 6e 63 6f 64 65 20 74 68 65 20 74 65  ta-encode the te
37128 72 6d 20 61 73 3a 0a 20 20 20 20 2a 2a 20 20 76  rm as:.    **  v
37129 61 72 69 6e 74 28 6e 50 72 65 66 69 78 29 0a 20  arint(nPrefix). 
3712a 20 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 6e 53     **  varint(nS
3712b 75 66 66 69 78 29 0a 20 20 20 20 2a 2a 20 20 63  uffix).    **  c
3712c 68 61 72 20 70 54 65 72 6d 53 75 66 66 69 78 5b  har pTermSuffix[
3712d 6e 53 75 66 66 69 78 5d 0a 20 20 20 20 2a 2f 0a  nSuffix].    */.
3712e 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56      n = fts3PutV
3712f 61 72 69 6e 74 28 63 2c 20 6e 50 72 65 66 69 78  arint(c, nPrefix
37130 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  );.    n += fts3
37131 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e  PutVarint(c+n, n
37132 54 65 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a 20  Term-nPrefix);. 
37133 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
37134 65 6e 64 32 28 26 70 57 72 69 74 65 72 2d 3e 64  end2(&pWriter->d
37135 61 74 61 2c 20 63 2c 20 6e 2c 20 70 54 65 72 6d  ata, c, n, pTerm
37136 2b 6e 50 72 65 66 69 78 2c 20 6e 54 65 72 6d 2d  +nPrefix, nTerm-
37137 6e 50 72 65 66 69 78 29 3b 0a 20 20 7d 0a 20 20  nPrefix);.  }.  
37138 64 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63  dataBufferReplac
37139 65 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d  e(&pWriter->term
3713a 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , pTerm, nTerm);
3713b 0a 0a 20 20 72 65 74 75 72 6e 20 6e 50 72 65 66  ..  return nPref
3713c 69 78 2b 31 3b 0a 7d 0a 0a 2f 2a 20 55 73 65 64  ix+1;.}../* Used
3713d 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6d   to avoid a memm
3713e 6f 76 65 20 77 68 65 6e 20 61 20 6c 61 72 67 65  ove when a large
3713f 20 61 6d 6f 75 6e 74 20 6f 66 20 64 6f 63 6c 69   amount of docli
37140 73 74 20 64 61 74 61 20 69 73 20 69 6e 0a 2a 2a  st data is in.**
37141 20 74 68 65 20 62 75 66 66 65 72 2e 20 20 54 68   the buffer.  Th
37142 69 73 20 63 6f 6e 73 74 72 75 63 74 73 20 61 20  is constructs a 
37143 6e 6f 64 65 20 61 6e 64 20 74 65 72 6d 20 68 65  node and term he
37144 61 64 65 72 20 62 65 66 6f 72 65 0a 2a 2a 20 69  ader before.** i
37145 44 6f 63 6c 69 73 74 44 61 74 61 20 61 6e 64 20  DoclistData and 
37146 66 6c 75 73 68 65 73 20 74 68 65 20 72 65 73 75  flushes the resu
37147 6c 74 69 6e 67 20 63 6f 6d 70 6c 65 74 65 20 6e  lting complete n
37148 6f 64 65 20 75 73 69 6e 67 0a 2a 2a 20 6c 65 61  ode using.** lea
37149 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46  fWriterInternalF
3714a 6c 75 73 68 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  lush()..*/.stati
3714b 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72  c int leafWriter
3714c 49 6e 6c 69 6e 65 46 6c 75 73 68 28 66 75 6c 6c  InlineFlush(full
3714d 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65  text_vtab *v, Le
3714e 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  afWriter *pWrite
3714f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
37150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37151 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
37152 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
37153 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
37154 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37155 20 20 20 69 6e 74 20 69 44 6f 63 6c 69 73 74 44     int iDoclistD
37156 61 74 61 29 7b 0a 20 20 63 68 61 72 20 63 5b 56  ata){.  char c[V
37157 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49 4e 54  ARINT_MAX+VARINT
37158 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 69 44 61  _MAX];.  int iDa
37159 74 61 2c 20 6e 20 3d 20 66 74 73 33 50 75 74 56  ta, n = fts3PutV
3715a 61 72 69 6e 74 28 63 2c 20 30 29 3b 0a 20 20 6e  arint(c, 0);.  n
3715b 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e   += fts3PutVarin
3715c 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 0a  t(c+n, nTerm);..
3715d 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c    /* There shoul
3715e 64 20 61 6c 77 61 79 73 20 62 65 20 72 6f 6f 6d  d always be room
3715f 20 66 6f 72 20 74 68 65 20 68 65 61 64 65 72 2e   for the header.
37160 20 20 45 76 65 6e 20 69 66 20 70 54 65 72 6d 20    Even if pTerm 
37161 73 68 61 72 65 64 0a 20 20 2a 2a 20 61 20 73 75  shared.  ** a su
37162 62 73 74 61 6e 74 69 61 6c 20 70 72 65 66 69 78  bstantial prefix
37163 20 77 69 74 68 20 74 68 65 20 70 72 65 76 69 6f   with the previo
37164 75 73 20 74 65 72 6d 2c 20 74 68 65 20 65 6e 74  us term, the ent
37165 69 72 65 20 70 72 65 66 69 78 0a 20 20 2a 2a 20  ire prefix.  ** 
37166 63 6f 75 6c 64 20 62 65 20 63 6f 6e 73 74 72 75  could be constru
37167 63 74 65 64 20 66 72 6f 6d 20 65 61 72 6c 69 65  cted from earlie
37168 72 20 64 61 74 61 20 69 6e 20 74 68 65 20 64 6f  r data in the do
37169 63 6c 69 73 74 2c 20 73 6f 20 74 68 65 72 65 0a  clist, so there.
3716a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72    ** should be r
3716b 6f 6f 6d 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  oom..  */.  asse
3716c 72 74 28 20 69 44 6f 63 6c 69 73 74 44 61 74 61  rt( iDoclistData
3716d 3e 3d 6e 2b 6e 54 65 72 6d 20 29 3b 0a 0a 20 20  >=n+nTerm );..  
3716e 69 44 61 74 61 20 3d 20 69 44 6f 63 6c 69 73 74  iData = iDoclist
3716f 44 61 74 61 2d 28 6e 2b 6e 54 65 72 6d 29 3b 0a  Data-(n+nTerm);.
37170 20 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72    memcpy(pWriter
37171 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61  ->data.pData+iDa
37172 74 61 2c 20 63 2c 20 6e 29 3b 0a 20 20 6d 65 6d  ta, c, n);.  mem
37173 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74  cpy(pWriter->dat
37174 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 6e 2c  a.pData+iData+n,
37175 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a   pTerm, nTerm);.
37176 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66 57 72  .  return leafWr
37177 69 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73  iterInternalFlus
37178 68 28 76 2c 20 70 57 72 69 74 65 72 2c 20 69 44  h(v, pWriter, iD
37179 61 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 64 61  ata, pWriter->da
3717a 74 61 2e 6e 44 61 74 61 2d 69 44 61 74 61 29 3b  ta.nData-iData);
3717b 0a 7d 0a 0a 2f 2a 20 50 75 73 68 20 70 54 65 72  .}../* Push pTer
3717c 6d 5b 6e 54 65 72 6d 5d 20 61 6c 6f 6e 67 20 77  m[nTerm] along w
3717d 69 74 68 20 74 68 65 20 64 6f 63 6c 69 73 74 20  ith the doclist 
3717e 64 61 74 61 20 74 6f 20 74 68 65 20 6c 65 61 66  data to the leaf
3717f 20 6c 61 79 65 72 20 6f 66 0a 2a 2a 20 25 5f 73   layer of.** %_s
37180 65 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74  egments..*/.stat
37181 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65  ic int leafWrite
37182 72 53 74 65 70 4d 65 72 67 65 28 66 75 6c 6c 74  rStepMerge(fullt
37183 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
37184 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
37185 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
37186 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37187 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
37188 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20  rm, int nTerm,. 
37189 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3718a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 4c                DL
3718b 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 73  Reader *pReaders
3718c 2c 20 69 6e 74 20 6e 52 65 61 64 65 72 73 29 7b  , int nReaders){
3718d 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54  .  char c[VARINT
3718e 5f 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d  _MAX+VARINT_MAX]
3718f 3b 0a 20 20 69 6e 74 20 69 54 65 72 6d 44 61 74  ;.  int iTermDat
37190 61 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74  a = pWriter->dat
37191 61 2e 6e 44 61 74 61 2c 20 69 44 6f 63 6c 69 73  a.nData, iDoclis
37192 74 44 61 74 61 3b 0a 20 20 69 6e 74 20 69 2c 20  tData;.  int i, 
37193 6e 44 61 74 61 2c 20 6e 2c 20 6e 41 63 74 75 61  nData, n, nActua
37194 6c 44 61 74 61 2c 20 6e 41 63 74 75 61 6c 2c 20  lData, nActual, 
37195 72 63 2c 20 6e 54 65 72 6d 44 69 73 74 69 6e 63  rc, nTermDistinc
37196 74 3b 0a 0a 20 20 41 53 53 45 52 54 5f 56 41 4c  t;..  ASSERT_VAL
37197 49 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 57 72  ID_LEAF_NODE(pWr
37198 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
37199 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  , pWriter->data.
3719a 6e 44 61 74 61 29 3b 0a 20 20 6e 54 65 72 6d 44  nData);.  nTermD
3719b 69 73 74 69 6e 63 74 20 3d 20 6c 65 61 66 57 72  istinct = leafWr
3719c 69 74 65 72 45 6e 63 6f 64 65 54 65 72 6d 28 70  iterEncodeTerm(p
3719d 57 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e  Writer, pTerm, n
3719e 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20 52 65 6d  Term);..  /* Rem
3719f 65 6d 62 65 72 20 6e 54 65 72 6d 44 69 73 74 69  ember nTermDisti
371a0 6e 63 74 20 69 66 20 6f 70 65 6e 69 6e 67 20 61  nct if opening a
371a1 20 6e 65 77 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20   new node. */.  
371a2 69 66 28 20 69 54 65 72 6d 44 61 74 61 3d 3d 30  if( iTermData==0
371a3 20 29 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72   ) pWriter->nTer
371a4 6d 44 69 73 74 69 6e 63 74 20 3d 20 6e 54 65 72  mDistinct = nTer
371a5 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 69 44  mDistinct;..  iD
371a6 6f 63 6c 69 73 74 44 61 74 61 20 3d 20 70 57 72  oclistData = pWr
371a7 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
371a8 3b 0a 0a 20 20 2f 2a 20 45 73 74 69 6d 61 74 65  ;..  /* Estimate
371a9 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
371aa 68 65 20 6d 65 72 67 65 64 20 64 6f 63 6c 69 73  he merged doclis
371ab 74 20 73 6f 20 77 65 20 63 61 6e 20 6c 65 61 76  t so we can leav
371ac 65 20 73 70 61 63 65 0a 20 20 2a 2a 20 74 6f 20  e space.  ** to 
371ad 65 6e 63 6f 64 65 20 69 74 2e 0a 20 20 2a 2f 0a  encode it..  */.
371ae 20 20 66 6f 72 28 69 3d 30 2c 20 6e 44 61 74 61    for(i=0, nData
371af 3d 30 3b 20 69 3c 6e 52 65 61 64 65 72 73 3b 20  =0; i<nReaders; 
371b0 69 2b 2b 29 7b 0a 20 20 20 20 6e 44 61 74 61 20  i++){.    nData 
371b1 2b 3d 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74  += dlrAllDataByt
371b2 65 73 28 26 70 52 65 61 64 65 72 73 5b 69 5d 29  es(&pReaders[i])
371b3 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 66 74 73 33  ;.  }.  n = fts3
371b4 50 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 44 61  PutVarint(c, nDa
371b5 74 61 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  ta);.  dataBuffe
371b6 72 41 70 70 65 6e 64 28 26 70 57 72 69 74 65 72  rAppend(&pWriter
371b7 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 29 3b 0a 0a  ->data, c, n);..
371b8 20 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 26    docListMerge(&
371b9 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20 70  pWriter->data, p
371ba 52 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65 72  Readers, nReader
371bb 73 29 3b 0a 20 20 41 53 53 45 52 54 5f 56 41 4c  s);.  ASSERT_VAL
371bc 49 44 5f 44 4f 43 4c 49 53 54 28 44 4c 5f 44 45  ID_DOCLIST(DL_DE
371bd 46 41 55 4c 54 2c 0a 20 20 20 20 20 20 20 20 20  FAULT,.         
371be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
371bf 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
371c0 61 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e  a+iDoclistData+n
371c1 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
371c2 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
371c3 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69 44 6f  ->data.nData-iDo
371c4 63 6c 69 73 74 44 61 74 61 2d 6e 2c 20 4e 55 4c  clistData-n, NUL
371c5 4c 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 61 63  L);..  /* The ac
371c6 74 75 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 64  tual amount of d
371c7 6f 63 6c 69 73 74 20 64 61 74 61 20 61 74 20 74  oclist data at t
371c8 68 69 73 20 70 6f 69 6e 74 20 63 6f 75 6c 64 20  his point could 
371c9 62 65 20 73 6d 61 6c 6c 65 72 0a 20 20 2a 2a 20  be smaller.  ** 
371ca 74 68 61 6e 20 74 68 65 20 6c 65 6e 67 74 68 20  than the length 
371cb 77 65 20 65 6e 63 6f 64 65 64 2e 20 20 41 64 64  we encoded.  Add
371cc 69 74 69 6f 6e 61 6c 6c 79 2c 20 74 68 65 20 73  itionally, the s
371cd 70 61 63 65 20 72 65 71 75 69 72 65 64 20 74 6f  pace required to
371ce 0a 20 20 2a 2a 20 65 6e 63 6f 64 65 20 74 68 69  .  ** encode thi
371cf 73 20 6c 65 6e 67 74 68 20 63 6f 75 6c 64 20 62  s length could b
371d0 65 20 73 6d 61 6c 6c 65 72 2e 20 20 46 6f 72 20  e smaller.  For 
371d1 73 6d 61 6c 6c 20 64 6f 63 6c 69 73 74 73 2c 20  small doclists, 
371d2 74 68 69 73 20 69 73 0a 20 20 2a 2a 20 6e 6f 74  this is.  ** not
371d3 20 61 20 62 69 67 20 64 65 61 6c 2c 20 77 65 20   a big deal, we 
371d4 63 61 6e 20 6a 75 73 74 20 75 73 65 20 6d 65 6d  can just use mem
371d5 6d 6f 76 65 28 29 20 74 6f 20 61 64 6a 75 73 74  move() to adjust
371d6 20 74 68 69 6e 67 73 2e 0a 20 20 2a 2f 0a 20 20   things..  */.  
371d7 6e 41 63 74 75 61 6c 44 61 74 61 20 3d 20 70 57  nActualData = pW
371d8 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
371d9 61 2d 28 69 44 6f 63 6c 69 73 74 44 61 74 61 2b  a-(iDoclistData+
371da 6e 29 3b 0a 20 20 6e 41 63 74 75 61 6c 20 3d 20  n);.  nActual = 
371db 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c  fts3PutVarint(c,
371dc 20 6e 41 63 74 75 61 6c 44 61 74 61 29 3b 0a 20   nActualData);. 
371dd 20 61 73 73 65 72 74 28 20 6e 41 63 74 75 61 6c   assert( nActual
371de 44 61 74 61 3c 3d 6e 44 61 74 61 20 29 3b 0a 20  Data<=nData );. 
371df 20 61 73 73 65 72 74 28 20 6e 41 63 74 75 61 6c   assert( nActual
371e0 3c 3d 6e 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  <=n );..  /* If 
371e1 74 68 65 20 6e 65 77 20 64 6f 63 6c 69 73 74 20  the new doclist 
371e2 69 73 20 62 69 67 20 65 6e 6f 75 67 68 20 66 6f  is big enough fo
371e3 72 20 66 6f 72 63 65 20 61 20 73 74 61 6e 64 61  r force a standa
371e4 6c 6f 6e 65 20 6c 65 61 66 0a 20 20 2a 2a 20 6e  lone leaf.  ** n
371e5 6f 64 65 2c 20 77 65 20 63 61 6e 20 69 6d 6d 65  ode, we can imme
371e6 64 69 61 74 65 6c 79 20 66 6c 75 73 68 20 69 74  diately flush it
371e7 20 69 6e 6c 69 6e 65 20 77 69 74 68 6f 75 74 20   inline without 
371e8 64 6f 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 6d  doing the.  ** m
371e9 65 6d 6d 6f 76 65 28 29 2e 0a 20 20 2a 2f 0a 20  emmove()..  */. 
371ea 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
371eb 54 68 69 73 20 74 65 73 74 20 6d 61 74 63 68 65  This test matche
371ec 73 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  s leafWriterStep
371ed 28 29 2c 20 77 68 69 63 68 20 64 6f 65 73 20 74  (), which does t
371ee 68 69 73 0a 20 20 2a 2a 20 74 65 73 74 20 62 65  his.  ** test be
371ef 66 6f 72 65 20 69 74 20 6b 6e 6f 77 73 20 74 68  fore it knows th
371f0 65 20 63 6f 73 74 20 74 6f 20 76 61 72 69 6e 74  e cost to varint
371f1 2d 65 6e 63 6f 64 65 20 74 68 65 20 74 65 72 6d  -encode the term
371f2 20 61 6e 64 0a 20 20 2a 2a 20 64 6f 63 6c 69 73   and.  ** doclis
371f3 74 20 6c 65 6e 67 74 68 73 2e 20 20 41 74 20 73  t lengths.  At s
371f4 6f 6d 65 20 70 6f 69 6e 74 2c 20 63 68 61 6e 67  ome point, chang
371f5 65 20 74 6f 0a 20 20 2a 2a 20 70 57 72 69 74 65  e to.  ** pWrite
371f6 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69 54  r->data.nData-iT
371f7 65 72 6d 44 61 74 61 3e 53 54 41 4e 44 41 4c 4f  ermData>STANDALO
371f8 4e 45 5f 4d 49 4e 2e 0a 20 20 2a 2f 0a 20 20 69  NE_MIN..  */.  i
371f9 66 28 20 6e 54 65 72 6d 2b 6e 41 63 74 75 61 6c  f( nTerm+nActual
371fa 44 61 74 61 3e 53 54 41 4e 44 41 4c 4f 4e 45 5f  Data>STANDALONE_
371fb 4d 49 4e 20 29 7b 0a 20 20 20 20 2f 2a 20 50 75  MIN ){.    /* Pu
371fc 73 68 20 6c 65 61 66 20 6e 6f 64 65 20 66 72 6f  sh leaf node fro
371fd 6d 20 62 65 66 6f 72 65 20 74 68 69 73 20 74 65  m before this te
371fe 72 6d 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 69  rm. */.    if( i
371ff 54 65 72 6d 44 61 74 61 3e 30 20 29 7b 0a 20 20  TermData>0 ){.  
37200 20 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69      rc = leafWri
37201 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68  terInternalFlush
37202 28 76 2c 20 70 57 72 69 74 65 72 2c 20 30 2c 20  (v, pWriter, 0, 
37203 69 54 65 72 6d 44 61 74 61 29 3b 0a 20 20 20 20  iTermData);.    
37204 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
37205 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
37206 0a 0a 20 20 20 20 20 20 70 57 72 69 74 65 72 2d  ..      pWriter-
37207 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 3d  >nTermDistinct =
37208 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a   nTermDistinct;.
37209 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69      }..    /* Fi
3720a 78 20 74 68 65 20 65 6e 63 6f 64 65 64 20 64 6f  x the encoded do
3720b 63 6c 69 73 74 20 6c 65 6e 67 74 68 2e 20 2a 2f  clist length. */
3720c 0a 20 20 20 20 69 44 6f 63 6c 69 73 74 44 61 74  .    iDoclistDat
3720d 61 20 2b 3d 20 6e 20 2d 20 6e 41 63 74 75 61 6c  a += n - nActual
3720e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 57 72  ;.    memcpy(pWr
3720f 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
37210 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2c 20 63  +iDoclistData, c
37211 2c 20 6e 41 63 74 75 61 6c 29 3b 0a 0a 20 20 20  , nActual);..   
37212 20 2f 2a 20 50 75 73 68 20 74 68 65 20 73 74 61   /* Push the sta
37213 6e 64 61 6c 6f 6e 65 20 6c 65 61 66 20 6e 6f 64  ndalone leaf nod
37214 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6c  e. */.    rc = l
37215 65 61 66 57 72 69 74 65 72 49 6e 6c 69 6e 65 46  eafWriterInlineF
37216 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 2c  lush(v, pWriter,
37217 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69   pTerm, nTerm, i
37218 44 6f 63 6c 69 73 74 44 61 74 61 29 3b 0a 20 20  DoclistData);.  
37219 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3721a 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
3721b 0a 0a 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 74  ..    /* Leave t
3721c 68 65 20 6e 6f 64 65 20 65 6d 70 74 79 2e 20 2a  he node empty. *
3721d 2f 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  /.    dataBuffer
3721e 52 65 73 65 74 28 26 70 57 72 69 74 65 72 2d 3e  Reset(&pWriter->
3721f 64 61 74 61 29 3b 0a 0a 20 20 20 20 72 65 74 75  data);..    retu
37220 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
37221 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
37222 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65  we know that the
37223 20 64 6f 63 6c 69 73 74 20 77 61 73 20 73 6d 61   doclist was sma
37224 6c 6c 2c 20 73 6f 20 64 6f 20 74 68 65 0a 20 20  ll, so do the.  
37225 2a 2a 20 6d 65 6d 6d 6f 76 65 20 69 66 20 69 6e  ** memmove if in
37226 64 69 63 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  dicated..  */.  
37227 69 66 28 20 6e 41 63 74 75 61 6c 3c 6e 20 29 7b  if( nActual<n ){
37228 0a 20 20 20 20 6d 65 6d 6d 6f 76 65 28 70 57 72  .    memmove(pWr
37229 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
3722a 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e 41  +iDoclistData+nA
3722b 63 74 75 61 6c 2c 0a 20 20 20 20 20 20 20 20 20  ctual,.         
3722c 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61     pWriter->data
3722d 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44  .pData+iDoclistD
3722e 61 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20 20  ata+n,.         
3722f 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61     pWriter->data
37230 2e 6e 44 61 74 61 2d 28 69 44 6f 63 6c 69 73 74  .nData-(iDoclist
37231 44 61 74 61 2b 6e 29 29 3b 0a 20 20 20 20 70 57  Data+n));.    pW
37232 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
37233 61 20 2d 3d 20 6e 2d 6e 41 63 74 75 61 6c 3b 0a  a -= n-nActual;.
37234 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63    }..  /* Replac
37235 65 20 77 72 69 74 74 65 6e 20 6c 65 6e 67 74 68  e written length
37236 20 77 69 74 68 20 61 63 74 75 61 6c 20 6c 65 6e   with actual len
37237 67 74 68 2e 20 2a 2f 0a 20 20 6d 65 6d 63 70 79  gth. */.  memcpy
37238 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
37239 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44 61 74  Data+iDoclistDat
3723a 61 2c 20 63 2c 20 6e 41 63 74 75 61 6c 29 3b 0a  a, c, nActual);.
3723b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e 6f 64  .  /* If the nod
3723c 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20  e is too large, 
3723d 62 72 65 61 6b 20 74 68 69 6e 67 73 20 75 70 2e  break things up.
3723e 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68   */.  /* TODO(sh
3723f 65 73 73 29 20 54 68 69 73 20 74 65 73 74 20 6d  ess) This test m
37240 61 74 63 68 65 73 20 6c 65 61 66 57 72 69 74 65  atches leafWrite
37241 72 53 74 65 70 28 29 2c 20 77 68 69 63 68 20 64  rStep(), which d
37242 6f 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 74 65  oes this.  ** te
37243 73 74 20 62 65 66 6f 72 65 20 69 74 20 6b 6e 6f  st before it kno
37244 77 73 20 74 68 65 20 63 6f 73 74 20 74 6f 20 76  ws the cost to v
37245 61 72 69 6e 74 2d 65 6e 63 6f 64 65 20 74 68 65  arint-encode the
37246 20 74 65 72 6d 20 61 6e 64 0a 20 20 2a 2a 20 64   term and.  ** d
37247 6f 63 6c 69 73 74 20 6c 65 6e 67 74 68 73 2e 20  oclist lengths. 
37248 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 2c 20   At some point, 
37249 63 68 61 6e 67 65 20 74 6f 0a 20 20 2a 2a 20 70  change to.  ** p
3724a 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
3724b 74 61 3e 4c 45 41 46 5f 4d 41 58 2e 0a 20 20 2a  ta>LEAF_MAX..  *
3724c 2f 0a 20 20 69 66 28 20 69 54 65 72 6d 44 61 74  /.  if( iTermDat
3724d 61 2b 6e 54 65 72 6d 2b 6e 41 63 74 75 61 6c 44  a+nTerm+nActualD
3724e 61 74 61 3e 4c 45 41 46 5f 4d 41 58 20 29 7b 0a  ata>LEAF_MAX ){.
3724f 20 20 20 20 2f 2a 20 46 6c 75 73 68 20 6f 75 74      /* Flush out
37250 20 74 68 65 20 6c 65 61 64 69 6e 67 20 64 61 74   the leading dat
37251 61 20 61 73 20 61 20 6e 6f 64 65 20 2a 2f 0a 20  a as a node */. 
37252 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74     rc = leafWrit
37253 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28  erInternalFlush(
37254 76 2c 20 70 57 72 69 74 65 72 2c 20 30 2c 20 69  v, pWriter, 0, i
37255 54 65 72 6d 44 61 74 61 29 3b 0a 20 20 20 20 69  TermData);.    i
37256 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
37257 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
37258 20 20 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72     pWriter->nTer
37259 6d 44 69 73 74 69 6e 63 74 20 3d 20 6e 54 65 72  mDistinct = nTer
3725a 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 20 20  mDistinct;..    
3725b 2f 2a 20 52 65 62 75 69 6c 64 20 68 65 61 64 65  /* Rebuild heade
3725c 72 20 75 73 69 6e 67 20 74 68 65 20 63 75 72 72  r using the curr
3725d 65 6e 74 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20  ent term */.    
3725e 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  n = fts3PutVarin
3725f 74 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  t(pWriter->data.
37260 70 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 20 6e  pData, 0);.    n
37261 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e   += fts3PutVarin
37262 74 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  t(pWriter->data.
37263 70 44 61 74 61 2b 6e 2c 20 6e 54 65 72 6d 29 3b  pData+n, nTerm);
37264 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 57 72 69  .    memcpy(pWri
37265 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
37266 6e 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29  n, pTerm, nTerm)
37267 3b 0a 20 20 20 20 6e 20 2b 3d 20 6e 54 65 72 6d  ;.    n += nTerm
37268 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20  ;..    /* There 
37269 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
3726a 20 72 6f 6f 6d 2c 20 62 65 63 61 75 73 65 20 74   room, because t
3726b 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 63 6f  he previous enco
3726c 64 69 6e 67 0a 20 20 20 20 2a 2a 20 69 6e 63 6c  ding.    ** incl
3726d 75 64 65 64 20 61 6c 6c 20 64 61 74 61 20 6e 65  uded all data ne
3726e 63 65 73 73 61 72 79 20 74 6f 20 63 6f 6e 73 74  cessary to const
3726f 72 75 63 74 20 74 68 65 20 74 65 72 6d 2e 0a 20  ruct the term.. 
37270 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
37271 28 20 6e 3c 69 44 6f 63 6c 69 73 74 44 61 74 61  ( n<iDoclistData
37272 20 29 3b 0a 20 20 20 20 2f 2a 20 53 6f 20 6c 6f   );.    /* So lo
37273 6e 67 20 61 73 20 53 54 41 4e 44 41 4c 4f 4e 45  ng as STANDALONE
37274 5f 4d 49 4e 20 69 73 20 68 61 6c 66 20 6f 72 20  _MIN is half or 
37275 6c 65 73 73 20 6f 66 20 4c 45 41 46 5f 4d 41 58  less of LEAF_MAX
37276 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c  , the.    ** fol
37277 6c 6f 77 69 6e 67 20 6d 65 6d 63 70 79 28 29 20  lowing memcpy() 
37278 69 73 20 73 61 66 65 20 28 61 73 20 6f 70 70 6f  is safe (as oppo
37279 73 65 64 20 74 6f 20 6e 65 65 64 69 6e 67 20 61  sed to needing a
3727a 20 6d 65 6d 6d 6f 76 65 29 2e 0a 20 20 20 20 2a   memmove)..    *
3727b 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 32 2a  /.    assert( 2*
3727c 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 3c 3d  STANDALONE_MIN<=
3727d 4c 45 41 46 5f 4d 41 58 20 29 3b 0a 20 20 20 20  LEAF_MAX );.    
3727e 61 73 73 65 72 74 28 20 6e 2b 70 57 72 69 74 65  assert( n+pWrite
3727f 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69 44  r->data.nData-iD
37280 6f 63 6c 69 73 74 44 61 74 61 3c 69 44 6f 63 6c  oclistData<iDocl
37281 69 73 74 44 61 74 61 20 29 3b 0a 20 20 20 20 6d  istData );.    m
37282 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64  emcpy(pWriter->d
37283 61 74 61 2e 70 44 61 74 61 2b 6e 2c 0a 20 20 20  ata.pData+n,.   
37284 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d          pWriter-
37285 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63  >data.pData+iDoc
37286 6c 69 73 74 44 61 74 61 2c 0a 20 20 20 20 20 20  listData,.      
37287 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61       pWriter->da
37288 74 61 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73  ta.nData-iDoclis
37289 74 44 61 74 61 29 3b 0a 20 20 20 20 70 57 72 69  tData);.    pWri
3728a 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 20  ter->data.nData 
3728b 2d 3d 20 69 44 6f 63 6c 69 73 74 44 61 74 61 2d  -= iDoclistData-
3728c 6e 3b 0a 20 20 7d 0a 20 20 41 53 53 45 52 54 5f  n;.  }.  ASSERT_
3728d 56 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f 44 45 28  VALID_LEAF_NODE(
3728e 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44  pWriter->data.pD
3728f 61 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 64 61  ata, pWriter->da
37290 74 61 2e 6e 44 61 74 61 29 3b 0a 0a 20 20 72 65  ta.nData);..  re
37291 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
37292 7d 0a 0a 2f 2a 20 50 75 73 68 20 70 54 65 72 6d  }../* Push pTerm
37293 5b 6e 54 65 72 6d 5d 20 61 6c 6f 6e 67 20 77 69  [nTerm] along wi
37294 74 68 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64  th the doclist d
37295 61 74 61 20 74 6f 20 74 68 65 20 6c 65 61 66 20  ata to the leaf 
37296 6c 61 79 65 72 20 6f 66 0a 2a 2a 20 25 5f 73 65  layer of.** %_se
37297 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 2f 2a 20 54 4f  gments..*/./* TO
37298 44 4f 28 73 68 65 73 73 29 20 52 65 76 69 73 65  DO(shess) Revise
37299 20 77 72 69 74 65 5a 65 72 6f 53 65 67 6d 65 6e   writeZeroSegmen
3729a 74 28 29 20 73 6f 20 74 68 61 74 20 64 6f 63 6c  t() so that docl
3729b 69 73 74 73 20 61 72 65 0a 2a 2a 20 63 6f 6e 73  ists are.** cons
3729c 74 72 75 63 74 65 64 20 64 69 72 65 63 74 6c 79  tructed directly
3729d 20 69 6e 20 70 57 72 69 74 65 72 2d 3e 64 61 74   in pWriter->dat
3729e 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  a..*/.static int
3729f 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 28   leafWriterStep(
372a0 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
372a1 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57  , LeafWriter *pW
372a2 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
372a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372a4 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
372a5 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20  rm, int nTerm,. 
372a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372a7 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
372a8 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
372a9 6e 44 61 74 61 29 7b 0a 20 20 69 6e 74 20 72 63  nData){.  int rc
372aa 3b 0a 20 20 44 4c 52 65 61 64 65 72 20 72 65 61  ;.  DLReader rea
372ab 64 65 72 3b 0a 0a 20 20 64 6c 72 49 6e 69 74 28  der;..  dlrInit(
372ac 26 72 65 61 64 65 72 2c 20 44 4c 5f 44 45 46 41  &reader, DL_DEFA
372ad 55 4c 54 2c 20 70 44 61 74 61 2c 20 6e 44 61 74  ULT, pData, nDat
372ae 61 29 3b 0a 20 20 72 63 20 3d 20 6c 65 61 66 57  a);.  rc = leafW
372af 72 69 74 65 72 53 74 65 70 4d 65 72 67 65 28 76  riterStepMerge(v
372b0 2c 20 70 57 72 69 74 65 72 2c 20 70 54 65 72 6d  , pWriter, pTerm
372b1 2c 20 6e 54 65 72 6d 2c 20 26 72 65 61 64 65 72  , nTerm, &reader
372b2 2c 20 31 29 3b 0a 20 20 64 6c 72 44 65 73 74 72  , 1);.  dlrDestr
372b3 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20  oy(&reader);..  
372b4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
372b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372b9 2f 0a 2f 2a 20 4c 65 61 66 52 65 61 64 65 72 20  /./* LeafReader 
372ba 69 73 20 75 73 65 64 20 74 6f 20 69 74 65 72 61  is used to itera
372bb 74 65 20 6f 76 65 72 20 61 6e 20 69 6e 64 69 76  te over an indiv
372bc 69 64 75 61 6c 20 6c 65 61 66 20 6e 6f 64 65 2e  idual leaf node.
372bd 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75   */.typedef stru
372be 63 74 20 4c 65 61 66 52 65 61 64 65 72 20 7b 0a  ct LeafReader {.
372bf 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65 72    DataBuffer ter
372c0 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63  m;          /* c
372c1 6f 70 79 20 6f 66 20 63 75 72 72 65 6e 74 20 74  opy of current t
372c2 65 72 6d 2e 20 2a 2f 0a 0a 20 20 63 6f 6e 73 74  erm. */..  const
372c3 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20   char *pData;   
372c4 20 20 20 20 20 2f 2a 20 64 61 74 61 20 66 6f 72       /* data for
372c5 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a   current term. *
372c6 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 7d  /.  int nData;.}
372c7 20 4c 65 61 66 52 65 61 64 65 72 3b 0a 0a 73 74   LeafReader;..st
372c8 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66 52 65  atic void leafRe
372c9 61 64 65 72 44 65 73 74 72 6f 79 28 4c 65 61 66  aderDestroy(Leaf
372ca 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
372cb 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  {.  dataBufferDe
372cc 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d 3e  stroy(&pReader->
372cd 74 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d 42 4c  term);.  SCRAMBL
372ce 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 73  E(pReader);.}..s
372cf 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65  tatic int leafRe
372d0 61 64 65 72 41 74 45 6e 64 28 4c 65 61 66 52 65  aderAtEnd(LeafRe
372d1 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
372d2 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
372d3 2d 3e 6e 44 61 74 61 3c 3d 30 3b 0a 7d 0a 0a 2f  ->nData<=0;.}../
372d4 2a 20 41 63 63 65 73 73 20 74 68 65 20 63 75 72  * Access the cur
372d5 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74  rent term. */.st
372d6 61 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61  atic int leafRea
372d7 64 65 72 54 65 72 6d 42 79 74 65 73 28 4c 65 61  derTermBytes(Lea
372d8 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  fReader *pReader
372d9 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  ){.  return pRea
372da 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3b  der->term.nData;
372db 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .}.static const 
372dc 63 68 61 72 20 2a 6c 65 61 66 52 65 61 64 65 72  char *leafReader
372dd 54 65 72 6d 28 4c 65 61 66 52 65 61 64 65 72 20  Term(LeafReader 
372de 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
372df 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e 74 65  ert( pReader->te
372e0 72 6d 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20  rm.nData>0 );.  
372e1 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
372e2 74 65 72 6d 2e 70 44 61 74 61 3b 0a 7d 0a 0a 2f  term.pData;.}../
372e3 2a 20 41 63 63 65 73 73 20 74 68 65 20 64 6f 63  * Access the doc
372e4 6c 69 73 74 20 64 61 74 61 20 66 6f 72 20 74 68  list data for th
372e5 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20  e current term. 
372e6 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65  */.static int le
372e7 61 66 52 65 61 64 65 72 44 61 74 61 42 79 74 65  afReaderDataByte
372e8 73 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  s(LeafReader *pR
372e9 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 44  eader){.  int nD
372ea 61 74 61 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ata;.  assert( p
372eb 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61  Reader->term.nDa
372ec 74 61 3e 30 20 29 3b 0a 20 20 66 74 73 33 47 65  ta>0 );.  fts3Ge
372ed 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
372ee 72 2d 3e 70 44 61 74 61 2c 20 26 6e 44 61 74 61  r->pData, &nData
372ef 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 44 61 74  );.  return nDat
372f0 61 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73  a;.}.static cons
372f1 74 20 63 68 61 72 20 2a 6c 65 61 66 52 65 61 64  t char *leafRead
372f2 65 72 44 61 74 61 28 4c 65 61 66 52 65 61 64 65  erData(LeafReade
372f3 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69  r *pReader){.  i
372f4 6e 74 20 6e 2c 20 6e 44 61 74 61 3b 0a 20 20 61  nt n, nData;.  a
372f5 73 73 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e  ssert( pReader->
372f6 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20 29 3b 0a  term.nData>0 );.
372f7 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
372f8 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70  int32(pReader->p
372f9 44 61 74 61 2c 20 26 6e 44 61 74 61 29 3b 0a 20  Data, &nData);. 
372fa 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
372fb 3e 70 44 61 74 61 2b 6e 3b 0a 7d 0a 0a 73 74 61  >pData+n;.}..sta
372fc 74 69 63 20 76 6f 69 64 20 6c 65 61 66 52 65 61  tic void leafRea
372fd 64 65 72 49 6e 69 74 28 63 6f 6e 73 74 20 63 68  derInit(const ch
372fe 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
372ff 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
37300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37301 20 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65   LeafReader *pRe
37302 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 54 65  ader){.  int nTe
37303 72 6d 2c 20 6e 3b 0a 0a 20 20 61 73 73 65 72 74  rm, n;..  assert
37304 28 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61  ( nData>0 );.  a
37305 73 73 65 72 74 28 20 70 44 61 74 61 5b 30 5d 3d  ssert( pData[0]=
37306 3d 27 5c 30 27 20 29 3b 0a 0a 20 20 43 4c 45 41  ='\0' );..  CLEA
37307 52 28 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 2f  R(pReader);..  /
37308 2a 20 52 65 61 64 20 74 68 65 20 66 69 72 73 74  * Read the first
37309 20 74 65 72 6d 2c 20 73 6b 69 70 70 69 6e 67 20   term, skipping 
3730a 74 68 65 20 68 65 61 64 65 72 20 62 79 74 65 2e  the header byte.
3730b 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65   */.  n = fts3Ge
3730c 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2b  tVarint32(pData+
3730d 31 2c 20 26 6e 54 65 72 6d 29 3b 0a 20 20 64 61  1, &nTerm);.  da
3730e 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 52  taBufferInit(&pR
3730f 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 6e 54 65  eader->term, nTe
37310 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  rm);.  dataBuffe
37311 72 52 65 70 6c 61 63 65 28 26 70 52 65 61 64 65  rReplace(&pReade
37312 72 2d 3e 74 65 72 6d 2c 20 70 44 61 74 61 2b 31  r->term, pData+1
37313 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 2f  +n, nTerm);..  /
37314 2a 20 50 6f 73 69 74 69 6f 6e 20 61 66 74 65 72  * Position after
37315 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 2e   the first term.
37316 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 31 2b   */.  assert( 1+
37317 6e 2b 6e 54 65 72 6d 3c 6e 44 61 74 61 20 29 3b  n+nTerm<nData );
37318 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  .  pReader->pDat
37319 61 20 3d 20 70 44 61 74 61 2b 31 2b 6e 2b 6e 54  a = pData+1+n+nT
3731a 65 72 6d 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  erm;.  pReader->
3731b 6e 44 61 74 61 20 3d 20 6e 44 61 74 61 2d 31 2d  nData = nData-1-
3731c 6e 2d 6e 54 65 72 6d 3b 0a 7d 0a 0a 2f 2a 20 53  n-nTerm;.}../* S
3731d 74 65 70 20 74 68 65 20 72 65 61 64 65 72 20 66  tep the reader f
3731e 6f 72 77 61 72 64 20 74 6f 20 74 68 65 20 6e 65  orward to the ne
3731f 78 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74  xt term. */.stat
37320 69 63 20 76 6f 69 64 20 6c 65 61 66 52 65 61 64  ic void leafRead
37321 65 72 53 74 65 70 28 4c 65 61 66 52 65 61 64 65  erStep(LeafReade
37322 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69  r *pReader){.  i
37323 6e 74 20 6e 2c 20 6e 44 61 74 61 2c 20 6e 50 72  nt n, nData, nPr
37324 65 66 69 78 2c 20 6e 53 75 66 66 69 78 3b 0a 20  efix, nSuffix;. 
37325 20 61 73 73 65 72 74 28 20 21 6c 65 61 66 52 65   assert( !leafRe
37326 61 64 65 72 41 74 45 6e 64 28 70 52 65 61 64 65  aderAtEnd(pReade
37327 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 53 6b 69 70  r) );..  /* Skip
37328 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 27   previous entry'
37329 73 20 64 61 74 61 20 62 6c 6f 63 6b 2e 20 2a 2f  s data block. */
3732a 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61  .  n = fts3GetVa
3732b 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
3732c 70 44 61 74 61 2c 20 26 6e 44 61 74 61 29 3b 0a  pData, &nData);.
3732d 20 20 61 73 73 65 72 74 28 20 6e 2b 6e 44 61 74    assert( n+nDat
3732e 61 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  a<=pReader->nDat
3732f 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  a );.  pReader->
37330 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 44 61 74 61  pData += n+nData
37331 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61  ;.  pReader->nDa
37332 74 61 20 2d 3d 20 6e 2b 6e 44 61 74 61 3b 0a 0a  ta -= n+nData;..
37333 20 20 69 66 28 20 21 6c 65 61 66 52 65 61 64 65    if( !leafReade
37334 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
37335 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72  ){.    /* Constr
37336 75 63 74 20 74 68 65 20 6e 65 77 20 74 65 72 6d  uct the new term
37337 20 75 73 69 6e 67 20 61 20 70 72 65 66 69 78 20   using a prefix 
37338 66 72 6f 6d 20 74 68 65 20 6f 6c 64 20 74 65 72  from the old ter
37339 6d 20 70 6c 75 73 20 61 0a 20 20 20 20 2a 2a 20  m plus a.    ** 
3733a 73 75 66 66 69 78 20 66 72 6f 6d 20 74 68 65 20  suffix from the 
3733b 6c 65 61 66 20 64 61 74 61 2e 0a 20 20 20 20 2a  leaf data..    *
3733c 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65  /.    n = fts3Ge
3733d 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
3733e 72 2d 3e 70 44 61 74 61 2c 20 26 6e 50 72 65 66  r->pData, &nPref
3733f 69 78 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74  ix);.    n += ft
37340 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
37341 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20  eader->pData+n, 
37342 26 6e 53 75 66 66 69 78 29 3b 0a 20 20 20 20 61  &nSuffix);.    a
37343 73 73 65 72 74 28 20 6e 2b 6e 53 75 66 66 69 78  ssert( n+nSuffix
37344 3c 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  <pReader->nData 
37345 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
37346 74 65 72 6d 2e 6e 44 61 74 61 20 3d 20 6e 50 72  term.nData = nPr
37347 65 66 69 78 3b 0a 20 20 20 20 64 61 74 61 42 75  efix;.    dataBu
37348 66 66 65 72 41 70 70 65 6e 64 28 26 70 52 65 61  fferAppend(&pRea
37349 64 65 72 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64  der->term, pRead
3734a 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e 53 75  er->pData+n, nSu
3734b 66 66 69 78 29 3b 0a 0a 20 20 20 20 70 52 65 61  ffix);..    pRea
3734c 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b  der->pData += n+
3734d 6e 53 75 66 66 69 78 3b 0a 20 20 20 20 70 52 65  nSuffix;.    pRe
3734e 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e  ader->nData -= n
3734f 2b 6e 53 75 66 66 69 78 3b 0a 20 20 7d 0a 7d 0a  +nSuffix;.  }.}.
37350 0a 2f 2a 20 73 74 72 63 6d 70 2d 73 74 79 6c 65  ./* strcmp-style
37351 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70   comparison of p
37352 52 65 61 64 65 72 27 73 20 63 75 72 72 65 6e 74  Reader's current
37353 20 74 65 72 6d 20 61 67 61 69 6e 73 74 20 70 54   term against pT
37354 65 72 6d 2e 0a 2a 2a 20 49 66 20 69 73 50 72 65  erm..** If isPre
37355 66 69 78 2c 20 65 71 75 61 6c 69 74 79 20 6d 65  fix, equality me
37356 61 6e 73 20 65 71 75 61 6c 20 74 68 72 6f 75 67  ans equal throug
37357 68 20 6e 54 65 72 6d 20 62 79 74 65 73 2e 0a 2a  h nTerm bytes..*
37358 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
37359 66 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 4c  fReaderTermCmp(L
3735a 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61 64  eafReader *pRead
3735b 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
3735c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3735d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
3735e 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69  rm, int nTerm, i
3735f 6e 74 20 69 73 50 72 65 66 69 78 29 7b 0a 20 20  nt isPrefix){.  
37360 69 6e 74 20 63 2c 20 6e 20 3d 20 70 52 65 61 64  int c, n = pRead
37361 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3c 6e  er->term.nData<n
37362 54 65 72 6d 20 3f 20 70 52 65 61 64 65 72 2d 3e  Term ? pReader->
37363 74 65 72 6d 2e 6e 44 61 74 61 20 3a 20 6e 54 65  term.nData : nTe
37364 72 6d 3b 0a 20 20 69 66 28 20 6e 3d 3d 30 20 29  rm;.  if( n==0 )
37365 7b 0a 20 20 20 20 69 66 28 20 70 52 65 61 64 65  {.    if( pReade
37366 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20  r->term.nData>0 
37367 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20  ) return -1;.   
37368 20 69 66 28 6e 54 65 72 6d 3e 30 20 29 20 72 65   if(nTerm>0 ) re
37369 74 75 72 6e 20 31 3b 0a 20 20 20 20 72 65 74 75  turn 1;.    retu
3736a 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 63 20 3d  rn 0;.  }..  c =
3736b 20 6d 65 6d 63 6d 70 28 70 52 65 61 64 65 72 2d   memcmp(pReader-
3736c 3e 74 65 72 6d 2e 70 44 61 74 61 2c 20 70 54 65  >term.pData, pTe
3736d 72 6d 2c 20 6e 29 3b 0a 20 20 69 66 28 20 63 21  rm, n);.  if( c!
3736e 3d 30 20 29 20 72 65 74 75 72 6e 20 63 3b 0a 20  =0 ) return c;. 
3736f 20 69 66 28 20 69 73 50 72 65 66 69 78 20 26 26   if( isPrefix &&
37370 20 6e 3d 3d 6e 54 65 72 6d 20 29 20 72 65 74 75   n==nTerm ) retu
37371 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70  rn 0;.  return p
37372 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61  Reader->term.nDa
37373 74 61 20 2d 20 6e 54 65 72 6d 3b 0a 7d 0a 0a 0a  ta - nTerm;.}...
37374 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
37375 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37376 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37377 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37378 2a 2f 0a 2f 2a 20 4c 65 61 76 65 73 52 65 61 64  */./* LeavesRead
37379 65 72 20 77 72 61 70 73 20 4c 65 61 66 52 65 61  er wraps LeafRea
3737a 64 65 72 20 74 6f 20 61 6c 6c 6f 77 20 69 74 65  der to allow ite
3737b 72 61 74 69 6e 67 20 6f 76 65 72 20 74 68 65 20  rating over the 
3737c 65 6e 74 69 72 65 0a 2a 2a 20 6c 65 61 66 20 6c  entire.** leaf l
3737d 61 79 65 72 20 6f 66 20 74 68 65 20 74 72 65 65  ayer of the tree
3737e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
3737f 75 63 74 20 4c 65 61 76 65 73 52 65 61 64 65 72  uct LeavesReader
37380 20 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20   {.  int idx;   
37381 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37382 2a 20 49 6e 64 65 78 20 77 69 74 68 69 6e 20 74  * Index within t
37383 68 65 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a 0a  he segment. */..
37384 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
37385 70 53 74 6d 74 3b 20 20 20 20 20 20 2f 2a 20 53  pStmt;      /* S
37386 74 61 74 65 6d 65 6e 74 20 77 65 27 72 65 20 73  tatement we're s
37387 74 72 65 61 6d 69 6e 67 20 6c 65 61 76 65 73 20  treaming leaves 
37388 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 65  from. */.  int e
37389 6f 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  of;             
3738a 20 20 20 20 20 2f 2a 20 77 65 27 76 65 20 73 65       /* we've se
3738b 65 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 66  en SQLITE_DONE f
3738c 72 6f 6d 20 70 53 74 6d 74 2e 20 2a 2f 0a 0a 20  rom pStmt. */.. 
3738d 20 4c 65 61 66 52 65 61 64 65 72 20 6c 65 61 66   LeafReader leaf
3738e 52 65 61 64 65 72 3b 20 20 20 20 2f 2a 20 72 65  Reader;    /* re
3738f 61 64 65 72 20 66 6f 72 20 74 68 65 20 63 75 72  ader for the cur
37390 72 65 6e 74 20 6c 65 61 66 2e 20 2a 2f 0a 20 20  rent leaf. */.  
37391 44 61 74 61 42 75 66 66 65 72 20 72 6f 6f 74 44  DataBuffer rootD
37392 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 72 6f 6f  ata;      /* roo
37393 74 20 64 61 74 61 20 66 6f 72 20 69 6e 6c 69 6e  t data for inlin
37394 65 2e 20 2a 2f 0a 7d 20 4c 65 61 76 65 73 52 65  e. */.} LeavesRe
37395 61 64 65 72 3b 0a 0a 2f 2a 20 41 63 63 65 73 73  ader;../* Access
37396 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72   the current ter
37397 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m. */.static int
37398 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
37399 6d 42 79 74 65 73 28 4c 65 61 76 65 73 52 65 61  mBytes(LeavesRea
3739a 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
3739b 20 61 73 73 65 72 74 28 20 21 70 52 65 61 64 65   assert( !pReade
3739c 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65 74 75  r->eof );.  retu
3739d 72 6e 20 6c 65 61 66 52 65 61 64 65 72 54 65 72  rn leafReaderTer
3739e 6d 42 79 74 65 73 28 26 70 52 65 61 64 65 72 2d  mBytes(&pReader-
3739f 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a  >leafReader);.}.
373a0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
373a1 72 20 2a 6c 65 61 76 65 73 52 65 61 64 65 72 54  r *leavesReaderT
373a2 65 72 6d 28 4c 65 61 76 65 73 52 65 61 64 65 72  erm(LeavesReader
373a3 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
373a4 73 65 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e  sert( !pReader->
373a5 65 6f 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  eof );.  return 
373a6 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d 28 26  leafReaderTerm(&
373a7 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61  pReader->leafRea
373a8 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 65  der);.}../* Acce
373a9 73 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64  ss the doclist d
373aa 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 72  ata for the curr
373ab 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61  ent term. */.sta
373ac 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65  tic int leavesRe
373ad 61 64 65 72 44 61 74 61 42 79 74 65 73 28 4c 65  aderDataBytes(Le
373ae 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61  avesReader *pRea
373af 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
373b0 21 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29 3b  !pReader->eof );
373b1 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52 65  .  return leafRe
373b2 61 64 65 72 44 61 74 61 42 79 74 65 73 28 26 70  aderDataBytes(&p
373b3 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64  Reader->leafRead
373b4 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f  er);.}.static co
373b5 6e 73 74 20 63 68 61 72 20 2a 6c 65 61 76 65 73  nst char *leaves
373b6 52 65 61 64 65 72 44 61 74 61 28 4c 65 61 76 65  ReaderData(Leave
373b7 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
373b8 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 52  ){.  assert( !pR
373b9 65 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20  eader->eof );.  
373ba 72 65 74 75 72 6e 20 6c 65 61 66 52 65 61 64 65  return leafReade
373bb 72 44 61 74 61 28 26 70 52 65 61 64 65 72 2d 3e  rData(&pReader->
373bc 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a 0a  leafReader);.}..
373bd 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65  static int leave
373be 73 52 65 61 64 65 72 41 74 45 6e 64 28 4c 65 61  sReaderAtEnd(Lea
373bf 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
373c0 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52  er){.  return pR
373c1 65 61 64 65 72 2d 3e 65 6f 66 3b 0a 7d 0a 0a 2f  eader->eof;.}../
373c2 2a 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61  * loadSegmentLea
373c3 76 65 73 28 29 20 6d 61 79 20 6e 6f 74 20 72 65  ves() may not re
373c4 61 64 20 61 6c 6c 20 74 68 65 20 77 61 79 20 74  ad all the way t
373c5 6f 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2c 20 74  o SQLITE_DONE, t
373c6 68 75 73 0a 2a 2a 20 6c 65 61 76 69 6e 67 20 74  hus.** leaving t
373c7 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e  he statement han
373c8 64 6c 65 20 6f 70 65 6e 2c 20 77 68 69 63 68 20  dle open, which 
373c9 6c 6f 63 6b 73 20 74 68 65 20 74 61 62 6c 65 2e  locks the table.
373ca 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
373cb 73 29 20 54 68 69 73 20 22 73 6f 6c 75 74 69 6f  s) This "solutio
373cc 6e 22 20 69 73 20 6e 6f 74 20 73 61 74 69 73 66  n" is not satisf
373cd 61 63 74 6f 72 79 2e 20 20 52 65 61 6c 6c 79 2c  actory.  Really,
373ce 20 74 68 65 72 65 0a 2a 2a 20 73 68 6f 75 6c 64   there.** should
373cf 20 62 65 20 63 68 65 63 6b 2d 69 6e 20 66 75 6e   be check-in fun
373d0 63 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 73 74  ction for all st
373d1 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 73 20  atement handles 
373d2 77 68 69 63 68 0a 2a 2a 20 61 72 72 61 6e 67 65  which.** arrange
373d3 73 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65  s to call sqlite
373d4 33 5f 72 65 73 65 74 28 29 2e 20 20 54 68 69 73  3_reset().  This
373d5 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 77 69 6c   most likely wil
373d6 6c 20 72 65 71 75 69 72 65 0a 2a 2a 20 6d 6f 64  l require.** mod
373d7 69 66 69 63 61 74 69 6f 6e 20 74 6f 20 63 6f 6e  ification to con
373d8 74 72 6f 6c 20 66 6c 6f 77 20 61 6c 6c 20 6f 76  trol flow all ov
373d9 65 72 20 74 68 65 20 70 6c 61 63 65 2c 20 74 68  er the place, th
373da 6f 75 67 68 2c 20 73 6f 20 66 6f 72 20 6e 6f 77  ough, so for now
373db 0a 2a 2a 20 6a 75 73 74 20 70 75 6e 74 2e 0a 2a  .** just punt..*
373dc 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 65 20 74 68  *.** Note the th
373dd 65 20 63 75 72 72 65 6e 74 20 73 79 73 74 65 6d  e current system
373de 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 73 65   assumes that se
373df 67 6d 65 6e 74 20 6d 65 72 67 65 73 20 77 69 6c  gment merges wil
373e0 6c 20 72 75 6e 20 74 6f 0a 2a 2a 20 63 6f 6d 70  l run to.** comp
373e1 6c 65 74 69 6f 6e 2c 20 77 68 69 63 68 20 69 73  letion, which is
373e2 20 77 68 79 20 74 68 69 73 20 70 61 72 74 69 63   why this partic
373e3 75 6c 61 72 20 70 72 6f 62 61 62 6c 79 20 68 61  ular probably ha
373e4 73 6e 27 74 20 61 72 69 73 65 6e 20 69 6e 0a 2a  sn't arisen in.*
373e5 2a 20 74 68 69 73 20 63 61 73 65 2e 20 20 50 72  * this case.  Pr
373e6 6f 62 61 62 6c 79 20 61 20 62 72 69 74 74 6c 65  obably a brittle
373e7 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 2a 2f 0a   assumption..*/.
373e8 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65  static int leave
373e9 73 52 65 61 64 65 72 52 65 73 65 74 28 4c 65 61  sReaderReset(Lea
373ea 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
373eb 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  er){.  return sq
373ec 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 65 61  lite3_reset(pRea
373ed 64 65 72 2d 3e 70 53 74 6d 74 29 3b 0a 7d 0a 0a  der->pStmt);.}..
373ee 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 76  static void leav
373ef 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  esReaderDestroy(
373f0 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
373f1 65 61 64 65 72 29 7b 0a 20 20 2f 2a 20 49 66 20  eader){.  /* If 
373f2 69 64 78 20 69 73 20 2d 31 2c 20 74 68 61 74 20  idx is -1, that 
373f3 6d 65 61 6e 73 20 77 65 27 72 65 20 75 73 69 6e  means we're usin
373f4 67 20 61 20 6e 6f 6e 2d 63 61 63 68 65 64 20 73  g a non-cached s
373f5 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 68 61  tatement.  ** ha
373f6 6e 64 6c 65 20 69 6e 20 74 68 65 20 6f 70 74 69  ndle in the opti
373f7 6d 69 7a 65 28 29 20 63 61 73 65 2c 20 73 6f 20  mize() case, so 
373f8 77 65 20 6e 65 65 64 20 74 6f 20 72 65 6c 65 61  we need to relea
373f9 73 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  se it..  */.  if
373fa 28 20 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  ( pReader->pStmt
373fb 21 3d 4e 55 4c 4c 20 26 26 20 70 52 65 61 64 65  !=NULL && pReade
373fc 72 2d 3e 69 64 78 3d 3d 2d 31 20 29 7b 0a 20 20  r->idx==-1 ){.  
373fd 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
373fe 7a 65 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d  ze(pReader->pStm
373ff 74 29 3b 0a 20 20 7d 0a 20 20 6c 65 61 66 52 65  t);.  }.  leafRe
37400 61 64 65 72 44 65 73 74 72 6f 79 28 26 70 52 65  aderDestroy(&pRe
37401 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72  ader->leafReader
37402 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44  );.  dataBufferD
37403 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d  estroy(&pReader-
37404 3e 72 6f 6f 74 44 61 74 61 29 3b 0a 20 20 53 43  >rootData);.  SC
37405 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72 29 3b  RAMBLE(pReader);
37406 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  .}../* Initializ
37407 65 20 70 52 65 61 64 65 72 20 77 69 74 68 20 74  e pReader with t
37408 68 65 20 67 69 76 65 6e 20 72 6f 6f 74 20 64 61  he given root da
37409 74 61 20 28 69 66 20 69 53 74 61 72 74 42 6c 6f  ta (if iStartBlo
3740a 63 6b 69 64 3d 3d 30 0a 2a 2a 20 74 68 65 20 6c  ckid==0.** the l
3740b 65 61 66 20 64 61 74 61 20 77 61 73 20 65 6e 74  eaf data was ent
3740c 69 72 65 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20  irely contained 
3740d 69 6e 20 74 68 65 20 72 6f 6f 74 29 2c 20 6f 72  in the root), or
3740e 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 74 72   from the.** str
3740f 65 61 6d 20 6f 66 20 62 6c 6f 63 6b 73 20 62 65  eam of blocks be
37410 74 77 65 65 6e 20 69 53 74 61 72 74 42 6c 6f 63  tween iStartBloc
37411 6b 69 64 20 61 6e 64 20 69 45 6e 64 42 6c 6f 63  kid and iEndBloc
37412 6b 69 64 2c 20 69 6e 63 6c 75 73 69 76 65 2e 0a  kid, inclusive..
37413 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65  */.static int le
37414 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 66  avesReaderInit(f
37415 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
37416 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37417 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
37418 20 69 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20   idx,.          
37419 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3741a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
3741b 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 0a 20 20  StartBlockid,.  
3741c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3741d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3741e 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b  _int64 iEndBlock
3741f 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  id,.            
37420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37421 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 6f 6f  const char *pRoo
37422 74 44 61 74 61 2c 20 69 6e 74 20 6e 52 6f 6f 74  tData, int nRoot
37423 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
37424 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37425 20 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a    LeavesReader *
37426 70 52 65 61 64 65 72 29 7b 0a 20 20 43 4c 45 41  pReader){.  CLEA
37427 52 28 70 52 65 61 64 65 72 29 3b 0a 20 20 70 52  R(pReader);.  pR
37428 65 61 64 65 72 2d 3e 69 64 78 20 3d 20 69 64 78  eader->idx = idx
37429 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  ;..  dataBufferI
3742a 6e 69 74 28 26 70 52 65 61 64 65 72 2d 3e 72 6f  nit(&pReader->ro
3742b 6f 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 69 66  otData, 0);.  if
3742c 28 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3d  ( iStartBlockid=
3742d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 45 6e 74  =0 ){.    /* Ent
3742e 69 72 65 20 6c 65 61 66 20 6c 65 76 65 6c 20 66  ire leaf level f
3742f 69 74 20 69 6e 20 72 6f 6f 74 20 64 61 74 61 2e  it in root data.
37430 20 2a 2f 0a 20 20 20 20 64 61 74 61 42 75 66 66   */.    dataBuff
37431 65 72 52 65 70 6c 61 63 65 28 26 70 52 65 61 64  erReplace(&pRead
37432 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2c 20 70 52  er->rootData, pR
37433 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61  ootData, nRootDa
37434 74 61 29 3b 0a 20 20 20 20 6c 65 61 66 52 65 61  ta);.    leafRea
37435 64 65 72 49 6e 69 74 28 70 52 65 61 64 65 72 2d  derInit(pReader-
37436 3e 72 6f 6f 74 44 61 74 61 2e 70 44 61 74 61 2c  >rootData.pData,
37437 20 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61   pReader->rootDa
37438 74 61 2e 6e 44 61 74 61 2c 0a 20 20 20 20 20 20  ta.nData,.      
37439 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70 52               &pR
3743a 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65  eader->leafReade
3743b 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r);.  }else{.   
3743c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
3743d 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  ;.    int rc = s
3743e 71 6c 5f 67 65 74 5f 6c 65 61 66 5f 73 74 61 74  ql_get_leaf_stat
3743f 65 6d 65 6e 74 28 76 2c 20 69 64 78 2c 20 26 73  ement(v, idx, &s
37440 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
37441 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
37442 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d 20  n rc;..    rc = 
37443 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
37444 36 34 28 73 2c 20 31 2c 20 69 53 74 61 72 74 42  64(s, 1, iStartB
37445 6c 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66 28  lockid);.    if(
37446 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
37447 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
37448 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
37449 6e 64 5f 69 6e 74 36 34 28 73 2c 20 32 2c 20 69  nd_int64(s, 2, i
3744a 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 20  EndBlockid);.   
3744b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3744c 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
3744d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
3744e 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 20 20 69  3_step(s);.    i
3744f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
37450 4e 45 20 29 7b 0a 20 20 20 20 20 20 70 52 65 61  NE ){.      pRea
37451 64 65 72 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20  der->eof = 1;.  
37452 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
37453 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E_OK;.    }.    
37454 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
37455 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OW ) return rc;.
37456 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 53  .    pReader->pS
37457 74 6d 74 20 3d 20 73 3b 0a 20 20 20 20 6c 65 61  tmt = s;.    lea
37458 66 52 65 61 64 65 72 49 6e 69 74 28 73 71 6c 69  fReaderInit(sqli
37459 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
3745a 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20  pReader->pStmt, 
3745b 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0),.            
3745c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63         sqlite3_c
3745d 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 52 65 61  olumn_bytes(pRea
3745e 64 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a  der->pStmt, 0),.
3745f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37460 20 20 20 26 70 52 65 61 64 65 72 2d 3e 6c 65 61     &pReader->lea
37461 66 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20  fReader);.  }.  
37462 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
37463 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 74 68 65  ;.}../* Step the
37464 20 63 75 72 72 65 6e 74 20 6c 65 61 66 20 66 6f   current leaf fo
37465 72 77 61 72 64 20 74 6f 20 74 68 65 20 6e 65 78  rward to the nex
37466 74 20 74 65 72 6d 2e 20 20 49 66 20 77 65 20 72  t term.  If we r
37467 65 61 63 68 20 74 68 65 0a 2a 2a 20 65 6e 64 20  each the.** end 
37468 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  of the current l
37469 65 61 66 2c 20 73 74 65 70 20 66 6f 72 77 61 72  eaf, step forwar
3746a 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 65  d to the next le
3746b 61 66 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61  af block..*/.sta
3746c 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65  tic int leavesRe
3746d 61 64 65 72 53 74 65 70 28 66 75 6c 6c 74 65 78  aderStep(fulltex
3746e 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 76 65  t_vtab *v, Leave
3746f 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
37470 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 6c 65  ){.  assert( !le
37471 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28  avesReaderAtEnd(
37472 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 6c 65  pReader) );.  le
37473 61 66 52 65 61 64 65 72 53 74 65 70 28 26 70 52  afReaderStep(&pR
37474 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65  eader->leafReade
37475 72 29 3b 0a 0a 20 20 69 66 28 20 6c 65 61 66 52  r);..  if( leafR
37476 65 61 64 65 72 41 74 45 6e 64 28 26 70 52 65 61  eaderAtEnd(&pRea
37477 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29  der->leafReader)
37478 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a   ){.    int rc;.
37479 20 20 20 20 69 66 28 20 70 52 65 61 64 65 72 2d      if( pReader-
3747a 3e 72 6f 6f 74 44 61 74 61 2e 70 44 61 74 61 20  >rootData.pData 
3747b 29 7b 0a 20 20 20 20 20 20 70 52 65 61 64 65 72  ){.      pReader
3747c 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20  ->eof = 1;.     
3747d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3747e 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  K;.    }.    rc 
3747f 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  = sqlite3_step(p
37480 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 29 3b 0a  Reader->pStmt);.
37481 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
37482 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20  TE_ROW ){.      
37483 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 3d 20 31  pReader->eof = 1
37484 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
37485 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f  c==SQLITE_DONE ?
37486 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 3b   SQLITE_OK : rc;
37487 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 61 66 52  .    }.    leafR
37488 65 61 64 65 72 44 65 73 74 72 6f 79 28 26 70 52  eaderDestroy(&pR
37489 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65  eader->leafReade
3748a 72 29 3b 0a 20 20 20 20 6c 65 61 66 52 65 61 64  r);.    leafRead
3748b 65 72 49 6e 69 74 28 73 71 6c 69 74 65 33 5f 63  erInit(sqlite3_c
3748c 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 52 65 61 64  olumn_blob(pRead
3748d 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a 20  er->pStmt, 0),. 
3748e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3748f 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
37490 5f 62 79 74 65 73 28 70 52 65 61 64 65 72 2d 3e  _bytes(pReader->
37491 70 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20  pStmt, 0),.     
37492 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
37493 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64  Reader->leafRead
37494 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  er);.  }.  retur
37495 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
37496 2f 2a 20 4f 72 64 65 72 20 4c 65 61 76 65 73 52  /* Order LeavesR
37497 65 61 64 65 72 73 20 62 79 20 74 68 65 69 72 20  eaders by their 
37498 74 65 72 6d 2c 20 69 67 6e 6f 72 69 6e 67 20 69  term, ignoring i
37499 64 78 2e 20 20 52 65 61 64 65 72 73 20 61 74 20  dx.  Readers at 
3749a 65 6f 66 0a 2a 2a 20 61 6c 77 61 79 73 20 73 6f  eof.** always so
3749b 72 74 20 74 6f 20 74 68 65 20 65 6e 64 2e 0a 2a  rt to the end..*
3749c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
3749d 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70  vesReaderTermCmp
3749e 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c  (LeavesReader *l
3749f 72 31 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72  r1, LeavesReader
374a0 20 2a 6c 72 32 29 7b 0a 20 20 69 66 28 20 6c 65   *lr2){.  if( le
374a1 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28  avesReaderAtEnd(
374a2 6c 72 31 29 20 29 7b 0a 20 20 20 20 69 66 28 20  lr1) ){.    if( 
374a3 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e  leavesReaderAtEn
374a4 64 28 6c 72 32 29 20 29 20 72 65 74 75 72 6e 20  d(lr2) ) return 
374a5 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  0;.    return 1;
374a6 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 61 76 65  .  }.  if( leave
374a7 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c 72 32  sReaderAtEnd(lr2
374a8 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 0a  ) ) return -1;..
374a9 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52 65 61    return leafRea
374aa 64 65 72 54 65 72 6d 43 6d 70 28 26 6c 72 31 2d  derTermCmp(&lr1-
374ab 3e 6c 65 61 66 52 65 61 64 65 72 2c 0a 20 20 20  >leafReader,.   
374ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
374ad 20 20 20 20 20 20 20 20 6c 65 61 76 65 73 52 65          leavesRe
374ae 61 64 65 72 54 65 72 6d 28 6c 72 32 29 2c 20 6c  aderTerm(lr2), l
374af 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42  eavesReaderTermB
374b0 79 74 65 73 28 6c 72 32 29 2c 0a 20 20 20 20 20  ytes(lr2),.     
374b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
374b2 20 20 20 20 20 20 30 29 3b 0a 7d 0a 0a 2f 2a 20        0);.}../* 
374b3 53 69 6d 69 6c 61 72 20 74 6f 20 6c 65 61 76 65  Similar to leave
374b4 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 29  sReaderTermCmp()
374b5 2c 20 77 69 74 68 20 61 64 64 69 74 69 6f 6e 61  , with additiona
374b6 6c 20 6f 72 64 65 72 69 6e 67 20 62 79 20 69 64  l ordering by id
374b7 78 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6f 6c 64  x.** so that old
374b8 65 72 20 73 65 67 6d 65 6e 74 73 20 73 6f 72 74  er segments sort
374b9 20 62 65 66 6f 72 65 20 6e 65 77 65 72 20 73 65   before newer se
374ba 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69  gments..*/.stati
374bb 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
374bc 65 72 43 6d 70 28 4c 65 61 76 65 73 52 65 61 64  erCmp(LeavesRead
374bd 65 72 20 2a 6c 72 31 2c 20 4c 65 61 76 65 73 52  er *lr1, LeavesR
374be 65 61 64 65 72 20 2a 6c 72 32 29 7b 0a 20 20 69  eader *lr2){.  i
374bf 6e 74 20 63 20 3d 20 6c 65 61 76 65 73 52 65 61  nt c = leavesRea
374c0 64 65 72 54 65 72 6d 43 6d 70 28 6c 72 31 2c 20  derTermCmp(lr1, 
374c1 6c 72 32 29 3b 0a 20 20 69 66 28 20 63 21 3d 30  lr2);.  if( c!=0
374c2 20 29 20 72 65 74 75 72 6e 20 63 3b 0a 20 20 72   ) return c;.  r
374c3 65 74 75 72 6e 20 6c 72 31 2d 3e 69 64 78 2d 6c  eturn lr1->idx-l
374c4 72 32 2d 3e 69 64 78 3b 0a 7d 0a 0a 2f 2a 20 41  r2->idx;.}../* A
374c5 73 73 75 6d 65 20 74 68 61 74 20 70 4c 72 5b 31  ssume that pLr[1
374c6 5d 2e 2e 70 4c 72 5b 6e 4c 72 5d 20 61 72 65 20  ]..pLr[nLr] are 
374c7 73 6f 72 74 65 64 2e 20 20 42 75 62 62 6c 65 20  sorted.  Bubble 
374c8 70 4c 72 5b 30 5d 20 69 6e 74 6f 20 69 74 73 0a  pLr[0] into its.
374c9 2a 2a 20 73 6f 72 74 65 64 20 70 6f 73 69 74 69  ** sorted positi
374ca 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
374cb 69 64 20 6c 65 61 76 65 73 52 65 61 64 65 72 52  id leavesReaderR
374cc 65 6f 72 64 65 72 28 4c 65 61 76 65 73 52 65 61  eorder(LeavesRea
374cd 64 65 72 20 2a 70 4c 72 2c 20 69 6e 74 20 6e 4c  der *pLr, int nL
374ce 72 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e 4c 72  r){.  while( nLr
374cf 3e 31 20 26 26 20 6c 65 61 76 65 73 52 65 61 64  >1 && leavesRead
374d0 65 72 43 6d 70 28 70 4c 72 2c 20 70 4c 72 2b 31  erCmp(pLr, pLr+1
374d1 29 3e 30 20 29 7b 0a 20 20 20 20 4c 65 61 76 65  )>0 ){.    Leave
374d2 73 52 65 61 64 65 72 20 74 6d 70 20 3d 20 70 4c  sReader tmp = pL
374d3 72 5b 30 5d 3b 0a 20 20 20 20 70 4c 72 5b 30 5d  r[0];.    pLr[0]
374d4 20 3d 20 70 4c 72 5b 31 5d 3b 0a 20 20 20 20 70   = pLr[1];.    p
374d5 4c 72 5b 31 5d 20 3d 20 74 6d 70 3b 0a 20 20 20  Lr[1] = tmp;.   
374d6 20 6e 4c 72 2d 2d 3b 0a 20 20 20 20 70 4c 72 2b   nLr--;.    pLr+
374d7 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 49 6e 69  +;.  }.}../* Ini
374d8 74 69 61 6c 69 7a 65 73 20 70 52 65 61 64 65 72  tializes pReader
374d9 73 20 77 69 74 68 20 74 68 65 20 73 65 67 6d 65  s with the segme
374da 6e 74 73 20 66 72 6f 6d 20 6c 65 76 65 6c 20 69  nts from level i
374db 4c 65 76 65 6c 2c 20 72 65 74 75 72 6e 69 6e 67  Level, returning
374dc 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
374dd 66 20 73 65 67 6d 65 6e 74 73 20 69 6e 20 2a 70  f segments in *p
374de 69 52 65 61 64 65 72 73 2e 20 20 4c 65 61 76 65  iReaders.  Leave
374df 73 20 70 52 65 61 64 65 72 73 20 69 6e 20 73 6f  s pReaders in so
374e0 72 74 65 64 0a 2a 2a 20 6f 72 64 65 72 2e 0a 2a  rted.** order..*
374e1 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
374e2 76 65 73 52 65 61 64 65 72 73 49 6e 69 74 28 66  vesReadersInit(f
374e3 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
374e4 20 69 6e 74 20 69 4c 65 76 65 6c 2c 0a 20 20 20   int iLevel,.   
374e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
374e6 20 20 20 20 20 20 20 20 20 20 4c 65 61 76 65 73            Leaves
374e7 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 73  Reader *pReaders
374e8 2c 20 69 6e 74 20 2a 70 69 52 65 61 64 65 72 73  , int *piReaders
374e9 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
374ea 74 20 2a 73 3b 0a 20 20 69 6e 74 20 69 2c 20 72  t *s;.  int i, r
374eb 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  c = sql_get_stat
374ec 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f  ement(v, SEGDIR_
374ed 53 45 4c 45 43 54 5f 4c 45 56 45 4c 5f 53 54 4d  SELECT_LEVEL_STM
374ee 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
374ef 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
374f0 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
374f1 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
374f2 74 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b  t(s, 1, iLevel);
374f3 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
374f4 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
374f5 3b 0a 0a 20 20 69 20 3d 20 30 3b 0a 20 20 77 68  ;..  i = 0;.  wh
374f6 69 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74  ile( (rc = sqlit
374f7 65 33 5f 73 74 65 70 28 73 29 29 3d 3d 53 51 4c  e3_step(s))==SQL
374f8 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 73  ITE_ROW ){.    s
374f9 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61  qlite_int64 iSta
374fa 72 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  rt = sqlite3_col
374fb 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 30 29 3b  umn_int64(s, 0);
374fc 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
374fd 34 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33  4 iEnd = sqlite3
374fe 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c  _column_int64(s,
374ff 20 31 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63   1);.    const c
37500 68 61 72 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d  har *pRootData =
37501 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
37502 62 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20  blob(s, 2);.    
37503 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d 20  int nRootData = 
37504 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
37505 79 74 65 73 28 73 2c 20 32 29 3b 0a 0a 20 20 20  ytes(s, 2);..   
37506 20 61 73 73 65 72 74 28 20 69 3c 4d 45 52 47 45   assert( i<MERGE
37507 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 20 20 72 63  _COUNT );.    rc
37508 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 49   = leavesReaderI
37509 6e 69 74 28 76 2c 20 69 2c 20 69 53 74 61 72 74  nit(v, i, iStart
3750a 2c 20 69 45 6e 64 2c 20 70 52 6f 6f 74 44 61 74  , iEnd, pRootDat
3750b 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20 20  a, nRootData,.  
3750c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3750d 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65 72          &pReader
3750e 73 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72  s[i]);.    if( r
3750f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62  c!=SQLITE_OK ) b
37510 72 65 61 6b 3b 0a 0a 20 20 20 20 69 2b 2b 3b 0a  reak;..    i++;.
37511 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51    }.  if( rc!=SQ
37512 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
37513 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29 7b   while( i-->0 ){
37514 0a 20 20 20 20 20 20 6c 65 61 76 65 73 52 65 61  .      leavesRea
37515 64 65 72 44 65 73 74 72 6f 79 28 26 70 52 65 61  derDestroy(&pRea
37516 64 65 72 73 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  ders[i]);.    }.
37517 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
37518 20 7d 0a 0a 20 20 2a 70 69 52 65 61 64 65 72 73   }..  *piReaders
37519 20 3d 20 69 3b 0a 0a 20 20 2f 2a 20 4c 65 61 76   = i;..  /* Leav
3751a 65 20 6f 75 72 20 72 65 73 75 6c 74 73 20 73 6f  e our results so
3751b 72 74 65 64 20 62 79 20 74 65 72 6d 2c 20 74 68  rted by term, th
3751c 65 6e 20 61 67 65 2e 20 2a 2f 0a 20 20 77 68 69  en age. */.  whi
3751d 6c 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 6c  le( i-- ){.    l
3751e 65 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64  eavesReaderReord
3751f 65 72 28 70 52 65 61 64 65 72 73 2b 69 2c 20 2a  er(pReaders+i, *
37520 70 69 52 65 61 64 65 72 73 2d 69 29 3b 0a 20 20  piReaders-i);.  
37521 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
37522 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 65 72 67  E_OK;.}../* Merg
37523 65 20 64 6f 63 6c 69 73 74 73 20 66 72 6f 6d 20  e doclists from 
37524 70 52 65 61 64 65 72 73 5b 6e 52 65 61 64 65 72  pReaders[nReader
37525 73 5d 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  s] into a single
37526 20 64 6f 63 6c 69 73 74 2c 20 77 68 69 63 68 0a   doclist, which.
37527 2a 2a 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  ** is written to
37528 20 70 57 72 69 74 65 72 2e 20 20 41 73 73 75 6d   pWriter.  Assum
37529 65 73 20 70 52 65 61 64 65 72 73 20 69 73 20 6f  es pReaders is o
3752a 72 64 65 72 65 64 20 6f 6c 64 65 73 74 20 74 6f  rdered oldest to
3752b 0a 2a 2a 20 6e 65 77 65 73 74 2e 0a 2a 2f 0a 2f  .** newest..*/./
3752c 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f  * TODO(shess) Co
3752d 6e 73 69 64 65 72 20 70 75 74 74 69 6e 67 20 74  nsider putting t
3752e 68 69 73 20 69 6e 6c 69 6e 65 20 69 6e 20 73 65  his inline in se
3752f 67 6d 65 6e 74 4d 65 72 67 65 28 29 2e 20 2a 2f  gmentMerge(). */
37530 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
37531 65 73 52 65 61 64 65 72 73 4d 65 72 67 65 28 66  esReadersMerge(f
37532 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
37533 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37534 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c                 L
37535 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65  eavesReader *pRe
37536 61 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61 64  aders, int nRead
37537 65 72 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ers,.           
37538 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37539 20 20 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70     LeafWriter *p
3753a 57 72 69 74 65 72 29 7b 0a 20 20 44 4c 52 65 61  Writer){.  DLRea
3753b 64 65 72 20 64 6c 52 65 61 64 65 72 73 5b 4d 45  der dlReaders[ME
3753c 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 63 6f  RGE_COUNT];.  co
3753d 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20  nst char *pTerm 
3753e 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  = leavesReaderTe
3753f 72 6d 28 70 52 65 61 64 65 72 73 29 3b 0a 20 20  rm(pReaders);.  
37540 69 6e 74 20 69 2c 20 6e 54 65 72 6d 20 3d 20 6c  int i, nTerm = l
37541 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42  eavesReaderTermB
37542 79 74 65 73 28 70 52 65 61 64 65 72 73 29 3b 0a  ytes(pReaders);.
37543 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 61 64  .  assert( nRead
37544 65 72 73 3c 3d 4d 45 52 47 45 5f 43 4f 55 4e 54  ers<=MERGE_COUNT
37545 20 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20   );..  for(i=0; 
37546 69 3c 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29  i<nReaders; i++)
37547 7b 0a 20 20 20 20 64 6c 72 49 6e 69 74 28 26 64  {.    dlrInit(&d
37548 6c 52 65 61 64 65 72 73 5b 69 5d 2c 20 44 4c 5f  lReaders[i], DL_
37549 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20 20  DEFAULT,.       
3754a 20 20 20 20 20 6c 65 61 76 65 73 52 65 61 64 65       leavesReade
3754b 72 44 61 74 61 28 70 52 65 61 64 65 72 73 2b 69  rData(pReaders+i
3754c 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c  ),.            l
3754d 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42  eavesReaderDataB
3754e 79 74 65 73 28 70 52 65 61 64 65 72 73 2b 69 29  ytes(pReaders+i)
3754f 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
37550 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 4d   leafWriterStepM
37551 65 72 67 65 28 76 2c 20 70 57 72 69 74 65 72 2c  erge(v, pWriter,
37552 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 64   pTerm, nTerm, d
37553 6c 52 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65  lReaders, nReade
37554 72 73 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61  rs);.}../* Forwa
37555 72 64 20 72 65 66 20 64 75 65 20 74 6f 20 6d 75  rd ref due to mu
37556 74 75 61 6c 20 72 65 63 75 72 73 69 6f 6e 20 77  tual recursion w
37557 69 74 68 20 73 65 67 64 69 72 4e 65 78 74 49 6e  ith segdirNextIn
37558 64 65 78 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63  dex(). */.static
37559 20 69 6e 74 20 73 65 67 6d 65 6e 74 4d 65 72 67   int segmentMerg
3755a 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
3755b 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29 3b  *v, int iLevel);
3755c 0a 0a 2f 2a 20 50 75 74 20 74 68 65 20 6e 65 78  ../* Put the nex
3755d 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 64 65  t available inde
3755e 78 20 61 74 20 69 4c 65 76 65 6c 20 69 6e 74 6f  x at iLevel into
3755f 20 2a 70 69 64 78 2e 20 20 49 66 20 69 4c 65 76   *pidx.  If iLev
37560 65 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 61  el.** already ha
37561 73 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65  s MERGE_COUNT se
37562 67 6d 65 6e 74 73 2c 20 74 68 65 79 20 61 72 65  gments, they are
37563 20 6d 65 72 67 65 64 20 74 6f 20 61 20 68 69 67   merged to a hig
37564 68 65 72 0a 2a 2a 20 6c 65 76 65 6c 20 74 6f 20  her.** level to 
37565 6d 61 6b 65 20 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74  make room..*/.st
37566 61 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 4e  atic int segdirN
37567 65 78 74 49 6e 64 65 78 28 66 75 6c 6c 74 65 78  extIndex(fulltex
37568 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69  t_vtab *v, int i
37569 4c 65 76 65 6c 2c 20 69 6e 74 20 2a 70 69 64 78  Level, int *pidx
3756a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 65  ){.  int rc = se
3756b 67 64 69 72 5f 6d 61 78 5f 69 6e 64 65 78 28 76  gdir_max_index(v
3756c 2c 20 69 4c 65 76 65 6c 2c 20 70 69 64 78 29 3b  , iLevel, pidx);
3756d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
3756e 45 5f 44 4f 4e 45 20 29 7b 20 20 20 20 20 20 20  E_DONE ){       
3756f 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 73 65 67         /* No seg
37570 6d 65 6e 74 73 20 61 74 20 69 4c 65 76 65 6c 2e  ments at iLevel.
37571 20 2a 2f 0a 20 20 20 20 2a 70 69 64 78 20 3d 20   */.    *pidx = 
37572 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72  0;.  }else if( r
37573 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
37574 0a 20 20 20 20 69 66 28 20 2a 70 69 64 78 3d 3d  .    if( *pidx==
37575 28 4d 45 52 47 45 5f 43 4f 55 4e 54 2d 31 29 20  (MERGE_COUNT-1) 
37576 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65  ){.      rc = se
37577 67 6d 65 6e 74 4d 65 72 67 65 28 76 2c 20 69 4c  gmentMerge(v, iL
37578 65 76 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28  evel);.      if(
37579 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3757a 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
3757b 20 20 2a 70 69 64 78 20 3d 20 30 3b 0a 20 20 20    *pidx = 0;.   
3757c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 2a   }else{.      (*
3757d 70 69 64 78 29 2b 2b 3b 0a 20 20 20 20 7d 0a 20  pidx)++;.    }. 
3757e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
3757f 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74  rn rc;.  }.  ret
37580 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
37581 0a 0a 2f 2a 20 4d 65 72 67 65 20 4d 45 52 47 45  ../* Merge MERGE
37582 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 20  _COUNT segments 
37583 61 74 20 69 4c 65 76 65 6c 20 69 6e 74 6f 20 61  at iLevel into a
37584 20 6e 65 77 20 73 65 67 6d 65 6e 74 20 61 74 0a   new segment at.
37585 2a 2a 20 69 4c 65 76 65 6c 2b 31 2e 20 20 49 66  ** iLevel+1.  If
37586 20 69 4c 65 76 65 6c 2b 31 20 69 73 20 61 6c 72   iLevel+1 is alr
37587 65 61 64 79 20 66 75 6c 6c 20 6f 66 20 73 65 67  eady full of seg
37588 6d 65 6e 74 73 2c 20 74 68 6f 73 65 20 77 69 6c  ments, those wil
37589 6c 20 62 65 0a 2a 2a 20 6d 65 72 67 65 64 20 74  l be.** merged t
3758a 6f 20 6d 61 6b 65 20 72 6f 6f 6d 2e 0a 2a 2f 0a  o make room..*/.
3758b 73 74 61 74 69 63 20 69 6e 74 20 73 65 67 6d 65  static int segme
3758c 6e 74 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74  ntMerge(fulltext
3758d 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
3758e 65 76 65 6c 29 7b 0a 20 20 4c 65 61 66 57 72 69  evel){.  LeafWri
3758f 74 65 72 20 77 72 69 74 65 72 3b 0a 20 20 4c 65  ter writer;.  Le
37590 61 76 65 73 52 65 61 64 65 72 20 6c 72 73 5b 4d  avesReader lrs[M
37591 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 69  ERGE_COUNT];.  i
37592 6e 74 20 69 2c 20 72 63 2c 20 69 64 78 20 3d 20  nt i, rc, idx = 
37593 30 3b 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69  0;..  /* Determi
37594 6e 65 20 74 68 65 20 6e 65 78 74 20 61 76 61 69  ne the next avai
37595 6c 61 62 6c 65 20 73 65 67 6d 65 6e 74 20 69 6e  lable segment in
37596 64 65 78 20 61 74 20 74 68 65 20 6e 65 78 74 20  dex at the next 
37597 6c 65 76 65 6c 2c 0a 20 20 2a 2a 20 6d 65 72 67  level,.  ** merg
37598 69 6e 67 20 61 73 20 6e 65 63 65 73 73 61 72 79  ing as necessary
37599 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 65  ..  */.  rc = se
3759a 67 64 69 72 4e 65 78 74 49 6e 64 65 78 28 76 2c  gdirNextIndex(v,
3759b 20 69 4c 65 76 65 6c 2b 31 2c 20 26 69 64 78 29   iLevel+1, &idx)
3759c 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
3759d 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
3759e 63 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68  c;..  /* TODO(sh
3759f 65 73 73 29 20 54 68 69 73 20 61 73 73 75 6d 65  ess) This assume
375a0 73 20 74 68 61 74 20 77 65 27 6c 6c 20 61 6c 77  s that we'll alw
375a1 61 79 73 20 73 65 65 20 65 78 61 63 74 6c 79 0a  ays see exactly.
375a2 20 20 2a 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54    ** MERGE_COUNT
375a3 20 73 65 67 6d 65 6e 74 73 20 74 6f 20 6d 65 72   segments to mer
375a4 67 65 20 61 74 20 61 20 67 69 76 65 6e 20 6c 65  ge at a given le
375a5 76 65 6c 2e 20 20 54 68 61 74 20 77 69 6c 6c 20  vel.  That will 
375a6 62 65 0a 20 20 2a 2a 20 62 72 6f 6b 65 6e 20 69  be.  ** broken i
375a7 66 20 77 65 20 61 6c 6c 6f 77 20 74 68 65 20 64  f we allow the d
375a8 65 76 65 6c 6f 70 65 72 20 74 6f 20 72 65 71 75  eveloper to requ
375a9 65 73 74 20 70 72 65 65 6d 70 74 69 76 65 20 6f  est preemptive o
375aa 72 0a 20 20 2a 2a 20 64 65 66 65 72 72 65 64 20  r.  ** deferred 
375ab 6d 65 72 67 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20  merging..  */.  
375ac 6d 65 6d 73 65 74 28 26 6c 72 73 2c 20 27 5c 30  memset(&lrs, '\0
375ad 27 2c 20 73 69 7a 65 6f 66 28 6c 72 73 29 29 3b  ', sizeof(lrs));
375ae 0a 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65  .  rc = leavesRe
375af 61 64 65 72 73 49 6e 69 74 28 76 2c 20 69 4c 65  adersInit(v, iLe
375b0 76 65 6c 2c 20 6c 72 73 2c 20 26 69 29 3b 0a 20  vel, lrs, &i);. 
375b1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
375b2 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
375b3 20 20 61 73 73 65 72 74 28 20 69 3d 3d 4d 45 52    assert( i==MER
375b4 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 0a 20 20 6c  GE_COUNT );..  l
375b5 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 69 4c  eafWriterInit(iL
375b6 65 76 65 6c 2b 31 2c 20 69 64 78 2c 20 26 77 72  evel+1, idx, &wr
375b7 69 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 53 69 6e  iter);..  /* Sin
375b8 63 65 20 6c 65 61 76 65 73 52 65 61 64 65 72 52  ce leavesReaderR
375b9 65 6f 72 64 65 72 28 29 20 70 75 73 68 65 73 20  eorder() pushes 
375ba 72 65 61 64 65 72 73 20 61 74 20 65 6f 66 20 74  readers at eof t
375bb 6f 20 74 68 65 20 65 6e 64 2c 0a 20 20 2a 2a 20  o the end,.  ** 
375bc 77 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 72  when the first r
375bd 65 61 64 65 72 20 69 73 20 65 6d 70 74 79 2c 20  eader is empty, 
375be 61 6c 6c 20 77 69 6c 6c 20 62 65 20 65 6d 70 74  all will be empt
375bf 79 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  y..  */.  while(
375c0 20 21 6c 65 61 76 65 73 52 65 61 64 65 72 41 74   !leavesReaderAt
375c1 45 6e 64 28 6c 72 73 29 20 29 7b 0a 20 20 20 20  End(lrs) ){.    
375c2 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
375c3 77 20 6d 61 6e 79 20 72 65 61 64 65 72 73 20 73  w many readers s
375c4 68 61 72 65 20 74 68 65 69 72 20 6e 65 78 74 20  hare their next 
375c5 74 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 66 6f 72  term. */.    for
375c6 28 69 3d 31 3b 20 69 3c 4d 45 52 47 45 5f 43 4f  (i=1; i<MERGE_CO
375c7 55 4e 54 20 26 26 20 21 6c 65 61 76 65 73 52 65  UNT && !leavesRe
375c8 61 64 65 72 41 74 45 6e 64 28 6c 72 73 2b 69 29  aderAtEnd(lrs+i)
375c9 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
375ca 28 20 30 21 3d 6c 65 61 76 65 73 52 65 61 64 65  ( 0!=leavesReade
375cb 72 54 65 72 6d 43 6d 70 28 6c 72 73 2c 20 6c 72  rTermCmp(lrs, lr
375cc 73 2b 69 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  s+i) ) break;.  
375cd 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 6c 65    }..    rc = le
375ce 61 76 65 73 52 65 61 64 65 72 73 4d 65 72 67 65  avesReadersMerge
375cf 28 76 2c 20 6c 72 73 2c 20 69 2c 20 26 77 72 69  (v, lrs, i, &wri
375d0 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ter);.    if( rc
375d1 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
375d2 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 2f 2a 20  to err;..    /* 
375d3 53 74 65 70 20 66 6f 72 77 61 72 64 20 74 68 6f  Step forward tho
375d4 73 65 20 74 68 61 74 20 77 65 72 65 20 6d 65 72  se that were mer
375d5 67 65 64 2e 20 2a 2f 0a 20 20 20 20 77 68 69 6c  ged. */.    whil
375d6 65 28 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20  e( i-->0 ){.    
375d7 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61    rc = leavesRea
375d8 64 65 72 53 74 65 70 28 76 2c 20 6c 72 73 2b 69  derStep(v, lrs+i
375d9 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
375da 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
375db 6f 20 65 72 72 3b 0a 0a 20 20 20 20 20 20 2f 2a  o err;..      /*
375dc 20 52 65 6f 72 64 65 72 20 62 79 20 74 65 72 6d   Reorder by term
375dd 2c 20 74 68 65 6e 20 62 79 20 61 67 65 2e 20 2a  , then by age. *
375de 2f 0a 20 20 20 20 20 20 6c 65 61 76 65 73 52 65  /.      leavesRe
375df 61 64 65 72 52 65 6f 72 64 65 72 28 6c 72 73 2b  aderReorder(lrs+
375e0 69 2c 20 4d 45 52 47 45 5f 43 4f 55 4e 54 2d 69  i, MERGE_COUNT-i
375e1 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
375e2 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 45 52 47 45  for(i=0; i<MERGE
375e3 5f 43 4f 55 4e 54 3b 20 69 2b 2b 29 7b 0a 20 20  _COUNT; i++){.  
375e4 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65    leavesReaderDe
375e5 73 74 72 6f 79 28 26 6c 72 73 5b 69 5d 29 3b 0a  stroy(&lrs[i]);.
375e6 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6c 65 61 66    }..  rc = leaf
375e7 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76  WriterFinalize(v
375e8 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20 6c 65  , &writer);.  le
375e9 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79 28  afWriterDestroy(
375ea 26 77 72 69 74 65 72 29 3b 0a 20 20 69 66 28 20  &writer);.  if( 
375eb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
375ec 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
375ed 20 44 65 6c 65 74 65 20 74 68 65 20 6d 65 72 67   Delete the merg
375ee 65 64 20 73 65 67 6d 65 6e 74 20 64 61 74 61 2e  ed segment data.
375ef 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 73 65 67   */.  return seg
375f0 64 69 72 5f 64 65 6c 65 74 65 28 76 2c 20 69 4c  dir_delete(v, iL
375f1 65 76 65 6c 29 3b 0a 0a 20 65 72 72 3a 0a 20 20  evel);.. err:.  
375f2 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 45 52 47 45  for(i=0; i<MERGE
375f3 5f 43 4f 55 4e 54 3b 20 69 2b 2b 29 7b 0a 20 20  _COUNT; i++){.  
375f4 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65    leavesReaderDe
375f5 73 74 72 6f 79 28 26 6c 72 73 5b 69 5d 29 3b 0a  stroy(&lrs[i]);.
375f6 20 20 7d 0a 20 20 6c 65 61 66 57 72 69 74 65 72    }.  leafWriter
375f7 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
375f8 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
375f9 0a 0a 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20  ../* Accumulate 
375fa 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 2a 61 63  the union of *ac
375fb 63 20 61 6e 64 20 2a 70 44 61 74 61 20 69 6e 74  c and *pData int
375fc 6f 20 2a 61 63 63 2e 20 2a 2f 0a 73 74 61 74 69  o *acc. */.stati
375fd 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 41 63  c void docListAc
375fe 63 75 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 44 61  cumulateUnion(Da
375ff 74 61 42 75 66 66 65 72 20 2a 61 63 63 2c 0a 20  taBuffer *acc,. 
37600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37601 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37602 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44    const char *pD
37603 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 20  ata, int nData) 
37604 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74  {.  DataBuffer t
37605 6d 70 20 3d 20 2a 61 63 63 3b 0a 20 20 64 61 74  mp = *acc;.  dat
37606 61 42 75 66 66 65 72 49 6e 69 74 28 61 63 63 2c  aBufferInit(acc,
37607 20 74 6d 70 2e 6e 44 61 74 61 2b 6e 44 61 74 61   tmp.nData+nData
37608 29 3b 0a 20 20 64 6f 63 4c 69 73 74 55 6e 69 6f  );.  docListUnio
37609 6e 28 74 6d 70 2e 70 44 61 74 61 2c 20 74 6d 70  n(tmp.pData, tmp
3760a 2e 6e 44 61 74 61 2c 20 70 44 61 74 61 2c 20 6e  .nData, pData, n
3760b 44 61 74 61 2c 20 61 63 63 29 3b 0a 20 20 64 61  Data, acc);.  da
3760c 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
3760d 26 74 6d 70 29 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44  &tmp);.}../* TOD
3760e 4f 28 73 68 65 73 73 29 20 49 74 20 6d 69 67 68  O(shess) It migh
3760f 74 20 62 65 20 69 6e 74 65 72 65 73 74 69 6e 67  t be interesting
37610 20 74 6f 20 65 78 70 6c 6f 72 65 20 64 69 66 66   to explore diff
37611 65 72 65 6e 74 20 6d 65 72 67 65 0a 2a 2a 20 73  erent merge.** s
37612 74 72 61 74 65 67 69 65 73 2c 20 68 65 72 65 2e  trategies, here.
37613 20 20 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c 20    For instance, 
37614 73 69 6e 63 65 20 74 68 69 73 20 69 73 20 61 20  since this is a 
37615 73 6f 72 74 65 64 20 6d 65 72 67 65 2c 20 77 65  sorted merge, we
37616 0a 2a 2a 20 63 6f 75 6c 64 20 65 61 73 69 6c 79  .** could easily
37617 20 6d 65 72 67 65 20 6d 61 6e 79 20 64 6f 63 6c   merge many docl
37618 69 73 74 73 20 69 6e 20 70 61 72 61 6c 6c 65 6c  ists in parallel
37619 2e 20 20 57 69 74 68 20 73 6f 6d 65 0a 2a 2a 20  .  With some.** 
3761a 63 6f 6d 70 72 65 68 65 6e 73 69 6f 6e 20 6f 66  comprehension of
3761b 20 74 68 65 20 73 74 6f 72 61 67 65 20 66 6f 72   the storage for
3761c 6d 61 74 2c 20 77 65 20 63 6f 75 6c 64 20 6d 65  mat, we could me
3761d 72 67 65 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a  rge all of the.*
3761e 2a 20 64 6f 63 6c 69 73 74 73 20 77 69 74 68 69  * doclists withi
3761f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 20 64 69  n a leaf node di
37620 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20  rectly from the 
37621 6c 65 61 66 20 6e 6f 64 65 27 73 20 73 74 6f 72  leaf node's stor
37622 61 67 65 2e 0a 2a 2a 20 49 74 20 6d 61 79 20 62  age..** It may b
37623 65 20 77 6f 72 74 68 77 68 69 6c 65 20 74 6f 20  e worthwhile to 
37624 6d 65 72 67 65 20 73 6d 61 6c 6c 65 72 20 64 6f  merge smaller do
37625 63 6c 69 73 74 73 20 62 65 66 6f 72 65 20 6c 61  clists before la
37626 72 67 65 72 0a 2a 2a 20 64 6f 63 6c 69 73 74 73  rger.** doclists
37627 2c 20 73 69 6e 63 65 20 74 68 65 79 20 63 61 6e  , since they can
37628 20 62 65 20 74 72 61 76 65 72 73 65 64 20 6d 6f   be traversed mo
37629 72 65 20 71 75 69 63 6b 6c 79 20 2d 20 62 75 74  re quickly - but
3762a 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20   the.** results 
3762b 6d 61 79 20 68 61 76 65 20 6c 65 73 73 20 6f 76  may have less ov
3762c 65 72 6c 61 70 2c 20 6d 61 6b 69 6e 67 20 74 68  erlap, making th
3762d 65 6d 20 6d 6f 72 65 20 65 78 70 65 6e 73 69 76  em more expensiv
3762e 65 20 69 6e 20 61 0a 2a 2a 20 64 69 66 66 65 72  e in a.** differ
3762f 65 6e 74 20 77 61 79 2e 0a 2a 2f 0a 0a 2f 2a 20  ent way..*/../* 
37630 53 63 61 6e 20 70 52 65 61 64 65 72 20 66 6f 72  Scan pReader for
37631 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2c 20 61 6e   pTerm/nTerm, an
37632 64 20 6d 65 72 67 65 20 74 68 65 20 74 65 72 6d  d merge the term
37633 27 73 20 64 6f 63 6c 69 73 74 20 6f 76 65 72 0a  's doclist over.
37634 2a 2a 20 2a 6f 75 74 20 28 61 6e 79 20 64 6f 63  ** *out (any doc
37635 6c 69 73 74 73 20 77 69 74 68 20 64 75 70 6c 69  lists with dupli
37636 63 61 74 65 20 64 6f 63 69 64 73 20 6f 76 65 72  cate docids over
37637 77 72 69 74 65 20 74 68 6f 73 65 20 69 6e 20 2a  write those in *
37638 6f 75 74 29 2e 0a 2a 2a 20 49 6e 74 65 72 6e 61  out)..** Interna
37639 6c 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 6c  l function for l
3763a 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 66 28 29  oadSegmentLeaf()
3763b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3763c 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65  loadSegmentLeave
3763d 73 49 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  sInt(fulltext_vt
3763e 61 62 20 2a 76 2c 20 4c 65 61 76 65 73 52 65 61  ab *v, LeavesRea
3763f 64 65 72 20 2a 70 52 65 61 64 65 72 2c 0a 20 20  der *pReader,.  
37640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37641 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
37642 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c  nst char *pTerm,
37643 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20   int nTerm, int 
37644 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20  isPrefix,.      
37645 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37646 20 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75            DataBu
37647 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 2f 2a  ffer *out){.  /*
37648 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 69 73   doclist data is
37649 20 61 63 63 75 6d 75 6c 61 74 65 64 20 69 6e 74   accumulated int
3764a 6f 20 70 42 75 66 66 65 72 73 20 73 69 6d 69 6c  o pBuffers simil
3764b 61 72 20 74 6f 20 68 6f 77 20 6f 6e 65 20 64 6f  ar to how one do
3764c 65 73 0a 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e  es.  ** incremen
3764d 74 20 69 6e 20 62 69 6e 61 72 79 20 61 72 69 74  t in binary arit
3764e 68 6d 65 74 69 63 2e 20 20 49 66 20 69 6e 64 65  hmetic.  If inde
3764f 78 20 30 20 69 73 20 65 6d 70 74 79 2c 20 74 68  x 0 is empty, th
37650 65 20 64 61 74 61 20 69 73 0a 20 20 2a 2a 20 73  e data is.  ** s
37651 74 6f 72 65 64 20 74 68 65 72 65 2e 20 20 49 66  tored there.  If
37652 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20 74   there is data t
37653 68 65 72 65 2c 20 69 74 20 69 73 20 6d 65 72 67  here, it is merg
37654 65 64 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20  ed and the.  ** 
37655 72 65 73 75 6c 74 73 20 63 61 72 72 69 65 64 20  results carried 
37656 69 6e 74 6f 20 70 6f 73 69 74 69 6f 6e 20 31 2c  into position 1,
37657 20 77 69 74 68 20 66 75 72 74 68 65 72 20 6d 65   with further me
37658 72 67 65 2d 61 6e 64 2d 63 61 72 72 79 0a 20 20  rge-and-carry.  
37659 2a 2a 20 75 6e 74 69 6c 20 61 6e 20 65 6d 70 74  ** until an empt
3765a 79 20 70 6f 73 69 74 69 6f 6e 20 69 73 20 66 6f  y position is fo
3765b 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 44 61 74 61  und..  */.  Data
3765c 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72 73  Buffer *pBuffers
3765d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 6e 74 20 6e   = NULL;.  int n
3765e 42 75 66 66 65 72 73 20 3d 20 30 2c 20 6e 4d 61  Buffers = 0, nMa
3765f 78 42 75 66 66 65 72 73 20 3d 20 30 2c 20 72 63  xBuffers = 0, rc
37660 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65  ;..  assert( nTe
37661 72 6d 3e 30 20 29 3b 0a 0a 20 20 66 6f 72 28 72  rm>0 );..  for(r
37662 63 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 72 63 3d  c=SQLITE_OK; rc=
37663 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c  =SQLITE_OK && !l
37664 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64  eavesReaderAtEnd
37665 28 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20  (pReader);.     
37666 20 72 63 3d 6c 65 61 76 65 73 52 65 61 64 65 72   rc=leavesReader
37667 53 74 65 70 28 76 2c 20 70 52 65 61 64 65 72 29  Step(v, pReader)
37668 29 7b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73  ){.    /* TODO(s
37669 68 65 73 73 29 20 52 65 61 6c 6c 79 20 77 61 6e  hess) Really wan
3766a 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  t leavesReaderTe
3766b 72 6d 43 6d 70 28 29 2c 20 62 75 74 20 74 68 61  rmCmp(), but tha
3766c 74 20 6e 61 6d 65 20 69 73 0a 20 20 20 20 2a 2a  t name is.    **
3766d 20 61 6c 72 65 61 64 79 20 74 61 6b 65 6e 20 74   already taken t
3766e 6f 20 63 6f 6d 70 61 72 65 20 74 68 65 20 74 65  o compare the te
3766f 72 6d 73 20 6f 66 20 74 77 6f 20 4c 65 61 76 65  rms of two Leave
37670 73 52 65 61 64 65 72 73 2e 20 20 54 68 69 6e 6b  sReaders.  Think
37671 0a 20 20 20 20 2a 2a 20 6f 6e 20 61 20 62 65 74  .    ** on a bet
37672 74 65 72 20 6e 61 6d 65 2e 20 20 5b 4d 65 61 6e  ter name.  [Mean
37673 77 68 69 6c 65 2c 20 62 72 65 61 6b 20 65 6e 63  while, break enc
37674 61 70 73 75 6c 61 74 69 6f 6e 20 72 61 74 68 65  apsulation rathe
37675 72 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 75 73  r than.    ** us
37676 65 20 61 20 63 6f 6e 66 75 73 69 6e 67 20 6e 61  e a confusing na
37677 6d 65 2e 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20  me.].    */.    
37678 69 6e 74 20 63 20 3d 20 6c 65 61 66 52 65 61 64  int c = leafRead
37679 65 72 54 65 72 6d 43 6d 70 28 26 70 52 65 61 64  erTermCmp(&pRead
3767a 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 2c 20  er->leafReader, 
3767b 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73  pTerm, nTerm, is
3767c 50 72 65 66 69 78 29 3b 0a 20 20 20 20 69 66 28  Prefix);.    if(
3767d 20 63 3e 30 20 29 20 62 72 65 61 6b 3b 20 20 20   c>0 ) break;   
3767e 20 20 20 2f 2a 20 50 61 73 74 20 61 6e 79 20 70     /* Past any p
3767f 6f 73 73 69 62 6c 65 20 6d 61 74 63 68 65 73 2e  ossible matches.
37680 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 30   */.    if( c==0
37681 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   ){.      const 
37682 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 6c 65  char *pData = le
37683 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28 70  avesReaderData(p
37684 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20 69  Reader);.      i
37685 6e 74 20 69 42 75 66 66 65 72 2c 20 6e 44 61 74  nt iBuffer, nDat
37686 61 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72  a = leavesReader
37687 44 61 74 61 42 79 74 65 73 28 70 52 65 61 64 65  DataBytes(pReade
37688 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69  r);..      /* Fi
37689 6e 64 20 74 68 65 20 66 69 72 73 74 20 65 6d 70  nd the first emp
3768a 74 79 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20  ty buffer. */.  
3768b 20 20 20 20 66 6f 72 28 69 42 75 66 66 65 72 3d      for(iBuffer=
3768c 30 3b 20 69 42 75 66 66 65 72 3c 6e 42 75 66 66  0; iBuffer<nBuff
3768d 65 72 73 3b 20 2b 2b 69 42 75 66 66 65 72 29 7b  ers; ++iBuffer){
3768e 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d  .        if( 0==
3768f 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72  pBuffers[iBuffer
37690 5d 2e 6e 44 61 74 61 20 29 20 62 72 65 61 6b 3b  ].nData ) break;
37691 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
37692 2f 2a 20 4f 75 74 20 6f 66 20 62 75 66 66 65 72  /* Out of buffer
37693 73 2c 20 61 64 64 20 61 6e 20 65 6d 70 74 79 20  s, add an empty 
37694 6f 6e 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  one. */.      if
37695 28 20 69 42 75 66 66 65 72 3d 3d 6e 42 75 66 66  ( iBuffer==nBuff
37696 65 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ers ){.        i
37697 66 28 20 6e 42 75 66 66 65 72 73 3d 3d 6e 4d 61  f( nBuffers==nMa
37698 78 42 75 66 66 65 72 73 20 29 7b 0a 20 20 20 20  xBuffers ){.    
37699 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
3769a 20 2a 70 3b 0a 20 20 20 20 20 20 20 20 20 20 6e   *p;.          n
3769b 4d 61 78 42 75 66 66 65 72 73 20 2b 3d 20 32 30  MaxBuffers += 20
3769c 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;..          /* 
3769d 4d 61 6e 75 61 6c 20 72 65 61 6c 6c 6f 63 20 73  Manual realloc s
3769e 6f 20 77 65 20 63 61 6e 20 68 61 6e 64 6c 65 20  o we can handle 
3769f 4e 55 4c 4c 20 61 70 70 72 6f 70 72 69 61 74 65  NULL appropriate
376a0 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ly. */.         
376a1 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c   p = sqlite3_mal
376a2 6c 6f 63 28 6e 4d 61 78 42 75 66 66 65 72 73 2a  loc(nMaxBuffers*
376a3 73 69 7a 65 6f 66 28 2a 70 42 75 66 66 65 72 73  sizeof(*pBuffers
376a4 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ));.          if
376a5 28 20 70 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  ( p==NULL ){.   
376a6 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
376a7 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
376a8 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
376a9 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20           }..    
376aa 20 20 20 20 20 20 69 66 28 20 6e 42 75 66 66 65        if( nBuffe
376ab 72 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  rs>0 ){.        
376ac 20 20 20 20 61 73 73 65 72 74 28 70 42 75 66 66      assert(pBuff
376ad 65 72 73 21 3d 4e 55 4c 4c 29 3b 0a 20 20 20 20  ers!=NULL);.    
376ae 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70          memcpy(p
376af 2c 20 70 42 75 66 66 65 72 73 2c 20 6e 42 75 66  , pBuffers, nBuf
376b0 66 65 72 73 2a 73 69 7a 65 6f 66 28 2a 70 42 75  fers*sizeof(*pBu
376b1 66 66 65 72 73 29 29 3b 0a 20 20 20 20 20 20 20  ffers));.       
376b2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
376b3 65 28 70 42 75 66 66 65 72 73 29 3b 0a 20 20 20  e(pBuffers);.   
376b4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
376b5 20 20 20 70 42 75 66 66 65 72 73 20 3d 20 70 3b     pBuffers = p;
376b6 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
376b7 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69     dataBufferIni
376b8 74 28 26 28 70 42 75 66 66 65 72 73 5b 6e 42 75  t(&(pBuffers[nBu
376b9 66 66 65 72 73 5d 29 2c 20 30 29 3b 0a 20 20 20  ffers]), 0);.   
376ba 20 20 20 20 20 6e 42 75 66 66 65 72 73 2b 2b 3b       nBuffers++;
376bb 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
376bc 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
376bd 2c 20 6d 75 73 74 20 68 61 76 65 20 61 6e 20 65  , must have an e
376be 6d 70 74 79 20 61 74 20 69 42 75 66 66 65 72 2e  mpty at iBuffer.
376bf 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
376c0 28 69 42 75 66 66 65 72 3c 6e 42 75 66 66 65 72  (iBuffer<nBuffer
376c1 73 20 26 26 20 70 42 75 66 66 65 72 73 5b 69 42  s && pBuffers[iB
376c2 75 66 66 65 72 5d 2e 6e 44 61 74 61 3d 3d 30 29  uffer].nData==0)
376c3 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 65  ;..      /* If e
376c4 6d 70 74 79 20 77 61 73 20 66 69 72 73 74 20 62  mpty was first b
376c5 75 66 66 65 72 2c 20 6e 6f 20 6e 65 65 64 20 66  uffer, no need f
376c6 6f 72 20 6d 65 72 67 65 20 6c 6f 67 69 63 2e 20  or merge logic. 
376c7 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 42 75  */.      if( iBu
376c8 66 66 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ffer==0 ){.     
376c9 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70     dataBufferRep
376ca 6c 61 63 65 28 26 28 70 42 75 66 66 65 72 73 5b  lace(&(pBuffers[
376cb 30 5d 29 2c 20 70 44 61 74 61 2c 20 6e 44 61 74  0]), pData, nDat
376cc 61 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  a);.      }else{
376cd 0a 20 20 20 20 20 20 20 20 2f 2a 20 70 41 63 63  .        /* pAcc
376ce 20 69 73 20 74 68 65 20 65 6d 70 74 79 20 62 75   is the empty bu
376cf 66 66 65 72 20 74 68 65 20 6d 65 72 67 65 64 20  ffer the merged 
376d0 64 61 74 61 20 77 69 6c 6c 20 65 6e 64 20 75 70  data will end up
376d1 20 69 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20   in. */.        
376d2 44 61 74 61 42 75 66 66 65 72 20 2a 70 41 63 63  DataBuffer *pAcc
376d3 20 3d 20 26 28 70 42 75 66 66 65 72 73 5b 69 42   = &(pBuffers[iB
376d4 75 66 66 65 72 5d 29 3b 0a 20 20 20 20 20 20 20  uffer]);.       
376d5 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 20 3d   DataBuffer *p =
376d6 20 26 28 70 42 75 66 66 65 72 73 5b 30 5d 29 3b   &(pBuffers[0]);
376d7 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e  ..        /* Han
376d8 64 6c 65 20 70 6f 73 69 74 69 6f 6e 20 30 20 73  dle position 0 s
376d9 70 65 63 69 61 6c 6c 79 20 74 6f 20 61 76 6f 69  pecially to avoi
376da 64 20 6e 65 65 64 20 74 6f 20 70 72 69 6d 65 20  d need to prime 
376db 70 41 63 63 0a 20 20 20 20 20 20 20 20 2a 2a 20  pAcc.        ** 
376dc 77 69 74 68 20 70 44 61 74 61 2f 6e 44 61 74 61  with pData/nData
376dd 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
376de 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 53       dataBufferS
376df 77 61 70 28 70 2c 20 70 41 63 63 29 3b 0a 20 20  wap(p, pAcc);.  
376e0 20 20 20 20 20 20 64 6f 63 4c 69 73 74 41 63 63        docListAcc
376e1 75 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 70 41 63  umulateUnion(pAc
376e2 63 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29  c, pData, nData)
376e3 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63  ;..        /* Ac
376e4 63 75 6d 75 6c 61 74 65 20 72 65 6d 61 69 6e 69  cumulate remaini
376e5 6e 67 20 64 6f 63 6c 69 73 74 73 20 69 6e 74 6f  ng doclists into
376e6 20 70 41 63 63 2e 20 2a 2f 0a 20 20 20 20 20 20   pAcc. */.      
376e7 20 20 66 6f 72 28 2b 2b 70 3b 20 70 3c 70 41 63    for(++p; p<pAc
376e8 63 3b 20 2b 2b 70 29 7b 0a 20 20 20 20 20 20 20  c; ++p){.       
376e9 20 20 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75     docListAccumu
376ea 6c 61 74 65 55 6e 69 6f 6e 28 70 41 63 63 2c 20  lateUnion(pAcc, 
376eb 70 2d 3e 70 44 61 74 61 2c 20 70 2d 3e 6e 44 61  p->pData, p->nDa
376ec 74 61 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  ta);..          
376ed 2f 2a 20 64 61 74 61 42 75 66 66 65 72 52 65 73  /* dataBufferRes
376ee 65 74 28 29 20 63 6f 75 6c 64 20 61 6c 6c 6f 77  et() could allow
376ef 20 61 20 6c 61 72 67 65 20 64 6f 63 6c 69 73 74   a large doclist
376f0 20 74 6f 20 62 6c 6f 77 20 75 70 0a 20 20 20 20   to blow up.    
376f1 20 20 20 20 20 20 2a 2a 20 6f 75 72 20 6d 65 6d        ** our mem
376f2 6f 72 79 20 72 65 71 75 69 72 65 6d 65 6e 74 73  ory requirements
376f3 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
376f4 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e           if( p->
376f5 6e 43 61 70 61 63 69 74 79 3c 31 30 32 34 20 29  nCapacity<1024 )
376f6 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 61  {.            da
376f7 74 61 42 75 66 66 65 72 52 65 73 65 74 28 70 29  taBufferReset(p)
376f8 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
376f9 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64  e{.            d
376fa 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
376fb 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  (p);.           
376fc 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
376fd 70 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  p, 0);.         
376fe 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
376ff 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
37700 20 20 2f 2a 20 55 6e 69 6f 6e 20 61 6c 6c 20 74    /* Union all t
37701 68 65 20 64 6f 63 6c 69 73 74 73 20 74 6f 67 65  he doclists toge
37702 74 68 65 72 20 69 6e 74 6f 20 2a 6f 75 74 2e 20  ther into *out. 
37703 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  */.  /* TODO(she
37704 73 73 29 20 57 68 61 74 20 69 66 20 2a 6f 75 74  ss) What if *out
37705 20 69 73 20 62 69 67 3f 20 20 53 69 67 68 2e 20   is big?  Sigh. 
37706 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
37707 49 54 45 5f 4f 4b 20 26 26 20 6e 42 75 66 66 65  ITE_OK && nBuffe
37708 72 73 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  rs>0 ){.    int 
37709 69 42 75 66 66 65 72 3b 0a 20 20 20 20 66 6f 72  iBuffer;.    for
3770a 28 69 42 75 66 66 65 72 3d 30 3b 20 69 42 75 66  (iBuffer=0; iBuf
3770b 66 65 72 3c 6e 42 75 66 66 65 72 73 3b 20 2b 2b  fer<nBuffers; ++
3770c 69 42 75 66 66 65 72 29 7b 0a 20 20 20 20 20 20  iBuffer){.      
3770d 69 66 28 20 70 42 75 66 66 65 72 73 5b 69 42 75  if( pBuffers[iBu
3770e 66 66 65 72 5d 2e 6e 44 61 74 61 3e 30 20 29 7b  ffer].nData>0 ){
3770f 0a 20 20 20 20 20 20 20 20 69 66 28 20 6f 75 74  .        if( out
37710 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20  ->nData==0 ){.  
37711 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
37712 65 72 53 77 61 70 28 6f 75 74 2c 20 26 28 70 42  erSwap(out, &(pB
37713 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 29  uffers[iBuffer])
37714 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
37715 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f 63 4c  {.          docL
37716 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55 6e 69  istAccumulateUni
37717 6f 6e 28 6f 75 74 2c 20 70 42 75 66 66 65 72 73  on(out, pBuffers
37718 5b 69 42 75 66 66 65 72 5d 2e 70 44 61 74 61 2c  [iBuffer].pData,
37719 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3771a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3771b 20 20 70 42 75 66 66 65 72 73 5b 69 42 75 66 66    pBuffers[iBuff
3771c 65 72 5d 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20  er].nData);.    
3771d 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
3771e 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65    }.  }..  while
3771f 28 20 6e 42 75 66 66 65 72 73 2d 2d 20 29 7b 0a  ( nBuffers-- ){.
37720 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
37721 73 74 72 6f 79 28 26 28 70 42 75 66 66 65 72 73  stroy(&(pBuffers
37722 5b 6e 42 75 66 66 65 72 73 5d 29 29 3b 0a 20 20  [nBuffers]));.  
37723 7d 0a 20 20 69 66 28 20 70 42 75 66 66 65 72 73  }.  if( pBuffers
37724 21 3d 4e 55 4c 4c 20 29 20 73 71 6c 69 74 65 33  !=NULL ) sqlite3
37725 5f 66 72 65 65 28 70 42 75 66 66 65 72 73 29 3b  _free(pBuffers);
37726 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
37727 0a 0a 2f 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65  ../* Call loadSe
37728 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 29  gmentLeavesInt()
37729 20 77 69 74 68 20 70 44 61 74 61 2f 6e 44 61 74   with pData/nDat
3772a 61 20 61 73 20 69 6e 70 75 74 2e 20 2a 2f 0a 73  a as input. */.s
3772b 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65  tatic int loadSe
3772c 67 6d 65 6e 74 4c 65 61 66 28 66 75 6c 6c 74 65  gmentLeaf(fullte
3772d 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e 73  xt_vtab *v, cons
3772e 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69  t char *pData, i
3772f 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20  nt nData,.      
37730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37731 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
37732 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
37733 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c  m, int isPrefix,
37734 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37735 20 20 20 20 20 20 20 20 20 20 20 20 44 61 74 61              Data
37736 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20  Buffer *out){.  
37737 4c 65 61 76 65 73 52 65 61 64 65 72 20 72 65 61  LeavesReader rea
37738 64 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  der;.  int rc;..
37739 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e    assert( nData>
3773a 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 2a  1 );.  assert( *
3773b 70 44 61 74 61 3d 3d 27 5c 30 27 20 29 3b 0a 20  pData=='\0' );. 
3773c 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64   rc = leavesRead
3773d 65 72 49 6e 69 74 28 76 2c 20 30 2c 20 30 2c 20  erInit(v, 0, 0, 
3773e 30 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  0, pData, nData,
3773f 20 26 72 65 61 64 65 72 29 3b 0a 20 20 69 66 28   &reader);.  if(
37740 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
37741 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
37742 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c  c = loadSegmentL
37743 65 61 76 65 73 49 6e 74 28 76 2c 20 26 72 65 61  eavesInt(v, &rea
37744 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72  der, pTerm, nTer
37745 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 6f 75 74  m, isPrefix, out
37746 29 3b 0a 20 20 6c 65 61 76 65 73 52 65 61 64 65  );.  leavesReade
37747 72 52 65 73 65 74 28 26 72 65 61 64 65 72 29 3b  rReset(&reader);
37748 0a 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 44  .  leavesReaderD
37749 65 73 74 72 6f 79 28 26 72 65 61 64 65 72 29 3b  estroy(&reader);
3774a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3774b 0a 2f 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65 67  ./* Call loadSeg
3774c 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 29 20  mentLeavesInt() 
3774d 77 69 74 68 20 74 68 65 20 6c 65 61 66 20 6e 6f  with the leaf no
3774e 64 65 73 20 66 72 6f 6d 20 69 53 74 61 72 74 4c  des from iStartL
3774f 65 61 66 20 74 6f 0a 2a 2a 20 69 45 6e 64 4c 65  eaf to.** iEndLe
37750 61 66 20 28 69 6e 63 6c 75 73 69 76 65 29 20 61  af (inclusive) a
37751 73 20 69 6e 70 75 74 2c 20 61 6e 64 20 6d 65 72  s input, and mer
37752 67 65 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  ge the resulting
37753 20 64 6f 63 6c 69 73 74 20 69 6e 74 6f 0a 2a 2a   doclist into.**
37754 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   out..*/.static 
37755 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c  int loadSegmentL
37756 65 61 76 65 73 28 66 75 6c 6c 74 65 78 74 5f 76  eaves(fulltext_v
37757 74 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20  tab *v,.        
37758 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37759 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
3775a 34 20 69 53 74 61 72 74 4c 65 61 66 2c 20 73 71  4 iStartLeaf, sq
3775b 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 4c  lite_int64 iEndL
3775c 65 61 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20  eaf,.           
3775d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3775e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
3775f 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
37760 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
37761 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37762 20 20 20 20 20 20 20 20 20 20 20 44 61 74 61 42             DataB
37763 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 69  uffer *out){.  i
37764 6e 74 20 72 63 3b 0a 20 20 4c 65 61 76 65 73 52  nt rc;.  LeavesR
37765 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a 0a 20  eader reader;.. 
37766 20 61 73 73 65 72 74 28 20 69 53 74 61 72 74 4c   assert( iStartL
37767 65 61 66 3c 3d 69 45 6e 64 4c 65 61 66 20 29 3b  eaf<=iEndLeaf );
37768 0a 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65  .  rc = leavesRe
37769 61 64 65 72 49 6e 69 74 28 76 2c 20 30 2c 20 69  aderInit(v, 0, i
3776a 53 74 61 72 74 4c 65 61 66 2c 20 69 45 6e 64 4c  StartLeaf, iEndL
3776b 65 61 66 2c 20 4e 55 4c 4c 2c 20 30 2c 20 26 72  eaf, NULL, 0, &r
3776c 65 61 64 65 72 29 3b 0a 20 20 69 66 28 20 72 63  eader);.  if( rc
3776d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
3776e 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
3776f 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76   loadSegmentLeav
37770 65 73 49 6e 74 28 76 2c 20 26 72 65 61 64 65 72  esInt(v, &reader
37771 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
37772 69 73 50 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a  isPrefix, out);.
37773 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 52 65    leavesReaderRe
37774 73 65 74 28 26 72 65 61 64 65 72 29 3b 0a 20 20  set(&reader);.  
37775 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73 74  leavesReaderDest
37776 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 20 20  roy(&reader);.  
37777 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
37778 20 54 61 6b 69 6e 67 20 70 44 61 74 61 2f 6e 44   Taking pData/nD
37779 61 74 61 20 61 73 20 61 6e 20 69 6e 74 65 72 69  ata as an interi
3777a 6f 72 20 6e 6f 64 65 2c 20 66 69 6e 64 20 74 68  or node, find th
3777b 65 20 73 65 71 75 65 6e 63 65 20 6f 66 20 63 68  e sequence of ch
3777c 69 6c 64 0a 2a 2a 20 6e 6f 64 65 73 20 77 68 69  ild.** nodes whi
3777d 63 68 20 63 6f 75 6c 64 20 69 6e 63 6c 75 64 65  ch could include
3777e 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2f 69 73 50   pTerm/nTerm/isP
3777f 72 65 66 69 78 2e 20 20 4e 6f 74 65 20 74 68 61  refix.  Note tha
37780 74 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 69 6f  t the.** interio
37781 72 20 6e 6f 64 65 20 74 65 72 6d 73 20 6c 6f 67  r node terms log
37782 69 63 61 6c 6c 79 20 63 6f 6d 65 20 62 65 74 77  ically come betw
37783 65 65 6e 20 74 68 65 20 62 6c 6f 63 6b 73 2c 20  een the blocks, 
37784 73 6f 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6f  so there is.** o
37785 6e 65 20 6d 6f 72 65 20 62 6c 6f 63 6b 69 64 20  ne more blockid 
37786 74 68 61 6e 20 74 68 65 72 65 20 61 72 65 20 74  than there are t
37787 65 72 6d 73 20 28 74 68 61 74 20 62 6c 6f 63 6b  erms (that block
37788 20 63 6f 6e 74 61 69 6e 73 20 74 65 72 6d 73 20   contains terms 
37789 3e 3d 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 69  >=.** the last i
3778a 6e 74 65 72 69 6f 72 2d 6e 6f 64 65 20 74 65 72  nterior-node ter
3778b 6d 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  m)..*/./* TODO(s
3778c 68 65 73 73 29 20 54 68 65 20 63 61 6c 6c 69 6e  hess) The callin
3778d 67 20 63 6f 64 65 20 6d 61 79 20 61 6c 72 65 61  g code may alrea
3778e 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65  dy know that the
3778f 20 65 6e 64 20 63 68 69 6c 64 20 69 73 0a 2a 2a   end child is.**
37790 20 6e 6f 74 20 77 6f 72 74 68 20 63 61 6c 63 75   not worth calcu
37791 6c 61 74 69 6e 67 2c 20 62 65 63 61 75 73 65 20  lating, because 
37792 74 68 65 20 65 6e 64 20 6d 61 79 20 62 65 20 69  the end may be i
37793 6e 20 61 20 6c 61 74 65 72 20 73 69 62 6c 69 6e  n a later siblin
37794 67 0a 2a 2a 20 6e 6f 64 65 2e 20 20 43 6f 6e 73  g.** node.  Cons
37795 69 64 65 72 20 77 68 65 74 68 65 72 20 62 72 65  ider whether bre
37796 61 6b 69 6e 67 20 73 79 6d 6d 65 74 72 79 20 69  aking symmetry i
37797 73 20 77 6f 72 74 68 77 68 69 6c 65 2e 20 20 49  s worthwhile.  I
37798 20 73 75 73 70 65 63 74 0a 2a 2a 20 69 74 20 69   suspect.** it i
37799 73 20 6e 6f 74 20 77 6f 72 74 68 77 68 69 6c 65  s not worthwhile
3779a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
3779b 20 67 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74   getChildrenCont
3779c 61 69 6e 69 6e 67 28 63 6f 6e 73 74 20 63 68 61  aining(const cha
3779d 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
3779e 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
3779f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
377a0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
377a1 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
377a2 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
377a3 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
377a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
377a5 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
377a6 34 20 2a 70 69 53 74 61 72 74 43 68 69 6c 64 2c  4 *piStartChild,
377a7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
377a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
377a9 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
377aa 2a 70 69 45 6e 64 43 68 69 6c 64 29 7b 0a 20 20  *piEndChild){.  
377ab 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 72  InteriorReader r
377ac 65 61 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  eader;..  assert
377ad 28 20 6e 44 61 74 61 3e 31 20 29 3b 0a 20 20 61  ( nData>1 );.  a
377ae 73 73 65 72 74 28 20 2a 70 44 61 74 61 21 3d 27  ssert( *pData!='
377af 5c 30 27 20 29 3b 0a 20 20 69 6e 74 65 72 69 6f  \0' );.  interio
377b0 72 52 65 61 64 65 72 49 6e 69 74 28 70 44 61 74  rReaderInit(pDat
377b1 61 2c 20 6e 44 61 74 61 2c 20 26 72 65 61 64 65  a, nData, &reade
377b2 72 29 3b 0a 0a 20 20 2f 2a 20 53 63 61 6e 20 66  r);..  /* Scan f
377b3 6f 72 20 74 68 65 20 66 69 72 73 74 20 63 68 69  or the first chi
377b4 6c 64 20 77 68 69 63 68 20 63 6f 75 6c 64 20 63  ld which could c
377b5 6f 6e 74 61 69 6e 20 70 54 65 72 6d 2f 6e 54 65  ontain pTerm/nTe
377b6 72 6d 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  rm. */.  while( 
377b7 21 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41  !interiorReaderA
377b8 74 45 6e 64 28 26 72 65 61 64 65 72 29 20 29 7b  tEnd(&reader) ){
377b9 0a 20 20 20 20 69 66 28 20 69 6e 74 65 72 69 6f  .    if( interio
377ba 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26  rReaderTermCmp(&
377bb 72 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e  reader, pTerm, n
377bc 54 65 72 6d 2c 20 30 29 3e 30 20 29 20 62 72 65  Term, 0)>0 ) bre
377bd 61 6b 3b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72  ak;.    interior
377be 52 65 61 64 65 72 53 74 65 70 28 26 72 65 61 64  ReaderStep(&read
377bf 65 72 29 3b 0a 20 20 7d 0a 20 20 2a 70 69 53 74  er);.  }.  *piSt
377c0 61 72 74 43 68 69 6c 64 20 3d 20 69 6e 74 65 72  artChild = inter
377c1 69 6f 72 52 65 61 64 65 72 43 75 72 72 65 6e 74  iorReaderCurrent
377c2 42 6c 6f 63 6b 69 64 28 26 72 65 61 64 65 72 29  Blockid(&reader)
377c3 3b 0a 0a 20 20 2f 2a 20 4b 65 65 70 20 73 63 61  ;..  /* Keep sca
377c4 6e 6e 69 6e 67 20 74 6f 20 66 69 6e 64 20 61 20  nning to find a 
377c5 74 65 72 6d 20 67 72 65 61 74 65 72 20 74 68 61  term greater tha
377c6 6e 20 6f 75 72 20 74 65 72 6d 2c 20 75 73 69 6e  n our term, usin
377c7 67 20 70 72 65 66 69 78 0a 20 20 2a 2a 20 63 6f  g prefix.  ** co
377c8 6d 70 61 72 69 73 6f 6e 20 69 66 20 69 6e 64 69  mparison if indi
377c9 63 61 74 65 64 2e 20 20 49 66 20 69 73 50 72 65  cated.  If isPre
377ca 66 69 78 20 69 73 20 66 61 6c 73 65 2c 20 74 68  fix is false, th
377cb 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 20  is will be the. 
377cc 20 2a 2a 20 73 61 6d 65 20 62 6c 6f 63 6b 69 64   ** same blockid
377cd 20 61 73 20 74 68 65 20 73 74 61 72 74 69 6e 67   as the starting
377ce 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 77   block..  */.  w
377cf 68 69 6c 65 28 20 21 69 6e 74 65 72 69 6f 72 52  hile( !interiorR
377d0 65 61 64 65 72 41 74 45 6e 64 28 26 72 65 61 64  eaderAtEnd(&read
377d1 65 72 29 20 29 7b 0a 20 20 20 20 69 66 28 20 69  er) ){.    if( i
377d2 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72  nteriorReaderTer
377d3 6d 43 6d 70 28 26 72 65 61 64 65 72 2c 20 70 54  mCmp(&reader, pT
377d4 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
377d5 65 66 69 78 29 3e 30 20 29 20 62 72 65 61 6b 3b  efix)>0 ) break;
377d6 0a 20 20 20 20 69 6e 74 65 72 69 6f 72 52 65 61  .    interiorRea
377d7 64 65 72 53 74 65 70 28 26 72 65 61 64 65 72 29  derStep(&reader)
377d8 3b 0a 20 20 7d 0a 20 20 2a 70 69 45 6e 64 43 68  ;.  }.  *piEndCh
377d9 69 6c 64 20 3d 20 69 6e 74 65 72 69 6f 72 52 65  ild = interiorRe
377da 61 64 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b  aderCurrentBlock
377db 69 64 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20  id(&reader);..  
377dc 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 44 65  interiorReaderDe
377dd 73 74 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a  stroy(&reader);.
377de 0a 20 20 2f 2a 20 43 68 69 6c 64 72 65 6e 20 6d  .  /* Children m
377df 75 73 74 20 61 73 63 65 6e 64 2c 20 61 6e 64 20  ust ascend, and 
377e0 69 66 20 21 70 72 65 66 69 78 2c 20 62 6f 74 68  if !prefix, both
377e1 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d   must be the sam
377e2 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  e. */.  assert( 
377e3 2a 70 69 45 6e 64 43 68 69 6c 64 3e 3d 2a 70 69  *piEndChild>=*pi
377e4 53 74 61 72 74 43 68 69 6c 64 20 29 3b 0a 20 20  StartChild );.  
377e5 61 73 73 65 72 74 28 20 69 73 50 72 65 66 69 78  assert( isPrefix
377e6 20 7c 7c 20 2a 70 69 53 74 61 72 74 43 68 69 6c   || *piStartChil
377e7 64 3d 3d 2a 70 69 45 6e 64 43 68 69 6c 64 20 29  d==*piEndChild )
377e8 3b 0a 7d 0a 0a 2f 2a 20 52 65 61 64 20 62 6c 6f  ;.}../* Read blo
377e9 63 6b 20 61 74 20 69 42 6c 6f 63 6b 69 64 20 61  ck at iBlockid a
377ea 6e 64 20 70 61 73 73 20 69 74 20 77 69 74 68 20  nd pass it with 
377eb 6f 74 68 65 72 20 70 61 72 61 6d 73 20 74 6f 0a  other params to.
377ec 2a 2a 20 67 65 74 43 68 69 6c 64 72 65 6e 43 6f  ** getChildrenCo
377ed 6e 74 61 69 6e 69 6e 67 28 29 2e 0a 2a 2f 0a 73  ntaining()..*/.s
377ee 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 41 6e  tatic int loadAn
377ef 64 47 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74  dGetChildrenCont
377f0 61 69 6e 69 6e 67 28 0a 20 20 66 75 6c 6c 74 65  aining(.  fullte
377f1 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 73 71  xt_vtab *v,.  sq
377f2 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63  lite_int64 iBloc
377f3 6b 69 64 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  kid,.  const cha
377f4 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
377f5 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
377f6 78 2c 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  x,.  sqlite_int6
377f7 34 20 2a 70 69 53 74 61 72 74 43 68 69 6c 64 2c  4 *piStartChild,
377f8 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
377f9 69 45 6e 64 43 68 69 6c 64 0a 29 7b 0a 20 20 73  iEndChild.){.  s
377fa 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 20 3d  qlite3_stmt *s =
377fb 20 4e 55 4c 4c 3b 0a 20 20 69 6e 74 20 72 63 3b   NULL;.  int rc;
377fc 0a 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f  ..  assert( iBlo
377fd 63 6b 69 64 21 3d 30 20 29 3b 0a 20 20 61 73 73  ckid!=0 );.  ass
377fe 65 72 74 28 20 70 54 65 72 6d 21 3d 4e 55 4c 4c  ert( pTerm!=NULL
377ff 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54   );.  assert( nT
37800 65 72 6d 21 3d 30 20 29 3b 20 20 20 20 20 20 20  erm!=0 );       
37801 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
37802 57 68 79 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68  Why not allow th
37803 69 73 3f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  is? */.  assert(
37804 20 70 69 53 74 61 72 74 43 68 69 6c 64 21 3d 4e   piStartChild!=N
37805 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ULL );.  assert(
37806 20 70 69 45 6e 64 43 68 69 6c 64 21 3d 4e 55 4c   piEndChild!=NUL
37807 4c 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c  L );..  rc = sql
37808 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
37809 2c 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54 5f 53  , BLOCK_SELECT_S
3780a 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
3780b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3780c 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
3780d 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
3780e 69 6e 74 36 34 28 73 2c 20 31 2c 20 69 42 6c 6f  int64(s, 1, iBlo
3780f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ckid);.  if( rc!
37810 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
37811 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
37812 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
37813 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
37814 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  E_DONE ) return 
37815 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
37816 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
37817 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OW ) return rc;.
37818 0a 20 20 67 65 74 43 68 69 6c 64 72 65 6e 43 6f  .  getChildrenCo
37819 6e 74 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33  ntaining(sqlite3
3781a 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20  _column_blob(s, 
3781b 30 29 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  0), sqlite3_colu
3781c 6d 6e 5f 62 79 74 65 73 28 73 2c 20 30 29 2c 0a  mn_bytes(s, 0),.
3781d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3781e 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20 6e          pTerm, n
3781f 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20  Term, isPrefix, 
37820 70 69 53 74 61 72 74 43 68 69 6c 64 2c 20 70 69  piStartChild, pi
37821 45 6e 64 43 68 69 6c 64 29 3b 0a 0a 20 20 2f 2a  EndChild);..  /*
37822 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20   We expect only 
37823 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73  one row.  We mus
37824 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65  t execute anothe
37825 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  r sqlite3_step()
37826 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74  .   * to complet
37827 65 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b  e the iteration;
37828 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74   otherwise the t
37829 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e  able will remain
3782a 0a 20 20 20 2a 20 6c 6f 63 6b 65 64 2e 20 2a 2f  .   * locked. */
3782b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
3782c 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72  step(s);.  if( r
3782d 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c==SQLITE_ROW ) 
3782e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
3782f 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ROR;.  if( rc!=S
37830 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74  QLITE_DONE ) ret
37831 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72  urn rc;..  retur
37832 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
37833 2f 2a 20 54 72 61 76 65 72 73 65 20 74 68 65 20  /* Traverse the 
37834 74 72 65 65 20 72 65 70 72 65 73 65 6e 74 65 64  tree represented
37835 20 62 79 20 70 44 61 74 61 5b 6e 44 61 74 61 5d   by pData[nData]
37836 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 0a 2a 2a 20   looking for.** 
37837 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 2c 20 70 6c  pTerm[nTerm], pl
37838 61 63 69 6e 67 20 69 74 73 20 64 6f 63 6c 69 73  acing its doclis
37839 74 20 69 6e 74 6f 20 2a 6f 75 74 2e 20 20 54 68  t into *out.  Th
3783a 69 73 20 69 73 20 69 6e 74 65 72 6e 61 6c 20 74  is is internal t
3783b 6f 0a 2a 2a 20 6c 6f 61 64 53 65 67 6d 65 6e 74  o.** loadSegment
3783c 28 29 20 74 6f 20 6d 61 6b 65 20 65 72 72 6f 72  () to make error
3783d 2d 68 61 6e 64 6c 69 6e 67 20 63 6c 65 61 6e 65  -handling cleane
3783e 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
3783f 20 6c 6f 61 64 53 65 67 6d 65 6e 74 49 6e 74 28   loadSegmentInt(
37840 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
37841 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  , const char *pD
37842 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
37843 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37844 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
37845 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e  _int64 iLeavesEn
37846 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
37847 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
37848 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
37849 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
3784a 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
3784b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3784c 20 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f     DataBuffer *o
3784d 75 74 29 7b 0a 20 20 2f 2a 20 53 70 65 63 69 61  ut){.  /* Specia
3784e 6c 20 63 61 73 65 20 77 68 65 72 65 20 72 6f 6f  l case where roo
3784f 74 20 69 73 20 61 20 6c 65 61 66 2e 20 2a 2f 0a  t is a leaf. */.
37850 20 20 69 66 28 20 2a 70 44 61 74 61 3d 3d 27 5c    if( *pData=='\
37851 30 27 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  0' ){.    return
37852 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 66   loadSegmentLeaf
37853 28 76 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  (v, pData, nData
37854 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
37855 69 73 50 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a  isPrefix, out);.
37856 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
37857 20 72 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 5f   rc;.    sqlite_
37858 69 6e 74 36 34 20 69 53 74 61 72 74 43 68 69 6c  int64 iStartChil
37859 64 2c 20 69 45 6e 64 43 68 69 6c 64 3b 0a 0a 20  d, iEndChild;.. 
3785a 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 70 44     /* Process pD
3785b 61 74 61 20 61 73 20 61 6e 20 69 6e 74 65 72 69  ata as an interi
3785c 6f 72 20 6e 6f 64 65 2c 20 74 68 65 6e 20 6c 6f  or node, then lo
3785d 6f 70 20 64 6f 77 6e 20 74 68 65 20 74 72 65 65  op down the tree
3785e 0a 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 77 65  .    ** until we
3785f 20 66 69 6e 64 20 74 68 65 20 73 65 74 20 6f 66   find the set of
37860 20 6c 65 61 66 20 6e 6f 64 65 73 20 74 6f 20 73   leaf nodes to s
37861 63 61 6e 20 66 6f 72 20 74 68 65 20 74 65 72 6d  can for the term
37862 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 67 65 74  ..    */.    get
37863 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69  ChildrenContaini
37864 6e 67 28 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  ng(pData, nData,
37865 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69   pTerm, nTerm, i
37866 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
37867 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37868 20 20 20 26 69 53 74 61 72 74 43 68 69 6c 64 2c     &iStartChild,
37869 20 26 69 45 6e 64 43 68 69 6c 64 29 3b 0a 20 20   &iEndChild);.  
3786a 20 20 77 68 69 6c 65 28 20 69 53 74 61 72 74 43    while( iStartC
3786b 68 69 6c 64 3e 69 4c 65 61 76 65 73 45 6e 64 20  hild>iLeavesEnd 
3786c 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f  ){.      sqlite_
3786d 69 6e 74 36 34 20 69 4e 65 78 74 53 74 61 72 74  int64 iNextStart
3786e 2c 20 69 4e 65 78 74 45 6e 64 3b 0a 20 20 20 20  , iNextEnd;.    
3786f 20 20 72 63 20 3d 20 6c 6f 61 64 41 6e 64 47 65    rc = loadAndGe
37870 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
37871 69 6e 67 28 76 2c 20 69 53 74 61 72 74 43 68 69  ing(v, iStartChi
37872 6c 64 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  ld, pTerm, nTerm
37873 2c 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20  , isPrefix,.    
37874 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37875 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37876 20 20 20 20 26 69 4e 65 78 74 53 74 61 72 74 2c      &iNextStart,
37877 20 26 69 4e 65 78 74 45 6e 64 29 3b 0a 20 20 20   &iNextEnd);.   
37878 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
37879 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
3787a 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 77  ;..      /* If w
3787b 65 27 76 65 20 62 72 61 6e 63 68 65 64 2c 20 66  e've branched, f
3787c 6f 6c 6c 6f 77 20 74 68 65 20 65 6e 64 20 62 72  ollow the end br
3787d 61 6e 63 68 2c 20 74 6f 6f 2e 20 2a 2f 0a 20 20  anch, too. */.  
3787e 20 20 20 20 69 66 28 20 69 53 74 61 72 74 43 68      if( iStartCh
3787f 69 6c 64 21 3d 69 45 6e 64 43 68 69 6c 64 20 29  ild!=iEndChild )
37880 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
37881 5f 69 6e 74 36 34 20 69 44 75 6d 6d 79 3b 0a 20  _int64 iDummy;. 
37882 20 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 61 64         rc = load
37883 41 6e 64 47 65 74 43 68 69 6c 64 72 65 6e 43 6f  AndGetChildrenCo
37884 6e 74 61 69 6e 69 6e 67 28 76 2c 20 69 45 6e 64  ntaining(v, iEnd
37885 43 68 69 6c 64 2c 20 70 54 65 72 6d 2c 20 6e 54  Child, pTerm, nT
37886 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 0a 20  erm, isPrefix,. 
37887 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37888 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37889 20 20 20 20 20 20 20 20 20 26 69 44 75 6d 6d 79           &iDummy
3788a 2c 20 26 69 4e 65 78 74 45 6e 64 29 3b 0a 20 20  , &iNextEnd);.  
3788b 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
3788c 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3788d 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   rc;.      }..  
3788e 20 20 20 20 61 73 73 65 72 74 28 20 69 4e 65 78      assert( iNex
3788f 74 53 74 61 72 74 3c 3d 69 4e 65 78 74 45 6e 64  tStart<=iNextEnd
37890 20 29 3b 0a 20 20 20 20 20 20 69 53 74 61 72 74   );.      iStart
37891 43 68 69 6c 64 20 3d 20 69 4e 65 78 74 53 74 61  Child = iNextSta
37892 72 74 3b 0a 20 20 20 20 20 20 69 45 6e 64 43 68  rt;.      iEndCh
37893 69 6c 64 20 3d 20 69 4e 65 78 74 45 6e 64 3b 0a  ild = iNextEnd;.
37894 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
37895 28 20 69 53 74 61 72 74 43 68 69 6c 64 3c 3d 69  ( iStartChild<=i
37896 4c 65 61 76 65 73 45 6e 64 20 29 3b 0a 20 20 20  LeavesEnd );.   
37897 20 61 73 73 65 72 74 28 20 69 45 6e 64 43 68 69   assert( iEndChi
37898 6c 64 3c 3d 69 4c 65 61 76 65 73 45 6e 64 20 29  ld<=iLeavesEnd )
37899 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 74  ;..    /* Scan t
3789a 68 72 6f 75 67 68 20 74 68 65 20 6c 65 61 66 20  hrough the leaf 
3789b 73 65 67 6d 65 6e 74 73 20 66 6f 72 20 64 6f 63  segments for doc
3789c 6c 69 73 74 73 2e 20 2a 2f 0a 20 20 20 20 72 65  lists. */.    re
3789d 74 75 72 6e 20 6c 6f 61 64 53 65 67 6d 65 6e 74  turn loadSegment
3789e 4c 65 61 76 65 73 28 76 2c 20 69 53 74 61 72 74  Leaves(v, iStart
3789f 43 68 69 6c 64 2c 20 69 45 6e 64 43 68 69 6c 64  Child, iEndChild
378a0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
378a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
378a2 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50  Term, nTerm, isP
378a3 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 7d  refix, out);.  }
378a4 0a 7d 0a 0a 2f 2a 20 43 61 6c 6c 20 6c 6f 61 64  .}../* Call load
378a5 53 65 67 6d 65 6e 74 49 6e 74 28 29 20 74 6f 20  SegmentInt() to 
378a6 63 6f 6c 6c 65 63 74 20 74 68 65 20 64 6f 63 6c  collect the docl
378a7 69 73 74 20 66 6f 72 20 70 54 65 72 6d 2f 6e 54  ist for pTerm/nT
378a8 65 72 6d 2c 20 74 68 65 6e 0a 2a 2a 20 6d 65 72  erm, then.** mer
378a9 67 65 20 69 74 73 20 64 6f 63 6c 69 73 74 20 6f  ge its doclist o
378aa 76 65 72 20 2a 6f 75 74 20 28 61 6e 79 20 64 75  ver *out (any du
378ab 70 6c 69 63 61 74 65 20 64 6f 63 6c 69 73 74 73  plicate doclists
378ac 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 0a 2a   read from the.*
378ad 2a 20 73 65 67 6d 65 6e 74 20 72 6f 6f 74 65 64  * segment rooted
378ae 20 61 74 20 70 44 61 74 61 20 77 69 6c 6c 20 6f   at pData will o
378af 76 65 72 77 72 69 74 65 20 74 68 6f 73 65 20 69  verwrite those i
378b0 6e 20 2a 6f 75 74 29 2e 0a 2a 2f 0a 2f 2a 20 54  n *out)..*/./* T
378b1 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69  ODO(shess) Consi
378b2 64 65 72 20 63 68 61 6e 67 69 6e 67 20 74 68 69  der changing thi
378b3 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  s to determine t
378b4 68 65 20 64 65 70 74 68 20 6f 66 20 74 68 65 0a  he depth of the.
378b5 2a 2a 20 6c 65 61 76 65 73 20 75 73 69 6e 67 20  ** leaves using 
378b6 65 69 74 68 65 72 20 74 68 65 20 66 69 72 73 74  either the first
378b7 20 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 69   characters of i
378b8 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 28 77  nterior nodes (w
378b9 68 65 6e 0a 2a 2a 20 3d 3d 31 2c 20 77 65 27 72  hen.** ==1, we'r
378ba 65 20 6f 6e 65 20 6c 65 76 65 6c 20 61 62 6f 76  e one level abov
378bb 65 20 74 68 65 20 6c 65 61 76 65 73 29 2c 20 6f  e the leaves), o
378bc 72 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72  r the first char
378bd 61 63 74 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20  acter of.** the 
378be 72 6f 6f 74 20 28 77 68 69 63 68 20 77 69 6c 6c  root (which will
378bf 20 64 65 73 63 72 69 62 65 20 74 68 65 20 68 65   describe the he
378c0 69 67 68 74 20 6f 66 20 74 68 65 20 74 72 65 65  ight of the tree
378c1 20 64 69 72 65 63 74 6c 79 29 2e 0a 2a 2a 20 45   directly)..** E
378c2 69 74 68 65 72 20 66 65 65 6c 73 20 73 6f 6d 65  ither feels some
378c3 77 68 61 74 20 74 72 69 63 6b 79 20 74 6f 20 6d  what tricky to m
378c4 65 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  e..*/./* TODO(sh
378c5 65 73 73 29 20 54 68 65 20 63 75 72 72 65 6e 74  ess) The current
378c6 20 6d 65 72 67 65 20 69 73 20 6c 69 6b 65 6c 79   merge is likely
378c7 20 74 6f 20 62 65 20 73 6c 6f 77 20 66 6f 72 20   to be slow for 
378c8 6c 61 72 67 65 0a 2a 2a 20 64 6f 63 6c 69 73 74  large.** doclist
378c9 73 20 28 74 68 6f 75 67 68 20 69 74 20 73 68 6f  s (though it sho
378ca 75 6c 64 20 70 72 6f 63 65 73 73 20 66 72 6f 6d  uld process from
378cb 20 6e 65 77 65 73 74 2f 73 6d 61 6c 6c 65 73 74   newest/smallest
378cc 20 74 6f 0a 2a 2a 20 6f 6c 64 65 73 74 2f 6c 61   to.** oldest/la
378cd 72 67 65 73 74 2c 20 73 6f 20 69 74 20 6d 61 79  rgest, so it may
378ce 20 6e 6f 74 20 62 65 20 74 68 61 74 20 62 61 64   not be that bad
378cf 29 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65 20  ).  It might be 
378d0 75 73 65 66 75 6c 20 74 6f 0a 2a 2a 20 6d 6f 64  useful to.** mod
378d1 69 66 79 20 74 68 69 6e 67 73 20 74 6f 20 61 6c  ify things to al
378d2 6c 6f 77 20 66 6f 72 20 4e 2d 77 61 79 20 6d 65  low for N-way me
378d3 72 67 69 6e 67 2e 20 20 54 68 69 73 20 63 6f 75  rging.  This cou
378d4 6c 64 20 65 69 74 68 65 72 20 62 65 0a 2a 2a 20  ld either be.** 
378d5 77 69 74 68 69 6e 20 61 20 73 65 67 6d 65 6e 74  within a segment
378d6 2c 20 77 69 74 68 20 70 61 69 72 77 69 73 65 20  , with pairwise 
378d7 6d 65 72 67 65 73 20 61 63 72 6f 73 73 20 73 65  merges across se
378d8 67 6d 65 6e 74 73 2c 20 6f 72 20 61 63 72 6f 73  gments, or acros
378d9 73 0a 2a 2a 20 61 6c 6c 20 73 65 67 6d 65 6e 74  s.** all segment
378da 73 20 61 74 20 6f 6e 63 65 2e 0a 2a 2f 0a 73 74  s at once..*/.st
378db 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65 67  atic int loadSeg
378dc 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  ment(fulltext_vt
378dd 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61  ab *v, const cha
378de 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
378df 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
378e0 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
378e1 74 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73  te_int64 iLeaves
378e2 45 6e 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  End,.           
378e3 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
378e4 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
378e5 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73  nt nTerm, int is
378e6 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20  Prefix,.        
378e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44                 D
378e8 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b  ataBuffer *out){
378e9 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 72 65  .  DataBuffer re
378ea 73 75 6c 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  sult;.  int rc;.
378eb 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61  .  assert( nData
378ec 3e 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  >1 );..  /* This
378ed 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 65 76   code should nev
378ee 65 72 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74  er be called wit
378ef 68 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74  h buffered updat
378f0 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  es. */.  assert(
378f1 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   v->nPendingData
378f2 3c 30 20 29 3b 0a 0a 20 20 64 61 74 61 42 75 66  <0 );..  dataBuf
378f3 66 65 72 49 6e 69 74 28 26 72 65 73 75 6c 74 2c  ferInit(&result,
378f4 20 30 29 3b 0a 20 20 72 63 20 3d 20 6c 6f 61 64   0);.  rc = load
378f5 53 65 67 6d 65 6e 74 49 6e 74 28 76 2c 20 70 44  SegmentInt(v, pD
378f6 61 74 61 2c 20 6e 44 61 74 61 2c 20 69 4c 65 61  ata, nData, iLea
378f7 76 65 73 45 6e 64 2c 0a 20 20 20 20 20 20 20 20  vesEnd,.        
378f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
378f9 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
378fa 65 66 69 78 2c 20 26 72 65 73 75 6c 74 29 3b 0a  efix, &result);.
378fb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
378fc 5f 4f 4b 20 26 26 20 72 65 73 75 6c 74 2e 6e 44  _OK && result.nD
378fd 61 74 61 3e 30 20 29 7b 0a 20 20 20 20 69 66 28  ata>0 ){.    if(
378fe 20 6f 75 74 2d 3e 6e 44 61 74 61 3d 3d 30 20 29   out->nData==0 )
378ff 7b 0a 20 20 20 20 20 20 44 61 74 61 42 75 66 66  {.      DataBuff
37900 65 72 20 74 6d 70 20 3d 20 2a 6f 75 74 3b 0a 20  er tmp = *out;. 
37901 20 20 20 20 20 2a 6f 75 74 20 3d 20 72 65 73 75       *out = resu
37902 6c 74 3b 0a 20 20 20 20 20 20 72 65 73 75 6c 74  lt;.      result
37903 20 3d 20 74 6d 70 3b 0a 20 20 20 20 7d 65 6c 73   = tmp;.    }els
37904 65 7b 0a 20 20 20 20 20 20 44 61 74 61 42 75 66  e{.      DataBuf
37905 66 65 72 20 6d 65 72 67 65 64 3b 0a 20 20 20 20  fer merged;.    
37906 20 20 44 4c 52 65 61 64 65 72 20 72 65 61 64 65    DLReader reade
37907 72 73 5b 32 5d 3b 0a 0a 20 20 20 20 20 20 64 6c  rs[2];..      dl
37908 72 49 6e 69 74 28 26 72 65 61 64 65 72 73 5b 30  rInit(&readers[0
37909 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 6f  ], DL_DEFAULT, o
3790a 75 74 2d 3e 70 44 61 74 61 2c 20 6f 75 74 2d 3e  ut->pData, out->
3790b 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 64 6c  nData);.      dl
3790c 72 49 6e 69 74 28 26 72 65 61 64 65 72 73 5b 31  rInit(&readers[1
3790d 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 72  ], DL_DEFAULT, r
3790e 65 73 75 6c 74 2e 70 44 61 74 61 2c 20 72 65 73  esult.pData, res
3790f 75 6c 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20  ult.nData);.    
37910 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
37911 28 26 6d 65 72 67 65 64 2c 20 6f 75 74 2d 3e 6e  (&merged, out->n
37912 44 61 74 61 2b 72 65 73 75 6c 74 2e 6e 44 61 74  Data+result.nDat
37913 61 29 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69 73  a);.      docLis
37914 74 4d 65 72 67 65 28 26 6d 65 72 67 65 64 2c 20  tMerge(&merged, 
37915 72 65 61 64 65 72 73 2c 20 32 29 3b 0a 20 20 20  readers, 2);.   
37916 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
37917 74 72 6f 79 28 6f 75 74 29 3b 0a 20 20 20 20 20  troy(out);.     
37918 20 2a 6f 75 74 20 3d 20 6d 65 72 67 65 64 3b 0a   *out = merged;.
37919 20 20 20 20 20 20 64 6c 72 44 65 73 74 72 6f 79        dlrDestroy
3791a 28 26 72 65 61 64 65 72 73 5b 30 5d 29 3b 0a 20  (&readers[0]);. 
3791b 20 20 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28       dlrDestroy(
3791c 26 72 65 61 64 65 72 73 5b 31 5d 29 3b 0a 20 20  &readers[1]);.  
3791d 20 20 7d 0a 20 20 7d 0a 20 20 64 61 74 61 42 75    }.  }.  dataBu
3791e 66 66 65 72 44 65 73 74 72 6f 79 28 26 72 65 73  fferDestroy(&res
3791f 75 6c 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ult);.  return r
37920 63 3b 0a 7d 0a 0a 2f 2a 20 53 63 61 6e 20 74 68  c;.}../* Scan th
37921 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 6d  e database and m
37922 65 72 67 65 20 74 6f 67 65 74 68 65 72 20 74 68  erge together th
37923 65 20 70 6f 73 74 69 6e 67 20 6c 69 73 74 73 20  e posting lists 
37924 66 6f 72 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20  for the term.** 
37925 69 6e 74 6f 20 2a 6f 75 74 2e 0a 2a 2f 0a 73 74  into *out..*/.st
37926 61 74 69 63 20 69 6e 74 20 74 65 72 6d 53 65 6c  atic int termSel
37927 65 63 74 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f  ect(.  fulltext_
37928 76 74 61 62 20 2a 76 2c 20 0a 20 20 69 6e 74 20  vtab *v, .  int 
37929 69 43 6f 6c 75 6d 6e 2c 0a 20 20 63 6f 6e 73 74  iColumn,.  const
3792a 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
3792b 74 20 6e 54 65 72 6d 2c 20 20 20 20 20 20 20 20  t nTerm,        
3792c 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20       /* Term to 
3792d 71 75 65 72 79 20 66 6f 72 20 2a 2f 0a 20 20 69  query for */.  i
3792e 6e 74 20 69 73 50 72 65 66 69 78 2c 20 20 20 20  nt isPrefix,    
3792f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37930 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
37931 20 66 6f 72 20 61 20 70 72 65 66 69 78 20 73 65   for a prefix se
37932 61 72 63 68 20 2a 2f 0a 20 20 44 6f 63 4c 69 73  arch */.  DocLis
37933 74 54 79 70 65 20 69 54 79 70 65 2c 20 0a 20 20  tType iType, .  
37934 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 20  DataBuffer *out 
37935 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37936 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
37937 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  te results here 
37938 2a 2f 0a 29 7b 0a 20 20 44 61 74 61 42 75 66 66  */.){.  DataBuff
37939 65 72 20 64 6f 63 6c 69 73 74 3b 0a 20 20 73 71  er doclist;.  sq
3793a 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
3793b 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65   int rc = sql_ge
3793c 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53  t_statement(v, S
3793d 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c  EGDIR_SELECT_ALL
3793e 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
3793f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
37940 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
37941 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 73 68 6f  /* This code sho
37942 75 6c 64 20 6e 65 76 65 72 20 62 65 20 63 61 6c  uld never be cal
37943 6c 65 64 20 77 69 74 68 20 62 75 66 66 65 72 65  led with buffere
37944 64 20 75 70 64 61 74 65 73 2e 20 2a 2f 0a 20 20  d updates. */.  
37945 61 73 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64  assert( v->nPend
37946 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a 0a 20 20  ingData<0 );..  
37947 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
37948 64 6f 63 6c 69 73 74 2c 20 30 29 3b 0a 20 20 64  doclist, 0);.  d
37949 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 6f 75  ataBufferInit(ou
3794a 74 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 72 61  t, 0);..  /* Tra
3794b 76 65 72 73 65 20 74 68 65 20 73 65 67 6d 65 6e  verse the segmen
3794c 74 73 20 66 72 6f 6d 20 6f 6c 64 65 73 74 20 74  ts from oldest t
3794d 6f 20 6e 65 77 65 73 74 20 73 6f 20 74 68 61 74  o newest so that
3794e 20 6e 65 77 65 72 20 64 6f 63 6c 69 73 74 0a 20   newer doclist. 
3794f 20 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 66 6f 72   ** elements for
37950 20 67 69 76 65 6e 20 64 6f 63 69 64 73 20 6f 76   given docids ov
37951 65 72 77 72 69 74 65 20 6f 6c 64 65 72 20 65 6c  erwrite older el
37952 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 77  ements..  */.  w
37953 68 69 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69  hile( (rc = sqli
37954 74 65 33 5f 73 74 65 70 28 73 29 29 3d 3d 53 51  te3_step(s))==SQ
37955 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
37956 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
37957 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  a = sqlite3_colu
37958 6d 6e 5f 62 6c 6f 62 28 73 2c 20 32 29 3b 0a 20  mn_blob(s, 2);. 
37959 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44 61     const int nDa
3795a 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ta = sqlite3_col
3795b 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32 29 3b  umn_bytes(s, 2);
3795c 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  .    const sqlit
3795d 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45  e_int64 iLeavesE
3795e 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  nd = sqlite3_col
3795f 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b  umn_int64(s, 1);
37960 0a 20 20 20 20 72 63 20 3d 20 6c 6f 61 64 53 65  .    rc = loadSe
37961 67 6d 65 6e 74 28 76 2c 20 70 44 61 74 61 2c 20  gment(v, pData, 
37962 6e 44 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e  nData, iLeavesEn
37963 64 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  d, pTerm, nTerm,
37964 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20   isPrefix,.     
37965 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37966 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 69  &doclist);.    i
37967 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
37968 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 7d   ) goto err;.  }
37969 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
3796a 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 69 66  E_DONE ){.    if
3796b 28 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 21  ( doclist.nData!
3796c 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  =0 ){.      /* T
3796d 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 6f  ODO(shess) The o
3796e 6c 64 20 74 65 72 6d 5f 73 65 6c 65 63 74 5f 61  ld term_select_a
3796f 6c 6c 28 29 20 63 6f 64 65 20 61 70 70 6c 69 65  ll() code applie
37970 64 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20  d the column.   
37971 20 20 20 2a 2a 20 72 65 73 74 72 69 63 74 20 61     ** restrict a
37972 73 20 77 65 20 6d 65 72 67 65 64 20 73 65 67 6d  s we merged segm
37973 65 6e 74 73 2c 20 6c 65 61 64 69 6e 67 20 74 6f  ents, leading to
37974 20 73 6d 61 6c 6c 65 72 20 62 75 66 66 65 72 73   smaller buffers
37975 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20  ..      ** This 
37976 69 73 20 70 72 6f 62 61 62 6c 79 20 77 6f 72 74  is probably wort
37977 68 77 68 69 6c 65 20 74 6f 20 62 72 69 6e 67 20  hwhile to bring 
37978 62 61 63 6b 2c 20 6f 6e 63 65 20 74 68 65 20 6e  back, once the n
37979 65 77 20 73 74 6f 72 61 67 65 0a 20 20 20 20 20  ew storage.     
3797a 20 2a 2a 20 73 79 73 74 65 6d 20 69 73 20 63 68   ** system is ch
3797b 65 63 6b 65 64 20 69 6e 2e 0a 20 20 20 20 20 20  ecked in..      
3797c 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f  */.      if( iCo
3797d 6c 75 6d 6e 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e  lumn==v->nColumn
3797e 29 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a  ) iColumn = -1;.
3797f 20 20 20 20 20 20 64 6f 63 4c 69 73 74 54 72 69        docListTri
37980 6d 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20 64 6f  m(DL_DEFAULT, do
37981 63 6c 69 73 74 2e 70 44 61 74 61 2c 20 64 6f 63  clist.pData, doc
37982 6c 69 73 74 2e 6e 44 61 74 61 2c 0a 20 20 20 20  list.nData,.    
37983 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 43                iC
37984 6f 6c 75 6d 6e 2c 20 69 54 79 70 65 2c 20 6f 75  olumn, iType, ou
37985 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  t);.    }.    rc
37986 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
37987 7d 0a 0a 20 65 72 72 3a 0a 20 20 64 61 74 61 42  }.. err:.  dataB
37988 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 64 6f  ufferDestroy(&do
37989 63 6c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e  clist);.  return
3798a 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a   rc;.}../*******
3798b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3798c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3798d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3798e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 55 73  *********/./* Us
3798f 65 64 20 74 6f 20 68 6f 6c 64 20 68 61 73 68 74  ed to hold hasht
37990 61 62 6c 65 20 64 61 74 61 20 66 6f 72 20 73 6f  able data for so
37991 72 74 69 6e 67 2e 20 2a 2f 0a 74 79 70 65 64 65  rting. */.typede
37992 66 20 73 74 72 75 63 74 20 54 65 72 6d 44 61 74  f struct TermDat
37993 61 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  a {.  const char
37994 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e   *pTerm;.  int n
37995 54 65 72 6d 3b 0a 20 20 44 4c 43 6f 6c 6c 65 63  Term;.  DLCollec
37996 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 3b  tor *pCollector;
37997 0a 7d 20 54 65 72 6d 44 61 74 61 3b 0a 0a 2f 2a  .} TermData;../*
37998 20 4f 72 64 65 72 73 20 54 65 72 6d 44 61 74 61   Orders TermData
37999 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 73 74 72   elements in str
3799a 63 6d 70 20 66 61 73 68 69 6f 6e 20 28 20 3c 30  cmp fashion ( <0
3799b 20 66 6f 72 20 6c 65 73 73 2d 74 68 61 6e 2c 20   for less-than, 
3799c 30 0a 2a 2a 20 66 6f 72 20 65 71 75 61 6c 2c 20  0.** for equal, 
3799d 3e 30 20 66 6f 72 20 67 72 65 61 74 65 72 2d 74  >0 for greater-t
3799e 68 61 6e 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  han)..*/.static 
3799f 69 6e 74 20 74 65 72 6d 44 61 74 61 43 6d 70 28  int termDataCmp(
379a0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 61 76 2c 20  const void *av, 
379a1 63 6f 6e 73 74 20 76 6f 69 64 20 2a 62 76 29 7b  const void *bv){
379a2 0a 20 20 63 6f 6e 73 74 20 54 65 72 6d 44 61 74  .  const TermDat
379a3 61 20 2a 61 20 3d 20 28 63 6f 6e 73 74 20 54 65  a *a = (const Te
379a4 72 6d 44 61 74 61 20 2a 29 61 76 3b 0a 20 20 63  rmData *)av;.  c
379a5 6f 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 62  onst TermData *b
379a6 20 3d 20 28 63 6f 6e 73 74 20 54 65 72 6d 44 61   = (const TermDa
379a7 74 61 20 2a 29 62 76 3b 0a 20 20 69 6e 74 20 6e  ta *)bv;.  int n
379a8 20 3d 20 61 2d 3e 6e 54 65 72 6d 3c 62 2d 3e 6e   = a->nTerm<b->n
379a9 54 65 72 6d 20 3f 20 61 2d 3e 6e 54 65 72 6d 20  Term ? a->nTerm 
379aa 3a 20 62 2d 3e 6e 54 65 72 6d 3b 0a 20 20 69 6e  : b->nTerm;.  in
379ab 74 20 63 20 3d 20 6d 65 6d 63 6d 70 28 61 2d 3e  t c = memcmp(a->
379ac 70 54 65 72 6d 2c 20 62 2d 3e 70 54 65 72 6d 2c  pTerm, b->pTerm,
379ad 20 6e 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20   n);.  if( c!=0 
379ae 29 20 72 65 74 75 72 6e 20 63 3b 0a 20 20 72 65  ) return c;.  re
379af 74 75 72 6e 20 61 2d 3e 6e 54 65 72 6d 2d 62 2d  turn a->nTerm-b-
379b0 3e 6e 54 65 72 6d 3b 0a 7d 0a 0a 2f 2a 20 4f 72  >nTerm;.}../* Or
379b1 64 65 72 20 70 54 65 72 6d 73 20 64 61 74 61 20  der pTerms data 
379b2 62 79 20 74 65 72 6d 2c 20 74 68 65 6e 20 77 72  by term, then wr
379b3 69 74 65 20 61 20 6e 65 77 20 6c 65 76 65 6c 20  ite a new level 
379b4 30 20 73 65 67 6d 65 6e 74 20 75 73 69 6e 67 0a  0 segment using.
379b5 2a 2a 20 4c 65 61 66 57 72 69 74 65 72 2e 0a 2a  ** LeafWriter..*
379b6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69  /.static int wri
379b7 74 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28 66 75  teZeroSegment(fu
379b8 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
379b9 66 74 73 33 48 61 73 68 20 2a 70 54 65 72 6d 73  fts3Hash *pTerms
379ba 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  ){.  fts3HashEle
379bb 6d 20 2a 65 3b 0a 20 20 69 6e 74 20 69 64 78 2c  m *e;.  int idx,
379bc 20 72 63 2c 20 69 2c 20 6e 3b 0a 20 20 54 65 72   rc, i, n;.  Ter
379bd 6d 44 61 74 61 20 2a 70 44 61 74 61 3b 0a 20 20  mData *pData;.  
379be 4c 65 61 66 57 72 69 74 65 72 20 77 72 69 74 65  LeafWriter write
379bf 72 3b 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  r;.  DataBuffer 
379c0 64 6c 3b 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d  dl;..  /* Determ
379c1 69 6e 65 20 74 68 65 20 6e 65 78 74 20 69 6e 64  ine the next ind
379c2 65 78 20 61 74 20 6c 65 76 65 6c 20 30 2c 20 6d  ex at level 0, m
379c3 65 72 67 69 6e 67 20 61 73 20 6e 65 63 65 73 73  erging as necess
379c4 61 72 79 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73  ary. */.  rc = s
379c5 65 67 64 69 72 4e 65 78 74 49 6e 64 65 78 28 76  egdirNextIndex(v
379c6 2c 20 30 2c 20 26 69 64 78 29 3b 0a 20 20 69 66  , 0, &idx);.  if
379c7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
379c8 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
379c9 6e 20 3d 20 66 74 73 33 48 61 73 68 43 6f 75 6e  n = fts3HashCoun
379ca 74 28 70 54 65 72 6d 73 29 3b 0a 20 20 70 44 61  t(pTerms);.  pDa
379cb 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ta = sqlite3_mal
379cc 6c 6f 63 28 6e 2a 73 69 7a 65 6f 66 28 54 65 72  loc(n*sizeof(Ter
379cd 6d 44 61 74 61 29 29 3b 0a 0a 20 20 66 6f 72 28  mData));..  for(
379ce 69 20 3d 20 30 2c 20 65 20 3d 20 66 74 73 33 48  i = 0, e = fts3H
379cf 61 73 68 46 69 72 73 74 28 70 54 65 72 6d 73 29  ashFirst(pTerms)
379d0 3b 20 65 3b 20 69 2b 2b 2c 20 65 20 3d 20 66 74  ; e; i++, e = ft
379d1 73 33 48 61 73 68 4e 65 78 74 28 65 29 29 7b 0a  s3HashNext(e)){.
379d2 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 20      assert( i<n 
379d3 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 5d 2e  );.    pData[i].
379d4 70 54 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68  pTerm = fts3Hash
379d5 4b 65 79 28 65 29 3b 0a 20 20 20 20 70 44 61 74  Key(e);.    pDat
379d6 61 5b 69 5d 2e 6e 54 65 72 6d 20 3d 20 66 74 73  a[i].nTerm = fts
379d7 33 48 61 73 68 4b 65 79 73 69 7a 65 28 65 29 3b  3HashKeysize(e);
379d8 0a 20 20 20 20 70 44 61 74 61 5b 69 5d 2e 70 43  .    pData[i].pC
379d9 6f 6c 6c 65 63 74 6f 72 20 3d 20 66 74 73 33 48  ollector = fts3H
379da 61 73 68 44 61 74 61 28 65 29 3b 0a 20 20 7d 0a  ashData(e);.  }.
379db 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 20 29    assert( i==n )
379dc 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  ;..  /* TODO(she
379dd 73 73 29 20 53 68 6f 75 6c 64 20 77 65 20 61 6c  ss) Should we al
379de 6c 6f 77 20 75 73 65 72 2d 64 65 66 69 6e 65 64  low user-defined
379df 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
379e0 6e 63 65 73 2c 0a 20 20 2a 2a 20 68 65 72 65 3f  nces,.  ** here?
379e1 20 20 49 20 74 68 69 6e 6b 20 77 65 20 6f 6e 6c    I think we onl
379e2 79 20 6e 65 65 64 20 74 68 61 74 20 6f 6e 63 65  y need that once
379e3 20 77 65 20 73 75 70 70 6f 72 74 20 70 72 65 66   we support pref
379e4 69 78 20 73 65 61 72 63 68 65 73 2e 0a 20 20 2a  ix searches..  *
379e5 2f 0a 20 20 69 66 28 20 6e 3e 31 20 29 20 71 73  /.  if( n>1 ) qs
379e6 6f 72 74 28 70 44 61 74 61 2c 20 6e 2c 20 73 69  ort(pData, n, si
379e7 7a 65 6f 66 28 2a 70 44 61 74 61 29 2c 20 74 65  zeof(*pData), te
379e8 72 6d 44 61 74 61 43 6d 70 29 3b 0a 0a 20 20 2f  rmDataCmp);..  /
379e9 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 52 65  * TODO(shess) Re
379ea 66 61 63 74 6f 72 20 73 6f 20 74 68 61 74 20 77  factor so that w
379eb 65 20 63 61 6e 20 77 72 69 74 65 20 64 69 72 65  e can write dire
379ec 63 74 6c 79 20 74 6f 20 74 68 65 20 73 65 67 6d  ctly to the segm
379ed 65 6e 74 0a 20 20 2a 2a 20 44 61 74 61 42 75 66  ent.  ** DataBuf
379ee 66 65 72 2c 20 61 73 20 68 61 70 70 65 6e 73 20  fer, as happens 
379ef 66 6f 72 20 73 65 67 6d 65 6e 74 20 6d 65 72 67  for segment merg
379f0 65 73 2e 0a 20 20 2a 2f 0a 20 20 6c 65 61 66 57  es..  */.  leafW
379f1 72 69 74 65 72 49 6e 69 74 28 30 2c 20 69 64 78  riterInit(0, idx
379f2 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20 64 61  , &writer);.  da
379f3 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 64 6c  taBufferInit(&dl
379f4 2c 20 30 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  , 0);.  for(i=0;
379f5 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<n; i++){.    
379f6 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
379f7 26 64 6c 29 3b 0a 20 20 20 20 64 6c 63 41 64 64  &dl);.    dlcAdd
379f8 44 6f 63 6c 69 73 74 28 70 44 61 74 61 5b 69 5d  Doclist(pData[i]
379f9 2e 70 43 6f 6c 6c 65 63 74 6f 72 2c 20 26 64 6c  .pCollector, &dl
379fa 29 3b 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 66  );.    rc = leaf
379fb 57 72 69 74 65 72 53 74 65 70 28 76 2c 20 26 77  WriterStep(v, &w
379fc 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
379fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
379fe 44 61 74 61 5b 69 5d 2e 70 54 65 72 6d 2c 20 70  Data[i].pTerm, p
379ff 44 61 74 61 5b 69 5d 2e 6e 54 65 72 6d 2c 20 64  Data[i].nTerm, d
37a00 6c 2e 70 44 61 74 61 2c 20 64 6c 2e 6e 44 61 74  l.pData, dl.nDat
37a01 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  a);.    if( rc!=
37a02 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
37a03 20 65 72 72 3b 0a 20 20 7d 0a 20 20 72 63 20 3d   err;.  }.  rc =
37a04 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c   leafWriterFinal
37a05 69 7a 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b  ize(v, &writer);
37a06 0a 0a 20 65 72 72 3a 0a 20 20 64 61 74 61 42 75  .. err:.  dataBu
37a07 66 66 65 72 44 65 73 74 72 6f 79 28 26 64 6c 29  fferDestroy(&dl)
37a08 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
37a09 28 70 44 61 74 61 29 3b 0a 20 20 6c 65 61 66 57  (pData);.  leafW
37a0a 72 69 74 65 72 44 65 73 74 72 6f 79 28 26 77 72  riterDestroy(&wr
37a0b 69 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  iter);.  return 
37a0c 72 63 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70 65 6e  rc;.}../* If pen
37a0d 64 69 6e 67 54 65 72 6d 73 20 68 61 73 20 64 61  dingTerms has da
37a0e 74 61 2c 20 66 72 65 65 20 69 74 2e 20 2a 2f 0a  ta, free it. */.
37a0f 73 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72  static int clear
37a10 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c  PendingTerms(ful
37a11 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a  ltext_vtab *v){.
37a12 20 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e    if( v->nPendin
37a13 67 44 61 74 61 3e 3d 30 20 29 7b 0a 20 20 20 20  gData>=0 ){.    
37a14 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b  fts3HashElem *e;
37a15 0a 20 20 20 20 66 6f 72 28 65 3d 66 74 73 33 48  .    for(e=fts3H
37a16 61 73 68 46 69 72 73 74 28 26 76 2d 3e 70 65 6e  ashFirst(&v->pen
37a17 64 69 6e 67 54 65 72 6d 73 29 3b 20 65 3b 20 65  dingTerms); e; e
37a18 3d 66 74 73 33 48 61 73 68 4e 65 78 74 28 65 29  =fts3HashNext(e)
37a19 29 7b 0a 20 20 20 20 20 20 64 6c 63 44 65 6c 65  ){.      dlcDele
37a1a 74 65 28 66 74 73 33 48 61 73 68 44 61 74 61 28  te(fts3HashData(
37a1b 65 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  e));.    }.    f
37a1c 74 73 33 48 61 73 68 43 6c 65 61 72 28 26 76 2d  ts3HashClear(&v-
37a1d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29 3b 0a  >pendingTerms);.
37a1e 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44      v->nPendingD
37a1f 61 74 61 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20  ata = -1;.  }.  
37a20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
37a21 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69  ;.}../* If pendi
37a22 6e 67 54 65 72 6d 73 20 68 61 73 20 64 61 74 61  ngTerms has data
37a23 2c 20 66 6c 75 73 68 20 69 74 20 74 6f 20 61 20  , flush it to a 
37a24 6c 65 76 65 6c 2d 7a 65 72 6f 20 73 65 67 6d 65  level-zero segme
37a25 6e 74 2c 20 61 6e 64 0a 2a 2a 20 66 72 65 65 20  nt, and.** free 
37a26 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  it..*/.static in
37a27 74 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  t flushPendingTe
37a28 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rms(fulltext_vta
37a29 62 20 2a 76 29 7b 0a 20 20 69 66 28 20 76 2d 3e  b *v){.  if( v->
37a2a 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e 3d 30 20  nPendingData>=0 
37a2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
37a2c 77 72 69 74 65 5a 65 72 6f 53 65 67 6d 65 6e 74  writeZeroSegment
37a2d 28 76 2c 20 26 76 2d 3e 70 65 6e 64 69 6e 67 54  (v, &v->pendingT
37a2e 65 72 6d 73 29 3b 0a 20 20 20 20 69 66 28 20 72  erms);.    if( r
37a2f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 63  c==SQLITE_OK ) c
37a30 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73  learPendingTerms
37a31 28 76 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  (v);.    return 
37a32 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  rc;.  }.  return
37a33 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
37a34 2a 20 49 66 20 70 65 6e 64 69 6e 67 54 65 72 6d  * If pendingTerm
37a35 73 20 69 73 20 22 74 6f 6f 20 62 69 67 22 2c 20  s is "too big", 
37a36 6f 72 20 64 6f 63 69 64 20 69 73 20 6f 75 74 20  or docid is out 
37a37 6f 66 20 6f 72 64 65 72 2c 20 66 6c 75 73 68 20  of order, flush 
37a38 69 74 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73  it..** Regardles
37a39 73 2c 20 62 65 20 63 65 72 74 61 69 6e 20 74 68  s, be certain th
37a3a 61 74 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  at pendingTerms 
37a3b 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 66  is initialized f
37a3c 6f 72 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  or use..*/.stati
37a3d 63 20 69 6e 74 20 69 6e 69 74 50 65 6e 64 69 6e  c int initPendin
37a3e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f  gTerms(fulltext_
37a3f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f  vtab *v, sqlite_
37a40 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20  int64 iDocid){. 
37a41 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
37a42 45 78 70 6c 6f 72 65 20 77 68 65 74 68 65 72 20  Explore whether 
37a43 70 61 72 74 69 61 6c 6c 79 20 66 6c 75 73 68 69  partially flushi
37a44 6e 67 20 74 68 65 20 62 75 66 66 65 72 20 6f 6e  ng the buffer on
37a45 0a 20 20 2a 2a 20 66 6f 72 63 65 64 2d 66 6c 75  .  ** forced-flu
37a46 73 68 20 77 6f 75 6c 64 20 70 72 6f 76 69 64 65  sh would provide
37a47 20 62 65 74 74 65 72 20 70 65 72 66 6f 72 6d 61   better performa
37a48 6e 63 65 2e 20 20 49 20 73 75 73 70 65 63 74 20  nce.  I suspect 
37a49 74 68 61 74 20 69 66 0a 20 20 2a 2a 20 77 65 20  that if.  ** we 
37a4a 6f 72 64 65 72 65 64 20 74 68 65 20 64 6f 63 6c  ordered the docl
37a4b 69 73 74 73 20 62 79 20 73 69 7a 65 20 61 6e 64  ists by size and
37a4c 20 66 6c 75 73 68 65 64 20 74 68 65 20 6c 61 72   flushed the lar
37a4d 67 65 73 74 20 75 6e 74 69 6c 20 74 68 65 0a 20  gest until the. 
37a4e 20 2a 2a 20 62 75 66 66 65 72 20 77 61 73 20 68   ** buffer was h
37a4f 61 6c 66 20 65 6d 70 74 79 2c 20 74 68 61 74 20  alf empty, that 
37a50 77 6f 75 6c 64 20 6c 65 74 20 74 68 65 20 6c 65  would let the le
37a51 73 73 20 66 72 65 71 75 65 6e 74 20 74 65 72 6d  ss frequent term
37a52 73 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 65 20  s.  ** generate 
37a53 6c 6f 6e 67 65 72 20 64 6f 63 6c 69 73 74 73 2e  longer doclists.
37a54 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 44 6f 63  .  */.  if( iDoc
37a55 69 64 3c 3d 76 2d 3e 69 50 72 65 76 44 6f 63 69  id<=v->iPrevDoci
37a56 64 20 7c 7c 20 76 2d 3e 6e 50 65 6e 64 69 6e 67  d || v->nPending
37a57 44 61 74 61 3e 6b 50 65 6e 64 69 6e 67 54 68 72  Data>kPendingThr
37a58 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 69 6e  eshold ){.    in
37a59 74 20 72 63 20 3d 20 66 6c 75 73 68 50 65 6e 64  t rc = flushPend
37a5a 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20  ingTerms(v);.   
37a5b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
37a5c 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
37a5d 20 20 7d 0a 20 20 69 66 28 20 76 2d 3e 6e 50 65    }.  if( v->nPe
37a5e 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 7b 0a 20  ndingData<0 ){. 
37a5f 20 20 20 66 74 73 33 48 61 73 68 49 6e 69 74 28     fts3HashInit(
37a60 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73  &v->pendingTerms
37a61 2c 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49  , FTS3_HASH_STRI
37a62 4e 47 2c 20 31 29 3b 0a 20 20 20 20 76 2d 3e 6e  NG, 1);.    v->n
37a63 50 65 6e 64 69 6e 67 44 61 74 61 20 3d 20 30 3b  PendingData = 0;
37a64 0a 20 20 7d 0a 20 20 76 2d 3e 69 50 72 65 76 44  .  }.  v->iPrevD
37a65 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a 20  ocid = iDocid;. 
37a66 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
37a67 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75  K;.}../* This fu
37a68 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
37a69 73 20 74 68 65 20 78 55 70 64 61 74 65 20 63 61  s the xUpdate ca
37a6a 6c 6c 62 61 63 6b 3b 20 69 74 20 69 73 20 74 68  llback; it is th
37a6b 65 20 74 6f 70 2d 6c 65 76 65 6c 20 65 6e 74 72  e top-level entr
37a6c 79 0a 20 2a 20 70 6f 69 6e 74 20 66 6f 72 20 69  y. * point for i
37a6d 6e 73 65 72 74 69 6e 67 2c 20 64 65 6c 65 74 69  nserting, deleti
37a6e 6e 67 20 6f 72 20 75 70 64 61 74 69 6e 67 20 61  ng or updating a
37a6f 20 72 6f 77 20 69 6e 20 61 20 66 75 6c 6c 2d 74   row in a full-t
37a70 65 78 74 20 74 61 62 6c 65 2e 20 2a 2f 0a 73 74  ext table. */.st
37a71 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
37a72 74 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f  tUpdate(sqlite3_
37a73 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74  vtab *pVtab, int
37a74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76   nArg, sqlite3_v
37a75 61 6c 75 65 20 2a 2a 70 70 41 72 67 2c 0a 20 20  alue **ppArg,.  
37a76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37a77 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
37a78 6e 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20  nt64 *pRowid){. 
37a79 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
37a7a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74  v = (fulltext_vt
37a7b 61 62 20 2a 29 20 70 56 74 61 62 3b 0a 20 20 69  ab *) pVtab;.  i
37a7c 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52 41  nt rc;..  FTSTRA
37a7d 43 45 28 28 22 46 54 53 33 20 55 70 64 61 74 65  CE(("FTS3 Update
37a7e 20 25 70 5c 6e 22 2c 20 70 56 74 61 62 29 29 3b   %p\n", pVtab));
37a7f 0a 0a 20 20 69 66 28 20 6e 41 72 67 3c 32 20 29  ..  if( nArg<2 )
37a80 7b 0a 20 20 20 20 72 63 20 3d 20 69 6e 64 65 78  {.    rc = index
37a81 5f 64 65 6c 65 74 65 28 76 2c 20 73 71 6c 69 74  _delete(v, sqlit
37a82 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70  e3_value_int64(p
37a83 70 41 72 67 5b 30 5d 29 29 3b 0a 20 20 20 20 69  pArg[0]));.    i
37a84 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
37a85 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
37a86 77 65 20 6a 75 73 74 20 64 65 6c 65 74 65 64 20  we just deleted 
37a87 74 68 65 20 6c 61 73 74 20 72 6f 77 20 69 6e 20  the last row in 
37a88 74 68 65 20 74 61 62 6c 65 2c 20 63 6c 65 61 72  the table, clear
37a89 20 6f 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a   out the.      *
37a8a 2a 20 69 6e 64 65 78 20 64 61 74 61 2e 0a 20 20  * index data..  
37a8b 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20      */.      rc 
37a8c 3d 20 63 6f 6e 74 65 6e 74 5f 65 78 69 73 74 73  = content_exists
37a8d 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  (v);.      if( r
37a8e 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
37a8f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
37a90 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
37a91 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c  else if( rc==SQL
37a92 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
37a93 20 20 20 20 2f 2a 20 43 6c 65 61 72 20 74 68 65      /* Clear the
37a94 20 70 65 6e 64 69 6e 67 20 74 65 72 6d 73 20 73   pending terms s
37a95 6f 20 77 65 20 64 6f 6e 27 74 20 66 6c 75 73 68  o we don't flush
37a96 20 61 20 75 73 65 6c 65 73 73 20 6c 65 76 65 6c   a useless level
37a97 2d 30 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65  -0.        ** se
37a98 67 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 74  gment when the t
37a99 72 61 6e 73 61 63 74 69 6f 6e 20 63 6c 6f 73 65  ransaction close
37a9a 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  s..        */.  
37a9b 20 20 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72        rc = clear
37a9c 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b  PendingTerms(v);
37a9d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
37a9e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
37a9f 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 65 67          rc = seg
37aa0 64 69 72 5f 64 65 6c 65 74 65 5f 61 6c 6c 28 76  dir_delete_all(v
37aa1 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
37aa2 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 65     }.    }.  } e
37aa3 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f  lse if( sqlite3_
37aa4 76 61 6c 75 65 5f 74 79 70 65 28 70 70 41 72 67  value_type(ppArg
37aa5 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 4e  [0]) != SQLITE_N
37aa6 55 4c 4c 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e  ULL ){.    /* An
37aa7 20 75 70 64 61 74 65 3a 0a 20 20 20 20 20 2a 20   update:.     * 
37aa8 70 70 41 72 67 5b 30 5d 20 3d 20 6f 6c 64 20 72  ppArg[0] = old r
37aa9 6f 77 69 64 0a 20 20 20 20 20 2a 20 70 70 41 72  owid.     * ppAr
37aaa 67 5b 31 5d 20 3d 20 6e 65 77 20 72 6f 77 69 64  g[1] = new rowid
37aab 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2e  .     * ppArg[2.
37aac 2e 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d  .2+v->nColumn-1]
37aad 20 3d 20 76 61 6c 75 65 73 0a 20 20 20 20 20 2a   = values.     *
37aae 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c   ppArg[2+v->nCol
37aaf 75 6d 6e 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72  umn] = value for
37ab0 20 6d 61 67 69 63 20 63 6f 6c 75 6d 6e 20 28 77   magic column (w
37ab1 65 20 69 67 6e 6f 72 65 20 74 68 69 73 29 0a 20  e ignore this). 
37ab2 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2b 76 2d      * ppArg[2+v-
37ab3 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 20 3d 20 76 61  >nColumn+1] = va
37ab4 6c 75 65 20 66 6f 72 20 64 6f 63 69 64 0a 20 20  lue for docid.  
37ab5 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
37ab6 5f 69 6e 74 36 34 20 72 6f 77 69 64 20 3d 20 73  _int64 rowid = s
37ab7 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
37ab8 36 34 28 70 70 41 72 67 5b 30 5d 29 3b 0a 20 20  64(ppArg[0]);.  
37ab9 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
37aba 6c 75 65 5f 74 79 70 65 28 70 70 41 72 67 5b 31  lue_type(ppArg[1
37abb 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) != SQLITE_INT
37abc 45 47 45 52 20 7c 7c 0a 20 20 20 20 20 20 20 20  EGER ||.        
37abd 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
37abe 74 36 34 28 70 70 41 72 67 5b 31 5d 29 20 21 3d  t64(ppArg[1]) !=
37abf 20 72 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20   rowid ){.      
37ac0 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
37ac1 52 3b 20 20 2f 2a 20 77 65 20 64 6f 6e 27 74 20  R;  /* we don't 
37ac2 61 6c 6c 6f 77 20 63 68 61 6e 67 69 6e 67 20 74  allow changing t
37ac3 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20  he rowid */.    
37ac4 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65  }else if( sqlite
37ac5 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 70 41  3_value_type(ppA
37ac6 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b  rg[2+v->nColumn+
37ac7 31 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e  1]) != SQLITE_IN
37ac8 54 45 47 45 52 20 7c 7c 0a 20 20 20 20 20 20 20  TEGER ||.       
37ac9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76         sqlite3_v
37aca 61 6c 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67  alue_int64(ppArg
37acb 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d  [2+v->nColumn+1]
37acc 29 20 21 3d 20 72 6f 77 69 64 20 29 7b 0a 20 20  ) != rowid ){.  
37acd 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
37ace 45 52 52 4f 52 3b 20 20 2f 2a 20 77 65 20 64 6f  ERROR;  /* we do
37acf 6e 27 74 20 61 6c 6c 6f 77 20 63 68 61 6e 67 69  n't allow changi
37ad0 6e 67 20 74 68 65 20 64 6f 63 69 64 20 2a 2f 0a  ng the docid */.
37ad1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
37ad2 20 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 32   assert( nArg==2
37ad3 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a  +v->nColumn+2);.
37ad4 20 20 20 20 20 20 72 63 20 3d 20 69 6e 64 65 78        rc = index
37ad5 5f 75 70 64 61 74 65 28 76 2c 20 72 6f 77 69 64  _update(v, rowid
37ad6 2c 20 26 70 70 41 72 67 5b 32 5d 29 3b 0a 20 20  , &ppArg[2]);.  
37ad7 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20    }.  } else {. 
37ad8 20 20 20 2f 2a 20 41 6e 20 69 6e 73 65 72 74 3a     /* An insert:
37ad9 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b 31 5d  .     * ppArg[1]
37ada 20 3d 20 72 65 71 75 65 73 74 65 64 20 72 6f 77   = requested row
37adb 69 64 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b  id.     * ppArg[
37adc 32 2e 2e 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2d  2..2+v->nColumn-
37add 31 5d 20 3d 20 76 61 6c 75 65 73 0a 20 20 20 20  1] = values.    
37ade 20 2a 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43   * ppArg[2+v->nC
37adf 6f 6c 75 6d 6e 5d 20 3d 20 76 61 6c 75 65 20 66  olumn] = value f
37ae0 6f 72 20 6d 61 67 69 63 20 63 6f 6c 75 6d 6e 20  or magic column 
37ae1 28 77 65 20 69 67 6e 6f 72 65 20 74 68 69 73 29  (we ignore this)
37ae2 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2b  .     * ppArg[2+
37ae3 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 20 3d 20  v->nColumn+1] = 
37ae4 76 61 6c 75 65 20 66 6f 72 20 64 6f 63 69 64 0a  value for docid.
37ae5 20 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69       */.    sqli
37ae6 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 71 75  te3_value *pRequ
37ae7 65 73 74 44 6f 63 69 64 20 3d 20 70 70 41 72 67  estDocid = ppArg
37ae8 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d  [2+v->nColumn+1]
37ae9 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 41  ;.    assert( nA
37aea 72 67 3d 3d 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e  rg==2+v->nColumn
37aeb 2b 32 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  +2);.    if( SQL
37aec 49 54 45 5f 4e 55 4c 4c 20 21 3d 20 73 71 6c 69  ITE_NULL != sqli
37aed 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70  te3_value_type(p
37aee 52 65 71 75 65 73 74 44 6f 63 69 64 29 20 26 26  RequestDocid) &&
37aef 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  .        SQLITE_
37af0 4e 55 4c 4c 20 21 3d 20 73 71 6c 69 74 65 33 5f  NULL != sqlite3_
37af1 76 61 6c 75 65 5f 74 79 70 65 28 70 70 41 72 67  value_type(ppArg
37af2 5b 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  [1]) ){.      /*
37af3 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e   TODO(shess) Con
37af4 73 69 64 65 72 20 61 6c 6c 6f 77 69 6e 67 20 74  sider allowing t
37af5 68 69 73 20 74 6f 20 77 6f 72 6b 20 69 66 20 74  his to work if t
37af6 68 65 20 76 61 6c 75 65 73 20 61 72 65 0a 20 20  he values are.  
37af7 20 20 20 20 2a 2a 20 69 64 65 6e 74 69 63 61 6c      ** identical
37af8 2e 20 20 49 27 6d 20 69 6e 63 6c 69 6e 65 64 20  .  I'm inclined 
37af9 74 6f 20 64 69 73 63 6f 75 72 61 67 65 20 74 68  to discourage th
37afa 61 74 20 75 73 61 67 65 2c 20 74 68 6f 75 67 68  at usage, though
37afb 2c 0a 20 20 20 20 20 20 2a 2a 20 67 69 76 65 6e  ,.      ** given
37afc 20 74 68 61 74 20 62 6f 74 68 20 72 6f 77 69 64   that both rowid
37afd 20 61 6e 64 20 64 6f 63 69 64 20 61 72 65 20 73   and docid are s
37afe 70 65 63 69 61 6c 20 63 6f 6c 75 6d 6e 73 2e 20  pecial columns. 
37aff 20 42 65 74 74 65 72 0a 20 20 20 20 20 20 2a 2a   Better.      **
37b00 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 64 65 66   would be to def
37b01 69 6e 65 20 6f 6e 65 20 6f 72 20 74 68 65 20 6f  ine one or the o
37b02 74 68 65 72 20 61 73 20 74 68 65 20 64 65 66 61  ther as the defa
37b03 75 6c 74 20 77 69 6e 6e 65 72 2c 0a 20 20 20 20  ult winner,.    
37b04 20 20 2a 2a 20 62 75 74 20 73 68 6f 75 6c 64 20    ** but should 
37b05 69 74 20 62 65 20 66 74 73 33 2d 63 65 6e 74 72  it be fts3-centr
37b06 69 63 20 28 64 6f 63 69 64 29 20 6f 72 20 53 51  ic (docid) or SQ
37b07 4c 69 74 65 2d 63 65 6e 74 72 69 63 0a 20 20 20  Lite-centric.   
37b08 20 20 20 2a 2a 20 28 72 6f 77 69 64 29 3f 0a 20     ** (rowid)?. 
37b09 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63       */.      rc
37b0a 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
37b0b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
37b0c 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c    if( SQLITE_NUL
37b0d 4c 20 3d 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  L == sqlite3_val
37b0e 75 65 5f 74 79 70 65 28 70 52 65 71 75 65 73 74  ue_type(pRequest
37b0f 44 6f 63 69 64 29 20 29 7b 0a 20 20 20 20 20 20  Docid) ){.      
37b10 20 20 70 52 65 71 75 65 73 74 44 6f 63 69 64 20    pRequestDocid 
37b11 3d 20 70 70 41 72 67 5b 31 5d 3b 0a 20 20 20 20  = ppArg[1];.    
37b12 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 69    }.      rc = i
37b13 6e 64 65 78 5f 69 6e 73 65 72 74 28 76 2c 20 70  ndex_insert(v, p
37b14 52 65 71 75 65 73 74 44 6f 63 69 64 2c 20 26 70  RequestDocid, &p
37b15 70 41 72 67 5b 32 5d 2c 20 70 52 6f 77 69 64 29  pArg[2], pRowid)
37b16 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
37b17 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61  eturn rc;.}..sta
37b18 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
37b19 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 76 74 61  Sync(sqlite3_vta
37b1a 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 46 54 53  b *pVtab){.  FTS
37b1b 54 52 41 43 45 28 28 22 46 54 53 33 20 78 53 79  TRACE(("FTS3 xSy
37b1c 6e 63 28 29 5c 6e 22 29 29 3b 0a 20 20 72 65 74  nc()\n"));.  ret
37b1d 75 72 6e 20 66 6c 75 73 68 50 65 6e 64 69 6e 67  urn flushPending
37b1e 54 65 72 6d 73 28 28 66 75 6c 6c 74 65 78 74 5f  Terms((fulltext_
37b1f 76 74 61 62 20 2a 29 70 56 74 61 62 29 3b 0a 7d  vtab *)pVtab);.}
37b20 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  ..static int ful
37b21 6c 74 65 78 74 42 65 67 69 6e 28 73 71 6c 69 74  ltextBegin(sqlit
37b22 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b  e3_vtab *pVtab){
37b23 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
37b24 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *v = (fulltext_
37b25 76 74 61 62 20 2a 29 20 70 56 74 61 62 3b 0a 20  vtab *) pVtab;. 
37b26 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33   FTSTRACE(("FTS3
37b27 20 78 42 65 67 69 6e 28 29 5c 6e 22 29 29 3b 0a   xBegin()\n"));.
37b28 0a 20 20 2f 2a 20 41 6e 79 20 62 75 66 66 65 72  .  /* Any buffer
37b29 65 64 20 75 70 64 61 74 65 73 20 73 68 6f 75 6c  ed updates shoul
37b2a 64 20 68 61 76 65 20 62 65 65 6e 20 63 6c 65 61  d have been clea
37b2b 72 65 64 20 62 79 20 74 68 65 20 70 72 65 76 69  red by the previ
37b2c 6f 75 73 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  ous.  ** transac
37b2d 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  tion..  */.  ass
37b2e 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67  ert( v->nPending
37b2f 44 61 74 61 3c 30 20 29 3b 0a 20 20 72 65 74 75  Data<0 );.  retu
37b30 72 6e 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54  rn clearPendingT
37b31 65 72 6d 73 28 76 29 3b 0a 7d 0a 0a 73 74 61 74  erms(v);.}..stat
37b32 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43  ic int fulltextC
37b33 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 5f 76 74  ommit(sqlite3_vt
37b34 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 66 75  ab *pVtab){.  fu
37b35 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
37b36 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20   (fulltext_vtab 
37b37 2a 29 20 70 56 74 61 62 3b 0a 20 20 46 54 53 54  *) pVtab;.  FTST
37b38 52 41 43 45 28 28 22 46 54 53 33 20 78 43 6f 6d  RACE(("FTS3 xCom
37b39 6d 69 74 28 29 5c 6e 22 29 29 3b 0a 0a 20 20 2f  mit()\n"));..  /
37b3a 2a 20 42 75 66 66 65 72 65 64 20 75 70 64 61 74  * Buffered updat
37b3b 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62  es should have b
37b3c 65 65 6e 20 63 6c 65 61 72 65 64 20 62 79 20 66  een cleared by f
37b3d 75 6c 6c 74 65 78 74 53 79 6e 63 28 29 2e 20 2a  ulltextSync(). *
37b3e 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e  /.  assert( v->n
37b3f 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b  PendingData<0 );
37b40 0a 20 20 72 65 74 75 72 6e 20 63 6c 65 61 72 50  .  return clearP
37b41 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a  endingTerms(v);.
37b42 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  }..static int fu
37b43 6c 6c 74 65 78 74 52 6f 6c 6c 62 61 63 6b 28 73  lltextRollback(s
37b44 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
37b45 61 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45 28  ab){.  FTSTRACE(
37b46 28 22 46 54 53 33 20 78 52 6f 6c 6c 62 61 63 6b  ("FTS3 xRollback
37b47 28 29 5c 6e 22 29 29 3b 0a 20 20 72 65 74 75 72  ()\n"));.  retur
37b48 6e 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65  n clearPendingTe
37b49 72 6d 73 28 28 66 75 6c 6c 74 65 78 74 5f 76 74  rms((fulltext_vt
37b4a 61 62 20 2a 29 70 56 74 61 62 29 3b 0a 7d 0a 0a  ab *)pVtab);.}..
37b4b 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
37b4c 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 6e 69 70  tion of the snip
37b4d 70 65 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  pet() function f
37b4e 6f 72 20 46 54 53 33 0a 2a 2f 0a 73 74 61 74 69  or FTS3.*/.stati
37b4f 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74 46 75  c void snippetFu
37b50 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
37b51 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c  ntext *pContext,
37b52 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
37b53 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
37b54 72 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78  rgv.){.  fulltex
37b55 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  t_cursor *pCurso
37b56 72 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20  r;.  if( argc<1 
37b57 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
37b58 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
37b59 70 65 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c  pe(argv[0])!=SQL
37b5a 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20  ITE_BLOB ||.    
37b5b 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
37b5c 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d  bytes(argv[0])!=
37b5d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20  sizeof(pCursor) 
37b5e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
37b5f 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e  esult_error(pCon
37b60 74 65 78 74 2c 20 22 69 6c 6c 65 67 61 6c 20 66  text, "illegal f
37b61 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
37b62 20 68 74 6d 6c 5f 73 6e 69 70 70 65 74 22 2c 2d   html_snippet",-
37b63 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  1);.  }else{.   
37b64 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74   const char *zSt
37b65 61 72 74 20 3d 20 22 3c 62 3e 22 3b 0a 20 20 20  art = "<b>";.   
37b66 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e   const char *zEn
37b67 64 20 3d 20 22 3c 2f 62 3e 22 3b 0a 20 20 20 20  d = "</b>";.    
37b68 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6c 6c  const char *zEll
37b69 69 70 73 69 73 20 3d 20 22 3c 62 3e 2e 2e 2e 3c  ipsis = "<b>...<
37b6a 2f 62 3e 22 3b 0a 20 20 20 20 6d 65 6d 63 70 79  /b>";.    memcpy
37b6b 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69 74  (&pCursor, sqlit
37b6c 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72  e3_value_blob(ar
37b6d 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70  gv[0]), sizeof(p
37b6e 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 69 66  Cursor));.    if
37b6f 28 20 61 72 67 63 3e 3d 32 20 29 7b 0a 20 20 20  ( argc>=2 ){.   
37b70 20 20 20 7a 53 74 61 72 74 20 3d 20 28 63 6f 6e     zStart = (con
37b71 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33  st char*)sqlite3
37b72 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
37b73 5b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [1]);.      if( 
37b74 61 72 67 63 3e 3d 33 20 29 7b 0a 20 20 20 20 20  argc>=3 ){.     
37b75 20 20 20 7a 45 6e 64 20 3d 20 28 63 6f 6e 73 74     zEnd = (const
37b76 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76   char*)sqlite3_v
37b77 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32  alue_text(argv[2
37b78 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]);.        if( 
37b79 61 72 67 63 3e 3d 34 20 29 7b 0a 20 20 20 20 20  argc>=4 ){.     
37b7a 20 20 20 20 20 7a 45 6c 6c 69 70 73 69 73 20 3d       zEllipsis =
37b7b 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
37b7c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
37b7d 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 20  (argv[3]);.     
37b7e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
37b7f 20 7d 0a 20 20 20 20 73 6e 69 70 70 65 74 41 6c   }.    snippetAl
37b80 6c 4f 66 66 73 65 74 73 28 70 43 75 72 73 6f 72  lOffsets(pCursor
37b81 29 3b 0a 20 20 20 20 73 6e 69 70 70 65 74 54 65  );.    snippetTe
37b82 78 74 28 70 43 75 72 73 6f 72 2c 20 7a 53 74 61  xt(pCursor, zSta
37b83 72 74 2c 20 7a 45 6e 64 2c 20 7a 45 6c 6c 69 70  rt, zEnd, zEllip
37b84 73 69 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  sis);.    sqlite
37b85 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
37b86 6f 6e 74 65 78 74 2c 20 70 43 75 72 73 6f 72 2d  ontext, pCursor-
37b87 3e 73 6e 69 70 70 65 74 2e 7a 53 6e 69 70 70 65  >snippet.zSnippe
37b88 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
37b89 20 20 20 20 20 20 20 20 20 20 20 70 43 75 72 73             pCurs
37b8a 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 6e 53 6e 69  or->snippet.nSni
37b8b 70 70 65 74 2c 20 53 51 4c 49 54 45 5f 53 54 41  ppet, SQLITE_STA
37b8c 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  TIC);.  }.}../*.
37b8d 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
37b8e 6e 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 73  n of the offsets
37b8f 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  () function for 
37b90 46 54 53 33 0a 2a 2f 0a 73 74 61 74 69 63 20 76  FTS3.*/.static v
37b91 6f 69 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65  oid snippetOffse
37b92 74 73 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  tsFunc(.  sqlite
37b93 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  3_context *pCont
37b94 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
37b95 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
37b96 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66 75 6c   **argv.){.  ful
37b97 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70 43  ltext_cursor *pC
37b98 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 61 72 67  ursor;.  if( arg
37b99 63 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  c<1 ) return;.  
37b9a 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
37b9b 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 21  e_type(argv[0])!
37b9c 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a  =SQLITE_BLOB ||.
37b9d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
37b9e 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
37b9f 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73  ])!=sizeof(pCurs
37ba0 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  or) ){.    sqlit
37ba1 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
37ba2 70 43 6f 6e 74 65 78 74 2c 20 22 69 6c 6c 65 67  pContext, "illeg
37ba3 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  al first argumen
37ba4 74 20 74 6f 20 6f 66 66 73 65 74 73 22 2c 2d 31  t to offsets",-1
37ba5 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
37ba6 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72 2c  memcpy(&pCursor,
37ba7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
37ba8 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73 69  lob(argv[0]), si
37ba9 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b 0a  zeof(pCursor));.
37baa 20 20 20 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66      snippetAllOf
37bab 66 73 65 74 73 28 70 43 75 72 73 6f 72 29 3b 0a  fsets(pCursor);.
37bac 20 20 20 20 73 6e 69 70 70 65 74 4f 66 66 73 65      snippetOffse
37bad 74 54 65 78 74 28 26 70 43 75 72 73 6f 72 2d 3e  tText(&pCursor->
37bae 73 6e 69 70 70 65 74 29 3b 0a 20 20 20 20 73 71  snippet);.    sq
37baf 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
37bb0 74 28 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20  t(pContext,.    
37bb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37bb2 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69      pCursor->sni
37bb3 70 70 65 74 2e 7a 4f 66 66 73 65 74 2c 20 70 43  ppet.zOffset, pC
37bb4 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 6e  ursor->snippet.n
37bb5 4f 66 66 73 65 74 2c 0a 20 20 20 20 20 20 20 20  Offset,.        
37bb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37bb7 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
37bb8 20 20 7d 0a 7d 0a 0a 2f 2a 20 4f 70 74 4c 65 61    }.}../* OptLea
37bb9 76 65 73 52 65 61 64 65 72 20 69 73 20 6e 65 61  vesReader is nea
37bba 72 6c 79 20 69 64 65 6e 74 69 63 61 6c 20 74 6f  rly identical to
37bbb 20 4c 65 61 76 65 73 52 65 61 64 65 72 2c 20 65   LeavesReader, e
37bbc 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 77 68  xcept that.** wh
37bbd 65 72 65 20 4c 65 61 76 65 73 52 65 61 64 65 72  ere LeavesReader
37bbe 20 69 73 20 67 65 61 72 65 64 20 74 6f 77 61 72   is geared towar
37bbf 64 73 20 74 68 65 20 6d 65 72 67 69 6e 67 20 6f  ds the merging o
37bc0 66 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 65  f complete.** se
37bc1 67 6d 65 6e 74 20 6c 65 76 65 6c 73 20 28 77 69  gment levels (wi
37bc2 74 68 20 65 78 61 63 74 6c 79 20 4d 45 52 47 45  th exactly MERGE
37bc3 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 29  _COUNT segments)
37bc4 2c 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65  , OptLeavesReade
37bc5 72 0a 2a 2a 20 69 73 20 67 65 61 72 65 64 20 74  r.** is geared t
37bc6 6f 77 61 72 64 73 20 69 6d 70 6c 65 6d 65 6e 74  owards implement
37bc7 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 74  ation of the opt
37bc8 69 6d 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e  imize() function
37bc9 2c 20 61 6e 64 0a 2a 2a 20 63 61 6e 20 6d 65 72  , and.** can mer
37bca 67 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20  ge all segments 
37bcb 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 20  simultaneously. 
37bcc 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6d 61   This version ma
37bcd 79 20 62 65 0a 2a 2a 20 73 6f 6d 65 77 68 61 74  y be.** somewhat
37bce 20 6c 65 73 73 20 65 66 66 69 63 69 65 6e 74 20   less efficient 
37bcf 74 68 61 6e 20 4c 65 61 76 65 73 52 65 61 64 65  than LeavesReade
37bd0 72 20 62 65 63 61 75 73 65 20 69 74 20 6d 65 72  r because it mer
37bd1 67 65 73 20 69 6e 74 6f 20 61 6e 0a 2a 2a 20 61  ges into an.** a
37bd2 63 63 75 6d 75 6c 61 74 6f 72 20 72 61 74 68 65  ccumulator rathe
37bd3 72 20 74 68 61 6e 20 64 6f 69 6e 67 20 61 6e 20  r than doing an 
37bd4 4e 2d 77 61 79 20 6d 65 72 67 65 2c 20 62 75 74  N-way merge, but
37bd5 20 73 69 6e 63 65 20 73 65 67 6d 65 6e 74 0a 2a   since segment.*
37bd6 2a 20 73 69 7a 65 20 67 72 6f 77 73 20 65 78 70  * size grows exp
37bd7 6f 6e 65 6e 74 69 61 6c 6c 79 20 28 73 6f 20 73  onentially (so s
37bd8 65 67 6d 65 6e 74 20 63 6f 75 6e 74 20 6c 6f 67  egment count log
37bd9 72 69 74 68 6d 69 63 61 6c 6c 79 29 20 74 68 69  rithmically) thi
37bda 73 20 69 73 0a 2a 2a 20 70 72 6f 62 61 62 6c 79  s is.** probably
37bdb 20 6e 6f 74 20 61 6e 20 69 6d 6d 65 64 69 61 74   not an immediat
37bdc 65 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 2f 2a  e problem..*/./*
37bdd 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 50 72   TODO(shess): Pr
37bde 6f 76 65 20 74 68 61 74 20 61 73 73 65 72 74 69  ove that asserti
37bdf 6f 6e 2c 20 6f 72 20 65 78 74 65 6e 64 20 74 68  on, or extend th
37be0 65 20 6d 65 72 67 65 20 63 6f 64 65 20 74 6f 0a  e merge code to.
37be1 2a 2a 20 6d 65 72 67 65 20 74 72 65 65 20 66 61  ** merge tree fa
37be2 73 68 69 6f 6e 20 28 6c 69 6b 65 20 74 68 65 20  shion (like the 
37be3 70 72 65 66 69 78 2d 73 65 61 72 63 68 69 6e 67  prefix-searching
37be4 20 63 6f 64 65 20 64 6f 65 73 29 2e 0a 2a 2f 0a   code does)..*/.
37be5 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20  /* TODO(shess): 
37be6 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
37be7 61 6e 64 20 4c 65 61 76 65 73 52 65 61 64 65 72  and LeavesReader
37be8 20 63 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20   could probably 
37be9 62 65 0a 2a 2a 20 6d 65 72 67 65 64 20 77 69 74  be.** merged wit
37bea 68 20 6c 69 74 74 6c 65 20 6f 72 20 6e 6f 20 6c  h little or no l
37beb 6f 73 73 20 6f 66 20 70 65 72 66 6f 72 6d 61 6e  oss of performan
37bec 63 65 20 66 6f 72 20 4c 65 61 76 65 73 52 65 61  ce for LeavesRea
37bed 64 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6d 65 72  der.  The.** mer
37bee 67 65 64 20 63 6f 64 65 20 77 6f 75 6c 64 20 6e  ged code would n
37bef 65 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 3e 4d  eed to handle >M
37bf0 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d 65  ERGE_COUNT segme
37bf1 6e 74 73 2c 20 61 6e 64 20 77 6f 75 6c 64 0a 2a  nts, and would.*
37bf2 2a 20 61 6c 73 6f 20 6e 65 65 64 20 74 6f 20 62  * also need to b
37bf3 65 20 61 62 6c 65 20 74 6f 20 6f 70 74 69 6f 6e  e able to option
37bf4 61 6c 6c 79 20 6f 70 74 69 6d 69 7a 65 20 61 77  ally optimize aw
37bf5 61 79 20 64 65 6c 65 74 65 73 2e 0a 2a 2f 0a 74  ay deletes..*/.t
37bf6 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4f 70  ypedef struct Op
37bf7 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 7b 0a  tLeavesReader {.
37bf8 20 20 2f 2a 20 53 65 67 6d 65 6e 74 20 6e 75 6d    /* Segment num
37bf9 62 65 72 2c 20 74 6f 20 6f 72 64 65 72 20 72 65  ber, to order re
37bfa 61 64 65 72 73 20 62 79 20 61 67 65 2e 20 2a 2f  aders by age. */
37bfb 0a 20 20 69 6e 74 20 73 65 67 6d 65 6e 74 3b 0a  .  int segment;.
37bfc 20 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 72    LeavesReader r
37bfd 65 61 64 65 72 3b 0a 7d 20 4f 70 74 4c 65 61 76  eader;.} OptLeav
37bfe 65 73 52 65 61 64 65 72 3b 0a 0a 73 74 61 74 69  esReader;..stati
37bff 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52  c int optLeavesR
37c00 65 61 64 65 72 41 74 45 6e 64 28 4f 70 74 4c 65  eaderAtEnd(OptLe
37c01 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61  avesReader *pRea
37c02 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c  der){.  return l
37c03 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64  eavesReaderAtEnd
37c04 28 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65  (&pReader->reade
37c05 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  r);.}.static int
37c06 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
37c07 54 65 72 6d 42 79 74 65 73 28 4f 70 74 4c 65 61  TermBytes(OptLea
37c08 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
37c09 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65  er){.  return le
37c0a 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79  avesReaderTermBy
37c0b 74 65 73 28 26 70 52 65 61 64 65 72 2d 3e 72 65  tes(&pReader->re
37c0c 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
37c0d 63 6f 6e 73 74 20 63 68 61 72 20 2a 6f 70 74 4c  const char *optL
37c0e 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28  eavesReaderData(
37c0f 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
37c10 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
37c11 75 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72  urn leavesReader
37c12 44 61 74 61 28 26 70 52 65 61 64 65 72 2d 3e 72  Data(&pReader->r
37c13 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63  eader);.}.static
37c14 20 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52 65   int optLeavesRe
37c15 61 64 65 72 44 61 74 61 42 79 74 65 73 28 4f 70  aderDataBytes(Op
37c16 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70  tLeavesReader *p
37c17 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
37c18 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 61  n leavesReaderDa
37c19 74 61 42 79 74 65 73 28 26 70 52 65 61 64 65 72  taBytes(&pReader
37c1a 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61  ->reader);.}.sta
37c1b 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
37c1c 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54  optLeavesReaderT
37c1d 65 72 6d 28 4f 70 74 4c 65 61 76 65 73 52 65 61  erm(OptLeavesRea
37c1e 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
37c1f 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52 65   return leavesRe
37c20 61 64 65 72 54 65 72 6d 28 26 70 52 65 61 64 65  aderTerm(&pReade
37c21 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73 74  r->reader);.}.st
37c22 61 74 69 63 20 69 6e 74 20 6f 70 74 4c 65 61 76  atic int optLeav
37c23 65 73 52 65 61 64 65 72 53 74 65 70 28 66 75 6c  esReaderStep(ful
37c24 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4f  ltext_vtab *v, O
37c25 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
37c26 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75  pReader){.  retu
37c27 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72 53  rn leavesReaderS
37c28 74 65 70 28 76 2c 20 26 70 52 65 61 64 65 72 2d  tep(v, &pReader-
37c29 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74  >reader);.}.stat
37c2a 69 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65 73  ic int optLeaves
37c2b 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 4f 70  ReaderTermCmp(Op
37c2c 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c  tLeavesReader *l
37c2d 72 31 2c 20 4f 70 74 4c 65 61 76 65 73 52 65 61  r1, OptLeavesRea
37c2e 64 65 72 20 2a 6c 72 32 29 7b 0a 20 20 72 65 74  der *lr2){.  ret
37c2f 75 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72  urn leavesReader
37c30 54 65 72 6d 43 6d 70 28 26 6c 72 31 2d 3e 72 65  TermCmp(&lr1->re
37c31 61 64 65 72 2c 20 26 6c 72 32 2d 3e 72 65 61 64  ader, &lr2->read
37c32 65 72 29 3b 0a 7d 0a 2f 2a 20 4f 72 64 65 72 20  er);.}./* Order 
37c33 62 79 20 74 65 72 6d 20 61 73 63 65 6e 64 69 6e  by term ascendin
37c34 67 2c 20 73 65 67 6d 65 6e 74 20 61 73 63 65 6e  g, segment ascen
37c35 64 69 6e 67 20 28 6f 6c 64 65 73 74 20 74 6f 20  ding (oldest to 
37c36 6e 65 77 65 73 74 29 2c 20 77 69 74 68 0a 2a 2a  newest), with.**
37c37 20 65 78 68 61 75 73 74 65 64 20 72 65 61 64 65   exhausted reade
37c38 72 73 20 74 6f 20 74 68 65 20 65 6e 64 2e 0a 2a  rs to the end..*
37c39 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74  /.static int opt
37c3a 4c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70 28  LeavesReaderCmp(
37c3b 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
37c3c 2a 6c 72 31 2c 20 4f 70 74 4c 65 61 76 65 73 52  *lr1, OptLeavesR
37c3d 65 61 64 65 72 20 2a 6c 72 32 29 7b 0a 20 20 69  eader *lr2){.  i
37c3e 6e 74 20 63 20 3d 20 6f 70 74 4c 65 61 76 65 73  nt c = optLeaves
37c3f 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 6c 72  ReaderTermCmp(lr
37c40 31 2c 20 6c 72 32 29 3b 0a 20 20 69 66 28 20 63  1, lr2);.  if( c
37c41 21 3d 30 20 29 20 72 65 74 75 72 6e 20 63 3b 0a  !=0 ) return c;.
37c42 20 20 72 65 74 75 72 6e 20 6c 72 31 2d 3e 73 65    return lr1->se
37c43 67 6d 65 6e 74 2d 6c 72 32 2d 3e 73 65 67 6d 65  gment-lr2->segme
37c44 6e 74 3b 0a 7d 0a 2f 2a 20 42 75 62 62 6c 65 20  nt;.}./* Bubble 
37c45 70 4c 72 5b 30 5d 20 74 6f 20 61 70 70 72 6f 70  pLr[0] to approp
37c46 72 69 61 74 65 20 70 6c 61 63 65 20 69 6e 20 70  riate place in p
37c47 4c 72 5b 31 2e 2e 6e 4c 72 2d 31 5d 2e 20 20 41  Lr[1..nLr-1].  A
37c48 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 70  ssumes that.** p
37c49 4c 72 5b 31 2e 2e 6e 4c 72 2d 31 5d 20 69 73 20  Lr[1..nLr-1] is 
37c4a 61 6c 72 65 61 64 79 20 73 6f 72 74 65 64 2e 0a  already sorted..
37c4b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
37c4c 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 52 65  ptLeavesReaderRe
37c4d 6f 72 64 65 72 28 4f 70 74 4c 65 61 76 65 73 52  order(OptLeavesR
37c4e 65 61 64 65 72 20 2a 70 4c 72 2c 20 69 6e 74 20  eader *pLr, int 
37c4f 6e 4c 72 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e  nLr){.  while( n
37c50 4c 72 3e 31 20 26 26 20 6f 70 74 4c 65 61 76 65  Lr>1 && optLeave
37c51 73 52 65 61 64 65 72 43 6d 70 28 70 4c 72 2c 20  sReaderCmp(pLr, 
37c52 70 4c 72 2b 31 29 3e 30 20 29 7b 0a 20 20 20 20  pLr+1)>0 ){.    
37c53 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
37c54 74 6d 70 20 3d 20 70 4c 72 5b 30 5d 3b 0a 20 20  tmp = pLr[0];.  
37c55 20 20 70 4c 72 5b 30 5d 20 3d 20 70 4c 72 5b 31    pLr[0] = pLr[1
37c56 5d 3b 0a 20 20 20 20 70 4c 72 5b 31 5d 20 3d 20  ];.    pLr[1] = 
37c57 74 6d 70 3b 0a 20 20 20 20 6e 4c 72 2d 2d 3b 0a  tmp;.    nLr--;.
37c58 20 20 20 20 70 4c 72 2b 2b 3b 0a 20 20 7d 0a 7d      pLr++;.  }.}
37c59 0a 0a 2f 2a 20 6f 70 74 69 6d 69 7a 65 28 29 20  ../* optimize() 
37c5a 68 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 2e  helper function.
37c5b 20 20 50 75 74 20 74 68 65 20 72 65 61 64 65 72    Put the reader
37c5c 73 20 69 6e 20 6f 72 64 65 72 20 61 6e 64 20 69  s in order and i
37c5d 74 65 72 61 74 65 0a 2a 2a 20 74 68 72 6f 75 67  terate.** throug
37c5e 68 20 74 68 65 6d 2c 20 6d 65 72 67 69 6e 67 20  h them, merging 
37c5f 64 6f 63 6c 69 73 74 73 20 66 6f 72 20 6d 61 74  doclists for mat
37c60 63 68 69 6e 67 20 74 65 72 6d 73 20 69 6e 74 6f  ching terms into
37c61 20 70 57 72 69 74 65 72 2e 0a 2a 2a 20 52 65 74   pWriter..** Ret
37c62 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  urns SQLITE_OK o
37c63 6e 20 73 75 63 63 65 73 73 2c 20 6f 72 20 74 68  n success, or th
37c64 65 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  e SQLite error c
37c65 6f 64 65 20 77 68 69 63 68 0a 2a 2a 20 70 72 65  ode which.** pre
37c66 76 65 6e 74 65 64 20 73 75 63 63 65 73 73 2e 0a  vented success..
37c67 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70  */.static int op
37c68 74 69 6d 69 7a 65 49 6e 74 65 72 6e 61 6c 28 66  timizeInternal(f
37c69 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
37c6a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37c6b 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 70 74               Opt
37c6c 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 72 65  LeavesReader *re
37c6d 61 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61 64  aders, int nRead
37c6e 65 72 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ers,.           
37c6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c70 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72   LeafWriter *pWr
37c71 69 74 65 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20  iter){.  int i, 
37c72 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
37c73 20 20 44 61 74 61 42 75 66 66 65 72 20 64 6f 63    DataBuffer doc
37c74 6c 69 73 74 2c 20 6d 65 72 67 65 64 2c 20 74 6d  list, merged, tm
37c75 70 3b 0a 0a 20 20 2f 2a 20 4f 72 64 65 72 20 74  p;..  /* Order t
37c76 68 65 20 72 65 61 64 65 72 73 2e 20 2a 2f 0a 20  he readers. */. 
37c77 20 69 20 3d 20 6e 52 65 61 64 65 72 73 3b 0a 20   i = nReaders;. 
37c78 20 77 68 69 6c 65 28 20 69 2d 2d 20 3e 20 30 20   while( i-- > 0 
37c79 29 7b 0a 20 20 20 20 6f 70 74 4c 65 61 76 65 73  ){.    optLeaves
37c7a 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 26 72  ReaderReorder(&r
37c7b 65 61 64 65 72 73 5b 69 5d 2c 20 6e 52 65 61 64  eaders[i], nRead
37c7c 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 0a 20 20 64  ers-i);.  }..  d
37c7d 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 64  ataBufferInit(&d
37c7e 6f 63 6c 69 73 74 2c 20 4c 45 41 46 5f 4d 41 58  oclist, LEAF_MAX
37c7f 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  );.  dataBufferI
37c80 6e 69 74 28 26 6d 65 72 67 65 64 2c 20 4c 45 41  nit(&merged, LEA
37c81 46 5f 4d 41 58 29 3b 0a 0a 20 20 2f 2a 20 45 78  F_MAX);..  /* Ex
37c82 68 61 75 73 74 65 64 20 72 65 61 64 65 72 73 20  hausted readers 
37c83 62 75 62 62 6c 65 20 74 6f 20 74 68 65 20 65 6e  bubble to the en
37c84 64 2c 20 73 6f 20 77 68 65 6e 20 74 68 65 20 66  d, so when the f
37c85 69 72 73 74 20 72 65 61 64 65 72 20 69 73 0a 20  irst reader is. 
37c86 20 2a 2a 20 61 74 20 65 6f 66 2c 20 61 6c 6c 20   ** at eof, all 
37c87 61 72 65 20 61 74 20 65 6f 66 2e 0a 20 20 2a 2f  are at eof..  */
37c88 0a 20 20 77 68 69 6c 65 28 20 21 6f 70 74 4c 65  .  while( !optLe
37c89 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28  avesReaderAtEnd(
37c8a 26 72 65 61 64 65 72 73 5b 30 5d 29 20 29 7b 0a  &readers[0]) ){.
37c8b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  .    /* Figure o
37c8c 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 61 64  ut how many read
37c8d 65 72 73 20 73 68 61 72 65 20 74 68 65 20 6e 65  ers share the ne
37c8e 78 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 20 20  xt term. */.    
37c8f 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 52 65 61 64  for(i=1; i<nRead
37c90 65 72 73 20 26 26 20 21 6f 70 74 4c 65 61 76 65  ers && !optLeave
37c91 73 52 65 61 64 65 72 41 74 45 6e 64 28 26 72 65  sReaderAtEnd(&re
37c92 61 64 65 72 73 5b 69 5d 29 3b 20 69 2b 2b 29 7b  aders[i]); i++){
37c93 0a 20 20 20 20 20 20 69 66 28 20 30 21 3d 6f 70  .      if( 0!=op
37c94 74 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72  tLeavesReaderTer
37c95 6d 43 6d 70 28 26 72 65 61 64 65 72 73 5b 30 5d  mCmp(&readers[0]
37c96 2c 20 26 72 65 61 64 65 72 73 5b 69 5d 29 20 29  , &readers[i]) )
37c97 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
37c98 20 20 20 2f 2a 20 53 70 65 63 69 61 6c 2d 63 61     /* Special-ca
37c99 73 65 20 66 6f 72 20 6e 6f 20 6d 65 72 67 65 2e  se for no merge.
37c9a 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 3d 3d 31   */.    if( i==1
37c9b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72 69   ){.      /* Tri
37c9c 6d 20 64 65 6c 65 74 69 6f 6e 73 20 66 72 6f 6d  m deletions from
37c9d 20 74 68 65 20 64 6f 63 6c 69 73 74 2e 20 2a 2f   the doclist. */
37c9e 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65  .      dataBuffe
37c9f 72 52 65 73 65 74 28 26 6d 65 72 67 65 64 29 3b  rReset(&merged);
37ca0 0a 20 20 20 20 20 20 64 6f 63 4c 69 73 74 54 72  .      docListTr
37ca1 69 6d 28 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20  im(DL_DEFAULT,. 
37ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37ca3 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
37ca4 44 61 74 61 28 26 72 65 61 64 65 72 73 5b 30 5d  Data(&readers[0]
37ca5 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
37ca6 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65       optLeavesRe
37ca7 61 64 65 72 44 61 74 61 42 79 74 65 73 28 26 72  aderDataBytes(&r
37ca8 65 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20  eaders[0]),.    
37ca9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31                -1
37caa 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 26 6d  , DL_DEFAULT, &m
37cab 65 72 67 65 64 29 3b 0a 20 20 20 20 7d 65 6c 73  erged);.    }els
37cac 65 7b 0a 20 20 20 20 20 20 44 4c 52 65 61 64 65  e{.      DLReade
37cad 72 20 64 6c 52 65 61 64 65 72 73 5b 4d 45 52 47  r dlReaders[MERG
37cae 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 20 20 20 20  E_COUNT];.      
37caf 69 6e 74 20 69 52 65 61 64 65 72 2c 20 6e 52 65  int iReader, nRe
37cb0 61 64 65 72 73 3b 0a 0a 20 20 20 20 20 20 2f 2a  aders;..      /*
37cb1 20 50 72 69 6d 65 20 74 68 65 20 70 69 70 65 6c   Prime the pipel
37cb2 69 6e 65 20 77 69 74 68 20 74 68 65 20 66 69 72  ine with the fir
37cb3 73 74 20 72 65 61 64 65 72 27 73 20 64 6f 63 6c  st reader's docl
37cb4 69 73 74 2e 20 20 41 66 74 65 72 0a 20 20 20 20  ist.  After.    
37cb5 20 20 2a 2a 20 6f 6e 65 20 70 61 73 73 20 69 6e    ** one pass in
37cb6 64 65 78 20 30 20 77 69 6c 6c 20 72 65 66 65 72  dex 0 will refer
37cb7 65 6e 63 65 20 74 68 65 20 61 63 63 75 6d 75 6c  ence the accumul
37cb8 61 74 65 64 20 64 6f 63 6c 69 73 74 2e 0a 20 20  ated doclist..  
37cb9 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6c 72      */.      dlr
37cba 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72 73 5b  Init(&dlReaders[
37cbb 30 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 0a  0], DL_DEFAULT,.
37cbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70                op
37cbd 74 4c 65 61 76 65 73 52 65 61 64 65 72 44 61 74  tLeavesReaderDat
37cbe 61 28 26 72 65 61 64 65 72 73 5b 30 5d 29 2c 0a  a(&readers[0]),.
37cbf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70                op
37cc0 74 4c 65 61 76 65 73 52 65 61 64 65 72 44 61 74  tLeavesReaderDat
37cc1 61 42 79 74 65 73 28 26 72 65 61 64 65 72 73 5b  aBytes(&readers[
37cc2 30 5d 29 29 3b 0a 20 20 20 20 20 20 69 52 65 61  0]));.      iRea
37cc3 64 65 72 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20  der = 1;..      
37cc4 61 73 73 65 72 74 28 20 69 52 65 61 64 65 72 3c  assert( iReader<
37cc5 69 20 29 3b 20 20 2f 2a 20 4d 75 73 74 20 65 78  i );  /* Must ex
37cc6 65 63 75 74 65 20 74 68 65 20 6c 6f 6f 70 20 61  ecute the loop a
37cc7 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 2a 2f  t least once. */
37cc8 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 52  .      while( iR
37cc9 65 61 64 65 72 3c 69 20 29 7b 0a 20 20 20 20 20  eader<i ){.     
37cca 20 20 20 2f 2a 20 4d 65 72 67 65 20 31 36 20 69     /* Merge 16 i
37ccb 6e 70 75 74 73 20 70 65 72 20 70 61 73 73 2e 20  nputs per pass. 
37ccc 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 20  */.        for( 
37ccd 6e 52 65 61 64 65 72 73 3d 31 3b 20 69 52 65 61  nReaders=1; iRea
37cce 64 65 72 3c 69 20 26 26 20 6e 52 65 61 64 65 72  der<i && nReader
37ccf 73 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 0a 20  s<MERGE_COUNT;. 
37cd0 20 20 20 20 20 20 20 20 20 20 20 20 69 52 65 61              iRea
37cd1 64 65 72 2b 2b 2c 20 6e 52 65 61 64 65 72 73 2b  der++, nReaders+
37cd2 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64  + ){.          d
37cd3 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72  lrInit(&dlReader
37cd4 73 5b 6e 52 65 61 64 65 72 73 5d 2c 20 44 4c 5f  s[nReaders], DL_
37cd5 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20 20  DEFAULT,.       
37cd6 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65             optLe
37cd7 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28 26  avesReaderData(&
37cd8 72 65 61 64 65 72 73 5b 69 52 65 61 64 65 72 5d  readers[iReader]
37cd9 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
37cda 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65       optLeavesRe
37cdb 61 64 65 72 44 61 74 61 42 79 74 65 73 28 26 72  aderDataBytes(&r
37cdc 65 61 64 65 72 73 5b 69 52 65 61 64 65 72 5d 29  eaders[iReader])
37cdd 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  );.        }..  
37cde 20 20 20 20 20 20 2f 2a 20 4d 65 72 67 65 20 64        /* Merge d
37cdf 6f 63 6c 69 73 74 73 20 61 6e 64 20 73 77 61 70  oclists and swap
37ce0 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 61 63 63   result into acc
37ce1 75 6d 75 6c 61 74 6f 72 2e 20 2a 2f 0a 20 20 20  umulator. */.   
37ce2 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52       dataBufferR
37ce3 65 73 65 74 28 26 6d 65 72 67 65 64 29 3b 0a 20  eset(&merged);. 
37ce4 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 4d 65         docListMe
37ce5 72 67 65 28 26 6d 65 72 67 65 64 2c 20 64 6c 52  rge(&merged, dlR
37ce6 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65 72 73  eaders, nReaders
37ce7 29 3b 0a 20 20 20 20 20 20 20 20 74 6d 70 20 3d  );.        tmp =
37ce8 20 6d 65 72 67 65 64 3b 0a 20 20 20 20 20 20 20   merged;.       
37ce9 20 6d 65 72 67 65 64 20 3d 20 64 6f 63 6c 69 73   merged = doclis
37cea 74 3b 0a 20 20 20 20 20 20 20 20 64 6f 63 6c 69  t;.        docli
37ceb 73 74 20 3d 20 74 6d 70 3b 0a 0a 20 20 20 20 20  st = tmp;..     
37cec 20 20 20 77 68 69 6c 65 28 20 6e 52 65 61 64 65     while( nReade
37ced 72 73 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20  rs-- > 0 ){.    
37cee 20 20 20 20 20 20 64 6c 72 44 65 73 74 72 6f 79        dlrDestroy
37cef 28 26 64 6c 52 65 61 64 65 72 73 5b 6e 52 65 61  (&dlReaders[nRea
37cf0 64 65 72 73 5d 29 3b 0a 20 20 20 20 20 20 20 20  ders]);.        
37cf1 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63  }..        /* Ac
37cf2 63 75 6d 75 6c 61 74 65 64 20 64 6f 63 6c 69 73  cumulated doclis
37cf3 74 20 74 6f 20 72 65 61 64 65 72 20 30 20 66 6f  t to reader 0 fo
37cf4 72 20 6e 65 78 74 20 70 61 73 73 2e 20 2a 2f 0a  r next pass. */.
37cf5 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28          dlrInit(
37cf6 26 64 6c 52 65 61 64 65 72 73 5b 30 5d 2c 20 44  &dlReaders[0], D
37cf7 4c 5f 44 45 46 41 55 4c 54 2c 20 64 6f 63 6c 69  L_DEFAULT, docli
37cf8 73 74 2e 70 44 61 74 61 2c 20 64 6f 63 6c 69 73  st.pData, doclis
37cf9 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  t.nData);.      
37cfa 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 74  }..      /* Dest
37cfb 72 6f 79 20 72 65 61 64 65 72 20 74 68 61 74 20  roy reader that 
37cfc 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20  was left in the 
37cfd 70 69 70 65 6c 69 6e 65 2e 20 2a 2f 0a 20 20 20  pipeline. */.   
37cfe 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 64     dlrDestroy(&d
37cff 6c 52 65 61 64 65 72 73 5b 30 5d 29 3b 0a 0a 20  lReaders[0]);.. 
37d00 20 20 20 20 20 2f 2a 20 54 72 69 6d 20 64 65 6c       /* Trim del
37d01 65 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20  etions from the 
37d02 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20  doclist. */.    
37d03 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65    dataBufferRese
37d04 74 28 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20  t(&merged);.    
37d05 20 20 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c    docListTrim(DL
37d06 5f 44 45 46 41 55 4c 54 2c 20 64 6f 63 6c 69 73  _DEFAULT, doclis
37d07 74 2e 70 44 61 74 61 2c 20 64 6f 63 6c 69 73 74  t.pData, doclist
37d08 2e 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20  .nData,.        
37d09 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 44 4c            -1, DL
37d0a 5f 44 45 46 41 55 4c 54 2c 20 26 6d 65 72 67 65  _DEFAULT, &merge
37d0b 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  d);.    }..    /
37d0c 2a 20 4f 6e 6c 79 20 70 61 73 73 20 64 6f 63 6c  * Only pass docl
37d0d 69 73 74 73 20 77 69 74 68 20 68 69 74 73 20 28  ists with hits (
37d0e 73 6b 69 70 20 69 66 20 61 6c 6c 20 68 69 74 73  skip if all hits
37d0f 20 64 65 6c 65 74 65 64 29 2e 20 2a 2f 0a 20 20   deleted). */.  
37d10 20 20 69 66 28 20 6d 65 72 67 65 64 2e 6e 44 61    if( merged.nDa
37d11 74 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63  ta>0 ){.      rc
37d12 20 3d 20 6c 65 61 66 57 72 69 74 65 72 53 74 65   = leafWriterSte
37d13 70 28 76 2c 20 70 57 72 69 74 65 72 2c 0a 20 20  p(v, pWriter,.  
37d14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d15 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65          optLeave
37d16 73 52 65 61 64 65 72 54 65 72 6d 28 26 72 65 61  sReaderTerm(&rea
37d17 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20  ders[0]),.      
37d18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d19 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61      optLeavesRea
37d1a 64 65 72 54 65 72 6d 42 79 74 65 73 28 26 72 65  derTermBytes(&re
37d1b 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20  aders[0]),.     
37d1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d1d 20 20 20 20 20 6d 65 72 67 65 64 2e 70 44 61 74       merged.pDat
37d1e 61 2c 20 6d 65 72 67 65 64 2e 6e 44 61 74 61 29  a, merged.nData)
37d1f 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
37d20 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
37d21 20 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   err;.    }..   
37d22 20 2f 2a 20 53 74 65 70 20 6d 65 72 67 65 64 20   /* Step merged 
37d23 72 65 61 64 65 72 73 20 74 6f 20 6e 65 78 74 20  readers to next 
37d24 74 65 72 6d 20 61 6e 64 20 72 65 6f 72 64 65 72  term and reorder
37d25 2e 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20  . */.    while( 
37d26 69 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 20  i-- > 0 ){.     
37d27 20 72 63 20 3d 20 6f 70 74 4c 65 61 76 65 73 52   rc = optLeavesR
37d28 65 61 64 65 72 53 74 65 70 28 76 2c 20 26 72 65  eaderStep(v, &re
37d29 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 20 20 20  aders[i]);.     
37d2a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
37d2b 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a  OK ) goto err;..
37d2c 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52        optLeavesR
37d2d 65 61 64 65 72 52 65 6f 72 64 65 72 28 26 72 65  eaderReorder(&re
37d2e 61 64 65 72 73 5b 69 5d 2c 20 6e 52 65 61 64 65  aders[i], nReade
37d2f 72 73 2d 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  rs-i);.    }.  }
37d30 0a 0a 20 65 72 72 3a 0a 20 20 64 61 74 61 42 75  .. err:.  dataBu
37d31 66 66 65 72 44 65 73 74 72 6f 79 28 26 64 6f 63  fferDestroy(&doc
37d32 6c 69 73 74 29 3b 0a 20 20 64 61 74 61 42 75 66  list);.  dataBuf
37d33 66 65 72 44 65 73 74 72 6f 79 28 26 6d 65 72 67  ferDestroy(&merg
37d34 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ed);.  return rc
37d35 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65 6d 65 6e  ;.}../* Implemen
37d36 74 20 6f 70 74 69 6d 69 7a 65 28 29 20 66 75 6e  t optimize() fun
37d37 63 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 2e 20  ction for FTS3. 
37d38 20 6f 70 74 69 6d 69 7a 65 28 74 29 20 6d 65 72   optimize(t) mer
37d39 67 65 73 20 61 6c 6c 0a 2a 2a 20 73 65 67 6d 65  ges all.** segme
37d3a 6e 74 73 20 69 6e 20 74 68 65 20 66 74 73 20 69  nts in the fts i
37d3b 6e 64 65 78 20 69 6e 74 6f 20 61 20 73 69 6e 67  ndex into a sing
37d3c 6c 65 20 73 65 67 6d 65 6e 74 2e 20 20 27 74 27  le segment.  't'
37d3d 20 69 73 20 74 68 65 20 6d 61 67 69 63 0a 2a 2a   is the magic.**
37d3e 20 74 61 62 6c 65 2d 6e 61 6d 65 64 20 63 6f 6c   table-named col
37d3f 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  umn..*/.static v
37d40 6f 69 64 20 6f 70 74 69 6d 69 7a 65 46 75 6e 63  oid optimizeFunc
37d41 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
37d42 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20   *pContext,.    
37d43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d44 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73       int argc, s
37d45 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
37d46 72 67 76 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  rgv){.  fulltext
37d47 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
37d48 3b 0a 20 20 69 66 28 20 61 72 67 63 3e 31 20 29  ;.  if( argc>1 )
37d49 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
37d4a 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74  sult_error(pCont
37d4b 65 78 74 2c 20 22 65 78 63 65 73 73 20 61 72 67  ext, "excess arg
37d4c 75 6d 65 6e 74 73 20 74 6f 20 6f 70 74 69 6d 69  uments to optimi
37d4d 7a 65 28 29 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c  ze()",-1);.  }el
37d4e 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  se if( sqlite3_v
37d4f 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
37d50 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20  ])!=SQLITE_BLOB 
37d51 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ||.            s
37d52 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
37d53 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a  es(argv[0])!=siz
37d54 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a  eof(pCursor) ){.
37d55 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
37d56 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78  lt_error(pContex
37d57 74 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73  t, "illegal firs
37d58 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 6f 70  t argument to op
37d59 74 69 6d 69 7a 65 22 2c 2d 31 29 3b 0a 20 20 7d  timize",-1);.  }
37d5a 65 6c 73 65 7b 0a 20 20 20 20 66 75 6c 6c 74 65  else{.    fullte
37d5b 78 74 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20 20  xt_vtab *v;.    
37d5c 69 6e 74 20 69 2c 20 72 63 2c 20 69 4d 61 78 4c  int i, rc, iMaxL
37d5d 65 76 65 6c 3b 0a 20 20 20 20 4f 70 74 4c 65 61  evel;.    OptLea
37d5e 76 65 73 52 65 61 64 65 72 20 2a 72 65 61 64 65  vesReader *reade
37d5f 72 73 3b 0a 20 20 20 20 69 6e 74 20 6e 52 65 61  rs;.    int nRea
37d60 64 65 72 73 3b 0a 20 20 20 20 4c 65 61 66 57 72  ders;.    LeafWr
37d61 69 74 65 72 20 77 72 69 74 65 72 3b 0a 20 20 20  iter writer;.   
37d62 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
37d63 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70  ;..    memcpy(&p
37d64 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f  Cursor, sqlite3_
37d65 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
37d66 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72  0]), sizeof(pCur
37d67 73 6f 72 29 29 3b 0a 20 20 20 20 76 20 3d 20 63  sor));.    v = c
37d68 75 72 73 6f 72 5f 76 74 61 62 28 70 43 75 72 73  ursor_vtab(pCurs
37d69 6f 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 6c 75  or);..    /* Flu
37d6a 73 68 20 61 6e 79 20 62 75 66 66 65 72 65 64 20  sh any buffered 
37d6b 75 70 64 61 74 65 73 20 62 65 66 6f 72 65 20 6f  updates before o
37d6c 70 74 69 6d 69 7a 69 6e 67 2e 20 2a 2f 0a 20 20  ptimizing. */.  
37d6d 20 20 72 63 20 3d 20 66 6c 75 73 68 50 65 6e 64    rc = flushPend
37d6e 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20  ingTerms(v);.   
37d6f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
37d70 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a  OK ) goto err;..
37d71 20 20 20 20 72 63 20 3d 20 73 65 67 64 69 72 5f      rc = segdir_
37d72 63 6f 75 6e 74 28 76 2c 20 26 6e 52 65 61 64 65  count(v, &nReade
37d73 72 73 2c 20 26 69 4d 61 78 4c 65 76 65 6c 29 3b  rs, &iMaxLevel);
37d74 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
37d75 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72  ITE_OK ) goto er
37d76 72 3b 0a 20 20 20 20 69 66 28 20 6e 52 65 61 64  r;.    if( nRead
37d77 65 72 73 3d 3d 30 20 7c 7c 20 6e 52 65 61 64 65  ers==0 || nReade
37d78 72 73 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 73  rs==1 ){.      s
37d79 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
37d7a 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e  xt(pContext, "In
37d7b 64 65 78 20 61 6c 72 65 61 64 79 20 6f 70 74 69  dex already opti
37d7c 6d 61 6c 22 2c 20 2d 31 2c 0a 20 20 20 20 20 20  mal", -1,.      
37d7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d7e 20 20 20 20 53 51 4c 49 54 45 5f 53 54 41 54 49      SQLITE_STATI
37d7f 43 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  C);.      return
37d80 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20  ;.    }..    rc 
37d81 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
37d82 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45  ent(v, SEGDIR_SE
37d83 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26  LECT_ALL_STMT, &
37d84 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  s);.    if( rc!=
37d85 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
37d86 20 65 72 72 3b 0a 0a 20 20 20 20 72 65 61 64 65   err;..    reade
37d87 72 73 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  rs = sqlite3_mal
37d88 6c 6f 63 28 6e 52 65 61 64 65 72 73 2a 73 69 7a  loc(nReaders*siz
37d89 65 6f 66 28 72 65 61 64 65 72 73 5b 30 5d 29 29  eof(readers[0]))
37d8a 3b 0a 20 20 20 20 69 66 28 20 72 65 61 64 65 72  ;.    if( reader
37d8b 73 3d 3d 4e 55 4c 4c 20 29 20 67 6f 74 6f 20 65  s==NULL ) goto e
37d8c 72 72 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f 74 65  rr;..    /* Note
37d8d 20 74 68 61 74 20 74 68 65 72 65 20 77 69 6c 6c   that there will
37d8e 20 61 6c 72 65 61 64 79 20 62 65 20 61 20 73 65   already be a se
37d8f 67 6d 65 6e 74 20 61 74 20 74 68 69 73 20 70 6f  gment at this po
37d90 73 69 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 75 6e  sition.    ** un
37d91 74 69 6c 20 77 65 20 63 61 6c 6c 20 73 65 67 64  til we call segd
37d92 69 72 5f 64 65 6c 65 74 65 28 29 20 6f 6e 20 69  ir_delete() on i
37d93 4d 61 78 4c 65 76 65 6c 2e 0a 20 20 20 20 2a 2f  MaxLevel..    */
37d94 0a 20 20 20 20 6c 65 61 66 57 72 69 74 65 72 49  .    leafWriterI
37d95 6e 69 74 28 69 4d 61 78 4c 65 76 65 6c 2c 20 30  nit(iMaxLevel, 0
37d96 2c 20 26 77 72 69 74 65 72 29 3b 0a 0a 20 20 20  , &writer);..   
37d97 20 69 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c   i = 0;.    whil
37d98 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  e( (rc = sqlite3
37d99 5f 73 74 65 70 28 73 29 29 3d 3d 53 51 4c 49 54  _step(s))==SQLIT
37d9a 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 73  E_ROW ){.      s
37d9b 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61  qlite_int64 iSta
37d9c 72 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  rt = sqlite3_col
37d9d 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 30 29 3b  umn_int64(s, 0);
37d9e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e  .      sqlite_in
37d9f 74 36 34 20 69 45 6e 64 20 3d 20 73 71 6c 69 74  t64 iEnd = sqlit
37da0 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
37da1 73 2c 20 31 29 3b 0a 20 20 20 20 20 20 63 6f 6e  s, 1);.      con
37da2 73 74 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61  st char *pRootDa
37da3 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ta = sqlite3_col
37da4 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20 32 29 3b 0a  umn_blob(s, 2);.
37da5 20 20 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 44        int nRootD
37da6 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ata = sqlite3_co
37da7 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32 29  lumn_bytes(s, 2)
37da8 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
37da9 20 69 3c 6e 52 65 61 64 65 72 73 20 29 3b 0a 20   i<nReaders );. 
37daa 20 20 20 20 20 72 63 20 3d 20 6c 65 61 76 65 73       rc = leaves
37dab 52 65 61 64 65 72 49 6e 69 74 28 76 2c 20 2d 31  ReaderInit(v, -1
37dac 2c 20 69 53 74 61 72 74 2c 20 69 45 6e 64 2c 20  , iStart, iEnd, 
37dad 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74  pRootData, nRoot
37dae 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
37daf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37db0 20 20 26 72 65 61 64 65 72 73 5b 69 5d 2e 72 65    &readers[i].re
37db1 61 64 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28  ader);.      if(
37db2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
37db3 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 72   break;..      r
37db4 65 61 64 65 72 73 5b 69 5d 2e 73 65 67 6d 65 6e  eaders[i].segmen
37db5 74 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 2b 2b  t = i;.      i++
37db6 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
37db7 49 66 20 77 65 20 6d 61 6e 61 67 65 64 20 74 6f  If we managed to
37db8 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65   successfully re
37db9 61 64 20 74 68 65 6d 20 61 6c 6c 2c 20 6f 70 74  ad them all, opt
37dba 69 6d 69 7a 65 20 74 68 65 6d 2e 20 2a 2f 0a 20  imize them. */. 
37dbb 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
37dbc 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20  E_DONE ){.      
37dbd 61 73 73 65 72 74 28 20 69 3d 3d 6e 52 65 61 64  assert( i==nRead
37dbe 65 72 73 20 29 3b 0a 20 20 20 20 20 20 72 63 20  ers );.      rc 
37dbf 3d 20 6f 70 74 69 6d 69 7a 65 49 6e 74 65 72 6e  = optimizeIntern
37dc0 61 6c 28 76 2c 20 72 65 61 64 65 72 73 2c 20 6e  al(v, readers, n
37dc1 52 65 61 64 65 72 73 2c 20 26 77 72 69 74 65 72  Readers, &writer
37dc2 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 77 68  );.    }..    wh
37dc3 69 6c 65 28 20 69 2d 2d 20 3e 20 30 20 29 7b 0a  ile( i-- > 0 ){.
37dc4 20 20 20 20 20 20 6c 65 61 76 65 73 52 65 61 64        leavesRead
37dc5 65 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65  erDestroy(&reade
37dc6 72 73 5b 69 5d 2e 72 65 61 64 65 72 29 3b 0a 20  rs[i].reader);. 
37dc7 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
37dc8 5f 66 72 65 65 28 72 65 61 64 65 72 73 29 3b 0a  _free(readers);.
37dc9 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 27 76 65  .    /* If we've
37dca 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 67 6f   successfully go
37dcb 74 74 65 6e 20 74 6f 20 68 65 72 65 2c 20 64 65  tten to here, de
37dcc 6c 65 74 65 20 74 68 65 20 6f 6c 64 20 73 65 67  lete the old seg
37dcd 6d 65 6e 74 73 0a 20 20 20 20 2a 2a 20 61 6e 64  ments.    ** and
37dce 20 66 6c 75 73 68 20 74 68 65 20 69 6e 74 65 72   flush the inter
37dcf 69 6f 72 20 73 74 72 75 63 74 75 72 65 20 6f 66  ior structure of
37dd0 20 74 68 65 20 6e 65 77 20 73 65 67 6d 65 6e 74   the new segment
37dd1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
37dd2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
37dd3 7b 0a 20 20 20 20 20 20 66 6f 72 28 20 69 3d 30  {.      for( i=0
37dd4 3b 20 69 3c 3d 69 4d 61 78 4c 65 76 65 6c 3b 20  ; i<=iMaxLevel; 
37dd5 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  i++ ){.        r
37dd6 63 20 3d 20 73 65 67 64 69 72 5f 64 65 6c 65 74  c = segdir_delet
37dd7 65 28 76 2c 20 69 29 3b 0a 20 20 20 20 20 20 20  e(v, i);.       
37dd8 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
37dd9 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  OK ) break;.    
37dda 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72    }..      if( r
37ddb 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c==SQLITE_OK ) r
37ddc 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46 69  c = leafWriterFi
37ddd 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72 69 74 65  nalize(v, &write
37dde 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6c  r);.    }..    l
37ddf 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79  eafWriterDestroy
37de0 28 26 77 72 69 74 65 72 29 3b 0a 0a 20 20 20 20  (&writer);..    
37de1 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
37de2 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20  K ) goto err;.. 
37de3 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
37de4 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74 2c  t_text(pContext,
37de5 20 22 49 6e 64 65 78 20 6f 70 74 69 6d 69 7a 65   "Index optimize
37de6 64 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53  d", -1, SQLITE_S
37de7 54 41 54 49 43 29 3b 0a 20 20 20 20 72 65 74 75  TATIC);.    retu
37de8 72 6e 3b 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f  rn;..    /* TODO
37de9 28 73 68 65 73 73 29 3a 20 45 72 72 6f 72 2d 68  (shess): Error-h
37dea 61 6e 64 6c 69 6e 67 20 6e 65 65 64 73 20 74 6f  andling needs to
37deb 20 62 65 20 69 6d 70 72 6f 76 65 64 20 61 6c 6f   be improved alo
37dec 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6c 69  ng the.    ** li
37ded 6e 65 73 20 6f 66 20 74 68 65 20 64 75 6d 70 5f  nes of the dump_
37dee 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20   functions..    
37def 2a 2f 0a 20 65 72 72 3a 0a 20 20 20 20 7b 0a 20  */. err:.    {. 
37df0 20 20 20 20 20 63 68 61 72 20 62 75 66 5b 35 31       char buf[51
37df1 32 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2];.      sqlite
37df2 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
37df3 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 45 72  f(buf), buf, "Er
37df4 72 6f 72 20 69 6e 20 6f 70 74 69 6d 69 7a 65 3a  ror in optimize:
37df5 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %s",.          
37df6 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
37df7 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c 69  ite3_errmsg(sqli
37df8 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
37df9 61 6e 64 6c 65 28 70 43 6f 6e 74 65 78 74 29 29  andle(pContext))
37dfa 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
37dfb 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43  _result_error(pC
37dfc 6f 6e 74 65 78 74 2c 20 62 75 66 2c 20 2d 31 29  ontext, buf, -1)
37dfd 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23  ;.    }.  }.}..#
37dfe 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
37dff 54 0a 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e  T./* Generate an
37e00 20 65 72 72 6f 72 20 6f 66 20 74 68 65 20 66 6f   error of the fo
37e01 72 6d 20 22 3c 70 72 65 66 69 78 3e 3a 20 3c 6d  rm "<prefix>: <m
37e02 73 67 3e 22 2e 20 20 49 66 20 6d 73 67 20 69 73  sg>".  If msg is
37e03 20 4e 55 4c 4c 2c 0a 2a 2a 20 70 75 6c 6c 20 74   NULL,.** pull t
37e04 68 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 74 68  he error from th
37e05 65 20 63 6f 6e 74 65 78 74 27 73 20 64 62 20 68  e context's db h
37e06 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  andle..*/.static
37e07 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65 45 72   void generateEr
37e08 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ror(sqlite3_cont
37e09 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20  ext *pContext,. 
37e0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37e0b 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
37e0c 68 61 72 20 2a 70 72 65 66 69 78 2c 20 63 6f 6e  har *prefix, con
37e0d 73 74 20 63 68 61 72 20 2a 6d 73 67 29 7b 0a 20  st char *msg){. 
37e0e 20 63 68 61 72 20 62 75 66 5b 35 31 32 5d 3b 0a   char buf[512];.
37e0f 20 20 69 66 28 20 6d 73 67 3d 3d 4e 55 4c 4c 20    if( msg==NULL 
37e10 29 20 6d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f  ) msg = sqlite3_
37e11 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 5f 63  errmsg(sqlite3_c
37e12 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
37e13 28 70 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73  (pContext));.  s
37e14 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
37e15 73 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66  sizeof(buf), buf
37e16 2c 20 22 25 73 3a 20 25 73 22 2c 20 70 72 65 66  , "%s: %s", pref
37e17 69 78 2c 20 6d 73 67 29 3b 0a 20 20 73 71 6c 69  ix, msg);.  sqli
37e18 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
37e19 28 70 43 6f 6e 74 65 78 74 2c 20 62 75 66 2c 20  (pContext, buf, 
37e1a 2d 31 29 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65  -1);.}../* Helpe
37e1b 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f  r function to co
37e1c 6c 6c 65 63 74 20 74 68 65 20 73 65 74 20 6f 66  llect the set of
37e1d 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 73 65   terms in the se
37e1e 67 6d 65 6e 74 20 69 6e 74 6f 0a 2a 2a 20 70 54  gment into.** pT
37e1f 65 72 6d 73 2e 20 20 54 68 65 20 73 65 67 6d 65  erms.  The segme
37e20 6e 74 20 69 73 20 64 65 66 69 6e 65 64 20 62 79  nt is defined by
37e21 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 73 20   the leaf nodes 
37e22 62 65 74 77 65 65 6e 0a 2a 2a 20 69 53 74 61 72  between.** iStar
37e23 74 42 6c 6f 63 6b 69 64 20 61 6e 64 20 69 45 6e  tBlockid and iEn
37e24 64 42 6c 6f 63 6b 69 64 2c 20 69 6e 63 6c 75 73  dBlockid, inclus
37e25 69 76 65 2c 20 6f 72 20 62 79 20 74 68 65 20 63  ive, or by the c
37e26 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 70 52  ontents of.** pR
37e27 6f 6f 74 44 61 74 61 20 69 66 20 69 53 74 61 72  ootData if iStar
37e28 74 42 6c 6f 63 6b 69 64 20 69 73 20 30 20 28 69  tBlockid is 0 (i
37e29 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65  n which case the
37e2a 20 65 6e 74 69 72 65 20 73 65 67 6d 65 6e 74 0a   entire segment.
37e2b 2a 2a 20 66 69 74 20 69 6e 20 61 20 6c 65 61 66  ** fit in a leaf
37e2c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
37e2d 20 63 6f 6c 6c 65 63 74 53 65 67 6d 65 6e 74 54   collectSegmentT
37e2e 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74  erms(fulltext_vt
37e2f 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f 73  ab *v, sqlite3_s
37e30 74 6d 74 20 2a 73 2c 0a 20 20 20 20 20 20 20 20  tmt *s,.        
37e31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37e32 20 20 20 20 20 20 20 66 74 73 33 48 61 73 68 20         fts3Hash 
37e33 2a 70 54 65 72 6d 73 29 7b 0a 20 20 63 6f 6e 73  *pTerms){.  cons
37e34 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  t sqlite_int64 i
37e35 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 3d 20 73  StartBlockid = s
37e36 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
37e37 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 63 6f 6e  t64(s, 0);.  con
37e38 73 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  st sqlite_int64 
37e39 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 73 71  iEndBlockid = sq
37e3a 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
37e3b 36 34 28 73 2c 20 31 29 3b 0a 20 20 63 6f 6e 73  64(s, 1);.  cons
37e3c 74 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61 74  t char *pRootDat
37e3d 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  a = sqlite3_colu
37e3e 6d 6e 5f 62 6c 6f 62 28 73 2c 20 32 29 3b 0a 20  mn_blob(s, 2);. 
37e3f 20 63 6f 6e 73 74 20 69 6e 74 20 6e 52 6f 6f 74   const int nRoot
37e40 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63  Data = sqlite3_c
37e41 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32  olumn_bytes(s, 2
37e42 29 3b 0a 20 20 4c 65 61 76 65 73 52 65 61 64 65  );.  LeavesReade
37e43 72 20 72 65 61 64 65 72 3b 0a 20 20 69 6e 74 20  r reader;.  int 
37e44 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65  rc = leavesReade
37e45 72 49 6e 69 74 28 76 2c 20 30 2c 20 69 53 74 61  rInit(v, 0, iSta
37e46 72 74 42 6c 6f 63 6b 69 64 2c 20 69 45 6e 64 42  rtBlockid, iEndB
37e47 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20  lockid,.        
37e48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37e49 20 20 20 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e      pRootData, n
37e4a 52 6f 6f 74 44 61 74 61 2c 20 26 72 65 61 64 65  RootData, &reade
37e4b 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  r);.  if( rc!=SQ
37e4c 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
37e4d 20 72 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 72   rc;..  while( r
37e4e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
37e4f 21 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45  !leavesReaderAtE
37e50 6e 64 28 26 72 65 61 64 65 72 29 20 29 7b 0a 20  nd(&reader) ){. 
37e51 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
37e52 54 65 72 6d 20 3d 20 6c 65 61 76 65 73 52 65 61  Term = leavesRea
37e53 64 65 72 54 65 72 6d 28 26 72 65 61 64 65 72 29  derTerm(&reader)
37e54 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  ;.    const int 
37e55 6e 54 65 72 6d 20 3d 20 6c 65 61 76 65 73 52 65  nTerm = leavesRe
37e56 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 26 72  aderTermBytes(&r
37e57 65 61 64 65 72 29 3b 0a 20 20 20 20 76 6f 69 64  eader);.    void
37e58 20 2a 6f 6c 64 56 61 6c 75 65 20 3d 20 73 71 6c   *oldValue = sql
37e59 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
37e5a 28 70 54 65 72 6d 73 2c 20 70 54 65 72 6d 2c 20  (pTerms, pTerm, 
37e5b 6e 54 65 72 6d 29 3b 0a 20 20 20 20 76 6f 69 64  nTerm);.    void
37e5c 20 2a 6e 65 77 56 61 6c 75 65 20 3d 20 28 76 6f   *newValue = (vo
37e5d 69 64 20 2a 29 28 28 63 68 61 72 20 2a 29 6f 6c  id *)((char *)ol
37e5e 64 56 61 6c 75 65 2b 31 29 3b 0a 0a 20 20 20 20  dValue+1);..    
37e5f 2f 2a 20 46 72 6f 6d 20 74 68 65 20 63 6f 6d 6d  /* From the comm
37e60 65 6e 74 20 62 65 66 6f 72 65 20 73 71 6c 69 74  ent before sqlit
37e61 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
37e62 20 69 6e 20 66 74 73 33 5f 68 61 73 68 2e 63 2c   in fts3_hash.c,
37e63 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61  .    ** the data
37e64 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 69 73   value passed is
37e65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 63 61 73   returned in cas
37e66 65 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  e of malloc fail
37e67 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ure..    */.    
37e68 69 66 28 20 6e 65 77 56 61 6c 75 65 3d 3d 73 71  if( newValue==sq
37e69 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73  lite3Fts3HashIns
37e6a 65 72 74 28 70 54 65 72 6d 73 2c 20 70 54 65 72  ert(pTerms, pTer
37e6b 6d 2c 20 6e 54 65 72 6d 2c 20 6e 65 77 56 61 6c  m, nTerm, newVal
37e6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ue) ){.      rc 
37e6d 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
37e6e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
37e6f 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64   rc = leavesRead
37e70 65 72 53 74 65 70 28 76 2c 20 26 72 65 61 64 65  erStep(v, &reade
37e71 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  r);.    }.  }.. 
37e72 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73   leavesReaderDes
37e73 74 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 20  troy(&reader);. 
37e74 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
37e75 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f  * Helper functio
37e76 6e 20 74 6f 20 62 75 69 6c 64 20 74 68 65 20 72  n to build the r
37e77 65 73 75 6c 74 20 73 74 72 69 6e 67 20 66 6f 72  esult string for
37e78 20 64 75 6d 70 5f 74 65 72 6d 73 28 29 2e 20 2a   dump_terms(). *
37e79 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 6e  /.static int gen
37e7a 65 72 61 74 65 54 65 72 6d 73 52 65 73 75 6c 74  erateTermsResult
37e7b 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
37e7c 20 2a 70 43 6f 6e 74 65 78 74 2c 20 66 74 73 33   *pContext, fts3
37e7d 48 61 73 68 20 2a 70 54 65 72 6d 73 29 7b 0a 20  Hash *pTerms){. 
37e7e 20 69 6e 74 20 69 54 65 72 6d 2c 20 6e 54 65 72   int iTerm, nTer
37e7f 6d 73 2c 20 6e 52 65 73 75 6c 74 42 79 74 65 73  ms, nResultBytes
37e80 2c 20 69 42 79 74 65 3b 0a 20 20 63 68 61 72 20  , iByte;.  char 
37e81 2a 72 65 73 75 6c 74 3b 0a 20 20 54 65 72 6d 44  *result;.  TermD
37e82 61 74 61 20 2a 70 44 61 74 61 3b 0a 20 20 66 74  ata *pData;.  ft
37e83 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 0a  s3HashElem *e;..
37e84 20 20 2f 2a 20 49 74 65 72 61 74 65 20 70 54 65    /* Iterate pTe
37e85 72 6d 73 20 74 6f 20 67 65 6e 65 72 61 74 65 20  rms to generate 
37e86 61 6e 20 61 72 72 61 79 20 6f 66 20 74 65 72 6d  an array of term
37e87 73 20 69 6e 20 70 44 61 74 61 20 66 6f 72 0a 20  s in pData for. 
37e88 20 2a 2a 20 73 6f 72 74 69 6e 67 2e 0a 20 20 2a   ** sorting..  *
37e89 2f 0a 20 20 6e 54 65 72 6d 73 20 3d 20 66 74 73  /.  nTerms = fts
37e8a 33 48 61 73 68 43 6f 75 6e 74 28 70 54 65 72 6d  3HashCount(pTerm
37e8b 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54  s);.  assert( nT
37e8c 65 72 6d 73 3e 30 20 29 3b 0a 20 20 70 44 61 74  erms>0 );.  pDat
37e8d 61 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  a = sqlite3_mall
37e8e 6f 63 28 6e 54 65 72 6d 73 2a 73 69 7a 65 6f 66  oc(nTerms*sizeof
37e8f 28 54 65 72 6d 44 61 74 61 29 29 3b 0a 20 20 69  (TermData));.  i
37e90 66 28 20 70 44 61 74 61 3d 3d 4e 55 4c 4c 20 29  f( pData==NULL )
37e91 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
37e92 4f 4d 45 4d 3b 0a 0a 20 20 6e 52 65 73 75 6c 74  OMEM;..  nResult
37e93 42 79 74 65 73 20 3d 20 30 3b 0a 20 20 66 6f 72  Bytes = 0;.  for
37e94 28 69 54 65 72 6d 20 3d 20 30 2c 20 65 20 3d 20  (iTerm = 0, e = 
37e95 66 74 73 33 48 61 73 68 46 69 72 73 74 28 70 54  fts3HashFirst(pT
37e96 65 72 6d 73 29 3b 20 65 3b 20 69 54 65 72 6d 2b  erms); e; iTerm+
37e97 2b 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68 4e  +, e = fts3HashN
37e98 65 78 74 28 65 29 29 7b 0a 20 20 20 20 6e 52 65  ext(e)){.    nRe
37e99 73 75 6c 74 42 79 74 65 73 20 2b 3d 20 66 74 73  sultBytes += fts
37e9a 33 48 61 73 68 4b 65 79 73 69 7a 65 28 65 29 2b  3HashKeysize(e)+
37e9b 31 3b 20 20 20 2f 2a 20 54 65 72 6d 20 70 6c 75  1;   /* Term plu
37e9c 73 20 74 72 61 69 6c 69 6e 67 20 73 70 61 63 65  s trailing space
37e9d 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
37e9e 69 54 65 72 6d 3c 6e 54 65 72 6d 73 20 29 3b 0a  iTerm<nTerms );.
37e9f 20 20 20 20 70 44 61 74 61 5b 69 54 65 72 6d 5d      pData[iTerm]
37ea0 2e 70 54 65 72 6d 20 3d 20 66 74 73 33 48 61 73  .pTerm = fts3Has
37ea1 68 4b 65 79 28 65 29 3b 0a 20 20 20 20 70 44 61  hKey(e);.    pDa
37ea2 74 61 5b 69 54 65 72 6d 5d 2e 6e 54 65 72 6d 20  ta[iTerm].nTerm 
37ea3 3d 20 66 74 73 33 48 61 73 68 4b 65 79 73 69 7a  = fts3HashKeysiz
37ea4 65 28 65 29 3b 0a 20 20 20 20 70 44 61 74 61 5b  e(e);.    pData[
37ea5 69 54 65 72 6d 5d 2e 70 43 6f 6c 6c 65 63 74 6f  iTerm].pCollecto
37ea6 72 20 3d 20 66 74 73 33 48 61 73 68 44 61 74 61  r = fts3HashData
37ea7 28 65 29 3b 20 20 2f 2a 20 75 6e 75 73 65 64 20  (e);  /* unused 
37ea8 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  */.  }.  assert(
37ea9 20 69 54 65 72 6d 3d 3d 6e 54 65 72 6d 73 20 29   iTerm==nTerms )
37eaa 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65  ;..  assert( nRe
37eab 73 75 6c 74 42 79 74 65 73 3e 30 20 29 3b 20 20  sultBytes>0 );  
37eac 20 2f 2a 20 6e 54 65 72 6d 73 3e 30 2c 20 6e 52   /* nTerms>0, nR
37ead 65 73 75 6c 74 73 42 79 74 65 73 20 6d 75 73 74  esultsBytes must
37eae 20 62 65 2c 20 74 6f 6f 2e 20 2a 2f 0a 20 20 72   be, too. */.  r
37eaf 65 73 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f  esult = sqlite3_
37eb0 6d 61 6c 6c 6f 63 28 6e 52 65 73 75 6c 74 42 79  malloc(nResultBy
37eb1 74 65 73 29 3b 0a 20 20 69 66 28 20 72 65 73 75  tes);.  if( resu
37eb2 6c 74 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  lt==NULL ){.    
37eb3 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 44 61  sqlite3_free(pDa
37eb4 74 61 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ta);.    return 
37eb5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
37eb6 7d 0a 0a 20 20 69 66 28 20 6e 54 65 72 6d 73 3e  }..  if( nTerms>
37eb7 31 20 29 20 71 73 6f 72 74 28 70 44 61 74 61 2c  1 ) qsort(pData,
37eb8 20 6e 54 65 72 6d 73 2c 20 73 69 7a 65 6f 66 28   nTerms, sizeof(
37eb9 2a 70 44 61 74 61 29 2c 20 74 65 72 6d 44 61 74  *pData), termDat
37eba 61 43 6d 70 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  aCmp);..  /* Rea
37ebb 64 20 74 68 65 20 74 65 72 6d 73 20 69 6e 20 6f  d the terms in o
37ebc 72 64 65 72 20 74 6f 20 62 75 69 6c 64 20 74 68  rder to build th
37ebd 65 20 72 65 73 75 6c 74 2e 20 2a 2f 0a 20 20 69  e result. */.  i
37ebe 42 79 74 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28  Byte = 0;.  for(
37ebf 69 54 65 72 6d 3d 30 3b 20 69 54 65 72 6d 3c 6e  iTerm=0; iTerm<n
37ec0 54 65 72 6d 73 3b 20 2b 2b 69 54 65 72 6d 29 7b  Terms; ++iTerm){
37ec1 0a 20 20 20 20 6d 65 6d 63 70 79 28 72 65 73 75  .    memcpy(resu
37ec2 6c 74 2b 69 42 79 74 65 2c 20 70 44 61 74 61 5b  lt+iByte, pData[
37ec3 69 54 65 72 6d 5d 2e 70 54 65 72 6d 2c 20 70 44  iTerm].pTerm, pD
37ec4 61 74 61 5b 69 54 65 72 6d 5d 2e 6e 54 65 72 6d  ata[iTerm].nTerm
37ec5 29 3b 0a 20 20 20 20 69 42 79 74 65 20 2b 3d 20  );.    iByte += 
37ec6 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e 6e 54 65  pData[iTerm].nTe
37ec7 72 6d 3b 0a 20 20 20 20 72 65 73 75 6c 74 5b 69  rm;.    result[i
37ec8 42 79 74 65 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20  Byte++] = ' ';. 
37ec9 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 42 79   }.  assert( iBy
37eca 74 65 3d 3d 6e 52 65 73 75 6c 74 42 79 74 65 73  te==nResultBytes
37ecb 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 65   );.  assert( re
37ecc 73 75 6c 74 5b 6e 52 65 73 75 6c 74 42 79 74 65  sult[nResultByte
37ecd 73 2d 31 5d 3d 3d 27 20 27 20 29 3b 0a 20 20 72  s-1]==' ' );.  r
37ece 65 73 75 6c 74 5b 6e 52 65 73 75 6c 74 42 79 74  esult[nResultByt
37ecf 65 73 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20  es-1] = '\0';.. 
37ed0 20 2f 2a 20 50 61 73 73 65 73 20 61 77 61 79 20   /* Passes away 
37ed1 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 72 65 73  ownership of res
37ed2 75 6c 74 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ult. */.  sqlite
37ed3 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
37ed4 6f 6e 74 65 78 74 2c 20 72 65 73 75 6c 74 2c 20  ontext, result, 
37ed5 6e 52 65 73 75 6c 74 42 79 74 65 73 2d 31 2c 20  nResultBytes-1, 
37ed6 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20  sqlite3_free);. 
37ed7 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 44   sqlite3_free(pD
37ed8 61 74 61 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ata);.  return S
37ed9 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
37eda 49 6d 70 6c 65 6d 65 6e 74 73 20 64 75 6d 70 5f  Implements dump_
37edb 74 65 72 6d 73 28 29 20 66 6f 72 20 75 73 65 20  terms() for use 
37edc 69 6e 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68  in inspecting th
37edd 65 20 66 74 73 33 20 69 6e 64 65 78 20 66 72 6f  e fts3 index fro
37ede 6d 0a 2a 2a 20 74 65 73 74 73 2e 20 20 54 45 58  m.** tests.  TEX
37edf 54 20 72 65 73 75 6c 74 20 63 6f 6e 74 61 69 6e  T result contain
37ee0 69 6e 67 20 74 68 65 20 6f 72 64 65 72 65 64 20  ing the ordered 
37ee1 6c 69 73 74 20 6f 66 20 74 65 72 6d 73 20 6a 6f  list of terms jo
37ee2 69 6e 65 64 20 62 79 0a 2a 2a 20 73 70 61 63 65  ined by.** space
37ee3 73 2e 20 20 64 75 6d 70 5f 74 65 72 6d 73 28 74  s.  dump_terms(t
37ee4 2c 20 6c 65 76 65 6c 2c 20 69 64 78 29 20 64 75  , level, idx) du
37ee5 6d 70 73 20 74 68 65 20 74 65 72 6d 73 20 66 6f  mps the terms fo
37ee6 72 20 74 68 65 20 73 65 67 6d 65 6e 74 0a 2a 2a  r the segment.**
37ee7 20 73 70 65 63 69 66 69 65 64 20 62 79 20 6c 65   specified by le
37ee8 76 65 6c 2c 20 69 64 78 20 28 69 6e 20 25 5f 73  vel, idx (in %_s
37ee9 65 67 64 69 72 29 2c 20 77 68 69 6c 65 20 64 75  egdir), while du
37eea 6d 70 5f 74 65 72 6d 73 28 74 29 20 64 75 6d 70  mp_terms(t) dump
37eeb 73 0a 2a 2a 20 61 6c 6c 20 74 65 72 6d 73 20 69  s.** all terms i
37eec 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 20 49 6e  n the index.  In
37eed 20 62 6f 74 68 20 63 61 73 65 73 20 74 20 69 73   both cases t is
37eee 20 74 68 65 20 66 74 73 20 74 61 62 6c 65 27 73   the fts table's
37eef 20 6d 61 67 69 63 0a 2a 2a 20 74 61 62 6c 65 2d   magic.** table-
37ef0 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f  named column..*/
37ef1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 75 6d  .static void dum
37ef2 70 54 65 72 6d 73 46 75 6e 63 28 0a 20 20 73 71  pTermsFunc(.  sq
37ef3 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
37ef4 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  Context,.  int a
37ef5 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
37ef6 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66  ue **argv.){.  f
37ef7 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
37ef8 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 61  pCursor;.  if( a
37ef9 72 67 63 21 3d 33 20 26 26 20 61 72 67 63 21 3d  rgc!=3 && argc!=
37efa 31 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74  1 ){.    generat
37efb 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  eError(pContext,
37efc 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22   "dump_terms", "
37efd 69 6e 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65  incorrect argume
37efe 6e 74 73 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69  nts");.  }else i
37eff 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
37f00 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 21 3d  _type(argv[0])!=
37f01 53 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20  SQLITE_BLOB ||. 
37f02 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
37f03 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
37f04 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28  rgv[0])!=sizeof(
37f05 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20  pCursor) ){.    
37f06 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43  generateError(pC
37f07 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65  ontext, "dump_te
37f08 72 6d 73 22 2c 20 22 69 6c 6c 65 67 61 6c 20 66  rms", "illegal f
37f09 69 72 73 74 20 61 72 67 75 6d 65 6e 74 22 29 3b  irst argument");
37f0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 75  .  }else{.    fu
37f0b 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 3b 0a  lltext_vtab *v;.
37f0c 20 20 20 20 66 74 73 33 48 61 73 68 20 74 65 72      fts3Hash ter
37f0d 6d 73 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ms;.    sqlite3_
37f0e 73 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a  stmt *s = NULL;.
37f0f 20 20 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 20      int rc;..   
37f10 20 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72   memcpy(&pCursor
37f11 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  , sqlite3_value_
37f12 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73  blob(argv[0]), s
37f13 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b  izeof(pCursor));
37f14 0a 20 20 20 20 76 20 3d 20 63 75 72 73 6f 72 5f  .    v = cursor_
37f15 76 74 61 62 28 70 43 75 72 73 6f 72 29 3b 0a 0a  vtab(pCursor);..
37f16 20 20 20 20 2f 2a 20 49 66 20 70 61 73 73 65 64      /* If passed
37f17 20 6f 6e 6c 79 20 74 68 65 20 63 75 72 73 6f 72   only the cursor
37f18 20 63 6f 6c 75 6d 6e 2c 20 67 65 74 20 61 6c 6c   column, get all
37f19 20 73 65 67 6d 65 6e 74 73 2e 20 20 4f 74 68 65   segments.  Othe
37f1a 72 77 69 73 65 0a 20 20 20 20 2a 2a 20 67 65 74  rwise.    ** get
37f1b 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64 65 73   the segment des
37f1c 63 72 69 62 65 64 20 62 79 20 74 68 65 20 66 6f  cribed by the fo
37f1d 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 61 72 67 75  llowing two argu
37f1e 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  ments..    */.  
37f1f 20 20 69 66 28 20 61 72 67 63 3d 3d 31 20 29 7b    if( argc==1 ){
37f20 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 5f  .      rc = sql_
37f21 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
37f22 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 41   SEGDIR_SELECT_A
37f23 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  LL_STMT, &s);.  
37f24 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
37f25 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  c = sql_get_stat
37f26 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f  ement(v, SEGDIR_
37f27 53 45 4c 45 43 54 5f 53 45 47 4d 45 4e 54 5f 53  SELECT_SEGMENT_S
37f28 54 4d 54 2c 20 26 73 29 3b 0a 20 20 20 20 20 20  TMT, &s);.      
37f29 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
37f2a 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
37f2b 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
37f2c 6e 74 28 73 2c 20 31 2c 20 73 71 6c 69 74 65 33  nt(s, 1, sqlite3
37f2d 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b  _value_int(argv[
37f2e 31 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66  1]));.        if
37f2f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
37f30 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
37f31 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
37f32 6e 74 28 73 2c 20 32 2c 20 73 71 6c 69 74 65 33  nt(s, 2, sqlite3
37f33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b  _value_int(argv[
37f34 32 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  2]));.        }.
37f35 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
37f36 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
37f37 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 65  E_OK ){.      ge
37f38 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e  nerateError(pCon
37f39 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d  text, "dump_term
37f3a 73 22 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 20  s", NULL);.     
37f3b 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a   return;.    }..
37f3c 20 20 20 20 2f 2a 20 43 6f 6c 6c 65 63 74 20 74      /* Collect t
37f3d 68 65 20 74 65 72 6d 73 20 66 6f 72 20 65 61 63  he terms for eac
37f3e 68 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a 20 20  h segment. */.  
37f3f 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73    sqlite3Fts3Has
37f40 68 49 6e 69 74 28 26 74 65 72 6d 73 2c 20 46 54  hInit(&terms, FT
37f41 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20  S3_HASH_STRING, 
37f42 31 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28  1);.    while( (
37f43 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
37f44 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(s))==SQLITE_RO
37f45 57 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  W ){.      rc = 
37f46 63 6f 6c 6c 65 63 74 53 65 67 6d 65 6e 74 54 65  collectSegmentTe
37f47 72 6d 73 28 76 2c 20 73 2c 20 26 74 65 72 6d 73  rms(v, s, &terms
37f48 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
37f49 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65  =SQLITE_OK ) bre
37f4a 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ak;.    }..    i
37f4b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  f( rc!=SQLITE_DO
37f4c 4e 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  NE ){.      sqli
37f4d 74 65 33 5f 72 65 73 65 74 28 73 29 3b 0a 20 20  te3_reset(s);.  
37f4e 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
37f4f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
37f50 70 5f 74 65 72 6d 73 22 2c 20 4e 55 4c 4c 29 3b  p_terms", NULL);
37f51 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
37f52 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54 65 72    const int nTer
37f53 6d 73 20 3d 20 66 74 73 33 48 61 73 68 43 6f 75  ms = fts3HashCou
37f54 6e 74 28 26 74 65 72 6d 73 29 3b 0a 20 20 20 20  nt(&terms);.    
37f55 20 20 69 66 28 20 6e 54 65 72 6d 73 3e 30 20 29    if( nTerms>0 )
37f56 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 67  {.        rc = g
37f57 65 6e 65 72 61 74 65 54 65 72 6d 73 52 65 73 75  enerateTermsResu
37f58 6c 74 28 70 43 6f 6e 74 65 78 74 2c 20 26 74 65  lt(pContext, &te
37f59 72 6d 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66  rms);.        if
37f5a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
37f5b 45 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  EM ){.          
37f5c 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43  generateError(pC
37f5d 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65  ontext, "dump_te
37f5e 72 6d 73 22 2c 20 22 6f 75 74 20 6f 66 20 6d 65  rms", "out of me
37f5f 6d 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 20 20  mory");.        
37f60 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
37f61 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
37f62 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20  ITE_OK );.      
37f63 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20    }.      }else 
37f64 69 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20  if( argc==3 ){. 
37f65 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70         /* The sp
37f66 65 63 69 66 69 63 20 73 65 67 6d 65 6e 74 20 61  ecific segment a
37f67 73 6b 65 64 20 66 6f 72 20 63 6f 75 6c 64 20 6e  sked for could n
37f68 6f 74 20 62 65 20 66 6f 75 6e 64 2e 20 2a 2f 0a  ot be found. */.
37f69 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74 65          generate
37f6a 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
37f6b 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22 73  "dump_terms", "s
37f6c 65 67 6d 65 6e 74 20 6e 6f 74 20 66 6f 75 6e 64  egment not found
37f6d 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ");.      }else{
37f6e 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 73  .        /* No s
37f6f 65 67 6d 65 6e 74 73 20 66 6f 75 6e 64 2e 20 2a  egments found. *
37f70 2f 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 4f 44  /.        /* TOD
37f71 4f 28 73 68 65 73 73 29 3a 20 49 74 20 73 68 6f  O(shess): It sho
37f72 75 6c 64 20 62 65 20 69 6d 70 6f 73 73 69 62 6c  uld be impossibl
37f73 65 20 74 6f 20 72 65 61 63 68 20 74 68 69 73 2e  e to reach this.
37f74 20 20 54 68 69 73 0a 20 20 20 20 20 20 20 20 2a    This.        *
37f75 2a 20 63 61 73 65 20 63 61 6e 20 6f 6e 6c 79 20  * case can only 
37f76 68 61 70 70 65 6e 20 66 6f 72 20 61 6e 20 65 6d  happen for an em
37f77 70 74 79 20 74 61 62 6c 65 2c 20 69 6e 20 77 68  pty table, in wh
37f78 69 63 68 20 63 61 73 65 0a 20 20 20 20 20 20 20  ich case.       
37f79 20 2a 2a 20 53 51 4c 69 74 65 20 68 61 73 20 6e   ** SQLite has n
37f7a 6f 20 72 6f 77 73 20 74 6f 20 63 61 6c 6c 20 74  o rows to call t
37f7b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 2e  his function on.
37f7c 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
37f7d 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
37f7e 6c 74 5f 6e 75 6c 6c 28 70 43 6f 6e 74 65 78 74  lt_null(pContext
37f7f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
37f80 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33  .    sqlite3Fts3
37f81 48 61 73 68 43 6c 65 61 72 28 26 74 65 72 6d 73  HashClear(&terms
37f82 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 45 78 70  );.  }.}../* Exp
37f83 61 6e 64 20 74 68 65 20 44 4c 5f 44 45 46 41 55  and the DL_DEFAU
37f84 4c 54 20 64 6f 63 6c 69 73 74 20 69 6e 20 70 44  LT doclist in pD
37f85 61 74 61 20 69 6e 74 6f 20 61 20 74 65 78 74 20  ata into a text 
37f86 72 65 73 75 6c 74 20 69 6e 0a 2a 2a 20 70 43 6f  result in.** pCo
37f87 6e 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ntext..*/.static
37f88 20 76 6f 69 64 20 63 72 65 61 74 65 44 6f 63 6c   void createDocl
37f89 69 73 74 52 65 73 75 6c 74 28 73 71 6c 69 74 65  istResult(sqlite
37f8a 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  3_context *pCont
37f8b 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ext,.           
37f8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37f8d 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
37f8e 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
37f8f 61 29 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72  a){.  DataBuffer
37f90 20 64 75 6d 70 3b 0a 20 20 44 4c 52 65 61 64 65   dump;.  DLReade
37f91 72 20 64 6c 52 65 61 64 65 72 3b 0a 0a 20 20 61  r dlReader;..  a
37f92 73 73 65 72 74 28 20 70 44 61 74 61 21 3d 4e 55  ssert( pData!=NU
37f93 4c 4c 20 26 26 20 6e 44 61 74 61 3e 30 20 29 3b  LL && nData>0 );
37f94 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ..  dataBufferIn
37f95 69 74 28 26 64 75 6d 70 2c 20 30 29 3b 0a 20 20  it(&dump, 0);.  
37f96 64 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65  dlrInit(&dlReade
37f97 72 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  r, DL_DEFAULT, p
37f98 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20 20  Data, nData);.  
37f99 66 6f 72 28 20 3b 20 21 64 6c 72 41 74 45 6e 64  for( ; !dlrAtEnd
37f9a 28 26 64 6c 52 65 61 64 65 72 29 3b 20 64 6c 72  (&dlReader); dlr
37f9b 53 74 65 70 28 26 64 6c 52 65 61 64 65 72 29 20  Step(&dlReader) 
37f9c 29 7b 0a 20 20 20 20 63 68 61 72 20 62 75 66 5b  ){.    char buf[
37f9d 32 35 36 5d 3b 0a 20 20 20 20 50 4c 52 65 61 64  256];.    PLRead
37f9e 65 72 20 70 6c 52 65 61 64 65 72 3b 0a 0a 20 20  er plReader;..  
37f9f 20 20 70 6c 72 49 6e 69 74 28 26 70 6c 52 65 61    plrInit(&plRea
37fa0 64 65 72 2c 20 26 64 6c 52 65 61 64 65 72 29 3b  der, &dlReader);
37fa1 0a 20 20 20 20 69 66 28 20 44 4c 5f 44 45 46 41  .    if( DL_DEFA
37fa2 55 4c 54 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 7c  ULT==DL_DOCIDS |
37fa3 7c 20 70 6c 72 41 74 45 6e 64 28 26 70 6c 52 65  | plrAtEnd(&plRe
37fa4 61 64 65 72 29 20 29 7b 0a 20 20 20 20 20 20 73  ader) ){.      s
37fa5 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
37fa6 73 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66  sizeof(buf), buf
37fa7 2c 20 22 5b 25 6c 6c 64 5d 20 22 2c 20 64 6c 72  , "[%lld] ", dlr
37fa8 44 6f 63 69 64 28 26 64 6c 52 65 61 64 65 72 29  Docid(&dlReader)
37fa9 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66  );.      dataBuf
37faa 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c  ferAppend(&dump,
37fab 20 62 75 66 2c 20 73 74 72 6c 65 6e 28 62 75 66   buf, strlen(buf
37fac 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ));.    }else{. 
37fad 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e       int iColumn
37fae 20 3d 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c   = plrColumn(&pl
37faf 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 20 20  Reader);..      
37fb0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
37fb1 28 73 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75  (sizeof(buf), bu
37fb2 66 2c 20 22 5b 25 6c 6c 64 20 25 64 5b 22 2c 0a  f, "[%lld %d[",.
37fb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fb4 20 20 20 20 20 20 20 64 6c 72 44 6f 63 69 64 28         dlrDocid(
37fb5 26 64 6c 52 65 61 64 65 72 29 2c 20 69 43 6f 6c  &dlReader), iCol
37fb6 75 6d 6e 29 3b 0a 20 20 20 20 20 20 64 61 74 61  umn);.      data
37fb7 42 75 66 66 65 72 41 70 70 65 6e 64 28 26 64 75  BufferAppend(&du
37fb8 6d 70 2c 20 62 75 66 2c 20 73 74 72 6c 65 6e 28  mp, buf, strlen(
37fb9 62 75 66 29 29 3b 0a 0a 20 20 20 20 20 20 66 6f  buf));..      fo
37fba 72 28 20 3b 20 21 70 6c 72 41 74 45 6e 64 28 26  r( ; !plrAtEnd(&
37fbb 70 6c 52 65 61 64 65 72 29 3b 20 70 6c 72 53 74  plReader); plrSt
37fbc 65 70 28 26 70 6c 52 65 61 64 65 72 29 20 29 7b  ep(&plReader) ){
37fbd 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 6c 72  .        if( plr
37fbe 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72  Column(&plReader
37fbf 29 21 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  )!=iColumn ){.  
37fc0 20 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20          iColumn 
37fc1 3d 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52  = plrColumn(&plR
37fc2 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20 20 20  eader);.        
37fc3 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
37fc4 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29 2c 20  tf(sizeof(buf), 
37fc5 62 75 66 2c 20 22 5d 20 25 64 5b 22 2c 20 69 43  buf, "] %d[", iC
37fc6 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20  olumn);.        
37fc7 20 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e    assert( dump.n
37fc8 44 61 74 61 3e 30 20 29 3b 0a 20 20 20 20 20 20  Data>0 );.      
37fc9 20 20 20 20 64 75 6d 70 2e 6e 44 61 74 61 2d 2d      dump.nData--
37fca 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
37fcb 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69        /* Overwri
37fcc 74 65 20 74 72 61 69 6c 69 6e 67 20 73 70 61 63  te trailing spac
37fcd 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  e. */.          
37fce 61 73 73 65 72 74 28 20 64 75 6d 70 2e 70 44 61  assert( dump.pDa
37fcf 74 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d  ta[dump.nData]==
37fd0 27 20 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ' ');.          
37fd1 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
37fd2 28 26 64 75 6d 70 2c 20 62 75 66 2c 20 73 74 72  (&dump, buf, str
37fd3 6c 65 6e 28 62 75 66 29 29 3b 0a 20 20 20 20 20  len(buf));.     
37fd4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
37fd5 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f   DL_DEFAULT==DL_
37fd6 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
37fd7 53 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  S ){.          s
37fd8 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
37fd9 73 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66  sizeof(buf), buf
37fda 2c 20 22 25 64 2c 25 64 2c 25 64 20 22 2c 0a 20  , "%d,%d,%d ",. 
37fdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fdc 20 20 20 20 20 20 20 20 20 20 70 6c 72 50 6f 73            plrPos
37fdd 69 74 69 6f 6e 28 26 70 6c 52 65 61 64 65 72 29  ition(&plReader)
37fde 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
37fdf 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72               plr
37fe0 53 74 61 72 74 4f 66 66 73 65 74 28 26 70 6c 52  StartOffset(&plR
37fe1 65 61 64 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66  eader), plrEndOf
37fe2 66 73 65 74 28 26 70 6c 52 65 61 64 65 72 29 29  fset(&plReader))
37fe3 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
37fe4 69 66 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d  if( DL_DEFAULT==
37fe5 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b 0a  DL_POSITIONS ){.
37fe6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
37fe7 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
37fe8 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 25 64  f(buf), buf, "%d
37fe9 20 22 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28   ", plrPosition(
37fea 26 70 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20  &plReader));.   
37feb 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
37fec 20 20 20 20 20 20 61 73 73 65 72 74 28 20 4e 55        assert( NU
37fed 4c 4c 3d 3d 22 55 6e 68 61 6e 64 6c 65 64 20 44  LL=="Unhandled D
37fee 4c 5f 44 45 46 41 55 4c 54 20 76 61 6c 75 65 22  L_DEFAULT value"
37fef 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
37ff0 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41       dataBufferA
37ff1 70 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75 66  ppend(&dump, buf
37ff2 2c 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a  , strlen(buf));.
37ff3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 6c        }.      pl
37ff4 72 44 65 73 74 72 6f 79 28 26 70 6c 52 65 61 64  rDestroy(&plRead
37ff5 65 72 29 3b 0a 0a 20 20 20 20 20 20 61 73 73 65  er);..      asse
37ff6 72 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e 30  rt( dump.nData>0
37ff7 20 29 3b 0a 20 20 20 20 20 20 64 75 6d 70 2e 6e   );.      dump.n
37ff8 44 61 74 61 2d 2d 3b 20 20 20 20 20 20 20 20 20  Data--;         
37ff9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
37ffa 76 65 72 77 72 69 74 65 20 74 72 61 69 6c 69 6e  verwrite trailin
37ffb 67 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20  g space. */.    
37ffc 20 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 70    assert( dump.p
37ffd 44 61 74 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d  Data[dump.nData]
37ffe 3d 3d 27 20 27 29 3b 0a 20 20 20 20 20 20 64 61  ==' ');.      da
37fff 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26  taBufferAppend(&
38000 64 75 6d 70 2c 20 22 5d 5d 20 22 2c 20 33 29 3b  dump, "]] ", 3);
38001 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 6c 72  .    }.  }.  dlr
38002 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65  Destroy(&dlReade
38003 72 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64  r);..  assert( d
38004 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20  ump.nData>0 );. 
38005 20 64 75 6d 70 2e 6e 44 61 74 61 2d 2d 3b 20 20   dump.nData--;  
38006 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38007 20 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20     /* Overwrite 
38008 74 72 61 69 6c 69 6e 67 20 73 70 61 63 65 2e 20  trailing space. 
38009 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 75 6d  */.  assert( dum
3800a 70 2e 70 44 61 74 61 5b 64 75 6d 70 2e 6e 44 61  p.pData[dump.nDa
3800b 74 61 5d 3d 3d 27 20 27 29 3b 0a 20 20 64 75 6d  ta]==' ');.  dum
3800c 70 2e 70 44 61 74 61 5b 64 75 6d 70 2e 6e 44 61  p.pData[dump.nDa
3800d 74 61 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 61 73  ta] = '\0';.  as
3800e 73 65 72 74 28 20 64 75 6d 70 2e 6e 44 61 74 61  sert( dump.nData
3800f 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 50 61 73 73  >0 );..  /* Pass
38010 65 73 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20  es ownership of 
38011 64 75 6d 70 27 73 20 62 75 66 66 65 72 20 74 6f  dump's buffer to
38012 20 70 43 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20   pContext. */.  
38013 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
38014 65 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 64 75  ext(pContext, du
38015 6d 70 2e 70 44 61 74 61 2c 20 64 75 6d 70 2e 6e  mp.pData, dump.n
38016 44 61 74 61 2c 20 73 71 6c 69 74 65 33 5f 66 72  Data, sqlite3_fr
38017 65 65 29 3b 0a 20 20 64 75 6d 70 2e 70 44 61 74  ee);.  dump.pDat
38018 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20 64 75 6d 70  a = NULL;.  dump
38019 2e 6e 44 61 74 61 20 3d 20 64 75 6d 70 2e 6e 43  .nData = dump.nC
3801a 61 70 61 63 69 74 79 20 3d 20 30 3b 0a 7d 0a 0a  apacity = 0;.}..
3801b 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74 73 20 64 75  /* Implements du
3801c 6d 70 5f 64 6f 63 6c 69 73 74 28 29 20 66 6f 72  mp_doclist() for
3801d 20 75 73 65 20 69 6e 20 69 6e 73 70 65 63 74 69   use in inspecti
3801e 6e 67 20 74 68 65 20 66 74 73 33 20 69 6e 64 65  ng the fts3 inde
3801f 78 20 66 72 6f 6d 0a 2a 2a 20 74 65 73 74 73 2e  x from.** tests.
38020 20 20 54 45 58 54 20 72 65 73 75 6c 74 20 63 6f    TEXT result co
38021 6e 74 61 69 6e 69 6e 67 20 61 20 73 74 72 69 6e  ntaining a strin
38022 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  g representation
38023 20 6f 66 20 74 68 65 0a 2a 2a 20 64 6f 63 6c 69   of the.** docli
38024 73 74 20 66 6f 72 20 74 68 65 20 69 6e 64 69 63  st for the indic
38025 61 74 65 64 20 74 65 72 6d 2e 20 20 64 75 6d 70  ated term.  dump
38026 5f 64 6f 63 6c 69 73 74 28 74 2c 20 74 65 72 6d  _doclist(t, term
38027 2c 20 6c 65 76 65 6c 2c 20 69 64 78 29 0a 2a 2a  , level, idx).**
38028 20 64 75 6d 70 73 20 74 68 65 20 64 6f 63 6c 69   dumps the docli
38029 73 74 20 66 6f 72 20 74 65 72 6d 20 66 72 6f 6d  st for term from
3802a 20 74 68 65 20 73 65 67 6d 65 6e 74 20 73 70 65   the segment spe
3802b 63 69 66 69 65 64 20 62 79 20 6c 65 76 65 6c 2c  cified by level,
3802c 20 69 64 78 0a 2a 2a 20 28 69 6e 20 25 5f 73 65   idx.** (in %_se
3802d 67 64 69 72 29 2c 20 77 68 69 6c 65 20 64 75 6d  gdir), while dum
3802e 70 5f 64 6f 63 6c 69 73 74 28 74 2c 20 74 65 72  p_doclist(t, ter
3802f 6d 29 20 64 75 6d 70 73 20 74 68 65 20 6c 6f 67  m) dumps the log
38030 69 63 61 6c 0a 2a 2a 20 64 6f 63 6c 69 73 74 20  ical.** doclist 
38031 66 6f 72 20 74 68 65 20 74 65 72 6d 20 61 63 72  for the term acr
38032 6f 73 73 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73  oss all segments
38033 2e 20 20 54 68 65 20 70 65 72 2d 73 65 67 6d 65  .  The per-segme
38034 6e 74 20 64 6f 63 6c 69 73 74 0a 2a 2a 20 63 61  nt doclist.** ca
38035 6e 20 63 6f 6e 74 61 69 6e 20 64 65 6c 65 74 69  n contain deleti
38036 6f 6e 73 2c 20 77 68 69 6c 65 20 74 68 65 20 66  ons, while the f
38037 75 6c 6c 2d 69 6e 64 65 78 20 64 6f 63 6c 69 73  ull-index doclis
38038 74 20 77 69 6c 6c 20 6e 6f 74 0a 2a 2a 20 28 64  t will not.** (d
38039 65 6c 65 74 69 6f 6e 73 20 61 72 65 20 6f 6d 69  eletions are omi
3803a 74 74 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73  tted)..**.** Res
3803b 75 6c 74 20 66 6f 72 6d 61 74 73 20 64 69 66 66  ult formats diff
3803c 65 72 20 77 69 74 68 20 74 68 65 20 73 65 74 74  er with the sett
3803d 69 6e 67 20 6f 66 20 44 4c 5f 44 45 46 41 55 4c  ing of DL_DEFAUL
3803e 54 53 2e 20 20 45 78 61 6d 70 6c 65 73 3a 0a 2a  TS.  Examples:.*
3803f 2a 0a 2a 2a 20 44 4c 5f 44 4f 43 49 44 53 3a 20  *.** DL_DOCIDS: 
38040 5b 31 5d 20 5b 33 5d 20 5b 37 5d 0a 2a 2a 20 44  [1] [3] [7].** D
38041 4c 5f 50 4f 53 49 54 49 4f 4e 53 3a 20 5b 31 20  L_POSITIONS: [1 
38042 30 5b 30 20 34 5d 20 31 5b 31 37 5d 5d 20 5b 33  0[0 4] 1[17]] [3
38043 20 31 5b 35 5d 5d 0a 2a 2a 20 44 4c 5f 50 4f 53   1[5]].** DL_POS
38044 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 3a 20  ITIONS_OFFSETS: 
38045 5b 31 20 30 5b 30 2c 30 2c 33 20 34 2c 32 33 2c  [1 0[0,0,3 4,23,
38046 32 36 5d 20 31 5b 31 37 2c 31 30 32 2c 31 30 35  26] 1[17,102,105
38047 5d 5d 20 5b 33 20 31 5b 35 2c 32 30 2c 32 33 5d  ]] [3 1[5,20,23]
38048 5d 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 61 63 68 20  ].**.** In each 
38049 63 61 73 65 20 74 68 65 20 6e 75 6d 62 65 72 20  case the number 
3804a 61 66 74 65 72 20 74 68 65 20 6f 75 74 65 72 20  after the outer 
3804b 27 5b 27 20 69 73 20 74 68 65 20 64 6f 63 69 64  '[' is the docid
3804c 2e 20 20 49 6e 20 74 68 65 0a 2a 2a 20 6c 61 74  .  In the.** lat
3804d 74 65 72 20 74 77 6f 20 63 61 73 65 73 2c 20 74  ter two cases, t
3804e 68 65 20 6e 75 6d 62 65 72 20 62 65 66 6f 72 65  he number before
3804f 20 74 68 65 20 69 6e 6e 65 72 20 27 5b 27 20 69   the inner '[' i
38050 73 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  s the column.** 
38051 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
38052 74 68 65 20 76 61 6c 75 65 73 20 77 69 74 68 69  the values withi
38053 6e 2e 20 20 46 6f 72 20 44 4c 5f 50 4f 53 49 54  n.  For DL_POSIT
38054 49 4f 4e 53 20 74 68 65 20 6e 75 6d 62 65 72 73  IONS the numbers
38055 0a 2a 2a 20 77 69 74 68 69 6e 20 61 72 65 20 74  .** within are t
38056 68 65 20 70 6f 73 69 74 69 6f 6e 73 2c 20 66 6f  he positions, fo
38057 72 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  r DL_POSITIONS_O
38058 46 46 53 45 54 53 20 74 68 65 79 20 61 72 65 20  FFSETS they are 
38059 74 68 65 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 2c  the.** position,
3805a 20 74 68 65 20 73 74 61 72 74 20 6f 66 66 73 65   the start offse
3805b 74 2c 20 61 6e 64 20 74 68 65 20 65 6e 64 20 6f  t, and the end o
3805c 66 66 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ffset..*/.static
3805d 20 76 6f 69 64 20 64 75 6d 70 44 6f 63 6c 69 73   void dumpDoclis
3805e 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  tFunc(.  sqlite3
3805f 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65  _context *pConte
38060 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  xt,.  int argc, 
38061 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
38062 61 72 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65  argv.){.  fullte
38063 78 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  xt_cursor *pCurs
38064 6f 72 3b 0a 20 20 69 66 28 20 61 72 67 63 21 3d  or;.  if( argc!=
38065 32 20 26 26 20 61 72 67 63 21 3d 34 20 29 7b 0a  2 && argc!=4 ){.
38066 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
38067 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
38068 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22 69 6e 63  p_doclist", "inc
38069 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73  orrect arguments
3806a 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ");.  }else if( 
3806b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
3806c 70 65 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c  pe(argv[0])!=SQL
3806d 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20  ITE_BLOB ||.    
3806e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
3806f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
38070 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75  [0])!=sizeof(pCu
38071 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 67 65 6e  rsor) ){.    gen
38072 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74  erateError(pCont
38073 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69  ext, "dump_docli
38074 73 74 22 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69  st", "illegal fi
38075 72 73 74 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a  rst argument");.
38076 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
38077 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
38078 72 67 76 5b 31 5d 29 3d 3d 4e 55 4c 4c 20 7c 7c  rgv[1])==NULL ||
38079 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
3807a 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
3807b 61 72 67 76 5b 31 5d 29 5b 30 5d 3d 3d 27 5c 30  argv[1])[0]=='\0
3807c 27 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74  ' ){.    generat
3807d 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  eError(pContext,
3807e 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c   "dump_doclist",
3807f 20 22 65 6d 70 74 79 20 73 65 63 6f 6e 64 20 61   "empty second a
38080 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c  rgument");.  }el
38081 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  se{.    const ch
38082 61 72 20 2a 70 54 65 72 6d 20 3d 20 28 63 6f 6e  ar *pTerm = (con
38083 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
38084 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
38085 76 5b 31 5d 29 3b 0a 20 20 20 20 63 6f 6e 73 74  v[1]);.    const
38086 20 69 6e 74 20 6e 54 65 72 6d 20 3d 20 73 74 72   int nTerm = str
38087 6c 65 6e 28 70 54 65 72 6d 29 3b 0a 20 20 20 20  len(pTerm);.    
38088 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
38089 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  ;.    int rc;.  
3808a 20 20 44 61 74 61 42 75 66 66 65 72 20 64 6f 63    DataBuffer doc
3808b 6c 69 73 74 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  list;..    memcp
3808c 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69  y(&pCursor, sqli
3808d 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
3808e 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28  rgv[0]), sizeof(
3808f 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 76  pCursor));.    v
38090 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28 70   = cursor_vtab(p
38091 43 75 72 73 6f 72 29 3b 0a 0a 20 20 20 20 64 61  Cursor);..    da
38092 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 64 6f  taBufferInit(&do
38093 63 6c 69 73 74 2c 20 30 29 3b 0a 0a 20 20 20 20  clist, 0);..    
38094 2f 2a 20 74 65 72 6d 53 65 6c 65 63 74 28 29 20  /* termSelect() 
38095 79 69 65 6c 64 73 20 74 68 65 20 73 61 6d 65 20  yields the same 
38096 6c 6f 67 69 63 61 6c 20 64 6f 63 6c 69 73 74 20  logical doclist 
38097 74 68 61 74 20 71 75 65 72 69 65 73 20 61 72 65  that queries are
38098 0a 20 20 20 20 2a 2a 20 72 75 6e 20 61 67 61 69  .    ** run agai
38099 6e 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  nst..    */.    
3809a 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20  if( argc==2 ){. 
3809b 20 20 20 20 20 72 63 20 3d 20 74 65 72 6d 53 65       rc = termSe
3809c 6c 65 63 74 28 76 2c 20 76 2d 3e 6e 43 6f 6c 75  lect(v, v->nColu
3809d 6d 6e 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  mn, pTerm, nTerm
3809e 2c 20 30 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c  , 0, DL_DEFAULT,
3809f 20 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20   &doclist);.    
380a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
380a1 69 74 65 33 5f 73 74 6d 74 20 2a 73 20 3d 20 4e  ite3_stmt *s = N
380a2 55 4c 4c 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47  ULL;..      /* G
380a3 65 74 20 6f 75 72 20 73 70 65 63 69 66 69 63 20  et our specific 
380a4 73 65 67 6d 65 6e 74 27 73 20 69 6e 66 6f 72 6d  segment's inform
380a5 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20  ation. */.      
380a6 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
380a7 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52  tement(v, SEGDIR
380a8 5f 53 45 4c 45 43 54 5f 53 45 47 4d 45 4e 54 5f  _SELECT_SEGMENT_
380a9 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 20 20 20  STMT, &s);.     
380aa 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
380ab 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
380ac 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
380ad 69 6e 74 28 73 2c 20 31 2c 20 73 71 6c 69 74 65  int(s, 1, sqlite
380ae 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
380af 5b 32 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 69  [2]));.        i
380b0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
380b1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
380b2 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
380b3 69 6e 74 28 73 2c 20 32 2c 20 73 71 6c 69 74 65  int(s, 2, sqlite
380b4 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
380b5 5b 33 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 7d  [3]));.        }
380b6 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
380b7 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
380b8 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
380b9 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
380ba 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
380bb 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
380bc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 61 74  ){.          dat
380bd 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
380be 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 20 20  doclist);.      
380bf 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
380c0 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
380c1 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22 73 65 67  p_doclist", "seg
380c2 6d 65 6e 74 20 6e 6f 74 20 66 6f 75 6e 64 22 29  ment not found")
380c3 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
380c4 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  rn;.        }.. 
380c5 20 20 20 20 20 20 20 2f 2a 20 46 6f 75 6e 64 20         /* Found 
380c6 61 20 73 65 67 6d 65 6e 74 2c 20 6c 6f 61 64 20  a segment, load 
380c7 69 74 20 69 6e 74 6f 20 64 6f 63 6c 69 73 74 2e  it into doclist.
380c8 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
380c9 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
380ca 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73  {.          cons
380cb 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  t sqlite_int64 i
380cc 4c 65 61 76 65 73 45 6e 64 20 3d 20 73 71 6c 69  LeavesEnd = sqli
380cd 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
380ce 28 73 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  (s, 1);.        
380cf 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44    const char *pD
380d0 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ata = sqlite3_co
380d1 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20 32 29 3b  lumn_blob(s, 2);
380d2 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74  .          const
380d3 20 69 6e 74 20 6e 44 61 74 61 20 3d 20 73 71 6c   int nData = sql
380d4 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
380d5 73 28 73 2c 20 32 29 3b 0a 0a 20 20 20 20 20 20  s(s, 2);..      
380d6 20 20 20 20 2f 2a 20 6c 6f 61 64 53 65 67 6d 65      /* loadSegme
380d7 6e 74 28 29 20 69 73 20 75 73 65 64 20 62 79 20  nt() is used by 
380d8 74 65 72 6d 53 65 6c 65 63 74 28 29 20 74 6f 20  termSelect() to 
380d9 6c 6f 61 64 20 65 61 63 68 0a 20 20 20 20 20 20  load each.      
380da 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 27 73      ** segment's
380db 20 64 61 74 61 2e 0a 20 20 20 20 20 20 20 20 20   data..         
380dc 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63   */.          rc
380dd 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 76   = loadSegment(v
380de 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , pData, nData, 
380df 69 4c 65 61 76 65 73 45 6e 64 2c 20 70 54 65 72  iLeavesEnd, pTer
380e0 6d 2c 20 6e 54 65 72 6d 2c 20 30 2c 0a 20 20 20  m, nTerm, 0,.   
380e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
380e2 20 20 20 20 20 20 20 20 26 64 6f 63 6c 69 73 74          &doclist
380e3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
380e4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
380e5 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
380e6 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
380e7 73 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 20  s);..           
380e8 20 2f 2a 20 53 68 6f 75 6c 64 20 6e 6f 74 20 68   /* Should not h
380e9 61 76 65 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  ave more than on
380ea 65 20 6d 61 74 63 68 69 6e 67 20 73 65 67 6d 65  e matching segme
380eb 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  nt. */.         
380ec 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
380ed 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20  E_DONE ){.      
380ee 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
380ef 72 65 73 65 74 28 73 29 3b 0a 20 20 20 20 20 20  reset(s);.      
380f0 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
380f1 65 72 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69  erDestroy(&docli
380f2 73 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  st);.           
380f3 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
380f4 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
380f5 5f 64 6f 63 6c 69 73 74 22 2c 20 22 69 6e 76 61  _doclist", "inva
380f6 6c 69 64 20 73 65 67 64 69 72 22 29 3b 0a 20 20  lid segdir");.  
380f7 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
380f8 72 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  rn;.            
380f9 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  }.            rc
380fa 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
380fb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
380fc 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
380fd 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
380fe 28 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  (s);.    }..    
380ff 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
38100 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64  K ){.      if( d
38101 6f 63 6c 69 73 74 2e 6e 44 61 74 61 3e 30 20 29  oclist.nData>0 )
38102 7b 0a 20 20 20 20 20 20 20 20 63 72 65 61 74 65  {.        create
38103 44 6f 63 6c 69 73 74 52 65 73 75 6c 74 28 70 43  DoclistResult(pC
38104 6f 6e 74 65 78 74 2c 20 64 6f 63 6c 69 73 74 2e  ontext, doclist.
38105 70 44 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e  pData, doclist.n
38106 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Data);.      }el
38107 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  se{.        /* T
38108 4f 44 4f 28 73 68 65 73 73 29 3a 20 54 68 69 73  ODO(shess): This
38109 20 63 61 6e 20 68 61 70 70 65 6e 20 69 66 20 74   can happen if t
3810a 68 65 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 70  he term is not p
3810b 72 65 73 65 6e 74 2c 20 6f 72 0a 20 20 20 20 20  resent, or.     
3810c 20 20 20 2a 2a 20 69 66 20 61 6c 6c 20 69 6e 73     ** if all ins
3810d 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 74 65  tances of the te
3810e 72 6d 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c  rm have been del
3810f 65 74 65 64 20 61 6e 64 20 74 68 69 73 20 69 73  eted and this is
38110 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 61  .        ** an a
38111 6c 6c 2d 69 6e 64 65 78 20 64 75 6d 70 2e 20 20  ll-index dump.  
38112 49 74 20 6d 61 79 20 62 65 20 69 6e 74 65 72 65  It may be intere
38113 73 74 69 6e 67 20 74 6f 20 64 69 73 74 69 6e 67  sting to disting
38114 75 69 73 68 0a 20 20 20 20 20 20 20 20 2a 2a 20  uish.        ** 
38115 74 68 65 73 65 20 63 61 73 65 73 2e 0a 20 20 20  these cases..   
38116 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
38117 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
38118 65 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 22 22  ext(pContext, ""
38119 2c 20 30 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  , 0, SQLITE_STAT
3811a 49 43 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  IC);.      }.   
3811b 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53   }else if( rc==S
3811c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
3811d 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 6f       /* Handle o
3811e 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 73  ut-of-memory cas
3811f 65 73 20 73 70 65 63 69 61 6c 6c 79 20 62 65 63  es specially bec
38120 61 75 73 65 20 69 66 20 74 68 65 79 20 61 72 65  ause if they are
38121 0a 20 20 20 20 20 20 2a 2a 20 67 65 6e 65 72 61  .      ** genera
38122 74 65 64 20 69 6e 20 66 74 73 33 20 63 6f 64 65  ted in fts3 code
38123 20 74 68 65 79 20 6d 61 79 20 6e 6f 74 20 62 65   they may not be
38124 20 72 65 66 6c 65 63 74 65 64 20 69 6e 20 74 68   reflected in th
38125 65 20 64 62 0a 20 20 20 20 20 20 2a 2a 20 68 61  e db.      ** ha
38126 6e 64 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ndle..      */. 
38127 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65       /* TODO(she
38128 73 73 29 3a 20 48 61 6e 64 6c 65 20 74 68 69 73  ss): Handle this
38129 20 6d 6f 72 65 20 63 6f 6d 70 72 65 68 65 6e 73   more comprehens
3812a 69 76 65 6c 79 2e 0a 20 20 20 20 20 20 2a 2a 20  ively..      ** 
3812b 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 29 20  sqlite3ErrStr() 
3812c 68 61 73 20 77 68 61 74 20 49 20 6e 65 65 64 2c  has what I need,
3812d 20 62 75 74 20 69 73 20 69 6e 74 65 72 6e 61 6c   but is internal
3812e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
3812f 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70   generateError(p
38130 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64  Context, "dump_d
38131 6f 63 6c 69 73 74 22 2c 20 22 6f 75 74 20 6f 66  oclist", "out of
38132 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20 7d   memory");.    }
38133 65 6c 73 65 7b 0a 20 20 20 20 20 20 67 65 6e 65  else{.      gene
38134 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65  rateError(pConte
38135 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73  xt, "dump_doclis
38136 74 22 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d  t", NULL);.    }
38137 0a 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ..    dataBuffer
38138 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74  Destroy(&doclist
38139 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  );.  }.}.#endif.
3813a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
3813b 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ine implements t
3813c 68 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e  he xFindFunction
3813d 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 65 20   method for the 
3813e 46 54 53 33 0a 2a 2a 20 76 69 72 74 75 61 6c 20  FTS3.** virtual 
3813f 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  table..*/.static
38140 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 46 69 6e   int fulltextFin
38141 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c  dFunction(.  sql
38142 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
38143 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20  ,.  int nArg,.  
38144 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
38145 65 2c 0a 20 20 76 6f 69 64 20 28 2a 2a 70 78 46  e,.  void (**pxF
38146 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
38147 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
38148 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f  3_value**),.  vo
38149 69 64 20 2a 2a 70 70 41 72 67 0a 29 7b 0a 20 20  id **ppArg.){.  
3814a 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65  if( strcmp(zName
3814b 2c 22 73 6e 69 70 70 65 74 22 29 3d 3d 30 20 29  ,"snippet")==0 )
3814c 7b 0a 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20  {.    *pxFunc = 
3814d 73 6e 69 70 70 65 74 46 75 6e 63 3b 0a 20 20 20  snippetFunc;.   
3814e 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c   return 1;.  }el
3814f 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e  se if( strcmp(zN
38150 61 6d 65 2c 22 6f 66 66 73 65 74 73 22 29 3d 3d  ame,"offsets")==
38151 30 20 29 7b 0a 20 20 20 20 2a 70 78 46 75 6e 63  0 ){.    *pxFunc
38152 20 3d 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74   = snippetOffset
38153 73 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72  sFunc;.    retur
38154 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 1;.  }else if(
38155 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 6f   strcmp(zName,"o
38156 70 74 69 6d 69 7a 65 22 29 3d 3d 30 20 29 7b 0a  ptimize")==0 ){.
38157 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 6f 70      *pxFunc = op
38158 74 69 6d 69 7a 65 46 75 6e 63 3b 0a 20 20 20 20  timizeFunc;.    
38159 72 65 74 75 72 6e 20 31 3b 0a 23 69 66 64 65 66  return 1;.#ifdef
3815a 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20   SQLITE_TEST.   
3815b 20 2f 2a 20 4e 4f 54 45 28 73 68 65 73 73 29 3a   /* NOTE(shess):
3815c 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   These functions
3815d 20 61 72 65 20 70 72 65 73 65 6e 74 20 6f 6e 6c   are present onl
3815e 79 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 20 20  y for testing.  
3815f 20 20 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20    ** purposes.  
38160 4e 6f 20 70 61 72 74 69 63 75 6c 61 72 20 65 66  No particular ef
38161 66 6f 72 74 20 69 73 20 6d 61 64 65 20 74 6f 20  fort is made to 
38162 6f 70 74 69 6d 69 7a 65 20 74 68 65 69 72 0a 20  optimize their. 
38163 20 20 20 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20     ** execution 
38164 6f 72 20 68 6f 77 20 74 68 65 79 20 62 75 69 6c  or how they buil
38165 64 20 74 68 65 69 72 20 72 65 73 75 6c 74 73 2e  d their results.
38166 0a 20 20 20 20 2a 2f 0a 20 20 7d 65 6c 73 65 20  .    */.  }else 
38167 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65  if( strcmp(zName
38168 2c 22 64 75 6d 70 5f 74 65 72 6d 73 22 29 3d 3d  ,"dump_terms")==
38169 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66 70 72 69  0 ){.    /* fpri
3816a 6e 74 66 28 73 74 64 65 72 72 2c 20 22 46 6f 75  ntf(stderr, "Fou
3816b 6e 64 20 64 75 6d 70 5f 74 65 72 6d 73 5c 6e 22  nd dump_terms\n"
3816c 29 3b 20 2a 2f 0a 20 20 20 20 2a 70 78 46 75 6e  ); */.    *pxFun
3816d 63 20 3d 20 64 75 6d 70 54 65 72 6d 73 46 75 6e  c = dumpTermsFun
3816e 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  c;.    return 1;
3816f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72  .  }else if( str
38170 63 6d 70 28 7a 4e 61 6d 65 2c 22 64 75 6d 70 5f  cmp(zName,"dump_
38171 64 6f 63 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a  doclist")==0 ){.
38172 20 20 20 20 2f 2a 20 66 70 72 69 6e 74 66 28 73      /* fprintf(s
38173 74 64 65 72 72 2c 20 22 46 6f 75 6e 64 20 64 75  tderr, "Found du
38174 6d 70 5f 64 6f 63 6c 69 73 74 5c 6e 22 29 3b 20  mp_doclist\n"); 
38175 2a 2f 0a 20 20 20 20 2a 70 78 46 75 6e 63 20 3d  */.    *pxFunc =
38176 20 64 75 6d 70 44 6f 63 6c 69 73 74 46 75 6e 63   dumpDoclistFunc
38177 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
38178 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74  #endif.  }.  ret
38179 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
3817a 52 65 6e 61 6d 65 20 61 6e 20 66 74 73 33 20 74  Rename an fts3 t
3817b 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
3817c 69 6e 74 20 66 75 6c 6c 74 65 78 74 52 65 6e 61  int fulltextRena
3817d 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  me(.  sqlite3_vt
3817e 61 62 20 2a 70 56 74 61 62 2c 0a 20 20 63 6f 6e  ab *pVtab,.  con
3817f 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 0a 29  st char *zName.)
38180 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  {.  fulltext_vta
38181 62 20 2a 70 20 3d 20 28 66 75 6c 6c 74 65 78 74  b *p = (fulltext
38182 5f 76 74 61 62 20 2a 29 70 56 74 61 62 3b 0a 20  _vtab *)pVtab;. 
38183 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
38184 5f 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61 72 20 2a  _NOMEM;.  char *
38185 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zSql = sqlite3_m
38186 70 72 69 6e 74 66 28 0a 20 20 20 20 22 41 4c 54  printf(.    "ALT
38187 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f  ER TABLE %Q.'%q_
38188 63 6f 6e 74 65 6e 74 27 20 20 52 45 4e 41 4d 45  content'  RENAME
38189 20 54 4f 20 27 25 71 5f 63 6f 6e 74 65 6e 74 27   TO '%q_content'
3818a 3b 22 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41  ;".    "ALTER TA
3818b 42 4c 45 20 25 51 2e 27 25 71 5f 73 65 67 6d 65  BLE %Q.'%q_segme
3818c 6e 74 73 27 20 52 45 4e 41 4d 45 20 54 4f 20 27  nts' RENAME TO '
3818d 25 71 5f 73 65 67 6d 65 6e 74 73 27 3b 22 0a 20  %q_segments';". 
3818e 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20     "ALTER TABLE 
3818f 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20 20  %Q.'%q_segdir'  
38190 20 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 73   RENAME TO '%q_s
38191 65 67 64 69 72 27 3b 22 0a 20 20 20 20 2c 20 70  egdir';".    , p
38192 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c  ->zDb, p->zName,
38193 20 7a 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 2d   zName .    , p-
38194 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20  >zDb, p->zName, 
38195 7a 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 2d 3e  zName .    , p->
38196 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a  zDb, p->zName, z
38197 4e 61 6d 65 0a 20 20 29 3b 0a 20 20 69 66 28 20  Name.  );.  if( 
38198 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d  zSql ){.    rc =
38199 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 70 2d   sqlite3_exec(p-
3819a 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c  >db, zSql, 0, 0,
3819b 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
3819c 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d  _free(zSql);.  }
3819d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3819e 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
3819f 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 66 74 73  lite3_module fts
381a0 33 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 2f 2a  3Module = {.  /*
381a1 20 69 56 65 72 73 69 6f 6e 20 20 20 20 20 20 2a   iVersion      *
381a2 2f 20 30 2c 0a 20 20 2f 2a 20 78 43 72 65 61 74  / 0,.  /* xCreat
381a3 65 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74  e       */ fullt
381a4 65 78 74 43 72 65 61 74 65 2c 0a 20 20 2f 2a 20  extCreate,.  /* 
381a5 78 43 6f 6e 6e 65 63 74 20 20 20 20 20 20 2a 2f  xConnect      */
381a6 20 66 75 6c 6c 74 65 78 74 43 6f 6e 6e 65 63 74   fulltextConnect
381a7 2c 0a 20 20 2f 2a 20 78 42 65 73 74 49 6e 64 65  ,.  /* xBestInde
381a8 78 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74  x    */ fulltext
381a9 42 65 73 74 49 6e 64 65 78 2c 0a 20 20 2f 2a 20  BestIndex,.  /* 
381aa 78 44 69 73 63 6f 6e 6e 65 63 74 20 20 20 2a 2f  xDisconnect   */
381ab 20 66 75 6c 6c 74 65 78 74 44 69 73 63 6f 6e 6e   fulltextDisconn
381ac 65 63 74 2c 0a 20 20 2f 2a 20 78 44 65 73 74 72  ect,.  /* xDestr
381ad 6f 79 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74  oy      */ fullt
381ae 65 78 74 44 65 73 74 72 6f 79 2c 0a 20 20 2f 2a  extDestroy,.  /*
381af 20 78 4f 70 65 6e 20 20 20 20 20 20 20 20 20 2a   xOpen         *
381b0 2f 20 66 75 6c 6c 74 65 78 74 4f 70 65 6e 2c 0a  / fulltextOpen,.
381b1 20 20 2f 2a 20 78 43 6c 6f 73 65 20 20 20 20 20    /* xClose     
381b2 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43 6c     */ fulltextCl
381b3 6f 73 65 2c 0a 20 20 2f 2a 20 78 46 69 6c 74 65  ose,.  /* xFilte
381b4 72 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74  r       */ fullt
381b5 65 78 74 46 69 6c 74 65 72 2c 0a 20 20 2f 2a 20  extFilter,.  /* 
381b6 78 4e 65 78 74 20 20 20 20 20 20 20 20 20 2a 2f  xNext         */
381b7 20 66 75 6c 6c 74 65 78 74 4e 65 78 74 2c 0a 20   fulltextNext,. 
381b8 20 2f 2a 20 78 45 6f 66 20 20 20 20 20 20 20 20   /* xEof        
381b9 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 45 6f 66    */ fulltextEof
381ba 2c 0a 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 20  ,.  /* xColumn  
381bb 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74       */ fulltext
381bc 43 6f 6c 75 6d 6e 2c 0a 20 20 2f 2a 20 78 52 6f  Column,.  /* xRo
381bd 77 69 64 20 20 20 20 20 20 20 20 2a 2f 20 66 75  wid        */ fu
381be 6c 6c 74 65 78 74 52 6f 77 69 64 2c 0a 20 20 2f  lltextRowid,.  /
381bf 2a 20 78 55 70 64 61 74 65 20 20 20 20 20 20 20  * xUpdate       
381c0 2a 2f 20 66 75 6c 6c 74 65 78 74 55 70 64 61 74  */ fulltextUpdat
381c1 65 2c 0a 20 20 2f 2a 20 78 42 65 67 69 6e 20 20  e,.  /* xBegin  
381c2 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
381c3 74 42 65 67 69 6e 2c 0a 20 20 2f 2a 20 78 53 79  tBegin,.  /* xSy
381c4 6e 63 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75  nc         */ fu
381c5 6c 6c 74 65 78 74 53 79 6e 63 2c 0a 20 20 2f 2a  lltextSync,.  /*
381c6 20 78 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 2a   xCommit       *
381c7 2f 20 66 75 6c 6c 74 65 78 74 43 6f 6d 6d 69 74  / fulltextCommit
381c8 2c 0a 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b  ,.  /* xRollback
381c9 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74       */ fulltext
381ca 52 6f 6c 6c 62 61 63 6b 2c 0a 20 20 2f 2a 20 78  Rollback,.  /* x
381cb 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 2a 2f 20  FindFunction */ 
381cc 66 75 6c 6c 74 65 78 74 46 69 6e 64 46 75 6e 63  fulltextFindFunc
381cd 74 69 6f 6e 2c 0a 20 20 2f 2a 20 78 52 65 6e 61  tion,.  /* xRena
381ce 6d 65 20 2a 2f 20 20 20 20 20 20 20 66 75 6c 6c  me */       full
381cf 74 65 78 74 52 65 6e 61 6d 65 2c 0a 7d 3b 0a 0a  textRename,.};..
381d0 73 74 61 74 69 63 20 76 6f 69 64 20 68 61 73 68  static void hash
381d1 44 65 73 74 72 6f 79 28 76 6f 69 64 20 2a 70 29  Destroy(void *p)
381d2 7b 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48  {.  fts3Hash *pH
381d3 61 73 68 20 3d 20 28 66 74 73 33 48 61 73 68 20  ash = (fts3Hash 
381d4 2a 29 70 3b 0a 20 20 73 71 6c 69 74 65 33 46 74  *)p;.  sqlite3Ft
381d5 73 33 48 61 73 68 43 6c 65 61 72 28 70 48 61 73  s3HashClear(pHas
381d6 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  h);.  sqlite3_fr
381d7 65 65 28 70 48 61 73 68 29 3b 0a 7d 0a 0a 2f 2a  ee(pHash);.}../*
381d8 0a 2a 2a 20 54 68 65 20 66 74 73 33 20 62 75 69  .** The fts3 bui
381d9 6c 74 2d 69 6e 20 74 6f 6b 65 6e 69 7a 65 72 73  lt-in tokenizers
381da 20 2d 20 22 73 69 6d 70 6c 65 22 20 61 6e 64 20   - "simple" and 
381db 22 70 6f 72 74 65 72 22 20 2d 20 61 72 65 20 69  "porter" - are i
381dc 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 69 6e  mplemented.** in
381dd 20 66 69 6c 65 73 20 66 74 73 33 5f 74 6f 6b 65   files fts3_toke
381de 6e 69 7a 65 72 31 2e 63 20 61 6e 64 20 66 74 73  nizer1.c and fts
381df 33 5f 70 6f 72 74 65 72 2e 63 20 72 65 73 70 65  3_porter.c respe
381e0 63 74 69 76 65 6c 79 2e 20 54 68 65 20 66 6f 6c  ctively. The fol
381e1 6c 6f 77 69 6e 67 0a 2a 2a 20 74 77 6f 20 66 6f  lowing.** two fo
381e2 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
381e3 6e 73 20 61 72 65 20 66 6f 72 20 66 75 6e 63 74  ns are for funct
381e4 69 6f 6e 73 20 64 65 63 6c 61 72 65 64 20 69 6e  ions declared in
381e5 20 74 68 65 73 65 20 66 69 6c 65 73 0a 2a 2a 20   these files.** 
381e6 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65  used to retrieve
381e7 20 74 68 65 20 72 65 73 70 65 63 74 69 76 65 20   the respective 
381e8 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e  implementations.
381e9 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 73  .**.** Calling s
381ea 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65  qlite3Fts3Simple
381eb 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
381ec 29 20 73 65 74 73 20 74 68 65 20 76 61 6c 75 65  ) sets the value
381ed 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62   pointed.** to b
381ee 79 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74  y the argument t
381ef 6f 20 70 6f 69 6e 74 20 61 20 74 68 65 20 22 73  o point a the "s
381f0 69 6d 70 6c 65 22 20 74 6f 6b 65 6e 69 7a 65 72  imple" tokenizer
381f1 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
381f2 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 2e 2e 2e  .** Function ...
381f3 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d  PorterTokenizerM
381f4 6f 64 75 6c 65 28 29 20 73 65 74 73 20 2a 70 4d  odule() sets *pM
381f5 6f 64 75 6c 65 20 74 6f 20 70 6f 69 6e 74 20 74  odule to point t
381f6 6f 20 74 68 65 0a 2a 2a 20 70 6f 72 74 65 72 20  o the.** porter 
381f7 74 6f 6b 65 6e 69 7a 65 72 2f 73 74 65 6d 6d 65  tokenizer/stemme
381f8 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  r implementation
381f9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
381fa 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
381fb 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69  Fts3SimpleTokeni
381fc 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65  zerModule(sqlite
381fd 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
381fe 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75  le const**ppModu
381ff 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  le);.SQLITE_PRIV
38200 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
38201 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e 69  Fts3PorterTokeni
38202 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65  zerModule(sqlite
38203 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
38204 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75  le const**ppModu
38205 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  le);.SQLITE_PRIV
38206 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
38207 46 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72  Fts3IcuTokenizer
38208 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74  Module(sqlite3_t
38209 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
3820a 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29  const**ppModule)
3820b 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
3820c 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
3820d 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28 73  3InitHashTable(s
3820e 71 6c 69 74 65 33 20 2a 2c 20 66 74 73 33 48 61  qlite3 *, fts3Ha
3820f 73 68 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  sh *, const char
38210 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74   *);../*.** Init
38211 69 61 6c 69 73 65 20 74 68 65 20 66 74 73 33 20  ialise the fts3 
38212 65 78 74 65 6e 73 69 6f 6e 2e 20 49 66 20 74 68  extension. If th
38213 69 73 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20  is extension is 
38214 62 75 69 6c 74 20 61 73 20 70 61 72 74 0a 2a 2a  built as part.**
38215 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20 6c   of the sqlite l
38216 69 62 72 61 72 79 2c 20 74 68 65 6e 20 74 68 69  ibrary, then thi
38217 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
38218 6c 6c 65 64 20 64 69 72 65 63 74 6c 79 20 62 79  lled directly by
38219 0a 2a 2a 20 53 51 4c 69 74 65 2e 20 49 66 20 66  .** SQLite. If f
3821a 74 73 33 20 69 73 20 62 75 69 6c 74 20 61 73 20  ts3 is built as 
3821b 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 6c 6f  a dynamically lo
3821c 61 64 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e  adable extension
3821d 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69  , this.** functi
3821e 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  on is called by 
3821f 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65  the sqlite3_exte
38220 6e 73 69 6f 6e 5f 69 6e 69 74 28 29 20 65 6e 74  nsion_init() ent
38221 72 79 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c  ry point..*/.SQL
38222 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
38223 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28  sqlite3Fts3Init(
38224 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
38225 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
38226 4f 4b 3b 0a 20 20 66 74 73 33 48 61 73 68 20 2a  OK;.  fts3Hash *
38227 70 48 61 73 68 20 3d 20 30 3b 0a 20 20 63 6f 6e  pHash = 0;.  con
38228 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
38229 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 53 69  izer_module *pSi
3822a 6d 70 6c 65 20 3d 20 30 3b 0a 20 20 63 6f 6e 73  mple = 0;.  cons
3822b 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
3822c 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 50 6f 72  zer_module *pPor
3822d 74 65 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74  ter = 0;.  const
3822e 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3822f 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 49 63 75 20  er_module *pIcu 
38230 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 46  = 0;..  sqlite3F
38231 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a  ts3SimpleTokeniz
38232 65 72 4d 6f 64 75 6c 65 28 26 70 53 69 6d 70 6c  erModule(&pSimpl
38233 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 74 73  e);.  sqlite3Fts
38234 33 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72  3PorterTokenizer
38235 4d 6f 64 75 6c 65 28 26 70 50 6f 72 74 65 72 29  Module(&pPorter)
38236 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
38237 45 4e 41 42 4c 45 5f 49 43 55 0a 20 20 73 71 6c  ENABLE_ICU.  sql
38238 69 74 65 33 46 74 73 33 49 63 75 54 6f 6b 65 6e  ite3Fts3IcuToken
38239 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 49 63 75  izerModule(&pIcu
3823a 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20  );.#endif..  /* 
3823b 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69  Allocate and ini
3823c 74 69 61 6c 69 73 65 20 74 68 65 20 68 61 73 68  tialise the hash
3823d 2d 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20 73  -table used to s
3823e 74 6f 72 65 20 74 6f 6b 65 6e 69 7a 65 72 73 2e  tore tokenizers.
3823f 20 2a 2f 0a 20 20 70 48 61 73 68 20 3d 20 73 71   */.  pHash = sq
38240 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
38241 65 6f 66 28 66 74 73 33 48 61 73 68 29 29 3b 0a  eof(fts3Hash));.
38242 20 20 69 66 28 20 21 70 48 61 73 68 20 29 7b 0a    if( !pHash ){.
38243 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
38244 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NOMEM;.  }else{.
38245 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48      sqlite3Fts3H
38246 61 73 68 49 6e 69 74 28 70 48 61 73 68 2c 20 46  ashInit(pHash, F
38247 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c  TS3_HASH_STRING,
38248 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c   1);.  }..  /* L
38249 6f 61 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e  oad the built-in
3824a 20 74 6f 6b 65 6e 69 7a 65 72 73 20 69 6e 74 6f   tokenizers into
3824b 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
3824c 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
3824d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
3824e 28 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  ( sqlite3Fts3Has
3824f 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22  hInsert(pHash, "
38250 73 69 6d 70 6c 65 22 2c 20 37 2c 20 28 76 6f 69  simple", 7, (voi
38251 64 20 2a 29 70 53 69 6d 70 6c 65 29 0a 20 20 20  d *)pSimple).   
38252 20 20 7c 7c 20 73 71 6c 69 74 65 33 46 74 73 33    || sqlite3Fts3
38253 48 61 73 68 49 6e 73 65 72 74 28 70 48 61 73 68  HashInsert(pHash
38254 2c 20 22 70 6f 72 74 65 72 22 2c 20 37 2c 20 28  , "porter", 7, (
38255 76 6f 69 64 20 2a 29 70 50 6f 72 74 65 72 29 20  void *)pPorter) 
38256 0a 20 20 20 20 20 7c 7c 20 28 70 49 63 75 20 26  .     || (pIcu &
38257 26 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  & sqlite3Fts3Has
38258 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22  hInsert(pHash, "
38259 69 63 75 22 2c 20 34 2c 20 28 76 6f 69 64 20 2a  icu", 4, (void *
3825a 29 70 49 63 75 29 29 0a 20 20 20 20 29 7b 0a 20  )pIcu)).    ){. 
3825b 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
3825c 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
3825d 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
3825e 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 46  _TEST.  sqlite3F
3825f 74 73 33 45 78 70 72 49 6e 69 74 54 65 73 74 49  ts3ExprInitTestI
38260 6e 74 65 72 66 61 63 65 28 64 62 29 3b 0a 23 65  nterface(db);.#e
38261 6e 64 69 66 0a 0a 20 20 2f 2a 20 43 72 65 61 74  ndif..  /* Creat
38262 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  e the virtual ta
38263 62 6c 65 20 77 72 61 70 70 65 72 20 61 72 6f 75  ble wrapper arou
38264 6e 64 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c  nd the hash-tabl
38265 65 20 61 6e 64 20 6f 76 65 72 6c 6f 61 64 20 0a  e and overload .
38266 20 20 2a 2a 20 74 68 65 20 74 77 6f 20 73 63 61    ** the two sca
38267 6c 61 72 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49  lar functions. I
38268 66 20 74 68 69 73 20 69 73 20 73 75 63 63 65 73  f this is succes
38269 73 66 75 6c 2c 20 72 65 67 69 73 74 65 72 20 74  sful, register t
3826a 68 65 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 77  he.  ** module w
3826b 69 74 68 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f  ith sqlite..  */
3826c 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
3826d 3d 3d 72 63 20 0a 20 20 20 26 26 20 53 51 4c 49  ==rc .   && SQLI
3826e 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
3826f 69 74 65 33 46 74 73 33 49 6e 69 74 48 61 73 68  ite3Fts3InitHash
38270 54 61 62 6c 65 28 64 62 2c 20 70 48 61 73 68 2c  Table(db, pHash,
38271 20 22 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72   "fts3_tokenizer
38272 22 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45  ")).   && SQLITE
38273 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
38274 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
38275 74 69 6f 6e 28 64 62 2c 20 22 73 6e 69 70 70 65  tion(db, "snippe
38276 74 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20 53  t", -1)).   && S
38277 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
38278 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
38279 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 6f  _function(db, "o
3827a 66 66 73 65 74 73 22 2c 20 2d 31 29 29 0a 20 20  ffsets", -1)).  
3827b 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28   && SQLITE_OK==(
3827c 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65  rc = sqlite3_ove
3827d 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64  rload_function(d
3827e 62 2c 20 22 6f 70 74 69 6d 69 7a 65 22 2c 20 2d  b, "optimize", -
3827f 31 29 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54  1)).#ifdef SQLIT
38280 45 5f 54 45 53 54 0a 20 20 20 26 26 20 53 51 4c  E_TEST.   && SQL
38281 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71  ITE_OK==(rc = sq
38282 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66  lite3_overload_f
38283 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 64 75 6d  unction(db, "dum
38284 70 5f 74 65 72 6d 73 22 2c 20 2d 31 29 29 0a 20  p_terms", -1)). 
38285 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
38286 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76  (rc = sqlite3_ov
38287 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
38288 64 62 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73  db, "dump_doclis
38289 74 22 2c 20 2d 31 29 29 0a 23 65 6e 64 69 66 0a  t", -1)).#endif.
3828a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20    ){.    return 
3828b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
3828c 6f 64 75 6c 65 5f 76 32 28 0a 20 20 20 20 20 20  odule_v2(.      
3828d 20 20 64 62 2c 20 22 66 74 73 33 22 2c 20 26 66    db, "fts3", &f
3828e 74 73 33 4d 6f 64 75 6c 65 2c 20 28 76 6f 69 64  ts3Module, (void
3828f 20 2a 29 70 48 61 73 68 2c 20 68 61 73 68 44 65   *)pHash, hashDe
38290 73 74 72 6f 79 0a 20 20 20 20 29 3b 0a 20 20 7d  stroy.    );.  }
38291 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20  ..  /* An error 
38292 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 44 65  has occurred. De
38293 6c 65 74 65 20 74 68 65 20 68 61 73 68 20 74 61  lete the hash ta
38294 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ble and return t
38295 68 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a  he error code. *
38296 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d  /.  assert( rc!=
38297 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69  SQLITE_OK );.  i
38298 66 28 20 70 48 61 73 68 20 29 7b 0a 20 20 20 20  f( pHash ){.    
38299 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43  sqlite3Fts3HashC
3829a 6c 65 61 72 28 70 48 61 73 68 29 3b 0a 20 20 20  lear(pHash);.   
3829b 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48   sqlite3_free(pH
3829c 61 73 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ash);.  }.  retu
3829d 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53  rn rc;.}..#if !S
3829e 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54  QLITE_CORE.SQLIT
3829f 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
382a0 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
382a1 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
382a2 20 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72   .  char **pzErr
382a3 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c  Msg,.  const sql
382a4 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
382a5 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c  s *pApi.){.  SQL
382a6 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
382a7 49 54 32 28 70 41 70 69 29 0a 20 20 72 65 74 75  IT2(pApi).  retu
382a8 72 6e 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e  rn sqlite3Fts3In
382a9 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66  it(db);.}.#endif
382aa 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
382ab 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
382ac 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
382ad 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
382ae 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
382af 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
382b0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
382b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382b3 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
382b4 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
382b5 66 74 73 33 5f 65 78 70 72 2e 63 20 2a 2a 2a 2a  fts3_expr.c ****
382b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382b8 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
382b9 4e 6f 76 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65  Nov 28.**.** The
382ba 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
382bb 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
382bc 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
382bd 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
382be 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
382bf 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
382c0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
382c1 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
382c2 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
382c3 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
382c4 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
382c5 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
382c6 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
382c7 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
382c8 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
382c9 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
382ca 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
382cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
382d0 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 63 6f 6e   This module con
382d1 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20  tains code that 
382d2 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 70 61 72  implements a par
382d3 73 65 72 20 66 6f 72 20 66 74 73 33 20 71 75 65  ser for fts3 que
382d4 72 79 20 73 74 72 69 6e 67 73 0a 2a 2a 20 28 74  ry strings.** (t
382d5 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 61 72  he right-hand ar
382d6 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 4d 41  gument to the MA
382d7 54 43 48 20 6f 70 65 72 61 74 6f 72 29 2e 20 42  TCH operator). B
382d8 65 63 61 75 73 65 20 74 68 65 20 73 75 70 70 6f  ecause the suppo
382d9 72 74 65 64 20 0a 2a 2a 20 73 79 6e 74 61 78 20  rted .** syntax 
382da 69 73 20 72 65 6c 61 74 69 76 65 6c 79 20 73 69  is relatively si
382db 6d 70 6c 65 2c 20 74 68 65 20 77 68 6f 6c 65 20  mple, the whole 
382dc 74 6f 6b 65 6e 69 7a 65 72 2f 70 61 72 73 65 72  tokenizer/parser
382dd 20 73 79 73 74 65 6d 20 69 73 0a 2a 2a 20 68 61   system is.** ha
382de 6e 64 2d 63 6f 64 65 64 2e 20 54 68 65 20 70 75  nd-coded. The pu
382df 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74  blic interface t
382e0 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73  o this module is
382e1 20 64 65 63 6c 61 72 65 64 20 69 6e 20 73 6f 75   declared in sou
382e2 72 63 65 0a 2a 2a 20 63 6f 64 65 20 66 69 6c 65  rce.** code file
382e3 20 22 66 74 73 33 5f 65 78 70 72 2e 68 22 2e 0a   "fts3_expr.h"..
382e4 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
382e5 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
382e6 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
382e7 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 2f 2a 0a  NABLE_FTS3)../*.
382e8 2a 2a 20 42 79 20 64 65 66 61 75 6c 74 2c 20 74  ** By default, t
382e9 68 69 73 20 6d 6f 64 75 6c 65 20 70 61 72 73 65  his module parse
382ea 73 20 74 68 65 20 6c 65 67 61 63 79 20 73 79 6e  s the legacy syn
382eb 74 61 78 20 74 68 61 74 20 68 61 73 20 62 65 65  tax that has bee
382ec 6e 20 0a 2a 2a 20 74 72 61 64 69 74 69 6f 6e 61  n .** traditiona
382ed 6c 6c 79 20 75 73 65 64 20 62 79 20 66 74 73 33  lly used by fts3
382ee 2e 20 4f 72 2c 20 69 66 20 53 51 4c 49 54 45 5f  . Or, if SQLITE_
382ef 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50 41 52 45  ENABLE_FTS3_PARE
382f0 4e 54 48 45 53 49 53 0a 2a 2a 20 69 73 20 64 65  NTHESIS.** is de
382f1 66 69 6e 65 64 2c 20 74 68 65 6e 20 69 74 20 75  fined, then it u
382f2 73 65 73 20 74 68 65 20 6e 65 77 20 73 79 6e 74  ses the new synt
382f3 61 78 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e  ax. The differen
382f4 63 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74  ces between.** t
382f5 68 65 20 6e 65 77 20 61 6e 64 20 74 68 65 20 6f  he new and the o
382f6 6c 64 20 73 79 6e 74 61 78 65 73 20 61 72 65 3a  ld syntaxes are:
382f7 0a 2a 2a 0a 2a 2a 20 20 61 29 20 54 68 65 20 6e  .**.**  a) The n
382f8 65 77 20 73 79 6e 74 61 78 20 73 75 70 70 6f 72  ew syntax suppor
382f9 74 73 20 70 61 72 65 6e 74 68 65 73 69 73 2e 20  ts parenthesis. 
382fa 54 68 65 20 6f 6c 64 20 64 6f 65 73 20 6e 6f 74  The old does not
382fb 2e 0a 2a 2a 0a 2a 2a 20 20 62 29 20 54 68 65 20  ..**.**  b) The 
382fc 6e 65 77 20 73 79 6e 74 61 78 20 73 75 70 70 6f  new syntax suppo
382fd 72 74 73 20 74 68 65 20 41 4e 44 20 61 6e 64 20  rts the AND and 
382fe 4e 4f 54 20 6f 70 65 72 61 74 6f 72 73 2e 20 54  NOT operators. T
382ff 68 65 20 6f 6c 64 20 64 6f 65 73 20 6e 6f 74 2e  he old does not.
38300 0a 2a 2a 0a 2a 2a 20 20 63 29 20 54 68 65 20 6f  .**.**  c) The o
38301 6c 64 20 73 79 6e 74 61 78 20 73 75 70 70 6f 72  ld syntax suppor
38302 74 73 20 74 68 65 20 22 2d 22 20 74 6f 6b 65 6e  ts the "-" token
38303 20 71 75 61 6c 69 66 69 65 72 2e 20 54 68 69 73   qualifier. This
38304 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 20 20 20 20   is not .**     
38305 73 75 70 70 6f 72 74 65 64 20 62 79 20 74 68 65  supported by the
38306 20 6e 65 77 20 73 79 6e 74 61 78 20 28 69 74 20   new syntax (it 
38307 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 74  is replaced by t
38308 68 65 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 29  he NOT operator)
38309 2e 0a 2a 2a 0a 2a 2a 20 20 64 29 20 57 68 65 6e  ..**.**  d) When
3830a 20 75 73 69 6e 67 20 74 68 65 20 6f 6c 64 20 73   using the old s
3830b 79 6e 74 61 78 2c 20 74 68 65 20 4f 52 20 6f 70  yntax, the OR op
3830c 65 72 61 74 6f 72 20 68 61 73 20 61 20 67 72 65  erator has a gre
3830d 61 74 65 72 20 70 72 65 63 65 64 65 6e 63 65 0a  ater precedence.
3830e 2a 2a 20 20 20 20 20 74 68 61 6e 20 61 6e 20 69  **     than an i
3830f 6d 70 6c 69 63 69 74 20 41 4e 44 2e 20 57 68 65  mplicit AND. Whe
38310 6e 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 2c  n using the new,
38311 20 62 6f 74 68 20 69 6d 70 6c 69 63 69 74 79 20   both implicity 
38312 61 6e 64 20 65 78 70 6c 69 63 69 74 0a 2a 2a 20  and explicit.** 
38313 20 20 20 20 41 4e 44 20 6f 70 65 72 61 74 6f 72      AND operator
38314 73 20 68 61 76 65 20 61 20 68 69 67 68 65 72 20  s have a higher 
38315 70 72 65 63 65 64 65 6e 63 65 20 74 68 61 6e 20  precedence than 
38316 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 6f 6d  OR..**.** If com
38317 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54  piled with SQLIT
38318 45 5f 54 45 53 54 20 64 65 66 69 6e 65 64 2c 20  E_TEST defined, 
38319 74 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65  then this module
3831a 20 65 78 70 6f 72 74 73 20 74 68 65 0a 2a 2a 20   exports the.** 
3831b 73 79 6d 62 6f 6c 20 22 69 6e 74 20 73 71 6c 69  symbol "int sqli
3831c 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
3831d 70 61 72 65 6e 74 68 65 73 65 73 22 2e 20 53 65  parentheses". Se
3831e 74 74 69 6e 67 20 74 68 69 73 20 76 61 72 69 61  tting this varia
3831f 62 6c 65 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 63  ble.** to zero c
38320 61 75 73 65 73 20 74 68 65 20 6d 6f 64 75 6c 65  auses the module
38321 20 74 6f 20 75 73 65 20 74 68 65 20 6f 6c 64 20   to use the old 
38322 73 79 6e 74 61 78 2e 20 49 66 20 69 74 20 69 73  syntax. If it is
38323 20 73 65 74 20 74 6f 20 0a 2a 2a 20 6e 6f 6e 2d   set to .** non-
38324 7a 65 72 6f 20 74 68 65 20 6e 65 77 20 73 79 6e  zero the new syn
38325 74 61 78 20 69 73 20 61 63 74 69 76 61 74 65 64  tax is activated
38326 2e 20 54 68 69 73 20 69 73 20 73 6f 20 62 6f 74  . This is so bot
38327 68 20 73 79 6e 74 61 78 65 73 20 63 61 6e 0a 2a  h syntaxes can.*
38328 2a 20 62 65 20 74 65 73 74 65 64 20 75 73 69 6e  * be tested usin
38329 67 20 61 20 73 69 6e 67 6c 65 20 62 75 69 6c 64  g a single build
3832a 20 6f 66 20 74 65 73 74 66 69 78 74 75 72 65 2e   of testfixture.
3832b 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
3832c 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
3832d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 74  I int sqlite3_ft
3832e 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74  s3_enable_parent
3832f 68 65 73 65 73 20 3d 20 30 3b 0a 23 65 6c 73 65  heses = 0;.#else
38330 0a 23 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f  .# ifdef SQLITE_
38331 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50 41 52 45  ENABLE_FTS3_PARE
38332 4e 54 48 45 53 49 53 20 0a 23 20 20 64 65 66 69  NTHESIS .#  defi
38333 6e 65 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f  ne sqlite3_fts3_
38334 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73  enable_parenthes
38335 65 73 20 31 0a 23 20 65 6c 73 65 0a 23 20 20 64  es 1.# else.#  d
38336 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 74  efine sqlite3_ft
38337 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74  s3_enable_parent
38338 68 65 73 65 73 20 30 0a 23 20 65 6e 64 69 66 0a  heses 0.# endif.
38339 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65  #endif../*.** De
3833a 66 61 75 6c 74 20 73 70 61 6e 20 66 6f 72 20 4e  fault span for N
3833b 45 41 52 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a  EAR operators..*
3833c 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3833d 5f 46 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e 45  _FTS3_DEFAULT_NE
3833e 41 52 5f 50 41 52 41 4d 20 31 30 0a 0a 0a 74 79  AR_PARAM 10...ty
3833f 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 72  pedef struct Par
38340 73 65 43 6f 6e 74 65 78 74 20 50 61 72 73 65 43  seContext ParseC
38341 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 50  ontext;.struct P
38342 61 72 73 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20  arseContext {.  
38343 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
38344 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20  r *pTokenizer;  
38345 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
38346 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e   module */.  con
38347 73 74 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 3b  st char **azCol;
38348 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38349 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c   /* Array of col
3834a 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 66 74  umn names for ft
3834b 73 33 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  s3 table */.  in
3834c 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  t nCol;         
3834d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3834e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
3834f 6e 74 72 69 65 73 20 69 6e 20 61 7a 43 6f 6c 5b  ntries in azCol[
38350 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 44 65 66 61  ] */.  int iDefa
38351 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  ultCol;         
38352 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
38353 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20 74 6f 20  fault column to 
38354 71 75 65 72 79 20 2a 2f 0a 20 20 73 71 6c 69 74  query */.  sqlit
38355 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
38356 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
38357 2a 20 57 72 69 74 65 20 65 72 72 6f 72 20 6d 65  * Write error me
38358 73 73 61 67 65 20 68 65 72 65 20 2a 2f 0a 20 20  ssage here */.  
38359 69 6e 74 20 6e 4e 65 73 74 3b 20 20 20 20 20 20  int nNest;      
3835a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3835b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3835c 20 6e 65 73 74 65 64 20 62 72 61 63 6b 65 74 73   nested brackets
3835d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
3835e 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65  is function is e
3835f 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65  quivalent to the
38360 20 73 74 61 6e 64 61 72 64 20 69 73 73 70 61 63   standard isspac
38361 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a  e() function. .*
38362 2a 0a 2a 2a 20 54 68 65 20 73 74 61 6e 64 61 72  *.** The standar
38363 64 20 69 73 73 70 61 63 65 28 29 20 63 61 6e 20  d isspace() can 
38364 62 65 20 61 77 6b 77 61 72 64 20 74 6f 20 75 73  be awkward to us
38365 65 20 73 61 66 65 6c 79 2c 20 62 65 63 61 75 73  e safely, becaus
38366 65 20 61 6c 74 68 6f 75 67 68 20 69 74 0a 2a 2a  e although it.**
38367 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 61   is defined to a
38368 63 63 65 70 74 20 61 6e 20 61 72 67 75 6d 65 6e  ccept an argumen
38369 74 20 6f 66 20 74 79 70 65 20 69 6e 74 2c 20 69  t of type int, i
3836a 74 73 20 62 65 68 61 76 69 6f 75 72 20 77 68 65  ts behaviour whe
3836b 6e 20 70 61 73 73 65 64 0a 2a 2a 20 61 6e 20 69  n passed.** an i
3836c 6e 74 65 67 65 72 20 74 68 61 74 20 66 61 6c 6c  nteger that fall
3836d 73 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65  s outside of the
3836e 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20 75 6e   range of the un
3836f 73 69 67 6e 65 64 20 63 68 61 72 20 74 79 70 65  signed char type
38370 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64  .** is undefined
38371 20 28 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73 2c   (and sometimes,
38372 20 22 75 6e 64 65 66 69 6e 65 64 22 20 6d 65 61   "undefined" mea
38373 6e 73 20 73 65 67 66 61 75 6c 74 29 2e 20 54 68  ns segfault). Th
38374 69 73 20 77 72 61 70 70 65 72 0a 2a 2a 20 69 73  is wrapper.** is
38375 20 64 65 66 69 6e 65 64 20 74 6f 20 61 63 63 65   defined to acce
38376 70 74 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6f  pt an argument o
38377 66 20 74 79 70 65 20 63 68 61 72 2c 20 61 6e 64  f type char, and
38378 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
38379 30 20 66 6f 72 0a 2a 2a 20 61 6e 79 20 76 61 6c  0 for.** any val
3837a 75 65 73 20 74 68 61 74 20 66 61 6c 6c 20 6f 75  ues that fall ou
3837b 74 73 69 64 65 20 6f 66 20 74 68 65 20 72 61 6e  tside of the ran
3837c 67 65 20 6f 66 20 74 68 65 20 75 6e 73 69 67 6e  ge of the unsign
3837d 65 64 20 63 68 61 72 20 74 79 70 65 20 28 69 2e  ed char type (i.
3837e 65 2e 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 76  e..** negative v
3837f 61 6c 75 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69  alues)..*/.stati
38380 63 20 69 6e 74 20 66 74 73 33 69 73 73 70 61 63  c int fts3isspac
38381 65 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74  e(char c){.  ret
38382 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20  urn (c&0x80)==0 
38383 3f 20 69 73 73 70 61 63 65 28 63 29 20 3a 20 30  ? isspace(c) : 0
38384 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61  ;.}../*.** Extra
38385 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65  ct the next toke
38386 6e 20 66 72 6f 6d 20 62 75 66 66 65 72 20 7a 20  n from buffer z 
38387 28 6c 65 6e 67 74 68 20 6e 29 20 75 73 69 6e 67  (length n) using
38388 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a   the tokenizer.*
38389 2a 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 66 6f  * and other info
3838a 72 6d 61 74 69 6f 6e 20 28 63 6f 6c 75 6d 6e 20  rmation (column 
3838b 6e 61 6d 65 73 20 65 74 63 2e 29 20 69 6e 20 70  names etc.) in p
3838c 50 61 72 73 65 2e 20 43 72 65 61 74 65 20 61 6e  Parse. Create an
3838d 20 46 74 73 33 45 78 70 72 0a 2a 2a 20 73 74 72   Fts3Expr.** str
3838e 75 63 74 75 72 65 20 6f 66 20 74 79 70 65 20 46  ucture of type F
3838f 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 63  TSQUERY_PHRASE c
38390 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 68 72 61  ontaining a phra
38391 73 65 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f 66  se consisting of
38392 20 74 68 69 73 0a 2a 2a 20 73 69 6e 67 6c 65 20   this.** single 
38393 74 6f 6b 65 6e 20 61 6e 64 20 73 65 74 20 2a 70  token and set *p
38394 70 45 78 70 72 20 74 6f 20 70 6f 69 6e 74 20 74  pExpr to point t
38395 6f 20 69 74 2e 20 49 66 20 74 68 65 20 65 6e 64  o it. If the end
38396 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 69   of the buffer i
38397 73 0a 2a 2a 20 72 65 61 63 68 65 64 20 62 65 66  s.** reached bef
38398 6f 72 65 20 61 20 74 6f 6b 65 6e 20 69 73 20 66  ore a token is f
38399 6f 75 6e 64 2c 20 73 65 74 20 2a 70 70 45 78 70  ound, set *ppExp
3839a 72 20 74 6f 20 7a 65 72 6f 2e 20 49 74 20 69 73  r to zero. It is
3839b 20 74 68 65 0a 2a 2a 20 72 65 73 70 6f 6e 73 69   the.** responsi
3839c 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61  bility of the ca
3839d 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c  ller to eventual
3839e 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65 20 74 68  ly deallocate th
3839f 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a 20  e allocated .** 
383a0 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75  Fts3Expr structu
383a1 72 65 20 28 69 66 20 61 6e 79 29 20 62 79 20 70  re (if any) by p
383a2 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c  assing it to sql
383a3 69 74 65 33 5f 66 72 65 65 28 29 2e 0a 2a 2a 0a  ite3_free()..**.
383a4 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
383a5 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75  _OK if successfu
383a6 6c 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  l, or SQLITE_NOM
383a7 45 4d 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61  EM if a memory a
383a8 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61 69  llocation.** fai
383a9 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ls..*/.static in
383aa 74 20 67 65 74 4e 65 78 74 54 6f 6b 65 6e 28 0a  t getNextToken(.
383ab 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a    ParseContext *
383ac 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
383ad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73            /* fts
383ae 33 20 71 75 65 72 79 20 70 61 72 73 65 20 63 6f  3 query parse co
383af 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
383b0 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  Col,            
383b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
383b2 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20     /* Value for 
383b3 46 74 73 33 50 68 72 61 73 65 2e 69 43 6f 6c 75  Fts3Phrase.iColu
383b4 6d 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  mn */.  const ch
383b5 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20  ar *z, int n,   
383b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
383b7 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20  /* Input string 
383b8 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a  */.  Fts3Expr **
383b9 70 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  ppExpr,         
383ba 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
383bb 4f 55 54 3a 20 65 78 70 72 65 73 73 69 6f 6e 20  OUT: expression 
383bc 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73  */.  int *pnCons
383bd 75 6d 65 64 20 20 20 20 20 20 20 20 20 20 20 20  umed            
383be 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
383bf 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62  OUT: Number of b
383c0 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f  ytes consumed */
383c1 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  .){.  sqlite3_to
383c2 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
383c3 7a 65 72 20 3d 20 70 50 61 72 73 65 2d 3e 70 54  zer = pParse->pT
383c4 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69  okenizer;.  sqli
383c5 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
383c6 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64  dule const *pMod
383c7 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72  ule = pTokenizer
383c8 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74  ->pModule;.  int
383c9 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74   rc;.  sqlite3_t
383ca 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
383cb 2a 70 43 75 72 73 6f 72 3b 0a 20 20 46 74 73 33  *pCursor;.  Fts3
383cc 45 78 70 72 20 2a 70 52 65 74 20 3d 20 30 3b 0a  Expr *pRet = 0;.
383cd 20 20 69 6e 74 20 6e 43 6f 6e 73 75 6d 65 64 20    int nConsumed 
383ce 3d 20 30 3b 0a 0a 20 20 72 63 20 3d 20 70 4d 6f  = 0;..  rc = pMo
383cf 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b  dule->xOpen(pTok
383d0 65 6e 69 7a 65 72 2c 20 7a 2c 20 6e 2c 20 26 70  enizer, z, n, &p
383d1 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 72  Cursor);.  if( r
383d2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
383d3 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
383d4 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 69 6e 74 20  zToken;.    int 
383d5 6e 54 6f 6b 65 6e 2c 20 69 53 74 61 72 74 2c 20  nToken, iStart, 
383d6 69 45 6e 64 2c 20 69 50 6f 73 69 74 69 6f 6e 3b  iEnd, iPosition;
383d7 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 20  .    int nByte; 
383d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
383d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
383da 20 74 6f 74 61 6c 20 73 70 61 63 65 20 74 6f 20   total space to 
383db 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 0a 20 20 20  allocate */..   
383dc 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e   pCursor->pToken
383dd 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65  izer = pTokenize
383de 72 3b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64  r;.    rc = pMod
383df 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72 73  ule->xNext(pCurs
383e0 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54  or, &zToken, &nT
383e1 6f 6b 65 6e 2c 20 26 69 53 74 61 72 74 2c 20 26  oken, &iStart, &
383e2 69 45 6e 64 2c 20 26 69 50 6f 73 69 74 69 6f 6e  iEnd, &iPosition
383e3 29 3b 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  );..    if( rc==
383e4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
383e5 20 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f     nByte = sizeo
383e6 66 28 46 74 73 33 45 78 70 72 29 20 2b 20 73 69  f(Fts3Expr) + si
383e7 7a 65 6f 66 28 46 74 73 33 50 68 72 61 73 65 29  zeof(Fts3Phrase)
383e8 20 2b 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20   + nToken;.     
383e9 20 70 52 65 74 20 3d 20 28 46 74 73 33 45 78 70   pRet = (Fts3Exp
383ea 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
383eb 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  oc(nByte);.     
383ec 20 69 66 28 20 21 70 52 65 74 20 29 7b 0a 20 20   if( !pRet ){.  
383ed 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
383ee 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
383ef 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 65  else{.        me
383f0 6d 73 65 74 28 70 52 65 74 2c 20 30 2c 20 6e 42  mset(pRet, 0, nB
383f1 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 70 52  yte);.        pR
383f2 65 74 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51  et->eType = FTSQ
383f3 55 45 52 59 5f 50 48 52 41 53 45 3b 0a 20 20 20  UERY_PHRASE;.   
383f4 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61       pRet->pPhra
383f5 73 65 20 3d 20 28 46 74 73 33 50 68 72 61 73 65  se = (Fts3Phrase
383f6 20 2a 29 26 70 52 65 74 5b 31 5d 3b 0a 20 20 20   *)&pRet[1];.   
383f7 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61       pRet->pPhra
383f8 73 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20 31 3b 0a  se->nToken = 1;.
383f9 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50          pRet->pP
383fa 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  hrase->iColumn =
383fb 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70   iCol;.        p
383fc 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54  Ret->pPhrase->aT
383fd 6f 6b 65 6e 5b 30 5d 2e 6e 20 3d 20 6e 54 6f 6b  oken[0].n = nTok
383fe 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74  en;.        pRet
383ff 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  ->pPhrase->aToke
38400 6e 5b 30 5d 2e 7a 20 3d 20 28 63 68 61 72 20 2a  n[0].z = (char *
38401 29 26 70 52 65 74 2d 3e 70 50 68 72 61 73 65 5b  )&pRet->pPhrase[
38402 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  1];.        memc
38403 70 79 28 70 52 65 74 2d 3e 70 50 68 72 61 73 65  py(pRet->pPhrase
38404 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 7a 2c 20 7a  ->aToken[0].z, z
38405 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a  Token, nToken);.
38406 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 45 6e  .        if( iEn
38407 64 3c 6e 20 26 26 20 7a 5b 69 45 6e 64 5d 3d 3d  d<n && z[iEnd]==
38408 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  '*' ){.         
38409 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e   pRet->pPhrase->
3840a 61 54 6f 6b 65 6e 5b 30 5d 2e 69 73 50 72 65 66  aToken[0].isPref
3840b 69 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ix = 1;.        
3840c 20 20 69 45 6e 64 2b 2b 3b 0a 20 20 20 20 20 20    iEnd++;.      
3840d 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
3840e 21 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e  !sqlite3_fts3_en
3840f 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
38410 20 26 26 20 69 53 74 61 72 74 3e 30 20 26 26 20   && iStart>0 && 
38411 7a 5b 69 53 74 61 72 74 2d 31 5d 3d 3d 27 2d 27  z[iStart-1]=='-'
38412 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52   ){.          pR
38413 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 69 73 4e  et->pPhrase->isN
38414 6f 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ot = 1;.        
38415 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
38416 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 69 45 6e 64  nConsumed = iEnd
38417 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 6f  ;.    }..    pMo
38418 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75  dule->xClose(pCu
38419 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 0a 20 20  rsor);.  }.  .  
3841a 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 6e 43  *pnConsumed = nC
3841b 6f 6e 73 75 6d 65 64 3b 0a 20 20 2a 70 70 45 78  onsumed;.  *ppEx
3841c 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 72 65 74  pr = pRet;.  ret
3841d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
3841e 2a 20 45 6e 6c 61 72 67 65 20 61 20 6d 65 6d 6f  * Enlarge a memo
3841f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20  ry allocation.  
38420 49 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d  If an out-of-mem
38421 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  ory allocation o
38422 63 63 75 72 73 2c 0a 2a 2a 20 74 68 65 6e 20 66  ccurs,.** then f
38423 72 65 65 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f  ree the old allo
38424 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20  cation..*/.void 
38425 2a 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72  *fts3ReallocOrFr
38426 65 65 28 76 6f 69 64 20 2a 70 4f 72 69 67 2c 20  ee(void *pOrig, 
38427 69 6e 74 20 6e 4e 65 77 29 7b 0a 20 20 76 6f 69  int nNew){.  voi
38428 64 20 2a 70 52 65 74 20 3d 20 73 71 6c 69 74 65  d *pRet = sqlite
38429 33 5f 72 65 61 6c 6c 6f 63 28 70 4f 72 69 67 2c  3_realloc(pOrig,
3842a 20 6e 4e 65 77 29 3b 0a 20 20 69 66 28 20 21 70   nNew);.  if( !p
3842b 52 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Ret ){.    sqlit
3842c 65 33 5f 66 72 65 65 28 70 4f 72 69 67 29 3b 0a  e3_free(pOrig);.
3842d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 52 65    }.  return pRe
3842e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 66 66  t;.}../*.** Buff
3842f 65 72 20 7a 49 6e 70 75 74 2c 20 6c 65 6e 67 74  er zInput, lengt
38430 68 20 6e 49 6e 70 75 74 2c 20 63 6f 6e 74 61 69  h nInput, contai
38431 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ns the contents 
38432 6f 66 20 61 20 71 75 6f 74 65 64 20 73 74 72 69  of a quoted stri
38433 6e 67 0a 2a 2a 20 74 68 61 74 20 61 70 70 65 61  ng.** that appea
38434 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  red as part of a
38435 6e 20 66 74 73 33 20 71 75 65 72 79 20 65 78 70  n fts3 query exp
38436 72 65 73 73 69 6f 6e 2e 20 4e 65 69 74 68 65 72  ression. Neither
38437 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72   quote character
38438 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20  .** is included 
38439 69 6e 20 74 68 65 20 62 75 66 66 65 72 2e 20 54  in the buffer. T
3843a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74  his function att
3843b 65 6d 70 74 73 20 74 6f 20 74 6f 6b 65 6e 69 7a  empts to tokeniz
3843c 65 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20  e the entire.** 
3843d 69 6e 70 75 74 20 62 75 66 66 65 72 20 61 6e 64  input buffer and
3843e 20 63 72 65 61 74 65 20 61 6e 20 46 74 73 33 45   create an Fts3E
3843f 78 70 72 20 73 74 72 75 63 74 75 72 65 20 6f 66  xpr structure of
38440 20 74 79 70 65 20 46 54 53 51 55 45 52 59 5f 50   type FTSQUERY_P
38441 48 52 41 53 45 20 0a 2a 2a 20 63 6f 6e 74 61 69  HRASE .** contai
38442 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 73  ning the results
38443 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65  ..**.** If succe
38444 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b  ssful, SQLITE_OK
38445 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
38446 20 2a 70 70 45 78 70 72 20 73 65 74 20 74 6f 20   *ppExpr set to 
38447 70 6f 69 6e 74 20 61 74 20 74 68 65 0a 2a 2a 20  point at the.** 
38448 61 6c 6c 6f 63 61 74 65 64 20 46 74 73 33 45 78  allocated Fts3Ex
38449 70 72 20 73 74 72 75 63 74 75 72 65 2e 20 4f 74  pr structure. Ot
3844a 68 65 72 77 69 73 65 2c 20 65 69 74 68 65 72 20  herwise, either 
3844b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 28 6f 75  SQLITE_NOMEM (ou
3844c 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 65  t of memory.** e
3844d 72 72 6f 72 29 20 6f 72 20 53 51 4c 49 54 45 5f  rror) or SQLITE_
3844e 45 52 52 4f 52 20 28 74 6f 6b 65 6e 69 7a 61 74  ERROR (tokenizat
3844f 69 6f 6e 20 65 72 72 6f 72 29 20 69 73 20 72 65  ion error) is re
38450 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45 78  turned and *ppEx
38451 70 72 20 73 65 74 0a 2a 2a 20 74 6f 20 30 2e 0a  pr set.** to 0..
38452 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
38453 74 4e 65 78 74 53 74 72 69 6e 67 28 0a 20 20 50  tNextString(.  P
38454 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61  arseContext *pPa
38455 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
38456 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71         /* fts3 q
38457 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65  uery parse conte
38458 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  xt */.  const ch
38459 61 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e 74 20  ar *zInput, int 
3845a 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 20  nInput,         
3845b 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20  /* Input string 
3845c 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a  */.  Fts3Expr **
3845d 70 70 45 78 70 72 20 20 20 20 20 20 20 20 20 20  ppExpr          
3845e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3845f 4f 55 54 3a 20 65 78 70 72 65 73 73 69 6f 6e 20  OUT: expression 
38460 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  */.){.  sqlite3_
38461 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
38462 6e 69 7a 65 72 20 3d 20 70 50 61 72 73 65 2d 3e  nizer = pParse->
38463 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71  pTokenizer;.  sq
38464 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
38465 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d  module const *pM
38466 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a  odule = pTokeniz
38467 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69  er->pModule;.  i
38468 6e 74 20 72 63 3b 0a 20 20 46 74 73 33 45 78 70  nt rc;.  Fts3Exp
38469 72 20 2a 70 20 3d 20 30 3b 0a 20 20 73 71 6c 69  r *p = 0;.  sqli
3846a 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
3846b 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 20 3d 20  rsor *pCursor = 
3846c 30 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65 6d 70  0;.  char *zTemp
3846d 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 54 65 6d   = 0;.  int nTem
3846e 70 20 3d 20 30 3b 0a 0a 20 20 72 63 20 3d 20 70  p = 0;..  rc = p
3846f 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54  Module->xOpen(pT
38470 6f 6b 65 6e 69 7a 65 72 2c 20 7a 49 6e 70 75 74  okenizer, zInput
38471 2c 20 6e 49 6e 70 75 74 2c 20 26 70 43 75 72 73  , nInput, &pCurs
38472 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  or);.  if( rc==S
38473 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
38474 69 6e 74 20 69 69 3b 0a 20 20 20 20 70 43 75 72  int ii;.    pCur
38475 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20  sor->pTokenizer 
38476 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20  = pTokenizer;.  
38477 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d    for(ii=0; rc==
38478 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29  SQLITE_OK; ii++)
38479 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
3847a 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  ar *zToken;.    
3847b 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 69 42    int nToken, iB
3847c 65 67 69 6e 2c 20 69 45 6e 64 2c 20 69 50 6f 73  egin, iEnd, iPos
3847d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 4d 6f  ;.      rc = pMo
3847e 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72  dule->xNext(pCur
3847f 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e  sor, &zToken, &n
38480 54 6f 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c 20  Token, &iBegin, 
38481 26 69 45 6e 64 2c 20 26 69 50 6f 73 29 3b 0a 20  &iEnd, &iPos);. 
38482 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
38483 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
38484 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69    int nByte = si
38485 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 20 2b  zeof(Fts3Expr) +
38486 20 73 69 7a 65 6f 66 28 46 74 73 33 50 68 72 61   sizeof(Fts3Phra
38487 73 65 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d  se);.        p =
38488 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72   fts3ReallocOrFr
38489 65 65 28 70 2c 20 6e 42 79 74 65 2b 69 69 2a 73  ee(p, nByte+ii*s
3848a 69 7a 65 6f 66 28 73 74 72 75 63 74 20 50 68 72  izeof(struct Phr
3848b 61 73 65 54 6f 6b 65 6e 29 29 3b 0a 20 20 20 20  aseToken));.    
3848c 20 20 20 20 7a 54 65 6d 70 20 3d 20 66 74 73 33      zTemp = fts3
3848d 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 7a 54  ReallocOrFree(zT
3848e 65 6d 70 2c 20 6e 54 65 6d 70 20 2b 20 6e 54 6f  emp, nTemp + nTo
3848f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ken);.        if
38490 28 20 21 70 20 7c 7c 20 21 7a 54 65 6d 70 20 29  ( !p || !zTemp )
38491 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  {.          goto
38492 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 20   no_mem;.       
38493 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69   }.        if( i
38494 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  i==0 ){.        
38495 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e    memset(p, 0, n
38496 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Byte);.         
38497 20 70 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46   p->pPhrase = (F
38498 74 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31  ts3Phrase *)&p[1
38499 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ];.        }.   
3849a 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 20       p->pPhrase 
3849b 3d 20 28 46 74 73 33 50 68 72 61 73 65 20 2a 29  = (Fts3Phrase *)
3849c 26 70 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 70  &p[1];.        p
3849d 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65  ->pPhrase->nToke
3849e 6e 20 3d 20 69 69 2b 31 3b 0a 20 20 20 20 20 20  n = ii+1;.      
3849f 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54    p->pPhrase->aT
384a0 6f 6b 65 6e 5b 69 69 5d 2e 6e 20 3d 20 6e 54 6f  oken[ii].n = nTo
384a1 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  ken;.        mem
384a2 63 70 79 28 26 7a 54 65 6d 70 5b 6e 54 65 6d 70  cpy(&zTemp[nTemp
384a3 5d 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65  ], zToken, nToke
384a4 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 54 65 6d  n);.        nTem
384a5 70 20 2b 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20  p += nToken;.   
384a6 20 20 20 20 20 69 66 28 20 69 45 6e 64 3c 6e 49       if( iEnd<nI
384a7 6e 70 75 74 20 26 26 20 7a 49 6e 70 75 74 5b 69  nput && zInput[i
384a8 45 6e 64 5d 3d 3d 27 2a 27 20 29 7b 0a 20 20 20  End]=='*' ){.   
384a9 20 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73         p->pPhras
384aa 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d 2e 69 73  e->aToken[ii].is
384ab 50 72 65 66 69 78 20 3d 20 31 3b 0a 20 20 20 20  Prefix = 1;.    
384ac 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
384ad 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d       p->pPhrase-
384ae 3e 61 54 6f 6b 65 6e 5b 69 69 5d 2e 69 73 50 72  >aToken[ii].isPr
384af 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20  efix = 0;.      
384b0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
384b1 7d 0a 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e  }..    pModule->
384b2 78 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29 3b  xClose(pCursor);
384b3 0a 20 20 20 20 70 43 75 72 73 6f 72 20 3d 20 30  .    pCursor = 0
384b4 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  ;.  }..  if( rc=
384b5 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
384b6 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20      int jj;.    
384b7 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20  char *zNew;.    
384b8 69 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 0a 20 20  int nNew = 0;.  
384b9 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69    int nByte = si
384ba 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 20 2b  zeof(Fts3Expr) +
384bb 20 73 69 7a 65 6f 66 28 46 74 73 33 50 68 72 61   sizeof(Fts3Phra
384bc 73 65 29 3b 0a 20 20 20 20 6e 42 79 74 65 20 2b  se);.    nByte +
384bd 3d 20 28 70 3f 28 70 2d 3e 70 50 68 72 61 73 65  = (p?(p->pPhrase
384be 2d 3e 6e 54 6f 6b 65 6e 2d 31 29 3a 30 29 20 2a  ->nToken-1):0) *
384bf 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 50   sizeof(struct P
384c0 68 72 61 73 65 54 6f 6b 65 6e 29 3b 0a 20 20 20  hraseToken);.   
384c1 20 70 20 3d 20 66 74 73 33 52 65 61 6c 6c 6f 63   p = fts3Realloc
384c2 4f 72 46 72 65 65 28 70 2c 20 6e 42 79 74 65 20  OrFree(p, nByte 
384c3 2b 20 6e 54 65 6d 70 29 3b 0a 20 20 20 20 69 66  + nTemp);.    if
384c4 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 67 6f  ( !p ){.      go
384c5 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d  to no_mem;.    }
384c6 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 20 29  .    if( zTemp )
384c7 7b 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d 20 26  {.      zNew = &
384c8 28 28 28 63 68 61 72 20 2a 29 70 29 5b 6e 42 79  (((char *)p)[nBy
384c9 74 65 5d 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63  te]);.      memc
384ca 70 79 28 7a 4e 65 77 2c 20 7a 54 65 6d 70 2c 20  py(zNew, zTemp, 
384cb 6e 54 65 6d 70 29 3b 0a 20 20 20 20 7d 65 6c 73  nTemp);.    }els
384cc 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28  e{.      memset(
384cd 70 2c 20 30 2c 20 6e 42 79 74 65 2b 6e 54 65 6d  p, 0, nByte+nTem
384ce 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  p);.    }.    p-
384cf 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74 73 33  >pPhrase = (Fts3
384d0 50 68 72 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a  Phrase *)&p[1];.
384d1 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a      for(jj=0; jj
384d2 3c 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  <p->pPhrase->nTo
384d3 6b 65 6e 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  ken; jj++){.    
384d4 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54    p->pPhrase->aT
384d5 6f 6b 65 6e 5b 6a 6a 5d 2e 7a 20 3d 20 26 7a 4e  oken[jj].z = &zN
384d6 65 77 5b 6e 4e 65 77 5d 3b 0a 20 20 20 20 20 20  ew[nNew];.      
384d7 6e 4e 65 77 20 2b 3d 20 70 2d 3e 70 50 68 72 61  nNew += p->pPhra
384d8 73 65 2d 3e 61 54 6f 6b 65 6e 5b 6a 6a 5d 2e 6e  se->aToken[jj].n
384d9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
384da 74 65 33 5f 66 72 65 65 28 7a 54 65 6d 70 29 3b  te3_free(zTemp);
384db 0a 20 20 20 20 70 2d 3e 65 54 79 70 65 20 3d 20  .    p->eType = 
384dc 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 3b  FTSQUERY_PHRASE;
384dd 0a 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d  .    p->pPhrase-
384de 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 50 61 72 73  >iColumn = pPars
384df 65 2d 3e 69 44 65 66 61 75 6c 74 43 6f 6c 3b 0a  e->iDefaultCol;.
384e0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
384e1 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 45 78  OK;.  }..  *ppEx
384e2 70 72 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e  pr = p;.  return
384e3 20 72 63 3b 0a 6e 6f 5f 6d 65 6d 3a 0a 0a 20 20   rc;.no_mem:..  
384e4 69 66 28 20 70 43 75 72 73 6f 72 20 29 7b 0a 20  if( pCursor ){. 
384e5 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f     pModule->xClo
384e6 73 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  se(pCursor);.  }
384e7 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
384e8 7a 54 65 6d 70 29 3b 0a 20 20 73 71 6c 69 74 65  zTemp);.  sqlite
384e9 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 2a 70 70  3_free(p);.  *pp
384ea 45 78 70 72 20 3d 20 30 3b 0a 20 20 72 65 74 75  Expr = 0;.  retu
384eb 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
384ec 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  .}../*.** Functi
384ed 6f 6e 20 67 65 74 4e 65 78 74 4e 6f 64 65 28 29  on getNextNode()
384ee 2c 20 77 68 69 63 68 20 69 73 20 63 61 6c 6c 65  , which is calle
384ef 64 20 62 79 20 66 74 73 33 45 78 70 72 50 61 72  d by fts3ExprPar
384f0 73 65 28 29 2c 20 6d 61 79 20 69 74 73 65 6c 66  se(), may itself
384f1 0a 2a 2a 20 63 61 6c 6c 20 66 74 73 33 45 78 70  .** call fts3Exp
384f2 72 50 61 72 73 65 28 29 2e 20 53 6f 20 74 68 69  rParse(). So thi
384f3 73 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72  s forward declar
384f4 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65  ation is require
384f5 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
384f6 20 66 74 73 33 45 78 70 72 50 61 72 73 65 28 50   fts3ExprParse(P
384f7 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 2c 20 63  arseContext *, c
384f8 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
384f9 2c 20 46 74 73 33 45 78 70 72 20 2a 2a 2c 20 69  , Fts3Expr **, i
384fa 6e 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  nt *);../*.** Th
384fb 65 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  e output variabl
384fc 65 20 2a 70 70 45 78 70 72 20 69 73 20 70 6f 70  e *ppExpr is pop
384fd 75 6c 61 74 65 64 20 77 69 74 68 20 61 6e 20 61  ulated with an a
384fe 6c 6c 6f 63 61 74 65 64 20 46 74 73 33 45 78 70  llocated Fts3Exp
384ff 72 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c  r .** structure,
38500 20 6f 72 20 73 65 74 20 74 6f 20 30 20 69 66 20   or set to 0 if 
38501 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
38502 6e 70 75 74 20 62 75 66 66 65 72 20 69 73 20 72  nput buffer is r
38503 65 61 63 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  eached..**.** Re
38504 74 75 72 6e 73 20 61 6e 20 53 51 4c 69 74 65 20  turns an SQLite 
38505 65 72 72 6f 72 20 63 6f 64 65 2e 20 53 51 4c 49  error code. SQLI
38506 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68  TE_OK if everyth
38507 69 6e 67 20 77 6f 72 6b 73 2c 20 53 51 4c 49 54  ing works, SQLIT
38508 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 66 20 61 20  E_NOMEM.** if a 
38509 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f  malloc failure o
3850a 63 63 75 72 73 2c 20 6f 72 20 53 51 4c 49 54 45  ccurs, or SQLITE
3850b 5f 45 52 52 4f 52 20 69 66 20 61 20 70 61 72 73  _ERROR if a pars
3850c 65 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  e error is encou
3850d 6e 74 65 72 65 64 2e 0a 2a 2a 20 49 66 20 53 51  ntered..** If SQ
3850e 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65  LITE_ERROR is re
3850f 74 75 72 6e 65 64 2c 20 70 43 6f 6e 74 65 78 74  turned, pContext
38510 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69   is populated wi
38511 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  th an error mess
38512 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  age..*/.static i
38513 6e 74 20 67 65 74 4e 65 78 74 4e 6f 64 65 28 0a  nt getNextNode(.
38514 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a    ParseContext *
38515 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
38516 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73            /* fts
38517 33 20 71 75 65 72 79 20 70 61 72 73 65 20 63 6f  3 query parse co
38518 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ntext */.  const
38519 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
3851a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3851b 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69     /* Input stri
3851c 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72  ng */.  Fts3Expr
3851d 20 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20   **ppExpr,      
3851e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3851f 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65 73 73 69  /* OUT: expressi
38520 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43  on */.  int *pnC
38521 6f 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20 20  onsumed         
38522 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38523 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f  /* OUT: Number o
38524 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64  f bytes consumed
38525 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20   */.){.  static 
38526 63 6f 6e 73 74 20 73 74 72 75 63 74 20 46 74 73  const struct Fts
38527 33 4b 65 79 77 6f 72 64 20 7b 0a 20 20 20 20 63  3Keyword {.    c
38528 68 61 72 20 7a 5b 34 5d 3b 20 20 20 20 20 20 20  har z[4];       
38529 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3852a 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64 20       /* Keyword 
3852b 74 65 78 74 20 2a 2f 0a 20 20 20 20 75 6e 73 69  text */.    unsi
3852c 67 6e 65 64 20 63 68 61 72 20 6e 3b 20 20 20 20  gned char n;    
3852d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3852e 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74    /* Length of t
3852f 68 65 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 20 20  he keyword */.  
38530 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
38531 70 61 72 65 6e 4f 6e 6c 79 3b 20 20 20 20 20 20  parenOnly;      
38532 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20          /* Only 
38533 76 61 6c 69 64 20 69 6e 20 70 61 72 65 6e 20 6d  valid in paren m
38534 6f 64 65 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67  ode */.    unsig
38535 6e 65 64 20 63 68 61 72 20 65 54 79 70 65 3b 20  ned char eType; 
38536 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38537 20 2f 2a 20 4b 65 79 77 6f 72 64 20 63 6f 64 65   /* Keyword code
38538 20 2a 2f 0a 20 20 7d 20 61 4b 65 79 77 6f 72 64   */.  } aKeyword
38539 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 4f 52  [] = {.    { "OR
3853a 22 20 2c 20 20 32 2c 20 30 2c 20 46 54 53 51 55  " ,  2, 0, FTSQU
3853b 45 52 59 5f 4f 52 20 20 20 7d 2c 0a 20 20 20 20  ERY_OR   },.    
3853c 7b 20 22 41 4e 44 22 2c 20 20 33 2c 20 31 2c 20  { "AND",  3, 1, 
3853d 46 54 53 51 55 45 52 59 5f 41 4e 44 20 20 7d 2c  FTSQUERY_AND  },
3853e 0a 20 20 20 20 7b 20 22 4e 4f 54 22 2c 20 20 33  .    { "NOT",  3
3853f 2c 20 31 2c 20 46 54 53 51 55 45 52 59 5f 4e 4f  , 1, FTSQUERY_NO
38540 54 20 20 7d 2c 0a 20 20 20 20 7b 20 22 4e 45 41  T  },.    { "NEA
38541 52 22 2c 20 34 2c 20 30 2c 20 46 54 53 51 55 45  R", 4, 0, FTSQUE
38542 52 59 5f 4e 45 41 52 20 7d 0a 20 20 7d 3b 0a 20  RY_NEAR }.  };. 
38543 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 69   int ii;.  int i
38544 43 6f 6c 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 4c  Col;.  int iColL
38545 65 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  en;.  int rc;.  
38546 46 74 73 33 45 78 70 72 20 2a 70 52 65 74 20 3d  Fts3Expr *pRet =
38547 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61   0;..  const cha
38548 72 20 2a 7a 49 6e 70 75 74 20 3d 20 7a 3b 0a 20  r *zInput = z;. 
38549 20 69 6e 74 20 6e 49 6e 70 75 74 20 3d 20 6e 3b   int nInput = n;
3854a 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72  ..  /* Skip over
3854b 20 61 6e 79 20 77 68 69 74 65 73 70 61 63 65 20   any whitespace 
3854c 62 65 66 6f 72 65 20 63 68 65 63 6b 69 6e 67 20  before checking 
3854d 66 6f 72 20 61 20 6b 65 79 77 6f 72 64 2c 20 61  for a keyword, a
3854e 6e 20 6f 70 65 6e 20 6f 72 0a 20 20 2a 2a 20 63  n open or.  ** c
3854f 6c 6f 73 65 20 62 72 61 63 6b 65 74 2c 20 6f 72  lose bracket, or
38550 20 61 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67   a quoted string
38551 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  . .  */.  while(
38552 20 6e 49 6e 70 75 74 3e 30 20 26 26 20 66 74 73   nInput>0 && fts
38553 33 69 73 73 70 61 63 65 28 2a 7a 49 6e 70 75 74  3isspace(*zInput
38554 29 20 29 7b 0a 20 20 20 20 6e 49 6e 70 75 74 2d  ) ){.    nInput-
38555 2d 3b 0a 20 20 20 20 7a 49 6e 70 75 74 2b 2b 3b  -;.    zInput++;
38556 0a 20 20 7d 0a 20 20 69 66 28 20 6e 49 6e 70 75  .  }.  if( nInpu
38557 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  t==0 ){.    retu
38558 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a  rn SQLITE_DONE;.
38559 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66    }..  /* See if
3855a 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20   we are dealing 
3855b 77 69 74 68 20 61 20 6b 65 79 77 6f 72 64 2e 20  with a keyword. 
3855c 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  */.  for(ii=0; i
3855d 69 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 61  i<(int)(sizeof(a
3855e 4b 65 79 77 6f 72 64 29 2f 73 69 7a 65 6f 66 28  Keyword)/sizeof(
3855f 73 74 72 75 63 74 20 46 74 73 33 4b 65 79 77 6f  struct Fts3Keywo
38560 72 64 29 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  rd)); ii++){.   
38561 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 46 74   const struct Ft
38562 73 33 4b 65 79 77 6f 72 64 20 2a 70 4b 65 79 20  s3Keyword *pKey 
38563 3d 20 26 61 4b 65 79 77 6f 72 64 5b 69 69 5d 3b  = &aKeyword[ii];
38564 0a 0a 20 20 20 20 69 66 28 20 28 70 4b 65 79 2d  ..    if( (pKey-
38565 3e 70 61 72 65 6e 4f 6e 6c 79 20 26 20 7e 73 71  >parenOnly & ~sq
38566 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c  lite3_fts3_enabl
38567 65 5f 70 61 72 65 6e 74 68 65 73 65 73 29 21 3d  e_parentheses)!=
38568 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69  0 ){.      conti
38569 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  nue;.    }..    
3856a 69 66 28 20 6e 49 6e 70 75 74 3e 3d 70 4b 65 79  if( nInput>=pKey
3856b 2d 3e 6e 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70  ->n && 0==memcmp
3856c 28 7a 49 6e 70 75 74 2c 20 70 4b 65 79 2d 3e 7a  (zInput, pKey->z
3856d 2c 20 70 4b 65 79 2d 3e 6e 29 20 29 7b 0a 20 20  , pKey->n) ){.  
3856e 20 20 20 20 69 6e 74 20 6e 4e 65 61 72 20 3d 20      int nNear = 
3856f 53 51 4c 49 54 45 5f 46 54 53 33 5f 44 45 46 41  SQLITE_FTS3_DEFA
38570 55 4c 54 5f 4e 45 41 52 5f 50 41 52 41 4d 3b 0a  ULT_NEAR_PARAM;.
38571 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 20 3d        int nKey =
38572 20 70 4b 65 79 2d 3e 6e 3b 0a 20 20 20 20 20 20   pKey->n;.      
38573 63 68 61 72 20 63 4e 65 78 74 3b 0a 0a 20 20 20  char cNext;..   
38574 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
38575 20 61 20 22 4e 45 41 52 22 20 6b 65 79 77 6f 72   a "NEAR" keywor
38576 64 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 6e 20  d, check for an 
38577 65 78 70 6c 69 63 69 74 20 6e 65 61 72 6e 65 73  explicit nearnes
38578 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  s. */.      if( 
38579 70 4b 65 79 2d 3e 65 54 79 70 65 3d 3d 46 54 53  pKey->eType==FTS
3857a 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20  QUERY_NEAR ){.  
3857b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 4b        assert( nK
3857c 65 79 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 20  ey==4 );.       
3857d 20 69 66 28 20 7a 49 6e 70 75 74 5b 34 5d 3d 3d   if( zInput[4]==
3857e 27 2f 27 20 26 26 20 7a 49 6e 70 75 74 5b 35 5d  '/' && zInput[5]
3857f 3e 3d 27 30 27 20 26 26 20 7a 49 6e 70 75 74 5b  >='0' && zInput[
38580 35 5d 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20  5]<='9' ){.     
38581 20 20 20 20 20 6e 4e 65 61 72 20 3d 20 30 3b 0a       nNear = 0;.
38582 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e 4b            for(nK
38583 65 79 3d 35 3b 20 7a 49 6e 70 75 74 5b 6e 4b 65  ey=5; zInput[nKe
38584 79 5d 3e 3d 27 30 27 20 26 26 20 7a 49 6e 70 75  y]>='0' && zInpu
38585 74 5b 6e 4b 65 79 5d 3c 3d 27 39 27 3b 20 6e 4b  t[nKey]<='9'; nK
38586 65 79 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  ey++){.         
38587 20 20 20 6e 4e 65 61 72 20 3d 20 6e 4e 65 61 72     nNear = nNear
38588 20 2a 20 31 30 20 2b 20 28 7a 49 6e 70 75 74 5b   * 10 + (zInput[
38589 6e 4b 65 79 5d 20 2d 20 27 30 27 29 3b 0a 20 20  nKey] - '0');.  
3858a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3858b 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
3858c 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f     /* At this po
3858d 69 6e 74 20 74 68 69 73 20 69 73 20 70 72 6f 62  int this is prob
3858e 61 62 6c 79 20 61 20 6b 65 79 77 6f 72 64 2e 20  ably a keyword. 
3858f 42 75 74 20 66 6f 72 20 74 68 61 74 20 74 6f 20  But for that to 
38590 62 65 20 74 72 75 65 2c 0a 20 20 20 20 20 20 2a  be true,.      *
38591 2a 20 74 68 65 20 6e 65 78 74 20 62 79 74 65 20  * the next byte 
38592 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 65 69 74  must contain eit
38593 68 65 72 20 77 68 69 74 65 73 70 61 63 65 2c 20  her whitespace, 
38594 61 6e 20 6f 70 65 6e 20 6f 72 20 63 6c 6f 73 65  an open or close
38595 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 65 6e 74  .      ** parent
38596 68 65 73 69 73 2c 20 61 20 71 75 6f 74 65 20 63  hesis, a quote c
38597 68 61 72 61 63 74 65 72 2c 20 6f 72 20 45 4f 46  haracter, or EOF
38598 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  . .      */.    
38599 20 20 63 4e 65 78 74 20 3d 20 7a 49 6e 70 75 74    cNext = zInput
3859a 5b 6e 4b 65 79 5d 3b 0a 20 20 20 20 20 20 69 66  [nKey];.      if
3859b 28 20 66 74 73 33 69 73 73 70 61 63 65 28 63 4e  ( fts3isspace(cN
3859c 65 78 74 29 20 0a 20 20 20 20 20 20 20 7c 7c 20  ext) .       || 
3859d 63 4e 65 78 74 3d 3d 27 22 27 20 7c 7c 20 63 4e  cNext=='"' || cN
3859e 65 78 74 3d 3d 27 28 27 20 7c 7c 20 63 4e 65 78  ext=='(' || cNex
3859f 74 3d 3d 27 29 27 20 7c 7c 20 63 4e 65 78 74 3d  t==')' || cNext=
385a0 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  =0.      ){.    
385a1 20 20 20 20 70 52 65 74 20 3d 20 28 46 74 73 33      pRet = (Fts3
385a2 45 78 70 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d  Expr *)sqlite3_m
385a3 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73  alloc(sizeof(Fts
385a4 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20  3Expr));.       
385a5 20 6d 65 6d 73 65 74 28 70 52 65 74 2c 20 30 2c   memset(pRet, 0,
385a6 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72   sizeof(Fts3Expr
385a7 29 29 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74  ));.        pRet
385a8 2d 3e 65 54 79 70 65 20 3d 20 70 4b 65 79 2d 3e  ->eType = pKey->
385a9 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 70  eType;.        p
385aa 52 65 74 2d 3e 6e 4e 65 61 72 20 3d 20 6e 4e 65  Ret->nNear = nNe
385ab 61 72 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 45  ar;.        *ppE
385ac 78 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 20 20  xpr = pRet;.    
385ad 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20      *pnConsumed 
385ae 3d 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b  = (zInput - z) +
385af 20 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 72   nKey;.        r
385b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
385b1 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
385b2 2f 2a 20 54 75 72 6e 73 20 6f 75 74 20 74 68 61  /* Turns out tha
385b3 74 20 77 61 73 6e 27 74 20 61 20 6b 65 79 77 6f  t wasn't a keywo
385b4 72 64 20 61 66 74 65 72 20 61 6c 6c 2e 20 54 68  rd after all. Th
385b5 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 74 68  is happens if th
385b6 65 0a 20 20 20 20 20 20 2a 2a 20 75 73 65 72 20  e.      ** user 
385b7 68 61 73 20 73 75 70 70 6c 69 65 64 20 61 20 74  has supplied a t
385b8 6f 6b 65 6e 20 73 75 63 68 20 61 73 20 22 4f 52  oken such as "OR
385b9 61 63 6c 65 22 2e 20 43 6f 6e 74 69 6e 75 65 2e  acle". Continue.
385ba 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 7d 0a  .      */.    }.
385bb 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
385bc 66 6f 72 20 61 6e 20 6f 70 65 6e 20 62 72 61 63  for an open brac
385bd 6b 65 74 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71  ket. */.  if( sq
385be 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c  lite3_fts3_enabl
385bf 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 29 7b  e_parentheses ){
385c0 0a 20 20 20 20 69 66 28 20 2a 7a 49 6e 70 75 74  .    if( *zInput
385c1 3d 3d 27 28 27 20 29 7b 0a 20 20 20 20 20 20 69  =='(' ){.      i
385c2 6e 74 20 6e 43 6f 6e 73 75 6d 65 64 3b 0a 20 20  nt nConsumed;.  
385c3 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
385c4 20 20 70 50 61 72 73 65 2d 3e 6e 4e 65 73 74 2b    pParse->nNest+
385c5 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74  +;.      rc = ft
385c6 73 33 45 78 70 72 50 61 72 73 65 28 70 50 61 72  s3ExprParse(pPar
385c7 73 65 2c 20 26 7a 49 6e 70 75 74 5b 31 5d 2c 20  se, &zInput[1], 
385c8 6e 49 6e 70 75 74 2d 31 2c 20 70 70 45 78 70 72  nInput-1, ppExpr
385c9 2c 20 26 6e 43 6f 6e 73 75 6d 65 64 29 3b 0a 20  , &nConsumed);. 
385ca 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
385cb 49 54 45 5f 4f 4b 20 26 26 20 21 2a 70 70 45 78  ITE_OK && !*ppEx
385cc 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  pr ){.        rc
385cd 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a   = SQLITE_DONE;.
385ce 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70        }.      *p
385cf 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e  nConsumed = (zIn
385d0 70 75 74 20 2d 20 7a 29 20 2b 20 31 20 2b 20 6e  put - z) + 1 + n
385d1 43 6f 6e 73 75 6d 65 64 3b 0a 20 20 20 20 20 20  Consumed;.      
385d2 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
385d3 0a 20 20 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  .  .    /* Check
385d4 20 66 6f 72 20 61 20 63 6c 6f 73 65 20 62 72 61   for a close bra
385d5 63 6b 65 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28  cket. */.    if(
385d6 20 2a 7a 49 6e 70 75 74 3d 3d 27 29 27 20 29 7b   *zInput==')' ){
385d7 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
385d8 4e 65 73 74 2d 2d 3b 0a 20 20 20 20 20 20 2a 70  Nest--;.      *p
385d9 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e  nConsumed = (zIn
385da 70 75 74 20 2d 20 7a 29 20 2b 20 31 3b 0a 20 20  put - z) + 1;.  
385db 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
385dc 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20  E_DONE;.    }.  
385dd 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 77  }..  /* See if w
385de 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
385df 74 68 20 61 20 71 75 6f 74 65 64 20 70 68 72 61  th a quoted phra
385e0 73 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 74  se. If this is t
385e1 68 65 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20  he case, then.  
385e2 2a 2a 20 73 65 61 72 63 68 20 66 6f 72 20 74 68  ** search for th
385e3 65 20 63 6c 6f 73 69 6e 67 20 71 75 6f 74 65 20  e closing quote 
385e4 61 6e 64 20 70 61 73 73 20 74 68 65 20 77 68 6f  and pass the who
385e5 6c 65 20 73 74 72 69 6e 67 20 74 6f 20 67 65 74  le string to get
385e6 4e 65 78 74 53 74 72 69 6e 67 28 29 0a 20 20 2a  NextString().  *
385e7 2a 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  * for processing
385e8 2e 20 54 68 69 73 20 69 73 20 65 61 73 79 20 74  . This is easy t
385e9 6f 20 64 6f 2c 20 61 73 20 66 74 73 33 20 68 61  o do, as fts3 ha
385ea 73 20 6e 6f 20 73 79 6e 74 61 78 20 66 6f 72 20  s no syntax for 
385eb 65 73 63 61 70 69 6e 67 0a 20 20 2a 2a 20 61 20  escaping.  ** a 
385ec 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 20  quote character 
385ed 65 6d 62 65 64 64 65 64 20 69 6e 20 61 20 73 74  embedded in a st
385ee 72 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ring..  */.  if(
385ef 20 2a 7a 49 6e 70 75 74 3d 3d 27 22 27 20 29 7b   *zInput=='"' ){
385f0 0a 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20 69  .    for(ii=1; i
385f1 69 3c 6e 49 6e 70 75 74 20 26 26 20 7a 49 6e 70  i<nInput && zInp
385f2 75 74 5b 69 69 5d 21 3d 27 22 27 3b 20 69 69 2b  ut[ii]!='"'; ii+
385f3 2b 29 3b 0a 20 20 20 20 2a 70 6e 43 6f 6e 73 75  +);.    *pnConsu
385f4 6d 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d 20  med = (zInput - 
385f5 7a 29 20 2b 20 69 69 20 2b 20 31 3b 0a 20 20 20  z) + ii + 1;.   
385f6 20 69 66 28 20 69 69 3d 3d 6e 49 6e 70 75 74 20   if( ii==nInput 
385f7 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
385f8 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
385f9 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 67    }.    return g
385fa 65 74 4e 65 78 74 53 74 72 69 6e 67 28 70 50 61  etNextString(pPa
385fb 72 73 65 2c 20 26 7a 49 6e 70 75 74 5b 31 5d 2c  rse, &zInput[1],
385fc 20 69 69 2d 31 2c 20 70 70 45 78 70 72 29 3b 0a   ii-1, ppExpr);.
385fd 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f    }...  /* If co
385fe 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74  ntrol flows to t
385ff 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 69 73 20  his point, this 
38600 6d 75 73 74 20 62 65 20 61 20 72 65 67 75 6c 61  must be a regula
38601 72 20 74 6f 6b 65 6e 2c 20 6f 72 20 0a 20 20 2a  r token, or .  *
38602 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  * the end of the
38603 20 69 6e 70 75 74 2e 20 52 65 61 64 20 61 20 72   input. Read a r
38604 65 67 75 6c 61 72 20 74 6f 6b 65 6e 20 75 73 69  egular token usi
38605 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74  ng the sqlite3_t
38606 6f 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6e  okenizer.  ** in
38607 74 65 72 66 61 63 65 2e 20 42 65 66 6f 72 65 20  terface. Before 
38608 64 6f 69 6e 67 20 73 6f 2c 20 66 69 67 75 72 65  doing so, figure
38609 20 6f 75 74 20 69 66 20 74 68 65 72 65 20 69 73   out if there is
3860a 20 61 6e 20 65 78 70 6c 69 63 69 74 0a 20 20 2a   an explicit.  *
3860b 2a 20 63 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69  * column specifi
3860c 65 72 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e  er for the token
3860d 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44  . .  **.  ** TOD
3860e 4f 3a 20 53 74 72 61 6e 67 65 6c 79 2c 20 69 74  O: Strangely, it
3860f 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
38610 20 74 6f 20 61 73 73 6f 63 69 61 74 65 20 61 20   to associate a 
38611 63 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69 65 72  column specifier
38612 0a 20 20 2a 2a 20 77 69 74 68 20 61 20 71 75 6f  .  ** with a quo
38613 74 65 64 20 70 68 72 61 73 65 2c 20 6f 6e 6c 79  ted phrase, only
38614 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74   with a single t
38615 6f 6b 65 6e 2e 20 4e 6f 74 20 73 75 72 65 20 69  oken. Not sure i
38616 66 20 74 68 69 73 20 77 61 73 0a 20 20 2a 2a 20  f this was.  ** 
38617 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  an implementatio
38618 6e 20 61 72 74 69 66 61 63 74 20 6f 72 20 61 6e  n artifact or an
38619 20 69 6e 74 65 6e 74 69 6f 6e 61 6c 20 64 65 63   intentional dec
3861a 69 73 69 6f 6e 20 77 68 65 6e 20 66 74 73 33 20  ision when fts3 
3861b 77 61 73 0a 20 20 2a 2a 20 66 69 72 73 74 20 69  was.  ** first i
3861c 6d 70 6c 65 6d 65 6e 74 65 64 2e 20 57 68 69 63  mplemented. Whic
3861d 68 65 76 65 72 20 69 74 20 77 61 73 2c 20 74 68  hever it was, th
3861e 69 73 20 6d 6f 64 75 6c 65 20 64 75 70 6c 69 63  is module duplic
3861f 61 74 65 73 20 74 68 65 20 0a 20 20 2a 2a 20 6c  ates the .  ** l
38620 69 6d 69 74 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  imitation..  */.
38621 20 20 69 43 6f 6c 20 3d 20 70 50 61 72 73 65 2d    iCol = pParse-
38622 3e 69 44 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20  >iDefaultCol;.  
38623 69 43 6f 6c 4c 65 6e 20 3d 20 30 3b 0a 20 20 66  iColLen = 0;.  f
38624 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 72  or(ii=0; ii<pPar
38625 73 65 2d 3e 6e 43 6f 6c 3b 20 69 69 2b 2b 29 7b  se->nCol; ii++){
38626 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
38627 2a 7a 53 74 72 20 3d 20 70 50 61 72 73 65 2d 3e  *zStr = pParse->
38628 61 7a 43 6f 6c 5b 69 69 5d 3b 0a 20 20 20 20 69  azCol[ii];.    i
38629 6e 74 20 6e 53 74 72 20 3d 20 73 74 72 6c 65 6e  nt nStr = strlen
3862a 28 7a 53 74 72 29 3b 0a 20 20 20 20 69 66 28 20  (zStr);.    if( 
3862b 6e 49 6e 70 75 74 3e 6e 53 74 72 20 26 26 20 7a  nInput>nStr && z
3862c 49 6e 70 75 74 5b 6e 53 74 72 5d 3d 3d 27 3a 27  Input[nStr]==':'
3862d 20 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65   .     && sqlite
3862e 33 5f 73 74 72 6e 69 63 6d 70 28 7a 53 74 72 2c  3_strnicmp(zStr,
3862f 20 7a 49 6e 70 75 74 2c 20 6e 53 74 72 29 3d 3d   zInput, nStr)==
38630 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  0 .    ){.      
38631 69 43 6f 6c 20 3d 20 69 69 3b 0a 20 20 20 20 20  iCol = ii;.     
38632 20 69 43 6f 6c 4c 65 6e 20 3d 20 28 28 7a 49 6e   iColLen = ((zIn
38633 70 75 74 20 2d 20 7a 29 20 2b 20 6e 53 74 72 20  put - z) + nStr 
38634 2b 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61  + 1);.      brea
38635 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  k;.    }.  }.  r
38636 63 20 3d 20 67 65 74 4e 65 78 74 54 6f 6b 65 6e  c = getNextToken
38637 28 70 50 61 72 73 65 2c 20 69 43 6f 6c 2c 20 26  (pParse, iCol, &
38638 7a 5b 69 43 6f 6c 4c 65 6e 5d 2c 20 6e 2d 69 43  z[iColLen], n-iC
38639 6f 6c 4c 65 6e 2c 20 70 70 45 78 70 72 2c 20 70  olLen, ppExpr, p
3863a 6e 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 2a 70  nConsumed);.  *p
3863b 6e 43 6f 6e 73 75 6d 65 64 20 2b 3d 20 69 43 6f  nConsumed += iCo
3863c 6c 4c 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20 72  lLen;.  return r
3863d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
3863e 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 46  argument is an F
3863f 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75 72  ts3Expr structur
38640 65 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 6f  e for a binary o
38641 70 65 72 61 74 6f 72 20 28 61 6e 79 20 74 79 70  perator (any typ
38642 65 0a 2a 2a 20 65 78 63 65 70 74 20 61 6e 20 46  e.** except an F
38643 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 29 2e  TSQUERY_PHRASE).
38644 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67   Return an integ
38645 65 72 20 76 61 6c 75 65 20 72 65 70 72 65 73 65  er value represe
38646 6e 74 69 6e 67 20 74 68 65 0a 2a 2a 20 70 72 65  nting the.** pre
38647 63 65 64 65 6e 63 65 20 6f 66 20 74 68 65 20 6f  cedence of the o
38648 70 65 72 61 74 6f 72 2e 20 4c 6f 77 65 72 20 76  perator. Lower v
38649 61 6c 75 65 73 20 68 61 76 65 20 61 20 68 69 67  alues have a hig
3864a 68 65 72 20 70 72 65 63 65 64 65 6e 63 65 20 28  her precedence (
3864b 69 2e 65 2e 0a 2a 2a 20 67 72 6f 75 70 20 6d 6f  i.e..** group mo
3864c 72 65 20 74 69 67 68 74 6c 79 29 2e 20 46 6f 72  re tightly). For
3864d 20 65 78 61 6d 70 6c 65 2c 20 69 6e 20 74 68 65   example, in the
3864e 20 43 20 6c 61 6e 67 75 61 67 65 2c 20 74 68 65   C language, the
3864f 20 3d 3d 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20   == operator.** 
38650 67 72 6f 75 70 73 20 6d 6f 72 65 20 74 69 67 68  groups more tigh
38651 74 6c 79 20 74 68 61 6e 20 7c 7c 2c 20 61 6e 64  tly than ||, and
38652 20 77 6f 75 6c 64 20 74 68 65 72 65 66 6f 72 65   would therefore
38653 20 68 61 76 65 20 61 20 68 69 67 68 65 72 20 70   have a higher p
38654 72 65 63 65 64 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a  recedence..**.**
38655 20 57 68 65 6e 20 75 73 69 6e 67 20 74 68 65 20   When using the 
38656 6e 65 77 20 66 74 73 33 20 71 75 65 72 79 20 73  new fts3 query s
38657 79 6e 74 61 78 20 28 77 68 65 6e 20 53 51 4c 49  yntax (when SQLI
38658 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50  TE_ENABLE_FTS3_P
38659 41 52 45 4e 54 48 45 53 49 53 0a 2a 2a 20 69 73  ARENTHESIS.** is
3865a 20 64 65 66 69 6e 65 64 29 2c 20 74 68 65 20 6f   defined), the o
3865b 72 64 65 72 20 6f 66 20 74 68 65 20 6f 70 65 72  rder of the oper
3865c 61 74 6f 72 73 20 69 6e 20 70 72 65 63 65 64 65  ators in precede
3865d 6e 63 65 20 66 72 6f 6d 20 68 69 67 68 65 73 74  nce from highest
3865e 20 74 6f 0a 2a 2a 20 6c 6f 77 65 73 74 20 69 73   to.** lowest is
3865f 3a 0a 2a 2a 0a 2a 2a 20 20 20 4e 45 41 52 0a 2a  :.**.**   NEAR.*
38660 2a 20 20 20 4e 4f 54 0a 2a 2a 20 20 20 41 4e 44  *   NOT.**   AND
38661 20 28 69 6e 63 6c 75 64 69 6e 67 20 69 6d 70 6c   (including impl
38662 69 63 69 74 20 41 4e 44 73 29 0a 2a 2a 20 20 20  icit ANDs).**   
38663 4f 52 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  OR.**.** Note th
38664 61 74 20 77 68 65 6e 20 75 73 69 6e 67 20 74 68  at when using th
38665 65 20 6f 6c 64 20 71 75 65 72 79 20 73 79 6e 74  e old query synt
38666 61 78 2c 20 74 68 65 20 4f 52 20 6f 70 65 72 61  ax, the OR opera
38667 74 6f 72 20 68 61 73 20 61 20 68 69 67 68 65 72  tor has a higher
38668 0a 2a 2a 20 70 72 65 63 65 64 65 6e 63 65 20 74  .** precedence t
38669 68 61 6e 20 74 68 65 20 41 4e 44 20 6f 70 65 72  han the AND oper
3866a 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ator..*/.static 
3866b 69 6e 74 20 6f 70 50 72 65 63 65 64 65 6e 63 65  int opPrecedence
3866c 28 46 74 73 33 45 78 70 72 20 2a 70 29 7b 0a 20  (Fts3Expr *p){. 
3866d 20 61 73 73 65 72 74 28 20 70 2d 3e 65 54 79 70   assert( p->eTyp
3866e 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e!=FTSQUERY_PHRA
3866f 53 45 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  SE );.  if( sqli
38670 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
38671 70 61 72 65 6e 74 68 65 73 65 73 20 29 7b 0a 20  parentheses ){. 
38672 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 65 54 79     return p->eTy
38673 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  pe;.  }else if( 
38674 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
38675 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 72  RY_NEAR ){.    r
38676 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65  eturn 1;.  }else
38677 20 69 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d 46   if( p->eType==F
38678 54 53 51 55 45 52 59 5f 4f 52 20 29 7b 0a 20 20  TSQUERY_OR ){.  
38679 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a    return 2;.  }.
3867a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 54 79    assert( p->eTy
3867b 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 41 4e 44  pe==FTSQUERY_AND
3867c 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 33 3b 0a   );.  return 3;.
3867d 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e  }../*.** Argumen
3867e 74 20 70 70 48 65 61 64 20 63 6f 6e 74 61 69 6e  t ppHead contain
3867f 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
38680 68 65 20 63 75 72 72 65 6e 74 20 68 65 61 64 20  he current head 
38681 6f 66 20 61 20 71 75 65 72 79 20 0a 2a 2a 20 65  of a query .** e
38682 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 62  xpression tree b
38683 65 69 6e 67 20 70 61 72 73 65 64 2e 20 70 50 72  eing parsed. pPr
38684 65 76 20 69 73 20 74 68 65 20 65 78 70 72 65 73  ev is the expres
38685 73 69 6f 6e 20 6e 6f 64 65 20 6d 6f 73 74 20 72  sion node most r
38686 65 63 65 6e 74 6c 79 0a 2a 2a 20 69 6e 73 65 72  ecently.** inser
38687 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72 65  ted into the tre
38688 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  e. This function
38689 20 61 64 64 73 20 70 4e 65 77 2c 20 77 68 69 63   adds pNew, whic
3868a 68 20 69 73 20 61 6c 77 61 79 73 20 61 20 62 69  h is always a bi
3868b 6e 61 72 79 0a 2a 2a 20 6f 70 65 72 61 74 6f 72  nary.** operator
3868c 20 6e 6f 64 65 2c 20 69 6e 74 6f 20 74 68 65 20   node, into the 
3868d 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
3868e 62 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65 6c  based on the rel
3868f 61 74 69 76 65 20 70 72 65 63 65 64 65 6e 63 65  ative precedence
38690 0a 2a 2a 20 6f 66 20 70 4e 65 77 20 61 6e 64 20  .** of pNew and 
38691 74 68 65 20 65 78 69 73 74 69 6e 67 20 6e 6f 64  the existing nod
38692 65 73 20 6f 66 20 74 68 65 20 74 72 65 65 2e 20  es of the tree. 
38693 54 68 69 73 20 6d 61 79 20 72 65 73 75 6c 74 20  This may result 
38694 69 6e 20 74 68 65 20 68 65 61 64 0a 2a 2a 20 6f  in the head.** o
38695 66 20 74 68 65 20 74 72 65 65 20 63 68 61 6e 67  f the tree chang
38696 69 6e 67 2c 20 69 6e 20 77 68 69 63 68 20 63 61  ing, in which ca
38697 73 65 20 2a 70 70 48 65 61 64 20 69 73 20 73 65  se *ppHead is se
38698 74 20 74 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f  t to the new roo
38699 74 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  t node..*/.stati
3869a 63 20 76 6f 69 64 20 69 6e 73 65 72 74 42 69 6e  c void insertBin
3869b 61 72 79 4f 70 65 72 61 74 6f 72 28 0a 20 20 46  aryOperator(.  F
3869c 74 73 33 45 78 70 72 20 2a 2a 70 70 48 65 61 64  ts3Expr **ppHead
3869d 2c 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  ,       /* Point
3869e 65 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e  er to the root n
3869f 6f 64 65 20 6f 66 20 61 20 74 72 65 65 20 2a 2f  ode of a tree */
386a0 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 50 72  .  Fts3Expr *pPr
386a1 65 76 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ev,         /* N
386a2 6f 64 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  ode most recentl
386a3 79 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  y inserted into 
386a4 74 68 65 20 74 72 65 65 20 2a 2f 0a 20 20 46 74  the tree */.  Ft
386a5 73 33 45 78 70 72 20 2a 70 4e 65 77 20 20 20 20  s3Expr *pNew    
386a6 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 62 69         /* New bi
386a7 6e 61 72 79 20 6e 6f 64 65 20 74 6f 20 69 6e 73  nary node to ins
386a8 65 72 74 20 69 6e 74 6f 20 65 78 70 72 65 73 73  ert into express
386a9 69 6f 6e 20 74 72 65 65 20 2a 2f 0a 29 7b 0a 20  ion tree */.){. 
386aa 20 46 74 73 33 45 78 70 72 20 2a 70 53 70 6c 69   Fts3Expr *pSpli
386ab 74 20 3d 20 70 50 72 65 76 3b 0a 20 20 77 68 69  t = pPrev;.  whi
386ac 6c 65 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72  le( pSplit->pPar
386ad 65 6e 74 20 26 26 20 6f 70 50 72 65 63 65 64 65  ent && opPrecede
386ae 6e 63 65 28 70 53 70 6c 69 74 2d 3e 70 50 61 72  nce(pSplit->pPar
386af 65 6e 74 29 3c 3d 6f 70 50 72 65 63 65 64 65 6e  ent)<=opPreceden
386b0 63 65 28 70 4e 65 77 29 20 29 7b 0a 20 20 20 20  ce(pNew) ){.    
386b1 70 53 70 6c 69 74 20 3d 20 70 53 70 6c 69 74 2d  pSplit = pSplit-
386b2 3e 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 0a 20  >pParent;.  }.. 
386b3 20 69 66 28 20 70 53 70 6c 69 74 2d 3e 70 50 61   if( pSplit->pPa
386b4 72 65 6e 74 20 29 7b 0a 20 20 20 20 61 73 73 65  rent ){.    asse
386b5 72 74 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72  rt( pSplit->pPar
386b6 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70 53 70  ent->pRight==pSp
386b7 6c 69 74 20 29 3b 0a 20 20 20 20 70 53 70 6c 69  lit );.    pSpli
386b8 74 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67  t->pParent->pRig
386b9 68 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70  ht = pNew;.    p
386ba 4e 65 77 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70  New->pParent = p
386bb 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 3b 0a  Split->pParent;.
386bc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70    }else{.    *pp
386bd 48 65 61 64 20 3d 20 70 4e 65 77 3b 0a 20 20 7d  Head = pNew;.  }
386be 0a 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d  .  pNew->pLeft =
386bf 20 70 53 70 6c 69 74 3b 0a 20 20 70 53 70 6c 69   pSplit;.  pSpli
386c0 74 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 65  t->pParent = pNe
386c1 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73  w;.}../*.** Pars
386c2 65 20 74 68 65 20 66 74 73 33 20 71 75 65 72 79  e the fts3 query
386c3 20 65 78 70 72 65 73 73 69 6f 6e 20 66 6f 75 6e   expression foun
386c4 64 20 69 6e 20 62 75 66 66 65 72 20 7a 2c 20 6c  d in buffer z, l
386c5 65 6e 67 74 68 20 6e 2e 20 54 68 69 73 20 66 75  ength n. This fu
386c6 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e  nction.** return
386c7 73 20 65 69 74 68 65 72 20 77 68 65 6e 20 74 68  s either when th
386c8 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66  e end of the buf
386c9 66 65 72 20 69 73 20 72 65 61 63 68 65 64 20 6f  fer is reached o
386ca 72 20 61 6e 20 75 6e 6d 61 74 63 68 65 64 20 0a  r an unmatched .
386cb 2a 2a 20 63 6c 6f 73 69 6e 67 20 62 72 61 63 6b  ** closing brack
386cc 65 74 20 2d 20 27 29 27 20 2d 20 69 73 20 65 6e  et - ')' - is en
386cd 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a  countered..**.**
386ce 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
386cf 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
386d0 75 72 6e 65 64 2c 20 2a 70 70 45 78 70 72 20 69  urned, *ppExpr i
386d1 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74  s set to point t
386d2 6f 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 64 20  o the.** parsed 
386d3 66 6f 72 6d 20 6f 66 20 74 68 65 20 65 78 70 72  form of the expr
386d4 65 73 73 69 6f 6e 20 61 6e 64 20 2a 70 6e 43 6f  ession and *pnCo
386d5 6e 73 75 6d 65 64 20 69 73 20 73 65 74 20 74 6f  nsumed is set to
386d6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
386d7 2a 20 62 79 74 65 73 20 72 65 61 64 20 66 72 6f  * bytes read fro
386d8 6d 20 62 75 66 66 65 72 20 7a 2e 20 4f 74 68 65  m buffer z. Othe
386d9 72 77 69 73 65 2c 20 2a 70 70 45 78 70 72 20 69  rwise, *ppExpr i
386da 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53  s set to 0 and S
386db 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28  QLITE_NOMEM.** (
386dc 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65 72  out of memory er
386dd 72 6f 72 29 20 6f 72 20 53 51 4c 49 54 45 5f 45  ror) or SQLITE_E
386de 52 52 4f 52 20 28 70 61 72 73 65 20 65 72 72 6f  RROR (parse erro
386df 72 29 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  r) is returned..
386e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
386e1 73 33 45 78 70 72 50 61 72 73 65 28 0a 20 20 50  s3ExprParse(.  P
386e2 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61  arseContext *pPa
386e3 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
386e4 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71         /* fts3 q
386e5 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65  uery parse conte
386e6 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  xt */.  const ch
386e7 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20  ar *z, int n,   
386e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
386e9 2f 2a 20 54 65 78 74 20 6f 66 20 4d 41 54 43 48  /* Text of MATCH
386ea 20 71 75 65 72 79 20 2a 2f 0a 20 20 46 74 73 33   query */.  Fts3
386eb 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c 20 20  Expr **ppExpr,  
386ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
386ed 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72 73      /* OUT: Pars
386ee 65 64 20 71 75 65 72 79 20 73 74 72 75 63 74 75  ed query structu
386ef 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43  re */.  int *pnC
386f0 6f 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20 20  onsumed         
386f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
386f2 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f  /* OUT: Number o
386f3 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64  f bytes consumed
386f4 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78 70   */.){.  Fts3Exp
386f5 72 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 46  r *pRet = 0;.  F
386f6 74 73 33 45 78 70 72 20 2a 70 50 72 65 76 20 3d  ts3Expr *pPrev =
386f7 20 30 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a   0;.  Fts3Expr *
386f8 70 4e 6f 74 42 72 61 6e 63 68 20 3d 20 30 3b 20  pNotBranch = 0; 
386f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
386fa 20 4f 6e 6c 79 20 75 73 65 64 20 69 6e 20 6c 65   Only used in le
386fb 67 61 63 79 20 70 61 72 73 65 20 6d 6f 64 65 20  gacy parse mode 
386fc 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 20 3d 20 6e  */.  int nIn = n
386fd 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
386fe 7a 49 6e 20 3d 20 7a 3b 0a 20 20 69 6e 74 20 72  zIn = z;.  int r
386ff 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
38700 20 69 6e 74 20 69 73 52 65 71 75 69 72 65 50 68   int isRequirePh
38701 72 61 73 65 20 3d 20 31 3b 0a 0a 20 20 77 68 69  rase = 1;..  whi
38702 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
38703 4b 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78 70  K ){.    Fts3Exp
38704 72 20 2a 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e  r *p = 0;.    in
38705 74 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20 20  t nByte = 0;.   
38706 20 72 63 20 3d 20 67 65 74 4e 65 78 74 4e 6f 64   rc = getNextNod
38707 65 28 70 50 61 72 73 65 2c 20 7a 49 6e 2c 20 6e  e(pParse, zIn, n
38708 49 6e 2c 20 26 70 2c 20 26 6e 42 79 74 65 29 3b  In, &p, &nByte);
38709 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
3870a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3870b 69 6e 74 20 69 73 50 68 72 61 73 65 3b 0a 0a 20  int isPhrase;.. 
3870c 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65       if( !sqlite
3870d 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61  3_fts3_enable_pa
3870e 72 65 6e 74 68 65 73 65 73 20 0a 20 20 20 20 20  rentheses .     
3870f 20 20 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46    && p->eType==F
38710 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 26  TSQUERY_PHRASE &
38711 26 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69 73  & p->pPhrase->is
38712 4e 6f 74 20 0a 20 20 20 20 20 20 29 7b 0a 20 20  Not .      ){.  
38713 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20        /* Create 
38714 61 6e 20 69 6d 70 6c 69 63 69 74 20 4e 4f 54 20  an implicit NOT 
38715 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0a 20 20 20  operator. */.   
38716 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70       Fts3Expr *p
38717 4e 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  Not = sqlite3_ma
38718 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73 33  lloc(sizeof(Fts3
38719 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20  Expr));.        
3871a 69 66 28 20 21 70 4e 6f 74 20 29 7b 0a 20 20 20  if( !pNot ){.   
3871b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74         sqlite3Ft
3871c 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20  s3ExprFree(p);. 
3871d 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
3871e 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
3871f 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70        goto exprp
38720 61 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20  arse_out;.      
38721 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 65 6d 73    }.        mems
38722 65 74 28 70 4e 6f 74 2c 20 30 2c 20 73 69 7a 65  et(pNot, 0, size
38723 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20  of(Fts3Expr));. 
38724 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e 65 54 79         pNot->eTy
38725 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f 4e 4f  pe = FTSQUERY_NO
38726 54 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f 74 2d  T;.        pNot-
38727 3e 70 52 69 67 68 74 20 3d 20 70 3b 0a 20 20 20  >pRight = p;.   
38728 20 20 20 20 20 69 66 28 20 70 4e 6f 74 42 72 61       if( pNotBra
38729 6e 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20  nch ){.         
3872a 20 70 4e 6f 74 2d 3e 70 4c 65 66 74 20 3d 20 70   pNot->pLeft = p
3872b 4e 6f 74 42 72 61 6e 63 68 3b 0a 20 20 20 20 20  NotBranch;.     
3872c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4e 6f     }.        pNo
3872d 74 42 72 61 6e 63 68 20 3d 20 70 4e 6f 74 3b 0a  tBranch = pNot;.
3872e 20 20 20 20 20 20 20 20 70 20 3d 20 70 50 72 65          p = pPre
3872f 76 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  v;.      }else{.
38730 20 20 20 20 20 20 20 20 69 6e 74 20 65 54 79 70          int eTyp
38731 65 20 3d 20 70 2d 3e 65 54 79 70 65 3b 0a 20 20  e = p->eType;.  
38732 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54        assert( eT
38733 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48  ype!=FTSQUERY_PH
38734 52 41 53 45 20 7c 7c 20 21 70 2d 3e 70 50 68 72  RASE || !p->pPhr
38735 61 73 65 2d 3e 69 73 4e 6f 74 20 29 3b 0a 20 20  ase->isNot );.  
38736 20 20 20 20 20 20 69 73 50 68 72 61 73 65 20 3d        isPhrase =
38737 20 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52   (eType==FTSQUER
38738 59 5f 50 48 52 41 53 45 20 7c 7c 20 70 2d 3e 70  Y_PHRASE || p->p
38739 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 20  Left);..        
3873a 2f 2a 20 54 68 65 20 69 73 52 65 71 75 69 72 65  /* The isRequire
3873b 50 68 72 61 73 65 20 76 61 72 69 61 62 6c 65 20  Phrase variable 
3873c 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69  is set to true i
3873d 66 20 61 20 70 68 72 61 73 65 20 6f 72 0a 20 20  f a phrase or.  
3873e 20 20 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72        ** an expr
3873f 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 65 64  ession contained
38740 20 69 6e 20 70 61 72 65 6e 74 68 65 73 69 73 20   in parenthesis 
38741 69 73 20 72 65 71 75 69 72 65 64 2e 20 49 66 20  is required. If 
38742 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 69 6e  a.        ** bin
38743 61 72 79 20 6f 70 65 72 61 74 6f 72 20 28 41 4e  ary operator (AN
38744 44 2c 20 4f 52 2c 20 4e 4f 54 20 6f 72 20 4e 45  D, OR, NOT or NE
38745 41 52 29 20 69 73 20 65 6e 63 6f 75 6e 74 65 64  AR) is encounted
38746 20 77 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a   when.        **
38747 20 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65   isRequirePhrase
38748 20 69 73 20 73 65 74 2c 20 74 68 69 73 20 69 73   is set, this is
38749 20 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72 2e   a syntax error.
3874a 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
3874b 20 20 20 20 69 66 28 20 21 69 73 50 68 72 61 73      if( !isPhras
3874c 65 20 26 26 20 69 73 52 65 71 75 69 72 65 50 68  e && isRequirePh
3874d 72 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20  rase ){.        
3874e 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
3874f 72 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  rFree(p);.      
38750 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
38751 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20  ERROR;.         
38752 20 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f   goto exprparse_
38753 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  out;.        }. 
38754 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73   .        if( is
38755 50 68 72 61 73 65 20 26 26 20 21 69 73 52 65 71  Phrase && !isReq
38756 75 69 72 65 50 68 72 61 73 65 20 29 7b 0a 20 20  uirePhrase ){.  
38757 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72          /* Inser
38758 74 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 41 4e  t an implicit AN
38759 44 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0a 20  D operator. */. 
3875a 20 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70           Fts3Exp
3875b 72 20 2a 70 41 6e 64 3b 0a 20 20 20 20 20 20 20  r *pAnd;.       
3875c 20 20 20 61 73 73 65 72 74 28 20 70 52 65 74 20     assert( pRet 
3875d 26 26 20 70 50 72 65 76 20 29 3b 0a 20 20 20 20  && pPrev );.    
3875e 20 20 20 20 20 20 70 41 6e 64 20 3d 20 73 71 6c        pAnd = sql
3875f 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
38760 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20  of(Fts3Expr));. 
38761 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 41           if( !pA
38762 6e 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  nd ){.          
38763 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
38764 72 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  rFree(p);.      
38765 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
38766 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20  E_NOMEM;.       
38767 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61       goto exprpa
38768 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  rse_out;.       
38769 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d     }.          m
3876a 65 6d 73 65 74 28 70 41 6e 64 2c 20 30 2c 20 73  emset(pAnd, 0, s
3876b 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 29  izeof(Fts3Expr))
3876c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 41 6e 64  ;.          pAnd
3876d 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55 45  ->eType = FTSQUE
3876e 52 59 5f 41 4e 44 3b 0a 20 20 20 20 20 20 20 20  RY_AND;.        
3876f 20 20 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70    insertBinaryOp
38770 65 72 61 74 6f 72 28 26 70 52 65 74 2c 20 70 50  erator(&pRet, pP
38771 72 65 76 2c 20 70 41 6e 64 29 3b 0a 20 20 20 20  rev, pAnd);.    
38772 20 20 20 20 20 20 70 50 72 65 76 20 3d 20 70 41        pPrev = pA
38773 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  nd;.        }.. 
38774 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 74         /* This t
38775 65 73 74 20 63 61 74 63 68 65 73 20 61 74 74 65  est catches atte
38776 6d 70 74 73 20 74 6f 20 6d 61 6b 65 20 65 69 74  mpts to make eit
38777 68 65 72 20 6f 70 65 72 61 6e 64 20 6f 66 20 61  her operand of a
38778 20 4e 45 41 52 0a 20 20 20 20 20 20 20 20 2a 2a   NEAR.        **
38779 20 6f 70 65 72 61 74 6f 72 20 73 6f 6d 65 74 68   operator someth
3877a 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61  ing other than a
3877b 20 70 68 72 61 73 65 2e 20 46 6f 72 20 65 78 61   phrase. For exa
3877c 6d 70 6c 65 2c 20 65 69 74 68 65 72 20 6f 66 0a  mple, either of.
3877d 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66          ** the f
3877e 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20 20 20  ollowing:.      
3877f 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
38780 20 20 20 28 62 72 61 63 6b 65 74 65 64 20 65 78     (bracketed ex
38781 70 72 65 73 73 69 6f 6e 29 20 4e 45 41 52 20 70  pression) NEAR p
38782 68 72 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a  hrase.        **
38783 20 20 20 20 70 68 72 61 73 65 20 4e 45 41 52 20      phrase NEAR 
38784 28 62 72 61 63 6b 65 74 65 64 20 65 78 70 72 65  (bracketed expre
38785 73 73 69 6f 6e 29 0a 20 20 20 20 20 20 20 20 2a  ssion).        *
38786 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 52 65 74  *.        ** Ret
38787 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 20  urn an error in 
38788 65 69 74 68 65 72 20 63 61 73 65 2e 0a 20 20 20  either case..   
38789 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
3878a 69 66 28 20 70 50 72 65 76 20 26 26 20 28 0a 20  if( pPrev && (. 
3878b 20 20 20 20 20 20 20 20 20 20 20 28 65 54 79 70             (eTyp
3878c 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
3878d 20 26 26 20 21 69 73 50 68 72 61 73 65 20 26 26   && !isPhrase &&
3878e 20 70 50 72 65 76 2d 3e 65 54 79 70 65 21 3d 46   pPrev->eType!=F
3878f 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 29 0a  TSQUERY_PHRASE).
38790 20 20 20 20 20 20 20 20 20 7c 7c 20 28 65 54 79           || (eTy
38791 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe!=FTSQUERY_PHR
38792 41 53 45 20 26 26 20 69 73 50 68 72 61 73 65 20  ASE && isPhrase 
38793 26 26 20 70 50 72 65 76 2d 3e 65 54 79 70 65 3d  && pPrev->eType=
38794 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 29 0a  =FTSQUERY_NEAR).
38795 20 20 20 20 20 20 20 20 29 29 7b 0a 20 20 20 20          )){.    
38796 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73        sqlite3Fts
38797 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20 20  3ExprFree(p);.  
38798 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
38799 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
3879a 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61       goto exprpa
3879b 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  rse_out;.       
3879c 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 69 66   }.  .        if
3879d 28 20 69 73 50 68 72 61 73 65 20 29 7b 0a 20 20  ( isPhrase ){.  
3879e 20 20 20 20 20 20 20 20 69 66 28 20 70 52 65 74          if( pRet
3879f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
387a0 61 73 73 65 72 74 28 20 70 50 72 65 76 20 26 26  assert( pPrev &&
387a1 20 70 50 72 65 76 2d 3e 70 4c 65 66 74 20 26 26   pPrev->pLeft &&
387a2 20 70 50 72 65 76 2d 3e 70 52 69 67 68 74 3d 3d   pPrev->pRight==
387a3 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  0 );.           
387a4 20 70 50 72 65 76 2d 3e 70 52 69 67 68 74 20 3d   pPrev->pRight =
387a5 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   p;.            
387a6 70 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 72  p->pParent = pPr
387a7 65 76 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  ev;.          }e
387a8 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
387a9 20 70 52 65 74 20 3d 20 70 3b 0a 20 20 20 20 20   pRet = p;.     
387aa 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
387ab 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
387ac 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70 65 72  insertBinaryOper
387ad 61 74 6f 72 28 26 70 52 65 74 2c 20 70 50 72 65  ator(&pRet, pPre
387ae 76 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 7d  v, p);.        }
387af 0a 20 20 20 20 20 20 20 20 69 73 52 65 71 75 69  .        isRequi
387b0 72 65 50 68 72 61 73 65 20 3d 20 21 69 73 50 68  rePhrase = !isPh
387b1 72 61 73 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rase;.      }.  
387b2 20 20 20 20 61 73 73 65 72 74 28 20 6e 42 79 74      assert( nByt
387b3 65 3e 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  e>0 );.    }.   
387b4 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
387b5 49 54 45 5f 4f 4b 20 7c 7c 20 28 6e 42 79 74 65  ITE_OK || (nByte
387b6 3e 30 20 26 26 20 6e 42 79 74 65 3c 3d 6e 49 6e  >0 && nByte<=nIn
387b7 29 20 29 3b 0a 20 20 20 20 6e 49 6e 20 2d 3d 20  ) );.    nIn -= 
387b8 6e 42 79 74 65 3b 0a 20 20 20 20 7a 49 6e 20 2b  nByte;.    zIn +
387b9 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 50 72  = nByte;.    pPr
387ba 65 76 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 69  ev = p;.  }..  i
387bb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
387bc 4e 45 20 26 26 20 70 52 65 74 20 26 26 20 69 73  NE && pRet && is
387bd 52 65 71 75 69 72 65 50 68 72 61 73 65 20 29 7b  RequirePhrase ){
387be 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
387bf 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 69  _ERROR;.  }..  i
387c0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
387c1 4e 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  NE ){.    rc = S
387c2 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 66  QLITE_OK;.    if
387c3 28 20 21 73 71 6c 69 74 65 33 5f 66 74 73 33 5f  ( !sqlite3_fts3_
387c4 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73  enable_parenthes
387c5 65 73 20 26 26 20 70 4e 6f 74 42 72 61 6e 63 68  es && pNotBranch
387c6 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70   ){.      if( !p
387c7 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  Ret ){.        r
387c8 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
387c9 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
387ca 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20         Fts3Expr 
387cb 2a 70 49 74 65 72 20 3d 20 70 4e 6f 74 42 72 61  *pIter = pNotBra
387cc 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 77 68 69  nch;.        whi
387cd 6c 65 28 20 70 49 74 65 72 2d 3e 70 4c 65 66 74  le( pIter->pLeft
387ce 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 49   ){.          pI
387cf 74 65 72 20 3d 20 70 49 74 65 72 2d 3e 70 4c 65  ter = pIter->pLe
387d0 66 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ft;.        }.  
387d1 20 20 20 20 20 20 70 49 74 65 72 2d 3e 70 4c 65        pIter->pLe
387d2 66 74 20 3d 20 70 52 65 74 3b 0a 20 20 20 20 20  ft = pRet;.     
387d3 20 20 20 70 52 65 74 20 3d 20 70 4e 6f 74 42 72     pRet = pNotBr
387d4 61 6e 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20  anch;.      }.  
387d5 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 6e 43 6f 6e    }.  }.  *pnCon
387d6 73 75 6d 65 64 20 3d 20 6e 20 2d 20 6e 49 6e 3b  sumed = n - nIn;
387d7 0a 0a 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3a  ..exprparse_out:
387d8 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
387d9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
387da 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28  te3Fts3ExprFree(
387db 70 52 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  pRet);.    sqlit
387dc 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
387dd 4e 6f 74 42 72 61 6e 63 68 29 3b 0a 20 20 20 20  NotBranch);.    
387de 70 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pRet = 0;.  }.  
387df 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74 3b 0a  *ppExpr = pRet;.
387e0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
387e1 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73  /*.** Parameters
387e2 20 7a 20 61 6e 64 20 6e 20 63 6f 6e 74 61 69 6e   z and n contain
387e3 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
387e4 64 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 62 75  d length of a bu
387e5 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 0a  ffer containing.
387e6 2a 2a 20 61 6e 20 66 74 73 33 20 71 75 65 72 79  ** an fts3 query
387e7 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 72 65 73   expression, res
387e8 70 65 63 74 69 76 65 6c 79 2e 20 54 68 69 73 20  pectively. This 
387e9 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74  function attempt
387ea 73 20 74 6f 20 70 61 72 73 65 20 74 68 65 0a 2a  s to parse the.*
387eb 2a 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  * query expressi
387ec 6f 6e 20 61 6e 64 20 63 72 65 61 74 65 20 61 20  on and create a 
387ed 74 72 65 65 20 6f 66 20 46 74 73 33 45 78 70 72  tree of Fts3Expr
387ee 20 73 74 72 75 63 74 75 72 65 73 20 72 65 70 72   structures repr
387ef 65 73 65 6e 74 69 6e 67 20 74 68 65 0a 2a 2a 20  esenting the.** 
387f0 70 61 72 73 65 64 20 65 78 70 72 65 73 73 69 6f  parsed expressio
387f1 6e 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  n. If successful
387f2 2c 20 2a 70 70 45 78 70 72 20 69 73 20 73 65 74  , *ppExpr is set
387f3 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
387f4 20 68 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20   head.** of the 
387f5 70 61 72 73 65 64 20 65 78 70 72 65 73 73 69 6f  parsed expressio
387f6 6e 20 74 72 65 65 20 61 6e 64 20 53 51 4c 49 54  n tree and SQLIT
387f7 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
387f8 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a  . If an error.**
387f9 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20   occurs, either 
387fa 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 28 6f 75  SQLITE_NOMEM (ou
387fb 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f  t-of-memory erro
387fc 72 29 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52  r) or SQLITE_ERR
387fd 4f 52 20 28 70 61 72 73 65 0a 2a 2a 20 65 72 72  OR (parse.** err
387fe 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65 64 20  or) is returned 
387ff 61 6e 64 20 2a 70 70 45 78 70 72 20 69 73 20 73  and *ppExpr is s
38800 65 74 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49  et to 0..**.** I
38801 66 20 70 61 72 61 6d 65 74 65 72 20 6e 20 69 73  f parameter n is
38802 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62   a negative numb
38803 65 72 2c 20 74 68 65 6e 20 7a 20 69 73 20 61 73  er, then z is as
38804 73 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20 74  sumed to point t
38805 6f 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69  o a.** nul-termi
38806 6e 61 74 65 64 20 73 74 72 69 6e 67 20 61 6e 64  nated string and
38807 20 74 68 65 20 6c 65 6e 67 74 68 20 69 73 20 64   the length is d
38808 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20  etermined using 
38809 73 74 72 6c 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20  strlen()..**.** 
3880a 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
3880b 74 65 72 2c 20 70 54 6f 6b 65 6e 69 7a 65 72 2c  ter, pTokenizer,
3880c 20 69 73 20 70 61 73 73 65 64 20 74 68 65 20 66   is passed the f
3880d 74 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f  ts3 tokenizer mo
3880e 64 75 6c 65 20 74 6f 0a 2a 2a 20 75 73 65 20 74  dule to.** use t
3880f 6f 20 6e 6f 72 6d 61 6c 69 7a 65 20 71 75 65 72  o normalize quer
38810 79 20 74 6f 6b 65 6e 73 20 77 68 69 6c 65 20 70  y tokens while p
38811 61 72 73 69 6e 67 20 74 68 65 20 65 78 70 72 65  arsing the expre
38812 73 73 69 6f 6e 2e 20 54 68 65 20 61 7a 43 6f 6c  ssion. The azCol
38813 5b 5d 0a 2a 2a 20 61 72 72 61 79 2c 20 77 68 69  [].** array, whi
38814 63 68 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  ch is assumed to
38815 20 63 6f 6e 74 61 69 6e 20 6e 43 6f 6c 20 65 6e   contain nCol en
38816 74 72 69 65 73 2c 20 73 68 6f 75 6c 64 20 63 6f  tries, should co
38817 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 0a  ntain the names.
38818 2a 2a 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d  ** of each colum
38819 6e 20 69 6e 20 74 68 65 20 74 61 72 67 65 74 20  n in the target 
3881a 66 74 73 33 20 74 61 62 6c 65 2c 20 69 6e 20 6f  fts3 table, in o
3881b 72 64 65 72 20 66 72 6f 6d 20 6c 65 66 74 20 74  rder from left t
3881c 6f 20 72 69 67 68 74 2e 20 0a 2a 2a 20 43 6f 6c  o right. .** Col
3881d 75 6d 6e 20 6e 61 6d 65 73 20 6d 75 73 74 20 62  umn names must b
3881e 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  e nul-terminated
3881f 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20   strings..**.** 
38820 54 68 65 20 69 44 65 66 61 75 6c 74 43 6f 6c 20  The iDefaultCol 
38821 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64  parameter should
38822 20 62 65 20 70 61 73 73 65 64 20 74 68 65 20 69   be passed the i
38823 6e 64 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c  ndex of the tabl
38824 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 68 61 74  e column.** that
38825 20 61 70 70 65 61 72 73 20 6f 6e 20 74 68 65 20   appears on the 
38826 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f  left-hand-side o
38827 66 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72  f the MATCH oper
38828 61 74 6f 72 20 28 74 68 65 20 64 65 66 61 75 6c  ator (the defaul
38829 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20 6d  t.** column to m
3882a 61 74 63 68 20 61 67 61 69 6e 73 74 20 66 6f 72  atch against for
3882b 20 74 6f 6b 65 6e 73 20 66 6f 72 20 77 68 69 63   tokens for whic
3882c 68 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  h a column name 
3882d 69 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c  is not explicitl
3882e 79 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 61  y.** specified a
3882f 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 71 75  s part of the qu
38830 65 72 79 20 73 74 72 69 6e 67 29 2c 20 6f 72 20  ery string), or 
38831 2d 31 20 69 66 20 74 6f 6b 65 6e 73 20 6d 61 79  -1 if tokens may
38832 20 62 79 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d   by default.** m
38833 61 74 63 68 20 61 6e 79 20 74 61 62 6c 65 20 63  atch any table c
38834 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  olumn..*/.SQLITE
38835 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
38836 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73  ite3Fts3ExprPars
38837 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  e(.  sqlite3_tok
38838 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
38839 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  er,      /* Toke
3883a 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f 0a  nizer module */.
3883b 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 2c 20    char **azCol, 
3883c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3883d 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f        /* Array o
3883e 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66  f column names f
3883f 6f 72 20 66 74 73 33 20 74 61 62 6c 65 20 2a 2f  or fts3 table */
38840 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20 20 20 20  .  int nCol,    
38841 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38842 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
38843 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
38844 7a 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  zCol[] */.  int 
38845 69 44 65 66 61 75 6c 74 43 6f 6c 2c 20 20 20 20  iDefaultCol,    
38846 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38847 2f 2a 20 44 65 66 61 75 6c 74 20 63 6f 6c 75 6d  /* Default colum
38848 6e 20 74 6f 20 71 75 65 72 79 20 2a 2f 0a 20 20  n to query */.  
38849 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
3884a 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20  nt n,           
3884b 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 4d      /* Text of M
3884c 41 54 43 48 20 71 75 65 72 79 20 2a 2f 0a 20 20  ATCH query */.  
3884d 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
3884e 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
3884f 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72 73      /* OUT: Pars
38850 65 64 20 71 75 65 72 79 20 73 74 72 75 63 74 75  ed query structu
38851 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e  re */.){.  int n
38852 50 61 72 73 65 64 3b 0a 20 20 69 6e 74 20 72 63  Parsed;.  int rc
38853 3b 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74  ;.  ParseContext
38854 20 73 50 61 72 73 65 3b 0a 20 20 73 50 61 72 73   sParse;.  sPars
38855 65 2e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70  e.pTokenizer = p
38856 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 50 61  Tokenizer;.  sPa
38857 72 73 65 2e 61 7a 43 6f 6c 20 3d 20 28 63 6f 6e  rse.azCol = (con
38858 73 74 20 63 68 61 72 20 2a 2a 29 61 7a 43 6f 6c  st char **)azCol
38859 3b 0a 20 20 73 50 61 72 73 65 2e 6e 43 6f 6c 20  ;.  sParse.nCol 
3885a 3d 20 6e 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65  = nCol;.  sParse
3885b 2e 69 44 65 66 61 75 6c 74 43 6f 6c 20 3d 20 69  .iDefaultCol = i
3885c 44 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 73 50  DefaultCol;.  sP
3885d 61 72 73 65 2e 6e 4e 65 73 74 20 3d 20 30 3b 0a  arse.nNest = 0;.
3885e 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20    if( z==0 ){.  
3885f 20 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a 20    *ppExpr = 0;. 
38860 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
38861 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  _OK;.  }.  if( n
38862 3c 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74  <0 ){.    n = st
38863 72 6c 65 6e 28 7a 29 3b 0a 20 20 7d 0a 20 20 72  rlen(z);.  }.  r
38864 63 20 3d 20 66 74 73 33 45 78 70 72 50 61 72 73  c = fts3ExprPars
38865 65 28 26 73 50 61 72 73 65 2c 20 7a 2c 20 6e 2c  e(&sParse, z, n,
38866 20 70 70 45 78 70 72 2c 20 26 6e 50 61 72 73 65   ppExpr, &nParse
38867 64 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  d);..  /* Check 
38868 66 6f 72 20 6d 69 73 6d 61 74 63 68 65 64 20 70  for mismatched p
38869 61 72 65 6e 74 68 65 73 69 73 20 2a 2f 0a 20 20  arenthesis */.  
3886a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3886b 4b 20 26 26 20 73 50 61 72 73 65 2e 6e 4e 65 73  K && sParse.nNes
3886c 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  t ){.    rc = SQ
3886d 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
3886e 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46  sqlite3Fts3ExprF
3886f 72 65 65 28 2a 70 70 45 78 70 72 29 3b 0a 20 20  ree(*ppExpr);.  
38870 20 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a 20    *ppExpr = 0;. 
38871 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
38872 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
38873 20 70 61 72 73 65 64 20 66 74 73 33 20 71 75 65   parsed fts3 que
38874 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6c  ry expression al
38875 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74  located by sqlit
38876 65 33 46 74 73 33 45 78 70 72 50 61 72 73 65 28  e3Fts3ExprParse(
38877 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
38878 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
38879 33 46 74 73 33 45 78 70 72 46 72 65 65 28 46 74  3Fts3ExprFree(Ft
3887a 73 33 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66  s3Expr *p){.  if
3887b 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
3887c 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
3887d 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71  ->pLeft);.    sq
3887e 6c 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65  lite3Fts3ExprFre
3887f 65 28 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20  e(p->pRight);.  
38880 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
38881 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  );.  }.}../*****
38882 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38883 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38884 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38885 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38886 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
38887 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38888 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38889 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3888a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3888b 2a 2a 2a 2a 2a 0a 2a 2a 20 45 76 65 72 79 74 68  *****.** Everyth
3888c 69 6e 67 20 61 66 74 65 72 20 74 68 69 73 20 70  ing after this p
3888d 6f 69 6e 74 20 69 73 20 6a 75 73 74 20 74 65 73  oint is just tes
3888e 74 20 63 6f 64 65 2e 0a 2a 2f 0a 0a 23 69 66 64  t code..*/..#ifd
3888f 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 0a  ef SQLITE_TEST..
38890 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20  ./*.** Function 
38891 74 6f 20 71 75 65 72 79 20 74 68 65 20 68 61 73  to query the has
38892 68 2d 74 61 62 6c 65 20 6f 66 20 74 6f 6b 65 6e  h-table of token
38893 69 7a 65 72 73 20 28 73 65 65 20 52 45 41 44 4d  izers (see READM
38894 45 2e 74 6f 6b 65 6e 69 7a 65 72 73 29 2e 0a 2a  E.tokenizers)..*
38895 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 71 75 65  /.static int que
38896 72 79 54 65 73 74 54 6f 6b 65 6e 69 7a 65 72 28  ryTestTokenizer(
38897 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
38898 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
38899 4e 61 6d 65 2c 20 20 0a 20 20 63 6f 6e 73 74 20  Name,  .  const 
3889a 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3889b 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70 0a 29 7b  r_module **pp.){
3889c 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
3889d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
3889e 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  ;.  const char z
3889f 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20  Sql[] = "SELECT 
388a0 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f  fts3_tokenizer(?
388a1 29 22 3b 0a 0a 20 20 2a 70 70 20 3d 20 30 3b 0a  )";..  *pp = 0;.
388a2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70    rc = sqlite3_p
388a3 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53  repare_v2(db, zS
388a4 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20  ql, -1, &pStmt, 
388a5 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  0);.  if( rc!=SQ
388a6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
388a7 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
388a8 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
388a9 78 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61  xt(pStmt, 1, zNa
388aa 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53  me, -1, SQLITE_S
388ab 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20 53 51  TATIC);.  if( SQ
388ac 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65  LITE_ROW==sqlite
388ad 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b  3_step(pStmt) ){
388ae 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
388af 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74  _column_type(pSt
388b0 6d 74 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 42  mt, 0)==SQLITE_B
388b1 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  LOB ){.      mem
388b2 63 70 79 28 70 70 2c 20 73 71 6c 69 74 65 33 5f  cpy(pp, sqlite3_
388b3 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d  column_blob(pStm
388b4 74 2c 20 30 29 2c 20 73 69 7a 65 6f 66 28 2a 70  t, 0), sizeof(*p
388b5 70 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  p));.    }.  }..
388b6 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
388b7 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
388b8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
388b9 66 75 6e 63 74 69 6f 6e 20 69 73 20 70 61 72 74  function is part
388ba 20 6f 66 20 74 68 65 20 74 65 73 74 20 69 6e 74   of the test int
388bb 65 72 66 61 63 65 20 66 6f 72 20 74 68 65 20 71  erface for the q
388bc 75 65 72 79 20 70 61 72 73 65 72 2e 20 49 74 0a  uery parser. It.
388bd 2a 2a 20 77 72 69 74 65 73 20 61 20 74 65 78 74  ** writes a text
388be 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
388bf 6f 66 20 74 68 65 20 71 75 65 72 79 20 65 78 70  of the query exp
388c0 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 6e  ression pExpr in
388c1 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72  to the.** buffer
388c2 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61   pointed to by a
388c3 72 67 75 6d 65 6e 74 20 7a 42 75 66 2e 20 49 74  rgument zBuf. It
388c4 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74   is assumed that
388c5 20 7a 42 75 66 20 69 73 20 6c 61 72 67 65 20 0a   zBuf is large .
388c6 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f  ** enough to sto
388c7 72 65 20 74 68 65 20 72 65 71 75 69 72 65 64 20  re the required 
388c8 74 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74  text representat
388c9 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
388ca 6f 69 64 20 65 78 70 72 54 6f 53 74 72 69 6e 67  oid exprToString
388cb 28 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72  (Fts3Expr *pExpr
388cc 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
388cd 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
388ce 65 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  eType ){.    cas
388cf 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  e FTSQUERY_PHRAS
388d0 45 3a 20 7b 0a 20 20 20 20 20 20 46 74 73 33 50  E: {.      Fts3P
388d1 68 72 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d  hrase *pPhrase =
388d2 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73 65 3b   pExpr->pPhrase;
388d3 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
388d4 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69      zBuf += spri
388d5 6e 74 66 28 7a 42 75 66 2c 20 22 50 48 52 41 53  ntf(zBuf, "PHRAS
388d6 45 20 25 64 20 25 64 22 2c 20 70 50 68 72 61 73  E %d %d", pPhras
388d7 65 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 50 68 72  e->iColumn, pPhr
388d8 61 73 65 2d 3e 69 73 4e 6f 74 29 3b 0a 20 20 20  ase->isNot);.   
388d9 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
388da 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69  hrase->nToken; i
388db 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75  ++){.        zBu
388dc 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75  f += sprintf(zBu
388dd 66 2c 22 20 25 2e 2a 73 22 2c 70 50 68 72 61 73  f," %.*s",pPhras
388de 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 6e 2c 70  e->aToken[i].n,p
388df 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69  Phrase->aToken[i
388e0 5d 2e 7a 29 3b 0a 20 20 20 20 20 20 20 20 7a 42  ].z);.        zB
388e1 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
388e2 75 66 2c 22 25 73 22 2c 20 28 70 50 68 72 61 73  uf,"%s", (pPhras
388e3 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 69 73 50  e->aToken[i].isP
388e4 72 65 66 69 78 3f 22 2b 22 3a 22 22 29 29 3b 0a  refix?"+":""));.
388e5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
388e6 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  turn;.    }..   
388e7 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e   case FTSQUERY_N
388e8 45 41 52 3a 0a 20 20 20 20 20 20 7a 42 75 66 20  EAR:.      zBuf 
388e9 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c  += sprintf(zBuf,
388ea 20 22 4e 45 41 52 2f 25 64 20 22 2c 20 70 45 78   "NEAR/%d ", pEx
388eb 70 72 2d 3e 6e 4e 65 61 72 29 3b 0a 20 20 20 20  pr->nNear);.    
388ec 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
388ed 65 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 3a 0a  e FTSQUERY_NOT:.
388ee 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70        zBuf += sp
388ef 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 4e 4f 54  rintf(zBuf, "NOT
388f0 20 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   ");.      break
388f1 3b 0a 20 20 20 20 63 61 73 65 20 46 54 53 51 55  ;.    case FTSQU
388f2 45 52 59 5f 41 4e 44 3a 0a 20 20 20 20 20 20 7a  ERY_AND:.      z
388f3 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a  Buf += sprintf(z
388f4 42 75 66 2c 20 22 41 4e 44 20 22 29 3b 0a 20 20  Buf, "AND ");.  
388f5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
388f6 61 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52 3a  ase FTSQUERY_OR:
388f7 0a 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73  .      zBuf += s
388f8 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 4f 52  printf(zBuf, "OR
388f9 20 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   ");.      break
388fa 3b 0a 20 20 7d 0a 0a 20 20 7a 42 75 66 20 2b 3d  ;.  }..  zBuf +=
388fb 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
388fc 7b 22 29 3b 0a 20 20 65 78 70 72 54 6f 53 74 72  {");.  exprToStr
388fd 69 6e 67 28 70 45 78 70 72 2d 3e 70 4c 65 66 74  ing(pExpr->pLeft
388fe 2c 20 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20  , zBuf);.  zBuf 
388ff 2b 3d 20 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b  += strlen(zBuf);
38900 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e  .  zBuf += sprin
38901 74 66 28 7a 42 75 66 2c 20 22 7d 20 22 29 3b 0a  tf(zBuf, "} ");.
38902 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e  .  zBuf += sprin
38903 74 66 28 7a 42 75 66 2c 20 22 7b 22 29 3b 0a 20  tf(zBuf, "{");. 
38904 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45   exprToString(pE
38905 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 7a 42 75  xpr->pRight, zBu
38906 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 74  f);.  zBuf += st
38907 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 7a 42  rlen(zBuf);.  zB
38908 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
38909 75 66 2c 20 22 7d 22 29 3b 0a 7d 0a 0a 2f 2a 0a  uf, "}");.}../*.
3890a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69  ** This is the i
3890b 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
3890c 20 61 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75   a scalar SQL fu
3890d 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74  nction used to t
3890e 65 73 74 20 74 68 65 20 0a 2a 2a 20 65 78 70 72  est the .** expr
3890f 65 73 73 69 6f 6e 20 70 61 72 73 65 72 2e 20 49  ession parser. I
38910 74 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c  t should be call
38911 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ed as follows:.*
38912 2a 0a 2a 2a 20 20 20 66 74 73 33 5f 65 78 70 72  *.**   fts3_expr
38913 74 65 73 74 28 3c 74 6f 6b 65 6e 69 7a 65 72 3e  test(<tokenizer>
38914 2c 20 3c 65 78 70 72 3e 2c 20 3c 63 6f 6c 75 6d  , <expr>, <colum
38915 6e 20 31 3e 2c 20 2e 2e 2e 29 3b 0a 2a 2a 0a 2a  n 1>, ...);.**.*
38916 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
38917 6d 65 6e 74 2c 20 3c 74 6f 6b 65 6e 69 7a 65 72  ment, <tokenizer
38918 3e 2c 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  >, is the name o
38919 66 20 74 68 65 20 66 74 73 33 20 74 6f 6b 65 6e  f the fts3 token
3891a 69 7a 65 72 20 75 73 65 64 0a 2a 2a 20 74 6f 20  izer used.** to 
3891b 70 61 72 73 65 20 74 68 65 20 71 75 65 72 79 20  parse the query 
3891c 65 78 70 72 65 73 73 69 6f 6e 20 28 73 65 65 20  expression (see 
3891d 52 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65 72  README.tokenizer
3891e 73 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61  s). The second a
3891f 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68  rgument.** is th
38920 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  e query expressi
38921 6f 6e 20 74 6f 20 70 61 72 73 65 2e 20 45 61 63  on to parse. Eac
38922 68 20 73 75 62 73 65 71 75 65 6e 74 20 61 72 67  h subsequent arg
38923 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 61 6d  ument is the nam
38924 65 0a 2a 2a 20 6f 66 20 61 20 63 6f 6c 75 6d 6e  e.** of a column
38925 20 6f 66 20 74 68 65 20 66 74 73 33 20 74 61 62   of the fts3 tab
38926 6c 65 20 74 68 61 74 20 74 68 65 20 71 75 65 72  le that the quer
38927 79 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79  y expression may
38928 20 72 65 66 65 72 20 74 6f 2e 0a 2a 2a 20 46 6f   refer to..** Fo
38929 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  r example:.**.**
3892a 20 20 20 53 45 4c 45 43 54 20 66 74 73 33 5f 65     SELECT fts3_e
3892b 78 70 72 74 65 73 74 28 27 73 69 6d 70 6c 65 27  xprtest('simple'
3892c 2c 20 27 42 69 6c 6c 20 63 6f 6c 32 3a 42 6c 6f  , 'Bill col2:Blo
3892d 67 67 73 27 2c 20 27 63 6f 6c 31 27 2c 20 27 63  ggs', 'col1', 'c
3892e 6f 6c 32 27 29 3b 0a 2a 2f 0a 73 74 61 74 69 63  ol2');.*/.static
3892f 20 76 6f 69 64 20 66 74 73 33 45 78 70 72 54 65   void fts3ExprTe
38930 73 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  st(.  sqlite3_co
38931 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
38932 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
38933 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
38934 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  gv.){.  sqlite3_
38935 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
38936 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20   const *pModule 
38937 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  = 0;.  sqlite3_t
38938 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
38939 69 7a 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20  izer = 0;.  int 
3893a 72 63 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43  rc;.  char **azC
3893b 6f 6c 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20  ol = 0;.  const 
3893c 63 68 61 72 20 2a 7a 45 78 70 72 3b 0a 20 20 69  char *zExpr;.  i
3893d 6e 74 20 6e 45 78 70 72 3b 0a 20 20 69 6e 74 20  nt nExpr;.  int 
3893e 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  nCol;.  int ii;.
3893f 20 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70    Fts3Expr *pExp
38940 72 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  r;.  sqlite3 *db
38941 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
38942 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
38943 74 65 78 74 29 3b 0a 0a 20 20 69 66 28 20 61 72  text);..  if( ar
38944 67 63 3c 33 20 29 7b 0a 20 20 20 20 73 71 6c 69  gc<3 ){.    sqli
38945 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
38946 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20  (context, .     
38947 20 20 20 22 55 73 61 67 65 3a 20 66 74 73 33 5f     "Usage: fts3_
38948 65 78 70 72 74 65 73 74 28 74 6f 6b 65 6e 69 7a  exprtest(tokeniz
38949 65 72 2c 20 65 78 70 72 2c 20 63 6f 6c 31 2c 20  er, expr, col1, 
3894a 2e 2e 2e 22 2c 20 2d 31 0a 20 20 20 20 29 3b 0a  ...", -1.    );.
3894b 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
3894c 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54 65 73  .  rc = queryTes
3894d 74 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 0a 20  tTokenizer(db,. 
3894e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3894f 20 20 20 20 20 20 20 20 20 28 63 6f 6e 73 74 20           (const 
38950 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
38951 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
38952 5d 29 2c 20 26 70 4d 6f 64 75 6c 65 29 3b 0a 20  ]), &pModule);. 
38953 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
38954 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c  NOMEM ){.    sql
38955 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
38956 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29  r_nomem(context)
38957 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74  ;.    goto exprt
38958 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65  est_out;.  }else
38959 20 69 66 28 20 21 70 4d 6f 64 75 6c 65 20 29 7b   if( !pModule ){
3895a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3895b 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
3895c 74 2c 20 22 4e 6f 20 73 75 63 68 20 74 6f 6b 65  t, "No such toke
3895d 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 22 2c 20 2d  nizer module", -
3895e 31 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70  1);.    goto exp
3895f 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  rtest_out;.  }..
38960 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
38961 78 43 72 65 61 74 65 28 30 2c 20 30 2c 20 26 70  xCreate(0, 0, &p
38962 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 61 73  Tokenizer);.  as
38963 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
38964 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51  _NOMEM || rc==SQ
38965 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28  LITE_OK );.  if(
38966 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
38967 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  M ){.    sqlite3
38968 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
38969 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  mem(context);.  
3896a 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f    goto exprtest_
3896b 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65  out;.  }.  pToke
3896c 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d  nizer->pModule =
3896d 20 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 7a 45 78   pModule;..  zEx
3896e 70 72 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  pr = (const char
3896f 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
38970 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
38971 20 20 6e 45 78 70 72 20 3d 20 73 71 6c 69 74 65    nExpr = sqlite
38972 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
38973 67 76 5b 31 5d 29 3b 0a 20 20 6e 43 6f 6c 20 3d  gv[1]);.  nCol =
38974 20 61 72 67 63 2d 32 3b 0a 20 20 61 7a 43 6f 6c   argc-2;.  azCol
38975 20 3d 20 28 63 68 61 72 20 2a 2a 29 73 71 6c 69   = (char **)sqli
38976 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 6f 6c 2a  te3_malloc(nCol*
38977 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 29 3b  sizeof(char *));
38978 0a 20 20 69 66 28 20 21 61 7a 43 6f 6c 20 29 7b  .  if( !azCol ){
38979 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3897a 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
3897b 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f  context);.    go
3897c 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b  to exprtest_out;
3897d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 69 3d 30 3b  .  }.  for(ii=0;
3897e 20 69 69 3c 6e 43 6f 6c 3b 20 69 69 2b 2b 29 7b   ii<nCol; ii++){
3897f 0a 20 20 20 20 61 7a 43 6f 6c 5b 69 69 5d 20 3d  .    azCol[ii] =
38980 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
38981 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
38982 5b 69 69 2b 32 5d 29 3b 0a 20 20 7d 0a 0a 20 20  [ii+2]);.  }..  
38983 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
38984 45 78 70 72 50 61 72 73 65 28 0a 20 20 20 20 20  ExprParse(.     
38985 20 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 61 7a 43   pTokenizer, azC
38986 6f 6c 2c 20 6e 43 6f 6c 2c 20 6e 43 6f 6c 2c 20  ol, nCol, nCol, 
38987 7a 45 78 70 72 2c 20 6e 45 78 70 72 2c 20 26 70  zExpr, nExpr, &p
38988 45 78 70 72 0a 20 20 29 3b 0a 20 20 69 66 28 20  Expr.  );.  if( 
38989 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
3898a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
3898b 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
3898c 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
3898d 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f   goto exprtest_o
3898e 75 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ut;.  }else if( 
3898f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
38990 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 34  .    char zBuf[4
38991 30 39 36 5d 3b 0a 20 20 20 20 65 78 70 72 54 6f  096];.    exprTo
38992 53 74 72 69 6e 67 28 70 45 78 70 72 2c 20 7a 42  String(pExpr, zB
38993 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  uf);.    sqlite3
38994 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
38995 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20  text, zBuf, -1, 
38996 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
38997 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74  );.    sqlite3Ft
38998 73 33 45 78 70 72 46 72 65 65 28 70 45 78 70 72  s3ExprFree(pExpr
38999 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
3899a 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
3899b 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 45  rror(context, "E
3899c 72 72 6f 72 20 70 61 72 73 69 6e 67 20 65 78 70  rror parsing exp
3899d 72 65 73 73 69 6f 6e 22 2c 20 2d 31 29 3b 0a 20  ression", -1);. 
3899e 20 7d 0a 0a 65 78 70 72 74 65 73 74 5f 6f 75 74   }..exprtest_out
3899f 3a 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 20  :.  if( pModule 
389a0 26 26 20 70 54 6f 6b 65 6e 69 7a 65 72 20 29 7b  && pTokenizer ){
389a1 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  .    rc = pModul
389a2 65 2d 3e 78 44 65 73 74 72 6f 79 28 70 54 6f 6b  e->xDestroy(pTok
389a3 65 6e 69 7a 65 72 29 3b 0a 20 20 7d 0a 20 20 73  enizer);.  }.  s
389a4 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 6f  qlite3_free(azCo
389a5 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  l);.}../*.** Reg
389a6 69 73 74 65 72 20 74 68 65 20 71 75 65 72 79 20  ister the query 
389a7 65 78 70 72 65 73 73 69 6f 6e 20 70 61 72 73 65  expression parse
389a8 72 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20  r test function 
389a9 66 74 73 33 5f 65 78 70 72 74 65 73 74 28 29 20  fts3_exprtest() 
389aa 0a 2a 2a 20 77 69 74 68 20 64 61 74 61 62 61 73  .** with databas
389ab 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e  e connection db.
389ac 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
389ad 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
389ae 46 74 73 33 45 78 70 72 49 6e 69 74 54 65 73 74  Fts3ExprInitTest
389af 49 6e 74 65 72 66 61 63 65 28 73 71 6c 69 74 65  Interface(sqlite
389b0 33 2a 20 64 62 29 7b 0a 20 20 73 71 6c 69 74 65  3* db){.  sqlite
389b1 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
389b2 6e 28 0a 20 20 20 20 20 20 64 62 2c 20 22 66 74  n(.      db, "ft
389b3 73 33 5f 65 78 70 72 74 65 73 74 22 2c 20 2d 31  s3_exprtest", -1
389b4 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
389b5 2c 20 66 74 73 33 45 78 70 72 54 65 73 74 2c 20  , fts3ExprTest, 
389b6 30 2c 20 30 0a 20 20 29 3b 0a 7d 0a 0a 23 65 6e  0, 0.  );.}..#en
389b7 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  dif.#endif /* !d
389b8 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
389b9 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
389ba 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
389bb 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  3) */../********
389bc 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
389bd 73 33 5f 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a  s3_expr.c ******
389be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
389c1 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
389c2 65 20 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a  e fts3_hash.c **
389c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389c5 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
389c6 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a  1 September 22.*
389c7 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
389c8 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
389c9 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
389ca 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
389cb 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
389cc 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
389cd 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
389ce 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
389cf 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
389d0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
389d1 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
389d2 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
389d3 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
389d4 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
389d5 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
389d6 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
389d7 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
389d8 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
389d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
389dd 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70   This is the imp
389de 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67  lementation of g
389df 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c  eneric hash-tabl
389e0 65 73 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  es used in SQLit
389e1 65 2e 0a 2a 2a 20 57 65 27 76 65 20 6d 6f 64 69  e..** We've modi
389e2 66 69 65 64 20 69 74 20 73 6c 69 67 68 74 6c 79  fied it slightly
389e3 20 74 6f 20 73 65 72 76 65 20 61 73 20 61 20 73   to serve as a s
389e4 74 61 6e 64 61 6c 6f 6e 65 20 68 61 73 68 20 74  tandalone hash t
389e5 61 62 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  able.** implemen
389e6 74 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66  tation for the f
389e7 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 69 6e  ull-text indexin
389e8 67 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 0a 2f 2a  g module..*/../*
389e9 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
389ea 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
389eb 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a  y compiled if:.*
389ec 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
389ed 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
389ee 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20  ing built as an 
389ef 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20  extension.**    
389f0 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73     (in which cas
389f1 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73  e SQLITE_CORE is
389f2 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f   not defined), o
389f3 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  r.**.**     * Th
389f4 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
389f5 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74   being built int
389f6 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a  o the core of.**
389f7 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69         SQLite (i
389f8 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
389f9 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20  ITE_ENABLE_FTS3 
389fa 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a  is defined)..*/.
389fb 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
389fc 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
389fd 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
389fe 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 2f 2a 0a 2a  LE_FTS3)..../*.*
389ff 2a 20 4d 61 6c 6c 6f 63 20 61 6e 64 20 46 72 65  * Malloc and Fre
38a00 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73  e functions.*/.s
38a01 74 61 74 69 63 20 76 6f 69 64 20 2a 66 74 73 33  tatic void *fts3
38a02 48 61 73 68 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e  HashMalloc(int n
38a03 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73  ){.  void *p = s
38a04 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 29  qlite3_malloc(n)
38a05 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
38a06 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29   memset(p, 0, n)
38a07 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
38a08 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
38a09 66 74 73 33 48 61 73 68 46 72 65 65 28 76 6f 69  fts3HashFree(voi
38a0a 64 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  d *p){.  sqlite3
38a0b 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 20  _free(p);.}../* 
38a0c 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79  Turn bulk memory
38a0d 20 69 6e 74 6f 20 61 20 68 61 73 68 20 74 61 62   into a hash tab
38a0e 6c 65 20 6f 62 6a 65 63 74 20 62 79 20 69 6e 69  le object by ini
38a0f 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a  tializing the.**
38a10 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 48   fields of the H
38a11 61 73 68 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ash structure..*
38a12 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 69 73 20 61  *.** "pNew" is a
38a13 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
38a14 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20  hash table that 
38a15 69 73 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c  is to be initial
38a16 69 7a 65 64 2e 0a 2a 2a 20 6b 65 79 43 6c 61 73  ized..** keyClas
38a17 73 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20  s is one of the 
38a18 63 6f 6e 73 74 61 6e 74 73 20 0a 2a 2a 20 46 54  constants .** FT
38a19 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 6f  S3_HASH_BINARY o
38a1a 72 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49  r FTS3_HASH_STRI
38a1b 4e 47 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f  NG.  The value o
38a1c 66 20 6b 65 79 43 6c 61 73 73 20 0a 2a 2a 20 64  f keyClass .** d
38a1d 65 74 65 72 6d 69 6e 65 73 20 77 68 61 74 20 6b  etermines what k
38a1e 69 6e 64 20 6f 66 20 6b 65 79 20 74 68 65 20 68  ind of key the h
38a1f 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c 20 75  ash table will u
38a20 73 65 2e 20 20 22 63 6f 70 79 4b 65 79 22 20 69  se.  "copyKey" i
38a21 73 0a 2a 2a 20 74 72 75 65 20 69 66 20 74 68 65  s.** true if the
38a22 20 68 61 73 68 20 74 61 62 6c 65 20 73 68 6f 75   hash table shou
38a23 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20  ld make its own 
38a24 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20  private copy of 
38a25 6b 65 79 73 20 61 6e 64 0a 2a 2a 20 66 61 6c 73  keys and.** fals
38a26 65 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6a  e if it should j
38a27 75 73 74 20 75 73 65 20 74 68 65 20 73 75 70 70  ust use the supp
38a28 6c 69 65 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  lied pointer..*/
38a29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
38a2a 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
38a2b 48 61 73 68 49 6e 69 74 28 66 74 73 33 48 61 73  HashInit(fts3Has
38a2c 68 20 2a 70 4e 65 77 2c 20 69 6e 74 20 6b 65 79  h *pNew, int key
38a2d 43 6c 61 73 73 2c 20 69 6e 74 20 63 6f 70 79 4b  Class, int copyK
38a2e 65 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  ey){.  assert( p
38a2f 4e 65 77 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  New!=0 );.  asse
38a30 72 74 28 20 6b 65 79 43 6c 61 73 73 3e 3d 46 54  rt( keyClass>=FT
38a31 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 26  S3_HASH_STRING &
38a32 26 20 6b 65 79 43 6c 61 73 73 3c 3d 46 54 53 33  & keyClass<=FTS3
38a33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a  _HASH_BINARY );.
38a34 20 20 70 4e 65 77 2d 3e 6b 65 79 43 6c 61 73 73    pNew->keyClass
38a35 20 3d 20 6b 65 79 43 6c 61 73 73 3b 0a 20 20 70   = keyClass;.  p
38a36 4e 65 77 2d 3e 63 6f 70 79 4b 65 79 20 3d 20 63  New->copyKey = c
38a37 6f 70 79 4b 65 79 3b 0a 20 20 70 4e 65 77 2d 3e  opyKey;.  pNew->
38a38 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65  first = 0;.  pNe
38a39 77 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20  w->count = 0;.  
38a3a 70 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30  pNew->htsize = 0
38a3b 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30  ;.  pNew->ht = 0
38a3c 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61  ;.}../* Remove a
38a3d 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20  ll entries from 
38a3e 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52  a hash table.  R
38a3f 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72  eclaim all memor
38a40 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20  y..** Call this 
38a41 72 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74  routine to delet
38a42 65 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f  e a hash table o
38a43 72 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73  r to reset a has
38a44 68 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68  h table.** to th
38a45 65 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a  e empty state..*
38a46 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
38a47 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
38a48 33 48 61 73 68 43 6c 65 61 72 28 66 74 73 33 48  3HashClear(fts3H
38a49 61 73 68 20 2a 70 48 29 7b 0a 20 20 66 74 73 33  ash *pH){.  fts3
38a4a 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20  HashElem *elem; 
38a4b 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
38a4c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20  ooping over all 
38a4d 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
38a4e 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65  table */..  asse
38a4f 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65  rt( pH!=0 );.  e
38a50 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b  lem = pH->first;
38a51 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30  .  pH->first = 0
38a52 3b 0a 20 20 66 74 73 33 48 61 73 68 46 72 65 65  ;.  fts3HashFree
38a53 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e  (pH->ht);.  pH->
38a54 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74  ht = 0;.  pH->ht
38a55 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c  size = 0;.  whil
38a56 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 66  e( elem ){.    f
38a57 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78  ts3HashElem *nex
38a58 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e  t_elem = elem->n
38a59 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d  ext;.    if( pH-
38a5a 3e 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d  >copyKey && elem
38a5b 2d 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  ->pKey ){.      
38a5c 66 74 73 33 48 61 73 68 46 72 65 65 28 65 6c 65  fts3HashFree(ele
38a5d 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 7d 0a  m->pKey);.    }.
38a5e 20 20 20 20 66 74 73 33 48 61 73 68 46 72 65 65      fts3HashFree
38a5f 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d  (elem);.    elem
38a60 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20   = next_elem;.  
38a61 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20  }.  pH->count = 
38a62 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68  0;.}../*.** Hash
38a63 20 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f 6e 20   and comparison 
38a64 66 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e 20 74  functions when t
38a65 68 65 20 6d 6f 64 65 20 69 73 20 46 54 53 33 5f  he mode is FTS3_
38a66 48 41 53 48 5f 53 54 52 49 4e 47 0a 2a 2f 0a 73  HASH_STRING.*/.s
38a67 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 74  tatic int fts3St
38a68 72 48 61 73 68 28 63 6f 6e 73 74 20 76 6f 69 64  rHash(const void
38a69 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
38a6a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
38a6b 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  *z = (const char
38a6c 20 2a 29 70 4b 65 79 3b 0a 20 20 69 6e 74 20 68   *)pKey;.  int h
38a6d 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 4b 65 79   = 0;.  if( nKey
38a6e 3c 3d 30 20 29 20 6e 4b 65 79 20 3d 20 28 69 6e  <=0 ) nKey = (in
38a6f 74 29 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20  t) strlen(z);.  
38a70 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20  while( nKey > 0 
38a71 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c   ){.    h = (h<<
38a72 33 29 20 5e 20 68 20 5e 20 2a 7a 2b 2b 3b 0a 20  3) ^ h ^ *z++;. 
38a73 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20     nKey--;.  }. 
38a74 20 72 65 74 75 72 6e 20 68 20 26 20 30 78 37 66   return h & 0x7f
38a75 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74 69 63  ffffff;.}.static
38a76 20 69 6e 74 20 66 74 73 33 53 74 72 43 6f 6d 70   int fts3StrComp
38a77 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  are(const void *
38a78 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63  pKey1, int n1, c
38a79 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
38a7a 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28  , int n2){.  if(
38a7b 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 72 6e   n1!=n2 ) return
38a7c 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72   1;.  return str
38a7d 6e 63 6d 70 28 28 63 6f 6e 73 74 20 63 68 61 72  ncmp((const char
38a7e 2a 29 70 4b 65 79 31 2c 28 63 6f 6e 73 74 20 63  *)pKey1,(const c
38a7f 68 61 72 2a 29 70 4b 65 79 32 2c 6e 31 29 3b 0a  har*)pKey2,n1);.
38a80 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e  }../*.** Hash an
38a81 64 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  d comparison fun
38a82 63 74 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 20  ctions when the 
38a83 6d 6f 64 65 20 69 73 20 46 54 53 33 5f 48 41 53  mode is FTS3_HAS
38a84 48 5f 42 49 4e 41 52 59 0a 2a 2f 0a 73 74 61 74  H_BINARY.*/.stat
38a85 69 63 20 69 6e 74 20 66 74 73 33 42 69 6e 48 61  ic int fts3BinHa
38a86 73 68 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  sh(const void *p
38a87 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a  Key, int nKey){.
38a88 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 63    int h = 0;.  c
38a89 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28  onst char *z = (
38a8a 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65  const char *)pKe
38a8b 79 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79  y;.  while( nKey
38a8c 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 68 20  -- > 0 ){.    h 
38a8d 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 2a  = (h<<3) ^ h ^ *
38a8e 28 7a 2b 2b 29 3b 0a 20 20 7d 0a 20 20 72 65 74  (z++);.  }.  ret
38a8f 75 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66  urn h & 0x7fffff
38a90 66 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  ff;.}.static int
38a91 20 66 74 73 33 42 69 6e 43 6f 6d 70 61 72 65 28   fts3BinCompare(
38a92 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
38a93 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74  1, int n1, const
38a94 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e   void *pKey2, in
38a95 74 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21  t n2){.  if( n1!
38a96 3d 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  =n2 ) return 1;.
38a97 20 20 72 65 74 75 72 6e 20 6d 65 6d 63 6d 70 28    return memcmp(
38a98 70 4b 65 79 31 2c 70 4b 65 79 32 2c 6e 31 29 3b  pKey1,pKey2,n1);
38a99 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
38a9a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
38a9b 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 68 61  e appropriate ha
38a9c 73 68 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65  sh function give
38a9d 6e 20 74 68 65 20 6b 65 79 20 63 6c 61 73 73 2e  n the key class.
38a9e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 73 79 6e  .**.** The C syn
38a9f 74 61 78 20 69 6e 20 74 68 69 73 20 66 75 6e 63  tax in this func
38aa0 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20  tion definition 
38aa1 6d 61 79 20 62 65 20 75 6e 66 61 6d 69 6c 61 72  may be unfamilar
38aa2 20 74 6f 20 73 6f 6d 65 20 0a 2a 2a 20 70 72 6f   to some .** pro
38aa3 67 72 61 6d 6d 65 72 73 2c 20 73 6f 20 77 65 20  grammers, so we 
38aa4 70 72 6f 76 69 64 65 20 74 68 65 20 66 6f 6c 6c  provide the foll
38aa5 6f 77 69 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c  owing additional
38aa6 20 65 78 70 6c 61 6e 61 74 69 6f 6e 3a 0a 2a 2a   explanation:.**
38aa7 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  .** The name of 
38aa8 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
38aa9 22 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e  "ftsHashFunction
38aaa 22 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e  ".  The function
38aab 20 74 61 6b 65 73 20 61 0a 2a 2a 20 73 69 6e 67   takes a.** sing
38aac 6c 65 20 70 61 72 61 6d 65 74 65 72 20 22 6b 65  le parameter "ke
38aad 79 43 6c 61 73 73 22 2e 20 20 54 68 65 20 72 65  yClass".  The re
38aae 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 74  turn value of ft
38aaf 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 29 0a  sHashFunction().
38ab0 2a 2a 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ** is a pointer 
38ab1 74 6f 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74  to another funct
38ab2 69 6f 6e 2e 20 20 53 70 65 63 69 66 69 63 61 6c  ion.  Specifical
38ab3 6c 79 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76  ly, the return v
38ab4 61 6c 75 65 0a 2a 2a 20 6f 66 20 66 74 73 48 61  alue.** of ftsHa
38ab5 73 68 46 75 6e 63 74 69 6f 6e 28 29 20 69 73 20  shFunction() is 
38ab6 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
38ab7 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 74 61 6b  unction that tak
38ab8 65 73 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72  es two parameter
38ab9 73 0a 2a 2a 20 77 69 74 68 20 74 79 70 65 73 20  s.** with types 
38aba 22 63 6f 6e 73 74 20 76 6f 69 64 2a 22 20 61 6e  "const void*" an
38abb 64 20 22 69 6e 74 22 20 61 6e 64 20 72 65 74 75  d "int" and retu
38abc 72 6e 73 20 61 6e 20 22 69 6e 74 22 2e 0a 2a 2f  rns an "int"..*/
38abd 0a 73 74 61 74 69 63 20 69 6e 74 20 28 2a 66 74  .static int (*ft
38abe 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 69 6e  sHashFunction(in
38abf 74 20 6b 65 79 43 6c 61 73 73 29 29 28 63 6f 6e  t keyClass))(con
38ac0 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20  st void*,int){. 
38ac1 20 69 66 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46   if( keyClass==F
38ac2 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20  TS3_HASH_STRING 
38ac3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 66  ){.    return &f
38ac4 74 73 33 53 74 72 48 61 73 68 3b 0a 20 20 7d 65  ts3StrHash;.  }e
38ac5 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
38ac6 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f   keyClass==FTS3_
38ac7 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20  HASH_BINARY );. 
38ac8 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 42     return &fts3B
38ac9 69 6e 48 61 73 68 3b 0a 20 20 7d 0a 7d 0a 0a 2f  inHash;.  }.}../
38aca 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
38acb 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70  inter to the app
38acc 72 6f 70 72 69 61 74 65 20 68 61 73 68 20 66 75  ropriate hash fu
38acd 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65  nction given the
38ace 20 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a   key class..**.*
38acf 2a 20 46 6f 72 20 68 65 6c 70 20 69 6e 20 69 6e  * For help in in
38ad0 74 65 72 70 72 65 74 65 64 20 74 68 65 20 6f 62  terpreted the ob
38ad1 73 63 75 72 65 20 43 20 63 6f 64 65 20 69 6e 20  scure C code in 
38ad2 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  the function def
38ad3 69 6e 69 74 69 6f 6e 2c 0a 2a 2a 20 73 65 65 20  inition,.** see 
38ad4 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65  the header comme
38ad5 6e 74 20 6f 6e 20 74 68 65 20 70 72 65 76 69 6f  nt on the previo
38ad6 75 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  us function..*/.
38ad7 73 74 61 74 69 63 20 69 6e 74 20 28 2a 66 74 73  static int (*fts
38ad8 43 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f 6e 28  CompareFunction(
38ad9 69 6e 74 20 6b 65 79 43 6c 61 73 73 29 29 28 63  int keyClass))(c
38ada 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
38adb 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 7b  onst void*,int){
38adc 0a 20 20 69 66 28 20 6b 65 79 43 6c 61 73 73 3d  .  if( keyClass=
38add 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e  =FTS3_HASH_STRIN
38ade 47 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  G ){.    return 
38adf 26 66 74 73 33 53 74 72 43 6f 6d 70 61 72 65 3b  &fts3StrCompare;
38ae0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
38ae1 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3d 3d  sert( keyClass==
38ae2 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59  FTS3_HASH_BINARY
38ae3 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 26   );.    return &
38ae4 66 74 73 33 42 69 6e 43 6f 6d 70 61 72 65 3b 0a  fts3BinCompare;.
38ae5 20 20 7d 0a 7d 0a 0a 2f 2a 20 4c 69 6e 6b 20 61    }.}../* Link a
38ae6 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74  n element into t
38ae7 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f  he hash table.*/
38ae8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
38ae9 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65 6d 65  3HashInsertEleme
38aea 6e 74 28 0a 20 20 66 74 73 33 48 61 73 68 20 2a  nt(.  fts3Hash *
38aeb 70 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  pH,            /
38aec 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68  * The complete h
38aed 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73  ash table */.  s
38aee 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 70  truct _fts3ht *p
38aef 45 6e 74 72 79 2c 20 20 2f 2a 20 54 68 65 20 65  Entry,  /* The e
38af0 6e 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20  ntry into which 
38af1 70 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64  pNew is inserted
38af2 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c   */.  fts3HashEl
38af3 65 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 20 2f  em *pNew       /
38af4 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f  * The element to
38af5 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
38af6 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  ){.  fts3HashEle
38af7 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 20 2f 2a  m *pHead;     /*
38af8 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 61   First element a
38af9 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74 72 79  lready in pEntry
38afa 20 2a 2f 0a 20 20 70 48 65 61 64 20 3d 20 70 45   */.  pHead = pE
38afb 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 69  ntry->chain;.  i
38afc 66 28 20 70 48 65 61 64 20 29 7b 0a 20 20 20 20  f( pHead ){.    
38afd 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 65  pNew->next = pHe
38afe 61 64 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72  ad;.    pNew->pr
38aff 65 76 20 3d 20 70 48 65 61 64 2d 3e 70 72 65 76  ev = pHead->prev
38b00 3b 0a 20 20 20 20 69 66 28 20 70 48 65 61 64 2d  ;.    if( pHead-
38b01 3e 70 72 65 76 20 29 7b 20 70 48 65 61 64 2d 3e  >prev ){ pHead->
38b02 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 70 4e 65  prev->next = pNe
38b03 77 3b 20 7d 0a 20 20 20 20 65 6c 73 65 20 20 20  w; }.    else   
38b04 20 20 20 20 20 20 20 20 20 20 7b 20 70 48 2d 3e            { pH->
38b05 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 20 7d 0a  first = pNew; }.
38b06 20 20 20 20 70 48 65 61 64 2d 3e 70 72 65 76 20      pHead->prev 
38b07 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b  = pNew;.  }else{
38b08 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20  .    pNew->next 
38b09 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20  = pH->first;.   
38b0a 20 69 66 28 20 70 48 2d 3e 66 69 72 73 74 20 29   if( pH->first )
38b0b 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e 70 72 65  { pH->first->pre
38b0c 76 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20  v = pNew; }.    
38b0d 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 30 3b 0a  pNew->prev = 0;.
38b0e 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20      pH->first = 
38b0f 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 45 6e 74  pNew;.  }.  pEnt
38b10 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 70  ry->count++;.  p
38b11 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70  Entry->chain = p
38b12 4e 65 77 3b 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69  New;.}.../* Resi
38b13 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  ze the hash tabl
38b14 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e  e so that it can
38b15 74 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22  tains "new_size"
38b16 20 62 75 63 6b 65 74 73 2e 0a 2a 2a 20 22 6e 65   buckets..** "ne
38b17 77 5f 73 69 7a 65 22 20 6d 75 73 74 20 62 65 20  w_size" must be 
38b18 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 20 54  a power of 2.  T
38b19 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69  he hash table mi
38b1a 67 68 74 20 66 61 69 6c 20 0a 2a 2a 20 74 6f 20  ght fail .** to 
38b1b 72 65 73 69 7a 65 20 69 66 20 73 71 6c 69 74 65  resize if sqlite
38b1c 4d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a  Malloc() fails..
38b1d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
38b1e 74 73 33 52 65 68 61 73 68 28 66 74 73 33 48 61  ts3Rehash(fts3Ha
38b1f 73 68 20 2a 70 48 2c 20 69 6e 74 20 6e 65 77 5f  sh *pH, int new_
38b20 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 74 20  size){.  struct 
38b21 5f 66 74 73 33 68 74 20 2a 6e 65 77 5f 68 74 3b  _fts3ht *new_ht;
38b22 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
38b23 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20   new hash table 
38b24 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  */.  fts3HashEle
38b25 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65  m *elem, *next_e
38b26 6c 65 6d 3b 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f  lem;  /* For loo
38b27 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69  ping over existi
38b28 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20  ng elements */. 
38b29 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f   int (*xHash)(co
38b2a 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  nst void*,int); 
38b2b 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75    /* The hash fu
38b2c 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73  nction */..  ass
38b2d 65 72 74 28 20 28 6e 65 77 5f 73 69 7a 65 20 26  ert( (new_size &
38b2e 20 28 6e 65 77 5f 73 69 7a 65 2d 31 29 29 3d 3d   (new_size-1))==
38b2f 30 20 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20  0 );.  new_ht = 
38b30 28 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20  (struct _fts3ht 
38b31 2a 29 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63  *)fts3HashMalloc
38b32 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f  ( new_size*sizeo
38b33 66 28 73 74 72 75 63 74 20 5f 66 74 73 33 68 74  f(struct _fts3ht
38b34 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 68  ) );.  if( new_h
38b35 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  t==0 ) return;. 
38b36 20 66 74 73 33 48 61 73 68 46 72 65 65 28 70 48   fts3HashFree(pH
38b37 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20  ->ht);.  pH->ht 
38b38 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e  = new_ht;.  pH->
38b39 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a  htsize = new_siz
38b3a 65 3b 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73  e;.  xHash = fts
38b3b 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d  HashFunction(pH-
38b3c 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 66 6f  >keyClass);.  fo
38b3d 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74  r(elem=pH->first
38b3e 2c 20 70 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65  , pH->first=0; e
38b3f 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74  lem; elem = next
38b40 5f 65 6c 65 6d 29 7b 0a 20 20 20 20 69 6e 74 20  _elem){.    int 
38b41 68 20 3d 20 28 2a 78 48 61 73 68 29 28 65 6c 65  h = (*xHash)(ele
38b42 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e  m->pKey, elem->n
38b43 4b 65 79 29 20 26 20 28 6e 65 77 5f 73 69 7a 65  Key) & (new_size
38b44 2d 31 29 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c  -1);.    next_el
38b45 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  em = elem->next;
38b46 0a 20 20 20 20 66 74 73 33 48 61 73 68 49 6e 73  .    fts3HashIns
38b47 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26  ertElement(pH, &
38b48 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d 29  new_ht[h], elem)
38b49 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 54 68 69 73  ;.  }.}../* This
38b4a 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69   function (for i
38b4b 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79  nternal use only
38b4c 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65  ) locates an ele
38b4d 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61  ment in an.** ha
38b4e 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61  sh table that ma
38b4f 74 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20  tches the given 
38b50 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66  key.  The hash f
38b51 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a  or this key has.
38b52 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ** already been 
38b53 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20  computed and is 
38b54 70 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74  passed as the 4t
38b55 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a  h parameter..*/.
38b56 73 74 61 74 69 63 20 66 74 73 33 48 61 73 68 45  static fts3HashE
38b57 6c 65 6d 20 2a 66 74 73 33 46 69 6e 64 45 6c 65  lem *fts3FindEle
38b58 6d 65 6e 74 42 79 48 61 73 68 28 0a 20 20 63 6f  mentByHash(.  co
38b59 6e 73 74 20 66 74 73 33 48 61 73 68 20 2a 70 48  nst fts3Hash *pH
38b5a 2c 20 2f 2a 20 54 68 65 20 70 48 20 74 6f 20 62  , /* The pH to b
38b5b 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20  e searched */.  
38b5c 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
38b5d 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 77  ,   /* The key w
38b5e 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 20  e are searching 
38b5f 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65  for */.  int nKe
38b60 79 2c 0a 20 20 69 6e 74 20 68 20 20 20 20 20 20  y,.  int h      
38b61 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
38b62 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65  hash for this ke
38b63 79 2e 20 2a 2f 0a 29 7b 0a 20 20 66 74 73 33 48  y. */.){.  fts3H
38b64 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
38b65 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
38b66 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74  d to loop thru t
38b67 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20  he element list 
38b68 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20  */.  int count; 
38b69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38b6a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
38b6b 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74 20 74   elements left t
38b6c 6f 20 74 65 73 74 20 2a 2f 0a 20 20 69 6e 74 20  o test */.  int 
38b6d 28 2a 78 43 6f 6d 70 61 72 65 29 28 63 6f 6e 73  (*xCompare)(cons
38b6e 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
38b6f 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f  t void*,int);  /
38b70 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  * comparison fun
38b71 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20  ction */..  if( 
38b72 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 73 74  pH->ht ){.    st
38b73 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 70 45  ruct _fts3ht *pE
38b74 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68  ntry = &pH->ht[h
38b75 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 45  ];.    elem = pE
38b76 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 20  ntry->chain;.   
38b77 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79 2d   count = pEntry-
38b78 3e 63 6f 75 6e 74 3b 0a 20 20 20 20 78 43 6f 6d  >count;.    xCom
38b79 70 61 72 65 20 3d 20 66 74 73 43 6f 6d 70 61 72  pare = ftsCompar
38b7a 65 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65  eFunction(pH->ke
38b7b 79 43 6c 61 73 73 29 3b 0a 20 20 20 20 77 68 69  yClass);.    whi
38b7c 6c 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 65  le( count-- && e
38b7d 6c 65 6d 20 29 7b 0a 20 20 20 20 20 20 69 66 28  lem ){.      if(
38b7e 20 28 2a 78 43 6f 6d 70 61 72 65 29 28 65 6c 65   (*xCompare)(ele
38b7f 6d 2d 3e 70 4b 65 79 2c 65 6c 65 6d 2d 3e 6e 4b  m->pKey,elem->nK
38b80 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30  ey,pKey,nKey)==0
38b81 20 29 7b 20 0a 20 20 20 20 20 20 20 20 72 65 74   ){ .        ret
38b82 75 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 20 20  urn elem;.      
38b83 7d 0a 20 20 20 20 20 20 65 6c 65 6d 20 3d 20 65  }.      elem = e
38b84 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d  lem->next;.    }
38b85 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
38b86 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20  .}../* Remove a 
38b87 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 6f  single entry fro
38b88 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
38b89 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72   given a pointer
38b8a 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d   to that.** elem
38b8b 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 6f  ent and a hash o
38b8c 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 20  n the element's 
38b8d 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  key..*/.static v
38b8e 6f 69 64 20 66 74 73 33 52 65 6d 6f 76 65 45 6c  oid fts3RemoveEl
38b8f 65 6d 65 6e 74 42 79 48 61 73 68 28 0a 20 20 66  ementByHash(.  f
38b90 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20 20 20  ts3Hash *pH,    
38b91 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63       /* The pH c
38b92 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22  ontaining "elem"
38b93 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c   */.  fts3HashEl
38b94 65 6d 2a 20 65 6c 65 6d 2c 20 20 20 2f 2a 20 54  em* elem,   /* T
38b95 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65  he element to be
38b96 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
38b97 65 20 70 48 20 2a 2f 0a 20 20 69 6e 74 20 68 20  e pH */.  int h 
38b98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38b99 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20 66 6f  /* Hash value fo
38b9a 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 2a 2f  r the element */
38b9b 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f 66 74  .){.  struct _ft
38b9c 73 33 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20  s3ht *pEntry;.  
38b9d 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29  if( elem->prev )
38b9e 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76  {.    elem->prev
38b9f 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e  ->next = elem->n
38ba0 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20  ext; .  }else{. 
38ba1 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65     pH->first = e
38ba2 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20  lem->next;.  }. 
38ba3 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20   if( elem->next 
38ba4 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78  ){.    elem->nex
38ba5 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e  t->prev = elem->
38ba6 70 72 65 76 3b 0a 20 20 7d 0a 20 20 70 45 6e 74  prev;.  }.  pEnt
38ba7 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b  ry = &pH->ht[h];
38ba8 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63  .  if( pEntry->c
38ba9 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20  hain==elem ){.  
38baa 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20    pEntry->chain 
38bab 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20  = elem->next;.  
38bac 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  }.  pEntry->coun
38bad 74 2d 2d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72  t--;.  if( pEntr
38bae 79 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20  y->count<=0 ){. 
38baf 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e     pEntry->chain
38bb0 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
38bb1 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20 65  pH->copyKey && e
38bb2 6c 65 6d 2d 3e 70 4b 65 79 20 29 7b 0a 20 20 20  lem->pKey ){.   
38bb3 20 66 74 73 33 48 61 73 68 46 72 65 65 28 65 6c   fts3HashFree(el
38bb4 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 7d 0a 20  em->pKey);.  }. 
38bb5 20 66 74 73 33 48 61 73 68 46 72 65 65 28 20 65   fts3HashFree( e
38bb6 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75  lem );.  pH->cou
38bb7 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e  nt--;.  if( pH->
38bb8 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20  count<=0 ){.    
38bb9 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69 72 73  assert( pH->firs
38bba 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  t==0 );.    asse
38bbb 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30  rt( pH->count==0
38bbc 20 29 3b 0a 20 20 20 20 66 74 73 33 48 61 73 68   );.    fts3Hash
38bbd 43 6c 65 61 72 28 70 48 29 3b 0a 20 20 7d 0a 7d  Clear(pH);.  }.}
38bbe 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  ../* Attempt to 
38bbf 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e  locate an elemen
38bc0 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61  t of the hash ta
38bc1 62 6c 65 20 70 48 20 77 69 74 68 20 61 20 6b 65  ble pH with a ke
38bc2 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65  y.** that matche
38bc3 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65  s pKey,nKey.  Re
38bc4 74 75 72 6e 20 74 68 65 20 64 61 74 61 20 66 6f  turn the data fo
38bc5 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69  r this element i
38bc6 66 20 69 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64  f it is.** found
38bc7 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65  , or NULL if the
38bc8 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a  re is no match..
38bc9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
38bca 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 46  E void *sqlite3F
38bcb 74 73 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73  ts3HashFind(cons
38bcc 74 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20  t fts3Hash *pH, 
38bcd 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
38bce 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69  , int nKey){.  i
38bcf 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20  nt h;           
38bd0 20 20 20 20 20 20 2f 2a 20 41 20 68 61 73 68 20        /* A hash 
38bd1 6f 6e 20 6b 65 79 20 2a 2f 0a 20 20 66 74 73 33  on key */.  fts3
38bd2 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20  HashElem *elem; 
38bd3 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e     /* The elemen
38bd4 74 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b  t that matches k
38bd5 65 79 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48  ey */.  int (*xH
38bd6 61 73 68 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  ash)(const void*
38bd7 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 68  ,int);  /* The h
38bd8 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ash function */.
38bd9 0a 20 20 69 66 28 20 70 48 3d 3d 30 20 7c 7c 20  .  if( pH==0 || 
38bda 70 48 2d 3e 68 74 3d 3d 30 20 29 20 72 65 74 75  pH->ht==0 ) retu
38bdb 72 6e 20 30 3b 0a 20 20 78 48 61 73 68 20 3d 20  rn 0;.  xHash = 
38bdc 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28  ftsHashFunction(
38bdd 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20  pH->keyClass);. 
38bde 20 61 73 73 65 72 74 28 20 78 48 61 73 68 21 3d   assert( xHash!=
38bdf 30 20 29 3b 0a 20 20 68 20 3d 20 28 2a 78 48 61  0 );.  h = (*xHa
38be0 73 68 29 28 70 4b 65 79 2c 6e 4b 65 79 29 3b 0a  sh)(pKey,nKey);.
38be1 20 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e 68    assert( (pH->h
38be2 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74 73  tsize & (pH->hts
38be3 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  ize-1))==0 );.  
38be4 65 6c 65 6d 20 3d 20 66 74 73 33 46 69 6e 64 45  elem = fts3FindE
38be5 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 70 48 2c  lementByHash(pH,
38be6 70 4b 65 79 2c 6e 4b 65 79 2c 20 68 20 26 20 28  pKey,nKey, h & (
38be7 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3b 0a  pH->htsize-1));.
38be8 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20    return elem ? 
38be9 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a  elem->data : 0;.
38bea 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20  }../* Insert an 
38beb 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65  element into the
38bec 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20   hash table pH. 
38bed 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79   The key is pKey
38bee 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65  ,nKey.** and the
38bef 20 64 61 74 61 20 69 73 20 22 64 61 74 61 22 2e   data is "data".
38bf0 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65  .**.** If no ele
38bf1 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68  ment exists with
38bf2 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c   a matching key,
38bf3 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65   then a new.** e
38bf4 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65  lement is create
38bf5 64 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74 68  d.  A copy of th
38bf6 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69 66  e key is made if
38bf7 20 74 68 65 20 63 6f 70 79 4b 65 79 0a 2a 2a 20   the copyKey.** 
38bf8 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e 55  flag is set.  NU
38bf9 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  LL is returned..
38bfa 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72  **.** If another
38bfb 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79   element already
38bfc 20 65 78 69 73 74 73 20 77 69 74 68 20 74 68 65   exists with the
38bfd 20 73 61 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20   same key, then 
38bfe 74 68 65 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20  the.** new data 
38bff 72 65 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64  replaces the old
38c00 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 6f 6c   data and the ol
38c01 64 20 64 61 74 61 20 69 73 20 72 65 74 75 72 6e  d data is return
38c02 65 64 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69  ed..** The key i
38c03 73 20 6e 6f 74 20 63 6f 70 69 65 64 20 69 6e 20  s not copied in 
38c04 74 68 69 73 20 69 6e 73 74 61 6e 63 65 2e 20 20  this instance.  
38c05 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  If a malloc fail
38c06 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e  s, then.** the n
38c07 65 77 20 64 61 74 61 20 69 73 20 72 65 74 75 72  ew data is retur
38c08 6e 65 64 20 61 6e 64 20 74 68 65 20 68 61 73 68  ned and the hash
38c09 20 74 61 62 6c 65 20 69 73 20 75 6e 63 68 61 6e   table is unchan
38c0a 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ged..**.** If th
38c0b 65 20 22 64 61 74 61 22 20 70 61 72 61 6d 65 74  e "data" paramet
38c0c 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  er to this funct
38c0d 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  ion is NULL, the
38c0e 6e 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74  n the.** element
38c0f 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
38c10 6f 20 22 6b 65 79 22 20 69 73 20 72 65 6d 6f 76  o "key" is remov
38c11 65 64 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  ed from the hash
38c12 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
38c13 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
38c14 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
38c15 6e 73 65 72 74 28 0a 20 20 66 74 73 33 48 61 73  nsert(.  fts3Has
38c16 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 2f 2a  h *pH,        /*
38c17 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   The hash table 
38c18 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a  to insert into *
38c19 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
38c1a 70 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20  pKey,    /* The 
38c1b 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65  key */.  int nKe
38c1c 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  y,            /*
38c1d 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
38c1e 20 69 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20   in the key */. 
38c1f 20 76 6f 69 64 20 2a 64 61 74 61 20 20 20 20 20   void *data     
38c20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
38c21 61 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 68 72  a */.){.  int hr
38c22 61 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  aw;             
38c23 20 20 20 20 2f 2a 20 52 61 77 20 68 61 73 68 20      /* Raw hash 
38c24 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79  value of the key
38c25 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20   */.  int h;    
38c26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38c27 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66 20 74  /* the hash of t
38c28 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61  he key modulo ha
38c29 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f  sh table size */
38c2a 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20  .  fts3HashElem 
38c2b 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20  *elem;       /* 
38c2c 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72  Used to loop thr
38c2d 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69  u the element li
38c2e 73 74 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68  st */.  fts3Hash
38c2f 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20  Elem *new_elem; 
38c30 20 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74    /* New element
38c31 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 48   added to the pH
38c32 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61 73   */.  int (*xHas
38c33 68 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  h)(const void*,i
38c34 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 68 61 73  nt);  /* The has
38c35 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20  h function */.. 
38c36 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29   assert( pH!=0 )
38c37 3b 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73 48  ;.  xHash = ftsH
38c38 61 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e  ashFunction(pH->
38c39 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 61 73 73  keyClass);.  ass
38c3a 65 72 74 28 20 78 48 61 73 68 21 3d 30 20 29 3b  ert( xHash!=0 );
38c3b 0a 20 20 68 72 61 77 20 3d 20 28 2a 78 48 61 73  .  hraw = (*xHas
38c3c 68 29 28 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a  h)(pKey, nKey);.
38c3d 20 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e 68    assert( (pH->h
38c3e 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74 73  tsize & (pH->hts
38c3f 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  ize-1))==0 );.  
38c40 68 20 3d 20 68 72 61 77 20 26 20 28 70 48 2d 3e  h = hraw & (pH->
38c41 68 74 73 69 7a 65 2d 31 29 3b 0a 20 20 65 6c 65  htsize-1);.  ele
38c42 6d 20 3d 20 66 74 73 33 46 69 6e 64 45 6c 65 6d  m = fts3FindElem
38c43 65 6e 74 42 79 48 61 73 68 28 70 48 2c 70 4b 65  entByHash(pH,pKe
38c44 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 69 66 28  y,nKey,h);.  if(
38c45 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 76 6f 69   elem ){.    voi
38c46 64 20 2a 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c  d *old_data = el
38c47 65 6d 2d 3e 64 61 74 61 3b 0a 20 20 20 20 69 66  em->data;.    if
38c48 28 20 64 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  ( data==0 ){.   
38c49 20 20 20 66 74 73 33 52 65 6d 6f 76 65 45 6c 65     fts3RemoveEle
38c4a 6d 65 6e 74 42 79 48 61 73 68 28 70 48 2c 65 6c  mentByHash(pH,el
38c4b 65 6d 2c 68 29 3b 0a 20 20 20 20 7d 65 6c 73 65  em,h);.    }else
38c4c 7b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 64 61  {.      elem->da
38c4d 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 20 20 7d  ta = data;.    }
38c4e 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f  .    return old_
38c4f 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20  data;.  }.  if( 
38c50 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e  data==0 ) return
38c51 20 30 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74   0;.  if( pH->ht
38c52 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 66  size==0 ){.    f
38c53 74 73 33 52 65 68 61 73 68 28 70 48 2c 38 29 3b  ts3Rehash(pH,8);
38c54 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 68 74 73  .    if( pH->hts
38c55 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ize==0 ){.      
38c56 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20  pH->count = 0;. 
38c57 20 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61       return data
38c58 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6e 65  ;.    }.  }.  ne
38c59 77 5f 65 6c 65 6d 20 3d 20 28 66 74 73 33 48 61  w_elem = (fts3Ha
38c5a 73 68 45 6c 65 6d 2a 29 66 74 73 33 48 61 73 68  shElem*)fts3Hash
38c5b 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 66  Malloc( sizeof(f
38c5c 74 73 33 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a  ts3HashElem) );.
38c5d 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d    if( new_elem==
38c5e 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b  0 ) return data;
38c5f 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b  .  if( pH->copyK
38c60 65 79 20 26 26 20 70 4b 65 79 21 3d 30 20 29 7b  ey && pKey!=0 ){
38c61 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70  .    new_elem->p
38c62 4b 65 79 20 3d 20 66 74 73 33 48 61 73 68 4d 61  Key = fts3HashMa
38c63 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a 20 20  lloc( nKey );.  
38c64 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 2d 3e    if( new_elem->
38c65 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pKey==0 ){.     
38c66 20 66 74 73 33 48 61 73 68 46 72 65 65 28 6e 65   fts3HashFree(ne
38c67 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 72  w_elem);.      r
38c68 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 20  eturn data;.    
38c69 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 28 76 6f  }.    memcpy((vo
38c6a 69 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b  id*)new_elem->pK
38c6b 65 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b  ey, pKey, nKey);
38c6c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65  .  }else{.    ne
38c6d 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28  w_elem->pKey = (
38c6e 76 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20 7d 0a  void*)pKey;.  }.
38c6f 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79    new_elem->nKey
38c70 20 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d 3e 63   = nKey;.  pH->c
38c71 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 48  ount++;.  if( pH
38c72 2d 3e 63 6f 75 6e 74 20 3e 20 70 48 2d 3e 68 74  ->count > pH->ht
38c73 73 69 7a 65 20 29 7b 0a 20 20 20 20 66 74 73 33  size ){.    fts3
38c74 52 65 68 61 73 68 28 70 48 2c 70 48 2d 3e 68 74  Rehash(pH,pH->ht
38c75 73 69 7a 65 2a 32 29 3b 0a 20 20 7d 0a 20 20 61  size*2);.  }.  a
38c76 73 73 65 72 74 28 20 70 48 2d 3e 68 74 73 69 7a  ssert( pH->htsiz
38c77 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  e>0 );.  assert(
38c78 20 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28   (pH->htsize & (
38c79 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d  pH->htsize-1))==
38c7a 30 20 29 3b 0a 20 20 68 20 3d 20 68 72 61 77 20  0 );.  h = hraw 
38c7b 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29  & (pH->htsize-1)
38c7c 3b 0a 20 20 66 74 73 33 48 61 73 68 49 6e 73 65  ;.  fts3HashInse
38c7d 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70  rtElement(pH, &p
38c7e 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c  H->ht[h], new_el
38c7f 65 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d  em);.  new_elem-
38c80 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20  >data = data;.  
38c81 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 65 6e  return 0;.}..#en
38c82 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
38c83 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
38c84 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
38c85 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
38c86 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
38c87 45 6e 64 20 6f 66 20 66 74 73 33 5f 68 61 73 68  End of fts3_hash
38c88 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
38c89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38c8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
38c8b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
38c8c 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
38c8d 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  porter.c *******
38c8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38c8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
38c90 2f 2a 0a 2a 2a 20 32 30 30 36 20 53 65 70 74 65  /*.** 2006 Septe
38c91 6d 62 65 72 20 33 30 0a 2a 2a 0a 2a 2a 20 54 68  mber 30.**.** Th
38c92 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
38c93 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
38c94 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
38c95 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
38c96 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
38c97 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
38c98 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
38c99 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
38c9a 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
38c9b 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
38c9c 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
38c9d 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
38c9e 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
38c9f 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
38ca0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
38ca1 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
38ca2 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
38ca3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38ca4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38ca5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38ca6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38ca7 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  ******.** Implem
38ca8 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
38ca9 66 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72 63 68  full-text-search
38caa 20 74 6f 6b 65 6e 69 7a 65 72 20 74 68 61 74 20   tokenizer that 
38cab 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 20  implements.** a 
38cac 50 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 2e 0a  Porter stemmer..
38cad 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
38cae 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
38caf 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64  is only compiled
38cb0 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a   if:.**.**     *
38cb1 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
38cb2 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
38cb3 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a  as an extension.
38cb4 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68 69  **       (in whi
38cb5 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43  ch case SQLITE_C
38cb6 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  ORE is not defin
38cb7 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20  ed), or.**.**   
38cb8 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64    * The FTS3 mod
38cb9 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69  ule is being bui
38cba 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65  lt into the core
38cbb 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c   of.**       SQL
38cbc 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63 61  ite (in which ca
38cbd 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  se SQLITE_ENABLE
38cbe 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65 64  _FTS3 is defined
38cbf 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  )..*/.#if !defin
38cc0 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20  ed(SQLITE_CORE) 
38cc1 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
38cc2 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a  E_ENABLE_FTS3)..
38cc3 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73 20 64  .../*.** Class d
38cc4 65 72 69 76 65 64 20 66 72 6f 6d 20 73 71 6c 69  erived from sqli
38cc5 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f  te3_tokenizer.*/
38cc6 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
38cc7 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
38cc8 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b   {.  sqlite3_tok
38cc9 65 6e 69 7a 65 72 20 62 61 73 65 3b 20 20 20 20  enizer base;    
38cca 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73 20    /* Base class 
38ccb 2a 2f 0a 7d 20 70 6f 72 74 65 72 5f 74 6f 6b 65  */.} porter_toke
38ccc 6e 69 7a 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6c  nizer;../*.** Cl
38ccd 61 73 73 20 64 65 72 69 76 65 64 20 66 72 6f 6d  ass derived from
38cce 20 73 71 6c 69 74 33 5f 74 6f 6b 65 6e 69 7a 65   sqlit3_tokenize
38ccf 72 5f 63 75 72 73 6f 72 0a 2a 2f 0a 74 79 70 65  r_cursor.*/.type
38cd0 64 65 66 20 73 74 72 75 63 74 20 70 6f 72 74 65  def struct porte
38cd1 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  r_tokenizer_curs
38cd2 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  or {.  sqlite3_t
38cd3 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
38cd4 62 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68  base;.  const ch
38cd5 61 72 20 2a 7a 49 6e 70 75 74 3b 20 20 20 20 20  ar *zInput;     
38cd6 20 20 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65       /* input we
38cd7 20 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20   are tokenizing 
38cd8 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b  */.  int nInput;
38cd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38cda 20 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65    /* size of the
38cdb 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20   input */.  int 
38cdc 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  iOffset;        
38cdd 20 20 20 20 20 20 20 20 20 2f 2a 20 63 75 72 72           /* curr
38cde 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20  ent position in 
38cdf 7a 49 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20  zInput */.  int 
38ce0 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  iToken;         
38ce1 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65           /* inde
38ce2 78 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65 6e 20  x of next token 
38ce3 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a  to be returned *
38ce4 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e  /.  char *zToken
38ce5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
38ce6 20 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f 72 20   /* storage for 
38ce7 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f  current token */
38ce8 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 61 74 65  .  int nAllocate
38ce9 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
38cea 2f 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  /* space allocat
38ceb 65 64 20 74 6f 20 7a 54 6f 6b 65 6e 20 62 75 66  ed to zToken buf
38cec 66 65 72 20 2a 2f 0a 7d 20 70 6f 72 74 65 72 5f  fer */.} porter_
38ced 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
38cee 3b 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  ;.../* Forward d
38cef 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74  eclaration */.st
38cf0 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
38cf1 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
38cf2 75 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65 6e 69  ule porterTokeni
38cf3 7a 65 72 4d 6f 64 75 6c 65 3b 0a 0a 0a 2f 2a 0a  zerModule;.../*.
38cf4 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
38cf5 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e  tokenizer instan
38cf6 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
38cf7 74 20 70 6f 72 74 65 72 43 72 65 61 74 65 28 0a  t porterCreate(.
38cf8 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73    int argc, cons
38cf9 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a  t char * const *
38cfa 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argv,.  sqlite3_
38cfb 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f  tokenizer **ppTo
38cfc 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 70 6f 72  kenizer.){.  por
38cfd 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74  ter_tokenizer *t
38cfe 3b 0a 20 20 74 20 3d 20 28 70 6f 72 74 65 72 5f  ;.  t = (porter_
38cff 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73 71 6c  tokenizer *) sql
38d00 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
38d01 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66 28 20 74  of(*t));.  if( t
38d02 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20  ==NULL ) return 
38d03 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
38d04 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20 73 69 7a  memset(t, 0, siz
38d05 65 6f 66 28 2a 74 29 29 3b 0a 20 20 2a 70 70 54  eof(*t));.  *ppT
38d06 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74 2d 3e 62  okenizer = &t->b
38d07 61 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ase;.  return SQ
38d08 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
38d09 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f 6b 65  * Destroy a toke
38d0a 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20  nizer.*/.static 
38d0b 69 6e 74 20 70 6f 72 74 65 72 44 65 73 74 72 6f  int porterDestro
38d0c 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  y(sqlite3_tokeni
38d0d 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29  zer *pTokenizer)
38d0e 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  {.  sqlite3_free
38d0f 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20  (pTokenizer);.  
38d10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
38d11 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61  ;.}../*.** Prepa
38d12 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65  re to begin toke
38d13 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75  nizing a particu
38d14 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65  lar string.  The
38d15 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e 67   input.** string
38d16 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64   to be tokenized
38d17 20 69 73 20 7a 49 6e 70 75 74 5b 30 2e 2e 6e 49   is zInput[0..nI
38d18 6e 70 75 74 2d 31 5d 2e 20 20 41 20 63 75 72 73  nput-1].  A curs
38d19 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e  or.** used to in
38d1a 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65  crementally toke
38d1b 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e 67  nize this string
38d1c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20   is returned in 
38d1d 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a  .** *ppCursor..*
38d1e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72  /.static int por
38d1f 74 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  terOpen(.  sqlit
38d20 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
38d21 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 20  okenizer,       
38d22 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a    /* The tokeniz
38d23 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  er */.  const ch
38d24 61 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e 74 20  ar *zInput, int 
38d25 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 2f  nInput,        /
38d26 2a 20 53 74 72 69 6e 67 20 74 6f 20 62 65 20 74  * String to be t
38d27 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20 20 73 71  okenized */.  sq
38d28 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
38d29 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f  cursor **ppCurso
38d2a 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b  r    /* OUT: Tok
38d2b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
38d2c 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f   */.){.  porter_
38d2d 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
38d2e 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 70 6f 72   *c;..  c = (por
38d2f 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ter_tokenizer_cu
38d30 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  rsor *) sqlite3_
38d31 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 63  malloc(sizeof(*c
38d32 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d 4e 55 4c  ));.  if( c==NUL
38d33 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
38d34 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63 2d 3e 7a  E_NOMEM;..  c->z
38d35 49 6e 70 75 74 20 3d 20 7a 49 6e 70 75 74 3b 0a  Input = zInput;.
38d36 20 20 69 66 28 20 7a 49 6e 70 75 74 3d 3d 30 20    if( zInput==0 
38d37 29 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75 74  ){.    c->nInput
38d38 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
38d39 28 20 6e 49 6e 70 75 74 3c 30 20 29 7b 0a 20 20  ( nInput<0 ){.  
38d3a 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 28 69    c->nInput = (i
38d3b 6e 74 29 73 74 72 6c 65 6e 28 7a 49 6e 70 75 74  nt)strlen(zInput
38d3c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
38d3d 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 6e 49 6e 70  c->nInput = nInp
38d3e 75 74 3b 0a 20 20 7d 0a 20 20 63 2d 3e 69 4f 66  ut;.  }.  c->iOf
38d3f 66 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20 20  fset = 0;       
38d40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61            /* sta
38d41 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 74  rt tokenizing at
38d42 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 2a   the beginning *
38d43 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20  /.  c->iToken = 
38d44 30 3b 0a 20 20 63 2d 3e 7a 54 6f 6b 65 6e 20 3d  0;.  c->zToken =
38d45 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20 20 20   NULL;          
38d46 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61 63 65       /* no space
38d47 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65 74 2e   allocated, yet.
38d48 20 2a 2f 0a 20 20 63 2d 3e 6e 41 6c 6c 6f 63 61   */.  c->nAlloca
38d49 74 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70 70 43  ted = 0;..  *ppC
38d4a 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65  ursor = &c->base
38d4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
38d4c 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
38d4d 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61 74  lose a tokenizat
38d4e 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65 76 69  ion cursor previ
38d4f 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62 79 20  ously opened by 
38d50 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 70 6f 72  a call to.** por
38d51 74 65 72 4f 70 65 6e 28 29 20 61 62 6f 76 65 2e  terOpen() above.
38d52 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
38d53 6f 72 74 65 72 43 6c 6f 73 65 28 73 71 6c 69 74  orterClose(sqlit
38d54 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
38d55 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20  sor *pCursor){. 
38d56 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65   porter_tokenize
38d57 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 70  r_cursor *c = (p
38d58 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
38d59 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
38d5a 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  r;.  sqlite3_fre
38d5b 65 28 63 2d 3e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  e(c->zToken);.  
38d5c 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 29 3b  sqlite3_free(c);
38d5d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
38d5e 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 56 6f 77  _OK;.}./*.** Vow
38d5f 65 6c 20 6f 72 20 63 6f 6e 73 6f 6e 61 6e 74 0a  el or consonant.
38d60 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
38d61 63 68 61 72 20 63 54 79 70 65 5b 5d 20 3d 20 7b  char cType[] = {
38d62 0a 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20  .   0, 1, 1, 1, 
38d63 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  0, 1, 1, 1, 0, 1
38d64 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
38d65 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
38d66 30 2c 0a 20 20 20 31 2c 20 31 2c 20 31 2c 20 32  0,.   1, 1, 1, 2
38d67 2c 20 31 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69 73  , 1.};../*.** is
38d68 43 6f 6e 73 6f 6e 61 6e 74 28 29 20 61 6e 64 20  Consonant() and 
38d69 69 73 56 6f 77 65 6c 28 29 20 64 65 74 65 72 6d  isVowel() determ
38d6a 69 6e 65 20 69 66 20 74 68 65 69 72 20 66 69 72  ine if their fir
38d6b 73 74 20 63 68 61 72 61 63 74 65 72 20 69 6e 0a  st character in.
38d6c 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 74 68  ** the string th
38d6d 65 79 20 70 6f 69 6e 74 20 74 6f 20 69 73 20 61  ey point to is a
38d6e 20 63 6f 6e 73 6f 6e 61 6e 74 20 6f 72 20 61 20   consonant or a 
38d6f 76 6f 77 65 6c 2c 20 61 63 63 6f 72 64 69 6e 67  vowel, according
38d70 0a 2a 2a 20 74 6f 20 50 6f 72 74 65 72 20 72 75  .** to Porter ru
38d71 6c 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 63 6f  ls.  .**.** A co
38d72 6e 73 6f 6e 61 74 65 20 69 73 20 61 6e 79 20 6c  nsonate is any l
38d73 65 74 74 65 72 20 6f 74 68 65 72 20 74 68 61 6e  etter other than
38d74 20 27 61 27 2c 20 27 65 27 2c 20 27 69 27 2c 20   'a', 'e', 'i', 
38d75 27 6f 27 2c 20 6f 72 20 27 75 27 2e 0a 2a 2a 20  'o', or 'u'..** 
38d76 27 59 27 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61  'Y' is a consona
38d77 6e 74 20 75 6e 6c 65 73 73 20 69 74 20 66 6f 6c  nt unless it fol
38d78 6c 6f 77 73 20 61 6e 6f 74 68 65 72 20 63 6f 6e  lows another con
38d79 73 6f 6e 61 6e 74 2c 0a 2a 2a 20 69 6e 20 77 68  sonant,.** in wh
38d7a 69 63 68 20 63 61 73 65 20 69 74 20 69 73 20 61  ich case it is a
38d7b 20 76 6f 77 65 6c 2e 0a 2a 2a 0a 2a 2a 20 49 6e   vowel..**.** In
38d7c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 2c 20   these routine, 
38d7d 74 68 65 20 6c 65 74 74 65 72 73 20 61 72 65 20  the letters are 
38d7e 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72  in reverse order
38d7f 2e 20 20 53 6f 20 74 68 65 20 27 79 27 20 72 75  .  So the 'y' ru
38d80 6c 65 0a 2a 2a 20 69 73 20 74 68 61 74 20 27 79  le.** is that 'y
38d81 27 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74  ' is a consonant
38d82 20 75 6e 6c 65 73 73 20 69 74 20 69 73 20 66 6f   unless it is fo
38d83 6c 6c 6f 77 65 64 20 62 79 20 61 6e 6f 74 68 65  llowed by anothe
38d84 72 0a 2a 2a 20 63 6f 6e 73 6f 6e 65 6e 74 2e 0a  r.** consonent..
38d85 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  */.static int is
38d86 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72  Vowel(const char
38d87 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 69  *);.static int i
38d88 73 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e 73 74  sConsonant(const
38d89 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74   char *z){.  int
38d8a 20 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d 20 2a   j;.  char x = *
38d8b 7a 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20 29 20  z;.  if( x==0 ) 
38d8c 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65  return 0;.  asse
38d8d 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20 78 3c  rt( x>='a' && x<
38d8e 3d 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20 63 54  ='z' );.  j = cT
38d8f 79 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20 69 66  ype[x-'a'];.  if
38d90 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e 20 6a  ( j<2 ) return j
38d91 3b 0a 20 20 72 65 74 75 72 6e 20 7a 5b 31 5d 3d  ;.  return z[1]=
38d92 3d 30 20 7c 7c 20 69 73 56 6f 77 65 6c 28 7a 20  =0 || isVowel(z 
38d93 2b 20 31 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69  + 1);.}.static i
38d94 6e 74 20 69 73 56 6f 77 65 6c 28 63 6f 6e 73 74  nt isVowel(const
38d95 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74   char *z){.  int
38d96 20 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d 20 2a   j;.  char x = *
38d97 7a 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20 29 20  z;.  if( x==0 ) 
38d98 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65  return 0;.  asse
38d99 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20 78 3c  rt( x>='a' && x<
38d9a 3d 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20 63 54  ='z' );.  j = cT
38d9b 79 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20 69 66  ype[x-'a'];.  if
38d9c 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e 20 31  ( j<2 ) return 1
38d9d 2d 6a 3b 0a 20 20 72 65 74 75 72 6e 20 69 73 43  -j;.  return isC
38d9e 6f 6e 73 6f 6e 61 6e 74 28 7a 20 2b 20 31 29 3b  onsonant(z + 1);
38d9f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 61 6e  .}../*.** Let an
38da0 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e  y sequence of on
38da1 65 20 6f 72 20 6d 6f 72 65 20 76 6f 77 65 6c 73  e or more vowels
38da2 20 62 65 20 72 65 70 72 65 73 65 6e 74 65 64 20   be represented 
38da3 62 79 20 56 20 61 6e 64 20 6c 65 74 0a 2a 2a 20  by V and let.** 
38da4 43 20 62 65 20 73 65 71 75 65 6e 63 65 20 6f 66  C be sequence of
38da5 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 6f 6e   one or more con
38da6 73 6f 6e 61 6e 74 73 2e 20 20 54 68 65 6e 20 65  sonants.  Then e
38da7 76 65 72 79 20 77 6f 72 64 20 63 61 6e 20 62 65  very word can be
38da8 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 65 64 20  .** represented 
38da9 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  as:.**.**       
38daa 20 20 20 20 5b 43 5d 20 28 56 43 29 7b 6d 7d 20      [C] (VC){m} 
38dab 5b 56 5d 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 6f  [V].**.** In pro
38dac 73 65 3a 20 20 41 20 77 6f 72 64 20 69 73 20 61  se:  A word is a
38dad 6e 20 6f 70 74 69 6f 6e 61 6c 20 63 6f 6e 73 6f  n optional conso
38dae 6e 61 6e 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79  nant followed by
38daf 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 76 6f 77 65   zero or.** vowe
38db0 6c 2d 63 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72  l-consonant pair
38db1 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e  s followed by an
38db2 20 6f 70 74 69 6f 6e 61 6c 20 76 6f 77 65 6c 2e   optional vowel.
38db3 20 20 22 6d 22 20 69 73 20 74 68 65 0a 2a 2a 20    "m" is the.** 
38db4 6e 75 6d 62 65 72 20 6f 66 20 76 6f 77 65 6c 20  number of vowel 
38db5 63 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72 73 2e  consonant pairs.
38db6 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63    This routine c
38db7 6f 6d 70 75 74 65 73 20 74 68 65 20 76 61 6c 75  omputes the valu
38db8 65 0a 2a 2a 20 6f 66 20 6d 20 66 6f 72 20 74 68  e.** of m for th
38db9 65 20 66 69 72 73 74 20 69 20 62 79 74 65 73 20  e first i bytes 
38dba 6f 66 20 61 20 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a  of a word..**.**
38dbb 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
38dbc 74 68 65 20 6d 2d 76 61 6c 75 65 20 66 6f 72 20  the m-value for 
38dbd 7a 20 69 73 20 31 20 6f 72 20 6d 6f 72 65 2e 20  z is 1 or more. 
38dbe 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
38dbf 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20  .** return true 
38dc0 69 66 20 7a 20 63 6f 6e 74 61 69 6e 73 20 61 74  if z contains at
38dc1 20 6c 65 61 73 74 20 6f 6e 65 20 76 6f 77 65 6c   least one vowel
38dc2 20 74 68 61 74 20 69 73 20 66 6f 6c 6c 6f 77 65   that is followe
38dc3 64 0a 2a 2a 20 62 79 20 61 20 63 6f 6e 73 6f 6e  d.** by a conson
38dc4 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  ant..**.** In th
38dc5 69 73 20 72 6f 75 74 69 6e 65 20 7a 5b 5d 20 69  is routine z[] i
38dc6 73 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64  s in reverse ord
38dc7 65 72 2e 20 20 53 6f 20 77 65 20 61 72 65 20 72  er.  So we are r
38dc8 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a  eally looking.**
38dc9 20 66 6f 72 20 61 6e 20 69 6e 73 74 61 6e 63 65   for an instance
38dca 20 6f 66 20 6f 66 20 61 20 63 6f 6e 73 6f 6e 61   of of a consona
38dcb 6e 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61  nt followed by a
38dcc 20 76 6f 77 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69   vowel..*/.stati
38dcd 63 20 69 6e 74 20 6d 5f 67 74 5f 30 28 63 6f 6e  c int m_gt_0(con
38dce 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77  st char *z){.  w
38dcf 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
38dd0 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
38dd1 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
38dd2 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  0;.  while( isCo
38dd3 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
38dd4 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  +; }.  return *z
38dd5 21 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20  !=0;.}../* Like 
38dd6 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63 65 70  mgt0 above excep
38dd7 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67  t we are looking
38dd8 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f 66 20   for a value of 
38dd9 6d 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 65 78  m which is.** ex
38dda 61 63 74 6c 79 20 31 0a 2a 2f 0a 73 74 61 74 69  actly 1.*/.stati
38ddb 63 20 69 6e 74 20 6d 5f 65 71 5f 31 28 63 6f 6e  c int m_eq_1(con
38ddc 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77  st char *z){.  w
38ddd 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
38dde 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
38ddf 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
38de0 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  0;.  while( isCo
38de1 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
38de2 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30  +; }.  if( *z==0
38de3 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77   ) return 0;.  w
38de4 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
38de5 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
38de6 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
38de7 31 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  1;.  while( isCo
38de8 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
38de9 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  +; }.  return *z
38dea 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20  ==0;.}../* Like 
38deb 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63 65 70  mgt0 above excep
38dec 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67  t we are looking
38ded 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f 66 20   for a value of 
38dee 6d 3e 31 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f  m>1 instead.** o
38def 72 20 6d 3e 30 0a 2a 2f 0a 73 74 61 74 69 63 20  r m>0.*/.static 
38df0 69 6e 74 20 6d 5f 67 74 5f 31 28 63 6f 6e 73 74  int m_gt_1(const
38df1 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69   char *z){.  whi
38df2 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20 29  le( isVowel(z) )
38df3 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a  { z++; }.  if( *
38df4 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  z==0 ) return 0;
38df5 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73  .  while( isCons
38df6 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b  onant(z) ){ z++;
38df7 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29   }.  if( *z==0 )
38df8 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69   return 0;.  whi
38df9 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20 29  le( isVowel(z) )
38dfa 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a  { z++; }.  if( *
38dfb 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  z==0 ) return 0;
38dfc 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73  .  while( isCons
38dfd 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b  onant(z) ){ z++;
38dfe 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d   }.  return *z!=
38dff 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  0;.}../*.** Retu
38e00 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 72 65  rn TRUE if there
38e01 20 69 73 20 61 20 76 6f 77 65 6c 20 61 6e 79 77   is a vowel anyw
38e02 68 65 72 65 20 77 69 74 68 69 6e 20 7a 5b 30 2e  here within z[0.
38e03 2e 6e 2d 31 5d 0a 2a 2f 0a 73 74 61 74 69 63 20  .n-1].*/.static 
38e04 69 6e 74 20 68 61 73 56 6f 77 65 6c 28 63 6f 6e  int hasVowel(con
38e05 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77  st char *z){.  w
38e06 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e  hile( isConsonan
38e07 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20  t(z) ){ z++; }. 
38e08 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d   return *z!=0;.}
38e09 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
38e0a 52 55 45 20 69 66 20 74 68 65 20 77 6f 72 64 20  RUE if the word 
38e0b 65 6e 64 73 20 69 6e 20 61 20 64 6f 75 62 6c 65  ends in a double
38e0c 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a   consonant..**.*
38e0d 2a 20 54 68 65 20 74 65 78 74 20 69 73 20 72 65  * The text is re
38e0e 76 65 72 73 65 64 20 68 65 72 65 2e 20 53 6f 20  versed here. So 
38e0f 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6c 6f  we are really lo
38e10 6f 6b 69 6e 67 20 61 74 0a 2a 2a 20 74 68 65 20  oking at.** the 
38e11 66 69 72 73 74 20 74 77 6f 20 63 68 61 72 61 63  first two charac
38e12 74 65 72 73 20 6f 66 20 7a 5b 5d 2e 0a 2a 2f 0a  ters of z[]..*/.
38e13 73 74 61 74 69 63 20 69 6e 74 20 64 6f 75 62 6c  static int doubl
38e14 65 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e 73 74  eConsonant(const
38e15 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65 74   char *z){.  ret
38e16 75 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  urn isConsonant(
38e17 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d 7a 5b 31 5d  z) && z[0]==z[1]
38e18 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28   && isConsonant(
38e19 7a 2b 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  z+1);.}../*.** R
38e1a 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
38e1b 65 20 77 6f 72 64 20 65 6e 64 73 20 77 69 74 68  e word ends with
38e1c 20 74 68 72 65 65 20 6c 65 74 74 65 72 73 20 77   three letters w
38e1d 68 69 63 68 0a 2a 2a 20 61 72 65 20 63 6f 6e 73  hich.** are cons
38e1e 6f 6e 61 6e 74 2d 76 6f 77 65 6c 2d 63 6f 6e 73  onant-vowel-cons
38e1f 6f 6e 65 6e 74 20 61 6e 64 20 77 68 65 72 65 20  onent and where 
38e20 74 68 65 20 66 69 6e 61 6c 20 63 6f 6e 73 6f 6e  the final conson
38e21 61 6e 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 27 77  ant.** is not 'w
38e22 27 2c 20 27 78 27 2c 20 6f 72 20 27 79 27 2e 0a  ', 'x', or 'y'..
38e23 2a 2a 0a 2a 2a 20 54 68 65 20 77 6f 72 64 20 69  **.** The word i
38e24 73 20 72 65 76 65 72 73 65 64 20 68 65 72 65 2e  s reversed here.
38e25 20 20 53 6f 20 77 65 20 61 72 65 20 72 65 61 6c    So we are real
38e26 6c 79 20 63 68 65 63 6b 69 6e 67 20 74 68 65 0a  ly checking the.
38e27 2a 2a 20 66 69 72 73 74 20 74 68 72 65 65 20 6c  ** first three l
38e28 65 74 74 65 72 73 20 61 6e 64 20 74 68 65 20 66  etters and the f
38e29 69 72 73 74 20 6f 6e 65 20 63 61 6e 6e 6f 74 20  irst one cannot 
38e2a 62 65 20 69 6e 20 5b 77 78 79 5d 2e 0a 2a 2f 0a  be in [wxy]..*/.
38e2b 73 74 61 74 69 63 20 69 6e 74 20 73 74 61 72 5f  static int star_
38e2c 6f 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  oh(const char *z
38e2d 29 7b 0a 20 20 72 65 74 75 72 6e 0a 20 20 20 20  ){.  return.    
38e2e 7a 5b 30 5d 21 3d 30 20 26 26 20 69 73 43 6f 6e  z[0]!=0 && isCon
38e2f 73 6f 6e 61 6e 74 28 7a 29 20 26 26 0a 20 20 20  sonant(z) &&.   
38e30 20 7a 5b 30 5d 21 3d 27 77 27 20 26 26 20 7a 5b   z[0]!='w' && z[
38e31 30 5d 21 3d 27 78 27 20 26 26 20 7a 5b 30 5d 21  0]!='x' && z[0]!
38e32 3d 27 79 27 20 26 26 0a 20 20 20 20 7a 5b 31 5d  ='y' &&.    z[1]
38e33 21 3d 30 20 26 26 20 69 73 56 6f 77 65 6c 28 7a  !=0 && isVowel(z
38e34 2b 31 29 20 26 26 0a 20 20 20 20 7a 5b 32 5d 21  +1) &&.    z[2]!
38e35 3d 30 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e  =0 && isConsonan
38e36 74 28 7a 2b 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  t(z+2);.}../*.**
38e37 20 49 66 20 74 68 65 20 77 6f 72 64 20 65 6e 64   If the word end
38e38 73 20 77 69 74 68 20 7a 46 72 6f 6d 20 61 6e 64  s with zFrom and
38e39 20 78 43 6f 6e 64 28 29 20 69 73 20 74 72 75 65   xCond() is true
38e3a 20 66 6f 72 20 74 68 65 20 73 74 65 6d 0a 2a 2a   for the stem.**
38e3b 20 6f 66 20 74 68 65 20 77 6f 72 64 20 74 68 61   of the word tha
38e3c 74 20 70 72 65 63 65 65 64 73 20 74 68 65 20 7a  t preceeds the z
38e3d 46 72 6f 6d 20 65 6e 64 69 6e 67 2c 20 74 68 65  From ending, the
38e3e 6e 20 63 68 61 6e 67 65 20 74 68 65 20 0a 2a 2a  n change the .**
38e3f 20 65 6e 64 69 6e 67 20 74 6f 20 7a 54 6f 2e 0a   ending to zTo..
38e40 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20  **.** The input 
38e41 77 6f 72 64 20 2a 70 7a 20 61 6e 64 20 7a 46 72  word *pz and zFr
38e42 6f 6d 20 61 72 65 20 62 6f 74 68 20 69 6e 20 72  om are both in r
38e43 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20 7a  everse order.  z
38e44 54 6f 0a 2a 2a 20 69 73 20 69 6e 20 6e 6f 72 6d  To.** is in norm
38e45 61 6c 20 6f 72 64 65 72 2e 20 0a 2a 2a 0a 2a 2a  al order. .**.**
38e46 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
38e47 7a 46 72 6f 6d 20 6d 61 74 63 68 65 73 2e 20 20  zFrom matches.  
38e48 52 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20  Return FALSE if 
38e49 7a 46 72 6f 6d 20 64 6f 65 73 20 6e 6f 74 0a 2a  zFrom does not.*
38e4a 2a 20 6d 61 74 63 68 2e 20 20 4e 6f 74 20 74 68  * match.  Not th
38e4b 61 74 20 54 52 55 45 20 69 73 20 72 65 74 75 72  at TRUE is retur
38e4c 6e 65 64 20 65 76 65 6e 20 69 66 20 78 43 6f 6e  ned even if xCon
38e4d 64 28 29 20 66 61 69 6c 73 20 61 6e 64 0a 2a 2a  d() fails and.**
38e4e 20 6e 6f 20 73 75 62 73 74 69 74 75 74 69 6f 6e   no substitution
38e4f 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74   occurs..*/.stat
38e50 69 63 20 69 6e 74 20 73 74 65 6d 28 0a 20 20 63  ic int stem(.  c
38e51 68 61 72 20 2a 2a 70 7a 2c 20 20 20 20 20 20 20  har **pz,       
38e52 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 6f 72        /* The wor
38e53 64 20 62 65 69 6e 67 20 73 74 65 6d 6d 65 64 20  d being stemmed 
38e54 28 52 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20  (Reversed) */.  
38e55 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 72 6f  const char *zFro
38e56 6d 2c 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  m,     /* If the
38e57 20 65 6e 64 69 6e 67 20 6d 61 74 63 68 65 73 20   ending matches 
38e58 74 68 69 73 2e 2e 2e 20 28 52 65 76 65 72 73 65  this... (Reverse
38e59 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  d) */.  const ch
38e5a 61 72 20 2a 7a 54 6f 2c 20 20 20 20 20 20 20 2f  ar *zTo,       /
38e5b 2a 20 2e 2e 2e 20 63 68 61 6e 67 65 20 74 68 65  * ... change the
38e5c 20 65 6e 64 69 6e 67 20 74 6f 20 74 68 69 73 20   ending to this 
38e5d 28 6e 6f 74 20 72 65 76 65 72 73 65 64 29 20 2a  (not reversed) *
38e5e 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 64 29  /.  int (*xCond)
38e5f 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 20 20 20  (const char*)   
38e60 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20 74 68 61  /* Condition tha
38e61 74 20 6d 75 73 74 20 62 65 20 74 72 75 65 20 2a  t must be true *
38e62 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 20 3d  /.){.  char *z =
38e63 20 2a 70 7a 3b 0a 20 20 77 68 69 6c 65 28 20 2a   *pz;.  while( *
38e64 7a 46 72 6f 6d 20 26 26 20 2a 7a 46 72 6f 6d 3d  zFrom && *zFrom=
38e65 3d 2a 7a 20 29 7b 20 7a 2b 2b 3b 20 7a 46 72 6f  =*z ){ z++; zFro
38e66 6d 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 46  m++; }.  if( *zF
38e67 72 6f 6d 21 3d 30 20 29 20 72 65 74 75 72 6e 20  rom!=0 ) return 
38e68 30 3b 0a 20 20 69 66 28 20 78 43 6f 6e 64 20 26  0;.  if( xCond &
38e69 26 20 21 78 43 6f 6e 64 28 7a 29 20 29 20 72 65  & !xCond(z) ) re
38e6a 74 75 72 6e 20 31 3b 0a 20 20 77 68 69 6c 65 28  turn 1;.  while(
38e6b 20 2a 7a 54 6f 20 29 7b 0a 20 20 20 20 2a 28 2d   *zTo ){.    *(-
38e6c 2d 7a 29 20 3d 20 2a 28 7a 54 6f 2b 2b 29 3b 0a  -z) = *(zTo++);.
38e6d 20 20 7d 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a 20    }.  *pz = z;. 
38e6e 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
38e6f 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
38e70 66 61 6c 6c 62 61 63 6b 20 73 74 65 6d 6d 65 72  fallback stemmer
38e71 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 70   used when the p
38e72 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 20 69 73  orter stemmer is
38e73 0a 2a 2a 20 69 6e 61 70 70 72 6f 70 72 69 61 74  .** inappropriat
38e74 65 2e 20 20 54 68 65 20 69 6e 70 75 74 20 77 6f  e.  The input wo
38e75 72 64 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74  rd is copied int
38e76 6f 20 74 68 65 20 6f 75 74 70 75 74 20 77 69 74  o the output wit
38e77 68 0a 2a 2a 20 55 53 2d 41 53 43 49 49 20 63 61  h.** US-ASCII ca
38e78 73 65 20 66 6f 6c 64 69 6e 67 2e 20 20 49 66 20  se folding.  If 
38e79 74 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 69  the input word i
38e7a 73 20 74 6f 6f 20 6c 6f 6e 67 20 28 6d 6f 72 65  s too long (more
38e7b 0a 2a 2a 20 74 68 61 6e 20 32 30 20 62 79 74 65  .** than 20 byte
38e7c 73 20 69 66 20 69 74 20 63 6f 6e 74 61 69 6e 73  s if it contains
38e7d 20 6e 6f 20 64 69 67 69 74 73 20 6f 72 20 6d 6f   no digits or mo
38e7e 72 65 20 74 68 61 6e 20 36 20 62 79 74 65 73 20  re than 6 bytes 
38e7f 69 66 0a 2a 2a 20 69 74 20 63 6f 6e 74 61 69 6e  if.** it contain
38e80 73 20 64 69 67 69 74 73 29 20 74 68 65 6e 20 77  s digits) then w
38e81 6f 72 64 20 69 73 20 74 72 75 6e 63 61 74 65 64  ord is truncated
38e82 20 74 6f 20 32 30 20 6f 72 20 36 20 62 79 74 65   to 20 or 6 byte
38e83 73 0a 2a 2a 20 62 79 20 74 61 6b 69 6e 67 20 31  s.** by taking 1
38e84 30 20 6f 72 20 33 20 62 79 74 65 73 20 66 72 6f  0 or 3 bytes fro
38e85 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
38e86 61 6e 64 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74  and end..*/.stat
38e87 69 63 20 76 6f 69 64 20 63 6f 70 79 5f 73 74 65  ic void copy_ste
38e88 6d 6d 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20  mmer(const char 
38e89 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c 20 63  *zIn, int nIn, c
38e8a 68 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74 20 2a  har *zOut, int *
38e8b 70 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20 69 2c  pnOut){.  int i,
38e8c 20 6d 78 2c 20 6a 3b 0a 20 20 69 6e 74 20 68 61   mx, j;.  int ha
38e8d 73 44 69 67 69 74 20 3d 20 30 3b 0a 20 20 66 6f  sDigit = 0;.  fo
38e8e 72 28 69 3d 30 3b 20 69 3c 6e 49 6e 3b 20 69 2b  r(i=0; i<nIn; i+
38e8f 2b 29 7b 0a 20 20 20 20 69 6e 74 20 63 20 3d 20  +){.    int c = 
38e90 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20  zIn[i];.    if( 
38e91 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d 27 5a 27  c>='A' && c<='Z'
38e92 20 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69   ){.      zOut[i
38e93 5d 20 3d 20 63 20 2d 20 27 41 27 20 2b 20 27 61  ] = c - 'A' + 'a
38e94 27 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ';.    }else{.  
38e95 20 20 20 20 69 66 28 20 63 3e 3d 27 30 27 20 26      if( c>='0' &
38e96 26 20 63 3c 3d 27 39 27 20 29 20 68 61 73 44 69  & c<='9' ) hasDi
38e97 67 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a  git = 1;.      z
38e98 4f 75 74 5b 69 5d 20 3d 20 63 3b 0a 20 20 20 20  Out[i] = c;.    
38e99 7d 0a 20 20 7d 0a 20 20 6d 78 20 3d 20 68 61 73  }.  }.  mx = has
38e9a 44 69 67 69 74 20 3f 20 33 20 3a 20 31 30 3b 0a  Digit ? 3 : 10;.
38e9b 20 20 69 66 28 20 6e 49 6e 3e 6d 78 2a 32 20 29    if( nIn>mx*2 )
38e9c 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 6d 78 2c 20  {.    for(j=mx, 
38e9d 69 3d 6e 49 6e 2d 6d 78 3b 20 69 3c 6e 49 6e 3b  i=nIn-mx; i<nIn;
38e9e 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20   i++, j++){.    
38e9f 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 7a 4f 75 74    zOut[j] = zOut
38ea0 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  [i];.    }.    i
38ea1 20 3d 20 6a 3b 0a 20 20 7d 0a 20 20 7a 4f 75 74   = j;.  }.  zOut
38ea2 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 6e 4f 75  [i] = 0;.  *pnOu
38ea3 74 20 3d 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  t = i;.}.../*.**
38ea4 20 53 74 65 6d 20 74 68 65 20 69 6e 70 75 74 20   Stem the input 
38ea5 77 6f 72 64 20 7a 49 6e 5b 30 2e 2e 6e 49 6e 2d  word zIn[0..nIn-
38ea6 31 5d 2e 20 20 53 74 6f 72 65 20 74 68 65 20 6f  1].  Store the o
38ea7 75 74 70 75 74 20 69 6e 20 7a 4f 75 74 2e 0a 2a  utput in zOut..*
38ea8 2a 20 7a 4f 75 74 20 69 73 20 61 74 20 6c 65 61  * zOut is at lea
38ea9 73 74 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f  st big enough to
38eaa 20 68 6f 6c 64 20 6e 49 6e 20 62 79 74 65 73 2e   hold nIn bytes.
38eab 20 20 57 72 69 74 65 20 74 68 65 20 61 63 74 75    Write the actu
38eac 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68  al.** size of th
38ead 65 20 6f 75 74 70 75 74 20 77 6f 72 64 20 28 65  e output word (e
38eae 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 20  xclusive of the 
38eaf 27 5c 30 27 20 74 65 72 6d 69 6e 61 74 6f 72 29  '\0' terminator)
38eb0 20 69 6e 74 6f 20 2a 70 6e 4f 75 74 2e 0a 2a 2a   into *pnOut..**
38eb1 0a 2a 2a 20 41 6e 79 20 75 70 70 65 72 2d 63 61  .** Any upper-ca
38eb2 73 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  se characters in
38eb3 20 74 68 65 20 55 53 2d 41 53 43 49 49 20 63 68   the US-ASCII ch
38eb4 61 72 61 63 74 65 72 20 73 65 74 20 28 5b 41 2d  aracter set ([A-
38eb5 5a 5d 29 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65  Z]).** are conve
38eb6 72 74 65 64 20 74 6f 20 6c 6f 77 65 72 20 63 61  rted to lower ca
38eb7 73 65 2e 20 20 55 70 70 65 72 2d 63 61 73 65 20  se.  Upper-case 
38eb8 55 54 46 20 63 68 61 72 61 63 74 65 72 73 20 61  UTF characters a
38eb9 72 65 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e  re.** unchanged.
38eba 0a 2a 2a 0a 2a 2a 20 57 6f 72 64 73 20 74 68 61  .**.** Words tha
38ebb 74 20 61 72 65 20 6c 6f 6e 67 65 72 20 74 68 61  t are longer tha
38ebc 6e 20 61 62 6f 75 74 20 32 30 20 62 79 74 65 73  n about 20 bytes
38ebd 20 61 72 65 20 73 74 65 6d 6d 65 64 20 62 79 20   are stemmed by 
38ebe 72 65 74 61 69 6e 69 6e 67 0a 2a 2a 20 61 20 66  retaining.** a f
38ebf 65 77 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68  ew bytes from th
38ec0 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20  e beginning and 
38ec1 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 77  the end of the w
38ec2 6f 72 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  ord.  If the.** 
38ec3 77 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 64 69  word contains di
38ec4 67 69 74 73 2c 20 33 20 62 79 74 65 73 20 61 72  gits, 3 bytes ar
38ec5 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65  e taken from the
38ec6 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 0a 2a   beginning and.*
38ec7 2a 20 33 20 62 79 74 65 73 20 66 72 6f 6d 20 74  * 3 bytes from t
38ec8 68 65 20 65 6e 64 2e 20 20 46 6f 72 20 6c 6f 6e  he end.  For lon
38ec9 67 20 77 6f 72 64 73 20 77 69 74 68 6f 75 74 20  g words without 
38eca 64 69 67 69 74 73 2c 20 31 30 20 62 79 74 65 73  digits, 10 bytes
38ecb 0a 2a 2a 20 61 72 65 20 74 61 6b 65 6e 20 66 72  .** are taken fr
38ecc 6f 6d 20 65 61 63 68 20 65 6e 64 2e 20 20 55 53  om each end.  US
38ecd 2d 41 53 43 49 49 20 63 61 73 65 20 66 6f 6c 64  -ASCII case fold
38ece 69 6e 67 20 73 74 69 6c 6c 20 61 70 70 6c 69 65  ing still applie
38ecf 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65  s..** .** If the
38ed0 20 69 6e 70 75 74 20 77 6f 72 64 20 63 6f 6e 74   input word cont
38ed1 61 69 6e 73 20 6e 6f 74 20 64 69 67 69 74 73 20  ains not digits 
38ed2 62 75 74 20 64 6f 65 73 20 63 68 61 72 61 63 74  but does charact
38ed3 65 72 73 20 6e 6f 74 20 0a 2a 2a 20 69 6e 20 5b  ers not .** in [
38ed4 61 2d 7a 41 2d 5a 5d 20 74 68 65 6e 20 6e 6f 20  a-zA-Z] then no 
38ed5 73 74 65 6d 6d 69 6e 67 20 69 73 20 61 74 74 65  stemming is atte
38ed6 6d 70 74 65 64 20 61 6e 64 20 74 68 69 73 20 72  mpted and this r
38ed7 6f 75 74 69 6e 65 20 6a 75 73 74 20 0a 2a 2a 20  outine just .** 
38ed8 63 6f 70 69 65 73 20 74 68 65 20 69 6e 70 75 74  copies the input
38ed9 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 20   into the input 
38eda 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20  into the output 
38edb 77 69 74 68 20 55 53 2d 41 53 43 49 49 0a 2a 2a  with US-ASCII.**
38edc 20 63 61 73 65 20 66 6f 6c 64 69 6e 67 2e 0a 2a   case folding..*
38edd 2a 0a 2a 2a 20 53 74 65 6d 6d 69 6e 67 20 6e 65  *.** Stemming ne
38ede 76 65 72 20 69 6e 63 72 65 61 73 65 73 20 74 68  ver increases th
38edf 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
38ee0 77 6f 72 64 2e 20 20 53 6f 20 74 68 65 72 65 20  word.  So there 
38ee1 69 73 0a 2a 2a 20 6e 6f 20 63 68 61 6e 63 65 20  is.** no chance 
38ee2 6f 66 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 74  of overflowing t
38ee3 68 65 20 7a 4f 75 74 20 62 75 66 66 65 72 2e 0a  he zOut buffer..
38ee4 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
38ee5 6f 72 74 65 72 5f 73 74 65 6d 6d 65 72 28 63 6f  orter_stemmer(co
38ee6 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69  nst char *zIn, i
38ee7 6e 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f  nt nIn, char *zO
38ee8 75 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b  ut, int *pnOut){
38ee9 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 63 3b 0a  .  int i, j, c;.
38eea 20 20 63 68 61 72 20 7a 52 65 76 65 72 73 65 5b    char zReverse[
38eeb 32 38 5d 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20  28];.  char *z, 
38eec 2a 7a 32 3b 0a 20 20 69 66 28 20 6e 49 6e 3c 33  *z2;.  if( nIn<3
38eed 20 7c 7c 20 6e 49 6e 3e 3d 73 69 7a 65 6f 66 28   || nIn>=sizeof(
38eee 7a 52 65 76 65 72 73 65 29 2d 37 20 29 7b 0a 20  zReverse)-7 ){. 
38eef 20 20 20 2f 2a 20 54 68 65 20 77 6f 72 64 20 69     /* The word i
38ef0 73 20 74 6f 6f 20 62 69 67 20 6f 72 20 74 6f 6f  s too big or too
38ef1 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65 20 70   small for the p
38ef2 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 2e 0a 20  orter stemmer.. 
38ef3 20 20 20 2a 2a 20 46 61 6c 6c 62 61 63 6b 20 74     ** Fallback t
38ef4 6f 20 74 68 65 20 63 6f 70 79 20 73 74 65 6d 6d  o the copy stemm
38ef5 65 72 20 2a 2f 0a 20 20 20 20 63 6f 70 79 5f 73  er */.    copy_s
38ef6 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c  temmer(zIn, nIn,
38ef7 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a 20   zOut, pnOut);. 
38ef8 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
38ef9 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 73 69 7a 65   for(i=0, j=size
38efa 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 36 3b 20  of(zReverse)-6; 
38efb 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2d 2d 29  i<nIn; i++, j--)
38efc 7b 0a 20 20 20 20 63 20 3d 20 7a 49 6e 5b 69 5d  {.    c = zIn[i]
38efd 3b 0a 20 20 20 20 69 66 28 20 63 3e 3d 27 41 27  ;.    if( c>='A'
38efe 20 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0a 20 20   && c<='Z' ){.  
38eff 20 20 20 20 7a 52 65 76 65 72 73 65 5b 6a 5d 20      zReverse[j] 
38f00 3d 20 63 20 2b 20 27 61 27 20 2d 20 27 41 27 3b  = c + 'a' - 'A';
38f01 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63  .    }else if( c
38f02 3e 3d 27 61 27 20 26 26 20 63 3c 3d 27 7a 27 20  >='a' && c<='z' 
38f03 29 7b 0a 20 20 20 20 20 20 7a 52 65 76 65 72 73  ){.      zRevers
38f04 65 5b 6a 5d 20 3d 20 63 3b 0a 20 20 20 20 7d 65  e[j] = c;.    }e
38f05 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  lse{.      /* Th
38f06 65 20 75 73 65 20 6f 66 20 61 20 63 68 61 72 61  e use of a chara
38f07 63 74 65 72 20 6e 6f 74 20 69 6e 20 5b 61 2d 7a  cter not in [a-z
38f08 41 2d 5a 5d 20 6d 65 61 6e 73 20 74 68 61 74 20  A-Z] means that 
38f09 77 65 20 66 61 6c 6c 62 61 63 6b 0a 20 20 20 20  we fallback.    
38f0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 63 6f 70 79    ** to the copy
38f0b 20 73 74 65 6d 6d 65 72 20 2a 2f 0a 20 20 20 20   stemmer */.    
38f0c 20 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 7a    copy_stemmer(z
38f0d 49 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c 20 70  In, nIn, zOut, p
38f0e 6e 4f 75 74 29 3b 0a 20 20 20 20 20 20 72 65 74  nOut);.      ret
38f0f 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
38f10 20 6d 65 6d 73 65 74 28 26 7a 52 65 76 65 72 73   memset(&zRevers
38f11 65 5b 73 69 7a 65 6f 66 28 7a 52 65 76 65 72 73  e[sizeof(zRevers
38f12 65 29 2d 35 5d 2c 20 30 2c 20 35 29 3b 0a 20 20  e)-5], 0, 5);.  
38f13 7a 20 3d 20 26 7a 52 65 76 65 72 73 65 5b 6a 2b  z = &zReverse[j+
38f14 31 5d 3b 0a 0a 0a 20 20 2f 2a 20 53 74 65 70 20  1];...  /* Step 
38f15 31 61 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d  1a */.  if( z[0]
38f16 3d 3d 27 73 27 20 29 7b 0a 20 20 20 20 69 66 28  =='s' ){.    if(
38f17 0a 20 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20  .     !stem(&z, 
38f18 22 73 65 73 73 22 2c 20 22 73 73 22 2c 20 30 29  "sess", "ss", 0)
38f19 20 26 26 0a 20 20 20 20 20 21 73 74 65 6d 28 26   &&.     !stem(&
38f1a 7a 2c 20 22 73 65 69 22 2c 20 22 69 22 2c 20 30  z, "sei", "i", 0
38f1b 29 20 20 26 26 0a 20 20 20 20 20 21 73 74 65 6d  )  &&.     !stem
38f1c 28 26 7a 2c 20 22 73 73 22 2c 20 22 73 73 22 2c  (&z, "ss", "ss",
38f1d 20 30 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20   0).    ){.     
38f1e 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   z++;.    }.  }.
38f1f 0a 20 20 2f 2a 20 53 74 65 70 20 31 62 20 2a 2f  .  /* Step 1b */
38f20 20 20 0a 20 20 7a 32 20 3d 20 7a 3b 0a 20 20 69    .  z2 = z;.  i
38f21 66 28 20 73 74 65 6d 28 26 7a 2c 20 22 64 65 65  f( stem(&z, "dee
38f22 22 2c 20 22 65 65 22 2c 20 6d 5f 67 74 5f 30 29  ", "ee", m_gt_0)
38f23 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f   ){.    /* Do no
38f24 74 68 69 6e 67 2e 20 20 54 68 65 20 77 6f 72 6b  thing.  The work
38f25 20 77 61 73 20 61 6c 6c 20 69 6e 20 74 68 65 20   was all in the 
38f26 74 65 73 74 20 2a 2f 0a 20 20 7d 65 6c 73 65 20  test */.  }else 
38f27 69 66 28 20 0a 20 20 20 20 20 28 73 74 65 6d 28  if( .     (stem(
38f28 26 7a 2c 20 22 67 6e 69 22 2c 20 22 22 2c 20 68  &z, "gni", "", h
38f29 61 73 56 6f 77 65 6c 29 20 7c 7c 20 73 74 65 6d  asVowel) || stem
38f2a 28 26 7a 2c 20 22 64 65 22 2c 20 22 22 2c 20 68  (&z, "de", "", h
38f2b 61 73 56 6f 77 65 6c 29 29 0a 20 20 20 20 20 20  asVowel)).      
38f2c 26 26 20 7a 21 3d 7a 32 0a 20 20 29 7b 0a 20 20  && z!=z2.  ){.  
38f2d 20 20 20 69 66 28 20 73 74 65 6d 28 26 7a 2c 20     if( stem(&z, 
38f2e 22 74 61 22 2c 20 22 61 74 65 22 2c 20 30 29 20  "ta", "ate", 0) 
38f2f 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
38f30 28 26 7a 2c 20 22 6c 62 22 2c 20 22 62 6c 65 22  (&z, "lb", "ble"
38f31 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20 20 20 20  , 0) ||.        
38f32 20 73 74 65 6d 28 26 7a 2c 20 22 7a 69 22 2c 20   stem(&z, "zi", 
38f33 22 69 7a 65 22 2c 20 30 29 20 29 7b 0a 20 20 20  "ize", 0) ){.   
38f34 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e      /* Do nothin
38f35 67 2e 20 20 54 68 65 20 77 6f 72 6b 20 77 61 73  g.  The work was
38f36 20 61 6c 6c 20 69 6e 20 74 68 65 20 74 65 73 74   all in the test
38f37 20 2a 2f 0a 20 20 20 20 20 7d 65 6c 73 65 20 69   */.     }else i
38f38 66 28 20 64 6f 75 62 6c 65 43 6f 6e 73 6f 6e 61  f( doubleConsona
38f39 6e 74 28 7a 29 20 26 26 20 28 2a 7a 21 3d 27 6c  nt(z) && (*z!='l
38f3a 27 20 26 26 20 2a 7a 21 3d 27 73 27 20 26 26 20  ' && *z!='s' && 
38f3b 2a 7a 21 3d 27 7a 27 29 20 29 7b 0a 20 20 20 20  *z!='z') ){.    
38f3c 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 20 7d 65 6c     z++;.     }el
38f3d 73 65 20 69 66 28 20 6d 5f 65 71 5f 31 28 7a 29  se if( m_eq_1(z)
38f3e 20 26 26 20 73 74 61 72 5f 6f 68 28 7a 29 20 29   && star_oh(z) )
38f3f 7b 0a 20 20 20 20 20 20 20 2a 28 2d 2d 7a 29 20  {.       *(--z) 
38f40 3d 20 27 65 27 3b 0a 20 20 20 20 20 7d 0a 20 20  = 'e';.     }.  
38f41 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 31 63 20  }..  /* Step 1c 
38f42 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27  */.  if( z[0]=='
38f43 79 27 20 26 26 20 68 61 73 56 6f 77 65 6c 28 7a  y' && hasVowel(z
38f44 2b 31 29 20 29 7b 0a 20 20 20 20 7a 5b 30 5d 20  +1) ){.    z[0] 
38f45 3d 20 27 69 27 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  = 'i';.  }..  /*
38f46 20 53 74 65 70 20 32 20 2a 2f 0a 20 20 73 77 69   Step 2 */.  swi
38f47 74 63 68 28 20 7a 5b 31 5d 20 29 7b 0a 20 20 20  tch( z[1] ){.   
38f48 63 61 73 65 20 27 61 27 3a 0a 20 20 20 20 20 73  case 'a':.     s
38f49 74 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69 74 61  tem(&z, "lanoita
38f4a 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f 30  ", "ate", m_gt_0
38f4b 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
38f4c 7a 2c 20 22 6c 61 6e 6f 69 74 22 2c 20 22 74 69  z, "lanoit", "ti
38f4d 6f 6e 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20  on", m_gt_0);.  
38f4e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
38f4f 65 20 27 63 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 'c':.     stem
38f50 28 26 7a 2c 20 22 69 63 6e 65 22 2c 20 22 65 6e  (&z, "icne", "en
38f51 63 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ce", m_gt_0) ||.
38f52 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
38f53 63 6e 61 22 2c 20 22 61 6e 63 65 22 2c 20 6d 5f  cna", "ance", m_
38f54 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
38f55 6b 3b 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a  k;.   case 'e':.
38f56 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 72       stem(&z, "r
38f57 65 7a 69 22 2c 20 22 69 7a 65 22 2c 20 6d 5f 67  ezi", "ize", m_g
38f58 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
38f59 3b 0a 20 20 20 63 61 73 65 20 27 67 27 3a 0a 20  ;.   case 'g':. 
38f5a 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 67      stem(&z, "ig
38f5b 6f 6c 22 2c 20 22 6c 6f 67 22 2c 20 6d 5f 67 74  ol", "log", m_gt
38f5c 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
38f5d 0a 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20  .   case 'l':.  
38f5e 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 62     stem(&z, "ilb
38f5f 22 2c 20 22 62 6c 65 22 2c 20 6d 5f 67 74 5f 30  ", "ble", m_gt_0
38f60 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
38f61 7a 2c 20 22 69 6c 6c 61 22 2c 20 22 61 6c 22 2c  z, "illa", "al",
38f62 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
38f63 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 74 6e 65   stem(&z, "iltne
38f64 22 2c 20 22 65 6e 74 22 2c 20 6d 5f 67 74 5f 30  ", "ent", m_gt_0
38f65 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
38f66 7a 2c 20 22 69 6c 65 22 2c 20 22 65 22 2c 20 6d  z, "ile", "e", m
38f67 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73  _gt_0) ||.     s
38f68 74 65 6d 28 26 7a 2c 20 22 69 6c 73 75 6f 22 2c  tem(&z, "ilsuo",
38f69 20 22 6f 75 73 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ous", m_gt_0);
38f6a 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
38f6b 63 61 73 65 20 27 6f 27 3a 0a 20 20 20 20 20 73  case 'o':.     s
38f6c 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 74 61 7a 69  tem(&z, "noitazi
38f6d 22 2c 20 22 69 7a 65 22 2c 20 6d 5f 67 74 5f 30  ", "ize", m_gt_0
38f6e 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
38f6f 7a 2c 20 22 6e 6f 69 74 61 22 2c 20 22 61 74 65  z, "noita", "ate
38f70 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
38f71 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 72 6f 74     stem(&z, "rot
38f72 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f  a", "ate", m_gt_
38f73 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  0);.     break;.
38f74 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20     case 's':.   
38f75 20 20 73 74 65 6d 28 26 7a 2c 20 22 6d 73 69 6c    stem(&z, "msil
38f76 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30  a", "al", m_gt_0
38f77 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
38f78 7a 2c 20 22 73 73 65 6e 65 76 69 22 2c 20 22 69  z, "ssenevi", "i
38f79 76 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ve", m_gt_0) ||.
38f7a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
38f7b 73 65 6e 6c 75 66 22 2c 20 22 66 75 6c 22 2c 20  senluf", "ful", 
38f7c 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
38f7d 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 73 75  stem(&z, "ssensu
38f7e 6f 22 2c 20 22 6f 75 73 22 2c 20 6d 5f 67 74 5f  o", "ous", m_gt_
38f7f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  0);.     break;.
38f80 20 20 20 63 61 73 65 20 27 74 27 3a 0a 20 20 20     case 't':.   
38f81 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 6c    stem(&z, "itil
38f82 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30  a", "al", m_gt_0
38f83 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
38f84 7a 2c 20 22 69 74 69 76 69 22 2c 20 22 69 76 65  z, "itivi", "ive
38f85 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
38f86 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69     stem(&z, "iti
38f87 6c 69 62 22 2c 20 22 62 6c 65 22 2c 20 6d 5f 67  lib", "ble", m_g
38f88 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
38f89 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  ;.  }..  /* Step
38f8a 20 33 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20   3 */.  switch( 
38f8b 7a 5b 30 5d 20 29 7b 0a 20 20 20 63 61 73 65 20  z[0] ){.   case 
38f8c 27 65 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'e':.     stem(&
38f8d 7a 2c 20 22 65 74 61 63 69 22 2c 20 22 69 63 22  z, "etaci", "ic"
38f8e 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
38f8f 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 76 69 74    stem(&z, "evit
38f90 61 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29 20  a", "", m_gt_0) 
38f91 20 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26    ||.     stem(&
38f92 7a 2c 20 22 65 7a 69 6c 61 22 2c 20 22 61 6c 22  z, "ezila", "al"
38f93 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20  , m_gt_0);.     
38f94 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
38f95 69 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  i':.     stem(&z
38f96 2c 20 22 69 74 69 63 69 22 2c 20 22 69 63 22 2c  , "itici", "ic",
38f97 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
38f98 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6c  reak;.   case 'l
38f99 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
38f9a 20 22 6c 61 63 69 22 2c 20 22 69 63 22 2c 20 6d   "laci", "ic", m
38f9b 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73  _gt_0) ||.     s
38f9c 74 65 6d 28 26 7a 2c 20 22 6c 75 66 22 2c 20 22  tem(&z, "luf", "
38f9d 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
38f9e 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
38f9f 27 73 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  's':.     stem(&
38fa0 7a 2c 20 22 73 73 65 6e 22 2c 20 22 22 2c 20 6d  z, "ssen", "", m
38fa1 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65  _gt_0);.     bre
38fa2 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  ak;.  }..  /* St
38fa3 65 70 20 34 20 2a 2f 0a 20 20 73 77 69 74 63 68  ep 4 */.  switch
38fa4 28 20 7a 5b 31 5d 20 29 7b 0a 20 20 20 63 61 73  ( z[1] ){.   cas
38fa5 65 20 27 61 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'a':.     if( 
38fa6 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20 6d 5f 67  z[0]=='l' && m_g
38fa7 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20  t_1(z+2) ){.    
38fa8 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20     z += 2;.     
38fa9 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  }.     break;.  
38faa 20 63 61 73 65 20 27 63 27 3a 0a 20 20 20 20 20   case 'c':.     
38fab 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26  if( z[0]=='e' &&
38fac 20 7a 5b 32 5d 3d 3d 27 6e 27 20 26 26 20 28 7a   z[2]=='n' && (z
38fad 5b 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b 33 5d  [3]=='a' || z[3]
38fae 3d 3d 27 65 27 29 20 20 26 26 20 6d 5f 67 74 5f  =='e')  && m_gt_
38faf 31 28 7a 2b 34 29 20 20 29 7b 0a 20 20 20 20 20  1(z+4)  ){.     
38fb0 20 20 7a 20 2b 3d 20 34 3b 0a 20 20 20 20 20 7d    z += 4;.     }
38fb1 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
38fb2 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20 69  case 'e':.     i
38fb3 66 28 20 7a 5b 30 5d 3d 3d 27 72 27 20 26 26 20  f( z[0]=='r' && 
38fb4 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20  m_gt_1(z+2) ){. 
38fb5 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20        z += 2;.  
38fb6 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b     }.     break;
38fb7 0a 20 20 20 63 61 73 65 20 27 69 27 3a 0a 20 20  .   case 'i':.  
38fb8 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 63 27     if( z[0]=='c'
38fb9 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20   && m_gt_1(z+2) 
38fba 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 32  ){.       z += 2
38fbb 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72  ;.     }.     br
38fbc 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6c 27  eak;.   case 'l'
38fbd 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
38fbe 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 62  ='e' && z[2]=='b
38fbf 27 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61 27 20  ' && (z[3]=='a' 
38fc0 7c 7c 20 7a 5b 33 5d 3d 3d 27 69 27 29 20 26 26  || z[3]=='i') &&
38fc1 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 29 7b 0a   m_gt_1(z+4) ){.
38fc2 20 20 20 20 20 20 20 7a 20 2b 3d 20 34 3b 0a 20         z += 4;. 
38fc3 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b      }.     break
38fc4 3b 0a 20 20 20 63 61 73 65 20 27 6e 27 3a 0a 20  ;.   case 'n':. 
38fc5 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 74      if( z[0]=='t
38fc6 27 20 29 7b 0a 20 20 20 20 20 20 20 69 66 28 20  ' ){.       if( 
38fc7 7a 5b 32 5d 3d 3d 27 61 27 20 29 7b 0a 20 20 20  z[2]=='a' ){.   
38fc8 20 20 20 20 20 20 69 66 28 20 6d 5f 67 74 5f 31        if( m_gt_1
38fc9 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+3) ){.       
38fca 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20      z += 3;.    
38fcb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 7d 65       }.       }e
38fcc 6c 73 65 20 69 66 28 20 7a 5b 32 5d 3d 3d 27 65  lse if( z[2]=='e
38fcd 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 73 74  ' ){.         st
38fce 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 65 22 2c 20  em(&z, "tneme", 
38fcf 22 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20  "", m_gt_1) ||. 
38fd0 20 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c          stem(&z,
38fd1 20 22 74 6e 65 6d 22 2c 20 22 22 2c 20 6d 5f 67   "tnem", "", m_g
38fd2 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20 20 20  t_1) ||.        
38fd3 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65 22 2c   stem(&z, "tne",
38fd4 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20   "", m_gt_1);.  
38fd5 20 20 20 20 20 7d 0a 20 20 20 20 20 7d 0a 20 20       }.     }.  
38fd6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
38fd7 65 20 27 6f 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'o':.     if( 
38fd8 7a 5b 30 5d 3d 3d 27 75 27 20 29 7b 0a 20 20 20  z[0]=='u' ){.   
38fd9 20 20 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a      if( m_gt_1(z
38fda 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  +2) ){.         
38fdb 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 7d  z += 2;.       }
38fdc 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  .     }else if( 
38fdd 7a 5b 33 5d 3d 3d 27 73 27 20 7c 7c 20 7a 5b 33  z[3]=='s' || z[3
38fde 5d 3d 3d 27 74 27 20 29 7b 0a 20 20 20 20 20 20  ]=='t' ){.      
38fdf 20 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 22 2c   stem(&z, "noi",
38fe0 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20   "", m_gt_1);.  
38fe1 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b     }.     break;
38fe2 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20  .   case 's':.  
38fe3 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 6d 27     if( z[0]=='m'
38fe4 20 26 26 20 7a 5b 32 5d 3d 3d 27 69 27 20 26 26   && z[2]=='i' &&
38fe5 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a   m_gt_1(z+3) ){.
38fe6 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20         z += 3;. 
38fe7 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b      }.     break
38fe8 3b 0a 20 20 20 63 61 73 65 20 27 74 27 3a 0a 20  ;.   case 't':. 
38fe9 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 74      stem(&z, "et
38fea 61 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20  a", "", m_gt_1) 
38feb 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ||.     stem(&z,
38fec 20 22 69 74 69 22 2c 20 22 22 2c 20 6d 5f 67 74   "iti", "", m_gt
38fed 5f 31 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _1);.     break;
38fee 0a 20 20 20 63 61 73 65 20 27 75 27 3a 0a 20 20  .   case 'u':.  
38fef 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 73 27     if( z[0]=='s'
38ff0 20 26 26 20 7a 5b 32 5d 3d 3d 27 6f 27 20 26 26   && z[2]=='o' &&
38ff1 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a   m_gt_1(z+3) ){.
38ff2 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20         z += 3;. 
38ff3 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b      }.     break
38ff4 3b 0a 20 20 20 63 61 73 65 20 27 76 27 3a 0a 20  ;.   case 'v':. 
38ff5 20 20 63 61 73 65 20 27 7a 27 3a 0a 20 20 20 20    case 'z':.    
38ff6 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 26   if( z[0]=='e' &
38ff7 26 20 7a 5b 32 5d 3d 3d 27 69 27 20 26 26 20 6d  & z[2]=='i' && m
38ff8 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20  _gt_1(z+3) ){.  
38ff9 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20       z += 3;.   
38ffa 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
38ffb 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35    }..  /* Step 5
38ffc 61 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d 3d  a */.  if( z[0]=
38ffd 3d 27 65 27 20 29 7b 0a 20 20 20 20 69 66 28 20  ='e' ){.    if( 
38ffe 6d 5f 67 74 5f 31 28 7a 2b 31 29 20 29 7b 0a 20  m_gt_1(z+1) ){. 
38fff 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65       z++;.    }e
39000 6c 73 65 20 69 66 28 20 6d 5f 65 71 5f 31 28 7a  lse if( m_eq_1(z
39001 2b 31 29 20 26 26 20 21 73 74 61 72 5f 6f 68 28  +1) && !star_oh(
39002 7a 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 7a 2b  z+1) ){.      z+
39003 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  +;.    }.  }..  
39004 2f 2a 20 53 74 65 70 20 35 62 20 2a 2f 0a 20 20  /* Step 5b */.  
39005 69 66 28 20 6d 5f 67 74 5f 31 28 7a 29 20 26 26  if( m_gt_1(z) &&
39006 20 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20 7a 5b   z[0]=='l' && z[
39007 31 5d 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 7a  1]=='l' ){.    z
39008 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 7a 5b  ++;.  }..  /* z[
39009 5d 20 69 73 20 6e 6f 77 20 74 68 65 20 73 74 65  ] is now the ste
3900a 6d 6d 65 64 20 77 6f 72 64 20 69 6e 20 72 65 76  mmed word in rev
3900b 65 72 73 65 20 6f 72 64 65 72 2e 20 20 46 6c 69  erse order.  Fli
3900c 70 20 69 74 20 62 61 63 6b 0a 20 20 2a 2a 20 61  p it back.  ** a
3900d 72 6f 75 6e 64 20 69 6e 74 6f 20 66 6f 72 77 61  round into forwa
3900e 72 64 20 6f 72 64 65 72 20 61 6e 64 20 72 65 74  rd order and ret
3900f 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 2a 70 6e 4f  urn..  */.  *pnO
39010 75 74 20 3d 20 69 20 3d 20 73 74 72 6c 65 6e 28  ut = i = strlen(
39011 7a 29 3b 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d 20  z);.  zOut[i] = 
39012 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 20 29  0;.  while( *z )
39013 7b 0a 20 20 20 20 7a 4f 75 74 5b 2d 2d 69 5d 20  {.    zOut[--i] 
39014 3d 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d 0a 7d 0a  = *(z++);.  }.}.
39015 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 72  ./*.** Character
39016 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 61  s that can be pa
39017 72 74 20 6f 66 20 61 20 74 6f 6b 65 6e 2e 20 20  rt of a token.  
39018 57 65 20 61 73 73 75 6d 65 20 61 6e 79 20 63 68  We assume any ch
39019 61 72 61 63 74 65 72 0a 2a 2a 20 77 68 6f 73 65  aracter.** whose
3901a 20 76 61 6c 75 65 20 69 73 20 67 72 65 61 74 65   value is greate
3901b 72 20 74 68 61 6e 20 30 78 38 30 20 28 61 6e 79  r than 0x80 (any
3901c 20 55 54 46 20 63 68 61 72 61 63 74 65 72 29 20   UTF character) 
3901d 63 61 6e 20 62 65 0a 2a 2a 20 70 61 72 74 20 6f  can be.** part o
3901e 66 20 61 20 74 6f 6b 65 6e 2e 20 20 49 6e 20 6f  f a token.  In o
3901f 74 68 65 72 20 77 6f 72 64 73 2c 20 64 65 6c 69  ther words, deli
39020 6d 69 74 65 72 73 20 61 6c 6c 20 6d 75 73 74 20  miters all must 
39021 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73 20 6f  have.** values o
39022 66 20 30 78 37 66 20 6f 72 20 6c 6f 77 65 72 2e  f 0x7f or lower.
39023 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
39024 20 63 68 61 72 20 70 6f 72 74 65 72 49 64 43 68   char porterIdCh
39025 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78  ar[] = {./* x0 x
39026 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36  1 x2 x3 x4 x5 x6
39027 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20   x7 x8 x9 xA xB 
39028 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20  xC xD xE xF */. 
39029 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31     1, 1, 1, 1, 1
3902a 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3902b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
3902c 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20  0,  /* 3x */.   
3902d 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
3902e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3902f 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
39030 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31    /* 4x */.    1
39031 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
39032 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
39033 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20  0, 0, 0, 0, 1,  
39034 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20  /* 5x */.    0, 
39035 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
39036 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
39037 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a   1, 1, 1, 1,  /*
39038 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c   6x */.    1, 1,
39039 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3903a 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
3903b 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37  , 0, 0, 0,  /* 7
3903c 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20  x */.};.#define 
3903d 69 73 44 65 6c 69 6d 28 43 29 20 28 28 28 63 68  isDelim(C) (((ch
3903e 3d 43 29 26 30 78 38 30 29 3d 3d 30 20 26 26 20  =C)&0x80)==0 && 
3903f 28 63 68 3c 30 78 33 30 20 7c 7c 20 21 70 6f 72  (ch<0x30 || !por
39040 74 65 72 49 64 43 68 61 72 5b 63 68 2d 30 78 33  terIdChar[ch-0x3
39041 30 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72  0]))../*.** Extr
39042 61 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b  act the next tok
39043 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69  en from a tokeni
39044 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e 20 20  zation cursor.  
39045 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 0a  The cursor must.
39046 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65  ** have been ope
39047 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63  ned by a prior c
39048 61 6c 6c 20 74 6f 20 70 6f 72 74 65 72 4f 70 65  all to porterOpe
39049 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  n()..*/.static i
3904a 6e 74 20 70 6f 72 74 65 72 4e 65 78 74 28 0a 20  nt porterNext(. 
3904b 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3904c 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  er_cursor *pCurs
3904d 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72  or,  /* Cursor r
3904e 65 74 75 72 6e 65 64 20 62 79 20 70 6f 72 74 65  eturned by porte
3904f 72 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  rOpen */.  const
39050 20 63 68 61 72 20 2a 2a 70 7a 54 6f 6b 65 6e 2c   char **pzToken,
39051 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39052 2a 20 4f 55 54 3a 20 2a 70 7a 54 6f 6b 65 6e 20  * OUT: *pzToken 
39053 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78  is the token tex
39054 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79  t */.  int *pnBy
39055 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
39056 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
39057 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
39058 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20  es in token */. 
39059 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66   int *piStartOff
3905a 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
3905b 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61       /* OUT: Sta
3905c 72 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  rting offset of 
3905d 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
3905e 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20  piEndOffset,    
3905f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39060 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66  * OUT: Ending of
39061 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  fset of token */
39062 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69  .  int *piPositi
39063 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
39064 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50         /* OUT: P
39065 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20  osition integer 
39066 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  of token */.){. 
39067 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65   porter_tokenize
39068 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 70  r_cursor *c = (p
39069 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
3906a 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
3906b 72 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  r;.  const char 
3906c 2a 7a 20 3d 20 63 2d 3e 7a 49 6e 70 75 74 3b 0a  *z = c->zInput;.
3906d 0a 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66  .  while( c->iOf
3906e 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 29  fset<c->nInput )
3906f 7b 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74  {.    int iStart
39070 4f 66 66 73 65 74 2c 20 63 68 3b 0a 0a 20 20 20  Offset, ch;..   
39071 20 2f 2a 20 53 63 61 6e 20 70 61 73 74 20 64 65   /* Scan past de
39072 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63 74 65  limiter characte
39073 72 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  rs */.    while(
39074 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e   c->iOffset<c->n
39075 49 6e 70 75 74 20 26 26 20 69 73 44 65 6c 69 6d  Input && isDelim
39076 28 7a 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20  (z[c->iOffset]) 
39077 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66 66  ){.      c->iOff
39078 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  set++;.    }..  
39079 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f 6e 2d 64    /* Count non-d
3907a 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63 74  elimiter charact
3907b 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69 53 74 61  ers. */.    iSta
3907c 72 74 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f  rtOffset = c->iO
3907d 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c 65  ffset;.    while
3907e 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e  ( c->iOffset<c->
3907f 6e 49 6e 70 75 74 20 26 26 20 21 69 73 44 65 6c  nInput && !isDel
39080 69 6d 28 7a 5b 63 2d 3e 69 4f 66 66 73 65 74 5d  im(z[c->iOffset]
39081 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f  ) ){.      c->iO
39082 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  ffset++;.    }..
39083 20 20 20 20 69 66 28 20 63 2d 3e 69 4f 66 66 73      if( c->iOffs
39084 65 74 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20  et>iStartOffset 
39085 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d  ){.      int n =
39086 20 63 2d 3e 69 4f 66 66 73 65 74 2d 69 53 74 61   c->iOffset-iSta
39087 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  rtOffset;.      
39088 69 66 28 20 6e 3e 63 2d 3e 6e 41 6c 6c 6f 63 61  if( n>c->nAlloca
39089 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ted ){.        c
3908a 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 6e  ->nAllocated = n
3908b 2b 32 30 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e  +20;.        c->
3908c 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33  zToken = sqlite3
3908d 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e 7a 54 6f 6b  _realloc(c->zTok
3908e 65 6e 2c 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65  en, c->nAllocate
3908f 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
39090 63 2d 3e 7a 54 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20  c->zToken==NULL 
39091 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
39092 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
39093 20 20 20 20 20 70 6f 72 74 65 72 5f 73 74 65 6d       porter_stem
39094 6d 65 72 28 26 7a 5b 69 53 74 61 72 74 4f 66 66  mer(&z[iStartOff
39095 73 65 74 5d 2c 20 6e 2c 20 63 2d 3e 7a 54 6f 6b  set], n, c->zTok
39096 65 6e 2c 20 70 6e 42 79 74 65 73 29 3b 0a 20 20  en, pnBytes);.  
39097 20 20 20 20 2a 70 7a 54 6f 6b 65 6e 20 3d 20 63      *pzToken = c
39098 2d 3e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  ->zToken;.      
39099 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d  *piStartOffset =
3909a 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20   iStartOffset;. 
3909b 20 20 20 20 20 2a 70 69 45 6e 64 4f 66 66 73 65       *piEndOffse
3909c 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a  t = c->iOffset;.
3909d 20 20 20 20 20 20 2a 70 69 50 6f 73 69 74 69 6f        *piPositio
3909e 6e 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b  n = c->iToken++;
3909f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
390a0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
390a1 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
390a2 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a  TE_DONE;.}../*.*
390a3 2a 20 54 68 65 20 73 65 74 20 6f 66 20 72 6f 75  * The set of rou
390a4 74 69 6e 65 73 20 74 68 61 74 20 69 6d 70 6c 65  tines that imple
390a5 6d 65 6e 74 20 74 68 65 20 70 6f 72 74 65 72 2d  ment the porter-
390a6 73 74 65 6d 6d 65 72 20 74 6f 6b 65 6e 69 7a 65  stemmer tokenize
390a7 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  r.*/.static cons
390a8 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
390a9 7a 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72 74 65  zer_module porte
390aa 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  rTokenizerModule
390ab 20 3d 20 7b 0a 20 20 30 2c 0a 20 20 70 6f 72 74   = {.  0,.  port
390ac 65 72 43 72 65 61 74 65 2c 0a 20 20 70 6f 72 74  erCreate,.  port
390ad 65 72 44 65 73 74 72 6f 79 2c 0a 20 20 70 6f 72  erDestroy,.  por
390ae 74 65 72 4f 70 65 6e 2c 0a 20 20 70 6f 72 74 65  terOpen,.  porte
390af 72 43 6c 6f 73 65 2c 0a 20 20 70 6f 72 74 65 72  rClose,.  porter
390b0 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  Next,.};../*.** 
390b1 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  Allocate a new p
390b2 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65 72 2e  orter tokenizer.
390b3 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
390b4 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a 2a  er to the new.**
390b5 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 20 2a 70   tokenizer in *p
390b6 70 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51 4c 49 54  pModule.*/.SQLIT
390b7 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
390b8 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65 72  qlite3Fts3Porter
390b9 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
390ba 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
390bb 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
390bc 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20  t**ppModule.){. 
390bd 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 70 6f   *ppModule = &po
390be 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  rterTokenizerMod
390bf 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  ule;.}..#endif /
390c0 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
390c1 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
390c2 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
390c3 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
390c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
390c5 66 20 66 74 73 33 5f 70 6f 72 74 65 72 2e 63 20  f fts3_porter.c 
390c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
390c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
390ca 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e   file fts3_token
390cb 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  izer.c *********
390cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
390ce 20 32 30 30 37 20 4a 75 6e 65 20 32 32 0a 2a 2a   2007 June 22.**
390cf 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
390d0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
390d1 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
390d2 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
390d3 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
390d4 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
390d5 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
390d6 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
390d7 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
390d8 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
390d9 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
390da 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
390db 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
390dc 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
390dd 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
390de 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
390df 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
390e0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
390e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390e5 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  *.**.** This is 
390e6 70 61 72 74 20 6f 66 20 61 6e 20 53 51 4c 69 74  part of an SQLit
390e7 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  e module impleme
390e8 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78 74 20  nting full-text 
390e9 73 65 61 72 63 68 2e 0a 2a 2a 20 54 68 69 73 20  search..** This 
390ea 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20  particular file 
390eb 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 67  implements the g
390ec 65 6e 65 72 69 63 20 74 6f 6b 65 6e 69 7a 65 72  eneric tokenizer
390ed 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a   interface..*/..
390ee 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  /*.** The code i
390ef 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f  n this file is o
390f0 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a  nly compiled if:
390f1 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
390f2 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
390f3 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61  being built as a
390f4 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20  n extension.**  
390f5 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63       (in which c
390f6 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  ase SQLITE_CORE 
390f7 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
390f8 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20   or.**.**     * 
390f9 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
390fa 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69  is being built i
390fb 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a  nto the core of.
390fc 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20  **       SQLite 
390fd 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
390fe 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
390ff 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a  3 is defined)..*
39100 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
39101 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
39102 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
39103 41 42 4c 45 5f 46 54 53 33 29 0a 0a 23 69 66 6e  ABLE_FTS3)..#ifn
39104 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a  def SQLITE_CORE.
39105 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49    SQLITE_EXTENSI
39106 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69 66 0a  ON_INIT1.#endif.
39107 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
39108 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51  tation of the SQ
39109 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  L scalar functio
3910a 6e 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  n for accessing 
3910b 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 0a  the underlying .
3910c 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20 54  ** hash table. T
3910d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
3910e 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20 66 6f   be called as fo
3910f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53  llows:.**.**   S
39110 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d  ELECT <function-
39111 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e  name>(<key-name>
39112 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c  );.**   SELECT <
39113 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c  function-name>(<
39114 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f 69 6e  key-name>, <poin
39115 74 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77 68 65  ter>);.**.** whe
39116 72 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d  re <function-nam
39117 65 3e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 70  e> is the name p
39118 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
39119 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ond argument.** 
3911a 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 46 74  to the sqlite3Ft
3911b 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28  s3InitHashTable(
3911c 29 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e 67 2e  ) function (e.g.
3911d 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72   'fts3_tokenizer
3911e 27 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ')..**.** If the
3911f 20 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67 75 6d   <pointer> argum
39120 65 6e 74 20 69 73 20 73 70 65 63 69 66 69 65 64  ent is specified
39121 2c 20 69 74 20 6d 75 73 74 20 62 65 20 61 20 62  , it must be a b
39122 6c 6f 62 20 76 61 6c 75 65 0a 2a 2a 20 63 6f 6e  lob value.** con
39123 74 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65  taining a pointe
39124 72 20 74 6f 20 62 65 20 73 74 6f 72 65 64 20 61  r to be stored a
39125 73 20 74 68 65 20 68 61 73 68 20 64 61 74 61 20  s the hash data 
39126 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
39127 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67 20 3c   to the string <
39128 6b 65 79 2d 6e 61 6d 65 3e 2e 20 49 66 20 3c 70  key-name>. If <p
39129 6f 69 6e 74 65 72 3e 20 69 73 20 6e 6f 74 20 73  ointer> is not s
3912a 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 0a 2a  pecified, then.*
3912b 2a 20 74 68 65 20 73 74 72 69 6e 67 20 3c 6b 65  * the string <ke
3912c 79 2d 6e 61 6d 65 3e 20 6d 75 73 74 20 61 6c 72  y-name> must alr
3912d 65 61 64 79 20 65 78 69 73 74 20 69 6e 20 74 68  eady exist in th
3912e 65 20 68 61 73 20 74 61 62 6c 65 2e 20 4f 74 68  e has table. Oth
3912f 65 72 77 69 73 65 2c 0a 2a 2a 20 61 6e 20 65 72  erwise,.** an er
39130 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ror is returned.
39131 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f  .**.** Whether o
39132 72 20 6e 6f 74 20 74 68 65 20 3c 70 6f 69 6e 74  r not the <point
39133 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69 73 20  er> argument is 
39134 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 20 76  specified, the v
39135 61 6c 75 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a  alue returned.**
39136 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e 74 61   is a blob conta
39137 69 6e 69 6e 67 20 74 68 65 20 70 6f 69 6e 74 65  ining the pointe
39138 72 20 73 74 6f 72 65 64 20 61 73 20 74 68 65 20  r stored as the 
39139 68 61 73 68 20 64 61 74 61 20 63 6f 72 72 65 73  hash data corres
3913a 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 73 74  ponding.** to st
3913b 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 20  ring <key-name> 
3913c 28 61 66 74 65 72 20 74 68 65 20 68 61 73 68 2d  (after the hash-
3913d 74 61 62 6c 65 20 69 73 20 75 70 64 61 74 65 64  table is updated
3913e 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29  , if applicable)
3913f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
39140 20 73 63 61 6c 61 72 46 75 6e 63 28 0a 20 20 73   scalarFunc(.  s
39141 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
39142 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
39143 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
39144 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
39145 20 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68   fts3Hash *pHash
39146 3b 0a 20 20 76 6f 69 64 20 2a 70 50 74 72 20 3d  ;.  void *pPtr =
39147 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   0;.  const unsi
39148 67 6e 65 64 20 63 68 61 72 20 2a 7a 4e 61 6d 65  gned char *zName
39149 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a  ;.  int nName;..
3914a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
3914b 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a  1 || argc==2 );.
3914c 0a 20 20 70 48 61 73 68 20 3d 20 28 66 74 73 33  .  pHash = (fts3
3914d 48 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f 75  Hash *)sqlite3_u
3914e 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
3914f 29 3b 0a 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71  );..  zName = sq
39150 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
39151 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 4e 61  (argv[0]);.  nNa
39152 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  me = sqlite3_val
39153 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
39154 29 2b 31 3b 0a 0a 20 20 69 66 28 20 61 72 67 63  )+1;..  if( argc
39155 3d 3d 32 20 29 7b 0a 20 20 20 20 76 6f 69 64 20  ==2 ){.    void 
39156 2a 70 4f 6c 64 3b 0a 20 20 20 20 69 6e 74 20 6e  *pOld;.    int n
39157 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
39158 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d 29 3b  _bytes(argv[1]);
39159 0a 20 20 20 20 69 66 28 20 6e 21 3d 73 69 7a 65  .    if( n!=size
3915a 6f 66 28 70 50 74 72 29 20 29 7b 0a 20 20 20 20  of(pPtr) ){.    
3915b 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3915c 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
3915d 22 61 72 67 75 6d 65 6e 74 20 74 79 70 65 20 6d  "argument type m
3915e 69 73 6d 61 74 63 68 22 2c 20 2d 31 29 3b 0a 20  ismatch", -1);. 
3915f 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
39160 20 7d 0a 20 20 20 20 70 50 74 72 20 3d 20 2a 28   }.    pPtr = *(
39161 76 6f 69 64 20 2a 2a 29 73 71 6c 69 74 65 33 5f  void **)sqlite3_
39162 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
39163 31 5d 29 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20  1]);.    pOld = 
39164 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
39165 6e 73 65 72 74 28 70 48 61 73 68 2c 20 28 76 6f  nsert(pHash, (vo
39166 69 64 20 2a 29 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  id *)zName, nNam
39167 65 2c 20 70 50 74 72 29 3b 0a 20 20 20 20 69 66  e, pPtr);.    if
39168 28 20 70 4f 6c 64 3d 3d 70 50 74 72 20 29 7b 0a  ( pOld==pPtr ){.
39169 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
3916a 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
3916b 78 74 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f  xt, "out of memo
3916c 72 79 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  ry", -1);.      
3916d 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
3916e 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 74 72 20  }else{.    pPtr 
3916f 3d 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  = sqlite3Fts3Has
39170 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a 4e 61  hFind(pHash, zNa
39171 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
39172 69 66 28 20 21 70 50 74 72 20 29 7b 0a 20 20 20  if( !pPtr ){.   
39173 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20     char *zErr = 
39174 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
39175 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a  "unknown tokeniz
39176 65 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  er: %s", zName);
39177 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
39178 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
39179 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a  ext, zErr, -1);.
3917a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
3917b 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  ee(zErr);.      
3917c 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
3917d 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  }..  sqlite3_res
3917e 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74  ult_blob(context
3917f 2c 20 28 76 6f 69 64 20 2a 29 26 70 50 74 72 2c  , (void *)&pPtr,
39180 20 73 69 7a 65 6f 66 28 70 50 74 72 29 2c 20 53   sizeof(pPtr), S
39181 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
39182 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
39183 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a 2a 20  TE_TEST.../*.** 
39184 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
39185 66 20 61 20 73 70 65 63 69 61 6c 20 53 51 4c 20  f a special SQL 
39186 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
39187 66 6f 72 20 74 65 73 74 69 6e 67 20 74 6f 6b 65  for testing toke
39188 6e 69 7a 65 72 73 20 0a 2a 2a 20 64 65 73 69 67  nizers .** desig
39189 6e 65 64 20 74 6f 20 62 65 20 75 73 65 64 20 69  ned to be used i
3918a 6e 20 63 6f 6e 63 65 72 74 20 77 69 74 68 20 74  n concert with t
3918b 68 65 20 54 63 6c 20 74 65 73 74 69 6e 67 20 66  he Tcl testing f
3918c 72 61 6d 65 77 6f 72 6b 2e 20 54 68 69 73 0a 2a  ramework. This.*
3918d 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20  * function must 
3918e 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74  be called with t
3918f 77 6f 20 61 72 67 75 6d 65 6e 74 73 3a 0a 2a 2a  wo arguments:.**
39190 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66 75  .**   SELECT <fu
39191 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65  nction-name>(<ke
39192 79 2d 6e 61 6d 65 3e 2c 20 3c 69 6e 70 75 74 2d  y-name>, <input-
39193 73 74 72 69 6e 67 3e 29 3b 0a 2a 2a 20 20 20 53  string>);.**   S
39194 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d  ELECT <function-
39195 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e  name>(<key-name>
39196 2c 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a 2a 2a  , <pointer>);.**
39197 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e 63 74  .** where <funct
39198 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68 65  ion-name> is the
39199 20 6e 61 6d 65 20 70 61 73 73 65 64 20 61 73 20   name passed as 
3919a 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
3919b 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71  ent.** to the sq
3919c 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61 73  lite3Fts3InitHas
3919d 68 54 61 62 6c 65 28 29 20 66 75 6e 63 74 69 6f  hTable() functio
3919e 6e 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74 6f  n (e.g. 'fts3_to
3919f 6b 65 6e 69 7a 65 72 27 29 0a 2a 2a 20 63 6f 6e  kenizer').** con
391a0 63 61 74 65 6e 61 74 65 64 20 77 69 74 68 20 74  catenated with t
391a1 68 65 20 73 74 72 69 6e 67 20 27 5f 74 65 73 74  he string '_test
391a2 27 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74 6f  ' (e.g. 'fts3_to
391a3 6b 65 6e 69 7a 65 72 5f 74 65 73 74 27 29 2e 0a  kenizer_test')..
391a4 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  **.** The return
391a5 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 69   value is a stri
391a6 6e 67 20 74 68 61 74 20 6d 61 79 20 62 65 20 69  ng that may be i
391a7 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20  nterpreted as a 
391a8 54 63 6c 0a 2a 2a 20 6c 69 73 74 2e 20 46 6f 72  Tcl.** list. For
391a9 20 65 61 63 68 20 74 6f 6b 65 6e 20 69 6e 20 74   each token in t
391aa 68 65 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e 67  he <input-string
391ab 3e 2c 20 74 68 72 65 65 20 65 6c 65 6d 65 6e 74  >, three element
391ac 73 20 61 72 65 0a 2a 2a 20 61 64 64 65 64 20 74  s are.** added t
391ad 6f 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 6c  o the returned l
391ae 69 73 74 2e 20 54 68 65 20 66 69 72 73 74 20 69  ist. The first i
391af 73 20 74 68 65 20 74 6f 6b 65 6e 20 70 6f 73 69  s the token posi
391b0 74 69 6f 6e 2c 20 74 68 65 20 0a 2a 2a 20 73 65  tion, the .** se
391b1 63 6f 6e 64 20 69 73 20 74 68 65 20 74 6f 6b 65  cond is the toke
391b2 6e 20 74 65 78 74 20 28 66 6f 6c 64 65 64 2c 20  n text (folded, 
391b3 73 74 65 6d 6d 65 64 2c 20 65 74 63 2e 29 20 61  stemmed, etc.) a
391b4 6e 64 20 74 68 65 20 74 68 69 72 64 20 69 73 20  nd the third is 
391b5 74 68 65 0a 2a 2a 20 73 75 62 73 74 72 69 6e 67  the.** substring
391b6 20 6f 66 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e   of <input-strin
391b7 67 3e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  g> associated wi
391b8 74 68 20 74 68 65 20 74 6f 6b 65 6e 2e 20 46 6f  th the token. Fo
391b9 72 20 65 78 61 6d 70 6c 65 2c 20 0a 2a 2a 20 75  r example, .** u
391ba 73 69 6e 67 20 74 68 65 20 62 75 69 6c 74 2d 69  sing the built-i
391bb 6e 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b 65 6e  n "simple" token
391bc 69 7a 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45  izer:.**.**   SE
391bd 4c 45 43 54 20 66 74 73 5f 74 6f 6b 65 6e 69 7a  LECT fts_tokeniz
391be 65 72 5f 74 65 73 74 28 27 73 69 6d 70 6c 65 27  er_test('simple'
391bf 2c 20 27 49 20 64 6f 6e 27 74 20 73 65 65 20 68  , 'I don't see h
391c0 6f 77 27 29 3b 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c  ow');.**.** will
391c1 20 72 65 74 75 72 6e 20 74 68 65 20 73 74 72 69   return the stri
391c2 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 7b 30 20  ng:.**.**   "{0 
391c3 69 20 49 20 31 20 64 6f 6e 74 20 64 6f 6e 27 74  i I 1 dont don't
391c4 20 32 20 73 65 65 20 73 65 65 20 33 20 68 6f 77   2 see see 3 how
391c5 20 68 6f 77 7d 22 0a 2a 2a 20 20 20 0a 2a 2f 0a   how}".**   .*/.
391c6 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74  static void test
391c7 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
391c8 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
391c9 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
391ca 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
391cb 61 72 67 76 0a 29 7b 0a 20 20 66 74 73 33 48 61  argv.){.  fts3Ha
391cc 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 73 71 6c  sh *pHash;.  sql
391cd 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
391ce 6f 64 75 6c 65 20 2a 70 3b 0a 20 20 73 71 6c 69  odule *p;.  sqli
391cf 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
391d0 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 0a 20  Tokenizer = 0;. 
391d1 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
391d2 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 73 72 20  er_cursor *pCsr 
391d3 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68  = 0;..  const ch
391d4 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a 20  ar *zErr = 0;.. 
391d5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
391d6 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b  me;.  int nName;
391d7 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
391d8 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e 49 6e  Input;.  int nIn
391d9 70 75 74 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68  put;..  const ch
391da 61 72 20 2a 7a 41 72 67 20 3d 20 30 3b 0a 0a 20  ar *zArg = 0;.. 
391db 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f   const char *zTo
391dc 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65  ken;.  int nToke
391dd 6e 3b 0a 20 20 69 6e 74 20 69 53 74 61 72 74 3b  n;.  int iStart;
391de 0a 20 20 69 6e 74 20 69 45 6e 64 3b 0a 20 20 69  .  int iEnd;.  i
391df 6e 74 20 69 50 6f 73 3b 0a 0a 20 20 54 63 6c 5f  nt iPos;..  Tcl_
391e0 4f 62 6a 20 2a 70 52 65 74 3b 0a 0a 20 20 61 73  Obj *pRet;..  as
391e1 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20 7c 7c  sert( argc==2 ||
391e2 20 61 72 67 63 3d 3d 33 20 29 3b 0a 0a 20 20 6e   argc==3 );..  n
391e3 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  Name = sqlite3_v
391e4 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
391e5 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28  0]);.  zName = (
391e6 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
391e7 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
391e8 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 49 6e 70  argv[0]);.  nInp
391e9 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ut = sqlite3_val
391ea 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 61 72  ue_bytes(argv[ar
391eb 67 63 2d 31 5d 29 3b 0a 20 20 7a 49 6e 70 75 74  gc-1]);.  zInput
391ec 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
391ed 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
391ee 65 78 74 28 61 72 67 76 5b 61 72 67 63 2d 31 5d  ext(argv[argc-1]
391ef 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  );..  if( argc==
391f0 33 20 29 7b 0a 20 20 20 20 7a 41 72 67 20 3d 20  3 ){.    zArg = 
391f1 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
391f2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
391f3 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 0a 0a  (argv[1]);.  }..
391f4 20 20 70 48 61 73 68 20 3d 20 28 66 74 73 33 48    pHash = (fts3H
391f5 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f 75 73  ash *)sqlite3_us
391f6 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
391f7 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 33  ;.  p = (sqlite3
391f8 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
391f9 65 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33 48  e *)sqlite3Fts3H
391fa 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a  ashFind(pHash, z
391fb 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a  Name, nName+1);.
391fc 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20  .  if( !p ){.   
391fd 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 73 71   char *zErr = sq
391fe 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 75  lite3_mprintf("u
391ff 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72  nknown tokenizer
39200 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  : %s", zName);. 
39201 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
39202 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
39203 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20   zErr, -1);.    
39204 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
39205 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  r);.    return;.
39206 20 20 7d 0a 0a 20 20 70 52 65 74 20 3d 20 54 63    }..  pRet = Tc
39207 6c 5f 4e 65 77 4f 62 6a 28 29 3b 0a 20 20 54 63  l_NewObj();.  Tc
39208 6c 5f 49 6e 63 72 52 65 66 43 6f 75 6e 74 28 70  l_IncrRefCount(p
39209 52 65 74 29 3b 0a 0a 20 20 69 66 28 20 53 51 4c  Ret);..  if( SQL
3920a 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 72 65 61  ITE_OK!=p->xCrea
3920b 74 65 28 7a 41 72 67 20 3f 20 31 20 3a 20 30 2c  te(zArg ? 1 : 0,
3920c 20 26 7a 41 72 67 2c 20 26 70 54 6f 6b 65 6e 69   &zArg, &pTokeni
3920d 7a 65 72 29 20 29 7b 0a 20 20 20 20 7a 45 72 72  zer) ){.    zErr
3920e 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 43 72   = "error in xCr
3920f 65 61 74 65 28 29 22 3b 0a 20 20 20 20 67 6f 74  eate()";.    got
39210 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20  o finish;.  }.  
39211 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64  pTokenizer->pMod
39212 75 6c 65 20 3d 20 70 3b 0a 20 20 69 66 28 20 53  ule = p;.  if( S
39213 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 4f 70  QLITE_OK!=p->xOp
39214 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a  en(pTokenizer, z
39215 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 26  Input, nInput, &
39216 70 43 73 72 29 20 29 7b 0a 20 20 20 20 7a 45 72  pCsr) ){.    zEr
39217 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 4f  r = "error in xO
39218 70 65 6e 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f  pen()";.    goto
39219 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20 70   finish;.  }.  p
3921a 43 73 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20  Csr->pTokenizer 
3921b 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 20  = pTokenizer;.. 
3921c 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 4f   while( SQLITE_O
3921d 4b 3d 3d 70 2d 3e 78 4e 65 78 74 28 70 43 73 72  K==p->xNext(pCsr
3921e 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b  , &zToken, &nTok
3921f 65 6e 2c 20 26 69 53 74 61 72 74 2c 20 26 69 45  en, &iStart, &iE
39220 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a 20 20  nd, &iPos) ){.  
39221 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70    Tcl_ListObjApp
39222 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52  endElement(0, pR
39223 65 74 2c 20 54 63 6c 5f 4e 65 77 49 6e 74 4f 62  et, Tcl_NewIntOb
39224 6a 28 69 50 6f 73 29 29 3b 0a 20 20 20 20 54 63  j(iPos));.    Tc
39225 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45  l_ListObjAppendE
39226 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c 20  lement(0, pRet, 
39227 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62 6a  Tcl_NewStringObj
39228 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29  (zToken, nToken)
39229 29 3b 0a 20 20 20 20 7a 54 6f 6b 65 6e 20 3d 20  );.    zToken = 
3922a 26 7a 49 6e 70 75 74 5b 69 53 74 61 72 74 5d 3b  &zInput[iStart];
3922b 0a 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 69 45  .    nToken = iE
3922c 6e 64 2d 69 53 74 61 72 74 3b 0a 20 20 20 20 54  nd-iStart;.    T
3922d 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64  cl_ListObjAppend
3922e 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c  Element(0, pRet,
3922f 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62   Tcl_NewStringOb
39230 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  j(zToken, nToken
39231 29 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 53  ));.  }..  if( S
39232 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 6c  QLITE_OK!=p->xCl
39233 6f 73 65 28 70 43 73 72 29 20 29 7b 0a 20 20 20  ose(pCsr) ){.   
39234 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20 69   zErr = "error i
39235 6e 20 78 43 6c 6f 73 65 28 29 22 3b 0a 20 20 20  n xClose()";.   
39236 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20   goto finish;.  
39237 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  }.  if( SQLITE_O
39238 4b 21 3d 70 2d 3e 78 44 65 73 74 72 6f 79 28 70  K!=p->xDestroy(p
39239 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a 20 20  Tokenizer) ){.  
3923a 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20    zErr = "error 
3923b 69 6e 20 78 44 65 73 74 72 6f 79 28 29 22 3b 0a  in xDestroy()";.
3923c 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b      goto finish;
3923d 0a 20 20 7d 0a 0a 66 69 6e 69 73 68 3a 0a 20 20  .  }..finish:.  
3923e 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20 20  if( zErr ){.    
3923f 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
39240 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45  rror(context, zE
39241 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 65 6c 73 65  rr, -1);.  }else
39242 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
39243 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
39244 74 2c 20 54 63 6c 5f 47 65 74 53 74 72 69 6e 67  t, Tcl_GetString
39245 28 70 52 65 74 29 2c 20 2d 31 2c 20 53 51 4c 49  (pRet), -1, SQLI
39246 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
39247 20 7d 0a 20 20 54 63 6c 5f 44 65 63 72 52 65 66   }.  Tcl_DecrRef
39248 43 6f 75 6e 74 28 70 52 65 74 29 3b 0a 7d 0a 0a  Count(pRet);.}..
39249 73 74 61 74 69 63 0a 69 6e 74 20 72 65 67 69 73  static.int regis
3924a 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20  terTokenizer(.  
3924b 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
3924c 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20  char *zName, .  
3924d 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
3924e 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
3924f 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  p.){.  int rc;. 
39250 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
39251 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68  Stmt;.  const ch
39252 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c  ar zSql[] = "SEL
39253 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ECT fts3_tokeniz
39254 65 72 28 3f 2c 20 3f 29 22 3b 0a 0a 20 20 72 63  er(?, ?)";..  rc
39255 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61   = sqlite3_prepa
39256 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20  re_v2(db, zSql, 
39257 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a  -1, &pStmt, 0);.
39258 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
39259 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
3925a 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  n rc;.  }..  sql
3925b 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 70  ite3_bind_text(p
3925c 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20  Stmt, 1, zName, 
3925d 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  -1, SQLITE_STATI
3925e 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  C);.  sqlite3_bi
3925f 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 32  nd_blob(pStmt, 2
39260 2c 20 26 70 2c 20 73 69 7a 65 6f 66 28 70 29 2c  , &p, sizeof(p),
39261 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
39262 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
39263 70 53 74 6d 74 29 3b 0a 0a 20 20 72 65 74 75 72  pStmt);..  retur
39264 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  n sqlite3_finali
39265 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73 74  ze(pStmt);.}..st
39266 61 74 69 63 0a 69 6e 74 20 71 75 65 72 79 54 6f  atic.int queryTo
39267 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74  kenizer(.  sqlit
39268 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20  e3 *db, .  char 
39269 2a 7a 4e 61 6d 65 2c 20 20 0a 20 20 63 6f 6e 73  *zName,  .  cons
3926a 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
3926b 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70 0a  zer_module **pp.
3926c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
3926d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
3926e 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  mt;.  const char
3926f 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43   zSql[] = "SELEC
39270 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  T fts3_tokenizer
39271 28 3f 29 22 3b 0a 0a 20 20 2a 70 70 20 3d 20 30  (?)";..  *pp = 0
39272 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
39273 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
39274 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74  zSql, -1, &pStmt
39275 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
39276 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
39277 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
39278 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
39279 74 65 78 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a  text(pStmt, 1, z
3927a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45  Name, -1, SQLITE
3927b 5f 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20  _STATIC);.  if( 
3927c 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69  SQLITE_ROW==sqli
3927d 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20  te3_step(pStmt) 
3927e 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
3927f 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70  e3_column_type(p
39280 53 74 6d 74 2c 20 30 29 3d 3d 53 51 4c 49 54 45  Stmt, 0)==SQLITE
39281 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 6d  _BLOB ){.      m
39282 65 6d 63 70 79 28 70 70 2c 20 73 71 6c 69 74 65  emcpy(pp, sqlite
39283 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53  3_column_blob(pS
39284 74 6d 74 2c 20 30 29 2c 20 73 69 7a 65 6f 66 28  tmt, 0), sizeof(
39285 2a 70 70 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  *pp));.    }.  }
39286 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
39287 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
39288 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  t);.}..SQLITE_PR
39289 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
3928a 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65  e3Fts3SimpleToke
3928b 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69  nizerModule(sqli
3928c 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
3928d 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f  dule const**ppMo
3928e 64 75 6c 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6d  dule);../*.** Im
3928f 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
39290 74 68 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74  the scalar funct
39291 69 6f 6e 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ion fts3_tokeniz
39292 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74  er_internal_test
39293 28 29 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ()..** This func
39294 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72  tion is used for
39295 20 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2c 20 69   testing only, i
39296 74 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65  t is not include
39297 64 20 69 6e 20 74 68 65 0a 2a 2a 20 62 75 69 6c  d in the.** buil
39298 64 20 75 6e 6c 65 73 73 20 53 51 4c 49 54 45 5f  d unless SQLITE_
39299 54 45 53 54 20 69 73 20 64 65 66 69 6e 65 64 2e  TEST is defined.
3929a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f  .**.** The purpo
3929b 73 65 20 6f 66 20 74 68 69 73 20 69 73 20 74 6f  se of this is to
3929c 20 74 65 73 74 20 74 68 61 74 20 74 68 65 20 66   test that the f
3929d 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 29 20  ts3_tokenizer() 
3929e 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20  function.** can 
3929f 62 65 20 75 73 65 64 20 61 73 20 64 65 73 69 67  be used as desig
392a0 6e 65 64 20 62 79 20 74 68 65 20 43 2d 63 6f 64  ned by the C-cod
392a1 65 20 69 6e 20 74 68 65 20 71 75 65 72 79 54 6f  e in the queryTo
392a2 6b 65 6e 69 7a 65 72 20 61 6e 64 0a 2a 2a 20 72  kenizer and.** r
392a3 65 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72  egisterTokenizer
392a4 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 61 62 6f  () functions abo
392a5 76 65 2e 20 54 68 65 73 65 20 74 77 6f 20 66 75  ve. These two fu
392a6 6e 63 74 69 6f 6e 73 20 61 72 65 20 72 65 70 65  nctions are repe
392a7 61 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 52  ated.** in the R
392a8 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65 72 20  EADME.tokenizer 
392a9 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70  file as an examp
392aa 6c 65 2c 20 73 6f 20 69 74 20 69 73 20 69 6d 70  le, so it is imp
392ab 6f 72 74 61 6e 74 20 74 6f 0a 2a 2a 20 74 65 73  ortant to.** tes
392ac 74 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 6f  t them..**.** To
392ad 20 72 75 6e 20 74 68 65 20 74 65 73 74 73 2c 20   run the tests, 
392ae 65 76 61 6c 75 61 74 65 20 74 68 65 20 66 74 73  evaluate the fts
392af 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65  3_tokenizer_inte
392b0 72 6e 61 6c 5f 74 65 73 74 28 29 20 73 63 61 6c  rnal_test() scal
392b1 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77  ar.** function w
392b2 69 74 68 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73  ith no arguments
392b3 2e 20 41 6e 20 61 73 73 65 72 74 28 29 20 77 69  . An assert() wi
392b4 6c 6c 20 66 61 69 6c 20 69 66 20 61 20 70 72 6f  ll fail if a pro
392b5 62 6c 65 6d 20 69 73 0a 2a 2a 20 64 65 74 65 63  blem is.** detec
392b6 74 65 64 2e 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a  ted. i.e.:.**.**
392b7 20 20 20 20 20 53 45 4c 45 43 54 20 66 74 73 33       SELECT fts3
392b8 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65 72  _tokenizer_inter
392b9 6e 61 6c 5f 74 65 73 74 28 29 3b 0a 2a 2a 0a 2a  nal_test();.**.*
392ba 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
392bb 74 54 65 73 74 46 75 6e 63 28 0a 20 20 73 71 6c  tTestFunc(.  sql
392bc 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
392bd 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
392be 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
392bf 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69  ue **argv.){.  i
392c0 6e 74 20 72 63 3b 0a 20 20 63 6f 6e 73 74 20 73  nt rc;.  const s
392c1 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
392c2 5f 6d 6f 64 75 6c 65 20 2a 70 31 3b 0a 20 20 63  _module *p1;.  c
392c3 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
392c4 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
392c5 32 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2;.  sqlite3 *db
392c6 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 73 71   = (sqlite3 *)sq
392c7 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
392c8 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 2f 2a 20  context);..  /* 
392c9 54 65 73 74 20 74 68 65 20 71 75 65 72 79 20 66  Test the query f
392ca 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  unction */.  sql
392cb 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f  ite3Fts3SimpleTo
392cc 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70  kenizerModule(&p
392cd 31 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79  1);.  rc = query
392ce 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 73  Tokenizer(db, "s
392cf 69 6d 70 6c 65 22 2c 20 26 70 32 29 3b 0a 20 20  imple", &p2);.  
392d0 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
392d1 54 45 5f 4f 4b 20 29 3b 0a 20 20 61 73 73 65 72  TE_OK );.  asser
392d2 74 28 20 70 31 3d 3d 70 32 20 29 3b 0a 20 20 72  t( p1==p2 );.  r
392d3 63 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a  c = queryTokeniz
392d4 65 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f  er(db, "nosuchto
392d5 6b 65 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b 0a  kenizer", &p2);.
392d6 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
392d7 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 20 20  LITE_ERROR );.  
392d8 61 73 73 65 72 74 28 20 70 32 3d 3d 30 20 29 3b  assert( p2==0 );
392d9 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 73 74  .  assert( 0==st
392da 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 65 72 72  rcmp(sqlite3_err
392db 6d 73 67 28 64 62 29 2c 20 22 75 6e 6b 6e 6f 77  msg(db), "unknow
392dc 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 6e 6f 73  n tokenizer: nos
392dd 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 29 20 29  uchtokenizer") )
392de 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74 68 65  ;..  /* Test the
392df 20 73 74 6f 72 61 67 65 20 66 75 6e 63 74 69 6f   storage functio
392e0 6e 20 2a 2f 0a 20 20 72 63 20 3d 20 72 65 67 69  n */.  rc = regi
392e1 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28 64 62  sterTokenizer(db
392e2 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a  , "nosuchtokeniz
392e3 65 72 22 2c 20 70 31 29 3b 0a 20 20 61 73 73 65  er", p1);.  asse
392e4 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
392e5 4b 20 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72  K );.  rc = quer
392e6 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22  yTokenizer(db, "
392e7 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22  nosuchtokenizer"
392e8 2c 20 26 70 32 29 3b 0a 20 20 61 73 73 65 72 74  , &p2);.  assert
392e9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
392ea 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 32 3d  );.  assert( p2=
392eb 3d 70 31 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65  =p1 );..  sqlite
392ec 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
392ed 6e 74 65 78 74 2c 20 22 6f 6b 22 2c 20 2d 31 2c  ntext, "ok", -1,
392ee 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
392ef 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  .}..#endif../*.*
392f0 2a 20 53 65 74 20 75 70 20 53 51 4c 20 6f 62 6a  * Set up SQL obj
392f1 65 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65  ects in database
392f2 20 64 62 20 75 73 65 64 20 74 6f 20 61 63 63 65   db used to acce
392f3 73 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ss the contents 
392f4 6f 66 0a 2a 2a 20 74 68 65 20 68 61 73 68 20 74  of.** the hash t
392f5 61 62 6c 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  able pointed to 
392f6 62 79 20 61 72 67 75 6d 65 6e 74 20 70 48 61 73  by argument pHas
392f7 68 2e 20 54 68 65 20 68 61 73 68 20 74 61 62 6c  h. The hash tabl
392f8 65 20 6d 75 73 74 0a 2a 2a 20 62 65 65 6e 20 69  e must.** been i
392f9 6e 69 74 69 61 6c 69 73 65 64 20 74 6f 20 75 73  nitialised to us
392fa 65 20 73 74 72 69 6e 67 20 6b 65 79 73 2c 20 61  e string keys, a
392fb 6e 64 20 74 6f 20 74 61 6b 65 20 61 20 70 72 69  nd to take a pri
392fc 76 61 74 65 20 63 6f 70 79 20 0a 2a 2a 20 6f 66  vate copy .** of
392fd 20 74 68 65 20 6b 65 79 20 77 68 65 6e 20 61 20   the key when a 
392fe 76 61 6c 75 65 20 69 73 20 69 6e 73 65 72 74 65  value is inserte
392ff 64 2e 20 69 2e 65 2e 20 62 79 20 61 20 63 61 6c  d. i.e. by a cal
39300 6c 20 73 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a  l similar to:.**
39301 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 46 74  .**    sqlite3Ft
39302 73 33 48 61 73 68 49 6e 69 74 28 70 48 61 73 68  s3HashInit(pHash
39303 2c 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49  , FTS3_HASH_STRI
39304 4e 47 2c 20 31 29 3b 0a 2a 2a 0a 2a 2a 20 54 68  NG, 1);.**.** Th
39305 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 73  is function adds
39306 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69   a scalar functi
39307 6f 6e 20 28 73 65 65 20 68 65 61 64 65 72 20 63  on (see header c
39308 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 2a 2a 20  omment above.** 
39309 73 63 61 6c 61 72 46 75 6e 63 28 29 20 69 6e 20  scalarFunc() in 
3930a 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65  this file for de
3930b 74 61 69 6c 73 29 20 61 6e 64 2c 20 69 66 20 45  tails) and, if E
3930c 4e 41 42 4c 45 5f 54 41 42 4c 45 20 69 73 0a 2a  NABLE_TABLE is.*
3930d 2a 20 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d  * defined at com
3930e 70 69 6c 61 74 69 6f 6e 20 74 69 6d 65 2c 20 61  pilation time, a
3930f 20 74 65 6d 70 6f 72 61 72 79 20 76 69 72 74 75   temporary virtu
39310 61 6c 20 74 61 62 6c 65 20 28 73 65 65 20 68 65  al table (see he
39311 61 64 65 72 20 0a 2a 2a 20 63 6f 6d 6d 65 6e 74  ader .** comment
39312 20 61 62 6f 76 65 20 73 74 72 75 63 74 20 48 61   above struct Ha
39313 73 68 54 61 62 6c 65 56 74 61 62 29 20 74 6f 20  shTableVtab) to 
39314 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
39315 65 6d 61 2e 20 42 6f 74 68 20 0a 2a 2a 20 70 72  ema. Both .** pr
39316 6f 76 69 64 65 20 72 65 61 64 2f 77 72 69 74 65  ovide read/write
39317 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 63   access to the c
39318 6f 6e 74 65 6e 74 73 20 6f 66 20 2a 70 48 61 73  ontents of *pHas
39319 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69  h..**.** The thi
3931a 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  rd argument to t
3931b 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 7a 4e  his function, zN
3931c 61 6d 65 2c 20 69 73 20 75 73 65 64 20 61 73 20  ame, is used as 
3931d 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 62  the name.** of b
3931e 6f 74 68 20 74 68 65 20 73 63 61 6c 61 72 20 61  oth the scalar a
3931f 6e 64 2c 20 69 66 20 63 72 65 61 74 65 64 2c 20  nd, if created, 
39320 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
39321 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
39322 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
39323 46 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c  Fts3InitHashTabl
39324 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
39325 2c 20 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70  , .  fts3Hash *p
39326 48 61 73 68 2c 20 0a 20 20 63 6f 6e 73 74 20 63  Hash, .  const c
39327 68 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20  har *zName.){.  
39328 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
39329 4f 4b 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20  OK;.  void *p = 
3932a 28 76 6f 69 64 20 2a 29 70 48 61 73 68 3b 0a 20  (void *)pHash;. 
3932b 20 63 6f 6e 73 74 20 69 6e 74 20 61 6e 79 20 3d   const int any =
3932c 20 53 51 4c 49 54 45 5f 41 4e 59 3b 0a 20 20 63   SQLITE_ANY;.  c
3932d 68 61 72 20 2a 7a 54 65 73 74 20 3d 20 30 3b 0a  har *zTest = 0;.
3932e 20 20 63 68 61 72 20 2a 7a 54 65 73 74 32 20 3d    char *zTest2 =
3932f 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   0;..#ifdef SQLI
39330 54 45 5f 54 45 53 54 0a 20 20 76 6f 69 64 20 2a  TE_TEST.  void *
39331 70 64 62 20 3d 20 28 76 6f 69 64 20 2a 29 64 62  pdb = (void *)db
39332 3b 0a 20 20 7a 54 65 73 74 20 3d 20 73 71 6c 69  ;.  zTest = sqli
39333 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f  te3_mprintf("%s_
39334 74 65 73 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  test", zName);. 
39335 20 7a 54 65 73 74 32 20 3d 20 73 71 6c 69 74 65   zTest2 = sqlite
39336 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f 69 6e  3_mprintf("%s_in
39337 74 65 72 6e 61 6c 5f 74 65 73 74 22 2c 20 7a 4e  ternal_test", zN
39338 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 54 65  ame);.  if( !zTe
39339 73 74 20 7c 7c 20 21 7a 54 65 73 74 32 20 29 7b  st || !zTest2 ){
3933a 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
3933b 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64  _NOMEM;.  }.#end
3933c 69 66 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51  if..  if( rc!=SQ
3933d 4c 49 54 45 5f 4f 4b 0a 20 20 20 7c 7c 20 28 72  LITE_OK.   || (r
3933e 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61  c = sqlite3_crea
3933f 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  te_function(db, 
39340 7a 4e 61 6d 65 2c 20 31 2c 20 61 6e 79 2c 20 70  zName, 1, any, p
39341 2c 20 73 63 61 6c 61 72 46 75 6e 63 2c 20 30 2c  , scalarFunc, 0,
39342 20 30 29 29 0a 20 20 20 7c 7c 20 28 72 63 20 3d   0)).   || (rc =
39343 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
39344 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61  function(db, zNa
39345 6d 65 2c 20 32 2c 20 61 6e 79 2c 20 70 2c 20 73  me, 2, any, p, s
39346 63 61 6c 61 72 46 75 6e 63 2c 20 30 2c 20 30 29  calarFunc, 0, 0)
39347 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ).#ifdef SQLITE_
39348 54 45 53 54 0a 20 20 20 7c 7c 20 28 72 63 20 3d  TEST.   || (rc =
39349 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
3934a 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65  function(db, zTe
3934b 73 74 2c 20 32 2c 20 61 6e 79 2c 20 70 2c 20 74  st, 2, any, p, t
3934c 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a  estFunc, 0, 0)).
3934d 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69     || (rc = sqli
3934e 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
3934f 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 2c 20 33  ion(db, zTest, 3
39350 2c 20 61 6e 79 2c 20 70 2c 20 74 65 73 74 46 75  , any, p, testFu
39351 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c  nc, 0, 0)).   ||
39352 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63   (rc = sqlite3_c
39353 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64  reate_function(d
39354 62 2c 20 7a 54 65 73 74 32 2c 20 30 2c 20 61 6e  b, zTest2, 0, an
39355 79 2c 20 70 64 62 2c 20 69 6e 74 54 65 73 74 46  y, pdb, intTestF
39356 75 6e 63 2c 20 30 2c 20 30 29 29 0a 23 65 6e 64  unc, 0, 0)).#end
39357 69 66 0a 20 20 29 3b 0a 0a 20 20 73 71 6c 69 74  if.  );..  sqlit
39358 65 33 5f 66 72 65 65 28 7a 54 65 73 74 29 3b 0a  e3_free(zTest);.
39359 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
3935a 54 65 73 74 32 29 3b 0a 20 20 72 65 74 75 72 6e  Test2);.  return
3935b 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f   rc;.}..#endif /
3935c 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
3935d 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3935e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3935f 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
39360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
39361 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  f fts3_tokenizer
39362 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
39363 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39364 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
39365 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
39366 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e   file fts3_token
39367 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  izer1.c ********
39368 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39369 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
3936a 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a   2006 Oct 10.**.
3936b 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
3936c 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
3936d 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
3936e 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
3936f 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
39370 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
39371 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
39372 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
39373 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
39374 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
39375 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
39376 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
39377 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
39378 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
39379 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
3937a 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
3937b 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
3937c 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
3937d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3937e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3937f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39381 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  .**.** Implement
39382 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 22 73 69  ation of the "si
39383 6d 70 6c 65 22 20 66 75 6c 6c 2d 74 65 78 74 2d  mple" full-text-
39384 73 65 61 72 63 68 20 74 6f 6b 65 6e 69 7a 65 72  search tokenizer
39385 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ..*/../*.** The 
39386 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
39387 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c  e is only compil
39388 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ed if:.**.**    
39389 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75   * The FTS3 modu
3938a 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c  le is being buil
3938b 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f  t as an extensio
3938c 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77  n.**       (in w
3938d 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
3938e 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66  _CORE is not def
3938f 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  ined), or.**.** 
39390 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d      * The FTS3 m
39391 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62  odule is being b
39392 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f  uilt into the co
39393 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53  re of.**       S
39394 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68 20  QLite (in which 
39395 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  case SQLITE_ENAB
39396 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e  LE_FTS3 is defin
39397 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ed)..*/.#if !def
39398 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
39399 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
3939a 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
3939b 0a 0a 0a 0a 0a 74 79 70 65 64 65 66 20 73 74 72  .....typedef str
3939c 75 63 74 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  uct simple_token
3939d 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33  izer {.  sqlite3
3939e 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65 3b  _tokenizer base;
3939f 0a 20 20 63 68 61 72 20 64 65 6c 69 6d 5b 31 32  .  char delim[12
393a0 38 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  8];             
393a1 2f 2a 20 66 6c 61 67 20 41 53 43 49 49 20 64 65  /* flag ASCII de
393a2 6c 69 6d 69 74 65 72 73 20 2a 2f 0a 7d 20 73 69  limiters */.} si
393a3 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a  mple_tokenizer;.
393a4 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
393a5 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
393a6 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69  _cursor {.  sqli
393a7 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
393a8 72 73 6f 72 20 62 61 73 65 3b 0a 20 20 63 6f 6e  rsor base;.  con
393a9 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75 74 3b  st char *pInput;
393aa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 70            /* inp
393ab 75 74 20 77 65 20 61 72 65 20 74 6f 6b 65 6e 69  ut we are tokeni
393ac 7a 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  zing */.  int nB
393ad 79 74 65 73 3b 20 20 20 20 20 20 20 20 20 20 20  ytes;           
393ae 20 20 20 20 20 20 20 2f 2a 20 73 69 7a 65 20 6f         /* size o
393af 66 20 74 68 65 20 69 6e 70 75 74 20 2a 2f 0a 20  f the input */. 
393b0 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20   int iOffset;   
393b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
393b2 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f   current positio
393b3 6e 20 69 6e 20 70 49 6e 70 75 74 20 2a 2f 0a 20  n in pInput */. 
393b4 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20 20 20   int iToken;    
393b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
393b6 20 69 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 74   index of next t
393b7 6f 6b 65 6e 20 74 6f 20 62 65 20 72 65 74 75 72  oken to be retur
393b8 6e 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70  ned */.  char *p
393b9 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  Token;          
393ba 20 20 20 20 20 20 2f 2a 20 73 74 6f 72 61 67 65        /* storage
393bb 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74 6f 6b   for current tok
393bc 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b  en */.  int nTok
393bd 65 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20 20 20  enAllocated;    
393be 20 20 20 20 20 2f 2a 20 73 70 61 63 65 20 61 6c       /* space al
393bf 6c 6f 63 61 74 65 64 20 74 6f 20 7a 54 6f 6b 65  located to zToke
393c0 6e 20 62 75 66 66 65 72 20 2a 2f 0a 7d 20 73 69  n buffer */.} si
393c1 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  mple_tokenizer_c
393c2 75 72 73 6f 72 3b 0a 0a 0a 2f 2a 20 46 6f 72 77  ursor;.../* Forw
393c3 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  ard declaration 
393c4 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
393c5 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
393c6 72 5f 6d 6f 64 75 6c 65 20 73 69 6d 70 6c 65 54  r_module simpleT
393c7 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a  okenizerModule;.
393c8 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70  .static int simp
393c9 6c 65 44 65 6c 69 6d 28 73 69 6d 70 6c 65 5f 74  leDelim(simple_t
393ca 6f 6b 65 6e 69 7a 65 72 20 2a 74 2c 20 75 6e 73  okenizer *t, uns
393cb 69 67 6e 65 64 20 63 68 61 72 20 63 29 7b 0a 20  igned char c){. 
393cc 20 72 65 74 75 72 6e 20 63 3c 30 78 38 30 20 26   return c<0x80 &
393cd 26 20 74 2d 3e 64 65 6c 69 6d 5b 63 5d 3b 0a 7d  & t->delim[c];.}
393ce 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
393cf 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69   new tokenizer i
393d0 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74  nstance..*/.stat
393d1 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 43 72 65  ic int simpleCre
393d2 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67 63 2c  ate(.  int argc,
393d3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f   const char * co
393d4 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20 73 71 6c  nst *argv,.  sql
393d5 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
393d6 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b 0a  *ppTokenizer.){.
393d7 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a    simple_tokeniz
393d8 65 72 20 2a 74 3b 0a 0a 20 20 74 20 3d 20 28 73  er *t;..  t = (s
393d9 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
393da 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
393db 63 28 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 20  c(sizeof(*t));. 
393dc 20 69 66 28 20 74 3d 3d 4e 55 4c 4c 20 29 20 72   if( t==NULL ) r
393dd 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
393de 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 74 2c 20  EM;.  memset(t, 
393df 30 2c 20 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a  0, sizeof(*t));.
393e0 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .  /* TODO(shess
393e1 29 20 44 65 6c 69 6d 69 74 65 72 73 20 6e 65 65  ) Delimiters nee
393e2 64 20 74 6f 20 72 65 6d 61 69 6e 20 74 68 65 20  d to remain the 
393e3 73 61 6d 65 20 66 72 6f 6d 20 72 75 6e 20 74 6f  same from run to
393e4 20 72 75 6e 2c 0a 20 20 2a 2a 20 65 6c 73 65 20   run,.  ** else 
393e5 77 65 20 6e 65 65 64 20 74 6f 20 72 65 69 6e 64  we need to reind
393e6 65 78 2e 20 20 4f 6e 65 20 73 6f 6c 75 74 69 6f  ex.  One solutio
393e7 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 65 74  n would be a met
393e8 61 2d 74 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20  a-table to.  ** 
393e9 74 72 61 63 6b 20 73 75 63 68 20 69 6e 66 6f 72  track such infor
393ea 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 64 61  mation in the da
393eb 74 61 62 61 73 65 2c 20 74 68 65 6e 20 77 65 27  tabase, then we'
393ec 64 20 6f 6e 6c 79 20 77 61 6e 74 20 74 68 69 73  d only want this
393ed 0a 20 20 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  .  ** informatio
393ee 6e 20 6f 6e 20 74 68 65 20 69 6e 69 74 69 61 6c  n on the initial
393ef 20 63 72 65 61 74 65 2e 0a 20 20 2a 2f 0a 20 20   create..  */.  
393f0 69 66 28 20 61 72 67 63 3e 31 20 29 7b 0a 20 20  if( argc>1 ){.  
393f1 20 20 69 6e 74 20 69 2c 20 6e 20 3d 20 73 74 72    int i, n = str
393f2 6c 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  len(argv[1]);.  
393f3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20    for(i=0; i<n; 
393f4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  i++){.      unsi
393f5 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d 20 61  gned char ch = a
393f6 72 67 76 5b 31 5d 5b 69 5d 3b 0a 20 20 20 20 20  rgv[1][i];.     
393f7 20 2f 2a 20 57 65 20 65 78 70 6c 69 63 69 74 6c   /* We explicitl
393f8 79 20 64 6f 6e 27 74 20 73 75 70 70 6f 72 74 20  y don't support 
393f9 55 54 46 2d 38 20 64 65 6c 69 6d 69 74 65 72 73  UTF-8 delimiters
393fa 20 66 6f 72 20 6e 6f 77 2e 20 2a 2f 0a 20 20 20   for now. */.   
393fb 20 20 20 69 66 28 20 63 68 3e 3d 30 78 38 30 20     if( ch>=0x80 
393fc 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
393fd 65 33 5f 66 72 65 65 28 74 29 3b 0a 20 20 20 20  e3_free(t);.    
393fe 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
393ff 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d  E_ERROR;.      }
39400 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b  .      t->delim[
39401 63 68 5d 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ch] = 1;.    }. 
39402 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a   } else {.    /*
39403 20 4d 61 72 6b 20 6e 6f 6e 2d 61 6c 70 68 61 6e   Mark non-alphan
39404 75 6d 65 72 69 63 20 41 53 43 49 49 20 63 68 61  umeric ASCII cha
39405 72 61 63 74 65 72 73 20 61 73 20 64 65 6c 69 6d  racters as delim
39406 69 74 65 72 73 20 2a 2f 0a 20 20 20 20 69 6e 74  iters */.    int
39407 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b   i;.    for(i=1;
39408 20 69 3c 30 78 38 30 3b 20 69 2b 2b 29 7b 0a 20   i<0x80; i++){. 
39409 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b 69 5d       t->delim[i]
3940a 20 3d 20 21 69 73 61 6c 6e 75 6d 28 69 29 3b 0a   = !isalnum(i);.
3940b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 70      }.  }..  *pp
3940c 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74 2d 3e  Tokenizer = &t->
3940d 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53  base;.  return S
3940e 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
3940f 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f 6b  ** Destroy a tok
39410 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63  enizer.*/.static
39411 20 69 6e 74 20 73 69 6d 70 6c 65 44 65 73 74 72   int simpleDestr
39412 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  oy(sqlite3_token
39413 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
39414 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ){.  sqlite3_fre
39415 65 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20  e(pTokenizer);. 
39416 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
39417 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70  K;.}../*.** Prep
39418 61 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b  are to begin tok
39419 65 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63  enizing a partic
3941a 75 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68  ular string.  Th
3941b 65 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e  e input.** strin
3941c 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65  g to be tokenize
3941d 64 20 69 73 20 70 49 6e 70 75 74 5b 30 2e 2e 6e  d is pInput[0..n
3941e 42 79 74 65 73 2d 31 5d 2e 20 20 41 20 63 75 72  Bytes-1].  A cur
3941f 73 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69  sor.** used to i
39420 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b  ncrementally tok
39421 65 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e  enize this strin
39422 67 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  g is returned in
39423 20 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a   .** *ppCursor..
39424 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69  */.static int si
39425 6d 70 6c 65 4f 70 65 6e 28 0a 20 20 73 71 6c 69  mpleOpen(.  sqli
39426 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
39427 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20  Tokenizer,      
39428 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69     /* The tokeni
39429 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  zer */.  const c
3942a 68 61 72 20 2a 70 49 6e 70 75 74 2c 20 69 6e 74  har *pInput, int
3942b 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
3942c 2f 2a 20 53 74 72 69 6e 67 20 74 6f 20 62 65 20  /* String to be 
3942d 74 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20 20 73  tokenized */.  s
3942e 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3942f 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
39430 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f  or    /* OUT: To
39431 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
39432 72 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70 6c 65  r */.){.  simple
39433 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
39434 72 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 73 69  r *c;..  c = (si
39435 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  mple_tokenizer_c
39436 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33  ursor *) sqlite3
39437 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a  _malloc(sizeof(*
39438 63 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d 4e 55  c));.  if( c==NU
39439 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  LL ) return SQLI
3943a 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63 2d 3e  TE_NOMEM;..  c->
3943b 70 49 6e 70 75 74 20 3d 20 70 49 6e 70 75 74 3b  pInput = pInput;
3943c 0a 20 20 69 66 28 20 70 49 6e 70 75 74 3d 3d 30  .  if( pInput==0
3943d 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74 65   ){.    c->nByte
3943e 73 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  s = 0;.  }else i
3943f 66 28 20 6e 42 79 74 65 73 3c 30 20 29 7b 0a 20  f( nBytes<0 ){. 
39440 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 28     c->nBytes = (
39441 69 6e 74 29 73 74 72 6c 65 6e 28 70 49 6e 70 75  int)strlen(pInpu
39442 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
39443 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 6e 42 79   c->nBytes = nBy
39444 74 65 73 3b 0a 20 20 7d 0a 20 20 63 2d 3e 69 4f  tes;.  }.  c->iO
39445 66 66 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20  ffset = 0;      
39446 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74             /* st
39447 61 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61  art tokenizing a
39448 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
39449 2a 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d  */.  c->iToken =
3944a 20 30 3b 0a 20 20 63 2d 3e 70 54 6f 6b 65 6e 20   0;.  c->pToken 
3944b 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20 20  = NULL;         
3944c 20 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61 63        /* no spac
3944d 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65 74  e allocated, yet
3944e 2e 20 2a 2f 0a 20 20 63 2d 3e 6e 54 6f 6b 65 6e  . */.  c->nToken
3944f 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b 0a 0a  Allocated = 0;..
39450 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 26 63    *ppCursor = &c
39451 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e  ->base;.  return
39452 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
39453 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b  *.** Close a tok
39454 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
39455 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
39456 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a  ed by a call to.
39457 2a 2a 20 73 69 6d 70 6c 65 4f 70 65 6e 28 29 20  ** simpleOpen() 
39458 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  above..*/.static
39459 20 69 6e 74 20 73 69 6d 70 6c 65 43 6c 6f 73 65   int simpleClose
3945a 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
3945b 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  er_cursor *pCurs
3945c 6f 72 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f  or){.  simple_to
3945d 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
3945e 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65  c = (simple_toke
3945f 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20  nizer_cursor *) 
39460 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69 74  pCursor;.  sqlit
39461 65 33 5f 66 72 65 65 28 63 2d 3e 70 54 6f 6b 65  e3_free(c->pToke
39462 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  n);.  sqlite3_fr
39463 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(c);.  return 
39464 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
39465 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
39466 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  next token from 
39467 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  a tokenization c
39468 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73  ursor.  The curs
39469 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20  or must.** have 
3946a 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61  been opened by a
3946b 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73   prior call to s
3946c 69 6d 70 6c 65 4f 70 65 6e 28 29 2e 0a 2a 2f 0a  impleOpen()..*/.
3946d 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c  static int simpl
3946e 65 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33  eNext(.  sqlite3
3946f 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
39470 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20  r *pCursor,  /* 
39471 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20  Cursor returned 
39472 62 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f  by simpleOpen */
39473 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
39474 70 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20  ppToken,        
39475 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a         /* OUT: *
39476 70 70 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 74  ppToken is the t
39477 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69  oken text */.  i
39478 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20 20  nt *pnBytes,    
39479 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3947a 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65     /* OUT: Numbe
3947b 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f  r of bytes in to
3947c 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
3947d 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20 20  StartOffset,    
3947e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3947f 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f 66  OUT: Starting of
39480 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  fset of token */
39481 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66  .  int *piEndOff
39482 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
39483 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
39484 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  nding offset of 
39485 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
39486 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20  piPosition      
39487 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39488 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20  * OUT: Position 
39489 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e  integer of token
3948a 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70 6c 65 5f   */.){.  simple_
3948b 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
3948c 20 2a 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f   *c = (simple_to
3948d 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
3948e 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 69 6d  ) pCursor;.  sim
3948f 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74  ple_tokenizer *t
39490 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e   = (simple_token
39491 69 7a 65 72 20 2a 29 20 70 43 75 72 73 6f 72 2d  izer *) pCursor-
39492 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 75  >pTokenizer;.  u
39493 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 20  nsigned char *p 
39494 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
39495 20 2a 29 63 2d 3e 70 49 6e 70 75 74 3b 0a 0a 20   *)c->pInput;.. 
39496 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73   while( c->iOffs
39497 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20 29 7b 0a  et<c->nBytes ){.
39498 20 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f 66      int iStartOf
39499 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a 20 53 63  fset;..    /* Sc
3949a 61 6e 20 70 61 73 74 20 64 65 6c 69 6d 69 74 65  an past delimite
3949b 72 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a  r characters */.
3949c 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f      while( c->iO
3949d 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20  ffset<c->nBytes 
3949e 26 26 20 73 69 6d 70 6c 65 44 65 6c 69 6d 28 74  && simpleDelim(t
3949f 2c 20 70 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29  , p[c->iOffset])
394a0 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66   ){.      c->iOf
394a1 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20  fset++;.    }.. 
394a2 20 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f 6e 2d     /* Count non-
394a3 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63  delimiter charac
394a4 74 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69 53 74  ters. */.    iSt
394a5 61 72 74 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69  artOffset = c->i
394a6 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c  Offset;.    whil
394a7 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d  e( c->iOffset<c-
394a8 3e 6e 42 79 74 65 73 20 26 26 20 21 73 69 6d 70  >nBytes && !simp
394a9 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63 2d 3e  leDelim(t, p[c->
394aa 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20  iOffset]) ){.   
394ab 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b     c->iOffset++;
394ac 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
394ad 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53 74 61 72  c->iOffset>iStar
394ae 74 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20  tOffset ){.     
394af 20 69 6e 74 20 69 2c 20 6e 20 3d 20 63 2d 3e 69   int i, n = c->i
394b0 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66  Offset-iStartOff
394b1 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  set;.      if( n
394b2 3e 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61  >c->nTokenAlloca
394b3 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ted ){.        c
394b4 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65  ->nTokenAllocate
394b5 64 20 3d 20 6e 2b 32 30 3b 0a 20 20 20 20 20 20  d = n+20;.      
394b6 20 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20 73 71    c->pToken = sq
394b7 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 63 2d  lite3_realloc(c-
394b8 3e 70 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 54 6f 6b  >pToken, c->nTok
394b9 65 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20 20  enAllocated);.  
394ba 20 20 20 20 20 20 69 66 28 20 63 2d 3e 70 54 6f        if( c->pTo
394bb 6b 65 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75  ken==NULL ) retu
394bc 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
394bd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
394be 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b  or(i=0; i<n; i++
394bf 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 4f  ){.        /* TO
394c0 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 6e  DO(shess) This n
394c1 65 65 64 73 20 65 78 70 61 6e 73 69 6f 6e 20 74  eeds expansion t
394c2 6f 20 68 61 6e 64 6c 65 20 55 54 46 2d 38 0a 20  o handle UTF-8. 
394c3 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 2d 69         ** case-i
394c4 6e 73 65 6e 73 69 74 69 76 69 74 79 2e 0a 20 20  nsensitivity..  
394c5 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
394c6 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
394c7 68 20 3d 20 70 5b 69 53 74 61 72 74 4f 66 66 73  h = p[iStartOffs
394c8 65 74 2b 69 5d 3b 0a 20 20 20 20 20 20 20 20 63  et+i];.        c
394c9 2d 3e 70 54 6f 6b 65 6e 5b 69 5d 20 3d 20 63 68  ->pToken[i] = ch
394ca 3c 30 78 38 30 20 3f 20 74 6f 6c 6f 77 65 72 28  <0x80 ? tolower(
394cb 63 68 29 20 3a 20 63 68 3b 0a 20 20 20 20 20 20  ch) : ch;.      
394cc 7d 0a 20 20 20 20 20 20 2a 70 70 54 6f 6b 65 6e  }.      *ppToken
394cd 20 3d 20 63 2d 3e 70 54 6f 6b 65 6e 3b 0a 20 20   = c->pToken;.  
394ce 20 20 20 20 2a 70 6e 42 79 74 65 73 20 3d 20 6e      *pnBytes = n
394cf 3b 0a 20 20 20 20 20 20 2a 70 69 53 74 61 72 74  ;.      *piStart
394d0 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72 74 4f  Offset = iStartO
394d1 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69  ffset;.      *pi
394d2 45 6e 64 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69  EndOffset = c->i
394d3 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70  Offset;.      *p
394d4 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63 2d 3e 69  iPosition = c->i
394d5 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 20 20 20 20  Token++;..      
394d6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
394d7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
394d8 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
394d9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
394da 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74  et of routines t
394db 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  hat implement th
394dc 65 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a  e simple tokeniz
394dd 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  er.*/.static con
394de 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
394df 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73 69 6d 70  izer_module simp
394e0 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
394e1 65 20 3d 20 7b 0a 20 20 30 2c 0a 20 20 73 69 6d  e = {.  0,.  sim
394e2 70 6c 65 43 72 65 61 74 65 2c 0a 20 20 73 69 6d  pleCreate,.  sim
394e3 70 6c 65 44 65 73 74 72 6f 79 2c 0a 20 20 73 69  pleDestroy,.  si
394e4 6d 70 6c 65 4f 70 65 6e 2c 0a 20 20 73 69 6d 70  mpleOpen,.  simp
394e5 6c 65 43 6c 6f 73 65 2c 0a 20 20 73 69 6d 70 6c  leClose,.  simpl
394e6 65 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  eNext,.};../*.**
394e7 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
394e8 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72  simple tokenizer
394e9 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
394ea 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a  ter to the new.*
394eb 2a 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 20 2a  * tokenizer in *
394ec 70 70 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51 4c 49  ppModule.*/.SQLI
394ed 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
394ee 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c  sqlite3Fts3Simpl
394ef 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  eTokenizerModule
394f0 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
394f1 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
394f2 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b 0a  st**ppModule.){.
394f3 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 73    *ppModule = &s
394f4 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
394f5 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  dule;.}..#endif 
394f6 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
394f7 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
394f8 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
394f9 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a  E_FTS3) */../***
394fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
394fb 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  of fts3_tokenize
394fc 72 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r1.c ***********
394fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
394fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
394ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
39500 6e 20 66 69 6c 65 20 72 74 72 65 65 2e 63 20 2a  n file rtree.c *
39501 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39502 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39503 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
39504 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
39505 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
39506 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
39507 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
39508 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
39509 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
3950a 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
3950b 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
3950c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
3950d 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
3950e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
3950f 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
39510 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
39511 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
39512 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
39513 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
39514 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
39515 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
39516 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
39517 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39518 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39519 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3951a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3951b 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
3951c 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f  contains code fo
3951d 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  r implementation
3951e 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  s of the r-tree 
3951f 61 6e 64 20 72 2a 2d 74 72 65 65 0a 2a 2a 20 61  and r*-tree.** a
39520 6c 67 6f 72 69 74 68 6d 73 20 70 61 63 6b 61 67  lgorithms packag
39521 65 64 20 61 73 20 61 6e 20 53 51 4c 69 74 65 20  ed as an SQLite 
39522 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
39523 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  dule..**.** $Id:
39524 20 72 74 72 65 65 2e 63 2c 76 20 31 2e 31 34 20   rtree.c,v 1.14 
39525 32 30 30 39 2f 30 38 2f 30 36 20 31 38 3a 33 36  2009/08/06 18:36
39526 3a 34 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :47 danielk1977 
39527 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64  Exp $.*/..#if !d
39528 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
39529 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
3952a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52  QLITE_ENABLE_RTR
3952b 45 45 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EE)../*.** This 
3952c 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e  file contains an
3952d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3952e 6f 66 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64  of a couple of d
3952f 69 66 66 65 72 65 6e 74 20 76 61 72 69 61 6e 74  ifferent variant
39530 73 0a 2a 2a 20 6f 66 20 74 68 65 20 72 2d 74 72  s.** of the r-tr
39531 65 65 20 61 6c 67 6f 72 69 74 68 6d 2e 20 53 65  ee algorithm. Se
39532 65 20 74 68 65 20 52 45 41 44 4d 45 20 66 69 6c  e the README fil
39533 65 20 66 6f 72 20 66 75 72 74 68 65 72 20 64 65  e for further de
39534 74 61 69 6c 73 2e 20 54 68 65 20 0a 2a 2a 20 73  tails. The .** s
39535 61 6d 65 20 64 61 74 61 2d 73 74 72 75 63 74 75  ame data-structu
39536 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 61  re is used for a
39537 6c 6c 2c 20 62 75 74 20 74 68 65 20 61 6c 67 6f  ll, but the algo
39538 72 69 74 68 6d 73 20 66 6f 72 20 69 6e 73 65 72  rithms for inser
39539 74 20 61 6e 64 0a 2a 2a 20 64 65 6c 65 74 65 20  t and.** delete 
3953a 6f 70 65 72 61 74 69 6f 6e 73 20 76 61 72 79 2e  operations vary.
3953b 20 54 68 65 20 76 61 72 69 61 6e 74 73 20 75 73   The variants us
3953c 65 64 20 61 72 65 20 73 65 6c 65 63 74 65 64 20  ed are selected 
3953d 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20  at compile time 
3953e 0a 2a 2a 20 62 79 20 64 65 66 69 6e 69 6e 67 20  .** by defining 
3953f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79  the following sy
39540 6d 62 6f 6c 73 3a 0a 2a 2f 0a 0a 2f 2a 20 45 69  mbols:.*/../* Ei
39541 74 68 65 72 2c 20 62 6f 74 68 20 6f 72 20 6e 6f  ther, both or no
39542 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
39543 69 6e 67 20 6d 61 79 20 62 65 20 73 65 74 20 74  ing may be set t
39544 6f 20 61 63 74 69 76 61 74 65 20 0a 2a 2a 20 72  o activate .** r
39545 2a 74 72 65 65 20 76 61 72 69 61 6e 74 20 61 6c  *tree variant al
39546 67 6f 72 69 74 68 6d 73 2e 0a 2a 2f 0a 23 64 65  gorithms..*/.#de
39547 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54  fine VARIANT_RST
39548 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42  ARTREE_CHOOSESUB
39549 54 52 45 45 20 30 0a 23 64 65 66 69 6e 65 20 56  TREE 0.#define V
3954a 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45  ARIANT_RSTARTREE
3954b 5f 52 45 49 4e 53 45 52 54 20 20 20 20 20 20 31  _REINSERT      1
3954c 0a 0a 2f 2a 20 0a 2a 2a 20 45 78 61 63 74 6c 79  ../* .** Exactly
3954d 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
3954e 6f 77 69 6e 67 20 6d 75 73 74 20 62 65 20 73 65  owing must be se
3954f 74 20 74 6f 20 31 2e 0a 2a 2f 0a 23 64 65 66 69  t to 1..*/.#defi
39550 6e 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  ne VARIANT_GUTTM
39551 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c  AN_QUADRATIC_SPL
39552 49 54 20 30 0a 23 64 65 66 69 6e 65 20 56 41 52  IT 0.#define VAR
39553 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
39554 45 41 52 5f 53 50 4c 49 54 20 20 20 20 30 0a 23  EAR_SPLIT    0.#
39555 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52  define VARIANT_R
39556 53 54 41 52 54 52 45 45 5f 53 50 4c 49 54 20 20  STARTREE_SPLIT  
39557 20 20 20 20 20 20 20 31 0a 0a 23 64 65 66 69 6e         1..#defin
39558 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  e VARIANT_GUTTMA
39559 4e 5f 53 50 4c 49 54 20 5c 0a 20 20 20 20 20 20  N_SPLIT \.      
3955a 20 20 28 56 41 52 49 41 4e 54 5f 47 55 54 54 4d    (VARIANT_GUTTM
3955b 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 7c  AN_LINEAR_SPLIT|
3955c 7c 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e  |VARIANT_GUTTMAN
3955d 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54  _QUADRATIC_SPLIT
3955e 29 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  )..#if VARIANT_G
3955f 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
39560 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e 65  _SPLIT.  #define
39561 20 50 69 63 6b 4e 65 78 74 20 51 75 61 64 72 61   PickNext Quadra
39562 74 69 63 50 69 63 6b 4e 65 78 74 0a 20 20 23 64  ticPickNext.  #d
39563 65 66 69 6e 65 20 50 69 63 6b 53 65 65 64 73 20  efine PickSeeds 
39564 51 75 61 64 72 61 74 69 63 50 69 63 6b 53 65 65  QuadraticPickSee
39565 64 73 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73  ds.  #define Ass
39566 69 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f  ignCells splitNo
39567 64 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66  deGuttman.#endif
39568 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54  .#if VARIANT_GUT
39569 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49  TMAN_LINEAR_SPLI
3956a 54 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  T.  #define Pick
3956b 4e 65 78 74 20 4c 69 6e 65 61 72 50 69 63 6b 4e  Next LinearPickN
3956c 65 78 74 0a 20 20 23 64 65 66 69 6e 65 20 50 69  ext.  #define Pi
3956d 63 6b 53 65 65 64 73 20 4c 69 6e 65 61 72 50 69  ckSeeds LinearPi
3956e 63 6b 53 65 65 64 73 0a 20 20 23 64 65 66 69 6e  ckSeeds.  #defin
3956f 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73 70  e AssignCells sp
39570 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 0a 23  litNodeGuttman.#
39571 65 6e 64 69 66 0a 23 69 66 20 56 41 52 49 41 4e  endif.#if VARIAN
39572 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49  T_RSTARTREE_SPLI
39573 54 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73 69  T.  #define Assi
39574 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f 64  gnCells splitNod
39575 65 53 74 61 72 74 72 65 65 0a 23 65 6e 64 69 66  eStartree.#endif
39576 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
39577 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f  E_CORE.  SQLITE_
39578 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a  EXTENSION_INIT1.
39579 23 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 0a 23  #else.#endif...#
3957a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d  ifndef SQLITE_AM
3957b 41 4c 47 41 4d 41 54 49 4f 4e 0a 74 79 70 65 64  ALGAMATION.typed
3957c 65 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  ef sqlite3_int64
3957d 20 69 36 34 3b 0a 74 79 70 65 64 65 66 20 75 6e   i64;.typedef un
3957e 73 69 67 6e 65 64 20 63 68 61 72 20 75 38 3b 0a  signed char u8;.
3957f 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
39580 20 69 6e 74 20 75 33 32 3b 0a 23 65 6e 64 69 66   int u32;.#endif
39581 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
39582 20 52 74 72 65 65 20 52 74 72 65 65 3b 0a 74 79   Rtree Rtree;.ty
39583 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72  pedef struct Rtr
39584 65 65 43 75 72 73 6f 72 20 52 74 72 65 65 43 75  eeCursor RtreeCu
39585 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74  rsor;.typedef st
39586 72 75 63 74 20 52 74 72 65 65 4e 6f 64 65 20 52  ruct RtreeNode R
39587 74 72 65 65 4e 6f 64 65 3b 0a 74 79 70 65 64 65  treeNode;.typede
39588 66 20 73 74 72 75 63 74 20 52 74 72 65 65 43 65  f struct RtreeCe
39589 6c 6c 20 52 74 72 65 65 43 65 6c 6c 3b 0a 74 79  ll RtreeCell;.ty
3958a 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72  pedef struct Rtr
3958b 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 52 74 72  eeConstraint Rtr
3958c 65 65 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 74 79  eeConstraint;.ty
3958d 70 65 64 65 66 20 75 6e 69 6f 6e 20 52 74 72 65  pedef union Rtre
3958e 65 43 6f 6f 72 64 20 52 74 72 65 65 43 6f 6f 72  eCoord RtreeCoor
3958f 64 3b 0a 0a 2f 2a 20 54 68 65 20 72 74 72 65 65  d;../* The rtree
39590 20 6d 61 79 20 68 61 76 65 20 62 65 74 77 65 65   may have betwee
39591 6e 20 31 20 61 6e 64 20 52 54 52 45 45 5f 4d 41  n 1 and RTREE_MA
39592 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 64 69 6d  X_DIMENSIONS dim
39593 65 6e 73 69 6f 6e 73 2e 20 2a 2f 0a 23 64 65 66  ensions. */.#def
39594 69 6e 65 20 52 54 52 45 45 5f 4d 41 58 5f 44 49  ine RTREE_MAX_DI
39595 4d 45 4e 53 49 4f 4e 53 20 35 0a 0a 2f 2a 20 53  MENSIONS 5../* S
39596 69 7a 65 20 6f 66 20 68 61 73 68 20 74 61 62 6c  ize of hash tabl
39597 65 20 52 74 72 65 65 2e 61 48 61 73 68 2e 20 54  e Rtree.aHash. T
39598 68 69 73 20 68 61 73 68 20 74 61 62 6c 65 20 69  his hash table i
39599 73 20 6e 6f 74 20 65 78 70 65 63 74 65 64 20 74  s not expected t
3959a 6f 0a 2a 2a 20 65 76 65 72 20 63 6f 6e 74 61 69  o.** ever contai
3959b 6e 20 76 65 72 79 20 6d 61 6e 79 20 65 6e 74 72  n very many entr
3959c 69 65 73 2c 20 73 6f 20 61 20 66 69 78 65 64 20  ies, so a fixed 
3959d 6e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74  number of bucket
3959e 73 20 69 73 20 0a 2a 2a 20 75 73 65 64 2e 0a 2a  s is .** used..*
3959f 2f 0a 23 64 65 66 69 6e 65 20 48 41 53 48 53 49  /.#define HASHSI
395a0 5a 45 20 31 32 38 0a 0a 2f 2a 20 0a 2a 2a 20 41  ZE 128../* .** A
395a1 6e 20 72 74 72 65 65 20 76 69 72 74 75 61 6c 2d  n rtree virtual-
395a2 74 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f  table object..*/
395a3 0a 73 74 72 75 63 74 20 52 74 72 65 65 20 7b 0a  .struct Rtree {.
395a4 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 62    sqlite3_vtab b
395a5 61 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ase;.  sqlite3 *
395a6 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
395a7 20 20 20 2f 2a 20 48 6f 73 74 20 64 61 74 61 62     /* Host datab
395a8 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
395a9 2f 0a 20 20 69 6e 74 20 69 4e 6f 64 65 53 69 7a  /.  int iNodeSiz
395aa 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
395ab 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65 73  /* Size in bytes
395ac 20 6f 66 20 65 61 63 68 20 6e 6f 64 65 20 69 6e   of each node in
395ad 20 74 68 65 20 6e 6f 64 65 20 74 61 62 6c 65 20   the node table 
395ae 2a 2f 0a 20 20 69 6e 74 20 6e 44 69 6d 3b 20 20  */.  int nDim;  
395af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
395b0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 69   /* Number of di
395b1 6d 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  mensions */.  in
395b2 74 20 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b  t nBytesPerCell;
395b3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
395b4 65 73 20 63 6f 6e 73 75 6d 65 64 20 70 65 72 20  es consumed per 
395b5 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 44  cell */.  int iD
395b6 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  epth;           
395b7 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
395b8 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 72 2d   depth of the r-
395b9 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
395ba 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  /.  char *zDb;  
395bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
395bc 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62  /* Name of datab
395bd 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72  ase containing r
395be 2d 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20  -tree table */. 
395bf 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
395c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
395c1 4e 61 6d 65 20 6f 66 20 72 2d 74 72 65 65 20 74  Name of r-tree t
395c2 61 62 6c 65 20 2a 2f 20 0a 20 20 52 74 72 65 65  able */ .  Rtree
395c3 4e 6f 64 65 20 2a 61 48 61 73 68 5b 48 41 53 48  Node *aHash[HASH
395c4 53 49 5a 45 5d 3b 20 2f 2a 20 48 61 73 68 20 74  SIZE]; /* Hash t
395c5 61 62 6c 65 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72  able of in-memor
395c6 79 20 6e 6f 64 65 73 2e 20 2a 2f 20 0a 20 20 69  y nodes. */ .  i
395c7 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20  nt nBusy;       
395c8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
395c9 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
395ca 75 73 65 72 73 20 6f 66 20 74 68 69 73 20 73 74  users of this st
395cb 72 75 63 74 75 72 65 20 2a 2f 0a 0a 20 20 2f 2a  ructure */..  /*
395cc 20 4c 69 73 74 20 6f 66 20 6e 6f 64 65 73 20 72   List of nodes r
395cd 65 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 20  emoved during a 
395ce 43 6f 6e 64 65 6e 73 65 54 72 65 65 20 6f 70 65  CondenseTree ope
395cf 72 61 74 69 6f 6e 2e 20 4c 69 73 74 20 69 73 0a  ration. List is.
395d0 20 20 2a 2a 20 6c 69 6e 6b 65 64 20 74 6f 67 65    ** linked toge
395d1 74 68 65 72 20 76 69 61 20 74 68 65 20 70 6f 69  ther via the poi
395d2 6e 74 65 72 20 6e 6f 72 6d 61 6c 6c 79 20 75 73  nter normally us
395d3 65 64 20 66 6f 72 20 68 61 73 68 20 63 68 61 69  ed for hash chai
395d4 6e 73 20 2d 0a 20 20 2a 2a 20 52 74 72 65 65 4e  ns -.  ** RtreeN
395d5 6f 64 65 2e 70 4e 65 78 74 2e 20 52 74 72 65 65  ode.pNext. Rtree
395d6 4e 6f 64 65 2e 69 4e 6f 64 65 20 73 74 6f 72 65  Node.iNode store
395d7 73 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 74  s the depth of t
395d8 68 65 20 73 75 62 2d 74 72 65 65 20 0a 20 20 2a  he sub-tree .  *
395d9 2a 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20  * headed by the 
395da 6e 6f 64 65 20 28 6c 65 61 66 20 6e 6f 64 65 73  node (leaf nodes
395db 20 68 61 76 65 20 52 74 72 65 65 4e 6f 64 65 2e   have RtreeNode.
395dc 69 4e 6f 64 65 3d 3d 30 29 2e 0a 20 20 2a 2f 0a  iNode==0)..  */.
395dd 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 44 65    RtreeNode *pDe
395de 6c 65 74 65 64 3b 0a 20 20 69 6e 74 20 69 52 65  leted;.  int iRe
395df 69 6e 73 65 72 74 48 65 69 67 68 74 3b 20 20 20  insertHeight;   
395e0 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f       /* Height o
395e1 66 20 73 75 62 2d 74 72 65 65 73 20 52 65 69 6e  f sub-trees Rein
395e2 73 65 72 74 28 29 20 68 61 73 20 72 75 6e 20 6f  sert() has run o
395e3 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 53 74 61 74 65  n */..  /* State
395e4 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 2f 77 72  ments to read/wr
395e5 69 74 65 2f 64 65 6c 65 74 65 20 61 20 72 65 63  ite/delete a rec
395e6 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f 6e 6f 64  ord from xxx_nod
395e7 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  e */.  sqlite3_s
395e8 74 6d 74 20 2a 70 52 65 61 64 4e 6f 64 65 3b 0a  tmt *pReadNode;.
395e9 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
395ea 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20 73 71  pWriteNode;.  sq
395eb 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c  lite3_stmt *pDel
395ec 65 74 65 4e 6f 64 65 3b 0a 0a 20 20 2f 2a 20 53  eteNode;..  /* S
395ed 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72 65 61  tatements to rea
395ee 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65 20 61  d/write/delete a
395ef 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78 78 78   record from xxx
395f0 5f 72 6f 77 69 64 20 2a 2f 0a 20 20 73 71 6c 69  _rowid */.  sqli
395f1 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64 52  te3_stmt *pReadR
395f2 6f 77 69 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  owid;.  sqlite3_
395f3 73 74 6d 74 20 2a 70 57 72 69 74 65 52 6f 77 69  stmt *pWriteRowi
395f4 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  d;.  sqlite3_stm
395f5 74 20 2a 70 44 65 6c 65 74 65 52 6f 77 69 64 3b  t *pDeleteRowid;
395f6 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74  ..  /* Statement
395f7 73 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f  s to read/write/
395f8 64 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20  delete a record 
395f9 66 72 6f 6d 20 78 78 78 5f 70 61 72 65 6e 74 20  from xxx_parent 
395fa 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
395fb 74 20 2a 70 52 65 61 64 50 61 72 65 6e 74 3b 0a  t *pReadParent;.
395fc 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
395fd 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20 20  pWriteParent;.  
395fe 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44  sqlite3_stmt *pD
395ff 65 6c 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20 20  eleteParent;..  
39600 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65 3b 0a  int eCoordType;.
39601 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65 20  };../* Possible 
39602 76 61 6c 75 65 73 20 66 6f 72 20 65 43 6f 6f 72  values for eCoor
39603 64 54 79 70 65 3a 20 2a 2f 0a 23 64 65 66 69 6e  dType: */.#defin
39604 65 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45  e RTREE_COORD_RE
39605 41 4c 33 32 20 30 0a 23 64 65 66 69 6e 65 20 52  AL32 0.#define R
39606 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32  TREE_COORD_INT32
39607 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d    1../*.** The m
39608 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  inimum number of
39609 20 63 65 6c 6c 73 20 61 6c 6c 6f 77 65 64 20 66   cells allowed f
3960a 6f 72 20 61 20 6e 6f 64 65 20 69 73 20 61 20 74  or a node is a t
3960b 68 69 72 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20  hird of the .** 
3960c 6d 61 78 69 6d 75 6d 2e 20 49 6e 20 47 75 74 6d  maximum. In Gutm
3960d 61 6e 27 73 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a  an's notation:.*
3960e 2a 0a 2a 2a 20 20 20 20 20 6d 20 3d 20 4d 2f 33  *.**     m = M/3
3960f 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 52 2a 2d  .**.** If an R*-
39610 74 72 65 65 20 22 52 65 69 6e 73 65 72 74 22 20  tree "Reinsert" 
39611 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65 71  operation is req
39612 75 69 72 65 64 2c 20 74 68 65 20 73 61 6d 65 20  uired, the same 
39613 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c  number of.** cel
39614 6c 73 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66  ls are removed f
39615 72 6f 6d 20 74 68 65 20 6f 76 65 72 66 75 6c 6c  rom the overfull
39616 20 6e 6f 64 65 20 61 6e 64 20 72 65 69 6e 73 65   node and reinse
39617 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72  rted into the tr
39618 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ee..*/.#define R
39619 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 29  TREE_MINCELLS(p)
3961a 20 28 28 28 28 70 29 2d 3e 69 4e 6f 64 65 53 69   ((((p)->iNodeSi
3961b 7a 65 2d 34 29 2f 28 70 29 2d 3e 6e 42 79 74 65  ze-4)/(p)->nByte
3961c 73 50 65 72 43 65 6c 6c 29 2f 33 29 0a 23 64 65  sPerCell)/3).#de
3961d 66 69 6e 65 20 52 54 52 45 45 5f 52 45 49 4e 53  fine RTREE_REINS
3961e 45 52 54 28 70 29 20 52 54 52 45 45 5f 4d 49 4e  ERT(p) RTREE_MIN
3961f 43 45 4c 4c 53 28 70 29 0a 23 64 65 66 69 6e 65  CELLS(p).#define
39620 20 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 20   RTREE_MAXCELLS 
39621 35 31 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74  51../* .** An rt
39622 72 65 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63  ree cursor objec
39623 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72  t..*/.struct Rtr
39624 65 65 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  eeCursor {.  sql
39625 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
39626 20 62 61 73 65 3b 0a 20 20 52 74 72 65 65 4e 6f   base;.  RtreeNo
39627 64 65 20 2a 70 4e 6f 64 65 3b 20 20 20 20 20 20  de *pNode;      
39628 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f             /* No
39629 64 65 20 63 75 72 73 6f 72 20 69 73 20 63 75 72  de cursor is cur
3962a 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
3962b 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c  at */.  int iCel
3962c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
3962d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
3962e 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 63 65  ex of current ce
3962f 6c 6c 20 69 6e 20 70 4e 6f 64 65 20 2a 2f 0a 20  ll in pNode */. 
39630 20 69 6e 74 20 69 53 74 72 61 74 65 67 79 3b 20   int iStrategy; 
39631 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39632 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 64     /* Copy of id
39633 78 4e 75 6d 20 73 65 61 72 63 68 20 70 61 72 61  xNum search para
39634 6d 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  meter */.  int n
39635 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20  Constraint;     
39636 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39637 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
39638 73 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74  s in aConstraint
39639 20 2a 2f 0a 20 20 52 74 72 65 65 43 6f 6e 73 74   */.  RtreeConst
3963a 72 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69  raint *aConstrai
3963b 6e 74 3b 20 20 20 20 20 2f 2a 20 53 65 61 72 63  nt;     /* Searc
3963c 68 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a  h constraints. *
3963d 2f 0a 7d 3b 0a 0a 75 6e 69 6f 6e 20 52 74 72 65  /.};..union Rtre
3963e 65 43 6f 6f 72 64 20 7b 0a 20 20 66 6c 6f 61 74  eCoord {.  float
3963f 20 66 3b 0a 20 20 69 6e 74 20 69 3b 0a 7d 3b 0a   f;.  int i;.};.
39640 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  ./*.** The argum
39641 65 6e 74 20 69 73 20 61 6e 20 52 74 72 65 65 43  ent is an RtreeC
39642 6f 6f 72 64 2e 20 52 65 74 75 72 6e 20 74 68 65  oord. Return the
39643 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 77 69   value stored wi
39644 74 68 69 6e 20 74 68 65 20 52 74 72 65 65 43 6f  thin the RtreeCo
39645 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 74 65 64  ord.** formatted
39646 20 61 73 20 61 20 64 6f 75 62 6c 65 2e 20 54 68   as a double. Th
39647 69 73 20 6d 61 63 72 6f 20 61 73 73 75 6d 65 73  is macro assumes
39648 20 74 68 61 74 20 6c 6f 63 61 6c 20 76 61 72 69   that local vari
39649 61 62 6c 65 20 70 52 74 72 65 65 20 70 6f 69 6e  able pRtree poin
3964a 74 73 0a 2a 2a 20 74 6f 20 74 68 65 20 52 74 72  ts.** to the Rtr
3964b 65 65 20 73 74 72 75 63 74 75 72 65 20 61 73 73  ee structure ass
3964c 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
3964d 20 52 74 72 65 65 43 6f 6f 72 64 2e 0a 2a 2f 0a   RtreeCoord..*/.
3964e 23 64 65 66 69 6e 65 20 44 43 4f 4f 52 44 28 63  #define DCOORD(c
3964f 6f 6f 72 64 29 20 28 20 20 20 20 20 20 20 20 20  oord) (         
39650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39651 20 20 5c 0a 20 20 28 70 52 74 72 65 65 2d 3e 65    \.  (pRtree->e
39652 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45  CoordType==RTREE
39653 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 29 20 3f  _COORD_REAL32) ?
39654 20 20 20 20 20 20 5c 0a 20 20 20 20 28 28 64 6f        \.    ((do
39655 75 62 6c 65 29 63 6f 6f 72 64 2e 66 29 20 3a 20  uble)coord.f) : 
39656 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39657 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
39658 28 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64 2e 69  ((double)coord.i
39659 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
3965a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
3965b 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 65 61 72 63  )../*.** A searc
3965c 68 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f  h constraint..*/
3965d 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 6f 6e  .struct RtreeCon
3965e 73 74 72 61 69 6e 74 20 7b 0a 20 20 69 6e 74 20  straint {.  int 
3965f 69 43 6f 6f 72 64 3b 20 20 20 20 20 20 20 20 20  iCoord;         
39660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39661 20 49 6e 64 65 78 20 6f 66 20 63 6f 6e 73 74 72   Index of constr
39662 61 69 6e 65 64 20 63 6f 6f 72 64 69 6e 61 74 65  ained coordinate
39663 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20   */.  int op;   
39664 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39665 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74          /* Const
39666 72 61 69 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f  raining operatio
39667 6e 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 56  n */.  double rV
39668 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20 20 20  alue;           
39669 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73           /* Cons
3966a 74 72 61 69 6e 74 20 76 61 6c 75 65 2e 20 2a 2f  traint value. */
3966b 0a 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65  .};../* Possible
3966c 20 76 61 6c 75 65 73 20 66 6f 72 20 52 74 72 65   values for Rtre
3966d 65 43 6f 6e 73 74 72 61 69 6e 74 2e 6f 70 20 2a  eConstraint.op *
3966e 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  /.#define RTREE_
3966f 45 51 20 30 78 34 31 0a 23 64 65 66 69 6e 65 20  EQ 0x41.#define 
39670 52 54 52 45 45 5f 4c 45 20 30 78 34 32 0a 23 64  RTREE_LE 0x42.#d
39671 65 66 69 6e 65 20 52 54 52 45 45 5f 4c 54 20 30  efine RTREE_LT 0
39672 78 34 33 0a 23 64 65 66 69 6e 65 20 52 54 52 45  x43.#define RTRE
39673 45 5f 47 45 20 30 78 34 34 0a 23 64 65 66 69 6e  E_GE 0x44.#defin
39674 65 20 52 54 52 45 45 5f 47 54 20 30 78 34 35 0a  e RTREE_GT 0x45.
39675 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65  ./* .** An rtree
39676 20 73 74 72 75 63 74 75 72 65 20 6e 6f 64 65 2e   structure node.
39677 0a 2a 2a 0a 2a 2a 20 44 61 74 61 20 66 6f 72 6d  .**.** Data form
39678 61 74 20 28 52 74 72 65 65 4e 6f 64 65 2e 7a 44  at (RtreeNode.zD
39679 61 74 61 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e  ata):.**.**   1.
3967a 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20   If the node is 
3967b 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 28 6e  the root node (n
3967c 6f 64 65 20 31 29 2c 20 74 68 65 6e 20 74 68 65  ode 1), then the
3967d 20 66 69 72 73 74 20 32 20 62 79 74 65 73 0a 2a   first 2 bytes.*
3967e 2a 20 20 20 20 20 20 6f 66 20 74 68 65 20 6e 6f  *      of the no
3967f 64 65 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74  de contain the t
39680 72 65 65 20 64 65 70 74 68 20 61 73 20 61 20 62  ree depth as a b
39681 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
39682 72 2e 0a 2a 2a 20 20 20 20 20 20 46 6f 72 20 6e  r..**      For n
39683 6f 6e 2d 72 6f 6f 74 20 6e 6f 64 65 73 2c 20 74  on-root nodes, t
39684 68 65 20 66 69 72 73 74 20 32 20 62 79 74 65 73  he first 2 bytes
39685 20 61 72 65 20 6c 65 66 74 20 75 6e 75 73 65 64   are left unused
39686 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65  ..**.**   2. The
39687 20 6e 65 78 74 20 32 20 62 79 74 65 73 20 63 6f   next 2 bytes co
39688 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72  ntain the number
39689 20 6f 66 20 65 6e 74 72 69 65 73 20 63 75 72 72   of entries curr
3968a 65 6e 74 6c 79 20 0a 2a 2a 20 20 20 20 20 20 73  ently .**      s
3968b 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 6f 64  tored in the nod
3968c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 54 68  e..**.**   3. Th
3968d 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74  e remainder of t
3968e 68 65 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 73  he node contains
3968f 20 74 68 65 20 6e 6f 64 65 20 65 6e 74 72 69 65   the node entrie
39690 73 2e 20 45 61 63 68 20 65 6e 74 72 79 0a 2a 2a  s. Each entry.**
39691 20 20 20 20 20 20 63 6f 6e 73 69 73 74 73 20 6f        consists o
39692 66 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 79 74  f a single 8-byt
39693 65 20 69 6e 74 65 67 65 72 20 66 6f 6c 6c 6f 77  e integer follow
39694 65 64 20 62 79 20 61 6e 20 65 76 65 6e 20 6e 75  ed by an even nu
39695 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 6f 66 20  mber.**      of 
39696 34 2d 62 79 74 65 20 63 6f 6f 72 64 69 6e 61 74  4-byte coordinat
39697 65 73 2e 20 46 6f 72 20 6c 65 61 66 20 6e 6f 64  es. For leaf nod
39698 65 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 69  es the integer i
39699 73 20 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 20  s the rowid.**  
3969a 20 20 20 20 6f 66 20 61 20 72 65 63 6f 72 64 2e      of a record.
3969b 20 46 6f 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   For internal no
3969c 64 65 73 20 69 74 20 69 73 20 74 68 65 20 6e 6f  des it is the no
3969d 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 0a 2a  de number of a.*
3969e 2a 20 20 20 20 20 20 63 68 69 6c 64 20 70 61 67  *      child pag
3969f 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72  e..*/.struct Rtr
396a0 65 65 4e 6f 64 65 20 7b 0a 20 20 52 74 72 65 65  eeNode {.  Rtree
396a1 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b 20 20  Node *pParent;  
396a2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
396a3 50 61 72 65 6e 74 20 6e 6f 64 65 20 2a 2f 0a 20  Parent node */. 
396a4 20 69 36 34 20 69 4e 6f 64 65 3b 0a 20 20 69 6e   i64 iNode;.  in
396a5 74 20 6e 52 65 66 3b 0a 20 20 69 6e 74 20 69 73  t nRef;.  int is
396a6 44 69 72 74 79 3b 0a 20 20 75 38 20 2a 7a 44 61  Dirty;.  u8 *zDa
396a7 74 61 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ta;.  RtreeNode 
396a8 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
396a9 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
396aa 6e 6f 64 65 20 69 6e 20 74 68 69 73 20 68 61 73  node in this has
396ab 68 20 63 68 61 69 6e 20 2a 2f 0a 7d 3b 0a 23 64  h chain */.};.#d
396ac 65 66 69 6e 65 20 4e 43 45 4c 4c 28 70 4e 6f 64  efine NCELL(pNod
396ad 65 29 20 72 65 61 64 49 6e 74 31 36 28 26 28 70  e) readInt16(&(p
396ae 4e 6f 64 65 29 2d 3e 7a 44 61 74 61 5b 32 5d 29  Node)->zData[2])
396af 0a 0a 2f 2a 20 0a 2a 2a 20 53 74 72 75 63 74 75  ../* .** Structu
396b0 72 65 20 74 6f 20 73 74 6f 72 65 20 61 20 64 65  re to store a de
396b1 73 65 72 69 61 6c 69 7a 65 64 20 72 74 72 65 65  serialized rtree
396b2 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74 72 75   record..*/.stru
396b3 63 74 20 52 74 72 65 65 43 65 6c 6c 20 7b 0a 20  ct RtreeCell {. 
396b4 20 69 36 34 20 69 52 6f 77 69 64 3b 0a 20 20 52   i64 iRowid;.  R
396b5 74 72 65 65 43 6f 6f 72 64 20 61 43 6f 6f 72 64  treeCoord aCoord
396b6 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e  [RTREE_MAX_DIMEN
396b7 53 49 4f 4e 53 2a 32 5d 3b 0a 7d 3b 0a 0a 23 69  SIONS*2];.};..#i
396b8 66 6e 64 65 66 20 4d 41 58 0a 23 20 64 65 66 69  fndef MAX.# defi
396b9 6e 65 20 4d 41 58 28 78 2c 79 29 20 28 28 78 29  ne MAX(x,y) ((x)
396ba 20 3c 20 28 79 29 20 3f 20 28 79 29 20 3a 20 28   < (y) ? (y) : (
396bb 78 29 29 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  x)).#endif.#ifnd
396bc 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20  ef MIN.# define 
396bd 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 20 3e 20  MIN(x,y) ((x) > 
396be 28 79 29 20 3f 20 28 79 29 20 3a 20 28 78 29 29  (y) ? (y) : (x))
396bf 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46  .#endif../*.** F
396c0 75 6e 63 74 69 6f 6e 73 20 74 6f 20 64 65 73 65  unctions to dese
396c1 72 69 61 6c 69 7a 65 20 61 20 31 36 20 62 69 74  rialize a 16 bit
396c2 20 69 6e 74 65 67 65 72 2c 20 33 32 20 62 69 74   integer, 32 bit
396c3 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64   real number and
396c4 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e 74 65 67  .** 64 bit integ
396c5 65 72 2e 20 54 68 65 20 64 65 73 65 72 69 61 6c  er. The deserial
396c6 69 7a 65 64 20 76 61 6c 75 65 20 69 73 20 72 65  ized value is re
396c7 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
396c8 63 20 69 6e 74 20 72 65 61 64 49 6e 74 31 36 28  c int readInt16(
396c9 75 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  u8 *p){.  return
396ca 20 28 70 5b 30 5d 3c 3c 38 29 20 2b 20 70 5b 31   (p[0]<<8) + p[1
396cb 5d 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  ];.}.static void
396cc 20 72 65 61 64 43 6f 6f 72 64 28 75 38 20 2a 70   readCoord(u8 *p
396cd 2c 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43  , RtreeCoord *pC
396ce 6f 6f 72 64 29 7b 0a 20 20 75 33 32 20 69 20 3d  oord){.  u32 i =
396cf 20 28 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b   (.    (((u32)p[
396d0 30 5d 29 20 3c 3c 20 32 34 29 20 2b 20 0a 20 20  0]) << 24) + .  
396d1 20 20 28 28 28 75 33 32 29 70 5b 31 5d 29 20 3c    (((u32)p[1]) <
396d2 3c 20 31 36 29 20 2b 20 0a 20 20 20 20 28 28 28  < 16) + .    (((
396d3 75 33 32 29 70 5b 32 5d 29 20 3c 3c 20 20 38 29  u32)p[2]) <<  8)
396d4 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
396d5 5b 33 5d 29 20 3c 3c 20 20 30 29 0a 20 20 29 3b  [3]) <<  0).  );
396d6 0a 20 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f 72  .  *(u32 *)pCoor
396d7 64 20 3d 20 69 3b 0a 7d 0a 73 74 61 74 69 63 20  d = i;.}.static 
396d8 69 36 34 20 72 65 61 64 49 6e 74 36 34 28 75 38  i64 readInt64(u8
396d9 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28   *p){.  return (
396da 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 30 5d  .    (((i64)p[0]
396db 29 20 3c 3c 20 35 36 29 20 2b 20 0a 20 20 20 20  ) << 56) + .    
396dc 28 28 28 69 36 34 29 70 5b 31 5d 29 20 3c 3c 20  (((i64)p[1]) << 
396dd 34 38 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  48) + .    (((i6
396de 34 29 70 5b 32 5d 29 20 3c 3c 20 34 30 29 20 2b  4)p[2]) << 40) +
396df 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 33   .    (((i64)p[3
396e0 5d 29 20 3c 3c 20 33 32 29 20 2b 20 0a 20 20 20  ]) << 32) + .   
396e1 20 28 28 28 69 36 34 29 70 5b 34 5d 29 20 3c 3c   (((i64)p[4]) <<
396e2 20 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28 69   24) + .    (((i
396e3 36 34 29 70 5b 35 5d 29 20 3c 3c 20 31 36 29 20  64)p[5]) << 16) 
396e4 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
396e5 36 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20 20  6]) <<  8) + .  
396e6 20 20 28 28 28 69 36 34 29 70 5b 37 5d 29 20 3c    (((i64)p[7]) <
396e7 3c 20 20 30 29 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a  <  0).  );.}../*
396e8 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 74 6f  .** Functions to
396e9 20 73 65 72 69 61 6c 69 7a 65 20 61 20 31 36 20   serialize a 16 
396ea 62 69 74 20 69 6e 74 65 67 65 72 2c 20 33 32 20  bit integer, 32 
396eb 62 69 74 20 72 65 61 6c 20 6e 75 6d 62 65 72 20  bit real number 
396ec 61 6e 64 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e  and.** 64 bit in
396ed 74 65 67 65 72 2e 20 54 68 65 20 76 61 6c 75 65  teger. The value
396ee 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
396ef 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
396f0 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 74   written.** to t
396f1 68 65 20 61 72 67 75 6d 65 6e 74 20 62 75 66 66  he argument buff
396f2 65 72 20 28 61 6c 77 61 79 73 20 32 2c 20 34 20  er (always 2, 4 
396f3 61 6e 64 20 38 20 72 65 73 70 65 63 74 69 76 65  and 8 respective
396f4 6c 79 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ly)..*/.static i
396f5 6e 74 20 77 72 69 74 65 49 6e 74 31 36 28 75 38  nt writeInt16(u8
396f6 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 70   *p, int i){.  p
396f7 5b 30 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [0] = (i>> 8)&0x
396f8 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 69 3e  FF;.  p[1] = (i>
396f9 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
396fa 75 72 6e 20 32 3b 0a 7d 0a 73 74 61 74 69 63 20  urn 2;.}.static 
396fb 69 6e 74 20 77 72 69 74 65 43 6f 6f 72 64 28 75  int writeCoord(u
396fc 38 20 2a 70 2c 20 52 74 72 65 65 43 6f 6f 72 64  8 *p, RtreeCoord
396fd 20 2a 70 43 6f 6f 72 64 29 7b 0a 20 20 75 33 32   *pCoord){.  u32
396fe 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69   i;.  assert( si
396ff 7a 65 6f 66 28 52 74 72 65 65 43 6f 6f 72 64 29  zeof(RtreeCoord)
39700 3d 3d 34 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==4 );.  assert(
39701 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20   sizeof(u32)==4 
39702 29 3b 0a 20 20 69 20 3d 20 2a 28 75 33 32 20 2a  );.  i = *(u32 *
39703 29 70 43 6f 6f 72 64 3b 0a 20 20 70 5b 30 5d 20  )pCoord;.  p[0] 
39704 3d 20 28 69 3e 3e 32 34 29 26 30 78 46 46 3b 0a  = (i>>24)&0xFF;.
39705 20 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 31 36 29    p[1] = (i>>16)
39706 26 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20  &0xFF;.  p[2] = 
39707 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20 20  (i>> 8)&0xFF;.  
39708 70 5b 33 5d 20 3d 20 28 69 3e 3e 20 30 29 26 30  p[3] = (i>> 0)&0
39709 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 34 3b  xFF;.  return 4;
3970a 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  .}.static int wr
3970b 69 74 65 49 6e 74 36 34 28 75 38 20 2a 70 2c 20  iteInt64(u8 *p, 
3970c 69 36 34 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d  i64 i){.  p[0] =
3970d 20 28 69 3e 3e 35 36 29 26 30 78 46 46 3b 0a 20   (i>>56)&0xFF;. 
3970e 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 34 38 29 26   p[1] = (i>>48)&
3970f 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20 28  0xFF;.  p[2] = (
39710 69 3e 3e 34 30 29 26 30 78 46 46 3b 0a 20 20 70  i>>40)&0xFF;.  p
39711 5b 33 5d 20 3d 20 28 69 3e 3e 33 32 29 26 30 78  [3] = (i>>32)&0x
39712 46 46 3b 0a 20 20 70 5b 34 5d 20 3d 20 28 69 3e  FF;.  p[4] = (i>
39713 3e 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 35  >24)&0xFF;.  p[5
39714 5d 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46  ] = (i>>16)&0xFF
39715 3b 0a 20 20 70 5b 36 5d 20 3d 20 28 69 3e 3e 20  ;.  p[6] = (i>> 
39716 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 37 5d 20  8)&0xFF;.  p[7] 
39717 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a  = (i>> 0)&0xFF;.
39718 20 20 72 65 74 75 72 6e 20 38 3b 0a 7d 0a 0a 2f    return 8;.}../
39719 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74  *.** Increment t
3971a 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
3971b 6e 74 20 6f 66 20 6e 6f 64 65 20 70 2e 0a 2a 2f  nt of node p..*/
3971c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64  .static void nod
3971d 65 52 65 66 65 72 65 6e 63 65 28 52 74 72 65 65  eReference(Rtree
3971e 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66 28 20  Node *p){.  if( 
3971f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 52 65 66  p ){.    p->nRef
39720 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ++;.  }.}../*.**
39721 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6e 74 65   Clear the conte
39722 6e 74 20 6f 66 20 6e 6f 64 65 20 70 20 28 73 65  nt of node p (se
39723 74 20 61 6c 6c 20 62 79 74 65 73 20 74 6f 20 30  t all bytes to 0
39724 78 30 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  x00)..*/.static 
39725 76 6f 69 64 20 6e 6f 64 65 5a 65 72 6f 28 52 74  void nodeZero(Rt
39726 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
39727 65 65 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66  eeNode *p){.  if
39728 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( p ){.    memse
39729 74 28 26 70 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20  t(&p->zData[2], 
3972a 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  0, pRtree->iNode
3972b 53 69 7a 65 2d 32 29 3b 0a 20 20 20 20 70 2d 3e  Size-2);.    p->
3972c 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 7d  isDirty = 1;.  }
3972d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
3972e 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20 69 4e  a node number iN
3972f 6f 64 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20  ode, return the 
39730 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6b 65  corresponding ke
39731 79 20 74 6f 20 75 73 65 0a 2a 2a 20 69 6e 20 74  y to use.** in t
39732 68 65 20 52 74 72 65 65 2e 61 48 61 73 68 20 74  he Rtree.aHash t
39733 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
39734 69 6e 74 20 6e 6f 64 65 48 61 73 68 28 69 36 34  int nodeHash(i64
39735 20 69 4e 6f 64 65 29 7b 0a 20 20 72 65 74 75 72   iNode){.  retur
39736 6e 20 28 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e  n (.    (iNode>>
39737 35 36 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 38  56) ^ (iNode>>48
39738 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 30 29 20  ) ^ (iNode>>40) 
39739 5e 20 28 69 4e 6f 64 65 3e 3e 33 32 29 20 5e 20  ^ (iNode>>32) ^ 
3973a 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e 32 34 29  .    (iNode>>24)
3973b 20 5e 20 28 69 4e 6f 64 65 3e 3e 31 36 29 20 5e   ^ (iNode>>16) ^
3973c 20 28 69 4e 6f 64 65 3e 3e 20 38 29 20 5e 20 28   (iNode>> 8) ^ (
3973d 69 4e 6f 64 65 3e 3e 20 30 29 0a 20 20 29 20 25  iNode>> 0).  ) %
3973e 20 48 41 53 48 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a   HASHSIZE;.}../*
3973f 0a 2a 2a 20 53 65 61 72 63 68 20 74 68 65 20 6e  .** Search the n
39740 6f 64 65 20 68 61 73 68 20 74 61 62 6c 65 20 66  ode hash table f
39741 6f 72 20 6e 6f 64 65 20 69 4e 6f 64 65 2e 20 49  or node iNode. I
39742 66 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20  f found, return 
39743 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
39744 69 74 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72  it. Otherwise, r
39745 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74  eturn 0..*/.stat
39746 69 63 20 52 74 72 65 65 4e 6f 64 65 20 2a 6e 6f  ic RtreeNode *no
39747 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 52 74 72  deHashLookup(Rtr
39748 65 65 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20  ee *pRtree, i64 
39749 69 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65 65 4e  iNode){.  RtreeN
3974a 6f 64 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  ode *p;.  assert
3974b 28 20 69 4e 6f 64 65 21 3d 30 20 29 3b 0a 20 20  ( iNode!=0 );.  
3974c 66 6f 72 28 70 3d 70 52 74 72 65 65 2d 3e 61 48  for(p=pRtree->aH
3974d 61 73 68 5b 6e 6f 64 65 48 61 73 68 28 69 4e 6f  ash[nodeHash(iNo
3974e 64 65 29 5d 3b 20 70 20 26 26 20 70 2d 3e 69 4e  de)]; p && p->iN
3974f 6f 64 65 21 3d 69 4e 6f 64 65 3b 20 70 3d 70 2d  ode!=iNode; p=p-
39750 3e 70 4e 65 78 74 29 3b 0a 20 20 72 65 74 75 72  >pNext);.  retur
39751 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  n p;.}../*.** Ad
39752 64 20 6e 6f 64 65 20 70 4e 6f 64 65 20 74 6f 20  d node pNode to 
39753 74 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61  the node hash ta
39754 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
39755 6f 69 64 20 6e 6f 64 65 48 61 73 68 49 6e 73 65  oid nodeHashInse
39756 72 74 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  rt(Rtree *pRtree
39757 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
39758 64 65 29 7b 0a 20 20 69 66 28 20 70 4e 6f 64 65  de){.  if( pNode
39759 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 48 61 73   ){.    int iHas
3975a 68 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  h;.    assert( p
3975b 4e 6f 64 65 2d 3e 70 4e 65 78 74 3d 3d 30 20 29  Node->pNext==0 )
3975c 3b 0a 20 20 20 20 69 48 61 73 68 20 3d 20 6e 6f  ;.    iHash = no
3975d 64 65 48 61 73 68 28 70 4e 6f 64 65 2d 3e 69 4e  deHash(pNode->iN
3975e 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  ode);.    pNode-
3975f 3e 70 4e 65 78 74 20 3d 20 70 52 74 72 65 65 2d  >pNext = pRtree-
39760 3e 61 48 61 73 68 5b 69 48 61 73 68 5d 3b 0a 20  >aHash[iHash];. 
39761 20 20 20 70 52 74 72 65 65 2d 3e 61 48 61 73 68     pRtree->aHash
39762 5b 69 48 61 73 68 5d 20 3d 20 70 4e 6f 64 65 3b  [iHash] = pNode;
39763 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
39764 6d 6f 76 65 20 6e 6f 64 65 20 70 4e 6f 64 65 20  move node pNode 
39765 66 72 6f 6d 20 74 68 65 20 6e 6f 64 65 20 68 61  from the node ha
39766 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  sh table..*/.sta
39767 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 48 61 73  tic void nodeHas
39768 68 44 65 6c 65 74 65 28 52 74 72 65 65 20 2a 70  hDelete(Rtree *p
39769 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
3976a 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65   *pNode){.  Rtre
3976b 65 4e 6f 64 65 20 2a 2a 70 70 3b 0a 20 20 69 66  eNode **pp;.  if
3976c 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21 3d  ( pNode->iNode!=
3976d 30 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 26 70  0 ){.    pp = &p
3976e 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e 6f 64  Rtree->aHash[nod
3976f 65 48 61 73 68 28 70 4e 6f 64 65 2d 3e 69 4e 6f  eHash(pNode->iNo
39770 64 65 29 5d 3b 0a 20 20 20 20 66 6f 72 28 20 3b  de)];.    for( ;
39771 20 28 2a 70 70 29 21 3d 70 4e 6f 64 65 3b 20 70   (*pp)!=pNode; p
39772 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78  p = &(*pp)->pNex
39773 74 29 7b 20 61 73 73 65 72 74 28 2a 70 70 29 3b  t){ assert(*pp);
39774 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20 70 4e 6f   }.    *pp = pNo
39775 64 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70  de->pNext;.    p
39776 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  Node->pNext = 0;
39777 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  .  }.}../*.** Al
39778 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72  locate and retur
39779 6e 20 6e 65 77 20 72 2d 74 72 65 65 20 6e 6f 64  n new r-tree nod
3977a 65 2e 20 49 6e 69 74 69 61 6c 6c 79 2c 20 28 52  e. Initially, (R
3977b 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65 3d 3d  treeNode.iNode==
3977c 30 29 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  0),.** indicatin
3977d 67 20 74 68 61 74 20 6e 6f 64 65 20 68 61 73 20  g that node has 
3977e 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 61 73 73  not yet been ass
3977f 69 67 6e 65 64 20 61 20 6e 6f 64 65 20 6e 75 6d  igned a node num
39780 62 65 72 2e 20 49 74 20 69 73 0a 2a 2a 20 61 73  ber. It is.** as
39781 73 69 67 6e 65 64 20 61 20 6e 6f 64 65 20 6e 75  signed a node nu
39782 6d 62 65 72 20 77 68 65 6e 20 6e 6f 64 65 57 72  mber when nodeWr
39783 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ite() is called 
39784 74 6f 20 77 72 69 74 65 20 74 68 65 0a 2a 2a 20  to write the.** 
39785 6e 6f 64 65 20 63 6f 6e 74 65 6e 74 73 20 6f 75  node contents ou
39786 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
39787 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74 72  e..*/.static Rtr
39788 65 65 4e 6f 64 65 20 2a 6e 6f 64 65 4e 65 77 28  eeNode *nodeNew(
39789 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
3978a 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e  treeNode *pParen
3978b 74 2c 20 69 6e 74 20 7a 65 72 6f 29 7b 0a 20 20  t, int zero){.  
3978c 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
3978d 3b 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74 72  ;.  pNode = (Rtr
3978e 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65 33  eeNode *)sqlite3
3978f 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
39790 74 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74 72  treeNode) + pRtr
39791 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a  ee->iNodeSize);.
39792 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
39793 20 20 20 6d 65 6d 73 65 74 28 70 4e 6f 64 65 2c     memset(pNode,
39794 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
39795 4e 6f 64 65 29 20 2b 20 28 7a 65 72 6f 3f 70 52  Node) + (zero?pR
39796 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 3a  tree->iNodeSize:
39797 30 29 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  0));.    pNode->
39798 7a 44 61 74 61 20 3d 20 28 75 38 20 2a 29 26 70  zData = (u8 *)&p
39799 4e 6f 64 65 5b 31 5d 3b 0a 20 20 20 20 70 4e 6f  Node[1];.    pNo
3979a 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  de->nRef = 1;.  
3979b 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74    pNode->pParent
3979c 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 20 20   = pParent;.    
3979d 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d  pNode->isDirty =
3979e 20 31 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66 65   1;.    nodeRefe
3979f 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a  rence(pParent);.
397a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 6f    }.  return pNo
397a1 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74  de;.}../*.** Obt
397a2 61 69 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20  ain a reference 
397a3 74 6f 20 61 6e 20 72 2d 74 72 65 65 20 6e 6f 64  to an r-tree nod
397a4 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
397a5 0a 6e 6f 64 65 41 63 71 75 69 72 65 28 0a 20 20  .nodeAcquire(.  
397a6 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20  Rtree *pRtree,  
397a7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 2d             /* R-
397a8 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
397a9 2f 0a 20 20 69 36 34 20 69 4e 6f 64 65 2c 20 20  /.  i64 iNode,  
397aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
397ab 2a 20 4e 6f 64 65 20 6e 75 6d 62 65 72 20 74 6f  * Node number to
397ac 20 6c 6f 61 64 20 2a 2f 0a 20 20 52 74 72 65 65   load */.  Rtree
397ad 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 2c 20 20  Node *pParent,  
397ae 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20        /* Either 
397af 74 68 65 20 70 61 72 65 6e 74 20 6e 6f 64 65 20  the parent node 
397b0 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 52 74 72  or NULL */.  Rtr
397b1 65 65 4e 6f 64 65 20 2a 2a 70 70 4e 6f 64 65 20  eeNode **ppNode 
397b2 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
397b3 41 63 71 75 69 72 65 64 20 6e 6f 64 65 20 2a 2f  Acquired node */
397b4 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
397b5 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
397b6 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  ;..  /* Check if
397b7 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6e   the requested n
397b8 6f 64 65 20 69 73 20 61 6c 72 65 61 64 79 20 69  ode is already i
397b9 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  n the hash table
397ba 2e 20 49 66 20 73 6f 2c 0a 20 20 2a 2a 20 69 6e  . If so,.  ** in
397bb 63 72 65 61 73 65 20 69 74 73 20 72 65 66 65 72  crease its refer
397bc 65 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72  ence count and r
397bd 65 74 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20  eturn it..  */. 
397be 20 69 66 28 20 28 70 4e 6f 64 65 20 3d 20 6e 6f   if( (pNode = no
397bf 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74  deHashLookup(pRt
397c0 72 65 65 2c 20 69 4e 6f 64 65 29 29 20 29 7b 0a  ree, iNode)) ){.
397c1 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
397c2 72 65 6e 74 20 7c 7c 20 21 70 4e 6f 64 65 2d 3e  rent || !pNode->
397c3 70 50 61 72 65 6e 74 20 7c 7c 20 70 4e 6f 64 65  pParent || pNode
397c4 2d 3e 70 50 61 72 65 6e 74 3d 3d 70 50 61 72 65  ->pParent==pPare
397c5 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50  nt );.    if( pP
397c6 61 72 65 6e 74 20 26 26 20 21 70 4e 6f 64 65 2d  arent && !pNode-
397c7 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  >pParent ){.    
397c8 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
397c9 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20  pParent);.      
397ca 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d  pNode->pParent =
397cb 20 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 7d 0a   pParent;.    }.
397cc 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b      pNode->nRef+
397cd 2b 3b 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d  +;.    *ppNode =
397ce 20 70 4e 6f 64 65 3b 0a 20 20 20 20 72 65 74 75   pNode;.    retu
397cf 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
397d0 7d 0a 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74  }..  pNode = (Rt
397d1 72 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65  reeNode *)sqlite
397d2 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
397d3 52 74 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74  RtreeNode) + pRt
397d4 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b  ree->iNodeSize);
397d5 0a 20 20 69 66 28 20 21 70 4e 6f 64 65 20 29 7b  .  if( !pNode ){
397d6 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d 20 30  .    *ppNode = 0
397d7 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
397d8 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
397d9 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20   pNode->pParent 
397da 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 70 4e 6f  = pParent;.  pNo
397db 64 65 2d 3e 7a 44 61 74 61 20 3d 20 28 75 38 20  de->zData = (u8 
397dc 2a 29 26 70 4e 6f 64 65 5b 31 5d 3b 0a 20 20 70  *)&pNode[1];.  p
397dd 4e 6f 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  Node->nRef = 1;.
397de 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d    pNode->iNode =
397df 20 69 4e 6f 64 65 3b 0a 20 20 70 4e 6f 64 65 2d   iNode;.  pNode-
397e0 3e 69 73 44 69 72 74 79 20 3d 20 30 3b 0a 20 20  >isDirty = 0;.  
397e1 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30  pNode->pNext = 0
397e2 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  ;..  sqlite3_bin
397e3 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e  d_int64(pRtree->
397e4 70 52 65 61 64 4e 6f 64 65 2c 20 31 2c 20 69 4e  pReadNode, 1, iN
397e5 6f 64 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  ode);.  rc = sql
397e6 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
397e7 2d 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20  ->pReadNode);.  
397e8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
397e9 4f 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  OW ){.    const 
397ea 75 38 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69  u8 *zBlob = sqli
397eb 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
397ec 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
397ed 65 2c 20 30 29 3b 0a 20 20 20 20 6d 65 6d 63 70  e, 0);.    memcp
397ee 79 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20  y(pNode->zData, 
397ef 7a 42 6c 6f 62 2c 20 70 52 74 72 65 65 2d 3e 69  zBlob, pRtree->i
397f0 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 20 20 6e  NodeSize);.    n
397f1 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 50 61  odeReference(pPa
397f2 72 65 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rent);.  }else{.
397f3 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
397f4 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f  (pNode);.    pNo
397f5 64 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2a  de = 0;.  }..  *
397f6 70 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b 0a  ppNode = pNode;.
397f7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
397f8 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
397f9 61 64 4e 6f 64 65 29 3b 0a 0a 20 20 69 66 28 20  adNode);..  if( 
397fa 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
397fb 20 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20   iNode==1 ){.   
397fc 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20   pRtree->iDepth 
397fd 3d 20 72 65 61 64 49 6e 74 31 36 28 70 4e 6f 64  = readInt16(pNod
397fe 65 2d 3e 7a 44 61 74 61 29 3b 0a 20 20 7d 0a 0a  e->zData);.  }..
397ff 20 20 61 73 73 65 72 74 28 20 28 72 63 3d 3d 53    assert( (rc==S
39800 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4e 6f 64  QLITE_OK && pNod
39801 65 29 20 7c 7c 20 28 70 4e 6f 64 65 3d 3d 30 20  e) || (pNode==0 
39802 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  && rc!=SQLITE_OK
39803 29 20 29 3b 0a 20 20 6e 6f 64 65 48 61 73 68 49  ) );.  nodeHashI
39804 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
39805 6f 64 65 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ode);..  return 
39806 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 76 65  rc;.}../*.** Ove
39807 72 77 72 69 74 65 20 63 65 6c 6c 20 69 43 65 6c  rwrite cell iCel
39808 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 20  l of node pNode 
39809 77 69 74 68 20 74 68 65 20 63 6f 6e 74 65 6e 74  with the content
3980a 73 20 6f 66 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73  s of pCell..*/.s
3980b 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 4f  tatic void nodeO
3980c 76 65 72 77 72 69 74 65 43 65 6c 6c 28 0a 20 20  verwriteCell(.  
3980d 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
3980e 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
3980f 64 65 2c 20 20 0a 20 20 52 74 72 65 65 43 65 6c  de,  .  RtreeCel
39810 6c 20 2a 70 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *pCell, .  int
39811 20 69 43 65 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20   iCell.){.  int 
39812 69 69 3b 0a 20 20 75 38 20 2a 70 20 3d 20 26 70  ii;.  u8 *p = &p
39813 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b 20  Node->zData[4 + 
39814 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
39815 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 3b 0a 20 20  rCell*iCell];.  
39816 70 20 2b 3d 20 77 72 69 74 65 49 6e 74 36 34 28  p += writeInt64(
39817 70 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  p, pCell->iRowid
39818 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
39819 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  i<(pRtree->nDim*
3981a 32 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 70  2); ii++){.    p
3981b 20 2b 3d 20 77 72 69 74 65 43 6f 6f 72 64 28 70   += writeCoord(p
3981c 2c 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64  , &pCell->aCoord
3981d 5b 69 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 4e 6f  [ii]);.  }.  pNo
3981e 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
3981f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
39820 20 63 65 6c 6c 20 74 68 65 20 63 65 6c 6c 20 77   cell the cell w
39821 69 74 68 20 69 6e 64 65 78 20 69 43 65 6c 6c 20  ith index iCell 
39822 66 72 6f 6d 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  from node pNode.
39823 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
39824 6e 6f 64 65 44 65 6c 65 74 65 43 65 6c 6c 28 52  nodeDeleteCell(R
39825 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
39826 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20  reeNode *pNode, 
39827 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38  int iCell){.  u8
39828 20 2a 70 44 73 74 20 3d 20 26 70 4e 6f 64 65 2d   *pDst = &pNode-
39829 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65  >zData[4 + pRtre
3982a 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
3982b 2a 69 43 65 6c 6c 5d 3b 0a 20 20 75 38 20 2a 70  *iCell];.  u8 *p
3982c 53 72 63 20 3d 20 26 70 44 73 74 5b 70 52 74 72  Src = &pDst[pRtr
3982d 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
3982e 6c 5d 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20  l];.  int nByte 
3982f 3d 20 28 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20  = (NCELL(pNode) 
39830 2d 20 69 43 65 6c 6c 20 2d 20 31 29 20 2a 20 70  - iCell - 1) * p
39831 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72  Rtree->nBytesPer
39832 43 65 6c 6c 3b 0a 20 20 6d 65 6d 6d 6f 76 65 28  Cell;.  memmove(
39833 70 44 73 74 2c 20 70 53 72 63 2c 20 6e 42 79 74  pDst, pSrc, nByt
39834 65 29 3b 0a 20 20 77 72 69 74 65 49 6e 74 31 36  e);.  writeInt16
39835 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 32  (&pNode->zData[2
39836 5d 2c 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2d  ], NCELL(pNode)-
39837 31 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44  1);.  pNode->isD
39838 69 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  irty = 1;.}../*.
39839 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63 6f  ** Insert the co
3983a 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70  ntents of cell p
3983b 43 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20 70  Cell into node p
3983c 4e 6f 64 65 2e 20 49 66 20 74 68 65 20 69 6e 73  Node. If the ins
3983d 65 72 74 0a 2a 2a 20 69 73 20 73 75 63 63 65 73  ert.** is succes
3983e 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c  sful, return SQL
3983f 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  ITE_OK..**.** If
39840 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 65 6e   there is not en
39841 6f 75 67 68 20 66 72 65 65 20 73 70 61 63 65 20  ough free space 
39842 69 6e 20 70 4e 6f 64 65 2c 20 72 65 74 75 72 6e  in pNode, return
39843 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a 2f   SQLITE_FULL..*/
39844 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65  .static int.node
39845 49 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 52 74  InsertCell(.  Rt
39846 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
39847 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
39848 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  , .  RtreeCell *
39849 70 43 65 6c 6c 20 0a 29 7b 0a 20 20 69 6e 74 20  pCell .){.  int 
3984a 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  nCell;          
3984b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
3984c 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 63  rent number of c
3984d 65 6c 6c 73 20 69 6e 20 70 4e 6f 64 65 20 2a 2f  ells in pNode */
3984e 0a 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 3b  .  int nMaxCell;
3984f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39850 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62   /* Maximum numb
39851 65 72 20 6f 66 20 63 65 6c 6c 73 20 66 6f 72 20  er of cells for 
39852 70 4e 6f 64 65 20 2a 2f 0a 0a 20 20 6e 4d 61 78  pNode */..  nMax
39853 43 65 6c 6c 20 3d 20 28 70 52 74 72 65 65 2d 3e  Cell = (pRtree->
39854 69 4e 6f 64 65 53 69 7a 65 2d 34 29 2f 70 52 74  iNodeSize-4)/pRt
39855 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65  ree->nBytesPerCe
39856 6c 6c 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43  ll;.  nCell = NC
39857 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 0a 20 20 61  ELL(pNode);..  a
39858 73 73 65 72 74 28 6e 43 65 6c 6c 3c 3d 6e 4d 61  ssert(nCell<=nMa
39859 78 43 65 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 6e  xCell);..  if( n
3985a 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 20 29 7b  Cell<nMaxCell ){
3985b 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69  .    nodeOverwri
3985c 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
3985d 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 6e 43 65  Node, pCell, nCe
3985e 6c 6c 29 3b 0a 20 20 20 20 77 72 69 74 65 49 6e  ll);.    writeIn
3985f 74 31 36 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74  t16(&pNode->zDat
39860 61 5b 32 5d 2c 20 6e 43 65 6c 6c 2b 31 29 3b 0a  a[2], nCell+1);.
39861 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72      pNode->isDir
39862 74 79 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72  ty = 1;.  }..  r
39863 65 74 75 72 6e 20 28 6e 43 65 6c 6c 3d 3d 6e 4d  eturn (nCell==nM
39864 61 78 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  axCell);.}../*.*
39865 2a 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73  * If the node is
39866 20 64 69 72 74 79 2c 20 77 72 69 74 65 20 69 74   dirty, write it
39867 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
39868 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
39869 69 6e 74 0a 6e 6f 64 65 57 72 69 74 65 28 52 74  int.nodeWrite(Rt
3986a 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3986b 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
3986c 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3986d 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64  E_OK;.  if( pNod
3986e 65 2d 3e 69 73 44 69 72 74 79 20 29 7b 0a 20 20  e->isDirty ){.  
3986f 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
39870 70 20 3d 20 70 52 74 72 65 65 2d 3e 70 57 72 69  p = pRtree->pWri
39871 74 65 4e 6f 64 65 3b 0a 20 20 20 20 69 66 28 20  teNode;.    if( 
39872 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 29 7b 0a  pNode->iNode ){.
39873 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69        sqlite3_bi
39874 6e 64 5f 69 6e 74 36 34 28 70 2c 20 31 2c 20 70  nd_int64(p, 1, p
39875 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
39876 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
39877 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c  qlite3_bind_null
39878 28 70 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20  (p, 1);.    }.  
39879 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62    sqlite3_bind_b
3987a 6c 6f 62 28 70 2c 20 32 2c 20 70 4e 6f 64 65 2d  lob(p, 2, pNode-
3987b 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e  >zData, pRtree->
3987c 69 4e 6f 64 65 53 69 7a 65 2c 20 53 51 4c 49 54  iNodeSize, SQLIT
3987d 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  E_STATIC);.    s
3987e 71 6c 69 74 65 33 5f 73 74 65 70 28 70 29 3b 0a  qlite3_step(p);.
3987f 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72      pNode->isDir
39880 74 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  ty = 0;.    rc =
39881 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
39882 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65  );.    if( pNode
39883 2d 3e 69 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63  ->iNode==0 && rc
39884 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39885 20 20 20 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64       pNode->iNod
39886 65 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  e = sqlite3_last
39887 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70 52  _insert_rowid(pR
39888 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20  tree->db);.     
39889 20 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28   nodeHashInsert(
3988a 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
3988b 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
3988c 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
3988d 52 65 6c 65 61 73 65 20 61 20 72 65 66 65 72 65  Release a refere
3988e 6e 63 65 20 74 6f 20 61 20 6e 6f 64 65 2e 20 49  nce to a node. I
3988f 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 64 69  f the node is di
39890 72 74 79 20 61 6e 64 20 74 68 65 20 72 65 66 65  rty and the refe
39891 72 65 6e 63 65 0a 2a 2a 20 63 6f 75 6e 74 20 64  rence.** count d
39892 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2c 20 74 68  rops to zero, th
39893 65 20 6e 6f 64 65 20 64 61 74 61 20 69 73 20 77  e node data is w
39894 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
39895 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  tabase..*/.stati
39896 63 20 69 6e 74 0a 6e 6f 64 65 52 65 6c 65 61 73  c int.nodeReleas
39897 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
39898 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
39899 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
3989a 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
3989b 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 61 73 73  pNode ){.    ass
3989c 65 72 74 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  ert( pNode->nRef
3989d 3e 30 20 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  >0 );.    pNode-
3989e 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28  >nRef--;.    if(
3989f 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 3d 3d 30 20   pNode->nRef==0 
398a0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 6f  ){.      if( pNo
398a1 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a  de->iNode==1 ){.
398a2 20 20 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e          pRtree->
398a3 69 44 65 70 74 68 20 3d 20 2d 31 3b 0a 20 20 20  iDepth = -1;.   
398a4 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
398a5 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 29 7b  Node->pParent ){
398a6 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  .        rc = no
398a7 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
398a8 2c 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  , pNode->pParent
398a9 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
398aa 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
398ab 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
398ac 20 3d 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74   = nodeWrite(pRt
398ad 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20  ree, pNode);.   
398ae 20 20 20 7d 0a 20 20 20 20 20 20 6e 6f 64 65 48     }.      nodeH
398af 61 73 68 44 65 6c 65 74 65 28 70 52 74 72 65 65  ashDelete(pRtree
398b0 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  , pNode);.      
398b1 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f  sqlite3_free(pNo
398b2 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  de);.    }.  }. 
398b3 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
398b4 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
398b5 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76  64-bit integer v
398b6 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64 20  alue associated 
398b7 77 69 74 68 20 63 65 6c 6c 20 69 43 65 6c 6c 20  with cell iCell 
398b8 6f 66 0a 2a 2a 20 6e 6f 64 65 20 70 4e 6f 64 65  of.** node pNode
398b9 2e 20 49 66 20 70 4e 6f 64 65 20 69 73 20 61 20  . If pNode is a 
398ba 6c 65 61 66 20 6e 6f 64 65 2c 20 74 68 69 73 20  leaf node, this 
398bb 69 73 20 61 20 72 6f 77 69 64 2e 20 49 66 20 69  is a rowid. If i
398bc 74 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 72  t is.** an inter
398bd 6e 61 6c 20 6e 6f 64 65 2c 20 74 68 65 6e 20 74  nal node, then t
398be 68 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65  he 64-bit intege
398bf 72 20 69 73 20 61 20 63 68 69 6c 64 20 70 61 67  r is a child pag
398c0 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
398c1 74 69 63 20 69 36 34 20 6e 6f 64 65 47 65 74 52  tic i64 nodeGetR
398c2 6f 77 69 64 28 0a 20 20 52 74 72 65 65 20 2a 70  owid(.  Rtree *p
398c3 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e  Rtree, .  RtreeN
398c4 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69  ode *pNode, .  i
398c5 6e 74 20 69 43 65 6c 6c 0a 29 7b 0a 20 20 61 73  nt iCell.){.  as
398c6 73 65 72 74 28 20 69 43 65 6c 6c 3c 4e 43 45 4c  sert( iCell<NCEL
398c7 4c 28 70 4e 6f 64 65 29 20 29 3b 0a 20 20 72 65  L(pNode) );.  re
398c8 74 75 72 6e 20 72 65 61 64 49 6e 74 36 34 28 26  turn readInt64(&
398c9 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b  pNode->zData[4 +
398ca 20 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50   pRtree->nBytesP
398cb 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 29 3b 0a  erCell*iCell]);.
398cc 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
398cd 63 6f 6f 72 64 69 6e 61 74 65 20 69 43 6f 6f 72  coordinate iCoor
398ce 64 20 66 72 6f 6d 20 63 65 6c 6c 20 69 43 65 6c  d from cell iCel
398cf 6c 20 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l in node pNode.
398d0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
398d1 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 0a 20 20  nodeGetCoord(.  
398d2 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
398d3 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
398d4 64 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c  de, .  int iCell
398d5 2c 0a 20 20 69 6e 74 20 69 43 6f 6f 72 64 2c 0a  ,.  int iCoord,.
398d6 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43    RtreeCoord *pC
398d7 6f 6f 72 64 20 20 20 20 20 20 20 20 20 20 20 2f  oord           /
398d8 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65  * Space to write
398d9 20 72 65 73 75 6c 74 20 74 6f 20 2a 2f 0a 29 7b   result to */.){
398da 0a 20 20 72 65 61 64 43 6f 6f 72 64 28 26 70 4e  .  readCoord(&pN
398db 6f 64 65 2d 3e 7a 44 61 74 61 5b 31 32 20 2b 20  ode->zData[12 + 
398dc 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
398dd 72 43 65 6c 6c 2a 69 43 65 6c 6c 20 2b 20 34 2a  rCell*iCell + 4*
398de 69 43 6f 6f 72 64 5d 2c 20 70 43 6f 6f 72 64 29  iCoord], pCoord)
398df 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72  ;.}../*.** Deser
398e0 69 61 6c 69 7a 65 20 63 65 6c 6c 20 69 43 65 6c  ialize cell iCel
398e1 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l of node pNode.
398e2 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 73 74   Populate the st
398e3 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 0a  ructure pointed.
398e4 2a 2a 20 74 6f 20 62 79 20 70 43 65 6c 6c 20 77  ** to by pCell w
398e5 69 74 68 20 74 68 65 20 72 65 73 75 6c 74 73 2e  ith the results.
398e6 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
398e7 6e 6f 64 65 47 65 74 43 65 6c 6c 28 0a 20 20 52  nodeGetCell(.  R
398e8 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
398e9 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
398ea 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c  e, .  int iCell,
398eb 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
398ec 65 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b  ell.){.  int ii;
398ed 0a 20 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  .  pCell->iRowid
398ee 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28   = nodeGetRowid(
398ef 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
398f0 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69 3d  Cell);.  for(ii=
398f1 30 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44  0; ii<pRtree->nD
398f2 69 6d 2a 32 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  im*2; ii++){.   
398f3 20 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70 52   nodeGetCoord(pR
398f4 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65  tree, pNode, iCe
398f5 6c 6c 2c 20 69 69 2c 20 26 70 43 65 6c 6c 2d 3e  ll, ii, &pCell->
398f6 61 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a 20 20 7d  aCoord[ii]);.  }
398f7 0a 7d 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  .}.../* Forward 
398f8 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20  declaration for 
398f9 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  the function tha
398fa 74 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20  t does the work 
398fb 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61  of.** the virtua
398fc 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
398fd 43 72 65 61 74 65 28 29 20 61 6e 64 20 78 43 6f  Create() and xCo
398fe 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 73 2e  nnect() methods.
398ff 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
39900 74 72 65 65 49 6e 69 74 28 0a 20 20 73 71 6c 69  treeInit(.  sqli
39901 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69  te3 *, void *, i
39902 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  nt, const char *
39903 63 6f 6e 73 74 2a 2c 20 73 71 6c 69 74 65 33 5f  const*, sqlite3_
39904 76 74 61 62 20 2a 2a 2c 20 63 68 61 72 20 2a 2a  vtab **, char **
39905 2c 20 69 6e 74 0a 29 3b 0a 0a 2f 2a 20 0a 2a 2a  , int.);../* .**
39906 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
39907 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72 65  able module xCre
39908 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ate method..*/.s
39909 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 43  tatic int rtreeC
3990a 72 65 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33  reate(.  sqlite3
3990b 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70 41   *db,.  void *pA
3990c 75 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  ux,.  int argc, 
3990d 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
3990e 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65  t*argv,.  sqlite
3990f 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
39910 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a  .  char **pzErr.
39911 29 7b 0a 20 20 72 65 74 75 72 6e 20 72 74 72 65  ){.  return rtre
39912 65 49 6e 69 74 28 64 62 2c 20 70 41 75 78 2c 20  eInit(db, pAux, 
39913 61 72 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74  argc, argv, ppVt
39914 61 62 2c 20 70 7a 45 72 72 2c 20 31 29 3b 0a 7d  ab, pzErr, 1);.}
39915 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
39916 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
39917 75 6c 65 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74  ule xConnect met
39918 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
39919 6e 74 20 72 74 72 65 65 43 6f 6e 6e 65 63 74 28  nt rtreeConnect(
3991a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
3991b 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20    void *pAux,.  
3991c 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20  int argc, const 
3991d 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76  char *const*argv
3991e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ,.  sqlite3_vtab
3991f 20 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68 61   **ppVtab,.  cha
39920 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 72  r **pzErr.){.  r
39921 65 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74 28  eturn rtreeInit(
39922 64 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c 20  db, pAux, argc, 
39923 61 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70 7a  argv, ppVtab, pz
39924 45 72 72 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Err, 0);.}../*.*
39925 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  * Increment the 
39926 72 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65  r-tree reference
39927 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69   count..*/.stati
39928 63 20 76 6f 69 64 20 72 74 72 65 65 52 65 66 65  c void rtreeRefe
39929 72 65 6e 63 65 28 52 74 72 65 65 20 2a 70 52 74  rence(Rtree *pRt
3992a 72 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d 3e  ree){.  pRtree->
3992b 6e 42 75 73 79 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a  nBusy++;.}../*.*
3992c 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  * Decrement the 
3992d 72 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65  r-tree reference
3992e 20 63 6f 75 6e 74 2e 20 57 68 65 6e 20 74 68 65   count. When the
3992f 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
39930 20 72 65 61 63 68 65 73 0a 2a 2a 20 7a 65 72 6f   reaches.** zero
39931 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 69   the structure i
39932 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74  s deleted..*/.st
39933 61 74 69 63 20 76 6f 69 64 20 72 74 72 65 65 52  atic void rtreeR
39934 65 6c 65 61 73 65 28 52 74 72 65 65 20 2a 70 52  elease(Rtree *pR
39935 74 72 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d  tree){.  pRtree-
39936 3e 6e 42 75 73 79 2d 2d 3b 0a 20 20 69 66 28 20  >nBusy--;.  if( 
39937 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 3d 3d 30  pRtree->nBusy==0
39938 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
39939 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d  finalize(pRtree-
3993a 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20 20  >pReadNode);.   
3993b 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
3993c 65 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  e(pRtree->pWrite
3993d 4e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Node);.    sqlit
3993e 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
3993f 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29  ee->pDeleteNode)
39940 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
39941 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
39942 52 65 61 64 52 6f 77 69 64 29 3b 0a 20 20 20 20  ReadRowid);.    
39943 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
39944 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  (pRtree->pWriteR
39945 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
39946 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
39947 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
39948 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
39949 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
3994a 70 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20 20  pReadParent);.  
3994b 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
3994c 7a 65 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  ze(pRtree->pWrit
3994d 65 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71  eParent);.    sq
3994e 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
3994f 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
39950 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  rent);.    sqlit
39951 65 33 5f 66 72 65 65 28 70 52 74 72 65 65 29 3b  e3_free(pRtree);
39952 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  .  }.}../* .** R
39953 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
39954 6c 65 20 6d 6f 64 75 6c 65 20 78 44 69 73 63 6f  le module xDisco
39955 6e 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f  nnect method..*/
39956 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
39957 65 44 69 73 63 6f 6e 6e 65 63 74 28 73 71 6c 69  eDisconnect(sqli
39958 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
39959 7b 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65  {.  rtreeRelease
3995a 28 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 29  ((Rtree *)pVtab)
3995b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
3995c 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  E_OK;.}../* .** 
3995d 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61  Rtree virtual ta
3995e 62 6c 65 20 6d 6f 64 75 6c 65 20 78 44 65 73 74  ble module xDest
3995f 72 6f 79 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  roy method..*/.s
39960 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 44  tatic int rtreeD
39961 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 76  estroy(sqlite3_v
39962 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 52  tab *pVtab){.  R
39963 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
39964 52 74 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20  Rtree *)pVtab;. 
39965 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20   int rc;.  char 
39966 2a 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74  *zCreate = sqlit
39967 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20  e3_mprintf(.    
39968 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27  "DROP TABLE '%q'
39969 2e 27 25 71 5f 6e 6f 64 65 27 3b 22 0a 20 20 20  .'%q_node';".   
3996a 20 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71   "DROP TABLE '%q
3996b 27 2e 27 25 71 5f 72 6f 77 69 64 27 3b 22 0a 20  '.'%q_rowid';". 
3996c 20 20 20 22 44 52 4f 50 20 54 41 42 4c 45 20 27     "DROP TABLE '
3996d 25 71 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 3b  %q'.'%q_parent';
3996e 22 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a  ",.    pRtree->z
3996f 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d  Db, pRtree->zNam
39970 65 2c 20 0a 20 20 20 20 70 52 74 72 65 65 2d 3e  e, .    pRtree->
39971 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
39972 6d 65 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e  me,.    pRtree->
39973 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
39974 6d 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 7a  me.  );.  if( !z
39975 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 72 63  Create ){.    rc
39976 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
39977 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
39978 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
39979 70 52 74 72 65 65 2d 3e 64 62 2c 20 7a 43 72 65  pRtree->db, zCre
3997a 61 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ate, 0, 0, 0);. 
3997b 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
3997c 7a 43 72 65 61 74 65 29 3b 0a 20 20 7d 0a 20 20  zCreate);.  }.  
3997d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3997e 4b 20 29 7b 0a 20 20 20 20 72 74 72 65 65 52 65  K ){.    rtreeRe
3997f 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20  lease(pRtree);. 
39980 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
39981 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  .}../* .** Rtree
39982 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
39983 6f 64 75 6c 65 20 78 4f 70 65 6e 20 6d 65 74 68  odule xOpen meth
39984 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
39985 74 20 72 74 72 65 65 4f 70 65 6e 28 73 71 6c 69  t rtreeOpen(sqli
39986 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c  te3_vtab *pVTab,
39987 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
39988 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29  rsor **ppCursor)
39989 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
3998a 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 52 74 72  ITE_NOMEM;.  Rtr
3998b 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 0a  eeCursor *pCsr;.
3998c 0a 20 20 70 43 73 72 20 3d 20 28 52 74 72 65 65  .  pCsr = (Rtree
3998d 43 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33  Cursor *)sqlite3
3998e 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
3998f 74 72 65 65 43 75 72 73 6f 72 29 29 3b 0a 20 20  treeCursor));.  
39990 69 66 28 20 70 43 73 72 20 29 7b 0a 20 20 20 20  if( pCsr ){.    
39991 6d 65 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20  memset(pCsr, 0, 
39992 73 69 7a 65 6f 66 28 52 74 72 65 65 43 75 72 73  sizeof(RtreeCurs
39993 6f 72 29 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e  or));.    pCsr->
39994 62 61 73 65 2e 70 56 74 61 62 20 3d 20 70 56 54  base.pVtab = pVT
39995 61 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ab;.    rc = SQL
39996 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 2a 70  ITE_OK;.  }.  *p
39997 70 43 75 72 73 6f 72 20 3d 20 28 73 71 6c 69 74  pCursor = (sqlit
39998 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
39999 29 70 43 73 72 3b 0a 0a 20 20 72 65 74 75 72 6e  )pCsr;..  return
3999a 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52   rc;.}../* .** R
3999b 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
3999c 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6c 6f 73 65  le module xClose
3999d 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
3999e 69 63 20 69 6e 74 20 72 74 72 65 65 43 6c 6f 73  ic int rtreeClos
3999f 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  e(sqlite3_vtab_c
399a0 75 72 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52  ursor *cur){.  R
399a1 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
399a2 52 74 72 65 65 20 2a 29 28 63 75 72 2d 3e 70 56  Rtree *)(cur->pV
399a3 74 61 62 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  tab);.  int rc;.
399a4 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70    RtreeCursor *p
399a5 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73  Csr = (RtreeCurs
399a6 6f 72 20 2a 29 63 75 72 3b 0a 20 20 73 71 6c 69  or *)cur;.  sqli
399a7 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61  te3_free(pCsr->a
399a8 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 72  Constraint);.  r
399a9 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
399aa 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e  pRtree, pCsr->pN
399ab 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ode);.  sqlite3_
399ac 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 72 65  free(pCsr);.  re
399ad 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
399ae 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20  * Rtree virtual 
399af 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 45 6f  table module xEo
399b0 66 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20  f method..**.** 
399b1 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
399b2 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f  if the cursor do
399b3 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  es not currently
399b4 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69   point to a vali
399b5 64 20 0a 2a 2a 20 72 65 63 6f 72 64 20 28 69 2e  d .** record (i.
399b6 65 20 69 66 20 74 68 65 20 73 63 61 6e 20 68 61  e if the scan ha
399b7 73 20 66 69 6e 69 73 68 65 64 29 2c 20 6f 72 20  s finished), or 
399b8 7a 65 72 6f 20 6f 74 68 65 72 77 69 73 65 2e 0a  zero otherwise..
399b9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
399ba 72 65 65 45 6f 66 28 73 71 6c 69 74 65 33 5f 76  reeEof(sqlite3_v
399bb 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72 29  tab_cursor *cur)
399bc 7b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20  {.  RtreeCursor 
399bd 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75  *pCsr = (RtreeCu
399be 72 73 6f 72 20 2a 29 63 75 72 3b 0a 20 20 72 65  rsor *)cur;.  re
399bf 74 75 72 6e 20 28 70 43 73 72 2d 3e 70 4e 6f 64  turn (pCsr->pNod
399c0 65 3d 3d 30 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  e==0);.}../* .**
399c1 20 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20   Cursor pCursor 
399c2 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73  currently points
399c3 20 74 6f 20 61 20 63 65 6c 6c 20 69 6e 20 61 20   to a cell in a 
399c4 6e 6f 6e 2d 6c 65 61 66 20 70 61 67 65 2e 0a 2a  non-leaf page..*
399c5 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
399c6 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68 65   the sub-tree he
399c7 61 64 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c  aded by the cell
399c8 20 69 73 20 66 69 6c 74 65 72 65 64 0a 2a 2a 20   is filtered.** 
399c9 28 65 78 63 6c 75 64 65 64 29 20 62 79 20 74 68  (excluded) by th
399ca 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e  e constraints in
399cb 20 74 68 65 20 70 43 75 72 73 6f 72 2d 3e 61 43   the pCursor->aC
399cc 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 0a 2a 2a 20  onstraint[] .** 
399cd 61 72 72 61 79 2c 20 6f 72 20 66 61 6c 73 65 20  array, or false 
399ce 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
399cf 61 74 69 63 20 69 6e 74 20 74 65 73 74 52 74 72  atic int testRtr
399d0 65 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52  eeCell(Rtree *pR
399d1 74 72 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f  tree, RtreeCurso
399d2 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52  r *pCursor){.  R
399d3 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
399d4 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 62   int ii;.  int b
399d5 52 65 73 20 3d 20 30 3b 0a 0a 20 20 6e 6f 64 65  Res = 0;..  node
399d6 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
399d7 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
399d8 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20  pCursor->iCell, 
399d9 26 63 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69  &cell);.  for(ii
399da 3d 30 3b 20 62 52 65 73 3d 3d 30 20 26 26 20 69  =0; bRes==0 && i
399db 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43 6f 6e 73  i<pCursor->nCons
399dc 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  traint; ii++){. 
399dd 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69     RtreeConstrai
399de 6e 74 20 2a 70 20 3d 20 26 70 43 75 72 73 6f 72  nt *p = &pCursor
399df 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69  ->aConstraint[ii
399e0 5d 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 65  ];.    double ce
399e1 6c 6c 5f 6d 69 6e 20 3d 20 44 43 4f 4f 52 44 28  ll_min = DCOORD(
399e2 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e  cell.aCoord[(p->
399e3 69 43 6f 6f 72 64 3e 3e 31 29 2a 32 5d 29 3b 0a  iCoord>>1)*2]);.
399e4 20 20 20 20 64 6f 75 62 6c 65 20 63 65 6c 6c 5f      double cell_
399e5 6d 61 78 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c  max = DCOORD(cel
399e6 6c 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f  l.aCoord[(p->iCo
399e7 6f 72 64 3e 3e 31 29 2a 32 2b 31 5d 29 3b 0a 0a  ord>>1)*2+1]);..
399e8 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 6f 70      assert(p->op
399e9 3d 3d 52 54 52 45 45 5f 4c 45 20 7c 7c 20 70 2d  ==RTREE_LE || p-
399ea 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 54 20 7c 7c  >op==RTREE_LT ||
399eb 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 45   p->op==RTREE_GE
399ec 20 0a 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e   .        || p->
399ed 6f 70 3d 3d 52 54 52 45 45 5f 47 54 20 7c 7c 20  op==RTREE_GT || 
399ee 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 45 51 0a  p->op==RTREE_EQ.
399ef 20 20 20 20 29 3b 0a 0a 20 20 20 20 73 77 69 74      );..    swit
399f0 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20  ch( p->op ){.   
399f1 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 4c 45     case RTREE_LE
399f2 3a 20 63 61 73 65 20 52 54 52 45 45 5f 4c 54 3a  : case RTREE_LT:
399f3 20 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c 75   bRes = p->rValu
399f4 65 3c 63 65 6c 6c 5f 6d 69 6e 3b 20 62 72 65 61  e<cell_min; brea
399f5 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54  k;.      case RT
399f6 52 45 45 5f 47 45 3a 20 63 61 73 65 20 52 54 52  REE_GE: case RTR
399f7 45 45 5f 47 54 3a 20 62 52 65 73 20 3d 20 70 2d  EE_GT: bRes = p-
399f8 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d 61 78  >rValue>cell_max
399f9 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63  ; break;.      c
399fa 61 73 65 20 52 54 52 45 45 5f 45 51 3a 20 0a 20  ase RTREE_EQ: . 
399fb 20 20 20 20 20 20 20 62 52 65 73 20 3d 20 28 70         bRes = (p
399fc 2d 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d 61  ->rValue>cell_ma
399fd 78 20 7c 7c 20 70 2d 3e 72 56 61 6c 75 65 3c 63  x || p->rValue<c
399fe 65 6c 6c 5f 6d 69 6e 29 3b 0a 20 20 20 20 20 20  ell_min);.      
399ff 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
39a00 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 62 52 65   }..  return bRe
39a01 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74  s;.}../* .** Ret
39a02 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
39a03 63 65 6c 6c 20 74 68 61 74 20 63 75 72 73 6f 72  cell that cursor
39a04 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74   pCursor current
39a05 6c 79 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20  ly points to.** 
39a06 77 6f 75 6c 64 20 62 65 20 66 69 6c 74 65 72 65  would be filtere
39a07 64 20 28 65 78 63 6c 75 64 65 64 29 20 62 79 20  d (excluded) by 
39a08 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  the constraints 
39a09 69 6e 20 74 68 65 20 0a 2a 2a 20 70 43 75 72 73  in the .** pCurs
39a0a 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  or->aConstraint[
39a0b 5d 20 61 72 72 61 79 2c 20 6f 72 20 66 61 6c 73  ] array, or fals
39a0c 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a  e otherwise..**.
39a0d 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
39a0e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
39a0f 65 20 63 65 6c 6c 20 69 73 20 70 61 72 74 20 6f  e cell is part o
39a10 66 20 61 20 6c 65 61 66 20 6e 6f 64 65 2e 0a 2a  f a leaf node..*
39a11 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 73  /.static int tes
39a12 74 52 74 72 65 65 45 6e 74 72 79 28 52 74 72 65  tRtreeEntry(Rtre
39a13 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
39a14 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  Cursor *pCursor)
39a15 7b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65  {.  RtreeCell ce
39a16 6c 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20  ll;.  int ii;.. 
39a17 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74   nodeGetCell(pRt
39a18 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e  ree, pCursor->pN
39a19 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e 69 43  ode, pCursor->iC
39a1a 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 66  ell, &cell);.  f
39a1b 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 72  or(ii=0; ii<pCur
39a1c 73 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  sor->nConstraint
39a1d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72  ; ii++){.    Rtr
39a1e 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20  eeConstraint *p 
39a1f 3d 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e  = &pCursor->aCon
39a20 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20  straint[ii];.   
39a21 20 64 6f 75 62 6c 65 20 63 6f 6f 72 64 20 3d 20   double coord = 
39a22 44 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f  DCOORD(cell.aCoo
39a23 72 64 5b 70 2d 3e 69 43 6f 6f 72 64 5d 29 3b 0a  rd[p->iCoord]);.
39a24 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
39a25 20 61 73 73 65 72 74 28 70 2d 3e 6f 70 3d 3d 52   assert(p->op==R
39a26 54 52 45 45 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70  TREE_LE || p->op
39a27 3d 3d 52 54 52 45 45 5f 4c 54 20 7c 7c 20 70 2d  ==RTREE_LT || p-
39a28 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 45 20 0a 20  >op==RTREE_GE . 
39a29 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d         || p->op=
39a2a 3d 52 54 52 45 45 5f 47 54 20 7c 7c 20 70 2d 3e  =RTREE_GT || p->
39a2b 6f 70 3d 3d 52 54 52 45 45 5f 45 51 0a 20 20 20  op==RTREE_EQ.   
39a2c 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20   );.    switch( 
39a2d 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 63  p->op ){.      c
39a2e 61 73 65 20 52 54 52 45 45 5f 4c 45 3a 20 72 65  ase RTREE_LE: re
39a2f 73 20 3d 20 28 63 6f 6f 72 64 3c 3d 70 2d 3e 72  s = (coord<=p->r
39a30 56 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a 20  Value); break;. 
39a31 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
39a32 4c 54 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64  LT: res = (coord
39a33 3c 70 2d 3e 72 56 61 6c 75 65 29 3b 20 20 62 72  <p->rValue);  br
39a34 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
39a35 52 54 52 45 45 5f 47 45 3a 20 72 65 73 20 3d 20  RTREE_GE: res = 
39a36 28 63 6f 6f 72 64 3e 3d 70 2d 3e 72 56 61 6c 75  (coord>=p->rValu
39a37 65 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  e); break;.     
39a38 20 63 61 73 65 20 52 54 52 45 45 5f 47 54 3a 20   case RTREE_GT: 
39a39 72 65 73 20 3d 20 28 63 6f 6f 72 64 3e 70 2d 3e  res = (coord>p->
39a3a 72 56 61 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b  rValue);  break;
39a3b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45  .      case RTRE
39a3c 45 5f 45 51 3a 20 72 65 73 20 3d 20 28 63 6f 6f  E_EQ: res = (coo
39a3d 72 64 3d 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20  rd==p->rValue); 
39a3e 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
39a3f 20 20 69 66 28 20 21 72 65 73 20 29 20 72 65 74    if( !res ) ret
39a40 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65  urn 1;.  }..  re
39a41 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
39a42 20 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20   Cursor pCursor 
39a43 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73  currently points
39a44 20 61 74 20 61 20 6e 6f 64 65 20 74 68 61 74 20   at a node that 
39a45 68 65 61 64 73 20 61 20 73 75 62 2d 74 72 65 65  heads a sub-tree
39a46 20 6f 66 0a 2a 2a 20 68 65 69 67 68 74 20 69 48   of.** height iH
39a47 65 69 67 68 74 20 28 69 66 20 69 48 65 69 67 68  eight (if iHeigh
39a48 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 6e  t==0, then the n
39a49 6f 64 65 20 69 73 20 61 20 6c 65 61 66 29 2e 20  ode is a leaf). 
39a4a 44 65 73 63 65 6e 64 0a 2a 2a 20 74 6f 20 70 6f  Descend.** to po
39a4b 69 6e 74 20 74 6f 20 74 68 65 20 6c 65 66 74 2d  int to the left-
39a4c 6d 6f 73 74 20 63 65 6c 6c 20 6f 66 20 74 68 65  most cell of the
39a4d 20 73 75 62 2d 74 72 65 65 20 74 68 61 74 20 6d   sub-tree that m
39a4e 61 74 63 68 65 73 20 74 68 65 20 0a 2a 2a 20 63  atches the .** c
39a4f 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e 73 74 72  onfigured constr
39a50 61 69 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  aints..*/.static
39a51 20 69 6e 74 20 64 65 73 63 65 6e 64 54 6f 43 65   int descendToCe
39a52 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ll(.  Rtree *pRt
39a53 72 65 65 2c 20 0a 20 20 52 74 72 65 65 43 75 72  ree, .  RtreeCur
39a54 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 0a 20  sor *pCursor, . 
39a55 20 69 6e 74 20 69 48 65 69 67 68 74 2c 0a 20 20   int iHeight,.  
39a56 69 6e 74 20 2a 70 45 6f 66 20 20 20 20 20 20 20  int *pEof       
39a57 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
39a58 3a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 66  : Set to true if
39a59 20 63 61 6e 6e 6f 74 20 64 65 73 63 65 6e 64 20   cannot descend 
39a5a 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 73 45 6f  */.){.  int isEo
39a5b 66 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  f;.  int rc;.  i
39a5c 6e 74 20 69 69 3b 0a 20 20 52 74 72 65 65 4e 6f  nt ii;.  RtreeNo
39a5d 64 65 20 2a 70 43 68 69 6c 64 3b 0a 20 20 73 71  de *pChild;.  sq
39a5e 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
39a5f 69 64 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64 65  id;..  RtreeNode
39a60 20 2a 70 53 61 76 65 64 4e 6f 64 65 20 3d 20 70   *pSavedNode = p
39a61 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 3b 0a 20  Cursor->pNode;. 
39a62 20 69 6e 74 20 69 53 61 76 65 64 43 65 6c 6c 20   int iSavedCell 
39a63 3d 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c  = pCursor->iCell
39a64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 48 65  ;..  assert( iHe
39a65 69 67 68 74 3e 3d 30 20 29 3b 0a 0a 20 20 69 66  ight>=0 );..  if
39a66 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a  ( iHeight==0 ){.
39a67 20 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73 74      isEof = test
39a68 52 74 72 65 65 45 6e 74 72 79 28 70 52 74 72 65  RtreeEntry(pRtre
39a69 65 2c 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  e, pCursor);.  }
39a6a 65 6c 73 65 7b 0a 20 20 20 20 69 73 45 6f 66 20  else{.    isEof 
39a6b 3d 20 74 65 73 74 52 74 72 65 65 43 65 6c 6c 28  = testRtreeCell(
39a6c 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 29  pRtree, pCursor)
39a6d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 45 6f  ;.  }.  if( isEo
39a6e 66 20 7c 7c 20 69 48 65 69 67 68 74 3d 3d 30 20  f || iHeight==0 
39a6f 29 7b 0a 20 20 20 20 2a 70 45 6f 66 20 3d 20 69  ){.    *pEof = i
39a70 73 45 6f 66 3b 0a 20 20 20 20 72 65 74 75 72 6e  sEof;.    return
39a71 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
39a72 0a 20 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65  .  iRowid = node
39a73 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
39a74 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c   pCursor->pNode,
39a75 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 29   pCursor->iCell)
39a76 3b 0a 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71  ;.  rc = nodeAcq
39a77 75 69 72 65 28 70 52 74 72 65 65 2c 20 69 52 6f  uire(pRtree, iRo
39a78 77 69 64 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e  wid, pCursor->pN
39a79 6f 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0a 20  ode, &pChild);. 
39a7a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
39a7b 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
39a7c 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 6e 6f 64 65   rc;.  }..  node
39a7d 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
39a7e 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 29 3b  pCursor->pNode);
39a7f 0a 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64  .  pCursor->pNod
39a80 65 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 69 73  e = pChild;.  is
39a81 45 6f 66 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69  Eof = 1;.  for(i
39a82 69 3d 30 3b 20 69 73 45 6f 66 20 26 26 20 69 69  i=0; isEof && ii
39a83 3c 4e 43 45 4c 4c 28 70 43 68 69 6c 64 29 3b 20  <NCELL(pChild); 
39a84 69 69 2b 2b 29 7b 0a 20 20 20 20 70 43 75 72 73  ii++){.    pCurs
39a85 6f 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 69 3b 0a  or->iCell = ii;.
39a86 20 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64      rc = descend
39a87 54 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  ToCell(pRtree, p
39a88 43 75 72 73 6f 72 2c 20 69 48 65 69 67 68 74 2d  Cursor, iHeight-
39a89 31 2c 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20  1, &isEof);.    
39a8a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
39a8b 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
39a8c 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
39a8d 0a 20 20 69 66 28 20 69 73 45 6f 66 20 29 7b 0a  .  if( isEof ){.
39a8e 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
39a8f 73 6f 72 2d 3e 70 4e 6f 64 65 3d 3d 70 43 68 69  sor->pNode==pChi
39a90 6c 64 20 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65  ld );.    nodeRe
39a91 66 65 72 65 6e 63 65 28 70 53 61 76 65 64 4e 6f  ference(pSavedNo
39a92 64 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 6c  de);.    nodeRel
39a93 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 68  ease(pRtree, pCh
39a94 69 6c 64 29 3b 0a 20 20 20 20 70 43 75 72 73 6f  ild);.    pCurso
39a95 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 53 61 76 65  r->pNode = pSave
39a96 64 4e 6f 64 65 3b 0a 20 20 20 20 70 43 75 72 73  dNode;.    pCurs
39a97 6f 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 53 61 76  or->iCell = iSav
39a98 65 64 43 65 6c 6c 3b 0a 20 20 7d 0a 0a 20 20 2a  edCell;.  }..  *
39a99 70 45 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20 20  pEof = isEof;.  
39a9a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
39a9b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 65 20 6f  ;.}../*.** One o
39a9c 66 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 6e  f the cells in n
39a9d 6f 64 65 20 70 4e 6f 64 65 20 69 73 20 67 75 61  ode pNode is gua
39a9e 72 61 6e 74 65 65 64 20 74 6f 20 68 61 76 65 20  ranteed to have 
39a9f 61 20 36 34 2d 62 69 74 20 0a 2a 2a 20 69 6e 74  a 64-bit .** int
39aa0 65 67 65 72 20 76 61 6c 75 65 20 65 71 75 61 6c  eger value equal
39aa1 20 74 6f 20 69 52 6f 77 69 64 2e 20 52 65 74 75   to iRowid. Retu
39aa2 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
39aa3 74 68 69 73 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74  this cell..*/.st
39aa4 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 52 6f 77  atic int nodeRow
39aa5 69 64 49 6e 64 65 78 28 52 74 72 65 65 20 2a 70  idIndex(Rtree *p
39aa6 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
39aa7 20 2a 70 4e 6f 64 65 2c 20 69 36 34 20 69 52 6f   *pNode, i64 iRo
39aa8 77 69 64 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  wid){.  int ii;.
39aa9 20 20 66 6f 72 28 69 69 3d 30 3b 20 6e 6f 64 65    for(ii=0; node
39aaa 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
39aab 20 70 4e 6f 64 65 2c 20 69 69 29 21 3d 69 52 6f   pNode, ii)!=iRo
39aac 77 69 64 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  wid; ii++){.    
39aad 61 73 73 65 72 74 28 20 69 69 3c 28 4e 43 45 4c  assert( ii<(NCEL
39aae 4c 28 70 4e 6f 64 65 29 2d 31 29 20 29 3b 0a 20  L(pNode)-1) );. 
39aaf 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 69 3b 0a   }.  return ii;.
39ab0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
39ab1 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
39ab2 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e 67   cell containing
39ab3 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 6e 6f   a pointer to no
39ab4 64 65 20 70 4e 6f 64 65 0a 2a 2a 20 69 6e 20 69  de pNode.** in i
39ab5 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20 70 4e  ts parent. If pN
39ab6 6f 64 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20  ode is the root 
39ab7 6e 6f 64 65 2c 20 72 65 74 75 72 6e 20 2d 31 2e  node, return -1.
39ab8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
39ab9 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 52  odeParentIndex(R
39aba 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
39abb 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b  reeNode *pNode){
39abc 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50  .  RtreeNode *pP
39abd 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70  arent = pNode->p
39abe 50 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70 50  Parent;.  if( pP
39abf 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 72 65 74  arent ){.    ret
39ac0 75 72 6e 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64  urn nodeRowidInd
39ac1 65 78 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  ex(pRtree, pPare
39ac2 6e 74 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  nt, pNode->iNode
39ac3 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
39ac4 2d 31 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  -1;.}../* .** Rt
39ac5 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
39ac6 65 20 6d 6f 64 75 6c 65 20 78 4e 65 78 74 20 6d  e module xNext m
39ac7 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
39ac8 20 69 6e 74 20 72 74 72 65 65 4e 65 78 74 28 73   int rtreeNext(s
39ac9 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
39aca 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 29  or *pVtabCursor)
39acb 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  {.  Rtree *pRtre
39acc 65 20 3d 20 28 52 74 72 65 65 20 2a 29 28 70 56  e = (Rtree *)(pV
39acd 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
39ace 29 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72  );.  RtreeCursor
39acf 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43   *pCsr = (RtreeC
39ad0 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75 72  ursor *)pVtabCur
39ad1 73 6f 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  sor;.  int rc = 
39ad2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66  SQLITE_OK;..  if
39ad3 28 20 70 43 73 72 2d 3e 69 53 74 72 61 74 65 67  ( pCsr->iStrateg
39ad4 79 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54  y==1 ){.    /* T
39ad5 68 69 73 20 22 73 63 61 6e 22 20 69 73 20 61 20  his "scan" is a 
39ad6 64 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20 62 79  direct lookup by
39ad7 20 72 6f 77 69 64 2e 20 54 68 65 72 65 20 69 73   rowid. There is
39ad8 20 6e 6f 20 6e 65 78 74 20 65 6e 74 72 79 2e 20   no next entry. 
39ad9 2a 2f 0a 20 20 20 20 6e 6f 64 65 52 65 6c 65 61  */.    nodeRelea
39ada 73 65 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d  se(pRtree, pCsr-
39adb 3e 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 43 73  >pNode);.    pCs
39adc 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20  r->pNode = 0;.  
39add 7d 0a 0a 20 20 65 6c 73 65 20 69 66 28 20 70 43  }..  else if( pC
39ade 73 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20 20 20  sr->pNode ){.   
39adf 20 2f 2a 20 4d 6f 76 65 20 74 6f 20 74 68 65 20   /* Move to the 
39ae0 6e 65 78 74 20 65 6e 74 72 79 20 74 68 61 74 20  next entry that 
39ae1 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f 6e 66  matches the conf
39ae2 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  igured constrain
39ae3 74 73 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ts. */.    int i
39ae4 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20  Height = 0;.    
39ae5 77 68 69 6c 65 28 20 70 43 73 72 2d 3e 70 4e 6f  while( pCsr->pNo
39ae6 64 65 20 29 7b 0a 20 20 20 20 20 20 52 74 72 65  de ){.      Rtre
39ae7 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 20 3d 20 70  eNode *pNode = p
39ae8 43 73 72 2d 3e 70 4e 6f 64 65 3b 0a 20 20 20 20  Csr->pNode;.    
39ae9 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
39aea 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ELL(pNode);.    
39aeb 20 20 66 6f 72 28 70 43 73 72 2d 3e 69 43 65 6c    for(pCsr->iCel
39aec 6c 2b 2b 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c  l++; pCsr->iCell
39aed 3c 6e 43 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43  <nCell; pCsr->iC
39aee 65 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ell++){.        
39aef 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20 20 20 20  int isEof;.     
39af0 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64 54     rc = descendT
39af1 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  oCell(pRtree, pC
39af2 73 72 2c 20 69 48 65 69 67 68 74 2c 20 26 69 73  sr, iHeight, &is
39af3 45 6f 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66  Eof);.        if
39af4 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
39af5 7c 7c 20 21 69 73 45 6f 66 20 29 7b 0a 20 20 20  || !isEof ){.   
39af6 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
39af7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
39af8 20 20 7d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e    }.      pCsr->
39af9 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 2d 3e 70  pNode = pNode->p
39afa 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 70 43  Parent;.      pC
39afb 73 72 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  sr->iCell = node
39afc 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72  ParentIndex(pRtr
39afd 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ee, pNode);.    
39afe 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
39aff 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20  pCsr->pNode);.  
39b00 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
39b01 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
39b02 20 20 20 20 20 20 69 48 65 69 67 68 74 2b 2b 3b        iHeight++;
39b03 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
39b04 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a  turn rc;.}../* .
39b05 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c  ** Rtree virtual
39b06 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 52   table module xR
39b07 6f 77 69 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  owid method..*/.
39b08 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
39b09 52 6f 77 69 64 28 73 71 6c 69 74 65 33 5f 76 74  Rowid(sqlite3_vt
39b0a 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62  ab_cursor *pVtab
39b0b 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69  Cursor, sqlite_i
39b0c 6e 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20  nt64 *pRowid){. 
39b0d 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d   Rtree *pRtree =
39b0e 20 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 43   (Rtree *)pVtabC
39b0f 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20  ursor->pVtab;.  
39b10 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73  RtreeCursor *pCs
39b11 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72  r = (RtreeCursor
39b12 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a   *)pVtabCursor;.
39b13 0a 20 20 61 73 73 65 72 74 28 70 43 73 72 2d 3e  .  assert(pCsr->
39b14 70 4e 6f 64 65 29 3b 0a 20 20 2a 70 52 6f 77 69  pNode);.  *pRowi
39b15 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64  d = nodeGetRowid
39b16 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70  (pRtree, pCsr->p
39b17 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c  Node, pCsr->iCel
39b18 6c 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  l);..  return SQ
39b19 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a  LITE_OK;.}../* .
39b1a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c  ** Rtree virtual
39b1b 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43   table module xC
39b1c 6f 6c 75 6d 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f  olumn method..*/
39b1d 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
39b1e 65 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f  eColumn(sqlite3_
39b1f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72  vtab_cursor *cur
39b20 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  , sqlite3_contex
39b21 74 20 2a 63 74 78 2c 20 69 6e 74 20 69 29 7b 0a  t *ctx, int i){.
39b22 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20    Rtree *pRtree 
39b23 3d 20 28 52 74 72 65 65 20 2a 29 63 75 72 2d 3e  = (Rtree *)cur->
39b24 70 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75  pVtab;.  RtreeCu
39b25 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74  rsor *pCsr = (Rt
39b26 72 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72 3b  reeCursor *)cur;
39b27 0a 0a 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a  ..  if( i==0 ){.
39b28 20 20 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d      i64 iRowid =
39b29 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52   nodeGetRowid(pR
39b2a 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64  tree, pCsr->pNod
39b2b 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b  e, pCsr->iCell);
39b2c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
39b2d 75 6c 74 5f 69 6e 74 36 34 28 63 74 78 2c 20 69  ult_int64(ctx, i
39b2e 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b  Rowid);.  }else{
39b2f 0a 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20  .    RtreeCoord 
39b30 63 3b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 6f  c;.    nodeGetCo
39b31 6f 72 64 28 70 52 74 72 65 65 2c 20 70 43 73 72  ord(pRtree, pCsr
39b32 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69  ->pNode, pCsr->i
39b33 43 65 6c 6c 2c 20 69 2d 31 2c 20 26 63 29 3b 0a  Cell, i-1, &c);.
39b34 20 20 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e      if( pRtree->
39b35 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45  eCoordType==RTRE
39b36 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29  E_COORD_REAL32 )
39b37 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
39b38 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 74  result_double(ct
39b39 78 2c 20 63 2e 66 29 3b 0a 20 20 20 20 7d 65 6c  x, c.f);.    }el
39b3a 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
39b3b 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64  ( pRtree->eCoord
39b3c 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52  Type==RTREE_COOR
39b3d 44 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20  D_INT32 );.     
39b3e 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
39b3f 69 6e 74 28 63 74 78 2c 20 63 2e 69 29 3b 0a 20  int(ctx, c.i);. 
39b40 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
39b41 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
39b42 0a 2f 2a 20 0a 2a 2a 20 55 73 65 20 6e 6f 64 65  ./* .** Use node
39b43 41 63 71 75 69 72 65 28 29 20 74 6f 20 6f 62 74  Acquire() to obt
39b44 61 69 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64  ain the leaf nod
39b45 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
39b46 20 72 65 63 6f 72 64 20 77 69 74 68 20 0a 2a 2a   record with .**
39b47 20 72 6f 77 69 64 20 69 52 6f 77 69 64 2e 20 49   rowid iRowid. I
39b48 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65  f successful, se
39b49 74 20 2a 70 70 4c 65 61 66 20 74 6f 20 70 6f 69  t *ppLeaf to poi
39b4a 6e 74 20 74 6f 20 74 68 65 20 6e 6f 64 65 20 61  nt to the node a
39b4b 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c  nd.** return SQL
39b4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 72 65  ITE_OK. If there
39b4d 20 69 73 20 6e 6f 20 73 75 63 68 20 72 65 63 6f   is no such reco
39b4e 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c  rd in the table,
39b4f 20 73 65 74 0a 2a 2a 20 2a 70 70 4c 65 61 66 20   set.** *ppLeaf 
39b50 74 6f 20 30 20 61 6e 64 20 72 65 74 75 72 6e 20  to 0 and return 
39b51 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e  SQLITE_OK. If an
39b52 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 73   error occurs, s
39b53 65 74 20 2a 70 70 4c 65 61 66 0a 2a 2a 20 74 6f  et *ppLeaf.** to
39b54 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e   zero and return
39b55 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
39b56 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
39b57 20 69 6e 74 20 66 69 6e 64 4c 65 61 66 4e 6f 64   int findLeafNod
39b58 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
39b59 20 69 36 34 20 69 52 6f 77 69 64 2c 20 52 74 72   i64 iRowid, Rtr
39b5a 65 65 4e 6f 64 65 20 2a 2a 70 70 4c 65 61 66 29  eeNode **ppLeaf)
39b5b 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 2a 70  {.  int rc;.  *p
39b5c 70 4c 65 61 66 20 3d 20 30 3b 0a 20 20 73 71 6c  pLeaf = 0;.  sql
39b5d 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
39b5e 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
39b5f 69 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a  id, 1, iRowid);.
39b60 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74    if( sqlite3_st
39b61 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ep(pRtree->pRead
39b62 52 6f 77 69 64 29 3d 3d 53 51 4c 49 54 45 5f 52  Rowid)==SQLITE_R
39b63 4f 57 20 29 7b 0a 20 20 20 20 69 36 34 20 69 4e  OW ){.    i64 iN
39b64 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ode = sqlite3_co
39b65 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72 65  lumn_int64(pRtre
39b66 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 30  e->pReadRowid, 0
39b67 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65  );.    rc = node
39b68 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20  Acquire(pRtree, 
39b69 69 4e 6f 64 65 2c 20 30 2c 20 70 70 4c 65 61 66  iNode, 0, ppLeaf
39b6a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
39b6b 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
39b6c 61 64 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73  adRowid);.  }els
39b6d 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e{.    rc = sqli
39b6e 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
39b6f 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a 20  ->pReadRowid);. 
39b70 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
39b71 7d 0a 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  }.../* .** Rtree
39b72 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
39b73 6f 64 75 6c 65 20 78 46 69 6c 74 65 72 20 6d 65  odule xFilter me
39b74 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
39b75 69 6e 74 20 72 74 72 65 65 46 69 6c 74 65 72 28  int rtreeFilter(
39b76 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
39b77 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
39b78 73 6f 72 2c 20 0a 20 20 69 6e 74 20 69 64 78 4e  sor, .  int idxN
39b79 75 6d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  um, const char *
39b7a 69 64 78 53 74 72 2c 0a 20 20 69 6e 74 20 61 72  idxStr,.  int ar
39b7b 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
39b7c 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 52 74  e **argv.){.  Rt
39b7d 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
39b7e 74 72 65 65 20 2a 29 70 56 74 61 62 43 75 72 73  tree *)pVtabCurs
39b7f 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 52 74 72  or->pVtab;.  Rtr
39b80 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
39b81 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
39b82 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 0a 20 20  pVtabCursor;..  
39b83 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f 74  RtreeNode *pRoot
39b84 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a   = 0;.  int ii;.
39b85 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
39b86 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65 65 52 65  E_OK;..  rtreeRe
39b87 66 65 72 65 6e 63 65 28 70 52 74 72 65 65 29 3b  ference(pRtree);
39b88 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
39b89 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  (pCsr->aConstrai
39b8a 6e 74 29 3b 0a 20 20 70 43 73 72 2d 3e 61 43 6f  nt);.  pCsr->aCo
39b8b 6e 73 74 72 61 69 6e 74 20 3d 20 30 3b 0a 20 20  nstraint = 0;.  
39b8c 70 43 73 72 2d 3e 69 53 74 72 61 74 65 67 79 20  pCsr->iStrategy 
39b8d 3d 20 69 64 78 4e 75 6d 3b 0a 0a 20 20 69 66 28  = idxNum;..  if(
39b8e 20 69 64 78 4e 75 6d 3d 3d 31 20 29 7b 0a 20 20   idxNum==1 ){.  
39b8f 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
39b90 65 20 2d 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f  e - lookup by ro
39b91 77 69 64 2e 20 2a 2f 0a 20 20 20 20 52 74 72 65  wid. */.    Rtre
39b92 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b 20 20 20  eNode *pLeaf;   
39b93 20 20 20 20 20 2f 2a 20 4c 65 61 66 20 6f 6e 20       /* Leaf on 
39b94 77 68 69 63 68 20 74 68 65 20 72 65 71 75 69 72  which the requir
39b95 65 64 20 63 65 6c 6c 20 72 65 73 69 64 65 73 20  ed cell resides 
39b96 2a 2f 0a 20 20 20 20 69 36 34 20 69 52 6f 77 69  */.    i64 iRowi
39b97 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  d = sqlite3_valu
39b98 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29  e_int64(argv[0])
39b99 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
39b9a 65 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  eafNode(pRtree, 
39b9b 69 52 6f 77 69 64 2c 20 26 70 4c 65 61 66 29 3b  iRowid, &pLeaf);
39b9c 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65  .    pCsr->pNode
39b9d 20 3d 20 70 4c 65 61 66 3b 20 0a 20 20 20 20 69   = pLeaf; .    i
39b9e 66 28 20 70 4c 65 61 66 20 26 26 20 72 63 3d 3d  f( pLeaf && rc==
39b9f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
39ba0 20 20 20 70 43 73 72 2d 3e 69 43 65 6c 6c 20 3d     pCsr->iCell =
39ba1 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28   nodeRowidIndex(
39ba2 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20 69  pRtree, pLeaf, i
39ba3 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  Rowid);.    }.  
39ba4 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 6f  }else{.    /* No
39ba5 72 6d 61 6c 20 63 61 73 65 20 2d 20 72 2d 74 72  rmal case - r-tr
39ba6 65 65 20 73 63 61 6e 2e 20 53 65 74 20 75 70 20  ee scan. Set up 
39ba7 74 68 65 20 52 74 72 65 65 43 75 72 73 6f 72 2e  the RtreeCursor.
39ba8 61 43 6f 6e 73 74 72 61 69 6e 74 20 61 72 72 61  aConstraint arra
39ba9 79 20 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74  y .    ** with t
39baa 68 65 20 63 6f 6e 66 69 67 75 72 65 64 20 63 6f  he configured co
39bab 6e 73 74 72 61 69 6e 74 73 2e 20 0a 20 20 20 20  nstraints. .    
39bac 2a 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63 3e  */.    if( argc>
39bad 30 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72 2d  0 ){.      pCsr-
39bae 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 73  >aConstraint = s
39baf 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
39bb0 7a 65 6f 66 28 52 74 72 65 65 43 6f 6e 73 74 72  zeof(RtreeConstr
39bb1 61 69 6e 74 29 2a 61 72 67 63 29 3b 0a 20 20 20  aint)*argc);.   
39bb2 20 20 20 70 43 73 72 2d 3e 6e 43 6f 6e 73 74 72     pCsr->nConstr
39bb3 61 69 6e 74 20 3d 20 61 72 67 63 3b 0a 20 20 20  aint = argc;.   
39bb4 20 20 20 69 66 28 20 21 70 43 73 72 2d 3e 61 43     if( !pCsr->aC
39bb5 6f 6e 73 74 72 61 69 6e 74 20 29 7b 0a 20 20 20  onstraint ){.   
39bb6 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
39bb7 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65  _NOMEM;.      }e
39bb8 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
39bb9 65 72 74 28 20 28 69 64 78 53 74 72 3d 3d 30 20  ert( (idxStr==0 
39bba 26 26 20 61 72 67 63 3d 3d 30 29 20 7c 7c 20 73  && argc==0) || s
39bbb 74 72 6c 65 6e 28 69 64 78 53 74 72 29 3d 3d 61  trlen(idxStr)==a
39bbc 72 67 63 2a 32 20 29 3b 0a 20 20 20 20 20 20 20  rgc*2 );.       
39bbd 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 61 72   for(ii=0; ii<ar
39bbe 67 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  gc; ii++){.     
39bbf 20 20 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72       RtreeConstr
39bc0 61 69 6e 74 20 2a 70 20 3d 20 26 70 43 73 72 2d  aint *p = &pCsr-
39bc1 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d  >aConstraint[ii]
39bc2 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6f  ;.          p->o
39bc3 70 20 3d 20 69 64 78 53 74 72 5b 69 69 2a 32 5d  p = idxStr[ii*2]
39bc4 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69  ;.          p->i
39bc5 43 6f 6f 72 64 20 3d 20 69 64 78 53 74 72 5b 69  Coord = idxStr[i
39bc6 69 2a 32 2b 31 5d 2d 27 61 27 3b 0a 20 20 20 20  i*2+1]-'a';.    
39bc7 20 20 20 20 20 20 70 2d 3e 72 56 61 6c 75 65 20        p->rValue 
39bc8 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
39bc9 64 6f 75 62 6c 65 28 61 72 67 76 5b 69 69 5d 29  double(argv[ii])
39bca 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
39bcb 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
39bcc 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
39bcd 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72  OK ){.      pCsr
39bce 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20  ->pNode = 0;.   
39bcf 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75     rc = nodeAcqu
39bd0 69 72 65 28 70 52 74 72 65 65 2c 20 31 2c 20 30  ire(pRtree, 1, 0
39bd1 2c 20 26 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d  , &pRoot);.    }
39bd2 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
39bd3 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
39bd4 69 6e 74 20 69 73 45 6f 66 20 3d 20 31 3b 0a 20  int isEof = 1;. 
39bd5 20 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d       int nCell =
39bd6 20 4e 43 45 4c 4c 28 70 52 6f 6f 74 29 3b 0a 20   NCELL(pRoot);. 
39bd7 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65       pCsr->pNode
39bd8 20 3d 20 70 52 6f 6f 74 3b 0a 20 20 20 20 20 20   = pRoot;.      
39bd9 66 6f 72 28 70 43 73 72 2d 3e 69 43 65 6c 6c 3d  for(pCsr->iCell=
39bda 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
39bdb 20 26 26 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c   && pCsr->iCell<
39bdc 6e 43 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43 65  nCell; pCsr->iCe
39bdd 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61  ll++){.        a
39bde 73 73 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f  ssert( pCsr->pNo
39bdf 64 65 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20  de==pRoot );.   
39be0 20 20 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e       rc = descen
39be1 64 54 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  dToCell(pRtree, 
39be2 70 43 73 72 2c 20 70 52 74 72 65 65 2d 3e 69 44  pCsr, pRtree->iD
39be3 65 70 74 68 2c 20 26 69 73 45 6f 66 29 3b 0a 20  epth, &isEof);. 
39be4 20 20 20 20 20 20 20 69 66 28 20 21 69 73 45 6f         if( !isEo
39be5 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  f ){.          b
39be6 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
39be7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
39be8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
39be9 26 26 20 69 73 45 6f 66 20 29 7b 0a 20 20 20 20  && isEof ){.    
39bea 20 20 20 20 61 73 73 65 72 74 28 20 70 43 73 72      assert( pCsr
39beb 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f 6f 74 20 29  ->pNode==pRoot )
39bec 3b 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 52 65  ;.        nodeRe
39bed 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 52  lease(pRtree, pR
39bee 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 70 43  oot);.        pC
39bef 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20  sr->pNode = 0;. 
39bf0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
39bf1 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
39bf2 4f 4b 20 7c 7c 20 21 70 43 73 72 2d 3e 70 4e 6f  OK || !pCsr->pNo
39bf3 64 65 20 7c 7c 20 70 43 73 72 2d 3e 69 43 65 6c  de || pCsr->iCel
39bf4 6c 3c 4e 43 45 4c 4c 28 70 43 73 72 2d 3e 70 4e  l<NCELL(pCsr->pN
39bf5 6f 64 65 29 20 29 3b 0a 20 20 20 20 7d 0a 20 20  ode) );.    }.  
39bf6 7d 0a 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73  }..  rtreeReleas
39bf7 65 28 70 52 74 72 65 65 29 3b 0a 20 20 72 65 74  e(pRtree);.  ret
39bf8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
39bf9 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
39bfa 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 42 65 73  able module xBes
39bfb 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 2e 20 54  tIndex method. T
39bfc 68 65 72 65 20 61 72 65 20 74 68 72 65 65 0a 2a  here are three.*
39bfd 2a 20 74 61 62 6c 65 20 73 63 61 6e 20 73 74 72  * table scan str
39bfe 61 74 65 67 69 65 73 20 74 6f 20 63 68 6f 6f 73  ategies to choos
39bff 65 20 66 72 6f 6d 20 28 69 6e 20 6f 72 64 65 72  e from (in order
39c00 20 66 72 6f 6d 20 6d 6f 73 74 20 74 6f 20 0a 2a   from most to .*
39c01 2a 20 6c 65 61 73 74 20 64 65 73 69 72 61 62 6c  * least desirabl
39c02 65 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 64 78 4e  e):.**.**   idxN
39c03 75 6d 20 20 20 20 20 69 64 78 53 74 72 20 20 20  um     idxStr   
39c04 20 20 20 20 20 53 74 72 61 74 65 67 79 0a 2a 2a       Strategy.**
39c05 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
39c06 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39c07 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39c08 2d 2d 2d 0a 2a 2a 20 20 20 20 20 31 20 20 20 20  ---.**     1    
39c09 20 20 20 20 55 6e 75 73 65 64 20 20 20 20 20 20      Unused      
39c0a 20 20 44 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20    Direct lookup 
39c0b 62 79 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20  by rowid..**    
39c0c 20 32 20 20 20 20 20 20 20 20 53 65 65 20 62 65   2        See be
39c0d 6c 6f 77 20 20 20 20 20 52 2d 74 72 65 65 20 71  low     R-tree q
39c0e 75 65 72 79 2e 0a 2a 2a 20 20 20 20 20 33 20 20  uery..**     3  
39c0f 20 20 20 20 20 20 55 6e 75 73 65 64 20 20 20 20        Unused    
39c10 20 20 20 20 46 75 6c 6c 20 74 61 62 6c 65 20 73      Full table s
39c11 63 61 6e 2e 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  can..**   ------
39c12 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39c13 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39c14 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a  ----------.**.**
39c15 20 49 66 20 73 74 72 61 74 65 67 79 20 31 20 6f   If strategy 1 o
39c16 72 20 33 20 69 73 20 75 73 65 64 2c 20 74 68 65  r 3 is used, the
39c17 6e 20 69 64 78 53 74 72 20 69 73 20 6e 6f 74 20  n idxStr is not 
39c18 6d 65 61 6e 69 6e 67 66 75 6c 2e 20 49 66 20 73  meaningful. If s
39c19 74 72 61 74 65 67 79 0a 2a 2a 20 32 20 69 73 20  trategy.** 2 is 
39c1a 75 73 65 64 2c 20 69 64 78 53 74 72 20 69 73 20  used, idxStr is 
39c1b 66 6f 72 6d 61 74 74 65 64 20 74 6f 20 63 6f 6e  formatted to con
39c1c 74 61 69 6e 20 32 20 62 79 74 65 73 20 66 6f 72  tain 2 bytes for
39c1d 20 65 61 63 68 20 0a 2a 2a 20 63 6f 6e 73 74 72   each .** constr
39c1e 61 69 6e 74 20 75 73 65 64 2e 20 54 68 65 20 66  aint used. The f
39c1f 69 72 73 74 20 74 77 6f 20 62 79 74 65 73 20 6f  irst two bytes o
39c20 66 20 69 64 78 53 74 72 20 63 6f 72 72 65 73 70  f idxStr corresp
39c21 6f 6e 64 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63  ond to .** the c
39c22 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 73 71 6c  onstraint in sql
39c23 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2e  ite3_index_info.
39c24 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
39c25 5b 5d 20 77 69 74 68 0a 2a 2a 20 28 61 72 67 76  [] with.** (argv
39c26 49 6e 64 65 78 3d 3d 31 29 20 65 74 63 2e 0a 2a  Index==1) etc..*
39c27 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6f  *.** The first o
39c28 66 20 65 61 63 68 20 70 61 69 72 20 6f 66 20 62  f each pair of b
39c29 79 74 65 73 20 69 6e 20 69 64 78 53 74 72 20 69  ytes in idxStr i
39c2a 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 63 6f  dentifies the co
39c2b 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 6f 70 65 72  nstraint.** oper
39c2c 61 74 6f 72 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ator as follows:
39c2d 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 65 72 61 74 6f  .**.**   Operato
39c2e 72 20 20 20 20 42 79 74 65 20 56 61 6c 75 65 0a  r    Byte Value.
39c2f 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
39c30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
39c31 20 20 20 20 3d 20 20 20 20 20 20 20 20 30 78 34      =        0x4
39c32 31 20 28 27 41 27 29 0a 2a 2a 20 20 20 20 20 3c  1 ('A').**     <
39c33 3d 20 20 20 20 20 20 20 20 30 78 34 32 20 28 27  =        0x42 ('
39c34 42 27 29 0a 2a 2a 20 20 20 20 20 20 3c 20 20 20  B').**      <   
39c35 20 20 20 20 20 30 78 34 33 20 28 27 43 27 29 0a       0x43 ('C').
39c36 2a 2a 20 20 20 20 20 3e 3d 20 20 20 20 20 20 20  **     >=       
39c37 20 30 78 34 34 20 28 27 44 27 29 0a 2a 2a 20 20   0x44 ('D').**  
39c38 20 20 20 20 3e 20 20 20 20 20 20 20 20 30 78 34      >        0x4
39c39 35 20 28 27 45 27 29 0a 2a 2a 20 20 20 2d 2d 2d  5 ('E').**   ---
39c3a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39c3b 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  ---.**.** The se
39c3c 63 6f 6e 64 20 6f 66 20 65 61 63 68 20 70 61 69  cond of each pai
39c3d 72 20 6f 66 20 62 79 74 65 73 20 69 64 65 6e 74  r of bytes ident
39c3e 69 66 69 65 73 20 74 68 65 20 63 6f 6f 72 64 69  ifies the coordi
39c3f 6e 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74  nate column.** t
39c40 6f 20 77 68 69 63 68 20 74 68 65 20 63 6f 6e 73  o which the cons
39c41 74 72 61 69 6e 74 20 61 70 70 6c 69 65 73 2e 20  traint applies. 
39c42 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6f  The leftmost coo
39c43 72 64 69 6e 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a  rdinate column.*
39c44 2a 20 69 73 20 27 61 27 2c 20 74 68 65 20 73 65  * is 'a', the se
39c45 63 6f 6e 64 20 66 72 6f 6d 20 74 68 65 20 6c 65  cond from the le
39c46 66 74 20 27 62 27 20 65 74 63 2e 0a 2a 2f 0a 73  ft 'b' etc..*/.s
39c47 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 42  tatic int rtreeB
39c48 65 73 74 49 6e 64 65 78 28 73 71 6c 69 74 65 33  estIndex(sqlite3
39c49 5f 76 74 61 62 20 2a 74 61 62 2c 20 73 71 6c 69  _vtab *tab, sqli
39c4a 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
39c4b 70 49 64 78 49 6e 66 6f 29 7b 0a 20 20 69 6e 74  pIdxInfo){.  int
39c4c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
39c4d 0a 20 20 69 6e 74 20 69 69 2c 20 63 43 6f 6c 3b  .  int ii, cCol;
39c4e 0a 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30  ..  int iIdx = 0
39c4f 3b 0a 20 20 63 68 61 72 20 7a 49 64 78 53 74 72  ;.  char zIdxStr
39c50 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e  [RTREE_MAX_DIMEN
39c51 53 49 4f 4e 53 2a 38 2b 31 5d 3b 0a 20 20 6d 65  SIONS*8+1];.  me
39c52 6d 73 65 74 28 7a 49 64 78 53 74 72 2c 20 30 2c  mset(zIdxStr, 0,
39c53 20 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72 29   sizeof(zIdxStr)
39c54 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 49  );..  assert( pI
39c55 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 3d 3d  dxInfo->idxStr==
39c56 30 20 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  0 );.  for(ii=0;
39c57 20 69 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43   ii<pIdxInfo->nC
39c58 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29  onstraint; ii++)
39c59 7b 0a 20 20 20 20 73 74 72 75 63 74 20 73 71 6c  {.    struct sql
39c5a 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
39c5b 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 49 64 78  raint *p = &pIdx
39c5c 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
39c5d 74 5b 69 69 5d 3b 0a 0a 20 20 20 20 69 66 28 20  t[ii];..    if( 
39c5e 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e  p->usable && p->
39c5f 69 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 70 2d  iColumn==0 && p-
39c60 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  >op==SQLITE_INDE
39c61 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20  X_CONSTRAINT_EQ 
39c62 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 68  ){.      /* We h
39c63 61 76 65 20 61 6e 20 65 71 75 61 6c 69 74 79 20  ave an equality 
39c64 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 68  constraint on th
39c65 65 20 72 6f 77 69 64 2e 20 55 73 65 20 73 74 72  e rowid. Use str
39c66 61 74 65 67 79 20 31 2e 20 2a 2f 0a 20 20 20 20  ategy 1. */.    
39c67 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
39c68 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 69 69 3b  for(jj=0; jj<ii;
39c69 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   jj++){.        
39c6a 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
39c6b 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 61  raintUsage[jj].a
39c6c 72 67 76 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20  rgvIndex = 0;.  
39c6d 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
39c6e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
39c6f 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 30 3b 0a 20  [jj].omit = 0;. 
39c70 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 64       }.      pId
39c71 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20  xInfo->idxNum = 
39c72 31 3b 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66  1;.      pIdxInf
39c73 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
39c74 61 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e 64 65  age[ii].argvInde
39c75 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 49 64  x = 1;.      pId
39c76 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
39c77 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d 69 74  ntUsage[jj].omit
39c78 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   = 1;..      /* 
39c79 54 68 69 73 20 73 74 72 61 74 65 67 79 20 69 6e  This strategy in
39c7a 76 6f 6c 76 65 73 20 61 20 74 77 6f 20 72 6f 77  volves a two row
39c7b 69 64 20 6c 6f 6f 6b 75 70 73 20 6f 6e 20 61 6e  id lookups on an
39c7c 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72   B-Tree structur
39c7d 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  es.      ** and 
39c7e 74 68 65 6e 20 61 20 6c 69 6e 65 61 72 20 73 65  then a linear se
39c7f 61 72 63 68 20 6f 66 20 61 6e 20 52 2d 54 72 65  arch of an R-Tre
39c80 65 20 6e 6f 64 65 2e 20 54 68 69 73 20 73 68 6f  e node. This sho
39c81 75 6c 64 20 62 65 20 0a 20 20 20 20 20 20 2a 2a  uld be .      **
39c82 20 63 6f 6e 73 69 64 65 72 65 64 20 61 6c 6d 6f   considered almo
39c83 73 74 20 61 73 20 71 75 69 63 6b 20 61 73 20 61  st as quick as a
39c84 20 64 69 72 65 63 74 20 72 6f 77 69 64 20 6c 6f   direct rowid lo
39c85 6f 6b 75 70 20 28 66 6f 72 20 77 68 69 63 68 20  okup (for which 
39c86 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  .      ** sqlite
39c87 20 75 73 65 73 20 61 6e 20 69 6e 74 65 72 6e 61   uses an interna
39c88 6c 20 63 6f 73 74 20 6f 66 20 30 2e 30 29 2e 0a  l cost of 0.0)..
39c89 20 20 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 20        */ .      
39c8a 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61  pIdxInfo->estima
39c8b 74 65 64 43 6f 73 74 20 3d 20 31 30 2e 30 3b 0a  tedCost = 10.0;.
39c8c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
39c8d 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20  ITE_OK;.    }.. 
39c8e 20 20 20 69 66 28 20 70 2d 3e 75 73 61 62 6c 65     if( p->usable
39c8f 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3e 30   && p->iColumn>0
39c90 20 29 7b 0a 20 20 20 20 20 20 75 38 20 6f 70 20   ){.      u8 op 
39c91 3d 20 30 3b 0a 20 20 20 20 20 20 73 77 69 74 63  = 0;.      switc
39c92 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  h( p->op ){.    
39c93 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
39c94 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
39c95 5f 45 51 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f  _EQ: op = RTREE_
39c96 45 51 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  EQ; break;.     
39c97 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
39c98 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
39c99 47 54 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 47  GT: op = RTREE_G
39c9a 54 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  T; break;.      
39c9b 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
39c9c 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c  DEX_CONSTRAINT_L
39c9d 45 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 45  E: op = RTREE_LE
39c9e 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
39c9f 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44   case SQLITE_IND
39ca0 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54  EX_CONSTRAINT_LT
39ca1 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 54 3b  : op = RTREE_LT;
39ca2 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
39ca3 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  case SQLITE_INDE
39ca4 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 3a  X_CONSTRAINT_GE:
39ca5 20 6f 70 20 3d 20 52 54 52 45 45 5f 47 45 3b 20   op = RTREE_GE; 
39ca6 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
39ca7 20 20 20 20 20 69 66 28 20 6f 70 20 29 7b 0a 20       if( op ){. 
39ca8 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73         /* Make s
39ca9 75 72 65 20 74 68 69 73 20 70 61 72 74 69 63 75  ure this particu
39caa 6c 61 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 68  lar constraint h
39cab 61 73 20 6e 6f 74 20 62 65 65 6e 20 75 73 65 64  as not been used
39cac 20 62 65 66 6f 72 65 2e 0a 20 20 20 20 20 20 20   before..       
39cad 20 2a 2a 20 49 66 20 69 74 20 68 61 73 20 62 65   ** If it has be
39cae 65 6e 20 75 73 65 64 20 62 65 66 6f 72 65 2c 20  en used before, 
39caf 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 20 20 20  ignore it..     
39cb0 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
39cb1 20 41 20 3c 3d 20 6f 72 20 3c 20 63 61 6e 20 62   A <= or < can b
39cb2 65 20 75 73 65 64 20 69 66 20 74 68 65 72 65 20  e used if there 
39cb3 69 73 20 61 20 70 72 69 6f 72 20 3e 3d 20 6f 72  is a prior >= or
39cb4 20 3e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41   >..        ** A
39cb5 20 3e 3d 20 6f 72 20 3e 20 63 61 6e 20 62 65 20   >= or > can be 
39cb6 75 73 65 64 20 69 66 20 74 68 65 72 65 20 69 73  used if there is
39cb7 20 61 20 70 72 69 6f 72 20 3c 20 6f 72 20 3c 3d   a prior < or <=
39cb8 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3c  ..        ** A <
39cb9 3d 20 6f 72 20 3c 20 69 73 20 64 69 73 71 75 61  = or < is disqua
39cba 6c 69 66 69 65 64 20 69 66 20 74 68 65 72 65 20  lified if there 
39cbb 69 73 20 61 20 70 72 69 6f 72 20 3c 3d 2c 20 3c  is a prior <=, <
39cbc 2c 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20  , or ==..       
39cbd 20 2a 2a 20 41 20 3e 3d 20 6f 72 20 3e 20 69 73   ** A >= or > is
39cbe 20 64 69 73 71 75 61 6c 69 66 69 65 64 20 69 66   disqualified if
39cbf 20 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f   there is a prio
39cc0 72 20 3e 3d 2c 20 3e 2c 20 6f 72 20 3d 3d 2e 0a  r >=, >, or ==..
39cc1 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3d 3d 20          ** A == 
39cc2 69 73 20 64 69 73 71 75 61 6c 69 66 65 64 20 69  is disqualifed i
39cc3 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 70  f there is any p
39cc4 72 69 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 2e  rior constraint.
39cc5 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
39cc6 20 20 20 20 69 6e 74 20 6a 2c 20 6f 70 6d 73 6b      int j, opmsk
39cc7 3b 0a 20 20 20 20 20 20 20 20 73 74 61 74 69 63  ;.        static
39cc8 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
39cc9 63 68 61 72 20 63 6f 6d 70 61 74 69 62 6c 65 5b  char compatible[
39cca 5d 20 3d 20 7b 20 30 2c 20 30 2c 20 31 2c 20 31  ] = { 0, 0, 1, 1
39ccb 2c 20 32 2c 20 32 20 7d 3b 0a 20 20 20 20 20 20  , 2, 2 };.      
39ccc 20 20 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74    assert( compat
39ccd 69 62 6c 65 5b 52 54 52 45 45 5f 45 51 20 26 20  ible[RTREE_EQ & 
39cce 37 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  7]==0 );.       
39ccf 20 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69   assert( compati
39cd0 62 6c 65 5b 52 54 52 45 45 5f 4c 54 20 26 20 37  ble[RTREE_LT & 7
39cd1 5d 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20  ]==1 );.        
39cd2 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62  assert( compatib
39cd3 6c 65 5b 52 54 52 45 45 5f 4c 45 20 26 20 37 5d  le[RTREE_LE & 7]
39cd4 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ==1 );.        a
39cd5 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c  ssert( compatibl
39cd6 65 5b 52 54 52 45 45 5f 47 54 20 26 20 37 5d 3d  e[RTREE_GT & 7]=
39cd7 3d 32 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  =2 );.        as
39cd8 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65  sert( compatible
39cd9 5b 52 54 52 45 45 5f 47 45 20 26 20 37 5d 3d 3d  [RTREE_GE & 7]==
39cda 32 20 29 3b 0a 20 20 20 20 20 20 20 20 63 43 6f  2 );.        cCo
39cdb 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 2d  l = p->iColumn -
39cdc 20 31 20 2b 20 27 61 27 3b 0a 20 20 20 20 20 20   1 + 'a';.      
39cdd 20 20 6f 70 6d 73 6b 20 3d 20 63 6f 6d 70 61 74    opmsk = compat
39cde 69 62 6c 65 5b 6f 70 20 26 20 37 5d 3b 0a 20 20  ible[op & 7];.  
39cdf 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
39ce0 3c 69 49 64 78 3b 20 6a 2b 3d 32 29 7b 0a 20 20  <iIdx; j+=2){.  
39ce1 20 20 20 20 20 20 20 20 69 66 28 20 7a 49 64 78          if( zIdx
39ce2 53 74 72 5b 6a 2b 31 5d 3d 3d 63 43 6f 6c 20 26  Str[j+1]==cCol &
39ce3 26 20 28 63 6f 6d 70 61 74 69 62 6c 65 5b 7a 49  & (compatible[zI
39ce4 64 78 53 74 72 5b 6a 5d 20 26 20 37 5d 20 26 20  dxStr[j] & 7] & 
39ce5 6f 70 6d 73 6b 29 21 3d 30 20 29 7b 0a 20 20 20  opmsk)!=0 ){.   
39ce6 20 20 20 20 20 20 20 20 20 6f 70 20 3d 20 30 3b           op = 0;
39ce7 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
39ce8 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
39ce9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
39cea 7d 0a 20 20 20 20 20 20 69 66 28 20 6f 70 20 29  }.      if( op )
39ceb 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
39cec 28 20 69 49 64 78 3c 73 69 7a 65 6f 66 28 7a 49  ( iIdx<sizeof(zI
39ced 64 78 53 74 72 29 2d 31 20 29 3b 0a 20 20 20 20  dxStr)-1 );.    
39cee 20 20 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78      zIdxStr[iIdx
39cef 2b 2b 5d 20 3d 20 6f 70 3b 0a 20 20 20 20 20 20  ++] = op;.      
39cf0 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78 2b 2b    zIdxStr[iIdx++
39cf1 5d 20 3d 20 63 43 6f 6c 3b 0a 20 20 20 20 20 20  ] = cCol;.      
39cf2 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e    pIdxInfo->aCon
39cf3 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d  straintUsage[ii]
39cf4 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 28 69 49  .argvIndex = (iI
39cf5 64 78 2f 32 29 3b 0a 20 20 20 20 20 20 20 20 70  dx/2);.        p
39cf6 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
39cf7 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e 6f 6d  aintUsage[ii].om
39cf8 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  it = 1;.      }.
39cf9 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 49 64      }.  }..  pId
39cfa 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20  xInfo->idxNum = 
39cfb 32 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e  2;.  pIdxInfo->n
39cfc 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20  eedToFreeIdxStr 
39cfd 3d 20 31 3b 0a 20 20 69 66 28 20 69 49 64 78 3e  = 1;.  if( iIdx>
39cfe 30 20 26 26 20 30 3d 3d 28 70 49 64 78 49 6e 66  0 && 0==(pIdxInf
39cff 6f 2d 3e 69 64 78 53 74 72 20 3d 20 73 71 6c 69  o->idxStr = sqli
39d00 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22  te3_mprintf("%s"
39d01 2c 20 7a 49 64 78 53 74 72 29 29 20 29 7b 0a 20  , zIdxStr)) ){. 
39d02 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
39d03 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73  _NOMEM;.  }.  as
39d04 73 65 72 74 28 20 69 49 64 78 3e 3d 30 20 29 3b  sert( iIdx>=0 );
39d05 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74  .  pIdxInfo->est
39d06 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 28 32 30  imatedCost = (20
39d07 30 30 30 30 30 2e 30 20 2f 20 28 64 6f 75 62 6c  00000.0 / (doubl
39d08 65 29 28 69 49 64 78 20 2b 20 31 29 29 3b 0a 20  e)(iIdx + 1));. 
39d09 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
39d0a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
39d0b 4e 2d 64 69 6d 65 6e 73 69 6f 6e 61 6c 20 76 6f  N-dimensional vo
39d0c 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 65 6c 6c  lumn of the cell
39d0d 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 2e 0a 2a   stored in *p..*
39d0e 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63  /.static float c
39d0f 65 6c 6c 41 72 65 61 28 52 74 72 65 65 20 2a 70  ellArea(Rtree *p
39d10 52 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c  Rtree, RtreeCell
39d11 20 2a 70 29 7b 0a 20 20 66 6c 6f 61 74 20 61 72   *p){.  float ar
39d12 65 61 20 3d 20 31 2e 30 3b 0a 20 20 69 6e 74 20  ea = 1.0;.  int 
39d13 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ii;.  for(ii=0; 
39d14 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
39d15 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
39d16 20 61 72 65 61 20 3d 20 61 72 65 61 20 2a 20 28   area = area * (
39d17 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
39d18 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f 4f 52 44  [ii+1]) - DCOORD
39d19 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 29  (p->aCoord[ii]))
39d1a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61  ;.  }.  return a
39d1b 72 65 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  rea;.}../*.** Re
39d1c 74 75 72 6e 20 74 68 65 20 6d 61 72 67 69 6e 20  turn the margin 
39d1d 6c 65 6e 67 74 68 20 6f 66 20 63 65 6c 6c 20 70  length of cell p
39d1e 2e 20 54 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e  . The margin len
39d1f 67 74 68 20 69 73 20 74 68 65 20 73 75 6d 0a 2a  gth is the sum.*
39d20 2a 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74 73  * of the objects
39d21 20 73 69 7a 65 20 69 6e 20 65 61 63 68 20 64 69   size in each di
39d22 6d 65 6e 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  mension..*/.stat
39d23 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4d 61 72  ic float cellMar
39d24 67 69 6e 28 52 74 72 65 65 20 2a 70 52 74 72 65  gin(Rtree *pRtre
39d25 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 29  e, RtreeCell *p)
39d26 7b 0a 20 20 66 6c 6f 61 74 20 6d 61 72 67 69 6e  {.  float margin
39d27 20 3d 20 30 2e 30 3b 0a 20 20 69 6e 74 20 69 69   = 0.0;.  int ii
39d28 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
39d29 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
39d2a 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 6d  ); ii+=2){.    m
39d2b 61 72 67 69 6e 20 2b 3d 20 28 44 43 4f 4f 52 44  argin += (DCOORD
39d2c 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  (p->aCoord[ii+1]
39d2d 29 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61 43  ) - DCOORD(p->aC
39d2e 6f 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a  oord[ii]));.  }.
39d2f 20 20 72 65 74 75 72 6e 20 6d 61 72 67 69 6e 3b    return margin;
39d30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 6f 72 65 20  .}../*.** Store 
39d31 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 63 65 6c  the union of cel
39d32 6c 73 20 70 31 20 61 6e 64 20 70 32 20 69 6e 20  ls p1 and p2 in 
39d33 70 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  p1..*/.static vo
39d34 69 64 20 63 65 6c 6c 55 6e 69 6f 6e 28 52 74 72  id cellUnion(Rtr
39d35 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
39d36 65 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65  eCell *p1, Rtree
39d37 43 65 6c 6c 20 2a 70 32 29 7b 0a 20 20 69 6e 74  Cell *p2){.  int
39d38 20 69 69 3b 0a 20 20 69 66 28 20 70 52 74 72 65   ii;.  if( pRtre
39d39 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52  e->eCoordType==R
39d3a 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33  TREE_COORD_REAL3
39d3b 32 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d  2 ){.    for(ii=
39d3c 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e  0; ii<(pRtree->n
39d3d 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a  Dim*2); ii+=2){.
39d3e 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64        p1->aCoord
39d3f 5b 69 69 5d 2e 66 20 3d 20 4d 49 4e 28 70 31 2d  [ii].f = MIN(p1-
39d40 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 2c 20 70  >aCoord[ii].f, p
39d41 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 29  2->aCoord[ii].f)
39d42 3b 0a 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f  ;.      p1->aCoo
39d43 72 64 5b 69 69 2b 31 5d 2e 66 20 3d 20 4d 41 58  rd[ii+1].f = MAX
39d44 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31  (p1->aCoord[ii+1
39d45 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b  ].f, p2->aCoord[
39d46 69 69 2b 31 5d 2e 66 29 3b 0a 20 20 20 20 7d 0a  ii+1].f);.    }.
39d47 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72    }else{.    for
39d48 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
39d49 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
39d4a 32 29 7b 0a 20 20 20 20 20 20 70 31 2d 3e 61 43  2){.      p1->aC
39d4b 6f 6f 72 64 5b 69 69 5d 2e 69 20 3d 20 4d 49 4e  oord[ii].i = MIN
39d4c 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e  (p1->aCoord[ii].
39d4d 69 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69  i, p2->aCoord[ii
39d4e 5d 2e 69 29 3b 0a 20 20 20 20 20 20 70 31 2d 3e  ].i);.      p1->
39d4f 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20 3d  aCoord[ii+1].i =
39d50 20 4d 41 58 28 70 31 2d 3e 61 43 6f 6f 72 64 5b   MAX(p1->aCoord[
39d51 69 69 2b 31 5d 2e 69 2c 20 70 32 2d 3e 61 43 6f  ii+1].i, p2->aCo
39d52 6f 72 64 5b 69 69 2b 31 5d 2e 69 29 3b 0a 20 20  ord[ii+1].i);.  
39d53 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
39d54 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
39d55 74 68 65 20 61 72 65 61 20 63 6f 76 65 72 65 64  the area covered
39d56 20 62 79 20 70 32 20 69 73 20 61 20 73 75 62 73   by p2 is a subs
39d57 65 74 20 6f 66 20 74 68 65 20 61 72 65 61 20 63  et of the area c
39d58 6f 76 65 72 65 64 0a 2a 2a 20 62 79 20 70 31 2e  overed.** by p1.
39d59 20 46 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65   False otherwise
39d5a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39d5b 63 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 52 74 72  cellContains(Rtr
39d5c 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
39d5d 65 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65  eCell *p1, Rtree
39d5e 43 65 6c 6c 20 2a 70 32 29 7b 0a 20 20 69 6e 74  Cell *p2){.  int
39d5f 20 69 69 3b 0a 20 20 69 6e 74 20 69 73 49 6e 74   ii;.  int isInt
39d60 20 3d 20 28 70 52 74 72 65 65 2d 3e 65 43 6f 6f   = (pRtree->eCoo
39d61 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
39d62 4f 52 44 5f 49 4e 54 33 32 29 3b 0a 20 20 66 6f  ORD_INT32);.  fo
39d63 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72  r(ii=0; ii<(pRtr
39d64 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b  ee->nDim*2); ii+
39d65 3d 32 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f  =2){.    RtreeCo
39d66 6f 72 64 20 2a 61 31 20 3d 20 26 70 31 2d 3e 61  ord *a1 = &p1->a
39d67 43 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20 52  Coord[ii];.    R
39d68 74 72 65 65 43 6f 6f 72 64 20 2a 61 32 20 3d 20  treeCoord *a2 = 
39d69 26 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 3b  &p2->aCoord[ii];
39d6a 0a 20 20 20 20 69 66 28 20 28 21 69 73 49 6e 74  .    if( (!isInt
39d6b 20 26 26 20 28 61 32 5b 30 5d 2e 66 3c 61 31 5b   && (a2[0].f<a1[
39d6c 30 5d 2e 66 20 7c 7c 20 61 32 5b 31 5d 2e 66 3e  0].f || a2[1].f>
39d6d 61 31 5b 31 5d 2e 66 29 29 20 0a 20 20 20 20 20  a1[1].f)) .     
39d6e 7c 7c 20 28 20 69 73 49 6e 74 20 26 26 20 28 61  || ( isInt && (a
39d6f 32 5b 30 5d 2e 69 3c 61 31 5b 30 5d 2e 69 20 7c  2[0].i<a1[0].i |
39d70 7c 20 61 32 5b 31 5d 2e 69 3e 61 31 5b 31 5d 2e  | a2[1].i>a1[1].
39d71 69 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  i)) .    ){.    
39d72 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
39d73 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  }.  }.  return 1
39d74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
39d75 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 63 65 6c  n the amount cel
39d76 6c 20 70 20 77 6f 75 6c 64 20 67 72 6f 77 20 62  l p would grow b
39d77 79 20 69 66 20 69 74 20 77 65 72 65 20 75 6e 69  y if it were uni
39d78 6f 6e 65 64 20 77 69 74 68 20 70 43 65 6c 6c 2e  oned with pCell.
39d79 0a 2a 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74  .*/.static float
39d7a 20 63 65 6c 6c 47 72 6f 77 74 68 28 52 74 72 65   cellGrowth(Rtre
39d7b 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
39d7c 43 65 6c 6c 20 2a 70 2c 20 52 74 72 65 65 43 65  Cell *p, RtreeCe
39d7d 6c 6c 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 66 6c  ll *pCell){.  fl
39d7e 6f 61 74 20 61 72 65 61 3b 0a 20 20 52 74 72 65  oat area;.  Rtre
39d7f 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 6d 65  eCell cell;.  me
39d80 6d 63 70 79 28 26 63 65 6c 6c 2c 20 70 2c 20 73  mcpy(&cell, p, s
39d81 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
39d82 29 3b 0a 20 20 61 72 65 61 20 3d 20 63 65 6c 6c  );.  area = cell
39d83 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65  Area(pRtree, &ce
39d84 6c 6c 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e  ll);.  cellUnion
39d85 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20  (pRtree, &cell, 
39d86 70 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  pCell);.  return
39d87 20 28 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65   (cellArea(pRtre
39d88 65 2c 20 26 63 65 6c 6c 29 2d 61 72 65 61 29 3b  e, &cell)-area);
39d89 0a 7d 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  .}..#if VARIANT_
39d8a 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
39d8b 53 55 42 54 52 45 45 20 7c 7c 20 56 41 52 49 41  SUBTREE || VARIA
39d8c 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c  NT_RSTARTREE_SPL
39d8d 49 54 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20  IT.static float 
39d8e 63 65 6c 6c 4f 76 65 72 6c 61 70 28 0a 20 20 52  cellOverlap(.  R
39d8f 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
39d90 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 0a   RtreeCell *p, .
39d91 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
39d92 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ll, .  int nCell
39d93 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c 75 64  , .  int iExclud
39d94 65 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  e.){.  int ii;. 
39d95 20 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 20 3d   float overlap =
39d96 20 30 2e 30 3b 0a 20 20 66 6f 72 28 69 69 3d 30   0.0;.  for(ii=0
39d97 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  ; ii<nCell; ii++
39d98 29 7b 0a 20 20 20 20 69 66 28 20 69 69 21 3d 69  ){.    if( ii!=i
39d99 45 78 63 6c 75 64 65 20 29 7b 0a 20 20 20 20 20  Exclude ){.     
39d9a 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 66   int jj;.      f
39d9b 6c 6f 61 74 20 6f 20 3d 20 31 2e 30 3b 0a 20 20  loat o = 1.0;.  
39d9c 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a      for(jj=0; jj
39d9d 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
39d9e 29 3b 20 6a 6a 2b 3d 32 29 7b 0a 20 20 20 20 20  ); jj+=2){.     
39d9f 20 20 20 64 6f 75 62 6c 65 20 78 31 3b 0a 20 20     double x1;.  
39da0 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 32 3b        double x2;
39da1 0a 0a 20 20 20 20 20 20 20 20 78 31 20 3d 20 4d  ..        x1 = M
39da2 41 58 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  AX(DCOORD(p->aCo
39da3 6f 72 64 5b 6a 6a 5d 29 2c 20 44 43 4f 4f 52 44  ord[jj]), DCOORD
39da4 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72  (aCell[ii].aCoor
39da5 64 5b 6a 6a 5d 29 29 3b 0a 20 20 20 20 20 20 20  d[jj]));.       
39da6 20 78 32 20 3d 20 4d 49 4e 28 44 43 4f 4f 52 44   x2 = MIN(DCOORD
39da7 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 2b 31 5d  (p->aCoord[jj+1]
39da8 29 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b  ), DCOORD(aCell[
39da9 69 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 2b 31 5d  ii].aCoord[jj+1]
39daa 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28  ));..        if(
39dab 20 78 32 3c 78 31 20 29 7b 0a 20 20 20 20 20 20   x2<x1 ){.      
39dac 20 20 20 20 6f 20 3d 20 30 2e 30 3b 0a 20 20 20      o = 0.0;.   
39dad 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
39dae 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
39daf 20 20 20 20 20 20 20 6f 20 3d 20 6f 20 2a 20 28         o = o * (
39db0 78 32 2d 78 31 29 3b 0a 20 20 20 20 20 20 20 20  x2-x1);.        
39db1 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
39db2 6f 76 65 72 6c 61 70 20 2b 3d 20 6f 3b 0a 20 20  overlap += o;.  
39db3 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
39db4 20 6f 76 65 72 6c 61 70 3b 0a 7d 0a 23 65 6e 64   overlap;.}.#end
39db5 69 66 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  if..#if VARIANT_
39db6 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
39db7 53 55 42 54 52 45 45 0a 73 74 61 74 69 63 20 66  SUBTREE.static f
39db8 6c 6f 61 74 20 63 65 6c 6c 4f 76 65 72 6c 61 70  loat cellOverlap
39db9 45 6e 6c 61 72 67 65 6d 65 6e 74 28 0a 20 20 52  Enlargement(.  R
39dba 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
39dbb 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 0a   RtreeCell *p, .
39dbc 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 49 6e    RtreeCell *pIn
39dbd 73 65 72 74 2c 20 0a 20 20 52 74 72 65 65 43 65  sert, .  RtreeCe
39dbe 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e  ll *aCell, .  in
39dbf 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20  t nCell, .  int 
39dc0 69 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20 66 6c  iExclude.){.  fl
39dc1 6f 61 74 20 62 65 66 6f 72 65 3b 0a 20 20 66 6c  oat before;.  fl
39dc2 6f 61 74 20 61 66 74 65 72 3b 0a 20 20 62 65 66  oat after;.  bef
39dc3 6f 72 65 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61  ore = cellOverla
39dc4 70 28 70 52 74 72 65 65 2c 20 70 2c 20 61 43 65  p(pRtree, p, aCe
39dc5 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69 45 78 63 6c  ll, nCell, iExcl
39dc6 75 64 65 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f  ude);.  cellUnio
39dc7 6e 28 70 52 74 72 65 65 2c 20 70 2c 20 70 49 6e  n(pRtree, p, pIn
39dc8 73 65 72 74 29 3b 0a 20 20 61 66 74 65 72 20 3d  sert);.  after =
39dc9 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74   cellOverlap(pRt
39dca 72 65 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e  ree, p, aCell, n
39dcb 43 65 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b  Cell, iExclude);
39dcc 0a 20 20 72 65 74 75 72 6e 20 61 66 74 65 72 2d  .  return after-
39dcd 62 65 66 6f 72 65 3b 0a 7d 0a 23 65 6e 64 69 66  before;.}.#endif
39dce 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
39dcf 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
39dd0 73 20 74 68 65 20 43 68 6f 6f 73 65 4c 65 61 66  s the ChooseLeaf
39dd1 20 61 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20   algorithm from 
39dd2 47 75 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2a 20 43  Gutman[84]..** C
39dd3 68 6f 6f 73 65 53 75 62 54 72 65 65 20 69 6e 20  hooseSubTree in 
39dd4 72 2a 74 72 65 65 20 74 65 72 6d 69 6e 6f 6c 6f  r*tree terminolo
39dd5 67 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  gy..*/.static in
39dd6 74 20 43 68 6f 6f 73 65 4c 65 61 66 28 0a 20 20  t ChooseLeaf(.  
39dd7 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20  Rtree *pRtree,  
39dd8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39dd9 52 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20  Rtree table */. 
39dda 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
39ddb 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l,            /*
39ddc 20 43 65 6c 6c 20 74 6f 20 69 6e 73 65 72 74 20   Cell to insert 
39ddd 69 6e 74 6f 20 72 74 72 65 65 20 2a 2f 0a 20 20  into rtree */.  
39dde 69 6e 74 20 69 48 65 69 67 68 74 2c 20 20 20 20  int iHeight,    
39ddf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39de0 48 65 69 67 68 74 20 6f 66 20 73 75 62 2d 74 72  Height of sub-tr
39de1 65 65 20 72 6f 6f 74 65 64 20 61 74 20 70 43 65  ee rooted at pCe
39de2 6c 6c 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64  ll */.  RtreeNod
39de3 65 20 2a 2a 70 70 4c 65 61 66 20 20 20 20 20 20  e **ppLeaf      
39de4 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 6c       /* OUT: Sel
39de5 65 63 74 65 64 20 6c 65 61 66 20 70 61 67 65 20  ected leaf page 
39de6 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
39de7 20 20 69 6e 74 20 69 69 3b 0a 20 20 52 74 72 65    int ii;.  Rtre
39de8 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20  eNode *pNode;.  
39de9 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
39dea 28 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26  (pRtree, 1, 0, &
39deb 70 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69  pNode);..  for(i
39dec 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
39ded 4f 4b 20 26 26 20 69 69 3c 28 70 52 74 72 65 65  OK && ii<(pRtree
39dee 2d 3e 69 44 65 70 74 68 2d 69 48 65 69 67 68 74  ->iDepth-iHeight
39def 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  ); ii++){.    in
39df0 74 20 69 43 65 6c 6c 3b 0a 20 20 20 20 73 71 6c  t iCell;.    sql
39df1 69 74 65 33 5f 69 6e 74 36 34 20 69 42 65 73 74  ite3_int64 iBest
39df2 3b 0a 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69  ;..    float fMi
39df3 6e 47 72 6f 77 74 68 3b 0a 20 20 20 20 66 6c 6f  nGrowth;.    flo
39df4 61 74 20 66 4d 69 6e 41 72 65 61 3b 0a 20 20 20  at fMinArea;.   
39df5 20 66 6c 6f 61 74 20 66 4d 69 6e 4f 76 65 72 6c   float fMinOverl
39df6 61 70 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 43 65  ap;..    int nCe
39df7 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
39df8 29 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  );.    RtreeCell
39df9 20 63 65 6c 6c 3b 0a 20 20 20 20 52 74 72 65 65   cell;.    Rtree
39dfa 4e 6f 64 65 20 2a 70 43 68 69 6c 64 3b 0a 0a 20  Node *pChild;.. 
39dfb 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43     RtreeCell *aC
39dfc 65 6c 6c 20 3d 20 30 3b 0a 0a 23 69 66 20 56 41  ell = 0;..#if VA
39dfd 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
39dfe 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 20 20  CHOOSESUBTREE.  
39dff 20 20 69 66 28 20 69 69 3d 3d 28 70 52 74 72 65    if( ii==(pRtre
39e00 65 2d 3e 69 44 65 70 74 68 2d 31 29 20 29 7b 0a  e->iDepth-1) ){.
39e01 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20        int jj;.  
39e02 20 20 20 20 61 43 65 6c 6c 20 3d 20 73 71 6c 69      aCell = sqli
39e03 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
39e04 66 28 52 74 72 65 65 43 65 6c 6c 29 2a 6e 43 65  f(RtreeCell)*nCe
39e05 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ll);.      if( !
39e06 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  aCell ){.       
39e07 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
39e08 45 4d 3b 0a 20 20 20 20 20 20 20 20 6e 6f 64 65  EM;.        node
39e09 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
39e0a 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20  pNode);.        
39e0b 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20  pNode = 0;.     
39e0c 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
39e0d 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6a     }.      for(j
39e0e 6a 3d 30 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a  j=0; jj<nCell; j
39e0f 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 6e 6f  j++){.        no
39e10 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65  deGetCell(pRtree
39e11 2c 20 70 4e 6f 64 65 2c 20 6a 6a 2c 20 26 61 43  , pNode, jj, &aC
39e12 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20  ell[jj]);.      
39e13 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  }.    }.#endif..
39e14 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68      /* Select th
39e15 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 77 68 69  e child node whi
39e16 63 68 20 77 69 6c 6c 20 62 65 20 65 6e 6c 61 72  ch will be enlar
39e17 67 65 64 20 74 68 65 20 6c 65 61 73 74 20 69 66  ged the least if
39e18 20 70 43 65 6c 6c 0a 20 20 20 20 2a 2a 20 69 73   pCell.    ** is
39e19 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69   inserted into i
39e1a 74 2e 20 52 65 73 6f 6c 76 65 20 74 69 65 73 20  t. Resolve ties 
39e1b 62 79 20 63 68 6f 6f 73 69 6e 67 20 74 68 65 20  by choosing the 
39e1c 65 6e 74 72 79 20 77 69 74 68 0a 20 20 20 20 2a  entry with.    *
39e1d 2a 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 61  * the smallest a
39e1e 72 65 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  rea..    */.    
39e1f 66 6f 72 28 69 43 65 6c 6c 3d 30 3b 20 69 43 65  for(iCell=0; iCe
39e20 6c 6c 3c 6e 43 65 6c 6c 3b 20 69 43 65 6c 6c 2b  ll<nCell; iCell+
39e21 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  +){.      float 
39e22 67 72 6f 77 74 68 3b 0a 20 20 20 20 20 20 66 6c  growth;.      fl
39e23 6f 61 74 20 61 72 65 61 3b 0a 20 20 20 20 20 20  oat area;.      
39e24 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 20 3d 20  float overlap = 
39e25 30 2e 30 3b 0a 20 20 20 20 20 20 6e 6f 64 65 47  0.0;.      nodeG
39e26 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  etCell(pRtree, p
39e27 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 26 63 65  Node, iCell, &ce
39e28 6c 6c 29 3b 0a 20 20 20 20 20 20 67 72 6f 77 74  ll);.      growt
39e29 68 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70  h = cellGrowth(p
39e2a 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43  Rtree, &cell, pC
39e2b 65 6c 6c 29 3b 0a 20 20 20 20 20 20 61 72 65 61  ell);.      area
39e2c 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72   = cellArea(pRtr
39e2d 65 65 2c 20 26 63 65 6c 6c 29 3b 0a 23 69 66 20  ee, &cell);.#if 
39e2e 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
39e2f 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a  E_CHOOSESUBTREE.
39e30 20 20 20 20 20 20 69 66 28 20 69 69 3d 3d 28 70        if( ii==(p
39e31 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 31 29  Rtree->iDepth-1)
39e32 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 76 65 72   ){.        over
39e33 6c 61 70 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61  lap = cellOverla
39e34 70 45 6e 6c 61 72 67 65 6d 65 6e 74 28 70 52 74  pEnlargement(pRt
39e35 72 65 65 2c 26 63 65 6c 6c 2c 70 43 65 6c 6c 2c  ree,&cell,pCell,
39e36 61 43 65 6c 6c 2c 6e 43 65 6c 6c 2c 69 43 65 6c  aCell,nCell,iCel
39e37 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  l);.      }.#end
39e38 69 66 0a 20 20 20 20 20 20 69 66 28 20 28 69 43  if.      if( (iC
39e39 65 6c 6c 3d 3d 30 29 20 0a 20 20 20 20 20 20 20  ell==0) .       
39e3a 7c 7c 20 28 6f 76 65 72 6c 61 70 3c 66 4d 69 6e  || (overlap<fMin
39e3b 4f 76 65 72 6c 61 70 29 20 0a 20 20 20 20 20 20  Overlap) .      
39e3c 20 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 4d   || (overlap==fM
39e3d 69 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72 6f  inOverlap && gro
39e3e 77 74 68 3c 66 4d 69 6e 47 72 6f 77 74 68 29 0a  wth<fMinGrowth).
39e3f 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c         || (overl
39e40 61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c 61 70 20  ap==fMinOverlap 
39e41 26 26 20 67 72 6f 77 74 68 3d 3d 66 4d 69 6e 47  && growth==fMinG
39e42 72 6f 77 74 68 20 26 26 20 61 72 65 61 3c 66 4d  rowth && area<fM
39e43 69 6e 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b  inArea).      ){
39e44 0a 20 20 20 20 20 20 20 20 66 4d 69 6e 4f 76 65  .        fMinOve
39e45 72 6c 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b 0a  rlap = overlap;.
39e46 20 20 20 20 20 20 20 20 66 4d 69 6e 47 72 6f 77          fMinGrow
39e47 74 68 20 3d 20 67 72 6f 77 74 68 3b 0a 20 20 20  th = growth;.   
39e48 20 20 20 20 20 66 4d 69 6e 41 72 65 61 20 3d 20       fMinArea = 
39e49 61 72 65 61 3b 0a 20 20 20 20 20 20 20 20 69 42  area;.        iB
39e4a 65 73 74 20 3d 20 63 65 6c 6c 2e 69 52 6f 77 69  est = cell.iRowi
39e4b 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  d;.      }.    }
39e4c 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
39e4d 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20 20 20 72  ee(aCell);.    r
39e4e 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
39e4f 70 52 74 72 65 65 2c 20 69 42 65 73 74 2c 20 70  pRtree, iBest, p
39e50 4e 6f 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0a  Node, &pChild);.
39e51 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
39e52 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
39e53 20 20 20 20 70 4e 6f 64 65 20 3d 20 70 43 68 69      pNode = pChi
39e54 6c 64 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4c 65  ld;.  }..  *ppLe
39e55 61 66 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 65  af = pNode;.  re
39e56 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
39e57 2a 20 41 20 63 65 6c 6c 20 77 69 74 68 20 74 68  * A cell with th
39e58 65 20 73 61 6d 65 20 63 6f 6e 74 65 6e 74 20 61  e same content a
39e59 73 20 70 43 65 6c 6c 20 68 61 73 20 6a 75 73 74  s pCell has just
39e5a 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64 20 69   been inserted i
39e5b 6e 74 6f 0a 2a 2a 20 74 68 65 20 6e 6f 64 65 20  nto.** the node 
39e5c 70 4e 6f 64 65 2e 20 54 68 69 73 20 66 75 6e 63  pNode. This func
39e5d 74 69 6f 6e 20 75 70 64 61 74 65 73 20 74 68 65  tion updates the
39e5e 20 62 6f 75 6e 64 69 6e 67 20 62 6f 78 20 63 65   bounding box ce
39e5f 6c 6c 73 20 69 6e 0a 2a 2a 20 61 6c 6c 20 61 6e  lls in.** all an
39e60 63 65 73 74 6f 72 20 65 6c 65 6d 65 6e 74 73 2e  cestor elements.
39e61 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
39e62 41 64 6a 75 73 74 54 72 65 65 28 0a 20 20 52 74  AdjustTree(.  Rt
39e63 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20 20 20  ree *pRtree,    
39e64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39e65 2f 2a 20 52 74 72 65 65 20 74 61 62 6c 65 20 2a  /* Rtree table *
39e66 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  /.  RtreeNode *p
39e67 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20  Node,           
39e68 20 20 20 20 20 20 2f 2a 20 41 64 6a 75 73 74 20        /* Adjust 
39e69 61 6e 63 65 73 74 72 79 20 6f 66 20 74 68 69 73  ancestry of this
39e6a 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 52 74 72 65   node. */.  Rtre
39e6b 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 20 20 20  eCell *pCell    
39e6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39e6d 20 54 68 69 73 20 63 65 6c 6c 20 77 61 73 20 6a   This cell was j
39e6e 75 73 74 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a  ust inserted */.
39e6f 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ){.  RtreeNode *
39e70 70 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 77 68 69  p = pNode;.  whi
39e71 6c 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20 29  le( p->pParent )
39e72 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  {.    RtreeCell 
39e73 63 65 6c 6c 3b 0a 20 20 20 20 52 74 72 65 65 4e  cell;.    RtreeN
39e74 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70  ode *pParent = p
39e75 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 69  ->pParent;.    i
39e76 6e 74 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 50  nt iCell = nodeP
39e77 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72 65  arentIndex(pRtre
39e78 65 2c 20 70 29 3b 0a 0a 20 20 20 20 6e 6f 64 65  e, p);..    node
39e79 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
39e7a 70 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20  pParent, iCell, 
39e7b 26 63 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20  &cell);.    if( 
39e7c 21 63 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 70 52  !cellContains(pR
39e7d 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65  tree, &cell, pCe
39e7e 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 65 6c  ll) ){.      cel
39e7f 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
39e80 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  cell, pCell);.  
39e81 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74      nodeOverwrit
39e82 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50  eCell(pRtree, pP
39e83 61 72 65 6e 74 2c 20 26 63 65 6c 6c 2c 20 69 43  arent, &cell, iC
39e84 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 0a 20 20  ell);.    }. .  
39e85 20 20 70 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20    p = pParent;. 
39e86 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74   }.}../*.** Writ
39e87 65 20 6d 61 70 70 69 6e 67 20 28 69 52 6f 77 69  e mapping (iRowi
39e88 64 2d 3e 69 4e 6f 64 65 29 20 74 6f 20 74 68 65  d->iNode) to the
39e89 20 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20 74   <rtree>_rowid t
39e8a 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
39e8b 69 6e 74 20 72 6f 77 69 64 57 72 69 74 65 28 52  int rowidWrite(R
39e8c 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 73 71  tree *pRtree, sq
39e8d 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
39e8e 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  id, sqlite3_int6
39e8f 34 20 69 4e 6f 64 65 29 7b 0a 20 20 73 71 6c 69  4 iNode){.  sqli
39e90 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
39e91 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
39e92 69 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a  id, 1, iRowid);.
39e93 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
39e94 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57 72  nt64(pRtree->pWr
39e95 69 74 65 52 6f 77 69 64 2c 20 32 2c 20 69 4e 6f  iteRowid, 2, iNo
39e96 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  de);.  sqlite3_s
39e97 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57 72 69  tep(pRtree->pWri
39e98 74 65 52 6f 77 69 64 29 3b 0a 20 20 72 65 74 75  teRowid);.  retu
39e99 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  rn sqlite3_reset
39e9a 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  (pRtree->pWriteR
39e9b 6f 77 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  owid);.}../*.** 
39e9c 57 72 69 74 65 20 6d 61 70 70 69 6e 67 20 28 69  Write mapping (i
39e9d 4e 6f 64 65 2d 3e 69 50 61 72 29 20 74 6f 20 74  Node->iPar) to t
39e9e 68 65 20 3c 72 74 72 65 65 3e 5f 70 61 72 65 6e  he <rtree>_paren
39e9f 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  t table..*/.stat
39ea0 69 63 20 69 6e 74 20 70 61 72 65 6e 74 57 72 69  ic int parentWri
39ea1 74 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  te(Rtree *pRtree
39ea2 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
39ea3 69 4e 6f 64 65 2c 20 73 71 6c 69 74 65 33 5f 69  iNode, sqlite3_i
39ea4 6e 74 36 34 20 69 50 61 72 29 7b 0a 20 20 73 71  nt64 iPar){.  sq
39ea5 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
39ea6 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  (pRtree->pWriteP
39ea7 61 72 65 6e 74 2c 20 31 2c 20 69 4e 6f 64 65 29  arent, 1, iNode)
39ea8 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ;.  sqlite3_bind
39ea9 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
39eaa 57 72 69 74 65 50 61 72 65 6e 74 2c 20 32 2c 20  WriteParent, 2, 
39eab 69 50 61 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  iPar);.  sqlite3
39eac 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57  _step(pRtree->pW
39ead 72 69 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 72  riteParent);.  r
39eae 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65  eturn sqlite3_re
39eaf 73 65 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69  set(pRtree->pWri
39eb0 74 65 50 61 72 65 6e 74 29 3b 0a 7d 0a 0a 73 74  teParent);.}..st
39eb1 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e  atic int rtreeIn
39eb2 73 65 72 74 43 65 6c 6c 28 52 74 72 65 65 20 2a  sertCell(Rtree *
39eb3 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20 52  , RtreeNode *, R
39eb4 74 72 65 65 43 65 6c 6c 20 2a 2c 20 69 6e 74 29  treeCell *, int)
39eb5 3b 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  ;..#if VARIANT_G
39eb6 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50  UTTMAN_LINEAR_SP
39eb7 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  LIT./*.** Implem
39eb8 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
39eb9 6c 69 6e 65 61 72 20 76 61 72 69 61 6e 74 20 6f  linear variant o
39eba 66 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28 29  f the PickNext()
39ebb 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a   function from.*
39ebc 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a  * Guttman[84]..*
39ebd 2f 0a 73 74 61 74 69 63 20 52 74 72 65 65 43 65  /.static RtreeCe
39ebe 6c 6c 20 2a 4c 69 6e 65 61 72 50 69 63 6b 4e 65  ll *LinearPickNe
39ebf 78 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  xt(.  Rtree *pRt
39ec0 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ree,.  RtreeCell
39ec1 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20   *aCell, .  int 
39ec2 6e 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65 65 43  nCell, .  RtreeC
39ec3 65 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c 20 0a  ell *pLeftBox, .
39ec4 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 52 69    RtreeCell *pRi
39ec5 67 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20 2a 61  ghtBox,.  int *a
39ec6 69 55 73 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69  iUsed.){.  int i
39ec7 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 61  i;.  for(ii=0; a
39ec8 69 55 73 65 64 5b 69 69 5d 3b 20 69 69 2b 2b 29  iUsed[ii]; ii++)
39ec9 3b 0a 20 20 61 69 55 73 65 64 5b 69 69 5d 20 3d  ;.  aiUsed[ii] =
39eca 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43   1;.  return &aC
39ecb 65 6c 6c 5b 69 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  ell[ii];.}../*.*
39ecc 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
39ecd 20 6f 66 20 74 68 65 20 6c 69 6e 65 61 72 20 76   of the linear v
39ece 61 72 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69  ariant of the Pi
39ecf 63 6b 53 65 65 64 73 28 29 20 66 75 6e 63 74 69  ckSeeds() functi
39ed0 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d  on from.** Guttm
39ed1 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69  an[84]..*/.stati
39ed2 63 20 76 6f 69 64 20 4c 69 6e 65 61 72 50 69 63  c void LinearPic
39ed3 6b 53 65 65 64 73 28 0a 20 20 52 74 72 65 65 20  kSeeds(.  Rtree 
39ed4 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
39ed5 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
39ed6 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
39ed7 74 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a  t *piLeftSeed, .
39ed8 20 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65    int *piRightSe
39ed9 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ed.){.  int i;. 
39eda 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20 3d   int iLeftSeed =
39edb 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67 68 74   0;.  int iRight
39edc 53 65 65 64 20 3d 20 31 3b 0a 20 20 66 6c 6f 61  Seed = 1;.  floa
39edd 74 20 6d 61 78 4e 6f 72 6d 61 6c 49 6e 6e 65 72  t maxNormalInner
39ede 57 69 64 74 68 20 3d 20 30 2e 30 3b 0a 0a 20 20  Width = 0.0;..  
39edf 2f 2a 20 50 69 63 6b 20 74 77 6f 20 22 73 65 65  /* Pick two "see
39ee0 64 22 20 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68  d" cells from th
39ee1 65 20 61 72 72 61 79 20 6f 66 20 63 65 6c 6c 73  e array of cells
39ee2 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 20  . The algorithm 
39ee3 75 73 65 64 0a 20 20 2a 2a 20 68 65 72 65 20 69  used.  ** here i
39ee4 73 20 74 68 65 20 4c 69 6e 65 61 72 50 69 63 6b  s the LinearPick
39ee5 53 65 65 64 73 20 61 6c 67 6f 72 69 74 68 6d 20  Seeds algorithm 
39ee6 66 72 6f 6d 20 47 75 74 6d 61 6e 5b 31 39 38 34  from Gutman[1984
39ee7 5d 2e 20 54 68 65 20 0a 20 20 2a 2a 20 69 6e 64  ]. The .  ** ind
39ee8 69 63 65 73 20 6f 66 20 74 68 65 20 74 77 6f 20  ices of the two 
39ee9 73 65 65 64 20 63 65 6c 6c 73 20 69 6e 20 74 68  seed cells in th
39eea 65 20 61 72 72 61 79 20 61 72 65 20 73 74 6f 72  e array are stor
39eeb 65 64 20 69 6e 20 6c 6f 63 61 6c 0a 20 20 2a 2a  ed in local.  **
39eec 20 76 61 72 69 61 62 6c 65 73 20 69 4c 65 66 74   variables iLeft
39eed 53 65 65 6b 20 61 6e 64 20 69 52 69 67 68 74 53  Seek and iRightS
39eee 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  eed..  */.  for(
39eef 69 3d 30 3b 20 69 3c 70 52 74 72 65 65 2d 3e 6e  i=0; i<pRtree->n
39ef0 44 69 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66  Dim; i++){.    f
39ef1 6c 6f 61 74 20 78 31 20 3d 20 44 43 4f 4f 52 44  loat x1 = DCOORD
39ef2 28 61 43 65 6c 6c 5b 30 5d 2e 61 43 6f 6f 72 64  (aCell[0].aCoord
39ef3 5b 69 2a 32 5d 29 3b 0a 20 20 20 20 66 6c 6f 61  [i*2]);.    floa
39ef4 74 20 78 32 20 3d 20 44 43 4f 4f 52 44 28 61 43  t x2 = DCOORD(aC
39ef5 65 6c 6c 5b 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a  ell[0].aCoord[i*
39ef6 32 2b 31 5d 29 3b 0a 20 20 20 20 66 6c 6f 61 74  2+1]);.    float
39ef7 20 78 33 20 3d 20 78 31 3b 0a 20 20 20 20 66 6c   x3 = x1;.    fl
39ef8 6f 61 74 20 78 34 20 3d 20 78 32 3b 0a 20 20 20  oat x4 = x2;.   
39ef9 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20 20 69 6e   int jj;..    in
39efa 74 20 69 43 65 6c 6c 4c 65 66 74 20 3d 20 30 3b  t iCellLeft = 0;
39efb 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 52 69  .    int iCellRi
39efc 67 68 74 20 3d 20 30 3b 0a 0a 20 20 20 20 66 6f  ght = 0;..    fo
39efd 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 43 65 6c 6c  r(jj=1; jj<nCell
39efe 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66  ; jj++){.      f
39eff 6c 6f 61 74 20 6c 65 66 74 20 3d 20 44 43 4f 4f  loat left = DCOO
39f00 52 44 28 61 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f  RD(aCell[jj].aCo
39f01 6f 72 64 5b 69 2a 32 5d 29 3b 0a 20 20 20 20 20  ord[i*2]);.     
39f02 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20 44   float right = D
39f03 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 6a 6a 5d 2e  COORD(aCell[jj].
39f04 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d 29 3b 0a  aCoord[i*2+1]);.
39f05 0a 20 20 20 20 20 20 69 66 28 20 6c 65 66 74 3c  .      if( left<
39f06 78 31 20 29 20 78 31 20 3d 20 6c 65 66 74 3b 0a  x1 ) x1 = left;.
39f07 20 20 20 20 20 20 69 66 28 20 72 69 67 68 74 3e        if( right>
39f08 78 34 20 29 20 78 34 20 3d 20 72 69 67 68 74 3b  x4 ) x4 = right;
39f09 0a 20 20 20 20 20 20 69 66 28 20 6c 65 66 74 3e  .      if( left>
39f0a 78 33 20 29 7b 0a 20 20 20 20 20 20 20 20 78 33  x3 ){.        x3
39f0b 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20 20 20   = left;.       
39f0c 20 69 43 65 6c 6c 52 69 67 68 74 20 3d 20 6a 6a   iCellRight = jj
39f0d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
39f0e 69 66 28 20 72 69 67 68 74 3c 78 32 20 29 7b 0a  if( right<x2 ){.
39f0f 20 20 20 20 20 20 20 20 78 32 20 3d 20 72 69 67          x2 = rig
39f10 68 74 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c  ht;.        iCel
39f11 6c 4c 65 66 74 20 3d 20 6a 6a 3b 0a 20 20 20 20  lLeft = jj;.    
39f12 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69    }.    }..    i
39f13 66 28 20 78 34 21 3d 78 31 20 29 7b 0a 20 20 20  f( x4!=x1 ){.   
39f14 20 20 20 66 6c 6f 61 74 20 6e 6f 72 6d 61 6c 77     float normalw
39f15 69 64 74 68 20 3d 20 28 78 33 20 2d 20 78 32 29  idth = (x3 - x2)
39f16 20 2f 20 28 78 34 20 2d 20 78 31 29 3b 0a 20 20   / (x4 - x1);.  
39f17 20 20 20 20 69 66 28 20 6e 6f 72 6d 61 6c 77 69      if( normalwi
39f18 64 74 68 3e 6d 61 78 4e 6f 72 6d 61 6c 49 6e 6e  dth>maxNormalInn
39f19 65 72 57 69 64 74 68 20 29 7b 0a 20 20 20 20 20  erWidth ){.     
39f1a 20 20 20 69 4c 65 66 74 53 65 65 64 20 3d 20 69     iLeftSeed = i
39f1b 43 65 6c 6c 4c 65 66 74 3b 0a 20 20 20 20 20 20  CellLeft;.      
39f1c 20 20 69 52 69 67 68 74 53 65 65 64 20 3d 20 69    iRightSeed = i
39f1d 43 65 6c 6c 52 69 67 68 74 3b 0a 20 20 20 20 20  CellRight;.     
39f1e 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
39f1f 2a 70 69 4c 65 66 74 53 65 65 64 20 3d 20 69 4c  *piLeftSeed = iL
39f20 65 66 74 53 65 65 64 3b 0a 20 20 2a 70 69 52 69  eftSeed;.  *piRi
39f21 67 68 74 53 65 65 64 20 3d 20 69 52 69 67 68 74  ghtSeed = iRight
39f22 53 65 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Seed;.}.#endif /
39f23 2a 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  * VARIANT_GUTTMA
39f24 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 20 2a  N_LINEAR_SPLIT *
39f25 2f 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  /..#if VARIANT_G
39f26 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
39f27 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70  _SPLIT./*.** Imp
39f28 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
39f29 68 65 20 71 75 61 64 72 61 74 69 63 20 76 61 72  he quadratic var
39f2a 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b  iant of the Pick
39f2b 4e 65 78 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  Next() function 
39f2c 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
39f2d 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52  84]..*/.static R
39f2e 74 72 65 65 43 65 6c 6c 20 2a 51 75 61 64 72 61  treeCell *Quadra
39f2f 74 69 63 50 69 63 6b 4e 65 78 74 28 0a 20 20 52  ticPickNext(.  R
39f30 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20  tree *pRtree,.  
39f31 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
39f32 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20  , .  int nCell, 
39f33 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c  .  RtreeCell *pL
39f34 65 66 74 42 6f 78 2c 20 0a 20 20 52 74 72 65 65  eftBox, .  Rtree
39f35 43 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c  Cell *pRightBox,
39f36 0a 20 20 69 6e 74 20 2a 61 69 55 73 65 64 0a 29  .  int *aiUsed.)
39f37 7b 0a 20 20 23 64 65 66 69 6e 65 20 46 41 42 53  {.  #define FABS
39f38 28 61 29 20 28 28 61 29 3c 30 2e 30 3f 2d 31 2e  (a) ((a)<0.0?-1.
39f39 30 2a 28 61 29 3a 28 61 29 29 0a 0a 20 20 69 6e  0*(a):(a))..  in
39f3a 74 20 69 53 65 6c 65 63 74 20 3d 20 2d 31 3b 0a  t iSelect = -1;.
39f3b 20 20 66 6c 6f 61 74 20 66 44 69 66 66 3b 0a 20    float fDiff;. 
39f3c 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69   int ii;.  for(i
39f3d 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
39f3e 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 69  i++){.    if( ai
39f3f 55 73 65 64 5b 69 69 5d 3d 3d 30 20 29 7b 0a 20  Used[ii]==0 ){. 
39f40 20 20 20 20 20 66 6c 6f 61 74 20 6c 65 66 74 20       float left 
39f41 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74  = cellGrowth(pRt
39f42 72 65 65 2c 20 70 4c 65 66 74 42 6f 78 2c 20 26  ree, pLeftBox, &
39f43 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20  aCell[ii]);.    
39f44 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20    float right = 
39f45 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65  cellGrowth(pRtre
39f46 65 2c 20 70 4c 65 66 74 42 6f 78 2c 20 26 61 43  e, pLeftBox, &aC
39f47 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20  ell[ii]);.      
39f48 66 6c 6f 61 74 20 64 69 66 66 20 3d 20 46 41 42  float diff = FAB
39f49 53 28 72 69 67 68 74 2d 6c 65 66 74 29 3b 0a 20  S(right-left);. 
39f4a 20 20 20 20 20 69 66 28 20 69 53 65 6c 65 63 74       if( iSelect
39f4b 3c 30 20 7c 7c 20 64 69 66 66 3e 66 44 69 66 66  <0 || diff>fDiff
39f4c 20 29 7b 0a 20 20 20 20 20 20 20 20 66 44 69 66   ){.        fDif
39f4d 66 20 3d 20 64 69 66 66 3b 0a 20 20 20 20 20 20  f = diff;.      
39f4e 20 20 69 53 65 6c 65 63 74 20 3d 20 69 69 3b 0a    iSelect = ii;.
39f4f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
39f50 7d 0a 20 20 61 69 55 73 65 64 5b 69 53 65 6c 65  }.  aiUsed[iSele
39f51 63 74 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75 72  ct] = 1;.  retur
39f52 6e 20 26 61 43 65 6c 6c 5b 69 53 65 6c 65 63 74  n &aCell[iSelect
39f53 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  ];.}../*.** Impl
39f54 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
39f55 65 20 71 75 61 64 72 61 74 69 63 20 76 61 72 69  e quadratic vari
39f56 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b 53  ant of the PickS
39f57 65 65 64 73 28 29 20 66 75 6e 63 74 69 6f 6e 20  eeds() function 
39f58 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
39f59 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  84]..*/.static v
39f5a 6f 69 64 20 51 75 61 64 72 61 74 69 63 50 69 63  oid QuadraticPic
39f5b 6b 53 65 65 64 73 28 0a 20 20 52 74 72 65 65 20  kSeeds(.  Rtree 
39f5c 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
39f5d 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
39f5e 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
39f5f 74 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a  t *piLeftSeed, .
39f60 20 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65    int *piRightSe
39f61 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  ed.){.  int ii;.
39f62 20 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 69 6e 74    int jj;..  int
39f63 20 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a   iLeftSeed = 0;.
39f64 20 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64    int iRightSeed
39f65 20 3d 20 31 3b 0a 20 20 66 6c 6f 61 74 20 66 57   = 1;.  float fW
39f66 61 73 74 65 20 3d 20 30 2e 30 3b 0a 0a 20 20 66  aste = 0.0;..  f
39f67 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c  or(ii=0; ii<nCel
39f68 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 66 6f  l; ii++){.    fo
39f69 72 28 6a 6a 3d 69 69 2b 31 3b 20 6a 6a 3c 6e 43  r(jj=ii+1; jj<nC
39f6a 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  ell; jj++){.    
39f6b 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20    float right = 
39f6c 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c  cellArea(pRtree,
39f6d 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20   &aCell[jj]);.  
39f6e 20 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74 68      float growth
39f6f 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52   = cellGrowth(pR
39f70 74 72 65 65 2c 20 26 61 43 65 6c 6c 5b 69 69 5d  tree, &aCell[ii]
39f71 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20  , &aCell[jj]);. 
39f72 20 20 20 20 20 66 6c 6f 61 74 20 77 61 73 74 65       float waste
39f73 20 3d 20 67 72 6f 77 74 68 20 2d 20 72 69 67 68   = growth - righ
39f74 74 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 77 61  t;..      if( wa
39f75 73 74 65 3e 66 57 61 73 74 65 20 29 7b 0a 20 20  ste>fWaste ){.  
39f76 20 20 20 20 20 20 69 4c 65 66 74 53 65 65 64 20        iLeftSeed 
39f77 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 69 52  = ii;.        iR
39f78 69 67 68 74 53 65 65 64 20 3d 20 6a 6a 3b 0a 20  ightSeed = jj;. 
39f79 20 20 20 20 20 20 20 66 57 61 73 74 65 20 3d 20         fWaste = 
39f7a 77 61 73 74 65 3b 0a 20 20 20 20 20 20 7d 0a 20  waste;.      }. 
39f7b 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c     }.  }..  *piL
39f7c 65 66 74 53 65 65 64 20 3d 20 69 4c 65 66 74 53  eftSeed = iLeftS
39f7d 65 65 64 3b 0a 20 20 2a 70 69 52 69 67 68 74 53  eed;.  *piRightS
39f7e 65 65 64 20 3d 20 69 52 69 67 68 74 53 65 65 64  eed = iRightSeed
39f7f 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41  ;.}.#endif /* VA
39f80 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55  RIANT_GUTTMAN_QU
39f81 41 44 52 41 54 49 43 5f 53 50 4c 49 54 20 2a 2f  ADRATIC_SPLIT */
39f82 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
39f83 73 20 61 49 64 78 2c 20 61 44 69 73 74 61 6e 63  s aIdx, aDistanc
39f84 65 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c 6c  e and aSpare all
39f85 20 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79 73   point to arrays
39f86 20 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64 78   of size.** nIdx
39f87 2e 20 54 68 65 20 61 49 64 78 20 61 72 72 61 79  . The aIdx array
39f88 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 65   contains the se
39f89 74 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66 72  t of integers fr
39f8a 6f 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49 64  om 0 to .** (nId
39f8b 78 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74 69  x-1) in no parti
39f8c 63 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68 69  cular order. Thi
39f8d 73 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74 73  s function sorts
39f8e 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 69   the values.** i
39f8f 6e 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e 67  n aIdx according
39f90 20 74 6f 20 74 68 65 20 69 6e 64 65 78 65 64 20   to the indexed 
39f91 76 61 6c 75 65 73 20 69 6e 20 61 44 69 73 74 61  values in aDista
39f92 6e 63 65 2e 20 46 6f 72 0a 2a 2a 20 65 78 61 6d  nce. For.** exam
39f93 70 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68  ple, assuming th
39f94 65 20 69 6e 70 75 74 73 3a 0a 2a 2a 0a 2a 2a 20  e inputs:.**.** 
39f95 20 20 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20    aIdx      = { 
39f96 30 2c 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33  0,   1,   2,   3
39f97 20 7d 0a 2a 2a 20 20 20 61 44 69 73 74 61 6e 63   }.**   aDistanc
39f98 65 20 3d 20 7b 20 35 2e 30 2c 20 32 2e 30 2c 20  e = { 5.0, 2.0, 
39f99 37 2e 30 2c 20 36 2e 30 20 7d 0a 2a 2a 0a 2a 2a  7.0, 6.0 }.**.**
39f9a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   this function s
39f9b 65 74 73 20 74 68 65 20 61 49 64 78 20 61 72 72  ets the aIdx arr
39f9c 61 79 20 74 6f 20 63 6f 6e 74 61 69 6e 3a 0a 2a  ay to contain:.*
39f9d 2a 0a 2a 2a 20 20 20 61 49 64 78 20 20 20 20 20  *.**   aIdx     
39f9e 20 3d 20 7b 20 30 2c 20 20 20 31 2c 20 20 20 32   = { 0,   1,   2
39f9f 2c 20 20 20 33 20 7d 0a 2a 2a 0a 2a 2a 20 54 68  ,   3 }.**.** Th
39fa0 65 20 61 53 70 61 72 65 20 61 72 72 61 79 20 69  e aSpare array i
39fa1 73 20 75 73 65 64 20 61 73 20 74 65 6d 70 6f 72  s used as tempor
39fa2 61 72 79 20 77 6f 72 6b 69 6e 67 20 73 70 61 63  ary working spac
39fa3 65 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74  e by the.** sort
39fa4 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a  ing algorithm..*
39fa5 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 53 6f  /.static void So
39fa6 72 74 42 79 44 69 73 74 61 6e 63 65 28 0a 20 20  rtByDistance(.  
39fa7 69 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20 69 6e  int *aIdx, .  in
39fa8 74 20 6e 49 64 78 2c 20 0a 20 20 66 6c 6f 61 74  t nIdx, .  float
39fa9 20 2a 61 44 69 73 74 61 6e 63 65 2c 20 0a 20 20   *aDistance, .  
39faa 69 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a 20  int *aSpare.){. 
39fab 20 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a 20   if( nIdx>1 ){. 
39fac 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30     int iLeft = 0
39fad 3b 0a 20 20 20 20 69 6e 74 20 69 52 69 67 68 74  ;.    int iRight
39fae 20 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e   = 0;..    int n
39faf 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20  Left = nIdx/2;. 
39fb0 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20     int nRight = 
39fb1 6e 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20  nIdx-nLeft;.    
39fb2 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61 49 64  int *aLeft = aId
39fb3 78 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52 69 67  x;.    int *aRig
39fb4 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74  ht = &aIdx[nLeft
39fb5 5d 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79 44 69  ];..    SortByDi
39fb6 73 74 61 6e 63 65 28 61 4c 65 66 74 2c 20 6e 4c  stance(aLeft, nL
39fb7 65 66 74 2c 20 61 44 69 73 74 61 6e 63 65 2c 20  eft, aDistance, 
39fb8 61 53 70 61 72 65 29 3b 0a 20 20 20 20 53 6f 72  aSpare);.    Sor
39fb9 74 42 79 44 69 73 74 61 6e 63 65 28 61 52 69 67  tByDistance(aRig
39fba 68 74 2c 20 6e 52 69 67 68 74 2c 20 61 44 69 73  ht, nRight, aDis
39fbb 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a  tance, aSpare);.
39fbc 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 53 70 61  .    memcpy(aSpa
39fbd 72 65 2c 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f  re, aLeft, sizeo
39fbe 66 28 69 6e 74 29 2a 6e 4c 65 66 74 29 3b 0a 20  f(int)*nLeft);. 
39fbf 20 20 20 61 4c 65 66 74 20 3d 20 61 53 70 61 72     aLeft = aSpar
39fc0 65 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69  e;..    while( i
39fc1 4c 65 66 74 3c 6e 4c 65 66 74 20 7c 7c 20 69 52  Left<nLeft || iR
39fc2 69 67 68 74 3c 6e 52 69 67 68 74 20 29 7b 0a 20  ight<nRight ){. 
39fc3 20 20 20 20 20 69 66 28 20 69 4c 65 66 74 3d 3d       if( iLeft==
39fc4 6e 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  nLeft ){.       
39fc5 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
39fc6 68 74 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69  ht] = aRight[iRi
39fc7 67 68 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 52  ght];.        iR
39fc8 69 67 68 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65  ight++;.      }e
39fc9 6c 73 65 20 69 66 28 20 69 52 69 67 68 74 3d 3d  lse if( iRight==
39fca 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  nRight ){.      
39fcb 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69    aIdx[iLeft+iRi
39fcc 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65  ght] = aLeft[iLe
39fcd 66 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 4c 65  ft];.        iLe
39fce 66 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ft++;.      }els
39fcf 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  e{.        float
39fd0 20 66 4c 65 66 74 20 3d 20 61 44 69 73 74 61 6e   fLeft = aDistan
39fd1 63 65 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d  ce[aLeft[iLeft]]
39fd2 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20  ;.        float 
39fd3 66 52 69 67 68 74 20 3d 20 61 44 69 73 74 61 6e  fRight = aDistan
39fd4 63 65 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74  ce[aRight[iRight
39fd5 5d 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]];.        if( 
39fd6 66 4c 65 66 74 3c 66 52 69 67 68 74 20 29 7b 0a  fLeft<fRight ){.
39fd7 20 20 20 20 20 20 20 20 20 20 61 49 64 78 5b 69            aIdx[i
39fd8 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
39fd9 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20 20  Left[iLeft];.   
39fda 20 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a         iLeft++;.
39fdb 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
39fdc 20 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c           aIdx[iL
39fdd 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52  eft+iRight] = aR
39fde 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20  ight[iRight];.  
39fdf 20 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b          iRight++
39fe0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
39fe1 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30    }.    }..#if 0
39fe2 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  .    /* Check th
39fe3 61 74 20 74 68 65 20 73 6f 72 74 20 77 6f 72 6b  at the sort work
39fe4 65 64 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20  ed */.    {.    
39fe5 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
39fe6 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 49 64  for(jj=1; jj<nId
39fe7 78 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  x; jj++){.      
39fe8 20 20 66 6c 6f 61 74 20 6c 65 66 74 20 3d 20 61    float left = a
39fe9 44 69 73 74 61 6e 63 65 5b 61 49 64 78 5b 6a 6a  Distance[aIdx[jj
39fea 2d 31 5d 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c  -1]];.        fl
39feb 6f 61 74 20 72 69 67 68 74 20 3d 20 61 44 69 73  oat right = aDis
39fec 74 61 6e 63 65 5b 61 49 64 78 5b 6a 6a 5d 5d 3b  tance[aIdx[jj]];
39fed 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
39fee 20 6c 65 66 74 3c 3d 72 69 67 68 74 20 29 3b 0a   left<=right );.
39fef 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
39ff0 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ndif.  }.}../*.*
39ff1 2a 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64 78  * Arguments aIdx
39ff2 2c 20 61 43 65 6c 6c 20 61 6e 64 20 61 53 70 61  , aCell and aSpa
39ff3 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20  re all point to 
39ff4 61 72 72 61 79 73 20 6f 66 20 73 69 7a 65 0a 2a  arrays of size.*
39ff5 2a 20 6e 49 64 78 2e 20 54 68 65 20 61 49 64 78  * nIdx. The aIdx
39ff6 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20   array contains 
39ff7 74 68 65 20 73 65 74 20 6f 66 20 69 6e 74 65 67  the set of integ
39ff8 65 72 73 20 66 72 6f 6d 20 30 20 74 6f 20 0a 2a  ers from 0 to .*
39ff9 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e 20 6e 6f  * (nIdx-1) in no
39ffa 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65   particular orde
39ffb 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
39ffc 20 73 6f 72 74 73 20 74 68 65 20 76 61 6c 75 65   sorts the value
39ffd 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20 61 63 63  s.** in aIdx acc
39ffe 6f 72 64 69 6e 67 20 74 6f 20 64 69 6d 65 6e 73  ording to dimens
39fff 69 6f 6e 20 69 44 69 6d 20 6f 66 20 74 68 65 20  ion iDim of the 
3a000 63 65 6c 6c 73 20 69 6e 20 61 43 65 6c 6c 2e 20  cells in aCell. 
3a001 54 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 76  The.** minimum v
3a002 61 6c 75 65 20 6f 66 20 64 69 6d 65 6e 73 69 6f  alue of dimensio
3a003 6e 20 69 44 69 6d 20 69 73 20 63 6f 6e 73 69 64  n iDim is consid
3a004 65 72 65 64 20 66 69 72 73 74 2c 20 74 68 65 0a  ered first, the.
3a005 2a 2a 20 6d 61 78 69 6d 75 6d 20 75 73 65 64 20  ** maximum used 
3a006 74 6f 20 62 72 65 61 6b 20 74 69 65 73 2e 0a 2a  to break ties..*
3a007 2a 0a 2a 2a 20 54 68 65 20 61 53 70 61 72 65 20  *.** The aSpare 
3a008 61 72 72 61 79 20 69 73 20 75 73 65 64 20 61 73  array is used as
3a009 20 74 65 6d 70 6f 72 61 72 79 20 77 6f 72 6b 69   temporary worki
3a00a 6e 67 20 73 70 61 63 65 20 62 79 20 74 68 65 0a  ng space by the.
3a00b 2a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67 6f 72  ** sorting algor
3a00c 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ithm..*/.static 
3a00d 76 6f 69 64 20 53 6f 72 74 42 79 44 69 6d 65 6e  void SortByDimen
3a00e 73 69 6f 6e 28 0a 20 20 52 74 72 65 65 20 2a 70  sion(.  Rtree *p
3a00f 52 74 72 65 65 2c 0a 20 20 69 6e 74 20 2a 61 49  Rtree,.  int *aI
3a010 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c  dx, .  int nIdx,
3a011 20 0a 20 20 69 6e 74 20 69 44 69 6d 2c 20 0a 20   .  int iDim, . 
3a012 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
3a013 6c 2c 20 0a 20 20 69 6e 74 20 2a 61 53 70 61 72  l, .  int *aSpar
3a014 65 0a 29 7b 0a 20 20 69 66 28 20 6e 49 64 78 3e  e.){.  if( nIdx>
3a015 31 20 29 7b 0a 0a 20 20 20 20 69 6e 74 20 69 4c  1 ){..    int iL
3a016 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  eft = 0;.    int
3a017 20 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20   iRight = 0;..  
3a018 20 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20 6e 49    int nLeft = nI
3a019 64 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20 6e 52  dx/2;.    int nR
3a01a 69 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c 65 66  ight = nIdx-nLef
3a01b 74 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c 65 66  t;.    int *aLef
3a01c 74 20 3d 20 61 49 64 78 3b 0a 20 20 20 20 69 6e  t = aIdx;.    in
3a01d 74 20 2a 61 52 69 67 68 74 20 3d 20 26 61 49 64  t *aRight = &aId
3a01e 78 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20 20 53  x[nLeft];..    S
3a01f 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70  ortByDimension(p
3a020 52 74 72 65 65 2c 20 61 4c 65 66 74 2c 20 6e 4c  Rtree, aLeft, nL
3a021 65 66 74 2c 20 69 44 69 6d 2c 20 61 43 65 6c 6c  eft, iDim, aCell
3a022 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 20 20 53  , aSpare);.    S
3a023 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70  ortByDimension(p
3a024 52 74 72 65 65 2c 20 61 52 69 67 68 74 2c 20 6e  Rtree, aRight, n
3a025 52 69 67 68 74 2c 20 69 44 69 6d 2c 20 61 43 65  Right, iDim, aCe
3a026 6c 6c 2c 20 61 53 70 61 72 65 29 3b 0a 0a 20 20  ll, aSpare);..  
3a027 20 20 6d 65 6d 63 70 79 28 61 53 70 61 72 65 2c    memcpy(aSpare,
3a028 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f 66 28 69   aLeft, sizeof(i
3a029 6e 74 29 2a 6e 4c 65 66 74 29 3b 0a 20 20 20 20  nt)*nLeft);.    
3a02a 61 4c 65 66 74 20 3d 20 61 53 70 61 72 65 3b 0a  aLeft = aSpare;.
3a02b 20 20 20 20 77 68 69 6c 65 28 20 69 4c 65 66 74      while( iLeft
3a02c 3c 6e 4c 65 66 74 20 7c 7c 20 69 52 69 67 68 74  <nLeft || iRight
3a02d 3c 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  <nRight ){.     
3a02e 20 64 6f 75 62 6c 65 20 78 6c 65 66 74 31 20 3d   double xleft1 =
3a02f 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 4c   DCOORD(aCell[aL
3a030 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61 43 6f 6f  eft[iLeft]].aCoo
3a031 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20  rd[iDim*2]);.   
3a032 20 20 20 64 6f 75 62 6c 65 20 78 6c 65 66 74 32     double xleft2
3a033 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
3a034 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61 43  aLeft[iLeft]].aC
3a035 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b  oord[iDim*2+1]);
3a036 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72  .      double xr
3a037 69 67 68 74 31 20 3d 20 44 43 4f 4f 52 44 28 61  ight1 = DCOORD(a
3a038 43 65 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67  Cell[aRight[iRig
3a039 68 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ht]].aCoord[iDim
3a03a 2a 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75 62  *2]);.      doub
3a03b 6c 65 20 78 72 69 67 68 74 32 20 3d 20 44 43 4f  le xright2 = DCO
3a03c 4f 52 44 28 61 43 65 6c 6c 5b 61 52 69 67 68 74  ORD(aCell[aRight
3a03d 5b 69 52 69 67 68 74 5d 5d 2e 61 43 6f 6f 72 64  [iRight]].aCoord
3a03e 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20  [iDim*2+1]);.   
3a03f 20 20 20 69 66 28 20 28 69 4c 65 66 74 21 3d 6e     if( (iLeft!=n
3a040 4c 65 66 74 29 20 26 26 20 28 28 69 52 69 67 68  Left) && ((iRigh
3a041 74 3d 3d 6e 52 69 67 68 74 29 0a 20 20 20 20 20  t==nRight).     
3a042 20 20 7c 7c 20 28 78 6c 65 66 74 31 3c 78 72 69    || (xleft1<xri
3a043 67 68 74 31 29 0a 20 20 20 20 20 20 20 7c 7c 20  ght1).       || 
3a044 28 78 6c 65 66 74 31 3d 3d 78 72 69 67 68 74 31  (xleft1==xright1
3a045 20 26 26 20 78 6c 65 66 74 32 3c 78 72 69 67 68   && xleft2<xrigh
3a046 74 32 29 0a 20 20 20 20 20 20 29 29 7b 0a 20 20  t2).      )){.  
3a047 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74        aIdx[iLeft
3a048 2b 69 52 69 67 68 74 5d 20 3d 20 61 4c 65 66 74  +iRight] = aLeft
3a049 5b 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20 20 20  [iLeft];.       
3a04a 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20 20   iLeft++;.      
3a04b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
3a04c 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74  Idx[iLeft+iRight
3a04d 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68  ] = aRight[iRigh
3a04e 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 52 69 67  t];.        iRig
3a04f 68 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ht++;.      }.  
3a050 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f    }..#if 0.    /
3a051 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
3a052 20 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f 0a   sort worked */.
3a053 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20      {.      int 
3a054 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a  jj;.      for(jj
3a055 3d 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b  =1; jj<nIdx; jj+
3a056 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61  +){.        floa
3a057 74 20 78 6c 65 66 74 31 20 3d 20 61 43 65 6c 6c  t xleft1 = aCell
3a058 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f  [aIdx[jj-1]].aCo
3a059 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20 20 20  ord[iDim*2];.   
3a05a 20 20 20 20 20 66 6c 6f 61 74 20 78 6c 65 66 74       float xleft
3a05b 32 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a  2 = aCell[aIdx[j
3a05c 6a 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  j-1]].aCoord[iDi
3a05d 6d 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20  m*2+1];.        
3a05e 66 6c 6f 61 74 20 78 72 69 67 68 74 31 20 3d 20  float xright1 = 
3a05f 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d 2e  aCell[aIdx[jj]].
3a060 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a  aCoord[iDim*2];.
3a061 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78 72          float xr
3a062 69 67 68 74 32 20 3d 20 61 43 65 6c 6c 5b 61 49  ight2 = aCell[aI
3a063 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b 69  dx[jj]].aCoord[i
3a064 44 69 6d 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20  Dim*2+1];.      
3a065 20 20 61 73 73 65 72 74 28 20 78 6c 65 66 74 31    assert( xleft1
3a066 3c 3d 78 72 69 67 68 74 31 20 26 26 20 28 78 6c  <=xright1 && (xl
3a067 65 66 74 31 3c 78 72 69 67 68 74 31 20 7c 7c 20  eft1<xright1 || 
3a068 78 6c 65 66 74 32 3c 3d 78 72 69 67 68 74 32 29  xleft2<=xright2)
3a069 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
3a06a 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a  }.#endif.  }.}..
3a06b 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41  #if VARIANT_RSTA
3a06c 52 54 52 45 45 5f 53 50 4c 49 54 0a 2f 2a 0a 2a  RTREE_SPLIT./*.*
3a06d 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
3a06e 20 6f 66 20 74 68 65 20 52 2a 2d 74 72 65 65 20   of the R*-tree 
3a06f 76 61 72 69 61 6e 74 20 6f 66 20 53 70 6c 69 74  variant of Split
3a070 4e 6f 64 65 20 66 72 6f 6d 20 42 65 63 6b 6d 61  Node from Beckma
3a071 6e 5b 31 39 39 30 5d 2e 0a 2a 2f 0a 73 74 61 74  n[1990]..*/.stat
3a072 69 63 20 69 6e 74 20 73 70 6c 69 74 4e 6f 64 65  ic int splitNode
3a073 53 74 61 72 74 72 65 65 28 0a 20 20 52 74 72 65  Startree(.  Rtre
3a074 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72  e *pRtree,.  Rtr
3a075 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 0a 20  eeCell *aCell,. 
3a076 20 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20 20 52 74   int nCell,.  Rt
3a077 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74 2c 0a  reeNode *pLeft,.
3a078 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 69    RtreeNode *pRi
3a079 67 68 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ght,.  RtreeCell
3a07a 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0a 20 20 52   *pBboxLeft,.  R
3a07b 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 52  treeCell *pBboxR
3a07c 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 2a 2a  ight.){.  int **
3a07d 61 61 53 6f 72 74 65 64 3b 0a 20 20 69 6e 74 20  aaSorted;.  int 
3a07e 2a 61 53 70 61 72 65 3b 0a 20 20 69 6e 74 20 69  *aSpare;.  int i
3a07f 69 3b 0a 0a 20 20 69 6e 74 20 69 42 65 73 74 44  i;..  int iBestD
3a080 69 6d 3b 0a 20 20 69 6e 74 20 69 42 65 73 74 53  im;.  int iBestS
3a081 70 6c 69 74 3b 0a 20 20 66 6c 6f 61 74 20 66 42  plit;.  float fB
3a082 65 73 74 4d 61 72 67 69 6e 3b 0a 0a 20 20 69 6e  estMargin;..  in
3a083 74 20 6e 42 79 74 65 20 3d 20 28 70 52 74 72 65  t nByte = (pRtre
3a084 65 2d 3e 6e 44 69 6d 2b 31 29 2a 28 73 69 7a 65  e->nDim+1)*(size
3a085 6f 66 28 69 6e 74 2a 29 2b 6e 43 65 6c 6c 2a 73  of(int*)+nCell*s
3a086 69 7a 65 6f 66 28 69 6e 74 29 29 3b 0a 0a 20 20  izeof(int));..  
3a087 61 61 53 6f 72 74 65 64 20 3d 20 28 69 6e 74 20  aaSorted = (int 
3a088 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  **)sqlite3_mallo
3a089 63 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20  c(nByte);.  if( 
3a08a 21 61 61 53 6f 72 74 65 64 20 29 7b 0a 20 20 20  !aaSorted ){.   
3a08b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
3a08c 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 61 53 70  OMEM;.  }..  aSp
3a08d 61 72 65 20 3d 20 26 28 28 69 6e 74 20 2a 29 26  are = &((int *)&
3a08e 61 61 53 6f 72 74 65 64 5b 70 52 74 72 65 65 2d  aaSorted[pRtree-
3a08f 3e 6e 44 69 6d 5d 29 5b 70 52 74 72 65 65 2d 3e  >nDim])[pRtree->
3a090 6e 44 69 6d 2a 6e 43 65 6c 6c 5d 3b 0a 20 20 6d  nDim*nCell];.  m
3a091 65 6d 73 65 74 28 61 61 53 6f 72 74 65 64 2c 20  emset(aaSorted, 
3a092 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 66 6f 72  0, nByte);.  for
3a093 28 69 69 3d 30 3b 20 69 69 3c 70 52 74 72 65 65  (ii=0; ii<pRtree
3a094 2d 3e 6e 44 69 6d 3b 20 69 69 2b 2b 29 7b 0a 20  ->nDim; ii++){. 
3a095 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 61     int jj;.    a
3a096 61 53 6f 72 74 65 64 5b 69 69 5d 20 3d 20 26 28  aSorted[ii] = &(
3a097 28 69 6e 74 20 2a 29 26 61 61 53 6f 72 74 65 64  (int *)&aaSorted
3a098 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29 5b  [pRtree->nDim])[
3a099 69 69 2a 6e 43 65 6c 6c 5d 3b 0a 20 20 20 20 66  ii*nCell];.    f
3a09a 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 6e 43 65 6c  or(jj=0; jj<nCel
3a09b 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  l; jj++){.      
3a09c 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6a 6a 5d  aaSorted[ii][jj]
3a09d 20 3d 20 6a 6a 3b 0a 20 20 20 20 7d 0a 20 20 20   = jj;.    }.   
3a09e 20 53 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e   SortByDimension
3a09f 28 70 52 74 72 65 65 2c 20 61 61 53 6f 72 74 65  (pRtree, aaSorte
3a0a0 64 5b 69 69 5d 2c 20 6e 43 65 6c 6c 2c 20 69 69  d[ii], nCell, ii
3a0a1 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65 29  , aCell, aSpare)
3a0a2 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 69 3d  ;.  }..  for(ii=
3a0a3 30 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44  0; ii<pRtree->nD
3a0a4 69 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 66  im; ii++){.    f
3a0a5 6c 6f 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e  loat margin = 0.
3a0a6 30 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 42 65  0;.    float fBe
3a0a7 73 74 4f 76 65 72 6c 61 70 3b 0a 20 20 20 20 66  stOverlap;.    f
3a0a8 6c 6f 61 74 20 66 42 65 73 74 41 72 65 61 3b 0a  loat fBestArea;.
3a0a9 20 20 20 20 69 6e 74 20 69 42 65 73 74 4c 65 66      int iBestLef
3a0aa 74 3b 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66 74  t;.    int nLeft
3a0ab 3b 0a 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 20  ;..    for(.    
3a0ac 20 20 6e 4c 65 66 74 3d 52 54 52 45 45 5f 4d 49    nLeft=RTREE_MI
3a0ad 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 3b 20  NCELLS(pRtree); 
3a0ae 0a 20 20 20 20 20 20 6e 4c 65 66 74 3c 3d 28 6e  .      nLeft<=(n
3a0af 43 65 6c 6c 2d 52 54 52 45 45 5f 4d 49 4e 43 45  Cell-RTREE_MINCE
3a0b0 4c 4c 53 28 70 52 74 72 65 65 29 29 3b 20 0a 20  LLS(pRtree)); . 
3a0b1 20 20 20 20 20 6e 4c 65 66 74 2b 2b 0a 20 20 20       nLeft++.   
3a0b2 20 29 7b 0a 20 20 20 20 20 20 52 74 72 65 65 43   ){.      RtreeC
3a0b3 65 6c 6c 20 6c 65 66 74 3b 0a 20 20 20 20 20 20  ell left;.      
3a0b4 52 74 72 65 65 43 65 6c 6c 20 72 69 67 68 74 3b  RtreeCell right;
3a0b5 0a 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b 0a 20  .      int kk;. 
3a0b6 20 20 20 20 20 66 6c 6f 61 74 20 6f 76 65 72 6c       float overl
3a0b7 61 70 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  ap;.      float 
3a0b8 61 72 65 61 3b 0a 0a 20 20 20 20 20 20 6d 65 6d  area;..      mem
3a0b9 63 70 79 28 26 6c 65 66 74 2c 20 26 61 43 65 6c  cpy(&left, &aCel
3a0ba 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 30  l[aaSorted[ii][0
3a0bb 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  ]], sizeof(Rtree
3a0bc 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20 20 6d 65  Cell));.      me
3a0bd 6d 63 70 79 28 26 72 69 67 68 74 2c 20 26 61 43  mcpy(&right, &aC
3a0be 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d  ell[aaSorted[ii]
3a0bf 5b 6e 43 65 6c 6c 2d 31 5d 5d 2c 20 73 69 7a 65  [nCell-1]], size
3a0c0 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a  of(RtreeCell));.
3a0c1 20 20 20 20 20 20 66 6f 72 28 6b 6b 3d 31 3b 20        for(kk=1; 
3a0c2 6b 6b 3c 28 6e 43 65 6c 6c 2d 31 29 3b 20 6b 6b  kk<(nCell-1); kk
3a0c3 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
3a0c4 20 6b 6b 3c 6e 4c 65 66 74 20 29 7b 0a 20 20 20   kk<nLeft ){.   
3a0c5 20 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e         cellUnion
3a0c6 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74 2c 20  (pRtree, &left, 
3a0c7 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b  &aCell[aaSorted[
3a0c8 69 69 5d 5b 6b 6b 5d 5d 29 3b 0a 20 20 20 20 20  ii][kk]]);.     
3a0c9 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3a0ca 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
3a0cb 74 72 65 65 2c 20 26 72 69 67 68 74 2c 20 26 61  tree, &right, &a
3a0cc 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69  Cell[aaSorted[ii
3a0cd 5d 5b 6b 6b 5d 5d 29 3b 0a 20 20 20 20 20 20 20  ][kk]]);.       
3a0ce 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
3a0cf 20 6d 61 72 67 69 6e 20 2b 3d 20 63 65 6c 6c 4d   margin += cellM
3a0d0 61 72 67 69 6e 28 70 52 74 72 65 65 2c 20 26 6c  argin(pRtree, &l
3a0d1 65 66 74 29 3b 0a 20 20 20 20 20 20 6d 61 72 67  eft);.      marg
3a0d2 69 6e 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e  in += cellMargin
3a0d3 28 70 52 74 72 65 65 2c 20 26 72 69 67 68 74 29  (pRtree, &right)
3a0d4 3b 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20  ;.      overlap 
3a0d5 3d 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52  = cellOverlap(pR
3a0d6 74 72 65 65 2c 20 26 6c 65 66 74 2c 20 26 72 69  tree, &left, &ri
3a0d7 67 68 74 2c 20 31 2c 20 2d 31 29 3b 0a 20 20 20  ght, 1, -1);.   
3a0d8 20 20 20 61 72 65 61 20 3d 20 63 65 6c 6c 41 72     area = cellAr
3a0d9 65 61 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74  ea(pRtree, &left
3a0da 29 20 2b 20 63 65 6c 6c 41 72 65 61 28 70 52 74  ) + cellArea(pRt
3a0db 72 65 65 2c 20 26 72 69 67 68 74 29 3b 0a 20 20  ree, &right);.  
3a0dc 20 20 20 20 69 66 28 20 28 6e 4c 65 66 74 3d 3d      if( (nLeft==
3a0dd 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
3a0de 52 74 72 65 65 29 29 0a 20 20 20 20 20 20 20 7c  Rtree)).       |
3a0df 7c 20 28 6f 76 65 72 6c 61 70 3c 66 42 65 73 74  | (overlap<fBest
3a0e0 4f 76 65 72 6c 61 70 29 0a 20 20 20 20 20 20 20  Overlap).       
3a0e1 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 42 65  || (overlap==fBe
3a0e2 73 74 4f 76 65 72 6c 61 70 20 26 26 20 61 72 65  stOverlap && are
3a0e3 61 3c 66 42 65 73 74 41 72 65 61 29 0a 20 20 20  a<fBestArea).   
3a0e4 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 42     ){.        iB
3a0e5 65 73 74 4c 65 66 74 20 3d 20 6e 4c 65 66 74 3b  estLeft = nLeft;
3a0e6 0a 20 20 20 20 20 20 20 20 66 42 65 73 74 4f 76  .        fBestOv
3a0e7 65 72 6c 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b  erlap = overlap;
3a0e8 0a 20 20 20 20 20 20 20 20 66 42 65 73 74 41 72  .        fBestAr
3a0e9 65 61 20 3d 20 61 72 65 61 3b 0a 20 20 20 20 20  ea = area;.     
3a0ea 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66   }.    }..    if
3a0eb 28 20 69 69 3d 3d 30 20 7c 7c 20 6d 61 72 67 69  ( ii==0 || margi
3a0ec 6e 3c 66 42 65 73 74 4d 61 72 67 69 6e 20 29 7b  n<fBestMargin ){
3a0ed 0a 20 20 20 20 20 20 69 42 65 73 74 44 69 6d 20  .      iBestDim 
3a0ee 3d 20 69 69 3b 0a 20 20 20 20 20 20 66 42 65 73  = ii;.      fBes
3a0ef 74 4d 61 72 67 69 6e 20 3d 20 6d 61 72 67 69 6e  tMargin = margin
3a0f0 3b 0a 20 20 20 20 20 20 69 42 65 73 74 53 70 6c  ;.      iBestSpl
3a0f1 69 74 20 3d 20 69 42 65 73 74 4c 65 66 74 3b 0a  it = iBestLeft;.
3a0f2 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6d 65 6d      }.  }..  mem
3a0f3 63 70 79 28 70 42 62 6f 78 4c 65 66 74 2c 20 26  cpy(pBboxLeft, &
3a0f4 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69  aCell[aaSorted[i
3a0f5 42 65 73 74 44 69 6d 5d 5b 30 5d 5d 2c 20 73 69  BestDim][0]], si
3a0f6 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
3a0f7 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78  ;.  memcpy(pBbox
3a0f8 52 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61  Right, &aCell[aa
3a0f9 53 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d  Sorted[iBestDim]
3a0fa 5b 69 42 65 73 74 53 70 6c 69 74 5d 5d 2c 20 73  [iBestSplit]], s
3a0fb 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
3a0fc 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
3a0fd 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a  i<nCell; ii++){.
3a0fe 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
3a0ff 54 61 72 67 65 74 20 3d 20 28 69 69 3c 69 42 65  Target = (ii<iBe
3a100 73 74 53 70 6c 69 74 29 3f 70 4c 65 66 74 3a 70  stSplit)?pLeft:p
3a101 52 69 67 68 74 3b 0a 20 20 20 20 52 74 72 65 65  Right;.    Rtree
3a102 43 65 6c 6c 20 2a 70 42 62 6f 78 20 3d 20 28 69  Cell *pBbox = (i
3a103 69 3c 69 42 65 73 74 53 70 6c 69 74 29 3f 70 42  i<iBestSplit)?pB
3a104 62 6f 78 4c 65 66 74 3a 70 42 62 6f 78 52 69 67  boxLeft:pBboxRig
3a105 68 74 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c  ht;.    RtreeCel
3a106 6c 20 2a 70 43 65 6c 6c 20 3d 20 26 61 43 65 6c  l *pCell = &aCel
3a107 6c 5b 61 61 53 6f 72 74 65 64 5b 69 42 65 73 74  l[aaSorted[iBest
3a108 44 69 6d 5d 5b 69 69 5d 5d 3b 0a 20 20 20 20 6e  Dim][ii]];.    n
3a109 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  odeInsertCell(pR
3a10a 74 72 65 65 2c 20 70 54 61 72 67 65 74 2c 20 70  tree, pTarget, p
3a10b 43 65 6c 6c 29 3b 0a 20 20 20 20 63 65 6c 6c 55  Cell);.    cellU
3a10c 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62  nion(pRtree, pBb
3a10d 6f 78 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 7d 0a  ox, pCell);.  }.
3a10e 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3a10f 61 61 53 6f 72 74 65 64 29 3b 0a 20 20 72 65 74  aaSorted);.  ret
3a110 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
3a111 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 56 41 52  .#endif..#if VAR
3a112 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 53 50 4c  IANT_GUTTMAN_SPL
3a113 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  IT./*.** Impleme
3a114 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 72  ntation of the r
3a115 65 67 75 6c 61 72 20 52 2d 74 72 65 65 20 53 70  egular R-tree Sp
3a116 6c 69 74 4e 6f 64 65 20 66 72 6f 6d 20 47 75 74  litNode from Gut
3a117 74 6d 61 6e 5b 31 39 38 34 5d 2e 0a 2a 2f 0a 73  tman[1984]..*/.s
3a118 74 61 74 69 63 20 69 6e 74 20 73 70 6c 69 74 4e  tatic int splitN
3a119 6f 64 65 47 75 74 74 6d 61 6e 28 0a 20 20 52 74  odeGuttman(.  Rt
3a11a 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52  ree *pRtree,.  R
3a11b 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c  treeCell *aCell,
3a11c 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20 20  .  int nCell,.  
3a11d 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74  RtreeNode *pLeft
3a11e 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ,.  RtreeNode *p
3a11f 52 69 67 68 74 2c 0a 20 20 52 74 72 65 65 43 65  Right,.  RtreeCe
3a120 6c 6c 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0a 20  ll *pBboxLeft,. 
3a121 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f   RtreeCell *pBbo
3a122 78 52 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20  xRight.){.  int 
3a123 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20  iLeftSeed = 0;. 
3a124 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64 20   int iRightSeed 
3a125 3d 20 31 3b 0a 20 20 69 6e 74 20 2a 61 69 55 73  = 1;.  int *aiUs
3a126 65 64 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  ed;.  int i;..  
3a127 61 69 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33  aiUsed = sqlite3
3a128 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 69  _malloc(sizeof(i
3a129 6e 74 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20 69 66  nt)*nCell);.  if
3a12a 28 20 21 61 69 55 73 65 64 20 29 7b 0a 20 20 20  ( !aiUsed ){.   
3a12b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
3a12c 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  OMEM;.  }.  mems
3a12d 65 74 28 61 69 55 73 65 64 2c 20 30 2c 20 73 69  et(aiUsed, 0, si
3a12e 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29  zeof(int)*nCell)
3a12f 3b 0a 0a 20 20 50 69 63 6b 53 65 65 64 73 28 70  ;..  PickSeeds(p
3a130 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43  Rtree, aCell, nC
3a131 65 6c 6c 2c 20 26 69 4c 65 66 74 53 65 65 64 2c  ell, &iLeftSeed,
3a132 20 26 69 52 69 67 68 74 53 65 65 64 29 3b 0a 0a   &iRightSeed);..
3a133 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 4c 65    memcpy(pBboxLe
3a134 66 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65 66 74  ft, &aCell[iLeft
3a135 53 65 65 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74  Seed], sizeof(Rt
3a136 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d  reeCell));.  mem
3a137 63 70 79 28 70 42 62 6f 78 52 69 67 68 74 2c 20  cpy(pBboxRight, 
3a138 26 61 43 65 6c 6c 5b 69 52 69 67 68 74 53 65 65  &aCell[iRightSee
3a139 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  d], sizeof(Rtree
3a13a 43 65 6c 6c 29 29 3b 0a 20 20 6e 6f 64 65 49 6e  Cell));.  nodeIn
3a13b 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
3a13c 20 70 4c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 69   pLeft, &aCell[i
3a13d 4c 65 66 74 53 65 65 64 5d 29 3b 0a 20 20 6e 6f  LeftSeed]);.  no
3a13e 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
3a13f 72 65 65 2c 20 70 52 69 67 68 74 2c 20 26 61 43  ree, pRight, &aC
3a140 65 6c 6c 5b 69 52 69 67 68 74 53 65 65 64 5d 29  ell[iRightSeed])
3a141 3b 0a 20 20 61 69 55 73 65 64 5b 69 4c 65 66 74  ;.  aiUsed[iLeft
3a142 53 65 65 64 5d 20 3d 20 31 3b 0a 20 20 61 69 55  Seed] = 1;.  aiU
3a143 73 65 64 5b 69 52 69 67 68 74 53 65 65 64 5d 20  sed[iRightSeed] 
3a144 3d 20 31 3b 0a 0a 20 20 66 6f 72 28 69 3d 6e 43  = 1;..  for(i=nC
3a145 65 6c 6c 2d 32 3b 20 69 3e 30 3b 20 69 2d 2d 29  ell-2; i>0; i--)
3a146 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  {.    RtreeCell 
3a147 2a 70 4e 65 78 74 3b 0a 20 20 20 20 70 4e 65 78  *pNext;.    pNex
3a148 74 20 3d 20 50 69 63 6b 4e 65 78 74 28 70 52 74  t = PickNext(pRt
3a149 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c  ree, aCell, nCel
3a14a 6c 2c 20 70 42 62 6f 78 4c 65 66 74 2c 20 70 42  l, pBboxLeft, pB
3a14b 62 6f 78 52 69 67 68 74 2c 20 61 69 55 73 65 64  boxRight, aiUsed
3a14c 29 3b 0a 20 20 20 20 66 6c 6f 61 74 20 64 69 66  );.    float dif
3a14d 66 20 3d 20 20 0a 20 20 20 20 20 20 63 65 6c 6c  f =  .      cell
3a14e 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70  Growth(pRtree, p
3a14f 42 62 6f 78 4c 65 66 74 2c 20 70 4e 65 78 74 29  BboxLeft, pNext)
3a150 20 2d 20 0a 20 20 20 20 20 20 63 65 6c 6c 47 72   - .      cellGr
3a151 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 42 62  owth(pRtree, pBb
3a152 6f 78 52 69 67 68 74 2c 20 70 4e 65 78 74 29 0a  oxRight, pNext).
3a153 20 20 20 20 3b 0a 20 20 20 20 69 66 28 20 28 52      ;.    if( (R
3a154 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52  TREE_MINCELLS(pR
3a155 74 72 65 65 29 2d 4e 43 45 4c 4c 28 70 52 69 67  tree)-NCELL(pRig
3a156 68 74 29 3d 3d 69 29 0a 20 20 20 20 20 7c 7c 20  ht)==i).     || 
3a157 28 64 69 66 66 3e 30 2e 30 20 26 26 20 28 52 54  (diff>0.0 && (RT
3a158 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
3a159 72 65 65 29 2d 4e 43 45 4c 4c 28 70 4c 65 66 74  ree)-NCELL(pLeft
3a15a 29 21 3d 69 29 29 0a 20 20 20 20 29 7b 0a 20 20  )!=i)).    ){.  
3a15b 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65      nodeInsertCe
3a15c 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67 68  ll(pRtree, pRigh
3a15d 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 20  t, pNext);.     
3a15e 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
3a15f 65 2c 20 70 42 62 6f 78 52 69 67 68 74 2c 20 70  e, pBboxRight, p
3a160 4e 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Next);.    }else
3a161 7b 0a 20 20 20 20 20 20 6e 6f 64 65 49 6e 73 65  {.      nodeInse
3a162 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  rtCell(pRtree, p
3a163 4c 65 66 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20  Left, pNext);.  
3a164 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
3a165 74 72 65 65 2c 20 70 42 62 6f 78 4c 65 66 74 2c  tree, pBboxLeft,
3a166 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20   pNext);.    }. 
3a167 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   }..  sqlite3_fr
3a168 65 65 28 61 69 55 73 65 64 29 3b 0a 20 20 72 65  ee(aiUsed);.  re
3a169 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3a16a 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69 63  }.#endif..static
3a16b 20 69 6e 74 20 75 70 64 61 74 65 4d 61 70 70 69   int updateMappi
3a16c 6e 67 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ng(.  Rtree *pRt
3a16d 72 65 65 2c 20 0a 20 20 69 36 34 20 69 52 6f 77  ree, .  i64 iRow
3a16e 69 64 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65  id, .  RtreeNode
3a16f 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20   *pNode, .  int 
3a170 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74  iHeight.){.  int
3a171 20 28 2a 78 53 65 74 4d 61 70 70 69 6e 67 29 28   (*xSetMapping)(
3a172 52 74 72 65 65 20 2a 2c 20 73 71 6c 69 74 65 33  Rtree *, sqlite3
3a173 5f 69 6e 74 36 34 2c 20 73 71 6c 69 74 65 33 5f  _int64, sqlite3_
3a174 69 6e 74 36 34 29 3b 0a 20 20 78 53 65 74 4d 61  int64);.  xSetMa
3a175 70 70 69 6e 67 20 3d 20 28 28 69 48 65 69 67 68  pping = ((iHeigh
3a176 74 3d 3d 30 29 3f 72 6f 77 69 64 57 72 69 74 65  t==0)?rowidWrite
3a177 3a 70 61 72 65 6e 74 57 72 69 74 65 29 3b 0a 20  :parentWrite);. 
3a178 20 69 66 28 20 69 48 65 69 67 68 74 3e 30 20 29   if( iHeight>0 )
3a179 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  {.    RtreeNode 
3a17a 2a 70 43 68 69 6c 64 20 3d 20 6e 6f 64 65 48 61  *pChild = nodeHa
3a17b 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c  shLookup(pRtree,
3a17c 20 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 69 66   iRowid);.    if
3a17d 28 20 70 43 68 69 6c 64 20 29 7b 0a 20 20 20 20  ( pChild ){.    
3a17e 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
3a17f 74 72 65 65 2c 20 70 43 68 69 6c 64 2d 3e 70 50  tree, pChild->pP
3a180 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 6e 6f  arent);.      no
3a181 64 65 52 65 66 65 72 65 6e 63 65 28 70 4e 6f 64  deReference(pNod
3a182 65 29 3b 0a 20 20 20 20 20 20 70 43 68 69 6c 64  e);.      pChild
3a183 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64  ->pParent = pNod
3a184 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  e;.    }.  }.  r
3a185 65 74 75 72 6e 20 78 53 65 74 4d 61 70 70 69 6e  eturn xSetMappin
3a186 67 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64  g(pRtree, iRowid
3a187 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  , pNode->iNode);
3a188 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 53  .}..static int S
3a189 70 6c 69 74 4e 6f 64 65 28 0a 20 20 52 74 72 65  plitNode(.  Rtre
3a18a 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72  e *pRtree,.  Rtr
3a18b 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a 20  eeNode *pNode,. 
3a18c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
3a18d 6c 2c 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74  l,.  int iHeight
3a18e 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  .){.  int i;.  i
3a18f 6e 74 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68  nt newCellIsRigh
3a190 74 20 3d 20 30 3b 0a 0a 20 20 69 6e 74 20 72 63  t = 0;..  int rc
3a191 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
3a192 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c  int nCell = NCEL
3a193 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 52 74 72 65  L(pNode);.  Rtre
3a194 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20 20  eCell *aCell;.  
3a195 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 0a 20 20  int *aiUsed;..  
3a196 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74  RtreeNode *pLeft
3a197 20 3d 20 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64   = 0;.  RtreeNod
3a198 65 20 2a 70 52 69 67 68 74 20 3d 20 30 3b 0a 0a  e *pRight = 0;..
3a199 20 20 52 74 72 65 65 43 65 6c 6c 20 6c 65 66 74    RtreeCell left
3a19a 62 62 6f 78 3b 0a 20 20 52 74 72 65 65 43 65 6c  bbox;.  RtreeCel
3a19b 6c 20 72 69 67 68 74 62 62 6f 78 3b 0a 0a 20 20  l rightbbox;..  
3a19c 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 20 61  /* Allocate an a
3a19d 72 72 61 79 20 61 6e 64 20 70 6f 70 75 6c 61 74  rray and populat
3a19e 65 20 69 74 20 77 69 74 68 20 61 20 63 6f 70 79  e it with a copy
3a19f 20 6f 66 20 70 43 65 6c 6c 20 61 6e 64 20 0a 20   of pCell and . 
3a1a0 20 2a 2a 20 61 6c 6c 20 63 65 6c 6c 73 20 66 72   ** all cells fr
3a1a1 6f 6d 20 6e 6f 64 65 20 70 4c 65 66 74 2e 20 54  om node pLeft. T
3a1a2 68 65 6e 20 7a 65 72 6f 20 74 68 65 20 6f 72 69  hen zero the ori
3a1a3 67 69 6e 61 6c 20 6e 6f 64 65 2e 0a 20 20 2a 2f  ginal node..  */
3a1a4 0a 20 20 61 43 65 6c 6c 20 3d 20 73 71 6c 69 74  .  aCell = sqlit
3a1a5 65 33 5f 6d 61 6c 6c 6f 63 28 28 73 69 7a 65 6f  e3_malloc((sizeo
3a1a6 66 28 52 74 72 65 65 43 65 6c 6c 29 2b 73 69 7a  f(RtreeCell)+siz
3a1a7 65 6f 66 28 69 6e 74 29 29 2a 28 6e 43 65 6c 6c  eof(int))*(nCell
3a1a8 2b 31 29 29 3b 0a 20 20 69 66 28 20 21 61 43 65  +1));.  if( !aCe
3a1a9 6c 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  ll ){.    rc = S
3a1aa 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
3a1ab 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
3a1ac 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 69 55 73 65  out;.  }.  aiUse
3a1ad 64 20 3d 20 28 69 6e 74 20 2a 29 26 61 43 65 6c  d = (int *)&aCel
3a1ae 6c 5b 6e 43 65 6c 6c 2b 31 5d 3b 0a 20 20 6d 65  l[nCell+1];.  me
3a1af 6d 73 65 74 28 61 69 55 73 65 64 2c 20 30 2c 20  mset(aiUsed, 0, 
3a1b0 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 43 65  sizeof(int)*(nCe
3a1b1 6c 6c 2b 31 29 29 3b 0a 20 20 66 6f 72 28 69 3d  ll+1));.  for(i=
3a1b2 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
3a1b3 7b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c  {.    nodeGetCel
3a1b4 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
3a1b5 20 69 2c 20 26 61 43 65 6c 6c 5b 69 5d 29 3b 0a   i, &aCell[i]);.
3a1b6 20 20 7d 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70    }.  nodeZero(p
3a1b7 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
3a1b8 20 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c 5b 6e   memcpy(&aCell[n
3a1b9 43 65 6c 6c 5d 2c 20 70 43 65 6c 6c 2c 20 73 69  Cell], pCell, si
3a1ba 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
3a1bb 3b 0a 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 0a 20 20  ;.  nCell++;..  
3a1bc 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  if( pNode->iNode
3a1bd 3d 3d 31 20 29 7b 0a 20 20 20 20 70 52 69 67 68  ==1 ){.    pRigh
3a1be 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52 74 72  t = nodeNew(pRtr
3a1bf 65 65 2c 20 70 4e 6f 64 65 2c 20 31 29 3b 0a 20  ee, pNode, 1);. 
3a1c0 20 20 20 70 4c 65 66 74 20 3d 20 6e 6f 64 65 4e     pLeft = nodeN
3a1c1 65 77 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ew(pRtree, pNode
3a1c2 2c 20 31 29 3b 0a 20 20 20 20 70 52 74 72 65 65  , 1);.    pRtree
3a1c3 2d 3e 69 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20  ->iDepth++;.    
3a1c4 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d  pNode->isDirty =
3a1c5 20 31 3b 0a 20 20 20 20 77 72 69 74 65 49 6e 74   1;.    writeInt
3a1c6 31 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c  16(pNode->zData,
3a1c7 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 29   pRtree->iDepth)
3a1c8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
3a1c9 4c 65 66 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  Left = pNode;.  
3a1ca 20 20 70 52 69 67 68 74 20 3d 20 6e 6f 64 65 4e    pRight = nodeN
3a1cb 65 77 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  ew(pRtree, pLeft
3a1cc 2d 3e 70 50 61 72 65 6e 74 2c 20 31 29 3b 0a 20  ->pParent, 1);. 
3a1cd 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65     nodeReference
3a1ce 28 70 4c 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20  (pLeft);.  }..  
3a1cf 69 66 28 20 21 70 4c 65 66 74 20 7c 7c 20 21 70  if( !pLeft || !p
3a1d0 52 69 67 68 74 20 29 7b 0a 20 20 20 20 72 63 20  Right ){.    rc 
3a1d1 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
3a1d2 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f      goto splitno
3a1d3 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 6d  de_out;.  }..  m
3a1d4 65 6d 73 65 74 28 70 4c 65 66 74 2d 3e 7a 44 61  emset(pLeft->zDa
3a1d5 74 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69  ta, 0, pRtree->i
3a1d6 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 6d 65 6d  NodeSize);.  mem
3a1d7 73 65 74 28 70 52 69 67 68 74 2d 3e 7a 44 61 74  set(pRight->zDat
3a1d8 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e  a, 0, pRtree->iN
3a1d9 6f 64 65 53 69 7a 65 29 3b 0a 0a 20 20 72 63 20  odeSize);..  rc 
3a1da 3d 20 41 73 73 69 67 6e 43 65 6c 6c 73 28 70 52  = AssignCells(pR
3a1db 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65  tree, aCell, nCe
3a1dc 6c 6c 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  ll, pLeft, pRigh
3a1dd 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20 26 72  t, &leftbbox, &r
3a1de 69 67 68 74 62 62 6f 78 29 3b 0a 20 20 69 66 28  ightbbox);.  if(
3a1df 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3a1e0 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74  {.    goto split
3a1e1 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  node_out;.  }.. 
3a1e2 20 2f 2a 20 45 6e 73 75 72 65 20 62 6f 74 68 20   /* Ensure both 
3a1e3 63 68 69 6c 64 20 6e 6f 64 65 73 20 68 61 76 65  child nodes have
3a1e4 20 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20 61 73   node numbers as
3a1e5 73 69 67 6e 65 64 20 74 6f 20 74 68 65 6d 2e 20  signed to them. 
3a1e6 2a 2f 0a 20 20 69 66 28 20 28 30 3d 3d 70 52 69  */.  if( (0==pRi
3a1e7 67 68 74 2d 3e 69 4e 6f 64 65 20 26 26 20 53 51  ght->iNode && SQ
3a1e8 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e  LITE_OK!=(rc = n
3a1e9 6f 64 65 57 72 69 74 65 28 70 52 74 72 65 65 2c  odeWrite(pRtree,
3a1ea 20 70 52 69 67 68 74 29 29 29 0a 20 20 20 7c 7c   pRight))).   ||
3a1eb 20 28 30 3d 3d 70 4c 65 66 74 2d 3e 69 4e 6f 64   (0==pLeft->iNod
3a1ec 65 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d  e && SQLITE_OK!=
3a1ed 28 72 63 20 3d 20 6e 6f 64 65 57 72 69 74 65 28  (rc = nodeWrite(
3a1ee 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 29 29  pRtree, pLeft)))
3a1ef 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73  .  ){.    goto s
3a1f0 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
3a1f1 7d 0a 0a 20 20 72 69 67 68 74 62 62 6f 78 2e 69  }..  rightbbox.i
3a1f2 52 6f 77 69 64 20 3d 20 70 52 69 67 68 74 2d 3e  Rowid = pRight->
3a1f3 69 4e 6f 64 65 3b 0a 20 20 6c 65 66 74 62 62 6f  iNode;.  leftbbo
3a1f4 78 2e 69 52 6f 77 69 64 20 3d 20 70 4c 65 66 74  x.iRowid = pLeft
3a1f5 2d 3e 69 4e 6f 64 65 3b 0a 0a 20 20 69 66 28 20  ->iNode;..  if( 
3a1f6 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20  pNode->iNode==1 
3a1f7 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 74 72 65  ){.    rc = rtre
3a1f8 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
3a1f9 65 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61 72 65  ee, pLeft->pPare
3a1fa 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20 69  nt, &leftbbox, i
3a1fb 48 65 69 67 68 74 2b 31 29 3b 0a 20 20 20 20 69  Height+1);.    i
3a1fc 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3a1fd 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73   ){.      goto s
3a1fe 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
3a1ff 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
3a200 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
3a201 65 6e 74 20 3d 20 70 4c 65 66 74 2d 3e 70 50 61  ent = pLeft->pPa
3a202 72 65 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 43  rent;.    int iC
3a203 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74  ell = nodeParent
3a204 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4c  Index(pRtree, pL
3a205 65 66 74 29 3b 0a 20 20 20 20 6e 6f 64 65 4f 76  eft);.    nodeOv
3a206 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74 72  erwriteCell(pRtr
3a207 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26 6c 65  ee, pParent, &le
3a208 66 74 62 62 6f 78 2c 20 69 43 65 6c 6c 29 3b 0a  ftbbox, iCell);.
3a209 20 20 20 20 41 64 6a 75 73 74 54 72 65 65 28 70      AdjustTree(p
3a20a 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20  Rtree, pParent, 
3a20b 26 6c 65 66 74 62 62 6f 78 29 3b 0a 20 20 7d 0a  &leftbbox);.  }.
3a20c 20 20 69 66 28 20 28 72 63 20 3d 20 72 74 72 65    if( (rc = rtre
3a20d 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
3a20e 65 65 2c 20 70 52 69 67 68 74 2d 3e 70 50 61 72  ee, pRight->pPar
3a20f 65 6e 74 2c 20 26 72 69 67 68 74 62 62 6f 78 2c  ent, &rightbbox,
3a210 20 69 48 65 69 67 68 74 2b 31 29 29 20 29 7b 0a   iHeight+1)) ){.
3a211 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f      goto splitno
3a212 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 66  de_out;.  }..  f
3a213 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c 28  or(i=0; i<NCELL(
3a214 70 52 69 67 68 74 29 3b 20 69 2b 2b 29 7b 0a 20  pRight); i++){. 
3a215 20 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d 20     i64 iRowid = 
3a216 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74  nodeGetRowid(pRt
3a217 72 65 65 2c 20 70 52 69 67 68 74 2c 20 69 29 3b  ree, pRight, i);
3a218 0a 20 20 20 20 72 63 20 3d 20 75 70 64 61 74 65  .    rc = update
3a219 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20  Mapping(pRtree, 
3a21a 69 52 6f 77 69 64 2c 20 70 52 69 67 68 74 2c 20  iRowid, pRight, 
3a21b 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 69 66  iHeight);.    if
3a21c 28 20 69 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d  ( iRowid==pCell-
3a21d 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  >iRowid ){.     
3a21e 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68 74 20   newCellIsRight 
3a21f 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 1;.    }.    i
3a220 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3a221 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73   ){.      goto s
3a222 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
3a223 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e    }.  }.  if( pN
3a224 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b  ode->iNode==1 ){
3a225 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
3a226 4e 43 45 4c 4c 28 70 4c 65 66 74 29 3b 20 69 2b  NCELL(pLeft); i+
3a227 2b 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69 52  +){.      i64 iR
3a228 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f  owid = nodeGetRo
3a229 77 69 64 28 70 52 74 72 65 65 2c 20 70 4c 65 66  wid(pRtree, pLef
3a22a 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 72 63 20  t, i);.      rc 
3a22b 3d 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28  = updateMapping(
3a22c 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20  pRtree, iRowid, 
3a22d 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74 29 3b  pLeft, iHeight);
3a22e 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
3a22f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3a230 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f      goto splitno
3a231 64 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  de_out;.      }.
3a232 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
3a233 28 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68 74  ( newCellIsRight
3a234 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
3a235 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28 70 52  updateMapping(pR
3a236 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f  tree, pCell->iRo
3a237 77 69 64 2c 20 70 4c 65 66 74 2c 20 69 48 65 69  wid, pLeft, iHei
3a238 67 68 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  ght);.  }..  if(
3a239 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3a23a 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52  {.    rc = nodeR
3a23b 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
3a23c 52 69 67 68 74 29 3b 0a 20 20 20 20 70 52 69 67  Right);.    pRig
3a23d 68 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  ht = 0;.  }.  if
3a23e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3a23f 29 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65  ){.    rc = node
3a240 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
3a241 70 4c 65 66 74 29 3b 0a 20 20 20 20 70 4c 65 66  pLeft);.    pLef
3a242 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 73 70 6c 69  t = 0;.  }..spli
3a243 74 6e 6f 64 65 5f 6f 75 74 3a 0a 20 20 6e 6f 64  tnode_out:.  nod
3a244 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
3a245 20 70 52 69 67 68 74 29 3b 0a 20 20 6e 6f 64 65   pRight);.  node
3a246 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
3a247 70 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69 74 65  pLeft);.  sqlite
3a248 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b 0a 20  3_free(aCell);. 
3a249 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73   return rc;.}..s
3a24a 74 61 74 69 63 20 69 6e 74 20 66 69 78 4c 65 61  tatic int fixLea
3a24b 66 50 61 72 65 6e 74 28 52 74 72 65 65 20 2a 70  fParent(Rtree *p
3a24c 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
3a24d 20 2a 70 4c 65 61 66 29 7b 0a 20 20 69 6e 74 20   *pLeaf){.  int 
3a24e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
3a24f 20 20 69 66 28 20 70 4c 65 61 66 2d 3e 69 4e 6f    if( pLeaf->iNo
3a250 64 65 21 3d 31 20 26 26 20 70 4c 65 61 66 2d 3e  de!=1 && pLeaf->
3a251 70 50 61 72 65 6e 74 3d 3d 30 20 29 7b 0a 20 20  pParent==0 ){.  
3a252 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
3a253 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65  nt64(pRtree->pRe
3a254 61 64 50 61 72 65 6e 74 2c 20 31 2c 20 70 4c 65  adParent, 1, pLe
3a255 61 66 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  af->iNode);.    
3a256 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74 65 70  if( sqlite3_step
3a257 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61  (pRtree->pReadPa
3a258 72 65 6e 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  rent)==SQLITE_RO
3a259 57 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69  W ){.      i64 i
3a25a 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f 63  Node = sqlite3_c
3a25b 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72  olumn_int64(pRtr
3a25c 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 2c  ee->pReadParent,
3a25d 20 30 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20   0);.      rc = 
3a25e 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
3a25f 65 65 2c 20 69 4e 6f 64 65 2c 20 30 2c 20 26 70  ee, iNode, 0, &p
3a260 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  Leaf->pParent);.
3a261 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3a262 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
3a263 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  OR;.    }.    sq
3a264 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
3a265 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 29  ee->pReadParent)
3a266 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
3a267 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
3a268 20 72 63 20 3d 20 66 69 78 4c 65 61 66 50 61 72   rc = fixLeafPar
3a269 65 6e 74 28 70 52 74 72 65 65 2c 20 70 4c 65 61  ent(pRtree, pLea
3a26a 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20  f->pParent);.   
3a26b 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
3a26c 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  rc;.}..static in
3a26d 74 20 64 65 6c 65 74 65 43 65 6c 6c 28 52 74 72  t deleteCell(Rtr
3a26e 65 65 20 2a 2c 20 52 74 72 65 65 4e 6f 64 65 20  ee *, RtreeNode 
3a26f 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 73  *, int, int);..s
3a270 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f 76 65  tatic int remove
3a271 4e 6f 64 65 28 52 74 72 65 65 20 2a 70 52 74 72  Node(Rtree *pRtr
3a272 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
3a273 4e 6f 64 65 2c 20 69 6e 74 20 69 48 65 69 67 68  Node, int iHeigh
3a274 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
3a275 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65  RtreeNode *pPare
3a276 6e 74 3b 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b  nt;.  int iCell;
3a277 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64  ..  assert( pNod
3a278 65 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 0a 20  e->nRef==1 );.. 
3a279 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 65   /* Remove the e
3a27a 6e 74 72 79 20 69 6e 20 74 68 65 20 70 61 72 65  ntry in the pare
3a27b 6e 74 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 69 43  nt cell. */.  iC
3a27c 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74  ell = nodeParent
3a27d 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4e  Index(pRtree, pN
3a27e 6f 64 65 29 3b 0a 20 20 70 50 61 72 65 6e 74 20  ode);.  pParent 
3a27f 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  = pNode->pParent
3a280 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65  ;.  pNode->pPare
3a281 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51  nt = 0;.  if( SQ
3a282 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 64  LITE_OK!=(rc = d
3a283 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72 65 65  eleteCell(pRtree
3a284 2c 20 70 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c  , pParent, iCell
3a285 2c 20 69 48 65 69 67 68 74 2b 31 29 29 20 0a 20  , iHeight+1)) . 
3a286 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    || SQLITE_OK!=
3a287 28 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73  (rc = nodeReleas
3a288 65 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  e(pRtree, pParen
3a289 74 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  t)).  ){.    ret
3a28a 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
3a28b 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 78 78 78  * Remove the xxx
3a28c 5f 6e 6f 64 65 20 65 6e 74 72 79 2e 20 2a 2f 0a  _node entry. */.
3a28d 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
3a28e 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44 65  nt64(pRtree->pDe
3a28f 6c 65 74 65 4e 6f 64 65 2c 20 31 2c 20 70 4e 6f  leteNode, 1, pNo
3a290 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 73 71  de->iNode);.  sq
3a291 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65  lite3_step(pRtre
3a292 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29 3b  e->pDeleteNode);
3a293 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
3a294 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  !=(rc = sqlite3_
3a295 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 44  reset(pRtree->pD
3a296 65 6c 65 74 65 4e 6f 64 65 29 29 20 29 7b 0a 20  eleteNode)) ){. 
3a297 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
3a298 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  }..  /* Remove t
3a299 68 65 20 78 78 78 5f 70 61 72 65 6e 74 20 65 6e  he xxx_parent en
3a29a 74 72 79 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  try. */.  sqlite
3a29b 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74  3_bind_int64(pRt
3a29c 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65  ree->pDeletePare
3a29d 6e 74 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e  nt, 1, pNode->iN
3a29e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ode);.  sqlite3_
3a29f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 44 65  step(pRtree->pDe
3a2a0 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 69  leteParent);.  i
3a2a1 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  f( SQLITE_OK!=(r
3a2a2 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
3a2a3 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  t(pRtree->pDelet
3a2a4 65 50 61 72 65 6e 74 29 29 20 29 7b 0a 20 20 20  eParent)) ){.   
3a2a5 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
3a2a6 20 20 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74    .  /* Remove t
3a2a7 68 65 20 6e 6f 64 65 20 66 72 6f 6d 20 74 68 65  he node from the
3a2a8 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68 20   in-memory hash 
3a2a9 74 61 62 6c 65 20 61 6e 64 20 6c 69 6e 6b 20 69  table and link i
3a2aa 74 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20  t into.  ** the 
3a2ab 52 74 72 65 65 2e 70 44 65 6c 65 74 65 64 20 6c  Rtree.pDeleted l
3a2ac 69 73 74 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  ist. Its content
3a2ad 73 20 77 69 6c 6c 20 62 65 20 72 65 2d 69 6e 73  s will be re-ins
3a2ae 65 72 74 65 64 20 6c 61 74 65 72 20 6f 6e 2e 0a  erted later on..
3a2af 20 20 2a 2f 0a 20 20 6e 6f 64 65 48 61 73 68 44    */.  nodeHashD
3a2b0 65 6c 65 74 65 28 70 52 74 72 65 65 2c 20 70 4e  elete(pRtree, pN
3a2b1 6f 64 65 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69  ode);.  pNode->i
3a2b2 4e 6f 64 65 20 3d 20 69 48 65 69 67 68 74 3b 0a  Node = iHeight;.
3a2b3 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d    pNode->pNext =
3a2b4 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65   pRtree->pDelete
3a2b5 64 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  d;.  pNode->nRef
3a2b6 2b 2b 3b 0a 20 20 70 52 74 72 65 65 2d 3e 70 44  ++;.  pRtree->pD
3a2b7 65 6c 65 74 65 64 20 3d 20 70 4e 6f 64 65 3b 0a  eleted = pNode;.
3a2b8 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3a2b9 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  _OK;.}..static v
3a2ba 6f 69 64 20 66 69 78 42 6f 75 6e 64 69 6e 67 42  oid fixBoundingB
3a2bb 6f 78 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ox(Rtree *pRtree
3a2bc 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
3a2bd 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65  de){.  RtreeNode
3a2be 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64   *pParent = pNod
3a2bf 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 69 66  e->pParent;.  if
3a2c0 28 20 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20  ( pParent ){.   
3a2c1 20 69 6e 74 20 69 69 3b 20 0a 20 20 20 20 69 6e   int ii; .    in
3a2c2 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28  t nCell = NCELL(
3a2c3 70 4e 6f 64 65 29 3b 0a 20 20 20 20 52 74 72 65  pNode);.    Rtre
3a2c4 65 43 65 6c 6c 20 62 6f 78 3b 20 20 20 20 20 20  eCell box;      
3a2c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a2c6 20 20 20 20 20 20 2f 2a 20 42 6f 75 6e 64 69 6e        /* Boundin
3a2c7 67 20 62 6f 78 20 66 6f 72 20 70 4e 6f 64 65 20  g box for pNode 
3a2c8 2a 2f 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65  */.    nodeGetCe
3a2c9 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
3a2ca 2c 20 30 2c 20 26 62 6f 78 29 3b 0a 20 20 20 20  , 0, &box);.    
3a2cb 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 6e 43 65  for(ii=1; ii<nCe
3a2cc 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  ll; ii++){.     
3a2cd 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b   RtreeCell cell;
3a2ce 0a 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65  .      nodeGetCe
3a2cf 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
3a2d0 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  , ii, &cell);.  
3a2d1 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
3a2d2 74 72 65 65 2c 20 26 62 6f 78 2c 20 26 63 65 6c  tree, &box, &cel
3a2d3 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 6f  l);.    }.    bo
3a2d4 78 2e 69 52 6f 77 69 64 20 3d 20 70 4e 6f 64 65  x.iRowid = pNode
3a2d5 2d 3e 69 4e 6f 64 65 3b 0a 20 20 20 20 69 69 20  ->iNode;.    ii 
3a2d6 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65  = nodeParentInde
3a2d7 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29  x(pRtree, pNode)
3a2d8 3b 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72  ;.    nodeOverwr
3a2d9 69 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  iteCell(pRtree, 
3a2da 70 50 61 72 65 6e 74 2c 20 26 62 6f 78 2c 20 69  pParent, &box, i
3a2db 69 29 3b 0a 20 20 20 20 66 69 78 42 6f 75 6e 64  i);.    fixBound
3a2dc 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70  ingBox(pRtree, p
3a2dd 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 0a  Parent);.  }.}..
3a2de 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65  /*.** Delete the
3a2df 20 63 65 6c 6c 20 61 74 20 69 6e 64 65 78 20 69   cell at index i
3a2e0 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f  Cell of node pNo
3a2e1 64 65 2e 20 41 66 74 65 72 20 72 65 6d 6f 76 69  de. After removi
3a2e2 6e 67 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 2c 20  ng the.** cell, 
3a2e3 61 64 6a 75 73 74 20 74 68 65 20 72 2d 74 72 65  adjust the r-tre
3a2e4 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  e data structure
3a2e5 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f   if required..*/
3a2e6 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 6c 65  .static int dele
3a2e7 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52  teCell(Rtree *pR
3a2e8 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
3a2e9 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65 6c  *pNode, int iCel
3a2ea 6c 2c 20 69 6e 74 20 69 48 65 69 67 68 74 29 7b  l, int iHeight){
3a2eb 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66  .  int rc;..  if
3a2ec 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
3a2ed 20 3d 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74   = fixLeafParent
3a2ee 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 29  (pRtree, pNode))
3a2ef 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
3a2f0 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d  c;.  }..  /* Rem
3a2f1 6f 76 65 20 74 68 65 20 63 65 6c 6c 20 66 72 6f  ove the cell fro
3a2f2 6d 20 74 68 65 20 6e 6f 64 65 2e 20 54 68 69 73  m the node. This
3a2f3 20 63 61 6c 6c 20 6a 75 73 74 20 6d 6f 76 65 73   call just moves
3a2f4 20 62 79 74 65 73 20 61 72 6f 75 6e 64 0a 20 20   bytes around.  
3a2f5 2a 2a 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  ** the in-memory
3a2f6 20 6e 6f 64 65 20 69 6d 61 67 65 2c 20 73 6f 20   node image, so 
3a2f7 69 74 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a  it cannot fail..
3a2f8 20 20 2a 2f 0a 20 20 6e 6f 64 65 44 65 6c 65 74    */.  nodeDelet
3a2f9 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e  eCell(pRtree, pN
3a2fa 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a 0a 20 20  ode, iCell);..  
3a2fb 2f 2a 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69  /* If the node i
3a2fc 73 20 6e 6f 74 20 74 68 65 20 74 72 65 65 20 72  s not the tree r
3a2fd 6f 6f 74 20 61 6e 64 20 6e 6f 77 20 68 61 73 20  oot and now has 
3a2fe 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6d 69  less than the mi
3a2ff 6e 69 6d 75 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65  nimum.  ** numbe
3a300 72 20 6f 66 20 63 65 6c 6c 73 2c 20 72 65 6d 6f  r of cells, remo
3a301 76 65 20 69 74 20 66 72 6f 6d 20 74 68 65 20 74  ve it from the t
3a302 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ree. Otherwise, 
3a303 75 70 64 61 74 65 20 74 68 65 0a 20 20 2a 2a 20  update the.  ** 
3a304 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65  cell in the pare
3a305 6e 74 20 6e 6f 64 65 20 73 6f 20 74 68 61 74 20  nt node so that 
3a306 69 74 20 74 69 67 68 74 6c 79 20 63 6f 6e 74 61  it tightly conta
3a307 69 6e 73 20 74 68 65 20 75 70 64 61 74 65 64 0a  ins the updated.
3a308 20 20 2a 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a    ** node..  */.
3a309 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
3a30a 64 65 21 3d 31 20 29 7b 0a 20 20 20 20 52 74 72  de!=1 ){.    Rtr
3a30b 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20  eeNode *pParent 
3a30c 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  = pNode->pParent
3a30d 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 72 65  ;.    if( (pPare
3a30e 6e 74 2d 3e 69 4e 6f 64 65 21 3d 31 20 7c 7c 20  nt->iNode!=1 || 
3a30f 4e 43 45 4c 4c 28 70 50 61 72 65 6e 74 29 21 3d  NCELL(pParent)!=
3a310 31 29 20 0a 20 20 20 20 20 26 26 20 28 4e 43 45  1) .     && (NCE
3a311 4c 4c 28 70 4e 6f 64 65 29 3c 52 54 52 45 45 5f  LL(pNode)<RTREE_
3a312 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29  MINCELLS(pRtree)
3a313 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  ).    ){.      r
3a314 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70  c = removeNode(p
3a315 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 48  Rtree, pNode, iH
3a316 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73  eight);.    }els
3a317 65 7b 0a 20 20 20 20 20 20 66 69 78 42 6f 75 6e  e{.      fixBoun
3a318 64 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c 20  dingBox(pRtree, 
3a319 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pNode);.    }.  
3a31a 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
3a31b 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 52 65  }..static int Re
3a31c 69 6e 73 65 72 74 28 0a 20 20 52 74 72 65 65 20  insert(.  Rtree 
3a31d 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
3a31e 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20  eNode *pNode, . 
3a31f 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
3a320 6c 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67 68  l, .  int iHeigh
3a321 74 0a 29 7b 0a 20 20 69 6e 74 20 2a 61 4f 72 64  t.){.  int *aOrd
3a322 65 72 3b 0a 20 20 69 6e 74 20 2a 61 53 70 61 72  er;.  int *aSpar
3a323 65 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  e;.  RtreeCell *
3a324 61 43 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20 2a  aCell;.  float *
3a325 61 44 69 73 74 61 6e 63 65 3b 0a 20 20 69 6e 74  aDistance;.  int
3a326 20 6e 43 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20   nCell;.  float 
3a327 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 52 54 52  aCenterCoord[RTR
3a328 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e  EE_MAX_DIMENSION
3a329 53 5d 3b 0a 20 20 69 6e 74 20 69 44 69 6d 3b 0a  S];.  int iDim;.
3a32a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20    int ii;.  int 
3a32b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
3a32c 0a 20 20 6d 65 6d 73 65 74 28 61 43 65 6e 74 65  .  memset(aCente
3a32d 72 43 6f 6f 72 64 2c 20 30 2c 20 73 69 7a 65 6f  rCoord, 0, sizeo
3a32e 66 28 66 6c 6f 61 74 29 2a 52 54 52 45 45 5f 4d  f(float)*RTREE_M
3a32f 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 29 3b 0a  AX_DIMENSIONS);.
3a330 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c  .  nCell = NCELL
3a331 28 70 4e 6f 64 65 29 2b 31 3b 0a 0a 20 20 2f 2a  (pNode)+1;..  /*
3a332 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 62 75   Allocate the bu
3a333 66 66 65 72 73 20 75 73 65 64 20 62 79 20 74 68  ffers used by th
3a334 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68  is operation. Th
3a335 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 0a  e allocation is.
3a336 20 20 2a 2a 20 72 65 6c 69 6e 71 75 69 73 68 65    ** relinquishe
3a337 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75  d before this fu
3a338 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a  nction returns..
3a339 20 20 2a 2f 0a 20 20 61 43 65 6c 6c 20 3d 20 28    */.  aCell = (
3a33a 52 74 72 65 65 43 65 6c 6c 20 2a 29 73 71 6c 69  RtreeCell *)sqli
3a33b 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 65 6c 6c  te3_malloc(nCell
3a33c 20 2a 20 28 0a 20 20 20 20 73 69 7a 65 6f 66 28   * (.    sizeof(
3a33d 52 74 72 65 65 43 65 6c 6c 29 20 2b 20 20 20 20  RtreeCell) +    
3a33e 20 20 20 20 20 2f 2a 20 61 43 65 6c 6c 20 61 72       /* aCell ar
3a33f 72 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f  ray */.    sizeo
3a340 66 28 69 6e 74 29 20 20 20 20 20 20 20 2b 20 20  f(int)       +  
3a341 20 20 20 20 20 20 20 2f 2a 20 61 4f 72 64 65 72         /* aOrder
3a342 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 73 69   array */.    si
3a343 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20 20  zeof(int)       
3a344 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70  +         /* aSp
3a345 61 72 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 20  are array */.   
3a346 20 73 69 7a 65 6f 66 28 66 6c 6f 61 74 29 20 20   sizeof(float)  
3a347 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a348 61 44 69 73 74 61 6e 63 65 20 61 72 72 61 79 20  aDistance array 
3a349 2a 2f 0a 20 20 29 29 3b 0a 20 20 69 66 28 20 21  */.  ));.  if( !
3a34a 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74  aCell ){.    ret
3a34b 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
3a34c 3b 0a 20 20 7d 0a 20 20 61 4f 72 64 65 72 20 20  ;.  }.  aOrder  
3a34d 20 20 3d 20 28 69 6e 74 20 2a 29 26 61 43 65 6c    = (int *)&aCel
3a34e 6c 5b 6e 43 65 6c 6c 5d 3b 0a 20 20 61 53 70 61  l[nCell];.  aSpa
3a34f 72 65 20 20 20 20 3d 20 28 69 6e 74 20 2a 29 26  re    = (int *)&
3a350 61 4f 72 64 65 72 5b 6e 43 65 6c 6c 5d 3b 0a 20  aOrder[nCell];. 
3a351 20 61 44 69 73 74 61 6e 63 65 20 3d 20 28 66 6c   aDistance = (fl
3a352 6f 61 74 20 2a 29 26 61 53 70 61 72 65 5b 6e 43  oat *)&aSpare[nC
3a353 65 6c 6c 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d  ell];..  for(ii=
3a354 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b  0; ii<nCell; ii+
3a355 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 69 3d 3d  +){.    if( ii==
3a356 28 6e 43 65 6c 6c 2d 31 29 20 29 7b 0a 20 20 20  (nCell-1) ){.   
3a357 20 20 20 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c     memcpy(&aCell
3a358 5b 69 69 5d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a  [ii], pCell, siz
3a359 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b  eof(RtreeCell));
3a35a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
3a35b 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
3a35c 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c  tree, pNode, ii,
3a35d 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20   &aCell[ii]);.  
3a35e 20 20 7d 0a 20 20 20 20 61 4f 72 64 65 72 5b 69    }.    aOrder[i
3a35f 69 5d 20 3d 20 69 69 3b 0a 20 20 20 20 66 6f 72  i] = ii;.    for
3a360 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52  (iDim=0; iDim<pR
3a361 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d  tree->nDim; iDim
3a362 2b 2b 29 7b 0a 20 20 20 20 20 20 61 43 65 6e 74  ++){.      aCent
3a363 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d  erCoord[iDim] +=
3a364 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69   DCOORD(aCell[ii
3a365 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d  ].aCoord[iDim*2]
3a366 29 3b 0a 20 20 20 20 20 20 61 43 65 6e 74 65 72  );.      aCenter
3a367 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44  Coord[iDim] += D
3a368 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
3a369 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d  aCoord[iDim*2+1]
3a36a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  );.    }.  }.  f
3a36b 6f 72 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c  or(iDim=0; iDim<
3a36c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44  pRtree->nDim; iD
3a36d 69 6d 2b 2b 29 7b 0a 20 20 20 20 61 43 65 6e 74  im++){.    aCent
3a36e 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 3d 20  erCoord[iDim] = 
3a36f 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69  aCenterCoord[iDi
3a370 6d 5d 2f 28 28 66 6c 6f 61 74 29 6e 43 65 6c 6c  m]/((float)nCell
3a371 2a 32 2e 30 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f  *2.0);.  }..  fo
3a372 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
3a373 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61 44 69  ; ii++){.    aDi
3a374 73 74 61 6e 63 65 5b 69 69 5d 20 3d 20 30 2e 30  stance[ii] = 0.0
3a375 3b 0a 20 20 20 20 66 6f 72 28 69 44 69 6d 3d 30  ;.    for(iDim=0
3a376 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e  ; iDim<pRtree->n
3a377 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20  Dim; iDim++){.  
3a378 20 20 20 20 66 6c 6f 61 74 20 63 6f 6f 72 64 20      float coord 
3a379 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69  = DCOORD(aCell[i
3a37a 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  i].aCoord[iDim*2
3a37b 2b 31 5d 29 20 2d 20 0a 20 20 20 20 20 20 20 20  +1]) - .        
3a37c 20 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69    DCOORD(aCell[i
3a37d 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  i].aCoord[iDim*2
3a37e 5d 29 3b 0a 20 20 20 20 20 20 61 44 69 73 74 61  ]);.      aDista
3a37f 6e 63 65 5b 69 69 5d 20 2b 3d 20 28 63 6f 6f 72  nce[ii] += (coor
3a380 64 2d 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69  d-aCenterCoord[i
3a381 44 69 6d 5d 29 2a 28 63 6f 6f 72 64 2d 61 43 65  Dim])*(coord-aCe
3a382 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 29  nterCoord[iDim])
3a383 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 53  ;.    }.  }..  S
3a384 6f 72 74 42 79 44 69 73 74 61 6e 63 65 28 61 4f  ortByDistance(aO
3a385 72 64 65 72 2c 20 6e 43 65 6c 6c 2c 20 61 44 69  rder, nCell, aDi
3a386 73 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b  stance, aSpare);
3a387 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72  .  nodeZero(pRtr
3a388 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a 20 20 66  ee, pNode);..  f
3a389 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  or(ii=0; rc==SQL
3a38a 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 28 6e 43  ITE_OK && ii<(nC
3a38b 65 6c 6c 2d 28 52 54 52 45 45 5f 4d 49 4e 43 45  ell-(RTREE_MINCE
3a38c 4c 4c 53 28 70 52 74 72 65 65 29 2b 31 29 29 3b  LLS(pRtree)+1));
3a38d 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65   ii++){.    Rtre
3a38e 65 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43 65 6c  eCell *p = &aCel
3a38f 6c 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a 20  l[aOrder[ii]];. 
3a390 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c     nodeInsertCel
3a391 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
3a392 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e   p);.    if( p->
3a393 69 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69  iRowid==pCell->i
3a394 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 69  Rowid ){.      i
3a395 66 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b  f( iHeight==0 ){
3a396 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 6f  .        rc = ro
3a397 77 69 64 57 72 69 74 65 28 70 52 74 72 65 65 2c  widWrite(pRtree,
3a398 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64   p->iRowid, pNod
3a399 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e->iNode);.     
3a39a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
3a39b 72 63 20 3d 20 70 61 72 65 6e 74 57 72 69 74 65  rc = parentWrite
3a39c 28 70 52 74 72 65 65 2c 20 70 2d 3e 69 52 6f 77  (pRtree, p->iRow
3a39d 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  id, pNode->iNode
3a39e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
3a39f 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
3a3a0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3a3a1 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70  fixBoundingBox(p
3a3a2 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
3a3a3 20 7d 0a 20 20 66 6f 72 28 3b 20 72 63 3d 3d 53   }.  for(; rc==S
3a3a4 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e  QLITE_OK && ii<n
3a3a5 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  Cell; ii++){.   
3a3a6 20 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64 65 20   /* Find a node 
3a3a7 74 6f 20 73 74 6f 72 65 20 74 68 69 73 20 63 65  to store this ce
3a3a8 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e  ll in. pNode->iN
3a3a9 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f  ode currently co
3a3aa 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68  ntains.    ** th
3a3ab 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 20  e height of the 
3a3ac 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
3a3ad 62 79 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20  by the cell..   
3a3ae 20 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64   */.    RtreeNod
3a3af 65 20 2a 70 49 6e 73 65 72 74 3b 0a 20 20 20 20  e *pInsert;.    
3a3b0 52 74 72 65 65 43 65 6c 6c 20 2a 70 20 3d 20 26  RtreeCell *p = &
3a3b1 61 43 65 6c 6c 5b 61 4f 72 64 65 72 5b 69 69 5d  aCell[aOrder[ii]
3a3b2 5d 3b 0a 20 20 20 20 72 63 20 3d 20 43 68 6f 6f  ];.    rc = Choo
3a3b3 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c 20 70  seLeaf(pRtree, p
3a3b4 2c 20 69 48 65 69 67 68 74 2c 20 26 70 49 6e 73  , iHeight, &pIns
3a3b5 65 72 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ert);.    if( rc
3a3b6 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3a3b7 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20       int rc2;.  
3a3b8 20 20 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e      rc = rtreeIn
3a3b9 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
3a3ba 20 70 49 6e 73 65 72 74 2c 20 70 2c 20 69 48 65   pInsert, p, iHe
3a3bb 69 67 68 74 29 3b 0a 20 20 20 20 20 20 72 63 32  ight);.      rc2
3a3bc 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
3a3bd 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 29 3b  Rtree, pInsert);
3a3be 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
3a3bf 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3a3c0 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
3a3c1 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
3a3c2 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3a3c3 61 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  aCell);.  return
3a3c4 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e   rc;.}../*.** In
3a3c5 73 65 72 74 20 63 65 6c 6c 20 70 43 65 6c 6c 20  sert cell pCell 
3a3c6 69 6e 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  into node pNode.
3a3c7 20 4e 6f 64 65 20 70 4e 6f 64 65 20 69 73 20 74   Node pNode is t
3a3c8 68 65 20 68 65 61 64 20 6f 66 20 61 20 0a 2a 2a  he head of a .**
3a3c9 20 73 75 62 74 72 65 65 20 69 48 65 69 67 68 74   subtree iHeight
3a3ca 20 68 69 67 68 20 28 6c 65 61 66 20 6e 6f 64 65   high (leaf node
3a3cb 73 20 68 61 76 65 20 69 48 65 69 67 68 74 3d 3d  s have iHeight==
3a3cc 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  0)..*/.static in
3a3cd 74 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  t rtreeInsertCel
3a3ce 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  l(.  Rtree *pRtr
3a3cf 65 65 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ee,.  RtreeNode 
3a3d0 2a 70 4e 6f 64 65 2c 0a 20 20 52 74 72 65 65 43  *pNode,.  RtreeC
3a3d1 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a 20 20 69 6e  ell *pCell,.  in
3a3d2 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69  t iHeight.){.  i
3a3d3 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
3a3d4 4b 3b 0a 20 20 69 66 28 20 69 48 65 69 67 68 74  K;.  if( iHeight
3a3d5 3e 30 20 29 7b 0a 20 20 20 20 52 74 72 65 65 4e  >0 ){.    RtreeN
3a3d6 6f 64 65 20 2a 70 43 68 69 6c 64 20 3d 20 6e 6f  ode *pChild = no
3a3d7 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74  deHashLookup(pRt
3a3d8 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77  ree, pCell->iRow
3a3d9 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70 43 68  id);.    if( pCh
3a3da 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64  ild ){.      nod
3a3db 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
3a3dc 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74   pChild->pParent
3a3dd 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66  );.      nodeRef
3a3de 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20  erence(pNode);. 
3a3df 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70 50 61       pChild->pPa
3a3e0 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  rent = pNode;.  
3a3e1 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 6f    }.  }.  if( no
3a3e2 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
3a3e3 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c  ree, pNode, pCel
3a3e4 6c 29 20 29 7b 0a 23 69 66 20 56 41 52 49 41 4e  l) ){.#if VARIAN
3a3e5 54 5f 52 53 54 41 52 54 52 45 45 5f 52 45 49 4e  T_RSTARTREE_REIN
3a3e6 53 45 52 54 0a 20 20 20 20 69 66 28 20 69 48 65  SERT.    if( iHe
3a3e7 69 67 68 74 3c 3d 70 52 74 72 65 65 2d 3e 69 52  ight<=pRtree->iR
3a3e8 65 69 6e 73 65 72 74 48 65 69 67 68 74 20 7c 7c  einsertHeight ||
3a3e9 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31   pNode->iNode==1
3a3ea 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 70  ){.      rc = Sp
3a3eb 6c 69 74 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  litNode(pRtree, 
3a3ec 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48  pNode, pCell, iH
3a3ed 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73  eight);.    }els
3a3ee 65 7b 0a 20 20 20 20 20 20 70 52 74 72 65 65 2d  e{.      pRtree-
3a3ef 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67 68 74  >iReinsertHeight
3a3f0 20 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 20 20   = iHeight;.    
3a3f1 20 20 72 63 20 3d 20 52 65 69 6e 73 65 72 74 28    rc = Reinsert(
3a3f2 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70  pRtree, pNode, p
3a3f3 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a  Cell, iHeight);.
3a3f4 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20      }.#else.    
3a3f5 72 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65 28 70  rc = SplitNode(p
3a3f6 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43  Rtree, pNode, pC
3a3f7 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a 23  ell, iHeight);.#
3a3f8 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20  endif.  }else{. 
3a3f9 20 20 20 41 64 6a 75 73 74 54 72 65 65 28 70 52     AdjustTree(pR
3a3fa 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65  tree, pNode, pCe
3a3fb 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 48 65  ll);.    if( iHe
3a3fc 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ight==0 ){.     
3a3fd 20 72 63 20 3d 20 72 6f 77 69 64 57 72 69 74 65   rc = rowidWrite
3a3fe 28 70 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e  (pRtree, pCell->
3a3ff 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69  iRowid, pNode->i
3a400 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Node);.    }else
3a401 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 72  {.      rc = par
3a402 65 6e 74 57 72 69 74 65 28 70 52 74 72 65 65 2c  entWrite(pRtree,
3a403 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20   pCell->iRowid, 
3a404 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20  pNode->iNode);. 
3a405 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
3a406 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
3a407 69 6e 74 20 72 65 69 6e 73 65 72 74 4e 6f 64 65  int reinsertNode
3a408 43 6f 6e 74 65 6e 74 28 52 74 72 65 65 20 2a 70  Content(Rtree *p
3a409 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
3a40a 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20   *pNode){.  int 
3a40b 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ii;.  int rc = S
3a40c 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
3a40d 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e  nCell = NCELL(pN
3a40e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d  ode);..  for(ii=
3a40f 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
3a410 20 26 26 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69   && ii<nCell; ii
3a411 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f  ++){.    RtreeNo
3a412 64 65 20 2a 70 49 6e 73 65 72 74 3b 0a 20 20 20  de *pInsert;.   
3a413 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b   RtreeCell cell;
3a414 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  .    nodeGetCell
3a415 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
3a416 69 69 2c 20 26 63 65 6c 6c 29 3b 0a 0a 20 20 20  ii, &cell);..   
3a417 20 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64 65 20   /* Find a node 
3a418 74 6f 20 73 74 6f 72 65 20 74 68 69 73 20 63 65  to store this ce
3a419 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e  ll in. pNode->iN
3a41a 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f  ode currently co
3a41b 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68  ntains.    ** th
3a41c 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 20  e height of the 
3a41d 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
3a41e 62 79 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20  by the cell..   
3a41f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 43 68 6f   */.    rc = Cho
3a420 6f 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c 20  oseLeaf(pRtree, 
3a421 26 63 65 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69 4e  &cell, pNode->iN
3a422 6f 64 65 2c 20 26 70 49 6e 73 65 72 74 29 3b 0a  ode, &pInsert);.
3a423 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
3a424 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
3a425 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72 63  nt rc2;.      rc
3a426 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65   = rtreeInsertCe
3a427 6c 6c 28 70 52 74 72 65 65 2c 20 70 49 6e 73 65  ll(pRtree, pInse
3a428 72 74 2c 20 26 63 65 6c 6c 2c 20 70 4e 6f 64 65  rt, &cell, pNode
3a429 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  ->iNode);.      
3a42a 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73  rc2 = nodeReleas
3a42b 65 28 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72  e(pRtree, pInser
3a42c 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  t);.      if( rc
3a42d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3a42e 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b         rc = rc2;
3a42f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
3a430 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
3a431 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6c 65 63 74 20  }../*.** Select 
3a432 61 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 75 73  a currently unus
3a433 65 64 20 72 6f 77 69 64 20 66 6f 72 20 61 20 6e  ed rowid for a n
3a434 65 77 20 72 2d 74 72 65 65 20 72 65 63 6f 72 64  ew r-tree record
3a435 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3a436 6e 65 77 52 6f 77 69 64 28 52 74 72 65 65 20 2a  newRowid(Rtree *
3a437 70 52 74 72 65 65 2c 20 69 36 34 20 2a 70 69 52  pRtree, i64 *piR
3a438 6f 77 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b  owid){.  int rc;
3a439 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
3a43a 6e 75 6c 6c 28 70 52 74 72 65 65 2d 3e 70 57 72  null(pRtree->pWr
3a43b 69 74 65 52 6f 77 69 64 2c 20 31 29 3b 0a 20 20  iteRowid, 1);.  
3a43c 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
3a43d 6c 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  l(pRtree->pWrite
3a43e 52 6f 77 69 64 2c 20 32 29 3b 0a 20 20 73 71 6c  Rowid, 2);.  sql
3a43f 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
3a440 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
3a441 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
3a442 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 57 72  eset(pRtree->pWr
3a443 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 2a 70 69  iteRowid);.  *pi
3a444 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  Rowid = sqlite3_
3a445 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
3a446 64 28 70 52 74 72 65 65 2d 3e 64 62 29 3b 0a 20  d(pRtree->db);. 
3a447 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
3a448 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74  ifndef NDEBUG.st
3a449 61 74 69 63 20 69 6e 74 20 68 61 73 68 49 73 45  atic int hashIsE
3a44a 6d 70 74 79 28 52 74 72 65 65 20 2a 70 52 74 72  mpty(Rtree *pRtr
3a44b 65 65 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  ee){.  int ii;. 
3a44c 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 48 41   for(ii=0; ii<HA
3a44d 53 48 53 49 5a 45 3b 20 69 69 2b 2b 29 7b 0a 20  SHSIZE; ii++){. 
3a44e 20 20 20 61 73 73 65 72 74 28 20 21 70 52 74 72     assert( !pRtr
3a44f 65 65 2d 3e 61 48 61 73 68 5b 69 69 5d 20 29 3b  ee->aHash[ii] );
3a450 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
3a451 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
3a452 20 54 68 65 20 78 55 70 64 61 74 65 20 6d 65 74   The xUpdate met
3a453 68 6f 64 20 66 6f 72 20 72 74 72 65 65 20 6d 6f  hod for rtree mo
3a454 64 75 6c 65 20 76 69 72 74 75 61 6c 20 74 61 62  dule virtual tab
3a455 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  les..*/.static i
3a456 6e 74 20 72 74 72 65 65 55 70 64 61 74 65 28 0a  nt rtreeUpdate(.
3a457 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
3a458 70 56 74 61 62 2c 20 0a 20 20 69 6e 74 20 6e 44  pVtab, .  int nD
3a459 61 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  ata, .  sqlite3_
3a45a 76 61 6c 75 65 20 2a 2a 61 7a 44 61 74 61 2c 20  value **azData, 
3a45b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
3a45c 2a 70 52 6f 77 69 64 0a 29 7b 0a 20 20 52 74 72  *pRowid.){.  Rtr
3a45d 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
3a45e 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69  ree *)pVtab;.  i
3a45f 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
3a460 4b 3b 0a 0a 20 20 72 74 72 65 65 52 65 66 65 72  K;..  rtreeRefer
3a461 65 6e 63 65 28 70 52 74 72 65 65 29 3b 0a 0a 20  ence(pRtree);.. 
3a462 20 61 73 73 65 72 74 28 6e 44 61 74 61 3e 3d 31   assert(nData>=1
3a463 29 3b 0a 20 20 61 73 73 65 72 74 28 68 61 73 68  );.  assert(hash
3a464 49 73 45 6d 70 74 79 28 70 52 74 72 65 65 29 29  IsEmpty(pRtree))
3a465 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 7a 44 61 74  ;..  /* If azDat
3a466 61 5b 30 5d 20 69 73 20 6e 6f 74 20 61 6e 20 53  a[0] is not an S
3a467 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 69  QL NULL value, i
3a468 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f  t is the rowid o
3a469 66 20 61 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20  f a.  ** record 
3a46a 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 74  to delete from t
3a46b 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65 2e  he r-tree table.
3a46c 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   The following b
3a46d 6c 6f 63 6b 20 64 6f 65 73 0a 20 20 2a 2a 20 6a  lock does.  ** j
3a46e 75 73 74 20 74 68 61 74 2e 0a 20 20 2a 2f 0a 20  ust that..  */. 
3a46f 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
3a470 75 65 5f 74 79 70 65 28 61 7a 44 61 74 61 5b 30  ue_type(azData[0
3a471 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])!=SQLITE_NULL 
3a472 29 7b 0a 20 20 20 20 69 36 34 20 69 44 65 6c 65  ){.    i64 iDele
3a473 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
3a474 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20     /* The rowid 
3a475 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20  to delete */.   
3a476 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61   RtreeNode *pLea
3a477 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  f;           /* 
3a478 4c 65 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69  Leaf node contai
3a479 6e 69 6e 67 20 72 65 63 6f 72 64 20 69 44 65 6c  ning record iDel
3a47a 65 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ete */.    int i
3a47b 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
3a47c 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
3a47d 6f 66 20 69 44 65 6c 65 74 65 20 63 65 6c 6c 20  of iDelete cell 
3a47e 69 6e 20 70 4c 65 61 66 20 2a 2f 0a 20 20 20 20  in pLeaf */.    
3a47f 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f 74  RtreeNode *pRoot
3a480 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e  ;..    /* Obtain
3a481 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
3a482 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 74 6f  the root node to
3a483 20 69 6e 69 74 69 61 6c 69 73 65 20 52 74 72 65   initialise Rtre
3a484 65 2e 69 44 65 70 74 68 20 2a 2f 0a 20 20 20 20  e.iDepth */.    
3a485 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
3a486 28 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26  (pRtree, 1, 0, &
3a487 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 2f 2a 20  pRoot);..    /* 
3a488 4f 62 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e  Obtain a referen
3a489 63 65 20 74 6f 20 74 68 65 20 6c 65 61 66 20 6e  ce to the leaf n
3a48a 6f 64 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ode that contain
3a48b 73 20 74 68 65 20 65 6e 74 72 79 20 0a 20 20 20  s the entry .   
3a48c 20 2a 2a 20 61 62 6f 75 74 20 74 6f 20 62 65 20   ** about to be 
3a48d 64 65 6c 65 74 65 64 2e 20 0a 20 20 20 20 2a 2f  deleted. .    */
3a48e 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
3a48f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3a490 69 44 65 6c 65 74 65 20 3d 20 73 71 6c 69 74 65  iDelete = sqlite
3a491 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 7a  3_value_int64(az
3a492 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20  Data[0]);.      
3a493 72 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f 64  rc = findLeafNod
3a494 65 28 70 52 74 72 65 65 2c 20 69 44 65 6c 65 74  e(pRtree, iDelet
3a495 65 2c 20 26 70 4c 65 61 66 29 3b 0a 20 20 20 20  e, &pLeaf);.    
3a496 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  }..    /* Delete
3a497 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 71 75 65   the cell in que
3a498 73 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6c  stion from the l
3a499 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20  eaf node. */.   
3a49a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3a49b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK ){.      int 
3a49c 72 63 32 3b 0a 20 20 20 20 20 20 69 43 65 6c 6c  rc2;.      iCell
3a49d 20 3d 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65   = nodeRowidInde
3a49e 78 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c  x(pRtree, pLeaf,
3a49f 20 69 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20   iDelete);.     
3a4a0 20 72 63 20 3d 20 64 65 6c 65 74 65 43 65 6c 6c   rc = deleteCell
3a4a1 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20  (pRtree, pLeaf, 
3a4a2 69 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20  iCell, 0);.     
3a4a3 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61   rc2 = nodeRelea
3a4a4 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  se(pRtree, pLeaf
3a4a5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
3a4a6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3a4a7 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a        rc = rc2;.
3a4a8 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
3a4a9 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65     /* Delete the
3a4aa 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 65   corresponding e
3a4ab 6e 74 72 79 20 69 6e 20 74 68 65 20 3c 72 74 72  ntry in the <rtr
3a4ac 65 65 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65 2e  ee>_rowid table.
3a4ad 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
3a4ae 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3a4af 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
3a4b0 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44  int64(pRtree->pD
3a4b1 65 6c 65 74 65 52 6f 77 69 64 2c 20 31 2c 20 69  eleteRowid, 1, i
3a4b2 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 73  Delete);.      s
3a4b3 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
3a4b4 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
3a4b5 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
3a4b6 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
3a4b7 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
3a4b8 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
3a4b9 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 72 6f   Check if the ro
3a4ba 6f 74 20 6e 6f 64 65 20 6e 6f 77 20 68 61 73 20  ot node now has 
3a4bb 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 68 69 6c  exactly one chil
3a4bc 64 2e 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76 65  d. If so, remove
3a4bd 0a 20 20 20 20 2a 2a 20 69 74 2c 20 73 63 68 65  .    ** it, sche
3a4be 64 75 6c 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  dule the content
3a4bf 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 66  s of the child f
3a4c0 6f 72 20 72 65 69 6e 73 65 72 74 69 6f 6e 20 61  or reinsertion a
3a4c1 6e 64 20 0a 20 20 20 20 2a 2a 20 72 65 64 75 63  nd .    ** reduc
3a4c2 65 20 74 68 65 20 74 72 65 65 20 68 65 69 67 68  e the tree heigh
3a4c3 74 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a  t by one..    **
3a4c4 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20  .    ** This is 
3a4c5 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 63 6f  equivalent to co
3a4c6 70 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e  pying the conten
3a4c7 74 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20  ts of the child 
3a4c8 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  into.    ** the 
3a4c9 72 6f 6f 74 20 6e 6f 64 65 20 28 74 68 65 20 6f  root node (the o
3a4ca 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 47 75  peration that Gu
3a4cb 74 6d 61 6e 27 73 20 70 61 70 65 72 20 73 61 79  tman's paper say
3a4cc 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 0a 20 20  s to perform .  
3a4cd 20 20 2a 2a 20 69 6e 20 74 68 69 73 20 73 63 65    ** in this sce
3a4ce 6e 61 72 69 6f 29 2e 0a 20 20 20 20 2a 2f 0a 20  nario)..    */. 
3a4cf 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
3a4d0 45 5f 4f 4b 20 26 26 20 70 52 74 72 65 65 2d 3e  E_OK && pRtree->
3a4d1 69 44 65 70 74 68 3e 30 20 29 7b 0a 20 20 20 20  iDepth>0 ){.    
3a4d2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3a4d3 5f 4f 4b 20 26 26 20 4e 43 45 4c 4c 28 70 52 6f  _OK && NCELL(pRo
3a4d4 6f 74 29 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ot)==1 ){.      
3a4d5 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68    RtreeNode *pCh
3a4d6 69 6c 64 3b 0a 20 20 20 20 20 20 20 20 69 36 34  ild;.        i64
3a4d7 20 69 43 68 69 6c 64 20 3d 20 6e 6f 64 65 47 65   iChild = nodeGe
3a4d8 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70  tRowid(pRtree, p
3a4d9 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  Root, 0);.      
3a4da 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69    rc = nodeAcqui
3a4db 72 65 28 70 52 74 72 65 65 2c 20 69 43 68 69 6c  re(pRtree, iChil
3a4dc 64 2c 20 70 52 6f 6f 74 2c 20 26 70 43 68 69 6c  d, pRoot, &pChil
3a4dd 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
3a4de 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
3a4df 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
3a4e0 72 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72 65  removeNode(pRtre
3a4e1 65 2c 20 70 43 68 69 6c 64 2c 20 70 52 74 72 65  e, pChild, pRtre
3a4e2 65 2d 3e 69 44 65 70 74 68 2d 31 29 3b 0a 20 20  e->iDepth-1);.  
3a4e3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3a4e4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3a4e5 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  K ){.          p
3a4e6 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 2d 3b  Rtree->iDepth--;
3a4e7 0a 20 20 20 20 20 20 20 20 20 20 77 72 69 74 65  .          write
3a4e8 49 6e 74 31 36 28 70 52 6f 6f 74 2d 3e 7a 44 61  Int16(pRoot->zDa
3a4e9 74 61 2c 20 70 52 74 72 65 65 2d 3e 69 44 65 70  ta, pRtree->iDep
3a4ea 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  th);.          p
3a4eb 52 6f 6f 74 2d 3e 69 73 44 69 72 74 79 20 3d 20  Root->isDirty = 
3a4ec 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
3a4ed 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
3a4ee 2f 2a 20 52 65 2d 69 6e 73 65 72 74 20 74 68 65  /* Re-insert the
3a4ef 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6e 79   contents of any
3a4f0 20 75 6e 64 65 72 66 75 6c 6c 20 6e 6f 64 65 73   underfull nodes
3a4f1 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
3a4f2 65 20 74 72 65 65 2e 20 2a 2f 0a 20 20 20 20 66  e tree. */.    f
3a4f3 6f 72 28 70 4c 65 61 66 3d 70 52 74 72 65 65 2d  or(pLeaf=pRtree-
3a4f4 3e 70 44 65 6c 65 74 65 64 3b 20 70 4c 65 61 66  >pDeleted; pLeaf
3a4f5 3b 20 70 4c 65 61 66 3d 70 52 74 72 65 65 2d 3e  ; pLeaf=pRtree->
3a4f6 70 44 65 6c 65 74 65 64 29 7b 0a 20 20 20 20 20  pDeleted){.     
3a4f7 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3a4f8 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
3a4f9 20 3d 20 72 65 69 6e 73 65 72 74 4e 6f 64 65 43   = reinsertNodeC
3a4fa 6f 6e 74 65 6e 74 28 70 52 74 72 65 65 2c 20 70  ontent(pRtree, p
3a4fb 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Leaf);.      }. 
3a4fc 20 20 20 20 20 70 52 74 72 65 65 2d 3e 70 44 65       pRtree->pDe
3a4fd 6c 65 74 65 64 20 3d 20 70 4c 65 61 66 2d 3e 70  leted = pLeaf->p
3a4fe 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69  Next;.      sqli
3a4ff 74 65 33 5f 66 72 65 65 28 70 4c 65 61 66 29 3b  te3_free(pLeaf);
3a500 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52  .    }..    /* R
3a501 65 6c 65 61 73 65 20 74 68 65 20 72 65 66 65 72  elease the refer
3a502 65 6e 63 65 20 74 6f 20 74 68 65 20 72 6f 6f 74  ence to the root
3a503 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 69 66   node. */.    if
3a504 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3a505 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  ){.      rc = no
3a506 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
3a507 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 65  , pRoot);.    }e
3a508 6c 73 65 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52  lse{.      nodeR
3a509 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
3a50a 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Root);.    }.  }
3a50b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 7a  ..  /* If the az
3a50c 44 61 74 61 5b 5d 20 61 72 72 61 79 20 63 6f 6e  Data[] array con
3a50d 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20  tains more than 
3a50e 6f 6e 65 20 65 6c 65 6d 65 6e 74 2c 20 65 6c 65  one element, ele
3a50f 6d 65 6e 74 73 0a 20 20 2a 2a 20 28 61 7a 44 61  ments.  ** (azDa
3a510 74 61 5b 32 5d 2e 2e 61 7a 44 61 74 61 5b 61 72  ta[2]..azData[ar
3a511 67 63 2d 31 5d 29 20 63 6f 6e 74 61 69 6e 20 61  gc-1]) contain a
3a512 20 6e 65 77 20 72 65 63 6f 72 64 20 74 6f 20 69   new record to i
3a513 6e 73 65 72 74 20 69 6e 74 6f 0a 20 20 2a 2a 20  nsert into.  ** 
3a514 74 68 65 20 72 2d 74 72 65 65 20 73 74 72 75 63  the r-tree struc
3a515 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ture..  */.  if(
3a516 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
3a517 26 20 6e 44 61 74 61 3e 31 20 29 7b 0a 20 20 20  & nData>1 ){.   
3a518 20 2f 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77   /* Insert a new
3a519 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68 65   record into the
3a51a 20 72 2d 74 72 65 65 20 2a 2f 0a 20 20 20 20 52   r-tree */.    R
3a51b 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
3a51c 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 52     int ii;.    R
3a51d 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b  treeNode *pLeaf;
3a51e 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74  ..    /* Populat
3a51f 65 20 74 68 65 20 63 65 6c 6c 2e 61 43 6f 6f 72  e the cell.aCoor
3a520 64 5b 5d 20 61 72 72 61 79 2e 20 54 68 65 20 66  d[] array. The f
3a521 69 72 73 74 20 63 6f 6f 72 64 69 6e 61 74 65 20  irst coordinate 
3a522 69 73 20 61 7a 44 61 74 61 5b 33 5d 2e 20 2a 2f  is azData[3]. */
3a523 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 44 61  .    assert( nDa
3a524 74 61 3d 3d 28 70 52 74 72 65 65 2d 3e 6e 44 69  ta==(pRtree->nDi
3a525 6d 2a 32 20 2b 20 33 29 20 29 3b 0a 20 20 20 20  m*2 + 3) );.    
3a526 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f  if( pRtree->eCoo
3a527 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
3a528 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20  ORD_REAL32 ){.  
3a529 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
3a52a 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
3a52b 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20  ); ii+=2){.     
3a52c 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69     cell.aCoord[i
3a52d 69 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29 73 71  i].f = (float)sq
3a52e 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
3a52f 6c 65 28 61 7a 44 61 74 61 5b 69 69 2b 33 5d 29  le(azData[ii+3])
3a530 3b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61  ;.        cell.a
3a531 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 3d 20  Coord[ii+1].f = 
3a532 28 66 6c 6f 61 74 29 73 71 6c 69 74 65 33 5f 76  (float)sqlite3_v
3a533 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 7a 44 61  alue_double(azDa
3a534 74 61 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20 20  ta[ii+4]);.     
3a535 20 20 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f 6f     if( cell.aCoo
3a536 72 64 5b 69 69 5d 2e 66 3e 63 65 6c 6c 2e 61 43  rd[ii].f>cell.aC
3a537 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 29 7b 0a  oord[ii+1].f ){.
3a538 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
3a539 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
3a53a 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
3a53b 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20   constraint;.   
3a53c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
3a53d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3a53e 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52  for(ii=0; ii<(pR
3a53f 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69  tree->nDim*2); i
3a540 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 63  i+=2){.        c
3a541 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  ell.aCoord[ii].i
3a542 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3a543 5f 69 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b 33  _int(azData[ii+3
3a544 5d 29 3b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c  ]);.        cell
3a545 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20  .aCoord[ii+1].i 
3a546 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3a547 69 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b 34 5d  int(azData[ii+4]
3a548 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  );.        if( c
3a549 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  ell.aCoord[ii].i
3a54a 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b  >cell.aCoord[ii+
3a54b 31 5d 2e 69 20 29 7b 0a 20 20 20 20 20 20 20 20  1].i ){.        
3a54c 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
3a54d 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20  NSTRAINT;.      
3a54e 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72 61      goto constra
3a54f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  int;.        }. 
3a550 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
3a551 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
3a552 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
3a553 20 6e 65 77 20 72 6f 77 2e 20 2a 2f 0a 20 20 20   new row. */.   
3a554 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
3a555 75 65 5f 74 79 70 65 28 61 7a 44 61 74 61 5b 32  ue_type(azData[2
3a556 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])==SQLITE_NULL 
3a557 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 65  ){.      rc = ne
3a558 77 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 26  wRowid(pRtree, &
3a559 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20  cell.iRowid);.  
3a55a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63    }else{.      c
3a55b 65 6c 6c 2e 69 52 6f 77 69 64 20 3d 20 73 71 6c  ell.iRowid = sql
3a55c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
3a55d 28 61 7a 44 61 74 61 5b 32 5d 29 3b 0a 20 20 20  (azData[2]);.   
3a55e 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
3a55f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
3a560 65 61 64 52 6f 77 69 64 2c 20 31 2c 20 63 65 6c  eadRowid, 1, cel
3a561 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  l.iRowid);.     
3a562 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
3a563 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52  =sqlite3_step(pR
3a564 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
3a565 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ){.        sql
3a566 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
3a567 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a  e->pReadRowid);.
3a568 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
3a569 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a  ITE_CONSTRAINT;.
3a56a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e          goto con
3a56b 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20 20 7d  straint;.      }
3a56c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
3a56d 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
3a56e 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a 20  ->pReadRowid);. 
3a56f 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
3a570 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3a571 20 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65       rc = Choose
3a572 4c 65 61 66 28 70 52 74 72 65 65 2c 20 26 63 65  Leaf(pRtree, &ce
3a573 6c 6c 2c 20 30 2c 20 26 70 4c 65 61 66 29 3b 0a  ll, 0, &pLeaf);.
3a574 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
3a575 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3a576 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20       int rc2;.  
3a577 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52 65 69      pRtree->iRei
3a578 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20 2d 31  nsertHeight = -1
3a579 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 74 72  ;.      rc = rtr
3a57a 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  eeInsertCell(pRt
3a57b 72 65 65 2c 20 70 4c 65 61 66 2c 20 26 63 65 6c  ree, pLeaf, &cel
3a57c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32  l, 0);.      rc2
3a57d 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
3a57e 52 74 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a 20  Rtree, pLeaf);. 
3a57f 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
3a580 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3a581 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20    rc = rc2;.    
3a582 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 63    }.    }.  }..c
3a583 6f 6e 73 74 72 61 69 6e 74 3a 0a 20 20 72 74 72  onstraint:.  rtr
3a584 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  eeRelease(pRtree
3a585 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
3a586 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 52 65  }../*.** The xRe
3a587 6e 61 6d 65 20 6d 65 74 68 6f 64 20 66 6f 72 20  name method for 
3a588 72 74 72 65 65 20 6d 6f 64 75 6c 65 20 76 69 72  rtree module vir
3a589 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a  tual tables..*/.
3a58a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
3a58b 52 65 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76  Rename(sqlite3_v
3a58c 74 61 62 20 2a 70 56 74 61 62 2c 20 63 6f 6e 73  tab *pVtab, cons
3a58d 74 20 63 68 61 72 20 2a 7a 4e 65 77 4e 61 6d 65  t char *zNewName
3a58e 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  ){.  Rtree *pRtr
3a58f 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56  ee = (Rtree *)pV
3a590 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  tab;.  int rc = 
3a591 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
3a592 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c  char *zSql = sql
3a593 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20  ite3_mprintf(.  
3a594 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25    "ALTER TABLE %
3a595 51 2e 27 25 71 5f 6e 6f 64 65 27 20 20 20 52 45  Q.'%q_node'   RE
3a596 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 6e 6f 64  NAME TO \"%w_nod
3a597 65 5c 22 3b 22 0a 20 20 20 20 22 41 4c 54 45 52  e\";".    "ALTER
3a598 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 70 61   TABLE %Q.'%q_pa
3a599 72 65 6e 74 27 20 52 45 4e 41 4d 45 20 54 4f 20  rent' RENAME TO 
3a59a 5c 22 25 77 5f 70 61 72 65 6e 74 5c 22 3b 22 0a  \"%w_parent\";".
3a59b 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45      "ALTER TABLE
3a59c 20 25 51 2e 27 25 71 5f 72 6f 77 69 64 27 20 20   %Q.'%q_rowid'  
3a59d 52 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 72  RENAME TO \"%w_r
3a59e 6f 77 69 64 5c 22 3b 22 0a 20 20 20 20 2c 20 70  owid\";".    , p
3a59f 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72  Rtree->zDb, pRtr
3a5a0 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e  ee->zName, zNewN
3a5a1 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74 72 65  ame .    , pRtre
3a5a2 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e  e->zDb, pRtree->
3a5a3 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 20  zName, zNewName 
3a5a4 0a 20 20 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a  .    , pRtree->z
3a5a5 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d  Db, pRtree->zNam
3a5a6 65 2c 20 7a 4e 65 77 4e 61 6d 65 0a 20 20 29 3b  e, zNewName.  );
3a5a7 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20  .  if( zSql ){. 
3a5a8 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
3a5a9 65 78 65 63 28 70 52 74 72 65 65 2d 3e 64 62 2c  exec(pRtree->db,
3a5aa 20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b   zSql, 0, 0, 0);
3a5ab 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
3a5ac 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 72  e(zSql);.  }.  r
3a5ad 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61  eturn rc;.}..sta
3a5ae 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  tic sqlite3_modu
3a5af 6c 65 20 72 74 72 65 65 4d 6f 64 75 6c 65 20 3d  le rtreeModule =
3a5b0 20 7b 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20   {.  0,         
3a5b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5b2 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
3a5b3 20 72 74 72 65 65 43 72 65 61 74 65 2c 20 20 20   rtreeCreate,   
3a5b4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a5b5 78 43 72 65 61 74 65 20 2d 20 63 72 65 61 74 65  xCreate - create
3a5b6 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74   a table */.  rt
3a5b7 72 65 65 43 6f 6e 6e 65 63 74 2c 20 20 20 20 20  reeConnect,     
3a5b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6f            /* xCo
3a5b9 6e 6e 65 63 74 20 2d 20 63 6f 6e 6e 65 63 74 20  nnect - connect 
3a5ba 74 6f 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74  to an existing t
3a5bb 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65 42  able */.  rtreeB
3a5bc 65 73 74 49 6e 64 65 78 2c 20 20 20 20 20 20 20  estIndex,       
3a5bd 20 20 20 20 20 20 2f 2a 20 78 42 65 73 74 49 6e        /* xBestIn
3a5be 64 65 78 20 2d 20 44 65 74 65 72 6d 69 6e 65 20  dex - Determine 
3a5bf 73 65 61 72 63 68 20 73 74 72 61 74 65 67 79 20  search strategy 
3a5c0 2a 2f 0a 20 20 72 74 72 65 65 44 69 73 63 6f 6e  */.  rtreeDiscon
3a5c1 6e 65 63 74 2c 20 20 20 20 20 20 20 20 20 20 20  nect,           
3a5c2 20 2f 2a 20 78 44 69 73 63 6f 6e 6e 65 63 74 20   /* xDisconnect 
3a5c3 2d 20 44 69 73 63 6f 6e 6e 65 63 74 20 66 72 6f  - Disconnect fro
3a5c4 6d 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72  m a table */.  r
3a5c5 74 72 65 65 44 65 73 74 72 6f 79 2c 20 20 20 20  treeDestroy,    
3a5c6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44             /* xD
3a5c7 65 73 74 72 6f 79 20 2d 20 44 72 6f 70 20 61 20  estroy - Drop a 
3a5c8 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65  table */.  rtree
3a5c9 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Open,           
3a5ca 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
3a5cb 2d 20 6f 70 65 6e 20 61 20 63 75 72 73 6f 72 20  - open a cursor 
3a5cc 2a 2f 0a 20 20 72 74 72 65 65 43 6c 6f 73 65 2c  */.  rtreeClose,
3a5cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5ce 20 2f 2a 20 78 43 6c 6f 73 65 20 2d 20 63 6c 6f   /* xClose - clo
3a5cf 73 65 20 61 20 63 75 72 73 6f 72 20 2a 2f 0a 20  se a cursor */. 
3a5d0 20 72 74 72 65 65 46 69 6c 74 65 72 2c 20 20 20   rtreeFilter,   
3a5d1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a5d2 78 46 69 6c 74 65 72 20 2d 20 63 6f 6e 66 69 67  xFilter - config
3a5d3 75 72 65 20 73 63 61 6e 20 63 6f 6e 73 74 72 61  ure scan constra
3a5d4 69 6e 74 73 20 2a 2f 0a 20 20 72 74 72 65 65 4e  ints */.  rtreeN
3a5d5 65 78 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ext,            
3a5d6 20 20 20 20 20 20 2f 2a 20 78 4e 65 78 74 20 2d        /* xNext -
3a5d7 20 61 64 76 61 6e 63 65 20 61 20 63 75 72 73 6f   advance a curso
3a5d8 72 20 2a 2f 0a 20 20 72 74 72 65 65 45 6f 66 2c  r */.  rtreeEof,
3a5d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5da 20 20 20 2f 2a 20 78 45 6f 66 20 2a 2f 0a 20 20     /* xEof */.  
3a5db 72 74 72 65 65 43 6f 6c 75 6d 6e 2c 20 20 20 20  rtreeColumn,    
3a5dc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3a5dd 43 6f 6c 75 6d 6e 20 2d 20 72 65 61 64 20 64 61  Column - read da
3a5de 74 61 20 2a 2f 0a 20 20 72 74 72 65 65 52 6f 77  ta */.  rtreeRow
3a5df 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  id,             
3a5e0 20 20 20 20 2f 2a 20 78 52 6f 77 69 64 20 2d 20      /* xRowid - 
3a5e1 72 65 61 64 20 64 61 74 61 20 2a 2f 0a 20 20 72  read data */.  r
3a5e2 74 72 65 65 55 70 64 61 74 65 2c 20 20 20 20 20  treeUpdate,     
3a5e3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55             /* xU
3a5e4 70 64 61 74 65 20 2d 20 77 72 69 74 65 20 64 61  pdate - write da
3a5e5 74 61 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ta */.  0,      
3a5e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5e7 20 20 20 20 2f 2a 20 78 42 65 67 69 6e 20 2d 20      /* xBegin - 
3a5e8 62 65 67 69 6e 20 74 72 61 6e 73 61 63 74 69 6f  begin transactio
3a5e9 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  n */.  0,       
3a5ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5eb 20 20 20 2f 2a 20 78 53 79 6e 63 20 2d 20 73 79     /* xSync - sy
3a5ec 6e 63 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  nc transaction *
3a5ed 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
3a5ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5ef 2f 2a 20 78 43 6f 6d 6d 69 74 20 2d 20 63 6f 6d  /* xCommit - com
3a5f0 6d 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  mit transaction 
3a5f1 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  */.  0,         
3a5f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5f3 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b 20 2d 20   /* xRollback - 
3a5f4 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63  rollback transac
3a5f5 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20  tion */.  0,    
3a5f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5f7 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 64 46 75        /* xFindFu
3a5f8 6e 63 74 69 6f 6e 20 2d 20 66 75 6e 63 74 69 6f  nction - functio
3a5f9 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 20 2a 2f  n overloading */
3a5fa 0a 20 20 72 74 72 65 65 52 65 6e 61 6d 65 20 20  .  rtreeRename  
3a5fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a5fc 2a 20 78 52 65 6e 61 6d 65 20 2d 20 72 65 6e 61  * xRename - rena
3a5fd 6d 65 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  me the table */.
3a5fe 7d 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 72  };..static int r
3a5ff 74 72 65 65 53 71 6c 49 6e 69 74 28 0a 20 20 52  treeSqlInit(.  R
3a600 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
3a601 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
3a602 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
3a603 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
3a604 2a 7a 50 72 65 66 69 78 2c 20 0a 20 20 69 6e 74  *zPrefix, .  int
3a605 20 69 73 43 72 65 61 74 65 0a 29 7b 0a 20 20 69   isCreate.){.  i
3a606 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
3a607 4b 3b 0a 0a 20 20 23 64 65 66 69 6e 65 20 4e 5f  K;..  #define N_
3a608 53 54 41 54 45 4d 45 4e 54 20 39 0a 20 20 73 74  STATEMENT 9.  st
3a609 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
3a60a 2a 61 7a 53 71 6c 5b 4e 5f 53 54 41 54 45 4d 45  *azSql[N_STATEME
3a60b 4e 54 5d 20 3d 20 7b 0a 20 20 20 20 2f 2a 20 52  NT] = {.    /* R
3a60c 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74 68  ead and write th
3a60d 65 20 78 78 78 5f 6e 6f 64 65 20 74 61 62 6c 65  e xxx_node table
3a60e 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43 54 20   */.    "SELECT 
3a60f 64 61 74 61 20 46 52 4f 4d 20 27 25 71 27 2e 27  data FROM '%q'.'
3a610 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20 6e  %q_node' WHERE n
3a611 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20  odeno = :1",.   
3a612 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c   "INSERT OR REPL
3a613 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25  ACE INTO '%q'.'%
3a614 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45 53 28 3a  q_node' VALUES(:
3a615 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45  1, :2)",.    "DE
3a616 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27  LETE FROM '%q'.'
3a617 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20 6e  %q_node' WHERE n
3a618 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 0a 20 20  odeno = :1",..  
3a619 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
3a61a 69 74 65 20 74 68 65 20 78 78 78 5f 72 6f 77 69  ite the xxx_rowi
3a61b 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22  d table */.    "
3a61c 53 45 4c 45 43 54 20 6e 6f 64 65 6e 6f 20 46 52  SELECT nodeno FR
3a61d 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69  OM '%q'.'%q_rowi
3a61e 64 27 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d  d' WHERE rowid =
3a61f 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53 45 52   :1",.    "INSER
3a620 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e 54  T OR REPLACE INT
3a621 4f 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69 64  O '%q'.'%q_rowid
3a622 27 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a 32 29  ' VALUES(:1, :2)
3a623 22 2c 0a 20 20 20 20 22 44 45 4c 45 54 45 20 46  ",.    "DELETE F
3a624 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f 77  ROM '%q'.'%q_row
3a625 69 64 27 20 57 48 45 52 45 20 72 6f 77 69 64 20  id' WHERE rowid 
3a626 3d 20 3a 31 22 2c 0a 0a 20 20 20 20 2f 2a 20 52  = :1",..    /* R
3a627 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74 68  ead and write th
3a628 65 20 78 78 78 5f 70 61 72 65 6e 74 20 74 61 62  e xxx_parent tab
3a629 6c 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43  le */.    "SELEC
3a62a 54 20 70 61 72 65 6e 74 6e 6f 64 65 20 46 52 4f  T parentnode FRO
3a62b 4d 20 27 25 71 27 2e 27 25 71 5f 70 61 72 65 6e  M '%q'.'%q_paren
3a62c 74 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f 20  t' WHERE nodeno 
3a62d 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53 45  = :1",.    "INSE
3a62e 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e  RT OR REPLACE IN
3a62f 54 4f 20 27 25 71 27 2e 27 25 71 5f 70 61 72 65  TO '%q'.'%q_pare
3a630 6e 74 27 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a  nt' VALUES(:1, :
3a631 32 29 22 2c 0a 20 20 20 20 22 44 45 4c 45 54 45  2)",.    "DELETE
3a632 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 70   FROM '%q'.'%q_p
3a633 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e 6f 64  arent' WHERE nod
3a634 65 6e 6f 20 3d 20 3a 31 22 0a 20 20 7d 3b 0a 20  eno = :1".  };. 
3a635 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
3a636 61 70 70 53 74 6d 74 5b 4e 5f 53 54 41 54 45 4d  appStmt[N_STATEM
3a637 45 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a  ENT];.  int i;..
3a638 20 20 70 52 74 72 65 65 2d 3e 64 62 20 3d 20 64    pRtree->db = d
3a639 62 3b 0a 0a 20 20 69 66 28 20 69 73 43 72 65 61  b;..  if( isCrea
3a63a 74 65 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  te ){.    char *
3a63b 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74 65  zCreate = sqlite
3a63c 33 5f 6d 70 72 69 6e 74 66 28 0a 22 43 52 45 41  3_mprintf(."CREA
3a63d 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e  TE TABLE \"%w\".
3a63e 5c 22 25 77 5f 6e 6f 64 65 5c 22 28 6e 6f 64 65  \"%w_node\"(node
3a63f 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  no INTEGER PRIMA
3a640 52 59 20 4b 45 59 2c 20 64 61 74 61 20 42 4c 4f  RY KEY, data BLO
3a641 42 29 3b 22 0a 22 43 52 45 41 54 45 20 54 41 42  B);"."CREATE TAB
3a642 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f 72  LE \"%w\".\"%w_r
3a643 6f 77 69 64 5c 22 28 72 6f 77 69 64 20 49 4e 54  owid\"(rowid INT
3a644 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
3a645 2c 20 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52  , nodeno INTEGER
3a646 29 3b 22 0a 22 43 52 45 41 54 45 20 54 41 42 4c  );"."CREATE TABL
3a647 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f 70 61  E \"%w\".\"%w_pa
3a648 72 65 6e 74 5c 22 28 6e 6f 64 65 6e 6f 20 49 4e  rent\"(nodeno IN
3a649 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
3a64a 59 2c 20 70 61 72 65 6e 74 6e 6f 64 65 20 49 4e  Y, parentnode IN
3a64b 54 45 47 45 52 29 3b 22 0a 22 49 4e 53 45 52 54  TEGER);"."INSERT
3a64c 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 6e   INTO '%q'.'%q_n
3a64d 6f 64 65 27 20 56 41 4c 55 45 53 28 31 2c 20 7a  ode' VALUES(1, z
3a64e 65 72 6f 62 6c 6f 62 28 25 64 29 29 22 2c 0a 20  eroblob(%d))",. 
3a64f 20 20 20 20 20 7a 44 62 2c 20 7a 50 72 65 66 69       zDb, zPrefi
3a650 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c  x, zDb, zPrefix,
3a651 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c 20 7a   zDb, zPrefix, z
3a652 44 62 2c 20 7a 50 72 65 66 69 78 2c 20 70 52 74  Db, zPrefix, pRt
3a653 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 0a 20  ree->iNodeSize. 
3a654 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a     );.    if( !z
3a655 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 20 20  Create ){.      
3a656 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
3a657 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  MEM;.    }.    r
3a658 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
3a659 28 64 62 2c 20 7a 43 72 65 61 74 65 2c 20 30 2c  (db, zCreate, 0,
3a65a 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
3a65b 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61 74 65  te3_free(zCreate
3a65c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
3a65d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3a65e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
3a65f 20 7d 0a 20 20 7d 0a 0a 20 20 61 70 70 53 74 6d   }.  }..  appStm
3a660 74 5b 30 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[0] = &pRtree->
3a661 70 52 65 61 64 4e 6f 64 65 3b 0a 20 20 61 70 70  pReadNode;.  app
3a662 53 74 6d 74 5b 31 5d 20 3d 20 26 70 52 74 72 65  Stmt[1] = &pRtre
3a663 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20  e->pWriteNode;. 
3a664 20 61 70 70 53 74 6d 74 5b 32 5d 20 3d 20 26 70   appStmt[2] = &p
3a665 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f  Rtree->pDeleteNo
3a666 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 33 5d  de;.  appStmt[3]
3a667 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65 61   = &pRtree->pRea
3a668 64 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74 6d  dRowid;.  appStm
3a669 74 5b 34 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[4] = &pRtree->
3a66a 70 57 72 69 74 65 52 6f 77 69 64 3b 0a 20 20 61  pWriteRowid;.  a
3a66b 70 70 53 74 6d 74 5b 35 5d 20 3d 20 26 70 52 74  ppStmt[5] = &pRt
3a66c 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69  ree->pDeleteRowi
3a66d 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 36 5d 20  d;.  appStmt[6] 
3a66e 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65 61 64  = &pRtree->pRead
3a66f 50 61 72 65 6e 74 3b 0a 20 20 61 70 70 53 74 6d  Parent;.  appStm
3a670 74 5b 37 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[7] = &pRtree->
3a671 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20 20  pWriteParent;.  
3a672 61 70 70 53 74 6d 74 5b 38 5d 20 3d 20 26 70 52  appStmt[8] = &pR
3a673 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72  tree->pDeletePar
3a674 65 6e 74 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  ent;..  for(i=0;
3a675 20 69 3c 4e 5f 53 54 41 54 45 4d 45 4e 54 20 26   i<N_STATEMENT &
3a676 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  & rc==SQLITE_OK;
3a677 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20   i++){.    char 
3a678 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f  *zSql = sqlite3_
3a679 6d 70 72 69 6e 74 66 28 61 7a 53 71 6c 5b 69 5d  mprintf(azSql[i]
3a67a 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 29 3b  , zDb, zPrefix);
3a67b 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b  .    if( zSql ){
3a67c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
3a67d 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64  te3_prepare_v2(d
3a67e 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 61 70 70  b, zSql, -1, app
3a67f 53 74 6d 74 5b 69 5d 2c 20 30 29 3b 20 0a 20 20  Stmt[i], 0); .  
3a680 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
3a681 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
3a682 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
3a683 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a  te3_free(zSql);.
3a684 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
3a685 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
3a686 72 6f 75 74 69 6e 65 20 71 75 65 72 69 65 73 20  routine queries 
3a687 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
3a688 64 62 20 66 6f 72 20 74 68 65 20 70 61 67 65 2d  db for the page-
3a689 73 69 7a 65 20 75 73 65 64 20 62 79 0a 2a 2a 20  size used by.** 
3a68a 64 61 74 61 62 61 73 65 20 7a 44 62 2e 20 49 66  database zDb. If
3a68b 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65   successful, the
3a68c 20 70 61 67 65 2d 73 69 7a 65 20 69 6e 20 62 79   page-size in by
3a68d 74 65 73 20 69 73 20 77 72 69 74 74 65 6e 20 74  tes is written t
3a68e 6f 0a 2a 2a 20 2a 70 69 50 61 67 65 53 69 7a 65  o.** *piPageSize
3a68f 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72   and SQLITE_OK r
3a690 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69  eturned. Otherwi
3a691 73 65 2c 20 61 6e 64 20 61 6e 20 53 51 4c 69 74  se, and an SQLit
3a692 65 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65  e error .** code
3a693 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
3a694 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 50  .static int getP
3a695 61 67 65 53 69 7a 65 28 73 71 6c 69 74 65 33 20  ageSize(sqlite3 
3a696 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
3a697 2a 7a 44 62 2c 20 69 6e 74 20 2a 70 69 50 61 67  *zDb, int *piPag
3a698 65 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63  eSize){.  int rc
3a699 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
3a69a 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20  .  char *zSql;. 
3a69b 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
3a69c 53 74 6d 74 20 3d 20 30 3b 0a 0a 20 20 7a 53 71  Stmt = 0;..  zSq
3a69d 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  l = sqlite3_mpri
3a69e 6e 74 66 28 22 50 52 41 47 4d 41 20 25 51 2e 70  ntf("PRAGMA %Q.p
3a69f 61 67 65 5f 73 69 7a 65 22 2c 20 7a 44 62 29 3b  age_size", zDb);
3a6a0 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a  .  if( !zSql ){.
3a6a1 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3a6a2 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
3a6a3 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
3a6a4 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c  pare_v2(db, zSql
3a6a5 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29  , -1, &pStmt, 0)
3a6a6 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
3a6a7 28 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63  (zSql);.  if( rc
3a6a8 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
3a6a9 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
3a6aa 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  }..  if( SQLITE_
3a6ab 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
3a6ac 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20  p(pStmt) ){.    
3a6ad 2a 70 69 50 61 67 65 53 69 7a 65 20 3d 20 73 71  *piPageSize = sq
3a6ae 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
3a6af 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a  (pStmt, 0);.  }.
3a6b0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
3a6b1 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
3a6b2 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73  ;.}../* .** This
3a6b3 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65   function is the
3a6b4 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3a6b5 6f 66 20 62 6f 74 68 20 74 68 65 20 78 43 6f 6e  of both the xCon
3a6b6 6e 65 63 74 20 61 6e 64 20 78 43 72 65 61 74 65  nect and xCreate
3a6b7 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 6f 66 20 74  .** methods of t
3a6b8 68 65 20 72 2d 74 72 65 65 20 76 69 72 74 75 61  he r-tree virtua
3a6b9 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20  l table..**.**  
3a6ba 20 61 72 67 76 5b 30 5d 20 20 20 2d 3e 20 6d 6f   argv[0]   -> mo
3a6bb 64 75 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61  dule name.**   a
3a6bc 72 67 76 5b 31 5d 20 20 20 2d 3e 20 64 61 74 61  rgv[1]   -> data
3a6bd 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61  base name.**   a
3a6be 72 67 76 5b 32 5d 20 20 20 2d 3e 20 74 61 62 6c  rgv[2]   -> tabl
3a6bf 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61 72 67 76  e name.**   argv
3a6c0 5b 2e 2e 2e 5d 20 2d 3e 20 63 6f 6c 75 6d 6e 20  [...] -> column 
3a6c1 6e 61 6d 65 73 2e 2e 2e 0a 2a 2f 0a 73 74 61 74  names....*/.stat
3a6c2 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 69 74  ic int rtreeInit
3a6c3 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
3a6c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6c5 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
3a6c6 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
3a6c7 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20  /.  void *pAux, 
3a6c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6c9 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
3a6ca 66 20 74 68 65 20 52 54 52 45 45 5f 43 4f 4f 52  f the RTREE_COOR
3a6cb 44 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f  D_* constants */
3a6cc 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e  .  int argc, con
3a6cd 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61  st char *const*a
3a6ce 72 67 76 2c 20 20 20 2f 2a 20 50 61 72 61 6d 65  rgv,   /* Parame
3a6cf 74 65 72 73 20 74 6f 20 43 52 45 41 54 45 20 54  ters to CREATE T
3a6d0 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 2a  ABLE statement *
3a6d1 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  /.  sqlite3_vtab
3a6d2 20 2a 2a 70 70 56 74 61 62 2c 20 20 20 20 20 20   **ppVtab,      
3a6d3 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3a6d4 4e 65 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c  New virtual tabl
3a6d5 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  e */.  char **pz
3a6d6 45 72 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  Err,            
3a6d7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
3a6d8 54 3a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65  T: Error message
3a6d9 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 69 6e  , if any */.  in
3a6da 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20 20  t isCreate      
3a6db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6dc 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 78 43    /* True for xC
3a6dd 72 65 61 74 65 2c 20 66 61 6c 73 65 20 66 6f 72  reate, false for
3a6de 20 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a   xConnect */.){.
3a6df 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3a6e0 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 50 61 67  E_OK;.  int iPag
3a6e1 65 53 69 7a 65 20 3d 20 30 3b 0a 20 20 52 74 72  eSize = 0;.  Rtr
3a6e2 65 65 20 2a 70 52 74 72 65 65 3b 0a 20 20 69 6e  ee *pRtree;.  in
3a6e3 74 20 6e 44 62 3b 20 20 20 20 20 20 20 20 20 20  t nDb;          
3a6e4 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
3a6e5 20 73 74 72 69 6e 67 20 61 72 67 76 5b 31 5d 20   string argv[1] 
3a6e6 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20  */.  int nName; 
3a6e7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
3a6e8 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 61  ngth of string a
3a6e9 72 67 76 5b 32 5d 20 2a 2f 0a 20 20 69 6e 74 20  rgv[2] */.  int 
3a6ea 65 43 6f 6f 72 64 54 79 70 65 20 3d 20 28 69 6e  eCoordType = (in
3a6eb 74 29 70 41 75 78 3b 0a 0a 20 20 63 6f 6e 73 74  t)pAux;..  const
3a6ec 20 63 68 61 72 20 2a 61 45 72 72 4d 73 67 5b 5d   char *aErrMsg[]
3a6ed 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20 20   = {.    0,     
3a6ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a6f1 2a 20 30 20 2a 2f 0a 20 20 20 20 22 57 72 6f 6e  * 0 */.    "Wron
3a6f2 67 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  g number of colu
3a6f3 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65  mns for an rtree
3a6f4 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20 20   table",        
3a6f5 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 22 54 6f   /* 1 */.    "To
3a6f6 6f 20 66 65 77 20 63 6f 6c 75 6d 6e 73 20 66 6f  o few columns fo
3a6f7 72 20 61 6e 20 72 74 72 65 65 20 74 61 62 6c 65  r an rtree table
3a6f8 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
3a6f9 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 22     /* 2 */.    "
3a6fa 54 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73  Too many columns
3a6fb 20 66 6f 72 20 61 6e 20 72 74 72 65 65 20 74 61   for an rtree ta
3a6fc 62 6c 65 22 20 20 20 20 20 20 20 20 20 20 20 20  ble"            
3a6fd 20 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 7d       /* 3 */.  }
3a6fe 3b 0a 0a 20 20 69 6e 74 20 69 45 72 72 20 3d 20  ;..  int iErr = 
3a6ff 28 61 72 67 63 3c 36 29 20 3f 20 32 20 3a 20 61  (argc<6) ? 2 : a
3a700 72 67 63 3e 28 52 54 52 45 45 5f 4d 41 58 5f 44  rgc>(RTREE_MAX_D
3a701 49 4d 45 4e 53 49 4f 4e 53 2a 32 2b 34 29 20 3f  IMENSIONS*2+4) ?
3a702 20 33 20 3a 20 61 72 67 63 25 32 3b 0a 20 20 69   3 : argc%2;.  i
3a703 66 28 20 61 45 72 72 4d 73 67 5b 69 45 72 72 5d  f( aErrMsg[iErr]
3a704 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d   ){.    *pzErr =
3a705 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
3a706 28 22 25 73 22 2c 20 61 45 72 72 4d 73 67 5b 69  ("%s", aErrMsg[i
3a707 45 72 72 5d 29 3b 0a 20 20 20 20 72 65 74 75 72  Err]);.    retur
3a708 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
3a709 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74 50    }..  rc = getP
3a70a 61 67 65 53 69 7a 65 28 64 62 2c 20 61 72 67 76  ageSize(db, argv
3a70b 5b 31 5d 2c 20 26 69 50 61 67 65 53 69 7a 65 29  [1], &iPageSize)
3a70c 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
3a70d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
3a70e 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
3a70f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73  * Allocate the s
3a710 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
3a711 63 74 75 72 65 20 2a 2f 0a 20 20 6e 44 62 20 3d  cture */.  nDb =
3a712 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29   strlen(argv[1])
3a713 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c  ;.  nName = strl
3a714 65 6e 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 70  en(argv[2]);.  p
3a715 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
3a716 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  )sqlite3_malloc(
3a717 73 69 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e 44  sizeof(Rtree)+nD
3a718 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20 69 66  b+nName+2);.  if
3a719 28 20 21 70 52 74 72 65 65 20 29 7b 0a 20 20 20  ( !pRtree ){.   
3a71a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
3a71b 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  OMEM;.  }.  mems
3a71c 65 74 28 70 52 74 72 65 65 2c 20 30 2c 20 73 69  et(pRtree, 0, si
3a71d 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e 44 62 2b  zeof(Rtree)+nDb+
3a71e 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20 70 52 74 72  nName+2);.  pRtr
3a71f 65 65 2d 3e 6e 42 75 73 79 20 3d 20 31 3b 0a 20  ee->nBusy = 1;. 
3a720 20 70 52 74 72 65 65 2d 3e 62 61 73 65 2e 70 4d   pRtree->base.pM
3a721 6f 64 75 6c 65 20 3d 20 26 72 74 72 65 65 4d 6f  odule = &rtreeMo
3a722 64 75 6c 65 3b 0a 20 20 70 52 74 72 65 65 2d 3e  dule;.  pRtree->
3a723 7a 44 62 20 3d 20 28 63 68 61 72 20 2a 29 26 70  zDb = (char *)&p
3a724 52 74 72 65 65 5b 31 5d 3b 0a 20 20 70 52 74 72  Rtree[1];.  pRtr
3a725 65 65 2d 3e 7a 4e 61 6d 65 20 3d 20 26 70 52 74  ee->zName = &pRt
3a726 72 65 65 2d 3e 7a 44 62 5b 6e 44 62 2b 31 5d 3b  ree->zDb[nDb+1];
3a727 0a 20 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d 20  .  pRtree->nDim 
3a728 3d 20 28 61 72 67 63 2d 34 29 2f 32 3b 0a 20 20  = (argc-4)/2;.  
3a729 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
3a72a 72 43 65 6c 6c 20 3d 20 38 20 2b 20 70 52 74 72  rCell = 8 + pRtr
3a72b 65 65 2d 3e 6e 44 69 6d 2a 34 2a 32 3b 0a 20 20  ee->nDim*4*2;.  
3a72c 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79  pRtree->eCoordTy
3a72d 70 65 20 3d 20 65 43 6f 6f 72 64 54 79 70 65 3b  pe = eCoordType;
3a72e 0a 20 20 6d 65 6d 63 70 79 28 70 52 74 72 65 65  .  memcpy(pRtree
3a72f 2d 3e 7a 44 62 2c 20 61 72 67 76 5b 31 5d 2c 20  ->zDb, argv[1], 
3a730 6e 44 62 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  nDb);.  memcpy(p
3a731 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 61 72  Rtree->zName, ar
3a732 67 76 5b 32 5d 2c 20 6e 4e 61 6d 65 29 3b 0a 0a  gv[2], nName);..
3a733 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
3a734 74 68 65 20 6e 6f 64 65 20 73 69 7a 65 20 74 6f  the node size to
3a735 20 75 73 65 2e 20 42 79 20 64 65 66 61 75 6c 74   use. By default
3a736 2c 20 75 73 65 20 36 34 20 62 79 74 65 73 20 6c  , use 64 bytes l
3a737 65 73 73 20 74 68 61 6e 0a 20 20 2a 2a 20 74 68  ess than.  ** th
3a738 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2d  e database page-
3a739 73 69 7a 65 2e 20 54 68 69 73 20 65 6e 73 75 72  size. This ensur
3a73a 65 73 20 74 68 61 74 20 65 61 63 68 20 6e 6f 64  es that each nod
3a73b 65 20 69 73 20 73 74 6f 72 65 64 20 6f 6e 0a 20  e is stored on. 
3a73c 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64 61 74   ** a single dat
3a73d 61 62 61 73 65 20 70 61 67 65 2e 0a 20 20 2a 2a  abase page..  **
3a73e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74  .  ** If the dat
3a73f 61 62 61 73 64 20 70 61 67 65 2d 73 69 7a 65 20  abasd page-size 
3a740 69 73 20 73 6f 20 6c 61 72 67 65 20 74 68 61 74  is so large that
3a741 20 6d 6f 72 65 20 74 68 61 6e 20 52 54 52 45 45   more than RTREE
3a742 5f 4d 41 58 43 45 4c 4c 53 0a 20 20 2a 2a 20 65  _MAXCELLS.  ** e
3a743 6e 74 72 69 65 73 20 77 6f 75 6c 64 20 66 69 74  ntries would fit
3a744 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6e 6f 64   in a single nod
3a745 65 2c 20 75 73 65 20 61 20 73 6d 61 6c 6c 65 72  e, use a smaller
3a746 20 6e 6f 64 65 2d 73 69 7a 65 2e 0a 20 20 2a 2f   node-size..  */
3a747 0a 20 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  .  pRtree->iNode
3a748 53 69 7a 65 20 3d 20 69 50 61 67 65 53 69 7a 65  Size = iPageSize
3a749 2d 36 34 3b 0a 20 20 69 66 28 20 28 34 2b 70 52  -64;.  if( (4+pR
3a74a 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43  tree->nBytesPerC
3a74b 65 6c 6c 2a 52 54 52 45 45 5f 4d 41 58 43 45 4c  ell*RTREE_MAXCEL
3a74c 4c 53 29 3c 70 52 74 72 65 65 2d 3e 69 4e 6f 64  LS)<pRtree->iNod
3a74d 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 70 52 74  eSize ){.    pRt
3a74e 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 3d  ree->iNodeSize =
3a74f 20 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74 65   4+pRtree->nByte
3a750 73 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f 4d  sPerCell*RTREE_M
3a751 41 58 43 45 4c 4c 53 3b 0a 20 20 7d 0a 0a 20 20  AXCELLS;.  }..  
3a752 2f 2a 20 43 72 65 61 74 65 2f 43 6f 6e 6e 65 63  /* Create/Connec
3a753 74 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79  t to the underly
3a754 69 6e 67 20 72 65 6c 61 74 69 6f 6e 61 6c 20 64  ing relational d
3a755 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
3a756 49 66 0a 20 20 2a 2a 20 74 68 61 74 20 69 73 20  If.  ** that is 
3a757 73 75 63 63 65 73 73 66 75 6c 2c 20 63 61 6c 6c  successful, call
3a758 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
3a759 5f 76 74 61 62 28 29 20 74 6f 20 63 6f 6e 66 69  _vtab() to confi
3a75a 67 75 72 65 0a 20 20 2a 2a 20 74 68 65 20 72 2d  gure.  ** the r-
3a75b 74 72 65 65 20 74 61 62 6c 65 20 73 63 68 65 6d  tree table schem
3a75c 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 72  a..  */.  if( (r
3a75d 63 20 3d 20 72 74 72 65 65 53 71 6c 49 6e 69 74  c = rtreeSqlInit
3a75e 28 70 52 74 72 65 65 2c 20 64 62 2c 20 61 72 67  (pRtree, db, arg
3a75f 76 5b 31 5d 2c 20 61 72 67 76 5b 32 5d 2c 20 69  v[1], argv[2], i
3a760 73 43 72 65 61 74 65 29 29 20 29 7b 0a 20 20 20  sCreate)) ){.   
3a761 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65   *pzErr = sqlite
3a762 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20  3_mprintf("%s", 
3a763 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
3a764 62 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  b));.  }else{.  
3a765 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73    char *zSql = s
3a766 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
3a767 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 28 25  CREATE TABLE x(%
3a768 73 22 2c 20 61 72 67 76 5b 33 5d 29 3b 0a 20 20  s", argv[3]);.  
3a769 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20    char *zTmp;.  
3a76a 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66 6f    int ii;.    fo
3a76b 72 28 69 69 3d 34 3b 20 7a 53 71 6c 20 26 26 20  r(ii=4; zSql && 
3a76c 69 69 3c 61 72 67 63 3b 20 69 69 2b 2b 29 7b 0a  ii<argc; ii++){.
3a76d 20 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53 71        zTmp = zSq
3a76e 6c 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d 20  l;.      zSql = 
3a76f 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
3a770 22 25 73 2c 20 25 73 22 2c 20 7a 54 6d 70 2c 20  "%s, %s", zTmp, 
3a771 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20 20 20  argv[ii]);.     
3a772 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54   sqlite3_free(zT
3a773 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  mp);.    }.    i
3a774 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20  f( zSql ){.     
3a775 20 7a 54 6d 70 20 3d 20 7a 53 71 6c 3b 0a 20 20   zTmp = zSql;.  
3a776 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74      zSql = sqlit
3a777 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 29 3b  e3_mprintf("%s);
3a778 22 2c 20 7a 54 6d 70 29 3b 0a 20 20 20 20 20 20  ", zTmp);.      
3a779 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 6d  sqlite3_free(zTm
3a77a 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  p);.    }.    if
3a77b 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20  ( !zSql ){.     
3a77c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
3a77d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  EM;.    }else if
3a77e 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
3a77f 20 3d 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61   = sqlite3_decla
3a780 72 65 5f 76 74 61 62 28 64 62 2c 20 7a 53 71 6c  re_vtab(db, zSql
3a781 29 29 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45  )) ){.      *pzE
3a782 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  rr = sqlite3_mpr
3a783 69 6e 74 66 28 22 25 73 22 2c 20 73 71 6c 69 74  intf("%s", sqlit
3a784 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a  e3_errmsg(db));.
3a785 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
3a786 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
3a787 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
3a788 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70  ITE_OK ){.    *p
3a789 70 56 74 61 62 20 3d 20 28 73 71 6c 69 74 65 33  pVtab = (sqlite3
3a78a 5f 76 74 61 62 20 2a 29 70 52 74 72 65 65 3b 0a  _vtab *)pRtree;.
3a78b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 74 72    }else{.    rtr
3a78c 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  eeRelease(pRtree
3a78d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
3a78e 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d  rc;.}.../*.** Im
3a78f 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
3a790 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  a scalar functio
3a791 6e 20 74 68 61 74 20 64 65 63 6f 64 65 73 20 72  n that decodes r
3a792 2d 74 72 65 65 20 6e 6f 64 65 73 20 74 6f 0a 2a  -tree nodes to.*
3a793 2a 20 68 75 6d 61 6e 20 72 65 61 64 61 62 6c 65  * human readable
3a794 20 73 74 72 69 6e 67 73 2e 20 54 68 69 73 20 63   strings. This c
3a795 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 64  an be used for d
3a796 65 62 75 67 67 69 6e 67 20 61 6e 64 20 61 6e 61  ebugging and ana
3a797 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  lysis..**.** The
3a798 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3a799 20 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d   takes two argum
3a79a 65 6e 74 73 2c 20 61 20 62 6c 6f 62 20 6f 66 20  ents, a blob of 
3a79b 64 61 74 61 20 63 6f 6e 74 61 69 6e 69 6e 67 0a  data containing.
3a79c 2a 2a 20 61 6e 20 72 2d 74 72 65 65 20 6e 6f 64  ** an r-tree nod
3a79d 65 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65  e, and the numbe
3a79e 72 20 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 73 20  r of dimensions 
3a79f 74 68 65 20 72 2d 74 72 65 65 20 69 6e 64 65 78  the r-tree index
3a7a0 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 74 77 6f  es..** For a two
3a7a1 2d 64 69 6d 65 6e 73 69 6f 6e 61 6c 20 72 2d 74  -dimensional r-t
3a7a2 72 65 65 20 73 74 72 75 63 74 75 72 65 20 63 61  ree structure ca
3a7a3 6c 6c 65 64 20 22 72 74 22 2c 20 74 6f 20 64 65  lled "rt", to de
3a7a4 73 65 72 69 61 6c 69 7a 65 0a 2a 2a 20 61 6c 6c  serialize.** all
3a7a5 20 6e 6f 64 65 73 2c 20 61 20 73 74 61 74 65 6d   nodes, a statem
3a7a6 65 6e 74 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20  ent like:.**.** 
3a7a7 20 20 53 45 4c 45 43 54 20 72 74 72 65 65 6e 6f    SELECT rtreeno
3a7a8 64 65 28 32 2c 20 64 61 74 61 29 20 46 52 4f 4d  de(2, data) FROM
3a7a9 20 72 74 5f 6e 6f 64 65 3b 0a 2a 2a 0a 2a 2a 20   rt_node;.**.** 
3a7aa 54 68 65 20 68 75 6d 61 6e 20 72 65 61 64 61 62  The human readab
3a7ab 6c 65 20 73 74 72 69 6e 67 20 74 61 6b 65 73 20  le string takes 
3a7ac 74 68 65 20 66 6f 72 6d 20 6f 66 20 61 20 54 63  the form of a Tc
3a7ad 6c 20 6c 69 73 74 20 77 69 74 68 20 6f 6e 65 0a  l list with one.
3a7ae 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63  ** entry for eac
3a7af 68 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72 2d  h cell in the r-
3a7b0 74 72 65 65 20 6e 6f 64 65 2e 20 45 61 63 68 20  tree node. Each 
3a7b1 65 6e 74 72 79 20 69 73 20 69 74 73 65 6c 66 20  entry is itself 
3a7b2 61 0a 2a 2a 20 6c 69 73 74 2c 20 63 6f 6e 74 61  a.** list, conta
3a7b3 69 6e 69 6e 67 20 74 68 65 20 38 2d 62 79 74 65  ining the 8-byte
3a7b4 20 72 6f 77 69 64 2f 70 61 67 65 6e 6f 20 66 6f   rowid/pageno fo
3a7b5 6c 6c 6f 77 65 64 20 62 79 20 74 68 65 20 0a 2a  llowed by the .*
3a7b6 2a 20 3c 6e 75 6d 2d 64 69 6d 65 6e 73 69 6f 6e  * <num-dimension
3a7b7 3e 2a 32 20 63 6f 6f 72 64 69 6e 61 74 65 73 2e  >*2 coordinates.
3a7b8 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3a7b9 72 74 72 65 65 6e 6f 64 65 28 73 71 6c 69 74 65  rtreenode(sqlite
3a7ba 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20  3_context *ctx, 
3a7bb 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
3a7bc 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
3a7bd 7b 0a 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20  {.  char *zText 
3a7be 3d 20 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65  = 0;.  RtreeNode
3a7bf 20 6e 6f 64 65 3b 0a 20 20 52 74 72 65 65 20 74   node;.  Rtree t
3a7c0 72 65 65 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a  ree;.  int ii;..
3a7c1 20 20 6d 65 6d 73 65 74 28 26 6e 6f 64 65 2c 20    memset(&node, 
3a7c2 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 4e  0, sizeof(RtreeN
3a7c3 6f 64 65 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28  ode));.  memset(
3a7c4 26 74 72 65 65 2c 20 30 2c 20 73 69 7a 65 6f 66  &tree, 0, sizeof
3a7c5 28 52 74 72 65 65 29 29 3b 0a 20 20 74 72 65 65  (Rtree));.  tree
3a7c6 2e 6e 44 69 6d 20 3d 20 73 71 6c 69 74 65 33 5f  .nDim = sqlite3_
3a7c7 76 61 6c 75 65 5f 69 6e 74 28 61 70 41 72 67 5b  value_int(apArg[
3a7c8 30 5d 29 3b 0a 20 20 74 72 65 65 2e 6e 42 79 74  0]);.  tree.nByt
3a7c9 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20 2b 20  esPerCell = 8 + 
3a7ca 38 20 2a 20 74 72 65 65 2e 6e 44 69 6d 3b 0a 20  8 * tree.nDim;. 
3a7cb 20 6e 6f 64 65 2e 7a 44 61 74 61 20 3d 20 28 75   node.zData = (u
3a7cc 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  8 *)sqlite3_valu
3a7cd 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 31 5d 29  e_blob(apArg[1])
3a7ce 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  ;..  for(ii=0; i
3a7cf 69 3c 4e 43 45 4c 4c 28 26 6e 6f 64 65 29 3b 20  i<NCELL(&node); 
3a7d0 69 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20  ii++){.    char 
3a7d1 7a 43 65 6c 6c 5b 35 31 32 5d 3b 0a 20 20 20 20  zCell[512];.    
3a7d2 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20  int nCell = 0;. 
3a7d3 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c     RtreeCell cel
3a7d4 6c 3b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 0a  l;.    int jj;..
3a7d5 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
3a7d6 26 74 72 65 65 2c 20 26 6e 6f 64 65 2c 20 69 69  &tree, &node, ii
3a7d7 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 73 71  , &cell);.    sq
3a7d8 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35  lite3_snprintf(5
3a7d9 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b  12-nCell,&zCell[
3a7da 6e 43 65 6c 6c 5d 2c 22 25 64 22 2c 20 63 65 6c  nCell],"%d", cel
3a7db 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 6e  l.iRowid);.    n
3a7dc 43 65 6c 6c 20 3d 20 73 74 72 6c 65 6e 28 7a 43  Cell = strlen(zC
3a7dd 65 6c 6c 29 3b 0a 20 20 20 20 66 6f 72 28 6a 6a  ell);.    for(jj
3a7de 3d 30 3b 20 6a 6a 3c 74 72 65 65 2e 6e 44 69 6d  =0; jj<tree.nDim
3a7df 2a 32 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20  *2; jj++){.     
3a7e0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
3a7e1 66 28 35 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65  f(512-nCell,&zCe
3a7e2 6c 6c 5b 6e 43 65 6c 6c 5d 2c 22 20 25 66 22 2c  ll[nCell]," %f",
3a7e3 28 64 6f 75 62 6c 65 29 63 65 6c 6c 2e 61 43 6f  (double)cell.aCo
3a7e4 6f 72 64 5b 6a 6a 5d 2e 66 29 3b 0a 20 20 20 20  ord[jj].f);.    
3a7e5 20 20 6e 43 65 6c 6c 20 3d 20 73 74 72 6c 65 6e    nCell = strlen
3a7e6 28 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 0a  (zCell);.    }..
3a7e7 20 20 20 20 69 66 28 20 7a 54 65 78 74 20 29 7b      if( zText ){
3a7e8 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 65  .      char *zTe
3a7e9 78 74 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f  xtNew = sqlite3_
3a7ea 6d 70 72 69 6e 74 66 28 22 25 73 20 7b 25 73 7d  mprintf("%s {%s}
3a7eb 22 2c 20 7a 54 65 78 74 2c 20 7a 43 65 6c 6c 29  ", zText, zCell)
3a7ec 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
3a7ed 66 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20 20  free(zText);.   
3a7ee 20 20 20 7a 54 65 78 74 20 3d 20 7a 54 65 78 74     zText = zText
3a7ef 4e 65 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  New;.    }else{.
3a7f0 20 20 20 20 20 20 7a 54 65 78 74 20 3d 20 73 71        zText = sq
3a7f1 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 7b  lite3_mprintf("{
3a7f2 25 73 7d 22 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20  %s}", zCell);.  
3a7f3 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 73 71 6c    }.  }.  .  sql
3a7f4 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
3a7f5 28 63 74 78 2c 20 7a 54 65 78 74 2c 20 2d 31 2c  (ctx, zText, -1,
3a7f6 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
3a7f7 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  }..static void r
3a7f8 74 72 65 65 64 65 70 74 68 28 73 71 6c 69 74 65  treedepth(sqlite
3a7f9 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20  3_context *ctx, 
3a7fa 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
3a7fb 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
3a7fc 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  {.  if( sqlite3_
3a7fd 76 61 6c 75 65 5f 74 79 70 65 28 61 70 41 72 67  value_type(apArg
3a7fe 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  [0])!=SQLITE_BLO
3a7ff 42 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33  B .   || sqlite3
3a800 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 70 41  _value_bytes(apA
3a801 72 67 5b 30 5d 29 3c 32 0a 20 20 29 7b 0a 20 20  rg[0])<2.  ){.  
3a802 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3a803 5f 65 72 72 6f 72 28 63 74 78 2c 20 22 49 6e 76  _error(ctx, "Inv
3a804 61 6c 69 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  alid argument to
3a805 20 72 74 72 65 65 64 65 70 74 68 28 29 22 2c 20   rtreedepth()", 
3a806 2d 31 29 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20  -1); .  }else{. 
3a807 20 20 20 75 38 20 2a 7a 42 6c 6f 62 20 3d 20 28     u8 *zBlob = (
3a808 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  u8 *)sqlite3_val
3a809 75 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 30 5d  ue_blob(apArg[0]
3a80a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
3a80b 65 73 75 6c 74 5f 69 6e 74 28 63 74 78 2c 20 72  esult_int(ctx, r
3a80c 65 61 64 49 6e 74 31 36 28 7a 42 6c 6f 62 29 29  eadInt16(zBlob))
3a80d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
3a80e 65 67 69 73 74 65 72 20 74 68 65 20 72 2d 74 72  egister the r-tr
3a80f 65 65 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 64  ee module with d
3a810 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64  atabase handle d
3a811 62 2e 20 54 68 69 73 20 63 72 65 61 74 65 73 20  b. This creates 
3a812 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74  the.** virtual t
3a813 61 62 6c 65 20 6d 6f 64 75 6c 65 20 22 72 74 72  able module "rtr
3a814 65 65 22 20 61 6e 64 20 74 68 65 20 64 65 62 75  ee" and the debu
3a815 67 67 69 6e 67 2f 61 6e 61 6c 79 73 69 73 20 73  gging/analysis s
3a816 63 61 6c 61 72 20 0a 2a 2a 20 66 75 6e 63 74 69  calar .** functi
3a817 6f 6e 20 22 72 74 72 65 65 6e 6f 64 65 22 2e 0a  on "rtreenode"..
3a818 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
3a819 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 74 72  E int sqlite3Rtr
3a81a 65 65 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a  eeInit(sqlite3 *
3a81b 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  db){.  int rc = 
3a81c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66  SQLITE_OK;..  if
3a81d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3a81e 29 7b 0a 20 20 20 20 69 6e 74 20 75 74 66 38 20  ){.    int utf8 
3a81f 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20  = SQLITE_UTF8;. 
3a820 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
3a821 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
3a822 64 62 2c 20 22 72 74 72 65 65 6e 6f 64 65 22 2c  db, "rtreenode",
3a823 20 32 2c 20 75 74 66 38 2c 20 30 2c 20 72 74 72   2, utf8, 0, rtr
3a824 65 65 6e 6f 64 65 2c 20 30 2c 20 30 29 3b 0a 20  eenode, 0, 0);. 
3a825 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
3a826 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e  ITE_OK ){.    in
3a827 74 20 75 74 66 38 20 3d 20 53 51 4c 49 54 45 5f  t utf8 = SQLITE_
3a828 55 54 46 38 3b 0a 20 20 20 20 72 63 20 3d 20 73  UTF8;.    rc = s
3a829 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
3a82a 6e 63 74 69 6f 6e 28 64 62 2c 20 22 72 74 72 65  nction(db, "rtre
3a82b 65 64 65 70 74 68 22 2c 20 31 2c 20 75 74 66 38  edepth", 1, utf8
3a82c 2c 20 30 2c 72 74 72 65 65 64 65 70 74 68 2c 20  , 0,rtreedepth, 
3a82d 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  0, 0);.  }.  if(
3a82e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3a82f 7b 0a 20 20 20 20 76 6f 69 64 20 2a 63 20 3d 20  {.    void *c = 
3a830 28 76 6f 69 64 20 2a 29 52 54 52 45 45 5f 43 4f  (void *)RTREE_CO
3a831 4f 52 44 5f 52 45 41 4c 33 32 3b 0a 20 20 20 20  ORD_REAL32;.    
3a832 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
3a833 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 64 62  ate_module_v2(db
3a834 2c 20 22 72 74 72 65 65 22 2c 20 26 72 74 72 65  , "rtree", &rtre
3a835 65 4d 6f 64 75 6c 65 2c 20 63 2c 20 30 29 3b 0a  eModule, c, 0);.
3a836 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
3a837 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 76  LITE_OK ){.    v
3a838 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a  oid *c = (void *
3a839 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54  )RTREE_COORD_INT
3a83a 33 32 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  32;.    rc = sql
3a83b 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
3a83c 6c 65 5f 76 32 28 64 62 2c 20 22 72 74 72 65 65  le_v2(db, "rtree
3a83d 5f 69 33 32 22 2c 20 26 72 74 72 65 65 4d 6f 64  _i32", &rtreeMod
3a83e 75 6c 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d 0a  ule, c, 0);.  }.
3a83f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3a840 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52  .#if !SQLITE_COR
3a841 45 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  E.SQLITE_API int
3a842 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69   sqlite3_extensi
3a843 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74  on_init(.  sqlit
3a844 65 33 20 2a 64 62 2c 0a 20 20 63 68 61 72 20 2a  e3 *db,.  char *
3a845 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e  *pzErrMsg,.  con
3a846 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
3a847 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b  outines *pApi.){
3a848 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53  .  SQLITE_EXTENS
3a849 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0a  ION_INIT2(pApi).
3a84a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
3a84b 52 74 72 65 65 49 6e 69 74 28 64 62 29 3b 0a 7d  RtreeInit(db);.}
3a84c 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a  .#endif..#endif.
3a84d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3a84e 20 45 6e 64 20 6f 66 20 72 74 72 65 65 2e 63 20   End of rtree.c 
3a84f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a851 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3a852 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3a853 20 42 65 67 69 6e 20 66 69 6c 65 20 69 63 75 2e   Begin file icu.
3a854 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
3a855 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a856 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3a857 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20  ./*.** 2007 May 
3a858 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
3a859 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
3a85a 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
3a85b 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3a85c 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
3a85d 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
3a85e 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
3a85f 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
3a860 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
3a861 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
3a862 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
3a863 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
3a864 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
3a865 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
3a866 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
3a867 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
3a868 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
3a869 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
3a86a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a86b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a86c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a86d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a86e 0a 2a 2a 20 24 49 64 3a 20 69 63 75 2e 63 2c 76  .** $Id: icu.c,v
3a86f 20 31 2e 37 20 32 30 30 37 2f 31 32 2f 31 33 20   1.7 2007/12/13 
3a870 32 31 3a 35 34 3a 31 31 20 64 72 68 20 45 78 70  21:54:11 drh Exp
3a871 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69   $.**.** This fi
3a872 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e  le implements an
3a873 20 69 6e 74 65 67 72 61 74 69 6f 6e 20 62 65 74   integration bet
3a874 77 65 65 6e 20 74 68 65 20 49 43 55 20 6c 69 62  ween the ICU lib
3a875 72 61 72 79 20 0a 2a 2a 20 28 22 49 6e 74 65 72  rary .** ("Inter
3a876 6e 61 74 69 6f 6e 61 6c 20 43 6f 6d 70 6f 6e 65  national Compone
3a877 6e 74 73 20 66 6f 72 20 55 6e 69 63 6f 64 65 22  nts for Unicode"
3a878 2c 20 61 6e 20 6f 70 65 6e 2d 73 6f 75 72 63 65  , an open-source
3a879 20 6c 69 62 72 61 72 79 20 0a 2a 2a 20 66 6f 72   library .** for
3a87a 20 68 61 6e 64 6c 69 6e 67 20 75 6e 69 63 6f 64   handling unicod
3a87b 65 20 64 61 74 61 29 20 61 6e 64 20 53 51 4c 69  e data) and SQLi
3a87c 74 65 2e 20 54 68 65 20 69 6e 74 65 67 72 61 74  te. The integrat
3a87d 69 6f 6e 20 75 73 65 73 20 0a 2a 2a 20 49 43 55  ion uses .** ICU
3a87e 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20   to provide the 
3a87f 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 20 53 51 4c  following to SQL
3a880 69 74 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41  ite:.**.**   * A
3a881 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
3a882 20 6f 66 20 74 68 65 20 53 51 4c 20 72 65 67 65   of the SQL rege
3a883 78 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 61  xp() function (a
3a884 6e 64 20 68 65 6e 63 65 20 52 45 47 45 58 50 0a  nd hence REGEXP.
3a885 2a 2a 20 20 20 20 20 6f 70 65 72 61 74 6f 72 29  **     operator)
3a886 20 75 73 69 6e 67 20 74 68 65 20 49 43 55 20 75   using the ICU u
3a887 72 65 67 65 78 5f 58 58 28 29 20 41 50 49 73 2e  regex_XX() APIs.
3a888 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6d 70 6c 65  .**.**   * Imple
3a889 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  mentations of th
3a88a 65 20 53 51 4c 20 73 63 61 6c 61 72 20 75 70 70  e SQL scalar upp
3a88b 65 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 29  er() and lower()
3a88c 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 20   functions.**   
3a88d 20 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70 69    for case mappi
3a88e 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6e  ng..**.**   * In
3a88f 74 65 67 72 61 74 69 6f 6e 20 6f 66 20 49 43 55  tegration of ICU
3a890 20 61 6e 64 20 53 51 4c 69 74 65 20 63 6f 6c 6c   and SQLite coll
3a891 61 74 69 6f 6e 20 73 65 71 65 6e 63 65 73 2e 0a  ation seqences..
3a892 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d 70  **.**   * An imp
3a893 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
3a894 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72  he LIKE operator
3a895 20 74 68 61 74 20 75 73 65 73 20 49 43 55 20 74   that uses ICU t
3a896 6f 20 0a 2a 2a 20 20 20 20 20 70 72 6f 76 69 64  o .**     provid
3a897 65 20 63 61 73 65 2d 69 6e 64 65 70 65 6e 64 65  e case-independe
3a898 6e 74 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2f 0a  nt matching..*/.
3a899 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
3a89a 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
3a89b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
3a89c 42 4c 45 5f 49 43 55 29 0a 0a 2f 2a 20 49 6e 63  BLE_ICU)../* Inc
3a89d 6c 75 64 65 20 49 43 55 20 68 65 61 64 65 72 73  lude ICU headers
3a89e 20 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e   */.#include <un
3a89f 69 63 6f 64 65 2f 75 74 79 70 65 73 2e 68 3e 0a  icode/utypes.h>.
3a8a0 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64  #include <unicod
3a8a1 65 2f 75 72 65 67 65 78 2e 68 3e 0a 23 69 6e 63  e/uregex.h>.#inc
3a8a2 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 73  lude <unicode/us
3a8a3 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64  tring.h>.#includ
3a8a4 65 20 3c 75 6e 69 63 6f 64 65 2f 75 63 6f 6c 2e  e <unicode/ucol.
3a8a5 68 3e 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  h>...#ifndef SQL
3a8a6 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54  ITE_CORE.  SQLIT
3a8a7 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54  E_EXTENSION_INIT
3a8a8 31 0a 23 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a  1.#else.#endif..
3a8a9 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65  /*.** Maximum le
3a8aa 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73 29 20  ngth (in bytes) 
3a8ab 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20 69  of the pattern i
3a8ac 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42  n a LIKE or GLOB
3a8ad 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f  .** operator..*/
3a8ae 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
3a8af 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MAX_LIKE_PATTERN
3a8b0 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65  _LENGTH.# define
3a8b1 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45   SQLITE_MAX_LIKE
3a8b2 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20  _PATTERN_LENGTH 
3a8b3 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  50000.#endif../*
3a8b4 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 6f 66 20 73  .** Version of s
3a8b5 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 74 68  qlite3_free() th
3a8b6 61 74 20 69 73 20 61 6c 77 61 79 73 20 61 20 66  at is always a f
3a8b7 75 6e 63 74 69 6f 6e 2c 20 6e 65 76 65 72 20 61  unction, never a
3a8b8 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69   macro..*/.stati
3a8b9 63 20 76 6f 69 64 20 78 46 72 65 65 28 76 6f 69  c void xFree(voi
3a8ba 64 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  d *p){.  sqlite3
3a8bb 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  _free(p);.}../*.
3a8bc 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20 55  ** Compare two U
3a8bd 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 6f 72  TF-8 strings for
3a8be 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 65 20   equality where 
3a8bf 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67  the first string
3a8c0 20 69 73 0a 2a 2a 20 61 20 22 4c 49 4b 45 22 20   is.** a "LIKE" 
3a8c1 65 78 70 72 65 73 73 69 6f 6e 2e 20 52 65 74 75  expression. Retu
3a8c2 72 6e 20 74 72 75 65 20 28 31 29 20 69 66 20 74  rn true (1) if t
3a8c3 68 65 79 20 61 72 65 20 74 68 65 20 73 61 6d 65  hey are the same
3a8c4 20 61 6e 64 20 0a 2a 2a 20 66 61 6c 73 65 20 28   and .** false (
3a8c5 30 29 20 69 66 20 74 68 65 79 20 61 72 65 20 64  0) if they are d
3a8c6 69 66 66 65 72 65 6e 74 2e 0a 2a 2f 0a 73 74 61  ifferent..*/.sta
3a8c7 74 69 63 20 69 6e 74 20 69 63 75 4c 69 6b 65 43  tic int icuLikeC
3a8c8 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 20  ompare(.  const 
3a8c9 75 69 6e 74 38 5f 74 20 2a 7a 50 61 74 74 65 72  uint8_t *zPatter
3a8ca 6e 2c 20 20 20 2f 2a 20 4c 49 4b 45 20 70 61 74  n,   /* LIKE pat
3a8cb 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tern */.  const 
3a8cc 75 69 6e 74 38 5f 74 20 2a 7a 53 74 72 69 6e 67  uint8_t *zString
3a8cd 2c 20 20 20 20 2f 2a 20 54 68 65 20 55 54 46 2d  ,    /* The UTF-
3a8ce 38 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70  8 string to comp
3a8cf 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20  are against */. 
3a8d0 20 63 6f 6e 73 74 20 55 43 68 61 72 33 32 20 75   const UChar32 u
3a8d1 45 73 63 20 20 20 20 20 20 20 20 20 2f 2a 20 54  Esc         /* T
3a8d2 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63  he escape charac
3a8d3 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74  ter */.){.  stat
3a8d4 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 4d 41 54  ic const int MAT
3a8d5 43 48 5f 4f 4e 45 20 3d 20 28 55 43 68 61 72 33  CH_ONE = (UChar3
3a8d6 32 29 27 5f 27 3b 0a 20 20 73 74 61 74 69 63 20  2)'_';.  static 
3a8d7 63 6f 6e 73 74 20 69 6e 74 20 4d 41 54 43 48 5f  const int MATCH_
3a8d8 41 4c 4c 20 3d 20 28 55 43 68 61 72 33 32 29 27  ALL = (UChar32)'
3a8d9 25 27 3b 0a 0a 20 20 69 6e 74 20 69 50 61 74 74  %';..  int iPatt
3a8da 65 72 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  ern = 0;       /
3a8db 2a 20 43 75 72 72 65 6e 74 20 62 79 74 65 20 69  * Current byte i
3a8dc 6e 64 65 78 20 69 6e 20 7a 50 61 74 74 65 72 6e  ndex in zPattern
3a8dd 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 72 69 6e   */.  int iStrin
3a8de 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  g = 0;        /*
3a8df 20 43 75 72 72 65 6e 74 20 62 79 74 65 20 69 6e   Current byte in
3a8e0 64 65 78 20 69 6e 20 7a 53 74 72 69 6e 67 20 2a  dex in zString *
3a8e1 2f 0a 0a 20 20 69 6e 74 20 70 72 65 76 45 73 63  /..  int prevEsc
3a8e2 61 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ape = 0;     /* 
3a8e3 54 72 75 65 20 69 66 20 74 68 65 20 70 72 65 76  True if the prev
3a8e4 69 6f 75 73 20 63 68 61 72 61 63 74 65 72 20 77  ious character w
3a8e5 61 73 20 75 45 73 63 20 2a 2f 0a 0a 20 20 77 68  as uEsc */..  wh
3a8e6 69 6c 65 28 20 7a 50 61 74 74 65 72 6e 5b 69 50  ile( zPattern[iP
3a8e7 61 74 74 65 72 6e 5d 21 3d 30 20 29 7b 0a 0a 20  attern]!=0 ){.. 
3a8e8 20 20 20 2f 2a 20 52 65 61 64 20 28 61 6e 64 20     /* Read (and 
3a8e9 63 6f 6e 73 75 6d 65 29 20 74 68 65 20 6e 65 78  consume) the nex
3a8ea 74 20 63 68 61 72 61 63 74 65 72 20 66 72 6f 6d  t character from
3a8eb 20 74 68 65 20 69 6e 70 75 74 20 70 61 74 74 65   the input patte
3a8ec 72 6e 2e 20 2a 2f 0a 20 20 20 20 55 43 68 61 72  rn. */.    UChar
3a8ed 33 32 20 75 50 61 74 74 65 72 6e 3b 0a 20 20 20  32 uPattern;.   
3a8ee 20 55 38 5f 4e 45 58 54 5f 55 4e 53 41 46 45 28   U8_NEXT_UNSAFE(
3a8ef 7a 50 61 74 74 65 72 6e 2c 20 69 50 61 74 74 65  zPattern, iPatte
3a8f0 72 6e 2c 20 75 50 61 74 74 65 72 6e 29 3b 0a 20  rn, uPattern);. 
3a8f1 20 20 20 61 73 73 65 72 74 28 75 50 61 74 74 65     assert(uPatte
3a8f2 72 6e 21 3d 30 29 3b 0a 0a 20 20 20 20 2f 2a 20  rn!=0);..    /* 
3a8f3 54 68 65 72 65 20 61 72 65 20 6e 6f 77 20 34 20  There are now 4 
3a8f4 70 6f 73 73 69 62 69 6c 69 74 69 65 73 3a 0a 20  possibilities:. 
3a8f5 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
3a8f6 20 31 2e 20 75 50 61 74 74 65 72 6e 20 69 73 20   1. uPattern is 
3a8f7 61 6e 20 75 6e 65 73 63 61 70 65 64 20 6d 61 74  an unescaped mat
3a8f8 63 68 2d 61 6c 6c 20 63 68 61 72 61 63 74 65 72  ch-all character
3a8f9 20 22 25 22 2c 0a 20 20 20 20 2a 2a 20 20 20 20   "%",.    **    
3a8fa 20 32 2e 20 75 50 61 74 74 65 72 6e 20 69 73 20   2. uPattern is 
3a8fb 61 6e 20 75 6e 65 73 63 61 70 65 64 20 6d 61 74  an unescaped mat
3a8fc 63 68 2d 6f 6e 65 20 63 68 61 72 61 63 74 65 72  ch-one character
3a8fd 20 22 5f 22 2c 0a 20 20 20 20 2a 2a 20 20 20 20   "_",.    **    
3a8fe 20 33 2e 20 75 50 61 74 74 65 72 6e 20 69 73 20   3. uPattern is 
3a8ff 61 6e 20 75 6e 65 73 63 61 70 65 64 20 65 73 63  an unescaped esc
3a900 61 70 65 20 63 68 61 72 61 63 74 65 72 2c 20 6f  ape character, o
3a901 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 34 2e 20  r.    **     4. 
3a902 75 50 61 74 74 65 72 6e 20 69 73 20 74 6f 20 62  uPattern is to b
3a903 65 20 68 61 6e 64 6c 65 64 20 61 73 20 61 6e 20  e handled as an 
3a904 6f 72 64 69 6e 61 72 79 20 63 68 61 72 61 63 74  ordinary charact
3a905 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  er.    */.    if
3a906 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26  ( !prevEscape &&
3a907 20 75 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43 48   uPattern==MATCH
3a908 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 20 20 2f 2a  _ALL ){.      /*
3a909 20 43 61 73 65 20 31 2e 20 2a 2f 0a 20 20 20 20   Case 1. */.    
3a90a 20 20 75 69 6e 74 38 5f 74 20 63 3b 0a 0a 20 20    uint8_t c;..  
3a90b 20 20 20 20 2f 2a 20 53 6b 69 70 20 61 6e 79 20      /* Skip any 
3a90c 4d 41 54 43 48 5f 41 4c 4c 20 6f 72 20 4d 41 54  MATCH_ALL or MAT
3a90d 43 48 5f 4f 4e 45 20 63 68 61 72 61 63 74 65 72  CH_ONE character
3a90e 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 0a  s that follow a.
3a90f 20 20 20 20 20 20 2a 2a 20 4d 41 54 43 48 5f 41        ** MATCH_A
3a910 4c 4c 2e 20 46 6f 72 20 65 61 63 68 20 4d 41 54  LL. For each MAT
3a911 43 48 5f 4f 4e 45 2c 20 73 6b 69 70 20 6f 6e 65  CH_ONE, skip one
3a912 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68   character in th
3a913 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 65 73 74  e .      ** test
3a914 20 73 74 72 69 6e 67 2e 0a 20 20 20 20 20 20 2a   string..      *
3a915 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28  /.      while( (
3a916 63 3d 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74  c=zPattern[iPatt
3a917 65 72 6e 5d 29 20 3d 3d 20 4d 41 54 43 48 5f 41  ern]) == MATCH_A
3a918 4c 4c 20 7c 7c 20 63 20 3d 3d 20 4d 41 54 43 48  LL || c == MATCH
3a919 5f 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20  _ONE ){.        
3a91a 69 66 28 20 63 3d 3d 4d 41 54 43 48 5f 4f 4e 45  if( c==MATCH_ONE
3a91b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
3a91c 28 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e  ( zString[iStrin
3a91d 67 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  g]==0 ) return 0
3a91e 3b 0a 20 20 20 20 20 20 20 20 20 20 55 38 5f 46  ;.          U8_F
3a91f 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74 72  WD_1_UNSAFE(zStr
3a920 69 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a 20  ing, iString);. 
3a921 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3a922 20 69 50 61 74 74 65 72 6e 2b 2b 3b 0a 20 20 20   iPattern++;.   
3a923 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
3a924 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72  zPattern[iPatter
3a925 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  n]==0 ) return 1
3a926 3b 0a 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ;..      while( 
3a927 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d  zString[iString]
3a928 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
3a929 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28 26  icuLikeCompare(&
3a92a 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72  zPattern[iPatter
3a92b 6e 5d 2c 20 26 7a 53 74 72 69 6e 67 5b 69 53 74  n], &zString[iSt
3a92c 72 69 6e 67 5d 2c 20 75 45 73 63 29 20 29 7b 0a  ring], uEsc) ){.
3a92d 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
3a92e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
3a92f 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55        U8_FWD_1_U
3a930 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69  NSAFE(zString, i
3a931 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 7d  String);.      }
3a932 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
3a933 0a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ..    }else if( 
3a934 21 70 72 65 76 45 73 63 61 70 65 20 26 26 20 75  !prevEscape && u
3a935 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43 48 5f 4f  Pattern==MATCH_O
3a936 4e 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43  NE ){.      /* C
3a937 61 73 65 20 32 2e 20 2a 2f 0a 20 20 20 20 20 20  ase 2. */.      
3a938 69 66 28 20 7a 53 74 72 69 6e 67 5b 69 53 74 72  if( zString[iStr
3a939 69 6e 67 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e  ing]==0 ) return
3a93a 20 30 3b 0a 20 20 20 20 20 20 55 38 5f 46 57 44   0;.      U8_FWD
3a93b 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e  _1_UNSAFE(zStrin
3a93c 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a 0a 20 20  g, iString);..  
3a93d 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65    }else if( !pre
3a93e 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74  vEscape && uPatt
3a93f 65 72 6e 3d 3d 75 45 73 63 29 7b 0a 20 20 20 20  ern==uEsc){.    
3a940 20 20 2f 2a 20 43 61 73 65 20 33 2e 20 2a 2f 0a    /* Case 3. */.
3a941 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70 65        prevEscape
3a942 20 3d 20 31 3b 0a 0a 20 20 20 20 7d 65 6c 73 65   = 1;..    }else
3a943 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20  {.      /* Case 
3a944 34 2e 20 2a 2f 0a 20 20 20 20 20 20 55 43 68 61  4. */.      UCha
3a945 72 33 32 20 75 53 74 72 69 6e 67 3b 0a 20 20 20  r32 uString;.   
3a946 20 20 20 55 38 5f 4e 45 58 54 5f 55 4e 53 41 46     U8_NEXT_UNSAF
3a947 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69  E(zString, iStri
3a948 6e 67 2c 20 75 53 74 72 69 6e 67 29 3b 0a 20 20  ng, uString);.  
3a949 20 20 20 20 75 53 74 72 69 6e 67 20 3d 20 75 5f      uString = u_
3a94a 66 6f 6c 64 43 61 73 65 28 75 53 74 72 69 6e 67  foldCase(uString
3a94b 2c 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f 44 45  , U_FOLD_CASE_DE
3a94c 46 41 55 4c 54 29 3b 0a 20 20 20 20 20 20 75 50  FAULT);.      uP
3a94d 61 74 74 65 72 6e 20 3d 20 75 5f 66 6f 6c 64 43  attern = u_foldC
3a94e 61 73 65 28 75 50 61 74 74 65 72 6e 2c 20 55 5f  ase(uPattern, U_
3a94f 46 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c  FOLD_CASE_DEFAUL
3a950 54 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75 53  T);.      if( uS
3a951 74 72 69 6e 67 21 3d 75 50 61 74 74 65 72 6e 20  tring!=uPattern 
3a952 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
3a953 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
3a954 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20     prevEscape = 
3a955 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  0;.    }.  }..  
3a956 72 65 74 75 72 6e 20 7a 53 74 72 69 6e 67 5b 69  return zString[i
3a957 53 74 72 69 6e 67 5d 3d 3d 30 3b 0a 7d 0a 0a 2f  String]==0;.}../
3a958 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3a959 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b 65 28  ion of the like(
3a95a 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20  ) SQL function. 
3a95b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
3a95c 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 65  mplements.** the
3a95d 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45 20 6f   build-in LIKE o
3a95e 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 66 69  perator.  The fi
3a95f 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
3a960 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
3a961 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e 20 61  the.** pattern a
3a962 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  nd the second ar
3a963 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73 74  gument is the st
3a964 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65 20 53  ring.  So, the S
3a965 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a  QL statements:.*
3a966 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c 49 4b  *.**       A LIK
3a967 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d 70  E B.**.** is imp
3a968 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69 6b 65  lemented as like
3a969 28 42 2c 20 41 29 2e 20 49 66 20 74 68 65 72 65  (B, A). If there
3a96a 20 69 73 20 61 6e 20 65 73 63 61 70 65 20 63 68   is an escape ch
3a96b 61 72 61 63 74 65 72 20 45 2c 20 0a 2a 2a 0a 2a  aracter E, .**.*
3a96c 2a 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42  *       A LIKE B
3a96d 20 45 53 43 41 50 45 20 45 0a 2a 2a 0a 2a 2a 20   ESCAPE E.**.** 
3a96e 69 73 20 6d 61 70 70 65 64 20 74 6f 20 6c 69 6b  is mapped to lik
3a96f 65 28 42 2c 20 41 2c 20 45 29 2e 0a 2a 2f 0a 73  e(B, A, E)..*/.s
3a970 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 4c 69  tatic void icuLi
3a971 6b 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  keFunc(.  sqlite
3a972 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
3a973 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c  xt, .  int argc,
3a974 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   .  sqlite3_valu
3a975 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f  e **argv.){.  co
3a976 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
3a977 72 20 2a 7a 41 20 3d 20 73 71 6c 69 74 65 33 5f  r *zA = sqlite3_
3a978 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
3a979 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  0]);.  const uns
3a97a 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 42 20 3d  igned char *zB =
3a97b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
3a97c 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  ext(argv[1]);.  
3a97d 55 43 68 61 72 33 32 20 75 45 73 63 20 3d 20 30  UChar32 uEsc = 0
3a97e 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68  ;..  /* Limit th
3a97f 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
3a980 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74  LIKE or GLOB pat
3a981 74 65 72 6e 20 74 6f 20 61 76 6f 69 64 20 70 72  tern to avoid pr
3a982 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20 64  oblems.  ** of d
3a983 65 65 70 20 72 65 63 75 72 73 69 6f 6e 20 61 6e  eep recursion an
3a984 64 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20 69  d N*N behavior i
3a985 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65  n patternCompare
3a986 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ()..  */.  if( s
3a987 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3a988 65 73 28 61 72 67 76 5b 30 5d 29 3e 53 51 4c 49  es(argv[0])>SQLI
3a989 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54  TE_MAX_LIKE_PATT
3a98a 45 52 4e 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20  ERN_LENGTH ){.  
3a98b 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3a98c 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
3a98d 22 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61  "LIKE or GLOB pa
3a98e 74 74 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65  ttern too comple
3a98f 78 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74  x", -1);.    ret
3a990 75 72 6e 3b 0a 20 20 7d 0a 0a 0a 20 20 69 66 28  urn;.  }...  if(
3a991 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20   argc==3 ){.    
3a992 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63 68  /* The escape ch
3a993 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 6d  aracter string m
3a994 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 61  ust consist of a
3a995 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68   single UTF-8 ch
3a996 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20  aracter..    ** 
3a997 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72  Otherwise, retur
3a998 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20  n an error..    
3a999 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 3d 20 73  */.    int nE= s
3a99a 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3a99b 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  es(argv[2]);.   
3a99c 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
3a99d 63 68 61 72 20 2a 7a 45 20 3d 20 73 71 6c 69 74  char *zE = sqlit
3a99e 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
3a99f 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 6e 74 20  gv[2]);.    int 
3a9a0 69 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 7a  i = 0;.    if( z
3a9a1 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  E==0 ) return;. 
3a9a2 20 20 20 55 38 5f 4e 45 58 54 28 7a 45 2c 20 69     U8_NEXT(zE, i
3a9a3 2c 20 6e 45 2c 20 75 45 73 63 29 3b 0a 20 20 20  , nE, uEsc);.   
3a9a4 20 69 66 28 20 69 21 3d 6e 45 29 7b 0a 20 20 20   if( i!=nE){.   
3a9a5 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
3a9a6 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
3a9a7 20 0a 20 20 20 20 20 20 20 20 20 20 22 45 53 43   .          "ESC
3a9a8 41 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d  APE expression m
3a9a9 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20  ust be a single 
3a9aa 63 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b  character", -1);
3a9ab 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
3a9ac 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
3a9ad 7a 41 20 26 26 20 7a 42 20 29 7b 0a 20 20 20 20  zA && zB ){.    
3a9ae 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
3a9af 6e 74 28 63 6f 6e 74 65 78 74 2c 20 69 63 75 4c  nt(context, icuL
3a9b0 69 6b 65 43 6f 6d 70 61 72 65 28 7a 41 2c 20 7a  ikeCompare(zA, z
3a9b1 42 2c 20 75 45 73 63 29 29 3b 0a 20 20 7d 0a 7d  B, uEsc));.  }.}
3a9b2 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
3a9b3 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
3a9b4 77 68 65 6e 20 61 6e 20 49 43 55 20 66 75 6e 63  when an ICU func
3a9b5 74 69 6f 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d  tion called from
3a9b6 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 69   within.** the i
3a9b7 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
3a9b8 20 61 6e 20 53 51 4c 20 73 63 61 6c 61 72 20 66   an SQL scalar f
3a9b9 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
3a9ba 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  an error..**.** 
3a9bb 54 68 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74  The scalar funct
3a9bc 69 6f 6e 20 63 6f 6e 74 65 78 74 20 70 61 73 73  ion context pass
3a9bd 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
3a9be 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20  argument is .** 
3a9bf 6c 6f 61 64 65 64 20 77 69 74 68 20 61 6e 20 65  loaded with an e
3a9c0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 61 73  rror message bas
3a9c1 65 64 20 6f 6e 20 74 68 65 20 66 6f 6c 6c 6f 77  ed on the follow
3a9c2 69 6e 67 20 74 77 6f 20 61 72 67 73 2e 0a 2a 2f  ing two args..*/
3a9c3 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75  .static void icu
3a9c4 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 0a 20  FunctionError(. 
3a9c5 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
3a9c6 20 2a 70 43 74 78 2c 20 20 20 20 20 20 20 2f 2a   *pCtx,       /*
3a9c7 20 53 51 4c 69 74 65 20 73 63 61 6c 61 72 20 66   SQLite scalar f
3a9c8 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20  unction context 
3a9c9 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3a9ca 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
3a9cb 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 49 43 55    /* Name of ICU
3a9cc 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 66   function that f
3a9cd 61 69 6c 65 64 20 2a 2f 0a 20 20 55 45 72 72 6f  ailed */.  UErro
3a9ce 72 43 6f 64 65 20 65 20 20 20 20 20 20 20 20 20  rCode e         
3a9cf 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72          /* Error
3a9d0 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 62   code returned b
3a9d1 79 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20 2a  y ICU function *
3a9d2 2f 0a 29 7b 0a 20 20 63 68 61 72 20 7a 42 75 66  /.){.  char zBuf
3a9d3 5b 31 32 38 5d 3b 0a 20 20 73 71 6c 69 74 65 33  [128];.  sqlite3
3a9d4 5f 73 6e 70 72 69 6e 74 66 28 31 32 38 2c 20 7a  _snprintf(128, z
3a9d5 42 75 66 2c 20 22 49 43 55 20 65 72 72 6f 72 3a  Buf, "ICU error:
3a9d6 20 25 73 28 29 3a 20 25 73 22 2c 20 7a 4e 61 6d   %s(): %s", zNam
3a9d7 65 2c 20 75 5f 65 72 72 6f 72 4e 61 6d 65 28 65  e, u_errorName(e
3a9d8 29 29 3b 0a 20 20 7a 42 75 66 5b 31 32 37 5d 20  ));.  zBuf[127] 
3a9d9 3d 20 27 5c 30 27 3b 0a 20 20 73 71 6c 69 74 65  = '\0';.  sqlite
3a9da 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70  3_result_error(p
3a9db 43 74 78 2c 20 7a 42 75 66 2c 20 2d 31 29 3b 0a  Ctx, zBuf, -1);.
3a9dc 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f  }../*.** Functio
3a9dd 6e 20 74 6f 20 64 65 6c 65 74 65 20 63 6f 6d 70  n to delete comp
3a9de 69 6c 65 64 20 72 65 67 65 78 70 20 6f 62 6a 65  iled regexp obje
3a9df 63 74 73 2e 20 52 65 67 69 73 74 65 72 65 64 20  cts. Registered 
3a9e0 61 73 0a 2a 2a 20 61 20 64 65 73 74 72 75 63 74  as.** a destruct
3a9e1 6f 72 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  or function with
3a9e2 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78   sqlite3_set_aux
3a9e3 64 61 74 61 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  data()..*/.stati
3a9e4 63 20 76 6f 69 64 20 69 63 75 52 65 67 65 78 70  c void icuRegexp
3a9e5 44 65 6c 65 74 65 28 76 6f 69 64 20 2a 70 29 7b  Delete(void *p){
3a9e6 0a 20 20 55 52 65 67 75 6c 61 72 45 78 70 72 65  .  URegularExpre
3a9e7 73 73 69 6f 6e 20 2a 70 45 78 70 72 20 3d 20 28  ssion *pExpr = (
3a9e8 55 52 65 67 75 6c 61 72 45 78 70 72 65 73 73 69  URegularExpressi
3a9e9 6f 6e 20 2a 29 70 3b 0a 20 20 75 72 65 67 65 78  on *)p;.  uregex
3a9ea 5f 63 6c 6f 73 65 28 70 45 78 70 72 29 3b 0a 7d  _close(pExpr);.}
3a9eb 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
3a9ec 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65  tation of SQLite
3a9ed 20 52 45 47 45 58 50 20 6f 70 65 72 61 74 6f 72   REGEXP operator
3a9ee 2e 20 54 68 69 73 20 73 63 61 6c 61 72 20 66 75  . This scalar fu
3a9ef 6e 63 74 69 6f 6e 20 74 61 6b 65 73 0a 2a 2a 20  nction takes.** 
3a9f0 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20 54  two arguments. T
3a9f1 68 65 20 66 69 72 73 74 20 69 73 20 61 20 72 65  he first is a re
3a9f2 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
3a9f3 20 70 61 74 74 65 72 6e 20 74 6f 20 63 6f 6d 70   pattern to comp
3a9f4 69 6c 65 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e  ile.** the secon
3a9f5 64 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 6f  d is a string to
3a9f6 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20 74   match against t
3a9f7 68 61 74 20 70 61 74 74 65 72 6e 2e 20 49 66 20  hat pattern. If 
3a9f8 65 69 74 68 65 72 20 0a 2a 2a 20 61 72 67 75 6d  either .** argum
3a9f9 65 6e 74 20 69 73 20 61 6e 20 53 51 4c 20 4e 55  ent is an SQL NU
3a9fa 4c 4c 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 49 73  LL, then NULL Is
3a9fb 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
3a9fc 77 69 73 65 2c 20 74 68 65 20 72 65 73 75 6c 74  wise, the result
3a9fd 0a 2a 2a 20 69 73 20 31 20 69 66 20 74 68 65 20  .** is 1 if the 
3a9fe 73 74 72 69 6e 67 20 6d 61 74 63 68 65 73 20 74  string matches t
3a9ff 68 65 20 70 61 74 74 65 72 6e 2c 20 6f 72 20 30  he pattern, or 0
3aa00 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a   otherwise..**.*
3aa01 2a 20 53 51 4c 69 74 65 20 6d 61 70 73 20 74 68  * SQLite maps th
3aa02 65 20 72 65 67 65 78 70 28 29 20 66 75 6e 63 74  e regexp() funct
3aa03 69 6f 6e 20 74 6f 20 74 68 65 20 72 65 67 65 78  ion to the regex
3aa04 70 28 29 20 6f 70 65 72 61 74 6f 72 20 73 75 63  p() operator suc
3aa05 68 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 66 6f  h.** that the fo
3aa06 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 61 72 65 20  llowing two are 
3aa07 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a  equivalent:.**.*
3aa08 2a 20 20 20 20 20 7a 53 74 72 69 6e 67 20 52 45  *     zString RE
3aa09 47 45 58 50 20 7a 50 61 74 74 65 72 6e 0a 2a 2a  GEXP zPattern.**
3aa0a 20 20 20 20 20 72 65 67 65 78 70 28 7a 50 61 74       regexp(zPat
3aa0b 74 65 72 6e 2c 20 7a 53 74 72 69 6e 67 29 0a 2a  tern, zString).*
3aa0c 2a 0a 2a 2a 20 55 73 65 73 20 74 68 65 20 66 6f  *.** Uses the fo
3aa0d 6c 6c 6f 77 69 6e 67 20 49 43 55 20 72 65 67 65  llowing ICU rege
3aa0e 78 70 20 41 50 49 73 3a 0a 2a 2a 0a 2a 2a 20 20  xp APIs:.**.**  
3aa0f 20 20 20 75 72 65 67 65 78 5f 6f 70 65 6e 28 29     uregex_open()
3aa10 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78 5f 6d  .**     uregex_m
3aa11 61 74 63 68 65 73 28 29 0a 2a 2a 20 20 20 20 20  atches().**     
3aa12 75 72 65 67 65 78 5f 63 6c 6f 73 65 28 29 0a 2a  uregex_close().*
3aa13 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  /.static void ic
3aa14 75 52 65 67 65 78 70 46 75 6e 63 28 73 71 6c 69  uRegexpFunc(sqli
3aa15 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20  te3_context *p, 
3aa16 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
3aa17 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
3aa18 7b 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73  {.  UErrorCode s
3aa19 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45  tatus = U_ZERO_E
3aa1a 52 52 4f 52 3b 0a 20 20 55 52 65 67 75 6c 61 72  RROR;.  URegular
3aa1b 45 78 70 72 65 73 73 69 6f 6e 20 2a 70 45 78 70  Expression *pExp
3aa1c 72 3b 0a 20 20 55 42 6f 6f 6c 20 72 65 73 3b 0a  r;.  UBool res;.
3aa1d 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a    const UChar *z
3aa1e 53 74 72 69 6e 67 20 3d 20 73 71 6c 69 74 65 33  String = sqlite3
3aa1f 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 61 70  _value_text16(ap
3aa20 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 49  Arg[1]);..  /* I
3aa21 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20  f the left hand 
3aa22 73 69 64 65 20 6f 66 20 74 68 65 20 72 65 67 65  side of the rege
3aa23 78 70 20 6f 70 65 72 61 74 6f 72 20 69 73 20 4e  xp operator is N
3aa24 55 4c 4c 2c 20 0a 20 20 2a 2a 20 74 68 65 6e 20  ULL, .  ** then 
3aa25 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c  the result is al
3aa26 73 6f 20 4e 55 4c 4c 2e 20 0a 20 20 2a 2f 0a 20  so NULL. .  */. 
3aa27 20 69 66 28 20 21 7a 53 74 72 69 6e 67 20 29 7b   if( !zString ){
3aa28 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
3aa29 0a 0a 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69  ..  pExpr = sqli
3aa2a 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
3aa2b 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 21 70 45  p, 0);.  if( !pE
3aa2c 78 70 72 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  xpr ){.    const
3aa2d 20 55 43 68 61 72 20 2a 7a 50 61 74 74 65 72 6e   UChar *zPattern
3aa2e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3aa2f 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 30 5d  _text16(apArg[0]
3aa30 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 50 61 74  );.    if( !zPat
3aa31 74 65 72 6e 20 29 7b 0a 20 20 20 20 20 20 72 65  tern ){.      re
3aa32 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
3aa33 70 45 78 70 72 20 3d 20 75 72 65 67 65 78 5f 6f  pExpr = uregex_o
3aa34 70 65 6e 28 7a 50 61 74 74 65 72 6e 2c 20 2d 31  pen(zPattern, -1
3aa35 2c 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29  , 0, 0, &status)
3aa36 3b 0a 0a 20 20 20 20 69 66 28 20 55 5f 53 55 43  ;..    if( U_SUC
3aa37 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
3aa38 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65        sqlite3_se
3aa39 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30 2c 20  t_auxdata(p, 0, 
3aa3a 70 45 78 70 72 2c 20 69 63 75 52 65 67 65 78 70  pExpr, icuRegexp
3aa3b 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d 65 6c  Delete);.    }el
3aa3c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
3aa3d 28 21 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  (!pExpr);.      
3aa3e 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72  icuFunctionError
3aa3f 28 70 2c 20 22 75 72 65 67 65 78 5f 6f 70 65 6e  (p, "uregex_open
3aa40 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20  ", status);.    
3aa41 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
3aa42 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 66 69 67    }..  /* Config
3aa43 75 72 65 20 74 68 65 20 74 65 78 74 20 74 68 61  ure the text tha
3aa44 74 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  t the regular ex
3aa45 70 72 65 73 73 69 6f 6e 20 6f 70 65 72 61 74 65  pression operate
3aa46 73 20 6f 6e 2e 20 2a 2f 0a 20 20 75 72 65 67 65  s on. */.  urege
3aa47 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72 2c  x_setText(pExpr,
3aa48 20 7a 53 74 72 69 6e 67 2c 20 2d 31 2c 20 26 73   zString, -1, &s
3aa49 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55  tatus);.  if( !U
3aa4a 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
3aa4b 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74   ){.    icuFunct
3aa4c 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65  ionError(p, "ure
3aa4d 67 65 78 5f 73 65 74 54 65 78 74 22 2c 20 73 74  gex_setText", st
3aa4e 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72  atus);.    retur
3aa4f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 74  n;.  }..  /* Att
3aa50 65 6d 70 74 20 74 68 65 20 6d 61 74 63 68 20 2a  empt the match *
3aa51 2f 0a 20 20 72 65 73 20 3d 20 75 72 65 67 65 78  /.  res = uregex
3aa52 5f 6d 61 74 63 68 65 73 28 70 45 78 70 72 2c 20  _matches(pExpr, 
3aa53 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69  0, &status);.  i
3aa54 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73 74  f( !U_SUCCESS(st
3aa55 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63 75  atus) ){.    icu
3aa56 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c  FunctionError(p,
3aa57 20 22 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73   "uregex_matches
3aa58 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20  ", status);.    
3aa59 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  return;.  }..  /
3aa5a 2a 20 53 65 74 20 74 68 65 20 74 65 78 74 20 74  * Set the text t
3aa5b 68 61 74 20 74 68 65 20 72 65 67 75 6c 61 72 20  hat the regular 
3aa5c 65 78 70 72 65 73 73 69 6f 6e 20 6f 70 65 72 61  expression opera
3aa5d 74 65 73 20 6f 6e 20 74 6f 20 61 20 4e 55 4c 4c  tes on to a NULL
3aa5e 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 54  .  ** pointer. T
3aa5f 68 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c  his is not reall
3aa60 79 20 6e 65 63 65 73 73 61 72 79 2c 20 62 75 74  y necessary, but
3aa61 20 69 74 20 69 73 20 74 69 64 69 65 72 20 74 68   it is tidier th
3aa62 61 6e 20 0a 20 20 2a 2a 20 6c 65 61 76 69 6e 67  an .  ** leaving
3aa63 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70   the regular exp
3aa64 72 65 73 73 69 6f 6e 20 6f 62 6a 65 63 74 20 63  ression object c
3aa65 6f 6e 66 69 67 75 72 65 64 20 77 69 74 68 20 61  onfigured with a
3aa66 6e 20 69 6e 76 61 6c 69 64 0a 20 20 2a 2a 20 70  n invalid.  ** p
3aa67 6f 69 6e 74 65 72 20 61 66 74 65 72 20 74 68 69  ointer after thi
3aa68 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
3aa69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 75 72 65 67 65  ns..  */.  urege
3aa6a 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72 2c  x_setText(pExpr,
3aa6b 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29 3b   0, 0, &status);
3aa6c 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 31 20  ..  /* Return 1 
3aa6d 6f 72 20 30 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  or 0. */.  sqlit
3aa6e 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 70 2c  e3_result_int(p,
3aa6f 20 72 65 73 20 3f 20 31 20 3a 20 30 29 3b 0a 7d   res ? 1 : 0);.}
3aa70 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
3aa71 74 61 74 69 6f 6e 73 20 6f 66 20 73 63 61 6c 61  tations of scala
3aa72 72 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  r functions for 
3aa73 63 61 73 65 20 6d 61 70 70 69 6e 67 20 2d 20 75  case mapping - u
3aa74 70 70 65 72 28 29 20 61 6e 64 20 0a 2a 2a 20 6c  pper() and .** l
3aa75 6f 77 65 72 28 29 2e 20 46 75 6e 63 74 69 6f 6e  ower(). Function
3aa76 20 75 70 70 65 72 28 29 20 63 6f 6e 76 65 72 74   upper() convert
3aa77 73 20 69 74 73 20 69 6e 70 75 74 20 74 6f 20 75  s its input to u
3aa78 70 70 65 72 2d 63 61 73 65 20 28 41 42 43 29 2e  pper-case (ABC).
3aa79 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 6c 6f 77  .** Function low
3aa7a 65 72 28 29 20 63 6f 6e 76 65 72 74 73 20 74 6f  er() converts to
3aa7b 20 6c 6f 77 65 72 2d 63 61 73 65 20 28 61 62 63   lower-case (abc
3aa7c 29 2e 0a 2a 2a 0a 2a 2a 20 49 43 55 20 70 72 6f  )..**.** ICU pro
3aa7d 76 69 64 65 73 20 74 77 6f 20 74 79 70 65 73 20  vides two types 
3aa7e 6f 66 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c  of case mapping,
3aa7f 20 22 67 65 6e 65 72 61 6c 22 20 63 61 73 65 20   "general" case 
3aa80 6d 61 70 70 69 6e 67 20 61 6e 64 0a 2a 2a 20 22  mapping and.** "
3aa81 6c 61 6e 67 75 61 67 65 20 73 70 65 63 69 66 69  language specifi
3aa82 63 22 2e 20 52 65 66 65 72 20 74 6f 20 49 43 55  c". Refer to ICU
3aa83 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
3aa84 6f 72 20 74 68 65 20 64 69 66 66 65 72 65 6e 63  or the differenc
3aa85 65 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 74 68  es.** between th
3aa86 65 20 74 77 6f 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  e two..**.** To 
3aa87 75 74 69 6c 69 73 65 20 22 67 65 6e 65 72 61 6c  utilise "general
3aa88 22 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20  " case mapping, 
3aa89 74 68 65 20 75 70 70 65 72 28 29 20 6f 72 20 6c  the upper() or l
3aa8a 6f 77 65 72 28 29 20 73 63 61 6c 61 72 20 0a 2a  ower() scalar .*
3aa8b 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  * functions are 
3aa8c 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 6f 6e 65  invoked with one
3aa8d 20 61 72 67 75 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a   argument:.**.**
3aa8e 20 20 20 20 20 75 70 70 65 72 28 27 41 42 43 27       upper('ABC'
3aa8f 29 20 2d 3e 20 27 61 62 63 27 0a 2a 2a 20 20 20  ) -> 'abc'.**   
3aa90 20 20 6c 6f 77 65 72 28 27 61 62 63 27 29 20 2d    lower('abc') -
3aa91 3e 20 27 41 42 43 27 0a 2a 2a 0a 2a 2a 20 54 6f  > 'ABC'.**.** To
3aa92 20 61 63 63 65 73 73 20 49 43 55 20 22 6c 61 6e   access ICU "lan
3aa93 67 75 61 67 65 20 73 70 65 63 69 66 69 63 22 20  guage specific" 
3aa94 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 75 70  case mapping, up
3aa95 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28 29  per() or lower()
3aa96 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 69 6e  .** should be in
3aa97 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61  voked with two a
3aa98 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 20 73 65  rguments. The se
3aa99 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
3aa9a 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20   the name.** of 
3aa9b 74 68 65 20 6c 6f 63 61 6c 65 20 74 6f 20 75 73  the locale to us
3aa9c 65 2e 20 50 61 73 73 69 6e 67 20 61 6e 20 65 6d  e. Passing an em
3aa9d 70 74 79 20 73 74 72 69 6e 67 20 28 22 22 29 20  pty string ("") 
3aa9e 6f 72 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75  or SQL NULL valu
3aa9f 65 0a 2a 2a 20 61 73 20 74 68 65 20 73 65 63 6f  e.** as the seco
3aaa0 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nd argument is t
3aaa1 68 65 20 73 61 6d 65 20 61 73 20 69 6e 76 6f 6b  he same as invok
3aaa2 69 6e 67 20 74 68 65 20 31 20 61 72 67 75 6d 65  ing the 1 argume
3aaa3 6e 74 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66  nt version.** of
3aaa4 20 75 70 70 65 72 28 29 20 6f 72 20 6c 6f 77 65   upper() or lowe
3aaa5 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6c  r()..**.**     l
3aaa6 6f 77 65 72 28 27 49 27 2c 20 27 65 6e 5f 75 73  ower('I', 'en_us
3aaa7 27 29 20 2d 3e 20 27 69 27 0a 2a 2a 20 20 20 20  ') -> 'i'.**    
3aaa8 20 6c 6f 77 65 72 28 27 49 27 2c 20 27 74 72 5f   lower('I', 'tr_
3aaa9 74 72 27 29 20 2d 3e 20 27 c4 b1 27 20 28 73 6d  tr') -> '..' (sm
3aaaa 61 6c 6c 20 64 6f 74 6c 65 73 73 20 69 29 0a 2a  all dotless i).*
3aaab 2a 0a 2a 2a 20 68 74 74 70 3a 2f 2f 77 77 77 2e  *.** http://www.
3aaac 69 63 75 2d 70 72 6f 6a 65 63 74 2e 6f 72 67 2f  icu-project.org/
3aaad 75 73 65 72 67 75 69 64 65 2f 70 6f 73 69 78 2e  userguide/posix.
3aaae 68 74 6d 6c 23 63 61 73 65 5f 6d 61 70 70 69 6e  html#case_mappin
3aaaf 67 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  gs.*/.static voi
3aab0 64 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 28  d icuCaseFunc16(
3aab1 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3aab2 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73 71  *p, int nArg, sq
3aab3 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
3aab4 41 72 67 29 7b 0a 20 20 63 6f 6e 73 74 20 55 43  Arg){.  const UC
3aab5 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0a 20 20 55  har *zInput;.  U
3aab6 43 68 61 72 20 2a 7a 4f 75 74 70 75 74 3b 0a 20  Char *zOutput;. 
3aab7 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a 20 20 69   int nInput;.  i
3aab8 6e 74 20 6e 4f 75 74 70 75 74 3b 0a 0a 20 20 55  nt nOutput;..  U
3aab9 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73  ErrorCode status
3aaba 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b   = U_ZERO_ERROR;
3aabb 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3aabc 4c 6f 63 61 6c 65 20 3d 20 30 3b 0a 0a 20 20 61  Locale = 0;..  a
3aabd 73 73 65 72 74 28 6e 41 72 67 3d 3d 31 20 7c 7c  ssert(nArg==1 ||
3aabe 20 6e 41 72 67 3d 3d 32 29 3b 0a 20 20 69 66 28   nArg==2);.  if(
3aabf 20 6e 41 72 67 3d 3d 32 20 29 7b 0a 20 20 20 20   nArg==2 ){.    
3aac0 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73 74  zLocale = (const
3aac1 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
3aac2 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72 67  value_text(apArg
3aac3 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 7a 49 6e  [1]);.  }..  zIn
3aac4 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  put = sqlite3_va
3aac5 6c 75 65 5f 74 65 78 74 31 36 28 61 70 41 72 67  lue_text16(apArg
3aac6 5b 30 5d 29 3b 0a 20 20 69 66 28 20 21 7a 49 6e  [0]);.  if( !zIn
3aac7 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  put ){.    retur
3aac8 6e 3b 0a 20 20 7d 0a 20 20 6e 49 6e 70 75 74 20  n;.  }.  nInput 
3aac9 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3aaca 62 79 74 65 73 31 36 28 61 70 41 72 67 5b 30 5d  bytes16(apArg[0]
3aacb 29 3b 0a 0a 20 20 6e 4f 75 74 70 75 74 20 3d 20  );..  nOutput = 
3aacc 6e 49 6e 70 75 74 20 2a 20 32 20 2b 20 32 3b 0a  nInput * 2 + 2;.
3aacd 20 20 7a 4f 75 74 70 75 74 20 3d 20 73 71 6c 69    zOutput = sqli
3aace 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4f 75 74 70  te3_malloc(nOutp
3aacf 75 74 29 3b 0a 20 20 69 66 28 20 21 7a 4f 75 74  ut);.  if( !zOut
3aad0 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  put ){.    retur
3aad1 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71  n;.  }..  if( sq
3aad2 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
3aad3 70 29 20 29 7b 0a 20 20 20 20 75 5f 73 74 72 54  p) ){.    u_strT
3aad4 6f 55 70 70 65 72 28 7a 4f 75 74 70 75 74 2c 20  oUpper(zOutput, 
3aad5 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75  nOutput/2, zInpu
3aad6 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f  t, nInput/2, zLo
3aad7 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0a  cale, &status);.
3aad8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 5f 73    }else{.    u_s
3aad9 74 72 54 6f 4c 6f 77 65 72 28 7a 4f 75 74 70 75  trToLower(zOutpu
3aada 74 2c 20 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49  t, nOutput/2, zI
3aadb 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20  nput, nInput/2, 
3aadc 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61 74 75 73  zLocale, &status
3aadd 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 55  );.  }..  if( !U
3aade 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
3aadf 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74   ){.    icuFunct
3aae0 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 5f 73  ionError(p, "u_s
3aae1 74 72 54 6f 4c 6f 77 65 72 28 29 2f 75 5f 73 74  trToLower()/u_st
3aae2 72 54 6f 55 70 70 65 72 22 2c 20 73 74 61 74 75  rToUpper", statu
3aae3 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  s);.    return;.
3aae4 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72    }..  sqlite3_r
3aae5 65 73 75 6c 74 5f 74 65 78 74 31 36 28 70 2c 20  esult_text16(p, 
3aae6 7a 4f 75 74 70 75 74 2c 20 2d 31 2c 20 78 46 72  zOutput, -1, xFr
3aae7 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ee);.}../*.** Co
3aae8 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
3aae9 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   destructor func
3aaea 74 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20 61  tion. The pCtx a
3aaeb 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74  rgument points t
3aaec 6f 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72  o.** a UCollator
3aaed 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69   structure previ
3aaee 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
3aaef 75 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28  using ucol_open(
3aaf0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
3aaf1 64 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 44 65  d icuCollationDe
3aaf2 6c 28 76 6f 69 64 20 2a 70 43 74 78 29 7b 0a 20  l(void *pCtx){. 
3aaf3 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20   UCollator *p = 
3aaf4 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43 74  (UCollator *)pCt
3aaf5 78 3b 0a 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28  x;.  ucol_close(
3aaf6 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c  p);.}../*.** Col
3aaf7 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
3aaf8 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
3aaf9 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20 61 72  ion. The pCtx ar
3aafa 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f  gument points to
3aafb 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72 20  .** a UCollator 
3aafc 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69 6f  structure previo
3aafd 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 75  usly allocated u
3aafe 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28 29  sing ucol_open()
3aaff 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3ab00 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c  icuCollationColl
3ab01 28 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 2c 0a  (.  void *pCtx,.
3ab02 20 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63    int nLeft,.  c
3ab03 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4c 65 66 74  onst void *zLeft
3ab04 2c 0a 20 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a  ,.  int nRight,.
3ab05 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 52    const void *zR
3ab06 69 67 68 74 0a 29 7b 0a 20 20 55 43 6f 6c 6c 61  ight.){.  UColla
3ab07 74 69 6f 6e 52 65 73 75 6c 74 20 72 65 73 3b 0a  tionResult res;.
3ab08 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d    UCollator *p =
3ab09 20 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43   (UCollator *)pC
3ab0a 74 78 3b 0a 20 20 72 65 73 20 3d 20 75 63 6f 6c  tx;.  res = ucol
3ab0b 5f 73 74 72 63 6f 6c 6c 28 70 2c 20 28 55 43 68  _strcoll(p, (UCh
3ab0c 61 72 20 2a 29 7a 4c 65 66 74 2c 20 6e 4c 65 66  ar *)zLeft, nLef
3ab0d 74 2f 32 2c 20 28 55 43 68 61 72 20 2a 29 7a 52  t/2, (UChar *)zR
3ab0e 69 67 68 74 2c 20 6e 52 69 67 68 74 2f 32 29 3b  ight, nRight/2);
3ab0f 0a 20 20 73 77 69 74 63 68 28 20 72 65 73 20 29  .  switch( res )
3ab10 7b 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f  {.    case UCOL_
3ab11 4c 45 53 53 3a 20 20 20 20 72 65 74 75 72 6e 20  LESS:    return 
3ab12 2d 31 3b 0a 20 20 20 20 63 61 73 65 20 55 43 4f  -1;.    case UCO
3ab13 4c 5f 47 52 45 41 54 45 52 3a 20 72 65 74 75 72  L_GREATER: retur
3ab14 6e 20 2b 31 3b 0a 20 20 20 20 63 61 73 65 20 55  n +1;.    case U
3ab15 43 4f 4c 5f 45 51 55 41 4c 3a 20 20 20 72 65 74  COL_EQUAL:   ret
3ab16 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
3ab17 65 72 74 28 21 22 55 6e 65 78 70 65 63 74 65 64  ert(!"Unexpected
3ab18 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72   return value fr
3ab19 6f 6d 20 75 63 6f 6c 5f 73 74 72 63 6f 6c 6c 28  om ucol_strcoll(
3ab1a 29 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  )");.  return 0;
3ab1b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
3ab1c 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
3ab1d 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
3ab1e 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69  icu_load_collati
3ab1f 6f 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  on()..**.** This
3ab20 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3ab21 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
3ab22 49 43 55 20 63 6f 6c 6c 61 74 69 6f 6e 20 62 61  ICU collation ba
3ab23 73 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a  sed collation .*
3ab24 2a 20 74 79 70 65 73 20 74 6f 20 61 6e 20 53 51  * types to an SQ
3ab25 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 63 6f  Lite database co
3ab26 6e 6e 65 63 74 69 6f 6e 2e 20 49 74 20 69 73 20  nnection. It is 
3ab27 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 63  intended to be c
3ab28 61 6c 6c 65 64 0a 2a 2a 20 61 73 20 66 6f 6c 6c  alled.** as foll
3ab29 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  ows:.**.**     S
3ab2a 45 4c 45 43 54 20 69 63 75 5f 6c 6f 61 64 5f 63  ELECT icu_load_c
3ab2b 6f 6c 6c 61 74 69 6f 6e 28 3c 6c 6f 63 61 6c 65  ollation(<locale
3ab2c 3e 2c 20 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61  >, <collation-na
3ab2d 6d 65 3e 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 72  me>);.**.** Wher
3ab2e 65 20 3c 6c 6f 63 61 6c 65 3e 20 69 73 20 61 20  e <locale> is a 
3ab2f 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e  string containin
3ab30 67 20 61 6e 20 49 43 55 20 6c 6f 63 61 6c 65 20  g an ICU locale 
3ab31 69 64 65 6e 74 69 66 69 65 72 20 28 69 2e 65 2e  identifier (i.e.
3ab32 0a 2a 2a 20 22 65 6e 5f 41 55 22 2c 20 22 74 72  .** "en_AU", "tr
3ab33 5f 54 52 22 20 65 74 63 2e 29 20 61 6e 64 20 3c  _TR" etc.) and <
3ab34 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e 20  collation-name> 
3ab35 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
3ab36 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20  he.** collation 
3ab37 73 65 71 75 65 6e 63 65 20 74 6f 20 63 72 65 61  sequence to crea
3ab38 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  te..*/.static vo
3ab39 69 64 20 69 63 75 4c 6f 61 64 43 6f 6c 6c 61 74  id icuLoadCollat
3ab3a 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ion(.  sqlite3_c
3ab3b 6f 6e 74 65 78 74 20 2a 70 2c 20 0a 20 20 69 6e  ontext *p, .  in
3ab3c 74 20 6e 41 72 67 2c 20 0a 20 20 73 71 6c 69 74  t nArg, .  sqlit
3ab3d 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
3ab3e 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
3ab3f 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 73  b = (sqlite3 *)s
3ab40 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
3ab41 28 70 29 3b 0a 20 20 55 45 72 72 6f 72 43 6f 64  (p);.  UErrorCod
3ab42 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52  e status = U_ZER
3ab43 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 6f 6e 73 74  O_ERROR;.  const
3ab44 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b 20   char *zLocale; 
3ab45 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 65 20 69       /* Locale i
3ab46 64 65 6e 74 69 66 69 65 72 20 2d 20 28 65 67 2e  dentifier - (eg.
3ab47 20 22 6a 70 5f 4a 50 22 29 20 2a 2f 0a 20 20 63   "jp_JP") */.  c
3ab48 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
3ab49 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20  ;        /* SQL 
3ab4a 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  Collation sequen
3ab4b 63 65 20 6e 61 6d 65 20 28 65 67 2e 20 22 6a 61  ce name (eg. "ja
3ab4c 70 61 6e 65 73 65 22 29 20 2a 2f 0a 20 20 55 43  panese") */.  UC
3ab4d 6f 6c 6c 61 74 6f 72 20 2a 70 55 43 6f 6c 6c 61  ollator *pUColla
3ab4e 74 6f 72 3b 20 20 20 20 2f 2a 20 49 43 55 20 6c  tor;    /* ICU l
3ab4f 69 62 72 61 72 79 20 63 6f 6c 6c 61 74 69 6f 6e  ibrary collation
3ab50 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
3ab51 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
3ab52 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
3ab53 20 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69 74   code from sqlit
3ab54 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
3ab55 69 6f 6e 5f 78 28 29 20 2a 2f 0a 0a 20 20 61 73  ion_x() */..  as
3ab56 73 65 72 74 28 6e 41 72 67 3d 3d 32 29 3b 0a 20  sert(nArg==2);. 
3ab57 20 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73   zLocale = (cons
3ab58 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
3ab59 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72  _value_text(apAr
3ab5a 67 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d  g[0]);.  zName =
3ab5b 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
3ab5c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
3ab5d 74 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20 20  t(apArg[1]);..  
3ab5e 69 66 28 20 21 7a 4c 6f 63 61 6c 65 20 7c 7c 20  if( !zLocale || 
3ab5f 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 72 65  !zName ){.    re
3ab60 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 55 43  turn;.  }..  pUC
3ab61 6f 6c 6c 61 74 6f 72 20 3d 20 75 63 6f 6c 5f 6f  ollator = ucol_o
3ab62 70 65 6e 28 7a 4c 6f 63 61 6c 65 2c 20 26 73 74  pen(zLocale, &st
3ab63 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f  atus);.  if( !U_
3ab64 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29 20  SUCCESS(status) 
3ab65 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74 69  ){.    icuFuncti
3ab66 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 63 6f 6c  onError(p, "ucol
3ab67 5f 6f 70 65 6e 22 2c 20 73 74 61 74 75 73 29 3b  _open", status);
3ab68 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
3ab69 0a 20 20 61 73 73 65 72 74 28 70 29 3b 0a 0a 20  .  assert(p);.. 
3ab6a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72   rc = sqlite3_cr
3ab6b 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
3ab6c 32 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c  2(db, zName, SQL
3ab6d 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64  ITE_UTF16, (void
3ab6e 20 2a 29 70 55 43 6f 6c 6c 61 74 6f 72 2c 20 0a   *)pUCollator, .
3ab6f 20 20 20 20 20 20 69 63 75 43 6f 6c 6c 61 74 69        icuCollati
3ab70 6f 6e 43 6f 6c 6c 2c 20 69 63 75 43 6f 6c 6c 61  onColl, icuColla
3ab71 74 69 6f 6e 44 65 6c 0a 20 20 29 3b 0a 20 20 69  tionDel.  );.  i
3ab72 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3ab73 20 29 7b 0a 20 20 20 20 75 63 6f 6c 5f 63 6c 6f   ){.    ucol_clo
3ab74 73 65 28 70 55 43 6f 6c 6c 61 74 6f 72 29 3b 0a  se(pUCollator);.
3ab75 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3ab76 6c 74 5f 65 72 72 6f 72 28 70 2c 20 22 45 72 72  lt_error(p, "Err
3ab77 6f 72 20 72 65 67 69 73 74 65 72 69 6e 67 20 63  or registering c
3ab78 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  ollation functio
3ab79 6e 22 2c 20 2d 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  n", -1);.  }.}..
3ab7a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74  /*.** Register t
3ab7b 68 65 20 49 43 55 20 65 78 74 65 6e 73 69 6f 6e  he ICU extension
3ab7c 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 74 68 20   functions with 
3ab7d 64 61 74 61 62 61 73 65 20 64 62 2e 0a 2a 2f 0a  database db..*/.
3ab7e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3ab7f 6e 74 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69  nt sqlite3IcuIni
3ab80 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  t(sqlite3 *db){.
3ab81 20 20 73 74 72 75 63 74 20 49 63 75 53 63 61 6c    struct IcuScal
3ab82 61 72 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ar {.    const c
3ab83 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
3ab84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab85 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 6e     /* Function n
3ab86 61 6d 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ame */.    int n
3ab87 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
3ab88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab89 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
3ab8a 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20  f arguments */. 
3ab8b 20 20 20 69 6e 74 20 65 6e 63 3b 20 20 20 20 20     int enc;     
3ab8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab8d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3ab8e 4f 70 74 69 6d 61 6c 20 74 65 78 74 20 65 6e 63  Optimal text enc
3ab8f 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 76 6f 69  oding */.    voi
3ab90 64 20 2a 70 43 6f 6e 74 65 78 74 3b 20 20 20 20  d *pContext;    
3ab91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab92 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
3ab93 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 63 6f  3_user_data() co
3ab94 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 76 6f 69  ntext */.    voi
3ab95 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74  d (*xFunc)(sqlit
3ab96 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
3ab97 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
3ab98 3b 0a 20 20 7d 20 73 63 61 6c 61 72 73 5b 5d 20  ;.  } scalars[] 
3ab99 3d 20 7b 0a 20 20 20 20 7b 22 72 65 67 65 78 70  = {.    {"regexp
3ab9a 22 2c 2d 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ",-1, SQLITE_ANY
3ab9b 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 69 63  ,          0, ic
3ab9c 75 52 65 67 65 78 70 46 75 6e 63 7d 2c 0a 0a 20  uRegexpFunc},.. 
3ab9d 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 31 2c     {"lower",  1,
3ab9e 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 20   SQLITE_UTF16,  
3ab9f 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65        0, icuCase
3aba0 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 6c  Func16},.    {"l
3aba1 6f 77 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54  ower",  2, SQLIT
3aba2 45 5f 55 54 46 31 36 2c 20 20 20 20 20 20 20 20  E_UTF16,        
3aba3 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  0, icuCaseFunc16
3aba4 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c  },.    {"upper",
3aba5 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 31    1, SQLITE_UTF1
3aba6 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75  6, (void*)1, icu
3aba7 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20  CaseFunc16},.   
3aba8 20 7b 22 75 70 70 65 72 22 2c 20 20 32 2c 20 53   {"upper",  2, S
3aba9 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f  QLITE_UTF16, (vo
3abaa 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75  id*)1, icuCaseFu
3abab 6e 63 31 36 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f  nc16},..    {"lo
3abac 77 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54 45  wer",  1, SQLITE
3abad 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20 30  _UTF8,         0
3abae 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d  , icuCaseFunc16}
3abaf 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20  ,.    {"lower", 
3abb0 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
3abb1 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 43           0, icuC
3abb2 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20  aseFunc16},.    
3abb3 7b 22 75 70 70 65 72 22 2c 20 20 31 2c 20 53 51  {"upper",  1, SQ
3abb4 4c 49 54 45 5f 55 54 46 38 2c 20 20 28 76 6f 69  LITE_UTF8,  (voi
3abb5 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75 6e  d*)1, icuCaseFun
3abb6 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65  c16},.    {"uppe
3abb7 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55  r",  2, SQLITE_U
3abb8 54 46 38 2c 20 20 28 76 6f 69 64 2a 29 31 2c 20  TF8,  (void*)1, 
3abb9 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a  icuCaseFunc16},.
3abba 0a 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20 20  .    {"like",   
3abbb 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  2, SQLITE_UTF8, 
3abbc 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 4c 69          0, icuLi
3abbd 6b 65 46 75 6e 63 7d 2c 0a 20 20 20 20 7b 22 6c  keFunc},.    {"l
3abbe 69 6b 65 22 2c 20 20 20 33 2c 20 53 51 4c 49 54  ike",   3, SQLIT
3abbf 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20  E_UTF8,         
3abc0 30 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d 2c  0, icuLikeFunc},
3abc1 0a 0a 20 20 20 20 7b 22 69 63 75 5f 6c 6f 61 64  ..    {"icu_load
3abc2 5f 63 6f 6c 6c 61 74 69 6f 6e 22 2c 20 20 32 2c  _collation",  2,
3abc3 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 28 76   SQLITE_UTF8, (v
3abc4 6f 69 64 2a 29 64 62 2c 20 69 63 75 4c 6f 61 64  oid*)db, icuLoad
3abc5 43 6f 6c 6c 61 74 69 6f 6e 7d 2c 0a 20 20 7d 3b  Collation},.  };
3abc6 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
3abc7 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b  ITE_OK;.  int i;
3abc8 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ..  for(i=0; rc=
3abc9 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
3abca 28 73 69 7a 65 6f 66 28 73 63 61 6c 61 72 73 29  (sizeof(scalars)
3abcb 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 49  /sizeof(struct I
3abcc 63 75 53 63 61 6c 61 72 29 29 3b 20 69 2b 2b 29  cuScalar)); i++)
3abcd 7b 0a 20 20 20 20 73 74 72 75 63 74 20 49 63 75  {.    struct Icu
3abce 53 63 61 6c 61 72 20 2a 70 20 3d 20 26 73 63 61  Scalar *p = &sca
3abcf 6c 61 72 73 5b 69 5d 3b 0a 20 20 20 20 72 63 20  lars[i];.    rc 
3abd0 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
3abd1 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 20 20 20  _function(.     
3abd2 20 20 20 64 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c     db, p->zName,
3abd3 20 70 2d 3e 6e 41 72 67 2c 20 70 2d 3e 65 6e 63   p->nArg, p->enc
3abd4 2c 20 70 2d 3e 70 43 6f 6e 74 65 78 74 2c 20 70  , p->pContext, p
3abd5 2d 3e 78 46 75 6e 63 2c 20 30 2c 20 30 0a 20 20  ->xFunc, 0, 0.  
3abd6 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75    );.  }..  retu
3abd7 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53  rn rc;.}..#if !S
3abd8 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54  QLITE_CORE.SQLIT
3abd9 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3abda 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
3abdb 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
3abdc 20 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72   .  char **pzErr
3abdd 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c  Msg,.  const sql
3abde 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
3abdf 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c  s *pApi.){.  SQL
3abe0 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
3abe1 49 54 32 28 70 41 70 69 29 0a 20 20 72 65 74 75  IT2(pApi).  retu
3abe2 72 6e 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69  rn sqlite3IcuIni
3abe3 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  t(db);.}.#endif.
3abe4 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
3abe5 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
3abe6 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  icu.c **********
3abe7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abe8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abe9 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
3abea 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
3abeb 69 6c 65 20 66 74 73 33 5f 69 63 75 2e 63 20 2a  ile fts3_icu.c *
3abec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abee 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
3abef 30 30 37 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a  007 June 22.**.*
3abf0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
3abf1 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
3abf2 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
3abf3 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
3abf4 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
3abf5 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
3abf6 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
3abf7 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
3abf8 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
3abf9 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
3abfa 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
3abfb 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
3abfc 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
3abfd 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
3abfe 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
3abff 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
3ac00 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
3ac01 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
3ac02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ac03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ac04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ac05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
3ac06 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
3ac07 74 73 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 66  ts a tokenizer f
3ac08 6f 72 20 66 74 73 33 20 62 61 73 65 64 20 6f 6e  or fts3 based on
3ac09 20 74 68 65 20 49 43 55 20 6c 69 62 72 61 72 79   the ICU library
3ac0a 2e 0a 2a 2a 20 0a 2a 2a 20 24 49 64 3a 20 66 74  ..** .** $Id: ft
3ac0b 73 33 5f 69 63 75 2e 63 2c 76 20 31 2e 33 20 32  s3_icu.c,v 1.3 2
3ac0c 30 30 38 2f 30 39 2f 30 31 20 31 38 3a 33 34 3a  008/09/01 18:34:
3ac0d 32 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  20 danielk1977 E
3ac0e 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64 65  xp $.*/..#if !de
3ac0f 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
3ac10 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
3ac11 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
3ac12 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ).#ifdef SQLITE_
3ac13 45 4e 41 42 4c 45 5f 49 43 55 0a 0a 0a 23 69 6e  ENABLE_ICU...#in
3ac14 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75  clude <unicode/u
3ac15 62 72 6b 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  brk.h>.#include 
3ac16 3c 75 6e 69 63 6f 64 65 2f 75 74 66 31 36 2e 68  <unicode/utf16.h
3ac17 3e 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  >..typedef struc
3ac18 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 49  t IcuTokenizer I
3ac19 63 75 54 6f 6b 65 6e 69 7a 65 72 3b 0a 74 79 70  cuTokenizer;.typ
3ac1a 65 64 65 66 20 73 74 72 75 63 74 20 49 63 75 43  edef struct IcuC
3ac1b 75 72 73 6f 72 20 49 63 75 43 75 72 73 6f 72 3b  ursor IcuCursor;
3ac1c 0a 0a 73 74 72 75 63 74 20 49 63 75 54 6f 6b 65  ..struct IcuToke
3ac1d 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74 65  nizer {.  sqlite
3ac1e 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65  3_tokenizer base
3ac1f 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c  ;.  char *zLocal
3ac20 65 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 49 63  e;.};..struct Ic
3ac21 75 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69  uCursor {.  sqli
3ac22 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
3ac23 72 73 6f 72 20 62 61 73 65 3b 0a 0a 20 20 55 42  rsor base;..  UB
3ac24 72 65 61 6b 49 74 65 72 61 74 6f 72 20 2a 70 49  reakIterator *pI
3ac25 74 65 72 3b 20 20 20 20 20 20 2f 2a 20 49 43 55  ter;      /* ICU
3ac26 20 62 72 65 61 6b 2d 69 74 65 72 61 74 6f 72 20   break-iterator 
3ac27 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20  object */.  int 
3ac28 6e 43 68 61 72 3b 20 20 20 20 20 20 20 20 20 20  nChar;          
3ac29 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3ac2a 72 20 6f 66 20 55 43 68 61 72 20 65 6c 65 6d 65  r of UChar eleme
3ac2b 6e 74 73 20 69 6e 20 70 49 6e 70 75 74 20 2a 2f  nts in pInput */
3ac2c 0a 20 20 55 43 68 61 72 20 2a 61 43 68 61 72 3b  .  UChar *aChar;
3ac2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3ac2e 2a 20 43 6f 70 79 20 6f 66 20 69 6e 70 75 74 20  * Copy of input 
3ac2f 75 73 69 6e 67 20 75 74 66 2d 31 36 20 65 6e 63  using utf-16 enc
3ac30 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 2a  oding */.  int *
3ac31 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  aOffset;        
3ac32 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
3ac33 73 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63  s of each charac
3ac34 74 65 72 20 69 6e 20 75 74 66 2d 38 20 69 6e 70  ter in utf-8 inp
3ac35 75 74 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 42 75  ut */..  int nBu
3ac36 66 66 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a 42  ffer;.  char *zB
3ac37 75 66 66 65 72 3b 0a 0a 20 20 69 6e 74 20 69 54  uffer;..  int iT
3ac38 6f 6b 65 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  oken;.};../*.** 
3ac39 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b  Create a new tok
3ac3a 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e  enizer instance.
3ac3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
3ac3c 63 75 43 72 65 61 74 65 28 0a 20 20 69 6e 74 20  cuCreate(.  int 
3ac3d 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20  argc,           
3ac3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ac3f 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
3ac40 74 72 69 65 73 20 69 6e 20 61 72 67 76 5b 5d 20  tries in argv[] 
3ac41 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3ac42 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 20 20  * const *argv,  
3ac43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
3ac44 65 6e 69 7a 65 72 20 63 72 65 61 74 69 6f 6e 20  enizer creation 
3ac45 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 73  arguments */.  s
3ac46 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3ac47 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 20   **ppTokenizer  
3ac48 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65 61      /* OUT: Crea
3ac49 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  ted tokenizer */
3ac4a 0a 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a  .){.  IcuTokeniz
3ac4b 65 72 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 20 3d  er *p;.  int n =
3ac4c 20 30 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3e   0;..  if( argc>
3ac4d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74 72  0 ){.    n = str
3ac4e 6c 65 6e 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a  len(argv[0])+1;.
3ac4f 20 20 7d 0a 20 20 70 20 3d 20 28 49 63 75 54 6f    }.  p = (IcuTo
3ac50 6b 65 6e 69 7a 65 72 20 2a 29 73 71 6c 69 74 65  kenizer *)sqlite
3ac51 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
3ac52 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 2b 6e 29  IcuTokenizer)+n)
3ac53 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20  ;.  if( !p ){.  
3ac54 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3ac55 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d  NOMEM;.  }.  mem
3ac56 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66  set(p, 0, sizeof
3ac57 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 29 3b  (IcuTokenizer));
3ac58 0a 0a 20 20 69 66 28 20 6e 20 29 7b 0a 20 20 20  ..  if( n ){.   
3ac59 20 70 2d 3e 7a 4c 6f 63 61 6c 65 20 3d 20 28 63   p->zLocale = (c
3ac5a 68 61 72 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  har *)&p[1];.   
3ac5b 20 6d 65 6d 63 70 79 28 70 2d 3e 7a 4c 6f 63 61   memcpy(p->zLoca
3ac5c 6c 65 2c 20 61 72 67 76 5b 30 5d 2c 20 6e 29 3b  le, argv[0], n);
3ac5d 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e  .  }..  *ppToken
3ac5e 69 7a 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f  izer = (sqlite3_
3ac5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 3b 0a 0a  tokenizer *)p;..
3ac60 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3ac61 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73  OK;.}../*.** Des
3ac62 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72  troy a tokenizer
3ac63 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
3ac64 63 75 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65  cuDestroy(sqlite
3ac65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
3ac66 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 49 63 75 54  kenizer){.  IcuT
3ac67 6f 6b 65 6e 69 7a 65 72 20 2a 70 20 3d 20 28 49  okenizer *p = (I
3ac68 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 54  cuTokenizer *)pT
3ac69 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69  okenizer;.  sqli
3ac6a 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72  te3_free(p);.  r
3ac6b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3ac6c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72  .}../*.** Prepar
3ac6d 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e  e to begin token
3ac6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c  izing a particul
3ac6f 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20  ar string.  The 
3ac70 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20  input.** string 
3ac71 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20  to be tokenized 
3ac72 69 73 20 70 49 6e 70 75 74 5b 30 2e 2e 6e 42 79  is pInput[0..nBy
3ac73 74 65 73 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f  tes-1].  A curso
3ac74 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e 63  r.** used to inc
3ac75 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e  rementally token
3ac76 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e 67 20  ize this string 
3ac77 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 0a  is returned in .
3ac78 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f  ** *ppCursor..*/
3ac79 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 4f  .static int icuO
3ac7a 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  pen(.  sqlite3_t
3ac7b 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
3ac7c 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  izer,         /*
3ac7d 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   The tokenizer *
3ac7e 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
3ac7f 7a 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 20  zInput,         
3ac80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
3ac81 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
3ac82 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20 20 20  int nInput,     
3ac83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ac84 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
3ac85 20 6f 66 20 7a 49 6e 70 75 74 20 69 6e 20 62 79   of zInput in by
3ac86 74 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  tes */.  sqlite3
3ac87 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
3ac88 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20 20  r **ppCursor    
3ac89 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61  /* OUT: Tokeniza
3ac8a 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29  tion cursor */.)
3ac8b 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72  {.  IcuTokenizer
3ac8c 20 2a 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69   *p = (IcuTokeni
3ac8d 7a 65 72 20 2a 29 70 54 6f 6b 65 6e 69 7a 65 72  zer *)pTokenizer
3ac8e 3b 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a 70  ;.  IcuCursor *p
3ac8f 43 73 72 3b 0a 0a 20 20 63 6f 6e 73 74 20 69 6e  Csr;..  const in
3ac90 74 33 32 5f 74 20 6f 70 74 20 3d 20 55 5f 46 4f  t32_t opt = U_FO
3ac91 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 3b  LD_CASE_DEFAULT;
3ac92 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74  .  UErrorCode st
3ac93 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52  atus = U_ZERO_ER
3ac94 52 4f 52 3b 0a 20 20 69 6e 74 20 6e 43 68 61 72  ROR;.  int nChar
3ac95 3b 0a 0a 20 20 55 43 68 61 72 33 32 20 63 3b 0a  ;..  UChar32 c;.
3ac96 20 20 69 6e 74 20 69 49 6e 70 75 74 20 3d 20 30    int iInput = 0
3ac97 3b 0a 20 20 69 6e 74 20 69 4f 75 74 20 3d 20 30  ;.  int iOut = 0
3ac98 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d  ;..  *ppCursor =
3ac99 20 30 3b 0a 0a 20 20 69 66 28 20 6e 49 6e 70 75   0;..  if( nInpu
3ac9a 74 3c 30 20 29 7b 0a 20 20 20 20 6e 49 6e 70 75  t<0 ){.    nInpu
3ac9b 74 20 3d 20 73 74 72 6c 65 6e 28 7a 49 6e 70 75  t = strlen(zInpu
3ac9c 74 29 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20  t);.  }.  nChar 
3ac9d 3d 20 6e 49 6e 70 75 74 2b 31 3b 0a 20 20 70 43  = nInput+1;.  pC
3ac9e 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f 72 20  sr = (IcuCursor 
3ac9f 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
3aca0 28 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49  (.      sizeof(I
3aca1 63 75 43 75 72 73 6f 72 29 20 2b 20 20 20 20 20  cuCursor) +     
3aca2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 63             /* Ic
3aca3 75 43 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20  uCursor */.     
3aca4 20 6e 43 68 61 72 20 2a 20 73 69 7a 65 6f 66 28   nChar * sizeof(
3aca5 55 43 68 61 72 29 20 2b 20 20 20 20 20 20 20 20  UChar) +        
3aca6 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f 72      /* IcuCursor
3aca7 2e 61 43 68 61 72 5b 5d 20 2a 2f 0a 20 20 20 20  .aChar[] */.    
3aca8 20 20 28 6e 43 68 61 72 2b 31 29 20 2a 20 73 69    (nChar+1) * si
3aca9 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20 20  zeof(int)       
3acaa 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f       /* IcuCurso
3acab 72 2e 61 4f 66 66 73 65 74 5b 5d 20 2a 2f 0a 20  r.aOffset[] */. 
3acac 20 29 3b 0a 20 20 69 66 28 20 21 70 43 73 72 20   );.  if( !pCsr 
3acad 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
3acae 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
3acaf 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2c 20 30    memset(pCsr, 0
3acb0 2c 20 73 69 7a 65 6f 66 28 49 63 75 43 75 72 73  , sizeof(IcuCurs
3acb1 6f 72 29 29 3b 0a 20 20 70 43 73 72 2d 3e 61 43  or));.  pCsr->aC
3acb2 68 61 72 20 3d 20 28 55 43 68 61 72 20 2a 29 26  har = (UChar *)&
3acb3 70 43 73 72 5b 31 5d 3b 0a 20 20 70 43 73 72 2d  pCsr[1];.  pCsr-
3acb4 3e 61 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 20  >aOffset = (int 
3acb5 2a 29 26 70 43 73 72 2d 3e 61 43 68 61 72 5b 6e  *)&pCsr->aChar[n
3acb6 43 68 61 72 5d 3b 0a 0a 20 20 70 43 73 72 2d 3e  Char];..  pCsr->
3acb7 61 4f 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d 20  aOffset[iOut] = 
3acb8 69 49 6e 70 75 74 3b 0a 20 20 55 38 5f 4e 45 58  iInput;.  U8_NEX
3acb9 54 28 7a 49 6e 70 75 74 2c 20 69 49 6e 70 75 74  T(zInput, iInput
3acba 2c 20 6e 49 6e 70 75 74 2c 20 63 29 3b 20 0a 20  , nInput, c); . 
3acbb 20 77 68 69 6c 65 28 20 63 3e 30 20 29 7b 0a 20   while( c>0 ){. 
3acbc 20 20 20 69 6e 74 20 69 73 45 72 72 6f 72 20 3d     int isError =
3acbd 20 30 3b 0a 20 20 20 20 63 20 3d 20 75 5f 66 6f   0;.    c = u_fo
3acbe 6c 64 43 61 73 65 28 63 2c 20 6f 70 74 29 3b 0a  ldCase(c, opt);.
3acbf 20 20 20 20 55 31 36 5f 41 50 50 45 4e 44 28 70      U16_APPEND(p
3acc0 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f 75 74  Csr->aChar, iOut
3acc1 2c 20 6e 43 68 61 72 2c 20 63 2c 20 69 73 45 72  , nChar, c, isEr
3acc2 72 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 69 73  ror);.    if( is
3acc3 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 73  Error ){.      s
3acc4 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72  qlite3_free(pCsr
3acc5 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
3acc6 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
3acc7 20 20 7d 0a 20 20 20 20 70 43 73 72 2d 3e 61 4f    }.    pCsr->aO
3acc8 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d 20 69 49  ffset[iOut] = iI
3acc9 6e 70 75 74 3b 0a 0a 20 20 20 20 69 66 28 20 69  nput;..    if( i
3acca 49 6e 70 75 74 3c 6e 49 6e 70 75 74 20 29 7b 0a  Input<nInput ){.
3accb 20 20 20 20 20 20 55 38 5f 4e 45 58 54 28 7a 49        U8_NEXT(zI
3accc 6e 70 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e 49  nput, iInput, nI
3accd 6e 70 75 74 2c 20 63 29 3b 0a 20 20 20 20 7d 65  nput, c);.    }e
3acce 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d 20 30  lse{.      c = 0
3accf 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70  ;.    }.  }..  p
3acd0 43 73 72 2d 3e 70 49 74 65 72 20 3d 20 75 62 72  Csr->pIter = ubr
3acd1 6b 5f 6f 70 65 6e 28 55 42 52 4b 5f 57 4f 52 44  k_open(UBRK_WORD
3acd2 2c 20 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 70 43  , p->zLocale, pC
3acd3 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f 75 74 2c  sr->aChar, iOut,
3acd4 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69 66 28   &status);.  if(
3acd5 20 21 55 5f 53 55 43 43 45 53 53 28 73 74 61 74   !U_SUCCESS(stat
3acd6 75 73 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  us) ){.    sqlit
3acd7 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20  e3_free(pCsr);. 
3acd8 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3acd9 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 43  _ERROR;.  }.  pC
3acda 73 72 2d 3e 6e 43 68 61 72 20 3d 20 69 4f 75 74  sr->nChar = iOut
3acdb 3b 0a 0a 20 20 75 62 72 6b 5f 66 69 72 73 74 28  ;..  ubrk_first(
3acdc 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20  pCsr->pIter);.  
3acdd 2a 70 70 43 75 72 73 6f 72 20 3d 20 28 73 71 6c  *ppCursor = (sql
3acde 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
3acdf 75 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a 20 20  ursor *)pCsr;.  
3ace0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3ace1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
3ace2 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   a tokenization 
3ace3 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c  cursor previousl
3ace4 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63 61  y opened by a ca
3ace5 6c 6c 20 74 6f 20 69 63 75 4f 70 65 6e 28 29 2e  ll to icuOpen().
3ace6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
3ace7 63 75 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  cuClose(sqlite3_
3ace8 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
3ace9 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 49 63   *pCursor){.  Ic
3acea 75 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20  uCursor *pCsr = 
3aceb 28 49 63 75 43 75 72 73 6f 72 20 2a 29 70 43 75  (IcuCursor *)pCu
3acec 72 73 6f 72 3b 0a 20 20 75 62 72 6b 5f 63 6c 6f  rsor;.  ubrk_clo
3aced 73 65 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b  se(pCsr->pIter);
3acee 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3acef 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 29 3b 0a  pCsr->zBuffer);.
3acf0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
3acf1 43 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  Csr);.  return S
3acf2 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
3acf3 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 6e  ** Extract the n
3acf4 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61  ext token from a
3acf5 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75   tokenization cu
3acf6 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rsor..*/.static 
3acf7 69 6e 74 20 69 63 75 4e 65 78 74 28 0a 20 20 73  int icuNext(.  s
3acf8 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3acf9 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
3acfa 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65 74  ,  /* Cursor ret
3acfb 75 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65 4f  urned by simpleO
3acfc 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  pen */.  const c
3acfd 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20  har **ppToken,  
3acfe 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3acff 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20 69 73  OUT: *ppToken is
3ad00 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20   the token text 
3ad01 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65  */.  int *pnByte
3ad02 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
3ad03 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
3ad04 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
3ad05 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69   in token */.  i
3ad06 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65  nt *piStartOffse
3ad07 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
3ad08 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74     /* OUT: Start
3ad09 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f  ing offset of to
3ad0a 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
3ad0b 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 20 20  EndOffset,      
3ad0c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3ad0d 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73  OUT: Ending offs
3ad0e 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20  et of token */. 
3ad0f 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e   int *piPosition
3ad10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ad11 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73       /* OUT: Pos
3ad12 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66  ition integer of
3ad13 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 49   token */.){.  I
3ad14 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  cuCursor *pCsr =
3ad15 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 70 43   (IcuCursor *)pC
3ad16 75 72 73 6f 72 3b 0a 0a 20 20 69 6e 74 20 69 53  ursor;..  int iS
3ad17 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20  tart = 0;.  int 
3ad18 69 45 6e 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20  iEnd = 0;.  int 
3ad19 6e 42 79 74 65 20 3d 20 30 3b 0a 0a 20 20 77 68  nByte = 0;..  wh
3ad1a 69 6c 65 28 20 69 53 74 61 72 74 3d 3d 69 45 6e  ile( iStart==iEn
3ad1b 64 20 29 7b 0a 20 20 20 20 55 43 68 61 72 33 32  d ){.    UChar32
3ad1c 20 63 3b 0a 0a 20 20 20 20 69 53 74 61 72 74 20   c;..    iStart 
3ad1d 3d 20 75 62 72 6b 5f 63 75 72 72 65 6e 74 28 70  = ubrk_current(p
3ad1e 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 20  Csr->pIter);.   
3ad1f 20 69 45 6e 64 20 3d 20 75 62 72 6b 5f 6e 65 78   iEnd = ubrk_nex
3ad20 74 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a  t(pCsr->pIter);.
3ad21 20 20 20 20 69 66 28 20 69 45 6e 64 3d 3d 55 42      if( iEnd==UB
3ad22 52 4b 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20  RK_DONE ){.     
3ad23 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
3ad24 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ONE;.    }..    
3ad25 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69 45  while( iStart<iE
3ad26 6e 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  nd ){.      int 
3ad27 69 57 68 69 74 65 20 3d 20 69 53 74 61 72 74 3b  iWhite = iStart;
3ad28 0a 20 20 20 20 20 20 55 38 5f 4e 45 58 54 28 70  .      U8_NEXT(p
3ad29 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 57 68 69  Csr->aChar, iWhi
3ad2a 74 65 2c 20 70 43 73 72 2d 3e 6e 43 68 61 72 2c  te, pCsr->nChar,
3ad2b 20 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75   c);.      if( u
3ad2c 5f 69 73 73 70 61 63 65 28 63 29 20 29 7b 0a 20  _isspace(c) ){. 
3ad2d 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20         iStart = 
3ad2e 69 57 68 69 74 65 3b 0a 20 20 20 20 20 20 7d 65  iWhite;.      }e
3ad2f 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65  lse{.        bre
3ad30 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
3ad31 7d 0a 20 20 20 20 61 73 73 65 72 74 28 69 53 74  }.    assert(iSt
3ad32 61 72 74 3c 3d 69 45 6e 64 29 3b 0a 20 20 7d 0a  art<=iEnd);.  }.
3ad33 0a 20 20 64 6f 20 7b 0a 20 20 20 20 55 45 72 72  .  do {.    UErr
3ad34 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20  orCode status = 
3ad35 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20  U_ZERO_ERROR;.  
3ad36 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b 0a 20    if( nByte ){. 
3ad37 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 20       char *zNew 
3ad38 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
3ad39 63 28 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 2c  c(pCsr->zBuffer,
3ad3a 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 69   nByte);.      i
3ad3b 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20 20 20  f( !zNew ){.    
3ad3c 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3ad3d 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
3ad3e 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 7a 42 75  .      pCsr->zBu
3ad3f 66 66 65 72 20 3d 20 7a 4e 65 77 3b 0a 20 20 20  ffer = zNew;.   
3ad40 20 20 20 70 43 73 72 2d 3e 6e 42 75 66 66 65 72     pCsr->nBuffer
3ad41 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 0a   = nByte;.    }.
3ad42 0a 20 20 20 20 75 5f 73 74 72 54 6f 55 54 46 38  .    u_strToUTF8
3ad43 28 0a 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e  (.        pCsr->
3ad44 7a 42 75 66 66 65 72 2c 20 70 43 73 72 2d 3e 6e  zBuffer, pCsr->n
3ad45 42 75 66 66 65 72 2c 20 26 6e 42 79 74 65 2c 20  Buffer, &nByte, 
3ad46 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 76 61 72     /* Output var
3ad47 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 70 43  s */.        &pC
3ad48 73 72 2d 3e 61 43 68 61 72 5b 69 53 74 61 72 74  sr->aChar[iStart
3ad49 5d 2c 20 69 45 6e 64 2d 69 53 74 61 72 74 2c 20  ], iEnd-iStart, 
3ad4a 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 76        /* Input v
3ad4b 61 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 26  ars */.        &
3ad4c 73 74 61 74 75 73 20 20 20 20 20 20 20 20 20 20  status          
3ad4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ad4e 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
3ad4f 74 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72  t success/failur
3ad50 65 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 7d 20  e */.    );.  } 
3ad51 77 68 69 6c 65 28 20 6e 42 79 74 65 3e 70 43 73  while( nByte>pCs
3ad52 72 2d 3e 6e 42 75 66 66 65 72 20 29 3b 0a 0a 20  r->nBuffer );.. 
3ad53 20 2a 70 70 54 6f 6b 65 6e 20 3d 20 70 43 73 72   *ppToken = pCsr
3ad54 2d 3e 7a 42 75 66 66 65 72 3b 0a 20 20 2a 70 6e  ->zBuffer;.  *pn
3ad55 42 79 74 65 73 20 3d 20 6e 42 79 74 65 3b 0a 20  Bytes = nByte;. 
3ad56 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20   *piStartOffset 
3ad57 3d 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b  = pCsr->aOffset[
3ad58 69 53 74 61 72 74 5d 3b 0a 20 20 2a 70 69 45 6e  iStart];.  *piEn
3ad59 64 4f 66 66 73 65 74 20 3d 20 70 43 73 72 2d 3e  dOffset = pCsr->
3ad5a 61 4f 66 66 73 65 74 5b 69 45 6e 64 5d 3b 0a 20  aOffset[iEnd];. 
3ad5b 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20 70   *piPosition = p
3ad5c 43 73 72 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a  Csr->iToken++;..
3ad5d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3ad5e 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OK;.}../*.** The
3ad5f 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
3ad60 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
3ad61 74 68 65 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e  the simple token
3ad62 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63  izer.*/.static c
3ad63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
3ad64 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 63  enizer_module ic
3ad65 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  uTokenizerModule
3ad66 20 3d 20 7b 0a 20 20 30 2c 20 20 20 20 20 20 20   = {.  0,       
3ad67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ad68 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
3ad69 2a 2f 0a 20 20 69 63 75 43 72 65 61 74 65 2c 20  */.  icuCreate, 
3ad6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ad6b 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20 2a 2f    /* xCreate  */
3ad6c 0a 20 20 69 63 75 44 65 73 74 72 6f 79 2c 20 20  .  icuDestroy,  
3ad6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ad6e 2f 2a 20 78 43 72 65 61 74 65 20 20 2a 2f 0a 20  /* xCreate  */. 
3ad6f 20 69 63 75 4f 70 65 6e 2c 20 20 20 20 20 20 20   icuOpen,       
3ad70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3ad71 20 78 4f 70 65 6e 20 20 20 20 2a 2f 0a 20 20 69   xOpen    */.  i
3ad72 63 75 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  cuClose,        
3ad73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3ad74 43 6c 6f 73 65 20 20 20 2a 2f 0a 20 20 69 63 75  Close   */.  icu
3ad75 4e 65 78 74 2c 20 20 20 20 20 20 20 20 20 20 20  Next,           
3ad76 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4e 65            /* xNe
3ad77 78 74 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  xt    */.};../*.
3ad78 2a 2a 20 53 65 74 20 2a 70 70 4d 6f 64 75 6c 65  ** Set *ppModule
3ad79 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
3ad7a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3ad7b 6f 66 20 74 68 65 20 49 43 55 20 74 6f 6b 65 6e  of the ICU token
3ad7c 69 7a 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  izer..*/.SQLITE_
3ad7d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
3ad7e 69 74 65 33 46 74 73 33 49 63 75 54 6f 6b 65 6e  ite3Fts3IcuToken
3ad7f 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71  izerModule(.  sq
3ad80 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3ad81 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
3ad82 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d  Module.){.  *ppM
3ad83 6f 64 75 6c 65 20 3d 20 26 69 63 75 54 6f 6b 65  odule = &icuToke
3ad84 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a  nizerModule;.}..
3ad85 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65  #endif /* define
3ad86 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
3ad87 49 43 55 29 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  ICU) */.#endif /
3ad88 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
3ad89 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3ad8a 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3ad8b 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
3ad8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
3ad8d 66 20 66 74 73 33 5f 69 63 75 2e 63 20 2a 2a 2a  f fts3_icu.c ***
3ad8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ad8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ad90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a                 *********/.